blob: 37ab1c565ce1eb3874c42b2e380be0d9e1add32a [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
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07001115static int iwl4965_set_ccmp_dynamic_key_info(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07001116 struct ieee80211_key_conf *keyconf,
1117 u8 sta_id)
1118{
1119 unsigned long flags;
1120 __le16 key_flags = 0;
1121
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07001122 key_flags |= (STA_KEY_FLG_CCMP | STA_KEY_FLG_MAP_KEY_MSK);
1123 key_flags |= cpu_to_le16(keyconf->keyidx << STA_KEY_FLG_KEYID_POS);
1124
1125 if (sta_id == priv->hw_setting.bcast_sta_id)
1126 key_flags |= STA_KEY_MULTICAST_MSK;
1127
1128 keyconf->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
1129 keyconf->hw_key_idx = keyconf->keyidx;
1130
1131 key_flags &= ~STA_KEY_FLG_INVALID;
1132
Zhu Yib481de92007-09-25 17:54:57 -07001133 spin_lock_irqsave(&priv->sta_lock, flags);
1134 priv->stations[sta_id].keyinfo.alg = keyconf->alg;
1135 priv->stations[sta_id].keyinfo.keylen = keyconf->keylen;
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07001136
Zhu Yib481de92007-09-25 17:54:57 -07001137 memcpy(priv->stations[sta_id].keyinfo.key, keyconf->key,
1138 keyconf->keylen);
1139
1140 memcpy(priv->stations[sta_id].sta.key.key, keyconf->key,
1141 keyconf->keylen);
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07001142
1143 priv->stations[sta_id].sta.key.key_offset
1144 = (sta_id % STA_KEY_MAX_NUM);/*FIXME*/
Zhu Yib481de92007-09-25 17:54:57 -07001145 priv->stations[sta_id].sta.key.key_flags = key_flags;
1146 priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
1147 priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
1148
1149 spin_unlock_irqrestore(&priv->sta_lock, flags);
1150
1151 IWL_DEBUG_INFO("hwcrypto: modify ucode station key info\n");
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07001152 return iwl4965_send_add_station(priv,
1153 &priv->stations[sta_id].sta, CMD_ASYNC);
1154}
1155
1156static int iwl4965_set_tkip_dynamic_key_info(struct iwl_priv *priv,
1157 struct ieee80211_key_conf *keyconf,
1158 u8 sta_id)
1159{
Emmanuel Grumbach2bc75082008-03-19 16:41:45 -07001160 unsigned long flags;
1161 int ret = 0;
1162
1163 keyconf->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
1164 keyconf->flags |= IEEE80211_KEY_FLAG_GENERATE_MMIC;
1165 keyconf->hw_key_idx = keyconf->keyidx;
1166
1167 spin_lock_irqsave(&priv->sta_lock, flags);
1168
1169 priv->stations[sta_id].keyinfo.alg = keyconf->alg;
1170 priv->stations[sta_id].keyinfo.conf = keyconf;
1171 priv->stations[sta_id].keyinfo.keylen = 16;
1172
1173 /* This copy is acutally not needed: we get the key with each TX */
1174 memcpy(priv->stations[sta_id].keyinfo.key, keyconf->key, 16);
1175
1176 memcpy(priv->stations[sta_id].sta.key.key, keyconf->key, 16);
1177
1178 spin_unlock_irqrestore(&priv->sta_lock, flags);
1179
1180 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07001181}
1182
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001183static int iwl4965_clear_sta_key_info(struct iwl_priv *priv, u8 sta_id)
Zhu Yib481de92007-09-25 17:54:57 -07001184{
1185 unsigned long flags;
1186
Emmanuel Grumbach6974e362008-04-14 21:16:06 -07001187 priv->key_mapping_key = 0;
1188
Zhu Yib481de92007-09-25 17:54:57 -07001189 spin_lock_irqsave(&priv->sta_lock, flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001190 memset(&priv->stations[sta_id].keyinfo, 0, sizeof(struct iwl4965_hw_key));
1191 memset(&priv->stations[sta_id].sta.key, 0, sizeof(struct iwl4965_keyinfo));
Zhu Yib481de92007-09-25 17:54:57 -07001192 priv->stations[sta_id].sta.key.key_flags = STA_KEY_FLG_NO_ENC;
1193 priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
1194 priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
1195 spin_unlock_irqrestore(&priv->sta_lock, flags);
1196
1197 IWL_DEBUG_INFO("hwcrypto: clear ucode station key info\n");
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001198 iwl4965_send_add_station(priv, &priv->stations[sta_id].sta, 0);
Zhu Yib481de92007-09-25 17:54:57 -07001199 return 0;
1200}
1201
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07001202static int iwl4965_set_dynamic_key(struct iwl_priv *priv,
1203 struct ieee80211_key_conf *key, u8 sta_id)
1204{
1205 int ret;
1206
Emmanuel Grumbach6974e362008-04-14 21:16:06 -07001207 priv->key_mapping_key = 1;
1208
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07001209 switch (key->alg) {
1210 case ALG_CCMP:
1211 ret = iwl4965_set_ccmp_dynamic_key_info(priv, key, sta_id);
1212 break;
1213 case ALG_TKIP:
1214 ret = iwl4965_set_tkip_dynamic_key_info(priv, key, sta_id);
1215 break;
1216 case ALG_WEP:
Emmanuel Grumbach0211ddd2008-04-14 21:16:07 -07001217 ret = iwl_set_wep_dynamic_key_info(priv, key, sta_id);
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07001218 break;
1219 default:
1220 IWL_ERROR("Unknown alg: %s alg = %d\n", __func__, key->alg);
1221 ret = -EINVAL;
1222 }
1223
1224 return ret;
1225}
1226
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001227static void iwl4965_clear_free_frames(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001228{
1229 struct list_head *element;
1230
1231 IWL_DEBUG_INFO("%d frames on pre-allocated heap on clear.\n",
1232 priv->frames_count);
1233
1234 while (!list_empty(&priv->free_frames)) {
1235 element = priv->free_frames.next;
1236 list_del(element);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001237 kfree(list_entry(element, struct iwl4965_frame, list));
Zhu Yib481de92007-09-25 17:54:57 -07001238 priv->frames_count--;
1239 }
1240
1241 if (priv->frames_count) {
1242 IWL_WARNING("%d frames still in use. Did we lose one?\n",
1243 priv->frames_count);
1244 priv->frames_count = 0;
1245 }
1246}
1247
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001248static struct iwl4965_frame *iwl4965_get_free_frame(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001249{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001250 struct iwl4965_frame *frame;
Zhu Yib481de92007-09-25 17:54:57 -07001251 struct list_head *element;
1252 if (list_empty(&priv->free_frames)) {
1253 frame = kzalloc(sizeof(*frame), GFP_KERNEL);
1254 if (!frame) {
1255 IWL_ERROR("Could not allocate frame!\n");
1256 return NULL;
1257 }
1258
1259 priv->frames_count++;
1260 return frame;
1261 }
1262
1263 element = priv->free_frames.next;
1264 list_del(element);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001265 return list_entry(element, struct iwl4965_frame, list);
Zhu Yib481de92007-09-25 17:54:57 -07001266}
1267
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001268static void iwl4965_free_frame(struct iwl_priv *priv, struct iwl4965_frame *frame)
Zhu Yib481de92007-09-25 17:54:57 -07001269{
1270 memset(frame, 0, sizeof(*frame));
1271 list_add(&frame->list, &priv->free_frames);
1272}
1273
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001274unsigned int iwl4965_fill_beacon_frame(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07001275 struct ieee80211_hdr *hdr,
1276 const u8 *dest, int left)
1277{
1278
Tomas Winkler3109ece2008-03-28 16:33:35 -07001279 if (!iwl_is_associated(priv) || !priv->ibss_beacon ||
Zhu Yib481de92007-09-25 17:54:57 -07001280 ((priv->iw_mode != IEEE80211_IF_TYPE_IBSS) &&
1281 (priv->iw_mode != IEEE80211_IF_TYPE_AP)))
1282 return 0;
1283
1284 if (priv->ibss_beacon->len > left)
1285 return 0;
1286
1287 memcpy(hdr, priv->ibss_beacon->data, priv->ibss_beacon->len);
1288
1289 return priv->ibss_beacon->len;
1290}
1291
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001292static u8 iwl4965_rate_get_lowest_plcp(int rate_mask)
Zhu Yib481de92007-09-25 17:54:57 -07001293{
1294 u8 i;
1295
1296 for (i = IWL_RATE_1M_INDEX; i != IWL_RATE_INVALID;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001297 i = iwl4965_rates[i].next_ieee) {
Zhu Yib481de92007-09-25 17:54:57 -07001298 if (rate_mask & (1 << i))
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001299 return iwl4965_rates[i].plcp;
Zhu Yib481de92007-09-25 17:54:57 -07001300 }
1301
1302 return IWL_RATE_INVALID;
1303}
1304
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001305static int iwl4965_send_beacon_cmd(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001306{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001307 struct iwl4965_frame *frame;
Zhu Yib481de92007-09-25 17:54:57 -07001308 unsigned int frame_size;
1309 int rc;
1310 u8 rate;
1311
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001312 frame = iwl4965_get_free_frame(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001313
1314 if (!frame) {
1315 IWL_ERROR("Could not obtain free frame buffer for beacon "
1316 "command.\n");
1317 return -ENOMEM;
1318 }
1319
1320 if (!(priv->staging_rxon.flags & RXON_FLG_BAND_24G_MSK)) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001321 rate = iwl4965_rate_get_lowest_plcp(priv->active_rate_basic &
Zhu Yib481de92007-09-25 17:54:57 -07001322 0xFF0);
1323 if (rate == IWL_INVALID_RATE)
1324 rate = IWL_RATE_6M_PLCP;
1325 } else {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001326 rate = iwl4965_rate_get_lowest_plcp(priv->active_rate_basic & 0xF);
Zhu Yib481de92007-09-25 17:54:57 -07001327 if (rate == IWL_INVALID_RATE)
1328 rate = IWL_RATE_1M_PLCP;
1329 }
1330
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001331 frame_size = iwl4965_hw_get_beacon_cmd(priv, frame, rate);
Zhu Yib481de92007-09-25 17:54:57 -07001332
Tomas Winkler857485c2008-03-21 13:53:44 -07001333 rc = iwl_send_cmd_pdu(priv, REPLY_TX_BEACON, frame_size,
Zhu Yib481de92007-09-25 17:54:57 -07001334 &frame->u.cmd[0]);
1335
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001336 iwl4965_free_frame(priv, frame);
Zhu Yib481de92007-09-25 17:54:57 -07001337
1338 return rc;
1339}
1340
1341/******************************************************************************
1342 *
Zhu Yib481de92007-09-25 17:54:57 -07001343 * Misc. internal state and helper functions
1344 *
1345 ******************************************************************************/
Zhu Yib481de92007-09-25 17:54:57 -07001346
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001347static void iwl4965_unset_hw_setting(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001348{
1349 if (priv->hw_setting.shared_virt)
1350 pci_free_consistent(priv->pci_dev,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001351 sizeof(struct iwl4965_shared),
Zhu Yib481de92007-09-25 17:54:57 -07001352 priv->hw_setting.shared_virt,
1353 priv->hw_setting.shared_phys);
1354}
1355
1356/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001357 * iwl4965_supported_rate_to_ie - fill in the supported rate in IE field
Zhu Yib481de92007-09-25 17:54:57 -07001358 *
1359 * return : set the bit for each supported rate insert in ie
1360 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001361static u16 iwl4965_supported_rate_to_ie(u8 *ie, u16 supported_rate,
Tomas Winklerc7c46672007-10-18 02:04:15 +02001362 u16 basic_rate, int *left)
Zhu Yib481de92007-09-25 17:54:57 -07001363{
1364 u16 ret_rates = 0, bit;
1365 int i;
Tomas Winklerc7c46672007-10-18 02:04:15 +02001366 u8 *cnt = ie;
1367 u8 *rates = ie + 1;
Zhu Yib481de92007-09-25 17:54:57 -07001368
1369 for (bit = 1, i = 0; i < IWL_RATE_COUNT; i++, bit <<= 1) {
1370 if (bit & supported_rate) {
1371 ret_rates |= bit;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001372 rates[*cnt] = iwl4965_rates[i].ieee |
Tomas Winklerc7c46672007-10-18 02:04:15 +02001373 ((bit & basic_rate) ? 0x80 : 0x00);
1374 (*cnt)++;
1375 (*left)--;
1376 if ((*left <= 0) ||
1377 (*cnt >= IWL_SUPPORTED_RATES_IE_LEN))
Zhu Yib481de92007-09-25 17:54:57 -07001378 break;
1379 }
1380 }
1381
1382 return ret_rates;
1383}
1384
Zhu Yib481de92007-09-25 17:54:57 -07001385/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001386 * iwl4965_fill_probe_req - fill in all required fields and IE for probe request
Zhu Yib481de92007-09-25 17:54:57 -07001387 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001388static u16 iwl4965_fill_probe_req(struct iwl_priv *priv,
Tomas Winkler78330fd2008-02-06 02:37:18 +02001389 enum ieee80211_band band,
1390 struct ieee80211_mgmt *frame,
1391 int left, int is_direct)
Zhu Yib481de92007-09-25 17:54:57 -07001392{
1393 int len = 0;
1394 u8 *pos = NULL;
mabbasbee488d2007-10-25 17:15:42 +08001395 u16 active_rates, ret_rates, cck_rates, active_rate_basic;
Ron Rindjunsky8fb88032007-11-26 16:14:38 +02001396#ifdef CONFIG_IWL4965_HT
Tomas Winkler78330fd2008-02-06 02:37:18 +02001397 const struct ieee80211_supported_band *sband =
1398 iwl4965_get_hw_mode(priv, band);
Ron Rindjunsky8fb88032007-11-26 16:14:38 +02001399#endif /* CONFIG_IWL4965_HT */
Zhu Yib481de92007-09-25 17:54:57 -07001400
1401 /* Make sure there is enough space for the probe request,
1402 * two mandatory IEs and the data */
1403 left -= 24;
1404 if (left < 0)
1405 return 0;
1406 len += 24;
1407
1408 frame->frame_control = cpu_to_le16(IEEE80211_STYPE_PROBE_REQ);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001409 memcpy(frame->da, iwl4965_broadcast_addr, ETH_ALEN);
Zhu Yib481de92007-09-25 17:54:57 -07001410 memcpy(frame->sa, priv->mac_addr, ETH_ALEN);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001411 memcpy(frame->bssid, iwl4965_broadcast_addr, ETH_ALEN);
Zhu Yib481de92007-09-25 17:54:57 -07001412 frame->seq_ctrl = 0;
1413
1414 /* fill in our indirect SSID IE */
1415 /* ...next IE... */
1416
1417 left -= 2;
1418 if (left < 0)
1419 return 0;
1420 len += 2;
1421 pos = &(frame->u.probe_req.variable[0]);
1422 *pos++ = WLAN_EID_SSID;
1423 *pos++ = 0;
1424
1425 /* fill in our direct SSID IE... */
1426 if (is_direct) {
1427 /* ...next IE... */
1428 left -= 2 + priv->essid_len;
1429 if (left < 0)
1430 return 0;
1431 /* ... fill it in... */
1432 *pos++ = WLAN_EID_SSID;
1433 *pos++ = priv->essid_len;
1434 memcpy(pos, priv->essid, priv->essid_len);
1435 pos += priv->essid_len;
1436 len += 2 + priv->essid_len;
1437 }
1438
1439 /* fill in supported rate */
1440 /* ...next IE... */
1441 left -= 2;
1442 if (left < 0)
1443 return 0;
Tomas Winklerc7c46672007-10-18 02:04:15 +02001444
Zhu Yib481de92007-09-25 17:54:57 -07001445 /* ... fill it in... */
1446 *pos++ = WLAN_EID_SUPP_RATES;
1447 *pos = 0;
Tomas Winklerc7c46672007-10-18 02:04:15 +02001448
mabbasbee488d2007-10-25 17:15:42 +08001449 /* exclude 60M rate */
1450 active_rates = priv->rates_mask;
1451 active_rates &= ~IWL_RATE_60M_MASK;
1452
1453 active_rate_basic = active_rates & IWL_BASIC_RATES_MASK;
Zhu Yib481de92007-09-25 17:54:57 -07001454
Tomas Winklerc7c46672007-10-18 02:04:15 +02001455 cck_rates = IWL_CCK_RATES_MASK & active_rates;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001456 ret_rates = iwl4965_supported_rate_to_ie(pos, cck_rates,
mabbasbee488d2007-10-25 17:15:42 +08001457 active_rate_basic, &left);
Tomas Winklerc7c46672007-10-18 02:04:15 +02001458 active_rates &= ~ret_rates;
1459
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001460 ret_rates = iwl4965_supported_rate_to_ie(pos, active_rates,
mabbasbee488d2007-10-25 17:15:42 +08001461 active_rate_basic, &left);
Tomas Winklerc7c46672007-10-18 02:04:15 +02001462 active_rates &= ~ret_rates;
1463
Zhu Yib481de92007-09-25 17:54:57 -07001464 len += 2 + *pos;
1465 pos += (*pos) + 1;
Tomas Winklerc7c46672007-10-18 02:04:15 +02001466 if (active_rates == 0)
Zhu Yib481de92007-09-25 17:54:57 -07001467 goto fill_end;
1468
1469 /* fill in supported extended rate */
1470 /* ...next IE... */
1471 left -= 2;
1472 if (left < 0)
1473 return 0;
1474 /* ... fill it in... */
1475 *pos++ = WLAN_EID_EXT_SUPP_RATES;
1476 *pos = 0;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001477 iwl4965_supported_rate_to_ie(pos, active_rates,
mabbasbee488d2007-10-25 17:15:42 +08001478 active_rate_basic, &left);
Zhu Yib481de92007-09-25 17:54:57 -07001479 if (*pos > 0)
1480 len += 2 + *pos;
1481
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08001482#ifdef CONFIG_IWL4965_HT
Tomas Winkler78330fd2008-02-06 02:37:18 +02001483 if (sband && sband->ht_info.ht_supported) {
1484 struct ieee80211_ht_cap *ht_cap;
Zhu Yib481de92007-09-25 17:54:57 -07001485 pos += (*pos) + 1;
1486 *pos++ = WLAN_EID_HT_CAPABILITY;
Ron Rindjunsky8fb88032007-11-26 16:14:38 +02001487 *pos++ = sizeof(struct ieee80211_ht_cap);
Tomas Winkler78330fd2008-02-06 02:37:18 +02001488 ht_cap = (struct ieee80211_ht_cap *)pos;
1489 ht_cap->cap_info = cpu_to_le16(sband->ht_info.cap);
1490 memcpy(ht_cap->supp_mcs_set, sband->ht_info.supp_mcs_set, 16);
1491 ht_cap->ampdu_params_info =(sband->ht_info.ampdu_factor &
1492 IEEE80211_HT_CAP_AMPDU_FACTOR) |
1493 ((sband->ht_info.ampdu_density << 2) &
1494 IEEE80211_HT_CAP_AMPDU_DENSITY);
Ron Rindjunsky8fb88032007-11-26 16:14:38 +02001495 len += 2 + sizeof(struct ieee80211_ht_cap);
Zhu Yib481de92007-09-25 17:54:57 -07001496 }
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08001497#endif /*CONFIG_IWL4965_HT */
Zhu Yib481de92007-09-25 17:54:57 -07001498
1499 fill_end:
1500 return (u16)len;
1501}
1502
1503/*
1504 * QoS support
1505*/
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001506static int iwl4965_send_qos_params_command(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001507 struct iwl4965_qosparam_cmd *qos)
Zhu Yib481de92007-09-25 17:54:57 -07001508{
1509
Tomas Winkler857485c2008-03-21 13:53:44 -07001510 return iwl_send_cmd_pdu(priv, REPLY_QOS_PARAM,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001511 sizeof(struct iwl4965_qosparam_cmd), qos);
Zhu Yib481de92007-09-25 17:54:57 -07001512}
1513
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001514static void iwl4965_activate_qos(struct iwl_priv *priv, u8 force)
Zhu Yib481de92007-09-25 17:54:57 -07001515{
1516 unsigned long flags;
1517
Zhu Yib481de92007-09-25 17:54:57 -07001518 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
1519 return;
1520
1521 if (!priv->qos_data.qos_enable)
1522 return;
1523
1524 spin_lock_irqsave(&priv->lock, flags);
1525 priv->qos_data.def_qos_parm.qos_flags = 0;
1526
1527 if (priv->qos_data.qos_cap.q_AP.queue_request &&
1528 !priv->qos_data.qos_cap.q_AP.txop_request)
1529 priv->qos_data.def_qos_parm.qos_flags |=
1530 QOS_PARAM_FLG_TXOP_TYPE_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07001531 if (priv->qos_data.qos_active)
1532 priv->qos_data.def_qos_parm.qos_flags |=
1533 QOS_PARAM_FLG_UPDATE_EDCA_MSK;
1534
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08001535#ifdef CONFIG_IWL4965_HT
Ron Rindjunskyfd105e72007-11-26 16:14:39 +02001536 if (priv->current_ht_config.is_ht)
Tomas Winklerf1f1f5c2007-10-25 17:15:26 +08001537 priv->qos_data.def_qos_parm.qos_flags |= QOS_PARAM_FLG_TGN_MSK;
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08001538#endif /* CONFIG_IWL4965_HT */
Tomas Winklerf1f1f5c2007-10-25 17:15:26 +08001539
Zhu Yib481de92007-09-25 17:54:57 -07001540 spin_unlock_irqrestore(&priv->lock, flags);
1541
Tomas Winkler3109ece2008-03-28 16:33:35 -07001542 if (force || iwl_is_associated(priv)) {
Tomas Winklerf1f1f5c2007-10-25 17:15:26 +08001543 IWL_DEBUG_QOS("send QoS cmd with Qos active=%d FLAGS=0x%X\n",
1544 priv->qos_data.qos_active,
1545 priv->qos_data.def_qos_parm.qos_flags);
Zhu Yib481de92007-09-25 17:54:57 -07001546
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001547 iwl4965_send_qos_params_command(priv,
Zhu Yib481de92007-09-25 17:54:57 -07001548 &(priv->qos_data.def_qos_parm));
1549 }
1550}
1551
Zhu Yib481de92007-09-25 17:54:57 -07001552/*
1553 * Power management (not Tx power!) functions
1554 */
1555#define MSEC_TO_USEC 1024
1556
1557#define NOSLP __constant_cpu_to_le16(0), 0, 0
1558#define SLP IWL_POWER_DRIVER_ALLOW_SLEEP_MSK, 0, 0
1559#define SLP_TIMEOUT(T) __constant_cpu_to_le32((T) * MSEC_TO_USEC)
1560#define SLP_VEC(X0, X1, X2, X3, X4) {__constant_cpu_to_le32(X0), \
1561 __constant_cpu_to_le32(X1), \
1562 __constant_cpu_to_le32(X2), \
1563 __constant_cpu_to_le32(X3), \
1564 __constant_cpu_to_le32(X4)}
1565
1566
1567/* default power management (not Tx power) table values */
1568/* for tim 0-10 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001569static struct iwl4965_power_vec_entry range_0[IWL_POWER_AC] = {
Zhu Yib481de92007-09-25 17:54:57 -07001570 {{NOSLP, SLP_TIMEOUT(0), SLP_TIMEOUT(0), SLP_VEC(0, 0, 0, 0, 0)}, 0},
1571 {{SLP, SLP_TIMEOUT(200), SLP_TIMEOUT(500), SLP_VEC(1, 2, 3, 4, 4)}, 0},
1572 {{SLP, SLP_TIMEOUT(200), SLP_TIMEOUT(300), SLP_VEC(2, 4, 6, 7, 7)}, 0},
1573 {{SLP, SLP_TIMEOUT(50), SLP_TIMEOUT(100), SLP_VEC(2, 6, 9, 9, 10)}, 0},
1574 {{SLP, SLP_TIMEOUT(50), SLP_TIMEOUT(25), SLP_VEC(2, 7, 9, 9, 10)}, 1},
1575 {{SLP, SLP_TIMEOUT(25), SLP_TIMEOUT(25), SLP_VEC(4, 7, 10, 10, 10)}, 1}
1576};
1577
1578/* for tim > 10 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001579static struct iwl4965_power_vec_entry range_1[IWL_POWER_AC] = {
Zhu Yib481de92007-09-25 17:54:57 -07001580 {{NOSLP, SLP_TIMEOUT(0), SLP_TIMEOUT(0), SLP_VEC(0, 0, 0, 0, 0)}, 0},
1581 {{SLP, SLP_TIMEOUT(200), SLP_TIMEOUT(500),
1582 SLP_VEC(1, 2, 3, 4, 0xFF)}, 0},
1583 {{SLP, SLP_TIMEOUT(200), SLP_TIMEOUT(300),
1584 SLP_VEC(2, 4, 6, 7, 0xFF)}, 0},
1585 {{SLP, SLP_TIMEOUT(50), SLP_TIMEOUT(100),
1586 SLP_VEC(2, 6, 9, 9, 0xFF)}, 0},
1587 {{SLP, SLP_TIMEOUT(50), SLP_TIMEOUT(25), SLP_VEC(2, 7, 9, 9, 0xFF)}, 0},
1588 {{SLP, SLP_TIMEOUT(25), SLP_TIMEOUT(25),
1589 SLP_VEC(4, 7, 10, 10, 0xFF)}, 0}
1590};
1591
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001592int iwl4965_power_init_handle(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001593{
1594 int rc = 0, i;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001595 struct iwl4965_power_mgr *pow_data;
1596 int size = sizeof(struct iwl4965_power_vec_entry) * IWL_POWER_AC;
Zhu Yib481de92007-09-25 17:54:57 -07001597 u16 pci_pm;
1598
1599 IWL_DEBUG_POWER("Initialize power \n");
1600
1601 pow_data = &(priv->power_data);
1602
1603 memset(pow_data, 0, sizeof(*pow_data));
1604
1605 pow_data->active_index = IWL_POWER_RANGE_0;
1606 pow_data->dtim_val = 0xffff;
1607
1608 memcpy(&pow_data->pwr_range_0[0], &range_0[0], size);
1609 memcpy(&pow_data->pwr_range_1[0], &range_1[0], size);
1610
1611 rc = pci_read_config_word(priv->pci_dev, PCI_LINK_CTRL, &pci_pm);
1612 if (rc != 0)
1613 return 0;
1614 else {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001615 struct iwl4965_powertable_cmd *cmd;
Zhu Yib481de92007-09-25 17:54:57 -07001616
1617 IWL_DEBUG_POWER("adjust power command flags\n");
1618
1619 for (i = 0; i < IWL_POWER_AC; i++) {
1620 cmd = &pow_data->pwr_range_0[i].cmd;
1621
1622 if (pci_pm & 0x1)
1623 cmd->flags &= ~IWL_POWER_PCI_PM_MSK;
1624 else
1625 cmd->flags |= IWL_POWER_PCI_PM_MSK;
1626 }
1627 }
1628 return rc;
1629}
1630
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001631static int iwl4965_update_power_cmd(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001632 struct iwl4965_powertable_cmd *cmd, u32 mode)
Zhu Yib481de92007-09-25 17:54:57 -07001633{
1634 int rc = 0, i;
1635 u8 skip;
1636 u32 max_sleep = 0;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001637 struct iwl4965_power_vec_entry *range;
Zhu Yib481de92007-09-25 17:54:57 -07001638 u8 period = 0;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001639 struct iwl4965_power_mgr *pow_data;
Zhu Yib481de92007-09-25 17:54:57 -07001640
1641 if (mode > IWL_POWER_INDEX_5) {
1642 IWL_DEBUG_POWER("Error invalid power mode \n");
1643 return -1;
1644 }
1645 pow_data = &(priv->power_data);
1646
1647 if (pow_data->active_index == IWL_POWER_RANGE_0)
1648 range = &pow_data->pwr_range_0[0];
1649 else
1650 range = &pow_data->pwr_range_1[1];
1651
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001652 memcpy(cmd, &range[mode].cmd, sizeof(struct iwl4965_powertable_cmd));
Zhu Yib481de92007-09-25 17:54:57 -07001653
1654#ifdef IWL_MAC80211_DISABLE
1655 if (priv->assoc_network != NULL) {
1656 unsigned long flags;
1657
1658 period = priv->assoc_network->tim.tim_period;
1659 }
1660#endif /*IWL_MAC80211_DISABLE */
1661 skip = range[mode].no_dtim;
1662
1663 if (period == 0) {
1664 period = 1;
1665 skip = 0;
1666 }
1667
1668 if (skip == 0) {
1669 max_sleep = period;
1670 cmd->flags &= ~IWL_POWER_SLEEP_OVER_DTIM_MSK;
1671 } else {
1672 __le32 slp_itrvl = cmd->sleep_interval[IWL_POWER_VEC_SIZE - 1];
1673 max_sleep = (le32_to_cpu(slp_itrvl) / period) * period;
1674 cmd->flags |= IWL_POWER_SLEEP_OVER_DTIM_MSK;
1675 }
1676
1677 for (i = 0; i < IWL_POWER_VEC_SIZE; i++) {
1678 if (le32_to_cpu(cmd->sleep_interval[i]) > max_sleep)
1679 cmd->sleep_interval[i] = cpu_to_le32(max_sleep);
1680 }
1681
1682 IWL_DEBUG_POWER("Flags value = 0x%08X\n", cmd->flags);
1683 IWL_DEBUG_POWER("Tx timeout = %u\n", le32_to_cpu(cmd->tx_data_timeout));
1684 IWL_DEBUG_POWER("Rx timeout = %u\n", le32_to_cpu(cmd->rx_data_timeout));
1685 IWL_DEBUG_POWER("Sleep interval vector = { %d , %d , %d , %d , %d }\n",
1686 le32_to_cpu(cmd->sleep_interval[0]),
1687 le32_to_cpu(cmd->sleep_interval[1]),
1688 le32_to_cpu(cmd->sleep_interval[2]),
1689 le32_to_cpu(cmd->sleep_interval[3]),
1690 le32_to_cpu(cmd->sleep_interval[4]));
1691
1692 return rc;
1693}
1694
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001695static int iwl4965_send_power_mode(struct iwl_priv *priv, u32 mode)
Zhu Yib481de92007-09-25 17:54:57 -07001696{
John W. Linville9a62f732007-11-15 16:27:36 -05001697 u32 uninitialized_var(final_mode);
Zhu Yib481de92007-09-25 17:54:57 -07001698 int rc;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001699 struct iwl4965_powertable_cmd cmd;
Zhu Yib481de92007-09-25 17:54:57 -07001700
1701 /* If on battery, set to 3,
Ian Schram01ebd062007-10-25 17:15:22 +08001702 * if plugged into AC power, set to CAM ("continuously aware mode"),
Zhu Yib481de92007-09-25 17:54:57 -07001703 * else user level */
1704 switch (mode) {
1705 case IWL_POWER_BATTERY:
1706 final_mode = IWL_POWER_INDEX_3;
1707 break;
1708 case IWL_POWER_AC:
1709 final_mode = IWL_POWER_MODE_CAM;
1710 break;
1711 default:
1712 final_mode = mode;
1713 break;
1714 }
1715
1716 cmd.keep_alive_beacons = 0;
1717
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001718 iwl4965_update_power_cmd(priv, &cmd, final_mode);
Zhu Yib481de92007-09-25 17:54:57 -07001719
Tomas Winkler857485c2008-03-21 13:53:44 -07001720 rc = iwl_send_cmd_pdu(priv, POWER_TABLE_CMD, sizeof(cmd), &cmd);
Zhu Yib481de92007-09-25 17:54:57 -07001721
1722 if (final_mode == IWL_POWER_MODE_CAM)
1723 clear_bit(STATUS_POWER_PMI, &priv->status);
1724 else
1725 set_bit(STATUS_POWER_PMI, &priv->status);
1726
1727 return rc;
1728}
1729
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001730int iwl4965_is_network_packet(struct iwl_priv *priv, struct ieee80211_hdr *header)
Zhu Yib481de92007-09-25 17:54:57 -07001731{
1732 /* Filter incoming packets to determine if they are targeted toward
1733 * this network, discarding packets coming from ourselves */
1734 switch (priv->iw_mode) {
1735 case IEEE80211_IF_TYPE_IBSS: /* Header: Dest. | Source | BSSID */
1736 /* packets from our adapter are dropped (echo) */
1737 if (!compare_ether_addr(header->addr2, priv->mac_addr))
1738 return 0;
1739 /* {broad,multi}cast packets to our IBSS go through */
1740 if (is_multicast_ether_addr(header->addr1))
1741 return !compare_ether_addr(header->addr3, priv->bssid);
1742 /* packets to our adapter go through */
1743 return !compare_ether_addr(header->addr1, priv->mac_addr);
1744 case IEEE80211_IF_TYPE_STA: /* Header: Dest. | AP{BSSID} | Source */
1745 /* packets from our adapter are dropped (echo) */
1746 if (!compare_ether_addr(header->addr3, priv->mac_addr))
1747 return 0;
1748 /* {broad,multi}cast packets to our BSS go through */
1749 if (is_multicast_ether_addr(header->addr1))
1750 return !compare_ether_addr(header->addr2, priv->bssid);
1751 /* packets to our adapter go through */
1752 return !compare_ether_addr(header->addr1, priv->mac_addr);
Tomas Winkler69dc5d92008-03-25 16:33:41 -07001753 default:
1754 break;
Zhu Yib481de92007-09-25 17:54:57 -07001755 }
1756
1757 return 1;
1758}
1759
1760#define TX_STATUS_ENTRY(x) case TX_STATUS_FAIL_ ## x: return #x
1761
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001762static const char *iwl4965_get_tx_fail_reason(u32 status)
Zhu Yib481de92007-09-25 17:54:57 -07001763{
1764 switch (status & TX_STATUS_MSK) {
1765 case TX_STATUS_SUCCESS:
1766 return "SUCCESS";
1767 TX_STATUS_ENTRY(SHORT_LIMIT);
1768 TX_STATUS_ENTRY(LONG_LIMIT);
1769 TX_STATUS_ENTRY(FIFO_UNDERRUN);
1770 TX_STATUS_ENTRY(MGMNT_ABORT);
1771 TX_STATUS_ENTRY(NEXT_FRAG);
1772 TX_STATUS_ENTRY(LIFE_EXPIRE);
1773 TX_STATUS_ENTRY(DEST_PS);
1774 TX_STATUS_ENTRY(ABORTED);
1775 TX_STATUS_ENTRY(BT_RETRY);
1776 TX_STATUS_ENTRY(STA_INVALID);
1777 TX_STATUS_ENTRY(FRAG_DROPPED);
1778 TX_STATUS_ENTRY(TID_DISABLE);
1779 TX_STATUS_ENTRY(FRAME_FLUSHED);
1780 TX_STATUS_ENTRY(INSUFFICIENT_CF_POLL);
1781 TX_STATUS_ENTRY(TX_LOCKED);
1782 TX_STATUS_ENTRY(NO_BEACON_ON_RADAR);
1783 }
1784
1785 return "UNKNOWN";
1786}
1787
1788/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001789 * iwl4965_scan_cancel - Cancel any currently executing HW scan
Zhu Yib481de92007-09-25 17:54:57 -07001790 *
1791 * NOTE: priv->mutex is not required before calling this function
1792 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001793static int iwl4965_scan_cancel(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001794{
1795 if (!test_bit(STATUS_SCAN_HW, &priv->status)) {
1796 clear_bit(STATUS_SCANNING, &priv->status);
1797 return 0;
1798 }
1799
1800 if (test_bit(STATUS_SCANNING, &priv->status)) {
1801 if (!test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
1802 IWL_DEBUG_SCAN("Queuing scan abort.\n");
1803 set_bit(STATUS_SCAN_ABORTING, &priv->status);
1804 queue_work(priv->workqueue, &priv->abort_scan);
1805
1806 } else
1807 IWL_DEBUG_SCAN("Scan abort already in progress.\n");
1808
1809 return test_bit(STATUS_SCANNING, &priv->status);
1810 }
1811
1812 return 0;
1813}
1814
1815/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001816 * iwl4965_scan_cancel_timeout - Cancel any currently executing HW scan
Zhu Yib481de92007-09-25 17:54:57 -07001817 * @ms: amount of time to wait (in milliseconds) for scan to abort
1818 *
1819 * NOTE: priv->mutex must be held before calling this function
1820 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001821static int iwl4965_scan_cancel_timeout(struct iwl_priv *priv, unsigned long ms)
Zhu Yib481de92007-09-25 17:54:57 -07001822{
1823 unsigned long now = jiffies;
1824 int ret;
1825
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001826 ret = iwl4965_scan_cancel(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001827 if (ret && ms) {
1828 mutex_unlock(&priv->mutex);
1829 while (!time_after(jiffies, now + msecs_to_jiffies(ms)) &&
1830 test_bit(STATUS_SCANNING, &priv->status))
1831 msleep(1);
1832 mutex_lock(&priv->mutex);
1833
1834 return test_bit(STATUS_SCANNING, &priv->status);
1835 }
1836
1837 return ret;
1838}
1839
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001840static void iwl4965_sequence_reset(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001841{
1842 /* Reset ieee stats */
1843
1844 /* We don't reset the net_device_stats (ieee->stats) on
1845 * re-association */
1846
1847 priv->last_seq_num = -1;
1848 priv->last_frag_num = -1;
1849 priv->last_packet_time = 0;
1850
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001851 iwl4965_scan_cancel(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001852}
1853
1854#define MAX_UCODE_BEACON_INTERVAL 4096
1855#define INTEL_CONN_LISTEN_INTERVAL __constant_cpu_to_le16(0xA)
1856
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001857static __le16 iwl4965_adjust_beacon_interval(u16 beacon_val)
Zhu Yib481de92007-09-25 17:54:57 -07001858{
1859 u16 new_val = 0;
1860 u16 beacon_factor = 0;
1861
1862 beacon_factor =
1863 (beacon_val + MAX_UCODE_BEACON_INTERVAL)
1864 / MAX_UCODE_BEACON_INTERVAL;
1865 new_val = beacon_val / beacon_factor;
1866
1867 return cpu_to_le16(new_val);
1868}
1869
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001870static void iwl4965_setup_rxon_timing(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001871{
1872 u64 interval_tm_unit;
1873 u64 tsf, result;
1874 unsigned long flags;
1875 struct ieee80211_conf *conf = NULL;
1876 u16 beacon_int = 0;
1877
1878 conf = ieee80211_get_hw_conf(priv->hw);
1879
1880 spin_lock_irqsave(&priv->lock, flags);
Tomas Winkler3109ece2008-03-28 16:33:35 -07001881 priv->rxon_timing.timestamp.dw[1] = cpu_to_le32(priv->timestamp >> 32);
1882 priv->rxon_timing.timestamp.dw[0] =
1883 cpu_to_le32(priv->timestamp & 0xFFFFFFFF);
Zhu Yib481de92007-09-25 17:54:57 -07001884
1885 priv->rxon_timing.listen_interval = INTEL_CONN_LISTEN_INTERVAL;
1886
Tomas Winkler3109ece2008-03-28 16:33:35 -07001887 tsf = priv->timestamp;
Zhu Yib481de92007-09-25 17:54:57 -07001888
1889 beacon_int = priv->beacon_int;
1890 spin_unlock_irqrestore(&priv->lock, flags);
1891
1892 if (priv->iw_mode == IEEE80211_IF_TYPE_STA) {
1893 if (beacon_int == 0) {
1894 priv->rxon_timing.beacon_interval = cpu_to_le16(100);
1895 priv->rxon_timing.beacon_init_val = cpu_to_le32(102400);
1896 } else {
1897 priv->rxon_timing.beacon_interval =
1898 cpu_to_le16(beacon_int);
1899 priv->rxon_timing.beacon_interval =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001900 iwl4965_adjust_beacon_interval(
Zhu Yib481de92007-09-25 17:54:57 -07001901 le16_to_cpu(priv->rxon_timing.beacon_interval));
1902 }
1903
1904 priv->rxon_timing.atim_window = 0;
1905 } else {
1906 priv->rxon_timing.beacon_interval =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001907 iwl4965_adjust_beacon_interval(conf->beacon_int);
Zhu Yib481de92007-09-25 17:54:57 -07001908 /* TODO: we need to get atim_window from upper stack
1909 * for now we set to 0 */
1910 priv->rxon_timing.atim_window = 0;
1911 }
1912
1913 interval_tm_unit =
1914 (le16_to_cpu(priv->rxon_timing.beacon_interval) * 1024);
1915 result = do_div(tsf, interval_tm_unit);
1916 priv->rxon_timing.beacon_init_val =
1917 cpu_to_le32((u32) ((u64) interval_tm_unit - result));
1918
1919 IWL_DEBUG_ASSOC
1920 ("beacon interval %d beacon timer %d beacon tim %d\n",
1921 le16_to_cpu(priv->rxon_timing.beacon_interval),
1922 le32_to_cpu(priv->rxon_timing.beacon_init_val),
1923 le16_to_cpu(priv->rxon_timing.atim_window));
1924}
1925
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001926static int iwl4965_scan_initiate(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001927{
1928 if (priv->iw_mode == IEEE80211_IF_TYPE_AP) {
1929 IWL_ERROR("APs don't scan.\n");
1930 return 0;
1931 }
1932
Tomas Winklerfee12472008-04-03 16:05:21 -07001933 if (!iwl_is_ready_rf(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07001934 IWL_DEBUG_SCAN("Aborting scan due to not ready.\n");
1935 return -EIO;
1936 }
1937
1938 if (test_bit(STATUS_SCANNING, &priv->status)) {
1939 IWL_DEBUG_SCAN("Scan already in progress.\n");
1940 return -EAGAIN;
1941 }
1942
1943 if (test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
1944 IWL_DEBUG_SCAN("Scan request while abort pending. "
1945 "Queuing.\n");
1946 return -EAGAIN;
1947 }
1948
1949 IWL_DEBUG_INFO("Starting scan...\n");
1950 priv->scan_bands = 2;
1951 set_bit(STATUS_SCANNING, &priv->status);
1952 priv->scan_start = jiffies;
1953 priv->scan_pass_start = priv->scan_start;
1954
1955 queue_work(priv->workqueue, &priv->request_scan);
1956
1957 return 0;
1958}
1959
Zhu Yib481de92007-09-25 17:54:57 -07001960
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001961static void iwl4965_set_flags_for_phymode(struct iwl_priv *priv,
Johannes Berg8318d782008-01-24 19:38:38 +01001962 enum ieee80211_band band)
Zhu Yib481de92007-09-25 17:54:57 -07001963{
Johannes Berg8318d782008-01-24 19:38:38 +01001964 if (band == IEEE80211_BAND_5GHZ) {
Zhu Yib481de92007-09-25 17:54:57 -07001965 priv->staging_rxon.flags &=
1966 ~(RXON_FLG_BAND_24G_MSK | RXON_FLG_AUTO_DETECT_MSK
1967 | RXON_FLG_CCK_MSK);
1968 priv->staging_rxon.flags |= RXON_FLG_SHORT_SLOT_MSK;
1969 } else {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001970 /* Copied from iwl4965_bg_post_associate() */
Zhu Yib481de92007-09-25 17:54:57 -07001971 if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_SLOT_TIME)
1972 priv->staging_rxon.flags |= RXON_FLG_SHORT_SLOT_MSK;
1973 else
1974 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
1975
1976 if (priv->iw_mode == IEEE80211_IF_TYPE_IBSS)
1977 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
1978
1979 priv->staging_rxon.flags |= RXON_FLG_BAND_24G_MSK;
1980 priv->staging_rxon.flags |= RXON_FLG_AUTO_DETECT_MSK;
1981 priv->staging_rxon.flags &= ~RXON_FLG_CCK_MSK;
1982 }
1983}
1984
1985/*
Ian Schram01ebd062007-10-25 17:15:22 +08001986 * initialize rxon structure with default values from eeprom
Zhu Yib481de92007-09-25 17:54:57 -07001987 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001988static void iwl4965_connection_init_rx_config(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001989{
Assaf Kraussbf85ea42008-03-14 10:38:49 -07001990 const struct iwl_channel_info *ch_info;
Zhu Yib481de92007-09-25 17:54:57 -07001991
1992 memset(&priv->staging_rxon, 0, sizeof(priv->staging_rxon));
1993
1994 switch (priv->iw_mode) {
1995 case IEEE80211_IF_TYPE_AP:
1996 priv->staging_rxon.dev_type = RXON_DEV_TYPE_AP;
1997 break;
1998
1999 case IEEE80211_IF_TYPE_STA:
2000 priv->staging_rxon.dev_type = RXON_DEV_TYPE_ESS;
2001 priv->staging_rxon.filter_flags = RXON_FILTER_ACCEPT_GRP_MSK;
2002 break;
2003
2004 case IEEE80211_IF_TYPE_IBSS:
2005 priv->staging_rxon.dev_type = RXON_DEV_TYPE_IBSS;
2006 priv->staging_rxon.flags = RXON_FLG_SHORT_PREAMBLE_MSK;
2007 priv->staging_rxon.filter_flags = RXON_FILTER_BCON_AWARE_MSK |
2008 RXON_FILTER_ACCEPT_GRP_MSK;
2009 break;
2010
2011 case IEEE80211_IF_TYPE_MNTR:
2012 priv->staging_rxon.dev_type = RXON_DEV_TYPE_SNIFFER;
2013 priv->staging_rxon.filter_flags = RXON_FILTER_PROMISC_MSK |
2014 RXON_FILTER_CTL2HOST_MSK | RXON_FILTER_ACCEPT_GRP_MSK;
2015 break;
Tomas Winkler69dc5d92008-03-25 16:33:41 -07002016 default:
2017 IWL_ERROR("Unsupported interface type %d\n", priv->iw_mode);
2018 break;
Zhu Yib481de92007-09-25 17:54:57 -07002019 }
2020
2021#if 0
2022 /* TODO: Figure out when short_preamble would be set and cache from
2023 * that */
2024 if (!hw_to_local(priv->hw)->short_preamble)
2025 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_PREAMBLE_MSK;
2026 else
2027 priv->staging_rxon.flags |= RXON_FLG_SHORT_PREAMBLE_MSK;
2028#endif
2029
Assaf Krauss8622e702008-03-21 13:53:43 -07002030 ch_info = iwl_get_channel_info(priv, priv->band,
Zhu Yib481de92007-09-25 17:54:57 -07002031 le16_to_cpu(priv->staging_rxon.channel));
2032
2033 if (!ch_info)
2034 ch_info = &priv->channel_info[0];
2035
2036 /*
2037 * in some case A channels are all non IBSS
2038 * in this case force B/G channel
2039 */
2040 if ((priv->iw_mode == IEEE80211_IF_TYPE_IBSS) &&
2041 !(is_channel_ibss(ch_info)))
2042 ch_info = &priv->channel_info[0];
2043
2044 priv->staging_rxon.channel = cpu_to_le16(ch_info->channel);
Johannes Berg8318d782008-01-24 19:38:38 +01002045 priv->band = ch_info->band;
Zhu Yib481de92007-09-25 17:54:57 -07002046
Johannes Berg8318d782008-01-24 19:38:38 +01002047 iwl4965_set_flags_for_phymode(priv, priv->band);
Zhu Yib481de92007-09-25 17:54:57 -07002048
2049 priv->staging_rxon.ofdm_basic_rates =
2050 (IWL_OFDM_RATES_MASK >> IWL_FIRST_OFDM_RATE) & 0xFF;
2051 priv->staging_rxon.cck_basic_rates =
2052 (IWL_CCK_RATES_MASK >> IWL_FIRST_CCK_RATE) & 0xF;
2053
2054 priv->staging_rxon.flags &= ~(RXON_FLG_CHANNEL_MODE_MIXED_MSK |
2055 RXON_FLG_CHANNEL_MODE_PURE_40_MSK);
2056 memcpy(priv->staging_rxon.node_addr, priv->mac_addr, ETH_ALEN);
2057 memcpy(priv->staging_rxon.wlap_bssid_addr, priv->mac_addr, ETH_ALEN);
2058 priv->staging_rxon.ofdm_ht_single_stream_basic_rates = 0xff;
2059 priv->staging_rxon.ofdm_ht_dual_stream_basic_rates = 0xff;
2060 iwl4965_set_rxon_chain(priv);
2061}
2062
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002063static int iwl4965_set_mode(struct iwl_priv *priv, int mode)
Zhu Yib481de92007-09-25 17:54:57 -07002064{
Zhu Yib481de92007-09-25 17:54:57 -07002065 if (mode == IEEE80211_IF_TYPE_IBSS) {
Assaf Kraussbf85ea42008-03-14 10:38:49 -07002066 const struct iwl_channel_info *ch_info;
Zhu Yib481de92007-09-25 17:54:57 -07002067
Assaf Krauss8622e702008-03-21 13:53:43 -07002068 ch_info = iwl_get_channel_info(priv,
Johannes Berg8318d782008-01-24 19:38:38 +01002069 priv->band,
Zhu Yib481de92007-09-25 17:54:57 -07002070 le16_to_cpu(priv->staging_rxon.channel));
2071
2072 if (!ch_info || !is_channel_ibss(ch_info)) {
2073 IWL_ERROR("channel %d not IBSS channel\n",
2074 le16_to_cpu(priv->staging_rxon.channel));
2075 return -EINVAL;
2076 }
2077 }
2078
Zhu Yib481de92007-09-25 17:54:57 -07002079 priv->iw_mode = mode;
2080
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002081 iwl4965_connection_init_rx_config(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002082 memcpy(priv->staging_rxon.node_addr, priv->mac_addr, ETH_ALEN);
2083
Assaf Kraussbf85ea42008-03-14 10:38:49 -07002084 iwlcore_clear_stations_table(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002085
Mohamed Abbasfde35712007-11-29 11:10:15 +08002086 /* dont commit rxon if rf-kill is on*/
Tomas Winklerfee12472008-04-03 16:05:21 -07002087 if (!iwl_is_ready_rf(priv))
Mohamed Abbasfde35712007-11-29 11:10:15 +08002088 return -EAGAIN;
2089
2090 cancel_delayed_work(&priv->scan_check);
2091 if (iwl4965_scan_cancel_timeout(priv, 100)) {
2092 IWL_WARNING("Aborted scan still in progress after 100ms\n");
2093 IWL_DEBUG_MAC80211("leaving - scan abort failed.\n");
2094 return -EAGAIN;
2095 }
2096
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002097 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002098
2099 return 0;
2100}
2101
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002102static void iwl4965_build_tx_cmd_hwcrypto(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07002103 struct ieee80211_tx_control *ctl,
Tomas Winkler857485c2008-03-21 13:53:44 -07002104 struct iwl_cmd *cmd,
Zhu Yib481de92007-09-25 17:54:57 -07002105 struct sk_buff *skb_frag,
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07002106 int sta_id)
Zhu Yib481de92007-09-25 17:54:57 -07002107{
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07002108 struct iwl4965_hw_key *keyinfo = &priv->stations[sta_id].keyinfo;
Emmanuel Grumbach6974e362008-04-14 21:16:06 -07002109 struct iwl_wep_key *wepkey;
2110 int keyidx = 0;
2111
2112 BUG_ON(ctl->key_idx > 3);
Zhu Yib481de92007-09-25 17:54:57 -07002113
2114 switch (keyinfo->alg) {
2115 case ALG_CCMP:
2116 cmd->cmd.tx.sec_ctl = TX_CMD_SEC_CCM;
2117 memcpy(cmd->cmd.tx.key, keyinfo->key, keyinfo->keylen);
Max Stepanov8236e182008-03-12 16:58:48 -07002118 if (ctl->flags & IEEE80211_TXCTL_AMPDU)
2119 cmd->cmd.tx.tx_flags |= TX_CMD_FLG_AGG_CCMP_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07002120 IWL_DEBUG_TX("tx_cmd with aes hwcrypto\n");
2121 break;
2122
2123 case ALG_TKIP:
Zhu Yib481de92007-09-25 17:54:57 -07002124 cmd->cmd.tx.sec_ctl = TX_CMD_SEC_TKIP;
Emmanuel Grumbach2bc75082008-03-19 16:41:45 -07002125 ieee80211_get_tkip_key(keyinfo->conf, skb_frag,
2126 IEEE80211_TKIP_P2_KEY, cmd->cmd.tx.key);
2127 IWL_DEBUG_TX("tx_cmd with tkip hwcrypto\n");
Zhu Yib481de92007-09-25 17:54:57 -07002128 break;
2129
2130 case ALG_WEP:
Emmanuel Grumbach6974e362008-04-14 21:16:06 -07002131 wepkey = &priv->wep_keys[ctl->key_idx];
2132 cmd->cmd.tx.sec_ctl = 0;
2133 if (priv->default_wep_key) {
2134 /* the WEP key was sent as static */
2135 keyidx = ctl->key_idx;
2136 memcpy(&cmd->cmd.tx.key[3], wepkey->key,
2137 wepkey->key_size);
2138 if (wepkey->key_size == WEP_KEY_LEN_128)
2139 cmd->cmd.tx.sec_ctl |= TX_CMD_SEC_KEY128;
2140 } else {
Emmanuel Grumbach0211ddd2008-04-14 21:16:07 -07002141 /* the WEP key was sent as dynamic */
2142 keyidx = keyinfo->keyidx;
2143 memcpy(&cmd->cmd.tx.key[3], keyinfo->key,
2144 keyinfo->keylen);
2145 if (keyinfo->keylen == WEP_KEY_LEN_128)
2146 cmd->cmd.tx.sec_ctl |= TX_CMD_SEC_KEY128;
Emmanuel Grumbach6974e362008-04-14 21:16:06 -07002147 }
Zhu Yib481de92007-09-25 17:54:57 -07002148
Emmanuel Grumbach6974e362008-04-14 21:16:06 -07002149 cmd->cmd.tx.sec_ctl |= (TX_CMD_SEC_WEP |
2150 (keyidx & TX_CMD_SEC_MSK) << TX_CMD_SEC_SHIFT);
Zhu Yib481de92007-09-25 17:54:57 -07002151
2152 IWL_DEBUG_TX("Configuring packet for WEP encryption "
Emmanuel Grumbach6974e362008-04-14 21:16:06 -07002153 "with key %d\n", keyidx);
Zhu Yib481de92007-09-25 17:54:57 -07002154 break;
2155
Zhu Yib481de92007-09-25 17:54:57 -07002156 default:
2157 printk(KERN_ERR "Unknown encode alg %d\n", keyinfo->alg);
2158 break;
2159 }
2160}
2161
2162/*
2163 * handle build REPLY_TX command notification.
2164 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002165static void iwl4965_build_tx_cmd_basic(struct iwl_priv *priv,
Tomas Winkler857485c2008-03-21 13:53:44 -07002166 struct iwl_cmd *cmd,
Zhu Yib481de92007-09-25 17:54:57 -07002167 struct ieee80211_tx_control *ctrl,
2168 struct ieee80211_hdr *hdr,
2169 int is_unicast, u8 std_id)
2170{
2171 __le16 *qc;
2172 u16 fc = le16_to_cpu(hdr->frame_control);
2173 __le32 tx_flags = cmd->cmd.tx.tx_flags;
2174
2175 cmd->cmd.tx.stop_time.life_time = TX_CMD_LIFE_TIME_INFINITE;
2176 if (!(ctrl->flags & IEEE80211_TXCTL_NO_ACK)) {
2177 tx_flags |= TX_CMD_FLG_ACK_MSK;
2178 if ((fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_MGMT)
2179 tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
2180 if (ieee80211_is_probe_response(fc) &&
2181 !(le16_to_cpu(hdr->seq_ctrl) & 0xf))
2182 tx_flags |= TX_CMD_FLG_TSF_MSK;
2183 } else {
2184 tx_flags &= (~TX_CMD_FLG_ACK_MSK);
2185 tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
2186 }
2187
Tomas Winkler87e4f7d2008-01-14 17:46:16 -08002188 if (ieee80211_is_back_request(fc))
2189 tx_flags |= TX_CMD_FLG_ACK_MSK | TX_CMD_FLG_IMM_BA_RSP_MASK;
2190
2191
Zhu Yib481de92007-09-25 17:54:57 -07002192 cmd->cmd.tx.sta_id = std_id;
2193 if (ieee80211_get_morefrag(hdr))
2194 tx_flags |= TX_CMD_FLG_MORE_FRAG_MSK;
2195
2196 qc = ieee80211_get_qos_ctrl(hdr);
2197 if (qc) {
2198 cmd->cmd.tx.tid_tspec = (u8) (le16_to_cpu(*qc) & 0xf);
2199 tx_flags &= ~TX_CMD_FLG_SEQ_CTL_MSK;
2200 } else
2201 tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
2202
2203 if (ctrl->flags & IEEE80211_TXCTL_USE_RTS_CTS) {
2204 tx_flags |= TX_CMD_FLG_RTS_MSK;
2205 tx_flags &= ~TX_CMD_FLG_CTS_MSK;
2206 } else if (ctrl->flags & IEEE80211_TXCTL_USE_CTS_PROTECT) {
2207 tx_flags &= ~TX_CMD_FLG_RTS_MSK;
2208 tx_flags |= TX_CMD_FLG_CTS_MSK;
2209 }
2210
2211 if ((tx_flags & TX_CMD_FLG_RTS_MSK) || (tx_flags & TX_CMD_FLG_CTS_MSK))
2212 tx_flags |= TX_CMD_FLG_FULL_TXOP_PROT_MSK;
2213
2214 tx_flags &= ~(TX_CMD_FLG_ANT_SEL_MSK);
2215 if ((fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_MGMT) {
2216 if ((fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_ASSOC_REQ ||
2217 (fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_REASSOC_REQ)
Ian Schrambc434dd2007-10-25 17:15:29 +08002218 cmd->cmd.tx.timeout.pm_frame_timeout = cpu_to_le16(3);
Zhu Yib481de92007-09-25 17:54:57 -07002219 else
Ian Schrambc434dd2007-10-25 17:15:29 +08002220 cmd->cmd.tx.timeout.pm_frame_timeout = cpu_to_le16(2);
Mohamed Abbasab53d8a2008-03-25 16:33:36 -07002221 } else {
Zhu Yib481de92007-09-25 17:54:57 -07002222 cmd->cmd.tx.timeout.pm_frame_timeout = 0;
Mohamed Abbasab53d8a2008-03-25 16:33:36 -07002223 }
Zhu Yib481de92007-09-25 17:54:57 -07002224
2225 cmd->cmd.tx.driver_txop = 0;
2226 cmd->cmd.tx.tx_flags = tx_flags;
2227 cmd->cmd.tx.next_frame_len = 0;
2228}
Tomas Winkler19758be2008-03-12 16:58:51 -07002229static void iwl_update_tx_stats(struct iwl_priv *priv, u16 fc, u16 len)
2230{
2231 /* 0 - mgmt, 1 - cnt, 2 - data */
2232 int idx = (fc & IEEE80211_FCTL_FTYPE) >> 2;
2233 priv->tx_stats[idx].cnt++;
2234 priv->tx_stats[idx].bytes += len;
2235}
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002236/**
2237 * iwl4965_get_sta_id - Find station's index within station table
2238 *
2239 * If new IBSS station, create new entry in station table
2240 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002241static int iwl4965_get_sta_id(struct iwl_priv *priv,
Ben Cahill9fbab512007-11-29 11:09:47 +08002242 struct ieee80211_hdr *hdr)
Zhu Yib481de92007-09-25 17:54:57 -07002243{
2244 int sta_id;
2245 u16 fc = le16_to_cpu(hdr->frame_control);
Joe Perches0795af52007-10-03 17:59:30 -07002246 DECLARE_MAC_BUF(mac);
Zhu Yib481de92007-09-25 17:54:57 -07002247
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002248 /* If this frame is broadcast or management, use broadcast station id */
Zhu Yib481de92007-09-25 17:54:57 -07002249 if (((fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_DATA) ||
2250 is_multicast_ether_addr(hdr->addr1))
2251 return priv->hw_setting.bcast_sta_id;
2252
2253 switch (priv->iw_mode) {
2254
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002255 /* If we are a client station in a BSS network, use the special
2256 * AP station entry (that's the only station we communicate with) */
Zhu Yib481de92007-09-25 17:54:57 -07002257 case IEEE80211_IF_TYPE_STA:
2258 return IWL_AP_ID;
2259
2260 /* If we are an AP, then find the station, or use BCAST */
2261 case IEEE80211_IF_TYPE_AP:
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002262 sta_id = iwl4965_hw_find_station(priv, hdr->addr1);
Zhu Yib481de92007-09-25 17:54:57 -07002263 if (sta_id != IWL_INVALID_STATION)
2264 return sta_id;
2265 return priv->hw_setting.bcast_sta_id;
2266
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002267 /* If this frame is going out to an IBSS network, find the station,
2268 * or create a new station table entry */
Zhu Yib481de92007-09-25 17:54:57 -07002269 case IEEE80211_IF_TYPE_IBSS:
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002270 sta_id = iwl4965_hw_find_station(priv, hdr->addr1);
Zhu Yib481de92007-09-25 17:54:57 -07002271 if (sta_id != IWL_INVALID_STATION)
2272 return sta_id;
2273
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002274 /* Create new station table entry */
Ron Rindjunsky67d62032007-11-26 16:14:40 +02002275 sta_id = iwl4965_add_station_flags(priv, hdr->addr1,
2276 0, CMD_ASYNC, NULL);
Zhu Yib481de92007-09-25 17:54:57 -07002277
2278 if (sta_id != IWL_INVALID_STATION)
2279 return sta_id;
2280
Joe Perches0795af52007-10-03 17:59:30 -07002281 IWL_DEBUG_DROP("Station %s not in station map. "
Zhu Yib481de92007-09-25 17:54:57 -07002282 "Defaulting to broadcast...\n",
Joe Perches0795af52007-10-03 17:59:30 -07002283 print_mac(mac, hdr->addr1));
Tomas Winkler0a6857e2008-03-12 16:58:49 -07002284 iwl_print_hex_dump(IWL_DL_DROP, (u8 *) hdr, sizeof(*hdr));
Zhu Yib481de92007-09-25 17:54:57 -07002285 return priv->hw_setting.bcast_sta_id;
2286
2287 default:
Ian Schram01ebd062007-10-25 17:15:22 +08002288 IWL_WARNING("Unknown mode of operation: %d", priv->iw_mode);
Zhu Yib481de92007-09-25 17:54:57 -07002289 return priv->hw_setting.bcast_sta_id;
2290 }
2291}
2292
2293/*
2294 * start REPLY_TX command process
2295 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002296static int iwl4965_tx_skb(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07002297 struct sk_buff *skb, struct ieee80211_tx_control *ctl)
2298{
2299 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002300 struct iwl4965_tfd_frame *tfd;
Zhu Yib481de92007-09-25 17:54:57 -07002301 u32 *control_flags;
2302 int txq_id = ctl->queue;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002303 struct iwl4965_tx_queue *txq = NULL;
2304 struct iwl4965_queue *q = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07002305 dma_addr_t phys_addr;
2306 dma_addr_t txcmd_phys;
Tomas Winkler87e4f7d2008-01-14 17:46:16 -08002307 dma_addr_t scratch_phys;
Tomas Winkler857485c2008-03-21 13:53:44 -07002308 struct iwl_cmd *out_cmd = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07002309 u16 len, idx, len_org;
2310 u8 id, hdr_len, unicast;
2311 u8 sta_id;
2312 u16 seq_number = 0;
2313 u16 fc;
2314 __le16 *qc;
2315 u8 wait_write_ptr = 0;
2316 unsigned long flags;
2317 int rc;
2318
2319 spin_lock_irqsave(&priv->lock, flags);
Tomas Winklerfee12472008-04-03 16:05:21 -07002320 if (iwl_is_rfkill(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07002321 IWL_DEBUG_DROP("Dropping - RF KILL\n");
2322 goto drop_unlock;
2323 }
2324
Johannes Berg32bfd352007-12-19 01:31:26 +01002325 if (!priv->vif) {
2326 IWL_DEBUG_DROP("Dropping - !priv->vif\n");
Zhu Yib481de92007-09-25 17:54:57 -07002327 goto drop_unlock;
2328 }
2329
Johannes Berg8318d782008-01-24 19:38:38 +01002330 if ((ctl->tx_rate->hw_value & 0xFF) == IWL_INVALID_RATE) {
Zhu Yib481de92007-09-25 17:54:57 -07002331 IWL_ERROR("ERROR: No TX rate available.\n");
2332 goto drop_unlock;
2333 }
2334
2335 unicast = !is_multicast_ether_addr(hdr->addr1);
2336 id = 0;
2337
2338 fc = le16_to_cpu(hdr->frame_control);
2339
Tomas Winkler0a6857e2008-03-12 16:58:49 -07002340#ifdef CONFIG_IWLWIFI_DEBUG
Zhu Yib481de92007-09-25 17:54:57 -07002341 if (ieee80211_is_auth(fc))
2342 IWL_DEBUG_TX("Sending AUTH frame\n");
2343 else if (ieee80211_is_assoc_request(fc))
2344 IWL_DEBUG_TX("Sending ASSOC frame\n");
2345 else if (ieee80211_is_reassoc_request(fc))
2346 IWL_DEBUG_TX("Sending REASSOC frame\n");
2347#endif
2348
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08002349 /* drop all data frame if we are not associated */
Gregory Greenman76f39152008-01-23 10:15:21 -08002350 if (((fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_DATA) &&
Tomas Winkler3109ece2008-03-28 16:33:35 -07002351 (!iwl_is_associated(priv) ||
Reinette Chatrea6477242008-02-14 10:40:28 -08002352 ((priv->iw_mode == IEEE80211_IF_TYPE_STA) && !priv->assoc_id) ||
Gregory Greenman76f39152008-01-23 10:15:21 -08002353 !priv->assoc_station_added)) {
Tomas Winkler3109ece2008-03-28 16:33:35 -07002354 IWL_DEBUG_DROP("Dropping - !iwl_is_associated\n");
Zhu Yib481de92007-09-25 17:54:57 -07002355 goto drop_unlock;
2356 }
2357
2358 spin_unlock_irqrestore(&priv->lock, flags);
2359
2360 hdr_len = ieee80211_get_hdrlen(fc);
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002361
2362 /* Find (or create) index into station table for destination station */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002363 sta_id = iwl4965_get_sta_id(priv, hdr);
Zhu Yib481de92007-09-25 17:54:57 -07002364 if (sta_id == IWL_INVALID_STATION) {
Joe Perches0795af52007-10-03 17:59:30 -07002365 DECLARE_MAC_BUF(mac);
2366
2367 IWL_DEBUG_DROP("Dropping - INVALID STATION: %s\n",
2368 print_mac(mac, hdr->addr1));
Zhu Yib481de92007-09-25 17:54:57 -07002369 goto drop;
2370 }
2371
2372 IWL_DEBUG_RATE("station Id %d\n", sta_id);
2373
2374 qc = ieee80211_get_qos_ctrl(hdr);
2375 if (qc) {
2376 u8 tid = (u8)(le16_to_cpu(*qc) & 0xf);
2377 seq_number = priv->stations[sta_id].tid[tid].seq_number &
2378 IEEE80211_SCTL_SEQ;
2379 hdr->seq_ctrl = cpu_to_le16(seq_number) |
2380 (hdr->seq_ctrl &
2381 __constant_cpu_to_le16(IEEE80211_SCTL_FRAG));
2382 seq_number += 0x10;
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08002383#ifdef CONFIG_IWL4965_HT
Zhu Yib481de92007-09-25 17:54:57 -07002384 /* aggregation is on for this <sta,tid> */
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02002385 if (ctl->flags & IEEE80211_TXCTL_AMPDU)
Zhu Yib481de92007-09-25 17:54:57 -07002386 txq_id = priv->stations[sta_id].tid[tid].agg.txq_id;
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02002387 priv->stations[sta_id].tid[tid].tfds_in_queue++;
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08002388#endif /* CONFIG_IWL4965_HT */
Zhu Yib481de92007-09-25 17:54:57 -07002389 }
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002390
2391 /* Descriptor for chosen Tx queue */
Zhu Yib481de92007-09-25 17:54:57 -07002392 txq = &priv->txq[txq_id];
2393 q = &txq->q;
2394
2395 spin_lock_irqsave(&priv->lock, flags);
2396
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002397 /* Set up first empty TFD within this queue's circular TFD buffer */
Tomas Winklerfc4b6852007-10-25 17:15:24 +08002398 tfd = &txq->bd[q->write_ptr];
Zhu Yib481de92007-09-25 17:54:57 -07002399 memset(tfd, 0, sizeof(*tfd));
2400 control_flags = (u32 *) tfd;
Tomas Winklerfc4b6852007-10-25 17:15:24 +08002401 idx = get_cmd_index(q, q->write_ptr, 0);
Zhu Yib481de92007-09-25 17:54:57 -07002402
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002403 /* Set up driver data for this TFD */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002404 memset(&(txq->txb[q->write_ptr]), 0, sizeof(struct iwl4965_tx_info));
Tomas Winklerfc4b6852007-10-25 17:15:24 +08002405 txq->txb[q->write_ptr].skb[0] = skb;
2406 memcpy(&(txq->txb[q->write_ptr].status.control),
Zhu Yib481de92007-09-25 17:54:57 -07002407 ctl, sizeof(struct ieee80211_tx_control));
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002408
2409 /* Set up first empty entry in queue's array of Tx/cmd buffers */
Zhu Yib481de92007-09-25 17:54:57 -07002410 out_cmd = &txq->cmd[idx];
2411 memset(&out_cmd->hdr, 0, sizeof(out_cmd->hdr));
2412 memset(&out_cmd->cmd.tx, 0, sizeof(out_cmd->cmd.tx));
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002413
2414 /*
2415 * Set up the Tx-command (not MAC!) header.
2416 * Store the chosen Tx queue and TFD index within the sequence field;
2417 * after Tx, uCode's Tx response will return this value so driver can
2418 * locate the frame within the tx queue and do post-tx processing.
2419 */
Zhu Yib481de92007-09-25 17:54:57 -07002420 out_cmd->hdr.cmd = REPLY_TX;
2421 out_cmd->hdr.sequence = cpu_to_le16((u16)(QUEUE_TO_SEQ(txq_id) |
Tomas Winklerfc4b6852007-10-25 17:15:24 +08002422 INDEX_TO_SEQ(q->write_ptr)));
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002423
2424 /* Copy MAC header from skb into command buffer */
Zhu Yib481de92007-09-25 17:54:57 -07002425 memcpy(out_cmd->cmd.tx.hdr, hdr, hdr_len);
2426
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002427 /*
2428 * Use the first empty entry in this queue's command buffer array
2429 * to contain the Tx command and MAC header concatenated together
2430 * (payload data will be in another buffer).
2431 * Size of this varies, due to varying MAC header length.
2432 * If end is not dword aligned, we'll have 2 extra bytes at the end
2433 * of the MAC header (device reads on dword boundaries).
2434 * We'll tell device about this padding later.
2435 */
Zhu Yib481de92007-09-25 17:54:57 -07002436 len = priv->hw_setting.tx_cmd_len +
Tomas Winkler857485c2008-03-21 13:53:44 -07002437 sizeof(struct iwl_cmd_header) + hdr_len;
Zhu Yib481de92007-09-25 17:54:57 -07002438
2439 len_org = len;
2440 len = (len + 3) & ~3;
2441
2442 if (len_org != len)
2443 len_org = 1;
2444 else
2445 len_org = 0;
2446
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002447 /* Physical address of this Tx command's header (not MAC header!),
2448 * within command buffer array. */
Tomas Winkler857485c2008-03-21 13:53:44 -07002449 txcmd_phys = txq->dma_addr_cmd + sizeof(struct iwl_cmd) * idx +
2450 offsetof(struct iwl_cmd, hdr);
Zhu Yib481de92007-09-25 17:54:57 -07002451
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002452 /* Add buffer containing Tx command and MAC(!) header to TFD's
2453 * first entry */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002454 iwl4965_hw_txq_attach_buf_to_tfd(priv, tfd, txcmd_phys, len);
Zhu Yib481de92007-09-25 17:54:57 -07002455
2456 if (!(ctl->flags & IEEE80211_TXCTL_DO_NOT_ENCRYPT))
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07002457 iwl4965_build_tx_cmd_hwcrypto(priv, ctl, out_cmd, skb, sta_id);
Zhu Yib481de92007-09-25 17:54:57 -07002458
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002459 /* Set up TFD's 2nd entry to point directly to remainder of skb,
2460 * if any (802.11 null frames have no payload). */
Zhu Yib481de92007-09-25 17:54:57 -07002461 len = skb->len - hdr_len;
2462 if (len) {
2463 phys_addr = pci_map_single(priv->pci_dev, skb->data + hdr_len,
2464 len, PCI_DMA_TODEVICE);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002465 iwl4965_hw_txq_attach_buf_to_tfd(priv, tfd, phys_addr, len);
Zhu Yib481de92007-09-25 17:54:57 -07002466 }
2467
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002468 /* Tell 4965 about any 2-byte padding after MAC header */
Zhu Yib481de92007-09-25 17:54:57 -07002469 if (len_org)
2470 out_cmd->cmd.tx.tx_flags |= TX_CMD_FLG_MH_PAD_MSK;
2471
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002472 /* Total # bytes to be transmitted */
Zhu Yib481de92007-09-25 17:54:57 -07002473 len = (u16)skb->len;
2474 out_cmd->cmd.tx.len = cpu_to_le16(len);
2475
2476 /* TODO need this for burst mode later on */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002477 iwl4965_build_tx_cmd_basic(priv, out_cmd, ctl, hdr, unicast, sta_id);
Zhu Yib481de92007-09-25 17:54:57 -07002478
2479 /* set is_hcca to 0; it probably will never be implemented */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002480 iwl4965_hw_build_tx_cmd_rate(priv, out_cmd, ctl, hdr, sta_id, 0);
Zhu Yib481de92007-09-25 17:54:57 -07002481
Tomas Winkler19758be2008-03-12 16:58:51 -07002482 iwl_update_tx_stats(priv, fc, len);
2483
Tomas Winkler857485c2008-03-21 13:53:44 -07002484 scratch_phys = txcmd_phys + sizeof(struct iwl_cmd_header) +
Tomas Winkler87e4f7d2008-01-14 17:46:16 -08002485 offsetof(struct iwl4965_tx_cmd, scratch);
2486 out_cmd->cmd.tx.dram_lsb_ptr = cpu_to_le32(scratch_phys);
2487 out_cmd->cmd.tx.dram_msb_ptr = iwl_get_dma_hi_address(scratch_phys);
2488
Zhu Yib481de92007-09-25 17:54:57 -07002489 if (!ieee80211_get_morefrag(hdr)) {
2490 txq->need_update = 1;
2491 if (qc) {
2492 u8 tid = (u8)(le16_to_cpu(*qc) & 0xf);
2493 priv->stations[sta_id].tid[tid].seq_number = seq_number;
2494 }
2495 } else {
2496 wait_write_ptr = 1;
2497 txq->need_update = 0;
2498 }
2499
Tomas Winkler0a6857e2008-03-12 16:58:49 -07002500 iwl_print_hex_dump(IWL_DL_TX, out_cmd->cmd.payload,
Zhu Yib481de92007-09-25 17:54:57 -07002501 sizeof(out_cmd->cmd.tx));
2502
Tomas Winkler0a6857e2008-03-12 16:58:49 -07002503 iwl_print_hex_dump(IWL_DL_TX, (u8 *)out_cmd->cmd.tx.hdr,
Zhu Yib481de92007-09-25 17:54:57 -07002504 ieee80211_get_hdrlen(fc));
2505
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002506 /* Set up entry for this TFD in Tx byte-count array */
Zhu Yib481de92007-09-25 17:54:57 -07002507 iwl4965_tx_queue_update_wr_ptr(priv, txq, len);
2508
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002509 /* Tell device the write index *just past* this latest filled TFD */
Tomas Winklerc54b6792008-03-06 17:36:53 -08002510 q->write_ptr = iwl_queue_inc_wrap(q->write_ptr, q->n_bd);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002511 rc = iwl4965_tx_queue_update_write_ptr(priv, txq);
Zhu Yib481de92007-09-25 17:54:57 -07002512 spin_unlock_irqrestore(&priv->lock, flags);
2513
2514 if (rc)
2515 return rc;
2516
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002517 if ((iwl4965_queue_space(q) < q->high_mark)
Zhu Yib481de92007-09-25 17:54:57 -07002518 && priv->mac80211_registered) {
2519 if (wait_write_ptr) {
2520 spin_lock_irqsave(&priv->lock, flags);
2521 txq->need_update = 1;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002522 iwl4965_tx_queue_update_write_ptr(priv, txq);
Zhu Yib481de92007-09-25 17:54:57 -07002523 spin_unlock_irqrestore(&priv->lock, flags);
2524 }
2525
2526 ieee80211_stop_queue(priv->hw, ctl->queue);
2527 }
2528
2529 return 0;
2530
2531drop_unlock:
2532 spin_unlock_irqrestore(&priv->lock, flags);
2533drop:
2534 return -1;
2535}
2536
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002537static void iwl4965_set_rate(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002538{
Johannes Berg8318d782008-01-24 19:38:38 +01002539 const struct ieee80211_supported_band *hw = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07002540 struct ieee80211_rate *rate;
2541 int i;
2542
Johannes Berg8318d782008-01-24 19:38:38 +01002543 hw = iwl4965_get_hw_mode(priv, priv->band);
Saleem Abdulrasoolc4ba9622007-11-18 23:59:08 -08002544 if (!hw) {
2545 IWL_ERROR("Failed to set rate: unable to get hw mode\n");
2546 return;
2547 }
Zhu Yib481de92007-09-25 17:54:57 -07002548
2549 priv->active_rate = 0;
2550 priv->active_rate_basic = 0;
2551
Johannes Berg8318d782008-01-24 19:38:38 +01002552 for (i = 0; i < hw->n_bitrates; i++) {
2553 rate = &(hw->bitrates[i]);
2554 if (rate->hw_value < IWL_RATE_COUNT)
2555 priv->active_rate |= (1 << rate->hw_value);
Zhu Yib481de92007-09-25 17:54:57 -07002556 }
2557
2558 IWL_DEBUG_RATE("Set active_rate = %0x, active_rate_basic = %0x\n",
2559 priv->active_rate, priv->active_rate_basic);
2560
2561 /*
2562 * If a basic rate is configured, then use it (adding IWL_RATE_1M_MASK)
2563 * otherwise set it to the default of all CCK rates and 6, 12, 24 for
2564 * OFDM
2565 */
2566 if (priv->active_rate_basic & IWL_CCK_BASIC_RATES_MASK)
2567 priv->staging_rxon.cck_basic_rates =
2568 ((priv->active_rate_basic &
2569 IWL_CCK_RATES_MASK) >> IWL_FIRST_CCK_RATE) & 0xF;
2570 else
2571 priv->staging_rxon.cck_basic_rates =
2572 (IWL_CCK_BASIC_RATES_MASK >> IWL_FIRST_CCK_RATE) & 0xF;
2573
2574 if (priv->active_rate_basic & IWL_OFDM_BASIC_RATES_MASK)
2575 priv->staging_rxon.ofdm_basic_rates =
2576 ((priv->active_rate_basic &
2577 (IWL_OFDM_BASIC_RATES_MASK | IWL_RATE_6M_MASK)) >>
2578 IWL_FIRST_OFDM_RATE) & 0xFF;
2579 else
2580 priv->staging_rxon.ofdm_basic_rates =
2581 (IWL_OFDM_BASIC_RATES_MASK >> IWL_FIRST_OFDM_RATE) & 0xFF;
2582}
2583
Mohamed Abbasad97edd2008-03-28 16:21:06 -07002584void iwl4965_radio_kill_sw(struct iwl_priv *priv, int disable_radio)
Zhu Yib481de92007-09-25 17:54:57 -07002585{
2586 unsigned long flags;
2587
2588 if (!!disable_radio == test_bit(STATUS_RF_KILL_SW, &priv->status))
2589 return;
2590
2591 IWL_DEBUG_RF_KILL("Manual SW RF KILL set to: RADIO %s\n",
2592 disable_radio ? "OFF" : "ON");
2593
2594 if (disable_radio) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002595 iwl4965_scan_cancel(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002596 /* FIXME: This is a workaround for AP */
2597 if (priv->iw_mode != IEEE80211_IF_TYPE_AP) {
2598 spin_lock_irqsave(&priv->lock, flags);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07002599 iwl_write32(priv, CSR_UCODE_DRV_GP1_SET,
Zhu Yib481de92007-09-25 17:54:57 -07002600 CSR_UCODE_SW_BIT_RFKILL);
2601 spin_unlock_irqrestore(&priv->lock, flags);
Mohamed Abbasad97edd2008-03-28 16:21:06 -07002602 /* call the host command only if no hw rf-kill set */
2603 if (!test_bit(STATUS_RF_KILL_HW, &priv->status))
2604 iwl4965_send_card_state(priv,
2605 CARD_STATE_CMD_DISABLE,
2606 0);
Zhu Yib481de92007-09-25 17:54:57 -07002607 set_bit(STATUS_RF_KILL_SW, &priv->status);
Mohamed Abbasad97edd2008-03-28 16:21:06 -07002608
2609 /* make sure mac80211 stop sending Tx frame */
2610 if (priv->mac80211_registered)
2611 ieee80211_stop_queues(priv->hw);
Zhu Yib481de92007-09-25 17:54:57 -07002612 }
2613 return;
2614 }
2615
2616 spin_lock_irqsave(&priv->lock, flags);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07002617 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
Zhu Yib481de92007-09-25 17:54:57 -07002618
2619 clear_bit(STATUS_RF_KILL_SW, &priv->status);
2620 spin_unlock_irqrestore(&priv->lock, flags);
2621
2622 /* wake up ucode */
2623 msleep(10);
2624
2625 spin_lock_irqsave(&priv->lock, flags);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07002626 iwl_read32(priv, CSR_UCODE_DRV_GP1);
2627 if (!iwl_grab_nic_access(priv))
2628 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002629 spin_unlock_irqrestore(&priv->lock, flags);
2630
2631 if (test_bit(STATUS_RF_KILL_HW, &priv->status)) {
2632 IWL_DEBUG_RF_KILL("Can not turn radio back on - "
2633 "disabled by HW switch\n");
2634 return;
2635 }
2636
2637 queue_work(priv->workqueue, &priv->restart);
2638 return;
2639}
2640
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002641void iwl4965_set_decrypted_flag(struct iwl_priv *priv, struct sk_buff *skb,
Zhu Yib481de92007-09-25 17:54:57 -07002642 u32 decrypt_res, struct ieee80211_rx_status *stats)
2643{
2644 u16 fc =
2645 le16_to_cpu(((struct ieee80211_hdr *)skb->data)->frame_control);
2646
2647 if (priv->active_rxon.filter_flags & RXON_FILTER_DIS_DECRYPT_MSK)
2648 return;
2649
2650 if (!(fc & IEEE80211_FCTL_PROTECTED))
2651 return;
2652
2653 IWL_DEBUG_RX("decrypt_res:0x%x\n", decrypt_res);
2654 switch (decrypt_res & RX_RES_STATUS_SEC_TYPE_MSK) {
2655 case RX_RES_STATUS_SEC_TYPE_TKIP:
Emmanuel Grumbach17e476b2008-03-19 16:41:42 -07002656 /* The uCode has got a bad phase 1 Key, pushes the packet.
2657 * Decryption will be done in SW. */
2658 if ((decrypt_res & RX_RES_STATUS_DECRYPT_TYPE_MSK) ==
2659 RX_RES_STATUS_BAD_KEY_TTAK)
2660 break;
2661
Zhu Yib481de92007-09-25 17:54:57 -07002662 if ((decrypt_res & RX_RES_STATUS_DECRYPT_TYPE_MSK) ==
2663 RX_RES_STATUS_BAD_ICV_MIC)
2664 stats->flag |= RX_FLAG_MMIC_ERROR;
2665 case RX_RES_STATUS_SEC_TYPE_WEP:
2666 case RX_RES_STATUS_SEC_TYPE_CCMP:
2667 if ((decrypt_res & RX_RES_STATUS_DECRYPT_TYPE_MSK) ==
2668 RX_RES_STATUS_DECRYPT_OK) {
2669 IWL_DEBUG_RX("hw decrypt successfully!!!\n");
2670 stats->flag |= RX_FLAG_DECRYPTED;
2671 }
2672 break;
2673
2674 default:
2675 break;
2676 }
2677}
2678
Zhu Yib481de92007-09-25 17:54:57 -07002679
2680#define IWL_PACKET_RETRY_TIME HZ
2681
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002682int iwl4965_is_duplicate_packet(struct iwl_priv *priv, struct ieee80211_hdr *header)
Zhu Yib481de92007-09-25 17:54:57 -07002683{
2684 u16 sc = le16_to_cpu(header->seq_ctrl);
2685 u16 seq = (sc & IEEE80211_SCTL_SEQ) >> 4;
2686 u16 frag = sc & IEEE80211_SCTL_FRAG;
2687 u16 *last_seq, *last_frag;
2688 unsigned long *last_time;
2689
2690 switch (priv->iw_mode) {
2691 case IEEE80211_IF_TYPE_IBSS:{
2692 struct list_head *p;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002693 struct iwl4965_ibss_seq *entry = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07002694 u8 *mac = header->addr2;
2695 int index = mac[5] & (IWL_IBSS_MAC_HASH_SIZE - 1);
2696
2697 __list_for_each(p, &priv->ibss_mac_hash[index]) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002698 entry = list_entry(p, struct iwl4965_ibss_seq, list);
Zhu Yib481de92007-09-25 17:54:57 -07002699 if (!compare_ether_addr(entry->mac, mac))
2700 break;
2701 }
2702 if (p == &priv->ibss_mac_hash[index]) {
2703 entry = kzalloc(sizeof(*entry), GFP_ATOMIC);
2704 if (!entry) {
Ian Schrambc434dd2007-10-25 17:15:29 +08002705 IWL_ERROR("Cannot malloc new mac entry\n");
Zhu Yib481de92007-09-25 17:54:57 -07002706 return 0;
2707 }
2708 memcpy(entry->mac, mac, ETH_ALEN);
2709 entry->seq_num = seq;
2710 entry->frag_num = frag;
2711 entry->packet_time = jiffies;
Ian Schrambc434dd2007-10-25 17:15:29 +08002712 list_add(&entry->list, &priv->ibss_mac_hash[index]);
Zhu Yib481de92007-09-25 17:54:57 -07002713 return 0;
2714 }
2715 last_seq = &entry->seq_num;
2716 last_frag = &entry->frag_num;
2717 last_time = &entry->packet_time;
2718 break;
2719 }
2720 case IEEE80211_IF_TYPE_STA:
2721 last_seq = &priv->last_seq_num;
2722 last_frag = &priv->last_frag_num;
2723 last_time = &priv->last_packet_time;
2724 break;
2725 default:
2726 return 0;
2727 }
2728 if ((*last_seq == seq) &&
2729 time_after(*last_time + IWL_PACKET_RETRY_TIME, jiffies)) {
2730 if (*last_frag == frag)
2731 goto drop;
2732 if (*last_frag + 1 != frag)
2733 /* out-of-order fragment */
2734 goto drop;
2735 } else
2736 *last_seq = seq;
2737
2738 *last_frag = frag;
2739 *last_time = jiffies;
2740 return 0;
2741
2742 drop:
2743 return 1;
2744}
2745
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08002746#ifdef CONFIG_IWL4965_SPECTRUM_MEASUREMENT
Zhu Yib481de92007-09-25 17:54:57 -07002747
2748#include "iwl-spectrum.h"
2749
2750#define BEACON_TIME_MASK_LOW 0x00FFFFFF
2751#define BEACON_TIME_MASK_HIGH 0xFF000000
2752#define TIME_UNIT 1024
2753
2754/*
2755 * extended beacon time format
2756 * time in usec will be changed into a 32-bit value in 8:24 format
2757 * the high 1 byte is the beacon counts
2758 * the lower 3 bytes is the time in usec within one beacon interval
2759 */
2760
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002761static u32 iwl4965_usecs_to_beacons(u32 usec, u32 beacon_interval)
Zhu Yib481de92007-09-25 17:54:57 -07002762{
2763 u32 quot;
2764 u32 rem;
2765 u32 interval = beacon_interval * 1024;
2766
2767 if (!interval || !usec)
2768 return 0;
2769
2770 quot = (usec / interval) & (BEACON_TIME_MASK_HIGH >> 24);
2771 rem = (usec % interval) & BEACON_TIME_MASK_LOW;
2772
2773 return (quot << 24) + rem;
2774}
2775
2776/* base is usually what we get from ucode with each received frame,
2777 * the same as HW timer counter counting down
2778 */
2779
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002780static __le32 iwl4965_add_beacon_time(u32 base, u32 addon, u32 beacon_interval)
Zhu Yib481de92007-09-25 17:54:57 -07002781{
2782 u32 base_low = base & BEACON_TIME_MASK_LOW;
2783 u32 addon_low = addon & BEACON_TIME_MASK_LOW;
2784 u32 interval = beacon_interval * TIME_UNIT;
2785 u32 res = (base & BEACON_TIME_MASK_HIGH) +
2786 (addon & BEACON_TIME_MASK_HIGH);
2787
2788 if (base_low > addon_low)
2789 res += base_low - addon_low;
2790 else if (base_low < addon_low) {
2791 res += interval + base_low - addon_low;
2792 res += (1 << 24);
2793 } else
2794 res += (1 << 24);
2795
2796 return cpu_to_le32(res);
2797}
2798
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002799static int iwl4965_get_measurement(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07002800 struct ieee80211_measurement_params *params,
2801 u8 type)
2802{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002803 struct iwl4965_spectrum_cmd spectrum;
2804 struct iwl4965_rx_packet *res;
Tomas Winkler857485c2008-03-21 13:53:44 -07002805 struct iwl_host_cmd cmd = {
Zhu Yib481de92007-09-25 17:54:57 -07002806 .id = REPLY_SPECTRUM_MEASUREMENT_CMD,
2807 .data = (void *)&spectrum,
2808 .meta.flags = CMD_WANT_SKB,
2809 };
2810 u32 add_time = le64_to_cpu(params->start_time);
2811 int rc;
2812 int spectrum_resp_status;
2813 int duration = le16_to_cpu(params->duration);
2814
Tomas Winkler3109ece2008-03-28 16:33:35 -07002815 if (iwl_is_associated(priv))
Zhu Yib481de92007-09-25 17:54:57 -07002816 add_time =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002817 iwl4965_usecs_to_beacons(
Zhu Yib481de92007-09-25 17:54:57 -07002818 le64_to_cpu(params->start_time) - priv->last_tsf,
2819 le16_to_cpu(priv->rxon_timing.beacon_interval));
2820
2821 memset(&spectrum, 0, sizeof(spectrum));
2822
2823 spectrum.channel_count = cpu_to_le16(1);
2824 spectrum.flags =
2825 RXON_FLG_TSF2HOST_MSK | RXON_FLG_ANT_A_MSK | RXON_FLG_DIS_DIV_MSK;
2826 spectrum.filter_flags = MEASUREMENT_FILTER_FLAG;
2827 cmd.len = sizeof(spectrum);
2828 spectrum.len = cpu_to_le16(cmd.len - sizeof(spectrum.len));
2829
Tomas Winkler3109ece2008-03-28 16:33:35 -07002830 if (iwl_is_associated(priv))
Zhu Yib481de92007-09-25 17:54:57 -07002831 spectrum.start_time =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002832 iwl4965_add_beacon_time(priv->last_beacon_time,
Zhu Yib481de92007-09-25 17:54:57 -07002833 add_time,
2834 le16_to_cpu(priv->rxon_timing.beacon_interval));
2835 else
2836 spectrum.start_time = 0;
2837
2838 spectrum.channels[0].duration = cpu_to_le32(duration * TIME_UNIT);
2839 spectrum.channels[0].channel = params->channel;
2840 spectrum.channels[0].type = type;
2841 if (priv->active_rxon.flags & RXON_FLG_BAND_24G_MSK)
2842 spectrum.flags |= RXON_FLG_BAND_24G_MSK |
2843 RXON_FLG_AUTO_DETECT_MSK | RXON_FLG_TGG_PROTECT_MSK;
2844
Tomas Winkler857485c2008-03-21 13:53:44 -07002845 rc = iwl_send_cmd_sync(priv, &cmd);
Zhu Yib481de92007-09-25 17:54:57 -07002846 if (rc)
2847 return rc;
2848
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002849 res = (struct iwl4965_rx_packet *)cmd.meta.u.skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07002850 if (res->hdr.flags & IWL_CMD_FAILED_MSK) {
2851 IWL_ERROR("Bad return from REPLY_RX_ON_ASSOC command\n");
2852 rc = -EIO;
2853 }
2854
2855 spectrum_resp_status = le16_to_cpu(res->u.spectrum.status);
2856 switch (spectrum_resp_status) {
2857 case 0: /* Command will be handled */
2858 if (res->u.spectrum.id != 0xff) {
2859 IWL_DEBUG_INFO
2860 ("Replaced existing measurement: %d\n",
2861 res->u.spectrum.id);
2862 priv->measurement_status &= ~MEASUREMENT_READY;
2863 }
2864 priv->measurement_status |= MEASUREMENT_ACTIVE;
2865 rc = 0;
2866 break;
2867
2868 case 1: /* Command will not be handled */
2869 rc = -EAGAIN;
2870 break;
2871 }
2872
2873 dev_kfree_skb_any(cmd.meta.u.skb);
2874
2875 return rc;
2876}
2877#endif
2878
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002879static void iwl4965_txstatus_to_ieee(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002880 struct iwl4965_tx_info *tx_sta)
Zhu Yib481de92007-09-25 17:54:57 -07002881{
2882
2883 tx_sta->status.ack_signal = 0;
2884 tx_sta->status.excessive_retries = 0;
2885 tx_sta->status.queue_length = 0;
2886 tx_sta->status.queue_number = 0;
2887
2888 if (in_interrupt())
2889 ieee80211_tx_status_irqsafe(priv->hw,
2890 tx_sta->skb[0], &(tx_sta->status));
2891 else
2892 ieee80211_tx_status(priv->hw,
2893 tx_sta->skb[0], &(tx_sta->status));
2894
2895 tx_sta->skb[0] = NULL;
2896}
2897
2898/**
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002899 * iwl4965_tx_queue_reclaim - Reclaim Tx queue entries already Tx'd
Zhu Yib481de92007-09-25 17:54:57 -07002900 *
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002901 * When FW advances 'R' index, all entries between old and new 'R' index
2902 * need to be reclaimed. As result, some free space forms. If there is
2903 * enough free space (> low mark), wake the stack that feeds us.
Zhu Yib481de92007-09-25 17:54:57 -07002904 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002905int iwl4965_tx_queue_reclaim(struct iwl_priv *priv, int txq_id, int index)
Zhu Yib481de92007-09-25 17:54:57 -07002906{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002907 struct iwl4965_tx_queue *txq = &priv->txq[txq_id];
2908 struct iwl4965_queue *q = &txq->q;
Zhu Yib481de92007-09-25 17:54:57 -07002909 int nfreed = 0;
2910
2911 if ((index >= q->n_bd) || (x2_queue_used(q, index) == 0)) {
2912 IWL_ERROR("Read index for DMA queue txq id (%d), index %d, "
2913 "is out of range [0-%d] %d %d.\n", txq_id,
Tomas Winklerfc4b6852007-10-25 17:15:24 +08002914 index, q->n_bd, q->write_ptr, q->read_ptr);
Zhu Yib481de92007-09-25 17:54:57 -07002915 return 0;
2916 }
2917
Tomas Winklerc54b6792008-03-06 17:36:53 -08002918 for (index = iwl_queue_inc_wrap(index, q->n_bd);
Tomas Winklerfc4b6852007-10-25 17:15:24 +08002919 q->read_ptr != index;
Tomas Winklerc54b6792008-03-06 17:36:53 -08002920 q->read_ptr = iwl_queue_inc_wrap(q->read_ptr, q->n_bd)) {
Zhu Yib481de92007-09-25 17:54:57 -07002921 if (txq_id != IWL_CMD_QUEUE_NUM) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002922 iwl4965_txstatus_to_ieee(priv,
Tomas Winklerfc4b6852007-10-25 17:15:24 +08002923 &(txq->txb[txq->q.read_ptr]));
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002924 iwl4965_hw_txq_free_tfd(priv, txq);
Zhu Yib481de92007-09-25 17:54:57 -07002925 } else if (nfreed > 1) {
2926 IWL_ERROR("HCMD skipped: index (%d) %d %d\n", index,
Tomas Winklerfc4b6852007-10-25 17:15:24 +08002927 q->write_ptr, q->read_ptr);
Zhu Yib481de92007-09-25 17:54:57 -07002928 queue_work(priv->workqueue, &priv->restart);
2929 }
2930 nfreed++;
2931 }
2932
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02002933/* if (iwl4965_queue_space(q) > q->low_mark && (txq_id >= 0) &&
Zhu Yib481de92007-09-25 17:54:57 -07002934 (txq_id != IWL_CMD_QUEUE_NUM) &&
2935 priv->mac80211_registered)
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02002936 ieee80211_wake_queue(priv->hw, txq_id); */
Zhu Yib481de92007-09-25 17:54:57 -07002937
2938
2939 return nfreed;
2940}
2941
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002942static int iwl4965_is_tx_success(u32 status)
Zhu Yib481de92007-09-25 17:54:57 -07002943{
2944 status &= TX_STATUS_MSK;
2945 return (status == TX_STATUS_SUCCESS)
2946 || (status == TX_STATUS_DIRECT_DONE);
2947}
2948
2949/******************************************************************************
2950 *
2951 * Generic RX handler implementations
2952 *
2953 ******************************************************************************/
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08002954#ifdef CONFIG_IWL4965_HT
Zhu Yib481de92007-09-25 17:54:57 -07002955
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002956static inline int iwl4965_get_ra_sta_id(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07002957 struct ieee80211_hdr *hdr)
2958{
2959 if (priv->iw_mode == IEEE80211_IF_TYPE_STA)
2960 return IWL_AP_ID;
2961 else {
2962 u8 *da = ieee80211_get_DA(hdr);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002963 return iwl4965_hw_find_station(priv, da);
Zhu Yib481de92007-09-25 17:54:57 -07002964 }
2965}
2966
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002967static struct ieee80211_hdr *iwl4965_tx_queue_get_hdr(
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002968 struct iwl_priv *priv, int txq_id, int idx)
Zhu Yib481de92007-09-25 17:54:57 -07002969{
2970 if (priv->txq[txq_id].txb[idx].skb[0])
2971 return (struct ieee80211_hdr *)priv->txq[txq_id].
2972 txb[idx].skb[0]->data;
2973 return NULL;
2974}
2975
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002976static inline u32 iwl4965_get_scd_ssn(struct iwl4965_tx_resp *tx_resp)
Zhu Yib481de92007-09-25 17:54:57 -07002977{
2978 __le32 *scd_ssn = (__le32 *)((u32 *)&tx_resp->status +
2979 tx_resp->frame_count);
2980 return le32_to_cpu(*scd_ssn) & MAX_SN;
2981
2982}
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002983
2984/**
2985 * iwl4965_tx_status_reply_tx - Handle Tx rspnse for frames in aggregation queue
2986 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002987static int iwl4965_tx_status_reply_tx(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002988 struct iwl4965_ht_agg *agg,
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02002989 struct iwl4965_tx_resp_agg *tx_resp,
Zhu Yib481de92007-09-25 17:54:57 -07002990 u16 start_idx)
2991{
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02002992 u16 status;
2993 struct agg_tx_status *frame_status = &tx_resp->status;
Zhu Yib481de92007-09-25 17:54:57 -07002994 struct ieee80211_tx_status *tx_status = NULL;
2995 struct ieee80211_hdr *hdr = NULL;
2996 int i, sh;
2997 int txq_id, idx;
2998 u16 seq;
2999
3000 if (agg->wait_for_ba)
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003001 IWL_DEBUG_TX_REPLY("got tx response w/o block-ack\n");
Zhu Yib481de92007-09-25 17:54:57 -07003002
3003 agg->frame_count = tx_resp->frame_count;
3004 agg->start_idx = start_idx;
3005 agg->rate_n_flags = le32_to_cpu(tx_resp->rate_n_flags);
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003006 agg->bitmap = 0;
Zhu Yib481de92007-09-25 17:54:57 -07003007
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003008 /* # frames attempted by Tx command */
Zhu Yib481de92007-09-25 17:54:57 -07003009 if (agg->frame_count == 1) {
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003010 /* Only one frame was attempted; no block-ack will arrive */
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003011 status = le16_to_cpu(frame_status[0].status);
3012 seq = le16_to_cpu(frame_status[0].sequence);
3013 idx = SEQ_TO_INDEX(seq);
3014 txq_id = SEQ_TO_QUEUE(seq);
Zhu Yib481de92007-09-25 17:54:57 -07003015
Zhu Yib481de92007-09-25 17:54:57 -07003016 /* FIXME: code repetition */
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003017 IWL_DEBUG_TX_REPLY("FrameCnt = %d, StartIdx=%d idx=%d\n",
3018 agg->frame_count, agg->start_idx, idx);
Zhu Yib481de92007-09-25 17:54:57 -07003019
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003020 tx_status = &(priv->txq[txq_id].txb[idx].status);
Zhu Yib481de92007-09-25 17:54:57 -07003021 tx_status->retry_count = tx_resp->failure_frame;
3022 tx_status->queue_number = status & 0xff;
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003023 tx_status->queue_length = tx_resp->failure_rts;
3024 tx_status->control.flags &= ~IEEE80211_TXCTL_AMPDU;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003025 tx_status->flags = iwl4965_is_tx_success(status)?
Zhu Yib481de92007-09-25 17:54:57 -07003026 IEEE80211_TX_STATUS_ACK : 0;
Ron Rindjunsky4c424e42008-03-04 18:09:27 -08003027 iwl4965_hwrate_to_tx_control(priv,
3028 le32_to_cpu(tx_resp->rate_n_flags),
3029 &tx_status->control);
Zhu Yib481de92007-09-25 17:54:57 -07003030 /* FIXME: code repetition end */
3031
3032 IWL_DEBUG_TX_REPLY("1 Frame 0x%x failure :%d\n",
3033 status & 0xff, tx_resp->failure_frame);
3034 IWL_DEBUG_TX_REPLY("Rate Info rate_n_flags=%x\n",
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003035 iwl4965_hw_get_rate_n_flags(tx_resp->rate_n_flags));
Zhu Yib481de92007-09-25 17:54:57 -07003036
3037 agg->wait_for_ba = 0;
3038 } else {
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003039 /* Two or more frames were attempted; expect block-ack */
Zhu Yib481de92007-09-25 17:54:57 -07003040 u64 bitmap = 0;
3041 int start = agg->start_idx;
3042
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003043 /* Construct bit-map of pending frames within Tx window */
Zhu Yib481de92007-09-25 17:54:57 -07003044 for (i = 0; i < agg->frame_count; i++) {
3045 u16 sc;
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003046 status = le16_to_cpu(frame_status[i].status);
3047 seq = le16_to_cpu(frame_status[i].sequence);
Zhu Yib481de92007-09-25 17:54:57 -07003048 idx = SEQ_TO_INDEX(seq);
3049 txq_id = SEQ_TO_QUEUE(seq);
3050
3051 if (status & (AGG_TX_STATE_FEW_BYTES_MSK |
3052 AGG_TX_STATE_ABORT_MSK))
3053 continue;
3054
3055 IWL_DEBUG_TX_REPLY("FrameCnt = %d, txq_id=%d idx=%d\n",
3056 agg->frame_count, txq_id, idx);
3057
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003058 hdr = iwl4965_tx_queue_get_hdr(priv, txq_id, idx);
Zhu Yib481de92007-09-25 17:54:57 -07003059
3060 sc = le16_to_cpu(hdr->seq_ctrl);
3061 if (idx != (SEQ_TO_SN(sc) & 0xff)) {
3062 IWL_ERROR("BUG_ON idx doesn't match seq control"
3063 " idx=%d, seq_idx=%d, seq=%d\n",
3064 idx, SEQ_TO_SN(sc),
3065 hdr->seq_ctrl);
3066 return -1;
3067 }
3068
3069 IWL_DEBUG_TX_REPLY("AGG Frame i=%d idx %d seq=%d\n",
3070 i, idx, SEQ_TO_SN(sc));
3071
3072 sh = idx - start;
3073 if (sh > 64) {
3074 sh = (start - idx) + 0xff;
3075 bitmap = bitmap << sh;
3076 sh = 0;
3077 start = idx;
3078 } else if (sh < -64)
3079 sh = 0xff - (start - idx);
3080 else if (sh < 0) {
3081 sh = start - idx;
3082 start = idx;
3083 bitmap = bitmap << sh;
3084 sh = 0;
3085 }
3086 bitmap |= (1 << sh);
3087 IWL_DEBUG_TX_REPLY("start=%d bitmap=0x%x\n",
3088 start, (u32)(bitmap & 0xFFFFFFFF));
3089 }
3090
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003091 agg->bitmap = bitmap;
Zhu Yib481de92007-09-25 17:54:57 -07003092 agg->start_idx = start;
3093 agg->rate_n_flags = le32_to_cpu(tx_resp->rate_n_flags);
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003094 IWL_DEBUG_TX_REPLY("Frames %d start_idx=%d bitmap=0x%llx\n",
Zhu Yib481de92007-09-25 17:54:57 -07003095 agg->frame_count, agg->start_idx,
John W. Linville06501d22008-04-01 17:38:47 -04003096 (unsigned long long)agg->bitmap);
Zhu Yib481de92007-09-25 17:54:57 -07003097
3098 if (bitmap)
3099 agg->wait_for_ba = 1;
3100 }
3101 return 0;
3102}
3103#endif
Zhu Yib481de92007-09-25 17:54:57 -07003104
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003105/**
3106 * iwl4965_rx_reply_tx - Handle standard (non-aggregation) Tx response
3107 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003108static void iwl4965_rx_reply_tx(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003109 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003110{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003111 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07003112 u16 sequence = le16_to_cpu(pkt->hdr.sequence);
3113 int txq_id = SEQ_TO_QUEUE(sequence);
3114 int index = SEQ_TO_INDEX(sequence);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003115 struct iwl4965_tx_queue *txq = &priv->txq[txq_id];
Zhu Yib481de92007-09-25 17:54:57 -07003116 struct ieee80211_tx_status *tx_status;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003117 struct iwl4965_tx_resp *tx_resp = (void *)&pkt->u.raw[0];
Zhu Yib481de92007-09-25 17:54:57 -07003118 u32 status = le32_to_cpu(tx_resp->status);
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08003119#ifdef CONFIG_IWL4965_HT
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003120 int tid = MAX_TID_COUNT, sta_id = IWL_INVALID_STATION;
3121 struct ieee80211_hdr *hdr;
3122 __le16 *qc;
Zhu Yib481de92007-09-25 17:54:57 -07003123#endif
3124
3125 if ((index >= txq->q.n_bd) || (x2_queue_used(&txq->q, index) == 0)) {
3126 IWL_ERROR("Read index for DMA queue txq_id (%d) index %d "
3127 "is out of range [0-%d] %d %d\n", txq_id,
Tomas Winklerfc4b6852007-10-25 17:15:24 +08003128 index, txq->q.n_bd, txq->q.write_ptr,
3129 txq->q.read_ptr);
Zhu Yib481de92007-09-25 17:54:57 -07003130 return;
3131 }
3132
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08003133#ifdef CONFIG_IWL4965_HT
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003134 hdr = iwl4965_tx_queue_get_hdr(priv, txq_id, index);
3135 qc = ieee80211_get_qos_ctrl(hdr);
Zhu Yib481de92007-09-25 17:54:57 -07003136
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003137 if (qc)
Zhu Yib481de92007-09-25 17:54:57 -07003138 tid = le16_to_cpu(*qc) & 0xf;
3139
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003140 sta_id = iwl4965_get_ra_sta_id(priv, hdr);
3141 if (txq->sched_retry && unlikely(sta_id == IWL_INVALID_STATION)) {
3142 IWL_ERROR("Station not known\n");
3143 return;
3144 }
3145
3146 if (txq->sched_retry) {
3147 const u32 scd_ssn = iwl4965_get_scd_ssn(tx_resp);
3148 struct iwl4965_ht_agg *agg = NULL;
3149
3150 if (!qc)
Zhu Yib481de92007-09-25 17:54:57 -07003151 return;
Zhu Yib481de92007-09-25 17:54:57 -07003152
3153 agg = &priv->stations[sta_id].tid[tid].agg;
3154
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003155 iwl4965_tx_status_reply_tx(priv, agg,
3156 (struct iwl4965_tx_resp_agg *)tx_resp, index);
Zhu Yib481de92007-09-25 17:54:57 -07003157
3158 if ((tx_resp->frame_count == 1) &&
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003159 !iwl4965_is_tx_success(status)) {
Zhu Yib481de92007-09-25 17:54:57 -07003160 /* TODO: send BAR */
3161 }
3162
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003163 if (txq->q.read_ptr != (scd_ssn & 0xff)) {
3164 int freed;
Tomas Winklerc54b6792008-03-06 17:36:53 -08003165 index = iwl_queue_dec_wrap(scd_ssn & 0xff, txq->q.n_bd);
Zhu Yib481de92007-09-25 17:54:57 -07003166 IWL_DEBUG_TX_REPLY("Retry scheduler reclaim scd_ssn "
3167 "%d index %d\n", scd_ssn , index);
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003168 freed = iwl4965_tx_queue_reclaim(priv, txq_id, index);
3169 priv->stations[sta_id].tid[tid].tfds_in_queue -= freed;
3170
3171 if (iwl4965_queue_space(&txq->q) > txq->q.low_mark &&
3172 txq_id >= 0 && priv->mac80211_registered &&
3173 agg->state != IWL_EMPTYING_HW_QUEUE_DELBA)
3174 ieee80211_wake_queue(priv->hw, txq_id);
3175
3176 iwl4965_check_empty_hw_queue(priv, sta_id, tid, txq_id);
Zhu Yib481de92007-09-25 17:54:57 -07003177 }
3178 } else {
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08003179#endif /* CONFIG_IWL4965_HT */
Tomas Winklerfc4b6852007-10-25 17:15:24 +08003180 tx_status = &(txq->txb[txq->q.read_ptr].status);
Zhu Yib481de92007-09-25 17:54:57 -07003181
3182 tx_status->retry_count = tx_resp->failure_frame;
3183 tx_status->queue_number = status;
3184 tx_status->queue_length = tx_resp->bt_kill_count;
3185 tx_status->queue_length |= tx_resp->failure_rts;
Zhu Yib481de92007-09-25 17:54:57 -07003186 tx_status->flags =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003187 iwl4965_is_tx_success(status) ? IEEE80211_TX_STATUS_ACK : 0;
Ron Rindjunsky4c424e42008-03-04 18:09:27 -08003188 iwl4965_hwrate_to_tx_control(priv, le32_to_cpu(tx_resp->rate_n_flags),
3189 &tx_status->control);
Zhu Yib481de92007-09-25 17:54:57 -07003190
Zhu Yib481de92007-09-25 17:54:57 -07003191 IWL_DEBUG_TX("Tx queue %d Status %s (0x%08x) rate_n_flags 0x%x "
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003192 "retries %d\n", txq_id, iwl4965_get_tx_fail_reason(status),
Zhu Yib481de92007-09-25 17:54:57 -07003193 status, le32_to_cpu(tx_resp->rate_n_flags),
3194 tx_resp->failure_frame);
3195
3196 IWL_DEBUG_TX_REPLY("Tx queue reclaim %d\n", index);
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003197 if (index != -1) {
3198 int freed = iwl4965_tx_queue_reclaim(priv, txq_id, index);
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08003199#ifdef CONFIG_IWL4965_HT
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003200 if (tid != MAX_TID_COUNT)
3201 priv->stations[sta_id].tid[tid].tfds_in_queue -= freed;
3202 if (iwl4965_queue_space(&txq->q) > txq->q.low_mark &&
3203 (txq_id >= 0) &&
3204 priv->mac80211_registered)
3205 ieee80211_wake_queue(priv->hw, txq_id);
3206 if (tid != MAX_TID_COUNT)
3207 iwl4965_check_empty_hw_queue(priv, sta_id, tid, txq_id);
3208#endif
Zhu Yib481de92007-09-25 17:54:57 -07003209 }
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003210#ifdef CONFIG_IWL4965_HT
3211 }
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08003212#endif /* CONFIG_IWL4965_HT */
Zhu Yib481de92007-09-25 17:54:57 -07003213
3214 if (iwl_check_bits(status, TX_ABORT_REQUIRED_MSK))
3215 IWL_ERROR("TODO: Implement Tx ABORT REQUIRED!!!\n");
3216}
3217
3218
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003219static void iwl4965_rx_reply_alive(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003220 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003221{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003222 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
3223 struct iwl4965_alive_resp *palive;
Zhu Yib481de92007-09-25 17:54:57 -07003224 struct delayed_work *pwork;
3225
3226 palive = &pkt->u.alive_frame;
3227
3228 IWL_DEBUG_INFO("Alive ucode status 0x%08X revision "
3229 "0x%01X 0x%01X\n",
3230 palive->is_valid, palive->ver_type,
3231 palive->ver_subtype);
3232
3233 if (palive->ver_subtype == INITIALIZE_SUBTYPE) {
3234 IWL_DEBUG_INFO("Initialization Alive received.\n");
3235 memcpy(&priv->card_alive_init,
3236 &pkt->u.alive_frame,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003237 sizeof(struct iwl4965_init_alive_resp));
Zhu Yib481de92007-09-25 17:54:57 -07003238 pwork = &priv->init_alive_start;
3239 } else {
3240 IWL_DEBUG_INFO("Runtime Alive received.\n");
3241 memcpy(&priv->card_alive, &pkt->u.alive_frame,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003242 sizeof(struct iwl4965_alive_resp));
Zhu Yib481de92007-09-25 17:54:57 -07003243 pwork = &priv->alive_start;
3244 }
3245
3246 /* We delay the ALIVE response by 5ms to
3247 * give the HW RF Kill time to activate... */
3248 if (palive->is_valid == UCODE_VALID_OK)
3249 queue_delayed_work(priv->workqueue, pwork,
3250 msecs_to_jiffies(5));
3251 else
3252 IWL_WARNING("uCode did not respond OK.\n");
3253}
3254
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003255static void iwl4965_rx_reply_add_sta(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003256 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003257{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003258 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07003259
3260 IWL_DEBUG_RX("Received REPLY_ADD_STA: 0x%02X\n", pkt->u.status);
3261 return;
3262}
3263
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003264static void iwl4965_rx_reply_error(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003265 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003266{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003267 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07003268
3269 IWL_ERROR("Error Reply type 0x%08X cmd %s (0x%02X) "
3270 "seq 0x%04X ser 0x%08X\n",
3271 le32_to_cpu(pkt->u.err_resp.error_type),
3272 get_cmd_string(pkt->u.err_resp.cmd_id),
3273 pkt->u.err_resp.cmd_id,
3274 le16_to_cpu(pkt->u.err_resp.bad_cmd_seq_num),
3275 le32_to_cpu(pkt->u.err_resp.error_info));
3276}
3277
3278#define TX_STATUS_ENTRY(x) case TX_STATUS_FAIL_ ## x: return #x
3279
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003280static void iwl4965_rx_csa(struct iwl_priv *priv, 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_rxon_cmd *rxon = (void *)&priv->active_rxon;
3284 struct iwl4965_csa_notification *csa = &(pkt->u.csa_notif);
Zhu Yib481de92007-09-25 17:54:57 -07003285 IWL_DEBUG_11H("CSA notif: channel %d, status %d\n",
3286 le16_to_cpu(csa->channel), le32_to_cpu(csa->status));
3287 rxon->channel = csa->channel;
3288 priv->staging_rxon.channel = csa->channel;
3289}
3290
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003291static void iwl4965_rx_spectrum_measure_notif(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003292 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003293{
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08003294#ifdef CONFIG_IWL4965_SPECTRUM_MEASUREMENT
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003295 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
3296 struct iwl4965_spectrum_notification *report = &(pkt->u.spectrum_notif);
Zhu Yib481de92007-09-25 17:54:57 -07003297
3298 if (!report->state) {
3299 IWL_DEBUG(IWL_DL_11H | IWL_DL_INFO,
3300 "Spectrum Measure Notification: Start\n");
3301 return;
3302 }
3303
3304 memcpy(&priv->measure_report, report, sizeof(*report));
3305 priv->measurement_status |= MEASUREMENT_READY;
3306#endif
3307}
3308
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003309static void iwl4965_rx_pm_sleep_notif(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003310 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003311{
Tomas Winkler0a6857e2008-03-12 16:58:49 -07003312#ifdef CONFIG_IWLWIFI_DEBUG
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003313 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
3314 struct iwl4965_sleep_notification *sleep = &(pkt->u.sleep_notif);
Zhu Yib481de92007-09-25 17:54:57 -07003315 IWL_DEBUG_RX("sleep mode: %d, src: %d\n",
3316 sleep->pm_sleep_mode, sleep->pm_wakeup_src);
3317#endif
3318}
3319
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003320static void iwl4965_rx_pm_debug_statistics_notif(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003321 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003322{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003323 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07003324 IWL_DEBUG_RADIO("Dumping %d bytes of unhandled "
3325 "notification for %s:\n",
3326 le32_to_cpu(pkt->len), get_cmd_string(pkt->hdr.cmd));
Tomas Winkler0a6857e2008-03-12 16:58:49 -07003327 iwl_print_hex_dump(IWL_DL_RADIO, pkt->u.raw, le32_to_cpu(pkt->len));
Zhu Yib481de92007-09-25 17:54:57 -07003328}
3329
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003330static void iwl4965_bg_beacon_update(struct work_struct *work)
Zhu Yib481de92007-09-25 17:54:57 -07003331{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003332 struct iwl_priv *priv =
3333 container_of(work, struct iwl_priv, beacon_update);
Zhu Yib481de92007-09-25 17:54:57 -07003334 struct sk_buff *beacon;
3335
3336 /* Pull updated AP beacon from mac80211. will fail if not in AP mode */
Johannes Berg32bfd352007-12-19 01:31:26 +01003337 beacon = ieee80211_beacon_get(priv->hw, priv->vif, NULL);
Zhu Yib481de92007-09-25 17:54:57 -07003338
3339 if (!beacon) {
3340 IWL_ERROR("update beacon failed\n");
3341 return;
3342 }
3343
3344 mutex_lock(&priv->mutex);
3345 /* new beacon skb is allocated every time; dispose previous.*/
3346 if (priv->ibss_beacon)
3347 dev_kfree_skb(priv->ibss_beacon);
3348
3349 priv->ibss_beacon = beacon;
3350 mutex_unlock(&priv->mutex);
3351
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003352 iwl4965_send_beacon_cmd(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003353}
3354
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003355static void iwl4965_rx_beacon_notif(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003356 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003357{
Tomas Winkler0a6857e2008-03-12 16:58:49 -07003358#ifdef CONFIG_IWLWIFI_DEBUG
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003359 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
3360 struct iwl4965_beacon_notif *beacon = &(pkt->u.beacon_status);
3361 u8 rate = iwl4965_hw_get_rate(beacon->beacon_notify_hdr.rate_n_flags);
Zhu Yib481de92007-09-25 17:54:57 -07003362
3363 IWL_DEBUG_RX("beacon status %x retries %d iss %d "
3364 "tsf %d %d rate %d\n",
3365 le32_to_cpu(beacon->beacon_notify_hdr.status) & TX_STATUS_MSK,
3366 beacon->beacon_notify_hdr.failure_frame,
3367 le32_to_cpu(beacon->ibss_mgr_status),
3368 le32_to_cpu(beacon->high_tsf),
3369 le32_to_cpu(beacon->low_tsf), rate);
3370#endif
3371
3372 if ((priv->iw_mode == IEEE80211_IF_TYPE_AP) &&
3373 (!test_bit(STATUS_EXIT_PENDING, &priv->status)))
3374 queue_work(priv->workqueue, &priv->beacon_update);
3375}
3376
3377/* Service response to REPLY_SCAN_CMD (0x80) */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003378static void iwl4965_rx_reply_scan(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003379 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003380{
Tomas Winkler0a6857e2008-03-12 16:58:49 -07003381#ifdef CONFIG_IWLWIFI_DEBUG
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003382 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
3383 struct iwl4965_scanreq_notification *notif =
3384 (struct iwl4965_scanreq_notification *)pkt->u.raw;
Zhu Yib481de92007-09-25 17:54:57 -07003385
3386 IWL_DEBUG_RX("Scan request status = 0x%x\n", notif->status);
3387#endif
3388}
3389
3390/* Service SCAN_START_NOTIFICATION (0x82) */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003391static void iwl4965_rx_scan_start_notif(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003392 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003393{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003394 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
3395 struct iwl4965_scanstart_notification *notif =
3396 (struct iwl4965_scanstart_notification *)pkt->u.raw;
Zhu Yib481de92007-09-25 17:54:57 -07003397 priv->scan_start_tsf = le32_to_cpu(notif->tsf_low);
3398 IWL_DEBUG_SCAN("Scan start: "
3399 "%d [802.11%s] "
3400 "(TSF: 0x%08X:%08X) - %d (beacon timer %u)\n",
3401 notif->channel,
3402 notif->band ? "bg" : "a",
3403 notif->tsf_high,
3404 notif->tsf_low, notif->status, notif->beacon_timer);
3405}
3406
3407/* Service SCAN_RESULTS_NOTIFICATION (0x83) */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003408static void iwl4965_rx_scan_results_notif(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003409 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003410{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003411 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
3412 struct iwl4965_scanresults_notification *notif =
3413 (struct iwl4965_scanresults_notification *)pkt->u.raw;
Zhu Yib481de92007-09-25 17:54:57 -07003414
3415 IWL_DEBUG_SCAN("Scan ch.res: "
3416 "%d [802.11%s] "
3417 "(TSF: 0x%08X:%08X) - %d "
3418 "elapsed=%lu usec (%dms since last)\n",
3419 notif->channel,
3420 notif->band ? "bg" : "a",
3421 le32_to_cpu(notif->tsf_high),
3422 le32_to_cpu(notif->tsf_low),
3423 le32_to_cpu(notif->statistics[0]),
3424 le32_to_cpu(notif->tsf_low) - priv->scan_start_tsf,
3425 jiffies_to_msecs(elapsed_jiffies
3426 (priv->last_scan_jiffies, jiffies)));
3427
3428 priv->last_scan_jiffies = jiffies;
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08003429 priv->next_scan_jiffies = 0;
Zhu Yib481de92007-09-25 17:54:57 -07003430}
3431
3432/* Service SCAN_COMPLETE_NOTIFICATION (0x84) */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003433static void iwl4965_rx_scan_complete_notif(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003434 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003435{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003436 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
3437 struct iwl4965_scancomplete_notification *scan_notif = (void *)pkt->u.raw;
Zhu Yib481de92007-09-25 17:54:57 -07003438
3439 IWL_DEBUG_SCAN("Scan complete: %d channels (TSF 0x%08X:%08X) - %d\n",
3440 scan_notif->scanned_channels,
3441 scan_notif->tsf_low,
3442 scan_notif->tsf_high, scan_notif->status);
3443
3444 /* The HW is no longer scanning */
3445 clear_bit(STATUS_SCAN_HW, &priv->status);
3446
3447 /* The scan completion notification came in, so kill that timer... */
3448 cancel_delayed_work(&priv->scan_check);
3449
3450 IWL_DEBUG_INFO("Scan pass on %sGHz took %dms\n",
3451 (priv->scan_bands == 2) ? "2.4" : "5.2",
3452 jiffies_to_msecs(elapsed_jiffies
3453 (priv->scan_pass_start, jiffies)));
3454
3455 /* Remove this scanned band from the list
3456 * of pending bands to scan */
3457 priv->scan_bands--;
3458
3459 /* If a request to abort was given, or the scan did not succeed
3460 * then we reset the scan state machine and terminate,
3461 * re-queuing another scan if one has been requested */
3462 if (test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
3463 IWL_DEBUG_INFO("Aborted scan completed.\n");
3464 clear_bit(STATUS_SCAN_ABORTING, &priv->status);
3465 } else {
3466 /* If there are more bands on this scan pass reschedule */
3467 if (priv->scan_bands > 0)
3468 goto reschedule;
3469 }
3470
3471 priv->last_scan_jiffies = jiffies;
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08003472 priv->next_scan_jiffies = 0;
Zhu Yib481de92007-09-25 17:54:57 -07003473 IWL_DEBUG_INFO("Setting scan to off\n");
3474
3475 clear_bit(STATUS_SCANNING, &priv->status);
3476
3477 IWL_DEBUG_INFO("Scan took %dms\n",
3478 jiffies_to_msecs(elapsed_jiffies(priv->scan_start, jiffies)));
3479
3480 queue_work(priv->workqueue, &priv->scan_completed);
3481
3482 return;
3483
3484reschedule:
3485 priv->scan_pass_start = jiffies;
3486 queue_work(priv->workqueue, &priv->request_scan);
3487}
3488
3489/* Handle notification from uCode that card's power state is changing
3490 * due to software, hardware, or critical temperature RFKILL */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003491static void iwl4965_rx_card_state_notif(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003492 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003493{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003494 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07003495 u32 flags = le32_to_cpu(pkt->u.card_state_notif.flags);
3496 unsigned long status = priv->status;
3497
3498 IWL_DEBUG_RF_KILL("Card state received: HW:%s SW:%s\n",
3499 (flags & HW_CARD_DISABLED) ? "Kill" : "On",
3500 (flags & SW_CARD_DISABLED) ? "Kill" : "On");
3501
3502 if (flags & (SW_CARD_DISABLED | HW_CARD_DISABLED |
3503 RF_CARD_DISABLED)) {
3504
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003505 iwl_write32(priv, CSR_UCODE_DRV_GP1_SET,
Zhu Yib481de92007-09-25 17:54:57 -07003506 CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
3507
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003508 if (!iwl_grab_nic_access(priv)) {
3509 iwl_write_direct32(
Zhu Yib481de92007-09-25 17:54:57 -07003510 priv, HBUS_TARG_MBX_C,
3511 HBUS_TARG_MBX_C_REG_BIT_CMD_BLOCKED);
3512
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003513 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003514 }
3515
3516 if (!(flags & RXON_CARD_DISABLED)) {
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003517 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR,
Zhu Yib481de92007-09-25 17:54:57 -07003518 CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003519 if (!iwl_grab_nic_access(priv)) {
3520 iwl_write_direct32(
Zhu Yib481de92007-09-25 17:54:57 -07003521 priv, HBUS_TARG_MBX_C,
3522 HBUS_TARG_MBX_C_REG_BIT_CMD_BLOCKED);
3523
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003524 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003525 }
3526 }
3527
3528 if (flags & RF_CARD_DISABLED) {
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003529 iwl_write32(priv, CSR_UCODE_DRV_GP1_SET,
Zhu Yib481de92007-09-25 17:54:57 -07003530 CSR_UCODE_DRV_GP1_REG_BIT_CT_KILL_EXIT);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003531 iwl_read32(priv, CSR_UCODE_DRV_GP1);
3532 if (!iwl_grab_nic_access(priv))
3533 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003534 }
3535 }
3536
3537 if (flags & HW_CARD_DISABLED)
3538 set_bit(STATUS_RF_KILL_HW, &priv->status);
3539 else
3540 clear_bit(STATUS_RF_KILL_HW, &priv->status);
3541
3542
3543 if (flags & SW_CARD_DISABLED)
3544 set_bit(STATUS_RF_KILL_SW, &priv->status);
3545 else
3546 clear_bit(STATUS_RF_KILL_SW, &priv->status);
3547
3548 if (!(flags & RXON_CARD_DISABLED))
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003549 iwl4965_scan_cancel(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003550
3551 if ((test_bit(STATUS_RF_KILL_HW, &status) !=
3552 test_bit(STATUS_RF_KILL_HW, &priv->status)) ||
3553 (test_bit(STATUS_RF_KILL_SW, &status) !=
3554 test_bit(STATUS_RF_KILL_SW, &priv->status)))
3555 queue_work(priv->workqueue, &priv->rf_kill);
3556 else
3557 wake_up_interruptible(&priv->wait_command_queue);
3558}
3559
3560/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003561 * iwl4965_setup_rx_handlers - Initialize Rx handler callbacks
Zhu Yib481de92007-09-25 17:54:57 -07003562 *
3563 * Setup the RX handlers for each of the reply types sent from the uCode
3564 * to the host.
3565 *
3566 * This function chains into the hardware specific files for them to setup
3567 * any hardware specific handlers as well.
3568 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003569static void iwl4965_setup_rx_handlers(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003570{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003571 priv->rx_handlers[REPLY_ALIVE] = iwl4965_rx_reply_alive;
3572 priv->rx_handlers[REPLY_ADD_STA] = iwl4965_rx_reply_add_sta;
3573 priv->rx_handlers[REPLY_ERROR] = iwl4965_rx_reply_error;
3574 priv->rx_handlers[CHANNEL_SWITCH_NOTIFICATION] = iwl4965_rx_csa;
Zhu Yib481de92007-09-25 17:54:57 -07003575 priv->rx_handlers[SPECTRUM_MEASURE_NOTIFICATION] =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003576 iwl4965_rx_spectrum_measure_notif;
3577 priv->rx_handlers[PM_SLEEP_NOTIFICATION] = iwl4965_rx_pm_sleep_notif;
Zhu Yib481de92007-09-25 17:54:57 -07003578 priv->rx_handlers[PM_DEBUG_STATISTIC_NOTIFIC] =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003579 iwl4965_rx_pm_debug_statistics_notif;
3580 priv->rx_handlers[BEACON_NOTIFICATION] = iwl4965_rx_beacon_notif;
Zhu Yib481de92007-09-25 17:54:57 -07003581
Ben Cahill9fbab512007-11-29 11:09:47 +08003582 /*
3583 * The same handler is used for both the REPLY to a discrete
3584 * statistics request from the host as well as for the periodic
3585 * statistics notifications (after received beacons) from the uCode.
Zhu Yib481de92007-09-25 17:54:57 -07003586 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003587 priv->rx_handlers[REPLY_STATISTICS_CMD] = iwl4965_hw_rx_statistics;
3588 priv->rx_handlers[STATISTICS_NOTIFICATION] = iwl4965_hw_rx_statistics;
Zhu Yib481de92007-09-25 17:54:57 -07003589
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003590 priv->rx_handlers[REPLY_SCAN_CMD] = iwl4965_rx_reply_scan;
3591 priv->rx_handlers[SCAN_START_NOTIFICATION] = iwl4965_rx_scan_start_notif;
Zhu Yib481de92007-09-25 17:54:57 -07003592 priv->rx_handlers[SCAN_RESULTS_NOTIFICATION] =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003593 iwl4965_rx_scan_results_notif;
Zhu Yib481de92007-09-25 17:54:57 -07003594 priv->rx_handlers[SCAN_COMPLETE_NOTIFICATION] =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003595 iwl4965_rx_scan_complete_notif;
3596 priv->rx_handlers[CARD_STATE_NOTIFICATION] = iwl4965_rx_card_state_notif;
3597 priv->rx_handlers[REPLY_TX] = iwl4965_rx_reply_tx;
Zhu Yib481de92007-09-25 17:54:57 -07003598
Ben Cahill9fbab512007-11-29 11:09:47 +08003599 /* Set up hardware specific Rx handlers */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003600 iwl4965_hw_rx_handler_setup(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003601}
3602
3603/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003604 * iwl4965_tx_cmd_complete - Pull unused buffers off the queue and reclaim them
Zhu Yib481de92007-09-25 17:54:57 -07003605 * @rxb: Rx buffer to reclaim
3606 *
3607 * If an Rx buffer has an async callback associated with it the callback
3608 * will be executed. The attached skb (if present) will only be freed
3609 * if the callback returns 1
3610 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003611static void iwl4965_tx_cmd_complete(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003612 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003613{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003614 struct iwl4965_rx_packet *pkt = (struct iwl4965_rx_packet *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07003615 u16 sequence = le16_to_cpu(pkt->hdr.sequence);
3616 int txq_id = SEQ_TO_QUEUE(sequence);
3617 int index = SEQ_TO_INDEX(sequence);
3618 int huge = sequence & SEQ_HUGE_FRAME;
3619 int cmd_index;
Tomas Winkler857485c2008-03-21 13:53:44 -07003620 struct iwl_cmd *cmd;
Zhu Yib481de92007-09-25 17:54:57 -07003621
3622 /* If a Tx command is being handled and it isn't in the actual
3623 * command queue then there a command routing bug has been introduced
3624 * in the queue management code. */
3625 if (txq_id != IWL_CMD_QUEUE_NUM)
3626 IWL_ERROR("Error wrong command queue %d command id 0x%X\n",
3627 txq_id, pkt->hdr.cmd);
3628 BUG_ON(txq_id != IWL_CMD_QUEUE_NUM);
3629
3630 cmd_index = get_cmd_index(&priv->txq[IWL_CMD_QUEUE_NUM].q, index, huge);
3631 cmd = &priv->txq[IWL_CMD_QUEUE_NUM].cmd[cmd_index];
3632
3633 /* Input error checking is done when commands are added to queue. */
3634 if (cmd->meta.flags & CMD_WANT_SKB) {
3635 cmd->meta.source->u.skb = rxb->skb;
3636 rxb->skb = NULL;
3637 } else if (cmd->meta.u.callback &&
3638 !cmd->meta.u.callback(priv, cmd, rxb->skb))
3639 rxb->skb = NULL;
3640
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003641 iwl4965_tx_queue_reclaim(priv, txq_id, index);
Zhu Yib481de92007-09-25 17:54:57 -07003642
3643 if (!(cmd->meta.flags & CMD_ASYNC)) {
3644 clear_bit(STATUS_HCMD_ACTIVE, &priv->status);
3645 wake_up_interruptible(&priv->wait_command_queue);
3646 }
3647}
3648
3649/************************** RX-FUNCTIONS ****************************/
3650/*
3651 * Rx theory of operation
3652 *
Ben Cahill9fbab512007-11-29 11:09:47 +08003653 * Driver allocates a circular buffer of Receive Buffer Descriptors (RBDs),
3654 * each of which point to Receive Buffers to be filled by 4965. These get
3655 * used not only for Rx frames, but for any command response or notification
3656 * from the 4965. The driver and 4965 manage the Rx buffers by means
3657 * of indexes into the circular buffer.
Zhu Yib481de92007-09-25 17:54:57 -07003658 *
3659 * Rx Queue Indexes
3660 * The host/firmware share two index registers for managing the Rx buffers.
3661 *
3662 * The READ index maps to the first position that the firmware may be writing
3663 * to -- the driver can read up to (but not including) this position and get
3664 * good data.
3665 * The READ index is managed by the firmware once the card is enabled.
3666 *
3667 * The WRITE index maps to the last position the driver has read from -- the
3668 * position preceding WRITE is the last slot the firmware can place a packet.
3669 *
3670 * The queue is empty (no good data) if WRITE = READ - 1, and is full if
3671 * WRITE = READ.
3672 *
Ben Cahill9fbab512007-11-29 11:09:47 +08003673 * During initialization, the host sets up the READ queue position to the first
Zhu Yib481de92007-09-25 17:54:57 -07003674 * INDEX position, and WRITE to the last (READ - 1 wrapped)
3675 *
Ben Cahill9fbab512007-11-29 11:09:47 +08003676 * When the firmware places a packet in a buffer, it will advance the READ index
Zhu Yib481de92007-09-25 17:54:57 -07003677 * and fire the RX interrupt. The driver can then query the READ index and
3678 * process as many packets as possible, moving the WRITE index forward as it
3679 * resets the Rx queue buffers with new memory.
3680 *
3681 * The management in the driver is as follows:
3682 * + A list of pre-allocated SKBs is stored in iwl->rxq->rx_free. When
3683 * iwl->rxq->free_count drops to or below RX_LOW_WATERMARK, work is scheduled
Ian Schram01ebd062007-10-25 17:15:22 +08003684 * to replenish the iwl->rxq->rx_free.
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003685 * + In iwl4965_rx_replenish (scheduled) if 'processed' != 'read' then the
Zhu Yib481de92007-09-25 17:54:57 -07003686 * iwl->rxq is replenished and the READ INDEX is updated (updating the
3687 * 'processed' and 'read' driver indexes as well)
3688 * + A received packet is processed and handed to the kernel network stack,
3689 * detached from the iwl->rxq. The driver 'processed' index is updated.
3690 * + The Host/Firmware iwl->rxq is replenished at tasklet time from the rx_free
3691 * list. If there are no allocated buffers in iwl->rxq->rx_free, the READ
3692 * INDEX is not incremented and iwl->status(RX_STALLED) is set. If there
3693 * were enough free buffers and RX_STALLED is set it is cleared.
3694 *
3695 *
3696 * Driver sequence:
3697 *
Ben Cahill9fbab512007-11-29 11:09:47 +08003698 * iwl4965_rx_queue_alloc() Allocates rx_free
3699 * iwl4965_rx_replenish() Replenishes rx_free list from rx_used, and calls
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003700 * iwl4965_rx_queue_restock
Ben Cahill9fbab512007-11-29 11:09:47 +08003701 * iwl4965_rx_queue_restock() Moves available buffers from rx_free into Rx
Zhu Yib481de92007-09-25 17:54:57 -07003702 * queue, updates firmware pointers, and updates
3703 * the WRITE index. If insufficient rx_free buffers
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003704 * are available, schedules iwl4965_rx_replenish
Zhu Yib481de92007-09-25 17:54:57 -07003705 *
3706 * -- enable interrupts --
Ben Cahill9fbab512007-11-29 11:09:47 +08003707 * ISR - iwl4965_rx() Detach iwl4965_rx_mem_buffers from pool up to the
Zhu Yib481de92007-09-25 17:54:57 -07003708 * READ INDEX, detaching the SKB from the pool.
3709 * Moves the packet buffer from queue to rx_used.
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003710 * Calls iwl4965_rx_queue_restock to refill any empty
Zhu Yib481de92007-09-25 17:54:57 -07003711 * slots.
3712 * ...
3713 *
3714 */
3715
3716/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003717 * iwl4965_rx_queue_space - Return number of free slots available in queue.
Zhu Yib481de92007-09-25 17:54:57 -07003718 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003719static int iwl4965_rx_queue_space(const struct iwl4965_rx_queue *q)
Zhu Yib481de92007-09-25 17:54:57 -07003720{
3721 int s = q->read - q->write;
3722 if (s <= 0)
3723 s += RX_QUEUE_SIZE;
3724 /* keep some buffer to not confuse full and empty queue */
3725 s -= 2;
3726 if (s < 0)
3727 s = 0;
3728 return s;
3729}
3730
3731/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003732 * iwl4965_rx_queue_update_write_ptr - Update the write pointer for the RX queue
Zhu Yib481de92007-09-25 17:54:57 -07003733 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003734int iwl4965_rx_queue_update_write_ptr(struct iwl_priv *priv, struct iwl4965_rx_queue *q)
Zhu Yib481de92007-09-25 17:54:57 -07003735{
3736 u32 reg = 0;
3737 int rc = 0;
3738 unsigned long flags;
3739
3740 spin_lock_irqsave(&q->lock, flags);
3741
3742 if (q->need_update == 0)
3743 goto exit_unlock;
3744
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003745 /* If power-saving is in use, make sure device is awake */
Zhu Yib481de92007-09-25 17:54:57 -07003746 if (test_bit(STATUS_POWER_PMI, &priv->status)) {
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003747 reg = iwl_read32(priv, CSR_UCODE_DRV_GP1);
Zhu Yib481de92007-09-25 17:54:57 -07003748
3749 if (reg & CSR_UCODE_DRV_GP1_BIT_MAC_SLEEP) {
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003750 iwl_set_bit(priv, CSR_GP_CNTRL,
Zhu Yib481de92007-09-25 17:54:57 -07003751 CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
3752 goto exit_unlock;
3753 }
3754
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003755 rc = iwl_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003756 if (rc)
3757 goto exit_unlock;
3758
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003759 /* Device expects a multiple of 8 */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003760 iwl_write_direct32(priv, FH_RSCSR_CHNL0_WPTR,
Zhu Yib481de92007-09-25 17:54:57 -07003761 q->write & ~0x7);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003762 iwl_release_nic_access(priv);
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003763
3764 /* Else device is assumed to be awake */
Zhu Yib481de92007-09-25 17:54:57 -07003765 } else
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003766 /* Device expects a multiple of 8 */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003767 iwl_write32(priv, FH_RSCSR_CHNL0_WPTR, q->write & ~0x7);
Zhu Yib481de92007-09-25 17:54:57 -07003768
3769
3770 q->need_update = 0;
3771
3772 exit_unlock:
3773 spin_unlock_irqrestore(&q->lock, flags);
3774 return rc;
3775}
3776
3777/**
Ben Cahill9fbab512007-11-29 11:09:47 +08003778 * iwl4965_dma_addr2rbd_ptr - convert a DMA address to a uCode read buffer ptr
Zhu Yib481de92007-09-25 17:54:57 -07003779 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003780static inline __le32 iwl4965_dma_addr2rbd_ptr(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07003781 dma_addr_t dma_addr)
3782{
3783 return cpu_to_le32((u32)(dma_addr >> 8));
3784}
3785
3786
3787/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003788 * iwl4965_rx_queue_restock - refill RX queue from pre-allocated pool
Zhu Yib481de92007-09-25 17:54:57 -07003789 *
Ben Cahill9fbab512007-11-29 11:09:47 +08003790 * If there are slots in the RX queue that need to be restocked,
Zhu Yib481de92007-09-25 17:54:57 -07003791 * and we have free pre-allocated buffers, fill the ranks as much
Ben Cahill9fbab512007-11-29 11:09:47 +08003792 * as we can, pulling from rx_free.
Zhu Yib481de92007-09-25 17:54:57 -07003793 *
3794 * This moves the 'write' index forward to catch up with 'processed', and
3795 * also updates the memory address in the firmware to reference the new
3796 * target buffer.
3797 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003798static int iwl4965_rx_queue_restock(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003799{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003800 struct iwl4965_rx_queue *rxq = &priv->rxq;
Zhu Yib481de92007-09-25 17:54:57 -07003801 struct list_head *element;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003802 struct iwl4965_rx_mem_buffer *rxb;
Zhu Yib481de92007-09-25 17:54:57 -07003803 unsigned long flags;
3804 int write, rc;
3805
3806 spin_lock_irqsave(&rxq->lock, flags);
3807 write = rxq->write & ~0x7;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003808 while ((iwl4965_rx_queue_space(rxq) > 0) && (rxq->free_count)) {
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003809 /* Get next free Rx buffer, remove from free list */
Zhu Yib481de92007-09-25 17:54:57 -07003810 element = rxq->rx_free.next;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003811 rxb = list_entry(element, struct iwl4965_rx_mem_buffer, list);
Zhu Yib481de92007-09-25 17:54:57 -07003812 list_del(element);
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003813
3814 /* Point to Rx buffer via next RBD in circular buffer */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003815 rxq->bd[rxq->write] = iwl4965_dma_addr2rbd_ptr(priv, rxb->dma_addr);
Zhu Yib481de92007-09-25 17:54:57 -07003816 rxq->queue[rxq->write] = rxb;
3817 rxq->write = (rxq->write + 1) & RX_QUEUE_MASK;
3818 rxq->free_count--;
3819 }
3820 spin_unlock_irqrestore(&rxq->lock, flags);
3821 /* If the pre-allocated buffer pool is dropping low, schedule to
3822 * refill it */
3823 if (rxq->free_count <= RX_LOW_WATERMARK)
3824 queue_work(priv->workqueue, &priv->rx_replenish);
3825
3826
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003827 /* If we've added more space for the firmware to place data, tell it.
3828 * Increment device's write pointer in multiples of 8. */
Zhu Yib481de92007-09-25 17:54:57 -07003829 if ((write != (rxq->write & ~0x7))
3830 || (abs(rxq->write - rxq->read) > 7)) {
3831 spin_lock_irqsave(&rxq->lock, flags);
3832 rxq->need_update = 1;
3833 spin_unlock_irqrestore(&rxq->lock, flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003834 rc = iwl4965_rx_queue_update_write_ptr(priv, rxq);
Zhu Yib481de92007-09-25 17:54:57 -07003835 if (rc)
3836 return rc;
3837 }
3838
3839 return 0;
3840}
3841
3842/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003843 * iwl4965_rx_replenish - Move all used packet from rx_used to rx_free
Zhu Yib481de92007-09-25 17:54:57 -07003844 *
3845 * When moving to rx_free an SKB is allocated for the slot.
3846 *
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003847 * Also restock the Rx queue via iwl4965_rx_queue_restock.
Ian Schram01ebd062007-10-25 17:15:22 +08003848 * This is called as a scheduled work item (except for during initialization)
Zhu Yib481de92007-09-25 17:54:57 -07003849 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003850static void iwl4965_rx_allocate(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003851{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003852 struct iwl4965_rx_queue *rxq = &priv->rxq;
Zhu Yib481de92007-09-25 17:54:57 -07003853 struct list_head *element;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003854 struct iwl4965_rx_mem_buffer *rxb;
Zhu Yib481de92007-09-25 17:54:57 -07003855 unsigned long flags;
3856 spin_lock_irqsave(&rxq->lock, flags);
3857 while (!list_empty(&rxq->rx_used)) {
3858 element = rxq->rx_used.next;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003859 rxb = list_entry(element, struct iwl4965_rx_mem_buffer, list);
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003860
3861 /* Alloc a new receive buffer */
Zhu Yib481de92007-09-25 17:54:57 -07003862 rxb->skb =
Ron Rindjunsky9ee1ba42007-11-26 16:14:42 +02003863 alloc_skb(priv->hw_setting.rx_buf_size,
3864 __GFP_NOWARN | GFP_ATOMIC);
Zhu Yib481de92007-09-25 17:54:57 -07003865 if (!rxb->skb) {
3866 if (net_ratelimit())
3867 printk(KERN_CRIT DRV_NAME
3868 ": Can not allocate SKB buffers\n");
3869 /* We don't reschedule replenish work here -- we will
3870 * call the restock method and if it still needs
3871 * more buffers it will schedule replenish */
3872 break;
3873 }
3874 priv->alloc_rxb_skb++;
3875 list_del(element);
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003876
3877 /* Get physical address of RB/SKB */
Zhu Yib481de92007-09-25 17:54:57 -07003878 rxb->dma_addr =
3879 pci_map_single(priv->pci_dev, rxb->skb->data,
Ron Rindjunsky9ee1ba42007-11-26 16:14:42 +02003880 priv->hw_setting.rx_buf_size, PCI_DMA_FROMDEVICE);
Zhu Yib481de92007-09-25 17:54:57 -07003881 list_add_tail(&rxb->list, &rxq->rx_free);
3882 rxq->free_count++;
3883 }
3884 spin_unlock_irqrestore(&rxq->lock, flags);
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08003885}
3886
3887/*
3888 * this should be called while priv->lock is locked
3889*/
Tomas Winkler4fd1f842007-12-05 20:59:58 +02003890static void __iwl4965_rx_replenish(void *data)
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08003891{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003892 struct iwl_priv *priv = data;
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08003893
3894 iwl4965_rx_allocate(priv);
3895 iwl4965_rx_queue_restock(priv);
3896}
3897
3898
3899void iwl4965_rx_replenish(void *data)
3900{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003901 struct iwl_priv *priv = data;
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08003902 unsigned long flags;
3903
3904 iwl4965_rx_allocate(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003905
3906 spin_lock_irqsave(&priv->lock, flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003907 iwl4965_rx_queue_restock(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003908 spin_unlock_irqrestore(&priv->lock, flags);
3909}
3910
3911/* Assumes that the skb field of the buffers in 'pool' is kept accurate.
Ben Cahill9fbab512007-11-29 11:09:47 +08003912 * If an SKB has been detached, the POOL needs to have its SKB set to NULL
Zhu Yib481de92007-09-25 17:54:57 -07003913 * This free routine walks the list of POOL entries and if SKB is set to
3914 * non NULL it is unmapped and freed
3915 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003916static void iwl4965_rx_queue_free(struct iwl_priv *priv, struct iwl4965_rx_queue *rxq)
Zhu Yib481de92007-09-25 17:54:57 -07003917{
3918 int i;
3919 for (i = 0; i < RX_QUEUE_SIZE + RX_FREE_BUFFERS; i++) {
3920 if (rxq->pool[i].skb != NULL) {
3921 pci_unmap_single(priv->pci_dev,
3922 rxq->pool[i].dma_addr,
Ron Rindjunsky9ee1ba42007-11-26 16:14:42 +02003923 priv->hw_setting.rx_buf_size,
3924 PCI_DMA_FROMDEVICE);
Zhu Yib481de92007-09-25 17:54:57 -07003925 dev_kfree_skb(rxq->pool[i].skb);
3926 }
3927 }
3928
3929 pci_free_consistent(priv->pci_dev, 4 * RX_QUEUE_SIZE, rxq->bd,
3930 rxq->dma_addr);
3931 rxq->bd = NULL;
3932}
3933
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003934int iwl4965_rx_queue_alloc(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003935{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003936 struct iwl4965_rx_queue *rxq = &priv->rxq;
Zhu Yib481de92007-09-25 17:54:57 -07003937 struct pci_dev *dev = priv->pci_dev;
3938 int i;
3939
3940 spin_lock_init(&rxq->lock);
3941 INIT_LIST_HEAD(&rxq->rx_free);
3942 INIT_LIST_HEAD(&rxq->rx_used);
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003943
3944 /* Alloc the circular buffer of Read Buffer Descriptors (RBDs) */
Zhu Yib481de92007-09-25 17:54:57 -07003945 rxq->bd = pci_alloc_consistent(dev, 4 * RX_QUEUE_SIZE, &rxq->dma_addr);
3946 if (!rxq->bd)
3947 return -ENOMEM;
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003948
Zhu Yib481de92007-09-25 17:54:57 -07003949 /* Fill the rx_used queue with _all_ of the Rx buffers */
3950 for (i = 0; i < RX_FREE_BUFFERS + RX_QUEUE_SIZE; i++)
3951 list_add_tail(&rxq->pool[i].list, &rxq->rx_used);
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003952
Zhu Yib481de92007-09-25 17:54:57 -07003953 /* Set us so that we have processed and used all buffers, but have
3954 * not restocked the Rx queue with fresh buffers */
3955 rxq->read = rxq->write = 0;
3956 rxq->free_count = 0;
3957 rxq->need_update = 0;
3958 return 0;
3959}
3960
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003961void iwl4965_rx_queue_reset(struct iwl_priv *priv, struct iwl4965_rx_queue *rxq)
Zhu Yib481de92007-09-25 17:54:57 -07003962{
3963 unsigned long flags;
3964 int i;
3965 spin_lock_irqsave(&rxq->lock, flags);
3966 INIT_LIST_HEAD(&rxq->rx_free);
3967 INIT_LIST_HEAD(&rxq->rx_used);
3968 /* Fill the rx_used queue with _all_ of the Rx buffers */
3969 for (i = 0; i < RX_FREE_BUFFERS + RX_QUEUE_SIZE; i++) {
3970 /* In the reset function, these buffers may have been allocated
3971 * to an SKB, so we need to unmap and free potential storage */
3972 if (rxq->pool[i].skb != NULL) {
3973 pci_unmap_single(priv->pci_dev,
3974 rxq->pool[i].dma_addr,
Ron Rindjunsky9ee1ba42007-11-26 16:14:42 +02003975 priv->hw_setting.rx_buf_size,
3976 PCI_DMA_FROMDEVICE);
Zhu Yib481de92007-09-25 17:54:57 -07003977 priv->alloc_rxb_skb--;
3978 dev_kfree_skb(rxq->pool[i].skb);
3979 rxq->pool[i].skb = NULL;
3980 }
3981 list_add_tail(&rxq->pool[i].list, &rxq->rx_used);
3982 }
3983
3984 /* Set us so that we have processed and used all buffers, but have
3985 * not restocked the Rx queue with fresh buffers */
3986 rxq->read = rxq->write = 0;
3987 rxq->free_count = 0;
3988 spin_unlock_irqrestore(&rxq->lock, flags);
3989}
3990
3991/* Convert linear signal-to-noise ratio into dB */
3992static u8 ratio2dB[100] = {
3993/* 0 1 2 3 4 5 6 7 8 9 */
3994 0, 0, 6, 10, 12, 14, 16, 17, 18, 19, /* 00 - 09 */
3995 20, 21, 22, 22, 23, 23, 24, 25, 26, 26, /* 10 - 19 */
3996 26, 26, 26, 27, 27, 28, 28, 28, 29, 29, /* 20 - 29 */
3997 29, 30, 30, 30, 31, 31, 31, 31, 32, 32, /* 30 - 39 */
3998 32, 32, 32, 33, 33, 33, 33, 33, 34, 34, /* 40 - 49 */
3999 34, 34, 34, 34, 35, 35, 35, 35, 35, 35, /* 50 - 59 */
4000 36, 36, 36, 36, 36, 36, 36, 37, 37, 37, /* 60 - 69 */
4001 37, 37, 37, 37, 37, 38, 38, 38, 38, 38, /* 70 - 79 */
4002 38, 38, 38, 38, 38, 39, 39, 39, 39, 39, /* 80 - 89 */
4003 39, 39, 39, 39, 39, 40, 40, 40, 40, 40 /* 90 - 99 */
4004};
4005
4006/* Calculates a relative dB value from a ratio of linear
4007 * (i.e. not dB) signal levels.
4008 * Conversion assumes that levels are voltages (20*log), not powers (10*log). */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004009int iwl4965_calc_db_from_ratio(int sig_ratio)
Zhu Yib481de92007-09-25 17:54:57 -07004010{
Adrian Bunkc899a572007-10-14 19:51:15 +02004011 /* 1000:1 or higher just report as 60 dB */
4012 if (sig_ratio >= 1000)
Zhu Yib481de92007-09-25 17:54:57 -07004013 return 60;
4014
Adrian Bunkc899a572007-10-14 19:51:15 +02004015 /* 100:1 or higher, divide by 10 and use table,
Zhu Yib481de92007-09-25 17:54:57 -07004016 * add 20 dB to make up for divide by 10 */
Adrian Bunkc899a572007-10-14 19:51:15 +02004017 if (sig_ratio >= 100)
Zhu Yib481de92007-09-25 17:54:57 -07004018 return (20 + (int)ratio2dB[sig_ratio/10]);
4019
4020 /* We shouldn't see this */
4021 if (sig_ratio < 1)
4022 return 0;
4023
4024 /* Use table for ratios 1:1 - 99:1 */
4025 return (int)ratio2dB[sig_ratio];
4026}
4027
4028#define PERFECT_RSSI (-20) /* dBm */
4029#define WORST_RSSI (-95) /* dBm */
4030#define RSSI_RANGE (PERFECT_RSSI - WORST_RSSI)
4031
4032/* Calculate an indication of rx signal quality (a percentage, not dBm!).
4033 * See http://www.ces.clemson.edu/linux/signal_quality.shtml for info
4034 * about formulas used below. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004035int iwl4965_calc_sig_qual(int rssi_dbm, int noise_dbm)
Zhu Yib481de92007-09-25 17:54:57 -07004036{
4037 int sig_qual;
4038 int degradation = PERFECT_RSSI - rssi_dbm;
4039
4040 /* If we get a noise measurement, use signal-to-noise ratio (SNR)
4041 * as indicator; formula is (signal dbm - noise dbm).
4042 * SNR at or above 40 is a great signal (100%).
4043 * Below that, scale to fit SNR of 0 - 40 dB within 0 - 100% indicator.
4044 * Weakest usable signal is usually 10 - 15 dB SNR. */
4045 if (noise_dbm) {
4046 if (rssi_dbm - noise_dbm >= 40)
4047 return 100;
4048 else if (rssi_dbm < noise_dbm)
4049 return 0;
4050 sig_qual = ((rssi_dbm - noise_dbm) * 5) / 2;
4051
4052 /* Else use just the signal level.
4053 * This formula is a least squares fit of data points collected and
4054 * compared with a reference system that had a percentage (%) display
4055 * for signal quality. */
4056 } else
4057 sig_qual = (100 * (RSSI_RANGE * RSSI_RANGE) - degradation *
4058 (15 * RSSI_RANGE + 62 * degradation)) /
4059 (RSSI_RANGE * RSSI_RANGE);
4060
4061 if (sig_qual > 100)
4062 sig_qual = 100;
4063 else if (sig_qual < 1)
4064 sig_qual = 0;
4065
4066 return sig_qual;
4067}
4068
4069/**
Ben Cahill9fbab512007-11-29 11:09:47 +08004070 * iwl4965_rx_handle - Main entry function for receiving responses from uCode
Zhu Yib481de92007-09-25 17:54:57 -07004071 *
4072 * Uses the priv->rx_handlers callback function array to invoke
4073 * the appropriate handlers, including command responses,
4074 * frame-received notifications, and other notifications.
4075 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004076static void iwl4965_rx_handle(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004077{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004078 struct iwl4965_rx_mem_buffer *rxb;
4079 struct iwl4965_rx_packet *pkt;
4080 struct iwl4965_rx_queue *rxq = &priv->rxq;
Zhu Yib481de92007-09-25 17:54:57 -07004081 u32 r, i;
4082 int reclaim;
4083 unsigned long flags;
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08004084 u8 fill_rx = 0;
Mohamed Abbasd68ab682008-02-07 13:16:33 -08004085 u32 count = 8;
Zhu Yib481de92007-09-25 17:54:57 -07004086
Cahill, Ben M6440adb2007-11-29 11:09:55 +08004087 /* uCode's read index (stored in shared DRAM) indicates the last Rx
4088 * buffer that the driver may process (last buffer filled by ucode). */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004089 r = iwl4965_hw_get_rx_read(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004090 i = rxq->read;
4091
4092 /* Rx interrupt, but nothing sent from uCode */
4093 if (i == r)
4094 IWL_DEBUG(IWL_DL_RX | IWL_DL_ISR, "r = %d, i = %d\n", r, i);
4095
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08004096 if (iwl4965_rx_queue_space(rxq) > (RX_QUEUE_SIZE / 2))
4097 fill_rx = 1;
4098
Zhu Yib481de92007-09-25 17:54:57 -07004099 while (i != r) {
4100 rxb = rxq->queue[i];
4101
Ben Cahill9fbab512007-11-29 11:09:47 +08004102 /* If an RXB doesn't have a Rx queue slot associated with it,
Zhu Yib481de92007-09-25 17:54:57 -07004103 * then a bug has been introduced in the queue refilling
4104 * routines -- catch it here */
4105 BUG_ON(rxb == NULL);
4106
4107 rxq->queue[i] = NULL;
4108
4109 pci_dma_sync_single_for_cpu(priv->pci_dev, rxb->dma_addr,
Ron Rindjunsky9ee1ba42007-11-26 16:14:42 +02004110 priv->hw_setting.rx_buf_size,
Zhu Yib481de92007-09-25 17:54:57 -07004111 PCI_DMA_FROMDEVICE);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004112 pkt = (struct iwl4965_rx_packet *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07004113
4114 /* Reclaim a command buffer only if this packet is a response
4115 * to a (driver-originated) command.
4116 * If the packet (e.g. Rx frame) originated from uCode,
4117 * there is no command buffer to reclaim.
4118 * Ucode should set SEQ_RX_FRAME bit if ucode-originated,
4119 * but apparently a few don't get set; catch them here. */
4120 reclaim = !(pkt->hdr.sequence & SEQ_RX_FRAME) &&
4121 (pkt->hdr.cmd != REPLY_RX_PHY_CMD) &&
Tomas Winkler857485c2008-03-21 13:53:44 -07004122 (pkt->hdr.cmd != REPLY_RX) &&
Zhu Yicfe01702007-09-27 11:27:31 +08004123 (pkt->hdr.cmd != REPLY_COMPRESSED_BA) &&
Zhu Yib481de92007-09-25 17:54:57 -07004124 (pkt->hdr.cmd != STATISTICS_NOTIFICATION) &&
4125 (pkt->hdr.cmd != REPLY_TX);
4126
4127 /* Based on type of command response or notification,
4128 * handle those that need handling via function in
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004129 * rx_handlers table. See iwl4965_setup_rx_handlers() */
Zhu Yib481de92007-09-25 17:54:57 -07004130 if (priv->rx_handlers[pkt->hdr.cmd]) {
4131 IWL_DEBUG(IWL_DL_HOST_COMMAND | IWL_DL_RX | IWL_DL_ISR,
4132 "r = %d, i = %d, %s, 0x%02x\n", r, i,
4133 get_cmd_string(pkt->hdr.cmd), pkt->hdr.cmd);
4134 priv->rx_handlers[pkt->hdr.cmd] (priv, rxb);
4135 } else {
4136 /* No handling needed */
4137 IWL_DEBUG(IWL_DL_HOST_COMMAND | IWL_DL_RX | IWL_DL_ISR,
4138 "r %d i %d No handler needed for %s, 0x%02x\n",
4139 r, i, get_cmd_string(pkt->hdr.cmd),
4140 pkt->hdr.cmd);
4141 }
4142
4143 if (reclaim) {
Ben Cahill9fbab512007-11-29 11:09:47 +08004144 /* Invoke any callbacks, transfer the skb to caller, and
Tomas Winkler857485c2008-03-21 13:53:44 -07004145 * fire off the (possibly) blocking iwl_send_cmd()
Zhu Yib481de92007-09-25 17:54:57 -07004146 * as we reclaim the driver command queue */
4147 if (rxb && rxb->skb)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004148 iwl4965_tx_cmd_complete(priv, rxb);
Zhu Yib481de92007-09-25 17:54:57 -07004149 else
4150 IWL_WARNING("Claim null rxb?\n");
4151 }
4152
4153 /* For now we just don't re-use anything. We can tweak this
4154 * later to try and re-use notification packets and SKBs that
4155 * fail to Rx correctly */
4156 if (rxb->skb != NULL) {
4157 priv->alloc_rxb_skb--;
4158 dev_kfree_skb_any(rxb->skb);
4159 rxb->skb = NULL;
4160 }
4161
4162 pci_unmap_single(priv->pci_dev, rxb->dma_addr,
Ron Rindjunsky9ee1ba42007-11-26 16:14:42 +02004163 priv->hw_setting.rx_buf_size,
4164 PCI_DMA_FROMDEVICE);
Zhu Yib481de92007-09-25 17:54:57 -07004165 spin_lock_irqsave(&rxq->lock, flags);
4166 list_add_tail(&rxb->list, &priv->rxq.rx_used);
4167 spin_unlock_irqrestore(&rxq->lock, flags);
4168 i = (i + 1) & RX_QUEUE_MASK;
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08004169 /* If there are a lot of unused frames,
4170 * restock the Rx queue so ucode wont assert. */
4171 if (fill_rx) {
4172 count++;
4173 if (count >= 8) {
4174 priv->rxq.read = i;
4175 __iwl4965_rx_replenish(priv);
4176 count = 0;
4177 }
4178 }
Zhu Yib481de92007-09-25 17:54:57 -07004179 }
4180
4181 /* Backtrack one entry */
4182 priv->rxq.read = i;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004183 iwl4965_rx_queue_restock(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004184}
4185
Cahill, Ben M6440adb2007-11-29 11:09:55 +08004186/**
4187 * iwl4965_tx_queue_update_write_ptr - Send new write index to hardware
4188 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004189static int iwl4965_tx_queue_update_write_ptr(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004190 struct iwl4965_tx_queue *txq)
Zhu Yib481de92007-09-25 17:54:57 -07004191{
4192 u32 reg = 0;
4193 int rc = 0;
4194 int txq_id = txq->q.id;
4195
4196 if (txq->need_update == 0)
4197 return rc;
4198
4199 /* if we're trying to save power */
4200 if (test_bit(STATUS_POWER_PMI, &priv->status)) {
4201 /* wake up nic if it's powered down ...
4202 * uCode will wake up, and interrupt us again, so next
4203 * time we'll skip this part. */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004204 reg = iwl_read32(priv, CSR_UCODE_DRV_GP1);
Zhu Yib481de92007-09-25 17:54:57 -07004205
4206 if (reg & CSR_UCODE_DRV_GP1_BIT_MAC_SLEEP) {
4207 IWL_DEBUG_INFO("Requesting wakeup, GP1 = 0x%x\n", reg);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004208 iwl_set_bit(priv, CSR_GP_CNTRL,
Zhu Yib481de92007-09-25 17:54:57 -07004209 CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
4210 return rc;
4211 }
4212
4213 /* restore this queue's parameters in nic hardware. */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004214 rc = iwl_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004215 if (rc)
4216 return rc;
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004217 iwl_write_direct32(priv, HBUS_TARG_WRPTR,
Tomas Winklerfc4b6852007-10-25 17:15:24 +08004218 txq->q.write_ptr | (txq_id << 8));
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004219 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004220
4221 /* else not in power-save mode, uCode will never sleep when we're
4222 * trying to tx (during RFKILL, we're not trying to tx). */
4223 } else
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004224 iwl_write32(priv, HBUS_TARG_WRPTR,
Tomas Winklerfc4b6852007-10-25 17:15:24 +08004225 txq->q.write_ptr | (txq_id << 8));
Zhu Yib481de92007-09-25 17:54:57 -07004226
4227 txq->need_update = 0;
4228
4229 return rc;
4230}
4231
Tomas Winkler0a6857e2008-03-12 16:58:49 -07004232#ifdef CONFIG_IWLWIFI_DEBUG
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004233static void iwl4965_print_rx_config_cmd(struct iwl4965_rxon_cmd *rxon)
Zhu Yib481de92007-09-25 17:54:57 -07004234{
Joe Perches0795af52007-10-03 17:59:30 -07004235 DECLARE_MAC_BUF(mac);
4236
Zhu Yib481de92007-09-25 17:54:57 -07004237 IWL_DEBUG_RADIO("RX CONFIG:\n");
Tomas Winkler0a6857e2008-03-12 16:58:49 -07004238 iwl_print_hex_dump(IWL_DL_RADIO, (u8 *) rxon, sizeof(*rxon));
Zhu Yib481de92007-09-25 17:54:57 -07004239 IWL_DEBUG_RADIO("u16 channel: 0x%x\n", le16_to_cpu(rxon->channel));
4240 IWL_DEBUG_RADIO("u32 flags: 0x%08X\n", le32_to_cpu(rxon->flags));
4241 IWL_DEBUG_RADIO("u32 filter_flags: 0x%08x\n",
4242 le32_to_cpu(rxon->filter_flags));
4243 IWL_DEBUG_RADIO("u8 dev_type: 0x%x\n", rxon->dev_type);
4244 IWL_DEBUG_RADIO("u8 ofdm_basic_rates: 0x%02x\n",
4245 rxon->ofdm_basic_rates);
4246 IWL_DEBUG_RADIO("u8 cck_basic_rates: 0x%02x\n", rxon->cck_basic_rates);
Joe Perches0795af52007-10-03 17:59:30 -07004247 IWL_DEBUG_RADIO("u8[6] node_addr: %s\n",
4248 print_mac(mac, rxon->node_addr));
4249 IWL_DEBUG_RADIO("u8[6] bssid_addr: %s\n",
4250 print_mac(mac, rxon->bssid_addr));
Zhu Yib481de92007-09-25 17:54:57 -07004251 IWL_DEBUG_RADIO("u16 assoc_id: 0x%x\n", le16_to_cpu(rxon->assoc_id));
4252}
4253#endif
4254
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004255static void iwl4965_enable_interrupts(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004256{
4257 IWL_DEBUG_ISR("Enabling interrupts\n");
4258 set_bit(STATUS_INT_ENABLED, &priv->status);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004259 iwl_write32(priv, CSR_INT_MASK, CSR_INI_SET_MASK);
Zhu Yib481de92007-09-25 17:54:57 -07004260}
4261
Mohamed Abbas0359fac2008-03-28 16:21:08 -07004262/* call this function to flush any scheduled tasklet */
4263static inline void iwl_synchronize_irq(struct iwl_priv *priv)
4264{
4265 /* wait to make sure we flush pedding tasklet*/
4266 synchronize_irq(priv->pci_dev->irq);
4267 tasklet_kill(&priv->irq_tasklet);
4268}
4269
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004270static inline void iwl4965_disable_interrupts(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004271{
4272 clear_bit(STATUS_INT_ENABLED, &priv->status);
4273
4274 /* disable interrupts from uCode/NIC to host */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004275 iwl_write32(priv, CSR_INT_MASK, 0x00000000);
Zhu Yib481de92007-09-25 17:54:57 -07004276
4277 /* acknowledge/clear/reset any interrupts still pending
4278 * from uCode or flow handler (Rx/Tx DMA) */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004279 iwl_write32(priv, CSR_INT, 0xffffffff);
4280 iwl_write32(priv, CSR_FH_INT_STATUS, 0xffffffff);
Zhu Yib481de92007-09-25 17:54:57 -07004281 IWL_DEBUG_ISR("Disabled interrupts\n");
4282}
4283
4284static const char *desc_lookup(int i)
4285{
4286 switch (i) {
4287 case 1:
4288 return "FAIL";
4289 case 2:
4290 return "BAD_PARAM";
4291 case 3:
4292 return "BAD_CHECKSUM";
4293 case 4:
4294 return "NMI_INTERRUPT";
4295 case 5:
4296 return "SYSASSERT";
4297 case 6:
4298 return "FATAL_ERROR";
4299 }
4300
4301 return "UNKNOWN";
4302}
4303
4304#define ERROR_START_OFFSET (1 * sizeof(u32))
4305#define ERROR_ELEM_SIZE (7 * sizeof(u32))
4306
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004307static void iwl4965_dump_nic_error_log(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004308{
4309 u32 data2, line;
4310 u32 desc, time, count, base, data1;
4311 u32 blink1, blink2, ilink1, ilink2;
4312 int rc;
4313
4314 base = le32_to_cpu(priv->card_alive.error_event_table_ptr);
4315
Tomas Winkler57aab752008-04-14 21:16:03 -07004316 if (!priv->cfg->ops->lib->is_valid_rtc_data_addr(base)) {
Zhu Yib481de92007-09-25 17:54:57 -07004317 IWL_ERROR("Not valid error log pointer 0x%08X\n", base);
4318 return;
4319 }
4320
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004321 rc = iwl_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004322 if (rc) {
4323 IWL_WARNING("Can not read from adapter at this time.\n");
4324 return;
4325 }
4326
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004327 count = iwl_read_targ_mem(priv, base);
Zhu Yib481de92007-09-25 17:54:57 -07004328
4329 if (ERROR_START_OFFSET <= count * ERROR_ELEM_SIZE) {
4330 IWL_ERROR("Start IWL Error Log Dump:\n");
Tomas Winkler2acae162008-03-02 01:25:59 +02004331 IWL_ERROR("Status: 0x%08lX, count: %d\n", priv->status, count);
Zhu Yib481de92007-09-25 17:54:57 -07004332 }
4333
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004334 desc = iwl_read_targ_mem(priv, base + 1 * sizeof(u32));
4335 blink1 = iwl_read_targ_mem(priv, base + 3 * sizeof(u32));
4336 blink2 = iwl_read_targ_mem(priv, base + 4 * sizeof(u32));
4337 ilink1 = iwl_read_targ_mem(priv, base + 5 * sizeof(u32));
4338 ilink2 = iwl_read_targ_mem(priv, base + 6 * sizeof(u32));
4339 data1 = iwl_read_targ_mem(priv, base + 7 * sizeof(u32));
4340 data2 = iwl_read_targ_mem(priv, base + 8 * sizeof(u32));
4341 line = iwl_read_targ_mem(priv, base + 9 * sizeof(u32));
4342 time = iwl_read_targ_mem(priv, base + 11 * sizeof(u32));
Zhu Yib481de92007-09-25 17:54:57 -07004343
4344 IWL_ERROR("Desc Time "
4345 "data1 data2 line\n");
4346 IWL_ERROR("%-13s (#%d) %010u 0x%08X 0x%08X %u\n",
4347 desc_lookup(desc), desc, time, data1, data2, line);
4348 IWL_ERROR("blink1 blink2 ilink1 ilink2\n");
4349 IWL_ERROR("0x%05X 0x%05X 0x%05X 0x%05X\n", blink1, blink2,
4350 ilink1, ilink2);
4351
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004352 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004353}
4354
4355#define EVENT_START_OFFSET (4 * sizeof(u32))
4356
4357/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004358 * iwl4965_print_event_log - Dump error event log to syslog
Zhu Yib481de92007-09-25 17:54:57 -07004359 *
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004360 * NOTE: Must be called with iwl_grab_nic_access() already obtained!
Zhu Yib481de92007-09-25 17:54:57 -07004361 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004362static void iwl4965_print_event_log(struct iwl_priv *priv, u32 start_idx,
Zhu Yib481de92007-09-25 17:54:57 -07004363 u32 num_events, u32 mode)
4364{
4365 u32 i;
4366 u32 base; /* SRAM byte address of event log header */
4367 u32 event_size; /* 2 u32s, or 3 u32s if timestamp recorded */
4368 u32 ptr; /* SRAM byte address of log data */
4369 u32 ev, time, data; /* event log data */
4370
4371 if (num_events == 0)
4372 return;
4373
4374 base = le32_to_cpu(priv->card_alive.log_event_table_ptr);
4375
4376 if (mode == 0)
4377 event_size = 2 * sizeof(u32);
4378 else
4379 event_size = 3 * sizeof(u32);
4380
4381 ptr = base + EVENT_START_OFFSET + (start_idx * event_size);
4382
4383 /* "time" is actually "data" for mode 0 (no timestamp).
4384 * place event id # at far right for easier visual parsing. */
4385 for (i = 0; i < num_events; i++) {
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004386 ev = iwl_read_targ_mem(priv, ptr);
Zhu Yib481de92007-09-25 17:54:57 -07004387 ptr += sizeof(u32);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004388 time = iwl_read_targ_mem(priv, ptr);
Zhu Yib481de92007-09-25 17:54:57 -07004389 ptr += sizeof(u32);
4390 if (mode == 0)
4391 IWL_ERROR("0x%08x\t%04u\n", time, ev); /* data, ev */
4392 else {
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004393 data = iwl_read_targ_mem(priv, ptr);
Zhu Yib481de92007-09-25 17:54:57 -07004394 ptr += sizeof(u32);
4395 IWL_ERROR("%010u\t0x%08x\t%04u\n", time, data, ev);
4396 }
4397 }
4398}
4399
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004400static void iwl4965_dump_nic_event_log(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004401{
4402 int rc;
4403 u32 base; /* SRAM byte address of event log header */
4404 u32 capacity; /* event log capacity in # entries */
4405 u32 mode; /* 0 - no timestamp, 1 - timestamp recorded */
4406 u32 num_wraps; /* # times uCode wrapped to top of log */
4407 u32 next_entry; /* index of next entry to be written by uCode */
4408 u32 size; /* # entries that we'll print */
4409
4410 base = le32_to_cpu(priv->card_alive.log_event_table_ptr);
Tomas Winkler57aab752008-04-14 21:16:03 -07004411 if (!priv->cfg->ops->lib->is_valid_rtc_data_addr(base)) {
Zhu Yib481de92007-09-25 17:54:57 -07004412 IWL_ERROR("Invalid event log pointer 0x%08X\n", base);
4413 return;
4414 }
4415
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004416 rc = iwl_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004417 if (rc) {
4418 IWL_WARNING("Can not read from adapter at this time.\n");
4419 return;
4420 }
4421
4422 /* event log header */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004423 capacity = iwl_read_targ_mem(priv, base);
4424 mode = iwl_read_targ_mem(priv, base + (1 * sizeof(u32)));
4425 num_wraps = iwl_read_targ_mem(priv, base + (2 * sizeof(u32)));
4426 next_entry = iwl_read_targ_mem(priv, base + (3 * sizeof(u32)));
Zhu Yib481de92007-09-25 17:54:57 -07004427
4428 size = num_wraps ? capacity : next_entry;
4429
4430 /* bail out if nothing in log */
4431 if (size == 0) {
Zhu Yi583fab32007-09-27 11:27:30 +08004432 IWL_ERROR("Start IWL Event Log Dump: nothing in log\n");
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004433 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004434 return;
4435 }
4436
Zhu Yi583fab32007-09-27 11:27:30 +08004437 IWL_ERROR("Start IWL Event Log Dump: display count %d, wraps %d\n",
Zhu Yib481de92007-09-25 17:54:57 -07004438 size, num_wraps);
4439
4440 /* if uCode has wrapped back to top of log, start at the oldest entry,
4441 * i.e the next one that uCode would fill. */
4442 if (num_wraps)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004443 iwl4965_print_event_log(priv, next_entry,
Zhu Yib481de92007-09-25 17:54:57 -07004444 capacity - next_entry, mode);
4445
4446 /* (then/else) start at top of log */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004447 iwl4965_print_event_log(priv, 0, next_entry, mode);
Zhu Yib481de92007-09-25 17:54:57 -07004448
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004449 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004450}
4451
4452/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004453 * iwl4965_irq_handle_error - called for HW or SW error interrupt from card
Zhu Yib481de92007-09-25 17:54:57 -07004454 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004455static void iwl4965_irq_handle_error(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004456{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004457 /* Set the FW error flag -- cleared on iwl4965_down */
Zhu Yib481de92007-09-25 17:54:57 -07004458 set_bit(STATUS_FW_ERROR, &priv->status);
4459
4460 /* Cancel currently queued command. */
4461 clear_bit(STATUS_HCMD_ACTIVE, &priv->status);
4462
Tomas Winkler0a6857e2008-03-12 16:58:49 -07004463#ifdef CONFIG_IWLWIFI_DEBUG
4464 if (iwl_debug_level & IWL_DL_FW_ERRORS) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004465 iwl4965_dump_nic_error_log(priv);
4466 iwl4965_dump_nic_event_log(priv);
4467 iwl4965_print_rx_config_cmd(&priv->staging_rxon);
Zhu Yib481de92007-09-25 17:54:57 -07004468 }
4469#endif
4470
4471 wake_up_interruptible(&priv->wait_command_queue);
4472
4473 /* Keep the restart process from trying to send host
4474 * commands by clearing the INIT status bit */
4475 clear_bit(STATUS_READY, &priv->status);
4476
4477 if (!test_bit(STATUS_EXIT_PENDING, &priv->status)) {
4478 IWL_DEBUG(IWL_DL_INFO | IWL_DL_FW_ERRORS,
4479 "Restarting adapter due to uCode error.\n");
4480
Tomas Winkler3109ece2008-03-28 16:33:35 -07004481 if (iwl_is_associated(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07004482 memcpy(&priv->recovery_rxon, &priv->active_rxon,
4483 sizeof(priv->recovery_rxon));
4484 priv->error_recovering = 1;
4485 }
4486 queue_work(priv->workqueue, &priv->restart);
4487 }
4488}
4489
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004490static void iwl4965_error_recovery(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004491{
4492 unsigned long flags;
4493
4494 memcpy(&priv->staging_rxon, &priv->recovery_rxon,
4495 sizeof(priv->staging_rxon));
4496 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004497 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004498
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004499 iwl4965_rxon_add_station(priv, priv->bssid, 1);
Zhu Yib481de92007-09-25 17:54:57 -07004500
4501 spin_lock_irqsave(&priv->lock, flags);
4502 priv->assoc_id = le16_to_cpu(priv->staging_rxon.assoc_id);
4503 priv->error_recovering = 0;
4504 spin_unlock_irqrestore(&priv->lock, flags);
4505}
4506
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004507static void iwl4965_irq_tasklet(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004508{
4509 u32 inta, handled = 0;
4510 u32 inta_fh;
4511 unsigned long flags;
Tomas Winkler0a6857e2008-03-12 16:58:49 -07004512#ifdef CONFIG_IWLWIFI_DEBUG
Zhu Yib481de92007-09-25 17:54:57 -07004513 u32 inta_mask;
4514#endif
4515
4516 spin_lock_irqsave(&priv->lock, flags);
4517
4518 /* Ack/clear/reset pending uCode interrupts.
4519 * Note: Some bits in CSR_INT are "OR" of bits in CSR_FH_INT_STATUS,
4520 * and will clear only when CSR_FH_INT_STATUS gets cleared. */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004521 inta = iwl_read32(priv, CSR_INT);
4522 iwl_write32(priv, CSR_INT, inta);
Zhu Yib481de92007-09-25 17:54:57 -07004523
4524 /* Ack/clear/reset pending flow-handler (DMA) interrupts.
4525 * Any new interrupts that happen after this, either while we're
4526 * in this tasklet, or later, will show up in next ISR/tasklet. */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004527 inta_fh = iwl_read32(priv, CSR_FH_INT_STATUS);
4528 iwl_write32(priv, CSR_FH_INT_STATUS, inta_fh);
Zhu Yib481de92007-09-25 17:54:57 -07004529
Tomas Winkler0a6857e2008-03-12 16:58:49 -07004530#ifdef CONFIG_IWLWIFI_DEBUG
4531 if (iwl_debug_level & IWL_DL_ISR) {
Ben Cahill9fbab512007-11-29 11:09:47 +08004532 /* just for debug */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004533 inta_mask = iwl_read32(priv, CSR_INT_MASK);
Zhu Yib481de92007-09-25 17:54:57 -07004534 IWL_DEBUG_ISR("inta 0x%08x, enabled 0x%08x, fh 0x%08x\n",
4535 inta, inta_mask, inta_fh);
4536 }
4537#endif
4538
4539 /* Since CSR_INT and CSR_FH_INT_STATUS reads and clears are not
4540 * atomic, make sure that inta covers all the interrupts that
4541 * we've discovered, even if FH interrupt came in just after
4542 * reading CSR_INT. */
Tomas Winkler6f83eaa2008-03-04 18:09:28 -08004543 if (inta_fh & CSR49_FH_INT_RX_MASK)
Zhu Yib481de92007-09-25 17:54:57 -07004544 inta |= CSR_INT_BIT_FH_RX;
Tomas Winkler6f83eaa2008-03-04 18:09:28 -08004545 if (inta_fh & CSR49_FH_INT_TX_MASK)
Zhu Yib481de92007-09-25 17:54:57 -07004546 inta |= CSR_INT_BIT_FH_TX;
4547
4548 /* Now service all interrupt bits discovered above. */
4549 if (inta & CSR_INT_BIT_HW_ERR) {
4550 IWL_ERROR("Microcode HW error detected. Restarting.\n");
4551
4552 /* Tell the device to stop sending interrupts */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004553 iwl4965_disable_interrupts(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004554
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004555 iwl4965_irq_handle_error(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004556
4557 handled |= CSR_INT_BIT_HW_ERR;
4558
4559 spin_unlock_irqrestore(&priv->lock, flags);
4560
4561 return;
4562 }
4563
Tomas Winkler0a6857e2008-03-12 16:58:49 -07004564#ifdef CONFIG_IWLWIFI_DEBUG
4565 if (iwl_debug_level & (IWL_DL_ISR)) {
Zhu Yib481de92007-09-25 17:54:57 -07004566 /* NIC fires this, but we don't use it, redundant with WAKEUP */
Joonwoo Park25c03d82008-01-23 10:15:20 -08004567 if (inta & CSR_INT_BIT_SCD)
4568 IWL_DEBUG_ISR("Scheduler finished to transmit "
4569 "the frame/frames.\n");
Zhu Yib481de92007-09-25 17:54:57 -07004570
4571 /* Alive notification via Rx interrupt will do the real work */
4572 if (inta & CSR_INT_BIT_ALIVE)
4573 IWL_DEBUG_ISR("Alive interrupt\n");
4574 }
4575#endif
4576 /* Safely ignore these bits for debug checks below */
Joonwoo Park25c03d82008-01-23 10:15:20 -08004577 inta &= ~(CSR_INT_BIT_SCD | CSR_INT_BIT_ALIVE);
Zhu Yib481de92007-09-25 17:54:57 -07004578
Ben Cahill9fbab512007-11-29 11:09:47 +08004579 /* HW RF KILL switch toggled */
Zhu Yib481de92007-09-25 17:54:57 -07004580 if (inta & CSR_INT_BIT_RF_KILL) {
4581 int hw_rf_kill = 0;
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004582 if (!(iwl_read32(priv, CSR_GP_CNTRL) &
Zhu Yib481de92007-09-25 17:54:57 -07004583 CSR_GP_CNTRL_REG_FLAG_HW_RF_KILL_SW))
4584 hw_rf_kill = 1;
4585
4586 IWL_DEBUG(IWL_DL_INFO | IWL_DL_RF_KILL | IWL_DL_ISR,
4587 "RF_KILL bit toggled to %s.\n",
4588 hw_rf_kill ? "disable radio":"enable radio");
4589
4590 /* Queue restart only if RF_KILL switch was set to "kill"
4591 * when we loaded driver, and is now set to "enable".
4592 * After we're Alive, RF_KILL gets handled by
Reinette Chatre32304552008-02-15 14:34:37 -08004593 * iwl4965_rx_card_state_notif() */
Zhu Yi53e49092007-12-06 16:08:44 +08004594 if (!hw_rf_kill && !test_bit(STATUS_ALIVE, &priv->status)) {
4595 clear_bit(STATUS_RF_KILL_HW, &priv->status);
Zhu Yib481de92007-09-25 17:54:57 -07004596 queue_work(priv->workqueue, &priv->restart);
Zhu Yi53e49092007-12-06 16:08:44 +08004597 }
Zhu Yib481de92007-09-25 17:54:57 -07004598
4599 handled |= CSR_INT_BIT_RF_KILL;
4600 }
4601
Ben Cahill9fbab512007-11-29 11:09:47 +08004602 /* Chip got too hot and stopped itself */
Zhu Yib481de92007-09-25 17:54:57 -07004603 if (inta & CSR_INT_BIT_CT_KILL) {
4604 IWL_ERROR("Microcode CT kill error detected.\n");
4605 handled |= CSR_INT_BIT_CT_KILL;
4606 }
4607
4608 /* Error detected by uCode */
4609 if (inta & CSR_INT_BIT_SW_ERR) {
4610 IWL_ERROR("Microcode SW error detected. Restarting 0x%X.\n",
4611 inta);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004612 iwl4965_irq_handle_error(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004613 handled |= CSR_INT_BIT_SW_ERR;
4614 }
4615
4616 /* uCode wakes up after power-down sleep */
4617 if (inta & CSR_INT_BIT_WAKEUP) {
4618 IWL_DEBUG_ISR("Wakeup interrupt\n");
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004619 iwl4965_rx_queue_update_write_ptr(priv, &priv->rxq);
4620 iwl4965_tx_queue_update_write_ptr(priv, &priv->txq[0]);
4621 iwl4965_tx_queue_update_write_ptr(priv, &priv->txq[1]);
4622 iwl4965_tx_queue_update_write_ptr(priv, &priv->txq[2]);
4623 iwl4965_tx_queue_update_write_ptr(priv, &priv->txq[3]);
4624 iwl4965_tx_queue_update_write_ptr(priv, &priv->txq[4]);
4625 iwl4965_tx_queue_update_write_ptr(priv, &priv->txq[5]);
Zhu Yib481de92007-09-25 17:54:57 -07004626
4627 handled |= CSR_INT_BIT_WAKEUP;
4628 }
4629
4630 /* All uCode command responses, including Tx command responses,
4631 * Rx "responses" (frame-received notification), and other
4632 * notifications from uCode come through here*/
4633 if (inta & (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX)) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004634 iwl4965_rx_handle(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004635 handled |= (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX);
4636 }
4637
4638 if (inta & CSR_INT_BIT_FH_TX) {
4639 IWL_DEBUG_ISR("Tx interrupt\n");
4640 handled |= CSR_INT_BIT_FH_TX;
4641 }
4642
4643 if (inta & ~handled)
4644 IWL_ERROR("Unhandled INTA bits 0x%08x\n", inta & ~handled);
4645
4646 if (inta & ~CSR_INI_SET_MASK) {
4647 IWL_WARNING("Disabled INTA bits 0x%08x were pending\n",
4648 inta & ~CSR_INI_SET_MASK);
4649 IWL_WARNING(" with FH_INT = 0x%08x\n", inta_fh);
4650 }
4651
4652 /* Re-enable all interrupts */
Mohamed Abbas0359fac2008-03-28 16:21:08 -07004653 /* only Re-enable if diabled by irq */
4654 if (test_bit(STATUS_INT_ENABLED, &priv->status))
4655 iwl4965_enable_interrupts(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004656
Tomas Winkler0a6857e2008-03-12 16:58:49 -07004657#ifdef CONFIG_IWLWIFI_DEBUG
4658 if (iwl_debug_level & (IWL_DL_ISR)) {
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004659 inta = iwl_read32(priv, CSR_INT);
4660 inta_mask = iwl_read32(priv, CSR_INT_MASK);
4661 inta_fh = iwl_read32(priv, CSR_FH_INT_STATUS);
Zhu Yib481de92007-09-25 17:54:57 -07004662 IWL_DEBUG_ISR("End inta 0x%08x, enabled 0x%08x, fh 0x%08x, "
4663 "flags 0x%08lx\n", inta, inta_mask, inta_fh, flags);
4664 }
4665#endif
4666 spin_unlock_irqrestore(&priv->lock, flags);
4667}
4668
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004669static irqreturn_t iwl4965_isr(int irq, void *data)
Zhu Yib481de92007-09-25 17:54:57 -07004670{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004671 struct iwl_priv *priv = data;
Zhu Yib481de92007-09-25 17:54:57 -07004672 u32 inta, inta_mask;
4673 u32 inta_fh;
4674 if (!priv)
4675 return IRQ_NONE;
4676
4677 spin_lock(&priv->lock);
4678
4679 /* Disable (but don't clear!) interrupts here to avoid
4680 * back-to-back ISRs and sporadic interrupts from our NIC.
4681 * If we have something to service, the tasklet will re-enable ints.
4682 * If we *don't* have something, we'll re-enable before leaving here. */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004683 inta_mask = iwl_read32(priv, CSR_INT_MASK); /* just for debug */
4684 iwl_write32(priv, CSR_INT_MASK, 0x00000000);
Zhu Yib481de92007-09-25 17:54:57 -07004685
4686 /* Discover which interrupts are active/pending */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004687 inta = iwl_read32(priv, CSR_INT);
4688 inta_fh = iwl_read32(priv, CSR_FH_INT_STATUS);
Zhu Yib481de92007-09-25 17:54:57 -07004689
4690 /* Ignore interrupt if there's nothing in NIC to service.
4691 * This may be due to IRQ shared with another device,
4692 * or due to sporadic interrupts thrown from our NIC. */
4693 if (!inta && !inta_fh) {
4694 IWL_DEBUG_ISR("Ignore interrupt, inta == 0, inta_fh == 0\n");
4695 goto none;
4696 }
4697
4698 if ((inta == 0xFFFFFFFF) || ((inta & 0xFFFFFFF0) == 0xa5a5a5a0)) {
Oliver Neukum66fbb542007-11-15 09:31:10 +08004699 /* Hardware disappeared. It might have already raised
4700 * an interrupt */
Zhu Yib481de92007-09-25 17:54:57 -07004701 IWL_WARNING("HARDWARE GONE?? INTA == 0x%080x\n", inta);
Oliver Neukum66fbb542007-11-15 09:31:10 +08004702 goto unplugged;
Zhu Yib481de92007-09-25 17:54:57 -07004703 }
4704
4705 IWL_DEBUG_ISR("ISR inta 0x%08x, enabled 0x%08x, fh 0x%08x\n",
4706 inta, inta_mask, inta_fh);
4707
Joonwoo Park25c03d82008-01-23 10:15:20 -08004708 inta &= ~CSR_INT_BIT_SCD;
4709
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004710 /* iwl4965_irq_tasklet() will service interrupts and re-enable them */
Joonwoo Park25c03d82008-01-23 10:15:20 -08004711 if (likely(inta || inta_fh))
4712 tasklet_schedule(&priv->irq_tasklet);
Zhu Yib481de92007-09-25 17:54:57 -07004713
Oliver Neukum66fbb542007-11-15 09:31:10 +08004714 unplugged:
4715 spin_unlock(&priv->lock);
Zhu Yib481de92007-09-25 17:54:57 -07004716 return IRQ_HANDLED;
4717
4718 none:
4719 /* re-enable interrupts here since we don't have anything to service. */
Mohamed Abbas0359fac2008-03-28 16:21:08 -07004720 /* only Re-enable if diabled by irq */
4721 if (test_bit(STATUS_INT_ENABLED, &priv->status))
4722 iwl4965_enable_interrupts(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004723 spin_unlock(&priv->lock);
4724 return IRQ_NONE;
4725}
4726
Zhu Yib481de92007-09-25 17:54:57 -07004727/* For active scan, listen ACTIVE_DWELL_TIME (msec) on each channel after
4728 * sending probe req. This should be set long enough to hear probe responses
4729 * from more than one AP. */
4730#define IWL_ACTIVE_DWELL_TIME_24 (20) /* all times in msec */
4731#define IWL_ACTIVE_DWELL_TIME_52 (10)
4732
4733/* For faster active scanning, scan will move to the next channel if fewer than
4734 * PLCP_QUIET_THRESH packets are heard on this channel within
4735 * ACTIVE_QUIET_TIME after sending probe request. This shortens the dwell
4736 * time if it's a quiet channel (nothing responded to our probe, and there's
4737 * no other traffic).
4738 * Disable "quiet" feature by setting PLCP_QUIET_THRESH to 0. */
4739#define IWL_PLCP_QUIET_THRESH __constant_cpu_to_le16(1) /* packets */
4740#define IWL_ACTIVE_QUIET_TIME __constant_cpu_to_le16(5) /* msec */
4741
4742/* For passive scan, listen PASSIVE_DWELL_TIME (msec) on each channel.
4743 * Must be set longer than active dwell time.
4744 * For the most reliable scan, set > AP beacon interval (typically 100msec). */
4745#define IWL_PASSIVE_DWELL_TIME_24 (20) /* all times in msec */
4746#define IWL_PASSIVE_DWELL_TIME_52 (10)
4747#define IWL_PASSIVE_DWELL_BASE (100)
4748#define IWL_CHANNEL_TUNE_TIME 5
4749
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004750static inline u16 iwl4965_get_active_dwell_time(struct iwl_priv *priv,
Johannes Berg8318d782008-01-24 19:38:38 +01004751 enum ieee80211_band band)
Zhu Yib481de92007-09-25 17:54:57 -07004752{
Johannes Berg8318d782008-01-24 19:38:38 +01004753 if (band == IEEE80211_BAND_5GHZ)
Zhu Yib481de92007-09-25 17:54:57 -07004754 return IWL_ACTIVE_DWELL_TIME_52;
4755 else
4756 return IWL_ACTIVE_DWELL_TIME_24;
4757}
4758
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004759static u16 iwl4965_get_passive_dwell_time(struct iwl_priv *priv,
Johannes Berg8318d782008-01-24 19:38:38 +01004760 enum ieee80211_band band)
Zhu Yib481de92007-09-25 17:54:57 -07004761{
Johannes Berg8318d782008-01-24 19:38:38 +01004762 u16 active = iwl4965_get_active_dwell_time(priv, band);
4763 u16 passive = (band != IEEE80211_BAND_5GHZ) ?
Zhu Yib481de92007-09-25 17:54:57 -07004764 IWL_PASSIVE_DWELL_BASE + IWL_PASSIVE_DWELL_TIME_24 :
4765 IWL_PASSIVE_DWELL_BASE + IWL_PASSIVE_DWELL_TIME_52;
4766
Tomas Winkler3109ece2008-03-28 16:33:35 -07004767 if (iwl_is_associated(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07004768 /* If we're associated, we clamp the maximum passive
4769 * dwell time to be 98% of the beacon interval (minus
4770 * 2 * channel tune time) */
4771 passive = priv->beacon_int;
4772 if ((passive > IWL_PASSIVE_DWELL_BASE) || !passive)
4773 passive = IWL_PASSIVE_DWELL_BASE;
4774 passive = (passive * 98) / 100 - IWL_CHANNEL_TUNE_TIME * 2;
4775 }
4776
4777 if (passive <= active)
4778 passive = active + 1;
4779
4780 return passive;
4781}
4782
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004783static int iwl4965_get_channels_for_scan(struct iwl_priv *priv,
Johannes Berg8318d782008-01-24 19:38:38 +01004784 enum ieee80211_band band,
Zhu Yib481de92007-09-25 17:54:57 -07004785 u8 is_active, u8 direct_mask,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004786 struct iwl4965_scan_channel *scan_ch)
Zhu Yib481de92007-09-25 17:54:57 -07004787{
4788 const struct ieee80211_channel *channels = NULL;
Johannes Berg8318d782008-01-24 19:38:38 +01004789 const struct ieee80211_supported_band *sband;
Assaf Kraussbf85ea42008-03-14 10:38:49 -07004790 const struct iwl_channel_info *ch_info;
Zhu Yib481de92007-09-25 17:54:57 -07004791 u16 passive_dwell = 0;
4792 u16 active_dwell = 0;
4793 int added, i;
4794
Johannes Berg8318d782008-01-24 19:38:38 +01004795 sband = iwl4965_get_hw_mode(priv, band);
4796 if (!sband)
Zhu Yib481de92007-09-25 17:54:57 -07004797 return 0;
4798
Johannes Berg8318d782008-01-24 19:38:38 +01004799 channels = sband->channels;
Zhu Yib481de92007-09-25 17:54:57 -07004800
Johannes Berg8318d782008-01-24 19:38:38 +01004801 active_dwell = iwl4965_get_active_dwell_time(priv, band);
4802 passive_dwell = iwl4965_get_passive_dwell_time(priv, band);
Zhu Yib481de92007-09-25 17:54:57 -07004803
Johannes Berg8318d782008-01-24 19:38:38 +01004804 for (i = 0, added = 0; i < sband->n_channels; i++) {
Johannes Berg182e2e62008-04-04 10:41:56 +02004805 if (channels[i].flags & IEEE80211_CHAN_DISABLED)
4806 continue;
4807
Johannes Berg8318d782008-01-24 19:38:38 +01004808 if (ieee80211_frequency_to_channel(channels[i].center_freq) ==
Zhu Yib481de92007-09-25 17:54:57 -07004809 le16_to_cpu(priv->active_rxon.channel)) {
Tomas Winkler3109ece2008-03-28 16:33:35 -07004810 if (iwl_is_associated(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07004811 IWL_DEBUG_SCAN
4812 ("Skipping current channel %d\n",
4813 le16_to_cpu(priv->active_rxon.channel));
4814 continue;
4815 }
4816 } else if (priv->only_active_channel)
4817 continue;
4818
Johannes Berg8318d782008-01-24 19:38:38 +01004819 scan_ch->channel = ieee80211_frequency_to_channel(channels[i].center_freq);
Zhu Yib481de92007-09-25 17:54:57 -07004820
Assaf Krauss8622e702008-03-21 13:53:43 -07004821 ch_info = iwl_get_channel_info(priv, band,
Ben Cahill9fbab512007-11-29 11:09:47 +08004822 scan_ch->channel);
Zhu Yib481de92007-09-25 17:54:57 -07004823 if (!is_channel_valid(ch_info)) {
4824 IWL_DEBUG_SCAN("Channel %d is INVALID for this SKU.\n",
4825 scan_ch->channel);
4826 continue;
4827 }
4828
4829 if (!is_active || is_channel_passive(ch_info) ||
Johannes Berg8318d782008-01-24 19:38:38 +01004830 (channels[i].flags & IEEE80211_CHAN_PASSIVE_SCAN))
Zhu Yib481de92007-09-25 17:54:57 -07004831 scan_ch->type = 0; /* passive */
4832 else
4833 scan_ch->type = 1; /* active */
4834
4835 if (scan_ch->type & 1)
4836 scan_ch->type |= (direct_mask << 1);
4837
4838 if (is_channel_narrow(ch_info))
4839 scan_ch->type |= (1 << 7);
4840
4841 scan_ch->active_dwell = cpu_to_le16(active_dwell);
4842 scan_ch->passive_dwell = cpu_to_le16(passive_dwell);
4843
Ben Cahill9fbab512007-11-29 11:09:47 +08004844 /* Set txpower levels to defaults */
Zhu Yib481de92007-09-25 17:54:57 -07004845 scan_ch->tpc.dsp_atten = 110;
4846 /* scan_pwr_info->tpc.dsp_atten; */
4847
4848 /*scan_pwr_info->tpc.tx_gain; */
Johannes Berg8318d782008-01-24 19:38:38 +01004849 if (band == IEEE80211_BAND_5GHZ)
Zhu Yib481de92007-09-25 17:54:57 -07004850 scan_ch->tpc.tx_gain = ((1 << 5) | (3 << 3)) | 3;
4851 else {
4852 scan_ch->tpc.tx_gain = ((1 << 5) | (5 << 3));
4853 /* NOTE: if we were doing 6Mb OFDM for scans we'd use
Ben Cahill9fbab512007-11-29 11:09:47 +08004854 * power level:
Reinette Chatre8a1b0242008-01-14 17:46:25 -08004855 * scan_ch->tpc.tx_gain = ((1 << 5) | (2 << 3)) | 3;
Zhu Yib481de92007-09-25 17:54:57 -07004856 */
4857 }
4858
4859 IWL_DEBUG_SCAN("Scanning %d [%s %d]\n",
4860 scan_ch->channel,
4861 (scan_ch->type & 1) ? "ACTIVE" : "PASSIVE",
4862 (scan_ch->type & 1) ?
4863 active_dwell : passive_dwell);
4864
4865 scan_ch++;
4866 added++;
4867 }
4868
4869 IWL_DEBUG_SCAN("total channels to scan %d \n", added);
4870 return added;
4871}
4872
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004873static void iwl4965_init_hw_rates(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07004874 struct ieee80211_rate *rates)
4875{
4876 int i;
4877
4878 for (i = 0; i < IWL_RATE_COUNT; i++) {
Johannes Berg8318d782008-01-24 19:38:38 +01004879 rates[i].bitrate = iwl4965_rates[i].ieee * 5;
4880 rates[i].hw_value = i; /* Rate scaling will work on indexes */
4881 rates[i].hw_value_short = i;
4882 rates[i].flags = 0;
4883 if ((i > IWL_LAST_OFDM_RATE) || (i < IWL_FIRST_OFDM_RATE)) {
Zhu Yib481de92007-09-25 17:54:57 -07004884 /*
Johannes Berg8318d782008-01-24 19:38:38 +01004885 * If CCK != 1M then set short preamble rate flag.
Zhu Yib481de92007-09-25 17:54:57 -07004886 */
Tomas Winkler35cdeaf2008-03-11 16:17:20 -07004887 rates[i].flags |=
4888 (iwl4965_rates[i].plcp == IWL_RATE_1M_PLCP) ?
4889 0 : IEEE80211_RATE_SHORT_PREAMBLE;
Zhu Yib481de92007-09-25 17:54:57 -07004890 }
Zhu Yib481de92007-09-25 17:54:57 -07004891 }
Zhu Yib481de92007-09-25 17:54:57 -07004892}
4893
4894/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004895 * iwl4965_init_geos - Initialize mac80211's geo/channel info based from eeprom
Zhu Yib481de92007-09-25 17:54:57 -07004896 */
Assaf Kraussbf85ea42008-03-14 10:38:49 -07004897int iwl4965_init_geos(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004898{
Assaf Kraussbf85ea42008-03-14 10:38:49 -07004899 struct iwl_channel_info *ch;
Tomas Winkler8211ef72008-03-02 01:36:04 +02004900 struct ieee80211_supported_band *sband;
Zhu Yib481de92007-09-25 17:54:57 -07004901 struct ieee80211_channel *channels;
4902 struct ieee80211_channel *geo_ch;
4903 struct ieee80211_rate *rates;
4904 int i = 0;
Zhu Yib481de92007-09-25 17:54:57 -07004905
Johannes Berg8318d782008-01-24 19:38:38 +01004906 if (priv->bands[IEEE80211_BAND_2GHZ].n_bitrates ||
4907 priv->bands[IEEE80211_BAND_5GHZ].n_bitrates) {
Zhu Yib481de92007-09-25 17:54:57 -07004908 IWL_DEBUG_INFO("Geography modes already initialized.\n");
4909 set_bit(STATUS_GEO_CONFIGURED, &priv->status);
4910 return 0;
4911 }
4912
Zhu Yib481de92007-09-25 17:54:57 -07004913 channels = kzalloc(sizeof(struct ieee80211_channel) *
4914 priv->channel_count, GFP_KERNEL);
Johannes Berg8318d782008-01-24 19:38:38 +01004915 if (!channels)
Zhu Yib481de92007-09-25 17:54:57 -07004916 return -ENOMEM;
Zhu Yib481de92007-09-25 17:54:57 -07004917
Tomas Winkler8211ef72008-03-02 01:36:04 +02004918 rates = kzalloc((sizeof(struct ieee80211_rate) * (IWL_RATE_COUNT + 1)),
Zhu Yib481de92007-09-25 17:54:57 -07004919 GFP_KERNEL);
4920 if (!rates) {
Zhu Yib481de92007-09-25 17:54:57 -07004921 kfree(channels);
4922 return -ENOMEM;
4923 }
4924
Zhu Yib481de92007-09-25 17:54:57 -07004925 /* 5.2GHz channels start after the 2.4GHz channels */
Tomas Winkler8211ef72008-03-02 01:36:04 +02004926 sband = &priv->bands[IEEE80211_BAND_5GHZ];
Assaf Kraussbf85ea42008-03-14 10:38:49 -07004927 sband->channels = &channels[ARRAY_SIZE(iwl_eeprom_band_1)];
Tomas Winkler8211ef72008-03-02 01:36:04 +02004928 /* just OFDM */
4929 sband->bitrates = &rates[IWL_FIRST_OFDM_RATE];
4930 sband->n_bitrates = IWL_RATE_COUNT - IWL_FIRST_OFDM_RATE;
Johannes Berg8318d782008-01-24 19:38:38 +01004931
Assaf Krauss1ea87392008-03-18 14:57:50 -07004932 iwl4965_init_ht_hw_capab(priv, &sband->ht_info, IEEE80211_BAND_5GHZ);
Tomas Winkler78330fd2008-02-06 02:37:18 +02004933
Tomas Winkler8211ef72008-03-02 01:36:04 +02004934 sband = &priv->bands[IEEE80211_BAND_2GHZ];
4935 sband->channels = channels;
4936 /* OFDM & CCK */
4937 sband->bitrates = rates;
4938 sband->n_bitrates = IWL_RATE_COUNT;
Zhu Yib481de92007-09-25 17:54:57 -07004939
Assaf Krauss1ea87392008-03-18 14:57:50 -07004940 iwl4965_init_ht_hw_capab(priv, &sband->ht_info, IEEE80211_BAND_2GHZ);
Tomas Winkler78330fd2008-02-06 02:37:18 +02004941
Zhu Yib481de92007-09-25 17:54:57 -07004942 priv->ieee_channels = channels;
4943 priv->ieee_rates = rates;
4944
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004945 iwl4965_init_hw_rates(priv, rates);
Zhu Yib481de92007-09-25 17:54:57 -07004946
Tomas Winkler8211ef72008-03-02 01:36:04 +02004947 for (i = 0; i < priv->channel_count; i++) {
Zhu Yib481de92007-09-25 17:54:57 -07004948 ch = &priv->channel_info[i];
4949
Tomas Winkler8211ef72008-03-02 01:36:04 +02004950 /* FIXME: might be removed if scan is OK */
4951 if (!is_channel_valid(ch))
Zhu Yib481de92007-09-25 17:54:57 -07004952 continue;
Zhu Yib481de92007-09-25 17:54:57 -07004953
Tomas Winkler8211ef72008-03-02 01:36:04 +02004954 if (is_channel_a_band(ch))
4955 sband = &priv->bands[IEEE80211_BAND_5GHZ];
4956 else
4957 sband = &priv->bands[IEEE80211_BAND_2GHZ];
Zhu Yib481de92007-09-25 17:54:57 -07004958
Tomas Winkler8211ef72008-03-02 01:36:04 +02004959 geo_ch = &sband->channels[sband->n_channels++];
4960
4961 geo_ch->center_freq = ieee80211_channel_to_frequency(ch->channel);
Johannes Berg8318d782008-01-24 19:38:38 +01004962 geo_ch->max_power = ch->max_power_avg;
4963 geo_ch->max_antenna_gain = 0xff;
Mohamed Abbas7b723042008-01-31 21:46:40 -08004964 geo_ch->hw_value = ch->channel;
Zhu Yib481de92007-09-25 17:54:57 -07004965
4966 if (is_channel_valid(ch)) {
Johannes Berg8318d782008-01-24 19:38:38 +01004967 if (!(ch->flags & EEPROM_CHANNEL_IBSS))
4968 geo_ch->flags |= IEEE80211_CHAN_NO_IBSS;
Zhu Yib481de92007-09-25 17:54:57 -07004969
Johannes Berg8318d782008-01-24 19:38:38 +01004970 if (!(ch->flags & EEPROM_CHANNEL_ACTIVE))
4971 geo_ch->flags |= IEEE80211_CHAN_PASSIVE_SCAN;
Zhu Yib481de92007-09-25 17:54:57 -07004972
4973 if (ch->flags & EEPROM_CHANNEL_RADAR)
Johannes Berg8318d782008-01-24 19:38:38 +01004974 geo_ch->flags |= IEEE80211_CHAN_RADAR;
Zhu Yib481de92007-09-25 17:54:57 -07004975
4976 if (ch->max_power_avg > priv->max_channel_txpower_limit)
4977 priv->max_channel_txpower_limit =
4978 ch->max_power_avg;
Tomas Winkler8211ef72008-03-02 01:36:04 +02004979 } else {
Johannes Berg8318d782008-01-24 19:38:38 +01004980 geo_ch->flags |= IEEE80211_CHAN_DISABLED;
Tomas Winkler8211ef72008-03-02 01:36:04 +02004981 }
4982
4983 /* Save flags for reg domain usage */
4984 geo_ch->orig_flags = geo_ch->flags;
4985
4986 IWL_DEBUG_INFO("Channel %d Freq=%d[%sGHz] %s flag=0%X\n",
4987 ch->channel, geo_ch->center_freq,
4988 is_channel_a_band(ch) ? "5.2" : "2.4",
4989 geo_ch->flags & IEEE80211_CHAN_DISABLED ?
4990 "restricted" : "valid",
4991 geo_ch->flags);
Zhu Yib481de92007-09-25 17:54:57 -07004992 }
4993
Tomas Winkler82b9a122008-03-04 18:09:30 -08004994 if ((priv->bands[IEEE80211_BAND_5GHZ].n_channels == 0) &&
4995 priv->cfg->sku & IWL_SKU_A) {
Zhu Yib481de92007-09-25 17:54:57 -07004996 printk(KERN_INFO DRV_NAME
4997 ": Incorrectly detected BG card as ABG. Please send "
4998 "your PCI ID 0x%04X:0x%04X to maintainer.\n",
4999 priv->pci_dev->device, priv->pci_dev->subsystem_device);
Tomas Winkler82b9a122008-03-04 18:09:30 -08005000 priv->cfg->sku &= ~IWL_SKU_A;
Zhu Yib481de92007-09-25 17:54:57 -07005001 }
5002
5003 printk(KERN_INFO DRV_NAME
5004 ": Tunable channels: %d 802.11bg, %d 802.11a channels\n",
Johannes Berg8318d782008-01-24 19:38:38 +01005005 priv->bands[IEEE80211_BAND_2GHZ].n_channels,
5006 priv->bands[IEEE80211_BAND_5GHZ].n_channels);
Zhu Yib481de92007-09-25 17:54:57 -07005007
John W. Linvillee0e0a672008-03-25 15:58:40 -04005008 if (priv->bands[IEEE80211_BAND_2GHZ].n_channels)
5009 priv->hw->wiphy->bands[IEEE80211_BAND_2GHZ] =
5010 &priv->bands[IEEE80211_BAND_2GHZ];
5011 if (priv->bands[IEEE80211_BAND_5GHZ].n_channels)
5012 priv->hw->wiphy->bands[IEEE80211_BAND_5GHZ] =
5013 &priv->bands[IEEE80211_BAND_5GHZ];
Zhu Yib481de92007-09-25 17:54:57 -07005014
Zhu Yib481de92007-09-25 17:54:57 -07005015 set_bit(STATUS_GEO_CONFIGURED, &priv->status);
5016
5017 return 0;
5018}
5019
Reinette Chatre849e0dc2008-01-23 10:15:18 -08005020/*
5021 * iwl4965_free_geos - undo allocations in iwl4965_init_geos
5022 */
Assaf Kraussbf85ea42008-03-14 10:38:49 -07005023void iwl4965_free_geos(struct iwl_priv *priv)
Reinette Chatre849e0dc2008-01-23 10:15:18 -08005024{
Reinette Chatre849e0dc2008-01-23 10:15:18 -08005025 kfree(priv->ieee_channels);
5026 kfree(priv->ieee_rates);
5027 clear_bit(STATUS_GEO_CONFIGURED, &priv->status);
5028}
5029
Zhu Yib481de92007-09-25 17:54:57 -07005030/******************************************************************************
5031 *
5032 * uCode download functions
5033 *
5034 ******************************************************************************/
5035
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005036static void iwl4965_dealloc_ucode_pci(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005037{
Tomas Winkler98c92212008-01-14 17:46:20 -08005038 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_code);
5039 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_data);
5040 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_data_backup);
5041 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_init);
5042 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_init_data);
5043 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_boot);
Zhu Yib481de92007-09-25 17:54:57 -07005044}
5045
5046/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005047 * iwl4965_verify_inst_full - verify runtime uCode image in card vs. host,
Zhu Yib481de92007-09-25 17:54:57 -07005048 * looking at all data.
5049 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005050static int iwl4965_verify_inst_full(struct iwl_priv *priv, __le32 *image,
Ben Cahill9fbab512007-11-29 11:09:47 +08005051 u32 len)
Zhu Yib481de92007-09-25 17:54:57 -07005052{
5053 u32 val;
5054 u32 save_len = len;
5055 int rc = 0;
5056 u32 errcnt;
5057
5058 IWL_DEBUG_INFO("ucode inst image size is %u\n", len);
5059
Tomas Winkler3395f6e2008-03-25 16:33:37 -07005060 rc = iwl_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005061 if (rc)
5062 return rc;
5063
Tomas Winkler3395f6e2008-03-25 16:33:37 -07005064 iwl_write_direct32(priv, HBUS_TARG_MEM_RADDR, RTC_INST_LOWER_BOUND);
Zhu Yib481de92007-09-25 17:54:57 -07005065
5066 errcnt = 0;
5067 for (; len > 0; len -= sizeof(u32), image++) {
5068 /* read data comes through single port, auto-incr addr */
5069 /* NOTE: Use the debugless read so we don't flood kernel log
5070 * if IWL_DL_IO is set */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07005071 val = _iwl_read_direct32(priv, HBUS_TARG_MEM_RDAT);
Zhu Yib481de92007-09-25 17:54:57 -07005072 if (val != le32_to_cpu(*image)) {
5073 IWL_ERROR("uCode INST section is invalid at "
5074 "offset 0x%x, is 0x%x, s/b 0x%x\n",
5075 save_len - len, val, le32_to_cpu(*image));
5076 rc = -EIO;
5077 errcnt++;
5078 if (errcnt >= 20)
5079 break;
5080 }
5081 }
5082
Tomas Winkler3395f6e2008-03-25 16:33:37 -07005083 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005084
5085 if (!errcnt)
5086 IWL_DEBUG_INFO
5087 ("ucode image in INSTRUCTION memory is good\n");
5088
5089 return rc;
5090}
5091
5092
5093/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005094 * iwl4965_verify_inst_sparse - verify runtime uCode image in card vs. host,
Zhu Yib481de92007-09-25 17:54:57 -07005095 * using sample data 100 bytes apart. If these sample points are good,
5096 * it's a pretty good bet that everything between them is good, too.
5097 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005098static int iwl4965_verify_inst_sparse(struct iwl_priv *priv, __le32 *image, u32 len)
Zhu Yib481de92007-09-25 17:54:57 -07005099{
5100 u32 val;
5101 int rc = 0;
5102 u32 errcnt = 0;
5103 u32 i;
5104
5105 IWL_DEBUG_INFO("ucode inst image size is %u\n", len);
5106
Tomas Winkler3395f6e2008-03-25 16:33:37 -07005107 rc = iwl_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005108 if (rc)
5109 return rc;
5110
5111 for (i = 0; i < len; i += 100, image += 100/sizeof(u32)) {
5112 /* read data comes through single port, auto-incr addr */
5113 /* NOTE: Use the debugless read so we don't flood kernel log
5114 * if IWL_DL_IO is set */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07005115 iwl_write_direct32(priv, HBUS_TARG_MEM_RADDR,
Zhu Yib481de92007-09-25 17:54:57 -07005116 i + RTC_INST_LOWER_BOUND);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07005117 val = _iwl_read_direct32(priv, HBUS_TARG_MEM_RDAT);
Zhu Yib481de92007-09-25 17:54:57 -07005118 if (val != le32_to_cpu(*image)) {
5119#if 0 /* Enable this if you want to see details */
5120 IWL_ERROR("uCode INST section is invalid at "
5121 "offset 0x%x, is 0x%x, s/b 0x%x\n",
5122 i, val, *image);
5123#endif
5124 rc = -EIO;
5125 errcnt++;
5126 if (errcnt >= 3)
5127 break;
5128 }
5129 }
5130
Tomas Winkler3395f6e2008-03-25 16:33:37 -07005131 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005132
5133 return rc;
5134}
5135
5136
5137/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005138 * iwl4965_verify_ucode - determine which instruction image is in SRAM,
Zhu Yib481de92007-09-25 17:54:57 -07005139 * and verify its contents
5140 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005141static int iwl4965_verify_ucode(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005142{
5143 __le32 *image;
5144 u32 len;
5145 int rc = 0;
5146
5147 /* Try bootstrap */
5148 image = (__le32 *)priv->ucode_boot.v_addr;
5149 len = priv->ucode_boot.len;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005150 rc = iwl4965_verify_inst_sparse(priv, image, len);
Zhu Yib481de92007-09-25 17:54:57 -07005151 if (rc == 0) {
5152 IWL_DEBUG_INFO("Bootstrap uCode is good in inst SRAM\n");
5153 return 0;
5154 }
5155
5156 /* Try initialize */
5157 image = (__le32 *)priv->ucode_init.v_addr;
5158 len = priv->ucode_init.len;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005159 rc = iwl4965_verify_inst_sparse(priv, image, len);
Zhu Yib481de92007-09-25 17:54:57 -07005160 if (rc == 0) {
5161 IWL_DEBUG_INFO("Initialize uCode is good in inst SRAM\n");
5162 return 0;
5163 }
5164
5165 /* Try runtime/protocol */
5166 image = (__le32 *)priv->ucode_code.v_addr;
5167 len = priv->ucode_code.len;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005168 rc = iwl4965_verify_inst_sparse(priv, image, len);
Zhu Yib481de92007-09-25 17:54:57 -07005169 if (rc == 0) {
5170 IWL_DEBUG_INFO("Runtime uCode is good in inst SRAM\n");
5171 return 0;
5172 }
5173
5174 IWL_ERROR("NO VALID UCODE IMAGE IN INSTRUCTION SRAM!!\n");
5175
Ben Cahill9fbab512007-11-29 11:09:47 +08005176 /* Since nothing seems to match, show first several data entries in
5177 * instruction SRAM, so maybe visual inspection will give a clue.
5178 * Selection of bootstrap image (vs. other images) is arbitrary. */
Zhu Yib481de92007-09-25 17:54:57 -07005179 image = (__le32 *)priv->ucode_boot.v_addr;
5180 len = priv->ucode_boot.len;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005181 rc = iwl4965_verify_inst_full(priv, image, len);
Zhu Yib481de92007-09-25 17:54:57 -07005182
5183 return rc;
5184}
5185
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005186static void iwl4965_nic_start(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005187{
5188 /* Remove all resets to allow NIC to operate */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07005189 iwl_write32(priv, CSR_RESET, 0);
Zhu Yib481de92007-09-25 17:54:57 -07005190}
5191
Tomas Winkler90e759d2007-11-29 11:09:41 +08005192
Zhu Yib481de92007-09-25 17:54:57 -07005193/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005194 * iwl4965_read_ucode - Read uCode images from disk file.
Zhu Yib481de92007-09-25 17:54:57 -07005195 *
5196 * Copy into buffers for card to fetch via bus-mastering
5197 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005198static int iwl4965_read_ucode(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005199{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005200 struct iwl4965_ucode *ucode;
Tomas Winkler90e759d2007-11-29 11:09:41 +08005201 int ret;
Zhu Yib481de92007-09-25 17:54:57 -07005202 const struct firmware *ucode_raw;
Tomas Winkler4bf775c2008-03-04 18:09:31 -08005203 const char *name = priv->cfg->fw_name;
Zhu Yib481de92007-09-25 17:54:57 -07005204 u8 *src;
5205 size_t len;
5206 u32 ver, inst_size, data_size, init_size, init_data_size, boot_size;
5207
5208 /* Ask kernel firmware_class module to get the boot firmware off disk.
5209 * request_firmware() is synchronous, file is in memory on return. */
Tomas Winkler90e759d2007-11-29 11:09:41 +08005210 ret = request_firmware(&ucode_raw, name, &priv->pci_dev->dev);
5211 if (ret < 0) {
5212 IWL_ERROR("%s firmware file req failed: Reason %d\n",
5213 name, ret);
Zhu Yib481de92007-09-25 17:54:57 -07005214 goto error;
5215 }
5216
5217 IWL_DEBUG_INFO("Got firmware '%s' file (%zd bytes) from disk\n",
5218 name, ucode_raw->size);
5219
5220 /* Make sure that we got at least our header! */
5221 if (ucode_raw->size < sizeof(*ucode)) {
5222 IWL_ERROR("File size way too small!\n");
Tomas Winkler90e759d2007-11-29 11:09:41 +08005223 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07005224 goto err_release;
5225 }
5226
5227 /* Data from ucode file: header followed by uCode images */
5228 ucode = (void *)ucode_raw->data;
5229
5230 ver = le32_to_cpu(ucode->ver);
5231 inst_size = le32_to_cpu(ucode->inst_size);
5232 data_size = le32_to_cpu(ucode->data_size);
5233 init_size = le32_to_cpu(ucode->init_size);
5234 init_data_size = le32_to_cpu(ucode->init_data_size);
5235 boot_size = le32_to_cpu(ucode->boot_size);
5236
5237 IWL_DEBUG_INFO("f/w package hdr ucode version = 0x%x\n", ver);
5238 IWL_DEBUG_INFO("f/w package hdr runtime inst size = %u\n",
5239 inst_size);
5240 IWL_DEBUG_INFO("f/w package hdr runtime data size = %u\n",
5241 data_size);
5242 IWL_DEBUG_INFO("f/w package hdr init inst size = %u\n",
5243 init_size);
5244 IWL_DEBUG_INFO("f/w package hdr init data size = %u\n",
5245 init_data_size);
5246 IWL_DEBUG_INFO("f/w package hdr boot inst size = %u\n",
5247 boot_size);
5248
5249 /* Verify size of file vs. image size info in file's header */
5250 if (ucode_raw->size < sizeof(*ucode) +
5251 inst_size + data_size + init_size +
5252 init_data_size + boot_size) {
5253
5254 IWL_DEBUG_INFO("uCode file size %d too small\n",
5255 (int)ucode_raw->size);
Tomas Winkler90e759d2007-11-29 11:09:41 +08005256 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07005257 goto err_release;
5258 }
5259
5260 /* Verify that uCode images will fit in card's SRAM */
5261 if (inst_size > IWL_MAX_INST_SIZE) {
Tomas Winkler90e759d2007-11-29 11:09:41 +08005262 IWL_DEBUG_INFO("uCode instr len %d too large to fit in\n",
5263 inst_size);
5264 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07005265 goto err_release;
5266 }
5267
5268 if (data_size > IWL_MAX_DATA_SIZE) {
Tomas Winkler90e759d2007-11-29 11:09:41 +08005269 IWL_DEBUG_INFO("uCode data len %d too large to fit in\n",
5270 data_size);
5271 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07005272 goto err_release;
5273 }
5274 if (init_size > IWL_MAX_INST_SIZE) {
5275 IWL_DEBUG_INFO
Tomas Winkler90e759d2007-11-29 11:09:41 +08005276 ("uCode init instr len %d too large to fit in\n",
5277 init_size);
5278 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07005279 goto err_release;
5280 }
5281 if (init_data_size > IWL_MAX_DATA_SIZE) {
5282 IWL_DEBUG_INFO
Tomas Winkler90e759d2007-11-29 11:09:41 +08005283 ("uCode init data len %d too large to fit in\n",
5284 init_data_size);
5285 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07005286 goto err_release;
5287 }
5288 if (boot_size > IWL_MAX_BSM_SIZE) {
5289 IWL_DEBUG_INFO
Tomas Winkler90e759d2007-11-29 11:09:41 +08005290 ("uCode boot instr len %d too large to fit in\n",
5291 boot_size);
5292 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07005293 goto err_release;
5294 }
5295
5296 /* Allocate ucode buffers for card's bus-master loading ... */
5297
5298 /* Runtime instructions and 2 copies of data:
5299 * 1) unmodified from disk
5300 * 2) backup cache for save/restore during power-downs */
5301 priv->ucode_code.len = inst_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08005302 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_code);
Zhu Yib481de92007-09-25 17:54:57 -07005303
5304 priv->ucode_data.len = data_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08005305 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_data);
Zhu Yib481de92007-09-25 17:54:57 -07005306
5307 priv->ucode_data_backup.len = data_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08005308 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_data_backup);
Zhu Yib481de92007-09-25 17:54:57 -07005309
5310 /* Initialization instructions and data */
Tomas Winkler90e759d2007-11-29 11:09:41 +08005311 if (init_size && init_data_size) {
5312 priv->ucode_init.len = init_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08005313 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_init);
Zhu Yib481de92007-09-25 17:54:57 -07005314
Tomas Winkler90e759d2007-11-29 11:09:41 +08005315 priv->ucode_init_data.len = init_data_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08005316 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_init_data);
Tomas Winkler90e759d2007-11-29 11:09:41 +08005317
5318 if (!priv->ucode_init.v_addr || !priv->ucode_init_data.v_addr)
5319 goto err_pci_alloc;
5320 }
Zhu Yib481de92007-09-25 17:54:57 -07005321
5322 /* Bootstrap (instructions only, no data) */
Tomas Winkler90e759d2007-11-29 11:09:41 +08005323 if (boot_size) {
5324 priv->ucode_boot.len = boot_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08005325 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_boot);
Zhu Yib481de92007-09-25 17:54:57 -07005326
Tomas Winkler90e759d2007-11-29 11:09:41 +08005327 if (!priv->ucode_boot.v_addr)
5328 goto err_pci_alloc;
5329 }
Zhu Yib481de92007-09-25 17:54:57 -07005330
5331 /* Copy images into buffers for card's bus-master reads ... */
5332
5333 /* Runtime instructions (first block of data in file) */
5334 src = &ucode->data[0];
5335 len = priv->ucode_code.len;
Tomas Winkler90e759d2007-11-29 11:09:41 +08005336 IWL_DEBUG_INFO("Copying (but not loading) uCode instr len %Zd\n", len);
Zhu Yib481de92007-09-25 17:54:57 -07005337 memcpy(priv->ucode_code.v_addr, src, len);
5338 IWL_DEBUG_INFO("uCode instr buf vaddr = 0x%p, paddr = 0x%08x\n",
5339 priv->ucode_code.v_addr, (u32)priv->ucode_code.p_addr);
5340
5341 /* Runtime data (2nd block)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005342 * NOTE: Copy into backup buffer will be done in iwl4965_up() */
Zhu Yib481de92007-09-25 17:54:57 -07005343 src = &ucode->data[inst_size];
5344 len = priv->ucode_data.len;
Tomas Winkler90e759d2007-11-29 11:09:41 +08005345 IWL_DEBUG_INFO("Copying (but not loading) uCode data len %Zd\n", len);
Zhu Yib481de92007-09-25 17:54:57 -07005346 memcpy(priv->ucode_data.v_addr, src, len);
5347 memcpy(priv->ucode_data_backup.v_addr, src, len);
5348
5349 /* Initialization instructions (3rd block) */
5350 if (init_size) {
5351 src = &ucode->data[inst_size + data_size];
5352 len = priv->ucode_init.len;
Tomas Winkler90e759d2007-11-29 11:09:41 +08005353 IWL_DEBUG_INFO("Copying (but not loading) init instr len %Zd\n",
5354 len);
Zhu Yib481de92007-09-25 17:54:57 -07005355 memcpy(priv->ucode_init.v_addr, src, len);
5356 }
5357
5358 /* Initialization data (4th block) */
5359 if (init_data_size) {
5360 src = &ucode->data[inst_size + data_size + init_size];
5361 len = priv->ucode_init_data.len;
Tomas Winkler90e759d2007-11-29 11:09:41 +08005362 IWL_DEBUG_INFO("Copying (but not loading) init data len %Zd\n",
5363 len);
Zhu Yib481de92007-09-25 17:54:57 -07005364 memcpy(priv->ucode_init_data.v_addr, src, len);
5365 }
5366
5367 /* Bootstrap instructions (5th block) */
5368 src = &ucode->data[inst_size + data_size + init_size + init_data_size];
5369 len = priv->ucode_boot.len;
Tomas Winkler90e759d2007-11-29 11:09:41 +08005370 IWL_DEBUG_INFO("Copying (but not loading) boot instr len %Zd\n", len);
Zhu Yib481de92007-09-25 17:54:57 -07005371 memcpy(priv->ucode_boot.v_addr, src, len);
5372
5373 /* We have our copies now, allow OS release its copies */
5374 release_firmware(ucode_raw);
5375 return 0;
5376
5377 err_pci_alloc:
5378 IWL_ERROR("failed to allocate pci memory\n");
Tomas Winkler90e759d2007-11-29 11:09:41 +08005379 ret = -ENOMEM;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005380 iwl4965_dealloc_ucode_pci(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005381
5382 err_release:
5383 release_firmware(ucode_raw);
5384
5385 error:
Tomas Winkler90e759d2007-11-29 11:09:41 +08005386 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07005387}
5388
5389
5390/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005391 * iwl4965_set_ucode_ptrs - Set uCode address location
Zhu Yib481de92007-09-25 17:54:57 -07005392 *
5393 * Tell initialization uCode where to find runtime uCode.
5394 *
5395 * BSM registers initially contain pointers to initialization uCode.
5396 * We need to replace them to load runtime uCode inst and data,
5397 * and to save runtime data when powering down.
5398 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005399static int iwl4965_set_ucode_ptrs(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005400{
5401 dma_addr_t pinst;
5402 dma_addr_t pdata;
5403 int rc = 0;
5404 unsigned long flags;
5405
5406 /* bits 35:4 for 4965 */
5407 pinst = priv->ucode_code.p_addr >> 4;
5408 pdata = priv->ucode_data_backup.p_addr >> 4;
5409
5410 spin_lock_irqsave(&priv->lock, flags);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07005411 rc = iwl_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005412 if (rc) {
5413 spin_unlock_irqrestore(&priv->lock, flags);
5414 return rc;
5415 }
5416
5417 /* Tell bootstrap uCode where to find image to load */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07005418 iwl_write_prph(priv, BSM_DRAM_INST_PTR_REG, pinst);
5419 iwl_write_prph(priv, BSM_DRAM_DATA_PTR_REG, pdata);
5420 iwl_write_prph(priv, BSM_DRAM_DATA_BYTECOUNT_REG,
Zhu Yib481de92007-09-25 17:54:57 -07005421 priv->ucode_data.len);
5422
5423 /* Inst bytecount must be last to set up, bit 31 signals uCode
5424 * that all new ptr/size info is in place */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07005425 iwl_write_prph(priv, BSM_DRAM_INST_BYTECOUNT_REG,
Zhu Yib481de92007-09-25 17:54:57 -07005426 priv->ucode_code.len | BSM_DRAM_INST_LOAD);
5427
Tomas Winkler3395f6e2008-03-25 16:33:37 -07005428 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005429
5430 spin_unlock_irqrestore(&priv->lock, flags);
5431
5432 IWL_DEBUG_INFO("Runtime uCode pointers are set.\n");
5433
5434 return rc;
5435}
5436
5437/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005438 * iwl4965_init_alive_start - Called after REPLY_ALIVE notification received
Zhu Yib481de92007-09-25 17:54:57 -07005439 *
5440 * Called after REPLY_ALIVE notification received from "initialize" uCode.
5441 *
5442 * The 4965 "initialize" ALIVE reply contains calibration data for:
5443 * Voltage, temperature, and MIMO tx gain correction, now stored in priv
5444 * (3945 does not contain this data).
5445 *
5446 * Tell "initialize" uCode to go ahead and load the runtime uCode.
5447*/
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005448static void iwl4965_init_alive_start(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005449{
5450 /* Check alive response for "valid" sign from uCode */
5451 if (priv->card_alive_init.is_valid != UCODE_VALID_OK) {
5452 /* We had an error bringing up the hardware, so take it
5453 * all the way back down so we can try again */
5454 IWL_DEBUG_INFO("Initialize Alive failed.\n");
5455 goto restart;
5456 }
5457
5458 /* Bootstrap uCode has loaded initialize uCode ... verify inst image.
5459 * This is a paranoid check, because we would not have gotten the
5460 * "initialize" alive if code weren't properly loaded. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005461 if (iwl4965_verify_ucode(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07005462 /* Runtime instruction load was bad;
5463 * take it all the way back down so we can try again */
5464 IWL_DEBUG_INFO("Bad \"initialize\" uCode load.\n");
5465 goto restart;
5466 }
5467
5468 /* Calculate temperature */
5469 priv->temperature = iwl4965_get_temperature(priv);
5470
5471 /* Send pointers to protocol/runtime uCode image ... init code will
5472 * load and launch runtime uCode, which will send us another "Alive"
5473 * notification. */
5474 IWL_DEBUG_INFO("Initialization Alive received.\n");
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005475 if (iwl4965_set_ucode_ptrs(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07005476 /* Runtime instruction load won't happen;
5477 * take it all the way back down so we can try again */
5478 IWL_DEBUG_INFO("Couldn't set up uCode pointers.\n");
5479 goto restart;
5480 }
5481 return;
5482
5483 restart:
5484 queue_work(priv->workqueue, &priv->restart);
5485}
5486
5487
5488/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005489 * iwl4965_alive_start - called after REPLY_ALIVE notification received
Zhu Yib481de92007-09-25 17:54:57 -07005490 * from protocol/runtime uCode (initialization uCode's
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005491 * Alive gets handled by iwl4965_init_alive_start()).
Zhu Yib481de92007-09-25 17:54:57 -07005492 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005493static void iwl4965_alive_start(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005494{
Tomas Winkler57aab752008-04-14 21:16:03 -07005495 int ret = 0;
Zhu Yib481de92007-09-25 17:54:57 -07005496
5497 IWL_DEBUG_INFO("Runtime Alive received.\n");
5498
5499 if (priv->card_alive.is_valid != UCODE_VALID_OK) {
5500 /* We had an error bringing up the hardware, so take it
5501 * all the way back down so we can try again */
5502 IWL_DEBUG_INFO("Alive failed.\n");
5503 goto restart;
5504 }
5505
5506 /* Initialize uCode has loaded Runtime uCode ... verify inst image.
5507 * This is a paranoid check, because we would not have gotten the
5508 * "runtime" alive if code weren't properly loaded. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005509 if (iwl4965_verify_ucode(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07005510 /* Runtime instruction load was bad;
5511 * take it all the way back down so we can try again */
5512 IWL_DEBUG_INFO("Bad runtime uCode load.\n");
5513 goto restart;
5514 }
5515
Assaf Kraussbf85ea42008-03-14 10:38:49 -07005516 iwlcore_clear_stations_table(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005517
Tomas Winkler57aab752008-04-14 21:16:03 -07005518 ret = priv->cfg->ops->lib->alive_notify(priv);
5519 if (ret) {
Zhu Yib481de92007-09-25 17:54:57 -07005520 IWL_WARNING("Could not complete ALIVE transition [ntf]: %d\n",
Tomas Winkler57aab752008-04-14 21:16:03 -07005521 ret);
Zhu Yib481de92007-09-25 17:54:57 -07005522 goto restart;
5523 }
5524
Ben Cahill9fbab512007-11-29 11:09:47 +08005525 /* After the ALIVE response, we can send host commands to 4965 uCode */
Zhu Yib481de92007-09-25 17:54:57 -07005526 set_bit(STATUS_ALIVE, &priv->status);
5527
5528 /* Clear out the uCode error bit if it is set */
5529 clear_bit(STATUS_FW_ERROR, &priv->status);
5530
Tomas Winklerfee12472008-04-03 16:05:21 -07005531 if (iwl_is_rfkill(priv))
Zhu Yib481de92007-09-25 17:54:57 -07005532 return;
5533
Zhu Yi5a66926a2008-01-14 17:46:18 -08005534 ieee80211_start_queues(priv->hw);
Zhu Yib481de92007-09-25 17:54:57 -07005535
5536 priv->active_rate = priv->rates_mask;
5537 priv->active_rate_basic = priv->rates_mask & IWL_BASIC_RATES_MASK;
5538
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005539 iwl4965_send_power_mode(priv, IWL_POWER_LEVEL(priv->power_mode));
Zhu Yib481de92007-09-25 17:54:57 -07005540
Tomas Winkler3109ece2008-03-28 16:33:35 -07005541 if (iwl_is_associated(priv)) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005542 struct iwl4965_rxon_cmd *active_rxon =
5543 (struct iwl4965_rxon_cmd *)(&priv->active_rxon);
Zhu Yib481de92007-09-25 17:54:57 -07005544
5545 memcpy(&priv->staging_rxon, &priv->active_rxon,
5546 sizeof(priv->staging_rxon));
5547 active_rxon->filter_flags &= ~RXON_FILTER_ASSOC_MSK;
5548 } else {
5549 /* Initialize our rx_config data */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005550 iwl4965_connection_init_rx_config(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005551 memcpy(priv->staging_rxon.node_addr, priv->mac_addr, ETH_ALEN);
5552 }
5553
Ben Cahill9fbab512007-11-29 11:09:47 +08005554 /* Configure Bluetooth device coexistence support */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005555 iwl4965_send_bt_config(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005556
5557 /* Configure the adapter for unassociated operation */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005558 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005559
5560 /* At this point, the NIC is initialized and operational */
5561 priv->notif_missed_beacons = 0;
Zhu Yib481de92007-09-25 17:54:57 -07005562
5563 iwl4965_rf_kill_ct_config(priv);
Zhu Yi5a66926a2008-01-14 17:46:18 -08005564
Reinette Chatrefe00b5a2008-04-03 16:05:23 -07005565 iwl_leds_register(priv);
5566
Zhu Yib481de92007-09-25 17:54:57 -07005567 IWL_DEBUG_INFO("ALIVE processing complete.\n");
Rick Farringtona9f46782008-03-18 14:57:49 -07005568 set_bit(STATUS_READY, &priv->status);
Zhu Yi5a66926a2008-01-14 17:46:18 -08005569 wake_up_interruptible(&priv->wait_command_queue);
Zhu Yib481de92007-09-25 17:54:57 -07005570
5571 if (priv->error_recovering)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005572 iwl4965_error_recovery(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005573
Mohamed Abbasc8381fd2008-03-28 16:21:05 -07005574 iwlcore_low_level_notify(priv, IWLCORE_START_EVT);
Mohamed Abbas84363e62008-04-04 16:59:58 -07005575 ieee80211_notify_mac(priv->hw, IEEE80211_NOTIFY_RE_ASSOC);
Zhu Yib481de92007-09-25 17:54:57 -07005576 return;
5577
5578 restart:
5579 queue_work(priv->workqueue, &priv->restart);
5580}
5581
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005582static void iwl4965_cancel_deferred_work(struct iwl_priv *priv);
Zhu Yib481de92007-09-25 17:54:57 -07005583
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005584static void __iwl4965_down(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005585{
5586 unsigned long flags;
5587 int exit_pending = test_bit(STATUS_EXIT_PENDING, &priv->status);
5588 struct ieee80211_conf *conf = NULL;
5589
5590 IWL_DEBUG_INFO(DRV_NAME " is going down\n");
5591
5592 conf = ieee80211_get_hw_conf(priv->hw);
5593
5594 if (!exit_pending)
5595 set_bit(STATUS_EXIT_PENDING, &priv->status);
5596
Mohamed Abbasab53d8a2008-03-25 16:33:36 -07005597 iwl_leds_unregister(priv);
5598
Mohamed Abbasc8381fd2008-03-28 16:21:05 -07005599 iwlcore_low_level_notify(priv, IWLCORE_STOP_EVT);
5600
Assaf Kraussbf85ea42008-03-14 10:38:49 -07005601 iwlcore_clear_stations_table(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005602
5603 /* Unblock any waiting calls */
5604 wake_up_interruptible_all(&priv->wait_command_queue);
5605
Zhu Yib481de92007-09-25 17:54:57 -07005606 /* Wipe out the EXIT_PENDING status bit if we are not actually
5607 * exiting the module */
5608 if (!exit_pending)
5609 clear_bit(STATUS_EXIT_PENDING, &priv->status);
5610
5611 /* stop and reset the on-board processor */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07005612 iwl_write32(priv, CSR_RESET, CSR_RESET_REG_FLAG_NEVO_RESET);
Zhu Yib481de92007-09-25 17:54:57 -07005613
5614 /* tell the device to stop sending interrupts */
Mohamed Abbas0359fac2008-03-28 16:21:08 -07005615 spin_lock_irqsave(&priv->lock, flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005616 iwl4965_disable_interrupts(priv);
Mohamed Abbas0359fac2008-03-28 16:21:08 -07005617 spin_unlock_irqrestore(&priv->lock, flags);
5618 iwl_synchronize_irq(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005619
5620 if (priv->mac80211_registered)
5621 ieee80211_stop_queues(priv->hw);
5622
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005623 /* If we have not previously called iwl4965_init() then
Zhu Yib481de92007-09-25 17:54:57 -07005624 * clear all bits but the RF Kill and SUSPEND bits and return */
Tomas Winklerfee12472008-04-03 16:05:21 -07005625 if (!iwl_is_init(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07005626 priv->status = test_bit(STATUS_RF_KILL_HW, &priv->status) <<
5627 STATUS_RF_KILL_HW |
5628 test_bit(STATUS_RF_KILL_SW, &priv->status) <<
5629 STATUS_RF_KILL_SW |
Reinette Chatre97888642008-02-06 11:20:38 -08005630 test_bit(STATUS_GEO_CONFIGURED, &priv->status) <<
5631 STATUS_GEO_CONFIGURED |
Zhu Yib481de92007-09-25 17:54:57 -07005632 test_bit(STATUS_IN_SUSPEND, &priv->status) <<
5633 STATUS_IN_SUSPEND;
5634 goto exit;
5635 }
5636
5637 /* ...otherwise clear out all the status bits but the RF Kill and
5638 * SUSPEND bits and continue taking the NIC down. */
5639 priv->status &= test_bit(STATUS_RF_KILL_HW, &priv->status) <<
5640 STATUS_RF_KILL_HW |
5641 test_bit(STATUS_RF_KILL_SW, &priv->status) <<
5642 STATUS_RF_KILL_SW |
Reinette Chatre97888642008-02-06 11:20:38 -08005643 test_bit(STATUS_GEO_CONFIGURED, &priv->status) <<
5644 STATUS_GEO_CONFIGURED |
Zhu Yib481de92007-09-25 17:54:57 -07005645 test_bit(STATUS_IN_SUSPEND, &priv->status) <<
5646 STATUS_IN_SUSPEND |
5647 test_bit(STATUS_FW_ERROR, &priv->status) <<
5648 STATUS_FW_ERROR;
5649
5650 spin_lock_irqsave(&priv->lock, flags);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07005651 iwl_clear_bit(priv, CSR_GP_CNTRL,
Ben Cahill9fbab512007-11-29 11:09:47 +08005652 CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
Zhu Yib481de92007-09-25 17:54:57 -07005653 spin_unlock_irqrestore(&priv->lock, flags);
5654
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005655 iwl4965_hw_txq_ctx_stop(priv);
5656 iwl4965_hw_rxq_stop(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005657
5658 spin_lock_irqsave(&priv->lock, flags);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07005659 if (!iwl_grab_nic_access(priv)) {
5660 iwl_write_prph(priv, APMG_CLK_DIS_REG,
Zhu Yib481de92007-09-25 17:54:57 -07005661 APMG_CLK_VAL_DMA_CLK_RQT);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07005662 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005663 }
5664 spin_unlock_irqrestore(&priv->lock, flags);
5665
5666 udelay(5);
5667
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005668 iwl4965_hw_nic_stop_master(priv);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07005669 iwl_set_bit(priv, CSR_RESET, CSR_RESET_REG_FLAG_SW_RESET);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005670 iwl4965_hw_nic_reset(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005671
5672 exit:
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005673 memset(&priv->card_alive, 0, sizeof(struct iwl4965_alive_resp));
Zhu Yib481de92007-09-25 17:54:57 -07005674
5675 if (priv->ibss_beacon)
5676 dev_kfree_skb(priv->ibss_beacon);
5677 priv->ibss_beacon = NULL;
5678
5679 /* clear out any free frames */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005680 iwl4965_clear_free_frames(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005681}
5682
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005683static void iwl4965_down(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005684{
5685 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005686 __iwl4965_down(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005687 mutex_unlock(&priv->mutex);
Zhu Yib24d22b2007-12-19 13:59:52 +08005688
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005689 iwl4965_cancel_deferred_work(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005690}
5691
5692#define MAX_HW_RESTARTS 5
5693
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005694static int __iwl4965_up(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005695{
Tomas Winkler57aab752008-04-14 21:16:03 -07005696 int i;
5697 int ret;
Zhu Yib481de92007-09-25 17:54:57 -07005698
5699 if (test_bit(STATUS_EXIT_PENDING, &priv->status)) {
5700 IWL_WARNING("Exit pending; will not bring the NIC up\n");
5701 return -EIO;
5702 }
5703
5704 if (test_bit(STATUS_RF_KILL_SW, &priv->status)) {
5705 IWL_WARNING("Radio disabled by SW RF kill (module "
5706 "parameter)\n");
Mohamed Abbasad97edd2008-03-28 16:21:06 -07005707 iwl_rfkill_set_hw_state(priv);
Zhu Yie655b9f2008-01-24 02:19:38 -08005708 return -ENODEV;
5709 }
5710
Reinette Chatree903fbd2008-01-30 22:05:15 -08005711 if (!priv->ucode_data_backup.v_addr || !priv->ucode_data.v_addr) {
5712 IWL_ERROR("ucode not available for device bringup\n");
5713 return -EIO;
5714 }
5715
Zhu Yie655b9f2008-01-24 02:19:38 -08005716 /* If platform's RF_KILL switch is NOT set to KILL */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07005717 if (iwl_read32(priv, CSR_GP_CNTRL) &
Zhu Yie655b9f2008-01-24 02:19:38 -08005718 CSR_GP_CNTRL_REG_FLAG_HW_RF_KILL_SW)
5719 clear_bit(STATUS_RF_KILL_HW, &priv->status);
5720 else {
5721 set_bit(STATUS_RF_KILL_HW, &priv->status);
5722 if (!test_bit(STATUS_IN_SUSPEND, &priv->status)) {
Mohamed Abbasad97edd2008-03-28 16:21:06 -07005723 iwl_rfkill_set_hw_state(priv);
Zhu Yie655b9f2008-01-24 02:19:38 -08005724 IWL_WARNING("Radio disabled by HW RF Kill switch\n");
5725 return -ENODEV;
5726 }
Zhu Yib481de92007-09-25 17:54:57 -07005727 }
5728
Mohamed Abbasad97edd2008-03-28 16:21:06 -07005729 iwl_rfkill_set_hw_state(priv);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07005730 iwl_write32(priv, CSR_INT, 0xFFFFFFFF);
Zhu Yib481de92007-09-25 17:54:57 -07005731
Tomas Winkler57aab752008-04-14 21:16:03 -07005732 ret = priv->cfg->ops->lib->hw_nic_init(priv);
5733 if (ret) {
5734 IWL_ERROR("Unable to init nic\n");
5735 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07005736 }
5737
5738 /* make sure rfkill handshake bits are cleared */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07005739 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
5740 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR,
Zhu Yib481de92007-09-25 17:54:57 -07005741 CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
5742
5743 /* clear (again), then enable host interrupts */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07005744 iwl_write32(priv, CSR_INT, 0xFFFFFFFF);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005745 iwl4965_enable_interrupts(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005746
5747 /* really make sure rfkill handshake bits are cleared */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07005748 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
5749 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
Zhu Yib481de92007-09-25 17:54:57 -07005750
5751 /* Copy original ucode data image from disk into backup cache.
5752 * This will be used to initialize the on-board processor's
5753 * data SRAM for a clean start when the runtime program first loads. */
5754 memcpy(priv->ucode_data_backup.v_addr, priv->ucode_data.v_addr,
Zhu Yi5a66926a2008-01-14 17:46:18 -08005755 priv->ucode_data.len);
Zhu Yib481de92007-09-25 17:54:57 -07005756
Zhu Yie655b9f2008-01-24 02:19:38 -08005757 /* We return success when we resume from suspend and rf_kill is on. */
5758 if (test_bit(STATUS_RF_KILL_HW, &priv->status))
Zhu Yib481de92007-09-25 17:54:57 -07005759 return 0;
Zhu Yib481de92007-09-25 17:54:57 -07005760
5761 for (i = 0; i < MAX_HW_RESTARTS; i++) {
5762
Assaf Kraussbf85ea42008-03-14 10:38:49 -07005763 iwlcore_clear_stations_table(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005764
5765 /* load bootstrap state machine,
5766 * load bootstrap program into processor's memory,
5767 * prepare to load the "initialize" uCode */
Tomas Winkler57aab752008-04-14 21:16:03 -07005768 ret = priv->cfg->ops->lib->load_ucode(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005769
Tomas Winkler57aab752008-04-14 21:16:03 -07005770 if (ret) {
5771 IWL_ERROR("Unable to set up bootstrap uCode: %d\n", ret);
Zhu Yib481de92007-09-25 17:54:57 -07005772 continue;
5773 }
5774
5775 /* start card; "initialize" will load runtime ucode */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005776 iwl4965_nic_start(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005777
Zhu Yib481de92007-09-25 17:54:57 -07005778 IWL_DEBUG_INFO(DRV_NAME " is coming up\n");
5779
5780 return 0;
5781 }
5782
5783 set_bit(STATUS_EXIT_PENDING, &priv->status);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005784 __iwl4965_down(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005785
5786 /* tried to restart and config the device for as long as our
5787 * patience could withstand */
5788 IWL_ERROR("Unable to initialize device after %d attempts.\n", i);
5789 return -EIO;
5790}
5791
5792
5793/*****************************************************************************
5794 *
5795 * Workqueue callbacks
5796 *
5797 *****************************************************************************/
5798
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005799static void iwl4965_bg_init_alive_start(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07005800{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005801 struct iwl_priv *priv =
5802 container_of(data, struct iwl_priv, init_alive_start.work);
Zhu Yib481de92007-09-25 17:54:57 -07005803
5804 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
5805 return;
5806
5807 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005808 iwl4965_init_alive_start(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005809 mutex_unlock(&priv->mutex);
5810}
5811
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005812static void iwl4965_bg_alive_start(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07005813{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005814 struct iwl_priv *priv =
5815 container_of(data, struct iwl_priv, alive_start.work);
Zhu Yib481de92007-09-25 17:54:57 -07005816
5817 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
5818 return;
5819
5820 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005821 iwl4965_alive_start(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005822 mutex_unlock(&priv->mutex);
5823}
5824
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005825static void iwl4965_bg_rf_kill(struct work_struct *work)
Zhu Yib481de92007-09-25 17:54:57 -07005826{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005827 struct iwl_priv *priv = container_of(work, struct iwl_priv, rf_kill);
Zhu Yib481de92007-09-25 17:54:57 -07005828
5829 wake_up_interruptible(&priv->wait_command_queue);
5830
5831 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
5832 return;
5833
5834 mutex_lock(&priv->mutex);
5835
Tomas Winklerfee12472008-04-03 16:05:21 -07005836 if (!iwl_is_rfkill(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07005837 IWL_DEBUG(IWL_DL_INFO | IWL_DL_RF_KILL,
5838 "HW and/or SW RF Kill no longer active, restarting "
5839 "device\n");
5840 if (!test_bit(STATUS_EXIT_PENDING, &priv->status))
5841 queue_work(priv->workqueue, &priv->restart);
5842 } else {
Mohamed Abbasad97edd2008-03-28 16:21:06 -07005843 /* make sure mac80211 stop sending Tx frame */
5844 if (priv->mac80211_registered)
5845 ieee80211_stop_queues(priv->hw);
Zhu Yib481de92007-09-25 17:54:57 -07005846
5847 if (!test_bit(STATUS_RF_KILL_HW, &priv->status))
5848 IWL_DEBUG_RF_KILL("Can not turn radio back on - "
5849 "disabled by SW switch\n");
5850 else
5851 IWL_WARNING("Radio Frequency Kill Switch is On:\n"
5852 "Kill switch must be turned off for "
5853 "wireless networking to work.\n");
5854 }
Mohamed Abbasad97edd2008-03-28 16:21:06 -07005855 iwl_rfkill_set_hw_state(priv);
5856
Zhu Yib481de92007-09-25 17:54:57 -07005857 mutex_unlock(&priv->mutex);
5858}
5859
5860#define IWL_SCAN_CHECK_WATCHDOG (7 * HZ)
5861
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005862static void iwl4965_bg_scan_check(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07005863{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005864 struct iwl_priv *priv =
5865 container_of(data, struct iwl_priv, scan_check.work);
Zhu Yib481de92007-09-25 17:54:57 -07005866
5867 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
5868 return;
5869
5870 mutex_lock(&priv->mutex);
5871 if (test_bit(STATUS_SCANNING, &priv->status) ||
5872 test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
5873 IWL_DEBUG(IWL_DL_INFO | IWL_DL_SCAN,
5874 "Scan completion watchdog resetting adapter (%dms)\n",
5875 jiffies_to_msecs(IWL_SCAN_CHECK_WATCHDOG));
mabbas052c4b92007-10-25 17:15:43 +08005876
Zhu Yib481de92007-09-25 17:54:57 -07005877 if (!test_bit(STATUS_EXIT_PENDING, &priv->status))
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005878 iwl4965_send_scan_abort(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005879 }
5880 mutex_unlock(&priv->mutex);
5881}
5882
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005883static void iwl4965_bg_request_scan(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07005884{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005885 struct iwl_priv *priv =
5886 container_of(data, struct iwl_priv, request_scan);
Tomas Winkler857485c2008-03-21 13:53:44 -07005887 struct iwl_host_cmd cmd = {
Zhu Yib481de92007-09-25 17:54:57 -07005888 .id = REPLY_SCAN_CMD,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005889 .len = sizeof(struct iwl4965_scan_cmd),
Zhu Yib481de92007-09-25 17:54:57 -07005890 .meta.flags = CMD_SIZE_HUGE,
5891 };
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005892 struct iwl4965_scan_cmd *scan;
Zhu Yib481de92007-09-25 17:54:57 -07005893 struct ieee80211_conf *conf = NULL;
Tomas Winkler78330fd2008-02-06 02:37:18 +02005894 u16 cmd_len;
Johannes Berg8318d782008-01-24 19:38:38 +01005895 enum ieee80211_band band;
Tomas Winkler78330fd2008-02-06 02:37:18 +02005896 u8 direct_mask;
Tomas Winkler857485c2008-03-21 13:53:44 -07005897 int ret = 0;
Zhu Yib481de92007-09-25 17:54:57 -07005898
5899 conf = ieee80211_get_hw_conf(priv->hw);
5900
5901 mutex_lock(&priv->mutex);
5902
Tomas Winklerfee12472008-04-03 16:05:21 -07005903 if (!iwl_is_ready(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07005904 IWL_WARNING("request scan called when driver not ready.\n");
5905 goto done;
5906 }
5907
5908 /* Make sure the scan wasn't cancelled before this queued work
5909 * was given the chance to run... */
5910 if (!test_bit(STATUS_SCANNING, &priv->status))
5911 goto done;
5912
5913 /* This should never be called or scheduled if there is currently
5914 * a scan active in the hardware. */
5915 if (test_bit(STATUS_SCAN_HW, &priv->status)) {
5916 IWL_DEBUG_INFO("Multiple concurrent scan requests in parallel. "
5917 "Ignoring second request.\n");
Tomas Winkler857485c2008-03-21 13:53:44 -07005918 ret = -EIO;
Zhu Yib481de92007-09-25 17:54:57 -07005919 goto done;
5920 }
5921
5922 if (test_bit(STATUS_EXIT_PENDING, &priv->status)) {
5923 IWL_DEBUG_SCAN("Aborting scan due to device shutdown\n");
5924 goto done;
5925 }
5926
5927 if (test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
5928 IWL_DEBUG_HC("Scan request while abort pending. Queuing.\n");
5929 goto done;
5930 }
5931
Tomas Winklerfee12472008-04-03 16:05:21 -07005932 if (iwl_is_rfkill(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07005933 IWL_DEBUG_HC("Aborting scan due to RF Kill activation\n");
5934 goto done;
5935 }
5936
5937 if (!test_bit(STATUS_READY, &priv->status)) {
5938 IWL_DEBUG_HC("Scan request while uninitialized. Queuing.\n");
5939 goto done;
5940 }
5941
5942 if (!priv->scan_bands) {
5943 IWL_DEBUG_HC("Aborting scan due to no requested bands\n");
5944 goto done;
5945 }
5946
5947 if (!priv->scan) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005948 priv->scan = kmalloc(sizeof(struct iwl4965_scan_cmd) +
Zhu Yib481de92007-09-25 17:54:57 -07005949 IWL_MAX_SCAN_SIZE, GFP_KERNEL);
5950 if (!priv->scan) {
Tomas Winkler857485c2008-03-21 13:53:44 -07005951 ret = -ENOMEM;
Zhu Yib481de92007-09-25 17:54:57 -07005952 goto done;
5953 }
5954 }
5955 scan = priv->scan;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005956 memset(scan, 0, sizeof(struct iwl4965_scan_cmd) + IWL_MAX_SCAN_SIZE);
Zhu Yib481de92007-09-25 17:54:57 -07005957
5958 scan->quiet_plcp_th = IWL_PLCP_QUIET_THRESH;
5959 scan->quiet_time = IWL_ACTIVE_QUIET_TIME;
5960
Tomas Winkler3109ece2008-03-28 16:33:35 -07005961 if (iwl_is_associated(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07005962 u16 interval = 0;
5963 u32 extra;
5964 u32 suspend_time = 100;
5965 u32 scan_suspend_time = 100;
5966 unsigned long flags;
5967
5968 IWL_DEBUG_INFO("Scanning while associated...\n");
5969
5970 spin_lock_irqsave(&priv->lock, flags);
5971 interval = priv->beacon_int;
5972 spin_unlock_irqrestore(&priv->lock, flags);
5973
5974 scan->suspend_time = 0;
mabbas052c4b92007-10-25 17:15:43 +08005975 scan->max_out_time = cpu_to_le32(200 * 1024);
Zhu Yib481de92007-09-25 17:54:57 -07005976 if (!interval)
5977 interval = suspend_time;
5978
5979 extra = (suspend_time / interval) << 22;
5980 scan_suspend_time = (extra |
5981 ((suspend_time % interval) * 1024));
5982 scan->suspend_time = cpu_to_le32(scan_suspend_time);
5983 IWL_DEBUG_SCAN("suspend_time 0x%X beacon interval %d\n",
5984 scan_suspend_time, interval);
5985 }
5986
5987 /* We should add the ability for user to lock to PASSIVE ONLY */
5988 if (priv->one_direct_scan) {
5989 IWL_DEBUG_SCAN
5990 ("Kicking off one direct scan for '%s'\n",
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005991 iwl4965_escape_essid(priv->direct_ssid,
Zhu Yib481de92007-09-25 17:54:57 -07005992 priv->direct_ssid_len));
5993 scan->direct_scan[0].id = WLAN_EID_SSID;
5994 scan->direct_scan[0].len = priv->direct_ssid_len;
5995 memcpy(scan->direct_scan[0].ssid,
5996 priv->direct_ssid, priv->direct_ssid_len);
5997 direct_mask = 1;
Tomas Winkler3109ece2008-03-28 16:33:35 -07005998 } else if (!iwl_is_associated(priv) && priv->essid_len) {
Zhu Yib481de92007-09-25 17:54:57 -07005999 scan->direct_scan[0].id = WLAN_EID_SSID;
6000 scan->direct_scan[0].len = priv->essid_len;
6001 memcpy(scan->direct_scan[0].ssid, priv->essid, priv->essid_len);
6002 direct_mask = 1;
Tomas Winkler857485c2008-03-21 13:53:44 -07006003 } else {
Zhu Yib481de92007-09-25 17:54:57 -07006004 direct_mask = 0;
Tomas Winkler857485c2008-03-21 13:53:44 -07006005 }
Zhu Yib481de92007-09-25 17:54:57 -07006006
Zhu Yib481de92007-09-25 17:54:57 -07006007 scan->tx_cmd.tx_flags = TX_CMD_FLG_SEQ_CTL_MSK;
6008 scan->tx_cmd.sta_id = priv->hw_setting.bcast_sta_id;
6009 scan->tx_cmd.stop_time.life_time = TX_CMD_LIFE_TIME_INFINITE;
6010
Zhu Yib481de92007-09-25 17:54:57 -07006011
6012 switch (priv->scan_bands) {
6013 case 2:
6014 scan->flags = RXON_FLG_BAND_24G_MSK | RXON_FLG_AUTO_DETECT_MSK;
6015 scan->tx_cmd.rate_n_flags =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006016 iwl4965_hw_set_rate_n_flags(IWL_RATE_1M_PLCP,
Zhu Yib481de92007-09-25 17:54:57 -07006017 RATE_MCS_ANT_B_MSK|RATE_MCS_CCK_MSK);
6018
6019 scan->good_CRC_th = 0;
Johannes Berg8318d782008-01-24 19:38:38 +01006020 band = IEEE80211_BAND_2GHZ;
Zhu Yib481de92007-09-25 17:54:57 -07006021 break;
6022
6023 case 1:
6024 scan->tx_cmd.rate_n_flags =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006025 iwl4965_hw_set_rate_n_flags(IWL_RATE_6M_PLCP,
Zhu Yib481de92007-09-25 17:54:57 -07006026 RATE_MCS_ANT_B_MSK);
6027 scan->good_CRC_th = IWL_GOOD_CRC_TH;
Johannes Berg8318d782008-01-24 19:38:38 +01006028 band = IEEE80211_BAND_5GHZ;
Zhu Yib481de92007-09-25 17:54:57 -07006029 break;
6030
6031 default:
6032 IWL_WARNING("Invalid scan band count\n");
6033 goto done;
6034 }
6035
Tomas Winkler78330fd2008-02-06 02:37:18 +02006036 /* We don't build a direct scan probe request; the uCode will do
6037 * that based on the direct_mask added to each channel entry */
6038 cmd_len = iwl4965_fill_probe_req(priv, band,
6039 (struct ieee80211_mgmt *)scan->data,
6040 IWL_MAX_SCAN_SIZE - sizeof(*scan), 0);
6041
6042 scan->tx_cmd.len = cpu_to_le16(cmd_len);
Zhu Yib481de92007-09-25 17:54:57 -07006043 /* select Rx chains */
6044
6045 /* Force use of chains B and C (0x6) for scan Rx.
6046 * Avoid A (0x1) because of its off-channel reception on A-band.
6047 * MIMO is not used here, but value is required to make uCode happy. */
6048 scan->rx_chain = RXON_RX_CHAIN_DRIVER_FORCE_MSK |
6049 cpu_to_le16((0x7 << RXON_RX_CHAIN_VALID_POS) |
6050 (0x6 << RXON_RX_CHAIN_FORCE_SEL_POS) |
6051 (0x7 << RXON_RX_CHAIN_FORCE_MIMO_SEL_POS));
6052
6053 if (priv->iw_mode == IEEE80211_IF_TYPE_MNTR)
6054 scan->filter_flags = RXON_FILTER_PROMISC_MSK;
6055
Reinette Chatre26c0f032008-03-11 16:17:15 -07006056 if (direct_mask) {
Zhu Yib481de92007-09-25 17:54:57 -07006057 IWL_DEBUG_SCAN
6058 ("Initiating direct scan for %s.\n",
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006059 iwl4965_escape_essid(priv->essid, priv->essid_len));
Reinette Chatre26c0f032008-03-11 16:17:15 -07006060 scan->channel_count =
6061 iwl4965_get_channels_for_scan(
6062 priv, band, 1, /* active */
6063 direct_mask,
6064 (void *)&scan->data[le16_to_cpu(scan->tx_cmd.len)]);
6065 } else {
Zhu Yib481de92007-09-25 17:54:57 -07006066 IWL_DEBUG_SCAN("Initiating indirect scan.\n");
Reinette Chatre26c0f032008-03-11 16:17:15 -07006067 scan->channel_count =
6068 iwl4965_get_channels_for_scan(
6069 priv, band, 0, /* passive */
6070 direct_mask,
6071 (void *)&scan->data[le16_to_cpu(scan->tx_cmd.len)]);
6072 }
Zhu Yib481de92007-09-25 17:54:57 -07006073
6074 cmd.len += le16_to_cpu(scan->tx_cmd.len) +
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006075 scan->channel_count * sizeof(struct iwl4965_scan_channel);
Zhu Yib481de92007-09-25 17:54:57 -07006076 cmd.data = scan;
6077 scan->len = cpu_to_le16(cmd.len);
6078
6079 set_bit(STATUS_SCAN_HW, &priv->status);
Tomas Winkler857485c2008-03-21 13:53:44 -07006080 ret = iwl_send_cmd_sync(priv, &cmd);
6081 if (ret)
Zhu Yib481de92007-09-25 17:54:57 -07006082 goto done;
6083
6084 queue_delayed_work(priv->workqueue, &priv->scan_check,
6085 IWL_SCAN_CHECK_WATCHDOG);
6086
6087 mutex_unlock(&priv->mutex);
6088 return;
6089
6090 done:
Ian Schram01ebd062007-10-25 17:15:22 +08006091 /* inform mac80211 scan aborted */
Zhu Yib481de92007-09-25 17:54:57 -07006092 queue_work(priv->workqueue, &priv->scan_completed);
6093 mutex_unlock(&priv->mutex);
6094}
6095
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006096static void iwl4965_bg_up(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07006097{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006098 struct iwl_priv *priv = container_of(data, struct iwl_priv, up);
Zhu Yib481de92007-09-25 17:54:57 -07006099
6100 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
6101 return;
6102
6103 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006104 __iwl4965_up(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006105 mutex_unlock(&priv->mutex);
6106}
6107
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006108static void iwl4965_bg_restart(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07006109{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006110 struct iwl_priv *priv = container_of(data, struct iwl_priv, restart);
Zhu Yib481de92007-09-25 17:54:57 -07006111
6112 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
6113 return;
6114
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006115 iwl4965_down(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006116 queue_work(priv->workqueue, &priv->up);
6117}
6118
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006119static void iwl4965_bg_rx_replenish(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07006120{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006121 struct iwl_priv *priv =
6122 container_of(data, struct iwl_priv, rx_replenish);
Zhu Yib481de92007-09-25 17:54:57 -07006123
6124 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
6125 return;
6126
6127 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006128 iwl4965_rx_replenish(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006129 mutex_unlock(&priv->mutex);
6130}
6131
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08006132#define IWL_DELAY_NEXT_SCAN (HZ*2)
6133
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006134static void iwl4965_bg_post_associate(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07006135{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006136 struct iwl_priv *priv = container_of(data, struct iwl_priv,
Zhu Yib481de92007-09-25 17:54:57 -07006137 post_associate.work);
Zhu Yib481de92007-09-25 17:54:57 -07006138 struct ieee80211_conf *conf = NULL;
Tomas Winkler857485c2008-03-21 13:53:44 -07006139 int ret = 0;
Joe Perches0795af52007-10-03 17:59:30 -07006140 DECLARE_MAC_BUF(mac);
Zhu Yib481de92007-09-25 17:54:57 -07006141
6142 if (priv->iw_mode == IEEE80211_IF_TYPE_AP) {
6143 IWL_ERROR("%s Should not be called in AP mode\n", __FUNCTION__);
6144 return;
6145 }
6146
Joe Perches0795af52007-10-03 17:59:30 -07006147 IWL_DEBUG_ASSOC("Associated as %d to: %s\n",
6148 priv->assoc_id,
6149 print_mac(mac, priv->active_rxon.bssid_addr));
Zhu Yib481de92007-09-25 17:54:57 -07006150
6151
6152 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
6153 return;
6154
6155 mutex_lock(&priv->mutex);
6156
Johannes Berg32bfd352007-12-19 01:31:26 +01006157 if (!priv->vif || !priv->is_open) {
Mohamed Abbas948c1712007-10-25 17:15:45 +08006158 mutex_unlock(&priv->mutex);
6159 return;
6160 }
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006161 iwl4965_scan_cancel_timeout(priv, 200);
mabbas052c4b92007-10-25 17:15:43 +08006162
Zhu Yib481de92007-09-25 17:54:57 -07006163 conf = ieee80211_get_hw_conf(priv->hw);
6164
6165 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006166 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006167
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006168 memset(&priv->rxon_timing, 0, sizeof(struct iwl4965_rxon_time_cmd));
6169 iwl4965_setup_rxon_timing(priv);
Tomas Winkler857485c2008-03-21 13:53:44 -07006170 ret = iwl_send_cmd_pdu(priv, REPLY_RXON_TIMING,
Zhu Yib481de92007-09-25 17:54:57 -07006171 sizeof(priv->rxon_timing), &priv->rxon_timing);
Tomas Winkler857485c2008-03-21 13:53:44 -07006172 if (ret)
Zhu Yib481de92007-09-25 17:54:57 -07006173 IWL_WARNING("REPLY_RXON_TIMING failed - "
6174 "Attempting to continue.\n");
6175
6176 priv->staging_rxon.filter_flags |= RXON_FILTER_ASSOC_MSK;
6177
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08006178#ifdef CONFIG_IWL4965_HT
Ron Rindjunskyfd105e72007-11-26 16:14:39 +02006179 if (priv->current_ht_config.is_ht)
6180 iwl4965_set_rxon_ht(priv, &priv->current_ht_config);
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08006181#endif /* CONFIG_IWL4965_HT*/
Zhu Yib481de92007-09-25 17:54:57 -07006182 iwl4965_set_rxon_chain(priv);
6183 priv->staging_rxon.assoc_id = cpu_to_le16(priv->assoc_id);
6184
6185 IWL_DEBUG_ASSOC("assoc id %d beacon interval %d\n",
6186 priv->assoc_id, priv->beacon_int);
6187
6188 if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_PREAMBLE)
6189 priv->staging_rxon.flags |= RXON_FLG_SHORT_PREAMBLE_MSK;
6190 else
6191 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_PREAMBLE_MSK;
6192
6193 if (priv->staging_rxon.flags & RXON_FLG_BAND_24G_MSK) {
6194 if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_SLOT_TIME)
6195 priv->staging_rxon.flags |= RXON_FLG_SHORT_SLOT_MSK;
6196 else
6197 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
6198
6199 if (priv->iw_mode == IEEE80211_IF_TYPE_IBSS)
6200 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
6201
6202 }
6203
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006204 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006205
6206 switch (priv->iw_mode) {
6207 case IEEE80211_IF_TYPE_STA:
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006208 iwl4965_rate_scale_init(priv->hw, IWL_AP_ID);
Zhu Yib481de92007-09-25 17:54:57 -07006209 break;
6210
6211 case IEEE80211_IF_TYPE_IBSS:
6212
6213 /* clear out the station table */
Assaf Kraussbf85ea42008-03-14 10:38:49 -07006214 iwlcore_clear_stations_table(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006215
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006216 iwl4965_rxon_add_station(priv, iwl4965_broadcast_addr, 0);
6217 iwl4965_rxon_add_station(priv, priv->bssid, 0);
6218 iwl4965_rate_scale_init(priv->hw, IWL_STA_ID);
6219 iwl4965_send_beacon_cmd(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006220
6221 break;
6222
6223 default:
6224 IWL_ERROR("%s Should not be called in %d mode\n",
6225 __FUNCTION__, priv->iw_mode);
6226 break;
6227 }
6228
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006229 iwl4965_sequence_reset(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006230
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08006231#ifdef CONFIG_IWL4965_SENSITIVITY
Zhu Yib481de92007-09-25 17:54:57 -07006232 /* Enable Rx differential gain and sensitivity calibrations */
6233 iwl4965_chain_noise_reset(priv);
6234 priv->start_calib = 1;
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08006235#endif /* CONFIG_IWL4965_SENSITIVITY */
Zhu Yib481de92007-09-25 17:54:57 -07006236
6237 if (priv->iw_mode == IEEE80211_IF_TYPE_IBSS)
6238 priv->assoc_station_added = 1;
6239
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006240 iwl4965_activate_qos(priv, 0);
Ron Rindjunsky292ae172008-02-06 11:20:39 -08006241
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08006242 /* we have just associated, don't start scan too early */
6243 priv->next_scan_jiffies = jiffies + IWL_DELAY_NEXT_SCAN;
Zhu Yib481de92007-09-25 17:54:57 -07006244 mutex_unlock(&priv->mutex);
6245}
6246
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006247static void iwl4965_bg_abort_scan(struct work_struct *work)
Zhu Yib481de92007-09-25 17:54:57 -07006248{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006249 struct iwl_priv *priv = container_of(work, struct iwl_priv, abort_scan);
Zhu Yib481de92007-09-25 17:54:57 -07006250
Tomas Winklerfee12472008-04-03 16:05:21 -07006251 if (!iwl_is_ready(priv))
Zhu Yib481de92007-09-25 17:54:57 -07006252 return;
6253
6254 mutex_lock(&priv->mutex);
6255
6256 set_bit(STATUS_SCAN_ABORTING, &priv->status);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006257 iwl4965_send_scan_abort(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006258
6259 mutex_unlock(&priv->mutex);
6260}
6261
Zhu Yi76bb77e2007-11-22 10:53:22 +08006262static int iwl4965_mac_config(struct ieee80211_hw *hw, struct ieee80211_conf *conf);
6263
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006264static void iwl4965_bg_scan_completed(struct work_struct *work)
Zhu Yib481de92007-09-25 17:54:57 -07006265{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006266 struct iwl_priv *priv =
6267 container_of(work, struct iwl_priv, scan_completed);
Zhu Yib481de92007-09-25 17:54:57 -07006268
6269 IWL_DEBUG(IWL_DL_INFO | IWL_DL_SCAN, "SCAN complete scan\n");
6270
6271 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
6272 return;
6273
Zhu Yia0646472007-12-20 14:10:01 +08006274 if (test_bit(STATUS_CONF_PENDING, &priv->status))
6275 iwl4965_mac_config(priv->hw, ieee80211_get_hw_conf(priv->hw));
Zhu Yi76bb77e2007-11-22 10:53:22 +08006276
Zhu Yib481de92007-09-25 17:54:57 -07006277 ieee80211_scan_completed(priv->hw);
6278
6279 /* Since setting the TXPOWER may have been deferred while
6280 * performing the scan, fire one off */
6281 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006282 iwl4965_hw_reg_send_txpower(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006283 mutex_unlock(&priv->mutex);
6284}
6285
6286/*****************************************************************************
6287 *
6288 * mac80211 entry point functions
6289 *
6290 *****************************************************************************/
6291
Zhu Yi5a66926a2008-01-14 17:46:18 -08006292#define UCODE_READY_TIMEOUT (2 * HZ)
6293
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006294static int iwl4965_mac_start(struct ieee80211_hw *hw)
Zhu Yib481de92007-09-25 17:54:57 -07006295{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006296 struct iwl_priv *priv = hw->priv;
Zhu Yi5a66926a2008-01-14 17:46:18 -08006297 int ret;
Zhu Yib481de92007-09-25 17:54:57 -07006298
6299 IWL_DEBUG_MAC80211("enter\n");
6300
Zhu Yi5a66926a2008-01-14 17:46:18 -08006301 if (pci_enable_device(priv->pci_dev)) {
6302 IWL_ERROR("Fail to pci_enable_device\n");
6303 return -ENODEV;
6304 }
6305 pci_restore_state(priv->pci_dev);
6306 pci_enable_msi(priv->pci_dev);
6307
6308 ret = request_irq(priv->pci_dev->irq, iwl4965_isr, IRQF_SHARED,
6309 DRV_NAME, priv);
6310 if (ret) {
6311 IWL_ERROR("Error allocating IRQ %d\n", priv->pci_dev->irq);
6312 goto out_disable_msi;
6313 }
6314
Zhu Yib481de92007-09-25 17:54:57 -07006315 /* we should be verifying the device is ready to be opened */
6316 mutex_lock(&priv->mutex);
6317
Zhu Yi5a66926a2008-01-14 17:46:18 -08006318 memset(&priv->staging_rxon, 0, sizeof(struct iwl4965_rxon_cmd));
6319 /* fetch ucode file from disk, alloc and copy to bus-master buffers ...
6320 * ucode filename and max sizes are card-specific. */
6321
6322 if (!priv->ucode_code.len) {
6323 ret = iwl4965_read_ucode(priv);
6324 if (ret) {
6325 IWL_ERROR("Could not read microcode: %d\n", ret);
6326 mutex_unlock(&priv->mutex);
6327 goto out_release_irq;
6328 }
6329 }
6330
Zhu Yie655b9f2008-01-24 02:19:38 -08006331 ret = __iwl4965_up(priv);
Zhu Yi5a66926a2008-01-14 17:46:18 -08006332
Zhu Yib481de92007-09-25 17:54:57 -07006333 mutex_unlock(&priv->mutex);
Zhu Yi5a66926a2008-01-14 17:46:18 -08006334
Zhu Yie655b9f2008-01-24 02:19:38 -08006335 if (ret)
6336 goto out_release_irq;
6337
6338 IWL_DEBUG_INFO("Start UP work done.\n");
6339
6340 if (test_bit(STATUS_IN_SUSPEND, &priv->status))
6341 return 0;
6342
Zhu Yi5a66926a2008-01-14 17:46:18 -08006343 /* Wait for START_ALIVE from ucode. Otherwise callbacks from
6344 * mac80211 will not be run successfully. */
6345 ret = wait_event_interruptible_timeout(priv->wait_command_queue,
6346 test_bit(STATUS_READY, &priv->status),
6347 UCODE_READY_TIMEOUT);
6348 if (!ret) {
6349 if (!test_bit(STATUS_READY, &priv->status)) {
6350 IWL_ERROR("Wait for START_ALIVE timeout after %dms.\n",
6351 jiffies_to_msecs(UCODE_READY_TIMEOUT));
6352 ret = -ETIMEDOUT;
6353 goto out_release_irq;
6354 }
6355 }
6356
Zhu Yie655b9f2008-01-24 02:19:38 -08006357 priv->is_open = 1;
Zhu Yib481de92007-09-25 17:54:57 -07006358 IWL_DEBUG_MAC80211("leave\n");
6359 return 0;
Zhu Yi5a66926a2008-01-14 17:46:18 -08006360
6361out_release_irq:
6362 free_irq(priv->pci_dev->irq, priv);
6363out_disable_msi:
6364 pci_disable_msi(priv->pci_dev);
Zhu Yie655b9f2008-01-24 02:19:38 -08006365 pci_disable_device(priv->pci_dev);
6366 priv->is_open = 0;
6367 IWL_DEBUG_MAC80211("leave - failed\n");
Zhu Yi5a66926a2008-01-14 17:46:18 -08006368 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07006369}
6370
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006371static void iwl4965_mac_stop(struct ieee80211_hw *hw)
Zhu Yib481de92007-09-25 17:54:57 -07006372{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006373 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07006374
6375 IWL_DEBUG_MAC80211("enter\n");
Mohamed Abbas948c1712007-10-25 17:15:45 +08006376
Zhu Yie655b9f2008-01-24 02:19:38 -08006377 if (!priv->is_open) {
6378 IWL_DEBUG_MAC80211("leave - skip\n");
6379 return;
6380 }
6381
Zhu Yib481de92007-09-25 17:54:57 -07006382 priv->is_open = 0;
Zhu Yi5a66926a2008-01-14 17:46:18 -08006383
Tomas Winklerfee12472008-04-03 16:05:21 -07006384 if (iwl_is_ready_rf(priv)) {
Zhu Yie655b9f2008-01-24 02:19:38 -08006385 /* stop mac, cancel any scan request and clear
6386 * RXON_FILTER_ASSOC_MSK BIT
6387 */
Zhu Yi5a66926a2008-01-14 17:46:18 -08006388 mutex_lock(&priv->mutex);
6389 iwl4965_scan_cancel_timeout(priv, 100);
6390 cancel_delayed_work(&priv->post_associate);
Mohamed Abbasfde35712007-11-29 11:10:15 +08006391 mutex_unlock(&priv->mutex);
Mohamed Abbasfde35712007-11-29 11:10:15 +08006392 }
6393
Zhu Yi5a66926a2008-01-14 17:46:18 -08006394 iwl4965_down(priv);
6395
6396 flush_workqueue(priv->workqueue);
6397 free_irq(priv->pci_dev->irq, priv);
6398 pci_disable_msi(priv->pci_dev);
6399 pci_save_state(priv->pci_dev);
6400 pci_disable_device(priv->pci_dev);
Mohamed Abbas948c1712007-10-25 17:15:45 +08006401
Zhu Yib481de92007-09-25 17:54:57 -07006402 IWL_DEBUG_MAC80211("leave\n");
Zhu Yib481de92007-09-25 17:54:57 -07006403}
6404
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006405static int iwl4965_mac_tx(struct ieee80211_hw *hw, struct sk_buff *skb,
Zhu Yib481de92007-09-25 17:54:57 -07006406 struct ieee80211_tx_control *ctl)
6407{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006408 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07006409
6410 IWL_DEBUG_MAC80211("enter\n");
6411
6412 if (priv->iw_mode == IEEE80211_IF_TYPE_MNTR) {
6413 IWL_DEBUG_MAC80211("leave - monitor\n");
6414 return -1;
6415 }
6416
6417 IWL_DEBUG_TX("dev->xmit(%d bytes) at rate 0x%02x\n", skb->len,
Johannes Berg8318d782008-01-24 19:38:38 +01006418 ctl->tx_rate->bitrate);
Zhu Yib481de92007-09-25 17:54:57 -07006419
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006420 if (iwl4965_tx_skb(priv, skb, ctl))
Zhu Yib481de92007-09-25 17:54:57 -07006421 dev_kfree_skb_any(skb);
6422
6423 IWL_DEBUG_MAC80211("leave\n");
6424 return 0;
6425}
6426
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006427static int iwl4965_mac_add_interface(struct ieee80211_hw *hw,
Zhu Yib481de92007-09-25 17:54:57 -07006428 struct ieee80211_if_init_conf *conf)
6429{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006430 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07006431 unsigned long flags;
Joe Perches0795af52007-10-03 17:59:30 -07006432 DECLARE_MAC_BUF(mac);
Zhu Yib481de92007-09-25 17:54:57 -07006433
Johannes Berg32bfd352007-12-19 01:31:26 +01006434 IWL_DEBUG_MAC80211("enter: type %d\n", conf->type);
Zhu Yib481de92007-09-25 17:54:57 -07006435
Johannes Berg32bfd352007-12-19 01:31:26 +01006436 if (priv->vif) {
6437 IWL_DEBUG_MAC80211("leave - vif != NULL\n");
Reinette Chatre75849d22008-01-23 10:15:17 -08006438 return -EOPNOTSUPP;
Zhu Yib481de92007-09-25 17:54:57 -07006439 }
6440
6441 spin_lock_irqsave(&priv->lock, flags);
Johannes Berg32bfd352007-12-19 01:31:26 +01006442 priv->vif = conf->vif;
Zhu Yib481de92007-09-25 17:54:57 -07006443
6444 spin_unlock_irqrestore(&priv->lock, flags);
6445
6446 mutex_lock(&priv->mutex);
Tomas Winkler864792e2007-11-27 21:00:52 +02006447
6448 if (conf->mac_addr) {
6449 IWL_DEBUG_MAC80211("Set %s\n", print_mac(mac, conf->mac_addr));
6450 memcpy(priv->mac_addr, conf->mac_addr, ETH_ALEN);
6451 }
Zhu Yib481de92007-09-25 17:54:57 -07006452
Tomas Winklerfee12472008-04-03 16:05:21 -07006453 if (iwl_is_ready(priv))
Zhu Yi5a66926a2008-01-14 17:46:18 -08006454 iwl4965_set_mode(priv, conf->type);
6455
Zhu Yib481de92007-09-25 17:54:57 -07006456 mutex_unlock(&priv->mutex);
6457
Zhu Yi5a66926a2008-01-14 17:46:18 -08006458 IWL_DEBUG_MAC80211("leave\n");
Zhu Yib481de92007-09-25 17:54:57 -07006459 return 0;
6460}
6461
6462/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006463 * iwl4965_mac_config - mac80211 config callback
Zhu Yib481de92007-09-25 17:54:57 -07006464 *
6465 * We ignore conf->flags & IEEE80211_CONF_SHORT_SLOT_TIME since it seems to
6466 * be set inappropriately and the driver currently sets the hardware up to
6467 * use it whenever needed.
6468 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006469static int iwl4965_mac_config(struct ieee80211_hw *hw, struct ieee80211_conf *conf)
Zhu Yib481de92007-09-25 17:54:57 -07006470{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006471 struct iwl_priv *priv = hw->priv;
Assaf Kraussbf85ea42008-03-14 10:38:49 -07006472 const struct iwl_channel_info *ch_info;
Zhu Yib481de92007-09-25 17:54:57 -07006473 unsigned long flags;
Zhu Yi76bb77e2007-11-22 10:53:22 +08006474 int ret = 0;
Zhu Yib481de92007-09-25 17:54:57 -07006475
6476 mutex_lock(&priv->mutex);
Johannes Berg8318d782008-01-24 19:38:38 +01006477 IWL_DEBUG_MAC80211("enter to channel %d\n", conf->channel->hw_value);
Zhu Yib481de92007-09-25 17:54:57 -07006478
Zhu Yi12342c42007-12-20 11:27:32 +08006479 priv->add_radiotap = !!(conf->flags & IEEE80211_CONF_RADIOTAP);
6480
Tomas Winklerfee12472008-04-03 16:05:21 -07006481 if (!iwl_is_ready(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07006482 IWL_DEBUG_MAC80211("leave - not ready\n");
Zhu Yi76bb77e2007-11-22 10:53:22 +08006483 ret = -EIO;
6484 goto out;
Zhu Yib481de92007-09-25 17:54:57 -07006485 }
6486
Assaf Krauss1ea87392008-03-18 14:57:50 -07006487 if (unlikely(!priv->cfg->mod_params->disable_hw_scan &&
Zhu Yib481de92007-09-25 17:54:57 -07006488 test_bit(STATUS_SCANNING, &priv->status))) {
Zhu Yia0646472007-12-20 14:10:01 +08006489 IWL_DEBUG_MAC80211("leave - scanning\n");
6490 set_bit(STATUS_CONF_PENDING, &priv->status);
Zhu Yib481de92007-09-25 17:54:57 -07006491 mutex_unlock(&priv->mutex);
Zhu Yia0646472007-12-20 14:10:01 +08006492 return 0;
Zhu Yib481de92007-09-25 17:54:57 -07006493 }
6494
6495 spin_lock_irqsave(&priv->lock, flags);
6496
Assaf Krauss8622e702008-03-21 13:53:43 -07006497 ch_info = iwl_get_channel_info(priv, conf->channel->band,
Johannes Berg8318d782008-01-24 19:38:38 +01006498 ieee80211_frequency_to_channel(conf->channel->center_freq));
Zhu Yib481de92007-09-25 17:54:57 -07006499 if (!is_channel_valid(ch_info)) {
Zhu Yib481de92007-09-25 17:54:57 -07006500 IWL_DEBUG_MAC80211("leave - invalid channel\n");
6501 spin_unlock_irqrestore(&priv->lock, flags);
Zhu Yi76bb77e2007-11-22 10:53:22 +08006502 ret = -EINVAL;
6503 goto out;
Zhu Yib481de92007-09-25 17:54:57 -07006504 }
6505
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08006506#ifdef CONFIG_IWL4965_HT
Tomas Winkler78330fd2008-02-06 02:37:18 +02006507 /* if we are switching from ht to 2.4 clear flags
Zhu Yib481de92007-09-25 17:54:57 -07006508 * from any ht related info since 2.4 does not
6509 * support ht */
Tomas Winkler78330fd2008-02-06 02:37:18 +02006510 if ((le16_to_cpu(priv->staging_rxon.channel) != conf->channel->hw_value)
Zhu Yib481de92007-09-25 17:54:57 -07006511#ifdef IEEE80211_CONF_CHANNEL_SWITCH
6512 && !(conf->flags & IEEE80211_CONF_CHANNEL_SWITCH)
6513#endif
6514 )
6515 priv->staging_rxon.flags = 0;
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08006516#endif /* CONFIG_IWL4965_HT */
Zhu Yib481de92007-09-25 17:54:57 -07006517
Assaf Kraussbf85ea42008-03-14 10:38:49 -07006518 iwlcore_set_rxon_channel(priv, conf->channel->band,
Johannes Berg8318d782008-01-24 19:38:38 +01006519 ieee80211_frequency_to_channel(conf->channel->center_freq));
Zhu Yib481de92007-09-25 17:54:57 -07006520
Johannes Berg8318d782008-01-24 19:38:38 +01006521 iwl4965_set_flags_for_phymode(priv, conf->channel->band);
Zhu Yib481de92007-09-25 17:54:57 -07006522
6523 /* The list of supported rates and rate mask can be different
Johannes Berg8318d782008-01-24 19:38:38 +01006524 * for each band; since the band may have changed, reset
Zhu Yib481de92007-09-25 17:54:57 -07006525 * the rate mask to what mac80211 lists */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006526 iwl4965_set_rate(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006527
6528 spin_unlock_irqrestore(&priv->lock, flags);
6529
6530#ifdef IEEE80211_CONF_CHANNEL_SWITCH
6531 if (conf->flags & IEEE80211_CONF_CHANNEL_SWITCH) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006532 iwl4965_hw_channel_switch(priv, conf->channel);
Zhu Yi76bb77e2007-11-22 10:53:22 +08006533 goto out;
Zhu Yib481de92007-09-25 17:54:57 -07006534 }
6535#endif
6536
Mohamed Abbasad97edd2008-03-28 16:21:06 -07006537 if (priv->cfg->ops->lib->radio_kill_sw)
6538 priv->cfg->ops->lib->radio_kill_sw(priv, !conf->radio_enabled);
Zhu Yib481de92007-09-25 17:54:57 -07006539
6540 if (!conf->radio_enabled) {
6541 IWL_DEBUG_MAC80211("leave - radio disabled\n");
Zhu Yi76bb77e2007-11-22 10:53:22 +08006542 goto out;
Zhu Yib481de92007-09-25 17:54:57 -07006543 }
6544
Tomas Winklerfee12472008-04-03 16:05:21 -07006545 if (iwl_is_rfkill(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07006546 IWL_DEBUG_MAC80211("leave - RF kill\n");
Zhu Yi76bb77e2007-11-22 10:53:22 +08006547 ret = -EIO;
6548 goto out;
Zhu Yib481de92007-09-25 17:54:57 -07006549 }
6550
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006551 iwl4965_set_rate(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006552
6553 if (memcmp(&priv->active_rxon,
6554 &priv->staging_rxon, sizeof(priv->staging_rxon)))
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006555 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006556 else
6557 IWL_DEBUG_INFO("No re-sending same RXON configuration.\n");
6558
6559 IWL_DEBUG_MAC80211("leave\n");
6560
Zhu Yia0646472007-12-20 14:10:01 +08006561out:
6562 clear_bit(STATUS_CONF_PENDING, &priv->status);
Zhu Yi5a66926a2008-01-14 17:46:18 -08006563 mutex_unlock(&priv->mutex);
Zhu Yi76bb77e2007-11-22 10:53:22 +08006564 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07006565}
6566
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006567static void iwl4965_config_ap(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07006568{
Tomas Winkler857485c2008-03-21 13:53:44 -07006569 int ret = 0;
Zhu Yib481de92007-09-25 17:54:57 -07006570
Maarten Lankhorstd986bcd2008-01-23 10:15:16 -08006571 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
Zhu Yib481de92007-09-25 17:54:57 -07006572 return;
6573
6574 /* The following should be done only at AP bring up */
6575 if ((priv->active_rxon.filter_flags & RXON_FILTER_ASSOC_MSK) == 0) {
6576
6577 /* RXON - unassoc (to set timing command) */
6578 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006579 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006580
6581 /* RXON Timing */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006582 memset(&priv->rxon_timing, 0, sizeof(struct iwl4965_rxon_time_cmd));
6583 iwl4965_setup_rxon_timing(priv);
Tomas Winkler857485c2008-03-21 13:53:44 -07006584 ret = iwl_send_cmd_pdu(priv, REPLY_RXON_TIMING,
Zhu Yib481de92007-09-25 17:54:57 -07006585 sizeof(priv->rxon_timing), &priv->rxon_timing);
Tomas Winkler857485c2008-03-21 13:53:44 -07006586 if (ret)
Zhu Yib481de92007-09-25 17:54:57 -07006587 IWL_WARNING("REPLY_RXON_TIMING failed - "
6588 "Attempting to continue.\n");
6589
6590 iwl4965_set_rxon_chain(priv);
6591
6592 /* FIXME: what should be the assoc_id for AP? */
6593 priv->staging_rxon.assoc_id = cpu_to_le16(priv->assoc_id);
6594 if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_PREAMBLE)
6595 priv->staging_rxon.flags |=
6596 RXON_FLG_SHORT_PREAMBLE_MSK;
6597 else
6598 priv->staging_rxon.flags &=
6599 ~RXON_FLG_SHORT_PREAMBLE_MSK;
6600
6601 if (priv->staging_rxon.flags & RXON_FLG_BAND_24G_MSK) {
6602 if (priv->assoc_capability &
6603 WLAN_CAPABILITY_SHORT_SLOT_TIME)
6604 priv->staging_rxon.flags |=
6605 RXON_FLG_SHORT_SLOT_MSK;
6606 else
6607 priv->staging_rxon.flags &=
6608 ~RXON_FLG_SHORT_SLOT_MSK;
6609
6610 if (priv->iw_mode == IEEE80211_IF_TYPE_IBSS)
6611 priv->staging_rxon.flags &=
6612 ~RXON_FLG_SHORT_SLOT_MSK;
6613 }
6614 /* restore RXON assoc */
6615 priv->staging_rxon.filter_flags |= RXON_FILTER_ASSOC_MSK;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006616 iwl4965_commit_rxon(priv);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006617 iwl4965_activate_qos(priv, 1);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006618 iwl4965_rxon_add_station(priv, iwl4965_broadcast_addr, 0);
Zhu Yie1493de2007-09-27 11:27:32 +08006619 }
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006620 iwl4965_send_beacon_cmd(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006621
6622 /* FIXME - we need to add code here to detect a totally new
6623 * configuration, reset the AP, unassoc, rxon timing, assoc,
6624 * clear sta table, add BCAST sta... */
6625}
6626
Johannes Berg32bfd352007-12-19 01:31:26 +01006627static int iwl4965_mac_config_interface(struct ieee80211_hw *hw,
6628 struct ieee80211_vif *vif,
Zhu Yib481de92007-09-25 17:54:57 -07006629 struct ieee80211_if_conf *conf)
6630{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006631 struct iwl_priv *priv = hw->priv;
Joe Perches0795af52007-10-03 17:59:30 -07006632 DECLARE_MAC_BUF(mac);
Zhu Yib481de92007-09-25 17:54:57 -07006633 unsigned long flags;
6634 int rc;
6635
6636 if (conf == NULL)
6637 return -EIO;
6638
Emmanuel Grumbachb716bb92008-03-04 18:09:32 -08006639 if (priv->vif != vif) {
6640 IWL_DEBUG_MAC80211("leave - priv->vif != vif\n");
6641 mutex_unlock(&priv->mutex);
6642 return 0;
6643 }
6644
Zhu Yib481de92007-09-25 17:54:57 -07006645 if ((priv->iw_mode == IEEE80211_IF_TYPE_AP) &&
6646 (!conf->beacon || !conf->ssid_len)) {
6647 IWL_DEBUG_MAC80211
6648 ("Leaving in AP mode because HostAPD is not ready.\n");
6649 return 0;
6650 }
6651
Tomas Winklerfee12472008-04-03 16:05:21 -07006652 if (!iwl_is_alive(priv))
Zhu Yi5a66926a2008-01-14 17:46:18 -08006653 return -EAGAIN;
6654
Zhu Yib481de92007-09-25 17:54:57 -07006655 mutex_lock(&priv->mutex);
6656
Zhu Yib481de92007-09-25 17:54:57 -07006657 if (conf->bssid)
Joe Perches0795af52007-10-03 17:59:30 -07006658 IWL_DEBUG_MAC80211("bssid: %s\n",
6659 print_mac(mac, conf->bssid));
Zhu Yib481de92007-09-25 17:54:57 -07006660
Johannes Berg4150c572007-09-17 01:29:23 -04006661/*
6662 * very dubious code was here; the probe filtering flag is never set:
6663 *
Zhu Yib481de92007-09-25 17:54:57 -07006664 if (unlikely(test_bit(STATUS_SCANNING, &priv->status)) &&
6665 !(priv->hw->flags & IEEE80211_HW_NO_PROBE_FILTERING)) {
Johannes Berg4150c572007-09-17 01:29:23 -04006666 */
Zhu Yib481de92007-09-25 17:54:57 -07006667
6668 if (priv->iw_mode == IEEE80211_IF_TYPE_AP) {
6669 if (!conf->bssid) {
6670 conf->bssid = priv->mac_addr;
6671 memcpy(priv->bssid, priv->mac_addr, ETH_ALEN);
Joe Perches0795af52007-10-03 17:59:30 -07006672 IWL_DEBUG_MAC80211("bssid was set to: %s\n",
6673 print_mac(mac, conf->bssid));
Zhu Yib481de92007-09-25 17:54:57 -07006674 }
6675 if (priv->ibss_beacon)
6676 dev_kfree_skb(priv->ibss_beacon);
6677
6678 priv->ibss_beacon = conf->beacon;
6679 }
6680
Tomas Winklerfee12472008-04-03 16:05:21 -07006681 if (iwl_is_rfkill(priv))
Mohamed Abbasfde35712007-11-29 11:10:15 +08006682 goto done;
6683
Zhu Yib481de92007-09-25 17:54:57 -07006684 if (conf->bssid && !is_zero_ether_addr(conf->bssid) &&
6685 !is_multicast_ether_addr(conf->bssid)) {
6686 /* If there is currently a HW scan going on in the background
6687 * then we need to cancel it else the RXON below will fail. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006688 if (iwl4965_scan_cancel_timeout(priv, 100)) {
Zhu Yib481de92007-09-25 17:54:57 -07006689 IWL_WARNING("Aborted scan still in progress "
6690 "after 100ms\n");
6691 IWL_DEBUG_MAC80211("leaving - scan abort failed.\n");
6692 mutex_unlock(&priv->mutex);
6693 return -EAGAIN;
6694 }
6695 memcpy(priv->staging_rxon.bssid_addr, conf->bssid, ETH_ALEN);
6696
6697 /* TODO: Audit driver for usage of these members and see
6698 * if mac80211 deprecates them (priv->bssid looks like it
6699 * shouldn't be there, but I haven't scanned the IBSS code
6700 * to verify) - jpk */
6701 memcpy(priv->bssid, conf->bssid, ETH_ALEN);
6702
6703 if (priv->iw_mode == IEEE80211_IF_TYPE_AP)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006704 iwl4965_config_ap(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006705 else {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006706 rc = iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006707 if ((priv->iw_mode == IEEE80211_IF_TYPE_STA) && rc)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006708 iwl4965_rxon_add_station(
Zhu Yib481de92007-09-25 17:54:57 -07006709 priv, priv->active_rxon.bssid_addr, 1);
6710 }
6711
6712 } else {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006713 iwl4965_scan_cancel_timeout(priv, 100);
Zhu Yib481de92007-09-25 17:54:57 -07006714 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006715 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006716 }
6717
Mohamed Abbasfde35712007-11-29 11:10:15 +08006718 done:
Zhu Yib481de92007-09-25 17:54:57 -07006719 spin_lock_irqsave(&priv->lock, flags);
6720 if (!conf->ssid_len)
6721 memset(priv->essid, 0, IW_ESSID_MAX_SIZE);
6722 else
6723 memcpy(priv->essid, conf->ssid, conf->ssid_len);
6724
6725 priv->essid_len = conf->ssid_len;
6726 spin_unlock_irqrestore(&priv->lock, flags);
6727
6728 IWL_DEBUG_MAC80211("leave\n");
6729 mutex_unlock(&priv->mutex);
6730
6731 return 0;
6732}
6733
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006734static void iwl4965_configure_filter(struct ieee80211_hw *hw,
Johannes Berg4150c572007-09-17 01:29:23 -04006735 unsigned int changed_flags,
6736 unsigned int *total_flags,
6737 int mc_count, struct dev_addr_list *mc_list)
6738{
6739 /*
6740 * XXX: dummy
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006741 * see also iwl4965_connection_init_rx_config
Johannes Berg4150c572007-09-17 01:29:23 -04006742 */
6743 *total_flags = 0;
6744}
6745
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006746static void iwl4965_mac_remove_interface(struct ieee80211_hw *hw,
Zhu Yib481de92007-09-25 17:54:57 -07006747 struct ieee80211_if_init_conf *conf)
6748{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006749 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07006750
6751 IWL_DEBUG_MAC80211("enter\n");
6752
6753 mutex_lock(&priv->mutex);
Mohamed Abbas948c1712007-10-25 17:15:45 +08006754
Tomas Winklerfee12472008-04-03 16:05:21 -07006755 if (iwl_is_ready_rf(priv)) {
Mohamed Abbasfde35712007-11-29 11:10:15 +08006756 iwl4965_scan_cancel_timeout(priv, 100);
6757 cancel_delayed_work(&priv->post_associate);
6758 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
6759 iwl4965_commit_rxon(priv);
6760 }
Johannes Berg32bfd352007-12-19 01:31:26 +01006761 if (priv->vif == conf->vif) {
6762 priv->vif = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07006763 memset(priv->bssid, 0, ETH_ALEN);
6764 memset(priv->essid, 0, IW_ESSID_MAX_SIZE);
6765 priv->essid_len = 0;
6766 }
6767 mutex_unlock(&priv->mutex);
6768
6769 IWL_DEBUG_MAC80211("leave\n");
6770
6771}
Johannes Berg471b3ef2007-12-28 14:32:58 +01006772
Tomas Winkler98952d52008-03-28 16:33:33 -07006773
6774#ifdef CONFIG_IWL4965_HT
6775static void iwl4965_ht_conf(struct iwl_priv *priv,
6776 struct ieee80211_bss_conf *bss_conf)
6777{
6778 struct ieee80211_ht_info *ht_conf = bss_conf->ht_conf;
6779 struct ieee80211_ht_bss_info *ht_bss_conf = bss_conf->ht_bss_conf;
6780 struct iwl_ht_info *iwl_conf = &priv->current_ht_config;
6781
6782 IWL_DEBUG_MAC80211("enter: \n");
6783
6784 iwl_conf->is_ht = bss_conf->assoc_ht;
6785
6786 if (!iwl_conf->is_ht)
6787 return;
6788
6789 priv->ps_mode = (u8)((ht_conf->cap & IEEE80211_HT_CAP_MIMO_PS) >> 2);
6790
6791 if (ht_conf->cap & IEEE80211_HT_CAP_SGI_20)
6792 iwl_conf->sgf |= 0x1;
6793 if (ht_conf->cap & IEEE80211_HT_CAP_SGI_40)
6794 iwl_conf->sgf |= 0x2;
6795
6796 iwl_conf->is_green_field = !!(ht_conf->cap & IEEE80211_HT_CAP_GRN_FLD);
6797 iwl_conf->max_amsdu_size =
6798 !!(ht_conf->cap & IEEE80211_HT_CAP_MAX_AMSDU);
6799
6800 iwl_conf->supported_chan_width =
6801 !!(ht_conf->cap & IEEE80211_HT_CAP_SUP_WIDTH);
6802 iwl_conf->extension_chan_offset =
6803 ht_bss_conf->bss_cap & IEEE80211_HT_IE_CHA_SEC_OFFSET;
6804 /* If no above or below channel supplied disable FAT channel */
6805 if (iwl_conf->extension_chan_offset != IWL_EXT_CHANNEL_OFFSET_ABOVE &&
6806 iwl_conf->extension_chan_offset != IWL_EXT_CHANNEL_OFFSET_BELOW)
6807 iwl_conf->supported_chan_width = 0;
6808
6809 iwl_conf->tx_mimo_ps_mode =
6810 (u8)((ht_conf->cap & IEEE80211_HT_CAP_MIMO_PS) >> 2);
6811 memcpy(iwl_conf->supp_mcs_set, ht_conf->supp_mcs_set, 16);
6812
6813 iwl_conf->control_channel = ht_bss_conf->primary_channel;
6814 iwl_conf->tx_chan_width =
6815 !!(ht_bss_conf->bss_cap & IEEE80211_HT_IE_CHA_WIDTH);
6816 iwl_conf->ht_protection =
6817 ht_bss_conf->bss_op_mode & IEEE80211_HT_IE_HT_PROTECTION;
6818 iwl_conf->non_GF_STA_present =
6819 !!(ht_bss_conf->bss_op_mode & IEEE80211_HT_IE_NON_GF_STA_PRSNT);
6820
6821 IWL_DEBUG_MAC80211("control channel %d\n", iwl_conf->control_channel);
6822 IWL_DEBUG_MAC80211("leave\n");
6823}
6824#else
6825static inline void iwl4965_ht_conf(struct iwl_priv *priv,
6826 struct ieee80211_bss_conf *bss_conf)
6827{
6828}
6829#endif
6830
Tomas Winkler3109ece2008-03-28 16:33:35 -07006831#define IWL_DELAY_NEXT_SCAN_AFTER_ASSOC (HZ*6)
Johannes Berg471b3ef2007-12-28 14:32:58 +01006832static void iwl4965_bss_info_changed(struct ieee80211_hw *hw,
6833 struct ieee80211_vif *vif,
6834 struct ieee80211_bss_conf *bss_conf,
6835 u32 changes)
Tomas Winkler220173b2007-10-16 00:50:25 +02006836{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006837 struct iwl_priv *priv = hw->priv;
Tomas Winkler220173b2007-10-16 00:50:25 +02006838
Tomas Winkler3109ece2008-03-28 16:33:35 -07006839 IWL_DEBUG_MAC80211("changes = 0x%X\n", changes);
6840
Johannes Berg471b3ef2007-12-28 14:32:58 +01006841 if (changes & BSS_CHANGED_ERP_PREAMBLE) {
Tomas Winkler3109ece2008-03-28 16:33:35 -07006842 IWL_DEBUG_MAC80211("ERP_PREAMBLE %d\n",
6843 bss_conf->use_short_preamble);
Johannes Berg471b3ef2007-12-28 14:32:58 +01006844 if (bss_conf->use_short_preamble)
Tomas Winkler220173b2007-10-16 00:50:25 +02006845 priv->staging_rxon.flags |= RXON_FLG_SHORT_PREAMBLE_MSK;
6846 else
6847 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_PREAMBLE_MSK;
6848 }
6849
Johannes Berg471b3ef2007-12-28 14:32:58 +01006850 if (changes & BSS_CHANGED_ERP_CTS_PROT) {
Tomas Winkler3109ece2008-03-28 16:33:35 -07006851 IWL_DEBUG_MAC80211("ERP_CTS %d\n", bss_conf->use_cts_prot);
Johannes Berg8318d782008-01-24 19:38:38 +01006852 if (bss_conf->use_cts_prot && (priv->band != IEEE80211_BAND_5GHZ))
Tomas Winkler220173b2007-10-16 00:50:25 +02006853 priv->staging_rxon.flags |= RXON_FLG_TGG_PROTECT_MSK;
6854 else
6855 priv->staging_rxon.flags &= ~RXON_FLG_TGG_PROTECT_MSK;
6856 }
6857
Tomas Winkler98952d52008-03-28 16:33:33 -07006858 if (changes & BSS_CHANGED_HT) {
Tomas Winkler3109ece2008-03-28 16:33:35 -07006859 IWL_DEBUG_MAC80211("HT %d\n", bss_conf->assoc_ht);
Tomas Winkler98952d52008-03-28 16:33:33 -07006860 iwl4965_ht_conf(priv, bss_conf);
6861 iwl4965_set_rxon_chain(priv);
6862 }
6863
Johannes Berg471b3ef2007-12-28 14:32:58 +01006864 if (changes & BSS_CHANGED_ASSOC) {
Tomas Winkler3109ece2008-03-28 16:33:35 -07006865 IWL_DEBUG_MAC80211("ASSOC %d\n", bss_conf->assoc);
6866 if (bss_conf->assoc) {
6867 priv->assoc_id = bss_conf->aid;
6868 priv->beacon_int = bss_conf->beacon_int;
6869 priv->timestamp = bss_conf->timestamp;
6870 priv->assoc_capability = bss_conf->assoc_capability;
6871 priv->next_scan_jiffies = jiffies +
6872 IWL_DELAY_NEXT_SCAN_AFTER_ASSOC;
6873 queue_work(priv->workqueue, &priv->post_associate.work);
6874 } else {
6875 priv->assoc_id = 0;
6876 IWL_DEBUG_MAC80211("DISASSOC %d\n", bss_conf->assoc);
6877 }
6878 } else if (changes && iwl_is_associated(priv) && priv->assoc_id) {
6879 IWL_DEBUG_MAC80211("Associated Changes %d\n", changes);
6880 iwl4965_send_rxon_assoc(priv);
Johannes Berg471b3ef2007-12-28 14:32:58 +01006881 }
6882
Tomas Winkler220173b2007-10-16 00:50:25 +02006883}
Zhu Yib481de92007-09-25 17:54:57 -07006884
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006885static int iwl4965_mac_hw_scan(struct ieee80211_hw *hw, u8 *ssid, size_t len)
Zhu Yib481de92007-09-25 17:54:57 -07006886{
6887 int rc = 0;
6888 unsigned long flags;
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006889 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07006890
6891 IWL_DEBUG_MAC80211("enter\n");
6892
mabbas052c4b92007-10-25 17:15:43 +08006893 mutex_lock(&priv->mutex);
Zhu Yib481de92007-09-25 17:54:57 -07006894 spin_lock_irqsave(&priv->lock, flags);
6895
Tomas Winklerfee12472008-04-03 16:05:21 -07006896 if (!iwl_is_ready_rf(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07006897 rc = -EIO;
6898 IWL_DEBUG_MAC80211("leave - not ready or exit pending\n");
6899 goto out_unlock;
6900 }
6901
6902 if (priv->iw_mode == IEEE80211_IF_TYPE_AP) { /* APs don't scan */
6903 rc = -EIO;
6904 IWL_ERROR("ERROR: APs don't scan\n");
6905 goto out_unlock;
6906 }
6907
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08006908 /* we don't schedule scan within next_scan_jiffies period */
6909 if (priv->next_scan_jiffies &&
6910 time_after(priv->next_scan_jiffies, jiffies)) {
6911 rc = -EAGAIN;
6912 goto out_unlock;
6913 }
Zhu Yib481de92007-09-25 17:54:57 -07006914 /* if we just finished scan ask for delay */
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08006915 if (priv->last_scan_jiffies && time_after(priv->last_scan_jiffies +
6916 IWL_DELAY_NEXT_SCAN, jiffies)) {
Zhu Yib481de92007-09-25 17:54:57 -07006917 rc = -EAGAIN;
6918 goto out_unlock;
6919 }
6920 if (len) {
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08006921 IWL_DEBUG_SCAN("direct scan for %s [%d]\n ",
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006922 iwl4965_escape_essid(ssid, len), (int)len);
Zhu Yib481de92007-09-25 17:54:57 -07006923
6924 priv->one_direct_scan = 1;
6925 priv->direct_ssid_len = (u8)
6926 min((u8) len, (u8) IW_ESSID_MAX_SIZE);
6927 memcpy(priv->direct_ssid, ssid, priv->direct_ssid_len);
Mohamed Abbas948c1712007-10-25 17:15:45 +08006928 } else
6929 priv->one_direct_scan = 0;
Zhu Yib481de92007-09-25 17:54:57 -07006930
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006931 rc = iwl4965_scan_initiate(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006932
6933 IWL_DEBUG_MAC80211("leave\n");
6934
6935out_unlock:
6936 spin_unlock_irqrestore(&priv->lock, flags);
mabbas052c4b92007-10-25 17:15:43 +08006937 mutex_unlock(&priv->mutex);
Zhu Yib481de92007-09-25 17:54:57 -07006938
6939 return rc;
6940}
6941
Emmanuel Grumbachab885f82008-03-20 15:06:43 +02006942static void iwl4965_mac_update_tkip_key(struct ieee80211_hw *hw,
6943 struct ieee80211_key_conf *keyconf, const u8 *addr,
6944 u32 iv32, u16 *phase1key)
6945{
6946 struct iwl_priv *priv = hw->priv;
6947 u8 sta_id = IWL_INVALID_STATION;
6948 unsigned long flags;
6949 __le16 key_flags = 0;
6950 int i;
6951 DECLARE_MAC_BUF(mac);
6952
6953 IWL_DEBUG_MAC80211("enter\n");
6954
6955 sta_id = iwl4965_hw_find_station(priv, addr);
6956 if (sta_id == IWL_INVALID_STATION) {
6957 IWL_DEBUG_MAC80211("leave - %s not in station map.\n",
6958 print_mac(mac, addr));
6959 return;
6960 }
6961
6962 iwl4965_scan_cancel_timeout(priv, 100);
6963
6964 key_flags |= (STA_KEY_FLG_TKIP | STA_KEY_FLG_MAP_KEY_MSK);
6965 key_flags |= cpu_to_le16(keyconf->keyidx << STA_KEY_FLG_KEYID_POS);
6966 key_flags &= ~STA_KEY_FLG_INVALID;
6967
6968 if (sta_id == priv->hw_setting.bcast_sta_id)
6969 key_flags |= STA_KEY_MULTICAST_MSK;
6970
6971 spin_lock_irqsave(&priv->sta_lock, flags);
6972
6973 priv->stations[sta_id].sta.key.key_offset =
6974 (sta_id % STA_KEY_MAX_NUM);/* FIXME */
6975 priv->stations[sta_id].sta.key.key_flags = key_flags;
6976 priv->stations[sta_id].sta.key.tkip_rx_tsc_byte2 = (u8) iv32;
6977
6978 for (i = 0; i < 5; i++)
6979 priv->stations[sta_id].sta.key.tkip_rx_ttak[i] =
6980 cpu_to_le16(phase1key[i]);
6981
6982 priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
6983 priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
6984
6985 iwl4965_send_add_station(priv, &priv->stations[sta_id].sta, CMD_ASYNC);
6986
6987 spin_unlock_irqrestore(&priv->sta_lock, flags);
6988
6989 IWL_DEBUG_MAC80211("leave\n");
6990}
6991
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006992static int iwl4965_mac_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
Zhu Yib481de92007-09-25 17:54:57 -07006993 const u8 *local_addr, const u8 *addr,
6994 struct ieee80211_key_conf *key)
6995{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006996 struct iwl_priv *priv = hw->priv;
Joe Perches0795af52007-10-03 17:59:30 -07006997 DECLARE_MAC_BUF(mac);
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07006998 int ret = 0;
6999 u8 sta_id = IWL_INVALID_STATION;
Emmanuel Grumbach6974e362008-04-14 21:16:06 -07007000 u8 is_default_wep_key = 0;
Zhu Yib481de92007-09-25 17:54:57 -07007001
7002 IWL_DEBUG_MAC80211("enter\n");
7003
Assaf Krauss1ea87392008-03-18 14:57:50 -07007004 if (!priv->cfg->mod_params->hw_crypto) {
Zhu Yib481de92007-09-25 17:54:57 -07007005 IWL_DEBUG_MAC80211("leave - hwcrypto disabled\n");
7006 return -EOPNOTSUPP;
7007 }
7008
7009 if (is_zero_ether_addr(addr))
7010 /* only support pairwise keys */
7011 return -EOPNOTSUPP;
7012
Emmanuel Grumbach6974e362008-04-14 21:16:06 -07007013 sta_id = iwl4965_hw_find_station(priv, addr);
7014 if (sta_id == IWL_INVALID_STATION) {
7015 IWL_DEBUG_MAC80211("leave - %s not in station map.\n",
7016 print_mac(mac, addr));
7017 return -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07007018
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07007019 }
Zhu Yib481de92007-09-25 17:54:57 -07007020
Emmanuel Grumbach6974e362008-04-14 21:16:06 -07007021 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007022 iwl4965_scan_cancel_timeout(priv, 100);
Emmanuel Grumbach6974e362008-04-14 21:16:06 -07007023 mutex_unlock(&priv->mutex);
7024
7025 /* If we are getting WEP group key and we didn't receive any key mapping
7026 * so far, we are in legacy wep mode (group key only), otherwise we are
7027 * in 1X mode.
7028 * In legacy wep mode, we use another host command to the uCode */
7029 if (key->alg == ALG_WEP && sta_id == priv->hw_setting.bcast_sta_id &&
7030 priv->iw_mode != IEEE80211_IF_TYPE_AP) {
7031 if (cmd == SET_KEY)
7032 is_default_wep_key = !priv->key_mapping_key;
7033 else
7034 is_default_wep_key = priv->default_wep_key;
7035 }
mabbas052c4b92007-10-25 17:15:43 +08007036
Zhu Yib481de92007-09-25 17:54:57 -07007037 switch (cmd) {
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07007038 case SET_KEY:
Emmanuel Grumbach6974e362008-04-14 21:16:06 -07007039 if (is_default_wep_key)
7040 ret = iwl_set_default_wep_key(priv, key);
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07007041 else
7042 ret = iwl4965_set_dynamic_key(priv, key, sta_id);
7043
7044 IWL_DEBUG_MAC80211("enable hwcrypto key\n");
Zhu Yib481de92007-09-25 17:54:57 -07007045 break;
7046 case DISABLE_KEY:
Emmanuel Grumbach6974e362008-04-14 21:16:06 -07007047 if (is_default_wep_key)
7048 ret = iwl_remove_default_wep_key(priv, key);
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07007049 else
7050 ret = iwl4965_clear_sta_key_info(priv, sta_id);
7051
7052 IWL_DEBUG_MAC80211("disable hwcrypto key\n");
Zhu Yib481de92007-09-25 17:54:57 -07007053 break;
7054 default:
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07007055 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07007056 }
7057
7058 IWL_DEBUG_MAC80211("leave\n");
Zhu Yib481de92007-09-25 17:54:57 -07007059
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07007060 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07007061}
7062
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007063static int iwl4965_mac_conf_tx(struct ieee80211_hw *hw, int queue,
Zhu Yib481de92007-09-25 17:54:57 -07007064 const struct ieee80211_tx_queue_params *params)
7065{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007066 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07007067 unsigned long flags;
7068 int q;
Zhu Yib481de92007-09-25 17:54:57 -07007069
7070 IWL_DEBUG_MAC80211("enter\n");
7071
Tomas Winklerfee12472008-04-03 16:05:21 -07007072 if (!iwl_is_ready_rf(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07007073 IWL_DEBUG_MAC80211("leave - RF not ready\n");
7074 return -EIO;
7075 }
7076
7077 if (queue >= AC_NUM) {
7078 IWL_DEBUG_MAC80211("leave - queue >= AC_NUM %d\n", queue);
7079 return 0;
7080 }
7081
Zhu Yib481de92007-09-25 17:54:57 -07007082 if (!priv->qos_data.qos_enable) {
7083 priv->qos_data.qos_active = 0;
7084 IWL_DEBUG_MAC80211("leave - qos not enabled\n");
7085 return 0;
7086 }
7087 q = AC_NUM - 1 - queue;
7088
7089 spin_lock_irqsave(&priv->lock, flags);
7090
7091 priv->qos_data.def_qos_parm.ac[q].cw_min = cpu_to_le16(params->cw_min);
7092 priv->qos_data.def_qos_parm.ac[q].cw_max = cpu_to_le16(params->cw_max);
7093 priv->qos_data.def_qos_parm.ac[q].aifsn = params->aifs;
7094 priv->qos_data.def_qos_parm.ac[q].edca_txop =
Johannes Berg3330d7b2008-02-10 16:49:38 +01007095 cpu_to_le16((params->txop * 32));
Zhu Yib481de92007-09-25 17:54:57 -07007096
7097 priv->qos_data.def_qos_parm.ac[q].reserved1 = 0;
7098 priv->qos_data.qos_active = 1;
7099
7100 spin_unlock_irqrestore(&priv->lock, flags);
7101
7102 mutex_lock(&priv->mutex);
7103 if (priv->iw_mode == IEEE80211_IF_TYPE_AP)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007104 iwl4965_activate_qos(priv, 1);
Tomas Winkler3109ece2008-03-28 16:33:35 -07007105 else if (priv->assoc_id && iwl_is_associated(priv))
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007106 iwl4965_activate_qos(priv, 0);
Zhu Yib481de92007-09-25 17:54:57 -07007107
7108 mutex_unlock(&priv->mutex);
7109
Zhu Yib481de92007-09-25 17:54:57 -07007110 IWL_DEBUG_MAC80211("leave\n");
7111 return 0;
7112}
7113
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007114static int iwl4965_mac_get_tx_stats(struct ieee80211_hw *hw,
Zhu Yib481de92007-09-25 17:54:57 -07007115 struct ieee80211_tx_queue_stats *stats)
7116{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007117 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07007118 int i, avail;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007119 struct iwl4965_tx_queue *txq;
7120 struct iwl4965_queue *q;
Zhu Yib481de92007-09-25 17:54:57 -07007121 unsigned long flags;
7122
7123 IWL_DEBUG_MAC80211("enter\n");
7124
Tomas Winklerfee12472008-04-03 16:05:21 -07007125 if (!iwl_is_ready_rf(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07007126 IWL_DEBUG_MAC80211("leave - RF not ready\n");
7127 return -EIO;
7128 }
7129
7130 spin_lock_irqsave(&priv->lock, flags);
7131
7132 for (i = 0; i < AC_NUM; i++) {
7133 txq = &priv->txq[i];
7134 q = &txq->q;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007135 avail = iwl4965_queue_space(q);
Zhu Yib481de92007-09-25 17:54:57 -07007136
7137 stats->data[i].len = q->n_window - avail;
7138 stats->data[i].limit = q->n_window - q->high_mark;
7139 stats->data[i].count = q->n_window;
7140
7141 }
7142 spin_unlock_irqrestore(&priv->lock, flags);
7143
7144 IWL_DEBUG_MAC80211("leave\n");
7145
7146 return 0;
7147}
7148
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007149static int iwl4965_mac_get_stats(struct ieee80211_hw *hw,
Zhu Yib481de92007-09-25 17:54:57 -07007150 struct ieee80211_low_level_stats *stats)
7151{
7152 IWL_DEBUG_MAC80211("enter\n");
7153 IWL_DEBUG_MAC80211("leave\n");
7154
7155 return 0;
7156}
7157
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007158static u64 iwl4965_mac_get_tsf(struct ieee80211_hw *hw)
Zhu Yib481de92007-09-25 17:54:57 -07007159{
7160 IWL_DEBUG_MAC80211("enter\n");
7161 IWL_DEBUG_MAC80211("leave\n");
7162
7163 return 0;
7164}
7165
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007166static void iwl4965_mac_reset_tsf(struct ieee80211_hw *hw)
Zhu Yib481de92007-09-25 17:54:57 -07007167{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007168 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07007169 unsigned long flags;
7170
7171 mutex_lock(&priv->mutex);
7172 IWL_DEBUG_MAC80211("enter\n");
7173
7174 priv->lq_mngr.lq_ready = 0;
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08007175#ifdef CONFIG_IWL4965_HT
Zhu Yib481de92007-09-25 17:54:57 -07007176 spin_lock_irqsave(&priv->lock, flags);
Ron Rindjunskyfd105e72007-11-26 16:14:39 +02007177 memset(&priv->current_ht_config, 0, sizeof(struct iwl_ht_info));
Zhu Yib481de92007-09-25 17:54:57 -07007178 spin_unlock_irqrestore(&priv->lock, flags);
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08007179#endif /* CONFIG_IWL4965_HT */
Zhu Yib481de92007-09-25 17:54:57 -07007180
Assaf Kraussbf85ea42008-03-14 10:38:49 -07007181 iwlcore_reset_qos(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007182
7183 cancel_delayed_work(&priv->post_associate);
7184
7185 spin_lock_irqsave(&priv->lock, flags);
7186 priv->assoc_id = 0;
7187 priv->assoc_capability = 0;
Zhu Yib481de92007-09-25 17:54:57 -07007188 priv->assoc_station_added = 0;
7189
7190 /* new association get rid of ibss beacon skb */
7191 if (priv->ibss_beacon)
7192 dev_kfree_skb(priv->ibss_beacon);
7193
7194 priv->ibss_beacon = NULL;
7195
7196 priv->beacon_int = priv->hw->conf.beacon_int;
Tomas Winkler3109ece2008-03-28 16:33:35 -07007197 priv->timestamp = 0;
Zhu Yib481de92007-09-25 17:54:57 -07007198 if ((priv->iw_mode == IEEE80211_IF_TYPE_STA))
7199 priv->beacon_int = 0;
7200
7201 spin_unlock_irqrestore(&priv->lock, flags);
7202
Tomas Winklerfee12472008-04-03 16:05:21 -07007203 if (!iwl_is_ready_rf(priv)) {
Mohamed Abbasfde35712007-11-29 11:10:15 +08007204 IWL_DEBUG_MAC80211("leave - not ready\n");
7205 mutex_unlock(&priv->mutex);
7206 return;
7207 }
7208
mabbas052c4b92007-10-25 17:15:43 +08007209 /* we are restarting association process
7210 * clear RXON_FILTER_ASSOC_MSK bit
7211 */
7212 if (priv->iw_mode != IEEE80211_IF_TYPE_AP) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007213 iwl4965_scan_cancel_timeout(priv, 100);
mabbas052c4b92007-10-25 17:15:43 +08007214 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007215 iwl4965_commit_rxon(priv);
mabbas052c4b92007-10-25 17:15:43 +08007216 }
7217
Zhu Yib481de92007-09-25 17:54:57 -07007218 /* Per mac80211.h: This is only used in IBSS mode... */
7219 if (priv->iw_mode != IEEE80211_IF_TYPE_IBSS) {
mabbas052c4b92007-10-25 17:15:43 +08007220
Zhu Yib481de92007-09-25 17:54:57 -07007221 IWL_DEBUG_MAC80211("leave - not in IBSS\n");
7222 mutex_unlock(&priv->mutex);
7223 return;
7224 }
7225
Zhu Yib481de92007-09-25 17:54:57 -07007226 priv->only_active_channel = 0;
7227
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007228 iwl4965_set_rate(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007229
7230 mutex_unlock(&priv->mutex);
7231
7232 IWL_DEBUG_MAC80211("leave\n");
Zhu Yib481de92007-09-25 17:54:57 -07007233}
7234
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007235static int iwl4965_mac_beacon_update(struct ieee80211_hw *hw, struct sk_buff *skb,
Zhu Yib481de92007-09-25 17:54:57 -07007236 struct ieee80211_tx_control *control)
7237{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007238 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07007239 unsigned long flags;
7240
7241 mutex_lock(&priv->mutex);
7242 IWL_DEBUG_MAC80211("enter\n");
7243
Tomas Winklerfee12472008-04-03 16:05:21 -07007244 if (!iwl_is_ready_rf(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07007245 IWL_DEBUG_MAC80211("leave - RF not ready\n");
7246 mutex_unlock(&priv->mutex);
7247 return -EIO;
7248 }
7249
7250 if (priv->iw_mode != IEEE80211_IF_TYPE_IBSS) {
7251 IWL_DEBUG_MAC80211("leave - not IBSS\n");
7252 mutex_unlock(&priv->mutex);
7253 return -EIO;
7254 }
7255
7256 spin_lock_irqsave(&priv->lock, flags);
7257
7258 if (priv->ibss_beacon)
7259 dev_kfree_skb(priv->ibss_beacon);
7260
7261 priv->ibss_beacon = skb;
7262
7263 priv->assoc_id = 0;
7264
7265 IWL_DEBUG_MAC80211("leave\n");
7266 spin_unlock_irqrestore(&priv->lock, flags);
7267
Assaf Kraussbf85ea42008-03-14 10:38:49 -07007268 iwlcore_reset_qos(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007269
7270 queue_work(priv->workqueue, &priv->post_associate.work);
7271
7272 mutex_unlock(&priv->mutex);
7273
7274 return 0;
7275}
7276
Zhu Yib481de92007-09-25 17:54:57 -07007277/*****************************************************************************
7278 *
7279 * sysfs attributes
7280 *
7281 *****************************************************************************/
7282
Tomas Winkler0a6857e2008-03-12 16:58:49 -07007283#ifdef CONFIG_IWLWIFI_DEBUG
Zhu Yib481de92007-09-25 17:54:57 -07007284
7285/*
7286 * The following adds a new attribute to the sysfs representation
7287 * of this device driver (i.e. a new file in /sys/bus/pci/drivers/iwl/)
7288 * used for controlling the debug level.
7289 *
7290 * See the level definitions in iwl for details.
7291 */
7292
7293static ssize_t show_debug_level(struct device_driver *d, char *buf)
7294{
Tomas Winkler0a6857e2008-03-12 16:58:49 -07007295 return sprintf(buf, "0x%08X\n", iwl_debug_level);
Zhu Yib481de92007-09-25 17:54:57 -07007296}
7297static ssize_t store_debug_level(struct device_driver *d,
7298 const char *buf, size_t count)
7299{
7300 char *p = (char *)buf;
7301 u32 val;
7302
7303 val = simple_strtoul(p, &p, 0);
7304 if (p == buf)
7305 printk(KERN_INFO DRV_NAME
7306 ": %s is not in hex or decimal form.\n", buf);
7307 else
Tomas Winkler0a6857e2008-03-12 16:58:49 -07007308 iwl_debug_level = val;
Zhu Yib481de92007-09-25 17:54:57 -07007309
7310 return strnlen(buf, count);
7311}
7312
7313static DRIVER_ATTR(debug_level, S_IWUSR | S_IRUGO,
7314 show_debug_level, store_debug_level);
7315
Tomas Winkler0a6857e2008-03-12 16:58:49 -07007316#endif /* CONFIG_IWLWIFI_DEBUG */
Zhu Yib481de92007-09-25 17:54:57 -07007317
Zhu Yib481de92007-09-25 17:54:57 -07007318
7319static ssize_t show_temperature(struct device *d,
7320 struct device_attribute *attr, char *buf)
7321{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007322 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07007323
Tomas Winklerfee12472008-04-03 16:05:21 -07007324 if (!iwl_is_alive(priv))
Zhu Yib481de92007-09-25 17:54:57 -07007325 return -EAGAIN;
7326
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007327 return sprintf(buf, "%d\n", iwl4965_hw_get_temperature(priv));
Zhu Yib481de92007-09-25 17:54:57 -07007328}
7329
7330static DEVICE_ATTR(temperature, S_IRUGO, show_temperature, NULL);
7331
7332static ssize_t show_rs_window(struct device *d,
7333 struct device_attribute *attr,
7334 char *buf)
7335{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007336 struct iwl_priv *priv = d->driver_data;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007337 return iwl4965_fill_rs_info(priv->hw, buf, IWL_AP_ID);
Zhu Yib481de92007-09-25 17:54:57 -07007338}
7339static DEVICE_ATTR(rs_window, S_IRUGO, show_rs_window, NULL);
7340
7341static ssize_t show_tx_power(struct device *d,
7342 struct device_attribute *attr, char *buf)
7343{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007344 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07007345 return sprintf(buf, "%d\n", priv->user_txpower_limit);
7346}
7347
7348static ssize_t store_tx_power(struct device *d,
7349 struct device_attribute *attr,
7350 const char *buf, size_t count)
7351{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007352 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07007353 char *p = (char *)buf;
7354 u32 val;
7355
7356 val = simple_strtoul(p, &p, 10);
7357 if (p == buf)
7358 printk(KERN_INFO DRV_NAME
7359 ": %s is not in decimal form.\n", buf);
7360 else
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007361 iwl4965_hw_reg_set_txpower(priv, val);
Zhu Yib481de92007-09-25 17:54:57 -07007362
7363 return count;
7364}
7365
7366static DEVICE_ATTR(tx_power, S_IWUSR | S_IRUGO, show_tx_power, store_tx_power);
7367
7368static ssize_t show_flags(struct device *d,
7369 struct device_attribute *attr, char *buf)
7370{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007371 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07007372
7373 return sprintf(buf, "0x%04X\n", priv->active_rxon.flags);
7374}
7375
7376static ssize_t store_flags(struct device *d,
7377 struct device_attribute *attr,
7378 const char *buf, size_t count)
7379{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007380 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07007381 u32 flags = simple_strtoul(buf, NULL, 0);
7382
7383 mutex_lock(&priv->mutex);
7384 if (le32_to_cpu(priv->staging_rxon.flags) != flags) {
7385 /* Cancel any currently running scans... */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007386 if (iwl4965_scan_cancel_timeout(priv, 100))
Zhu Yib481de92007-09-25 17:54:57 -07007387 IWL_WARNING("Could not cancel scan.\n");
7388 else {
7389 IWL_DEBUG_INFO("Committing rxon.flags = 0x%04X\n",
7390 flags);
7391 priv->staging_rxon.flags = cpu_to_le32(flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007392 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007393 }
7394 }
7395 mutex_unlock(&priv->mutex);
7396
7397 return count;
7398}
7399
7400static DEVICE_ATTR(flags, S_IWUSR | S_IRUGO, show_flags, store_flags);
7401
7402static ssize_t show_filter_flags(struct device *d,
7403 struct device_attribute *attr, char *buf)
7404{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007405 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07007406
7407 return sprintf(buf, "0x%04X\n",
7408 le32_to_cpu(priv->active_rxon.filter_flags));
7409}
7410
7411static ssize_t store_filter_flags(struct device *d,
7412 struct device_attribute *attr,
7413 const char *buf, size_t count)
7414{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007415 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07007416 u32 filter_flags = simple_strtoul(buf, NULL, 0);
7417
7418 mutex_lock(&priv->mutex);
7419 if (le32_to_cpu(priv->staging_rxon.filter_flags) != filter_flags) {
7420 /* Cancel any currently running scans... */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007421 if (iwl4965_scan_cancel_timeout(priv, 100))
Zhu Yib481de92007-09-25 17:54:57 -07007422 IWL_WARNING("Could not cancel scan.\n");
7423 else {
7424 IWL_DEBUG_INFO("Committing rxon.filter_flags = "
7425 "0x%04X\n", filter_flags);
7426 priv->staging_rxon.filter_flags =
7427 cpu_to_le32(filter_flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007428 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007429 }
7430 }
7431 mutex_unlock(&priv->mutex);
7432
7433 return count;
7434}
7435
7436static DEVICE_ATTR(filter_flags, S_IWUSR | S_IRUGO, show_filter_flags,
7437 store_filter_flags);
7438
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08007439#ifdef CONFIG_IWL4965_SPECTRUM_MEASUREMENT
Zhu Yib481de92007-09-25 17:54:57 -07007440
7441static ssize_t show_measurement(struct device *d,
7442 struct device_attribute *attr, char *buf)
7443{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007444 struct iwl_priv *priv = dev_get_drvdata(d);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007445 struct iwl4965_spectrum_notification measure_report;
Zhu Yib481de92007-09-25 17:54:57 -07007446 u32 size = sizeof(measure_report), len = 0, ofs = 0;
7447 u8 *data = (u8 *) & measure_report;
7448 unsigned long flags;
7449
7450 spin_lock_irqsave(&priv->lock, flags);
7451 if (!(priv->measurement_status & MEASUREMENT_READY)) {
7452 spin_unlock_irqrestore(&priv->lock, flags);
7453 return 0;
7454 }
7455 memcpy(&measure_report, &priv->measure_report, size);
7456 priv->measurement_status = 0;
7457 spin_unlock_irqrestore(&priv->lock, flags);
7458
7459 while (size && (PAGE_SIZE - len)) {
7460 hex_dump_to_buffer(data + ofs, size, 16, 1, buf + len,
7461 PAGE_SIZE - len, 1);
7462 len = strlen(buf);
7463 if (PAGE_SIZE - len)
7464 buf[len++] = '\n';
7465
7466 ofs += 16;
7467 size -= min(size, 16U);
7468 }
7469
7470 return len;
7471}
7472
7473static ssize_t store_measurement(struct device *d,
7474 struct device_attribute *attr,
7475 const char *buf, size_t count)
7476{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007477 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07007478 struct ieee80211_measurement_params params = {
7479 .channel = le16_to_cpu(priv->active_rxon.channel),
7480 .start_time = cpu_to_le64(priv->last_tsf),
7481 .duration = cpu_to_le16(1),
7482 };
7483 u8 type = IWL_MEASURE_BASIC;
7484 u8 buffer[32];
7485 u8 channel;
7486
7487 if (count) {
7488 char *p = buffer;
7489 strncpy(buffer, buf, min(sizeof(buffer), count));
7490 channel = simple_strtoul(p, NULL, 0);
7491 if (channel)
7492 params.channel = channel;
7493
7494 p = buffer;
7495 while (*p && *p != ' ')
7496 p++;
7497 if (*p)
7498 type = simple_strtoul(p + 1, NULL, 0);
7499 }
7500
7501 IWL_DEBUG_INFO("Invoking measurement of type %d on "
7502 "channel %d (for '%s')\n", type, params.channel, buf);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007503 iwl4965_get_measurement(priv, &params, type);
Zhu Yib481de92007-09-25 17:54:57 -07007504
7505 return count;
7506}
7507
7508static DEVICE_ATTR(measurement, S_IRUSR | S_IWUSR,
7509 show_measurement, store_measurement);
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08007510#endif /* CONFIG_IWL4965_SPECTRUM_MEASUREMENT */
Zhu Yib481de92007-09-25 17:54:57 -07007511
7512static ssize_t store_retry_rate(struct device *d,
7513 struct device_attribute *attr,
7514 const char *buf, size_t count)
7515{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007516 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07007517
7518 priv->retry_rate = simple_strtoul(buf, NULL, 0);
7519 if (priv->retry_rate <= 0)
7520 priv->retry_rate = 1;
7521
7522 return count;
7523}
7524
7525static ssize_t show_retry_rate(struct device *d,
7526 struct device_attribute *attr, char *buf)
7527{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007528 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07007529 return sprintf(buf, "%d", priv->retry_rate);
7530}
7531
7532static DEVICE_ATTR(retry_rate, S_IWUSR | S_IRUSR, show_retry_rate,
7533 store_retry_rate);
7534
7535static ssize_t store_power_level(struct device *d,
7536 struct device_attribute *attr,
7537 const char *buf, size_t count)
7538{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007539 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07007540 int rc;
7541 int mode;
7542
7543 mode = simple_strtoul(buf, NULL, 0);
7544 mutex_lock(&priv->mutex);
7545
Tomas Winklerfee12472008-04-03 16:05:21 -07007546 if (!iwl_is_ready(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07007547 rc = -EAGAIN;
7548 goto out;
7549 }
7550
7551 if ((mode < 1) || (mode > IWL_POWER_LIMIT) || (mode == IWL_POWER_AC))
7552 mode = IWL_POWER_AC;
7553 else
7554 mode |= IWL_POWER_ENABLED;
7555
7556 if (mode != priv->power_mode) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007557 rc = iwl4965_send_power_mode(priv, IWL_POWER_LEVEL(mode));
Zhu Yib481de92007-09-25 17:54:57 -07007558 if (rc) {
7559 IWL_DEBUG_MAC80211("failed setting power mode.\n");
7560 goto out;
7561 }
7562 priv->power_mode = mode;
7563 }
7564
7565 rc = count;
7566
7567 out:
7568 mutex_unlock(&priv->mutex);
7569 return rc;
7570}
7571
7572#define MAX_WX_STRING 80
7573
7574/* Values are in microsecond */
7575static const s32 timeout_duration[] = {
7576 350000,
7577 250000,
7578 75000,
7579 37000,
7580 25000,
7581};
7582static const s32 period_duration[] = {
7583 400000,
7584 700000,
7585 1000000,
7586 1000000,
7587 1000000
7588};
7589
7590static ssize_t show_power_level(struct device *d,
7591 struct device_attribute *attr, char *buf)
7592{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007593 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07007594 int level = IWL_POWER_LEVEL(priv->power_mode);
7595 char *p = buf;
7596
7597 p += sprintf(p, "%d ", level);
7598 switch (level) {
7599 case IWL_POWER_MODE_CAM:
7600 case IWL_POWER_AC:
7601 p += sprintf(p, "(AC)");
7602 break;
7603 case IWL_POWER_BATTERY:
7604 p += sprintf(p, "(BATTERY)");
7605 break;
7606 default:
7607 p += sprintf(p,
7608 "(Timeout %dms, Period %dms)",
7609 timeout_duration[level - 1] / 1000,
7610 period_duration[level - 1] / 1000);
7611 }
7612
7613 if (!(priv->power_mode & IWL_POWER_ENABLED))
7614 p += sprintf(p, " OFF\n");
7615 else
7616 p += sprintf(p, " \n");
7617
7618 return (p - buf + 1);
7619
7620}
7621
7622static DEVICE_ATTR(power_level, S_IWUSR | S_IRUSR, show_power_level,
7623 store_power_level);
7624
7625static ssize_t show_channels(struct device *d,
7626 struct device_attribute *attr, char *buf)
7627{
Johannes Berg8318d782008-01-24 19:38:38 +01007628 /* all this shit doesn't belong into sysfs anyway */
7629 return 0;
Zhu Yib481de92007-09-25 17:54:57 -07007630}
7631
7632static DEVICE_ATTR(channels, S_IRUSR, show_channels, NULL);
7633
7634static ssize_t show_statistics(struct device *d,
7635 struct device_attribute *attr, char *buf)
7636{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007637 struct iwl_priv *priv = dev_get_drvdata(d);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007638 u32 size = sizeof(struct iwl4965_notif_statistics);
Zhu Yib481de92007-09-25 17:54:57 -07007639 u32 len = 0, ofs = 0;
7640 u8 *data = (u8 *) & priv->statistics;
7641 int rc = 0;
7642
Tomas Winklerfee12472008-04-03 16:05:21 -07007643 if (!iwl_is_alive(priv))
Zhu Yib481de92007-09-25 17:54:57 -07007644 return -EAGAIN;
7645
7646 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007647 rc = iwl4965_send_statistics_request(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007648 mutex_unlock(&priv->mutex);
7649
7650 if (rc) {
7651 len = sprintf(buf,
7652 "Error sending statistics request: 0x%08X\n", rc);
7653 return len;
7654 }
7655
7656 while (size && (PAGE_SIZE - len)) {
7657 hex_dump_to_buffer(data + ofs, size, 16, 1, buf + len,
7658 PAGE_SIZE - len, 1);
7659 len = strlen(buf);
7660 if (PAGE_SIZE - len)
7661 buf[len++] = '\n';
7662
7663 ofs += 16;
7664 size -= min(size, 16U);
7665 }
7666
7667 return len;
7668}
7669
7670static DEVICE_ATTR(statistics, S_IRUGO, show_statistics, NULL);
7671
7672static ssize_t show_antenna(struct device *d,
7673 struct device_attribute *attr, char *buf)
7674{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007675 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07007676
Tomas Winklerfee12472008-04-03 16:05:21 -07007677 if (!iwl_is_alive(priv))
Zhu Yib481de92007-09-25 17:54:57 -07007678 return -EAGAIN;
7679
7680 return sprintf(buf, "%d\n", priv->antenna);
7681}
7682
7683static ssize_t store_antenna(struct device *d,
7684 struct device_attribute *attr,
7685 const char *buf, size_t count)
7686{
7687 int ant;
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007688 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07007689
7690 if (count == 0)
7691 return 0;
7692
7693 if (sscanf(buf, "%1i", &ant) != 1) {
7694 IWL_DEBUG_INFO("not in hex or decimal form.\n");
7695 return count;
7696 }
7697
7698 if ((ant >= 0) && (ant <= 2)) {
7699 IWL_DEBUG_INFO("Setting antenna select to %d.\n", ant);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007700 priv->antenna = (enum iwl4965_antenna)ant;
Zhu Yib481de92007-09-25 17:54:57 -07007701 } else
7702 IWL_DEBUG_INFO("Bad antenna select value %d.\n", ant);
7703
7704
7705 return count;
7706}
7707
7708static DEVICE_ATTR(antenna, S_IWUSR | S_IRUGO, show_antenna, store_antenna);
7709
7710static ssize_t show_status(struct device *d,
7711 struct device_attribute *attr, char *buf)
7712{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007713 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Tomas Winklerfee12472008-04-03 16:05:21 -07007714 if (!iwl_is_alive(priv))
Zhu Yib481de92007-09-25 17:54:57 -07007715 return -EAGAIN;
7716 return sprintf(buf, "0x%08x\n", (int)priv->status);
7717}
7718
7719static DEVICE_ATTR(status, S_IRUGO, show_status, NULL);
7720
7721static ssize_t dump_error_log(struct device *d,
7722 struct device_attribute *attr,
7723 const char *buf, size_t count)
7724{
7725 char *p = (char *)buf;
7726
7727 if (p[0] == '1')
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007728 iwl4965_dump_nic_error_log((struct iwl_priv *)d->driver_data);
Zhu Yib481de92007-09-25 17:54:57 -07007729
7730 return strnlen(buf, count);
7731}
7732
7733static DEVICE_ATTR(dump_errors, S_IWUSR, NULL, dump_error_log);
7734
7735static ssize_t dump_event_log(struct device *d,
7736 struct device_attribute *attr,
7737 const char *buf, size_t count)
7738{
7739 char *p = (char *)buf;
7740
7741 if (p[0] == '1')
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007742 iwl4965_dump_nic_event_log((struct iwl_priv *)d->driver_data);
Zhu Yib481de92007-09-25 17:54:57 -07007743
7744 return strnlen(buf, count);
7745}
7746
7747static DEVICE_ATTR(dump_events, S_IWUSR, NULL, dump_event_log);
7748
7749/*****************************************************************************
7750 *
7751 * driver setup and teardown
7752 *
7753 *****************************************************************************/
7754
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007755static void iwl4965_setup_deferred_work(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07007756{
7757 priv->workqueue = create_workqueue(DRV_NAME);
7758
7759 init_waitqueue_head(&priv->wait_command_queue);
7760
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007761 INIT_WORK(&priv->up, iwl4965_bg_up);
7762 INIT_WORK(&priv->restart, iwl4965_bg_restart);
7763 INIT_WORK(&priv->rx_replenish, iwl4965_bg_rx_replenish);
7764 INIT_WORK(&priv->scan_completed, iwl4965_bg_scan_completed);
7765 INIT_WORK(&priv->request_scan, iwl4965_bg_request_scan);
7766 INIT_WORK(&priv->abort_scan, iwl4965_bg_abort_scan);
7767 INIT_WORK(&priv->rf_kill, iwl4965_bg_rf_kill);
7768 INIT_WORK(&priv->beacon_update, iwl4965_bg_beacon_update);
7769 INIT_DELAYED_WORK(&priv->post_associate, iwl4965_bg_post_associate);
7770 INIT_DELAYED_WORK(&priv->init_alive_start, iwl4965_bg_init_alive_start);
7771 INIT_DELAYED_WORK(&priv->alive_start, iwl4965_bg_alive_start);
7772 INIT_DELAYED_WORK(&priv->scan_check, iwl4965_bg_scan_check);
Zhu Yib481de92007-09-25 17:54:57 -07007773
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007774 iwl4965_hw_setup_deferred_work(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007775
7776 tasklet_init(&priv->irq_tasklet, (void (*)(unsigned long))
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007777 iwl4965_irq_tasklet, (unsigned long)priv);
Zhu Yib481de92007-09-25 17:54:57 -07007778}
7779
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007780static void iwl4965_cancel_deferred_work(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07007781{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007782 iwl4965_hw_cancel_deferred_work(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007783
Joonwoo Park3ae6a052007-11-29 10:43:16 +09007784 cancel_delayed_work_sync(&priv->init_alive_start);
Zhu Yib481de92007-09-25 17:54:57 -07007785 cancel_delayed_work(&priv->scan_check);
7786 cancel_delayed_work(&priv->alive_start);
7787 cancel_delayed_work(&priv->post_associate);
7788 cancel_work_sync(&priv->beacon_update);
7789}
7790
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007791static struct attribute *iwl4965_sysfs_entries[] = {
Zhu Yib481de92007-09-25 17:54:57 -07007792 &dev_attr_antenna.attr,
7793 &dev_attr_channels.attr,
7794 &dev_attr_dump_errors.attr,
7795 &dev_attr_dump_events.attr,
7796 &dev_attr_flags.attr,
7797 &dev_attr_filter_flags.attr,
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08007798#ifdef CONFIG_IWL4965_SPECTRUM_MEASUREMENT
Zhu Yib481de92007-09-25 17:54:57 -07007799 &dev_attr_measurement.attr,
7800#endif
7801 &dev_attr_power_level.attr,
7802 &dev_attr_retry_rate.attr,
Zhu Yib481de92007-09-25 17:54:57 -07007803 &dev_attr_rs_window.attr,
7804 &dev_attr_statistics.attr,
7805 &dev_attr_status.attr,
7806 &dev_attr_temperature.attr,
Zhu Yib481de92007-09-25 17:54:57 -07007807 &dev_attr_tx_power.attr,
7808
7809 NULL
7810};
7811
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007812static struct attribute_group iwl4965_attribute_group = {
Zhu Yib481de92007-09-25 17:54:57 -07007813 .name = NULL, /* put in device directory */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007814 .attrs = iwl4965_sysfs_entries,
Zhu Yib481de92007-09-25 17:54:57 -07007815};
7816
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007817static struct ieee80211_ops iwl4965_hw_ops = {
7818 .tx = iwl4965_mac_tx,
7819 .start = iwl4965_mac_start,
7820 .stop = iwl4965_mac_stop,
7821 .add_interface = iwl4965_mac_add_interface,
7822 .remove_interface = iwl4965_mac_remove_interface,
7823 .config = iwl4965_mac_config,
7824 .config_interface = iwl4965_mac_config_interface,
7825 .configure_filter = iwl4965_configure_filter,
7826 .set_key = iwl4965_mac_set_key,
Emmanuel Grumbachab885f82008-03-20 15:06:43 +02007827 .update_tkip_key = iwl4965_mac_update_tkip_key,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007828 .get_stats = iwl4965_mac_get_stats,
7829 .get_tx_stats = iwl4965_mac_get_tx_stats,
7830 .conf_tx = iwl4965_mac_conf_tx,
7831 .get_tsf = iwl4965_mac_get_tsf,
7832 .reset_tsf = iwl4965_mac_reset_tsf,
7833 .beacon_update = iwl4965_mac_beacon_update,
Johannes Berg471b3ef2007-12-28 14:32:58 +01007834 .bss_info_changed = iwl4965_bss_info_changed,
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08007835#ifdef CONFIG_IWL4965_HT
Ron Rindjunsky9ab46172007-12-25 17:00:38 +02007836 .ampdu_action = iwl4965_mac_ampdu_action,
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08007837#endif /* CONFIG_IWL4965_HT */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007838 .hw_scan = iwl4965_mac_hw_scan
Zhu Yib481de92007-09-25 17:54:57 -07007839};
7840
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007841static int iwl4965_pci_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
Zhu Yib481de92007-09-25 17:54:57 -07007842{
7843 int err = 0;
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007844 struct iwl_priv *priv;
Zhu Yib481de92007-09-25 17:54:57 -07007845 struct ieee80211_hw *hw;
Tomas Winkler82b9a122008-03-04 18:09:30 -08007846 struct iwl_cfg *cfg = (struct iwl_cfg *)(ent->driver_data);
Mohamed Abbas0359fac2008-03-28 16:21:08 -07007847 unsigned long flags;
Zhu Yi5a66926a2008-01-14 17:46:18 -08007848 DECLARE_MAC_BUF(mac);
Zhu Yib481de92007-09-25 17:54:57 -07007849
Assaf Krauss316c30d2008-03-14 10:38:46 -07007850 /************************
7851 * 1. Allocating HW data
7852 ************************/
7853
Cahill, Ben M6440adb2007-11-29 11:09:55 +08007854 /* Disabling hardware scan means that mac80211 will perform scans
7855 * "the hard way", rather than using device's scan. */
Assaf Krauss1ea87392008-03-18 14:57:50 -07007856 if (cfg->mod_params->disable_hw_scan) {
Zhu Yib481de92007-09-25 17:54:57 -07007857 IWL_DEBUG_INFO("Disabling hw_scan\n");
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007858 iwl4965_hw_ops.hw_scan = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07007859 }
7860
Assaf Krauss1d0a0822008-03-14 10:38:48 -07007861 hw = iwl_alloc_all(cfg, &iwl4965_hw_ops);
7862 if (!hw) {
Zhu Yib481de92007-09-25 17:54:57 -07007863 err = -ENOMEM;
7864 goto out;
7865 }
Assaf Krauss1d0a0822008-03-14 10:38:48 -07007866 priv = hw->priv;
7867 /* At this point both hw and priv are allocated. */
7868
Zhu Yib481de92007-09-25 17:54:57 -07007869 SET_IEEE80211_DEV(hw, &pdev->dev);
7870
7871 IWL_DEBUG_INFO("*** LOAD DRIVER ***\n");
Tomas Winkler82b9a122008-03-04 18:09:30 -08007872 priv->cfg = cfg;
Zhu Yib481de92007-09-25 17:54:57 -07007873 priv->pci_dev = pdev;
Assaf Krauss316c30d2008-03-14 10:38:46 -07007874
Tomas Winkler0a6857e2008-03-12 16:58:49 -07007875#ifdef CONFIG_IWLWIFI_DEBUG
Assaf Krauss1ea87392008-03-18 14:57:50 -07007876 iwl_debug_level = priv->cfg->mod_params->debug;
Zhu Yib481de92007-09-25 17:54:57 -07007877 atomic_set(&priv->restrict_refcnt, 0);
7878#endif
Zhu Yib481de92007-09-25 17:54:57 -07007879
Assaf Krauss316c30d2008-03-14 10:38:46 -07007880 /**************************
7881 * 2. Initializing PCI bus
7882 **************************/
7883 if (pci_enable_device(pdev)) {
7884 err = -ENODEV;
7885 goto out_ieee80211_free_hw;
7886 }
7887
7888 pci_set_master(pdev);
7889
7890 err = pci_set_dma_mask(pdev, DMA_32BIT_MASK);
7891 if (!err)
7892 err = pci_set_consistent_dma_mask(pdev, DMA_32BIT_MASK);
7893 if (err) {
7894 printk(KERN_WARNING DRV_NAME
7895 ": No suitable DMA available.\n");
7896 goto out_pci_disable_device;
7897 }
7898
7899 err = pci_request_regions(pdev, DRV_NAME);
7900 if (err)
7901 goto out_pci_disable_device;
7902
7903 pci_set_drvdata(pdev, priv);
7904
7905 /* We disable the RETRY_TIMEOUT register (0x41) to keep
7906 * PCI Tx retries from interfering with C3 CPU state */
7907 pci_write_config_byte(pdev, 0x41, 0x00);
7908
7909 /***********************
7910 * 3. Read REV register
7911 ***********************/
7912 priv->hw_base = pci_iomap(pdev, 0, 0);
7913 if (!priv->hw_base) {
7914 err = -ENODEV;
7915 goto out_pci_release_regions;
7916 }
7917
7918 IWL_DEBUG_INFO("pci_resource_len = 0x%08llx\n",
7919 (unsigned long long) pci_resource_len(pdev, 0));
7920 IWL_DEBUG_INFO("pci_resource_base = %p\n", priv->hw_base);
7921
7922 printk(KERN_INFO DRV_NAME
7923 ": Detected Intel Wireless WiFi Link %s\n", priv->cfg->name);
7924
7925 /*****************
7926 * 4. Read EEPROM
7927 *****************/
7928 /* nic init */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07007929 iwl_set_bit(priv, CSR_GIO_CHICKEN_BITS,
Assaf Krauss316c30d2008-03-14 10:38:46 -07007930 CSR_GIO_CHICKEN_BITS_REG_BIT_DIS_L0S_EXIT_TIMER);
7931
Tomas Winkler3395f6e2008-03-25 16:33:37 -07007932 iwl_set_bit(priv, CSR_GP_CNTRL, CSR_GP_CNTRL_REG_FLAG_INIT_DONE);
7933 err = iwl_poll_bit(priv, CSR_GP_CNTRL,
Assaf Krauss316c30d2008-03-14 10:38:46 -07007934 CSR_GP_CNTRL_REG_FLAG_MAC_CLOCK_READY,
7935 CSR_GP_CNTRL_REG_FLAG_MAC_CLOCK_READY, 25000);
7936 if (err < 0) {
7937 IWL_DEBUG_INFO("Failed to init the card\n");
7938 goto out_iounmap;
7939 }
7940 /* Read the EEPROM */
7941 err = iwl_eeprom_init(priv);
7942 if (err) {
7943 IWL_ERROR("Unable to init EEPROM\n");
7944 goto out_iounmap;
7945 }
7946 /* MAC Address location in EEPROM same for 3945/4965 */
7947 iwl_eeprom_get_mac(priv, priv->mac_addr);
7948 IWL_DEBUG_INFO("MAC address: %s\n", print_mac(mac, priv->mac_addr));
7949 SET_IEEE80211_PERM_ADDR(priv->hw, priv->mac_addr);
7950
7951 /************************
7952 * 5. Setup HW constants
7953 ************************/
7954 /* Device-specific setup */
7955 if (iwl4965_hw_set_hw_setting(priv)) {
7956 IWL_ERROR("failed to set hw settings\n");
7957 goto out_iounmap;
7958 }
7959
7960 /*******************
7961 * 6. Setup hw/priv
7962 *******************/
Zhu Yib481de92007-09-25 17:54:57 -07007963
Assaf Kraussbf85ea42008-03-14 10:38:49 -07007964 err = iwl_setup(priv);
7965 if (err)
Assaf Krauss316c30d2008-03-14 10:38:46 -07007966 goto out_unset_hw_settings;
Assaf Kraussbf85ea42008-03-14 10:38:49 -07007967 /* At this point both hw and priv are initialized. */
Assaf Krauss316c30d2008-03-14 10:38:46 -07007968
7969 /**********************************
7970 * 7. Initialize module parameters
7971 **********************************/
7972
7973 /* Disable radio (SW RF KILL) via parameter when loading driver */
Assaf Krauss1ea87392008-03-18 14:57:50 -07007974 if (priv->cfg->mod_params->disable) {
Assaf Krauss316c30d2008-03-14 10:38:46 -07007975 set_bit(STATUS_RF_KILL_SW, &priv->status);
7976 IWL_DEBUG_INFO("Radio disabled.\n");
7977 }
7978
Assaf Krauss1ea87392008-03-18 14:57:50 -07007979 if (priv->cfg->mod_params->enable_qos)
Assaf Krauss316c30d2008-03-14 10:38:46 -07007980 priv->qos_data.qos_enable = 1;
7981
7982 /********************
7983 * 8. Setup services
7984 ********************/
Mohamed Abbas0359fac2008-03-28 16:21:08 -07007985 spin_lock_irqsave(&priv->lock, flags);
Assaf Krauss316c30d2008-03-14 10:38:46 -07007986 iwl4965_disable_interrupts(priv);
Mohamed Abbas0359fac2008-03-28 16:21:08 -07007987 spin_unlock_irqrestore(&priv->lock, flags);
Assaf Krauss316c30d2008-03-14 10:38:46 -07007988
7989 err = sysfs_create_group(&pdev->dev.kobj, &iwl4965_attribute_group);
7990 if (err) {
7991 IWL_ERROR("failed to create sysfs device attributes\n");
Assaf Kraussbf85ea42008-03-14 10:38:49 -07007992 goto out_unset_hw_settings;
Assaf Krauss316c30d2008-03-14 10:38:46 -07007993 }
7994
7995 err = iwl_dbgfs_register(priv, DRV_NAME);
7996 if (err) {
7997 IWL_ERROR("failed to create debugfs files\n");
7998 goto out_remove_sysfs;
7999 }
8000
8001 iwl4965_setup_deferred_work(priv);
8002 iwl4965_setup_rx_handlers(priv);
8003
8004 /********************
8005 * 9. Conclude
8006 ********************/
Zhu Yi5a66926a2008-01-14 17:46:18 -08008007 pci_save_state(pdev);
8008 pci_disable_device(pdev);
Zhu Yib481de92007-09-25 17:54:57 -07008009
Mohamed Abbasc8381fd2008-03-28 16:21:05 -07008010 /* notify iwlcore to init */
8011 iwlcore_low_level_notify(priv, IWLCORE_INIT_EVT);
Zhu Yib481de92007-09-25 17:54:57 -07008012 return 0;
8013
Assaf Krauss316c30d2008-03-14 10:38:46 -07008014 out_remove_sysfs:
8015 sysfs_remove_group(&pdev->dev.kobj, &iwl4965_attribute_group);
Assaf Krauss316c30d2008-03-14 10:38:46 -07008016 out_unset_hw_settings:
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008017 iwl4965_unset_hw_setting(priv);
Zhu Yib481de92007-09-25 17:54:57 -07008018 out_iounmap:
8019 pci_iounmap(pdev, priv->hw_base);
8020 out_pci_release_regions:
8021 pci_release_regions(pdev);
Assaf Krauss316c30d2008-03-14 10:38:46 -07008022 pci_set_drvdata(pdev, NULL);
Zhu Yib481de92007-09-25 17:54:57 -07008023 out_pci_disable_device:
8024 pci_disable_device(pdev);
Zhu Yib481de92007-09-25 17:54:57 -07008025 out_ieee80211_free_hw:
8026 ieee80211_free_hw(priv->hw);
8027 out:
8028 return err;
8029}
8030
Reinette Chatrec83dbf62008-03-21 13:53:41 -07008031static void __devexit iwl4965_pci_remove(struct pci_dev *pdev)
Zhu Yib481de92007-09-25 17:54:57 -07008032{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07008033 struct iwl_priv *priv = pci_get_drvdata(pdev);
Zhu Yib481de92007-09-25 17:54:57 -07008034 struct list_head *p, *q;
8035 int i;
Mohamed Abbas0359fac2008-03-28 16:21:08 -07008036 unsigned long flags;
Zhu Yib481de92007-09-25 17:54:57 -07008037
8038 if (!priv)
8039 return;
8040
8041 IWL_DEBUG_INFO("*** UNLOAD DRIVER ***\n");
8042
Ron Rindjunskyc4f55232008-03-28 16:21:10 -07008043 if (priv->mac80211_registered) {
8044 ieee80211_unregister_hw(priv->hw);
8045 priv->mac80211_registered = 0;
8046 }
8047
Zhu Yib481de92007-09-25 17:54:57 -07008048 set_bit(STATUS_EXIT_PENDING, &priv->status);
Zhu Yib24d22b2007-12-19 13:59:52 +08008049
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008050 iwl4965_down(priv);
Zhu Yib481de92007-09-25 17:54:57 -07008051
Mohamed Abbas0359fac2008-03-28 16:21:08 -07008052 /* make sure we flush any pending irq or
8053 * tasklet for the driver
8054 */
8055 spin_lock_irqsave(&priv->lock, flags);
8056 iwl4965_disable_interrupts(priv);
8057 spin_unlock_irqrestore(&priv->lock, flags);
8058
8059 iwl_synchronize_irq(priv);
8060
Zhu Yib481de92007-09-25 17:54:57 -07008061 /* Free MAC hash list for ADHOC */
8062 for (i = 0; i < IWL_IBSS_MAC_HASH_SIZE; i++) {
8063 list_for_each_safe(p, q, &priv->ibss_mac_hash[i]) {
8064 list_del(p);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008065 kfree(list_entry(p, struct iwl4965_ibss_seq, list));
Zhu Yib481de92007-09-25 17:54:57 -07008066 }
8067 }
8068
Mohamed Abbasc8381fd2008-03-28 16:21:05 -07008069 iwlcore_low_level_notify(priv, IWLCORE_REMOVE_EVT);
Tomas Winkler712b6cf2008-03-12 16:58:52 -07008070 iwl_dbgfs_unregister(priv);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008071 sysfs_remove_group(&pdev->dev.kobj, &iwl4965_attribute_group);
Zhu Yib481de92007-09-25 17:54:57 -07008072
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008073 iwl4965_dealloc_ucode_pci(priv);
Zhu Yib481de92007-09-25 17:54:57 -07008074
8075 if (priv->rxq.bd)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008076 iwl4965_rx_queue_free(priv, &priv->rxq);
8077 iwl4965_hw_txq_ctx_free(priv);
Zhu Yib481de92007-09-25 17:54:57 -07008078
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008079 iwl4965_unset_hw_setting(priv);
Assaf Kraussbf85ea42008-03-14 10:38:49 -07008080 iwlcore_clear_stations_table(priv);
Zhu Yib481de92007-09-25 17:54:57 -07008081
Zhu Yib481de92007-09-25 17:54:57 -07008082
Mohamed Abbas948c1712007-10-25 17:15:45 +08008083 /*netif_stop_queue(dev); */
8084 flush_workqueue(priv->workqueue);
8085
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008086 /* ieee80211_unregister_hw calls iwl4965_mac_stop, which flushes
Zhu Yib481de92007-09-25 17:54:57 -07008087 * priv->workqueue... so we can't take down the workqueue
8088 * until now... */
8089 destroy_workqueue(priv->workqueue);
8090 priv->workqueue = NULL;
8091
Zhu Yib481de92007-09-25 17:54:57 -07008092 pci_iounmap(pdev, priv->hw_base);
8093 pci_release_regions(pdev);
8094 pci_disable_device(pdev);
8095 pci_set_drvdata(pdev, NULL);
8096
Assaf Kraussbf85ea42008-03-14 10:38:49 -07008097 iwl_free_channel_map(priv);
Reinette Chatre849e0dc2008-01-23 10:15:18 -08008098 iwl4965_free_geos(priv);
Zhu Yib481de92007-09-25 17:54:57 -07008099
8100 if (priv->ibss_beacon)
8101 dev_kfree_skb(priv->ibss_beacon);
8102
8103 ieee80211_free_hw(priv->hw);
8104}
8105
8106#ifdef CONFIG_PM
8107
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008108static int iwl4965_pci_suspend(struct pci_dev *pdev, pm_message_t state)
Zhu Yib481de92007-09-25 17:54:57 -07008109{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07008110 struct iwl_priv *priv = pci_get_drvdata(pdev);
Zhu Yib481de92007-09-25 17:54:57 -07008111
Zhu Yie655b9f2008-01-24 02:19:38 -08008112 if (priv->is_open) {
8113 set_bit(STATUS_IN_SUSPEND, &priv->status);
8114 iwl4965_mac_stop(priv->hw);
8115 priv->is_open = 1;
8116 }
Zhu Yib481de92007-09-25 17:54:57 -07008117
Zhu Yib481de92007-09-25 17:54:57 -07008118 pci_set_power_state(pdev, PCI_D3hot);
8119
Zhu Yib481de92007-09-25 17:54:57 -07008120 return 0;
8121}
8122
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008123static int iwl4965_pci_resume(struct pci_dev *pdev)
Zhu Yib481de92007-09-25 17:54:57 -07008124{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07008125 struct iwl_priv *priv = pci_get_drvdata(pdev);
Zhu Yib481de92007-09-25 17:54:57 -07008126
Zhu Yib481de92007-09-25 17:54:57 -07008127 pci_set_power_state(pdev, PCI_D0);
Zhu Yib481de92007-09-25 17:54:57 -07008128
Zhu Yie655b9f2008-01-24 02:19:38 -08008129 if (priv->is_open)
8130 iwl4965_mac_start(priv->hw);
Zhu Yib481de92007-09-25 17:54:57 -07008131
Zhu Yie655b9f2008-01-24 02:19:38 -08008132 clear_bit(STATUS_IN_SUSPEND, &priv->status);
Zhu Yib481de92007-09-25 17:54:57 -07008133 return 0;
8134}
8135
8136#endif /* CONFIG_PM */
8137
8138/*****************************************************************************
8139 *
8140 * driver and module entry point
8141 *
8142 *****************************************************************************/
8143
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008144static struct pci_driver iwl4965_driver = {
Zhu Yib481de92007-09-25 17:54:57 -07008145 .name = DRV_NAME,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008146 .id_table = iwl4965_hw_card_ids,
8147 .probe = iwl4965_pci_probe,
8148 .remove = __devexit_p(iwl4965_pci_remove),
Zhu Yib481de92007-09-25 17:54:57 -07008149#ifdef CONFIG_PM
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008150 .suspend = iwl4965_pci_suspend,
8151 .resume = iwl4965_pci_resume,
Zhu Yib481de92007-09-25 17:54:57 -07008152#endif
8153};
8154
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008155static int __init iwl4965_init(void)
Zhu Yib481de92007-09-25 17:54:57 -07008156{
8157
8158 int ret;
8159 printk(KERN_INFO DRV_NAME ": " DRV_DESCRIPTION ", " DRV_VERSION "\n");
8160 printk(KERN_INFO DRV_NAME ": " DRV_COPYRIGHT "\n");
Reinette Chatre897e1cf2008-03-28 16:21:09 -07008161
8162 ret = iwl4965_rate_control_register();
8163 if (ret) {
8164 IWL_ERROR("Unable to register rate control algorithm: %d\n", ret);
8165 return ret;
8166 }
8167
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008168 ret = pci_register_driver(&iwl4965_driver);
Zhu Yib481de92007-09-25 17:54:57 -07008169 if (ret) {
8170 IWL_ERROR("Unable to initialize PCI module\n");
Reinette Chatre897e1cf2008-03-28 16:21:09 -07008171 goto error_register;
Zhu Yib481de92007-09-25 17:54:57 -07008172 }
Tomas Winkler0a6857e2008-03-12 16:58:49 -07008173#ifdef CONFIG_IWLWIFI_DEBUG
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008174 ret = driver_create_file(&iwl4965_driver.driver, &driver_attr_debug_level);
Zhu Yib481de92007-09-25 17:54:57 -07008175 if (ret) {
8176 IWL_ERROR("Unable to create driver sysfs file\n");
Reinette Chatre897e1cf2008-03-28 16:21:09 -07008177 goto error_debug;
Zhu Yib481de92007-09-25 17:54:57 -07008178 }
8179#endif
8180
8181 return ret;
Reinette Chatre897e1cf2008-03-28 16:21:09 -07008182
8183#ifdef CONFIG_IWLWIFI_DEBUG
8184error_debug:
8185 pci_unregister_driver(&iwl4965_driver);
8186#endif
8187error_register:
8188 iwl4965_rate_control_unregister();
8189 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07008190}
8191
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008192static void __exit iwl4965_exit(void)
Zhu Yib481de92007-09-25 17:54:57 -07008193{
Tomas Winkler0a6857e2008-03-12 16:58:49 -07008194#ifdef CONFIG_IWLWIFI_DEBUG
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008195 driver_remove_file(&iwl4965_driver.driver, &driver_attr_debug_level);
Zhu Yib481de92007-09-25 17:54:57 -07008196#endif
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008197 pci_unregister_driver(&iwl4965_driver);
Reinette Chatre897e1cf2008-03-28 16:21:09 -07008198 iwl4965_rate_control_unregister();
Zhu Yib481de92007-09-25 17:54:57 -07008199}
8200
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008201module_exit(iwl4965_exit);
8202module_init(iwl4965_init);