blob: 8de301d1da8e1226f9f2fccd2ba8b8e05e78589f [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/**
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800434 * iwl4965_add_station_flags - Add station to tables in driver and device
435 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700436u8 iwl4965_add_station_flags(struct iwl_priv *priv, const u8 *addr,
Ron Rindjunsky67d62032007-11-26 16:14:40 +0200437 int is_ap, u8 flags, void *ht_data)
Zhu Yib481de92007-09-25 17:54:57 -0700438{
439 int i;
440 int index = IWL_INVALID_STATION;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800441 struct iwl4965_station_entry *station;
Zhu Yib481de92007-09-25 17:54:57 -0700442 unsigned long flags_spin;
Joe Perches0795af52007-10-03 17:59:30 -0700443 DECLARE_MAC_BUF(mac);
Zhu Yib481de92007-09-25 17:54:57 -0700444
445 spin_lock_irqsave(&priv->sta_lock, flags_spin);
446 if (is_ap)
447 index = IWL_AP_ID;
448 else if (is_broadcast_ether_addr(addr))
449 index = priv->hw_setting.bcast_sta_id;
450 else
451 for (i = IWL_STA_ID; i < priv->hw_setting.max_stations; i++) {
452 if (!compare_ether_addr(priv->stations[i].sta.sta.addr,
453 addr)) {
454 index = i;
455 break;
456 }
457
458 if (!priv->stations[i].used &&
459 index == IWL_INVALID_STATION)
460 index = i;
461 }
462
463
Ben Cahill9fbab512007-11-29 11:09:47 +0800464 /* These two conditions have the same outcome, but keep them separate
465 since they have different meanings */
Zhu Yib481de92007-09-25 17:54:57 -0700466 if (unlikely(index == IWL_INVALID_STATION)) {
467 spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
468 return index;
469 }
470
471 if (priv->stations[index].used &&
472 !compare_ether_addr(priv->stations[index].sta.sta.addr, addr)) {
473 spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
474 return index;
475 }
476
477
Joe Perches0795af52007-10-03 17:59:30 -0700478 IWL_DEBUG_ASSOC("Add STA ID %d: %s\n", index, print_mac(mac, addr));
Zhu Yib481de92007-09-25 17:54:57 -0700479 station = &priv->stations[index];
480 station->used = 1;
481 priv->num_stations++;
482
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800483 /* Set up the REPLY_ADD_STA command to send to device */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800484 memset(&station->sta, 0, sizeof(struct iwl4965_addsta_cmd));
Zhu Yib481de92007-09-25 17:54:57 -0700485 memcpy(station->sta.sta.addr, addr, ETH_ALEN);
486 station->sta.mode = 0;
487 station->sta.sta.sta_id = index;
488 station->sta.station_flags = 0;
489
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +0800490#ifdef CONFIG_IWL4965_HT
Zhu Yib481de92007-09-25 17:54:57 -0700491 /* BCAST station and IBSS stations do not work in HT mode */
492 if (index != priv->hw_setting.bcast_sta_id &&
493 priv->iw_mode != IEEE80211_IF_TYPE_IBSS)
Ron Rindjunsky67d62032007-11-26 16:14:40 +0200494 iwl4965_set_ht_add_station(priv, index,
495 (struct ieee80211_ht_info *) ht_data);
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +0800496#endif /*CONFIG_IWL4965_HT*/
Zhu Yib481de92007-09-25 17:54:57 -0700497
498 spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800499
500 /* Add station to device's station table */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800501 iwl4965_send_add_station(priv, &station->sta, flags);
Zhu Yib481de92007-09-25 17:54:57 -0700502 return index;
503
504}
505
506/*************** DRIVER STATUS FUNCTIONS *****/
507
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700508static inline int iwl4965_is_ready(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700509{
510 /* The adapter is 'ready' if READY and GEO_CONFIGURED bits are
511 * set but EXIT_PENDING is not */
512 return test_bit(STATUS_READY, &priv->status) &&
513 test_bit(STATUS_GEO_CONFIGURED, &priv->status) &&
514 !test_bit(STATUS_EXIT_PENDING, &priv->status);
515}
516
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700517static inline int iwl4965_is_alive(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700518{
519 return test_bit(STATUS_ALIVE, &priv->status);
520}
521
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700522static inline int iwl4965_is_init(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700523{
524 return test_bit(STATUS_INIT, &priv->status);
525}
526
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700527static inline int iwl4965_is_rfkill(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700528{
529 return test_bit(STATUS_RF_KILL_HW, &priv->status) ||
530 test_bit(STATUS_RF_KILL_SW, &priv->status);
531}
532
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700533static inline int iwl4965_is_ready_rf(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700534{
535
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800536 if (iwl4965_is_rfkill(priv))
Zhu Yib481de92007-09-25 17:54:57 -0700537 return 0;
538
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800539 return iwl4965_is_ready(priv);
Zhu Yib481de92007-09-25 17:54:57 -0700540}
541
542/*************** HOST COMMAND QUEUE FUNCTIONS *****/
543
544#define IWL_CMD(x) case x : return #x
545
546static const char *get_cmd_string(u8 cmd)
547{
548 switch (cmd) {
549 IWL_CMD(REPLY_ALIVE);
550 IWL_CMD(REPLY_ERROR);
551 IWL_CMD(REPLY_RXON);
552 IWL_CMD(REPLY_RXON_ASSOC);
553 IWL_CMD(REPLY_QOS_PARAM);
554 IWL_CMD(REPLY_RXON_TIMING);
555 IWL_CMD(REPLY_ADD_STA);
556 IWL_CMD(REPLY_REMOVE_STA);
557 IWL_CMD(REPLY_REMOVE_ALL_STA);
558 IWL_CMD(REPLY_TX);
559 IWL_CMD(REPLY_RATE_SCALE);
560 IWL_CMD(REPLY_LEDS_CMD);
561 IWL_CMD(REPLY_TX_LINK_QUALITY_CMD);
562 IWL_CMD(RADAR_NOTIFICATION);
563 IWL_CMD(REPLY_QUIET_CMD);
564 IWL_CMD(REPLY_CHANNEL_SWITCH);
565 IWL_CMD(CHANNEL_SWITCH_NOTIFICATION);
566 IWL_CMD(REPLY_SPECTRUM_MEASUREMENT_CMD);
567 IWL_CMD(SPECTRUM_MEASURE_NOTIFICATION);
568 IWL_CMD(POWER_TABLE_CMD);
569 IWL_CMD(PM_SLEEP_NOTIFICATION);
570 IWL_CMD(PM_DEBUG_STATISTIC_NOTIFIC);
571 IWL_CMD(REPLY_SCAN_CMD);
572 IWL_CMD(REPLY_SCAN_ABORT_CMD);
573 IWL_CMD(SCAN_START_NOTIFICATION);
574 IWL_CMD(SCAN_RESULTS_NOTIFICATION);
575 IWL_CMD(SCAN_COMPLETE_NOTIFICATION);
576 IWL_CMD(BEACON_NOTIFICATION);
577 IWL_CMD(REPLY_TX_BEACON);
578 IWL_CMD(WHO_IS_AWAKE_NOTIFICATION);
579 IWL_CMD(QUIET_NOTIFICATION);
580 IWL_CMD(REPLY_TX_PWR_TABLE_CMD);
581 IWL_CMD(MEASURE_ABORT_NOTIFICATION);
582 IWL_CMD(REPLY_BT_CONFIG);
583 IWL_CMD(REPLY_STATISTICS_CMD);
584 IWL_CMD(STATISTICS_NOTIFICATION);
585 IWL_CMD(REPLY_CARD_STATE_CMD);
586 IWL_CMD(CARD_STATE_NOTIFICATION);
587 IWL_CMD(MISSED_BEACONS_NOTIFICATION);
588 IWL_CMD(REPLY_CT_KILL_CONFIG_CMD);
589 IWL_CMD(SENSITIVITY_CMD);
590 IWL_CMD(REPLY_PHY_CALIBRATION_CMD);
591 IWL_CMD(REPLY_RX_PHY_CMD);
592 IWL_CMD(REPLY_RX_MPDU_CMD);
593 IWL_CMD(REPLY_4965_RX);
594 IWL_CMD(REPLY_COMPRESSED_BA);
595 default:
596 return "UNKNOWN";
597
598 }
599}
600
601#define HOST_COMPLETE_TIMEOUT (HZ / 2)
602
603/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800604 * iwl4965_enqueue_hcmd - enqueue a uCode command
Zhu Yib481de92007-09-25 17:54:57 -0700605 * @priv: device private data point
606 * @cmd: a point to the ucode command structure
607 *
608 * The function returns < 0 values to indicate the operation is
609 * failed. On success, it turns the index (> 0) of command in the
610 * command queue.
611 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700612static int iwl4965_enqueue_hcmd(struct iwl_priv *priv, struct iwl4965_host_cmd *cmd)
Zhu Yib481de92007-09-25 17:54:57 -0700613{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800614 struct iwl4965_tx_queue *txq = &priv->txq[IWL_CMD_QUEUE_NUM];
615 struct iwl4965_queue *q = &txq->q;
616 struct iwl4965_tfd_frame *tfd;
Zhu Yib481de92007-09-25 17:54:57 -0700617 u32 *control_flags;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800618 struct iwl4965_cmd *out_cmd;
Zhu Yib481de92007-09-25 17:54:57 -0700619 u32 idx;
620 u16 fix_size = (u16)(cmd->len + sizeof(out_cmd->hdr));
621 dma_addr_t phys_addr;
622 int ret;
623 unsigned long flags;
624
625 /* If any of the command structures end up being larger than
626 * the TFD_MAX_PAYLOAD_SIZE, and it sent as a 'small' command then
627 * we will need to increase the size of the TFD entries */
628 BUG_ON((fix_size > TFD_MAX_PAYLOAD_SIZE) &&
629 !(cmd->meta.flags & CMD_SIZE_HUGE));
630
Gregory Greenmanc342a1b2008-02-06 11:20:40 -0800631 if (iwl4965_is_rfkill(priv)) {
632 IWL_DEBUG_INFO("Not sending command - RF KILL");
633 return -EIO;
634 }
635
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800636 if (iwl4965_queue_space(q) < ((cmd->meta.flags & CMD_ASYNC) ? 2 : 1)) {
Zhu Yib481de92007-09-25 17:54:57 -0700637 IWL_ERROR("No space for Tx\n");
638 return -ENOSPC;
639 }
640
641 spin_lock_irqsave(&priv->hcmd_lock, flags);
642
Tomas Winklerfc4b6852007-10-25 17:15:24 +0800643 tfd = &txq->bd[q->write_ptr];
Zhu Yib481de92007-09-25 17:54:57 -0700644 memset(tfd, 0, sizeof(*tfd));
645
646 control_flags = (u32 *) tfd;
647
Tomas Winklerfc4b6852007-10-25 17:15:24 +0800648 idx = get_cmd_index(q, q->write_ptr, cmd->meta.flags & CMD_SIZE_HUGE);
Zhu Yib481de92007-09-25 17:54:57 -0700649 out_cmd = &txq->cmd[idx];
650
651 out_cmd->hdr.cmd = cmd->id;
652 memcpy(&out_cmd->meta, &cmd->meta, sizeof(cmd->meta));
653 memcpy(&out_cmd->cmd.payload, cmd->data, cmd->len);
654
655 /* At this point, the out_cmd now has all of the incoming cmd
656 * information */
657
658 out_cmd->hdr.flags = 0;
659 out_cmd->hdr.sequence = cpu_to_le16(QUEUE_TO_SEQ(IWL_CMD_QUEUE_NUM) |
Tomas Winklerfc4b6852007-10-25 17:15:24 +0800660 INDEX_TO_SEQ(q->write_ptr));
Zhu Yib481de92007-09-25 17:54:57 -0700661 if (out_cmd->meta.flags & CMD_SIZE_HUGE)
662 out_cmd->hdr.sequence |= cpu_to_le16(SEQ_HUGE_FRAME);
663
664 phys_addr = txq->dma_addr_cmd + sizeof(txq->cmd[0]) * idx +
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800665 offsetof(struct iwl4965_cmd, hdr);
666 iwl4965_hw_txq_attach_buf_to_tfd(priv, tfd, phys_addr, fix_size);
Zhu Yib481de92007-09-25 17:54:57 -0700667
668 IWL_DEBUG_HC("Sending command %s (#%x), seq: 0x%04X, "
669 "%d bytes at %d[%d]:%d\n",
670 get_cmd_string(out_cmd->hdr.cmd),
671 out_cmd->hdr.cmd, le16_to_cpu(out_cmd->hdr.sequence),
Tomas Winklerfc4b6852007-10-25 17:15:24 +0800672 fix_size, q->write_ptr, idx, IWL_CMD_QUEUE_NUM);
Zhu Yib481de92007-09-25 17:54:57 -0700673
674 txq->need_update = 1;
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800675
676 /* Set up entry in queue's byte count circular buffer */
Zhu Yib481de92007-09-25 17:54:57 -0700677 ret = iwl4965_tx_queue_update_wr_ptr(priv, txq, 0);
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800678
679 /* Increment and update queue's write index */
Tomas Winklerc54b6792008-03-06 17:36:53 -0800680 q->write_ptr = iwl_queue_inc_wrap(q->write_ptr, q->n_bd);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800681 iwl4965_tx_queue_update_write_ptr(priv, txq);
Zhu Yib481de92007-09-25 17:54:57 -0700682
683 spin_unlock_irqrestore(&priv->hcmd_lock, flags);
684 return ret ? ret : idx;
685}
686
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700687static int iwl4965_send_cmd_async(struct iwl_priv *priv, struct iwl4965_host_cmd *cmd)
Zhu Yib481de92007-09-25 17:54:57 -0700688{
689 int ret;
690
691 BUG_ON(!(cmd->meta.flags & CMD_ASYNC));
692
693 /* An asynchronous command can not expect an SKB to be set. */
694 BUG_ON(cmd->meta.flags & CMD_WANT_SKB);
695
696 /* An asynchronous command MUST have a callback. */
697 BUG_ON(!cmd->meta.u.callback);
698
699 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
700 return -EBUSY;
701
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800702 ret = iwl4965_enqueue_hcmd(priv, cmd);
Zhu Yib481de92007-09-25 17:54:57 -0700703 if (ret < 0) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800704 IWL_ERROR("Error sending %s: iwl4965_enqueue_hcmd failed: %d\n",
Zhu Yib481de92007-09-25 17:54:57 -0700705 get_cmd_string(cmd->id), ret);
706 return ret;
707 }
708 return 0;
709}
710
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700711static int iwl4965_send_cmd_sync(struct iwl_priv *priv, struct iwl4965_host_cmd *cmd)
Zhu Yib481de92007-09-25 17:54:57 -0700712{
713 int cmd_idx;
714 int ret;
715 static atomic_t entry = ATOMIC_INIT(0); /* reentrance protection */
716
717 BUG_ON(cmd->meta.flags & CMD_ASYNC);
718
719 /* A synchronous command can not have a callback set. */
720 BUG_ON(cmd->meta.u.callback != NULL);
721
722 if (atomic_xchg(&entry, 1)) {
723 IWL_ERROR("Error sending %s: Already sending a host command\n",
724 get_cmd_string(cmd->id));
725 return -EBUSY;
726 }
727
728 set_bit(STATUS_HCMD_ACTIVE, &priv->status);
729
730 if (cmd->meta.flags & CMD_WANT_SKB)
731 cmd->meta.source = &cmd->meta;
732
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800733 cmd_idx = iwl4965_enqueue_hcmd(priv, cmd);
Zhu Yib481de92007-09-25 17:54:57 -0700734 if (cmd_idx < 0) {
735 ret = cmd_idx;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800736 IWL_ERROR("Error sending %s: iwl4965_enqueue_hcmd failed: %d\n",
Zhu Yib481de92007-09-25 17:54:57 -0700737 get_cmd_string(cmd->id), ret);
738 goto out;
739 }
740
741 ret = wait_event_interruptible_timeout(priv->wait_command_queue,
742 !test_bit(STATUS_HCMD_ACTIVE, &priv->status),
743 HOST_COMPLETE_TIMEOUT);
744 if (!ret) {
745 if (test_bit(STATUS_HCMD_ACTIVE, &priv->status)) {
746 IWL_ERROR("Error sending %s: time out after %dms.\n",
747 get_cmd_string(cmd->id),
748 jiffies_to_msecs(HOST_COMPLETE_TIMEOUT));
749
750 clear_bit(STATUS_HCMD_ACTIVE, &priv->status);
751 ret = -ETIMEDOUT;
752 goto cancel;
753 }
754 }
755
756 if (test_bit(STATUS_RF_KILL_HW, &priv->status)) {
757 IWL_DEBUG_INFO("Command %s aborted: RF KILL Switch\n",
758 get_cmd_string(cmd->id));
759 ret = -ECANCELED;
760 goto fail;
761 }
762 if (test_bit(STATUS_FW_ERROR, &priv->status)) {
763 IWL_DEBUG_INFO("Command %s failed: FW Error\n",
764 get_cmd_string(cmd->id));
765 ret = -EIO;
766 goto fail;
767 }
768 if ((cmd->meta.flags & CMD_WANT_SKB) && !cmd->meta.u.skb) {
769 IWL_ERROR("Error: Response NULL in '%s'\n",
770 get_cmd_string(cmd->id));
771 ret = -EIO;
772 goto out;
773 }
774
775 ret = 0;
776 goto out;
777
778cancel:
779 if (cmd->meta.flags & CMD_WANT_SKB) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800780 struct iwl4965_cmd *qcmd;
Zhu Yib481de92007-09-25 17:54:57 -0700781
782 /* Cancel the CMD_WANT_SKB flag for the cmd in the
783 * TX cmd queue. Otherwise in case the cmd comes
784 * in later, it will possibly set an invalid
785 * address (cmd->meta.source). */
786 qcmd = &priv->txq[IWL_CMD_QUEUE_NUM].cmd[cmd_idx];
787 qcmd->meta.flags &= ~CMD_WANT_SKB;
788 }
789fail:
790 if (cmd->meta.u.skb) {
791 dev_kfree_skb_any(cmd->meta.u.skb);
792 cmd->meta.u.skb = NULL;
793 }
794out:
795 atomic_set(&entry, 0);
796 return ret;
797}
798
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -0700799static void iwl4965_set_rxon_hwcrypto(struct iwl_priv *priv, int hw_decrypt)
800{
801 struct iwl4965_rxon_cmd *rxon = &priv->staging_rxon;
802
803 if (hw_decrypt)
804 rxon->filter_flags &= ~RXON_FILTER_DIS_DECRYPT_MSK;
805 else
806 rxon->filter_flags |= RXON_FILTER_DIS_DECRYPT_MSK;
807
808}
809
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700810int iwl4965_send_cmd(struct iwl_priv *priv, struct iwl4965_host_cmd *cmd)
Zhu Yib481de92007-09-25 17:54:57 -0700811{
Zhu Yib481de92007-09-25 17:54:57 -0700812 if (cmd->meta.flags & CMD_ASYNC)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800813 return iwl4965_send_cmd_async(priv, cmd);
Zhu Yib481de92007-09-25 17:54:57 -0700814
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800815 return iwl4965_send_cmd_sync(priv, cmd);
Zhu Yib481de92007-09-25 17:54:57 -0700816}
817
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700818int iwl4965_send_cmd_pdu(struct iwl_priv *priv, u8 id, u16 len, const void *data)
Zhu Yib481de92007-09-25 17:54:57 -0700819{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800820 struct iwl4965_host_cmd cmd = {
Zhu Yib481de92007-09-25 17:54:57 -0700821 .id = id,
822 .len = len,
823 .data = data,
824 };
825
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800826 return iwl4965_send_cmd_sync(priv, &cmd);
Zhu Yib481de92007-09-25 17:54:57 -0700827}
828
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700829static int __must_check iwl4965_send_cmd_u32(struct iwl_priv *priv, u8 id, u32 val)
Zhu Yib481de92007-09-25 17:54:57 -0700830{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800831 struct iwl4965_host_cmd cmd = {
Zhu Yib481de92007-09-25 17:54:57 -0700832 .id = id,
833 .len = sizeof(val),
834 .data = &val,
835 };
836
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800837 return iwl4965_send_cmd_sync(priv, &cmd);
Zhu Yib481de92007-09-25 17:54:57 -0700838}
839
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700840int iwl4965_send_statistics_request(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700841{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800842 return iwl4965_send_cmd_u32(priv, REPLY_STATISTICS_CMD, 0);
Zhu Yib481de92007-09-25 17:54:57 -0700843}
844
845/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800846 * iwl4965_rxon_add_station - add station into station table.
Zhu Yib481de92007-09-25 17:54:57 -0700847 *
848 * there is only one AP station with id= IWL_AP_ID
Ben Cahill9fbab512007-11-29 11:09:47 +0800849 * NOTE: mutex must be held before calling this fnction
850 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700851static int iwl4965_rxon_add_station(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -0700852 const u8 *addr, int is_ap)
853{
Zhu Yi556f8db2007-09-27 11:27:33 +0800854 u8 sta_id;
Zhu Yib481de92007-09-25 17:54:57 -0700855
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800856 /* Add station to device's station table */
Ron Rindjunsky67d62032007-11-26 16:14:40 +0200857#ifdef CONFIG_IWL4965_HT
858 struct ieee80211_conf *conf = &priv->hw->conf;
859 struct ieee80211_ht_info *cur_ht_config = &conf->ht_conf;
860
861 if ((is_ap) &&
862 (conf->flags & IEEE80211_CONF_SUPPORT_HT_MODE) &&
863 (priv->iw_mode == IEEE80211_IF_TYPE_STA))
864 sta_id = iwl4965_add_station_flags(priv, addr, is_ap,
865 0, cur_ht_config);
866 else
867#endif /* CONFIG_IWL4965_HT */
868 sta_id = iwl4965_add_station_flags(priv, addr, is_ap,
869 0, NULL);
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800870
871 /* Set up default rate scaling table in device's station table */
Zhu Yib481de92007-09-25 17:54:57 -0700872 iwl4965_add_station(priv, addr, is_ap);
873
Zhu Yi556f8db2007-09-27 11:27:33 +0800874 return sta_id;
Zhu Yib481de92007-09-25 17:54:57 -0700875}
876
877/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800878 * iwl4965_check_rxon_cmd - validate RXON structure is valid
Zhu Yib481de92007-09-25 17:54:57 -0700879 *
880 * NOTE: This is really only useful during development and can eventually
881 * be #ifdef'd out once the driver is stable and folks aren't actively
882 * making changes
883 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800884static int iwl4965_check_rxon_cmd(struct iwl4965_rxon_cmd *rxon)
Zhu Yib481de92007-09-25 17:54:57 -0700885{
886 int error = 0;
887 int counter = 1;
888
889 if (rxon->flags & RXON_FLG_BAND_24G_MSK) {
890 error |= le32_to_cpu(rxon->flags &
891 (RXON_FLG_TGJ_NARROW_BAND_MSK |
892 RXON_FLG_RADAR_DETECT_MSK));
893 if (error)
894 IWL_WARNING("check 24G fields %d | %d\n",
895 counter++, error);
896 } else {
897 error |= (rxon->flags & RXON_FLG_SHORT_SLOT_MSK) ?
898 0 : le32_to_cpu(RXON_FLG_SHORT_SLOT_MSK);
899 if (error)
900 IWL_WARNING("check 52 fields %d | %d\n",
901 counter++, error);
902 error |= le32_to_cpu(rxon->flags & RXON_FLG_CCK_MSK);
903 if (error)
904 IWL_WARNING("check 52 CCK %d | %d\n",
905 counter++, error);
906 }
907 error |= (rxon->node_addr[0] | rxon->bssid_addr[0]) & 0x1;
908 if (error)
909 IWL_WARNING("check mac addr %d | %d\n", counter++, error);
910
911 /* make sure basic rates 6Mbps and 1Mbps are supported */
912 error |= (((rxon->ofdm_basic_rates & IWL_RATE_6M_MASK) == 0) &&
913 ((rxon->cck_basic_rates & IWL_RATE_1M_MASK) == 0));
914 if (error)
915 IWL_WARNING("check basic rate %d | %d\n", counter++, error);
916
917 error |= (le16_to_cpu(rxon->assoc_id) > 2007);
918 if (error)
919 IWL_WARNING("check assoc id %d | %d\n", counter++, error);
920
921 error |= ((rxon->flags & (RXON_FLG_CCK_MSK | RXON_FLG_SHORT_SLOT_MSK))
922 == (RXON_FLG_CCK_MSK | RXON_FLG_SHORT_SLOT_MSK));
923 if (error)
924 IWL_WARNING("check CCK and short slot %d | %d\n",
925 counter++, error);
926
927 error |= ((rxon->flags & (RXON_FLG_CCK_MSK | RXON_FLG_AUTO_DETECT_MSK))
928 == (RXON_FLG_CCK_MSK | RXON_FLG_AUTO_DETECT_MSK));
929 if (error)
930 IWL_WARNING("check CCK & auto detect %d | %d\n",
931 counter++, error);
932
933 error |= ((rxon->flags & (RXON_FLG_AUTO_DETECT_MSK |
934 RXON_FLG_TGG_PROTECT_MSK)) == RXON_FLG_TGG_PROTECT_MSK);
935 if (error)
936 IWL_WARNING("check TGG and auto detect %d | %d\n",
937 counter++, error);
938
939 if (error)
940 IWL_WARNING("Tuning to channel %d\n",
941 le16_to_cpu(rxon->channel));
942
943 if (error) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800944 IWL_ERROR("Not a valid iwl4965_rxon_assoc_cmd field values\n");
Zhu Yib481de92007-09-25 17:54:57 -0700945 return -1;
946 }
947 return 0;
948}
949
950/**
Ben Cahill9fbab512007-11-29 11:09:47 +0800951 * iwl4965_full_rxon_required - check if full RXON (vs RXON_ASSOC) cmd is needed
Ian Schram01ebd062007-10-25 17:15:22 +0800952 * @priv: staging_rxon is compared to active_rxon
Zhu Yib481de92007-09-25 17:54:57 -0700953 *
Ben Cahill9fbab512007-11-29 11:09:47 +0800954 * If the RXON structure is changing enough to require a new tune,
955 * or is clearing the RXON_FILTER_ASSOC_MSK, then return 1 to indicate that
956 * a new tune (full RXON command, rather than RXON_ASSOC cmd) is required.
Zhu Yib481de92007-09-25 17:54:57 -0700957 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700958static int iwl4965_full_rxon_required(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700959{
960
961 /* These items are only settable from the full RXON command */
962 if (!(priv->active_rxon.filter_flags & RXON_FILTER_ASSOC_MSK) ||
963 compare_ether_addr(priv->staging_rxon.bssid_addr,
964 priv->active_rxon.bssid_addr) ||
965 compare_ether_addr(priv->staging_rxon.node_addr,
966 priv->active_rxon.node_addr) ||
967 compare_ether_addr(priv->staging_rxon.wlap_bssid_addr,
968 priv->active_rxon.wlap_bssid_addr) ||
969 (priv->staging_rxon.dev_type != priv->active_rxon.dev_type) ||
970 (priv->staging_rxon.channel != priv->active_rxon.channel) ||
971 (priv->staging_rxon.air_propagation !=
972 priv->active_rxon.air_propagation) ||
973 (priv->staging_rxon.ofdm_ht_single_stream_basic_rates !=
974 priv->active_rxon.ofdm_ht_single_stream_basic_rates) ||
975 (priv->staging_rxon.ofdm_ht_dual_stream_basic_rates !=
976 priv->active_rxon.ofdm_ht_dual_stream_basic_rates) ||
977 (priv->staging_rxon.rx_chain != priv->active_rxon.rx_chain) ||
978 (priv->staging_rxon.assoc_id != priv->active_rxon.assoc_id))
979 return 1;
980
981 /* flags, filter_flags, ofdm_basic_rates, and cck_basic_rates can
982 * be updated with the RXON_ASSOC command -- however only some
983 * flag transitions are allowed using RXON_ASSOC */
984
985 /* Check if we are not switching bands */
986 if ((priv->staging_rxon.flags & RXON_FLG_BAND_24G_MSK) !=
987 (priv->active_rxon.flags & RXON_FLG_BAND_24G_MSK))
988 return 1;
989
990 /* Check if we are switching association toggle */
991 if ((priv->staging_rxon.filter_flags & RXON_FILTER_ASSOC_MSK) !=
992 (priv->active_rxon.filter_flags & RXON_FILTER_ASSOC_MSK))
993 return 1;
994
995 return 0;
996}
997
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700998static int iwl4965_send_rxon_assoc(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700999{
1000 int rc = 0;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001001 struct iwl4965_rx_packet *res = NULL;
1002 struct iwl4965_rxon_assoc_cmd rxon_assoc;
1003 struct iwl4965_host_cmd cmd = {
Zhu Yib481de92007-09-25 17:54:57 -07001004 .id = REPLY_RXON_ASSOC,
1005 .len = sizeof(rxon_assoc),
1006 .meta.flags = CMD_WANT_SKB,
1007 .data = &rxon_assoc,
1008 };
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001009 const struct iwl4965_rxon_cmd *rxon1 = &priv->staging_rxon;
1010 const struct iwl4965_rxon_cmd *rxon2 = &priv->active_rxon;
Zhu Yib481de92007-09-25 17:54:57 -07001011
1012 if ((rxon1->flags == rxon2->flags) &&
1013 (rxon1->filter_flags == rxon2->filter_flags) &&
1014 (rxon1->cck_basic_rates == rxon2->cck_basic_rates) &&
1015 (rxon1->ofdm_ht_single_stream_basic_rates ==
1016 rxon2->ofdm_ht_single_stream_basic_rates) &&
1017 (rxon1->ofdm_ht_dual_stream_basic_rates ==
1018 rxon2->ofdm_ht_dual_stream_basic_rates) &&
1019 (rxon1->rx_chain == rxon2->rx_chain) &&
1020 (rxon1->ofdm_basic_rates == rxon2->ofdm_basic_rates)) {
1021 IWL_DEBUG_INFO("Using current RXON_ASSOC. Not resending.\n");
1022 return 0;
1023 }
1024
1025 rxon_assoc.flags = priv->staging_rxon.flags;
1026 rxon_assoc.filter_flags = priv->staging_rxon.filter_flags;
1027 rxon_assoc.ofdm_basic_rates = priv->staging_rxon.ofdm_basic_rates;
1028 rxon_assoc.cck_basic_rates = priv->staging_rxon.cck_basic_rates;
1029 rxon_assoc.reserved = 0;
1030 rxon_assoc.ofdm_ht_single_stream_basic_rates =
1031 priv->staging_rxon.ofdm_ht_single_stream_basic_rates;
1032 rxon_assoc.ofdm_ht_dual_stream_basic_rates =
1033 priv->staging_rxon.ofdm_ht_dual_stream_basic_rates;
1034 rxon_assoc.rx_chain_select_flags = priv->staging_rxon.rx_chain;
1035
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001036 rc = iwl4965_send_cmd_sync(priv, &cmd);
Zhu Yib481de92007-09-25 17:54:57 -07001037 if (rc)
1038 return rc;
1039
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001040 res = (struct iwl4965_rx_packet *)cmd.meta.u.skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07001041 if (res->hdr.flags & IWL_CMD_FAILED_MSK) {
1042 IWL_ERROR("Bad return from REPLY_RXON_ASSOC command\n");
1043 rc = -EIO;
1044 }
1045
1046 priv->alloc_rxb_skb--;
1047 dev_kfree_skb_any(cmd.meta.u.skb);
1048
1049 return rc;
1050}
1051
1052/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001053 * iwl4965_commit_rxon - commit staging_rxon to hardware
Zhu Yib481de92007-09-25 17:54:57 -07001054 *
Ian Schram01ebd062007-10-25 17:15:22 +08001055 * The RXON command in staging_rxon is committed to the hardware and
Zhu Yib481de92007-09-25 17:54:57 -07001056 * the active_rxon structure is updated with the new data. This
1057 * function correctly transitions out of the RXON_ASSOC_MSK state if
1058 * a HW tune is required based on the RXON structure changes.
1059 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001060static int iwl4965_commit_rxon(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001061{
1062 /* cast away the const for active_rxon in this function */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001063 struct iwl4965_rxon_cmd *active_rxon = (void *)&priv->active_rxon;
Joe Perches0795af52007-10-03 17:59:30 -07001064 DECLARE_MAC_BUF(mac);
Zhu Yib481de92007-09-25 17:54:57 -07001065 int rc = 0;
1066
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001067 if (!iwl4965_is_alive(priv))
Zhu Yib481de92007-09-25 17:54:57 -07001068 return -1;
1069
1070 /* always get timestamp with Rx frame */
1071 priv->staging_rxon.flags |= RXON_FLG_TSF2HOST_MSK;
1072
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001073 rc = iwl4965_check_rxon_cmd(&priv->staging_rxon);
Zhu Yib481de92007-09-25 17:54:57 -07001074 if (rc) {
1075 IWL_ERROR("Invalid RXON configuration. Not committing.\n");
1076 return -EINVAL;
1077 }
1078
1079 /* If we don't need to send a full RXON, we can use
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001080 * iwl4965_rxon_assoc_cmd which is used to reconfigure filter
Zhu Yib481de92007-09-25 17:54:57 -07001081 * and other flags for the current radio configuration. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001082 if (!iwl4965_full_rxon_required(priv)) {
1083 rc = iwl4965_send_rxon_assoc(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001084 if (rc) {
1085 IWL_ERROR("Error setting RXON_ASSOC "
1086 "configuration (%d).\n", rc);
1087 return rc;
1088 }
1089
1090 memcpy(active_rxon, &priv->staging_rxon, sizeof(*active_rxon));
1091
1092 return 0;
1093 }
1094
1095 /* station table will be cleared */
1096 priv->assoc_station_added = 0;
1097
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08001098#ifdef CONFIG_IWL4965_SENSITIVITY
Zhu Yib481de92007-09-25 17:54:57 -07001099 priv->sensitivity_data.state = IWL_SENS_CALIB_NEED_REINIT;
1100 if (!priv->error_recovering)
1101 priv->start_calib = 0;
1102
1103 iwl4965_init_sensitivity(priv, CMD_ASYNC, 1);
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08001104#endif /* CONFIG_IWL4965_SENSITIVITY */
Zhu Yib481de92007-09-25 17:54:57 -07001105
1106 /* If we are currently associated and the new config requires
1107 * an RXON_ASSOC and the new config wants the associated mask enabled,
1108 * we must clear the associated from the active configuration
1109 * before we apply the new config */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001110 if (iwl4965_is_associated(priv) &&
Zhu Yib481de92007-09-25 17:54:57 -07001111 (priv->staging_rxon.filter_flags & RXON_FILTER_ASSOC_MSK)) {
1112 IWL_DEBUG_INFO("Toggling associated bit on current RXON\n");
1113 active_rxon->filter_flags &= ~RXON_FILTER_ASSOC_MSK;
1114
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001115 rc = iwl4965_send_cmd_pdu(priv, REPLY_RXON,
1116 sizeof(struct iwl4965_rxon_cmd),
Zhu Yib481de92007-09-25 17:54:57 -07001117 &priv->active_rxon);
1118
1119 /* If the mask clearing failed then we set
1120 * active_rxon back to what it was previously */
1121 if (rc) {
1122 active_rxon->filter_flags |= RXON_FILTER_ASSOC_MSK;
1123 IWL_ERROR("Error clearing ASSOC_MSK on current "
1124 "configuration (%d).\n", rc);
1125 return rc;
1126 }
Zhu Yib481de92007-09-25 17:54:57 -07001127 }
1128
1129 IWL_DEBUG_INFO("Sending RXON\n"
1130 "* with%s RXON_FILTER_ASSOC_MSK\n"
1131 "* channel = %d\n"
Joe Perches0795af52007-10-03 17:59:30 -07001132 "* bssid = %s\n",
Zhu Yib481de92007-09-25 17:54:57 -07001133 ((priv->staging_rxon.filter_flags &
1134 RXON_FILTER_ASSOC_MSK) ? "" : "out"),
1135 le16_to_cpu(priv->staging_rxon.channel),
Joe Perches0795af52007-10-03 17:59:30 -07001136 print_mac(mac, priv->staging_rxon.bssid_addr));
Zhu Yib481de92007-09-25 17:54:57 -07001137
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07001138 iwl4965_set_rxon_hwcrypto(priv, priv->cfg->mod_params->hw_crypto);
Zhu Yib481de92007-09-25 17:54:57 -07001139 /* Apply the new configuration */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001140 rc = iwl4965_send_cmd_pdu(priv, REPLY_RXON,
1141 sizeof(struct iwl4965_rxon_cmd), &priv->staging_rxon);
Zhu Yib481de92007-09-25 17:54:57 -07001142 if (rc) {
1143 IWL_ERROR("Error setting new configuration (%d).\n", rc);
1144 return rc;
1145 }
1146
Assaf Kraussbf85ea42008-03-14 10:38:49 -07001147 iwlcore_clear_stations_table(priv);
Zhu Yi556f8db2007-09-27 11:27:33 +08001148
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08001149#ifdef CONFIG_IWL4965_SENSITIVITY
Zhu Yib481de92007-09-25 17:54:57 -07001150 if (!priv->error_recovering)
1151 priv->start_calib = 0;
1152
1153 priv->sensitivity_data.state = IWL_SENS_CALIB_NEED_REINIT;
1154 iwl4965_init_sensitivity(priv, CMD_ASYNC, 1);
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08001155#endif /* CONFIG_IWL4965_SENSITIVITY */
Zhu Yib481de92007-09-25 17:54:57 -07001156
1157 memcpy(active_rxon, &priv->staging_rxon, sizeof(*active_rxon));
1158
1159 /* If we issue a new RXON command which required a tune then we must
1160 * send a new TXPOWER command or we won't be able to Tx any frames */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001161 rc = iwl4965_hw_reg_send_txpower(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001162 if (rc) {
1163 IWL_ERROR("Error setting Tx power (%d).\n", rc);
1164 return rc;
1165 }
1166
1167 /* Add the broadcast address so we can send broadcast frames */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001168 if (iwl4965_rxon_add_station(priv, iwl4965_broadcast_addr, 0) ==
Zhu Yib481de92007-09-25 17:54:57 -07001169 IWL_INVALID_STATION) {
1170 IWL_ERROR("Error adding BROADCAST address for transmit.\n");
1171 return -EIO;
1172 }
1173
1174 /* If we have set the ASSOC_MSK and we are in BSS mode then
1175 * add the IWL_AP_ID to the station rate table */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001176 if (iwl4965_is_associated(priv) &&
Zhu Yib481de92007-09-25 17:54:57 -07001177 (priv->iw_mode == IEEE80211_IF_TYPE_STA)) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001178 if (iwl4965_rxon_add_station(priv, priv->active_rxon.bssid_addr, 1)
Zhu Yib481de92007-09-25 17:54:57 -07001179 == IWL_INVALID_STATION) {
1180 IWL_ERROR("Error adding AP address for transmit.\n");
1181 return -EIO;
1182 }
1183 priv->assoc_station_added = 1;
1184 }
1185
1186 return 0;
1187}
1188
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001189static int iwl4965_send_bt_config(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001190{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001191 struct iwl4965_bt_cmd bt_cmd = {
Zhu Yib481de92007-09-25 17:54:57 -07001192 .flags = 3,
1193 .lead_time = 0xAA,
1194 .max_kill = 1,
1195 .kill_ack_mask = 0,
1196 .kill_cts_mask = 0,
1197 };
1198
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001199 return iwl4965_send_cmd_pdu(priv, REPLY_BT_CONFIG,
1200 sizeof(struct iwl4965_bt_cmd), &bt_cmd);
Zhu Yib481de92007-09-25 17:54:57 -07001201}
1202
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001203static int iwl4965_send_scan_abort(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001204{
1205 int rc = 0;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001206 struct iwl4965_rx_packet *res;
1207 struct iwl4965_host_cmd cmd = {
Zhu Yib481de92007-09-25 17:54:57 -07001208 .id = REPLY_SCAN_ABORT_CMD,
1209 .meta.flags = CMD_WANT_SKB,
1210 };
1211
1212 /* If there isn't a scan actively going on in the hardware
1213 * then we are in between scan bands and not actually
1214 * actively scanning, so don't send the abort command */
1215 if (!test_bit(STATUS_SCAN_HW, &priv->status)) {
1216 clear_bit(STATUS_SCAN_ABORTING, &priv->status);
1217 return 0;
1218 }
1219
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001220 rc = iwl4965_send_cmd_sync(priv, &cmd);
Zhu Yib481de92007-09-25 17:54:57 -07001221 if (rc) {
1222 clear_bit(STATUS_SCAN_ABORTING, &priv->status);
1223 return rc;
1224 }
1225
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001226 res = (struct iwl4965_rx_packet *)cmd.meta.u.skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07001227 if (res->u.status != CAN_ABORT_STATUS) {
1228 /* The scan abort will return 1 for success or
1229 * 2 for "failure". A failure condition can be
1230 * due to simply not being in an active scan which
1231 * can occur if we send the scan abort before we
1232 * the microcode has notified us that a scan is
1233 * completed. */
1234 IWL_DEBUG_INFO("SCAN_ABORT returned %d.\n", res->u.status);
1235 clear_bit(STATUS_SCAN_ABORTING, &priv->status);
1236 clear_bit(STATUS_SCAN_HW, &priv->status);
1237 }
1238
1239 dev_kfree_skb_any(cmd.meta.u.skb);
1240
1241 return rc;
1242}
1243
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001244static int iwl4965_card_state_sync_callback(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001245 struct iwl4965_cmd *cmd,
Zhu Yib481de92007-09-25 17:54:57 -07001246 struct sk_buff *skb)
1247{
1248 return 1;
1249}
1250
1251/*
1252 * CARD_STATE_CMD
1253 *
Ben Cahill9fbab512007-11-29 11:09:47 +08001254 * Use: Sets the device's internal card state to enable, disable, or halt
Zhu Yib481de92007-09-25 17:54:57 -07001255 *
1256 * When in the 'enable' state the card operates as normal.
1257 * When in the 'disable' state, the card enters into a low power mode.
1258 * When in the 'halt' state, the card is shut down and must be fully
1259 * restarted to come back on.
1260 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001261static int iwl4965_send_card_state(struct iwl_priv *priv, u32 flags, u8 meta_flag)
Zhu Yib481de92007-09-25 17:54:57 -07001262{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001263 struct iwl4965_host_cmd cmd = {
Zhu Yib481de92007-09-25 17:54:57 -07001264 .id = REPLY_CARD_STATE_CMD,
1265 .len = sizeof(u32),
1266 .data = &flags,
1267 .meta.flags = meta_flag,
1268 };
1269
1270 if (meta_flag & CMD_ASYNC)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001271 cmd.meta.u.callback = iwl4965_card_state_sync_callback;
Zhu Yib481de92007-09-25 17:54:57 -07001272
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001273 return iwl4965_send_cmd(priv, &cmd);
Zhu Yib481de92007-09-25 17:54:57 -07001274}
1275
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001276static int iwl4965_add_sta_sync_callback(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001277 struct iwl4965_cmd *cmd, struct sk_buff *skb)
Zhu Yib481de92007-09-25 17:54:57 -07001278{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001279 struct iwl4965_rx_packet *res = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07001280
1281 if (!skb) {
1282 IWL_ERROR("Error: Response NULL in REPLY_ADD_STA.\n");
1283 return 1;
1284 }
1285
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001286 res = (struct iwl4965_rx_packet *)skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07001287 if (res->hdr.flags & IWL_CMD_FAILED_MSK) {
1288 IWL_ERROR("Bad return from REPLY_ADD_STA (0x%08X)\n",
1289 res->hdr.flags);
1290 return 1;
1291 }
1292
1293 switch (res->u.add_sta.status) {
1294 case ADD_STA_SUCCESS_MSK:
1295 break;
1296 default:
1297 break;
1298 }
1299
1300 /* We didn't cache the SKB; let the caller free it */
1301 return 1;
1302}
1303
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001304int iwl4965_send_add_station(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001305 struct iwl4965_addsta_cmd *sta, u8 flags)
Zhu Yib481de92007-09-25 17:54:57 -07001306{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001307 struct iwl4965_rx_packet *res = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07001308 int rc = 0;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001309 struct iwl4965_host_cmd cmd = {
Zhu Yib481de92007-09-25 17:54:57 -07001310 .id = REPLY_ADD_STA,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001311 .len = sizeof(struct iwl4965_addsta_cmd),
Zhu Yib481de92007-09-25 17:54:57 -07001312 .meta.flags = flags,
1313 .data = sta,
1314 };
1315
1316 if (flags & CMD_ASYNC)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001317 cmd.meta.u.callback = iwl4965_add_sta_sync_callback;
Zhu Yib481de92007-09-25 17:54:57 -07001318 else
1319 cmd.meta.flags |= CMD_WANT_SKB;
1320
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001321 rc = iwl4965_send_cmd(priv, &cmd);
Zhu Yib481de92007-09-25 17:54:57 -07001322
1323 if (rc || (flags & CMD_ASYNC))
1324 return rc;
1325
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001326 res = (struct iwl4965_rx_packet *)cmd.meta.u.skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07001327 if (res->hdr.flags & IWL_CMD_FAILED_MSK) {
1328 IWL_ERROR("Bad return from REPLY_ADD_STA (0x%08X)\n",
1329 res->hdr.flags);
1330 rc = -EIO;
1331 }
1332
1333 if (rc == 0) {
1334 switch (res->u.add_sta.status) {
1335 case ADD_STA_SUCCESS_MSK:
1336 IWL_DEBUG_INFO("REPLY_ADD_STA PASSED\n");
1337 break;
1338 default:
1339 rc = -EIO;
1340 IWL_WARNING("REPLY_ADD_STA failed\n");
1341 break;
1342 }
1343 }
1344
1345 priv->alloc_rxb_skb--;
1346 dev_kfree_skb_any(cmd.meta.u.skb);
1347
1348 return rc;
1349}
1350
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07001351static int iwl4965_set_ccmp_dynamic_key_info(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07001352 struct ieee80211_key_conf *keyconf,
1353 u8 sta_id)
1354{
1355 unsigned long flags;
1356 __le16 key_flags = 0;
1357
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07001358 key_flags |= (STA_KEY_FLG_CCMP | STA_KEY_FLG_MAP_KEY_MSK);
1359 key_flags |= cpu_to_le16(keyconf->keyidx << STA_KEY_FLG_KEYID_POS);
1360
1361 if (sta_id == priv->hw_setting.bcast_sta_id)
1362 key_flags |= STA_KEY_MULTICAST_MSK;
1363
1364 keyconf->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
1365 keyconf->hw_key_idx = keyconf->keyidx;
1366
1367 key_flags &= ~STA_KEY_FLG_INVALID;
1368
Zhu Yib481de92007-09-25 17:54:57 -07001369 spin_lock_irqsave(&priv->sta_lock, flags);
1370 priv->stations[sta_id].keyinfo.alg = keyconf->alg;
1371 priv->stations[sta_id].keyinfo.keylen = keyconf->keylen;
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07001372
Zhu Yib481de92007-09-25 17:54:57 -07001373 memcpy(priv->stations[sta_id].keyinfo.key, keyconf->key,
1374 keyconf->keylen);
1375
1376 memcpy(priv->stations[sta_id].sta.key.key, keyconf->key,
1377 keyconf->keylen);
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07001378
1379 priv->stations[sta_id].sta.key.key_offset
1380 = (sta_id % STA_KEY_MAX_NUM);/*FIXME*/
Zhu Yib481de92007-09-25 17:54:57 -07001381 priv->stations[sta_id].sta.key.key_flags = key_flags;
1382 priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
1383 priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
1384
1385 spin_unlock_irqrestore(&priv->sta_lock, flags);
1386
1387 IWL_DEBUG_INFO("hwcrypto: modify ucode station key info\n");
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07001388 return iwl4965_send_add_station(priv,
1389 &priv->stations[sta_id].sta, CMD_ASYNC);
1390}
1391
1392static int iwl4965_set_tkip_dynamic_key_info(struct iwl_priv *priv,
1393 struct ieee80211_key_conf *keyconf,
1394 u8 sta_id)
1395{
1396 return -EOPNOTSUPP;
Zhu Yib481de92007-09-25 17:54:57 -07001397}
1398
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001399static int iwl4965_clear_sta_key_info(struct iwl_priv *priv, u8 sta_id)
Zhu Yib481de92007-09-25 17:54:57 -07001400{
1401 unsigned long flags;
1402
1403 spin_lock_irqsave(&priv->sta_lock, flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001404 memset(&priv->stations[sta_id].keyinfo, 0, sizeof(struct iwl4965_hw_key));
1405 memset(&priv->stations[sta_id].sta.key, 0, sizeof(struct iwl4965_keyinfo));
Zhu Yib481de92007-09-25 17:54:57 -07001406 priv->stations[sta_id].sta.key.key_flags = STA_KEY_FLG_NO_ENC;
1407 priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
1408 priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
1409 spin_unlock_irqrestore(&priv->sta_lock, flags);
1410
1411 IWL_DEBUG_INFO("hwcrypto: clear ucode station key info\n");
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001412 iwl4965_send_add_station(priv, &priv->stations[sta_id].sta, 0);
Zhu Yib481de92007-09-25 17:54:57 -07001413 return 0;
1414}
1415
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07001416static int iwl4965_set_dynamic_key(struct iwl_priv *priv,
1417 struct ieee80211_key_conf *key, u8 sta_id)
1418{
1419 int ret;
1420
1421 switch (key->alg) {
1422 case ALG_CCMP:
1423 ret = iwl4965_set_ccmp_dynamic_key_info(priv, key, sta_id);
1424 break;
1425 case ALG_TKIP:
1426 ret = iwl4965_set_tkip_dynamic_key_info(priv, key, sta_id);
1427 break;
1428 case ALG_WEP:
1429 ret = -EOPNOTSUPP;
1430 break;
1431 default:
1432 IWL_ERROR("Unknown alg: %s alg = %d\n", __func__, key->alg);
1433 ret = -EINVAL;
1434 }
1435
1436 return ret;
1437}
1438
1439static int iwl4965_remove_static_key(struct iwl_priv *priv)
1440{
1441 int ret = -EOPNOTSUPP;
1442
1443 return ret;
1444}
1445
1446static int iwl4965_set_static_key(struct iwl_priv *priv,
1447 struct ieee80211_key_conf *key)
1448{
1449 if (key->alg == ALG_WEP)
1450 return -EOPNOTSUPP;
1451
1452 IWL_ERROR("Static key invalid: alg %d\n", key->alg);
1453 return -EINVAL;
1454}
1455
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001456static void iwl4965_clear_free_frames(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001457{
1458 struct list_head *element;
1459
1460 IWL_DEBUG_INFO("%d frames on pre-allocated heap on clear.\n",
1461 priv->frames_count);
1462
1463 while (!list_empty(&priv->free_frames)) {
1464 element = priv->free_frames.next;
1465 list_del(element);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001466 kfree(list_entry(element, struct iwl4965_frame, list));
Zhu Yib481de92007-09-25 17:54:57 -07001467 priv->frames_count--;
1468 }
1469
1470 if (priv->frames_count) {
1471 IWL_WARNING("%d frames still in use. Did we lose one?\n",
1472 priv->frames_count);
1473 priv->frames_count = 0;
1474 }
1475}
1476
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001477static struct iwl4965_frame *iwl4965_get_free_frame(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001478{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001479 struct iwl4965_frame *frame;
Zhu Yib481de92007-09-25 17:54:57 -07001480 struct list_head *element;
1481 if (list_empty(&priv->free_frames)) {
1482 frame = kzalloc(sizeof(*frame), GFP_KERNEL);
1483 if (!frame) {
1484 IWL_ERROR("Could not allocate frame!\n");
1485 return NULL;
1486 }
1487
1488 priv->frames_count++;
1489 return frame;
1490 }
1491
1492 element = priv->free_frames.next;
1493 list_del(element);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001494 return list_entry(element, struct iwl4965_frame, list);
Zhu Yib481de92007-09-25 17:54:57 -07001495}
1496
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001497static void iwl4965_free_frame(struct iwl_priv *priv, struct iwl4965_frame *frame)
Zhu Yib481de92007-09-25 17:54:57 -07001498{
1499 memset(frame, 0, sizeof(*frame));
1500 list_add(&frame->list, &priv->free_frames);
1501}
1502
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001503unsigned int iwl4965_fill_beacon_frame(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07001504 struct ieee80211_hdr *hdr,
1505 const u8 *dest, int left)
1506{
1507
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001508 if (!iwl4965_is_associated(priv) || !priv->ibss_beacon ||
Zhu Yib481de92007-09-25 17:54:57 -07001509 ((priv->iw_mode != IEEE80211_IF_TYPE_IBSS) &&
1510 (priv->iw_mode != IEEE80211_IF_TYPE_AP)))
1511 return 0;
1512
1513 if (priv->ibss_beacon->len > left)
1514 return 0;
1515
1516 memcpy(hdr, priv->ibss_beacon->data, priv->ibss_beacon->len);
1517
1518 return priv->ibss_beacon->len;
1519}
1520
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001521static u8 iwl4965_rate_get_lowest_plcp(int rate_mask)
Zhu Yib481de92007-09-25 17:54:57 -07001522{
1523 u8 i;
1524
1525 for (i = IWL_RATE_1M_INDEX; i != IWL_RATE_INVALID;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001526 i = iwl4965_rates[i].next_ieee) {
Zhu Yib481de92007-09-25 17:54:57 -07001527 if (rate_mask & (1 << i))
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001528 return iwl4965_rates[i].plcp;
Zhu Yib481de92007-09-25 17:54:57 -07001529 }
1530
1531 return IWL_RATE_INVALID;
1532}
1533
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001534static int iwl4965_send_beacon_cmd(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001535{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001536 struct iwl4965_frame *frame;
Zhu Yib481de92007-09-25 17:54:57 -07001537 unsigned int frame_size;
1538 int rc;
1539 u8 rate;
1540
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001541 frame = iwl4965_get_free_frame(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001542
1543 if (!frame) {
1544 IWL_ERROR("Could not obtain free frame buffer for beacon "
1545 "command.\n");
1546 return -ENOMEM;
1547 }
1548
1549 if (!(priv->staging_rxon.flags & RXON_FLG_BAND_24G_MSK)) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001550 rate = iwl4965_rate_get_lowest_plcp(priv->active_rate_basic &
Zhu Yib481de92007-09-25 17:54:57 -07001551 0xFF0);
1552 if (rate == IWL_INVALID_RATE)
1553 rate = IWL_RATE_6M_PLCP;
1554 } else {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001555 rate = iwl4965_rate_get_lowest_plcp(priv->active_rate_basic & 0xF);
Zhu Yib481de92007-09-25 17:54:57 -07001556 if (rate == IWL_INVALID_RATE)
1557 rate = IWL_RATE_1M_PLCP;
1558 }
1559
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001560 frame_size = iwl4965_hw_get_beacon_cmd(priv, frame, rate);
Zhu Yib481de92007-09-25 17:54:57 -07001561
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001562 rc = iwl4965_send_cmd_pdu(priv, REPLY_TX_BEACON, frame_size,
Zhu Yib481de92007-09-25 17:54:57 -07001563 &frame->u.cmd[0]);
1564
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001565 iwl4965_free_frame(priv, frame);
Zhu Yib481de92007-09-25 17:54:57 -07001566
1567 return rc;
1568}
1569
1570/******************************************************************************
1571 *
Zhu Yib481de92007-09-25 17:54:57 -07001572 * Misc. internal state and helper functions
1573 *
1574 ******************************************************************************/
Zhu Yib481de92007-09-25 17:54:57 -07001575
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001576static void iwl4965_unset_hw_setting(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001577{
1578 if (priv->hw_setting.shared_virt)
1579 pci_free_consistent(priv->pci_dev,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001580 sizeof(struct iwl4965_shared),
Zhu Yib481de92007-09-25 17:54:57 -07001581 priv->hw_setting.shared_virt,
1582 priv->hw_setting.shared_phys);
1583}
1584
1585/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001586 * iwl4965_supported_rate_to_ie - fill in the supported rate in IE field
Zhu Yib481de92007-09-25 17:54:57 -07001587 *
1588 * return : set the bit for each supported rate insert in ie
1589 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001590static u16 iwl4965_supported_rate_to_ie(u8 *ie, u16 supported_rate,
Tomas Winklerc7c46672007-10-18 02:04:15 +02001591 u16 basic_rate, int *left)
Zhu Yib481de92007-09-25 17:54:57 -07001592{
1593 u16 ret_rates = 0, bit;
1594 int i;
Tomas Winklerc7c46672007-10-18 02:04:15 +02001595 u8 *cnt = ie;
1596 u8 *rates = ie + 1;
Zhu Yib481de92007-09-25 17:54:57 -07001597
1598 for (bit = 1, i = 0; i < IWL_RATE_COUNT; i++, bit <<= 1) {
1599 if (bit & supported_rate) {
1600 ret_rates |= bit;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001601 rates[*cnt] = iwl4965_rates[i].ieee |
Tomas Winklerc7c46672007-10-18 02:04:15 +02001602 ((bit & basic_rate) ? 0x80 : 0x00);
1603 (*cnt)++;
1604 (*left)--;
1605 if ((*left <= 0) ||
1606 (*cnt >= IWL_SUPPORTED_RATES_IE_LEN))
Zhu Yib481de92007-09-25 17:54:57 -07001607 break;
1608 }
1609 }
1610
1611 return ret_rates;
1612}
1613
Zhu Yib481de92007-09-25 17:54:57 -07001614/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001615 * iwl4965_fill_probe_req - fill in all required fields and IE for probe request
Zhu Yib481de92007-09-25 17:54:57 -07001616 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001617static u16 iwl4965_fill_probe_req(struct iwl_priv *priv,
Tomas Winkler78330fd2008-02-06 02:37:18 +02001618 enum ieee80211_band band,
1619 struct ieee80211_mgmt *frame,
1620 int left, int is_direct)
Zhu Yib481de92007-09-25 17:54:57 -07001621{
1622 int len = 0;
1623 u8 *pos = NULL;
mabbasbee488d2007-10-25 17:15:42 +08001624 u16 active_rates, ret_rates, cck_rates, active_rate_basic;
Ron Rindjunsky8fb88032007-11-26 16:14:38 +02001625#ifdef CONFIG_IWL4965_HT
Tomas Winkler78330fd2008-02-06 02:37:18 +02001626 const struct ieee80211_supported_band *sband =
1627 iwl4965_get_hw_mode(priv, band);
Ron Rindjunsky8fb88032007-11-26 16:14:38 +02001628#endif /* CONFIG_IWL4965_HT */
Zhu Yib481de92007-09-25 17:54:57 -07001629
1630 /* Make sure there is enough space for the probe request,
1631 * two mandatory IEs and the data */
1632 left -= 24;
1633 if (left < 0)
1634 return 0;
1635 len += 24;
1636
1637 frame->frame_control = cpu_to_le16(IEEE80211_STYPE_PROBE_REQ);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001638 memcpy(frame->da, iwl4965_broadcast_addr, ETH_ALEN);
Zhu Yib481de92007-09-25 17:54:57 -07001639 memcpy(frame->sa, priv->mac_addr, ETH_ALEN);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001640 memcpy(frame->bssid, iwl4965_broadcast_addr, ETH_ALEN);
Zhu Yib481de92007-09-25 17:54:57 -07001641 frame->seq_ctrl = 0;
1642
1643 /* fill in our indirect SSID IE */
1644 /* ...next IE... */
1645
1646 left -= 2;
1647 if (left < 0)
1648 return 0;
1649 len += 2;
1650 pos = &(frame->u.probe_req.variable[0]);
1651 *pos++ = WLAN_EID_SSID;
1652 *pos++ = 0;
1653
1654 /* fill in our direct SSID IE... */
1655 if (is_direct) {
1656 /* ...next IE... */
1657 left -= 2 + priv->essid_len;
1658 if (left < 0)
1659 return 0;
1660 /* ... fill it in... */
1661 *pos++ = WLAN_EID_SSID;
1662 *pos++ = priv->essid_len;
1663 memcpy(pos, priv->essid, priv->essid_len);
1664 pos += priv->essid_len;
1665 len += 2 + priv->essid_len;
1666 }
1667
1668 /* fill in supported rate */
1669 /* ...next IE... */
1670 left -= 2;
1671 if (left < 0)
1672 return 0;
Tomas Winklerc7c46672007-10-18 02:04:15 +02001673
Zhu Yib481de92007-09-25 17:54:57 -07001674 /* ... fill it in... */
1675 *pos++ = WLAN_EID_SUPP_RATES;
1676 *pos = 0;
Tomas Winklerc7c46672007-10-18 02:04:15 +02001677
mabbasbee488d2007-10-25 17:15:42 +08001678 /* exclude 60M rate */
1679 active_rates = priv->rates_mask;
1680 active_rates &= ~IWL_RATE_60M_MASK;
1681
1682 active_rate_basic = active_rates & IWL_BASIC_RATES_MASK;
Zhu Yib481de92007-09-25 17:54:57 -07001683
Tomas Winklerc7c46672007-10-18 02:04:15 +02001684 cck_rates = IWL_CCK_RATES_MASK & active_rates;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001685 ret_rates = iwl4965_supported_rate_to_ie(pos, cck_rates,
mabbasbee488d2007-10-25 17:15:42 +08001686 active_rate_basic, &left);
Tomas Winklerc7c46672007-10-18 02:04:15 +02001687 active_rates &= ~ret_rates;
1688
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001689 ret_rates = iwl4965_supported_rate_to_ie(pos, active_rates,
mabbasbee488d2007-10-25 17:15:42 +08001690 active_rate_basic, &left);
Tomas Winklerc7c46672007-10-18 02:04:15 +02001691 active_rates &= ~ret_rates;
1692
Zhu Yib481de92007-09-25 17:54:57 -07001693 len += 2 + *pos;
1694 pos += (*pos) + 1;
Tomas Winklerc7c46672007-10-18 02:04:15 +02001695 if (active_rates == 0)
Zhu Yib481de92007-09-25 17:54:57 -07001696 goto fill_end;
1697
1698 /* fill in supported extended rate */
1699 /* ...next IE... */
1700 left -= 2;
1701 if (left < 0)
1702 return 0;
1703 /* ... fill it in... */
1704 *pos++ = WLAN_EID_EXT_SUPP_RATES;
1705 *pos = 0;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001706 iwl4965_supported_rate_to_ie(pos, active_rates,
mabbasbee488d2007-10-25 17:15:42 +08001707 active_rate_basic, &left);
Zhu Yib481de92007-09-25 17:54:57 -07001708 if (*pos > 0)
1709 len += 2 + *pos;
1710
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08001711#ifdef CONFIG_IWL4965_HT
Tomas Winkler78330fd2008-02-06 02:37:18 +02001712 if (sband && sband->ht_info.ht_supported) {
1713 struct ieee80211_ht_cap *ht_cap;
Zhu Yib481de92007-09-25 17:54:57 -07001714 pos += (*pos) + 1;
1715 *pos++ = WLAN_EID_HT_CAPABILITY;
Ron Rindjunsky8fb88032007-11-26 16:14:38 +02001716 *pos++ = sizeof(struct ieee80211_ht_cap);
Tomas Winkler78330fd2008-02-06 02:37:18 +02001717 ht_cap = (struct ieee80211_ht_cap *)pos;
1718 ht_cap->cap_info = cpu_to_le16(sband->ht_info.cap);
1719 memcpy(ht_cap->supp_mcs_set, sband->ht_info.supp_mcs_set, 16);
1720 ht_cap->ampdu_params_info =(sband->ht_info.ampdu_factor &
1721 IEEE80211_HT_CAP_AMPDU_FACTOR) |
1722 ((sband->ht_info.ampdu_density << 2) &
1723 IEEE80211_HT_CAP_AMPDU_DENSITY);
Ron Rindjunsky8fb88032007-11-26 16:14:38 +02001724 len += 2 + sizeof(struct ieee80211_ht_cap);
Zhu Yib481de92007-09-25 17:54:57 -07001725 }
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08001726#endif /*CONFIG_IWL4965_HT */
Zhu Yib481de92007-09-25 17:54:57 -07001727
1728 fill_end:
1729 return (u16)len;
1730}
1731
1732/*
1733 * QoS support
1734*/
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001735static int iwl4965_send_qos_params_command(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001736 struct iwl4965_qosparam_cmd *qos)
Zhu Yib481de92007-09-25 17:54:57 -07001737{
1738
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001739 return iwl4965_send_cmd_pdu(priv, REPLY_QOS_PARAM,
1740 sizeof(struct iwl4965_qosparam_cmd), qos);
Zhu Yib481de92007-09-25 17:54:57 -07001741}
1742
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001743static void iwl4965_activate_qos(struct iwl_priv *priv, u8 force)
Zhu Yib481de92007-09-25 17:54:57 -07001744{
1745 unsigned long flags;
1746
Zhu Yib481de92007-09-25 17:54:57 -07001747 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
1748 return;
1749
1750 if (!priv->qos_data.qos_enable)
1751 return;
1752
1753 spin_lock_irqsave(&priv->lock, flags);
1754 priv->qos_data.def_qos_parm.qos_flags = 0;
1755
1756 if (priv->qos_data.qos_cap.q_AP.queue_request &&
1757 !priv->qos_data.qos_cap.q_AP.txop_request)
1758 priv->qos_data.def_qos_parm.qos_flags |=
1759 QOS_PARAM_FLG_TXOP_TYPE_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07001760 if (priv->qos_data.qos_active)
1761 priv->qos_data.def_qos_parm.qos_flags |=
1762 QOS_PARAM_FLG_UPDATE_EDCA_MSK;
1763
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08001764#ifdef CONFIG_IWL4965_HT
Ron Rindjunskyfd105e72007-11-26 16:14:39 +02001765 if (priv->current_ht_config.is_ht)
Tomas Winklerf1f1f5c2007-10-25 17:15:26 +08001766 priv->qos_data.def_qos_parm.qos_flags |= QOS_PARAM_FLG_TGN_MSK;
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08001767#endif /* CONFIG_IWL4965_HT */
Tomas Winklerf1f1f5c2007-10-25 17:15:26 +08001768
Zhu Yib481de92007-09-25 17:54:57 -07001769 spin_unlock_irqrestore(&priv->lock, flags);
1770
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001771 if (force || iwl4965_is_associated(priv)) {
Tomas Winklerf1f1f5c2007-10-25 17:15:26 +08001772 IWL_DEBUG_QOS("send QoS cmd with Qos active=%d FLAGS=0x%X\n",
1773 priv->qos_data.qos_active,
1774 priv->qos_data.def_qos_parm.qos_flags);
Zhu Yib481de92007-09-25 17:54:57 -07001775
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001776 iwl4965_send_qos_params_command(priv,
Zhu Yib481de92007-09-25 17:54:57 -07001777 &(priv->qos_data.def_qos_parm));
1778 }
1779}
1780
Zhu Yib481de92007-09-25 17:54:57 -07001781/*
1782 * Power management (not Tx power!) functions
1783 */
1784#define MSEC_TO_USEC 1024
1785
1786#define NOSLP __constant_cpu_to_le16(0), 0, 0
1787#define SLP IWL_POWER_DRIVER_ALLOW_SLEEP_MSK, 0, 0
1788#define SLP_TIMEOUT(T) __constant_cpu_to_le32((T) * MSEC_TO_USEC)
1789#define SLP_VEC(X0, X1, X2, X3, X4) {__constant_cpu_to_le32(X0), \
1790 __constant_cpu_to_le32(X1), \
1791 __constant_cpu_to_le32(X2), \
1792 __constant_cpu_to_le32(X3), \
1793 __constant_cpu_to_le32(X4)}
1794
1795
1796/* default power management (not Tx power) table values */
1797/* for tim 0-10 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001798static struct iwl4965_power_vec_entry range_0[IWL_POWER_AC] = {
Zhu Yib481de92007-09-25 17:54:57 -07001799 {{NOSLP, SLP_TIMEOUT(0), SLP_TIMEOUT(0), SLP_VEC(0, 0, 0, 0, 0)}, 0},
1800 {{SLP, SLP_TIMEOUT(200), SLP_TIMEOUT(500), SLP_VEC(1, 2, 3, 4, 4)}, 0},
1801 {{SLP, SLP_TIMEOUT(200), SLP_TIMEOUT(300), SLP_VEC(2, 4, 6, 7, 7)}, 0},
1802 {{SLP, SLP_TIMEOUT(50), SLP_TIMEOUT(100), SLP_VEC(2, 6, 9, 9, 10)}, 0},
1803 {{SLP, SLP_TIMEOUT(50), SLP_TIMEOUT(25), SLP_VEC(2, 7, 9, 9, 10)}, 1},
1804 {{SLP, SLP_TIMEOUT(25), SLP_TIMEOUT(25), SLP_VEC(4, 7, 10, 10, 10)}, 1}
1805};
1806
1807/* for tim > 10 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001808static struct iwl4965_power_vec_entry range_1[IWL_POWER_AC] = {
Zhu Yib481de92007-09-25 17:54:57 -07001809 {{NOSLP, SLP_TIMEOUT(0), SLP_TIMEOUT(0), SLP_VEC(0, 0, 0, 0, 0)}, 0},
1810 {{SLP, SLP_TIMEOUT(200), SLP_TIMEOUT(500),
1811 SLP_VEC(1, 2, 3, 4, 0xFF)}, 0},
1812 {{SLP, SLP_TIMEOUT(200), SLP_TIMEOUT(300),
1813 SLP_VEC(2, 4, 6, 7, 0xFF)}, 0},
1814 {{SLP, SLP_TIMEOUT(50), SLP_TIMEOUT(100),
1815 SLP_VEC(2, 6, 9, 9, 0xFF)}, 0},
1816 {{SLP, SLP_TIMEOUT(50), SLP_TIMEOUT(25), SLP_VEC(2, 7, 9, 9, 0xFF)}, 0},
1817 {{SLP, SLP_TIMEOUT(25), SLP_TIMEOUT(25),
1818 SLP_VEC(4, 7, 10, 10, 0xFF)}, 0}
1819};
1820
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001821int iwl4965_power_init_handle(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001822{
1823 int rc = 0, i;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001824 struct iwl4965_power_mgr *pow_data;
1825 int size = sizeof(struct iwl4965_power_vec_entry) * IWL_POWER_AC;
Zhu Yib481de92007-09-25 17:54:57 -07001826 u16 pci_pm;
1827
1828 IWL_DEBUG_POWER("Initialize power \n");
1829
1830 pow_data = &(priv->power_data);
1831
1832 memset(pow_data, 0, sizeof(*pow_data));
1833
1834 pow_data->active_index = IWL_POWER_RANGE_0;
1835 pow_data->dtim_val = 0xffff;
1836
1837 memcpy(&pow_data->pwr_range_0[0], &range_0[0], size);
1838 memcpy(&pow_data->pwr_range_1[0], &range_1[0], size);
1839
1840 rc = pci_read_config_word(priv->pci_dev, PCI_LINK_CTRL, &pci_pm);
1841 if (rc != 0)
1842 return 0;
1843 else {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001844 struct iwl4965_powertable_cmd *cmd;
Zhu Yib481de92007-09-25 17:54:57 -07001845
1846 IWL_DEBUG_POWER("adjust power command flags\n");
1847
1848 for (i = 0; i < IWL_POWER_AC; i++) {
1849 cmd = &pow_data->pwr_range_0[i].cmd;
1850
1851 if (pci_pm & 0x1)
1852 cmd->flags &= ~IWL_POWER_PCI_PM_MSK;
1853 else
1854 cmd->flags |= IWL_POWER_PCI_PM_MSK;
1855 }
1856 }
1857 return rc;
1858}
1859
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001860static int iwl4965_update_power_cmd(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001861 struct iwl4965_powertable_cmd *cmd, u32 mode)
Zhu Yib481de92007-09-25 17:54:57 -07001862{
1863 int rc = 0, i;
1864 u8 skip;
1865 u32 max_sleep = 0;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001866 struct iwl4965_power_vec_entry *range;
Zhu Yib481de92007-09-25 17:54:57 -07001867 u8 period = 0;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001868 struct iwl4965_power_mgr *pow_data;
Zhu Yib481de92007-09-25 17:54:57 -07001869
1870 if (mode > IWL_POWER_INDEX_5) {
1871 IWL_DEBUG_POWER("Error invalid power mode \n");
1872 return -1;
1873 }
1874 pow_data = &(priv->power_data);
1875
1876 if (pow_data->active_index == IWL_POWER_RANGE_0)
1877 range = &pow_data->pwr_range_0[0];
1878 else
1879 range = &pow_data->pwr_range_1[1];
1880
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001881 memcpy(cmd, &range[mode].cmd, sizeof(struct iwl4965_powertable_cmd));
Zhu Yib481de92007-09-25 17:54:57 -07001882
1883#ifdef IWL_MAC80211_DISABLE
1884 if (priv->assoc_network != NULL) {
1885 unsigned long flags;
1886
1887 period = priv->assoc_network->tim.tim_period;
1888 }
1889#endif /*IWL_MAC80211_DISABLE */
1890 skip = range[mode].no_dtim;
1891
1892 if (period == 0) {
1893 period = 1;
1894 skip = 0;
1895 }
1896
1897 if (skip == 0) {
1898 max_sleep = period;
1899 cmd->flags &= ~IWL_POWER_SLEEP_OVER_DTIM_MSK;
1900 } else {
1901 __le32 slp_itrvl = cmd->sleep_interval[IWL_POWER_VEC_SIZE - 1];
1902 max_sleep = (le32_to_cpu(slp_itrvl) / period) * period;
1903 cmd->flags |= IWL_POWER_SLEEP_OVER_DTIM_MSK;
1904 }
1905
1906 for (i = 0; i < IWL_POWER_VEC_SIZE; i++) {
1907 if (le32_to_cpu(cmd->sleep_interval[i]) > max_sleep)
1908 cmd->sleep_interval[i] = cpu_to_le32(max_sleep);
1909 }
1910
1911 IWL_DEBUG_POWER("Flags value = 0x%08X\n", cmd->flags);
1912 IWL_DEBUG_POWER("Tx timeout = %u\n", le32_to_cpu(cmd->tx_data_timeout));
1913 IWL_DEBUG_POWER("Rx timeout = %u\n", le32_to_cpu(cmd->rx_data_timeout));
1914 IWL_DEBUG_POWER("Sleep interval vector = { %d , %d , %d , %d , %d }\n",
1915 le32_to_cpu(cmd->sleep_interval[0]),
1916 le32_to_cpu(cmd->sleep_interval[1]),
1917 le32_to_cpu(cmd->sleep_interval[2]),
1918 le32_to_cpu(cmd->sleep_interval[3]),
1919 le32_to_cpu(cmd->sleep_interval[4]));
1920
1921 return rc;
1922}
1923
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001924static int iwl4965_send_power_mode(struct iwl_priv *priv, u32 mode)
Zhu Yib481de92007-09-25 17:54:57 -07001925{
John W. Linville9a62f732007-11-15 16:27:36 -05001926 u32 uninitialized_var(final_mode);
Zhu Yib481de92007-09-25 17:54:57 -07001927 int rc;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001928 struct iwl4965_powertable_cmd cmd;
Zhu Yib481de92007-09-25 17:54:57 -07001929
1930 /* If on battery, set to 3,
Ian Schram01ebd062007-10-25 17:15:22 +08001931 * if plugged into AC power, set to CAM ("continuously aware mode"),
Zhu Yib481de92007-09-25 17:54:57 -07001932 * else user level */
1933 switch (mode) {
1934 case IWL_POWER_BATTERY:
1935 final_mode = IWL_POWER_INDEX_3;
1936 break;
1937 case IWL_POWER_AC:
1938 final_mode = IWL_POWER_MODE_CAM;
1939 break;
1940 default:
1941 final_mode = mode;
1942 break;
1943 }
1944
1945 cmd.keep_alive_beacons = 0;
1946
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001947 iwl4965_update_power_cmd(priv, &cmd, final_mode);
Zhu Yib481de92007-09-25 17:54:57 -07001948
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001949 rc = iwl4965_send_cmd_pdu(priv, POWER_TABLE_CMD, sizeof(cmd), &cmd);
Zhu Yib481de92007-09-25 17:54:57 -07001950
1951 if (final_mode == IWL_POWER_MODE_CAM)
1952 clear_bit(STATUS_POWER_PMI, &priv->status);
1953 else
1954 set_bit(STATUS_POWER_PMI, &priv->status);
1955
1956 return rc;
1957}
1958
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001959int iwl4965_is_network_packet(struct iwl_priv *priv, struct ieee80211_hdr *header)
Zhu Yib481de92007-09-25 17:54:57 -07001960{
1961 /* Filter incoming packets to determine if they are targeted toward
1962 * this network, discarding packets coming from ourselves */
1963 switch (priv->iw_mode) {
1964 case IEEE80211_IF_TYPE_IBSS: /* Header: Dest. | Source | BSSID */
1965 /* packets from our adapter are dropped (echo) */
1966 if (!compare_ether_addr(header->addr2, priv->mac_addr))
1967 return 0;
1968 /* {broad,multi}cast packets to our IBSS go through */
1969 if (is_multicast_ether_addr(header->addr1))
1970 return !compare_ether_addr(header->addr3, priv->bssid);
1971 /* packets to our adapter go through */
1972 return !compare_ether_addr(header->addr1, priv->mac_addr);
1973 case IEEE80211_IF_TYPE_STA: /* Header: Dest. | AP{BSSID} | Source */
1974 /* packets from our adapter are dropped (echo) */
1975 if (!compare_ether_addr(header->addr3, priv->mac_addr))
1976 return 0;
1977 /* {broad,multi}cast packets to our BSS go through */
1978 if (is_multicast_ether_addr(header->addr1))
1979 return !compare_ether_addr(header->addr2, priv->bssid);
1980 /* packets to our adapter go through */
1981 return !compare_ether_addr(header->addr1, priv->mac_addr);
1982 }
1983
1984 return 1;
1985}
1986
1987#define TX_STATUS_ENTRY(x) case TX_STATUS_FAIL_ ## x: return #x
1988
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001989static const char *iwl4965_get_tx_fail_reason(u32 status)
Zhu Yib481de92007-09-25 17:54:57 -07001990{
1991 switch (status & TX_STATUS_MSK) {
1992 case TX_STATUS_SUCCESS:
1993 return "SUCCESS";
1994 TX_STATUS_ENTRY(SHORT_LIMIT);
1995 TX_STATUS_ENTRY(LONG_LIMIT);
1996 TX_STATUS_ENTRY(FIFO_UNDERRUN);
1997 TX_STATUS_ENTRY(MGMNT_ABORT);
1998 TX_STATUS_ENTRY(NEXT_FRAG);
1999 TX_STATUS_ENTRY(LIFE_EXPIRE);
2000 TX_STATUS_ENTRY(DEST_PS);
2001 TX_STATUS_ENTRY(ABORTED);
2002 TX_STATUS_ENTRY(BT_RETRY);
2003 TX_STATUS_ENTRY(STA_INVALID);
2004 TX_STATUS_ENTRY(FRAG_DROPPED);
2005 TX_STATUS_ENTRY(TID_DISABLE);
2006 TX_STATUS_ENTRY(FRAME_FLUSHED);
2007 TX_STATUS_ENTRY(INSUFFICIENT_CF_POLL);
2008 TX_STATUS_ENTRY(TX_LOCKED);
2009 TX_STATUS_ENTRY(NO_BEACON_ON_RADAR);
2010 }
2011
2012 return "UNKNOWN";
2013}
2014
2015/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002016 * iwl4965_scan_cancel - Cancel any currently executing HW scan
Zhu Yib481de92007-09-25 17:54:57 -07002017 *
2018 * NOTE: priv->mutex is not required before calling this function
2019 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002020static int iwl4965_scan_cancel(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002021{
2022 if (!test_bit(STATUS_SCAN_HW, &priv->status)) {
2023 clear_bit(STATUS_SCANNING, &priv->status);
2024 return 0;
2025 }
2026
2027 if (test_bit(STATUS_SCANNING, &priv->status)) {
2028 if (!test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
2029 IWL_DEBUG_SCAN("Queuing scan abort.\n");
2030 set_bit(STATUS_SCAN_ABORTING, &priv->status);
2031 queue_work(priv->workqueue, &priv->abort_scan);
2032
2033 } else
2034 IWL_DEBUG_SCAN("Scan abort already in progress.\n");
2035
2036 return test_bit(STATUS_SCANNING, &priv->status);
2037 }
2038
2039 return 0;
2040}
2041
2042/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002043 * iwl4965_scan_cancel_timeout - Cancel any currently executing HW scan
Zhu Yib481de92007-09-25 17:54:57 -07002044 * @ms: amount of time to wait (in milliseconds) for scan to abort
2045 *
2046 * NOTE: priv->mutex must be held before calling this function
2047 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002048static int iwl4965_scan_cancel_timeout(struct iwl_priv *priv, unsigned long ms)
Zhu Yib481de92007-09-25 17:54:57 -07002049{
2050 unsigned long now = jiffies;
2051 int ret;
2052
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002053 ret = iwl4965_scan_cancel(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002054 if (ret && ms) {
2055 mutex_unlock(&priv->mutex);
2056 while (!time_after(jiffies, now + msecs_to_jiffies(ms)) &&
2057 test_bit(STATUS_SCANNING, &priv->status))
2058 msleep(1);
2059 mutex_lock(&priv->mutex);
2060
2061 return test_bit(STATUS_SCANNING, &priv->status);
2062 }
2063
2064 return ret;
2065}
2066
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002067static void iwl4965_sequence_reset(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002068{
2069 /* Reset ieee stats */
2070
2071 /* We don't reset the net_device_stats (ieee->stats) on
2072 * re-association */
2073
2074 priv->last_seq_num = -1;
2075 priv->last_frag_num = -1;
2076 priv->last_packet_time = 0;
2077
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002078 iwl4965_scan_cancel(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002079}
2080
2081#define MAX_UCODE_BEACON_INTERVAL 4096
2082#define INTEL_CONN_LISTEN_INTERVAL __constant_cpu_to_le16(0xA)
2083
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002084static __le16 iwl4965_adjust_beacon_interval(u16 beacon_val)
Zhu Yib481de92007-09-25 17:54:57 -07002085{
2086 u16 new_val = 0;
2087 u16 beacon_factor = 0;
2088
2089 beacon_factor =
2090 (beacon_val + MAX_UCODE_BEACON_INTERVAL)
2091 / MAX_UCODE_BEACON_INTERVAL;
2092 new_val = beacon_val / beacon_factor;
2093
2094 return cpu_to_le16(new_val);
2095}
2096
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002097static void iwl4965_setup_rxon_timing(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002098{
2099 u64 interval_tm_unit;
2100 u64 tsf, result;
2101 unsigned long flags;
2102 struct ieee80211_conf *conf = NULL;
2103 u16 beacon_int = 0;
2104
2105 conf = ieee80211_get_hw_conf(priv->hw);
2106
2107 spin_lock_irqsave(&priv->lock, flags);
2108 priv->rxon_timing.timestamp.dw[1] = cpu_to_le32(priv->timestamp1);
2109 priv->rxon_timing.timestamp.dw[0] = cpu_to_le32(priv->timestamp0);
2110
2111 priv->rxon_timing.listen_interval = INTEL_CONN_LISTEN_INTERVAL;
2112
2113 tsf = priv->timestamp1;
2114 tsf = ((tsf << 32) | priv->timestamp0);
2115
2116 beacon_int = priv->beacon_int;
2117 spin_unlock_irqrestore(&priv->lock, flags);
2118
2119 if (priv->iw_mode == IEEE80211_IF_TYPE_STA) {
2120 if (beacon_int == 0) {
2121 priv->rxon_timing.beacon_interval = cpu_to_le16(100);
2122 priv->rxon_timing.beacon_init_val = cpu_to_le32(102400);
2123 } else {
2124 priv->rxon_timing.beacon_interval =
2125 cpu_to_le16(beacon_int);
2126 priv->rxon_timing.beacon_interval =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002127 iwl4965_adjust_beacon_interval(
Zhu Yib481de92007-09-25 17:54:57 -07002128 le16_to_cpu(priv->rxon_timing.beacon_interval));
2129 }
2130
2131 priv->rxon_timing.atim_window = 0;
2132 } else {
2133 priv->rxon_timing.beacon_interval =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002134 iwl4965_adjust_beacon_interval(conf->beacon_int);
Zhu Yib481de92007-09-25 17:54:57 -07002135 /* TODO: we need to get atim_window from upper stack
2136 * for now we set to 0 */
2137 priv->rxon_timing.atim_window = 0;
2138 }
2139
2140 interval_tm_unit =
2141 (le16_to_cpu(priv->rxon_timing.beacon_interval) * 1024);
2142 result = do_div(tsf, interval_tm_unit);
2143 priv->rxon_timing.beacon_init_val =
2144 cpu_to_le32((u32) ((u64) interval_tm_unit - result));
2145
2146 IWL_DEBUG_ASSOC
2147 ("beacon interval %d beacon timer %d beacon tim %d\n",
2148 le16_to_cpu(priv->rxon_timing.beacon_interval),
2149 le32_to_cpu(priv->rxon_timing.beacon_init_val),
2150 le16_to_cpu(priv->rxon_timing.atim_window));
2151}
2152
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002153static int iwl4965_scan_initiate(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002154{
2155 if (priv->iw_mode == IEEE80211_IF_TYPE_AP) {
2156 IWL_ERROR("APs don't scan.\n");
2157 return 0;
2158 }
2159
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002160 if (!iwl4965_is_ready_rf(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07002161 IWL_DEBUG_SCAN("Aborting scan due to not ready.\n");
2162 return -EIO;
2163 }
2164
2165 if (test_bit(STATUS_SCANNING, &priv->status)) {
2166 IWL_DEBUG_SCAN("Scan already in progress.\n");
2167 return -EAGAIN;
2168 }
2169
2170 if (test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
2171 IWL_DEBUG_SCAN("Scan request while abort pending. "
2172 "Queuing.\n");
2173 return -EAGAIN;
2174 }
2175
2176 IWL_DEBUG_INFO("Starting scan...\n");
2177 priv->scan_bands = 2;
2178 set_bit(STATUS_SCANNING, &priv->status);
2179 priv->scan_start = jiffies;
2180 priv->scan_pass_start = priv->scan_start;
2181
2182 queue_work(priv->workqueue, &priv->request_scan);
2183
2184 return 0;
2185}
2186
Zhu Yib481de92007-09-25 17:54:57 -07002187
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002188static void iwl4965_set_flags_for_phymode(struct iwl_priv *priv,
Johannes Berg8318d782008-01-24 19:38:38 +01002189 enum ieee80211_band band)
Zhu Yib481de92007-09-25 17:54:57 -07002190{
Johannes Berg8318d782008-01-24 19:38:38 +01002191 if (band == IEEE80211_BAND_5GHZ) {
Zhu Yib481de92007-09-25 17:54:57 -07002192 priv->staging_rxon.flags &=
2193 ~(RXON_FLG_BAND_24G_MSK | RXON_FLG_AUTO_DETECT_MSK
2194 | RXON_FLG_CCK_MSK);
2195 priv->staging_rxon.flags |= RXON_FLG_SHORT_SLOT_MSK;
2196 } else {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002197 /* Copied from iwl4965_bg_post_associate() */
Zhu Yib481de92007-09-25 17:54:57 -07002198 if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_SLOT_TIME)
2199 priv->staging_rxon.flags |= RXON_FLG_SHORT_SLOT_MSK;
2200 else
2201 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
2202
2203 if (priv->iw_mode == IEEE80211_IF_TYPE_IBSS)
2204 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
2205
2206 priv->staging_rxon.flags |= RXON_FLG_BAND_24G_MSK;
2207 priv->staging_rxon.flags |= RXON_FLG_AUTO_DETECT_MSK;
2208 priv->staging_rxon.flags &= ~RXON_FLG_CCK_MSK;
2209 }
2210}
2211
2212/*
Ian Schram01ebd062007-10-25 17:15:22 +08002213 * initialize rxon structure with default values from eeprom
Zhu Yib481de92007-09-25 17:54:57 -07002214 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002215static void iwl4965_connection_init_rx_config(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002216{
Assaf Kraussbf85ea42008-03-14 10:38:49 -07002217 const struct iwl_channel_info *ch_info;
Zhu Yib481de92007-09-25 17:54:57 -07002218
2219 memset(&priv->staging_rxon, 0, sizeof(priv->staging_rxon));
2220
2221 switch (priv->iw_mode) {
2222 case IEEE80211_IF_TYPE_AP:
2223 priv->staging_rxon.dev_type = RXON_DEV_TYPE_AP;
2224 break;
2225
2226 case IEEE80211_IF_TYPE_STA:
2227 priv->staging_rxon.dev_type = RXON_DEV_TYPE_ESS;
2228 priv->staging_rxon.filter_flags = RXON_FILTER_ACCEPT_GRP_MSK;
2229 break;
2230
2231 case IEEE80211_IF_TYPE_IBSS:
2232 priv->staging_rxon.dev_type = RXON_DEV_TYPE_IBSS;
2233 priv->staging_rxon.flags = RXON_FLG_SHORT_PREAMBLE_MSK;
2234 priv->staging_rxon.filter_flags = RXON_FILTER_BCON_AWARE_MSK |
2235 RXON_FILTER_ACCEPT_GRP_MSK;
2236 break;
2237
2238 case IEEE80211_IF_TYPE_MNTR:
2239 priv->staging_rxon.dev_type = RXON_DEV_TYPE_SNIFFER;
2240 priv->staging_rxon.filter_flags = RXON_FILTER_PROMISC_MSK |
2241 RXON_FILTER_CTL2HOST_MSK | RXON_FILTER_ACCEPT_GRP_MSK;
2242 break;
2243 }
2244
2245#if 0
2246 /* TODO: Figure out when short_preamble would be set and cache from
2247 * that */
2248 if (!hw_to_local(priv->hw)->short_preamble)
2249 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_PREAMBLE_MSK;
2250 else
2251 priv->staging_rxon.flags |= RXON_FLG_SHORT_PREAMBLE_MSK;
2252#endif
2253
Johannes Berg8318d782008-01-24 19:38:38 +01002254 ch_info = iwl4965_get_channel_info(priv, priv->band,
Zhu Yib481de92007-09-25 17:54:57 -07002255 le16_to_cpu(priv->staging_rxon.channel));
2256
2257 if (!ch_info)
2258 ch_info = &priv->channel_info[0];
2259
2260 /*
2261 * in some case A channels are all non IBSS
2262 * in this case force B/G channel
2263 */
2264 if ((priv->iw_mode == IEEE80211_IF_TYPE_IBSS) &&
2265 !(is_channel_ibss(ch_info)))
2266 ch_info = &priv->channel_info[0];
2267
2268 priv->staging_rxon.channel = cpu_to_le16(ch_info->channel);
Johannes Berg8318d782008-01-24 19:38:38 +01002269 priv->band = ch_info->band;
Zhu Yib481de92007-09-25 17:54:57 -07002270
Johannes Berg8318d782008-01-24 19:38:38 +01002271 iwl4965_set_flags_for_phymode(priv, priv->band);
Zhu Yib481de92007-09-25 17:54:57 -07002272
2273 priv->staging_rxon.ofdm_basic_rates =
2274 (IWL_OFDM_RATES_MASK >> IWL_FIRST_OFDM_RATE) & 0xFF;
2275 priv->staging_rxon.cck_basic_rates =
2276 (IWL_CCK_RATES_MASK >> IWL_FIRST_CCK_RATE) & 0xF;
2277
2278 priv->staging_rxon.flags &= ~(RXON_FLG_CHANNEL_MODE_MIXED_MSK |
2279 RXON_FLG_CHANNEL_MODE_PURE_40_MSK);
2280 memcpy(priv->staging_rxon.node_addr, priv->mac_addr, ETH_ALEN);
2281 memcpy(priv->staging_rxon.wlap_bssid_addr, priv->mac_addr, ETH_ALEN);
2282 priv->staging_rxon.ofdm_ht_single_stream_basic_rates = 0xff;
2283 priv->staging_rxon.ofdm_ht_dual_stream_basic_rates = 0xff;
2284 iwl4965_set_rxon_chain(priv);
2285}
2286
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002287static int iwl4965_set_mode(struct iwl_priv *priv, int mode)
Zhu Yib481de92007-09-25 17:54:57 -07002288{
Zhu Yib481de92007-09-25 17:54:57 -07002289 if (mode == IEEE80211_IF_TYPE_IBSS) {
Assaf Kraussbf85ea42008-03-14 10:38:49 -07002290 const struct iwl_channel_info *ch_info;
Zhu Yib481de92007-09-25 17:54:57 -07002291
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002292 ch_info = iwl4965_get_channel_info(priv,
Johannes Berg8318d782008-01-24 19:38:38 +01002293 priv->band,
Zhu Yib481de92007-09-25 17:54:57 -07002294 le16_to_cpu(priv->staging_rxon.channel));
2295
2296 if (!ch_info || !is_channel_ibss(ch_info)) {
2297 IWL_ERROR("channel %d not IBSS channel\n",
2298 le16_to_cpu(priv->staging_rxon.channel));
2299 return -EINVAL;
2300 }
2301 }
2302
Zhu Yib481de92007-09-25 17:54:57 -07002303 priv->iw_mode = mode;
2304
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002305 iwl4965_connection_init_rx_config(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002306 memcpy(priv->staging_rxon.node_addr, priv->mac_addr, ETH_ALEN);
2307
Assaf Kraussbf85ea42008-03-14 10:38:49 -07002308 iwlcore_clear_stations_table(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002309
Mohamed Abbasfde35712007-11-29 11:10:15 +08002310 /* dont commit rxon if rf-kill is on*/
2311 if (!iwl4965_is_ready_rf(priv))
2312 return -EAGAIN;
2313
2314 cancel_delayed_work(&priv->scan_check);
2315 if (iwl4965_scan_cancel_timeout(priv, 100)) {
2316 IWL_WARNING("Aborted scan still in progress after 100ms\n");
2317 IWL_DEBUG_MAC80211("leaving - scan abort failed.\n");
2318 return -EAGAIN;
2319 }
2320
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002321 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002322
2323 return 0;
2324}
2325
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002326static void iwl4965_build_tx_cmd_hwcrypto(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07002327 struct ieee80211_tx_control *ctl,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002328 struct iwl4965_cmd *cmd,
Zhu Yib481de92007-09-25 17:54:57 -07002329 struct sk_buff *skb_frag,
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07002330 int sta_id)
Zhu Yib481de92007-09-25 17:54:57 -07002331{
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07002332 struct iwl4965_hw_key *keyinfo = &priv->stations[sta_id].keyinfo;
Zhu Yib481de92007-09-25 17:54:57 -07002333
2334 switch (keyinfo->alg) {
2335 case ALG_CCMP:
2336 cmd->cmd.tx.sec_ctl = TX_CMD_SEC_CCM;
2337 memcpy(cmd->cmd.tx.key, keyinfo->key, keyinfo->keylen);
Max Stepanov8236e182008-03-12 16:58:48 -07002338 if (ctl->flags & IEEE80211_TXCTL_AMPDU)
2339 cmd->cmd.tx.tx_flags |= TX_CMD_FLG_AGG_CCMP_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07002340 IWL_DEBUG_TX("tx_cmd with aes hwcrypto\n");
2341 break;
2342
2343 case ALG_TKIP:
2344#if 0
2345 cmd->cmd.tx.sec_ctl = TX_CMD_SEC_TKIP;
2346
2347 if (last_frag)
2348 memcpy(cmd->cmd.tx.tkip_mic.byte, skb_frag->tail - 8,
2349 8);
2350 else
2351 memset(cmd->cmd.tx.tkip_mic.byte, 0, 8);
2352#endif
2353 break;
2354
2355 case ALG_WEP:
2356 cmd->cmd.tx.sec_ctl = TX_CMD_SEC_WEP |
2357 (ctl->key_idx & TX_CMD_SEC_MSK) << TX_CMD_SEC_SHIFT;
2358
2359 if (keyinfo->keylen == 13)
2360 cmd->cmd.tx.sec_ctl |= TX_CMD_SEC_KEY128;
2361
2362 memcpy(&cmd->cmd.tx.key[3], keyinfo->key, keyinfo->keylen);
2363
2364 IWL_DEBUG_TX("Configuring packet for WEP encryption "
2365 "with key %d\n", ctl->key_idx);
2366 break;
2367
Zhu Yib481de92007-09-25 17:54:57 -07002368 default:
2369 printk(KERN_ERR "Unknown encode alg %d\n", keyinfo->alg);
2370 break;
2371 }
2372}
2373
2374/*
2375 * handle build REPLY_TX command notification.
2376 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002377static void iwl4965_build_tx_cmd_basic(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002378 struct iwl4965_cmd *cmd,
Zhu Yib481de92007-09-25 17:54:57 -07002379 struct ieee80211_tx_control *ctrl,
2380 struct ieee80211_hdr *hdr,
2381 int is_unicast, u8 std_id)
2382{
2383 __le16 *qc;
2384 u16 fc = le16_to_cpu(hdr->frame_control);
2385 __le32 tx_flags = cmd->cmd.tx.tx_flags;
2386
2387 cmd->cmd.tx.stop_time.life_time = TX_CMD_LIFE_TIME_INFINITE;
2388 if (!(ctrl->flags & IEEE80211_TXCTL_NO_ACK)) {
2389 tx_flags |= TX_CMD_FLG_ACK_MSK;
2390 if ((fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_MGMT)
2391 tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
2392 if (ieee80211_is_probe_response(fc) &&
2393 !(le16_to_cpu(hdr->seq_ctrl) & 0xf))
2394 tx_flags |= TX_CMD_FLG_TSF_MSK;
2395 } else {
2396 tx_flags &= (~TX_CMD_FLG_ACK_MSK);
2397 tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
2398 }
2399
Tomas Winkler87e4f7d2008-01-14 17:46:16 -08002400 if (ieee80211_is_back_request(fc))
2401 tx_flags |= TX_CMD_FLG_ACK_MSK | TX_CMD_FLG_IMM_BA_RSP_MASK;
2402
2403
Zhu Yib481de92007-09-25 17:54:57 -07002404 cmd->cmd.tx.sta_id = std_id;
2405 if (ieee80211_get_morefrag(hdr))
2406 tx_flags |= TX_CMD_FLG_MORE_FRAG_MSK;
2407
2408 qc = ieee80211_get_qos_ctrl(hdr);
2409 if (qc) {
2410 cmd->cmd.tx.tid_tspec = (u8) (le16_to_cpu(*qc) & 0xf);
2411 tx_flags &= ~TX_CMD_FLG_SEQ_CTL_MSK;
2412 } else
2413 tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
2414
2415 if (ctrl->flags & IEEE80211_TXCTL_USE_RTS_CTS) {
2416 tx_flags |= TX_CMD_FLG_RTS_MSK;
2417 tx_flags &= ~TX_CMD_FLG_CTS_MSK;
2418 } else if (ctrl->flags & IEEE80211_TXCTL_USE_CTS_PROTECT) {
2419 tx_flags &= ~TX_CMD_FLG_RTS_MSK;
2420 tx_flags |= TX_CMD_FLG_CTS_MSK;
2421 }
2422
2423 if ((tx_flags & TX_CMD_FLG_RTS_MSK) || (tx_flags & TX_CMD_FLG_CTS_MSK))
2424 tx_flags |= TX_CMD_FLG_FULL_TXOP_PROT_MSK;
2425
2426 tx_flags &= ~(TX_CMD_FLG_ANT_SEL_MSK);
2427 if ((fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_MGMT) {
2428 if ((fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_ASSOC_REQ ||
2429 (fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_REASSOC_REQ)
Ian Schrambc434dd2007-10-25 17:15:29 +08002430 cmd->cmd.tx.timeout.pm_frame_timeout = cpu_to_le16(3);
Zhu Yib481de92007-09-25 17:54:57 -07002431 else
Ian Schrambc434dd2007-10-25 17:15:29 +08002432 cmd->cmd.tx.timeout.pm_frame_timeout = cpu_to_le16(2);
Zhu Yib481de92007-09-25 17:54:57 -07002433 } else
2434 cmd->cmd.tx.timeout.pm_frame_timeout = 0;
2435
2436 cmd->cmd.tx.driver_txop = 0;
2437 cmd->cmd.tx.tx_flags = tx_flags;
2438 cmd->cmd.tx.next_frame_len = 0;
2439}
Tomas Winkler19758be2008-03-12 16:58:51 -07002440static void iwl_update_tx_stats(struct iwl_priv *priv, u16 fc, u16 len)
2441{
2442 /* 0 - mgmt, 1 - cnt, 2 - data */
2443 int idx = (fc & IEEE80211_FCTL_FTYPE) >> 2;
2444 priv->tx_stats[idx].cnt++;
2445 priv->tx_stats[idx].bytes += len;
2446}
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002447/**
2448 * iwl4965_get_sta_id - Find station's index within station table
2449 *
2450 * If new IBSS station, create new entry in station table
2451 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002452static int iwl4965_get_sta_id(struct iwl_priv *priv,
Ben Cahill9fbab512007-11-29 11:09:47 +08002453 struct ieee80211_hdr *hdr)
Zhu Yib481de92007-09-25 17:54:57 -07002454{
2455 int sta_id;
2456 u16 fc = le16_to_cpu(hdr->frame_control);
Joe Perches0795af52007-10-03 17:59:30 -07002457 DECLARE_MAC_BUF(mac);
Zhu Yib481de92007-09-25 17:54:57 -07002458
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002459 /* If this frame is broadcast or management, use broadcast station id */
Zhu Yib481de92007-09-25 17:54:57 -07002460 if (((fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_DATA) ||
2461 is_multicast_ether_addr(hdr->addr1))
2462 return priv->hw_setting.bcast_sta_id;
2463
2464 switch (priv->iw_mode) {
2465
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002466 /* If we are a client station in a BSS network, use the special
2467 * AP station entry (that's the only station we communicate with) */
Zhu Yib481de92007-09-25 17:54:57 -07002468 case IEEE80211_IF_TYPE_STA:
2469 return IWL_AP_ID;
2470
2471 /* If we are an AP, then find the station, or use BCAST */
2472 case IEEE80211_IF_TYPE_AP:
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002473 sta_id = iwl4965_hw_find_station(priv, hdr->addr1);
Zhu Yib481de92007-09-25 17:54:57 -07002474 if (sta_id != IWL_INVALID_STATION)
2475 return sta_id;
2476 return priv->hw_setting.bcast_sta_id;
2477
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002478 /* If this frame is going out to an IBSS network, find the station,
2479 * or create a new station table entry */
Zhu Yib481de92007-09-25 17:54:57 -07002480 case IEEE80211_IF_TYPE_IBSS:
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002481 sta_id = iwl4965_hw_find_station(priv, hdr->addr1);
Zhu Yib481de92007-09-25 17:54:57 -07002482 if (sta_id != IWL_INVALID_STATION)
2483 return sta_id;
2484
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002485 /* Create new station table entry */
Ron Rindjunsky67d62032007-11-26 16:14:40 +02002486 sta_id = iwl4965_add_station_flags(priv, hdr->addr1,
2487 0, CMD_ASYNC, NULL);
Zhu Yib481de92007-09-25 17:54:57 -07002488
2489 if (sta_id != IWL_INVALID_STATION)
2490 return sta_id;
2491
Joe Perches0795af52007-10-03 17:59:30 -07002492 IWL_DEBUG_DROP("Station %s not in station map. "
Zhu Yib481de92007-09-25 17:54:57 -07002493 "Defaulting to broadcast...\n",
Joe Perches0795af52007-10-03 17:59:30 -07002494 print_mac(mac, hdr->addr1));
Tomas Winkler0a6857e2008-03-12 16:58:49 -07002495 iwl_print_hex_dump(IWL_DL_DROP, (u8 *) hdr, sizeof(*hdr));
Zhu Yib481de92007-09-25 17:54:57 -07002496 return priv->hw_setting.bcast_sta_id;
2497
2498 default:
Ian Schram01ebd062007-10-25 17:15:22 +08002499 IWL_WARNING("Unknown mode of operation: %d", priv->iw_mode);
Zhu Yib481de92007-09-25 17:54:57 -07002500 return priv->hw_setting.bcast_sta_id;
2501 }
2502}
2503
2504/*
2505 * start REPLY_TX command process
2506 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002507static int iwl4965_tx_skb(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07002508 struct sk_buff *skb, struct ieee80211_tx_control *ctl)
2509{
2510 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002511 struct iwl4965_tfd_frame *tfd;
Zhu Yib481de92007-09-25 17:54:57 -07002512 u32 *control_flags;
2513 int txq_id = ctl->queue;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002514 struct iwl4965_tx_queue *txq = NULL;
2515 struct iwl4965_queue *q = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07002516 dma_addr_t phys_addr;
2517 dma_addr_t txcmd_phys;
Tomas Winkler87e4f7d2008-01-14 17:46:16 -08002518 dma_addr_t scratch_phys;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002519 struct iwl4965_cmd *out_cmd = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07002520 u16 len, idx, len_org;
2521 u8 id, hdr_len, unicast;
2522 u8 sta_id;
2523 u16 seq_number = 0;
2524 u16 fc;
2525 __le16 *qc;
2526 u8 wait_write_ptr = 0;
2527 unsigned long flags;
2528 int rc;
2529
2530 spin_lock_irqsave(&priv->lock, flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002531 if (iwl4965_is_rfkill(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07002532 IWL_DEBUG_DROP("Dropping - RF KILL\n");
2533 goto drop_unlock;
2534 }
2535
Johannes Berg32bfd352007-12-19 01:31:26 +01002536 if (!priv->vif) {
2537 IWL_DEBUG_DROP("Dropping - !priv->vif\n");
Zhu Yib481de92007-09-25 17:54:57 -07002538 goto drop_unlock;
2539 }
2540
Johannes Berg8318d782008-01-24 19:38:38 +01002541 if ((ctl->tx_rate->hw_value & 0xFF) == IWL_INVALID_RATE) {
Zhu Yib481de92007-09-25 17:54:57 -07002542 IWL_ERROR("ERROR: No TX rate available.\n");
2543 goto drop_unlock;
2544 }
2545
2546 unicast = !is_multicast_ether_addr(hdr->addr1);
2547 id = 0;
2548
2549 fc = le16_to_cpu(hdr->frame_control);
2550
Tomas Winkler0a6857e2008-03-12 16:58:49 -07002551#ifdef CONFIG_IWLWIFI_DEBUG
Zhu Yib481de92007-09-25 17:54:57 -07002552 if (ieee80211_is_auth(fc))
2553 IWL_DEBUG_TX("Sending AUTH frame\n");
2554 else if (ieee80211_is_assoc_request(fc))
2555 IWL_DEBUG_TX("Sending ASSOC frame\n");
2556 else if (ieee80211_is_reassoc_request(fc))
2557 IWL_DEBUG_TX("Sending REASSOC frame\n");
2558#endif
2559
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08002560 /* drop all data frame if we are not associated */
Gregory Greenman76f39152008-01-23 10:15:21 -08002561 if (((fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_DATA) &&
2562 (!iwl4965_is_associated(priv) ||
Reinette Chatrea6477242008-02-14 10:40:28 -08002563 ((priv->iw_mode == IEEE80211_IF_TYPE_STA) && !priv->assoc_id) ||
Gregory Greenman76f39152008-01-23 10:15:21 -08002564 !priv->assoc_station_added)) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002565 IWL_DEBUG_DROP("Dropping - !iwl4965_is_associated\n");
Zhu Yib481de92007-09-25 17:54:57 -07002566 goto drop_unlock;
2567 }
2568
2569 spin_unlock_irqrestore(&priv->lock, flags);
2570
2571 hdr_len = ieee80211_get_hdrlen(fc);
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002572
2573 /* Find (or create) index into station table for destination station */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002574 sta_id = iwl4965_get_sta_id(priv, hdr);
Zhu Yib481de92007-09-25 17:54:57 -07002575 if (sta_id == IWL_INVALID_STATION) {
Joe Perches0795af52007-10-03 17:59:30 -07002576 DECLARE_MAC_BUF(mac);
2577
2578 IWL_DEBUG_DROP("Dropping - INVALID STATION: %s\n",
2579 print_mac(mac, hdr->addr1));
Zhu Yib481de92007-09-25 17:54:57 -07002580 goto drop;
2581 }
2582
2583 IWL_DEBUG_RATE("station Id %d\n", sta_id);
2584
2585 qc = ieee80211_get_qos_ctrl(hdr);
2586 if (qc) {
2587 u8 tid = (u8)(le16_to_cpu(*qc) & 0xf);
2588 seq_number = priv->stations[sta_id].tid[tid].seq_number &
2589 IEEE80211_SCTL_SEQ;
2590 hdr->seq_ctrl = cpu_to_le16(seq_number) |
2591 (hdr->seq_ctrl &
2592 __constant_cpu_to_le16(IEEE80211_SCTL_FRAG));
2593 seq_number += 0x10;
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08002594#ifdef CONFIG_IWL4965_HT
Zhu Yib481de92007-09-25 17:54:57 -07002595 /* aggregation is on for this <sta,tid> */
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02002596 if (ctl->flags & IEEE80211_TXCTL_AMPDU)
Zhu Yib481de92007-09-25 17:54:57 -07002597 txq_id = priv->stations[sta_id].tid[tid].agg.txq_id;
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02002598 priv->stations[sta_id].tid[tid].tfds_in_queue++;
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08002599#endif /* CONFIG_IWL4965_HT */
Zhu Yib481de92007-09-25 17:54:57 -07002600 }
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002601
2602 /* Descriptor for chosen Tx queue */
Zhu Yib481de92007-09-25 17:54:57 -07002603 txq = &priv->txq[txq_id];
2604 q = &txq->q;
2605
2606 spin_lock_irqsave(&priv->lock, flags);
2607
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002608 /* Set up first empty TFD within this queue's circular TFD buffer */
Tomas Winklerfc4b6852007-10-25 17:15:24 +08002609 tfd = &txq->bd[q->write_ptr];
Zhu Yib481de92007-09-25 17:54:57 -07002610 memset(tfd, 0, sizeof(*tfd));
2611 control_flags = (u32 *) tfd;
Tomas Winklerfc4b6852007-10-25 17:15:24 +08002612 idx = get_cmd_index(q, q->write_ptr, 0);
Zhu Yib481de92007-09-25 17:54:57 -07002613
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002614 /* Set up driver data for this TFD */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002615 memset(&(txq->txb[q->write_ptr]), 0, sizeof(struct iwl4965_tx_info));
Tomas Winklerfc4b6852007-10-25 17:15:24 +08002616 txq->txb[q->write_ptr].skb[0] = skb;
2617 memcpy(&(txq->txb[q->write_ptr].status.control),
Zhu Yib481de92007-09-25 17:54:57 -07002618 ctl, sizeof(struct ieee80211_tx_control));
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002619
2620 /* Set up first empty entry in queue's array of Tx/cmd buffers */
Zhu Yib481de92007-09-25 17:54:57 -07002621 out_cmd = &txq->cmd[idx];
2622 memset(&out_cmd->hdr, 0, sizeof(out_cmd->hdr));
2623 memset(&out_cmd->cmd.tx, 0, sizeof(out_cmd->cmd.tx));
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002624
2625 /*
2626 * Set up the Tx-command (not MAC!) header.
2627 * Store the chosen Tx queue and TFD index within the sequence field;
2628 * after Tx, uCode's Tx response will return this value so driver can
2629 * locate the frame within the tx queue and do post-tx processing.
2630 */
Zhu Yib481de92007-09-25 17:54:57 -07002631 out_cmd->hdr.cmd = REPLY_TX;
2632 out_cmd->hdr.sequence = cpu_to_le16((u16)(QUEUE_TO_SEQ(txq_id) |
Tomas Winklerfc4b6852007-10-25 17:15:24 +08002633 INDEX_TO_SEQ(q->write_ptr)));
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002634
2635 /* Copy MAC header from skb into command buffer */
Zhu Yib481de92007-09-25 17:54:57 -07002636 memcpy(out_cmd->cmd.tx.hdr, hdr, hdr_len);
2637
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002638 /*
2639 * Use the first empty entry in this queue's command buffer array
2640 * to contain the Tx command and MAC header concatenated together
2641 * (payload data will be in another buffer).
2642 * Size of this varies, due to varying MAC header length.
2643 * If end is not dword aligned, we'll have 2 extra bytes at the end
2644 * of the MAC header (device reads on dword boundaries).
2645 * We'll tell device about this padding later.
2646 */
Zhu Yib481de92007-09-25 17:54:57 -07002647 len = priv->hw_setting.tx_cmd_len +
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002648 sizeof(struct iwl4965_cmd_header) + hdr_len;
Zhu Yib481de92007-09-25 17:54:57 -07002649
2650 len_org = len;
2651 len = (len + 3) & ~3;
2652
2653 if (len_org != len)
2654 len_org = 1;
2655 else
2656 len_org = 0;
2657
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002658 /* Physical address of this Tx command's header (not MAC header!),
2659 * within command buffer array. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002660 txcmd_phys = txq->dma_addr_cmd + sizeof(struct iwl4965_cmd) * idx +
2661 offsetof(struct iwl4965_cmd, hdr);
Zhu Yib481de92007-09-25 17:54:57 -07002662
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002663 /* Add buffer containing Tx command and MAC(!) header to TFD's
2664 * first entry */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002665 iwl4965_hw_txq_attach_buf_to_tfd(priv, tfd, txcmd_phys, len);
Zhu Yib481de92007-09-25 17:54:57 -07002666
2667 if (!(ctl->flags & IEEE80211_TXCTL_DO_NOT_ENCRYPT))
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07002668 iwl4965_build_tx_cmd_hwcrypto(priv, ctl, out_cmd, skb, sta_id);
Zhu Yib481de92007-09-25 17:54:57 -07002669
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002670 /* Set up TFD's 2nd entry to point directly to remainder of skb,
2671 * if any (802.11 null frames have no payload). */
Zhu Yib481de92007-09-25 17:54:57 -07002672 len = skb->len - hdr_len;
2673 if (len) {
2674 phys_addr = pci_map_single(priv->pci_dev, skb->data + hdr_len,
2675 len, PCI_DMA_TODEVICE);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002676 iwl4965_hw_txq_attach_buf_to_tfd(priv, tfd, phys_addr, len);
Zhu Yib481de92007-09-25 17:54:57 -07002677 }
2678
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002679 /* Tell 4965 about any 2-byte padding after MAC header */
Zhu Yib481de92007-09-25 17:54:57 -07002680 if (len_org)
2681 out_cmd->cmd.tx.tx_flags |= TX_CMD_FLG_MH_PAD_MSK;
2682
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002683 /* Total # bytes to be transmitted */
Zhu Yib481de92007-09-25 17:54:57 -07002684 len = (u16)skb->len;
2685 out_cmd->cmd.tx.len = cpu_to_le16(len);
2686
2687 /* TODO need this for burst mode later on */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002688 iwl4965_build_tx_cmd_basic(priv, out_cmd, ctl, hdr, unicast, sta_id);
Zhu Yib481de92007-09-25 17:54:57 -07002689
2690 /* set is_hcca to 0; it probably will never be implemented */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002691 iwl4965_hw_build_tx_cmd_rate(priv, out_cmd, ctl, hdr, sta_id, 0);
Zhu Yib481de92007-09-25 17:54:57 -07002692
Tomas Winkler19758be2008-03-12 16:58:51 -07002693 iwl_update_tx_stats(priv, fc, len);
2694
Tomas Winkler87e4f7d2008-01-14 17:46:16 -08002695 scratch_phys = txcmd_phys + sizeof(struct iwl4965_cmd_header) +
2696 offsetof(struct iwl4965_tx_cmd, scratch);
2697 out_cmd->cmd.tx.dram_lsb_ptr = cpu_to_le32(scratch_phys);
2698 out_cmd->cmd.tx.dram_msb_ptr = iwl_get_dma_hi_address(scratch_phys);
2699
Zhu Yib481de92007-09-25 17:54:57 -07002700 if (!ieee80211_get_morefrag(hdr)) {
2701 txq->need_update = 1;
2702 if (qc) {
2703 u8 tid = (u8)(le16_to_cpu(*qc) & 0xf);
2704 priv->stations[sta_id].tid[tid].seq_number = seq_number;
2705 }
2706 } else {
2707 wait_write_ptr = 1;
2708 txq->need_update = 0;
2709 }
2710
Tomas Winkler0a6857e2008-03-12 16:58:49 -07002711 iwl_print_hex_dump(IWL_DL_TX, out_cmd->cmd.payload,
Zhu Yib481de92007-09-25 17:54:57 -07002712 sizeof(out_cmd->cmd.tx));
2713
Tomas Winkler0a6857e2008-03-12 16:58:49 -07002714 iwl_print_hex_dump(IWL_DL_TX, (u8 *)out_cmd->cmd.tx.hdr,
Zhu Yib481de92007-09-25 17:54:57 -07002715 ieee80211_get_hdrlen(fc));
2716
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002717 /* Set up entry for this TFD in Tx byte-count array */
Zhu Yib481de92007-09-25 17:54:57 -07002718 iwl4965_tx_queue_update_wr_ptr(priv, txq, len);
2719
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002720 /* Tell device the write index *just past* this latest filled TFD */
Tomas Winklerc54b6792008-03-06 17:36:53 -08002721 q->write_ptr = iwl_queue_inc_wrap(q->write_ptr, q->n_bd);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002722 rc = iwl4965_tx_queue_update_write_ptr(priv, txq);
Zhu Yib481de92007-09-25 17:54:57 -07002723 spin_unlock_irqrestore(&priv->lock, flags);
2724
2725 if (rc)
2726 return rc;
2727
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002728 if ((iwl4965_queue_space(q) < q->high_mark)
Zhu Yib481de92007-09-25 17:54:57 -07002729 && priv->mac80211_registered) {
2730 if (wait_write_ptr) {
2731 spin_lock_irqsave(&priv->lock, flags);
2732 txq->need_update = 1;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002733 iwl4965_tx_queue_update_write_ptr(priv, txq);
Zhu Yib481de92007-09-25 17:54:57 -07002734 spin_unlock_irqrestore(&priv->lock, flags);
2735 }
2736
2737 ieee80211_stop_queue(priv->hw, ctl->queue);
2738 }
2739
2740 return 0;
2741
2742drop_unlock:
2743 spin_unlock_irqrestore(&priv->lock, flags);
2744drop:
2745 return -1;
2746}
2747
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002748static void iwl4965_set_rate(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002749{
Johannes Berg8318d782008-01-24 19:38:38 +01002750 const struct ieee80211_supported_band *hw = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07002751 struct ieee80211_rate *rate;
2752 int i;
2753
Johannes Berg8318d782008-01-24 19:38:38 +01002754 hw = iwl4965_get_hw_mode(priv, priv->band);
Saleem Abdulrasoolc4ba9622007-11-18 23:59:08 -08002755 if (!hw) {
2756 IWL_ERROR("Failed to set rate: unable to get hw mode\n");
2757 return;
2758 }
Zhu Yib481de92007-09-25 17:54:57 -07002759
2760 priv->active_rate = 0;
2761 priv->active_rate_basic = 0;
2762
Johannes Berg8318d782008-01-24 19:38:38 +01002763 for (i = 0; i < hw->n_bitrates; i++) {
2764 rate = &(hw->bitrates[i]);
2765 if (rate->hw_value < IWL_RATE_COUNT)
2766 priv->active_rate |= (1 << rate->hw_value);
Zhu Yib481de92007-09-25 17:54:57 -07002767 }
2768
2769 IWL_DEBUG_RATE("Set active_rate = %0x, active_rate_basic = %0x\n",
2770 priv->active_rate, priv->active_rate_basic);
2771
2772 /*
2773 * If a basic rate is configured, then use it (adding IWL_RATE_1M_MASK)
2774 * otherwise set it to the default of all CCK rates and 6, 12, 24 for
2775 * OFDM
2776 */
2777 if (priv->active_rate_basic & IWL_CCK_BASIC_RATES_MASK)
2778 priv->staging_rxon.cck_basic_rates =
2779 ((priv->active_rate_basic &
2780 IWL_CCK_RATES_MASK) >> IWL_FIRST_CCK_RATE) & 0xF;
2781 else
2782 priv->staging_rxon.cck_basic_rates =
2783 (IWL_CCK_BASIC_RATES_MASK >> IWL_FIRST_CCK_RATE) & 0xF;
2784
2785 if (priv->active_rate_basic & IWL_OFDM_BASIC_RATES_MASK)
2786 priv->staging_rxon.ofdm_basic_rates =
2787 ((priv->active_rate_basic &
2788 (IWL_OFDM_BASIC_RATES_MASK | IWL_RATE_6M_MASK)) >>
2789 IWL_FIRST_OFDM_RATE) & 0xFF;
2790 else
2791 priv->staging_rxon.ofdm_basic_rates =
2792 (IWL_OFDM_BASIC_RATES_MASK >> IWL_FIRST_OFDM_RATE) & 0xFF;
2793}
2794
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002795static void iwl4965_radio_kill_sw(struct iwl_priv *priv, int disable_radio)
Zhu Yib481de92007-09-25 17:54:57 -07002796{
2797 unsigned long flags;
2798
2799 if (!!disable_radio == test_bit(STATUS_RF_KILL_SW, &priv->status))
2800 return;
2801
2802 IWL_DEBUG_RF_KILL("Manual SW RF KILL set to: RADIO %s\n",
2803 disable_radio ? "OFF" : "ON");
2804
2805 if (disable_radio) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002806 iwl4965_scan_cancel(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002807 /* FIXME: This is a workaround for AP */
2808 if (priv->iw_mode != IEEE80211_IF_TYPE_AP) {
2809 spin_lock_irqsave(&priv->lock, flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002810 iwl4965_write32(priv, CSR_UCODE_DRV_GP1_SET,
Zhu Yib481de92007-09-25 17:54:57 -07002811 CSR_UCODE_SW_BIT_RFKILL);
2812 spin_unlock_irqrestore(&priv->lock, flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002813 iwl4965_send_card_state(priv, CARD_STATE_CMD_DISABLE, 0);
Zhu Yib481de92007-09-25 17:54:57 -07002814 set_bit(STATUS_RF_KILL_SW, &priv->status);
2815 }
2816 return;
2817 }
2818
2819 spin_lock_irqsave(&priv->lock, flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002820 iwl4965_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
Zhu Yib481de92007-09-25 17:54:57 -07002821
2822 clear_bit(STATUS_RF_KILL_SW, &priv->status);
2823 spin_unlock_irqrestore(&priv->lock, flags);
2824
2825 /* wake up ucode */
2826 msleep(10);
2827
2828 spin_lock_irqsave(&priv->lock, flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002829 iwl4965_read32(priv, CSR_UCODE_DRV_GP1);
2830 if (!iwl4965_grab_nic_access(priv))
2831 iwl4965_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002832 spin_unlock_irqrestore(&priv->lock, flags);
2833
2834 if (test_bit(STATUS_RF_KILL_HW, &priv->status)) {
2835 IWL_DEBUG_RF_KILL("Can not turn radio back on - "
2836 "disabled by HW switch\n");
2837 return;
2838 }
2839
2840 queue_work(priv->workqueue, &priv->restart);
2841 return;
2842}
2843
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002844void iwl4965_set_decrypted_flag(struct iwl_priv *priv, struct sk_buff *skb,
Zhu Yib481de92007-09-25 17:54:57 -07002845 u32 decrypt_res, struct ieee80211_rx_status *stats)
2846{
2847 u16 fc =
2848 le16_to_cpu(((struct ieee80211_hdr *)skb->data)->frame_control);
2849
2850 if (priv->active_rxon.filter_flags & RXON_FILTER_DIS_DECRYPT_MSK)
2851 return;
2852
2853 if (!(fc & IEEE80211_FCTL_PROTECTED))
2854 return;
2855
2856 IWL_DEBUG_RX("decrypt_res:0x%x\n", decrypt_res);
2857 switch (decrypt_res & RX_RES_STATUS_SEC_TYPE_MSK) {
2858 case RX_RES_STATUS_SEC_TYPE_TKIP:
Emmanuel Grumbach17e476b2008-03-19 16:41:42 -07002859 /* The uCode has got a bad phase 1 Key, pushes the packet.
2860 * Decryption will be done in SW. */
2861 if ((decrypt_res & RX_RES_STATUS_DECRYPT_TYPE_MSK) ==
2862 RX_RES_STATUS_BAD_KEY_TTAK)
2863 break;
2864
Zhu Yib481de92007-09-25 17:54:57 -07002865 if ((decrypt_res & RX_RES_STATUS_DECRYPT_TYPE_MSK) ==
2866 RX_RES_STATUS_BAD_ICV_MIC)
2867 stats->flag |= RX_FLAG_MMIC_ERROR;
2868 case RX_RES_STATUS_SEC_TYPE_WEP:
2869 case RX_RES_STATUS_SEC_TYPE_CCMP:
2870 if ((decrypt_res & RX_RES_STATUS_DECRYPT_TYPE_MSK) ==
2871 RX_RES_STATUS_DECRYPT_OK) {
2872 IWL_DEBUG_RX("hw decrypt successfully!!!\n");
2873 stats->flag |= RX_FLAG_DECRYPTED;
2874 }
2875 break;
2876
2877 default:
2878 break;
2879 }
2880}
2881
Zhu Yib481de92007-09-25 17:54:57 -07002882
2883#define IWL_PACKET_RETRY_TIME HZ
2884
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002885int iwl4965_is_duplicate_packet(struct iwl_priv *priv, struct ieee80211_hdr *header)
Zhu Yib481de92007-09-25 17:54:57 -07002886{
2887 u16 sc = le16_to_cpu(header->seq_ctrl);
2888 u16 seq = (sc & IEEE80211_SCTL_SEQ) >> 4;
2889 u16 frag = sc & IEEE80211_SCTL_FRAG;
2890 u16 *last_seq, *last_frag;
2891 unsigned long *last_time;
2892
2893 switch (priv->iw_mode) {
2894 case IEEE80211_IF_TYPE_IBSS:{
2895 struct list_head *p;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002896 struct iwl4965_ibss_seq *entry = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07002897 u8 *mac = header->addr2;
2898 int index = mac[5] & (IWL_IBSS_MAC_HASH_SIZE - 1);
2899
2900 __list_for_each(p, &priv->ibss_mac_hash[index]) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002901 entry = list_entry(p, struct iwl4965_ibss_seq, list);
Zhu Yib481de92007-09-25 17:54:57 -07002902 if (!compare_ether_addr(entry->mac, mac))
2903 break;
2904 }
2905 if (p == &priv->ibss_mac_hash[index]) {
2906 entry = kzalloc(sizeof(*entry), GFP_ATOMIC);
2907 if (!entry) {
Ian Schrambc434dd2007-10-25 17:15:29 +08002908 IWL_ERROR("Cannot malloc new mac entry\n");
Zhu Yib481de92007-09-25 17:54:57 -07002909 return 0;
2910 }
2911 memcpy(entry->mac, mac, ETH_ALEN);
2912 entry->seq_num = seq;
2913 entry->frag_num = frag;
2914 entry->packet_time = jiffies;
Ian Schrambc434dd2007-10-25 17:15:29 +08002915 list_add(&entry->list, &priv->ibss_mac_hash[index]);
Zhu Yib481de92007-09-25 17:54:57 -07002916 return 0;
2917 }
2918 last_seq = &entry->seq_num;
2919 last_frag = &entry->frag_num;
2920 last_time = &entry->packet_time;
2921 break;
2922 }
2923 case IEEE80211_IF_TYPE_STA:
2924 last_seq = &priv->last_seq_num;
2925 last_frag = &priv->last_frag_num;
2926 last_time = &priv->last_packet_time;
2927 break;
2928 default:
2929 return 0;
2930 }
2931 if ((*last_seq == seq) &&
2932 time_after(*last_time + IWL_PACKET_RETRY_TIME, jiffies)) {
2933 if (*last_frag == frag)
2934 goto drop;
2935 if (*last_frag + 1 != frag)
2936 /* out-of-order fragment */
2937 goto drop;
2938 } else
2939 *last_seq = seq;
2940
2941 *last_frag = frag;
2942 *last_time = jiffies;
2943 return 0;
2944
2945 drop:
2946 return 1;
2947}
2948
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08002949#ifdef CONFIG_IWL4965_SPECTRUM_MEASUREMENT
Zhu Yib481de92007-09-25 17:54:57 -07002950
2951#include "iwl-spectrum.h"
2952
2953#define BEACON_TIME_MASK_LOW 0x00FFFFFF
2954#define BEACON_TIME_MASK_HIGH 0xFF000000
2955#define TIME_UNIT 1024
2956
2957/*
2958 * extended beacon time format
2959 * time in usec will be changed into a 32-bit value in 8:24 format
2960 * the high 1 byte is the beacon counts
2961 * the lower 3 bytes is the time in usec within one beacon interval
2962 */
2963
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002964static u32 iwl4965_usecs_to_beacons(u32 usec, u32 beacon_interval)
Zhu Yib481de92007-09-25 17:54:57 -07002965{
2966 u32 quot;
2967 u32 rem;
2968 u32 interval = beacon_interval * 1024;
2969
2970 if (!interval || !usec)
2971 return 0;
2972
2973 quot = (usec / interval) & (BEACON_TIME_MASK_HIGH >> 24);
2974 rem = (usec % interval) & BEACON_TIME_MASK_LOW;
2975
2976 return (quot << 24) + rem;
2977}
2978
2979/* base is usually what we get from ucode with each received frame,
2980 * the same as HW timer counter counting down
2981 */
2982
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002983static __le32 iwl4965_add_beacon_time(u32 base, u32 addon, u32 beacon_interval)
Zhu Yib481de92007-09-25 17:54:57 -07002984{
2985 u32 base_low = base & BEACON_TIME_MASK_LOW;
2986 u32 addon_low = addon & BEACON_TIME_MASK_LOW;
2987 u32 interval = beacon_interval * TIME_UNIT;
2988 u32 res = (base & BEACON_TIME_MASK_HIGH) +
2989 (addon & BEACON_TIME_MASK_HIGH);
2990
2991 if (base_low > addon_low)
2992 res += base_low - addon_low;
2993 else if (base_low < addon_low) {
2994 res += interval + base_low - addon_low;
2995 res += (1 << 24);
2996 } else
2997 res += (1 << 24);
2998
2999 return cpu_to_le32(res);
3000}
3001
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003002static int iwl4965_get_measurement(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07003003 struct ieee80211_measurement_params *params,
3004 u8 type)
3005{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003006 struct iwl4965_spectrum_cmd spectrum;
3007 struct iwl4965_rx_packet *res;
3008 struct iwl4965_host_cmd cmd = {
Zhu Yib481de92007-09-25 17:54:57 -07003009 .id = REPLY_SPECTRUM_MEASUREMENT_CMD,
3010 .data = (void *)&spectrum,
3011 .meta.flags = CMD_WANT_SKB,
3012 };
3013 u32 add_time = le64_to_cpu(params->start_time);
3014 int rc;
3015 int spectrum_resp_status;
3016 int duration = le16_to_cpu(params->duration);
3017
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003018 if (iwl4965_is_associated(priv))
Zhu Yib481de92007-09-25 17:54:57 -07003019 add_time =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003020 iwl4965_usecs_to_beacons(
Zhu Yib481de92007-09-25 17:54:57 -07003021 le64_to_cpu(params->start_time) - priv->last_tsf,
3022 le16_to_cpu(priv->rxon_timing.beacon_interval));
3023
3024 memset(&spectrum, 0, sizeof(spectrum));
3025
3026 spectrum.channel_count = cpu_to_le16(1);
3027 spectrum.flags =
3028 RXON_FLG_TSF2HOST_MSK | RXON_FLG_ANT_A_MSK | RXON_FLG_DIS_DIV_MSK;
3029 spectrum.filter_flags = MEASUREMENT_FILTER_FLAG;
3030 cmd.len = sizeof(spectrum);
3031 spectrum.len = cpu_to_le16(cmd.len - sizeof(spectrum.len));
3032
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003033 if (iwl4965_is_associated(priv))
Zhu Yib481de92007-09-25 17:54:57 -07003034 spectrum.start_time =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003035 iwl4965_add_beacon_time(priv->last_beacon_time,
Zhu Yib481de92007-09-25 17:54:57 -07003036 add_time,
3037 le16_to_cpu(priv->rxon_timing.beacon_interval));
3038 else
3039 spectrum.start_time = 0;
3040
3041 spectrum.channels[0].duration = cpu_to_le32(duration * TIME_UNIT);
3042 spectrum.channels[0].channel = params->channel;
3043 spectrum.channels[0].type = type;
3044 if (priv->active_rxon.flags & RXON_FLG_BAND_24G_MSK)
3045 spectrum.flags |= RXON_FLG_BAND_24G_MSK |
3046 RXON_FLG_AUTO_DETECT_MSK | RXON_FLG_TGG_PROTECT_MSK;
3047
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003048 rc = iwl4965_send_cmd_sync(priv, &cmd);
Zhu Yib481de92007-09-25 17:54:57 -07003049 if (rc)
3050 return rc;
3051
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003052 res = (struct iwl4965_rx_packet *)cmd.meta.u.skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07003053 if (res->hdr.flags & IWL_CMD_FAILED_MSK) {
3054 IWL_ERROR("Bad return from REPLY_RX_ON_ASSOC command\n");
3055 rc = -EIO;
3056 }
3057
3058 spectrum_resp_status = le16_to_cpu(res->u.spectrum.status);
3059 switch (spectrum_resp_status) {
3060 case 0: /* Command will be handled */
3061 if (res->u.spectrum.id != 0xff) {
3062 IWL_DEBUG_INFO
3063 ("Replaced existing measurement: %d\n",
3064 res->u.spectrum.id);
3065 priv->measurement_status &= ~MEASUREMENT_READY;
3066 }
3067 priv->measurement_status |= MEASUREMENT_ACTIVE;
3068 rc = 0;
3069 break;
3070
3071 case 1: /* Command will not be handled */
3072 rc = -EAGAIN;
3073 break;
3074 }
3075
3076 dev_kfree_skb_any(cmd.meta.u.skb);
3077
3078 return rc;
3079}
3080#endif
3081
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003082static void iwl4965_txstatus_to_ieee(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003083 struct iwl4965_tx_info *tx_sta)
Zhu Yib481de92007-09-25 17:54:57 -07003084{
3085
3086 tx_sta->status.ack_signal = 0;
3087 tx_sta->status.excessive_retries = 0;
3088 tx_sta->status.queue_length = 0;
3089 tx_sta->status.queue_number = 0;
3090
3091 if (in_interrupt())
3092 ieee80211_tx_status_irqsafe(priv->hw,
3093 tx_sta->skb[0], &(tx_sta->status));
3094 else
3095 ieee80211_tx_status(priv->hw,
3096 tx_sta->skb[0], &(tx_sta->status));
3097
3098 tx_sta->skb[0] = NULL;
3099}
3100
3101/**
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003102 * iwl4965_tx_queue_reclaim - Reclaim Tx queue entries already Tx'd
Zhu Yib481de92007-09-25 17:54:57 -07003103 *
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003104 * When FW advances 'R' index, all entries between old and new 'R' index
3105 * need to be reclaimed. As result, some free space forms. If there is
3106 * enough free space (> low mark), wake the stack that feeds us.
Zhu Yib481de92007-09-25 17:54:57 -07003107 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003108int iwl4965_tx_queue_reclaim(struct iwl_priv *priv, int txq_id, int index)
Zhu Yib481de92007-09-25 17:54:57 -07003109{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003110 struct iwl4965_tx_queue *txq = &priv->txq[txq_id];
3111 struct iwl4965_queue *q = &txq->q;
Zhu Yib481de92007-09-25 17:54:57 -07003112 int nfreed = 0;
3113
3114 if ((index >= q->n_bd) || (x2_queue_used(q, index) == 0)) {
3115 IWL_ERROR("Read index for DMA queue txq id (%d), index %d, "
3116 "is out of range [0-%d] %d %d.\n", txq_id,
Tomas Winklerfc4b6852007-10-25 17:15:24 +08003117 index, q->n_bd, q->write_ptr, q->read_ptr);
Zhu Yib481de92007-09-25 17:54:57 -07003118 return 0;
3119 }
3120
Tomas Winklerc54b6792008-03-06 17:36:53 -08003121 for (index = iwl_queue_inc_wrap(index, q->n_bd);
Tomas Winklerfc4b6852007-10-25 17:15:24 +08003122 q->read_ptr != index;
Tomas Winklerc54b6792008-03-06 17:36:53 -08003123 q->read_ptr = iwl_queue_inc_wrap(q->read_ptr, q->n_bd)) {
Zhu Yib481de92007-09-25 17:54:57 -07003124 if (txq_id != IWL_CMD_QUEUE_NUM) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003125 iwl4965_txstatus_to_ieee(priv,
Tomas Winklerfc4b6852007-10-25 17:15:24 +08003126 &(txq->txb[txq->q.read_ptr]));
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003127 iwl4965_hw_txq_free_tfd(priv, txq);
Zhu Yib481de92007-09-25 17:54:57 -07003128 } else if (nfreed > 1) {
3129 IWL_ERROR("HCMD skipped: index (%d) %d %d\n", index,
Tomas Winklerfc4b6852007-10-25 17:15:24 +08003130 q->write_ptr, q->read_ptr);
Zhu Yib481de92007-09-25 17:54:57 -07003131 queue_work(priv->workqueue, &priv->restart);
3132 }
3133 nfreed++;
3134 }
3135
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003136/* if (iwl4965_queue_space(q) > q->low_mark && (txq_id >= 0) &&
Zhu Yib481de92007-09-25 17:54:57 -07003137 (txq_id != IWL_CMD_QUEUE_NUM) &&
3138 priv->mac80211_registered)
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003139 ieee80211_wake_queue(priv->hw, txq_id); */
Zhu Yib481de92007-09-25 17:54:57 -07003140
3141
3142 return nfreed;
3143}
3144
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003145static int iwl4965_is_tx_success(u32 status)
Zhu Yib481de92007-09-25 17:54:57 -07003146{
3147 status &= TX_STATUS_MSK;
3148 return (status == TX_STATUS_SUCCESS)
3149 || (status == TX_STATUS_DIRECT_DONE);
3150}
3151
3152/******************************************************************************
3153 *
3154 * Generic RX handler implementations
3155 *
3156 ******************************************************************************/
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08003157#ifdef CONFIG_IWL4965_HT
Zhu Yib481de92007-09-25 17:54:57 -07003158
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003159static inline int iwl4965_get_ra_sta_id(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07003160 struct ieee80211_hdr *hdr)
3161{
3162 if (priv->iw_mode == IEEE80211_IF_TYPE_STA)
3163 return IWL_AP_ID;
3164 else {
3165 u8 *da = ieee80211_get_DA(hdr);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003166 return iwl4965_hw_find_station(priv, da);
Zhu Yib481de92007-09-25 17:54:57 -07003167 }
3168}
3169
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003170static struct ieee80211_hdr *iwl4965_tx_queue_get_hdr(
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003171 struct iwl_priv *priv, int txq_id, int idx)
Zhu Yib481de92007-09-25 17:54:57 -07003172{
3173 if (priv->txq[txq_id].txb[idx].skb[0])
3174 return (struct ieee80211_hdr *)priv->txq[txq_id].
3175 txb[idx].skb[0]->data;
3176 return NULL;
3177}
3178
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003179static inline u32 iwl4965_get_scd_ssn(struct iwl4965_tx_resp *tx_resp)
Zhu Yib481de92007-09-25 17:54:57 -07003180{
3181 __le32 *scd_ssn = (__le32 *)((u32 *)&tx_resp->status +
3182 tx_resp->frame_count);
3183 return le32_to_cpu(*scd_ssn) & MAX_SN;
3184
3185}
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003186
3187/**
3188 * iwl4965_tx_status_reply_tx - Handle Tx rspnse for frames in aggregation queue
3189 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003190static int iwl4965_tx_status_reply_tx(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003191 struct iwl4965_ht_agg *agg,
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003192 struct iwl4965_tx_resp_agg *tx_resp,
Zhu Yib481de92007-09-25 17:54:57 -07003193 u16 start_idx)
3194{
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003195 u16 status;
3196 struct agg_tx_status *frame_status = &tx_resp->status;
Zhu Yib481de92007-09-25 17:54:57 -07003197 struct ieee80211_tx_status *tx_status = NULL;
3198 struct ieee80211_hdr *hdr = NULL;
3199 int i, sh;
3200 int txq_id, idx;
3201 u16 seq;
3202
3203 if (agg->wait_for_ba)
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003204 IWL_DEBUG_TX_REPLY("got tx response w/o block-ack\n");
Zhu Yib481de92007-09-25 17:54:57 -07003205
3206 agg->frame_count = tx_resp->frame_count;
3207 agg->start_idx = start_idx;
3208 agg->rate_n_flags = le32_to_cpu(tx_resp->rate_n_flags);
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003209 agg->bitmap = 0;
Zhu Yib481de92007-09-25 17:54:57 -07003210
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003211 /* # frames attempted by Tx command */
Zhu Yib481de92007-09-25 17:54:57 -07003212 if (agg->frame_count == 1) {
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003213 /* Only one frame was attempted; no block-ack will arrive */
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003214 status = le16_to_cpu(frame_status[0].status);
3215 seq = le16_to_cpu(frame_status[0].sequence);
3216 idx = SEQ_TO_INDEX(seq);
3217 txq_id = SEQ_TO_QUEUE(seq);
Zhu Yib481de92007-09-25 17:54:57 -07003218
Zhu Yib481de92007-09-25 17:54:57 -07003219 /* FIXME: code repetition */
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003220 IWL_DEBUG_TX_REPLY("FrameCnt = %d, StartIdx=%d idx=%d\n",
3221 agg->frame_count, agg->start_idx, idx);
Zhu Yib481de92007-09-25 17:54:57 -07003222
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003223 tx_status = &(priv->txq[txq_id].txb[idx].status);
Zhu Yib481de92007-09-25 17:54:57 -07003224 tx_status->retry_count = tx_resp->failure_frame;
3225 tx_status->queue_number = status & 0xff;
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003226 tx_status->queue_length = tx_resp->failure_rts;
3227 tx_status->control.flags &= ~IEEE80211_TXCTL_AMPDU;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003228 tx_status->flags = iwl4965_is_tx_success(status)?
Zhu Yib481de92007-09-25 17:54:57 -07003229 IEEE80211_TX_STATUS_ACK : 0;
Ron Rindjunsky4c424e42008-03-04 18:09:27 -08003230 iwl4965_hwrate_to_tx_control(priv,
3231 le32_to_cpu(tx_resp->rate_n_flags),
3232 &tx_status->control);
Zhu Yib481de92007-09-25 17:54:57 -07003233 /* FIXME: code repetition end */
3234
3235 IWL_DEBUG_TX_REPLY("1 Frame 0x%x failure :%d\n",
3236 status & 0xff, tx_resp->failure_frame);
3237 IWL_DEBUG_TX_REPLY("Rate Info rate_n_flags=%x\n",
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003238 iwl4965_hw_get_rate_n_flags(tx_resp->rate_n_flags));
Zhu Yib481de92007-09-25 17:54:57 -07003239
3240 agg->wait_for_ba = 0;
3241 } else {
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003242 /* Two or more frames were attempted; expect block-ack */
Zhu Yib481de92007-09-25 17:54:57 -07003243 u64 bitmap = 0;
3244 int start = agg->start_idx;
3245
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003246 /* Construct bit-map of pending frames within Tx window */
Zhu Yib481de92007-09-25 17:54:57 -07003247 for (i = 0; i < agg->frame_count; i++) {
3248 u16 sc;
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003249 status = le16_to_cpu(frame_status[i].status);
3250 seq = le16_to_cpu(frame_status[i].sequence);
Zhu Yib481de92007-09-25 17:54:57 -07003251 idx = SEQ_TO_INDEX(seq);
3252 txq_id = SEQ_TO_QUEUE(seq);
3253
3254 if (status & (AGG_TX_STATE_FEW_BYTES_MSK |
3255 AGG_TX_STATE_ABORT_MSK))
3256 continue;
3257
3258 IWL_DEBUG_TX_REPLY("FrameCnt = %d, txq_id=%d idx=%d\n",
3259 agg->frame_count, txq_id, idx);
3260
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003261 hdr = iwl4965_tx_queue_get_hdr(priv, txq_id, idx);
Zhu Yib481de92007-09-25 17:54:57 -07003262
3263 sc = le16_to_cpu(hdr->seq_ctrl);
3264 if (idx != (SEQ_TO_SN(sc) & 0xff)) {
3265 IWL_ERROR("BUG_ON idx doesn't match seq control"
3266 " idx=%d, seq_idx=%d, seq=%d\n",
3267 idx, SEQ_TO_SN(sc),
3268 hdr->seq_ctrl);
3269 return -1;
3270 }
3271
3272 IWL_DEBUG_TX_REPLY("AGG Frame i=%d idx %d seq=%d\n",
3273 i, idx, SEQ_TO_SN(sc));
3274
3275 sh = idx - start;
3276 if (sh > 64) {
3277 sh = (start - idx) + 0xff;
3278 bitmap = bitmap << sh;
3279 sh = 0;
3280 start = idx;
3281 } else if (sh < -64)
3282 sh = 0xff - (start - idx);
3283 else if (sh < 0) {
3284 sh = start - idx;
3285 start = idx;
3286 bitmap = bitmap << sh;
3287 sh = 0;
3288 }
3289 bitmap |= (1 << sh);
3290 IWL_DEBUG_TX_REPLY("start=%d bitmap=0x%x\n",
3291 start, (u32)(bitmap & 0xFFFFFFFF));
3292 }
3293
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003294 agg->bitmap = bitmap;
Zhu Yib481de92007-09-25 17:54:57 -07003295 agg->start_idx = start;
3296 agg->rate_n_flags = le32_to_cpu(tx_resp->rate_n_flags);
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003297 IWL_DEBUG_TX_REPLY("Frames %d start_idx=%d bitmap=0x%llx\n",
Zhu Yib481de92007-09-25 17:54:57 -07003298 agg->frame_count, agg->start_idx,
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003299 agg->bitmap);
Zhu Yib481de92007-09-25 17:54:57 -07003300
3301 if (bitmap)
3302 agg->wait_for_ba = 1;
3303 }
3304 return 0;
3305}
3306#endif
Zhu Yib481de92007-09-25 17:54:57 -07003307
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003308/**
3309 * iwl4965_rx_reply_tx - Handle standard (non-aggregation) Tx response
3310 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003311static void iwl4965_rx_reply_tx(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003312 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003313{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003314 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07003315 u16 sequence = le16_to_cpu(pkt->hdr.sequence);
3316 int txq_id = SEQ_TO_QUEUE(sequence);
3317 int index = SEQ_TO_INDEX(sequence);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003318 struct iwl4965_tx_queue *txq = &priv->txq[txq_id];
Zhu Yib481de92007-09-25 17:54:57 -07003319 struct ieee80211_tx_status *tx_status;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003320 struct iwl4965_tx_resp *tx_resp = (void *)&pkt->u.raw[0];
Zhu Yib481de92007-09-25 17:54:57 -07003321 u32 status = le32_to_cpu(tx_resp->status);
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08003322#ifdef CONFIG_IWL4965_HT
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003323 int tid = MAX_TID_COUNT, sta_id = IWL_INVALID_STATION;
3324 struct ieee80211_hdr *hdr;
3325 __le16 *qc;
Zhu Yib481de92007-09-25 17:54:57 -07003326#endif
3327
3328 if ((index >= txq->q.n_bd) || (x2_queue_used(&txq->q, index) == 0)) {
3329 IWL_ERROR("Read index for DMA queue txq_id (%d) index %d "
3330 "is out of range [0-%d] %d %d\n", txq_id,
Tomas Winklerfc4b6852007-10-25 17:15:24 +08003331 index, txq->q.n_bd, txq->q.write_ptr,
3332 txq->q.read_ptr);
Zhu Yib481de92007-09-25 17:54:57 -07003333 return;
3334 }
3335
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08003336#ifdef CONFIG_IWL4965_HT
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003337 hdr = iwl4965_tx_queue_get_hdr(priv, txq_id, index);
3338 qc = ieee80211_get_qos_ctrl(hdr);
Zhu Yib481de92007-09-25 17:54:57 -07003339
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003340 if (qc)
Zhu Yib481de92007-09-25 17:54:57 -07003341 tid = le16_to_cpu(*qc) & 0xf;
3342
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003343 sta_id = iwl4965_get_ra_sta_id(priv, hdr);
3344 if (txq->sched_retry && unlikely(sta_id == IWL_INVALID_STATION)) {
3345 IWL_ERROR("Station not known\n");
3346 return;
3347 }
3348
3349 if (txq->sched_retry) {
3350 const u32 scd_ssn = iwl4965_get_scd_ssn(tx_resp);
3351 struct iwl4965_ht_agg *agg = NULL;
3352
3353 if (!qc)
Zhu Yib481de92007-09-25 17:54:57 -07003354 return;
Zhu Yib481de92007-09-25 17:54:57 -07003355
3356 agg = &priv->stations[sta_id].tid[tid].agg;
3357
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003358 iwl4965_tx_status_reply_tx(priv, agg,
3359 (struct iwl4965_tx_resp_agg *)tx_resp, index);
Zhu Yib481de92007-09-25 17:54:57 -07003360
3361 if ((tx_resp->frame_count == 1) &&
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003362 !iwl4965_is_tx_success(status)) {
Zhu Yib481de92007-09-25 17:54:57 -07003363 /* TODO: send BAR */
3364 }
3365
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003366 if (txq->q.read_ptr != (scd_ssn & 0xff)) {
3367 int freed;
Tomas Winklerc54b6792008-03-06 17:36:53 -08003368 index = iwl_queue_dec_wrap(scd_ssn & 0xff, txq->q.n_bd);
Zhu Yib481de92007-09-25 17:54:57 -07003369 IWL_DEBUG_TX_REPLY("Retry scheduler reclaim scd_ssn "
3370 "%d index %d\n", scd_ssn , index);
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003371 freed = iwl4965_tx_queue_reclaim(priv, txq_id, index);
3372 priv->stations[sta_id].tid[tid].tfds_in_queue -= freed;
3373
3374 if (iwl4965_queue_space(&txq->q) > txq->q.low_mark &&
3375 txq_id >= 0 && priv->mac80211_registered &&
3376 agg->state != IWL_EMPTYING_HW_QUEUE_DELBA)
3377 ieee80211_wake_queue(priv->hw, txq_id);
3378
3379 iwl4965_check_empty_hw_queue(priv, sta_id, tid, txq_id);
Zhu Yib481de92007-09-25 17:54:57 -07003380 }
3381 } else {
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08003382#endif /* CONFIG_IWL4965_HT */
Tomas Winklerfc4b6852007-10-25 17:15:24 +08003383 tx_status = &(txq->txb[txq->q.read_ptr].status);
Zhu Yib481de92007-09-25 17:54:57 -07003384
3385 tx_status->retry_count = tx_resp->failure_frame;
3386 tx_status->queue_number = status;
3387 tx_status->queue_length = tx_resp->bt_kill_count;
3388 tx_status->queue_length |= tx_resp->failure_rts;
Zhu Yib481de92007-09-25 17:54:57 -07003389 tx_status->flags =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003390 iwl4965_is_tx_success(status) ? IEEE80211_TX_STATUS_ACK : 0;
Ron Rindjunsky4c424e42008-03-04 18:09:27 -08003391 iwl4965_hwrate_to_tx_control(priv, le32_to_cpu(tx_resp->rate_n_flags),
3392 &tx_status->control);
Zhu Yib481de92007-09-25 17:54:57 -07003393
Zhu Yib481de92007-09-25 17:54:57 -07003394 IWL_DEBUG_TX("Tx queue %d Status %s (0x%08x) rate_n_flags 0x%x "
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003395 "retries %d\n", txq_id, iwl4965_get_tx_fail_reason(status),
Zhu Yib481de92007-09-25 17:54:57 -07003396 status, le32_to_cpu(tx_resp->rate_n_flags),
3397 tx_resp->failure_frame);
3398
3399 IWL_DEBUG_TX_REPLY("Tx queue reclaim %d\n", index);
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003400 if (index != -1) {
3401 int freed = iwl4965_tx_queue_reclaim(priv, txq_id, index);
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08003402#ifdef CONFIG_IWL4965_HT
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003403 if (tid != MAX_TID_COUNT)
3404 priv->stations[sta_id].tid[tid].tfds_in_queue -= freed;
3405 if (iwl4965_queue_space(&txq->q) > txq->q.low_mark &&
3406 (txq_id >= 0) &&
3407 priv->mac80211_registered)
3408 ieee80211_wake_queue(priv->hw, txq_id);
3409 if (tid != MAX_TID_COUNT)
3410 iwl4965_check_empty_hw_queue(priv, sta_id, tid, txq_id);
3411#endif
Zhu Yib481de92007-09-25 17:54:57 -07003412 }
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003413#ifdef CONFIG_IWL4965_HT
3414 }
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08003415#endif /* CONFIG_IWL4965_HT */
Zhu Yib481de92007-09-25 17:54:57 -07003416
3417 if (iwl_check_bits(status, TX_ABORT_REQUIRED_MSK))
3418 IWL_ERROR("TODO: Implement Tx ABORT REQUIRED!!!\n");
3419}
3420
3421
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003422static void iwl4965_rx_reply_alive(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003423 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003424{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003425 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
3426 struct iwl4965_alive_resp *palive;
Zhu Yib481de92007-09-25 17:54:57 -07003427 struct delayed_work *pwork;
3428
3429 palive = &pkt->u.alive_frame;
3430
3431 IWL_DEBUG_INFO("Alive ucode status 0x%08X revision "
3432 "0x%01X 0x%01X\n",
3433 palive->is_valid, palive->ver_type,
3434 palive->ver_subtype);
3435
3436 if (palive->ver_subtype == INITIALIZE_SUBTYPE) {
3437 IWL_DEBUG_INFO("Initialization Alive received.\n");
3438 memcpy(&priv->card_alive_init,
3439 &pkt->u.alive_frame,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003440 sizeof(struct iwl4965_init_alive_resp));
Zhu Yib481de92007-09-25 17:54:57 -07003441 pwork = &priv->init_alive_start;
3442 } else {
3443 IWL_DEBUG_INFO("Runtime Alive received.\n");
3444 memcpy(&priv->card_alive, &pkt->u.alive_frame,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003445 sizeof(struct iwl4965_alive_resp));
Zhu Yib481de92007-09-25 17:54:57 -07003446 pwork = &priv->alive_start;
3447 }
3448
3449 /* We delay the ALIVE response by 5ms to
3450 * give the HW RF Kill time to activate... */
3451 if (palive->is_valid == UCODE_VALID_OK)
3452 queue_delayed_work(priv->workqueue, pwork,
3453 msecs_to_jiffies(5));
3454 else
3455 IWL_WARNING("uCode did not respond OK.\n");
3456}
3457
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003458static void iwl4965_rx_reply_add_sta(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003459 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003460{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003461 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07003462
3463 IWL_DEBUG_RX("Received REPLY_ADD_STA: 0x%02X\n", pkt->u.status);
3464 return;
3465}
3466
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003467static void iwl4965_rx_reply_error(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003468 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003469{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003470 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07003471
3472 IWL_ERROR("Error Reply type 0x%08X cmd %s (0x%02X) "
3473 "seq 0x%04X ser 0x%08X\n",
3474 le32_to_cpu(pkt->u.err_resp.error_type),
3475 get_cmd_string(pkt->u.err_resp.cmd_id),
3476 pkt->u.err_resp.cmd_id,
3477 le16_to_cpu(pkt->u.err_resp.bad_cmd_seq_num),
3478 le32_to_cpu(pkt->u.err_resp.error_info));
3479}
3480
3481#define TX_STATUS_ENTRY(x) case TX_STATUS_FAIL_ ## x: return #x
3482
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003483static void iwl4965_rx_csa(struct iwl_priv *priv, struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003484{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003485 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
3486 struct iwl4965_rxon_cmd *rxon = (void *)&priv->active_rxon;
3487 struct iwl4965_csa_notification *csa = &(pkt->u.csa_notif);
Zhu Yib481de92007-09-25 17:54:57 -07003488 IWL_DEBUG_11H("CSA notif: channel %d, status %d\n",
3489 le16_to_cpu(csa->channel), le32_to_cpu(csa->status));
3490 rxon->channel = csa->channel;
3491 priv->staging_rxon.channel = csa->channel;
3492}
3493
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003494static void iwl4965_rx_spectrum_measure_notif(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003495 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003496{
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08003497#ifdef CONFIG_IWL4965_SPECTRUM_MEASUREMENT
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003498 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
3499 struct iwl4965_spectrum_notification *report = &(pkt->u.spectrum_notif);
Zhu Yib481de92007-09-25 17:54:57 -07003500
3501 if (!report->state) {
3502 IWL_DEBUG(IWL_DL_11H | IWL_DL_INFO,
3503 "Spectrum Measure Notification: Start\n");
3504 return;
3505 }
3506
3507 memcpy(&priv->measure_report, report, sizeof(*report));
3508 priv->measurement_status |= MEASUREMENT_READY;
3509#endif
3510}
3511
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003512static void iwl4965_rx_pm_sleep_notif(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003513 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003514{
Tomas Winkler0a6857e2008-03-12 16:58:49 -07003515#ifdef CONFIG_IWLWIFI_DEBUG
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003516 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
3517 struct iwl4965_sleep_notification *sleep = &(pkt->u.sleep_notif);
Zhu Yib481de92007-09-25 17:54:57 -07003518 IWL_DEBUG_RX("sleep mode: %d, src: %d\n",
3519 sleep->pm_sleep_mode, sleep->pm_wakeup_src);
3520#endif
3521}
3522
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003523static void iwl4965_rx_pm_debug_statistics_notif(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003524 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003525{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003526 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07003527 IWL_DEBUG_RADIO("Dumping %d bytes of unhandled "
3528 "notification for %s:\n",
3529 le32_to_cpu(pkt->len), get_cmd_string(pkt->hdr.cmd));
Tomas Winkler0a6857e2008-03-12 16:58:49 -07003530 iwl_print_hex_dump(IWL_DL_RADIO, pkt->u.raw, le32_to_cpu(pkt->len));
Zhu Yib481de92007-09-25 17:54:57 -07003531}
3532
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003533static void iwl4965_bg_beacon_update(struct work_struct *work)
Zhu Yib481de92007-09-25 17:54:57 -07003534{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003535 struct iwl_priv *priv =
3536 container_of(work, struct iwl_priv, beacon_update);
Zhu Yib481de92007-09-25 17:54:57 -07003537 struct sk_buff *beacon;
3538
3539 /* Pull updated AP beacon from mac80211. will fail if not in AP mode */
Johannes Berg32bfd352007-12-19 01:31:26 +01003540 beacon = ieee80211_beacon_get(priv->hw, priv->vif, NULL);
Zhu Yib481de92007-09-25 17:54:57 -07003541
3542 if (!beacon) {
3543 IWL_ERROR("update beacon failed\n");
3544 return;
3545 }
3546
3547 mutex_lock(&priv->mutex);
3548 /* new beacon skb is allocated every time; dispose previous.*/
3549 if (priv->ibss_beacon)
3550 dev_kfree_skb(priv->ibss_beacon);
3551
3552 priv->ibss_beacon = beacon;
3553 mutex_unlock(&priv->mutex);
3554
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003555 iwl4965_send_beacon_cmd(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003556}
3557
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003558static void iwl4965_rx_beacon_notif(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003559 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003560{
Tomas Winkler0a6857e2008-03-12 16:58:49 -07003561#ifdef CONFIG_IWLWIFI_DEBUG
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003562 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
3563 struct iwl4965_beacon_notif *beacon = &(pkt->u.beacon_status);
3564 u8 rate = iwl4965_hw_get_rate(beacon->beacon_notify_hdr.rate_n_flags);
Zhu Yib481de92007-09-25 17:54:57 -07003565
3566 IWL_DEBUG_RX("beacon status %x retries %d iss %d "
3567 "tsf %d %d rate %d\n",
3568 le32_to_cpu(beacon->beacon_notify_hdr.status) & TX_STATUS_MSK,
3569 beacon->beacon_notify_hdr.failure_frame,
3570 le32_to_cpu(beacon->ibss_mgr_status),
3571 le32_to_cpu(beacon->high_tsf),
3572 le32_to_cpu(beacon->low_tsf), rate);
3573#endif
3574
3575 if ((priv->iw_mode == IEEE80211_IF_TYPE_AP) &&
3576 (!test_bit(STATUS_EXIT_PENDING, &priv->status)))
3577 queue_work(priv->workqueue, &priv->beacon_update);
3578}
3579
3580/* Service response to REPLY_SCAN_CMD (0x80) */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003581static void iwl4965_rx_reply_scan(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003582 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003583{
Tomas Winkler0a6857e2008-03-12 16:58:49 -07003584#ifdef CONFIG_IWLWIFI_DEBUG
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003585 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
3586 struct iwl4965_scanreq_notification *notif =
3587 (struct iwl4965_scanreq_notification *)pkt->u.raw;
Zhu Yib481de92007-09-25 17:54:57 -07003588
3589 IWL_DEBUG_RX("Scan request status = 0x%x\n", notif->status);
3590#endif
3591}
3592
3593/* Service SCAN_START_NOTIFICATION (0x82) */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003594static void iwl4965_rx_scan_start_notif(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003595 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003596{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003597 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
3598 struct iwl4965_scanstart_notification *notif =
3599 (struct iwl4965_scanstart_notification *)pkt->u.raw;
Zhu Yib481de92007-09-25 17:54:57 -07003600 priv->scan_start_tsf = le32_to_cpu(notif->tsf_low);
3601 IWL_DEBUG_SCAN("Scan start: "
3602 "%d [802.11%s] "
3603 "(TSF: 0x%08X:%08X) - %d (beacon timer %u)\n",
3604 notif->channel,
3605 notif->band ? "bg" : "a",
3606 notif->tsf_high,
3607 notif->tsf_low, notif->status, notif->beacon_timer);
3608}
3609
3610/* Service SCAN_RESULTS_NOTIFICATION (0x83) */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003611static void iwl4965_rx_scan_results_notif(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003612 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003613{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003614 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
3615 struct iwl4965_scanresults_notification *notif =
3616 (struct iwl4965_scanresults_notification *)pkt->u.raw;
Zhu Yib481de92007-09-25 17:54:57 -07003617
3618 IWL_DEBUG_SCAN("Scan ch.res: "
3619 "%d [802.11%s] "
3620 "(TSF: 0x%08X:%08X) - %d "
3621 "elapsed=%lu usec (%dms since last)\n",
3622 notif->channel,
3623 notif->band ? "bg" : "a",
3624 le32_to_cpu(notif->tsf_high),
3625 le32_to_cpu(notif->tsf_low),
3626 le32_to_cpu(notif->statistics[0]),
3627 le32_to_cpu(notif->tsf_low) - priv->scan_start_tsf,
3628 jiffies_to_msecs(elapsed_jiffies
3629 (priv->last_scan_jiffies, jiffies)));
3630
3631 priv->last_scan_jiffies = jiffies;
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08003632 priv->next_scan_jiffies = 0;
Zhu Yib481de92007-09-25 17:54:57 -07003633}
3634
3635/* Service SCAN_COMPLETE_NOTIFICATION (0x84) */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003636static void iwl4965_rx_scan_complete_notif(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003637 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003638{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003639 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
3640 struct iwl4965_scancomplete_notification *scan_notif = (void *)pkt->u.raw;
Zhu Yib481de92007-09-25 17:54:57 -07003641
3642 IWL_DEBUG_SCAN("Scan complete: %d channels (TSF 0x%08X:%08X) - %d\n",
3643 scan_notif->scanned_channels,
3644 scan_notif->tsf_low,
3645 scan_notif->tsf_high, scan_notif->status);
3646
3647 /* The HW is no longer scanning */
3648 clear_bit(STATUS_SCAN_HW, &priv->status);
3649
3650 /* The scan completion notification came in, so kill that timer... */
3651 cancel_delayed_work(&priv->scan_check);
3652
3653 IWL_DEBUG_INFO("Scan pass on %sGHz took %dms\n",
3654 (priv->scan_bands == 2) ? "2.4" : "5.2",
3655 jiffies_to_msecs(elapsed_jiffies
3656 (priv->scan_pass_start, jiffies)));
3657
3658 /* Remove this scanned band from the list
3659 * of pending bands to scan */
3660 priv->scan_bands--;
3661
3662 /* If a request to abort was given, or the scan did not succeed
3663 * then we reset the scan state machine and terminate,
3664 * re-queuing another scan if one has been requested */
3665 if (test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
3666 IWL_DEBUG_INFO("Aborted scan completed.\n");
3667 clear_bit(STATUS_SCAN_ABORTING, &priv->status);
3668 } else {
3669 /* If there are more bands on this scan pass reschedule */
3670 if (priv->scan_bands > 0)
3671 goto reschedule;
3672 }
3673
3674 priv->last_scan_jiffies = jiffies;
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08003675 priv->next_scan_jiffies = 0;
Zhu Yib481de92007-09-25 17:54:57 -07003676 IWL_DEBUG_INFO("Setting scan to off\n");
3677
3678 clear_bit(STATUS_SCANNING, &priv->status);
3679
3680 IWL_DEBUG_INFO("Scan took %dms\n",
3681 jiffies_to_msecs(elapsed_jiffies(priv->scan_start, jiffies)));
3682
3683 queue_work(priv->workqueue, &priv->scan_completed);
3684
3685 return;
3686
3687reschedule:
3688 priv->scan_pass_start = jiffies;
3689 queue_work(priv->workqueue, &priv->request_scan);
3690}
3691
3692/* Handle notification from uCode that card's power state is changing
3693 * due to software, hardware, or critical temperature RFKILL */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003694static void iwl4965_rx_card_state_notif(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003695 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003696{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003697 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07003698 u32 flags = le32_to_cpu(pkt->u.card_state_notif.flags);
3699 unsigned long status = priv->status;
3700
3701 IWL_DEBUG_RF_KILL("Card state received: HW:%s SW:%s\n",
3702 (flags & HW_CARD_DISABLED) ? "Kill" : "On",
3703 (flags & SW_CARD_DISABLED) ? "Kill" : "On");
3704
3705 if (flags & (SW_CARD_DISABLED | HW_CARD_DISABLED |
3706 RF_CARD_DISABLED)) {
3707
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003708 iwl4965_write32(priv, CSR_UCODE_DRV_GP1_SET,
Zhu Yib481de92007-09-25 17:54:57 -07003709 CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
3710
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003711 if (!iwl4965_grab_nic_access(priv)) {
3712 iwl4965_write_direct32(
Zhu Yib481de92007-09-25 17:54:57 -07003713 priv, HBUS_TARG_MBX_C,
3714 HBUS_TARG_MBX_C_REG_BIT_CMD_BLOCKED);
3715
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003716 iwl4965_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003717 }
3718
3719 if (!(flags & RXON_CARD_DISABLED)) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003720 iwl4965_write32(priv, CSR_UCODE_DRV_GP1_CLR,
Zhu Yib481de92007-09-25 17:54:57 -07003721 CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003722 if (!iwl4965_grab_nic_access(priv)) {
3723 iwl4965_write_direct32(
Zhu Yib481de92007-09-25 17:54:57 -07003724 priv, HBUS_TARG_MBX_C,
3725 HBUS_TARG_MBX_C_REG_BIT_CMD_BLOCKED);
3726
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003727 iwl4965_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003728 }
3729 }
3730
3731 if (flags & RF_CARD_DISABLED) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003732 iwl4965_write32(priv, CSR_UCODE_DRV_GP1_SET,
Zhu Yib481de92007-09-25 17:54:57 -07003733 CSR_UCODE_DRV_GP1_REG_BIT_CT_KILL_EXIT);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003734 iwl4965_read32(priv, CSR_UCODE_DRV_GP1);
3735 if (!iwl4965_grab_nic_access(priv))
3736 iwl4965_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003737 }
3738 }
3739
3740 if (flags & HW_CARD_DISABLED)
3741 set_bit(STATUS_RF_KILL_HW, &priv->status);
3742 else
3743 clear_bit(STATUS_RF_KILL_HW, &priv->status);
3744
3745
3746 if (flags & SW_CARD_DISABLED)
3747 set_bit(STATUS_RF_KILL_SW, &priv->status);
3748 else
3749 clear_bit(STATUS_RF_KILL_SW, &priv->status);
3750
3751 if (!(flags & RXON_CARD_DISABLED))
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003752 iwl4965_scan_cancel(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003753
3754 if ((test_bit(STATUS_RF_KILL_HW, &status) !=
3755 test_bit(STATUS_RF_KILL_HW, &priv->status)) ||
3756 (test_bit(STATUS_RF_KILL_SW, &status) !=
3757 test_bit(STATUS_RF_KILL_SW, &priv->status)))
3758 queue_work(priv->workqueue, &priv->rf_kill);
3759 else
3760 wake_up_interruptible(&priv->wait_command_queue);
3761}
3762
3763/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003764 * iwl4965_setup_rx_handlers - Initialize Rx handler callbacks
Zhu Yib481de92007-09-25 17:54:57 -07003765 *
3766 * Setup the RX handlers for each of the reply types sent from the uCode
3767 * to the host.
3768 *
3769 * This function chains into the hardware specific files for them to setup
3770 * any hardware specific handlers as well.
3771 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003772static void iwl4965_setup_rx_handlers(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003773{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003774 priv->rx_handlers[REPLY_ALIVE] = iwl4965_rx_reply_alive;
3775 priv->rx_handlers[REPLY_ADD_STA] = iwl4965_rx_reply_add_sta;
3776 priv->rx_handlers[REPLY_ERROR] = iwl4965_rx_reply_error;
3777 priv->rx_handlers[CHANNEL_SWITCH_NOTIFICATION] = iwl4965_rx_csa;
Zhu Yib481de92007-09-25 17:54:57 -07003778 priv->rx_handlers[SPECTRUM_MEASURE_NOTIFICATION] =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003779 iwl4965_rx_spectrum_measure_notif;
3780 priv->rx_handlers[PM_SLEEP_NOTIFICATION] = iwl4965_rx_pm_sleep_notif;
Zhu Yib481de92007-09-25 17:54:57 -07003781 priv->rx_handlers[PM_DEBUG_STATISTIC_NOTIFIC] =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003782 iwl4965_rx_pm_debug_statistics_notif;
3783 priv->rx_handlers[BEACON_NOTIFICATION] = iwl4965_rx_beacon_notif;
Zhu Yib481de92007-09-25 17:54:57 -07003784
Ben Cahill9fbab512007-11-29 11:09:47 +08003785 /*
3786 * The same handler is used for both the REPLY to a discrete
3787 * statistics request from the host as well as for the periodic
3788 * statistics notifications (after received beacons) from the uCode.
Zhu Yib481de92007-09-25 17:54:57 -07003789 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003790 priv->rx_handlers[REPLY_STATISTICS_CMD] = iwl4965_hw_rx_statistics;
3791 priv->rx_handlers[STATISTICS_NOTIFICATION] = iwl4965_hw_rx_statistics;
Zhu Yib481de92007-09-25 17:54:57 -07003792
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003793 priv->rx_handlers[REPLY_SCAN_CMD] = iwl4965_rx_reply_scan;
3794 priv->rx_handlers[SCAN_START_NOTIFICATION] = iwl4965_rx_scan_start_notif;
Zhu Yib481de92007-09-25 17:54:57 -07003795 priv->rx_handlers[SCAN_RESULTS_NOTIFICATION] =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003796 iwl4965_rx_scan_results_notif;
Zhu Yib481de92007-09-25 17:54:57 -07003797 priv->rx_handlers[SCAN_COMPLETE_NOTIFICATION] =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003798 iwl4965_rx_scan_complete_notif;
3799 priv->rx_handlers[CARD_STATE_NOTIFICATION] = iwl4965_rx_card_state_notif;
3800 priv->rx_handlers[REPLY_TX] = iwl4965_rx_reply_tx;
Zhu Yib481de92007-09-25 17:54:57 -07003801
Ben Cahill9fbab512007-11-29 11:09:47 +08003802 /* Set up hardware specific Rx handlers */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003803 iwl4965_hw_rx_handler_setup(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003804}
3805
3806/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003807 * iwl4965_tx_cmd_complete - Pull unused buffers off the queue and reclaim them
Zhu Yib481de92007-09-25 17:54:57 -07003808 * @rxb: Rx buffer to reclaim
3809 *
3810 * If an Rx buffer has an async callback associated with it the callback
3811 * will be executed. The attached skb (if present) will only be freed
3812 * if the callback returns 1
3813 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003814static void iwl4965_tx_cmd_complete(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003815 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003816{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003817 struct iwl4965_rx_packet *pkt = (struct iwl4965_rx_packet *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07003818 u16 sequence = le16_to_cpu(pkt->hdr.sequence);
3819 int txq_id = SEQ_TO_QUEUE(sequence);
3820 int index = SEQ_TO_INDEX(sequence);
3821 int huge = sequence & SEQ_HUGE_FRAME;
3822 int cmd_index;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003823 struct iwl4965_cmd *cmd;
Zhu Yib481de92007-09-25 17:54:57 -07003824
3825 /* If a Tx command is being handled and it isn't in the actual
3826 * command queue then there a command routing bug has been introduced
3827 * in the queue management code. */
3828 if (txq_id != IWL_CMD_QUEUE_NUM)
3829 IWL_ERROR("Error wrong command queue %d command id 0x%X\n",
3830 txq_id, pkt->hdr.cmd);
3831 BUG_ON(txq_id != IWL_CMD_QUEUE_NUM);
3832
3833 cmd_index = get_cmd_index(&priv->txq[IWL_CMD_QUEUE_NUM].q, index, huge);
3834 cmd = &priv->txq[IWL_CMD_QUEUE_NUM].cmd[cmd_index];
3835
3836 /* Input error checking is done when commands are added to queue. */
3837 if (cmd->meta.flags & CMD_WANT_SKB) {
3838 cmd->meta.source->u.skb = rxb->skb;
3839 rxb->skb = NULL;
3840 } else if (cmd->meta.u.callback &&
3841 !cmd->meta.u.callback(priv, cmd, rxb->skb))
3842 rxb->skb = NULL;
3843
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003844 iwl4965_tx_queue_reclaim(priv, txq_id, index);
Zhu Yib481de92007-09-25 17:54:57 -07003845
3846 if (!(cmd->meta.flags & CMD_ASYNC)) {
3847 clear_bit(STATUS_HCMD_ACTIVE, &priv->status);
3848 wake_up_interruptible(&priv->wait_command_queue);
3849 }
3850}
3851
3852/************************** RX-FUNCTIONS ****************************/
3853/*
3854 * Rx theory of operation
3855 *
Ben Cahill9fbab512007-11-29 11:09:47 +08003856 * Driver allocates a circular buffer of Receive Buffer Descriptors (RBDs),
3857 * each of which point to Receive Buffers to be filled by 4965. These get
3858 * used not only for Rx frames, but for any command response or notification
3859 * from the 4965. The driver and 4965 manage the Rx buffers by means
3860 * of indexes into the circular buffer.
Zhu Yib481de92007-09-25 17:54:57 -07003861 *
3862 * Rx Queue Indexes
3863 * The host/firmware share two index registers for managing the Rx buffers.
3864 *
3865 * The READ index maps to the first position that the firmware may be writing
3866 * to -- the driver can read up to (but not including) this position and get
3867 * good data.
3868 * The READ index is managed by the firmware once the card is enabled.
3869 *
3870 * The WRITE index maps to the last position the driver has read from -- the
3871 * position preceding WRITE is the last slot the firmware can place a packet.
3872 *
3873 * The queue is empty (no good data) if WRITE = READ - 1, and is full if
3874 * WRITE = READ.
3875 *
Ben Cahill9fbab512007-11-29 11:09:47 +08003876 * During initialization, the host sets up the READ queue position to the first
Zhu Yib481de92007-09-25 17:54:57 -07003877 * INDEX position, and WRITE to the last (READ - 1 wrapped)
3878 *
Ben Cahill9fbab512007-11-29 11:09:47 +08003879 * When the firmware places a packet in a buffer, it will advance the READ index
Zhu Yib481de92007-09-25 17:54:57 -07003880 * and fire the RX interrupt. The driver can then query the READ index and
3881 * process as many packets as possible, moving the WRITE index forward as it
3882 * resets the Rx queue buffers with new memory.
3883 *
3884 * The management in the driver is as follows:
3885 * + A list of pre-allocated SKBs is stored in iwl->rxq->rx_free. When
3886 * iwl->rxq->free_count drops to or below RX_LOW_WATERMARK, work is scheduled
Ian Schram01ebd062007-10-25 17:15:22 +08003887 * to replenish the iwl->rxq->rx_free.
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003888 * + In iwl4965_rx_replenish (scheduled) if 'processed' != 'read' then the
Zhu Yib481de92007-09-25 17:54:57 -07003889 * iwl->rxq is replenished and the READ INDEX is updated (updating the
3890 * 'processed' and 'read' driver indexes as well)
3891 * + A received packet is processed and handed to the kernel network stack,
3892 * detached from the iwl->rxq. The driver 'processed' index is updated.
3893 * + The Host/Firmware iwl->rxq is replenished at tasklet time from the rx_free
3894 * list. If there are no allocated buffers in iwl->rxq->rx_free, the READ
3895 * INDEX is not incremented and iwl->status(RX_STALLED) is set. If there
3896 * were enough free buffers and RX_STALLED is set it is cleared.
3897 *
3898 *
3899 * Driver sequence:
3900 *
Ben Cahill9fbab512007-11-29 11:09:47 +08003901 * iwl4965_rx_queue_alloc() Allocates rx_free
3902 * iwl4965_rx_replenish() Replenishes rx_free list from rx_used, and calls
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003903 * iwl4965_rx_queue_restock
Ben Cahill9fbab512007-11-29 11:09:47 +08003904 * iwl4965_rx_queue_restock() Moves available buffers from rx_free into Rx
Zhu Yib481de92007-09-25 17:54:57 -07003905 * queue, updates firmware pointers, and updates
3906 * the WRITE index. If insufficient rx_free buffers
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003907 * are available, schedules iwl4965_rx_replenish
Zhu Yib481de92007-09-25 17:54:57 -07003908 *
3909 * -- enable interrupts --
Ben Cahill9fbab512007-11-29 11:09:47 +08003910 * ISR - iwl4965_rx() Detach iwl4965_rx_mem_buffers from pool up to the
Zhu Yib481de92007-09-25 17:54:57 -07003911 * READ INDEX, detaching the SKB from the pool.
3912 * Moves the packet buffer from queue to rx_used.
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003913 * Calls iwl4965_rx_queue_restock to refill any empty
Zhu Yib481de92007-09-25 17:54:57 -07003914 * slots.
3915 * ...
3916 *
3917 */
3918
3919/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003920 * iwl4965_rx_queue_space - Return number of free slots available in queue.
Zhu Yib481de92007-09-25 17:54:57 -07003921 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003922static int iwl4965_rx_queue_space(const struct iwl4965_rx_queue *q)
Zhu Yib481de92007-09-25 17:54:57 -07003923{
3924 int s = q->read - q->write;
3925 if (s <= 0)
3926 s += RX_QUEUE_SIZE;
3927 /* keep some buffer to not confuse full and empty queue */
3928 s -= 2;
3929 if (s < 0)
3930 s = 0;
3931 return s;
3932}
3933
3934/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003935 * iwl4965_rx_queue_update_write_ptr - Update the write pointer for the RX queue
Zhu Yib481de92007-09-25 17:54:57 -07003936 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003937int iwl4965_rx_queue_update_write_ptr(struct iwl_priv *priv, struct iwl4965_rx_queue *q)
Zhu Yib481de92007-09-25 17:54:57 -07003938{
3939 u32 reg = 0;
3940 int rc = 0;
3941 unsigned long flags;
3942
3943 spin_lock_irqsave(&q->lock, flags);
3944
3945 if (q->need_update == 0)
3946 goto exit_unlock;
3947
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003948 /* If power-saving is in use, make sure device is awake */
Zhu Yib481de92007-09-25 17:54:57 -07003949 if (test_bit(STATUS_POWER_PMI, &priv->status)) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003950 reg = iwl4965_read32(priv, CSR_UCODE_DRV_GP1);
Zhu Yib481de92007-09-25 17:54:57 -07003951
3952 if (reg & CSR_UCODE_DRV_GP1_BIT_MAC_SLEEP) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003953 iwl4965_set_bit(priv, CSR_GP_CNTRL,
Zhu Yib481de92007-09-25 17:54:57 -07003954 CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
3955 goto exit_unlock;
3956 }
3957
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003958 rc = iwl4965_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003959 if (rc)
3960 goto exit_unlock;
3961
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003962 /* Device expects a multiple of 8 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003963 iwl4965_write_direct32(priv, FH_RSCSR_CHNL0_WPTR,
Zhu Yib481de92007-09-25 17:54:57 -07003964 q->write & ~0x7);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003965 iwl4965_release_nic_access(priv);
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003966
3967 /* Else device is assumed to be awake */
Zhu Yib481de92007-09-25 17:54:57 -07003968 } else
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003969 /* Device expects a multiple of 8 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003970 iwl4965_write32(priv, FH_RSCSR_CHNL0_WPTR, q->write & ~0x7);
Zhu Yib481de92007-09-25 17:54:57 -07003971
3972
3973 q->need_update = 0;
3974
3975 exit_unlock:
3976 spin_unlock_irqrestore(&q->lock, flags);
3977 return rc;
3978}
3979
3980/**
Ben Cahill9fbab512007-11-29 11:09:47 +08003981 * iwl4965_dma_addr2rbd_ptr - convert a DMA address to a uCode read buffer ptr
Zhu Yib481de92007-09-25 17:54:57 -07003982 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003983static inline __le32 iwl4965_dma_addr2rbd_ptr(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07003984 dma_addr_t dma_addr)
3985{
3986 return cpu_to_le32((u32)(dma_addr >> 8));
3987}
3988
3989
3990/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003991 * iwl4965_rx_queue_restock - refill RX queue from pre-allocated pool
Zhu Yib481de92007-09-25 17:54:57 -07003992 *
Ben Cahill9fbab512007-11-29 11:09:47 +08003993 * If there are slots in the RX queue that need to be restocked,
Zhu Yib481de92007-09-25 17:54:57 -07003994 * and we have free pre-allocated buffers, fill the ranks as much
Ben Cahill9fbab512007-11-29 11:09:47 +08003995 * as we can, pulling from rx_free.
Zhu Yib481de92007-09-25 17:54:57 -07003996 *
3997 * This moves the 'write' index forward to catch up with 'processed', and
3998 * also updates the memory address in the firmware to reference the new
3999 * target buffer.
4000 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004001static int iwl4965_rx_queue_restock(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004002{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004003 struct iwl4965_rx_queue *rxq = &priv->rxq;
Zhu Yib481de92007-09-25 17:54:57 -07004004 struct list_head *element;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004005 struct iwl4965_rx_mem_buffer *rxb;
Zhu Yib481de92007-09-25 17:54:57 -07004006 unsigned long flags;
4007 int write, rc;
4008
4009 spin_lock_irqsave(&rxq->lock, flags);
4010 write = rxq->write & ~0x7;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004011 while ((iwl4965_rx_queue_space(rxq) > 0) && (rxq->free_count)) {
Cahill, Ben M6440adb2007-11-29 11:09:55 +08004012 /* Get next free Rx buffer, remove from free list */
Zhu Yib481de92007-09-25 17:54:57 -07004013 element = rxq->rx_free.next;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004014 rxb = list_entry(element, struct iwl4965_rx_mem_buffer, list);
Zhu Yib481de92007-09-25 17:54:57 -07004015 list_del(element);
Cahill, Ben M6440adb2007-11-29 11:09:55 +08004016
4017 /* Point to Rx buffer via next RBD in circular buffer */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004018 rxq->bd[rxq->write] = iwl4965_dma_addr2rbd_ptr(priv, rxb->dma_addr);
Zhu Yib481de92007-09-25 17:54:57 -07004019 rxq->queue[rxq->write] = rxb;
4020 rxq->write = (rxq->write + 1) & RX_QUEUE_MASK;
4021 rxq->free_count--;
4022 }
4023 spin_unlock_irqrestore(&rxq->lock, flags);
4024 /* If the pre-allocated buffer pool is dropping low, schedule to
4025 * refill it */
4026 if (rxq->free_count <= RX_LOW_WATERMARK)
4027 queue_work(priv->workqueue, &priv->rx_replenish);
4028
4029
Cahill, Ben M6440adb2007-11-29 11:09:55 +08004030 /* If we've added more space for the firmware to place data, tell it.
4031 * Increment device's write pointer in multiples of 8. */
Zhu Yib481de92007-09-25 17:54:57 -07004032 if ((write != (rxq->write & ~0x7))
4033 || (abs(rxq->write - rxq->read) > 7)) {
4034 spin_lock_irqsave(&rxq->lock, flags);
4035 rxq->need_update = 1;
4036 spin_unlock_irqrestore(&rxq->lock, flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004037 rc = iwl4965_rx_queue_update_write_ptr(priv, rxq);
Zhu Yib481de92007-09-25 17:54:57 -07004038 if (rc)
4039 return rc;
4040 }
4041
4042 return 0;
4043}
4044
4045/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004046 * iwl4965_rx_replenish - Move all used packet from rx_used to rx_free
Zhu Yib481de92007-09-25 17:54:57 -07004047 *
4048 * When moving to rx_free an SKB is allocated for the slot.
4049 *
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004050 * Also restock the Rx queue via iwl4965_rx_queue_restock.
Ian Schram01ebd062007-10-25 17:15:22 +08004051 * This is called as a scheduled work item (except for during initialization)
Zhu Yib481de92007-09-25 17:54:57 -07004052 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004053static void iwl4965_rx_allocate(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004054{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004055 struct iwl4965_rx_queue *rxq = &priv->rxq;
Zhu Yib481de92007-09-25 17:54:57 -07004056 struct list_head *element;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004057 struct iwl4965_rx_mem_buffer *rxb;
Zhu Yib481de92007-09-25 17:54:57 -07004058 unsigned long flags;
4059 spin_lock_irqsave(&rxq->lock, flags);
4060 while (!list_empty(&rxq->rx_used)) {
4061 element = rxq->rx_used.next;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004062 rxb = list_entry(element, struct iwl4965_rx_mem_buffer, list);
Cahill, Ben M6440adb2007-11-29 11:09:55 +08004063
4064 /* Alloc a new receive buffer */
Zhu Yib481de92007-09-25 17:54:57 -07004065 rxb->skb =
Ron Rindjunsky9ee1ba42007-11-26 16:14:42 +02004066 alloc_skb(priv->hw_setting.rx_buf_size,
4067 __GFP_NOWARN | GFP_ATOMIC);
Zhu Yib481de92007-09-25 17:54:57 -07004068 if (!rxb->skb) {
4069 if (net_ratelimit())
4070 printk(KERN_CRIT DRV_NAME
4071 ": Can not allocate SKB buffers\n");
4072 /* We don't reschedule replenish work here -- we will
4073 * call the restock method and if it still needs
4074 * more buffers it will schedule replenish */
4075 break;
4076 }
4077 priv->alloc_rxb_skb++;
4078 list_del(element);
Cahill, Ben M6440adb2007-11-29 11:09:55 +08004079
4080 /* Get physical address of RB/SKB */
Zhu Yib481de92007-09-25 17:54:57 -07004081 rxb->dma_addr =
4082 pci_map_single(priv->pci_dev, rxb->skb->data,
Ron Rindjunsky9ee1ba42007-11-26 16:14:42 +02004083 priv->hw_setting.rx_buf_size, PCI_DMA_FROMDEVICE);
Zhu Yib481de92007-09-25 17:54:57 -07004084 list_add_tail(&rxb->list, &rxq->rx_free);
4085 rxq->free_count++;
4086 }
4087 spin_unlock_irqrestore(&rxq->lock, flags);
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08004088}
4089
4090/*
4091 * this should be called while priv->lock is locked
4092*/
Tomas Winkler4fd1f842007-12-05 20:59:58 +02004093static void __iwl4965_rx_replenish(void *data)
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08004094{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004095 struct iwl_priv *priv = data;
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08004096
4097 iwl4965_rx_allocate(priv);
4098 iwl4965_rx_queue_restock(priv);
4099}
4100
4101
4102void iwl4965_rx_replenish(void *data)
4103{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004104 struct iwl_priv *priv = data;
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08004105 unsigned long flags;
4106
4107 iwl4965_rx_allocate(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004108
4109 spin_lock_irqsave(&priv->lock, flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004110 iwl4965_rx_queue_restock(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004111 spin_unlock_irqrestore(&priv->lock, flags);
4112}
4113
4114/* Assumes that the skb field of the buffers in 'pool' is kept accurate.
Ben Cahill9fbab512007-11-29 11:09:47 +08004115 * If an SKB has been detached, the POOL needs to have its SKB set to NULL
Zhu Yib481de92007-09-25 17:54:57 -07004116 * This free routine walks the list of POOL entries and if SKB is set to
4117 * non NULL it is unmapped and freed
4118 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004119static void iwl4965_rx_queue_free(struct iwl_priv *priv, struct iwl4965_rx_queue *rxq)
Zhu Yib481de92007-09-25 17:54:57 -07004120{
4121 int i;
4122 for (i = 0; i < RX_QUEUE_SIZE + RX_FREE_BUFFERS; i++) {
4123 if (rxq->pool[i].skb != NULL) {
4124 pci_unmap_single(priv->pci_dev,
4125 rxq->pool[i].dma_addr,
Ron Rindjunsky9ee1ba42007-11-26 16:14:42 +02004126 priv->hw_setting.rx_buf_size,
4127 PCI_DMA_FROMDEVICE);
Zhu Yib481de92007-09-25 17:54:57 -07004128 dev_kfree_skb(rxq->pool[i].skb);
4129 }
4130 }
4131
4132 pci_free_consistent(priv->pci_dev, 4 * RX_QUEUE_SIZE, rxq->bd,
4133 rxq->dma_addr);
4134 rxq->bd = NULL;
4135}
4136
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004137int iwl4965_rx_queue_alloc(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 pci_dev *dev = priv->pci_dev;
4141 int i;
4142
4143 spin_lock_init(&rxq->lock);
4144 INIT_LIST_HEAD(&rxq->rx_free);
4145 INIT_LIST_HEAD(&rxq->rx_used);
Cahill, Ben M6440adb2007-11-29 11:09:55 +08004146
4147 /* Alloc the circular buffer of Read Buffer Descriptors (RBDs) */
Zhu Yib481de92007-09-25 17:54:57 -07004148 rxq->bd = pci_alloc_consistent(dev, 4 * RX_QUEUE_SIZE, &rxq->dma_addr);
4149 if (!rxq->bd)
4150 return -ENOMEM;
Cahill, Ben M6440adb2007-11-29 11:09:55 +08004151
Zhu Yib481de92007-09-25 17:54:57 -07004152 /* Fill the rx_used queue with _all_ of the Rx buffers */
4153 for (i = 0; i < RX_FREE_BUFFERS + RX_QUEUE_SIZE; i++)
4154 list_add_tail(&rxq->pool[i].list, &rxq->rx_used);
Cahill, Ben M6440adb2007-11-29 11:09:55 +08004155
Zhu Yib481de92007-09-25 17:54:57 -07004156 /* Set us so that we have processed and used all buffers, but have
4157 * not restocked the Rx queue with fresh buffers */
4158 rxq->read = rxq->write = 0;
4159 rxq->free_count = 0;
4160 rxq->need_update = 0;
4161 return 0;
4162}
4163
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004164void iwl4965_rx_queue_reset(struct iwl_priv *priv, struct iwl4965_rx_queue *rxq)
Zhu Yib481de92007-09-25 17:54:57 -07004165{
4166 unsigned long flags;
4167 int i;
4168 spin_lock_irqsave(&rxq->lock, flags);
4169 INIT_LIST_HEAD(&rxq->rx_free);
4170 INIT_LIST_HEAD(&rxq->rx_used);
4171 /* Fill the rx_used queue with _all_ of the Rx buffers */
4172 for (i = 0; i < RX_FREE_BUFFERS + RX_QUEUE_SIZE; i++) {
4173 /* In the reset function, these buffers may have been allocated
4174 * to an SKB, so we need to unmap and free potential storage */
4175 if (rxq->pool[i].skb != NULL) {
4176 pci_unmap_single(priv->pci_dev,
4177 rxq->pool[i].dma_addr,
Ron Rindjunsky9ee1ba42007-11-26 16:14:42 +02004178 priv->hw_setting.rx_buf_size,
4179 PCI_DMA_FROMDEVICE);
Zhu Yib481de92007-09-25 17:54:57 -07004180 priv->alloc_rxb_skb--;
4181 dev_kfree_skb(rxq->pool[i].skb);
4182 rxq->pool[i].skb = NULL;
4183 }
4184 list_add_tail(&rxq->pool[i].list, &rxq->rx_used);
4185 }
4186
4187 /* Set us so that we have processed and used all buffers, but have
4188 * not restocked the Rx queue with fresh buffers */
4189 rxq->read = rxq->write = 0;
4190 rxq->free_count = 0;
4191 spin_unlock_irqrestore(&rxq->lock, flags);
4192}
4193
4194/* Convert linear signal-to-noise ratio into dB */
4195static u8 ratio2dB[100] = {
4196/* 0 1 2 3 4 5 6 7 8 9 */
4197 0, 0, 6, 10, 12, 14, 16, 17, 18, 19, /* 00 - 09 */
4198 20, 21, 22, 22, 23, 23, 24, 25, 26, 26, /* 10 - 19 */
4199 26, 26, 26, 27, 27, 28, 28, 28, 29, 29, /* 20 - 29 */
4200 29, 30, 30, 30, 31, 31, 31, 31, 32, 32, /* 30 - 39 */
4201 32, 32, 32, 33, 33, 33, 33, 33, 34, 34, /* 40 - 49 */
4202 34, 34, 34, 34, 35, 35, 35, 35, 35, 35, /* 50 - 59 */
4203 36, 36, 36, 36, 36, 36, 36, 37, 37, 37, /* 60 - 69 */
4204 37, 37, 37, 37, 37, 38, 38, 38, 38, 38, /* 70 - 79 */
4205 38, 38, 38, 38, 38, 39, 39, 39, 39, 39, /* 80 - 89 */
4206 39, 39, 39, 39, 39, 40, 40, 40, 40, 40 /* 90 - 99 */
4207};
4208
4209/* Calculates a relative dB value from a ratio of linear
4210 * (i.e. not dB) signal levels.
4211 * Conversion assumes that levels are voltages (20*log), not powers (10*log). */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004212int iwl4965_calc_db_from_ratio(int sig_ratio)
Zhu Yib481de92007-09-25 17:54:57 -07004213{
Adrian Bunkc899a572007-10-14 19:51:15 +02004214 /* 1000:1 or higher just report as 60 dB */
4215 if (sig_ratio >= 1000)
Zhu Yib481de92007-09-25 17:54:57 -07004216 return 60;
4217
Adrian Bunkc899a572007-10-14 19:51:15 +02004218 /* 100:1 or higher, divide by 10 and use table,
Zhu Yib481de92007-09-25 17:54:57 -07004219 * add 20 dB to make up for divide by 10 */
Adrian Bunkc899a572007-10-14 19:51:15 +02004220 if (sig_ratio >= 100)
Zhu Yib481de92007-09-25 17:54:57 -07004221 return (20 + (int)ratio2dB[sig_ratio/10]);
4222
4223 /* We shouldn't see this */
4224 if (sig_ratio < 1)
4225 return 0;
4226
4227 /* Use table for ratios 1:1 - 99:1 */
4228 return (int)ratio2dB[sig_ratio];
4229}
4230
4231#define PERFECT_RSSI (-20) /* dBm */
4232#define WORST_RSSI (-95) /* dBm */
4233#define RSSI_RANGE (PERFECT_RSSI - WORST_RSSI)
4234
4235/* Calculate an indication of rx signal quality (a percentage, not dBm!).
4236 * See http://www.ces.clemson.edu/linux/signal_quality.shtml for info
4237 * about formulas used below. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004238int iwl4965_calc_sig_qual(int rssi_dbm, int noise_dbm)
Zhu Yib481de92007-09-25 17:54:57 -07004239{
4240 int sig_qual;
4241 int degradation = PERFECT_RSSI - rssi_dbm;
4242
4243 /* If we get a noise measurement, use signal-to-noise ratio (SNR)
4244 * as indicator; formula is (signal dbm - noise dbm).
4245 * SNR at or above 40 is a great signal (100%).
4246 * Below that, scale to fit SNR of 0 - 40 dB within 0 - 100% indicator.
4247 * Weakest usable signal is usually 10 - 15 dB SNR. */
4248 if (noise_dbm) {
4249 if (rssi_dbm - noise_dbm >= 40)
4250 return 100;
4251 else if (rssi_dbm < noise_dbm)
4252 return 0;
4253 sig_qual = ((rssi_dbm - noise_dbm) * 5) / 2;
4254
4255 /* Else use just the signal level.
4256 * This formula is a least squares fit of data points collected and
4257 * compared with a reference system that had a percentage (%) display
4258 * for signal quality. */
4259 } else
4260 sig_qual = (100 * (RSSI_RANGE * RSSI_RANGE) - degradation *
4261 (15 * RSSI_RANGE + 62 * degradation)) /
4262 (RSSI_RANGE * RSSI_RANGE);
4263
4264 if (sig_qual > 100)
4265 sig_qual = 100;
4266 else if (sig_qual < 1)
4267 sig_qual = 0;
4268
4269 return sig_qual;
4270}
4271
4272/**
Ben Cahill9fbab512007-11-29 11:09:47 +08004273 * iwl4965_rx_handle - Main entry function for receiving responses from uCode
Zhu Yib481de92007-09-25 17:54:57 -07004274 *
4275 * Uses the priv->rx_handlers callback function array to invoke
4276 * the appropriate handlers, including command responses,
4277 * frame-received notifications, and other notifications.
4278 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004279static void iwl4965_rx_handle(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004280{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004281 struct iwl4965_rx_mem_buffer *rxb;
4282 struct iwl4965_rx_packet *pkt;
4283 struct iwl4965_rx_queue *rxq = &priv->rxq;
Zhu Yib481de92007-09-25 17:54:57 -07004284 u32 r, i;
4285 int reclaim;
4286 unsigned long flags;
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08004287 u8 fill_rx = 0;
Mohamed Abbasd68ab682008-02-07 13:16:33 -08004288 u32 count = 8;
Zhu Yib481de92007-09-25 17:54:57 -07004289
Cahill, Ben M6440adb2007-11-29 11:09:55 +08004290 /* uCode's read index (stored in shared DRAM) indicates the last Rx
4291 * buffer that the driver may process (last buffer filled by ucode). */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004292 r = iwl4965_hw_get_rx_read(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004293 i = rxq->read;
4294
4295 /* Rx interrupt, but nothing sent from uCode */
4296 if (i == r)
4297 IWL_DEBUG(IWL_DL_RX | IWL_DL_ISR, "r = %d, i = %d\n", r, i);
4298
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08004299 if (iwl4965_rx_queue_space(rxq) > (RX_QUEUE_SIZE / 2))
4300 fill_rx = 1;
4301
Zhu Yib481de92007-09-25 17:54:57 -07004302 while (i != r) {
4303 rxb = rxq->queue[i];
4304
Ben Cahill9fbab512007-11-29 11:09:47 +08004305 /* If an RXB doesn't have a Rx queue slot associated with it,
Zhu Yib481de92007-09-25 17:54:57 -07004306 * then a bug has been introduced in the queue refilling
4307 * routines -- catch it here */
4308 BUG_ON(rxb == NULL);
4309
4310 rxq->queue[i] = NULL;
4311
4312 pci_dma_sync_single_for_cpu(priv->pci_dev, rxb->dma_addr,
Ron Rindjunsky9ee1ba42007-11-26 16:14:42 +02004313 priv->hw_setting.rx_buf_size,
Zhu Yib481de92007-09-25 17:54:57 -07004314 PCI_DMA_FROMDEVICE);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004315 pkt = (struct iwl4965_rx_packet *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07004316
4317 /* Reclaim a command buffer only if this packet is a response
4318 * to a (driver-originated) command.
4319 * If the packet (e.g. Rx frame) originated from uCode,
4320 * there is no command buffer to reclaim.
4321 * Ucode should set SEQ_RX_FRAME bit if ucode-originated,
4322 * but apparently a few don't get set; catch them here. */
4323 reclaim = !(pkt->hdr.sequence & SEQ_RX_FRAME) &&
4324 (pkt->hdr.cmd != REPLY_RX_PHY_CMD) &&
4325 (pkt->hdr.cmd != REPLY_4965_RX) &&
Zhu Yicfe01702007-09-27 11:27:31 +08004326 (pkt->hdr.cmd != REPLY_COMPRESSED_BA) &&
Zhu Yib481de92007-09-25 17:54:57 -07004327 (pkt->hdr.cmd != STATISTICS_NOTIFICATION) &&
4328 (pkt->hdr.cmd != REPLY_TX);
4329
4330 /* Based on type of command response or notification,
4331 * handle those that need handling via function in
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004332 * rx_handlers table. See iwl4965_setup_rx_handlers() */
Zhu Yib481de92007-09-25 17:54:57 -07004333 if (priv->rx_handlers[pkt->hdr.cmd]) {
4334 IWL_DEBUG(IWL_DL_HOST_COMMAND | IWL_DL_RX | IWL_DL_ISR,
4335 "r = %d, i = %d, %s, 0x%02x\n", r, i,
4336 get_cmd_string(pkt->hdr.cmd), pkt->hdr.cmd);
4337 priv->rx_handlers[pkt->hdr.cmd] (priv, rxb);
4338 } else {
4339 /* No handling needed */
4340 IWL_DEBUG(IWL_DL_HOST_COMMAND | IWL_DL_RX | IWL_DL_ISR,
4341 "r %d i %d No handler needed for %s, 0x%02x\n",
4342 r, i, get_cmd_string(pkt->hdr.cmd),
4343 pkt->hdr.cmd);
4344 }
4345
4346 if (reclaim) {
Ben Cahill9fbab512007-11-29 11:09:47 +08004347 /* Invoke any callbacks, transfer the skb to caller, and
4348 * fire off the (possibly) blocking iwl4965_send_cmd()
Zhu Yib481de92007-09-25 17:54:57 -07004349 * as we reclaim the driver command queue */
4350 if (rxb && rxb->skb)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004351 iwl4965_tx_cmd_complete(priv, rxb);
Zhu Yib481de92007-09-25 17:54:57 -07004352 else
4353 IWL_WARNING("Claim null rxb?\n");
4354 }
4355
4356 /* For now we just don't re-use anything. We can tweak this
4357 * later to try and re-use notification packets and SKBs that
4358 * fail to Rx correctly */
4359 if (rxb->skb != NULL) {
4360 priv->alloc_rxb_skb--;
4361 dev_kfree_skb_any(rxb->skb);
4362 rxb->skb = NULL;
4363 }
4364
4365 pci_unmap_single(priv->pci_dev, rxb->dma_addr,
Ron Rindjunsky9ee1ba42007-11-26 16:14:42 +02004366 priv->hw_setting.rx_buf_size,
4367 PCI_DMA_FROMDEVICE);
Zhu Yib481de92007-09-25 17:54:57 -07004368 spin_lock_irqsave(&rxq->lock, flags);
4369 list_add_tail(&rxb->list, &priv->rxq.rx_used);
4370 spin_unlock_irqrestore(&rxq->lock, flags);
4371 i = (i + 1) & RX_QUEUE_MASK;
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08004372 /* If there are a lot of unused frames,
4373 * restock the Rx queue so ucode wont assert. */
4374 if (fill_rx) {
4375 count++;
4376 if (count >= 8) {
4377 priv->rxq.read = i;
4378 __iwl4965_rx_replenish(priv);
4379 count = 0;
4380 }
4381 }
Zhu Yib481de92007-09-25 17:54:57 -07004382 }
4383
4384 /* Backtrack one entry */
4385 priv->rxq.read = i;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004386 iwl4965_rx_queue_restock(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004387}
4388
Cahill, Ben M6440adb2007-11-29 11:09:55 +08004389/**
4390 * iwl4965_tx_queue_update_write_ptr - Send new write index to hardware
4391 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004392static int iwl4965_tx_queue_update_write_ptr(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004393 struct iwl4965_tx_queue *txq)
Zhu Yib481de92007-09-25 17:54:57 -07004394{
4395 u32 reg = 0;
4396 int rc = 0;
4397 int txq_id = txq->q.id;
4398
4399 if (txq->need_update == 0)
4400 return rc;
4401
4402 /* if we're trying to save power */
4403 if (test_bit(STATUS_POWER_PMI, &priv->status)) {
4404 /* wake up nic if it's powered down ...
4405 * uCode will wake up, and interrupt us again, so next
4406 * time we'll skip this part. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004407 reg = iwl4965_read32(priv, CSR_UCODE_DRV_GP1);
Zhu Yib481de92007-09-25 17:54:57 -07004408
4409 if (reg & CSR_UCODE_DRV_GP1_BIT_MAC_SLEEP) {
4410 IWL_DEBUG_INFO("Requesting wakeup, GP1 = 0x%x\n", reg);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004411 iwl4965_set_bit(priv, CSR_GP_CNTRL,
Zhu Yib481de92007-09-25 17:54:57 -07004412 CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
4413 return rc;
4414 }
4415
4416 /* restore this queue's parameters in nic hardware. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004417 rc = iwl4965_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004418 if (rc)
4419 return rc;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004420 iwl4965_write_direct32(priv, HBUS_TARG_WRPTR,
Tomas Winklerfc4b6852007-10-25 17:15:24 +08004421 txq->q.write_ptr | (txq_id << 8));
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004422 iwl4965_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004423
4424 /* else not in power-save mode, uCode will never sleep when we're
4425 * trying to tx (during RFKILL, we're not trying to tx). */
4426 } else
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004427 iwl4965_write32(priv, HBUS_TARG_WRPTR,
Tomas Winklerfc4b6852007-10-25 17:15:24 +08004428 txq->q.write_ptr | (txq_id << 8));
Zhu Yib481de92007-09-25 17:54:57 -07004429
4430 txq->need_update = 0;
4431
4432 return rc;
4433}
4434
Tomas Winkler0a6857e2008-03-12 16:58:49 -07004435#ifdef CONFIG_IWLWIFI_DEBUG
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004436static void iwl4965_print_rx_config_cmd(struct iwl4965_rxon_cmd *rxon)
Zhu Yib481de92007-09-25 17:54:57 -07004437{
Joe Perches0795af52007-10-03 17:59:30 -07004438 DECLARE_MAC_BUF(mac);
4439
Zhu Yib481de92007-09-25 17:54:57 -07004440 IWL_DEBUG_RADIO("RX CONFIG:\n");
Tomas Winkler0a6857e2008-03-12 16:58:49 -07004441 iwl_print_hex_dump(IWL_DL_RADIO, (u8 *) rxon, sizeof(*rxon));
Zhu Yib481de92007-09-25 17:54:57 -07004442 IWL_DEBUG_RADIO("u16 channel: 0x%x\n", le16_to_cpu(rxon->channel));
4443 IWL_DEBUG_RADIO("u32 flags: 0x%08X\n", le32_to_cpu(rxon->flags));
4444 IWL_DEBUG_RADIO("u32 filter_flags: 0x%08x\n",
4445 le32_to_cpu(rxon->filter_flags));
4446 IWL_DEBUG_RADIO("u8 dev_type: 0x%x\n", rxon->dev_type);
4447 IWL_DEBUG_RADIO("u8 ofdm_basic_rates: 0x%02x\n",
4448 rxon->ofdm_basic_rates);
4449 IWL_DEBUG_RADIO("u8 cck_basic_rates: 0x%02x\n", rxon->cck_basic_rates);
Joe Perches0795af52007-10-03 17:59:30 -07004450 IWL_DEBUG_RADIO("u8[6] node_addr: %s\n",
4451 print_mac(mac, rxon->node_addr));
4452 IWL_DEBUG_RADIO("u8[6] bssid_addr: %s\n",
4453 print_mac(mac, rxon->bssid_addr));
Zhu Yib481de92007-09-25 17:54:57 -07004454 IWL_DEBUG_RADIO("u16 assoc_id: 0x%x\n", le16_to_cpu(rxon->assoc_id));
4455}
4456#endif
4457
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004458static void iwl4965_enable_interrupts(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004459{
4460 IWL_DEBUG_ISR("Enabling interrupts\n");
4461 set_bit(STATUS_INT_ENABLED, &priv->status);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004462 iwl4965_write32(priv, CSR_INT_MASK, CSR_INI_SET_MASK);
Zhu Yib481de92007-09-25 17:54:57 -07004463}
4464
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004465static inline void iwl4965_disable_interrupts(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004466{
4467 clear_bit(STATUS_INT_ENABLED, &priv->status);
4468
4469 /* disable interrupts from uCode/NIC to host */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004470 iwl4965_write32(priv, CSR_INT_MASK, 0x00000000);
Zhu Yib481de92007-09-25 17:54:57 -07004471
4472 /* acknowledge/clear/reset any interrupts still pending
4473 * from uCode or flow handler (Rx/Tx DMA) */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004474 iwl4965_write32(priv, CSR_INT, 0xffffffff);
4475 iwl4965_write32(priv, CSR_FH_INT_STATUS, 0xffffffff);
Zhu Yib481de92007-09-25 17:54:57 -07004476 IWL_DEBUG_ISR("Disabled interrupts\n");
4477}
4478
4479static const char *desc_lookup(int i)
4480{
4481 switch (i) {
4482 case 1:
4483 return "FAIL";
4484 case 2:
4485 return "BAD_PARAM";
4486 case 3:
4487 return "BAD_CHECKSUM";
4488 case 4:
4489 return "NMI_INTERRUPT";
4490 case 5:
4491 return "SYSASSERT";
4492 case 6:
4493 return "FATAL_ERROR";
4494 }
4495
4496 return "UNKNOWN";
4497}
4498
4499#define ERROR_START_OFFSET (1 * sizeof(u32))
4500#define ERROR_ELEM_SIZE (7 * sizeof(u32))
4501
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004502static void iwl4965_dump_nic_error_log(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004503{
4504 u32 data2, line;
4505 u32 desc, time, count, base, data1;
4506 u32 blink1, blink2, ilink1, ilink2;
4507 int rc;
4508
4509 base = le32_to_cpu(priv->card_alive.error_event_table_ptr);
4510
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004511 if (!iwl4965_hw_valid_rtc_data_addr(base)) {
Zhu Yib481de92007-09-25 17:54:57 -07004512 IWL_ERROR("Not valid error log pointer 0x%08X\n", base);
4513 return;
4514 }
4515
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004516 rc = iwl4965_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004517 if (rc) {
4518 IWL_WARNING("Can not read from adapter at this time.\n");
4519 return;
4520 }
4521
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004522 count = iwl4965_read_targ_mem(priv, base);
Zhu Yib481de92007-09-25 17:54:57 -07004523
4524 if (ERROR_START_OFFSET <= count * ERROR_ELEM_SIZE) {
4525 IWL_ERROR("Start IWL Error Log Dump:\n");
Tomas Winkler2acae162008-03-02 01:25:59 +02004526 IWL_ERROR("Status: 0x%08lX, count: %d\n", priv->status, count);
Zhu Yib481de92007-09-25 17:54:57 -07004527 }
4528
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004529 desc = iwl4965_read_targ_mem(priv, base + 1 * sizeof(u32));
4530 blink1 = iwl4965_read_targ_mem(priv, base + 3 * sizeof(u32));
4531 blink2 = iwl4965_read_targ_mem(priv, base + 4 * sizeof(u32));
4532 ilink1 = iwl4965_read_targ_mem(priv, base + 5 * sizeof(u32));
4533 ilink2 = iwl4965_read_targ_mem(priv, base + 6 * sizeof(u32));
4534 data1 = iwl4965_read_targ_mem(priv, base + 7 * sizeof(u32));
4535 data2 = iwl4965_read_targ_mem(priv, base + 8 * sizeof(u32));
4536 line = iwl4965_read_targ_mem(priv, base + 9 * sizeof(u32));
4537 time = iwl4965_read_targ_mem(priv, base + 11 * sizeof(u32));
Zhu Yib481de92007-09-25 17:54:57 -07004538
4539 IWL_ERROR("Desc Time "
4540 "data1 data2 line\n");
4541 IWL_ERROR("%-13s (#%d) %010u 0x%08X 0x%08X %u\n",
4542 desc_lookup(desc), desc, time, data1, data2, line);
4543 IWL_ERROR("blink1 blink2 ilink1 ilink2\n");
4544 IWL_ERROR("0x%05X 0x%05X 0x%05X 0x%05X\n", blink1, blink2,
4545 ilink1, ilink2);
4546
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004547 iwl4965_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004548}
4549
4550#define EVENT_START_OFFSET (4 * sizeof(u32))
4551
4552/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004553 * iwl4965_print_event_log - Dump error event log to syslog
Zhu Yib481de92007-09-25 17:54:57 -07004554 *
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004555 * NOTE: Must be called with iwl4965_grab_nic_access() already obtained!
Zhu Yib481de92007-09-25 17:54:57 -07004556 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004557static void iwl4965_print_event_log(struct iwl_priv *priv, u32 start_idx,
Zhu Yib481de92007-09-25 17:54:57 -07004558 u32 num_events, u32 mode)
4559{
4560 u32 i;
4561 u32 base; /* SRAM byte address of event log header */
4562 u32 event_size; /* 2 u32s, or 3 u32s if timestamp recorded */
4563 u32 ptr; /* SRAM byte address of log data */
4564 u32 ev, time, data; /* event log data */
4565
4566 if (num_events == 0)
4567 return;
4568
4569 base = le32_to_cpu(priv->card_alive.log_event_table_ptr);
4570
4571 if (mode == 0)
4572 event_size = 2 * sizeof(u32);
4573 else
4574 event_size = 3 * sizeof(u32);
4575
4576 ptr = base + EVENT_START_OFFSET + (start_idx * event_size);
4577
4578 /* "time" is actually "data" for mode 0 (no timestamp).
4579 * place event id # at far right for easier visual parsing. */
4580 for (i = 0; i < num_events; i++) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004581 ev = iwl4965_read_targ_mem(priv, ptr);
Zhu Yib481de92007-09-25 17:54:57 -07004582 ptr += sizeof(u32);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004583 time = iwl4965_read_targ_mem(priv, ptr);
Zhu Yib481de92007-09-25 17:54:57 -07004584 ptr += sizeof(u32);
4585 if (mode == 0)
4586 IWL_ERROR("0x%08x\t%04u\n", time, ev); /* data, ev */
4587 else {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004588 data = iwl4965_read_targ_mem(priv, ptr);
Zhu Yib481de92007-09-25 17:54:57 -07004589 ptr += sizeof(u32);
4590 IWL_ERROR("%010u\t0x%08x\t%04u\n", time, data, ev);
4591 }
4592 }
4593}
4594
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004595static void iwl4965_dump_nic_event_log(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004596{
4597 int rc;
4598 u32 base; /* SRAM byte address of event log header */
4599 u32 capacity; /* event log capacity in # entries */
4600 u32 mode; /* 0 - no timestamp, 1 - timestamp recorded */
4601 u32 num_wraps; /* # times uCode wrapped to top of log */
4602 u32 next_entry; /* index of next entry to be written by uCode */
4603 u32 size; /* # entries that we'll print */
4604
4605 base = le32_to_cpu(priv->card_alive.log_event_table_ptr);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004606 if (!iwl4965_hw_valid_rtc_data_addr(base)) {
Zhu Yib481de92007-09-25 17:54:57 -07004607 IWL_ERROR("Invalid event log pointer 0x%08X\n", base);
4608 return;
4609 }
4610
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004611 rc = iwl4965_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004612 if (rc) {
4613 IWL_WARNING("Can not read from adapter at this time.\n");
4614 return;
4615 }
4616
4617 /* event log header */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004618 capacity = iwl4965_read_targ_mem(priv, base);
4619 mode = iwl4965_read_targ_mem(priv, base + (1 * sizeof(u32)));
4620 num_wraps = iwl4965_read_targ_mem(priv, base + (2 * sizeof(u32)));
4621 next_entry = iwl4965_read_targ_mem(priv, base + (3 * sizeof(u32)));
Zhu Yib481de92007-09-25 17:54:57 -07004622
4623 size = num_wraps ? capacity : next_entry;
4624
4625 /* bail out if nothing in log */
4626 if (size == 0) {
Zhu Yi583fab32007-09-27 11:27:30 +08004627 IWL_ERROR("Start IWL Event Log Dump: nothing in log\n");
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004628 iwl4965_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004629 return;
4630 }
4631
Zhu Yi583fab32007-09-27 11:27:30 +08004632 IWL_ERROR("Start IWL Event Log Dump: display count %d, wraps %d\n",
Zhu Yib481de92007-09-25 17:54:57 -07004633 size, num_wraps);
4634
4635 /* if uCode has wrapped back to top of log, start at the oldest entry,
4636 * i.e the next one that uCode would fill. */
4637 if (num_wraps)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004638 iwl4965_print_event_log(priv, next_entry,
Zhu Yib481de92007-09-25 17:54:57 -07004639 capacity - next_entry, mode);
4640
4641 /* (then/else) start at top of log */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004642 iwl4965_print_event_log(priv, 0, next_entry, mode);
Zhu Yib481de92007-09-25 17:54:57 -07004643
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004644 iwl4965_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004645}
4646
4647/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004648 * iwl4965_irq_handle_error - called for HW or SW error interrupt from card
Zhu Yib481de92007-09-25 17:54:57 -07004649 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004650static void iwl4965_irq_handle_error(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004651{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004652 /* Set the FW error flag -- cleared on iwl4965_down */
Zhu Yib481de92007-09-25 17:54:57 -07004653 set_bit(STATUS_FW_ERROR, &priv->status);
4654
4655 /* Cancel currently queued command. */
4656 clear_bit(STATUS_HCMD_ACTIVE, &priv->status);
4657
Tomas Winkler0a6857e2008-03-12 16:58:49 -07004658#ifdef CONFIG_IWLWIFI_DEBUG
4659 if (iwl_debug_level & IWL_DL_FW_ERRORS) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004660 iwl4965_dump_nic_error_log(priv);
4661 iwl4965_dump_nic_event_log(priv);
4662 iwl4965_print_rx_config_cmd(&priv->staging_rxon);
Zhu Yib481de92007-09-25 17:54:57 -07004663 }
4664#endif
4665
4666 wake_up_interruptible(&priv->wait_command_queue);
4667
4668 /* Keep the restart process from trying to send host
4669 * commands by clearing the INIT status bit */
4670 clear_bit(STATUS_READY, &priv->status);
4671
4672 if (!test_bit(STATUS_EXIT_PENDING, &priv->status)) {
4673 IWL_DEBUG(IWL_DL_INFO | IWL_DL_FW_ERRORS,
4674 "Restarting adapter due to uCode error.\n");
4675
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004676 if (iwl4965_is_associated(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07004677 memcpy(&priv->recovery_rxon, &priv->active_rxon,
4678 sizeof(priv->recovery_rxon));
4679 priv->error_recovering = 1;
4680 }
4681 queue_work(priv->workqueue, &priv->restart);
4682 }
4683}
4684
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004685static void iwl4965_error_recovery(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004686{
4687 unsigned long flags;
4688
4689 memcpy(&priv->staging_rxon, &priv->recovery_rxon,
4690 sizeof(priv->staging_rxon));
4691 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004692 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004693
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004694 iwl4965_rxon_add_station(priv, priv->bssid, 1);
Zhu Yib481de92007-09-25 17:54:57 -07004695
4696 spin_lock_irqsave(&priv->lock, flags);
4697 priv->assoc_id = le16_to_cpu(priv->staging_rxon.assoc_id);
4698 priv->error_recovering = 0;
4699 spin_unlock_irqrestore(&priv->lock, flags);
4700}
4701
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004702static void iwl4965_irq_tasklet(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004703{
4704 u32 inta, handled = 0;
4705 u32 inta_fh;
4706 unsigned long flags;
Tomas Winkler0a6857e2008-03-12 16:58:49 -07004707#ifdef CONFIG_IWLWIFI_DEBUG
Zhu Yib481de92007-09-25 17:54:57 -07004708 u32 inta_mask;
4709#endif
4710
4711 spin_lock_irqsave(&priv->lock, flags);
4712
4713 /* Ack/clear/reset pending uCode interrupts.
4714 * Note: Some bits in CSR_INT are "OR" of bits in CSR_FH_INT_STATUS,
4715 * and will clear only when CSR_FH_INT_STATUS gets cleared. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004716 inta = iwl4965_read32(priv, CSR_INT);
4717 iwl4965_write32(priv, CSR_INT, inta);
Zhu Yib481de92007-09-25 17:54:57 -07004718
4719 /* Ack/clear/reset pending flow-handler (DMA) interrupts.
4720 * Any new interrupts that happen after this, either while we're
4721 * in this tasklet, or later, will show up in next ISR/tasklet. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004722 inta_fh = iwl4965_read32(priv, CSR_FH_INT_STATUS);
4723 iwl4965_write32(priv, CSR_FH_INT_STATUS, inta_fh);
Zhu Yib481de92007-09-25 17:54:57 -07004724
Tomas Winkler0a6857e2008-03-12 16:58:49 -07004725#ifdef CONFIG_IWLWIFI_DEBUG
4726 if (iwl_debug_level & IWL_DL_ISR) {
Ben Cahill9fbab512007-11-29 11:09:47 +08004727 /* just for debug */
4728 inta_mask = iwl4965_read32(priv, CSR_INT_MASK);
Zhu Yib481de92007-09-25 17:54:57 -07004729 IWL_DEBUG_ISR("inta 0x%08x, enabled 0x%08x, fh 0x%08x\n",
4730 inta, inta_mask, inta_fh);
4731 }
4732#endif
4733
4734 /* Since CSR_INT and CSR_FH_INT_STATUS reads and clears are not
4735 * atomic, make sure that inta covers all the interrupts that
4736 * we've discovered, even if FH interrupt came in just after
4737 * reading CSR_INT. */
Tomas Winkler6f83eaa2008-03-04 18:09:28 -08004738 if (inta_fh & CSR49_FH_INT_RX_MASK)
Zhu Yib481de92007-09-25 17:54:57 -07004739 inta |= CSR_INT_BIT_FH_RX;
Tomas Winkler6f83eaa2008-03-04 18:09:28 -08004740 if (inta_fh & CSR49_FH_INT_TX_MASK)
Zhu Yib481de92007-09-25 17:54:57 -07004741 inta |= CSR_INT_BIT_FH_TX;
4742
4743 /* Now service all interrupt bits discovered above. */
4744 if (inta & CSR_INT_BIT_HW_ERR) {
4745 IWL_ERROR("Microcode HW error detected. Restarting.\n");
4746
4747 /* Tell the device to stop sending interrupts */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004748 iwl4965_disable_interrupts(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004749
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004750 iwl4965_irq_handle_error(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004751
4752 handled |= CSR_INT_BIT_HW_ERR;
4753
4754 spin_unlock_irqrestore(&priv->lock, flags);
4755
4756 return;
4757 }
4758
Tomas Winkler0a6857e2008-03-12 16:58:49 -07004759#ifdef CONFIG_IWLWIFI_DEBUG
4760 if (iwl_debug_level & (IWL_DL_ISR)) {
Zhu Yib481de92007-09-25 17:54:57 -07004761 /* NIC fires this, but we don't use it, redundant with WAKEUP */
Joonwoo Park25c03d82008-01-23 10:15:20 -08004762 if (inta & CSR_INT_BIT_SCD)
4763 IWL_DEBUG_ISR("Scheduler finished to transmit "
4764 "the frame/frames.\n");
Zhu Yib481de92007-09-25 17:54:57 -07004765
4766 /* Alive notification via Rx interrupt will do the real work */
4767 if (inta & CSR_INT_BIT_ALIVE)
4768 IWL_DEBUG_ISR("Alive interrupt\n");
4769 }
4770#endif
4771 /* Safely ignore these bits for debug checks below */
Joonwoo Park25c03d82008-01-23 10:15:20 -08004772 inta &= ~(CSR_INT_BIT_SCD | CSR_INT_BIT_ALIVE);
Zhu Yib481de92007-09-25 17:54:57 -07004773
Ben Cahill9fbab512007-11-29 11:09:47 +08004774 /* HW RF KILL switch toggled */
Zhu Yib481de92007-09-25 17:54:57 -07004775 if (inta & CSR_INT_BIT_RF_KILL) {
4776 int hw_rf_kill = 0;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004777 if (!(iwl4965_read32(priv, CSR_GP_CNTRL) &
Zhu Yib481de92007-09-25 17:54:57 -07004778 CSR_GP_CNTRL_REG_FLAG_HW_RF_KILL_SW))
4779 hw_rf_kill = 1;
4780
4781 IWL_DEBUG(IWL_DL_INFO | IWL_DL_RF_KILL | IWL_DL_ISR,
4782 "RF_KILL bit toggled to %s.\n",
4783 hw_rf_kill ? "disable radio":"enable radio");
4784
4785 /* Queue restart only if RF_KILL switch was set to "kill"
4786 * when we loaded driver, and is now set to "enable".
4787 * After we're Alive, RF_KILL gets handled by
Reinette Chatre32304552008-02-15 14:34:37 -08004788 * iwl4965_rx_card_state_notif() */
Zhu Yi53e49092007-12-06 16:08:44 +08004789 if (!hw_rf_kill && !test_bit(STATUS_ALIVE, &priv->status)) {
4790 clear_bit(STATUS_RF_KILL_HW, &priv->status);
Zhu Yib481de92007-09-25 17:54:57 -07004791 queue_work(priv->workqueue, &priv->restart);
Zhu Yi53e49092007-12-06 16:08:44 +08004792 }
Zhu Yib481de92007-09-25 17:54:57 -07004793
4794 handled |= CSR_INT_BIT_RF_KILL;
4795 }
4796
Ben Cahill9fbab512007-11-29 11:09:47 +08004797 /* Chip got too hot and stopped itself */
Zhu Yib481de92007-09-25 17:54:57 -07004798 if (inta & CSR_INT_BIT_CT_KILL) {
4799 IWL_ERROR("Microcode CT kill error detected.\n");
4800 handled |= CSR_INT_BIT_CT_KILL;
4801 }
4802
4803 /* Error detected by uCode */
4804 if (inta & CSR_INT_BIT_SW_ERR) {
4805 IWL_ERROR("Microcode SW error detected. Restarting 0x%X.\n",
4806 inta);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004807 iwl4965_irq_handle_error(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004808 handled |= CSR_INT_BIT_SW_ERR;
4809 }
4810
4811 /* uCode wakes up after power-down sleep */
4812 if (inta & CSR_INT_BIT_WAKEUP) {
4813 IWL_DEBUG_ISR("Wakeup interrupt\n");
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004814 iwl4965_rx_queue_update_write_ptr(priv, &priv->rxq);
4815 iwl4965_tx_queue_update_write_ptr(priv, &priv->txq[0]);
4816 iwl4965_tx_queue_update_write_ptr(priv, &priv->txq[1]);
4817 iwl4965_tx_queue_update_write_ptr(priv, &priv->txq[2]);
4818 iwl4965_tx_queue_update_write_ptr(priv, &priv->txq[3]);
4819 iwl4965_tx_queue_update_write_ptr(priv, &priv->txq[4]);
4820 iwl4965_tx_queue_update_write_ptr(priv, &priv->txq[5]);
Zhu Yib481de92007-09-25 17:54:57 -07004821
4822 handled |= CSR_INT_BIT_WAKEUP;
4823 }
4824
4825 /* All uCode command responses, including Tx command responses,
4826 * Rx "responses" (frame-received notification), and other
4827 * notifications from uCode come through here*/
4828 if (inta & (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX)) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004829 iwl4965_rx_handle(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004830 handled |= (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX);
4831 }
4832
4833 if (inta & CSR_INT_BIT_FH_TX) {
4834 IWL_DEBUG_ISR("Tx interrupt\n");
4835 handled |= CSR_INT_BIT_FH_TX;
4836 }
4837
4838 if (inta & ~handled)
4839 IWL_ERROR("Unhandled INTA bits 0x%08x\n", inta & ~handled);
4840
4841 if (inta & ~CSR_INI_SET_MASK) {
4842 IWL_WARNING("Disabled INTA bits 0x%08x were pending\n",
4843 inta & ~CSR_INI_SET_MASK);
4844 IWL_WARNING(" with FH_INT = 0x%08x\n", inta_fh);
4845 }
4846
4847 /* Re-enable all interrupts */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004848 iwl4965_enable_interrupts(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004849
Tomas Winkler0a6857e2008-03-12 16:58:49 -07004850#ifdef CONFIG_IWLWIFI_DEBUG
4851 if (iwl_debug_level & (IWL_DL_ISR)) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004852 inta = iwl4965_read32(priv, CSR_INT);
4853 inta_mask = iwl4965_read32(priv, CSR_INT_MASK);
4854 inta_fh = iwl4965_read32(priv, CSR_FH_INT_STATUS);
Zhu Yib481de92007-09-25 17:54:57 -07004855 IWL_DEBUG_ISR("End inta 0x%08x, enabled 0x%08x, fh 0x%08x, "
4856 "flags 0x%08lx\n", inta, inta_mask, inta_fh, flags);
4857 }
4858#endif
4859 spin_unlock_irqrestore(&priv->lock, flags);
4860}
4861
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004862static irqreturn_t iwl4965_isr(int irq, void *data)
Zhu Yib481de92007-09-25 17:54:57 -07004863{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004864 struct iwl_priv *priv = data;
Zhu Yib481de92007-09-25 17:54:57 -07004865 u32 inta, inta_mask;
4866 u32 inta_fh;
4867 if (!priv)
4868 return IRQ_NONE;
4869
4870 spin_lock(&priv->lock);
4871
4872 /* Disable (but don't clear!) interrupts here to avoid
4873 * back-to-back ISRs and sporadic interrupts from our NIC.
4874 * If we have something to service, the tasklet will re-enable ints.
4875 * If we *don't* have something, we'll re-enable before leaving here. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004876 inta_mask = iwl4965_read32(priv, CSR_INT_MASK); /* just for debug */
4877 iwl4965_write32(priv, CSR_INT_MASK, 0x00000000);
Zhu Yib481de92007-09-25 17:54:57 -07004878
4879 /* Discover which interrupts are active/pending */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004880 inta = iwl4965_read32(priv, CSR_INT);
4881 inta_fh = iwl4965_read32(priv, CSR_FH_INT_STATUS);
Zhu Yib481de92007-09-25 17:54:57 -07004882
4883 /* Ignore interrupt if there's nothing in NIC to service.
4884 * This may be due to IRQ shared with another device,
4885 * or due to sporadic interrupts thrown from our NIC. */
4886 if (!inta && !inta_fh) {
4887 IWL_DEBUG_ISR("Ignore interrupt, inta == 0, inta_fh == 0\n");
4888 goto none;
4889 }
4890
4891 if ((inta == 0xFFFFFFFF) || ((inta & 0xFFFFFFF0) == 0xa5a5a5a0)) {
Oliver Neukum66fbb542007-11-15 09:31:10 +08004892 /* Hardware disappeared. It might have already raised
4893 * an interrupt */
Zhu Yib481de92007-09-25 17:54:57 -07004894 IWL_WARNING("HARDWARE GONE?? INTA == 0x%080x\n", inta);
Oliver Neukum66fbb542007-11-15 09:31:10 +08004895 goto unplugged;
Zhu Yib481de92007-09-25 17:54:57 -07004896 }
4897
4898 IWL_DEBUG_ISR("ISR inta 0x%08x, enabled 0x%08x, fh 0x%08x\n",
4899 inta, inta_mask, inta_fh);
4900
Joonwoo Park25c03d82008-01-23 10:15:20 -08004901 inta &= ~CSR_INT_BIT_SCD;
4902
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004903 /* iwl4965_irq_tasklet() will service interrupts and re-enable them */
Joonwoo Park25c03d82008-01-23 10:15:20 -08004904 if (likely(inta || inta_fh))
4905 tasklet_schedule(&priv->irq_tasklet);
Zhu Yib481de92007-09-25 17:54:57 -07004906
Oliver Neukum66fbb542007-11-15 09:31:10 +08004907 unplugged:
4908 spin_unlock(&priv->lock);
Zhu Yib481de92007-09-25 17:54:57 -07004909 return IRQ_HANDLED;
4910
4911 none:
4912 /* re-enable interrupts here since we don't have anything to service. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004913 iwl4965_enable_interrupts(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004914 spin_unlock(&priv->lock);
4915 return IRQ_NONE;
4916}
4917
Zhu Yib481de92007-09-25 17:54:57 -07004918/* For active scan, listen ACTIVE_DWELL_TIME (msec) on each channel after
4919 * sending probe req. This should be set long enough to hear probe responses
4920 * from more than one AP. */
4921#define IWL_ACTIVE_DWELL_TIME_24 (20) /* all times in msec */
4922#define IWL_ACTIVE_DWELL_TIME_52 (10)
4923
4924/* For faster active scanning, scan will move to the next channel if fewer than
4925 * PLCP_QUIET_THRESH packets are heard on this channel within
4926 * ACTIVE_QUIET_TIME after sending probe request. This shortens the dwell
4927 * time if it's a quiet channel (nothing responded to our probe, and there's
4928 * no other traffic).
4929 * Disable "quiet" feature by setting PLCP_QUIET_THRESH to 0. */
4930#define IWL_PLCP_QUIET_THRESH __constant_cpu_to_le16(1) /* packets */
4931#define IWL_ACTIVE_QUIET_TIME __constant_cpu_to_le16(5) /* msec */
4932
4933/* For passive scan, listen PASSIVE_DWELL_TIME (msec) on each channel.
4934 * Must be set longer than active dwell time.
4935 * For the most reliable scan, set > AP beacon interval (typically 100msec). */
4936#define IWL_PASSIVE_DWELL_TIME_24 (20) /* all times in msec */
4937#define IWL_PASSIVE_DWELL_TIME_52 (10)
4938#define IWL_PASSIVE_DWELL_BASE (100)
4939#define IWL_CHANNEL_TUNE_TIME 5
4940
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004941static inline u16 iwl4965_get_active_dwell_time(struct iwl_priv *priv,
Johannes Berg8318d782008-01-24 19:38:38 +01004942 enum ieee80211_band band)
Zhu Yib481de92007-09-25 17:54:57 -07004943{
Johannes Berg8318d782008-01-24 19:38:38 +01004944 if (band == IEEE80211_BAND_5GHZ)
Zhu Yib481de92007-09-25 17:54:57 -07004945 return IWL_ACTIVE_DWELL_TIME_52;
4946 else
4947 return IWL_ACTIVE_DWELL_TIME_24;
4948}
4949
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004950static u16 iwl4965_get_passive_dwell_time(struct iwl_priv *priv,
Johannes Berg8318d782008-01-24 19:38:38 +01004951 enum ieee80211_band band)
Zhu Yib481de92007-09-25 17:54:57 -07004952{
Johannes Berg8318d782008-01-24 19:38:38 +01004953 u16 active = iwl4965_get_active_dwell_time(priv, band);
4954 u16 passive = (band != IEEE80211_BAND_5GHZ) ?
Zhu Yib481de92007-09-25 17:54:57 -07004955 IWL_PASSIVE_DWELL_BASE + IWL_PASSIVE_DWELL_TIME_24 :
4956 IWL_PASSIVE_DWELL_BASE + IWL_PASSIVE_DWELL_TIME_52;
4957
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004958 if (iwl4965_is_associated(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07004959 /* If we're associated, we clamp the maximum passive
4960 * dwell time to be 98% of the beacon interval (minus
4961 * 2 * channel tune time) */
4962 passive = priv->beacon_int;
4963 if ((passive > IWL_PASSIVE_DWELL_BASE) || !passive)
4964 passive = IWL_PASSIVE_DWELL_BASE;
4965 passive = (passive * 98) / 100 - IWL_CHANNEL_TUNE_TIME * 2;
4966 }
4967
4968 if (passive <= active)
4969 passive = active + 1;
4970
4971 return passive;
4972}
4973
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004974static int iwl4965_get_channels_for_scan(struct iwl_priv *priv,
Johannes Berg8318d782008-01-24 19:38:38 +01004975 enum ieee80211_band band,
Zhu Yib481de92007-09-25 17:54:57 -07004976 u8 is_active, u8 direct_mask,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004977 struct iwl4965_scan_channel *scan_ch)
Zhu Yib481de92007-09-25 17:54:57 -07004978{
4979 const struct ieee80211_channel *channels = NULL;
Johannes Berg8318d782008-01-24 19:38:38 +01004980 const struct ieee80211_supported_band *sband;
Assaf Kraussbf85ea42008-03-14 10:38:49 -07004981 const struct iwl_channel_info *ch_info;
Zhu Yib481de92007-09-25 17:54:57 -07004982 u16 passive_dwell = 0;
4983 u16 active_dwell = 0;
4984 int added, i;
4985
Johannes Berg8318d782008-01-24 19:38:38 +01004986 sband = iwl4965_get_hw_mode(priv, band);
4987 if (!sband)
Zhu Yib481de92007-09-25 17:54:57 -07004988 return 0;
4989
Johannes Berg8318d782008-01-24 19:38:38 +01004990 channels = sband->channels;
Zhu Yib481de92007-09-25 17:54:57 -07004991
Johannes Berg8318d782008-01-24 19:38:38 +01004992 active_dwell = iwl4965_get_active_dwell_time(priv, band);
4993 passive_dwell = iwl4965_get_passive_dwell_time(priv, band);
Zhu Yib481de92007-09-25 17:54:57 -07004994
Johannes Berg8318d782008-01-24 19:38:38 +01004995 for (i = 0, added = 0; i < sband->n_channels; i++) {
4996 if (ieee80211_frequency_to_channel(channels[i].center_freq) ==
Zhu Yib481de92007-09-25 17:54:57 -07004997 le16_to_cpu(priv->active_rxon.channel)) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004998 if (iwl4965_is_associated(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07004999 IWL_DEBUG_SCAN
5000 ("Skipping current channel %d\n",
5001 le16_to_cpu(priv->active_rxon.channel));
5002 continue;
5003 }
5004 } else if (priv->only_active_channel)
5005 continue;
5006
Johannes Berg8318d782008-01-24 19:38:38 +01005007 scan_ch->channel = ieee80211_frequency_to_channel(channels[i].center_freq);
Zhu Yib481de92007-09-25 17:54:57 -07005008
Johannes Berg8318d782008-01-24 19:38:38 +01005009 ch_info = iwl4965_get_channel_info(priv, band,
Ben Cahill9fbab512007-11-29 11:09:47 +08005010 scan_ch->channel);
Zhu Yib481de92007-09-25 17:54:57 -07005011 if (!is_channel_valid(ch_info)) {
5012 IWL_DEBUG_SCAN("Channel %d is INVALID for this SKU.\n",
5013 scan_ch->channel);
5014 continue;
5015 }
5016
5017 if (!is_active || is_channel_passive(ch_info) ||
Johannes Berg8318d782008-01-24 19:38:38 +01005018 (channels[i].flags & IEEE80211_CHAN_PASSIVE_SCAN))
Zhu Yib481de92007-09-25 17:54:57 -07005019 scan_ch->type = 0; /* passive */
5020 else
5021 scan_ch->type = 1; /* active */
5022
5023 if (scan_ch->type & 1)
5024 scan_ch->type |= (direct_mask << 1);
5025
5026 if (is_channel_narrow(ch_info))
5027 scan_ch->type |= (1 << 7);
5028
5029 scan_ch->active_dwell = cpu_to_le16(active_dwell);
5030 scan_ch->passive_dwell = cpu_to_le16(passive_dwell);
5031
Ben Cahill9fbab512007-11-29 11:09:47 +08005032 /* Set txpower levels to defaults */
Zhu Yib481de92007-09-25 17:54:57 -07005033 scan_ch->tpc.dsp_atten = 110;
5034 /* scan_pwr_info->tpc.dsp_atten; */
5035
5036 /*scan_pwr_info->tpc.tx_gain; */
Johannes Berg8318d782008-01-24 19:38:38 +01005037 if (band == IEEE80211_BAND_5GHZ)
Zhu Yib481de92007-09-25 17:54:57 -07005038 scan_ch->tpc.tx_gain = ((1 << 5) | (3 << 3)) | 3;
5039 else {
5040 scan_ch->tpc.tx_gain = ((1 << 5) | (5 << 3));
5041 /* NOTE: if we were doing 6Mb OFDM for scans we'd use
Ben Cahill9fbab512007-11-29 11:09:47 +08005042 * power level:
Reinette Chatre8a1b0242008-01-14 17:46:25 -08005043 * scan_ch->tpc.tx_gain = ((1 << 5) | (2 << 3)) | 3;
Zhu Yib481de92007-09-25 17:54:57 -07005044 */
5045 }
5046
5047 IWL_DEBUG_SCAN("Scanning %d [%s %d]\n",
5048 scan_ch->channel,
5049 (scan_ch->type & 1) ? "ACTIVE" : "PASSIVE",
5050 (scan_ch->type & 1) ?
5051 active_dwell : passive_dwell);
5052
5053 scan_ch++;
5054 added++;
5055 }
5056
5057 IWL_DEBUG_SCAN("total channels to scan %d \n", added);
5058 return added;
5059}
5060
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005061static void iwl4965_init_hw_rates(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07005062 struct ieee80211_rate *rates)
5063{
5064 int i;
5065
5066 for (i = 0; i < IWL_RATE_COUNT; i++) {
Johannes Berg8318d782008-01-24 19:38:38 +01005067 rates[i].bitrate = iwl4965_rates[i].ieee * 5;
5068 rates[i].hw_value = i; /* Rate scaling will work on indexes */
5069 rates[i].hw_value_short = i;
5070 rates[i].flags = 0;
5071 if ((i > IWL_LAST_OFDM_RATE) || (i < IWL_FIRST_OFDM_RATE)) {
Zhu Yib481de92007-09-25 17:54:57 -07005072 /*
Johannes Berg8318d782008-01-24 19:38:38 +01005073 * If CCK != 1M then set short preamble rate flag.
Zhu Yib481de92007-09-25 17:54:57 -07005074 */
Tomas Winkler35cdeaf2008-03-11 16:17:20 -07005075 rates[i].flags |=
5076 (iwl4965_rates[i].plcp == IWL_RATE_1M_PLCP) ?
5077 0 : IEEE80211_RATE_SHORT_PREAMBLE;
Zhu Yib481de92007-09-25 17:54:57 -07005078 }
Zhu Yib481de92007-09-25 17:54:57 -07005079 }
Zhu Yib481de92007-09-25 17:54:57 -07005080}
5081
5082/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005083 * iwl4965_init_geos - Initialize mac80211's geo/channel info based from eeprom
Zhu Yib481de92007-09-25 17:54:57 -07005084 */
Assaf Kraussbf85ea42008-03-14 10:38:49 -07005085int iwl4965_init_geos(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005086{
Assaf Kraussbf85ea42008-03-14 10:38:49 -07005087 struct iwl_channel_info *ch;
Tomas Winkler8211ef72008-03-02 01:36:04 +02005088 struct ieee80211_supported_band *sband;
Zhu Yib481de92007-09-25 17:54:57 -07005089 struct ieee80211_channel *channels;
5090 struct ieee80211_channel *geo_ch;
5091 struct ieee80211_rate *rates;
5092 int i = 0;
Zhu Yib481de92007-09-25 17:54:57 -07005093
Johannes Berg8318d782008-01-24 19:38:38 +01005094 if (priv->bands[IEEE80211_BAND_2GHZ].n_bitrates ||
5095 priv->bands[IEEE80211_BAND_5GHZ].n_bitrates) {
Zhu Yib481de92007-09-25 17:54:57 -07005096 IWL_DEBUG_INFO("Geography modes already initialized.\n");
5097 set_bit(STATUS_GEO_CONFIGURED, &priv->status);
5098 return 0;
5099 }
5100
Zhu Yib481de92007-09-25 17:54:57 -07005101 channels = kzalloc(sizeof(struct ieee80211_channel) *
5102 priv->channel_count, GFP_KERNEL);
Johannes Berg8318d782008-01-24 19:38:38 +01005103 if (!channels)
Zhu Yib481de92007-09-25 17:54:57 -07005104 return -ENOMEM;
Zhu Yib481de92007-09-25 17:54:57 -07005105
Tomas Winkler8211ef72008-03-02 01:36:04 +02005106 rates = kzalloc((sizeof(struct ieee80211_rate) * (IWL_RATE_COUNT + 1)),
Zhu Yib481de92007-09-25 17:54:57 -07005107 GFP_KERNEL);
5108 if (!rates) {
Zhu Yib481de92007-09-25 17:54:57 -07005109 kfree(channels);
5110 return -ENOMEM;
5111 }
5112
Zhu Yib481de92007-09-25 17:54:57 -07005113 /* 5.2GHz channels start after the 2.4GHz channels */
Tomas Winkler8211ef72008-03-02 01:36:04 +02005114 sband = &priv->bands[IEEE80211_BAND_5GHZ];
Assaf Kraussbf85ea42008-03-14 10:38:49 -07005115 sband->channels = &channels[ARRAY_SIZE(iwl_eeprom_band_1)];
Tomas Winkler8211ef72008-03-02 01:36:04 +02005116 /* just OFDM */
5117 sband->bitrates = &rates[IWL_FIRST_OFDM_RATE];
5118 sband->n_bitrates = IWL_RATE_COUNT - IWL_FIRST_OFDM_RATE;
Johannes Berg8318d782008-01-24 19:38:38 +01005119
Tomas Winkler8211ef72008-03-02 01:36:04 +02005120 iwl4965_init_ht_hw_capab(&sband->ht_info, IEEE80211_BAND_5GHZ);
Tomas Winkler78330fd2008-02-06 02:37:18 +02005121
Tomas Winkler8211ef72008-03-02 01:36:04 +02005122 sband = &priv->bands[IEEE80211_BAND_2GHZ];
5123 sband->channels = channels;
5124 /* OFDM & CCK */
5125 sband->bitrates = rates;
5126 sband->n_bitrates = IWL_RATE_COUNT;
Zhu Yib481de92007-09-25 17:54:57 -07005127
Tomas Winkler8211ef72008-03-02 01:36:04 +02005128 iwl4965_init_ht_hw_capab(&sband->ht_info, IEEE80211_BAND_2GHZ);
Tomas Winkler78330fd2008-02-06 02:37:18 +02005129
Zhu Yib481de92007-09-25 17:54:57 -07005130 priv->ieee_channels = channels;
5131 priv->ieee_rates = rates;
5132
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005133 iwl4965_init_hw_rates(priv, rates);
Zhu Yib481de92007-09-25 17:54:57 -07005134
Tomas Winkler8211ef72008-03-02 01:36:04 +02005135 for (i = 0; i < priv->channel_count; i++) {
Zhu Yib481de92007-09-25 17:54:57 -07005136 ch = &priv->channel_info[i];
5137
Tomas Winkler8211ef72008-03-02 01:36:04 +02005138 /* FIXME: might be removed if scan is OK */
5139 if (!is_channel_valid(ch))
Zhu Yib481de92007-09-25 17:54:57 -07005140 continue;
Zhu Yib481de92007-09-25 17:54:57 -07005141
Tomas Winkler8211ef72008-03-02 01:36:04 +02005142 if (is_channel_a_band(ch))
5143 sband = &priv->bands[IEEE80211_BAND_5GHZ];
5144 else
5145 sband = &priv->bands[IEEE80211_BAND_2GHZ];
Zhu Yib481de92007-09-25 17:54:57 -07005146
Tomas Winkler8211ef72008-03-02 01:36:04 +02005147 geo_ch = &sband->channels[sband->n_channels++];
5148
5149 geo_ch->center_freq = ieee80211_channel_to_frequency(ch->channel);
Johannes Berg8318d782008-01-24 19:38:38 +01005150 geo_ch->max_power = ch->max_power_avg;
5151 geo_ch->max_antenna_gain = 0xff;
Mohamed Abbas7b723042008-01-31 21:46:40 -08005152 geo_ch->hw_value = ch->channel;
Zhu Yib481de92007-09-25 17:54:57 -07005153
5154 if (is_channel_valid(ch)) {
Johannes Berg8318d782008-01-24 19:38:38 +01005155 if (!(ch->flags & EEPROM_CHANNEL_IBSS))
5156 geo_ch->flags |= IEEE80211_CHAN_NO_IBSS;
Zhu Yib481de92007-09-25 17:54:57 -07005157
Johannes Berg8318d782008-01-24 19:38:38 +01005158 if (!(ch->flags & EEPROM_CHANNEL_ACTIVE))
5159 geo_ch->flags |= IEEE80211_CHAN_PASSIVE_SCAN;
Zhu Yib481de92007-09-25 17:54:57 -07005160
5161 if (ch->flags & EEPROM_CHANNEL_RADAR)
Johannes Berg8318d782008-01-24 19:38:38 +01005162 geo_ch->flags |= IEEE80211_CHAN_RADAR;
Zhu Yib481de92007-09-25 17:54:57 -07005163
5164 if (ch->max_power_avg > priv->max_channel_txpower_limit)
5165 priv->max_channel_txpower_limit =
5166 ch->max_power_avg;
Tomas Winkler8211ef72008-03-02 01:36:04 +02005167 } else {
Johannes Berg8318d782008-01-24 19:38:38 +01005168 geo_ch->flags |= IEEE80211_CHAN_DISABLED;
Tomas Winkler8211ef72008-03-02 01:36:04 +02005169 }
5170
5171 /* Save flags for reg domain usage */
5172 geo_ch->orig_flags = geo_ch->flags;
5173
5174 IWL_DEBUG_INFO("Channel %d Freq=%d[%sGHz] %s flag=0%X\n",
5175 ch->channel, geo_ch->center_freq,
5176 is_channel_a_band(ch) ? "5.2" : "2.4",
5177 geo_ch->flags & IEEE80211_CHAN_DISABLED ?
5178 "restricted" : "valid",
5179 geo_ch->flags);
Zhu Yib481de92007-09-25 17:54:57 -07005180 }
5181
Tomas Winkler82b9a122008-03-04 18:09:30 -08005182 if ((priv->bands[IEEE80211_BAND_5GHZ].n_channels == 0) &&
5183 priv->cfg->sku & IWL_SKU_A) {
Zhu Yib481de92007-09-25 17:54:57 -07005184 printk(KERN_INFO DRV_NAME
5185 ": Incorrectly detected BG card as ABG. Please send "
5186 "your PCI ID 0x%04X:0x%04X to maintainer.\n",
5187 priv->pci_dev->device, priv->pci_dev->subsystem_device);
Tomas Winkler82b9a122008-03-04 18:09:30 -08005188 priv->cfg->sku &= ~IWL_SKU_A;
Zhu Yib481de92007-09-25 17:54:57 -07005189 }
5190
5191 printk(KERN_INFO DRV_NAME
5192 ": Tunable channels: %d 802.11bg, %d 802.11a channels\n",
Johannes Berg8318d782008-01-24 19:38:38 +01005193 priv->bands[IEEE80211_BAND_2GHZ].n_channels,
5194 priv->bands[IEEE80211_BAND_5GHZ].n_channels);
Zhu Yib481de92007-09-25 17:54:57 -07005195
Johannes Berg8318d782008-01-24 19:38:38 +01005196 priv->hw->wiphy->bands[IEEE80211_BAND_2GHZ] = &priv->bands[IEEE80211_BAND_2GHZ];
5197 priv->hw->wiphy->bands[IEEE80211_BAND_5GHZ] = &priv->bands[IEEE80211_BAND_5GHZ];
Zhu Yib481de92007-09-25 17:54:57 -07005198
Zhu Yib481de92007-09-25 17:54:57 -07005199 set_bit(STATUS_GEO_CONFIGURED, &priv->status);
5200
5201 return 0;
5202}
5203
Reinette Chatre849e0dc2008-01-23 10:15:18 -08005204/*
5205 * iwl4965_free_geos - undo allocations in iwl4965_init_geos
5206 */
Assaf Kraussbf85ea42008-03-14 10:38:49 -07005207void iwl4965_free_geos(struct iwl_priv *priv)
Reinette Chatre849e0dc2008-01-23 10:15:18 -08005208{
Reinette Chatre849e0dc2008-01-23 10:15:18 -08005209 kfree(priv->ieee_channels);
5210 kfree(priv->ieee_rates);
5211 clear_bit(STATUS_GEO_CONFIGURED, &priv->status);
5212}
5213
Zhu Yib481de92007-09-25 17:54:57 -07005214/******************************************************************************
5215 *
5216 * uCode download functions
5217 *
5218 ******************************************************************************/
5219
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005220static void iwl4965_dealloc_ucode_pci(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005221{
Tomas Winkler98c92212008-01-14 17:46:20 -08005222 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_code);
5223 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_data);
5224 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_data_backup);
5225 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_init);
5226 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_init_data);
5227 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_boot);
Zhu Yib481de92007-09-25 17:54:57 -07005228}
5229
5230/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005231 * iwl4965_verify_inst_full - verify runtime uCode image in card vs. host,
Zhu Yib481de92007-09-25 17:54:57 -07005232 * looking at all data.
5233 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005234static int iwl4965_verify_inst_full(struct iwl_priv *priv, __le32 *image,
Ben Cahill9fbab512007-11-29 11:09:47 +08005235 u32 len)
Zhu Yib481de92007-09-25 17:54:57 -07005236{
5237 u32 val;
5238 u32 save_len = len;
5239 int rc = 0;
5240 u32 errcnt;
5241
5242 IWL_DEBUG_INFO("ucode inst image size is %u\n", len);
5243
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005244 rc = iwl4965_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005245 if (rc)
5246 return rc;
5247
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005248 iwl4965_write_direct32(priv, HBUS_TARG_MEM_RADDR, RTC_INST_LOWER_BOUND);
Zhu Yib481de92007-09-25 17:54:57 -07005249
5250 errcnt = 0;
5251 for (; len > 0; len -= sizeof(u32), image++) {
5252 /* read data comes through single port, auto-incr addr */
5253 /* NOTE: Use the debugless read so we don't flood kernel log
5254 * if IWL_DL_IO is set */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005255 val = _iwl4965_read_direct32(priv, HBUS_TARG_MEM_RDAT);
Zhu Yib481de92007-09-25 17:54:57 -07005256 if (val != le32_to_cpu(*image)) {
5257 IWL_ERROR("uCode INST section is invalid at "
5258 "offset 0x%x, is 0x%x, s/b 0x%x\n",
5259 save_len - len, val, le32_to_cpu(*image));
5260 rc = -EIO;
5261 errcnt++;
5262 if (errcnt >= 20)
5263 break;
5264 }
5265 }
5266
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005267 iwl4965_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005268
5269 if (!errcnt)
5270 IWL_DEBUG_INFO
5271 ("ucode image in INSTRUCTION memory is good\n");
5272
5273 return rc;
5274}
5275
5276
5277/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005278 * iwl4965_verify_inst_sparse - verify runtime uCode image in card vs. host,
Zhu Yib481de92007-09-25 17:54:57 -07005279 * using sample data 100 bytes apart. If these sample points are good,
5280 * it's a pretty good bet that everything between them is good, too.
5281 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005282static int iwl4965_verify_inst_sparse(struct iwl_priv *priv, __le32 *image, u32 len)
Zhu Yib481de92007-09-25 17:54:57 -07005283{
5284 u32 val;
5285 int rc = 0;
5286 u32 errcnt = 0;
5287 u32 i;
5288
5289 IWL_DEBUG_INFO("ucode inst image size is %u\n", len);
5290
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005291 rc = iwl4965_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005292 if (rc)
5293 return rc;
5294
5295 for (i = 0; i < len; i += 100, image += 100/sizeof(u32)) {
5296 /* read data comes through single port, auto-incr addr */
5297 /* NOTE: Use the debugless read so we don't flood kernel log
5298 * if IWL_DL_IO is set */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005299 iwl4965_write_direct32(priv, HBUS_TARG_MEM_RADDR,
Zhu Yib481de92007-09-25 17:54:57 -07005300 i + RTC_INST_LOWER_BOUND);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005301 val = _iwl4965_read_direct32(priv, HBUS_TARG_MEM_RDAT);
Zhu Yib481de92007-09-25 17:54:57 -07005302 if (val != le32_to_cpu(*image)) {
5303#if 0 /* Enable this if you want to see details */
5304 IWL_ERROR("uCode INST section is invalid at "
5305 "offset 0x%x, is 0x%x, s/b 0x%x\n",
5306 i, val, *image);
5307#endif
5308 rc = -EIO;
5309 errcnt++;
5310 if (errcnt >= 3)
5311 break;
5312 }
5313 }
5314
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005315 iwl4965_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005316
5317 return rc;
5318}
5319
5320
5321/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005322 * iwl4965_verify_ucode - determine which instruction image is in SRAM,
Zhu Yib481de92007-09-25 17:54:57 -07005323 * and verify its contents
5324 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005325static int iwl4965_verify_ucode(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005326{
5327 __le32 *image;
5328 u32 len;
5329 int rc = 0;
5330
5331 /* Try bootstrap */
5332 image = (__le32 *)priv->ucode_boot.v_addr;
5333 len = priv->ucode_boot.len;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005334 rc = iwl4965_verify_inst_sparse(priv, image, len);
Zhu Yib481de92007-09-25 17:54:57 -07005335 if (rc == 0) {
5336 IWL_DEBUG_INFO("Bootstrap uCode is good in inst SRAM\n");
5337 return 0;
5338 }
5339
5340 /* Try initialize */
5341 image = (__le32 *)priv->ucode_init.v_addr;
5342 len = priv->ucode_init.len;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005343 rc = iwl4965_verify_inst_sparse(priv, image, len);
Zhu Yib481de92007-09-25 17:54:57 -07005344 if (rc == 0) {
5345 IWL_DEBUG_INFO("Initialize uCode is good in inst SRAM\n");
5346 return 0;
5347 }
5348
5349 /* Try runtime/protocol */
5350 image = (__le32 *)priv->ucode_code.v_addr;
5351 len = priv->ucode_code.len;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005352 rc = iwl4965_verify_inst_sparse(priv, image, len);
Zhu Yib481de92007-09-25 17:54:57 -07005353 if (rc == 0) {
5354 IWL_DEBUG_INFO("Runtime uCode is good in inst SRAM\n");
5355 return 0;
5356 }
5357
5358 IWL_ERROR("NO VALID UCODE IMAGE IN INSTRUCTION SRAM!!\n");
5359
Ben Cahill9fbab512007-11-29 11:09:47 +08005360 /* Since nothing seems to match, show first several data entries in
5361 * instruction SRAM, so maybe visual inspection will give a clue.
5362 * Selection of bootstrap image (vs. other images) is arbitrary. */
Zhu Yib481de92007-09-25 17:54:57 -07005363 image = (__le32 *)priv->ucode_boot.v_addr;
5364 len = priv->ucode_boot.len;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005365 rc = iwl4965_verify_inst_full(priv, image, len);
Zhu Yib481de92007-09-25 17:54:57 -07005366
5367 return rc;
5368}
5369
5370
5371/* check contents of special bootstrap uCode SRAM */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005372static int iwl4965_verify_bsm(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005373{
5374 __le32 *image = priv->ucode_boot.v_addr;
5375 u32 len = priv->ucode_boot.len;
5376 u32 reg;
5377 u32 val;
5378
5379 IWL_DEBUG_INFO("Begin verify bsm\n");
5380
5381 /* verify BSM SRAM contents */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005382 val = iwl4965_read_prph(priv, BSM_WR_DWCOUNT_REG);
Zhu Yib481de92007-09-25 17:54:57 -07005383 for (reg = BSM_SRAM_LOWER_BOUND;
5384 reg < BSM_SRAM_LOWER_BOUND + len;
5385 reg += sizeof(u32), image ++) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005386 val = iwl4965_read_prph(priv, reg);
Zhu Yib481de92007-09-25 17:54:57 -07005387 if (val != le32_to_cpu(*image)) {
5388 IWL_ERROR("BSM uCode verification failed at "
5389 "addr 0x%08X+%u (of %u), is 0x%x, s/b 0x%x\n",
5390 BSM_SRAM_LOWER_BOUND,
5391 reg - BSM_SRAM_LOWER_BOUND, len,
5392 val, le32_to_cpu(*image));
5393 return -EIO;
5394 }
5395 }
5396
5397 IWL_DEBUG_INFO("BSM bootstrap uCode image OK\n");
5398
5399 return 0;
5400}
5401
5402/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005403 * iwl4965_load_bsm - Load bootstrap instructions
Zhu Yib481de92007-09-25 17:54:57 -07005404 *
5405 * BSM operation:
5406 *
5407 * The Bootstrap State Machine (BSM) stores a short bootstrap uCode program
5408 * in special SRAM that does not power down during RFKILL. When powering back
5409 * up after power-saving sleeps (or during initial uCode load), the BSM loads
5410 * the bootstrap program into the on-board processor, and starts it.
5411 *
5412 * The bootstrap program loads (via DMA) instructions and data for a new
5413 * program from host DRAM locations indicated by the host driver in the
5414 * BSM_DRAM_* registers. Once the new program is loaded, it starts
5415 * automatically.
5416 *
5417 * When initializing the NIC, the host driver points the BSM to the
5418 * "initialize" uCode image. This uCode sets up some internal data, then
5419 * notifies host via "initialize alive" that it is complete.
5420 *
5421 * The host then replaces the BSM_DRAM_* pointer values to point to the
5422 * normal runtime uCode instructions and a backup uCode data cache buffer
5423 * (filled initially with starting data values for the on-board processor),
5424 * then triggers the "initialize" uCode to load and launch the runtime uCode,
5425 * which begins normal operation.
5426 *
5427 * When doing a power-save shutdown, runtime uCode saves data SRAM into
5428 * the backup data cache in DRAM before SRAM is powered down.
5429 *
5430 * When powering back up, the BSM loads the bootstrap program. This reloads
5431 * the runtime uCode instructions and the backup data cache into SRAM,
5432 * and re-launches the runtime uCode from where it left off.
5433 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005434static int iwl4965_load_bsm(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005435{
5436 __le32 *image = priv->ucode_boot.v_addr;
5437 u32 len = priv->ucode_boot.len;
5438 dma_addr_t pinst;
5439 dma_addr_t pdata;
5440 u32 inst_len;
5441 u32 data_len;
5442 int rc;
5443 int i;
5444 u32 done;
5445 u32 reg_offset;
5446
5447 IWL_DEBUG_INFO("Begin load bsm\n");
5448
5449 /* make sure bootstrap program is no larger than BSM's SRAM size */
5450 if (len > IWL_MAX_BSM_SIZE)
5451 return -EINVAL;
5452
5453 /* Tell bootstrap uCode where to find the "Initialize" uCode
Ben Cahill9fbab512007-11-29 11:09:47 +08005454 * in host DRAM ... host DRAM physical address bits 35:4 for 4965.
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005455 * NOTE: iwl4965_initialize_alive_start() will replace these values,
Zhu Yib481de92007-09-25 17:54:57 -07005456 * after the "initialize" uCode has run, to point to
5457 * runtime/protocol instructions and backup data cache. */
5458 pinst = priv->ucode_init.p_addr >> 4;
5459 pdata = priv->ucode_init_data.p_addr >> 4;
5460 inst_len = priv->ucode_init.len;
5461 data_len = priv->ucode_init_data.len;
5462
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005463 rc = iwl4965_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005464 if (rc)
5465 return rc;
5466
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005467 iwl4965_write_prph(priv, BSM_DRAM_INST_PTR_REG, pinst);
5468 iwl4965_write_prph(priv, BSM_DRAM_DATA_PTR_REG, pdata);
5469 iwl4965_write_prph(priv, BSM_DRAM_INST_BYTECOUNT_REG, inst_len);
5470 iwl4965_write_prph(priv, BSM_DRAM_DATA_BYTECOUNT_REG, data_len);
Zhu Yib481de92007-09-25 17:54:57 -07005471
5472 /* Fill BSM memory with bootstrap instructions */
5473 for (reg_offset = BSM_SRAM_LOWER_BOUND;
5474 reg_offset < BSM_SRAM_LOWER_BOUND + len;
5475 reg_offset += sizeof(u32), image++)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005476 _iwl4965_write_prph(priv, reg_offset,
Zhu Yib481de92007-09-25 17:54:57 -07005477 le32_to_cpu(*image));
5478
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005479 rc = iwl4965_verify_bsm(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005480 if (rc) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005481 iwl4965_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005482 return rc;
5483 }
5484
5485 /* Tell BSM to copy from BSM SRAM into instruction SRAM, when asked */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005486 iwl4965_write_prph(priv, BSM_WR_MEM_SRC_REG, 0x0);
5487 iwl4965_write_prph(priv, BSM_WR_MEM_DST_REG,
Zhu Yib481de92007-09-25 17:54:57 -07005488 RTC_INST_LOWER_BOUND);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005489 iwl4965_write_prph(priv, BSM_WR_DWCOUNT_REG, len / sizeof(u32));
Zhu Yib481de92007-09-25 17:54:57 -07005490
5491 /* Load bootstrap code into instruction SRAM now,
5492 * to prepare to load "initialize" uCode */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005493 iwl4965_write_prph(priv, BSM_WR_CTRL_REG,
Zhu Yib481de92007-09-25 17:54:57 -07005494 BSM_WR_CTRL_REG_BIT_START);
5495
5496 /* Wait for load of bootstrap uCode to finish */
5497 for (i = 0; i < 100; i++) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005498 done = iwl4965_read_prph(priv, BSM_WR_CTRL_REG);
Zhu Yib481de92007-09-25 17:54:57 -07005499 if (!(done & BSM_WR_CTRL_REG_BIT_START))
5500 break;
5501 udelay(10);
5502 }
5503 if (i < 100)
5504 IWL_DEBUG_INFO("BSM write complete, poll %d iterations\n", i);
5505 else {
5506 IWL_ERROR("BSM write did not complete!\n");
5507 return -EIO;
5508 }
5509
5510 /* Enable future boot loads whenever power management unit triggers it
5511 * (e.g. when powering back up after power-save shutdown) */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005512 iwl4965_write_prph(priv, BSM_WR_CTRL_REG,
Zhu Yib481de92007-09-25 17:54:57 -07005513 BSM_WR_CTRL_REG_BIT_START_EN);
5514
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005515 iwl4965_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005516
5517 return 0;
5518}
5519
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005520static void iwl4965_nic_start(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005521{
5522 /* Remove all resets to allow NIC to operate */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005523 iwl4965_write32(priv, CSR_RESET, 0);
Zhu Yib481de92007-09-25 17:54:57 -07005524}
5525
Tomas Winkler90e759d2007-11-29 11:09:41 +08005526
Zhu Yib481de92007-09-25 17:54:57 -07005527/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005528 * iwl4965_read_ucode - Read uCode images from disk file.
Zhu Yib481de92007-09-25 17:54:57 -07005529 *
5530 * Copy into buffers for card to fetch via bus-mastering
5531 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005532static int iwl4965_read_ucode(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005533{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005534 struct iwl4965_ucode *ucode;
Tomas Winkler90e759d2007-11-29 11:09:41 +08005535 int ret;
Zhu Yib481de92007-09-25 17:54:57 -07005536 const struct firmware *ucode_raw;
Tomas Winkler4bf775c2008-03-04 18:09:31 -08005537 const char *name = priv->cfg->fw_name;
Zhu Yib481de92007-09-25 17:54:57 -07005538 u8 *src;
5539 size_t len;
5540 u32 ver, inst_size, data_size, init_size, init_data_size, boot_size;
5541
5542 /* Ask kernel firmware_class module to get the boot firmware off disk.
5543 * request_firmware() is synchronous, file is in memory on return. */
Tomas Winkler90e759d2007-11-29 11:09:41 +08005544 ret = request_firmware(&ucode_raw, name, &priv->pci_dev->dev);
5545 if (ret < 0) {
5546 IWL_ERROR("%s firmware file req failed: Reason %d\n",
5547 name, ret);
Zhu Yib481de92007-09-25 17:54:57 -07005548 goto error;
5549 }
5550
5551 IWL_DEBUG_INFO("Got firmware '%s' file (%zd bytes) from disk\n",
5552 name, ucode_raw->size);
5553
5554 /* Make sure that we got at least our header! */
5555 if (ucode_raw->size < sizeof(*ucode)) {
5556 IWL_ERROR("File size way too small!\n");
Tomas Winkler90e759d2007-11-29 11:09:41 +08005557 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07005558 goto err_release;
5559 }
5560
5561 /* Data from ucode file: header followed by uCode images */
5562 ucode = (void *)ucode_raw->data;
5563
5564 ver = le32_to_cpu(ucode->ver);
5565 inst_size = le32_to_cpu(ucode->inst_size);
5566 data_size = le32_to_cpu(ucode->data_size);
5567 init_size = le32_to_cpu(ucode->init_size);
5568 init_data_size = le32_to_cpu(ucode->init_data_size);
5569 boot_size = le32_to_cpu(ucode->boot_size);
5570
5571 IWL_DEBUG_INFO("f/w package hdr ucode version = 0x%x\n", ver);
5572 IWL_DEBUG_INFO("f/w package hdr runtime inst size = %u\n",
5573 inst_size);
5574 IWL_DEBUG_INFO("f/w package hdr runtime data size = %u\n",
5575 data_size);
5576 IWL_DEBUG_INFO("f/w package hdr init inst size = %u\n",
5577 init_size);
5578 IWL_DEBUG_INFO("f/w package hdr init data size = %u\n",
5579 init_data_size);
5580 IWL_DEBUG_INFO("f/w package hdr boot inst size = %u\n",
5581 boot_size);
5582
5583 /* Verify size of file vs. image size info in file's header */
5584 if (ucode_raw->size < sizeof(*ucode) +
5585 inst_size + data_size + init_size +
5586 init_data_size + boot_size) {
5587
5588 IWL_DEBUG_INFO("uCode file size %d too small\n",
5589 (int)ucode_raw->size);
Tomas Winkler90e759d2007-11-29 11:09:41 +08005590 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07005591 goto err_release;
5592 }
5593
5594 /* Verify that uCode images will fit in card's SRAM */
5595 if (inst_size > IWL_MAX_INST_SIZE) {
Tomas Winkler90e759d2007-11-29 11:09:41 +08005596 IWL_DEBUG_INFO("uCode instr len %d too large to fit in\n",
5597 inst_size);
5598 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07005599 goto err_release;
5600 }
5601
5602 if (data_size > IWL_MAX_DATA_SIZE) {
Tomas Winkler90e759d2007-11-29 11:09:41 +08005603 IWL_DEBUG_INFO("uCode data len %d too large to fit in\n",
5604 data_size);
5605 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07005606 goto err_release;
5607 }
5608 if (init_size > IWL_MAX_INST_SIZE) {
5609 IWL_DEBUG_INFO
Tomas Winkler90e759d2007-11-29 11:09:41 +08005610 ("uCode init instr len %d too large to fit in\n",
5611 init_size);
5612 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07005613 goto err_release;
5614 }
5615 if (init_data_size > IWL_MAX_DATA_SIZE) {
5616 IWL_DEBUG_INFO
Tomas Winkler90e759d2007-11-29 11:09:41 +08005617 ("uCode init data len %d too large to fit in\n",
5618 init_data_size);
5619 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07005620 goto err_release;
5621 }
5622 if (boot_size > IWL_MAX_BSM_SIZE) {
5623 IWL_DEBUG_INFO
Tomas Winkler90e759d2007-11-29 11:09:41 +08005624 ("uCode boot instr len %d too large to fit in\n",
5625 boot_size);
5626 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07005627 goto err_release;
5628 }
5629
5630 /* Allocate ucode buffers for card's bus-master loading ... */
5631
5632 /* Runtime instructions and 2 copies of data:
5633 * 1) unmodified from disk
5634 * 2) backup cache for save/restore during power-downs */
5635 priv->ucode_code.len = inst_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08005636 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_code);
Zhu Yib481de92007-09-25 17:54:57 -07005637
5638 priv->ucode_data.len = data_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08005639 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_data);
Zhu Yib481de92007-09-25 17:54:57 -07005640
5641 priv->ucode_data_backup.len = data_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08005642 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_data_backup);
Zhu Yib481de92007-09-25 17:54:57 -07005643
5644 /* Initialization instructions and data */
Tomas Winkler90e759d2007-11-29 11:09:41 +08005645 if (init_size && init_data_size) {
5646 priv->ucode_init.len = init_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08005647 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_init);
Zhu Yib481de92007-09-25 17:54:57 -07005648
Tomas Winkler90e759d2007-11-29 11:09:41 +08005649 priv->ucode_init_data.len = init_data_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08005650 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_init_data);
Tomas Winkler90e759d2007-11-29 11:09:41 +08005651
5652 if (!priv->ucode_init.v_addr || !priv->ucode_init_data.v_addr)
5653 goto err_pci_alloc;
5654 }
Zhu Yib481de92007-09-25 17:54:57 -07005655
5656 /* Bootstrap (instructions only, no data) */
Tomas Winkler90e759d2007-11-29 11:09:41 +08005657 if (boot_size) {
5658 priv->ucode_boot.len = boot_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08005659 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_boot);
Zhu Yib481de92007-09-25 17:54:57 -07005660
Tomas Winkler90e759d2007-11-29 11:09:41 +08005661 if (!priv->ucode_boot.v_addr)
5662 goto err_pci_alloc;
5663 }
Zhu Yib481de92007-09-25 17:54:57 -07005664
5665 /* Copy images into buffers for card's bus-master reads ... */
5666
5667 /* Runtime instructions (first block of data in file) */
5668 src = &ucode->data[0];
5669 len = priv->ucode_code.len;
Tomas Winkler90e759d2007-11-29 11:09:41 +08005670 IWL_DEBUG_INFO("Copying (but not loading) uCode instr len %Zd\n", len);
Zhu Yib481de92007-09-25 17:54:57 -07005671 memcpy(priv->ucode_code.v_addr, src, len);
5672 IWL_DEBUG_INFO("uCode instr buf vaddr = 0x%p, paddr = 0x%08x\n",
5673 priv->ucode_code.v_addr, (u32)priv->ucode_code.p_addr);
5674
5675 /* Runtime data (2nd block)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005676 * NOTE: Copy into backup buffer will be done in iwl4965_up() */
Zhu Yib481de92007-09-25 17:54:57 -07005677 src = &ucode->data[inst_size];
5678 len = priv->ucode_data.len;
Tomas Winkler90e759d2007-11-29 11:09:41 +08005679 IWL_DEBUG_INFO("Copying (but not loading) uCode data len %Zd\n", len);
Zhu Yib481de92007-09-25 17:54:57 -07005680 memcpy(priv->ucode_data.v_addr, src, len);
5681 memcpy(priv->ucode_data_backup.v_addr, src, len);
5682
5683 /* Initialization instructions (3rd block) */
5684 if (init_size) {
5685 src = &ucode->data[inst_size + data_size];
5686 len = priv->ucode_init.len;
Tomas Winkler90e759d2007-11-29 11:09:41 +08005687 IWL_DEBUG_INFO("Copying (but not loading) init instr len %Zd\n",
5688 len);
Zhu Yib481de92007-09-25 17:54:57 -07005689 memcpy(priv->ucode_init.v_addr, src, len);
5690 }
5691
5692 /* Initialization data (4th block) */
5693 if (init_data_size) {
5694 src = &ucode->data[inst_size + data_size + init_size];
5695 len = priv->ucode_init_data.len;
Tomas Winkler90e759d2007-11-29 11:09:41 +08005696 IWL_DEBUG_INFO("Copying (but not loading) init data len %Zd\n",
5697 len);
Zhu Yib481de92007-09-25 17:54:57 -07005698 memcpy(priv->ucode_init_data.v_addr, src, len);
5699 }
5700
5701 /* Bootstrap instructions (5th block) */
5702 src = &ucode->data[inst_size + data_size + init_size + init_data_size];
5703 len = priv->ucode_boot.len;
Tomas Winkler90e759d2007-11-29 11:09:41 +08005704 IWL_DEBUG_INFO("Copying (but not loading) boot instr len %Zd\n", len);
Zhu Yib481de92007-09-25 17:54:57 -07005705 memcpy(priv->ucode_boot.v_addr, src, len);
5706
5707 /* We have our copies now, allow OS release its copies */
5708 release_firmware(ucode_raw);
5709 return 0;
5710
5711 err_pci_alloc:
5712 IWL_ERROR("failed to allocate pci memory\n");
Tomas Winkler90e759d2007-11-29 11:09:41 +08005713 ret = -ENOMEM;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005714 iwl4965_dealloc_ucode_pci(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005715
5716 err_release:
5717 release_firmware(ucode_raw);
5718
5719 error:
Tomas Winkler90e759d2007-11-29 11:09:41 +08005720 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07005721}
5722
5723
5724/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005725 * iwl4965_set_ucode_ptrs - Set uCode address location
Zhu Yib481de92007-09-25 17:54:57 -07005726 *
5727 * Tell initialization uCode where to find runtime uCode.
5728 *
5729 * BSM registers initially contain pointers to initialization uCode.
5730 * We need to replace them to load runtime uCode inst and data,
5731 * and to save runtime data when powering down.
5732 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005733static int iwl4965_set_ucode_ptrs(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005734{
5735 dma_addr_t pinst;
5736 dma_addr_t pdata;
5737 int rc = 0;
5738 unsigned long flags;
5739
5740 /* bits 35:4 for 4965 */
5741 pinst = priv->ucode_code.p_addr >> 4;
5742 pdata = priv->ucode_data_backup.p_addr >> 4;
5743
5744 spin_lock_irqsave(&priv->lock, flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005745 rc = iwl4965_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005746 if (rc) {
5747 spin_unlock_irqrestore(&priv->lock, flags);
5748 return rc;
5749 }
5750
5751 /* Tell bootstrap uCode where to find image to load */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005752 iwl4965_write_prph(priv, BSM_DRAM_INST_PTR_REG, pinst);
5753 iwl4965_write_prph(priv, BSM_DRAM_DATA_PTR_REG, pdata);
5754 iwl4965_write_prph(priv, BSM_DRAM_DATA_BYTECOUNT_REG,
Zhu Yib481de92007-09-25 17:54:57 -07005755 priv->ucode_data.len);
5756
5757 /* Inst bytecount must be last to set up, bit 31 signals uCode
5758 * that all new ptr/size info is in place */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005759 iwl4965_write_prph(priv, BSM_DRAM_INST_BYTECOUNT_REG,
Zhu Yib481de92007-09-25 17:54:57 -07005760 priv->ucode_code.len | BSM_DRAM_INST_LOAD);
5761
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005762 iwl4965_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005763
5764 spin_unlock_irqrestore(&priv->lock, flags);
5765
5766 IWL_DEBUG_INFO("Runtime uCode pointers are set.\n");
5767
5768 return rc;
5769}
5770
5771/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005772 * iwl4965_init_alive_start - Called after REPLY_ALIVE notification received
Zhu Yib481de92007-09-25 17:54:57 -07005773 *
5774 * Called after REPLY_ALIVE notification received from "initialize" uCode.
5775 *
5776 * The 4965 "initialize" ALIVE reply contains calibration data for:
5777 * Voltage, temperature, and MIMO tx gain correction, now stored in priv
5778 * (3945 does not contain this data).
5779 *
5780 * Tell "initialize" uCode to go ahead and load the runtime uCode.
5781*/
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005782static void iwl4965_init_alive_start(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005783{
5784 /* Check alive response for "valid" sign from uCode */
5785 if (priv->card_alive_init.is_valid != UCODE_VALID_OK) {
5786 /* We had an error bringing up the hardware, so take it
5787 * all the way back down so we can try again */
5788 IWL_DEBUG_INFO("Initialize Alive failed.\n");
5789 goto restart;
5790 }
5791
5792 /* Bootstrap uCode has loaded initialize uCode ... verify inst image.
5793 * This is a paranoid check, because we would not have gotten the
5794 * "initialize" alive if code weren't properly loaded. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005795 if (iwl4965_verify_ucode(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07005796 /* Runtime instruction load was bad;
5797 * take it all the way back down so we can try again */
5798 IWL_DEBUG_INFO("Bad \"initialize\" uCode load.\n");
5799 goto restart;
5800 }
5801
5802 /* Calculate temperature */
5803 priv->temperature = iwl4965_get_temperature(priv);
5804
5805 /* Send pointers to protocol/runtime uCode image ... init code will
5806 * load and launch runtime uCode, which will send us another "Alive"
5807 * notification. */
5808 IWL_DEBUG_INFO("Initialization Alive received.\n");
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005809 if (iwl4965_set_ucode_ptrs(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07005810 /* Runtime instruction load won't happen;
5811 * take it all the way back down so we can try again */
5812 IWL_DEBUG_INFO("Couldn't set up uCode pointers.\n");
5813 goto restart;
5814 }
5815 return;
5816
5817 restart:
5818 queue_work(priv->workqueue, &priv->restart);
5819}
5820
5821
5822/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005823 * iwl4965_alive_start - called after REPLY_ALIVE notification received
Zhu Yib481de92007-09-25 17:54:57 -07005824 * from protocol/runtime uCode (initialization uCode's
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005825 * Alive gets handled by iwl4965_init_alive_start()).
Zhu Yib481de92007-09-25 17:54:57 -07005826 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005827static void iwl4965_alive_start(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005828{
5829 int rc = 0;
5830
5831 IWL_DEBUG_INFO("Runtime Alive received.\n");
5832
5833 if (priv->card_alive.is_valid != UCODE_VALID_OK) {
5834 /* We had an error bringing up the hardware, so take it
5835 * all the way back down so we can try again */
5836 IWL_DEBUG_INFO("Alive failed.\n");
5837 goto restart;
5838 }
5839
5840 /* Initialize uCode has loaded Runtime uCode ... verify inst image.
5841 * This is a paranoid check, because we would not have gotten the
5842 * "runtime" alive if code weren't properly loaded. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005843 if (iwl4965_verify_ucode(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07005844 /* Runtime instruction load was bad;
5845 * take it all the way back down so we can try again */
5846 IWL_DEBUG_INFO("Bad runtime uCode load.\n");
5847 goto restart;
5848 }
5849
Assaf Kraussbf85ea42008-03-14 10:38:49 -07005850 iwlcore_clear_stations_table(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005851
5852 rc = iwl4965_alive_notify(priv);
5853 if (rc) {
5854 IWL_WARNING("Could not complete ALIVE transition [ntf]: %d\n",
5855 rc);
5856 goto restart;
5857 }
5858
Ben Cahill9fbab512007-11-29 11:09:47 +08005859 /* After the ALIVE response, we can send host commands to 4965 uCode */
Zhu Yib481de92007-09-25 17:54:57 -07005860 set_bit(STATUS_ALIVE, &priv->status);
5861
5862 /* Clear out the uCode error bit if it is set */
5863 clear_bit(STATUS_FW_ERROR, &priv->status);
5864
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005865 if (iwl4965_is_rfkill(priv))
Zhu Yib481de92007-09-25 17:54:57 -07005866 return;
5867
Zhu Yi5a66926a2008-01-14 17:46:18 -08005868 ieee80211_start_queues(priv->hw);
Zhu Yib481de92007-09-25 17:54:57 -07005869
5870 priv->active_rate = priv->rates_mask;
5871 priv->active_rate_basic = priv->rates_mask & IWL_BASIC_RATES_MASK;
5872
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005873 iwl4965_send_power_mode(priv, IWL_POWER_LEVEL(priv->power_mode));
Zhu Yib481de92007-09-25 17:54:57 -07005874
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005875 if (iwl4965_is_associated(priv)) {
5876 struct iwl4965_rxon_cmd *active_rxon =
5877 (struct iwl4965_rxon_cmd *)(&priv->active_rxon);
Zhu Yib481de92007-09-25 17:54:57 -07005878
5879 memcpy(&priv->staging_rxon, &priv->active_rxon,
5880 sizeof(priv->staging_rxon));
5881 active_rxon->filter_flags &= ~RXON_FILTER_ASSOC_MSK;
5882 } else {
5883 /* Initialize our rx_config data */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005884 iwl4965_connection_init_rx_config(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005885 memcpy(priv->staging_rxon.node_addr, priv->mac_addr, ETH_ALEN);
5886 }
5887
Ben Cahill9fbab512007-11-29 11:09:47 +08005888 /* Configure Bluetooth device coexistence support */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005889 iwl4965_send_bt_config(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005890
5891 /* Configure the adapter for unassociated operation */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005892 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005893
5894 /* At this point, the NIC is initialized and operational */
5895 priv->notif_missed_beacons = 0;
5896 set_bit(STATUS_READY, &priv->status);
5897
5898 iwl4965_rf_kill_ct_config(priv);
Zhu Yi5a66926a2008-01-14 17:46:18 -08005899
Zhu Yib481de92007-09-25 17:54:57 -07005900 IWL_DEBUG_INFO("ALIVE processing complete.\n");
Zhu Yi5a66926a2008-01-14 17:46:18 -08005901 wake_up_interruptible(&priv->wait_command_queue);
Zhu Yib481de92007-09-25 17:54:57 -07005902
5903 if (priv->error_recovering)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005904 iwl4965_error_recovery(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005905
5906 return;
5907
5908 restart:
5909 queue_work(priv->workqueue, &priv->restart);
5910}
5911
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005912static void iwl4965_cancel_deferred_work(struct iwl_priv *priv);
Zhu Yib481de92007-09-25 17:54:57 -07005913
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005914static void __iwl4965_down(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005915{
5916 unsigned long flags;
5917 int exit_pending = test_bit(STATUS_EXIT_PENDING, &priv->status);
5918 struct ieee80211_conf *conf = NULL;
5919
5920 IWL_DEBUG_INFO(DRV_NAME " is going down\n");
5921
5922 conf = ieee80211_get_hw_conf(priv->hw);
5923
5924 if (!exit_pending)
5925 set_bit(STATUS_EXIT_PENDING, &priv->status);
5926
Assaf Kraussbf85ea42008-03-14 10:38:49 -07005927 iwlcore_clear_stations_table(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005928
5929 /* Unblock any waiting calls */
5930 wake_up_interruptible_all(&priv->wait_command_queue);
5931
Zhu Yib481de92007-09-25 17:54:57 -07005932 /* Wipe out the EXIT_PENDING status bit if we are not actually
5933 * exiting the module */
5934 if (!exit_pending)
5935 clear_bit(STATUS_EXIT_PENDING, &priv->status);
5936
5937 /* stop and reset the on-board processor */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005938 iwl4965_write32(priv, CSR_RESET, CSR_RESET_REG_FLAG_NEVO_RESET);
Zhu Yib481de92007-09-25 17:54:57 -07005939
5940 /* tell the device to stop sending interrupts */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005941 iwl4965_disable_interrupts(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005942
5943 if (priv->mac80211_registered)
5944 ieee80211_stop_queues(priv->hw);
5945
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005946 /* If we have not previously called iwl4965_init() then
Zhu Yib481de92007-09-25 17:54:57 -07005947 * clear all bits but the RF Kill and SUSPEND bits and return */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005948 if (!iwl4965_is_init(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07005949 priv->status = test_bit(STATUS_RF_KILL_HW, &priv->status) <<
5950 STATUS_RF_KILL_HW |
5951 test_bit(STATUS_RF_KILL_SW, &priv->status) <<
5952 STATUS_RF_KILL_SW |
Reinette Chatre97888642008-02-06 11:20:38 -08005953 test_bit(STATUS_GEO_CONFIGURED, &priv->status) <<
5954 STATUS_GEO_CONFIGURED |
Zhu Yib481de92007-09-25 17:54:57 -07005955 test_bit(STATUS_IN_SUSPEND, &priv->status) <<
5956 STATUS_IN_SUSPEND;
5957 goto exit;
5958 }
5959
5960 /* ...otherwise clear out all the status bits but the RF Kill and
5961 * SUSPEND bits and continue taking the NIC down. */
5962 priv->status &= test_bit(STATUS_RF_KILL_HW, &priv->status) <<
5963 STATUS_RF_KILL_HW |
5964 test_bit(STATUS_RF_KILL_SW, &priv->status) <<
5965 STATUS_RF_KILL_SW |
Reinette Chatre97888642008-02-06 11:20:38 -08005966 test_bit(STATUS_GEO_CONFIGURED, &priv->status) <<
5967 STATUS_GEO_CONFIGURED |
Zhu Yib481de92007-09-25 17:54:57 -07005968 test_bit(STATUS_IN_SUSPEND, &priv->status) <<
5969 STATUS_IN_SUSPEND |
5970 test_bit(STATUS_FW_ERROR, &priv->status) <<
5971 STATUS_FW_ERROR;
5972
5973 spin_lock_irqsave(&priv->lock, flags);
Ben Cahill9fbab512007-11-29 11:09:47 +08005974 iwl4965_clear_bit(priv, CSR_GP_CNTRL,
5975 CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
Zhu Yib481de92007-09-25 17:54:57 -07005976 spin_unlock_irqrestore(&priv->lock, flags);
5977
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005978 iwl4965_hw_txq_ctx_stop(priv);
5979 iwl4965_hw_rxq_stop(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005980
5981 spin_lock_irqsave(&priv->lock, flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005982 if (!iwl4965_grab_nic_access(priv)) {
5983 iwl4965_write_prph(priv, APMG_CLK_DIS_REG,
Zhu Yib481de92007-09-25 17:54:57 -07005984 APMG_CLK_VAL_DMA_CLK_RQT);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005985 iwl4965_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005986 }
5987 spin_unlock_irqrestore(&priv->lock, flags);
5988
5989 udelay(5);
5990
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005991 iwl4965_hw_nic_stop_master(priv);
5992 iwl4965_set_bit(priv, CSR_RESET, CSR_RESET_REG_FLAG_SW_RESET);
5993 iwl4965_hw_nic_reset(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005994
5995 exit:
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005996 memset(&priv->card_alive, 0, sizeof(struct iwl4965_alive_resp));
Zhu Yib481de92007-09-25 17:54:57 -07005997
5998 if (priv->ibss_beacon)
5999 dev_kfree_skb(priv->ibss_beacon);
6000 priv->ibss_beacon = NULL;
6001
6002 /* clear out any free frames */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006003 iwl4965_clear_free_frames(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006004}
6005
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006006static void iwl4965_down(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07006007{
6008 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006009 __iwl4965_down(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006010 mutex_unlock(&priv->mutex);
Zhu Yib24d22b2007-12-19 13:59:52 +08006011
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006012 iwl4965_cancel_deferred_work(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006013}
6014
6015#define MAX_HW_RESTARTS 5
6016
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006017static int __iwl4965_up(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07006018{
6019 int rc, i;
Zhu Yib481de92007-09-25 17:54:57 -07006020
6021 if (test_bit(STATUS_EXIT_PENDING, &priv->status)) {
6022 IWL_WARNING("Exit pending; will not bring the NIC up\n");
6023 return -EIO;
6024 }
6025
6026 if (test_bit(STATUS_RF_KILL_SW, &priv->status)) {
6027 IWL_WARNING("Radio disabled by SW RF kill (module "
6028 "parameter)\n");
Zhu Yie655b9f2008-01-24 02:19:38 -08006029 return -ENODEV;
6030 }
6031
Reinette Chatree903fbd2008-01-30 22:05:15 -08006032 if (!priv->ucode_data_backup.v_addr || !priv->ucode_data.v_addr) {
6033 IWL_ERROR("ucode not available for device bringup\n");
6034 return -EIO;
6035 }
6036
Zhu Yie655b9f2008-01-24 02:19:38 -08006037 /* If platform's RF_KILL switch is NOT set to KILL */
6038 if (iwl4965_read32(priv, CSR_GP_CNTRL) &
6039 CSR_GP_CNTRL_REG_FLAG_HW_RF_KILL_SW)
6040 clear_bit(STATUS_RF_KILL_HW, &priv->status);
6041 else {
6042 set_bit(STATUS_RF_KILL_HW, &priv->status);
6043 if (!test_bit(STATUS_IN_SUSPEND, &priv->status)) {
6044 IWL_WARNING("Radio disabled by HW RF Kill switch\n");
6045 return -ENODEV;
6046 }
Zhu Yib481de92007-09-25 17:54:57 -07006047 }
6048
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006049 iwl4965_write32(priv, CSR_INT, 0xFFFFFFFF);
Zhu Yib481de92007-09-25 17:54:57 -07006050
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006051 rc = iwl4965_hw_nic_init(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006052 if (rc) {
6053 IWL_ERROR("Unable to int nic\n");
6054 return rc;
6055 }
6056
6057 /* make sure rfkill handshake bits are cleared */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006058 iwl4965_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
6059 iwl4965_write32(priv, CSR_UCODE_DRV_GP1_CLR,
Zhu Yib481de92007-09-25 17:54:57 -07006060 CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
6061
6062 /* clear (again), then enable host interrupts */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006063 iwl4965_write32(priv, CSR_INT, 0xFFFFFFFF);
6064 iwl4965_enable_interrupts(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006065
6066 /* really make sure rfkill handshake bits are cleared */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006067 iwl4965_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
6068 iwl4965_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
Zhu Yib481de92007-09-25 17:54:57 -07006069
6070 /* Copy original ucode data image from disk into backup cache.
6071 * This will be used to initialize the on-board processor's
6072 * data SRAM for a clean start when the runtime program first loads. */
6073 memcpy(priv->ucode_data_backup.v_addr, priv->ucode_data.v_addr,
Zhu Yi5a66926a2008-01-14 17:46:18 -08006074 priv->ucode_data.len);
Zhu Yib481de92007-09-25 17:54:57 -07006075
Zhu Yie655b9f2008-01-24 02:19:38 -08006076 /* We return success when we resume from suspend and rf_kill is on. */
6077 if (test_bit(STATUS_RF_KILL_HW, &priv->status))
Zhu Yib481de92007-09-25 17:54:57 -07006078 return 0;
Zhu Yib481de92007-09-25 17:54:57 -07006079
6080 for (i = 0; i < MAX_HW_RESTARTS; i++) {
6081
Assaf Kraussbf85ea42008-03-14 10:38:49 -07006082 iwlcore_clear_stations_table(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006083
6084 /* load bootstrap state machine,
6085 * load bootstrap program into processor's memory,
6086 * prepare to load the "initialize" uCode */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006087 rc = iwl4965_load_bsm(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006088
6089 if (rc) {
6090 IWL_ERROR("Unable to set up bootstrap uCode: %d\n", rc);
6091 continue;
6092 }
6093
6094 /* start card; "initialize" will load runtime ucode */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006095 iwl4965_nic_start(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006096
Zhu Yib481de92007-09-25 17:54:57 -07006097 IWL_DEBUG_INFO(DRV_NAME " is coming up\n");
6098
6099 return 0;
6100 }
6101
6102 set_bit(STATUS_EXIT_PENDING, &priv->status);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006103 __iwl4965_down(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006104
6105 /* tried to restart and config the device for as long as our
6106 * patience could withstand */
6107 IWL_ERROR("Unable to initialize device after %d attempts.\n", i);
6108 return -EIO;
6109}
6110
6111
6112/*****************************************************************************
6113 *
6114 * Workqueue callbacks
6115 *
6116 *****************************************************************************/
6117
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006118static void iwl4965_bg_init_alive_start(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07006119{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006120 struct iwl_priv *priv =
6121 container_of(data, struct iwl_priv, init_alive_start.work);
Zhu Yib481de92007-09-25 17:54:57 -07006122
6123 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
6124 return;
6125
6126 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006127 iwl4965_init_alive_start(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006128 mutex_unlock(&priv->mutex);
6129}
6130
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006131static void iwl4965_bg_alive_start(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07006132{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006133 struct iwl_priv *priv =
6134 container_of(data, struct iwl_priv, alive_start.work);
Zhu Yib481de92007-09-25 17:54:57 -07006135
6136 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
6137 return;
6138
6139 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006140 iwl4965_alive_start(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006141 mutex_unlock(&priv->mutex);
6142}
6143
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006144static void iwl4965_bg_rf_kill(struct work_struct *work)
Zhu Yib481de92007-09-25 17:54:57 -07006145{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006146 struct iwl_priv *priv = container_of(work, struct iwl_priv, rf_kill);
Zhu Yib481de92007-09-25 17:54:57 -07006147
6148 wake_up_interruptible(&priv->wait_command_queue);
6149
6150 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
6151 return;
6152
6153 mutex_lock(&priv->mutex);
6154
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006155 if (!iwl4965_is_rfkill(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07006156 IWL_DEBUG(IWL_DL_INFO | IWL_DL_RF_KILL,
6157 "HW and/or SW RF Kill no longer active, restarting "
6158 "device\n");
6159 if (!test_bit(STATUS_EXIT_PENDING, &priv->status))
6160 queue_work(priv->workqueue, &priv->restart);
6161 } else {
6162
6163 if (!test_bit(STATUS_RF_KILL_HW, &priv->status))
6164 IWL_DEBUG_RF_KILL("Can not turn radio back on - "
6165 "disabled by SW switch\n");
6166 else
6167 IWL_WARNING("Radio Frequency Kill Switch is On:\n"
6168 "Kill switch must be turned off for "
6169 "wireless networking to work.\n");
6170 }
6171 mutex_unlock(&priv->mutex);
6172}
6173
6174#define IWL_SCAN_CHECK_WATCHDOG (7 * HZ)
6175
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006176static void iwl4965_bg_scan_check(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07006177{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006178 struct iwl_priv *priv =
6179 container_of(data, struct iwl_priv, scan_check.work);
Zhu Yib481de92007-09-25 17:54:57 -07006180
6181 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
6182 return;
6183
6184 mutex_lock(&priv->mutex);
6185 if (test_bit(STATUS_SCANNING, &priv->status) ||
6186 test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
6187 IWL_DEBUG(IWL_DL_INFO | IWL_DL_SCAN,
6188 "Scan completion watchdog resetting adapter (%dms)\n",
6189 jiffies_to_msecs(IWL_SCAN_CHECK_WATCHDOG));
mabbas052c4b92007-10-25 17:15:43 +08006190
Zhu Yib481de92007-09-25 17:54:57 -07006191 if (!test_bit(STATUS_EXIT_PENDING, &priv->status))
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006192 iwl4965_send_scan_abort(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006193 }
6194 mutex_unlock(&priv->mutex);
6195}
6196
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006197static void iwl4965_bg_request_scan(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07006198{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006199 struct iwl_priv *priv =
6200 container_of(data, struct iwl_priv, request_scan);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006201 struct iwl4965_host_cmd cmd = {
Zhu Yib481de92007-09-25 17:54:57 -07006202 .id = REPLY_SCAN_CMD,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006203 .len = sizeof(struct iwl4965_scan_cmd),
Zhu Yib481de92007-09-25 17:54:57 -07006204 .meta.flags = CMD_SIZE_HUGE,
6205 };
6206 int rc = 0;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006207 struct iwl4965_scan_cmd *scan;
Zhu Yib481de92007-09-25 17:54:57 -07006208 struct ieee80211_conf *conf = NULL;
Tomas Winkler78330fd2008-02-06 02:37:18 +02006209 u16 cmd_len;
Johannes Berg8318d782008-01-24 19:38:38 +01006210 enum ieee80211_band band;
Tomas Winkler78330fd2008-02-06 02:37:18 +02006211 u8 direct_mask;
Zhu Yib481de92007-09-25 17:54:57 -07006212
6213 conf = ieee80211_get_hw_conf(priv->hw);
6214
6215 mutex_lock(&priv->mutex);
6216
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006217 if (!iwl4965_is_ready(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07006218 IWL_WARNING("request scan called when driver not ready.\n");
6219 goto done;
6220 }
6221
6222 /* Make sure the scan wasn't cancelled before this queued work
6223 * was given the chance to run... */
6224 if (!test_bit(STATUS_SCANNING, &priv->status))
6225 goto done;
6226
6227 /* This should never be called or scheduled if there is currently
6228 * a scan active in the hardware. */
6229 if (test_bit(STATUS_SCAN_HW, &priv->status)) {
6230 IWL_DEBUG_INFO("Multiple concurrent scan requests in parallel. "
6231 "Ignoring second request.\n");
6232 rc = -EIO;
6233 goto done;
6234 }
6235
6236 if (test_bit(STATUS_EXIT_PENDING, &priv->status)) {
6237 IWL_DEBUG_SCAN("Aborting scan due to device shutdown\n");
6238 goto done;
6239 }
6240
6241 if (test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
6242 IWL_DEBUG_HC("Scan request while abort pending. Queuing.\n");
6243 goto done;
6244 }
6245
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006246 if (iwl4965_is_rfkill(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07006247 IWL_DEBUG_HC("Aborting scan due to RF Kill activation\n");
6248 goto done;
6249 }
6250
6251 if (!test_bit(STATUS_READY, &priv->status)) {
6252 IWL_DEBUG_HC("Scan request while uninitialized. Queuing.\n");
6253 goto done;
6254 }
6255
6256 if (!priv->scan_bands) {
6257 IWL_DEBUG_HC("Aborting scan due to no requested bands\n");
6258 goto done;
6259 }
6260
6261 if (!priv->scan) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006262 priv->scan = kmalloc(sizeof(struct iwl4965_scan_cmd) +
Zhu Yib481de92007-09-25 17:54:57 -07006263 IWL_MAX_SCAN_SIZE, GFP_KERNEL);
6264 if (!priv->scan) {
6265 rc = -ENOMEM;
6266 goto done;
6267 }
6268 }
6269 scan = priv->scan;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006270 memset(scan, 0, sizeof(struct iwl4965_scan_cmd) + IWL_MAX_SCAN_SIZE);
Zhu Yib481de92007-09-25 17:54:57 -07006271
6272 scan->quiet_plcp_th = IWL_PLCP_QUIET_THRESH;
6273 scan->quiet_time = IWL_ACTIVE_QUIET_TIME;
6274
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006275 if (iwl4965_is_associated(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07006276 u16 interval = 0;
6277 u32 extra;
6278 u32 suspend_time = 100;
6279 u32 scan_suspend_time = 100;
6280 unsigned long flags;
6281
6282 IWL_DEBUG_INFO("Scanning while associated...\n");
6283
6284 spin_lock_irqsave(&priv->lock, flags);
6285 interval = priv->beacon_int;
6286 spin_unlock_irqrestore(&priv->lock, flags);
6287
6288 scan->suspend_time = 0;
mabbas052c4b92007-10-25 17:15:43 +08006289 scan->max_out_time = cpu_to_le32(200 * 1024);
Zhu Yib481de92007-09-25 17:54:57 -07006290 if (!interval)
6291 interval = suspend_time;
6292
6293 extra = (suspend_time / interval) << 22;
6294 scan_suspend_time = (extra |
6295 ((suspend_time % interval) * 1024));
6296 scan->suspend_time = cpu_to_le32(scan_suspend_time);
6297 IWL_DEBUG_SCAN("suspend_time 0x%X beacon interval %d\n",
6298 scan_suspend_time, interval);
6299 }
6300
6301 /* We should add the ability for user to lock to PASSIVE ONLY */
6302 if (priv->one_direct_scan) {
6303 IWL_DEBUG_SCAN
6304 ("Kicking off one direct scan for '%s'\n",
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006305 iwl4965_escape_essid(priv->direct_ssid,
Zhu Yib481de92007-09-25 17:54:57 -07006306 priv->direct_ssid_len));
6307 scan->direct_scan[0].id = WLAN_EID_SSID;
6308 scan->direct_scan[0].len = priv->direct_ssid_len;
6309 memcpy(scan->direct_scan[0].ssid,
6310 priv->direct_ssid, priv->direct_ssid_len);
6311 direct_mask = 1;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006312 } else if (!iwl4965_is_associated(priv) && priv->essid_len) {
Zhu Yib481de92007-09-25 17:54:57 -07006313 scan->direct_scan[0].id = WLAN_EID_SSID;
6314 scan->direct_scan[0].len = priv->essid_len;
6315 memcpy(scan->direct_scan[0].ssid, priv->essid, priv->essid_len);
6316 direct_mask = 1;
6317 } else
6318 direct_mask = 0;
6319
Zhu Yib481de92007-09-25 17:54:57 -07006320 scan->tx_cmd.tx_flags = TX_CMD_FLG_SEQ_CTL_MSK;
6321 scan->tx_cmd.sta_id = priv->hw_setting.bcast_sta_id;
6322 scan->tx_cmd.stop_time.life_time = TX_CMD_LIFE_TIME_INFINITE;
6323
Zhu Yib481de92007-09-25 17:54:57 -07006324
6325 switch (priv->scan_bands) {
6326 case 2:
6327 scan->flags = RXON_FLG_BAND_24G_MSK | RXON_FLG_AUTO_DETECT_MSK;
6328 scan->tx_cmd.rate_n_flags =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006329 iwl4965_hw_set_rate_n_flags(IWL_RATE_1M_PLCP,
Zhu Yib481de92007-09-25 17:54:57 -07006330 RATE_MCS_ANT_B_MSK|RATE_MCS_CCK_MSK);
6331
6332 scan->good_CRC_th = 0;
Johannes Berg8318d782008-01-24 19:38:38 +01006333 band = IEEE80211_BAND_2GHZ;
Zhu Yib481de92007-09-25 17:54:57 -07006334 break;
6335
6336 case 1:
6337 scan->tx_cmd.rate_n_flags =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006338 iwl4965_hw_set_rate_n_flags(IWL_RATE_6M_PLCP,
Zhu Yib481de92007-09-25 17:54:57 -07006339 RATE_MCS_ANT_B_MSK);
6340 scan->good_CRC_th = IWL_GOOD_CRC_TH;
Johannes Berg8318d782008-01-24 19:38:38 +01006341 band = IEEE80211_BAND_5GHZ;
Zhu Yib481de92007-09-25 17:54:57 -07006342 break;
6343
6344 default:
6345 IWL_WARNING("Invalid scan band count\n");
6346 goto done;
6347 }
6348
Tomas Winkler78330fd2008-02-06 02:37:18 +02006349 /* We don't build a direct scan probe request; the uCode will do
6350 * that based on the direct_mask added to each channel entry */
6351 cmd_len = iwl4965_fill_probe_req(priv, band,
6352 (struct ieee80211_mgmt *)scan->data,
6353 IWL_MAX_SCAN_SIZE - sizeof(*scan), 0);
6354
6355 scan->tx_cmd.len = cpu_to_le16(cmd_len);
Zhu Yib481de92007-09-25 17:54:57 -07006356 /* select Rx chains */
6357
6358 /* Force use of chains B and C (0x6) for scan Rx.
6359 * Avoid A (0x1) because of its off-channel reception on A-band.
6360 * MIMO is not used here, but value is required to make uCode happy. */
6361 scan->rx_chain = RXON_RX_CHAIN_DRIVER_FORCE_MSK |
6362 cpu_to_le16((0x7 << RXON_RX_CHAIN_VALID_POS) |
6363 (0x6 << RXON_RX_CHAIN_FORCE_SEL_POS) |
6364 (0x7 << RXON_RX_CHAIN_FORCE_MIMO_SEL_POS));
6365
6366 if (priv->iw_mode == IEEE80211_IF_TYPE_MNTR)
6367 scan->filter_flags = RXON_FILTER_PROMISC_MSK;
6368
Reinette Chatre26c0f032008-03-11 16:17:15 -07006369 if (direct_mask) {
Zhu Yib481de92007-09-25 17:54:57 -07006370 IWL_DEBUG_SCAN
6371 ("Initiating direct scan for %s.\n",
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006372 iwl4965_escape_essid(priv->essid, priv->essid_len));
Reinette Chatre26c0f032008-03-11 16:17:15 -07006373 scan->channel_count =
6374 iwl4965_get_channels_for_scan(
6375 priv, band, 1, /* active */
6376 direct_mask,
6377 (void *)&scan->data[le16_to_cpu(scan->tx_cmd.len)]);
6378 } else {
Zhu Yib481de92007-09-25 17:54:57 -07006379 IWL_DEBUG_SCAN("Initiating indirect scan.\n");
Reinette Chatre26c0f032008-03-11 16:17:15 -07006380 scan->channel_count =
6381 iwl4965_get_channels_for_scan(
6382 priv, band, 0, /* passive */
6383 direct_mask,
6384 (void *)&scan->data[le16_to_cpu(scan->tx_cmd.len)]);
6385 }
Zhu Yib481de92007-09-25 17:54:57 -07006386
6387 cmd.len += le16_to_cpu(scan->tx_cmd.len) +
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006388 scan->channel_count * sizeof(struct iwl4965_scan_channel);
Zhu Yib481de92007-09-25 17:54:57 -07006389 cmd.data = scan;
6390 scan->len = cpu_to_le16(cmd.len);
6391
6392 set_bit(STATUS_SCAN_HW, &priv->status);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006393 rc = iwl4965_send_cmd_sync(priv, &cmd);
Zhu Yib481de92007-09-25 17:54:57 -07006394 if (rc)
6395 goto done;
6396
6397 queue_delayed_work(priv->workqueue, &priv->scan_check,
6398 IWL_SCAN_CHECK_WATCHDOG);
6399
6400 mutex_unlock(&priv->mutex);
6401 return;
6402
6403 done:
Ian Schram01ebd062007-10-25 17:15:22 +08006404 /* inform mac80211 scan aborted */
Zhu Yib481de92007-09-25 17:54:57 -07006405 queue_work(priv->workqueue, &priv->scan_completed);
6406 mutex_unlock(&priv->mutex);
6407}
6408
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006409static void iwl4965_bg_up(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07006410{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006411 struct iwl_priv *priv = container_of(data, struct iwl_priv, up);
Zhu Yib481de92007-09-25 17:54:57 -07006412
6413 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
6414 return;
6415
6416 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006417 __iwl4965_up(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006418 mutex_unlock(&priv->mutex);
6419}
6420
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006421static void iwl4965_bg_restart(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07006422{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006423 struct iwl_priv *priv = container_of(data, struct iwl_priv, restart);
Zhu Yib481de92007-09-25 17:54:57 -07006424
6425 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
6426 return;
6427
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006428 iwl4965_down(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006429 queue_work(priv->workqueue, &priv->up);
6430}
6431
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006432static void iwl4965_bg_rx_replenish(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07006433{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006434 struct iwl_priv *priv =
6435 container_of(data, struct iwl_priv, rx_replenish);
Zhu Yib481de92007-09-25 17:54:57 -07006436
6437 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
6438 return;
6439
6440 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006441 iwl4965_rx_replenish(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006442 mutex_unlock(&priv->mutex);
6443}
6444
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08006445#define IWL_DELAY_NEXT_SCAN (HZ*2)
6446
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006447static void iwl4965_bg_post_associate(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07006448{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006449 struct iwl_priv *priv = container_of(data, struct iwl_priv,
Zhu Yib481de92007-09-25 17:54:57 -07006450 post_associate.work);
6451
6452 int rc = 0;
6453 struct ieee80211_conf *conf = NULL;
Joe Perches0795af52007-10-03 17:59:30 -07006454 DECLARE_MAC_BUF(mac);
Zhu Yib481de92007-09-25 17:54:57 -07006455
6456 if (priv->iw_mode == IEEE80211_IF_TYPE_AP) {
6457 IWL_ERROR("%s Should not be called in AP mode\n", __FUNCTION__);
6458 return;
6459 }
6460
Joe Perches0795af52007-10-03 17:59:30 -07006461 IWL_DEBUG_ASSOC("Associated as %d to: %s\n",
6462 priv->assoc_id,
6463 print_mac(mac, priv->active_rxon.bssid_addr));
Zhu Yib481de92007-09-25 17:54:57 -07006464
6465
6466 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
6467 return;
6468
6469 mutex_lock(&priv->mutex);
6470
Johannes Berg32bfd352007-12-19 01:31:26 +01006471 if (!priv->vif || !priv->is_open) {
Mohamed Abbas948c1712007-10-25 17:15:45 +08006472 mutex_unlock(&priv->mutex);
6473 return;
6474 }
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006475 iwl4965_scan_cancel_timeout(priv, 200);
mabbas052c4b92007-10-25 17:15:43 +08006476
Zhu Yib481de92007-09-25 17:54:57 -07006477 conf = ieee80211_get_hw_conf(priv->hw);
6478
6479 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006480 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006481
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006482 memset(&priv->rxon_timing, 0, sizeof(struct iwl4965_rxon_time_cmd));
6483 iwl4965_setup_rxon_timing(priv);
6484 rc = iwl4965_send_cmd_pdu(priv, REPLY_RXON_TIMING,
Zhu Yib481de92007-09-25 17:54:57 -07006485 sizeof(priv->rxon_timing), &priv->rxon_timing);
6486 if (rc)
6487 IWL_WARNING("REPLY_RXON_TIMING failed - "
6488 "Attempting to continue.\n");
6489
6490 priv->staging_rxon.filter_flags |= RXON_FILTER_ASSOC_MSK;
6491
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08006492#ifdef CONFIG_IWL4965_HT
Ron Rindjunskyfd105e72007-11-26 16:14:39 +02006493 if (priv->current_ht_config.is_ht)
6494 iwl4965_set_rxon_ht(priv, &priv->current_ht_config);
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08006495#endif /* CONFIG_IWL4965_HT*/
Zhu Yib481de92007-09-25 17:54:57 -07006496 iwl4965_set_rxon_chain(priv);
6497 priv->staging_rxon.assoc_id = cpu_to_le16(priv->assoc_id);
6498
6499 IWL_DEBUG_ASSOC("assoc id %d beacon interval %d\n",
6500 priv->assoc_id, priv->beacon_int);
6501
6502 if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_PREAMBLE)
6503 priv->staging_rxon.flags |= RXON_FLG_SHORT_PREAMBLE_MSK;
6504 else
6505 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_PREAMBLE_MSK;
6506
6507 if (priv->staging_rxon.flags & RXON_FLG_BAND_24G_MSK) {
6508 if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_SLOT_TIME)
6509 priv->staging_rxon.flags |= RXON_FLG_SHORT_SLOT_MSK;
6510 else
6511 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
6512
6513 if (priv->iw_mode == IEEE80211_IF_TYPE_IBSS)
6514 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
6515
6516 }
6517
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006518 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006519
6520 switch (priv->iw_mode) {
6521 case IEEE80211_IF_TYPE_STA:
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006522 iwl4965_rate_scale_init(priv->hw, IWL_AP_ID);
Zhu Yib481de92007-09-25 17:54:57 -07006523 break;
6524
6525 case IEEE80211_IF_TYPE_IBSS:
6526
6527 /* clear out the station table */
Assaf Kraussbf85ea42008-03-14 10:38:49 -07006528 iwlcore_clear_stations_table(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006529
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006530 iwl4965_rxon_add_station(priv, iwl4965_broadcast_addr, 0);
6531 iwl4965_rxon_add_station(priv, priv->bssid, 0);
6532 iwl4965_rate_scale_init(priv->hw, IWL_STA_ID);
6533 iwl4965_send_beacon_cmd(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006534
6535 break;
6536
6537 default:
6538 IWL_ERROR("%s Should not be called in %d mode\n",
6539 __FUNCTION__, priv->iw_mode);
6540 break;
6541 }
6542
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006543 iwl4965_sequence_reset(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006544
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08006545#ifdef CONFIG_IWL4965_SENSITIVITY
Zhu Yib481de92007-09-25 17:54:57 -07006546 /* Enable Rx differential gain and sensitivity calibrations */
6547 iwl4965_chain_noise_reset(priv);
6548 priv->start_calib = 1;
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08006549#endif /* CONFIG_IWL4965_SENSITIVITY */
Zhu Yib481de92007-09-25 17:54:57 -07006550
6551 if (priv->iw_mode == IEEE80211_IF_TYPE_IBSS)
6552 priv->assoc_station_added = 1;
6553
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006554 iwl4965_activate_qos(priv, 0);
Ron Rindjunsky292ae172008-02-06 11:20:39 -08006555
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08006556 /* we have just associated, don't start scan too early */
6557 priv->next_scan_jiffies = jiffies + IWL_DELAY_NEXT_SCAN;
Zhu Yib481de92007-09-25 17:54:57 -07006558 mutex_unlock(&priv->mutex);
6559}
6560
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006561static void iwl4965_bg_abort_scan(struct work_struct *work)
Zhu Yib481de92007-09-25 17:54:57 -07006562{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006563 struct iwl_priv *priv = container_of(work, struct iwl_priv, abort_scan);
Zhu Yib481de92007-09-25 17:54:57 -07006564
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006565 if (!iwl4965_is_ready(priv))
Zhu Yib481de92007-09-25 17:54:57 -07006566 return;
6567
6568 mutex_lock(&priv->mutex);
6569
6570 set_bit(STATUS_SCAN_ABORTING, &priv->status);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006571 iwl4965_send_scan_abort(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006572
6573 mutex_unlock(&priv->mutex);
6574}
6575
Zhu Yi76bb77e2007-11-22 10:53:22 +08006576static int iwl4965_mac_config(struct ieee80211_hw *hw, struct ieee80211_conf *conf);
6577
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006578static void iwl4965_bg_scan_completed(struct work_struct *work)
Zhu Yib481de92007-09-25 17:54:57 -07006579{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006580 struct iwl_priv *priv =
6581 container_of(work, struct iwl_priv, scan_completed);
Zhu Yib481de92007-09-25 17:54:57 -07006582
6583 IWL_DEBUG(IWL_DL_INFO | IWL_DL_SCAN, "SCAN complete scan\n");
6584
6585 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
6586 return;
6587
Zhu Yia0646472007-12-20 14:10:01 +08006588 if (test_bit(STATUS_CONF_PENDING, &priv->status))
6589 iwl4965_mac_config(priv->hw, ieee80211_get_hw_conf(priv->hw));
Zhu Yi76bb77e2007-11-22 10:53:22 +08006590
Zhu Yib481de92007-09-25 17:54:57 -07006591 ieee80211_scan_completed(priv->hw);
6592
6593 /* Since setting the TXPOWER may have been deferred while
6594 * performing the scan, fire one off */
6595 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006596 iwl4965_hw_reg_send_txpower(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006597 mutex_unlock(&priv->mutex);
6598}
6599
6600/*****************************************************************************
6601 *
6602 * mac80211 entry point functions
6603 *
6604 *****************************************************************************/
6605
Zhu Yi5a66926a2008-01-14 17:46:18 -08006606#define UCODE_READY_TIMEOUT (2 * HZ)
6607
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006608static int iwl4965_mac_start(struct ieee80211_hw *hw)
Zhu Yib481de92007-09-25 17:54:57 -07006609{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006610 struct iwl_priv *priv = hw->priv;
Zhu Yi5a66926a2008-01-14 17:46:18 -08006611 int ret;
Zhu Yib481de92007-09-25 17:54:57 -07006612
6613 IWL_DEBUG_MAC80211("enter\n");
6614
Zhu Yi5a66926a2008-01-14 17:46:18 -08006615 if (pci_enable_device(priv->pci_dev)) {
6616 IWL_ERROR("Fail to pci_enable_device\n");
6617 return -ENODEV;
6618 }
6619 pci_restore_state(priv->pci_dev);
6620 pci_enable_msi(priv->pci_dev);
6621
6622 ret = request_irq(priv->pci_dev->irq, iwl4965_isr, IRQF_SHARED,
6623 DRV_NAME, priv);
6624 if (ret) {
6625 IWL_ERROR("Error allocating IRQ %d\n", priv->pci_dev->irq);
6626 goto out_disable_msi;
6627 }
6628
Zhu Yib481de92007-09-25 17:54:57 -07006629 /* we should be verifying the device is ready to be opened */
6630 mutex_lock(&priv->mutex);
6631
Zhu Yi5a66926a2008-01-14 17:46:18 -08006632 memset(&priv->staging_rxon, 0, sizeof(struct iwl4965_rxon_cmd));
6633 /* fetch ucode file from disk, alloc and copy to bus-master buffers ...
6634 * ucode filename and max sizes are card-specific. */
6635
6636 if (!priv->ucode_code.len) {
6637 ret = iwl4965_read_ucode(priv);
6638 if (ret) {
6639 IWL_ERROR("Could not read microcode: %d\n", ret);
6640 mutex_unlock(&priv->mutex);
6641 goto out_release_irq;
6642 }
6643 }
6644
Zhu Yie655b9f2008-01-24 02:19:38 -08006645 ret = __iwl4965_up(priv);
Zhu Yi5a66926a2008-01-14 17:46:18 -08006646
Zhu Yib481de92007-09-25 17:54:57 -07006647 mutex_unlock(&priv->mutex);
Zhu Yi5a66926a2008-01-14 17:46:18 -08006648
Zhu Yie655b9f2008-01-24 02:19:38 -08006649 if (ret)
6650 goto out_release_irq;
6651
6652 IWL_DEBUG_INFO("Start UP work done.\n");
6653
6654 if (test_bit(STATUS_IN_SUSPEND, &priv->status))
6655 return 0;
6656
Zhu Yi5a66926a2008-01-14 17:46:18 -08006657 /* Wait for START_ALIVE from ucode. Otherwise callbacks from
6658 * mac80211 will not be run successfully. */
6659 ret = wait_event_interruptible_timeout(priv->wait_command_queue,
6660 test_bit(STATUS_READY, &priv->status),
6661 UCODE_READY_TIMEOUT);
6662 if (!ret) {
6663 if (!test_bit(STATUS_READY, &priv->status)) {
6664 IWL_ERROR("Wait for START_ALIVE timeout after %dms.\n",
6665 jiffies_to_msecs(UCODE_READY_TIMEOUT));
6666 ret = -ETIMEDOUT;
6667 goto out_release_irq;
6668 }
6669 }
6670
Zhu Yie655b9f2008-01-24 02:19:38 -08006671 priv->is_open = 1;
Zhu Yib481de92007-09-25 17:54:57 -07006672 IWL_DEBUG_MAC80211("leave\n");
6673 return 0;
Zhu Yi5a66926a2008-01-14 17:46:18 -08006674
6675out_release_irq:
6676 free_irq(priv->pci_dev->irq, priv);
6677out_disable_msi:
6678 pci_disable_msi(priv->pci_dev);
Zhu Yie655b9f2008-01-24 02:19:38 -08006679 pci_disable_device(priv->pci_dev);
6680 priv->is_open = 0;
6681 IWL_DEBUG_MAC80211("leave - failed\n");
Zhu Yi5a66926a2008-01-14 17:46:18 -08006682 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07006683}
6684
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006685static void iwl4965_mac_stop(struct ieee80211_hw *hw)
Zhu Yib481de92007-09-25 17:54:57 -07006686{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006687 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07006688
6689 IWL_DEBUG_MAC80211("enter\n");
Mohamed Abbas948c1712007-10-25 17:15:45 +08006690
Zhu Yie655b9f2008-01-24 02:19:38 -08006691 if (!priv->is_open) {
6692 IWL_DEBUG_MAC80211("leave - skip\n");
6693 return;
6694 }
6695
Zhu Yib481de92007-09-25 17:54:57 -07006696 priv->is_open = 0;
Zhu Yi5a66926a2008-01-14 17:46:18 -08006697
6698 if (iwl4965_is_ready_rf(priv)) {
Zhu Yie655b9f2008-01-24 02:19:38 -08006699 /* stop mac, cancel any scan request and clear
6700 * RXON_FILTER_ASSOC_MSK BIT
6701 */
Zhu Yi5a66926a2008-01-14 17:46:18 -08006702 mutex_lock(&priv->mutex);
6703 iwl4965_scan_cancel_timeout(priv, 100);
6704 cancel_delayed_work(&priv->post_associate);
Mohamed Abbasfde35712007-11-29 11:10:15 +08006705 mutex_unlock(&priv->mutex);
Mohamed Abbasfde35712007-11-29 11:10:15 +08006706 }
6707
Zhu Yi5a66926a2008-01-14 17:46:18 -08006708 iwl4965_down(priv);
6709
6710 flush_workqueue(priv->workqueue);
6711 free_irq(priv->pci_dev->irq, priv);
6712 pci_disable_msi(priv->pci_dev);
6713 pci_save_state(priv->pci_dev);
6714 pci_disable_device(priv->pci_dev);
Mohamed Abbas948c1712007-10-25 17:15:45 +08006715
Zhu Yib481de92007-09-25 17:54:57 -07006716 IWL_DEBUG_MAC80211("leave\n");
Zhu Yib481de92007-09-25 17:54:57 -07006717}
6718
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006719static int iwl4965_mac_tx(struct ieee80211_hw *hw, struct sk_buff *skb,
Zhu Yib481de92007-09-25 17:54:57 -07006720 struct ieee80211_tx_control *ctl)
6721{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006722 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07006723
6724 IWL_DEBUG_MAC80211("enter\n");
6725
6726 if (priv->iw_mode == IEEE80211_IF_TYPE_MNTR) {
6727 IWL_DEBUG_MAC80211("leave - monitor\n");
6728 return -1;
6729 }
6730
6731 IWL_DEBUG_TX("dev->xmit(%d bytes) at rate 0x%02x\n", skb->len,
Johannes Berg8318d782008-01-24 19:38:38 +01006732 ctl->tx_rate->bitrate);
Zhu Yib481de92007-09-25 17:54:57 -07006733
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006734 if (iwl4965_tx_skb(priv, skb, ctl))
Zhu Yib481de92007-09-25 17:54:57 -07006735 dev_kfree_skb_any(skb);
6736
6737 IWL_DEBUG_MAC80211("leave\n");
6738 return 0;
6739}
6740
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006741static int iwl4965_mac_add_interface(struct ieee80211_hw *hw,
Zhu Yib481de92007-09-25 17:54:57 -07006742 struct ieee80211_if_init_conf *conf)
6743{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006744 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07006745 unsigned long flags;
Joe Perches0795af52007-10-03 17:59:30 -07006746 DECLARE_MAC_BUF(mac);
Zhu Yib481de92007-09-25 17:54:57 -07006747
Johannes Berg32bfd352007-12-19 01:31:26 +01006748 IWL_DEBUG_MAC80211("enter: type %d\n", conf->type);
Zhu Yib481de92007-09-25 17:54:57 -07006749
Johannes Berg32bfd352007-12-19 01:31:26 +01006750 if (priv->vif) {
6751 IWL_DEBUG_MAC80211("leave - vif != NULL\n");
Reinette Chatre75849d22008-01-23 10:15:17 -08006752 return -EOPNOTSUPP;
Zhu Yib481de92007-09-25 17:54:57 -07006753 }
6754
6755 spin_lock_irqsave(&priv->lock, flags);
Johannes Berg32bfd352007-12-19 01:31:26 +01006756 priv->vif = conf->vif;
Zhu Yib481de92007-09-25 17:54:57 -07006757
6758 spin_unlock_irqrestore(&priv->lock, flags);
6759
6760 mutex_lock(&priv->mutex);
Tomas Winkler864792e2007-11-27 21:00:52 +02006761
6762 if (conf->mac_addr) {
6763 IWL_DEBUG_MAC80211("Set %s\n", print_mac(mac, conf->mac_addr));
6764 memcpy(priv->mac_addr, conf->mac_addr, ETH_ALEN);
6765 }
Zhu Yib481de92007-09-25 17:54:57 -07006766
Zhu Yi5a66926a2008-01-14 17:46:18 -08006767 if (iwl4965_is_ready(priv))
6768 iwl4965_set_mode(priv, conf->type);
6769
Zhu Yib481de92007-09-25 17:54:57 -07006770 mutex_unlock(&priv->mutex);
6771
Zhu Yi5a66926a2008-01-14 17:46:18 -08006772 IWL_DEBUG_MAC80211("leave\n");
Zhu Yib481de92007-09-25 17:54:57 -07006773 return 0;
6774}
6775
6776/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006777 * iwl4965_mac_config - mac80211 config callback
Zhu Yib481de92007-09-25 17:54:57 -07006778 *
6779 * We ignore conf->flags & IEEE80211_CONF_SHORT_SLOT_TIME since it seems to
6780 * be set inappropriately and the driver currently sets the hardware up to
6781 * use it whenever needed.
6782 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006783static int iwl4965_mac_config(struct ieee80211_hw *hw, struct ieee80211_conf *conf)
Zhu Yib481de92007-09-25 17:54:57 -07006784{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006785 struct iwl_priv *priv = hw->priv;
Assaf Kraussbf85ea42008-03-14 10:38:49 -07006786 const struct iwl_channel_info *ch_info;
Zhu Yib481de92007-09-25 17:54:57 -07006787 unsigned long flags;
Zhu Yi76bb77e2007-11-22 10:53:22 +08006788 int ret = 0;
Zhu Yib481de92007-09-25 17:54:57 -07006789
6790 mutex_lock(&priv->mutex);
Johannes Berg8318d782008-01-24 19:38:38 +01006791 IWL_DEBUG_MAC80211("enter to channel %d\n", conf->channel->hw_value);
Zhu Yib481de92007-09-25 17:54:57 -07006792
Zhu Yi12342c42007-12-20 11:27:32 +08006793 priv->add_radiotap = !!(conf->flags & IEEE80211_CONF_RADIOTAP);
6794
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006795 if (!iwl4965_is_ready(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07006796 IWL_DEBUG_MAC80211("leave - not ready\n");
Zhu Yi76bb77e2007-11-22 10:53:22 +08006797 ret = -EIO;
6798 goto out;
Zhu Yib481de92007-09-25 17:54:57 -07006799 }
6800
Assaf Krauss00acbc92008-03-14 10:38:47 -07006801 if (unlikely(!iwl4965_mod_params.disable_hw_scan &&
Zhu Yib481de92007-09-25 17:54:57 -07006802 test_bit(STATUS_SCANNING, &priv->status))) {
Zhu Yia0646472007-12-20 14:10:01 +08006803 IWL_DEBUG_MAC80211("leave - scanning\n");
6804 set_bit(STATUS_CONF_PENDING, &priv->status);
Zhu Yib481de92007-09-25 17:54:57 -07006805 mutex_unlock(&priv->mutex);
Zhu Yia0646472007-12-20 14:10:01 +08006806 return 0;
Zhu Yib481de92007-09-25 17:54:57 -07006807 }
6808
6809 spin_lock_irqsave(&priv->lock, flags);
6810
Johannes Berg8318d782008-01-24 19:38:38 +01006811 ch_info = iwl4965_get_channel_info(priv, conf->channel->band,
6812 ieee80211_frequency_to_channel(conf->channel->center_freq));
Zhu Yib481de92007-09-25 17:54:57 -07006813 if (!is_channel_valid(ch_info)) {
Zhu Yib481de92007-09-25 17:54:57 -07006814 IWL_DEBUG_MAC80211("leave - invalid channel\n");
6815 spin_unlock_irqrestore(&priv->lock, flags);
Zhu Yi76bb77e2007-11-22 10:53:22 +08006816 ret = -EINVAL;
6817 goto out;
Zhu Yib481de92007-09-25 17:54:57 -07006818 }
6819
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08006820#ifdef CONFIG_IWL4965_HT
Tomas Winkler78330fd2008-02-06 02:37:18 +02006821 /* if we are switching from ht to 2.4 clear flags
Zhu Yib481de92007-09-25 17:54:57 -07006822 * from any ht related info since 2.4 does not
6823 * support ht */
Tomas Winkler78330fd2008-02-06 02:37:18 +02006824 if ((le16_to_cpu(priv->staging_rxon.channel) != conf->channel->hw_value)
Zhu Yib481de92007-09-25 17:54:57 -07006825#ifdef IEEE80211_CONF_CHANNEL_SWITCH
6826 && !(conf->flags & IEEE80211_CONF_CHANNEL_SWITCH)
6827#endif
6828 )
6829 priv->staging_rxon.flags = 0;
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08006830#endif /* CONFIG_IWL4965_HT */
Zhu Yib481de92007-09-25 17:54:57 -07006831
Assaf Kraussbf85ea42008-03-14 10:38:49 -07006832 iwlcore_set_rxon_channel(priv, conf->channel->band,
Johannes Berg8318d782008-01-24 19:38:38 +01006833 ieee80211_frequency_to_channel(conf->channel->center_freq));
Zhu Yib481de92007-09-25 17:54:57 -07006834
Johannes Berg8318d782008-01-24 19:38:38 +01006835 iwl4965_set_flags_for_phymode(priv, conf->channel->band);
Zhu Yib481de92007-09-25 17:54:57 -07006836
6837 /* The list of supported rates and rate mask can be different
Johannes Berg8318d782008-01-24 19:38:38 +01006838 * for each band; since the band may have changed, reset
Zhu Yib481de92007-09-25 17:54:57 -07006839 * the rate mask to what mac80211 lists */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006840 iwl4965_set_rate(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006841
6842 spin_unlock_irqrestore(&priv->lock, flags);
6843
6844#ifdef IEEE80211_CONF_CHANNEL_SWITCH
6845 if (conf->flags & IEEE80211_CONF_CHANNEL_SWITCH) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006846 iwl4965_hw_channel_switch(priv, conf->channel);
Zhu Yi76bb77e2007-11-22 10:53:22 +08006847 goto out;
Zhu Yib481de92007-09-25 17:54:57 -07006848 }
6849#endif
6850
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006851 iwl4965_radio_kill_sw(priv, !conf->radio_enabled);
Zhu Yib481de92007-09-25 17:54:57 -07006852
6853 if (!conf->radio_enabled) {
6854 IWL_DEBUG_MAC80211("leave - radio disabled\n");
Zhu Yi76bb77e2007-11-22 10:53:22 +08006855 goto out;
Zhu Yib481de92007-09-25 17:54:57 -07006856 }
6857
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006858 if (iwl4965_is_rfkill(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07006859 IWL_DEBUG_MAC80211("leave - RF kill\n");
Zhu Yi76bb77e2007-11-22 10:53:22 +08006860 ret = -EIO;
6861 goto out;
Zhu Yib481de92007-09-25 17:54:57 -07006862 }
6863
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006864 iwl4965_set_rate(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006865
6866 if (memcmp(&priv->active_rxon,
6867 &priv->staging_rxon, sizeof(priv->staging_rxon)))
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006868 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006869 else
6870 IWL_DEBUG_INFO("No re-sending same RXON configuration.\n");
6871
6872 IWL_DEBUG_MAC80211("leave\n");
6873
Zhu Yia0646472007-12-20 14:10:01 +08006874out:
6875 clear_bit(STATUS_CONF_PENDING, &priv->status);
Zhu Yi5a66926a2008-01-14 17:46:18 -08006876 mutex_unlock(&priv->mutex);
Zhu Yi76bb77e2007-11-22 10:53:22 +08006877 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07006878}
6879
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006880static void iwl4965_config_ap(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07006881{
6882 int rc = 0;
6883
Maarten Lankhorstd986bcd2008-01-23 10:15:16 -08006884 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
Zhu Yib481de92007-09-25 17:54:57 -07006885 return;
6886
6887 /* The following should be done only at AP bring up */
6888 if ((priv->active_rxon.filter_flags & RXON_FILTER_ASSOC_MSK) == 0) {
6889
6890 /* RXON - unassoc (to set timing command) */
6891 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006892 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006893
6894 /* RXON Timing */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006895 memset(&priv->rxon_timing, 0, sizeof(struct iwl4965_rxon_time_cmd));
6896 iwl4965_setup_rxon_timing(priv);
6897 rc = iwl4965_send_cmd_pdu(priv, REPLY_RXON_TIMING,
Zhu Yib481de92007-09-25 17:54:57 -07006898 sizeof(priv->rxon_timing), &priv->rxon_timing);
6899 if (rc)
6900 IWL_WARNING("REPLY_RXON_TIMING failed - "
6901 "Attempting to continue.\n");
6902
6903 iwl4965_set_rxon_chain(priv);
6904
6905 /* FIXME: what should be the assoc_id for AP? */
6906 priv->staging_rxon.assoc_id = cpu_to_le16(priv->assoc_id);
6907 if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_PREAMBLE)
6908 priv->staging_rxon.flags |=
6909 RXON_FLG_SHORT_PREAMBLE_MSK;
6910 else
6911 priv->staging_rxon.flags &=
6912 ~RXON_FLG_SHORT_PREAMBLE_MSK;
6913
6914 if (priv->staging_rxon.flags & RXON_FLG_BAND_24G_MSK) {
6915 if (priv->assoc_capability &
6916 WLAN_CAPABILITY_SHORT_SLOT_TIME)
6917 priv->staging_rxon.flags |=
6918 RXON_FLG_SHORT_SLOT_MSK;
6919 else
6920 priv->staging_rxon.flags &=
6921 ~RXON_FLG_SHORT_SLOT_MSK;
6922
6923 if (priv->iw_mode == IEEE80211_IF_TYPE_IBSS)
6924 priv->staging_rxon.flags &=
6925 ~RXON_FLG_SHORT_SLOT_MSK;
6926 }
6927 /* restore RXON assoc */
6928 priv->staging_rxon.filter_flags |= RXON_FILTER_ASSOC_MSK;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006929 iwl4965_commit_rxon(priv);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006930 iwl4965_activate_qos(priv, 1);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006931 iwl4965_rxon_add_station(priv, iwl4965_broadcast_addr, 0);
Zhu Yie1493de2007-09-27 11:27:32 +08006932 }
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006933 iwl4965_send_beacon_cmd(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006934
6935 /* FIXME - we need to add code here to detect a totally new
6936 * configuration, reset the AP, unassoc, rxon timing, assoc,
6937 * clear sta table, add BCAST sta... */
6938}
6939
Johannes Berg32bfd352007-12-19 01:31:26 +01006940static int iwl4965_mac_config_interface(struct ieee80211_hw *hw,
6941 struct ieee80211_vif *vif,
Zhu Yib481de92007-09-25 17:54:57 -07006942 struct ieee80211_if_conf *conf)
6943{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006944 struct iwl_priv *priv = hw->priv;
Joe Perches0795af52007-10-03 17:59:30 -07006945 DECLARE_MAC_BUF(mac);
Zhu Yib481de92007-09-25 17:54:57 -07006946 unsigned long flags;
6947 int rc;
6948
6949 if (conf == NULL)
6950 return -EIO;
6951
Emmanuel Grumbachb716bb92008-03-04 18:09:32 -08006952 if (priv->vif != vif) {
6953 IWL_DEBUG_MAC80211("leave - priv->vif != vif\n");
6954 mutex_unlock(&priv->mutex);
6955 return 0;
6956 }
6957
Zhu Yib481de92007-09-25 17:54:57 -07006958 if ((priv->iw_mode == IEEE80211_IF_TYPE_AP) &&
6959 (!conf->beacon || !conf->ssid_len)) {
6960 IWL_DEBUG_MAC80211
6961 ("Leaving in AP mode because HostAPD is not ready.\n");
6962 return 0;
6963 }
6964
Zhu Yi5a66926a2008-01-14 17:46:18 -08006965 if (!iwl4965_is_alive(priv))
6966 return -EAGAIN;
6967
Zhu Yib481de92007-09-25 17:54:57 -07006968 mutex_lock(&priv->mutex);
6969
Zhu Yib481de92007-09-25 17:54:57 -07006970 if (conf->bssid)
Joe Perches0795af52007-10-03 17:59:30 -07006971 IWL_DEBUG_MAC80211("bssid: %s\n",
6972 print_mac(mac, conf->bssid));
Zhu Yib481de92007-09-25 17:54:57 -07006973
Johannes Berg4150c572007-09-17 01:29:23 -04006974/*
6975 * very dubious code was here; the probe filtering flag is never set:
6976 *
Zhu Yib481de92007-09-25 17:54:57 -07006977 if (unlikely(test_bit(STATUS_SCANNING, &priv->status)) &&
6978 !(priv->hw->flags & IEEE80211_HW_NO_PROBE_FILTERING)) {
Johannes Berg4150c572007-09-17 01:29:23 -04006979 */
Zhu Yib481de92007-09-25 17:54:57 -07006980
6981 if (priv->iw_mode == IEEE80211_IF_TYPE_AP) {
6982 if (!conf->bssid) {
6983 conf->bssid = priv->mac_addr;
6984 memcpy(priv->bssid, priv->mac_addr, ETH_ALEN);
Joe Perches0795af52007-10-03 17:59:30 -07006985 IWL_DEBUG_MAC80211("bssid was set to: %s\n",
6986 print_mac(mac, conf->bssid));
Zhu Yib481de92007-09-25 17:54:57 -07006987 }
6988 if (priv->ibss_beacon)
6989 dev_kfree_skb(priv->ibss_beacon);
6990
6991 priv->ibss_beacon = conf->beacon;
6992 }
6993
Mohamed Abbasfde35712007-11-29 11:10:15 +08006994 if (iwl4965_is_rfkill(priv))
6995 goto done;
6996
Zhu Yib481de92007-09-25 17:54:57 -07006997 if (conf->bssid && !is_zero_ether_addr(conf->bssid) &&
6998 !is_multicast_ether_addr(conf->bssid)) {
6999 /* If there is currently a HW scan going on in the background
7000 * then we need to cancel it else the RXON below will fail. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007001 if (iwl4965_scan_cancel_timeout(priv, 100)) {
Zhu Yib481de92007-09-25 17:54:57 -07007002 IWL_WARNING("Aborted scan still in progress "
7003 "after 100ms\n");
7004 IWL_DEBUG_MAC80211("leaving - scan abort failed.\n");
7005 mutex_unlock(&priv->mutex);
7006 return -EAGAIN;
7007 }
7008 memcpy(priv->staging_rxon.bssid_addr, conf->bssid, ETH_ALEN);
7009
7010 /* TODO: Audit driver for usage of these members and see
7011 * if mac80211 deprecates them (priv->bssid looks like it
7012 * shouldn't be there, but I haven't scanned the IBSS code
7013 * to verify) - jpk */
7014 memcpy(priv->bssid, conf->bssid, ETH_ALEN);
7015
7016 if (priv->iw_mode == IEEE80211_IF_TYPE_AP)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007017 iwl4965_config_ap(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007018 else {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007019 rc = iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007020 if ((priv->iw_mode == IEEE80211_IF_TYPE_STA) && rc)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007021 iwl4965_rxon_add_station(
Zhu Yib481de92007-09-25 17:54:57 -07007022 priv, priv->active_rxon.bssid_addr, 1);
7023 }
7024
7025 } else {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007026 iwl4965_scan_cancel_timeout(priv, 100);
Zhu Yib481de92007-09-25 17:54:57 -07007027 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007028 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007029 }
7030
Mohamed Abbasfde35712007-11-29 11:10:15 +08007031 done:
Zhu Yib481de92007-09-25 17:54:57 -07007032 spin_lock_irqsave(&priv->lock, flags);
7033 if (!conf->ssid_len)
7034 memset(priv->essid, 0, IW_ESSID_MAX_SIZE);
7035 else
7036 memcpy(priv->essid, conf->ssid, conf->ssid_len);
7037
7038 priv->essid_len = conf->ssid_len;
7039 spin_unlock_irqrestore(&priv->lock, flags);
7040
7041 IWL_DEBUG_MAC80211("leave\n");
7042 mutex_unlock(&priv->mutex);
7043
7044 return 0;
7045}
7046
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007047static void iwl4965_configure_filter(struct ieee80211_hw *hw,
Johannes Berg4150c572007-09-17 01:29:23 -04007048 unsigned int changed_flags,
7049 unsigned int *total_flags,
7050 int mc_count, struct dev_addr_list *mc_list)
7051{
7052 /*
7053 * XXX: dummy
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007054 * see also iwl4965_connection_init_rx_config
Johannes Berg4150c572007-09-17 01:29:23 -04007055 */
7056 *total_flags = 0;
7057}
7058
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007059static void iwl4965_mac_remove_interface(struct ieee80211_hw *hw,
Zhu Yib481de92007-09-25 17:54:57 -07007060 struct ieee80211_if_init_conf *conf)
7061{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007062 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07007063
7064 IWL_DEBUG_MAC80211("enter\n");
7065
7066 mutex_lock(&priv->mutex);
Mohamed Abbas948c1712007-10-25 17:15:45 +08007067
Mohamed Abbasfde35712007-11-29 11:10:15 +08007068 if (iwl4965_is_ready_rf(priv)) {
7069 iwl4965_scan_cancel_timeout(priv, 100);
7070 cancel_delayed_work(&priv->post_associate);
7071 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
7072 iwl4965_commit_rxon(priv);
7073 }
Johannes Berg32bfd352007-12-19 01:31:26 +01007074 if (priv->vif == conf->vif) {
7075 priv->vif = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07007076 memset(priv->bssid, 0, ETH_ALEN);
7077 memset(priv->essid, 0, IW_ESSID_MAX_SIZE);
7078 priv->essid_len = 0;
7079 }
7080 mutex_unlock(&priv->mutex);
7081
7082 IWL_DEBUG_MAC80211("leave\n");
7083
7084}
Johannes Berg471b3ef2007-12-28 14:32:58 +01007085
7086static void iwl4965_bss_info_changed(struct ieee80211_hw *hw,
7087 struct ieee80211_vif *vif,
7088 struct ieee80211_bss_conf *bss_conf,
7089 u32 changes)
Tomas Winkler220173b2007-10-16 00:50:25 +02007090{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007091 struct iwl_priv *priv = hw->priv;
Tomas Winkler220173b2007-10-16 00:50:25 +02007092
Johannes Berg471b3ef2007-12-28 14:32:58 +01007093 if (changes & BSS_CHANGED_ERP_PREAMBLE) {
7094 if (bss_conf->use_short_preamble)
Tomas Winkler220173b2007-10-16 00:50:25 +02007095 priv->staging_rxon.flags |= RXON_FLG_SHORT_PREAMBLE_MSK;
7096 else
7097 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_PREAMBLE_MSK;
7098 }
7099
Johannes Berg471b3ef2007-12-28 14:32:58 +01007100 if (changes & BSS_CHANGED_ERP_CTS_PROT) {
Johannes Berg8318d782008-01-24 19:38:38 +01007101 if (bss_conf->use_cts_prot && (priv->band != IEEE80211_BAND_5GHZ))
Tomas Winkler220173b2007-10-16 00:50:25 +02007102 priv->staging_rxon.flags |= RXON_FLG_TGG_PROTECT_MSK;
7103 else
7104 priv->staging_rxon.flags &= ~RXON_FLG_TGG_PROTECT_MSK;
7105 }
7106
Johannes Berg471b3ef2007-12-28 14:32:58 +01007107 if (changes & BSS_CHANGED_ASSOC) {
7108 /*
7109 * TODO:
7110 * do stuff instead of sniffing assoc resp
7111 */
7112 }
7113
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007114 if (iwl4965_is_associated(priv))
7115 iwl4965_send_rxon_assoc(priv);
Tomas Winkler220173b2007-10-16 00:50:25 +02007116}
Zhu Yib481de92007-09-25 17:54:57 -07007117
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007118static int iwl4965_mac_hw_scan(struct ieee80211_hw *hw, u8 *ssid, size_t len)
Zhu Yib481de92007-09-25 17:54:57 -07007119{
7120 int rc = 0;
7121 unsigned long flags;
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007122 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07007123
7124 IWL_DEBUG_MAC80211("enter\n");
7125
mabbas052c4b92007-10-25 17:15:43 +08007126 mutex_lock(&priv->mutex);
Zhu Yib481de92007-09-25 17:54:57 -07007127 spin_lock_irqsave(&priv->lock, flags);
7128
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007129 if (!iwl4965_is_ready_rf(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07007130 rc = -EIO;
7131 IWL_DEBUG_MAC80211("leave - not ready or exit pending\n");
7132 goto out_unlock;
7133 }
7134
7135 if (priv->iw_mode == IEEE80211_IF_TYPE_AP) { /* APs don't scan */
7136 rc = -EIO;
7137 IWL_ERROR("ERROR: APs don't scan\n");
7138 goto out_unlock;
7139 }
7140
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08007141 /* we don't schedule scan within next_scan_jiffies period */
7142 if (priv->next_scan_jiffies &&
7143 time_after(priv->next_scan_jiffies, jiffies)) {
7144 rc = -EAGAIN;
7145 goto out_unlock;
7146 }
Zhu Yib481de92007-09-25 17:54:57 -07007147 /* if we just finished scan ask for delay */
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08007148 if (priv->last_scan_jiffies && time_after(priv->last_scan_jiffies +
7149 IWL_DELAY_NEXT_SCAN, jiffies)) {
Zhu Yib481de92007-09-25 17:54:57 -07007150 rc = -EAGAIN;
7151 goto out_unlock;
7152 }
7153 if (len) {
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08007154 IWL_DEBUG_SCAN("direct scan for %s [%d]\n ",
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007155 iwl4965_escape_essid(ssid, len), (int)len);
Zhu Yib481de92007-09-25 17:54:57 -07007156
7157 priv->one_direct_scan = 1;
7158 priv->direct_ssid_len = (u8)
7159 min((u8) len, (u8) IW_ESSID_MAX_SIZE);
7160 memcpy(priv->direct_ssid, ssid, priv->direct_ssid_len);
Mohamed Abbas948c1712007-10-25 17:15:45 +08007161 } else
7162 priv->one_direct_scan = 0;
Zhu Yib481de92007-09-25 17:54:57 -07007163
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007164 rc = iwl4965_scan_initiate(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007165
7166 IWL_DEBUG_MAC80211("leave\n");
7167
7168out_unlock:
7169 spin_unlock_irqrestore(&priv->lock, flags);
mabbas052c4b92007-10-25 17:15:43 +08007170 mutex_unlock(&priv->mutex);
Zhu Yib481de92007-09-25 17:54:57 -07007171
7172 return rc;
7173}
7174
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007175static int iwl4965_mac_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
Zhu Yib481de92007-09-25 17:54:57 -07007176 const u8 *local_addr, const u8 *addr,
7177 struct ieee80211_key_conf *key)
7178{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007179 struct iwl_priv *priv = hw->priv;
Joe Perches0795af52007-10-03 17:59:30 -07007180 DECLARE_MAC_BUF(mac);
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07007181 int ret = 0;
7182 u8 sta_id = IWL_INVALID_STATION;
7183 u8 static_key;
Zhu Yib481de92007-09-25 17:54:57 -07007184
7185 IWL_DEBUG_MAC80211("enter\n");
7186
Assaf Krauss00acbc92008-03-14 10:38:47 -07007187 if (!iwl4965_mod_params.hw_crypto) {
Zhu Yib481de92007-09-25 17:54:57 -07007188 IWL_DEBUG_MAC80211("leave - hwcrypto disabled\n");
7189 return -EOPNOTSUPP;
7190 }
7191
7192 if (is_zero_ether_addr(addr))
7193 /* only support pairwise keys */
7194 return -EOPNOTSUPP;
7195
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07007196 /* FIXME: need to differenciate between static and dynamic key
7197 * in the level of mac80211 */
7198 static_key = !iwl4965_is_associated(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007199
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07007200 if (!static_key) {
7201 sta_id = iwl4965_hw_find_station(priv, addr);
7202 if (sta_id == IWL_INVALID_STATION) {
7203 IWL_DEBUG_MAC80211("leave - %s not in station map.\n",
7204 print_mac(mac, addr));
7205 return -EINVAL;
7206 }
7207 }
Zhu Yib481de92007-09-25 17:54:57 -07007208
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007209 iwl4965_scan_cancel_timeout(priv, 100);
mabbas052c4b92007-10-25 17:15:43 +08007210
Zhu Yib481de92007-09-25 17:54:57 -07007211 switch (cmd) {
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07007212 case SET_KEY:
7213 if (static_key)
7214 ret = iwl4965_set_static_key(priv, key);
7215 else
7216 ret = iwl4965_set_dynamic_key(priv, key, sta_id);
7217
7218 IWL_DEBUG_MAC80211("enable hwcrypto key\n");
Zhu Yib481de92007-09-25 17:54:57 -07007219 break;
7220 case DISABLE_KEY:
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07007221 if (static_key)
7222 ret = iwl4965_remove_static_key(priv);
7223 else
7224 ret = iwl4965_clear_sta_key_info(priv, sta_id);
7225
7226 IWL_DEBUG_MAC80211("disable hwcrypto key\n");
Zhu Yib481de92007-09-25 17:54:57 -07007227 break;
7228 default:
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07007229 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07007230 }
7231
7232 IWL_DEBUG_MAC80211("leave\n");
Zhu Yib481de92007-09-25 17:54:57 -07007233
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07007234 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07007235}
7236
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007237static int iwl4965_mac_conf_tx(struct ieee80211_hw *hw, int queue,
Zhu Yib481de92007-09-25 17:54:57 -07007238 const struct ieee80211_tx_queue_params *params)
7239{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007240 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07007241 unsigned long flags;
7242 int q;
Zhu Yib481de92007-09-25 17:54:57 -07007243
7244 IWL_DEBUG_MAC80211("enter\n");
7245
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007246 if (!iwl4965_is_ready_rf(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07007247 IWL_DEBUG_MAC80211("leave - RF not ready\n");
7248 return -EIO;
7249 }
7250
7251 if (queue >= AC_NUM) {
7252 IWL_DEBUG_MAC80211("leave - queue >= AC_NUM %d\n", queue);
7253 return 0;
7254 }
7255
Zhu Yib481de92007-09-25 17:54:57 -07007256 if (!priv->qos_data.qos_enable) {
7257 priv->qos_data.qos_active = 0;
7258 IWL_DEBUG_MAC80211("leave - qos not enabled\n");
7259 return 0;
7260 }
7261 q = AC_NUM - 1 - queue;
7262
7263 spin_lock_irqsave(&priv->lock, flags);
7264
7265 priv->qos_data.def_qos_parm.ac[q].cw_min = cpu_to_le16(params->cw_min);
7266 priv->qos_data.def_qos_parm.ac[q].cw_max = cpu_to_le16(params->cw_max);
7267 priv->qos_data.def_qos_parm.ac[q].aifsn = params->aifs;
7268 priv->qos_data.def_qos_parm.ac[q].edca_txop =
Johannes Berg3330d7b2008-02-10 16:49:38 +01007269 cpu_to_le16((params->txop * 32));
Zhu Yib481de92007-09-25 17:54:57 -07007270
7271 priv->qos_data.def_qos_parm.ac[q].reserved1 = 0;
7272 priv->qos_data.qos_active = 1;
7273
7274 spin_unlock_irqrestore(&priv->lock, flags);
7275
7276 mutex_lock(&priv->mutex);
7277 if (priv->iw_mode == IEEE80211_IF_TYPE_AP)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007278 iwl4965_activate_qos(priv, 1);
7279 else if (priv->assoc_id && iwl4965_is_associated(priv))
7280 iwl4965_activate_qos(priv, 0);
Zhu Yib481de92007-09-25 17:54:57 -07007281
7282 mutex_unlock(&priv->mutex);
7283
Zhu Yib481de92007-09-25 17:54:57 -07007284 IWL_DEBUG_MAC80211("leave\n");
7285 return 0;
7286}
7287
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007288static int iwl4965_mac_get_tx_stats(struct ieee80211_hw *hw,
Zhu Yib481de92007-09-25 17:54:57 -07007289 struct ieee80211_tx_queue_stats *stats)
7290{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007291 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07007292 int i, avail;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007293 struct iwl4965_tx_queue *txq;
7294 struct iwl4965_queue *q;
Zhu Yib481de92007-09-25 17:54:57 -07007295 unsigned long flags;
7296
7297 IWL_DEBUG_MAC80211("enter\n");
7298
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007299 if (!iwl4965_is_ready_rf(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07007300 IWL_DEBUG_MAC80211("leave - RF not ready\n");
7301 return -EIO;
7302 }
7303
7304 spin_lock_irqsave(&priv->lock, flags);
7305
7306 for (i = 0; i < AC_NUM; i++) {
7307 txq = &priv->txq[i];
7308 q = &txq->q;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007309 avail = iwl4965_queue_space(q);
Zhu Yib481de92007-09-25 17:54:57 -07007310
7311 stats->data[i].len = q->n_window - avail;
7312 stats->data[i].limit = q->n_window - q->high_mark;
7313 stats->data[i].count = q->n_window;
7314
7315 }
7316 spin_unlock_irqrestore(&priv->lock, flags);
7317
7318 IWL_DEBUG_MAC80211("leave\n");
7319
7320 return 0;
7321}
7322
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007323static int iwl4965_mac_get_stats(struct ieee80211_hw *hw,
Zhu Yib481de92007-09-25 17:54:57 -07007324 struct ieee80211_low_level_stats *stats)
7325{
7326 IWL_DEBUG_MAC80211("enter\n");
7327 IWL_DEBUG_MAC80211("leave\n");
7328
7329 return 0;
7330}
7331
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007332static u64 iwl4965_mac_get_tsf(struct ieee80211_hw *hw)
Zhu Yib481de92007-09-25 17:54:57 -07007333{
7334 IWL_DEBUG_MAC80211("enter\n");
7335 IWL_DEBUG_MAC80211("leave\n");
7336
7337 return 0;
7338}
7339
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007340static void iwl4965_mac_reset_tsf(struct ieee80211_hw *hw)
Zhu Yib481de92007-09-25 17:54:57 -07007341{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007342 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07007343 unsigned long flags;
7344
7345 mutex_lock(&priv->mutex);
7346 IWL_DEBUG_MAC80211("enter\n");
7347
7348 priv->lq_mngr.lq_ready = 0;
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08007349#ifdef CONFIG_IWL4965_HT
Zhu Yib481de92007-09-25 17:54:57 -07007350 spin_lock_irqsave(&priv->lock, flags);
Ron Rindjunskyfd105e72007-11-26 16:14:39 +02007351 memset(&priv->current_ht_config, 0, sizeof(struct iwl_ht_info));
Zhu Yib481de92007-09-25 17:54:57 -07007352 spin_unlock_irqrestore(&priv->lock, flags);
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08007353#endif /* CONFIG_IWL4965_HT */
Zhu Yib481de92007-09-25 17:54:57 -07007354
Assaf Kraussbf85ea42008-03-14 10:38:49 -07007355 iwlcore_reset_qos(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007356
7357 cancel_delayed_work(&priv->post_associate);
7358
7359 spin_lock_irqsave(&priv->lock, flags);
7360 priv->assoc_id = 0;
7361 priv->assoc_capability = 0;
7362 priv->call_post_assoc_from_beacon = 0;
7363 priv->assoc_station_added = 0;
7364
7365 /* new association get rid of ibss beacon skb */
7366 if (priv->ibss_beacon)
7367 dev_kfree_skb(priv->ibss_beacon);
7368
7369 priv->ibss_beacon = NULL;
7370
7371 priv->beacon_int = priv->hw->conf.beacon_int;
7372 priv->timestamp1 = 0;
7373 priv->timestamp0 = 0;
7374 if ((priv->iw_mode == IEEE80211_IF_TYPE_STA))
7375 priv->beacon_int = 0;
7376
7377 spin_unlock_irqrestore(&priv->lock, flags);
7378
Mohamed Abbasfde35712007-11-29 11:10:15 +08007379 if (!iwl4965_is_ready_rf(priv)) {
7380 IWL_DEBUG_MAC80211("leave - not ready\n");
7381 mutex_unlock(&priv->mutex);
7382 return;
7383 }
7384
mabbas052c4b92007-10-25 17:15:43 +08007385 /* we are restarting association process
7386 * clear RXON_FILTER_ASSOC_MSK bit
7387 */
7388 if (priv->iw_mode != IEEE80211_IF_TYPE_AP) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007389 iwl4965_scan_cancel_timeout(priv, 100);
mabbas052c4b92007-10-25 17:15:43 +08007390 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007391 iwl4965_commit_rxon(priv);
mabbas052c4b92007-10-25 17:15:43 +08007392 }
7393
Zhu Yib481de92007-09-25 17:54:57 -07007394 /* Per mac80211.h: This is only used in IBSS mode... */
7395 if (priv->iw_mode != IEEE80211_IF_TYPE_IBSS) {
mabbas052c4b92007-10-25 17:15:43 +08007396
Zhu Yib481de92007-09-25 17:54:57 -07007397 IWL_DEBUG_MAC80211("leave - not in IBSS\n");
7398 mutex_unlock(&priv->mutex);
7399 return;
7400 }
7401
Zhu Yib481de92007-09-25 17:54:57 -07007402 priv->only_active_channel = 0;
7403
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007404 iwl4965_set_rate(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007405
7406 mutex_unlock(&priv->mutex);
7407
7408 IWL_DEBUG_MAC80211("leave\n");
Zhu Yib481de92007-09-25 17:54:57 -07007409}
7410
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007411static int iwl4965_mac_beacon_update(struct ieee80211_hw *hw, struct sk_buff *skb,
Zhu Yib481de92007-09-25 17:54:57 -07007412 struct ieee80211_tx_control *control)
7413{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007414 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07007415 unsigned long flags;
7416
7417 mutex_lock(&priv->mutex);
7418 IWL_DEBUG_MAC80211("enter\n");
7419
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007420 if (!iwl4965_is_ready_rf(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07007421 IWL_DEBUG_MAC80211("leave - RF not ready\n");
7422 mutex_unlock(&priv->mutex);
7423 return -EIO;
7424 }
7425
7426 if (priv->iw_mode != IEEE80211_IF_TYPE_IBSS) {
7427 IWL_DEBUG_MAC80211("leave - not IBSS\n");
7428 mutex_unlock(&priv->mutex);
7429 return -EIO;
7430 }
7431
7432 spin_lock_irqsave(&priv->lock, flags);
7433
7434 if (priv->ibss_beacon)
7435 dev_kfree_skb(priv->ibss_beacon);
7436
7437 priv->ibss_beacon = skb;
7438
7439 priv->assoc_id = 0;
7440
7441 IWL_DEBUG_MAC80211("leave\n");
7442 spin_unlock_irqrestore(&priv->lock, flags);
7443
Assaf Kraussbf85ea42008-03-14 10:38:49 -07007444 iwlcore_reset_qos(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007445
7446 queue_work(priv->workqueue, &priv->post_associate.work);
7447
7448 mutex_unlock(&priv->mutex);
7449
7450 return 0;
7451}
7452
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08007453#ifdef CONFIG_IWL4965_HT
Zhu Yib481de92007-09-25 17:54:57 -07007454
Ron Rindjunskyfd105e72007-11-26 16:14:39 +02007455static void iwl4965_ht_info_fill(struct ieee80211_conf *conf,
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007456 struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07007457{
Ron Rindjunskyfd105e72007-11-26 16:14:39 +02007458 struct iwl_ht_info *iwl_conf = &priv->current_ht_config;
7459 struct ieee80211_ht_info *ht_conf = &conf->ht_conf;
7460 struct ieee80211_ht_bss_info *ht_bss_conf = &conf->ht_bss_conf;
Zhu Yib481de92007-09-25 17:54:57 -07007461
7462 IWL_DEBUG_MAC80211("enter: \n");
7463
Ron Rindjunskyfd105e72007-11-26 16:14:39 +02007464 if (!(conf->flags & IEEE80211_CONF_SUPPORT_HT_MODE)) {
7465 iwl_conf->is_ht = 0;
7466 return;
Zhu Yib481de92007-09-25 17:54:57 -07007467 }
7468
Ron Rindjunskyfd105e72007-11-26 16:14:39 +02007469 iwl_conf->is_ht = 1;
7470 priv->ps_mode = (u8)((ht_conf->cap & IEEE80211_HT_CAP_MIMO_PS) >> 2);
Zhu Yib481de92007-09-25 17:54:57 -07007471
Ron Rindjunskyfd105e72007-11-26 16:14:39 +02007472 if (ht_conf->cap & IEEE80211_HT_CAP_SGI_20)
7473 iwl_conf->sgf |= 0x1;
7474 if (ht_conf->cap & IEEE80211_HT_CAP_SGI_40)
7475 iwl_conf->sgf |= 0x2;
Zhu Yib481de92007-09-25 17:54:57 -07007476
Ron Rindjunskyfd105e72007-11-26 16:14:39 +02007477 iwl_conf->is_green_field = !!(ht_conf->cap & IEEE80211_HT_CAP_GRN_FLD);
7478 iwl_conf->max_amsdu_size =
7479 !!(ht_conf->cap & IEEE80211_HT_CAP_MAX_AMSDU);
Guy Cohen134eb5d2008-03-04 18:09:25 -08007480
Ron Rindjunskyfd105e72007-11-26 16:14:39 +02007481 iwl_conf->supported_chan_width =
7482 !!(ht_conf->cap & IEEE80211_HT_CAP_SUP_WIDTH);
Guy Cohen134eb5d2008-03-04 18:09:25 -08007483 iwl_conf->extension_chan_offset =
7484 ht_bss_conf->bss_cap & IEEE80211_HT_IE_CHA_SEC_OFFSET;
7485 /* If no above or below channel supplied disable FAT channel */
7486 if (iwl_conf->extension_chan_offset != IWL_EXT_CHANNEL_OFFSET_ABOVE &&
7487 iwl_conf->extension_chan_offset != IWL_EXT_CHANNEL_OFFSET_BELOW)
7488 iwl_conf->supported_chan_width = 0;
7489
Ron Rindjunskyfd105e72007-11-26 16:14:39 +02007490 iwl_conf->tx_mimo_ps_mode =
7491 (u8)((ht_conf->cap & IEEE80211_HT_CAP_MIMO_PS) >> 2);
7492 memcpy(iwl_conf->supp_mcs_set, ht_conf->supp_mcs_set, 16);
Zhu Yib481de92007-09-25 17:54:57 -07007493
Ron Rindjunskyfd105e72007-11-26 16:14:39 +02007494 iwl_conf->control_channel = ht_bss_conf->primary_channel;
Ron Rindjunskyfd105e72007-11-26 16:14:39 +02007495 iwl_conf->tx_chan_width =
7496 !!(ht_bss_conf->bss_cap & IEEE80211_HT_IE_CHA_WIDTH);
7497 iwl_conf->ht_protection =
7498 ht_bss_conf->bss_op_mode & IEEE80211_HT_IE_HT_PROTECTION;
7499 iwl_conf->non_GF_STA_present =
7500 !!(ht_bss_conf->bss_op_mode & IEEE80211_HT_IE_NON_GF_STA_PRSNT);
Zhu Yib481de92007-09-25 17:54:57 -07007501
Ron Rindjunskyfd105e72007-11-26 16:14:39 +02007502 IWL_DEBUG_MAC80211("control channel %d\n",
7503 iwl_conf->control_channel);
Zhu Yib481de92007-09-25 17:54:57 -07007504 IWL_DEBUG_MAC80211("leave\n");
Zhu Yib481de92007-09-25 17:54:57 -07007505}
7506
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007507static int iwl4965_mac_conf_ht(struct ieee80211_hw *hw,
Ron Rindjunskyfd105e72007-11-26 16:14:39 +02007508 struct ieee80211_conf *conf)
Zhu Yib481de92007-09-25 17:54:57 -07007509{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007510 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07007511
7512 IWL_DEBUG_MAC80211("enter: \n");
7513
Ron Rindjunskyfd105e72007-11-26 16:14:39 +02007514 iwl4965_ht_info_fill(conf, priv);
Zhu Yib481de92007-09-25 17:54:57 -07007515 iwl4965_set_rxon_chain(priv);
7516
7517 if (priv && priv->assoc_id &&
7518 (priv->iw_mode == IEEE80211_IF_TYPE_STA)) {
7519 unsigned long flags;
7520
7521 spin_lock_irqsave(&priv->lock, flags);
7522 if (priv->beacon_int)
7523 queue_work(priv->workqueue, &priv->post_associate.work);
7524 else
7525 priv->call_post_assoc_from_beacon = 1;
7526 spin_unlock_irqrestore(&priv->lock, flags);
7527 }
7528
Ron Rindjunskyfd105e72007-11-26 16:14:39 +02007529 IWL_DEBUG_MAC80211("leave:\n");
7530 return 0;
Zhu Yib481de92007-09-25 17:54:57 -07007531}
7532
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08007533#endif /*CONFIG_IWL4965_HT*/
Zhu Yib481de92007-09-25 17:54:57 -07007534
7535/*****************************************************************************
7536 *
7537 * sysfs attributes
7538 *
7539 *****************************************************************************/
7540
Tomas Winkler0a6857e2008-03-12 16:58:49 -07007541#ifdef CONFIG_IWLWIFI_DEBUG
Zhu Yib481de92007-09-25 17:54:57 -07007542
7543/*
7544 * The following adds a new attribute to the sysfs representation
7545 * of this device driver (i.e. a new file in /sys/bus/pci/drivers/iwl/)
7546 * used for controlling the debug level.
7547 *
7548 * See the level definitions in iwl for details.
7549 */
7550
7551static ssize_t show_debug_level(struct device_driver *d, char *buf)
7552{
Tomas Winkler0a6857e2008-03-12 16:58:49 -07007553 return sprintf(buf, "0x%08X\n", iwl_debug_level);
Zhu Yib481de92007-09-25 17:54:57 -07007554}
7555static ssize_t store_debug_level(struct device_driver *d,
7556 const char *buf, size_t count)
7557{
7558 char *p = (char *)buf;
7559 u32 val;
7560
7561 val = simple_strtoul(p, &p, 0);
7562 if (p == buf)
7563 printk(KERN_INFO DRV_NAME
7564 ": %s is not in hex or decimal form.\n", buf);
7565 else
Tomas Winkler0a6857e2008-03-12 16:58:49 -07007566 iwl_debug_level = val;
Zhu Yib481de92007-09-25 17:54:57 -07007567
7568 return strnlen(buf, count);
7569}
7570
7571static DRIVER_ATTR(debug_level, S_IWUSR | S_IRUGO,
7572 show_debug_level, store_debug_level);
7573
Tomas Winkler0a6857e2008-03-12 16:58:49 -07007574#endif /* CONFIG_IWLWIFI_DEBUG */
Zhu Yib481de92007-09-25 17:54:57 -07007575
7576static ssize_t show_rf_kill(struct device *d,
7577 struct device_attribute *attr, char *buf)
7578{
7579 /*
7580 * 0 - RF kill not enabled
7581 * 1 - SW based RF kill active (sysfs)
7582 * 2 - HW based RF kill active
7583 * 3 - Both HW and SW based RF kill active
7584 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007585 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07007586 int val = (test_bit(STATUS_RF_KILL_SW, &priv->status) ? 0x1 : 0x0) |
7587 (test_bit(STATUS_RF_KILL_HW, &priv->status) ? 0x2 : 0x0);
7588
7589 return sprintf(buf, "%i\n", val);
7590}
7591
7592static ssize_t store_rf_kill(struct device *d,
7593 struct device_attribute *attr,
7594 const char *buf, size_t count)
7595{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007596 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07007597
7598 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007599 iwl4965_radio_kill_sw(priv, buf[0] == '1');
Zhu Yib481de92007-09-25 17:54:57 -07007600 mutex_unlock(&priv->mutex);
7601
7602 return count;
7603}
7604
7605static DEVICE_ATTR(rf_kill, S_IWUSR | S_IRUGO, show_rf_kill, store_rf_kill);
7606
7607static ssize_t show_temperature(struct device *d,
7608 struct device_attribute *attr, char *buf)
7609{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007610 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07007611
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007612 if (!iwl4965_is_alive(priv))
Zhu Yib481de92007-09-25 17:54:57 -07007613 return -EAGAIN;
7614
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007615 return sprintf(buf, "%d\n", iwl4965_hw_get_temperature(priv));
Zhu Yib481de92007-09-25 17:54:57 -07007616}
7617
7618static DEVICE_ATTR(temperature, S_IRUGO, show_temperature, NULL);
7619
7620static ssize_t show_rs_window(struct device *d,
7621 struct device_attribute *attr,
7622 char *buf)
7623{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007624 struct iwl_priv *priv = d->driver_data;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007625 return iwl4965_fill_rs_info(priv->hw, buf, IWL_AP_ID);
Zhu Yib481de92007-09-25 17:54:57 -07007626}
7627static DEVICE_ATTR(rs_window, S_IRUGO, show_rs_window, NULL);
7628
7629static ssize_t show_tx_power(struct device *d,
7630 struct device_attribute *attr, char *buf)
7631{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007632 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07007633 return sprintf(buf, "%d\n", priv->user_txpower_limit);
7634}
7635
7636static ssize_t store_tx_power(struct device *d,
7637 struct device_attribute *attr,
7638 const char *buf, size_t count)
7639{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007640 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07007641 char *p = (char *)buf;
7642 u32 val;
7643
7644 val = simple_strtoul(p, &p, 10);
7645 if (p == buf)
7646 printk(KERN_INFO DRV_NAME
7647 ": %s is not in decimal form.\n", buf);
7648 else
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007649 iwl4965_hw_reg_set_txpower(priv, val);
Zhu Yib481de92007-09-25 17:54:57 -07007650
7651 return count;
7652}
7653
7654static DEVICE_ATTR(tx_power, S_IWUSR | S_IRUGO, show_tx_power, store_tx_power);
7655
7656static ssize_t show_flags(struct device *d,
7657 struct device_attribute *attr, char *buf)
7658{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007659 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07007660
7661 return sprintf(buf, "0x%04X\n", priv->active_rxon.flags);
7662}
7663
7664static ssize_t store_flags(struct device *d,
7665 struct device_attribute *attr,
7666 const char *buf, size_t count)
7667{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007668 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07007669 u32 flags = simple_strtoul(buf, NULL, 0);
7670
7671 mutex_lock(&priv->mutex);
7672 if (le32_to_cpu(priv->staging_rxon.flags) != flags) {
7673 /* Cancel any currently running scans... */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007674 if (iwl4965_scan_cancel_timeout(priv, 100))
Zhu Yib481de92007-09-25 17:54:57 -07007675 IWL_WARNING("Could not cancel scan.\n");
7676 else {
7677 IWL_DEBUG_INFO("Committing rxon.flags = 0x%04X\n",
7678 flags);
7679 priv->staging_rxon.flags = cpu_to_le32(flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007680 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007681 }
7682 }
7683 mutex_unlock(&priv->mutex);
7684
7685 return count;
7686}
7687
7688static DEVICE_ATTR(flags, S_IWUSR | S_IRUGO, show_flags, store_flags);
7689
7690static ssize_t show_filter_flags(struct device *d,
7691 struct device_attribute *attr, char *buf)
7692{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007693 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07007694
7695 return sprintf(buf, "0x%04X\n",
7696 le32_to_cpu(priv->active_rxon.filter_flags));
7697}
7698
7699static ssize_t store_filter_flags(struct device *d,
7700 struct device_attribute *attr,
7701 const char *buf, size_t count)
7702{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007703 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07007704 u32 filter_flags = simple_strtoul(buf, NULL, 0);
7705
7706 mutex_lock(&priv->mutex);
7707 if (le32_to_cpu(priv->staging_rxon.filter_flags) != filter_flags) {
7708 /* Cancel any currently running scans... */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007709 if (iwl4965_scan_cancel_timeout(priv, 100))
Zhu Yib481de92007-09-25 17:54:57 -07007710 IWL_WARNING("Could not cancel scan.\n");
7711 else {
7712 IWL_DEBUG_INFO("Committing rxon.filter_flags = "
7713 "0x%04X\n", filter_flags);
7714 priv->staging_rxon.filter_flags =
7715 cpu_to_le32(filter_flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007716 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007717 }
7718 }
7719 mutex_unlock(&priv->mutex);
7720
7721 return count;
7722}
7723
7724static DEVICE_ATTR(filter_flags, S_IWUSR | S_IRUGO, show_filter_flags,
7725 store_filter_flags);
7726
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08007727#ifdef CONFIG_IWL4965_SPECTRUM_MEASUREMENT
Zhu Yib481de92007-09-25 17:54:57 -07007728
7729static ssize_t show_measurement(struct device *d,
7730 struct device_attribute *attr, char *buf)
7731{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007732 struct iwl_priv *priv = dev_get_drvdata(d);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007733 struct iwl4965_spectrum_notification measure_report;
Zhu Yib481de92007-09-25 17:54:57 -07007734 u32 size = sizeof(measure_report), len = 0, ofs = 0;
7735 u8 *data = (u8 *) & measure_report;
7736 unsigned long flags;
7737
7738 spin_lock_irqsave(&priv->lock, flags);
7739 if (!(priv->measurement_status & MEASUREMENT_READY)) {
7740 spin_unlock_irqrestore(&priv->lock, flags);
7741 return 0;
7742 }
7743 memcpy(&measure_report, &priv->measure_report, size);
7744 priv->measurement_status = 0;
7745 spin_unlock_irqrestore(&priv->lock, flags);
7746
7747 while (size && (PAGE_SIZE - len)) {
7748 hex_dump_to_buffer(data + ofs, size, 16, 1, buf + len,
7749 PAGE_SIZE - len, 1);
7750 len = strlen(buf);
7751 if (PAGE_SIZE - len)
7752 buf[len++] = '\n';
7753
7754 ofs += 16;
7755 size -= min(size, 16U);
7756 }
7757
7758 return len;
7759}
7760
7761static ssize_t store_measurement(struct device *d,
7762 struct device_attribute *attr,
7763 const char *buf, size_t count)
7764{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007765 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07007766 struct ieee80211_measurement_params params = {
7767 .channel = le16_to_cpu(priv->active_rxon.channel),
7768 .start_time = cpu_to_le64(priv->last_tsf),
7769 .duration = cpu_to_le16(1),
7770 };
7771 u8 type = IWL_MEASURE_BASIC;
7772 u8 buffer[32];
7773 u8 channel;
7774
7775 if (count) {
7776 char *p = buffer;
7777 strncpy(buffer, buf, min(sizeof(buffer), count));
7778 channel = simple_strtoul(p, NULL, 0);
7779 if (channel)
7780 params.channel = channel;
7781
7782 p = buffer;
7783 while (*p && *p != ' ')
7784 p++;
7785 if (*p)
7786 type = simple_strtoul(p + 1, NULL, 0);
7787 }
7788
7789 IWL_DEBUG_INFO("Invoking measurement of type %d on "
7790 "channel %d (for '%s')\n", type, params.channel, buf);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007791 iwl4965_get_measurement(priv, &params, type);
Zhu Yib481de92007-09-25 17:54:57 -07007792
7793 return count;
7794}
7795
7796static DEVICE_ATTR(measurement, S_IRUSR | S_IWUSR,
7797 show_measurement, store_measurement);
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08007798#endif /* CONFIG_IWL4965_SPECTRUM_MEASUREMENT */
Zhu Yib481de92007-09-25 17:54:57 -07007799
7800static ssize_t store_retry_rate(struct device *d,
7801 struct device_attribute *attr,
7802 const char *buf, size_t count)
7803{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007804 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07007805
7806 priv->retry_rate = simple_strtoul(buf, NULL, 0);
7807 if (priv->retry_rate <= 0)
7808 priv->retry_rate = 1;
7809
7810 return count;
7811}
7812
7813static ssize_t show_retry_rate(struct device *d,
7814 struct device_attribute *attr, char *buf)
7815{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007816 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07007817 return sprintf(buf, "%d", priv->retry_rate);
7818}
7819
7820static DEVICE_ATTR(retry_rate, S_IWUSR | S_IRUSR, show_retry_rate,
7821 store_retry_rate);
7822
7823static ssize_t store_power_level(struct device *d,
7824 struct device_attribute *attr,
7825 const char *buf, size_t count)
7826{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007827 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07007828 int rc;
7829 int mode;
7830
7831 mode = simple_strtoul(buf, NULL, 0);
7832 mutex_lock(&priv->mutex);
7833
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007834 if (!iwl4965_is_ready(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07007835 rc = -EAGAIN;
7836 goto out;
7837 }
7838
7839 if ((mode < 1) || (mode > IWL_POWER_LIMIT) || (mode == IWL_POWER_AC))
7840 mode = IWL_POWER_AC;
7841 else
7842 mode |= IWL_POWER_ENABLED;
7843
7844 if (mode != priv->power_mode) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007845 rc = iwl4965_send_power_mode(priv, IWL_POWER_LEVEL(mode));
Zhu Yib481de92007-09-25 17:54:57 -07007846 if (rc) {
7847 IWL_DEBUG_MAC80211("failed setting power mode.\n");
7848 goto out;
7849 }
7850 priv->power_mode = mode;
7851 }
7852
7853 rc = count;
7854
7855 out:
7856 mutex_unlock(&priv->mutex);
7857 return rc;
7858}
7859
7860#define MAX_WX_STRING 80
7861
7862/* Values are in microsecond */
7863static const s32 timeout_duration[] = {
7864 350000,
7865 250000,
7866 75000,
7867 37000,
7868 25000,
7869};
7870static const s32 period_duration[] = {
7871 400000,
7872 700000,
7873 1000000,
7874 1000000,
7875 1000000
7876};
7877
7878static ssize_t show_power_level(struct device *d,
7879 struct device_attribute *attr, char *buf)
7880{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007881 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07007882 int level = IWL_POWER_LEVEL(priv->power_mode);
7883 char *p = buf;
7884
7885 p += sprintf(p, "%d ", level);
7886 switch (level) {
7887 case IWL_POWER_MODE_CAM:
7888 case IWL_POWER_AC:
7889 p += sprintf(p, "(AC)");
7890 break;
7891 case IWL_POWER_BATTERY:
7892 p += sprintf(p, "(BATTERY)");
7893 break;
7894 default:
7895 p += sprintf(p,
7896 "(Timeout %dms, Period %dms)",
7897 timeout_duration[level - 1] / 1000,
7898 period_duration[level - 1] / 1000);
7899 }
7900
7901 if (!(priv->power_mode & IWL_POWER_ENABLED))
7902 p += sprintf(p, " OFF\n");
7903 else
7904 p += sprintf(p, " \n");
7905
7906 return (p - buf + 1);
7907
7908}
7909
7910static DEVICE_ATTR(power_level, S_IWUSR | S_IRUSR, show_power_level,
7911 store_power_level);
7912
7913static ssize_t show_channels(struct device *d,
7914 struct device_attribute *attr, char *buf)
7915{
Johannes Berg8318d782008-01-24 19:38:38 +01007916 /* all this shit doesn't belong into sysfs anyway */
7917 return 0;
Zhu Yib481de92007-09-25 17:54:57 -07007918}
7919
7920static DEVICE_ATTR(channels, S_IRUSR, show_channels, NULL);
7921
7922static ssize_t show_statistics(struct device *d,
7923 struct device_attribute *attr, char *buf)
7924{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007925 struct iwl_priv *priv = dev_get_drvdata(d);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007926 u32 size = sizeof(struct iwl4965_notif_statistics);
Zhu Yib481de92007-09-25 17:54:57 -07007927 u32 len = 0, ofs = 0;
7928 u8 *data = (u8 *) & priv->statistics;
7929 int rc = 0;
7930
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007931 if (!iwl4965_is_alive(priv))
Zhu Yib481de92007-09-25 17:54:57 -07007932 return -EAGAIN;
7933
7934 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007935 rc = iwl4965_send_statistics_request(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007936 mutex_unlock(&priv->mutex);
7937
7938 if (rc) {
7939 len = sprintf(buf,
7940 "Error sending statistics request: 0x%08X\n", rc);
7941 return len;
7942 }
7943
7944 while (size && (PAGE_SIZE - len)) {
7945 hex_dump_to_buffer(data + ofs, size, 16, 1, buf + len,
7946 PAGE_SIZE - len, 1);
7947 len = strlen(buf);
7948 if (PAGE_SIZE - len)
7949 buf[len++] = '\n';
7950
7951 ofs += 16;
7952 size -= min(size, 16U);
7953 }
7954
7955 return len;
7956}
7957
7958static DEVICE_ATTR(statistics, S_IRUGO, show_statistics, NULL);
7959
7960static ssize_t show_antenna(struct device *d,
7961 struct device_attribute *attr, char *buf)
7962{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007963 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07007964
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007965 if (!iwl4965_is_alive(priv))
Zhu Yib481de92007-09-25 17:54:57 -07007966 return -EAGAIN;
7967
7968 return sprintf(buf, "%d\n", priv->antenna);
7969}
7970
7971static ssize_t store_antenna(struct device *d,
7972 struct device_attribute *attr,
7973 const char *buf, size_t count)
7974{
7975 int ant;
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007976 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07007977
7978 if (count == 0)
7979 return 0;
7980
7981 if (sscanf(buf, "%1i", &ant) != 1) {
7982 IWL_DEBUG_INFO("not in hex or decimal form.\n");
7983 return count;
7984 }
7985
7986 if ((ant >= 0) && (ant <= 2)) {
7987 IWL_DEBUG_INFO("Setting antenna select to %d.\n", ant);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007988 priv->antenna = (enum iwl4965_antenna)ant;
Zhu Yib481de92007-09-25 17:54:57 -07007989 } else
7990 IWL_DEBUG_INFO("Bad antenna select value %d.\n", ant);
7991
7992
7993 return count;
7994}
7995
7996static DEVICE_ATTR(antenna, S_IWUSR | S_IRUGO, show_antenna, store_antenna);
7997
7998static ssize_t show_status(struct device *d,
7999 struct device_attribute *attr, char *buf)
8000{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07008001 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008002 if (!iwl4965_is_alive(priv))
Zhu Yib481de92007-09-25 17:54:57 -07008003 return -EAGAIN;
8004 return sprintf(buf, "0x%08x\n", (int)priv->status);
8005}
8006
8007static DEVICE_ATTR(status, S_IRUGO, show_status, NULL);
8008
8009static ssize_t dump_error_log(struct device *d,
8010 struct device_attribute *attr,
8011 const char *buf, size_t count)
8012{
8013 char *p = (char *)buf;
8014
8015 if (p[0] == '1')
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07008016 iwl4965_dump_nic_error_log((struct iwl_priv *)d->driver_data);
Zhu Yib481de92007-09-25 17:54:57 -07008017
8018 return strnlen(buf, count);
8019}
8020
8021static DEVICE_ATTR(dump_errors, S_IWUSR, NULL, dump_error_log);
8022
8023static ssize_t dump_event_log(struct device *d,
8024 struct device_attribute *attr,
8025 const char *buf, size_t count)
8026{
8027 char *p = (char *)buf;
8028
8029 if (p[0] == '1')
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07008030 iwl4965_dump_nic_event_log((struct iwl_priv *)d->driver_data);
Zhu Yib481de92007-09-25 17:54:57 -07008031
8032 return strnlen(buf, count);
8033}
8034
8035static DEVICE_ATTR(dump_events, S_IWUSR, NULL, dump_event_log);
8036
8037/*****************************************************************************
8038 *
8039 * driver setup and teardown
8040 *
8041 *****************************************************************************/
8042
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07008043static void iwl4965_setup_deferred_work(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07008044{
8045 priv->workqueue = create_workqueue(DRV_NAME);
8046
8047 init_waitqueue_head(&priv->wait_command_queue);
8048
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008049 INIT_WORK(&priv->up, iwl4965_bg_up);
8050 INIT_WORK(&priv->restart, iwl4965_bg_restart);
8051 INIT_WORK(&priv->rx_replenish, iwl4965_bg_rx_replenish);
8052 INIT_WORK(&priv->scan_completed, iwl4965_bg_scan_completed);
8053 INIT_WORK(&priv->request_scan, iwl4965_bg_request_scan);
8054 INIT_WORK(&priv->abort_scan, iwl4965_bg_abort_scan);
8055 INIT_WORK(&priv->rf_kill, iwl4965_bg_rf_kill);
8056 INIT_WORK(&priv->beacon_update, iwl4965_bg_beacon_update);
8057 INIT_DELAYED_WORK(&priv->post_associate, iwl4965_bg_post_associate);
8058 INIT_DELAYED_WORK(&priv->init_alive_start, iwl4965_bg_init_alive_start);
8059 INIT_DELAYED_WORK(&priv->alive_start, iwl4965_bg_alive_start);
8060 INIT_DELAYED_WORK(&priv->scan_check, iwl4965_bg_scan_check);
Zhu Yib481de92007-09-25 17:54:57 -07008061
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008062 iwl4965_hw_setup_deferred_work(priv);
Zhu Yib481de92007-09-25 17:54:57 -07008063
8064 tasklet_init(&priv->irq_tasklet, (void (*)(unsigned long))
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008065 iwl4965_irq_tasklet, (unsigned long)priv);
Zhu Yib481de92007-09-25 17:54:57 -07008066}
8067
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07008068static void iwl4965_cancel_deferred_work(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07008069{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008070 iwl4965_hw_cancel_deferred_work(priv);
Zhu Yib481de92007-09-25 17:54:57 -07008071
Joonwoo Park3ae6a052007-11-29 10:43:16 +09008072 cancel_delayed_work_sync(&priv->init_alive_start);
Zhu Yib481de92007-09-25 17:54:57 -07008073 cancel_delayed_work(&priv->scan_check);
8074 cancel_delayed_work(&priv->alive_start);
8075 cancel_delayed_work(&priv->post_associate);
8076 cancel_work_sync(&priv->beacon_update);
8077}
8078
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008079static struct attribute *iwl4965_sysfs_entries[] = {
Zhu Yib481de92007-09-25 17:54:57 -07008080 &dev_attr_antenna.attr,
8081 &dev_attr_channels.attr,
8082 &dev_attr_dump_errors.attr,
8083 &dev_attr_dump_events.attr,
8084 &dev_attr_flags.attr,
8085 &dev_attr_filter_flags.attr,
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08008086#ifdef CONFIG_IWL4965_SPECTRUM_MEASUREMENT
Zhu Yib481de92007-09-25 17:54:57 -07008087 &dev_attr_measurement.attr,
8088#endif
8089 &dev_attr_power_level.attr,
8090 &dev_attr_retry_rate.attr,
8091 &dev_attr_rf_kill.attr,
8092 &dev_attr_rs_window.attr,
8093 &dev_attr_statistics.attr,
8094 &dev_attr_status.attr,
8095 &dev_attr_temperature.attr,
Zhu Yib481de92007-09-25 17:54:57 -07008096 &dev_attr_tx_power.attr,
8097
8098 NULL
8099};
8100
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008101static struct attribute_group iwl4965_attribute_group = {
Zhu Yib481de92007-09-25 17:54:57 -07008102 .name = NULL, /* put in device directory */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008103 .attrs = iwl4965_sysfs_entries,
Zhu Yib481de92007-09-25 17:54:57 -07008104};
8105
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008106static struct ieee80211_ops iwl4965_hw_ops = {
8107 .tx = iwl4965_mac_tx,
8108 .start = iwl4965_mac_start,
8109 .stop = iwl4965_mac_stop,
8110 .add_interface = iwl4965_mac_add_interface,
8111 .remove_interface = iwl4965_mac_remove_interface,
8112 .config = iwl4965_mac_config,
8113 .config_interface = iwl4965_mac_config_interface,
8114 .configure_filter = iwl4965_configure_filter,
8115 .set_key = iwl4965_mac_set_key,
8116 .get_stats = iwl4965_mac_get_stats,
8117 .get_tx_stats = iwl4965_mac_get_tx_stats,
8118 .conf_tx = iwl4965_mac_conf_tx,
8119 .get_tsf = iwl4965_mac_get_tsf,
8120 .reset_tsf = iwl4965_mac_reset_tsf,
8121 .beacon_update = iwl4965_mac_beacon_update,
Johannes Berg471b3ef2007-12-28 14:32:58 +01008122 .bss_info_changed = iwl4965_bss_info_changed,
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08008123#ifdef CONFIG_IWL4965_HT
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008124 .conf_ht = iwl4965_mac_conf_ht,
Ron Rindjunsky9ab46172007-12-25 17:00:38 +02008125 .ampdu_action = iwl4965_mac_ampdu_action,
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08008126#endif /* CONFIG_IWL4965_HT */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008127 .hw_scan = iwl4965_mac_hw_scan
Zhu Yib481de92007-09-25 17:54:57 -07008128};
8129
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008130static int iwl4965_pci_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
Zhu Yib481de92007-09-25 17:54:57 -07008131{
8132 int err = 0;
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07008133 struct iwl_priv *priv;
Zhu Yib481de92007-09-25 17:54:57 -07008134 struct ieee80211_hw *hw;
Tomas Winkler82b9a122008-03-04 18:09:30 -08008135 struct iwl_cfg *cfg = (struct iwl_cfg *)(ent->driver_data);
Zhu Yi5a66926a2008-01-14 17:46:18 -08008136 DECLARE_MAC_BUF(mac);
Zhu Yib481de92007-09-25 17:54:57 -07008137
Assaf Krauss316c30d2008-03-14 10:38:46 -07008138 /************************
8139 * 1. Allocating HW data
8140 ************************/
8141
Cahill, Ben M6440adb2007-11-29 11:09:55 +08008142 /* Disabling hardware scan means that mac80211 will perform scans
8143 * "the hard way", rather than using device's scan. */
Assaf Krauss00acbc92008-03-14 10:38:47 -07008144 if (iwl4965_mod_params.disable_hw_scan) {
Zhu Yib481de92007-09-25 17:54:57 -07008145 IWL_DEBUG_INFO("Disabling hw_scan\n");
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008146 iwl4965_hw_ops.hw_scan = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07008147 }
8148
Assaf Krauss1d0a0822008-03-14 10:38:48 -07008149 hw = iwl_alloc_all(cfg, &iwl4965_hw_ops);
8150 if (!hw) {
Zhu Yib481de92007-09-25 17:54:57 -07008151 err = -ENOMEM;
8152 goto out;
8153 }
Assaf Krauss1d0a0822008-03-14 10:38:48 -07008154 priv = hw->priv;
8155 /* At this point both hw and priv are allocated. */
8156
Zhu Yib481de92007-09-25 17:54:57 -07008157 SET_IEEE80211_DEV(hw, &pdev->dev);
8158
8159 IWL_DEBUG_INFO("*** LOAD DRIVER ***\n");
Tomas Winkler82b9a122008-03-04 18:09:30 -08008160 priv->cfg = cfg;
Zhu Yib481de92007-09-25 17:54:57 -07008161 priv->pci_dev = pdev;
Assaf Krauss316c30d2008-03-14 10:38:46 -07008162
Tomas Winkler0a6857e2008-03-12 16:58:49 -07008163#ifdef CONFIG_IWLWIFI_DEBUG
Assaf Krauss00acbc92008-03-14 10:38:47 -07008164 iwl_debug_level = iwl4965_mod_params.debug;
Zhu Yib481de92007-09-25 17:54:57 -07008165 atomic_set(&priv->restrict_refcnt, 0);
8166#endif
Zhu Yib481de92007-09-25 17:54:57 -07008167
Assaf Krauss316c30d2008-03-14 10:38:46 -07008168 /**************************
8169 * 2. Initializing PCI bus
8170 **************************/
8171 if (pci_enable_device(pdev)) {
8172 err = -ENODEV;
8173 goto out_ieee80211_free_hw;
8174 }
8175
8176 pci_set_master(pdev);
8177
8178 err = pci_set_dma_mask(pdev, DMA_32BIT_MASK);
8179 if (!err)
8180 err = pci_set_consistent_dma_mask(pdev, DMA_32BIT_MASK);
8181 if (err) {
8182 printk(KERN_WARNING DRV_NAME
8183 ": No suitable DMA available.\n");
8184 goto out_pci_disable_device;
8185 }
8186
8187 err = pci_request_regions(pdev, DRV_NAME);
8188 if (err)
8189 goto out_pci_disable_device;
8190
8191 pci_set_drvdata(pdev, priv);
8192
8193 /* We disable the RETRY_TIMEOUT register (0x41) to keep
8194 * PCI Tx retries from interfering with C3 CPU state */
8195 pci_write_config_byte(pdev, 0x41, 0x00);
8196
8197 /***********************
8198 * 3. Read REV register
8199 ***********************/
8200 priv->hw_base = pci_iomap(pdev, 0, 0);
8201 if (!priv->hw_base) {
8202 err = -ENODEV;
8203 goto out_pci_release_regions;
8204 }
8205
8206 IWL_DEBUG_INFO("pci_resource_len = 0x%08llx\n",
8207 (unsigned long long) pci_resource_len(pdev, 0));
8208 IWL_DEBUG_INFO("pci_resource_base = %p\n", priv->hw_base);
8209
8210 printk(KERN_INFO DRV_NAME
8211 ": Detected Intel Wireless WiFi Link %s\n", priv->cfg->name);
8212
8213 /*****************
8214 * 4. Read EEPROM
8215 *****************/
8216 /* nic init */
8217 iwl4965_set_bit(priv, CSR_GIO_CHICKEN_BITS,
8218 CSR_GIO_CHICKEN_BITS_REG_BIT_DIS_L0S_EXIT_TIMER);
8219
8220 iwl4965_set_bit(priv, CSR_GP_CNTRL, CSR_GP_CNTRL_REG_FLAG_INIT_DONE);
8221 err = iwl4965_poll_bit(priv, CSR_GP_CNTRL,
8222 CSR_GP_CNTRL_REG_FLAG_MAC_CLOCK_READY,
8223 CSR_GP_CNTRL_REG_FLAG_MAC_CLOCK_READY, 25000);
8224 if (err < 0) {
8225 IWL_DEBUG_INFO("Failed to init the card\n");
8226 goto out_iounmap;
8227 }
8228 /* Read the EEPROM */
8229 err = iwl_eeprom_init(priv);
8230 if (err) {
8231 IWL_ERROR("Unable to init EEPROM\n");
8232 goto out_iounmap;
8233 }
8234 /* MAC Address location in EEPROM same for 3945/4965 */
8235 iwl_eeprom_get_mac(priv, priv->mac_addr);
8236 IWL_DEBUG_INFO("MAC address: %s\n", print_mac(mac, priv->mac_addr));
8237 SET_IEEE80211_PERM_ADDR(priv->hw, priv->mac_addr);
8238
8239 /************************
8240 * 5. Setup HW constants
8241 ************************/
8242 /* Device-specific setup */
8243 if (iwl4965_hw_set_hw_setting(priv)) {
8244 IWL_ERROR("failed to set hw settings\n");
8245 goto out_iounmap;
8246 }
8247
8248 /*******************
8249 * 6. Setup hw/priv
8250 *******************/
Zhu Yib481de92007-09-25 17:54:57 -07008251
Assaf Kraussbf85ea42008-03-14 10:38:49 -07008252 err = iwl_setup(priv);
8253 if (err)
Assaf Krauss316c30d2008-03-14 10:38:46 -07008254 goto out_unset_hw_settings;
Assaf Kraussbf85ea42008-03-14 10:38:49 -07008255 /* At this point both hw and priv are initialized. */
Assaf Krauss316c30d2008-03-14 10:38:46 -07008256
8257 /**********************************
8258 * 7. Initialize module parameters
8259 **********************************/
8260
8261 /* Disable radio (SW RF KILL) via parameter when loading driver */
Assaf Krauss00acbc92008-03-14 10:38:47 -07008262 if (iwl4965_mod_params.disable) {
Assaf Krauss316c30d2008-03-14 10:38:46 -07008263 set_bit(STATUS_RF_KILL_SW, &priv->status);
8264 IWL_DEBUG_INFO("Radio disabled.\n");
8265 }
8266
Assaf Krauss00acbc92008-03-14 10:38:47 -07008267 if (iwl4965_mod_params.enable_qos)
Assaf Krauss316c30d2008-03-14 10:38:46 -07008268 priv->qos_data.qos_enable = 1;
8269
8270 /********************
8271 * 8. Setup services
8272 ********************/
8273 iwl4965_disable_interrupts(priv);
8274
8275 err = sysfs_create_group(&pdev->dev.kobj, &iwl4965_attribute_group);
8276 if (err) {
8277 IWL_ERROR("failed to create sysfs device attributes\n");
Assaf Kraussbf85ea42008-03-14 10:38:49 -07008278 goto out_unset_hw_settings;
Assaf Krauss316c30d2008-03-14 10:38:46 -07008279 }
8280
8281 err = iwl_dbgfs_register(priv, DRV_NAME);
8282 if (err) {
8283 IWL_ERROR("failed to create debugfs files\n");
8284 goto out_remove_sysfs;
8285 }
8286
8287 iwl4965_setup_deferred_work(priv);
8288 iwl4965_setup_rx_handlers(priv);
8289
8290 /********************
8291 * 9. Conclude
8292 ********************/
Zhu Yi5a66926a2008-01-14 17:46:18 -08008293 pci_save_state(pdev);
8294 pci_disable_device(pdev);
Zhu Yib481de92007-09-25 17:54:57 -07008295
8296 return 0;
8297
Assaf Krauss316c30d2008-03-14 10:38:46 -07008298 out_remove_sysfs:
8299 sysfs_remove_group(&pdev->dev.kobj, &iwl4965_attribute_group);
Assaf Krauss316c30d2008-03-14 10:38:46 -07008300 out_unset_hw_settings:
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008301 iwl4965_unset_hw_setting(priv);
Zhu Yib481de92007-09-25 17:54:57 -07008302 out_iounmap:
8303 pci_iounmap(pdev, priv->hw_base);
8304 out_pci_release_regions:
8305 pci_release_regions(pdev);
Assaf Krauss316c30d2008-03-14 10:38:46 -07008306 pci_set_drvdata(pdev, NULL);
Zhu Yib481de92007-09-25 17:54:57 -07008307 out_pci_disable_device:
8308 pci_disable_device(pdev);
Zhu Yib481de92007-09-25 17:54:57 -07008309 out_ieee80211_free_hw:
8310 ieee80211_free_hw(priv->hw);
8311 out:
8312 return err;
8313}
8314
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008315static void iwl4965_pci_remove(struct pci_dev *pdev)
Zhu Yib481de92007-09-25 17:54:57 -07008316{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07008317 struct iwl_priv *priv = pci_get_drvdata(pdev);
Zhu Yib481de92007-09-25 17:54:57 -07008318 struct list_head *p, *q;
8319 int i;
8320
8321 if (!priv)
8322 return;
8323
8324 IWL_DEBUG_INFO("*** UNLOAD DRIVER ***\n");
8325
Zhu Yib481de92007-09-25 17:54:57 -07008326 set_bit(STATUS_EXIT_PENDING, &priv->status);
Zhu Yib24d22b2007-12-19 13:59:52 +08008327
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008328 iwl4965_down(priv);
Zhu Yib481de92007-09-25 17:54:57 -07008329
8330 /* Free MAC hash list for ADHOC */
8331 for (i = 0; i < IWL_IBSS_MAC_HASH_SIZE; i++) {
8332 list_for_each_safe(p, q, &priv->ibss_mac_hash[i]) {
8333 list_del(p);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008334 kfree(list_entry(p, struct iwl4965_ibss_seq, list));
Zhu Yib481de92007-09-25 17:54:57 -07008335 }
8336 }
8337
Tomas Winkler712b6cf2008-03-12 16:58:52 -07008338 iwl_dbgfs_unregister(priv);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008339 sysfs_remove_group(&pdev->dev.kobj, &iwl4965_attribute_group);
Zhu Yib481de92007-09-25 17:54:57 -07008340
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008341 iwl4965_dealloc_ucode_pci(priv);
Zhu Yib481de92007-09-25 17:54:57 -07008342
8343 if (priv->rxq.bd)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008344 iwl4965_rx_queue_free(priv, &priv->rxq);
8345 iwl4965_hw_txq_ctx_free(priv);
Zhu Yib481de92007-09-25 17:54:57 -07008346
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008347 iwl4965_unset_hw_setting(priv);
Assaf Kraussbf85ea42008-03-14 10:38:49 -07008348 iwlcore_clear_stations_table(priv);
Zhu Yib481de92007-09-25 17:54:57 -07008349
8350 if (priv->mac80211_registered) {
8351 ieee80211_unregister_hw(priv->hw);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008352 iwl4965_rate_control_unregister(priv->hw);
Zhu Yib481de92007-09-25 17:54:57 -07008353 }
8354
Mohamed Abbas948c1712007-10-25 17:15:45 +08008355 /*netif_stop_queue(dev); */
8356 flush_workqueue(priv->workqueue);
8357
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008358 /* ieee80211_unregister_hw calls iwl4965_mac_stop, which flushes
Zhu Yib481de92007-09-25 17:54:57 -07008359 * priv->workqueue... so we can't take down the workqueue
8360 * until now... */
8361 destroy_workqueue(priv->workqueue);
8362 priv->workqueue = NULL;
8363
Zhu Yib481de92007-09-25 17:54:57 -07008364 pci_iounmap(pdev, priv->hw_base);
8365 pci_release_regions(pdev);
8366 pci_disable_device(pdev);
8367 pci_set_drvdata(pdev, NULL);
8368
Assaf Kraussbf85ea42008-03-14 10:38:49 -07008369 iwl_free_channel_map(priv);
Reinette Chatre849e0dc2008-01-23 10:15:18 -08008370 iwl4965_free_geos(priv);
Zhu Yib481de92007-09-25 17:54:57 -07008371
8372 if (priv->ibss_beacon)
8373 dev_kfree_skb(priv->ibss_beacon);
8374
8375 ieee80211_free_hw(priv->hw);
8376}
8377
8378#ifdef CONFIG_PM
8379
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008380static int iwl4965_pci_suspend(struct pci_dev *pdev, pm_message_t state)
Zhu Yib481de92007-09-25 17:54:57 -07008381{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07008382 struct iwl_priv *priv = pci_get_drvdata(pdev);
Zhu Yib481de92007-09-25 17:54:57 -07008383
Zhu Yie655b9f2008-01-24 02:19:38 -08008384 if (priv->is_open) {
8385 set_bit(STATUS_IN_SUSPEND, &priv->status);
8386 iwl4965_mac_stop(priv->hw);
8387 priv->is_open = 1;
8388 }
Zhu Yib481de92007-09-25 17:54:57 -07008389
Zhu Yib481de92007-09-25 17:54:57 -07008390 pci_set_power_state(pdev, PCI_D3hot);
8391
Zhu Yib481de92007-09-25 17:54:57 -07008392 return 0;
8393}
8394
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008395static int iwl4965_pci_resume(struct pci_dev *pdev)
Zhu Yib481de92007-09-25 17:54:57 -07008396{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07008397 struct iwl_priv *priv = pci_get_drvdata(pdev);
Zhu Yib481de92007-09-25 17:54:57 -07008398
Zhu Yib481de92007-09-25 17:54:57 -07008399 pci_set_power_state(pdev, PCI_D0);
Zhu Yib481de92007-09-25 17:54:57 -07008400
Zhu Yie655b9f2008-01-24 02:19:38 -08008401 if (priv->is_open)
8402 iwl4965_mac_start(priv->hw);
Zhu Yib481de92007-09-25 17:54:57 -07008403
Zhu Yie655b9f2008-01-24 02:19:38 -08008404 clear_bit(STATUS_IN_SUSPEND, &priv->status);
Zhu Yib481de92007-09-25 17:54:57 -07008405 return 0;
8406}
8407
8408#endif /* CONFIG_PM */
8409
8410/*****************************************************************************
8411 *
8412 * driver and module entry point
8413 *
8414 *****************************************************************************/
8415
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008416static struct pci_driver iwl4965_driver = {
Zhu Yib481de92007-09-25 17:54:57 -07008417 .name = DRV_NAME,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008418 .id_table = iwl4965_hw_card_ids,
8419 .probe = iwl4965_pci_probe,
8420 .remove = __devexit_p(iwl4965_pci_remove),
Zhu Yib481de92007-09-25 17:54:57 -07008421#ifdef CONFIG_PM
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008422 .suspend = iwl4965_pci_suspend,
8423 .resume = iwl4965_pci_resume,
Zhu Yib481de92007-09-25 17:54:57 -07008424#endif
8425};
8426
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008427static int __init iwl4965_init(void)
Zhu Yib481de92007-09-25 17:54:57 -07008428{
8429
8430 int ret;
8431 printk(KERN_INFO DRV_NAME ": " DRV_DESCRIPTION ", " DRV_VERSION "\n");
8432 printk(KERN_INFO DRV_NAME ": " DRV_COPYRIGHT "\n");
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008433 ret = pci_register_driver(&iwl4965_driver);
Zhu Yib481de92007-09-25 17:54:57 -07008434 if (ret) {
8435 IWL_ERROR("Unable to initialize PCI module\n");
8436 return ret;
8437 }
Tomas Winkler0a6857e2008-03-12 16:58:49 -07008438#ifdef CONFIG_IWLWIFI_DEBUG
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008439 ret = driver_create_file(&iwl4965_driver.driver, &driver_attr_debug_level);
Zhu Yib481de92007-09-25 17:54:57 -07008440 if (ret) {
8441 IWL_ERROR("Unable to create driver sysfs file\n");
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008442 pci_unregister_driver(&iwl4965_driver);
Zhu Yib481de92007-09-25 17:54:57 -07008443 return ret;
8444 }
8445#endif
8446
8447 return ret;
8448}
8449
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008450static void __exit iwl4965_exit(void)
Zhu Yib481de92007-09-25 17:54:57 -07008451{
Tomas Winkler0a6857e2008-03-12 16:58:49 -07008452#ifdef CONFIG_IWLWIFI_DEBUG
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008453 driver_remove_file(&iwl4965_driver.driver, &driver_attr_debug_level);
Zhu Yib481de92007-09-25 17:54:57 -07008454#endif
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008455 pci_unregister_driver(&iwl4965_driver);
Zhu Yib481de92007-09-25 17:54:57 -07008456}
8457
Assaf Krauss00acbc92008-03-14 10:38:47 -07008458module_param_named(antenna, iwl4965_mod_params.antenna, int, 0444);
Zhu Yib481de92007-09-25 17:54:57 -07008459MODULE_PARM_DESC(antenna, "select antenna (1=Main, 2=Aux, default 0 [both])");
Assaf Krauss00acbc92008-03-14 10:38:47 -07008460module_param_named(disable, iwl4965_mod_params.disable, int, 0444);
Zhu Yib481de92007-09-25 17:54:57 -07008461MODULE_PARM_DESC(disable, "manually disable the radio (default 0 [radio on])");
Assaf Krauss00acbc92008-03-14 10:38:47 -07008462module_param_named(hwcrypto, iwl4965_mod_params.hw_crypto, int, 0444);
Zhu Yib481de92007-09-25 17:54:57 -07008463MODULE_PARM_DESC(hwcrypto,
8464 "using hardware crypto engine (default 0 [software])\n");
Assaf Krauss00acbc92008-03-14 10:38:47 -07008465module_param_named(debug, iwl4965_mod_params.debug, int, 0444);
Zhu Yib481de92007-09-25 17:54:57 -07008466MODULE_PARM_DESC(debug, "debug output mask");
Assaf Krauss00acbc92008-03-14 10:38:47 -07008467module_param_named(
8468 disable_hw_scan, iwl4965_mod_params.disable_hw_scan, int, 0444);
Zhu Yib481de92007-09-25 17:54:57 -07008469MODULE_PARM_DESC(disable_hw_scan, "disable hardware scanning (default 0)");
8470
Assaf Krauss00acbc92008-03-14 10:38:47 -07008471module_param_named(queues_num, iwl4965_mod_params.num_of_queues, int, 0444);
Zhu Yib481de92007-09-25 17:54:57 -07008472MODULE_PARM_DESC(queues_num, "number of hw queues.");
8473
8474/* QoS */
Assaf Krauss00acbc92008-03-14 10:38:47 -07008475module_param_named(qos_enable, iwl4965_mod_params.enable_qos, int, 0444);
Zhu Yib481de92007-09-25 17:54:57 -07008476MODULE_PARM_DESC(qos_enable, "enable all QoS functionality");
Assaf Krauss00acbc92008-03-14 10:38:47 -07008477module_param_named(amsdu_size_8K, iwl4965_mod_params.amsdu_size_8K, int, 0444);
Ron Rindjunsky9ee1ba42007-11-26 16:14:42 +02008478MODULE_PARM_DESC(amsdu_size_8K, "enable 8K amsdu size");
Zhu Yib481de92007-09-25 17:54:57 -07008479
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008480module_exit(iwl4965_exit);
8481module_init(iwl4965_init);