blob: a8fa1bfa570b22ab529bbf1919e6aaf643f5c18a [file] [log] [blame]
Zhu Yib481de92007-09-25 17:54:57 -07001/******************************************************************************
2 *
Reinette Chatreeb7ae892008-03-11 16:17:17 -07003 * Copyright(c) 2003 - 2008 Intel Corporation. All rights reserved.
Zhu Yib481de92007-09-25 17:54:57 -07004 *
5 * Portions of this file are derived from the ipw3945 project, as well
6 * as portions of the ieee80211 subsystem header files.
7 *
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of version 2 of the GNU General Public License as
10 * published by the Free Software Foundation.
11 *
12 * This program is distributed in the hope that it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 * more details.
16 *
17 * You should have received a copy of the GNU General Public License along with
18 * this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
20 *
21 * The full GNU General Public License is included in this distribution in the
22 * file called LICENSE.
23 *
24 * Contact Information:
25 * James P. Ketrenos <ipw2100-admin@linux.intel.com>
26 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
27 *
28 *****************************************************************************/
29
Zhu Yib481de92007-09-25 17:54:57 -070030#include <linux/kernel.h>
31#include <linux/module.h>
32#include <linux/version.h>
33#include <linux/init.h>
34#include <linux/pci.h>
35#include <linux/dma-mapping.h>
36#include <linux/delay.h>
37#include <linux/skbuff.h>
38#include <linux/netdevice.h>
39#include <linux/wireless.h>
40#include <linux/firmware.h>
Zhu Yib481de92007-09-25 17:54:57 -070041#include <linux/etherdevice.h>
42#include <linux/if_arp.h>
43
Zhu Yib481de92007-09-25 17:54:57 -070044#include <net/mac80211.h>
45
46#include <asm/div64.h>
47
Assaf Krauss6bc913b2008-03-11 16:17:18 -070048#include "iwl-eeprom.h"
Tomas Winkler82b9a122008-03-04 18:09:30 -080049#include "iwl-core.h"
Zhu Yib481de92007-09-25 17:54:57 -070050#include "iwl-4965.h"
51#include "iwl-helpers.h"
52
Tomas Winklerc79dd5b2008-03-12 16:58:50 -070053static int iwl4965_tx_queue_update_write_ptr(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -080054 struct iwl4965_tx_queue *txq);
Christoph Hellwig416e1432007-10-25 17:15:49 +080055
Zhu Yib481de92007-09-25 17:54:57 -070056/******************************************************************************
57 *
58 * module boiler plate
59 *
60 ******************************************************************************/
61
62/* module parameters */
Assaf Krauss00acbc92008-03-14 10:38:47 -070063struct iwl_mod_params iwl4965_mod_params = {
64 .num_of_queues = IWL_MAX_NUM_QUEUES,
65 .enable_qos = 1,
66 .amsdu_size_8K = 1,
67 /* the rest are 0 by default */
68};
Zhu Yib481de92007-09-25 17:54:57 -070069
70/*
71 * module name, copyright, version, etc.
72 * NOTE: DRV_NAME is defined in iwlwifi.h for use by iwl-debug.h and printk
73 */
74
75#define DRV_DESCRIPTION "Intel(R) Wireless WiFi Link 4965AGN driver for Linux"
76
Tomas Winkler0a6857e2008-03-12 16:58:49 -070077#ifdef CONFIG_IWLWIFI_DEBUG
Zhu Yib481de92007-09-25 17:54:57 -070078#define VD "d"
79#else
80#define VD
81#endif
82
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +080083#ifdef CONFIG_IWL4965_SPECTRUM_MEASUREMENT
Zhu Yib481de92007-09-25 17:54:57 -070084#define VS "s"
85#else
86#define VS
87#endif
88
Tomas Winklerdf48c322008-03-06 10:40:19 -080089#define DRV_VERSION IWLWIFI_VERSION VD VS
Zhu Yib481de92007-09-25 17:54:57 -070090
Zhu Yib481de92007-09-25 17:54:57 -070091
92MODULE_DESCRIPTION(DRV_DESCRIPTION);
93MODULE_VERSION(DRV_VERSION);
94MODULE_AUTHOR(DRV_COPYRIGHT);
95MODULE_LICENSE("GPL");
96
97__le16 *ieee80211_get_qos_ctrl(struct ieee80211_hdr *hdr)
98{
99 u16 fc = le16_to_cpu(hdr->frame_control);
100 int hdr_len = ieee80211_get_hdrlen(fc);
101
102 if ((fc & 0x00cc) == (IEEE80211_STYPE_QOS_DATA | IEEE80211_FTYPE_DATA))
103 return (__le16 *) ((u8 *) hdr + hdr_len - QOS_CONTROL_LEN);
104 return NULL;
105}
106
Johannes Berg8318d782008-01-24 19:38:38 +0100107static const struct ieee80211_supported_band *iwl4965_get_hw_mode(
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700108 struct iwl_priv *priv, enum ieee80211_band band)
Zhu Yib481de92007-09-25 17:54:57 -0700109{
Johannes Berg8318d782008-01-24 19:38:38 +0100110 return priv->hw->wiphy->bands[band];
Zhu Yib481de92007-09-25 17:54:57 -0700111}
112
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800113static int iwl4965_is_empty_essid(const char *essid, int essid_len)
Zhu Yib481de92007-09-25 17:54:57 -0700114{
115 /* Single white space is for Linksys APs */
116 if (essid_len == 1 && essid[0] == ' ')
117 return 1;
118
119 /* Otherwise, if the entire essid is 0, we assume it is hidden */
120 while (essid_len) {
121 essid_len--;
122 if (essid[essid_len] != '\0')
123 return 0;
124 }
125
126 return 1;
127}
128
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800129static const char *iwl4965_escape_essid(const char *essid, u8 essid_len)
Zhu Yib481de92007-09-25 17:54:57 -0700130{
131 static char escaped[IW_ESSID_MAX_SIZE * 2 + 1];
132 const char *s = essid;
133 char *d = escaped;
134
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800135 if (iwl4965_is_empty_essid(essid, essid_len)) {
Zhu Yib481de92007-09-25 17:54:57 -0700136 memcpy(escaped, "<hidden>", sizeof("<hidden>"));
137 return escaped;
138 }
139
140 essid_len = min(essid_len, (u8) IW_ESSID_MAX_SIZE);
141 while (essid_len--) {
142 if (*s == '\0') {
143 *d++ = '\\';
144 *d++ = '0';
145 s++;
146 } else
147 *d++ = *s++;
148 }
149 *d = '\0';
150 return escaped;
151}
152
Zhu Yib481de92007-09-25 17:54:57 -0700153/*************** DMA-QUEUE-GENERAL-FUNCTIONS *****
154 * DMA services
155 *
156 * Theory of operation
157 *
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800158 * A Tx or Rx queue resides in host DRAM, and is comprised of a circular buffer
159 * of buffer descriptors, each of which points to one or more data buffers for
160 * the device to read from or fill. Driver and device exchange status of each
161 * queue via "read" and "write" pointers. Driver keeps minimum of 2 empty
162 * entries in each circular buffer, to protect against confusing empty and full
163 * queue states.
164 *
165 * The device reads or writes the data in the queues via the device's several
166 * DMA/FIFO channels. Each queue is mapped to a single DMA channel.
Zhu Yib481de92007-09-25 17:54:57 -0700167 *
168 * For Tx queue, there are low mark and high mark limits. If, after queuing
169 * the packet for Tx, free space become < low mark, Tx queue stopped. When
170 * reclaiming packets (on 'tx done IRQ), if free space become > high mark,
171 * Tx queue resumed.
172 *
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800173 * The 4965 operates with up to 17 queues: One receive queue, one transmit
174 * queue (#4) for sending commands to the device firmware, and 15 other
175 * Tx queues that may be mapped to prioritized Tx DMA/FIFO channels.
Ben Cahille3851442007-11-29 11:10:07 +0800176 *
177 * See more detailed info in iwl-4965-hw.h.
Zhu Yib481de92007-09-25 17:54:57 -0700178 ***************************************************/
179
Ron Rindjunskyfe01b472008-01-28 14:07:24 +0200180int iwl4965_queue_space(const struct iwl4965_queue *q)
Zhu Yib481de92007-09-25 17:54:57 -0700181{
Tomas Winklerfc4b6852007-10-25 17:15:24 +0800182 int s = q->read_ptr - q->write_ptr;
Zhu Yib481de92007-09-25 17:54:57 -0700183
Tomas Winklerfc4b6852007-10-25 17:15:24 +0800184 if (q->read_ptr > q->write_ptr)
Zhu Yib481de92007-09-25 17:54:57 -0700185 s -= q->n_bd;
186
187 if (s <= 0)
188 s += q->n_window;
189 /* keep some reserve to not confuse empty and full situations */
190 s -= 2;
191 if (s < 0)
192 s = 0;
193 return s;
194}
195
Zhu Yib481de92007-09-25 17:54:57 -0700196
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800197static inline int x2_queue_used(const struct iwl4965_queue *q, int i)
Zhu Yib481de92007-09-25 17:54:57 -0700198{
Tomas Winklerfc4b6852007-10-25 17:15:24 +0800199 return q->write_ptr > q->read_ptr ?
200 (i >= q->read_ptr && i < q->write_ptr) :
201 !(i < q->read_ptr && i >= q->write_ptr);
Zhu Yib481de92007-09-25 17:54:57 -0700202}
203
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800204static inline u8 get_cmd_index(struct iwl4965_queue *q, u32 index, int is_huge)
Zhu Yib481de92007-09-25 17:54:57 -0700205{
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800206 /* This is for scan command, the big buffer at end of command array */
Zhu Yib481de92007-09-25 17:54:57 -0700207 if (is_huge)
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800208 return q->n_window; /* must be power of 2 */
Zhu Yib481de92007-09-25 17:54:57 -0700209
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800210 /* Otherwise, use normal size buffers */
Zhu Yib481de92007-09-25 17:54:57 -0700211 return index & (q->n_window - 1);
212}
213
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800214/**
215 * iwl4965_queue_init - Initialize queue's high/low-water and read/write indexes
216 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700217static int iwl4965_queue_init(struct iwl_priv *priv, struct iwl4965_queue *q,
Zhu Yib481de92007-09-25 17:54:57 -0700218 int count, int slots_num, u32 id)
219{
220 q->n_bd = count;
221 q->n_window = slots_num;
222 q->id = id;
223
Tomas Winklerc54b6792008-03-06 17:36:53 -0800224 /* count must be power-of-two size, otherwise iwl_queue_inc_wrap
225 * and iwl_queue_dec_wrap are broken. */
Zhu Yib481de92007-09-25 17:54:57 -0700226 BUG_ON(!is_power_of_2(count));
227
228 /* slots_num must be power-of-two size, otherwise
229 * get_cmd_index is broken. */
230 BUG_ON(!is_power_of_2(slots_num));
231
232 q->low_mark = q->n_window / 4;
233 if (q->low_mark < 4)
234 q->low_mark = 4;
235
236 q->high_mark = q->n_window / 8;
237 if (q->high_mark < 2)
238 q->high_mark = 2;
239
Tomas Winklerfc4b6852007-10-25 17:15:24 +0800240 q->write_ptr = q->read_ptr = 0;
Zhu Yib481de92007-09-25 17:54:57 -0700241
242 return 0;
243}
244
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800245/**
246 * iwl4965_tx_queue_alloc - Alloc driver data and TFD CB for one Tx/cmd queue
247 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700248static int iwl4965_tx_queue_alloc(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800249 struct iwl4965_tx_queue *txq, u32 id)
Zhu Yib481de92007-09-25 17:54:57 -0700250{
251 struct pci_dev *dev = priv->pci_dev;
252
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800253 /* Driver private data, only for Tx (not command) queues,
254 * not shared with device. */
Zhu Yib481de92007-09-25 17:54:57 -0700255 if (id != IWL_CMD_QUEUE_NUM) {
256 txq->txb = kmalloc(sizeof(txq->txb[0]) *
257 TFD_QUEUE_SIZE_MAX, GFP_KERNEL);
258 if (!txq->txb) {
Ian Schram01ebd062007-10-25 17:15:22 +0800259 IWL_ERROR("kmalloc for auxiliary BD "
Zhu Yib481de92007-09-25 17:54:57 -0700260 "structures failed\n");
261 goto error;
262 }
263 } else
264 txq->txb = NULL;
265
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800266 /* Circular buffer of transmit frame descriptors (TFDs),
267 * shared with device */
Zhu Yib481de92007-09-25 17:54:57 -0700268 txq->bd = pci_alloc_consistent(dev,
269 sizeof(txq->bd[0]) * TFD_QUEUE_SIZE_MAX,
270 &txq->q.dma_addr);
271
272 if (!txq->bd) {
273 IWL_ERROR("pci_alloc_consistent(%zd) failed\n",
274 sizeof(txq->bd[0]) * TFD_QUEUE_SIZE_MAX);
275 goto error;
276 }
277 txq->q.id = id;
278
279 return 0;
280
281 error:
282 if (txq->txb) {
283 kfree(txq->txb);
284 txq->txb = NULL;
285 }
286
287 return -ENOMEM;
288}
289
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +0800290/**
291 * iwl4965_tx_queue_init - Allocate and initialize one tx/cmd queue
292 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700293int iwl4965_tx_queue_init(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800294 struct iwl4965_tx_queue *txq, int slots_num, u32 txq_id)
Zhu Yib481de92007-09-25 17:54:57 -0700295{
296 struct pci_dev *dev = priv->pci_dev;
297 int len;
298 int rc = 0;
299
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +0800300 /*
301 * Alloc buffer array for commands (Tx or other types of commands).
302 * For the command queue (#4), allocate command space + one big
303 * command for scan, since scan command is very huge; the system will
304 * not have two scans at the same time, so only one is needed.
Tomas Winklerbb542442007-12-05 20:59:59 +0200305 * For normal Tx queues (all other queues), no super-size command
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +0800306 * space is needed.
307 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800308 len = sizeof(struct iwl4965_cmd) * slots_num;
Zhu Yib481de92007-09-25 17:54:57 -0700309 if (txq_id == IWL_CMD_QUEUE_NUM)
310 len += IWL_MAX_SCAN_SIZE;
311 txq->cmd = pci_alloc_consistent(dev, len, &txq->dma_addr_cmd);
312 if (!txq->cmd)
313 return -ENOMEM;
314
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +0800315 /* Alloc driver data array and TFD circular buffer */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800316 rc = iwl4965_tx_queue_alloc(priv, txq, txq_id);
Zhu Yib481de92007-09-25 17:54:57 -0700317 if (rc) {
318 pci_free_consistent(dev, len, txq->cmd, txq->dma_addr_cmd);
319
320 return -ENOMEM;
321 }
322 txq->need_update = 0;
323
324 /* TFD_QUEUE_SIZE_MAX must be power-of-two size, otherwise
Tomas Winklerc54b6792008-03-06 17:36:53 -0800325 * iwl_queue_inc_wrap and iwl_queue_dec_wrap are broken. */
Zhu Yib481de92007-09-25 17:54:57 -0700326 BUILD_BUG_ON(TFD_QUEUE_SIZE_MAX & (TFD_QUEUE_SIZE_MAX - 1));
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +0800327
328 /* Initialize queue's high/low-water marks, and head/tail indexes */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800329 iwl4965_queue_init(priv, &txq->q, TFD_QUEUE_SIZE_MAX, slots_num, txq_id);
Zhu Yib481de92007-09-25 17:54:57 -0700330
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +0800331 /* Tell device where to find queue */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800332 iwl4965_hw_tx_queue_init(priv, txq);
Zhu Yib481de92007-09-25 17:54:57 -0700333
334 return 0;
335}
336
337/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800338 * iwl4965_tx_queue_free - Deallocate DMA queue.
Zhu Yib481de92007-09-25 17:54:57 -0700339 * @txq: Transmit queue to deallocate.
340 *
341 * Empty queue by removing and destroying all BD's.
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800342 * Free all buffers.
343 * 0-fill, but do not free "txq" descriptor structure.
Zhu Yib481de92007-09-25 17:54:57 -0700344 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700345void iwl4965_tx_queue_free(struct iwl_priv *priv, struct iwl4965_tx_queue *txq)
Zhu Yib481de92007-09-25 17:54:57 -0700346{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800347 struct iwl4965_queue *q = &txq->q;
Zhu Yib481de92007-09-25 17:54:57 -0700348 struct pci_dev *dev = priv->pci_dev;
349 int len;
350
351 if (q->n_bd == 0)
352 return;
353
354 /* first, empty all BD's */
Tomas Winklerfc4b6852007-10-25 17:15:24 +0800355 for (; q->write_ptr != q->read_ptr;
Tomas Winklerc54b6792008-03-06 17:36:53 -0800356 q->read_ptr = iwl_queue_inc_wrap(q->read_ptr, q->n_bd))
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800357 iwl4965_hw_txq_free_tfd(priv, txq);
Zhu Yib481de92007-09-25 17:54:57 -0700358
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800359 len = sizeof(struct iwl4965_cmd) * q->n_window;
Zhu Yib481de92007-09-25 17:54:57 -0700360 if (q->id == IWL_CMD_QUEUE_NUM)
361 len += IWL_MAX_SCAN_SIZE;
362
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800363 /* De-alloc array of command/tx buffers */
Zhu Yib481de92007-09-25 17:54:57 -0700364 pci_free_consistent(dev, len, txq->cmd, txq->dma_addr_cmd);
365
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800366 /* De-alloc circular buffer of TFDs */
Zhu Yib481de92007-09-25 17:54:57 -0700367 if (txq->q.n_bd)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800368 pci_free_consistent(dev, sizeof(struct iwl4965_tfd_frame) *
Zhu Yib481de92007-09-25 17:54:57 -0700369 txq->q.n_bd, txq->bd, txq->q.dma_addr);
370
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800371 /* De-alloc array of per-TFD driver data */
Zhu Yib481de92007-09-25 17:54:57 -0700372 if (txq->txb) {
373 kfree(txq->txb);
374 txq->txb = NULL;
375 }
376
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800377 /* 0-fill queue descriptor structure */
Zhu Yib481de92007-09-25 17:54:57 -0700378 memset(txq, 0, sizeof(*txq));
379}
380
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800381const u8 iwl4965_broadcast_addr[ETH_ALEN] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
Zhu Yib481de92007-09-25 17:54:57 -0700382
383/*************** STATION TABLE MANAGEMENT ****
Ben Cahill9fbab512007-11-29 11:09:47 +0800384 * mac80211 should be examined to determine if sta_info is duplicating
Zhu Yib481de92007-09-25 17:54:57 -0700385 * the functionality provided here
386 */
387
388/**************************************************************/
389
Ian Schram01ebd062007-10-25 17:15:22 +0800390#if 0 /* temporary disable till we add real remove station */
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800391/**
392 * iwl4965_remove_station - Remove driver's knowledge of station.
393 *
394 * NOTE: This does not remove station from device's station table.
395 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700396static u8 iwl4965_remove_station(struct iwl_priv *priv, const u8 *addr, int is_ap)
Zhu Yib481de92007-09-25 17:54:57 -0700397{
398 int index = IWL_INVALID_STATION;
399 int i;
400 unsigned long flags;
401
402 spin_lock_irqsave(&priv->sta_lock, flags);
403
404 if (is_ap)
405 index = IWL_AP_ID;
406 else if (is_broadcast_ether_addr(addr))
407 index = priv->hw_setting.bcast_sta_id;
408 else
409 for (i = IWL_STA_ID; i < priv->hw_setting.max_stations; i++)
410 if (priv->stations[i].used &&
411 !compare_ether_addr(priv->stations[i].sta.sta.addr,
412 addr)) {
413 index = i;
414 break;
415 }
416
417 if (unlikely(index == IWL_INVALID_STATION))
418 goto out;
419
420 if (priv->stations[index].used) {
421 priv->stations[index].used = 0;
422 priv->num_stations--;
423 }
424
425 BUG_ON(priv->num_stations < 0);
426
427out:
428 spin_unlock_irqrestore(&priv->sta_lock, flags);
429 return 0;
430}
Zhu Yi556f8db2007-09-27 11:27:33 +0800431#endif
Zhu Yib481de92007-09-25 17:54:57 -0700432
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800433/**
434 * iwl4965_clear_stations_table - Clear the driver's station table
435 *
436 * NOTE: This does not clear or otherwise alter the device's station table.
437 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700438static void iwl4965_clear_stations_table(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700439{
440 unsigned long flags;
441
442 spin_lock_irqsave(&priv->sta_lock, flags);
443
444 priv->num_stations = 0;
445 memset(priv->stations, 0, sizeof(priv->stations));
446
447 spin_unlock_irqrestore(&priv->sta_lock, flags);
448}
449
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800450/**
451 * iwl4965_add_station_flags - Add station to tables in driver and device
452 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700453u8 iwl4965_add_station_flags(struct iwl_priv *priv, const u8 *addr,
Ron Rindjunsky67d62032007-11-26 16:14:40 +0200454 int is_ap, u8 flags, void *ht_data)
Zhu Yib481de92007-09-25 17:54:57 -0700455{
456 int i;
457 int index = IWL_INVALID_STATION;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800458 struct iwl4965_station_entry *station;
Zhu Yib481de92007-09-25 17:54:57 -0700459 unsigned long flags_spin;
Joe Perches0795af52007-10-03 17:59:30 -0700460 DECLARE_MAC_BUF(mac);
Zhu Yib481de92007-09-25 17:54:57 -0700461
462 spin_lock_irqsave(&priv->sta_lock, flags_spin);
463 if (is_ap)
464 index = IWL_AP_ID;
465 else if (is_broadcast_ether_addr(addr))
466 index = priv->hw_setting.bcast_sta_id;
467 else
468 for (i = IWL_STA_ID; i < priv->hw_setting.max_stations; i++) {
469 if (!compare_ether_addr(priv->stations[i].sta.sta.addr,
470 addr)) {
471 index = i;
472 break;
473 }
474
475 if (!priv->stations[i].used &&
476 index == IWL_INVALID_STATION)
477 index = i;
478 }
479
480
Ben Cahill9fbab512007-11-29 11:09:47 +0800481 /* These two conditions have the same outcome, but keep them separate
482 since they have different meanings */
Zhu Yib481de92007-09-25 17:54:57 -0700483 if (unlikely(index == IWL_INVALID_STATION)) {
484 spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
485 return index;
486 }
487
488 if (priv->stations[index].used &&
489 !compare_ether_addr(priv->stations[index].sta.sta.addr, addr)) {
490 spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
491 return index;
492 }
493
494
Joe Perches0795af52007-10-03 17:59:30 -0700495 IWL_DEBUG_ASSOC("Add STA ID %d: %s\n", index, print_mac(mac, addr));
Zhu Yib481de92007-09-25 17:54:57 -0700496 station = &priv->stations[index];
497 station->used = 1;
498 priv->num_stations++;
499
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800500 /* Set up the REPLY_ADD_STA command to send to device */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800501 memset(&station->sta, 0, sizeof(struct iwl4965_addsta_cmd));
Zhu Yib481de92007-09-25 17:54:57 -0700502 memcpy(station->sta.sta.addr, addr, ETH_ALEN);
503 station->sta.mode = 0;
504 station->sta.sta.sta_id = index;
505 station->sta.station_flags = 0;
506
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +0800507#ifdef CONFIG_IWL4965_HT
Zhu Yib481de92007-09-25 17:54:57 -0700508 /* BCAST station and IBSS stations do not work in HT mode */
509 if (index != priv->hw_setting.bcast_sta_id &&
510 priv->iw_mode != IEEE80211_IF_TYPE_IBSS)
Ron Rindjunsky67d62032007-11-26 16:14:40 +0200511 iwl4965_set_ht_add_station(priv, index,
512 (struct ieee80211_ht_info *) ht_data);
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +0800513#endif /*CONFIG_IWL4965_HT*/
Zhu Yib481de92007-09-25 17:54:57 -0700514
515 spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800516
517 /* Add station to device's station table */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800518 iwl4965_send_add_station(priv, &station->sta, flags);
Zhu Yib481de92007-09-25 17:54:57 -0700519 return index;
520
521}
522
523/*************** DRIVER STATUS FUNCTIONS *****/
524
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700525static inline int iwl4965_is_ready(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700526{
527 /* The adapter is 'ready' if READY and GEO_CONFIGURED bits are
528 * set but EXIT_PENDING is not */
529 return test_bit(STATUS_READY, &priv->status) &&
530 test_bit(STATUS_GEO_CONFIGURED, &priv->status) &&
531 !test_bit(STATUS_EXIT_PENDING, &priv->status);
532}
533
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700534static inline int iwl4965_is_alive(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700535{
536 return test_bit(STATUS_ALIVE, &priv->status);
537}
538
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700539static inline int iwl4965_is_init(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700540{
541 return test_bit(STATUS_INIT, &priv->status);
542}
543
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700544static inline int iwl4965_is_rfkill(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700545{
546 return test_bit(STATUS_RF_KILL_HW, &priv->status) ||
547 test_bit(STATUS_RF_KILL_SW, &priv->status);
548}
549
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700550static inline int iwl4965_is_ready_rf(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700551{
552
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800553 if (iwl4965_is_rfkill(priv))
Zhu Yib481de92007-09-25 17:54:57 -0700554 return 0;
555
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800556 return iwl4965_is_ready(priv);
Zhu Yib481de92007-09-25 17:54:57 -0700557}
558
559/*************** HOST COMMAND QUEUE FUNCTIONS *****/
560
561#define IWL_CMD(x) case x : return #x
562
563static const char *get_cmd_string(u8 cmd)
564{
565 switch (cmd) {
566 IWL_CMD(REPLY_ALIVE);
567 IWL_CMD(REPLY_ERROR);
568 IWL_CMD(REPLY_RXON);
569 IWL_CMD(REPLY_RXON_ASSOC);
570 IWL_CMD(REPLY_QOS_PARAM);
571 IWL_CMD(REPLY_RXON_TIMING);
572 IWL_CMD(REPLY_ADD_STA);
573 IWL_CMD(REPLY_REMOVE_STA);
574 IWL_CMD(REPLY_REMOVE_ALL_STA);
575 IWL_CMD(REPLY_TX);
576 IWL_CMD(REPLY_RATE_SCALE);
577 IWL_CMD(REPLY_LEDS_CMD);
578 IWL_CMD(REPLY_TX_LINK_QUALITY_CMD);
579 IWL_CMD(RADAR_NOTIFICATION);
580 IWL_CMD(REPLY_QUIET_CMD);
581 IWL_CMD(REPLY_CHANNEL_SWITCH);
582 IWL_CMD(CHANNEL_SWITCH_NOTIFICATION);
583 IWL_CMD(REPLY_SPECTRUM_MEASUREMENT_CMD);
584 IWL_CMD(SPECTRUM_MEASURE_NOTIFICATION);
585 IWL_CMD(POWER_TABLE_CMD);
586 IWL_CMD(PM_SLEEP_NOTIFICATION);
587 IWL_CMD(PM_DEBUG_STATISTIC_NOTIFIC);
588 IWL_CMD(REPLY_SCAN_CMD);
589 IWL_CMD(REPLY_SCAN_ABORT_CMD);
590 IWL_CMD(SCAN_START_NOTIFICATION);
591 IWL_CMD(SCAN_RESULTS_NOTIFICATION);
592 IWL_CMD(SCAN_COMPLETE_NOTIFICATION);
593 IWL_CMD(BEACON_NOTIFICATION);
594 IWL_CMD(REPLY_TX_BEACON);
595 IWL_CMD(WHO_IS_AWAKE_NOTIFICATION);
596 IWL_CMD(QUIET_NOTIFICATION);
597 IWL_CMD(REPLY_TX_PWR_TABLE_CMD);
598 IWL_CMD(MEASURE_ABORT_NOTIFICATION);
599 IWL_CMD(REPLY_BT_CONFIG);
600 IWL_CMD(REPLY_STATISTICS_CMD);
601 IWL_CMD(STATISTICS_NOTIFICATION);
602 IWL_CMD(REPLY_CARD_STATE_CMD);
603 IWL_CMD(CARD_STATE_NOTIFICATION);
604 IWL_CMD(MISSED_BEACONS_NOTIFICATION);
605 IWL_CMD(REPLY_CT_KILL_CONFIG_CMD);
606 IWL_CMD(SENSITIVITY_CMD);
607 IWL_CMD(REPLY_PHY_CALIBRATION_CMD);
608 IWL_CMD(REPLY_RX_PHY_CMD);
609 IWL_CMD(REPLY_RX_MPDU_CMD);
610 IWL_CMD(REPLY_4965_RX);
611 IWL_CMD(REPLY_COMPRESSED_BA);
612 default:
613 return "UNKNOWN";
614
615 }
616}
617
618#define HOST_COMPLETE_TIMEOUT (HZ / 2)
619
620/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800621 * iwl4965_enqueue_hcmd - enqueue a uCode command
Zhu Yib481de92007-09-25 17:54:57 -0700622 * @priv: device private data point
623 * @cmd: a point to the ucode command structure
624 *
625 * The function returns < 0 values to indicate the operation is
626 * failed. On success, it turns the index (> 0) of command in the
627 * command queue.
628 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700629static int iwl4965_enqueue_hcmd(struct iwl_priv *priv, struct iwl4965_host_cmd *cmd)
Zhu Yib481de92007-09-25 17:54:57 -0700630{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800631 struct iwl4965_tx_queue *txq = &priv->txq[IWL_CMD_QUEUE_NUM];
632 struct iwl4965_queue *q = &txq->q;
633 struct iwl4965_tfd_frame *tfd;
Zhu Yib481de92007-09-25 17:54:57 -0700634 u32 *control_flags;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800635 struct iwl4965_cmd *out_cmd;
Zhu Yib481de92007-09-25 17:54:57 -0700636 u32 idx;
637 u16 fix_size = (u16)(cmd->len + sizeof(out_cmd->hdr));
638 dma_addr_t phys_addr;
639 int ret;
640 unsigned long flags;
641
642 /* If any of the command structures end up being larger than
643 * the TFD_MAX_PAYLOAD_SIZE, and it sent as a 'small' command then
644 * we will need to increase the size of the TFD entries */
645 BUG_ON((fix_size > TFD_MAX_PAYLOAD_SIZE) &&
646 !(cmd->meta.flags & CMD_SIZE_HUGE));
647
Gregory Greenmanc342a1b2008-02-06 11:20:40 -0800648 if (iwl4965_is_rfkill(priv)) {
649 IWL_DEBUG_INFO("Not sending command - RF KILL");
650 return -EIO;
651 }
652
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800653 if (iwl4965_queue_space(q) < ((cmd->meta.flags & CMD_ASYNC) ? 2 : 1)) {
Zhu Yib481de92007-09-25 17:54:57 -0700654 IWL_ERROR("No space for Tx\n");
655 return -ENOSPC;
656 }
657
658 spin_lock_irqsave(&priv->hcmd_lock, flags);
659
Tomas Winklerfc4b6852007-10-25 17:15:24 +0800660 tfd = &txq->bd[q->write_ptr];
Zhu Yib481de92007-09-25 17:54:57 -0700661 memset(tfd, 0, sizeof(*tfd));
662
663 control_flags = (u32 *) tfd;
664
Tomas Winklerfc4b6852007-10-25 17:15:24 +0800665 idx = get_cmd_index(q, q->write_ptr, cmd->meta.flags & CMD_SIZE_HUGE);
Zhu Yib481de92007-09-25 17:54:57 -0700666 out_cmd = &txq->cmd[idx];
667
668 out_cmd->hdr.cmd = cmd->id;
669 memcpy(&out_cmd->meta, &cmd->meta, sizeof(cmd->meta));
670 memcpy(&out_cmd->cmd.payload, cmd->data, cmd->len);
671
672 /* At this point, the out_cmd now has all of the incoming cmd
673 * information */
674
675 out_cmd->hdr.flags = 0;
676 out_cmd->hdr.sequence = cpu_to_le16(QUEUE_TO_SEQ(IWL_CMD_QUEUE_NUM) |
Tomas Winklerfc4b6852007-10-25 17:15:24 +0800677 INDEX_TO_SEQ(q->write_ptr));
Zhu Yib481de92007-09-25 17:54:57 -0700678 if (out_cmd->meta.flags & CMD_SIZE_HUGE)
679 out_cmd->hdr.sequence |= cpu_to_le16(SEQ_HUGE_FRAME);
680
681 phys_addr = txq->dma_addr_cmd + sizeof(txq->cmd[0]) * idx +
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800682 offsetof(struct iwl4965_cmd, hdr);
683 iwl4965_hw_txq_attach_buf_to_tfd(priv, tfd, phys_addr, fix_size);
Zhu Yib481de92007-09-25 17:54:57 -0700684
685 IWL_DEBUG_HC("Sending command %s (#%x), seq: 0x%04X, "
686 "%d bytes at %d[%d]:%d\n",
687 get_cmd_string(out_cmd->hdr.cmd),
688 out_cmd->hdr.cmd, le16_to_cpu(out_cmd->hdr.sequence),
Tomas Winklerfc4b6852007-10-25 17:15:24 +0800689 fix_size, q->write_ptr, idx, IWL_CMD_QUEUE_NUM);
Zhu Yib481de92007-09-25 17:54:57 -0700690
691 txq->need_update = 1;
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800692
693 /* Set up entry in queue's byte count circular buffer */
Zhu Yib481de92007-09-25 17:54:57 -0700694 ret = iwl4965_tx_queue_update_wr_ptr(priv, txq, 0);
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800695
696 /* Increment and update queue's write index */
Tomas Winklerc54b6792008-03-06 17:36:53 -0800697 q->write_ptr = iwl_queue_inc_wrap(q->write_ptr, q->n_bd);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800698 iwl4965_tx_queue_update_write_ptr(priv, txq);
Zhu Yib481de92007-09-25 17:54:57 -0700699
700 spin_unlock_irqrestore(&priv->hcmd_lock, flags);
701 return ret ? ret : idx;
702}
703
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700704static int iwl4965_send_cmd_async(struct iwl_priv *priv, struct iwl4965_host_cmd *cmd)
Zhu Yib481de92007-09-25 17:54:57 -0700705{
706 int ret;
707
708 BUG_ON(!(cmd->meta.flags & CMD_ASYNC));
709
710 /* An asynchronous command can not expect an SKB to be set. */
711 BUG_ON(cmd->meta.flags & CMD_WANT_SKB);
712
713 /* An asynchronous command MUST have a callback. */
714 BUG_ON(!cmd->meta.u.callback);
715
716 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
717 return -EBUSY;
718
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800719 ret = iwl4965_enqueue_hcmd(priv, cmd);
Zhu Yib481de92007-09-25 17:54:57 -0700720 if (ret < 0) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800721 IWL_ERROR("Error sending %s: iwl4965_enqueue_hcmd failed: %d\n",
Zhu Yib481de92007-09-25 17:54:57 -0700722 get_cmd_string(cmd->id), ret);
723 return ret;
724 }
725 return 0;
726}
727
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700728static int iwl4965_send_cmd_sync(struct iwl_priv *priv, struct iwl4965_host_cmd *cmd)
Zhu Yib481de92007-09-25 17:54:57 -0700729{
730 int cmd_idx;
731 int ret;
732 static atomic_t entry = ATOMIC_INIT(0); /* reentrance protection */
733
734 BUG_ON(cmd->meta.flags & CMD_ASYNC);
735
736 /* A synchronous command can not have a callback set. */
737 BUG_ON(cmd->meta.u.callback != NULL);
738
739 if (atomic_xchg(&entry, 1)) {
740 IWL_ERROR("Error sending %s: Already sending a host command\n",
741 get_cmd_string(cmd->id));
742 return -EBUSY;
743 }
744
745 set_bit(STATUS_HCMD_ACTIVE, &priv->status);
746
747 if (cmd->meta.flags & CMD_WANT_SKB)
748 cmd->meta.source = &cmd->meta;
749
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800750 cmd_idx = iwl4965_enqueue_hcmd(priv, cmd);
Zhu Yib481de92007-09-25 17:54:57 -0700751 if (cmd_idx < 0) {
752 ret = cmd_idx;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800753 IWL_ERROR("Error sending %s: iwl4965_enqueue_hcmd failed: %d\n",
Zhu Yib481de92007-09-25 17:54:57 -0700754 get_cmd_string(cmd->id), ret);
755 goto out;
756 }
757
758 ret = wait_event_interruptible_timeout(priv->wait_command_queue,
759 !test_bit(STATUS_HCMD_ACTIVE, &priv->status),
760 HOST_COMPLETE_TIMEOUT);
761 if (!ret) {
762 if (test_bit(STATUS_HCMD_ACTIVE, &priv->status)) {
763 IWL_ERROR("Error sending %s: time out after %dms.\n",
764 get_cmd_string(cmd->id),
765 jiffies_to_msecs(HOST_COMPLETE_TIMEOUT));
766
767 clear_bit(STATUS_HCMD_ACTIVE, &priv->status);
768 ret = -ETIMEDOUT;
769 goto cancel;
770 }
771 }
772
773 if (test_bit(STATUS_RF_KILL_HW, &priv->status)) {
774 IWL_DEBUG_INFO("Command %s aborted: RF KILL Switch\n",
775 get_cmd_string(cmd->id));
776 ret = -ECANCELED;
777 goto fail;
778 }
779 if (test_bit(STATUS_FW_ERROR, &priv->status)) {
780 IWL_DEBUG_INFO("Command %s failed: FW Error\n",
781 get_cmd_string(cmd->id));
782 ret = -EIO;
783 goto fail;
784 }
785 if ((cmd->meta.flags & CMD_WANT_SKB) && !cmd->meta.u.skb) {
786 IWL_ERROR("Error: Response NULL in '%s'\n",
787 get_cmd_string(cmd->id));
788 ret = -EIO;
789 goto out;
790 }
791
792 ret = 0;
793 goto out;
794
795cancel:
796 if (cmd->meta.flags & CMD_WANT_SKB) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800797 struct iwl4965_cmd *qcmd;
Zhu Yib481de92007-09-25 17:54:57 -0700798
799 /* Cancel the CMD_WANT_SKB flag for the cmd in the
800 * TX cmd queue. Otherwise in case the cmd comes
801 * in later, it will possibly set an invalid
802 * address (cmd->meta.source). */
803 qcmd = &priv->txq[IWL_CMD_QUEUE_NUM].cmd[cmd_idx];
804 qcmd->meta.flags &= ~CMD_WANT_SKB;
805 }
806fail:
807 if (cmd->meta.u.skb) {
808 dev_kfree_skb_any(cmd->meta.u.skb);
809 cmd->meta.u.skb = NULL;
810 }
811out:
812 atomic_set(&entry, 0);
813 return ret;
814}
815
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700816int iwl4965_send_cmd(struct iwl_priv *priv, struct iwl4965_host_cmd *cmd)
Zhu Yib481de92007-09-25 17:54:57 -0700817{
Zhu Yib481de92007-09-25 17:54:57 -0700818 if (cmd->meta.flags & CMD_ASYNC)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800819 return iwl4965_send_cmd_async(priv, cmd);
Zhu Yib481de92007-09-25 17:54:57 -0700820
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800821 return iwl4965_send_cmd_sync(priv, cmd);
Zhu Yib481de92007-09-25 17:54:57 -0700822}
823
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700824int iwl4965_send_cmd_pdu(struct iwl_priv *priv, u8 id, u16 len, const void *data)
Zhu Yib481de92007-09-25 17:54:57 -0700825{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800826 struct iwl4965_host_cmd cmd = {
Zhu Yib481de92007-09-25 17:54:57 -0700827 .id = id,
828 .len = len,
829 .data = data,
830 };
831
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800832 return iwl4965_send_cmd_sync(priv, &cmd);
Zhu Yib481de92007-09-25 17:54:57 -0700833}
834
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700835static int __must_check iwl4965_send_cmd_u32(struct iwl_priv *priv, u8 id, u32 val)
Zhu Yib481de92007-09-25 17:54:57 -0700836{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800837 struct iwl4965_host_cmd cmd = {
Zhu Yib481de92007-09-25 17:54:57 -0700838 .id = id,
839 .len = sizeof(val),
840 .data = &val,
841 };
842
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800843 return iwl4965_send_cmd_sync(priv, &cmd);
Zhu Yib481de92007-09-25 17:54:57 -0700844}
845
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700846int iwl4965_send_statistics_request(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700847{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800848 return iwl4965_send_cmd_u32(priv, REPLY_STATISTICS_CMD, 0);
Zhu Yib481de92007-09-25 17:54:57 -0700849}
850
851/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800852 * iwl4965_rxon_add_station - add station into station table.
Zhu Yib481de92007-09-25 17:54:57 -0700853 *
854 * there is only one AP station with id= IWL_AP_ID
Ben Cahill9fbab512007-11-29 11:09:47 +0800855 * NOTE: mutex must be held before calling this fnction
856 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700857static int iwl4965_rxon_add_station(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -0700858 const u8 *addr, int is_ap)
859{
Zhu Yi556f8db2007-09-27 11:27:33 +0800860 u8 sta_id;
Zhu Yib481de92007-09-25 17:54:57 -0700861
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800862 /* Add station to device's station table */
Ron Rindjunsky67d62032007-11-26 16:14:40 +0200863#ifdef CONFIG_IWL4965_HT
864 struct ieee80211_conf *conf = &priv->hw->conf;
865 struct ieee80211_ht_info *cur_ht_config = &conf->ht_conf;
866
867 if ((is_ap) &&
868 (conf->flags & IEEE80211_CONF_SUPPORT_HT_MODE) &&
869 (priv->iw_mode == IEEE80211_IF_TYPE_STA))
870 sta_id = iwl4965_add_station_flags(priv, addr, is_ap,
871 0, cur_ht_config);
872 else
873#endif /* CONFIG_IWL4965_HT */
874 sta_id = iwl4965_add_station_flags(priv, addr, is_ap,
875 0, NULL);
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800876
877 /* Set up default rate scaling table in device's station table */
Zhu Yib481de92007-09-25 17:54:57 -0700878 iwl4965_add_station(priv, addr, is_ap);
879
Zhu Yi556f8db2007-09-27 11:27:33 +0800880 return sta_id;
Zhu Yib481de92007-09-25 17:54:57 -0700881}
882
883/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800884 * iwl4965_set_rxon_channel - Set the phymode and channel values in staging RXON
Zhu Yib481de92007-09-25 17:54:57 -0700885 * @phymode: MODE_IEEE80211A sets to 5.2GHz; all else set to 2.4GHz
886 * @channel: Any channel valid for the requested phymode
887
888 * In addition to setting the staging RXON, priv->phymode is also set.
889 *
890 * NOTE: Does not commit to the hardware; it sets appropriate bit fields
891 * in the staging RXON flag structure based on the phymode
892 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700893static int iwl4965_set_rxon_channel(struct iwl_priv *priv,
Johannes Berg8318d782008-01-24 19:38:38 +0100894 enum ieee80211_band band,
Ben Cahill9fbab512007-11-29 11:09:47 +0800895 u16 channel)
Zhu Yib481de92007-09-25 17:54:57 -0700896{
Johannes Berg8318d782008-01-24 19:38:38 +0100897 if (!iwl4965_get_channel_info(priv, band, channel)) {
Zhu Yib481de92007-09-25 17:54:57 -0700898 IWL_DEBUG_INFO("Could not set channel to %d [%d]\n",
Johannes Berg8318d782008-01-24 19:38:38 +0100899 channel, band);
Zhu Yib481de92007-09-25 17:54:57 -0700900 return -EINVAL;
901 }
902
903 if ((le16_to_cpu(priv->staging_rxon.channel) == channel) &&
Johannes Berg8318d782008-01-24 19:38:38 +0100904 (priv->band == band))
Zhu Yib481de92007-09-25 17:54:57 -0700905 return 0;
906
907 priv->staging_rxon.channel = cpu_to_le16(channel);
Johannes Berg8318d782008-01-24 19:38:38 +0100908 if (band == IEEE80211_BAND_5GHZ)
Zhu Yib481de92007-09-25 17:54:57 -0700909 priv->staging_rxon.flags &= ~RXON_FLG_BAND_24G_MSK;
910 else
911 priv->staging_rxon.flags |= RXON_FLG_BAND_24G_MSK;
912
Johannes Berg8318d782008-01-24 19:38:38 +0100913 priv->band = band;
Zhu Yib481de92007-09-25 17:54:57 -0700914
Johannes Berg8318d782008-01-24 19:38:38 +0100915 IWL_DEBUG_INFO("Staging channel set to %d [%d]\n", channel, band);
Zhu Yib481de92007-09-25 17:54:57 -0700916
917 return 0;
918}
919
920/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800921 * iwl4965_check_rxon_cmd - validate RXON structure is valid
Zhu Yib481de92007-09-25 17:54:57 -0700922 *
923 * NOTE: This is really only useful during development and can eventually
924 * be #ifdef'd out once the driver is stable and folks aren't actively
925 * making changes
926 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800927static int iwl4965_check_rxon_cmd(struct iwl4965_rxon_cmd *rxon)
Zhu Yib481de92007-09-25 17:54:57 -0700928{
929 int error = 0;
930 int counter = 1;
931
932 if (rxon->flags & RXON_FLG_BAND_24G_MSK) {
933 error |= le32_to_cpu(rxon->flags &
934 (RXON_FLG_TGJ_NARROW_BAND_MSK |
935 RXON_FLG_RADAR_DETECT_MSK));
936 if (error)
937 IWL_WARNING("check 24G fields %d | %d\n",
938 counter++, error);
939 } else {
940 error |= (rxon->flags & RXON_FLG_SHORT_SLOT_MSK) ?
941 0 : le32_to_cpu(RXON_FLG_SHORT_SLOT_MSK);
942 if (error)
943 IWL_WARNING("check 52 fields %d | %d\n",
944 counter++, error);
945 error |= le32_to_cpu(rxon->flags & RXON_FLG_CCK_MSK);
946 if (error)
947 IWL_WARNING("check 52 CCK %d | %d\n",
948 counter++, error);
949 }
950 error |= (rxon->node_addr[0] | rxon->bssid_addr[0]) & 0x1;
951 if (error)
952 IWL_WARNING("check mac addr %d | %d\n", counter++, error);
953
954 /* make sure basic rates 6Mbps and 1Mbps are supported */
955 error |= (((rxon->ofdm_basic_rates & IWL_RATE_6M_MASK) == 0) &&
956 ((rxon->cck_basic_rates & IWL_RATE_1M_MASK) == 0));
957 if (error)
958 IWL_WARNING("check basic rate %d | %d\n", counter++, error);
959
960 error |= (le16_to_cpu(rxon->assoc_id) > 2007);
961 if (error)
962 IWL_WARNING("check assoc id %d | %d\n", counter++, error);
963
964 error |= ((rxon->flags & (RXON_FLG_CCK_MSK | RXON_FLG_SHORT_SLOT_MSK))
965 == (RXON_FLG_CCK_MSK | RXON_FLG_SHORT_SLOT_MSK));
966 if (error)
967 IWL_WARNING("check CCK and short slot %d | %d\n",
968 counter++, error);
969
970 error |= ((rxon->flags & (RXON_FLG_CCK_MSK | RXON_FLG_AUTO_DETECT_MSK))
971 == (RXON_FLG_CCK_MSK | RXON_FLG_AUTO_DETECT_MSK));
972 if (error)
973 IWL_WARNING("check CCK & auto detect %d | %d\n",
974 counter++, error);
975
976 error |= ((rxon->flags & (RXON_FLG_AUTO_DETECT_MSK |
977 RXON_FLG_TGG_PROTECT_MSK)) == RXON_FLG_TGG_PROTECT_MSK);
978 if (error)
979 IWL_WARNING("check TGG and auto detect %d | %d\n",
980 counter++, error);
981
982 if (error)
983 IWL_WARNING("Tuning to channel %d\n",
984 le16_to_cpu(rxon->channel));
985
986 if (error) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800987 IWL_ERROR("Not a valid iwl4965_rxon_assoc_cmd field values\n");
Zhu Yib481de92007-09-25 17:54:57 -0700988 return -1;
989 }
990 return 0;
991}
992
993/**
Ben Cahill9fbab512007-11-29 11:09:47 +0800994 * iwl4965_full_rxon_required - check if full RXON (vs RXON_ASSOC) cmd is needed
Ian Schram01ebd062007-10-25 17:15:22 +0800995 * @priv: staging_rxon is compared to active_rxon
Zhu Yib481de92007-09-25 17:54:57 -0700996 *
Ben Cahill9fbab512007-11-29 11:09:47 +0800997 * If the RXON structure is changing enough to require a new tune,
998 * or is clearing the RXON_FILTER_ASSOC_MSK, then return 1 to indicate that
999 * a new tune (full RXON command, rather than RXON_ASSOC cmd) is required.
Zhu Yib481de92007-09-25 17:54:57 -07001000 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001001static int iwl4965_full_rxon_required(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001002{
1003
1004 /* These items are only settable from the full RXON command */
1005 if (!(priv->active_rxon.filter_flags & RXON_FILTER_ASSOC_MSK) ||
1006 compare_ether_addr(priv->staging_rxon.bssid_addr,
1007 priv->active_rxon.bssid_addr) ||
1008 compare_ether_addr(priv->staging_rxon.node_addr,
1009 priv->active_rxon.node_addr) ||
1010 compare_ether_addr(priv->staging_rxon.wlap_bssid_addr,
1011 priv->active_rxon.wlap_bssid_addr) ||
1012 (priv->staging_rxon.dev_type != priv->active_rxon.dev_type) ||
1013 (priv->staging_rxon.channel != priv->active_rxon.channel) ||
1014 (priv->staging_rxon.air_propagation !=
1015 priv->active_rxon.air_propagation) ||
1016 (priv->staging_rxon.ofdm_ht_single_stream_basic_rates !=
1017 priv->active_rxon.ofdm_ht_single_stream_basic_rates) ||
1018 (priv->staging_rxon.ofdm_ht_dual_stream_basic_rates !=
1019 priv->active_rxon.ofdm_ht_dual_stream_basic_rates) ||
1020 (priv->staging_rxon.rx_chain != priv->active_rxon.rx_chain) ||
1021 (priv->staging_rxon.assoc_id != priv->active_rxon.assoc_id))
1022 return 1;
1023
1024 /* flags, filter_flags, ofdm_basic_rates, and cck_basic_rates can
1025 * be updated with the RXON_ASSOC command -- however only some
1026 * flag transitions are allowed using RXON_ASSOC */
1027
1028 /* Check if we are not switching bands */
1029 if ((priv->staging_rxon.flags & RXON_FLG_BAND_24G_MSK) !=
1030 (priv->active_rxon.flags & RXON_FLG_BAND_24G_MSK))
1031 return 1;
1032
1033 /* Check if we are switching association toggle */
1034 if ((priv->staging_rxon.filter_flags & RXON_FILTER_ASSOC_MSK) !=
1035 (priv->active_rxon.filter_flags & RXON_FILTER_ASSOC_MSK))
1036 return 1;
1037
1038 return 0;
1039}
1040
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001041static int iwl4965_send_rxon_assoc(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001042{
1043 int rc = 0;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001044 struct iwl4965_rx_packet *res = NULL;
1045 struct iwl4965_rxon_assoc_cmd rxon_assoc;
1046 struct iwl4965_host_cmd cmd = {
Zhu Yib481de92007-09-25 17:54:57 -07001047 .id = REPLY_RXON_ASSOC,
1048 .len = sizeof(rxon_assoc),
1049 .meta.flags = CMD_WANT_SKB,
1050 .data = &rxon_assoc,
1051 };
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001052 const struct iwl4965_rxon_cmd *rxon1 = &priv->staging_rxon;
1053 const struct iwl4965_rxon_cmd *rxon2 = &priv->active_rxon;
Zhu Yib481de92007-09-25 17:54:57 -07001054
1055 if ((rxon1->flags == rxon2->flags) &&
1056 (rxon1->filter_flags == rxon2->filter_flags) &&
1057 (rxon1->cck_basic_rates == rxon2->cck_basic_rates) &&
1058 (rxon1->ofdm_ht_single_stream_basic_rates ==
1059 rxon2->ofdm_ht_single_stream_basic_rates) &&
1060 (rxon1->ofdm_ht_dual_stream_basic_rates ==
1061 rxon2->ofdm_ht_dual_stream_basic_rates) &&
1062 (rxon1->rx_chain == rxon2->rx_chain) &&
1063 (rxon1->ofdm_basic_rates == rxon2->ofdm_basic_rates)) {
1064 IWL_DEBUG_INFO("Using current RXON_ASSOC. Not resending.\n");
1065 return 0;
1066 }
1067
1068 rxon_assoc.flags = priv->staging_rxon.flags;
1069 rxon_assoc.filter_flags = priv->staging_rxon.filter_flags;
1070 rxon_assoc.ofdm_basic_rates = priv->staging_rxon.ofdm_basic_rates;
1071 rxon_assoc.cck_basic_rates = priv->staging_rxon.cck_basic_rates;
1072 rxon_assoc.reserved = 0;
1073 rxon_assoc.ofdm_ht_single_stream_basic_rates =
1074 priv->staging_rxon.ofdm_ht_single_stream_basic_rates;
1075 rxon_assoc.ofdm_ht_dual_stream_basic_rates =
1076 priv->staging_rxon.ofdm_ht_dual_stream_basic_rates;
1077 rxon_assoc.rx_chain_select_flags = priv->staging_rxon.rx_chain;
1078
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001079 rc = iwl4965_send_cmd_sync(priv, &cmd);
Zhu Yib481de92007-09-25 17:54:57 -07001080 if (rc)
1081 return rc;
1082
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001083 res = (struct iwl4965_rx_packet *)cmd.meta.u.skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07001084 if (res->hdr.flags & IWL_CMD_FAILED_MSK) {
1085 IWL_ERROR("Bad return from REPLY_RXON_ASSOC command\n");
1086 rc = -EIO;
1087 }
1088
1089 priv->alloc_rxb_skb--;
1090 dev_kfree_skb_any(cmd.meta.u.skb);
1091
1092 return rc;
1093}
1094
1095/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001096 * iwl4965_commit_rxon - commit staging_rxon to hardware
Zhu Yib481de92007-09-25 17:54:57 -07001097 *
Ian Schram01ebd062007-10-25 17:15:22 +08001098 * The RXON command in staging_rxon is committed to the hardware and
Zhu Yib481de92007-09-25 17:54:57 -07001099 * the active_rxon structure is updated with the new data. This
1100 * function correctly transitions out of the RXON_ASSOC_MSK state if
1101 * a HW tune is required based on the RXON structure changes.
1102 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001103static int iwl4965_commit_rxon(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001104{
1105 /* cast away the const for active_rxon in this function */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001106 struct iwl4965_rxon_cmd *active_rxon = (void *)&priv->active_rxon;
Joe Perches0795af52007-10-03 17:59:30 -07001107 DECLARE_MAC_BUF(mac);
Zhu Yib481de92007-09-25 17:54:57 -07001108 int rc = 0;
1109
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001110 if (!iwl4965_is_alive(priv))
Zhu Yib481de92007-09-25 17:54:57 -07001111 return -1;
1112
1113 /* always get timestamp with Rx frame */
1114 priv->staging_rxon.flags |= RXON_FLG_TSF2HOST_MSK;
1115
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001116 rc = iwl4965_check_rxon_cmd(&priv->staging_rxon);
Zhu Yib481de92007-09-25 17:54:57 -07001117 if (rc) {
1118 IWL_ERROR("Invalid RXON configuration. Not committing.\n");
1119 return -EINVAL;
1120 }
1121
1122 /* If we don't need to send a full RXON, we can use
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001123 * iwl4965_rxon_assoc_cmd which is used to reconfigure filter
Zhu Yib481de92007-09-25 17:54:57 -07001124 * and other flags for the current radio configuration. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001125 if (!iwl4965_full_rxon_required(priv)) {
1126 rc = iwl4965_send_rxon_assoc(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001127 if (rc) {
1128 IWL_ERROR("Error setting RXON_ASSOC "
1129 "configuration (%d).\n", rc);
1130 return rc;
1131 }
1132
1133 memcpy(active_rxon, &priv->staging_rxon, sizeof(*active_rxon));
1134
1135 return 0;
1136 }
1137
1138 /* station table will be cleared */
1139 priv->assoc_station_added = 0;
1140
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08001141#ifdef CONFIG_IWL4965_SENSITIVITY
Zhu Yib481de92007-09-25 17:54:57 -07001142 priv->sensitivity_data.state = IWL_SENS_CALIB_NEED_REINIT;
1143 if (!priv->error_recovering)
1144 priv->start_calib = 0;
1145
1146 iwl4965_init_sensitivity(priv, CMD_ASYNC, 1);
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08001147#endif /* CONFIG_IWL4965_SENSITIVITY */
Zhu Yib481de92007-09-25 17:54:57 -07001148
1149 /* If we are currently associated and the new config requires
1150 * an RXON_ASSOC and the new config wants the associated mask enabled,
1151 * we must clear the associated from the active configuration
1152 * before we apply the new config */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001153 if (iwl4965_is_associated(priv) &&
Zhu Yib481de92007-09-25 17:54:57 -07001154 (priv->staging_rxon.filter_flags & RXON_FILTER_ASSOC_MSK)) {
1155 IWL_DEBUG_INFO("Toggling associated bit on current RXON\n");
1156 active_rxon->filter_flags &= ~RXON_FILTER_ASSOC_MSK;
1157
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001158 rc = iwl4965_send_cmd_pdu(priv, REPLY_RXON,
1159 sizeof(struct iwl4965_rxon_cmd),
Zhu Yib481de92007-09-25 17:54:57 -07001160 &priv->active_rxon);
1161
1162 /* If the mask clearing failed then we set
1163 * active_rxon back to what it was previously */
1164 if (rc) {
1165 active_rxon->filter_flags |= RXON_FILTER_ASSOC_MSK;
1166 IWL_ERROR("Error clearing ASSOC_MSK on current "
1167 "configuration (%d).\n", rc);
1168 return rc;
1169 }
Zhu Yib481de92007-09-25 17:54:57 -07001170 }
1171
1172 IWL_DEBUG_INFO("Sending RXON\n"
1173 "* with%s RXON_FILTER_ASSOC_MSK\n"
1174 "* channel = %d\n"
Joe Perches0795af52007-10-03 17:59:30 -07001175 "* bssid = %s\n",
Zhu Yib481de92007-09-25 17:54:57 -07001176 ((priv->staging_rxon.filter_flags &
1177 RXON_FILTER_ASSOC_MSK) ? "" : "out"),
1178 le16_to_cpu(priv->staging_rxon.channel),
Joe Perches0795af52007-10-03 17:59:30 -07001179 print_mac(mac, priv->staging_rxon.bssid_addr));
Zhu Yib481de92007-09-25 17:54:57 -07001180
1181 /* Apply the new configuration */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001182 rc = iwl4965_send_cmd_pdu(priv, REPLY_RXON,
1183 sizeof(struct iwl4965_rxon_cmd), &priv->staging_rxon);
Zhu Yib481de92007-09-25 17:54:57 -07001184 if (rc) {
1185 IWL_ERROR("Error setting new configuration (%d).\n", rc);
1186 return rc;
1187 }
1188
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001189 iwl4965_clear_stations_table(priv);
Zhu Yi556f8db2007-09-27 11:27:33 +08001190
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08001191#ifdef CONFIG_IWL4965_SENSITIVITY
Zhu Yib481de92007-09-25 17:54:57 -07001192 if (!priv->error_recovering)
1193 priv->start_calib = 0;
1194
1195 priv->sensitivity_data.state = IWL_SENS_CALIB_NEED_REINIT;
1196 iwl4965_init_sensitivity(priv, CMD_ASYNC, 1);
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08001197#endif /* CONFIG_IWL4965_SENSITIVITY */
Zhu Yib481de92007-09-25 17:54:57 -07001198
1199 memcpy(active_rxon, &priv->staging_rxon, sizeof(*active_rxon));
1200
1201 /* If we issue a new RXON command which required a tune then we must
1202 * send a new TXPOWER command or we won't be able to Tx any frames */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001203 rc = iwl4965_hw_reg_send_txpower(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001204 if (rc) {
1205 IWL_ERROR("Error setting Tx power (%d).\n", rc);
1206 return rc;
1207 }
1208
1209 /* Add the broadcast address so we can send broadcast frames */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001210 if (iwl4965_rxon_add_station(priv, iwl4965_broadcast_addr, 0) ==
Zhu Yib481de92007-09-25 17:54:57 -07001211 IWL_INVALID_STATION) {
1212 IWL_ERROR("Error adding BROADCAST address for transmit.\n");
1213 return -EIO;
1214 }
1215
1216 /* If we have set the ASSOC_MSK and we are in BSS mode then
1217 * add the IWL_AP_ID to the station rate table */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001218 if (iwl4965_is_associated(priv) &&
Zhu Yib481de92007-09-25 17:54:57 -07001219 (priv->iw_mode == IEEE80211_IF_TYPE_STA)) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001220 if (iwl4965_rxon_add_station(priv, priv->active_rxon.bssid_addr, 1)
Zhu Yib481de92007-09-25 17:54:57 -07001221 == IWL_INVALID_STATION) {
1222 IWL_ERROR("Error adding AP address for transmit.\n");
1223 return -EIO;
1224 }
1225 priv->assoc_station_added = 1;
1226 }
1227
1228 return 0;
1229}
1230
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001231static int iwl4965_send_bt_config(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001232{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001233 struct iwl4965_bt_cmd bt_cmd = {
Zhu Yib481de92007-09-25 17:54:57 -07001234 .flags = 3,
1235 .lead_time = 0xAA,
1236 .max_kill = 1,
1237 .kill_ack_mask = 0,
1238 .kill_cts_mask = 0,
1239 };
1240
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001241 return iwl4965_send_cmd_pdu(priv, REPLY_BT_CONFIG,
1242 sizeof(struct iwl4965_bt_cmd), &bt_cmd);
Zhu Yib481de92007-09-25 17:54:57 -07001243}
1244
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001245static int iwl4965_send_scan_abort(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001246{
1247 int rc = 0;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001248 struct iwl4965_rx_packet *res;
1249 struct iwl4965_host_cmd cmd = {
Zhu Yib481de92007-09-25 17:54:57 -07001250 .id = REPLY_SCAN_ABORT_CMD,
1251 .meta.flags = CMD_WANT_SKB,
1252 };
1253
1254 /* If there isn't a scan actively going on in the hardware
1255 * then we are in between scan bands and not actually
1256 * actively scanning, so don't send the abort command */
1257 if (!test_bit(STATUS_SCAN_HW, &priv->status)) {
1258 clear_bit(STATUS_SCAN_ABORTING, &priv->status);
1259 return 0;
1260 }
1261
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001262 rc = iwl4965_send_cmd_sync(priv, &cmd);
Zhu Yib481de92007-09-25 17:54:57 -07001263 if (rc) {
1264 clear_bit(STATUS_SCAN_ABORTING, &priv->status);
1265 return rc;
1266 }
1267
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001268 res = (struct iwl4965_rx_packet *)cmd.meta.u.skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07001269 if (res->u.status != CAN_ABORT_STATUS) {
1270 /* The scan abort will return 1 for success or
1271 * 2 for "failure". A failure condition can be
1272 * due to simply not being in an active scan which
1273 * can occur if we send the scan abort before we
1274 * the microcode has notified us that a scan is
1275 * completed. */
1276 IWL_DEBUG_INFO("SCAN_ABORT returned %d.\n", res->u.status);
1277 clear_bit(STATUS_SCAN_ABORTING, &priv->status);
1278 clear_bit(STATUS_SCAN_HW, &priv->status);
1279 }
1280
1281 dev_kfree_skb_any(cmd.meta.u.skb);
1282
1283 return rc;
1284}
1285
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001286static int iwl4965_card_state_sync_callback(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001287 struct iwl4965_cmd *cmd,
Zhu Yib481de92007-09-25 17:54:57 -07001288 struct sk_buff *skb)
1289{
1290 return 1;
1291}
1292
1293/*
1294 * CARD_STATE_CMD
1295 *
Ben Cahill9fbab512007-11-29 11:09:47 +08001296 * Use: Sets the device's internal card state to enable, disable, or halt
Zhu Yib481de92007-09-25 17:54:57 -07001297 *
1298 * When in the 'enable' state the card operates as normal.
1299 * When in the 'disable' state, the card enters into a low power mode.
1300 * When in the 'halt' state, the card is shut down and must be fully
1301 * restarted to come back on.
1302 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001303static int iwl4965_send_card_state(struct iwl_priv *priv, u32 flags, u8 meta_flag)
Zhu Yib481de92007-09-25 17:54:57 -07001304{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001305 struct iwl4965_host_cmd cmd = {
Zhu Yib481de92007-09-25 17:54:57 -07001306 .id = REPLY_CARD_STATE_CMD,
1307 .len = sizeof(u32),
1308 .data = &flags,
1309 .meta.flags = meta_flag,
1310 };
1311
1312 if (meta_flag & CMD_ASYNC)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001313 cmd.meta.u.callback = iwl4965_card_state_sync_callback;
Zhu Yib481de92007-09-25 17:54:57 -07001314
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001315 return iwl4965_send_cmd(priv, &cmd);
Zhu Yib481de92007-09-25 17:54:57 -07001316}
1317
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001318static int iwl4965_add_sta_sync_callback(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001319 struct iwl4965_cmd *cmd, struct sk_buff *skb)
Zhu Yib481de92007-09-25 17:54:57 -07001320{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001321 struct iwl4965_rx_packet *res = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07001322
1323 if (!skb) {
1324 IWL_ERROR("Error: Response NULL in REPLY_ADD_STA.\n");
1325 return 1;
1326 }
1327
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001328 res = (struct iwl4965_rx_packet *)skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07001329 if (res->hdr.flags & IWL_CMD_FAILED_MSK) {
1330 IWL_ERROR("Bad return from REPLY_ADD_STA (0x%08X)\n",
1331 res->hdr.flags);
1332 return 1;
1333 }
1334
1335 switch (res->u.add_sta.status) {
1336 case ADD_STA_SUCCESS_MSK:
1337 break;
1338 default:
1339 break;
1340 }
1341
1342 /* We didn't cache the SKB; let the caller free it */
1343 return 1;
1344}
1345
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001346int iwl4965_send_add_station(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001347 struct iwl4965_addsta_cmd *sta, u8 flags)
Zhu Yib481de92007-09-25 17:54:57 -07001348{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001349 struct iwl4965_rx_packet *res = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07001350 int rc = 0;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001351 struct iwl4965_host_cmd cmd = {
Zhu Yib481de92007-09-25 17:54:57 -07001352 .id = REPLY_ADD_STA,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001353 .len = sizeof(struct iwl4965_addsta_cmd),
Zhu Yib481de92007-09-25 17:54:57 -07001354 .meta.flags = flags,
1355 .data = sta,
1356 };
1357
1358 if (flags & CMD_ASYNC)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001359 cmd.meta.u.callback = iwl4965_add_sta_sync_callback;
Zhu Yib481de92007-09-25 17:54:57 -07001360 else
1361 cmd.meta.flags |= CMD_WANT_SKB;
1362
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001363 rc = iwl4965_send_cmd(priv, &cmd);
Zhu Yib481de92007-09-25 17:54:57 -07001364
1365 if (rc || (flags & CMD_ASYNC))
1366 return rc;
1367
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001368 res = (struct iwl4965_rx_packet *)cmd.meta.u.skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07001369 if (res->hdr.flags & IWL_CMD_FAILED_MSK) {
1370 IWL_ERROR("Bad return from REPLY_ADD_STA (0x%08X)\n",
1371 res->hdr.flags);
1372 rc = -EIO;
1373 }
1374
1375 if (rc == 0) {
1376 switch (res->u.add_sta.status) {
1377 case ADD_STA_SUCCESS_MSK:
1378 IWL_DEBUG_INFO("REPLY_ADD_STA PASSED\n");
1379 break;
1380 default:
1381 rc = -EIO;
1382 IWL_WARNING("REPLY_ADD_STA failed\n");
1383 break;
1384 }
1385 }
1386
1387 priv->alloc_rxb_skb--;
1388 dev_kfree_skb_any(cmd.meta.u.skb);
1389
1390 return rc;
1391}
1392
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001393static int iwl4965_update_sta_key_info(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07001394 struct ieee80211_key_conf *keyconf,
1395 u8 sta_id)
1396{
1397 unsigned long flags;
1398 __le16 key_flags = 0;
1399
1400 switch (keyconf->alg) {
1401 case ALG_CCMP:
1402 key_flags |= STA_KEY_FLG_CCMP;
1403 key_flags |= cpu_to_le16(
1404 keyconf->keyidx << STA_KEY_FLG_KEYID_POS);
1405 key_flags &= ~STA_KEY_FLG_INVALID;
1406 break;
1407 case ALG_TKIP:
1408 case ALG_WEP:
Zhu Yib481de92007-09-25 17:54:57 -07001409 default:
1410 return -EINVAL;
1411 }
1412 spin_lock_irqsave(&priv->sta_lock, flags);
1413 priv->stations[sta_id].keyinfo.alg = keyconf->alg;
1414 priv->stations[sta_id].keyinfo.keylen = keyconf->keylen;
1415 memcpy(priv->stations[sta_id].keyinfo.key, keyconf->key,
1416 keyconf->keylen);
1417
1418 memcpy(priv->stations[sta_id].sta.key.key, keyconf->key,
1419 keyconf->keylen);
1420 priv->stations[sta_id].sta.key.key_flags = key_flags;
1421 priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
1422 priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
1423
1424 spin_unlock_irqrestore(&priv->sta_lock, flags);
1425
1426 IWL_DEBUG_INFO("hwcrypto: modify ucode station key info\n");
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001427 iwl4965_send_add_station(priv, &priv->stations[sta_id].sta, 0);
Zhu Yib481de92007-09-25 17:54:57 -07001428 return 0;
1429}
1430
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001431static int iwl4965_clear_sta_key_info(struct iwl_priv *priv, u8 sta_id)
Zhu Yib481de92007-09-25 17:54:57 -07001432{
1433 unsigned long flags;
1434
1435 spin_lock_irqsave(&priv->sta_lock, flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001436 memset(&priv->stations[sta_id].keyinfo, 0, sizeof(struct iwl4965_hw_key));
1437 memset(&priv->stations[sta_id].sta.key, 0, sizeof(struct iwl4965_keyinfo));
Zhu Yib481de92007-09-25 17:54:57 -07001438 priv->stations[sta_id].sta.key.key_flags = STA_KEY_FLG_NO_ENC;
1439 priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
1440 priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
1441 spin_unlock_irqrestore(&priv->sta_lock, flags);
1442
1443 IWL_DEBUG_INFO("hwcrypto: clear ucode station key info\n");
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001444 iwl4965_send_add_station(priv, &priv->stations[sta_id].sta, 0);
Zhu Yib481de92007-09-25 17:54:57 -07001445 return 0;
1446}
1447
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001448static void iwl4965_clear_free_frames(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001449{
1450 struct list_head *element;
1451
1452 IWL_DEBUG_INFO("%d frames on pre-allocated heap on clear.\n",
1453 priv->frames_count);
1454
1455 while (!list_empty(&priv->free_frames)) {
1456 element = priv->free_frames.next;
1457 list_del(element);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001458 kfree(list_entry(element, struct iwl4965_frame, list));
Zhu Yib481de92007-09-25 17:54:57 -07001459 priv->frames_count--;
1460 }
1461
1462 if (priv->frames_count) {
1463 IWL_WARNING("%d frames still in use. Did we lose one?\n",
1464 priv->frames_count);
1465 priv->frames_count = 0;
1466 }
1467}
1468
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001469static struct iwl4965_frame *iwl4965_get_free_frame(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001470{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001471 struct iwl4965_frame *frame;
Zhu Yib481de92007-09-25 17:54:57 -07001472 struct list_head *element;
1473 if (list_empty(&priv->free_frames)) {
1474 frame = kzalloc(sizeof(*frame), GFP_KERNEL);
1475 if (!frame) {
1476 IWL_ERROR("Could not allocate frame!\n");
1477 return NULL;
1478 }
1479
1480 priv->frames_count++;
1481 return frame;
1482 }
1483
1484 element = priv->free_frames.next;
1485 list_del(element);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001486 return list_entry(element, struct iwl4965_frame, list);
Zhu Yib481de92007-09-25 17:54:57 -07001487}
1488
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001489static void iwl4965_free_frame(struct iwl_priv *priv, struct iwl4965_frame *frame)
Zhu Yib481de92007-09-25 17:54:57 -07001490{
1491 memset(frame, 0, sizeof(*frame));
1492 list_add(&frame->list, &priv->free_frames);
1493}
1494
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001495unsigned int iwl4965_fill_beacon_frame(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07001496 struct ieee80211_hdr *hdr,
1497 const u8 *dest, int left)
1498{
1499
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001500 if (!iwl4965_is_associated(priv) || !priv->ibss_beacon ||
Zhu Yib481de92007-09-25 17:54:57 -07001501 ((priv->iw_mode != IEEE80211_IF_TYPE_IBSS) &&
1502 (priv->iw_mode != IEEE80211_IF_TYPE_AP)))
1503 return 0;
1504
1505 if (priv->ibss_beacon->len > left)
1506 return 0;
1507
1508 memcpy(hdr, priv->ibss_beacon->data, priv->ibss_beacon->len);
1509
1510 return priv->ibss_beacon->len;
1511}
1512
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001513static u8 iwl4965_rate_get_lowest_plcp(int rate_mask)
Zhu Yib481de92007-09-25 17:54:57 -07001514{
1515 u8 i;
1516
1517 for (i = IWL_RATE_1M_INDEX; i != IWL_RATE_INVALID;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001518 i = iwl4965_rates[i].next_ieee) {
Zhu Yib481de92007-09-25 17:54:57 -07001519 if (rate_mask & (1 << i))
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001520 return iwl4965_rates[i].plcp;
Zhu Yib481de92007-09-25 17:54:57 -07001521 }
1522
1523 return IWL_RATE_INVALID;
1524}
1525
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001526static int iwl4965_send_beacon_cmd(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001527{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001528 struct iwl4965_frame *frame;
Zhu Yib481de92007-09-25 17:54:57 -07001529 unsigned int frame_size;
1530 int rc;
1531 u8 rate;
1532
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001533 frame = iwl4965_get_free_frame(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001534
1535 if (!frame) {
1536 IWL_ERROR("Could not obtain free frame buffer for beacon "
1537 "command.\n");
1538 return -ENOMEM;
1539 }
1540
1541 if (!(priv->staging_rxon.flags & RXON_FLG_BAND_24G_MSK)) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001542 rate = iwl4965_rate_get_lowest_plcp(priv->active_rate_basic &
Zhu Yib481de92007-09-25 17:54:57 -07001543 0xFF0);
1544 if (rate == IWL_INVALID_RATE)
1545 rate = IWL_RATE_6M_PLCP;
1546 } else {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001547 rate = iwl4965_rate_get_lowest_plcp(priv->active_rate_basic & 0xF);
Zhu Yib481de92007-09-25 17:54:57 -07001548 if (rate == IWL_INVALID_RATE)
1549 rate = IWL_RATE_1M_PLCP;
1550 }
1551
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001552 frame_size = iwl4965_hw_get_beacon_cmd(priv, frame, rate);
Zhu Yib481de92007-09-25 17:54:57 -07001553
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001554 rc = iwl4965_send_cmd_pdu(priv, REPLY_TX_BEACON, frame_size,
Zhu Yib481de92007-09-25 17:54:57 -07001555 &frame->u.cmd[0]);
1556
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001557 iwl4965_free_frame(priv, frame);
Zhu Yib481de92007-09-25 17:54:57 -07001558
1559 return rc;
1560}
1561
1562/******************************************************************************
1563 *
Zhu Yib481de92007-09-25 17:54:57 -07001564 * Misc. internal state and helper functions
1565 *
1566 ******************************************************************************/
Zhu Yib481de92007-09-25 17:54:57 -07001567
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001568static void iwl4965_unset_hw_setting(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001569{
1570 if (priv->hw_setting.shared_virt)
1571 pci_free_consistent(priv->pci_dev,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001572 sizeof(struct iwl4965_shared),
Zhu Yib481de92007-09-25 17:54:57 -07001573 priv->hw_setting.shared_virt,
1574 priv->hw_setting.shared_phys);
1575}
1576
1577/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001578 * iwl4965_supported_rate_to_ie - fill in the supported rate in IE field
Zhu Yib481de92007-09-25 17:54:57 -07001579 *
1580 * return : set the bit for each supported rate insert in ie
1581 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001582static u16 iwl4965_supported_rate_to_ie(u8 *ie, u16 supported_rate,
Tomas Winklerc7c46672007-10-18 02:04:15 +02001583 u16 basic_rate, int *left)
Zhu Yib481de92007-09-25 17:54:57 -07001584{
1585 u16 ret_rates = 0, bit;
1586 int i;
Tomas Winklerc7c46672007-10-18 02:04:15 +02001587 u8 *cnt = ie;
1588 u8 *rates = ie + 1;
Zhu Yib481de92007-09-25 17:54:57 -07001589
1590 for (bit = 1, i = 0; i < IWL_RATE_COUNT; i++, bit <<= 1) {
1591 if (bit & supported_rate) {
1592 ret_rates |= bit;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001593 rates[*cnt] = iwl4965_rates[i].ieee |
Tomas Winklerc7c46672007-10-18 02:04:15 +02001594 ((bit & basic_rate) ? 0x80 : 0x00);
1595 (*cnt)++;
1596 (*left)--;
1597 if ((*left <= 0) ||
1598 (*cnt >= IWL_SUPPORTED_RATES_IE_LEN))
Zhu Yib481de92007-09-25 17:54:57 -07001599 break;
1600 }
1601 }
1602
1603 return ret_rates;
1604}
1605
Zhu Yib481de92007-09-25 17:54:57 -07001606/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001607 * iwl4965_fill_probe_req - fill in all required fields and IE for probe request
Zhu Yib481de92007-09-25 17:54:57 -07001608 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001609static u16 iwl4965_fill_probe_req(struct iwl_priv *priv,
Tomas Winkler78330fd2008-02-06 02:37:18 +02001610 enum ieee80211_band band,
1611 struct ieee80211_mgmt *frame,
1612 int left, int is_direct)
Zhu Yib481de92007-09-25 17:54:57 -07001613{
1614 int len = 0;
1615 u8 *pos = NULL;
mabbasbee488d2007-10-25 17:15:42 +08001616 u16 active_rates, ret_rates, cck_rates, active_rate_basic;
Ron Rindjunsky8fb88032007-11-26 16:14:38 +02001617#ifdef CONFIG_IWL4965_HT
Tomas Winkler78330fd2008-02-06 02:37:18 +02001618 const struct ieee80211_supported_band *sband =
1619 iwl4965_get_hw_mode(priv, band);
Ron Rindjunsky8fb88032007-11-26 16:14:38 +02001620#endif /* CONFIG_IWL4965_HT */
Zhu Yib481de92007-09-25 17:54:57 -07001621
1622 /* Make sure there is enough space for the probe request,
1623 * two mandatory IEs and the data */
1624 left -= 24;
1625 if (left < 0)
1626 return 0;
1627 len += 24;
1628
1629 frame->frame_control = cpu_to_le16(IEEE80211_STYPE_PROBE_REQ);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001630 memcpy(frame->da, iwl4965_broadcast_addr, ETH_ALEN);
Zhu Yib481de92007-09-25 17:54:57 -07001631 memcpy(frame->sa, priv->mac_addr, ETH_ALEN);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001632 memcpy(frame->bssid, iwl4965_broadcast_addr, ETH_ALEN);
Zhu Yib481de92007-09-25 17:54:57 -07001633 frame->seq_ctrl = 0;
1634
1635 /* fill in our indirect SSID IE */
1636 /* ...next IE... */
1637
1638 left -= 2;
1639 if (left < 0)
1640 return 0;
1641 len += 2;
1642 pos = &(frame->u.probe_req.variable[0]);
1643 *pos++ = WLAN_EID_SSID;
1644 *pos++ = 0;
1645
1646 /* fill in our direct SSID IE... */
1647 if (is_direct) {
1648 /* ...next IE... */
1649 left -= 2 + priv->essid_len;
1650 if (left < 0)
1651 return 0;
1652 /* ... fill it in... */
1653 *pos++ = WLAN_EID_SSID;
1654 *pos++ = priv->essid_len;
1655 memcpy(pos, priv->essid, priv->essid_len);
1656 pos += priv->essid_len;
1657 len += 2 + priv->essid_len;
1658 }
1659
1660 /* fill in supported rate */
1661 /* ...next IE... */
1662 left -= 2;
1663 if (left < 0)
1664 return 0;
Tomas Winklerc7c46672007-10-18 02:04:15 +02001665
Zhu Yib481de92007-09-25 17:54:57 -07001666 /* ... fill it in... */
1667 *pos++ = WLAN_EID_SUPP_RATES;
1668 *pos = 0;
Tomas Winklerc7c46672007-10-18 02:04:15 +02001669
mabbasbee488d2007-10-25 17:15:42 +08001670 /* exclude 60M rate */
1671 active_rates = priv->rates_mask;
1672 active_rates &= ~IWL_RATE_60M_MASK;
1673
1674 active_rate_basic = active_rates & IWL_BASIC_RATES_MASK;
Zhu Yib481de92007-09-25 17:54:57 -07001675
Tomas Winklerc7c46672007-10-18 02:04:15 +02001676 cck_rates = IWL_CCK_RATES_MASK & active_rates;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001677 ret_rates = iwl4965_supported_rate_to_ie(pos, cck_rates,
mabbasbee488d2007-10-25 17:15:42 +08001678 active_rate_basic, &left);
Tomas Winklerc7c46672007-10-18 02:04:15 +02001679 active_rates &= ~ret_rates;
1680
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001681 ret_rates = iwl4965_supported_rate_to_ie(pos, active_rates,
mabbasbee488d2007-10-25 17:15:42 +08001682 active_rate_basic, &left);
Tomas Winklerc7c46672007-10-18 02:04:15 +02001683 active_rates &= ~ret_rates;
1684
Zhu Yib481de92007-09-25 17:54:57 -07001685 len += 2 + *pos;
1686 pos += (*pos) + 1;
Tomas Winklerc7c46672007-10-18 02:04:15 +02001687 if (active_rates == 0)
Zhu Yib481de92007-09-25 17:54:57 -07001688 goto fill_end;
1689
1690 /* fill in supported extended rate */
1691 /* ...next IE... */
1692 left -= 2;
1693 if (left < 0)
1694 return 0;
1695 /* ... fill it in... */
1696 *pos++ = WLAN_EID_EXT_SUPP_RATES;
1697 *pos = 0;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001698 iwl4965_supported_rate_to_ie(pos, active_rates,
mabbasbee488d2007-10-25 17:15:42 +08001699 active_rate_basic, &left);
Zhu Yib481de92007-09-25 17:54:57 -07001700 if (*pos > 0)
1701 len += 2 + *pos;
1702
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08001703#ifdef CONFIG_IWL4965_HT
Tomas Winkler78330fd2008-02-06 02:37:18 +02001704 if (sband && sband->ht_info.ht_supported) {
1705 struct ieee80211_ht_cap *ht_cap;
Zhu Yib481de92007-09-25 17:54:57 -07001706 pos += (*pos) + 1;
1707 *pos++ = WLAN_EID_HT_CAPABILITY;
Ron Rindjunsky8fb88032007-11-26 16:14:38 +02001708 *pos++ = sizeof(struct ieee80211_ht_cap);
Tomas Winkler78330fd2008-02-06 02:37:18 +02001709 ht_cap = (struct ieee80211_ht_cap *)pos;
1710 ht_cap->cap_info = cpu_to_le16(sband->ht_info.cap);
1711 memcpy(ht_cap->supp_mcs_set, sband->ht_info.supp_mcs_set, 16);
1712 ht_cap->ampdu_params_info =(sband->ht_info.ampdu_factor &
1713 IEEE80211_HT_CAP_AMPDU_FACTOR) |
1714 ((sband->ht_info.ampdu_density << 2) &
1715 IEEE80211_HT_CAP_AMPDU_DENSITY);
Ron Rindjunsky8fb88032007-11-26 16:14:38 +02001716 len += 2 + sizeof(struct ieee80211_ht_cap);
Zhu Yib481de92007-09-25 17:54:57 -07001717 }
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08001718#endif /*CONFIG_IWL4965_HT */
Zhu Yib481de92007-09-25 17:54:57 -07001719
1720 fill_end:
1721 return (u16)len;
1722}
1723
1724/*
1725 * QoS support
1726*/
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001727static int iwl4965_send_qos_params_command(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001728 struct iwl4965_qosparam_cmd *qos)
Zhu Yib481de92007-09-25 17:54:57 -07001729{
1730
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001731 return iwl4965_send_cmd_pdu(priv, REPLY_QOS_PARAM,
1732 sizeof(struct iwl4965_qosparam_cmd), qos);
Zhu Yib481de92007-09-25 17:54:57 -07001733}
1734
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001735static void iwl4965_reset_qos(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001736{
1737 u16 cw_min = 15;
1738 u16 cw_max = 1023;
1739 u8 aifs = 2;
1740 u8 is_legacy = 0;
1741 unsigned long flags;
1742 int i;
1743
1744 spin_lock_irqsave(&priv->lock, flags);
1745 priv->qos_data.qos_active = 0;
1746
1747 if (priv->iw_mode == IEEE80211_IF_TYPE_IBSS) {
1748 if (priv->qos_data.qos_enable)
1749 priv->qos_data.qos_active = 1;
1750 if (!(priv->active_rate & 0xfff0)) {
1751 cw_min = 31;
1752 is_legacy = 1;
1753 }
1754 } else if (priv->iw_mode == IEEE80211_IF_TYPE_AP) {
1755 if (priv->qos_data.qos_enable)
1756 priv->qos_data.qos_active = 1;
1757 } else if (!(priv->staging_rxon.flags & RXON_FLG_SHORT_SLOT_MSK)) {
1758 cw_min = 31;
1759 is_legacy = 1;
1760 }
1761
1762 if (priv->qos_data.qos_active)
1763 aifs = 3;
1764
1765 priv->qos_data.def_qos_parm.ac[0].cw_min = cpu_to_le16(cw_min);
1766 priv->qos_data.def_qos_parm.ac[0].cw_max = cpu_to_le16(cw_max);
1767 priv->qos_data.def_qos_parm.ac[0].aifsn = aifs;
1768 priv->qos_data.def_qos_parm.ac[0].edca_txop = 0;
1769 priv->qos_data.def_qos_parm.ac[0].reserved1 = 0;
1770
1771 if (priv->qos_data.qos_active) {
1772 i = 1;
1773 priv->qos_data.def_qos_parm.ac[i].cw_min = cpu_to_le16(cw_min);
1774 priv->qos_data.def_qos_parm.ac[i].cw_max = cpu_to_le16(cw_max);
1775 priv->qos_data.def_qos_parm.ac[i].aifsn = 7;
1776 priv->qos_data.def_qos_parm.ac[i].edca_txop = 0;
1777 priv->qos_data.def_qos_parm.ac[i].reserved1 = 0;
1778
1779 i = 2;
1780 priv->qos_data.def_qos_parm.ac[i].cw_min =
1781 cpu_to_le16((cw_min + 1) / 2 - 1);
1782 priv->qos_data.def_qos_parm.ac[i].cw_max =
1783 cpu_to_le16(cw_max);
1784 priv->qos_data.def_qos_parm.ac[i].aifsn = 2;
1785 if (is_legacy)
1786 priv->qos_data.def_qos_parm.ac[i].edca_txop =
1787 cpu_to_le16(6016);
1788 else
1789 priv->qos_data.def_qos_parm.ac[i].edca_txop =
1790 cpu_to_le16(3008);
1791 priv->qos_data.def_qos_parm.ac[i].reserved1 = 0;
1792
1793 i = 3;
1794 priv->qos_data.def_qos_parm.ac[i].cw_min =
1795 cpu_to_le16((cw_min + 1) / 4 - 1);
1796 priv->qos_data.def_qos_parm.ac[i].cw_max =
1797 cpu_to_le16((cw_max + 1) / 2 - 1);
1798 priv->qos_data.def_qos_parm.ac[i].aifsn = 2;
1799 priv->qos_data.def_qos_parm.ac[i].reserved1 = 0;
1800 if (is_legacy)
1801 priv->qos_data.def_qos_parm.ac[i].edca_txop =
1802 cpu_to_le16(3264);
1803 else
1804 priv->qos_data.def_qos_parm.ac[i].edca_txop =
1805 cpu_to_le16(1504);
1806 } else {
1807 for (i = 1; i < 4; i++) {
1808 priv->qos_data.def_qos_parm.ac[i].cw_min =
1809 cpu_to_le16(cw_min);
1810 priv->qos_data.def_qos_parm.ac[i].cw_max =
1811 cpu_to_le16(cw_max);
1812 priv->qos_data.def_qos_parm.ac[i].aifsn = aifs;
1813 priv->qos_data.def_qos_parm.ac[i].edca_txop = 0;
1814 priv->qos_data.def_qos_parm.ac[i].reserved1 = 0;
1815 }
1816 }
1817 IWL_DEBUG_QOS("set QoS to default \n");
1818
1819 spin_unlock_irqrestore(&priv->lock, flags);
1820}
1821
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001822static void iwl4965_activate_qos(struct iwl_priv *priv, u8 force)
Zhu Yib481de92007-09-25 17:54:57 -07001823{
1824 unsigned long flags;
1825
Zhu Yib481de92007-09-25 17:54:57 -07001826 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
1827 return;
1828
1829 if (!priv->qos_data.qos_enable)
1830 return;
1831
1832 spin_lock_irqsave(&priv->lock, flags);
1833 priv->qos_data.def_qos_parm.qos_flags = 0;
1834
1835 if (priv->qos_data.qos_cap.q_AP.queue_request &&
1836 !priv->qos_data.qos_cap.q_AP.txop_request)
1837 priv->qos_data.def_qos_parm.qos_flags |=
1838 QOS_PARAM_FLG_TXOP_TYPE_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07001839 if (priv->qos_data.qos_active)
1840 priv->qos_data.def_qos_parm.qos_flags |=
1841 QOS_PARAM_FLG_UPDATE_EDCA_MSK;
1842
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08001843#ifdef CONFIG_IWL4965_HT
Ron Rindjunskyfd105e72007-11-26 16:14:39 +02001844 if (priv->current_ht_config.is_ht)
Tomas Winklerf1f1f5c2007-10-25 17:15:26 +08001845 priv->qos_data.def_qos_parm.qos_flags |= QOS_PARAM_FLG_TGN_MSK;
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08001846#endif /* CONFIG_IWL4965_HT */
Tomas Winklerf1f1f5c2007-10-25 17:15:26 +08001847
Zhu Yib481de92007-09-25 17:54:57 -07001848 spin_unlock_irqrestore(&priv->lock, flags);
1849
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001850 if (force || iwl4965_is_associated(priv)) {
Tomas Winklerf1f1f5c2007-10-25 17:15:26 +08001851 IWL_DEBUG_QOS("send QoS cmd with Qos active=%d FLAGS=0x%X\n",
1852 priv->qos_data.qos_active,
1853 priv->qos_data.def_qos_parm.qos_flags);
Zhu Yib481de92007-09-25 17:54:57 -07001854
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001855 iwl4965_send_qos_params_command(priv,
Zhu Yib481de92007-09-25 17:54:57 -07001856 &(priv->qos_data.def_qos_parm));
1857 }
1858}
1859
Zhu Yib481de92007-09-25 17:54:57 -07001860/*
1861 * Power management (not Tx power!) functions
1862 */
1863#define MSEC_TO_USEC 1024
1864
1865#define NOSLP __constant_cpu_to_le16(0), 0, 0
1866#define SLP IWL_POWER_DRIVER_ALLOW_SLEEP_MSK, 0, 0
1867#define SLP_TIMEOUT(T) __constant_cpu_to_le32((T) * MSEC_TO_USEC)
1868#define SLP_VEC(X0, X1, X2, X3, X4) {__constant_cpu_to_le32(X0), \
1869 __constant_cpu_to_le32(X1), \
1870 __constant_cpu_to_le32(X2), \
1871 __constant_cpu_to_le32(X3), \
1872 __constant_cpu_to_le32(X4)}
1873
1874
1875/* default power management (not Tx power) table values */
1876/* for tim 0-10 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001877static struct iwl4965_power_vec_entry range_0[IWL_POWER_AC] = {
Zhu Yib481de92007-09-25 17:54:57 -07001878 {{NOSLP, SLP_TIMEOUT(0), SLP_TIMEOUT(0), SLP_VEC(0, 0, 0, 0, 0)}, 0},
1879 {{SLP, SLP_TIMEOUT(200), SLP_TIMEOUT(500), SLP_VEC(1, 2, 3, 4, 4)}, 0},
1880 {{SLP, SLP_TIMEOUT(200), SLP_TIMEOUT(300), SLP_VEC(2, 4, 6, 7, 7)}, 0},
1881 {{SLP, SLP_TIMEOUT(50), SLP_TIMEOUT(100), SLP_VEC(2, 6, 9, 9, 10)}, 0},
1882 {{SLP, SLP_TIMEOUT(50), SLP_TIMEOUT(25), SLP_VEC(2, 7, 9, 9, 10)}, 1},
1883 {{SLP, SLP_TIMEOUT(25), SLP_TIMEOUT(25), SLP_VEC(4, 7, 10, 10, 10)}, 1}
1884};
1885
1886/* for tim > 10 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001887static struct iwl4965_power_vec_entry range_1[IWL_POWER_AC] = {
Zhu Yib481de92007-09-25 17:54:57 -07001888 {{NOSLP, SLP_TIMEOUT(0), SLP_TIMEOUT(0), SLP_VEC(0, 0, 0, 0, 0)}, 0},
1889 {{SLP, SLP_TIMEOUT(200), SLP_TIMEOUT(500),
1890 SLP_VEC(1, 2, 3, 4, 0xFF)}, 0},
1891 {{SLP, SLP_TIMEOUT(200), SLP_TIMEOUT(300),
1892 SLP_VEC(2, 4, 6, 7, 0xFF)}, 0},
1893 {{SLP, SLP_TIMEOUT(50), SLP_TIMEOUT(100),
1894 SLP_VEC(2, 6, 9, 9, 0xFF)}, 0},
1895 {{SLP, SLP_TIMEOUT(50), SLP_TIMEOUT(25), SLP_VEC(2, 7, 9, 9, 0xFF)}, 0},
1896 {{SLP, SLP_TIMEOUT(25), SLP_TIMEOUT(25),
1897 SLP_VEC(4, 7, 10, 10, 0xFF)}, 0}
1898};
1899
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001900int iwl4965_power_init_handle(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001901{
1902 int rc = 0, i;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001903 struct iwl4965_power_mgr *pow_data;
1904 int size = sizeof(struct iwl4965_power_vec_entry) * IWL_POWER_AC;
Zhu Yib481de92007-09-25 17:54:57 -07001905 u16 pci_pm;
1906
1907 IWL_DEBUG_POWER("Initialize power \n");
1908
1909 pow_data = &(priv->power_data);
1910
1911 memset(pow_data, 0, sizeof(*pow_data));
1912
1913 pow_data->active_index = IWL_POWER_RANGE_0;
1914 pow_data->dtim_val = 0xffff;
1915
1916 memcpy(&pow_data->pwr_range_0[0], &range_0[0], size);
1917 memcpy(&pow_data->pwr_range_1[0], &range_1[0], size);
1918
1919 rc = pci_read_config_word(priv->pci_dev, PCI_LINK_CTRL, &pci_pm);
1920 if (rc != 0)
1921 return 0;
1922 else {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001923 struct iwl4965_powertable_cmd *cmd;
Zhu Yib481de92007-09-25 17:54:57 -07001924
1925 IWL_DEBUG_POWER("adjust power command flags\n");
1926
1927 for (i = 0; i < IWL_POWER_AC; i++) {
1928 cmd = &pow_data->pwr_range_0[i].cmd;
1929
1930 if (pci_pm & 0x1)
1931 cmd->flags &= ~IWL_POWER_PCI_PM_MSK;
1932 else
1933 cmd->flags |= IWL_POWER_PCI_PM_MSK;
1934 }
1935 }
1936 return rc;
1937}
1938
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001939static int iwl4965_update_power_cmd(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001940 struct iwl4965_powertable_cmd *cmd, u32 mode)
Zhu Yib481de92007-09-25 17:54:57 -07001941{
1942 int rc = 0, i;
1943 u8 skip;
1944 u32 max_sleep = 0;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001945 struct iwl4965_power_vec_entry *range;
Zhu Yib481de92007-09-25 17:54:57 -07001946 u8 period = 0;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001947 struct iwl4965_power_mgr *pow_data;
Zhu Yib481de92007-09-25 17:54:57 -07001948
1949 if (mode > IWL_POWER_INDEX_5) {
1950 IWL_DEBUG_POWER("Error invalid power mode \n");
1951 return -1;
1952 }
1953 pow_data = &(priv->power_data);
1954
1955 if (pow_data->active_index == IWL_POWER_RANGE_0)
1956 range = &pow_data->pwr_range_0[0];
1957 else
1958 range = &pow_data->pwr_range_1[1];
1959
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001960 memcpy(cmd, &range[mode].cmd, sizeof(struct iwl4965_powertable_cmd));
Zhu Yib481de92007-09-25 17:54:57 -07001961
1962#ifdef IWL_MAC80211_DISABLE
1963 if (priv->assoc_network != NULL) {
1964 unsigned long flags;
1965
1966 period = priv->assoc_network->tim.tim_period;
1967 }
1968#endif /*IWL_MAC80211_DISABLE */
1969 skip = range[mode].no_dtim;
1970
1971 if (period == 0) {
1972 period = 1;
1973 skip = 0;
1974 }
1975
1976 if (skip == 0) {
1977 max_sleep = period;
1978 cmd->flags &= ~IWL_POWER_SLEEP_OVER_DTIM_MSK;
1979 } else {
1980 __le32 slp_itrvl = cmd->sleep_interval[IWL_POWER_VEC_SIZE - 1];
1981 max_sleep = (le32_to_cpu(slp_itrvl) / period) * period;
1982 cmd->flags |= IWL_POWER_SLEEP_OVER_DTIM_MSK;
1983 }
1984
1985 for (i = 0; i < IWL_POWER_VEC_SIZE; i++) {
1986 if (le32_to_cpu(cmd->sleep_interval[i]) > max_sleep)
1987 cmd->sleep_interval[i] = cpu_to_le32(max_sleep);
1988 }
1989
1990 IWL_DEBUG_POWER("Flags value = 0x%08X\n", cmd->flags);
1991 IWL_DEBUG_POWER("Tx timeout = %u\n", le32_to_cpu(cmd->tx_data_timeout));
1992 IWL_DEBUG_POWER("Rx timeout = %u\n", le32_to_cpu(cmd->rx_data_timeout));
1993 IWL_DEBUG_POWER("Sleep interval vector = { %d , %d , %d , %d , %d }\n",
1994 le32_to_cpu(cmd->sleep_interval[0]),
1995 le32_to_cpu(cmd->sleep_interval[1]),
1996 le32_to_cpu(cmd->sleep_interval[2]),
1997 le32_to_cpu(cmd->sleep_interval[3]),
1998 le32_to_cpu(cmd->sleep_interval[4]));
1999
2000 return rc;
2001}
2002
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002003static int iwl4965_send_power_mode(struct iwl_priv *priv, u32 mode)
Zhu Yib481de92007-09-25 17:54:57 -07002004{
John W. Linville9a62f732007-11-15 16:27:36 -05002005 u32 uninitialized_var(final_mode);
Zhu Yib481de92007-09-25 17:54:57 -07002006 int rc;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002007 struct iwl4965_powertable_cmd cmd;
Zhu Yib481de92007-09-25 17:54:57 -07002008
2009 /* If on battery, set to 3,
Ian Schram01ebd062007-10-25 17:15:22 +08002010 * if plugged into AC power, set to CAM ("continuously aware mode"),
Zhu Yib481de92007-09-25 17:54:57 -07002011 * else user level */
2012 switch (mode) {
2013 case IWL_POWER_BATTERY:
2014 final_mode = IWL_POWER_INDEX_3;
2015 break;
2016 case IWL_POWER_AC:
2017 final_mode = IWL_POWER_MODE_CAM;
2018 break;
2019 default:
2020 final_mode = mode;
2021 break;
2022 }
2023
2024 cmd.keep_alive_beacons = 0;
2025
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002026 iwl4965_update_power_cmd(priv, &cmd, final_mode);
Zhu Yib481de92007-09-25 17:54:57 -07002027
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002028 rc = iwl4965_send_cmd_pdu(priv, POWER_TABLE_CMD, sizeof(cmd), &cmd);
Zhu Yib481de92007-09-25 17:54:57 -07002029
2030 if (final_mode == IWL_POWER_MODE_CAM)
2031 clear_bit(STATUS_POWER_PMI, &priv->status);
2032 else
2033 set_bit(STATUS_POWER_PMI, &priv->status);
2034
2035 return rc;
2036}
2037
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002038int iwl4965_is_network_packet(struct iwl_priv *priv, struct ieee80211_hdr *header)
Zhu Yib481de92007-09-25 17:54:57 -07002039{
2040 /* Filter incoming packets to determine if they are targeted toward
2041 * this network, discarding packets coming from ourselves */
2042 switch (priv->iw_mode) {
2043 case IEEE80211_IF_TYPE_IBSS: /* Header: Dest. | Source | BSSID */
2044 /* packets from our adapter are dropped (echo) */
2045 if (!compare_ether_addr(header->addr2, priv->mac_addr))
2046 return 0;
2047 /* {broad,multi}cast packets to our IBSS go through */
2048 if (is_multicast_ether_addr(header->addr1))
2049 return !compare_ether_addr(header->addr3, priv->bssid);
2050 /* packets to our adapter go through */
2051 return !compare_ether_addr(header->addr1, priv->mac_addr);
2052 case IEEE80211_IF_TYPE_STA: /* Header: Dest. | AP{BSSID} | Source */
2053 /* packets from our adapter are dropped (echo) */
2054 if (!compare_ether_addr(header->addr3, priv->mac_addr))
2055 return 0;
2056 /* {broad,multi}cast packets to our BSS go through */
2057 if (is_multicast_ether_addr(header->addr1))
2058 return !compare_ether_addr(header->addr2, priv->bssid);
2059 /* packets to our adapter go through */
2060 return !compare_ether_addr(header->addr1, priv->mac_addr);
2061 }
2062
2063 return 1;
2064}
2065
2066#define TX_STATUS_ENTRY(x) case TX_STATUS_FAIL_ ## x: return #x
2067
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002068static const char *iwl4965_get_tx_fail_reason(u32 status)
Zhu Yib481de92007-09-25 17:54:57 -07002069{
2070 switch (status & TX_STATUS_MSK) {
2071 case TX_STATUS_SUCCESS:
2072 return "SUCCESS";
2073 TX_STATUS_ENTRY(SHORT_LIMIT);
2074 TX_STATUS_ENTRY(LONG_LIMIT);
2075 TX_STATUS_ENTRY(FIFO_UNDERRUN);
2076 TX_STATUS_ENTRY(MGMNT_ABORT);
2077 TX_STATUS_ENTRY(NEXT_FRAG);
2078 TX_STATUS_ENTRY(LIFE_EXPIRE);
2079 TX_STATUS_ENTRY(DEST_PS);
2080 TX_STATUS_ENTRY(ABORTED);
2081 TX_STATUS_ENTRY(BT_RETRY);
2082 TX_STATUS_ENTRY(STA_INVALID);
2083 TX_STATUS_ENTRY(FRAG_DROPPED);
2084 TX_STATUS_ENTRY(TID_DISABLE);
2085 TX_STATUS_ENTRY(FRAME_FLUSHED);
2086 TX_STATUS_ENTRY(INSUFFICIENT_CF_POLL);
2087 TX_STATUS_ENTRY(TX_LOCKED);
2088 TX_STATUS_ENTRY(NO_BEACON_ON_RADAR);
2089 }
2090
2091 return "UNKNOWN";
2092}
2093
2094/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002095 * iwl4965_scan_cancel - Cancel any currently executing HW scan
Zhu Yib481de92007-09-25 17:54:57 -07002096 *
2097 * NOTE: priv->mutex is not required before calling this function
2098 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002099static int iwl4965_scan_cancel(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002100{
2101 if (!test_bit(STATUS_SCAN_HW, &priv->status)) {
2102 clear_bit(STATUS_SCANNING, &priv->status);
2103 return 0;
2104 }
2105
2106 if (test_bit(STATUS_SCANNING, &priv->status)) {
2107 if (!test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
2108 IWL_DEBUG_SCAN("Queuing scan abort.\n");
2109 set_bit(STATUS_SCAN_ABORTING, &priv->status);
2110 queue_work(priv->workqueue, &priv->abort_scan);
2111
2112 } else
2113 IWL_DEBUG_SCAN("Scan abort already in progress.\n");
2114
2115 return test_bit(STATUS_SCANNING, &priv->status);
2116 }
2117
2118 return 0;
2119}
2120
2121/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002122 * iwl4965_scan_cancel_timeout - Cancel any currently executing HW scan
Zhu Yib481de92007-09-25 17:54:57 -07002123 * @ms: amount of time to wait (in milliseconds) for scan to abort
2124 *
2125 * NOTE: priv->mutex must be held before calling this function
2126 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002127static int iwl4965_scan_cancel_timeout(struct iwl_priv *priv, unsigned long ms)
Zhu Yib481de92007-09-25 17:54:57 -07002128{
2129 unsigned long now = jiffies;
2130 int ret;
2131
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002132 ret = iwl4965_scan_cancel(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002133 if (ret && ms) {
2134 mutex_unlock(&priv->mutex);
2135 while (!time_after(jiffies, now + msecs_to_jiffies(ms)) &&
2136 test_bit(STATUS_SCANNING, &priv->status))
2137 msleep(1);
2138 mutex_lock(&priv->mutex);
2139
2140 return test_bit(STATUS_SCANNING, &priv->status);
2141 }
2142
2143 return ret;
2144}
2145
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002146static void iwl4965_sequence_reset(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002147{
2148 /* Reset ieee stats */
2149
2150 /* We don't reset the net_device_stats (ieee->stats) on
2151 * re-association */
2152
2153 priv->last_seq_num = -1;
2154 priv->last_frag_num = -1;
2155 priv->last_packet_time = 0;
2156
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002157 iwl4965_scan_cancel(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002158}
2159
2160#define MAX_UCODE_BEACON_INTERVAL 4096
2161#define INTEL_CONN_LISTEN_INTERVAL __constant_cpu_to_le16(0xA)
2162
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002163static __le16 iwl4965_adjust_beacon_interval(u16 beacon_val)
Zhu Yib481de92007-09-25 17:54:57 -07002164{
2165 u16 new_val = 0;
2166 u16 beacon_factor = 0;
2167
2168 beacon_factor =
2169 (beacon_val + MAX_UCODE_BEACON_INTERVAL)
2170 / MAX_UCODE_BEACON_INTERVAL;
2171 new_val = beacon_val / beacon_factor;
2172
2173 return cpu_to_le16(new_val);
2174}
2175
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002176static void iwl4965_setup_rxon_timing(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002177{
2178 u64 interval_tm_unit;
2179 u64 tsf, result;
2180 unsigned long flags;
2181 struct ieee80211_conf *conf = NULL;
2182 u16 beacon_int = 0;
2183
2184 conf = ieee80211_get_hw_conf(priv->hw);
2185
2186 spin_lock_irqsave(&priv->lock, flags);
2187 priv->rxon_timing.timestamp.dw[1] = cpu_to_le32(priv->timestamp1);
2188 priv->rxon_timing.timestamp.dw[0] = cpu_to_le32(priv->timestamp0);
2189
2190 priv->rxon_timing.listen_interval = INTEL_CONN_LISTEN_INTERVAL;
2191
2192 tsf = priv->timestamp1;
2193 tsf = ((tsf << 32) | priv->timestamp0);
2194
2195 beacon_int = priv->beacon_int;
2196 spin_unlock_irqrestore(&priv->lock, flags);
2197
2198 if (priv->iw_mode == IEEE80211_IF_TYPE_STA) {
2199 if (beacon_int == 0) {
2200 priv->rxon_timing.beacon_interval = cpu_to_le16(100);
2201 priv->rxon_timing.beacon_init_val = cpu_to_le32(102400);
2202 } else {
2203 priv->rxon_timing.beacon_interval =
2204 cpu_to_le16(beacon_int);
2205 priv->rxon_timing.beacon_interval =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002206 iwl4965_adjust_beacon_interval(
Zhu Yib481de92007-09-25 17:54:57 -07002207 le16_to_cpu(priv->rxon_timing.beacon_interval));
2208 }
2209
2210 priv->rxon_timing.atim_window = 0;
2211 } else {
2212 priv->rxon_timing.beacon_interval =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002213 iwl4965_adjust_beacon_interval(conf->beacon_int);
Zhu Yib481de92007-09-25 17:54:57 -07002214 /* TODO: we need to get atim_window from upper stack
2215 * for now we set to 0 */
2216 priv->rxon_timing.atim_window = 0;
2217 }
2218
2219 interval_tm_unit =
2220 (le16_to_cpu(priv->rxon_timing.beacon_interval) * 1024);
2221 result = do_div(tsf, interval_tm_unit);
2222 priv->rxon_timing.beacon_init_val =
2223 cpu_to_le32((u32) ((u64) interval_tm_unit - result));
2224
2225 IWL_DEBUG_ASSOC
2226 ("beacon interval %d beacon timer %d beacon tim %d\n",
2227 le16_to_cpu(priv->rxon_timing.beacon_interval),
2228 le32_to_cpu(priv->rxon_timing.beacon_init_val),
2229 le16_to_cpu(priv->rxon_timing.atim_window));
2230}
2231
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002232static int iwl4965_scan_initiate(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002233{
2234 if (priv->iw_mode == IEEE80211_IF_TYPE_AP) {
2235 IWL_ERROR("APs don't scan.\n");
2236 return 0;
2237 }
2238
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002239 if (!iwl4965_is_ready_rf(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07002240 IWL_DEBUG_SCAN("Aborting scan due to not ready.\n");
2241 return -EIO;
2242 }
2243
2244 if (test_bit(STATUS_SCANNING, &priv->status)) {
2245 IWL_DEBUG_SCAN("Scan already in progress.\n");
2246 return -EAGAIN;
2247 }
2248
2249 if (test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
2250 IWL_DEBUG_SCAN("Scan request while abort pending. "
2251 "Queuing.\n");
2252 return -EAGAIN;
2253 }
2254
2255 IWL_DEBUG_INFO("Starting scan...\n");
2256 priv->scan_bands = 2;
2257 set_bit(STATUS_SCANNING, &priv->status);
2258 priv->scan_start = jiffies;
2259 priv->scan_pass_start = priv->scan_start;
2260
2261 queue_work(priv->workqueue, &priv->request_scan);
2262
2263 return 0;
2264}
2265
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002266static int iwl4965_set_rxon_hwcrypto(struct iwl_priv *priv, int hw_decrypt)
Zhu Yib481de92007-09-25 17:54:57 -07002267{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002268 struct iwl4965_rxon_cmd *rxon = &priv->staging_rxon;
Zhu Yib481de92007-09-25 17:54:57 -07002269
2270 if (hw_decrypt)
2271 rxon->filter_flags &= ~RXON_FILTER_DIS_DECRYPT_MSK;
2272 else
2273 rxon->filter_flags |= RXON_FILTER_DIS_DECRYPT_MSK;
2274
2275 return 0;
2276}
2277
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002278static void iwl4965_set_flags_for_phymode(struct iwl_priv *priv,
Johannes Berg8318d782008-01-24 19:38:38 +01002279 enum ieee80211_band band)
Zhu Yib481de92007-09-25 17:54:57 -07002280{
Johannes Berg8318d782008-01-24 19:38:38 +01002281 if (band == IEEE80211_BAND_5GHZ) {
Zhu Yib481de92007-09-25 17:54:57 -07002282 priv->staging_rxon.flags &=
2283 ~(RXON_FLG_BAND_24G_MSK | RXON_FLG_AUTO_DETECT_MSK
2284 | RXON_FLG_CCK_MSK);
2285 priv->staging_rxon.flags |= RXON_FLG_SHORT_SLOT_MSK;
2286 } else {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002287 /* Copied from iwl4965_bg_post_associate() */
Zhu Yib481de92007-09-25 17:54:57 -07002288 if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_SLOT_TIME)
2289 priv->staging_rxon.flags |= RXON_FLG_SHORT_SLOT_MSK;
2290 else
2291 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
2292
2293 if (priv->iw_mode == IEEE80211_IF_TYPE_IBSS)
2294 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
2295
2296 priv->staging_rxon.flags |= RXON_FLG_BAND_24G_MSK;
2297 priv->staging_rxon.flags |= RXON_FLG_AUTO_DETECT_MSK;
2298 priv->staging_rxon.flags &= ~RXON_FLG_CCK_MSK;
2299 }
2300}
2301
2302/*
Ian Schram01ebd062007-10-25 17:15:22 +08002303 * initialize rxon structure with default values from eeprom
Zhu Yib481de92007-09-25 17:54:57 -07002304 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002305static void iwl4965_connection_init_rx_config(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002306{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002307 const struct iwl4965_channel_info *ch_info;
Zhu Yib481de92007-09-25 17:54:57 -07002308
2309 memset(&priv->staging_rxon, 0, sizeof(priv->staging_rxon));
2310
2311 switch (priv->iw_mode) {
2312 case IEEE80211_IF_TYPE_AP:
2313 priv->staging_rxon.dev_type = RXON_DEV_TYPE_AP;
2314 break;
2315
2316 case IEEE80211_IF_TYPE_STA:
2317 priv->staging_rxon.dev_type = RXON_DEV_TYPE_ESS;
2318 priv->staging_rxon.filter_flags = RXON_FILTER_ACCEPT_GRP_MSK;
2319 break;
2320
2321 case IEEE80211_IF_TYPE_IBSS:
2322 priv->staging_rxon.dev_type = RXON_DEV_TYPE_IBSS;
2323 priv->staging_rxon.flags = RXON_FLG_SHORT_PREAMBLE_MSK;
2324 priv->staging_rxon.filter_flags = RXON_FILTER_BCON_AWARE_MSK |
2325 RXON_FILTER_ACCEPT_GRP_MSK;
2326 break;
2327
2328 case IEEE80211_IF_TYPE_MNTR:
2329 priv->staging_rxon.dev_type = RXON_DEV_TYPE_SNIFFER;
2330 priv->staging_rxon.filter_flags = RXON_FILTER_PROMISC_MSK |
2331 RXON_FILTER_CTL2HOST_MSK | RXON_FILTER_ACCEPT_GRP_MSK;
2332 break;
2333 }
2334
2335#if 0
2336 /* TODO: Figure out when short_preamble would be set and cache from
2337 * that */
2338 if (!hw_to_local(priv->hw)->short_preamble)
2339 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_PREAMBLE_MSK;
2340 else
2341 priv->staging_rxon.flags |= RXON_FLG_SHORT_PREAMBLE_MSK;
2342#endif
2343
Johannes Berg8318d782008-01-24 19:38:38 +01002344 ch_info = iwl4965_get_channel_info(priv, priv->band,
Zhu Yib481de92007-09-25 17:54:57 -07002345 le16_to_cpu(priv->staging_rxon.channel));
2346
2347 if (!ch_info)
2348 ch_info = &priv->channel_info[0];
2349
2350 /*
2351 * in some case A channels are all non IBSS
2352 * in this case force B/G channel
2353 */
2354 if ((priv->iw_mode == IEEE80211_IF_TYPE_IBSS) &&
2355 !(is_channel_ibss(ch_info)))
2356 ch_info = &priv->channel_info[0];
2357
2358 priv->staging_rxon.channel = cpu_to_le16(ch_info->channel);
Johannes Berg8318d782008-01-24 19:38:38 +01002359 priv->band = ch_info->band;
Zhu Yib481de92007-09-25 17:54:57 -07002360
Johannes Berg8318d782008-01-24 19:38:38 +01002361 iwl4965_set_flags_for_phymode(priv, priv->band);
Zhu Yib481de92007-09-25 17:54:57 -07002362
2363 priv->staging_rxon.ofdm_basic_rates =
2364 (IWL_OFDM_RATES_MASK >> IWL_FIRST_OFDM_RATE) & 0xFF;
2365 priv->staging_rxon.cck_basic_rates =
2366 (IWL_CCK_RATES_MASK >> IWL_FIRST_CCK_RATE) & 0xF;
2367
2368 priv->staging_rxon.flags &= ~(RXON_FLG_CHANNEL_MODE_MIXED_MSK |
2369 RXON_FLG_CHANNEL_MODE_PURE_40_MSK);
2370 memcpy(priv->staging_rxon.node_addr, priv->mac_addr, ETH_ALEN);
2371 memcpy(priv->staging_rxon.wlap_bssid_addr, priv->mac_addr, ETH_ALEN);
2372 priv->staging_rxon.ofdm_ht_single_stream_basic_rates = 0xff;
2373 priv->staging_rxon.ofdm_ht_dual_stream_basic_rates = 0xff;
2374 iwl4965_set_rxon_chain(priv);
2375}
2376
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002377static int iwl4965_set_mode(struct iwl_priv *priv, int mode)
Zhu Yib481de92007-09-25 17:54:57 -07002378{
Zhu Yib481de92007-09-25 17:54:57 -07002379 if (mode == IEEE80211_IF_TYPE_IBSS) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002380 const struct iwl4965_channel_info *ch_info;
Zhu Yib481de92007-09-25 17:54:57 -07002381
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002382 ch_info = iwl4965_get_channel_info(priv,
Johannes Berg8318d782008-01-24 19:38:38 +01002383 priv->band,
Zhu Yib481de92007-09-25 17:54:57 -07002384 le16_to_cpu(priv->staging_rxon.channel));
2385
2386 if (!ch_info || !is_channel_ibss(ch_info)) {
2387 IWL_ERROR("channel %d not IBSS channel\n",
2388 le16_to_cpu(priv->staging_rxon.channel));
2389 return -EINVAL;
2390 }
2391 }
2392
Zhu Yib481de92007-09-25 17:54:57 -07002393 priv->iw_mode = mode;
2394
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002395 iwl4965_connection_init_rx_config(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002396 memcpy(priv->staging_rxon.node_addr, priv->mac_addr, ETH_ALEN);
2397
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002398 iwl4965_clear_stations_table(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002399
Mohamed Abbasfde35712007-11-29 11:10:15 +08002400 /* dont commit rxon if rf-kill is on*/
2401 if (!iwl4965_is_ready_rf(priv))
2402 return -EAGAIN;
2403
2404 cancel_delayed_work(&priv->scan_check);
2405 if (iwl4965_scan_cancel_timeout(priv, 100)) {
2406 IWL_WARNING("Aborted scan still in progress after 100ms\n");
2407 IWL_DEBUG_MAC80211("leaving - scan abort failed.\n");
2408 return -EAGAIN;
2409 }
2410
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002411 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002412
2413 return 0;
2414}
2415
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002416static void iwl4965_build_tx_cmd_hwcrypto(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07002417 struct ieee80211_tx_control *ctl,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002418 struct iwl4965_cmd *cmd,
Zhu Yib481de92007-09-25 17:54:57 -07002419 struct sk_buff *skb_frag,
2420 int last_frag)
2421{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002422 struct iwl4965_hw_key *keyinfo = &priv->stations[ctl->key_idx].keyinfo;
Zhu Yib481de92007-09-25 17:54:57 -07002423
2424 switch (keyinfo->alg) {
2425 case ALG_CCMP:
2426 cmd->cmd.tx.sec_ctl = TX_CMD_SEC_CCM;
2427 memcpy(cmd->cmd.tx.key, keyinfo->key, keyinfo->keylen);
Max Stepanov8236e182008-03-12 16:58:48 -07002428 if (ctl->flags & IEEE80211_TXCTL_AMPDU)
2429 cmd->cmd.tx.tx_flags |= TX_CMD_FLG_AGG_CCMP_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07002430 IWL_DEBUG_TX("tx_cmd with aes hwcrypto\n");
2431 break;
2432
2433 case ALG_TKIP:
2434#if 0
2435 cmd->cmd.tx.sec_ctl = TX_CMD_SEC_TKIP;
2436
2437 if (last_frag)
2438 memcpy(cmd->cmd.tx.tkip_mic.byte, skb_frag->tail - 8,
2439 8);
2440 else
2441 memset(cmd->cmd.tx.tkip_mic.byte, 0, 8);
2442#endif
2443 break;
2444
2445 case ALG_WEP:
2446 cmd->cmd.tx.sec_ctl = TX_CMD_SEC_WEP |
2447 (ctl->key_idx & TX_CMD_SEC_MSK) << TX_CMD_SEC_SHIFT;
2448
2449 if (keyinfo->keylen == 13)
2450 cmd->cmd.tx.sec_ctl |= TX_CMD_SEC_KEY128;
2451
2452 memcpy(&cmd->cmd.tx.key[3], keyinfo->key, keyinfo->keylen);
2453
2454 IWL_DEBUG_TX("Configuring packet for WEP encryption "
2455 "with key %d\n", ctl->key_idx);
2456 break;
2457
Zhu Yib481de92007-09-25 17:54:57 -07002458 default:
2459 printk(KERN_ERR "Unknown encode alg %d\n", keyinfo->alg);
2460 break;
2461 }
2462}
2463
2464/*
2465 * handle build REPLY_TX command notification.
2466 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002467static void iwl4965_build_tx_cmd_basic(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002468 struct iwl4965_cmd *cmd,
Zhu Yib481de92007-09-25 17:54:57 -07002469 struct ieee80211_tx_control *ctrl,
2470 struct ieee80211_hdr *hdr,
2471 int is_unicast, u8 std_id)
2472{
2473 __le16 *qc;
2474 u16 fc = le16_to_cpu(hdr->frame_control);
2475 __le32 tx_flags = cmd->cmd.tx.tx_flags;
2476
2477 cmd->cmd.tx.stop_time.life_time = TX_CMD_LIFE_TIME_INFINITE;
2478 if (!(ctrl->flags & IEEE80211_TXCTL_NO_ACK)) {
2479 tx_flags |= TX_CMD_FLG_ACK_MSK;
2480 if ((fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_MGMT)
2481 tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
2482 if (ieee80211_is_probe_response(fc) &&
2483 !(le16_to_cpu(hdr->seq_ctrl) & 0xf))
2484 tx_flags |= TX_CMD_FLG_TSF_MSK;
2485 } else {
2486 tx_flags &= (~TX_CMD_FLG_ACK_MSK);
2487 tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
2488 }
2489
Tomas Winkler87e4f7d2008-01-14 17:46:16 -08002490 if (ieee80211_is_back_request(fc))
2491 tx_flags |= TX_CMD_FLG_ACK_MSK | TX_CMD_FLG_IMM_BA_RSP_MASK;
2492
2493
Zhu Yib481de92007-09-25 17:54:57 -07002494 cmd->cmd.tx.sta_id = std_id;
2495 if (ieee80211_get_morefrag(hdr))
2496 tx_flags |= TX_CMD_FLG_MORE_FRAG_MSK;
2497
2498 qc = ieee80211_get_qos_ctrl(hdr);
2499 if (qc) {
2500 cmd->cmd.tx.tid_tspec = (u8) (le16_to_cpu(*qc) & 0xf);
2501 tx_flags &= ~TX_CMD_FLG_SEQ_CTL_MSK;
2502 } else
2503 tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
2504
2505 if (ctrl->flags & IEEE80211_TXCTL_USE_RTS_CTS) {
2506 tx_flags |= TX_CMD_FLG_RTS_MSK;
2507 tx_flags &= ~TX_CMD_FLG_CTS_MSK;
2508 } else if (ctrl->flags & IEEE80211_TXCTL_USE_CTS_PROTECT) {
2509 tx_flags &= ~TX_CMD_FLG_RTS_MSK;
2510 tx_flags |= TX_CMD_FLG_CTS_MSK;
2511 }
2512
2513 if ((tx_flags & TX_CMD_FLG_RTS_MSK) || (tx_flags & TX_CMD_FLG_CTS_MSK))
2514 tx_flags |= TX_CMD_FLG_FULL_TXOP_PROT_MSK;
2515
2516 tx_flags &= ~(TX_CMD_FLG_ANT_SEL_MSK);
2517 if ((fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_MGMT) {
2518 if ((fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_ASSOC_REQ ||
2519 (fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_REASSOC_REQ)
Ian Schrambc434dd2007-10-25 17:15:29 +08002520 cmd->cmd.tx.timeout.pm_frame_timeout = cpu_to_le16(3);
Zhu Yib481de92007-09-25 17:54:57 -07002521 else
Ian Schrambc434dd2007-10-25 17:15:29 +08002522 cmd->cmd.tx.timeout.pm_frame_timeout = cpu_to_le16(2);
Zhu Yib481de92007-09-25 17:54:57 -07002523 } else
2524 cmd->cmd.tx.timeout.pm_frame_timeout = 0;
2525
2526 cmd->cmd.tx.driver_txop = 0;
2527 cmd->cmd.tx.tx_flags = tx_flags;
2528 cmd->cmd.tx.next_frame_len = 0;
2529}
Tomas Winkler19758be2008-03-12 16:58:51 -07002530static void iwl_update_tx_stats(struct iwl_priv *priv, u16 fc, u16 len)
2531{
2532 /* 0 - mgmt, 1 - cnt, 2 - data */
2533 int idx = (fc & IEEE80211_FCTL_FTYPE) >> 2;
2534 priv->tx_stats[idx].cnt++;
2535 priv->tx_stats[idx].bytes += len;
2536}
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002537/**
2538 * iwl4965_get_sta_id - Find station's index within station table
2539 *
2540 * If new IBSS station, create new entry in station table
2541 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002542static int iwl4965_get_sta_id(struct iwl_priv *priv,
Ben Cahill9fbab512007-11-29 11:09:47 +08002543 struct ieee80211_hdr *hdr)
Zhu Yib481de92007-09-25 17:54:57 -07002544{
2545 int sta_id;
2546 u16 fc = le16_to_cpu(hdr->frame_control);
Joe Perches0795af52007-10-03 17:59:30 -07002547 DECLARE_MAC_BUF(mac);
Zhu Yib481de92007-09-25 17:54:57 -07002548
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002549 /* If this frame is broadcast or management, use broadcast station id */
Zhu Yib481de92007-09-25 17:54:57 -07002550 if (((fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_DATA) ||
2551 is_multicast_ether_addr(hdr->addr1))
2552 return priv->hw_setting.bcast_sta_id;
2553
2554 switch (priv->iw_mode) {
2555
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002556 /* If we are a client station in a BSS network, use the special
2557 * AP station entry (that's the only station we communicate with) */
Zhu Yib481de92007-09-25 17:54:57 -07002558 case IEEE80211_IF_TYPE_STA:
2559 return IWL_AP_ID;
2560
2561 /* If we are an AP, then find the station, or use BCAST */
2562 case IEEE80211_IF_TYPE_AP:
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002563 sta_id = iwl4965_hw_find_station(priv, hdr->addr1);
Zhu Yib481de92007-09-25 17:54:57 -07002564 if (sta_id != IWL_INVALID_STATION)
2565 return sta_id;
2566 return priv->hw_setting.bcast_sta_id;
2567
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002568 /* If this frame is going out to an IBSS network, find the station,
2569 * or create a new station table entry */
Zhu Yib481de92007-09-25 17:54:57 -07002570 case IEEE80211_IF_TYPE_IBSS:
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002571 sta_id = iwl4965_hw_find_station(priv, hdr->addr1);
Zhu Yib481de92007-09-25 17:54:57 -07002572 if (sta_id != IWL_INVALID_STATION)
2573 return sta_id;
2574
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002575 /* Create new station table entry */
Ron Rindjunsky67d62032007-11-26 16:14:40 +02002576 sta_id = iwl4965_add_station_flags(priv, hdr->addr1,
2577 0, CMD_ASYNC, NULL);
Zhu Yib481de92007-09-25 17:54:57 -07002578
2579 if (sta_id != IWL_INVALID_STATION)
2580 return sta_id;
2581
Joe Perches0795af52007-10-03 17:59:30 -07002582 IWL_DEBUG_DROP("Station %s not in station map. "
Zhu Yib481de92007-09-25 17:54:57 -07002583 "Defaulting to broadcast...\n",
Joe Perches0795af52007-10-03 17:59:30 -07002584 print_mac(mac, hdr->addr1));
Tomas Winkler0a6857e2008-03-12 16:58:49 -07002585 iwl_print_hex_dump(IWL_DL_DROP, (u8 *) hdr, sizeof(*hdr));
Zhu Yib481de92007-09-25 17:54:57 -07002586 return priv->hw_setting.bcast_sta_id;
2587
2588 default:
Ian Schram01ebd062007-10-25 17:15:22 +08002589 IWL_WARNING("Unknown mode of operation: %d", priv->iw_mode);
Zhu Yib481de92007-09-25 17:54:57 -07002590 return priv->hw_setting.bcast_sta_id;
2591 }
2592}
2593
2594/*
2595 * start REPLY_TX command process
2596 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002597static int iwl4965_tx_skb(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07002598 struct sk_buff *skb, struct ieee80211_tx_control *ctl)
2599{
2600 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002601 struct iwl4965_tfd_frame *tfd;
Zhu Yib481de92007-09-25 17:54:57 -07002602 u32 *control_flags;
2603 int txq_id = ctl->queue;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002604 struct iwl4965_tx_queue *txq = NULL;
2605 struct iwl4965_queue *q = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07002606 dma_addr_t phys_addr;
2607 dma_addr_t txcmd_phys;
Tomas Winkler87e4f7d2008-01-14 17:46:16 -08002608 dma_addr_t scratch_phys;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002609 struct iwl4965_cmd *out_cmd = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07002610 u16 len, idx, len_org;
2611 u8 id, hdr_len, unicast;
2612 u8 sta_id;
2613 u16 seq_number = 0;
2614 u16 fc;
2615 __le16 *qc;
2616 u8 wait_write_ptr = 0;
2617 unsigned long flags;
2618 int rc;
2619
2620 spin_lock_irqsave(&priv->lock, flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002621 if (iwl4965_is_rfkill(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07002622 IWL_DEBUG_DROP("Dropping - RF KILL\n");
2623 goto drop_unlock;
2624 }
2625
Johannes Berg32bfd352007-12-19 01:31:26 +01002626 if (!priv->vif) {
2627 IWL_DEBUG_DROP("Dropping - !priv->vif\n");
Zhu Yib481de92007-09-25 17:54:57 -07002628 goto drop_unlock;
2629 }
2630
Johannes Berg8318d782008-01-24 19:38:38 +01002631 if ((ctl->tx_rate->hw_value & 0xFF) == IWL_INVALID_RATE) {
Zhu Yib481de92007-09-25 17:54:57 -07002632 IWL_ERROR("ERROR: No TX rate available.\n");
2633 goto drop_unlock;
2634 }
2635
2636 unicast = !is_multicast_ether_addr(hdr->addr1);
2637 id = 0;
2638
2639 fc = le16_to_cpu(hdr->frame_control);
2640
Tomas Winkler0a6857e2008-03-12 16:58:49 -07002641#ifdef CONFIG_IWLWIFI_DEBUG
Zhu Yib481de92007-09-25 17:54:57 -07002642 if (ieee80211_is_auth(fc))
2643 IWL_DEBUG_TX("Sending AUTH frame\n");
2644 else if (ieee80211_is_assoc_request(fc))
2645 IWL_DEBUG_TX("Sending ASSOC frame\n");
2646 else if (ieee80211_is_reassoc_request(fc))
2647 IWL_DEBUG_TX("Sending REASSOC frame\n");
2648#endif
2649
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08002650 /* drop all data frame if we are not associated */
Gregory Greenman76f39152008-01-23 10:15:21 -08002651 if (((fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_DATA) &&
2652 (!iwl4965_is_associated(priv) ||
Reinette Chatrea6477242008-02-14 10:40:28 -08002653 ((priv->iw_mode == IEEE80211_IF_TYPE_STA) && !priv->assoc_id) ||
Gregory Greenman76f39152008-01-23 10:15:21 -08002654 !priv->assoc_station_added)) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002655 IWL_DEBUG_DROP("Dropping - !iwl4965_is_associated\n");
Zhu Yib481de92007-09-25 17:54:57 -07002656 goto drop_unlock;
2657 }
2658
2659 spin_unlock_irqrestore(&priv->lock, flags);
2660
2661 hdr_len = ieee80211_get_hdrlen(fc);
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002662
2663 /* Find (or create) index into station table for destination station */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002664 sta_id = iwl4965_get_sta_id(priv, hdr);
Zhu Yib481de92007-09-25 17:54:57 -07002665 if (sta_id == IWL_INVALID_STATION) {
Joe Perches0795af52007-10-03 17:59:30 -07002666 DECLARE_MAC_BUF(mac);
2667
2668 IWL_DEBUG_DROP("Dropping - INVALID STATION: %s\n",
2669 print_mac(mac, hdr->addr1));
Zhu Yib481de92007-09-25 17:54:57 -07002670 goto drop;
2671 }
2672
2673 IWL_DEBUG_RATE("station Id %d\n", sta_id);
2674
2675 qc = ieee80211_get_qos_ctrl(hdr);
2676 if (qc) {
2677 u8 tid = (u8)(le16_to_cpu(*qc) & 0xf);
2678 seq_number = priv->stations[sta_id].tid[tid].seq_number &
2679 IEEE80211_SCTL_SEQ;
2680 hdr->seq_ctrl = cpu_to_le16(seq_number) |
2681 (hdr->seq_ctrl &
2682 __constant_cpu_to_le16(IEEE80211_SCTL_FRAG));
2683 seq_number += 0x10;
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08002684#ifdef CONFIG_IWL4965_HT
Zhu Yib481de92007-09-25 17:54:57 -07002685 /* aggregation is on for this <sta,tid> */
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02002686 if (ctl->flags & IEEE80211_TXCTL_AMPDU)
Zhu Yib481de92007-09-25 17:54:57 -07002687 txq_id = priv->stations[sta_id].tid[tid].agg.txq_id;
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02002688 priv->stations[sta_id].tid[tid].tfds_in_queue++;
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08002689#endif /* CONFIG_IWL4965_HT */
Zhu Yib481de92007-09-25 17:54:57 -07002690 }
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002691
2692 /* Descriptor for chosen Tx queue */
Zhu Yib481de92007-09-25 17:54:57 -07002693 txq = &priv->txq[txq_id];
2694 q = &txq->q;
2695
2696 spin_lock_irqsave(&priv->lock, flags);
2697
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002698 /* Set up first empty TFD within this queue's circular TFD buffer */
Tomas Winklerfc4b6852007-10-25 17:15:24 +08002699 tfd = &txq->bd[q->write_ptr];
Zhu Yib481de92007-09-25 17:54:57 -07002700 memset(tfd, 0, sizeof(*tfd));
2701 control_flags = (u32 *) tfd;
Tomas Winklerfc4b6852007-10-25 17:15:24 +08002702 idx = get_cmd_index(q, q->write_ptr, 0);
Zhu Yib481de92007-09-25 17:54:57 -07002703
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002704 /* Set up driver data for this TFD */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002705 memset(&(txq->txb[q->write_ptr]), 0, sizeof(struct iwl4965_tx_info));
Tomas Winklerfc4b6852007-10-25 17:15:24 +08002706 txq->txb[q->write_ptr].skb[0] = skb;
2707 memcpy(&(txq->txb[q->write_ptr].status.control),
Zhu Yib481de92007-09-25 17:54:57 -07002708 ctl, sizeof(struct ieee80211_tx_control));
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002709
2710 /* Set up first empty entry in queue's array of Tx/cmd buffers */
Zhu Yib481de92007-09-25 17:54:57 -07002711 out_cmd = &txq->cmd[idx];
2712 memset(&out_cmd->hdr, 0, sizeof(out_cmd->hdr));
2713 memset(&out_cmd->cmd.tx, 0, sizeof(out_cmd->cmd.tx));
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002714
2715 /*
2716 * Set up the Tx-command (not MAC!) header.
2717 * Store the chosen Tx queue and TFD index within the sequence field;
2718 * after Tx, uCode's Tx response will return this value so driver can
2719 * locate the frame within the tx queue and do post-tx processing.
2720 */
Zhu Yib481de92007-09-25 17:54:57 -07002721 out_cmd->hdr.cmd = REPLY_TX;
2722 out_cmd->hdr.sequence = cpu_to_le16((u16)(QUEUE_TO_SEQ(txq_id) |
Tomas Winklerfc4b6852007-10-25 17:15:24 +08002723 INDEX_TO_SEQ(q->write_ptr)));
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002724
2725 /* Copy MAC header from skb into command buffer */
Zhu Yib481de92007-09-25 17:54:57 -07002726 memcpy(out_cmd->cmd.tx.hdr, hdr, hdr_len);
2727
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002728 /*
2729 * Use the first empty entry in this queue's command buffer array
2730 * to contain the Tx command and MAC header concatenated together
2731 * (payload data will be in another buffer).
2732 * Size of this varies, due to varying MAC header length.
2733 * If end is not dword aligned, we'll have 2 extra bytes at the end
2734 * of the MAC header (device reads on dword boundaries).
2735 * We'll tell device about this padding later.
2736 */
Zhu Yib481de92007-09-25 17:54:57 -07002737 len = priv->hw_setting.tx_cmd_len +
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002738 sizeof(struct iwl4965_cmd_header) + hdr_len;
Zhu Yib481de92007-09-25 17:54:57 -07002739
2740 len_org = len;
2741 len = (len + 3) & ~3;
2742
2743 if (len_org != len)
2744 len_org = 1;
2745 else
2746 len_org = 0;
2747
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002748 /* Physical address of this Tx command's header (not MAC header!),
2749 * within command buffer array. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002750 txcmd_phys = txq->dma_addr_cmd + sizeof(struct iwl4965_cmd) * idx +
2751 offsetof(struct iwl4965_cmd, hdr);
Zhu Yib481de92007-09-25 17:54:57 -07002752
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002753 /* Add buffer containing Tx command and MAC(!) header to TFD's
2754 * first entry */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002755 iwl4965_hw_txq_attach_buf_to_tfd(priv, tfd, txcmd_phys, len);
Zhu Yib481de92007-09-25 17:54:57 -07002756
2757 if (!(ctl->flags & IEEE80211_TXCTL_DO_NOT_ENCRYPT))
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002758 iwl4965_build_tx_cmd_hwcrypto(priv, ctl, out_cmd, skb, 0);
Zhu Yib481de92007-09-25 17:54:57 -07002759
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002760 /* Set up TFD's 2nd entry to point directly to remainder of skb,
2761 * if any (802.11 null frames have no payload). */
Zhu Yib481de92007-09-25 17:54:57 -07002762 len = skb->len - hdr_len;
2763 if (len) {
2764 phys_addr = pci_map_single(priv->pci_dev, skb->data + hdr_len,
2765 len, PCI_DMA_TODEVICE);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002766 iwl4965_hw_txq_attach_buf_to_tfd(priv, tfd, phys_addr, len);
Zhu Yib481de92007-09-25 17:54:57 -07002767 }
2768
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002769 /* Tell 4965 about any 2-byte padding after MAC header */
Zhu Yib481de92007-09-25 17:54:57 -07002770 if (len_org)
2771 out_cmd->cmd.tx.tx_flags |= TX_CMD_FLG_MH_PAD_MSK;
2772
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002773 /* Total # bytes to be transmitted */
Zhu Yib481de92007-09-25 17:54:57 -07002774 len = (u16)skb->len;
2775 out_cmd->cmd.tx.len = cpu_to_le16(len);
2776
2777 /* TODO need this for burst mode later on */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002778 iwl4965_build_tx_cmd_basic(priv, out_cmd, ctl, hdr, unicast, sta_id);
Zhu Yib481de92007-09-25 17:54:57 -07002779
2780 /* set is_hcca to 0; it probably will never be implemented */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002781 iwl4965_hw_build_tx_cmd_rate(priv, out_cmd, ctl, hdr, sta_id, 0);
Zhu Yib481de92007-09-25 17:54:57 -07002782
Tomas Winkler19758be2008-03-12 16:58:51 -07002783 iwl_update_tx_stats(priv, fc, len);
2784
Tomas Winkler87e4f7d2008-01-14 17:46:16 -08002785 scratch_phys = txcmd_phys + sizeof(struct iwl4965_cmd_header) +
2786 offsetof(struct iwl4965_tx_cmd, scratch);
2787 out_cmd->cmd.tx.dram_lsb_ptr = cpu_to_le32(scratch_phys);
2788 out_cmd->cmd.tx.dram_msb_ptr = iwl_get_dma_hi_address(scratch_phys);
2789
Zhu Yib481de92007-09-25 17:54:57 -07002790 if (!ieee80211_get_morefrag(hdr)) {
2791 txq->need_update = 1;
2792 if (qc) {
2793 u8 tid = (u8)(le16_to_cpu(*qc) & 0xf);
2794 priv->stations[sta_id].tid[tid].seq_number = seq_number;
2795 }
2796 } else {
2797 wait_write_ptr = 1;
2798 txq->need_update = 0;
2799 }
2800
Tomas Winkler0a6857e2008-03-12 16:58:49 -07002801 iwl_print_hex_dump(IWL_DL_TX, out_cmd->cmd.payload,
Zhu Yib481de92007-09-25 17:54:57 -07002802 sizeof(out_cmd->cmd.tx));
2803
Tomas Winkler0a6857e2008-03-12 16:58:49 -07002804 iwl_print_hex_dump(IWL_DL_TX, (u8 *)out_cmd->cmd.tx.hdr,
Zhu Yib481de92007-09-25 17:54:57 -07002805 ieee80211_get_hdrlen(fc));
2806
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002807 /* Set up entry for this TFD in Tx byte-count array */
Zhu Yib481de92007-09-25 17:54:57 -07002808 iwl4965_tx_queue_update_wr_ptr(priv, txq, len);
2809
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002810 /* Tell device the write index *just past* this latest filled TFD */
Tomas Winklerc54b6792008-03-06 17:36:53 -08002811 q->write_ptr = iwl_queue_inc_wrap(q->write_ptr, q->n_bd);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002812 rc = iwl4965_tx_queue_update_write_ptr(priv, txq);
Zhu Yib481de92007-09-25 17:54:57 -07002813 spin_unlock_irqrestore(&priv->lock, flags);
2814
2815 if (rc)
2816 return rc;
2817
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002818 if ((iwl4965_queue_space(q) < q->high_mark)
Zhu Yib481de92007-09-25 17:54:57 -07002819 && priv->mac80211_registered) {
2820 if (wait_write_ptr) {
2821 spin_lock_irqsave(&priv->lock, flags);
2822 txq->need_update = 1;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002823 iwl4965_tx_queue_update_write_ptr(priv, txq);
Zhu Yib481de92007-09-25 17:54:57 -07002824 spin_unlock_irqrestore(&priv->lock, flags);
2825 }
2826
2827 ieee80211_stop_queue(priv->hw, ctl->queue);
2828 }
2829
2830 return 0;
2831
2832drop_unlock:
2833 spin_unlock_irqrestore(&priv->lock, flags);
2834drop:
2835 return -1;
2836}
2837
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002838static void iwl4965_set_rate(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002839{
Johannes Berg8318d782008-01-24 19:38:38 +01002840 const struct ieee80211_supported_band *hw = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07002841 struct ieee80211_rate *rate;
2842 int i;
2843
Johannes Berg8318d782008-01-24 19:38:38 +01002844 hw = iwl4965_get_hw_mode(priv, priv->band);
Saleem Abdulrasoolc4ba9622007-11-18 23:59:08 -08002845 if (!hw) {
2846 IWL_ERROR("Failed to set rate: unable to get hw mode\n");
2847 return;
2848 }
Zhu Yib481de92007-09-25 17:54:57 -07002849
2850 priv->active_rate = 0;
2851 priv->active_rate_basic = 0;
2852
Johannes Berg8318d782008-01-24 19:38:38 +01002853 for (i = 0; i < hw->n_bitrates; i++) {
2854 rate = &(hw->bitrates[i]);
2855 if (rate->hw_value < IWL_RATE_COUNT)
2856 priv->active_rate |= (1 << rate->hw_value);
Zhu Yib481de92007-09-25 17:54:57 -07002857 }
2858
2859 IWL_DEBUG_RATE("Set active_rate = %0x, active_rate_basic = %0x\n",
2860 priv->active_rate, priv->active_rate_basic);
2861
2862 /*
2863 * If a basic rate is configured, then use it (adding IWL_RATE_1M_MASK)
2864 * otherwise set it to the default of all CCK rates and 6, 12, 24 for
2865 * OFDM
2866 */
2867 if (priv->active_rate_basic & IWL_CCK_BASIC_RATES_MASK)
2868 priv->staging_rxon.cck_basic_rates =
2869 ((priv->active_rate_basic &
2870 IWL_CCK_RATES_MASK) >> IWL_FIRST_CCK_RATE) & 0xF;
2871 else
2872 priv->staging_rxon.cck_basic_rates =
2873 (IWL_CCK_BASIC_RATES_MASK >> IWL_FIRST_CCK_RATE) & 0xF;
2874
2875 if (priv->active_rate_basic & IWL_OFDM_BASIC_RATES_MASK)
2876 priv->staging_rxon.ofdm_basic_rates =
2877 ((priv->active_rate_basic &
2878 (IWL_OFDM_BASIC_RATES_MASK | IWL_RATE_6M_MASK)) >>
2879 IWL_FIRST_OFDM_RATE) & 0xFF;
2880 else
2881 priv->staging_rxon.ofdm_basic_rates =
2882 (IWL_OFDM_BASIC_RATES_MASK >> IWL_FIRST_OFDM_RATE) & 0xFF;
2883}
2884
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002885static void iwl4965_radio_kill_sw(struct iwl_priv *priv, int disable_radio)
Zhu Yib481de92007-09-25 17:54:57 -07002886{
2887 unsigned long flags;
2888
2889 if (!!disable_radio == test_bit(STATUS_RF_KILL_SW, &priv->status))
2890 return;
2891
2892 IWL_DEBUG_RF_KILL("Manual SW RF KILL set to: RADIO %s\n",
2893 disable_radio ? "OFF" : "ON");
2894
2895 if (disable_radio) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002896 iwl4965_scan_cancel(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002897 /* FIXME: This is a workaround for AP */
2898 if (priv->iw_mode != IEEE80211_IF_TYPE_AP) {
2899 spin_lock_irqsave(&priv->lock, flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002900 iwl4965_write32(priv, CSR_UCODE_DRV_GP1_SET,
Zhu Yib481de92007-09-25 17:54:57 -07002901 CSR_UCODE_SW_BIT_RFKILL);
2902 spin_unlock_irqrestore(&priv->lock, flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002903 iwl4965_send_card_state(priv, CARD_STATE_CMD_DISABLE, 0);
Zhu Yib481de92007-09-25 17:54:57 -07002904 set_bit(STATUS_RF_KILL_SW, &priv->status);
2905 }
2906 return;
2907 }
2908
2909 spin_lock_irqsave(&priv->lock, flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002910 iwl4965_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
Zhu Yib481de92007-09-25 17:54:57 -07002911
2912 clear_bit(STATUS_RF_KILL_SW, &priv->status);
2913 spin_unlock_irqrestore(&priv->lock, flags);
2914
2915 /* wake up ucode */
2916 msleep(10);
2917
2918 spin_lock_irqsave(&priv->lock, flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002919 iwl4965_read32(priv, CSR_UCODE_DRV_GP1);
2920 if (!iwl4965_grab_nic_access(priv))
2921 iwl4965_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002922 spin_unlock_irqrestore(&priv->lock, flags);
2923
2924 if (test_bit(STATUS_RF_KILL_HW, &priv->status)) {
2925 IWL_DEBUG_RF_KILL("Can not turn radio back on - "
2926 "disabled by HW switch\n");
2927 return;
2928 }
2929
2930 queue_work(priv->workqueue, &priv->restart);
2931 return;
2932}
2933
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002934void iwl4965_set_decrypted_flag(struct iwl_priv *priv, struct sk_buff *skb,
Zhu Yib481de92007-09-25 17:54:57 -07002935 u32 decrypt_res, struct ieee80211_rx_status *stats)
2936{
2937 u16 fc =
2938 le16_to_cpu(((struct ieee80211_hdr *)skb->data)->frame_control);
2939
2940 if (priv->active_rxon.filter_flags & RXON_FILTER_DIS_DECRYPT_MSK)
2941 return;
2942
2943 if (!(fc & IEEE80211_FCTL_PROTECTED))
2944 return;
2945
2946 IWL_DEBUG_RX("decrypt_res:0x%x\n", decrypt_res);
2947 switch (decrypt_res & RX_RES_STATUS_SEC_TYPE_MSK) {
2948 case RX_RES_STATUS_SEC_TYPE_TKIP:
2949 if ((decrypt_res & RX_RES_STATUS_DECRYPT_TYPE_MSK) ==
2950 RX_RES_STATUS_BAD_ICV_MIC)
2951 stats->flag |= RX_FLAG_MMIC_ERROR;
2952 case RX_RES_STATUS_SEC_TYPE_WEP:
2953 case RX_RES_STATUS_SEC_TYPE_CCMP:
2954 if ((decrypt_res & RX_RES_STATUS_DECRYPT_TYPE_MSK) ==
2955 RX_RES_STATUS_DECRYPT_OK) {
2956 IWL_DEBUG_RX("hw decrypt successfully!!!\n");
2957 stats->flag |= RX_FLAG_DECRYPTED;
2958 }
2959 break;
2960
2961 default:
2962 break;
2963 }
2964}
2965
Zhu Yib481de92007-09-25 17:54:57 -07002966
2967#define IWL_PACKET_RETRY_TIME HZ
2968
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002969int iwl4965_is_duplicate_packet(struct iwl_priv *priv, struct ieee80211_hdr *header)
Zhu Yib481de92007-09-25 17:54:57 -07002970{
2971 u16 sc = le16_to_cpu(header->seq_ctrl);
2972 u16 seq = (sc & IEEE80211_SCTL_SEQ) >> 4;
2973 u16 frag = sc & IEEE80211_SCTL_FRAG;
2974 u16 *last_seq, *last_frag;
2975 unsigned long *last_time;
2976
2977 switch (priv->iw_mode) {
2978 case IEEE80211_IF_TYPE_IBSS:{
2979 struct list_head *p;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002980 struct iwl4965_ibss_seq *entry = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07002981 u8 *mac = header->addr2;
2982 int index = mac[5] & (IWL_IBSS_MAC_HASH_SIZE - 1);
2983
2984 __list_for_each(p, &priv->ibss_mac_hash[index]) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002985 entry = list_entry(p, struct iwl4965_ibss_seq, list);
Zhu Yib481de92007-09-25 17:54:57 -07002986 if (!compare_ether_addr(entry->mac, mac))
2987 break;
2988 }
2989 if (p == &priv->ibss_mac_hash[index]) {
2990 entry = kzalloc(sizeof(*entry), GFP_ATOMIC);
2991 if (!entry) {
Ian Schrambc434dd2007-10-25 17:15:29 +08002992 IWL_ERROR("Cannot malloc new mac entry\n");
Zhu Yib481de92007-09-25 17:54:57 -07002993 return 0;
2994 }
2995 memcpy(entry->mac, mac, ETH_ALEN);
2996 entry->seq_num = seq;
2997 entry->frag_num = frag;
2998 entry->packet_time = jiffies;
Ian Schrambc434dd2007-10-25 17:15:29 +08002999 list_add(&entry->list, &priv->ibss_mac_hash[index]);
Zhu Yib481de92007-09-25 17:54:57 -07003000 return 0;
3001 }
3002 last_seq = &entry->seq_num;
3003 last_frag = &entry->frag_num;
3004 last_time = &entry->packet_time;
3005 break;
3006 }
3007 case IEEE80211_IF_TYPE_STA:
3008 last_seq = &priv->last_seq_num;
3009 last_frag = &priv->last_frag_num;
3010 last_time = &priv->last_packet_time;
3011 break;
3012 default:
3013 return 0;
3014 }
3015 if ((*last_seq == seq) &&
3016 time_after(*last_time + IWL_PACKET_RETRY_TIME, jiffies)) {
3017 if (*last_frag == frag)
3018 goto drop;
3019 if (*last_frag + 1 != frag)
3020 /* out-of-order fragment */
3021 goto drop;
3022 } else
3023 *last_seq = seq;
3024
3025 *last_frag = frag;
3026 *last_time = jiffies;
3027 return 0;
3028
3029 drop:
3030 return 1;
3031}
3032
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08003033#ifdef CONFIG_IWL4965_SPECTRUM_MEASUREMENT
Zhu Yib481de92007-09-25 17:54:57 -07003034
3035#include "iwl-spectrum.h"
3036
3037#define BEACON_TIME_MASK_LOW 0x00FFFFFF
3038#define BEACON_TIME_MASK_HIGH 0xFF000000
3039#define TIME_UNIT 1024
3040
3041/*
3042 * extended beacon time format
3043 * time in usec will be changed into a 32-bit value in 8:24 format
3044 * the high 1 byte is the beacon counts
3045 * the lower 3 bytes is the time in usec within one beacon interval
3046 */
3047
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003048static u32 iwl4965_usecs_to_beacons(u32 usec, u32 beacon_interval)
Zhu Yib481de92007-09-25 17:54:57 -07003049{
3050 u32 quot;
3051 u32 rem;
3052 u32 interval = beacon_interval * 1024;
3053
3054 if (!interval || !usec)
3055 return 0;
3056
3057 quot = (usec / interval) & (BEACON_TIME_MASK_HIGH >> 24);
3058 rem = (usec % interval) & BEACON_TIME_MASK_LOW;
3059
3060 return (quot << 24) + rem;
3061}
3062
3063/* base is usually what we get from ucode with each received frame,
3064 * the same as HW timer counter counting down
3065 */
3066
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003067static __le32 iwl4965_add_beacon_time(u32 base, u32 addon, u32 beacon_interval)
Zhu Yib481de92007-09-25 17:54:57 -07003068{
3069 u32 base_low = base & BEACON_TIME_MASK_LOW;
3070 u32 addon_low = addon & BEACON_TIME_MASK_LOW;
3071 u32 interval = beacon_interval * TIME_UNIT;
3072 u32 res = (base & BEACON_TIME_MASK_HIGH) +
3073 (addon & BEACON_TIME_MASK_HIGH);
3074
3075 if (base_low > addon_low)
3076 res += base_low - addon_low;
3077 else if (base_low < addon_low) {
3078 res += interval + base_low - addon_low;
3079 res += (1 << 24);
3080 } else
3081 res += (1 << 24);
3082
3083 return cpu_to_le32(res);
3084}
3085
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003086static int iwl4965_get_measurement(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07003087 struct ieee80211_measurement_params *params,
3088 u8 type)
3089{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003090 struct iwl4965_spectrum_cmd spectrum;
3091 struct iwl4965_rx_packet *res;
3092 struct iwl4965_host_cmd cmd = {
Zhu Yib481de92007-09-25 17:54:57 -07003093 .id = REPLY_SPECTRUM_MEASUREMENT_CMD,
3094 .data = (void *)&spectrum,
3095 .meta.flags = CMD_WANT_SKB,
3096 };
3097 u32 add_time = le64_to_cpu(params->start_time);
3098 int rc;
3099 int spectrum_resp_status;
3100 int duration = le16_to_cpu(params->duration);
3101
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003102 if (iwl4965_is_associated(priv))
Zhu Yib481de92007-09-25 17:54:57 -07003103 add_time =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003104 iwl4965_usecs_to_beacons(
Zhu Yib481de92007-09-25 17:54:57 -07003105 le64_to_cpu(params->start_time) - priv->last_tsf,
3106 le16_to_cpu(priv->rxon_timing.beacon_interval));
3107
3108 memset(&spectrum, 0, sizeof(spectrum));
3109
3110 spectrum.channel_count = cpu_to_le16(1);
3111 spectrum.flags =
3112 RXON_FLG_TSF2HOST_MSK | RXON_FLG_ANT_A_MSK | RXON_FLG_DIS_DIV_MSK;
3113 spectrum.filter_flags = MEASUREMENT_FILTER_FLAG;
3114 cmd.len = sizeof(spectrum);
3115 spectrum.len = cpu_to_le16(cmd.len - sizeof(spectrum.len));
3116
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003117 if (iwl4965_is_associated(priv))
Zhu Yib481de92007-09-25 17:54:57 -07003118 spectrum.start_time =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003119 iwl4965_add_beacon_time(priv->last_beacon_time,
Zhu Yib481de92007-09-25 17:54:57 -07003120 add_time,
3121 le16_to_cpu(priv->rxon_timing.beacon_interval));
3122 else
3123 spectrum.start_time = 0;
3124
3125 spectrum.channels[0].duration = cpu_to_le32(duration * TIME_UNIT);
3126 spectrum.channels[0].channel = params->channel;
3127 spectrum.channels[0].type = type;
3128 if (priv->active_rxon.flags & RXON_FLG_BAND_24G_MSK)
3129 spectrum.flags |= RXON_FLG_BAND_24G_MSK |
3130 RXON_FLG_AUTO_DETECT_MSK | RXON_FLG_TGG_PROTECT_MSK;
3131
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003132 rc = iwl4965_send_cmd_sync(priv, &cmd);
Zhu Yib481de92007-09-25 17:54:57 -07003133 if (rc)
3134 return rc;
3135
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003136 res = (struct iwl4965_rx_packet *)cmd.meta.u.skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07003137 if (res->hdr.flags & IWL_CMD_FAILED_MSK) {
3138 IWL_ERROR("Bad return from REPLY_RX_ON_ASSOC command\n");
3139 rc = -EIO;
3140 }
3141
3142 spectrum_resp_status = le16_to_cpu(res->u.spectrum.status);
3143 switch (spectrum_resp_status) {
3144 case 0: /* Command will be handled */
3145 if (res->u.spectrum.id != 0xff) {
3146 IWL_DEBUG_INFO
3147 ("Replaced existing measurement: %d\n",
3148 res->u.spectrum.id);
3149 priv->measurement_status &= ~MEASUREMENT_READY;
3150 }
3151 priv->measurement_status |= MEASUREMENT_ACTIVE;
3152 rc = 0;
3153 break;
3154
3155 case 1: /* Command will not be handled */
3156 rc = -EAGAIN;
3157 break;
3158 }
3159
3160 dev_kfree_skb_any(cmd.meta.u.skb);
3161
3162 return rc;
3163}
3164#endif
3165
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003166static void iwl4965_txstatus_to_ieee(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003167 struct iwl4965_tx_info *tx_sta)
Zhu Yib481de92007-09-25 17:54:57 -07003168{
3169
3170 tx_sta->status.ack_signal = 0;
3171 tx_sta->status.excessive_retries = 0;
3172 tx_sta->status.queue_length = 0;
3173 tx_sta->status.queue_number = 0;
3174
3175 if (in_interrupt())
3176 ieee80211_tx_status_irqsafe(priv->hw,
3177 tx_sta->skb[0], &(tx_sta->status));
3178 else
3179 ieee80211_tx_status(priv->hw,
3180 tx_sta->skb[0], &(tx_sta->status));
3181
3182 tx_sta->skb[0] = NULL;
3183}
3184
3185/**
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003186 * iwl4965_tx_queue_reclaim - Reclaim Tx queue entries already Tx'd
Zhu Yib481de92007-09-25 17:54:57 -07003187 *
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003188 * When FW advances 'R' index, all entries between old and new 'R' index
3189 * need to be reclaimed. As result, some free space forms. If there is
3190 * enough free space (> low mark), wake the stack that feeds us.
Zhu Yib481de92007-09-25 17:54:57 -07003191 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003192int iwl4965_tx_queue_reclaim(struct iwl_priv *priv, int txq_id, int index)
Zhu Yib481de92007-09-25 17:54:57 -07003193{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003194 struct iwl4965_tx_queue *txq = &priv->txq[txq_id];
3195 struct iwl4965_queue *q = &txq->q;
Zhu Yib481de92007-09-25 17:54:57 -07003196 int nfreed = 0;
3197
3198 if ((index >= q->n_bd) || (x2_queue_used(q, index) == 0)) {
3199 IWL_ERROR("Read index for DMA queue txq id (%d), index %d, "
3200 "is out of range [0-%d] %d %d.\n", txq_id,
Tomas Winklerfc4b6852007-10-25 17:15:24 +08003201 index, q->n_bd, q->write_ptr, q->read_ptr);
Zhu Yib481de92007-09-25 17:54:57 -07003202 return 0;
3203 }
3204
Tomas Winklerc54b6792008-03-06 17:36:53 -08003205 for (index = iwl_queue_inc_wrap(index, q->n_bd);
Tomas Winklerfc4b6852007-10-25 17:15:24 +08003206 q->read_ptr != index;
Tomas Winklerc54b6792008-03-06 17:36:53 -08003207 q->read_ptr = iwl_queue_inc_wrap(q->read_ptr, q->n_bd)) {
Zhu Yib481de92007-09-25 17:54:57 -07003208 if (txq_id != IWL_CMD_QUEUE_NUM) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003209 iwl4965_txstatus_to_ieee(priv,
Tomas Winklerfc4b6852007-10-25 17:15:24 +08003210 &(txq->txb[txq->q.read_ptr]));
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003211 iwl4965_hw_txq_free_tfd(priv, txq);
Zhu Yib481de92007-09-25 17:54:57 -07003212 } else if (nfreed > 1) {
3213 IWL_ERROR("HCMD skipped: index (%d) %d %d\n", index,
Tomas Winklerfc4b6852007-10-25 17:15:24 +08003214 q->write_ptr, q->read_ptr);
Zhu Yib481de92007-09-25 17:54:57 -07003215 queue_work(priv->workqueue, &priv->restart);
3216 }
3217 nfreed++;
3218 }
3219
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003220/* if (iwl4965_queue_space(q) > q->low_mark && (txq_id >= 0) &&
Zhu Yib481de92007-09-25 17:54:57 -07003221 (txq_id != IWL_CMD_QUEUE_NUM) &&
3222 priv->mac80211_registered)
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003223 ieee80211_wake_queue(priv->hw, txq_id); */
Zhu Yib481de92007-09-25 17:54:57 -07003224
3225
3226 return nfreed;
3227}
3228
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003229static int iwl4965_is_tx_success(u32 status)
Zhu Yib481de92007-09-25 17:54:57 -07003230{
3231 status &= TX_STATUS_MSK;
3232 return (status == TX_STATUS_SUCCESS)
3233 || (status == TX_STATUS_DIRECT_DONE);
3234}
3235
3236/******************************************************************************
3237 *
3238 * Generic RX handler implementations
3239 *
3240 ******************************************************************************/
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08003241#ifdef CONFIG_IWL4965_HT
Zhu Yib481de92007-09-25 17:54:57 -07003242
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003243static inline int iwl4965_get_ra_sta_id(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07003244 struct ieee80211_hdr *hdr)
3245{
3246 if (priv->iw_mode == IEEE80211_IF_TYPE_STA)
3247 return IWL_AP_ID;
3248 else {
3249 u8 *da = ieee80211_get_DA(hdr);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003250 return iwl4965_hw_find_station(priv, da);
Zhu Yib481de92007-09-25 17:54:57 -07003251 }
3252}
3253
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003254static struct ieee80211_hdr *iwl4965_tx_queue_get_hdr(
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003255 struct iwl_priv *priv, int txq_id, int idx)
Zhu Yib481de92007-09-25 17:54:57 -07003256{
3257 if (priv->txq[txq_id].txb[idx].skb[0])
3258 return (struct ieee80211_hdr *)priv->txq[txq_id].
3259 txb[idx].skb[0]->data;
3260 return NULL;
3261}
3262
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003263static inline u32 iwl4965_get_scd_ssn(struct iwl4965_tx_resp *tx_resp)
Zhu Yib481de92007-09-25 17:54:57 -07003264{
3265 __le32 *scd_ssn = (__le32 *)((u32 *)&tx_resp->status +
3266 tx_resp->frame_count);
3267 return le32_to_cpu(*scd_ssn) & MAX_SN;
3268
3269}
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003270
3271/**
3272 * iwl4965_tx_status_reply_tx - Handle Tx rspnse for frames in aggregation queue
3273 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003274static int iwl4965_tx_status_reply_tx(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003275 struct iwl4965_ht_agg *agg,
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003276 struct iwl4965_tx_resp_agg *tx_resp,
Zhu Yib481de92007-09-25 17:54:57 -07003277 u16 start_idx)
3278{
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003279 u16 status;
3280 struct agg_tx_status *frame_status = &tx_resp->status;
Zhu Yib481de92007-09-25 17:54:57 -07003281 struct ieee80211_tx_status *tx_status = NULL;
3282 struct ieee80211_hdr *hdr = NULL;
3283 int i, sh;
3284 int txq_id, idx;
3285 u16 seq;
3286
3287 if (agg->wait_for_ba)
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003288 IWL_DEBUG_TX_REPLY("got tx response w/o block-ack\n");
Zhu Yib481de92007-09-25 17:54:57 -07003289
3290 agg->frame_count = tx_resp->frame_count;
3291 agg->start_idx = start_idx;
3292 agg->rate_n_flags = le32_to_cpu(tx_resp->rate_n_flags);
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003293 agg->bitmap = 0;
Zhu Yib481de92007-09-25 17:54:57 -07003294
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003295 /* # frames attempted by Tx command */
Zhu Yib481de92007-09-25 17:54:57 -07003296 if (agg->frame_count == 1) {
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003297 /* Only one frame was attempted; no block-ack will arrive */
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003298 status = le16_to_cpu(frame_status[0].status);
3299 seq = le16_to_cpu(frame_status[0].sequence);
3300 idx = SEQ_TO_INDEX(seq);
3301 txq_id = SEQ_TO_QUEUE(seq);
Zhu Yib481de92007-09-25 17:54:57 -07003302
Zhu Yib481de92007-09-25 17:54:57 -07003303 /* FIXME: code repetition */
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003304 IWL_DEBUG_TX_REPLY("FrameCnt = %d, StartIdx=%d idx=%d\n",
3305 agg->frame_count, agg->start_idx, idx);
Zhu Yib481de92007-09-25 17:54:57 -07003306
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003307 tx_status = &(priv->txq[txq_id].txb[idx].status);
Zhu Yib481de92007-09-25 17:54:57 -07003308 tx_status->retry_count = tx_resp->failure_frame;
3309 tx_status->queue_number = status & 0xff;
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003310 tx_status->queue_length = tx_resp->failure_rts;
3311 tx_status->control.flags &= ~IEEE80211_TXCTL_AMPDU;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003312 tx_status->flags = iwl4965_is_tx_success(status)?
Zhu Yib481de92007-09-25 17:54:57 -07003313 IEEE80211_TX_STATUS_ACK : 0;
Ron Rindjunsky4c424e42008-03-04 18:09:27 -08003314 iwl4965_hwrate_to_tx_control(priv,
3315 le32_to_cpu(tx_resp->rate_n_flags),
3316 &tx_status->control);
Zhu Yib481de92007-09-25 17:54:57 -07003317 /* FIXME: code repetition end */
3318
3319 IWL_DEBUG_TX_REPLY("1 Frame 0x%x failure :%d\n",
3320 status & 0xff, tx_resp->failure_frame);
3321 IWL_DEBUG_TX_REPLY("Rate Info rate_n_flags=%x\n",
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003322 iwl4965_hw_get_rate_n_flags(tx_resp->rate_n_flags));
Zhu Yib481de92007-09-25 17:54:57 -07003323
3324 agg->wait_for_ba = 0;
3325 } else {
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003326 /* Two or more frames were attempted; expect block-ack */
Zhu Yib481de92007-09-25 17:54:57 -07003327 u64 bitmap = 0;
3328 int start = agg->start_idx;
3329
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003330 /* Construct bit-map of pending frames within Tx window */
Zhu Yib481de92007-09-25 17:54:57 -07003331 for (i = 0; i < agg->frame_count; i++) {
3332 u16 sc;
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003333 status = le16_to_cpu(frame_status[i].status);
3334 seq = le16_to_cpu(frame_status[i].sequence);
Zhu Yib481de92007-09-25 17:54:57 -07003335 idx = SEQ_TO_INDEX(seq);
3336 txq_id = SEQ_TO_QUEUE(seq);
3337
3338 if (status & (AGG_TX_STATE_FEW_BYTES_MSK |
3339 AGG_TX_STATE_ABORT_MSK))
3340 continue;
3341
3342 IWL_DEBUG_TX_REPLY("FrameCnt = %d, txq_id=%d idx=%d\n",
3343 agg->frame_count, txq_id, idx);
3344
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003345 hdr = iwl4965_tx_queue_get_hdr(priv, txq_id, idx);
Zhu Yib481de92007-09-25 17:54:57 -07003346
3347 sc = le16_to_cpu(hdr->seq_ctrl);
3348 if (idx != (SEQ_TO_SN(sc) & 0xff)) {
3349 IWL_ERROR("BUG_ON idx doesn't match seq control"
3350 " idx=%d, seq_idx=%d, seq=%d\n",
3351 idx, SEQ_TO_SN(sc),
3352 hdr->seq_ctrl);
3353 return -1;
3354 }
3355
3356 IWL_DEBUG_TX_REPLY("AGG Frame i=%d idx %d seq=%d\n",
3357 i, idx, SEQ_TO_SN(sc));
3358
3359 sh = idx - start;
3360 if (sh > 64) {
3361 sh = (start - idx) + 0xff;
3362 bitmap = bitmap << sh;
3363 sh = 0;
3364 start = idx;
3365 } else if (sh < -64)
3366 sh = 0xff - (start - idx);
3367 else if (sh < 0) {
3368 sh = start - idx;
3369 start = idx;
3370 bitmap = bitmap << sh;
3371 sh = 0;
3372 }
3373 bitmap |= (1 << sh);
3374 IWL_DEBUG_TX_REPLY("start=%d bitmap=0x%x\n",
3375 start, (u32)(bitmap & 0xFFFFFFFF));
3376 }
3377
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003378 agg->bitmap = bitmap;
Zhu Yib481de92007-09-25 17:54:57 -07003379 agg->start_idx = start;
3380 agg->rate_n_flags = le32_to_cpu(tx_resp->rate_n_flags);
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003381 IWL_DEBUG_TX_REPLY("Frames %d start_idx=%d bitmap=0x%llx\n",
Zhu Yib481de92007-09-25 17:54:57 -07003382 agg->frame_count, agg->start_idx,
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003383 agg->bitmap);
Zhu Yib481de92007-09-25 17:54:57 -07003384
3385 if (bitmap)
3386 agg->wait_for_ba = 1;
3387 }
3388 return 0;
3389}
3390#endif
Zhu Yib481de92007-09-25 17:54:57 -07003391
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003392/**
3393 * iwl4965_rx_reply_tx - Handle standard (non-aggregation) Tx response
3394 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003395static void iwl4965_rx_reply_tx(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003396 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003397{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003398 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07003399 u16 sequence = le16_to_cpu(pkt->hdr.sequence);
3400 int txq_id = SEQ_TO_QUEUE(sequence);
3401 int index = SEQ_TO_INDEX(sequence);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003402 struct iwl4965_tx_queue *txq = &priv->txq[txq_id];
Zhu Yib481de92007-09-25 17:54:57 -07003403 struct ieee80211_tx_status *tx_status;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003404 struct iwl4965_tx_resp *tx_resp = (void *)&pkt->u.raw[0];
Zhu Yib481de92007-09-25 17:54:57 -07003405 u32 status = le32_to_cpu(tx_resp->status);
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08003406#ifdef CONFIG_IWL4965_HT
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003407 int tid = MAX_TID_COUNT, sta_id = IWL_INVALID_STATION;
3408 struct ieee80211_hdr *hdr;
3409 __le16 *qc;
Zhu Yib481de92007-09-25 17:54:57 -07003410#endif
3411
3412 if ((index >= txq->q.n_bd) || (x2_queue_used(&txq->q, index) == 0)) {
3413 IWL_ERROR("Read index for DMA queue txq_id (%d) index %d "
3414 "is out of range [0-%d] %d %d\n", txq_id,
Tomas Winklerfc4b6852007-10-25 17:15:24 +08003415 index, txq->q.n_bd, txq->q.write_ptr,
3416 txq->q.read_ptr);
Zhu Yib481de92007-09-25 17:54:57 -07003417 return;
3418 }
3419
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08003420#ifdef CONFIG_IWL4965_HT
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003421 hdr = iwl4965_tx_queue_get_hdr(priv, txq_id, index);
3422 qc = ieee80211_get_qos_ctrl(hdr);
Zhu Yib481de92007-09-25 17:54:57 -07003423
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003424 if (qc)
Zhu Yib481de92007-09-25 17:54:57 -07003425 tid = le16_to_cpu(*qc) & 0xf;
3426
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003427 sta_id = iwl4965_get_ra_sta_id(priv, hdr);
3428 if (txq->sched_retry && unlikely(sta_id == IWL_INVALID_STATION)) {
3429 IWL_ERROR("Station not known\n");
3430 return;
3431 }
3432
3433 if (txq->sched_retry) {
3434 const u32 scd_ssn = iwl4965_get_scd_ssn(tx_resp);
3435 struct iwl4965_ht_agg *agg = NULL;
3436
3437 if (!qc)
Zhu Yib481de92007-09-25 17:54:57 -07003438 return;
Zhu Yib481de92007-09-25 17:54:57 -07003439
3440 agg = &priv->stations[sta_id].tid[tid].agg;
3441
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003442 iwl4965_tx_status_reply_tx(priv, agg,
3443 (struct iwl4965_tx_resp_agg *)tx_resp, index);
Zhu Yib481de92007-09-25 17:54:57 -07003444
3445 if ((tx_resp->frame_count == 1) &&
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003446 !iwl4965_is_tx_success(status)) {
Zhu Yib481de92007-09-25 17:54:57 -07003447 /* TODO: send BAR */
3448 }
3449
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003450 if (txq->q.read_ptr != (scd_ssn & 0xff)) {
3451 int freed;
Tomas Winklerc54b6792008-03-06 17:36:53 -08003452 index = iwl_queue_dec_wrap(scd_ssn & 0xff, txq->q.n_bd);
Zhu Yib481de92007-09-25 17:54:57 -07003453 IWL_DEBUG_TX_REPLY("Retry scheduler reclaim scd_ssn "
3454 "%d index %d\n", scd_ssn , index);
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003455 freed = iwl4965_tx_queue_reclaim(priv, txq_id, index);
3456 priv->stations[sta_id].tid[tid].tfds_in_queue -= freed;
3457
3458 if (iwl4965_queue_space(&txq->q) > txq->q.low_mark &&
3459 txq_id >= 0 && priv->mac80211_registered &&
3460 agg->state != IWL_EMPTYING_HW_QUEUE_DELBA)
3461 ieee80211_wake_queue(priv->hw, txq_id);
3462
3463 iwl4965_check_empty_hw_queue(priv, sta_id, tid, txq_id);
Zhu Yib481de92007-09-25 17:54:57 -07003464 }
3465 } else {
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08003466#endif /* CONFIG_IWL4965_HT */
Tomas Winklerfc4b6852007-10-25 17:15:24 +08003467 tx_status = &(txq->txb[txq->q.read_ptr].status);
Zhu Yib481de92007-09-25 17:54:57 -07003468
3469 tx_status->retry_count = tx_resp->failure_frame;
3470 tx_status->queue_number = status;
3471 tx_status->queue_length = tx_resp->bt_kill_count;
3472 tx_status->queue_length |= tx_resp->failure_rts;
Zhu Yib481de92007-09-25 17:54:57 -07003473 tx_status->flags =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003474 iwl4965_is_tx_success(status) ? IEEE80211_TX_STATUS_ACK : 0;
Ron Rindjunsky4c424e42008-03-04 18:09:27 -08003475 iwl4965_hwrate_to_tx_control(priv, le32_to_cpu(tx_resp->rate_n_flags),
3476 &tx_status->control);
Zhu Yib481de92007-09-25 17:54:57 -07003477
Zhu Yib481de92007-09-25 17:54:57 -07003478 IWL_DEBUG_TX("Tx queue %d Status %s (0x%08x) rate_n_flags 0x%x "
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003479 "retries %d\n", txq_id, iwl4965_get_tx_fail_reason(status),
Zhu Yib481de92007-09-25 17:54:57 -07003480 status, le32_to_cpu(tx_resp->rate_n_flags),
3481 tx_resp->failure_frame);
3482
3483 IWL_DEBUG_TX_REPLY("Tx queue reclaim %d\n", index);
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003484 if (index != -1) {
3485 int freed = iwl4965_tx_queue_reclaim(priv, txq_id, index);
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08003486#ifdef CONFIG_IWL4965_HT
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003487 if (tid != MAX_TID_COUNT)
3488 priv->stations[sta_id].tid[tid].tfds_in_queue -= freed;
3489 if (iwl4965_queue_space(&txq->q) > txq->q.low_mark &&
3490 (txq_id >= 0) &&
3491 priv->mac80211_registered)
3492 ieee80211_wake_queue(priv->hw, txq_id);
3493 if (tid != MAX_TID_COUNT)
3494 iwl4965_check_empty_hw_queue(priv, sta_id, tid, txq_id);
3495#endif
Zhu Yib481de92007-09-25 17:54:57 -07003496 }
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003497#ifdef CONFIG_IWL4965_HT
3498 }
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08003499#endif /* CONFIG_IWL4965_HT */
Zhu Yib481de92007-09-25 17:54:57 -07003500
3501 if (iwl_check_bits(status, TX_ABORT_REQUIRED_MSK))
3502 IWL_ERROR("TODO: Implement Tx ABORT REQUIRED!!!\n");
3503}
3504
3505
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003506static void iwl4965_rx_reply_alive(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003507 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003508{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003509 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
3510 struct iwl4965_alive_resp *palive;
Zhu Yib481de92007-09-25 17:54:57 -07003511 struct delayed_work *pwork;
3512
3513 palive = &pkt->u.alive_frame;
3514
3515 IWL_DEBUG_INFO("Alive ucode status 0x%08X revision "
3516 "0x%01X 0x%01X\n",
3517 palive->is_valid, palive->ver_type,
3518 palive->ver_subtype);
3519
3520 if (palive->ver_subtype == INITIALIZE_SUBTYPE) {
3521 IWL_DEBUG_INFO("Initialization Alive received.\n");
3522 memcpy(&priv->card_alive_init,
3523 &pkt->u.alive_frame,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003524 sizeof(struct iwl4965_init_alive_resp));
Zhu Yib481de92007-09-25 17:54:57 -07003525 pwork = &priv->init_alive_start;
3526 } else {
3527 IWL_DEBUG_INFO("Runtime Alive received.\n");
3528 memcpy(&priv->card_alive, &pkt->u.alive_frame,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003529 sizeof(struct iwl4965_alive_resp));
Zhu Yib481de92007-09-25 17:54:57 -07003530 pwork = &priv->alive_start;
3531 }
3532
3533 /* We delay the ALIVE response by 5ms to
3534 * give the HW RF Kill time to activate... */
3535 if (palive->is_valid == UCODE_VALID_OK)
3536 queue_delayed_work(priv->workqueue, pwork,
3537 msecs_to_jiffies(5));
3538 else
3539 IWL_WARNING("uCode did not respond OK.\n");
3540}
3541
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003542static void iwl4965_rx_reply_add_sta(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003543 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003544{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003545 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07003546
3547 IWL_DEBUG_RX("Received REPLY_ADD_STA: 0x%02X\n", pkt->u.status);
3548 return;
3549}
3550
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003551static void iwl4965_rx_reply_error(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003552 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003553{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003554 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07003555
3556 IWL_ERROR("Error Reply type 0x%08X cmd %s (0x%02X) "
3557 "seq 0x%04X ser 0x%08X\n",
3558 le32_to_cpu(pkt->u.err_resp.error_type),
3559 get_cmd_string(pkt->u.err_resp.cmd_id),
3560 pkt->u.err_resp.cmd_id,
3561 le16_to_cpu(pkt->u.err_resp.bad_cmd_seq_num),
3562 le32_to_cpu(pkt->u.err_resp.error_info));
3563}
3564
3565#define TX_STATUS_ENTRY(x) case TX_STATUS_FAIL_ ## x: return #x
3566
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003567static void iwl4965_rx_csa(struct iwl_priv *priv, struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003568{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003569 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
3570 struct iwl4965_rxon_cmd *rxon = (void *)&priv->active_rxon;
3571 struct iwl4965_csa_notification *csa = &(pkt->u.csa_notif);
Zhu Yib481de92007-09-25 17:54:57 -07003572 IWL_DEBUG_11H("CSA notif: channel %d, status %d\n",
3573 le16_to_cpu(csa->channel), le32_to_cpu(csa->status));
3574 rxon->channel = csa->channel;
3575 priv->staging_rxon.channel = csa->channel;
3576}
3577
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003578static void iwl4965_rx_spectrum_measure_notif(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003579 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003580{
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08003581#ifdef CONFIG_IWL4965_SPECTRUM_MEASUREMENT
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003582 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
3583 struct iwl4965_spectrum_notification *report = &(pkt->u.spectrum_notif);
Zhu Yib481de92007-09-25 17:54:57 -07003584
3585 if (!report->state) {
3586 IWL_DEBUG(IWL_DL_11H | IWL_DL_INFO,
3587 "Spectrum Measure Notification: Start\n");
3588 return;
3589 }
3590
3591 memcpy(&priv->measure_report, report, sizeof(*report));
3592 priv->measurement_status |= MEASUREMENT_READY;
3593#endif
3594}
3595
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003596static void iwl4965_rx_pm_sleep_notif(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003597 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003598{
Tomas Winkler0a6857e2008-03-12 16:58:49 -07003599#ifdef CONFIG_IWLWIFI_DEBUG
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003600 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
3601 struct iwl4965_sleep_notification *sleep = &(pkt->u.sleep_notif);
Zhu Yib481de92007-09-25 17:54:57 -07003602 IWL_DEBUG_RX("sleep mode: %d, src: %d\n",
3603 sleep->pm_sleep_mode, sleep->pm_wakeup_src);
3604#endif
3605}
3606
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003607static void iwl4965_rx_pm_debug_statistics_notif(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003608 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003609{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003610 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07003611 IWL_DEBUG_RADIO("Dumping %d bytes of unhandled "
3612 "notification for %s:\n",
3613 le32_to_cpu(pkt->len), get_cmd_string(pkt->hdr.cmd));
Tomas Winkler0a6857e2008-03-12 16:58:49 -07003614 iwl_print_hex_dump(IWL_DL_RADIO, pkt->u.raw, le32_to_cpu(pkt->len));
Zhu Yib481de92007-09-25 17:54:57 -07003615}
3616
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003617static void iwl4965_bg_beacon_update(struct work_struct *work)
Zhu Yib481de92007-09-25 17:54:57 -07003618{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003619 struct iwl_priv *priv =
3620 container_of(work, struct iwl_priv, beacon_update);
Zhu Yib481de92007-09-25 17:54:57 -07003621 struct sk_buff *beacon;
3622
3623 /* Pull updated AP beacon from mac80211. will fail if not in AP mode */
Johannes Berg32bfd352007-12-19 01:31:26 +01003624 beacon = ieee80211_beacon_get(priv->hw, priv->vif, NULL);
Zhu Yib481de92007-09-25 17:54:57 -07003625
3626 if (!beacon) {
3627 IWL_ERROR("update beacon failed\n");
3628 return;
3629 }
3630
3631 mutex_lock(&priv->mutex);
3632 /* new beacon skb is allocated every time; dispose previous.*/
3633 if (priv->ibss_beacon)
3634 dev_kfree_skb(priv->ibss_beacon);
3635
3636 priv->ibss_beacon = beacon;
3637 mutex_unlock(&priv->mutex);
3638
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003639 iwl4965_send_beacon_cmd(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003640}
3641
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003642static void iwl4965_rx_beacon_notif(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003643 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003644{
Tomas Winkler0a6857e2008-03-12 16:58:49 -07003645#ifdef CONFIG_IWLWIFI_DEBUG
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003646 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
3647 struct iwl4965_beacon_notif *beacon = &(pkt->u.beacon_status);
3648 u8 rate = iwl4965_hw_get_rate(beacon->beacon_notify_hdr.rate_n_flags);
Zhu Yib481de92007-09-25 17:54:57 -07003649
3650 IWL_DEBUG_RX("beacon status %x retries %d iss %d "
3651 "tsf %d %d rate %d\n",
3652 le32_to_cpu(beacon->beacon_notify_hdr.status) & TX_STATUS_MSK,
3653 beacon->beacon_notify_hdr.failure_frame,
3654 le32_to_cpu(beacon->ibss_mgr_status),
3655 le32_to_cpu(beacon->high_tsf),
3656 le32_to_cpu(beacon->low_tsf), rate);
3657#endif
3658
3659 if ((priv->iw_mode == IEEE80211_IF_TYPE_AP) &&
3660 (!test_bit(STATUS_EXIT_PENDING, &priv->status)))
3661 queue_work(priv->workqueue, &priv->beacon_update);
3662}
3663
3664/* Service response to REPLY_SCAN_CMD (0x80) */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003665static void iwl4965_rx_reply_scan(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003666 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003667{
Tomas Winkler0a6857e2008-03-12 16:58:49 -07003668#ifdef CONFIG_IWLWIFI_DEBUG
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003669 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
3670 struct iwl4965_scanreq_notification *notif =
3671 (struct iwl4965_scanreq_notification *)pkt->u.raw;
Zhu Yib481de92007-09-25 17:54:57 -07003672
3673 IWL_DEBUG_RX("Scan request status = 0x%x\n", notif->status);
3674#endif
3675}
3676
3677/* Service SCAN_START_NOTIFICATION (0x82) */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003678static void iwl4965_rx_scan_start_notif(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003679 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003680{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003681 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
3682 struct iwl4965_scanstart_notification *notif =
3683 (struct iwl4965_scanstart_notification *)pkt->u.raw;
Zhu Yib481de92007-09-25 17:54:57 -07003684 priv->scan_start_tsf = le32_to_cpu(notif->tsf_low);
3685 IWL_DEBUG_SCAN("Scan start: "
3686 "%d [802.11%s] "
3687 "(TSF: 0x%08X:%08X) - %d (beacon timer %u)\n",
3688 notif->channel,
3689 notif->band ? "bg" : "a",
3690 notif->tsf_high,
3691 notif->tsf_low, notif->status, notif->beacon_timer);
3692}
3693
3694/* Service SCAN_RESULTS_NOTIFICATION (0x83) */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003695static void iwl4965_rx_scan_results_notif(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003696 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003697{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003698 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
3699 struct iwl4965_scanresults_notification *notif =
3700 (struct iwl4965_scanresults_notification *)pkt->u.raw;
Zhu Yib481de92007-09-25 17:54:57 -07003701
3702 IWL_DEBUG_SCAN("Scan ch.res: "
3703 "%d [802.11%s] "
3704 "(TSF: 0x%08X:%08X) - %d "
3705 "elapsed=%lu usec (%dms since last)\n",
3706 notif->channel,
3707 notif->band ? "bg" : "a",
3708 le32_to_cpu(notif->tsf_high),
3709 le32_to_cpu(notif->tsf_low),
3710 le32_to_cpu(notif->statistics[0]),
3711 le32_to_cpu(notif->tsf_low) - priv->scan_start_tsf,
3712 jiffies_to_msecs(elapsed_jiffies
3713 (priv->last_scan_jiffies, jiffies)));
3714
3715 priv->last_scan_jiffies = jiffies;
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08003716 priv->next_scan_jiffies = 0;
Zhu Yib481de92007-09-25 17:54:57 -07003717}
3718
3719/* Service SCAN_COMPLETE_NOTIFICATION (0x84) */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003720static void iwl4965_rx_scan_complete_notif(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003721 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003722{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003723 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
3724 struct iwl4965_scancomplete_notification *scan_notif = (void *)pkt->u.raw;
Zhu Yib481de92007-09-25 17:54:57 -07003725
3726 IWL_DEBUG_SCAN("Scan complete: %d channels (TSF 0x%08X:%08X) - %d\n",
3727 scan_notif->scanned_channels,
3728 scan_notif->tsf_low,
3729 scan_notif->tsf_high, scan_notif->status);
3730
3731 /* The HW is no longer scanning */
3732 clear_bit(STATUS_SCAN_HW, &priv->status);
3733
3734 /* The scan completion notification came in, so kill that timer... */
3735 cancel_delayed_work(&priv->scan_check);
3736
3737 IWL_DEBUG_INFO("Scan pass on %sGHz took %dms\n",
3738 (priv->scan_bands == 2) ? "2.4" : "5.2",
3739 jiffies_to_msecs(elapsed_jiffies
3740 (priv->scan_pass_start, jiffies)));
3741
3742 /* Remove this scanned band from the list
3743 * of pending bands to scan */
3744 priv->scan_bands--;
3745
3746 /* If a request to abort was given, or the scan did not succeed
3747 * then we reset the scan state machine and terminate,
3748 * re-queuing another scan if one has been requested */
3749 if (test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
3750 IWL_DEBUG_INFO("Aborted scan completed.\n");
3751 clear_bit(STATUS_SCAN_ABORTING, &priv->status);
3752 } else {
3753 /* If there are more bands on this scan pass reschedule */
3754 if (priv->scan_bands > 0)
3755 goto reschedule;
3756 }
3757
3758 priv->last_scan_jiffies = jiffies;
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08003759 priv->next_scan_jiffies = 0;
Zhu Yib481de92007-09-25 17:54:57 -07003760 IWL_DEBUG_INFO("Setting scan to off\n");
3761
3762 clear_bit(STATUS_SCANNING, &priv->status);
3763
3764 IWL_DEBUG_INFO("Scan took %dms\n",
3765 jiffies_to_msecs(elapsed_jiffies(priv->scan_start, jiffies)));
3766
3767 queue_work(priv->workqueue, &priv->scan_completed);
3768
3769 return;
3770
3771reschedule:
3772 priv->scan_pass_start = jiffies;
3773 queue_work(priv->workqueue, &priv->request_scan);
3774}
3775
3776/* Handle notification from uCode that card's power state is changing
3777 * due to software, hardware, or critical temperature RFKILL */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003778static void iwl4965_rx_card_state_notif(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003779 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003780{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003781 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07003782 u32 flags = le32_to_cpu(pkt->u.card_state_notif.flags);
3783 unsigned long status = priv->status;
3784
3785 IWL_DEBUG_RF_KILL("Card state received: HW:%s SW:%s\n",
3786 (flags & HW_CARD_DISABLED) ? "Kill" : "On",
3787 (flags & SW_CARD_DISABLED) ? "Kill" : "On");
3788
3789 if (flags & (SW_CARD_DISABLED | HW_CARD_DISABLED |
3790 RF_CARD_DISABLED)) {
3791
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003792 iwl4965_write32(priv, CSR_UCODE_DRV_GP1_SET,
Zhu Yib481de92007-09-25 17:54:57 -07003793 CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
3794
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003795 if (!iwl4965_grab_nic_access(priv)) {
3796 iwl4965_write_direct32(
Zhu Yib481de92007-09-25 17:54:57 -07003797 priv, HBUS_TARG_MBX_C,
3798 HBUS_TARG_MBX_C_REG_BIT_CMD_BLOCKED);
3799
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003800 iwl4965_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003801 }
3802
3803 if (!(flags & RXON_CARD_DISABLED)) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003804 iwl4965_write32(priv, CSR_UCODE_DRV_GP1_CLR,
Zhu Yib481de92007-09-25 17:54:57 -07003805 CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003806 if (!iwl4965_grab_nic_access(priv)) {
3807 iwl4965_write_direct32(
Zhu Yib481de92007-09-25 17:54:57 -07003808 priv, HBUS_TARG_MBX_C,
3809 HBUS_TARG_MBX_C_REG_BIT_CMD_BLOCKED);
3810
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003811 iwl4965_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003812 }
3813 }
3814
3815 if (flags & RF_CARD_DISABLED) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003816 iwl4965_write32(priv, CSR_UCODE_DRV_GP1_SET,
Zhu Yib481de92007-09-25 17:54:57 -07003817 CSR_UCODE_DRV_GP1_REG_BIT_CT_KILL_EXIT);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003818 iwl4965_read32(priv, CSR_UCODE_DRV_GP1);
3819 if (!iwl4965_grab_nic_access(priv))
3820 iwl4965_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003821 }
3822 }
3823
3824 if (flags & HW_CARD_DISABLED)
3825 set_bit(STATUS_RF_KILL_HW, &priv->status);
3826 else
3827 clear_bit(STATUS_RF_KILL_HW, &priv->status);
3828
3829
3830 if (flags & SW_CARD_DISABLED)
3831 set_bit(STATUS_RF_KILL_SW, &priv->status);
3832 else
3833 clear_bit(STATUS_RF_KILL_SW, &priv->status);
3834
3835 if (!(flags & RXON_CARD_DISABLED))
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003836 iwl4965_scan_cancel(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003837
3838 if ((test_bit(STATUS_RF_KILL_HW, &status) !=
3839 test_bit(STATUS_RF_KILL_HW, &priv->status)) ||
3840 (test_bit(STATUS_RF_KILL_SW, &status) !=
3841 test_bit(STATUS_RF_KILL_SW, &priv->status)))
3842 queue_work(priv->workqueue, &priv->rf_kill);
3843 else
3844 wake_up_interruptible(&priv->wait_command_queue);
3845}
3846
3847/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003848 * iwl4965_setup_rx_handlers - Initialize Rx handler callbacks
Zhu Yib481de92007-09-25 17:54:57 -07003849 *
3850 * Setup the RX handlers for each of the reply types sent from the uCode
3851 * to the host.
3852 *
3853 * This function chains into the hardware specific files for them to setup
3854 * any hardware specific handlers as well.
3855 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003856static void iwl4965_setup_rx_handlers(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003857{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003858 priv->rx_handlers[REPLY_ALIVE] = iwl4965_rx_reply_alive;
3859 priv->rx_handlers[REPLY_ADD_STA] = iwl4965_rx_reply_add_sta;
3860 priv->rx_handlers[REPLY_ERROR] = iwl4965_rx_reply_error;
3861 priv->rx_handlers[CHANNEL_SWITCH_NOTIFICATION] = iwl4965_rx_csa;
Zhu Yib481de92007-09-25 17:54:57 -07003862 priv->rx_handlers[SPECTRUM_MEASURE_NOTIFICATION] =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003863 iwl4965_rx_spectrum_measure_notif;
3864 priv->rx_handlers[PM_SLEEP_NOTIFICATION] = iwl4965_rx_pm_sleep_notif;
Zhu Yib481de92007-09-25 17:54:57 -07003865 priv->rx_handlers[PM_DEBUG_STATISTIC_NOTIFIC] =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003866 iwl4965_rx_pm_debug_statistics_notif;
3867 priv->rx_handlers[BEACON_NOTIFICATION] = iwl4965_rx_beacon_notif;
Zhu Yib481de92007-09-25 17:54:57 -07003868
Ben Cahill9fbab512007-11-29 11:09:47 +08003869 /*
3870 * The same handler is used for both the REPLY to a discrete
3871 * statistics request from the host as well as for the periodic
3872 * statistics notifications (after received beacons) from the uCode.
Zhu Yib481de92007-09-25 17:54:57 -07003873 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003874 priv->rx_handlers[REPLY_STATISTICS_CMD] = iwl4965_hw_rx_statistics;
3875 priv->rx_handlers[STATISTICS_NOTIFICATION] = iwl4965_hw_rx_statistics;
Zhu Yib481de92007-09-25 17:54:57 -07003876
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003877 priv->rx_handlers[REPLY_SCAN_CMD] = iwl4965_rx_reply_scan;
3878 priv->rx_handlers[SCAN_START_NOTIFICATION] = iwl4965_rx_scan_start_notif;
Zhu Yib481de92007-09-25 17:54:57 -07003879 priv->rx_handlers[SCAN_RESULTS_NOTIFICATION] =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003880 iwl4965_rx_scan_results_notif;
Zhu Yib481de92007-09-25 17:54:57 -07003881 priv->rx_handlers[SCAN_COMPLETE_NOTIFICATION] =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003882 iwl4965_rx_scan_complete_notif;
3883 priv->rx_handlers[CARD_STATE_NOTIFICATION] = iwl4965_rx_card_state_notif;
3884 priv->rx_handlers[REPLY_TX] = iwl4965_rx_reply_tx;
Zhu Yib481de92007-09-25 17:54:57 -07003885
Ben Cahill9fbab512007-11-29 11:09:47 +08003886 /* Set up hardware specific Rx handlers */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003887 iwl4965_hw_rx_handler_setup(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003888}
3889
3890/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003891 * iwl4965_tx_cmd_complete - Pull unused buffers off the queue and reclaim them
Zhu Yib481de92007-09-25 17:54:57 -07003892 * @rxb: Rx buffer to reclaim
3893 *
3894 * If an Rx buffer has an async callback associated with it the callback
3895 * will be executed. The attached skb (if present) will only be freed
3896 * if the callback returns 1
3897 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003898static void iwl4965_tx_cmd_complete(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003899 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003900{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003901 struct iwl4965_rx_packet *pkt = (struct iwl4965_rx_packet *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07003902 u16 sequence = le16_to_cpu(pkt->hdr.sequence);
3903 int txq_id = SEQ_TO_QUEUE(sequence);
3904 int index = SEQ_TO_INDEX(sequence);
3905 int huge = sequence & SEQ_HUGE_FRAME;
3906 int cmd_index;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003907 struct iwl4965_cmd *cmd;
Zhu Yib481de92007-09-25 17:54:57 -07003908
3909 /* If a Tx command is being handled and it isn't in the actual
3910 * command queue then there a command routing bug has been introduced
3911 * in the queue management code. */
3912 if (txq_id != IWL_CMD_QUEUE_NUM)
3913 IWL_ERROR("Error wrong command queue %d command id 0x%X\n",
3914 txq_id, pkt->hdr.cmd);
3915 BUG_ON(txq_id != IWL_CMD_QUEUE_NUM);
3916
3917 cmd_index = get_cmd_index(&priv->txq[IWL_CMD_QUEUE_NUM].q, index, huge);
3918 cmd = &priv->txq[IWL_CMD_QUEUE_NUM].cmd[cmd_index];
3919
3920 /* Input error checking is done when commands are added to queue. */
3921 if (cmd->meta.flags & CMD_WANT_SKB) {
3922 cmd->meta.source->u.skb = rxb->skb;
3923 rxb->skb = NULL;
3924 } else if (cmd->meta.u.callback &&
3925 !cmd->meta.u.callback(priv, cmd, rxb->skb))
3926 rxb->skb = NULL;
3927
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003928 iwl4965_tx_queue_reclaim(priv, txq_id, index);
Zhu Yib481de92007-09-25 17:54:57 -07003929
3930 if (!(cmd->meta.flags & CMD_ASYNC)) {
3931 clear_bit(STATUS_HCMD_ACTIVE, &priv->status);
3932 wake_up_interruptible(&priv->wait_command_queue);
3933 }
3934}
3935
3936/************************** RX-FUNCTIONS ****************************/
3937/*
3938 * Rx theory of operation
3939 *
Ben Cahill9fbab512007-11-29 11:09:47 +08003940 * Driver allocates a circular buffer of Receive Buffer Descriptors (RBDs),
3941 * each of which point to Receive Buffers to be filled by 4965. These get
3942 * used not only for Rx frames, but for any command response or notification
3943 * from the 4965. The driver and 4965 manage the Rx buffers by means
3944 * of indexes into the circular buffer.
Zhu Yib481de92007-09-25 17:54:57 -07003945 *
3946 * Rx Queue Indexes
3947 * The host/firmware share two index registers for managing the Rx buffers.
3948 *
3949 * The READ index maps to the first position that the firmware may be writing
3950 * to -- the driver can read up to (but not including) this position and get
3951 * good data.
3952 * The READ index is managed by the firmware once the card is enabled.
3953 *
3954 * The WRITE index maps to the last position the driver has read from -- the
3955 * position preceding WRITE is the last slot the firmware can place a packet.
3956 *
3957 * The queue is empty (no good data) if WRITE = READ - 1, and is full if
3958 * WRITE = READ.
3959 *
Ben Cahill9fbab512007-11-29 11:09:47 +08003960 * During initialization, the host sets up the READ queue position to the first
Zhu Yib481de92007-09-25 17:54:57 -07003961 * INDEX position, and WRITE to the last (READ - 1 wrapped)
3962 *
Ben Cahill9fbab512007-11-29 11:09:47 +08003963 * When the firmware places a packet in a buffer, it will advance the READ index
Zhu Yib481de92007-09-25 17:54:57 -07003964 * and fire the RX interrupt. The driver can then query the READ index and
3965 * process as many packets as possible, moving the WRITE index forward as it
3966 * resets the Rx queue buffers with new memory.
3967 *
3968 * The management in the driver is as follows:
3969 * + A list of pre-allocated SKBs is stored in iwl->rxq->rx_free. When
3970 * iwl->rxq->free_count drops to or below RX_LOW_WATERMARK, work is scheduled
Ian Schram01ebd062007-10-25 17:15:22 +08003971 * to replenish the iwl->rxq->rx_free.
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003972 * + In iwl4965_rx_replenish (scheduled) if 'processed' != 'read' then the
Zhu Yib481de92007-09-25 17:54:57 -07003973 * iwl->rxq is replenished and the READ INDEX is updated (updating the
3974 * 'processed' and 'read' driver indexes as well)
3975 * + A received packet is processed and handed to the kernel network stack,
3976 * detached from the iwl->rxq. The driver 'processed' index is updated.
3977 * + The Host/Firmware iwl->rxq is replenished at tasklet time from the rx_free
3978 * list. If there are no allocated buffers in iwl->rxq->rx_free, the READ
3979 * INDEX is not incremented and iwl->status(RX_STALLED) is set. If there
3980 * were enough free buffers and RX_STALLED is set it is cleared.
3981 *
3982 *
3983 * Driver sequence:
3984 *
Ben Cahill9fbab512007-11-29 11:09:47 +08003985 * iwl4965_rx_queue_alloc() Allocates rx_free
3986 * iwl4965_rx_replenish() Replenishes rx_free list from rx_used, and calls
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003987 * iwl4965_rx_queue_restock
Ben Cahill9fbab512007-11-29 11:09:47 +08003988 * iwl4965_rx_queue_restock() Moves available buffers from rx_free into Rx
Zhu Yib481de92007-09-25 17:54:57 -07003989 * queue, updates firmware pointers, and updates
3990 * the WRITE index. If insufficient rx_free buffers
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003991 * are available, schedules iwl4965_rx_replenish
Zhu Yib481de92007-09-25 17:54:57 -07003992 *
3993 * -- enable interrupts --
Ben Cahill9fbab512007-11-29 11:09:47 +08003994 * ISR - iwl4965_rx() Detach iwl4965_rx_mem_buffers from pool up to the
Zhu Yib481de92007-09-25 17:54:57 -07003995 * READ INDEX, detaching the SKB from the pool.
3996 * Moves the packet buffer from queue to rx_used.
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003997 * Calls iwl4965_rx_queue_restock to refill any empty
Zhu Yib481de92007-09-25 17:54:57 -07003998 * slots.
3999 * ...
4000 *
4001 */
4002
4003/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004004 * iwl4965_rx_queue_space - Return number of free slots available in queue.
Zhu Yib481de92007-09-25 17:54:57 -07004005 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004006static int iwl4965_rx_queue_space(const struct iwl4965_rx_queue *q)
Zhu Yib481de92007-09-25 17:54:57 -07004007{
4008 int s = q->read - q->write;
4009 if (s <= 0)
4010 s += RX_QUEUE_SIZE;
4011 /* keep some buffer to not confuse full and empty queue */
4012 s -= 2;
4013 if (s < 0)
4014 s = 0;
4015 return s;
4016}
4017
4018/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004019 * iwl4965_rx_queue_update_write_ptr - Update the write pointer for the RX queue
Zhu Yib481de92007-09-25 17:54:57 -07004020 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004021int iwl4965_rx_queue_update_write_ptr(struct iwl_priv *priv, struct iwl4965_rx_queue *q)
Zhu Yib481de92007-09-25 17:54:57 -07004022{
4023 u32 reg = 0;
4024 int rc = 0;
4025 unsigned long flags;
4026
4027 spin_lock_irqsave(&q->lock, flags);
4028
4029 if (q->need_update == 0)
4030 goto exit_unlock;
4031
Cahill, Ben M6440adb2007-11-29 11:09:55 +08004032 /* If power-saving is in use, make sure device is awake */
Zhu Yib481de92007-09-25 17:54:57 -07004033 if (test_bit(STATUS_POWER_PMI, &priv->status)) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004034 reg = iwl4965_read32(priv, CSR_UCODE_DRV_GP1);
Zhu Yib481de92007-09-25 17:54:57 -07004035
4036 if (reg & CSR_UCODE_DRV_GP1_BIT_MAC_SLEEP) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004037 iwl4965_set_bit(priv, CSR_GP_CNTRL,
Zhu Yib481de92007-09-25 17:54:57 -07004038 CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
4039 goto exit_unlock;
4040 }
4041
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004042 rc = iwl4965_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004043 if (rc)
4044 goto exit_unlock;
4045
Cahill, Ben M6440adb2007-11-29 11:09:55 +08004046 /* Device expects a multiple of 8 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004047 iwl4965_write_direct32(priv, FH_RSCSR_CHNL0_WPTR,
Zhu Yib481de92007-09-25 17:54:57 -07004048 q->write & ~0x7);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004049 iwl4965_release_nic_access(priv);
Cahill, Ben M6440adb2007-11-29 11:09:55 +08004050
4051 /* Else device is assumed to be awake */
Zhu Yib481de92007-09-25 17:54:57 -07004052 } else
Cahill, Ben M6440adb2007-11-29 11:09:55 +08004053 /* Device expects a multiple of 8 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004054 iwl4965_write32(priv, FH_RSCSR_CHNL0_WPTR, q->write & ~0x7);
Zhu Yib481de92007-09-25 17:54:57 -07004055
4056
4057 q->need_update = 0;
4058
4059 exit_unlock:
4060 spin_unlock_irqrestore(&q->lock, flags);
4061 return rc;
4062}
4063
4064/**
Ben Cahill9fbab512007-11-29 11:09:47 +08004065 * iwl4965_dma_addr2rbd_ptr - convert a DMA address to a uCode read buffer ptr
Zhu Yib481de92007-09-25 17:54:57 -07004066 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004067static inline __le32 iwl4965_dma_addr2rbd_ptr(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07004068 dma_addr_t dma_addr)
4069{
4070 return cpu_to_le32((u32)(dma_addr >> 8));
4071}
4072
4073
4074/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004075 * iwl4965_rx_queue_restock - refill RX queue from pre-allocated pool
Zhu Yib481de92007-09-25 17:54:57 -07004076 *
Ben Cahill9fbab512007-11-29 11:09:47 +08004077 * If there are slots in the RX queue that need to be restocked,
Zhu Yib481de92007-09-25 17:54:57 -07004078 * and we have free pre-allocated buffers, fill the ranks as much
Ben Cahill9fbab512007-11-29 11:09:47 +08004079 * as we can, pulling from rx_free.
Zhu Yib481de92007-09-25 17:54:57 -07004080 *
4081 * This moves the 'write' index forward to catch up with 'processed', and
4082 * also updates the memory address in the firmware to reference the new
4083 * target buffer.
4084 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004085static int iwl4965_rx_queue_restock(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004086{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004087 struct iwl4965_rx_queue *rxq = &priv->rxq;
Zhu Yib481de92007-09-25 17:54:57 -07004088 struct list_head *element;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004089 struct iwl4965_rx_mem_buffer *rxb;
Zhu Yib481de92007-09-25 17:54:57 -07004090 unsigned long flags;
4091 int write, rc;
4092
4093 spin_lock_irqsave(&rxq->lock, flags);
4094 write = rxq->write & ~0x7;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004095 while ((iwl4965_rx_queue_space(rxq) > 0) && (rxq->free_count)) {
Cahill, Ben M6440adb2007-11-29 11:09:55 +08004096 /* Get next free Rx buffer, remove from free list */
Zhu Yib481de92007-09-25 17:54:57 -07004097 element = rxq->rx_free.next;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004098 rxb = list_entry(element, struct iwl4965_rx_mem_buffer, list);
Zhu Yib481de92007-09-25 17:54:57 -07004099 list_del(element);
Cahill, Ben M6440adb2007-11-29 11:09:55 +08004100
4101 /* Point to Rx buffer via next RBD in circular buffer */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004102 rxq->bd[rxq->write] = iwl4965_dma_addr2rbd_ptr(priv, rxb->dma_addr);
Zhu Yib481de92007-09-25 17:54:57 -07004103 rxq->queue[rxq->write] = rxb;
4104 rxq->write = (rxq->write + 1) & RX_QUEUE_MASK;
4105 rxq->free_count--;
4106 }
4107 spin_unlock_irqrestore(&rxq->lock, flags);
4108 /* If the pre-allocated buffer pool is dropping low, schedule to
4109 * refill it */
4110 if (rxq->free_count <= RX_LOW_WATERMARK)
4111 queue_work(priv->workqueue, &priv->rx_replenish);
4112
4113
Cahill, Ben M6440adb2007-11-29 11:09:55 +08004114 /* If we've added more space for the firmware to place data, tell it.
4115 * Increment device's write pointer in multiples of 8. */
Zhu Yib481de92007-09-25 17:54:57 -07004116 if ((write != (rxq->write & ~0x7))
4117 || (abs(rxq->write - rxq->read) > 7)) {
4118 spin_lock_irqsave(&rxq->lock, flags);
4119 rxq->need_update = 1;
4120 spin_unlock_irqrestore(&rxq->lock, flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004121 rc = iwl4965_rx_queue_update_write_ptr(priv, rxq);
Zhu Yib481de92007-09-25 17:54:57 -07004122 if (rc)
4123 return rc;
4124 }
4125
4126 return 0;
4127}
4128
4129/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004130 * iwl4965_rx_replenish - Move all used packet from rx_used to rx_free
Zhu Yib481de92007-09-25 17:54:57 -07004131 *
4132 * When moving to rx_free an SKB is allocated for the slot.
4133 *
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004134 * Also restock the Rx queue via iwl4965_rx_queue_restock.
Ian Schram01ebd062007-10-25 17:15:22 +08004135 * This is called as a scheduled work item (except for during initialization)
Zhu Yib481de92007-09-25 17:54:57 -07004136 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004137static void iwl4965_rx_allocate(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004138{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004139 struct iwl4965_rx_queue *rxq = &priv->rxq;
Zhu Yib481de92007-09-25 17:54:57 -07004140 struct list_head *element;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004141 struct iwl4965_rx_mem_buffer *rxb;
Zhu Yib481de92007-09-25 17:54:57 -07004142 unsigned long flags;
4143 spin_lock_irqsave(&rxq->lock, flags);
4144 while (!list_empty(&rxq->rx_used)) {
4145 element = rxq->rx_used.next;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004146 rxb = list_entry(element, struct iwl4965_rx_mem_buffer, list);
Cahill, Ben M6440adb2007-11-29 11:09:55 +08004147
4148 /* Alloc a new receive buffer */
Zhu Yib481de92007-09-25 17:54:57 -07004149 rxb->skb =
Ron Rindjunsky9ee1ba42007-11-26 16:14:42 +02004150 alloc_skb(priv->hw_setting.rx_buf_size,
4151 __GFP_NOWARN | GFP_ATOMIC);
Zhu Yib481de92007-09-25 17:54:57 -07004152 if (!rxb->skb) {
4153 if (net_ratelimit())
4154 printk(KERN_CRIT DRV_NAME
4155 ": Can not allocate SKB buffers\n");
4156 /* We don't reschedule replenish work here -- we will
4157 * call the restock method and if it still needs
4158 * more buffers it will schedule replenish */
4159 break;
4160 }
4161 priv->alloc_rxb_skb++;
4162 list_del(element);
Cahill, Ben M6440adb2007-11-29 11:09:55 +08004163
4164 /* Get physical address of RB/SKB */
Zhu Yib481de92007-09-25 17:54:57 -07004165 rxb->dma_addr =
4166 pci_map_single(priv->pci_dev, rxb->skb->data,
Ron Rindjunsky9ee1ba42007-11-26 16:14:42 +02004167 priv->hw_setting.rx_buf_size, PCI_DMA_FROMDEVICE);
Zhu Yib481de92007-09-25 17:54:57 -07004168 list_add_tail(&rxb->list, &rxq->rx_free);
4169 rxq->free_count++;
4170 }
4171 spin_unlock_irqrestore(&rxq->lock, flags);
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08004172}
4173
4174/*
4175 * this should be called while priv->lock is locked
4176*/
Tomas Winkler4fd1f842007-12-05 20:59:58 +02004177static void __iwl4965_rx_replenish(void *data)
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08004178{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004179 struct iwl_priv *priv = data;
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08004180
4181 iwl4965_rx_allocate(priv);
4182 iwl4965_rx_queue_restock(priv);
4183}
4184
4185
4186void iwl4965_rx_replenish(void *data)
4187{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004188 struct iwl_priv *priv = data;
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08004189 unsigned long flags;
4190
4191 iwl4965_rx_allocate(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004192
4193 spin_lock_irqsave(&priv->lock, flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004194 iwl4965_rx_queue_restock(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004195 spin_unlock_irqrestore(&priv->lock, flags);
4196}
4197
4198/* Assumes that the skb field of the buffers in 'pool' is kept accurate.
Ben Cahill9fbab512007-11-29 11:09:47 +08004199 * If an SKB has been detached, the POOL needs to have its SKB set to NULL
Zhu Yib481de92007-09-25 17:54:57 -07004200 * This free routine walks the list of POOL entries and if SKB is set to
4201 * non NULL it is unmapped and freed
4202 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004203static void iwl4965_rx_queue_free(struct iwl_priv *priv, struct iwl4965_rx_queue *rxq)
Zhu Yib481de92007-09-25 17:54:57 -07004204{
4205 int i;
4206 for (i = 0; i < RX_QUEUE_SIZE + RX_FREE_BUFFERS; i++) {
4207 if (rxq->pool[i].skb != NULL) {
4208 pci_unmap_single(priv->pci_dev,
4209 rxq->pool[i].dma_addr,
Ron Rindjunsky9ee1ba42007-11-26 16:14:42 +02004210 priv->hw_setting.rx_buf_size,
4211 PCI_DMA_FROMDEVICE);
Zhu Yib481de92007-09-25 17:54:57 -07004212 dev_kfree_skb(rxq->pool[i].skb);
4213 }
4214 }
4215
4216 pci_free_consistent(priv->pci_dev, 4 * RX_QUEUE_SIZE, rxq->bd,
4217 rxq->dma_addr);
4218 rxq->bd = NULL;
4219}
4220
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004221int iwl4965_rx_queue_alloc(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004222{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004223 struct iwl4965_rx_queue *rxq = &priv->rxq;
Zhu Yib481de92007-09-25 17:54:57 -07004224 struct pci_dev *dev = priv->pci_dev;
4225 int i;
4226
4227 spin_lock_init(&rxq->lock);
4228 INIT_LIST_HEAD(&rxq->rx_free);
4229 INIT_LIST_HEAD(&rxq->rx_used);
Cahill, Ben M6440adb2007-11-29 11:09:55 +08004230
4231 /* Alloc the circular buffer of Read Buffer Descriptors (RBDs) */
Zhu Yib481de92007-09-25 17:54:57 -07004232 rxq->bd = pci_alloc_consistent(dev, 4 * RX_QUEUE_SIZE, &rxq->dma_addr);
4233 if (!rxq->bd)
4234 return -ENOMEM;
Cahill, Ben M6440adb2007-11-29 11:09:55 +08004235
Zhu Yib481de92007-09-25 17:54:57 -07004236 /* Fill the rx_used queue with _all_ of the Rx buffers */
4237 for (i = 0; i < RX_FREE_BUFFERS + RX_QUEUE_SIZE; i++)
4238 list_add_tail(&rxq->pool[i].list, &rxq->rx_used);
Cahill, Ben M6440adb2007-11-29 11:09:55 +08004239
Zhu Yib481de92007-09-25 17:54:57 -07004240 /* Set us so that we have processed and used all buffers, but have
4241 * not restocked the Rx queue with fresh buffers */
4242 rxq->read = rxq->write = 0;
4243 rxq->free_count = 0;
4244 rxq->need_update = 0;
4245 return 0;
4246}
4247
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004248void iwl4965_rx_queue_reset(struct iwl_priv *priv, struct iwl4965_rx_queue *rxq)
Zhu Yib481de92007-09-25 17:54:57 -07004249{
4250 unsigned long flags;
4251 int i;
4252 spin_lock_irqsave(&rxq->lock, flags);
4253 INIT_LIST_HEAD(&rxq->rx_free);
4254 INIT_LIST_HEAD(&rxq->rx_used);
4255 /* Fill the rx_used queue with _all_ of the Rx buffers */
4256 for (i = 0; i < RX_FREE_BUFFERS + RX_QUEUE_SIZE; i++) {
4257 /* In the reset function, these buffers may have been allocated
4258 * to an SKB, so we need to unmap and free potential storage */
4259 if (rxq->pool[i].skb != NULL) {
4260 pci_unmap_single(priv->pci_dev,
4261 rxq->pool[i].dma_addr,
Ron Rindjunsky9ee1ba42007-11-26 16:14:42 +02004262 priv->hw_setting.rx_buf_size,
4263 PCI_DMA_FROMDEVICE);
Zhu Yib481de92007-09-25 17:54:57 -07004264 priv->alloc_rxb_skb--;
4265 dev_kfree_skb(rxq->pool[i].skb);
4266 rxq->pool[i].skb = NULL;
4267 }
4268 list_add_tail(&rxq->pool[i].list, &rxq->rx_used);
4269 }
4270
4271 /* Set us so that we have processed and used all buffers, but have
4272 * not restocked the Rx queue with fresh buffers */
4273 rxq->read = rxq->write = 0;
4274 rxq->free_count = 0;
4275 spin_unlock_irqrestore(&rxq->lock, flags);
4276}
4277
4278/* Convert linear signal-to-noise ratio into dB */
4279static u8 ratio2dB[100] = {
4280/* 0 1 2 3 4 5 6 7 8 9 */
4281 0, 0, 6, 10, 12, 14, 16, 17, 18, 19, /* 00 - 09 */
4282 20, 21, 22, 22, 23, 23, 24, 25, 26, 26, /* 10 - 19 */
4283 26, 26, 26, 27, 27, 28, 28, 28, 29, 29, /* 20 - 29 */
4284 29, 30, 30, 30, 31, 31, 31, 31, 32, 32, /* 30 - 39 */
4285 32, 32, 32, 33, 33, 33, 33, 33, 34, 34, /* 40 - 49 */
4286 34, 34, 34, 34, 35, 35, 35, 35, 35, 35, /* 50 - 59 */
4287 36, 36, 36, 36, 36, 36, 36, 37, 37, 37, /* 60 - 69 */
4288 37, 37, 37, 37, 37, 38, 38, 38, 38, 38, /* 70 - 79 */
4289 38, 38, 38, 38, 38, 39, 39, 39, 39, 39, /* 80 - 89 */
4290 39, 39, 39, 39, 39, 40, 40, 40, 40, 40 /* 90 - 99 */
4291};
4292
4293/* Calculates a relative dB value from a ratio of linear
4294 * (i.e. not dB) signal levels.
4295 * Conversion assumes that levels are voltages (20*log), not powers (10*log). */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004296int iwl4965_calc_db_from_ratio(int sig_ratio)
Zhu Yib481de92007-09-25 17:54:57 -07004297{
Adrian Bunkc899a572007-10-14 19:51:15 +02004298 /* 1000:1 or higher just report as 60 dB */
4299 if (sig_ratio >= 1000)
Zhu Yib481de92007-09-25 17:54:57 -07004300 return 60;
4301
Adrian Bunkc899a572007-10-14 19:51:15 +02004302 /* 100:1 or higher, divide by 10 and use table,
Zhu Yib481de92007-09-25 17:54:57 -07004303 * add 20 dB to make up for divide by 10 */
Adrian Bunkc899a572007-10-14 19:51:15 +02004304 if (sig_ratio >= 100)
Zhu Yib481de92007-09-25 17:54:57 -07004305 return (20 + (int)ratio2dB[sig_ratio/10]);
4306
4307 /* We shouldn't see this */
4308 if (sig_ratio < 1)
4309 return 0;
4310
4311 /* Use table for ratios 1:1 - 99:1 */
4312 return (int)ratio2dB[sig_ratio];
4313}
4314
4315#define PERFECT_RSSI (-20) /* dBm */
4316#define WORST_RSSI (-95) /* dBm */
4317#define RSSI_RANGE (PERFECT_RSSI - WORST_RSSI)
4318
4319/* Calculate an indication of rx signal quality (a percentage, not dBm!).
4320 * See http://www.ces.clemson.edu/linux/signal_quality.shtml for info
4321 * about formulas used below. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004322int iwl4965_calc_sig_qual(int rssi_dbm, int noise_dbm)
Zhu Yib481de92007-09-25 17:54:57 -07004323{
4324 int sig_qual;
4325 int degradation = PERFECT_RSSI - rssi_dbm;
4326
4327 /* If we get a noise measurement, use signal-to-noise ratio (SNR)
4328 * as indicator; formula is (signal dbm - noise dbm).
4329 * SNR at or above 40 is a great signal (100%).
4330 * Below that, scale to fit SNR of 0 - 40 dB within 0 - 100% indicator.
4331 * Weakest usable signal is usually 10 - 15 dB SNR. */
4332 if (noise_dbm) {
4333 if (rssi_dbm - noise_dbm >= 40)
4334 return 100;
4335 else if (rssi_dbm < noise_dbm)
4336 return 0;
4337 sig_qual = ((rssi_dbm - noise_dbm) * 5) / 2;
4338
4339 /* Else use just the signal level.
4340 * This formula is a least squares fit of data points collected and
4341 * compared with a reference system that had a percentage (%) display
4342 * for signal quality. */
4343 } else
4344 sig_qual = (100 * (RSSI_RANGE * RSSI_RANGE) - degradation *
4345 (15 * RSSI_RANGE + 62 * degradation)) /
4346 (RSSI_RANGE * RSSI_RANGE);
4347
4348 if (sig_qual > 100)
4349 sig_qual = 100;
4350 else if (sig_qual < 1)
4351 sig_qual = 0;
4352
4353 return sig_qual;
4354}
4355
4356/**
Ben Cahill9fbab512007-11-29 11:09:47 +08004357 * iwl4965_rx_handle - Main entry function for receiving responses from uCode
Zhu Yib481de92007-09-25 17:54:57 -07004358 *
4359 * Uses the priv->rx_handlers callback function array to invoke
4360 * the appropriate handlers, including command responses,
4361 * frame-received notifications, and other notifications.
4362 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004363static void iwl4965_rx_handle(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004364{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004365 struct iwl4965_rx_mem_buffer *rxb;
4366 struct iwl4965_rx_packet *pkt;
4367 struct iwl4965_rx_queue *rxq = &priv->rxq;
Zhu Yib481de92007-09-25 17:54:57 -07004368 u32 r, i;
4369 int reclaim;
4370 unsigned long flags;
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08004371 u8 fill_rx = 0;
Mohamed Abbasd68ab682008-02-07 13:16:33 -08004372 u32 count = 8;
Zhu Yib481de92007-09-25 17:54:57 -07004373
Cahill, Ben M6440adb2007-11-29 11:09:55 +08004374 /* uCode's read index (stored in shared DRAM) indicates the last Rx
4375 * buffer that the driver may process (last buffer filled by ucode). */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004376 r = iwl4965_hw_get_rx_read(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004377 i = rxq->read;
4378
4379 /* Rx interrupt, but nothing sent from uCode */
4380 if (i == r)
4381 IWL_DEBUG(IWL_DL_RX | IWL_DL_ISR, "r = %d, i = %d\n", r, i);
4382
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08004383 if (iwl4965_rx_queue_space(rxq) > (RX_QUEUE_SIZE / 2))
4384 fill_rx = 1;
4385
Zhu Yib481de92007-09-25 17:54:57 -07004386 while (i != r) {
4387 rxb = rxq->queue[i];
4388
Ben Cahill9fbab512007-11-29 11:09:47 +08004389 /* If an RXB doesn't have a Rx queue slot associated with it,
Zhu Yib481de92007-09-25 17:54:57 -07004390 * then a bug has been introduced in the queue refilling
4391 * routines -- catch it here */
4392 BUG_ON(rxb == NULL);
4393
4394 rxq->queue[i] = NULL;
4395
4396 pci_dma_sync_single_for_cpu(priv->pci_dev, rxb->dma_addr,
Ron Rindjunsky9ee1ba42007-11-26 16:14:42 +02004397 priv->hw_setting.rx_buf_size,
Zhu Yib481de92007-09-25 17:54:57 -07004398 PCI_DMA_FROMDEVICE);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004399 pkt = (struct iwl4965_rx_packet *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07004400
4401 /* Reclaim a command buffer only if this packet is a response
4402 * to a (driver-originated) command.
4403 * If the packet (e.g. Rx frame) originated from uCode,
4404 * there is no command buffer to reclaim.
4405 * Ucode should set SEQ_RX_FRAME bit if ucode-originated,
4406 * but apparently a few don't get set; catch them here. */
4407 reclaim = !(pkt->hdr.sequence & SEQ_RX_FRAME) &&
4408 (pkt->hdr.cmd != REPLY_RX_PHY_CMD) &&
4409 (pkt->hdr.cmd != REPLY_4965_RX) &&
Zhu Yicfe01702007-09-27 11:27:31 +08004410 (pkt->hdr.cmd != REPLY_COMPRESSED_BA) &&
Zhu Yib481de92007-09-25 17:54:57 -07004411 (pkt->hdr.cmd != STATISTICS_NOTIFICATION) &&
4412 (pkt->hdr.cmd != REPLY_TX);
4413
4414 /* Based on type of command response or notification,
4415 * handle those that need handling via function in
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004416 * rx_handlers table. See iwl4965_setup_rx_handlers() */
Zhu Yib481de92007-09-25 17:54:57 -07004417 if (priv->rx_handlers[pkt->hdr.cmd]) {
4418 IWL_DEBUG(IWL_DL_HOST_COMMAND | IWL_DL_RX | IWL_DL_ISR,
4419 "r = %d, i = %d, %s, 0x%02x\n", r, i,
4420 get_cmd_string(pkt->hdr.cmd), pkt->hdr.cmd);
4421 priv->rx_handlers[pkt->hdr.cmd] (priv, rxb);
4422 } else {
4423 /* No handling needed */
4424 IWL_DEBUG(IWL_DL_HOST_COMMAND | IWL_DL_RX | IWL_DL_ISR,
4425 "r %d i %d No handler needed for %s, 0x%02x\n",
4426 r, i, get_cmd_string(pkt->hdr.cmd),
4427 pkt->hdr.cmd);
4428 }
4429
4430 if (reclaim) {
Ben Cahill9fbab512007-11-29 11:09:47 +08004431 /* Invoke any callbacks, transfer the skb to caller, and
4432 * fire off the (possibly) blocking iwl4965_send_cmd()
Zhu Yib481de92007-09-25 17:54:57 -07004433 * as we reclaim the driver command queue */
4434 if (rxb && rxb->skb)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004435 iwl4965_tx_cmd_complete(priv, rxb);
Zhu Yib481de92007-09-25 17:54:57 -07004436 else
4437 IWL_WARNING("Claim null rxb?\n");
4438 }
4439
4440 /* For now we just don't re-use anything. We can tweak this
4441 * later to try and re-use notification packets and SKBs that
4442 * fail to Rx correctly */
4443 if (rxb->skb != NULL) {
4444 priv->alloc_rxb_skb--;
4445 dev_kfree_skb_any(rxb->skb);
4446 rxb->skb = NULL;
4447 }
4448
4449 pci_unmap_single(priv->pci_dev, rxb->dma_addr,
Ron Rindjunsky9ee1ba42007-11-26 16:14:42 +02004450 priv->hw_setting.rx_buf_size,
4451 PCI_DMA_FROMDEVICE);
Zhu Yib481de92007-09-25 17:54:57 -07004452 spin_lock_irqsave(&rxq->lock, flags);
4453 list_add_tail(&rxb->list, &priv->rxq.rx_used);
4454 spin_unlock_irqrestore(&rxq->lock, flags);
4455 i = (i + 1) & RX_QUEUE_MASK;
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08004456 /* If there are a lot of unused frames,
4457 * restock the Rx queue so ucode wont assert. */
4458 if (fill_rx) {
4459 count++;
4460 if (count >= 8) {
4461 priv->rxq.read = i;
4462 __iwl4965_rx_replenish(priv);
4463 count = 0;
4464 }
4465 }
Zhu Yib481de92007-09-25 17:54:57 -07004466 }
4467
4468 /* Backtrack one entry */
4469 priv->rxq.read = i;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004470 iwl4965_rx_queue_restock(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004471}
4472
Cahill, Ben M6440adb2007-11-29 11:09:55 +08004473/**
4474 * iwl4965_tx_queue_update_write_ptr - Send new write index to hardware
4475 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004476static int iwl4965_tx_queue_update_write_ptr(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004477 struct iwl4965_tx_queue *txq)
Zhu Yib481de92007-09-25 17:54:57 -07004478{
4479 u32 reg = 0;
4480 int rc = 0;
4481 int txq_id = txq->q.id;
4482
4483 if (txq->need_update == 0)
4484 return rc;
4485
4486 /* if we're trying to save power */
4487 if (test_bit(STATUS_POWER_PMI, &priv->status)) {
4488 /* wake up nic if it's powered down ...
4489 * uCode will wake up, and interrupt us again, so next
4490 * time we'll skip this part. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004491 reg = iwl4965_read32(priv, CSR_UCODE_DRV_GP1);
Zhu Yib481de92007-09-25 17:54:57 -07004492
4493 if (reg & CSR_UCODE_DRV_GP1_BIT_MAC_SLEEP) {
4494 IWL_DEBUG_INFO("Requesting wakeup, GP1 = 0x%x\n", reg);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004495 iwl4965_set_bit(priv, CSR_GP_CNTRL,
Zhu Yib481de92007-09-25 17:54:57 -07004496 CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
4497 return rc;
4498 }
4499
4500 /* restore this queue's parameters in nic hardware. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004501 rc = iwl4965_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004502 if (rc)
4503 return rc;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004504 iwl4965_write_direct32(priv, HBUS_TARG_WRPTR,
Tomas Winklerfc4b6852007-10-25 17:15:24 +08004505 txq->q.write_ptr | (txq_id << 8));
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004506 iwl4965_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004507
4508 /* else not in power-save mode, uCode will never sleep when we're
4509 * trying to tx (during RFKILL, we're not trying to tx). */
4510 } else
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004511 iwl4965_write32(priv, HBUS_TARG_WRPTR,
Tomas Winklerfc4b6852007-10-25 17:15:24 +08004512 txq->q.write_ptr | (txq_id << 8));
Zhu Yib481de92007-09-25 17:54:57 -07004513
4514 txq->need_update = 0;
4515
4516 return rc;
4517}
4518
Tomas Winkler0a6857e2008-03-12 16:58:49 -07004519#ifdef CONFIG_IWLWIFI_DEBUG
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004520static void iwl4965_print_rx_config_cmd(struct iwl4965_rxon_cmd *rxon)
Zhu Yib481de92007-09-25 17:54:57 -07004521{
Joe Perches0795af52007-10-03 17:59:30 -07004522 DECLARE_MAC_BUF(mac);
4523
Zhu Yib481de92007-09-25 17:54:57 -07004524 IWL_DEBUG_RADIO("RX CONFIG:\n");
Tomas Winkler0a6857e2008-03-12 16:58:49 -07004525 iwl_print_hex_dump(IWL_DL_RADIO, (u8 *) rxon, sizeof(*rxon));
Zhu Yib481de92007-09-25 17:54:57 -07004526 IWL_DEBUG_RADIO("u16 channel: 0x%x\n", le16_to_cpu(rxon->channel));
4527 IWL_DEBUG_RADIO("u32 flags: 0x%08X\n", le32_to_cpu(rxon->flags));
4528 IWL_DEBUG_RADIO("u32 filter_flags: 0x%08x\n",
4529 le32_to_cpu(rxon->filter_flags));
4530 IWL_DEBUG_RADIO("u8 dev_type: 0x%x\n", rxon->dev_type);
4531 IWL_DEBUG_RADIO("u8 ofdm_basic_rates: 0x%02x\n",
4532 rxon->ofdm_basic_rates);
4533 IWL_DEBUG_RADIO("u8 cck_basic_rates: 0x%02x\n", rxon->cck_basic_rates);
Joe Perches0795af52007-10-03 17:59:30 -07004534 IWL_DEBUG_RADIO("u8[6] node_addr: %s\n",
4535 print_mac(mac, rxon->node_addr));
4536 IWL_DEBUG_RADIO("u8[6] bssid_addr: %s\n",
4537 print_mac(mac, rxon->bssid_addr));
Zhu Yib481de92007-09-25 17:54:57 -07004538 IWL_DEBUG_RADIO("u16 assoc_id: 0x%x\n", le16_to_cpu(rxon->assoc_id));
4539}
4540#endif
4541
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004542static void iwl4965_enable_interrupts(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004543{
4544 IWL_DEBUG_ISR("Enabling interrupts\n");
4545 set_bit(STATUS_INT_ENABLED, &priv->status);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004546 iwl4965_write32(priv, CSR_INT_MASK, CSR_INI_SET_MASK);
Zhu Yib481de92007-09-25 17:54:57 -07004547}
4548
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004549static inline void iwl4965_disable_interrupts(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004550{
4551 clear_bit(STATUS_INT_ENABLED, &priv->status);
4552
4553 /* disable interrupts from uCode/NIC to host */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004554 iwl4965_write32(priv, CSR_INT_MASK, 0x00000000);
Zhu Yib481de92007-09-25 17:54:57 -07004555
4556 /* acknowledge/clear/reset any interrupts still pending
4557 * from uCode or flow handler (Rx/Tx DMA) */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004558 iwl4965_write32(priv, CSR_INT, 0xffffffff);
4559 iwl4965_write32(priv, CSR_FH_INT_STATUS, 0xffffffff);
Zhu Yib481de92007-09-25 17:54:57 -07004560 IWL_DEBUG_ISR("Disabled interrupts\n");
4561}
4562
4563static const char *desc_lookup(int i)
4564{
4565 switch (i) {
4566 case 1:
4567 return "FAIL";
4568 case 2:
4569 return "BAD_PARAM";
4570 case 3:
4571 return "BAD_CHECKSUM";
4572 case 4:
4573 return "NMI_INTERRUPT";
4574 case 5:
4575 return "SYSASSERT";
4576 case 6:
4577 return "FATAL_ERROR";
4578 }
4579
4580 return "UNKNOWN";
4581}
4582
4583#define ERROR_START_OFFSET (1 * sizeof(u32))
4584#define ERROR_ELEM_SIZE (7 * sizeof(u32))
4585
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004586static void iwl4965_dump_nic_error_log(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004587{
4588 u32 data2, line;
4589 u32 desc, time, count, base, data1;
4590 u32 blink1, blink2, ilink1, ilink2;
4591 int rc;
4592
4593 base = le32_to_cpu(priv->card_alive.error_event_table_ptr);
4594
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004595 if (!iwl4965_hw_valid_rtc_data_addr(base)) {
Zhu Yib481de92007-09-25 17:54:57 -07004596 IWL_ERROR("Not valid error log pointer 0x%08X\n", base);
4597 return;
4598 }
4599
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004600 rc = iwl4965_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004601 if (rc) {
4602 IWL_WARNING("Can not read from adapter at this time.\n");
4603 return;
4604 }
4605
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004606 count = iwl4965_read_targ_mem(priv, base);
Zhu Yib481de92007-09-25 17:54:57 -07004607
4608 if (ERROR_START_OFFSET <= count * ERROR_ELEM_SIZE) {
4609 IWL_ERROR("Start IWL Error Log Dump:\n");
Tomas Winkler2acae162008-03-02 01:25:59 +02004610 IWL_ERROR("Status: 0x%08lX, count: %d\n", priv->status, count);
Zhu Yib481de92007-09-25 17:54:57 -07004611 }
4612
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004613 desc = iwl4965_read_targ_mem(priv, base + 1 * sizeof(u32));
4614 blink1 = iwl4965_read_targ_mem(priv, base + 3 * sizeof(u32));
4615 blink2 = iwl4965_read_targ_mem(priv, base + 4 * sizeof(u32));
4616 ilink1 = iwl4965_read_targ_mem(priv, base + 5 * sizeof(u32));
4617 ilink2 = iwl4965_read_targ_mem(priv, base + 6 * sizeof(u32));
4618 data1 = iwl4965_read_targ_mem(priv, base + 7 * sizeof(u32));
4619 data2 = iwl4965_read_targ_mem(priv, base + 8 * sizeof(u32));
4620 line = iwl4965_read_targ_mem(priv, base + 9 * sizeof(u32));
4621 time = iwl4965_read_targ_mem(priv, base + 11 * sizeof(u32));
Zhu Yib481de92007-09-25 17:54:57 -07004622
4623 IWL_ERROR("Desc Time "
4624 "data1 data2 line\n");
4625 IWL_ERROR("%-13s (#%d) %010u 0x%08X 0x%08X %u\n",
4626 desc_lookup(desc), desc, time, data1, data2, line);
4627 IWL_ERROR("blink1 blink2 ilink1 ilink2\n");
4628 IWL_ERROR("0x%05X 0x%05X 0x%05X 0x%05X\n", blink1, blink2,
4629 ilink1, ilink2);
4630
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004631 iwl4965_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004632}
4633
4634#define EVENT_START_OFFSET (4 * sizeof(u32))
4635
4636/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004637 * iwl4965_print_event_log - Dump error event log to syslog
Zhu Yib481de92007-09-25 17:54:57 -07004638 *
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004639 * NOTE: Must be called with iwl4965_grab_nic_access() already obtained!
Zhu Yib481de92007-09-25 17:54:57 -07004640 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004641static void iwl4965_print_event_log(struct iwl_priv *priv, u32 start_idx,
Zhu Yib481de92007-09-25 17:54:57 -07004642 u32 num_events, u32 mode)
4643{
4644 u32 i;
4645 u32 base; /* SRAM byte address of event log header */
4646 u32 event_size; /* 2 u32s, or 3 u32s if timestamp recorded */
4647 u32 ptr; /* SRAM byte address of log data */
4648 u32 ev, time, data; /* event log data */
4649
4650 if (num_events == 0)
4651 return;
4652
4653 base = le32_to_cpu(priv->card_alive.log_event_table_ptr);
4654
4655 if (mode == 0)
4656 event_size = 2 * sizeof(u32);
4657 else
4658 event_size = 3 * sizeof(u32);
4659
4660 ptr = base + EVENT_START_OFFSET + (start_idx * event_size);
4661
4662 /* "time" is actually "data" for mode 0 (no timestamp).
4663 * place event id # at far right for easier visual parsing. */
4664 for (i = 0; i < num_events; i++) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004665 ev = iwl4965_read_targ_mem(priv, ptr);
Zhu Yib481de92007-09-25 17:54:57 -07004666 ptr += sizeof(u32);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004667 time = iwl4965_read_targ_mem(priv, ptr);
Zhu Yib481de92007-09-25 17:54:57 -07004668 ptr += sizeof(u32);
4669 if (mode == 0)
4670 IWL_ERROR("0x%08x\t%04u\n", time, ev); /* data, ev */
4671 else {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004672 data = iwl4965_read_targ_mem(priv, ptr);
Zhu Yib481de92007-09-25 17:54:57 -07004673 ptr += sizeof(u32);
4674 IWL_ERROR("%010u\t0x%08x\t%04u\n", time, data, ev);
4675 }
4676 }
4677}
4678
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004679static void iwl4965_dump_nic_event_log(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004680{
4681 int rc;
4682 u32 base; /* SRAM byte address of event log header */
4683 u32 capacity; /* event log capacity in # entries */
4684 u32 mode; /* 0 - no timestamp, 1 - timestamp recorded */
4685 u32 num_wraps; /* # times uCode wrapped to top of log */
4686 u32 next_entry; /* index of next entry to be written by uCode */
4687 u32 size; /* # entries that we'll print */
4688
4689 base = le32_to_cpu(priv->card_alive.log_event_table_ptr);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004690 if (!iwl4965_hw_valid_rtc_data_addr(base)) {
Zhu Yib481de92007-09-25 17:54:57 -07004691 IWL_ERROR("Invalid event log pointer 0x%08X\n", base);
4692 return;
4693 }
4694
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004695 rc = iwl4965_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004696 if (rc) {
4697 IWL_WARNING("Can not read from adapter at this time.\n");
4698 return;
4699 }
4700
4701 /* event log header */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004702 capacity = iwl4965_read_targ_mem(priv, base);
4703 mode = iwl4965_read_targ_mem(priv, base + (1 * sizeof(u32)));
4704 num_wraps = iwl4965_read_targ_mem(priv, base + (2 * sizeof(u32)));
4705 next_entry = iwl4965_read_targ_mem(priv, base + (3 * sizeof(u32)));
Zhu Yib481de92007-09-25 17:54:57 -07004706
4707 size = num_wraps ? capacity : next_entry;
4708
4709 /* bail out if nothing in log */
4710 if (size == 0) {
Zhu Yi583fab32007-09-27 11:27:30 +08004711 IWL_ERROR("Start IWL Event Log Dump: nothing in log\n");
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004712 iwl4965_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004713 return;
4714 }
4715
Zhu Yi583fab32007-09-27 11:27:30 +08004716 IWL_ERROR("Start IWL Event Log Dump: display count %d, wraps %d\n",
Zhu Yib481de92007-09-25 17:54:57 -07004717 size, num_wraps);
4718
4719 /* if uCode has wrapped back to top of log, start at the oldest entry,
4720 * i.e the next one that uCode would fill. */
4721 if (num_wraps)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004722 iwl4965_print_event_log(priv, next_entry,
Zhu Yib481de92007-09-25 17:54:57 -07004723 capacity - next_entry, mode);
4724
4725 /* (then/else) start at top of log */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004726 iwl4965_print_event_log(priv, 0, next_entry, mode);
Zhu Yib481de92007-09-25 17:54:57 -07004727
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004728 iwl4965_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004729}
4730
4731/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004732 * iwl4965_irq_handle_error - called for HW or SW error interrupt from card
Zhu Yib481de92007-09-25 17:54:57 -07004733 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004734static void iwl4965_irq_handle_error(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004735{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004736 /* Set the FW error flag -- cleared on iwl4965_down */
Zhu Yib481de92007-09-25 17:54:57 -07004737 set_bit(STATUS_FW_ERROR, &priv->status);
4738
4739 /* Cancel currently queued command. */
4740 clear_bit(STATUS_HCMD_ACTIVE, &priv->status);
4741
Tomas Winkler0a6857e2008-03-12 16:58:49 -07004742#ifdef CONFIG_IWLWIFI_DEBUG
4743 if (iwl_debug_level & IWL_DL_FW_ERRORS) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004744 iwl4965_dump_nic_error_log(priv);
4745 iwl4965_dump_nic_event_log(priv);
4746 iwl4965_print_rx_config_cmd(&priv->staging_rxon);
Zhu Yib481de92007-09-25 17:54:57 -07004747 }
4748#endif
4749
4750 wake_up_interruptible(&priv->wait_command_queue);
4751
4752 /* Keep the restart process from trying to send host
4753 * commands by clearing the INIT status bit */
4754 clear_bit(STATUS_READY, &priv->status);
4755
4756 if (!test_bit(STATUS_EXIT_PENDING, &priv->status)) {
4757 IWL_DEBUG(IWL_DL_INFO | IWL_DL_FW_ERRORS,
4758 "Restarting adapter due to uCode error.\n");
4759
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004760 if (iwl4965_is_associated(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07004761 memcpy(&priv->recovery_rxon, &priv->active_rxon,
4762 sizeof(priv->recovery_rxon));
4763 priv->error_recovering = 1;
4764 }
4765 queue_work(priv->workqueue, &priv->restart);
4766 }
4767}
4768
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004769static void iwl4965_error_recovery(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004770{
4771 unsigned long flags;
4772
4773 memcpy(&priv->staging_rxon, &priv->recovery_rxon,
4774 sizeof(priv->staging_rxon));
4775 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004776 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004777
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004778 iwl4965_rxon_add_station(priv, priv->bssid, 1);
Zhu Yib481de92007-09-25 17:54:57 -07004779
4780 spin_lock_irqsave(&priv->lock, flags);
4781 priv->assoc_id = le16_to_cpu(priv->staging_rxon.assoc_id);
4782 priv->error_recovering = 0;
4783 spin_unlock_irqrestore(&priv->lock, flags);
4784}
4785
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004786static void iwl4965_irq_tasklet(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004787{
4788 u32 inta, handled = 0;
4789 u32 inta_fh;
4790 unsigned long flags;
Tomas Winkler0a6857e2008-03-12 16:58:49 -07004791#ifdef CONFIG_IWLWIFI_DEBUG
Zhu Yib481de92007-09-25 17:54:57 -07004792 u32 inta_mask;
4793#endif
4794
4795 spin_lock_irqsave(&priv->lock, flags);
4796
4797 /* Ack/clear/reset pending uCode interrupts.
4798 * Note: Some bits in CSR_INT are "OR" of bits in CSR_FH_INT_STATUS,
4799 * and will clear only when CSR_FH_INT_STATUS gets cleared. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004800 inta = iwl4965_read32(priv, CSR_INT);
4801 iwl4965_write32(priv, CSR_INT, inta);
Zhu Yib481de92007-09-25 17:54:57 -07004802
4803 /* Ack/clear/reset pending flow-handler (DMA) interrupts.
4804 * Any new interrupts that happen after this, either while we're
4805 * in this tasklet, or later, will show up in next ISR/tasklet. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004806 inta_fh = iwl4965_read32(priv, CSR_FH_INT_STATUS);
4807 iwl4965_write32(priv, CSR_FH_INT_STATUS, inta_fh);
Zhu Yib481de92007-09-25 17:54:57 -07004808
Tomas Winkler0a6857e2008-03-12 16:58:49 -07004809#ifdef CONFIG_IWLWIFI_DEBUG
4810 if (iwl_debug_level & IWL_DL_ISR) {
Ben Cahill9fbab512007-11-29 11:09:47 +08004811 /* just for debug */
4812 inta_mask = iwl4965_read32(priv, CSR_INT_MASK);
Zhu Yib481de92007-09-25 17:54:57 -07004813 IWL_DEBUG_ISR("inta 0x%08x, enabled 0x%08x, fh 0x%08x\n",
4814 inta, inta_mask, inta_fh);
4815 }
4816#endif
4817
4818 /* Since CSR_INT and CSR_FH_INT_STATUS reads and clears are not
4819 * atomic, make sure that inta covers all the interrupts that
4820 * we've discovered, even if FH interrupt came in just after
4821 * reading CSR_INT. */
Tomas Winkler6f83eaa2008-03-04 18:09:28 -08004822 if (inta_fh & CSR49_FH_INT_RX_MASK)
Zhu Yib481de92007-09-25 17:54:57 -07004823 inta |= CSR_INT_BIT_FH_RX;
Tomas Winkler6f83eaa2008-03-04 18:09:28 -08004824 if (inta_fh & CSR49_FH_INT_TX_MASK)
Zhu Yib481de92007-09-25 17:54:57 -07004825 inta |= CSR_INT_BIT_FH_TX;
4826
4827 /* Now service all interrupt bits discovered above. */
4828 if (inta & CSR_INT_BIT_HW_ERR) {
4829 IWL_ERROR("Microcode HW error detected. Restarting.\n");
4830
4831 /* Tell the device to stop sending interrupts */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004832 iwl4965_disable_interrupts(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004833
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004834 iwl4965_irq_handle_error(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004835
4836 handled |= CSR_INT_BIT_HW_ERR;
4837
4838 spin_unlock_irqrestore(&priv->lock, flags);
4839
4840 return;
4841 }
4842
Tomas Winkler0a6857e2008-03-12 16:58:49 -07004843#ifdef CONFIG_IWLWIFI_DEBUG
4844 if (iwl_debug_level & (IWL_DL_ISR)) {
Zhu Yib481de92007-09-25 17:54:57 -07004845 /* NIC fires this, but we don't use it, redundant with WAKEUP */
Joonwoo Park25c03d82008-01-23 10:15:20 -08004846 if (inta & CSR_INT_BIT_SCD)
4847 IWL_DEBUG_ISR("Scheduler finished to transmit "
4848 "the frame/frames.\n");
Zhu Yib481de92007-09-25 17:54:57 -07004849
4850 /* Alive notification via Rx interrupt will do the real work */
4851 if (inta & CSR_INT_BIT_ALIVE)
4852 IWL_DEBUG_ISR("Alive interrupt\n");
4853 }
4854#endif
4855 /* Safely ignore these bits for debug checks below */
Joonwoo Park25c03d82008-01-23 10:15:20 -08004856 inta &= ~(CSR_INT_BIT_SCD | CSR_INT_BIT_ALIVE);
Zhu Yib481de92007-09-25 17:54:57 -07004857
Ben Cahill9fbab512007-11-29 11:09:47 +08004858 /* HW RF KILL switch toggled */
Zhu Yib481de92007-09-25 17:54:57 -07004859 if (inta & CSR_INT_BIT_RF_KILL) {
4860 int hw_rf_kill = 0;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004861 if (!(iwl4965_read32(priv, CSR_GP_CNTRL) &
Zhu Yib481de92007-09-25 17:54:57 -07004862 CSR_GP_CNTRL_REG_FLAG_HW_RF_KILL_SW))
4863 hw_rf_kill = 1;
4864
4865 IWL_DEBUG(IWL_DL_INFO | IWL_DL_RF_KILL | IWL_DL_ISR,
4866 "RF_KILL bit toggled to %s.\n",
4867 hw_rf_kill ? "disable radio":"enable radio");
4868
4869 /* Queue restart only if RF_KILL switch was set to "kill"
4870 * when we loaded driver, and is now set to "enable".
4871 * After we're Alive, RF_KILL gets handled by
Reinette Chatre32304552008-02-15 14:34:37 -08004872 * iwl4965_rx_card_state_notif() */
Zhu Yi53e49092007-12-06 16:08:44 +08004873 if (!hw_rf_kill && !test_bit(STATUS_ALIVE, &priv->status)) {
4874 clear_bit(STATUS_RF_KILL_HW, &priv->status);
Zhu Yib481de92007-09-25 17:54:57 -07004875 queue_work(priv->workqueue, &priv->restart);
Zhu Yi53e49092007-12-06 16:08:44 +08004876 }
Zhu Yib481de92007-09-25 17:54:57 -07004877
4878 handled |= CSR_INT_BIT_RF_KILL;
4879 }
4880
Ben Cahill9fbab512007-11-29 11:09:47 +08004881 /* Chip got too hot and stopped itself */
Zhu Yib481de92007-09-25 17:54:57 -07004882 if (inta & CSR_INT_BIT_CT_KILL) {
4883 IWL_ERROR("Microcode CT kill error detected.\n");
4884 handled |= CSR_INT_BIT_CT_KILL;
4885 }
4886
4887 /* Error detected by uCode */
4888 if (inta & CSR_INT_BIT_SW_ERR) {
4889 IWL_ERROR("Microcode SW error detected. Restarting 0x%X.\n",
4890 inta);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004891 iwl4965_irq_handle_error(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004892 handled |= CSR_INT_BIT_SW_ERR;
4893 }
4894
4895 /* uCode wakes up after power-down sleep */
4896 if (inta & CSR_INT_BIT_WAKEUP) {
4897 IWL_DEBUG_ISR("Wakeup interrupt\n");
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004898 iwl4965_rx_queue_update_write_ptr(priv, &priv->rxq);
4899 iwl4965_tx_queue_update_write_ptr(priv, &priv->txq[0]);
4900 iwl4965_tx_queue_update_write_ptr(priv, &priv->txq[1]);
4901 iwl4965_tx_queue_update_write_ptr(priv, &priv->txq[2]);
4902 iwl4965_tx_queue_update_write_ptr(priv, &priv->txq[3]);
4903 iwl4965_tx_queue_update_write_ptr(priv, &priv->txq[4]);
4904 iwl4965_tx_queue_update_write_ptr(priv, &priv->txq[5]);
Zhu Yib481de92007-09-25 17:54:57 -07004905
4906 handled |= CSR_INT_BIT_WAKEUP;
4907 }
4908
4909 /* All uCode command responses, including Tx command responses,
4910 * Rx "responses" (frame-received notification), and other
4911 * notifications from uCode come through here*/
4912 if (inta & (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX)) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004913 iwl4965_rx_handle(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004914 handled |= (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX);
4915 }
4916
4917 if (inta & CSR_INT_BIT_FH_TX) {
4918 IWL_DEBUG_ISR("Tx interrupt\n");
4919 handled |= CSR_INT_BIT_FH_TX;
4920 }
4921
4922 if (inta & ~handled)
4923 IWL_ERROR("Unhandled INTA bits 0x%08x\n", inta & ~handled);
4924
4925 if (inta & ~CSR_INI_SET_MASK) {
4926 IWL_WARNING("Disabled INTA bits 0x%08x were pending\n",
4927 inta & ~CSR_INI_SET_MASK);
4928 IWL_WARNING(" with FH_INT = 0x%08x\n", inta_fh);
4929 }
4930
4931 /* Re-enable all interrupts */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004932 iwl4965_enable_interrupts(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004933
Tomas Winkler0a6857e2008-03-12 16:58:49 -07004934#ifdef CONFIG_IWLWIFI_DEBUG
4935 if (iwl_debug_level & (IWL_DL_ISR)) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004936 inta = iwl4965_read32(priv, CSR_INT);
4937 inta_mask = iwl4965_read32(priv, CSR_INT_MASK);
4938 inta_fh = iwl4965_read32(priv, CSR_FH_INT_STATUS);
Zhu Yib481de92007-09-25 17:54:57 -07004939 IWL_DEBUG_ISR("End inta 0x%08x, enabled 0x%08x, fh 0x%08x, "
4940 "flags 0x%08lx\n", inta, inta_mask, inta_fh, flags);
4941 }
4942#endif
4943 spin_unlock_irqrestore(&priv->lock, flags);
4944}
4945
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004946static irqreturn_t iwl4965_isr(int irq, void *data)
Zhu Yib481de92007-09-25 17:54:57 -07004947{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004948 struct iwl_priv *priv = data;
Zhu Yib481de92007-09-25 17:54:57 -07004949 u32 inta, inta_mask;
4950 u32 inta_fh;
4951 if (!priv)
4952 return IRQ_NONE;
4953
4954 spin_lock(&priv->lock);
4955
4956 /* Disable (but don't clear!) interrupts here to avoid
4957 * back-to-back ISRs and sporadic interrupts from our NIC.
4958 * If we have something to service, the tasklet will re-enable ints.
4959 * If we *don't* have something, we'll re-enable before leaving here. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004960 inta_mask = iwl4965_read32(priv, CSR_INT_MASK); /* just for debug */
4961 iwl4965_write32(priv, CSR_INT_MASK, 0x00000000);
Zhu Yib481de92007-09-25 17:54:57 -07004962
4963 /* Discover which interrupts are active/pending */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004964 inta = iwl4965_read32(priv, CSR_INT);
4965 inta_fh = iwl4965_read32(priv, CSR_FH_INT_STATUS);
Zhu Yib481de92007-09-25 17:54:57 -07004966
4967 /* Ignore interrupt if there's nothing in NIC to service.
4968 * This may be due to IRQ shared with another device,
4969 * or due to sporadic interrupts thrown from our NIC. */
4970 if (!inta && !inta_fh) {
4971 IWL_DEBUG_ISR("Ignore interrupt, inta == 0, inta_fh == 0\n");
4972 goto none;
4973 }
4974
4975 if ((inta == 0xFFFFFFFF) || ((inta & 0xFFFFFFF0) == 0xa5a5a5a0)) {
Oliver Neukum66fbb542007-11-15 09:31:10 +08004976 /* Hardware disappeared. It might have already raised
4977 * an interrupt */
Zhu Yib481de92007-09-25 17:54:57 -07004978 IWL_WARNING("HARDWARE GONE?? INTA == 0x%080x\n", inta);
Oliver Neukum66fbb542007-11-15 09:31:10 +08004979 goto unplugged;
Zhu Yib481de92007-09-25 17:54:57 -07004980 }
4981
4982 IWL_DEBUG_ISR("ISR inta 0x%08x, enabled 0x%08x, fh 0x%08x\n",
4983 inta, inta_mask, inta_fh);
4984
Joonwoo Park25c03d82008-01-23 10:15:20 -08004985 inta &= ~CSR_INT_BIT_SCD;
4986
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004987 /* iwl4965_irq_tasklet() will service interrupts and re-enable them */
Joonwoo Park25c03d82008-01-23 10:15:20 -08004988 if (likely(inta || inta_fh))
4989 tasklet_schedule(&priv->irq_tasklet);
Zhu Yib481de92007-09-25 17:54:57 -07004990
Oliver Neukum66fbb542007-11-15 09:31:10 +08004991 unplugged:
4992 spin_unlock(&priv->lock);
Zhu Yib481de92007-09-25 17:54:57 -07004993 return IRQ_HANDLED;
4994
4995 none:
4996 /* re-enable interrupts here since we don't have anything to service. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004997 iwl4965_enable_interrupts(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004998 spin_unlock(&priv->lock);
4999 return IRQ_NONE;
5000}
5001
5002/************************** EEPROM BANDS ****************************
5003 *
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005004 * The iwl4965_eeprom_band definitions below provide the mapping from the
Zhu Yib481de92007-09-25 17:54:57 -07005005 * EEPROM contents to the specific channel number supported for each
5006 * band.
5007 *
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005008 * For example, iwl_priv->eeprom.band_3_channels[4] from the band_3
Zhu Yib481de92007-09-25 17:54:57 -07005009 * definition below maps to physical channel 42 in the 5.2GHz spectrum.
5010 * The specific geography and calibration information for that channel
5011 * is contained in the eeprom map itself.
5012 *
5013 * During init, we copy the eeprom information and channel map
5014 * information into priv->channel_info_24/52 and priv->channel_map_24/52
5015 *
5016 * channel_map_24/52 provides the index in the channel_info array for a
5017 * given channel. We have to have two separate maps as there is channel
5018 * overlap with the 2.4GHz and 5.2GHz spectrum as seen in band_1 and
5019 * band_2
5020 *
5021 * A value of 0xff stored in the channel_map indicates that the channel
5022 * is not supported by the hardware at all.
5023 *
5024 * A value of 0xfe in the channel_map indicates that the channel is not
5025 * valid for Tx with the current hardware. This means that
5026 * while the system can tune and receive on a given channel, it may not
5027 * be able to associate or transmit any frames on that
5028 * channel. There is no corresponding channel information for that
5029 * entry.
5030 *
5031 *********************************************************************/
5032
5033/* 2.4 GHz */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005034static const u8 iwl4965_eeprom_band_1[14] = {
Zhu Yib481de92007-09-25 17:54:57 -07005035 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14
5036};
5037
5038/* 5.2 GHz bands */
Ben Cahill9fbab512007-11-29 11:09:47 +08005039static const u8 iwl4965_eeprom_band_2[] = { /* 4915-5080MHz */
Zhu Yib481de92007-09-25 17:54:57 -07005040 183, 184, 185, 187, 188, 189, 192, 196, 7, 8, 11, 12, 16
5041};
5042
Ben Cahill9fbab512007-11-29 11:09:47 +08005043static const u8 iwl4965_eeprom_band_3[] = { /* 5170-5320MHz */
Zhu Yib481de92007-09-25 17:54:57 -07005044 34, 36, 38, 40, 42, 44, 46, 48, 52, 56, 60, 64
5045};
5046
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005047static const u8 iwl4965_eeprom_band_4[] = { /* 5500-5700MHz */
Zhu Yib481de92007-09-25 17:54:57 -07005048 100, 104, 108, 112, 116, 120, 124, 128, 132, 136, 140
5049};
5050
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005051static const u8 iwl4965_eeprom_band_5[] = { /* 5725-5825MHz */
Zhu Yib481de92007-09-25 17:54:57 -07005052 145, 149, 153, 157, 161, 165
5053};
5054
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005055static u8 iwl4965_eeprom_band_6[] = { /* 2.4 FAT channel */
Zhu Yib481de92007-09-25 17:54:57 -07005056 1, 2, 3, 4, 5, 6, 7
5057};
5058
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005059static u8 iwl4965_eeprom_band_7[] = { /* 5.2 FAT channel */
Zhu Yib481de92007-09-25 17:54:57 -07005060 36, 44, 52, 60, 100, 108, 116, 124, 132, 149, 157
5061};
5062
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005063static void iwl4965_init_band_reference(const struct iwl_priv *priv,
Ben Cahill9fbab512007-11-29 11:09:47 +08005064 int band,
Zhu Yib481de92007-09-25 17:54:57 -07005065 int *eeprom_ch_count,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005066 const struct iwl4965_eeprom_channel
Zhu Yib481de92007-09-25 17:54:57 -07005067 **eeprom_ch_info,
5068 const u8 **eeprom_ch_index)
5069{
5070 switch (band) {
5071 case 1: /* 2.4GHz band */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005072 *eeprom_ch_count = ARRAY_SIZE(iwl4965_eeprom_band_1);
Zhu Yib481de92007-09-25 17:54:57 -07005073 *eeprom_ch_info = priv->eeprom.band_1_channels;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005074 *eeprom_ch_index = iwl4965_eeprom_band_1;
Zhu Yib481de92007-09-25 17:54:57 -07005075 break;
Ben Cahill9fbab512007-11-29 11:09:47 +08005076 case 2: /* 4.9GHz band */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005077 *eeprom_ch_count = ARRAY_SIZE(iwl4965_eeprom_band_2);
Zhu Yib481de92007-09-25 17:54:57 -07005078 *eeprom_ch_info = priv->eeprom.band_2_channels;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005079 *eeprom_ch_index = iwl4965_eeprom_band_2;
Zhu Yib481de92007-09-25 17:54:57 -07005080 break;
5081 case 3: /* 5.2GHz band */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005082 *eeprom_ch_count = ARRAY_SIZE(iwl4965_eeprom_band_3);
Zhu Yib481de92007-09-25 17:54:57 -07005083 *eeprom_ch_info = priv->eeprom.band_3_channels;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005084 *eeprom_ch_index = iwl4965_eeprom_band_3;
Zhu Yib481de92007-09-25 17:54:57 -07005085 break;
Ben Cahill9fbab512007-11-29 11:09:47 +08005086 case 4: /* 5.5GHz band */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005087 *eeprom_ch_count = ARRAY_SIZE(iwl4965_eeprom_band_4);
Zhu Yib481de92007-09-25 17:54:57 -07005088 *eeprom_ch_info = priv->eeprom.band_4_channels;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005089 *eeprom_ch_index = iwl4965_eeprom_band_4;
Zhu Yib481de92007-09-25 17:54:57 -07005090 break;
Ben Cahill9fbab512007-11-29 11:09:47 +08005091 case 5: /* 5.7GHz band */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005092 *eeprom_ch_count = ARRAY_SIZE(iwl4965_eeprom_band_5);
Zhu Yib481de92007-09-25 17:54:57 -07005093 *eeprom_ch_info = priv->eeprom.band_5_channels;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005094 *eeprom_ch_index = iwl4965_eeprom_band_5;
Zhu Yib481de92007-09-25 17:54:57 -07005095 break;
Ben Cahill9fbab512007-11-29 11:09:47 +08005096 case 6: /* 2.4GHz FAT channels */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005097 *eeprom_ch_count = ARRAY_SIZE(iwl4965_eeprom_band_6);
Zhu Yib481de92007-09-25 17:54:57 -07005098 *eeprom_ch_info = priv->eeprom.band_24_channels;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005099 *eeprom_ch_index = iwl4965_eeprom_band_6;
Zhu Yib481de92007-09-25 17:54:57 -07005100 break;
Ben Cahill9fbab512007-11-29 11:09:47 +08005101 case 7: /* 5 GHz FAT channels */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005102 *eeprom_ch_count = ARRAY_SIZE(iwl4965_eeprom_band_7);
Zhu Yib481de92007-09-25 17:54:57 -07005103 *eeprom_ch_info = priv->eeprom.band_52_channels;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005104 *eeprom_ch_index = iwl4965_eeprom_band_7;
Zhu Yib481de92007-09-25 17:54:57 -07005105 break;
5106 default:
5107 BUG();
5108 return;
5109 }
5110}
5111
Cahill, Ben M6440adb2007-11-29 11:09:55 +08005112/**
5113 * iwl4965_get_channel_info - Find driver's private channel info
5114 *
5115 * Based on band and channel number.
5116 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005117const struct iwl4965_channel_info *iwl4965_get_channel_info(const struct iwl_priv *priv,
Johannes Berg8318d782008-01-24 19:38:38 +01005118 enum ieee80211_band band, u16 channel)
Zhu Yib481de92007-09-25 17:54:57 -07005119{
5120 int i;
5121
Johannes Berg8318d782008-01-24 19:38:38 +01005122 switch (band) {
5123 case IEEE80211_BAND_5GHZ:
Zhu Yib481de92007-09-25 17:54:57 -07005124 for (i = 14; i < priv->channel_count; i++) {
5125 if (priv->channel_info[i].channel == channel)
5126 return &priv->channel_info[i];
5127 }
5128 break;
Johannes Berg8318d782008-01-24 19:38:38 +01005129 case IEEE80211_BAND_2GHZ:
Zhu Yib481de92007-09-25 17:54:57 -07005130 if (channel >= 1 && channel <= 14)
5131 return &priv->channel_info[channel - 1];
5132 break;
Johannes Berg8318d782008-01-24 19:38:38 +01005133 default:
5134 BUG();
Zhu Yib481de92007-09-25 17:54:57 -07005135 }
5136
5137 return NULL;
5138}
5139
5140#define CHECK_AND_PRINT(x) ((eeprom_ch_info[ch].flags & EEPROM_CHANNEL_##x) \
5141 ? # x " " : "")
5142
Cahill, Ben M6440adb2007-11-29 11:09:55 +08005143/**
5144 * iwl4965_init_channel_map - Set up driver's info for all possible channels
5145 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005146static int iwl4965_init_channel_map(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005147{
5148 int eeprom_ch_count = 0;
5149 const u8 *eeprom_ch_index = NULL;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005150 const struct iwl4965_eeprom_channel *eeprom_ch_info = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07005151 int band, ch;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005152 struct iwl4965_channel_info *ch_info;
Zhu Yib481de92007-09-25 17:54:57 -07005153
5154 if (priv->channel_count) {
5155 IWL_DEBUG_INFO("Channel map already initialized.\n");
5156 return 0;
5157 }
5158
5159 if (priv->eeprom.version < 0x2f) {
5160 IWL_WARNING("Unsupported EEPROM version: 0x%04X\n",
5161 priv->eeprom.version);
5162 return -EINVAL;
5163 }
5164
5165 IWL_DEBUG_INFO("Initializing regulatory info from EEPROM\n");
5166
5167 priv->channel_count =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005168 ARRAY_SIZE(iwl4965_eeprom_band_1) +
5169 ARRAY_SIZE(iwl4965_eeprom_band_2) +
5170 ARRAY_SIZE(iwl4965_eeprom_band_3) +
5171 ARRAY_SIZE(iwl4965_eeprom_band_4) +
5172 ARRAY_SIZE(iwl4965_eeprom_band_5);
Zhu Yib481de92007-09-25 17:54:57 -07005173
5174 IWL_DEBUG_INFO("Parsing data for %d channels.\n", priv->channel_count);
5175
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005176 priv->channel_info = kzalloc(sizeof(struct iwl4965_channel_info) *
Zhu Yib481de92007-09-25 17:54:57 -07005177 priv->channel_count, GFP_KERNEL);
5178 if (!priv->channel_info) {
5179 IWL_ERROR("Could not allocate channel_info\n");
5180 priv->channel_count = 0;
5181 return -ENOMEM;
5182 }
5183
5184 ch_info = priv->channel_info;
5185
5186 /* Loop through the 5 EEPROM bands adding them in order to the
5187 * channel map we maintain (that contains additional information than
5188 * what just in the EEPROM) */
5189 for (band = 1; band <= 5; band++) {
5190
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005191 iwl4965_init_band_reference(priv, band, &eeprom_ch_count,
Zhu Yib481de92007-09-25 17:54:57 -07005192 &eeprom_ch_info, &eeprom_ch_index);
5193
5194 /* Loop through each band adding each of the channels */
5195 for (ch = 0; ch < eeprom_ch_count; ch++) {
5196 ch_info->channel = eeprom_ch_index[ch];
Johannes Berg8318d782008-01-24 19:38:38 +01005197 ch_info->band = (band == 1) ? IEEE80211_BAND_2GHZ :
5198 IEEE80211_BAND_5GHZ;
Zhu Yib481de92007-09-25 17:54:57 -07005199
5200 /* permanently store EEPROM's channel regulatory flags
5201 * and max power in channel info database. */
5202 ch_info->eeprom = eeprom_ch_info[ch];
5203
5204 /* Copy the run-time flags so they are there even on
5205 * invalid channels */
5206 ch_info->flags = eeprom_ch_info[ch].flags;
5207
5208 if (!(is_channel_valid(ch_info))) {
5209 IWL_DEBUG_INFO("Ch. %d Flags %x [%sGHz] - "
5210 "No traffic\n",
5211 ch_info->channel,
5212 ch_info->flags,
5213 is_channel_a_band(ch_info) ?
5214 "5.2" : "2.4");
5215 ch_info++;
5216 continue;
5217 }
5218
5219 /* Initialize regulatory-based run-time data */
5220 ch_info->max_power_avg = ch_info->curr_txpow =
5221 eeprom_ch_info[ch].max_power_avg;
5222 ch_info->scan_power = eeprom_ch_info[ch].max_power_avg;
5223 ch_info->min_power = 0;
5224
Tomas Winkler8211ef72008-03-02 01:36:04 +02005225 IWL_DEBUG_INFO("Ch. %d [%sGHz] %s%s%s%s%s%s%s(0x%02x"
Zhu Yib481de92007-09-25 17:54:57 -07005226 " %ddBm): Ad-Hoc %ssupported\n",
5227 ch_info->channel,
5228 is_channel_a_band(ch_info) ?
5229 "5.2" : "2.4",
Tomas Winkler8211ef72008-03-02 01:36:04 +02005230 CHECK_AND_PRINT(VALID),
Zhu Yib481de92007-09-25 17:54:57 -07005231 CHECK_AND_PRINT(IBSS),
5232 CHECK_AND_PRINT(ACTIVE),
5233 CHECK_AND_PRINT(RADAR),
5234 CHECK_AND_PRINT(WIDE),
5235 CHECK_AND_PRINT(NARROW),
5236 CHECK_AND_PRINT(DFS),
5237 eeprom_ch_info[ch].flags,
5238 eeprom_ch_info[ch].max_power_avg,
5239 ((eeprom_ch_info[ch].
5240 flags & EEPROM_CHANNEL_IBSS)
5241 && !(eeprom_ch_info[ch].
5242 flags & EEPROM_CHANNEL_RADAR))
5243 ? "" : "not ");
5244
5245 /* Set the user_txpower_limit to the highest power
5246 * supported by any channel */
5247 if (eeprom_ch_info[ch].max_power_avg >
5248 priv->user_txpower_limit)
5249 priv->user_txpower_limit =
5250 eeprom_ch_info[ch].max_power_avg;
5251
5252 ch_info++;
5253 }
5254 }
5255
Cahill, Ben M6440adb2007-11-29 11:09:55 +08005256 /* Two additional EEPROM bands for 2.4 and 5 GHz FAT channels */
Zhu Yib481de92007-09-25 17:54:57 -07005257 for (band = 6; band <= 7; band++) {
Johannes Berg8318d782008-01-24 19:38:38 +01005258 enum ieee80211_band ieeeband;
Zhu Yib481de92007-09-25 17:54:57 -07005259 u8 fat_extension_chan;
5260
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005261 iwl4965_init_band_reference(priv, band, &eeprom_ch_count,
Zhu Yib481de92007-09-25 17:54:57 -07005262 &eeprom_ch_info, &eeprom_ch_index);
5263
Cahill, Ben M6440adb2007-11-29 11:09:55 +08005264 /* EEPROM band 6 is 2.4, band 7 is 5 GHz */
Johannes Berg8318d782008-01-24 19:38:38 +01005265 ieeeband = (band == 6) ? IEEE80211_BAND_2GHZ : IEEE80211_BAND_5GHZ;
Cahill, Ben M6440adb2007-11-29 11:09:55 +08005266
Zhu Yib481de92007-09-25 17:54:57 -07005267 /* Loop through each band adding each of the channels */
5268 for (ch = 0; ch < eeprom_ch_count; ch++) {
5269
5270 if ((band == 6) &&
5271 ((eeprom_ch_index[ch] == 5) ||
5272 (eeprom_ch_index[ch] == 6) ||
5273 (eeprom_ch_index[ch] == 7)))
5274 fat_extension_chan = HT_IE_EXT_CHANNEL_MAX;
5275 else
5276 fat_extension_chan = HT_IE_EXT_CHANNEL_ABOVE;
5277
Cahill, Ben M6440adb2007-11-29 11:09:55 +08005278 /* Set up driver's info for lower half */
Johannes Berg8318d782008-01-24 19:38:38 +01005279 iwl4965_set_fat_chan_info(priv, ieeeband,
Zhu Yib481de92007-09-25 17:54:57 -07005280 eeprom_ch_index[ch],
5281 &(eeprom_ch_info[ch]),
5282 fat_extension_chan);
5283
Cahill, Ben M6440adb2007-11-29 11:09:55 +08005284 /* Set up driver's info for upper half */
Johannes Berg8318d782008-01-24 19:38:38 +01005285 iwl4965_set_fat_chan_info(priv, ieeeband,
Zhu Yib481de92007-09-25 17:54:57 -07005286 (eeprom_ch_index[ch] + 4),
5287 &(eeprom_ch_info[ch]),
5288 HT_IE_EXT_CHANNEL_BELOW);
5289 }
5290 }
5291
5292 return 0;
5293}
5294
Reinette Chatre849e0dc2008-01-23 10:15:18 -08005295/*
5296 * iwl4965_free_channel_map - undo allocations in iwl4965_init_channel_map
5297 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005298static void iwl4965_free_channel_map(struct iwl_priv *priv)
Reinette Chatre849e0dc2008-01-23 10:15:18 -08005299{
5300 kfree(priv->channel_info);
5301 priv->channel_count = 0;
5302}
5303
Zhu Yib481de92007-09-25 17:54:57 -07005304/* For active scan, listen ACTIVE_DWELL_TIME (msec) on each channel after
5305 * sending probe req. This should be set long enough to hear probe responses
5306 * from more than one AP. */
5307#define IWL_ACTIVE_DWELL_TIME_24 (20) /* all times in msec */
5308#define IWL_ACTIVE_DWELL_TIME_52 (10)
5309
5310/* For faster active scanning, scan will move to the next channel if fewer than
5311 * PLCP_QUIET_THRESH packets are heard on this channel within
5312 * ACTIVE_QUIET_TIME after sending probe request. This shortens the dwell
5313 * time if it's a quiet channel (nothing responded to our probe, and there's
5314 * no other traffic).
5315 * Disable "quiet" feature by setting PLCP_QUIET_THRESH to 0. */
5316#define IWL_PLCP_QUIET_THRESH __constant_cpu_to_le16(1) /* packets */
5317#define IWL_ACTIVE_QUIET_TIME __constant_cpu_to_le16(5) /* msec */
5318
5319/* For passive scan, listen PASSIVE_DWELL_TIME (msec) on each channel.
5320 * Must be set longer than active dwell time.
5321 * For the most reliable scan, set > AP beacon interval (typically 100msec). */
5322#define IWL_PASSIVE_DWELL_TIME_24 (20) /* all times in msec */
5323#define IWL_PASSIVE_DWELL_TIME_52 (10)
5324#define IWL_PASSIVE_DWELL_BASE (100)
5325#define IWL_CHANNEL_TUNE_TIME 5
5326
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005327static inline u16 iwl4965_get_active_dwell_time(struct iwl_priv *priv,
Johannes Berg8318d782008-01-24 19:38:38 +01005328 enum ieee80211_band band)
Zhu Yib481de92007-09-25 17:54:57 -07005329{
Johannes Berg8318d782008-01-24 19:38:38 +01005330 if (band == IEEE80211_BAND_5GHZ)
Zhu Yib481de92007-09-25 17:54:57 -07005331 return IWL_ACTIVE_DWELL_TIME_52;
5332 else
5333 return IWL_ACTIVE_DWELL_TIME_24;
5334}
5335
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005336static u16 iwl4965_get_passive_dwell_time(struct iwl_priv *priv,
Johannes Berg8318d782008-01-24 19:38:38 +01005337 enum ieee80211_band band)
Zhu Yib481de92007-09-25 17:54:57 -07005338{
Johannes Berg8318d782008-01-24 19:38:38 +01005339 u16 active = iwl4965_get_active_dwell_time(priv, band);
5340 u16 passive = (band != IEEE80211_BAND_5GHZ) ?
Zhu Yib481de92007-09-25 17:54:57 -07005341 IWL_PASSIVE_DWELL_BASE + IWL_PASSIVE_DWELL_TIME_24 :
5342 IWL_PASSIVE_DWELL_BASE + IWL_PASSIVE_DWELL_TIME_52;
5343
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005344 if (iwl4965_is_associated(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07005345 /* If we're associated, we clamp the maximum passive
5346 * dwell time to be 98% of the beacon interval (minus
5347 * 2 * channel tune time) */
5348 passive = priv->beacon_int;
5349 if ((passive > IWL_PASSIVE_DWELL_BASE) || !passive)
5350 passive = IWL_PASSIVE_DWELL_BASE;
5351 passive = (passive * 98) / 100 - IWL_CHANNEL_TUNE_TIME * 2;
5352 }
5353
5354 if (passive <= active)
5355 passive = active + 1;
5356
5357 return passive;
5358}
5359
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005360static int iwl4965_get_channels_for_scan(struct iwl_priv *priv,
Johannes Berg8318d782008-01-24 19:38:38 +01005361 enum ieee80211_band band,
Zhu Yib481de92007-09-25 17:54:57 -07005362 u8 is_active, u8 direct_mask,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005363 struct iwl4965_scan_channel *scan_ch)
Zhu Yib481de92007-09-25 17:54:57 -07005364{
5365 const struct ieee80211_channel *channels = NULL;
Johannes Berg8318d782008-01-24 19:38:38 +01005366 const struct ieee80211_supported_band *sband;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005367 const struct iwl4965_channel_info *ch_info;
Zhu Yib481de92007-09-25 17:54:57 -07005368 u16 passive_dwell = 0;
5369 u16 active_dwell = 0;
5370 int added, i;
5371
Johannes Berg8318d782008-01-24 19:38:38 +01005372 sband = iwl4965_get_hw_mode(priv, band);
5373 if (!sband)
Zhu Yib481de92007-09-25 17:54:57 -07005374 return 0;
5375
Johannes Berg8318d782008-01-24 19:38:38 +01005376 channels = sband->channels;
Zhu Yib481de92007-09-25 17:54:57 -07005377
Johannes Berg8318d782008-01-24 19:38:38 +01005378 active_dwell = iwl4965_get_active_dwell_time(priv, band);
5379 passive_dwell = iwl4965_get_passive_dwell_time(priv, band);
Zhu Yib481de92007-09-25 17:54:57 -07005380
Johannes Berg8318d782008-01-24 19:38:38 +01005381 for (i = 0, added = 0; i < sband->n_channels; i++) {
5382 if (ieee80211_frequency_to_channel(channels[i].center_freq) ==
Zhu Yib481de92007-09-25 17:54:57 -07005383 le16_to_cpu(priv->active_rxon.channel)) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005384 if (iwl4965_is_associated(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07005385 IWL_DEBUG_SCAN
5386 ("Skipping current channel %d\n",
5387 le16_to_cpu(priv->active_rxon.channel));
5388 continue;
5389 }
5390 } else if (priv->only_active_channel)
5391 continue;
5392
Johannes Berg8318d782008-01-24 19:38:38 +01005393 scan_ch->channel = ieee80211_frequency_to_channel(channels[i].center_freq);
Zhu Yib481de92007-09-25 17:54:57 -07005394
Johannes Berg8318d782008-01-24 19:38:38 +01005395 ch_info = iwl4965_get_channel_info(priv, band,
Ben Cahill9fbab512007-11-29 11:09:47 +08005396 scan_ch->channel);
Zhu Yib481de92007-09-25 17:54:57 -07005397 if (!is_channel_valid(ch_info)) {
5398 IWL_DEBUG_SCAN("Channel %d is INVALID for this SKU.\n",
5399 scan_ch->channel);
5400 continue;
5401 }
5402
5403 if (!is_active || is_channel_passive(ch_info) ||
Johannes Berg8318d782008-01-24 19:38:38 +01005404 (channels[i].flags & IEEE80211_CHAN_PASSIVE_SCAN))
Zhu Yib481de92007-09-25 17:54:57 -07005405 scan_ch->type = 0; /* passive */
5406 else
5407 scan_ch->type = 1; /* active */
5408
5409 if (scan_ch->type & 1)
5410 scan_ch->type |= (direct_mask << 1);
5411
5412 if (is_channel_narrow(ch_info))
5413 scan_ch->type |= (1 << 7);
5414
5415 scan_ch->active_dwell = cpu_to_le16(active_dwell);
5416 scan_ch->passive_dwell = cpu_to_le16(passive_dwell);
5417
Ben Cahill9fbab512007-11-29 11:09:47 +08005418 /* Set txpower levels to defaults */
Zhu Yib481de92007-09-25 17:54:57 -07005419 scan_ch->tpc.dsp_atten = 110;
5420 /* scan_pwr_info->tpc.dsp_atten; */
5421
5422 /*scan_pwr_info->tpc.tx_gain; */
Johannes Berg8318d782008-01-24 19:38:38 +01005423 if (band == IEEE80211_BAND_5GHZ)
Zhu Yib481de92007-09-25 17:54:57 -07005424 scan_ch->tpc.tx_gain = ((1 << 5) | (3 << 3)) | 3;
5425 else {
5426 scan_ch->tpc.tx_gain = ((1 << 5) | (5 << 3));
5427 /* NOTE: if we were doing 6Mb OFDM for scans we'd use
Ben Cahill9fbab512007-11-29 11:09:47 +08005428 * power level:
Reinette Chatre8a1b0242008-01-14 17:46:25 -08005429 * scan_ch->tpc.tx_gain = ((1 << 5) | (2 << 3)) | 3;
Zhu Yib481de92007-09-25 17:54:57 -07005430 */
5431 }
5432
5433 IWL_DEBUG_SCAN("Scanning %d [%s %d]\n",
5434 scan_ch->channel,
5435 (scan_ch->type & 1) ? "ACTIVE" : "PASSIVE",
5436 (scan_ch->type & 1) ?
5437 active_dwell : passive_dwell);
5438
5439 scan_ch++;
5440 added++;
5441 }
5442
5443 IWL_DEBUG_SCAN("total channels to scan %d \n", added);
5444 return added;
5445}
5446
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005447static void iwl4965_init_hw_rates(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07005448 struct ieee80211_rate *rates)
5449{
5450 int i;
5451
5452 for (i = 0; i < IWL_RATE_COUNT; i++) {
Johannes Berg8318d782008-01-24 19:38:38 +01005453 rates[i].bitrate = iwl4965_rates[i].ieee * 5;
5454 rates[i].hw_value = i; /* Rate scaling will work on indexes */
5455 rates[i].hw_value_short = i;
5456 rates[i].flags = 0;
5457 if ((i > IWL_LAST_OFDM_RATE) || (i < IWL_FIRST_OFDM_RATE)) {
Zhu Yib481de92007-09-25 17:54:57 -07005458 /*
Johannes Berg8318d782008-01-24 19:38:38 +01005459 * If CCK != 1M then set short preamble rate flag.
Zhu Yib481de92007-09-25 17:54:57 -07005460 */
Tomas Winkler35cdeaf2008-03-11 16:17:20 -07005461 rates[i].flags |=
5462 (iwl4965_rates[i].plcp == IWL_RATE_1M_PLCP) ?
5463 0 : IEEE80211_RATE_SHORT_PREAMBLE;
Zhu Yib481de92007-09-25 17:54:57 -07005464 }
Zhu Yib481de92007-09-25 17:54:57 -07005465 }
Zhu Yib481de92007-09-25 17:54:57 -07005466}
5467
5468/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005469 * iwl4965_init_geos - Initialize mac80211's geo/channel info based from eeprom
Zhu Yib481de92007-09-25 17:54:57 -07005470 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005471static int iwl4965_init_geos(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005472{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005473 struct iwl4965_channel_info *ch;
Tomas Winkler8211ef72008-03-02 01:36:04 +02005474 struct ieee80211_supported_band *sband;
Zhu Yib481de92007-09-25 17:54:57 -07005475 struct ieee80211_channel *channels;
5476 struct ieee80211_channel *geo_ch;
5477 struct ieee80211_rate *rates;
5478 int i = 0;
Zhu Yib481de92007-09-25 17:54:57 -07005479
Johannes Berg8318d782008-01-24 19:38:38 +01005480 if (priv->bands[IEEE80211_BAND_2GHZ].n_bitrates ||
5481 priv->bands[IEEE80211_BAND_5GHZ].n_bitrates) {
Zhu Yib481de92007-09-25 17:54:57 -07005482 IWL_DEBUG_INFO("Geography modes already initialized.\n");
5483 set_bit(STATUS_GEO_CONFIGURED, &priv->status);
5484 return 0;
5485 }
5486
Zhu Yib481de92007-09-25 17:54:57 -07005487 channels = kzalloc(sizeof(struct ieee80211_channel) *
5488 priv->channel_count, GFP_KERNEL);
Johannes Berg8318d782008-01-24 19:38:38 +01005489 if (!channels)
Zhu Yib481de92007-09-25 17:54:57 -07005490 return -ENOMEM;
Zhu Yib481de92007-09-25 17:54:57 -07005491
Tomas Winkler8211ef72008-03-02 01:36:04 +02005492 rates = kzalloc((sizeof(struct ieee80211_rate) * (IWL_RATE_COUNT + 1)),
Zhu Yib481de92007-09-25 17:54:57 -07005493 GFP_KERNEL);
5494 if (!rates) {
Zhu Yib481de92007-09-25 17:54:57 -07005495 kfree(channels);
5496 return -ENOMEM;
5497 }
5498
Zhu Yib481de92007-09-25 17:54:57 -07005499 /* 5.2GHz channels start after the 2.4GHz channels */
Tomas Winkler8211ef72008-03-02 01:36:04 +02005500 sband = &priv->bands[IEEE80211_BAND_5GHZ];
5501 sband->channels = &channels[ARRAY_SIZE(iwl4965_eeprom_band_1)];
5502 /* just OFDM */
5503 sband->bitrates = &rates[IWL_FIRST_OFDM_RATE];
5504 sband->n_bitrates = IWL_RATE_COUNT - IWL_FIRST_OFDM_RATE;
Johannes Berg8318d782008-01-24 19:38:38 +01005505
Tomas Winkler8211ef72008-03-02 01:36:04 +02005506 iwl4965_init_ht_hw_capab(&sband->ht_info, IEEE80211_BAND_5GHZ);
Tomas Winkler78330fd2008-02-06 02:37:18 +02005507
Tomas Winkler8211ef72008-03-02 01:36:04 +02005508 sband = &priv->bands[IEEE80211_BAND_2GHZ];
5509 sband->channels = channels;
5510 /* OFDM & CCK */
5511 sband->bitrates = rates;
5512 sband->n_bitrates = IWL_RATE_COUNT;
Zhu Yib481de92007-09-25 17:54:57 -07005513
Tomas Winkler8211ef72008-03-02 01:36:04 +02005514 iwl4965_init_ht_hw_capab(&sband->ht_info, IEEE80211_BAND_2GHZ);
Tomas Winkler78330fd2008-02-06 02:37:18 +02005515
Zhu Yib481de92007-09-25 17:54:57 -07005516 priv->ieee_channels = channels;
5517 priv->ieee_rates = rates;
5518
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005519 iwl4965_init_hw_rates(priv, rates);
Zhu Yib481de92007-09-25 17:54:57 -07005520
Tomas Winkler8211ef72008-03-02 01:36:04 +02005521 for (i = 0; i < priv->channel_count; i++) {
Zhu Yib481de92007-09-25 17:54:57 -07005522 ch = &priv->channel_info[i];
5523
Tomas Winkler8211ef72008-03-02 01:36:04 +02005524 /* FIXME: might be removed if scan is OK */
5525 if (!is_channel_valid(ch))
Zhu Yib481de92007-09-25 17:54:57 -07005526 continue;
Zhu Yib481de92007-09-25 17:54:57 -07005527
Tomas Winkler8211ef72008-03-02 01:36:04 +02005528 if (is_channel_a_band(ch))
5529 sband = &priv->bands[IEEE80211_BAND_5GHZ];
5530 else
5531 sband = &priv->bands[IEEE80211_BAND_2GHZ];
Zhu Yib481de92007-09-25 17:54:57 -07005532
Tomas Winkler8211ef72008-03-02 01:36:04 +02005533 geo_ch = &sband->channels[sband->n_channels++];
5534
5535 geo_ch->center_freq = ieee80211_channel_to_frequency(ch->channel);
Johannes Berg8318d782008-01-24 19:38:38 +01005536 geo_ch->max_power = ch->max_power_avg;
5537 geo_ch->max_antenna_gain = 0xff;
Mohamed Abbas7b723042008-01-31 21:46:40 -08005538 geo_ch->hw_value = ch->channel;
Zhu Yib481de92007-09-25 17:54:57 -07005539
5540 if (is_channel_valid(ch)) {
Johannes Berg8318d782008-01-24 19:38:38 +01005541 if (!(ch->flags & EEPROM_CHANNEL_IBSS))
5542 geo_ch->flags |= IEEE80211_CHAN_NO_IBSS;
Zhu Yib481de92007-09-25 17:54:57 -07005543
Johannes Berg8318d782008-01-24 19:38:38 +01005544 if (!(ch->flags & EEPROM_CHANNEL_ACTIVE))
5545 geo_ch->flags |= IEEE80211_CHAN_PASSIVE_SCAN;
Zhu Yib481de92007-09-25 17:54:57 -07005546
5547 if (ch->flags & EEPROM_CHANNEL_RADAR)
Johannes Berg8318d782008-01-24 19:38:38 +01005548 geo_ch->flags |= IEEE80211_CHAN_RADAR;
Zhu Yib481de92007-09-25 17:54:57 -07005549
5550 if (ch->max_power_avg > priv->max_channel_txpower_limit)
5551 priv->max_channel_txpower_limit =
5552 ch->max_power_avg;
Tomas Winkler8211ef72008-03-02 01:36:04 +02005553 } else {
Johannes Berg8318d782008-01-24 19:38:38 +01005554 geo_ch->flags |= IEEE80211_CHAN_DISABLED;
Tomas Winkler8211ef72008-03-02 01:36:04 +02005555 }
5556
5557 /* Save flags for reg domain usage */
5558 geo_ch->orig_flags = geo_ch->flags;
5559
5560 IWL_DEBUG_INFO("Channel %d Freq=%d[%sGHz] %s flag=0%X\n",
5561 ch->channel, geo_ch->center_freq,
5562 is_channel_a_band(ch) ? "5.2" : "2.4",
5563 geo_ch->flags & IEEE80211_CHAN_DISABLED ?
5564 "restricted" : "valid",
5565 geo_ch->flags);
Zhu Yib481de92007-09-25 17:54:57 -07005566 }
5567
Tomas Winkler82b9a122008-03-04 18:09:30 -08005568 if ((priv->bands[IEEE80211_BAND_5GHZ].n_channels == 0) &&
5569 priv->cfg->sku & IWL_SKU_A) {
Zhu Yib481de92007-09-25 17:54:57 -07005570 printk(KERN_INFO DRV_NAME
5571 ": Incorrectly detected BG card as ABG. Please send "
5572 "your PCI ID 0x%04X:0x%04X to maintainer.\n",
5573 priv->pci_dev->device, priv->pci_dev->subsystem_device);
Tomas Winkler82b9a122008-03-04 18:09:30 -08005574 priv->cfg->sku &= ~IWL_SKU_A;
Zhu Yib481de92007-09-25 17:54:57 -07005575 }
5576
5577 printk(KERN_INFO DRV_NAME
5578 ": Tunable channels: %d 802.11bg, %d 802.11a channels\n",
Johannes Berg8318d782008-01-24 19:38:38 +01005579 priv->bands[IEEE80211_BAND_2GHZ].n_channels,
5580 priv->bands[IEEE80211_BAND_5GHZ].n_channels);
Zhu Yib481de92007-09-25 17:54:57 -07005581
Johannes Berg8318d782008-01-24 19:38:38 +01005582 priv->hw->wiphy->bands[IEEE80211_BAND_2GHZ] = &priv->bands[IEEE80211_BAND_2GHZ];
5583 priv->hw->wiphy->bands[IEEE80211_BAND_5GHZ] = &priv->bands[IEEE80211_BAND_5GHZ];
Zhu Yib481de92007-09-25 17:54:57 -07005584
Zhu Yib481de92007-09-25 17:54:57 -07005585 set_bit(STATUS_GEO_CONFIGURED, &priv->status);
5586
5587 return 0;
5588}
5589
Reinette Chatre849e0dc2008-01-23 10:15:18 -08005590/*
5591 * iwl4965_free_geos - undo allocations in iwl4965_init_geos
5592 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005593static void iwl4965_free_geos(struct iwl_priv *priv)
Reinette Chatre849e0dc2008-01-23 10:15:18 -08005594{
Reinette Chatre849e0dc2008-01-23 10:15:18 -08005595 kfree(priv->ieee_channels);
5596 kfree(priv->ieee_rates);
5597 clear_bit(STATUS_GEO_CONFIGURED, &priv->status);
5598}
5599
Zhu Yib481de92007-09-25 17:54:57 -07005600/******************************************************************************
5601 *
5602 * uCode download functions
5603 *
5604 ******************************************************************************/
5605
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005606static void iwl4965_dealloc_ucode_pci(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005607{
Tomas Winkler98c92212008-01-14 17:46:20 -08005608 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_code);
5609 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_data);
5610 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_data_backup);
5611 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_init);
5612 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_init_data);
5613 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_boot);
Zhu Yib481de92007-09-25 17:54:57 -07005614}
5615
5616/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005617 * iwl4965_verify_inst_full - verify runtime uCode image in card vs. host,
Zhu Yib481de92007-09-25 17:54:57 -07005618 * looking at all data.
5619 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005620static int iwl4965_verify_inst_full(struct iwl_priv *priv, __le32 *image,
Ben Cahill9fbab512007-11-29 11:09:47 +08005621 u32 len)
Zhu Yib481de92007-09-25 17:54:57 -07005622{
5623 u32 val;
5624 u32 save_len = len;
5625 int rc = 0;
5626 u32 errcnt;
5627
5628 IWL_DEBUG_INFO("ucode inst image size is %u\n", len);
5629
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005630 rc = iwl4965_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005631 if (rc)
5632 return rc;
5633
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005634 iwl4965_write_direct32(priv, HBUS_TARG_MEM_RADDR, RTC_INST_LOWER_BOUND);
Zhu Yib481de92007-09-25 17:54:57 -07005635
5636 errcnt = 0;
5637 for (; len > 0; len -= sizeof(u32), image++) {
5638 /* read data comes through single port, auto-incr addr */
5639 /* NOTE: Use the debugless read so we don't flood kernel log
5640 * if IWL_DL_IO is set */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005641 val = _iwl4965_read_direct32(priv, HBUS_TARG_MEM_RDAT);
Zhu Yib481de92007-09-25 17:54:57 -07005642 if (val != le32_to_cpu(*image)) {
5643 IWL_ERROR("uCode INST section is invalid at "
5644 "offset 0x%x, is 0x%x, s/b 0x%x\n",
5645 save_len - len, val, le32_to_cpu(*image));
5646 rc = -EIO;
5647 errcnt++;
5648 if (errcnt >= 20)
5649 break;
5650 }
5651 }
5652
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005653 iwl4965_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005654
5655 if (!errcnt)
5656 IWL_DEBUG_INFO
5657 ("ucode image in INSTRUCTION memory is good\n");
5658
5659 return rc;
5660}
5661
5662
5663/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005664 * iwl4965_verify_inst_sparse - verify runtime uCode image in card vs. host,
Zhu Yib481de92007-09-25 17:54:57 -07005665 * using sample data 100 bytes apart. If these sample points are good,
5666 * it's a pretty good bet that everything between them is good, too.
5667 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005668static int iwl4965_verify_inst_sparse(struct iwl_priv *priv, __le32 *image, u32 len)
Zhu Yib481de92007-09-25 17:54:57 -07005669{
5670 u32 val;
5671 int rc = 0;
5672 u32 errcnt = 0;
5673 u32 i;
5674
5675 IWL_DEBUG_INFO("ucode inst image size is %u\n", len);
5676
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005677 rc = iwl4965_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005678 if (rc)
5679 return rc;
5680
5681 for (i = 0; i < len; i += 100, image += 100/sizeof(u32)) {
5682 /* read data comes through single port, auto-incr addr */
5683 /* NOTE: Use the debugless read so we don't flood kernel log
5684 * if IWL_DL_IO is set */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005685 iwl4965_write_direct32(priv, HBUS_TARG_MEM_RADDR,
Zhu Yib481de92007-09-25 17:54:57 -07005686 i + RTC_INST_LOWER_BOUND);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005687 val = _iwl4965_read_direct32(priv, HBUS_TARG_MEM_RDAT);
Zhu Yib481de92007-09-25 17:54:57 -07005688 if (val != le32_to_cpu(*image)) {
5689#if 0 /* Enable this if you want to see details */
5690 IWL_ERROR("uCode INST section is invalid at "
5691 "offset 0x%x, is 0x%x, s/b 0x%x\n",
5692 i, val, *image);
5693#endif
5694 rc = -EIO;
5695 errcnt++;
5696 if (errcnt >= 3)
5697 break;
5698 }
5699 }
5700
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005701 iwl4965_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005702
5703 return rc;
5704}
5705
5706
5707/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005708 * iwl4965_verify_ucode - determine which instruction image is in SRAM,
Zhu Yib481de92007-09-25 17:54:57 -07005709 * and verify its contents
5710 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005711static int iwl4965_verify_ucode(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005712{
5713 __le32 *image;
5714 u32 len;
5715 int rc = 0;
5716
5717 /* Try bootstrap */
5718 image = (__le32 *)priv->ucode_boot.v_addr;
5719 len = priv->ucode_boot.len;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005720 rc = iwl4965_verify_inst_sparse(priv, image, len);
Zhu Yib481de92007-09-25 17:54:57 -07005721 if (rc == 0) {
5722 IWL_DEBUG_INFO("Bootstrap uCode is good in inst SRAM\n");
5723 return 0;
5724 }
5725
5726 /* Try initialize */
5727 image = (__le32 *)priv->ucode_init.v_addr;
5728 len = priv->ucode_init.len;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005729 rc = iwl4965_verify_inst_sparse(priv, image, len);
Zhu Yib481de92007-09-25 17:54:57 -07005730 if (rc == 0) {
5731 IWL_DEBUG_INFO("Initialize uCode is good in inst SRAM\n");
5732 return 0;
5733 }
5734
5735 /* Try runtime/protocol */
5736 image = (__le32 *)priv->ucode_code.v_addr;
5737 len = priv->ucode_code.len;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005738 rc = iwl4965_verify_inst_sparse(priv, image, len);
Zhu Yib481de92007-09-25 17:54:57 -07005739 if (rc == 0) {
5740 IWL_DEBUG_INFO("Runtime uCode is good in inst SRAM\n");
5741 return 0;
5742 }
5743
5744 IWL_ERROR("NO VALID UCODE IMAGE IN INSTRUCTION SRAM!!\n");
5745
Ben Cahill9fbab512007-11-29 11:09:47 +08005746 /* Since nothing seems to match, show first several data entries in
5747 * instruction SRAM, so maybe visual inspection will give a clue.
5748 * Selection of bootstrap image (vs. other images) is arbitrary. */
Zhu Yib481de92007-09-25 17:54:57 -07005749 image = (__le32 *)priv->ucode_boot.v_addr;
5750 len = priv->ucode_boot.len;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005751 rc = iwl4965_verify_inst_full(priv, image, len);
Zhu Yib481de92007-09-25 17:54:57 -07005752
5753 return rc;
5754}
5755
5756
5757/* check contents of special bootstrap uCode SRAM */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005758static int iwl4965_verify_bsm(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005759{
5760 __le32 *image = priv->ucode_boot.v_addr;
5761 u32 len = priv->ucode_boot.len;
5762 u32 reg;
5763 u32 val;
5764
5765 IWL_DEBUG_INFO("Begin verify bsm\n");
5766
5767 /* verify BSM SRAM contents */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005768 val = iwl4965_read_prph(priv, BSM_WR_DWCOUNT_REG);
Zhu Yib481de92007-09-25 17:54:57 -07005769 for (reg = BSM_SRAM_LOWER_BOUND;
5770 reg < BSM_SRAM_LOWER_BOUND + len;
5771 reg += sizeof(u32), image ++) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005772 val = iwl4965_read_prph(priv, reg);
Zhu Yib481de92007-09-25 17:54:57 -07005773 if (val != le32_to_cpu(*image)) {
5774 IWL_ERROR("BSM uCode verification failed at "
5775 "addr 0x%08X+%u (of %u), is 0x%x, s/b 0x%x\n",
5776 BSM_SRAM_LOWER_BOUND,
5777 reg - BSM_SRAM_LOWER_BOUND, len,
5778 val, le32_to_cpu(*image));
5779 return -EIO;
5780 }
5781 }
5782
5783 IWL_DEBUG_INFO("BSM bootstrap uCode image OK\n");
5784
5785 return 0;
5786}
5787
5788/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005789 * iwl4965_load_bsm - Load bootstrap instructions
Zhu Yib481de92007-09-25 17:54:57 -07005790 *
5791 * BSM operation:
5792 *
5793 * The Bootstrap State Machine (BSM) stores a short bootstrap uCode program
5794 * in special SRAM that does not power down during RFKILL. When powering back
5795 * up after power-saving sleeps (or during initial uCode load), the BSM loads
5796 * the bootstrap program into the on-board processor, and starts it.
5797 *
5798 * The bootstrap program loads (via DMA) instructions and data for a new
5799 * program from host DRAM locations indicated by the host driver in the
5800 * BSM_DRAM_* registers. Once the new program is loaded, it starts
5801 * automatically.
5802 *
5803 * When initializing the NIC, the host driver points the BSM to the
5804 * "initialize" uCode image. This uCode sets up some internal data, then
5805 * notifies host via "initialize alive" that it is complete.
5806 *
5807 * The host then replaces the BSM_DRAM_* pointer values to point to the
5808 * normal runtime uCode instructions and a backup uCode data cache buffer
5809 * (filled initially with starting data values for the on-board processor),
5810 * then triggers the "initialize" uCode to load and launch the runtime uCode,
5811 * which begins normal operation.
5812 *
5813 * When doing a power-save shutdown, runtime uCode saves data SRAM into
5814 * the backup data cache in DRAM before SRAM is powered down.
5815 *
5816 * When powering back up, the BSM loads the bootstrap program. This reloads
5817 * the runtime uCode instructions and the backup data cache into SRAM,
5818 * and re-launches the runtime uCode from where it left off.
5819 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005820static int iwl4965_load_bsm(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005821{
5822 __le32 *image = priv->ucode_boot.v_addr;
5823 u32 len = priv->ucode_boot.len;
5824 dma_addr_t pinst;
5825 dma_addr_t pdata;
5826 u32 inst_len;
5827 u32 data_len;
5828 int rc;
5829 int i;
5830 u32 done;
5831 u32 reg_offset;
5832
5833 IWL_DEBUG_INFO("Begin load bsm\n");
5834
5835 /* make sure bootstrap program is no larger than BSM's SRAM size */
5836 if (len > IWL_MAX_BSM_SIZE)
5837 return -EINVAL;
5838
5839 /* Tell bootstrap uCode where to find the "Initialize" uCode
Ben Cahill9fbab512007-11-29 11:09:47 +08005840 * in host DRAM ... host DRAM physical address bits 35:4 for 4965.
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005841 * NOTE: iwl4965_initialize_alive_start() will replace these values,
Zhu Yib481de92007-09-25 17:54:57 -07005842 * after the "initialize" uCode has run, to point to
5843 * runtime/protocol instructions and backup data cache. */
5844 pinst = priv->ucode_init.p_addr >> 4;
5845 pdata = priv->ucode_init_data.p_addr >> 4;
5846 inst_len = priv->ucode_init.len;
5847 data_len = priv->ucode_init_data.len;
5848
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005849 rc = iwl4965_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005850 if (rc)
5851 return rc;
5852
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005853 iwl4965_write_prph(priv, BSM_DRAM_INST_PTR_REG, pinst);
5854 iwl4965_write_prph(priv, BSM_DRAM_DATA_PTR_REG, pdata);
5855 iwl4965_write_prph(priv, BSM_DRAM_INST_BYTECOUNT_REG, inst_len);
5856 iwl4965_write_prph(priv, BSM_DRAM_DATA_BYTECOUNT_REG, data_len);
Zhu Yib481de92007-09-25 17:54:57 -07005857
5858 /* Fill BSM memory with bootstrap instructions */
5859 for (reg_offset = BSM_SRAM_LOWER_BOUND;
5860 reg_offset < BSM_SRAM_LOWER_BOUND + len;
5861 reg_offset += sizeof(u32), image++)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005862 _iwl4965_write_prph(priv, reg_offset,
Zhu Yib481de92007-09-25 17:54:57 -07005863 le32_to_cpu(*image));
5864
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005865 rc = iwl4965_verify_bsm(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005866 if (rc) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005867 iwl4965_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005868 return rc;
5869 }
5870
5871 /* Tell BSM to copy from BSM SRAM into instruction SRAM, when asked */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005872 iwl4965_write_prph(priv, BSM_WR_MEM_SRC_REG, 0x0);
5873 iwl4965_write_prph(priv, BSM_WR_MEM_DST_REG,
Zhu Yib481de92007-09-25 17:54:57 -07005874 RTC_INST_LOWER_BOUND);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005875 iwl4965_write_prph(priv, BSM_WR_DWCOUNT_REG, len / sizeof(u32));
Zhu Yib481de92007-09-25 17:54:57 -07005876
5877 /* Load bootstrap code into instruction SRAM now,
5878 * to prepare to load "initialize" uCode */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005879 iwl4965_write_prph(priv, BSM_WR_CTRL_REG,
Zhu Yib481de92007-09-25 17:54:57 -07005880 BSM_WR_CTRL_REG_BIT_START);
5881
5882 /* Wait for load of bootstrap uCode to finish */
5883 for (i = 0; i < 100; i++) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005884 done = iwl4965_read_prph(priv, BSM_WR_CTRL_REG);
Zhu Yib481de92007-09-25 17:54:57 -07005885 if (!(done & BSM_WR_CTRL_REG_BIT_START))
5886 break;
5887 udelay(10);
5888 }
5889 if (i < 100)
5890 IWL_DEBUG_INFO("BSM write complete, poll %d iterations\n", i);
5891 else {
5892 IWL_ERROR("BSM write did not complete!\n");
5893 return -EIO;
5894 }
5895
5896 /* Enable future boot loads whenever power management unit triggers it
5897 * (e.g. when powering back up after power-save shutdown) */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005898 iwl4965_write_prph(priv, BSM_WR_CTRL_REG,
Zhu Yib481de92007-09-25 17:54:57 -07005899 BSM_WR_CTRL_REG_BIT_START_EN);
5900
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005901 iwl4965_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005902
5903 return 0;
5904}
5905
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005906static void iwl4965_nic_start(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005907{
5908 /* Remove all resets to allow NIC to operate */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005909 iwl4965_write32(priv, CSR_RESET, 0);
Zhu Yib481de92007-09-25 17:54:57 -07005910}
5911
Tomas Winkler90e759d2007-11-29 11:09:41 +08005912
Zhu Yib481de92007-09-25 17:54:57 -07005913/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005914 * iwl4965_read_ucode - Read uCode images from disk file.
Zhu Yib481de92007-09-25 17:54:57 -07005915 *
5916 * Copy into buffers for card to fetch via bus-mastering
5917 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005918static int iwl4965_read_ucode(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005919{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005920 struct iwl4965_ucode *ucode;
Tomas Winkler90e759d2007-11-29 11:09:41 +08005921 int ret;
Zhu Yib481de92007-09-25 17:54:57 -07005922 const struct firmware *ucode_raw;
Tomas Winkler4bf775c2008-03-04 18:09:31 -08005923 const char *name = priv->cfg->fw_name;
Zhu Yib481de92007-09-25 17:54:57 -07005924 u8 *src;
5925 size_t len;
5926 u32 ver, inst_size, data_size, init_size, init_data_size, boot_size;
5927
5928 /* Ask kernel firmware_class module to get the boot firmware off disk.
5929 * request_firmware() is synchronous, file is in memory on return. */
Tomas Winkler90e759d2007-11-29 11:09:41 +08005930 ret = request_firmware(&ucode_raw, name, &priv->pci_dev->dev);
5931 if (ret < 0) {
5932 IWL_ERROR("%s firmware file req failed: Reason %d\n",
5933 name, ret);
Zhu Yib481de92007-09-25 17:54:57 -07005934 goto error;
5935 }
5936
5937 IWL_DEBUG_INFO("Got firmware '%s' file (%zd bytes) from disk\n",
5938 name, ucode_raw->size);
5939
5940 /* Make sure that we got at least our header! */
5941 if (ucode_raw->size < sizeof(*ucode)) {
5942 IWL_ERROR("File size way too small!\n");
Tomas Winkler90e759d2007-11-29 11:09:41 +08005943 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07005944 goto err_release;
5945 }
5946
5947 /* Data from ucode file: header followed by uCode images */
5948 ucode = (void *)ucode_raw->data;
5949
5950 ver = le32_to_cpu(ucode->ver);
5951 inst_size = le32_to_cpu(ucode->inst_size);
5952 data_size = le32_to_cpu(ucode->data_size);
5953 init_size = le32_to_cpu(ucode->init_size);
5954 init_data_size = le32_to_cpu(ucode->init_data_size);
5955 boot_size = le32_to_cpu(ucode->boot_size);
5956
5957 IWL_DEBUG_INFO("f/w package hdr ucode version = 0x%x\n", ver);
5958 IWL_DEBUG_INFO("f/w package hdr runtime inst size = %u\n",
5959 inst_size);
5960 IWL_DEBUG_INFO("f/w package hdr runtime data size = %u\n",
5961 data_size);
5962 IWL_DEBUG_INFO("f/w package hdr init inst size = %u\n",
5963 init_size);
5964 IWL_DEBUG_INFO("f/w package hdr init data size = %u\n",
5965 init_data_size);
5966 IWL_DEBUG_INFO("f/w package hdr boot inst size = %u\n",
5967 boot_size);
5968
5969 /* Verify size of file vs. image size info in file's header */
5970 if (ucode_raw->size < sizeof(*ucode) +
5971 inst_size + data_size + init_size +
5972 init_data_size + boot_size) {
5973
5974 IWL_DEBUG_INFO("uCode file size %d too small\n",
5975 (int)ucode_raw->size);
Tomas Winkler90e759d2007-11-29 11:09:41 +08005976 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07005977 goto err_release;
5978 }
5979
5980 /* Verify that uCode images will fit in card's SRAM */
5981 if (inst_size > IWL_MAX_INST_SIZE) {
Tomas Winkler90e759d2007-11-29 11:09:41 +08005982 IWL_DEBUG_INFO("uCode instr len %d too large to fit in\n",
5983 inst_size);
5984 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07005985 goto err_release;
5986 }
5987
5988 if (data_size > IWL_MAX_DATA_SIZE) {
Tomas Winkler90e759d2007-11-29 11:09:41 +08005989 IWL_DEBUG_INFO("uCode data len %d too large to fit in\n",
5990 data_size);
5991 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07005992 goto err_release;
5993 }
5994 if (init_size > IWL_MAX_INST_SIZE) {
5995 IWL_DEBUG_INFO
Tomas Winkler90e759d2007-11-29 11:09:41 +08005996 ("uCode init instr len %d too large to fit in\n",
5997 init_size);
5998 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07005999 goto err_release;
6000 }
6001 if (init_data_size > IWL_MAX_DATA_SIZE) {
6002 IWL_DEBUG_INFO
Tomas Winkler90e759d2007-11-29 11:09:41 +08006003 ("uCode init data len %d too large to fit in\n",
6004 init_data_size);
6005 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07006006 goto err_release;
6007 }
6008 if (boot_size > IWL_MAX_BSM_SIZE) {
6009 IWL_DEBUG_INFO
Tomas Winkler90e759d2007-11-29 11:09:41 +08006010 ("uCode boot instr len %d too large to fit in\n",
6011 boot_size);
6012 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07006013 goto err_release;
6014 }
6015
6016 /* Allocate ucode buffers for card's bus-master loading ... */
6017
6018 /* Runtime instructions and 2 copies of data:
6019 * 1) unmodified from disk
6020 * 2) backup cache for save/restore during power-downs */
6021 priv->ucode_code.len = inst_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08006022 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_code);
Zhu Yib481de92007-09-25 17:54:57 -07006023
6024 priv->ucode_data.len = data_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08006025 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_data);
Zhu Yib481de92007-09-25 17:54:57 -07006026
6027 priv->ucode_data_backup.len = data_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08006028 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_data_backup);
Zhu Yib481de92007-09-25 17:54:57 -07006029
6030 /* Initialization instructions and data */
Tomas Winkler90e759d2007-11-29 11:09:41 +08006031 if (init_size && init_data_size) {
6032 priv->ucode_init.len = init_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08006033 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_init);
Zhu Yib481de92007-09-25 17:54:57 -07006034
Tomas Winkler90e759d2007-11-29 11:09:41 +08006035 priv->ucode_init_data.len = init_data_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08006036 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_init_data);
Tomas Winkler90e759d2007-11-29 11:09:41 +08006037
6038 if (!priv->ucode_init.v_addr || !priv->ucode_init_data.v_addr)
6039 goto err_pci_alloc;
6040 }
Zhu Yib481de92007-09-25 17:54:57 -07006041
6042 /* Bootstrap (instructions only, no data) */
Tomas Winkler90e759d2007-11-29 11:09:41 +08006043 if (boot_size) {
6044 priv->ucode_boot.len = boot_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08006045 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_boot);
Zhu Yib481de92007-09-25 17:54:57 -07006046
Tomas Winkler90e759d2007-11-29 11:09:41 +08006047 if (!priv->ucode_boot.v_addr)
6048 goto err_pci_alloc;
6049 }
Zhu Yib481de92007-09-25 17:54:57 -07006050
6051 /* Copy images into buffers for card's bus-master reads ... */
6052
6053 /* Runtime instructions (first block of data in file) */
6054 src = &ucode->data[0];
6055 len = priv->ucode_code.len;
Tomas Winkler90e759d2007-11-29 11:09:41 +08006056 IWL_DEBUG_INFO("Copying (but not loading) uCode instr len %Zd\n", len);
Zhu Yib481de92007-09-25 17:54:57 -07006057 memcpy(priv->ucode_code.v_addr, src, len);
6058 IWL_DEBUG_INFO("uCode instr buf vaddr = 0x%p, paddr = 0x%08x\n",
6059 priv->ucode_code.v_addr, (u32)priv->ucode_code.p_addr);
6060
6061 /* Runtime data (2nd block)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006062 * NOTE: Copy into backup buffer will be done in iwl4965_up() */
Zhu Yib481de92007-09-25 17:54:57 -07006063 src = &ucode->data[inst_size];
6064 len = priv->ucode_data.len;
Tomas Winkler90e759d2007-11-29 11:09:41 +08006065 IWL_DEBUG_INFO("Copying (but not loading) uCode data len %Zd\n", len);
Zhu Yib481de92007-09-25 17:54:57 -07006066 memcpy(priv->ucode_data.v_addr, src, len);
6067 memcpy(priv->ucode_data_backup.v_addr, src, len);
6068
6069 /* Initialization instructions (3rd block) */
6070 if (init_size) {
6071 src = &ucode->data[inst_size + data_size];
6072 len = priv->ucode_init.len;
Tomas Winkler90e759d2007-11-29 11:09:41 +08006073 IWL_DEBUG_INFO("Copying (but not loading) init instr len %Zd\n",
6074 len);
Zhu Yib481de92007-09-25 17:54:57 -07006075 memcpy(priv->ucode_init.v_addr, src, len);
6076 }
6077
6078 /* Initialization data (4th block) */
6079 if (init_data_size) {
6080 src = &ucode->data[inst_size + data_size + init_size];
6081 len = priv->ucode_init_data.len;
Tomas Winkler90e759d2007-11-29 11:09:41 +08006082 IWL_DEBUG_INFO("Copying (but not loading) init data len %Zd\n",
6083 len);
Zhu Yib481de92007-09-25 17:54:57 -07006084 memcpy(priv->ucode_init_data.v_addr, src, len);
6085 }
6086
6087 /* Bootstrap instructions (5th block) */
6088 src = &ucode->data[inst_size + data_size + init_size + init_data_size];
6089 len = priv->ucode_boot.len;
Tomas Winkler90e759d2007-11-29 11:09:41 +08006090 IWL_DEBUG_INFO("Copying (but not loading) boot instr len %Zd\n", len);
Zhu Yib481de92007-09-25 17:54:57 -07006091 memcpy(priv->ucode_boot.v_addr, src, len);
6092
6093 /* We have our copies now, allow OS release its copies */
6094 release_firmware(ucode_raw);
6095 return 0;
6096
6097 err_pci_alloc:
6098 IWL_ERROR("failed to allocate pci memory\n");
Tomas Winkler90e759d2007-11-29 11:09:41 +08006099 ret = -ENOMEM;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006100 iwl4965_dealloc_ucode_pci(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006101
6102 err_release:
6103 release_firmware(ucode_raw);
6104
6105 error:
Tomas Winkler90e759d2007-11-29 11:09:41 +08006106 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07006107}
6108
6109
6110/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006111 * iwl4965_set_ucode_ptrs - Set uCode address location
Zhu Yib481de92007-09-25 17:54:57 -07006112 *
6113 * Tell initialization uCode where to find runtime uCode.
6114 *
6115 * BSM registers initially contain pointers to initialization uCode.
6116 * We need to replace them to load runtime uCode inst and data,
6117 * and to save runtime data when powering down.
6118 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006119static int iwl4965_set_ucode_ptrs(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07006120{
6121 dma_addr_t pinst;
6122 dma_addr_t pdata;
6123 int rc = 0;
6124 unsigned long flags;
6125
6126 /* bits 35:4 for 4965 */
6127 pinst = priv->ucode_code.p_addr >> 4;
6128 pdata = priv->ucode_data_backup.p_addr >> 4;
6129
6130 spin_lock_irqsave(&priv->lock, flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006131 rc = iwl4965_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006132 if (rc) {
6133 spin_unlock_irqrestore(&priv->lock, flags);
6134 return rc;
6135 }
6136
6137 /* Tell bootstrap uCode where to find image to load */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006138 iwl4965_write_prph(priv, BSM_DRAM_INST_PTR_REG, pinst);
6139 iwl4965_write_prph(priv, BSM_DRAM_DATA_PTR_REG, pdata);
6140 iwl4965_write_prph(priv, BSM_DRAM_DATA_BYTECOUNT_REG,
Zhu Yib481de92007-09-25 17:54:57 -07006141 priv->ucode_data.len);
6142
6143 /* Inst bytecount must be last to set up, bit 31 signals uCode
6144 * that all new ptr/size info is in place */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006145 iwl4965_write_prph(priv, BSM_DRAM_INST_BYTECOUNT_REG,
Zhu Yib481de92007-09-25 17:54:57 -07006146 priv->ucode_code.len | BSM_DRAM_INST_LOAD);
6147
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006148 iwl4965_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006149
6150 spin_unlock_irqrestore(&priv->lock, flags);
6151
6152 IWL_DEBUG_INFO("Runtime uCode pointers are set.\n");
6153
6154 return rc;
6155}
6156
6157/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006158 * iwl4965_init_alive_start - Called after REPLY_ALIVE notification received
Zhu Yib481de92007-09-25 17:54:57 -07006159 *
6160 * Called after REPLY_ALIVE notification received from "initialize" uCode.
6161 *
6162 * The 4965 "initialize" ALIVE reply contains calibration data for:
6163 * Voltage, temperature, and MIMO tx gain correction, now stored in priv
6164 * (3945 does not contain this data).
6165 *
6166 * Tell "initialize" uCode to go ahead and load the runtime uCode.
6167*/
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006168static void iwl4965_init_alive_start(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07006169{
6170 /* Check alive response for "valid" sign from uCode */
6171 if (priv->card_alive_init.is_valid != UCODE_VALID_OK) {
6172 /* We had an error bringing up the hardware, so take it
6173 * all the way back down so we can try again */
6174 IWL_DEBUG_INFO("Initialize Alive failed.\n");
6175 goto restart;
6176 }
6177
6178 /* Bootstrap uCode has loaded initialize uCode ... verify inst image.
6179 * This is a paranoid check, because we would not have gotten the
6180 * "initialize" alive if code weren't properly loaded. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006181 if (iwl4965_verify_ucode(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07006182 /* Runtime instruction load was bad;
6183 * take it all the way back down so we can try again */
6184 IWL_DEBUG_INFO("Bad \"initialize\" uCode load.\n");
6185 goto restart;
6186 }
6187
6188 /* Calculate temperature */
6189 priv->temperature = iwl4965_get_temperature(priv);
6190
6191 /* Send pointers to protocol/runtime uCode image ... init code will
6192 * load and launch runtime uCode, which will send us another "Alive"
6193 * notification. */
6194 IWL_DEBUG_INFO("Initialization Alive received.\n");
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006195 if (iwl4965_set_ucode_ptrs(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07006196 /* Runtime instruction load won't happen;
6197 * take it all the way back down so we can try again */
6198 IWL_DEBUG_INFO("Couldn't set up uCode pointers.\n");
6199 goto restart;
6200 }
6201 return;
6202
6203 restart:
6204 queue_work(priv->workqueue, &priv->restart);
6205}
6206
6207
6208/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006209 * iwl4965_alive_start - called after REPLY_ALIVE notification received
Zhu Yib481de92007-09-25 17:54:57 -07006210 * from protocol/runtime uCode (initialization uCode's
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006211 * Alive gets handled by iwl4965_init_alive_start()).
Zhu Yib481de92007-09-25 17:54:57 -07006212 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006213static void iwl4965_alive_start(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07006214{
6215 int rc = 0;
6216
6217 IWL_DEBUG_INFO("Runtime Alive received.\n");
6218
6219 if (priv->card_alive.is_valid != UCODE_VALID_OK) {
6220 /* We had an error bringing up the hardware, so take it
6221 * all the way back down so we can try again */
6222 IWL_DEBUG_INFO("Alive failed.\n");
6223 goto restart;
6224 }
6225
6226 /* Initialize uCode has loaded Runtime uCode ... verify inst image.
6227 * This is a paranoid check, because we would not have gotten the
6228 * "runtime" alive if code weren't properly loaded. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006229 if (iwl4965_verify_ucode(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07006230 /* Runtime instruction load was bad;
6231 * take it all the way back down so we can try again */
6232 IWL_DEBUG_INFO("Bad runtime uCode load.\n");
6233 goto restart;
6234 }
6235
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006236 iwl4965_clear_stations_table(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006237
6238 rc = iwl4965_alive_notify(priv);
6239 if (rc) {
6240 IWL_WARNING("Could not complete ALIVE transition [ntf]: %d\n",
6241 rc);
6242 goto restart;
6243 }
6244
Ben Cahill9fbab512007-11-29 11:09:47 +08006245 /* After the ALIVE response, we can send host commands to 4965 uCode */
Zhu Yib481de92007-09-25 17:54:57 -07006246 set_bit(STATUS_ALIVE, &priv->status);
6247
6248 /* Clear out the uCode error bit if it is set */
6249 clear_bit(STATUS_FW_ERROR, &priv->status);
6250
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006251 if (iwl4965_is_rfkill(priv))
Zhu Yib481de92007-09-25 17:54:57 -07006252 return;
6253
Zhu Yi5a66926a2008-01-14 17:46:18 -08006254 ieee80211_start_queues(priv->hw);
Zhu Yib481de92007-09-25 17:54:57 -07006255
6256 priv->active_rate = priv->rates_mask;
6257 priv->active_rate_basic = priv->rates_mask & IWL_BASIC_RATES_MASK;
6258
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006259 iwl4965_send_power_mode(priv, IWL_POWER_LEVEL(priv->power_mode));
Zhu Yib481de92007-09-25 17:54:57 -07006260
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006261 if (iwl4965_is_associated(priv)) {
6262 struct iwl4965_rxon_cmd *active_rxon =
6263 (struct iwl4965_rxon_cmd *)(&priv->active_rxon);
Zhu Yib481de92007-09-25 17:54:57 -07006264
6265 memcpy(&priv->staging_rxon, &priv->active_rxon,
6266 sizeof(priv->staging_rxon));
6267 active_rxon->filter_flags &= ~RXON_FILTER_ASSOC_MSK;
6268 } else {
6269 /* Initialize our rx_config data */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006270 iwl4965_connection_init_rx_config(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006271 memcpy(priv->staging_rxon.node_addr, priv->mac_addr, ETH_ALEN);
6272 }
6273
Ben Cahill9fbab512007-11-29 11:09:47 +08006274 /* Configure Bluetooth device coexistence support */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006275 iwl4965_send_bt_config(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006276
6277 /* Configure the adapter for unassociated operation */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006278 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006279
6280 /* At this point, the NIC is initialized and operational */
6281 priv->notif_missed_beacons = 0;
6282 set_bit(STATUS_READY, &priv->status);
6283
6284 iwl4965_rf_kill_ct_config(priv);
Zhu Yi5a66926a2008-01-14 17:46:18 -08006285
Zhu Yib481de92007-09-25 17:54:57 -07006286 IWL_DEBUG_INFO("ALIVE processing complete.\n");
Zhu Yi5a66926a2008-01-14 17:46:18 -08006287 wake_up_interruptible(&priv->wait_command_queue);
Zhu Yib481de92007-09-25 17:54:57 -07006288
6289 if (priv->error_recovering)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006290 iwl4965_error_recovery(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006291
6292 return;
6293
6294 restart:
6295 queue_work(priv->workqueue, &priv->restart);
6296}
6297
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006298static void iwl4965_cancel_deferred_work(struct iwl_priv *priv);
Zhu Yib481de92007-09-25 17:54:57 -07006299
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006300static void __iwl4965_down(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07006301{
6302 unsigned long flags;
6303 int exit_pending = test_bit(STATUS_EXIT_PENDING, &priv->status);
6304 struct ieee80211_conf *conf = NULL;
6305
6306 IWL_DEBUG_INFO(DRV_NAME " is going down\n");
6307
6308 conf = ieee80211_get_hw_conf(priv->hw);
6309
6310 if (!exit_pending)
6311 set_bit(STATUS_EXIT_PENDING, &priv->status);
6312
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006313 iwl4965_clear_stations_table(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006314
6315 /* Unblock any waiting calls */
6316 wake_up_interruptible_all(&priv->wait_command_queue);
6317
Zhu Yib481de92007-09-25 17:54:57 -07006318 /* Wipe out the EXIT_PENDING status bit if we are not actually
6319 * exiting the module */
6320 if (!exit_pending)
6321 clear_bit(STATUS_EXIT_PENDING, &priv->status);
6322
6323 /* stop and reset the on-board processor */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006324 iwl4965_write32(priv, CSR_RESET, CSR_RESET_REG_FLAG_NEVO_RESET);
Zhu Yib481de92007-09-25 17:54:57 -07006325
6326 /* tell the device to stop sending interrupts */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006327 iwl4965_disable_interrupts(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006328
6329 if (priv->mac80211_registered)
6330 ieee80211_stop_queues(priv->hw);
6331
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006332 /* If we have not previously called iwl4965_init() then
Zhu Yib481de92007-09-25 17:54:57 -07006333 * clear all bits but the RF Kill and SUSPEND bits and return */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006334 if (!iwl4965_is_init(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07006335 priv->status = test_bit(STATUS_RF_KILL_HW, &priv->status) <<
6336 STATUS_RF_KILL_HW |
6337 test_bit(STATUS_RF_KILL_SW, &priv->status) <<
6338 STATUS_RF_KILL_SW |
Reinette Chatre97888642008-02-06 11:20:38 -08006339 test_bit(STATUS_GEO_CONFIGURED, &priv->status) <<
6340 STATUS_GEO_CONFIGURED |
Zhu Yib481de92007-09-25 17:54:57 -07006341 test_bit(STATUS_IN_SUSPEND, &priv->status) <<
6342 STATUS_IN_SUSPEND;
6343 goto exit;
6344 }
6345
6346 /* ...otherwise clear out all the status bits but the RF Kill and
6347 * SUSPEND bits and continue taking the NIC down. */
6348 priv->status &= test_bit(STATUS_RF_KILL_HW, &priv->status) <<
6349 STATUS_RF_KILL_HW |
6350 test_bit(STATUS_RF_KILL_SW, &priv->status) <<
6351 STATUS_RF_KILL_SW |
Reinette Chatre97888642008-02-06 11:20:38 -08006352 test_bit(STATUS_GEO_CONFIGURED, &priv->status) <<
6353 STATUS_GEO_CONFIGURED |
Zhu Yib481de92007-09-25 17:54:57 -07006354 test_bit(STATUS_IN_SUSPEND, &priv->status) <<
6355 STATUS_IN_SUSPEND |
6356 test_bit(STATUS_FW_ERROR, &priv->status) <<
6357 STATUS_FW_ERROR;
6358
6359 spin_lock_irqsave(&priv->lock, flags);
Ben Cahill9fbab512007-11-29 11:09:47 +08006360 iwl4965_clear_bit(priv, CSR_GP_CNTRL,
6361 CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
Zhu Yib481de92007-09-25 17:54:57 -07006362 spin_unlock_irqrestore(&priv->lock, flags);
6363
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006364 iwl4965_hw_txq_ctx_stop(priv);
6365 iwl4965_hw_rxq_stop(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006366
6367 spin_lock_irqsave(&priv->lock, flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006368 if (!iwl4965_grab_nic_access(priv)) {
6369 iwl4965_write_prph(priv, APMG_CLK_DIS_REG,
Zhu Yib481de92007-09-25 17:54:57 -07006370 APMG_CLK_VAL_DMA_CLK_RQT);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006371 iwl4965_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006372 }
6373 spin_unlock_irqrestore(&priv->lock, flags);
6374
6375 udelay(5);
6376
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006377 iwl4965_hw_nic_stop_master(priv);
6378 iwl4965_set_bit(priv, CSR_RESET, CSR_RESET_REG_FLAG_SW_RESET);
6379 iwl4965_hw_nic_reset(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006380
6381 exit:
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006382 memset(&priv->card_alive, 0, sizeof(struct iwl4965_alive_resp));
Zhu Yib481de92007-09-25 17:54:57 -07006383
6384 if (priv->ibss_beacon)
6385 dev_kfree_skb(priv->ibss_beacon);
6386 priv->ibss_beacon = NULL;
6387
6388 /* clear out any free frames */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006389 iwl4965_clear_free_frames(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006390}
6391
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006392static void iwl4965_down(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07006393{
6394 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006395 __iwl4965_down(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006396 mutex_unlock(&priv->mutex);
Zhu Yib24d22b2007-12-19 13:59:52 +08006397
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006398 iwl4965_cancel_deferred_work(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006399}
6400
6401#define MAX_HW_RESTARTS 5
6402
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006403static int __iwl4965_up(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07006404{
6405 int rc, i;
Zhu Yib481de92007-09-25 17:54:57 -07006406
6407 if (test_bit(STATUS_EXIT_PENDING, &priv->status)) {
6408 IWL_WARNING("Exit pending; will not bring the NIC up\n");
6409 return -EIO;
6410 }
6411
6412 if (test_bit(STATUS_RF_KILL_SW, &priv->status)) {
6413 IWL_WARNING("Radio disabled by SW RF kill (module "
6414 "parameter)\n");
Zhu Yie655b9f2008-01-24 02:19:38 -08006415 return -ENODEV;
6416 }
6417
Reinette Chatree903fbd2008-01-30 22:05:15 -08006418 if (!priv->ucode_data_backup.v_addr || !priv->ucode_data.v_addr) {
6419 IWL_ERROR("ucode not available for device bringup\n");
6420 return -EIO;
6421 }
6422
Zhu Yie655b9f2008-01-24 02:19:38 -08006423 /* If platform's RF_KILL switch is NOT set to KILL */
6424 if (iwl4965_read32(priv, CSR_GP_CNTRL) &
6425 CSR_GP_CNTRL_REG_FLAG_HW_RF_KILL_SW)
6426 clear_bit(STATUS_RF_KILL_HW, &priv->status);
6427 else {
6428 set_bit(STATUS_RF_KILL_HW, &priv->status);
6429 if (!test_bit(STATUS_IN_SUSPEND, &priv->status)) {
6430 IWL_WARNING("Radio disabled by HW RF Kill switch\n");
6431 return -ENODEV;
6432 }
Zhu Yib481de92007-09-25 17:54:57 -07006433 }
6434
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006435 iwl4965_write32(priv, CSR_INT, 0xFFFFFFFF);
Zhu Yib481de92007-09-25 17:54:57 -07006436
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006437 rc = iwl4965_hw_nic_init(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006438 if (rc) {
6439 IWL_ERROR("Unable to int nic\n");
6440 return rc;
6441 }
6442
6443 /* make sure rfkill handshake bits are cleared */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006444 iwl4965_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
6445 iwl4965_write32(priv, CSR_UCODE_DRV_GP1_CLR,
Zhu Yib481de92007-09-25 17:54:57 -07006446 CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
6447
6448 /* clear (again), then enable host interrupts */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006449 iwl4965_write32(priv, CSR_INT, 0xFFFFFFFF);
6450 iwl4965_enable_interrupts(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006451
6452 /* really make sure rfkill handshake bits are cleared */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006453 iwl4965_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
6454 iwl4965_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
Zhu Yib481de92007-09-25 17:54:57 -07006455
6456 /* Copy original ucode data image from disk into backup cache.
6457 * This will be used to initialize the on-board processor's
6458 * data SRAM for a clean start when the runtime program first loads. */
6459 memcpy(priv->ucode_data_backup.v_addr, priv->ucode_data.v_addr,
Zhu Yi5a66926a2008-01-14 17:46:18 -08006460 priv->ucode_data.len);
Zhu Yib481de92007-09-25 17:54:57 -07006461
Zhu Yie655b9f2008-01-24 02:19:38 -08006462 /* We return success when we resume from suspend and rf_kill is on. */
6463 if (test_bit(STATUS_RF_KILL_HW, &priv->status))
Zhu Yib481de92007-09-25 17:54:57 -07006464 return 0;
Zhu Yib481de92007-09-25 17:54:57 -07006465
6466 for (i = 0; i < MAX_HW_RESTARTS; i++) {
6467
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006468 iwl4965_clear_stations_table(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006469
6470 /* load bootstrap state machine,
6471 * load bootstrap program into processor's memory,
6472 * prepare to load the "initialize" uCode */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006473 rc = iwl4965_load_bsm(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006474
6475 if (rc) {
6476 IWL_ERROR("Unable to set up bootstrap uCode: %d\n", rc);
6477 continue;
6478 }
6479
6480 /* start card; "initialize" will load runtime ucode */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006481 iwl4965_nic_start(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006482
Zhu Yib481de92007-09-25 17:54:57 -07006483 IWL_DEBUG_INFO(DRV_NAME " is coming up\n");
6484
6485 return 0;
6486 }
6487
6488 set_bit(STATUS_EXIT_PENDING, &priv->status);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006489 __iwl4965_down(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006490
6491 /* tried to restart and config the device for as long as our
6492 * patience could withstand */
6493 IWL_ERROR("Unable to initialize device after %d attempts.\n", i);
6494 return -EIO;
6495}
6496
6497
6498/*****************************************************************************
6499 *
6500 * Workqueue callbacks
6501 *
6502 *****************************************************************************/
6503
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006504static void iwl4965_bg_init_alive_start(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07006505{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006506 struct iwl_priv *priv =
6507 container_of(data, struct iwl_priv, init_alive_start.work);
Zhu Yib481de92007-09-25 17:54:57 -07006508
6509 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
6510 return;
6511
6512 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006513 iwl4965_init_alive_start(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006514 mutex_unlock(&priv->mutex);
6515}
6516
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006517static void iwl4965_bg_alive_start(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07006518{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006519 struct iwl_priv *priv =
6520 container_of(data, struct iwl_priv, alive_start.work);
Zhu Yib481de92007-09-25 17:54:57 -07006521
6522 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
6523 return;
6524
6525 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006526 iwl4965_alive_start(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006527 mutex_unlock(&priv->mutex);
6528}
6529
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006530static void iwl4965_bg_rf_kill(struct work_struct *work)
Zhu Yib481de92007-09-25 17:54:57 -07006531{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006532 struct iwl_priv *priv = container_of(work, struct iwl_priv, rf_kill);
Zhu Yib481de92007-09-25 17:54:57 -07006533
6534 wake_up_interruptible(&priv->wait_command_queue);
6535
6536 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
6537 return;
6538
6539 mutex_lock(&priv->mutex);
6540
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006541 if (!iwl4965_is_rfkill(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07006542 IWL_DEBUG(IWL_DL_INFO | IWL_DL_RF_KILL,
6543 "HW and/or SW RF Kill no longer active, restarting "
6544 "device\n");
6545 if (!test_bit(STATUS_EXIT_PENDING, &priv->status))
6546 queue_work(priv->workqueue, &priv->restart);
6547 } else {
6548
6549 if (!test_bit(STATUS_RF_KILL_HW, &priv->status))
6550 IWL_DEBUG_RF_KILL("Can not turn radio back on - "
6551 "disabled by SW switch\n");
6552 else
6553 IWL_WARNING("Radio Frequency Kill Switch is On:\n"
6554 "Kill switch must be turned off for "
6555 "wireless networking to work.\n");
6556 }
6557 mutex_unlock(&priv->mutex);
6558}
6559
6560#define IWL_SCAN_CHECK_WATCHDOG (7 * HZ)
6561
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006562static void iwl4965_bg_scan_check(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07006563{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006564 struct iwl_priv *priv =
6565 container_of(data, struct iwl_priv, scan_check.work);
Zhu Yib481de92007-09-25 17:54:57 -07006566
6567 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
6568 return;
6569
6570 mutex_lock(&priv->mutex);
6571 if (test_bit(STATUS_SCANNING, &priv->status) ||
6572 test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
6573 IWL_DEBUG(IWL_DL_INFO | IWL_DL_SCAN,
6574 "Scan completion watchdog resetting adapter (%dms)\n",
6575 jiffies_to_msecs(IWL_SCAN_CHECK_WATCHDOG));
mabbas052c4b92007-10-25 17:15:43 +08006576
Zhu Yib481de92007-09-25 17:54:57 -07006577 if (!test_bit(STATUS_EXIT_PENDING, &priv->status))
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006578 iwl4965_send_scan_abort(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006579 }
6580 mutex_unlock(&priv->mutex);
6581}
6582
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006583static void iwl4965_bg_request_scan(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07006584{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006585 struct iwl_priv *priv =
6586 container_of(data, struct iwl_priv, request_scan);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006587 struct iwl4965_host_cmd cmd = {
Zhu Yib481de92007-09-25 17:54:57 -07006588 .id = REPLY_SCAN_CMD,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006589 .len = sizeof(struct iwl4965_scan_cmd),
Zhu Yib481de92007-09-25 17:54:57 -07006590 .meta.flags = CMD_SIZE_HUGE,
6591 };
6592 int rc = 0;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006593 struct iwl4965_scan_cmd *scan;
Zhu Yib481de92007-09-25 17:54:57 -07006594 struct ieee80211_conf *conf = NULL;
Tomas Winkler78330fd2008-02-06 02:37:18 +02006595 u16 cmd_len;
Johannes Berg8318d782008-01-24 19:38:38 +01006596 enum ieee80211_band band;
Tomas Winkler78330fd2008-02-06 02:37:18 +02006597 u8 direct_mask;
Zhu Yib481de92007-09-25 17:54:57 -07006598
6599 conf = ieee80211_get_hw_conf(priv->hw);
6600
6601 mutex_lock(&priv->mutex);
6602
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006603 if (!iwl4965_is_ready(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07006604 IWL_WARNING("request scan called when driver not ready.\n");
6605 goto done;
6606 }
6607
6608 /* Make sure the scan wasn't cancelled before this queued work
6609 * was given the chance to run... */
6610 if (!test_bit(STATUS_SCANNING, &priv->status))
6611 goto done;
6612
6613 /* This should never be called or scheduled if there is currently
6614 * a scan active in the hardware. */
6615 if (test_bit(STATUS_SCAN_HW, &priv->status)) {
6616 IWL_DEBUG_INFO("Multiple concurrent scan requests in parallel. "
6617 "Ignoring second request.\n");
6618 rc = -EIO;
6619 goto done;
6620 }
6621
6622 if (test_bit(STATUS_EXIT_PENDING, &priv->status)) {
6623 IWL_DEBUG_SCAN("Aborting scan due to device shutdown\n");
6624 goto done;
6625 }
6626
6627 if (test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
6628 IWL_DEBUG_HC("Scan request while abort pending. Queuing.\n");
6629 goto done;
6630 }
6631
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006632 if (iwl4965_is_rfkill(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07006633 IWL_DEBUG_HC("Aborting scan due to RF Kill activation\n");
6634 goto done;
6635 }
6636
6637 if (!test_bit(STATUS_READY, &priv->status)) {
6638 IWL_DEBUG_HC("Scan request while uninitialized. Queuing.\n");
6639 goto done;
6640 }
6641
6642 if (!priv->scan_bands) {
6643 IWL_DEBUG_HC("Aborting scan due to no requested bands\n");
6644 goto done;
6645 }
6646
6647 if (!priv->scan) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006648 priv->scan = kmalloc(sizeof(struct iwl4965_scan_cmd) +
Zhu Yib481de92007-09-25 17:54:57 -07006649 IWL_MAX_SCAN_SIZE, GFP_KERNEL);
6650 if (!priv->scan) {
6651 rc = -ENOMEM;
6652 goto done;
6653 }
6654 }
6655 scan = priv->scan;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006656 memset(scan, 0, sizeof(struct iwl4965_scan_cmd) + IWL_MAX_SCAN_SIZE);
Zhu Yib481de92007-09-25 17:54:57 -07006657
6658 scan->quiet_plcp_th = IWL_PLCP_QUIET_THRESH;
6659 scan->quiet_time = IWL_ACTIVE_QUIET_TIME;
6660
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006661 if (iwl4965_is_associated(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07006662 u16 interval = 0;
6663 u32 extra;
6664 u32 suspend_time = 100;
6665 u32 scan_suspend_time = 100;
6666 unsigned long flags;
6667
6668 IWL_DEBUG_INFO("Scanning while associated...\n");
6669
6670 spin_lock_irqsave(&priv->lock, flags);
6671 interval = priv->beacon_int;
6672 spin_unlock_irqrestore(&priv->lock, flags);
6673
6674 scan->suspend_time = 0;
mabbas052c4b92007-10-25 17:15:43 +08006675 scan->max_out_time = cpu_to_le32(200 * 1024);
Zhu Yib481de92007-09-25 17:54:57 -07006676 if (!interval)
6677 interval = suspend_time;
6678
6679 extra = (suspend_time / interval) << 22;
6680 scan_suspend_time = (extra |
6681 ((suspend_time % interval) * 1024));
6682 scan->suspend_time = cpu_to_le32(scan_suspend_time);
6683 IWL_DEBUG_SCAN("suspend_time 0x%X beacon interval %d\n",
6684 scan_suspend_time, interval);
6685 }
6686
6687 /* We should add the ability for user to lock to PASSIVE ONLY */
6688 if (priv->one_direct_scan) {
6689 IWL_DEBUG_SCAN
6690 ("Kicking off one direct scan for '%s'\n",
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006691 iwl4965_escape_essid(priv->direct_ssid,
Zhu Yib481de92007-09-25 17:54:57 -07006692 priv->direct_ssid_len));
6693 scan->direct_scan[0].id = WLAN_EID_SSID;
6694 scan->direct_scan[0].len = priv->direct_ssid_len;
6695 memcpy(scan->direct_scan[0].ssid,
6696 priv->direct_ssid, priv->direct_ssid_len);
6697 direct_mask = 1;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006698 } else if (!iwl4965_is_associated(priv) && priv->essid_len) {
Zhu Yib481de92007-09-25 17:54:57 -07006699 scan->direct_scan[0].id = WLAN_EID_SSID;
6700 scan->direct_scan[0].len = priv->essid_len;
6701 memcpy(scan->direct_scan[0].ssid, priv->essid, priv->essid_len);
6702 direct_mask = 1;
6703 } else
6704 direct_mask = 0;
6705
Zhu Yib481de92007-09-25 17:54:57 -07006706 scan->tx_cmd.tx_flags = TX_CMD_FLG_SEQ_CTL_MSK;
6707 scan->tx_cmd.sta_id = priv->hw_setting.bcast_sta_id;
6708 scan->tx_cmd.stop_time.life_time = TX_CMD_LIFE_TIME_INFINITE;
6709
Zhu Yib481de92007-09-25 17:54:57 -07006710
6711 switch (priv->scan_bands) {
6712 case 2:
6713 scan->flags = RXON_FLG_BAND_24G_MSK | RXON_FLG_AUTO_DETECT_MSK;
6714 scan->tx_cmd.rate_n_flags =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006715 iwl4965_hw_set_rate_n_flags(IWL_RATE_1M_PLCP,
Zhu Yib481de92007-09-25 17:54:57 -07006716 RATE_MCS_ANT_B_MSK|RATE_MCS_CCK_MSK);
6717
6718 scan->good_CRC_th = 0;
Johannes Berg8318d782008-01-24 19:38:38 +01006719 band = IEEE80211_BAND_2GHZ;
Zhu Yib481de92007-09-25 17:54:57 -07006720 break;
6721
6722 case 1:
6723 scan->tx_cmd.rate_n_flags =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006724 iwl4965_hw_set_rate_n_flags(IWL_RATE_6M_PLCP,
Zhu Yib481de92007-09-25 17:54:57 -07006725 RATE_MCS_ANT_B_MSK);
6726 scan->good_CRC_th = IWL_GOOD_CRC_TH;
Johannes Berg8318d782008-01-24 19:38:38 +01006727 band = IEEE80211_BAND_5GHZ;
Zhu Yib481de92007-09-25 17:54:57 -07006728 break;
6729
6730 default:
6731 IWL_WARNING("Invalid scan band count\n");
6732 goto done;
6733 }
6734
Tomas Winkler78330fd2008-02-06 02:37:18 +02006735 /* We don't build a direct scan probe request; the uCode will do
6736 * that based on the direct_mask added to each channel entry */
6737 cmd_len = iwl4965_fill_probe_req(priv, band,
6738 (struct ieee80211_mgmt *)scan->data,
6739 IWL_MAX_SCAN_SIZE - sizeof(*scan), 0);
6740
6741 scan->tx_cmd.len = cpu_to_le16(cmd_len);
Zhu Yib481de92007-09-25 17:54:57 -07006742 /* select Rx chains */
6743
6744 /* Force use of chains B and C (0x6) for scan Rx.
6745 * Avoid A (0x1) because of its off-channel reception on A-band.
6746 * MIMO is not used here, but value is required to make uCode happy. */
6747 scan->rx_chain = RXON_RX_CHAIN_DRIVER_FORCE_MSK |
6748 cpu_to_le16((0x7 << RXON_RX_CHAIN_VALID_POS) |
6749 (0x6 << RXON_RX_CHAIN_FORCE_SEL_POS) |
6750 (0x7 << RXON_RX_CHAIN_FORCE_MIMO_SEL_POS));
6751
6752 if (priv->iw_mode == IEEE80211_IF_TYPE_MNTR)
6753 scan->filter_flags = RXON_FILTER_PROMISC_MSK;
6754
Reinette Chatre26c0f032008-03-11 16:17:15 -07006755 if (direct_mask) {
Zhu Yib481de92007-09-25 17:54:57 -07006756 IWL_DEBUG_SCAN
6757 ("Initiating direct scan for %s.\n",
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006758 iwl4965_escape_essid(priv->essid, priv->essid_len));
Reinette Chatre26c0f032008-03-11 16:17:15 -07006759 scan->channel_count =
6760 iwl4965_get_channels_for_scan(
6761 priv, band, 1, /* active */
6762 direct_mask,
6763 (void *)&scan->data[le16_to_cpu(scan->tx_cmd.len)]);
6764 } else {
Zhu Yib481de92007-09-25 17:54:57 -07006765 IWL_DEBUG_SCAN("Initiating indirect scan.\n");
Reinette Chatre26c0f032008-03-11 16:17:15 -07006766 scan->channel_count =
6767 iwl4965_get_channels_for_scan(
6768 priv, band, 0, /* passive */
6769 direct_mask,
6770 (void *)&scan->data[le16_to_cpu(scan->tx_cmd.len)]);
6771 }
Zhu Yib481de92007-09-25 17:54:57 -07006772
6773 cmd.len += le16_to_cpu(scan->tx_cmd.len) +
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006774 scan->channel_count * sizeof(struct iwl4965_scan_channel);
Zhu Yib481de92007-09-25 17:54:57 -07006775 cmd.data = scan;
6776 scan->len = cpu_to_le16(cmd.len);
6777
6778 set_bit(STATUS_SCAN_HW, &priv->status);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006779 rc = iwl4965_send_cmd_sync(priv, &cmd);
Zhu Yib481de92007-09-25 17:54:57 -07006780 if (rc)
6781 goto done;
6782
6783 queue_delayed_work(priv->workqueue, &priv->scan_check,
6784 IWL_SCAN_CHECK_WATCHDOG);
6785
6786 mutex_unlock(&priv->mutex);
6787 return;
6788
6789 done:
Ian Schram01ebd062007-10-25 17:15:22 +08006790 /* inform mac80211 scan aborted */
Zhu Yib481de92007-09-25 17:54:57 -07006791 queue_work(priv->workqueue, &priv->scan_completed);
6792 mutex_unlock(&priv->mutex);
6793}
6794
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006795static void iwl4965_bg_up(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07006796{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006797 struct iwl_priv *priv = container_of(data, struct iwl_priv, up);
Zhu Yib481de92007-09-25 17:54:57 -07006798
6799 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
6800 return;
6801
6802 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006803 __iwl4965_up(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006804 mutex_unlock(&priv->mutex);
6805}
6806
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006807static void iwl4965_bg_restart(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07006808{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006809 struct iwl_priv *priv = container_of(data, struct iwl_priv, restart);
Zhu Yib481de92007-09-25 17:54:57 -07006810
6811 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
6812 return;
6813
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006814 iwl4965_down(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006815 queue_work(priv->workqueue, &priv->up);
6816}
6817
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006818static void iwl4965_bg_rx_replenish(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07006819{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006820 struct iwl_priv *priv =
6821 container_of(data, struct iwl_priv, rx_replenish);
Zhu Yib481de92007-09-25 17:54:57 -07006822
6823 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
6824 return;
6825
6826 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006827 iwl4965_rx_replenish(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006828 mutex_unlock(&priv->mutex);
6829}
6830
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08006831#define IWL_DELAY_NEXT_SCAN (HZ*2)
6832
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006833static void iwl4965_bg_post_associate(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07006834{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006835 struct iwl_priv *priv = container_of(data, struct iwl_priv,
Zhu Yib481de92007-09-25 17:54:57 -07006836 post_associate.work);
6837
6838 int rc = 0;
6839 struct ieee80211_conf *conf = NULL;
Joe Perches0795af52007-10-03 17:59:30 -07006840 DECLARE_MAC_BUF(mac);
Zhu Yib481de92007-09-25 17:54:57 -07006841
6842 if (priv->iw_mode == IEEE80211_IF_TYPE_AP) {
6843 IWL_ERROR("%s Should not be called in AP mode\n", __FUNCTION__);
6844 return;
6845 }
6846
Joe Perches0795af52007-10-03 17:59:30 -07006847 IWL_DEBUG_ASSOC("Associated as %d to: %s\n",
6848 priv->assoc_id,
6849 print_mac(mac, priv->active_rxon.bssid_addr));
Zhu Yib481de92007-09-25 17:54:57 -07006850
6851
6852 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
6853 return;
6854
6855 mutex_lock(&priv->mutex);
6856
Johannes Berg32bfd352007-12-19 01:31:26 +01006857 if (!priv->vif || !priv->is_open) {
Mohamed Abbas948c1712007-10-25 17:15:45 +08006858 mutex_unlock(&priv->mutex);
6859 return;
6860 }
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006861 iwl4965_scan_cancel_timeout(priv, 200);
mabbas052c4b92007-10-25 17:15:43 +08006862
Zhu Yib481de92007-09-25 17:54:57 -07006863 conf = ieee80211_get_hw_conf(priv->hw);
6864
6865 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006866 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006867
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006868 memset(&priv->rxon_timing, 0, sizeof(struct iwl4965_rxon_time_cmd));
6869 iwl4965_setup_rxon_timing(priv);
6870 rc = iwl4965_send_cmd_pdu(priv, REPLY_RXON_TIMING,
Zhu Yib481de92007-09-25 17:54:57 -07006871 sizeof(priv->rxon_timing), &priv->rxon_timing);
6872 if (rc)
6873 IWL_WARNING("REPLY_RXON_TIMING failed - "
6874 "Attempting to continue.\n");
6875
6876 priv->staging_rxon.filter_flags |= RXON_FILTER_ASSOC_MSK;
6877
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08006878#ifdef CONFIG_IWL4965_HT
Ron Rindjunskyfd105e72007-11-26 16:14:39 +02006879 if (priv->current_ht_config.is_ht)
6880 iwl4965_set_rxon_ht(priv, &priv->current_ht_config);
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08006881#endif /* CONFIG_IWL4965_HT*/
Zhu Yib481de92007-09-25 17:54:57 -07006882 iwl4965_set_rxon_chain(priv);
6883 priv->staging_rxon.assoc_id = cpu_to_le16(priv->assoc_id);
6884
6885 IWL_DEBUG_ASSOC("assoc id %d beacon interval %d\n",
6886 priv->assoc_id, priv->beacon_int);
6887
6888 if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_PREAMBLE)
6889 priv->staging_rxon.flags |= RXON_FLG_SHORT_PREAMBLE_MSK;
6890 else
6891 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_PREAMBLE_MSK;
6892
6893 if (priv->staging_rxon.flags & RXON_FLG_BAND_24G_MSK) {
6894 if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_SLOT_TIME)
6895 priv->staging_rxon.flags |= RXON_FLG_SHORT_SLOT_MSK;
6896 else
6897 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
6898
6899 if (priv->iw_mode == IEEE80211_IF_TYPE_IBSS)
6900 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
6901
6902 }
6903
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006904 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006905
6906 switch (priv->iw_mode) {
6907 case IEEE80211_IF_TYPE_STA:
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006908 iwl4965_rate_scale_init(priv->hw, IWL_AP_ID);
Zhu Yib481de92007-09-25 17:54:57 -07006909 break;
6910
6911 case IEEE80211_IF_TYPE_IBSS:
6912
6913 /* clear out the station table */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006914 iwl4965_clear_stations_table(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006915
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006916 iwl4965_rxon_add_station(priv, iwl4965_broadcast_addr, 0);
6917 iwl4965_rxon_add_station(priv, priv->bssid, 0);
6918 iwl4965_rate_scale_init(priv->hw, IWL_STA_ID);
6919 iwl4965_send_beacon_cmd(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006920
6921 break;
6922
6923 default:
6924 IWL_ERROR("%s Should not be called in %d mode\n",
6925 __FUNCTION__, priv->iw_mode);
6926 break;
6927 }
6928
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006929 iwl4965_sequence_reset(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006930
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08006931#ifdef CONFIG_IWL4965_SENSITIVITY
Zhu Yib481de92007-09-25 17:54:57 -07006932 /* Enable Rx differential gain and sensitivity calibrations */
6933 iwl4965_chain_noise_reset(priv);
6934 priv->start_calib = 1;
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08006935#endif /* CONFIG_IWL4965_SENSITIVITY */
Zhu Yib481de92007-09-25 17:54:57 -07006936
6937 if (priv->iw_mode == IEEE80211_IF_TYPE_IBSS)
6938 priv->assoc_station_added = 1;
6939
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006940 iwl4965_activate_qos(priv, 0);
Ron Rindjunsky292ae172008-02-06 11:20:39 -08006941
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08006942 /* we have just associated, don't start scan too early */
6943 priv->next_scan_jiffies = jiffies + IWL_DELAY_NEXT_SCAN;
Zhu Yib481de92007-09-25 17:54:57 -07006944 mutex_unlock(&priv->mutex);
6945}
6946
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006947static void iwl4965_bg_abort_scan(struct work_struct *work)
Zhu Yib481de92007-09-25 17:54:57 -07006948{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006949 struct iwl_priv *priv = container_of(work, struct iwl_priv, abort_scan);
Zhu Yib481de92007-09-25 17:54:57 -07006950
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006951 if (!iwl4965_is_ready(priv))
Zhu Yib481de92007-09-25 17:54:57 -07006952 return;
6953
6954 mutex_lock(&priv->mutex);
6955
6956 set_bit(STATUS_SCAN_ABORTING, &priv->status);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006957 iwl4965_send_scan_abort(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006958
6959 mutex_unlock(&priv->mutex);
6960}
6961
Zhu Yi76bb77e2007-11-22 10:53:22 +08006962static int iwl4965_mac_config(struct ieee80211_hw *hw, struct ieee80211_conf *conf);
6963
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006964static void iwl4965_bg_scan_completed(struct work_struct *work)
Zhu Yib481de92007-09-25 17:54:57 -07006965{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006966 struct iwl_priv *priv =
6967 container_of(work, struct iwl_priv, scan_completed);
Zhu Yib481de92007-09-25 17:54:57 -07006968
6969 IWL_DEBUG(IWL_DL_INFO | IWL_DL_SCAN, "SCAN complete scan\n");
6970
6971 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
6972 return;
6973
Zhu Yia0646472007-12-20 14:10:01 +08006974 if (test_bit(STATUS_CONF_PENDING, &priv->status))
6975 iwl4965_mac_config(priv->hw, ieee80211_get_hw_conf(priv->hw));
Zhu Yi76bb77e2007-11-22 10:53:22 +08006976
Zhu Yib481de92007-09-25 17:54:57 -07006977 ieee80211_scan_completed(priv->hw);
6978
6979 /* Since setting the TXPOWER may have been deferred while
6980 * performing the scan, fire one off */
6981 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006982 iwl4965_hw_reg_send_txpower(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006983 mutex_unlock(&priv->mutex);
6984}
6985
6986/*****************************************************************************
6987 *
6988 * mac80211 entry point functions
6989 *
6990 *****************************************************************************/
6991
Zhu Yi5a66926a2008-01-14 17:46:18 -08006992#define UCODE_READY_TIMEOUT (2 * HZ)
6993
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006994static int iwl4965_mac_start(struct ieee80211_hw *hw)
Zhu Yib481de92007-09-25 17:54:57 -07006995{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006996 struct iwl_priv *priv = hw->priv;
Zhu Yi5a66926a2008-01-14 17:46:18 -08006997 int ret;
Zhu Yib481de92007-09-25 17:54:57 -07006998
6999 IWL_DEBUG_MAC80211("enter\n");
7000
Zhu Yi5a66926a2008-01-14 17:46:18 -08007001 if (pci_enable_device(priv->pci_dev)) {
7002 IWL_ERROR("Fail to pci_enable_device\n");
7003 return -ENODEV;
7004 }
7005 pci_restore_state(priv->pci_dev);
7006 pci_enable_msi(priv->pci_dev);
7007
7008 ret = request_irq(priv->pci_dev->irq, iwl4965_isr, IRQF_SHARED,
7009 DRV_NAME, priv);
7010 if (ret) {
7011 IWL_ERROR("Error allocating IRQ %d\n", priv->pci_dev->irq);
7012 goto out_disable_msi;
7013 }
7014
Zhu Yib481de92007-09-25 17:54:57 -07007015 /* we should be verifying the device is ready to be opened */
7016 mutex_lock(&priv->mutex);
7017
Zhu Yi5a66926a2008-01-14 17:46:18 -08007018 memset(&priv->staging_rxon, 0, sizeof(struct iwl4965_rxon_cmd));
7019 /* fetch ucode file from disk, alloc and copy to bus-master buffers ...
7020 * ucode filename and max sizes are card-specific. */
7021
7022 if (!priv->ucode_code.len) {
7023 ret = iwl4965_read_ucode(priv);
7024 if (ret) {
7025 IWL_ERROR("Could not read microcode: %d\n", ret);
7026 mutex_unlock(&priv->mutex);
7027 goto out_release_irq;
7028 }
7029 }
7030
Zhu Yie655b9f2008-01-24 02:19:38 -08007031 ret = __iwl4965_up(priv);
Zhu Yi5a66926a2008-01-14 17:46:18 -08007032
Zhu Yib481de92007-09-25 17:54:57 -07007033 mutex_unlock(&priv->mutex);
Zhu Yi5a66926a2008-01-14 17:46:18 -08007034
Zhu Yie655b9f2008-01-24 02:19:38 -08007035 if (ret)
7036 goto out_release_irq;
7037
7038 IWL_DEBUG_INFO("Start UP work done.\n");
7039
7040 if (test_bit(STATUS_IN_SUSPEND, &priv->status))
7041 return 0;
7042
Zhu Yi5a66926a2008-01-14 17:46:18 -08007043 /* Wait for START_ALIVE from ucode. Otherwise callbacks from
7044 * mac80211 will not be run successfully. */
7045 ret = wait_event_interruptible_timeout(priv->wait_command_queue,
7046 test_bit(STATUS_READY, &priv->status),
7047 UCODE_READY_TIMEOUT);
7048 if (!ret) {
7049 if (!test_bit(STATUS_READY, &priv->status)) {
7050 IWL_ERROR("Wait for START_ALIVE timeout after %dms.\n",
7051 jiffies_to_msecs(UCODE_READY_TIMEOUT));
7052 ret = -ETIMEDOUT;
7053 goto out_release_irq;
7054 }
7055 }
7056
Zhu Yie655b9f2008-01-24 02:19:38 -08007057 priv->is_open = 1;
Zhu Yib481de92007-09-25 17:54:57 -07007058 IWL_DEBUG_MAC80211("leave\n");
7059 return 0;
Zhu Yi5a66926a2008-01-14 17:46:18 -08007060
7061out_release_irq:
7062 free_irq(priv->pci_dev->irq, priv);
7063out_disable_msi:
7064 pci_disable_msi(priv->pci_dev);
Zhu Yie655b9f2008-01-24 02:19:38 -08007065 pci_disable_device(priv->pci_dev);
7066 priv->is_open = 0;
7067 IWL_DEBUG_MAC80211("leave - failed\n");
Zhu Yi5a66926a2008-01-14 17:46:18 -08007068 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07007069}
7070
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007071static void iwl4965_mac_stop(struct ieee80211_hw *hw)
Zhu Yib481de92007-09-25 17:54:57 -07007072{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007073 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07007074
7075 IWL_DEBUG_MAC80211("enter\n");
Mohamed Abbas948c1712007-10-25 17:15:45 +08007076
Zhu Yie655b9f2008-01-24 02:19:38 -08007077 if (!priv->is_open) {
7078 IWL_DEBUG_MAC80211("leave - skip\n");
7079 return;
7080 }
7081
Zhu Yib481de92007-09-25 17:54:57 -07007082 priv->is_open = 0;
Zhu Yi5a66926a2008-01-14 17:46:18 -08007083
7084 if (iwl4965_is_ready_rf(priv)) {
Zhu Yie655b9f2008-01-24 02:19:38 -08007085 /* stop mac, cancel any scan request and clear
7086 * RXON_FILTER_ASSOC_MSK BIT
7087 */
Zhu Yi5a66926a2008-01-14 17:46:18 -08007088 mutex_lock(&priv->mutex);
7089 iwl4965_scan_cancel_timeout(priv, 100);
7090 cancel_delayed_work(&priv->post_associate);
Mohamed Abbasfde35712007-11-29 11:10:15 +08007091 mutex_unlock(&priv->mutex);
Mohamed Abbasfde35712007-11-29 11:10:15 +08007092 }
7093
Zhu Yi5a66926a2008-01-14 17:46:18 -08007094 iwl4965_down(priv);
7095
7096 flush_workqueue(priv->workqueue);
7097 free_irq(priv->pci_dev->irq, priv);
7098 pci_disable_msi(priv->pci_dev);
7099 pci_save_state(priv->pci_dev);
7100 pci_disable_device(priv->pci_dev);
Mohamed Abbas948c1712007-10-25 17:15:45 +08007101
Zhu Yib481de92007-09-25 17:54:57 -07007102 IWL_DEBUG_MAC80211("leave\n");
Zhu Yib481de92007-09-25 17:54:57 -07007103}
7104
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007105static int iwl4965_mac_tx(struct ieee80211_hw *hw, struct sk_buff *skb,
Zhu Yib481de92007-09-25 17:54:57 -07007106 struct ieee80211_tx_control *ctl)
7107{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007108 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07007109
7110 IWL_DEBUG_MAC80211("enter\n");
7111
7112 if (priv->iw_mode == IEEE80211_IF_TYPE_MNTR) {
7113 IWL_DEBUG_MAC80211("leave - monitor\n");
7114 return -1;
7115 }
7116
7117 IWL_DEBUG_TX("dev->xmit(%d bytes) at rate 0x%02x\n", skb->len,
Johannes Berg8318d782008-01-24 19:38:38 +01007118 ctl->tx_rate->bitrate);
Zhu Yib481de92007-09-25 17:54:57 -07007119
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007120 if (iwl4965_tx_skb(priv, skb, ctl))
Zhu Yib481de92007-09-25 17:54:57 -07007121 dev_kfree_skb_any(skb);
7122
7123 IWL_DEBUG_MAC80211("leave\n");
7124 return 0;
7125}
7126
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007127static int iwl4965_mac_add_interface(struct ieee80211_hw *hw,
Zhu Yib481de92007-09-25 17:54:57 -07007128 struct ieee80211_if_init_conf *conf)
7129{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007130 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07007131 unsigned long flags;
Joe Perches0795af52007-10-03 17:59:30 -07007132 DECLARE_MAC_BUF(mac);
Zhu Yib481de92007-09-25 17:54:57 -07007133
Johannes Berg32bfd352007-12-19 01:31:26 +01007134 IWL_DEBUG_MAC80211("enter: type %d\n", conf->type);
Zhu Yib481de92007-09-25 17:54:57 -07007135
Johannes Berg32bfd352007-12-19 01:31:26 +01007136 if (priv->vif) {
7137 IWL_DEBUG_MAC80211("leave - vif != NULL\n");
Reinette Chatre75849d22008-01-23 10:15:17 -08007138 return -EOPNOTSUPP;
Zhu Yib481de92007-09-25 17:54:57 -07007139 }
7140
7141 spin_lock_irqsave(&priv->lock, flags);
Johannes Berg32bfd352007-12-19 01:31:26 +01007142 priv->vif = conf->vif;
Zhu Yib481de92007-09-25 17:54:57 -07007143
7144 spin_unlock_irqrestore(&priv->lock, flags);
7145
7146 mutex_lock(&priv->mutex);
Tomas Winkler864792e2007-11-27 21:00:52 +02007147
7148 if (conf->mac_addr) {
7149 IWL_DEBUG_MAC80211("Set %s\n", print_mac(mac, conf->mac_addr));
7150 memcpy(priv->mac_addr, conf->mac_addr, ETH_ALEN);
7151 }
Zhu Yib481de92007-09-25 17:54:57 -07007152
Zhu Yi5a66926a2008-01-14 17:46:18 -08007153 if (iwl4965_is_ready(priv))
7154 iwl4965_set_mode(priv, conf->type);
7155
Zhu Yib481de92007-09-25 17:54:57 -07007156 mutex_unlock(&priv->mutex);
7157
Zhu Yi5a66926a2008-01-14 17:46:18 -08007158 IWL_DEBUG_MAC80211("leave\n");
Zhu Yib481de92007-09-25 17:54:57 -07007159 return 0;
7160}
7161
7162/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007163 * iwl4965_mac_config - mac80211 config callback
Zhu Yib481de92007-09-25 17:54:57 -07007164 *
7165 * We ignore conf->flags & IEEE80211_CONF_SHORT_SLOT_TIME since it seems to
7166 * be set inappropriately and the driver currently sets the hardware up to
7167 * use it whenever needed.
7168 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007169static int iwl4965_mac_config(struct ieee80211_hw *hw, struct ieee80211_conf *conf)
Zhu Yib481de92007-09-25 17:54:57 -07007170{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007171 struct iwl_priv *priv = hw->priv;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007172 const struct iwl4965_channel_info *ch_info;
Zhu Yib481de92007-09-25 17:54:57 -07007173 unsigned long flags;
Zhu Yi76bb77e2007-11-22 10:53:22 +08007174 int ret = 0;
Zhu Yib481de92007-09-25 17:54:57 -07007175
7176 mutex_lock(&priv->mutex);
Johannes Berg8318d782008-01-24 19:38:38 +01007177 IWL_DEBUG_MAC80211("enter to channel %d\n", conf->channel->hw_value);
Zhu Yib481de92007-09-25 17:54:57 -07007178
Zhu Yi12342c42007-12-20 11:27:32 +08007179 priv->add_radiotap = !!(conf->flags & IEEE80211_CONF_RADIOTAP);
7180
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007181 if (!iwl4965_is_ready(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07007182 IWL_DEBUG_MAC80211("leave - not ready\n");
Zhu Yi76bb77e2007-11-22 10:53:22 +08007183 ret = -EIO;
7184 goto out;
Zhu Yib481de92007-09-25 17:54:57 -07007185 }
7186
Assaf Krauss00acbc92008-03-14 10:38:47 -07007187 if (unlikely(!iwl4965_mod_params.disable_hw_scan &&
Zhu Yib481de92007-09-25 17:54:57 -07007188 test_bit(STATUS_SCANNING, &priv->status))) {
Zhu Yia0646472007-12-20 14:10:01 +08007189 IWL_DEBUG_MAC80211("leave - scanning\n");
7190 set_bit(STATUS_CONF_PENDING, &priv->status);
Zhu Yib481de92007-09-25 17:54:57 -07007191 mutex_unlock(&priv->mutex);
Zhu Yia0646472007-12-20 14:10:01 +08007192 return 0;
Zhu Yib481de92007-09-25 17:54:57 -07007193 }
7194
7195 spin_lock_irqsave(&priv->lock, flags);
7196
Johannes Berg8318d782008-01-24 19:38:38 +01007197 ch_info = iwl4965_get_channel_info(priv, conf->channel->band,
7198 ieee80211_frequency_to_channel(conf->channel->center_freq));
Zhu Yib481de92007-09-25 17:54:57 -07007199 if (!is_channel_valid(ch_info)) {
Zhu Yib481de92007-09-25 17:54:57 -07007200 IWL_DEBUG_MAC80211("leave - invalid channel\n");
7201 spin_unlock_irqrestore(&priv->lock, flags);
Zhu Yi76bb77e2007-11-22 10:53:22 +08007202 ret = -EINVAL;
7203 goto out;
Zhu Yib481de92007-09-25 17:54:57 -07007204 }
7205
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08007206#ifdef CONFIG_IWL4965_HT
Tomas Winkler78330fd2008-02-06 02:37:18 +02007207 /* if we are switching from ht to 2.4 clear flags
Zhu Yib481de92007-09-25 17:54:57 -07007208 * from any ht related info since 2.4 does not
7209 * support ht */
Tomas Winkler78330fd2008-02-06 02:37:18 +02007210 if ((le16_to_cpu(priv->staging_rxon.channel) != conf->channel->hw_value)
Zhu Yib481de92007-09-25 17:54:57 -07007211#ifdef IEEE80211_CONF_CHANNEL_SWITCH
7212 && !(conf->flags & IEEE80211_CONF_CHANNEL_SWITCH)
7213#endif
7214 )
7215 priv->staging_rxon.flags = 0;
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08007216#endif /* CONFIG_IWL4965_HT */
Zhu Yib481de92007-09-25 17:54:57 -07007217
Johannes Berg8318d782008-01-24 19:38:38 +01007218 iwl4965_set_rxon_channel(priv, conf->channel->band,
7219 ieee80211_frequency_to_channel(conf->channel->center_freq));
Zhu Yib481de92007-09-25 17:54:57 -07007220
Johannes Berg8318d782008-01-24 19:38:38 +01007221 iwl4965_set_flags_for_phymode(priv, conf->channel->band);
Zhu Yib481de92007-09-25 17:54:57 -07007222
7223 /* The list of supported rates and rate mask can be different
Johannes Berg8318d782008-01-24 19:38:38 +01007224 * for each band; since the band may have changed, reset
Zhu Yib481de92007-09-25 17:54:57 -07007225 * the rate mask to what mac80211 lists */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007226 iwl4965_set_rate(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007227
7228 spin_unlock_irqrestore(&priv->lock, flags);
7229
7230#ifdef IEEE80211_CONF_CHANNEL_SWITCH
7231 if (conf->flags & IEEE80211_CONF_CHANNEL_SWITCH) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007232 iwl4965_hw_channel_switch(priv, conf->channel);
Zhu Yi76bb77e2007-11-22 10:53:22 +08007233 goto out;
Zhu Yib481de92007-09-25 17:54:57 -07007234 }
7235#endif
7236
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007237 iwl4965_radio_kill_sw(priv, !conf->radio_enabled);
Zhu Yib481de92007-09-25 17:54:57 -07007238
7239 if (!conf->radio_enabled) {
7240 IWL_DEBUG_MAC80211("leave - radio disabled\n");
Zhu Yi76bb77e2007-11-22 10:53:22 +08007241 goto out;
Zhu Yib481de92007-09-25 17:54:57 -07007242 }
7243
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007244 if (iwl4965_is_rfkill(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07007245 IWL_DEBUG_MAC80211("leave - RF kill\n");
Zhu Yi76bb77e2007-11-22 10:53:22 +08007246 ret = -EIO;
7247 goto out;
Zhu Yib481de92007-09-25 17:54:57 -07007248 }
7249
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007250 iwl4965_set_rate(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007251
7252 if (memcmp(&priv->active_rxon,
7253 &priv->staging_rxon, sizeof(priv->staging_rxon)))
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007254 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007255 else
7256 IWL_DEBUG_INFO("No re-sending same RXON configuration.\n");
7257
7258 IWL_DEBUG_MAC80211("leave\n");
7259
Zhu Yia0646472007-12-20 14:10:01 +08007260out:
7261 clear_bit(STATUS_CONF_PENDING, &priv->status);
Zhu Yi5a66926a2008-01-14 17:46:18 -08007262 mutex_unlock(&priv->mutex);
Zhu Yi76bb77e2007-11-22 10:53:22 +08007263 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07007264}
7265
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007266static void iwl4965_config_ap(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07007267{
7268 int rc = 0;
7269
Maarten Lankhorstd986bcd2008-01-23 10:15:16 -08007270 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
Zhu Yib481de92007-09-25 17:54:57 -07007271 return;
7272
7273 /* The following should be done only at AP bring up */
7274 if ((priv->active_rxon.filter_flags & RXON_FILTER_ASSOC_MSK) == 0) {
7275
7276 /* RXON - unassoc (to set timing command) */
7277 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007278 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007279
7280 /* RXON Timing */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007281 memset(&priv->rxon_timing, 0, sizeof(struct iwl4965_rxon_time_cmd));
7282 iwl4965_setup_rxon_timing(priv);
7283 rc = iwl4965_send_cmd_pdu(priv, REPLY_RXON_TIMING,
Zhu Yib481de92007-09-25 17:54:57 -07007284 sizeof(priv->rxon_timing), &priv->rxon_timing);
7285 if (rc)
7286 IWL_WARNING("REPLY_RXON_TIMING failed - "
7287 "Attempting to continue.\n");
7288
7289 iwl4965_set_rxon_chain(priv);
7290
7291 /* FIXME: what should be the assoc_id for AP? */
7292 priv->staging_rxon.assoc_id = cpu_to_le16(priv->assoc_id);
7293 if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_PREAMBLE)
7294 priv->staging_rxon.flags |=
7295 RXON_FLG_SHORT_PREAMBLE_MSK;
7296 else
7297 priv->staging_rxon.flags &=
7298 ~RXON_FLG_SHORT_PREAMBLE_MSK;
7299
7300 if (priv->staging_rxon.flags & RXON_FLG_BAND_24G_MSK) {
7301 if (priv->assoc_capability &
7302 WLAN_CAPABILITY_SHORT_SLOT_TIME)
7303 priv->staging_rxon.flags |=
7304 RXON_FLG_SHORT_SLOT_MSK;
7305 else
7306 priv->staging_rxon.flags &=
7307 ~RXON_FLG_SHORT_SLOT_MSK;
7308
7309 if (priv->iw_mode == IEEE80211_IF_TYPE_IBSS)
7310 priv->staging_rxon.flags &=
7311 ~RXON_FLG_SHORT_SLOT_MSK;
7312 }
7313 /* restore RXON assoc */
7314 priv->staging_rxon.filter_flags |= RXON_FILTER_ASSOC_MSK;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007315 iwl4965_commit_rxon(priv);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007316 iwl4965_activate_qos(priv, 1);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007317 iwl4965_rxon_add_station(priv, iwl4965_broadcast_addr, 0);
Zhu Yie1493de2007-09-27 11:27:32 +08007318 }
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007319 iwl4965_send_beacon_cmd(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007320
7321 /* FIXME - we need to add code here to detect a totally new
7322 * configuration, reset the AP, unassoc, rxon timing, assoc,
7323 * clear sta table, add BCAST sta... */
7324}
7325
Johannes Berg32bfd352007-12-19 01:31:26 +01007326static int iwl4965_mac_config_interface(struct ieee80211_hw *hw,
7327 struct ieee80211_vif *vif,
Zhu Yib481de92007-09-25 17:54:57 -07007328 struct ieee80211_if_conf *conf)
7329{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007330 struct iwl_priv *priv = hw->priv;
Joe Perches0795af52007-10-03 17:59:30 -07007331 DECLARE_MAC_BUF(mac);
Zhu Yib481de92007-09-25 17:54:57 -07007332 unsigned long flags;
7333 int rc;
7334
7335 if (conf == NULL)
7336 return -EIO;
7337
Emmanuel Grumbachb716bb92008-03-04 18:09:32 -08007338 if (priv->vif != vif) {
7339 IWL_DEBUG_MAC80211("leave - priv->vif != vif\n");
7340 mutex_unlock(&priv->mutex);
7341 return 0;
7342 }
7343
Zhu Yib481de92007-09-25 17:54:57 -07007344 if ((priv->iw_mode == IEEE80211_IF_TYPE_AP) &&
7345 (!conf->beacon || !conf->ssid_len)) {
7346 IWL_DEBUG_MAC80211
7347 ("Leaving in AP mode because HostAPD is not ready.\n");
7348 return 0;
7349 }
7350
Zhu Yi5a66926a2008-01-14 17:46:18 -08007351 if (!iwl4965_is_alive(priv))
7352 return -EAGAIN;
7353
Zhu Yib481de92007-09-25 17:54:57 -07007354 mutex_lock(&priv->mutex);
7355
Zhu Yib481de92007-09-25 17:54:57 -07007356 if (conf->bssid)
Joe Perches0795af52007-10-03 17:59:30 -07007357 IWL_DEBUG_MAC80211("bssid: %s\n",
7358 print_mac(mac, conf->bssid));
Zhu Yib481de92007-09-25 17:54:57 -07007359
Johannes Berg4150c572007-09-17 01:29:23 -04007360/*
7361 * very dubious code was here; the probe filtering flag is never set:
7362 *
Zhu Yib481de92007-09-25 17:54:57 -07007363 if (unlikely(test_bit(STATUS_SCANNING, &priv->status)) &&
7364 !(priv->hw->flags & IEEE80211_HW_NO_PROBE_FILTERING)) {
Johannes Berg4150c572007-09-17 01:29:23 -04007365 */
Zhu Yib481de92007-09-25 17:54:57 -07007366
7367 if (priv->iw_mode == IEEE80211_IF_TYPE_AP) {
7368 if (!conf->bssid) {
7369 conf->bssid = priv->mac_addr;
7370 memcpy(priv->bssid, priv->mac_addr, ETH_ALEN);
Joe Perches0795af52007-10-03 17:59:30 -07007371 IWL_DEBUG_MAC80211("bssid was set to: %s\n",
7372 print_mac(mac, conf->bssid));
Zhu Yib481de92007-09-25 17:54:57 -07007373 }
7374 if (priv->ibss_beacon)
7375 dev_kfree_skb(priv->ibss_beacon);
7376
7377 priv->ibss_beacon = conf->beacon;
7378 }
7379
Mohamed Abbasfde35712007-11-29 11:10:15 +08007380 if (iwl4965_is_rfkill(priv))
7381 goto done;
7382
Zhu Yib481de92007-09-25 17:54:57 -07007383 if (conf->bssid && !is_zero_ether_addr(conf->bssid) &&
7384 !is_multicast_ether_addr(conf->bssid)) {
7385 /* If there is currently a HW scan going on in the background
7386 * then we need to cancel it else the RXON below will fail. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007387 if (iwl4965_scan_cancel_timeout(priv, 100)) {
Zhu Yib481de92007-09-25 17:54:57 -07007388 IWL_WARNING("Aborted scan still in progress "
7389 "after 100ms\n");
7390 IWL_DEBUG_MAC80211("leaving - scan abort failed.\n");
7391 mutex_unlock(&priv->mutex);
7392 return -EAGAIN;
7393 }
7394 memcpy(priv->staging_rxon.bssid_addr, conf->bssid, ETH_ALEN);
7395
7396 /* TODO: Audit driver for usage of these members and see
7397 * if mac80211 deprecates them (priv->bssid looks like it
7398 * shouldn't be there, but I haven't scanned the IBSS code
7399 * to verify) - jpk */
7400 memcpy(priv->bssid, conf->bssid, ETH_ALEN);
7401
7402 if (priv->iw_mode == IEEE80211_IF_TYPE_AP)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007403 iwl4965_config_ap(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007404 else {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007405 rc = iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007406 if ((priv->iw_mode == IEEE80211_IF_TYPE_STA) && rc)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007407 iwl4965_rxon_add_station(
Zhu Yib481de92007-09-25 17:54:57 -07007408 priv, priv->active_rxon.bssid_addr, 1);
7409 }
7410
7411 } else {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007412 iwl4965_scan_cancel_timeout(priv, 100);
Zhu Yib481de92007-09-25 17:54:57 -07007413 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007414 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007415 }
7416
Mohamed Abbasfde35712007-11-29 11:10:15 +08007417 done:
Zhu Yib481de92007-09-25 17:54:57 -07007418 spin_lock_irqsave(&priv->lock, flags);
7419 if (!conf->ssid_len)
7420 memset(priv->essid, 0, IW_ESSID_MAX_SIZE);
7421 else
7422 memcpy(priv->essid, conf->ssid, conf->ssid_len);
7423
7424 priv->essid_len = conf->ssid_len;
7425 spin_unlock_irqrestore(&priv->lock, flags);
7426
7427 IWL_DEBUG_MAC80211("leave\n");
7428 mutex_unlock(&priv->mutex);
7429
7430 return 0;
7431}
7432
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007433static void iwl4965_configure_filter(struct ieee80211_hw *hw,
Johannes Berg4150c572007-09-17 01:29:23 -04007434 unsigned int changed_flags,
7435 unsigned int *total_flags,
7436 int mc_count, struct dev_addr_list *mc_list)
7437{
7438 /*
7439 * XXX: dummy
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007440 * see also iwl4965_connection_init_rx_config
Johannes Berg4150c572007-09-17 01:29:23 -04007441 */
7442 *total_flags = 0;
7443}
7444
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007445static void iwl4965_mac_remove_interface(struct ieee80211_hw *hw,
Zhu Yib481de92007-09-25 17:54:57 -07007446 struct ieee80211_if_init_conf *conf)
7447{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007448 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07007449
7450 IWL_DEBUG_MAC80211("enter\n");
7451
7452 mutex_lock(&priv->mutex);
Mohamed Abbas948c1712007-10-25 17:15:45 +08007453
Mohamed Abbasfde35712007-11-29 11:10:15 +08007454 if (iwl4965_is_ready_rf(priv)) {
7455 iwl4965_scan_cancel_timeout(priv, 100);
7456 cancel_delayed_work(&priv->post_associate);
7457 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
7458 iwl4965_commit_rxon(priv);
7459 }
Johannes Berg32bfd352007-12-19 01:31:26 +01007460 if (priv->vif == conf->vif) {
7461 priv->vif = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07007462 memset(priv->bssid, 0, ETH_ALEN);
7463 memset(priv->essid, 0, IW_ESSID_MAX_SIZE);
7464 priv->essid_len = 0;
7465 }
7466 mutex_unlock(&priv->mutex);
7467
7468 IWL_DEBUG_MAC80211("leave\n");
7469
7470}
Johannes Berg471b3ef2007-12-28 14:32:58 +01007471
7472static void iwl4965_bss_info_changed(struct ieee80211_hw *hw,
7473 struct ieee80211_vif *vif,
7474 struct ieee80211_bss_conf *bss_conf,
7475 u32 changes)
Tomas Winkler220173b2007-10-16 00:50:25 +02007476{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007477 struct iwl_priv *priv = hw->priv;
Tomas Winkler220173b2007-10-16 00:50:25 +02007478
Johannes Berg471b3ef2007-12-28 14:32:58 +01007479 if (changes & BSS_CHANGED_ERP_PREAMBLE) {
7480 if (bss_conf->use_short_preamble)
Tomas Winkler220173b2007-10-16 00:50:25 +02007481 priv->staging_rxon.flags |= RXON_FLG_SHORT_PREAMBLE_MSK;
7482 else
7483 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_PREAMBLE_MSK;
7484 }
7485
Johannes Berg471b3ef2007-12-28 14:32:58 +01007486 if (changes & BSS_CHANGED_ERP_CTS_PROT) {
Johannes Berg8318d782008-01-24 19:38:38 +01007487 if (bss_conf->use_cts_prot && (priv->band != IEEE80211_BAND_5GHZ))
Tomas Winkler220173b2007-10-16 00:50:25 +02007488 priv->staging_rxon.flags |= RXON_FLG_TGG_PROTECT_MSK;
7489 else
7490 priv->staging_rxon.flags &= ~RXON_FLG_TGG_PROTECT_MSK;
7491 }
7492
Johannes Berg471b3ef2007-12-28 14:32:58 +01007493 if (changes & BSS_CHANGED_ASSOC) {
7494 /*
7495 * TODO:
7496 * do stuff instead of sniffing assoc resp
7497 */
7498 }
7499
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007500 if (iwl4965_is_associated(priv))
7501 iwl4965_send_rxon_assoc(priv);
Tomas Winkler220173b2007-10-16 00:50:25 +02007502}
Zhu Yib481de92007-09-25 17:54:57 -07007503
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007504static int iwl4965_mac_hw_scan(struct ieee80211_hw *hw, u8 *ssid, size_t len)
Zhu Yib481de92007-09-25 17:54:57 -07007505{
7506 int rc = 0;
7507 unsigned long flags;
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007508 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07007509
7510 IWL_DEBUG_MAC80211("enter\n");
7511
mabbas052c4b92007-10-25 17:15:43 +08007512 mutex_lock(&priv->mutex);
Zhu Yib481de92007-09-25 17:54:57 -07007513 spin_lock_irqsave(&priv->lock, flags);
7514
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007515 if (!iwl4965_is_ready_rf(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07007516 rc = -EIO;
7517 IWL_DEBUG_MAC80211("leave - not ready or exit pending\n");
7518 goto out_unlock;
7519 }
7520
7521 if (priv->iw_mode == IEEE80211_IF_TYPE_AP) { /* APs don't scan */
7522 rc = -EIO;
7523 IWL_ERROR("ERROR: APs don't scan\n");
7524 goto out_unlock;
7525 }
7526
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08007527 /* we don't schedule scan within next_scan_jiffies period */
7528 if (priv->next_scan_jiffies &&
7529 time_after(priv->next_scan_jiffies, jiffies)) {
7530 rc = -EAGAIN;
7531 goto out_unlock;
7532 }
Zhu Yib481de92007-09-25 17:54:57 -07007533 /* if we just finished scan ask for delay */
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08007534 if (priv->last_scan_jiffies && time_after(priv->last_scan_jiffies +
7535 IWL_DELAY_NEXT_SCAN, jiffies)) {
Zhu Yib481de92007-09-25 17:54:57 -07007536 rc = -EAGAIN;
7537 goto out_unlock;
7538 }
7539 if (len) {
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08007540 IWL_DEBUG_SCAN("direct scan for %s [%d]\n ",
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007541 iwl4965_escape_essid(ssid, len), (int)len);
Zhu Yib481de92007-09-25 17:54:57 -07007542
7543 priv->one_direct_scan = 1;
7544 priv->direct_ssid_len = (u8)
7545 min((u8) len, (u8) IW_ESSID_MAX_SIZE);
7546 memcpy(priv->direct_ssid, ssid, priv->direct_ssid_len);
Mohamed Abbas948c1712007-10-25 17:15:45 +08007547 } else
7548 priv->one_direct_scan = 0;
Zhu Yib481de92007-09-25 17:54:57 -07007549
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007550 rc = iwl4965_scan_initiate(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007551
7552 IWL_DEBUG_MAC80211("leave\n");
7553
7554out_unlock:
7555 spin_unlock_irqrestore(&priv->lock, flags);
mabbas052c4b92007-10-25 17:15:43 +08007556 mutex_unlock(&priv->mutex);
Zhu Yib481de92007-09-25 17:54:57 -07007557
7558 return rc;
7559}
7560
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007561static int iwl4965_mac_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
Zhu Yib481de92007-09-25 17:54:57 -07007562 const u8 *local_addr, const u8 *addr,
7563 struct ieee80211_key_conf *key)
7564{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007565 struct iwl_priv *priv = hw->priv;
Joe Perches0795af52007-10-03 17:59:30 -07007566 DECLARE_MAC_BUF(mac);
Zhu Yib481de92007-09-25 17:54:57 -07007567 int rc = 0;
7568 u8 sta_id;
7569
7570 IWL_DEBUG_MAC80211("enter\n");
7571
Assaf Krauss00acbc92008-03-14 10:38:47 -07007572 if (!iwl4965_mod_params.hw_crypto) {
Zhu Yib481de92007-09-25 17:54:57 -07007573 IWL_DEBUG_MAC80211("leave - hwcrypto disabled\n");
7574 return -EOPNOTSUPP;
7575 }
7576
7577 if (is_zero_ether_addr(addr))
7578 /* only support pairwise keys */
7579 return -EOPNOTSUPP;
7580
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007581 sta_id = iwl4965_hw_find_station(priv, addr);
Zhu Yib481de92007-09-25 17:54:57 -07007582 if (sta_id == IWL_INVALID_STATION) {
Joe Perches0795af52007-10-03 17:59:30 -07007583 IWL_DEBUG_MAC80211("leave - %s not in station map.\n",
7584 print_mac(mac, addr));
Zhu Yib481de92007-09-25 17:54:57 -07007585 return -EINVAL;
7586 }
7587
7588 mutex_lock(&priv->mutex);
7589
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007590 iwl4965_scan_cancel_timeout(priv, 100);
mabbas052c4b92007-10-25 17:15:43 +08007591
Zhu Yib481de92007-09-25 17:54:57 -07007592 switch (cmd) {
7593 case SET_KEY:
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007594 rc = iwl4965_update_sta_key_info(priv, key, sta_id);
Zhu Yib481de92007-09-25 17:54:57 -07007595 if (!rc) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007596 iwl4965_set_rxon_hwcrypto(priv, 1);
7597 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007598 key->hw_key_idx = sta_id;
7599 IWL_DEBUG_MAC80211("set_key success, using hwcrypto\n");
7600 key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
7601 }
7602 break;
7603 case DISABLE_KEY:
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007604 rc = iwl4965_clear_sta_key_info(priv, sta_id);
Zhu Yib481de92007-09-25 17:54:57 -07007605 if (!rc) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007606 iwl4965_set_rxon_hwcrypto(priv, 0);
7607 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007608 IWL_DEBUG_MAC80211("disable hwcrypto key\n");
7609 }
7610 break;
7611 default:
7612 rc = -EINVAL;
7613 }
7614
7615 IWL_DEBUG_MAC80211("leave\n");
7616 mutex_unlock(&priv->mutex);
7617
7618 return rc;
7619}
7620
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007621static int iwl4965_mac_conf_tx(struct ieee80211_hw *hw, int queue,
Zhu Yib481de92007-09-25 17:54:57 -07007622 const struct ieee80211_tx_queue_params *params)
7623{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007624 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07007625 unsigned long flags;
7626 int q;
Zhu Yib481de92007-09-25 17:54:57 -07007627
7628 IWL_DEBUG_MAC80211("enter\n");
7629
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007630 if (!iwl4965_is_ready_rf(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07007631 IWL_DEBUG_MAC80211("leave - RF not ready\n");
7632 return -EIO;
7633 }
7634
7635 if (queue >= AC_NUM) {
7636 IWL_DEBUG_MAC80211("leave - queue >= AC_NUM %d\n", queue);
7637 return 0;
7638 }
7639
Zhu Yib481de92007-09-25 17:54:57 -07007640 if (!priv->qos_data.qos_enable) {
7641 priv->qos_data.qos_active = 0;
7642 IWL_DEBUG_MAC80211("leave - qos not enabled\n");
7643 return 0;
7644 }
7645 q = AC_NUM - 1 - queue;
7646
7647 spin_lock_irqsave(&priv->lock, flags);
7648
7649 priv->qos_data.def_qos_parm.ac[q].cw_min = cpu_to_le16(params->cw_min);
7650 priv->qos_data.def_qos_parm.ac[q].cw_max = cpu_to_le16(params->cw_max);
7651 priv->qos_data.def_qos_parm.ac[q].aifsn = params->aifs;
7652 priv->qos_data.def_qos_parm.ac[q].edca_txop =
Johannes Berg3330d7b2008-02-10 16:49:38 +01007653 cpu_to_le16((params->txop * 32));
Zhu Yib481de92007-09-25 17:54:57 -07007654
7655 priv->qos_data.def_qos_parm.ac[q].reserved1 = 0;
7656 priv->qos_data.qos_active = 1;
7657
7658 spin_unlock_irqrestore(&priv->lock, flags);
7659
7660 mutex_lock(&priv->mutex);
7661 if (priv->iw_mode == IEEE80211_IF_TYPE_AP)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007662 iwl4965_activate_qos(priv, 1);
7663 else if (priv->assoc_id && iwl4965_is_associated(priv))
7664 iwl4965_activate_qos(priv, 0);
Zhu Yib481de92007-09-25 17:54:57 -07007665
7666 mutex_unlock(&priv->mutex);
7667
Zhu Yib481de92007-09-25 17:54:57 -07007668 IWL_DEBUG_MAC80211("leave\n");
7669 return 0;
7670}
7671
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007672static int iwl4965_mac_get_tx_stats(struct ieee80211_hw *hw,
Zhu Yib481de92007-09-25 17:54:57 -07007673 struct ieee80211_tx_queue_stats *stats)
7674{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007675 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07007676 int i, avail;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007677 struct iwl4965_tx_queue *txq;
7678 struct iwl4965_queue *q;
Zhu Yib481de92007-09-25 17:54:57 -07007679 unsigned long flags;
7680
7681 IWL_DEBUG_MAC80211("enter\n");
7682
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007683 if (!iwl4965_is_ready_rf(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07007684 IWL_DEBUG_MAC80211("leave - RF not ready\n");
7685 return -EIO;
7686 }
7687
7688 spin_lock_irqsave(&priv->lock, flags);
7689
7690 for (i = 0; i < AC_NUM; i++) {
7691 txq = &priv->txq[i];
7692 q = &txq->q;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007693 avail = iwl4965_queue_space(q);
Zhu Yib481de92007-09-25 17:54:57 -07007694
7695 stats->data[i].len = q->n_window - avail;
7696 stats->data[i].limit = q->n_window - q->high_mark;
7697 stats->data[i].count = q->n_window;
7698
7699 }
7700 spin_unlock_irqrestore(&priv->lock, flags);
7701
7702 IWL_DEBUG_MAC80211("leave\n");
7703
7704 return 0;
7705}
7706
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007707static int iwl4965_mac_get_stats(struct ieee80211_hw *hw,
Zhu Yib481de92007-09-25 17:54:57 -07007708 struct ieee80211_low_level_stats *stats)
7709{
7710 IWL_DEBUG_MAC80211("enter\n");
7711 IWL_DEBUG_MAC80211("leave\n");
7712
7713 return 0;
7714}
7715
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007716static u64 iwl4965_mac_get_tsf(struct ieee80211_hw *hw)
Zhu Yib481de92007-09-25 17:54:57 -07007717{
7718 IWL_DEBUG_MAC80211("enter\n");
7719 IWL_DEBUG_MAC80211("leave\n");
7720
7721 return 0;
7722}
7723
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007724static void iwl4965_mac_reset_tsf(struct ieee80211_hw *hw)
Zhu Yib481de92007-09-25 17:54:57 -07007725{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007726 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07007727 unsigned long flags;
7728
7729 mutex_lock(&priv->mutex);
7730 IWL_DEBUG_MAC80211("enter\n");
7731
7732 priv->lq_mngr.lq_ready = 0;
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08007733#ifdef CONFIG_IWL4965_HT
Zhu Yib481de92007-09-25 17:54:57 -07007734 spin_lock_irqsave(&priv->lock, flags);
Ron Rindjunskyfd105e72007-11-26 16:14:39 +02007735 memset(&priv->current_ht_config, 0, sizeof(struct iwl_ht_info));
Zhu Yib481de92007-09-25 17:54:57 -07007736 spin_unlock_irqrestore(&priv->lock, flags);
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08007737#endif /* CONFIG_IWL4965_HT */
Zhu Yib481de92007-09-25 17:54:57 -07007738
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007739 iwl4965_reset_qos(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007740
7741 cancel_delayed_work(&priv->post_associate);
7742
7743 spin_lock_irqsave(&priv->lock, flags);
7744 priv->assoc_id = 0;
7745 priv->assoc_capability = 0;
7746 priv->call_post_assoc_from_beacon = 0;
7747 priv->assoc_station_added = 0;
7748
7749 /* new association get rid of ibss beacon skb */
7750 if (priv->ibss_beacon)
7751 dev_kfree_skb(priv->ibss_beacon);
7752
7753 priv->ibss_beacon = NULL;
7754
7755 priv->beacon_int = priv->hw->conf.beacon_int;
7756 priv->timestamp1 = 0;
7757 priv->timestamp0 = 0;
7758 if ((priv->iw_mode == IEEE80211_IF_TYPE_STA))
7759 priv->beacon_int = 0;
7760
7761 spin_unlock_irqrestore(&priv->lock, flags);
7762
Mohamed Abbasfde35712007-11-29 11:10:15 +08007763 if (!iwl4965_is_ready_rf(priv)) {
7764 IWL_DEBUG_MAC80211("leave - not ready\n");
7765 mutex_unlock(&priv->mutex);
7766 return;
7767 }
7768
mabbas052c4b92007-10-25 17:15:43 +08007769 /* we are restarting association process
7770 * clear RXON_FILTER_ASSOC_MSK bit
7771 */
7772 if (priv->iw_mode != IEEE80211_IF_TYPE_AP) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007773 iwl4965_scan_cancel_timeout(priv, 100);
mabbas052c4b92007-10-25 17:15:43 +08007774 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007775 iwl4965_commit_rxon(priv);
mabbas052c4b92007-10-25 17:15:43 +08007776 }
7777
Zhu Yib481de92007-09-25 17:54:57 -07007778 /* Per mac80211.h: This is only used in IBSS mode... */
7779 if (priv->iw_mode != IEEE80211_IF_TYPE_IBSS) {
mabbas052c4b92007-10-25 17:15:43 +08007780
Zhu Yib481de92007-09-25 17:54:57 -07007781 IWL_DEBUG_MAC80211("leave - not in IBSS\n");
7782 mutex_unlock(&priv->mutex);
7783 return;
7784 }
7785
Zhu Yib481de92007-09-25 17:54:57 -07007786 priv->only_active_channel = 0;
7787
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007788 iwl4965_set_rate(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007789
7790 mutex_unlock(&priv->mutex);
7791
7792 IWL_DEBUG_MAC80211("leave\n");
Zhu Yib481de92007-09-25 17:54:57 -07007793}
7794
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007795static int iwl4965_mac_beacon_update(struct ieee80211_hw *hw, struct sk_buff *skb,
Zhu Yib481de92007-09-25 17:54:57 -07007796 struct ieee80211_tx_control *control)
7797{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007798 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07007799 unsigned long flags;
7800
7801 mutex_lock(&priv->mutex);
7802 IWL_DEBUG_MAC80211("enter\n");
7803
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007804 if (!iwl4965_is_ready_rf(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07007805 IWL_DEBUG_MAC80211("leave - RF not ready\n");
7806 mutex_unlock(&priv->mutex);
7807 return -EIO;
7808 }
7809
7810 if (priv->iw_mode != IEEE80211_IF_TYPE_IBSS) {
7811 IWL_DEBUG_MAC80211("leave - not IBSS\n");
7812 mutex_unlock(&priv->mutex);
7813 return -EIO;
7814 }
7815
7816 spin_lock_irqsave(&priv->lock, flags);
7817
7818 if (priv->ibss_beacon)
7819 dev_kfree_skb(priv->ibss_beacon);
7820
7821 priv->ibss_beacon = skb;
7822
7823 priv->assoc_id = 0;
7824
7825 IWL_DEBUG_MAC80211("leave\n");
7826 spin_unlock_irqrestore(&priv->lock, flags);
7827
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007828 iwl4965_reset_qos(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007829
7830 queue_work(priv->workqueue, &priv->post_associate.work);
7831
7832 mutex_unlock(&priv->mutex);
7833
7834 return 0;
7835}
7836
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08007837#ifdef CONFIG_IWL4965_HT
Zhu Yib481de92007-09-25 17:54:57 -07007838
Ron Rindjunskyfd105e72007-11-26 16:14:39 +02007839static void iwl4965_ht_info_fill(struct ieee80211_conf *conf,
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007840 struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07007841{
Ron Rindjunskyfd105e72007-11-26 16:14:39 +02007842 struct iwl_ht_info *iwl_conf = &priv->current_ht_config;
7843 struct ieee80211_ht_info *ht_conf = &conf->ht_conf;
7844 struct ieee80211_ht_bss_info *ht_bss_conf = &conf->ht_bss_conf;
Zhu Yib481de92007-09-25 17:54:57 -07007845
7846 IWL_DEBUG_MAC80211("enter: \n");
7847
Ron Rindjunskyfd105e72007-11-26 16:14:39 +02007848 if (!(conf->flags & IEEE80211_CONF_SUPPORT_HT_MODE)) {
7849 iwl_conf->is_ht = 0;
7850 return;
Zhu Yib481de92007-09-25 17:54:57 -07007851 }
7852
Ron Rindjunskyfd105e72007-11-26 16:14:39 +02007853 iwl_conf->is_ht = 1;
7854 priv->ps_mode = (u8)((ht_conf->cap & IEEE80211_HT_CAP_MIMO_PS) >> 2);
Zhu Yib481de92007-09-25 17:54:57 -07007855
Ron Rindjunskyfd105e72007-11-26 16:14:39 +02007856 if (ht_conf->cap & IEEE80211_HT_CAP_SGI_20)
7857 iwl_conf->sgf |= 0x1;
7858 if (ht_conf->cap & IEEE80211_HT_CAP_SGI_40)
7859 iwl_conf->sgf |= 0x2;
Zhu Yib481de92007-09-25 17:54:57 -07007860
Ron Rindjunskyfd105e72007-11-26 16:14:39 +02007861 iwl_conf->is_green_field = !!(ht_conf->cap & IEEE80211_HT_CAP_GRN_FLD);
7862 iwl_conf->max_amsdu_size =
7863 !!(ht_conf->cap & IEEE80211_HT_CAP_MAX_AMSDU);
Guy Cohen134eb5d2008-03-04 18:09:25 -08007864
Ron Rindjunskyfd105e72007-11-26 16:14:39 +02007865 iwl_conf->supported_chan_width =
7866 !!(ht_conf->cap & IEEE80211_HT_CAP_SUP_WIDTH);
Guy Cohen134eb5d2008-03-04 18:09:25 -08007867 iwl_conf->extension_chan_offset =
7868 ht_bss_conf->bss_cap & IEEE80211_HT_IE_CHA_SEC_OFFSET;
7869 /* If no above or below channel supplied disable FAT channel */
7870 if (iwl_conf->extension_chan_offset != IWL_EXT_CHANNEL_OFFSET_ABOVE &&
7871 iwl_conf->extension_chan_offset != IWL_EXT_CHANNEL_OFFSET_BELOW)
7872 iwl_conf->supported_chan_width = 0;
7873
Ron Rindjunskyfd105e72007-11-26 16:14:39 +02007874 iwl_conf->tx_mimo_ps_mode =
7875 (u8)((ht_conf->cap & IEEE80211_HT_CAP_MIMO_PS) >> 2);
7876 memcpy(iwl_conf->supp_mcs_set, ht_conf->supp_mcs_set, 16);
Zhu Yib481de92007-09-25 17:54:57 -07007877
Ron Rindjunskyfd105e72007-11-26 16:14:39 +02007878 iwl_conf->control_channel = ht_bss_conf->primary_channel;
Ron Rindjunskyfd105e72007-11-26 16:14:39 +02007879 iwl_conf->tx_chan_width =
7880 !!(ht_bss_conf->bss_cap & IEEE80211_HT_IE_CHA_WIDTH);
7881 iwl_conf->ht_protection =
7882 ht_bss_conf->bss_op_mode & IEEE80211_HT_IE_HT_PROTECTION;
7883 iwl_conf->non_GF_STA_present =
7884 !!(ht_bss_conf->bss_op_mode & IEEE80211_HT_IE_NON_GF_STA_PRSNT);
Zhu Yib481de92007-09-25 17:54:57 -07007885
Ron Rindjunskyfd105e72007-11-26 16:14:39 +02007886 IWL_DEBUG_MAC80211("control channel %d\n",
7887 iwl_conf->control_channel);
Zhu Yib481de92007-09-25 17:54:57 -07007888 IWL_DEBUG_MAC80211("leave\n");
Zhu Yib481de92007-09-25 17:54:57 -07007889}
7890
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007891static int iwl4965_mac_conf_ht(struct ieee80211_hw *hw,
Ron Rindjunskyfd105e72007-11-26 16:14:39 +02007892 struct ieee80211_conf *conf)
Zhu Yib481de92007-09-25 17:54:57 -07007893{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007894 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07007895
7896 IWL_DEBUG_MAC80211("enter: \n");
7897
Ron Rindjunskyfd105e72007-11-26 16:14:39 +02007898 iwl4965_ht_info_fill(conf, priv);
Zhu Yib481de92007-09-25 17:54:57 -07007899 iwl4965_set_rxon_chain(priv);
7900
7901 if (priv && priv->assoc_id &&
7902 (priv->iw_mode == IEEE80211_IF_TYPE_STA)) {
7903 unsigned long flags;
7904
7905 spin_lock_irqsave(&priv->lock, flags);
7906 if (priv->beacon_int)
7907 queue_work(priv->workqueue, &priv->post_associate.work);
7908 else
7909 priv->call_post_assoc_from_beacon = 1;
7910 spin_unlock_irqrestore(&priv->lock, flags);
7911 }
7912
Ron Rindjunskyfd105e72007-11-26 16:14:39 +02007913 IWL_DEBUG_MAC80211("leave:\n");
7914 return 0;
Zhu Yib481de92007-09-25 17:54:57 -07007915}
7916
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08007917#endif /*CONFIG_IWL4965_HT*/
Zhu Yib481de92007-09-25 17:54:57 -07007918
7919/*****************************************************************************
7920 *
7921 * sysfs attributes
7922 *
7923 *****************************************************************************/
7924
Tomas Winkler0a6857e2008-03-12 16:58:49 -07007925#ifdef CONFIG_IWLWIFI_DEBUG
Zhu Yib481de92007-09-25 17:54:57 -07007926
7927/*
7928 * The following adds a new attribute to the sysfs representation
7929 * of this device driver (i.e. a new file in /sys/bus/pci/drivers/iwl/)
7930 * used for controlling the debug level.
7931 *
7932 * See the level definitions in iwl for details.
7933 */
7934
7935static ssize_t show_debug_level(struct device_driver *d, char *buf)
7936{
Tomas Winkler0a6857e2008-03-12 16:58:49 -07007937 return sprintf(buf, "0x%08X\n", iwl_debug_level);
Zhu Yib481de92007-09-25 17:54:57 -07007938}
7939static ssize_t store_debug_level(struct device_driver *d,
7940 const char *buf, size_t count)
7941{
7942 char *p = (char *)buf;
7943 u32 val;
7944
7945 val = simple_strtoul(p, &p, 0);
7946 if (p == buf)
7947 printk(KERN_INFO DRV_NAME
7948 ": %s is not in hex or decimal form.\n", buf);
7949 else
Tomas Winkler0a6857e2008-03-12 16:58:49 -07007950 iwl_debug_level = val;
Zhu Yib481de92007-09-25 17:54:57 -07007951
7952 return strnlen(buf, count);
7953}
7954
7955static DRIVER_ATTR(debug_level, S_IWUSR | S_IRUGO,
7956 show_debug_level, store_debug_level);
7957
Tomas Winkler0a6857e2008-03-12 16:58:49 -07007958#endif /* CONFIG_IWLWIFI_DEBUG */
Zhu Yib481de92007-09-25 17:54:57 -07007959
7960static ssize_t show_rf_kill(struct device *d,
7961 struct device_attribute *attr, char *buf)
7962{
7963 /*
7964 * 0 - RF kill not enabled
7965 * 1 - SW based RF kill active (sysfs)
7966 * 2 - HW based RF kill active
7967 * 3 - Both HW and SW based RF kill active
7968 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007969 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07007970 int val = (test_bit(STATUS_RF_KILL_SW, &priv->status) ? 0x1 : 0x0) |
7971 (test_bit(STATUS_RF_KILL_HW, &priv->status) ? 0x2 : 0x0);
7972
7973 return sprintf(buf, "%i\n", val);
7974}
7975
7976static ssize_t store_rf_kill(struct device *d,
7977 struct device_attribute *attr,
7978 const char *buf, size_t count)
7979{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007980 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07007981
7982 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007983 iwl4965_radio_kill_sw(priv, buf[0] == '1');
Zhu Yib481de92007-09-25 17:54:57 -07007984 mutex_unlock(&priv->mutex);
7985
7986 return count;
7987}
7988
7989static DEVICE_ATTR(rf_kill, S_IWUSR | S_IRUGO, show_rf_kill, store_rf_kill);
7990
7991static ssize_t show_temperature(struct device *d,
7992 struct device_attribute *attr, char *buf)
7993{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007994 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07007995
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007996 if (!iwl4965_is_alive(priv))
Zhu Yib481de92007-09-25 17:54:57 -07007997 return -EAGAIN;
7998
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007999 return sprintf(buf, "%d\n", iwl4965_hw_get_temperature(priv));
Zhu Yib481de92007-09-25 17:54:57 -07008000}
8001
8002static DEVICE_ATTR(temperature, S_IRUGO, show_temperature, NULL);
8003
8004static ssize_t show_rs_window(struct device *d,
8005 struct device_attribute *attr,
8006 char *buf)
8007{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07008008 struct iwl_priv *priv = d->driver_data;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008009 return iwl4965_fill_rs_info(priv->hw, buf, IWL_AP_ID);
Zhu Yib481de92007-09-25 17:54:57 -07008010}
8011static DEVICE_ATTR(rs_window, S_IRUGO, show_rs_window, NULL);
8012
8013static ssize_t show_tx_power(struct device *d,
8014 struct device_attribute *attr, char *buf)
8015{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07008016 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07008017 return sprintf(buf, "%d\n", priv->user_txpower_limit);
8018}
8019
8020static ssize_t store_tx_power(struct device *d,
8021 struct device_attribute *attr,
8022 const char *buf, size_t count)
8023{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07008024 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07008025 char *p = (char *)buf;
8026 u32 val;
8027
8028 val = simple_strtoul(p, &p, 10);
8029 if (p == buf)
8030 printk(KERN_INFO DRV_NAME
8031 ": %s is not in decimal form.\n", buf);
8032 else
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008033 iwl4965_hw_reg_set_txpower(priv, val);
Zhu Yib481de92007-09-25 17:54:57 -07008034
8035 return count;
8036}
8037
8038static DEVICE_ATTR(tx_power, S_IWUSR | S_IRUGO, show_tx_power, store_tx_power);
8039
8040static ssize_t show_flags(struct device *d,
8041 struct device_attribute *attr, char *buf)
8042{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07008043 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07008044
8045 return sprintf(buf, "0x%04X\n", priv->active_rxon.flags);
8046}
8047
8048static ssize_t store_flags(struct device *d,
8049 struct device_attribute *attr,
8050 const char *buf, size_t count)
8051{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07008052 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07008053 u32 flags = simple_strtoul(buf, NULL, 0);
8054
8055 mutex_lock(&priv->mutex);
8056 if (le32_to_cpu(priv->staging_rxon.flags) != flags) {
8057 /* Cancel any currently running scans... */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008058 if (iwl4965_scan_cancel_timeout(priv, 100))
Zhu Yib481de92007-09-25 17:54:57 -07008059 IWL_WARNING("Could not cancel scan.\n");
8060 else {
8061 IWL_DEBUG_INFO("Committing rxon.flags = 0x%04X\n",
8062 flags);
8063 priv->staging_rxon.flags = cpu_to_le32(flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008064 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07008065 }
8066 }
8067 mutex_unlock(&priv->mutex);
8068
8069 return count;
8070}
8071
8072static DEVICE_ATTR(flags, S_IWUSR | S_IRUGO, show_flags, store_flags);
8073
8074static ssize_t show_filter_flags(struct device *d,
8075 struct device_attribute *attr, char *buf)
8076{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07008077 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07008078
8079 return sprintf(buf, "0x%04X\n",
8080 le32_to_cpu(priv->active_rxon.filter_flags));
8081}
8082
8083static ssize_t store_filter_flags(struct device *d,
8084 struct device_attribute *attr,
8085 const char *buf, size_t count)
8086{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07008087 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07008088 u32 filter_flags = simple_strtoul(buf, NULL, 0);
8089
8090 mutex_lock(&priv->mutex);
8091 if (le32_to_cpu(priv->staging_rxon.filter_flags) != filter_flags) {
8092 /* Cancel any currently running scans... */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008093 if (iwl4965_scan_cancel_timeout(priv, 100))
Zhu Yib481de92007-09-25 17:54:57 -07008094 IWL_WARNING("Could not cancel scan.\n");
8095 else {
8096 IWL_DEBUG_INFO("Committing rxon.filter_flags = "
8097 "0x%04X\n", filter_flags);
8098 priv->staging_rxon.filter_flags =
8099 cpu_to_le32(filter_flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008100 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07008101 }
8102 }
8103 mutex_unlock(&priv->mutex);
8104
8105 return count;
8106}
8107
8108static DEVICE_ATTR(filter_flags, S_IWUSR | S_IRUGO, show_filter_flags,
8109 store_filter_flags);
8110
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08008111#ifdef CONFIG_IWL4965_SPECTRUM_MEASUREMENT
Zhu Yib481de92007-09-25 17:54:57 -07008112
8113static ssize_t show_measurement(struct device *d,
8114 struct device_attribute *attr, char *buf)
8115{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07008116 struct iwl_priv *priv = dev_get_drvdata(d);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008117 struct iwl4965_spectrum_notification measure_report;
Zhu Yib481de92007-09-25 17:54:57 -07008118 u32 size = sizeof(measure_report), len = 0, ofs = 0;
8119 u8 *data = (u8 *) & measure_report;
8120 unsigned long flags;
8121
8122 spin_lock_irqsave(&priv->lock, flags);
8123 if (!(priv->measurement_status & MEASUREMENT_READY)) {
8124 spin_unlock_irqrestore(&priv->lock, flags);
8125 return 0;
8126 }
8127 memcpy(&measure_report, &priv->measure_report, size);
8128 priv->measurement_status = 0;
8129 spin_unlock_irqrestore(&priv->lock, flags);
8130
8131 while (size && (PAGE_SIZE - len)) {
8132 hex_dump_to_buffer(data + ofs, size, 16, 1, buf + len,
8133 PAGE_SIZE - len, 1);
8134 len = strlen(buf);
8135 if (PAGE_SIZE - len)
8136 buf[len++] = '\n';
8137
8138 ofs += 16;
8139 size -= min(size, 16U);
8140 }
8141
8142 return len;
8143}
8144
8145static ssize_t store_measurement(struct device *d,
8146 struct device_attribute *attr,
8147 const char *buf, size_t count)
8148{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07008149 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07008150 struct ieee80211_measurement_params params = {
8151 .channel = le16_to_cpu(priv->active_rxon.channel),
8152 .start_time = cpu_to_le64(priv->last_tsf),
8153 .duration = cpu_to_le16(1),
8154 };
8155 u8 type = IWL_MEASURE_BASIC;
8156 u8 buffer[32];
8157 u8 channel;
8158
8159 if (count) {
8160 char *p = buffer;
8161 strncpy(buffer, buf, min(sizeof(buffer), count));
8162 channel = simple_strtoul(p, NULL, 0);
8163 if (channel)
8164 params.channel = channel;
8165
8166 p = buffer;
8167 while (*p && *p != ' ')
8168 p++;
8169 if (*p)
8170 type = simple_strtoul(p + 1, NULL, 0);
8171 }
8172
8173 IWL_DEBUG_INFO("Invoking measurement of type %d on "
8174 "channel %d (for '%s')\n", type, params.channel, buf);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008175 iwl4965_get_measurement(priv, &params, type);
Zhu Yib481de92007-09-25 17:54:57 -07008176
8177 return count;
8178}
8179
8180static DEVICE_ATTR(measurement, S_IRUSR | S_IWUSR,
8181 show_measurement, store_measurement);
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08008182#endif /* CONFIG_IWL4965_SPECTRUM_MEASUREMENT */
Zhu Yib481de92007-09-25 17:54:57 -07008183
8184static ssize_t store_retry_rate(struct device *d,
8185 struct device_attribute *attr,
8186 const char *buf, size_t count)
8187{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07008188 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07008189
8190 priv->retry_rate = simple_strtoul(buf, NULL, 0);
8191 if (priv->retry_rate <= 0)
8192 priv->retry_rate = 1;
8193
8194 return count;
8195}
8196
8197static ssize_t show_retry_rate(struct device *d,
8198 struct device_attribute *attr, char *buf)
8199{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07008200 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07008201 return sprintf(buf, "%d", priv->retry_rate);
8202}
8203
8204static DEVICE_ATTR(retry_rate, S_IWUSR | S_IRUSR, show_retry_rate,
8205 store_retry_rate);
8206
8207static ssize_t store_power_level(struct device *d,
8208 struct device_attribute *attr,
8209 const char *buf, size_t count)
8210{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07008211 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07008212 int rc;
8213 int mode;
8214
8215 mode = simple_strtoul(buf, NULL, 0);
8216 mutex_lock(&priv->mutex);
8217
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008218 if (!iwl4965_is_ready(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07008219 rc = -EAGAIN;
8220 goto out;
8221 }
8222
8223 if ((mode < 1) || (mode > IWL_POWER_LIMIT) || (mode == IWL_POWER_AC))
8224 mode = IWL_POWER_AC;
8225 else
8226 mode |= IWL_POWER_ENABLED;
8227
8228 if (mode != priv->power_mode) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008229 rc = iwl4965_send_power_mode(priv, IWL_POWER_LEVEL(mode));
Zhu Yib481de92007-09-25 17:54:57 -07008230 if (rc) {
8231 IWL_DEBUG_MAC80211("failed setting power mode.\n");
8232 goto out;
8233 }
8234 priv->power_mode = mode;
8235 }
8236
8237 rc = count;
8238
8239 out:
8240 mutex_unlock(&priv->mutex);
8241 return rc;
8242}
8243
8244#define MAX_WX_STRING 80
8245
8246/* Values are in microsecond */
8247static const s32 timeout_duration[] = {
8248 350000,
8249 250000,
8250 75000,
8251 37000,
8252 25000,
8253};
8254static const s32 period_duration[] = {
8255 400000,
8256 700000,
8257 1000000,
8258 1000000,
8259 1000000
8260};
8261
8262static ssize_t show_power_level(struct device *d,
8263 struct device_attribute *attr, char *buf)
8264{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07008265 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07008266 int level = IWL_POWER_LEVEL(priv->power_mode);
8267 char *p = buf;
8268
8269 p += sprintf(p, "%d ", level);
8270 switch (level) {
8271 case IWL_POWER_MODE_CAM:
8272 case IWL_POWER_AC:
8273 p += sprintf(p, "(AC)");
8274 break;
8275 case IWL_POWER_BATTERY:
8276 p += sprintf(p, "(BATTERY)");
8277 break;
8278 default:
8279 p += sprintf(p,
8280 "(Timeout %dms, Period %dms)",
8281 timeout_duration[level - 1] / 1000,
8282 period_duration[level - 1] / 1000);
8283 }
8284
8285 if (!(priv->power_mode & IWL_POWER_ENABLED))
8286 p += sprintf(p, " OFF\n");
8287 else
8288 p += sprintf(p, " \n");
8289
8290 return (p - buf + 1);
8291
8292}
8293
8294static DEVICE_ATTR(power_level, S_IWUSR | S_IRUSR, show_power_level,
8295 store_power_level);
8296
8297static ssize_t show_channels(struct device *d,
8298 struct device_attribute *attr, char *buf)
8299{
Johannes Berg8318d782008-01-24 19:38:38 +01008300 /* all this shit doesn't belong into sysfs anyway */
8301 return 0;
Zhu Yib481de92007-09-25 17:54:57 -07008302}
8303
8304static DEVICE_ATTR(channels, S_IRUSR, show_channels, NULL);
8305
8306static ssize_t show_statistics(struct device *d,
8307 struct device_attribute *attr, char *buf)
8308{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07008309 struct iwl_priv *priv = dev_get_drvdata(d);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008310 u32 size = sizeof(struct iwl4965_notif_statistics);
Zhu Yib481de92007-09-25 17:54:57 -07008311 u32 len = 0, ofs = 0;
8312 u8 *data = (u8 *) & priv->statistics;
8313 int rc = 0;
8314
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008315 if (!iwl4965_is_alive(priv))
Zhu Yib481de92007-09-25 17:54:57 -07008316 return -EAGAIN;
8317
8318 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008319 rc = iwl4965_send_statistics_request(priv);
Zhu Yib481de92007-09-25 17:54:57 -07008320 mutex_unlock(&priv->mutex);
8321
8322 if (rc) {
8323 len = sprintf(buf,
8324 "Error sending statistics request: 0x%08X\n", rc);
8325 return len;
8326 }
8327
8328 while (size && (PAGE_SIZE - len)) {
8329 hex_dump_to_buffer(data + ofs, size, 16, 1, buf + len,
8330 PAGE_SIZE - len, 1);
8331 len = strlen(buf);
8332 if (PAGE_SIZE - len)
8333 buf[len++] = '\n';
8334
8335 ofs += 16;
8336 size -= min(size, 16U);
8337 }
8338
8339 return len;
8340}
8341
8342static DEVICE_ATTR(statistics, S_IRUGO, show_statistics, NULL);
8343
8344static ssize_t show_antenna(struct device *d,
8345 struct device_attribute *attr, char *buf)
8346{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07008347 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07008348
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008349 if (!iwl4965_is_alive(priv))
Zhu Yib481de92007-09-25 17:54:57 -07008350 return -EAGAIN;
8351
8352 return sprintf(buf, "%d\n", priv->antenna);
8353}
8354
8355static ssize_t store_antenna(struct device *d,
8356 struct device_attribute *attr,
8357 const char *buf, size_t count)
8358{
8359 int ant;
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07008360 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07008361
8362 if (count == 0)
8363 return 0;
8364
8365 if (sscanf(buf, "%1i", &ant) != 1) {
8366 IWL_DEBUG_INFO("not in hex or decimal form.\n");
8367 return count;
8368 }
8369
8370 if ((ant >= 0) && (ant <= 2)) {
8371 IWL_DEBUG_INFO("Setting antenna select to %d.\n", ant);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008372 priv->antenna = (enum iwl4965_antenna)ant;
Zhu Yib481de92007-09-25 17:54:57 -07008373 } else
8374 IWL_DEBUG_INFO("Bad antenna select value %d.\n", ant);
8375
8376
8377 return count;
8378}
8379
8380static DEVICE_ATTR(antenna, S_IWUSR | S_IRUGO, show_antenna, store_antenna);
8381
8382static ssize_t show_status(struct device *d,
8383 struct device_attribute *attr, char *buf)
8384{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07008385 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008386 if (!iwl4965_is_alive(priv))
Zhu Yib481de92007-09-25 17:54:57 -07008387 return -EAGAIN;
8388 return sprintf(buf, "0x%08x\n", (int)priv->status);
8389}
8390
8391static DEVICE_ATTR(status, S_IRUGO, show_status, NULL);
8392
8393static ssize_t dump_error_log(struct device *d,
8394 struct device_attribute *attr,
8395 const char *buf, size_t count)
8396{
8397 char *p = (char *)buf;
8398
8399 if (p[0] == '1')
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07008400 iwl4965_dump_nic_error_log((struct iwl_priv *)d->driver_data);
Zhu Yib481de92007-09-25 17:54:57 -07008401
8402 return strnlen(buf, count);
8403}
8404
8405static DEVICE_ATTR(dump_errors, S_IWUSR, NULL, dump_error_log);
8406
8407static ssize_t dump_event_log(struct device *d,
8408 struct device_attribute *attr,
8409 const char *buf, size_t count)
8410{
8411 char *p = (char *)buf;
8412
8413 if (p[0] == '1')
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07008414 iwl4965_dump_nic_event_log((struct iwl_priv *)d->driver_data);
Zhu Yib481de92007-09-25 17:54:57 -07008415
8416 return strnlen(buf, count);
8417}
8418
8419static DEVICE_ATTR(dump_events, S_IWUSR, NULL, dump_event_log);
8420
8421/*****************************************************************************
8422 *
8423 * driver setup and teardown
8424 *
8425 *****************************************************************************/
8426
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07008427static void iwl4965_setup_deferred_work(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07008428{
8429 priv->workqueue = create_workqueue(DRV_NAME);
8430
8431 init_waitqueue_head(&priv->wait_command_queue);
8432
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008433 INIT_WORK(&priv->up, iwl4965_bg_up);
8434 INIT_WORK(&priv->restart, iwl4965_bg_restart);
8435 INIT_WORK(&priv->rx_replenish, iwl4965_bg_rx_replenish);
8436 INIT_WORK(&priv->scan_completed, iwl4965_bg_scan_completed);
8437 INIT_WORK(&priv->request_scan, iwl4965_bg_request_scan);
8438 INIT_WORK(&priv->abort_scan, iwl4965_bg_abort_scan);
8439 INIT_WORK(&priv->rf_kill, iwl4965_bg_rf_kill);
8440 INIT_WORK(&priv->beacon_update, iwl4965_bg_beacon_update);
8441 INIT_DELAYED_WORK(&priv->post_associate, iwl4965_bg_post_associate);
8442 INIT_DELAYED_WORK(&priv->init_alive_start, iwl4965_bg_init_alive_start);
8443 INIT_DELAYED_WORK(&priv->alive_start, iwl4965_bg_alive_start);
8444 INIT_DELAYED_WORK(&priv->scan_check, iwl4965_bg_scan_check);
Zhu Yib481de92007-09-25 17:54:57 -07008445
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008446 iwl4965_hw_setup_deferred_work(priv);
Zhu Yib481de92007-09-25 17:54:57 -07008447
8448 tasklet_init(&priv->irq_tasklet, (void (*)(unsigned long))
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008449 iwl4965_irq_tasklet, (unsigned long)priv);
Zhu Yib481de92007-09-25 17:54:57 -07008450}
8451
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07008452static void iwl4965_cancel_deferred_work(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07008453{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008454 iwl4965_hw_cancel_deferred_work(priv);
Zhu Yib481de92007-09-25 17:54:57 -07008455
Joonwoo Park3ae6a052007-11-29 10:43:16 +09008456 cancel_delayed_work_sync(&priv->init_alive_start);
Zhu Yib481de92007-09-25 17:54:57 -07008457 cancel_delayed_work(&priv->scan_check);
8458 cancel_delayed_work(&priv->alive_start);
8459 cancel_delayed_work(&priv->post_associate);
8460 cancel_work_sync(&priv->beacon_update);
8461}
8462
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008463static struct attribute *iwl4965_sysfs_entries[] = {
Zhu Yib481de92007-09-25 17:54:57 -07008464 &dev_attr_antenna.attr,
8465 &dev_attr_channels.attr,
8466 &dev_attr_dump_errors.attr,
8467 &dev_attr_dump_events.attr,
8468 &dev_attr_flags.attr,
8469 &dev_attr_filter_flags.attr,
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08008470#ifdef CONFIG_IWL4965_SPECTRUM_MEASUREMENT
Zhu Yib481de92007-09-25 17:54:57 -07008471 &dev_attr_measurement.attr,
8472#endif
8473 &dev_attr_power_level.attr,
8474 &dev_attr_retry_rate.attr,
8475 &dev_attr_rf_kill.attr,
8476 &dev_attr_rs_window.attr,
8477 &dev_attr_statistics.attr,
8478 &dev_attr_status.attr,
8479 &dev_attr_temperature.attr,
Zhu Yib481de92007-09-25 17:54:57 -07008480 &dev_attr_tx_power.attr,
8481
8482 NULL
8483};
8484
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008485static struct attribute_group iwl4965_attribute_group = {
Zhu Yib481de92007-09-25 17:54:57 -07008486 .name = NULL, /* put in device directory */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008487 .attrs = iwl4965_sysfs_entries,
Zhu Yib481de92007-09-25 17:54:57 -07008488};
8489
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008490static struct ieee80211_ops iwl4965_hw_ops = {
8491 .tx = iwl4965_mac_tx,
8492 .start = iwl4965_mac_start,
8493 .stop = iwl4965_mac_stop,
8494 .add_interface = iwl4965_mac_add_interface,
8495 .remove_interface = iwl4965_mac_remove_interface,
8496 .config = iwl4965_mac_config,
8497 .config_interface = iwl4965_mac_config_interface,
8498 .configure_filter = iwl4965_configure_filter,
8499 .set_key = iwl4965_mac_set_key,
8500 .get_stats = iwl4965_mac_get_stats,
8501 .get_tx_stats = iwl4965_mac_get_tx_stats,
8502 .conf_tx = iwl4965_mac_conf_tx,
8503 .get_tsf = iwl4965_mac_get_tsf,
8504 .reset_tsf = iwl4965_mac_reset_tsf,
8505 .beacon_update = iwl4965_mac_beacon_update,
Johannes Berg471b3ef2007-12-28 14:32:58 +01008506 .bss_info_changed = iwl4965_bss_info_changed,
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08008507#ifdef CONFIG_IWL4965_HT
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008508 .conf_ht = iwl4965_mac_conf_ht,
Ron Rindjunsky9ab46172007-12-25 17:00:38 +02008509 .ampdu_action = iwl4965_mac_ampdu_action,
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08008510#endif /* CONFIG_IWL4965_HT */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008511 .hw_scan = iwl4965_mac_hw_scan
Zhu Yib481de92007-09-25 17:54:57 -07008512};
8513
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008514static int iwl4965_pci_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
Zhu Yib481de92007-09-25 17:54:57 -07008515{
8516 int err = 0;
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07008517 struct iwl_priv *priv;
Zhu Yib481de92007-09-25 17:54:57 -07008518 struct ieee80211_hw *hw;
Tomas Winkler82b9a122008-03-04 18:09:30 -08008519 struct iwl_cfg *cfg = (struct iwl_cfg *)(ent->driver_data);
Zhu Yib481de92007-09-25 17:54:57 -07008520 int i;
Zhu Yi5a66926a2008-01-14 17:46:18 -08008521 DECLARE_MAC_BUF(mac);
Zhu Yib481de92007-09-25 17:54:57 -07008522
Assaf Krauss316c30d2008-03-14 10:38:46 -07008523 /************************
8524 * 1. Allocating HW data
8525 ************************/
8526
Cahill, Ben M6440adb2007-11-29 11:09:55 +08008527 /* Disabling hardware scan means that mac80211 will perform scans
8528 * "the hard way", rather than using device's scan. */
Assaf Krauss00acbc92008-03-14 10:38:47 -07008529 if (iwl4965_mod_params.disable_hw_scan) {
Zhu Yib481de92007-09-25 17:54:57 -07008530 IWL_DEBUG_INFO("Disabling hw_scan\n");
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008531 iwl4965_hw_ops.hw_scan = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07008532 }
8533
Assaf Krauss1d0a0822008-03-14 10:38:48 -07008534 hw = iwl_alloc_all(cfg, &iwl4965_hw_ops);
8535 if (!hw) {
Zhu Yib481de92007-09-25 17:54:57 -07008536 err = -ENOMEM;
8537 goto out;
8538 }
Assaf Krauss1d0a0822008-03-14 10:38:48 -07008539 priv = hw->priv;
8540 /* At this point both hw and priv are allocated. */
8541
Zhu Yib481de92007-09-25 17:54:57 -07008542 SET_IEEE80211_DEV(hw, &pdev->dev);
8543
8544 IWL_DEBUG_INFO("*** LOAD DRIVER ***\n");
Tomas Winkler82b9a122008-03-04 18:09:30 -08008545 priv->cfg = cfg;
Zhu Yib481de92007-09-25 17:54:57 -07008546 priv->pci_dev = pdev;
Assaf Krauss316c30d2008-03-14 10:38:46 -07008547
Tomas Winkler0a6857e2008-03-12 16:58:49 -07008548#ifdef CONFIG_IWLWIFI_DEBUG
Assaf Krauss00acbc92008-03-14 10:38:47 -07008549 iwl_debug_level = iwl4965_mod_params.debug;
Zhu Yib481de92007-09-25 17:54:57 -07008550 atomic_set(&priv->restrict_refcnt, 0);
8551#endif
Zhu Yib481de92007-09-25 17:54:57 -07008552
Assaf Krauss316c30d2008-03-14 10:38:46 -07008553 /**************************
8554 * 2. Initializing PCI bus
8555 **************************/
8556 if (pci_enable_device(pdev)) {
8557 err = -ENODEV;
8558 goto out_ieee80211_free_hw;
8559 }
8560
8561 pci_set_master(pdev);
8562
8563 err = pci_set_dma_mask(pdev, DMA_32BIT_MASK);
8564 if (!err)
8565 err = pci_set_consistent_dma_mask(pdev, DMA_32BIT_MASK);
8566 if (err) {
8567 printk(KERN_WARNING DRV_NAME
8568 ": No suitable DMA available.\n");
8569 goto out_pci_disable_device;
8570 }
8571
8572 err = pci_request_regions(pdev, DRV_NAME);
8573 if (err)
8574 goto out_pci_disable_device;
8575
8576 pci_set_drvdata(pdev, priv);
8577
8578 /* We disable the RETRY_TIMEOUT register (0x41) to keep
8579 * PCI Tx retries from interfering with C3 CPU state */
8580 pci_write_config_byte(pdev, 0x41, 0x00);
8581
8582 /***********************
8583 * 3. Read REV register
8584 ***********************/
8585 priv->hw_base = pci_iomap(pdev, 0, 0);
8586 if (!priv->hw_base) {
8587 err = -ENODEV;
8588 goto out_pci_release_regions;
8589 }
8590
8591 IWL_DEBUG_INFO("pci_resource_len = 0x%08llx\n",
8592 (unsigned long long) pci_resource_len(pdev, 0));
8593 IWL_DEBUG_INFO("pci_resource_base = %p\n", priv->hw_base);
8594
8595 printk(KERN_INFO DRV_NAME
8596 ": Detected Intel Wireless WiFi Link %s\n", priv->cfg->name);
8597
8598 /*****************
8599 * 4. Read EEPROM
8600 *****************/
8601 /* nic init */
8602 iwl4965_set_bit(priv, CSR_GIO_CHICKEN_BITS,
8603 CSR_GIO_CHICKEN_BITS_REG_BIT_DIS_L0S_EXIT_TIMER);
8604
8605 iwl4965_set_bit(priv, CSR_GP_CNTRL, CSR_GP_CNTRL_REG_FLAG_INIT_DONE);
8606 err = iwl4965_poll_bit(priv, CSR_GP_CNTRL,
8607 CSR_GP_CNTRL_REG_FLAG_MAC_CLOCK_READY,
8608 CSR_GP_CNTRL_REG_FLAG_MAC_CLOCK_READY, 25000);
8609 if (err < 0) {
8610 IWL_DEBUG_INFO("Failed to init the card\n");
8611 goto out_iounmap;
8612 }
8613 /* Read the EEPROM */
8614 err = iwl_eeprom_init(priv);
8615 if (err) {
8616 IWL_ERROR("Unable to init EEPROM\n");
8617 goto out_iounmap;
8618 }
8619 /* MAC Address location in EEPROM same for 3945/4965 */
8620 iwl_eeprom_get_mac(priv, priv->mac_addr);
8621 IWL_DEBUG_INFO("MAC address: %s\n", print_mac(mac, priv->mac_addr));
8622 SET_IEEE80211_PERM_ADDR(priv->hw, priv->mac_addr);
8623
8624 /************************
8625 * 5. Setup HW constants
8626 ************************/
8627 /* Device-specific setup */
8628 if (iwl4965_hw_set_hw_setting(priv)) {
8629 IWL_ERROR("failed to set hw settings\n");
8630 goto out_iounmap;
8631 }
8632
8633 /*******************
8634 * 6. Setup hw/priv
8635 *******************/
Zhu Yib481de92007-09-25 17:54:57 -07008636
8637 /* Tell mac80211 and its clients (e.g. Wireless Extensions)
8638 * the range of signal quality values that we'll provide.
8639 * Negative values for level/noise indicate that we'll provide dBm.
8640 * For WE, at least, non-0 values here *enable* display of values
8641 * in app (iwconfig). */
8642 hw->max_rssi = -20; /* signal level, negative indicates dBm */
8643 hw->max_noise = -20; /* noise level, negative indicates dBm */
8644 hw->max_signal = 100; /* link quality indication (%) */
8645
8646 /* Tell mac80211 our Tx characteristics */
8647 hw->flags = IEEE80211_HW_HOST_GEN_BEACON_TEMPLATE;
8648
Cahill, Ben M6440adb2007-11-29 11:09:55 +08008649 /* Default value; 4 EDCA QOS priorities */
Zhu Yib481de92007-09-25 17:54:57 -07008650 hw->queues = 4;
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08008651#ifdef CONFIG_IWL4965_HT
Cahill, Ben M6440adb2007-11-29 11:09:55 +08008652 /* Enhanced value; more queues, to support 11n aggregation */
Zhu Yib481de92007-09-25 17:54:57 -07008653 hw->queues = 16;
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08008654#endif /* CONFIG_IWL4965_HT */
Zhu Yib481de92007-09-25 17:54:57 -07008655
Assaf Krauss316c30d2008-03-14 10:38:46 -07008656 hw->rate_control_algorithm = "iwl-4965-rs";
Assaf Krauss00acbc92008-03-14 10:38:47 -07008657 priv->antenna = (enum iwl4965_antenna)iwl4965_mod_params.antenna;
Assaf Krauss316c30d2008-03-14 10:38:46 -07008658 priv->retry_rate = 1;
8659 priv->ibss_beacon = NULL;
8660
Zhu Yib481de92007-09-25 17:54:57 -07008661 spin_lock_init(&priv->lock);
8662 spin_lock_init(&priv->power_data.lock);
8663 spin_lock_init(&priv->sta_lock);
8664 spin_lock_init(&priv->hcmd_lock);
8665 spin_lock_init(&priv->lq_mngr.lock);
8666
8667 for (i = 0; i < IWL_IBSS_MAC_HASH_SIZE; i++)
8668 INIT_LIST_HEAD(&priv->ibss_mac_hash[i]);
8669
8670 INIT_LIST_HEAD(&priv->free_frames);
8671
8672 mutex_init(&priv->mutex);
Zhu Yib481de92007-09-25 17:54:57 -07008673
Cahill, Ben M6440adb2007-11-29 11:09:55 +08008674 /* Clear the driver's (not device's) station table */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008675 iwl4965_clear_stations_table(priv);
Zhu Yib481de92007-09-25 17:54:57 -07008676
8677 priv->data_retry_limit = -1;
8678 priv->ieee_channels = NULL;
8679 priv->ieee_rates = NULL;
Johannes Berg8318d782008-01-24 19:38:38 +01008680 priv->band = IEEE80211_BAND_2GHZ;
Zhu Yib481de92007-09-25 17:54:57 -07008681
Zhu Yib481de92007-09-25 17:54:57 -07008682 priv->iw_mode = IEEE80211_IF_TYPE_STA;
8683
Zhu Yib481de92007-09-25 17:54:57 -07008684 priv->use_ant_b_for_management_frame = 1; /* start with ant B */
Zhu Yib481de92007-09-25 17:54:57 -07008685 priv->valid_antenna = 0x7; /* assume all 3 connected */
8686 priv->ps_mode = IWL_MIMO_PS_NONE;
Zhu Yib481de92007-09-25 17:54:57 -07008687
Cahill, Ben M6440adb2007-11-29 11:09:55 +08008688 /* Choose which receivers/antennas to use */
Zhu Yib481de92007-09-25 17:54:57 -07008689 iwl4965_set_rxon_chain(priv);
8690
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008691 iwl4965_reset_qos(priv);
Zhu Yib481de92007-09-25 17:54:57 -07008692
8693 priv->qos_data.qos_active = 0;
8694 priv->qos_data.qos_cap.val = 0;
Zhu Yib481de92007-09-25 17:54:57 -07008695
Johannes Berg8318d782008-01-24 19:38:38 +01008696 iwl4965_set_rxon_channel(priv, IEEE80211_BAND_2GHZ, 6);
Zhu Yib481de92007-09-25 17:54:57 -07008697
8698 priv->rates_mask = IWL_RATES_MASK;
8699 /* If power management is turned on, default to AC mode */
8700 priv->power_mode = IWL_POWER_AC;
8701 priv->user_txpower_limit = IWL_DEFAULT_TX_POWER;
8702
Reinette Chatre849e0dc2008-01-23 10:15:18 -08008703 err = iwl4965_init_channel_map(priv);
8704 if (err) {
8705 IWL_ERROR("initializing regulatory failed: %d\n", err);
Assaf Krauss316c30d2008-03-14 10:38:46 -07008706 goto out_unset_hw_settings;
Reinette Chatre849e0dc2008-01-23 10:15:18 -08008707 }
8708
8709 err = iwl4965_init_geos(priv);
8710 if (err) {
8711 IWL_ERROR("initializing geos failed: %d\n", err);
8712 goto out_free_channel_map;
8713 }
Reinette Chatre849e0dc2008-01-23 10:15:18 -08008714
Zhu Yi5a66926a2008-01-14 17:46:18 -08008715 iwl4965_rate_control_register(priv->hw);
8716 err = ieee80211_register_hw(priv->hw);
8717 if (err) {
8718 IWL_ERROR("Failed to register network device (error %d)\n", err);
Reinette Chatre849e0dc2008-01-23 10:15:18 -08008719 goto out_free_geos;
Zhu Yib481de92007-09-25 17:54:57 -07008720 }
8721
Zhu Yi5a66926a2008-01-14 17:46:18 -08008722 priv->hw->conf.beacon_int = 100;
8723 priv->mac80211_registered = 1;
Assaf Krauss316c30d2008-03-14 10:38:46 -07008724
8725 /**********************************
8726 * 7. Initialize module parameters
8727 **********************************/
8728
8729 /* Disable radio (SW RF KILL) via parameter when loading driver */
Assaf Krauss00acbc92008-03-14 10:38:47 -07008730 if (iwl4965_mod_params.disable) {
Assaf Krauss316c30d2008-03-14 10:38:46 -07008731 set_bit(STATUS_RF_KILL_SW, &priv->status);
8732 IWL_DEBUG_INFO("Radio disabled.\n");
8733 }
8734
Assaf Krauss00acbc92008-03-14 10:38:47 -07008735 if (iwl4965_mod_params.enable_qos)
Assaf Krauss316c30d2008-03-14 10:38:46 -07008736 priv->qos_data.qos_enable = 1;
8737
8738 /********************
8739 * 8. Setup services
8740 ********************/
8741 iwl4965_disable_interrupts(priv);
8742
8743 err = sysfs_create_group(&pdev->dev.kobj, &iwl4965_attribute_group);
8744 if (err) {
8745 IWL_ERROR("failed to create sysfs device attributes\n");
8746 goto out_free_geos;
8747 }
8748
8749 err = iwl_dbgfs_register(priv, DRV_NAME);
8750 if (err) {
8751 IWL_ERROR("failed to create debugfs files\n");
8752 goto out_remove_sysfs;
8753 }
8754
8755 iwl4965_setup_deferred_work(priv);
8756 iwl4965_setup_rx_handlers(priv);
8757
8758 /********************
8759 * 9. Conclude
8760 ********************/
Zhu Yi5a66926a2008-01-14 17:46:18 -08008761 pci_save_state(pdev);
8762 pci_disable_device(pdev);
Zhu Yib481de92007-09-25 17:54:57 -07008763
8764 return 0;
8765
Assaf Krauss316c30d2008-03-14 10:38:46 -07008766 out_remove_sysfs:
8767 sysfs_remove_group(&pdev->dev.kobj, &iwl4965_attribute_group);
Reinette Chatre849e0dc2008-01-23 10:15:18 -08008768 out_free_geos:
8769 iwl4965_free_geos(priv);
8770 out_free_channel_map:
8771 iwl4965_free_channel_map(priv);
Assaf Krauss316c30d2008-03-14 10:38:46 -07008772 out_unset_hw_settings:
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008773 iwl4965_unset_hw_setting(priv);
Zhu Yib481de92007-09-25 17:54:57 -07008774 out_iounmap:
8775 pci_iounmap(pdev, priv->hw_base);
8776 out_pci_release_regions:
8777 pci_release_regions(pdev);
Assaf Krauss316c30d2008-03-14 10:38:46 -07008778 pci_set_drvdata(pdev, NULL);
Zhu Yib481de92007-09-25 17:54:57 -07008779 out_pci_disable_device:
8780 pci_disable_device(pdev);
Zhu Yib481de92007-09-25 17:54:57 -07008781 out_ieee80211_free_hw:
8782 ieee80211_free_hw(priv->hw);
8783 out:
8784 return err;
8785}
8786
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008787static void iwl4965_pci_remove(struct pci_dev *pdev)
Zhu Yib481de92007-09-25 17:54:57 -07008788{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07008789 struct iwl_priv *priv = pci_get_drvdata(pdev);
Zhu Yib481de92007-09-25 17:54:57 -07008790 struct list_head *p, *q;
8791 int i;
8792
8793 if (!priv)
8794 return;
8795
8796 IWL_DEBUG_INFO("*** UNLOAD DRIVER ***\n");
8797
Zhu Yib481de92007-09-25 17:54:57 -07008798 set_bit(STATUS_EXIT_PENDING, &priv->status);
Zhu Yib24d22b2007-12-19 13:59:52 +08008799
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008800 iwl4965_down(priv);
Zhu Yib481de92007-09-25 17:54:57 -07008801
8802 /* Free MAC hash list for ADHOC */
8803 for (i = 0; i < IWL_IBSS_MAC_HASH_SIZE; i++) {
8804 list_for_each_safe(p, q, &priv->ibss_mac_hash[i]) {
8805 list_del(p);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008806 kfree(list_entry(p, struct iwl4965_ibss_seq, list));
Zhu Yib481de92007-09-25 17:54:57 -07008807 }
8808 }
8809
Tomas Winkler712b6cf2008-03-12 16:58:52 -07008810 iwl_dbgfs_unregister(priv);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008811 sysfs_remove_group(&pdev->dev.kobj, &iwl4965_attribute_group);
Zhu Yib481de92007-09-25 17:54:57 -07008812
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008813 iwl4965_dealloc_ucode_pci(priv);
Zhu Yib481de92007-09-25 17:54:57 -07008814
8815 if (priv->rxq.bd)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008816 iwl4965_rx_queue_free(priv, &priv->rxq);
8817 iwl4965_hw_txq_ctx_free(priv);
Zhu Yib481de92007-09-25 17:54:57 -07008818
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008819 iwl4965_unset_hw_setting(priv);
8820 iwl4965_clear_stations_table(priv);
Zhu Yib481de92007-09-25 17:54:57 -07008821
8822 if (priv->mac80211_registered) {
8823 ieee80211_unregister_hw(priv->hw);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008824 iwl4965_rate_control_unregister(priv->hw);
Zhu Yib481de92007-09-25 17:54:57 -07008825 }
8826
Mohamed Abbas948c1712007-10-25 17:15:45 +08008827 /*netif_stop_queue(dev); */
8828 flush_workqueue(priv->workqueue);
8829
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008830 /* ieee80211_unregister_hw calls iwl4965_mac_stop, which flushes
Zhu Yib481de92007-09-25 17:54:57 -07008831 * priv->workqueue... so we can't take down the workqueue
8832 * until now... */
8833 destroy_workqueue(priv->workqueue);
8834 priv->workqueue = NULL;
8835
Zhu Yib481de92007-09-25 17:54:57 -07008836 pci_iounmap(pdev, priv->hw_base);
8837 pci_release_regions(pdev);
8838 pci_disable_device(pdev);
8839 pci_set_drvdata(pdev, NULL);
8840
Reinette Chatre849e0dc2008-01-23 10:15:18 -08008841 iwl4965_free_channel_map(priv);
8842 iwl4965_free_geos(priv);
Zhu Yib481de92007-09-25 17:54:57 -07008843
8844 if (priv->ibss_beacon)
8845 dev_kfree_skb(priv->ibss_beacon);
8846
8847 ieee80211_free_hw(priv->hw);
8848}
8849
8850#ifdef CONFIG_PM
8851
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008852static int iwl4965_pci_suspend(struct pci_dev *pdev, pm_message_t state)
Zhu Yib481de92007-09-25 17:54:57 -07008853{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07008854 struct iwl_priv *priv = pci_get_drvdata(pdev);
Zhu Yib481de92007-09-25 17:54:57 -07008855
Zhu Yie655b9f2008-01-24 02:19:38 -08008856 if (priv->is_open) {
8857 set_bit(STATUS_IN_SUSPEND, &priv->status);
8858 iwl4965_mac_stop(priv->hw);
8859 priv->is_open = 1;
8860 }
Zhu Yib481de92007-09-25 17:54:57 -07008861
Zhu Yib481de92007-09-25 17:54:57 -07008862 pci_set_power_state(pdev, PCI_D3hot);
8863
Zhu Yib481de92007-09-25 17:54:57 -07008864 return 0;
8865}
8866
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008867static int iwl4965_pci_resume(struct pci_dev *pdev)
Zhu Yib481de92007-09-25 17:54:57 -07008868{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07008869 struct iwl_priv *priv = pci_get_drvdata(pdev);
Zhu Yib481de92007-09-25 17:54:57 -07008870
Zhu Yib481de92007-09-25 17:54:57 -07008871 pci_set_power_state(pdev, PCI_D0);
Zhu Yib481de92007-09-25 17:54:57 -07008872
Zhu Yie655b9f2008-01-24 02:19:38 -08008873 if (priv->is_open)
8874 iwl4965_mac_start(priv->hw);
Zhu Yib481de92007-09-25 17:54:57 -07008875
Zhu Yie655b9f2008-01-24 02:19:38 -08008876 clear_bit(STATUS_IN_SUSPEND, &priv->status);
Zhu Yib481de92007-09-25 17:54:57 -07008877 return 0;
8878}
8879
8880#endif /* CONFIG_PM */
8881
8882/*****************************************************************************
8883 *
8884 * driver and module entry point
8885 *
8886 *****************************************************************************/
8887
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008888static struct pci_driver iwl4965_driver = {
Zhu Yib481de92007-09-25 17:54:57 -07008889 .name = DRV_NAME,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008890 .id_table = iwl4965_hw_card_ids,
8891 .probe = iwl4965_pci_probe,
8892 .remove = __devexit_p(iwl4965_pci_remove),
Zhu Yib481de92007-09-25 17:54:57 -07008893#ifdef CONFIG_PM
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008894 .suspend = iwl4965_pci_suspend,
8895 .resume = iwl4965_pci_resume,
Zhu Yib481de92007-09-25 17:54:57 -07008896#endif
8897};
8898
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008899static int __init iwl4965_init(void)
Zhu Yib481de92007-09-25 17:54:57 -07008900{
8901
8902 int ret;
8903 printk(KERN_INFO DRV_NAME ": " DRV_DESCRIPTION ", " DRV_VERSION "\n");
8904 printk(KERN_INFO DRV_NAME ": " DRV_COPYRIGHT "\n");
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008905 ret = pci_register_driver(&iwl4965_driver);
Zhu Yib481de92007-09-25 17:54:57 -07008906 if (ret) {
8907 IWL_ERROR("Unable to initialize PCI module\n");
8908 return ret;
8909 }
Tomas Winkler0a6857e2008-03-12 16:58:49 -07008910#ifdef CONFIG_IWLWIFI_DEBUG
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008911 ret = driver_create_file(&iwl4965_driver.driver, &driver_attr_debug_level);
Zhu Yib481de92007-09-25 17:54:57 -07008912 if (ret) {
8913 IWL_ERROR("Unable to create driver sysfs file\n");
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008914 pci_unregister_driver(&iwl4965_driver);
Zhu Yib481de92007-09-25 17:54:57 -07008915 return ret;
8916 }
8917#endif
8918
8919 return ret;
8920}
8921
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008922static void __exit iwl4965_exit(void)
Zhu Yib481de92007-09-25 17:54:57 -07008923{
Tomas Winkler0a6857e2008-03-12 16:58:49 -07008924#ifdef CONFIG_IWLWIFI_DEBUG
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008925 driver_remove_file(&iwl4965_driver.driver, &driver_attr_debug_level);
Zhu Yib481de92007-09-25 17:54:57 -07008926#endif
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008927 pci_unregister_driver(&iwl4965_driver);
Zhu Yib481de92007-09-25 17:54:57 -07008928}
8929
Assaf Krauss00acbc92008-03-14 10:38:47 -07008930module_param_named(antenna, iwl4965_mod_params.antenna, int, 0444);
Zhu Yib481de92007-09-25 17:54:57 -07008931MODULE_PARM_DESC(antenna, "select antenna (1=Main, 2=Aux, default 0 [both])");
Assaf Krauss00acbc92008-03-14 10:38:47 -07008932module_param_named(disable, iwl4965_mod_params.disable, int, 0444);
Zhu Yib481de92007-09-25 17:54:57 -07008933MODULE_PARM_DESC(disable, "manually disable the radio (default 0 [radio on])");
Assaf Krauss00acbc92008-03-14 10:38:47 -07008934module_param_named(hwcrypto, iwl4965_mod_params.hw_crypto, int, 0444);
Zhu Yib481de92007-09-25 17:54:57 -07008935MODULE_PARM_DESC(hwcrypto,
8936 "using hardware crypto engine (default 0 [software])\n");
Assaf Krauss00acbc92008-03-14 10:38:47 -07008937module_param_named(debug, iwl4965_mod_params.debug, int, 0444);
Zhu Yib481de92007-09-25 17:54:57 -07008938MODULE_PARM_DESC(debug, "debug output mask");
Assaf Krauss00acbc92008-03-14 10:38:47 -07008939module_param_named(
8940 disable_hw_scan, iwl4965_mod_params.disable_hw_scan, int, 0444);
Zhu Yib481de92007-09-25 17:54:57 -07008941MODULE_PARM_DESC(disable_hw_scan, "disable hardware scanning (default 0)");
8942
Assaf Krauss00acbc92008-03-14 10:38:47 -07008943module_param_named(queues_num, iwl4965_mod_params.num_of_queues, int, 0444);
Zhu Yib481de92007-09-25 17:54:57 -07008944MODULE_PARM_DESC(queues_num, "number of hw queues.");
8945
8946/* QoS */
Assaf Krauss00acbc92008-03-14 10:38:47 -07008947module_param_named(qos_enable, iwl4965_mod_params.enable_qos, int, 0444);
Zhu Yib481de92007-09-25 17:54:57 -07008948MODULE_PARM_DESC(qos_enable, "enable all QoS functionality");
Assaf Krauss00acbc92008-03-14 10:38:47 -07008949module_param_named(amsdu_size_8K, iwl4965_mod_params.amsdu_size_8K, int, 0444);
Ron Rindjunsky9ee1ba42007-11-26 16:14:42 +02008950MODULE_PARM_DESC(amsdu_size_8K, "enable 8K amsdu size");
Zhu Yib481de92007-09-25 17:54:57 -07008951
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008952module_exit(iwl4965_exit);
8953module_init(iwl4965_init);