blob: 5dfc414792c132d7cbd59663fe11e32e677ff0ee [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"
Zhu Yib481de92007-09-25 17:54:57 -070049#include "iwl-4965.h"
Tomas Winklerfee12472008-04-03 16:05:21 -070050#include "iwl-core.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
Zhu Yib481de92007-09-25 17:54:57 -0700499
Zhu Yib481de92007-09-25 17:54:57 -0700500
501/*************** HOST COMMAND QUEUE FUNCTIONS *****/
502
Zhu Yib481de92007-09-25 17:54:57 -0700503/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800504 * iwl4965_enqueue_hcmd - enqueue a uCode command
Zhu Yib481de92007-09-25 17:54:57 -0700505 * @priv: device private data point
506 * @cmd: a point to the ucode command structure
507 *
508 * The function returns < 0 values to indicate the operation is
509 * failed. On success, it turns the index (> 0) of command in the
510 * command queue.
511 */
Tomas Winkler857485c2008-03-21 13:53:44 -0700512int iwl4965_enqueue_hcmd(struct iwl_priv *priv, struct iwl_host_cmd *cmd)
Zhu Yib481de92007-09-25 17:54:57 -0700513{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800514 struct iwl4965_tx_queue *txq = &priv->txq[IWL_CMD_QUEUE_NUM];
515 struct iwl4965_queue *q = &txq->q;
516 struct iwl4965_tfd_frame *tfd;
Zhu Yib481de92007-09-25 17:54:57 -0700517 u32 *control_flags;
Tomas Winkler857485c2008-03-21 13:53:44 -0700518 struct iwl_cmd *out_cmd;
Zhu Yib481de92007-09-25 17:54:57 -0700519 u32 idx;
520 u16 fix_size = (u16)(cmd->len + sizeof(out_cmd->hdr));
521 dma_addr_t phys_addr;
522 int ret;
523 unsigned long flags;
524
525 /* If any of the command structures end up being larger than
526 * the TFD_MAX_PAYLOAD_SIZE, and it sent as a 'small' command then
527 * we will need to increase the size of the TFD entries */
528 BUG_ON((fix_size > TFD_MAX_PAYLOAD_SIZE) &&
529 !(cmd->meta.flags & CMD_SIZE_HUGE));
530
Tomas Winklerfee12472008-04-03 16:05:21 -0700531 if (iwl_is_rfkill(priv)) {
Gregory Greenmanc342a1b2008-02-06 11:20:40 -0800532 IWL_DEBUG_INFO("Not sending command - RF KILL");
533 return -EIO;
534 }
535
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800536 if (iwl4965_queue_space(q) < ((cmd->meta.flags & CMD_ASYNC) ? 2 : 1)) {
Zhu Yib481de92007-09-25 17:54:57 -0700537 IWL_ERROR("No space for Tx\n");
538 return -ENOSPC;
539 }
540
541 spin_lock_irqsave(&priv->hcmd_lock, flags);
542
Tomas Winklerfc4b6852007-10-25 17:15:24 +0800543 tfd = &txq->bd[q->write_ptr];
Zhu Yib481de92007-09-25 17:54:57 -0700544 memset(tfd, 0, sizeof(*tfd));
545
546 control_flags = (u32 *) tfd;
547
Tomas Winklerfc4b6852007-10-25 17:15:24 +0800548 idx = get_cmd_index(q, q->write_ptr, cmd->meta.flags & CMD_SIZE_HUGE);
Zhu Yib481de92007-09-25 17:54:57 -0700549 out_cmd = &txq->cmd[idx];
550
551 out_cmd->hdr.cmd = cmd->id;
552 memcpy(&out_cmd->meta, &cmd->meta, sizeof(cmd->meta));
553 memcpy(&out_cmd->cmd.payload, cmd->data, cmd->len);
554
555 /* At this point, the out_cmd now has all of the incoming cmd
556 * information */
557
558 out_cmd->hdr.flags = 0;
559 out_cmd->hdr.sequence = cpu_to_le16(QUEUE_TO_SEQ(IWL_CMD_QUEUE_NUM) |
Tomas Winklerfc4b6852007-10-25 17:15:24 +0800560 INDEX_TO_SEQ(q->write_ptr));
Zhu Yib481de92007-09-25 17:54:57 -0700561 if (out_cmd->meta.flags & CMD_SIZE_HUGE)
562 out_cmd->hdr.sequence |= cpu_to_le16(SEQ_HUGE_FRAME);
563
564 phys_addr = txq->dma_addr_cmd + sizeof(txq->cmd[0]) * idx +
Tomas Winkler857485c2008-03-21 13:53:44 -0700565 offsetof(struct iwl_cmd, hdr);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800566 iwl4965_hw_txq_attach_buf_to_tfd(priv, tfd, phys_addr, fix_size);
Zhu Yib481de92007-09-25 17:54:57 -0700567
568 IWL_DEBUG_HC("Sending command %s (#%x), seq: 0x%04X, "
569 "%d bytes at %d[%d]:%d\n",
570 get_cmd_string(out_cmd->hdr.cmd),
571 out_cmd->hdr.cmd, le16_to_cpu(out_cmd->hdr.sequence),
Tomas Winklerfc4b6852007-10-25 17:15:24 +0800572 fix_size, q->write_ptr, idx, IWL_CMD_QUEUE_NUM);
Zhu Yib481de92007-09-25 17:54:57 -0700573
574 txq->need_update = 1;
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800575
576 /* Set up entry in queue's byte count circular buffer */
Zhu Yib481de92007-09-25 17:54:57 -0700577 ret = iwl4965_tx_queue_update_wr_ptr(priv, txq, 0);
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800578
579 /* Increment and update queue's write index */
Tomas Winklerc54b6792008-03-06 17:36:53 -0800580 q->write_ptr = iwl_queue_inc_wrap(q->write_ptr, q->n_bd);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800581 iwl4965_tx_queue_update_write_ptr(priv, txq);
Zhu Yib481de92007-09-25 17:54:57 -0700582
583 spin_unlock_irqrestore(&priv->hcmd_lock, flags);
584 return ret ? ret : idx;
585}
586
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -0700587static void iwl4965_set_rxon_hwcrypto(struct iwl_priv *priv, int hw_decrypt)
588{
589 struct iwl4965_rxon_cmd *rxon = &priv->staging_rxon;
590
591 if (hw_decrypt)
592 rxon->filter_flags &= ~RXON_FILTER_DIS_DECRYPT_MSK;
593 else
594 rxon->filter_flags |= RXON_FILTER_DIS_DECRYPT_MSK;
595
596}
597
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700598int iwl4965_send_statistics_request(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700599{
Tomas Winkler857485c2008-03-21 13:53:44 -0700600 u32 flags = 0;
601 return iwl_send_cmd_pdu(priv, REPLY_STATISTICS_CMD,
602 sizeof(flags), &flags);
Zhu Yib481de92007-09-25 17:54:57 -0700603}
604
605/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800606 * iwl4965_rxon_add_station - add station into station table.
Zhu Yib481de92007-09-25 17:54:57 -0700607 *
608 * there is only one AP station with id= IWL_AP_ID
Ben Cahill9fbab512007-11-29 11:09:47 +0800609 * NOTE: mutex must be held before calling this fnction
610 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700611static int iwl4965_rxon_add_station(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -0700612 const u8 *addr, int is_ap)
613{
Zhu Yi556f8db2007-09-27 11:27:33 +0800614 u8 sta_id;
Zhu Yib481de92007-09-25 17:54:57 -0700615
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800616 /* Add station to device's station table */
Ron Rindjunsky67d62032007-11-26 16:14:40 +0200617#ifdef CONFIG_IWL4965_HT
618 struct ieee80211_conf *conf = &priv->hw->conf;
619 struct ieee80211_ht_info *cur_ht_config = &conf->ht_conf;
620
621 if ((is_ap) &&
622 (conf->flags & IEEE80211_CONF_SUPPORT_HT_MODE) &&
623 (priv->iw_mode == IEEE80211_IF_TYPE_STA))
624 sta_id = iwl4965_add_station_flags(priv, addr, is_ap,
625 0, cur_ht_config);
626 else
627#endif /* CONFIG_IWL4965_HT */
628 sta_id = iwl4965_add_station_flags(priv, addr, is_ap,
629 0, NULL);
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800630
631 /* Set up default rate scaling table in device's station table */
Zhu Yib481de92007-09-25 17:54:57 -0700632 iwl4965_add_station(priv, addr, is_ap);
633
Zhu Yi556f8db2007-09-27 11:27:33 +0800634 return sta_id;
Zhu Yib481de92007-09-25 17:54:57 -0700635}
636
637/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800638 * iwl4965_check_rxon_cmd - validate RXON structure is valid
Zhu Yib481de92007-09-25 17:54:57 -0700639 *
640 * NOTE: This is really only useful during development and can eventually
641 * be #ifdef'd out once the driver is stable and folks aren't actively
642 * making changes
643 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800644static int iwl4965_check_rxon_cmd(struct iwl4965_rxon_cmd *rxon)
Zhu Yib481de92007-09-25 17:54:57 -0700645{
646 int error = 0;
647 int counter = 1;
648
649 if (rxon->flags & RXON_FLG_BAND_24G_MSK) {
650 error |= le32_to_cpu(rxon->flags &
651 (RXON_FLG_TGJ_NARROW_BAND_MSK |
652 RXON_FLG_RADAR_DETECT_MSK));
653 if (error)
654 IWL_WARNING("check 24G fields %d | %d\n",
655 counter++, error);
656 } else {
657 error |= (rxon->flags & RXON_FLG_SHORT_SLOT_MSK) ?
658 0 : le32_to_cpu(RXON_FLG_SHORT_SLOT_MSK);
659 if (error)
660 IWL_WARNING("check 52 fields %d | %d\n",
661 counter++, error);
662 error |= le32_to_cpu(rxon->flags & RXON_FLG_CCK_MSK);
663 if (error)
664 IWL_WARNING("check 52 CCK %d | %d\n",
665 counter++, error);
666 }
667 error |= (rxon->node_addr[0] | rxon->bssid_addr[0]) & 0x1;
668 if (error)
669 IWL_WARNING("check mac addr %d | %d\n", counter++, error);
670
671 /* make sure basic rates 6Mbps and 1Mbps are supported */
672 error |= (((rxon->ofdm_basic_rates & IWL_RATE_6M_MASK) == 0) &&
673 ((rxon->cck_basic_rates & IWL_RATE_1M_MASK) == 0));
674 if (error)
675 IWL_WARNING("check basic rate %d | %d\n", counter++, error);
676
677 error |= (le16_to_cpu(rxon->assoc_id) > 2007);
678 if (error)
679 IWL_WARNING("check assoc id %d | %d\n", counter++, error);
680
681 error |= ((rxon->flags & (RXON_FLG_CCK_MSK | RXON_FLG_SHORT_SLOT_MSK))
682 == (RXON_FLG_CCK_MSK | RXON_FLG_SHORT_SLOT_MSK));
683 if (error)
684 IWL_WARNING("check CCK and short slot %d | %d\n",
685 counter++, error);
686
687 error |= ((rxon->flags & (RXON_FLG_CCK_MSK | RXON_FLG_AUTO_DETECT_MSK))
688 == (RXON_FLG_CCK_MSK | RXON_FLG_AUTO_DETECT_MSK));
689 if (error)
690 IWL_WARNING("check CCK & auto detect %d | %d\n",
691 counter++, error);
692
693 error |= ((rxon->flags & (RXON_FLG_AUTO_DETECT_MSK |
694 RXON_FLG_TGG_PROTECT_MSK)) == RXON_FLG_TGG_PROTECT_MSK);
695 if (error)
696 IWL_WARNING("check TGG and auto detect %d | %d\n",
697 counter++, error);
698
699 if (error)
700 IWL_WARNING("Tuning to channel %d\n",
701 le16_to_cpu(rxon->channel));
702
703 if (error) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800704 IWL_ERROR("Not a valid iwl4965_rxon_assoc_cmd field values\n");
Zhu Yib481de92007-09-25 17:54:57 -0700705 return -1;
706 }
707 return 0;
708}
709
710/**
Ben Cahill9fbab512007-11-29 11:09:47 +0800711 * iwl4965_full_rxon_required - check if full RXON (vs RXON_ASSOC) cmd is needed
Ian Schram01ebd062007-10-25 17:15:22 +0800712 * @priv: staging_rxon is compared to active_rxon
Zhu Yib481de92007-09-25 17:54:57 -0700713 *
Ben Cahill9fbab512007-11-29 11:09:47 +0800714 * If the RXON structure is changing enough to require a new tune,
715 * or is clearing the RXON_FILTER_ASSOC_MSK, then return 1 to indicate that
716 * a new tune (full RXON command, rather than RXON_ASSOC cmd) is required.
Zhu Yib481de92007-09-25 17:54:57 -0700717 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700718static int iwl4965_full_rxon_required(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700719{
720
721 /* These items are only settable from the full RXON command */
722 if (!(priv->active_rxon.filter_flags & RXON_FILTER_ASSOC_MSK) ||
723 compare_ether_addr(priv->staging_rxon.bssid_addr,
724 priv->active_rxon.bssid_addr) ||
725 compare_ether_addr(priv->staging_rxon.node_addr,
726 priv->active_rxon.node_addr) ||
727 compare_ether_addr(priv->staging_rxon.wlap_bssid_addr,
728 priv->active_rxon.wlap_bssid_addr) ||
729 (priv->staging_rxon.dev_type != priv->active_rxon.dev_type) ||
730 (priv->staging_rxon.channel != priv->active_rxon.channel) ||
731 (priv->staging_rxon.air_propagation !=
732 priv->active_rxon.air_propagation) ||
733 (priv->staging_rxon.ofdm_ht_single_stream_basic_rates !=
734 priv->active_rxon.ofdm_ht_single_stream_basic_rates) ||
735 (priv->staging_rxon.ofdm_ht_dual_stream_basic_rates !=
736 priv->active_rxon.ofdm_ht_dual_stream_basic_rates) ||
737 (priv->staging_rxon.rx_chain != priv->active_rxon.rx_chain) ||
738 (priv->staging_rxon.assoc_id != priv->active_rxon.assoc_id))
739 return 1;
740
741 /* flags, filter_flags, ofdm_basic_rates, and cck_basic_rates can
742 * be updated with the RXON_ASSOC command -- however only some
743 * flag transitions are allowed using RXON_ASSOC */
744
745 /* Check if we are not switching bands */
746 if ((priv->staging_rxon.flags & RXON_FLG_BAND_24G_MSK) !=
747 (priv->active_rxon.flags & RXON_FLG_BAND_24G_MSK))
748 return 1;
749
750 /* Check if we are switching association toggle */
751 if ((priv->staging_rxon.filter_flags & RXON_FILTER_ASSOC_MSK) !=
752 (priv->active_rxon.filter_flags & RXON_FILTER_ASSOC_MSK))
753 return 1;
754
755 return 0;
756}
757
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700758static int iwl4965_send_rxon_assoc(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700759{
760 int rc = 0;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800761 struct iwl4965_rx_packet *res = NULL;
762 struct iwl4965_rxon_assoc_cmd rxon_assoc;
Tomas Winkler857485c2008-03-21 13:53:44 -0700763 struct iwl_host_cmd cmd = {
Zhu Yib481de92007-09-25 17:54:57 -0700764 .id = REPLY_RXON_ASSOC,
765 .len = sizeof(rxon_assoc),
766 .meta.flags = CMD_WANT_SKB,
767 .data = &rxon_assoc,
768 };
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800769 const struct iwl4965_rxon_cmd *rxon1 = &priv->staging_rxon;
770 const struct iwl4965_rxon_cmd *rxon2 = &priv->active_rxon;
Zhu Yib481de92007-09-25 17:54:57 -0700771
772 if ((rxon1->flags == rxon2->flags) &&
773 (rxon1->filter_flags == rxon2->filter_flags) &&
774 (rxon1->cck_basic_rates == rxon2->cck_basic_rates) &&
775 (rxon1->ofdm_ht_single_stream_basic_rates ==
776 rxon2->ofdm_ht_single_stream_basic_rates) &&
777 (rxon1->ofdm_ht_dual_stream_basic_rates ==
778 rxon2->ofdm_ht_dual_stream_basic_rates) &&
779 (rxon1->rx_chain == rxon2->rx_chain) &&
780 (rxon1->ofdm_basic_rates == rxon2->ofdm_basic_rates)) {
781 IWL_DEBUG_INFO("Using current RXON_ASSOC. Not resending.\n");
782 return 0;
783 }
784
785 rxon_assoc.flags = priv->staging_rxon.flags;
786 rxon_assoc.filter_flags = priv->staging_rxon.filter_flags;
787 rxon_assoc.ofdm_basic_rates = priv->staging_rxon.ofdm_basic_rates;
788 rxon_assoc.cck_basic_rates = priv->staging_rxon.cck_basic_rates;
789 rxon_assoc.reserved = 0;
790 rxon_assoc.ofdm_ht_single_stream_basic_rates =
791 priv->staging_rxon.ofdm_ht_single_stream_basic_rates;
792 rxon_assoc.ofdm_ht_dual_stream_basic_rates =
793 priv->staging_rxon.ofdm_ht_dual_stream_basic_rates;
794 rxon_assoc.rx_chain_select_flags = priv->staging_rxon.rx_chain;
795
Tomas Winkler857485c2008-03-21 13:53:44 -0700796 rc = iwl_send_cmd_sync(priv, &cmd);
Zhu Yib481de92007-09-25 17:54:57 -0700797 if (rc)
798 return rc;
799
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800800 res = (struct iwl4965_rx_packet *)cmd.meta.u.skb->data;
Zhu Yib481de92007-09-25 17:54:57 -0700801 if (res->hdr.flags & IWL_CMD_FAILED_MSK) {
802 IWL_ERROR("Bad return from REPLY_RXON_ASSOC command\n");
803 rc = -EIO;
804 }
805
806 priv->alloc_rxb_skb--;
807 dev_kfree_skb_any(cmd.meta.u.skb);
808
809 return rc;
810}
811
812/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800813 * iwl4965_commit_rxon - commit staging_rxon to hardware
Zhu Yib481de92007-09-25 17:54:57 -0700814 *
Ian Schram01ebd062007-10-25 17:15:22 +0800815 * The RXON command in staging_rxon is committed to the hardware and
Zhu Yib481de92007-09-25 17:54:57 -0700816 * the active_rxon structure is updated with the new data. This
817 * function correctly transitions out of the RXON_ASSOC_MSK state if
818 * a HW tune is required based on the RXON structure changes.
819 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700820static int iwl4965_commit_rxon(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700821{
822 /* cast away the const for active_rxon in this function */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800823 struct iwl4965_rxon_cmd *active_rxon = (void *)&priv->active_rxon;
Joe Perches0795af52007-10-03 17:59:30 -0700824 DECLARE_MAC_BUF(mac);
Zhu Yib481de92007-09-25 17:54:57 -0700825 int rc = 0;
826
Tomas Winklerfee12472008-04-03 16:05:21 -0700827 if (!iwl_is_alive(priv))
Zhu Yib481de92007-09-25 17:54:57 -0700828 return -1;
829
830 /* always get timestamp with Rx frame */
831 priv->staging_rxon.flags |= RXON_FLG_TSF2HOST_MSK;
832
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800833 rc = iwl4965_check_rxon_cmd(&priv->staging_rxon);
Zhu Yib481de92007-09-25 17:54:57 -0700834 if (rc) {
835 IWL_ERROR("Invalid RXON configuration. Not committing.\n");
836 return -EINVAL;
837 }
838
839 /* If we don't need to send a full RXON, we can use
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800840 * iwl4965_rxon_assoc_cmd which is used to reconfigure filter
Zhu Yib481de92007-09-25 17:54:57 -0700841 * and other flags for the current radio configuration. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800842 if (!iwl4965_full_rxon_required(priv)) {
843 rc = iwl4965_send_rxon_assoc(priv);
Zhu Yib481de92007-09-25 17:54:57 -0700844 if (rc) {
845 IWL_ERROR("Error setting RXON_ASSOC "
846 "configuration (%d).\n", rc);
847 return rc;
848 }
849
850 memcpy(active_rxon, &priv->staging_rxon, sizeof(*active_rxon));
851
852 return 0;
853 }
854
855 /* station table will be cleared */
856 priv->assoc_station_added = 0;
857
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +0800858#ifdef CONFIG_IWL4965_SENSITIVITY
Zhu Yib481de92007-09-25 17:54:57 -0700859 priv->sensitivity_data.state = IWL_SENS_CALIB_NEED_REINIT;
860 if (!priv->error_recovering)
861 priv->start_calib = 0;
862
863 iwl4965_init_sensitivity(priv, CMD_ASYNC, 1);
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +0800864#endif /* CONFIG_IWL4965_SENSITIVITY */
Zhu Yib481de92007-09-25 17:54:57 -0700865
866 /* If we are currently associated and the new config requires
867 * an RXON_ASSOC and the new config wants the associated mask enabled,
868 * we must clear the associated from the active configuration
869 * before we apply the new config */
Tomas Winkler3109ece2008-03-28 16:33:35 -0700870 if (iwl_is_associated(priv) &&
Zhu Yib481de92007-09-25 17:54:57 -0700871 (priv->staging_rxon.filter_flags & RXON_FILTER_ASSOC_MSK)) {
872 IWL_DEBUG_INFO("Toggling associated bit on current RXON\n");
873 active_rxon->filter_flags &= ~RXON_FILTER_ASSOC_MSK;
874
Tomas Winkler857485c2008-03-21 13:53:44 -0700875 rc = iwl_send_cmd_pdu(priv, REPLY_RXON,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800876 sizeof(struct iwl4965_rxon_cmd),
Zhu Yib481de92007-09-25 17:54:57 -0700877 &priv->active_rxon);
878
879 /* If the mask clearing failed then we set
880 * active_rxon back to what it was previously */
881 if (rc) {
882 active_rxon->filter_flags |= RXON_FILTER_ASSOC_MSK;
883 IWL_ERROR("Error clearing ASSOC_MSK on current "
884 "configuration (%d).\n", rc);
885 return rc;
886 }
Zhu Yib481de92007-09-25 17:54:57 -0700887 }
888
889 IWL_DEBUG_INFO("Sending RXON\n"
890 "* with%s RXON_FILTER_ASSOC_MSK\n"
891 "* channel = %d\n"
Joe Perches0795af52007-10-03 17:59:30 -0700892 "* bssid = %s\n",
Zhu Yib481de92007-09-25 17:54:57 -0700893 ((priv->staging_rxon.filter_flags &
894 RXON_FILTER_ASSOC_MSK) ? "" : "out"),
895 le16_to_cpu(priv->staging_rxon.channel),
Joe Perches0795af52007-10-03 17:59:30 -0700896 print_mac(mac, priv->staging_rxon.bssid_addr));
Zhu Yib481de92007-09-25 17:54:57 -0700897
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -0700898 iwl4965_set_rxon_hwcrypto(priv, priv->cfg->mod_params->hw_crypto);
Zhu Yib481de92007-09-25 17:54:57 -0700899 /* Apply the new configuration */
Tomas Winkler857485c2008-03-21 13:53:44 -0700900 rc = iwl_send_cmd_pdu(priv, REPLY_RXON,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800901 sizeof(struct iwl4965_rxon_cmd), &priv->staging_rxon);
Zhu Yib481de92007-09-25 17:54:57 -0700902 if (rc) {
903 IWL_ERROR("Error setting new configuration (%d).\n", rc);
904 return rc;
905 }
906
Assaf Kraussbf85ea42008-03-14 10:38:49 -0700907 iwlcore_clear_stations_table(priv);
Zhu Yi556f8db2007-09-27 11:27:33 +0800908
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +0800909#ifdef CONFIG_IWL4965_SENSITIVITY
Zhu Yib481de92007-09-25 17:54:57 -0700910 if (!priv->error_recovering)
911 priv->start_calib = 0;
912
913 priv->sensitivity_data.state = IWL_SENS_CALIB_NEED_REINIT;
914 iwl4965_init_sensitivity(priv, CMD_ASYNC, 1);
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +0800915#endif /* CONFIG_IWL4965_SENSITIVITY */
Zhu Yib481de92007-09-25 17:54:57 -0700916
917 memcpy(active_rxon, &priv->staging_rxon, sizeof(*active_rxon));
918
919 /* If we issue a new RXON command which required a tune then we must
920 * send a new TXPOWER command or we won't be able to Tx any frames */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800921 rc = iwl4965_hw_reg_send_txpower(priv);
Zhu Yib481de92007-09-25 17:54:57 -0700922 if (rc) {
923 IWL_ERROR("Error setting Tx power (%d).\n", rc);
924 return rc;
925 }
926
927 /* Add the broadcast address so we can send broadcast frames */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800928 if (iwl4965_rxon_add_station(priv, iwl4965_broadcast_addr, 0) ==
Zhu Yib481de92007-09-25 17:54:57 -0700929 IWL_INVALID_STATION) {
930 IWL_ERROR("Error adding BROADCAST address for transmit.\n");
931 return -EIO;
932 }
933
934 /* If we have set the ASSOC_MSK and we are in BSS mode then
935 * add the IWL_AP_ID to the station rate table */
Tomas Winkler3109ece2008-03-28 16:33:35 -0700936 if (iwl_is_associated(priv) &&
Zhu Yib481de92007-09-25 17:54:57 -0700937 (priv->iw_mode == IEEE80211_IF_TYPE_STA)) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800938 if (iwl4965_rxon_add_station(priv, priv->active_rxon.bssid_addr, 1)
Zhu Yib481de92007-09-25 17:54:57 -0700939 == IWL_INVALID_STATION) {
940 IWL_ERROR("Error adding AP address for transmit.\n");
941 return -EIO;
942 }
943 priv->assoc_station_added = 1;
944 }
945
946 return 0;
947}
948
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700949static int iwl4965_send_bt_config(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700950{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800951 struct iwl4965_bt_cmd bt_cmd = {
Zhu Yib481de92007-09-25 17:54:57 -0700952 .flags = 3,
953 .lead_time = 0xAA,
954 .max_kill = 1,
955 .kill_ack_mask = 0,
956 .kill_cts_mask = 0,
957 };
958
Tomas Winkler857485c2008-03-21 13:53:44 -0700959 return iwl_send_cmd_pdu(priv, REPLY_BT_CONFIG,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800960 sizeof(struct iwl4965_bt_cmd), &bt_cmd);
Zhu Yib481de92007-09-25 17:54:57 -0700961}
962
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700963static int iwl4965_send_scan_abort(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700964{
965 int rc = 0;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800966 struct iwl4965_rx_packet *res;
Tomas Winkler857485c2008-03-21 13:53:44 -0700967 struct iwl_host_cmd cmd = {
Zhu Yib481de92007-09-25 17:54:57 -0700968 .id = REPLY_SCAN_ABORT_CMD,
969 .meta.flags = CMD_WANT_SKB,
970 };
971
972 /* If there isn't a scan actively going on in the hardware
973 * then we are in between scan bands and not actually
974 * actively scanning, so don't send the abort command */
975 if (!test_bit(STATUS_SCAN_HW, &priv->status)) {
976 clear_bit(STATUS_SCAN_ABORTING, &priv->status);
977 return 0;
978 }
979
Tomas Winkler857485c2008-03-21 13:53:44 -0700980 rc = iwl_send_cmd_sync(priv, &cmd);
Zhu Yib481de92007-09-25 17:54:57 -0700981 if (rc) {
982 clear_bit(STATUS_SCAN_ABORTING, &priv->status);
983 return rc;
984 }
985
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800986 res = (struct iwl4965_rx_packet *)cmd.meta.u.skb->data;
Zhu Yib481de92007-09-25 17:54:57 -0700987 if (res->u.status != CAN_ABORT_STATUS) {
988 /* The scan abort will return 1 for success or
989 * 2 for "failure". A failure condition can be
990 * due to simply not being in an active scan which
991 * can occur if we send the scan abort before we
992 * the microcode has notified us that a scan is
993 * completed. */
994 IWL_DEBUG_INFO("SCAN_ABORT returned %d.\n", res->u.status);
995 clear_bit(STATUS_SCAN_ABORTING, &priv->status);
996 clear_bit(STATUS_SCAN_HW, &priv->status);
997 }
998
999 dev_kfree_skb_any(cmd.meta.u.skb);
1000
1001 return rc;
1002}
1003
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001004static int iwl4965_card_state_sync_callback(struct iwl_priv *priv,
Tomas Winkler857485c2008-03-21 13:53:44 -07001005 struct iwl_cmd *cmd,
Zhu Yib481de92007-09-25 17:54:57 -07001006 struct sk_buff *skb)
1007{
1008 return 1;
1009}
1010
1011/*
1012 * CARD_STATE_CMD
1013 *
Ben Cahill9fbab512007-11-29 11:09:47 +08001014 * Use: Sets the device's internal card state to enable, disable, or halt
Zhu Yib481de92007-09-25 17:54:57 -07001015 *
1016 * When in the 'enable' state the card operates as normal.
1017 * When in the 'disable' state, the card enters into a low power mode.
1018 * When in the 'halt' state, the card is shut down and must be fully
1019 * restarted to come back on.
1020 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001021static int iwl4965_send_card_state(struct iwl_priv *priv, u32 flags, u8 meta_flag)
Zhu Yib481de92007-09-25 17:54:57 -07001022{
Tomas Winkler857485c2008-03-21 13:53:44 -07001023 struct iwl_host_cmd cmd = {
Zhu Yib481de92007-09-25 17:54:57 -07001024 .id = REPLY_CARD_STATE_CMD,
1025 .len = sizeof(u32),
1026 .data = &flags,
1027 .meta.flags = meta_flag,
1028 };
1029
1030 if (meta_flag & CMD_ASYNC)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001031 cmd.meta.u.callback = iwl4965_card_state_sync_callback;
Zhu Yib481de92007-09-25 17:54:57 -07001032
Tomas Winkler857485c2008-03-21 13:53:44 -07001033 return iwl_send_cmd(priv, &cmd);
Zhu Yib481de92007-09-25 17:54:57 -07001034}
1035
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001036static int iwl4965_add_sta_sync_callback(struct iwl_priv *priv,
Tomas Winkler857485c2008-03-21 13:53:44 -07001037 struct iwl_cmd *cmd, struct sk_buff *skb)
Zhu Yib481de92007-09-25 17:54:57 -07001038{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001039 struct iwl4965_rx_packet *res = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07001040
1041 if (!skb) {
1042 IWL_ERROR("Error: Response NULL in REPLY_ADD_STA.\n");
1043 return 1;
1044 }
1045
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001046 res = (struct iwl4965_rx_packet *)skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07001047 if (res->hdr.flags & IWL_CMD_FAILED_MSK) {
1048 IWL_ERROR("Bad return from REPLY_ADD_STA (0x%08X)\n",
1049 res->hdr.flags);
1050 return 1;
1051 }
1052
1053 switch (res->u.add_sta.status) {
1054 case ADD_STA_SUCCESS_MSK:
1055 break;
1056 default:
1057 break;
1058 }
1059
1060 /* We didn't cache the SKB; let the caller free it */
1061 return 1;
1062}
1063
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001064int iwl4965_send_add_station(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001065 struct iwl4965_addsta_cmd *sta, u8 flags)
Zhu Yib481de92007-09-25 17:54:57 -07001066{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001067 struct iwl4965_rx_packet *res = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07001068 int rc = 0;
Tomas Winkler857485c2008-03-21 13:53:44 -07001069 struct iwl_host_cmd cmd = {
Zhu Yib481de92007-09-25 17:54:57 -07001070 .id = REPLY_ADD_STA,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001071 .len = sizeof(struct iwl4965_addsta_cmd),
Zhu Yib481de92007-09-25 17:54:57 -07001072 .meta.flags = flags,
1073 .data = sta,
1074 };
1075
1076 if (flags & CMD_ASYNC)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001077 cmd.meta.u.callback = iwl4965_add_sta_sync_callback;
Zhu Yib481de92007-09-25 17:54:57 -07001078 else
1079 cmd.meta.flags |= CMD_WANT_SKB;
1080
Tomas Winkler857485c2008-03-21 13:53:44 -07001081 rc = iwl_send_cmd(priv, &cmd);
Zhu Yib481de92007-09-25 17:54:57 -07001082
1083 if (rc || (flags & CMD_ASYNC))
1084 return rc;
1085
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001086 res = (struct iwl4965_rx_packet *)cmd.meta.u.skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07001087 if (res->hdr.flags & IWL_CMD_FAILED_MSK) {
1088 IWL_ERROR("Bad return from REPLY_ADD_STA (0x%08X)\n",
1089 res->hdr.flags);
1090 rc = -EIO;
1091 }
1092
1093 if (rc == 0) {
1094 switch (res->u.add_sta.status) {
1095 case ADD_STA_SUCCESS_MSK:
1096 IWL_DEBUG_INFO("REPLY_ADD_STA PASSED\n");
1097 break;
1098 default:
1099 rc = -EIO;
1100 IWL_WARNING("REPLY_ADD_STA failed\n");
1101 break;
1102 }
1103 }
1104
1105 priv->alloc_rxb_skb--;
1106 dev_kfree_skb_any(cmd.meta.u.skb);
1107
1108 return rc;
1109}
1110
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07001111static int iwl4965_set_ccmp_dynamic_key_info(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07001112 struct ieee80211_key_conf *keyconf,
1113 u8 sta_id)
1114{
1115 unsigned long flags;
1116 __le16 key_flags = 0;
1117
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07001118 key_flags |= (STA_KEY_FLG_CCMP | STA_KEY_FLG_MAP_KEY_MSK);
1119 key_flags |= cpu_to_le16(keyconf->keyidx << STA_KEY_FLG_KEYID_POS);
1120
1121 if (sta_id == priv->hw_setting.bcast_sta_id)
1122 key_flags |= STA_KEY_MULTICAST_MSK;
1123
1124 keyconf->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
1125 keyconf->hw_key_idx = keyconf->keyidx;
1126
1127 key_flags &= ~STA_KEY_FLG_INVALID;
1128
Zhu Yib481de92007-09-25 17:54:57 -07001129 spin_lock_irqsave(&priv->sta_lock, flags);
1130 priv->stations[sta_id].keyinfo.alg = keyconf->alg;
1131 priv->stations[sta_id].keyinfo.keylen = keyconf->keylen;
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07001132
Zhu Yib481de92007-09-25 17:54:57 -07001133 memcpy(priv->stations[sta_id].keyinfo.key, keyconf->key,
1134 keyconf->keylen);
1135
1136 memcpy(priv->stations[sta_id].sta.key.key, keyconf->key,
1137 keyconf->keylen);
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07001138
1139 priv->stations[sta_id].sta.key.key_offset
1140 = (sta_id % STA_KEY_MAX_NUM);/*FIXME*/
Zhu Yib481de92007-09-25 17:54:57 -07001141 priv->stations[sta_id].sta.key.key_flags = key_flags;
1142 priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
1143 priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
1144
1145 spin_unlock_irqrestore(&priv->sta_lock, flags);
1146
1147 IWL_DEBUG_INFO("hwcrypto: modify ucode station key info\n");
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07001148 return iwl4965_send_add_station(priv,
1149 &priv->stations[sta_id].sta, CMD_ASYNC);
1150}
1151
1152static int iwl4965_set_tkip_dynamic_key_info(struct iwl_priv *priv,
1153 struct ieee80211_key_conf *keyconf,
1154 u8 sta_id)
1155{
Emmanuel Grumbach2bc75082008-03-19 16:41:45 -07001156 unsigned long flags;
1157 int ret = 0;
1158
1159 keyconf->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
1160 keyconf->flags |= IEEE80211_KEY_FLAG_GENERATE_MMIC;
1161 keyconf->hw_key_idx = keyconf->keyidx;
1162
1163 spin_lock_irqsave(&priv->sta_lock, flags);
1164
1165 priv->stations[sta_id].keyinfo.alg = keyconf->alg;
1166 priv->stations[sta_id].keyinfo.conf = keyconf;
1167 priv->stations[sta_id].keyinfo.keylen = 16;
1168
1169 /* This copy is acutally not needed: we get the key with each TX */
1170 memcpy(priv->stations[sta_id].keyinfo.key, keyconf->key, 16);
1171
1172 memcpy(priv->stations[sta_id].sta.key.key, keyconf->key, 16);
1173
1174 spin_unlock_irqrestore(&priv->sta_lock, flags);
1175
1176 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07001177}
1178
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001179static int iwl4965_clear_sta_key_info(struct iwl_priv *priv, u8 sta_id)
Zhu Yib481de92007-09-25 17:54:57 -07001180{
1181 unsigned long flags;
1182
1183 spin_lock_irqsave(&priv->sta_lock, flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001184 memset(&priv->stations[sta_id].keyinfo, 0, sizeof(struct iwl4965_hw_key));
1185 memset(&priv->stations[sta_id].sta.key, 0, sizeof(struct iwl4965_keyinfo));
Zhu Yib481de92007-09-25 17:54:57 -07001186 priv->stations[sta_id].sta.key.key_flags = STA_KEY_FLG_NO_ENC;
1187 priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
1188 priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
1189 spin_unlock_irqrestore(&priv->sta_lock, flags);
1190
1191 IWL_DEBUG_INFO("hwcrypto: clear ucode station key info\n");
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001192 iwl4965_send_add_station(priv, &priv->stations[sta_id].sta, 0);
Zhu Yib481de92007-09-25 17:54:57 -07001193 return 0;
1194}
1195
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07001196static int iwl4965_set_dynamic_key(struct iwl_priv *priv,
1197 struct ieee80211_key_conf *key, u8 sta_id)
1198{
1199 int ret;
1200
1201 switch (key->alg) {
1202 case ALG_CCMP:
1203 ret = iwl4965_set_ccmp_dynamic_key_info(priv, key, sta_id);
1204 break;
1205 case ALG_TKIP:
1206 ret = iwl4965_set_tkip_dynamic_key_info(priv, key, sta_id);
1207 break;
1208 case ALG_WEP:
1209 ret = -EOPNOTSUPP;
1210 break;
1211 default:
1212 IWL_ERROR("Unknown alg: %s alg = %d\n", __func__, key->alg);
1213 ret = -EINVAL;
1214 }
1215
1216 return ret;
1217}
1218
1219static int iwl4965_remove_static_key(struct iwl_priv *priv)
1220{
1221 int ret = -EOPNOTSUPP;
1222
1223 return ret;
1224}
1225
1226static int iwl4965_set_static_key(struct iwl_priv *priv,
1227 struct ieee80211_key_conf *key)
1228{
1229 if (key->alg == ALG_WEP)
1230 return -EOPNOTSUPP;
1231
1232 IWL_ERROR("Static key invalid: alg %d\n", key->alg);
1233 return -EINVAL;
1234}
1235
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001236static void iwl4965_clear_free_frames(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001237{
1238 struct list_head *element;
1239
1240 IWL_DEBUG_INFO("%d frames on pre-allocated heap on clear.\n",
1241 priv->frames_count);
1242
1243 while (!list_empty(&priv->free_frames)) {
1244 element = priv->free_frames.next;
1245 list_del(element);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001246 kfree(list_entry(element, struct iwl4965_frame, list));
Zhu Yib481de92007-09-25 17:54:57 -07001247 priv->frames_count--;
1248 }
1249
1250 if (priv->frames_count) {
1251 IWL_WARNING("%d frames still in use. Did we lose one?\n",
1252 priv->frames_count);
1253 priv->frames_count = 0;
1254 }
1255}
1256
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001257static struct iwl4965_frame *iwl4965_get_free_frame(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001258{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001259 struct iwl4965_frame *frame;
Zhu Yib481de92007-09-25 17:54:57 -07001260 struct list_head *element;
1261 if (list_empty(&priv->free_frames)) {
1262 frame = kzalloc(sizeof(*frame), GFP_KERNEL);
1263 if (!frame) {
1264 IWL_ERROR("Could not allocate frame!\n");
1265 return NULL;
1266 }
1267
1268 priv->frames_count++;
1269 return frame;
1270 }
1271
1272 element = priv->free_frames.next;
1273 list_del(element);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001274 return list_entry(element, struct iwl4965_frame, list);
Zhu Yib481de92007-09-25 17:54:57 -07001275}
1276
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001277static void iwl4965_free_frame(struct iwl_priv *priv, struct iwl4965_frame *frame)
Zhu Yib481de92007-09-25 17:54:57 -07001278{
1279 memset(frame, 0, sizeof(*frame));
1280 list_add(&frame->list, &priv->free_frames);
1281}
1282
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001283unsigned int iwl4965_fill_beacon_frame(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07001284 struct ieee80211_hdr *hdr,
1285 const u8 *dest, int left)
1286{
1287
Tomas Winkler3109ece2008-03-28 16:33:35 -07001288 if (!iwl_is_associated(priv) || !priv->ibss_beacon ||
Zhu Yib481de92007-09-25 17:54:57 -07001289 ((priv->iw_mode != IEEE80211_IF_TYPE_IBSS) &&
1290 (priv->iw_mode != IEEE80211_IF_TYPE_AP)))
1291 return 0;
1292
1293 if (priv->ibss_beacon->len > left)
1294 return 0;
1295
1296 memcpy(hdr, priv->ibss_beacon->data, priv->ibss_beacon->len);
1297
1298 return priv->ibss_beacon->len;
1299}
1300
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001301static u8 iwl4965_rate_get_lowest_plcp(int rate_mask)
Zhu Yib481de92007-09-25 17:54:57 -07001302{
1303 u8 i;
1304
1305 for (i = IWL_RATE_1M_INDEX; i != IWL_RATE_INVALID;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001306 i = iwl4965_rates[i].next_ieee) {
Zhu Yib481de92007-09-25 17:54:57 -07001307 if (rate_mask & (1 << i))
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001308 return iwl4965_rates[i].plcp;
Zhu Yib481de92007-09-25 17:54:57 -07001309 }
1310
1311 return IWL_RATE_INVALID;
1312}
1313
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001314static int iwl4965_send_beacon_cmd(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001315{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001316 struct iwl4965_frame *frame;
Zhu Yib481de92007-09-25 17:54:57 -07001317 unsigned int frame_size;
1318 int rc;
1319 u8 rate;
1320
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001321 frame = iwl4965_get_free_frame(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001322
1323 if (!frame) {
1324 IWL_ERROR("Could not obtain free frame buffer for beacon "
1325 "command.\n");
1326 return -ENOMEM;
1327 }
1328
1329 if (!(priv->staging_rxon.flags & RXON_FLG_BAND_24G_MSK)) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001330 rate = iwl4965_rate_get_lowest_plcp(priv->active_rate_basic &
Zhu Yib481de92007-09-25 17:54:57 -07001331 0xFF0);
1332 if (rate == IWL_INVALID_RATE)
1333 rate = IWL_RATE_6M_PLCP;
1334 } else {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001335 rate = iwl4965_rate_get_lowest_plcp(priv->active_rate_basic & 0xF);
Zhu Yib481de92007-09-25 17:54:57 -07001336 if (rate == IWL_INVALID_RATE)
1337 rate = IWL_RATE_1M_PLCP;
1338 }
1339
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001340 frame_size = iwl4965_hw_get_beacon_cmd(priv, frame, rate);
Zhu Yib481de92007-09-25 17:54:57 -07001341
Tomas Winkler857485c2008-03-21 13:53:44 -07001342 rc = iwl_send_cmd_pdu(priv, REPLY_TX_BEACON, frame_size,
Zhu Yib481de92007-09-25 17:54:57 -07001343 &frame->u.cmd[0]);
1344
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001345 iwl4965_free_frame(priv, frame);
Zhu Yib481de92007-09-25 17:54:57 -07001346
1347 return rc;
1348}
1349
1350/******************************************************************************
1351 *
Zhu Yib481de92007-09-25 17:54:57 -07001352 * Misc. internal state and helper functions
1353 *
1354 ******************************************************************************/
Zhu Yib481de92007-09-25 17:54:57 -07001355
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001356static void iwl4965_unset_hw_setting(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001357{
1358 if (priv->hw_setting.shared_virt)
1359 pci_free_consistent(priv->pci_dev,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001360 sizeof(struct iwl4965_shared),
Zhu Yib481de92007-09-25 17:54:57 -07001361 priv->hw_setting.shared_virt,
1362 priv->hw_setting.shared_phys);
1363}
1364
1365/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001366 * iwl4965_supported_rate_to_ie - fill in the supported rate in IE field
Zhu Yib481de92007-09-25 17:54:57 -07001367 *
1368 * return : set the bit for each supported rate insert in ie
1369 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001370static u16 iwl4965_supported_rate_to_ie(u8 *ie, u16 supported_rate,
Tomas Winklerc7c46672007-10-18 02:04:15 +02001371 u16 basic_rate, int *left)
Zhu Yib481de92007-09-25 17:54:57 -07001372{
1373 u16 ret_rates = 0, bit;
1374 int i;
Tomas Winklerc7c46672007-10-18 02:04:15 +02001375 u8 *cnt = ie;
1376 u8 *rates = ie + 1;
Zhu Yib481de92007-09-25 17:54:57 -07001377
1378 for (bit = 1, i = 0; i < IWL_RATE_COUNT; i++, bit <<= 1) {
1379 if (bit & supported_rate) {
1380 ret_rates |= bit;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001381 rates[*cnt] = iwl4965_rates[i].ieee |
Tomas Winklerc7c46672007-10-18 02:04:15 +02001382 ((bit & basic_rate) ? 0x80 : 0x00);
1383 (*cnt)++;
1384 (*left)--;
1385 if ((*left <= 0) ||
1386 (*cnt >= IWL_SUPPORTED_RATES_IE_LEN))
Zhu Yib481de92007-09-25 17:54:57 -07001387 break;
1388 }
1389 }
1390
1391 return ret_rates;
1392}
1393
Zhu Yib481de92007-09-25 17:54:57 -07001394/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001395 * iwl4965_fill_probe_req - fill in all required fields and IE for probe request
Zhu Yib481de92007-09-25 17:54:57 -07001396 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001397static u16 iwl4965_fill_probe_req(struct iwl_priv *priv,
Tomas Winkler78330fd2008-02-06 02:37:18 +02001398 enum ieee80211_band band,
1399 struct ieee80211_mgmt *frame,
1400 int left, int is_direct)
Zhu Yib481de92007-09-25 17:54:57 -07001401{
1402 int len = 0;
1403 u8 *pos = NULL;
mabbasbee488d2007-10-25 17:15:42 +08001404 u16 active_rates, ret_rates, cck_rates, active_rate_basic;
Ron Rindjunsky8fb880322007-11-26 16:14:38 +02001405#ifdef CONFIG_IWL4965_HT
Tomas Winkler78330fd2008-02-06 02:37:18 +02001406 const struct ieee80211_supported_band *sband =
1407 iwl4965_get_hw_mode(priv, band);
Ron Rindjunsky8fb880322007-11-26 16:14:38 +02001408#endif /* CONFIG_IWL4965_HT */
Zhu Yib481de92007-09-25 17:54:57 -07001409
1410 /* Make sure there is enough space for the probe request,
1411 * two mandatory IEs and the data */
1412 left -= 24;
1413 if (left < 0)
1414 return 0;
1415 len += 24;
1416
1417 frame->frame_control = cpu_to_le16(IEEE80211_STYPE_PROBE_REQ);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001418 memcpy(frame->da, iwl4965_broadcast_addr, ETH_ALEN);
Zhu Yib481de92007-09-25 17:54:57 -07001419 memcpy(frame->sa, priv->mac_addr, ETH_ALEN);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001420 memcpy(frame->bssid, iwl4965_broadcast_addr, ETH_ALEN);
Zhu Yib481de92007-09-25 17:54:57 -07001421 frame->seq_ctrl = 0;
1422
1423 /* fill in our indirect SSID IE */
1424 /* ...next IE... */
1425
1426 left -= 2;
1427 if (left < 0)
1428 return 0;
1429 len += 2;
1430 pos = &(frame->u.probe_req.variable[0]);
1431 *pos++ = WLAN_EID_SSID;
1432 *pos++ = 0;
1433
1434 /* fill in our direct SSID IE... */
1435 if (is_direct) {
1436 /* ...next IE... */
1437 left -= 2 + priv->essid_len;
1438 if (left < 0)
1439 return 0;
1440 /* ... fill it in... */
1441 *pos++ = WLAN_EID_SSID;
1442 *pos++ = priv->essid_len;
1443 memcpy(pos, priv->essid, priv->essid_len);
1444 pos += priv->essid_len;
1445 len += 2 + priv->essid_len;
1446 }
1447
1448 /* fill in supported rate */
1449 /* ...next IE... */
1450 left -= 2;
1451 if (left < 0)
1452 return 0;
Tomas Winklerc7c46672007-10-18 02:04:15 +02001453
Zhu Yib481de92007-09-25 17:54:57 -07001454 /* ... fill it in... */
1455 *pos++ = WLAN_EID_SUPP_RATES;
1456 *pos = 0;
Tomas Winklerc7c46672007-10-18 02:04:15 +02001457
mabbasbee488d2007-10-25 17:15:42 +08001458 /* exclude 60M rate */
1459 active_rates = priv->rates_mask;
1460 active_rates &= ~IWL_RATE_60M_MASK;
1461
1462 active_rate_basic = active_rates & IWL_BASIC_RATES_MASK;
Zhu Yib481de92007-09-25 17:54:57 -07001463
Tomas Winklerc7c46672007-10-18 02:04:15 +02001464 cck_rates = IWL_CCK_RATES_MASK & active_rates;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001465 ret_rates = iwl4965_supported_rate_to_ie(pos, cck_rates,
mabbasbee488d2007-10-25 17:15:42 +08001466 active_rate_basic, &left);
Tomas Winklerc7c46672007-10-18 02:04:15 +02001467 active_rates &= ~ret_rates;
1468
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001469 ret_rates = iwl4965_supported_rate_to_ie(pos, active_rates,
mabbasbee488d2007-10-25 17:15:42 +08001470 active_rate_basic, &left);
Tomas Winklerc7c46672007-10-18 02:04:15 +02001471 active_rates &= ~ret_rates;
1472
Zhu Yib481de92007-09-25 17:54:57 -07001473 len += 2 + *pos;
1474 pos += (*pos) + 1;
Tomas Winklerc7c46672007-10-18 02:04:15 +02001475 if (active_rates == 0)
Zhu Yib481de92007-09-25 17:54:57 -07001476 goto fill_end;
1477
1478 /* fill in supported extended rate */
1479 /* ...next IE... */
1480 left -= 2;
1481 if (left < 0)
1482 return 0;
1483 /* ... fill it in... */
1484 *pos++ = WLAN_EID_EXT_SUPP_RATES;
1485 *pos = 0;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001486 iwl4965_supported_rate_to_ie(pos, active_rates,
mabbasbee488d2007-10-25 17:15:42 +08001487 active_rate_basic, &left);
Zhu Yib481de92007-09-25 17:54:57 -07001488 if (*pos > 0)
1489 len += 2 + *pos;
1490
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08001491#ifdef CONFIG_IWL4965_HT
Tomas Winkler78330fd2008-02-06 02:37:18 +02001492 if (sband && sband->ht_info.ht_supported) {
1493 struct ieee80211_ht_cap *ht_cap;
Zhu Yib481de92007-09-25 17:54:57 -07001494 pos += (*pos) + 1;
1495 *pos++ = WLAN_EID_HT_CAPABILITY;
Ron Rindjunsky8fb880322007-11-26 16:14:38 +02001496 *pos++ = sizeof(struct ieee80211_ht_cap);
Tomas Winkler78330fd2008-02-06 02:37:18 +02001497 ht_cap = (struct ieee80211_ht_cap *)pos;
1498 ht_cap->cap_info = cpu_to_le16(sband->ht_info.cap);
1499 memcpy(ht_cap->supp_mcs_set, sband->ht_info.supp_mcs_set, 16);
1500 ht_cap->ampdu_params_info =(sband->ht_info.ampdu_factor &
1501 IEEE80211_HT_CAP_AMPDU_FACTOR) |
1502 ((sband->ht_info.ampdu_density << 2) &
1503 IEEE80211_HT_CAP_AMPDU_DENSITY);
Ron Rindjunsky8fb880322007-11-26 16:14:38 +02001504 len += 2 + sizeof(struct ieee80211_ht_cap);
Zhu Yib481de92007-09-25 17:54:57 -07001505 }
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08001506#endif /*CONFIG_IWL4965_HT */
Zhu Yib481de92007-09-25 17:54:57 -07001507
1508 fill_end:
1509 return (u16)len;
1510}
1511
1512/*
1513 * QoS support
1514*/
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001515static int iwl4965_send_qos_params_command(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001516 struct iwl4965_qosparam_cmd *qos)
Zhu Yib481de92007-09-25 17:54:57 -07001517{
1518
Tomas Winkler857485c2008-03-21 13:53:44 -07001519 return iwl_send_cmd_pdu(priv, REPLY_QOS_PARAM,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001520 sizeof(struct iwl4965_qosparam_cmd), qos);
Zhu Yib481de92007-09-25 17:54:57 -07001521}
1522
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001523static void iwl4965_activate_qos(struct iwl_priv *priv, u8 force)
Zhu Yib481de92007-09-25 17:54:57 -07001524{
1525 unsigned long flags;
1526
Zhu Yib481de92007-09-25 17:54:57 -07001527 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
1528 return;
1529
1530 if (!priv->qos_data.qos_enable)
1531 return;
1532
1533 spin_lock_irqsave(&priv->lock, flags);
1534 priv->qos_data.def_qos_parm.qos_flags = 0;
1535
1536 if (priv->qos_data.qos_cap.q_AP.queue_request &&
1537 !priv->qos_data.qos_cap.q_AP.txop_request)
1538 priv->qos_data.def_qos_parm.qos_flags |=
1539 QOS_PARAM_FLG_TXOP_TYPE_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07001540 if (priv->qos_data.qos_active)
1541 priv->qos_data.def_qos_parm.qos_flags |=
1542 QOS_PARAM_FLG_UPDATE_EDCA_MSK;
1543
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08001544#ifdef CONFIG_IWL4965_HT
Ron Rindjunskyfd105e72007-11-26 16:14:39 +02001545 if (priv->current_ht_config.is_ht)
Tomas Winklerf1f1f5c2007-10-25 17:15:26 +08001546 priv->qos_data.def_qos_parm.qos_flags |= QOS_PARAM_FLG_TGN_MSK;
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08001547#endif /* CONFIG_IWL4965_HT */
Tomas Winklerf1f1f5c2007-10-25 17:15:26 +08001548
Zhu Yib481de92007-09-25 17:54:57 -07001549 spin_unlock_irqrestore(&priv->lock, flags);
1550
Tomas Winkler3109ece2008-03-28 16:33:35 -07001551 if (force || iwl_is_associated(priv)) {
Tomas Winklerf1f1f5c2007-10-25 17:15:26 +08001552 IWL_DEBUG_QOS("send QoS cmd with Qos active=%d FLAGS=0x%X\n",
1553 priv->qos_data.qos_active,
1554 priv->qos_data.def_qos_parm.qos_flags);
Zhu Yib481de92007-09-25 17:54:57 -07001555
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001556 iwl4965_send_qos_params_command(priv,
Zhu Yib481de92007-09-25 17:54:57 -07001557 &(priv->qos_data.def_qos_parm));
1558 }
1559}
1560
Zhu Yib481de92007-09-25 17:54:57 -07001561/*
1562 * Power management (not Tx power!) functions
1563 */
1564#define MSEC_TO_USEC 1024
1565
1566#define NOSLP __constant_cpu_to_le16(0), 0, 0
1567#define SLP IWL_POWER_DRIVER_ALLOW_SLEEP_MSK, 0, 0
1568#define SLP_TIMEOUT(T) __constant_cpu_to_le32((T) * MSEC_TO_USEC)
1569#define SLP_VEC(X0, X1, X2, X3, X4) {__constant_cpu_to_le32(X0), \
1570 __constant_cpu_to_le32(X1), \
1571 __constant_cpu_to_le32(X2), \
1572 __constant_cpu_to_le32(X3), \
1573 __constant_cpu_to_le32(X4)}
1574
1575
1576/* default power management (not Tx power) table values */
1577/* for tim 0-10 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001578static struct iwl4965_power_vec_entry range_0[IWL_POWER_AC] = {
Zhu Yib481de92007-09-25 17:54:57 -07001579 {{NOSLP, SLP_TIMEOUT(0), SLP_TIMEOUT(0), SLP_VEC(0, 0, 0, 0, 0)}, 0},
1580 {{SLP, SLP_TIMEOUT(200), SLP_TIMEOUT(500), SLP_VEC(1, 2, 3, 4, 4)}, 0},
1581 {{SLP, SLP_TIMEOUT(200), SLP_TIMEOUT(300), SLP_VEC(2, 4, 6, 7, 7)}, 0},
1582 {{SLP, SLP_TIMEOUT(50), SLP_TIMEOUT(100), SLP_VEC(2, 6, 9, 9, 10)}, 0},
1583 {{SLP, SLP_TIMEOUT(50), SLP_TIMEOUT(25), SLP_VEC(2, 7, 9, 9, 10)}, 1},
1584 {{SLP, SLP_TIMEOUT(25), SLP_TIMEOUT(25), SLP_VEC(4, 7, 10, 10, 10)}, 1}
1585};
1586
1587/* for tim > 10 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001588static struct iwl4965_power_vec_entry range_1[IWL_POWER_AC] = {
Zhu Yib481de92007-09-25 17:54:57 -07001589 {{NOSLP, SLP_TIMEOUT(0), SLP_TIMEOUT(0), SLP_VEC(0, 0, 0, 0, 0)}, 0},
1590 {{SLP, SLP_TIMEOUT(200), SLP_TIMEOUT(500),
1591 SLP_VEC(1, 2, 3, 4, 0xFF)}, 0},
1592 {{SLP, SLP_TIMEOUT(200), SLP_TIMEOUT(300),
1593 SLP_VEC(2, 4, 6, 7, 0xFF)}, 0},
1594 {{SLP, SLP_TIMEOUT(50), SLP_TIMEOUT(100),
1595 SLP_VEC(2, 6, 9, 9, 0xFF)}, 0},
1596 {{SLP, SLP_TIMEOUT(50), SLP_TIMEOUT(25), SLP_VEC(2, 7, 9, 9, 0xFF)}, 0},
1597 {{SLP, SLP_TIMEOUT(25), SLP_TIMEOUT(25),
1598 SLP_VEC(4, 7, 10, 10, 0xFF)}, 0}
1599};
1600
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001601int iwl4965_power_init_handle(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001602{
1603 int rc = 0, i;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001604 struct iwl4965_power_mgr *pow_data;
1605 int size = sizeof(struct iwl4965_power_vec_entry) * IWL_POWER_AC;
Zhu Yib481de92007-09-25 17:54:57 -07001606 u16 pci_pm;
1607
1608 IWL_DEBUG_POWER("Initialize power \n");
1609
1610 pow_data = &(priv->power_data);
1611
1612 memset(pow_data, 0, sizeof(*pow_data));
1613
1614 pow_data->active_index = IWL_POWER_RANGE_0;
1615 pow_data->dtim_val = 0xffff;
1616
1617 memcpy(&pow_data->pwr_range_0[0], &range_0[0], size);
1618 memcpy(&pow_data->pwr_range_1[0], &range_1[0], size);
1619
1620 rc = pci_read_config_word(priv->pci_dev, PCI_LINK_CTRL, &pci_pm);
1621 if (rc != 0)
1622 return 0;
1623 else {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001624 struct iwl4965_powertable_cmd *cmd;
Zhu Yib481de92007-09-25 17:54:57 -07001625
1626 IWL_DEBUG_POWER("adjust power command flags\n");
1627
1628 for (i = 0; i < IWL_POWER_AC; i++) {
1629 cmd = &pow_data->pwr_range_0[i].cmd;
1630
1631 if (pci_pm & 0x1)
1632 cmd->flags &= ~IWL_POWER_PCI_PM_MSK;
1633 else
1634 cmd->flags |= IWL_POWER_PCI_PM_MSK;
1635 }
1636 }
1637 return rc;
1638}
1639
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001640static int iwl4965_update_power_cmd(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001641 struct iwl4965_powertable_cmd *cmd, u32 mode)
Zhu Yib481de92007-09-25 17:54:57 -07001642{
1643 int rc = 0, i;
1644 u8 skip;
1645 u32 max_sleep = 0;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001646 struct iwl4965_power_vec_entry *range;
Zhu Yib481de92007-09-25 17:54:57 -07001647 u8 period = 0;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001648 struct iwl4965_power_mgr *pow_data;
Zhu Yib481de92007-09-25 17:54:57 -07001649
1650 if (mode > IWL_POWER_INDEX_5) {
1651 IWL_DEBUG_POWER("Error invalid power mode \n");
1652 return -1;
1653 }
1654 pow_data = &(priv->power_data);
1655
1656 if (pow_data->active_index == IWL_POWER_RANGE_0)
1657 range = &pow_data->pwr_range_0[0];
1658 else
1659 range = &pow_data->pwr_range_1[1];
1660
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001661 memcpy(cmd, &range[mode].cmd, sizeof(struct iwl4965_powertable_cmd));
Zhu Yib481de92007-09-25 17:54:57 -07001662
1663#ifdef IWL_MAC80211_DISABLE
1664 if (priv->assoc_network != NULL) {
1665 unsigned long flags;
1666
1667 period = priv->assoc_network->tim.tim_period;
1668 }
1669#endif /*IWL_MAC80211_DISABLE */
1670 skip = range[mode].no_dtim;
1671
1672 if (period == 0) {
1673 period = 1;
1674 skip = 0;
1675 }
1676
1677 if (skip == 0) {
1678 max_sleep = period;
1679 cmd->flags &= ~IWL_POWER_SLEEP_OVER_DTIM_MSK;
1680 } else {
1681 __le32 slp_itrvl = cmd->sleep_interval[IWL_POWER_VEC_SIZE - 1];
1682 max_sleep = (le32_to_cpu(slp_itrvl) / period) * period;
1683 cmd->flags |= IWL_POWER_SLEEP_OVER_DTIM_MSK;
1684 }
1685
1686 for (i = 0; i < IWL_POWER_VEC_SIZE; i++) {
1687 if (le32_to_cpu(cmd->sleep_interval[i]) > max_sleep)
1688 cmd->sleep_interval[i] = cpu_to_le32(max_sleep);
1689 }
1690
1691 IWL_DEBUG_POWER("Flags value = 0x%08X\n", cmd->flags);
1692 IWL_DEBUG_POWER("Tx timeout = %u\n", le32_to_cpu(cmd->tx_data_timeout));
1693 IWL_DEBUG_POWER("Rx timeout = %u\n", le32_to_cpu(cmd->rx_data_timeout));
1694 IWL_DEBUG_POWER("Sleep interval vector = { %d , %d , %d , %d , %d }\n",
1695 le32_to_cpu(cmd->sleep_interval[0]),
1696 le32_to_cpu(cmd->sleep_interval[1]),
1697 le32_to_cpu(cmd->sleep_interval[2]),
1698 le32_to_cpu(cmd->sleep_interval[3]),
1699 le32_to_cpu(cmd->sleep_interval[4]));
1700
1701 return rc;
1702}
1703
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001704static int iwl4965_send_power_mode(struct iwl_priv *priv, u32 mode)
Zhu Yib481de92007-09-25 17:54:57 -07001705{
John W. Linville9a62f732007-11-15 16:27:36 -05001706 u32 uninitialized_var(final_mode);
Zhu Yib481de92007-09-25 17:54:57 -07001707 int rc;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001708 struct iwl4965_powertable_cmd cmd;
Zhu Yib481de92007-09-25 17:54:57 -07001709
1710 /* If on battery, set to 3,
Ian Schram01ebd062007-10-25 17:15:22 +08001711 * if plugged into AC power, set to CAM ("continuously aware mode"),
Zhu Yib481de92007-09-25 17:54:57 -07001712 * else user level */
1713 switch (mode) {
1714 case IWL_POWER_BATTERY:
1715 final_mode = IWL_POWER_INDEX_3;
1716 break;
1717 case IWL_POWER_AC:
1718 final_mode = IWL_POWER_MODE_CAM;
1719 break;
1720 default:
1721 final_mode = mode;
1722 break;
1723 }
1724
1725 cmd.keep_alive_beacons = 0;
1726
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001727 iwl4965_update_power_cmd(priv, &cmd, final_mode);
Zhu Yib481de92007-09-25 17:54:57 -07001728
Tomas Winkler857485c2008-03-21 13:53:44 -07001729 rc = iwl_send_cmd_pdu(priv, POWER_TABLE_CMD, sizeof(cmd), &cmd);
Zhu Yib481de92007-09-25 17:54:57 -07001730
1731 if (final_mode == IWL_POWER_MODE_CAM)
1732 clear_bit(STATUS_POWER_PMI, &priv->status);
1733 else
1734 set_bit(STATUS_POWER_PMI, &priv->status);
1735
1736 return rc;
1737}
1738
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001739int iwl4965_is_network_packet(struct iwl_priv *priv, struct ieee80211_hdr *header)
Zhu Yib481de92007-09-25 17:54:57 -07001740{
1741 /* Filter incoming packets to determine if they are targeted toward
1742 * this network, discarding packets coming from ourselves */
1743 switch (priv->iw_mode) {
1744 case IEEE80211_IF_TYPE_IBSS: /* Header: Dest. | Source | BSSID */
1745 /* packets from our adapter are dropped (echo) */
1746 if (!compare_ether_addr(header->addr2, priv->mac_addr))
1747 return 0;
1748 /* {broad,multi}cast packets to our IBSS go through */
1749 if (is_multicast_ether_addr(header->addr1))
1750 return !compare_ether_addr(header->addr3, priv->bssid);
1751 /* packets to our adapter go through */
1752 return !compare_ether_addr(header->addr1, priv->mac_addr);
1753 case IEEE80211_IF_TYPE_STA: /* Header: Dest. | AP{BSSID} | Source */
1754 /* packets from our adapter are dropped (echo) */
1755 if (!compare_ether_addr(header->addr3, priv->mac_addr))
1756 return 0;
1757 /* {broad,multi}cast packets to our BSS go through */
1758 if (is_multicast_ether_addr(header->addr1))
1759 return !compare_ether_addr(header->addr2, priv->bssid);
1760 /* packets to our adapter go through */
1761 return !compare_ether_addr(header->addr1, priv->mac_addr);
Tomas Winkler69dc5d92008-03-25 16:33:41 -07001762 default:
1763 break;
Zhu Yib481de92007-09-25 17:54:57 -07001764 }
1765
1766 return 1;
1767}
1768
1769#define TX_STATUS_ENTRY(x) case TX_STATUS_FAIL_ ## x: return #x
1770
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001771static const char *iwl4965_get_tx_fail_reason(u32 status)
Zhu Yib481de92007-09-25 17:54:57 -07001772{
1773 switch (status & TX_STATUS_MSK) {
1774 case TX_STATUS_SUCCESS:
1775 return "SUCCESS";
1776 TX_STATUS_ENTRY(SHORT_LIMIT);
1777 TX_STATUS_ENTRY(LONG_LIMIT);
1778 TX_STATUS_ENTRY(FIFO_UNDERRUN);
1779 TX_STATUS_ENTRY(MGMNT_ABORT);
1780 TX_STATUS_ENTRY(NEXT_FRAG);
1781 TX_STATUS_ENTRY(LIFE_EXPIRE);
1782 TX_STATUS_ENTRY(DEST_PS);
1783 TX_STATUS_ENTRY(ABORTED);
1784 TX_STATUS_ENTRY(BT_RETRY);
1785 TX_STATUS_ENTRY(STA_INVALID);
1786 TX_STATUS_ENTRY(FRAG_DROPPED);
1787 TX_STATUS_ENTRY(TID_DISABLE);
1788 TX_STATUS_ENTRY(FRAME_FLUSHED);
1789 TX_STATUS_ENTRY(INSUFFICIENT_CF_POLL);
1790 TX_STATUS_ENTRY(TX_LOCKED);
1791 TX_STATUS_ENTRY(NO_BEACON_ON_RADAR);
1792 }
1793
1794 return "UNKNOWN";
1795}
1796
1797/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001798 * iwl4965_scan_cancel - Cancel any currently executing HW scan
Zhu Yib481de92007-09-25 17:54:57 -07001799 *
1800 * NOTE: priv->mutex is not required before calling this function
1801 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001802static int iwl4965_scan_cancel(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001803{
1804 if (!test_bit(STATUS_SCAN_HW, &priv->status)) {
1805 clear_bit(STATUS_SCANNING, &priv->status);
1806 return 0;
1807 }
1808
1809 if (test_bit(STATUS_SCANNING, &priv->status)) {
1810 if (!test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
1811 IWL_DEBUG_SCAN("Queuing scan abort.\n");
1812 set_bit(STATUS_SCAN_ABORTING, &priv->status);
1813 queue_work(priv->workqueue, &priv->abort_scan);
1814
1815 } else
1816 IWL_DEBUG_SCAN("Scan abort already in progress.\n");
1817
1818 return test_bit(STATUS_SCANNING, &priv->status);
1819 }
1820
1821 return 0;
1822}
1823
1824/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001825 * iwl4965_scan_cancel_timeout - Cancel any currently executing HW scan
Zhu Yib481de92007-09-25 17:54:57 -07001826 * @ms: amount of time to wait (in milliseconds) for scan to abort
1827 *
1828 * NOTE: priv->mutex must be held before calling this function
1829 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001830static int iwl4965_scan_cancel_timeout(struct iwl_priv *priv, unsigned long ms)
Zhu Yib481de92007-09-25 17:54:57 -07001831{
1832 unsigned long now = jiffies;
1833 int ret;
1834
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001835 ret = iwl4965_scan_cancel(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001836 if (ret && ms) {
1837 mutex_unlock(&priv->mutex);
1838 while (!time_after(jiffies, now + msecs_to_jiffies(ms)) &&
1839 test_bit(STATUS_SCANNING, &priv->status))
1840 msleep(1);
1841 mutex_lock(&priv->mutex);
1842
1843 return test_bit(STATUS_SCANNING, &priv->status);
1844 }
1845
1846 return ret;
1847}
1848
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001849static void iwl4965_sequence_reset(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001850{
1851 /* Reset ieee stats */
1852
1853 /* We don't reset the net_device_stats (ieee->stats) on
1854 * re-association */
1855
1856 priv->last_seq_num = -1;
1857 priv->last_frag_num = -1;
1858 priv->last_packet_time = 0;
1859
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001860 iwl4965_scan_cancel(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001861}
1862
1863#define MAX_UCODE_BEACON_INTERVAL 4096
1864#define INTEL_CONN_LISTEN_INTERVAL __constant_cpu_to_le16(0xA)
1865
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001866static __le16 iwl4965_adjust_beacon_interval(u16 beacon_val)
Zhu Yib481de92007-09-25 17:54:57 -07001867{
1868 u16 new_val = 0;
1869 u16 beacon_factor = 0;
1870
1871 beacon_factor =
1872 (beacon_val + MAX_UCODE_BEACON_INTERVAL)
1873 / MAX_UCODE_BEACON_INTERVAL;
1874 new_val = beacon_val / beacon_factor;
1875
1876 return cpu_to_le16(new_val);
1877}
1878
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001879static void iwl4965_setup_rxon_timing(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001880{
1881 u64 interval_tm_unit;
1882 u64 tsf, result;
1883 unsigned long flags;
1884 struct ieee80211_conf *conf = NULL;
1885 u16 beacon_int = 0;
1886
1887 conf = ieee80211_get_hw_conf(priv->hw);
1888
1889 spin_lock_irqsave(&priv->lock, flags);
Tomas Winkler3109ece2008-03-28 16:33:35 -07001890 priv->rxon_timing.timestamp.dw[1] = cpu_to_le32(priv->timestamp >> 32);
1891 priv->rxon_timing.timestamp.dw[0] =
1892 cpu_to_le32(priv->timestamp & 0xFFFFFFFF);
Zhu Yib481de92007-09-25 17:54:57 -07001893
1894 priv->rxon_timing.listen_interval = INTEL_CONN_LISTEN_INTERVAL;
1895
Tomas Winkler3109ece2008-03-28 16:33:35 -07001896 tsf = priv->timestamp;
Zhu Yib481de92007-09-25 17:54:57 -07001897
1898 beacon_int = priv->beacon_int;
1899 spin_unlock_irqrestore(&priv->lock, flags);
1900
1901 if (priv->iw_mode == IEEE80211_IF_TYPE_STA) {
1902 if (beacon_int == 0) {
1903 priv->rxon_timing.beacon_interval = cpu_to_le16(100);
1904 priv->rxon_timing.beacon_init_val = cpu_to_le32(102400);
1905 } else {
1906 priv->rxon_timing.beacon_interval =
1907 cpu_to_le16(beacon_int);
1908 priv->rxon_timing.beacon_interval =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001909 iwl4965_adjust_beacon_interval(
Zhu Yib481de92007-09-25 17:54:57 -07001910 le16_to_cpu(priv->rxon_timing.beacon_interval));
1911 }
1912
1913 priv->rxon_timing.atim_window = 0;
1914 } else {
1915 priv->rxon_timing.beacon_interval =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001916 iwl4965_adjust_beacon_interval(conf->beacon_int);
Zhu Yib481de92007-09-25 17:54:57 -07001917 /* TODO: we need to get atim_window from upper stack
1918 * for now we set to 0 */
1919 priv->rxon_timing.atim_window = 0;
1920 }
1921
1922 interval_tm_unit =
1923 (le16_to_cpu(priv->rxon_timing.beacon_interval) * 1024);
1924 result = do_div(tsf, interval_tm_unit);
1925 priv->rxon_timing.beacon_init_val =
1926 cpu_to_le32((u32) ((u64) interval_tm_unit - result));
1927
1928 IWL_DEBUG_ASSOC
1929 ("beacon interval %d beacon timer %d beacon tim %d\n",
1930 le16_to_cpu(priv->rxon_timing.beacon_interval),
1931 le32_to_cpu(priv->rxon_timing.beacon_init_val),
1932 le16_to_cpu(priv->rxon_timing.atim_window));
1933}
1934
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001935static int iwl4965_scan_initiate(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001936{
1937 if (priv->iw_mode == IEEE80211_IF_TYPE_AP) {
1938 IWL_ERROR("APs don't scan.\n");
1939 return 0;
1940 }
1941
Tomas Winklerfee12472008-04-03 16:05:21 -07001942 if (!iwl_is_ready_rf(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07001943 IWL_DEBUG_SCAN("Aborting scan due to not ready.\n");
1944 return -EIO;
1945 }
1946
1947 if (test_bit(STATUS_SCANNING, &priv->status)) {
1948 IWL_DEBUG_SCAN("Scan already in progress.\n");
1949 return -EAGAIN;
1950 }
1951
1952 if (test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
1953 IWL_DEBUG_SCAN("Scan request while abort pending. "
1954 "Queuing.\n");
1955 return -EAGAIN;
1956 }
1957
1958 IWL_DEBUG_INFO("Starting scan...\n");
1959 priv->scan_bands = 2;
1960 set_bit(STATUS_SCANNING, &priv->status);
1961 priv->scan_start = jiffies;
1962 priv->scan_pass_start = priv->scan_start;
1963
1964 queue_work(priv->workqueue, &priv->request_scan);
1965
1966 return 0;
1967}
1968
Zhu Yib481de92007-09-25 17:54:57 -07001969
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001970static void iwl4965_set_flags_for_phymode(struct iwl_priv *priv,
Johannes Berg8318d782008-01-24 19:38:38 +01001971 enum ieee80211_band band)
Zhu Yib481de92007-09-25 17:54:57 -07001972{
Johannes Berg8318d782008-01-24 19:38:38 +01001973 if (band == IEEE80211_BAND_5GHZ) {
Zhu Yib481de92007-09-25 17:54:57 -07001974 priv->staging_rxon.flags &=
1975 ~(RXON_FLG_BAND_24G_MSK | RXON_FLG_AUTO_DETECT_MSK
1976 | RXON_FLG_CCK_MSK);
1977 priv->staging_rxon.flags |= RXON_FLG_SHORT_SLOT_MSK;
1978 } else {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001979 /* Copied from iwl4965_bg_post_associate() */
Zhu Yib481de92007-09-25 17:54:57 -07001980 if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_SLOT_TIME)
1981 priv->staging_rxon.flags |= RXON_FLG_SHORT_SLOT_MSK;
1982 else
1983 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
1984
1985 if (priv->iw_mode == IEEE80211_IF_TYPE_IBSS)
1986 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
1987
1988 priv->staging_rxon.flags |= RXON_FLG_BAND_24G_MSK;
1989 priv->staging_rxon.flags |= RXON_FLG_AUTO_DETECT_MSK;
1990 priv->staging_rxon.flags &= ~RXON_FLG_CCK_MSK;
1991 }
1992}
1993
1994/*
Ian Schram01ebd062007-10-25 17:15:22 +08001995 * initialize rxon structure with default values from eeprom
Zhu Yib481de92007-09-25 17:54:57 -07001996 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001997static void iwl4965_connection_init_rx_config(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001998{
Assaf Kraussbf85ea42008-03-14 10:38:49 -07001999 const struct iwl_channel_info *ch_info;
Zhu Yib481de92007-09-25 17:54:57 -07002000
2001 memset(&priv->staging_rxon, 0, sizeof(priv->staging_rxon));
2002
2003 switch (priv->iw_mode) {
2004 case IEEE80211_IF_TYPE_AP:
2005 priv->staging_rxon.dev_type = RXON_DEV_TYPE_AP;
2006 break;
2007
2008 case IEEE80211_IF_TYPE_STA:
2009 priv->staging_rxon.dev_type = RXON_DEV_TYPE_ESS;
2010 priv->staging_rxon.filter_flags = RXON_FILTER_ACCEPT_GRP_MSK;
2011 break;
2012
2013 case IEEE80211_IF_TYPE_IBSS:
2014 priv->staging_rxon.dev_type = RXON_DEV_TYPE_IBSS;
2015 priv->staging_rxon.flags = RXON_FLG_SHORT_PREAMBLE_MSK;
2016 priv->staging_rxon.filter_flags = RXON_FILTER_BCON_AWARE_MSK |
2017 RXON_FILTER_ACCEPT_GRP_MSK;
2018 break;
2019
2020 case IEEE80211_IF_TYPE_MNTR:
2021 priv->staging_rxon.dev_type = RXON_DEV_TYPE_SNIFFER;
2022 priv->staging_rxon.filter_flags = RXON_FILTER_PROMISC_MSK |
2023 RXON_FILTER_CTL2HOST_MSK | RXON_FILTER_ACCEPT_GRP_MSK;
2024 break;
Tomas Winkler69dc5d92008-03-25 16:33:41 -07002025 default:
2026 IWL_ERROR("Unsupported interface type %d\n", priv->iw_mode);
2027 break;
Zhu Yib481de92007-09-25 17:54:57 -07002028 }
2029
2030#if 0
2031 /* TODO: Figure out when short_preamble would be set and cache from
2032 * that */
2033 if (!hw_to_local(priv->hw)->short_preamble)
2034 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_PREAMBLE_MSK;
2035 else
2036 priv->staging_rxon.flags |= RXON_FLG_SHORT_PREAMBLE_MSK;
2037#endif
2038
Assaf Krauss8622e702008-03-21 13:53:43 -07002039 ch_info = iwl_get_channel_info(priv, priv->band,
Zhu Yib481de92007-09-25 17:54:57 -07002040 le16_to_cpu(priv->staging_rxon.channel));
2041
2042 if (!ch_info)
2043 ch_info = &priv->channel_info[0];
2044
2045 /*
2046 * in some case A channels are all non IBSS
2047 * in this case force B/G channel
2048 */
2049 if ((priv->iw_mode == IEEE80211_IF_TYPE_IBSS) &&
2050 !(is_channel_ibss(ch_info)))
2051 ch_info = &priv->channel_info[0];
2052
2053 priv->staging_rxon.channel = cpu_to_le16(ch_info->channel);
Johannes Berg8318d782008-01-24 19:38:38 +01002054 priv->band = ch_info->band;
Zhu Yib481de92007-09-25 17:54:57 -07002055
Johannes Berg8318d782008-01-24 19:38:38 +01002056 iwl4965_set_flags_for_phymode(priv, priv->band);
Zhu Yib481de92007-09-25 17:54:57 -07002057
2058 priv->staging_rxon.ofdm_basic_rates =
2059 (IWL_OFDM_RATES_MASK >> IWL_FIRST_OFDM_RATE) & 0xFF;
2060 priv->staging_rxon.cck_basic_rates =
2061 (IWL_CCK_RATES_MASK >> IWL_FIRST_CCK_RATE) & 0xF;
2062
2063 priv->staging_rxon.flags &= ~(RXON_FLG_CHANNEL_MODE_MIXED_MSK |
2064 RXON_FLG_CHANNEL_MODE_PURE_40_MSK);
2065 memcpy(priv->staging_rxon.node_addr, priv->mac_addr, ETH_ALEN);
2066 memcpy(priv->staging_rxon.wlap_bssid_addr, priv->mac_addr, ETH_ALEN);
2067 priv->staging_rxon.ofdm_ht_single_stream_basic_rates = 0xff;
2068 priv->staging_rxon.ofdm_ht_dual_stream_basic_rates = 0xff;
2069 iwl4965_set_rxon_chain(priv);
2070}
2071
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002072static int iwl4965_set_mode(struct iwl_priv *priv, int mode)
Zhu Yib481de92007-09-25 17:54:57 -07002073{
Zhu Yib481de92007-09-25 17:54:57 -07002074 if (mode == IEEE80211_IF_TYPE_IBSS) {
Assaf Kraussbf85ea42008-03-14 10:38:49 -07002075 const struct iwl_channel_info *ch_info;
Zhu Yib481de92007-09-25 17:54:57 -07002076
Assaf Krauss8622e702008-03-21 13:53:43 -07002077 ch_info = iwl_get_channel_info(priv,
Johannes Berg8318d782008-01-24 19:38:38 +01002078 priv->band,
Zhu Yib481de92007-09-25 17:54:57 -07002079 le16_to_cpu(priv->staging_rxon.channel));
2080
2081 if (!ch_info || !is_channel_ibss(ch_info)) {
2082 IWL_ERROR("channel %d not IBSS channel\n",
2083 le16_to_cpu(priv->staging_rxon.channel));
2084 return -EINVAL;
2085 }
2086 }
2087
Zhu Yib481de92007-09-25 17:54:57 -07002088 priv->iw_mode = mode;
2089
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002090 iwl4965_connection_init_rx_config(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002091 memcpy(priv->staging_rxon.node_addr, priv->mac_addr, ETH_ALEN);
2092
Assaf Kraussbf85ea42008-03-14 10:38:49 -07002093 iwlcore_clear_stations_table(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002094
Mohamed Abbasfde35712007-11-29 11:10:15 +08002095 /* dont commit rxon if rf-kill is on*/
Tomas Winklerfee12472008-04-03 16:05:21 -07002096 if (!iwl_is_ready_rf(priv))
Mohamed Abbasfde35712007-11-29 11:10:15 +08002097 return -EAGAIN;
2098
2099 cancel_delayed_work(&priv->scan_check);
2100 if (iwl4965_scan_cancel_timeout(priv, 100)) {
2101 IWL_WARNING("Aborted scan still in progress after 100ms\n");
2102 IWL_DEBUG_MAC80211("leaving - scan abort failed.\n");
2103 return -EAGAIN;
2104 }
2105
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002106 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002107
2108 return 0;
2109}
2110
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002111static void iwl4965_build_tx_cmd_hwcrypto(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07002112 struct ieee80211_tx_control *ctl,
Tomas Winkler857485c2008-03-21 13:53:44 -07002113 struct iwl_cmd *cmd,
Zhu Yib481de92007-09-25 17:54:57 -07002114 struct sk_buff *skb_frag,
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07002115 int sta_id)
Zhu Yib481de92007-09-25 17:54:57 -07002116{
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07002117 struct iwl4965_hw_key *keyinfo = &priv->stations[sta_id].keyinfo;
Zhu Yib481de92007-09-25 17:54:57 -07002118
2119 switch (keyinfo->alg) {
2120 case ALG_CCMP:
2121 cmd->cmd.tx.sec_ctl = TX_CMD_SEC_CCM;
2122 memcpy(cmd->cmd.tx.key, keyinfo->key, keyinfo->keylen);
Max Stepanov8236e182008-03-12 16:58:48 -07002123 if (ctl->flags & IEEE80211_TXCTL_AMPDU)
2124 cmd->cmd.tx.tx_flags |= TX_CMD_FLG_AGG_CCMP_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07002125 IWL_DEBUG_TX("tx_cmd with aes hwcrypto\n");
2126 break;
2127
2128 case ALG_TKIP:
Zhu Yib481de92007-09-25 17:54:57 -07002129 cmd->cmd.tx.sec_ctl = TX_CMD_SEC_TKIP;
Emmanuel Grumbach2bc75082008-03-19 16:41:45 -07002130 ieee80211_get_tkip_key(keyinfo->conf, skb_frag,
2131 IEEE80211_TKIP_P2_KEY, cmd->cmd.tx.key);
2132 IWL_DEBUG_TX("tx_cmd with tkip hwcrypto\n");
Zhu Yib481de92007-09-25 17:54:57 -07002133 break;
2134
2135 case ALG_WEP:
2136 cmd->cmd.tx.sec_ctl = TX_CMD_SEC_WEP |
2137 (ctl->key_idx & TX_CMD_SEC_MSK) << TX_CMD_SEC_SHIFT;
2138
2139 if (keyinfo->keylen == 13)
2140 cmd->cmd.tx.sec_ctl |= TX_CMD_SEC_KEY128;
2141
2142 memcpy(&cmd->cmd.tx.key[3], keyinfo->key, keyinfo->keylen);
2143
2144 IWL_DEBUG_TX("Configuring packet for WEP encryption "
2145 "with key %d\n", ctl->key_idx);
2146 break;
2147
Zhu Yib481de92007-09-25 17:54:57 -07002148 default:
2149 printk(KERN_ERR "Unknown encode alg %d\n", keyinfo->alg);
2150 break;
2151 }
2152}
2153
2154/*
2155 * handle build REPLY_TX command notification.
2156 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002157static void iwl4965_build_tx_cmd_basic(struct iwl_priv *priv,
Tomas Winkler857485c2008-03-21 13:53:44 -07002158 struct iwl_cmd *cmd,
Zhu Yib481de92007-09-25 17:54:57 -07002159 struct ieee80211_tx_control *ctrl,
2160 struct ieee80211_hdr *hdr,
2161 int is_unicast, u8 std_id)
2162{
2163 __le16 *qc;
2164 u16 fc = le16_to_cpu(hdr->frame_control);
2165 __le32 tx_flags = cmd->cmd.tx.tx_flags;
2166
2167 cmd->cmd.tx.stop_time.life_time = TX_CMD_LIFE_TIME_INFINITE;
2168 if (!(ctrl->flags & IEEE80211_TXCTL_NO_ACK)) {
2169 tx_flags |= TX_CMD_FLG_ACK_MSK;
2170 if ((fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_MGMT)
2171 tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
2172 if (ieee80211_is_probe_response(fc) &&
2173 !(le16_to_cpu(hdr->seq_ctrl) & 0xf))
2174 tx_flags |= TX_CMD_FLG_TSF_MSK;
2175 } else {
2176 tx_flags &= (~TX_CMD_FLG_ACK_MSK);
2177 tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
2178 }
2179
Tomas Winkler87e4f7d2008-01-14 17:46:16 -08002180 if (ieee80211_is_back_request(fc))
2181 tx_flags |= TX_CMD_FLG_ACK_MSK | TX_CMD_FLG_IMM_BA_RSP_MASK;
2182
2183
Zhu Yib481de92007-09-25 17:54:57 -07002184 cmd->cmd.tx.sta_id = std_id;
2185 if (ieee80211_get_morefrag(hdr))
2186 tx_flags |= TX_CMD_FLG_MORE_FRAG_MSK;
2187
2188 qc = ieee80211_get_qos_ctrl(hdr);
2189 if (qc) {
2190 cmd->cmd.tx.tid_tspec = (u8) (le16_to_cpu(*qc) & 0xf);
2191 tx_flags &= ~TX_CMD_FLG_SEQ_CTL_MSK;
2192 } else
2193 tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
2194
2195 if (ctrl->flags & IEEE80211_TXCTL_USE_RTS_CTS) {
2196 tx_flags |= TX_CMD_FLG_RTS_MSK;
2197 tx_flags &= ~TX_CMD_FLG_CTS_MSK;
2198 } else if (ctrl->flags & IEEE80211_TXCTL_USE_CTS_PROTECT) {
2199 tx_flags &= ~TX_CMD_FLG_RTS_MSK;
2200 tx_flags |= TX_CMD_FLG_CTS_MSK;
2201 }
2202
2203 if ((tx_flags & TX_CMD_FLG_RTS_MSK) || (tx_flags & TX_CMD_FLG_CTS_MSK))
2204 tx_flags |= TX_CMD_FLG_FULL_TXOP_PROT_MSK;
2205
2206 tx_flags &= ~(TX_CMD_FLG_ANT_SEL_MSK);
2207 if ((fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_MGMT) {
2208 if ((fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_ASSOC_REQ ||
2209 (fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_REASSOC_REQ)
Ian Schrambc434dd2007-10-25 17:15:29 +08002210 cmd->cmd.tx.timeout.pm_frame_timeout = cpu_to_le16(3);
Zhu Yib481de92007-09-25 17:54:57 -07002211 else
Ian Schrambc434dd2007-10-25 17:15:29 +08002212 cmd->cmd.tx.timeout.pm_frame_timeout = cpu_to_le16(2);
Mohamed Abbasab53d8a2008-03-25 16:33:36 -07002213 } else {
Zhu Yib481de92007-09-25 17:54:57 -07002214 cmd->cmd.tx.timeout.pm_frame_timeout = 0;
Mohamed Abbasab53d8a2008-03-25 16:33:36 -07002215 }
Zhu Yib481de92007-09-25 17:54:57 -07002216
2217 cmd->cmd.tx.driver_txop = 0;
2218 cmd->cmd.tx.tx_flags = tx_flags;
2219 cmd->cmd.tx.next_frame_len = 0;
2220}
Tomas Winkler19758be2008-03-12 16:58:51 -07002221static void iwl_update_tx_stats(struct iwl_priv *priv, u16 fc, u16 len)
2222{
2223 /* 0 - mgmt, 1 - cnt, 2 - data */
2224 int idx = (fc & IEEE80211_FCTL_FTYPE) >> 2;
2225 priv->tx_stats[idx].cnt++;
2226 priv->tx_stats[idx].bytes += len;
2227}
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002228/**
2229 * iwl4965_get_sta_id - Find station's index within station table
2230 *
2231 * If new IBSS station, create new entry in station table
2232 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002233static int iwl4965_get_sta_id(struct iwl_priv *priv,
Ben Cahill9fbab512007-11-29 11:09:47 +08002234 struct ieee80211_hdr *hdr)
Zhu Yib481de92007-09-25 17:54:57 -07002235{
2236 int sta_id;
2237 u16 fc = le16_to_cpu(hdr->frame_control);
Joe Perches0795af52007-10-03 17:59:30 -07002238 DECLARE_MAC_BUF(mac);
Zhu Yib481de92007-09-25 17:54:57 -07002239
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002240 /* If this frame is broadcast or management, use broadcast station id */
Zhu Yib481de92007-09-25 17:54:57 -07002241 if (((fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_DATA) ||
2242 is_multicast_ether_addr(hdr->addr1))
2243 return priv->hw_setting.bcast_sta_id;
2244
2245 switch (priv->iw_mode) {
2246
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002247 /* If we are a client station in a BSS network, use the special
2248 * AP station entry (that's the only station we communicate with) */
Zhu Yib481de92007-09-25 17:54:57 -07002249 case IEEE80211_IF_TYPE_STA:
2250 return IWL_AP_ID;
2251
2252 /* If we are an AP, then find the station, or use BCAST */
2253 case IEEE80211_IF_TYPE_AP:
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002254 sta_id = iwl4965_hw_find_station(priv, hdr->addr1);
Zhu Yib481de92007-09-25 17:54:57 -07002255 if (sta_id != IWL_INVALID_STATION)
2256 return sta_id;
2257 return priv->hw_setting.bcast_sta_id;
2258
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002259 /* If this frame is going out to an IBSS network, find the station,
2260 * or create a new station table entry */
Zhu Yib481de92007-09-25 17:54:57 -07002261 case IEEE80211_IF_TYPE_IBSS:
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002262 sta_id = iwl4965_hw_find_station(priv, hdr->addr1);
Zhu Yib481de92007-09-25 17:54:57 -07002263 if (sta_id != IWL_INVALID_STATION)
2264 return sta_id;
2265
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002266 /* Create new station table entry */
Ron Rindjunsky67d62032007-11-26 16:14:40 +02002267 sta_id = iwl4965_add_station_flags(priv, hdr->addr1,
2268 0, CMD_ASYNC, NULL);
Zhu Yib481de92007-09-25 17:54:57 -07002269
2270 if (sta_id != IWL_INVALID_STATION)
2271 return sta_id;
2272
Joe Perches0795af52007-10-03 17:59:30 -07002273 IWL_DEBUG_DROP("Station %s not in station map. "
Zhu Yib481de92007-09-25 17:54:57 -07002274 "Defaulting to broadcast...\n",
Joe Perches0795af52007-10-03 17:59:30 -07002275 print_mac(mac, hdr->addr1));
Tomas Winkler0a6857e2008-03-12 16:58:49 -07002276 iwl_print_hex_dump(IWL_DL_DROP, (u8 *) hdr, sizeof(*hdr));
Zhu Yib481de92007-09-25 17:54:57 -07002277 return priv->hw_setting.bcast_sta_id;
2278
2279 default:
Ian Schram01ebd062007-10-25 17:15:22 +08002280 IWL_WARNING("Unknown mode of operation: %d", priv->iw_mode);
Zhu Yib481de92007-09-25 17:54:57 -07002281 return priv->hw_setting.bcast_sta_id;
2282 }
2283}
2284
2285/*
2286 * start REPLY_TX command process
2287 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002288static int iwl4965_tx_skb(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07002289 struct sk_buff *skb, struct ieee80211_tx_control *ctl)
2290{
2291 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002292 struct iwl4965_tfd_frame *tfd;
Zhu Yib481de92007-09-25 17:54:57 -07002293 u32 *control_flags;
2294 int txq_id = ctl->queue;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002295 struct iwl4965_tx_queue *txq = NULL;
2296 struct iwl4965_queue *q = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07002297 dma_addr_t phys_addr;
2298 dma_addr_t txcmd_phys;
Tomas Winkler87e4f7d2008-01-14 17:46:16 -08002299 dma_addr_t scratch_phys;
Tomas Winkler857485c2008-03-21 13:53:44 -07002300 struct iwl_cmd *out_cmd = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07002301 u16 len, idx, len_org;
2302 u8 id, hdr_len, unicast;
2303 u8 sta_id;
2304 u16 seq_number = 0;
2305 u16 fc;
2306 __le16 *qc;
2307 u8 wait_write_ptr = 0;
2308 unsigned long flags;
2309 int rc;
2310
2311 spin_lock_irqsave(&priv->lock, flags);
Tomas Winklerfee12472008-04-03 16:05:21 -07002312 if (iwl_is_rfkill(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07002313 IWL_DEBUG_DROP("Dropping - RF KILL\n");
2314 goto drop_unlock;
2315 }
2316
Johannes Berg32bfd352007-12-19 01:31:26 +01002317 if (!priv->vif) {
2318 IWL_DEBUG_DROP("Dropping - !priv->vif\n");
Zhu Yib481de92007-09-25 17:54:57 -07002319 goto drop_unlock;
2320 }
2321
Johannes Berg8318d782008-01-24 19:38:38 +01002322 if ((ctl->tx_rate->hw_value & 0xFF) == IWL_INVALID_RATE) {
Zhu Yib481de92007-09-25 17:54:57 -07002323 IWL_ERROR("ERROR: No TX rate available.\n");
2324 goto drop_unlock;
2325 }
2326
2327 unicast = !is_multicast_ether_addr(hdr->addr1);
2328 id = 0;
2329
2330 fc = le16_to_cpu(hdr->frame_control);
2331
Tomas Winkler0a6857e2008-03-12 16:58:49 -07002332#ifdef CONFIG_IWLWIFI_DEBUG
Zhu Yib481de92007-09-25 17:54:57 -07002333 if (ieee80211_is_auth(fc))
2334 IWL_DEBUG_TX("Sending AUTH frame\n");
2335 else if (ieee80211_is_assoc_request(fc))
2336 IWL_DEBUG_TX("Sending ASSOC frame\n");
2337 else if (ieee80211_is_reassoc_request(fc))
2338 IWL_DEBUG_TX("Sending REASSOC frame\n");
2339#endif
2340
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08002341 /* drop all data frame if we are not associated */
Gregory Greenman76f39152008-01-23 10:15:21 -08002342 if (((fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_DATA) &&
Tomas Winkler3109ece2008-03-28 16:33:35 -07002343 (!iwl_is_associated(priv) ||
Reinette Chatrea6477242008-02-14 10:40:28 -08002344 ((priv->iw_mode == IEEE80211_IF_TYPE_STA) && !priv->assoc_id) ||
Gregory Greenman76f39152008-01-23 10:15:21 -08002345 !priv->assoc_station_added)) {
Tomas Winkler3109ece2008-03-28 16:33:35 -07002346 IWL_DEBUG_DROP("Dropping - !iwl_is_associated\n");
Zhu Yib481de92007-09-25 17:54:57 -07002347 goto drop_unlock;
2348 }
2349
2350 spin_unlock_irqrestore(&priv->lock, flags);
2351
2352 hdr_len = ieee80211_get_hdrlen(fc);
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002353
2354 /* Find (or create) index into station table for destination station */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002355 sta_id = iwl4965_get_sta_id(priv, hdr);
Zhu Yib481de92007-09-25 17:54:57 -07002356 if (sta_id == IWL_INVALID_STATION) {
Joe Perches0795af52007-10-03 17:59:30 -07002357 DECLARE_MAC_BUF(mac);
2358
2359 IWL_DEBUG_DROP("Dropping - INVALID STATION: %s\n",
2360 print_mac(mac, hdr->addr1));
Zhu Yib481de92007-09-25 17:54:57 -07002361 goto drop;
2362 }
2363
2364 IWL_DEBUG_RATE("station Id %d\n", sta_id);
2365
2366 qc = ieee80211_get_qos_ctrl(hdr);
2367 if (qc) {
2368 u8 tid = (u8)(le16_to_cpu(*qc) & 0xf);
2369 seq_number = priv->stations[sta_id].tid[tid].seq_number &
2370 IEEE80211_SCTL_SEQ;
2371 hdr->seq_ctrl = cpu_to_le16(seq_number) |
2372 (hdr->seq_ctrl &
2373 __constant_cpu_to_le16(IEEE80211_SCTL_FRAG));
2374 seq_number += 0x10;
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08002375#ifdef CONFIG_IWL4965_HT
Zhu Yib481de92007-09-25 17:54:57 -07002376 /* aggregation is on for this <sta,tid> */
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02002377 if (ctl->flags & IEEE80211_TXCTL_AMPDU)
Zhu Yib481de92007-09-25 17:54:57 -07002378 txq_id = priv->stations[sta_id].tid[tid].agg.txq_id;
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02002379 priv->stations[sta_id].tid[tid].tfds_in_queue++;
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08002380#endif /* CONFIG_IWL4965_HT */
Zhu Yib481de92007-09-25 17:54:57 -07002381 }
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002382
2383 /* Descriptor for chosen Tx queue */
Zhu Yib481de92007-09-25 17:54:57 -07002384 txq = &priv->txq[txq_id];
2385 q = &txq->q;
2386
2387 spin_lock_irqsave(&priv->lock, flags);
2388
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002389 /* Set up first empty TFD within this queue's circular TFD buffer */
Tomas Winklerfc4b6852007-10-25 17:15:24 +08002390 tfd = &txq->bd[q->write_ptr];
Zhu Yib481de92007-09-25 17:54:57 -07002391 memset(tfd, 0, sizeof(*tfd));
2392 control_flags = (u32 *) tfd;
Tomas Winklerfc4b6852007-10-25 17:15:24 +08002393 idx = get_cmd_index(q, q->write_ptr, 0);
Zhu Yib481de92007-09-25 17:54:57 -07002394
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002395 /* Set up driver data for this TFD */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002396 memset(&(txq->txb[q->write_ptr]), 0, sizeof(struct iwl4965_tx_info));
Tomas Winklerfc4b6852007-10-25 17:15:24 +08002397 txq->txb[q->write_ptr].skb[0] = skb;
2398 memcpy(&(txq->txb[q->write_ptr].status.control),
Zhu Yib481de92007-09-25 17:54:57 -07002399 ctl, sizeof(struct ieee80211_tx_control));
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002400
2401 /* Set up first empty entry in queue's array of Tx/cmd buffers */
Zhu Yib481de92007-09-25 17:54:57 -07002402 out_cmd = &txq->cmd[idx];
2403 memset(&out_cmd->hdr, 0, sizeof(out_cmd->hdr));
2404 memset(&out_cmd->cmd.tx, 0, sizeof(out_cmd->cmd.tx));
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002405
2406 /*
2407 * Set up the Tx-command (not MAC!) header.
2408 * Store the chosen Tx queue and TFD index within the sequence field;
2409 * after Tx, uCode's Tx response will return this value so driver can
2410 * locate the frame within the tx queue and do post-tx processing.
2411 */
Zhu Yib481de92007-09-25 17:54:57 -07002412 out_cmd->hdr.cmd = REPLY_TX;
2413 out_cmd->hdr.sequence = cpu_to_le16((u16)(QUEUE_TO_SEQ(txq_id) |
Tomas Winklerfc4b6852007-10-25 17:15:24 +08002414 INDEX_TO_SEQ(q->write_ptr)));
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002415
2416 /* Copy MAC header from skb into command buffer */
Zhu Yib481de92007-09-25 17:54:57 -07002417 memcpy(out_cmd->cmd.tx.hdr, hdr, hdr_len);
2418
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002419 /*
2420 * Use the first empty entry in this queue's command buffer array
2421 * to contain the Tx command and MAC header concatenated together
2422 * (payload data will be in another buffer).
2423 * Size of this varies, due to varying MAC header length.
2424 * If end is not dword aligned, we'll have 2 extra bytes at the end
2425 * of the MAC header (device reads on dword boundaries).
2426 * We'll tell device about this padding later.
2427 */
Zhu Yib481de92007-09-25 17:54:57 -07002428 len = priv->hw_setting.tx_cmd_len +
Tomas Winkler857485c2008-03-21 13:53:44 -07002429 sizeof(struct iwl_cmd_header) + hdr_len;
Zhu Yib481de92007-09-25 17:54:57 -07002430
2431 len_org = len;
2432 len = (len + 3) & ~3;
2433
2434 if (len_org != len)
2435 len_org = 1;
2436 else
2437 len_org = 0;
2438
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002439 /* Physical address of this Tx command's header (not MAC header!),
2440 * within command buffer array. */
Tomas Winkler857485c2008-03-21 13:53:44 -07002441 txcmd_phys = txq->dma_addr_cmd + sizeof(struct iwl_cmd) * idx +
2442 offsetof(struct iwl_cmd, hdr);
Zhu Yib481de92007-09-25 17:54:57 -07002443
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002444 /* Add buffer containing Tx command and MAC(!) header to TFD's
2445 * first entry */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002446 iwl4965_hw_txq_attach_buf_to_tfd(priv, tfd, txcmd_phys, len);
Zhu Yib481de92007-09-25 17:54:57 -07002447
2448 if (!(ctl->flags & IEEE80211_TXCTL_DO_NOT_ENCRYPT))
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07002449 iwl4965_build_tx_cmd_hwcrypto(priv, ctl, out_cmd, skb, sta_id);
Zhu Yib481de92007-09-25 17:54:57 -07002450
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002451 /* Set up TFD's 2nd entry to point directly to remainder of skb,
2452 * if any (802.11 null frames have no payload). */
Zhu Yib481de92007-09-25 17:54:57 -07002453 len = skb->len - hdr_len;
2454 if (len) {
2455 phys_addr = pci_map_single(priv->pci_dev, skb->data + hdr_len,
2456 len, PCI_DMA_TODEVICE);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002457 iwl4965_hw_txq_attach_buf_to_tfd(priv, tfd, phys_addr, len);
Zhu Yib481de92007-09-25 17:54:57 -07002458 }
2459
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002460 /* Tell 4965 about any 2-byte padding after MAC header */
Zhu Yib481de92007-09-25 17:54:57 -07002461 if (len_org)
2462 out_cmd->cmd.tx.tx_flags |= TX_CMD_FLG_MH_PAD_MSK;
2463
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002464 /* Total # bytes to be transmitted */
Zhu Yib481de92007-09-25 17:54:57 -07002465 len = (u16)skb->len;
2466 out_cmd->cmd.tx.len = cpu_to_le16(len);
2467
2468 /* TODO need this for burst mode later on */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002469 iwl4965_build_tx_cmd_basic(priv, out_cmd, ctl, hdr, unicast, sta_id);
Zhu Yib481de92007-09-25 17:54:57 -07002470
2471 /* set is_hcca to 0; it probably will never be implemented */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002472 iwl4965_hw_build_tx_cmd_rate(priv, out_cmd, ctl, hdr, sta_id, 0);
Zhu Yib481de92007-09-25 17:54:57 -07002473
Tomas Winkler19758be2008-03-12 16:58:51 -07002474 iwl_update_tx_stats(priv, fc, len);
2475
Tomas Winkler857485c2008-03-21 13:53:44 -07002476 scratch_phys = txcmd_phys + sizeof(struct iwl_cmd_header) +
Tomas Winkler87e4f7d2008-01-14 17:46:16 -08002477 offsetof(struct iwl4965_tx_cmd, scratch);
2478 out_cmd->cmd.tx.dram_lsb_ptr = cpu_to_le32(scratch_phys);
2479 out_cmd->cmd.tx.dram_msb_ptr = iwl_get_dma_hi_address(scratch_phys);
2480
Zhu Yib481de92007-09-25 17:54:57 -07002481 if (!ieee80211_get_morefrag(hdr)) {
2482 txq->need_update = 1;
2483 if (qc) {
2484 u8 tid = (u8)(le16_to_cpu(*qc) & 0xf);
2485 priv->stations[sta_id].tid[tid].seq_number = seq_number;
2486 }
2487 } else {
2488 wait_write_ptr = 1;
2489 txq->need_update = 0;
2490 }
2491
Tomas Winkler0a6857e2008-03-12 16:58:49 -07002492 iwl_print_hex_dump(IWL_DL_TX, out_cmd->cmd.payload,
Zhu Yib481de92007-09-25 17:54:57 -07002493 sizeof(out_cmd->cmd.tx));
2494
Tomas Winkler0a6857e2008-03-12 16:58:49 -07002495 iwl_print_hex_dump(IWL_DL_TX, (u8 *)out_cmd->cmd.tx.hdr,
Zhu Yib481de92007-09-25 17:54:57 -07002496 ieee80211_get_hdrlen(fc));
2497
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002498 /* Set up entry for this TFD in Tx byte-count array */
Zhu Yib481de92007-09-25 17:54:57 -07002499 iwl4965_tx_queue_update_wr_ptr(priv, txq, len);
2500
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002501 /* Tell device the write index *just past* this latest filled TFD */
Tomas Winklerc54b6792008-03-06 17:36:53 -08002502 q->write_ptr = iwl_queue_inc_wrap(q->write_ptr, q->n_bd);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002503 rc = iwl4965_tx_queue_update_write_ptr(priv, txq);
Zhu Yib481de92007-09-25 17:54:57 -07002504 spin_unlock_irqrestore(&priv->lock, flags);
2505
2506 if (rc)
2507 return rc;
2508
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002509 if ((iwl4965_queue_space(q) < q->high_mark)
Zhu Yib481de92007-09-25 17:54:57 -07002510 && priv->mac80211_registered) {
2511 if (wait_write_ptr) {
2512 spin_lock_irqsave(&priv->lock, flags);
2513 txq->need_update = 1;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002514 iwl4965_tx_queue_update_write_ptr(priv, txq);
Zhu Yib481de92007-09-25 17:54:57 -07002515 spin_unlock_irqrestore(&priv->lock, flags);
2516 }
2517
2518 ieee80211_stop_queue(priv->hw, ctl->queue);
2519 }
2520
2521 return 0;
2522
2523drop_unlock:
2524 spin_unlock_irqrestore(&priv->lock, flags);
2525drop:
2526 return -1;
2527}
2528
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002529static void iwl4965_set_rate(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002530{
Johannes Berg8318d782008-01-24 19:38:38 +01002531 const struct ieee80211_supported_band *hw = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07002532 struct ieee80211_rate *rate;
2533 int i;
2534
Johannes Berg8318d782008-01-24 19:38:38 +01002535 hw = iwl4965_get_hw_mode(priv, priv->band);
Saleem Abdulrasoolc4ba9622007-11-18 23:59:08 -08002536 if (!hw) {
2537 IWL_ERROR("Failed to set rate: unable to get hw mode\n");
2538 return;
2539 }
Zhu Yib481de92007-09-25 17:54:57 -07002540
2541 priv->active_rate = 0;
2542 priv->active_rate_basic = 0;
2543
Johannes Berg8318d782008-01-24 19:38:38 +01002544 for (i = 0; i < hw->n_bitrates; i++) {
2545 rate = &(hw->bitrates[i]);
2546 if (rate->hw_value < IWL_RATE_COUNT)
2547 priv->active_rate |= (1 << rate->hw_value);
Zhu Yib481de92007-09-25 17:54:57 -07002548 }
2549
2550 IWL_DEBUG_RATE("Set active_rate = %0x, active_rate_basic = %0x\n",
2551 priv->active_rate, priv->active_rate_basic);
2552
2553 /*
2554 * If a basic rate is configured, then use it (adding IWL_RATE_1M_MASK)
2555 * otherwise set it to the default of all CCK rates and 6, 12, 24 for
2556 * OFDM
2557 */
2558 if (priv->active_rate_basic & IWL_CCK_BASIC_RATES_MASK)
2559 priv->staging_rxon.cck_basic_rates =
2560 ((priv->active_rate_basic &
2561 IWL_CCK_RATES_MASK) >> IWL_FIRST_CCK_RATE) & 0xF;
2562 else
2563 priv->staging_rxon.cck_basic_rates =
2564 (IWL_CCK_BASIC_RATES_MASK >> IWL_FIRST_CCK_RATE) & 0xF;
2565
2566 if (priv->active_rate_basic & IWL_OFDM_BASIC_RATES_MASK)
2567 priv->staging_rxon.ofdm_basic_rates =
2568 ((priv->active_rate_basic &
2569 (IWL_OFDM_BASIC_RATES_MASK | IWL_RATE_6M_MASK)) >>
2570 IWL_FIRST_OFDM_RATE) & 0xFF;
2571 else
2572 priv->staging_rxon.ofdm_basic_rates =
2573 (IWL_OFDM_BASIC_RATES_MASK >> IWL_FIRST_OFDM_RATE) & 0xFF;
2574}
2575
Mohamed Abbasad97edd2008-03-28 16:21:06 -07002576void iwl4965_radio_kill_sw(struct iwl_priv *priv, int disable_radio)
Zhu Yib481de92007-09-25 17:54:57 -07002577{
2578 unsigned long flags;
2579
2580 if (!!disable_radio == test_bit(STATUS_RF_KILL_SW, &priv->status))
2581 return;
2582
2583 IWL_DEBUG_RF_KILL("Manual SW RF KILL set to: RADIO %s\n",
2584 disable_radio ? "OFF" : "ON");
2585
2586 if (disable_radio) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002587 iwl4965_scan_cancel(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002588 /* FIXME: This is a workaround for AP */
2589 if (priv->iw_mode != IEEE80211_IF_TYPE_AP) {
2590 spin_lock_irqsave(&priv->lock, flags);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07002591 iwl_write32(priv, CSR_UCODE_DRV_GP1_SET,
Zhu Yib481de92007-09-25 17:54:57 -07002592 CSR_UCODE_SW_BIT_RFKILL);
2593 spin_unlock_irqrestore(&priv->lock, flags);
Mohamed Abbasad97edd2008-03-28 16:21:06 -07002594 /* call the host command only if no hw rf-kill set */
2595 if (!test_bit(STATUS_RF_KILL_HW, &priv->status))
2596 iwl4965_send_card_state(priv,
2597 CARD_STATE_CMD_DISABLE,
2598 0);
Zhu Yib481de92007-09-25 17:54:57 -07002599 set_bit(STATUS_RF_KILL_SW, &priv->status);
Mohamed Abbasad97edd2008-03-28 16:21:06 -07002600
2601 /* make sure mac80211 stop sending Tx frame */
2602 if (priv->mac80211_registered)
2603 ieee80211_stop_queues(priv->hw);
Zhu Yib481de92007-09-25 17:54:57 -07002604 }
2605 return;
2606 }
2607
2608 spin_lock_irqsave(&priv->lock, flags);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07002609 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
Zhu Yib481de92007-09-25 17:54:57 -07002610
2611 clear_bit(STATUS_RF_KILL_SW, &priv->status);
2612 spin_unlock_irqrestore(&priv->lock, flags);
2613
2614 /* wake up ucode */
2615 msleep(10);
2616
2617 spin_lock_irqsave(&priv->lock, flags);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07002618 iwl_read32(priv, CSR_UCODE_DRV_GP1);
2619 if (!iwl_grab_nic_access(priv))
2620 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002621 spin_unlock_irqrestore(&priv->lock, flags);
2622
2623 if (test_bit(STATUS_RF_KILL_HW, &priv->status)) {
2624 IWL_DEBUG_RF_KILL("Can not turn radio back on - "
2625 "disabled by HW switch\n");
2626 return;
2627 }
2628
2629 queue_work(priv->workqueue, &priv->restart);
2630 return;
2631}
2632
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002633void iwl4965_set_decrypted_flag(struct iwl_priv *priv, struct sk_buff *skb,
Zhu Yib481de92007-09-25 17:54:57 -07002634 u32 decrypt_res, struct ieee80211_rx_status *stats)
2635{
2636 u16 fc =
2637 le16_to_cpu(((struct ieee80211_hdr *)skb->data)->frame_control);
2638
2639 if (priv->active_rxon.filter_flags & RXON_FILTER_DIS_DECRYPT_MSK)
2640 return;
2641
2642 if (!(fc & IEEE80211_FCTL_PROTECTED))
2643 return;
2644
2645 IWL_DEBUG_RX("decrypt_res:0x%x\n", decrypt_res);
2646 switch (decrypt_res & RX_RES_STATUS_SEC_TYPE_MSK) {
2647 case RX_RES_STATUS_SEC_TYPE_TKIP:
Emmanuel Grumbach17e476b2008-03-19 16:41:42 -07002648 /* The uCode has got a bad phase 1 Key, pushes the packet.
2649 * Decryption will be done in SW. */
2650 if ((decrypt_res & RX_RES_STATUS_DECRYPT_TYPE_MSK) ==
2651 RX_RES_STATUS_BAD_KEY_TTAK)
2652 break;
2653
Zhu Yib481de92007-09-25 17:54:57 -07002654 if ((decrypt_res & RX_RES_STATUS_DECRYPT_TYPE_MSK) ==
2655 RX_RES_STATUS_BAD_ICV_MIC)
2656 stats->flag |= RX_FLAG_MMIC_ERROR;
2657 case RX_RES_STATUS_SEC_TYPE_WEP:
2658 case RX_RES_STATUS_SEC_TYPE_CCMP:
2659 if ((decrypt_res & RX_RES_STATUS_DECRYPT_TYPE_MSK) ==
2660 RX_RES_STATUS_DECRYPT_OK) {
2661 IWL_DEBUG_RX("hw decrypt successfully!!!\n");
2662 stats->flag |= RX_FLAG_DECRYPTED;
2663 }
2664 break;
2665
2666 default:
2667 break;
2668 }
2669}
2670
Zhu Yib481de92007-09-25 17:54:57 -07002671
2672#define IWL_PACKET_RETRY_TIME HZ
2673
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002674int iwl4965_is_duplicate_packet(struct iwl_priv *priv, struct ieee80211_hdr *header)
Zhu Yib481de92007-09-25 17:54:57 -07002675{
2676 u16 sc = le16_to_cpu(header->seq_ctrl);
2677 u16 seq = (sc & IEEE80211_SCTL_SEQ) >> 4;
2678 u16 frag = sc & IEEE80211_SCTL_FRAG;
2679 u16 *last_seq, *last_frag;
2680 unsigned long *last_time;
2681
2682 switch (priv->iw_mode) {
2683 case IEEE80211_IF_TYPE_IBSS:{
2684 struct list_head *p;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002685 struct iwl4965_ibss_seq *entry = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07002686 u8 *mac = header->addr2;
2687 int index = mac[5] & (IWL_IBSS_MAC_HASH_SIZE - 1);
2688
2689 __list_for_each(p, &priv->ibss_mac_hash[index]) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002690 entry = list_entry(p, struct iwl4965_ibss_seq, list);
Zhu Yib481de92007-09-25 17:54:57 -07002691 if (!compare_ether_addr(entry->mac, mac))
2692 break;
2693 }
2694 if (p == &priv->ibss_mac_hash[index]) {
2695 entry = kzalloc(sizeof(*entry), GFP_ATOMIC);
2696 if (!entry) {
Ian Schrambc434dd2007-10-25 17:15:29 +08002697 IWL_ERROR("Cannot malloc new mac entry\n");
Zhu Yib481de92007-09-25 17:54:57 -07002698 return 0;
2699 }
2700 memcpy(entry->mac, mac, ETH_ALEN);
2701 entry->seq_num = seq;
2702 entry->frag_num = frag;
2703 entry->packet_time = jiffies;
Ian Schrambc434dd2007-10-25 17:15:29 +08002704 list_add(&entry->list, &priv->ibss_mac_hash[index]);
Zhu Yib481de92007-09-25 17:54:57 -07002705 return 0;
2706 }
2707 last_seq = &entry->seq_num;
2708 last_frag = &entry->frag_num;
2709 last_time = &entry->packet_time;
2710 break;
2711 }
2712 case IEEE80211_IF_TYPE_STA:
2713 last_seq = &priv->last_seq_num;
2714 last_frag = &priv->last_frag_num;
2715 last_time = &priv->last_packet_time;
2716 break;
2717 default:
2718 return 0;
2719 }
2720 if ((*last_seq == seq) &&
2721 time_after(*last_time + IWL_PACKET_RETRY_TIME, jiffies)) {
2722 if (*last_frag == frag)
2723 goto drop;
2724 if (*last_frag + 1 != frag)
2725 /* out-of-order fragment */
2726 goto drop;
2727 } else
2728 *last_seq = seq;
2729
2730 *last_frag = frag;
2731 *last_time = jiffies;
2732 return 0;
2733
2734 drop:
2735 return 1;
2736}
2737
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08002738#ifdef CONFIG_IWL4965_SPECTRUM_MEASUREMENT
Zhu Yib481de92007-09-25 17:54:57 -07002739
2740#include "iwl-spectrum.h"
2741
2742#define BEACON_TIME_MASK_LOW 0x00FFFFFF
2743#define BEACON_TIME_MASK_HIGH 0xFF000000
2744#define TIME_UNIT 1024
2745
2746/*
2747 * extended beacon time format
2748 * time in usec will be changed into a 32-bit value in 8:24 format
2749 * the high 1 byte is the beacon counts
2750 * the lower 3 bytes is the time in usec within one beacon interval
2751 */
2752
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002753static u32 iwl4965_usecs_to_beacons(u32 usec, u32 beacon_interval)
Zhu Yib481de92007-09-25 17:54:57 -07002754{
2755 u32 quot;
2756 u32 rem;
2757 u32 interval = beacon_interval * 1024;
2758
2759 if (!interval || !usec)
2760 return 0;
2761
2762 quot = (usec / interval) & (BEACON_TIME_MASK_HIGH >> 24);
2763 rem = (usec % interval) & BEACON_TIME_MASK_LOW;
2764
2765 return (quot << 24) + rem;
2766}
2767
2768/* base is usually what we get from ucode with each received frame,
2769 * the same as HW timer counter counting down
2770 */
2771
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002772static __le32 iwl4965_add_beacon_time(u32 base, u32 addon, u32 beacon_interval)
Zhu Yib481de92007-09-25 17:54:57 -07002773{
2774 u32 base_low = base & BEACON_TIME_MASK_LOW;
2775 u32 addon_low = addon & BEACON_TIME_MASK_LOW;
2776 u32 interval = beacon_interval * TIME_UNIT;
2777 u32 res = (base & BEACON_TIME_MASK_HIGH) +
2778 (addon & BEACON_TIME_MASK_HIGH);
2779
2780 if (base_low > addon_low)
2781 res += base_low - addon_low;
2782 else if (base_low < addon_low) {
2783 res += interval + base_low - addon_low;
2784 res += (1 << 24);
2785 } else
2786 res += (1 << 24);
2787
2788 return cpu_to_le32(res);
2789}
2790
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002791static int iwl4965_get_measurement(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07002792 struct ieee80211_measurement_params *params,
2793 u8 type)
2794{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002795 struct iwl4965_spectrum_cmd spectrum;
2796 struct iwl4965_rx_packet *res;
Tomas Winkler857485c2008-03-21 13:53:44 -07002797 struct iwl_host_cmd cmd = {
Zhu Yib481de92007-09-25 17:54:57 -07002798 .id = REPLY_SPECTRUM_MEASUREMENT_CMD,
2799 .data = (void *)&spectrum,
2800 .meta.flags = CMD_WANT_SKB,
2801 };
2802 u32 add_time = le64_to_cpu(params->start_time);
2803 int rc;
2804 int spectrum_resp_status;
2805 int duration = le16_to_cpu(params->duration);
2806
Tomas Winkler3109ece2008-03-28 16:33:35 -07002807 if (iwl_is_associated(priv))
Zhu Yib481de92007-09-25 17:54:57 -07002808 add_time =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002809 iwl4965_usecs_to_beacons(
Zhu Yib481de92007-09-25 17:54:57 -07002810 le64_to_cpu(params->start_time) - priv->last_tsf,
2811 le16_to_cpu(priv->rxon_timing.beacon_interval));
2812
2813 memset(&spectrum, 0, sizeof(spectrum));
2814
2815 spectrum.channel_count = cpu_to_le16(1);
2816 spectrum.flags =
2817 RXON_FLG_TSF2HOST_MSK | RXON_FLG_ANT_A_MSK | RXON_FLG_DIS_DIV_MSK;
2818 spectrum.filter_flags = MEASUREMENT_FILTER_FLAG;
2819 cmd.len = sizeof(spectrum);
2820 spectrum.len = cpu_to_le16(cmd.len - sizeof(spectrum.len));
2821
Tomas Winkler3109ece2008-03-28 16:33:35 -07002822 if (iwl_is_associated(priv))
Zhu Yib481de92007-09-25 17:54:57 -07002823 spectrum.start_time =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002824 iwl4965_add_beacon_time(priv->last_beacon_time,
Zhu Yib481de92007-09-25 17:54:57 -07002825 add_time,
2826 le16_to_cpu(priv->rxon_timing.beacon_interval));
2827 else
2828 spectrum.start_time = 0;
2829
2830 spectrum.channels[0].duration = cpu_to_le32(duration * TIME_UNIT);
2831 spectrum.channels[0].channel = params->channel;
2832 spectrum.channels[0].type = type;
2833 if (priv->active_rxon.flags & RXON_FLG_BAND_24G_MSK)
2834 spectrum.flags |= RXON_FLG_BAND_24G_MSK |
2835 RXON_FLG_AUTO_DETECT_MSK | RXON_FLG_TGG_PROTECT_MSK;
2836
Tomas Winkler857485c2008-03-21 13:53:44 -07002837 rc = iwl_send_cmd_sync(priv, &cmd);
Zhu Yib481de92007-09-25 17:54:57 -07002838 if (rc)
2839 return rc;
2840
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002841 res = (struct iwl4965_rx_packet *)cmd.meta.u.skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07002842 if (res->hdr.flags & IWL_CMD_FAILED_MSK) {
2843 IWL_ERROR("Bad return from REPLY_RX_ON_ASSOC command\n");
2844 rc = -EIO;
2845 }
2846
2847 spectrum_resp_status = le16_to_cpu(res->u.spectrum.status);
2848 switch (spectrum_resp_status) {
2849 case 0: /* Command will be handled */
2850 if (res->u.spectrum.id != 0xff) {
2851 IWL_DEBUG_INFO
2852 ("Replaced existing measurement: %d\n",
2853 res->u.spectrum.id);
2854 priv->measurement_status &= ~MEASUREMENT_READY;
2855 }
2856 priv->measurement_status |= MEASUREMENT_ACTIVE;
2857 rc = 0;
2858 break;
2859
2860 case 1: /* Command will not be handled */
2861 rc = -EAGAIN;
2862 break;
2863 }
2864
2865 dev_kfree_skb_any(cmd.meta.u.skb);
2866
2867 return rc;
2868}
2869#endif
2870
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002871static void iwl4965_txstatus_to_ieee(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002872 struct iwl4965_tx_info *tx_sta)
Zhu Yib481de92007-09-25 17:54:57 -07002873{
2874
2875 tx_sta->status.ack_signal = 0;
2876 tx_sta->status.excessive_retries = 0;
2877 tx_sta->status.queue_length = 0;
2878 tx_sta->status.queue_number = 0;
2879
2880 if (in_interrupt())
2881 ieee80211_tx_status_irqsafe(priv->hw,
2882 tx_sta->skb[0], &(tx_sta->status));
2883 else
2884 ieee80211_tx_status(priv->hw,
2885 tx_sta->skb[0], &(tx_sta->status));
2886
2887 tx_sta->skb[0] = NULL;
2888}
2889
2890/**
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002891 * iwl4965_tx_queue_reclaim - Reclaim Tx queue entries already Tx'd
Zhu Yib481de92007-09-25 17:54:57 -07002892 *
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002893 * When FW advances 'R' index, all entries between old and new 'R' index
2894 * need to be reclaimed. As result, some free space forms. If there is
2895 * enough free space (> low mark), wake the stack that feeds us.
Zhu Yib481de92007-09-25 17:54:57 -07002896 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002897int iwl4965_tx_queue_reclaim(struct iwl_priv *priv, int txq_id, int index)
Zhu Yib481de92007-09-25 17:54:57 -07002898{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002899 struct iwl4965_tx_queue *txq = &priv->txq[txq_id];
2900 struct iwl4965_queue *q = &txq->q;
Zhu Yib481de92007-09-25 17:54:57 -07002901 int nfreed = 0;
2902
2903 if ((index >= q->n_bd) || (x2_queue_used(q, index) == 0)) {
2904 IWL_ERROR("Read index for DMA queue txq id (%d), index %d, "
2905 "is out of range [0-%d] %d %d.\n", txq_id,
Tomas Winklerfc4b6852007-10-25 17:15:24 +08002906 index, q->n_bd, q->write_ptr, q->read_ptr);
Zhu Yib481de92007-09-25 17:54:57 -07002907 return 0;
2908 }
2909
Tomas Winklerc54b6792008-03-06 17:36:53 -08002910 for (index = iwl_queue_inc_wrap(index, q->n_bd);
Tomas Winklerfc4b6852007-10-25 17:15:24 +08002911 q->read_ptr != index;
Tomas Winklerc54b6792008-03-06 17:36:53 -08002912 q->read_ptr = iwl_queue_inc_wrap(q->read_ptr, q->n_bd)) {
Zhu Yib481de92007-09-25 17:54:57 -07002913 if (txq_id != IWL_CMD_QUEUE_NUM) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002914 iwl4965_txstatus_to_ieee(priv,
Tomas Winklerfc4b6852007-10-25 17:15:24 +08002915 &(txq->txb[txq->q.read_ptr]));
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002916 iwl4965_hw_txq_free_tfd(priv, txq);
Zhu Yib481de92007-09-25 17:54:57 -07002917 } else if (nfreed > 1) {
2918 IWL_ERROR("HCMD skipped: index (%d) %d %d\n", index,
Tomas Winklerfc4b6852007-10-25 17:15:24 +08002919 q->write_ptr, q->read_ptr);
Zhu Yib481de92007-09-25 17:54:57 -07002920 queue_work(priv->workqueue, &priv->restart);
2921 }
2922 nfreed++;
2923 }
2924
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02002925/* if (iwl4965_queue_space(q) > q->low_mark && (txq_id >= 0) &&
Zhu Yib481de92007-09-25 17:54:57 -07002926 (txq_id != IWL_CMD_QUEUE_NUM) &&
2927 priv->mac80211_registered)
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02002928 ieee80211_wake_queue(priv->hw, txq_id); */
Zhu Yib481de92007-09-25 17:54:57 -07002929
2930
2931 return nfreed;
2932}
2933
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002934static int iwl4965_is_tx_success(u32 status)
Zhu Yib481de92007-09-25 17:54:57 -07002935{
2936 status &= TX_STATUS_MSK;
2937 return (status == TX_STATUS_SUCCESS)
2938 || (status == TX_STATUS_DIRECT_DONE);
2939}
2940
2941/******************************************************************************
2942 *
2943 * Generic RX handler implementations
2944 *
2945 ******************************************************************************/
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08002946#ifdef CONFIG_IWL4965_HT
Zhu Yib481de92007-09-25 17:54:57 -07002947
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002948static inline int iwl4965_get_ra_sta_id(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07002949 struct ieee80211_hdr *hdr)
2950{
2951 if (priv->iw_mode == IEEE80211_IF_TYPE_STA)
2952 return IWL_AP_ID;
2953 else {
2954 u8 *da = ieee80211_get_DA(hdr);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002955 return iwl4965_hw_find_station(priv, da);
Zhu Yib481de92007-09-25 17:54:57 -07002956 }
2957}
2958
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002959static struct ieee80211_hdr *iwl4965_tx_queue_get_hdr(
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002960 struct iwl_priv *priv, int txq_id, int idx)
Zhu Yib481de92007-09-25 17:54:57 -07002961{
2962 if (priv->txq[txq_id].txb[idx].skb[0])
2963 return (struct ieee80211_hdr *)priv->txq[txq_id].
2964 txb[idx].skb[0]->data;
2965 return NULL;
2966}
2967
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002968static inline u32 iwl4965_get_scd_ssn(struct iwl4965_tx_resp *tx_resp)
Zhu Yib481de92007-09-25 17:54:57 -07002969{
2970 __le32 *scd_ssn = (__le32 *)((u32 *)&tx_resp->status +
2971 tx_resp->frame_count);
2972 return le32_to_cpu(*scd_ssn) & MAX_SN;
2973
2974}
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002975
2976/**
2977 * iwl4965_tx_status_reply_tx - Handle Tx rspnse for frames in aggregation queue
2978 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002979static int iwl4965_tx_status_reply_tx(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002980 struct iwl4965_ht_agg *agg,
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02002981 struct iwl4965_tx_resp_agg *tx_resp,
Zhu Yib481de92007-09-25 17:54:57 -07002982 u16 start_idx)
2983{
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02002984 u16 status;
2985 struct agg_tx_status *frame_status = &tx_resp->status;
Zhu Yib481de92007-09-25 17:54:57 -07002986 struct ieee80211_tx_status *tx_status = NULL;
2987 struct ieee80211_hdr *hdr = NULL;
2988 int i, sh;
2989 int txq_id, idx;
2990 u16 seq;
2991
2992 if (agg->wait_for_ba)
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002993 IWL_DEBUG_TX_REPLY("got tx response w/o block-ack\n");
Zhu Yib481de92007-09-25 17:54:57 -07002994
2995 agg->frame_count = tx_resp->frame_count;
2996 agg->start_idx = start_idx;
2997 agg->rate_n_flags = le32_to_cpu(tx_resp->rate_n_flags);
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02002998 agg->bitmap = 0;
Zhu Yib481de92007-09-25 17:54:57 -07002999
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003000 /* # frames attempted by Tx command */
Zhu Yib481de92007-09-25 17:54:57 -07003001 if (agg->frame_count == 1) {
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003002 /* Only one frame was attempted; no block-ack will arrive */
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003003 status = le16_to_cpu(frame_status[0].status);
3004 seq = le16_to_cpu(frame_status[0].sequence);
3005 idx = SEQ_TO_INDEX(seq);
3006 txq_id = SEQ_TO_QUEUE(seq);
Zhu Yib481de92007-09-25 17:54:57 -07003007
Zhu Yib481de92007-09-25 17:54:57 -07003008 /* FIXME: code repetition */
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003009 IWL_DEBUG_TX_REPLY("FrameCnt = %d, StartIdx=%d idx=%d\n",
3010 agg->frame_count, agg->start_idx, idx);
Zhu Yib481de92007-09-25 17:54:57 -07003011
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003012 tx_status = &(priv->txq[txq_id].txb[idx].status);
Zhu Yib481de92007-09-25 17:54:57 -07003013 tx_status->retry_count = tx_resp->failure_frame;
3014 tx_status->queue_number = status & 0xff;
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003015 tx_status->queue_length = tx_resp->failure_rts;
3016 tx_status->control.flags &= ~IEEE80211_TXCTL_AMPDU;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003017 tx_status->flags = iwl4965_is_tx_success(status)?
Zhu Yib481de92007-09-25 17:54:57 -07003018 IEEE80211_TX_STATUS_ACK : 0;
Ron Rindjunsky4c424e42008-03-04 18:09:27 -08003019 iwl4965_hwrate_to_tx_control(priv,
3020 le32_to_cpu(tx_resp->rate_n_flags),
3021 &tx_status->control);
Zhu Yib481de92007-09-25 17:54:57 -07003022 /* FIXME: code repetition end */
3023
3024 IWL_DEBUG_TX_REPLY("1 Frame 0x%x failure :%d\n",
3025 status & 0xff, tx_resp->failure_frame);
3026 IWL_DEBUG_TX_REPLY("Rate Info rate_n_flags=%x\n",
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003027 iwl4965_hw_get_rate_n_flags(tx_resp->rate_n_flags));
Zhu Yib481de92007-09-25 17:54:57 -07003028
3029 agg->wait_for_ba = 0;
3030 } else {
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003031 /* Two or more frames were attempted; expect block-ack */
Zhu Yib481de92007-09-25 17:54:57 -07003032 u64 bitmap = 0;
3033 int start = agg->start_idx;
3034
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003035 /* Construct bit-map of pending frames within Tx window */
Zhu Yib481de92007-09-25 17:54:57 -07003036 for (i = 0; i < agg->frame_count; i++) {
3037 u16 sc;
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003038 status = le16_to_cpu(frame_status[i].status);
3039 seq = le16_to_cpu(frame_status[i].sequence);
Zhu Yib481de92007-09-25 17:54:57 -07003040 idx = SEQ_TO_INDEX(seq);
3041 txq_id = SEQ_TO_QUEUE(seq);
3042
3043 if (status & (AGG_TX_STATE_FEW_BYTES_MSK |
3044 AGG_TX_STATE_ABORT_MSK))
3045 continue;
3046
3047 IWL_DEBUG_TX_REPLY("FrameCnt = %d, txq_id=%d idx=%d\n",
3048 agg->frame_count, txq_id, idx);
3049
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003050 hdr = iwl4965_tx_queue_get_hdr(priv, txq_id, idx);
Zhu Yib481de92007-09-25 17:54:57 -07003051
3052 sc = le16_to_cpu(hdr->seq_ctrl);
3053 if (idx != (SEQ_TO_SN(sc) & 0xff)) {
3054 IWL_ERROR("BUG_ON idx doesn't match seq control"
3055 " idx=%d, seq_idx=%d, seq=%d\n",
3056 idx, SEQ_TO_SN(sc),
3057 hdr->seq_ctrl);
3058 return -1;
3059 }
3060
3061 IWL_DEBUG_TX_REPLY("AGG Frame i=%d idx %d seq=%d\n",
3062 i, idx, SEQ_TO_SN(sc));
3063
3064 sh = idx - start;
3065 if (sh > 64) {
3066 sh = (start - idx) + 0xff;
3067 bitmap = bitmap << sh;
3068 sh = 0;
3069 start = idx;
3070 } else if (sh < -64)
3071 sh = 0xff - (start - idx);
3072 else if (sh < 0) {
3073 sh = start - idx;
3074 start = idx;
3075 bitmap = bitmap << sh;
3076 sh = 0;
3077 }
3078 bitmap |= (1 << sh);
3079 IWL_DEBUG_TX_REPLY("start=%d bitmap=0x%x\n",
3080 start, (u32)(bitmap & 0xFFFFFFFF));
3081 }
3082
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003083 agg->bitmap = bitmap;
Zhu Yib481de92007-09-25 17:54:57 -07003084 agg->start_idx = start;
3085 agg->rate_n_flags = le32_to_cpu(tx_resp->rate_n_flags);
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003086 IWL_DEBUG_TX_REPLY("Frames %d start_idx=%d bitmap=0x%llx\n",
Zhu Yib481de92007-09-25 17:54:57 -07003087 agg->frame_count, agg->start_idx,
John W. Linville06501d22008-04-01 17:38:47 -04003088 (unsigned long long)agg->bitmap);
Zhu Yib481de92007-09-25 17:54:57 -07003089
3090 if (bitmap)
3091 agg->wait_for_ba = 1;
3092 }
3093 return 0;
3094}
3095#endif
Zhu Yib481de92007-09-25 17:54:57 -07003096
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003097/**
3098 * iwl4965_rx_reply_tx - Handle standard (non-aggregation) Tx response
3099 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003100static void iwl4965_rx_reply_tx(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003101 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003102{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003103 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07003104 u16 sequence = le16_to_cpu(pkt->hdr.sequence);
3105 int txq_id = SEQ_TO_QUEUE(sequence);
3106 int index = SEQ_TO_INDEX(sequence);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003107 struct iwl4965_tx_queue *txq = &priv->txq[txq_id];
Zhu Yib481de92007-09-25 17:54:57 -07003108 struct ieee80211_tx_status *tx_status;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003109 struct iwl4965_tx_resp *tx_resp = (void *)&pkt->u.raw[0];
Zhu Yib481de92007-09-25 17:54:57 -07003110 u32 status = le32_to_cpu(tx_resp->status);
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08003111#ifdef CONFIG_IWL4965_HT
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003112 int tid = MAX_TID_COUNT, sta_id = IWL_INVALID_STATION;
3113 struct ieee80211_hdr *hdr;
3114 __le16 *qc;
Zhu Yib481de92007-09-25 17:54:57 -07003115#endif
3116
3117 if ((index >= txq->q.n_bd) || (x2_queue_used(&txq->q, index) == 0)) {
3118 IWL_ERROR("Read index for DMA queue txq_id (%d) index %d "
3119 "is out of range [0-%d] %d %d\n", txq_id,
Tomas Winklerfc4b6852007-10-25 17:15:24 +08003120 index, txq->q.n_bd, txq->q.write_ptr,
3121 txq->q.read_ptr);
Zhu Yib481de92007-09-25 17:54:57 -07003122 return;
3123 }
3124
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08003125#ifdef CONFIG_IWL4965_HT
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003126 hdr = iwl4965_tx_queue_get_hdr(priv, txq_id, index);
3127 qc = ieee80211_get_qos_ctrl(hdr);
Zhu Yib481de92007-09-25 17:54:57 -07003128
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003129 if (qc)
Zhu Yib481de92007-09-25 17:54:57 -07003130 tid = le16_to_cpu(*qc) & 0xf;
3131
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003132 sta_id = iwl4965_get_ra_sta_id(priv, hdr);
3133 if (txq->sched_retry && unlikely(sta_id == IWL_INVALID_STATION)) {
3134 IWL_ERROR("Station not known\n");
3135 return;
3136 }
3137
3138 if (txq->sched_retry) {
3139 const u32 scd_ssn = iwl4965_get_scd_ssn(tx_resp);
3140 struct iwl4965_ht_agg *agg = NULL;
3141
3142 if (!qc)
Zhu Yib481de92007-09-25 17:54:57 -07003143 return;
Zhu Yib481de92007-09-25 17:54:57 -07003144
3145 agg = &priv->stations[sta_id].tid[tid].agg;
3146
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003147 iwl4965_tx_status_reply_tx(priv, agg,
3148 (struct iwl4965_tx_resp_agg *)tx_resp, index);
Zhu Yib481de92007-09-25 17:54:57 -07003149
3150 if ((tx_resp->frame_count == 1) &&
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003151 !iwl4965_is_tx_success(status)) {
Zhu Yib481de92007-09-25 17:54:57 -07003152 /* TODO: send BAR */
3153 }
3154
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003155 if (txq->q.read_ptr != (scd_ssn & 0xff)) {
3156 int freed;
Tomas Winklerc54b6792008-03-06 17:36:53 -08003157 index = iwl_queue_dec_wrap(scd_ssn & 0xff, txq->q.n_bd);
Zhu Yib481de92007-09-25 17:54:57 -07003158 IWL_DEBUG_TX_REPLY("Retry scheduler reclaim scd_ssn "
3159 "%d index %d\n", scd_ssn , index);
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003160 freed = iwl4965_tx_queue_reclaim(priv, txq_id, index);
3161 priv->stations[sta_id].tid[tid].tfds_in_queue -= freed;
3162
3163 if (iwl4965_queue_space(&txq->q) > txq->q.low_mark &&
3164 txq_id >= 0 && priv->mac80211_registered &&
3165 agg->state != IWL_EMPTYING_HW_QUEUE_DELBA)
3166 ieee80211_wake_queue(priv->hw, txq_id);
3167
3168 iwl4965_check_empty_hw_queue(priv, sta_id, tid, txq_id);
Zhu Yib481de92007-09-25 17:54:57 -07003169 }
3170 } else {
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08003171#endif /* CONFIG_IWL4965_HT */
Tomas Winklerfc4b6852007-10-25 17:15:24 +08003172 tx_status = &(txq->txb[txq->q.read_ptr].status);
Zhu Yib481de92007-09-25 17:54:57 -07003173
3174 tx_status->retry_count = tx_resp->failure_frame;
3175 tx_status->queue_number = status;
3176 tx_status->queue_length = tx_resp->bt_kill_count;
3177 tx_status->queue_length |= tx_resp->failure_rts;
Zhu Yib481de92007-09-25 17:54:57 -07003178 tx_status->flags =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003179 iwl4965_is_tx_success(status) ? IEEE80211_TX_STATUS_ACK : 0;
Ron Rindjunsky4c424e42008-03-04 18:09:27 -08003180 iwl4965_hwrate_to_tx_control(priv, le32_to_cpu(tx_resp->rate_n_flags),
3181 &tx_status->control);
Zhu Yib481de92007-09-25 17:54:57 -07003182
Zhu Yib481de92007-09-25 17:54:57 -07003183 IWL_DEBUG_TX("Tx queue %d Status %s (0x%08x) rate_n_flags 0x%x "
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003184 "retries %d\n", txq_id, iwl4965_get_tx_fail_reason(status),
Zhu Yib481de92007-09-25 17:54:57 -07003185 status, le32_to_cpu(tx_resp->rate_n_flags),
3186 tx_resp->failure_frame);
3187
3188 IWL_DEBUG_TX_REPLY("Tx queue reclaim %d\n", index);
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003189 if (index != -1) {
3190 int freed = iwl4965_tx_queue_reclaim(priv, txq_id, index);
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08003191#ifdef CONFIG_IWL4965_HT
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003192 if (tid != MAX_TID_COUNT)
3193 priv->stations[sta_id].tid[tid].tfds_in_queue -= freed;
3194 if (iwl4965_queue_space(&txq->q) > txq->q.low_mark &&
3195 (txq_id >= 0) &&
3196 priv->mac80211_registered)
3197 ieee80211_wake_queue(priv->hw, txq_id);
3198 if (tid != MAX_TID_COUNT)
3199 iwl4965_check_empty_hw_queue(priv, sta_id, tid, txq_id);
3200#endif
Zhu Yib481de92007-09-25 17:54:57 -07003201 }
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003202#ifdef CONFIG_IWL4965_HT
3203 }
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08003204#endif /* CONFIG_IWL4965_HT */
Zhu Yib481de92007-09-25 17:54:57 -07003205
3206 if (iwl_check_bits(status, TX_ABORT_REQUIRED_MSK))
3207 IWL_ERROR("TODO: Implement Tx ABORT REQUIRED!!!\n");
3208}
3209
3210
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003211static void iwl4965_rx_reply_alive(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003212 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003213{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003214 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
3215 struct iwl4965_alive_resp *palive;
Zhu Yib481de92007-09-25 17:54:57 -07003216 struct delayed_work *pwork;
3217
3218 palive = &pkt->u.alive_frame;
3219
3220 IWL_DEBUG_INFO("Alive ucode status 0x%08X revision "
3221 "0x%01X 0x%01X\n",
3222 palive->is_valid, palive->ver_type,
3223 palive->ver_subtype);
3224
3225 if (palive->ver_subtype == INITIALIZE_SUBTYPE) {
3226 IWL_DEBUG_INFO("Initialization Alive received.\n");
3227 memcpy(&priv->card_alive_init,
3228 &pkt->u.alive_frame,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003229 sizeof(struct iwl4965_init_alive_resp));
Zhu Yib481de92007-09-25 17:54:57 -07003230 pwork = &priv->init_alive_start;
3231 } else {
3232 IWL_DEBUG_INFO("Runtime Alive received.\n");
3233 memcpy(&priv->card_alive, &pkt->u.alive_frame,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003234 sizeof(struct iwl4965_alive_resp));
Zhu Yib481de92007-09-25 17:54:57 -07003235 pwork = &priv->alive_start;
3236 }
3237
3238 /* We delay the ALIVE response by 5ms to
3239 * give the HW RF Kill time to activate... */
3240 if (palive->is_valid == UCODE_VALID_OK)
3241 queue_delayed_work(priv->workqueue, pwork,
3242 msecs_to_jiffies(5));
3243 else
3244 IWL_WARNING("uCode did not respond OK.\n");
3245}
3246
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003247static void iwl4965_rx_reply_add_sta(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003248 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003249{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003250 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07003251
3252 IWL_DEBUG_RX("Received REPLY_ADD_STA: 0x%02X\n", pkt->u.status);
3253 return;
3254}
3255
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003256static void iwl4965_rx_reply_error(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003257 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003258{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003259 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07003260
3261 IWL_ERROR("Error Reply type 0x%08X cmd %s (0x%02X) "
3262 "seq 0x%04X ser 0x%08X\n",
3263 le32_to_cpu(pkt->u.err_resp.error_type),
3264 get_cmd_string(pkt->u.err_resp.cmd_id),
3265 pkt->u.err_resp.cmd_id,
3266 le16_to_cpu(pkt->u.err_resp.bad_cmd_seq_num),
3267 le32_to_cpu(pkt->u.err_resp.error_info));
3268}
3269
3270#define TX_STATUS_ENTRY(x) case TX_STATUS_FAIL_ ## x: return #x
3271
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003272static void iwl4965_rx_csa(struct iwl_priv *priv, struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003273{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003274 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
3275 struct iwl4965_rxon_cmd *rxon = (void *)&priv->active_rxon;
3276 struct iwl4965_csa_notification *csa = &(pkt->u.csa_notif);
Zhu Yib481de92007-09-25 17:54:57 -07003277 IWL_DEBUG_11H("CSA notif: channel %d, status %d\n",
3278 le16_to_cpu(csa->channel), le32_to_cpu(csa->status));
3279 rxon->channel = csa->channel;
3280 priv->staging_rxon.channel = csa->channel;
3281}
3282
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003283static void iwl4965_rx_spectrum_measure_notif(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003284 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003285{
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08003286#ifdef CONFIG_IWL4965_SPECTRUM_MEASUREMENT
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003287 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
3288 struct iwl4965_spectrum_notification *report = &(pkt->u.spectrum_notif);
Zhu Yib481de92007-09-25 17:54:57 -07003289
3290 if (!report->state) {
3291 IWL_DEBUG(IWL_DL_11H | IWL_DL_INFO,
3292 "Spectrum Measure Notification: Start\n");
3293 return;
3294 }
3295
3296 memcpy(&priv->measure_report, report, sizeof(*report));
3297 priv->measurement_status |= MEASUREMENT_READY;
3298#endif
3299}
3300
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003301static void iwl4965_rx_pm_sleep_notif(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003302 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003303{
Tomas Winkler0a6857e2008-03-12 16:58:49 -07003304#ifdef CONFIG_IWLWIFI_DEBUG
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003305 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
3306 struct iwl4965_sleep_notification *sleep = &(pkt->u.sleep_notif);
Zhu Yib481de92007-09-25 17:54:57 -07003307 IWL_DEBUG_RX("sleep mode: %d, src: %d\n",
3308 sleep->pm_sleep_mode, sleep->pm_wakeup_src);
3309#endif
3310}
3311
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003312static void iwl4965_rx_pm_debug_statistics_notif(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003313 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003314{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003315 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07003316 IWL_DEBUG_RADIO("Dumping %d bytes of unhandled "
3317 "notification for %s:\n",
3318 le32_to_cpu(pkt->len), get_cmd_string(pkt->hdr.cmd));
Tomas Winkler0a6857e2008-03-12 16:58:49 -07003319 iwl_print_hex_dump(IWL_DL_RADIO, pkt->u.raw, le32_to_cpu(pkt->len));
Zhu Yib481de92007-09-25 17:54:57 -07003320}
3321
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003322static void iwl4965_bg_beacon_update(struct work_struct *work)
Zhu Yib481de92007-09-25 17:54:57 -07003323{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003324 struct iwl_priv *priv =
3325 container_of(work, struct iwl_priv, beacon_update);
Zhu Yib481de92007-09-25 17:54:57 -07003326 struct sk_buff *beacon;
3327
3328 /* Pull updated AP beacon from mac80211. will fail if not in AP mode */
Johannes Berg32bfd352007-12-19 01:31:26 +01003329 beacon = ieee80211_beacon_get(priv->hw, priv->vif, NULL);
Zhu Yib481de92007-09-25 17:54:57 -07003330
3331 if (!beacon) {
3332 IWL_ERROR("update beacon failed\n");
3333 return;
3334 }
3335
3336 mutex_lock(&priv->mutex);
3337 /* new beacon skb is allocated every time; dispose previous.*/
3338 if (priv->ibss_beacon)
3339 dev_kfree_skb(priv->ibss_beacon);
3340
3341 priv->ibss_beacon = beacon;
3342 mutex_unlock(&priv->mutex);
3343
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003344 iwl4965_send_beacon_cmd(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003345}
3346
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003347static void iwl4965_rx_beacon_notif(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003348 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003349{
Tomas Winkler0a6857e2008-03-12 16:58:49 -07003350#ifdef CONFIG_IWLWIFI_DEBUG
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003351 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
3352 struct iwl4965_beacon_notif *beacon = &(pkt->u.beacon_status);
3353 u8 rate = iwl4965_hw_get_rate(beacon->beacon_notify_hdr.rate_n_flags);
Zhu Yib481de92007-09-25 17:54:57 -07003354
3355 IWL_DEBUG_RX("beacon status %x retries %d iss %d "
3356 "tsf %d %d rate %d\n",
3357 le32_to_cpu(beacon->beacon_notify_hdr.status) & TX_STATUS_MSK,
3358 beacon->beacon_notify_hdr.failure_frame,
3359 le32_to_cpu(beacon->ibss_mgr_status),
3360 le32_to_cpu(beacon->high_tsf),
3361 le32_to_cpu(beacon->low_tsf), rate);
3362#endif
3363
3364 if ((priv->iw_mode == IEEE80211_IF_TYPE_AP) &&
3365 (!test_bit(STATUS_EXIT_PENDING, &priv->status)))
3366 queue_work(priv->workqueue, &priv->beacon_update);
3367}
3368
3369/* Service response to REPLY_SCAN_CMD (0x80) */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003370static void iwl4965_rx_reply_scan(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003371 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003372{
Tomas Winkler0a6857e2008-03-12 16:58:49 -07003373#ifdef CONFIG_IWLWIFI_DEBUG
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003374 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
3375 struct iwl4965_scanreq_notification *notif =
3376 (struct iwl4965_scanreq_notification *)pkt->u.raw;
Zhu Yib481de92007-09-25 17:54:57 -07003377
3378 IWL_DEBUG_RX("Scan request status = 0x%x\n", notif->status);
3379#endif
3380}
3381
3382/* Service SCAN_START_NOTIFICATION (0x82) */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003383static void iwl4965_rx_scan_start_notif(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003384 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003385{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003386 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
3387 struct iwl4965_scanstart_notification *notif =
3388 (struct iwl4965_scanstart_notification *)pkt->u.raw;
Zhu Yib481de92007-09-25 17:54:57 -07003389 priv->scan_start_tsf = le32_to_cpu(notif->tsf_low);
3390 IWL_DEBUG_SCAN("Scan start: "
3391 "%d [802.11%s] "
3392 "(TSF: 0x%08X:%08X) - %d (beacon timer %u)\n",
3393 notif->channel,
3394 notif->band ? "bg" : "a",
3395 notif->tsf_high,
3396 notif->tsf_low, notif->status, notif->beacon_timer);
3397}
3398
3399/* Service SCAN_RESULTS_NOTIFICATION (0x83) */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003400static void iwl4965_rx_scan_results_notif(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003401 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003402{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003403 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
3404 struct iwl4965_scanresults_notification *notif =
3405 (struct iwl4965_scanresults_notification *)pkt->u.raw;
Zhu Yib481de92007-09-25 17:54:57 -07003406
3407 IWL_DEBUG_SCAN("Scan ch.res: "
3408 "%d [802.11%s] "
3409 "(TSF: 0x%08X:%08X) - %d "
3410 "elapsed=%lu usec (%dms since last)\n",
3411 notif->channel,
3412 notif->band ? "bg" : "a",
3413 le32_to_cpu(notif->tsf_high),
3414 le32_to_cpu(notif->tsf_low),
3415 le32_to_cpu(notif->statistics[0]),
3416 le32_to_cpu(notif->tsf_low) - priv->scan_start_tsf,
3417 jiffies_to_msecs(elapsed_jiffies
3418 (priv->last_scan_jiffies, jiffies)));
3419
3420 priv->last_scan_jiffies = jiffies;
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08003421 priv->next_scan_jiffies = 0;
Zhu Yib481de92007-09-25 17:54:57 -07003422}
3423
3424/* Service SCAN_COMPLETE_NOTIFICATION (0x84) */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003425static void iwl4965_rx_scan_complete_notif(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003426 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003427{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003428 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
3429 struct iwl4965_scancomplete_notification *scan_notif = (void *)pkt->u.raw;
Zhu Yib481de92007-09-25 17:54:57 -07003430
3431 IWL_DEBUG_SCAN("Scan complete: %d channels (TSF 0x%08X:%08X) - %d\n",
3432 scan_notif->scanned_channels,
3433 scan_notif->tsf_low,
3434 scan_notif->tsf_high, scan_notif->status);
3435
3436 /* The HW is no longer scanning */
3437 clear_bit(STATUS_SCAN_HW, &priv->status);
3438
3439 /* The scan completion notification came in, so kill that timer... */
3440 cancel_delayed_work(&priv->scan_check);
3441
3442 IWL_DEBUG_INFO("Scan pass on %sGHz took %dms\n",
3443 (priv->scan_bands == 2) ? "2.4" : "5.2",
3444 jiffies_to_msecs(elapsed_jiffies
3445 (priv->scan_pass_start, jiffies)));
3446
3447 /* Remove this scanned band from the list
3448 * of pending bands to scan */
3449 priv->scan_bands--;
3450
3451 /* If a request to abort was given, or the scan did not succeed
3452 * then we reset the scan state machine and terminate,
3453 * re-queuing another scan if one has been requested */
3454 if (test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
3455 IWL_DEBUG_INFO("Aborted scan completed.\n");
3456 clear_bit(STATUS_SCAN_ABORTING, &priv->status);
3457 } else {
3458 /* If there are more bands on this scan pass reschedule */
3459 if (priv->scan_bands > 0)
3460 goto reschedule;
3461 }
3462
3463 priv->last_scan_jiffies = jiffies;
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08003464 priv->next_scan_jiffies = 0;
Zhu Yib481de92007-09-25 17:54:57 -07003465 IWL_DEBUG_INFO("Setting scan to off\n");
3466
3467 clear_bit(STATUS_SCANNING, &priv->status);
3468
3469 IWL_DEBUG_INFO("Scan took %dms\n",
3470 jiffies_to_msecs(elapsed_jiffies(priv->scan_start, jiffies)));
3471
3472 queue_work(priv->workqueue, &priv->scan_completed);
3473
3474 return;
3475
3476reschedule:
3477 priv->scan_pass_start = jiffies;
3478 queue_work(priv->workqueue, &priv->request_scan);
3479}
3480
3481/* Handle notification from uCode that card's power state is changing
3482 * due to software, hardware, or critical temperature RFKILL */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003483static void iwl4965_rx_card_state_notif(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003484 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003485{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003486 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07003487 u32 flags = le32_to_cpu(pkt->u.card_state_notif.flags);
3488 unsigned long status = priv->status;
3489
3490 IWL_DEBUG_RF_KILL("Card state received: HW:%s SW:%s\n",
3491 (flags & HW_CARD_DISABLED) ? "Kill" : "On",
3492 (flags & SW_CARD_DISABLED) ? "Kill" : "On");
3493
3494 if (flags & (SW_CARD_DISABLED | HW_CARD_DISABLED |
3495 RF_CARD_DISABLED)) {
3496
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003497 iwl_write32(priv, CSR_UCODE_DRV_GP1_SET,
Zhu Yib481de92007-09-25 17:54:57 -07003498 CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
3499
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003500 if (!iwl_grab_nic_access(priv)) {
3501 iwl_write_direct32(
Zhu Yib481de92007-09-25 17:54:57 -07003502 priv, HBUS_TARG_MBX_C,
3503 HBUS_TARG_MBX_C_REG_BIT_CMD_BLOCKED);
3504
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003505 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003506 }
3507
3508 if (!(flags & RXON_CARD_DISABLED)) {
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003509 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR,
Zhu Yib481de92007-09-25 17:54:57 -07003510 CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003511 if (!iwl_grab_nic_access(priv)) {
3512 iwl_write_direct32(
Zhu Yib481de92007-09-25 17:54:57 -07003513 priv, HBUS_TARG_MBX_C,
3514 HBUS_TARG_MBX_C_REG_BIT_CMD_BLOCKED);
3515
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003516 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003517 }
3518 }
3519
3520 if (flags & RF_CARD_DISABLED) {
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003521 iwl_write32(priv, CSR_UCODE_DRV_GP1_SET,
Zhu Yib481de92007-09-25 17:54:57 -07003522 CSR_UCODE_DRV_GP1_REG_BIT_CT_KILL_EXIT);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003523 iwl_read32(priv, CSR_UCODE_DRV_GP1);
3524 if (!iwl_grab_nic_access(priv))
3525 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003526 }
3527 }
3528
3529 if (flags & HW_CARD_DISABLED)
3530 set_bit(STATUS_RF_KILL_HW, &priv->status);
3531 else
3532 clear_bit(STATUS_RF_KILL_HW, &priv->status);
3533
3534
3535 if (flags & SW_CARD_DISABLED)
3536 set_bit(STATUS_RF_KILL_SW, &priv->status);
3537 else
3538 clear_bit(STATUS_RF_KILL_SW, &priv->status);
3539
3540 if (!(flags & RXON_CARD_DISABLED))
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003541 iwl4965_scan_cancel(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003542
3543 if ((test_bit(STATUS_RF_KILL_HW, &status) !=
3544 test_bit(STATUS_RF_KILL_HW, &priv->status)) ||
3545 (test_bit(STATUS_RF_KILL_SW, &status) !=
3546 test_bit(STATUS_RF_KILL_SW, &priv->status)))
3547 queue_work(priv->workqueue, &priv->rf_kill);
3548 else
3549 wake_up_interruptible(&priv->wait_command_queue);
3550}
3551
3552/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003553 * iwl4965_setup_rx_handlers - Initialize Rx handler callbacks
Zhu Yib481de92007-09-25 17:54:57 -07003554 *
3555 * Setup the RX handlers for each of the reply types sent from the uCode
3556 * to the host.
3557 *
3558 * This function chains into the hardware specific files for them to setup
3559 * any hardware specific handlers as well.
3560 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003561static void iwl4965_setup_rx_handlers(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003562{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003563 priv->rx_handlers[REPLY_ALIVE] = iwl4965_rx_reply_alive;
3564 priv->rx_handlers[REPLY_ADD_STA] = iwl4965_rx_reply_add_sta;
3565 priv->rx_handlers[REPLY_ERROR] = iwl4965_rx_reply_error;
3566 priv->rx_handlers[CHANNEL_SWITCH_NOTIFICATION] = iwl4965_rx_csa;
Zhu Yib481de92007-09-25 17:54:57 -07003567 priv->rx_handlers[SPECTRUM_MEASURE_NOTIFICATION] =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003568 iwl4965_rx_spectrum_measure_notif;
3569 priv->rx_handlers[PM_SLEEP_NOTIFICATION] = iwl4965_rx_pm_sleep_notif;
Zhu Yib481de92007-09-25 17:54:57 -07003570 priv->rx_handlers[PM_DEBUG_STATISTIC_NOTIFIC] =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003571 iwl4965_rx_pm_debug_statistics_notif;
3572 priv->rx_handlers[BEACON_NOTIFICATION] = iwl4965_rx_beacon_notif;
Zhu Yib481de92007-09-25 17:54:57 -07003573
Ben Cahill9fbab512007-11-29 11:09:47 +08003574 /*
3575 * The same handler is used for both the REPLY to a discrete
3576 * statistics request from the host as well as for the periodic
3577 * statistics notifications (after received beacons) from the uCode.
Zhu Yib481de92007-09-25 17:54:57 -07003578 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003579 priv->rx_handlers[REPLY_STATISTICS_CMD] = iwl4965_hw_rx_statistics;
3580 priv->rx_handlers[STATISTICS_NOTIFICATION] = iwl4965_hw_rx_statistics;
Zhu Yib481de92007-09-25 17:54:57 -07003581
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003582 priv->rx_handlers[REPLY_SCAN_CMD] = iwl4965_rx_reply_scan;
3583 priv->rx_handlers[SCAN_START_NOTIFICATION] = iwl4965_rx_scan_start_notif;
Zhu Yib481de92007-09-25 17:54:57 -07003584 priv->rx_handlers[SCAN_RESULTS_NOTIFICATION] =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003585 iwl4965_rx_scan_results_notif;
Zhu Yib481de92007-09-25 17:54:57 -07003586 priv->rx_handlers[SCAN_COMPLETE_NOTIFICATION] =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003587 iwl4965_rx_scan_complete_notif;
3588 priv->rx_handlers[CARD_STATE_NOTIFICATION] = iwl4965_rx_card_state_notif;
3589 priv->rx_handlers[REPLY_TX] = iwl4965_rx_reply_tx;
Zhu Yib481de92007-09-25 17:54:57 -07003590
Ben Cahill9fbab512007-11-29 11:09:47 +08003591 /* Set up hardware specific Rx handlers */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003592 iwl4965_hw_rx_handler_setup(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003593}
3594
3595/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003596 * iwl4965_tx_cmd_complete - Pull unused buffers off the queue and reclaim them
Zhu Yib481de92007-09-25 17:54:57 -07003597 * @rxb: Rx buffer to reclaim
3598 *
3599 * If an Rx buffer has an async callback associated with it the callback
3600 * will be executed. The attached skb (if present) will only be freed
3601 * if the callback returns 1
3602 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003603static void iwl4965_tx_cmd_complete(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003604 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003605{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003606 struct iwl4965_rx_packet *pkt = (struct iwl4965_rx_packet *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07003607 u16 sequence = le16_to_cpu(pkt->hdr.sequence);
3608 int txq_id = SEQ_TO_QUEUE(sequence);
3609 int index = SEQ_TO_INDEX(sequence);
3610 int huge = sequence & SEQ_HUGE_FRAME;
3611 int cmd_index;
Tomas Winkler857485c2008-03-21 13:53:44 -07003612 struct iwl_cmd *cmd;
Zhu Yib481de92007-09-25 17:54:57 -07003613
3614 /* If a Tx command is being handled and it isn't in the actual
3615 * command queue then there a command routing bug has been introduced
3616 * in the queue management code. */
3617 if (txq_id != IWL_CMD_QUEUE_NUM)
3618 IWL_ERROR("Error wrong command queue %d command id 0x%X\n",
3619 txq_id, pkt->hdr.cmd);
3620 BUG_ON(txq_id != IWL_CMD_QUEUE_NUM);
3621
3622 cmd_index = get_cmd_index(&priv->txq[IWL_CMD_QUEUE_NUM].q, index, huge);
3623 cmd = &priv->txq[IWL_CMD_QUEUE_NUM].cmd[cmd_index];
3624
3625 /* Input error checking is done when commands are added to queue. */
3626 if (cmd->meta.flags & CMD_WANT_SKB) {
3627 cmd->meta.source->u.skb = rxb->skb;
3628 rxb->skb = NULL;
3629 } else if (cmd->meta.u.callback &&
3630 !cmd->meta.u.callback(priv, cmd, rxb->skb))
3631 rxb->skb = NULL;
3632
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003633 iwl4965_tx_queue_reclaim(priv, txq_id, index);
Zhu Yib481de92007-09-25 17:54:57 -07003634
3635 if (!(cmd->meta.flags & CMD_ASYNC)) {
3636 clear_bit(STATUS_HCMD_ACTIVE, &priv->status);
3637 wake_up_interruptible(&priv->wait_command_queue);
3638 }
3639}
3640
3641/************************** RX-FUNCTIONS ****************************/
3642/*
3643 * Rx theory of operation
3644 *
Ben Cahill9fbab512007-11-29 11:09:47 +08003645 * Driver allocates a circular buffer of Receive Buffer Descriptors (RBDs),
3646 * each of which point to Receive Buffers to be filled by 4965. These get
3647 * used not only for Rx frames, but for any command response or notification
3648 * from the 4965. The driver and 4965 manage the Rx buffers by means
3649 * of indexes into the circular buffer.
Zhu Yib481de92007-09-25 17:54:57 -07003650 *
3651 * Rx Queue Indexes
3652 * The host/firmware share two index registers for managing the Rx buffers.
3653 *
3654 * The READ index maps to the first position that the firmware may be writing
3655 * to -- the driver can read up to (but not including) this position and get
3656 * good data.
3657 * The READ index is managed by the firmware once the card is enabled.
3658 *
3659 * The WRITE index maps to the last position the driver has read from -- the
3660 * position preceding WRITE is the last slot the firmware can place a packet.
3661 *
3662 * The queue is empty (no good data) if WRITE = READ - 1, and is full if
3663 * WRITE = READ.
3664 *
Ben Cahill9fbab512007-11-29 11:09:47 +08003665 * During initialization, the host sets up the READ queue position to the first
Zhu Yib481de92007-09-25 17:54:57 -07003666 * INDEX position, and WRITE to the last (READ - 1 wrapped)
3667 *
Ben Cahill9fbab512007-11-29 11:09:47 +08003668 * When the firmware places a packet in a buffer, it will advance the READ index
Zhu Yib481de92007-09-25 17:54:57 -07003669 * and fire the RX interrupt. The driver can then query the READ index and
3670 * process as many packets as possible, moving the WRITE index forward as it
3671 * resets the Rx queue buffers with new memory.
3672 *
3673 * The management in the driver is as follows:
3674 * + A list of pre-allocated SKBs is stored in iwl->rxq->rx_free. When
3675 * iwl->rxq->free_count drops to or below RX_LOW_WATERMARK, work is scheduled
Ian Schram01ebd062007-10-25 17:15:22 +08003676 * to replenish the iwl->rxq->rx_free.
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003677 * + In iwl4965_rx_replenish (scheduled) if 'processed' != 'read' then the
Zhu Yib481de92007-09-25 17:54:57 -07003678 * iwl->rxq is replenished and the READ INDEX is updated (updating the
3679 * 'processed' and 'read' driver indexes as well)
3680 * + A received packet is processed and handed to the kernel network stack,
3681 * detached from the iwl->rxq. The driver 'processed' index is updated.
3682 * + The Host/Firmware iwl->rxq is replenished at tasklet time from the rx_free
3683 * list. If there are no allocated buffers in iwl->rxq->rx_free, the READ
3684 * INDEX is not incremented and iwl->status(RX_STALLED) is set. If there
3685 * were enough free buffers and RX_STALLED is set it is cleared.
3686 *
3687 *
3688 * Driver sequence:
3689 *
Ben Cahill9fbab512007-11-29 11:09:47 +08003690 * iwl4965_rx_queue_alloc() Allocates rx_free
3691 * iwl4965_rx_replenish() Replenishes rx_free list from rx_used, and calls
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003692 * iwl4965_rx_queue_restock
Ben Cahill9fbab512007-11-29 11:09:47 +08003693 * iwl4965_rx_queue_restock() Moves available buffers from rx_free into Rx
Zhu Yib481de92007-09-25 17:54:57 -07003694 * queue, updates firmware pointers, and updates
3695 * the WRITE index. If insufficient rx_free buffers
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003696 * are available, schedules iwl4965_rx_replenish
Zhu Yib481de92007-09-25 17:54:57 -07003697 *
3698 * -- enable interrupts --
Ben Cahill9fbab512007-11-29 11:09:47 +08003699 * ISR - iwl4965_rx() Detach iwl4965_rx_mem_buffers from pool up to the
Zhu Yib481de92007-09-25 17:54:57 -07003700 * READ INDEX, detaching the SKB from the pool.
3701 * Moves the packet buffer from queue to rx_used.
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003702 * Calls iwl4965_rx_queue_restock to refill any empty
Zhu Yib481de92007-09-25 17:54:57 -07003703 * slots.
3704 * ...
3705 *
3706 */
3707
3708/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003709 * iwl4965_rx_queue_space - Return number of free slots available in queue.
Zhu Yib481de92007-09-25 17:54:57 -07003710 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003711static int iwl4965_rx_queue_space(const struct iwl4965_rx_queue *q)
Zhu Yib481de92007-09-25 17:54:57 -07003712{
3713 int s = q->read - q->write;
3714 if (s <= 0)
3715 s += RX_QUEUE_SIZE;
3716 /* keep some buffer to not confuse full and empty queue */
3717 s -= 2;
3718 if (s < 0)
3719 s = 0;
3720 return s;
3721}
3722
3723/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003724 * iwl4965_rx_queue_update_write_ptr - Update the write pointer for the RX queue
Zhu Yib481de92007-09-25 17:54:57 -07003725 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003726int iwl4965_rx_queue_update_write_ptr(struct iwl_priv *priv, struct iwl4965_rx_queue *q)
Zhu Yib481de92007-09-25 17:54:57 -07003727{
3728 u32 reg = 0;
3729 int rc = 0;
3730 unsigned long flags;
3731
3732 spin_lock_irqsave(&q->lock, flags);
3733
3734 if (q->need_update == 0)
3735 goto exit_unlock;
3736
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003737 /* If power-saving is in use, make sure device is awake */
Zhu Yib481de92007-09-25 17:54:57 -07003738 if (test_bit(STATUS_POWER_PMI, &priv->status)) {
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003739 reg = iwl_read32(priv, CSR_UCODE_DRV_GP1);
Zhu Yib481de92007-09-25 17:54:57 -07003740
3741 if (reg & CSR_UCODE_DRV_GP1_BIT_MAC_SLEEP) {
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003742 iwl_set_bit(priv, CSR_GP_CNTRL,
Zhu Yib481de92007-09-25 17:54:57 -07003743 CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
3744 goto exit_unlock;
3745 }
3746
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003747 rc = iwl_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003748 if (rc)
3749 goto exit_unlock;
3750
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003751 /* Device expects a multiple of 8 */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003752 iwl_write_direct32(priv, FH_RSCSR_CHNL0_WPTR,
Zhu Yib481de92007-09-25 17:54:57 -07003753 q->write & ~0x7);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003754 iwl_release_nic_access(priv);
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003755
3756 /* Else device is assumed to be awake */
Zhu Yib481de92007-09-25 17:54:57 -07003757 } else
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003758 /* Device expects a multiple of 8 */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003759 iwl_write32(priv, FH_RSCSR_CHNL0_WPTR, q->write & ~0x7);
Zhu Yib481de92007-09-25 17:54:57 -07003760
3761
3762 q->need_update = 0;
3763
3764 exit_unlock:
3765 spin_unlock_irqrestore(&q->lock, flags);
3766 return rc;
3767}
3768
3769/**
Ben Cahill9fbab512007-11-29 11:09:47 +08003770 * iwl4965_dma_addr2rbd_ptr - convert a DMA address to a uCode read buffer ptr
Zhu Yib481de92007-09-25 17:54:57 -07003771 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003772static inline __le32 iwl4965_dma_addr2rbd_ptr(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07003773 dma_addr_t dma_addr)
3774{
3775 return cpu_to_le32((u32)(dma_addr >> 8));
3776}
3777
3778
3779/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003780 * iwl4965_rx_queue_restock - refill RX queue from pre-allocated pool
Zhu Yib481de92007-09-25 17:54:57 -07003781 *
Ben Cahill9fbab512007-11-29 11:09:47 +08003782 * If there are slots in the RX queue that need to be restocked,
Zhu Yib481de92007-09-25 17:54:57 -07003783 * and we have free pre-allocated buffers, fill the ranks as much
Ben Cahill9fbab512007-11-29 11:09:47 +08003784 * as we can, pulling from rx_free.
Zhu Yib481de92007-09-25 17:54:57 -07003785 *
3786 * This moves the 'write' index forward to catch up with 'processed', and
3787 * also updates the memory address in the firmware to reference the new
3788 * target buffer.
3789 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003790static int iwl4965_rx_queue_restock(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003791{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003792 struct iwl4965_rx_queue *rxq = &priv->rxq;
Zhu Yib481de92007-09-25 17:54:57 -07003793 struct list_head *element;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003794 struct iwl4965_rx_mem_buffer *rxb;
Zhu Yib481de92007-09-25 17:54:57 -07003795 unsigned long flags;
3796 int write, rc;
3797
3798 spin_lock_irqsave(&rxq->lock, flags);
3799 write = rxq->write & ~0x7;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003800 while ((iwl4965_rx_queue_space(rxq) > 0) && (rxq->free_count)) {
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003801 /* Get next free Rx buffer, remove from free list */
Zhu Yib481de92007-09-25 17:54:57 -07003802 element = rxq->rx_free.next;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003803 rxb = list_entry(element, struct iwl4965_rx_mem_buffer, list);
Zhu Yib481de92007-09-25 17:54:57 -07003804 list_del(element);
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003805
3806 /* Point to Rx buffer via next RBD in circular buffer */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003807 rxq->bd[rxq->write] = iwl4965_dma_addr2rbd_ptr(priv, rxb->dma_addr);
Zhu Yib481de92007-09-25 17:54:57 -07003808 rxq->queue[rxq->write] = rxb;
3809 rxq->write = (rxq->write + 1) & RX_QUEUE_MASK;
3810 rxq->free_count--;
3811 }
3812 spin_unlock_irqrestore(&rxq->lock, flags);
3813 /* If the pre-allocated buffer pool is dropping low, schedule to
3814 * refill it */
3815 if (rxq->free_count <= RX_LOW_WATERMARK)
3816 queue_work(priv->workqueue, &priv->rx_replenish);
3817
3818
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003819 /* If we've added more space for the firmware to place data, tell it.
3820 * Increment device's write pointer in multiples of 8. */
Zhu Yib481de92007-09-25 17:54:57 -07003821 if ((write != (rxq->write & ~0x7))
3822 || (abs(rxq->write - rxq->read) > 7)) {
3823 spin_lock_irqsave(&rxq->lock, flags);
3824 rxq->need_update = 1;
3825 spin_unlock_irqrestore(&rxq->lock, flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003826 rc = iwl4965_rx_queue_update_write_ptr(priv, rxq);
Zhu Yib481de92007-09-25 17:54:57 -07003827 if (rc)
3828 return rc;
3829 }
3830
3831 return 0;
3832}
3833
3834/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003835 * iwl4965_rx_replenish - Move all used packet from rx_used to rx_free
Zhu Yib481de92007-09-25 17:54:57 -07003836 *
3837 * When moving to rx_free an SKB is allocated for the slot.
3838 *
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003839 * Also restock the Rx queue via iwl4965_rx_queue_restock.
Ian Schram01ebd062007-10-25 17:15:22 +08003840 * This is called as a scheduled work item (except for during initialization)
Zhu Yib481de92007-09-25 17:54:57 -07003841 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003842static void iwl4965_rx_allocate(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003843{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003844 struct iwl4965_rx_queue *rxq = &priv->rxq;
Zhu Yib481de92007-09-25 17:54:57 -07003845 struct list_head *element;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003846 struct iwl4965_rx_mem_buffer *rxb;
Zhu Yib481de92007-09-25 17:54:57 -07003847 unsigned long flags;
3848 spin_lock_irqsave(&rxq->lock, flags);
3849 while (!list_empty(&rxq->rx_used)) {
3850 element = rxq->rx_used.next;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003851 rxb = list_entry(element, struct iwl4965_rx_mem_buffer, list);
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003852
3853 /* Alloc a new receive buffer */
Zhu Yib481de92007-09-25 17:54:57 -07003854 rxb->skb =
Ron Rindjunsky9ee1ba42007-11-26 16:14:42 +02003855 alloc_skb(priv->hw_setting.rx_buf_size,
3856 __GFP_NOWARN | GFP_ATOMIC);
Zhu Yib481de92007-09-25 17:54:57 -07003857 if (!rxb->skb) {
3858 if (net_ratelimit())
3859 printk(KERN_CRIT DRV_NAME
3860 ": Can not allocate SKB buffers\n");
3861 /* We don't reschedule replenish work here -- we will
3862 * call the restock method and if it still needs
3863 * more buffers it will schedule replenish */
3864 break;
3865 }
3866 priv->alloc_rxb_skb++;
3867 list_del(element);
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003868
3869 /* Get physical address of RB/SKB */
Zhu Yib481de92007-09-25 17:54:57 -07003870 rxb->dma_addr =
3871 pci_map_single(priv->pci_dev, rxb->skb->data,
Ron Rindjunsky9ee1ba42007-11-26 16:14:42 +02003872 priv->hw_setting.rx_buf_size, PCI_DMA_FROMDEVICE);
Zhu Yib481de92007-09-25 17:54:57 -07003873 list_add_tail(&rxb->list, &rxq->rx_free);
3874 rxq->free_count++;
3875 }
3876 spin_unlock_irqrestore(&rxq->lock, flags);
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08003877}
3878
3879/*
3880 * this should be called while priv->lock is locked
3881*/
Tomas Winkler4fd1f842007-12-05 20:59:58 +02003882static void __iwl4965_rx_replenish(void *data)
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08003883{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003884 struct iwl_priv *priv = data;
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08003885
3886 iwl4965_rx_allocate(priv);
3887 iwl4965_rx_queue_restock(priv);
3888}
3889
3890
3891void iwl4965_rx_replenish(void *data)
3892{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003893 struct iwl_priv *priv = data;
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08003894 unsigned long flags;
3895
3896 iwl4965_rx_allocate(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003897
3898 spin_lock_irqsave(&priv->lock, flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003899 iwl4965_rx_queue_restock(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003900 spin_unlock_irqrestore(&priv->lock, flags);
3901}
3902
3903/* Assumes that the skb field of the buffers in 'pool' is kept accurate.
Ben Cahill9fbab512007-11-29 11:09:47 +08003904 * If an SKB has been detached, the POOL needs to have its SKB set to NULL
Zhu Yib481de92007-09-25 17:54:57 -07003905 * This free routine walks the list of POOL entries and if SKB is set to
3906 * non NULL it is unmapped and freed
3907 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003908static void iwl4965_rx_queue_free(struct iwl_priv *priv, struct iwl4965_rx_queue *rxq)
Zhu Yib481de92007-09-25 17:54:57 -07003909{
3910 int i;
3911 for (i = 0; i < RX_QUEUE_SIZE + RX_FREE_BUFFERS; i++) {
3912 if (rxq->pool[i].skb != NULL) {
3913 pci_unmap_single(priv->pci_dev,
3914 rxq->pool[i].dma_addr,
Ron Rindjunsky9ee1ba42007-11-26 16:14:42 +02003915 priv->hw_setting.rx_buf_size,
3916 PCI_DMA_FROMDEVICE);
Zhu Yib481de92007-09-25 17:54:57 -07003917 dev_kfree_skb(rxq->pool[i].skb);
3918 }
3919 }
3920
3921 pci_free_consistent(priv->pci_dev, 4 * RX_QUEUE_SIZE, rxq->bd,
3922 rxq->dma_addr);
3923 rxq->bd = NULL;
3924}
3925
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003926int iwl4965_rx_queue_alloc(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003927{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003928 struct iwl4965_rx_queue *rxq = &priv->rxq;
Zhu Yib481de92007-09-25 17:54:57 -07003929 struct pci_dev *dev = priv->pci_dev;
3930 int i;
3931
3932 spin_lock_init(&rxq->lock);
3933 INIT_LIST_HEAD(&rxq->rx_free);
3934 INIT_LIST_HEAD(&rxq->rx_used);
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003935
3936 /* Alloc the circular buffer of Read Buffer Descriptors (RBDs) */
Zhu Yib481de92007-09-25 17:54:57 -07003937 rxq->bd = pci_alloc_consistent(dev, 4 * RX_QUEUE_SIZE, &rxq->dma_addr);
3938 if (!rxq->bd)
3939 return -ENOMEM;
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003940
Zhu Yib481de92007-09-25 17:54:57 -07003941 /* Fill the rx_used queue with _all_ of the Rx buffers */
3942 for (i = 0; i < RX_FREE_BUFFERS + RX_QUEUE_SIZE; i++)
3943 list_add_tail(&rxq->pool[i].list, &rxq->rx_used);
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003944
Zhu Yib481de92007-09-25 17:54:57 -07003945 /* Set us so that we have processed and used all buffers, but have
3946 * not restocked the Rx queue with fresh buffers */
3947 rxq->read = rxq->write = 0;
3948 rxq->free_count = 0;
3949 rxq->need_update = 0;
3950 return 0;
3951}
3952
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003953void iwl4965_rx_queue_reset(struct iwl_priv *priv, struct iwl4965_rx_queue *rxq)
Zhu Yib481de92007-09-25 17:54:57 -07003954{
3955 unsigned long flags;
3956 int i;
3957 spin_lock_irqsave(&rxq->lock, flags);
3958 INIT_LIST_HEAD(&rxq->rx_free);
3959 INIT_LIST_HEAD(&rxq->rx_used);
3960 /* Fill the rx_used queue with _all_ of the Rx buffers */
3961 for (i = 0; i < RX_FREE_BUFFERS + RX_QUEUE_SIZE; i++) {
3962 /* In the reset function, these buffers may have been allocated
3963 * to an SKB, so we need to unmap and free potential storage */
3964 if (rxq->pool[i].skb != NULL) {
3965 pci_unmap_single(priv->pci_dev,
3966 rxq->pool[i].dma_addr,
Ron Rindjunsky9ee1ba42007-11-26 16:14:42 +02003967 priv->hw_setting.rx_buf_size,
3968 PCI_DMA_FROMDEVICE);
Zhu Yib481de92007-09-25 17:54:57 -07003969 priv->alloc_rxb_skb--;
3970 dev_kfree_skb(rxq->pool[i].skb);
3971 rxq->pool[i].skb = NULL;
3972 }
3973 list_add_tail(&rxq->pool[i].list, &rxq->rx_used);
3974 }
3975
3976 /* Set us so that we have processed and used all buffers, but have
3977 * not restocked the Rx queue with fresh buffers */
3978 rxq->read = rxq->write = 0;
3979 rxq->free_count = 0;
3980 spin_unlock_irqrestore(&rxq->lock, flags);
3981}
3982
3983/* Convert linear signal-to-noise ratio into dB */
3984static u8 ratio2dB[100] = {
3985/* 0 1 2 3 4 5 6 7 8 9 */
3986 0, 0, 6, 10, 12, 14, 16, 17, 18, 19, /* 00 - 09 */
3987 20, 21, 22, 22, 23, 23, 24, 25, 26, 26, /* 10 - 19 */
3988 26, 26, 26, 27, 27, 28, 28, 28, 29, 29, /* 20 - 29 */
3989 29, 30, 30, 30, 31, 31, 31, 31, 32, 32, /* 30 - 39 */
3990 32, 32, 32, 33, 33, 33, 33, 33, 34, 34, /* 40 - 49 */
3991 34, 34, 34, 34, 35, 35, 35, 35, 35, 35, /* 50 - 59 */
3992 36, 36, 36, 36, 36, 36, 36, 37, 37, 37, /* 60 - 69 */
3993 37, 37, 37, 37, 37, 38, 38, 38, 38, 38, /* 70 - 79 */
3994 38, 38, 38, 38, 38, 39, 39, 39, 39, 39, /* 80 - 89 */
3995 39, 39, 39, 39, 39, 40, 40, 40, 40, 40 /* 90 - 99 */
3996};
3997
3998/* Calculates a relative dB value from a ratio of linear
3999 * (i.e. not dB) signal levels.
4000 * Conversion assumes that levels are voltages (20*log), not powers (10*log). */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004001int iwl4965_calc_db_from_ratio(int sig_ratio)
Zhu Yib481de92007-09-25 17:54:57 -07004002{
Adrian Bunkc899a572007-10-14 19:51:15 +02004003 /* 1000:1 or higher just report as 60 dB */
4004 if (sig_ratio >= 1000)
Zhu Yib481de92007-09-25 17:54:57 -07004005 return 60;
4006
Adrian Bunkc899a572007-10-14 19:51:15 +02004007 /* 100:1 or higher, divide by 10 and use table,
Zhu Yib481de92007-09-25 17:54:57 -07004008 * add 20 dB to make up for divide by 10 */
Adrian Bunkc899a572007-10-14 19:51:15 +02004009 if (sig_ratio >= 100)
Zhu Yib481de92007-09-25 17:54:57 -07004010 return (20 + (int)ratio2dB[sig_ratio/10]);
4011
4012 /* We shouldn't see this */
4013 if (sig_ratio < 1)
4014 return 0;
4015
4016 /* Use table for ratios 1:1 - 99:1 */
4017 return (int)ratio2dB[sig_ratio];
4018}
4019
4020#define PERFECT_RSSI (-20) /* dBm */
4021#define WORST_RSSI (-95) /* dBm */
4022#define RSSI_RANGE (PERFECT_RSSI - WORST_RSSI)
4023
4024/* Calculate an indication of rx signal quality (a percentage, not dBm!).
4025 * See http://www.ces.clemson.edu/linux/signal_quality.shtml for info
4026 * about formulas used below. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004027int iwl4965_calc_sig_qual(int rssi_dbm, int noise_dbm)
Zhu Yib481de92007-09-25 17:54:57 -07004028{
4029 int sig_qual;
4030 int degradation = PERFECT_RSSI - rssi_dbm;
4031
4032 /* If we get a noise measurement, use signal-to-noise ratio (SNR)
4033 * as indicator; formula is (signal dbm - noise dbm).
4034 * SNR at or above 40 is a great signal (100%).
4035 * Below that, scale to fit SNR of 0 - 40 dB within 0 - 100% indicator.
4036 * Weakest usable signal is usually 10 - 15 dB SNR. */
4037 if (noise_dbm) {
4038 if (rssi_dbm - noise_dbm >= 40)
4039 return 100;
4040 else if (rssi_dbm < noise_dbm)
4041 return 0;
4042 sig_qual = ((rssi_dbm - noise_dbm) * 5) / 2;
4043
4044 /* Else use just the signal level.
4045 * This formula is a least squares fit of data points collected and
4046 * compared with a reference system that had a percentage (%) display
4047 * for signal quality. */
4048 } else
4049 sig_qual = (100 * (RSSI_RANGE * RSSI_RANGE) - degradation *
4050 (15 * RSSI_RANGE + 62 * degradation)) /
4051 (RSSI_RANGE * RSSI_RANGE);
4052
4053 if (sig_qual > 100)
4054 sig_qual = 100;
4055 else if (sig_qual < 1)
4056 sig_qual = 0;
4057
4058 return sig_qual;
4059}
4060
4061/**
Ben Cahill9fbab512007-11-29 11:09:47 +08004062 * iwl4965_rx_handle - Main entry function for receiving responses from uCode
Zhu Yib481de92007-09-25 17:54:57 -07004063 *
4064 * Uses the priv->rx_handlers callback function array to invoke
4065 * the appropriate handlers, including command responses,
4066 * frame-received notifications, and other notifications.
4067 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004068static void iwl4965_rx_handle(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004069{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004070 struct iwl4965_rx_mem_buffer *rxb;
4071 struct iwl4965_rx_packet *pkt;
4072 struct iwl4965_rx_queue *rxq = &priv->rxq;
Zhu Yib481de92007-09-25 17:54:57 -07004073 u32 r, i;
4074 int reclaim;
4075 unsigned long flags;
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08004076 u8 fill_rx = 0;
Mohamed Abbasd68ab682008-02-07 13:16:33 -08004077 u32 count = 8;
Zhu Yib481de92007-09-25 17:54:57 -07004078
Cahill, Ben M6440adb2007-11-29 11:09:55 +08004079 /* uCode's read index (stored in shared DRAM) indicates the last Rx
4080 * buffer that the driver may process (last buffer filled by ucode). */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004081 r = iwl4965_hw_get_rx_read(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004082 i = rxq->read;
4083
4084 /* Rx interrupt, but nothing sent from uCode */
4085 if (i == r)
4086 IWL_DEBUG(IWL_DL_RX | IWL_DL_ISR, "r = %d, i = %d\n", r, i);
4087
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08004088 if (iwl4965_rx_queue_space(rxq) > (RX_QUEUE_SIZE / 2))
4089 fill_rx = 1;
4090
Zhu Yib481de92007-09-25 17:54:57 -07004091 while (i != r) {
4092 rxb = rxq->queue[i];
4093
Ben Cahill9fbab512007-11-29 11:09:47 +08004094 /* If an RXB doesn't have a Rx queue slot associated with it,
Zhu Yib481de92007-09-25 17:54:57 -07004095 * then a bug has been introduced in the queue refilling
4096 * routines -- catch it here */
4097 BUG_ON(rxb == NULL);
4098
4099 rxq->queue[i] = NULL;
4100
4101 pci_dma_sync_single_for_cpu(priv->pci_dev, rxb->dma_addr,
Ron Rindjunsky9ee1ba42007-11-26 16:14:42 +02004102 priv->hw_setting.rx_buf_size,
Zhu Yib481de92007-09-25 17:54:57 -07004103 PCI_DMA_FROMDEVICE);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004104 pkt = (struct iwl4965_rx_packet *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07004105
4106 /* Reclaim a command buffer only if this packet is a response
4107 * to a (driver-originated) command.
4108 * If the packet (e.g. Rx frame) originated from uCode,
4109 * there is no command buffer to reclaim.
4110 * Ucode should set SEQ_RX_FRAME bit if ucode-originated,
4111 * but apparently a few don't get set; catch them here. */
4112 reclaim = !(pkt->hdr.sequence & SEQ_RX_FRAME) &&
4113 (pkt->hdr.cmd != REPLY_RX_PHY_CMD) &&
Tomas Winkler857485c2008-03-21 13:53:44 -07004114 (pkt->hdr.cmd != REPLY_RX) &&
Zhu Yicfe01702007-09-27 11:27:31 +08004115 (pkt->hdr.cmd != REPLY_COMPRESSED_BA) &&
Zhu Yib481de92007-09-25 17:54:57 -07004116 (pkt->hdr.cmd != STATISTICS_NOTIFICATION) &&
4117 (pkt->hdr.cmd != REPLY_TX);
4118
4119 /* Based on type of command response or notification,
4120 * handle those that need handling via function in
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004121 * rx_handlers table. See iwl4965_setup_rx_handlers() */
Zhu Yib481de92007-09-25 17:54:57 -07004122 if (priv->rx_handlers[pkt->hdr.cmd]) {
4123 IWL_DEBUG(IWL_DL_HOST_COMMAND | IWL_DL_RX | IWL_DL_ISR,
4124 "r = %d, i = %d, %s, 0x%02x\n", r, i,
4125 get_cmd_string(pkt->hdr.cmd), pkt->hdr.cmd);
4126 priv->rx_handlers[pkt->hdr.cmd] (priv, rxb);
4127 } else {
4128 /* No handling needed */
4129 IWL_DEBUG(IWL_DL_HOST_COMMAND | IWL_DL_RX | IWL_DL_ISR,
4130 "r %d i %d No handler needed for %s, 0x%02x\n",
4131 r, i, get_cmd_string(pkt->hdr.cmd),
4132 pkt->hdr.cmd);
4133 }
4134
4135 if (reclaim) {
Ben Cahill9fbab512007-11-29 11:09:47 +08004136 /* Invoke any callbacks, transfer the skb to caller, and
Tomas Winkler857485c2008-03-21 13:53:44 -07004137 * fire off the (possibly) blocking iwl_send_cmd()
Zhu Yib481de92007-09-25 17:54:57 -07004138 * as we reclaim the driver command queue */
4139 if (rxb && rxb->skb)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004140 iwl4965_tx_cmd_complete(priv, rxb);
Zhu Yib481de92007-09-25 17:54:57 -07004141 else
4142 IWL_WARNING("Claim null rxb?\n");
4143 }
4144
4145 /* For now we just don't re-use anything. We can tweak this
4146 * later to try and re-use notification packets and SKBs that
4147 * fail to Rx correctly */
4148 if (rxb->skb != NULL) {
4149 priv->alloc_rxb_skb--;
4150 dev_kfree_skb_any(rxb->skb);
4151 rxb->skb = NULL;
4152 }
4153
4154 pci_unmap_single(priv->pci_dev, rxb->dma_addr,
Ron Rindjunsky9ee1ba42007-11-26 16:14:42 +02004155 priv->hw_setting.rx_buf_size,
4156 PCI_DMA_FROMDEVICE);
Zhu Yib481de92007-09-25 17:54:57 -07004157 spin_lock_irqsave(&rxq->lock, flags);
4158 list_add_tail(&rxb->list, &priv->rxq.rx_used);
4159 spin_unlock_irqrestore(&rxq->lock, flags);
4160 i = (i + 1) & RX_QUEUE_MASK;
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08004161 /* If there are a lot of unused frames,
4162 * restock the Rx queue so ucode wont assert. */
4163 if (fill_rx) {
4164 count++;
4165 if (count >= 8) {
4166 priv->rxq.read = i;
4167 __iwl4965_rx_replenish(priv);
4168 count = 0;
4169 }
4170 }
Zhu Yib481de92007-09-25 17:54:57 -07004171 }
4172
4173 /* Backtrack one entry */
4174 priv->rxq.read = i;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004175 iwl4965_rx_queue_restock(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004176}
4177
Cahill, Ben M6440adb2007-11-29 11:09:55 +08004178/**
4179 * iwl4965_tx_queue_update_write_ptr - Send new write index to hardware
4180 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004181static int iwl4965_tx_queue_update_write_ptr(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004182 struct iwl4965_tx_queue *txq)
Zhu Yib481de92007-09-25 17:54:57 -07004183{
4184 u32 reg = 0;
4185 int rc = 0;
4186 int txq_id = txq->q.id;
4187
4188 if (txq->need_update == 0)
4189 return rc;
4190
4191 /* if we're trying to save power */
4192 if (test_bit(STATUS_POWER_PMI, &priv->status)) {
4193 /* wake up nic if it's powered down ...
4194 * uCode will wake up, and interrupt us again, so next
4195 * time we'll skip this part. */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004196 reg = iwl_read32(priv, CSR_UCODE_DRV_GP1);
Zhu Yib481de92007-09-25 17:54:57 -07004197
4198 if (reg & CSR_UCODE_DRV_GP1_BIT_MAC_SLEEP) {
4199 IWL_DEBUG_INFO("Requesting wakeup, GP1 = 0x%x\n", reg);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004200 iwl_set_bit(priv, CSR_GP_CNTRL,
Zhu Yib481de92007-09-25 17:54:57 -07004201 CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
4202 return rc;
4203 }
4204
4205 /* restore this queue's parameters in nic hardware. */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004206 rc = iwl_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004207 if (rc)
4208 return rc;
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004209 iwl_write_direct32(priv, HBUS_TARG_WRPTR,
Tomas Winklerfc4b6852007-10-25 17:15:24 +08004210 txq->q.write_ptr | (txq_id << 8));
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004211 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004212
4213 /* else not in power-save mode, uCode will never sleep when we're
4214 * trying to tx (during RFKILL, we're not trying to tx). */
4215 } else
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004216 iwl_write32(priv, HBUS_TARG_WRPTR,
Tomas Winklerfc4b6852007-10-25 17:15:24 +08004217 txq->q.write_ptr | (txq_id << 8));
Zhu Yib481de92007-09-25 17:54:57 -07004218
4219 txq->need_update = 0;
4220
4221 return rc;
4222}
4223
Tomas Winkler0a6857e2008-03-12 16:58:49 -07004224#ifdef CONFIG_IWLWIFI_DEBUG
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004225static void iwl4965_print_rx_config_cmd(struct iwl4965_rxon_cmd *rxon)
Zhu Yib481de92007-09-25 17:54:57 -07004226{
Joe Perches0795af52007-10-03 17:59:30 -07004227 DECLARE_MAC_BUF(mac);
4228
Zhu Yib481de92007-09-25 17:54:57 -07004229 IWL_DEBUG_RADIO("RX CONFIG:\n");
Tomas Winkler0a6857e2008-03-12 16:58:49 -07004230 iwl_print_hex_dump(IWL_DL_RADIO, (u8 *) rxon, sizeof(*rxon));
Zhu Yib481de92007-09-25 17:54:57 -07004231 IWL_DEBUG_RADIO("u16 channel: 0x%x\n", le16_to_cpu(rxon->channel));
4232 IWL_DEBUG_RADIO("u32 flags: 0x%08X\n", le32_to_cpu(rxon->flags));
4233 IWL_DEBUG_RADIO("u32 filter_flags: 0x%08x\n",
4234 le32_to_cpu(rxon->filter_flags));
4235 IWL_DEBUG_RADIO("u8 dev_type: 0x%x\n", rxon->dev_type);
4236 IWL_DEBUG_RADIO("u8 ofdm_basic_rates: 0x%02x\n",
4237 rxon->ofdm_basic_rates);
4238 IWL_DEBUG_RADIO("u8 cck_basic_rates: 0x%02x\n", rxon->cck_basic_rates);
Joe Perches0795af52007-10-03 17:59:30 -07004239 IWL_DEBUG_RADIO("u8[6] node_addr: %s\n",
4240 print_mac(mac, rxon->node_addr));
4241 IWL_DEBUG_RADIO("u8[6] bssid_addr: %s\n",
4242 print_mac(mac, rxon->bssid_addr));
Zhu Yib481de92007-09-25 17:54:57 -07004243 IWL_DEBUG_RADIO("u16 assoc_id: 0x%x\n", le16_to_cpu(rxon->assoc_id));
4244}
4245#endif
4246
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004247static void iwl4965_enable_interrupts(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004248{
4249 IWL_DEBUG_ISR("Enabling interrupts\n");
4250 set_bit(STATUS_INT_ENABLED, &priv->status);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004251 iwl_write32(priv, CSR_INT_MASK, CSR_INI_SET_MASK);
Zhu Yib481de92007-09-25 17:54:57 -07004252}
4253
Mohamed Abbas0359fac2008-03-28 16:21:08 -07004254/* call this function to flush any scheduled tasklet */
4255static inline void iwl_synchronize_irq(struct iwl_priv *priv)
4256{
4257 /* wait to make sure we flush pedding tasklet*/
4258 synchronize_irq(priv->pci_dev->irq);
4259 tasklet_kill(&priv->irq_tasklet);
4260}
4261
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004262static inline void iwl4965_disable_interrupts(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004263{
4264 clear_bit(STATUS_INT_ENABLED, &priv->status);
4265
4266 /* disable interrupts from uCode/NIC to host */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004267 iwl_write32(priv, CSR_INT_MASK, 0x00000000);
Zhu Yib481de92007-09-25 17:54:57 -07004268
4269 /* acknowledge/clear/reset any interrupts still pending
4270 * from uCode or flow handler (Rx/Tx DMA) */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004271 iwl_write32(priv, CSR_INT, 0xffffffff);
4272 iwl_write32(priv, CSR_FH_INT_STATUS, 0xffffffff);
Zhu Yib481de92007-09-25 17:54:57 -07004273 IWL_DEBUG_ISR("Disabled interrupts\n");
4274}
4275
4276static const char *desc_lookup(int i)
4277{
4278 switch (i) {
4279 case 1:
4280 return "FAIL";
4281 case 2:
4282 return "BAD_PARAM";
4283 case 3:
4284 return "BAD_CHECKSUM";
4285 case 4:
4286 return "NMI_INTERRUPT";
4287 case 5:
4288 return "SYSASSERT";
4289 case 6:
4290 return "FATAL_ERROR";
4291 }
4292
4293 return "UNKNOWN";
4294}
4295
4296#define ERROR_START_OFFSET (1 * sizeof(u32))
4297#define ERROR_ELEM_SIZE (7 * sizeof(u32))
4298
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004299static void iwl4965_dump_nic_error_log(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004300{
4301 u32 data2, line;
4302 u32 desc, time, count, base, data1;
4303 u32 blink1, blink2, ilink1, ilink2;
4304 int rc;
4305
4306 base = le32_to_cpu(priv->card_alive.error_event_table_ptr);
4307
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004308 if (!iwl4965_hw_valid_rtc_data_addr(base)) {
Zhu Yib481de92007-09-25 17:54:57 -07004309 IWL_ERROR("Not valid error log pointer 0x%08X\n", base);
4310 return;
4311 }
4312
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004313 rc = iwl_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004314 if (rc) {
4315 IWL_WARNING("Can not read from adapter at this time.\n");
4316 return;
4317 }
4318
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004319 count = iwl_read_targ_mem(priv, base);
Zhu Yib481de92007-09-25 17:54:57 -07004320
4321 if (ERROR_START_OFFSET <= count * ERROR_ELEM_SIZE) {
4322 IWL_ERROR("Start IWL Error Log Dump:\n");
Tomas Winkler2acae162008-03-02 01:25:59 +02004323 IWL_ERROR("Status: 0x%08lX, count: %d\n", priv->status, count);
Zhu Yib481de92007-09-25 17:54:57 -07004324 }
4325
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004326 desc = iwl_read_targ_mem(priv, base + 1 * sizeof(u32));
4327 blink1 = iwl_read_targ_mem(priv, base + 3 * sizeof(u32));
4328 blink2 = iwl_read_targ_mem(priv, base + 4 * sizeof(u32));
4329 ilink1 = iwl_read_targ_mem(priv, base + 5 * sizeof(u32));
4330 ilink2 = iwl_read_targ_mem(priv, base + 6 * sizeof(u32));
4331 data1 = iwl_read_targ_mem(priv, base + 7 * sizeof(u32));
4332 data2 = iwl_read_targ_mem(priv, base + 8 * sizeof(u32));
4333 line = iwl_read_targ_mem(priv, base + 9 * sizeof(u32));
4334 time = iwl_read_targ_mem(priv, base + 11 * sizeof(u32));
Zhu Yib481de92007-09-25 17:54:57 -07004335
4336 IWL_ERROR("Desc Time "
4337 "data1 data2 line\n");
4338 IWL_ERROR("%-13s (#%d) %010u 0x%08X 0x%08X %u\n",
4339 desc_lookup(desc), desc, time, data1, data2, line);
4340 IWL_ERROR("blink1 blink2 ilink1 ilink2\n");
4341 IWL_ERROR("0x%05X 0x%05X 0x%05X 0x%05X\n", blink1, blink2,
4342 ilink1, ilink2);
4343
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004344 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004345}
4346
4347#define EVENT_START_OFFSET (4 * sizeof(u32))
4348
4349/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004350 * iwl4965_print_event_log - Dump error event log to syslog
Zhu Yib481de92007-09-25 17:54:57 -07004351 *
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004352 * NOTE: Must be called with iwl_grab_nic_access() already obtained!
Zhu Yib481de92007-09-25 17:54:57 -07004353 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004354static void iwl4965_print_event_log(struct iwl_priv *priv, u32 start_idx,
Zhu Yib481de92007-09-25 17:54:57 -07004355 u32 num_events, u32 mode)
4356{
4357 u32 i;
4358 u32 base; /* SRAM byte address of event log header */
4359 u32 event_size; /* 2 u32s, or 3 u32s if timestamp recorded */
4360 u32 ptr; /* SRAM byte address of log data */
4361 u32 ev, time, data; /* event log data */
4362
4363 if (num_events == 0)
4364 return;
4365
4366 base = le32_to_cpu(priv->card_alive.log_event_table_ptr);
4367
4368 if (mode == 0)
4369 event_size = 2 * sizeof(u32);
4370 else
4371 event_size = 3 * sizeof(u32);
4372
4373 ptr = base + EVENT_START_OFFSET + (start_idx * event_size);
4374
4375 /* "time" is actually "data" for mode 0 (no timestamp).
4376 * place event id # at far right for easier visual parsing. */
4377 for (i = 0; i < num_events; i++) {
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004378 ev = iwl_read_targ_mem(priv, ptr);
Zhu Yib481de92007-09-25 17:54:57 -07004379 ptr += sizeof(u32);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004380 time = iwl_read_targ_mem(priv, ptr);
Zhu Yib481de92007-09-25 17:54:57 -07004381 ptr += sizeof(u32);
4382 if (mode == 0)
4383 IWL_ERROR("0x%08x\t%04u\n", time, ev); /* data, ev */
4384 else {
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004385 data = iwl_read_targ_mem(priv, ptr);
Zhu Yib481de92007-09-25 17:54:57 -07004386 ptr += sizeof(u32);
4387 IWL_ERROR("%010u\t0x%08x\t%04u\n", time, data, ev);
4388 }
4389 }
4390}
4391
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004392static void iwl4965_dump_nic_event_log(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004393{
4394 int rc;
4395 u32 base; /* SRAM byte address of event log header */
4396 u32 capacity; /* event log capacity in # entries */
4397 u32 mode; /* 0 - no timestamp, 1 - timestamp recorded */
4398 u32 num_wraps; /* # times uCode wrapped to top of log */
4399 u32 next_entry; /* index of next entry to be written by uCode */
4400 u32 size; /* # entries that we'll print */
4401
4402 base = le32_to_cpu(priv->card_alive.log_event_table_ptr);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004403 if (!iwl4965_hw_valid_rtc_data_addr(base)) {
Zhu Yib481de92007-09-25 17:54:57 -07004404 IWL_ERROR("Invalid event log pointer 0x%08X\n", base);
4405 return;
4406 }
4407
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004408 rc = iwl_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004409 if (rc) {
4410 IWL_WARNING("Can not read from adapter at this time.\n");
4411 return;
4412 }
4413
4414 /* event log header */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004415 capacity = iwl_read_targ_mem(priv, base);
4416 mode = iwl_read_targ_mem(priv, base + (1 * sizeof(u32)));
4417 num_wraps = iwl_read_targ_mem(priv, base + (2 * sizeof(u32)));
4418 next_entry = iwl_read_targ_mem(priv, base + (3 * sizeof(u32)));
Zhu Yib481de92007-09-25 17:54:57 -07004419
4420 size = num_wraps ? capacity : next_entry;
4421
4422 /* bail out if nothing in log */
4423 if (size == 0) {
Zhu Yi583fab32007-09-27 11:27:30 +08004424 IWL_ERROR("Start IWL Event Log Dump: nothing in log\n");
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004425 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004426 return;
4427 }
4428
Zhu Yi583fab32007-09-27 11:27:30 +08004429 IWL_ERROR("Start IWL Event Log Dump: display count %d, wraps %d\n",
Zhu Yib481de92007-09-25 17:54:57 -07004430 size, num_wraps);
4431
4432 /* if uCode has wrapped back to top of log, start at the oldest entry,
4433 * i.e the next one that uCode would fill. */
4434 if (num_wraps)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004435 iwl4965_print_event_log(priv, next_entry,
Zhu Yib481de92007-09-25 17:54:57 -07004436 capacity - next_entry, mode);
4437
4438 /* (then/else) start at top of log */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004439 iwl4965_print_event_log(priv, 0, next_entry, mode);
Zhu Yib481de92007-09-25 17:54:57 -07004440
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004441 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004442}
4443
4444/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004445 * iwl4965_irq_handle_error - called for HW or SW error interrupt from card
Zhu Yib481de92007-09-25 17:54:57 -07004446 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004447static void iwl4965_irq_handle_error(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004448{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004449 /* Set the FW error flag -- cleared on iwl4965_down */
Zhu Yib481de92007-09-25 17:54:57 -07004450 set_bit(STATUS_FW_ERROR, &priv->status);
4451
4452 /* Cancel currently queued command. */
4453 clear_bit(STATUS_HCMD_ACTIVE, &priv->status);
4454
Tomas Winkler0a6857e2008-03-12 16:58:49 -07004455#ifdef CONFIG_IWLWIFI_DEBUG
4456 if (iwl_debug_level & IWL_DL_FW_ERRORS) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004457 iwl4965_dump_nic_error_log(priv);
4458 iwl4965_dump_nic_event_log(priv);
4459 iwl4965_print_rx_config_cmd(&priv->staging_rxon);
Zhu Yib481de92007-09-25 17:54:57 -07004460 }
4461#endif
4462
4463 wake_up_interruptible(&priv->wait_command_queue);
4464
4465 /* Keep the restart process from trying to send host
4466 * commands by clearing the INIT status bit */
4467 clear_bit(STATUS_READY, &priv->status);
4468
4469 if (!test_bit(STATUS_EXIT_PENDING, &priv->status)) {
4470 IWL_DEBUG(IWL_DL_INFO | IWL_DL_FW_ERRORS,
4471 "Restarting adapter due to uCode error.\n");
4472
Tomas Winkler3109ece2008-03-28 16:33:35 -07004473 if (iwl_is_associated(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07004474 memcpy(&priv->recovery_rxon, &priv->active_rxon,
4475 sizeof(priv->recovery_rxon));
4476 priv->error_recovering = 1;
4477 }
4478 queue_work(priv->workqueue, &priv->restart);
4479 }
4480}
4481
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004482static void iwl4965_error_recovery(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004483{
4484 unsigned long flags;
4485
4486 memcpy(&priv->staging_rxon, &priv->recovery_rxon,
4487 sizeof(priv->staging_rxon));
4488 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004489 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004490
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004491 iwl4965_rxon_add_station(priv, priv->bssid, 1);
Zhu Yib481de92007-09-25 17:54:57 -07004492
4493 spin_lock_irqsave(&priv->lock, flags);
4494 priv->assoc_id = le16_to_cpu(priv->staging_rxon.assoc_id);
4495 priv->error_recovering = 0;
4496 spin_unlock_irqrestore(&priv->lock, flags);
4497}
4498
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004499static void iwl4965_irq_tasklet(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004500{
4501 u32 inta, handled = 0;
4502 u32 inta_fh;
4503 unsigned long flags;
Tomas Winkler0a6857e2008-03-12 16:58:49 -07004504#ifdef CONFIG_IWLWIFI_DEBUG
Zhu Yib481de92007-09-25 17:54:57 -07004505 u32 inta_mask;
4506#endif
4507
4508 spin_lock_irqsave(&priv->lock, flags);
4509
4510 /* Ack/clear/reset pending uCode interrupts.
4511 * Note: Some bits in CSR_INT are "OR" of bits in CSR_FH_INT_STATUS,
4512 * and will clear only when CSR_FH_INT_STATUS gets cleared. */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004513 inta = iwl_read32(priv, CSR_INT);
4514 iwl_write32(priv, CSR_INT, inta);
Zhu Yib481de92007-09-25 17:54:57 -07004515
4516 /* Ack/clear/reset pending flow-handler (DMA) interrupts.
4517 * Any new interrupts that happen after this, either while we're
4518 * in this tasklet, or later, will show up in next ISR/tasklet. */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004519 inta_fh = iwl_read32(priv, CSR_FH_INT_STATUS);
4520 iwl_write32(priv, CSR_FH_INT_STATUS, inta_fh);
Zhu Yib481de92007-09-25 17:54:57 -07004521
Tomas Winkler0a6857e2008-03-12 16:58:49 -07004522#ifdef CONFIG_IWLWIFI_DEBUG
4523 if (iwl_debug_level & IWL_DL_ISR) {
Ben Cahill9fbab512007-11-29 11:09:47 +08004524 /* just for debug */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004525 inta_mask = iwl_read32(priv, CSR_INT_MASK);
Zhu Yib481de92007-09-25 17:54:57 -07004526 IWL_DEBUG_ISR("inta 0x%08x, enabled 0x%08x, fh 0x%08x\n",
4527 inta, inta_mask, inta_fh);
4528 }
4529#endif
4530
4531 /* Since CSR_INT and CSR_FH_INT_STATUS reads and clears are not
4532 * atomic, make sure that inta covers all the interrupts that
4533 * we've discovered, even if FH interrupt came in just after
4534 * reading CSR_INT. */
Tomas Winkler6f83eaa2008-03-04 18:09:28 -08004535 if (inta_fh & CSR49_FH_INT_RX_MASK)
Zhu Yib481de92007-09-25 17:54:57 -07004536 inta |= CSR_INT_BIT_FH_RX;
Tomas Winkler6f83eaa2008-03-04 18:09:28 -08004537 if (inta_fh & CSR49_FH_INT_TX_MASK)
Zhu Yib481de92007-09-25 17:54:57 -07004538 inta |= CSR_INT_BIT_FH_TX;
4539
4540 /* Now service all interrupt bits discovered above. */
4541 if (inta & CSR_INT_BIT_HW_ERR) {
4542 IWL_ERROR("Microcode HW error detected. Restarting.\n");
4543
4544 /* Tell the device to stop sending interrupts */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004545 iwl4965_disable_interrupts(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004546
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004547 iwl4965_irq_handle_error(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004548
4549 handled |= CSR_INT_BIT_HW_ERR;
4550
4551 spin_unlock_irqrestore(&priv->lock, flags);
4552
4553 return;
4554 }
4555
Tomas Winkler0a6857e2008-03-12 16:58:49 -07004556#ifdef CONFIG_IWLWIFI_DEBUG
4557 if (iwl_debug_level & (IWL_DL_ISR)) {
Zhu Yib481de92007-09-25 17:54:57 -07004558 /* NIC fires this, but we don't use it, redundant with WAKEUP */
Joonwoo Park25c03d82008-01-23 10:15:20 -08004559 if (inta & CSR_INT_BIT_SCD)
4560 IWL_DEBUG_ISR("Scheduler finished to transmit "
4561 "the frame/frames.\n");
Zhu Yib481de92007-09-25 17:54:57 -07004562
4563 /* Alive notification via Rx interrupt will do the real work */
4564 if (inta & CSR_INT_BIT_ALIVE)
4565 IWL_DEBUG_ISR("Alive interrupt\n");
4566 }
4567#endif
4568 /* Safely ignore these bits for debug checks below */
Joonwoo Park25c03d82008-01-23 10:15:20 -08004569 inta &= ~(CSR_INT_BIT_SCD | CSR_INT_BIT_ALIVE);
Zhu Yib481de92007-09-25 17:54:57 -07004570
Ben Cahill9fbab512007-11-29 11:09:47 +08004571 /* HW RF KILL switch toggled */
Zhu Yib481de92007-09-25 17:54:57 -07004572 if (inta & CSR_INT_BIT_RF_KILL) {
4573 int hw_rf_kill = 0;
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004574 if (!(iwl_read32(priv, CSR_GP_CNTRL) &
Zhu Yib481de92007-09-25 17:54:57 -07004575 CSR_GP_CNTRL_REG_FLAG_HW_RF_KILL_SW))
4576 hw_rf_kill = 1;
4577
4578 IWL_DEBUG(IWL_DL_INFO | IWL_DL_RF_KILL | IWL_DL_ISR,
4579 "RF_KILL bit toggled to %s.\n",
4580 hw_rf_kill ? "disable radio":"enable radio");
4581
4582 /* Queue restart only if RF_KILL switch was set to "kill"
4583 * when we loaded driver, and is now set to "enable".
4584 * After we're Alive, RF_KILL gets handled by
Reinette Chatre32304552008-02-15 14:34:37 -08004585 * iwl4965_rx_card_state_notif() */
Zhu Yi53e49092007-12-06 16:08:44 +08004586 if (!hw_rf_kill && !test_bit(STATUS_ALIVE, &priv->status)) {
4587 clear_bit(STATUS_RF_KILL_HW, &priv->status);
Zhu Yib481de92007-09-25 17:54:57 -07004588 queue_work(priv->workqueue, &priv->restart);
Zhu Yi53e49092007-12-06 16:08:44 +08004589 }
Zhu Yib481de92007-09-25 17:54:57 -07004590
4591 handled |= CSR_INT_BIT_RF_KILL;
4592 }
4593
Ben Cahill9fbab512007-11-29 11:09:47 +08004594 /* Chip got too hot and stopped itself */
Zhu Yib481de92007-09-25 17:54:57 -07004595 if (inta & CSR_INT_BIT_CT_KILL) {
4596 IWL_ERROR("Microcode CT kill error detected.\n");
4597 handled |= CSR_INT_BIT_CT_KILL;
4598 }
4599
4600 /* Error detected by uCode */
4601 if (inta & CSR_INT_BIT_SW_ERR) {
4602 IWL_ERROR("Microcode SW error detected. Restarting 0x%X.\n",
4603 inta);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004604 iwl4965_irq_handle_error(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004605 handled |= CSR_INT_BIT_SW_ERR;
4606 }
4607
4608 /* uCode wakes up after power-down sleep */
4609 if (inta & CSR_INT_BIT_WAKEUP) {
4610 IWL_DEBUG_ISR("Wakeup interrupt\n");
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004611 iwl4965_rx_queue_update_write_ptr(priv, &priv->rxq);
4612 iwl4965_tx_queue_update_write_ptr(priv, &priv->txq[0]);
4613 iwl4965_tx_queue_update_write_ptr(priv, &priv->txq[1]);
4614 iwl4965_tx_queue_update_write_ptr(priv, &priv->txq[2]);
4615 iwl4965_tx_queue_update_write_ptr(priv, &priv->txq[3]);
4616 iwl4965_tx_queue_update_write_ptr(priv, &priv->txq[4]);
4617 iwl4965_tx_queue_update_write_ptr(priv, &priv->txq[5]);
Zhu Yib481de92007-09-25 17:54:57 -07004618
4619 handled |= CSR_INT_BIT_WAKEUP;
4620 }
4621
4622 /* All uCode command responses, including Tx command responses,
4623 * Rx "responses" (frame-received notification), and other
4624 * notifications from uCode come through here*/
4625 if (inta & (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX)) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004626 iwl4965_rx_handle(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004627 handled |= (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX);
4628 }
4629
4630 if (inta & CSR_INT_BIT_FH_TX) {
4631 IWL_DEBUG_ISR("Tx interrupt\n");
4632 handled |= CSR_INT_BIT_FH_TX;
4633 }
4634
4635 if (inta & ~handled)
4636 IWL_ERROR("Unhandled INTA bits 0x%08x\n", inta & ~handled);
4637
4638 if (inta & ~CSR_INI_SET_MASK) {
4639 IWL_WARNING("Disabled INTA bits 0x%08x were pending\n",
4640 inta & ~CSR_INI_SET_MASK);
4641 IWL_WARNING(" with FH_INT = 0x%08x\n", inta_fh);
4642 }
4643
4644 /* Re-enable all interrupts */
Mohamed Abbas0359fac2008-03-28 16:21:08 -07004645 /* only Re-enable if diabled by irq */
4646 if (test_bit(STATUS_INT_ENABLED, &priv->status))
4647 iwl4965_enable_interrupts(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004648
Tomas Winkler0a6857e2008-03-12 16:58:49 -07004649#ifdef CONFIG_IWLWIFI_DEBUG
4650 if (iwl_debug_level & (IWL_DL_ISR)) {
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004651 inta = iwl_read32(priv, CSR_INT);
4652 inta_mask = iwl_read32(priv, CSR_INT_MASK);
4653 inta_fh = iwl_read32(priv, CSR_FH_INT_STATUS);
Zhu Yib481de92007-09-25 17:54:57 -07004654 IWL_DEBUG_ISR("End inta 0x%08x, enabled 0x%08x, fh 0x%08x, "
4655 "flags 0x%08lx\n", inta, inta_mask, inta_fh, flags);
4656 }
4657#endif
4658 spin_unlock_irqrestore(&priv->lock, flags);
4659}
4660
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004661static irqreturn_t iwl4965_isr(int irq, void *data)
Zhu Yib481de92007-09-25 17:54:57 -07004662{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004663 struct iwl_priv *priv = data;
Zhu Yib481de92007-09-25 17:54:57 -07004664 u32 inta, inta_mask;
4665 u32 inta_fh;
4666 if (!priv)
4667 return IRQ_NONE;
4668
4669 spin_lock(&priv->lock);
4670
4671 /* Disable (but don't clear!) interrupts here to avoid
4672 * back-to-back ISRs and sporadic interrupts from our NIC.
4673 * If we have something to service, the tasklet will re-enable ints.
4674 * If we *don't* have something, we'll re-enable before leaving here. */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004675 inta_mask = iwl_read32(priv, CSR_INT_MASK); /* just for debug */
4676 iwl_write32(priv, CSR_INT_MASK, 0x00000000);
Zhu Yib481de92007-09-25 17:54:57 -07004677
4678 /* Discover which interrupts are active/pending */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004679 inta = iwl_read32(priv, CSR_INT);
4680 inta_fh = iwl_read32(priv, CSR_FH_INT_STATUS);
Zhu Yib481de92007-09-25 17:54:57 -07004681
4682 /* Ignore interrupt if there's nothing in NIC to service.
4683 * This may be due to IRQ shared with another device,
4684 * or due to sporadic interrupts thrown from our NIC. */
4685 if (!inta && !inta_fh) {
4686 IWL_DEBUG_ISR("Ignore interrupt, inta == 0, inta_fh == 0\n");
4687 goto none;
4688 }
4689
4690 if ((inta == 0xFFFFFFFF) || ((inta & 0xFFFFFFF0) == 0xa5a5a5a0)) {
Oliver Neukum66fbb542007-11-15 09:31:10 +08004691 /* Hardware disappeared. It might have already raised
4692 * an interrupt */
Zhu Yib481de92007-09-25 17:54:57 -07004693 IWL_WARNING("HARDWARE GONE?? INTA == 0x%080x\n", inta);
Oliver Neukum66fbb542007-11-15 09:31:10 +08004694 goto unplugged;
Zhu Yib481de92007-09-25 17:54:57 -07004695 }
4696
4697 IWL_DEBUG_ISR("ISR inta 0x%08x, enabled 0x%08x, fh 0x%08x\n",
4698 inta, inta_mask, inta_fh);
4699
Joonwoo Park25c03d82008-01-23 10:15:20 -08004700 inta &= ~CSR_INT_BIT_SCD;
4701
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004702 /* iwl4965_irq_tasklet() will service interrupts and re-enable them */
Joonwoo Park25c03d82008-01-23 10:15:20 -08004703 if (likely(inta || inta_fh))
4704 tasklet_schedule(&priv->irq_tasklet);
Zhu Yib481de92007-09-25 17:54:57 -07004705
Oliver Neukum66fbb542007-11-15 09:31:10 +08004706 unplugged:
4707 spin_unlock(&priv->lock);
Zhu Yib481de92007-09-25 17:54:57 -07004708 return IRQ_HANDLED;
4709
4710 none:
4711 /* re-enable interrupts here since we don't have anything to service. */
Mohamed Abbas0359fac2008-03-28 16:21:08 -07004712 /* only Re-enable if diabled by irq */
4713 if (test_bit(STATUS_INT_ENABLED, &priv->status))
4714 iwl4965_enable_interrupts(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004715 spin_unlock(&priv->lock);
4716 return IRQ_NONE;
4717}
4718
Zhu Yib481de92007-09-25 17:54:57 -07004719/* For active scan, listen ACTIVE_DWELL_TIME (msec) on each channel after
4720 * sending probe req. This should be set long enough to hear probe responses
4721 * from more than one AP. */
4722#define IWL_ACTIVE_DWELL_TIME_24 (20) /* all times in msec */
4723#define IWL_ACTIVE_DWELL_TIME_52 (10)
4724
4725/* For faster active scanning, scan will move to the next channel if fewer than
4726 * PLCP_QUIET_THRESH packets are heard on this channel within
4727 * ACTIVE_QUIET_TIME after sending probe request. This shortens the dwell
4728 * time if it's a quiet channel (nothing responded to our probe, and there's
4729 * no other traffic).
4730 * Disable "quiet" feature by setting PLCP_QUIET_THRESH to 0. */
4731#define IWL_PLCP_QUIET_THRESH __constant_cpu_to_le16(1) /* packets */
4732#define IWL_ACTIVE_QUIET_TIME __constant_cpu_to_le16(5) /* msec */
4733
4734/* For passive scan, listen PASSIVE_DWELL_TIME (msec) on each channel.
4735 * Must be set longer than active dwell time.
4736 * For the most reliable scan, set > AP beacon interval (typically 100msec). */
4737#define IWL_PASSIVE_DWELL_TIME_24 (20) /* all times in msec */
4738#define IWL_PASSIVE_DWELL_TIME_52 (10)
4739#define IWL_PASSIVE_DWELL_BASE (100)
4740#define IWL_CHANNEL_TUNE_TIME 5
4741
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004742static inline u16 iwl4965_get_active_dwell_time(struct iwl_priv *priv,
Johannes Berg8318d782008-01-24 19:38:38 +01004743 enum ieee80211_band band)
Zhu Yib481de92007-09-25 17:54:57 -07004744{
Johannes Berg8318d782008-01-24 19:38:38 +01004745 if (band == IEEE80211_BAND_5GHZ)
Zhu Yib481de92007-09-25 17:54:57 -07004746 return IWL_ACTIVE_DWELL_TIME_52;
4747 else
4748 return IWL_ACTIVE_DWELL_TIME_24;
4749}
4750
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004751static u16 iwl4965_get_passive_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 u16 active = iwl4965_get_active_dwell_time(priv, band);
4755 u16 passive = (band != IEEE80211_BAND_5GHZ) ?
Zhu Yib481de92007-09-25 17:54:57 -07004756 IWL_PASSIVE_DWELL_BASE + IWL_PASSIVE_DWELL_TIME_24 :
4757 IWL_PASSIVE_DWELL_BASE + IWL_PASSIVE_DWELL_TIME_52;
4758
Tomas Winkler3109ece2008-03-28 16:33:35 -07004759 if (iwl_is_associated(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07004760 /* If we're associated, we clamp the maximum passive
4761 * dwell time to be 98% of the beacon interval (minus
4762 * 2 * channel tune time) */
4763 passive = priv->beacon_int;
4764 if ((passive > IWL_PASSIVE_DWELL_BASE) || !passive)
4765 passive = IWL_PASSIVE_DWELL_BASE;
4766 passive = (passive * 98) / 100 - IWL_CHANNEL_TUNE_TIME * 2;
4767 }
4768
4769 if (passive <= active)
4770 passive = active + 1;
4771
4772 return passive;
4773}
4774
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004775static int iwl4965_get_channels_for_scan(struct iwl_priv *priv,
Johannes Berg8318d782008-01-24 19:38:38 +01004776 enum ieee80211_band band,
Zhu Yib481de92007-09-25 17:54:57 -07004777 u8 is_active, u8 direct_mask,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004778 struct iwl4965_scan_channel *scan_ch)
Zhu Yib481de92007-09-25 17:54:57 -07004779{
4780 const struct ieee80211_channel *channels = NULL;
Johannes Berg8318d782008-01-24 19:38:38 +01004781 const struct ieee80211_supported_band *sband;
Assaf Kraussbf85ea42008-03-14 10:38:49 -07004782 const struct iwl_channel_info *ch_info;
Zhu Yib481de92007-09-25 17:54:57 -07004783 u16 passive_dwell = 0;
4784 u16 active_dwell = 0;
4785 int added, i;
4786
Johannes Berg8318d782008-01-24 19:38:38 +01004787 sband = iwl4965_get_hw_mode(priv, band);
4788 if (!sband)
Zhu Yib481de92007-09-25 17:54:57 -07004789 return 0;
4790
Johannes Berg8318d782008-01-24 19:38:38 +01004791 channels = sband->channels;
Zhu Yib481de92007-09-25 17:54:57 -07004792
Johannes Berg8318d782008-01-24 19:38:38 +01004793 active_dwell = iwl4965_get_active_dwell_time(priv, band);
4794 passive_dwell = iwl4965_get_passive_dwell_time(priv, band);
Zhu Yib481de92007-09-25 17:54:57 -07004795
Johannes Berg8318d782008-01-24 19:38:38 +01004796 for (i = 0, added = 0; i < sband->n_channels; i++) {
4797 if (ieee80211_frequency_to_channel(channels[i].center_freq) ==
Zhu Yib481de92007-09-25 17:54:57 -07004798 le16_to_cpu(priv->active_rxon.channel)) {
Tomas Winkler3109ece2008-03-28 16:33:35 -07004799 if (iwl_is_associated(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07004800 IWL_DEBUG_SCAN
4801 ("Skipping current channel %d\n",
4802 le16_to_cpu(priv->active_rxon.channel));
4803 continue;
4804 }
4805 } else if (priv->only_active_channel)
4806 continue;
4807
Johannes Berg8318d782008-01-24 19:38:38 +01004808 scan_ch->channel = ieee80211_frequency_to_channel(channels[i].center_freq);
Zhu Yib481de92007-09-25 17:54:57 -07004809
Assaf Krauss8622e702008-03-21 13:53:43 -07004810 ch_info = iwl_get_channel_info(priv, band,
Ben Cahill9fbab512007-11-29 11:09:47 +08004811 scan_ch->channel);
Zhu Yib481de92007-09-25 17:54:57 -07004812 if (!is_channel_valid(ch_info)) {
4813 IWL_DEBUG_SCAN("Channel %d is INVALID for this SKU.\n",
4814 scan_ch->channel);
4815 continue;
4816 }
4817
4818 if (!is_active || is_channel_passive(ch_info) ||
Johannes Berg8318d782008-01-24 19:38:38 +01004819 (channels[i].flags & IEEE80211_CHAN_PASSIVE_SCAN))
Zhu Yib481de92007-09-25 17:54:57 -07004820 scan_ch->type = 0; /* passive */
4821 else
4822 scan_ch->type = 1; /* active */
4823
4824 if (scan_ch->type & 1)
4825 scan_ch->type |= (direct_mask << 1);
4826
4827 if (is_channel_narrow(ch_info))
4828 scan_ch->type |= (1 << 7);
4829
4830 scan_ch->active_dwell = cpu_to_le16(active_dwell);
4831 scan_ch->passive_dwell = cpu_to_le16(passive_dwell);
4832
Ben Cahill9fbab512007-11-29 11:09:47 +08004833 /* Set txpower levels to defaults */
Zhu Yib481de92007-09-25 17:54:57 -07004834 scan_ch->tpc.dsp_atten = 110;
4835 /* scan_pwr_info->tpc.dsp_atten; */
4836
4837 /*scan_pwr_info->tpc.tx_gain; */
Johannes Berg8318d782008-01-24 19:38:38 +01004838 if (band == IEEE80211_BAND_5GHZ)
Zhu Yib481de92007-09-25 17:54:57 -07004839 scan_ch->tpc.tx_gain = ((1 << 5) | (3 << 3)) | 3;
4840 else {
4841 scan_ch->tpc.tx_gain = ((1 << 5) | (5 << 3));
4842 /* NOTE: if we were doing 6Mb OFDM for scans we'd use
Ben Cahill9fbab512007-11-29 11:09:47 +08004843 * power level:
Reinette Chatre8a1b0242008-01-14 17:46:25 -08004844 * scan_ch->tpc.tx_gain = ((1 << 5) | (2 << 3)) | 3;
Zhu Yib481de92007-09-25 17:54:57 -07004845 */
4846 }
4847
4848 IWL_DEBUG_SCAN("Scanning %d [%s %d]\n",
4849 scan_ch->channel,
4850 (scan_ch->type & 1) ? "ACTIVE" : "PASSIVE",
4851 (scan_ch->type & 1) ?
4852 active_dwell : passive_dwell);
4853
4854 scan_ch++;
4855 added++;
4856 }
4857
4858 IWL_DEBUG_SCAN("total channels to scan %d \n", added);
4859 return added;
4860}
4861
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004862static void iwl4965_init_hw_rates(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07004863 struct ieee80211_rate *rates)
4864{
4865 int i;
4866
4867 for (i = 0; i < IWL_RATE_COUNT; i++) {
Johannes Berg8318d782008-01-24 19:38:38 +01004868 rates[i].bitrate = iwl4965_rates[i].ieee * 5;
4869 rates[i].hw_value = i; /* Rate scaling will work on indexes */
4870 rates[i].hw_value_short = i;
4871 rates[i].flags = 0;
4872 if ((i > IWL_LAST_OFDM_RATE) || (i < IWL_FIRST_OFDM_RATE)) {
Zhu Yib481de92007-09-25 17:54:57 -07004873 /*
Johannes Berg8318d782008-01-24 19:38:38 +01004874 * If CCK != 1M then set short preamble rate flag.
Zhu Yib481de92007-09-25 17:54:57 -07004875 */
Tomas Winkler35cdeaf2008-03-11 16:17:20 -07004876 rates[i].flags |=
4877 (iwl4965_rates[i].plcp == IWL_RATE_1M_PLCP) ?
4878 0 : IEEE80211_RATE_SHORT_PREAMBLE;
Zhu Yib481de92007-09-25 17:54:57 -07004879 }
Zhu Yib481de92007-09-25 17:54:57 -07004880 }
Zhu Yib481de92007-09-25 17:54:57 -07004881}
4882
4883/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004884 * iwl4965_init_geos - Initialize mac80211's geo/channel info based from eeprom
Zhu Yib481de92007-09-25 17:54:57 -07004885 */
Assaf Kraussbf85ea42008-03-14 10:38:49 -07004886int iwl4965_init_geos(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004887{
Assaf Kraussbf85ea42008-03-14 10:38:49 -07004888 struct iwl_channel_info *ch;
Tomas Winkler8211ef72008-03-02 01:36:04 +02004889 struct ieee80211_supported_band *sband;
Zhu Yib481de92007-09-25 17:54:57 -07004890 struct ieee80211_channel *channels;
4891 struct ieee80211_channel *geo_ch;
4892 struct ieee80211_rate *rates;
4893 int i = 0;
Zhu Yib481de92007-09-25 17:54:57 -07004894
Johannes Berg8318d782008-01-24 19:38:38 +01004895 if (priv->bands[IEEE80211_BAND_2GHZ].n_bitrates ||
4896 priv->bands[IEEE80211_BAND_5GHZ].n_bitrates) {
Zhu Yib481de92007-09-25 17:54:57 -07004897 IWL_DEBUG_INFO("Geography modes already initialized.\n");
4898 set_bit(STATUS_GEO_CONFIGURED, &priv->status);
4899 return 0;
4900 }
4901
Zhu Yib481de92007-09-25 17:54:57 -07004902 channels = kzalloc(sizeof(struct ieee80211_channel) *
4903 priv->channel_count, GFP_KERNEL);
Johannes Berg8318d782008-01-24 19:38:38 +01004904 if (!channels)
Zhu Yib481de92007-09-25 17:54:57 -07004905 return -ENOMEM;
Zhu Yib481de92007-09-25 17:54:57 -07004906
Tomas Winkler8211ef72008-03-02 01:36:04 +02004907 rates = kzalloc((sizeof(struct ieee80211_rate) * (IWL_RATE_COUNT + 1)),
Zhu Yib481de92007-09-25 17:54:57 -07004908 GFP_KERNEL);
4909 if (!rates) {
Zhu Yib481de92007-09-25 17:54:57 -07004910 kfree(channels);
4911 return -ENOMEM;
4912 }
4913
Zhu Yib481de92007-09-25 17:54:57 -07004914 /* 5.2GHz channels start after the 2.4GHz channels */
Tomas Winkler8211ef72008-03-02 01:36:04 +02004915 sband = &priv->bands[IEEE80211_BAND_5GHZ];
Assaf Kraussbf85ea42008-03-14 10:38:49 -07004916 sband->channels = &channels[ARRAY_SIZE(iwl_eeprom_band_1)];
Tomas Winkler8211ef72008-03-02 01:36:04 +02004917 /* just OFDM */
4918 sband->bitrates = &rates[IWL_FIRST_OFDM_RATE];
4919 sband->n_bitrates = IWL_RATE_COUNT - IWL_FIRST_OFDM_RATE;
Johannes Berg8318d782008-01-24 19:38:38 +01004920
Assaf Krauss1ea87392008-03-18 14:57:50 -07004921 iwl4965_init_ht_hw_capab(priv, &sband->ht_info, IEEE80211_BAND_5GHZ);
Tomas Winkler78330fd2008-02-06 02:37:18 +02004922
Tomas Winkler8211ef72008-03-02 01:36:04 +02004923 sband = &priv->bands[IEEE80211_BAND_2GHZ];
4924 sband->channels = channels;
4925 /* OFDM & CCK */
4926 sband->bitrates = rates;
4927 sband->n_bitrates = IWL_RATE_COUNT;
Zhu Yib481de92007-09-25 17:54:57 -07004928
Assaf Krauss1ea87392008-03-18 14:57:50 -07004929 iwl4965_init_ht_hw_capab(priv, &sband->ht_info, IEEE80211_BAND_2GHZ);
Tomas Winkler78330fd2008-02-06 02:37:18 +02004930
Zhu Yib481de92007-09-25 17:54:57 -07004931 priv->ieee_channels = channels;
4932 priv->ieee_rates = rates;
4933
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004934 iwl4965_init_hw_rates(priv, rates);
Zhu Yib481de92007-09-25 17:54:57 -07004935
Tomas Winkler8211ef72008-03-02 01:36:04 +02004936 for (i = 0; i < priv->channel_count; i++) {
Zhu Yib481de92007-09-25 17:54:57 -07004937 ch = &priv->channel_info[i];
4938
Tomas Winkler8211ef72008-03-02 01:36:04 +02004939 /* FIXME: might be removed if scan is OK */
4940 if (!is_channel_valid(ch))
Zhu Yib481de92007-09-25 17:54:57 -07004941 continue;
Zhu Yib481de92007-09-25 17:54:57 -07004942
Tomas Winkler8211ef72008-03-02 01:36:04 +02004943 if (is_channel_a_band(ch))
4944 sband = &priv->bands[IEEE80211_BAND_5GHZ];
4945 else
4946 sband = &priv->bands[IEEE80211_BAND_2GHZ];
Zhu Yib481de92007-09-25 17:54:57 -07004947
Tomas Winkler8211ef72008-03-02 01:36:04 +02004948 geo_ch = &sband->channels[sband->n_channels++];
4949
4950 geo_ch->center_freq = ieee80211_channel_to_frequency(ch->channel);
Johannes Berg8318d782008-01-24 19:38:38 +01004951 geo_ch->max_power = ch->max_power_avg;
4952 geo_ch->max_antenna_gain = 0xff;
Mohamed Abbas7b723042008-01-31 21:46:40 -08004953 geo_ch->hw_value = ch->channel;
Zhu Yib481de92007-09-25 17:54:57 -07004954
4955 if (is_channel_valid(ch)) {
Johannes Berg8318d782008-01-24 19:38:38 +01004956 if (!(ch->flags & EEPROM_CHANNEL_IBSS))
4957 geo_ch->flags |= IEEE80211_CHAN_NO_IBSS;
Zhu Yib481de92007-09-25 17:54:57 -07004958
Johannes Berg8318d782008-01-24 19:38:38 +01004959 if (!(ch->flags & EEPROM_CHANNEL_ACTIVE))
4960 geo_ch->flags |= IEEE80211_CHAN_PASSIVE_SCAN;
Zhu Yib481de92007-09-25 17:54:57 -07004961
4962 if (ch->flags & EEPROM_CHANNEL_RADAR)
Johannes Berg8318d782008-01-24 19:38:38 +01004963 geo_ch->flags |= IEEE80211_CHAN_RADAR;
Zhu Yib481de92007-09-25 17:54:57 -07004964
4965 if (ch->max_power_avg > priv->max_channel_txpower_limit)
4966 priv->max_channel_txpower_limit =
4967 ch->max_power_avg;
Tomas Winkler8211ef72008-03-02 01:36:04 +02004968 } else {
Johannes Berg8318d782008-01-24 19:38:38 +01004969 geo_ch->flags |= IEEE80211_CHAN_DISABLED;
Tomas Winkler8211ef72008-03-02 01:36:04 +02004970 }
4971
4972 /* Save flags for reg domain usage */
4973 geo_ch->orig_flags = geo_ch->flags;
4974
4975 IWL_DEBUG_INFO("Channel %d Freq=%d[%sGHz] %s flag=0%X\n",
4976 ch->channel, geo_ch->center_freq,
4977 is_channel_a_band(ch) ? "5.2" : "2.4",
4978 geo_ch->flags & IEEE80211_CHAN_DISABLED ?
4979 "restricted" : "valid",
4980 geo_ch->flags);
Zhu Yib481de92007-09-25 17:54:57 -07004981 }
4982
Tomas Winkler82b9a122008-03-04 18:09:30 -08004983 if ((priv->bands[IEEE80211_BAND_5GHZ].n_channels == 0) &&
4984 priv->cfg->sku & IWL_SKU_A) {
Zhu Yib481de92007-09-25 17:54:57 -07004985 printk(KERN_INFO DRV_NAME
4986 ": Incorrectly detected BG card as ABG. Please send "
4987 "your PCI ID 0x%04X:0x%04X to maintainer.\n",
4988 priv->pci_dev->device, priv->pci_dev->subsystem_device);
Tomas Winkler82b9a122008-03-04 18:09:30 -08004989 priv->cfg->sku &= ~IWL_SKU_A;
Zhu Yib481de92007-09-25 17:54:57 -07004990 }
4991
4992 printk(KERN_INFO DRV_NAME
4993 ": Tunable channels: %d 802.11bg, %d 802.11a channels\n",
Johannes Berg8318d782008-01-24 19:38:38 +01004994 priv->bands[IEEE80211_BAND_2GHZ].n_channels,
4995 priv->bands[IEEE80211_BAND_5GHZ].n_channels);
Zhu Yib481de92007-09-25 17:54:57 -07004996
John W. Linvillee0e0a672008-03-25 15:58:40 -04004997 if (priv->bands[IEEE80211_BAND_2GHZ].n_channels)
4998 priv->hw->wiphy->bands[IEEE80211_BAND_2GHZ] =
4999 &priv->bands[IEEE80211_BAND_2GHZ];
5000 if (priv->bands[IEEE80211_BAND_5GHZ].n_channels)
5001 priv->hw->wiphy->bands[IEEE80211_BAND_5GHZ] =
5002 &priv->bands[IEEE80211_BAND_5GHZ];
Zhu Yib481de92007-09-25 17:54:57 -07005003
Zhu Yib481de92007-09-25 17:54:57 -07005004 set_bit(STATUS_GEO_CONFIGURED, &priv->status);
5005
5006 return 0;
5007}
5008
Reinette Chatre849e0dc2008-01-23 10:15:18 -08005009/*
5010 * iwl4965_free_geos - undo allocations in iwl4965_init_geos
5011 */
Assaf Kraussbf85ea42008-03-14 10:38:49 -07005012void iwl4965_free_geos(struct iwl_priv *priv)
Reinette Chatre849e0dc2008-01-23 10:15:18 -08005013{
Reinette Chatre849e0dc2008-01-23 10:15:18 -08005014 kfree(priv->ieee_channels);
5015 kfree(priv->ieee_rates);
5016 clear_bit(STATUS_GEO_CONFIGURED, &priv->status);
5017}
5018
Zhu Yib481de92007-09-25 17:54:57 -07005019/******************************************************************************
5020 *
5021 * uCode download functions
5022 *
5023 ******************************************************************************/
5024
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005025static void iwl4965_dealloc_ucode_pci(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005026{
Tomas Winkler98c92212008-01-14 17:46:20 -08005027 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_code);
5028 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_data);
5029 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_data_backup);
5030 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_init);
5031 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_init_data);
5032 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_boot);
Zhu Yib481de92007-09-25 17:54:57 -07005033}
5034
5035/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005036 * iwl4965_verify_inst_full - verify runtime uCode image in card vs. host,
Zhu Yib481de92007-09-25 17:54:57 -07005037 * looking at all data.
5038 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005039static int iwl4965_verify_inst_full(struct iwl_priv *priv, __le32 *image,
Ben Cahill9fbab512007-11-29 11:09:47 +08005040 u32 len)
Zhu Yib481de92007-09-25 17:54:57 -07005041{
5042 u32 val;
5043 u32 save_len = len;
5044 int rc = 0;
5045 u32 errcnt;
5046
5047 IWL_DEBUG_INFO("ucode inst image size is %u\n", len);
5048
Tomas Winkler3395f6e2008-03-25 16:33:37 -07005049 rc = iwl_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005050 if (rc)
5051 return rc;
5052
Tomas Winkler3395f6e2008-03-25 16:33:37 -07005053 iwl_write_direct32(priv, HBUS_TARG_MEM_RADDR, RTC_INST_LOWER_BOUND);
Zhu Yib481de92007-09-25 17:54:57 -07005054
5055 errcnt = 0;
5056 for (; len > 0; len -= sizeof(u32), image++) {
5057 /* read data comes through single port, auto-incr addr */
5058 /* NOTE: Use the debugless read so we don't flood kernel log
5059 * if IWL_DL_IO is set */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07005060 val = _iwl_read_direct32(priv, HBUS_TARG_MEM_RDAT);
Zhu Yib481de92007-09-25 17:54:57 -07005061 if (val != le32_to_cpu(*image)) {
5062 IWL_ERROR("uCode INST section is invalid at "
5063 "offset 0x%x, is 0x%x, s/b 0x%x\n",
5064 save_len - len, val, le32_to_cpu(*image));
5065 rc = -EIO;
5066 errcnt++;
5067 if (errcnt >= 20)
5068 break;
5069 }
5070 }
5071
Tomas Winkler3395f6e2008-03-25 16:33:37 -07005072 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005073
5074 if (!errcnt)
5075 IWL_DEBUG_INFO
5076 ("ucode image in INSTRUCTION memory is good\n");
5077
5078 return rc;
5079}
5080
5081
5082/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005083 * iwl4965_verify_inst_sparse - verify runtime uCode image in card vs. host,
Zhu Yib481de92007-09-25 17:54:57 -07005084 * using sample data 100 bytes apart. If these sample points are good,
5085 * it's a pretty good bet that everything between them is good, too.
5086 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005087static int iwl4965_verify_inst_sparse(struct iwl_priv *priv, __le32 *image, u32 len)
Zhu Yib481de92007-09-25 17:54:57 -07005088{
5089 u32 val;
5090 int rc = 0;
5091 u32 errcnt = 0;
5092 u32 i;
5093
5094 IWL_DEBUG_INFO("ucode inst image size is %u\n", len);
5095
Tomas Winkler3395f6e2008-03-25 16:33:37 -07005096 rc = iwl_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005097 if (rc)
5098 return rc;
5099
5100 for (i = 0; i < len; i += 100, image += 100/sizeof(u32)) {
5101 /* read data comes through single port, auto-incr addr */
5102 /* NOTE: Use the debugless read so we don't flood kernel log
5103 * if IWL_DL_IO is set */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07005104 iwl_write_direct32(priv, HBUS_TARG_MEM_RADDR,
Zhu Yib481de92007-09-25 17:54:57 -07005105 i + RTC_INST_LOWER_BOUND);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07005106 val = _iwl_read_direct32(priv, HBUS_TARG_MEM_RDAT);
Zhu Yib481de92007-09-25 17:54:57 -07005107 if (val != le32_to_cpu(*image)) {
5108#if 0 /* Enable this if you want to see details */
5109 IWL_ERROR("uCode INST section is invalid at "
5110 "offset 0x%x, is 0x%x, s/b 0x%x\n",
5111 i, val, *image);
5112#endif
5113 rc = -EIO;
5114 errcnt++;
5115 if (errcnt >= 3)
5116 break;
5117 }
5118 }
5119
Tomas Winkler3395f6e2008-03-25 16:33:37 -07005120 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005121
5122 return rc;
5123}
5124
5125
5126/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005127 * iwl4965_verify_ucode - determine which instruction image is in SRAM,
Zhu Yib481de92007-09-25 17:54:57 -07005128 * and verify its contents
5129 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005130static int iwl4965_verify_ucode(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005131{
5132 __le32 *image;
5133 u32 len;
5134 int rc = 0;
5135
5136 /* Try bootstrap */
5137 image = (__le32 *)priv->ucode_boot.v_addr;
5138 len = priv->ucode_boot.len;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005139 rc = iwl4965_verify_inst_sparse(priv, image, len);
Zhu Yib481de92007-09-25 17:54:57 -07005140 if (rc == 0) {
5141 IWL_DEBUG_INFO("Bootstrap uCode is good in inst SRAM\n");
5142 return 0;
5143 }
5144
5145 /* Try initialize */
5146 image = (__le32 *)priv->ucode_init.v_addr;
5147 len = priv->ucode_init.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("Initialize uCode is good in inst SRAM\n");
5151 return 0;
5152 }
5153
5154 /* Try runtime/protocol */
5155 image = (__le32 *)priv->ucode_code.v_addr;
5156 len = priv->ucode_code.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("Runtime uCode is good in inst SRAM\n");
5160 return 0;
5161 }
5162
5163 IWL_ERROR("NO VALID UCODE IMAGE IN INSTRUCTION SRAM!!\n");
5164
Ben Cahill9fbab512007-11-29 11:09:47 +08005165 /* Since nothing seems to match, show first several data entries in
5166 * instruction SRAM, so maybe visual inspection will give a clue.
5167 * Selection of bootstrap image (vs. other images) is arbitrary. */
Zhu Yib481de92007-09-25 17:54:57 -07005168 image = (__le32 *)priv->ucode_boot.v_addr;
5169 len = priv->ucode_boot.len;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005170 rc = iwl4965_verify_inst_full(priv, image, len);
Zhu Yib481de92007-09-25 17:54:57 -07005171
5172 return rc;
5173}
5174
5175
5176/* check contents of special bootstrap uCode SRAM */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005177static int iwl4965_verify_bsm(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005178{
5179 __le32 *image = priv->ucode_boot.v_addr;
5180 u32 len = priv->ucode_boot.len;
5181 u32 reg;
5182 u32 val;
5183
5184 IWL_DEBUG_INFO("Begin verify bsm\n");
5185
5186 /* verify BSM SRAM contents */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07005187 val = iwl_read_prph(priv, BSM_WR_DWCOUNT_REG);
Zhu Yib481de92007-09-25 17:54:57 -07005188 for (reg = BSM_SRAM_LOWER_BOUND;
5189 reg < BSM_SRAM_LOWER_BOUND + len;
5190 reg += sizeof(u32), image ++) {
Tomas Winkler3395f6e2008-03-25 16:33:37 -07005191 val = iwl_read_prph(priv, reg);
Zhu Yib481de92007-09-25 17:54:57 -07005192 if (val != le32_to_cpu(*image)) {
5193 IWL_ERROR("BSM uCode verification failed at "
5194 "addr 0x%08X+%u (of %u), is 0x%x, s/b 0x%x\n",
5195 BSM_SRAM_LOWER_BOUND,
5196 reg - BSM_SRAM_LOWER_BOUND, len,
5197 val, le32_to_cpu(*image));
5198 return -EIO;
5199 }
5200 }
5201
5202 IWL_DEBUG_INFO("BSM bootstrap uCode image OK\n");
5203
5204 return 0;
5205}
5206
5207/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005208 * iwl4965_load_bsm - Load bootstrap instructions
Zhu Yib481de92007-09-25 17:54:57 -07005209 *
5210 * BSM operation:
5211 *
5212 * The Bootstrap State Machine (BSM) stores a short bootstrap uCode program
5213 * in special SRAM that does not power down during RFKILL. When powering back
5214 * up after power-saving sleeps (or during initial uCode load), the BSM loads
5215 * the bootstrap program into the on-board processor, and starts it.
5216 *
5217 * The bootstrap program loads (via DMA) instructions and data for a new
5218 * program from host DRAM locations indicated by the host driver in the
5219 * BSM_DRAM_* registers. Once the new program is loaded, it starts
5220 * automatically.
5221 *
5222 * When initializing the NIC, the host driver points the BSM to the
5223 * "initialize" uCode image. This uCode sets up some internal data, then
5224 * notifies host via "initialize alive" that it is complete.
5225 *
5226 * The host then replaces the BSM_DRAM_* pointer values to point to the
5227 * normal runtime uCode instructions and a backup uCode data cache buffer
5228 * (filled initially with starting data values for the on-board processor),
5229 * then triggers the "initialize" uCode to load and launch the runtime uCode,
5230 * which begins normal operation.
5231 *
5232 * When doing a power-save shutdown, runtime uCode saves data SRAM into
5233 * the backup data cache in DRAM before SRAM is powered down.
5234 *
5235 * When powering back up, the BSM loads the bootstrap program. This reloads
5236 * the runtime uCode instructions and the backup data cache into SRAM,
5237 * and re-launches the runtime uCode from where it left off.
5238 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005239static int iwl4965_load_bsm(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005240{
5241 __le32 *image = priv->ucode_boot.v_addr;
5242 u32 len = priv->ucode_boot.len;
5243 dma_addr_t pinst;
5244 dma_addr_t pdata;
5245 u32 inst_len;
5246 u32 data_len;
5247 int rc;
5248 int i;
5249 u32 done;
5250 u32 reg_offset;
5251
5252 IWL_DEBUG_INFO("Begin load bsm\n");
5253
5254 /* make sure bootstrap program is no larger than BSM's SRAM size */
5255 if (len > IWL_MAX_BSM_SIZE)
5256 return -EINVAL;
5257
5258 /* Tell bootstrap uCode where to find the "Initialize" uCode
Ben Cahill9fbab512007-11-29 11:09:47 +08005259 * in host DRAM ... host DRAM physical address bits 35:4 for 4965.
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005260 * NOTE: iwl4965_initialize_alive_start() will replace these values,
Zhu Yib481de92007-09-25 17:54:57 -07005261 * after the "initialize" uCode has run, to point to
5262 * runtime/protocol instructions and backup data cache. */
5263 pinst = priv->ucode_init.p_addr >> 4;
5264 pdata = priv->ucode_init_data.p_addr >> 4;
5265 inst_len = priv->ucode_init.len;
5266 data_len = priv->ucode_init_data.len;
5267
Tomas Winkler3395f6e2008-03-25 16:33:37 -07005268 rc = iwl_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005269 if (rc)
5270 return rc;
5271
Tomas Winkler3395f6e2008-03-25 16:33:37 -07005272 iwl_write_prph(priv, BSM_DRAM_INST_PTR_REG, pinst);
5273 iwl_write_prph(priv, BSM_DRAM_DATA_PTR_REG, pdata);
5274 iwl_write_prph(priv, BSM_DRAM_INST_BYTECOUNT_REG, inst_len);
5275 iwl_write_prph(priv, BSM_DRAM_DATA_BYTECOUNT_REG, data_len);
Zhu Yib481de92007-09-25 17:54:57 -07005276
5277 /* Fill BSM memory with bootstrap instructions */
5278 for (reg_offset = BSM_SRAM_LOWER_BOUND;
5279 reg_offset < BSM_SRAM_LOWER_BOUND + len;
5280 reg_offset += sizeof(u32), image++)
Tomas Winkler3395f6e2008-03-25 16:33:37 -07005281 _iwl_write_prph(priv, reg_offset,
Zhu Yib481de92007-09-25 17:54:57 -07005282 le32_to_cpu(*image));
5283
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005284 rc = iwl4965_verify_bsm(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005285 if (rc) {
Tomas Winkler3395f6e2008-03-25 16:33:37 -07005286 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005287 return rc;
5288 }
5289
5290 /* Tell BSM to copy from BSM SRAM into instruction SRAM, when asked */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07005291 iwl_write_prph(priv, BSM_WR_MEM_SRC_REG, 0x0);
5292 iwl_write_prph(priv, BSM_WR_MEM_DST_REG,
Zhu Yib481de92007-09-25 17:54:57 -07005293 RTC_INST_LOWER_BOUND);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07005294 iwl_write_prph(priv, BSM_WR_DWCOUNT_REG, len / sizeof(u32));
Zhu Yib481de92007-09-25 17:54:57 -07005295
5296 /* Load bootstrap code into instruction SRAM now,
5297 * to prepare to load "initialize" uCode */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07005298 iwl_write_prph(priv, BSM_WR_CTRL_REG,
Zhu Yib481de92007-09-25 17:54:57 -07005299 BSM_WR_CTRL_REG_BIT_START);
5300
5301 /* Wait for load of bootstrap uCode to finish */
5302 for (i = 0; i < 100; i++) {
Tomas Winkler3395f6e2008-03-25 16:33:37 -07005303 done = iwl_read_prph(priv, BSM_WR_CTRL_REG);
Zhu Yib481de92007-09-25 17:54:57 -07005304 if (!(done & BSM_WR_CTRL_REG_BIT_START))
5305 break;
5306 udelay(10);
5307 }
5308 if (i < 100)
5309 IWL_DEBUG_INFO("BSM write complete, poll %d iterations\n", i);
5310 else {
5311 IWL_ERROR("BSM write did not complete!\n");
5312 return -EIO;
5313 }
5314
5315 /* Enable future boot loads whenever power management unit triggers it
5316 * (e.g. when powering back up after power-save shutdown) */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07005317 iwl_write_prph(priv, BSM_WR_CTRL_REG,
Zhu Yib481de92007-09-25 17:54:57 -07005318 BSM_WR_CTRL_REG_BIT_START_EN);
5319
Tomas Winkler3395f6e2008-03-25 16:33:37 -07005320 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005321
5322 return 0;
5323}
5324
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005325static void iwl4965_nic_start(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005326{
5327 /* Remove all resets to allow NIC to operate */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07005328 iwl_write32(priv, CSR_RESET, 0);
Zhu Yib481de92007-09-25 17:54:57 -07005329}
5330
Tomas Winkler90e759d2007-11-29 11:09:41 +08005331
Zhu Yib481de92007-09-25 17:54:57 -07005332/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005333 * iwl4965_read_ucode - Read uCode images from disk file.
Zhu Yib481de92007-09-25 17:54:57 -07005334 *
5335 * Copy into buffers for card to fetch via bus-mastering
5336 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005337static int iwl4965_read_ucode(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005338{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005339 struct iwl4965_ucode *ucode;
Tomas Winkler90e759d2007-11-29 11:09:41 +08005340 int ret;
Zhu Yib481de92007-09-25 17:54:57 -07005341 const struct firmware *ucode_raw;
Tomas Winkler4bf775c2008-03-04 18:09:31 -08005342 const char *name = priv->cfg->fw_name;
Zhu Yib481de92007-09-25 17:54:57 -07005343 u8 *src;
5344 size_t len;
5345 u32 ver, inst_size, data_size, init_size, init_data_size, boot_size;
5346
5347 /* Ask kernel firmware_class module to get the boot firmware off disk.
5348 * request_firmware() is synchronous, file is in memory on return. */
Tomas Winkler90e759d2007-11-29 11:09:41 +08005349 ret = request_firmware(&ucode_raw, name, &priv->pci_dev->dev);
5350 if (ret < 0) {
5351 IWL_ERROR("%s firmware file req failed: Reason %d\n",
5352 name, ret);
Zhu Yib481de92007-09-25 17:54:57 -07005353 goto error;
5354 }
5355
5356 IWL_DEBUG_INFO("Got firmware '%s' file (%zd bytes) from disk\n",
5357 name, ucode_raw->size);
5358
5359 /* Make sure that we got at least our header! */
5360 if (ucode_raw->size < sizeof(*ucode)) {
5361 IWL_ERROR("File size way too small!\n");
Tomas Winkler90e759d2007-11-29 11:09:41 +08005362 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07005363 goto err_release;
5364 }
5365
5366 /* Data from ucode file: header followed by uCode images */
5367 ucode = (void *)ucode_raw->data;
5368
5369 ver = le32_to_cpu(ucode->ver);
5370 inst_size = le32_to_cpu(ucode->inst_size);
5371 data_size = le32_to_cpu(ucode->data_size);
5372 init_size = le32_to_cpu(ucode->init_size);
5373 init_data_size = le32_to_cpu(ucode->init_data_size);
5374 boot_size = le32_to_cpu(ucode->boot_size);
5375
5376 IWL_DEBUG_INFO("f/w package hdr ucode version = 0x%x\n", ver);
5377 IWL_DEBUG_INFO("f/w package hdr runtime inst size = %u\n",
5378 inst_size);
5379 IWL_DEBUG_INFO("f/w package hdr runtime data size = %u\n",
5380 data_size);
5381 IWL_DEBUG_INFO("f/w package hdr init inst size = %u\n",
5382 init_size);
5383 IWL_DEBUG_INFO("f/w package hdr init data size = %u\n",
5384 init_data_size);
5385 IWL_DEBUG_INFO("f/w package hdr boot inst size = %u\n",
5386 boot_size);
5387
5388 /* Verify size of file vs. image size info in file's header */
5389 if (ucode_raw->size < sizeof(*ucode) +
5390 inst_size + data_size + init_size +
5391 init_data_size + boot_size) {
5392
5393 IWL_DEBUG_INFO("uCode file size %d too small\n",
5394 (int)ucode_raw->size);
Tomas Winkler90e759d2007-11-29 11:09:41 +08005395 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07005396 goto err_release;
5397 }
5398
5399 /* Verify that uCode images will fit in card's SRAM */
5400 if (inst_size > IWL_MAX_INST_SIZE) {
Tomas Winkler90e759d2007-11-29 11:09:41 +08005401 IWL_DEBUG_INFO("uCode instr len %d too large to fit in\n",
5402 inst_size);
5403 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07005404 goto err_release;
5405 }
5406
5407 if (data_size > IWL_MAX_DATA_SIZE) {
Tomas Winkler90e759d2007-11-29 11:09:41 +08005408 IWL_DEBUG_INFO("uCode data len %d too large to fit in\n",
5409 data_size);
5410 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07005411 goto err_release;
5412 }
5413 if (init_size > IWL_MAX_INST_SIZE) {
5414 IWL_DEBUG_INFO
Tomas Winkler90e759d2007-11-29 11:09:41 +08005415 ("uCode init instr len %d too large to fit in\n",
5416 init_size);
5417 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07005418 goto err_release;
5419 }
5420 if (init_data_size > IWL_MAX_DATA_SIZE) {
5421 IWL_DEBUG_INFO
Tomas Winkler90e759d2007-11-29 11:09:41 +08005422 ("uCode init data len %d too large to fit in\n",
5423 init_data_size);
5424 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07005425 goto err_release;
5426 }
5427 if (boot_size > IWL_MAX_BSM_SIZE) {
5428 IWL_DEBUG_INFO
Tomas Winkler90e759d2007-11-29 11:09:41 +08005429 ("uCode boot instr len %d too large to fit in\n",
5430 boot_size);
5431 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07005432 goto err_release;
5433 }
5434
5435 /* Allocate ucode buffers for card's bus-master loading ... */
5436
5437 /* Runtime instructions and 2 copies of data:
5438 * 1) unmodified from disk
5439 * 2) backup cache for save/restore during power-downs */
5440 priv->ucode_code.len = inst_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08005441 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_code);
Zhu Yib481de92007-09-25 17:54:57 -07005442
5443 priv->ucode_data.len = data_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08005444 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_data);
Zhu Yib481de92007-09-25 17:54:57 -07005445
5446 priv->ucode_data_backup.len = data_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08005447 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_data_backup);
Zhu Yib481de92007-09-25 17:54:57 -07005448
5449 /* Initialization instructions and data */
Tomas Winkler90e759d2007-11-29 11:09:41 +08005450 if (init_size && init_data_size) {
5451 priv->ucode_init.len = init_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08005452 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_init);
Zhu Yib481de92007-09-25 17:54:57 -07005453
Tomas Winkler90e759d2007-11-29 11:09:41 +08005454 priv->ucode_init_data.len = init_data_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08005455 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_init_data);
Tomas Winkler90e759d2007-11-29 11:09:41 +08005456
5457 if (!priv->ucode_init.v_addr || !priv->ucode_init_data.v_addr)
5458 goto err_pci_alloc;
5459 }
Zhu Yib481de92007-09-25 17:54:57 -07005460
5461 /* Bootstrap (instructions only, no data) */
Tomas Winkler90e759d2007-11-29 11:09:41 +08005462 if (boot_size) {
5463 priv->ucode_boot.len = boot_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08005464 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_boot);
Zhu Yib481de92007-09-25 17:54:57 -07005465
Tomas Winkler90e759d2007-11-29 11:09:41 +08005466 if (!priv->ucode_boot.v_addr)
5467 goto err_pci_alloc;
5468 }
Zhu Yib481de92007-09-25 17:54:57 -07005469
5470 /* Copy images into buffers for card's bus-master reads ... */
5471
5472 /* Runtime instructions (first block of data in file) */
5473 src = &ucode->data[0];
5474 len = priv->ucode_code.len;
Tomas Winkler90e759d2007-11-29 11:09:41 +08005475 IWL_DEBUG_INFO("Copying (but not loading) uCode instr len %Zd\n", len);
Zhu Yib481de92007-09-25 17:54:57 -07005476 memcpy(priv->ucode_code.v_addr, src, len);
5477 IWL_DEBUG_INFO("uCode instr buf vaddr = 0x%p, paddr = 0x%08x\n",
5478 priv->ucode_code.v_addr, (u32)priv->ucode_code.p_addr);
5479
5480 /* Runtime data (2nd block)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005481 * NOTE: Copy into backup buffer will be done in iwl4965_up() */
Zhu Yib481de92007-09-25 17:54:57 -07005482 src = &ucode->data[inst_size];
5483 len = priv->ucode_data.len;
Tomas Winkler90e759d2007-11-29 11:09:41 +08005484 IWL_DEBUG_INFO("Copying (but not loading) uCode data len %Zd\n", len);
Zhu Yib481de92007-09-25 17:54:57 -07005485 memcpy(priv->ucode_data.v_addr, src, len);
5486 memcpy(priv->ucode_data_backup.v_addr, src, len);
5487
5488 /* Initialization instructions (3rd block) */
5489 if (init_size) {
5490 src = &ucode->data[inst_size + data_size];
5491 len = priv->ucode_init.len;
Tomas Winkler90e759d2007-11-29 11:09:41 +08005492 IWL_DEBUG_INFO("Copying (but not loading) init instr len %Zd\n",
5493 len);
Zhu Yib481de92007-09-25 17:54:57 -07005494 memcpy(priv->ucode_init.v_addr, src, len);
5495 }
5496
5497 /* Initialization data (4th block) */
5498 if (init_data_size) {
5499 src = &ucode->data[inst_size + data_size + init_size];
5500 len = priv->ucode_init_data.len;
Tomas Winkler90e759d2007-11-29 11:09:41 +08005501 IWL_DEBUG_INFO("Copying (but not loading) init data len %Zd\n",
5502 len);
Zhu Yib481de92007-09-25 17:54:57 -07005503 memcpy(priv->ucode_init_data.v_addr, src, len);
5504 }
5505
5506 /* Bootstrap instructions (5th block) */
5507 src = &ucode->data[inst_size + data_size + init_size + init_data_size];
5508 len = priv->ucode_boot.len;
Tomas Winkler90e759d2007-11-29 11:09:41 +08005509 IWL_DEBUG_INFO("Copying (but not loading) boot instr len %Zd\n", len);
Zhu Yib481de92007-09-25 17:54:57 -07005510 memcpy(priv->ucode_boot.v_addr, src, len);
5511
5512 /* We have our copies now, allow OS release its copies */
5513 release_firmware(ucode_raw);
5514 return 0;
5515
5516 err_pci_alloc:
5517 IWL_ERROR("failed to allocate pci memory\n");
Tomas Winkler90e759d2007-11-29 11:09:41 +08005518 ret = -ENOMEM;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005519 iwl4965_dealloc_ucode_pci(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005520
5521 err_release:
5522 release_firmware(ucode_raw);
5523
5524 error:
Tomas Winkler90e759d2007-11-29 11:09:41 +08005525 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07005526}
5527
5528
5529/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005530 * iwl4965_set_ucode_ptrs - Set uCode address location
Zhu Yib481de92007-09-25 17:54:57 -07005531 *
5532 * Tell initialization uCode where to find runtime uCode.
5533 *
5534 * BSM registers initially contain pointers to initialization uCode.
5535 * We need to replace them to load runtime uCode inst and data,
5536 * and to save runtime data when powering down.
5537 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005538static int iwl4965_set_ucode_ptrs(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005539{
5540 dma_addr_t pinst;
5541 dma_addr_t pdata;
5542 int rc = 0;
5543 unsigned long flags;
5544
5545 /* bits 35:4 for 4965 */
5546 pinst = priv->ucode_code.p_addr >> 4;
5547 pdata = priv->ucode_data_backup.p_addr >> 4;
5548
5549 spin_lock_irqsave(&priv->lock, flags);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07005550 rc = iwl_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005551 if (rc) {
5552 spin_unlock_irqrestore(&priv->lock, flags);
5553 return rc;
5554 }
5555
5556 /* Tell bootstrap uCode where to find image to load */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07005557 iwl_write_prph(priv, BSM_DRAM_INST_PTR_REG, pinst);
5558 iwl_write_prph(priv, BSM_DRAM_DATA_PTR_REG, pdata);
5559 iwl_write_prph(priv, BSM_DRAM_DATA_BYTECOUNT_REG,
Zhu Yib481de92007-09-25 17:54:57 -07005560 priv->ucode_data.len);
5561
5562 /* Inst bytecount must be last to set up, bit 31 signals uCode
5563 * that all new ptr/size info is in place */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07005564 iwl_write_prph(priv, BSM_DRAM_INST_BYTECOUNT_REG,
Zhu Yib481de92007-09-25 17:54:57 -07005565 priv->ucode_code.len | BSM_DRAM_INST_LOAD);
5566
Tomas Winkler3395f6e2008-03-25 16:33:37 -07005567 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005568
5569 spin_unlock_irqrestore(&priv->lock, flags);
5570
5571 IWL_DEBUG_INFO("Runtime uCode pointers are set.\n");
5572
5573 return rc;
5574}
5575
5576/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005577 * iwl4965_init_alive_start - Called after REPLY_ALIVE notification received
Zhu Yib481de92007-09-25 17:54:57 -07005578 *
5579 * Called after REPLY_ALIVE notification received from "initialize" uCode.
5580 *
5581 * The 4965 "initialize" ALIVE reply contains calibration data for:
5582 * Voltage, temperature, and MIMO tx gain correction, now stored in priv
5583 * (3945 does not contain this data).
5584 *
5585 * Tell "initialize" uCode to go ahead and load the runtime uCode.
5586*/
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005587static void iwl4965_init_alive_start(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005588{
5589 /* Check alive response for "valid" sign from uCode */
5590 if (priv->card_alive_init.is_valid != UCODE_VALID_OK) {
5591 /* We had an error bringing up the hardware, so take it
5592 * all the way back down so we can try again */
5593 IWL_DEBUG_INFO("Initialize Alive failed.\n");
5594 goto restart;
5595 }
5596
5597 /* Bootstrap uCode has loaded initialize uCode ... verify inst image.
5598 * This is a paranoid check, because we would not have gotten the
5599 * "initialize" alive if code weren't properly loaded. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005600 if (iwl4965_verify_ucode(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07005601 /* Runtime instruction load was bad;
5602 * take it all the way back down so we can try again */
5603 IWL_DEBUG_INFO("Bad \"initialize\" uCode load.\n");
5604 goto restart;
5605 }
5606
5607 /* Calculate temperature */
5608 priv->temperature = iwl4965_get_temperature(priv);
5609
5610 /* Send pointers to protocol/runtime uCode image ... init code will
5611 * load and launch runtime uCode, which will send us another "Alive"
5612 * notification. */
5613 IWL_DEBUG_INFO("Initialization Alive received.\n");
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005614 if (iwl4965_set_ucode_ptrs(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07005615 /* Runtime instruction load won't happen;
5616 * take it all the way back down so we can try again */
5617 IWL_DEBUG_INFO("Couldn't set up uCode pointers.\n");
5618 goto restart;
5619 }
5620 return;
5621
5622 restart:
5623 queue_work(priv->workqueue, &priv->restart);
5624}
5625
5626
5627/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005628 * iwl4965_alive_start - called after REPLY_ALIVE notification received
Zhu Yib481de92007-09-25 17:54:57 -07005629 * from protocol/runtime uCode (initialization uCode's
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005630 * Alive gets handled by iwl4965_init_alive_start()).
Zhu Yib481de92007-09-25 17:54:57 -07005631 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005632static void iwl4965_alive_start(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005633{
5634 int rc = 0;
5635
5636 IWL_DEBUG_INFO("Runtime Alive received.\n");
5637
5638 if (priv->card_alive.is_valid != UCODE_VALID_OK) {
5639 /* We had an error bringing up the hardware, so take it
5640 * all the way back down so we can try again */
5641 IWL_DEBUG_INFO("Alive failed.\n");
5642 goto restart;
5643 }
5644
5645 /* Initialize uCode has loaded Runtime uCode ... verify inst image.
5646 * This is a paranoid check, because we would not have gotten the
5647 * "runtime" alive if code weren't properly loaded. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005648 if (iwl4965_verify_ucode(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07005649 /* Runtime instruction load was bad;
5650 * take it all the way back down so we can try again */
5651 IWL_DEBUG_INFO("Bad runtime uCode load.\n");
5652 goto restart;
5653 }
5654
Assaf Kraussbf85ea42008-03-14 10:38:49 -07005655 iwlcore_clear_stations_table(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005656
5657 rc = iwl4965_alive_notify(priv);
5658 if (rc) {
5659 IWL_WARNING("Could not complete ALIVE transition [ntf]: %d\n",
5660 rc);
5661 goto restart;
5662 }
5663
Ben Cahill9fbab512007-11-29 11:09:47 +08005664 /* After the ALIVE response, we can send host commands to 4965 uCode */
Zhu Yib481de92007-09-25 17:54:57 -07005665 set_bit(STATUS_ALIVE, &priv->status);
5666
5667 /* Clear out the uCode error bit if it is set */
5668 clear_bit(STATUS_FW_ERROR, &priv->status);
5669
Tomas Winklerfee12472008-04-03 16:05:21 -07005670 if (iwl_is_rfkill(priv))
Zhu Yib481de92007-09-25 17:54:57 -07005671 return;
5672
Zhu Yi5a66926a2008-01-14 17:46:18 -08005673 ieee80211_start_queues(priv->hw);
Zhu Yib481de92007-09-25 17:54:57 -07005674
5675 priv->active_rate = priv->rates_mask;
5676 priv->active_rate_basic = priv->rates_mask & IWL_BASIC_RATES_MASK;
5677
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005678 iwl4965_send_power_mode(priv, IWL_POWER_LEVEL(priv->power_mode));
Zhu Yib481de92007-09-25 17:54:57 -07005679
Tomas Winkler3109ece2008-03-28 16:33:35 -07005680 if (iwl_is_associated(priv)) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005681 struct iwl4965_rxon_cmd *active_rxon =
5682 (struct iwl4965_rxon_cmd *)(&priv->active_rxon);
Zhu Yib481de92007-09-25 17:54:57 -07005683
5684 memcpy(&priv->staging_rxon, &priv->active_rxon,
5685 sizeof(priv->staging_rxon));
5686 active_rxon->filter_flags &= ~RXON_FILTER_ASSOC_MSK;
5687 } else {
5688 /* Initialize our rx_config data */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005689 iwl4965_connection_init_rx_config(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005690 memcpy(priv->staging_rxon.node_addr, priv->mac_addr, ETH_ALEN);
5691 }
5692
Ben Cahill9fbab512007-11-29 11:09:47 +08005693 /* Configure Bluetooth device coexistence support */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005694 iwl4965_send_bt_config(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005695
5696 /* Configure the adapter for unassociated operation */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005697 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005698
5699 /* At this point, the NIC is initialized and operational */
5700 priv->notif_missed_beacons = 0;
Zhu Yib481de92007-09-25 17:54:57 -07005701
5702 iwl4965_rf_kill_ct_config(priv);
Zhu Yi5a66926a2008-01-14 17:46:18 -08005703
Zhu Yib481de92007-09-25 17:54:57 -07005704 IWL_DEBUG_INFO("ALIVE processing complete.\n");
Rick Farringtona9f46782008-03-18 14:57:49 -07005705 set_bit(STATUS_READY, &priv->status);
Zhu Yi5a66926a2008-01-14 17:46:18 -08005706 wake_up_interruptible(&priv->wait_command_queue);
Zhu Yib481de92007-09-25 17:54:57 -07005707
Mohamed Abbasab53d8a2008-03-25 16:33:36 -07005708 iwl_leds_register(priv);
5709
Zhu Yib481de92007-09-25 17:54:57 -07005710 if (priv->error_recovering)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005711 iwl4965_error_recovery(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005712
Mohamed Abbasc8381fd2008-03-28 16:21:05 -07005713 iwlcore_low_level_notify(priv, IWLCORE_START_EVT);
Zhu Yib481de92007-09-25 17:54:57 -07005714 return;
5715
5716 restart:
5717 queue_work(priv->workqueue, &priv->restart);
5718}
5719
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005720static void iwl4965_cancel_deferred_work(struct iwl_priv *priv);
Zhu Yib481de92007-09-25 17:54:57 -07005721
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005722static void __iwl4965_down(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005723{
5724 unsigned long flags;
5725 int exit_pending = test_bit(STATUS_EXIT_PENDING, &priv->status);
5726 struct ieee80211_conf *conf = NULL;
5727
5728 IWL_DEBUG_INFO(DRV_NAME " is going down\n");
5729
5730 conf = ieee80211_get_hw_conf(priv->hw);
5731
5732 if (!exit_pending)
5733 set_bit(STATUS_EXIT_PENDING, &priv->status);
5734
Mohamed Abbasab53d8a2008-03-25 16:33:36 -07005735 iwl_leds_unregister(priv);
5736
Mohamed Abbasc8381fd2008-03-28 16:21:05 -07005737 iwlcore_low_level_notify(priv, IWLCORE_STOP_EVT);
5738
Assaf Kraussbf85ea42008-03-14 10:38:49 -07005739 iwlcore_clear_stations_table(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005740
5741 /* Unblock any waiting calls */
5742 wake_up_interruptible_all(&priv->wait_command_queue);
5743
Zhu Yib481de92007-09-25 17:54:57 -07005744 /* Wipe out the EXIT_PENDING status bit if we are not actually
5745 * exiting the module */
5746 if (!exit_pending)
5747 clear_bit(STATUS_EXIT_PENDING, &priv->status);
5748
5749 /* stop and reset the on-board processor */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07005750 iwl_write32(priv, CSR_RESET, CSR_RESET_REG_FLAG_NEVO_RESET);
Zhu Yib481de92007-09-25 17:54:57 -07005751
5752 /* tell the device to stop sending interrupts */
Mohamed Abbas0359fac2008-03-28 16:21:08 -07005753 spin_lock_irqsave(&priv->lock, flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005754 iwl4965_disable_interrupts(priv);
Mohamed Abbas0359fac2008-03-28 16:21:08 -07005755 spin_unlock_irqrestore(&priv->lock, flags);
5756 iwl_synchronize_irq(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005757
5758 if (priv->mac80211_registered)
5759 ieee80211_stop_queues(priv->hw);
5760
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005761 /* If we have not previously called iwl4965_init() then
Zhu Yib481de92007-09-25 17:54:57 -07005762 * clear all bits but the RF Kill and SUSPEND bits and return */
Tomas Winklerfee12472008-04-03 16:05:21 -07005763 if (!iwl_is_init(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07005764 priv->status = test_bit(STATUS_RF_KILL_HW, &priv->status) <<
5765 STATUS_RF_KILL_HW |
5766 test_bit(STATUS_RF_KILL_SW, &priv->status) <<
5767 STATUS_RF_KILL_SW |
Reinette Chatre97888642008-02-06 11:20:38 -08005768 test_bit(STATUS_GEO_CONFIGURED, &priv->status) <<
5769 STATUS_GEO_CONFIGURED |
Zhu Yib481de92007-09-25 17:54:57 -07005770 test_bit(STATUS_IN_SUSPEND, &priv->status) <<
5771 STATUS_IN_SUSPEND;
5772 goto exit;
5773 }
5774
5775 /* ...otherwise clear out all the status bits but the RF Kill and
5776 * SUSPEND bits and continue taking the NIC down. */
5777 priv->status &= test_bit(STATUS_RF_KILL_HW, &priv->status) <<
5778 STATUS_RF_KILL_HW |
5779 test_bit(STATUS_RF_KILL_SW, &priv->status) <<
5780 STATUS_RF_KILL_SW |
Reinette Chatre97888642008-02-06 11:20:38 -08005781 test_bit(STATUS_GEO_CONFIGURED, &priv->status) <<
5782 STATUS_GEO_CONFIGURED |
Zhu Yib481de92007-09-25 17:54:57 -07005783 test_bit(STATUS_IN_SUSPEND, &priv->status) <<
5784 STATUS_IN_SUSPEND |
5785 test_bit(STATUS_FW_ERROR, &priv->status) <<
5786 STATUS_FW_ERROR;
5787
5788 spin_lock_irqsave(&priv->lock, flags);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07005789 iwl_clear_bit(priv, CSR_GP_CNTRL,
Ben Cahill9fbab512007-11-29 11:09:47 +08005790 CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
Zhu Yib481de92007-09-25 17:54:57 -07005791 spin_unlock_irqrestore(&priv->lock, flags);
5792
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005793 iwl4965_hw_txq_ctx_stop(priv);
5794 iwl4965_hw_rxq_stop(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005795
5796 spin_lock_irqsave(&priv->lock, flags);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07005797 if (!iwl_grab_nic_access(priv)) {
5798 iwl_write_prph(priv, APMG_CLK_DIS_REG,
Zhu Yib481de92007-09-25 17:54:57 -07005799 APMG_CLK_VAL_DMA_CLK_RQT);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07005800 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005801 }
5802 spin_unlock_irqrestore(&priv->lock, flags);
5803
5804 udelay(5);
5805
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005806 iwl4965_hw_nic_stop_master(priv);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07005807 iwl_set_bit(priv, CSR_RESET, CSR_RESET_REG_FLAG_SW_RESET);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005808 iwl4965_hw_nic_reset(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005809
5810 exit:
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005811 memset(&priv->card_alive, 0, sizeof(struct iwl4965_alive_resp));
Zhu Yib481de92007-09-25 17:54:57 -07005812
5813 if (priv->ibss_beacon)
5814 dev_kfree_skb(priv->ibss_beacon);
5815 priv->ibss_beacon = NULL;
5816
5817 /* clear out any free frames */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005818 iwl4965_clear_free_frames(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005819}
5820
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005821static void iwl4965_down(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005822{
5823 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005824 __iwl4965_down(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005825 mutex_unlock(&priv->mutex);
Zhu Yib24d22b2007-12-19 13:59:52 +08005826
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005827 iwl4965_cancel_deferred_work(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005828}
5829
5830#define MAX_HW_RESTARTS 5
5831
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005832static int __iwl4965_up(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005833{
5834 int rc, i;
Zhu Yib481de92007-09-25 17:54:57 -07005835
5836 if (test_bit(STATUS_EXIT_PENDING, &priv->status)) {
5837 IWL_WARNING("Exit pending; will not bring the NIC up\n");
5838 return -EIO;
5839 }
5840
5841 if (test_bit(STATUS_RF_KILL_SW, &priv->status)) {
5842 IWL_WARNING("Radio disabled by SW RF kill (module "
5843 "parameter)\n");
Mohamed Abbasad97edd2008-03-28 16:21:06 -07005844 iwl_rfkill_set_hw_state(priv);
Zhu Yie655b9f2008-01-24 02:19:38 -08005845 return -ENODEV;
5846 }
5847
Reinette Chatree903fbd2008-01-30 22:05:15 -08005848 if (!priv->ucode_data_backup.v_addr || !priv->ucode_data.v_addr) {
5849 IWL_ERROR("ucode not available for device bringup\n");
5850 return -EIO;
5851 }
5852
Zhu Yie655b9f2008-01-24 02:19:38 -08005853 /* If platform's RF_KILL switch is NOT set to KILL */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07005854 if (iwl_read32(priv, CSR_GP_CNTRL) &
Zhu Yie655b9f2008-01-24 02:19:38 -08005855 CSR_GP_CNTRL_REG_FLAG_HW_RF_KILL_SW)
5856 clear_bit(STATUS_RF_KILL_HW, &priv->status);
5857 else {
5858 set_bit(STATUS_RF_KILL_HW, &priv->status);
5859 if (!test_bit(STATUS_IN_SUSPEND, &priv->status)) {
Mohamed Abbasad97edd2008-03-28 16:21:06 -07005860 iwl_rfkill_set_hw_state(priv);
Zhu Yie655b9f2008-01-24 02:19:38 -08005861 IWL_WARNING("Radio disabled by HW RF Kill switch\n");
5862 return -ENODEV;
5863 }
Zhu Yib481de92007-09-25 17:54:57 -07005864 }
5865
Mohamed Abbasad97edd2008-03-28 16:21:06 -07005866 iwl_rfkill_set_hw_state(priv);
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
Tomas Winklerfee12472008-04-03 16:05:21 -07005973 if (!iwl_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 {
Mohamed Abbasad97edd2008-03-28 16:21:06 -07005980 /* make sure mac80211 stop sending Tx frame */
5981 if (priv->mac80211_registered)
5982 ieee80211_stop_queues(priv->hw);
Zhu Yib481de92007-09-25 17:54:57 -07005983
5984 if (!test_bit(STATUS_RF_KILL_HW, &priv->status))
5985 IWL_DEBUG_RF_KILL("Can not turn radio back on - "
5986 "disabled by SW switch\n");
5987 else
5988 IWL_WARNING("Radio Frequency Kill Switch is On:\n"
5989 "Kill switch must be turned off for "
5990 "wireless networking to work.\n");
5991 }
Mohamed Abbasad97edd2008-03-28 16:21:06 -07005992 iwl_rfkill_set_hw_state(priv);
5993
Zhu Yib481de92007-09-25 17:54:57 -07005994 mutex_unlock(&priv->mutex);
5995}
5996
5997#define IWL_SCAN_CHECK_WATCHDOG (7 * HZ)
5998
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005999static void iwl4965_bg_scan_check(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07006000{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006001 struct iwl_priv *priv =
6002 container_of(data, struct iwl_priv, scan_check.work);
Zhu Yib481de92007-09-25 17:54:57 -07006003
6004 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
6005 return;
6006
6007 mutex_lock(&priv->mutex);
6008 if (test_bit(STATUS_SCANNING, &priv->status) ||
6009 test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
6010 IWL_DEBUG(IWL_DL_INFO | IWL_DL_SCAN,
6011 "Scan completion watchdog resetting adapter (%dms)\n",
6012 jiffies_to_msecs(IWL_SCAN_CHECK_WATCHDOG));
mabbas052c4b92007-10-25 17:15:43 +08006013
Zhu Yib481de92007-09-25 17:54:57 -07006014 if (!test_bit(STATUS_EXIT_PENDING, &priv->status))
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006015 iwl4965_send_scan_abort(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006016 }
6017 mutex_unlock(&priv->mutex);
6018}
6019
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006020static void iwl4965_bg_request_scan(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07006021{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006022 struct iwl_priv *priv =
6023 container_of(data, struct iwl_priv, request_scan);
Tomas Winkler857485c2008-03-21 13:53:44 -07006024 struct iwl_host_cmd cmd = {
Zhu Yib481de92007-09-25 17:54:57 -07006025 .id = REPLY_SCAN_CMD,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006026 .len = sizeof(struct iwl4965_scan_cmd),
Zhu Yib481de92007-09-25 17:54:57 -07006027 .meta.flags = CMD_SIZE_HUGE,
6028 };
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006029 struct iwl4965_scan_cmd *scan;
Zhu Yib481de92007-09-25 17:54:57 -07006030 struct ieee80211_conf *conf = NULL;
Tomas Winkler78330fd2008-02-06 02:37:18 +02006031 u16 cmd_len;
Johannes Berg8318d782008-01-24 19:38:38 +01006032 enum ieee80211_band band;
Tomas Winkler78330fd2008-02-06 02:37:18 +02006033 u8 direct_mask;
Tomas Winkler857485c2008-03-21 13:53:44 -07006034 int ret = 0;
Zhu Yib481de92007-09-25 17:54:57 -07006035
6036 conf = ieee80211_get_hw_conf(priv->hw);
6037
6038 mutex_lock(&priv->mutex);
6039
Tomas Winklerfee12472008-04-03 16:05:21 -07006040 if (!iwl_is_ready(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07006041 IWL_WARNING("request scan called when driver not ready.\n");
6042 goto done;
6043 }
6044
6045 /* Make sure the scan wasn't cancelled before this queued work
6046 * was given the chance to run... */
6047 if (!test_bit(STATUS_SCANNING, &priv->status))
6048 goto done;
6049
6050 /* This should never be called or scheduled if there is currently
6051 * a scan active in the hardware. */
6052 if (test_bit(STATUS_SCAN_HW, &priv->status)) {
6053 IWL_DEBUG_INFO("Multiple concurrent scan requests in parallel. "
6054 "Ignoring second request.\n");
Tomas Winkler857485c2008-03-21 13:53:44 -07006055 ret = -EIO;
Zhu Yib481de92007-09-25 17:54:57 -07006056 goto done;
6057 }
6058
6059 if (test_bit(STATUS_EXIT_PENDING, &priv->status)) {
6060 IWL_DEBUG_SCAN("Aborting scan due to device shutdown\n");
6061 goto done;
6062 }
6063
6064 if (test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
6065 IWL_DEBUG_HC("Scan request while abort pending. Queuing.\n");
6066 goto done;
6067 }
6068
Tomas Winklerfee12472008-04-03 16:05:21 -07006069 if (iwl_is_rfkill(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07006070 IWL_DEBUG_HC("Aborting scan due to RF Kill activation\n");
6071 goto done;
6072 }
6073
6074 if (!test_bit(STATUS_READY, &priv->status)) {
6075 IWL_DEBUG_HC("Scan request while uninitialized. Queuing.\n");
6076 goto done;
6077 }
6078
6079 if (!priv->scan_bands) {
6080 IWL_DEBUG_HC("Aborting scan due to no requested bands\n");
6081 goto done;
6082 }
6083
6084 if (!priv->scan) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006085 priv->scan = kmalloc(sizeof(struct iwl4965_scan_cmd) +
Zhu Yib481de92007-09-25 17:54:57 -07006086 IWL_MAX_SCAN_SIZE, GFP_KERNEL);
6087 if (!priv->scan) {
Tomas Winkler857485c2008-03-21 13:53:44 -07006088 ret = -ENOMEM;
Zhu Yib481de92007-09-25 17:54:57 -07006089 goto done;
6090 }
6091 }
6092 scan = priv->scan;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006093 memset(scan, 0, sizeof(struct iwl4965_scan_cmd) + IWL_MAX_SCAN_SIZE);
Zhu Yib481de92007-09-25 17:54:57 -07006094
6095 scan->quiet_plcp_th = IWL_PLCP_QUIET_THRESH;
6096 scan->quiet_time = IWL_ACTIVE_QUIET_TIME;
6097
Tomas Winkler3109ece2008-03-28 16:33:35 -07006098 if (iwl_is_associated(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07006099 u16 interval = 0;
6100 u32 extra;
6101 u32 suspend_time = 100;
6102 u32 scan_suspend_time = 100;
6103 unsigned long flags;
6104
6105 IWL_DEBUG_INFO("Scanning while associated...\n");
6106
6107 spin_lock_irqsave(&priv->lock, flags);
6108 interval = priv->beacon_int;
6109 spin_unlock_irqrestore(&priv->lock, flags);
6110
6111 scan->suspend_time = 0;
mabbas052c4b92007-10-25 17:15:43 +08006112 scan->max_out_time = cpu_to_le32(200 * 1024);
Zhu Yib481de92007-09-25 17:54:57 -07006113 if (!interval)
6114 interval = suspend_time;
6115
6116 extra = (suspend_time / interval) << 22;
6117 scan_suspend_time = (extra |
6118 ((suspend_time % interval) * 1024));
6119 scan->suspend_time = cpu_to_le32(scan_suspend_time);
6120 IWL_DEBUG_SCAN("suspend_time 0x%X beacon interval %d\n",
6121 scan_suspend_time, interval);
6122 }
6123
6124 /* We should add the ability for user to lock to PASSIVE ONLY */
6125 if (priv->one_direct_scan) {
6126 IWL_DEBUG_SCAN
6127 ("Kicking off one direct scan for '%s'\n",
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006128 iwl4965_escape_essid(priv->direct_ssid,
Zhu Yib481de92007-09-25 17:54:57 -07006129 priv->direct_ssid_len));
6130 scan->direct_scan[0].id = WLAN_EID_SSID;
6131 scan->direct_scan[0].len = priv->direct_ssid_len;
6132 memcpy(scan->direct_scan[0].ssid,
6133 priv->direct_ssid, priv->direct_ssid_len);
6134 direct_mask = 1;
Tomas Winkler3109ece2008-03-28 16:33:35 -07006135 } else if (!iwl_is_associated(priv) && priv->essid_len) {
Zhu Yib481de92007-09-25 17:54:57 -07006136 scan->direct_scan[0].id = WLAN_EID_SSID;
6137 scan->direct_scan[0].len = priv->essid_len;
6138 memcpy(scan->direct_scan[0].ssid, priv->essid, priv->essid_len);
6139 direct_mask = 1;
Tomas Winkler857485c2008-03-21 13:53:44 -07006140 } else {
Zhu Yib481de92007-09-25 17:54:57 -07006141 direct_mask = 0;
Tomas Winkler857485c2008-03-21 13:53:44 -07006142 }
Zhu Yib481de92007-09-25 17:54:57 -07006143
Zhu Yib481de92007-09-25 17:54:57 -07006144 scan->tx_cmd.tx_flags = TX_CMD_FLG_SEQ_CTL_MSK;
6145 scan->tx_cmd.sta_id = priv->hw_setting.bcast_sta_id;
6146 scan->tx_cmd.stop_time.life_time = TX_CMD_LIFE_TIME_INFINITE;
6147
Zhu Yib481de92007-09-25 17:54:57 -07006148
6149 switch (priv->scan_bands) {
6150 case 2:
6151 scan->flags = RXON_FLG_BAND_24G_MSK | RXON_FLG_AUTO_DETECT_MSK;
6152 scan->tx_cmd.rate_n_flags =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006153 iwl4965_hw_set_rate_n_flags(IWL_RATE_1M_PLCP,
Zhu Yib481de92007-09-25 17:54:57 -07006154 RATE_MCS_ANT_B_MSK|RATE_MCS_CCK_MSK);
6155
6156 scan->good_CRC_th = 0;
Johannes Berg8318d782008-01-24 19:38:38 +01006157 band = IEEE80211_BAND_2GHZ;
Zhu Yib481de92007-09-25 17:54:57 -07006158 break;
6159
6160 case 1:
6161 scan->tx_cmd.rate_n_flags =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006162 iwl4965_hw_set_rate_n_flags(IWL_RATE_6M_PLCP,
Zhu Yib481de92007-09-25 17:54:57 -07006163 RATE_MCS_ANT_B_MSK);
6164 scan->good_CRC_th = IWL_GOOD_CRC_TH;
Johannes Berg8318d782008-01-24 19:38:38 +01006165 band = IEEE80211_BAND_5GHZ;
Zhu Yib481de92007-09-25 17:54:57 -07006166 break;
6167
6168 default:
6169 IWL_WARNING("Invalid scan band count\n");
6170 goto done;
6171 }
6172
Tomas Winkler78330fd2008-02-06 02:37:18 +02006173 /* We don't build a direct scan probe request; the uCode will do
6174 * that based on the direct_mask added to each channel entry */
6175 cmd_len = iwl4965_fill_probe_req(priv, band,
6176 (struct ieee80211_mgmt *)scan->data,
6177 IWL_MAX_SCAN_SIZE - sizeof(*scan), 0);
6178
6179 scan->tx_cmd.len = cpu_to_le16(cmd_len);
Zhu Yib481de92007-09-25 17:54:57 -07006180 /* select Rx chains */
6181
6182 /* Force use of chains B and C (0x6) for scan Rx.
6183 * Avoid A (0x1) because of its off-channel reception on A-band.
6184 * MIMO is not used here, but value is required to make uCode happy. */
6185 scan->rx_chain = RXON_RX_CHAIN_DRIVER_FORCE_MSK |
6186 cpu_to_le16((0x7 << RXON_RX_CHAIN_VALID_POS) |
6187 (0x6 << RXON_RX_CHAIN_FORCE_SEL_POS) |
6188 (0x7 << RXON_RX_CHAIN_FORCE_MIMO_SEL_POS));
6189
6190 if (priv->iw_mode == IEEE80211_IF_TYPE_MNTR)
6191 scan->filter_flags = RXON_FILTER_PROMISC_MSK;
6192
Reinette Chatre26c0f032008-03-11 16:17:15 -07006193 if (direct_mask) {
Zhu Yib481de92007-09-25 17:54:57 -07006194 IWL_DEBUG_SCAN
6195 ("Initiating direct scan for %s.\n",
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006196 iwl4965_escape_essid(priv->essid, priv->essid_len));
Reinette Chatre26c0f032008-03-11 16:17:15 -07006197 scan->channel_count =
6198 iwl4965_get_channels_for_scan(
6199 priv, band, 1, /* active */
6200 direct_mask,
6201 (void *)&scan->data[le16_to_cpu(scan->tx_cmd.len)]);
6202 } else {
Zhu Yib481de92007-09-25 17:54:57 -07006203 IWL_DEBUG_SCAN("Initiating indirect scan.\n");
Reinette Chatre26c0f032008-03-11 16:17:15 -07006204 scan->channel_count =
6205 iwl4965_get_channels_for_scan(
6206 priv, band, 0, /* passive */
6207 direct_mask,
6208 (void *)&scan->data[le16_to_cpu(scan->tx_cmd.len)]);
6209 }
Zhu Yib481de92007-09-25 17:54:57 -07006210
6211 cmd.len += le16_to_cpu(scan->tx_cmd.len) +
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006212 scan->channel_count * sizeof(struct iwl4965_scan_channel);
Zhu Yib481de92007-09-25 17:54:57 -07006213 cmd.data = scan;
6214 scan->len = cpu_to_le16(cmd.len);
6215
6216 set_bit(STATUS_SCAN_HW, &priv->status);
Tomas Winkler857485c2008-03-21 13:53:44 -07006217 ret = iwl_send_cmd_sync(priv, &cmd);
6218 if (ret)
Zhu Yib481de92007-09-25 17:54:57 -07006219 goto done;
6220
6221 queue_delayed_work(priv->workqueue, &priv->scan_check,
6222 IWL_SCAN_CHECK_WATCHDOG);
6223
6224 mutex_unlock(&priv->mutex);
6225 return;
6226
6227 done:
Ian Schram01ebd062007-10-25 17:15:22 +08006228 /* inform mac80211 scan aborted */
Zhu Yib481de92007-09-25 17:54:57 -07006229 queue_work(priv->workqueue, &priv->scan_completed);
6230 mutex_unlock(&priv->mutex);
6231}
6232
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006233static void iwl4965_bg_up(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07006234{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006235 struct iwl_priv *priv = container_of(data, struct iwl_priv, up);
Zhu Yib481de92007-09-25 17:54:57 -07006236
6237 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
6238 return;
6239
6240 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006241 __iwl4965_up(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006242 mutex_unlock(&priv->mutex);
6243}
6244
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006245static void iwl4965_bg_restart(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07006246{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006247 struct iwl_priv *priv = container_of(data, struct iwl_priv, restart);
Zhu Yib481de92007-09-25 17:54:57 -07006248
6249 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
6250 return;
6251
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006252 iwl4965_down(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006253 queue_work(priv->workqueue, &priv->up);
6254}
6255
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006256static void iwl4965_bg_rx_replenish(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07006257{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006258 struct iwl_priv *priv =
6259 container_of(data, struct iwl_priv, rx_replenish);
Zhu Yib481de92007-09-25 17:54:57 -07006260
6261 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
6262 return;
6263
6264 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006265 iwl4965_rx_replenish(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006266 mutex_unlock(&priv->mutex);
6267}
6268
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08006269#define IWL_DELAY_NEXT_SCAN (HZ*2)
6270
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006271static void iwl4965_bg_post_associate(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07006272{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006273 struct iwl_priv *priv = container_of(data, struct iwl_priv,
Zhu Yib481de92007-09-25 17:54:57 -07006274 post_associate.work);
Zhu Yib481de92007-09-25 17:54:57 -07006275 struct ieee80211_conf *conf = NULL;
Tomas Winkler857485c2008-03-21 13:53:44 -07006276 int ret = 0;
Joe Perches0795af52007-10-03 17:59:30 -07006277 DECLARE_MAC_BUF(mac);
Zhu Yib481de92007-09-25 17:54:57 -07006278
6279 if (priv->iw_mode == IEEE80211_IF_TYPE_AP) {
6280 IWL_ERROR("%s Should not be called in AP mode\n", __FUNCTION__);
6281 return;
6282 }
6283
Joe Perches0795af52007-10-03 17:59:30 -07006284 IWL_DEBUG_ASSOC("Associated as %d to: %s\n",
6285 priv->assoc_id,
6286 print_mac(mac, priv->active_rxon.bssid_addr));
Zhu Yib481de92007-09-25 17:54:57 -07006287
6288
6289 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
6290 return;
6291
6292 mutex_lock(&priv->mutex);
6293
Johannes Berg32bfd352007-12-19 01:31:26 +01006294 if (!priv->vif || !priv->is_open) {
Mohamed Abbas948c1712007-10-25 17:15:45 +08006295 mutex_unlock(&priv->mutex);
6296 return;
6297 }
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006298 iwl4965_scan_cancel_timeout(priv, 200);
mabbas052c4b92007-10-25 17:15:43 +08006299
Zhu Yib481de92007-09-25 17:54:57 -07006300 conf = ieee80211_get_hw_conf(priv->hw);
6301
6302 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006303 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006304
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006305 memset(&priv->rxon_timing, 0, sizeof(struct iwl4965_rxon_time_cmd));
6306 iwl4965_setup_rxon_timing(priv);
Tomas Winkler857485c2008-03-21 13:53:44 -07006307 ret = iwl_send_cmd_pdu(priv, REPLY_RXON_TIMING,
Zhu Yib481de92007-09-25 17:54:57 -07006308 sizeof(priv->rxon_timing), &priv->rxon_timing);
Tomas Winkler857485c2008-03-21 13:53:44 -07006309 if (ret)
Zhu Yib481de92007-09-25 17:54:57 -07006310 IWL_WARNING("REPLY_RXON_TIMING failed - "
6311 "Attempting to continue.\n");
6312
6313 priv->staging_rxon.filter_flags |= RXON_FILTER_ASSOC_MSK;
6314
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08006315#ifdef CONFIG_IWL4965_HT
Ron Rindjunskyfd105e72007-11-26 16:14:39 +02006316 if (priv->current_ht_config.is_ht)
6317 iwl4965_set_rxon_ht(priv, &priv->current_ht_config);
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08006318#endif /* CONFIG_IWL4965_HT*/
Zhu Yib481de92007-09-25 17:54:57 -07006319 iwl4965_set_rxon_chain(priv);
6320 priv->staging_rxon.assoc_id = cpu_to_le16(priv->assoc_id);
6321
6322 IWL_DEBUG_ASSOC("assoc id %d beacon interval %d\n",
6323 priv->assoc_id, priv->beacon_int);
6324
6325 if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_PREAMBLE)
6326 priv->staging_rxon.flags |= RXON_FLG_SHORT_PREAMBLE_MSK;
6327 else
6328 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_PREAMBLE_MSK;
6329
6330 if (priv->staging_rxon.flags & RXON_FLG_BAND_24G_MSK) {
6331 if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_SLOT_TIME)
6332 priv->staging_rxon.flags |= RXON_FLG_SHORT_SLOT_MSK;
6333 else
6334 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
6335
6336 if (priv->iw_mode == IEEE80211_IF_TYPE_IBSS)
6337 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
6338
6339 }
6340
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006341 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006342
6343 switch (priv->iw_mode) {
6344 case IEEE80211_IF_TYPE_STA:
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006345 iwl4965_rate_scale_init(priv->hw, IWL_AP_ID);
Zhu Yib481de92007-09-25 17:54:57 -07006346 break;
6347
6348 case IEEE80211_IF_TYPE_IBSS:
6349
6350 /* clear out the station table */
Assaf Kraussbf85ea42008-03-14 10:38:49 -07006351 iwlcore_clear_stations_table(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006352
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006353 iwl4965_rxon_add_station(priv, iwl4965_broadcast_addr, 0);
6354 iwl4965_rxon_add_station(priv, priv->bssid, 0);
6355 iwl4965_rate_scale_init(priv->hw, IWL_STA_ID);
6356 iwl4965_send_beacon_cmd(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006357
6358 break;
6359
6360 default:
6361 IWL_ERROR("%s Should not be called in %d mode\n",
6362 __FUNCTION__, priv->iw_mode);
6363 break;
6364 }
6365
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006366 iwl4965_sequence_reset(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006367
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08006368#ifdef CONFIG_IWL4965_SENSITIVITY
Zhu Yib481de92007-09-25 17:54:57 -07006369 /* Enable Rx differential gain and sensitivity calibrations */
6370 iwl4965_chain_noise_reset(priv);
6371 priv->start_calib = 1;
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08006372#endif /* CONFIG_IWL4965_SENSITIVITY */
Zhu Yib481de92007-09-25 17:54:57 -07006373
6374 if (priv->iw_mode == IEEE80211_IF_TYPE_IBSS)
6375 priv->assoc_station_added = 1;
6376
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006377 iwl4965_activate_qos(priv, 0);
Ron Rindjunsky292ae172008-02-06 11:20:39 -08006378
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08006379 /* we have just associated, don't start scan too early */
6380 priv->next_scan_jiffies = jiffies + IWL_DELAY_NEXT_SCAN;
Zhu Yib481de92007-09-25 17:54:57 -07006381 mutex_unlock(&priv->mutex);
6382}
6383
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006384static void iwl4965_bg_abort_scan(struct work_struct *work)
Zhu Yib481de92007-09-25 17:54:57 -07006385{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006386 struct iwl_priv *priv = container_of(work, struct iwl_priv, abort_scan);
Zhu Yib481de92007-09-25 17:54:57 -07006387
Tomas Winklerfee12472008-04-03 16:05:21 -07006388 if (!iwl_is_ready(priv))
Zhu Yib481de92007-09-25 17:54:57 -07006389 return;
6390
6391 mutex_lock(&priv->mutex);
6392
6393 set_bit(STATUS_SCAN_ABORTING, &priv->status);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006394 iwl4965_send_scan_abort(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006395
6396 mutex_unlock(&priv->mutex);
6397}
6398
Zhu Yi76bb77e2007-11-22 10:53:22 +08006399static int iwl4965_mac_config(struct ieee80211_hw *hw, struct ieee80211_conf *conf);
6400
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006401static void iwl4965_bg_scan_completed(struct work_struct *work)
Zhu Yib481de92007-09-25 17:54:57 -07006402{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006403 struct iwl_priv *priv =
6404 container_of(work, struct iwl_priv, scan_completed);
Zhu Yib481de92007-09-25 17:54:57 -07006405
6406 IWL_DEBUG(IWL_DL_INFO | IWL_DL_SCAN, "SCAN complete scan\n");
6407
6408 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
6409 return;
6410
Zhu Yia0646472007-12-20 14:10:01 +08006411 if (test_bit(STATUS_CONF_PENDING, &priv->status))
6412 iwl4965_mac_config(priv->hw, ieee80211_get_hw_conf(priv->hw));
Zhu Yi76bb77e2007-11-22 10:53:22 +08006413
Zhu Yib481de92007-09-25 17:54:57 -07006414 ieee80211_scan_completed(priv->hw);
6415
6416 /* Since setting the TXPOWER may have been deferred while
6417 * performing the scan, fire one off */
6418 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006419 iwl4965_hw_reg_send_txpower(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006420 mutex_unlock(&priv->mutex);
6421}
6422
6423/*****************************************************************************
6424 *
6425 * mac80211 entry point functions
6426 *
6427 *****************************************************************************/
6428
Zhu Yi5a66926a2008-01-14 17:46:18 -08006429#define UCODE_READY_TIMEOUT (2 * HZ)
6430
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006431static int iwl4965_mac_start(struct ieee80211_hw *hw)
Zhu Yib481de92007-09-25 17:54:57 -07006432{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006433 struct iwl_priv *priv = hw->priv;
Zhu Yi5a66926a2008-01-14 17:46:18 -08006434 int ret;
Zhu Yib481de92007-09-25 17:54:57 -07006435
6436 IWL_DEBUG_MAC80211("enter\n");
6437
Zhu Yi5a66926a2008-01-14 17:46:18 -08006438 if (pci_enable_device(priv->pci_dev)) {
6439 IWL_ERROR("Fail to pci_enable_device\n");
6440 return -ENODEV;
6441 }
6442 pci_restore_state(priv->pci_dev);
6443 pci_enable_msi(priv->pci_dev);
6444
6445 ret = request_irq(priv->pci_dev->irq, iwl4965_isr, IRQF_SHARED,
6446 DRV_NAME, priv);
6447 if (ret) {
6448 IWL_ERROR("Error allocating IRQ %d\n", priv->pci_dev->irq);
6449 goto out_disable_msi;
6450 }
6451
Zhu Yib481de92007-09-25 17:54:57 -07006452 /* we should be verifying the device is ready to be opened */
6453 mutex_lock(&priv->mutex);
6454
Zhu Yi5a66926a2008-01-14 17:46:18 -08006455 memset(&priv->staging_rxon, 0, sizeof(struct iwl4965_rxon_cmd));
6456 /* fetch ucode file from disk, alloc and copy to bus-master buffers ...
6457 * ucode filename and max sizes are card-specific. */
6458
6459 if (!priv->ucode_code.len) {
6460 ret = iwl4965_read_ucode(priv);
6461 if (ret) {
6462 IWL_ERROR("Could not read microcode: %d\n", ret);
6463 mutex_unlock(&priv->mutex);
6464 goto out_release_irq;
6465 }
6466 }
6467
Zhu Yie655b9f2008-01-24 02:19:38 -08006468 ret = __iwl4965_up(priv);
Zhu Yi5a66926a2008-01-14 17:46:18 -08006469
Zhu Yib481de92007-09-25 17:54:57 -07006470 mutex_unlock(&priv->mutex);
Zhu Yi5a66926a2008-01-14 17:46:18 -08006471
Zhu Yie655b9f2008-01-24 02:19:38 -08006472 if (ret)
6473 goto out_release_irq;
6474
6475 IWL_DEBUG_INFO("Start UP work done.\n");
6476
6477 if (test_bit(STATUS_IN_SUSPEND, &priv->status))
6478 return 0;
6479
Zhu Yi5a66926a2008-01-14 17:46:18 -08006480 /* Wait for START_ALIVE from ucode. Otherwise callbacks from
6481 * mac80211 will not be run successfully. */
6482 ret = wait_event_interruptible_timeout(priv->wait_command_queue,
6483 test_bit(STATUS_READY, &priv->status),
6484 UCODE_READY_TIMEOUT);
6485 if (!ret) {
6486 if (!test_bit(STATUS_READY, &priv->status)) {
6487 IWL_ERROR("Wait for START_ALIVE timeout after %dms.\n",
6488 jiffies_to_msecs(UCODE_READY_TIMEOUT));
6489 ret = -ETIMEDOUT;
6490 goto out_release_irq;
6491 }
6492 }
6493
Zhu Yie655b9f2008-01-24 02:19:38 -08006494 priv->is_open = 1;
Zhu Yib481de92007-09-25 17:54:57 -07006495 IWL_DEBUG_MAC80211("leave\n");
6496 return 0;
Zhu Yi5a66926a2008-01-14 17:46:18 -08006497
6498out_release_irq:
6499 free_irq(priv->pci_dev->irq, priv);
6500out_disable_msi:
6501 pci_disable_msi(priv->pci_dev);
Zhu Yie655b9f2008-01-24 02:19:38 -08006502 pci_disable_device(priv->pci_dev);
6503 priv->is_open = 0;
6504 IWL_DEBUG_MAC80211("leave - failed\n");
Zhu Yi5a66926a2008-01-14 17:46:18 -08006505 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07006506}
6507
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006508static void iwl4965_mac_stop(struct ieee80211_hw *hw)
Zhu Yib481de92007-09-25 17:54:57 -07006509{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006510 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07006511
6512 IWL_DEBUG_MAC80211("enter\n");
Mohamed Abbas948c1712007-10-25 17:15:45 +08006513
Zhu Yie655b9f2008-01-24 02:19:38 -08006514 if (!priv->is_open) {
6515 IWL_DEBUG_MAC80211("leave - skip\n");
6516 return;
6517 }
6518
Zhu Yib481de92007-09-25 17:54:57 -07006519 priv->is_open = 0;
Zhu Yi5a66926a2008-01-14 17:46:18 -08006520
Tomas Winklerfee12472008-04-03 16:05:21 -07006521 if (iwl_is_ready_rf(priv)) {
Zhu Yie655b9f2008-01-24 02:19:38 -08006522 /* stop mac, cancel any scan request and clear
6523 * RXON_FILTER_ASSOC_MSK BIT
6524 */
Zhu Yi5a66926a2008-01-14 17:46:18 -08006525 mutex_lock(&priv->mutex);
6526 iwl4965_scan_cancel_timeout(priv, 100);
6527 cancel_delayed_work(&priv->post_associate);
Mohamed Abbasfde35712007-11-29 11:10:15 +08006528 mutex_unlock(&priv->mutex);
Mohamed Abbasfde35712007-11-29 11:10:15 +08006529 }
6530
Zhu Yi5a66926a2008-01-14 17:46:18 -08006531 iwl4965_down(priv);
6532
6533 flush_workqueue(priv->workqueue);
6534 free_irq(priv->pci_dev->irq, priv);
6535 pci_disable_msi(priv->pci_dev);
6536 pci_save_state(priv->pci_dev);
6537 pci_disable_device(priv->pci_dev);
Mohamed Abbas948c1712007-10-25 17:15:45 +08006538
Zhu Yib481de92007-09-25 17:54:57 -07006539 IWL_DEBUG_MAC80211("leave\n");
Zhu Yib481de92007-09-25 17:54:57 -07006540}
6541
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006542static int iwl4965_mac_tx(struct ieee80211_hw *hw, struct sk_buff *skb,
Zhu Yib481de92007-09-25 17:54:57 -07006543 struct ieee80211_tx_control *ctl)
6544{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006545 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07006546
6547 IWL_DEBUG_MAC80211("enter\n");
6548
6549 if (priv->iw_mode == IEEE80211_IF_TYPE_MNTR) {
6550 IWL_DEBUG_MAC80211("leave - monitor\n");
6551 return -1;
6552 }
6553
6554 IWL_DEBUG_TX("dev->xmit(%d bytes) at rate 0x%02x\n", skb->len,
Johannes Berg8318d782008-01-24 19:38:38 +01006555 ctl->tx_rate->bitrate);
Zhu Yib481de92007-09-25 17:54:57 -07006556
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006557 if (iwl4965_tx_skb(priv, skb, ctl))
Zhu Yib481de92007-09-25 17:54:57 -07006558 dev_kfree_skb_any(skb);
6559
6560 IWL_DEBUG_MAC80211("leave\n");
6561 return 0;
6562}
6563
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006564static int iwl4965_mac_add_interface(struct ieee80211_hw *hw,
Zhu Yib481de92007-09-25 17:54:57 -07006565 struct ieee80211_if_init_conf *conf)
6566{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006567 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07006568 unsigned long flags;
Joe Perches0795af52007-10-03 17:59:30 -07006569 DECLARE_MAC_BUF(mac);
Zhu Yib481de92007-09-25 17:54:57 -07006570
Johannes Berg32bfd352007-12-19 01:31:26 +01006571 IWL_DEBUG_MAC80211("enter: type %d\n", conf->type);
Zhu Yib481de92007-09-25 17:54:57 -07006572
Johannes Berg32bfd352007-12-19 01:31:26 +01006573 if (priv->vif) {
6574 IWL_DEBUG_MAC80211("leave - vif != NULL\n");
Reinette Chatre75849d22008-01-23 10:15:17 -08006575 return -EOPNOTSUPP;
Zhu Yib481de92007-09-25 17:54:57 -07006576 }
6577
6578 spin_lock_irqsave(&priv->lock, flags);
Johannes Berg32bfd352007-12-19 01:31:26 +01006579 priv->vif = conf->vif;
Zhu Yib481de92007-09-25 17:54:57 -07006580
6581 spin_unlock_irqrestore(&priv->lock, flags);
6582
6583 mutex_lock(&priv->mutex);
Tomas Winkler864792e2007-11-27 21:00:52 +02006584
6585 if (conf->mac_addr) {
6586 IWL_DEBUG_MAC80211("Set %s\n", print_mac(mac, conf->mac_addr));
6587 memcpy(priv->mac_addr, conf->mac_addr, ETH_ALEN);
6588 }
Zhu Yib481de92007-09-25 17:54:57 -07006589
Tomas Winklerfee12472008-04-03 16:05:21 -07006590 if (iwl_is_ready(priv))
Zhu Yi5a66926a2008-01-14 17:46:18 -08006591 iwl4965_set_mode(priv, conf->type);
6592
Zhu Yib481de92007-09-25 17:54:57 -07006593 mutex_unlock(&priv->mutex);
6594
Zhu Yi5a66926a2008-01-14 17:46:18 -08006595 IWL_DEBUG_MAC80211("leave\n");
Zhu Yib481de92007-09-25 17:54:57 -07006596 return 0;
6597}
6598
6599/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006600 * iwl4965_mac_config - mac80211 config callback
Zhu Yib481de92007-09-25 17:54:57 -07006601 *
6602 * We ignore conf->flags & IEEE80211_CONF_SHORT_SLOT_TIME since it seems to
6603 * be set inappropriately and the driver currently sets the hardware up to
6604 * use it whenever needed.
6605 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006606static int iwl4965_mac_config(struct ieee80211_hw *hw, struct ieee80211_conf *conf)
Zhu Yib481de92007-09-25 17:54:57 -07006607{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006608 struct iwl_priv *priv = hw->priv;
Assaf Kraussbf85ea42008-03-14 10:38:49 -07006609 const struct iwl_channel_info *ch_info;
Zhu Yib481de92007-09-25 17:54:57 -07006610 unsigned long flags;
Zhu Yi76bb77e2007-11-22 10:53:22 +08006611 int ret = 0;
Zhu Yib481de92007-09-25 17:54:57 -07006612
6613 mutex_lock(&priv->mutex);
Johannes Berg8318d782008-01-24 19:38:38 +01006614 IWL_DEBUG_MAC80211("enter to channel %d\n", conf->channel->hw_value);
Zhu Yib481de92007-09-25 17:54:57 -07006615
Zhu Yi12342c42007-12-20 11:27:32 +08006616 priv->add_radiotap = !!(conf->flags & IEEE80211_CONF_RADIOTAP);
6617
Tomas Winklerfee12472008-04-03 16:05:21 -07006618 if (!iwl_is_ready(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07006619 IWL_DEBUG_MAC80211("leave - not ready\n");
Zhu Yi76bb77e2007-11-22 10:53:22 +08006620 ret = -EIO;
6621 goto out;
Zhu Yib481de92007-09-25 17:54:57 -07006622 }
6623
Assaf Krauss1ea87392008-03-18 14:57:50 -07006624 if (unlikely(!priv->cfg->mod_params->disable_hw_scan &&
Zhu Yib481de92007-09-25 17:54:57 -07006625 test_bit(STATUS_SCANNING, &priv->status))) {
Zhu Yia0646472007-12-20 14:10:01 +08006626 IWL_DEBUG_MAC80211("leave - scanning\n");
6627 set_bit(STATUS_CONF_PENDING, &priv->status);
Zhu Yib481de92007-09-25 17:54:57 -07006628 mutex_unlock(&priv->mutex);
Zhu Yia0646472007-12-20 14:10:01 +08006629 return 0;
Zhu Yib481de92007-09-25 17:54:57 -07006630 }
6631
6632 spin_lock_irqsave(&priv->lock, flags);
6633
Assaf Krauss8622e702008-03-21 13:53:43 -07006634 ch_info = iwl_get_channel_info(priv, conf->channel->band,
Johannes Berg8318d782008-01-24 19:38:38 +01006635 ieee80211_frequency_to_channel(conf->channel->center_freq));
Zhu Yib481de92007-09-25 17:54:57 -07006636 if (!is_channel_valid(ch_info)) {
Zhu Yib481de92007-09-25 17:54:57 -07006637 IWL_DEBUG_MAC80211("leave - invalid channel\n");
6638 spin_unlock_irqrestore(&priv->lock, flags);
Zhu Yi76bb77e2007-11-22 10:53:22 +08006639 ret = -EINVAL;
6640 goto out;
Zhu Yib481de92007-09-25 17:54:57 -07006641 }
6642
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08006643#ifdef CONFIG_IWL4965_HT
Tomas Winkler78330fd2008-02-06 02:37:18 +02006644 /* if we are switching from ht to 2.4 clear flags
Zhu Yib481de92007-09-25 17:54:57 -07006645 * from any ht related info since 2.4 does not
6646 * support ht */
Tomas Winkler78330fd2008-02-06 02:37:18 +02006647 if ((le16_to_cpu(priv->staging_rxon.channel) != conf->channel->hw_value)
Zhu Yib481de92007-09-25 17:54:57 -07006648#ifdef IEEE80211_CONF_CHANNEL_SWITCH
6649 && !(conf->flags & IEEE80211_CONF_CHANNEL_SWITCH)
6650#endif
6651 )
6652 priv->staging_rxon.flags = 0;
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08006653#endif /* CONFIG_IWL4965_HT */
Zhu Yib481de92007-09-25 17:54:57 -07006654
Assaf Kraussbf85ea42008-03-14 10:38:49 -07006655 iwlcore_set_rxon_channel(priv, conf->channel->band,
Johannes Berg8318d782008-01-24 19:38:38 +01006656 ieee80211_frequency_to_channel(conf->channel->center_freq));
Zhu Yib481de92007-09-25 17:54:57 -07006657
Johannes Berg8318d782008-01-24 19:38:38 +01006658 iwl4965_set_flags_for_phymode(priv, conf->channel->band);
Zhu Yib481de92007-09-25 17:54:57 -07006659
6660 /* The list of supported rates and rate mask can be different
Johannes Berg8318d782008-01-24 19:38:38 +01006661 * for each band; since the band may have changed, reset
Zhu Yib481de92007-09-25 17:54:57 -07006662 * the rate mask to what mac80211 lists */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006663 iwl4965_set_rate(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006664
6665 spin_unlock_irqrestore(&priv->lock, flags);
6666
6667#ifdef IEEE80211_CONF_CHANNEL_SWITCH
6668 if (conf->flags & IEEE80211_CONF_CHANNEL_SWITCH) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006669 iwl4965_hw_channel_switch(priv, conf->channel);
Zhu Yi76bb77e2007-11-22 10:53:22 +08006670 goto out;
Zhu Yib481de92007-09-25 17:54:57 -07006671 }
6672#endif
6673
Mohamed Abbasad97edd2008-03-28 16:21:06 -07006674 if (priv->cfg->ops->lib->radio_kill_sw)
6675 priv->cfg->ops->lib->radio_kill_sw(priv, !conf->radio_enabled);
Zhu Yib481de92007-09-25 17:54:57 -07006676
6677 if (!conf->radio_enabled) {
6678 IWL_DEBUG_MAC80211("leave - radio disabled\n");
Zhu Yi76bb77e2007-11-22 10:53:22 +08006679 goto out;
Zhu Yib481de92007-09-25 17:54:57 -07006680 }
6681
Tomas Winklerfee12472008-04-03 16:05:21 -07006682 if (iwl_is_rfkill(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07006683 IWL_DEBUG_MAC80211("leave - RF kill\n");
Zhu Yi76bb77e2007-11-22 10:53:22 +08006684 ret = -EIO;
6685 goto out;
Zhu Yib481de92007-09-25 17:54:57 -07006686 }
6687
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006688 iwl4965_set_rate(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006689
6690 if (memcmp(&priv->active_rxon,
6691 &priv->staging_rxon, sizeof(priv->staging_rxon)))
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006692 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006693 else
6694 IWL_DEBUG_INFO("No re-sending same RXON configuration.\n");
6695
6696 IWL_DEBUG_MAC80211("leave\n");
6697
Zhu Yia0646472007-12-20 14:10:01 +08006698out:
6699 clear_bit(STATUS_CONF_PENDING, &priv->status);
Zhu Yi5a66926a2008-01-14 17:46:18 -08006700 mutex_unlock(&priv->mutex);
Zhu Yi76bb77e2007-11-22 10:53:22 +08006701 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07006702}
6703
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006704static void iwl4965_config_ap(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07006705{
Tomas Winkler857485c2008-03-21 13:53:44 -07006706 int ret = 0;
Zhu Yib481de92007-09-25 17:54:57 -07006707
Maarten Lankhorstd986bcd2008-01-23 10:15:16 -08006708 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
Zhu Yib481de92007-09-25 17:54:57 -07006709 return;
6710
6711 /* The following should be done only at AP bring up */
6712 if ((priv->active_rxon.filter_flags & RXON_FILTER_ASSOC_MSK) == 0) {
6713
6714 /* RXON - unassoc (to set timing command) */
6715 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006716 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006717
6718 /* RXON Timing */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006719 memset(&priv->rxon_timing, 0, sizeof(struct iwl4965_rxon_time_cmd));
6720 iwl4965_setup_rxon_timing(priv);
Tomas Winkler857485c2008-03-21 13:53:44 -07006721 ret = iwl_send_cmd_pdu(priv, REPLY_RXON_TIMING,
Zhu Yib481de92007-09-25 17:54:57 -07006722 sizeof(priv->rxon_timing), &priv->rxon_timing);
Tomas Winkler857485c2008-03-21 13:53:44 -07006723 if (ret)
Zhu Yib481de92007-09-25 17:54:57 -07006724 IWL_WARNING("REPLY_RXON_TIMING failed - "
6725 "Attempting to continue.\n");
6726
6727 iwl4965_set_rxon_chain(priv);
6728
6729 /* FIXME: what should be the assoc_id for AP? */
6730 priv->staging_rxon.assoc_id = cpu_to_le16(priv->assoc_id);
6731 if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_PREAMBLE)
6732 priv->staging_rxon.flags |=
6733 RXON_FLG_SHORT_PREAMBLE_MSK;
6734 else
6735 priv->staging_rxon.flags &=
6736 ~RXON_FLG_SHORT_PREAMBLE_MSK;
6737
6738 if (priv->staging_rxon.flags & RXON_FLG_BAND_24G_MSK) {
6739 if (priv->assoc_capability &
6740 WLAN_CAPABILITY_SHORT_SLOT_TIME)
6741 priv->staging_rxon.flags |=
6742 RXON_FLG_SHORT_SLOT_MSK;
6743 else
6744 priv->staging_rxon.flags &=
6745 ~RXON_FLG_SHORT_SLOT_MSK;
6746
6747 if (priv->iw_mode == IEEE80211_IF_TYPE_IBSS)
6748 priv->staging_rxon.flags &=
6749 ~RXON_FLG_SHORT_SLOT_MSK;
6750 }
6751 /* restore RXON assoc */
6752 priv->staging_rxon.filter_flags |= RXON_FILTER_ASSOC_MSK;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006753 iwl4965_commit_rxon(priv);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006754 iwl4965_activate_qos(priv, 1);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006755 iwl4965_rxon_add_station(priv, iwl4965_broadcast_addr, 0);
Zhu Yie1493de2007-09-27 11:27:32 +08006756 }
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006757 iwl4965_send_beacon_cmd(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006758
6759 /* FIXME - we need to add code here to detect a totally new
6760 * configuration, reset the AP, unassoc, rxon timing, assoc,
6761 * clear sta table, add BCAST sta... */
6762}
6763
Johannes Berg32bfd352007-12-19 01:31:26 +01006764static int iwl4965_mac_config_interface(struct ieee80211_hw *hw,
6765 struct ieee80211_vif *vif,
Zhu Yib481de92007-09-25 17:54:57 -07006766 struct ieee80211_if_conf *conf)
6767{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006768 struct iwl_priv *priv = hw->priv;
Joe Perches0795af52007-10-03 17:59:30 -07006769 DECLARE_MAC_BUF(mac);
Zhu Yib481de92007-09-25 17:54:57 -07006770 unsigned long flags;
6771 int rc;
6772
6773 if (conf == NULL)
6774 return -EIO;
6775
Emmanuel Grumbachb716bb92008-03-04 18:09:32 -08006776 if (priv->vif != vif) {
6777 IWL_DEBUG_MAC80211("leave - priv->vif != vif\n");
6778 mutex_unlock(&priv->mutex);
6779 return 0;
6780 }
6781
Zhu Yib481de92007-09-25 17:54:57 -07006782 if ((priv->iw_mode == IEEE80211_IF_TYPE_AP) &&
6783 (!conf->beacon || !conf->ssid_len)) {
6784 IWL_DEBUG_MAC80211
6785 ("Leaving in AP mode because HostAPD is not ready.\n");
6786 return 0;
6787 }
6788
Tomas Winklerfee12472008-04-03 16:05:21 -07006789 if (!iwl_is_alive(priv))
Zhu Yi5a66926a2008-01-14 17:46:18 -08006790 return -EAGAIN;
6791
Zhu Yib481de92007-09-25 17:54:57 -07006792 mutex_lock(&priv->mutex);
6793
Zhu Yib481de92007-09-25 17:54:57 -07006794 if (conf->bssid)
Joe Perches0795af52007-10-03 17:59:30 -07006795 IWL_DEBUG_MAC80211("bssid: %s\n",
6796 print_mac(mac, conf->bssid));
Zhu Yib481de92007-09-25 17:54:57 -07006797
Johannes Berg4150c572007-09-17 01:29:23 -04006798/*
6799 * very dubious code was here; the probe filtering flag is never set:
6800 *
Zhu Yib481de92007-09-25 17:54:57 -07006801 if (unlikely(test_bit(STATUS_SCANNING, &priv->status)) &&
6802 !(priv->hw->flags & IEEE80211_HW_NO_PROBE_FILTERING)) {
Johannes Berg4150c572007-09-17 01:29:23 -04006803 */
Zhu Yib481de92007-09-25 17:54:57 -07006804
6805 if (priv->iw_mode == IEEE80211_IF_TYPE_AP) {
6806 if (!conf->bssid) {
6807 conf->bssid = priv->mac_addr;
6808 memcpy(priv->bssid, priv->mac_addr, ETH_ALEN);
Joe Perches0795af52007-10-03 17:59:30 -07006809 IWL_DEBUG_MAC80211("bssid was set to: %s\n",
6810 print_mac(mac, conf->bssid));
Zhu Yib481de92007-09-25 17:54:57 -07006811 }
6812 if (priv->ibss_beacon)
6813 dev_kfree_skb(priv->ibss_beacon);
6814
6815 priv->ibss_beacon = conf->beacon;
6816 }
6817
Tomas Winklerfee12472008-04-03 16:05:21 -07006818 if (iwl_is_rfkill(priv))
Mohamed Abbasfde35712007-11-29 11:10:15 +08006819 goto done;
6820
Zhu Yib481de92007-09-25 17:54:57 -07006821 if (conf->bssid && !is_zero_ether_addr(conf->bssid) &&
6822 !is_multicast_ether_addr(conf->bssid)) {
6823 /* If there is currently a HW scan going on in the background
6824 * then we need to cancel it else the RXON below will fail. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006825 if (iwl4965_scan_cancel_timeout(priv, 100)) {
Zhu Yib481de92007-09-25 17:54:57 -07006826 IWL_WARNING("Aborted scan still in progress "
6827 "after 100ms\n");
6828 IWL_DEBUG_MAC80211("leaving - scan abort failed.\n");
6829 mutex_unlock(&priv->mutex);
6830 return -EAGAIN;
6831 }
6832 memcpy(priv->staging_rxon.bssid_addr, conf->bssid, ETH_ALEN);
6833
6834 /* TODO: Audit driver for usage of these members and see
6835 * if mac80211 deprecates them (priv->bssid looks like it
6836 * shouldn't be there, but I haven't scanned the IBSS code
6837 * to verify) - jpk */
6838 memcpy(priv->bssid, conf->bssid, ETH_ALEN);
6839
6840 if (priv->iw_mode == IEEE80211_IF_TYPE_AP)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006841 iwl4965_config_ap(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006842 else {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006843 rc = iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006844 if ((priv->iw_mode == IEEE80211_IF_TYPE_STA) && rc)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006845 iwl4965_rxon_add_station(
Zhu Yib481de92007-09-25 17:54:57 -07006846 priv, priv->active_rxon.bssid_addr, 1);
6847 }
6848
6849 } else {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006850 iwl4965_scan_cancel_timeout(priv, 100);
Zhu Yib481de92007-09-25 17:54:57 -07006851 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006852 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006853 }
6854
Mohamed Abbasfde35712007-11-29 11:10:15 +08006855 done:
Zhu Yib481de92007-09-25 17:54:57 -07006856 spin_lock_irqsave(&priv->lock, flags);
6857 if (!conf->ssid_len)
6858 memset(priv->essid, 0, IW_ESSID_MAX_SIZE);
6859 else
6860 memcpy(priv->essid, conf->ssid, conf->ssid_len);
6861
6862 priv->essid_len = conf->ssid_len;
6863 spin_unlock_irqrestore(&priv->lock, flags);
6864
6865 IWL_DEBUG_MAC80211("leave\n");
6866 mutex_unlock(&priv->mutex);
6867
6868 return 0;
6869}
6870
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006871static void iwl4965_configure_filter(struct ieee80211_hw *hw,
Johannes Berg4150c572007-09-17 01:29:23 -04006872 unsigned int changed_flags,
6873 unsigned int *total_flags,
6874 int mc_count, struct dev_addr_list *mc_list)
6875{
6876 /*
6877 * XXX: dummy
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006878 * see also iwl4965_connection_init_rx_config
Johannes Berg4150c572007-09-17 01:29:23 -04006879 */
6880 *total_flags = 0;
6881}
6882
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006883static void iwl4965_mac_remove_interface(struct ieee80211_hw *hw,
Zhu Yib481de92007-09-25 17:54:57 -07006884 struct ieee80211_if_init_conf *conf)
6885{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006886 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07006887
6888 IWL_DEBUG_MAC80211("enter\n");
6889
6890 mutex_lock(&priv->mutex);
Mohamed Abbas948c1712007-10-25 17:15:45 +08006891
Tomas Winklerfee12472008-04-03 16:05:21 -07006892 if (iwl_is_ready_rf(priv)) {
Mohamed Abbasfde35712007-11-29 11:10:15 +08006893 iwl4965_scan_cancel_timeout(priv, 100);
6894 cancel_delayed_work(&priv->post_associate);
6895 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
6896 iwl4965_commit_rxon(priv);
6897 }
Johannes Berg32bfd352007-12-19 01:31:26 +01006898 if (priv->vif == conf->vif) {
6899 priv->vif = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07006900 memset(priv->bssid, 0, ETH_ALEN);
6901 memset(priv->essid, 0, IW_ESSID_MAX_SIZE);
6902 priv->essid_len = 0;
6903 }
6904 mutex_unlock(&priv->mutex);
6905
6906 IWL_DEBUG_MAC80211("leave\n");
6907
6908}
Johannes Berg471b3ef2007-12-28 14:32:58 +01006909
Tomas Winkler98952d52008-03-28 16:33:33 -07006910
6911#ifdef CONFIG_IWL4965_HT
6912static void iwl4965_ht_conf(struct iwl_priv *priv,
6913 struct ieee80211_bss_conf *bss_conf)
6914{
6915 struct ieee80211_ht_info *ht_conf = bss_conf->ht_conf;
6916 struct ieee80211_ht_bss_info *ht_bss_conf = bss_conf->ht_bss_conf;
6917 struct iwl_ht_info *iwl_conf = &priv->current_ht_config;
6918
6919 IWL_DEBUG_MAC80211("enter: \n");
6920
6921 iwl_conf->is_ht = bss_conf->assoc_ht;
6922
6923 if (!iwl_conf->is_ht)
6924 return;
6925
6926 priv->ps_mode = (u8)((ht_conf->cap & IEEE80211_HT_CAP_MIMO_PS) >> 2);
6927
6928 if (ht_conf->cap & IEEE80211_HT_CAP_SGI_20)
6929 iwl_conf->sgf |= 0x1;
6930 if (ht_conf->cap & IEEE80211_HT_CAP_SGI_40)
6931 iwl_conf->sgf |= 0x2;
6932
6933 iwl_conf->is_green_field = !!(ht_conf->cap & IEEE80211_HT_CAP_GRN_FLD);
6934 iwl_conf->max_amsdu_size =
6935 !!(ht_conf->cap & IEEE80211_HT_CAP_MAX_AMSDU);
6936
6937 iwl_conf->supported_chan_width =
6938 !!(ht_conf->cap & IEEE80211_HT_CAP_SUP_WIDTH);
6939 iwl_conf->extension_chan_offset =
6940 ht_bss_conf->bss_cap & IEEE80211_HT_IE_CHA_SEC_OFFSET;
6941 /* If no above or below channel supplied disable FAT channel */
6942 if (iwl_conf->extension_chan_offset != IWL_EXT_CHANNEL_OFFSET_ABOVE &&
6943 iwl_conf->extension_chan_offset != IWL_EXT_CHANNEL_OFFSET_BELOW)
6944 iwl_conf->supported_chan_width = 0;
6945
6946 iwl_conf->tx_mimo_ps_mode =
6947 (u8)((ht_conf->cap & IEEE80211_HT_CAP_MIMO_PS) >> 2);
6948 memcpy(iwl_conf->supp_mcs_set, ht_conf->supp_mcs_set, 16);
6949
6950 iwl_conf->control_channel = ht_bss_conf->primary_channel;
6951 iwl_conf->tx_chan_width =
6952 !!(ht_bss_conf->bss_cap & IEEE80211_HT_IE_CHA_WIDTH);
6953 iwl_conf->ht_protection =
6954 ht_bss_conf->bss_op_mode & IEEE80211_HT_IE_HT_PROTECTION;
6955 iwl_conf->non_GF_STA_present =
6956 !!(ht_bss_conf->bss_op_mode & IEEE80211_HT_IE_NON_GF_STA_PRSNT);
6957
6958 IWL_DEBUG_MAC80211("control channel %d\n", iwl_conf->control_channel);
6959 IWL_DEBUG_MAC80211("leave\n");
6960}
6961#else
6962static inline void iwl4965_ht_conf(struct iwl_priv *priv,
6963 struct ieee80211_bss_conf *bss_conf)
6964{
6965}
6966#endif
6967
Tomas Winkler3109ece2008-03-28 16:33:35 -07006968#define IWL_DELAY_NEXT_SCAN_AFTER_ASSOC (HZ*6)
Johannes Berg471b3ef2007-12-28 14:32:58 +01006969static void iwl4965_bss_info_changed(struct ieee80211_hw *hw,
6970 struct ieee80211_vif *vif,
6971 struct ieee80211_bss_conf *bss_conf,
6972 u32 changes)
Tomas Winkler220173b2007-10-16 00:50:25 +02006973{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006974 struct iwl_priv *priv = hw->priv;
Tomas Winkler220173b2007-10-16 00:50:25 +02006975
Tomas Winkler3109ece2008-03-28 16:33:35 -07006976 IWL_DEBUG_MAC80211("changes = 0x%X\n", changes);
6977
Johannes Berg471b3ef2007-12-28 14:32:58 +01006978 if (changes & BSS_CHANGED_ERP_PREAMBLE) {
Tomas Winkler3109ece2008-03-28 16:33:35 -07006979 IWL_DEBUG_MAC80211("ERP_PREAMBLE %d\n",
6980 bss_conf->use_short_preamble);
Johannes Berg471b3ef2007-12-28 14:32:58 +01006981 if (bss_conf->use_short_preamble)
Tomas Winkler220173b2007-10-16 00:50:25 +02006982 priv->staging_rxon.flags |= RXON_FLG_SHORT_PREAMBLE_MSK;
6983 else
6984 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_PREAMBLE_MSK;
6985 }
6986
Johannes Berg471b3ef2007-12-28 14:32:58 +01006987 if (changes & BSS_CHANGED_ERP_CTS_PROT) {
Tomas Winkler3109ece2008-03-28 16:33:35 -07006988 IWL_DEBUG_MAC80211("ERP_CTS %d\n", bss_conf->use_cts_prot);
Johannes Berg8318d782008-01-24 19:38:38 +01006989 if (bss_conf->use_cts_prot && (priv->band != IEEE80211_BAND_5GHZ))
Tomas Winkler220173b2007-10-16 00:50:25 +02006990 priv->staging_rxon.flags |= RXON_FLG_TGG_PROTECT_MSK;
6991 else
6992 priv->staging_rxon.flags &= ~RXON_FLG_TGG_PROTECT_MSK;
6993 }
6994
Tomas Winkler98952d52008-03-28 16:33:33 -07006995 if (changes & BSS_CHANGED_HT) {
Tomas Winkler3109ece2008-03-28 16:33:35 -07006996 IWL_DEBUG_MAC80211("HT %d\n", bss_conf->assoc_ht);
Tomas Winkler98952d52008-03-28 16:33:33 -07006997 iwl4965_ht_conf(priv, bss_conf);
6998 iwl4965_set_rxon_chain(priv);
6999 }
7000
Johannes Berg471b3ef2007-12-28 14:32:58 +01007001 if (changes & BSS_CHANGED_ASSOC) {
Tomas Winkler3109ece2008-03-28 16:33:35 -07007002 IWL_DEBUG_MAC80211("ASSOC %d\n", bss_conf->assoc);
7003 if (bss_conf->assoc) {
7004 priv->assoc_id = bss_conf->aid;
7005 priv->beacon_int = bss_conf->beacon_int;
7006 priv->timestamp = bss_conf->timestamp;
7007 priv->assoc_capability = bss_conf->assoc_capability;
7008 priv->next_scan_jiffies = jiffies +
7009 IWL_DELAY_NEXT_SCAN_AFTER_ASSOC;
7010 queue_work(priv->workqueue, &priv->post_associate.work);
7011 } else {
7012 priv->assoc_id = 0;
7013 IWL_DEBUG_MAC80211("DISASSOC %d\n", bss_conf->assoc);
7014 }
7015 } else if (changes && iwl_is_associated(priv) && priv->assoc_id) {
7016 IWL_DEBUG_MAC80211("Associated Changes %d\n", changes);
7017 iwl4965_send_rxon_assoc(priv);
Johannes Berg471b3ef2007-12-28 14:32:58 +01007018 }
7019
Tomas Winkler220173b2007-10-16 00:50:25 +02007020}
Zhu Yib481de92007-09-25 17:54:57 -07007021
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007022static int iwl4965_mac_hw_scan(struct ieee80211_hw *hw, u8 *ssid, size_t len)
Zhu Yib481de92007-09-25 17:54:57 -07007023{
7024 int rc = 0;
7025 unsigned long flags;
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007026 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07007027
7028 IWL_DEBUG_MAC80211("enter\n");
7029
mabbas052c4b92007-10-25 17:15:43 +08007030 mutex_lock(&priv->mutex);
Zhu Yib481de92007-09-25 17:54:57 -07007031 spin_lock_irqsave(&priv->lock, flags);
7032
Tomas Winklerfee12472008-04-03 16:05:21 -07007033 if (!iwl_is_ready_rf(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07007034 rc = -EIO;
7035 IWL_DEBUG_MAC80211("leave - not ready or exit pending\n");
7036 goto out_unlock;
7037 }
7038
7039 if (priv->iw_mode == IEEE80211_IF_TYPE_AP) { /* APs don't scan */
7040 rc = -EIO;
7041 IWL_ERROR("ERROR: APs don't scan\n");
7042 goto out_unlock;
7043 }
7044
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08007045 /* we don't schedule scan within next_scan_jiffies period */
7046 if (priv->next_scan_jiffies &&
7047 time_after(priv->next_scan_jiffies, jiffies)) {
7048 rc = -EAGAIN;
7049 goto out_unlock;
7050 }
Zhu Yib481de92007-09-25 17:54:57 -07007051 /* if we just finished scan ask for delay */
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08007052 if (priv->last_scan_jiffies && time_after(priv->last_scan_jiffies +
7053 IWL_DELAY_NEXT_SCAN, jiffies)) {
Zhu Yib481de92007-09-25 17:54:57 -07007054 rc = -EAGAIN;
7055 goto out_unlock;
7056 }
7057 if (len) {
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08007058 IWL_DEBUG_SCAN("direct scan for %s [%d]\n ",
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007059 iwl4965_escape_essid(ssid, len), (int)len);
Zhu Yib481de92007-09-25 17:54:57 -07007060
7061 priv->one_direct_scan = 1;
7062 priv->direct_ssid_len = (u8)
7063 min((u8) len, (u8) IW_ESSID_MAX_SIZE);
7064 memcpy(priv->direct_ssid, ssid, priv->direct_ssid_len);
Mohamed Abbas948c1712007-10-25 17:15:45 +08007065 } else
7066 priv->one_direct_scan = 0;
Zhu Yib481de92007-09-25 17:54:57 -07007067
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007068 rc = iwl4965_scan_initiate(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007069
7070 IWL_DEBUG_MAC80211("leave\n");
7071
7072out_unlock:
7073 spin_unlock_irqrestore(&priv->lock, flags);
mabbas052c4b92007-10-25 17:15:43 +08007074 mutex_unlock(&priv->mutex);
Zhu Yib481de92007-09-25 17:54:57 -07007075
7076 return rc;
7077}
7078
Emmanuel Grumbachab885f82008-03-20 15:06:43 +02007079static void iwl4965_mac_update_tkip_key(struct ieee80211_hw *hw,
7080 struct ieee80211_key_conf *keyconf, const u8 *addr,
7081 u32 iv32, u16 *phase1key)
7082{
7083 struct iwl_priv *priv = hw->priv;
7084 u8 sta_id = IWL_INVALID_STATION;
7085 unsigned long flags;
7086 __le16 key_flags = 0;
7087 int i;
7088 DECLARE_MAC_BUF(mac);
7089
7090 IWL_DEBUG_MAC80211("enter\n");
7091
7092 sta_id = iwl4965_hw_find_station(priv, addr);
7093 if (sta_id == IWL_INVALID_STATION) {
7094 IWL_DEBUG_MAC80211("leave - %s not in station map.\n",
7095 print_mac(mac, addr));
7096 return;
7097 }
7098
7099 iwl4965_scan_cancel_timeout(priv, 100);
7100
7101 key_flags |= (STA_KEY_FLG_TKIP | STA_KEY_FLG_MAP_KEY_MSK);
7102 key_flags |= cpu_to_le16(keyconf->keyidx << STA_KEY_FLG_KEYID_POS);
7103 key_flags &= ~STA_KEY_FLG_INVALID;
7104
7105 if (sta_id == priv->hw_setting.bcast_sta_id)
7106 key_flags |= STA_KEY_MULTICAST_MSK;
7107
7108 spin_lock_irqsave(&priv->sta_lock, flags);
7109
7110 priv->stations[sta_id].sta.key.key_offset =
7111 (sta_id % STA_KEY_MAX_NUM);/* FIXME */
7112 priv->stations[sta_id].sta.key.key_flags = key_flags;
7113 priv->stations[sta_id].sta.key.tkip_rx_tsc_byte2 = (u8) iv32;
7114
7115 for (i = 0; i < 5; i++)
7116 priv->stations[sta_id].sta.key.tkip_rx_ttak[i] =
7117 cpu_to_le16(phase1key[i]);
7118
7119 priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
7120 priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
7121
7122 iwl4965_send_add_station(priv, &priv->stations[sta_id].sta, CMD_ASYNC);
7123
7124 spin_unlock_irqrestore(&priv->sta_lock, flags);
7125
7126 IWL_DEBUG_MAC80211("leave\n");
7127}
7128
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007129static int iwl4965_mac_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
Zhu Yib481de92007-09-25 17:54:57 -07007130 const u8 *local_addr, const u8 *addr,
7131 struct ieee80211_key_conf *key)
7132{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007133 struct iwl_priv *priv = hw->priv;
Joe Perches0795af52007-10-03 17:59:30 -07007134 DECLARE_MAC_BUF(mac);
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07007135 int ret = 0;
7136 u8 sta_id = IWL_INVALID_STATION;
7137 u8 static_key;
Zhu Yib481de92007-09-25 17:54:57 -07007138
7139 IWL_DEBUG_MAC80211("enter\n");
7140
Assaf Krauss1ea87392008-03-18 14:57:50 -07007141 if (!priv->cfg->mod_params->hw_crypto) {
Zhu Yib481de92007-09-25 17:54:57 -07007142 IWL_DEBUG_MAC80211("leave - hwcrypto disabled\n");
7143 return -EOPNOTSUPP;
7144 }
7145
7146 if (is_zero_ether_addr(addr))
7147 /* only support pairwise keys */
7148 return -EOPNOTSUPP;
7149
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07007150 /* FIXME: need to differenciate between static and dynamic key
7151 * in the level of mac80211 */
Tomas Winkler3109ece2008-03-28 16:33:35 -07007152 static_key = !iwl_is_associated(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007153
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07007154 if (!static_key) {
7155 sta_id = iwl4965_hw_find_station(priv, addr);
7156 if (sta_id == IWL_INVALID_STATION) {
7157 IWL_DEBUG_MAC80211("leave - %s not in station map.\n",
7158 print_mac(mac, addr));
7159 return -EINVAL;
7160 }
7161 }
Zhu Yib481de92007-09-25 17:54:57 -07007162
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007163 iwl4965_scan_cancel_timeout(priv, 100);
mabbas052c4b92007-10-25 17:15:43 +08007164
Zhu Yib481de92007-09-25 17:54:57 -07007165 switch (cmd) {
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07007166 case SET_KEY:
7167 if (static_key)
7168 ret = iwl4965_set_static_key(priv, key);
7169 else
7170 ret = iwl4965_set_dynamic_key(priv, key, sta_id);
7171
7172 IWL_DEBUG_MAC80211("enable hwcrypto key\n");
Zhu Yib481de92007-09-25 17:54:57 -07007173 break;
7174 case DISABLE_KEY:
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07007175 if (static_key)
7176 ret = iwl4965_remove_static_key(priv);
7177 else
7178 ret = iwl4965_clear_sta_key_info(priv, sta_id);
7179
7180 IWL_DEBUG_MAC80211("disable hwcrypto key\n");
Zhu Yib481de92007-09-25 17:54:57 -07007181 break;
7182 default:
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07007183 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07007184 }
7185
7186 IWL_DEBUG_MAC80211("leave\n");
Zhu Yib481de92007-09-25 17:54:57 -07007187
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07007188 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07007189}
7190
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007191static int iwl4965_mac_conf_tx(struct ieee80211_hw *hw, int queue,
Zhu Yib481de92007-09-25 17:54:57 -07007192 const struct ieee80211_tx_queue_params *params)
7193{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007194 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07007195 unsigned long flags;
7196 int q;
Zhu Yib481de92007-09-25 17:54:57 -07007197
7198 IWL_DEBUG_MAC80211("enter\n");
7199
Tomas Winklerfee12472008-04-03 16:05:21 -07007200 if (!iwl_is_ready_rf(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07007201 IWL_DEBUG_MAC80211("leave - RF not ready\n");
7202 return -EIO;
7203 }
7204
7205 if (queue >= AC_NUM) {
7206 IWL_DEBUG_MAC80211("leave - queue >= AC_NUM %d\n", queue);
7207 return 0;
7208 }
7209
Zhu Yib481de92007-09-25 17:54:57 -07007210 if (!priv->qos_data.qos_enable) {
7211 priv->qos_data.qos_active = 0;
7212 IWL_DEBUG_MAC80211("leave - qos not enabled\n");
7213 return 0;
7214 }
7215 q = AC_NUM - 1 - queue;
7216
7217 spin_lock_irqsave(&priv->lock, flags);
7218
7219 priv->qos_data.def_qos_parm.ac[q].cw_min = cpu_to_le16(params->cw_min);
7220 priv->qos_data.def_qos_parm.ac[q].cw_max = cpu_to_le16(params->cw_max);
7221 priv->qos_data.def_qos_parm.ac[q].aifsn = params->aifs;
7222 priv->qos_data.def_qos_parm.ac[q].edca_txop =
Johannes Berg3330d7b2008-02-10 16:49:38 +01007223 cpu_to_le16((params->txop * 32));
Zhu Yib481de92007-09-25 17:54:57 -07007224
7225 priv->qos_data.def_qos_parm.ac[q].reserved1 = 0;
7226 priv->qos_data.qos_active = 1;
7227
7228 spin_unlock_irqrestore(&priv->lock, flags);
7229
7230 mutex_lock(&priv->mutex);
7231 if (priv->iw_mode == IEEE80211_IF_TYPE_AP)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007232 iwl4965_activate_qos(priv, 1);
Tomas Winkler3109ece2008-03-28 16:33:35 -07007233 else if (priv->assoc_id && iwl_is_associated(priv))
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007234 iwl4965_activate_qos(priv, 0);
Zhu Yib481de92007-09-25 17:54:57 -07007235
7236 mutex_unlock(&priv->mutex);
7237
Zhu Yib481de92007-09-25 17:54:57 -07007238 IWL_DEBUG_MAC80211("leave\n");
7239 return 0;
7240}
7241
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007242static int iwl4965_mac_get_tx_stats(struct ieee80211_hw *hw,
Zhu Yib481de92007-09-25 17:54:57 -07007243 struct ieee80211_tx_queue_stats *stats)
7244{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007245 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07007246 int i, avail;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007247 struct iwl4965_tx_queue *txq;
7248 struct iwl4965_queue *q;
Zhu Yib481de92007-09-25 17:54:57 -07007249 unsigned long flags;
7250
7251 IWL_DEBUG_MAC80211("enter\n");
7252
Tomas Winklerfee12472008-04-03 16:05:21 -07007253 if (!iwl_is_ready_rf(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07007254 IWL_DEBUG_MAC80211("leave - RF not ready\n");
7255 return -EIO;
7256 }
7257
7258 spin_lock_irqsave(&priv->lock, flags);
7259
7260 for (i = 0; i < AC_NUM; i++) {
7261 txq = &priv->txq[i];
7262 q = &txq->q;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007263 avail = iwl4965_queue_space(q);
Zhu Yib481de92007-09-25 17:54:57 -07007264
7265 stats->data[i].len = q->n_window - avail;
7266 stats->data[i].limit = q->n_window - q->high_mark;
7267 stats->data[i].count = q->n_window;
7268
7269 }
7270 spin_unlock_irqrestore(&priv->lock, flags);
7271
7272 IWL_DEBUG_MAC80211("leave\n");
7273
7274 return 0;
7275}
7276
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007277static int iwl4965_mac_get_stats(struct ieee80211_hw *hw,
Zhu Yib481de92007-09-25 17:54:57 -07007278 struct ieee80211_low_level_stats *stats)
7279{
7280 IWL_DEBUG_MAC80211("enter\n");
7281 IWL_DEBUG_MAC80211("leave\n");
7282
7283 return 0;
7284}
7285
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007286static u64 iwl4965_mac_get_tsf(struct ieee80211_hw *hw)
Zhu Yib481de92007-09-25 17:54:57 -07007287{
7288 IWL_DEBUG_MAC80211("enter\n");
7289 IWL_DEBUG_MAC80211("leave\n");
7290
7291 return 0;
7292}
7293
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007294static void iwl4965_mac_reset_tsf(struct ieee80211_hw *hw)
Zhu Yib481de92007-09-25 17:54:57 -07007295{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007296 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07007297 unsigned long flags;
7298
7299 mutex_lock(&priv->mutex);
7300 IWL_DEBUG_MAC80211("enter\n");
7301
7302 priv->lq_mngr.lq_ready = 0;
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08007303#ifdef CONFIG_IWL4965_HT
Zhu Yib481de92007-09-25 17:54:57 -07007304 spin_lock_irqsave(&priv->lock, flags);
Ron Rindjunskyfd105e72007-11-26 16:14:39 +02007305 memset(&priv->current_ht_config, 0, sizeof(struct iwl_ht_info));
Zhu Yib481de92007-09-25 17:54:57 -07007306 spin_unlock_irqrestore(&priv->lock, flags);
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08007307#endif /* CONFIG_IWL4965_HT */
Zhu Yib481de92007-09-25 17:54:57 -07007308
Assaf Kraussbf85ea42008-03-14 10:38:49 -07007309 iwlcore_reset_qos(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007310
7311 cancel_delayed_work(&priv->post_associate);
7312
7313 spin_lock_irqsave(&priv->lock, flags);
7314 priv->assoc_id = 0;
7315 priv->assoc_capability = 0;
Zhu Yib481de92007-09-25 17:54:57 -07007316 priv->assoc_station_added = 0;
7317
7318 /* new association get rid of ibss beacon skb */
7319 if (priv->ibss_beacon)
7320 dev_kfree_skb(priv->ibss_beacon);
7321
7322 priv->ibss_beacon = NULL;
7323
7324 priv->beacon_int = priv->hw->conf.beacon_int;
Tomas Winkler3109ece2008-03-28 16:33:35 -07007325 priv->timestamp = 0;
Zhu Yib481de92007-09-25 17:54:57 -07007326 if ((priv->iw_mode == IEEE80211_IF_TYPE_STA))
7327 priv->beacon_int = 0;
7328
7329 spin_unlock_irqrestore(&priv->lock, flags);
7330
Tomas Winklerfee12472008-04-03 16:05:21 -07007331 if (!iwl_is_ready_rf(priv)) {
Mohamed Abbasfde35712007-11-29 11:10:15 +08007332 IWL_DEBUG_MAC80211("leave - not ready\n");
7333 mutex_unlock(&priv->mutex);
7334 return;
7335 }
7336
mabbas052c4b92007-10-25 17:15:43 +08007337 /* we are restarting association process
7338 * clear RXON_FILTER_ASSOC_MSK bit
7339 */
7340 if (priv->iw_mode != IEEE80211_IF_TYPE_AP) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007341 iwl4965_scan_cancel_timeout(priv, 100);
mabbas052c4b92007-10-25 17:15:43 +08007342 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007343 iwl4965_commit_rxon(priv);
mabbas052c4b92007-10-25 17:15:43 +08007344 }
7345
Zhu Yib481de92007-09-25 17:54:57 -07007346 /* Per mac80211.h: This is only used in IBSS mode... */
7347 if (priv->iw_mode != IEEE80211_IF_TYPE_IBSS) {
mabbas052c4b92007-10-25 17:15:43 +08007348
Zhu Yib481de92007-09-25 17:54:57 -07007349 IWL_DEBUG_MAC80211("leave - not in IBSS\n");
7350 mutex_unlock(&priv->mutex);
7351 return;
7352 }
7353
Zhu Yib481de92007-09-25 17:54:57 -07007354 priv->only_active_channel = 0;
7355
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007356 iwl4965_set_rate(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007357
7358 mutex_unlock(&priv->mutex);
7359
7360 IWL_DEBUG_MAC80211("leave\n");
Zhu Yib481de92007-09-25 17:54:57 -07007361}
7362
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007363static int iwl4965_mac_beacon_update(struct ieee80211_hw *hw, struct sk_buff *skb,
Zhu Yib481de92007-09-25 17:54:57 -07007364 struct ieee80211_tx_control *control)
7365{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007366 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07007367 unsigned long flags;
7368
7369 mutex_lock(&priv->mutex);
7370 IWL_DEBUG_MAC80211("enter\n");
7371
Tomas Winklerfee12472008-04-03 16:05:21 -07007372 if (!iwl_is_ready_rf(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07007373 IWL_DEBUG_MAC80211("leave - RF not ready\n");
7374 mutex_unlock(&priv->mutex);
7375 return -EIO;
7376 }
7377
7378 if (priv->iw_mode != IEEE80211_IF_TYPE_IBSS) {
7379 IWL_DEBUG_MAC80211("leave - not IBSS\n");
7380 mutex_unlock(&priv->mutex);
7381 return -EIO;
7382 }
7383
7384 spin_lock_irqsave(&priv->lock, flags);
7385
7386 if (priv->ibss_beacon)
7387 dev_kfree_skb(priv->ibss_beacon);
7388
7389 priv->ibss_beacon = skb;
7390
7391 priv->assoc_id = 0;
7392
7393 IWL_DEBUG_MAC80211("leave\n");
7394 spin_unlock_irqrestore(&priv->lock, flags);
7395
Assaf Kraussbf85ea42008-03-14 10:38:49 -07007396 iwlcore_reset_qos(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007397
7398 queue_work(priv->workqueue, &priv->post_associate.work);
7399
7400 mutex_unlock(&priv->mutex);
7401
7402 return 0;
7403}
7404
Zhu Yib481de92007-09-25 17:54:57 -07007405/*****************************************************************************
7406 *
7407 * sysfs attributes
7408 *
7409 *****************************************************************************/
7410
Tomas Winkler0a6857e2008-03-12 16:58:49 -07007411#ifdef CONFIG_IWLWIFI_DEBUG
Zhu Yib481de92007-09-25 17:54:57 -07007412
7413/*
7414 * The following adds a new attribute to the sysfs representation
7415 * of this device driver (i.e. a new file in /sys/bus/pci/drivers/iwl/)
7416 * used for controlling the debug level.
7417 *
7418 * See the level definitions in iwl for details.
7419 */
7420
7421static ssize_t show_debug_level(struct device_driver *d, char *buf)
7422{
Tomas Winkler0a6857e2008-03-12 16:58:49 -07007423 return sprintf(buf, "0x%08X\n", iwl_debug_level);
Zhu Yib481de92007-09-25 17:54:57 -07007424}
7425static ssize_t store_debug_level(struct device_driver *d,
7426 const char *buf, size_t count)
7427{
7428 char *p = (char *)buf;
7429 u32 val;
7430
7431 val = simple_strtoul(p, &p, 0);
7432 if (p == buf)
7433 printk(KERN_INFO DRV_NAME
7434 ": %s is not in hex or decimal form.\n", buf);
7435 else
Tomas Winkler0a6857e2008-03-12 16:58:49 -07007436 iwl_debug_level = val;
Zhu Yib481de92007-09-25 17:54:57 -07007437
7438 return strnlen(buf, count);
7439}
7440
7441static DRIVER_ATTR(debug_level, S_IWUSR | S_IRUGO,
7442 show_debug_level, store_debug_level);
7443
Tomas Winkler0a6857e2008-03-12 16:58:49 -07007444#endif /* CONFIG_IWLWIFI_DEBUG */
Zhu Yib481de92007-09-25 17:54:57 -07007445
Zhu Yib481de92007-09-25 17:54:57 -07007446
7447static ssize_t show_temperature(struct device *d,
7448 struct device_attribute *attr, char *buf)
7449{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007450 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07007451
Tomas Winklerfee12472008-04-03 16:05:21 -07007452 if (!iwl_is_alive(priv))
Zhu Yib481de92007-09-25 17:54:57 -07007453 return -EAGAIN;
7454
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007455 return sprintf(buf, "%d\n", iwl4965_hw_get_temperature(priv));
Zhu Yib481de92007-09-25 17:54:57 -07007456}
7457
7458static DEVICE_ATTR(temperature, S_IRUGO, show_temperature, NULL);
7459
7460static ssize_t show_rs_window(struct device *d,
7461 struct device_attribute *attr,
7462 char *buf)
7463{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007464 struct iwl_priv *priv = d->driver_data;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007465 return iwl4965_fill_rs_info(priv->hw, buf, IWL_AP_ID);
Zhu Yib481de92007-09-25 17:54:57 -07007466}
7467static DEVICE_ATTR(rs_window, S_IRUGO, show_rs_window, NULL);
7468
7469static ssize_t show_tx_power(struct device *d,
7470 struct device_attribute *attr, char *buf)
7471{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007472 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07007473 return sprintf(buf, "%d\n", priv->user_txpower_limit);
7474}
7475
7476static ssize_t store_tx_power(struct device *d,
7477 struct device_attribute *attr,
7478 const char *buf, size_t count)
7479{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007480 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07007481 char *p = (char *)buf;
7482 u32 val;
7483
7484 val = simple_strtoul(p, &p, 10);
7485 if (p == buf)
7486 printk(KERN_INFO DRV_NAME
7487 ": %s is not in decimal form.\n", buf);
7488 else
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007489 iwl4965_hw_reg_set_txpower(priv, val);
Zhu Yib481de92007-09-25 17:54:57 -07007490
7491 return count;
7492}
7493
7494static DEVICE_ATTR(tx_power, S_IWUSR | S_IRUGO, show_tx_power, store_tx_power);
7495
7496static ssize_t show_flags(struct device *d,
7497 struct device_attribute *attr, char *buf)
7498{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007499 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07007500
7501 return sprintf(buf, "0x%04X\n", priv->active_rxon.flags);
7502}
7503
7504static ssize_t store_flags(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 u32 flags = simple_strtoul(buf, NULL, 0);
7510
7511 mutex_lock(&priv->mutex);
7512 if (le32_to_cpu(priv->staging_rxon.flags) != flags) {
7513 /* Cancel any currently running scans... */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007514 if (iwl4965_scan_cancel_timeout(priv, 100))
Zhu Yib481de92007-09-25 17:54:57 -07007515 IWL_WARNING("Could not cancel scan.\n");
7516 else {
7517 IWL_DEBUG_INFO("Committing rxon.flags = 0x%04X\n",
7518 flags);
7519 priv->staging_rxon.flags = cpu_to_le32(flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007520 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007521 }
7522 }
7523 mutex_unlock(&priv->mutex);
7524
7525 return count;
7526}
7527
7528static DEVICE_ATTR(flags, S_IWUSR | S_IRUGO, show_flags, store_flags);
7529
7530static ssize_t show_filter_flags(struct device *d,
7531 struct device_attribute *attr, char *buf)
7532{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007533 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07007534
7535 return sprintf(buf, "0x%04X\n",
7536 le32_to_cpu(priv->active_rxon.filter_flags));
7537}
7538
7539static ssize_t store_filter_flags(struct device *d,
7540 struct device_attribute *attr,
7541 const char *buf, size_t count)
7542{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007543 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07007544 u32 filter_flags = simple_strtoul(buf, NULL, 0);
7545
7546 mutex_lock(&priv->mutex);
7547 if (le32_to_cpu(priv->staging_rxon.filter_flags) != filter_flags) {
7548 /* Cancel any currently running scans... */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007549 if (iwl4965_scan_cancel_timeout(priv, 100))
Zhu Yib481de92007-09-25 17:54:57 -07007550 IWL_WARNING("Could not cancel scan.\n");
7551 else {
7552 IWL_DEBUG_INFO("Committing rxon.filter_flags = "
7553 "0x%04X\n", filter_flags);
7554 priv->staging_rxon.filter_flags =
7555 cpu_to_le32(filter_flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007556 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007557 }
7558 }
7559 mutex_unlock(&priv->mutex);
7560
7561 return count;
7562}
7563
7564static DEVICE_ATTR(filter_flags, S_IWUSR | S_IRUGO, show_filter_flags,
7565 store_filter_flags);
7566
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08007567#ifdef CONFIG_IWL4965_SPECTRUM_MEASUREMENT
Zhu Yib481de92007-09-25 17:54:57 -07007568
7569static ssize_t show_measurement(struct device *d,
7570 struct device_attribute *attr, char *buf)
7571{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007572 struct iwl_priv *priv = dev_get_drvdata(d);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007573 struct iwl4965_spectrum_notification measure_report;
Zhu Yib481de92007-09-25 17:54:57 -07007574 u32 size = sizeof(measure_report), len = 0, ofs = 0;
7575 u8 *data = (u8 *) & measure_report;
7576 unsigned long flags;
7577
7578 spin_lock_irqsave(&priv->lock, flags);
7579 if (!(priv->measurement_status & MEASUREMENT_READY)) {
7580 spin_unlock_irqrestore(&priv->lock, flags);
7581 return 0;
7582 }
7583 memcpy(&measure_report, &priv->measure_report, size);
7584 priv->measurement_status = 0;
7585 spin_unlock_irqrestore(&priv->lock, flags);
7586
7587 while (size && (PAGE_SIZE - len)) {
7588 hex_dump_to_buffer(data + ofs, size, 16, 1, buf + len,
7589 PAGE_SIZE - len, 1);
7590 len = strlen(buf);
7591 if (PAGE_SIZE - len)
7592 buf[len++] = '\n';
7593
7594 ofs += 16;
7595 size -= min(size, 16U);
7596 }
7597
7598 return len;
7599}
7600
7601static ssize_t store_measurement(struct device *d,
7602 struct device_attribute *attr,
7603 const char *buf, size_t count)
7604{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007605 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07007606 struct ieee80211_measurement_params params = {
7607 .channel = le16_to_cpu(priv->active_rxon.channel),
7608 .start_time = cpu_to_le64(priv->last_tsf),
7609 .duration = cpu_to_le16(1),
7610 };
7611 u8 type = IWL_MEASURE_BASIC;
7612 u8 buffer[32];
7613 u8 channel;
7614
7615 if (count) {
7616 char *p = buffer;
7617 strncpy(buffer, buf, min(sizeof(buffer), count));
7618 channel = simple_strtoul(p, NULL, 0);
7619 if (channel)
7620 params.channel = channel;
7621
7622 p = buffer;
7623 while (*p && *p != ' ')
7624 p++;
7625 if (*p)
7626 type = simple_strtoul(p + 1, NULL, 0);
7627 }
7628
7629 IWL_DEBUG_INFO("Invoking measurement of type %d on "
7630 "channel %d (for '%s')\n", type, params.channel, buf);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007631 iwl4965_get_measurement(priv, &params, type);
Zhu Yib481de92007-09-25 17:54:57 -07007632
7633 return count;
7634}
7635
7636static DEVICE_ATTR(measurement, S_IRUSR | S_IWUSR,
7637 show_measurement, store_measurement);
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08007638#endif /* CONFIG_IWL4965_SPECTRUM_MEASUREMENT */
Zhu Yib481de92007-09-25 17:54:57 -07007639
7640static ssize_t store_retry_rate(struct device *d,
7641 struct device_attribute *attr,
7642 const char *buf, size_t count)
7643{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007644 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07007645
7646 priv->retry_rate = simple_strtoul(buf, NULL, 0);
7647 if (priv->retry_rate <= 0)
7648 priv->retry_rate = 1;
7649
7650 return count;
7651}
7652
7653static ssize_t show_retry_rate(struct device *d,
7654 struct device_attribute *attr, char *buf)
7655{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007656 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07007657 return sprintf(buf, "%d", priv->retry_rate);
7658}
7659
7660static DEVICE_ATTR(retry_rate, S_IWUSR | S_IRUSR, show_retry_rate,
7661 store_retry_rate);
7662
7663static ssize_t store_power_level(struct device *d,
7664 struct device_attribute *attr,
7665 const char *buf, size_t count)
7666{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007667 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07007668 int rc;
7669 int mode;
7670
7671 mode = simple_strtoul(buf, NULL, 0);
7672 mutex_lock(&priv->mutex);
7673
Tomas Winklerfee12472008-04-03 16:05:21 -07007674 if (!iwl_is_ready(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07007675 rc = -EAGAIN;
7676 goto out;
7677 }
7678
7679 if ((mode < 1) || (mode > IWL_POWER_LIMIT) || (mode == IWL_POWER_AC))
7680 mode = IWL_POWER_AC;
7681 else
7682 mode |= IWL_POWER_ENABLED;
7683
7684 if (mode != priv->power_mode) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007685 rc = iwl4965_send_power_mode(priv, IWL_POWER_LEVEL(mode));
Zhu Yib481de92007-09-25 17:54:57 -07007686 if (rc) {
7687 IWL_DEBUG_MAC80211("failed setting power mode.\n");
7688 goto out;
7689 }
7690 priv->power_mode = mode;
7691 }
7692
7693 rc = count;
7694
7695 out:
7696 mutex_unlock(&priv->mutex);
7697 return rc;
7698}
7699
7700#define MAX_WX_STRING 80
7701
7702/* Values are in microsecond */
7703static const s32 timeout_duration[] = {
7704 350000,
7705 250000,
7706 75000,
7707 37000,
7708 25000,
7709};
7710static const s32 period_duration[] = {
7711 400000,
7712 700000,
7713 1000000,
7714 1000000,
7715 1000000
7716};
7717
7718static ssize_t show_power_level(struct device *d,
7719 struct device_attribute *attr, char *buf)
7720{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007721 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07007722 int level = IWL_POWER_LEVEL(priv->power_mode);
7723 char *p = buf;
7724
7725 p += sprintf(p, "%d ", level);
7726 switch (level) {
7727 case IWL_POWER_MODE_CAM:
7728 case IWL_POWER_AC:
7729 p += sprintf(p, "(AC)");
7730 break;
7731 case IWL_POWER_BATTERY:
7732 p += sprintf(p, "(BATTERY)");
7733 break;
7734 default:
7735 p += sprintf(p,
7736 "(Timeout %dms, Period %dms)",
7737 timeout_duration[level - 1] / 1000,
7738 period_duration[level - 1] / 1000);
7739 }
7740
7741 if (!(priv->power_mode & IWL_POWER_ENABLED))
7742 p += sprintf(p, " OFF\n");
7743 else
7744 p += sprintf(p, " \n");
7745
7746 return (p - buf + 1);
7747
7748}
7749
7750static DEVICE_ATTR(power_level, S_IWUSR | S_IRUSR, show_power_level,
7751 store_power_level);
7752
7753static ssize_t show_channels(struct device *d,
7754 struct device_attribute *attr, char *buf)
7755{
Johannes Berg8318d782008-01-24 19:38:38 +01007756 /* all this shit doesn't belong into sysfs anyway */
7757 return 0;
Zhu Yib481de92007-09-25 17:54:57 -07007758}
7759
7760static DEVICE_ATTR(channels, S_IRUSR, show_channels, NULL);
7761
7762static ssize_t show_statistics(struct device *d,
7763 struct device_attribute *attr, char *buf)
7764{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007765 struct iwl_priv *priv = dev_get_drvdata(d);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007766 u32 size = sizeof(struct iwl4965_notif_statistics);
Zhu Yib481de92007-09-25 17:54:57 -07007767 u32 len = 0, ofs = 0;
7768 u8 *data = (u8 *) & priv->statistics;
7769 int rc = 0;
7770
Tomas Winklerfee12472008-04-03 16:05:21 -07007771 if (!iwl_is_alive(priv))
Zhu Yib481de92007-09-25 17:54:57 -07007772 return -EAGAIN;
7773
7774 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007775 rc = iwl4965_send_statistics_request(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007776 mutex_unlock(&priv->mutex);
7777
7778 if (rc) {
7779 len = sprintf(buf,
7780 "Error sending statistics request: 0x%08X\n", rc);
7781 return len;
7782 }
7783
7784 while (size && (PAGE_SIZE - len)) {
7785 hex_dump_to_buffer(data + ofs, size, 16, 1, buf + len,
7786 PAGE_SIZE - len, 1);
7787 len = strlen(buf);
7788 if (PAGE_SIZE - len)
7789 buf[len++] = '\n';
7790
7791 ofs += 16;
7792 size -= min(size, 16U);
7793 }
7794
7795 return len;
7796}
7797
7798static DEVICE_ATTR(statistics, S_IRUGO, show_statistics, NULL);
7799
7800static ssize_t show_antenna(struct device *d,
7801 struct device_attribute *attr, char *buf)
7802{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007803 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07007804
Tomas Winklerfee12472008-04-03 16:05:21 -07007805 if (!iwl_is_alive(priv))
Zhu Yib481de92007-09-25 17:54:57 -07007806 return -EAGAIN;
7807
7808 return sprintf(buf, "%d\n", priv->antenna);
7809}
7810
7811static ssize_t store_antenna(struct device *d,
7812 struct device_attribute *attr,
7813 const char *buf, size_t count)
7814{
7815 int ant;
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007816 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07007817
7818 if (count == 0)
7819 return 0;
7820
7821 if (sscanf(buf, "%1i", &ant) != 1) {
7822 IWL_DEBUG_INFO("not in hex or decimal form.\n");
7823 return count;
7824 }
7825
7826 if ((ant >= 0) && (ant <= 2)) {
7827 IWL_DEBUG_INFO("Setting antenna select to %d.\n", ant);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007828 priv->antenna = (enum iwl4965_antenna)ant;
Zhu Yib481de92007-09-25 17:54:57 -07007829 } else
7830 IWL_DEBUG_INFO("Bad antenna select value %d.\n", ant);
7831
7832
7833 return count;
7834}
7835
7836static DEVICE_ATTR(antenna, S_IWUSR | S_IRUGO, show_antenna, store_antenna);
7837
7838static ssize_t show_status(struct device *d,
7839 struct device_attribute *attr, char *buf)
7840{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007841 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Tomas Winklerfee12472008-04-03 16:05:21 -07007842 if (!iwl_is_alive(priv))
Zhu Yib481de92007-09-25 17:54:57 -07007843 return -EAGAIN;
7844 return sprintf(buf, "0x%08x\n", (int)priv->status);
7845}
7846
7847static DEVICE_ATTR(status, S_IRUGO, show_status, NULL);
7848
7849static ssize_t dump_error_log(struct device *d,
7850 struct device_attribute *attr,
7851 const char *buf, size_t count)
7852{
7853 char *p = (char *)buf;
7854
7855 if (p[0] == '1')
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007856 iwl4965_dump_nic_error_log((struct iwl_priv *)d->driver_data);
Zhu Yib481de92007-09-25 17:54:57 -07007857
7858 return strnlen(buf, count);
7859}
7860
7861static DEVICE_ATTR(dump_errors, S_IWUSR, NULL, dump_error_log);
7862
7863static ssize_t dump_event_log(struct device *d,
7864 struct device_attribute *attr,
7865 const char *buf, size_t count)
7866{
7867 char *p = (char *)buf;
7868
7869 if (p[0] == '1')
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007870 iwl4965_dump_nic_event_log((struct iwl_priv *)d->driver_data);
Zhu Yib481de92007-09-25 17:54:57 -07007871
7872 return strnlen(buf, count);
7873}
7874
7875static DEVICE_ATTR(dump_events, S_IWUSR, NULL, dump_event_log);
7876
7877/*****************************************************************************
7878 *
7879 * driver setup and teardown
7880 *
7881 *****************************************************************************/
7882
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007883static void iwl4965_setup_deferred_work(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07007884{
7885 priv->workqueue = create_workqueue(DRV_NAME);
7886
7887 init_waitqueue_head(&priv->wait_command_queue);
7888
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007889 INIT_WORK(&priv->up, iwl4965_bg_up);
7890 INIT_WORK(&priv->restart, iwl4965_bg_restart);
7891 INIT_WORK(&priv->rx_replenish, iwl4965_bg_rx_replenish);
7892 INIT_WORK(&priv->scan_completed, iwl4965_bg_scan_completed);
7893 INIT_WORK(&priv->request_scan, iwl4965_bg_request_scan);
7894 INIT_WORK(&priv->abort_scan, iwl4965_bg_abort_scan);
7895 INIT_WORK(&priv->rf_kill, iwl4965_bg_rf_kill);
7896 INIT_WORK(&priv->beacon_update, iwl4965_bg_beacon_update);
7897 INIT_DELAYED_WORK(&priv->post_associate, iwl4965_bg_post_associate);
7898 INIT_DELAYED_WORK(&priv->init_alive_start, iwl4965_bg_init_alive_start);
7899 INIT_DELAYED_WORK(&priv->alive_start, iwl4965_bg_alive_start);
7900 INIT_DELAYED_WORK(&priv->scan_check, iwl4965_bg_scan_check);
Zhu Yib481de92007-09-25 17:54:57 -07007901
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007902 iwl4965_hw_setup_deferred_work(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007903
7904 tasklet_init(&priv->irq_tasklet, (void (*)(unsigned long))
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007905 iwl4965_irq_tasklet, (unsigned long)priv);
Zhu Yib481de92007-09-25 17:54:57 -07007906}
7907
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007908static void iwl4965_cancel_deferred_work(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07007909{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007910 iwl4965_hw_cancel_deferred_work(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007911
Joonwoo Park3ae6a052007-11-29 10:43:16 +09007912 cancel_delayed_work_sync(&priv->init_alive_start);
Zhu Yib481de92007-09-25 17:54:57 -07007913 cancel_delayed_work(&priv->scan_check);
7914 cancel_delayed_work(&priv->alive_start);
7915 cancel_delayed_work(&priv->post_associate);
7916 cancel_work_sync(&priv->beacon_update);
7917}
7918
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007919static struct attribute *iwl4965_sysfs_entries[] = {
Zhu Yib481de92007-09-25 17:54:57 -07007920 &dev_attr_antenna.attr,
7921 &dev_attr_channels.attr,
7922 &dev_attr_dump_errors.attr,
7923 &dev_attr_dump_events.attr,
7924 &dev_attr_flags.attr,
7925 &dev_attr_filter_flags.attr,
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08007926#ifdef CONFIG_IWL4965_SPECTRUM_MEASUREMENT
Zhu Yib481de92007-09-25 17:54:57 -07007927 &dev_attr_measurement.attr,
7928#endif
7929 &dev_attr_power_level.attr,
7930 &dev_attr_retry_rate.attr,
Zhu Yib481de92007-09-25 17:54:57 -07007931 &dev_attr_rs_window.attr,
7932 &dev_attr_statistics.attr,
7933 &dev_attr_status.attr,
7934 &dev_attr_temperature.attr,
Zhu Yib481de92007-09-25 17:54:57 -07007935 &dev_attr_tx_power.attr,
7936
7937 NULL
7938};
7939
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007940static struct attribute_group iwl4965_attribute_group = {
Zhu Yib481de92007-09-25 17:54:57 -07007941 .name = NULL, /* put in device directory */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007942 .attrs = iwl4965_sysfs_entries,
Zhu Yib481de92007-09-25 17:54:57 -07007943};
7944
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007945static struct ieee80211_ops iwl4965_hw_ops = {
7946 .tx = iwl4965_mac_tx,
7947 .start = iwl4965_mac_start,
7948 .stop = iwl4965_mac_stop,
7949 .add_interface = iwl4965_mac_add_interface,
7950 .remove_interface = iwl4965_mac_remove_interface,
7951 .config = iwl4965_mac_config,
7952 .config_interface = iwl4965_mac_config_interface,
7953 .configure_filter = iwl4965_configure_filter,
7954 .set_key = iwl4965_mac_set_key,
Emmanuel Grumbachab885f82008-03-20 15:06:43 +02007955 .update_tkip_key = iwl4965_mac_update_tkip_key,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007956 .get_stats = iwl4965_mac_get_stats,
7957 .get_tx_stats = iwl4965_mac_get_tx_stats,
7958 .conf_tx = iwl4965_mac_conf_tx,
7959 .get_tsf = iwl4965_mac_get_tsf,
7960 .reset_tsf = iwl4965_mac_reset_tsf,
7961 .beacon_update = iwl4965_mac_beacon_update,
Johannes Berg471b3ef2007-12-28 14:32:58 +01007962 .bss_info_changed = iwl4965_bss_info_changed,
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08007963#ifdef CONFIG_IWL4965_HT
Ron Rindjunsky9ab46172007-12-25 17:00:38 +02007964 .ampdu_action = iwl4965_mac_ampdu_action,
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08007965#endif /* CONFIG_IWL4965_HT */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007966 .hw_scan = iwl4965_mac_hw_scan
Zhu Yib481de92007-09-25 17:54:57 -07007967};
7968
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007969static int iwl4965_pci_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
Zhu Yib481de92007-09-25 17:54:57 -07007970{
7971 int err = 0;
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007972 struct iwl_priv *priv;
Zhu Yib481de92007-09-25 17:54:57 -07007973 struct ieee80211_hw *hw;
Tomas Winkler82b9a122008-03-04 18:09:30 -08007974 struct iwl_cfg *cfg = (struct iwl_cfg *)(ent->driver_data);
Mohamed Abbas0359fac2008-03-28 16:21:08 -07007975 unsigned long flags;
Zhu Yi5a66926a2008-01-14 17:46:18 -08007976 DECLARE_MAC_BUF(mac);
Zhu Yib481de92007-09-25 17:54:57 -07007977
Assaf Krauss316c30d2008-03-14 10:38:46 -07007978 /************************
7979 * 1. Allocating HW data
7980 ************************/
7981
Cahill, Ben M6440adb2007-11-29 11:09:55 +08007982 /* Disabling hardware scan means that mac80211 will perform scans
7983 * "the hard way", rather than using device's scan. */
Assaf Krauss1ea87392008-03-18 14:57:50 -07007984 if (cfg->mod_params->disable_hw_scan) {
Zhu Yib481de92007-09-25 17:54:57 -07007985 IWL_DEBUG_INFO("Disabling hw_scan\n");
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007986 iwl4965_hw_ops.hw_scan = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07007987 }
7988
Assaf Krauss1d0a0822008-03-14 10:38:48 -07007989 hw = iwl_alloc_all(cfg, &iwl4965_hw_ops);
7990 if (!hw) {
Zhu Yib481de92007-09-25 17:54:57 -07007991 err = -ENOMEM;
7992 goto out;
7993 }
Assaf Krauss1d0a0822008-03-14 10:38:48 -07007994 priv = hw->priv;
7995 /* At this point both hw and priv are allocated. */
7996
Zhu Yib481de92007-09-25 17:54:57 -07007997 SET_IEEE80211_DEV(hw, &pdev->dev);
7998
7999 IWL_DEBUG_INFO("*** LOAD DRIVER ***\n");
Tomas Winkler82b9a122008-03-04 18:09:30 -08008000 priv->cfg = cfg;
Zhu Yib481de92007-09-25 17:54:57 -07008001 priv->pci_dev = pdev;
Assaf Krauss316c30d2008-03-14 10:38:46 -07008002
Tomas Winkler0a6857e2008-03-12 16:58:49 -07008003#ifdef CONFIG_IWLWIFI_DEBUG
Assaf Krauss1ea87392008-03-18 14:57:50 -07008004 iwl_debug_level = priv->cfg->mod_params->debug;
Zhu Yib481de92007-09-25 17:54:57 -07008005 atomic_set(&priv->restrict_refcnt, 0);
8006#endif
Zhu Yib481de92007-09-25 17:54:57 -07008007
Assaf Krauss316c30d2008-03-14 10:38:46 -07008008 /**************************
8009 * 2. Initializing PCI bus
8010 **************************/
8011 if (pci_enable_device(pdev)) {
8012 err = -ENODEV;
8013 goto out_ieee80211_free_hw;
8014 }
8015
8016 pci_set_master(pdev);
8017
8018 err = pci_set_dma_mask(pdev, DMA_32BIT_MASK);
8019 if (!err)
8020 err = pci_set_consistent_dma_mask(pdev, DMA_32BIT_MASK);
8021 if (err) {
8022 printk(KERN_WARNING DRV_NAME
8023 ": No suitable DMA available.\n");
8024 goto out_pci_disable_device;
8025 }
8026
8027 err = pci_request_regions(pdev, DRV_NAME);
8028 if (err)
8029 goto out_pci_disable_device;
8030
8031 pci_set_drvdata(pdev, priv);
8032
8033 /* We disable the RETRY_TIMEOUT register (0x41) to keep
8034 * PCI Tx retries from interfering with C3 CPU state */
8035 pci_write_config_byte(pdev, 0x41, 0x00);
8036
8037 /***********************
8038 * 3. Read REV register
8039 ***********************/
8040 priv->hw_base = pci_iomap(pdev, 0, 0);
8041 if (!priv->hw_base) {
8042 err = -ENODEV;
8043 goto out_pci_release_regions;
8044 }
8045
8046 IWL_DEBUG_INFO("pci_resource_len = 0x%08llx\n",
8047 (unsigned long long) pci_resource_len(pdev, 0));
8048 IWL_DEBUG_INFO("pci_resource_base = %p\n", priv->hw_base);
8049
8050 printk(KERN_INFO DRV_NAME
8051 ": Detected Intel Wireless WiFi Link %s\n", priv->cfg->name);
8052
8053 /*****************
8054 * 4. Read EEPROM
8055 *****************/
8056 /* nic init */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07008057 iwl_set_bit(priv, CSR_GIO_CHICKEN_BITS,
Assaf Krauss316c30d2008-03-14 10:38:46 -07008058 CSR_GIO_CHICKEN_BITS_REG_BIT_DIS_L0S_EXIT_TIMER);
8059
Tomas Winkler3395f6e2008-03-25 16:33:37 -07008060 iwl_set_bit(priv, CSR_GP_CNTRL, CSR_GP_CNTRL_REG_FLAG_INIT_DONE);
8061 err = iwl_poll_bit(priv, CSR_GP_CNTRL,
Assaf Krauss316c30d2008-03-14 10:38:46 -07008062 CSR_GP_CNTRL_REG_FLAG_MAC_CLOCK_READY,
8063 CSR_GP_CNTRL_REG_FLAG_MAC_CLOCK_READY, 25000);
8064 if (err < 0) {
8065 IWL_DEBUG_INFO("Failed to init the card\n");
8066 goto out_iounmap;
8067 }
8068 /* Read the EEPROM */
8069 err = iwl_eeprom_init(priv);
8070 if (err) {
8071 IWL_ERROR("Unable to init EEPROM\n");
8072 goto out_iounmap;
8073 }
8074 /* MAC Address location in EEPROM same for 3945/4965 */
8075 iwl_eeprom_get_mac(priv, priv->mac_addr);
8076 IWL_DEBUG_INFO("MAC address: %s\n", print_mac(mac, priv->mac_addr));
8077 SET_IEEE80211_PERM_ADDR(priv->hw, priv->mac_addr);
8078
8079 /************************
8080 * 5. Setup HW constants
8081 ************************/
8082 /* Device-specific setup */
8083 if (iwl4965_hw_set_hw_setting(priv)) {
8084 IWL_ERROR("failed to set hw settings\n");
8085 goto out_iounmap;
8086 }
8087
8088 /*******************
8089 * 6. Setup hw/priv
8090 *******************/
Zhu Yib481de92007-09-25 17:54:57 -07008091
Assaf Kraussbf85ea42008-03-14 10:38:49 -07008092 err = iwl_setup(priv);
8093 if (err)
Assaf Krauss316c30d2008-03-14 10:38:46 -07008094 goto out_unset_hw_settings;
Assaf Kraussbf85ea42008-03-14 10:38:49 -07008095 /* At this point both hw and priv are initialized. */
Assaf Krauss316c30d2008-03-14 10:38:46 -07008096
8097 /**********************************
8098 * 7. Initialize module parameters
8099 **********************************/
8100
8101 /* Disable radio (SW RF KILL) via parameter when loading driver */
Assaf Krauss1ea87392008-03-18 14:57:50 -07008102 if (priv->cfg->mod_params->disable) {
Assaf Krauss316c30d2008-03-14 10:38:46 -07008103 set_bit(STATUS_RF_KILL_SW, &priv->status);
8104 IWL_DEBUG_INFO("Radio disabled.\n");
8105 }
8106
Assaf Krauss1ea87392008-03-18 14:57:50 -07008107 if (priv->cfg->mod_params->enable_qos)
Assaf Krauss316c30d2008-03-14 10:38:46 -07008108 priv->qos_data.qos_enable = 1;
8109
8110 /********************
8111 * 8. Setup services
8112 ********************/
Mohamed Abbas0359fac2008-03-28 16:21:08 -07008113 spin_lock_irqsave(&priv->lock, flags);
Assaf Krauss316c30d2008-03-14 10:38:46 -07008114 iwl4965_disable_interrupts(priv);
Mohamed Abbas0359fac2008-03-28 16:21:08 -07008115 spin_unlock_irqrestore(&priv->lock, flags);
Assaf Krauss316c30d2008-03-14 10:38:46 -07008116
8117 err = sysfs_create_group(&pdev->dev.kobj, &iwl4965_attribute_group);
8118 if (err) {
8119 IWL_ERROR("failed to create sysfs device attributes\n");
Assaf Kraussbf85ea42008-03-14 10:38:49 -07008120 goto out_unset_hw_settings;
Assaf Krauss316c30d2008-03-14 10:38:46 -07008121 }
8122
8123 err = iwl_dbgfs_register(priv, DRV_NAME);
8124 if (err) {
8125 IWL_ERROR("failed to create debugfs files\n");
8126 goto out_remove_sysfs;
8127 }
8128
8129 iwl4965_setup_deferred_work(priv);
8130 iwl4965_setup_rx_handlers(priv);
8131
8132 /********************
8133 * 9. Conclude
8134 ********************/
Zhu Yi5a66926a2008-01-14 17:46:18 -08008135 pci_save_state(pdev);
8136 pci_disable_device(pdev);
Zhu Yib481de92007-09-25 17:54:57 -07008137
Mohamed Abbasc8381fd2008-03-28 16:21:05 -07008138 /* notify iwlcore to init */
8139 iwlcore_low_level_notify(priv, IWLCORE_INIT_EVT);
Zhu Yib481de92007-09-25 17:54:57 -07008140 return 0;
8141
Assaf Krauss316c30d2008-03-14 10:38:46 -07008142 out_remove_sysfs:
8143 sysfs_remove_group(&pdev->dev.kobj, &iwl4965_attribute_group);
Assaf Krauss316c30d2008-03-14 10:38:46 -07008144 out_unset_hw_settings:
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008145 iwl4965_unset_hw_setting(priv);
Zhu Yib481de92007-09-25 17:54:57 -07008146 out_iounmap:
8147 pci_iounmap(pdev, priv->hw_base);
8148 out_pci_release_regions:
8149 pci_release_regions(pdev);
Assaf Krauss316c30d2008-03-14 10:38:46 -07008150 pci_set_drvdata(pdev, NULL);
Zhu Yib481de92007-09-25 17:54:57 -07008151 out_pci_disable_device:
8152 pci_disable_device(pdev);
Zhu Yib481de92007-09-25 17:54:57 -07008153 out_ieee80211_free_hw:
8154 ieee80211_free_hw(priv->hw);
8155 out:
8156 return err;
8157}
8158
Reinette Chatrec83dbf62008-03-21 13:53:41 -07008159static void __devexit iwl4965_pci_remove(struct pci_dev *pdev)
Zhu Yib481de92007-09-25 17:54:57 -07008160{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07008161 struct iwl_priv *priv = pci_get_drvdata(pdev);
Zhu Yib481de92007-09-25 17:54:57 -07008162 struct list_head *p, *q;
8163 int i;
Mohamed Abbas0359fac2008-03-28 16:21:08 -07008164 unsigned long flags;
Zhu Yib481de92007-09-25 17:54:57 -07008165
8166 if (!priv)
8167 return;
8168
8169 IWL_DEBUG_INFO("*** UNLOAD DRIVER ***\n");
8170
Ron Rindjunskyc4f55232008-03-28 16:21:10 -07008171 if (priv->mac80211_registered) {
8172 ieee80211_unregister_hw(priv->hw);
8173 priv->mac80211_registered = 0;
8174 }
8175
Zhu Yib481de92007-09-25 17:54:57 -07008176 set_bit(STATUS_EXIT_PENDING, &priv->status);
Zhu Yib24d22b2007-12-19 13:59:52 +08008177
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008178 iwl4965_down(priv);
Zhu Yib481de92007-09-25 17:54:57 -07008179
Mohamed Abbas0359fac2008-03-28 16:21:08 -07008180 /* make sure we flush any pending irq or
8181 * tasklet for the driver
8182 */
8183 spin_lock_irqsave(&priv->lock, flags);
8184 iwl4965_disable_interrupts(priv);
8185 spin_unlock_irqrestore(&priv->lock, flags);
8186
8187 iwl_synchronize_irq(priv);
8188
Zhu Yib481de92007-09-25 17:54:57 -07008189 /* Free MAC hash list for ADHOC */
8190 for (i = 0; i < IWL_IBSS_MAC_HASH_SIZE; i++) {
8191 list_for_each_safe(p, q, &priv->ibss_mac_hash[i]) {
8192 list_del(p);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008193 kfree(list_entry(p, struct iwl4965_ibss_seq, list));
Zhu Yib481de92007-09-25 17:54:57 -07008194 }
8195 }
8196
Mohamed Abbasc8381fd2008-03-28 16:21:05 -07008197 iwlcore_low_level_notify(priv, IWLCORE_REMOVE_EVT);
Tomas Winkler712b6cf2008-03-12 16:58:52 -07008198 iwl_dbgfs_unregister(priv);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008199 sysfs_remove_group(&pdev->dev.kobj, &iwl4965_attribute_group);
Zhu Yib481de92007-09-25 17:54:57 -07008200
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008201 iwl4965_dealloc_ucode_pci(priv);
Zhu Yib481de92007-09-25 17:54:57 -07008202
8203 if (priv->rxq.bd)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008204 iwl4965_rx_queue_free(priv, &priv->rxq);
8205 iwl4965_hw_txq_ctx_free(priv);
Zhu Yib481de92007-09-25 17:54:57 -07008206
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008207 iwl4965_unset_hw_setting(priv);
Assaf Kraussbf85ea42008-03-14 10:38:49 -07008208 iwlcore_clear_stations_table(priv);
Zhu Yib481de92007-09-25 17:54:57 -07008209
Zhu Yib481de92007-09-25 17:54:57 -07008210
Mohamed Abbas948c1712007-10-25 17:15:45 +08008211 /*netif_stop_queue(dev); */
8212 flush_workqueue(priv->workqueue);
8213
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008214 /* ieee80211_unregister_hw calls iwl4965_mac_stop, which flushes
Zhu Yib481de92007-09-25 17:54:57 -07008215 * priv->workqueue... so we can't take down the workqueue
8216 * until now... */
8217 destroy_workqueue(priv->workqueue);
8218 priv->workqueue = NULL;
8219
Zhu Yib481de92007-09-25 17:54:57 -07008220 pci_iounmap(pdev, priv->hw_base);
8221 pci_release_regions(pdev);
8222 pci_disable_device(pdev);
8223 pci_set_drvdata(pdev, NULL);
8224
Assaf Kraussbf85ea42008-03-14 10:38:49 -07008225 iwl_free_channel_map(priv);
Reinette Chatre849e0dc2008-01-23 10:15:18 -08008226 iwl4965_free_geos(priv);
Zhu Yib481de92007-09-25 17:54:57 -07008227
8228 if (priv->ibss_beacon)
8229 dev_kfree_skb(priv->ibss_beacon);
8230
8231 ieee80211_free_hw(priv->hw);
8232}
8233
8234#ifdef CONFIG_PM
8235
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008236static int iwl4965_pci_suspend(struct pci_dev *pdev, pm_message_t state)
Zhu Yib481de92007-09-25 17:54:57 -07008237{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07008238 struct iwl_priv *priv = pci_get_drvdata(pdev);
Zhu Yib481de92007-09-25 17:54:57 -07008239
Zhu Yie655b9f2008-01-24 02:19:38 -08008240 if (priv->is_open) {
8241 set_bit(STATUS_IN_SUSPEND, &priv->status);
8242 iwl4965_mac_stop(priv->hw);
8243 priv->is_open = 1;
8244 }
Zhu Yib481de92007-09-25 17:54:57 -07008245
Zhu Yib481de92007-09-25 17:54:57 -07008246 pci_set_power_state(pdev, PCI_D3hot);
8247
Zhu Yib481de92007-09-25 17:54:57 -07008248 return 0;
8249}
8250
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008251static int iwl4965_pci_resume(struct pci_dev *pdev)
Zhu Yib481de92007-09-25 17:54:57 -07008252{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07008253 struct iwl_priv *priv = pci_get_drvdata(pdev);
Zhu Yib481de92007-09-25 17:54:57 -07008254
Zhu Yib481de92007-09-25 17:54:57 -07008255 pci_set_power_state(pdev, PCI_D0);
Zhu Yib481de92007-09-25 17:54:57 -07008256
Zhu Yie655b9f2008-01-24 02:19:38 -08008257 if (priv->is_open)
8258 iwl4965_mac_start(priv->hw);
Zhu Yib481de92007-09-25 17:54:57 -07008259
Zhu Yie655b9f2008-01-24 02:19:38 -08008260 clear_bit(STATUS_IN_SUSPEND, &priv->status);
Zhu Yib481de92007-09-25 17:54:57 -07008261 return 0;
8262}
8263
8264#endif /* CONFIG_PM */
8265
8266/*****************************************************************************
8267 *
8268 * driver and module entry point
8269 *
8270 *****************************************************************************/
8271
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008272static struct pci_driver iwl4965_driver = {
Zhu Yib481de92007-09-25 17:54:57 -07008273 .name = DRV_NAME,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008274 .id_table = iwl4965_hw_card_ids,
8275 .probe = iwl4965_pci_probe,
8276 .remove = __devexit_p(iwl4965_pci_remove),
Zhu Yib481de92007-09-25 17:54:57 -07008277#ifdef CONFIG_PM
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008278 .suspend = iwl4965_pci_suspend,
8279 .resume = iwl4965_pci_resume,
Zhu Yib481de92007-09-25 17:54:57 -07008280#endif
8281};
8282
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008283static int __init iwl4965_init(void)
Zhu Yib481de92007-09-25 17:54:57 -07008284{
8285
8286 int ret;
8287 printk(KERN_INFO DRV_NAME ": " DRV_DESCRIPTION ", " DRV_VERSION "\n");
8288 printk(KERN_INFO DRV_NAME ": " DRV_COPYRIGHT "\n");
Reinette Chatre897e1cf2008-03-28 16:21:09 -07008289
8290 ret = iwl4965_rate_control_register();
8291 if (ret) {
8292 IWL_ERROR("Unable to register rate control algorithm: %d\n", ret);
8293 return ret;
8294 }
8295
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008296 ret = pci_register_driver(&iwl4965_driver);
Zhu Yib481de92007-09-25 17:54:57 -07008297 if (ret) {
8298 IWL_ERROR("Unable to initialize PCI module\n");
Reinette Chatre897e1cf2008-03-28 16:21:09 -07008299 goto error_register;
Zhu Yib481de92007-09-25 17:54:57 -07008300 }
Tomas Winkler0a6857e2008-03-12 16:58:49 -07008301#ifdef CONFIG_IWLWIFI_DEBUG
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008302 ret = driver_create_file(&iwl4965_driver.driver, &driver_attr_debug_level);
Zhu Yib481de92007-09-25 17:54:57 -07008303 if (ret) {
8304 IWL_ERROR("Unable to create driver sysfs file\n");
Reinette Chatre897e1cf2008-03-28 16:21:09 -07008305 goto error_debug;
Zhu Yib481de92007-09-25 17:54:57 -07008306 }
8307#endif
8308
8309 return ret;
Reinette Chatre897e1cf2008-03-28 16:21:09 -07008310
8311#ifdef CONFIG_IWLWIFI_DEBUG
8312error_debug:
8313 pci_unregister_driver(&iwl4965_driver);
8314#endif
8315error_register:
8316 iwl4965_rate_control_unregister();
8317 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07008318}
8319
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008320static void __exit iwl4965_exit(void)
Zhu Yib481de92007-09-25 17:54:57 -07008321{
Tomas Winkler0a6857e2008-03-12 16:58:49 -07008322#ifdef CONFIG_IWLWIFI_DEBUG
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008323 driver_remove_file(&iwl4965_driver.driver, &driver_attr_debug_level);
Zhu Yib481de92007-09-25 17:54:57 -07008324#endif
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008325 pci_unregister_driver(&iwl4965_driver);
Reinette Chatre897e1cf2008-03-28 16:21:09 -07008326 iwl4965_rate_control_unregister();
Zhu Yib481de92007-09-25 17:54:57 -07008327}
8328
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008329module_exit(iwl4965_exit);
8330module_init(iwl4965_init);