blob: bfefb05e18e938b14a41c01678cb6cf80accfabd [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"
Emmanuel Grumbach6974e362008-04-14 21:16:06 -070053#include "iwl-sta.h"
Zhu Yib481de92007-09-25 17:54:57 -070054
Tomas Winklerc79dd5b2008-03-12 16:58:50 -070055static int iwl4965_tx_queue_update_write_ptr(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -080056 struct iwl4965_tx_queue *txq);
Christoph Hellwig416e1432007-10-25 17:15:49 +080057
Zhu Yib481de92007-09-25 17:54:57 -070058/******************************************************************************
59 *
60 * module boiler plate
61 *
62 ******************************************************************************/
63
Zhu Yib481de92007-09-25 17:54:57 -070064/*
65 * module name, copyright, version, etc.
66 * NOTE: DRV_NAME is defined in iwlwifi.h for use by iwl-debug.h and printk
67 */
68
69#define DRV_DESCRIPTION "Intel(R) Wireless WiFi Link 4965AGN driver for Linux"
70
Tomas Winkler0a6857e2008-03-12 16:58:49 -070071#ifdef CONFIG_IWLWIFI_DEBUG
Zhu Yib481de92007-09-25 17:54:57 -070072#define VD "d"
73#else
74#define VD
75#endif
76
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +080077#ifdef CONFIG_IWL4965_SPECTRUM_MEASUREMENT
Zhu Yib481de92007-09-25 17:54:57 -070078#define VS "s"
79#else
80#define VS
81#endif
82
Tomas Winklerdf48c322008-03-06 10:40:19 -080083#define DRV_VERSION IWLWIFI_VERSION VD VS
Zhu Yib481de92007-09-25 17:54:57 -070084
Zhu Yib481de92007-09-25 17:54:57 -070085
86MODULE_DESCRIPTION(DRV_DESCRIPTION);
87MODULE_VERSION(DRV_VERSION);
88MODULE_AUTHOR(DRV_COPYRIGHT);
89MODULE_LICENSE("GPL");
90
91__le16 *ieee80211_get_qos_ctrl(struct ieee80211_hdr *hdr)
92{
93 u16 fc = le16_to_cpu(hdr->frame_control);
94 int hdr_len = ieee80211_get_hdrlen(fc);
95
96 if ((fc & 0x00cc) == (IEEE80211_STYPE_QOS_DATA | IEEE80211_FTYPE_DATA))
97 return (__le16 *) ((u8 *) hdr + hdr_len - QOS_CONTROL_LEN);
98 return NULL;
99}
100
Johannes Berg8318d782008-01-24 19:38:38 +0100101static const struct ieee80211_supported_band *iwl4965_get_hw_mode(
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700102 struct iwl_priv *priv, enum ieee80211_band band)
Zhu Yib481de92007-09-25 17:54:57 -0700103{
Johannes Berg8318d782008-01-24 19:38:38 +0100104 return priv->hw->wiphy->bands[band];
Zhu Yib481de92007-09-25 17:54:57 -0700105}
106
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800107static int iwl4965_is_empty_essid(const char *essid, int essid_len)
Zhu Yib481de92007-09-25 17:54:57 -0700108{
109 /* Single white space is for Linksys APs */
110 if (essid_len == 1 && essid[0] == ' ')
111 return 1;
112
113 /* Otherwise, if the entire essid is 0, we assume it is hidden */
114 while (essid_len) {
115 essid_len--;
116 if (essid[essid_len] != '\0')
117 return 0;
118 }
119
120 return 1;
121}
122
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800123static const char *iwl4965_escape_essid(const char *essid, u8 essid_len)
Zhu Yib481de92007-09-25 17:54:57 -0700124{
125 static char escaped[IW_ESSID_MAX_SIZE * 2 + 1];
126 const char *s = essid;
127 char *d = escaped;
128
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800129 if (iwl4965_is_empty_essid(essid, essid_len)) {
Zhu Yib481de92007-09-25 17:54:57 -0700130 memcpy(escaped, "<hidden>", sizeof("<hidden>"));
131 return escaped;
132 }
133
134 essid_len = min(essid_len, (u8) IW_ESSID_MAX_SIZE);
135 while (essid_len--) {
136 if (*s == '\0') {
137 *d++ = '\\';
138 *d++ = '0';
139 s++;
140 } else
141 *d++ = *s++;
142 }
143 *d = '\0';
144 return escaped;
145}
146
Zhu Yib481de92007-09-25 17:54:57 -0700147/*************** DMA-QUEUE-GENERAL-FUNCTIONS *****
148 * DMA services
149 *
150 * Theory of operation
151 *
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800152 * A Tx or Rx queue resides in host DRAM, and is comprised of a circular buffer
153 * of buffer descriptors, each of which points to one or more data buffers for
154 * the device to read from or fill. Driver and device exchange status of each
155 * queue via "read" and "write" pointers. Driver keeps minimum of 2 empty
156 * entries in each circular buffer, to protect against confusing empty and full
157 * queue states.
158 *
159 * The device reads or writes the data in the queues via the device's several
160 * DMA/FIFO channels. Each queue is mapped to a single DMA channel.
Zhu Yib481de92007-09-25 17:54:57 -0700161 *
162 * For Tx queue, there are low mark and high mark limits. If, after queuing
163 * the packet for Tx, free space become < low mark, Tx queue stopped. When
164 * reclaiming packets (on 'tx done IRQ), if free space become > high mark,
165 * Tx queue resumed.
166 *
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800167 * The 4965 operates with up to 17 queues: One receive queue, one transmit
168 * queue (#4) for sending commands to the device firmware, and 15 other
169 * Tx queues that may be mapped to prioritized Tx DMA/FIFO channels.
Ben Cahille3851442007-11-29 11:10:07 +0800170 *
171 * See more detailed info in iwl-4965-hw.h.
Zhu Yib481de92007-09-25 17:54:57 -0700172 ***************************************************/
173
Ron Rindjunskyfe01b472008-01-28 14:07:24 +0200174int iwl4965_queue_space(const struct iwl4965_queue *q)
Zhu Yib481de92007-09-25 17:54:57 -0700175{
Tomas Winklerfc4b6852007-10-25 17:15:24 +0800176 int s = q->read_ptr - q->write_ptr;
Zhu Yib481de92007-09-25 17:54:57 -0700177
Tomas Winklerfc4b6852007-10-25 17:15:24 +0800178 if (q->read_ptr > q->write_ptr)
Zhu Yib481de92007-09-25 17:54:57 -0700179 s -= q->n_bd;
180
181 if (s <= 0)
182 s += q->n_window;
183 /* keep some reserve to not confuse empty and full situations */
184 s -= 2;
185 if (s < 0)
186 s = 0;
187 return s;
188}
189
Zhu Yib481de92007-09-25 17:54:57 -0700190
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800191static inline int x2_queue_used(const struct iwl4965_queue *q, int i)
Zhu Yib481de92007-09-25 17:54:57 -0700192{
Tomas Winklerfc4b6852007-10-25 17:15:24 +0800193 return q->write_ptr > q->read_ptr ?
194 (i >= q->read_ptr && i < q->write_ptr) :
195 !(i < q->read_ptr && i >= q->write_ptr);
Zhu Yib481de92007-09-25 17:54:57 -0700196}
197
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800198static inline u8 get_cmd_index(struct iwl4965_queue *q, u32 index, int is_huge)
Zhu Yib481de92007-09-25 17:54:57 -0700199{
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800200 /* This is for scan command, the big buffer at end of command array */
Zhu Yib481de92007-09-25 17:54:57 -0700201 if (is_huge)
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800202 return q->n_window; /* must be power of 2 */
Zhu Yib481de92007-09-25 17:54:57 -0700203
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800204 /* Otherwise, use normal size buffers */
Zhu Yib481de92007-09-25 17:54:57 -0700205 return index & (q->n_window - 1);
206}
207
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800208/**
209 * iwl4965_queue_init - Initialize queue's high/low-water and read/write indexes
210 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700211static int iwl4965_queue_init(struct iwl_priv *priv, struct iwl4965_queue *q,
Zhu Yib481de92007-09-25 17:54:57 -0700212 int count, int slots_num, u32 id)
213{
214 q->n_bd = count;
215 q->n_window = slots_num;
216 q->id = id;
217
Tomas Winklerc54b6792008-03-06 17:36:53 -0800218 /* count must be power-of-two size, otherwise iwl_queue_inc_wrap
219 * and iwl_queue_dec_wrap are broken. */
Zhu Yib481de92007-09-25 17:54:57 -0700220 BUG_ON(!is_power_of_2(count));
221
222 /* slots_num must be power-of-two size, otherwise
223 * get_cmd_index is broken. */
224 BUG_ON(!is_power_of_2(slots_num));
225
226 q->low_mark = q->n_window / 4;
227 if (q->low_mark < 4)
228 q->low_mark = 4;
229
230 q->high_mark = q->n_window / 8;
231 if (q->high_mark < 2)
232 q->high_mark = 2;
233
Tomas Winklerfc4b6852007-10-25 17:15:24 +0800234 q->write_ptr = q->read_ptr = 0;
Zhu Yib481de92007-09-25 17:54:57 -0700235
236 return 0;
237}
238
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800239/**
240 * iwl4965_tx_queue_alloc - Alloc driver data and TFD CB for one Tx/cmd queue
241 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700242static int iwl4965_tx_queue_alloc(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800243 struct iwl4965_tx_queue *txq, u32 id)
Zhu Yib481de92007-09-25 17:54:57 -0700244{
245 struct pci_dev *dev = priv->pci_dev;
246
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800247 /* Driver private data, only for Tx (not command) queues,
248 * not shared with device. */
Zhu Yib481de92007-09-25 17:54:57 -0700249 if (id != IWL_CMD_QUEUE_NUM) {
250 txq->txb = kmalloc(sizeof(txq->txb[0]) *
251 TFD_QUEUE_SIZE_MAX, GFP_KERNEL);
252 if (!txq->txb) {
Ian Schram01ebd062007-10-25 17:15:22 +0800253 IWL_ERROR("kmalloc for auxiliary BD "
Zhu Yib481de92007-09-25 17:54:57 -0700254 "structures failed\n");
255 goto error;
256 }
257 } else
258 txq->txb = NULL;
259
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800260 /* Circular buffer of transmit frame descriptors (TFDs),
261 * shared with device */
Zhu Yib481de92007-09-25 17:54:57 -0700262 txq->bd = pci_alloc_consistent(dev,
263 sizeof(txq->bd[0]) * TFD_QUEUE_SIZE_MAX,
264 &txq->q.dma_addr);
265
266 if (!txq->bd) {
267 IWL_ERROR("pci_alloc_consistent(%zd) failed\n",
268 sizeof(txq->bd[0]) * TFD_QUEUE_SIZE_MAX);
269 goto error;
270 }
271 txq->q.id = id;
272
273 return 0;
274
275 error:
276 if (txq->txb) {
277 kfree(txq->txb);
278 txq->txb = NULL;
279 }
280
281 return -ENOMEM;
282}
283
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +0800284/**
285 * iwl4965_tx_queue_init - Allocate and initialize one tx/cmd queue
286 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700287int iwl4965_tx_queue_init(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800288 struct iwl4965_tx_queue *txq, int slots_num, u32 txq_id)
Zhu Yib481de92007-09-25 17:54:57 -0700289{
290 struct pci_dev *dev = priv->pci_dev;
291 int len;
292 int rc = 0;
293
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +0800294 /*
295 * Alloc buffer array for commands (Tx or other types of commands).
296 * For the command queue (#4), allocate command space + one big
297 * command for scan, since scan command is very huge; the system will
298 * not have two scans at the same time, so only one is needed.
Tomas Winklerbb542442007-12-05 20:59:59 +0200299 * For normal Tx queues (all other queues), no super-size command
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +0800300 * space is needed.
301 */
Tomas Winkler857485c2008-03-21 13:53:44 -0700302 len = sizeof(struct iwl_cmd) * slots_num;
Zhu Yib481de92007-09-25 17:54:57 -0700303 if (txq_id == IWL_CMD_QUEUE_NUM)
304 len += IWL_MAX_SCAN_SIZE;
305 txq->cmd = pci_alloc_consistent(dev, len, &txq->dma_addr_cmd);
306 if (!txq->cmd)
307 return -ENOMEM;
308
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +0800309 /* Alloc driver data array and TFD circular buffer */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800310 rc = iwl4965_tx_queue_alloc(priv, txq, txq_id);
Zhu Yib481de92007-09-25 17:54:57 -0700311 if (rc) {
312 pci_free_consistent(dev, len, txq->cmd, txq->dma_addr_cmd);
313
314 return -ENOMEM;
315 }
316 txq->need_update = 0;
317
318 /* TFD_QUEUE_SIZE_MAX must be power-of-two size, otherwise
Tomas Winklerc54b6792008-03-06 17:36:53 -0800319 * iwl_queue_inc_wrap and iwl_queue_dec_wrap are broken. */
Zhu Yib481de92007-09-25 17:54:57 -0700320 BUILD_BUG_ON(TFD_QUEUE_SIZE_MAX & (TFD_QUEUE_SIZE_MAX - 1));
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +0800321
322 /* Initialize queue's high/low-water marks, and head/tail indexes */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800323 iwl4965_queue_init(priv, &txq->q, TFD_QUEUE_SIZE_MAX, slots_num, txq_id);
Zhu Yib481de92007-09-25 17:54:57 -0700324
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +0800325 /* Tell device where to find queue */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800326 iwl4965_hw_tx_queue_init(priv, txq);
Zhu Yib481de92007-09-25 17:54:57 -0700327
328 return 0;
329}
330
331/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800332 * iwl4965_tx_queue_free - Deallocate DMA queue.
Zhu Yib481de92007-09-25 17:54:57 -0700333 * @txq: Transmit queue to deallocate.
334 *
335 * Empty queue by removing and destroying all BD's.
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800336 * Free all buffers.
337 * 0-fill, but do not free "txq" descriptor structure.
Zhu Yib481de92007-09-25 17:54:57 -0700338 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700339void iwl4965_tx_queue_free(struct iwl_priv *priv, struct iwl4965_tx_queue *txq)
Zhu Yib481de92007-09-25 17:54:57 -0700340{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800341 struct iwl4965_queue *q = &txq->q;
Zhu Yib481de92007-09-25 17:54:57 -0700342 struct pci_dev *dev = priv->pci_dev;
343 int len;
344
345 if (q->n_bd == 0)
346 return;
347
348 /* first, empty all BD's */
Tomas Winklerfc4b6852007-10-25 17:15:24 +0800349 for (; q->write_ptr != q->read_ptr;
Tomas Winklerc54b6792008-03-06 17:36:53 -0800350 q->read_ptr = iwl_queue_inc_wrap(q->read_ptr, q->n_bd))
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800351 iwl4965_hw_txq_free_tfd(priv, txq);
Zhu Yib481de92007-09-25 17:54:57 -0700352
Tomas Winkler857485c2008-03-21 13:53:44 -0700353 len = sizeof(struct iwl_cmd) * q->n_window;
Zhu Yib481de92007-09-25 17:54:57 -0700354 if (q->id == IWL_CMD_QUEUE_NUM)
355 len += IWL_MAX_SCAN_SIZE;
356
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800357 /* De-alloc array of command/tx buffers */
Zhu Yib481de92007-09-25 17:54:57 -0700358 pci_free_consistent(dev, len, txq->cmd, txq->dma_addr_cmd);
359
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800360 /* De-alloc circular buffer of TFDs */
Zhu Yib481de92007-09-25 17:54:57 -0700361 if (txq->q.n_bd)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800362 pci_free_consistent(dev, sizeof(struct iwl4965_tfd_frame) *
Zhu Yib481de92007-09-25 17:54:57 -0700363 txq->q.n_bd, txq->bd, txq->q.dma_addr);
364
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800365 /* De-alloc array of per-TFD driver data */
Zhu Yib481de92007-09-25 17:54:57 -0700366 if (txq->txb) {
367 kfree(txq->txb);
368 txq->txb = NULL;
369 }
370
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800371 /* 0-fill queue descriptor structure */
Zhu Yib481de92007-09-25 17:54:57 -0700372 memset(txq, 0, sizeof(*txq));
373}
374
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800375const u8 iwl4965_broadcast_addr[ETH_ALEN] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
Zhu Yib481de92007-09-25 17:54:57 -0700376
377/*************** STATION TABLE MANAGEMENT ****
Ben Cahill9fbab512007-11-29 11:09:47 +0800378 * mac80211 should be examined to determine if sta_info is duplicating
Zhu Yib481de92007-09-25 17:54:57 -0700379 * the functionality provided here
380 */
381
382/**************************************************************/
383
Ian Schram01ebd062007-10-25 17:15:22 +0800384#if 0 /* temporary disable till we add real remove station */
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800385/**
386 * iwl4965_remove_station - Remove driver's knowledge of station.
387 *
388 * NOTE: This does not remove station from device's station table.
389 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700390static u8 iwl4965_remove_station(struct iwl_priv *priv, const u8 *addr, int is_ap)
Zhu Yib481de92007-09-25 17:54:57 -0700391{
392 int index = IWL_INVALID_STATION;
393 int i;
394 unsigned long flags;
395
396 spin_lock_irqsave(&priv->sta_lock, flags);
397
398 if (is_ap)
399 index = IWL_AP_ID;
400 else if (is_broadcast_ether_addr(addr))
401 index = priv->hw_setting.bcast_sta_id;
402 else
403 for (i = IWL_STA_ID; i < priv->hw_setting.max_stations; i++)
404 if (priv->stations[i].used &&
405 !compare_ether_addr(priv->stations[i].sta.sta.addr,
406 addr)) {
407 index = i;
408 break;
409 }
410
411 if (unlikely(index == IWL_INVALID_STATION))
412 goto out;
413
414 if (priv->stations[index].used) {
415 priv->stations[index].used = 0;
416 priv->num_stations--;
417 }
418
419 BUG_ON(priv->num_stations < 0);
420
421out:
422 spin_unlock_irqrestore(&priv->sta_lock, flags);
423 return 0;
424}
Zhu Yi556f8db2007-09-27 11:27:33 +0800425#endif
Zhu Yib481de92007-09-25 17:54:57 -0700426
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800427/**
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800428 * iwl4965_add_station_flags - Add station to tables in driver and device
429 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700430u8 iwl4965_add_station_flags(struct iwl_priv *priv, const u8 *addr,
Ron Rindjunsky67d62032007-11-26 16:14:40 +0200431 int is_ap, u8 flags, void *ht_data)
Zhu Yib481de92007-09-25 17:54:57 -0700432{
433 int i;
434 int index = IWL_INVALID_STATION;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800435 struct iwl4965_station_entry *station;
Zhu Yib481de92007-09-25 17:54:57 -0700436 unsigned long flags_spin;
Joe Perches0795af52007-10-03 17:59:30 -0700437 DECLARE_MAC_BUF(mac);
Zhu Yib481de92007-09-25 17:54:57 -0700438
439 spin_lock_irqsave(&priv->sta_lock, flags_spin);
440 if (is_ap)
441 index = IWL_AP_ID;
442 else if (is_broadcast_ether_addr(addr))
443 index = priv->hw_setting.bcast_sta_id;
444 else
445 for (i = IWL_STA_ID; i < priv->hw_setting.max_stations; i++) {
446 if (!compare_ether_addr(priv->stations[i].sta.sta.addr,
447 addr)) {
448 index = i;
449 break;
450 }
451
452 if (!priv->stations[i].used &&
453 index == IWL_INVALID_STATION)
454 index = i;
455 }
456
457
Ben Cahill9fbab512007-11-29 11:09:47 +0800458 /* These two conditions have the same outcome, but keep them separate
459 since they have different meanings */
Zhu Yib481de92007-09-25 17:54:57 -0700460 if (unlikely(index == IWL_INVALID_STATION)) {
461 spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
462 return index;
463 }
464
465 if (priv->stations[index].used &&
466 !compare_ether_addr(priv->stations[index].sta.sta.addr, addr)) {
467 spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
468 return index;
469 }
470
471
Joe Perches0795af52007-10-03 17:59:30 -0700472 IWL_DEBUG_ASSOC("Add STA ID %d: %s\n", index, print_mac(mac, addr));
Zhu Yib481de92007-09-25 17:54:57 -0700473 station = &priv->stations[index];
474 station->used = 1;
475 priv->num_stations++;
476
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800477 /* Set up the REPLY_ADD_STA command to send to device */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800478 memset(&station->sta, 0, sizeof(struct iwl4965_addsta_cmd));
Zhu Yib481de92007-09-25 17:54:57 -0700479 memcpy(station->sta.sta.addr, addr, ETH_ALEN);
480 station->sta.mode = 0;
481 station->sta.sta.sta_id = index;
482 station->sta.station_flags = 0;
483
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +0800484#ifdef CONFIG_IWL4965_HT
Zhu Yib481de92007-09-25 17:54:57 -0700485 /* BCAST station and IBSS stations do not work in HT mode */
486 if (index != priv->hw_setting.bcast_sta_id &&
487 priv->iw_mode != IEEE80211_IF_TYPE_IBSS)
Ron Rindjunsky67d62032007-11-26 16:14:40 +0200488 iwl4965_set_ht_add_station(priv, index,
489 (struct ieee80211_ht_info *) ht_data);
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +0800490#endif /*CONFIG_IWL4965_HT*/
Zhu Yib481de92007-09-25 17:54:57 -0700491
492 spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800493
494 /* Add station to device's station table */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800495 iwl4965_send_add_station(priv, &station->sta, flags);
Zhu Yib481de92007-09-25 17:54:57 -0700496 return index;
497
498}
499
Zhu Yib481de92007-09-25 17:54:57 -0700500
Zhu Yib481de92007-09-25 17:54:57 -0700501
502/*************** HOST COMMAND QUEUE FUNCTIONS *****/
503
Zhu Yib481de92007-09-25 17:54:57 -0700504/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800505 * iwl4965_enqueue_hcmd - enqueue a uCode command
Zhu Yib481de92007-09-25 17:54:57 -0700506 * @priv: device private data point
507 * @cmd: a point to the ucode command structure
508 *
509 * The function returns < 0 values to indicate the operation is
510 * failed. On success, it turns the index (> 0) of command in the
511 * command queue.
512 */
Tomas Winkler857485c2008-03-21 13:53:44 -0700513int iwl4965_enqueue_hcmd(struct iwl_priv *priv, struct iwl_host_cmd *cmd)
Zhu Yib481de92007-09-25 17:54:57 -0700514{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800515 struct iwl4965_tx_queue *txq = &priv->txq[IWL_CMD_QUEUE_NUM];
516 struct iwl4965_queue *q = &txq->q;
517 struct iwl4965_tfd_frame *tfd;
Zhu Yib481de92007-09-25 17:54:57 -0700518 u32 *control_flags;
Tomas Winkler857485c2008-03-21 13:53:44 -0700519 struct iwl_cmd *out_cmd;
Zhu Yib481de92007-09-25 17:54:57 -0700520 u32 idx;
521 u16 fix_size = (u16)(cmd->len + sizeof(out_cmd->hdr));
522 dma_addr_t phys_addr;
523 int ret;
524 unsigned long flags;
525
526 /* If any of the command structures end up being larger than
527 * the TFD_MAX_PAYLOAD_SIZE, and it sent as a 'small' command then
528 * we will need to increase the size of the TFD entries */
529 BUG_ON((fix_size > TFD_MAX_PAYLOAD_SIZE) &&
530 !(cmd->meta.flags & CMD_SIZE_HUGE));
531
Tomas Winklerfee12472008-04-03 16:05:21 -0700532 if (iwl_is_rfkill(priv)) {
Gregory Greenmanc342a1b2008-02-06 11:20:40 -0800533 IWL_DEBUG_INFO("Not sending command - RF KILL");
534 return -EIO;
535 }
536
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800537 if (iwl4965_queue_space(q) < ((cmd->meta.flags & CMD_ASYNC) ? 2 : 1)) {
Zhu Yib481de92007-09-25 17:54:57 -0700538 IWL_ERROR("No space for Tx\n");
539 return -ENOSPC;
540 }
541
542 spin_lock_irqsave(&priv->hcmd_lock, flags);
543
Tomas Winklerfc4b6852007-10-25 17:15:24 +0800544 tfd = &txq->bd[q->write_ptr];
Zhu Yib481de92007-09-25 17:54:57 -0700545 memset(tfd, 0, sizeof(*tfd));
546
547 control_flags = (u32 *) tfd;
548
Tomas Winklerfc4b6852007-10-25 17:15:24 +0800549 idx = get_cmd_index(q, q->write_ptr, cmd->meta.flags & CMD_SIZE_HUGE);
Zhu Yib481de92007-09-25 17:54:57 -0700550 out_cmd = &txq->cmd[idx];
551
552 out_cmd->hdr.cmd = cmd->id;
553 memcpy(&out_cmd->meta, &cmd->meta, sizeof(cmd->meta));
554 memcpy(&out_cmd->cmd.payload, cmd->data, cmd->len);
555
556 /* At this point, the out_cmd now has all of the incoming cmd
557 * information */
558
559 out_cmd->hdr.flags = 0;
560 out_cmd->hdr.sequence = cpu_to_le16(QUEUE_TO_SEQ(IWL_CMD_QUEUE_NUM) |
Tomas Winklerfc4b6852007-10-25 17:15:24 +0800561 INDEX_TO_SEQ(q->write_ptr));
Zhu Yib481de92007-09-25 17:54:57 -0700562 if (out_cmd->meta.flags & CMD_SIZE_HUGE)
563 out_cmd->hdr.sequence |= cpu_to_le16(SEQ_HUGE_FRAME);
564
565 phys_addr = txq->dma_addr_cmd + sizeof(txq->cmd[0]) * idx +
Tomas Winkler857485c2008-03-21 13:53:44 -0700566 offsetof(struct iwl_cmd, hdr);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800567 iwl4965_hw_txq_attach_buf_to_tfd(priv, tfd, phys_addr, fix_size);
Zhu Yib481de92007-09-25 17:54:57 -0700568
569 IWL_DEBUG_HC("Sending command %s (#%x), seq: 0x%04X, "
570 "%d bytes at %d[%d]:%d\n",
571 get_cmd_string(out_cmd->hdr.cmd),
572 out_cmd->hdr.cmd, le16_to_cpu(out_cmd->hdr.sequence),
Tomas Winklerfc4b6852007-10-25 17:15:24 +0800573 fix_size, q->write_ptr, idx, IWL_CMD_QUEUE_NUM);
Zhu Yib481de92007-09-25 17:54:57 -0700574
575 txq->need_update = 1;
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800576
577 /* Set up entry in queue's byte count circular buffer */
Zhu Yib481de92007-09-25 17:54:57 -0700578 ret = iwl4965_tx_queue_update_wr_ptr(priv, txq, 0);
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800579
580 /* Increment and update queue's write index */
Tomas Winklerc54b6792008-03-06 17:36:53 -0800581 q->write_ptr = iwl_queue_inc_wrap(q->write_ptr, q->n_bd);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800582 iwl4965_tx_queue_update_write_ptr(priv, txq);
Zhu Yib481de92007-09-25 17:54:57 -0700583
584 spin_unlock_irqrestore(&priv->hcmd_lock, flags);
585 return ret ? ret : idx;
586}
587
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -0700588static void iwl4965_set_rxon_hwcrypto(struct iwl_priv *priv, int hw_decrypt)
589{
590 struct iwl4965_rxon_cmd *rxon = &priv->staging_rxon;
591
592 if (hw_decrypt)
593 rxon->filter_flags &= ~RXON_FILTER_DIS_DECRYPT_MSK;
594 else
595 rxon->filter_flags |= RXON_FILTER_DIS_DECRYPT_MSK;
596
597}
598
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700599int iwl4965_send_statistics_request(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700600{
Tomas Winkler857485c2008-03-21 13:53:44 -0700601 u32 flags = 0;
602 return iwl_send_cmd_pdu(priv, REPLY_STATISTICS_CMD,
603 sizeof(flags), &flags);
Zhu Yib481de92007-09-25 17:54:57 -0700604}
605
606/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800607 * iwl4965_rxon_add_station - add station into station table.
Zhu Yib481de92007-09-25 17:54:57 -0700608 *
609 * there is only one AP station with id= IWL_AP_ID
Ben Cahill9fbab512007-11-29 11:09:47 +0800610 * NOTE: mutex must be held before calling this fnction
611 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700612static int iwl4965_rxon_add_station(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -0700613 const u8 *addr, int is_ap)
614{
Zhu Yi556f8db2007-09-27 11:27:33 +0800615 u8 sta_id;
Zhu Yib481de92007-09-25 17:54:57 -0700616
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800617 /* Add station to device's station table */
Ron Rindjunsky67d62032007-11-26 16:14:40 +0200618#ifdef CONFIG_IWL4965_HT
619 struct ieee80211_conf *conf = &priv->hw->conf;
620 struct ieee80211_ht_info *cur_ht_config = &conf->ht_conf;
621
622 if ((is_ap) &&
623 (conf->flags & IEEE80211_CONF_SUPPORT_HT_MODE) &&
624 (priv->iw_mode == IEEE80211_IF_TYPE_STA))
625 sta_id = iwl4965_add_station_flags(priv, addr, is_ap,
626 0, cur_ht_config);
627 else
628#endif /* CONFIG_IWL4965_HT */
629 sta_id = iwl4965_add_station_flags(priv, addr, is_ap,
630 0, NULL);
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800631
632 /* Set up default rate scaling table in device's station table */
Zhu Yib481de92007-09-25 17:54:57 -0700633 iwl4965_add_station(priv, addr, is_ap);
634
Zhu Yi556f8db2007-09-27 11:27:33 +0800635 return sta_id;
Zhu Yib481de92007-09-25 17:54:57 -0700636}
637
638/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800639 * iwl4965_check_rxon_cmd - validate RXON structure is valid
Zhu Yib481de92007-09-25 17:54:57 -0700640 *
641 * NOTE: This is really only useful during development and can eventually
642 * be #ifdef'd out once the driver is stable and folks aren't actively
643 * making changes
644 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800645static int iwl4965_check_rxon_cmd(struct iwl4965_rxon_cmd *rxon)
Zhu Yib481de92007-09-25 17:54:57 -0700646{
647 int error = 0;
648 int counter = 1;
649
650 if (rxon->flags & RXON_FLG_BAND_24G_MSK) {
651 error |= le32_to_cpu(rxon->flags &
652 (RXON_FLG_TGJ_NARROW_BAND_MSK |
653 RXON_FLG_RADAR_DETECT_MSK));
654 if (error)
655 IWL_WARNING("check 24G fields %d | %d\n",
656 counter++, error);
657 } else {
658 error |= (rxon->flags & RXON_FLG_SHORT_SLOT_MSK) ?
659 0 : le32_to_cpu(RXON_FLG_SHORT_SLOT_MSK);
660 if (error)
661 IWL_WARNING("check 52 fields %d | %d\n",
662 counter++, error);
663 error |= le32_to_cpu(rxon->flags & RXON_FLG_CCK_MSK);
664 if (error)
665 IWL_WARNING("check 52 CCK %d | %d\n",
666 counter++, error);
667 }
668 error |= (rxon->node_addr[0] | rxon->bssid_addr[0]) & 0x1;
669 if (error)
670 IWL_WARNING("check mac addr %d | %d\n", counter++, error);
671
672 /* make sure basic rates 6Mbps and 1Mbps are supported */
673 error |= (((rxon->ofdm_basic_rates & IWL_RATE_6M_MASK) == 0) &&
674 ((rxon->cck_basic_rates & IWL_RATE_1M_MASK) == 0));
675 if (error)
676 IWL_WARNING("check basic rate %d | %d\n", counter++, error);
677
678 error |= (le16_to_cpu(rxon->assoc_id) > 2007);
679 if (error)
680 IWL_WARNING("check assoc id %d | %d\n", counter++, error);
681
682 error |= ((rxon->flags & (RXON_FLG_CCK_MSK | RXON_FLG_SHORT_SLOT_MSK))
683 == (RXON_FLG_CCK_MSK | RXON_FLG_SHORT_SLOT_MSK));
684 if (error)
685 IWL_WARNING("check CCK and short slot %d | %d\n",
686 counter++, error);
687
688 error |= ((rxon->flags & (RXON_FLG_CCK_MSK | RXON_FLG_AUTO_DETECT_MSK))
689 == (RXON_FLG_CCK_MSK | RXON_FLG_AUTO_DETECT_MSK));
690 if (error)
691 IWL_WARNING("check CCK & auto detect %d | %d\n",
692 counter++, error);
693
694 error |= ((rxon->flags & (RXON_FLG_AUTO_DETECT_MSK |
695 RXON_FLG_TGG_PROTECT_MSK)) == RXON_FLG_TGG_PROTECT_MSK);
696 if (error)
697 IWL_WARNING("check TGG and auto detect %d | %d\n",
698 counter++, error);
699
700 if (error)
701 IWL_WARNING("Tuning to channel %d\n",
702 le16_to_cpu(rxon->channel));
703
704 if (error) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800705 IWL_ERROR("Not a valid iwl4965_rxon_assoc_cmd field values\n");
Zhu Yib481de92007-09-25 17:54:57 -0700706 return -1;
707 }
708 return 0;
709}
710
711/**
Ben Cahill9fbab512007-11-29 11:09:47 +0800712 * iwl4965_full_rxon_required - check if full RXON (vs RXON_ASSOC) cmd is needed
Ian Schram01ebd062007-10-25 17:15:22 +0800713 * @priv: staging_rxon is compared to active_rxon
Zhu Yib481de92007-09-25 17:54:57 -0700714 *
Ben Cahill9fbab512007-11-29 11:09:47 +0800715 * If the RXON structure is changing enough to require a new tune,
716 * or is clearing the RXON_FILTER_ASSOC_MSK, then return 1 to indicate that
717 * a new tune (full RXON command, rather than RXON_ASSOC cmd) is required.
Zhu Yib481de92007-09-25 17:54:57 -0700718 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700719static int iwl4965_full_rxon_required(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700720{
721
722 /* These items are only settable from the full RXON command */
723 if (!(priv->active_rxon.filter_flags & RXON_FILTER_ASSOC_MSK) ||
724 compare_ether_addr(priv->staging_rxon.bssid_addr,
725 priv->active_rxon.bssid_addr) ||
726 compare_ether_addr(priv->staging_rxon.node_addr,
727 priv->active_rxon.node_addr) ||
728 compare_ether_addr(priv->staging_rxon.wlap_bssid_addr,
729 priv->active_rxon.wlap_bssid_addr) ||
730 (priv->staging_rxon.dev_type != priv->active_rxon.dev_type) ||
731 (priv->staging_rxon.channel != priv->active_rxon.channel) ||
732 (priv->staging_rxon.air_propagation !=
733 priv->active_rxon.air_propagation) ||
734 (priv->staging_rxon.ofdm_ht_single_stream_basic_rates !=
735 priv->active_rxon.ofdm_ht_single_stream_basic_rates) ||
736 (priv->staging_rxon.ofdm_ht_dual_stream_basic_rates !=
737 priv->active_rxon.ofdm_ht_dual_stream_basic_rates) ||
738 (priv->staging_rxon.rx_chain != priv->active_rxon.rx_chain) ||
739 (priv->staging_rxon.assoc_id != priv->active_rxon.assoc_id))
740 return 1;
741
742 /* flags, filter_flags, ofdm_basic_rates, and cck_basic_rates can
743 * be updated with the RXON_ASSOC command -- however only some
744 * flag transitions are allowed using RXON_ASSOC */
745
746 /* Check if we are not switching bands */
747 if ((priv->staging_rxon.flags & RXON_FLG_BAND_24G_MSK) !=
748 (priv->active_rxon.flags & RXON_FLG_BAND_24G_MSK))
749 return 1;
750
751 /* Check if we are switching association toggle */
752 if ((priv->staging_rxon.filter_flags & RXON_FILTER_ASSOC_MSK) !=
753 (priv->active_rxon.filter_flags & RXON_FILTER_ASSOC_MSK))
754 return 1;
755
756 return 0;
757}
758
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700759static int iwl4965_send_rxon_assoc(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700760{
761 int rc = 0;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800762 struct iwl4965_rx_packet *res = NULL;
763 struct iwl4965_rxon_assoc_cmd rxon_assoc;
Tomas Winkler857485c2008-03-21 13:53:44 -0700764 struct iwl_host_cmd cmd = {
Zhu Yib481de92007-09-25 17:54:57 -0700765 .id = REPLY_RXON_ASSOC,
766 .len = sizeof(rxon_assoc),
767 .meta.flags = CMD_WANT_SKB,
768 .data = &rxon_assoc,
769 };
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800770 const struct iwl4965_rxon_cmd *rxon1 = &priv->staging_rxon;
771 const struct iwl4965_rxon_cmd *rxon2 = &priv->active_rxon;
Zhu Yib481de92007-09-25 17:54:57 -0700772
773 if ((rxon1->flags == rxon2->flags) &&
774 (rxon1->filter_flags == rxon2->filter_flags) &&
775 (rxon1->cck_basic_rates == rxon2->cck_basic_rates) &&
776 (rxon1->ofdm_ht_single_stream_basic_rates ==
777 rxon2->ofdm_ht_single_stream_basic_rates) &&
778 (rxon1->ofdm_ht_dual_stream_basic_rates ==
779 rxon2->ofdm_ht_dual_stream_basic_rates) &&
780 (rxon1->rx_chain == rxon2->rx_chain) &&
781 (rxon1->ofdm_basic_rates == rxon2->ofdm_basic_rates)) {
782 IWL_DEBUG_INFO("Using current RXON_ASSOC. Not resending.\n");
783 return 0;
784 }
785
786 rxon_assoc.flags = priv->staging_rxon.flags;
787 rxon_assoc.filter_flags = priv->staging_rxon.filter_flags;
788 rxon_assoc.ofdm_basic_rates = priv->staging_rxon.ofdm_basic_rates;
789 rxon_assoc.cck_basic_rates = priv->staging_rxon.cck_basic_rates;
790 rxon_assoc.reserved = 0;
791 rxon_assoc.ofdm_ht_single_stream_basic_rates =
792 priv->staging_rxon.ofdm_ht_single_stream_basic_rates;
793 rxon_assoc.ofdm_ht_dual_stream_basic_rates =
794 priv->staging_rxon.ofdm_ht_dual_stream_basic_rates;
795 rxon_assoc.rx_chain_select_flags = priv->staging_rxon.rx_chain;
796
Tomas Winkler857485c2008-03-21 13:53:44 -0700797 rc = iwl_send_cmd_sync(priv, &cmd);
Zhu Yib481de92007-09-25 17:54:57 -0700798 if (rc)
799 return rc;
800
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800801 res = (struct iwl4965_rx_packet *)cmd.meta.u.skb->data;
Zhu Yib481de92007-09-25 17:54:57 -0700802 if (res->hdr.flags & IWL_CMD_FAILED_MSK) {
803 IWL_ERROR("Bad return from REPLY_RXON_ASSOC command\n");
804 rc = -EIO;
805 }
806
807 priv->alloc_rxb_skb--;
808 dev_kfree_skb_any(cmd.meta.u.skb);
809
810 return rc;
811}
812
813/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800814 * iwl4965_commit_rxon - commit staging_rxon to hardware
Zhu Yib481de92007-09-25 17:54:57 -0700815 *
Ian Schram01ebd062007-10-25 17:15:22 +0800816 * The RXON command in staging_rxon is committed to the hardware and
Zhu Yib481de92007-09-25 17:54:57 -0700817 * the active_rxon structure is updated with the new data. This
818 * function correctly transitions out of the RXON_ASSOC_MSK state if
819 * a HW tune is required based on the RXON structure changes.
820 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700821static int iwl4965_commit_rxon(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700822{
823 /* cast away the const for active_rxon in this function */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800824 struct iwl4965_rxon_cmd *active_rxon = (void *)&priv->active_rxon;
Joe Perches0795af52007-10-03 17:59:30 -0700825 DECLARE_MAC_BUF(mac);
Zhu Yib481de92007-09-25 17:54:57 -0700826 int rc = 0;
827
Tomas Winklerfee12472008-04-03 16:05:21 -0700828 if (!iwl_is_alive(priv))
Zhu Yib481de92007-09-25 17:54:57 -0700829 return -1;
830
831 /* always get timestamp with Rx frame */
832 priv->staging_rxon.flags |= RXON_FLG_TSF2HOST_MSK;
833
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800834 rc = iwl4965_check_rxon_cmd(&priv->staging_rxon);
Zhu Yib481de92007-09-25 17:54:57 -0700835 if (rc) {
836 IWL_ERROR("Invalid RXON configuration. Not committing.\n");
837 return -EINVAL;
838 }
839
840 /* If we don't need to send a full RXON, we can use
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800841 * iwl4965_rxon_assoc_cmd which is used to reconfigure filter
Zhu Yib481de92007-09-25 17:54:57 -0700842 * and other flags for the current radio configuration. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800843 if (!iwl4965_full_rxon_required(priv)) {
844 rc = iwl4965_send_rxon_assoc(priv);
Zhu Yib481de92007-09-25 17:54:57 -0700845 if (rc) {
846 IWL_ERROR("Error setting RXON_ASSOC "
847 "configuration (%d).\n", rc);
848 return rc;
849 }
850
851 memcpy(active_rxon, &priv->staging_rxon, sizeof(*active_rxon));
852
853 return 0;
854 }
855
856 /* station table will be cleared */
857 priv->assoc_station_added = 0;
858
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +0800859#ifdef CONFIG_IWL4965_SENSITIVITY
Zhu Yib481de92007-09-25 17:54:57 -0700860 priv->sensitivity_data.state = IWL_SENS_CALIB_NEED_REINIT;
861 if (!priv->error_recovering)
862 priv->start_calib = 0;
863
864 iwl4965_init_sensitivity(priv, CMD_ASYNC, 1);
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +0800865#endif /* CONFIG_IWL4965_SENSITIVITY */
Zhu Yib481de92007-09-25 17:54:57 -0700866
867 /* If we are currently associated and the new config requires
868 * an RXON_ASSOC and the new config wants the associated mask enabled,
869 * we must clear the associated from the active configuration
870 * before we apply the new config */
Tomas Winkler3109ece2008-03-28 16:33:35 -0700871 if (iwl_is_associated(priv) &&
Zhu Yib481de92007-09-25 17:54:57 -0700872 (priv->staging_rxon.filter_flags & RXON_FILTER_ASSOC_MSK)) {
873 IWL_DEBUG_INFO("Toggling associated bit on current RXON\n");
874 active_rxon->filter_flags &= ~RXON_FILTER_ASSOC_MSK;
875
Tomas Winkler857485c2008-03-21 13:53:44 -0700876 rc = iwl_send_cmd_pdu(priv, REPLY_RXON,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800877 sizeof(struct iwl4965_rxon_cmd),
Zhu Yib481de92007-09-25 17:54:57 -0700878 &priv->active_rxon);
879
880 /* If the mask clearing failed then we set
881 * active_rxon back to what it was previously */
882 if (rc) {
883 active_rxon->filter_flags |= RXON_FILTER_ASSOC_MSK;
884 IWL_ERROR("Error clearing ASSOC_MSK on current "
885 "configuration (%d).\n", rc);
886 return rc;
887 }
Zhu Yib481de92007-09-25 17:54:57 -0700888 }
889
890 IWL_DEBUG_INFO("Sending RXON\n"
891 "* with%s RXON_FILTER_ASSOC_MSK\n"
892 "* channel = %d\n"
Joe Perches0795af52007-10-03 17:59:30 -0700893 "* bssid = %s\n",
Zhu Yib481de92007-09-25 17:54:57 -0700894 ((priv->staging_rxon.filter_flags &
895 RXON_FILTER_ASSOC_MSK) ? "" : "out"),
896 le16_to_cpu(priv->staging_rxon.channel),
Joe Perches0795af52007-10-03 17:59:30 -0700897 print_mac(mac, priv->staging_rxon.bssid_addr));
Zhu Yib481de92007-09-25 17:54:57 -0700898
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -0700899 iwl4965_set_rxon_hwcrypto(priv, priv->cfg->mod_params->hw_crypto);
Zhu Yib481de92007-09-25 17:54:57 -0700900 /* Apply the new configuration */
Tomas Winkler857485c2008-03-21 13:53:44 -0700901 rc = iwl_send_cmd_pdu(priv, REPLY_RXON,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800902 sizeof(struct iwl4965_rxon_cmd), &priv->staging_rxon);
Zhu Yib481de92007-09-25 17:54:57 -0700903 if (rc) {
904 IWL_ERROR("Error setting new configuration (%d).\n", rc);
905 return rc;
906 }
907
Assaf Kraussbf85ea42008-03-14 10:38:49 -0700908 iwlcore_clear_stations_table(priv);
Zhu Yi556f8db2007-09-27 11:27:33 +0800909
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +0800910#ifdef CONFIG_IWL4965_SENSITIVITY
Zhu Yib481de92007-09-25 17:54:57 -0700911 if (!priv->error_recovering)
912 priv->start_calib = 0;
913
914 priv->sensitivity_data.state = IWL_SENS_CALIB_NEED_REINIT;
915 iwl4965_init_sensitivity(priv, CMD_ASYNC, 1);
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +0800916#endif /* CONFIG_IWL4965_SENSITIVITY */
Zhu Yib481de92007-09-25 17:54:57 -0700917
918 memcpy(active_rxon, &priv->staging_rxon, sizeof(*active_rxon));
919
920 /* If we issue a new RXON command which required a tune then we must
921 * send a new TXPOWER command or we won't be able to Tx any frames */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800922 rc = iwl4965_hw_reg_send_txpower(priv);
Zhu Yib481de92007-09-25 17:54:57 -0700923 if (rc) {
924 IWL_ERROR("Error setting Tx power (%d).\n", rc);
925 return rc;
926 }
927
928 /* Add the broadcast address so we can send broadcast frames */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800929 if (iwl4965_rxon_add_station(priv, iwl4965_broadcast_addr, 0) ==
Zhu Yib481de92007-09-25 17:54:57 -0700930 IWL_INVALID_STATION) {
931 IWL_ERROR("Error adding BROADCAST address for transmit.\n");
932 return -EIO;
933 }
934
935 /* If we have set the ASSOC_MSK and we are in BSS mode then
936 * add the IWL_AP_ID to the station rate table */
Tomas Winkler3109ece2008-03-28 16:33:35 -0700937 if (iwl_is_associated(priv) &&
Zhu Yib481de92007-09-25 17:54:57 -0700938 (priv->iw_mode == IEEE80211_IF_TYPE_STA)) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800939 if (iwl4965_rxon_add_station(priv, priv->active_rxon.bssid_addr, 1)
Zhu Yib481de92007-09-25 17:54:57 -0700940 == IWL_INVALID_STATION) {
941 IWL_ERROR("Error adding AP address for transmit.\n");
942 return -EIO;
943 }
944 priv->assoc_station_added = 1;
Emmanuel Grumbach6974e362008-04-14 21:16:06 -0700945 if (priv->default_wep_key &&
946 iwl_send_static_wepkey_cmd(priv, 0))
947 IWL_ERROR("Could not send WEP static key.\n");
Zhu Yib481de92007-09-25 17:54:57 -0700948 }
949
950 return 0;
951}
952
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700953static int iwl4965_send_bt_config(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700954{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800955 struct iwl4965_bt_cmd bt_cmd = {
Zhu Yib481de92007-09-25 17:54:57 -0700956 .flags = 3,
957 .lead_time = 0xAA,
958 .max_kill = 1,
959 .kill_ack_mask = 0,
960 .kill_cts_mask = 0,
961 };
962
Tomas Winkler857485c2008-03-21 13:53:44 -0700963 return iwl_send_cmd_pdu(priv, REPLY_BT_CONFIG,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800964 sizeof(struct iwl4965_bt_cmd), &bt_cmd);
Zhu Yib481de92007-09-25 17:54:57 -0700965}
966
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700967static int iwl4965_send_scan_abort(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700968{
969 int rc = 0;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800970 struct iwl4965_rx_packet *res;
Tomas Winkler857485c2008-03-21 13:53:44 -0700971 struct iwl_host_cmd cmd = {
Zhu Yib481de92007-09-25 17:54:57 -0700972 .id = REPLY_SCAN_ABORT_CMD,
973 .meta.flags = CMD_WANT_SKB,
974 };
975
976 /* If there isn't a scan actively going on in the hardware
977 * then we are in between scan bands and not actually
978 * actively scanning, so don't send the abort command */
979 if (!test_bit(STATUS_SCAN_HW, &priv->status)) {
980 clear_bit(STATUS_SCAN_ABORTING, &priv->status);
981 return 0;
982 }
983
Tomas Winkler857485c2008-03-21 13:53:44 -0700984 rc = iwl_send_cmd_sync(priv, &cmd);
Zhu Yib481de92007-09-25 17:54:57 -0700985 if (rc) {
986 clear_bit(STATUS_SCAN_ABORTING, &priv->status);
987 return rc;
988 }
989
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800990 res = (struct iwl4965_rx_packet *)cmd.meta.u.skb->data;
Zhu Yib481de92007-09-25 17:54:57 -0700991 if (res->u.status != CAN_ABORT_STATUS) {
992 /* The scan abort will return 1 for success or
993 * 2 for "failure". A failure condition can be
994 * due to simply not being in an active scan which
995 * can occur if we send the scan abort before we
996 * the microcode has notified us that a scan is
997 * completed. */
998 IWL_DEBUG_INFO("SCAN_ABORT returned %d.\n", res->u.status);
999 clear_bit(STATUS_SCAN_ABORTING, &priv->status);
1000 clear_bit(STATUS_SCAN_HW, &priv->status);
1001 }
1002
1003 dev_kfree_skb_any(cmd.meta.u.skb);
1004
1005 return rc;
1006}
1007
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001008static int iwl4965_card_state_sync_callback(struct iwl_priv *priv,
Tomas Winkler857485c2008-03-21 13:53:44 -07001009 struct iwl_cmd *cmd,
Zhu Yib481de92007-09-25 17:54:57 -07001010 struct sk_buff *skb)
1011{
1012 return 1;
1013}
1014
1015/*
1016 * CARD_STATE_CMD
1017 *
Ben Cahill9fbab512007-11-29 11:09:47 +08001018 * Use: Sets the device's internal card state to enable, disable, or halt
Zhu Yib481de92007-09-25 17:54:57 -07001019 *
1020 * When in the 'enable' state the card operates as normal.
1021 * When in the 'disable' state, the card enters into a low power mode.
1022 * When in the 'halt' state, the card is shut down and must be fully
1023 * restarted to come back on.
1024 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001025static int iwl4965_send_card_state(struct iwl_priv *priv, u32 flags, u8 meta_flag)
Zhu Yib481de92007-09-25 17:54:57 -07001026{
Tomas Winkler857485c2008-03-21 13:53:44 -07001027 struct iwl_host_cmd cmd = {
Zhu Yib481de92007-09-25 17:54:57 -07001028 .id = REPLY_CARD_STATE_CMD,
1029 .len = sizeof(u32),
1030 .data = &flags,
1031 .meta.flags = meta_flag,
1032 };
1033
1034 if (meta_flag & CMD_ASYNC)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001035 cmd.meta.u.callback = iwl4965_card_state_sync_callback;
Zhu Yib481de92007-09-25 17:54:57 -07001036
Tomas Winkler857485c2008-03-21 13:53:44 -07001037 return iwl_send_cmd(priv, &cmd);
Zhu Yib481de92007-09-25 17:54:57 -07001038}
1039
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001040static int iwl4965_add_sta_sync_callback(struct iwl_priv *priv,
Tomas Winkler857485c2008-03-21 13:53:44 -07001041 struct iwl_cmd *cmd, struct sk_buff *skb)
Zhu Yib481de92007-09-25 17:54:57 -07001042{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001043 struct iwl4965_rx_packet *res = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07001044
1045 if (!skb) {
1046 IWL_ERROR("Error: Response NULL in REPLY_ADD_STA.\n");
1047 return 1;
1048 }
1049
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001050 res = (struct iwl4965_rx_packet *)skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07001051 if (res->hdr.flags & IWL_CMD_FAILED_MSK) {
1052 IWL_ERROR("Bad return from REPLY_ADD_STA (0x%08X)\n",
1053 res->hdr.flags);
1054 return 1;
1055 }
1056
1057 switch (res->u.add_sta.status) {
1058 case ADD_STA_SUCCESS_MSK:
1059 break;
1060 default:
1061 break;
1062 }
1063
1064 /* We didn't cache the SKB; let the caller free it */
1065 return 1;
1066}
1067
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001068int iwl4965_send_add_station(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001069 struct iwl4965_addsta_cmd *sta, u8 flags)
Zhu Yib481de92007-09-25 17:54:57 -07001070{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001071 struct iwl4965_rx_packet *res = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07001072 int rc = 0;
Tomas Winkler857485c2008-03-21 13:53:44 -07001073 struct iwl_host_cmd cmd = {
Zhu Yib481de92007-09-25 17:54:57 -07001074 .id = REPLY_ADD_STA,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001075 .len = sizeof(struct iwl4965_addsta_cmd),
Zhu Yib481de92007-09-25 17:54:57 -07001076 .meta.flags = flags,
1077 .data = sta,
1078 };
1079
1080 if (flags & CMD_ASYNC)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001081 cmd.meta.u.callback = iwl4965_add_sta_sync_callback;
Zhu Yib481de92007-09-25 17:54:57 -07001082 else
1083 cmd.meta.flags |= CMD_WANT_SKB;
1084
Tomas Winkler857485c2008-03-21 13:53:44 -07001085 rc = iwl_send_cmd(priv, &cmd);
Zhu Yib481de92007-09-25 17:54:57 -07001086
1087 if (rc || (flags & CMD_ASYNC))
1088 return rc;
1089
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001090 res = (struct iwl4965_rx_packet *)cmd.meta.u.skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07001091 if (res->hdr.flags & IWL_CMD_FAILED_MSK) {
1092 IWL_ERROR("Bad return from REPLY_ADD_STA (0x%08X)\n",
1093 res->hdr.flags);
1094 rc = -EIO;
1095 }
1096
1097 if (rc == 0) {
1098 switch (res->u.add_sta.status) {
1099 case ADD_STA_SUCCESS_MSK:
1100 IWL_DEBUG_INFO("REPLY_ADD_STA PASSED\n");
1101 break;
1102 default:
1103 rc = -EIO;
1104 IWL_WARNING("REPLY_ADD_STA failed\n");
1105 break;
1106 }
1107 }
1108
1109 priv->alloc_rxb_skb--;
1110 dev_kfree_skb_any(cmd.meta.u.skb);
1111
1112 return rc;
1113}
1114
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001115static void iwl4965_clear_free_frames(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001116{
1117 struct list_head *element;
1118
1119 IWL_DEBUG_INFO("%d frames on pre-allocated heap on clear.\n",
1120 priv->frames_count);
1121
1122 while (!list_empty(&priv->free_frames)) {
1123 element = priv->free_frames.next;
1124 list_del(element);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001125 kfree(list_entry(element, struct iwl4965_frame, list));
Zhu Yib481de92007-09-25 17:54:57 -07001126 priv->frames_count--;
1127 }
1128
1129 if (priv->frames_count) {
1130 IWL_WARNING("%d frames still in use. Did we lose one?\n",
1131 priv->frames_count);
1132 priv->frames_count = 0;
1133 }
1134}
1135
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001136static struct iwl4965_frame *iwl4965_get_free_frame(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001137{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001138 struct iwl4965_frame *frame;
Zhu Yib481de92007-09-25 17:54:57 -07001139 struct list_head *element;
1140 if (list_empty(&priv->free_frames)) {
1141 frame = kzalloc(sizeof(*frame), GFP_KERNEL);
1142 if (!frame) {
1143 IWL_ERROR("Could not allocate frame!\n");
1144 return NULL;
1145 }
1146
1147 priv->frames_count++;
1148 return frame;
1149 }
1150
1151 element = priv->free_frames.next;
1152 list_del(element);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001153 return list_entry(element, struct iwl4965_frame, list);
Zhu Yib481de92007-09-25 17:54:57 -07001154}
1155
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001156static void iwl4965_free_frame(struct iwl_priv *priv, struct iwl4965_frame *frame)
Zhu Yib481de92007-09-25 17:54:57 -07001157{
1158 memset(frame, 0, sizeof(*frame));
1159 list_add(&frame->list, &priv->free_frames);
1160}
1161
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001162unsigned int iwl4965_fill_beacon_frame(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07001163 struct ieee80211_hdr *hdr,
1164 const u8 *dest, int left)
1165{
1166
Tomas Winkler3109ece2008-03-28 16:33:35 -07001167 if (!iwl_is_associated(priv) || !priv->ibss_beacon ||
Zhu Yib481de92007-09-25 17:54:57 -07001168 ((priv->iw_mode != IEEE80211_IF_TYPE_IBSS) &&
1169 (priv->iw_mode != IEEE80211_IF_TYPE_AP)))
1170 return 0;
1171
1172 if (priv->ibss_beacon->len > left)
1173 return 0;
1174
1175 memcpy(hdr, priv->ibss_beacon->data, priv->ibss_beacon->len);
1176
1177 return priv->ibss_beacon->len;
1178}
1179
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001180static u8 iwl4965_rate_get_lowest_plcp(int rate_mask)
Zhu Yib481de92007-09-25 17:54:57 -07001181{
1182 u8 i;
1183
1184 for (i = IWL_RATE_1M_INDEX; i != IWL_RATE_INVALID;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001185 i = iwl4965_rates[i].next_ieee) {
Zhu Yib481de92007-09-25 17:54:57 -07001186 if (rate_mask & (1 << i))
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001187 return iwl4965_rates[i].plcp;
Zhu Yib481de92007-09-25 17:54:57 -07001188 }
1189
1190 return IWL_RATE_INVALID;
1191}
1192
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001193static int iwl4965_send_beacon_cmd(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001194{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001195 struct iwl4965_frame *frame;
Zhu Yib481de92007-09-25 17:54:57 -07001196 unsigned int frame_size;
1197 int rc;
1198 u8 rate;
1199
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001200 frame = iwl4965_get_free_frame(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001201
1202 if (!frame) {
1203 IWL_ERROR("Could not obtain free frame buffer for beacon "
1204 "command.\n");
1205 return -ENOMEM;
1206 }
1207
1208 if (!(priv->staging_rxon.flags & RXON_FLG_BAND_24G_MSK)) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001209 rate = iwl4965_rate_get_lowest_plcp(priv->active_rate_basic &
Zhu Yib481de92007-09-25 17:54:57 -07001210 0xFF0);
1211 if (rate == IWL_INVALID_RATE)
1212 rate = IWL_RATE_6M_PLCP;
1213 } else {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001214 rate = iwl4965_rate_get_lowest_plcp(priv->active_rate_basic & 0xF);
Zhu Yib481de92007-09-25 17:54:57 -07001215 if (rate == IWL_INVALID_RATE)
1216 rate = IWL_RATE_1M_PLCP;
1217 }
1218
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001219 frame_size = iwl4965_hw_get_beacon_cmd(priv, frame, rate);
Zhu Yib481de92007-09-25 17:54:57 -07001220
Tomas Winkler857485c2008-03-21 13:53:44 -07001221 rc = iwl_send_cmd_pdu(priv, REPLY_TX_BEACON, frame_size,
Zhu Yib481de92007-09-25 17:54:57 -07001222 &frame->u.cmd[0]);
1223
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001224 iwl4965_free_frame(priv, frame);
Zhu Yib481de92007-09-25 17:54:57 -07001225
1226 return rc;
1227}
1228
1229/******************************************************************************
1230 *
Zhu Yib481de92007-09-25 17:54:57 -07001231 * Misc. internal state and helper functions
1232 *
1233 ******************************************************************************/
Zhu Yib481de92007-09-25 17:54:57 -07001234
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001235static void iwl4965_unset_hw_setting(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001236{
1237 if (priv->hw_setting.shared_virt)
1238 pci_free_consistent(priv->pci_dev,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001239 sizeof(struct iwl4965_shared),
Zhu Yib481de92007-09-25 17:54:57 -07001240 priv->hw_setting.shared_virt,
1241 priv->hw_setting.shared_phys);
1242}
1243
1244/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001245 * iwl4965_supported_rate_to_ie - fill in the supported rate in IE field
Zhu Yib481de92007-09-25 17:54:57 -07001246 *
1247 * return : set the bit for each supported rate insert in ie
1248 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001249static u16 iwl4965_supported_rate_to_ie(u8 *ie, u16 supported_rate,
Tomas Winklerc7c46672007-10-18 02:04:15 +02001250 u16 basic_rate, int *left)
Zhu Yib481de92007-09-25 17:54:57 -07001251{
1252 u16 ret_rates = 0, bit;
1253 int i;
Tomas Winklerc7c46672007-10-18 02:04:15 +02001254 u8 *cnt = ie;
1255 u8 *rates = ie + 1;
Zhu Yib481de92007-09-25 17:54:57 -07001256
1257 for (bit = 1, i = 0; i < IWL_RATE_COUNT; i++, bit <<= 1) {
1258 if (bit & supported_rate) {
1259 ret_rates |= bit;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001260 rates[*cnt] = iwl4965_rates[i].ieee |
Tomas Winklerc7c46672007-10-18 02:04:15 +02001261 ((bit & basic_rate) ? 0x80 : 0x00);
1262 (*cnt)++;
1263 (*left)--;
1264 if ((*left <= 0) ||
1265 (*cnt >= IWL_SUPPORTED_RATES_IE_LEN))
Zhu Yib481de92007-09-25 17:54:57 -07001266 break;
1267 }
1268 }
1269
1270 return ret_rates;
1271}
1272
Zhu Yib481de92007-09-25 17:54:57 -07001273/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001274 * iwl4965_fill_probe_req - fill in all required fields and IE for probe request
Zhu Yib481de92007-09-25 17:54:57 -07001275 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001276static u16 iwl4965_fill_probe_req(struct iwl_priv *priv,
Tomas Winkler78330fd2008-02-06 02:37:18 +02001277 enum ieee80211_band band,
1278 struct ieee80211_mgmt *frame,
1279 int left, int is_direct)
Zhu Yib481de92007-09-25 17:54:57 -07001280{
1281 int len = 0;
1282 u8 *pos = NULL;
mabbasbee488d2007-10-25 17:15:42 +08001283 u16 active_rates, ret_rates, cck_rates, active_rate_basic;
Ron Rindjunsky8fb88032007-11-26 16:14:38 +02001284#ifdef CONFIG_IWL4965_HT
Tomas Winkler78330fd2008-02-06 02:37:18 +02001285 const struct ieee80211_supported_band *sband =
1286 iwl4965_get_hw_mode(priv, band);
Ron Rindjunsky8fb88032007-11-26 16:14:38 +02001287#endif /* CONFIG_IWL4965_HT */
Zhu Yib481de92007-09-25 17:54:57 -07001288
1289 /* Make sure there is enough space for the probe request,
1290 * two mandatory IEs and the data */
1291 left -= 24;
1292 if (left < 0)
1293 return 0;
1294 len += 24;
1295
1296 frame->frame_control = cpu_to_le16(IEEE80211_STYPE_PROBE_REQ);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001297 memcpy(frame->da, iwl4965_broadcast_addr, ETH_ALEN);
Zhu Yib481de92007-09-25 17:54:57 -07001298 memcpy(frame->sa, priv->mac_addr, ETH_ALEN);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001299 memcpy(frame->bssid, iwl4965_broadcast_addr, ETH_ALEN);
Zhu Yib481de92007-09-25 17:54:57 -07001300 frame->seq_ctrl = 0;
1301
1302 /* fill in our indirect SSID IE */
1303 /* ...next IE... */
1304
1305 left -= 2;
1306 if (left < 0)
1307 return 0;
1308 len += 2;
1309 pos = &(frame->u.probe_req.variable[0]);
1310 *pos++ = WLAN_EID_SSID;
1311 *pos++ = 0;
1312
1313 /* fill in our direct SSID IE... */
1314 if (is_direct) {
1315 /* ...next IE... */
1316 left -= 2 + priv->essid_len;
1317 if (left < 0)
1318 return 0;
1319 /* ... fill it in... */
1320 *pos++ = WLAN_EID_SSID;
1321 *pos++ = priv->essid_len;
1322 memcpy(pos, priv->essid, priv->essid_len);
1323 pos += priv->essid_len;
1324 len += 2 + priv->essid_len;
1325 }
1326
1327 /* fill in supported rate */
1328 /* ...next IE... */
1329 left -= 2;
1330 if (left < 0)
1331 return 0;
Tomas Winklerc7c46672007-10-18 02:04:15 +02001332
Zhu Yib481de92007-09-25 17:54:57 -07001333 /* ... fill it in... */
1334 *pos++ = WLAN_EID_SUPP_RATES;
1335 *pos = 0;
Tomas Winklerc7c46672007-10-18 02:04:15 +02001336
mabbasbee488d2007-10-25 17:15:42 +08001337 /* exclude 60M rate */
1338 active_rates = priv->rates_mask;
1339 active_rates &= ~IWL_RATE_60M_MASK;
1340
1341 active_rate_basic = active_rates & IWL_BASIC_RATES_MASK;
Zhu Yib481de92007-09-25 17:54:57 -07001342
Tomas Winklerc7c46672007-10-18 02:04:15 +02001343 cck_rates = IWL_CCK_RATES_MASK & active_rates;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001344 ret_rates = iwl4965_supported_rate_to_ie(pos, cck_rates,
mabbasbee488d2007-10-25 17:15:42 +08001345 active_rate_basic, &left);
Tomas Winklerc7c46672007-10-18 02:04:15 +02001346 active_rates &= ~ret_rates;
1347
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001348 ret_rates = iwl4965_supported_rate_to_ie(pos, active_rates,
mabbasbee488d2007-10-25 17:15:42 +08001349 active_rate_basic, &left);
Tomas Winklerc7c46672007-10-18 02:04:15 +02001350 active_rates &= ~ret_rates;
1351
Zhu Yib481de92007-09-25 17:54:57 -07001352 len += 2 + *pos;
1353 pos += (*pos) + 1;
Tomas Winklerc7c46672007-10-18 02:04:15 +02001354 if (active_rates == 0)
Zhu Yib481de92007-09-25 17:54:57 -07001355 goto fill_end;
1356
1357 /* fill in supported extended rate */
1358 /* ...next IE... */
1359 left -= 2;
1360 if (left < 0)
1361 return 0;
1362 /* ... fill it in... */
1363 *pos++ = WLAN_EID_EXT_SUPP_RATES;
1364 *pos = 0;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001365 iwl4965_supported_rate_to_ie(pos, active_rates,
mabbasbee488d2007-10-25 17:15:42 +08001366 active_rate_basic, &left);
Zhu Yib481de92007-09-25 17:54:57 -07001367 if (*pos > 0)
1368 len += 2 + *pos;
1369
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08001370#ifdef CONFIG_IWL4965_HT
Tomas Winkler78330fd2008-02-06 02:37:18 +02001371 if (sband && sband->ht_info.ht_supported) {
1372 struct ieee80211_ht_cap *ht_cap;
Zhu Yib481de92007-09-25 17:54:57 -07001373 pos += (*pos) + 1;
1374 *pos++ = WLAN_EID_HT_CAPABILITY;
Ron Rindjunsky8fb88032007-11-26 16:14:38 +02001375 *pos++ = sizeof(struct ieee80211_ht_cap);
Tomas Winkler78330fd2008-02-06 02:37:18 +02001376 ht_cap = (struct ieee80211_ht_cap *)pos;
1377 ht_cap->cap_info = cpu_to_le16(sband->ht_info.cap);
1378 memcpy(ht_cap->supp_mcs_set, sband->ht_info.supp_mcs_set, 16);
1379 ht_cap->ampdu_params_info =(sband->ht_info.ampdu_factor &
1380 IEEE80211_HT_CAP_AMPDU_FACTOR) |
1381 ((sband->ht_info.ampdu_density << 2) &
1382 IEEE80211_HT_CAP_AMPDU_DENSITY);
Ron Rindjunsky8fb88032007-11-26 16:14:38 +02001383 len += 2 + sizeof(struct ieee80211_ht_cap);
Zhu Yib481de92007-09-25 17:54:57 -07001384 }
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08001385#endif /*CONFIG_IWL4965_HT */
Zhu Yib481de92007-09-25 17:54:57 -07001386
1387 fill_end:
1388 return (u16)len;
1389}
1390
1391/*
1392 * QoS support
1393*/
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001394static int iwl4965_send_qos_params_command(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001395 struct iwl4965_qosparam_cmd *qos)
Zhu Yib481de92007-09-25 17:54:57 -07001396{
1397
Tomas Winkler857485c2008-03-21 13:53:44 -07001398 return iwl_send_cmd_pdu(priv, REPLY_QOS_PARAM,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001399 sizeof(struct iwl4965_qosparam_cmd), qos);
Zhu Yib481de92007-09-25 17:54:57 -07001400}
1401
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001402static void iwl4965_activate_qos(struct iwl_priv *priv, u8 force)
Zhu Yib481de92007-09-25 17:54:57 -07001403{
1404 unsigned long flags;
1405
Zhu Yib481de92007-09-25 17:54:57 -07001406 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
1407 return;
1408
1409 if (!priv->qos_data.qos_enable)
1410 return;
1411
1412 spin_lock_irqsave(&priv->lock, flags);
1413 priv->qos_data.def_qos_parm.qos_flags = 0;
1414
1415 if (priv->qos_data.qos_cap.q_AP.queue_request &&
1416 !priv->qos_data.qos_cap.q_AP.txop_request)
1417 priv->qos_data.def_qos_parm.qos_flags |=
1418 QOS_PARAM_FLG_TXOP_TYPE_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07001419 if (priv->qos_data.qos_active)
1420 priv->qos_data.def_qos_parm.qos_flags |=
1421 QOS_PARAM_FLG_UPDATE_EDCA_MSK;
1422
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08001423#ifdef CONFIG_IWL4965_HT
Ron Rindjunskyfd105e72007-11-26 16:14:39 +02001424 if (priv->current_ht_config.is_ht)
Tomas Winklerf1f1f5c2007-10-25 17:15:26 +08001425 priv->qos_data.def_qos_parm.qos_flags |= QOS_PARAM_FLG_TGN_MSK;
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08001426#endif /* CONFIG_IWL4965_HT */
Tomas Winklerf1f1f5c2007-10-25 17:15:26 +08001427
Zhu Yib481de92007-09-25 17:54:57 -07001428 spin_unlock_irqrestore(&priv->lock, flags);
1429
Tomas Winkler3109ece2008-03-28 16:33:35 -07001430 if (force || iwl_is_associated(priv)) {
Tomas Winklerf1f1f5c2007-10-25 17:15:26 +08001431 IWL_DEBUG_QOS("send QoS cmd with Qos active=%d FLAGS=0x%X\n",
1432 priv->qos_data.qos_active,
1433 priv->qos_data.def_qos_parm.qos_flags);
Zhu Yib481de92007-09-25 17:54:57 -07001434
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001435 iwl4965_send_qos_params_command(priv,
Zhu Yib481de92007-09-25 17:54:57 -07001436 &(priv->qos_data.def_qos_parm));
1437 }
1438}
1439
Zhu Yib481de92007-09-25 17:54:57 -07001440/*
1441 * Power management (not Tx power!) functions
1442 */
1443#define MSEC_TO_USEC 1024
1444
1445#define NOSLP __constant_cpu_to_le16(0), 0, 0
1446#define SLP IWL_POWER_DRIVER_ALLOW_SLEEP_MSK, 0, 0
1447#define SLP_TIMEOUT(T) __constant_cpu_to_le32((T) * MSEC_TO_USEC)
1448#define SLP_VEC(X0, X1, X2, X3, X4) {__constant_cpu_to_le32(X0), \
1449 __constant_cpu_to_le32(X1), \
1450 __constant_cpu_to_le32(X2), \
1451 __constant_cpu_to_le32(X3), \
1452 __constant_cpu_to_le32(X4)}
1453
1454
1455/* default power management (not Tx power) table values */
1456/* for tim 0-10 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001457static struct iwl4965_power_vec_entry range_0[IWL_POWER_AC] = {
Zhu Yib481de92007-09-25 17:54:57 -07001458 {{NOSLP, SLP_TIMEOUT(0), SLP_TIMEOUT(0), SLP_VEC(0, 0, 0, 0, 0)}, 0},
1459 {{SLP, SLP_TIMEOUT(200), SLP_TIMEOUT(500), SLP_VEC(1, 2, 3, 4, 4)}, 0},
1460 {{SLP, SLP_TIMEOUT(200), SLP_TIMEOUT(300), SLP_VEC(2, 4, 6, 7, 7)}, 0},
1461 {{SLP, SLP_TIMEOUT(50), SLP_TIMEOUT(100), SLP_VEC(2, 6, 9, 9, 10)}, 0},
1462 {{SLP, SLP_TIMEOUT(50), SLP_TIMEOUT(25), SLP_VEC(2, 7, 9, 9, 10)}, 1},
1463 {{SLP, SLP_TIMEOUT(25), SLP_TIMEOUT(25), SLP_VEC(4, 7, 10, 10, 10)}, 1}
1464};
1465
1466/* for tim > 10 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001467static struct iwl4965_power_vec_entry range_1[IWL_POWER_AC] = {
Zhu Yib481de92007-09-25 17:54:57 -07001468 {{NOSLP, SLP_TIMEOUT(0), SLP_TIMEOUT(0), SLP_VEC(0, 0, 0, 0, 0)}, 0},
1469 {{SLP, SLP_TIMEOUT(200), SLP_TIMEOUT(500),
1470 SLP_VEC(1, 2, 3, 4, 0xFF)}, 0},
1471 {{SLP, SLP_TIMEOUT(200), SLP_TIMEOUT(300),
1472 SLP_VEC(2, 4, 6, 7, 0xFF)}, 0},
1473 {{SLP, SLP_TIMEOUT(50), SLP_TIMEOUT(100),
1474 SLP_VEC(2, 6, 9, 9, 0xFF)}, 0},
1475 {{SLP, SLP_TIMEOUT(50), SLP_TIMEOUT(25), SLP_VEC(2, 7, 9, 9, 0xFF)}, 0},
1476 {{SLP, SLP_TIMEOUT(25), SLP_TIMEOUT(25),
1477 SLP_VEC(4, 7, 10, 10, 0xFF)}, 0}
1478};
1479
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001480int iwl4965_power_init_handle(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001481{
1482 int rc = 0, i;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001483 struct iwl4965_power_mgr *pow_data;
1484 int size = sizeof(struct iwl4965_power_vec_entry) * IWL_POWER_AC;
Zhu Yib481de92007-09-25 17:54:57 -07001485 u16 pci_pm;
1486
1487 IWL_DEBUG_POWER("Initialize power \n");
1488
1489 pow_data = &(priv->power_data);
1490
1491 memset(pow_data, 0, sizeof(*pow_data));
1492
1493 pow_data->active_index = IWL_POWER_RANGE_0;
1494 pow_data->dtim_val = 0xffff;
1495
1496 memcpy(&pow_data->pwr_range_0[0], &range_0[0], size);
1497 memcpy(&pow_data->pwr_range_1[0], &range_1[0], size);
1498
1499 rc = pci_read_config_word(priv->pci_dev, PCI_LINK_CTRL, &pci_pm);
1500 if (rc != 0)
1501 return 0;
1502 else {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001503 struct iwl4965_powertable_cmd *cmd;
Zhu Yib481de92007-09-25 17:54:57 -07001504
1505 IWL_DEBUG_POWER("adjust power command flags\n");
1506
1507 for (i = 0; i < IWL_POWER_AC; i++) {
1508 cmd = &pow_data->pwr_range_0[i].cmd;
1509
1510 if (pci_pm & 0x1)
1511 cmd->flags &= ~IWL_POWER_PCI_PM_MSK;
1512 else
1513 cmd->flags |= IWL_POWER_PCI_PM_MSK;
1514 }
1515 }
1516 return rc;
1517}
1518
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001519static int iwl4965_update_power_cmd(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001520 struct iwl4965_powertable_cmd *cmd, u32 mode)
Zhu Yib481de92007-09-25 17:54:57 -07001521{
1522 int rc = 0, i;
1523 u8 skip;
1524 u32 max_sleep = 0;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001525 struct iwl4965_power_vec_entry *range;
Zhu Yib481de92007-09-25 17:54:57 -07001526 u8 period = 0;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001527 struct iwl4965_power_mgr *pow_data;
Zhu Yib481de92007-09-25 17:54:57 -07001528
1529 if (mode > IWL_POWER_INDEX_5) {
1530 IWL_DEBUG_POWER("Error invalid power mode \n");
1531 return -1;
1532 }
1533 pow_data = &(priv->power_data);
1534
1535 if (pow_data->active_index == IWL_POWER_RANGE_0)
1536 range = &pow_data->pwr_range_0[0];
1537 else
1538 range = &pow_data->pwr_range_1[1];
1539
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001540 memcpy(cmd, &range[mode].cmd, sizeof(struct iwl4965_powertable_cmd));
Zhu Yib481de92007-09-25 17:54:57 -07001541
1542#ifdef IWL_MAC80211_DISABLE
1543 if (priv->assoc_network != NULL) {
1544 unsigned long flags;
1545
1546 period = priv->assoc_network->tim.tim_period;
1547 }
1548#endif /*IWL_MAC80211_DISABLE */
1549 skip = range[mode].no_dtim;
1550
1551 if (period == 0) {
1552 period = 1;
1553 skip = 0;
1554 }
1555
1556 if (skip == 0) {
1557 max_sleep = period;
1558 cmd->flags &= ~IWL_POWER_SLEEP_OVER_DTIM_MSK;
1559 } else {
1560 __le32 slp_itrvl = cmd->sleep_interval[IWL_POWER_VEC_SIZE - 1];
1561 max_sleep = (le32_to_cpu(slp_itrvl) / period) * period;
1562 cmd->flags |= IWL_POWER_SLEEP_OVER_DTIM_MSK;
1563 }
1564
1565 for (i = 0; i < IWL_POWER_VEC_SIZE; i++) {
1566 if (le32_to_cpu(cmd->sleep_interval[i]) > max_sleep)
1567 cmd->sleep_interval[i] = cpu_to_le32(max_sleep);
1568 }
1569
1570 IWL_DEBUG_POWER("Flags value = 0x%08X\n", cmd->flags);
1571 IWL_DEBUG_POWER("Tx timeout = %u\n", le32_to_cpu(cmd->tx_data_timeout));
1572 IWL_DEBUG_POWER("Rx timeout = %u\n", le32_to_cpu(cmd->rx_data_timeout));
1573 IWL_DEBUG_POWER("Sleep interval vector = { %d , %d , %d , %d , %d }\n",
1574 le32_to_cpu(cmd->sleep_interval[0]),
1575 le32_to_cpu(cmd->sleep_interval[1]),
1576 le32_to_cpu(cmd->sleep_interval[2]),
1577 le32_to_cpu(cmd->sleep_interval[3]),
1578 le32_to_cpu(cmd->sleep_interval[4]));
1579
1580 return rc;
1581}
1582
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001583static int iwl4965_send_power_mode(struct iwl_priv *priv, u32 mode)
Zhu Yib481de92007-09-25 17:54:57 -07001584{
John W. Linville9a62f732007-11-15 16:27:36 -05001585 u32 uninitialized_var(final_mode);
Zhu Yib481de92007-09-25 17:54:57 -07001586 int rc;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001587 struct iwl4965_powertable_cmd cmd;
Zhu Yib481de92007-09-25 17:54:57 -07001588
1589 /* If on battery, set to 3,
Ian Schram01ebd062007-10-25 17:15:22 +08001590 * if plugged into AC power, set to CAM ("continuously aware mode"),
Zhu Yib481de92007-09-25 17:54:57 -07001591 * else user level */
1592 switch (mode) {
1593 case IWL_POWER_BATTERY:
1594 final_mode = IWL_POWER_INDEX_3;
1595 break;
1596 case IWL_POWER_AC:
1597 final_mode = IWL_POWER_MODE_CAM;
1598 break;
1599 default:
1600 final_mode = mode;
1601 break;
1602 }
1603
1604 cmd.keep_alive_beacons = 0;
1605
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001606 iwl4965_update_power_cmd(priv, &cmd, final_mode);
Zhu Yib481de92007-09-25 17:54:57 -07001607
Tomas Winkler857485c2008-03-21 13:53:44 -07001608 rc = iwl_send_cmd_pdu(priv, POWER_TABLE_CMD, sizeof(cmd), &cmd);
Zhu Yib481de92007-09-25 17:54:57 -07001609
1610 if (final_mode == IWL_POWER_MODE_CAM)
1611 clear_bit(STATUS_POWER_PMI, &priv->status);
1612 else
1613 set_bit(STATUS_POWER_PMI, &priv->status);
1614
1615 return rc;
1616}
1617
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001618int iwl4965_is_network_packet(struct iwl_priv *priv, struct ieee80211_hdr *header)
Zhu Yib481de92007-09-25 17:54:57 -07001619{
1620 /* Filter incoming packets to determine if they are targeted toward
1621 * this network, discarding packets coming from ourselves */
1622 switch (priv->iw_mode) {
1623 case IEEE80211_IF_TYPE_IBSS: /* Header: Dest. | Source | BSSID */
1624 /* packets from our adapter are dropped (echo) */
1625 if (!compare_ether_addr(header->addr2, priv->mac_addr))
1626 return 0;
1627 /* {broad,multi}cast packets to our IBSS go through */
1628 if (is_multicast_ether_addr(header->addr1))
1629 return !compare_ether_addr(header->addr3, priv->bssid);
1630 /* packets to our adapter go through */
1631 return !compare_ether_addr(header->addr1, priv->mac_addr);
1632 case IEEE80211_IF_TYPE_STA: /* Header: Dest. | AP{BSSID} | Source */
1633 /* packets from our adapter are dropped (echo) */
1634 if (!compare_ether_addr(header->addr3, priv->mac_addr))
1635 return 0;
1636 /* {broad,multi}cast packets to our BSS go through */
1637 if (is_multicast_ether_addr(header->addr1))
1638 return !compare_ether_addr(header->addr2, priv->bssid);
1639 /* packets to our adapter go through */
1640 return !compare_ether_addr(header->addr1, priv->mac_addr);
Tomas Winkler69dc5d92008-03-25 16:33:41 -07001641 default:
1642 break;
Zhu Yib481de92007-09-25 17:54:57 -07001643 }
1644
1645 return 1;
1646}
1647
1648#define TX_STATUS_ENTRY(x) case TX_STATUS_FAIL_ ## x: return #x
1649
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001650static const char *iwl4965_get_tx_fail_reason(u32 status)
Zhu Yib481de92007-09-25 17:54:57 -07001651{
1652 switch (status & TX_STATUS_MSK) {
1653 case TX_STATUS_SUCCESS:
1654 return "SUCCESS";
1655 TX_STATUS_ENTRY(SHORT_LIMIT);
1656 TX_STATUS_ENTRY(LONG_LIMIT);
1657 TX_STATUS_ENTRY(FIFO_UNDERRUN);
1658 TX_STATUS_ENTRY(MGMNT_ABORT);
1659 TX_STATUS_ENTRY(NEXT_FRAG);
1660 TX_STATUS_ENTRY(LIFE_EXPIRE);
1661 TX_STATUS_ENTRY(DEST_PS);
1662 TX_STATUS_ENTRY(ABORTED);
1663 TX_STATUS_ENTRY(BT_RETRY);
1664 TX_STATUS_ENTRY(STA_INVALID);
1665 TX_STATUS_ENTRY(FRAG_DROPPED);
1666 TX_STATUS_ENTRY(TID_DISABLE);
1667 TX_STATUS_ENTRY(FRAME_FLUSHED);
1668 TX_STATUS_ENTRY(INSUFFICIENT_CF_POLL);
1669 TX_STATUS_ENTRY(TX_LOCKED);
1670 TX_STATUS_ENTRY(NO_BEACON_ON_RADAR);
1671 }
1672
1673 return "UNKNOWN";
1674}
1675
1676/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001677 * iwl4965_scan_cancel - Cancel any currently executing HW scan
Zhu Yib481de92007-09-25 17:54:57 -07001678 *
1679 * NOTE: priv->mutex is not required before calling this function
1680 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001681static int iwl4965_scan_cancel(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001682{
1683 if (!test_bit(STATUS_SCAN_HW, &priv->status)) {
1684 clear_bit(STATUS_SCANNING, &priv->status);
1685 return 0;
1686 }
1687
1688 if (test_bit(STATUS_SCANNING, &priv->status)) {
1689 if (!test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
1690 IWL_DEBUG_SCAN("Queuing scan abort.\n");
1691 set_bit(STATUS_SCAN_ABORTING, &priv->status);
1692 queue_work(priv->workqueue, &priv->abort_scan);
1693
1694 } else
1695 IWL_DEBUG_SCAN("Scan abort already in progress.\n");
1696
1697 return test_bit(STATUS_SCANNING, &priv->status);
1698 }
1699
1700 return 0;
1701}
1702
1703/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001704 * iwl4965_scan_cancel_timeout - Cancel any currently executing HW scan
Zhu Yib481de92007-09-25 17:54:57 -07001705 * @ms: amount of time to wait (in milliseconds) for scan to abort
1706 *
1707 * NOTE: priv->mutex must be held before calling this function
1708 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001709static int iwl4965_scan_cancel_timeout(struct iwl_priv *priv, unsigned long ms)
Zhu Yib481de92007-09-25 17:54:57 -07001710{
1711 unsigned long now = jiffies;
1712 int ret;
1713
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001714 ret = iwl4965_scan_cancel(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001715 if (ret && ms) {
1716 mutex_unlock(&priv->mutex);
1717 while (!time_after(jiffies, now + msecs_to_jiffies(ms)) &&
1718 test_bit(STATUS_SCANNING, &priv->status))
1719 msleep(1);
1720 mutex_lock(&priv->mutex);
1721
1722 return test_bit(STATUS_SCANNING, &priv->status);
1723 }
1724
1725 return ret;
1726}
1727
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001728static void iwl4965_sequence_reset(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001729{
1730 /* Reset ieee stats */
1731
1732 /* We don't reset the net_device_stats (ieee->stats) on
1733 * re-association */
1734
1735 priv->last_seq_num = -1;
1736 priv->last_frag_num = -1;
1737 priv->last_packet_time = 0;
1738
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001739 iwl4965_scan_cancel(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001740}
1741
1742#define MAX_UCODE_BEACON_INTERVAL 4096
1743#define INTEL_CONN_LISTEN_INTERVAL __constant_cpu_to_le16(0xA)
1744
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001745static __le16 iwl4965_adjust_beacon_interval(u16 beacon_val)
Zhu Yib481de92007-09-25 17:54:57 -07001746{
1747 u16 new_val = 0;
1748 u16 beacon_factor = 0;
1749
1750 beacon_factor =
1751 (beacon_val + MAX_UCODE_BEACON_INTERVAL)
1752 / MAX_UCODE_BEACON_INTERVAL;
1753 new_val = beacon_val / beacon_factor;
1754
1755 return cpu_to_le16(new_val);
1756}
1757
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001758static void iwl4965_setup_rxon_timing(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001759{
1760 u64 interval_tm_unit;
1761 u64 tsf, result;
1762 unsigned long flags;
1763 struct ieee80211_conf *conf = NULL;
1764 u16 beacon_int = 0;
1765
1766 conf = ieee80211_get_hw_conf(priv->hw);
1767
1768 spin_lock_irqsave(&priv->lock, flags);
Tomas Winkler3109ece2008-03-28 16:33:35 -07001769 priv->rxon_timing.timestamp.dw[1] = cpu_to_le32(priv->timestamp >> 32);
1770 priv->rxon_timing.timestamp.dw[0] =
1771 cpu_to_le32(priv->timestamp & 0xFFFFFFFF);
Zhu Yib481de92007-09-25 17:54:57 -07001772
1773 priv->rxon_timing.listen_interval = INTEL_CONN_LISTEN_INTERVAL;
1774
Tomas Winkler3109ece2008-03-28 16:33:35 -07001775 tsf = priv->timestamp;
Zhu Yib481de92007-09-25 17:54:57 -07001776
1777 beacon_int = priv->beacon_int;
1778 spin_unlock_irqrestore(&priv->lock, flags);
1779
1780 if (priv->iw_mode == IEEE80211_IF_TYPE_STA) {
1781 if (beacon_int == 0) {
1782 priv->rxon_timing.beacon_interval = cpu_to_le16(100);
1783 priv->rxon_timing.beacon_init_val = cpu_to_le32(102400);
1784 } else {
1785 priv->rxon_timing.beacon_interval =
1786 cpu_to_le16(beacon_int);
1787 priv->rxon_timing.beacon_interval =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001788 iwl4965_adjust_beacon_interval(
Zhu Yib481de92007-09-25 17:54:57 -07001789 le16_to_cpu(priv->rxon_timing.beacon_interval));
1790 }
1791
1792 priv->rxon_timing.atim_window = 0;
1793 } else {
1794 priv->rxon_timing.beacon_interval =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001795 iwl4965_adjust_beacon_interval(conf->beacon_int);
Zhu Yib481de92007-09-25 17:54:57 -07001796 /* TODO: we need to get atim_window from upper stack
1797 * for now we set to 0 */
1798 priv->rxon_timing.atim_window = 0;
1799 }
1800
1801 interval_tm_unit =
1802 (le16_to_cpu(priv->rxon_timing.beacon_interval) * 1024);
1803 result = do_div(tsf, interval_tm_unit);
1804 priv->rxon_timing.beacon_init_val =
1805 cpu_to_le32((u32) ((u64) interval_tm_unit - result));
1806
1807 IWL_DEBUG_ASSOC
1808 ("beacon interval %d beacon timer %d beacon tim %d\n",
1809 le16_to_cpu(priv->rxon_timing.beacon_interval),
1810 le32_to_cpu(priv->rxon_timing.beacon_init_val),
1811 le16_to_cpu(priv->rxon_timing.atim_window));
1812}
1813
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001814static int iwl4965_scan_initiate(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001815{
1816 if (priv->iw_mode == IEEE80211_IF_TYPE_AP) {
1817 IWL_ERROR("APs don't scan.\n");
1818 return 0;
1819 }
1820
Tomas Winklerfee12472008-04-03 16:05:21 -07001821 if (!iwl_is_ready_rf(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07001822 IWL_DEBUG_SCAN("Aborting scan due to not ready.\n");
1823 return -EIO;
1824 }
1825
1826 if (test_bit(STATUS_SCANNING, &priv->status)) {
1827 IWL_DEBUG_SCAN("Scan already in progress.\n");
1828 return -EAGAIN;
1829 }
1830
1831 if (test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
1832 IWL_DEBUG_SCAN("Scan request while abort pending. "
1833 "Queuing.\n");
1834 return -EAGAIN;
1835 }
1836
1837 IWL_DEBUG_INFO("Starting scan...\n");
1838 priv->scan_bands = 2;
1839 set_bit(STATUS_SCANNING, &priv->status);
1840 priv->scan_start = jiffies;
1841 priv->scan_pass_start = priv->scan_start;
1842
1843 queue_work(priv->workqueue, &priv->request_scan);
1844
1845 return 0;
1846}
1847
Zhu Yib481de92007-09-25 17:54:57 -07001848
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001849static void iwl4965_set_flags_for_phymode(struct iwl_priv *priv,
Johannes Berg8318d782008-01-24 19:38:38 +01001850 enum ieee80211_band band)
Zhu Yib481de92007-09-25 17:54:57 -07001851{
Johannes Berg8318d782008-01-24 19:38:38 +01001852 if (band == IEEE80211_BAND_5GHZ) {
Zhu Yib481de92007-09-25 17:54:57 -07001853 priv->staging_rxon.flags &=
1854 ~(RXON_FLG_BAND_24G_MSK | RXON_FLG_AUTO_DETECT_MSK
1855 | RXON_FLG_CCK_MSK);
1856 priv->staging_rxon.flags |= RXON_FLG_SHORT_SLOT_MSK;
1857 } else {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001858 /* Copied from iwl4965_bg_post_associate() */
Zhu Yib481de92007-09-25 17:54:57 -07001859 if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_SLOT_TIME)
1860 priv->staging_rxon.flags |= RXON_FLG_SHORT_SLOT_MSK;
1861 else
1862 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
1863
1864 if (priv->iw_mode == IEEE80211_IF_TYPE_IBSS)
1865 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
1866
1867 priv->staging_rxon.flags |= RXON_FLG_BAND_24G_MSK;
1868 priv->staging_rxon.flags |= RXON_FLG_AUTO_DETECT_MSK;
1869 priv->staging_rxon.flags &= ~RXON_FLG_CCK_MSK;
1870 }
1871}
1872
1873/*
Ian Schram01ebd062007-10-25 17:15:22 +08001874 * initialize rxon structure with default values from eeprom
Zhu Yib481de92007-09-25 17:54:57 -07001875 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001876static void iwl4965_connection_init_rx_config(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001877{
Assaf Kraussbf85ea42008-03-14 10:38:49 -07001878 const struct iwl_channel_info *ch_info;
Zhu Yib481de92007-09-25 17:54:57 -07001879
1880 memset(&priv->staging_rxon, 0, sizeof(priv->staging_rxon));
1881
1882 switch (priv->iw_mode) {
1883 case IEEE80211_IF_TYPE_AP:
1884 priv->staging_rxon.dev_type = RXON_DEV_TYPE_AP;
1885 break;
1886
1887 case IEEE80211_IF_TYPE_STA:
1888 priv->staging_rxon.dev_type = RXON_DEV_TYPE_ESS;
1889 priv->staging_rxon.filter_flags = RXON_FILTER_ACCEPT_GRP_MSK;
1890 break;
1891
1892 case IEEE80211_IF_TYPE_IBSS:
1893 priv->staging_rxon.dev_type = RXON_DEV_TYPE_IBSS;
1894 priv->staging_rxon.flags = RXON_FLG_SHORT_PREAMBLE_MSK;
1895 priv->staging_rxon.filter_flags = RXON_FILTER_BCON_AWARE_MSK |
1896 RXON_FILTER_ACCEPT_GRP_MSK;
1897 break;
1898
1899 case IEEE80211_IF_TYPE_MNTR:
1900 priv->staging_rxon.dev_type = RXON_DEV_TYPE_SNIFFER;
1901 priv->staging_rxon.filter_flags = RXON_FILTER_PROMISC_MSK |
1902 RXON_FILTER_CTL2HOST_MSK | RXON_FILTER_ACCEPT_GRP_MSK;
1903 break;
Tomas Winkler69dc5d92008-03-25 16:33:41 -07001904 default:
1905 IWL_ERROR("Unsupported interface type %d\n", priv->iw_mode);
1906 break;
Zhu Yib481de92007-09-25 17:54:57 -07001907 }
1908
1909#if 0
1910 /* TODO: Figure out when short_preamble would be set and cache from
1911 * that */
1912 if (!hw_to_local(priv->hw)->short_preamble)
1913 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_PREAMBLE_MSK;
1914 else
1915 priv->staging_rxon.flags |= RXON_FLG_SHORT_PREAMBLE_MSK;
1916#endif
1917
Assaf Krauss8622e702008-03-21 13:53:43 -07001918 ch_info = iwl_get_channel_info(priv, priv->band,
Zhu Yib481de92007-09-25 17:54:57 -07001919 le16_to_cpu(priv->staging_rxon.channel));
1920
1921 if (!ch_info)
1922 ch_info = &priv->channel_info[0];
1923
1924 /*
1925 * in some case A channels are all non IBSS
1926 * in this case force B/G channel
1927 */
1928 if ((priv->iw_mode == IEEE80211_IF_TYPE_IBSS) &&
1929 !(is_channel_ibss(ch_info)))
1930 ch_info = &priv->channel_info[0];
1931
1932 priv->staging_rxon.channel = cpu_to_le16(ch_info->channel);
Johannes Berg8318d782008-01-24 19:38:38 +01001933 priv->band = ch_info->band;
Zhu Yib481de92007-09-25 17:54:57 -07001934
Johannes Berg8318d782008-01-24 19:38:38 +01001935 iwl4965_set_flags_for_phymode(priv, priv->band);
Zhu Yib481de92007-09-25 17:54:57 -07001936
1937 priv->staging_rxon.ofdm_basic_rates =
1938 (IWL_OFDM_RATES_MASK >> IWL_FIRST_OFDM_RATE) & 0xFF;
1939 priv->staging_rxon.cck_basic_rates =
1940 (IWL_CCK_RATES_MASK >> IWL_FIRST_CCK_RATE) & 0xF;
1941
1942 priv->staging_rxon.flags &= ~(RXON_FLG_CHANNEL_MODE_MIXED_MSK |
1943 RXON_FLG_CHANNEL_MODE_PURE_40_MSK);
1944 memcpy(priv->staging_rxon.node_addr, priv->mac_addr, ETH_ALEN);
1945 memcpy(priv->staging_rxon.wlap_bssid_addr, priv->mac_addr, ETH_ALEN);
1946 priv->staging_rxon.ofdm_ht_single_stream_basic_rates = 0xff;
1947 priv->staging_rxon.ofdm_ht_dual_stream_basic_rates = 0xff;
1948 iwl4965_set_rxon_chain(priv);
1949}
1950
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001951static int iwl4965_set_mode(struct iwl_priv *priv, int mode)
Zhu Yib481de92007-09-25 17:54:57 -07001952{
Zhu Yib481de92007-09-25 17:54:57 -07001953 if (mode == IEEE80211_IF_TYPE_IBSS) {
Assaf Kraussbf85ea42008-03-14 10:38:49 -07001954 const struct iwl_channel_info *ch_info;
Zhu Yib481de92007-09-25 17:54:57 -07001955
Assaf Krauss8622e702008-03-21 13:53:43 -07001956 ch_info = iwl_get_channel_info(priv,
Johannes Berg8318d782008-01-24 19:38:38 +01001957 priv->band,
Zhu Yib481de92007-09-25 17:54:57 -07001958 le16_to_cpu(priv->staging_rxon.channel));
1959
1960 if (!ch_info || !is_channel_ibss(ch_info)) {
1961 IWL_ERROR("channel %d not IBSS channel\n",
1962 le16_to_cpu(priv->staging_rxon.channel));
1963 return -EINVAL;
1964 }
1965 }
1966
Zhu Yib481de92007-09-25 17:54:57 -07001967 priv->iw_mode = mode;
1968
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001969 iwl4965_connection_init_rx_config(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001970 memcpy(priv->staging_rxon.node_addr, priv->mac_addr, ETH_ALEN);
1971
Assaf Kraussbf85ea42008-03-14 10:38:49 -07001972 iwlcore_clear_stations_table(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001973
Mohamed Abbasfde35712007-11-29 11:10:15 +08001974 /* dont commit rxon if rf-kill is on*/
Tomas Winklerfee12472008-04-03 16:05:21 -07001975 if (!iwl_is_ready_rf(priv))
Mohamed Abbasfde35712007-11-29 11:10:15 +08001976 return -EAGAIN;
1977
1978 cancel_delayed_work(&priv->scan_check);
1979 if (iwl4965_scan_cancel_timeout(priv, 100)) {
1980 IWL_WARNING("Aborted scan still in progress after 100ms\n");
1981 IWL_DEBUG_MAC80211("leaving - scan abort failed.\n");
1982 return -EAGAIN;
1983 }
1984
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001985 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001986
1987 return 0;
1988}
1989
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001990static void iwl4965_build_tx_cmd_hwcrypto(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07001991 struct ieee80211_tx_control *ctl,
Tomas Winkler857485c2008-03-21 13:53:44 -07001992 struct iwl_cmd *cmd,
Zhu Yib481de92007-09-25 17:54:57 -07001993 struct sk_buff *skb_frag,
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07001994 int sta_id)
Zhu Yib481de92007-09-25 17:54:57 -07001995{
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07001996 struct iwl4965_hw_key *keyinfo = &priv->stations[sta_id].keyinfo;
Emmanuel Grumbach6974e362008-04-14 21:16:06 -07001997 struct iwl_wep_key *wepkey;
1998 int keyidx = 0;
1999
2000 BUG_ON(ctl->key_idx > 3);
Zhu Yib481de92007-09-25 17:54:57 -07002001
2002 switch (keyinfo->alg) {
2003 case ALG_CCMP:
2004 cmd->cmd.tx.sec_ctl = TX_CMD_SEC_CCM;
2005 memcpy(cmd->cmd.tx.key, keyinfo->key, keyinfo->keylen);
Max Stepanov8236e182008-03-12 16:58:48 -07002006 if (ctl->flags & IEEE80211_TXCTL_AMPDU)
2007 cmd->cmd.tx.tx_flags |= TX_CMD_FLG_AGG_CCMP_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07002008 IWL_DEBUG_TX("tx_cmd with aes hwcrypto\n");
2009 break;
2010
2011 case ALG_TKIP:
Zhu Yib481de92007-09-25 17:54:57 -07002012 cmd->cmd.tx.sec_ctl = TX_CMD_SEC_TKIP;
Emmanuel Grumbach2bc75082008-03-19 16:41:45 -07002013 ieee80211_get_tkip_key(keyinfo->conf, skb_frag,
2014 IEEE80211_TKIP_P2_KEY, cmd->cmd.tx.key);
2015 IWL_DEBUG_TX("tx_cmd with tkip hwcrypto\n");
Zhu Yib481de92007-09-25 17:54:57 -07002016 break;
2017
2018 case ALG_WEP:
Emmanuel Grumbach6974e362008-04-14 21:16:06 -07002019 wepkey = &priv->wep_keys[ctl->key_idx];
2020 cmd->cmd.tx.sec_ctl = 0;
2021 if (priv->default_wep_key) {
2022 /* the WEP key was sent as static */
2023 keyidx = ctl->key_idx;
2024 memcpy(&cmd->cmd.tx.key[3], wepkey->key,
2025 wepkey->key_size);
2026 if (wepkey->key_size == WEP_KEY_LEN_128)
2027 cmd->cmd.tx.sec_ctl |= TX_CMD_SEC_KEY128;
2028 } else {
Emmanuel Grumbach0211ddd2008-04-14 21:16:07 -07002029 /* the WEP key was sent as dynamic */
2030 keyidx = keyinfo->keyidx;
2031 memcpy(&cmd->cmd.tx.key[3], keyinfo->key,
2032 keyinfo->keylen);
2033 if (keyinfo->keylen == WEP_KEY_LEN_128)
2034 cmd->cmd.tx.sec_ctl |= TX_CMD_SEC_KEY128;
Emmanuel Grumbach6974e362008-04-14 21:16:06 -07002035 }
Zhu Yib481de92007-09-25 17:54:57 -07002036
Emmanuel Grumbach6974e362008-04-14 21:16:06 -07002037 cmd->cmd.tx.sec_ctl |= (TX_CMD_SEC_WEP |
2038 (keyidx & TX_CMD_SEC_MSK) << TX_CMD_SEC_SHIFT);
Zhu Yib481de92007-09-25 17:54:57 -07002039
2040 IWL_DEBUG_TX("Configuring packet for WEP encryption "
Emmanuel Grumbach6974e362008-04-14 21:16:06 -07002041 "with key %d\n", keyidx);
Zhu Yib481de92007-09-25 17:54:57 -07002042 break;
2043
Zhu Yib481de92007-09-25 17:54:57 -07002044 default:
2045 printk(KERN_ERR "Unknown encode alg %d\n", keyinfo->alg);
2046 break;
2047 }
2048}
2049
2050/*
2051 * handle build REPLY_TX command notification.
2052 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002053static void iwl4965_build_tx_cmd_basic(struct iwl_priv *priv,
Tomas Winkler857485c2008-03-21 13:53:44 -07002054 struct iwl_cmd *cmd,
Zhu Yib481de92007-09-25 17:54:57 -07002055 struct ieee80211_tx_control *ctrl,
2056 struct ieee80211_hdr *hdr,
2057 int is_unicast, u8 std_id)
2058{
2059 __le16 *qc;
2060 u16 fc = le16_to_cpu(hdr->frame_control);
2061 __le32 tx_flags = cmd->cmd.tx.tx_flags;
2062
2063 cmd->cmd.tx.stop_time.life_time = TX_CMD_LIFE_TIME_INFINITE;
2064 if (!(ctrl->flags & IEEE80211_TXCTL_NO_ACK)) {
2065 tx_flags |= TX_CMD_FLG_ACK_MSK;
2066 if ((fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_MGMT)
2067 tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
2068 if (ieee80211_is_probe_response(fc) &&
2069 !(le16_to_cpu(hdr->seq_ctrl) & 0xf))
2070 tx_flags |= TX_CMD_FLG_TSF_MSK;
2071 } else {
2072 tx_flags &= (~TX_CMD_FLG_ACK_MSK);
2073 tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
2074 }
2075
Tomas Winkler87e4f7d2008-01-14 17:46:16 -08002076 if (ieee80211_is_back_request(fc))
2077 tx_flags |= TX_CMD_FLG_ACK_MSK | TX_CMD_FLG_IMM_BA_RSP_MASK;
2078
2079
Zhu Yib481de92007-09-25 17:54:57 -07002080 cmd->cmd.tx.sta_id = std_id;
2081 if (ieee80211_get_morefrag(hdr))
2082 tx_flags |= TX_CMD_FLG_MORE_FRAG_MSK;
2083
2084 qc = ieee80211_get_qos_ctrl(hdr);
2085 if (qc) {
2086 cmd->cmd.tx.tid_tspec = (u8) (le16_to_cpu(*qc) & 0xf);
2087 tx_flags &= ~TX_CMD_FLG_SEQ_CTL_MSK;
2088 } else
2089 tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
2090
2091 if (ctrl->flags & IEEE80211_TXCTL_USE_RTS_CTS) {
2092 tx_flags |= TX_CMD_FLG_RTS_MSK;
2093 tx_flags &= ~TX_CMD_FLG_CTS_MSK;
2094 } else if (ctrl->flags & IEEE80211_TXCTL_USE_CTS_PROTECT) {
2095 tx_flags &= ~TX_CMD_FLG_RTS_MSK;
2096 tx_flags |= TX_CMD_FLG_CTS_MSK;
2097 }
2098
2099 if ((tx_flags & TX_CMD_FLG_RTS_MSK) || (tx_flags & TX_CMD_FLG_CTS_MSK))
2100 tx_flags |= TX_CMD_FLG_FULL_TXOP_PROT_MSK;
2101
2102 tx_flags &= ~(TX_CMD_FLG_ANT_SEL_MSK);
2103 if ((fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_MGMT) {
2104 if ((fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_ASSOC_REQ ||
2105 (fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_REASSOC_REQ)
Ian Schrambc434dd2007-10-25 17:15:29 +08002106 cmd->cmd.tx.timeout.pm_frame_timeout = cpu_to_le16(3);
Zhu Yib481de92007-09-25 17:54:57 -07002107 else
Ian Schrambc434dd2007-10-25 17:15:29 +08002108 cmd->cmd.tx.timeout.pm_frame_timeout = cpu_to_le16(2);
Mohamed Abbasab53d8a2008-03-25 16:33:36 -07002109 } else {
Zhu Yib481de92007-09-25 17:54:57 -07002110 cmd->cmd.tx.timeout.pm_frame_timeout = 0;
Mohamed Abbasab53d8a2008-03-25 16:33:36 -07002111 }
Zhu Yib481de92007-09-25 17:54:57 -07002112
2113 cmd->cmd.tx.driver_txop = 0;
2114 cmd->cmd.tx.tx_flags = tx_flags;
2115 cmd->cmd.tx.next_frame_len = 0;
2116}
Tomas Winkler19758be2008-03-12 16:58:51 -07002117static void iwl_update_tx_stats(struct iwl_priv *priv, u16 fc, u16 len)
2118{
2119 /* 0 - mgmt, 1 - cnt, 2 - data */
2120 int idx = (fc & IEEE80211_FCTL_FTYPE) >> 2;
2121 priv->tx_stats[idx].cnt++;
2122 priv->tx_stats[idx].bytes += len;
2123}
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002124/**
2125 * iwl4965_get_sta_id - Find station's index within station table
2126 *
2127 * If new IBSS station, create new entry in station table
2128 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002129static int iwl4965_get_sta_id(struct iwl_priv *priv,
Ben Cahill9fbab512007-11-29 11:09:47 +08002130 struct ieee80211_hdr *hdr)
Zhu Yib481de92007-09-25 17:54:57 -07002131{
2132 int sta_id;
2133 u16 fc = le16_to_cpu(hdr->frame_control);
Joe Perches0795af52007-10-03 17:59:30 -07002134 DECLARE_MAC_BUF(mac);
Zhu Yib481de92007-09-25 17:54:57 -07002135
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002136 /* If this frame is broadcast or management, use broadcast station id */
Zhu Yib481de92007-09-25 17:54:57 -07002137 if (((fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_DATA) ||
2138 is_multicast_ether_addr(hdr->addr1))
2139 return priv->hw_setting.bcast_sta_id;
2140
2141 switch (priv->iw_mode) {
2142
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002143 /* If we are a client station in a BSS network, use the special
2144 * AP station entry (that's the only station we communicate with) */
Zhu Yib481de92007-09-25 17:54:57 -07002145 case IEEE80211_IF_TYPE_STA:
2146 return IWL_AP_ID;
2147
2148 /* If we are an AP, then find the station, or use BCAST */
2149 case IEEE80211_IF_TYPE_AP:
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002150 sta_id = iwl4965_hw_find_station(priv, hdr->addr1);
Zhu Yib481de92007-09-25 17:54:57 -07002151 if (sta_id != IWL_INVALID_STATION)
2152 return sta_id;
2153 return priv->hw_setting.bcast_sta_id;
2154
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002155 /* If this frame is going out to an IBSS network, find the station,
2156 * or create a new station table entry */
Zhu Yib481de92007-09-25 17:54:57 -07002157 case IEEE80211_IF_TYPE_IBSS:
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002158 sta_id = iwl4965_hw_find_station(priv, hdr->addr1);
Zhu Yib481de92007-09-25 17:54:57 -07002159 if (sta_id != IWL_INVALID_STATION)
2160 return sta_id;
2161
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002162 /* Create new station table entry */
Ron Rindjunsky67d62032007-11-26 16:14:40 +02002163 sta_id = iwl4965_add_station_flags(priv, hdr->addr1,
2164 0, CMD_ASYNC, NULL);
Zhu Yib481de92007-09-25 17:54:57 -07002165
2166 if (sta_id != IWL_INVALID_STATION)
2167 return sta_id;
2168
Joe Perches0795af52007-10-03 17:59:30 -07002169 IWL_DEBUG_DROP("Station %s not in station map. "
Zhu Yib481de92007-09-25 17:54:57 -07002170 "Defaulting to broadcast...\n",
Joe Perches0795af52007-10-03 17:59:30 -07002171 print_mac(mac, hdr->addr1));
Tomas Winkler0a6857e2008-03-12 16:58:49 -07002172 iwl_print_hex_dump(IWL_DL_DROP, (u8 *) hdr, sizeof(*hdr));
Zhu Yib481de92007-09-25 17:54:57 -07002173 return priv->hw_setting.bcast_sta_id;
2174
2175 default:
Ian Schram01ebd062007-10-25 17:15:22 +08002176 IWL_WARNING("Unknown mode of operation: %d", priv->iw_mode);
Zhu Yib481de92007-09-25 17:54:57 -07002177 return priv->hw_setting.bcast_sta_id;
2178 }
2179}
2180
2181/*
2182 * start REPLY_TX command process
2183 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002184static int iwl4965_tx_skb(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07002185 struct sk_buff *skb, struct ieee80211_tx_control *ctl)
2186{
2187 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002188 struct iwl4965_tfd_frame *tfd;
Zhu Yib481de92007-09-25 17:54:57 -07002189 u32 *control_flags;
2190 int txq_id = ctl->queue;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002191 struct iwl4965_tx_queue *txq = NULL;
2192 struct iwl4965_queue *q = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07002193 dma_addr_t phys_addr;
2194 dma_addr_t txcmd_phys;
Tomas Winkler87e4f7d2008-01-14 17:46:16 -08002195 dma_addr_t scratch_phys;
Tomas Winkler857485c2008-03-21 13:53:44 -07002196 struct iwl_cmd *out_cmd = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07002197 u16 len, idx, len_org;
2198 u8 id, hdr_len, unicast;
2199 u8 sta_id;
2200 u16 seq_number = 0;
2201 u16 fc;
2202 __le16 *qc;
2203 u8 wait_write_ptr = 0;
2204 unsigned long flags;
2205 int rc;
2206
2207 spin_lock_irqsave(&priv->lock, flags);
Tomas Winklerfee12472008-04-03 16:05:21 -07002208 if (iwl_is_rfkill(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07002209 IWL_DEBUG_DROP("Dropping - RF KILL\n");
2210 goto drop_unlock;
2211 }
2212
Johannes Berg32bfd352007-12-19 01:31:26 +01002213 if (!priv->vif) {
2214 IWL_DEBUG_DROP("Dropping - !priv->vif\n");
Zhu Yib481de92007-09-25 17:54:57 -07002215 goto drop_unlock;
2216 }
2217
Johannes Berg8318d782008-01-24 19:38:38 +01002218 if ((ctl->tx_rate->hw_value & 0xFF) == IWL_INVALID_RATE) {
Zhu Yib481de92007-09-25 17:54:57 -07002219 IWL_ERROR("ERROR: No TX rate available.\n");
2220 goto drop_unlock;
2221 }
2222
2223 unicast = !is_multicast_ether_addr(hdr->addr1);
2224 id = 0;
2225
2226 fc = le16_to_cpu(hdr->frame_control);
2227
Tomas Winkler0a6857e2008-03-12 16:58:49 -07002228#ifdef CONFIG_IWLWIFI_DEBUG
Zhu Yib481de92007-09-25 17:54:57 -07002229 if (ieee80211_is_auth(fc))
2230 IWL_DEBUG_TX("Sending AUTH frame\n");
2231 else if (ieee80211_is_assoc_request(fc))
2232 IWL_DEBUG_TX("Sending ASSOC frame\n");
2233 else if (ieee80211_is_reassoc_request(fc))
2234 IWL_DEBUG_TX("Sending REASSOC frame\n");
2235#endif
2236
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08002237 /* drop all data frame if we are not associated */
Gregory Greenman76f39152008-01-23 10:15:21 -08002238 if (((fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_DATA) &&
Tomas Winkler3109ece2008-03-28 16:33:35 -07002239 (!iwl_is_associated(priv) ||
Reinette Chatrea6477242008-02-14 10:40:28 -08002240 ((priv->iw_mode == IEEE80211_IF_TYPE_STA) && !priv->assoc_id) ||
Gregory Greenman76f39152008-01-23 10:15:21 -08002241 !priv->assoc_station_added)) {
Tomas Winkler3109ece2008-03-28 16:33:35 -07002242 IWL_DEBUG_DROP("Dropping - !iwl_is_associated\n");
Zhu Yib481de92007-09-25 17:54:57 -07002243 goto drop_unlock;
2244 }
2245
2246 spin_unlock_irqrestore(&priv->lock, flags);
2247
2248 hdr_len = ieee80211_get_hdrlen(fc);
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002249
2250 /* Find (or create) index into station table for destination station */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002251 sta_id = iwl4965_get_sta_id(priv, hdr);
Zhu Yib481de92007-09-25 17:54:57 -07002252 if (sta_id == IWL_INVALID_STATION) {
Joe Perches0795af52007-10-03 17:59:30 -07002253 DECLARE_MAC_BUF(mac);
2254
2255 IWL_DEBUG_DROP("Dropping - INVALID STATION: %s\n",
2256 print_mac(mac, hdr->addr1));
Zhu Yib481de92007-09-25 17:54:57 -07002257 goto drop;
2258 }
2259
2260 IWL_DEBUG_RATE("station Id %d\n", sta_id);
2261
2262 qc = ieee80211_get_qos_ctrl(hdr);
2263 if (qc) {
2264 u8 tid = (u8)(le16_to_cpu(*qc) & 0xf);
2265 seq_number = priv->stations[sta_id].tid[tid].seq_number &
2266 IEEE80211_SCTL_SEQ;
2267 hdr->seq_ctrl = cpu_to_le16(seq_number) |
2268 (hdr->seq_ctrl &
2269 __constant_cpu_to_le16(IEEE80211_SCTL_FRAG));
2270 seq_number += 0x10;
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08002271#ifdef CONFIG_IWL4965_HT
Zhu Yib481de92007-09-25 17:54:57 -07002272 /* aggregation is on for this <sta,tid> */
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02002273 if (ctl->flags & IEEE80211_TXCTL_AMPDU)
Zhu Yib481de92007-09-25 17:54:57 -07002274 txq_id = priv->stations[sta_id].tid[tid].agg.txq_id;
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02002275 priv->stations[sta_id].tid[tid].tfds_in_queue++;
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08002276#endif /* CONFIG_IWL4965_HT */
Zhu Yib481de92007-09-25 17:54:57 -07002277 }
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002278
2279 /* Descriptor for chosen Tx queue */
Zhu Yib481de92007-09-25 17:54:57 -07002280 txq = &priv->txq[txq_id];
2281 q = &txq->q;
2282
2283 spin_lock_irqsave(&priv->lock, flags);
2284
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002285 /* Set up first empty TFD within this queue's circular TFD buffer */
Tomas Winklerfc4b6852007-10-25 17:15:24 +08002286 tfd = &txq->bd[q->write_ptr];
Zhu Yib481de92007-09-25 17:54:57 -07002287 memset(tfd, 0, sizeof(*tfd));
2288 control_flags = (u32 *) tfd;
Tomas Winklerfc4b6852007-10-25 17:15:24 +08002289 idx = get_cmd_index(q, q->write_ptr, 0);
Zhu Yib481de92007-09-25 17:54:57 -07002290
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002291 /* Set up driver data for this TFD */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002292 memset(&(txq->txb[q->write_ptr]), 0, sizeof(struct iwl4965_tx_info));
Tomas Winklerfc4b6852007-10-25 17:15:24 +08002293 txq->txb[q->write_ptr].skb[0] = skb;
2294 memcpy(&(txq->txb[q->write_ptr].status.control),
Zhu Yib481de92007-09-25 17:54:57 -07002295 ctl, sizeof(struct ieee80211_tx_control));
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002296
2297 /* Set up first empty entry in queue's array of Tx/cmd buffers */
Zhu Yib481de92007-09-25 17:54:57 -07002298 out_cmd = &txq->cmd[idx];
2299 memset(&out_cmd->hdr, 0, sizeof(out_cmd->hdr));
2300 memset(&out_cmd->cmd.tx, 0, sizeof(out_cmd->cmd.tx));
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002301
2302 /*
2303 * Set up the Tx-command (not MAC!) header.
2304 * Store the chosen Tx queue and TFD index within the sequence field;
2305 * after Tx, uCode's Tx response will return this value so driver can
2306 * locate the frame within the tx queue and do post-tx processing.
2307 */
Zhu Yib481de92007-09-25 17:54:57 -07002308 out_cmd->hdr.cmd = REPLY_TX;
2309 out_cmd->hdr.sequence = cpu_to_le16((u16)(QUEUE_TO_SEQ(txq_id) |
Tomas Winklerfc4b6852007-10-25 17:15:24 +08002310 INDEX_TO_SEQ(q->write_ptr)));
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002311
2312 /* Copy MAC header from skb into command buffer */
Zhu Yib481de92007-09-25 17:54:57 -07002313 memcpy(out_cmd->cmd.tx.hdr, hdr, hdr_len);
2314
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002315 /*
2316 * Use the first empty entry in this queue's command buffer array
2317 * to contain the Tx command and MAC header concatenated together
2318 * (payload data will be in another buffer).
2319 * Size of this varies, due to varying MAC header length.
2320 * If end is not dword aligned, we'll have 2 extra bytes at the end
2321 * of the MAC header (device reads on dword boundaries).
2322 * We'll tell device about this padding later.
2323 */
Zhu Yib481de92007-09-25 17:54:57 -07002324 len = priv->hw_setting.tx_cmd_len +
Tomas Winkler857485c2008-03-21 13:53:44 -07002325 sizeof(struct iwl_cmd_header) + hdr_len;
Zhu Yib481de92007-09-25 17:54:57 -07002326
2327 len_org = len;
2328 len = (len + 3) & ~3;
2329
2330 if (len_org != len)
2331 len_org = 1;
2332 else
2333 len_org = 0;
2334
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002335 /* Physical address of this Tx command's header (not MAC header!),
2336 * within command buffer array. */
Tomas Winkler857485c2008-03-21 13:53:44 -07002337 txcmd_phys = txq->dma_addr_cmd + sizeof(struct iwl_cmd) * idx +
2338 offsetof(struct iwl_cmd, hdr);
Zhu Yib481de92007-09-25 17:54:57 -07002339
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002340 /* Add buffer containing Tx command and MAC(!) header to TFD's
2341 * first entry */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002342 iwl4965_hw_txq_attach_buf_to_tfd(priv, tfd, txcmd_phys, len);
Zhu Yib481de92007-09-25 17:54:57 -07002343
2344 if (!(ctl->flags & IEEE80211_TXCTL_DO_NOT_ENCRYPT))
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07002345 iwl4965_build_tx_cmd_hwcrypto(priv, ctl, out_cmd, skb, sta_id);
Zhu Yib481de92007-09-25 17:54:57 -07002346
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002347 /* Set up TFD's 2nd entry to point directly to remainder of skb,
2348 * if any (802.11 null frames have no payload). */
Zhu Yib481de92007-09-25 17:54:57 -07002349 len = skb->len - hdr_len;
2350 if (len) {
2351 phys_addr = pci_map_single(priv->pci_dev, skb->data + hdr_len,
2352 len, PCI_DMA_TODEVICE);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002353 iwl4965_hw_txq_attach_buf_to_tfd(priv, tfd, phys_addr, len);
Zhu Yib481de92007-09-25 17:54:57 -07002354 }
2355
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002356 /* Tell 4965 about any 2-byte padding after MAC header */
Zhu Yib481de92007-09-25 17:54:57 -07002357 if (len_org)
2358 out_cmd->cmd.tx.tx_flags |= TX_CMD_FLG_MH_PAD_MSK;
2359
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002360 /* Total # bytes to be transmitted */
Zhu Yib481de92007-09-25 17:54:57 -07002361 len = (u16)skb->len;
2362 out_cmd->cmd.tx.len = cpu_to_le16(len);
2363
2364 /* TODO need this for burst mode later on */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002365 iwl4965_build_tx_cmd_basic(priv, out_cmd, ctl, hdr, unicast, sta_id);
Zhu Yib481de92007-09-25 17:54:57 -07002366
2367 /* set is_hcca to 0; it probably will never be implemented */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002368 iwl4965_hw_build_tx_cmd_rate(priv, out_cmd, ctl, hdr, sta_id, 0);
Zhu Yib481de92007-09-25 17:54:57 -07002369
Tomas Winkler19758be2008-03-12 16:58:51 -07002370 iwl_update_tx_stats(priv, fc, len);
2371
Tomas Winkler857485c2008-03-21 13:53:44 -07002372 scratch_phys = txcmd_phys + sizeof(struct iwl_cmd_header) +
Tomas Winkler87e4f7d2008-01-14 17:46:16 -08002373 offsetof(struct iwl4965_tx_cmd, scratch);
2374 out_cmd->cmd.tx.dram_lsb_ptr = cpu_to_le32(scratch_phys);
2375 out_cmd->cmd.tx.dram_msb_ptr = iwl_get_dma_hi_address(scratch_phys);
2376
Zhu Yib481de92007-09-25 17:54:57 -07002377 if (!ieee80211_get_morefrag(hdr)) {
2378 txq->need_update = 1;
2379 if (qc) {
2380 u8 tid = (u8)(le16_to_cpu(*qc) & 0xf);
2381 priv->stations[sta_id].tid[tid].seq_number = seq_number;
2382 }
2383 } else {
2384 wait_write_ptr = 1;
2385 txq->need_update = 0;
2386 }
2387
Tomas Winkler0a6857e2008-03-12 16:58:49 -07002388 iwl_print_hex_dump(IWL_DL_TX, out_cmd->cmd.payload,
Zhu Yib481de92007-09-25 17:54:57 -07002389 sizeof(out_cmd->cmd.tx));
2390
Tomas Winkler0a6857e2008-03-12 16:58:49 -07002391 iwl_print_hex_dump(IWL_DL_TX, (u8 *)out_cmd->cmd.tx.hdr,
Zhu Yib481de92007-09-25 17:54:57 -07002392 ieee80211_get_hdrlen(fc));
2393
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002394 /* Set up entry for this TFD in Tx byte-count array */
Zhu Yib481de92007-09-25 17:54:57 -07002395 iwl4965_tx_queue_update_wr_ptr(priv, txq, len);
2396
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002397 /* Tell device the write index *just past* this latest filled TFD */
Tomas Winklerc54b6792008-03-06 17:36:53 -08002398 q->write_ptr = iwl_queue_inc_wrap(q->write_ptr, q->n_bd);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002399 rc = iwl4965_tx_queue_update_write_ptr(priv, txq);
Zhu Yib481de92007-09-25 17:54:57 -07002400 spin_unlock_irqrestore(&priv->lock, flags);
2401
2402 if (rc)
2403 return rc;
2404
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002405 if ((iwl4965_queue_space(q) < q->high_mark)
Zhu Yib481de92007-09-25 17:54:57 -07002406 && priv->mac80211_registered) {
2407 if (wait_write_ptr) {
2408 spin_lock_irqsave(&priv->lock, flags);
2409 txq->need_update = 1;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002410 iwl4965_tx_queue_update_write_ptr(priv, txq);
Zhu Yib481de92007-09-25 17:54:57 -07002411 spin_unlock_irqrestore(&priv->lock, flags);
2412 }
2413
2414 ieee80211_stop_queue(priv->hw, ctl->queue);
2415 }
2416
2417 return 0;
2418
2419drop_unlock:
2420 spin_unlock_irqrestore(&priv->lock, flags);
2421drop:
2422 return -1;
2423}
2424
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002425static void iwl4965_set_rate(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002426{
Johannes Berg8318d782008-01-24 19:38:38 +01002427 const struct ieee80211_supported_band *hw = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07002428 struct ieee80211_rate *rate;
2429 int i;
2430
Johannes Berg8318d782008-01-24 19:38:38 +01002431 hw = iwl4965_get_hw_mode(priv, priv->band);
Saleem Abdulrasoolc4ba9622007-11-18 23:59:08 -08002432 if (!hw) {
2433 IWL_ERROR("Failed to set rate: unable to get hw mode\n");
2434 return;
2435 }
Zhu Yib481de92007-09-25 17:54:57 -07002436
2437 priv->active_rate = 0;
2438 priv->active_rate_basic = 0;
2439
Johannes Berg8318d782008-01-24 19:38:38 +01002440 for (i = 0; i < hw->n_bitrates; i++) {
2441 rate = &(hw->bitrates[i]);
2442 if (rate->hw_value < IWL_RATE_COUNT)
2443 priv->active_rate |= (1 << rate->hw_value);
Zhu Yib481de92007-09-25 17:54:57 -07002444 }
2445
2446 IWL_DEBUG_RATE("Set active_rate = %0x, active_rate_basic = %0x\n",
2447 priv->active_rate, priv->active_rate_basic);
2448
2449 /*
2450 * If a basic rate is configured, then use it (adding IWL_RATE_1M_MASK)
2451 * otherwise set it to the default of all CCK rates and 6, 12, 24 for
2452 * OFDM
2453 */
2454 if (priv->active_rate_basic & IWL_CCK_BASIC_RATES_MASK)
2455 priv->staging_rxon.cck_basic_rates =
2456 ((priv->active_rate_basic &
2457 IWL_CCK_RATES_MASK) >> IWL_FIRST_CCK_RATE) & 0xF;
2458 else
2459 priv->staging_rxon.cck_basic_rates =
2460 (IWL_CCK_BASIC_RATES_MASK >> IWL_FIRST_CCK_RATE) & 0xF;
2461
2462 if (priv->active_rate_basic & IWL_OFDM_BASIC_RATES_MASK)
2463 priv->staging_rxon.ofdm_basic_rates =
2464 ((priv->active_rate_basic &
2465 (IWL_OFDM_BASIC_RATES_MASK | IWL_RATE_6M_MASK)) >>
2466 IWL_FIRST_OFDM_RATE) & 0xFF;
2467 else
2468 priv->staging_rxon.ofdm_basic_rates =
2469 (IWL_OFDM_BASIC_RATES_MASK >> IWL_FIRST_OFDM_RATE) & 0xFF;
2470}
2471
Mohamed Abbasad97edd22008-03-28 16:21:06 -07002472void iwl4965_radio_kill_sw(struct iwl_priv *priv, int disable_radio)
Zhu Yib481de92007-09-25 17:54:57 -07002473{
2474 unsigned long flags;
2475
2476 if (!!disable_radio == test_bit(STATUS_RF_KILL_SW, &priv->status))
2477 return;
2478
2479 IWL_DEBUG_RF_KILL("Manual SW RF KILL set to: RADIO %s\n",
2480 disable_radio ? "OFF" : "ON");
2481
2482 if (disable_radio) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002483 iwl4965_scan_cancel(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002484 /* FIXME: This is a workaround for AP */
2485 if (priv->iw_mode != IEEE80211_IF_TYPE_AP) {
2486 spin_lock_irqsave(&priv->lock, flags);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07002487 iwl_write32(priv, CSR_UCODE_DRV_GP1_SET,
Zhu Yib481de92007-09-25 17:54:57 -07002488 CSR_UCODE_SW_BIT_RFKILL);
2489 spin_unlock_irqrestore(&priv->lock, flags);
Mohamed Abbasad97edd22008-03-28 16:21:06 -07002490 /* call the host command only if no hw rf-kill set */
2491 if (!test_bit(STATUS_RF_KILL_HW, &priv->status))
2492 iwl4965_send_card_state(priv,
2493 CARD_STATE_CMD_DISABLE,
2494 0);
Zhu Yib481de92007-09-25 17:54:57 -07002495 set_bit(STATUS_RF_KILL_SW, &priv->status);
Mohamed Abbasad97edd22008-03-28 16:21:06 -07002496
2497 /* make sure mac80211 stop sending Tx frame */
2498 if (priv->mac80211_registered)
2499 ieee80211_stop_queues(priv->hw);
Zhu Yib481de92007-09-25 17:54:57 -07002500 }
2501 return;
2502 }
2503
2504 spin_lock_irqsave(&priv->lock, flags);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07002505 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
Zhu Yib481de92007-09-25 17:54:57 -07002506
2507 clear_bit(STATUS_RF_KILL_SW, &priv->status);
2508 spin_unlock_irqrestore(&priv->lock, flags);
2509
2510 /* wake up ucode */
2511 msleep(10);
2512
2513 spin_lock_irqsave(&priv->lock, flags);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07002514 iwl_read32(priv, CSR_UCODE_DRV_GP1);
2515 if (!iwl_grab_nic_access(priv))
2516 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002517 spin_unlock_irqrestore(&priv->lock, flags);
2518
2519 if (test_bit(STATUS_RF_KILL_HW, &priv->status)) {
2520 IWL_DEBUG_RF_KILL("Can not turn radio back on - "
2521 "disabled by HW switch\n");
2522 return;
2523 }
2524
2525 queue_work(priv->workqueue, &priv->restart);
2526 return;
2527}
2528
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002529void iwl4965_set_decrypted_flag(struct iwl_priv *priv, struct sk_buff *skb,
Zhu Yib481de92007-09-25 17:54:57 -07002530 u32 decrypt_res, struct ieee80211_rx_status *stats)
2531{
2532 u16 fc =
2533 le16_to_cpu(((struct ieee80211_hdr *)skb->data)->frame_control);
2534
2535 if (priv->active_rxon.filter_flags & RXON_FILTER_DIS_DECRYPT_MSK)
2536 return;
2537
2538 if (!(fc & IEEE80211_FCTL_PROTECTED))
2539 return;
2540
2541 IWL_DEBUG_RX("decrypt_res:0x%x\n", decrypt_res);
2542 switch (decrypt_res & RX_RES_STATUS_SEC_TYPE_MSK) {
2543 case RX_RES_STATUS_SEC_TYPE_TKIP:
Emmanuel Grumbach17e476b2008-03-19 16:41:42 -07002544 /* The uCode has got a bad phase 1 Key, pushes the packet.
2545 * Decryption will be done in SW. */
2546 if ((decrypt_res & RX_RES_STATUS_DECRYPT_TYPE_MSK) ==
2547 RX_RES_STATUS_BAD_KEY_TTAK)
2548 break;
2549
Zhu Yib481de92007-09-25 17:54:57 -07002550 if ((decrypt_res & RX_RES_STATUS_DECRYPT_TYPE_MSK) ==
2551 RX_RES_STATUS_BAD_ICV_MIC)
2552 stats->flag |= RX_FLAG_MMIC_ERROR;
2553 case RX_RES_STATUS_SEC_TYPE_WEP:
2554 case RX_RES_STATUS_SEC_TYPE_CCMP:
2555 if ((decrypt_res & RX_RES_STATUS_DECRYPT_TYPE_MSK) ==
2556 RX_RES_STATUS_DECRYPT_OK) {
2557 IWL_DEBUG_RX("hw decrypt successfully!!!\n");
2558 stats->flag |= RX_FLAG_DECRYPTED;
2559 }
2560 break;
2561
2562 default:
2563 break;
2564 }
2565}
2566
Zhu Yib481de92007-09-25 17:54:57 -07002567
2568#define IWL_PACKET_RETRY_TIME HZ
2569
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002570int iwl4965_is_duplicate_packet(struct iwl_priv *priv, struct ieee80211_hdr *header)
Zhu Yib481de92007-09-25 17:54:57 -07002571{
2572 u16 sc = le16_to_cpu(header->seq_ctrl);
2573 u16 seq = (sc & IEEE80211_SCTL_SEQ) >> 4;
2574 u16 frag = sc & IEEE80211_SCTL_FRAG;
2575 u16 *last_seq, *last_frag;
2576 unsigned long *last_time;
2577
2578 switch (priv->iw_mode) {
2579 case IEEE80211_IF_TYPE_IBSS:{
2580 struct list_head *p;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002581 struct iwl4965_ibss_seq *entry = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07002582 u8 *mac = header->addr2;
2583 int index = mac[5] & (IWL_IBSS_MAC_HASH_SIZE - 1);
2584
2585 __list_for_each(p, &priv->ibss_mac_hash[index]) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002586 entry = list_entry(p, struct iwl4965_ibss_seq, list);
Zhu Yib481de92007-09-25 17:54:57 -07002587 if (!compare_ether_addr(entry->mac, mac))
2588 break;
2589 }
2590 if (p == &priv->ibss_mac_hash[index]) {
2591 entry = kzalloc(sizeof(*entry), GFP_ATOMIC);
2592 if (!entry) {
Ian Schrambc434dd2007-10-25 17:15:29 +08002593 IWL_ERROR("Cannot malloc new mac entry\n");
Zhu Yib481de92007-09-25 17:54:57 -07002594 return 0;
2595 }
2596 memcpy(entry->mac, mac, ETH_ALEN);
2597 entry->seq_num = seq;
2598 entry->frag_num = frag;
2599 entry->packet_time = jiffies;
Ian Schrambc434dd2007-10-25 17:15:29 +08002600 list_add(&entry->list, &priv->ibss_mac_hash[index]);
Zhu Yib481de92007-09-25 17:54:57 -07002601 return 0;
2602 }
2603 last_seq = &entry->seq_num;
2604 last_frag = &entry->frag_num;
2605 last_time = &entry->packet_time;
2606 break;
2607 }
2608 case IEEE80211_IF_TYPE_STA:
2609 last_seq = &priv->last_seq_num;
2610 last_frag = &priv->last_frag_num;
2611 last_time = &priv->last_packet_time;
2612 break;
2613 default:
2614 return 0;
2615 }
2616 if ((*last_seq == seq) &&
2617 time_after(*last_time + IWL_PACKET_RETRY_TIME, jiffies)) {
2618 if (*last_frag == frag)
2619 goto drop;
2620 if (*last_frag + 1 != frag)
2621 /* out-of-order fragment */
2622 goto drop;
2623 } else
2624 *last_seq = seq;
2625
2626 *last_frag = frag;
2627 *last_time = jiffies;
2628 return 0;
2629
2630 drop:
2631 return 1;
2632}
2633
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08002634#ifdef CONFIG_IWL4965_SPECTRUM_MEASUREMENT
Zhu Yib481de92007-09-25 17:54:57 -07002635
2636#include "iwl-spectrum.h"
2637
2638#define BEACON_TIME_MASK_LOW 0x00FFFFFF
2639#define BEACON_TIME_MASK_HIGH 0xFF000000
2640#define TIME_UNIT 1024
2641
2642/*
2643 * extended beacon time format
2644 * time in usec will be changed into a 32-bit value in 8:24 format
2645 * the high 1 byte is the beacon counts
2646 * the lower 3 bytes is the time in usec within one beacon interval
2647 */
2648
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002649static u32 iwl4965_usecs_to_beacons(u32 usec, u32 beacon_interval)
Zhu Yib481de92007-09-25 17:54:57 -07002650{
2651 u32 quot;
2652 u32 rem;
2653 u32 interval = beacon_interval * 1024;
2654
2655 if (!interval || !usec)
2656 return 0;
2657
2658 quot = (usec / interval) & (BEACON_TIME_MASK_HIGH >> 24);
2659 rem = (usec % interval) & BEACON_TIME_MASK_LOW;
2660
2661 return (quot << 24) + rem;
2662}
2663
2664/* base is usually what we get from ucode with each received frame,
2665 * the same as HW timer counter counting down
2666 */
2667
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002668static __le32 iwl4965_add_beacon_time(u32 base, u32 addon, u32 beacon_interval)
Zhu Yib481de92007-09-25 17:54:57 -07002669{
2670 u32 base_low = base & BEACON_TIME_MASK_LOW;
2671 u32 addon_low = addon & BEACON_TIME_MASK_LOW;
2672 u32 interval = beacon_interval * TIME_UNIT;
2673 u32 res = (base & BEACON_TIME_MASK_HIGH) +
2674 (addon & BEACON_TIME_MASK_HIGH);
2675
2676 if (base_low > addon_low)
2677 res += base_low - addon_low;
2678 else if (base_low < addon_low) {
2679 res += interval + base_low - addon_low;
2680 res += (1 << 24);
2681 } else
2682 res += (1 << 24);
2683
2684 return cpu_to_le32(res);
2685}
2686
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002687static int iwl4965_get_measurement(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07002688 struct ieee80211_measurement_params *params,
2689 u8 type)
2690{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002691 struct iwl4965_spectrum_cmd spectrum;
2692 struct iwl4965_rx_packet *res;
Tomas Winkler857485c2008-03-21 13:53:44 -07002693 struct iwl_host_cmd cmd = {
Zhu Yib481de92007-09-25 17:54:57 -07002694 .id = REPLY_SPECTRUM_MEASUREMENT_CMD,
2695 .data = (void *)&spectrum,
2696 .meta.flags = CMD_WANT_SKB,
2697 };
2698 u32 add_time = le64_to_cpu(params->start_time);
2699 int rc;
2700 int spectrum_resp_status;
2701 int duration = le16_to_cpu(params->duration);
2702
Tomas Winkler3109ece2008-03-28 16:33:35 -07002703 if (iwl_is_associated(priv))
Zhu Yib481de92007-09-25 17:54:57 -07002704 add_time =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002705 iwl4965_usecs_to_beacons(
Zhu Yib481de92007-09-25 17:54:57 -07002706 le64_to_cpu(params->start_time) - priv->last_tsf,
2707 le16_to_cpu(priv->rxon_timing.beacon_interval));
2708
2709 memset(&spectrum, 0, sizeof(spectrum));
2710
2711 spectrum.channel_count = cpu_to_le16(1);
2712 spectrum.flags =
2713 RXON_FLG_TSF2HOST_MSK | RXON_FLG_ANT_A_MSK | RXON_FLG_DIS_DIV_MSK;
2714 spectrum.filter_flags = MEASUREMENT_FILTER_FLAG;
2715 cmd.len = sizeof(spectrum);
2716 spectrum.len = cpu_to_le16(cmd.len - sizeof(spectrum.len));
2717
Tomas Winkler3109ece2008-03-28 16:33:35 -07002718 if (iwl_is_associated(priv))
Zhu Yib481de92007-09-25 17:54:57 -07002719 spectrum.start_time =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002720 iwl4965_add_beacon_time(priv->last_beacon_time,
Zhu Yib481de92007-09-25 17:54:57 -07002721 add_time,
2722 le16_to_cpu(priv->rxon_timing.beacon_interval));
2723 else
2724 spectrum.start_time = 0;
2725
2726 spectrum.channels[0].duration = cpu_to_le32(duration * TIME_UNIT);
2727 spectrum.channels[0].channel = params->channel;
2728 spectrum.channels[0].type = type;
2729 if (priv->active_rxon.flags & RXON_FLG_BAND_24G_MSK)
2730 spectrum.flags |= RXON_FLG_BAND_24G_MSK |
2731 RXON_FLG_AUTO_DETECT_MSK | RXON_FLG_TGG_PROTECT_MSK;
2732
Tomas Winkler857485c2008-03-21 13:53:44 -07002733 rc = iwl_send_cmd_sync(priv, &cmd);
Zhu Yib481de92007-09-25 17:54:57 -07002734 if (rc)
2735 return rc;
2736
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002737 res = (struct iwl4965_rx_packet *)cmd.meta.u.skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07002738 if (res->hdr.flags & IWL_CMD_FAILED_MSK) {
2739 IWL_ERROR("Bad return from REPLY_RX_ON_ASSOC command\n");
2740 rc = -EIO;
2741 }
2742
2743 spectrum_resp_status = le16_to_cpu(res->u.spectrum.status);
2744 switch (spectrum_resp_status) {
2745 case 0: /* Command will be handled */
2746 if (res->u.spectrum.id != 0xff) {
2747 IWL_DEBUG_INFO
2748 ("Replaced existing measurement: %d\n",
2749 res->u.spectrum.id);
2750 priv->measurement_status &= ~MEASUREMENT_READY;
2751 }
2752 priv->measurement_status |= MEASUREMENT_ACTIVE;
2753 rc = 0;
2754 break;
2755
2756 case 1: /* Command will not be handled */
2757 rc = -EAGAIN;
2758 break;
2759 }
2760
2761 dev_kfree_skb_any(cmd.meta.u.skb);
2762
2763 return rc;
2764}
2765#endif
2766
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002767static void iwl4965_txstatus_to_ieee(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002768 struct iwl4965_tx_info *tx_sta)
Zhu Yib481de92007-09-25 17:54:57 -07002769{
2770
2771 tx_sta->status.ack_signal = 0;
2772 tx_sta->status.excessive_retries = 0;
2773 tx_sta->status.queue_length = 0;
2774 tx_sta->status.queue_number = 0;
2775
2776 if (in_interrupt())
2777 ieee80211_tx_status_irqsafe(priv->hw,
2778 tx_sta->skb[0], &(tx_sta->status));
2779 else
2780 ieee80211_tx_status(priv->hw,
2781 tx_sta->skb[0], &(tx_sta->status));
2782
2783 tx_sta->skb[0] = NULL;
2784}
2785
2786/**
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002787 * iwl4965_tx_queue_reclaim - Reclaim Tx queue entries already Tx'd
Zhu Yib481de92007-09-25 17:54:57 -07002788 *
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002789 * When FW advances 'R' index, all entries between old and new 'R' index
2790 * need to be reclaimed. As result, some free space forms. If there is
2791 * enough free space (> low mark), wake the stack that feeds us.
Zhu Yib481de92007-09-25 17:54:57 -07002792 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002793int iwl4965_tx_queue_reclaim(struct iwl_priv *priv, int txq_id, int index)
Zhu Yib481de92007-09-25 17:54:57 -07002794{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002795 struct iwl4965_tx_queue *txq = &priv->txq[txq_id];
2796 struct iwl4965_queue *q = &txq->q;
Zhu Yib481de92007-09-25 17:54:57 -07002797 int nfreed = 0;
2798
2799 if ((index >= q->n_bd) || (x2_queue_used(q, index) == 0)) {
2800 IWL_ERROR("Read index for DMA queue txq id (%d), index %d, "
2801 "is out of range [0-%d] %d %d.\n", txq_id,
Tomas Winklerfc4b6852007-10-25 17:15:24 +08002802 index, q->n_bd, q->write_ptr, q->read_ptr);
Zhu Yib481de92007-09-25 17:54:57 -07002803 return 0;
2804 }
2805
Tomas Winklerc54b6792008-03-06 17:36:53 -08002806 for (index = iwl_queue_inc_wrap(index, q->n_bd);
Tomas Winklerfc4b6852007-10-25 17:15:24 +08002807 q->read_ptr != index;
Tomas Winklerc54b6792008-03-06 17:36:53 -08002808 q->read_ptr = iwl_queue_inc_wrap(q->read_ptr, q->n_bd)) {
Zhu Yib481de92007-09-25 17:54:57 -07002809 if (txq_id != IWL_CMD_QUEUE_NUM) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002810 iwl4965_txstatus_to_ieee(priv,
Tomas Winklerfc4b6852007-10-25 17:15:24 +08002811 &(txq->txb[txq->q.read_ptr]));
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002812 iwl4965_hw_txq_free_tfd(priv, txq);
Zhu Yib481de92007-09-25 17:54:57 -07002813 } else if (nfreed > 1) {
2814 IWL_ERROR("HCMD skipped: index (%d) %d %d\n", index,
Tomas Winklerfc4b6852007-10-25 17:15:24 +08002815 q->write_ptr, q->read_ptr);
Zhu Yib481de92007-09-25 17:54:57 -07002816 queue_work(priv->workqueue, &priv->restart);
2817 }
2818 nfreed++;
2819 }
2820
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02002821/* if (iwl4965_queue_space(q) > q->low_mark && (txq_id >= 0) &&
Zhu Yib481de92007-09-25 17:54:57 -07002822 (txq_id != IWL_CMD_QUEUE_NUM) &&
2823 priv->mac80211_registered)
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02002824 ieee80211_wake_queue(priv->hw, txq_id); */
Zhu Yib481de92007-09-25 17:54:57 -07002825
2826
2827 return nfreed;
2828}
2829
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002830static int iwl4965_is_tx_success(u32 status)
Zhu Yib481de92007-09-25 17:54:57 -07002831{
2832 status &= TX_STATUS_MSK;
2833 return (status == TX_STATUS_SUCCESS)
2834 || (status == TX_STATUS_DIRECT_DONE);
2835}
2836
2837/******************************************************************************
2838 *
2839 * Generic RX handler implementations
2840 *
2841 ******************************************************************************/
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08002842#ifdef CONFIG_IWL4965_HT
Zhu Yib481de92007-09-25 17:54:57 -07002843
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002844static inline int iwl4965_get_ra_sta_id(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07002845 struct ieee80211_hdr *hdr)
2846{
2847 if (priv->iw_mode == IEEE80211_IF_TYPE_STA)
2848 return IWL_AP_ID;
2849 else {
2850 u8 *da = ieee80211_get_DA(hdr);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002851 return iwl4965_hw_find_station(priv, da);
Zhu Yib481de92007-09-25 17:54:57 -07002852 }
2853}
2854
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002855static struct ieee80211_hdr *iwl4965_tx_queue_get_hdr(
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002856 struct iwl_priv *priv, int txq_id, int idx)
Zhu Yib481de92007-09-25 17:54:57 -07002857{
2858 if (priv->txq[txq_id].txb[idx].skb[0])
2859 return (struct ieee80211_hdr *)priv->txq[txq_id].
2860 txb[idx].skb[0]->data;
2861 return NULL;
2862}
2863
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002864static inline u32 iwl4965_get_scd_ssn(struct iwl4965_tx_resp *tx_resp)
Zhu Yib481de92007-09-25 17:54:57 -07002865{
2866 __le32 *scd_ssn = (__le32 *)((u32 *)&tx_resp->status +
2867 tx_resp->frame_count);
2868 return le32_to_cpu(*scd_ssn) & MAX_SN;
2869
2870}
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002871
2872/**
2873 * iwl4965_tx_status_reply_tx - Handle Tx rspnse for frames in aggregation queue
2874 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002875static int iwl4965_tx_status_reply_tx(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002876 struct iwl4965_ht_agg *agg,
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02002877 struct iwl4965_tx_resp_agg *tx_resp,
Zhu Yib481de92007-09-25 17:54:57 -07002878 u16 start_idx)
2879{
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02002880 u16 status;
2881 struct agg_tx_status *frame_status = &tx_resp->status;
Zhu Yib481de92007-09-25 17:54:57 -07002882 struct ieee80211_tx_status *tx_status = NULL;
2883 struct ieee80211_hdr *hdr = NULL;
2884 int i, sh;
2885 int txq_id, idx;
2886 u16 seq;
2887
2888 if (agg->wait_for_ba)
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002889 IWL_DEBUG_TX_REPLY("got tx response w/o block-ack\n");
Zhu Yib481de92007-09-25 17:54:57 -07002890
2891 agg->frame_count = tx_resp->frame_count;
2892 agg->start_idx = start_idx;
2893 agg->rate_n_flags = le32_to_cpu(tx_resp->rate_n_flags);
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02002894 agg->bitmap = 0;
Zhu Yib481de92007-09-25 17:54:57 -07002895
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002896 /* # frames attempted by Tx command */
Zhu Yib481de92007-09-25 17:54:57 -07002897 if (agg->frame_count == 1) {
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002898 /* Only one frame was attempted; no block-ack will arrive */
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02002899 status = le16_to_cpu(frame_status[0].status);
2900 seq = le16_to_cpu(frame_status[0].sequence);
2901 idx = SEQ_TO_INDEX(seq);
2902 txq_id = SEQ_TO_QUEUE(seq);
Zhu Yib481de92007-09-25 17:54:57 -07002903
Zhu Yib481de92007-09-25 17:54:57 -07002904 /* FIXME: code repetition */
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02002905 IWL_DEBUG_TX_REPLY("FrameCnt = %d, StartIdx=%d idx=%d\n",
2906 agg->frame_count, agg->start_idx, idx);
Zhu Yib481de92007-09-25 17:54:57 -07002907
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02002908 tx_status = &(priv->txq[txq_id].txb[idx].status);
Zhu Yib481de92007-09-25 17:54:57 -07002909 tx_status->retry_count = tx_resp->failure_frame;
2910 tx_status->queue_number = status & 0xff;
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02002911 tx_status->queue_length = tx_resp->failure_rts;
2912 tx_status->control.flags &= ~IEEE80211_TXCTL_AMPDU;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002913 tx_status->flags = iwl4965_is_tx_success(status)?
Zhu Yib481de92007-09-25 17:54:57 -07002914 IEEE80211_TX_STATUS_ACK : 0;
Ron Rindjunsky4c424e42008-03-04 18:09:27 -08002915 iwl4965_hwrate_to_tx_control(priv,
2916 le32_to_cpu(tx_resp->rate_n_flags),
2917 &tx_status->control);
Zhu Yib481de92007-09-25 17:54:57 -07002918 /* FIXME: code repetition end */
2919
2920 IWL_DEBUG_TX_REPLY("1 Frame 0x%x failure :%d\n",
2921 status & 0xff, tx_resp->failure_frame);
2922 IWL_DEBUG_TX_REPLY("Rate Info rate_n_flags=%x\n",
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002923 iwl4965_hw_get_rate_n_flags(tx_resp->rate_n_flags));
Zhu Yib481de92007-09-25 17:54:57 -07002924
2925 agg->wait_for_ba = 0;
2926 } else {
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002927 /* Two or more frames were attempted; expect block-ack */
Zhu Yib481de92007-09-25 17:54:57 -07002928 u64 bitmap = 0;
2929 int start = agg->start_idx;
2930
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002931 /* Construct bit-map of pending frames within Tx window */
Zhu Yib481de92007-09-25 17:54:57 -07002932 for (i = 0; i < agg->frame_count; i++) {
2933 u16 sc;
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02002934 status = le16_to_cpu(frame_status[i].status);
2935 seq = le16_to_cpu(frame_status[i].sequence);
Zhu Yib481de92007-09-25 17:54:57 -07002936 idx = SEQ_TO_INDEX(seq);
2937 txq_id = SEQ_TO_QUEUE(seq);
2938
2939 if (status & (AGG_TX_STATE_FEW_BYTES_MSK |
2940 AGG_TX_STATE_ABORT_MSK))
2941 continue;
2942
2943 IWL_DEBUG_TX_REPLY("FrameCnt = %d, txq_id=%d idx=%d\n",
2944 agg->frame_count, txq_id, idx);
2945
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002946 hdr = iwl4965_tx_queue_get_hdr(priv, txq_id, idx);
Zhu Yib481de92007-09-25 17:54:57 -07002947
2948 sc = le16_to_cpu(hdr->seq_ctrl);
2949 if (idx != (SEQ_TO_SN(sc) & 0xff)) {
2950 IWL_ERROR("BUG_ON idx doesn't match seq control"
2951 " idx=%d, seq_idx=%d, seq=%d\n",
2952 idx, SEQ_TO_SN(sc),
2953 hdr->seq_ctrl);
2954 return -1;
2955 }
2956
2957 IWL_DEBUG_TX_REPLY("AGG Frame i=%d idx %d seq=%d\n",
2958 i, idx, SEQ_TO_SN(sc));
2959
2960 sh = idx - start;
2961 if (sh > 64) {
2962 sh = (start - idx) + 0xff;
2963 bitmap = bitmap << sh;
2964 sh = 0;
2965 start = idx;
2966 } else if (sh < -64)
2967 sh = 0xff - (start - idx);
2968 else if (sh < 0) {
2969 sh = start - idx;
2970 start = idx;
2971 bitmap = bitmap << sh;
2972 sh = 0;
2973 }
2974 bitmap |= (1 << sh);
2975 IWL_DEBUG_TX_REPLY("start=%d bitmap=0x%x\n",
2976 start, (u32)(bitmap & 0xFFFFFFFF));
2977 }
2978
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02002979 agg->bitmap = bitmap;
Zhu Yib481de92007-09-25 17:54:57 -07002980 agg->start_idx = start;
2981 agg->rate_n_flags = le32_to_cpu(tx_resp->rate_n_flags);
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02002982 IWL_DEBUG_TX_REPLY("Frames %d start_idx=%d bitmap=0x%llx\n",
Zhu Yib481de92007-09-25 17:54:57 -07002983 agg->frame_count, agg->start_idx,
John W. Linville06501d22008-04-01 17:38:47 -04002984 (unsigned long long)agg->bitmap);
Zhu Yib481de92007-09-25 17:54:57 -07002985
2986 if (bitmap)
2987 agg->wait_for_ba = 1;
2988 }
2989 return 0;
2990}
2991#endif
Zhu Yib481de92007-09-25 17:54:57 -07002992
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002993/**
2994 * iwl4965_rx_reply_tx - Handle standard (non-aggregation) Tx response
2995 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002996static void iwl4965_rx_reply_tx(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002997 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07002998{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002999 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07003000 u16 sequence = le16_to_cpu(pkt->hdr.sequence);
3001 int txq_id = SEQ_TO_QUEUE(sequence);
3002 int index = SEQ_TO_INDEX(sequence);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003003 struct iwl4965_tx_queue *txq = &priv->txq[txq_id];
Zhu Yib481de92007-09-25 17:54:57 -07003004 struct ieee80211_tx_status *tx_status;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003005 struct iwl4965_tx_resp *tx_resp = (void *)&pkt->u.raw[0];
Zhu Yib481de92007-09-25 17:54:57 -07003006 u32 status = le32_to_cpu(tx_resp->status);
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08003007#ifdef CONFIG_IWL4965_HT
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003008 int tid = MAX_TID_COUNT, sta_id = IWL_INVALID_STATION;
3009 struct ieee80211_hdr *hdr;
3010 __le16 *qc;
Zhu Yib481de92007-09-25 17:54:57 -07003011#endif
3012
3013 if ((index >= txq->q.n_bd) || (x2_queue_used(&txq->q, index) == 0)) {
3014 IWL_ERROR("Read index for DMA queue txq_id (%d) index %d "
3015 "is out of range [0-%d] %d %d\n", txq_id,
Tomas Winklerfc4b6852007-10-25 17:15:24 +08003016 index, txq->q.n_bd, txq->q.write_ptr,
3017 txq->q.read_ptr);
Zhu Yib481de92007-09-25 17:54:57 -07003018 return;
3019 }
3020
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08003021#ifdef CONFIG_IWL4965_HT
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003022 hdr = iwl4965_tx_queue_get_hdr(priv, txq_id, index);
3023 qc = ieee80211_get_qos_ctrl(hdr);
Zhu Yib481de92007-09-25 17:54:57 -07003024
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003025 if (qc)
Zhu Yib481de92007-09-25 17:54:57 -07003026 tid = le16_to_cpu(*qc) & 0xf;
3027
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003028 sta_id = iwl4965_get_ra_sta_id(priv, hdr);
3029 if (txq->sched_retry && unlikely(sta_id == IWL_INVALID_STATION)) {
3030 IWL_ERROR("Station not known\n");
3031 return;
3032 }
3033
3034 if (txq->sched_retry) {
3035 const u32 scd_ssn = iwl4965_get_scd_ssn(tx_resp);
3036 struct iwl4965_ht_agg *agg = NULL;
3037
3038 if (!qc)
Zhu Yib481de92007-09-25 17:54:57 -07003039 return;
Zhu Yib481de92007-09-25 17:54:57 -07003040
3041 agg = &priv->stations[sta_id].tid[tid].agg;
3042
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003043 iwl4965_tx_status_reply_tx(priv, agg,
3044 (struct iwl4965_tx_resp_agg *)tx_resp, index);
Zhu Yib481de92007-09-25 17:54:57 -07003045
3046 if ((tx_resp->frame_count == 1) &&
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003047 !iwl4965_is_tx_success(status)) {
Zhu Yib481de92007-09-25 17:54:57 -07003048 /* TODO: send BAR */
3049 }
3050
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003051 if (txq->q.read_ptr != (scd_ssn & 0xff)) {
3052 int freed;
Tomas Winklerc54b6792008-03-06 17:36:53 -08003053 index = iwl_queue_dec_wrap(scd_ssn & 0xff, txq->q.n_bd);
Zhu Yib481de92007-09-25 17:54:57 -07003054 IWL_DEBUG_TX_REPLY("Retry scheduler reclaim scd_ssn "
3055 "%d index %d\n", scd_ssn , index);
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003056 freed = iwl4965_tx_queue_reclaim(priv, txq_id, index);
3057 priv->stations[sta_id].tid[tid].tfds_in_queue -= freed;
3058
3059 if (iwl4965_queue_space(&txq->q) > txq->q.low_mark &&
3060 txq_id >= 0 && priv->mac80211_registered &&
3061 agg->state != IWL_EMPTYING_HW_QUEUE_DELBA)
3062 ieee80211_wake_queue(priv->hw, txq_id);
3063
3064 iwl4965_check_empty_hw_queue(priv, sta_id, tid, txq_id);
Zhu Yib481de92007-09-25 17:54:57 -07003065 }
3066 } else {
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08003067#endif /* CONFIG_IWL4965_HT */
Tomas Winklerfc4b6852007-10-25 17:15:24 +08003068 tx_status = &(txq->txb[txq->q.read_ptr].status);
Zhu Yib481de92007-09-25 17:54:57 -07003069
3070 tx_status->retry_count = tx_resp->failure_frame;
3071 tx_status->queue_number = status;
3072 tx_status->queue_length = tx_resp->bt_kill_count;
3073 tx_status->queue_length |= tx_resp->failure_rts;
Zhu Yib481de92007-09-25 17:54:57 -07003074 tx_status->flags =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003075 iwl4965_is_tx_success(status) ? IEEE80211_TX_STATUS_ACK : 0;
Ron Rindjunsky4c424e42008-03-04 18:09:27 -08003076 iwl4965_hwrate_to_tx_control(priv, le32_to_cpu(tx_resp->rate_n_flags),
3077 &tx_status->control);
Zhu Yib481de92007-09-25 17:54:57 -07003078
Zhu Yib481de92007-09-25 17:54:57 -07003079 IWL_DEBUG_TX("Tx queue %d Status %s (0x%08x) rate_n_flags 0x%x "
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003080 "retries %d\n", txq_id, iwl4965_get_tx_fail_reason(status),
Zhu Yib481de92007-09-25 17:54:57 -07003081 status, le32_to_cpu(tx_resp->rate_n_flags),
3082 tx_resp->failure_frame);
3083
3084 IWL_DEBUG_TX_REPLY("Tx queue reclaim %d\n", index);
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003085 if (index != -1) {
3086 int freed = iwl4965_tx_queue_reclaim(priv, txq_id, index);
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08003087#ifdef CONFIG_IWL4965_HT
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003088 if (tid != MAX_TID_COUNT)
3089 priv->stations[sta_id].tid[tid].tfds_in_queue -= freed;
3090 if (iwl4965_queue_space(&txq->q) > txq->q.low_mark &&
3091 (txq_id >= 0) &&
3092 priv->mac80211_registered)
3093 ieee80211_wake_queue(priv->hw, txq_id);
3094 if (tid != MAX_TID_COUNT)
3095 iwl4965_check_empty_hw_queue(priv, sta_id, tid, txq_id);
3096#endif
Zhu Yib481de92007-09-25 17:54:57 -07003097 }
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003098#ifdef CONFIG_IWL4965_HT
3099 }
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08003100#endif /* CONFIG_IWL4965_HT */
Zhu Yib481de92007-09-25 17:54:57 -07003101
3102 if (iwl_check_bits(status, TX_ABORT_REQUIRED_MSK))
3103 IWL_ERROR("TODO: Implement Tx ABORT REQUIRED!!!\n");
3104}
3105
3106
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003107static void iwl4965_rx_reply_alive(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003108 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003109{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003110 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
3111 struct iwl4965_alive_resp *palive;
Zhu Yib481de92007-09-25 17:54:57 -07003112 struct delayed_work *pwork;
3113
3114 palive = &pkt->u.alive_frame;
3115
3116 IWL_DEBUG_INFO("Alive ucode status 0x%08X revision "
3117 "0x%01X 0x%01X\n",
3118 palive->is_valid, palive->ver_type,
3119 palive->ver_subtype);
3120
3121 if (palive->ver_subtype == INITIALIZE_SUBTYPE) {
3122 IWL_DEBUG_INFO("Initialization Alive received.\n");
3123 memcpy(&priv->card_alive_init,
3124 &pkt->u.alive_frame,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003125 sizeof(struct iwl4965_init_alive_resp));
Zhu Yib481de92007-09-25 17:54:57 -07003126 pwork = &priv->init_alive_start;
3127 } else {
3128 IWL_DEBUG_INFO("Runtime Alive received.\n");
3129 memcpy(&priv->card_alive, &pkt->u.alive_frame,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003130 sizeof(struct iwl4965_alive_resp));
Zhu Yib481de92007-09-25 17:54:57 -07003131 pwork = &priv->alive_start;
3132 }
3133
3134 /* We delay the ALIVE response by 5ms to
3135 * give the HW RF Kill time to activate... */
3136 if (palive->is_valid == UCODE_VALID_OK)
3137 queue_delayed_work(priv->workqueue, pwork,
3138 msecs_to_jiffies(5));
3139 else
3140 IWL_WARNING("uCode did not respond OK.\n");
3141}
3142
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003143static void iwl4965_rx_reply_add_sta(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003144 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003145{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003146 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07003147
3148 IWL_DEBUG_RX("Received REPLY_ADD_STA: 0x%02X\n", pkt->u.status);
3149 return;
3150}
3151
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003152static void iwl4965_rx_reply_error(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003153 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003154{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003155 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07003156
3157 IWL_ERROR("Error Reply type 0x%08X cmd %s (0x%02X) "
3158 "seq 0x%04X ser 0x%08X\n",
3159 le32_to_cpu(pkt->u.err_resp.error_type),
3160 get_cmd_string(pkt->u.err_resp.cmd_id),
3161 pkt->u.err_resp.cmd_id,
3162 le16_to_cpu(pkt->u.err_resp.bad_cmd_seq_num),
3163 le32_to_cpu(pkt->u.err_resp.error_info));
3164}
3165
3166#define TX_STATUS_ENTRY(x) case TX_STATUS_FAIL_ ## x: return #x
3167
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003168static void iwl4965_rx_csa(struct iwl_priv *priv, struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003169{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003170 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
3171 struct iwl4965_rxon_cmd *rxon = (void *)&priv->active_rxon;
3172 struct iwl4965_csa_notification *csa = &(pkt->u.csa_notif);
Zhu Yib481de92007-09-25 17:54:57 -07003173 IWL_DEBUG_11H("CSA notif: channel %d, status %d\n",
3174 le16_to_cpu(csa->channel), le32_to_cpu(csa->status));
3175 rxon->channel = csa->channel;
3176 priv->staging_rxon.channel = csa->channel;
3177}
3178
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003179static void iwl4965_rx_spectrum_measure_notif(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003180 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003181{
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08003182#ifdef CONFIG_IWL4965_SPECTRUM_MEASUREMENT
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003183 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
3184 struct iwl4965_spectrum_notification *report = &(pkt->u.spectrum_notif);
Zhu Yib481de92007-09-25 17:54:57 -07003185
3186 if (!report->state) {
3187 IWL_DEBUG(IWL_DL_11H | IWL_DL_INFO,
3188 "Spectrum Measure Notification: Start\n");
3189 return;
3190 }
3191
3192 memcpy(&priv->measure_report, report, sizeof(*report));
3193 priv->measurement_status |= MEASUREMENT_READY;
3194#endif
3195}
3196
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003197static void iwl4965_rx_pm_sleep_notif(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003198 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003199{
Tomas Winkler0a6857e2008-03-12 16:58:49 -07003200#ifdef CONFIG_IWLWIFI_DEBUG
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003201 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
3202 struct iwl4965_sleep_notification *sleep = &(pkt->u.sleep_notif);
Zhu Yib481de92007-09-25 17:54:57 -07003203 IWL_DEBUG_RX("sleep mode: %d, src: %d\n",
3204 sleep->pm_sleep_mode, sleep->pm_wakeup_src);
3205#endif
3206}
3207
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003208static void iwl4965_rx_pm_debug_statistics_notif(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003209 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003210{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003211 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07003212 IWL_DEBUG_RADIO("Dumping %d bytes of unhandled "
3213 "notification for %s:\n",
3214 le32_to_cpu(pkt->len), get_cmd_string(pkt->hdr.cmd));
Tomas Winkler0a6857e2008-03-12 16:58:49 -07003215 iwl_print_hex_dump(IWL_DL_RADIO, pkt->u.raw, le32_to_cpu(pkt->len));
Zhu Yib481de92007-09-25 17:54:57 -07003216}
3217
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003218static void iwl4965_bg_beacon_update(struct work_struct *work)
Zhu Yib481de92007-09-25 17:54:57 -07003219{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003220 struct iwl_priv *priv =
3221 container_of(work, struct iwl_priv, beacon_update);
Zhu Yib481de92007-09-25 17:54:57 -07003222 struct sk_buff *beacon;
3223
3224 /* Pull updated AP beacon from mac80211. will fail if not in AP mode */
Johannes Berg32bfd352007-12-19 01:31:26 +01003225 beacon = ieee80211_beacon_get(priv->hw, priv->vif, NULL);
Zhu Yib481de92007-09-25 17:54:57 -07003226
3227 if (!beacon) {
3228 IWL_ERROR("update beacon failed\n");
3229 return;
3230 }
3231
3232 mutex_lock(&priv->mutex);
3233 /* new beacon skb is allocated every time; dispose previous.*/
3234 if (priv->ibss_beacon)
3235 dev_kfree_skb(priv->ibss_beacon);
3236
3237 priv->ibss_beacon = beacon;
3238 mutex_unlock(&priv->mutex);
3239
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003240 iwl4965_send_beacon_cmd(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003241}
3242
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003243static void iwl4965_rx_beacon_notif(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003244 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003245{
Tomas Winkler0a6857e2008-03-12 16:58:49 -07003246#ifdef CONFIG_IWLWIFI_DEBUG
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003247 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
3248 struct iwl4965_beacon_notif *beacon = &(pkt->u.beacon_status);
3249 u8 rate = iwl4965_hw_get_rate(beacon->beacon_notify_hdr.rate_n_flags);
Zhu Yib481de92007-09-25 17:54:57 -07003250
3251 IWL_DEBUG_RX("beacon status %x retries %d iss %d "
3252 "tsf %d %d rate %d\n",
3253 le32_to_cpu(beacon->beacon_notify_hdr.status) & TX_STATUS_MSK,
3254 beacon->beacon_notify_hdr.failure_frame,
3255 le32_to_cpu(beacon->ibss_mgr_status),
3256 le32_to_cpu(beacon->high_tsf),
3257 le32_to_cpu(beacon->low_tsf), rate);
3258#endif
3259
3260 if ((priv->iw_mode == IEEE80211_IF_TYPE_AP) &&
3261 (!test_bit(STATUS_EXIT_PENDING, &priv->status)))
3262 queue_work(priv->workqueue, &priv->beacon_update);
3263}
3264
3265/* Service response to REPLY_SCAN_CMD (0x80) */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003266static void iwl4965_rx_reply_scan(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003267 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003268{
Tomas Winkler0a6857e2008-03-12 16:58:49 -07003269#ifdef CONFIG_IWLWIFI_DEBUG
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003270 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
3271 struct iwl4965_scanreq_notification *notif =
3272 (struct iwl4965_scanreq_notification *)pkt->u.raw;
Zhu Yib481de92007-09-25 17:54:57 -07003273
3274 IWL_DEBUG_RX("Scan request status = 0x%x\n", notif->status);
3275#endif
3276}
3277
3278/* Service SCAN_START_NOTIFICATION (0x82) */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003279static void iwl4965_rx_scan_start_notif(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003280 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003281{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003282 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
3283 struct iwl4965_scanstart_notification *notif =
3284 (struct iwl4965_scanstart_notification *)pkt->u.raw;
Zhu Yib481de92007-09-25 17:54:57 -07003285 priv->scan_start_tsf = le32_to_cpu(notif->tsf_low);
3286 IWL_DEBUG_SCAN("Scan start: "
3287 "%d [802.11%s] "
3288 "(TSF: 0x%08X:%08X) - %d (beacon timer %u)\n",
3289 notif->channel,
3290 notif->band ? "bg" : "a",
3291 notif->tsf_high,
3292 notif->tsf_low, notif->status, notif->beacon_timer);
3293}
3294
3295/* Service SCAN_RESULTS_NOTIFICATION (0x83) */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003296static void iwl4965_rx_scan_results_notif(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003297 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003298{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003299 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
3300 struct iwl4965_scanresults_notification *notif =
3301 (struct iwl4965_scanresults_notification *)pkt->u.raw;
Zhu Yib481de92007-09-25 17:54:57 -07003302
3303 IWL_DEBUG_SCAN("Scan ch.res: "
3304 "%d [802.11%s] "
3305 "(TSF: 0x%08X:%08X) - %d "
3306 "elapsed=%lu usec (%dms since last)\n",
3307 notif->channel,
3308 notif->band ? "bg" : "a",
3309 le32_to_cpu(notif->tsf_high),
3310 le32_to_cpu(notif->tsf_low),
3311 le32_to_cpu(notif->statistics[0]),
3312 le32_to_cpu(notif->tsf_low) - priv->scan_start_tsf,
3313 jiffies_to_msecs(elapsed_jiffies
3314 (priv->last_scan_jiffies, jiffies)));
3315
3316 priv->last_scan_jiffies = jiffies;
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08003317 priv->next_scan_jiffies = 0;
Zhu Yib481de92007-09-25 17:54:57 -07003318}
3319
3320/* Service SCAN_COMPLETE_NOTIFICATION (0x84) */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003321static void iwl4965_rx_scan_complete_notif(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003322 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003323{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003324 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
3325 struct iwl4965_scancomplete_notification *scan_notif = (void *)pkt->u.raw;
Zhu Yib481de92007-09-25 17:54:57 -07003326
3327 IWL_DEBUG_SCAN("Scan complete: %d channels (TSF 0x%08X:%08X) - %d\n",
3328 scan_notif->scanned_channels,
3329 scan_notif->tsf_low,
3330 scan_notif->tsf_high, scan_notif->status);
3331
3332 /* The HW is no longer scanning */
3333 clear_bit(STATUS_SCAN_HW, &priv->status);
3334
3335 /* The scan completion notification came in, so kill that timer... */
3336 cancel_delayed_work(&priv->scan_check);
3337
3338 IWL_DEBUG_INFO("Scan pass on %sGHz took %dms\n",
3339 (priv->scan_bands == 2) ? "2.4" : "5.2",
3340 jiffies_to_msecs(elapsed_jiffies
3341 (priv->scan_pass_start, jiffies)));
3342
3343 /* Remove this scanned band from the list
3344 * of pending bands to scan */
3345 priv->scan_bands--;
3346
3347 /* If a request to abort was given, or the scan did not succeed
3348 * then we reset the scan state machine and terminate,
3349 * re-queuing another scan if one has been requested */
3350 if (test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
3351 IWL_DEBUG_INFO("Aborted scan completed.\n");
3352 clear_bit(STATUS_SCAN_ABORTING, &priv->status);
3353 } else {
3354 /* If there are more bands on this scan pass reschedule */
3355 if (priv->scan_bands > 0)
3356 goto reschedule;
3357 }
3358
3359 priv->last_scan_jiffies = jiffies;
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08003360 priv->next_scan_jiffies = 0;
Zhu Yib481de92007-09-25 17:54:57 -07003361 IWL_DEBUG_INFO("Setting scan to off\n");
3362
3363 clear_bit(STATUS_SCANNING, &priv->status);
3364
3365 IWL_DEBUG_INFO("Scan took %dms\n",
3366 jiffies_to_msecs(elapsed_jiffies(priv->scan_start, jiffies)));
3367
3368 queue_work(priv->workqueue, &priv->scan_completed);
3369
3370 return;
3371
3372reschedule:
3373 priv->scan_pass_start = jiffies;
3374 queue_work(priv->workqueue, &priv->request_scan);
3375}
3376
3377/* Handle notification from uCode that card's power state is changing
3378 * due to software, hardware, or critical temperature RFKILL */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003379static void iwl4965_rx_card_state_notif(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003380 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003381{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003382 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07003383 u32 flags = le32_to_cpu(pkt->u.card_state_notif.flags);
3384 unsigned long status = priv->status;
3385
3386 IWL_DEBUG_RF_KILL("Card state received: HW:%s SW:%s\n",
3387 (flags & HW_CARD_DISABLED) ? "Kill" : "On",
3388 (flags & SW_CARD_DISABLED) ? "Kill" : "On");
3389
3390 if (flags & (SW_CARD_DISABLED | HW_CARD_DISABLED |
3391 RF_CARD_DISABLED)) {
3392
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003393 iwl_write32(priv, CSR_UCODE_DRV_GP1_SET,
Zhu Yib481de92007-09-25 17:54:57 -07003394 CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
3395
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003396 if (!iwl_grab_nic_access(priv)) {
3397 iwl_write_direct32(
Zhu Yib481de92007-09-25 17:54:57 -07003398 priv, HBUS_TARG_MBX_C,
3399 HBUS_TARG_MBX_C_REG_BIT_CMD_BLOCKED);
3400
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003401 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003402 }
3403
3404 if (!(flags & RXON_CARD_DISABLED)) {
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003405 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR,
Zhu Yib481de92007-09-25 17:54:57 -07003406 CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003407 if (!iwl_grab_nic_access(priv)) {
3408 iwl_write_direct32(
Zhu Yib481de92007-09-25 17:54:57 -07003409 priv, HBUS_TARG_MBX_C,
3410 HBUS_TARG_MBX_C_REG_BIT_CMD_BLOCKED);
3411
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003412 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003413 }
3414 }
3415
3416 if (flags & RF_CARD_DISABLED) {
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003417 iwl_write32(priv, CSR_UCODE_DRV_GP1_SET,
Zhu Yib481de92007-09-25 17:54:57 -07003418 CSR_UCODE_DRV_GP1_REG_BIT_CT_KILL_EXIT);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003419 iwl_read32(priv, CSR_UCODE_DRV_GP1);
3420 if (!iwl_grab_nic_access(priv))
3421 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003422 }
3423 }
3424
3425 if (flags & HW_CARD_DISABLED)
3426 set_bit(STATUS_RF_KILL_HW, &priv->status);
3427 else
3428 clear_bit(STATUS_RF_KILL_HW, &priv->status);
3429
3430
3431 if (flags & SW_CARD_DISABLED)
3432 set_bit(STATUS_RF_KILL_SW, &priv->status);
3433 else
3434 clear_bit(STATUS_RF_KILL_SW, &priv->status);
3435
3436 if (!(flags & RXON_CARD_DISABLED))
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003437 iwl4965_scan_cancel(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003438
3439 if ((test_bit(STATUS_RF_KILL_HW, &status) !=
3440 test_bit(STATUS_RF_KILL_HW, &priv->status)) ||
3441 (test_bit(STATUS_RF_KILL_SW, &status) !=
3442 test_bit(STATUS_RF_KILL_SW, &priv->status)))
3443 queue_work(priv->workqueue, &priv->rf_kill);
3444 else
3445 wake_up_interruptible(&priv->wait_command_queue);
3446}
3447
3448/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003449 * iwl4965_setup_rx_handlers - Initialize Rx handler callbacks
Zhu Yib481de92007-09-25 17:54:57 -07003450 *
3451 * Setup the RX handlers for each of the reply types sent from the uCode
3452 * to the host.
3453 *
3454 * This function chains into the hardware specific files for them to setup
3455 * any hardware specific handlers as well.
3456 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003457static void iwl4965_setup_rx_handlers(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003458{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003459 priv->rx_handlers[REPLY_ALIVE] = iwl4965_rx_reply_alive;
3460 priv->rx_handlers[REPLY_ADD_STA] = iwl4965_rx_reply_add_sta;
3461 priv->rx_handlers[REPLY_ERROR] = iwl4965_rx_reply_error;
3462 priv->rx_handlers[CHANNEL_SWITCH_NOTIFICATION] = iwl4965_rx_csa;
Zhu Yib481de92007-09-25 17:54:57 -07003463 priv->rx_handlers[SPECTRUM_MEASURE_NOTIFICATION] =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003464 iwl4965_rx_spectrum_measure_notif;
3465 priv->rx_handlers[PM_SLEEP_NOTIFICATION] = iwl4965_rx_pm_sleep_notif;
Zhu Yib481de92007-09-25 17:54:57 -07003466 priv->rx_handlers[PM_DEBUG_STATISTIC_NOTIFIC] =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003467 iwl4965_rx_pm_debug_statistics_notif;
3468 priv->rx_handlers[BEACON_NOTIFICATION] = iwl4965_rx_beacon_notif;
Zhu Yib481de92007-09-25 17:54:57 -07003469
Ben Cahill9fbab512007-11-29 11:09:47 +08003470 /*
3471 * The same handler is used for both the REPLY to a discrete
3472 * statistics request from the host as well as for the periodic
3473 * statistics notifications (after received beacons) from the uCode.
Zhu Yib481de92007-09-25 17:54:57 -07003474 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003475 priv->rx_handlers[REPLY_STATISTICS_CMD] = iwl4965_hw_rx_statistics;
3476 priv->rx_handlers[STATISTICS_NOTIFICATION] = iwl4965_hw_rx_statistics;
Zhu Yib481de92007-09-25 17:54:57 -07003477
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003478 priv->rx_handlers[REPLY_SCAN_CMD] = iwl4965_rx_reply_scan;
3479 priv->rx_handlers[SCAN_START_NOTIFICATION] = iwl4965_rx_scan_start_notif;
Zhu Yib481de92007-09-25 17:54:57 -07003480 priv->rx_handlers[SCAN_RESULTS_NOTIFICATION] =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003481 iwl4965_rx_scan_results_notif;
Zhu Yib481de92007-09-25 17:54:57 -07003482 priv->rx_handlers[SCAN_COMPLETE_NOTIFICATION] =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003483 iwl4965_rx_scan_complete_notif;
3484 priv->rx_handlers[CARD_STATE_NOTIFICATION] = iwl4965_rx_card_state_notif;
3485 priv->rx_handlers[REPLY_TX] = iwl4965_rx_reply_tx;
Zhu Yib481de92007-09-25 17:54:57 -07003486
Ben Cahill9fbab512007-11-29 11:09:47 +08003487 /* Set up hardware specific Rx handlers */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003488 iwl4965_hw_rx_handler_setup(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003489}
3490
3491/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003492 * iwl4965_tx_cmd_complete - Pull unused buffers off the queue and reclaim them
Zhu Yib481de92007-09-25 17:54:57 -07003493 * @rxb: Rx buffer to reclaim
3494 *
3495 * If an Rx buffer has an async callback associated with it the callback
3496 * will be executed. The attached skb (if present) will only be freed
3497 * if the callback returns 1
3498 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003499static void iwl4965_tx_cmd_complete(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003500 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003501{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003502 struct iwl4965_rx_packet *pkt = (struct iwl4965_rx_packet *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07003503 u16 sequence = le16_to_cpu(pkt->hdr.sequence);
3504 int txq_id = SEQ_TO_QUEUE(sequence);
3505 int index = SEQ_TO_INDEX(sequence);
3506 int huge = sequence & SEQ_HUGE_FRAME;
3507 int cmd_index;
Tomas Winkler857485c2008-03-21 13:53:44 -07003508 struct iwl_cmd *cmd;
Zhu Yib481de92007-09-25 17:54:57 -07003509
3510 /* If a Tx command is being handled and it isn't in the actual
3511 * command queue then there a command routing bug has been introduced
3512 * in the queue management code. */
3513 if (txq_id != IWL_CMD_QUEUE_NUM)
3514 IWL_ERROR("Error wrong command queue %d command id 0x%X\n",
3515 txq_id, pkt->hdr.cmd);
3516 BUG_ON(txq_id != IWL_CMD_QUEUE_NUM);
3517
3518 cmd_index = get_cmd_index(&priv->txq[IWL_CMD_QUEUE_NUM].q, index, huge);
3519 cmd = &priv->txq[IWL_CMD_QUEUE_NUM].cmd[cmd_index];
3520
3521 /* Input error checking is done when commands are added to queue. */
3522 if (cmd->meta.flags & CMD_WANT_SKB) {
3523 cmd->meta.source->u.skb = rxb->skb;
3524 rxb->skb = NULL;
3525 } else if (cmd->meta.u.callback &&
3526 !cmd->meta.u.callback(priv, cmd, rxb->skb))
3527 rxb->skb = NULL;
3528
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003529 iwl4965_tx_queue_reclaim(priv, txq_id, index);
Zhu Yib481de92007-09-25 17:54:57 -07003530
3531 if (!(cmd->meta.flags & CMD_ASYNC)) {
3532 clear_bit(STATUS_HCMD_ACTIVE, &priv->status);
3533 wake_up_interruptible(&priv->wait_command_queue);
3534 }
3535}
3536
3537/************************** RX-FUNCTIONS ****************************/
3538/*
3539 * Rx theory of operation
3540 *
Ben Cahill9fbab512007-11-29 11:09:47 +08003541 * Driver allocates a circular buffer of Receive Buffer Descriptors (RBDs),
3542 * each of which point to Receive Buffers to be filled by 4965. These get
3543 * used not only for Rx frames, but for any command response or notification
3544 * from the 4965. The driver and 4965 manage the Rx buffers by means
3545 * of indexes into the circular buffer.
Zhu Yib481de92007-09-25 17:54:57 -07003546 *
3547 * Rx Queue Indexes
3548 * The host/firmware share two index registers for managing the Rx buffers.
3549 *
3550 * The READ index maps to the first position that the firmware may be writing
3551 * to -- the driver can read up to (but not including) this position and get
3552 * good data.
3553 * The READ index is managed by the firmware once the card is enabled.
3554 *
3555 * The WRITE index maps to the last position the driver has read from -- the
3556 * position preceding WRITE is the last slot the firmware can place a packet.
3557 *
3558 * The queue is empty (no good data) if WRITE = READ - 1, and is full if
3559 * WRITE = READ.
3560 *
Ben Cahill9fbab512007-11-29 11:09:47 +08003561 * During initialization, the host sets up the READ queue position to the first
Zhu Yib481de92007-09-25 17:54:57 -07003562 * INDEX position, and WRITE to the last (READ - 1 wrapped)
3563 *
Ben Cahill9fbab512007-11-29 11:09:47 +08003564 * When the firmware places a packet in a buffer, it will advance the READ index
Zhu Yib481de92007-09-25 17:54:57 -07003565 * and fire the RX interrupt. The driver can then query the READ index and
3566 * process as many packets as possible, moving the WRITE index forward as it
3567 * resets the Rx queue buffers with new memory.
3568 *
3569 * The management in the driver is as follows:
3570 * + A list of pre-allocated SKBs is stored in iwl->rxq->rx_free. When
3571 * iwl->rxq->free_count drops to or below RX_LOW_WATERMARK, work is scheduled
Ian Schram01ebd062007-10-25 17:15:22 +08003572 * to replenish the iwl->rxq->rx_free.
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003573 * + In iwl4965_rx_replenish (scheduled) if 'processed' != 'read' then the
Zhu Yib481de92007-09-25 17:54:57 -07003574 * iwl->rxq is replenished and the READ INDEX is updated (updating the
3575 * 'processed' and 'read' driver indexes as well)
3576 * + A received packet is processed and handed to the kernel network stack,
3577 * detached from the iwl->rxq. The driver 'processed' index is updated.
3578 * + The Host/Firmware iwl->rxq is replenished at tasklet time from the rx_free
3579 * list. If there are no allocated buffers in iwl->rxq->rx_free, the READ
3580 * INDEX is not incremented and iwl->status(RX_STALLED) is set. If there
3581 * were enough free buffers and RX_STALLED is set it is cleared.
3582 *
3583 *
3584 * Driver sequence:
3585 *
Ben Cahill9fbab512007-11-29 11:09:47 +08003586 * iwl4965_rx_queue_alloc() Allocates rx_free
3587 * iwl4965_rx_replenish() Replenishes rx_free list from rx_used, and calls
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003588 * iwl4965_rx_queue_restock
Ben Cahill9fbab512007-11-29 11:09:47 +08003589 * iwl4965_rx_queue_restock() Moves available buffers from rx_free into Rx
Zhu Yib481de92007-09-25 17:54:57 -07003590 * queue, updates firmware pointers, and updates
3591 * the WRITE index. If insufficient rx_free buffers
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003592 * are available, schedules iwl4965_rx_replenish
Zhu Yib481de92007-09-25 17:54:57 -07003593 *
3594 * -- enable interrupts --
Ben Cahill9fbab512007-11-29 11:09:47 +08003595 * ISR - iwl4965_rx() Detach iwl4965_rx_mem_buffers from pool up to the
Zhu Yib481de92007-09-25 17:54:57 -07003596 * READ INDEX, detaching the SKB from the pool.
3597 * Moves the packet buffer from queue to rx_used.
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003598 * Calls iwl4965_rx_queue_restock to refill any empty
Zhu Yib481de92007-09-25 17:54:57 -07003599 * slots.
3600 * ...
3601 *
3602 */
3603
3604/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003605 * iwl4965_rx_queue_space - Return number of free slots available in queue.
Zhu Yib481de92007-09-25 17:54:57 -07003606 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003607static int iwl4965_rx_queue_space(const struct iwl4965_rx_queue *q)
Zhu Yib481de92007-09-25 17:54:57 -07003608{
3609 int s = q->read - q->write;
3610 if (s <= 0)
3611 s += RX_QUEUE_SIZE;
3612 /* keep some buffer to not confuse full and empty queue */
3613 s -= 2;
3614 if (s < 0)
3615 s = 0;
3616 return s;
3617}
3618
3619/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003620 * iwl4965_rx_queue_update_write_ptr - Update the write pointer for the RX queue
Zhu Yib481de92007-09-25 17:54:57 -07003621 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003622int iwl4965_rx_queue_update_write_ptr(struct iwl_priv *priv, struct iwl4965_rx_queue *q)
Zhu Yib481de92007-09-25 17:54:57 -07003623{
3624 u32 reg = 0;
3625 int rc = 0;
3626 unsigned long flags;
3627
3628 spin_lock_irqsave(&q->lock, flags);
3629
3630 if (q->need_update == 0)
3631 goto exit_unlock;
3632
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003633 /* If power-saving is in use, make sure device is awake */
Zhu Yib481de92007-09-25 17:54:57 -07003634 if (test_bit(STATUS_POWER_PMI, &priv->status)) {
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003635 reg = iwl_read32(priv, CSR_UCODE_DRV_GP1);
Zhu Yib481de92007-09-25 17:54:57 -07003636
3637 if (reg & CSR_UCODE_DRV_GP1_BIT_MAC_SLEEP) {
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003638 iwl_set_bit(priv, CSR_GP_CNTRL,
Zhu Yib481de92007-09-25 17:54:57 -07003639 CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
3640 goto exit_unlock;
3641 }
3642
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003643 rc = iwl_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003644 if (rc)
3645 goto exit_unlock;
3646
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003647 /* Device expects a multiple of 8 */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003648 iwl_write_direct32(priv, FH_RSCSR_CHNL0_WPTR,
Zhu Yib481de92007-09-25 17:54:57 -07003649 q->write & ~0x7);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003650 iwl_release_nic_access(priv);
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003651
3652 /* Else device is assumed to be awake */
Zhu Yib481de92007-09-25 17:54:57 -07003653 } else
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003654 /* Device expects a multiple of 8 */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003655 iwl_write32(priv, FH_RSCSR_CHNL0_WPTR, q->write & ~0x7);
Zhu Yib481de92007-09-25 17:54:57 -07003656
3657
3658 q->need_update = 0;
3659
3660 exit_unlock:
3661 spin_unlock_irqrestore(&q->lock, flags);
3662 return rc;
3663}
3664
3665/**
Ben Cahill9fbab512007-11-29 11:09:47 +08003666 * iwl4965_dma_addr2rbd_ptr - convert a DMA address to a uCode read buffer ptr
Zhu Yib481de92007-09-25 17:54:57 -07003667 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003668static inline __le32 iwl4965_dma_addr2rbd_ptr(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07003669 dma_addr_t dma_addr)
3670{
3671 return cpu_to_le32((u32)(dma_addr >> 8));
3672}
3673
3674
3675/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003676 * iwl4965_rx_queue_restock - refill RX queue from pre-allocated pool
Zhu Yib481de92007-09-25 17:54:57 -07003677 *
Ben Cahill9fbab512007-11-29 11:09:47 +08003678 * If there are slots in the RX queue that need to be restocked,
Zhu Yib481de92007-09-25 17:54:57 -07003679 * and we have free pre-allocated buffers, fill the ranks as much
Ben Cahill9fbab512007-11-29 11:09:47 +08003680 * as we can, pulling from rx_free.
Zhu Yib481de92007-09-25 17:54:57 -07003681 *
3682 * This moves the 'write' index forward to catch up with 'processed', and
3683 * also updates the memory address in the firmware to reference the new
3684 * target buffer.
3685 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003686static int iwl4965_rx_queue_restock(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003687{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003688 struct iwl4965_rx_queue *rxq = &priv->rxq;
Zhu Yib481de92007-09-25 17:54:57 -07003689 struct list_head *element;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003690 struct iwl4965_rx_mem_buffer *rxb;
Zhu Yib481de92007-09-25 17:54:57 -07003691 unsigned long flags;
3692 int write, rc;
3693
3694 spin_lock_irqsave(&rxq->lock, flags);
3695 write = rxq->write & ~0x7;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003696 while ((iwl4965_rx_queue_space(rxq) > 0) && (rxq->free_count)) {
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003697 /* Get next free Rx buffer, remove from free list */
Zhu Yib481de92007-09-25 17:54:57 -07003698 element = rxq->rx_free.next;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003699 rxb = list_entry(element, struct iwl4965_rx_mem_buffer, list);
Zhu Yib481de92007-09-25 17:54:57 -07003700 list_del(element);
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003701
3702 /* Point to Rx buffer via next RBD in circular buffer */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003703 rxq->bd[rxq->write] = iwl4965_dma_addr2rbd_ptr(priv, rxb->dma_addr);
Zhu Yib481de92007-09-25 17:54:57 -07003704 rxq->queue[rxq->write] = rxb;
3705 rxq->write = (rxq->write + 1) & RX_QUEUE_MASK;
3706 rxq->free_count--;
3707 }
3708 spin_unlock_irqrestore(&rxq->lock, flags);
3709 /* If the pre-allocated buffer pool is dropping low, schedule to
3710 * refill it */
3711 if (rxq->free_count <= RX_LOW_WATERMARK)
3712 queue_work(priv->workqueue, &priv->rx_replenish);
3713
3714
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003715 /* If we've added more space for the firmware to place data, tell it.
3716 * Increment device's write pointer in multiples of 8. */
Zhu Yib481de92007-09-25 17:54:57 -07003717 if ((write != (rxq->write & ~0x7))
3718 || (abs(rxq->write - rxq->read) > 7)) {
3719 spin_lock_irqsave(&rxq->lock, flags);
3720 rxq->need_update = 1;
3721 spin_unlock_irqrestore(&rxq->lock, flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003722 rc = iwl4965_rx_queue_update_write_ptr(priv, rxq);
Zhu Yib481de92007-09-25 17:54:57 -07003723 if (rc)
3724 return rc;
3725 }
3726
3727 return 0;
3728}
3729
3730/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003731 * iwl4965_rx_replenish - Move all used packet from rx_used to rx_free
Zhu Yib481de92007-09-25 17:54:57 -07003732 *
3733 * When moving to rx_free an SKB is allocated for the slot.
3734 *
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003735 * Also restock the Rx queue via iwl4965_rx_queue_restock.
Ian Schram01ebd062007-10-25 17:15:22 +08003736 * This is called as a scheduled work item (except for during initialization)
Zhu Yib481de92007-09-25 17:54:57 -07003737 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003738static void iwl4965_rx_allocate(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003739{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003740 struct iwl4965_rx_queue *rxq = &priv->rxq;
Zhu Yib481de92007-09-25 17:54:57 -07003741 struct list_head *element;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003742 struct iwl4965_rx_mem_buffer *rxb;
Zhu Yib481de92007-09-25 17:54:57 -07003743 unsigned long flags;
3744 spin_lock_irqsave(&rxq->lock, flags);
3745 while (!list_empty(&rxq->rx_used)) {
3746 element = rxq->rx_used.next;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003747 rxb = list_entry(element, struct iwl4965_rx_mem_buffer, list);
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003748
3749 /* Alloc a new receive buffer */
Zhu Yib481de92007-09-25 17:54:57 -07003750 rxb->skb =
Ron Rindjunsky9ee1ba42007-11-26 16:14:42 +02003751 alloc_skb(priv->hw_setting.rx_buf_size,
3752 __GFP_NOWARN | GFP_ATOMIC);
Zhu Yib481de92007-09-25 17:54:57 -07003753 if (!rxb->skb) {
3754 if (net_ratelimit())
3755 printk(KERN_CRIT DRV_NAME
3756 ": Can not allocate SKB buffers\n");
3757 /* We don't reschedule replenish work here -- we will
3758 * call the restock method and if it still needs
3759 * more buffers it will schedule replenish */
3760 break;
3761 }
3762 priv->alloc_rxb_skb++;
3763 list_del(element);
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003764
3765 /* Get physical address of RB/SKB */
Zhu Yib481de92007-09-25 17:54:57 -07003766 rxb->dma_addr =
3767 pci_map_single(priv->pci_dev, rxb->skb->data,
Ron Rindjunsky9ee1ba42007-11-26 16:14:42 +02003768 priv->hw_setting.rx_buf_size, PCI_DMA_FROMDEVICE);
Zhu Yib481de92007-09-25 17:54:57 -07003769 list_add_tail(&rxb->list, &rxq->rx_free);
3770 rxq->free_count++;
3771 }
3772 spin_unlock_irqrestore(&rxq->lock, flags);
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08003773}
3774
3775/*
3776 * this should be called while priv->lock is locked
3777*/
Tomas Winkler4fd1f842007-12-05 20:59:58 +02003778static void __iwl4965_rx_replenish(void *data)
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08003779{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003780 struct iwl_priv *priv = data;
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08003781
3782 iwl4965_rx_allocate(priv);
3783 iwl4965_rx_queue_restock(priv);
3784}
3785
3786
3787void iwl4965_rx_replenish(void *data)
3788{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003789 struct iwl_priv *priv = data;
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08003790 unsigned long flags;
3791
3792 iwl4965_rx_allocate(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003793
3794 spin_lock_irqsave(&priv->lock, flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003795 iwl4965_rx_queue_restock(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003796 spin_unlock_irqrestore(&priv->lock, flags);
3797}
3798
3799/* Assumes that the skb field of the buffers in 'pool' is kept accurate.
Ben Cahill9fbab512007-11-29 11:09:47 +08003800 * If an SKB has been detached, the POOL needs to have its SKB set to NULL
Zhu Yib481de92007-09-25 17:54:57 -07003801 * This free routine walks the list of POOL entries and if SKB is set to
3802 * non NULL it is unmapped and freed
3803 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003804static void iwl4965_rx_queue_free(struct iwl_priv *priv, struct iwl4965_rx_queue *rxq)
Zhu Yib481de92007-09-25 17:54:57 -07003805{
3806 int i;
3807 for (i = 0; i < RX_QUEUE_SIZE + RX_FREE_BUFFERS; i++) {
3808 if (rxq->pool[i].skb != NULL) {
3809 pci_unmap_single(priv->pci_dev,
3810 rxq->pool[i].dma_addr,
Ron Rindjunsky9ee1ba42007-11-26 16:14:42 +02003811 priv->hw_setting.rx_buf_size,
3812 PCI_DMA_FROMDEVICE);
Zhu Yib481de92007-09-25 17:54:57 -07003813 dev_kfree_skb(rxq->pool[i].skb);
3814 }
3815 }
3816
3817 pci_free_consistent(priv->pci_dev, 4 * RX_QUEUE_SIZE, rxq->bd,
3818 rxq->dma_addr);
3819 rxq->bd = NULL;
3820}
3821
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003822int iwl4965_rx_queue_alloc(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003823{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003824 struct iwl4965_rx_queue *rxq = &priv->rxq;
Zhu Yib481de92007-09-25 17:54:57 -07003825 struct pci_dev *dev = priv->pci_dev;
3826 int i;
3827
3828 spin_lock_init(&rxq->lock);
3829 INIT_LIST_HEAD(&rxq->rx_free);
3830 INIT_LIST_HEAD(&rxq->rx_used);
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003831
3832 /* Alloc the circular buffer of Read Buffer Descriptors (RBDs) */
Zhu Yib481de92007-09-25 17:54:57 -07003833 rxq->bd = pci_alloc_consistent(dev, 4 * RX_QUEUE_SIZE, &rxq->dma_addr);
3834 if (!rxq->bd)
3835 return -ENOMEM;
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003836
Zhu Yib481de92007-09-25 17:54:57 -07003837 /* Fill the rx_used queue with _all_ of the Rx buffers */
3838 for (i = 0; i < RX_FREE_BUFFERS + RX_QUEUE_SIZE; i++)
3839 list_add_tail(&rxq->pool[i].list, &rxq->rx_used);
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003840
Zhu Yib481de92007-09-25 17:54:57 -07003841 /* Set us so that we have processed and used all buffers, but have
3842 * not restocked the Rx queue with fresh buffers */
3843 rxq->read = rxq->write = 0;
3844 rxq->free_count = 0;
3845 rxq->need_update = 0;
3846 return 0;
3847}
3848
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003849void iwl4965_rx_queue_reset(struct iwl_priv *priv, struct iwl4965_rx_queue *rxq)
Zhu Yib481de92007-09-25 17:54:57 -07003850{
3851 unsigned long flags;
3852 int i;
3853 spin_lock_irqsave(&rxq->lock, flags);
3854 INIT_LIST_HEAD(&rxq->rx_free);
3855 INIT_LIST_HEAD(&rxq->rx_used);
3856 /* Fill the rx_used queue with _all_ of the Rx buffers */
3857 for (i = 0; i < RX_FREE_BUFFERS + RX_QUEUE_SIZE; i++) {
3858 /* In the reset function, these buffers may have been allocated
3859 * to an SKB, so we need to unmap and free potential storage */
3860 if (rxq->pool[i].skb != NULL) {
3861 pci_unmap_single(priv->pci_dev,
3862 rxq->pool[i].dma_addr,
Ron Rindjunsky9ee1ba42007-11-26 16:14:42 +02003863 priv->hw_setting.rx_buf_size,
3864 PCI_DMA_FROMDEVICE);
Zhu Yib481de92007-09-25 17:54:57 -07003865 priv->alloc_rxb_skb--;
3866 dev_kfree_skb(rxq->pool[i].skb);
3867 rxq->pool[i].skb = NULL;
3868 }
3869 list_add_tail(&rxq->pool[i].list, &rxq->rx_used);
3870 }
3871
3872 /* Set us so that we have processed and used all buffers, but have
3873 * not restocked the Rx queue with fresh buffers */
3874 rxq->read = rxq->write = 0;
3875 rxq->free_count = 0;
3876 spin_unlock_irqrestore(&rxq->lock, flags);
3877}
3878
3879/* Convert linear signal-to-noise ratio into dB */
3880static u8 ratio2dB[100] = {
3881/* 0 1 2 3 4 5 6 7 8 9 */
3882 0, 0, 6, 10, 12, 14, 16, 17, 18, 19, /* 00 - 09 */
3883 20, 21, 22, 22, 23, 23, 24, 25, 26, 26, /* 10 - 19 */
3884 26, 26, 26, 27, 27, 28, 28, 28, 29, 29, /* 20 - 29 */
3885 29, 30, 30, 30, 31, 31, 31, 31, 32, 32, /* 30 - 39 */
3886 32, 32, 32, 33, 33, 33, 33, 33, 34, 34, /* 40 - 49 */
3887 34, 34, 34, 34, 35, 35, 35, 35, 35, 35, /* 50 - 59 */
3888 36, 36, 36, 36, 36, 36, 36, 37, 37, 37, /* 60 - 69 */
3889 37, 37, 37, 37, 37, 38, 38, 38, 38, 38, /* 70 - 79 */
3890 38, 38, 38, 38, 38, 39, 39, 39, 39, 39, /* 80 - 89 */
3891 39, 39, 39, 39, 39, 40, 40, 40, 40, 40 /* 90 - 99 */
3892};
3893
3894/* Calculates a relative dB value from a ratio of linear
3895 * (i.e. not dB) signal levels.
3896 * Conversion assumes that levels are voltages (20*log), not powers (10*log). */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003897int iwl4965_calc_db_from_ratio(int sig_ratio)
Zhu Yib481de92007-09-25 17:54:57 -07003898{
Adrian Bunkc899a572007-10-14 19:51:15 +02003899 /* 1000:1 or higher just report as 60 dB */
3900 if (sig_ratio >= 1000)
Zhu Yib481de92007-09-25 17:54:57 -07003901 return 60;
3902
Adrian Bunkc899a572007-10-14 19:51:15 +02003903 /* 100:1 or higher, divide by 10 and use table,
Zhu Yib481de92007-09-25 17:54:57 -07003904 * add 20 dB to make up for divide by 10 */
Adrian Bunkc899a572007-10-14 19:51:15 +02003905 if (sig_ratio >= 100)
Zhu Yib481de92007-09-25 17:54:57 -07003906 return (20 + (int)ratio2dB[sig_ratio/10]);
3907
3908 /* We shouldn't see this */
3909 if (sig_ratio < 1)
3910 return 0;
3911
3912 /* Use table for ratios 1:1 - 99:1 */
3913 return (int)ratio2dB[sig_ratio];
3914}
3915
3916#define PERFECT_RSSI (-20) /* dBm */
3917#define WORST_RSSI (-95) /* dBm */
3918#define RSSI_RANGE (PERFECT_RSSI - WORST_RSSI)
3919
3920/* Calculate an indication of rx signal quality (a percentage, not dBm!).
3921 * See http://www.ces.clemson.edu/linux/signal_quality.shtml for info
3922 * about formulas used below. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003923int iwl4965_calc_sig_qual(int rssi_dbm, int noise_dbm)
Zhu Yib481de92007-09-25 17:54:57 -07003924{
3925 int sig_qual;
3926 int degradation = PERFECT_RSSI - rssi_dbm;
3927
3928 /* If we get a noise measurement, use signal-to-noise ratio (SNR)
3929 * as indicator; formula is (signal dbm - noise dbm).
3930 * SNR at or above 40 is a great signal (100%).
3931 * Below that, scale to fit SNR of 0 - 40 dB within 0 - 100% indicator.
3932 * Weakest usable signal is usually 10 - 15 dB SNR. */
3933 if (noise_dbm) {
3934 if (rssi_dbm - noise_dbm >= 40)
3935 return 100;
3936 else if (rssi_dbm < noise_dbm)
3937 return 0;
3938 sig_qual = ((rssi_dbm - noise_dbm) * 5) / 2;
3939
3940 /* Else use just the signal level.
3941 * This formula is a least squares fit of data points collected and
3942 * compared with a reference system that had a percentage (%) display
3943 * for signal quality. */
3944 } else
3945 sig_qual = (100 * (RSSI_RANGE * RSSI_RANGE) - degradation *
3946 (15 * RSSI_RANGE + 62 * degradation)) /
3947 (RSSI_RANGE * RSSI_RANGE);
3948
3949 if (sig_qual > 100)
3950 sig_qual = 100;
3951 else if (sig_qual < 1)
3952 sig_qual = 0;
3953
3954 return sig_qual;
3955}
3956
3957/**
Ben Cahill9fbab512007-11-29 11:09:47 +08003958 * iwl4965_rx_handle - Main entry function for receiving responses from uCode
Zhu Yib481de92007-09-25 17:54:57 -07003959 *
3960 * Uses the priv->rx_handlers callback function array to invoke
3961 * the appropriate handlers, including command responses,
3962 * frame-received notifications, and other notifications.
3963 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003964static void iwl4965_rx_handle(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003965{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003966 struct iwl4965_rx_mem_buffer *rxb;
3967 struct iwl4965_rx_packet *pkt;
3968 struct iwl4965_rx_queue *rxq = &priv->rxq;
Zhu Yib481de92007-09-25 17:54:57 -07003969 u32 r, i;
3970 int reclaim;
3971 unsigned long flags;
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08003972 u8 fill_rx = 0;
Mohamed Abbasd68ab682008-02-07 13:16:33 -08003973 u32 count = 8;
Zhu Yib481de92007-09-25 17:54:57 -07003974
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003975 /* uCode's read index (stored in shared DRAM) indicates the last Rx
3976 * buffer that the driver may process (last buffer filled by ucode). */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003977 r = iwl4965_hw_get_rx_read(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003978 i = rxq->read;
3979
3980 /* Rx interrupt, but nothing sent from uCode */
3981 if (i == r)
3982 IWL_DEBUG(IWL_DL_RX | IWL_DL_ISR, "r = %d, i = %d\n", r, i);
3983
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08003984 if (iwl4965_rx_queue_space(rxq) > (RX_QUEUE_SIZE / 2))
3985 fill_rx = 1;
3986
Zhu Yib481de92007-09-25 17:54:57 -07003987 while (i != r) {
3988 rxb = rxq->queue[i];
3989
Ben Cahill9fbab512007-11-29 11:09:47 +08003990 /* If an RXB doesn't have a Rx queue slot associated with it,
Zhu Yib481de92007-09-25 17:54:57 -07003991 * then a bug has been introduced in the queue refilling
3992 * routines -- catch it here */
3993 BUG_ON(rxb == NULL);
3994
3995 rxq->queue[i] = NULL;
3996
3997 pci_dma_sync_single_for_cpu(priv->pci_dev, rxb->dma_addr,
Ron Rindjunsky9ee1ba42007-11-26 16:14:42 +02003998 priv->hw_setting.rx_buf_size,
Zhu Yib481de92007-09-25 17:54:57 -07003999 PCI_DMA_FROMDEVICE);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004000 pkt = (struct iwl4965_rx_packet *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07004001
4002 /* Reclaim a command buffer only if this packet is a response
4003 * to a (driver-originated) command.
4004 * If the packet (e.g. Rx frame) originated from uCode,
4005 * there is no command buffer to reclaim.
4006 * Ucode should set SEQ_RX_FRAME bit if ucode-originated,
4007 * but apparently a few don't get set; catch them here. */
4008 reclaim = !(pkt->hdr.sequence & SEQ_RX_FRAME) &&
4009 (pkt->hdr.cmd != REPLY_RX_PHY_CMD) &&
Tomas Winkler857485c2008-03-21 13:53:44 -07004010 (pkt->hdr.cmd != REPLY_RX) &&
Zhu Yicfe01702007-09-27 11:27:31 +08004011 (pkt->hdr.cmd != REPLY_COMPRESSED_BA) &&
Zhu Yib481de92007-09-25 17:54:57 -07004012 (pkt->hdr.cmd != STATISTICS_NOTIFICATION) &&
4013 (pkt->hdr.cmd != REPLY_TX);
4014
4015 /* Based on type of command response or notification,
4016 * handle those that need handling via function in
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004017 * rx_handlers table. See iwl4965_setup_rx_handlers() */
Zhu Yib481de92007-09-25 17:54:57 -07004018 if (priv->rx_handlers[pkt->hdr.cmd]) {
4019 IWL_DEBUG(IWL_DL_HOST_COMMAND | IWL_DL_RX | IWL_DL_ISR,
4020 "r = %d, i = %d, %s, 0x%02x\n", r, i,
4021 get_cmd_string(pkt->hdr.cmd), pkt->hdr.cmd);
4022 priv->rx_handlers[pkt->hdr.cmd] (priv, rxb);
4023 } else {
4024 /* No handling needed */
4025 IWL_DEBUG(IWL_DL_HOST_COMMAND | IWL_DL_RX | IWL_DL_ISR,
4026 "r %d i %d No handler needed for %s, 0x%02x\n",
4027 r, i, get_cmd_string(pkt->hdr.cmd),
4028 pkt->hdr.cmd);
4029 }
4030
4031 if (reclaim) {
Ben Cahill9fbab512007-11-29 11:09:47 +08004032 /* Invoke any callbacks, transfer the skb to caller, and
Tomas Winkler857485c2008-03-21 13:53:44 -07004033 * fire off the (possibly) blocking iwl_send_cmd()
Zhu Yib481de92007-09-25 17:54:57 -07004034 * as we reclaim the driver command queue */
4035 if (rxb && rxb->skb)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004036 iwl4965_tx_cmd_complete(priv, rxb);
Zhu Yib481de92007-09-25 17:54:57 -07004037 else
4038 IWL_WARNING("Claim null rxb?\n");
4039 }
4040
4041 /* For now we just don't re-use anything. We can tweak this
4042 * later to try and re-use notification packets and SKBs that
4043 * fail to Rx correctly */
4044 if (rxb->skb != NULL) {
4045 priv->alloc_rxb_skb--;
4046 dev_kfree_skb_any(rxb->skb);
4047 rxb->skb = NULL;
4048 }
4049
4050 pci_unmap_single(priv->pci_dev, rxb->dma_addr,
Ron Rindjunsky9ee1ba42007-11-26 16:14:42 +02004051 priv->hw_setting.rx_buf_size,
4052 PCI_DMA_FROMDEVICE);
Zhu Yib481de92007-09-25 17:54:57 -07004053 spin_lock_irqsave(&rxq->lock, flags);
4054 list_add_tail(&rxb->list, &priv->rxq.rx_used);
4055 spin_unlock_irqrestore(&rxq->lock, flags);
4056 i = (i + 1) & RX_QUEUE_MASK;
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08004057 /* If there are a lot of unused frames,
4058 * restock the Rx queue so ucode wont assert. */
4059 if (fill_rx) {
4060 count++;
4061 if (count >= 8) {
4062 priv->rxq.read = i;
4063 __iwl4965_rx_replenish(priv);
4064 count = 0;
4065 }
4066 }
Zhu Yib481de92007-09-25 17:54:57 -07004067 }
4068
4069 /* Backtrack one entry */
4070 priv->rxq.read = i;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004071 iwl4965_rx_queue_restock(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004072}
4073
Cahill, Ben M6440adb2007-11-29 11:09:55 +08004074/**
4075 * iwl4965_tx_queue_update_write_ptr - Send new write index to hardware
4076 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004077static int iwl4965_tx_queue_update_write_ptr(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004078 struct iwl4965_tx_queue *txq)
Zhu Yib481de92007-09-25 17:54:57 -07004079{
4080 u32 reg = 0;
4081 int rc = 0;
4082 int txq_id = txq->q.id;
4083
4084 if (txq->need_update == 0)
4085 return rc;
4086
4087 /* if we're trying to save power */
4088 if (test_bit(STATUS_POWER_PMI, &priv->status)) {
4089 /* wake up nic if it's powered down ...
4090 * uCode will wake up, and interrupt us again, so next
4091 * time we'll skip this part. */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004092 reg = iwl_read32(priv, CSR_UCODE_DRV_GP1);
Zhu Yib481de92007-09-25 17:54:57 -07004093
4094 if (reg & CSR_UCODE_DRV_GP1_BIT_MAC_SLEEP) {
4095 IWL_DEBUG_INFO("Requesting wakeup, GP1 = 0x%x\n", reg);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004096 iwl_set_bit(priv, CSR_GP_CNTRL,
Zhu Yib481de92007-09-25 17:54:57 -07004097 CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
4098 return rc;
4099 }
4100
4101 /* restore this queue's parameters in nic hardware. */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004102 rc = iwl_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004103 if (rc)
4104 return rc;
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004105 iwl_write_direct32(priv, HBUS_TARG_WRPTR,
Tomas Winklerfc4b6852007-10-25 17:15:24 +08004106 txq->q.write_ptr | (txq_id << 8));
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004107 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004108
4109 /* else not in power-save mode, uCode will never sleep when we're
4110 * trying to tx (during RFKILL, we're not trying to tx). */
4111 } else
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004112 iwl_write32(priv, HBUS_TARG_WRPTR,
Tomas Winklerfc4b6852007-10-25 17:15:24 +08004113 txq->q.write_ptr | (txq_id << 8));
Zhu Yib481de92007-09-25 17:54:57 -07004114
4115 txq->need_update = 0;
4116
4117 return rc;
4118}
4119
Tomas Winkler0a6857e2008-03-12 16:58:49 -07004120#ifdef CONFIG_IWLWIFI_DEBUG
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004121static void iwl4965_print_rx_config_cmd(struct iwl4965_rxon_cmd *rxon)
Zhu Yib481de92007-09-25 17:54:57 -07004122{
Joe Perches0795af52007-10-03 17:59:30 -07004123 DECLARE_MAC_BUF(mac);
4124
Zhu Yib481de92007-09-25 17:54:57 -07004125 IWL_DEBUG_RADIO("RX CONFIG:\n");
Tomas Winkler0a6857e2008-03-12 16:58:49 -07004126 iwl_print_hex_dump(IWL_DL_RADIO, (u8 *) rxon, sizeof(*rxon));
Zhu Yib481de92007-09-25 17:54:57 -07004127 IWL_DEBUG_RADIO("u16 channel: 0x%x\n", le16_to_cpu(rxon->channel));
4128 IWL_DEBUG_RADIO("u32 flags: 0x%08X\n", le32_to_cpu(rxon->flags));
4129 IWL_DEBUG_RADIO("u32 filter_flags: 0x%08x\n",
4130 le32_to_cpu(rxon->filter_flags));
4131 IWL_DEBUG_RADIO("u8 dev_type: 0x%x\n", rxon->dev_type);
4132 IWL_DEBUG_RADIO("u8 ofdm_basic_rates: 0x%02x\n",
4133 rxon->ofdm_basic_rates);
4134 IWL_DEBUG_RADIO("u8 cck_basic_rates: 0x%02x\n", rxon->cck_basic_rates);
Joe Perches0795af52007-10-03 17:59:30 -07004135 IWL_DEBUG_RADIO("u8[6] node_addr: %s\n",
4136 print_mac(mac, rxon->node_addr));
4137 IWL_DEBUG_RADIO("u8[6] bssid_addr: %s\n",
4138 print_mac(mac, rxon->bssid_addr));
Zhu Yib481de92007-09-25 17:54:57 -07004139 IWL_DEBUG_RADIO("u16 assoc_id: 0x%x\n", le16_to_cpu(rxon->assoc_id));
4140}
4141#endif
4142
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004143static void iwl4965_enable_interrupts(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004144{
4145 IWL_DEBUG_ISR("Enabling interrupts\n");
4146 set_bit(STATUS_INT_ENABLED, &priv->status);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004147 iwl_write32(priv, CSR_INT_MASK, CSR_INI_SET_MASK);
Zhu Yib481de92007-09-25 17:54:57 -07004148}
4149
Mohamed Abbas0359fac2008-03-28 16:21:08 -07004150/* call this function to flush any scheduled tasklet */
4151static inline void iwl_synchronize_irq(struct iwl_priv *priv)
4152{
4153 /* wait to make sure we flush pedding tasklet*/
4154 synchronize_irq(priv->pci_dev->irq);
4155 tasklet_kill(&priv->irq_tasklet);
4156}
4157
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004158static inline void iwl4965_disable_interrupts(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004159{
4160 clear_bit(STATUS_INT_ENABLED, &priv->status);
4161
4162 /* disable interrupts from uCode/NIC to host */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004163 iwl_write32(priv, CSR_INT_MASK, 0x00000000);
Zhu Yib481de92007-09-25 17:54:57 -07004164
4165 /* acknowledge/clear/reset any interrupts still pending
4166 * from uCode or flow handler (Rx/Tx DMA) */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004167 iwl_write32(priv, CSR_INT, 0xffffffff);
4168 iwl_write32(priv, CSR_FH_INT_STATUS, 0xffffffff);
Zhu Yib481de92007-09-25 17:54:57 -07004169 IWL_DEBUG_ISR("Disabled interrupts\n");
4170}
4171
4172static const char *desc_lookup(int i)
4173{
4174 switch (i) {
4175 case 1:
4176 return "FAIL";
4177 case 2:
4178 return "BAD_PARAM";
4179 case 3:
4180 return "BAD_CHECKSUM";
4181 case 4:
4182 return "NMI_INTERRUPT";
4183 case 5:
4184 return "SYSASSERT";
4185 case 6:
4186 return "FATAL_ERROR";
4187 }
4188
4189 return "UNKNOWN";
4190}
4191
4192#define ERROR_START_OFFSET (1 * sizeof(u32))
4193#define ERROR_ELEM_SIZE (7 * sizeof(u32))
4194
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004195static void iwl4965_dump_nic_error_log(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004196{
4197 u32 data2, line;
4198 u32 desc, time, count, base, data1;
4199 u32 blink1, blink2, ilink1, ilink2;
4200 int rc;
4201
4202 base = le32_to_cpu(priv->card_alive.error_event_table_ptr);
4203
Tomas Winkler57aab752008-04-14 21:16:03 -07004204 if (!priv->cfg->ops->lib->is_valid_rtc_data_addr(base)) {
Zhu Yib481de92007-09-25 17:54:57 -07004205 IWL_ERROR("Not valid error log pointer 0x%08X\n", base);
4206 return;
4207 }
4208
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004209 rc = iwl_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004210 if (rc) {
4211 IWL_WARNING("Can not read from adapter at this time.\n");
4212 return;
4213 }
4214
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004215 count = iwl_read_targ_mem(priv, base);
Zhu Yib481de92007-09-25 17:54:57 -07004216
4217 if (ERROR_START_OFFSET <= count * ERROR_ELEM_SIZE) {
4218 IWL_ERROR("Start IWL Error Log Dump:\n");
Tomas Winkler2acae162008-03-02 01:25:59 +02004219 IWL_ERROR("Status: 0x%08lX, count: %d\n", priv->status, count);
Zhu Yib481de92007-09-25 17:54:57 -07004220 }
4221
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004222 desc = iwl_read_targ_mem(priv, base + 1 * sizeof(u32));
4223 blink1 = iwl_read_targ_mem(priv, base + 3 * sizeof(u32));
4224 blink2 = iwl_read_targ_mem(priv, base + 4 * sizeof(u32));
4225 ilink1 = iwl_read_targ_mem(priv, base + 5 * sizeof(u32));
4226 ilink2 = iwl_read_targ_mem(priv, base + 6 * sizeof(u32));
4227 data1 = iwl_read_targ_mem(priv, base + 7 * sizeof(u32));
4228 data2 = iwl_read_targ_mem(priv, base + 8 * sizeof(u32));
4229 line = iwl_read_targ_mem(priv, base + 9 * sizeof(u32));
4230 time = iwl_read_targ_mem(priv, base + 11 * sizeof(u32));
Zhu Yib481de92007-09-25 17:54:57 -07004231
4232 IWL_ERROR("Desc Time "
4233 "data1 data2 line\n");
4234 IWL_ERROR("%-13s (#%d) %010u 0x%08X 0x%08X %u\n",
4235 desc_lookup(desc), desc, time, data1, data2, line);
4236 IWL_ERROR("blink1 blink2 ilink1 ilink2\n");
4237 IWL_ERROR("0x%05X 0x%05X 0x%05X 0x%05X\n", blink1, blink2,
4238 ilink1, ilink2);
4239
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004240 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004241}
4242
4243#define EVENT_START_OFFSET (4 * sizeof(u32))
4244
4245/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004246 * iwl4965_print_event_log - Dump error event log to syslog
Zhu Yib481de92007-09-25 17:54:57 -07004247 *
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004248 * NOTE: Must be called with iwl_grab_nic_access() already obtained!
Zhu Yib481de92007-09-25 17:54:57 -07004249 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004250static void iwl4965_print_event_log(struct iwl_priv *priv, u32 start_idx,
Zhu Yib481de92007-09-25 17:54:57 -07004251 u32 num_events, u32 mode)
4252{
4253 u32 i;
4254 u32 base; /* SRAM byte address of event log header */
4255 u32 event_size; /* 2 u32s, or 3 u32s if timestamp recorded */
4256 u32 ptr; /* SRAM byte address of log data */
4257 u32 ev, time, data; /* event log data */
4258
4259 if (num_events == 0)
4260 return;
4261
4262 base = le32_to_cpu(priv->card_alive.log_event_table_ptr);
4263
4264 if (mode == 0)
4265 event_size = 2 * sizeof(u32);
4266 else
4267 event_size = 3 * sizeof(u32);
4268
4269 ptr = base + EVENT_START_OFFSET + (start_idx * event_size);
4270
4271 /* "time" is actually "data" for mode 0 (no timestamp).
4272 * place event id # at far right for easier visual parsing. */
4273 for (i = 0; i < num_events; i++) {
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004274 ev = iwl_read_targ_mem(priv, ptr);
Zhu Yib481de92007-09-25 17:54:57 -07004275 ptr += sizeof(u32);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004276 time = iwl_read_targ_mem(priv, ptr);
Zhu Yib481de92007-09-25 17:54:57 -07004277 ptr += sizeof(u32);
4278 if (mode == 0)
4279 IWL_ERROR("0x%08x\t%04u\n", time, ev); /* data, ev */
4280 else {
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004281 data = iwl_read_targ_mem(priv, ptr);
Zhu Yib481de92007-09-25 17:54:57 -07004282 ptr += sizeof(u32);
4283 IWL_ERROR("%010u\t0x%08x\t%04u\n", time, data, ev);
4284 }
4285 }
4286}
4287
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004288static void iwl4965_dump_nic_event_log(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004289{
4290 int rc;
4291 u32 base; /* SRAM byte address of event log header */
4292 u32 capacity; /* event log capacity in # entries */
4293 u32 mode; /* 0 - no timestamp, 1 - timestamp recorded */
4294 u32 num_wraps; /* # times uCode wrapped to top of log */
4295 u32 next_entry; /* index of next entry to be written by uCode */
4296 u32 size; /* # entries that we'll print */
4297
4298 base = le32_to_cpu(priv->card_alive.log_event_table_ptr);
Tomas Winkler57aab752008-04-14 21:16:03 -07004299 if (!priv->cfg->ops->lib->is_valid_rtc_data_addr(base)) {
Zhu Yib481de92007-09-25 17:54:57 -07004300 IWL_ERROR("Invalid event log pointer 0x%08X\n", base);
4301 return;
4302 }
4303
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004304 rc = iwl_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004305 if (rc) {
4306 IWL_WARNING("Can not read from adapter at this time.\n");
4307 return;
4308 }
4309
4310 /* event log header */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004311 capacity = iwl_read_targ_mem(priv, base);
4312 mode = iwl_read_targ_mem(priv, base + (1 * sizeof(u32)));
4313 num_wraps = iwl_read_targ_mem(priv, base + (2 * sizeof(u32)));
4314 next_entry = iwl_read_targ_mem(priv, base + (3 * sizeof(u32)));
Zhu Yib481de92007-09-25 17:54:57 -07004315
4316 size = num_wraps ? capacity : next_entry;
4317
4318 /* bail out if nothing in log */
4319 if (size == 0) {
Zhu Yi583fab32007-09-27 11:27:30 +08004320 IWL_ERROR("Start IWL Event Log Dump: nothing in log\n");
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004321 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004322 return;
4323 }
4324
Zhu Yi583fab32007-09-27 11:27:30 +08004325 IWL_ERROR("Start IWL Event Log Dump: display count %d, wraps %d\n",
Zhu Yib481de92007-09-25 17:54:57 -07004326 size, num_wraps);
4327
4328 /* if uCode has wrapped back to top of log, start at the oldest entry,
4329 * i.e the next one that uCode would fill. */
4330 if (num_wraps)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004331 iwl4965_print_event_log(priv, next_entry,
Zhu Yib481de92007-09-25 17:54:57 -07004332 capacity - next_entry, mode);
4333
4334 /* (then/else) start at top of log */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004335 iwl4965_print_event_log(priv, 0, next_entry, mode);
Zhu Yib481de92007-09-25 17:54:57 -07004336
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004337 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004338}
4339
4340/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004341 * iwl4965_irq_handle_error - called for HW or SW error interrupt from card
Zhu Yib481de92007-09-25 17:54:57 -07004342 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004343static void iwl4965_irq_handle_error(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004344{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004345 /* Set the FW error flag -- cleared on iwl4965_down */
Zhu Yib481de92007-09-25 17:54:57 -07004346 set_bit(STATUS_FW_ERROR, &priv->status);
4347
4348 /* Cancel currently queued command. */
4349 clear_bit(STATUS_HCMD_ACTIVE, &priv->status);
4350
Tomas Winkler0a6857e2008-03-12 16:58:49 -07004351#ifdef CONFIG_IWLWIFI_DEBUG
4352 if (iwl_debug_level & IWL_DL_FW_ERRORS) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004353 iwl4965_dump_nic_error_log(priv);
4354 iwl4965_dump_nic_event_log(priv);
4355 iwl4965_print_rx_config_cmd(&priv->staging_rxon);
Zhu Yib481de92007-09-25 17:54:57 -07004356 }
4357#endif
4358
4359 wake_up_interruptible(&priv->wait_command_queue);
4360
4361 /* Keep the restart process from trying to send host
4362 * commands by clearing the INIT status bit */
4363 clear_bit(STATUS_READY, &priv->status);
4364
4365 if (!test_bit(STATUS_EXIT_PENDING, &priv->status)) {
4366 IWL_DEBUG(IWL_DL_INFO | IWL_DL_FW_ERRORS,
4367 "Restarting adapter due to uCode error.\n");
4368
Tomas Winkler3109ece2008-03-28 16:33:35 -07004369 if (iwl_is_associated(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07004370 memcpy(&priv->recovery_rxon, &priv->active_rxon,
4371 sizeof(priv->recovery_rxon));
4372 priv->error_recovering = 1;
4373 }
4374 queue_work(priv->workqueue, &priv->restart);
4375 }
4376}
4377
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004378static void iwl4965_error_recovery(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004379{
4380 unsigned long flags;
4381
4382 memcpy(&priv->staging_rxon, &priv->recovery_rxon,
4383 sizeof(priv->staging_rxon));
4384 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004385 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004386
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004387 iwl4965_rxon_add_station(priv, priv->bssid, 1);
Zhu Yib481de92007-09-25 17:54:57 -07004388
4389 spin_lock_irqsave(&priv->lock, flags);
4390 priv->assoc_id = le16_to_cpu(priv->staging_rxon.assoc_id);
4391 priv->error_recovering = 0;
4392 spin_unlock_irqrestore(&priv->lock, flags);
4393}
4394
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004395static void iwl4965_irq_tasklet(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004396{
4397 u32 inta, handled = 0;
4398 u32 inta_fh;
4399 unsigned long flags;
Tomas Winkler0a6857e2008-03-12 16:58:49 -07004400#ifdef CONFIG_IWLWIFI_DEBUG
Zhu Yib481de92007-09-25 17:54:57 -07004401 u32 inta_mask;
4402#endif
4403
4404 spin_lock_irqsave(&priv->lock, flags);
4405
4406 /* Ack/clear/reset pending uCode interrupts.
4407 * Note: Some bits in CSR_INT are "OR" of bits in CSR_FH_INT_STATUS,
4408 * and will clear only when CSR_FH_INT_STATUS gets cleared. */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004409 inta = iwl_read32(priv, CSR_INT);
4410 iwl_write32(priv, CSR_INT, inta);
Zhu Yib481de92007-09-25 17:54:57 -07004411
4412 /* Ack/clear/reset pending flow-handler (DMA) interrupts.
4413 * Any new interrupts that happen after this, either while we're
4414 * in this tasklet, or later, will show up in next ISR/tasklet. */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004415 inta_fh = iwl_read32(priv, CSR_FH_INT_STATUS);
4416 iwl_write32(priv, CSR_FH_INT_STATUS, inta_fh);
Zhu Yib481de92007-09-25 17:54:57 -07004417
Tomas Winkler0a6857e2008-03-12 16:58:49 -07004418#ifdef CONFIG_IWLWIFI_DEBUG
4419 if (iwl_debug_level & IWL_DL_ISR) {
Ben Cahill9fbab512007-11-29 11:09:47 +08004420 /* just for debug */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004421 inta_mask = iwl_read32(priv, CSR_INT_MASK);
Zhu Yib481de92007-09-25 17:54:57 -07004422 IWL_DEBUG_ISR("inta 0x%08x, enabled 0x%08x, fh 0x%08x\n",
4423 inta, inta_mask, inta_fh);
4424 }
4425#endif
4426
4427 /* Since CSR_INT and CSR_FH_INT_STATUS reads and clears are not
4428 * atomic, make sure that inta covers all the interrupts that
4429 * we've discovered, even if FH interrupt came in just after
4430 * reading CSR_INT. */
Tomas Winkler6f83eaa2008-03-04 18:09:28 -08004431 if (inta_fh & CSR49_FH_INT_RX_MASK)
Zhu Yib481de92007-09-25 17:54:57 -07004432 inta |= CSR_INT_BIT_FH_RX;
Tomas Winkler6f83eaa2008-03-04 18:09:28 -08004433 if (inta_fh & CSR49_FH_INT_TX_MASK)
Zhu Yib481de92007-09-25 17:54:57 -07004434 inta |= CSR_INT_BIT_FH_TX;
4435
4436 /* Now service all interrupt bits discovered above. */
4437 if (inta & CSR_INT_BIT_HW_ERR) {
4438 IWL_ERROR("Microcode HW error detected. Restarting.\n");
4439
4440 /* Tell the device to stop sending interrupts */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004441 iwl4965_disable_interrupts(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004442
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004443 iwl4965_irq_handle_error(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004444
4445 handled |= CSR_INT_BIT_HW_ERR;
4446
4447 spin_unlock_irqrestore(&priv->lock, flags);
4448
4449 return;
4450 }
4451
Tomas Winkler0a6857e2008-03-12 16:58:49 -07004452#ifdef CONFIG_IWLWIFI_DEBUG
4453 if (iwl_debug_level & (IWL_DL_ISR)) {
Zhu Yib481de92007-09-25 17:54:57 -07004454 /* NIC fires this, but we don't use it, redundant with WAKEUP */
Joonwoo Park25c03d82008-01-23 10:15:20 -08004455 if (inta & CSR_INT_BIT_SCD)
4456 IWL_DEBUG_ISR("Scheduler finished to transmit "
4457 "the frame/frames.\n");
Zhu Yib481de92007-09-25 17:54:57 -07004458
4459 /* Alive notification via Rx interrupt will do the real work */
4460 if (inta & CSR_INT_BIT_ALIVE)
4461 IWL_DEBUG_ISR("Alive interrupt\n");
4462 }
4463#endif
4464 /* Safely ignore these bits for debug checks below */
Joonwoo Park25c03d82008-01-23 10:15:20 -08004465 inta &= ~(CSR_INT_BIT_SCD | CSR_INT_BIT_ALIVE);
Zhu Yib481de92007-09-25 17:54:57 -07004466
Ben Cahill9fbab512007-11-29 11:09:47 +08004467 /* HW RF KILL switch toggled */
Zhu Yib481de92007-09-25 17:54:57 -07004468 if (inta & CSR_INT_BIT_RF_KILL) {
4469 int hw_rf_kill = 0;
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004470 if (!(iwl_read32(priv, CSR_GP_CNTRL) &
Zhu Yib481de92007-09-25 17:54:57 -07004471 CSR_GP_CNTRL_REG_FLAG_HW_RF_KILL_SW))
4472 hw_rf_kill = 1;
4473
4474 IWL_DEBUG(IWL_DL_INFO | IWL_DL_RF_KILL | IWL_DL_ISR,
4475 "RF_KILL bit toggled to %s.\n",
4476 hw_rf_kill ? "disable radio":"enable radio");
4477
4478 /* Queue restart only if RF_KILL switch was set to "kill"
4479 * when we loaded driver, and is now set to "enable".
4480 * After we're Alive, RF_KILL gets handled by
Reinette Chatre32304552008-02-15 14:34:37 -08004481 * iwl4965_rx_card_state_notif() */
Zhu Yi53e49092007-12-06 16:08:44 +08004482 if (!hw_rf_kill && !test_bit(STATUS_ALIVE, &priv->status)) {
4483 clear_bit(STATUS_RF_KILL_HW, &priv->status);
Zhu Yib481de92007-09-25 17:54:57 -07004484 queue_work(priv->workqueue, &priv->restart);
Zhu Yi53e49092007-12-06 16:08:44 +08004485 }
Zhu Yib481de92007-09-25 17:54:57 -07004486
4487 handled |= CSR_INT_BIT_RF_KILL;
4488 }
4489
Ben Cahill9fbab512007-11-29 11:09:47 +08004490 /* Chip got too hot and stopped itself */
Zhu Yib481de92007-09-25 17:54:57 -07004491 if (inta & CSR_INT_BIT_CT_KILL) {
4492 IWL_ERROR("Microcode CT kill error detected.\n");
4493 handled |= CSR_INT_BIT_CT_KILL;
4494 }
4495
4496 /* Error detected by uCode */
4497 if (inta & CSR_INT_BIT_SW_ERR) {
4498 IWL_ERROR("Microcode SW error detected. Restarting 0x%X.\n",
4499 inta);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004500 iwl4965_irq_handle_error(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004501 handled |= CSR_INT_BIT_SW_ERR;
4502 }
4503
4504 /* uCode wakes up after power-down sleep */
4505 if (inta & CSR_INT_BIT_WAKEUP) {
4506 IWL_DEBUG_ISR("Wakeup interrupt\n");
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004507 iwl4965_rx_queue_update_write_ptr(priv, &priv->rxq);
4508 iwl4965_tx_queue_update_write_ptr(priv, &priv->txq[0]);
4509 iwl4965_tx_queue_update_write_ptr(priv, &priv->txq[1]);
4510 iwl4965_tx_queue_update_write_ptr(priv, &priv->txq[2]);
4511 iwl4965_tx_queue_update_write_ptr(priv, &priv->txq[3]);
4512 iwl4965_tx_queue_update_write_ptr(priv, &priv->txq[4]);
4513 iwl4965_tx_queue_update_write_ptr(priv, &priv->txq[5]);
Zhu Yib481de92007-09-25 17:54:57 -07004514
4515 handled |= CSR_INT_BIT_WAKEUP;
4516 }
4517
4518 /* All uCode command responses, including Tx command responses,
4519 * Rx "responses" (frame-received notification), and other
4520 * notifications from uCode come through here*/
4521 if (inta & (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX)) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004522 iwl4965_rx_handle(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004523 handled |= (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX);
4524 }
4525
4526 if (inta & CSR_INT_BIT_FH_TX) {
4527 IWL_DEBUG_ISR("Tx interrupt\n");
4528 handled |= CSR_INT_BIT_FH_TX;
4529 }
4530
4531 if (inta & ~handled)
4532 IWL_ERROR("Unhandled INTA bits 0x%08x\n", inta & ~handled);
4533
4534 if (inta & ~CSR_INI_SET_MASK) {
4535 IWL_WARNING("Disabled INTA bits 0x%08x were pending\n",
4536 inta & ~CSR_INI_SET_MASK);
4537 IWL_WARNING(" with FH_INT = 0x%08x\n", inta_fh);
4538 }
4539
4540 /* Re-enable all interrupts */
Mohamed Abbas0359fac2008-03-28 16:21:08 -07004541 /* only Re-enable if diabled by irq */
4542 if (test_bit(STATUS_INT_ENABLED, &priv->status))
4543 iwl4965_enable_interrupts(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004544
Tomas Winkler0a6857e2008-03-12 16:58:49 -07004545#ifdef CONFIG_IWLWIFI_DEBUG
4546 if (iwl_debug_level & (IWL_DL_ISR)) {
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004547 inta = iwl_read32(priv, CSR_INT);
4548 inta_mask = iwl_read32(priv, CSR_INT_MASK);
4549 inta_fh = iwl_read32(priv, CSR_FH_INT_STATUS);
Zhu Yib481de92007-09-25 17:54:57 -07004550 IWL_DEBUG_ISR("End inta 0x%08x, enabled 0x%08x, fh 0x%08x, "
4551 "flags 0x%08lx\n", inta, inta_mask, inta_fh, flags);
4552 }
4553#endif
4554 spin_unlock_irqrestore(&priv->lock, flags);
4555}
4556
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004557static irqreturn_t iwl4965_isr(int irq, void *data)
Zhu Yib481de92007-09-25 17:54:57 -07004558{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004559 struct iwl_priv *priv = data;
Zhu Yib481de92007-09-25 17:54:57 -07004560 u32 inta, inta_mask;
4561 u32 inta_fh;
4562 if (!priv)
4563 return IRQ_NONE;
4564
4565 spin_lock(&priv->lock);
4566
4567 /* Disable (but don't clear!) interrupts here to avoid
4568 * back-to-back ISRs and sporadic interrupts from our NIC.
4569 * If we have something to service, the tasklet will re-enable ints.
4570 * If we *don't* have something, we'll re-enable before leaving here. */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004571 inta_mask = iwl_read32(priv, CSR_INT_MASK); /* just for debug */
4572 iwl_write32(priv, CSR_INT_MASK, 0x00000000);
Zhu Yib481de92007-09-25 17:54:57 -07004573
4574 /* Discover which interrupts are active/pending */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004575 inta = iwl_read32(priv, CSR_INT);
4576 inta_fh = iwl_read32(priv, CSR_FH_INT_STATUS);
Zhu Yib481de92007-09-25 17:54:57 -07004577
4578 /* Ignore interrupt if there's nothing in NIC to service.
4579 * This may be due to IRQ shared with another device,
4580 * or due to sporadic interrupts thrown from our NIC. */
4581 if (!inta && !inta_fh) {
4582 IWL_DEBUG_ISR("Ignore interrupt, inta == 0, inta_fh == 0\n");
4583 goto none;
4584 }
4585
4586 if ((inta == 0xFFFFFFFF) || ((inta & 0xFFFFFFF0) == 0xa5a5a5a0)) {
Oliver Neukum66fbb542007-11-15 09:31:10 +08004587 /* Hardware disappeared. It might have already raised
4588 * an interrupt */
Zhu Yib481de92007-09-25 17:54:57 -07004589 IWL_WARNING("HARDWARE GONE?? INTA == 0x%080x\n", inta);
Oliver Neukum66fbb542007-11-15 09:31:10 +08004590 goto unplugged;
Zhu Yib481de92007-09-25 17:54:57 -07004591 }
4592
4593 IWL_DEBUG_ISR("ISR inta 0x%08x, enabled 0x%08x, fh 0x%08x\n",
4594 inta, inta_mask, inta_fh);
4595
Joonwoo Park25c03d82008-01-23 10:15:20 -08004596 inta &= ~CSR_INT_BIT_SCD;
4597
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004598 /* iwl4965_irq_tasklet() will service interrupts and re-enable them */
Joonwoo Park25c03d82008-01-23 10:15:20 -08004599 if (likely(inta || inta_fh))
4600 tasklet_schedule(&priv->irq_tasklet);
Zhu Yib481de92007-09-25 17:54:57 -07004601
Oliver Neukum66fbb542007-11-15 09:31:10 +08004602 unplugged:
4603 spin_unlock(&priv->lock);
Zhu Yib481de92007-09-25 17:54:57 -07004604 return IRQ_HANDLED;
4605
4606 none:
4607 /* re-enable interrupts here since we don't have anything to service. */
Mohamed Abbas0359fac2008-03-28 16:21:08 -07004608 /* only Re-enable if diabled by irq */
4609 if (test_bit(STATUS_INT_ENABLED, &priv->status))
4610 iwl4965_enable_interrupts(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004611 spin_unlock(&priv->lock);
4612 return IRQ_NONE;
4613}
4614
Zhu Yib481de92007-09-25 17:54:57 -07004615/* For active scan, listen ACTIVE_DWELL_TIME (msec) on each channel after
4616 * sending probe req. This should be set long enough to hear probe responses
4617 * from more than one AP. */
4618#define IWL_ACTIVE_DWELL_TIME_24 (20) /* all times in msec */
4619#define IWL_ACTIVE_DWELL_TIME_52 (10)
4620
4621/* For faster active scanning, scan will move to the next channel if fewer than
4622 * PLCP_QUIET_THRESH packets are heard on this channel within
4623 * ACTIVE_QUIET_TIME after sending probe request. This shortens the dwell
4624 * time if it's a quiet channel (nothing responded to our probe, and there's
4625 * no other traffic).
4626 * Disable "quiet" feature by setting PLCP_QUIET_THRESH to 0. */
4627#define IWL_PLCP_QUIET_THRESH __constant_cpu_to_le16(1) /* packets */
4628#define IWL_ACTIVE_QUIET_TIME __constant_cpu_to_le16(5) /* msec */
4629
4630/* For passive scan, listen PASSIVE_DWELL_TIME (msec) on each channel.
4631 * Must be set longer than active dwell time.
4632 * For the most reliable scan, set > AP beacon interval (typically 100msec). */
4633#define IWL_PASSIVE_DWELL_TIME_24 (20) /* all times in msec */
4634#define IWL_PASSIVE_DWELL_TIME_52 (10)
4635#define IWL_PASSIVE_DWELL_BASE (100)
4636#define IWL_CHANNEL_TUNE_TIME 5
4637
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004638static inline u16 iwl4965_get_active_dwell_time(struct iwl_priv *priv,
Johannes Berg8318d782008-01-24 19:38:38 +01004639 enum ieee80211_band band)
Zhu Yib481de92007-09-25 17:54:57 -07004640{
Johannes Berg8318d782008-01-24 19:38:38 +01004641 if (band == IEEE80211_BAND_5GHZ)
Zhu Yib481de92007-09-25 17:54:57 -07004642 return IWL_ACTIVE_DWELL_TIME_52;
4643 else
4644 return IWL_ACTIVE_DWELL_TIME_24;
4645}
4646
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004647static u16 iwl4965_get_passive_dwell_time(struct iwl_priv *priv,
Johannes Berg8318d782008-01-24 19:38:38 +01004648 enum ieee80211_band band)
Zhu Yib481de92007-09-25 17:54:57 -07004649{
Johannes Berg8318d782008-01-24 19:38:38 +01004650 u16 active = iwl4965_get_active_dwell_time(priv, band);
4651 u16 passive = (band != IEEE80211_BAND_5GHZ) ?
Zhu Yib481de92007-09-25 17:54:57 -07004652 IWL_PASSIVE_DWELL_BASE + IWL_PASSIVE_DWELL_TIME_24 :
4653 IWL_PASSIVE_DWELL_BASE + IWL_PASSIVE_DWELL_TIME_52;
4654
Tomas Winkler3109ece2008-03-28 16:33:35 -07004655 if (iwl_is_associated(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07004656 /* If we're associated, we clamp the maximum passive
4657 * dwell time to be 98% of the beacon interval (minus
4658 * 2 * channel tune time) */
4659 passive = priv->beacon_int;
4660 if ((passive > IWL_PASSIVE_DWELL_BASE) || !passive)
4661 passive = IWL_PASSIVE_DWELL_BASE;
4662 passive = (passive * 98) / 100 - IWL_CHANNEL_TUNE_TIME * 2;
4663 }
4664
4665 if (passive <= active)
4666 passive = active + 1;
4667
4668 return passive;
4669}
4670
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004671static int iwl4965_get_channels_for_scan(struct iwl_priv *priv,
Johannes Berg8318d782008-01-24 19:38:38 +01004672 enum ieee80211_band band,
Zhu Yib481de92007-09-25 17:54:57 -07004673 u8 is_active, u8 direct_mask,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004674 struct iwl4965_scan_channel *scan_ch)
Zhu Yib481de92007-09-25 17:54:57 -07004675{
4676 const struct ieee80211_channel *channels = NULL;
Johannes Berg8318d782008-01-24 19:38:38 +01004677 const struct ieee80211_supported_band *sband;
Assaf Kraussbf85ea42008-03-14 10:38:49 -07004678 const struct iwl_channel_info *ch_info;
Zhu Yib481de92007-09-25 17:54:57 -07004679 u16 passive_dwell = 0;
4680 u16 active_dwell = 0;
4681 int added, i;
4682
Johannes Berg8318d782008-01-24 19:38:38 +01004683 sband = iwl4965_get_hw_mode(priv, band);
4684 if (!sband)
Zhu Yib481de92007-09-25 17:54:57 -07004685 return 0;
4686
Johannes Berg8318d782008-01-24 19:38:38 +01004687 channels = sband->channels;
Zhu Yib481de92007-09-25 17:54:57 -07004688
Johannes Berg8318d782008-01-24 19:38:38 +01004689 active_dwell = iwl4965_get_active_dwell_time(priv, band);
4690 passive_dwell = iwl4965_get_passive_dwell_time(priv, band);
Zhu Yib481de92007-09-25 17:54:57 -07004691
Johannes Berg8318d782008-01-24 19:38:38 +01004692 for (i = 0, added = 0; i < sband->n_channels; i++) {
Johannes Berg182e2e62008-04-04 10:41:56 +02004693 if (channels[i].flags & IEEE80211_CHAN_DISABLED)
4694 continue;
4695
Johannes Berg8318d782008-01-24 19:38:38 +01004696 if (ieee80211_frequency_to_channel(channels[i].center_freq) ==
Zhu Yib481de92007-09-25 17:54:57 -07004697 le16_to_cpu(priv->active_rxon.channel)) {
Tomas Winkler3109ece2008-03-28 16:33:35 -07004698 if (iwl_is_associated(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07004699 IWL_DEBUG_SCAN
4700 ("Skipping current channel %d\n",
4701 le16_to_cpu(priv->active_rxon.channel));
4702 continue;
4703 }
4704 } else if (priv->only_active_channel)
4705 continue;
4706
Johannes Berg8318d782008-01-24 19:38:38 +01004707 scan_ch->channel = ieee80211_frequency_to_channel(channels[i].center_freq);
Zhu Yib481de92007-09-25 17:54:57 -07004708
Assaf Krauss8622e702008-03-21 13:53:43 -07004709 ch_info = iwl_get_channel_info(priv, band,
Ben Cahill9fbab512007-11-29 11:09:47 +08004710 scan_ch->channel);
Zhu Yib481de92007-09-25 17:54:57 -07004711 if (!is_channel_valid(ch_info)) {
4712 IWL_DEBUG_SCAN("Channel %d is INVALID for this SKU.\n",
4713 scan_ch->channel);
4714 continue;
4715 }
4716
4717 if (!is_active || is_channel_passive(ch_info) ||
Johannes Berg8318d782008-01-24 19:38:38 +01004718 (channels[i].flags & IEEE80211_CHAN_PASSIVE_SCAN))
Zhu Yib481de92007-09-25 17:54:57 -07004719 scan_ch->type = 0; /* passive */
4720 else
4721 scan_ch->type = 1; /* active */
4722
4723 if (scan_ch->type & 1)
4724 scan_ch->type |= (direct_mask << 1);
4725
4726 if (is_channel_narrow(ch_info))
4727 scan_ch->type |= (1 << 7);
4728
4729 scan_ch->active_dwell = cpu_to_le16(active_dwell);
4730 scan_ch->passive_dwell = cpu_to_le16(passive_dwell);
4731
Ben Cahill9fbab512007-11-29 11:09:47 +08004732 /* Set txpower levels to defaults */
Zhu Yib481de92007-09-25 17:54:57 -07004733 scan_ch->tpc.dsp_atten = 110;
4734 /* scan_pwr_info->tpc.dsp_atten; */
4735
4736 /*scan_pwr_info->tpc.tx_gain; */
Johannes Berg8318d782008-01-24 19:38:38 +01004737 if (band == IEEE80211_BAND_5GHZ)
Zhu Yib481de92007-09-25 17:54:57 -07004738 scan_ch->tpc.tx_gain = ((1 << 5) | (3 << 3)) | 3;
4739 else {
4740 scan_ch->tpc.tx_gain = ((1 << 5) | (5 << 3));
4741 /* NOTE: if we were doing 6Mb OFDM for scans we'd use
Ben Cahill9fbab512007-11-29 11:09:47 +08004742 * power level:
Reinette Chatre8a1b0242008-01-14 17:46:25 -08004743 * scan_ch->tpc.tx_gain = ((1 << 5) | (2 << 3)) | 3;
Zhu Yib481de92007-09-25 17:54:57 -07004744 */
4745 }
4746
4747 IWL_DEBUG_SCAN("Scanning %d [%s %d]\n",
4748 scan_ch->channel,
4749 (scan_ch->type & 1) ? "ACTIVE" : "PASSIVE",
4750 (scan_ch->type & 1) ?
4751 active_dwell : passive_dwell);
4752
4753 scan_ch++;
4754 added++;
4755 }
4756
4757 IWL_DEBUG_SCAN("total channels to scan %d \n", added);
4758 return added;
4759}
4760
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004761static void iwl4965_init_hw_rates(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07004762 struct ieee80211_rate *rates)
4763{
4764 int i;
4765
4766 for (i = 0; i < IWL_RATE_COUNT; i++) {
Johannes Berg8318d782008-01-24 19:38:38 +01004767 rates[i].bitrate = iwl4965_rates[i].ieee * 5;
4768 rates[i].hw_value = i; /* Rate scaling will work on indexes */
4769 rates[i].hw_value_short = i;
4770 rates[i].flags = 0;
4771 if ((i > IWL_LAST_OFDM_RATE) || (i < IWL_FIRST_OFDM_RATE)) {
Zhu Yib481de92007-09-25 17:54:57 -07004772 /*
Johannes Berg8318d782008-01-24 19:38:38 +01004773 * If CCK != 1M then set short preamble rate flag.
Zhu Yib481de92007-09-25 17:54:57 -07004774 */
Tomas Winkler35cdeaf2008-03-11 16:17:20 -07004775 rates[i].flags |=
4776 (iwl4965_rates[i].plcp == IWL_RATE_1M_PLCP) ?
4777 0 : IEEE80211_RATE_SHORT_PREAMBLE;
Zhu Yib481de92007-09-25 17:54:57 -07004778 }
Zhu Yib481de92007-09-25 17:54:57 -07004779 }
Zhu Yib481de92007-09-25 17:54:57 -07004780}
4781
4782/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004783 * iwl4965_init_geos - Initialize mac80211's geo/channel info based from eeprom
Zhu Yib481de92007-09-25 17:54:57 -07004784 */
Assaf Kraussbf85ea42008-03-14 10:38:49 -07004785int iwl4965_init_geos(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004786{
Assaf Kraussbf85ea42008-03-14 10:38:49 -07004787 struct iwl_channel_info *ch;
Tomas Winkler8211ef72008-03-02 01:36:04 +02004788 struct ieee80211_supported_band *sband;
Zhu Yib481de92007-09-25 17:54:57 -07004789 struct ieee80211_channel *channels;
4790 struct ieee80211_channel *geo_ch;
4791 struct ieee80211_rate *rates;
4792 int i = 0;
Zhu Yib481de92007-09-25 17:54:57 -07004793
Johannes Berg8318d782008-01-24 19:38:38 +01004794 if (priv->bands[IEEE80211_BAND_2GHZ].n_bitrates ||
4795 priv->bands[IEEE80211_BAND_5GHZ].n_bitrates) {
Zhu Yib481de92007-09-25 17:54:57 -07004796 IWL_DEBUG_INFO("Geography modes already initialized.\n");
4797 set_bit(STATUS_GEO_CONFIGURED, &priv->status);
4798 return 0;
4799 }
4800
Zhu Yib481de92007-09-25 17:54:57 -07004801 channels = kzalloc(sizeof(struct ieee80211_channel) *
4802 priv->channel_count, GFP_KERNEL);
Johannes Berg8318d782008-01-24 19:38:38 +01004803 if (!channels)
Zhu Yib481de92007-09-25 17:54:57 -07004804 return -ENOMEM;
Zhu Yib481de92007-09-25 17:54:57 -07004805
Tomas Winkler8211ef72008-03-02 01:36:04 +02004806 rates = kzalloc((sizeof(struct ieee80211_rate) * (IWL_RATE_COUNT + 1)),
Zhu Yib481de92007-09-25 17:54:57 -07004807 GFP_KERNEL);
4808 if (!rates) {
Zhu Yib481de92007-09-25 17:54:57 -07004809 kfree(channels);
4810 return -ENOMEM;
4811 }
4812
Zhu Yib481de92007-09-25 17:54:57 -07004813 /* 5.2GHz channels start after the 2.4GHz channels */
Tomas Winkler8211ef72008-03-02 01:36:04 +02004814 sband = &priv->bands[IEEE80211_BAND_5GHZ];
Assaf Kraussbf85ea42008-03-14 10:38:49 -07004815 sband->channels = &channels[ARRAY_SIZE(iwl_eeprom_band_1)];
Tomas Winkler8211ef72008-03-02 01:36:04 +02004816 /* just OFDM */
4817 sband->bitrates = &rates[IWL_FIRST_OFDM_RATE];
4818 sband->n_bitrates = IWL_RATE_COUNT - IWL_FIRST_OFDM_RATE;
Johannes Berg8318d782008-01-24 19:38:38 +01004819
Assaf Krauss1ea87392008-03-18 14:57:50 -07004820 iwl4965_init_ht_hw_capab(priv, &sband->ht_info, IEEE80211_BAND_5GHZ);
Tomas Winkler78330fd2008-02-06 02:37:18 +02004821
Tomas Winkler8211ef72008-03-02 01:36:04 +02004822 sband = &priv->bands[IEEE80211_BAND_2GHZ];
4823 sband->channels = channels;
4824 /* OFDM & CCK */
4825 sband->bitrates = rates;
4826 sband->n_bitrates = IWL_RATE_COUNT;
Zhu Yib481de92007-09-25 17:54:57 -07004827
Assaf Krauss1ea87392008-03-18 14:57:50 -07004828 iwl4965_init_ht_hw_capab(priv, &sband->ht_info, IEEE80211_BAND_2GHZ);
Tomas Winkler78330fd2008-02-06 02:37:18 +02004829
Zhu Yib481de92007-09-25 17:54:57 -07004830 priv->ieee_channels = channels;
4831 priv->ieee_rates = rates;
4832
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004833 iwl4965_init_hw_rates(priv, rates);
Zhu Yib481de92007-09-25 17:54:57 -07004834
Tomas Winkler8211ef72008-03-02 01:36:04 +02004835 for (i = 0; i < priv->channel_count; i++) {
Zhu Yib481de92007-09-25 17:54:57 -07004836 ch = &priv->channel_info[i];
4837
Tomas Winkler8211ef72008-03-02 01:36:04 +02004838 /* FIXME: might be removed if scan is OK */
4839 if (!is_channel_valid(ch))
Zhu Yib481de92007-09-25 17:54:57 -07004840 continue;
Zhu Yib481de92007-09-25 17:54:57 -07004841
Tomas Winkler8211ef72008-03-02 01:36:04 +02004842 if (is_channel_a_band(ch))
4843 sband = &priv->bands[IEEE80211_BAND_5GHZ];
4844 else
4845 sband = &priv->bands[IEEE80211_BAND_2GHZ];
Zhu Yib481de92007-09-25 17:54:57 -07004846
Tomas Winkler8211ef72008-03-02 01:36:04 +02004847 geo_ch = &sband->channels[sband->n_channels++];
4848
4849 geo_ch->center_freq = ieee80211_channel_to_frequency(ch->channel);
Johannes Berg8318d782008-01-24 19:38:38 +01004850 geo_ch->max_power = ch->max_power_avg;
4851 geo_ch->max_antenna_gain = 0xff;
Mohamed Abbas7b723042008-01-31 21:46:40 -08004852 geo_ch->hw_value = ch->channel;
Zhu Yib481de92007-09-25 17:54:57 -07004853
4854 if (is_channel_valid(ch)) {
Johannes Berg8318d782008-01-24 19:38:38 +01004855 if (!(ch->flags & EEPROM_CHANNEL_IBSS))
4856 geo_ch->flags |= IEEE80211_CHAN_NO_IBSS;
Zhu Yib481de92007-09-25 17:54:57 -07004857
Johannes Berg8318d782008-01-24 19:38:38 +01004858 if (!(ch->flags & EEPROM_CHANNEL_ACTIVE))
4859 geo_ch->flags |= IEEE80211_CHAN_PASSIVE_SCAN;
Zhu Yib481de92007-09-25 17:54:57 -07004860
4861 if (ch->flags & EEPROM_CHANNEL_RADAR)
Johannes Berg8318d782008-01-24 19:38:38 +01004862 geo_ch->flags |= IEEE80211_CHAN_RADAR;
Zhu Yib481de92007-09-25 17:54:57 -07004863
4864 if (ch->max_power_avg > priv->max_channel_txpower_limit)
4865 priv->max_channel_txpower_limit =
4866 ch->max_power_avg;
Tomas Winkler8211ef72008-03-02 01:36:04 +02004867 } else {
Johannes Berg8318d782008-01-24 19:38:38 +01004868 geo_ch->flags |= IEEE80211_CHAN_DISABLED;
Tomas Winkler8211ef72008-03-02 01:36:04 +02004869 }
4870
4871 /* Save flags for reg domain usage */
4872 geo_ch->orig_flags = geo_ch->flags;
4873
4874 IWL_DEBUG_INFO("Channel %d Freq=%d[%sGHz] %s flag=0%X\n",
4875 ch->channel, geo_ch->center_freq,
4876 is_channel_a_band(ch) ? "5.2" : "2.4",
4877 geo_ch->flags & IEEE80211_CHAN_DISABLED ?
4878 "restricted" : "valid",
4879 geo_ch->flags);
Zhu Yib481de92007-09-25 17:54:57 -07004880 }
4881
Tomas Winkler82b9a122008-03-04 18:09:30 -08004882 if ((priv->bands[IEEE80211_BAND_5GHZ].n_channels == 0) &&
4883 priv->cfg->sku & IWL_SKU_A) {
Zhu Yib481de92007-09-25 17:54:57 -07004884 printk(KERN_INFO DRV_NAME
4885 ": Incorrectly detected BG card as ABG. Please send "
4886 "your PCI ID 0x%04X:0x%04X to maintainer.\n",
4887 priv->pci_dev->device, priv->pci_dev->subsystem_device);
Tomas Winkler82b9a122008-03-04 18:09:30 -08004888 priv->cfg->sku &= ~IWL_SKU_A;
Zhu Yib481de92007-09-25 17:54:57 -07004889 }
4890
4891 printk(KERN_INFO DRV_NAME
4892 ": Tunable channels: %d 802.11bg, %d 802.11a channels\n",
Johannes Berg8318d782008-01-24 19:38:38 +01004893 priv->bands[IEEE80211_BAND_2GHZ].n_channels,
4894 priv->bands[IEEE80211_BAND_5GHZ].n_channels);
Zhu Yib481de92007-09-25 17:54:57 -07004895
John W. Linvillee0e0a672008-03-25 15:58:40 -04004896 if (priv->bands[IEEE80211_BAND_2GHZ].n_channels)
4897 priv->hw->wiphy->bands[IEEE80211_BAND_2GHZ] =
4898 &priv->bands[IEEE80211_BAND_2GHZ];
4899 if (priv->bands[IEEE80211_BAND_5GHZ].n_channels)
4900 priv->hw->wiphy->bands[IEEE80211_BAND_5GHZ] =
4901 &priv->bands[IEEE80211_BAND_5GHZ];
Zhu Yib481de92007-09-25 17:54:57 -07004902
Zhu Yib481de92007-09-25 17:54:57 -07004903 set_bit(STATUS_GEO_CONFIGURED, &priv->status);
4904
4905 return 0;
4906}
4907
Reinette Chatre849e0dc2008-01-23 10:15:18 -08004908/*
4909 * iwl4965_free_geos - undo allocations in iwl4965_init_geos
4910 */
Assaf Kraussbf85ea42008-03-14 10:38:49 -07004911void iwl4965_free_geos(struct iwl_priv *priv)
Reinette Chatre849e0dc2008-01-23 10:15:18 -08004912{
Reinette Chatre849e0dc2008-01-23 10:15:18 -08004913 kfree(priv->ieee_channels);
4914 kfree(priv->ieee_rates);
4915 clear_bit(STATUS_GEO_CONFIGURED, &priv->status);
4916}
4917
Zhu Yib481de92007-09-25 17:54:57 -07004918/******************************************************************************
4919 *
4920 * uCode download functions
4921 *
4922 ******************************************************************************/
4923
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004924static void iwl4965_dealloc_ucode_pci(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004925{
Tomas Winkler98c92212008-01-14 17:46:20 -08004926 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_code);
4927 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_data);
4928 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_data_backup);
4929 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_init);
4930 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_init_data);
4931 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_boot);
Zhu Yib481de92007-09-25 17:54:57 -07004932}
4933
4934/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004935 * iwl4965_verify_inst_full - verify runtime uCode image in card vs. host,
Zhu Yib481de92007-09-25 17:54:57 -07004936 * looking at all data.
4937 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004938static int iwl4965_verify_inst_full(struct iwl_priv *priv, __le32 *image,
Ben Cahill9fbab512007-11-29 11:09:47 +08004939 u32 len)
Zhu Yib481de92007-09-25 17:54:57 -07004940{
4941 u32 val;
4942 u32 save_len = len;
4943 int rc = 0;
4944 u32 errcnt;
4945
4946 IWL_DEBUG_INFO("ucode inst image size is %u\n", len);
4947
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004948 rc = iwl_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004949 if (rc)
4950 return rc;
4951
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004952 iwl_write_direct32(priv, HBUS_TARG_MEM_RADDR, RTC_INST_LOWER_BOUND);
Zhu Yib481de92007-09-25 17:54:57 -07004953
4954 errcnt = 0;
4955 for (; len > 0; len -= sizeof(u32), image++) {
4956 /* read data comes through single port, auto-incr addr */
4957 /* NOTE: Use the debugless read so we don't flood kernel log
4958 * if IWL_DL_IO is set */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004959 val = _iwl_read_direct32(priv, HBUS_TARG_MEM_RDAT);
Zhu Yib481de92007-09-25 17:54:57 -07004960 if (val != le32_to_cpu(*image)) {
4961 IWL_ERROR("uCode INST section is invalid at "
4962 "offset 0x%x, is 0x%x, s/b 0x%x\n",
4963 save_len - len, val, le32_to_cpu(*image));
4964 rc = -EIO;
4965 errcnt++;
4966 if (errcnt >= 20)
4967 break;
4968 }
4969 }
4970
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004971 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004972
4973 if (!errcnt)
4974 IWL_DEBUG_INFO
4975 ("ucode image in INSTRUCTION memory is good\n");
4976
4977 return rc;
4978}
4979
4980
4981/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004982 * iwl4965_verify_inst_sparse - verify runtime uCode image in card vs. host,
Zhu Yib481de92007-09-25 17:54:57 -07004983 * using sample data 100 bytes apart. If these sample points are good,
4984 * it's a pretty good bet that everything between them is good, too.
4985 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004986static int iwl4965_verify_inst_sparse(struct iwl_priv *priv, __le32 *image, u32 len)
Zhu Yib481de92007-09-25 17:54:57 -07004987{
4988 u32 val;
4989 int rc = 0;
4990 u32 errcnt = 0;
4991 u32 i;
4992
4993 IWL_DEBUG_INFO("ucode inst image size is %u\n", len);
4994
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004995 rc = iwl_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004996 if (rc)
4997 return rc;
4998
4999 for (i = 0; i < len; i += 100, image += 100/sizeof(u32)) {
5000 /* read data comes through single port, auto-incr addr */
5001 /* NOTE: Use the debugless read so we don't flood kernel log
5002 * if IWL_DL_IO is set */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07005003 iwl_write_direct32(priv, HBUS_TARG_MEM_RADDR,
Zhu Yib481de92007-09-25 17:54:57 -07005004 i + RTC_INST_LOWER_BOUND);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07005005 val = _iwl_read_direct32(priv, HBUS_TARG_MEM_RDAT);
Zhu Yib481de92007-09-25 17:54:57 -07005006 if (val != le32_to_cpu(*image)) {
5007#if 0 /* Enable this if you want to see details */
5008 IWL_ERROR("uCode INST section is invalid at "
5009 "offset 0x%x, is 0x%x, s/b 0x%x\n",
5010 i, val, *image);
5011#endif
5012 rc = -EIO;
5013 errcnt++;
5014 if (errcnt >= 3)
5015 break;
5016 }
5017 }
5018
Tomas Winkler3395f6e2008-03-25 16:33:37 -07005019 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005020
5021 return rc;
5022}
5023
5024
5025/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005026 * iwl4965_verify_ucode - determine which instruction image is in SRAM,
Zhu Yib481de92007-09-25 17:54:57 -07005027 * and verify its contents
5028 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005029static int iwl4965_verify_ucode(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005030{
5031 __le32 *image;
5032 u32 len;
5033 int rc = 0;
5034
5035 /* Try bootstrap */
5036 image = (__le32 *)priv->ucode_boot.v_addr;
5037 len = priv->ucode_boot.len;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005038 rc = iwl4965_verify_inst_sparse(priv, image, len);
Zhu Yib481de92007-09-25 17:54:57 -07005039 if (rc == 0) {
5040 IWL_DEBUG_INFO("Bootstrap uCode is good in inst SRAM\n");
5041 return 0;
5042 }
5043
5044 /* Try initialize */
5045 image = (__le32 *)priv->ucode_init.v_addr;
5046 len = priv->ucode_init.len;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005047 rc = iwl4965_verify_inst_sparse(priv, image, len);
Zhu Yib481de92007-09-25 17:54:57 -07005048 if (rc == 0) {
5049 IWL_DEBUG_INFO("Initialize uCode is good in inst SRAM\n");
5050 return 0;
5051 }
5052
5053 /* Try runtime/protocol */
5054 image = (__le32 *)priv->ucode_code.v_addr;
5055 len = priv->ucode_code.len;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005056 rc = iwl4965_verify_inst_sparse(priv, image, len);
Zhu Yib481de92007-09-25 17:54:57 -07005057 if (rc == 0) {
5058 IWL_DEBUG_INFO("Runtime uCode is good in inst SRAM\n");
5059 return 0;
5060 }
5061
5062 IWL_ERROR("NO VALID UCODE IMAGE IN INSTRUCTION SRAM!!\n");
5063
Ben Cahill9fbab512007-11-29 11:09:47 +08005064 /* Since nothing seems to match, show first several data entries in
5065 * instruction SRAM, so maybe visual inspection will give a clue.
5066 * Selection of bootstrap image (vs. other images) is arbitrary. */
Zhu Yib481de92007-09-25 17:54:57 -07005067 image = (__le32 *)priv->ucode_boot.v_addr;
5068 len = priv->ucode_boot.len;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005069 rc = iwl4965_verify_inst_full(priv, image, len);
Zhu Yib481de92007-09-25 17:54:57 -07005070
5071 return rc;
5072}
5073
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005074static void iwl4965_nic_start(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005075{
5076 /* Remove all resets to allow NIC to operate */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07005077 iwl_write32(priv, CSR_RESET, 0);
Zhu Yib481de92007-09-25 17:54:57 -07005078}
5079
Tomas Winkler90e759d2007-11-29 11:09:41 +08005080
Zhu Yib481de92007-09-25 17:54:57 -07005081/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005082 * iwl4965_read_ucode - Read uCode images from disk file.
Zhu Yib481de92007-09-25 17:54:57 -07005083 *
5084 * Copy into buffers for card to fetch via bus-mastering
5085 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005086static int iwl4965_read_ucode(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005087{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005088 struct iwl4965_ucode *ucode;
Tomas Winkler90e759d2007-11-29 11:09:41 +08005089 int ret;
Zhu Yib481de92007-09-25 17:54:57 -07005090 const struct firmware *ucode_raw;
Tomas Winkler4bf775c2008-03-04 18:09:31 -08005091 const char *name = priv->cfg->fw_name;
Zhu Yib481de92007-09-25 17:54:57 -07005092 u8 *src;
5093 size_t len;
5094 u32 ver, inst_size, data_size, init_size, init_data_size, boot_size;
5095
5096 /* Ask kernel firmware_class module to get the boot firmware off disk.
5097 * request_firmware() is synchronous, file is in memory on return. */
Tomas Winkler90e759d2007-11-29 11:09:41 +08005098 ret = request_firmware(&ucode_raw, name, &priv->pci_dev->dev);
5099 if (ret < 0) {
5100 IWL_ERROR("%s firmware file req failed: Reason %d\n",
5101 name, ret);
Zhu Yib481de92007-09-25 17:54:57 -07005102 goto error;
5103 }
5104
5105 IWL_DEBUG_INFO("Got firmware '%s' file (%zd bytes) from disk\n",
5106 name, ucode_raw->size);
5107
5108 /* Make sure that we got at least our header! */
5109 if (ucode_raw->size < sizeof(*ucode)) {
5110 IWL_ERROR("File size way too small!\n");
Tomas Winkler90e759d2007-11-29 11:09:41 +08005111 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07005112 goto err_release;
5113 }
5114
5115 /* Data from ucode file: header followed by uCode images */
5116 ucode = (void *)ucode_raw->data;
5117
5118 ver = le32_to_cpu(ucode->ver);
5119 inst_size = le32_to_cpu(ucode->inst_size);
5120 data_size = le32_to_cpu(ucode->data_size);
5121 init_size = le32_to_cpu(ucode->init_size);
5122 init_data_size = le32_to_cpu(ucode->init_data_size);
5123 boot_size = le32_to_cpu(ucode->boot_size);
5124
5125 IWL_DEBUG_INFO("f/w package hdr ucode version = 0x%x\n", ver);
5126 IWL_DEBUG_INFO("f/w package hdr runtime inst size = %u\n",
5127 inst_size);
5128 IWL_DEBUG_INFO("f/w package hdr runtime data size = %u\n",
5129 data_size);
5130 IWL_DEBUG_INFO("f/w package hdr init inst size = %u\n",
5131 init_size);
5132 IWL_DEBUG_INFO("f/w package hdr init data size = %u\n",
5133 init_data_size);
5134 IWL_DEBUG_INFO("f/w package hdr boot inst size = %u\n",
5135 boot_size);
5136
5137 /* Verify size of file vs. image size info in file's header */
5138 if (ucode_raw->size < sizeof(*ucode) +
5139 inst_size + data_size + init_size +
5140 init_data_size + boot_size) {
5141
5142 IWL_DEBUG_INFO("uCode file size %d too small\n",
5143 (int)ucode_raw->size);
Tomas Winkler90e759d2007-11-29 11:09:41 +08005144 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07005145 goto err_release;
5146 }
5147
5148 /* Verify that uCode images will fit in card's SRAM */
5149 if (inst_size > IWL_MAX_INST_SIZE) {
Tomas Winkler90e759d2007-11-29 11:09:41 +08005150 IWL_DEBUG_INFO("uCode instr len %d too large to fit in\n",
5151 inst_size);
5152 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07005153 goto err_release;
5154 }
5155
5156 if (data_size > IWL_MAX_DATA_SIZE) {
Tomas Winkler90e759d2007-11-29 11:09:41 +08005157 IWL_DEBUG_INFO("uCode data len %d too large to fit in\n",
5158 data_size);
5159 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07005160 goto err_release;
5161 }
5162 if (init_size > IWL_MAX_INST_SIZE) {
5163 IWL_DEBUG_INFO
Tomas Winkler90e759d2007-11-29 11:09:41 +08005164 ("uCode init instr len %d too large to fit in\n",
5165 init_size);
5166 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07005167 goto err_release;
5168 }
5169 if (init_data_size > IWL_MAX_DATA_SIZE) {
5170 IWL_DEBUG_INFO
Tomas Winkler90e759d2007-11-29 11:09:41 +08005171 ("uCode init data len %d too large to fit in\n",
5172 init_data_size);
5173 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07005174 goto err_release;
5175 }
5176 if (boot_size > IWL_MAX_BSM_SIZE) {
5177 IWL_DEBUG_INFO
Tomas Winkler90e759d2007-11-29 11:09:41 +08005178 ("uCode boot instr len %d too large to fit in\n",
5179 boot_size);
5180 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07005181 goto err_release;
5182 }
5183
5184 /* Allocate ucode buffers for card's bus-master loading ... */
5185
5186 /* Runtime instructions and 2 copies of data:
5187 * 1) unmodified from disk
5188 * 2) backup cache for save/restore during power-downs */
5189 priv->ucode_code.len = inst_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08005190 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_code);
Zhu Yib481de92007-09-25 17:54:57 -07005191
5192 priv->ucode_data.len = data_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08005193 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_data);
Zhu Yib481de92007-09-25 17:54:57 -07005194
5195 priv->ucode_data_backup.len = data_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08005196 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_data_backup);
Zhu Yib481de92007-09-25 17:54:57 -07005197
5198 /* Initialization instructions and data */
Tomas Winkler90e759d2007-11-29 11:09:41 +08005199 if (init_size && init_data_size) {
5200 priv->ucode_init.len = init_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08005201 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_init);
Zhu Yib481de92007-09-25 17:54:57 -07005202
Tomas Winkler90e759d2007-11-29 11:09:41 +08005203 priv->ucode_init_data.len = init_data_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08005204 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_init_data);
Tomas Winkler90e759d2007-11-29 11:09:41 +08005205
5206 if (!priv->ucode_init.v_addr || !priv->ucode_init_data.v_addr)
5207 goto err_pci_alloc;
5208 }
Zhu Yib481de92007-09-25 17:54:57 -07005209
5210 /* Bootstrap (instructions only, no data) */
Tomas Winkler90e759d2007-11-29 11:09:41 +08005211 if (boot_size) {
5212 priv->ucode_boot.len = boot_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08005213 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_boot);
Zhu Yib481de92007-09-25 17:54:57 -07005214
Tomas Winkler90e759d2007-11-29 11:09:41 +08005215 if (!priv->ucode_boot.v_addr)
5216 goto err_pci_alloc;
5217 }
Zhu Yib481de92007-09-25 17:54:57 -07005218
5219 /* Copy images into buffers for card's bus-master reads ... */
5220
5221 /* Runtime instructions (first block of data in file) */
5222 src = &ucode->data[0];
5223 len = priv->ucode_code.len;
Tomas Winkler90e759d2007-11-29 11:09:41 +08005224 IWL_DEBUG_INFO("Copying (but not loading) uCode instr len %Zd\n", len);
Zhu Yib481de92007-09-25 17:54:57 -07005225 memcpy(priv->ucode_code.v_addr, src, len);
5226 IWL_DEBUG_INFO("uCode instr buf vaddr = 0x%p, paddr = 0x%08x\n",
5227 priv->ucode_code.v_addr, (u32)priv->ucode_code.p_addr);
5228
5229 /* Runtime data (2nd block)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005230 * NOTE: Copy into backup buffer will be done in iwl4965_up() */
Zhu Yib481de92007-09-25 17:54:57 -07005231 src = &ucode->data[inst_size];
5232 len = priv->ucode_data.len;
Tomas Winkler90e759d2007-11-29 11:09:41 +08005233 IWL_DEBUG_INFO("Copying (but not loading) uCode data len %Zd\n", len);
Zhu Yib481de92007-09-25 17:54:57 -07005234 memcpy(priv->ucode_data.v_addr, src, len);
5235 memcpy(priv->ucode_data_backup.v_addr, src, len);
5236
5237 /* Initialization instructions (3rd block) */
5238 if (init_size) {
5239 src = &ucode->data[inst_size + data_size];
5240 len = priv->ucode_init.len;
Tomas Winkler90e759d2007-11-29 11:09:41 +08005241 IWL_DEBUG_INFO("Copying (but not loading) init instr len %Zd\n",
5242 len);
Zhu Yib481de92007-09-25 17:54:57 -07005243 memcpy(priv->ucode_init.v_addr, src, len);
5244 }
5245
5246 /* Initialization data (4th block) */
5247 if (init_data_size) {
5248 src = &ucode->data[inst_size + data_size + init_size];
5249 len = priv->ucode_init_data.len;
Tomas Winkler90e759d2007-11-29 11:09:41 +08005250 IWL_DEBUG_INFO("Copying (but not loading) init data len %Zd\n",
5251 len);
Zhu Yib481de92007-09-25 17:54:57 -07005252 memcpy(priv->ucode_init_data.v_addr, src, len);
5253 }
5254
5255 /* Bootstrap instructions (5th block) */
5256 src = &ucode->data[inst_size + data_size + init_size + init_data_size];
5257 len = priv->ucode_boot.len;
Tomas Winkler90e759d2007-11-29 11:09:41 +08005258 IWL_DEBUG_INFO("Copying (but not loading) boot instr len %Zd\n", len);
Zhu Yib481de92007-09-25 17:54:57 -07005259 memcpy(priv->ucode_boot.v_addr, src, len);
5260
5261 /* We have our copies now, allow OS release its copies */
5262 release_firmware(ucode_raw);
5263 return 0;
5264
5265 err_pci_alloc:
5266 IWL_ERROR("failed to allocate pci memory\n");
Tomas Winkler90e759d2007-11-29 11:09:41 +08005267 ret = -ENOMEM;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005268 iwl4965_dealloc_ucode_pci(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005269
5270 err_release:
5271 release_firmware(ucode_raw);
5272
5273 error:
Tomas Winkler90e759d2007-11-29 11:09:41 +08005274 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07005275}
5276
5277
5278/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005279 * iwl4965_set_ucode_ptrs - Set uCode address location
Zhu Yib481de92007-09-25 17:54:57 -07005280 *
5281 * Tell initialization uCode where to find runtime uCode.
5282 *
5283 * BSM registers initially contain pointers to initialization uCode.
5284 * We need to replace them to load runtime uCode inst and data,
5285 * and to save runtime data when powering down.
5286 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005287static int iwl4965_set_ucode_ptrs(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005288{
5289 dma_addr_t pinst;
5290 dma_addr_t pdata;
5291 int rc = 0;
5292 unsigned long flags;
5293
5294 /* bits 35:4 for 4965 */
5295 pinst = priv->ucode_code.p_addr >> 4;
5296 pdata = priv->ucode_data_backup.p_addr >> 4;
5297
5298 spin_lock_irqsave(&priv->lock, flags);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07005299 rc = iwl_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005300 if (rc) {
5301 spin_unlock_irqrestore(&priv->lock, flags);
5302 return rc;
5303 }
5304
5305 /* Tell bootstrap uCode where to find image to load */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07005306 iwl_write_prph(priv, BSM_DRAM_INST_PTR_REG, pinst);
5307 iwl_write_prph(priv, BSM_DRAM_DATA_PTR_REG, pdata);
5308 iwl_write_prph(priv, BSM_DRAM_DATA_BYTECOUNT_REG,
Zhu Yib481de92007-09-25 17:54:57 -07005309 priv->ucode_data.len);
5310
5311 /* Inst bytecount must be last to set up, bit 31 signals uCode
5312 * that all new ptr/size info is in place */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07005313 iwl_write_prph(priv, BSM_DRAM_INST_BYTECOUNT_REG,
Zhu Yib481de92007-09-25 17:54:57 -07005314 priv->ucode_code.len | BSM_DRAM_INST_LOAD);
5315
Tomas Winkler3395f6e2008-03-25 16:33:37 -07005316 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005317
5318 spin_unlock_irqrestore(&priv->lock, flags);
5319
5320 IWL_DEBUG_INFO("Runtime uCode pointers are set.\n");
5321
5322 return rc;
5323}
5324
5325/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005326 * iwl4965_init_alive_start - Called after REPLY_ALIVE notification received
Zhu Yib481de92007-09-25 17:54:57 -07005327 *
5328 * Called after REPLY_ALIVE notification received from "initialize" uCode.
5329 *
5330 * The 4965 "initialize" ALIVE reply contains calibration data for:
5331 * Voltage, temperature, and MIMO tx gain correction, now stored in priv
5332 * (3945 does not contain this data).
5333 *
5334 * Tell "initialize" uCode to go ahead and load the runtime uCode.
5335*/
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005336static void iwl4965_init_alive_start(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005337{
5338 /* Check alive response for "valid" sign from uCode */
5339 if (priv->card_alive_init.is_valid != UCODE_VALID_OK) {
5340 /* We had an error bringing up the hardware, so take it
5341 * all the way back down so we can try again */
5342 IWL_DEBUG_INFO("Initialize Alive failed.\n");
5343 goto restart;
5344 }
5345
5346 /* Bootstrap uCode has loaded initialize uCode ... verify inst image.
5347 * This is a paranoid check, because we would not have gotten the
5348 * "initialize" alive if code weren't properly loaded. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005349 if (iwl4965_verify_ucode(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07005350 /* Runtime instruction load was bad;
5351 * take it all the way back down so we can try again */
5352 IWL_DEBUG_INFO("Bad \"initialize\" uCode load.\n");
5353 goto restart;
5354 }
5355
5356 /* Calculate temperature */
5357 priv->temperature = iwl4965_get_temperature(priv);
5358
5359 /* Send pointers to protocol/runtime uCode image ... init code will
5360 * load and launch runtime uCode, which will send us another "Alive"
5361 * notification. */
5362 IWL_DEBUG_INFO("Initialization Alive received.\n");
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005363 if (iwl4965_set_ucode_ptrs(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07005364 /* Runtime instruction load won't happen;
5365 * take it all the way back down so we can try again */
5366 IWL_DEBUG_INFO("Couldn't set up uCode pointers.\n");
5367 goto restart;
5368 }
5369 return;
5370
5371 restart:
5372 queue_work(priv->workqueue, &priv->restart);
5373}
5374
5375
5376/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005377 * iwl4965_alive_start - called after REPLY_ALIVE notification received
Zhu Yib481de92007-09-25 17:54:57 -07005378 * from protocol/runtime uCode (initialization uCode's
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005379 * Alive gets handled by iwl4965_init_alive_start()).
Zhu Yib481de92007-09-25 17:54:57 -07005380 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005381static void iwl4965_alive_start(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005382{
Tomas Winkler57aab752008-04-14 21:16:03 -07005383 int ret = 0;
Zhu Yib481de92007-09-25 17:54:57 -07005384
5385 IWL_DEBUG_INFO("Runtime Alive received.\n");
5386
5387 if (priv->card_alive.is_valid != UCODE_VALID_OK) {
5388 /* We had an error bringing up the hardware, so take it
5389 * all the way back down so we can try again */
5390 IWL_DEBUG_INFO("Alive failed.\n");
5391 goto restart;
5392 }
5393
5394 /* Initialize uCode has loaded Runtime uCode ... verify inst image.
5395 * This is a paranoid check, because we would not have gotten the
5396 * "runtime" alive if code weren't properly loaded. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005397 if (iwl4965_verify_ucode(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07005398 /* Runtime instruction load was bad;
5399 * take it all the way back down so we can try again */
5400 IWL_DEBUG_INFO("Bad runtime uCode load.\n");
5401 goto restart;
5402 }
5403
Assaf Kraussbf85ea42008-03-14 10:38:49 -07005404 iwlcore_clear_stations_table(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005405
Tomas Winkler57aab752008-04-14 21:16:03 -07005406 ret = priv->cfg->ops->lib->alive_notify(priv);
5407 if (ret) {
Zhu Yib481de92007-09-25 17:54:57 -07005408 IWL_WARNING("Could not complete ALIVE transition [ntf]: %d\n",
Tomas Winkler57aab752008-04-14 21:16:03 -07005409 ret);
Zhu Yib481de92007-09-25 17:54:57 -07005410 goto restart;
5411 }
5412
Ben Cahill9fbab512007-11-29 11:09:47 +08005413 /* After the ALIVE response, we can send host commands to 4965 uCode */
Zhu Yib481de92007-09-25 17:54:57 -07005414 set_bit(STATUS_ALIVE, &priv->status);
5415
5416 /* Clear out the uCode error bit if it is set */
5417 clear_bit(STATUS_FW_ERROR, &priv->status);
5418
Tomas Winklerfee12472008-04-03 16:05:21 -07005419 if (iwl_is_rfkill(priv))
Zhu Yib481de92007-09-25 17:54:57 -07005420 return;
5421
Zhu Yi5a66926a2008-01-14 17:46:18 -08005422 ieee80211_start_queues(priv->hw);
Zhu Yib481de92007-09-25 17:54:57 -07005423
5424 priv->active_rate = priv->rates_mask;
5425 priv->active_rate_basic = priv->rates_mask & IWL_BASIC_RATES_MASK;
5426
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005427 iwl4965_send_power_mode(priv, IWL_POWER_LEVEL(priv->power_mode));
Zhu Yib481de92007-09-25 17:54:57 -07005428
Tomas Winkler3109ece2008-03-28 16:33:35 -07005429 if (iwl_is_associated(priv)) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005430 struct iwl4965_rxon_cmd *active_rxon =
5431 (struct iwl4965_rxon_cmd *)(&priv->active_rxon);
Zhu Yib481de92007-09-25 17:54:57 -07005432
5433 memcpy(&priv->staging_rxon, &priv->active_rxon,
5434 sizeof(priv->staging_rxon));
5435 active_rxon->filter_flags &= ~RXON_FILTER_ASSOC_MSK;
5436 } else {
5437 /* Initialize our rx_config data */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005438 iwl4965_connection_init_rx_config(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005439 memcpy(priv->staging_rxon.node_addr, priv->mac_addr, ETH_ALEN);
5440 }
5441
Ben Cahill9fbab512007-11-29 11:09:47 +08005442 /* Configure Bluetooth device coexistence support */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005443 iwl4965_send_bt_config(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005444
5445 /* Configure the adapter for unassociated operation */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005446 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005447
5448 /* At this point, the NIC is initialized and operational */
5449 priv->notif_missed_beacons = 0;
Zhu Yib481de92007-09-25 17:54:57 -07005450
5451 iwl4965_rf_kill_ct_config(priv);
Zhu Yi5a66926a2008-01-14 17:46:18 -08005452
Reinette Chatrefe00b5a2008-04-03 16:05:23 -07005453 iwl_leds_register(priv);
5454
Zhu Yib481de92007-09-25 17:54:57 -07005455 IWL_DEBUG_INFO("ALIVE processing complete.\n");
Rick Farringtona9f46782008-03-18 14:57:49 -07005456 set_bit(STATUS_READY, &priv->status);
Zhu Yi5a66926a2008-01-14 17:46:18 -08005457 wake_up_interruptible(&priv->wait_command_queue);
Zhu Yib481de92007-09-25 17:54:57 -07005458
5459 if (priv->error_recovering)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005460 iwl4965_error_recovery(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005461
Mohamed Abbasc8381fd2008-03-28 16:21:05 -07005462 iwlcore_low_level_notify(priv, IWLCORE_START_EVT);
Mohamed Abbas84363e62008-04-04 16:59:58 -07005463 ieee80211_notify_mac(priv->hw, IEEE80211_NOTIFY_RE_ASSOC);
Zhu Yib481de92007-09-25 17:54:57 -07005464 return;
5465
5466 restart:
5467 queue_work(priv->workqueue, &priv->restart);
5468}
5469
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005470static void iwl4965_cancel_deferred_work(struct iwl_priv *priv);
Zhu Yib481de92007-09-25 17:54:57 -07005471
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005472static void __iwl4965_down(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005473{
5474 unsigned long flags;
5475 int exit_pending = test_bit(STATUS_EXIT_PENDING, &priv->status);
5476 struct ieee80211_conf *conf = NULL;
5477
5478 IWL_DEBUG_INFO(DRV_NAME " is going down\n");
5479
5480 conf = ieee80211_get_hw_conf(priv->hw);
5481
5482 if (!exit_pending)
5483 set_bit(STATUS_EXIT_PENDING, &priv->status);
5484
Mohamed Abbasab53d8a2008-03-25 16:33:36 -07005485 iwl_leds_unregister(priv);
5486
Mohamed Abbasc8381fd2008-03-28 16:21:05 -07005487 iwlcore_low_level_notify(priv, IWLCORE_STOP_EVT);
5488
Assaf Kraussbf85ea42008-03-14 10:38:49 -07005489 iwlcore_clear_stations_table(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005490
5491 /* Unblock any waiting calls */
5492 wake_up_interruptible_all(&priv->wait_command_queue);
5493
Zhu Yib481de92007-09-25 17:54:57 -07005494 /* Wipe out the EXIT_PENDING status bit if we are not actually
5495 * exiting the module */
5496 if (!exit_pending)
5497 clear_bit(STATUS_EXIT_PENDING, &priv->status);
5498
5499 /* stop and reset the on-board processor */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07005500 iwl_write32(priv, CSR_RESET, CSR_RESET_REG_FLAG_NEVO_RESET);
Zhu Yib481de92007-09-25 17:54:57 -07005501
5502 /* tell the device to stop sending interrupts */
Mohamed Abbas0359fac2008-03-28 16:21:08 -07005503 spin_lock_irqsave(&priv->lock, flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005504 iwl4965_disable_interrupts(priv);
Mohamed Abbas0359fac2008-03-28 16:21:08 -07005505 spin_unlock_irqrestore(&priv->lock, flags);
5506 iwl_synchronize_irq(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005507
5508 if (priv->mac80211_registered)
5509 ieee80211_stop_queues(priv->hw);
5510
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005511 /* If we have not previously called iwl4965_init() then
Zhu Yib481de92007-09-25 17:54:57 -07005512 * clear all bits but the RF Kill and SUSPEND bits and return */
Tomas Winklerfee12472008-04-03 16:05:21 -07005513 if (!iwl_is_init(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07005514 priv->status = test_bit(STATUS_RF_KILL_HW, &priv->status) <<
5515 STATUS_RF_KILL_HW |
5516 test_bit(STATUS_RF_KILL_SW, &priv->status) <<
5517 STATUS_RF_KILL_SW |
Reinette Chatre97888642008-02-06 11:20:38 -08005518 test_bit(STATUS_GEO_CONFIGURED, &priv->status) <<
5519 STATUS_GEO_CONFIGURED |
Zhu Yib481de92007-09-25 17:54:57 -07005520 test_bit(STATUS_IN_SUSPEND, &priv->status) <<
5521 STATUS_IN_SUSPEND;
5522 goto exit;
5523 }
5524
5525 /* ...otherwise clear out all the status bits but the RF Kill and
5526 * SUSPEND bits and continue taking the NIC down. */
5527 priv->status &= test_bit(STATUS_RF_KILL_HW, &priv->status) <<
5528 STATUS_RF_KILL_HW |
5529 test_bit(STATUS_RF_KILL_SW, &priv->status) <<
5530 STATUS_RF_KILL_SW |
Reinette Chatre97888642008-02-06 11:20:38 -08005531 test_bit(STATUS_GEO_CONFIGURED, &priv->status) <<
5532 STATUS_GEO_CONFIGURED |
Zhu Yib481de92007-09-25 17:54:57 -07005533 test_bit(STATUS_IN_SUSPEND, &priv->status) <<
5534 STATUS_IN_SUSPEND |
5535 test_bit(STATUS_FW_ERROR, &priv->status) <<
5536 STATUS_FW_ERROR;
5537
5538 spin_lock_irqsave(&priv->lock, flags);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07005539 iwl_clear_bit(priv, CSR_GP_CNTRL,
Ben Cahill9fbab512007-11-29 11:09:47 +08005540 CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
Zhu Yib481de92007-09-25 17:54:57 -07005541 spin_unlock_irqrestore(&priv->lock, flags);
5542
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005543 iwl4965_hw_txq_ctx_stop(priv);
5544 iwl4965_hw_rxq_stop(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005545
5546 spin_lock_irqsave(&priv->lock, flags);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07005547 if (!iwl_grab_nic_access(priv)) {
5548 iwl_write_prph(priv, APMG_CLK_DIS_REG,
Zhu Yib481de92007-09-25 17:54:57 -07005549 APMG_CLK_VAL_DMA_CLK_RQT);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07005550 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005551 }
5552 spin_unlock_irqrestore(&priv->lock, flags);
5553
5554 udelay(5);
5555
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005556 iwl4965_hw_nic_stop_master(priv);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07005557 iwl_set_bit(priv, CSR_RESET, CSR_RESET_REG_FLAG_SW_RESET);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005558 iwl4965_hw_nic_reset(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005559
5560 exit:
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005561 memset(&priv->card_alive, 0, sizeof(struct iwl4965_alive_resp));
Zhu Yib481de92007-09-25 17:54:57 -07005562
5563 if (priv->ibss_beacon)
5564 dev_kfree_skb(priv->ibss_beacon);
5565 priv->ibss_beacon = NULL;
5566
5567 /* clear out any free frames */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005568 iwl4965_clear_free_frames(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005569}
5570
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005571static void iwl4965_down(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005572{
5573 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005574 __iwl4965_down(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005575 mutex_unlock(&priv->mutex);
Zhu Yib24d22b2007-12-19 13:59:52 +08005576
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005577 iwl4965_cancel_deferred_work(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005578}
5579
5580#define MAX_HW_RESTARTS 5
5581
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005582static int __iwl4965_up(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005583{
Tomas Winkler57aab752008-04-14 21:16:03 -07005584 int i;
5585 int ret;
Zhu Yib481de92007-09-25 17:54:57 -07005586
5587 if (test_bit(STATUS_EXIT_PENDING, &priv->status)) {
5588 IWL_WARNING("Exit pending; will not bring the NIC up\n");
5589 return -EIO;
5590 }
5591
5592 if (test_bit(STATUS_RF_KILL_SW, &priv->status)) {
5593 IWL_WARNING("Radio disabled by SW RF kill (module "
5594 "parameter)\n");
Mohamed Abbasad97edd22008-03-28 16:21:06 -07005595 iwl_rfkill_set_hw_state(priv);
Zhu Yie655b9f2008-01-24 02:19:38 -08005596 return -ENODEV;
5597 }
5598
Reinette Chatree903fbd2008-01-30 22:05:15 -08005599 if (!priv->ucode_data_backup.v_addr || !priv->ucode_data.v_addr) {
5600 IWL_ERROR("ucode not available for device bringup\n");
5601 return -EIO;
5602 }
5603
Zhu Yie655b9f2008-01-24 02:19:38 -08005604 /* If platform's RF_KILL switch is NOT set to KILL */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07005605 if (iwl_read32(priv, CSR_GP_CNTRL) &
Zhu Yie655b9f2008-01-24 02:19:38 -08005606 CSR_GP_CNTRL_REG_FLAG_HW_RF_KILL_SW)
5607 clear_bit(STATUS_RF_KILL_HW, &priv->status);
5608 else {
5609 set_bit(STATUS_RF_KILL_HW, &priv->status);
5610 if (!test_bit(STATUS_IN_SUSPEND, &priv->status)) {
Mohamed Abbasad97edd22008-03-28 16:21:06 -07005611 iwl_rfkill_set_hw_state(priv);
Zhu Yie655b9f2008-01-24 02:19:38 -08005612 IWL_WARNING("Radio disabled by HW RF Kill switch\n");
5613 return -ENODEV;
5614 }
Zhu Yib481de92007-09-25 17:54:57 -07005615 }
5616
Mohamed Abbasad97edd22008-03-28 16:21:06 -07005617 iwl_rfkill_set_hw_state(priv);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07005618 iwl_write32(priv, CSR_INT, 0xFFFFFFFF);
Zhu Yib481de92007-09-25 17:54:57 -07005619
Tomas Winkler57aab752008-04-14 21:16:03 -07005620 ret = priv->cfg->ops->lib->hw_nic_init(priv);
5621 if (ret) {
5622 IWL_ERROR("Unable to init nic\n");
5623 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07005624 }
5625
5626 /* make sure rfkill handshake bits are cleared */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07005627 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
5628 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR,
Zhu Yib481de92007-09-25 17:54:57 -07005629 CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
5630
5631 /* clear (again), then enable host interrupts */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07005632 iwl_write32(priv, CSR_INT, 0xFFFFFFFF);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005633 iwl4965_enable_interrupts(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005634
5635 /* really make sure rfkill handshake bits are cleared */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07005636 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
5637 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
Zhu Yib481de92007-09-25 17:54:57 -07005638
5639 /* Copy original ucode data image from disk into backup cache.
5640 * This will be used to initialize the on-board processor's
5641 * data SRAM for a clean start when the runtime program first loads. */
5642 memcpy(priv->ucode_data_backup.v_addr, priv->ucode_data.v_addr,
Zhu Yi5a66926a2008-01-14 17:46:18 -08005643 priv->ucode_data.len);
Zhu Yib481de92007-09-25 17:54:57 -07005644
Zhu Yie655b9f2008-01-24 02:19:38 -08005645 /* We return success when we resume from suspend and rf_kill is on. */
5646 if (test_bit(STATUS_RF_KILL_HW, &priv->status))
Zhu Yib481de92007-09-25 17:54:57 -07005647 return 0;
Zhu Yib481de92007-09-25 17:54:57 -07005648
5649 for (i = 0; i < MAX_HW_RESTARTS; i++) {
5650
Assaf Kraussbf85ea42008-03-14 10:38:49 -07005651 iwlcore_clear_stations_table(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005652
5653 /* load bootstrap state machine,
5654 * load bootstrap program into processor's memory,
5655 * prepare to load the "initialize" uCode */
Tomas Winkler57aab752008-04-14 21:16:03 -07005656 ret = priv->cfg->ops->lib->load_ucode(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005657
Tomas Winkler57aab752008-04-14 21:16:03 -07005658 if (ret) {
5659 IWL_ERROR("Unable to set up bootstrap uCode: %d\n", ret);
Zhu Yib481de92007-09-25 17:54:57 -07005660 continue;
5661 }
5662
5663 /* start card; "initialize" will load runtime ucode */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005664 iwl4965_nic_start(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005665
Zhu Yib481de92007-09-25 17:54:57 -07005666 IWL_DEBUG_INFO(DRV_NAME " is coming up\n");
5667
5668 return 0;
5669 }
5670
5671 set_bit(STATUS_EXIT_PENDING, &priv->status);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005672 __iwl4965_down(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005673
5674 /* tried to restart and config the device for as long as our
5675 * patience could withstand */
5676 IWL_ERROR("Unable to initialize device after %d attempts.\n", i);
5677 return -EIO;
5678}
5679
5680
5681/*****************************************************************************
5682 *
5683 * Workqueue callbacks
5684 *
5685 *****************************************************************************/
5686
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005687static void iwl4965_bg_init_alive_start(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07005688{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005689 struct iwl_priv *priv =
5690 container_of(data, struct iwl_priv, init_alive_start.work);
Zhu Yib481de92007-09-25 17:54:57 -07005691
5692 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
5693 return;
5694
5695 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005696 iwl4965_init_alive_start(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005697 mutex_unlock(&priv->mutex);
5698}
5699
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005700static void iwl4965_bg_alive_start(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07005701{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005702 struct iwl_priv *priv =
5703 container_of(data, struct iwl_priv, alive_start.work);
Zhu Yib481de92007-09-25 17:54:57 -07005704
5705 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
5706 return;
5707
5708 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005709 iwl4965_alive_start(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005710 mutex_unlock(&priv->mutex);
5711}
5712
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005713static void iwl4965_bg_rf_kill(struct work_struct *work)
Zhu Yib481de92007-09-25 17:54:57 -07005714{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005715 struct iwl_priv *priv = container_of(work, struct iwl_priv, rf_kill);
Zhu Yib481de92007-09-25 17:54:57 -07005716
5717 wake_up_interruptible(&priv->wait_command_queue);
5718
5719 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
5720 return;
5721
5722 mutex_lock(&priv->mutex);
5723
Tomas Winklerfee12472008-04-03 16:05:21 -07005724 if (!iwl_is_rfkill(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07005725 IWL_DEBUG(IWL_DL_INFO | IWL_DL_RF_KILL,
5726 "HW and/or SW RF Kill no longer active, restarting "
5727 "device\n");
5728 if (!test_bit(STATUS_EXIT_PENDING, &priv->status))
5729 queue_work(priv->workqueue, &priv->restart);
5730 } else {
Mohamed Abbasad97edd22008-03-28 16:21:06 -07005731 /* make sure mac80211 stop sending Tx frame */
5732 if (priv->mac80211_registered)
5733 ieee80211_stop_queues(priv->hw);
Zhu Yib481de92007-09-25 17:54:57 -07005734
5735 if (!test_bit(STATUS_RF_KILL_HW, &priv->status))
5736 IWL_DEBUG_RF_KILL("Can not turn radio back on - "
5737 "disabled by SW switch\n");
5738 else
5739 IWL_WARNING("Radio Frequency Kill Switch is On:\n"
5740 "Kill switch must be turned off for "
5741 "wireless networking to work.\n");
5742 }
Mohamed Abbasad97edd22008-03-28 16:21:06 -07005743 iwl_rfkill_set_hw_state(priv);
5744
Zhu Yib481de92007-09-25 17:54:57 -07005745 mutex_unlock(&priv->mutex);
5746}
5747
5748#define IWL_SCAN_CHECK_WATCHDOG (7 * HZ)
5749
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005750static void iwl4965_bg_scan_check(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07005751{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005752 struct iwl_priv *priv =
5753 container_of(data, struct iwl_priv, scan_check.work);
Zhu Yib481de92007-09-25 17:54:57 -07005754
5755 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
5756 return;
5757
5758 mutex_lock(&priv->mutex);
5759 if (test_bit(STATUS_SCANNING, &priv->status) ||
5760 test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
5761 IWL_DEBUG(IWL_DL_INFO | IWL_DL_SCAN,
5762 "Scan completion watchdog resetting adapter (%dms)\n",
5763 jiffies_to_msecs(IWL_SCAN_CHECK_WATCHDOG));
mabbas052c4b92007-10-25 17:15:43 +08005764
Zhu Yib481de92007-09-25 17:54:57 -07005765 if (!test_bit(STATUS_EXIT_PENDING, &priv->status))
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005766 iwl4965_send_scan_abort(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005767 }
5768 mutex_unlock(&priv->mutex);
5769}
5770
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005771static void iwl4965_bg_request_scan(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07005772{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005773 struct iwl_priv *priv =
5774 container_of(data, struct iwl_priv, request_scan);
Tomas Winkler857485c2008-03-21 13:53:44 -07005775 struct iwl_host_cmd cmd = {
Zhu Yib481de92007-09-25 17:54:57 -07005776 .id = REPLY_SCAN_CMD,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005777 .len = sizeof(struct iwl4965_scan_cmd),
Zhu Yib481de92007-09-25 17:54:57 -07005778 .meta.flags = CMD_SIZE_HUGE,
5779 };
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005780 struct iwl4965_scan_cmd *scan;
Zhu Yib481de92007-09-25 17:54:57 -07005781 struct ieee80211_conf *conf = NULL;
Tomas Winkler78330fd2008-02-06 02:37:18 +02005782 u16 cmd_len;
Johannes Berg8318d782008-01-24 19:38:38 +01005783 enum ieee80211_band band;
Tomas Winkler78330fd2008-02-06 02:37:18 +02005784 u8 direct_mask;
Tomas Winkler857485c2008-03-21 13:53:44 -07005785 int ret = 0;
Zhu Yib481de92007-09-25 17:54:57 -07005786
5787 conf = ieee80211_get_hw_conf(priv->hw);
5788
5789 mutex_lock(&priv->mutex);
5790
Tomas Winklerfee12472008-04-03 16:05:21 -07005791 if (!iwl_is_ready(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07005792 IWL_WARNING("request scan called when driver not ready.\n");
5793 goto done;
5794 }
5795
5796 /* Make sure the scan wasn't cancelled before this queued work
5797 * was given the chance to run... */
5798 if (!test_bit(STATUS_SCANNING, &priv->status))
5799 goto done;
5800
5801 /* This should never be called or scheduled if there is currently
5802 * a scan active in the hardware. */
5803 if (test_bit(STATUS_SCAN_HW, &priv->status)) {
5804 IWL_DEBUG_INFO("Multiple concurrent scan requests in parallel. "
5805 "Ignoring second request.\n");
Tomas Winkler857485c2008-03-21 13:53:44 -07005806 ret = -EIO;
Zhu Yib481de92007-09-25 17:54:57 -07005807 goto done;
5808 }
5809
5810 if (test_bit(STATUS_EXIT_PENDING, &priv->status)) {
5811 IWL_DEBUG_SCAN("Aborting scan due to device shutdown\n");
5812 goto done;
5813 }
5814
5815 if (test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
5816 IWL_DEBUG_HC("Scan request while abort pending. Queuing.\n");
5817 goto done;
5818 }
5819
Tomas Winklerfee12472008-04-03 16:05:21 -07005820 if (iwl_is_rfkill(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07005821 IWL_DEBUG_HC("Aborting scan due to RF Kill activation\n");
5822 goto done;
5823 }
5824
5825 if (!test_bit(STATUS_READY, &priv->status)) {
5826 IWL_DEBUG_HC("Scan request while uninitialized. Queuing.\n");
5827 goto done;
5828 }
5829
5830 if (!priv->scan_bands) {
5831 IWL_DEBUG_HC("Aborting scan due to no requested bands\n");
5832 goto done;
5833 }
5834
5835 if (!priv->scan) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005836 priv->scan = kmalloc(sizeof(struct iwl4965_scan_cmd) +
Zhu Yib481de92007-09-25 17:54:57 -07005837 IWL_MAX_SCAN_SIZE, GFP_KERNEL);
5838 if (!priv->scan) {
Tomas Winkler857485c2008-03-21 13:53:44 -07005839 ret = -ENOMEM;
Zhu Yib481de92007-09-25 17:54:57 -07005840 goto done;
5841 }
5842 }
5843 scan = priv->scan;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005844 memset(scan, 0, sizeof(struct iwl4965_scan_cmd) + IWL_MAX_SCAN_SIZE);
Zhu Yib481de92007-09-25 17:54:57 -07005845
5846 scan->quiet_plcp_th = IWL_PLCP_QUIET_THRESH;
5847 scan->quiet_time = IWL_ACTIVE_QUIET_TIME;
5848
Tomas Winkler3109ece2008-03-28 16:33:35 -07005849 if (iwl_is_associated(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07005850 u16 interval = 0;
5851 u32 extra;
5852 u32 suspend_time = 100;
5853 u32 scan_suspend_time = 100;
5854 unsigned long flags;
5855
5856 IWL_DEBUG_INFO("Scanning while associated...\n");
5857
5858 spin_lock_irqsave(&priv->lock, flags);
5859 interval = priv->beacon_int;
5860 spin_unlock_irqrestore(&priv->lock, flags);
5861
5862 scan->suspend_time = 0;
mabbas052c4b92007-10-25 17:15:43 +08005863 scan->max_out_time = cpu_to_le32(200 * 1024);
Zhu Yib481de92007-09-25 17:54:57 -07005864 if (!interval)
5865 interval = suspend_time;
5866
5867 extra = (suspend_time / interval) << 22;
5868 scan_suspend_time = (extra |
5869 ((suspend_time % interval) * 1024));
5870 scan->suspend_time = cpu_to_le32(scan_suspend_time);
5871 IWL_DEBUG_SCAN("suspend_time 0x%X beacon interval %d\n",
5872 scan_suspend_time, interval);
5873 }
5874
5875 /* We should add the ability for user to lock to PASSIVE ONLY */
5876 if (priv->one_direct_scan) {
5877 IWL_DEBUG_SCAN
5878 ("Kicking off one direct scan for '%s'\n",
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005879 iwl4965_escape_essid(priv->direct_ssid,
Zhu Yib481de92007-09-25 17:54:57 -07005880 priv->direct_ssid_len));
5881 scan->direct_scan[0].id = WLAN_EID_SSID;
5882 scan->direct_scan[0].len = priv->direct_ssid_len;
5883 memcpy(scan->direct_scan[0].ssid,
5884 priv->direct_ssid, priv->direct_ssid_len);
5885 direct_mask = 1;
Tomas Winkler3109ece2008-03-28 16:33:35 -07005886 } else if (!iwl_is_associated(priv) && priv->essid_len) {
Zhu Yib481de92007-09-25 17:54:57 -07005887 scan->direct_scan[0].id = WLAN_EID_SSID;
5888 scan->direct_scan[0].len = priv->essid_len;
5889 memcpy(scan->direct_scan[0].ssid, priv->essid, priv->essid_len);
5890 direct_mask = 1;
Tomas Winkler857485c2008-03-21 13:53:44 -07005891 } else {
Zhu Yib481de92007-09-25 17:54:57 -07005892 direct_mask = 0;
Tomas Winkler857485c2008-03-21 13:53:44 -07005893 }
Zhu Yib481de92007-09-25 17:54:57 -07005894
Zhu Yib481de92007-09-25 17:54:57 -07005895 scan->tx_cmd.tx_flags = TX_CMD_FLG_SEQ_CTL_MSK;
5896 scan->tx_cmd.sta_id = priv->hw_setting.bcast_sta_id;
5897 scan->tx_cmd.stop_time.life_time = TX_CMD_LIFE_TIME_INFINITE;
5898
Zhu Yib481de92007-09-25 17:54:57 -07005899
5900 switch (priv->scan_bands) {
5901 case 2:
5902 scan->flags = RXON_FLG_BAND_24G_MSK | RXON_FLG_AUTO_DETECT_MSK;
5903 scan->tx_cmd.rate_n_flags =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005904 iwl4965_hw_set_rate_n_flags(IWL_RATE_1M_PLCP,
Zhu Yib481de92007-09-25 17:54:57 -07005905 RATE_MCS_ANT_B_MSK|RATE_MCS_CCK_MSK);
5906
5907 scan->good_CRC_th = 0;
Johannes Berg8318d782008-01-24 19:38:38 +01005908 band = IEEE80211_BAND_2GHZ;
Zhu Yib481de92007-09-25 17:54:57 -07005909 break;
5910
5911 case 1:
5912 scan->tx_cmd.rate_n_flags =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005913 iwl4965_hw_set_rate_n_flags(IWL_RATE_6M_PLCP,
Zhu Yib481de92007-09-25 17:54:57 -07005914 RATE_MCS_ANT_B_MSK);
5915 scan->good_CRC_th = IWL_GOOD_CRC_TH;
Johannes Berg8318d782008-01-24 19:38:38 +01005916 band = IEEE80211_BAND_5GHZ;
Zhu Yib481de92007-09-25 17:54:57 -07005917 break;
5918
5919 default:
5920 IWL_WARNING("Invalid scan band count\n");
5921 goto done;
5922 }
5923
Tomas Winkler78330fd2008-02-06 02:37:18 +02005924 /* We don't build a direct scan probe request; the uCode will do
5925 * that based on the direct_mask added to each channel entry */
5926 cmd_len = iwl4965_fill_probe_req(priv, band,
5927 (struct ieee80211_mgmt *)scan->data,
5928 IWL_MAX_SCAN_SIZE - sizeof(*scan), 0);
5929
5930 scan->tx_cmd.len = cpu_to_le16(cmd_len);
Zhu Yib481de92007-09-25 17:54:57 -07005931 /* select Rx chains */
5932
5933 /* Force use of chains B and C (0x6) for scan Rx.
5934 * Avoid A (0x1) because of its off-channel reception on A-band.
5935 * MIMO is not used here, but value is required to make uCode happy. */
5936 scan->rx_chain = RXON_RX_CHAIN_DRIVER_FORCE_MSK |
5937 cpu_to_le16((0x7 << RXON_RX_CHAIN_VALID_POS) |
5938 (0x6 << RXON_RX_CHAIN_FORCE_SEL_POS) |
5939 (0x7 << RXON_RX_CHAIN_FORCE_MIMO_SEL_POS));
5940
5941 if (priv->iw_mode == IEEE80211_IF_TYPE_MNTR)
5942 scan->filter_flags = RXON_FILTER_PROMISC_MSK;
5943
Reinette Chatre26c0f032008-03-11 16:17:15 -07005944 if (direct_mask) {
Zhu Yib481de92007-09-25 17:54:57 -07005945 IWL_DEBUG_SCAN
5946 ("Initiating direct scan for %s.\n",
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005947 iwl4965_escape_essid(priv->essid, priv->essid_len));
Reinette Chatre26c0f032008-03-11 16:17:15 -07005948 scan->channel_count =
5949 iwl4965_get_channels_for_scan(
5950 priv, band, 1, /* active */
5951 direct_mask,
5952 (void *)&scan->data[le16_to_cpu(scan->tx_cmd.len)]);
5953 } else {
Zhu Yib481de92007-09-25 17:54:57 -07005954 IWL_DEBUG_SCAN("Initiating indirect scan.\n");
Reinette Chatre26c0f032008-03-11 16:17:15 -07005955 scan->channel_count =
5956 iwl4965_get_channels_for_scan(
5957 priv, band, 0, /* passive */
5958 direct_mask,
5959 (void *)&scan->data[le16_to_cpu(scan->tx_cmd.len)]);
5960 }
Zhu Yib481de92007-09-25 17:54:57 -07005961
5962 cmd.len += le16_to_cpu(scan->tx_cmd.len) +
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005963 scan->channel_count * sizeof(struct iwl4965_scan_channel);
Zhu Yib481de92007-09-25 17:54:57 -07005964 cmd.data = scan;
5965 scan->len = cpu_to_le16(cmd.len);
5966
5967 set_bit(STATUS_SCAN_HW, &priv->status);
Tomas Winkler857485c2008-03-21 13:53:44 -07005968 ret = iwl_send_cmd_sync(priv, &cmd);
5969 if (ret)
Zhu Yib481de92007-09-25 17:54:57 -07005970 goto done;
5971
5972 queue_delayed_work(priv->workqueue, &priv->scan_check,
5973 IWL_SCAN_CHECK_WATCHDOG);
5974
5975 mutex_unlock(&priv->mutex);
5976 return;
5977
5978 done:
Ian Schram01ebd062007-10-25 17:15:22 +08005979 /* inform mac80211 scan aborted */
Zhu Yib481de92007-09-25 17:54:57 -07005980 queue_work(priv->workqueue, &priv->scan_completed);
5981 mutex_unlock(&priv->mutex);
5982}
5983
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005984static void iwl4965_bg_up(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07005985{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005986 struct iwl_priv *priv = container_of(data, struct iwl_priv, up);
Zhu Yib481de92007-09-25 17:54:57 -07005987
5988 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
5989 return;
5990
5991 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005992 __iwl4965_up(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005993 mutex_unlock(&priv->mutex);
5994}
5995
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005996static void iwl4965_bg_restart(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07005997{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005998 struct iwl_priv *priv = container_of(data, struct iwl_priv, restart);
Zhu Yib481de92007-09-25 17:54:57 -07005999
6000 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
6001 return;
6002
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006003 iwl4965_down(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006004 queue_work(priv->workqueue, &priv->up);
6005}
6006
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006007static void iwl4965_bg_rx_replenish(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07006008{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006009 struct iwl_priv *priv =
6010 container_of(data, struct iwl_priv, rx_replenish);
Zhu Yib481de92007-09-25 17:54:57 -07006011
6012 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
6013 return;
6014
6015 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006016 iwl4965_rx_replenish(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006017 mutex_unlock(&priv->mutex);
6018}
6019
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08006020#define IWL_DELAY_NEXT_SCAN (HZ*2)
6021
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006022static void iwl4965_bg_post_associate(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07006023{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006024 struct iwl_priv *priv = container_of(data, struct iwl_priv,
Zhu Yib481de92007-09-25 17:54:57 -07006025 post_associate.work);
Zhu Yib481de92007-09-25 17:54:57 -07006026 struct ieee80211_conf *conf = NULL;
Tomas Winkler857485c2008-03-21 13:53:44 -07006027 int ret = 0;
Joe Perches0795af52007-10-03 17:59:30 -07006028 DECLARE_MAC_BUF(mac);
Zhu Yib481de92007-09-25 17:54:57 -07006029
6030 if (priv->iw_mode == IEEE80211_IF_TYPE_AP) {
6031 IWL_ERROR("%s Should not be called in AP mode\n", __FUNCTION__);
6032 return;
6033 }
6034
Joe Perches0795af52007-10-03 17:59:30 -07006035 IWL_DEBUG_ASSOC("Associated as %d to: %s\n",
6036 priv->assoc_id,
6037 print_mac(mac, priv->active_rxon.bssid_addr));
Zhu Yib481de92007-09-25 17:54:57 -07006038
6039
6040 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
6041 return;
6042
6043 mutex_lock(&priv->mutex);
6044
Johannes Berg32bfd352007-12-19 01:31:26 +01006045 if (!priv->vif || !priv->is_open) {
Mohamed Abbas948c1712007-10-25 17:15:45 +08006046 mutex_unlock(&priv->mutex);
6047 return;
6048 }
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006049 iwl4965_scan_cancel_timeout(priv, 200);
mabbas052c4b92007-10-25 17:15:43 +08006050
Zhu Yib481de92007-09-25 17:54:57 -07006051 conf = ieee80211_get_hw_conf(priv->hw);
6052
6053 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006054 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006055
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006056 memset(&priv->rxon_timing, 0, sizeof(struct iwl4965_rxon_time_cmd));
6057 iwl4965_setup_rxon_timing(priv);
Tomas Winkler857485c2008-03-21 13:53:44 -07006058 ret = iwl_send_cmd_pdu(priv, REPLY_RXON_TIMING,
Zhu Yib481de92007-09-25 17:54:57 -07006059 sizeof(priv->rxon_timing), &priv->rxon_timing);
Tomas Winkler857485c2008-03-21 13:53:44 -07006060 if (ret)
Zhu Yib481de92007-09-25 17:54:57 -07006061 IWL_WARNING("REPLY_RXON_TIMING failed - "
6062 "Attempting to continue.\n");
6063
6064 priv->staging_rxon.filter_flags |= RXON_FILTER_ASSOC_MSK;
6065
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08006066#ifdef CONFIG_IWL4965_HT
Ron Rindjunskyfd105e72007-11-26 16:14:39 +02006067 if (priv->current_ht_config.is_ht)
6068 iwl4965_set_rxon_ht(priv, &priv->current_ht_config);
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08006069#endif /* CONFIG_IWL4965_HT*/
Zhu Yib481de92007-09-25 17:54:57 -07006070 iwl4965_set_rxon_chain(priv);
6071 priv->staging_rxon.assoc_id = cpu_to_le16(priv->assoc_id);
6072
6073 IWL_DEBUG_ASSOC("assoc id %d beacon interval %d\n",
6074 priv->assoc_id, priv->beacon_int);
6075
6076 if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_PREAMBLE)
6077 priv->staging_rxon.flags |= RXON_FLG_SHORT_PREAMBLE_MSK;
6078 else
6079 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_PREAMBLE_MSK;
6080
6081 if (priv->staging_rxon.flags & RXON_FLG_BAND_24G_MSK) {
6082 if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_SLOT_TIME)
6083 priv->staging_rxon.flags |= RXON_FLG_SHORT_SLOT_MSK;
6084 else
6085 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
6086
6087 if (priv->iw_mode == IEEE80211_IF_TYPE_IBSS)
6088 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
6089
6090 }
6091
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006092 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006093
6094 switch (priv->iw_mode) {
6095 case IEEE80211_IF_TYPE_STA:
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006096 iwl4965_rate_scale_init(priv->hw, IWL_AP_ID);
Zhu Yib481de92007-09-25 17:54:57 -07006097 break;
6098
6099 case IEEE80211_IF_TYPE_IBSS:
6100
6101 /* clear out the station table */
Assaf Kraussbf85ea42008-03-14 10:38:49 -07006102 iwlcore_clear_stations_table(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006103
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006104 iwl4965_rxon_add_station(priv, iwl4965_broadcast_addr, 0);
6105 iwl4965_rxon_add_station(priv, priv->bssid, 0);
6106 iwl4965_rate_scale_init(priv->hw, IWL_STA_ID);
6107 iwl4965_send_beacon_cmd(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006108
6109 break;
6110
6111 default:
6112 IWL_ERROR("%s Should not be called in %d mode\n",
6113 __FUNCTION__, priv->iw_mode);
6114 break;
6115 }
6116
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006117 iwl4965_sequence_reset(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006118
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08006119#ifdef CONFIG_IWL4965_SENSITIVITY
Zhu Yib481de92007-09-25 17:54:57 -07006120 /* Enable Rx differential gain and sensitivity calibrations */
6121 iwl4965_chain_noise_reset(priv);
6122 priv->start_calib = 1;
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08006123#endif /* CONFIG_IWL4965_SENSITIVITY */
Zhu Yib481de92007-09-25 17:54:57 -07006124
6125 if (priv->iw_mode == IEEE80211_IF_TYPE_IBSS)
6126 priv->assoc_station_added = 1;
6127
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006128 iwl4965_activate_qos(priv, 0);
Ron Rindjunsky292ae172008-02-06 11:20:39 -08006129
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08006130 /* we have just associated, don't start scan too early */
6131 priv->next_scan_jiffies = jiffies + IWL_DELAY_NEXT_SCAN;
Zhu Yib481de92007-09-25 17:54:57 -07006132 mutex_unlock(&priv->mutex);
6133}
6134
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006135static void iwl4965_bg_abort_scan(struct work_struct *work)
Zhu Yib481de92007-09-25 17:54:57 -07006136{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006137 struct iwl_priv *priv = container_of(work, struct iwl_priv, abort_scan);
Zhu Yib481de92007-09-25 17:54:57 -07006138
Tomas Winklerfee12472008-04-03 16:05:21 -07006139 if (!iwl_is_ready(priv))
Zhu Yib481de92007-09-25 17:54:57 -07006140 return;
6141
6142 mutex_lock(&priv->mutex);
6143
6144 set_bit(STATUS_SCAN_ABORTING, &priv->status);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006145 iwl4965_send_scan_abort(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006146
6147 mutex_unlock(&priv->mutex);
6148}
6149
Zhu Yi76bb77e2007-11-22 10:53:22 +08006150static int iwl4965_mac_config(struct ieee80211_hw *hw, struct ieee80211_conf *conf);
6151
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006152static void iwl4965_bg_scan_completed(struct work_struct *work)
Zhu Yib481de92007-09-25 17:54:57 -07006153{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006154 struct iwl_priv *priv =
6155 container_of(work, struct iwl_priv, scan_completed);
Zhu Yib481de92007-09-25 17:54:57 -07006156
6157 IWL_DEBUG(IWL_DL_INFO | IWL_DL_SCAN, "SCAN complete scan\n");
6158
6159 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
6160 return;
6161
Zhu Yia0646472007-12-20 14:10:01 +08006162 if (test_bit(STATUS_CONF_PENDING, &priv->status))
6163 iwl4965_mac_config(priv->hw, ieee80211_get_hw_conf(priv->hw));
Zhu Yi76bb77e2007-11-22 10:53:22 +08006164
Zhu Yib481de92007-09-25 17:54:57 -07006165 ieee80211_scan_completed(priv->hw);
6166
6167 /* Since setting the TXPOWER may have been deferred while
6168 * performing the scan, fire one off */
6169 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006170 iwl4965_hw_reg_send_txpower(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006171 mutex_unlock(&priv->mutex);
6172}
6173
6174/*****************************************************************************
6175 *
6176 * mac80211 entry point functions
6177 *
6178 *****************************************************************************/
6179
Zhu Yi5a66926a2008-01-14 17:46:18 -08006180#define UCODE_READY_TIMEOUT (2 * HZ)
6181
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006182static int iwl4965_mac_start(struct ieee80211_hw *hw)
Zhu Yib481de92007-09-25 17:54:57 -07006183{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006184 struct iwl_priv *priv = hw->priv;
Zhu Yi5a66926a2008-01-14 17:46:18 -08006185 int ret;
Zhu Yib481de92007-09-25 17:54:57 -07006186
6187 IWL_DEBUG_MAC80211("enter\n");
6188
Zhu Yi5a66926a2008-01-14 17:46:18 -08006189 if (pci_enable_device(priv->pci_dev)) {
6190 IWL_ERROR("Fail to pci_enable_device\n");
6191 return -ENODEV;
6192 }
6193 pci_restore_state(priv->pci_dev);
6194 pci_enable_msi(priv->pci_dev);
6195
6196 ret = request_irq(priv->pci_dev->irq, iwl4965_isr, IRQF_SHARED,
6197 DRV_NAME, priv);
6198 if (ret) {
6199 IWL_ERROR("Error allocating IRQ %d\n", priv->pci_dev->irq);
6200 goto out_disable_msi;
6201 }
6202
Zhu Yib481de92007-09-25 17:54:57 -07006203 /* we should be verifying the device is ready to be opened */
6204 mutex_lock(&priv->mutex);
6205
Zhu Yi5a66926a2008-01-14 17:46:18 -08006206 memset(&priv->staging_rxon, 0, sizeof(struct iwl4965_rxon_cmd));
6207 /* fetch ucode file from disk, alloc and copy to bus-master buffers ...
6208 * ucode filename and max sizes are card-specific. */
6209
6210 if (!priv->ucode_code.len) {
6211 ret = iwl4965_read_ucode(priv);
6212 if (ret) {
6213 IWL_ERROR("Could not read microcode: %d\n", ret);
6214 mutex_unlock(&priv->mutex);
6215 goto out_release_irq;
6216 }
6217 }
6218
Zhu Yie655b9f2008-01-24 02:19:38 -08006219 ret = __iwl4965_up(priv);
Zhu Yi5a66926a2008-01-14 17:46:18 -08006220
Zhu Yib481de92007-09-25 17:54:57 -07006221 mutex_unlock(&priv->mutex);
Zhu Yi5a66926a2008-01-14 17:46:18 -08006222
Zhu Yie655b9f2008-01-24 02:19:38 -08006223 if (ret)
6224 goto out_release_irq;
6225
6226 IWL_DEBUG_INFO("Start UP work done.\n");
6227
6228 if (test_bit(STATUS_IN_SUSPEND, &priv->status))
6229 return 0;
6230
Zhu Yi5a66926a2008-01-14 17:46:18 -08006231 /* Wait for START_ALIVE from ucode. Otherwise callbacks from
6232 * mac80211 will not be run successfully. */
6233 ret = wait_event_interruptible_timeout(priv->wait_command_queue,
6234 test_bit(STATUS_READY, &priv->status),
6235 UCODE_READY_TIMEOUT);
6236 if (!ret) {
6237 if (!test_bit(STATUS_READY, &priv->status)) {
6238 IWL_ERROR("Wait for START_ALIVE timeout after %dms.\n",
6239 jiffies_to_msecs(UCODE_READY_TIMEOUT));
6240 ret = -ETIMEDOUT;
6241 goto out_release_irq;
6242 }
6243 }
6244
Zhu Yie655b9f2008-01-24 02:19:38 -08006245 priv->is_open = 1;
Zhu Yib481de92007-09-25 17:54:57 -07006246 IWL_DEBUG_MAC80211("leave\n");
6247 return 0;
Zhu Yi5a66926a2008-01-14 17:46:18 -08006248
6249out_release_irq:
6250 free_irq(priv->pci_dev->irq, priv);
6251out_disable_msi:
6252 pci_disable_msi(priv->pci_dev);
Zhu Yie655b9f2008-01-24 02:19:38 -08006253 pci_disable_device(priv->pci_dev);
6254 priv->is_open = 0;
6255 IWL_DEBUG_MAC80211("leave - failed\n");
Zhu Yi5a66926a2008-01-14 17:46:18 -08006256 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07006257}
6258
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006259static void iwl4965_mac_stop(struct ieee80211_hw *hw)
Zhu Yib481de92007-09-25 17:54:57 -07006260{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006261 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07006262
6263 IWL_DEBUG_MAC80211("enter\n");
Mohamed Abbas948c1712007-10-25 17:15:45 +08006264
Zhu Yie655b9f2008-01-24 02:19:38 -08006265 if (!priv->is_open) {
6266 IWL_DEBUG_MAC80211("leave - skip\n");
6267 return;
6268 }
6269
Zhu Yib481de92007-09-25 17:54:57 -07006270 priv->is_open = 0;
Zhu Yi5a66926a2008-01-14 17:46:18 -08006271
Tomas Winklerfee12472008-04-03 16:05:21 -07006272 if (iwl_is_ready_rf(priv)) {
Zhu Yie655b9f2008-01-24 02:19:38 -08006273 /* stop mac, cancel any scan request and clear
6274 * RXON_FILTER_ASSOC_MSK BIT
6275 */
Zhu Yi5a66926a2008-01-14 17:46:18 -08006276 mutex_lock(&priv->mutex);
6277 iwl4965_scan_cancel_timeout(priv, 100);
6278 cancel_delayed_work(&priv->post_associate);
Mohamed Abbasfde35712007-11-29 11:10:15 +08006279 mutex_unlock(&priv->mutex);
Mohamed Abbasfde35712007-11-29 11:10:15 +08006280 }
6281
Zhu Yi5a66926a2008-01-14 17:46:18 -08006282 iwl4965_down(priv);
6283
6284 flush_workqueue(priv->workqueue);
6285 free_irq(priv->pci_dev->irq, priv);
6286 pci_disable_msi(priv->pci_dev);
6287 pci_save_state(priv->pci_dev);
6288 pci_disable_device(priv->pci_dev);
Mohamed Abbas948c1712007-10-25 17:15:45 +08006289
Zhu Yib481de92007-09-25 17:54:57 -07006290 IWL_DEBUG_MAC80211("leave\n");
Zhu Yib481de92007-09-25 17:54:57 -07006291}
6292
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006293static int iwl4965_mac_tx(struct ieee80211_hw *hw, struct sk_buff *skb,
Zhu Yib481de92007-09-25 17:54:57 -07006294 struct ieee80211_tx_control *ctl)
6295{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006296 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07006297
6298 IWL_DEBUG_MAC80211("enter\n");
6299
6300 if (priv->iw_mode == IEEE80211_IF_TYPE_MNTR) {
6301 IWL_DEBUG_MAC80211("leave - monitor\n");
6302 return -1;
6303 }
6304
6305 IWL_DEBUG_TX("dev->xmit(%d bytes) at rate 0x%02x\n", skb->len,
Johannes Berg8318d782008-01-24 19:38:38 +01006306 ctl->tx_rate->bitrate);
Zhu Yib481de92007-09-25 17:54:57 -07006307
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006308 if (iwl4965_tx_skb(priv, skb, ctl))
Zhu Yib481de92007-09-25 17:54:57 -07006309 dev_kfree_skb_any(skb);
6310
6311 IWL_DEBUG_MAC80211("leave\n");
6312 return 0;
6313}
6314
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006315static int iwl4965_mac_add_interface(struct ieee80211_hw *hw,
Zhu Yib481de92007-09-25 17:54:57 -07006316 struct ieee80211_if_init_conf *conf)
6317{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006318 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07006319 unsigned long flags;
Joe Perches0795af52007-10-03 17:59:30 -07006320 DECLARE_MAC_BUF(mac);
Zhu Yib481de92007-09-25 17:54:57 -07006321
Johannes Berg32bfd352007-12-19 01:31:26 +01006322 IWL_DEBUG_MAC80211("enter: type %d\n", conf->type);
Zhu Yib481de92007-09-25 17:54:57 -07006323
Johannes Berg32bfd352007-12-19 01:31:26 +01006324 if (priv->vif) {
6325 IWL_DEBUG_MAC80211("leave - vif != NULL\n");
Reinette Chatre75849d22008-01-23 10:15:17 -08006326 return -EOPNOTSUPP;
Zhu Yib481de92007-09-25 17:54:57 -07006327 }
6328
6329 spin_lock_irqsave(&priv->lock, flags);
Johannes Berg32bfd352007-12-19 01:31:26 +01006330 priv->vif = conf->vif;
Zhu Yib481de92007-09-25 17:54:57 -07006331
6332 spin_unlock_irqrestore(&priv->lock, flags);
6333
6334 mutex_lock(&priv->mutex);
Tomas Winkler864792e2007-11-27 21:00:52 +02006335
6336 if (conf->mac_addr) {
6337 IWL_DEBUG_MAC80211("Set %s\n", print_mac(mac, conf->mac_addr));
6338 memcpy(priv->mac_addr, conf->mac_addr, ETH_ALEN);
6339 }
Zhu Yib481de92007-09-25 17:54:57 -07006340
Tomas Winklerfee12472008-04-03 16:05:21 -07006341 if (iwl_is_ready(priv))
Zhu Yi5a66926a2008-01-14 17:46:18 -08006342 iwl4965_set_mode(priv, conf->type);
6343
Zhu Yib481de92007-09-25 17:54:57 -07006344 mutex_unlock(&priv->mutex);
6345
Zhu Yi5a66926a2008-01-14 17:46:18 -08006346 IWL_DEBUG_MAC80211("leave\n");
Zhu Yib481de92007-09-25 17:54:57 -07006347 return 0;
6348}
6349
6350/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006351 * iwl4965_mac_config - mac80211 config callback
Zhu Yib481de92007-09-25 17:54:57 -07006352 *
6353 * We ignore conf->flags & IEEE80211_CONF_SHORT_SLOT_TIME since it seems to
6354 * be set inappropriately and the driver currently sets the hardware up to
6355 * use it whenever needed.
6356 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006357static int iwl4965_mac_config(struct ieee80211_hw *hw, struct ieee80211_conf *conf)
Zhu Yib481de92007-09-25 17:54:57 -07006358{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006359 struct iwl_priv *priv = hw->priv;
Assaf Kraussbf85ea42008-03-14 10:38:49 -07006360 const struct iwl_channel_info *ch_info;
Zhu Yib481de92007-09-25 17:54:57 -07006361 unsigned long flags;
Zhu Yi76bb77e2007-11-22 10:53:22 +08006362 int ret = 0;
Zhu Yib481de92007-09-25 17:54:57 -07006363
6364 mutex_lock(&priv->mutex);
Johannes Berg8318d782008-01-24 19:38:38 +01006365 IWL_DEBUG_MAC80211("enter to channel %d\n", conf->channel->hw_value);
Zhu Yib481de92007-09-25 17:54:57 -07006366
Zhu Yi12342c42007-12-20 11:27:32 +08006367 priv->add_radiotap = !!(conf->flags & IEEE80211_CONF_RADIOTAP);
6368
Tomas Winklerfee12472008-04-03 16:05:21 -07006369 if (!iwl_is_ready(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07006370 IWL_DEBUG_MAC80211("leave - not ready\n");
Zhu Yi76bb77e2007-11-22 10:53:22 +08006371 ret = -EIO;
6372 goto out;
Zhu Yib481de92007-09-25 17:54:57 -07006373 }
6374
Assaf Krauss1ea87392008-03-18 14:57:50 -07006375 if (unlikely(!priv->cfg->mod_params->disable_hw_scan &&
Zhu Yib481de92007-09-25 17:54:57 -07006376 test_bit(STATUS_SCANNING, &priv->status))) {
Zhu Yia0646472007-12-20 14:10:01 +08006377 IWL_DEBUG_MAC80211("leave - scanning\n");
6378 set_bit(STATUS_CONF_PENDING, &priv->status);
Zhu Yib481de92007-09-25 17:54:57 -07006379 mutex_unlock(&priv->mutex);
Zhu Yia0646472007-12-20 14:10:01 +08006380 return 0;
Zhu Yib481de92007-09-25 17:54:57 -07006381 }
6382
6383 spin_lock_irqsave(&priv->lock, flags);
6384
Assaf Krauss8622e702008-03-21 13:53:43 -07006385 ch_info = iwl_get_channel_info(priv, conf->channel->band,
Johannes Berg8318d782008-01-24 19:38:38 +01006386 ieee80211_frequency_to_channel(conf->channel->center_freq));
Zhu Yib481de92007-09-25 17:54:57 -07006387 if (!is_channel_valid(ch_info)) {
Zhu Yib481de92007-09-25 17:54:57 -07006388 IWL_DEBUG_MAC80211("leave - invalid channel\n");
6389 spin_unlock_irqrestore(&priv->lock, flags);
Zhu Yi76bb77e2007-11-22 10:53:22 +08006390 ret = -EINVAL;
6391 goto out;
Zhu Yib481de92007-09-25 17:54:57 -07006392 }
6393
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08006394#ifdef CONFIG_IWL4965_HT
Tomas Winkler78330fd2008-02-06 02:37:18 +02006395 /* if we are switching from ht to 2.4 clear flags
Zhu Yib481de92007-09-25 17:54:57 -07006396 * from any ht related info since 2.4 does not
6397 * support ht */
Tomas Winkler78330fd2008-02-06 02:37:18 +02006398 if ((le16_to_cpu(priv->staging_rxon.channel) != conf->channel->hw_value)
Zhu Yib481de92007-09-25 17:54:57 -07006399#ifdef IEEE80211_CONF_CHANNEL_SWITCH
6400 && !(conf->flags & IEEE80211_CONF_CHANNEL_SWITCH)
6401#endif
6402 )
6403 priv->staging_rxon.flags = 0;
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08006404#endif /* CONFIG_IWL4965_HT */
Zhu Yib481de92007-09-25 17:54:57 -07006405
Assaf Kraussbf85ea42008-03-14 10:38:49 -07006406 iwlcore_set_rxon_channel(priv, conf->channel->band,
Johannes Berg8318d782008-01-24 19:38:38 +01006407 ieee80211_frequency_to_channel(conf->channel->center_freq));
Zhu Yib481de92007-09-25 17:54:57 -07006408
Johannes Berg8318d782008-01-24 19:38:38 +01006409 iwl4965_set_flags_for_phymode(priv, conf->channel->band);
Zhu Yib481de92007-09-25 17:54:57 -07006410
6411 /* The list of supported rates and rate mask can be different
Johannes Berg8318d782008-01-24 19:38:38 +01006412 * for each band; since the band may have changed, reset
Zhu Yib481de92007-09-25 17:54:57 -07006413 * the rate mask to what mac80211 lists */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006414 iwl4965_set_rate(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006415
6416 spin_unlock_irqrestore(&priv->lock, flags);
6417
6418#ifdef IEEE80211_CONF_CHANNEL_SWITCH
6419 if (conf->flags & IEEE80211_CONF_CHANNEL_SWITCH) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006420 iwl4965_hw_channel_switch(priv, conf->channel);
Zhu Yi76bb77e2007-11-22 10:53:22 +08006421 goto out;
Zhu Yib481de92007-09-25 17:54:57 -07006422 }
6423#endif
6424
Mohamed Abbasad97edd22008-03-28 16:21:06 -07006425 if (priv->cfg->ops->lib->radio_kill_sw)
6426 priv->cfg->ops->lib->radio_kill_sw(priv, !conf->radio_enabled);
Zhu Yib481de92007-09-25 17:54:57 -07006427
6428 if (!conf->radio_enabled) {
6429 IWL_DEBUG_MAC80211("leave - radio disabled\n");
Zhu Yi76bb77e2007-11-22 10:53:22 +08006430 goto out;
Zhu Yib481de92007-09-25 17:54:57 -07006431 }
6432
Tomas Winklerfee12472008-04-03 16:05:21 -07006433 if (iwl_is_rfkill(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07006434 IWL_DEBUG_MAC80211("leave - RF kill\n");
Zhu Yi76bb77e2007-11-22 10:53:22 +08006435 ret = -EIO;
6436 goto out;
Zhu Yib481de92007-09-25 17:54:57 -07006437 }
6438
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006439 iwl4965_set_rate(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006440
6441 if (memcmp(&priv->active_rxon,
6442 &priv->staging_rxon, sizeof(priv->staging_rxon)))
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006443 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006444 else
6445 IWL_DEBUG_INFO("No re-sending same RXON configuration.\n");
6446
6447 IWL_DEBUG_MAC80211("leave\n");
6448
Zhu Yia0646472007-12-20 14:10:01 +08006449out:
6450 clear_bit(STATUS_CONF_PENDING, &priv->status);
Zhu Yi5a66926a2008-01-14 17:46:18 -08006451 mutex_unlock(&priv->mutex);
Zhu Yi76bb77e2007-11-22 10:53:22 +08006452 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07006453}
6454
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006455static void iwl4965_config_ap(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07006456{
Tomas Winkler857485c2008-03-21 13:53:44 -07006457 int ret = 0;
Zhu Yib481de92007-09-25 17:54:57 -07006458
Maarten Lankhorstd986bcd2008-01-23 10:15:16 -08006459 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
Zhu Yib481de92007-09-25 17:54:57 -07006460 return;
6461
6462 /* The following should be done only at AP bring up */
6463 if ((priv->active_rxon.filter_flags & RXON_FILTER_ASSOC_MSK) == 0) {
6464
6465 /* RXON - unassoc (to set timing command) */
6466 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006467 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006468
6469 /* RXON Timing */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006470 memset(&priv->rxon_timing, 0, sizeof(struct iwl4965_rxon_time_cmd));
6471 iwl4965_setup_rxon_timing(priv);
Tomas Winkler857485c2008-03-21 13:53:44 -07006472 ret = iwl_send_cmd_pdu(priv, REPLY_RXON_TIMING,
Zhu Yib481de92007-09-25 17:54:57 -07006473 sizeof(priv->rxon_timing), &priv->rxon_timing);
Tomas Winkler857485c2008-03-21 13:53:44 -07006474 if (ret)
Zhu Yib481de92007-09-25 17:54:57 -07006475 IWL_WARNING("REPLY_RXON_TIMING failed - "
6476 "Attempting to continue.\n");
6477
6478 iwl4965_set_rxon_chain(priv);
6479
6480 /* FIXME: what should be the assoc_id for AP? */
6481 priv->staging_rxon.assoc_id = cpu_to_le16(priv->assoc_id);
6482 if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_PREAMBLE)
6483 priv->staging_rxon.flags |=
6484 RXON_FLG_SHORT_PREAMBLE_MSK;
6485 else
6486 priv->staging_rxon.flags &=
6487 ~RXON_FLG_SHORT_PREAMBLE_MSK;
6488
6489 if (priv->staging_rxon.flags & RXON_FLG_BAND_24G_MSK) {
6490 if (priv->assoc_capability &
6491 WLAN_CAPABILITY_SHORT_SLOT_TIME)
6492 priv->staging_rxon.flags |=
6493 RXON_FLG_SHORT_SLOT_MSK;
6494 else
6495 priv->staging_rxon.flags &=
6496 ~RXON_FLG_SHORT_SLOT_MSK;
6497
6498 if (priv->iw_mode == IEEE80211_IF_TYPE_IBSS)
6499 priv->staging_rxon.flags &=
6500 ~RXON_FLG_SHORT_SLOT_MSK;
6501 }
6502 /* restore RXON assoc */
6503 priv->staging_rxon.filter_flags |= RXON_FILTER_ASSOC_MSK;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006504 iwl4965_commit_rxon(priv);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006505 iwl4965_activate_qos(priv, 1);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006506 iwl4965_rxon_add_station(priv, iwl4965_broadcast_addr, 0);
Zhu Yie1493de2007-09-27 11:27:32 +08006507 }
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006508 iwl4965_send_beacon_cmd(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006509
6510 /* FIXME - we need to add code here to detect a totally new
6511 * configuration, reset the AP, unassoc, rxon timing, assoc,
6512 * clear sta table, add BCAST sta... */
6513}
6514
Johannes Berg32bfd352007-12-19 01:31:26 +01006515static int iwl4965_mac_config_interface(struct ieee80211_hw *hw,
6516 struct ieee80211_vif *vif,
Zhu Yib481de92007-09-25 17:54:57 -07006517 struct ieee80211_if_conf *conf)
6518{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006519 struct iwl_priv *priv = hw->priv;
Joe Perches0795af52007-10-03 17:59:30 -07006520 DECLARE_MAC_BUF(mac);
Zhu Yib481de92007-09-25 17:54:57 -07006521 unsigned long flags;
6522 int rc;
6523
6524 if (conf == NULL)
6525 return -EIO;
6526
Emmanuel Grumbachb716bb92008-03-04 18:09:32 -08006527 if (priv->vif != vif) {
6528 IWL_DEBUG_MAC80211("leave - priv->vif != vif\n");
6529 mutex_unlock(&priv->mutex);
6530 return 0;
6531 }
6532
Zhu Yib481de92007-09-25 17:54:57 -07006533 if ((priv->iw_mode == IEEE80211_IF_TYPE_AP) &&
6534 (!conf->beacon || !conf->ssid_len)) {
6535 IWL_DEBUG_MAC80211
6536 ("Leaving in AP mode because HostAPD is not ready.\n");
6537 return 0;
6538 }
6539
Tomas Winklerfee12472008-04-03 16:05:21 -07006540 if (!iwl_is_alive(priv))
Zhu Yi5a66926a2008-01-14 17:46:18 -08006541 return -EAGAIN;
6542
Zhu Yib481de92007-09-25 17:54:57 -07006543 mutex_lock(&priv->mutex);
6544
Zhu Yib481de92007-09-25 17:54:57 -07006545 if (conf->bssid)
Joe Perches0795af52007-10-03 17:59:30 -07006546 IWL_DEBUG_MAC80211("bssid: %s\n",
6547 print_mac(mac, conf->bssid));
Zhu Yib481de92007-09-25 17:54:57 -07006548
Johannes Berg4150c572007-09-17 01:29:23 -04006549/*
6550 * very dubious code was here; the probe filtering flag is never set:
6551 *
Zhu Yib481de92007-09-25 17:54:57 -07006552 if (unlikely(test_bit(STATUS_SCANNING, &priv->status)) &&
6553 !(priv->hw->flags & IEEE80211_HW_NO_PROBE_FILTERING)) {
Johannes Berg4150c572007-09-17 01:29:23 -04006554 */
Zhu Yib481de92007-09-25 17:54:57 -07006555
6556 if (priv->iw_mode == IEEE80211_IF_TYPE_AP) {
6557 if (!conf->bssid) {
6558 conf->bssid = priv->mac_addr;
6559 memcpy(priv->bssid, priv->mac_addr, ETH_ALEN);
Joe Perches0795af52007-10-03 17:59:30 -07006560 IWL_DEBUG_MAC80211("bssid was set to: %s\n",
6561 print_mac(mac, conf->bssid));
Zhu Yib481de92007-09-25 17:54:57 -07006562 }
6563 if (priv->ibss_beacon)
6564 dev_kfree_skb(priv->ibss_beacon);
6565
6566 priv->ibss_beacon = conf->beacon;
6567 }
6568
Tomas Winklerfee12472008-04-03 16:05:21 -07006569 if (iwl_is_rfkill(priv))
Mohamed Abbasfde35712007-11-29 11:10:15 +08006570 goto done;
6571
Zhu Yib481de92007-09-25 17:54:57 -07006572 if (conf->bssid && !is_zero_ether_addr(conf->bssid) &&
6573 !is_multicast_ether_addr(conf->bssid)) {
6574 /* If there is currently a HW scan going on in the background
6575 * then we need to cancel it else the RXON below will fail. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006576 if (iwl4965_scan_cancel_timeout(priv, 100)) {
Zhu Yib481de92007-09-25 17:54:57 -07006577 IWL_WARNING("Aborted scan still in progress "
6578 "after 100ms\n");
6579 IWL_DEBUG_MAC80211("leaving - scan abort failed.\n");
6580 mutex_unlock(&priv->mutex);
6581 return -EAGAIN;
6582 }
6583 memcpy(priv->staging_rxon.bssid_addr, conf->bssid, ETH_ALEN);
6584
6585 /* TODO: Audit driver for usage of these members and see
6586 * if mac80211 deprecates them (priv->bssid looks like it
6587 * shouldn't be there, but I haven't scanned the IBSS code
6588 * to verify) - jpk */
6589 memcpy(priv->bssid, conf->bssid, ETH_ALEN);
6590
6591 if (priv->iw_mode == IEEE80211_IF_TYPE_AP)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006592 iwl4965_config_ap(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006593 else {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006594 rc = iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006595 if ((priv->iw_mode == IEEE80211_IF_TYPE_STA) && rc)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006596 iwl4965_rxon_add_station(
Zhu Yib481de92007-09-25 17:54:57 -07006597 priv, priv->active_rxon.bssid_addr, 1);
6598 }
6599
6600 } else {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006601 iwl4965_scan_cancel_timeout(priv, 100);
Zhu Yib481de92007-09-25 17:54:57 -07006602 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006603 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006604 }
6605
Mohamed Abbasfde35712007-11-29 11:10:15 +08006606 done:
Zhu Yib481de92007-09-25 17:54:57 -07006607 spin_lock_irqsave(&priv->lock, flags);
6608 if (!conf->ssid_len)
6609 memset(priv->essid, 0, IW_ESSID_MAX_SIZE);
6610 else
6611 memcpy(priv->essid, conf->ssid, conf->ssid_len);
6612
6613 priv->essid_len = conf->ssid_len;
6614 spin_unlock_irqrestore(&priv->lock, flags);
6615
6616 IWL_DEBUG_MAC80211("leave\n");
6617 mutex_unlock(&priv->mutex);
6618
6619 return 0;
6620}
6621
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006622static void iwl4965_configure_filter(struct ieee80211_hw *hw,
Johannes Berg4150c572007-09-17 01:29:23 -04006623 unsigned int changed_flags,
6624 unsigned int *total_flags,
6625 int mc_count, struct dev_addr_list *mc_list)
6626{
6627 /*
6628 * XXX: dummy
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006629 * see also iwl4965_connection_init_rx_config
Johannes Berg4150c572007-09-17 01:29:23 -04006630 */
6631 *total_flags = 0;
6632}
6633
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006634static void iwl4965_mac_remove_interface(struct ieee80211_hw *hw,
Zhu Yib481de92007-09-25 17:54:57 -07006635 struct ieee80211_if_init_conf *conf)
6636{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006637 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07006638
6639 IWL_DEBUG_MAC80211("enter\n");
6640
6641 mutex_lock(&priv->mutex);
Mohamed Abbas948c1712007-10-25 17:15:45 +08006642
Tomas Winklerfee12472008-04-03 16:05:21 -07006643 if (iwl_is_ready_rf(priv)) {
Mohamed Abbasfde35712007-11-29 11:10:15 +08006644 iwl4965_scan_cancel_timeout(priv, 100);
6645 cancel_delayed_work(&priv->post_associate);
6646 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
6647 iwl4965_commit_rxon(priv);
6648 }
Johannes Berg32bfd352007-12-19 01:31:26 +01006649 if (priv->vif == conf->vif) {
6650 priv->vif = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07006651 memset(priv->bssid, 0, ETH_ALEN);
6652 memset(priv->essid, 0, IW_ESSID_MAX_SIZE);
6653 priv->essid_len = 0;
6654 }
6655 mutex_unlock(&priv->mutex);
6656
6657 IWL_DEBUG_MAC80211("leave\n");
6658
6659}
Johannes Berg471b3ef2007-12-28 14:32:58 +01006660
Tomas Winkler98952d52008-03-28 16:33:33 -07006661
6662#ifdef CONFIG_IWL4965_HT
6663static void iwl4965_ht_conf(struct iwl_priv *priv,
6664 struct ieee80211_bss_conf *bss_conf)
6665{
6666 struct ieee80211_ht_info *ht_conf = bss_conf->ht_conf;
6667 struct ieee80211_ht_bss_info *ht_bss_conf = bss_conf->ht_bss_conf;
6668 struct iwl_ht_info *iwl_conf = &priv->current_ht_config;
6669
6670 IWL_DEBUG_MAC80211("enter: \n");
6671
6672 iwl_conf->is_ht = bss_conf->assoc_ht;
6673
6674 if (!iwl_conf->is_ht)
6675 return;
6676
6677 priv->ps_mode = (u8)((ht_conf->cap & IEEE80211_HT_CAP_MIMO_PS) >> 2);
6678
6679 if (ht_conf->cap & IEEE80211_HT_CAP_SGI_20)
6680 iwl_conf->sgf |= 0x1;
6681 if (ht_conf->cap & IEEE80211_HT_CAP_SGI_40)
6682 iwl_conf->sgf |= 0x2;
6683
6684 iwl_conf->is_green_field = !!(ht_conf->cap & IEEE80211_HT_CAP_GRN_FLD);
6685 iwl_conf->max_amsdu_size =
6686 !!(ht_conf->cap & IEEE80211_HT_CAP_MAX_AMSDU);
6687
6688 iwl_conf->supported_chan_width =
6689 !!(ht_conf->cap & IEEE80211_HT_CAP_SUP_WIDTH);
6690 iwl_conf->extension_chan_offset =
6691 ht_bss_conf->bss_cap & IEEE80211_HT_IE_CHA_SEC_OFFSET;
6692 /* If no above or below channel supplied disable FAT channel */
6693 if (iwl_conf->extension_chan_offset != IWL_EXT_CHANNEL_OFFSET_ABOVE &&
6694 iwl_conf->extension_chan_offset != IWL_EXT_CHANNEL_OFFSET_BELOW)
6695 iwl_conf->supported_chan_width = 0;
6696
6697 iwl_conf->tx_mimo_ps_mode =
6698 (u8)((ht_conf->cap & IEEE80211_HT_CAP_MIMO_PS) >> 2);
6699 memcpy(iwl_conf->supp_mcs_set, ht_conf->supp_mcs_set, 16);
6700
6701 iwl_conf->control_channel = ht_bss_conf->primary_channel;
6702 iwl_conf->tx_chan_width =
6703 !!(ht_bss_conf->bss_cap & IEEE80211_HT_IE_CHA_WIDTH);
6704 iwl_conf->ht_protection =
6705 ht_bss_conf->bss_op_mode & IEEE80211_HT_IE_HT_PROTECTION;
6706 iwl_conf->non_GF_STA_present =
6707 !!(ht_bss_conf->bss_op_mode & IEEE80211_HT_IE_NON_GF_STA_PRSNT);
6708
6709 IWL_DEBUG_MAC80211("control channel %d\n", iwl_conf->control_channel);
6710 IWL_DEBUG_MAC80211("leave\n");
6711}
6712#else
6713static inline void iwl4965_ht_conf(struct iwl_priv *priv,
6714 struct ieee80211_bss_conf *bss_conf)
6715{
6716}
6717#endif
6718
Tomas Winkler3109ece2008-03-28 16:33:35 -07006719#define IWL_DELAY_NEXT_SCAN_AFTER_ASSOC (HZ*6)
Johannes Berg471b3ef2007-12-28 14:32:58 +01006720static void iwl4965_bss_info_changed(struct ieee80211_hw *hw,
6721 struct ieee80211_vif *vif,
6722 struct ieee80211_bss_conf *bss_conf,
6723 u32 changes)
Tomas Winkler220173b2007-10-16 00:50:25 +02006724{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006725 struct iwl_priv *priv = hw->priv;
Tomas Winkler220173b2007-10-16 00:50:25 +02006726
Tomas Winkler3109ece2008-03-28 16:33:35 -07006727 IWL_DEBUG_MAC80211("changes = 0x%X\n", changes);
6728
Johannes Berg471b3ef2007-12-28 14:32:58 +01006729 if (changes & BSS_CHANGED_ERP_PREAMBLE) {
Tomas Winkler3109ece2008-03-28 16:33:35 -07006730 IWL_DEBUG_MAC80211("ERP_PREAMBLE %d\n",
6731 bss_conf->use_short_preamble);
Johannes Berg471b3ef2007-12-28 14:32:58 +01006732 if (bss_conf->use_short_preamble)
Tomas Winkler220173b2007-10-16 00:50:25 +02006733 priv->staging_rxon.flags |= RXON_FLG_SHORT_PREAMBLE_MSK;
6734 else
6735 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_PREAMBLE_MSK;
6736 }
6737
Johannes Berg471b3ef2007-12-28 14:32:58 +01006738 if (changes & BSS_CHANGED_ERP_CTS_PROT) {
Tomas Winkler3109ece2008-03-28 16:33:35 -07006739 IWL_DEBUG_MAC80211("ERP_CTS %d\n", bss_conf->use_cts_prot);
Johannes Berg8318d782008-01-24 19:38:38 +01006740 if (bss_conf->use_cts_prot && (priv->band != IEEE80211_BAND_5GHZ))
Tomas Winkler220173b2007-10-16 00:50:25 +02006741 priv->staging_rxon.flags |= RXON_FLG_TGG_PROTECT_MSK;
6742 else
6743 priv->staging_rxon.flags &= ~RXON_FLG_TGG_PROTECT_MSK;
6744 }
6745
Tomas Winkler98952d52008-03-28 16:33:33 -07006746 if (changes & BSS_CHANGED_HT) {
Tomas Winkler3109ece2008-03-28 16:33:35 -07006747 IWL_DEBUG_MAC80211("HT %d\n", bss_conf->assoc_ht);
Tomas Winkler98952d52008-03-28 16:33:33 -07006748 iwl4965_ht_conf(priv, bss_conf);
6749 iwl4965_set_rxon_chain(priv);
6750 }
6751
Johannes Berg471b3ef2007-12-28 14:32:58 +01006752 if (changes & BSS_CHANGED_ASSOC) {
Tomas Winkler3109ece2008-03-28 16:33:35 -07006753 IWL_DEBUG_MAC80211("ASSOC %d\n", bss_conf->assoc);
6754 if (bss_conf->assoc) {
6755 priv->assoc_id = bss_conf->aid;
6756 priv->beacon_int = bss_conf->beacon_int;
6757 priv->timestamp = bss_conf->timestamp;
6758 priv->assoc_capability = bss_conf->assoc_capability;
6759 priv->next_scan_jiffies = jiffies +
6760 IWL_DELAY_NEXT_SCAN_AFTER_ASSOC;
6761 queue_work(priv->workqueue, &priv->post_associate.work);
6762 } else {
6763 priv->assoc_id = 0;
6764 IWL_DEBUG_MAC80211("DISASSOC %d\n", bss_conf->assoc);
6765 }
6766 } else if (changes && iwl_is_associated(priv) && priv->assoc_id) {
6767 IWL_DEBUG_MAC80211("Associated Changes %d\n", changes);
6768 iwl4965_send_rxon_assoc(priv);
Johannes Berg471b3ef2007-12-28 14:32:58 +01006769 }
6770
Tomas Winkler220173b2007-10-16 00:50:25 +02006771}
Zhu Yib481de92007-09-25 17:54:57 -07006772
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006773static int iwl4965_mac_hw_scan(struct ieee80211_hw *hw, u8 *ssid, size_t len)
Zhu Yib481de92007-09-25 17:54:57 -07006774{
6775 int rc = 0;
6776 unsigned long flags;
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006777 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07006778
6779 IWL_DEBUG_MAC80211("enter\n");
6780
mabbas052c4b92007-10-25 17:15:43 +08006781 mutex_lock(&priv->mutex);
Zhu Yib481de92007-09-25 17:54:57 -07006782 spin_lock_irqsave(&priv->lock, flags);
6783
Tomas Winklerfee12472008-04-03 16:05:21 -07006784 if (!iwl_is_ready_rf(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07006785 rc = -EIO;
6786 IWL_DEBUG_MAC80211("leave - not ready or exit pending\n");
6787 goto out_unlock;
6788 }
6789
6790 if (priv->iw_mode == IEEE80211_IF_TYPE_AP) { /* APs don't scan */
6791 rc = -EIO;
6792 IWL_ERROR("ERROR: APs don't scan\n");
6793 goto out_unlock;
6794 }
6795
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08006796 /* we don't schedule scan within next_scan_jiffies period */
6797 if (priv->next_scan_jiffies &&
6798 time_after(priv->next_scan_jiffies, jiffies)) {
6799 rc = -EAGAIN;
6800 goto out_unlock;
6801 }
Zhu Yib481de92007-09-25 17:54:57 -07006802 /* if we just finished scan ask for delay */
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08006803 if (priv->last_scan_jiffies && time_after(priv->last_scan_jiffies +
6804 IWL_DELAY_NEXT_SCAN, jiffies)) {
Zhu Yib481de92007-09-25 17:54:57 -07006805 rc = -EAGAIN;
6806 goto out_unlock;
6807 }
6808 if (len) {
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08006809 IWL_DEBUG_SCAN("direct scan for %s [%d]\n ",
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006810 iwl4965_escape_essid(ssid, len), (int)len);
Zhu Yib481de92007-09-25 17:54:57 -07006811
6812 priv->one_direct_scan = 1;
6813 priv->direct_ssid_len = (u8)
6814 min((u8) len, (u8) IW_ESSID_MAX_SIZE);
6815 memcpy(priv->direct_ssid, ssid, priv->direct_ssid_len);
Mohamed Abbas948c1712007-10-25 17:15:45 +08006816 } else
6817 priv->one_direct_scan = 0;
Zhu Yib481de92007-09-25 17:54:57 -07006818
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006819 rc = iwl4965_scan_initiate(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006820
6821 IWL_DEBUG_MAC80211("leave\n");
6822
6823out_unlock:
6824 spin_unlock_irqrestore(&priv->lock, flags);
mabbas052c4b92007-10-25 17:15:43 +08006825 mutex_unlock(&priv->mutex);
Zhu Yib481de92007-09-25 17:54:57 -07006826
6827 return rc;
6828}
6829
Emmanuel Grumbachab885f82008-03-20 15:06:43 +02006830static void iwl4965_mac_update_tkip_key(struct ieee80211_hw *hw,
6831 struct ieee80211_key_conf *keyconf, const u8 *addr,
6832 u32 iv32, u16 *phase1key)
6833{
6834 struct iwl_priv *priv = hw->priv;
6835 u8 sta_id = IWL_INVALID_STATION;
6836 unsigned long flags;
6837 __le16 key_flags = 0;
6838 int i;
6839 DECLARE_MAC_BUF(mac);
6840
6841 IWL_DEBUG_MAC80211("enter\n");
6842
6843 sta_id = iwl4965_hw_find_station(priv, addr);
6844 if (sta_id == IWL_INVALID_STATION) {
6845 IWL_DEBUG_MAC80211("leave - %s not in station map.\n",
6846 print_mac(mac, addr));
6847 return;
6848 }
6849
6850 iwl4965_scan_cancel_timeout(priv, 100);
6851
6852 key_flags |= (STA_KEY_FLG_TKIP | STA_KEY_FLG_MAP_KEY_MSK);
6853 key_flags |= cpu_to_le16(keyconf->keyidx << STA_KEY_FLG_KEYID_POS);
6854 key_flags &= ~STA_KEY_FLG_INVALID;
6855
6856 if (sta_id == priv->hw_setting.bcast_sta_id)
6857 key_flags |= STA_KEY_MULTICAST_MSK;
6858
6859 spin_lock_irqsave(&priv->sta_lock, flags);
6860
6861 priv->stations[sta_id].sta.key.key_offset =
Emmanuel Grumbach80fb47a2008-04-14 21:16:08 -07006862 iwl_get_free_ucode_key_index(priv);
Emmanuel Grumbachab885f82008-03-20 15:06:43 +02006863 priv->stations[sta_id].sta.key.key_flags = key_flags;
6864 priv->stations[sta_id].sta.key.tkip_rx_tsc_byte2 = (u8) iv32;
6865
6866 for (i = 0; i < 5; i++)
6867 priv->stations[sta_id].sta.key.tkip_rx_ttak[i] =
6868 cpu_to_le16(phase1key[i]);
6869
6870 priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
6871 priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
6872
6873 iwl4965_send_add_station(priv, &priv->stations[sta_id].sta, CMD_ASYNC);
6874
6875 spin_unlock_irqrestore(&priv->sta_lock, flags);
6876
6877 IWL_DEBUG_MAC80211("leave\n");
6878}
6879
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006880static int iwl4965_mac_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
Zhu Yib481de92007-09-25 17:54:57 -07006881 const u8 *local_addr, const u8 *addr,
6882 struct ieee80211_key_conf *key)
6883{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006884 struct iwl_priv *priv = hw->priv;
Joe Perches0795af52007-10-03 17:59:30 -07006885 DECLARE_MAC_BUF(mac);
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07006886 int ret = 0;
6887 u8 sta_id = IWL_INVALID_STATION;
Emmanuel Grumbach6974e362008-04-14 21:16:06 -07006888 u8 is_default_wep_key = 0;
Zhu Yib481de92007-09-25 17:54:57 -07006889
6890 IWL_DEBUG_MAC80211("enter\n");
6891
Assaf Krauss1ea87392008-03-18 14:57:50 -07006892 if (!priv->cfg->mod_params->hw_crypto) {
Zhu Yib481de92007-09-25 17:54:57 -07006893 IWL_DEBUG_MAC80211("leave - hwcrypto disabled\n");
6894 return -EOPNOTSUPP;
6895 }
6896
6897 if (is_zero_ether_addr(addr))
6898 /* only support pairwise keys */
6899 return -EOPNOTSUPP;
6900
Emmanuel Grumbach6974e362008-04-14 21:16:06 -07006901 sta_id = iwl4965_hw_find_station(priv, addr);
6902 if (sta_id == IWL_INVALID_STATION) {
6903 IWL_DEBUG_MAC80211("leave - %s not in station map.\n",
6904 print_mac(mac, addr));
6905 return -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07006906
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07006907 }
Zhu Yib481de92007-09-25 17:54:57 -07006908
Emmanuel Grumbach6974e362008-04-14 21:16:06 -07006909 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006910 iwl4965_scan_cancel_timeout(priv, 100);
Emmanuel Grumbach6974e362008-04-14 21:16:06 -07006911 mutex_unlock(&priv->mutex);
6912
6913 /* If we are getting WEP group key and we didn't receive any key mapping
6914 * so far, we are in legacy wep mode (group key only), otherwise we are
6915 * in 1X mode.
6916 * In legacy wep mode, we use another host command to the uCode */
6917 if (key->alg == ALG_WEP && sta_id == priv->hw_setting.bcast_sta_id &&
6918 priv->iw_mode != IEEE80211_IF_TYPE_AP) {
6919 if (cmd == SET_KEY)
6920 is_default_wep_key = !priv->key_mapping_key;
6921 else
6922 is_default_wep_key = priv->default_wep_key;
6923 }
mabbas052c4b92007-10-25 17:15:43 +08006924
Zhu Yib481de92007-09-25 17:54:57 -07006925 switch (cmd) {
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07006926 case SET_KEY:
Emmanuel Grumbach6974e362008-04-14 21:16:06 -07006927 if (is_default_wep_key)
6928 ret = iwl_set_default_wep_key(priv, key);
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07006929 else
Emmanuel Grumbach74805132008-04-14 21:16:09 -07006930 ret = iwl_set_dynamic_key(priv, key, sta_id);
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07006931
6932 IWL_DEBUG_MAC80211("enable hwcrypto key\n");
Zhu Yib481de92007-09-25 17:54:57 -07006933 break;
6934 case DISABLE_KEY:
Emmanuel Grumbach6974e362008-04-14 21:16:06 -07006935 if (is_default_wep_key)
6936 ret = iwl_remove_default_wep_key(priv, key);
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07006937 else
Emmanuel Grumbach74805132008-04-14 21:16:09 -07006938 ret = iwl_remove_dynamic_key(priv, sta_id);
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07006939
6940 IWL_DEBUG_MAC80211("disable hwcrypto key\n");
Zhu Yib481de92007-09-25 17:54:57 -07006941 break;
6942 default:
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07006943 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07006944 }
6945
6946 IWL_DEBUG_MAC80211("leave\n");
Zhu Yib481de92007-09-25 17:54:57 -07006947
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07006948 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07006949}
6950
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006951static int iwl4965_mac_conf_tx(struct ieee80211_hw *hw, int queue,
Zhu Yib481de92007-09-25 17:54:57 -07006952 const struct ieee80211_tx_queue_params *params)
6953{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006954 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07006955 unsigned long flags;
6956 int q;
Zhu Yib481de92007-09-25 17:54:57 -07006957
6958 IWL_DEBUG_MAC80211("enter\n");
6959
Tomas Winklerfee12472008-04-03 16:05:21 -07006960 if (!iwl_is_ready_rf(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07006961 IWL_DEBUG_MAC80211("leave - RF not ready\n");
6962 return -EIO;
6963 }
6964
6965 if (queue >= AC_NUM) {
6966 IWL_DEBUG_MAC80211("leave - queue >= AC_NUM %d\n", queue);
6967 return 0;
6968 }
6969
Zhu Yib481de92007-09-25 17:54:57 -07006970 if (!priv->qos_data.qos_enable) {
6971 priv->qos_data.qos_active = 0;
6972 IWL_DEBUG_MAC80211("leave - qos not enabled\n");
6973 return 0;
6974 }
6975 q = AC_NUM - 1 - queue;
6976
6977 spin_lock_irqsave(&priv->lock, flags);
6978
6979 priv->qos_data.def_qos_parm.ac[q].cw_min = cpu_to_le16(params->cw_min);
6980 priv->qos_data.def_qos_parm.ac[q].cw_max = cpu_to_le16(params->cw_max);
6981 priv->qos_data.def_qos_parm.ac[q].aifsn = params->aifs;
6982 priv->qos_data.def_qos_parm.ac[q].edca_txop =
Johannes Berg3330d7b2008-02-10 16:49:38 +01006983 cpu_to_le16((params->txop * 32));
Zhu Yib481de92007-09-25 17:54:57 -07006984
6985 priv->qos_data.def_qos_parm.ac[q].reserved1 = 0;
6986 priv->qos_data.qos_active = 1;
6987
6988 spin_unlock_irqrestore(&priv->lock, flags);
6989
6990 mutex_lock(&priv->mutex);
6991 if (priv->iw_mode == IEEE80211_IF_TYPE_AP)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006992 iwl4965_activate_qos(priv, 1);
Tomas Winkler3109ece2008-03-28 16:33:35 -07006993 else if (priv->assoc_id && iwl_is_associated(priv))
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006994 iwl4965_activate_qos(priv, 0);
Zhu Yib481de92007-09-25 17:54:57 -07006995
6996 mutex_unlock(&priv->mutex);
6997
Zhu Yib481de92007-09-25 17:54:57 -07006998 IWL_DEBUG_MAC80211("leave\n");
6999 return 0;
7000}
7001
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007002static int iwl4965_mac_get_tx_stats(struct ieee80211_hw *hw,
Zhu Yib481de92007-09-25 17:54:57 -07007003 struct ieee80211_tx_queue_stats *stats)
7004{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007005 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07007006 int i, avail;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007007 struct iwl4965_tx_queue *txq;
7008 struct iwl4965_queue *q;
Zhu Yib481de92007-09-25 17:54:57 -07007009 unsigned long flags;
7010
7011 IWL_DEBUG_MAC80211("enter\n");
7012
Tomas Winklerfee12472008-04-03 16:05:21 -07007013 if (!iwl_is_ready_rf(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07007014 IWL_DEBUG_MAC80211("leave - RF not ready\n");
7015 return -EIO;
7016 }
7017
7018 spin_lock_irqsave(&priv->lock, flags);
7019
7020 for (i = 0; i < AC_NUM; i++) {
7021 txq = &priv->txq[i];
7022 q = &txq->q;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007023 avail = iwl4965_queue_space(q);
Zhu Yib481de92007-09-25 17:54:57 -07007024
7025 stats->data[i].len = q->n_window - avail;
7026 stats->data[i].limit = q->n_window - q->high_mark;
7027 stats->data[i].count = q->n_window;
7028
7029 }
7030 spin_unlock_irqrestore(&priv->lock, flags);
7031
7032 IWL_DEBUG_MAC80211("leave\n");
7033
7034 return 0;
7035}
7036
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007037static int iwl4965_mac_get_stats(struct ieee80211_hw *hw,
Zhu Yib481de92007-09-25 17:54:57 -07007038 struct ieee80211_low_level_stats *stats)
7039{
7040 IWL_DEBUG_MAC80211("enter\n");
7041 IWL_DEBUG_MAC80211("leave\n");
7042
7043 return 0;
7044}
7045
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007046static u64 iwl4965_mac_get_tsf(struct ieee80211_hw *hw)
Zhu Yib481de92007-09-25 17:54:57 -07007047{
7048 IWL_DEBUG_MAC80211("enter\n");
7049 IWL_DEBUG_MAC80211("leave\n");
7050
7051 return 0;
7052}
7053
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007054static void iwl4965_mac_reset_tsf(struct ieee80211_hw *hw)
Zhu Yib481de92007-09-25 17:54:57 -07007055{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007056 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07007057 unsigned long flags;
7058
7059 mutex_lock(&priv->mutex);
7060 IWL_DEBUG_MAC80211("enter\n");
7061
7062 priv->lq_mngr.lq_ready = 0;
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08007063#ifdef CONFIG_IWL4965_HT
Zhu Yib481de92007-09-25 17:54:57 -07007064 spin_lock_irqsave(&priv->lock, flags);
Ron Rindjunskyfd105e72007-11-26 16:14:39 +02007065 memset(&priv->current_ht_config, 0, sizeof(struct iwl_ht_info));
Zhu Yib481de92007-09-25 17:54:57 -07007066 spin_unlock_irqrestore(&priv->lock, flags);
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08007067#endif /* CONFIG_IWL4965_HT */
Zhu Yib481de92007-09-25 17:54:57 -07007068
Assaf Kraussbf85ea42008-03-14 10:38:49 -07007069 iwlcore_reset_qos(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007070
7071 cancel_delayed_work(&priv->post_associate);
7072
7073 spin_lock_irqsave(&priv->lock, flags);
7074 priv->assoc_id = 0;
7075 priv->assoc_capability = 0;
Zhu Yib481de92007-09-25 17:54:57 -07007076 priv->assoc_station_added = 0;
7077
7078 /* new association get rid of ibss beacon skb */
7079 if (priv->ibss_beacon)
7080 dev_kfree_skb(priv->ibss_beacon);
7081
7082 priv->ibss_beacon = NULL;
7083
7084 priv->beacon_int = priv->hw->conf.beacon_int;
Tomas Winkler3109ece2008-03-28 16:33:35 -07007085 priv->timestamp = 0;
Zhu Yib481de92007-09-25 17:54:57 -07007086 if ((priv->iw_mode == IEEE80211_IF_TYPE_STA))
7087 priv->beacon_int = 0;
7088
7089 spin_unlock_irqrestore(&priv->lock, flags);
7090
Tomas Winklerfee12472008-04-03 16:05:21 -07007091 if (!iwl_is_ready_rf(priv)) {
Mohamed Abbasfde35712007-11-29 11:10:15 +08007092 IWL_DEBUG_MAC80211("leave - not ready\n");
7093 mutex_unlock(&priv->mutex);
7094 return;
7095 }
7096
mabbas052c4b92007-10-25 17:15:43 +08007097 /* we are restarting association process
7098 * clear RXON_FILTER_ASSOC_MSK bit
7099 */
7100 if (priv->iw_mode != IEEE80211_IF_TYPE_AP) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007101 iwl4965_scan_cancel_timeout(priv, 100);
mabbas052c4b92007-10-25 17:15:43 +08007102 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007103 iwl4965_commit_rxon(priv);
mabbas052c4b92007-10-25 17:15:43 +08007104 }
7105
Zhu Yib481de92007-09-25 17:54:57 -07007106 /* Per mac80211.h: This is only used in IBSS mode... */
7107 if (priv->iw_mode != IEEE80211_IF_TYPE_IBSS) {
mabbas052c4b92007-10-25 17:15:43 +08007108
Zhu Yib481de92007-09-25 17:54:57 -07007109 IWL_DEBUG_MAC80211("leave - not in IBSS\n");
7110 mutex_unlock(&priv->mutex);
7111 return;
7112 }
7113
Zhu Yib481de92007-09-25 17:54:57 -07007114 priv->only_active_channel = 0;
7115
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007116 iwl4965_set_rate(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007117
7118 mutex_unlock(&priv->mutex);
7119
7120 IWL_DEBUG_MAC80211("leave\n");
Zhu Yib481de92007-09-25 17:54:57 -07007121}
7122
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007123static int iwl4965_mac_beacon_update(struct ieee80211_hw *hw, struct sk_buff *skb,
Zhu Yib481de92007-09-25 17:54:57 -07007124 struct ieee80211_tx_control *control)
7125{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007126 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07007127 unsigned long flags;
7128
7129 mutex_lock(&priv->mutex);
7130 IWL_DEBUG_MAC80211("enter\n");
7131
Tomas Winklerfee12472008-04-03 16:05:21 -07007132 if (!iwl_is_ready_rf(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07007133 IWL_DEBUG_MAC80211("leave - RF not ready\n");
7134 mutex_unlock(&priv->mutex);
7135 return -EIO;
7136 }
7137
7138 if (priv->iw_mode != IEEE80211_IF_TYPE_IBSS) {
7139 IWL_DEBUG_MAC80211("leave - not IBSS\n");
7140 mutex_unlock(&priv->mutex);
7141 return -EIO;
7142 }
7143
7144 spin_lock_irqsave(&priv->lock, flags);
7145
7146 if (priv->ibss_beacon)
7147 dev_kfree_skb(priv->ibss_beacon);
7148
7149 priv->ibss_beacon = skb;
7150
7151 priv->assoc_id = 0;
7152
7153 IWL_DEBUG_MAC80211("leave\n");
7154 spin_unlock_irqrestore(&priv->lock, flags);
7155
Assaf Kraussbf85ea42008-03-14 10:38:49 -07007156 iwlcore_reset_qos(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007157
7158 queue_work(priv->workqueue, &priv->post_associate.work);
7159
7160 mutex_unlock(&priv->mutex);
7161
7162 return 0;
7163}
7164
Zhu Yib481de92007-09-25 17:54:57 -07007165/*****************************************************************************
7166 *
7167 * sysfs attributes
7168 *
7169 *****************************************************************************/
7170
Tomas Winkler0a6857e2008-03-12 16:58:49 -07007171#ifdef CONFIG_IWLWIFI_DEBUG
Zhu Yib481de92007-09-25 17:54:57 -07007172
7173/*
7174 * The following adds a new attribute to the sysfs representation
7175 * of this device driver (i.e. a new file in /sys/bus/pci/drivers/iwl/)
7176 * used for controlling the debug level.
7177 *
7178 * See the level definitions in iwl for details.
7179 */
7180
7181static ssize_t show_debug_level(struct device_driver *d, char *buf)
7182{
Tomas Winkler0a6857e2008-03-12 16:58:49 -07007183 return sprintf(buf, "0x%08X\n", iwl_debug_level);
Zhu Yib481de92007-09-25 17:54:57 -07007184}
7185static ssize_t store_debug_level(struct device_driver *d,
7186 const char *buf, size_t count)
7187{
7188 char *p = (char *)buf;
7189 u32 val;
7190
7191 val = simple_strtoul(p, &p, 0);
7192 if (p == buf)
7193 printk(KERN_INFO DRV_NAME
7194 ": %s is not in hex or decimal form.\n", buf);
7195 else
Tomas Winkler0a6857e2008-03-12 16:58:49 -07007196 iwl_debug_level = val;
Zhu Yib481de92007-09-25 17:54:57 -07007197
7198 return strnlen(buf, count);
7199}
7200
7201static DRIVER_ATTR(debug_level, S_IWUSR | S_IRUGO,
7202 show_debug_level, store_debug_level);
7203
Tomas Winkler0a6857e2008-03-12 16:58:49 -07007204#endif /* CONFIG_IWLWIFI_DEBUG */
Zhu Yib481de92007-09-25 17:54:57 -07007205
Zhu Yib481de92007-09-25 17:54:57 -07007206
7207static ssize_t show_temperature(struct device *d,
7208 struct device_attribute *attr, char *buf)
7209{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007210 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07007211
Tomas Winklerfee12472008-04-03 16:05:21 -07007212 if (!iwl_is_alive(priv))
Zhu Yib481de92007-09-25 17:54:57 -07007213 return -EAGAIN;
7214
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007215 return sprintf(buf, "%d\n", iwl4965_hw_get_temperature(priv));
Zhu Yib481de92007-09-25 17:54:57 -07007216}
7217
7218static DEVICE_ATTR(temperature, S_IRUGO, show_temperature, NULL);
7219
7220static ssize_t show_rs_window(struct device *d,
7221 struct device_attribute *attr,
7222 char *buf)
7223{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007224 struct iwl_priv *priv = d->driver_data;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007225 return iwl4965_fill_rs_info(priv->hw, buf, IWL_AP_ID);
Zhu Yib481de92007-09-25 17:54:57 -07007226}
7227static DEVICE_ATTR(rs_window, S_IRUGO, show_rs_window, NULL);
7228
7229static ssize_t show_tx_power(struct device *d,
7230 struct device_attribute *attr, char *buf)
7231{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007232 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07007233 return sprintf(buf, "%d\n", priv->user_txpower_limit);
7234}
7235
7236static ssize_t store_tx_power(struct device *d,
7237 struct device_attribute *attr,
7238 const char *buf, size_t count)
7239{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007240 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07007241 char *p = (char *)buf;
7242 u32 val;
7243
7244 val = simple_strtoul(p, &p, 10);
7245 if (p == buf)
7246 printk(KERN_INFO DRV_NAME
7247 ": %s is not in decimal form.\n", buf);
7248 else
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007249 iwl4965_hw_reg_set_txpower(priv, val);
Zhu Yib481de92007-09-25 17:54:57 -07007250
7251 return count;
7252}
7253
7254static DEVICE_ATTR(tx_power, S_IWUSR | S_IRUGO, show_tx_power, store_tx_power);
7255
7256static ssize_t show_flags(struct device *d,
7257 struct device_attribute *attr, char *buf)
7258{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007259 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07007260
7261 return sprintf(buf, "0x%04X\n", priv->active_rxon.flags);
7262}
7263
7264static ssize_t store_flags(struct device *d,
7265 struct device_attribute *attr,
7266 const char *buf, size_t count)
7267{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007268 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07007269 u32 flags = simple_strtoul(buf, NULL, 0);
7270
7271 mutex_lock(&priv->mutex);
7272 if (le32_to_cpu(priv->staging_rxon.flags) != flags) {
7273 /* Cancel any currently running scans... */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007274 if (iwl4965_scan_cancel_timeout(priv, 100))
Zhu Yib481de92007-09-25 17:54:57 -07007275 IWL_WARNING("Could not cancel scan.\n");
7276 else {
7277 IWL_DEBUG_INFO("Committing rxon.flags = 0x%04X\n",
7278 flags);
7279 priv->staging_rxon.flags = cpu_to_le32(flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007280 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007281 }
7282 }
7283 mutex_unlock(&priv->mutex);
7284
7285 return count;
7286}
7287
7288static DEVICE_ATTR(flags, S_IWUSR | S_IRUGO, show_flags, store_flags);
7289
7290static ssize_t show_filter_flags(struct device *d,
7291 struct device_attribute *attr, char *buf)
7292{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007293 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07007294
7295 return sprintf(buf, "0x%04X\n",
7296 le32_to_cpu(priv->active_rxon.filter_flags));
7297}
7298
7299static ssize_t store_filter_flags(struct device *d,
7300 struct device_attribute *attr,
7301 const char *buf, size_t count)
7302{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007303 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07007304 u32 filter_flags = simple_strtoul(buf, NULL, 0);
7305
7306 mutex_lock(&priv->mutex);
7307 if (le32_to_cpu(priv->staging_rxon.filter_flags) != filter_flags) {
7308 /* Cancel any currently running scans... */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007309 if (iwl4965_scan_cancel_timeout(priv, 100))
Zhu Yib481de92007-09-25 17:54:57 -07007310 IWL_WARNING("Could not cancel scan.\n");
7311 else {
7312 IWL_DEBUG_INFO("Committing rxon.filter_flags = "
7313 "0x%04X\n", filter_flags);
7314 priv->staging_rxon.filter_flags =
7315 cpu_to_le32(filter_flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007316 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007317 }
7318 }
7319 mutex_unlock(&priv->mutex);
7320
7321 return count;
7322}
7323
7324static DEVICE_ATTR(filter_flags, S_IWUSR | S_IRUGO, show_filter_flags,
7325 store_filter_flags);
7326
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08007327#ifdef CONFIG_IWL4965_SPECTRUM_MEASUREMENT
Zhu Yib481de92007-09-25 17:54:57 -07007328
7329static ssize_t show_measurement(struct device *d,
7330 struct device_attribute *attr, char *buf)
7331{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007332 struct iwl_priv *priv = dev_get_drvdata(d);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007333 struct iwl4965_spectrum_notification measure_report;
Zhu Yib481de92007-09-25 17:54:57 -07007334 u32 size = sizeof(measure_report), len = 0, ofs = 0;
7335 u8 *data = (u8 *) & measure_report;
7336 unsigned long flags;
7337
7338 spin_lock_irqsave(&priv->lock, flags);
7339 if (!(priv->measurement_status & MEASUREMENT_READY)) {
7340 spin_unlock_irqrestore(&priv->lock, flags);
7341 return 0;
7342 }
7343 memcpy(&measure_report, &priv->measure_report, size);
7344 priv->measurement_status = 0;
7345 spin_unlock_irqrestore(&priv->lock, flags);
7346
7347 while (size && (PAGE_SIZE - len)) {
7348 hex_dump_to_buffer(data + ofs, size, 16, 1, buf + len,
7349 PAGE_SIZE - len, 1);
7350 len = strlen(buf);
7351 if (PAGE_SIZE - len)
7352 buf[len++] = '\n';
7353
7354 ofs += 16;
7355 size -= min(size, 16U);
7356 }
7357
7358 return len;
7359}
7360
7361static ssize_t store_measurement(struct device *d,
7362 struct device_attribute *attr,
7363 const char *buf, size_t count)
7364{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007365 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07007366 struct ieee80211_measurement_params params = {
7367 .channel = le16_to_cpu(priv->active_rxon.channel),
7368 .start_time = cpu_to_le64(priv->last_tsf),
7369 .duration = cpu_to_le16(1),
7370 };
7371 u8 type = IWL_MEASURE_BASIC;
7372 u8 buffer[32];
7373 u8 channel;
7374
7375 if (count) {
7376 char *p = buffer;
7377 strncpy(buffer, buf, min(sizeof(buffer), count));
7378 channel = simple_strtoul(p, NULL, 0);
7379 if (channel)
7380 params.channel = channel;
7381
7382 p = buffer;
7383 while (*p && *p != ' ')
7384 p++;
7385 if (*p)
7386 type = simple_strtoul(p + 1, NULL, 0);
7387 }
7388
7389 IWL_DEBUG_INFO("Invoking measurement of type %d on "
7390 "channel %d (for '%s')\n", type, params.channel, buf);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007391 iwl4965_get_measurement(priv, &params, type);
Zhu Yib481de92007-09-25 17:54:57 -07007392
7393 return count;
7394}
7395
7396static DEVICE_ATTR(measurement, S_IRUSR | S_IWUSR,
7397 show_measurement, store_measurement);
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08007398#endif /* CONFIG_IWL4965_SPECTRUM_MEASUREMENT */
Zhu Yib481de92007-09-25 17:54:57 -07007399
7400static ssize_t store_retry_rate(struct device *d,
7401 struct device_attribute *attr,
7402 const char *buf, size_t count)
7403{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007404 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07007405
7406 priv->retry_rate = simple_strtoul(buf, NULL, 0);
7407 if (priv->retry_rate <= 0)
7408 priv->retry_rate = 1;
7409
7410 return count;
7411}
7412
7413static ssize_t show_retry_rate(struct device *d,
7414 struct device_attribute *attr, char *buf)
7415{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007416 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07007417 return sprintf(buf, "%d", priv->retry_rate);
7418}
7419
7420static DEVICE_ATTR(retry_rate, S_IWUSR | S_IRUSR, show_retry_rate,
7421 store_retry_rate);
7422
7423static ssize_t store_power_level(struct device *d,
7424 struct device_attribute *attr,
7425 const char *buf, size_t count)
7426{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007427 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07007428 int rc;
7429 int mode;
7430
7431 mode = simple_strtoul(buf, NULL, 0);
7432 mutex_lock(&priv->mutex);
7433
Tomas Winklerfee12472008-04-03 16:05:21 -07007434 if (!iwl_is_ready(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07007435 rc = -EAGAIN;
7436 goto out;
7437 }
7438
7439 if ((mode < 1) || (mode > IWL_POWER_LIMIT) || (mode == IWL_POWER_AC))
7440 mode = IWL_POWER_AC;
7441 else
7442 mode |= IWL_POWER_ENABLED;
7443
7444 if (mode != priv->power_mode) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007445 rc = iwl4965_send_power_mode(priv, IWL_POWER_LEVEL(mode));
Zhu Yib481de92007-09-25 17:54:57 -07007446 if (rc) {
7447 IWL_DEBUG_MAC80211("failed setting power mode.\n");
7448 goto out;
7449 }
7450 priv->power_mode = mode;
7451 }
7452
7453 rc = count;
7454
7455 out:
7456 mutex_unlock(&priv->mutex);
7457 return rc;
7458}
7459
7460#define MAX_WX_STRING 80
7461
7462/* Values are in microsecond */
7463static const s32 timeout_duration[] = {
7464 350000,
7465 250000,
7466 75000,
7467 37000,
7468 25000,
7469};
7470static const s32 period_duration[] = {
7471 400000,
7472 700000,
7473 1000000,
7474 1000000,
7475 1000000
7476};
7477
7478static ssize_t show_power_level(struct device *d,
7479 struct device_attribute *attr, char *buf)
7480{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007481 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07007482 int level = IWL_POWER_LEVEL(priv->power_mode);
7483 char *p = buf;
7484
7485 p += sprintf(p, "%d ", level);
7486 switch (level) {
7487 case IWL_POWER_MODE_CAM:
7488 case IWL_POWER_AC:
7489 p += sprintf(p, "(AC)");
7490 break;
7491 case IWL_POWER_BATTERY:
7492 p += sprintf(p, "(BATTERY)");
7493 break;
7494 default:
7495 p += sprintf(p,
7496 "(Timeout %dms, Period %dms)",
7497 timeout_duration[level - 1] / 1000,
7498 period_duration[level - 1] / 1000);
7499 }
7500
7501 if (!(priv->power_mode & IWL_POWER_ENABLED))
7502 p += sprintf(p, " OFF\n");
7503 else
7504 p += sprintf(p, " \n");
7505
7506 return (p - buf + 1);
7507
7508}
7509
7510static DEVICE_ATTR(power_level, S_IWUSR | S_IRUSR, show_power_level,
7511 store_power_level);
7512
7513static ssize_t show_channels(struct device *d,
7514 struct device_attribute *attr, char *buf)
7515{
Johannes Berg8318d782008-01-24 19:38:38 +01007516 /* all this shit doesn't belong into sysfs anyway */
7517 return 0;
Zhu Yib481de92007-09-25 17:54:57 -07007518}
7519
7520static DEVICE_ATTR(channels, S_IRUSR, show_channels, NULL);
7521
7522static ssize_t show_statistics(struct device *d,
7523 struct device_attribute *attr, char *buf)
7524{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007525 struct iwl_priv *priv = dev_get_drvdata(d);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007526 u32 size = sizeof(struct iwl4965_notif_statistics);
Zhu Yib481de92007-09-25 17:54:57 -07007527 u32 len = 0, ofs = 0;
7528 u8 *data = (u8 *) & priv->statistics;
7529 int rc = 0;
7530
Tomas Winklerfee12472008-04-03 16:05:21 -07007531 if (!iwl_is_alive(priv))
Zhu Yib481de92007-09-25 17:54:57 -07007532 return -EAGAIN;
7533
7534 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007535 rc = iwl4965_send_statistics_request(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007536 mutex_unlock(&priv->mutex);
7537
7538 if (rc) {
7539 len = sprintf(buf,
7540 "Error sending statistics request: 0x%08X\n", rc);
7541 return len;
7542 }
7543
7544 while (size && (PAGE_SIZE - len)) {
7545 hex_dump_to_buffer(data + ofs, size, 16, 1, buf + len,
7546 PAGE_SIZE - len, 1);
7547 len = strlen(buf);
7548 if (PAGE_SIZE - len)
7549 buf[len++] = '\n';
7550
7551 ofs += 16;
7552 size -= min(size, 16U);
7553 }
7554
7555 return len;
7556}
7557
7558static DEVICE_ATTR(statistics, S_IRUGO, show_statistics, NULL);
7559
7560static ssize_t show_antenna(struct device *d,
7561 struct device_attribute *attr, char *buf)
7562{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007563 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07007564
Tomas Winklerfee12472008-04-03 16:05:21 -07007565 if (!iwl_is_alive(priv))
Zhu Yib481de92007-09-25 17:54:57 -07007566 return -EAGAIN;
7567
7568 return sprintf(buf, "%d\n", priv->antenna);
7569}
7570
7571static ssize_t store_antenna(struct device *d,
7572 struct device_attribute *attr,
7573 const char *buf, size_t count)
7574{
7575 int ant;
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007576 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07007577
7578 if (count == 0)
7579 return 0;
7580
7581 if (sscanf(buf, "%1i", &ant) != 1) {
7582 IWL_DEBUG_INFO("not in hex or decimal form.\n");
7583 return count;
7584 }
7585
7586 if ((ant >= 0) && (ant <= 2)) {
7587 IWL_DEBUG_INFO("Setting antenna select to %d.\n", ant);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007588 priv->antenna = (enum iwl4965_antenna)ant;
Zhu Yib481de92007-09-25 17:54:57 -07007589 } else
7590 IWL_DEBUG_INFO("Bad antenna select value %d.\n", ant);
7591
7592
7593 return count;
7594}
7595
7596static DEVICE_ATTR(antenna, S_IWUSR | S_IRUGO, show_antenna, store_antenna);
7597
7598static ssize_t show_status(struct device *d,
7599 struct device_attribute *attr, char *buf)
7600{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007601 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Tomas Winklerfee12472008-04-03 16:05:21 -07007602 if (!iwl_is_alive(priv))
Zhu Yib481de92007-09-25 17:54:57 -07007603 return -EAGAIN;
7604 return sprintf(buf, "0x%08x\n", (int)priv->status);
7605}
7606
7607static DEVICE_ATTR(status, S_IRUGO, show_status, NULL);
7608
7609static ssize_t dump_error_log(struct device *d,
7610 struct device_attribute *attr,
7611 const char *buf, size_t count)
7612{
7613 char *p = (char *)buf;
7614
7615 if (p[0] == '1')
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007616 iwl4965_dump_nic_error_log((struct iwl_priv *)d->driver_data);
Zhu Yib481de92007-09-25 17:54:57 -07007617
7618 return strnlen(buf, count);
7619}
7620
7621static DEVICE_ATTR(dump_errors, S_IWUSR, NULL, dump_error_log);
7622
7623static ssize_t dump_event_log(struct device *d,
7624 struct device_attribute *attr,
7625 const char *buf, size_t count)
7626{
7627 char *p = (char *)buf;
7628
7629 if (p[0] == '1')
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007630 iwl4965_dump_nic_event_log((struct iwl_priv *)d->driver_data);
Zhu Yib481de92007-09-25 17:54:57 -07007631
7632 return strnlen(buf, count);
7633}
7634
7635static DEVICE_ATTR(dump_events, S_IWUSR, NULL, dump_event_log);
7636
7637/*****************************************************************************
7638 *
7639 * driver setup and teardown
7640 *
7641 *****************************************************************************/
7642
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007643static void iwl4965_setup_deferred_work(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07007644{
7645 priv->workqueue = create_workqueue(DRV_NAME);
7646
7647 init_waitqueue_head(&priv->wait_command_queue);
7648
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007649 INIT_WORK(&priv->up, iwl4965_bg_up);
7650 INIT_WORK(&priv->restart, iwl4965_bg_restart);
7651 INIT_WORK(&priv->rx_replenish, iwl4965_bg_rx_replenish);
7652 INIT_WORK(&priv->scan_completed, iwl4965_bg_scan_completed);
7653 INIT_WORK(&priv->request_scan, iwl4965_bg_request_scan);
7654 INIT_WORK(&priv->abort_scan, iwl4965_bg_abort_scan);
7655 INIT_WORK(&priv->rf_kill, iwl4965_bg_rf_kill);
7656 INIT_WORK(&priv->beacon_update, iwl4965_bg_beacon_update);
7657 INIT_DELAYED_WORK(&priv->post_associate, iwl4965_bg_post_associate);
7658 INIT_DELAYED_WORK(&priv->init_alive_start, iwl4965_bg_init_alive_start);
7659 INIT_DELAYED_WORK(&priv->alive_start, iwl4965_bg_alive_start);
7660 INIT_DELAYED_WORK(&priv->scan_check, iwl4965_bg_scan_check);
Zhu Yib481de92007-09-25 17:54:57 -07007661
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007662 iwl4965_hw_setup_deferred_work(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007663
7664 tasklet_init(&priv->irq_tasklet, (void (*)(unsigned long))
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007665 iwl4965_irq_tasklet, (unsigned long)priv);
Zhu Yib481de92007-09-25 17:54:57 -07007666}
7667
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007668static void iwl4965_cancel_deferred_work(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07007669{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007670 iwl4965_hw_cancel_deferred_work(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007671
Joonwoo Park3ae6a052007-11-29 10:43:16 +09007672 cancel_delayed_work_sync(&priv->init_alive_start);
Zhu Yib481de92007-09-25 17:54:57 -07007673 cancel_delayed_work(&priv->scan_check);
7674 cancel_delayed_work(&priv->alive_start);
7675 cancel_delayed_work(&priv->post_associate);
7676 cancel_work_sync(&priv->beacon_update);
7677}
7678
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007679static struct attribute *iwl4965_sysfs_entries[] = {
Zhu Yib481de92007-09-25 17:54:57 -07007680 &dev_attr_antenna.attr,
7681 &dev_attr_channels.attr,
7682 &dev_attr_dump_errors.attr,
7683 &dev_attr_dump_events.attr,
7684 &dev_attr_flags.attr,
7685 &dev_attr_filter_flags.attr,
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08007686#ifdef CONFIG_IWL4965_SPECTRUM_MEASUREMENT
Zhu Yib481de92007-09-25 17:54:57 -07007687 &dev_attr_measurement.attr,
7688#endif
7689 &dev_attr_power_level.attr,
7690 &dev_attr_retry_rate.attr,
Zhu Yib481de92007-09-25 17:54:57 -07007691 &dev_attr_rs_window.attr,
7692 &dev_attr_statistics.attr,
7693 &dev_attr_status.attr,
7694 &dev_attr_temperature.attr,
Zhu Yib481de92007-09-25 17:54:57 -07007695 &dev_attr_tx_power.attr,
7696
7697 NULL
7698};
7699
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007700static struct attribute_group iwl4965_attribute_group = {
Zhu Yib481de92007-09-25 17:54:57 -07007701 .name = NULL, /* put in device directory */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007702 .attrs = iwl4965_sysfs_entries,
Zhu Yib481de92007-09-25 17:54:57 -07007703};
7704
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007705static struct ieee80211_ops iwl4965_hw_ops = {
7706 .tx = iwl4965_mac_tx,
7707 .start = iwl4965_mac_start,
7708 .stop = iwl4965_mac_stop,
7709 .add_interface = iwl4965_mac_add_interface,
7710 .remove_interface = iwl4965_mac_remove_interface,
7711 .config = iwl4965_mac_config,
7712 .config_interface = iwl4965_mac_config_interface,
7713 .configure_filter = iwl4965_configure_filter,
7714 .set_key = iwl4965_mac_set_key,
Emmanuel Grumbachab885f82008-03-20 15:06:43 +02007715 .update_tkip_key = iwl4965_mac_update_tkip_key,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007716 .get_stats = iwl4965_mac_get_stats,
7717 .get_tx_stats = iwl4965_mac_get_tx_stats,
7718 .conf_tx = iwl4965_mac_conf_tx,
7719 .get_tsf = iwl4965_mac_get_tsf,
7720 .reset_tsf = iwl4965_mac_reset_tsf,
7721 .beacon_update = iwl4965_mac_beacon_update,
Johannes Berg471b3ef2007-12-28 14:32:58 +01007722 .bss_info_changed = iwl4965_bss_info_changed,
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08007723#ifdef CONFIG_IWL4965_HT
Ron Rindjunsky9ab46172007-12-25 17:00:38 +02007724 .ampdu_action = iwl4965_mac_ampdu_action,
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08007725#endif /* CONFIG_IWL4965_HT */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007726 .hw_scan = iwl4965_mac_hw_scan
Zhu Yib481de92007-09-25 17:54:57 -07007727};
7728
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007729static int iwl4965_pci_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
Zhu Yib481de92007-09-25 17:54:57 -07007730{
7731 int err = 0;
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007732 struct iwl_priv *priv;
Zhu Yib481de92007-09-25 17:54:57 -07007733 struct ieee80211_hw *hw;
Tomas Winkler82b9a122008-03-04 18:09:30 -08007734 struct iwl_cfg *cfg = (struct iwl_cfg *)(ent->driver_data);
Mohamed Abbas0359fac2008-03-28 16:21:08 -07007735 unsigned long flags;
Zhu Yi5a66926a2008-01-14 17:46:18 -08007736 DECLARE_MAC_BUF(mac);
Zhu Yib481de92007-09-25 17:54:57 -07007737
Assaf Krauss316c30d2008-03-14 10:38:46 -07007738 /************************
7739 * 1. Allocating HW data
7740 ************************/
7741
Cahill, Ben M6440adb2007-11-29 11:09:55 +08007742 /* Disabling hardware scan means that mac80211 will perform scans
7743 * "the hard way", rather than using device's scan. */
Assaf Krauss1ea87392008-03-18 14:57:50 -07007744 if (cfg->mod_params->disable_hw_scan) {
Zhu Yib481de92007-09-25 17:54:57 -07007745 IWL_DEBUG_INFO("Disabling hw_scan\n");
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007746 iwl4965_hw_ops.hw_scan = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07007747 }
7748
Assaf Krauss1d0a0822008-03-14 10:38:48 -07007749 hw = iwl_alloc_all(cfg, &iwl4965_hw_ops);
7750 if (!hw) {
Zhu Yib481de92007-09-25 17:54:57 -07007751 err = -ENOMEM;
7752 goto out;
7753 }
Assaf Krauss1d0a0822008-03-14 10:38:48 -07007754 priv = hw->priv;
7755 /* At this point both hw and priv are allocated. */
7756
Zhu Yib481de92007-09-25 17:54:57 -07007757 SET_IEEE80211_DEV(hw, &pdev->dev);
7758
7759 IWL_DEBUG_INFO("*** LOAD DRIVER ***\n");
Tomas Winkler82b9a122008-03-04 18:09:30 -08007760 priv->cfg = cfg;
Zhu Yib481de92007-09-25 17:54:57 -07007761 priv->pci_dev = pdev;
Assaf Krauss316c30d2008-03-14 10:38:46 -07007762
Tomas Winkler0a6857e2008-03-12 16:58:49 -07007763#ifdef CONFIG_IWLWIFI_DEBUG
Assaf Krauss1ea87392008-03-18 14:57:50 -07007764 iwl_debug_level = priv->cfg->mod_params->debug;
Zhu Yib481de92007-09-25 17:54:57 -07007765 atomic_set(&priv->restrict_refcnt, 0);
7766#endif
Zhu Yib481de92007-09-25 17:54:57 -07007767
Assaf Krauss316c30d2008-03-14 10:38:46 -07007768 /**************************
7769 * 2. Initializing PCI bus
7770 **************************/
7771 if (pci_enable_device(pdev)) {
7772 err = -ENODEV;
7773 goto out_ieee80211_free_hw;
7774 }
7775
7776 pci_set_master(pdev);
7777
7778 err = pci_set_dma_mask(pdev, DMA_32BIT_MASK);
7779 if (!err)
7780 err = pci_set_consistent_dma_mask(pdev, DMA_32BIT_MASK);
7781 if (err) {
7782 printk(KERN_WARNING DRV_NAME
7783 ": No suitable DMA available.\n");
7784 goto out_pci_disable_device;
7785 }
7786
7787 err = pci_request_regions(pdev, DRV_NAME);
7788 if (err)
7789 goto out_pci_disable_device;
7790
7791 pci_set_drvdata(pdev, priv);
7792
7793 /* We disable the RETRY_TIMEOUT register (0x41) to keep
7794 * PCI Tx retries from interfering with C3 CPU state */
7795 pci_write_config_byte(pdev, 0x41, 0x00);
7796
7797 /***********************
7798 * 3. Read REV register
7799 ***********************/
7800 priv->hw_base = pci_iomap(pdev, 0, 0);
7801 if (!priv->hw_base) {
7802 err = -ENODEV;
7803 goto out_pci_release_regions;
7804 }
7805
7806 IWL_DEBUG_INFO("pci_resource_len = 0x%08llx\n",
7807 (unsigned long long) pci_resource_len(pdev, 0));
7808 IWL_DEBUG_INFO("pci_resource_base = %p\n", priv->hw_base);
7809
7810 printk(KERN_INFO DRV_NAME
7811 ": Detected Intel Wireless WiFi Link %s\n", priv->cfg->name);
7812
7813 /*****************
7814 * 4. Read EEPROM
7815 *****************/
7816 /* nic init */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07007817 iwl_set_bit(priv, CSR_GIO_CHICKEN_BITS,
Assaf Krauss316c30d2008-03-14 10:38:46 -07007818 CSR_GIO_CHICKEN_BITS_REG_BIT_DIS_L0S_EXIT_TIMER);
7819
Tomas Winkler3395f6e2008-03-25 16:33:37 -07007820 iwl_set_bit(priv, CSR_GP_CNTRL, CSR_GP_CNTRL_REG_FLAG_INIT_DONE);
7821 err = iwl_poll_bit(priv, CSR_GP_CNTRL,
Assaf Krauss316c30d2008-03-14 10:38:46 -07007822 CSR_GP_CNTRL_REG_FLAG_MAC_CLOCK_READY,
7823 CSR_GP_CNTRL_REG_FLAG_MAC_CLOCK_READY, 25000);
7824 if (err < 0) {
7825 IWL_DEBUG_INFO("Failed to init the card\n");
7826 goto out_iounmap;
7827 }
7828 /* Read the EEPROM */
7829 err = iwl_eeprom_init(priv);
7830 if (err) {
7831 IWL_ERROR("Unable to init EEPROM\n");
7832 goto out_iounmap;
7833 }
7834 /* MAC Address location in EEPROM same for 3945/4965 */
7835 iwl_eeprom_get_mac(priv, priv->mac_addr);
7836 IWL_DEBUG_INFO("MAC address: %s\n", print_mac(mac, priv->mac_addr));
7837 SET_IEEE80211_PERM_ADDR(priv->hw, priv->mac_addr);
7838
7839 /************************
7840 * 5. Setup HW constants
7841 ************************/
7842 /* Device-specific setup */
7843 if (iwl4965_hw_set_hw_setting(priv)) {
7844 IWL_ERROR("failed to set hw settings\n");
7845 goto out_iounmap;
7846 }
7847
7848 /*******************
7849 * 6. Setup hw/priv
7850 *******************/
Zhu Yib481de92007-09-25 17:54:57 -07007851
Assaf Kraussbf85ea42008-03-14 10:38:49 -07007852 err = iwl_setup(priv);
7853 if (err)
Assaf Krauss316c30d2008-03-14 10:38:46 -07007854 goto out_unset_hw_settings;
Assaf Kraussbf85ea42008-03-14 10:38:49 -07007855 /* At this point both hw and priv are initialized. */
Assaf Krauss316c30d2008-03-14 10:38:46 -07007856
7857 /**********************************
7858 * 7. Initialize module parameters
7859 **********************************/
7860
7861 /* Disable radio (SW RF KILL) via parameter when loading driver */
Assaf Krauss1ea87392008-03-18 14:57:50 -07007862 if (priv->cfg->mod_params->disable) {
Assaf Krauss316c30d2008-03-14 10:38:46 -07007863 set_bit(STATUS_RF_KILL_SW, &priv->status);
7864 IWL_DEBUG_INFO("Radio disabled.\n");
7865 }
7866
Assaf Krauss1ea87392008-03-18 14:57:50 -07007867 if (priv->cfg->mod_params->enable_qos)
Assaf Krauss316c30d2008-03-14 10:38:46 -07007868 priv->qos_data.qos_enable = 1;
7869
7870 /********************
7871 * 8. Setup services
7872 ********************/
Mohamed Abbas0359fac2008-03-28 16:21:08 -07007873 spin_lock_irqsave(&priv->lock, flags);
Assaf Krauss316c30d2008-03-14 10:38:46 -07007874 iwl4965_disable_interrupts(priv);
Mohamed Abbas0359fac2008-03-28 16:21:08 -07007875 spin_unlock_irqrestore(&priv->lock, flags);
Assaf Krauss316c30d2008-03-14 10:38:46 -07007876
7877 err = sysfs_create_group(&pdev->dev.kobj, &iwl4965_attribute_group);
7878 if (err) {
7879 IWL_ERROR("failed to create sysfs device attributes\n");
Assaf Kraussbf85ea42008-03-14 10:38:49 -07007880 goto out_unset_hw_settings;
Assaf Krauss316c30d2008-03-14 10:38:46 -07007881 }
7882
7883 err = iwl_dbgfs_register(priv, DRV_NAME);
7884 if (err) {
7885 IWL_ERROR("failed to create debugfs files\n");
7886 goto out_remove_sysfs;
7887 }
7888
7889 iwl4965_setup_deferred_work(priv);
7890 iwl4965_setup_rx_handlers(priv);
7891
7892 /********************
7893 * 9. Conclude
7894 ********************/
Zhu Yi5a66926a2008-01-14 17:46:18 -08007895 pci_save_state(pdev);
7896 pci_disable_device(pdev);
Zhu Yib481de92007-09-25 17:54:57 -07007897
Mohamed Abbasc8381fd2008-03-28 16:21:05 -07007898 /* notify iwlcore to init */
7899 iwlcore_low_level_notify(priv, IWLCORE_INIT_EVT);
Zhu Yib481de92007-09-25 17:54:57 -07007900 return 0;
7901
Assaf Krauss316c30d2008-03-14 10:38:46 -07007902 out_remove_sysfs:
7903 sysfs_remove_group(&pdev->dev.kobj, &iwl4965_attribute_group);
Assaf Krauss316c30d2008-03-14 10:38:46 -07007904 out_unset_hw_settings:
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007905 iwl4965_unset_hw_setting(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007906 out_iounmap:
7907 pci_iounmap(pdev, priv->hw_base);
7908 out_pci_release_regions:
7909 pci_release_regions(pdev);
Assaf Krauss316c30d2008-03-14 10:38:46 -07007910 pci_set_drvdata(pdev, NULL);
Zhu Yib481de92007-09-25 17:54:57 -07007911 out_pci_disable_device:
7912 pci_disable_device(pdev);
Zhu Yib481de92007-09-25 17:54:57 -07007913 out_ieee80211_free_hw:
7914 ieee80211_free_hw(priv->hw);
7915 out:
7916 return err;
7917}
7918
Reinette Chatrec83dbf62008-03-21 13:53:41 -07007919static void __devexit iwl4965_pci_remove(struct pci_dev *pdev)
Zhu Yib481de92007-09-25 17:54:57 -07007920{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007921 struct iwl_priv *priv = pci_get_drvdata(pdev);
Zhu Yib481de92007-09-25 17:54:57 -07007922 struct list_head *p, *q;
7923 int i;
Mohamed Abbas0359fac2008-03-28 16:21:08 -07007924 unsigned long flags;
Zhu Yib481de92007-09-25 17:54:57 -07007925
7926 if (!priv)
7927 return;
7928
7929 IWL_DEBUG_INFO("*** UNLOAD DRIVER ***\n");
7930
Ron Rindjunskyc4f55232008-03-28 16:21:10 -07007931 if (priv->mac80211_registered) {
7932 ieee80211_unregister_hw(priv->hw);
7933 priv->mac80211_registered = 0;
7934 }
7935
Zhu Yib481de92007-09-25 17:54:57 -07007936 set_bit(STATUS_EXIT_PENDING, &priv->status);
Zhu Yib24d22b2007-12-19 13:59:52 +08007937
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007938 iwl4965_down(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007939
Mohamed Abbas0359fac2008-03-28 16:21:08 -07007940 /* make sure we flush any pending irq or
7941 * tasklet for the driver
7942 */
7943 spin_lock_irqsave(&priv->lock, flags);
7944 iwl4965_disable_interrupts(priv);
7945 spin_unlock_irqrestore(&priv->lock, flags);
7946
7947 iwl_synchronize_irq(priv);
7948
Zhu Yib481de92007-09-25 17:54:57 -07007949 /* Free MAC hash list for ADHOC */
7950 for (i = 0; i < IWL_IBSS_MAC_HASH_SIZE; i++) {
7951 list_for_each_safe(p, q, &priv->ibss_mac_hash[i]) {
7952 list_del(p);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007953 kfree(list_entry(p, struct iwl4965_ibss_seq, list));
Zhu Yib481de92007-09-25 17:54:57 -07007954 }
7955 }
7956
Mohamed Abbasc8381fd2008-03-28 16:21:05 -07007957 iwlcore_low_level_notify(priv, IWLCORE_REMOVE_EVT);
Tomas Winkler712b6cf2008-03-12 16:58:52 -07007958 iwl_dbgfs_unregister(priv);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007959 sysfs_remove_group(&pdev->dev.kobj, &iwl4965_attribute_group);
Zhu Yib481de92007-09-25 17:54:57 -07007960
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007961 iwl4965_dealloc_ucode_pci(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007962
7963 if (priv->rxq.bd)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007964 iwl4965_rx_queue_free(priv, &priv->rxq);
7965 iwl4965_hw_txq_ctx_free(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007966
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007967 iwl4965_unset_hw_setting(priv);
Assaf Kraussbf85ea42008-03-14 10:38:49 -07007968 iwlcore_clear_stations_table(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007969
Zhu Yib481de92007-09-25 17:54:57 -07007970
Mohamed Abbas948c1712007-10-25 17:15:45 +08007971 /*netif_stop_queue(dev); */
7972 flush_workqueue(priv->workqueue);
7973
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007974 /* ieee80211_unregister_hw calls iwl4965_mac_stop, which flushes
Zhu Yib481de92007-09-25 17:54:57 -07007975 * priv->workqueue... so we can't take down the workqueue
7976 * until now... */
7977 destroy_workqueue(priv->workqueue);
7978 priv->workqueue = NULL;
7979
Zhu Yib481de92007-09-25 17:54:57 -07007980 pci_iounmap(pdev, priv->hw_base);
7981 pci_release_regions(pdev);
7982 pci_disable_device(pdev);
7983 pci_set_drvdata(pdev, NULL);
7984
Assaf Kraussbf85ea42008-03-14 10:38:49 -07007985 iwl_free_channel_map(priv);
Reinette Chatre849e0dc2008-01-23 10:15:18 -08007986 iwl4965_free_geos(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007987
7988 if (priv->ibss_beacon)
7989 dev_kfree_skb(priv->ibss_beacon);
7990
7991 ieee80211_free_hw(priv->hw);
7992}
7993
7994#ifdef CONFIG_PM
7995
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007996static int iwl4965_pci_suspend(struct pci_dev *pdev, pm_message_t state)
Zhu Yib481de92007-09-25 17:54:57 -07007997{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007998 struct iwl_priv *priv = pci_get_drvdata(pdev);
Zhu Yib481de92007-09-25 17:54:57 -07007999
Zhu Yie655b9f2008-01-24 02:19:38 -08008000 if (priv->is_open) {
8001 set_bit(STATUS_IN_SUSPEND, &priv->status);
8002 iwl4965_mac_stop(priv->hw);
8003 priv->is_open = 1;
8004 }
Zhu Yib481de92007-09-25 17:54:57 -07008005
Zhu Yib481de92007-09-25 17:54:57 -07008006 pci_set_power_state(pdev, PCI_D3hot);
8007
Zhu Yib481de92007-09-25 17:54:57 -07008008 return 0;
8009}
8010
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008011static int iwl4965_pci_resume(struct pci_dev *pdev)
Zhu Yib481de92007-09-25 17:54:57 -07008012{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07008013 struct iwl_priv *priv = pci_get_drvdata(pdev);
Zhu Yib481de92007-09-25 17:54:57 -07008014
Zhu Yib481de92007-09-25 17:54:57 -07008015 pci_set_power_state(pdev, PCI_D0);
Zhu Yib481de92007-09-25 17:54:57 -07008016
Zhu Yie655b9f2008-01-24 02:19:38 -08008017 if (priv->is_open)
8018 iwl4965_mac_start(priv->hw);
Zhu Yib481de92007-09-25 17:54:57 -07008019
Zhu Yie655b9f2008-01-24 02:19:38 -08008020 clear_bit(STATUS_IN_SUSPEND, &priv->status);
Zhu Yib481de92007-09-25 17:54:57 -07008021 return 0;
8022}
8023
8024#endif /* CONFIG_PM */
8025
8026/*****************************************************************************
8027 *
8028 * driver and module entry point
8029 *
8030 *****************************************************************************/
8031
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008032static struct pci_driver iwl4965_driver = {
Zhu Yib481de92007-09-25 17:54:57 -07008033 .name = DRV_NAME,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008034 .id_table = iwl4965_hw_card_ids,
8035 .probe = iwl4965_pci_probe,
8036 .remove = __devexit_p(iwl4965_pci_remove),
Zhu Yib481de92007-09-25 17:54:57 -07008037#ifdef CONFIG_PM
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008038 .suspend = iwl4965_pci_suspend,
8039 .resume = iwl4965_pci_resume,
Zhu Yib481de92007-09-25 17:54:57 -07008040#endif
8041};
8042
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008043static int __init iwl4965_init(void)
Zhu Yib481de92007-09-25 17:54:57 -07008044{
8045
8046 int ret;
8047 printk(KERN_INFO DRV_NAME ": " DRV_DESCRIPTION ", " DRV_VERSION "\n");
8048 printk(KERN_INFO DRV_NAME ": " DRV_COPYRIGHT "\n");
Reinette Chatre897e1cf2008-03-28 16:21:09 -07008049
8050 ret = iwl4965_rate_control_register();
8051 if (ret) {
8052 IWL_ERROR("Unable to register rate control algorithm: %d\n", ret);
8053 return ret;
8054 }
8055
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008056 ret = pci_register_driver(&iwl4965_driver);
Zhu Yib481de92007-09-25 17:54:57 -07008057 if (ret) {
8058 IWL_ERROR("Unable to initialize PCI module\n");
Reinette Chatre897e1cf2008-03-28 16:21:09 -07008059 goto error_register;
Zhu Yib481de92007-09-25 17:54:57 -07008060 }
Tomas Winkler0a6857e2008-03-12 16:58:49 -07008061#ifdef CONFIG_IWLWIFI_DEBUG
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008062 ret = driver_create_file(&iwl4965_driver.driver, &driver_attr_debug_level);
Zhu Yib481de92007-09-25 17:54:57 -07008063 if (ret) {
8064 IWL_ERROR("Unable to create driver sysfs file\n");
Reinette Chatre897e1cf2008-03-28 16:21:09 -07008065 goto error_debug;
Zhu Yib481de92007-09-25 17:54:57 -07008066 }
8067#endif
8068
8069 return ret;
Reinette Chatre897e1cf2008-03-28 16:21:09 -07008070
8071#ifdef CONFIG_IWLWIFI_DEBUG
8072error_debug:
8073 pci_unregister_driver(&iwl4965_driver);
8074#endif
8075error_register:
8076 iwl4965_rate_control_unregister();
8077 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07008078}
8079
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008080static void __exit iwl4965_exit(void)
Zhu Yib481de92007-09-25 17:54:57 -07008081{
Tomas Winkler0a6857e2008-03-12 16:58:49 -07008082#ifdef CONFIG_IWLWIFI_DEBUG
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008083 driver_remove_file(&iwl4965_driver.driver, &driver_attr_debug_level);
Zhu Yib481de92007-09-25 17:54:57 -07008084#endif
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008085 pci_unregister_driver(&iwl4965_driver);
Reinette Chatre897e1cf2008-03-28 16:21:09 -07008086 iwl4965_rate_control_unregister();
Zhu Yib481de92007-09-25 17:54:57 -07008087}
8088
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008089module_exit(iwl4965_exit);
8090module_init(iwl4965_init);