blob: bd5375984045671569e0ab2596182ba6fd0b23ff [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{
Emmanuel Grumbach2bc75082008-03-19 16:41:45 -07001396 unsigned long flags;
1397 int ret = 0;
1398
1399 keyconf->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
1400 keyconf->flags |= IEEE80211_KEY_FLAG_GENERATE_MMIC;
1401 keyconf->hw_key_idx = keyconf->keyidx;
1402
1403 spin_lock_irqsave(&priv->sta_lock, flags);
1404
1405 priv->stations[sta_id].keyinfo.alg = keyconf->alg;
1406 priv->stations[sta_id].keyinfo.conf = keyconf;
1407 priv->stations[sta_id].keyinfo.keylen = 16;
1408
1409 /* This copy is acutally not needed: we get the key with each TX */
1410 memcpy(priv->stations[sta_id].keyinfo.key, keyconf->key, 16);
1411
1412 memcpy(priv->stations[sta_id].sta.key.key, keyconf->key, 16);
1413
1414 spin_unlock_irqrestore(&priv->sta_lock, flags);
1415
1416 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07001417}
1418
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001419static int iwl4965_clear_sta_key_info(struct iwl_priv *priv, u8 sta_id)
Zhu Yib481de92007-09-25 17:54:57 -07001420{
1421 unsigned long flags;
1422
1423 spin_lock_irqsave(&priv->sta_lock, flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001424 memset(&priv->stations[sta_id].keyinfo, 0, sizeof(struct iwl4965_hw_key));
1425 memset(&priv->stations[sta_id].sta.key, 0, sizeof(struct iwl4965_keyinfo));
Zhu Yib481de92007-09-25 17:54:57 -07001426 priv->stations[sta_id].sta.key.key_flags = STA_KEY_FLG_NO_ENC;
1427 priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
1428 priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
1429 spin_unlock_irqrestore(&priv->sta_lock, flags);
1430
1431 IWL_DEBUG_INFO("hwcrypto: clear ucode station key info\n");
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001432 iwl4965_send_add_station(priv, &priv->stations[sta_id].sta, 0);
Zhu Yib481de92007-09-25 17:54:57 -07001433 return 0;
1434}
1435
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07001436static int iwl4965_set_dynamic_key(struct iwl_priv *priv,
1437 struct ieee80211_key_conf *key, u8 sta_id)
1438{
1439 int ret;
1440
1441 switch (key->alg) {
1442 case ALG_CCMP:
1443 ret = iwl4965_set_ccmp_dynamic_key_info(priv, key, sta_id);
1444 break;
1445 case ALG_TKIP:
1446 ret = iwl4965_set_tkip_dynamic_key_info(priv, key, sta_id);
1447 break;
1448 case ALG_WEP:
1449 ret = -EOPNOTSUPP;
1450 break;
1451 default:
1452 IWL_ERROR("Unknown alg: %s alg = %d\n", __func__, key->alg);
1453 ret = -EINVAL;
1454 }
1455
1456 return ret;
1457}
1458
1459static int iwl4965_remove_static_key(struct iwl_priv *priv)
1460{
1461 int ret = -EOPNOTSUPP;
1462
1463 return ret;
1464}
1465
1466static int iwl4965_set_static_key(struct iwl_priv *priv,
1467 struct ieee80211_key_conf *key)
1468{
1469 if (key->alg == ALG_WEP)
1470 return -EOPNOTSUPP;
1471
1472 IWL_ERROR("Static key invalid: alg %d\n", key->alg);
1473 return -EINVAL;
1474}
1475
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001476static void iwl4965_clear_free_frames(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001477{
1478 struct list_head *element;
1479
1480 IWL_DEBUG_INFO("%d frames on pre-allocated heap on clear.\n",
1481 priv->frames_count);
1482
1483 while (!list_empty(&priv->free_frames)) {
1484 element = priv->free_frames.next;
1485 list_del(element);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001486 kfree(list_entry(element, struct iwl4965_frame, list));
Zhu Yib481de92007-09-25 17:54:57 -07001487 priv->frames_count--;
1488 }
1489
1490 if (priv->frames_count) {
1491 IWL_WARNING("%d frames still in use. Did we lose one?\n",
1492 priv->frames_count);
1493 priv->frames_count = 0;
1494 }
1495}
1496
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001497static struct iwl4965_frame *iwl4965_get_free_frame(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001498{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001499 struct iwl4965_frame *frame;
Zhu Yib481de92007-09-25 17:54:57 -07001500 struct list_head *element;
1501 if (list_empty(&priv->free_frames)) {
1502 frame = kzalloc(sizeof(*frame), GFP_KERNEL);
1503 if (!frame) {
1504 IWL_ERROR("Could not allocate frame!\n");
1505 return NULL;
1506 }
1507
1508 priv->frames_count++;
1509 return frame;
1510 }
1511
1512 element = priv->free_frames.next;
1513 list_del(element);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001514 return list_entry(element, struct iwl4965_frame, list);
Zhu Yib481de92007-09-25 17:54:57 -07001515}
1516
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001517static void iwl4965_free_frame(struct iwl_priv *priv, struct iwl4965_frame *frame)
Zhu Yib481de92007-09-25 17:54:57 -07001518{
1519 memset(frame, 0, sizeof(*frame));
1520 list_add(&frame->list, &priv->free_frames);
1521}
1522
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001523unsigned int iwl4965_fill_beacon_frame(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07001524 struct ieee80211_hdr *hdr,
1525 const u8 *dest, int left)
1526{
1527
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001528 if (!iwl4965_is_associated(priv) || !priv->ibss_beacon ||
Zhu Yib481de92007-09-25 17:54:57 -07001529 ((priv->iw_mode != IEEE80211_IF_TYPE_IBSS) &&
1530 (priv->iw_mode != IEEE80211_IF_TYPE_AP)))
1531 return 0;
1532
1533 if (priv->ibss_beacon->len > left)
1534 return 0;
1535
1536 memcpy(hdr, priv->ibss_beacon->data, priv->ibss_beacon->len);
1537
1538 return priv->ibss_beacon->len;
1539}
1540
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001541static u8 iwl4965_rate_get_lowest_plcp(int rate_mask)
Zhu Yib481de92007-09-25 17:54:57 -07001542{
1543 u8 i;
1544
1545 for (i = IWL_RATE_1M_INDEX; i != IWL_RATE_INVALID;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001546 i = iwl4965_rates[i].next_ieee) {
Zhu Yib481de92007-09-25 17:54:57 -07001547 if (rate_mask & (1 << i))
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001548 return iwl4965_rates[i].plcp;
Zhu Yib481de92007-09-25 17:54:57 -07001549 }
1550
1551 return IWL_RATE_INVALID;
1552}
1553
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001554static int iwl4965_send_beacon_cmd(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001555{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001556 struct iwl4965_frame *frame;
Zhu Yib481de92007-09-25 17:54:57 -07001557 unsigned int frame_size;
1558 int rc;
1559 u8 rate;
1560
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001561 frame = iwl4965_get_free_frame(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001562
1563 if (!frame) {
1564 IWL_ERROR("Could not obtain free frame buffer for beacon "
1565 "command.\n");
1566 return -ENOMEM;
1567 }
1568
1569 if (!(priv->staging_rxon.flags & RXON_FLG_BAND_24G_MSK)) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001570 rate = iwl4965_rate_get_lowest_plcp(priv->active_rate_basic &
Zhu Yib481de92007-09-25 17:54:57 -07001571 0xFF0);
1572 if (rate == IWL_INVALID_RATE)
1573 rate = IWL_RATE_6M_PLCP;
1574 } else {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001575 rate = iwl4965_rate_get_lowest_plcp(priv->active_rate_basic & 0xF);
Zhu Yib481de92007-09-25 17:54:57 -07001576 if (rate == IWL_INVALID_RATE)
1577 rate = IWL_RATE_1M_PLCP;
1578 }
1579
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001580 frame_size = iwl4965_hw_get_beacon_cmd(priv, frame, rate);
Zhu Yib481de92007-09-25 17:54:57 -07001581
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001582 rc = iwl4965_send_cmd_pdu(priv, REPLY_TX_BEACON, frame_size,
Zhu Yib481de92007-09-25 17:54:57 -07001583 &frame->u.cmd[0]);
1584
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001585 iwl4965_free_frame(priv, frame);
Zhu Yib481de92007-09-25 17:54:57 -07001586
1587 return rc;
1588}
1589
1590/******************************************************************************
1591 *
Zhu Yib481de92007-09-25 17:54:57 -07001592 * Misc. internal state and helper functions
1593 *
1594 ******************************************************************************/
Zhu Yib481de92007-09-25 17:54:57 -07001595
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001596static void iwl4965_unset_hw_setting(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001597{
1598 if (priv->hw_setting.shared_virt)
1599 pci_free_consistent(priv->pci_dev,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001600 sizeof(struct iwl4965_shared),
Zhu Yib481de92007-09-25 17:54:57 -07001601 priv->hw_setting.shared_virt,
1602 priv->hw_setting.shared_phys);
1603}
1604
1605/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001606 * iwl4965_supported_rate_to_ie - fill in the supported rate in IE field
Zhu Yib481de92007-09-25 17:54:57 -07001607 *
1608 * return : set the bit for each supported rate insert in ie
1609 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001610static u16 iwl4965_supported_rate_to_ie(u8 *ie, u16 supported_rate,
Tomas Winklerc7c46672007-10-18 02:04:15 +02001611 u16 basic_rate, int *left)
Zhu Yib481de92007-09-25 17:54:57 -07001612{
1613 u16 ret_rates = 0, bit;
1614 int i;
Tomas Winklerc7c46672007-10-18 02:04:15 +02001615 u8 *cnt = ie;
1616 u8 *rates = ie + 1;
Zhu Yib481de92007-09-25 17:54:57 -07001617
1618 for (bit = 1, i = 0; i < IWL_RATE_COUNT; i++, bit <<= 1) {
1619 if (bit & supported_rate) {
1620 ret_rates |= bit;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001621 rates[*cnt] = iwl4965_rates[i].ieee |
Tomas Winklerc7c46672007-10-18 02:04:15 +02001622 ((bit & basic_rate) ? 0x80 : 0x00);
1623 (*cnt)++;
1624 (*left)--;
1625 if ((*left <= 0) ||
1626 (*cnt >= IWL_SUPPORTED_RATES_IE_LEN))
Zhu Yib481de92007-09-25 17:54:57 -07001627 break;
1628 }
1629 }
1630
1631 return ret_rates;
1632}
1633
Zhu Yib481de92007-09-25 17:54:57 -07001634/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001635 * iwl4965_fill_probe_req - fill in all required fields and IE for probe request
Zhu Yib481de92007-09-25 17:54:57 -07001636 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001637static u16 iwl4965_fill_probe_req(struct iwl_priv *priv,
Tomas Winkler78330fd2008-02-06 02:37:18 +02001638 enum ieee80211_band band,
1639 struct ieee80211_mgmt *frame,
1640 int left, int is_direct)
Zhu Yib481de92007-09-25 17:54:57 -07001641{
1642 int len = 0;
1643 u8 *pos = NULL;
mabbasbee488d2007-10-25 17:15:42 +08001644 u16 active_rates, ret_rates, cck_rates, active_rate_basic;
Ron Rindjunsky8fb880322007-11-26 16:14:38 +02001645#ifdef CONFIG_IWL4965_HT
Tomas Winkler78330fd2008-02-06 02:37:18 +02001646 const struct ieee80211_supported_band *sband =
1647 iwl4965_get_hw_mode(priv, band);
Ron Rindjunsky8fb880322007-11-26 16:14:38 +02001648#endif /* CONFIG_IWL4965_HT */
Zhu Yib481de92007-09-25 17:54:57 -07001649
1650 /* Make sure there is enough space for the probe request,
1651 * two mandatory IEs and the data */
1652 left -= 24;
1653 if (left < 0)
1654 return 0;
1655 len += 24;
1656
1657 frame->frame_control = cpu_to_le16(IEEE80211_STYPE_PROBE_REQ);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001658 memcpy(frame->da, iwl4965_broadcast_addr, ETH_ALEN);
Zhu Yib481de92007-09-25 17:54:57 -07001659 memcpy(frame->sa, priv->mac_addr, ETH_ALEN);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001660 memcpy(frame->bssid, iwl4965_broadcast_addr, ETH_ALEN);
Zhu Yib481de92007-09-25 17:54:57 -07001661 frame->seq_ctrl = 0;
1662
1663 /* fill in our indirect SSID IE */
1664 /* ...next IE... */
1665
1666 left -= 2;
1667 if (left < 0)
1668 return 0;
1669 len += 2;
1670 pos = &(frame->u.probe_req.variable[0]);
1671 *pos++ = WLAN_EID_SSID;
1672 *pos++ = 0;
1673
1674 /* fill in our direct SSID IE... */
1675 if (is_direct) {
1676 /* ...next IE... */
1677 left -= 2 + priv->essid_len;
1678 if (left < 0)
1679 return 0;
1680 /* ... fill it in... */
1681 *pos++ = WLAN_EID_SSID;
1682 *pos++ = priv->essid_len;
1683 memcpy(pos, priv->essid, priv->essid_len);
1684 pos += priv->essid_len;
1685 len += 2 + priv->essid_len;
1686 }
1687
1688 /* fill in supported rate */
1689 /* ...next IE... */
1690 left -= 2;
1691 if (left < 0)
1692 return 0;
Tomas Winklerc7c46672007-10-18 02:04:15 +02001693
Zhu Yib481de92007-09-25 17:54:57 -07001694 /* ... fill it in... */
1695 *pos++ = WLAN_EID_SUPP_RATES;
1696 *pos = 0;
Tomas Winklerc7c46672007-10-18 02:04:15 +02001697
mabbasbee488d2007-10-25 17:15:42 +08001698 /* exclude 60M rate */
1699 active_rates = priv->rates_mask;
1700 active_rates &= ~IWL_RATE_60M_MASK;
1701
1702 active_rate_basic = active_rates & IWL_BASIC_RATES_MASK;
Zhu Yib481de92007-09-25 17:54:57 -07001703
Tomas Winklerc7c46672007-10-18 02:04:15 +02001704 cck_rates = IWL_CCK_RATES_MASK & active_rates;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001705 ret_rates = iwl4965_supported_rate_to_ie(pos, cck_rates,
mabbasbee488d2007-10-25 17:15:42 +08001706 active_rate_basic, &left);
Tomas Winklerc7c46672007-10-18 02:04:15 +02001707 active_rates &= ~ret_rates;
1708
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001709 ret_rates = iwl4965_supported_rate_to_ie(pos, active_rates,
mabbasbee488d2007-10-25 17:15:42 +08001710 active_rate_basic, &left);
Tomas Winklerc7c46672007-10-18 02:04:15 +02001711 active_rates &= ~ret_rates;
1712
Zhu Yib481de92007-09-25 17:54:57 -07001713 len += 2 + *pos;
1714 pos += (*pos) + 1;
Tomas Winklerc7c46672007-10-18 02:04:15 +02001715 if (active_rates == 0)
Zhu Yib481de92007-09-25 17:54:57 -07001716 goto fill_end;
1717
1718 /* fill in supported extended rate */
1719 /* ...next IE... */
1720 left -= 2;
1721 if (left < 0)
1722 return 0;
1723 /* ... fill it in... */
1724 *pos++ = WLAN_EID_EXT_SUPP_RATES;
1725 *pos = 0;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001726 iwl4965_supported_rate_to_ie(pos, active_rates,
mabbasbee488d2007-10-25 17:15:42 +08001727 active_rate_basic, &left);
Zhu Yib481de92007-09-25 17:54:57 -07001728 if (*pos > 0)
1729 len += 2 + *pos;
1730
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08001731#ifdef CONFIG_IWL4965_HT
Tomas Winkler78330fd2008-02-06 02:37:18 +02001732 if (sband && sband->ht_info.ht_supported) {
1733 struct ieee80211_ht_cap *ht_cap;
Zhu Yib481de92007-09-25 17:54:57 -07001734 pos += (*pos) + 1;
1735 *pos++ = WLAN_EID_HT_CAPABILITY;
Ron Rindjunsky8fb880322007-11-26 16:14:38 +02001736 *pos++ = sizeof(struct ieee80211_ht_cap);
Tomas Winkler78330fd2008-02-06 02:37:18 +02001737 ht_cap = (struct ieee80211_ht_cap *)pos;
1738 ht_cap->cap_info = cpu_to_le16(sband->ht_info.cap);
1739 memcpy(ht_cap->supp_mcs_set, sband->ht_info.supp_mcs_set, 16);
1740 ht_cap->ampdu_params_info =(sband->ht_info.ampdu_factor &
1741 IEEE80211_HT_CAP_AMPDU_FACTOR) |
1742 ((sband->ht_info.ampdu_density << 2) &
1743 IEEE80211_HT_CAP_AMPDU_DENSITY);
Ron Rindjunsky8fb880322007-11-26 16:14:38 +02001744 len += 2 + sizeof(struct ieee80211_ht_cap);
Zhu Yib481de92007-09-25 17:54:57 -07001745 }
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08001746#endif /*CONFIG_IWL4965_HT */
Zhu Yib481de92007-09-25 17:54:57 -07001747
1748 fill_end:
1749 return (u16)len;
1750}
1751
1752/*
1753 * QoS support
1754*/
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001755static int iwl4965_send_qos_params_command(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001756 struct iwl4965_qosparam_cmd *qos)
Zhu Yib481de92007-09-25 17:54:57 -07001757{
1758
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001759 return iwl4965_send_cmd_pdu(priv, REPLY_QOS_PARAM,
1760 sizeof(struct iwl4965_qosparam_cmd), qos);
Zhu Yib481de92007-09-25 17:54:57 -07001761}
1762
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001763static void iwl4965_activate_qos(struct iwl_priv *priv, u8 force)
Zhu Yib481de92007-09-25 17:54:57 -07001764{
1765 unsigned long flags;
1766
Zhu Yib481de92007-09-25 17:54:57 -07001767 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
1768 return;
1769
1770 if (!priv->qos_data.qos_enable)
1771 return;
1772
1773 spin_lock_irqsave(&priv->lock, flags);
1774 priv->qos_data.def_qos_parm.qos_flags = 0;
1775
1776 if (priv->qos_data.qos_cap.q_AP.queue_request &&
1777 !priv->qos_data.qos_cap.q_AP.txop_request)
1778 priv->qos_data.def_qos_parm.qos_flags |=
1779 QOS_PARAM_FLG_TXOP_TYPE_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07001780 if (priv->qos_data.qos_active)
1781 priv->qos_data.def_qos_parm.qos_flags |=
1782 QOS_PARAM_FLG_UPDATE_EDCA_MSK;
1783
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08001784#ifdef CONFIG_IWL4965_HT
Ron Rindjunskyfd105e72007-11-26 16:14:39 +02001785 if (priv->current_ht_config.is_ht)
Tomas Winklerf1f1f5c2007-10-25 17:15:26 +08001786 priv->qos_data.def_qos_parm.qos_flags |= QOS_PARAM_FLG_TGN_MSK;
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08001787#endif /* CONFIG_IWL4965_HT */
Tomas Winklerf1f1f5c2007-10-25 17:15:26 +08001788
Zhu Yib481de92007-09-25 17:54:57 -07001789 spin_unlock_irqrestore(&priv->lock, flags);
1790
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001791 if (force || iwl4965_is_associated(priv)) {
Tomas Winklerf1f1f5c2007-10-25 17:15:26 +08001792 IWL_DEBUG_QOS("send QoS cmd with Qos active=%d FLAGS=0x%X\n",
1793 priv->qos_data.qos_active,
1794 priv->qos_data.def_qos_parm.qos_flags);
Zhu Yib481de92007-09-25 17:54:57 -07001795
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001796 iwl4965_send_qos_params_command(priv,
Zhu Yib481de92007-09-25 17:54:57 -07001797 &(priv->qos_data.def_qos_parm));
1798 }
1799}
1800
Zhu Yib481de92007-09-25 17:54:57 -07001801/*
1802 * Power management (not Tx power!) functions
1803 */
1804#define MSEC_TO_USEC 1024
1805
1806#define NOSLP __constant_cpu_to_le16(0), 0, 0
1807#define SLP IWL_POWER_DRIVER_ALLOW_SLEEP_MSK, 0, 0
1808#define SLP_TIMEOUT(T) __constant_cpu_to_le32((T) * MSEC_TO_USEC)
1809#define SLP_VEC(X0, X1, X2, X3, X4) {__constant_cpu_to_le32(X0), \
1810 __constant_cpu_to_le32(X1), \
1811 __constant_cpu_to_le32(X2), \
1812 __constant_cpu_to_le32(X3), \
1813 __constant_cpu_to_le32(X4)}
1814
1815
1816/* default power management (not Tx power) table values */
1817/* for tim 0-10 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001818static struct iwl4965_power_vec_entry range_0[IWL_POWER_AC] = {
Zhu Yib481de92007-09-25 17:54:57 -07001819 {{NOSLP, SLP_TIMEOUT(0), SLP_TIMEOUT(0), SLP_VEC(0, 0, 0, 0, 0)}, 0},
1820 {{SLP, SLP_TIMEOUT(200), SLP_TIMEOUT(500), SLP_VEC(1, 2, 3, 4, 4)}, 0},
1821 {{SLP, SLP_TIMEOUT(200), SLP_TIMEOUT(300), SLP_VEC(2, 4, 6, 7, 7)}, 0},
1822 {{SLP, SLP_TIMEOUT(50), SLP_TIMEOUT(100), SLP_VEC(2, 6, 9, 9, 10)}, 0},
1823 {{SLP, SLP_TIMEOUT(50), SLP_TIMEOUT(25), SLP_VEC(2, 7, 9, 9, 10)}, 1},
1824 {{SLP, SLP_TIMEOUT(25), SLP_TIMEOUT(25), SLP_VEC(4, 7, 10, 10, 10)}, 1}
1825};
1826
1827/* for tim > 10 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001828static struct iwl4965_power_vec_entry range_1[IWL_POWER_AC] = {
Zhu Yib481de92007-09-25 17:54:57 -07001829 {{NOSLP, SLP_TIMEOUT(0), SLP_TIMEOUT(0), SLP_VEC(0, 0, 0, 0, 0)}, 0},
1830 {{SLP, SLP_TIMEOUT(200), SLP_TIMEOUT(500),
1831 SLP_VEC(1, 2, 3, 4, 0xFF)}, 0},
1832 {{SLP, SLP_TIMEOUT(200), SLP_TIMEOUT(300),
1833 SLP_VEC(2, 4, 6, 7, 0xFF)}, 0},
1834 {{SLP, SLP_TIMEOUT(50), SLP_TIMEOUT(100),
1835 SLP_VEC(2, 6, 9, 9, 0xFF)}, 0},
1836 {{SLP, SLP_TIMEOUT(50), SLP_TIMEOUT(25), SLP_VEC(2, 7, 9, 9, 0xFF)}, 0},
1837 {{SLP, SLP_TIMEOUT(25), SLP_TIMEOUT(25),
1838 SLP_VEC(4, 7, 10, 10, 0xFF)}, 0}
1839};
1840
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001841int iwl4965_power_init_handle(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001842{
1843 int rc = 0, i;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001844 struct iwl4965_power_mgr *pow_data;
1845 int size = sizeof(struct iwl4965_power_vec_entry) * IWL_POWER_AC;
Zhu Yib481de92007-09-25 17:54:57 -07001846 u16 pci_pm;
1847
1848 IWL_DEBUG_POWER("Initialize power \n");
1849
1850 pow_data = &(priv->power_data);
1851
1852 memset(pow_data, 0, sizeof(*pow_data));
1853
1854 pow_data->active_index = IWL_POWER_RANGE_0;
1855 pow_data->dtim_val = 0xffff;
1856
1857 memcpy(&pow_data->pwr_range_0[0], &range_0[0], size);
1858 memcpy(&pow_data->pwr_range_1[0], &range_1[0], size);
1859
1860 rc = pci_read_config_word(priv->pci_dev, PCI_LINK_CTRL, &pci_pm);
1861 if (rc != 0)
1862 return 0;
1863 else {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001864 struct iwl4965_powertable_cmd *cmd;
Zhu Yib481de92007-09-25 17:54:57 -07001865
1866 IWL_DEBUG_POWER("adjust power command flags\n");
1867
1868 for (i = 0; i < IWL_POWER_AC; i++) {
1869 cmd = &pow_data->pwr_range_0[i].cmd;
1870
1871 if (pci_pm & 0x1)
1872 cmd->flags &= ~IWL_POWER_PCI_PM_MSK;
1873 else
1874 cmd->flags |= IWL_POWER_PCI_PM_MSK;
1875 }
1876 }
1877 return rc;
1878}
1879
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001880static int iwl4965_update_power_cmd(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001881 struct iwl4965_powertable_cmd *cmd, u32 mode)
Zhu Yib481de92007-09-25 17:54:57 -07001882{
1883 int rc = 0, i;
1884 u8 skip;
1885 u32 max_sleep = 0;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001886 struct iwl4965_power_vec_entry *range;
Zhu Yib481de92007-09-25 17:54:57 -07001887 u8 period = 0;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001888 struct iwl4965_power_mgr *pow_data;
Zhu Yib481de92007-09-25 17:54:57 -07001889
1890 if (mode > IWL_POWER_INDEX_5) {
1891 IWL_DEBUG_POWER("Error invalid power mode \n");
1892 return -1;
1893 }
1894 pow_data = &(priv->power_data);
1895
1896 if (pow_data->active_index == IWL_POWER_RANGE_0)
1897 range = &pow_data->pwr_range_0[0];
1898 else
1899 range = &pow_data->pwr_range_1[1];
1900
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001901 memcpy(cmd, &range[mode].cmd, sizeof(struct iwl4965_powertable_cmd));
Zhu Yib481de92007-09-25 17:54:57 -07001902
1903#ifdef IWL_MAC80211_DISABLE
1904 if (priv->assoc_network != NULL) {
1905 unsigned long flags;
1906
1907 period = priv->assoc_network->tim.tim_period;
1908 }
1909#endif /*IWL_MAC80211_DISABLE */
1910 skip = range[mode].no_dtim;
1911
1912 if (period == 0) {
1913 period = 1;
1914 skip = 0;
1915 }
1916
1917 if (skip == 0) {
1918 max_sleep = period;
1919 cmd->flags &= ~IWL_POWER_SLEEP_OVER_DTIM_MSK;
1920 } else {
1921 __le32 slp_itrvl = cmd->sleep_interval[IWL_POWER_VEC_SIZE - 1];
1922 max_sleep = (le32_to_cpu(slp_itrvl) / period) * period;
1923 cmd->flags |= IWL_POWER_SLEEP_OVER_DTIM_MSK;
1924 }
1925
1926 for (i = 0; i < IWL_POWER_VEC_SIZE; i++) {
1927 if (le32_to_cpu(cmd->sleep_interval[i]) > max_sleep)
1928 cmd->sleep_interval[i] = cpu_to_le32(max_sleep);
1929 }
1930
1931 IWL_DEBUG_POWER("Flags value = 0x%08X\n", cmd->flags);
1932 IWL_DEBUG_POWER("Tx timeout = %u\n", le32_to_cpu(cmd->tx_data_timeout));
1933 IWL_DEBUG_POWER("Rx timeout = %u\n", le32_to_cpu(cmd->rx_data_timeout));
1934 IWL_DEBUG_POWER("Sleep interval vector = { %d , %d , %d , %d , %d }\n",
1935 le32_to_cpu(cmd->sleep_interval[0]),
1936 le32_to_cpu(cmd->sleep_interval[1]),
1937 le32_to_cpu(cmd->sleep_interval[2]),
1938 le32_to_cpu(cmd->sleep_interval[3]),
1939 le32_to_cpu(cmd->sleep_interval[4]));
1940
1941 return rc;
1942}
1943
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001944static int iwl4965_send_power_mode(struct iwl_priv *priv, u32 mode)
Zhu Yib481de92007-09-25 17:54:57 -07001945{
John W. Linville9a62f732007-11-15 16:27:36 -05001946 u32 uninitialized_var(final_mode);
Zhu Yib481de92007-09-25 17:54:57 -07001947 int rc;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001948 struct iwl4965_powertable_cmd cmd;
Zhu Yib481de92007-09-25 17:54:57 -07001949
1950 /* If on battery, set to 3,
Ian Schram01ebd062007-10-25 17:15:22 +08001951 * if plugged into AC power, set to CAM ("continuously aware mode"),
Zhu Yib481de92007-09-25 17:54:57 -07001952 * else user level */
1953 switch (mode) {
1954 case IWL_POWER_BATTERY:
1955 final_mode = IWL_POWER_INDEX_3;
1956 break;
1957 case IWL_POWER_AC:
1958 final_mode = IWL_POWER_MODE_CAM;
1959 break;
1960 default:
1961 final_mode = mode;
1962 break;
1963 }
1964
1965 cmd.keep_alive_beacons = 0;
1966
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001967 iwl4965_update_power_cmd(priv, &cmd, final_mode);
Zhu Yib481de92007-09-25 17:54:57 -07001968
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001969 rc = iwl4965_send_cmd_pdu(priv, POWER_TABLE_CMD, sizeof(cmd), &cmd);
Zhu Yib481de92007-09-25 17:54:57 -07001970
1971 if (final_mode == IWL_POWER_MODE_CAM)
1972 clear_bit(STATUS_POWER_PMI, &priv->status);
1973 else
1974 set_bit(STATUS_POWER_PMI, &priv->status);
1975
1976 return rc;
1977}
1978
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001979int iwl4965_is_network_packet(struct iwl_priv *priv, struct ieee80211_hdr *header)
Zhu Yib481de92007-09-25 17:54:57 -07001980{
1981 /* Filter incoming packets to determine if they are targeted toward
1982 * this network, discarding packets coming from ourselves */
1983 switch (priv->iw_mode) {
1984 case IEEE80211_IF_TYPE_IBSS: /* Header: Dest. | Source | BSSID */
1985 /* packets from our adapter are dropped (echo) */
1986 if (!compare_ether_addr(header->addr2, priv->mac_addr))
1987 return 0;
1988 /* {broad,multi}cast packets to our IBSS go through */
1989 if (is_multicast_ether_addr(header->addr1))
1990 return !compare_ether_addr(header->addr3, priv->bssid);
1991 /* packets to our adapter go through */
1992 return !compare_ether_addr(header->addr1, priv->mac_addr);
1993 case IEEE80211_IF_TYPE_STA: /* Header: Dest. | AP{BSSID} | Source */
1994 /* packets from our adapter are dropped (echo) */
1995 if (!compare_ether_addr(header->addr3, priv->mac_addr))
1996 return 0;
1997 /* {broad,multi}cast packets to our BSS go through */
1998 if (is_multicast_ether_addr(header->addr1))
1999 return !compare_ether_addr(header->addr2, priv->bssid);
2000 /* packets to our adapter go through */
2001 return !compare_ether_addr(header->addr1, priv->mac_addr);
2002 }
2003
2004 return 1;
2005}
2006
2007#define TX_STATUS_ENTRY(x) case TX_STATUS_FAIL_ ## x: return #x
2008
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002009static const char *iwl4965_get_tx_fail_reason(u32 status)
Zhu Yib481de92007-09-25 17:54:57 -07002010{
2011 switch (status & TX_STATUS_MSK) {
2012 case TX_STATUS_SUCCESS:
2013 return "SUCCESS";
2014 TX_STATUS_ENTRY(SHORT_LIMIT);
2015 TX_STATUS_ENTRY(LONG_LIMIT);
2016 TX_STATUS_ENTRY(FIFO_UNDERRUN);
2017 TX_STATUS_ENTRY(MGMNT_ABORT);
2018 TX_STATUS_ENTRY(NEXT_FRAG);
2019 TX_STATUS_ENTRY(LIFE_EXPIRE);
2020 TX_STATUS_ENTRY(DEST_PS);
2021 TX_STATUS_ENTRY(ABORTED);
2022 TX_STATUS_ENTRY(BT_RETRY);
2023 TX_STATUS_ENTRY(STA_INVALID);
2024 TX_STATUS_ENTRY(FRAG_DROPPED);
2025 TX_STATUS_ENTRY(TID_DISABLE);
2026 TX_STATUS_ENTRY(FRAME_FLUSHED);
2027 TX_STATUS_ENTRY(INSUFFICIENT_CF_POLL);
2028 TX_STATUS_ENTRY(TX_LOCKED);
2029 TX_STATUS_ENTRY(NO_BEACON_ON_RADAR);
2030 }
2031
2032 return "UNKNOWN";
2033}
2034
2035/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002036 * iwl4965_scan_cancel - Cancel any currently executing HW scan
Zhu Yib481de92007-09-25 17:54:57 -07002037 *
2038 * NOTE: priv->mutex is not required before calling this function
2039 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002040static int iwl4965_scan_cancel(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002041{
2042 if (!test_bit(STATUS_SCAN_HW, &priv->status)) {
2043 clear_bit(STATUS_SCANNING, &priv->status);
2044 return 0;
2045 }
2046
2047 if (test_bit(STATUS_SCANNING, &priv->status)) {
2048 if (!test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
2049 IWL_DEBUG_SCAN("Queuing scan abort.\n");
2050 set_bit(STATUS_SCAN_ABORTING, &priv->status);
2051 queue_work(priv->workqueue, &priv->abort_scan);
2052
2053 } else
2054 IWL_DEBUG_SCAN("Scan abort already in progress.\n");
2055
2056 return test_bit(STATUS_SCANNING, &priv->status);
2057 }
2058
2059 return 0;
2060}
2061
2062/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002063 * iwl4965_scan_cancel_timeout - Cancel any currently executing HW scan
Zhu Yib481de92007-09-25 17:54:57 -07002064 * @ms: amount of time to wait (in milliseconds) for scan to abort
2065 *
2066 * NOTE: priv->mutex must be held before calling this function
2067 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002068static int iwl4965_scan_cancel_timeout(struct iwl_priv *priv, unsigned long ms)
Zhu Yib481de92007-09-25 17:54:57 -07002069{
2070 unsigned long now = jiffies;
2071 int ret;
2072
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002073 ret = iwl4965_scan_cancel(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002074 if (ret && ms) {
2075 mutex_unlock(&priv->mutex);
2076 while (!time_after(jiffies, now + msecs_to_jiffies(ms)) &&
2077 test_bit(STATUS_SCANNING, &priv->status))
2078 msleep(1);
2079 mutex_lock(&priv->mutex);
2080
2081 return test_bit(STATUS_SCANNING, &priv->status);
2082 }
2083
2084 return ret;
2085}
2086
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002087static void iwl4965_sequence_reset(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002088{
2089 /* Reset ieee stats */
2090
2091 /* We don't reset the net_device_stats (ieee->stats) on
2092 * re-association */
2093
2094 priv->last_seq_num = -1;
2095 priv->last_frag_num = -1;
2096 priv->last_packet_time = 0;
2097
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002098 iwl4965_scan_cancel(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002099}
2100
2101#define MAX_UCODE_BEACON_INTERVAL 4096
2102#define INTEL_CONN_LISTEN_INTERVAL __constant_cpu_to_le16(0xA)
2103
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002104static __le16 iwl4965_adjust_beacon_interval(u16 beacon_val)
Zhu Yib481de92007-09-25 17:54:57 -07002105{
2106 u16 new_val = 0;
2107 u16 beacon_factor = 0;
2108
2109 beacon_factor =
2110 (beacon_val + MAX_UCODE_BEACON_INTERVAL)
2111 / MAX_UCODE_BEACON_INTERVAL;
2112 new_val = beacon_val / beacon_factor;
2113
2114 return cpu_to_le16(new_val);
2115}
2116
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002117static void iwl4965_setup_rxon_timing(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002118{
2119 u64 interval_tm_unit;
2120 u64 tsf, result;
2121 unsigned long flags;
2122 struct ieee80211_conf *conf = NULL;
2123 u16 beacon_int = 0;
2124
2125 conf = ieee80211_get_hw_conf(priv->hw);
2126
2127 spin_lock_irqsave(&priv->lock, flags);
2128 priv->rxon_timing.timestamp.dw[1] = cpu_to_le32(priv->timestamp1);
2129 priv->rxon_timing.timestamp.dw[0] = cpu_to_le32(priv->timestamp0);
2130
2131 priv->rxon_timing.listen_interval = INTEL_CONN_LISTEN_INTERVAL;
2132
2133 tsf = priv->timestamp1;
2134 tsf = ((tsf << 32) | priv->timestamp0);
2135
2136 beacon_int = priv->beacon_int;
2137 spin_unlock_irqrestore(&priv->lock, flags);
2138
2139 if (priv->iw_mode == IEEE80211_IF_TYPE_STA) {
2140 if (beacon_int == 0) {
2141 priv->rxon_timing.beacon_interval = cpu_to_le16(100);
2142 priv->rxon_timing.beacon_init_val = cpu_to_le32(102400);
2143 } else {
2144 priv->rxon_timing.beacon_interval =
2145 cpu_to_le16(beacon_int);
2146 priv->rxon_timing.beacon_interval =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002147 iwl4965_adjust_beacon_interval(
Zhu Yib481de92007-09-25 17:54:57 -07002148 le16_to_cpu(priv->rxon_timing.beacon_interval));
2149 }
2150
2151 priv->rxon_timing.atim_window = 0;
2152 } else {
2153 priv->rxon_timing.beacon_interval =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002154 iwl4965_adjust_beacon_interval(conf->beacon_int);
Zhu Yib481de92007-09-25 17:54:57 -07002155 /* TODO: we need to get atim_window from upper stack
2156 * for now we set to 0 */
2157 priv->rxon_timing.atim_window = 0;
2158 }
2159
2160 interval_tm_unit =
2161 (le16_to_cpu(priv->rxon_timing.beacon_interval) * 1024);
2162 result = do_div(tsf, interval_tm_unit);
2163 priv->rxon_timing.beacon_init_val =
2164 cpu_to_le32((u32) ((u64) interval_tm_unit - result));
2165
2166 IWL_DEBUG_ASSOC
2167 ("beacon interval %d beacon timer %d beacon tim %d\n",
2168 le16_to_cpu(priv->rxon_timing.beacon_interval),
2169 le32_to_cpu(priv->rxon_timing.beacon_init_val),
2170 le16_to_cpu(priv->rxon_timing.atim_window));
2171}
2172
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002173static int iwl4965_scan_initiate(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002174{
2175 if (priv->iw_mode == IEEE80211_IF_TYPE_AP) {
2176 IWL_ERROR("APs don't scan.\n");
2177 return 0;
2178 }
2179
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002180 if (!iwl4965_is_ready_rf(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07002181 IWL_DEBUG_SCAN("Aborting scan due to not ready.\n");
2182 return -EIO;
2183 }
2184
2185 if (test_bit(STATUS_SCANNING, &priv->status)) {
2186 IWL_DEBUG_SCAN("Scan already in progress.\n");
2187 return -EAGAIN;
2188 }
2189
2190 if (test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
2191 IWL_DEBUG_SCAN("Scan request while abort pending. "
2192 "Queuing.\n");
2193 return -EAGAIN;
2194 }
2195
2196 IWL_DEBUG_INFO("Starting scan...\n");
2197 priv->scan_bands = 2;
2198 set_bit(STATUS_SCANNING, &priv->status);
2199 priv->scan_start = jiffies;
2200 priv->scan_pass_start = priv->scan_start;
2201
2202 queue_work(priv->workqueue, &priv->request_scan);
2203
2204 return 0;
2205}
2206
Zhu Yib481de92007-09-25 17:54:57 -07002207
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002208static void iwl4965_set_flags_for_phymode(struct iwl_priv *priv,
Johannes Berg8318d782008-01-24 19:38:38 +01002209 enum ieee80211_band band)
Zhu Yib481de92007-09-25 17:54:57 -07002210{
Johannes Berg8318d782008-01-24 19:38:38 +01002211 if (band == IEEE80211_BAND_5GHZ) {
Zhu Yib481de92007-09-25 17:54:57 -07002212 priv->staging_rxon.flags &=
2213 ~(RXON_FLG_BAND_24G_MSK | RXON_FLG_AUTO_DETECT_MSK
2214 | RXON_FLG_CCK_MSK);
2215 priv->staging_rxon.flags |= RXON_FLG_SHORT_SLOT_MSK;
2216 } else {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002217 /* Copied from iwl4965_bg_post_associate() */
Zhu Yib481de92007-09-25 17:54:57 -07002218 if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_SLOT_TIME)
2219 priv->staging_rxon.flags |= RXON_FLG_SHORT_SLOT_MSK;
2220 else
2221 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
2222
2223 if (priv->iw_mode == IEEE80211_IF_TYPE_IBSS)
2224 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
2225
2226 priv->staging_rxon.flags |= RXON_FLG_BAND_24G_MSK;
2227 priv->staging_rxon.flags |= RXON_FLG_AUTO_DETECT_MSK;
2228 priv->staging_rxon.flags &= ~RXON_FLG_CCK_MSK;
2229 }
2230}
2231
2232/*
Ian Schram01ebd062007-10-25 17:15:22 +08002233 * initialize rxon structure with default values from eeprom
Zhu Yib481de92007-09-25 17:54:57 -07002234 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002235static void iwl4965_connection_init_rx_config(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002236{
Assaf Kraussbf85ea42008-03-14 10:38:49 -07002237 const struct iwl_channel_info *ch_info;
Zhu Yib481de92007-09-25 17:54:57 -07002238
2239 memset(&priv->staging_rxon, 0, sizeof(priv->staging_rxon));
2240
2241 switch (priv->iw_mode) {
2242 case IEEE80211_IF_TYPE_AP:
2243 priv->staging_rxon.dev_type = RXON_DEV_TYPE_AP;
2244 break;
2245
2246 case IEEE80211_IF_TYPE_STA:
2247 priv->staging_rxon.dev_type = RXON_DEV_TYPE_ESS;
2248 priv->staging_rxon.filter_flags = RXON_FILTER_ACCEPT_GRP_MSK;
2249 break;
2250
2251 case IEEE80211_IF_TYPE_IBSS:
2252 priv->staging_rxon.dev_type = RXON_DEV_TYPE_IBSS;
2253 priv->staging_rxon.flags = RXON_FLG_SHORT_PREAMBLE_MSK;
2254 priv->staging_rxon.filter_flags = RXON_FILTER_BCON_AWARE_MSK |
2255 RXON_FILTER_ACCEPT_GRP_MSK;
2256 break;
2257
2258 case IEEE80211_IF_TYPE_MNTR:
2259 priv->staging_rxon.dev_type = RXON_DEV_TYPE_SNIFFER;
2260 priv->staging_rxon.filter_flags = RXON_FILTER_PROMISC_MSK |
2261 RXON_FILTER_CTL2HOST_MSK | RXON_FILTER_ACCEPT_GRP_MSK;
2262 break;
2263 }
2264
2265#if 0
2266 /* TODO: Figure out when short_preamble would be set and cache from
2267 * that */
2268 if (!hw_to_local(priv->hw)->short_preamble)
2269 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_PREAMBLE_MSK;
2270 else
2271 priv->staging_rxon.flags |= RXON_FLG_SHORT_PREAMBLE_MSK;
2272#endif
2273
Johannes Berg8318d782008-01-24 19:38:38 +01002274 ch_info = iwl4965_get_channel_info(priv, priv->band,
Zhu Yib481de92007-09-25 17:54:57 -07002275 le16_to_cpu(priv->staging_rxon.channel));
2276
2277 if (!ch_info)
2278 ch_info = &priv->channel_info[0];
2279
2280 /*
2281 * in some case A channels are all non IBSS
2282 * in this case force B/G channel
2283 */
2284 if ((priv->iw_mode == IEEE80211_IF_TYPE_IBSS) &&
2285 !(is_channel_ibss(ch_info)))
2286 ch_info = &priv->channel_info[0];
2287
2288 priv->staging_rxon.channel = cpu_to_le16(ch_info->channel);
Johannes Berg8318d782008-01-24 19:38:38 +01002289 priv->band = ch_info->band;
Zhu Yib481de92007-09-25 17:54:57 -07002290
Johannes Berg8318d782008-01-24 19:38:38 +01002291 iwl4965_set_flags_for_phymode(priv, priv->band);
Zhu Yib481de92007-09-25 17:54:57 -07002292
2293 priv->staging_rxon.ofdm_basic_rates =
2294 (IWL_OFDM_RATES_MASK >> IWL_FIRST_OFDM_RATE) & 0xFF;
2295 priv->staging_rxon.cck_basic_rates =
2296 (IWL_CCK_RATES_MASK >> IWL_FIRST_CCK_RATE) & 0xF;
2297
2298 priv->staging_rxon.flags &= ~(RXON_FLG_CHANNEL_MODE_MIXED_MSK |
2299 RXON_FLG_CHANNEL_MODE_PURE_40_MSK);
2300 memcpy(priv->staging_rxon.node_addr, priv->mac_addr, ETH_ALEN);
2301 memcpy(priv->staging_rxon.wlap_bssid_addr, priv->mac_addr, ETH_ALEN);
2302 priv->staging_rxon.ofdm_ht_single_stream_basic_rates = 0xff;
2303 priv->staging_rxon.ofdm_ht_dual_stream_basic_rates = 0xff;
2304 iwl4965_set_rxon_chain(priv);
2305}
2306
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002307static int iwl4965_set_mode(struct iwl_priv *priv, int mode)
Zhu Yib481de92007-09-25 17:54:57 -07002308{
Zhu Yib481de92007-09-25 17:54:57 -07002309 if (mode == IEEE80211_IF_TYPE_IBSS) {
Assaf Kraussbf85ea42008-03-14 10:38:49 -07002310 const struct iwl_channel_info *ch_info;
Zhu Yib481de92007-09-25 17:54:57 -07002311
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002312 ch_info = iwl4965_get_channel_info(priv,
Johannes Berg8318d782008-01-24 19:38:38 +01002313 priv->band,
Zhu Yib481de92007-09-25 17:54:57 -07002314 le16_to_cpu(priv->staging_rxon.channel));
2315
2316 if (!ch_info || !is_channel_ibss(ch_info)) {
2317 IWL_ERROR("channel %d not IBSS channel\n",
2318 le16_to_cpu(priv->staging_rxon.channel));
2319 return -EINVAL;
2320 }
2321 }
2322
Zhu Yib481de92007-09-25 17:54:57 -07002323 priv->iw_mode = mode;
2324
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002325 iwl4965_connection_init_rx_config(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002326 memcpy(priv->staging_rxon.node_addr, priv->mac_addr, ETH_ALEN);
2327
Assaf Kraussbf85ea42008-03-14 10:38:49 -07002328 iwlcore_clear_stations_table(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002329
Mohamed Abbasfde35712007-11-29 11:10:15 +08002330 /* dont commit rxon if rf-kill is on*/
2331 if (!iwl4965_is_ready_rf(priv))
2332 return -EAGAIN;
2333
2334 cancel_delayed_work(&priv->scan_check);
2335 if (iwl4965_scan_cancel_timeout(priv, 100)) {
2336 IWL_WARNING("Aborted scan still in progress after 100ms\n");
2337 IWL_DEBUG_MAC80211("leaving - scan abort failed.\n");
2338 return -EAGAIN;
2339 }
2340
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002341 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002342
2343 return 0;
2344}
2345
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002346static void iwl4965_build_tx_cmd_hwcrypto(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07002347 struct ieee80211_tx_control *ctl,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002348 struct iwl4965_cmd *cmd,
Zhu Yib481de92007-09-25 17:54:57 -07002349 struct sk_buff *skb_frag,
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07002350 int sta_id)
Zhu Yib481de92007-09-25 17:54:57 -07002351{
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07002352 struct iwl4965_hw_key *keyinfo = &priv->stations[sta_id].keyinfo;
Zhu Yib481de92007-09-25 17:54:57 -07002353
2354 switch (keyinfo->alg) {
2355 case ALG_CCMP:
2356 cmd->cmd.tx.sec_ctl = TX_CMD_SEC_CCM;
2357 memcpy(cmd->cmd.tx.key, keyinfo->key, keyinfo->keylen);
Max Stepanov8236e182008-03-12 16:58:48 -07002358 if (ctl->flags & IEEE80211_TXCTL_AMPDU)
2359 cmd->cmd.tx.tx_flags |= TX_CMD_FLG_AGG_CCMP_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07002360 IWL_DEBUG_TX("tx_cmd with aes hwcrypto\n");
2361 break;
2362
2363 case ALG_TKIP:
Zhu Yib481de92007-09-25 17:54:57 -07002364 cmd->cmd.tx.sec_ctl = TX_CMD_SEC_TKIP;
Emmanuel Grumbach2bc75082008-03-19 16:41:45 -07002365 ieee80211_get_tkip_key(keyinfo->conf, skb_frag,
2366 IEEE80211_TKIP_P2_KEY, cmd->cmd.tx.key);
2367 IWL_DEBUG_TX("tx_cmd with tkip hwcrypto\n");
Zhu Yib481de92007-09-25 17:54:57 -07002368 break;
2369
2370 case ALG_WEP:
2371 cmd->cmd.tx.sec_ctl = TX_CMD_SEC_WEP |
2372 (ctl->key_idx & TX_CMD_SEC_MSK) << TX_CMD_SEC_SHIFT;
2373
2374 if (keyinfo->keylen == 13)
2375 cmd->cmd.tx.sec_ctl |= TX_CMD_SEC_KEY128;
2376
2377 memcpy(&cmd->cmd.tx.key[3], keyinfo->key, keyinfo->keylen);
2378
2379 IWL_DEBUG_TX("Configuring packet for WEP encryption "
2380 "with key %d\n", ctl->key_idx);
2381 break;
2382
Zhu Yib481de92007-09-25 17:54:57 -07002383 default:
2384 printk(KERN_ERR "Unknown encode alg %d\n", keyinfo->alg);
2385 break;
2386 }
2387}
2388
2389/*
2390 * handle build REPLY_TX command notification.
2391 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002392static void iwl4965_build_tx_cmd_basic(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002393 struct iwl4965_cmd *cmd,
Zhu Yib481de92007-09-25 17:54:57 -07002394 struct ieee80211_tx_control *ctrl,
2395 struct ieee80211_hdr *hdr,
2396 int is_unicast, u8 std_id)
2397{
2398 __le16 *qc;
2399 u16 fc = le16_to_cpu(hdr->frame_control);
2400 __le32 tx_flags = cmd->cmd.tx.tx_flags;
2401
2402 cmd->cmd.tx.stop_time.life_time = TX_CMD_LIFE_TIME_INFINITE;
2403 if (!(ctrl->flags & IEEE80211_TXCTL_NO_ACK)) {
2404 tx_flags |= TX_CMD_FLG_ACK_MSK;
2405 if ((fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_MGMT)
2406 tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
2407 if (ieee80211_is_probe_response(fc) &&
2408 !(le16_to_cpu(hdr->seq_ctrl) & 0xf))
2409 tx_flags |= TX_CMD_FLG_TSF_MSK;
2410 } else {
2411 tx_flags &= (~TX_CMD_FLG_ACK_MSK);
2412 tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
2413 }
2414
Tomas Winkler87e4f7d2008-01-14 17:46:16 -08002415 if (ieee80211_is_back_request(fc))
2416 tx_flags |= TX_CMD_FLG_ACK_MSK | TX_CMD_FLG_IMM_BA_RSP_MASK;
2417
2418
Zhu Yib481de92007-09-25 17:54:57 -07002419 cmd->cmd.tx.sta_id = std_id;
2420 if (ieee80211_get_morefrag(hdr))
2421 tx_flags |= TX_CMD_FLG_MORE_FRAG_MSK;
2422
2423 qc = ieee80211_get_qos_ctrl(hdr);
2424 if (qc) {
2425 cmd->cmd.tx.tid_tspec = (u8) (le16_to_cpu(*qc) & 0xf);
2426 tx_flags &= ~TX_CMD_FLG_SEQ_CTL_MSK;
2427 } else
2428 tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
2429
2430 if (ctrl->flags & IEEE80211_TXCTL_USE_RTS_CTS) {
2431 tx_flags |= TX_CMD_FLG_RTS_MSK;
2432 tx_flags &= ~TX_CMD_FLG_CTS_MSK;
2433 } else if (ctrl->flags & IEEE80211_TXCTL_USE_CTS_PROTECT) {
2434 tx_flags &= ~TX_CMD_FLG_RTS_MSK;
2435 tx_flags |= TX_CMD_FLG_CTS_MSK;
2436 }
2437
2438 if ((tx_flags & TX_CMD_FLG_RTS_MSK) || (tx_flags & TX_CMD_FLG_CTS_MSK))
2439 tx_flags |= TX_CMD_FLG_FULL_TXOP_PROT_MSK;
2440
2441 tx_flags &= ~(TX_CMD_FLG_ANT_SEL_MSK);
2442 if ((fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_MGMT) {
2443 if ((fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_ASSOC_REQ ||
2444 (fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_REASSOC_REQ)
Ian Schrambc434dd2007-10-25 17:15:29 +08002445 cmd->cmd.tx.timeout.pm_frame_timeout = cpu_to_le16(3);
Zhu Yib481de92007-09-25 17:54:57 -07002446 else
Ian Schrambc434dd2007-10-25 17:15:29 +08002447 cmd->cmd.tx.timeout.pm_frame_timeout = cpu_to_le16(2);
Zhu Yib481de92007-09-25 17:54:57 -07002448 } else
2449 cmd->cmd.tx.timeout.pm_frame_timeout = 0;
2450
2451 cmd->cmd.tx.driver_txop = 0;
2452 cmd->cmd.tx.tx_flags = tx_flags;
2453 cmd->cmd.tx.next_frame_len = 0;
2454}
Tomas Winkler19758be2008-03-12 16:58:51 -07002455static void iwl_update_tx_stats(struct iwl_priv *priv, u16 fc, u16 len)
2456{
2457 /* 0 - mgmt, 1 - cnt, 2 - data */
2458 int idx = (fc & IEEE80211_FCTL_FTYPE) >> 2;
2459 priv->tx_stats[idx].cnt++;
2460 priv->tx_stats[idx].bytes += len;
2461}
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002462/**
2463 * iwl4965_get_sta_id - Find station's index within station table
2464 *
2465 * If new IBSS station, create new entry in station table
2466 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002467static int iwl4965_get_sta_id(struct iwl_priv *priv,
Ben Cahill9fbab512007-11-29 11:09:47 +08002468 struct ieee80211_hdr *hdr)
Zhu Yib481de92007-09-25 17:54:57 -07002469{
2470 int sta_id;
2471 u16 fc = le16_to_cpu(hdr->frame_control);
Joe Perches0795af52007-10-03 17:59:30 -07002472 DECLARE_MAC_BUF(mac);
Zhu Yib481de92007-09-25 17:54:57 -07002473
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002474 /* If this frame is broadcast or management, use broadcast station id */
Zhu Yib481de92007-09-25 17:54:57 -07002475 if (((fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_DATA) ||
2476 is_multicast_ether_addr(hdr->addr1))
2477 return priv->hw_setting.bcast_sta_id;
2478
2479 switch (priv->iw_mode) {
2480
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002481 /* If we are a client station in a BSS network, use the special
2482 * AP station entry (that's the only station we communicate with) */
Zhu Yib481de92007-09-25 17:54:57 -07002483 case IEEE80211_IF_TYPE_STA:
2484 return IWL_AP_ID;
2485
2486 /* If we are an AP, then find the station, or use BCAST */
2487 case IEEE80211_IF_TYPE_AP:
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002488 sta_id = iwl4965_hw_find_station(priv, hdr->addr1);
Zhu Yib481de92007-09-25 17:54:57 -07002489 if (sta_id != IWL_INVALID_STATION)
2490 return sta_id;
2491 return priv->hw_setting.bcast_sta_id;
2492
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002493 /* If this frame is going out to an IBSS network, find the station,
2494 * or create a new station table entry */
Zhu Yib481de92007-09-25 17:54:57 -07002495 case IEEE80211_IF_TYPE_IBSS:
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002496 sta_id = iwl4965_hw_find_station(priv, hdr->addr1);
Zhu Yib481de92007-09-25 17:54:57 -07002497 if (sta_id != IWL_INVALID_STATION)
2498 return sta_id;
2499
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002500 /* Create new station table entry */
Ron Rindjunsky67d62032007-11-26 16:14:40 +02002501 sta_id = iwl4965_add_station_flags(priv, hdr->addr1,
2502 0, CMD_ASYNC, NULL);
Zhu Yib481de92007-09-25 17:54:57 -07002503
2504 if (sta_id != IWL_INVALID_STATION)
2505 return sta_id;
2506
Joe Perches0795af52007-10-03 17:59:30 -07002507 IWL_DEBUG_DROP("Station %s not in station map. "
Zhu Yib481de92007-09-25 17:54:57 -07002508 "Defaulting to broadcast...\n",
Joe Perches0795af52007-10-03 17:59:30 -07002509 print_mac(mac, hdr->addr1));
Tomas Winkler0a6857e2008-03-12 16:58:49 -07002510 iwl_print_hex_dump(IWL_DL_DROP, (u8 *) hdr, sizeof(*hdr));
Zhu Yib481de92007-09-25 17:54:57 -07002511 return priv->hw_setting.bcast_sta_id;
2512
2513 default:
Ian Schram01ebd062007-10-25 17:15:22 +08002514 IWL_WARNING("Unknown mode of operation: %d", priv->iw_mode);
Zhu Yib481de92007-09-25 17:54:57 -07002515 return priv->hw_setting.bcast_sta_id;
2516 }
2517}
2518
2519/*
2520 * start REPLY_TX command process
2521 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002522static int iwl4965_tx_skb(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07002523 struct sk_buff *skb, struct ieee80211_tx_control *ctl)
2524{
2525 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002526 struct iwl4965_tfd_frame *tfd;
Zhu Yib481de92007-09-25 17:54:57 -07002527 u32 *control_flags;
2528 int txq_id = ctl->queue;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002529 struct iwl4965_tx_queue *txq = NULL;
2530 struct iwl4965_queue *q = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07002531 dma_addr_t phys_addr;
2532 dma_addr_t txcmd_phys;
Tomas Winkler87e4f7d2008-01-14 17:46:16 -08002533 dma_addr_t scratch_phys;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002534 struct iwl4965_cmd *out_cmd = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07002535 u16 len, idx, len_org;
2536 u8 id, hdr_len, unicast;
2537 u8 sta_id;
2538 u16 seq_number = 0;
2539 u16 fc;
2540 __le16 *qc;
2541 u8 wait_write_ptr = 0;
2542 unsigned long flags;
2543 int rc;
2544
2545 spin_lock_irqsave(&priv->lock, flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002546 if (iwl4965_is_rfkill(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07002547 IWL_DEBUG_DROP("Dropping - RF KILL\n");
2548 goto drop_unlock;
2549 }
2550
Johannes Berg32bfd352007-12-19 01:31:26 +01002551 if (!priv->vif) {
2552 IWL_DEBUG_DROP("Dropping - !priv->vif\n");
Zhu Yib481de92007-09-25 17:54:57 -07002553 goto drop_unlock;
2554 }
2555
Johannes Berg8318d782008-01-24 19:38:38 +01002556 if ((ctl->tx_rate->hw_value & 0xFF) == IWL_INVALID_RATE) {
Zhu Yib481de92007-09-25 17:54:57 -07002557 IWL_ERROR("ERROR: No TX rate available.\n");
2558 goto drop_unlock;
2559 }
2560
2561 unicast = !is_multicast_ether_addr(hdr->addr1);
2562 id = 0;
2563
2564 fc = le16_to_cpu(hdr->frame_control);
2565
Tomas Winkler0a6857e2008-03-12 16:58:49 -07002566#ifdef CONFIG_IWLWIFI_DEBUG
Zhu Yib481de92007-09-25 17:54:57 -07002567 if (ieee80211_is_auth(fc))
2568 IWL_DEBUG_TX("Sending AUTH frame\n");
2569 else if (ieee80211_is_assoc_request(fc))
2570 IWL_DEBUG_TX("Sending ASSOC frame\n");
2571 else if (ieee80211_is_reassoc_request(fc))
2572 IWL_DEBUG_TX("Sending REASSOC frame\n");
2573#endif
2574
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08002575 /* drop all data frame if we are not associated */
Gregory Greenman76f39152008-01-23 10:15:21 -08002576 if (((fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_DATA) &&
2577 (!iwl4965_is_associated(priv) ||
Reinette Chatrea6477242008-02-14 10:40:28 -08002578 ((priv->iw_mode == IEEE80211_IF_TYPE_STA) && !priv->assoc_id) ||
Gregory Greenman76f39152008-01-23 10:15:21 -08002579 !priv->assoc_station_added)) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002580 IWL_DEBUG_DROP("Dropping - !iwl4965_is_associated\n");
Zhu Yib481de92007-09-25 17:54:57 -07002581 goto drop_unlock;
2582 }
2583
2584 spin_unlock_irqrestore(&priv->lock, flags);
2585
2586 hdr_len = ieee80211_get_hdrlen(fc);
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002587
2588 /* Find (or create) index into station table for destination station */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002589 sta_id = iwl4965_get_sta_id(priv, hdr);
Zhu Yib481de92007-09-25 17:54:57 -07002590 if (sta_id == IWL_INVALID_STATION) {
Joe Perches0795af52007-10-03 17:59:30 -07002591 DECLARE_MAC_BUF(mac);
2592
2593 IWL_DEBUG_DROP("Dropping - INVALID STATION: %s\n",
2594 print_mac(mac, hdr->addr1));
Zhu Yib481de92007-09-25 17:54:57 -07002595 goto drop;
2596 }
2597
2598 IWL_DEBUG_RATE("station Id %d\n", sta_id);
2599
2600 qc = ieee80211_get_qos_ctrl(hdr);
2601 if (qc) {
2602 u8 tid = (u8)(le16_to_cpu(*qc) & 0xf);
2603 seq_number = priv->stations[sta_id].tid[tid].seq_number &
2604 IEEE80211_SCTL_SEQ;
2605 hdr->seq_ctrl = cpu_to_le16(seq_number) |
2606 (hdr->seq_ctrl &
2607 __constant_cpu_to_le16(IEEE80211_SCTL_FRAG));
2608 seq_number += 0x10;
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08002609#ifdef CONFIG_IWL4965_HT
Zhu Yib481de92007-09-25 17:54:57 -07002610 /* aggregation is on for this <sta,tid> */
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02002611 if (ctl->flags & IEEE80211_TXCTL_AMPDU)
Zhu Yib481de92007-09-25 17:54:57 -07002612 txq_id = priv->stations[sta_id].tid[tid].agg.txq_id;
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02002613 priv->stations[sta_id].tid[tid].tfds_in_queue++;
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08002614#endif /* CONFIG_IWL4965_HT */
Zhu Yib481de92007-09-25 17:54:57 -07002615 }
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002616
2617 /* Descriptor for chosen Tx queue */
Zhu Yib481de92007-09-25 17:54:57 -07002618 txq = &priv->txq[txq_id];
2619 q = &txq->q;
2620
2621 spin_lock_irqsave(&priv->lock, flags);
2622
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002623 /* Set up first empty TFD within this queue's circular TFD buffer */
Tomas Winklerfc4b6852007-10-25 17:15:24 +08002624 tfd = &txq->bd[q->write_ptr];
Zhu Yib481de92007-09-25 17:54:57 -07002625 memset(tfd, 0, sizeof(*tfd));
2626 control_flags = (u32 *) tfd;
Tomas Winklerfc4b6852007-10-25 17:15:24 +08002627 idx = get_cmd_index(q, q->write_ptr, 0);
Zhu Yib481de92007-09-25 17:54:57 -07002628
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002629 /* Set up driver data for this TFD */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002630 memset(&(txq->txb[q->write_ptr]), 0, sizeof(struct iwl4965_tx_info));
Tomas Winklerfc4b6852007-10-25 17:15:24 +08002631 txq->txb[q->write_ptr].skb[0] = skb;
2632 memcpy(&(txq->txb[q->write_ptr].status.control),
Zhu Yib481de92007-09-25 17:54:57 -07002633 ctl, sizeof(struct ieee80211_tx_control));
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002634
2635 /* Set up first empty entry in queue's array of Tx/cmd buffers */
Zhu Yib481de92007-09-25 17:54:57 -07002636 out_cmd = &txq->cmd[idx];
2637 memset(&out_cmd->hdr, 0, sizeof(out_cmd->hdr));
2638 memset(&out_cmd->cmd.tx, 0, sizeof(out_cmd->cmd.tx));
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002639
2640 /*
2641 * Set up the Tx-command (not MAC!) header.
2642 * Store the chosen Tx queue and TFD index within the sequence field;
2643 * after Tx, uCode's Tx response will return this value so driver can
2644 * locate the frame within the tx queue and do post-tx processing.
2645 */
Zhu Yib481de92007-09-25 17:54:57 -07002646 out_cmd->hdr.cmd = REPLY_TX;
2647 out_cmd->hdr.sequence = cpu_to_le16((u16)(QUEUE_TO_SEQ(txq_id) |
Tomas Winklerfc4b6852007-10-25 17:15:24 +08002648 INDEX_TO_SEQ(q->write_ptr)));
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002649
2650 /* Copy MAC header from skb into command buffer */
Zhu Yib481de92007-09-25 17:54:57 -07002651 memcpy(out_cmd->cmd.tx.hdr, hdr, hdr_len);
2652
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002653 /*
2654 * Use the first empty entry in this queue's command buffer array
2655 * to contain the Tx command and MAC header concatenated together
2656 * (payload data will be in another buffer).
2657 * Size of this varies, due to varying MAC header length.
2658 * If end is not dword aligned, we'll have 2 extra bytes at the end
2659 * of the MAC header (device reads on dword boundaries).
2660 * We'll tell device about this padding later.
2661 */
Zhu Yib481de92007-09-25 17:54:57 -07002662 len = priv->hw_setting.tx_cmd_len +
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002663 sizeof(struct iwl4965_cmd_header) + hdr_len;
Zhu Yib481de92007-09-25 17:54:57 -07002664
2665 len_org = len;
2666 len = (len + 3) & ~3;
2667
2668 if (len_org != len)
2669 len_org = 1;
2670 else
2671 len_org = 0;
2672
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002673 /* Physical address of this Tx command's header (not MAC header!),
2674 * within command buffer array. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002675 txcmd_phys = txq->dma_addr_cmd + sizeof(struct iwl4965_cmd) * idx +
2676 offsetof(struct iwl4965_cmd, hdr);
Zhu Yib481de92007-09-25 17:54:57 -07002677
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002678 /* Add buffer containing Tx command and MAC(!) header to TFD's
2679 * first entry */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002680 iwl4965_hw_txq_attach_buf_to_tfd(priv, tfd, txcmd_phys, len);
Zhu Yib481de92007-09-25 17:54:57 -07002681
2682 if (!(ctl->flags & IEEE80211_TXCTL_DO_NOT_ENCRYPT))
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07002683 iwl4965_build_tx_cmd_hwcrypto(priv, ctl, out_cmd, skb, sta_id);
Zhu Yib481de92007-09-25 17:54:57 -07002684
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002685 /* Set up TFD's 2nd entry to point directly to remainder of skb,
2686 * if any (802.11 null frames have no payload). */
Zhu Yib481de92007-09-25 17:54:57 -07002687 len = skb->len - hdr_len;
2688 if (len) {
2689 phys_addr = pci_map_single(priv->pci_dev, skb->data + hdr_len,
2690 len, PCI_DMA_TODEVICE);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002691 iwl4965_hw_txq_attach_buf_to_tfd(priv, tfd, phys_addr, len);
Zhu Yib481de92007-09-25 17:54:57 -07002692 }
2693
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002694 /* Tell 4965 about any 2-byte padding after MAC header */
Zhu Yib481de92007-09-25 17:54:57 -07002695 if (len_org)
2696 out_cmd->cmd.tx.tx_flags |= TX_CMD_FLG_MH_PAD_MSK;
2697
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002698 /* Total # bytes to be transmitted */
Zhu Yib481de92007-09-25 17:54:57 -07002699 len = (u16)skb->len;
2700 out_cmd->cmd.tx.len = cpu_to_le16(len);
2701
2702 /* TODO need this for burst mode later on */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002703 iwl4965_build_tx_cmd_basic(priv, out_cmd, ctl, hdr, unicast, sta_id);
Zhu Yib481de92007-09-25 17:54:57 -07002704
2705 /* set is_hcca to 0; it probably will never be implemented */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002706 iwl4965_hw_build_tx_cmd_rate(priv, out_cmd, ctl, hdr, sta_id, 0);
Zhu Yib481de92007-09-25 17:54:57 -07002707
Tomas Winkler19758be2008-03-12 16:58:51 -07002708 iwl_update_tx_stats(priv, fc, len);
2709
Tomas Winkler87e4f7d2008-01-14 17:46:16 -08002710 scratch_phys = txcmd_phys + sizeof(struct iwl4965_cmd_header) +
2711 offsetof(struct iwl4965_tx_cmd, scratch);
2712 out_cmd->cmd.tx.dram_lsb_ptr = cpu_to_le32(scratch_phys);
2713 out_cmd->cmd.tx.dram_msb_ptr = iwl_get_dma_hi_address(scratch_phys);
2714
Zhu Yib481de92007-09-25 17:54:57 -07002715 if (!ieee80211_get_morefrag(hdr)) {
2716 txq->need_update = 1;
2717 if (qc) {
2718 u8 tid = (u8)(le16_to_cpu(*qc) & 0xf);
2719 priv->stations[sta_id].tid[tid].seq_number = seq_number;
2720 }
2721 } else {
2722 wait_write_ptr = 1;
2723 txq->need_update = 0;
2724 }
2725
Tomas Winkler0a6857e2008-03-12 16:58:49 -07002726 iwl_print_hex_dump(IWL_DL_TX, out_cmd->cmd.payload,
Zhu Yib481de92007-09-25 17:54:57 -07002727 sizeof(out_cmd->cmd.tx));
2728
Tomas Winkler0a6857e2008-03-12 16:58:49 -07002729 iwl_print_hex_dump(IWL_DL_TX, (u8 *)out_cmd->cmd.tx.hdr,
Zhu Yib481de92007-09-25 17:54:57 -07002730 ieee80211_get_hdrlen(fc));
2731
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002732 /* Set up entry for this TFD in Tx byte-count array */
Zhu Yib481de92007-09-25 17:54:57 -07002733 iwl4965_tx_queue_update_wr_ptr(priv, txq, len);
2734
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002735 /* Tell device the write index *just past* this latest filled TFD */
Tomas Winklerc54b6792008-03-06 17:36:53 -08002736 q->write_ptr = iwl_queue_inc_wrap(q->write_ptr, q->n_bd);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002737 rc = iwl4965_tx_queue_update_write_ptr(priv, txq);
Zhu Yib481de92007-09-25 17:54:57 -07002738 spin_unlock_irqrestore(&priv->lock, flags);
2739
2740 if (rc)
2741 return rc;
2742
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002743 if ((iwl4965_queue_space(q) < q->high_mark)
Zhu Yib481de92007-09-25 17:54:57 -07002744 && priv->mac80211_registered) {
2745 if (wait_write_ptr) {
2746 spin_lock_irqsave(&priv->lock, flags);
2747 txq->need_update = 1;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002748 iwl4965_tx_queue_update_write_ptr(priv, txq);
Zhu Yib481de92007-09-25 17:54:57 -07002749 spin_unlock_irqrestore(&priv->lock, flags);
2750 }
2751
2752 ieee80211_stop_queue(priv->hw, ctl->queue);
2753 }
2754
2755 return 0;
2756
2757drop_unlock:
2758 spin_unlock_irqrestore(&priv->lock, flags);
2759drop:
2760 return -1;
2761}
2762
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002763static void iwl4965_set_rate(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002764{
Johannes Berg8318d782008-01-24 19:38:38 +01002765 const struct ieee80211_supported_band *hw = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07002766 struct ieee80211_rate *rate;
2767 int i;
2768
Johannes Berg8318d782008-01-24 19:38:38 +01002769 hw = iwl4965_get_hw_mode(priv, priv->band);
Saleem Abdulrasoolc4ba9622007-11-18 23:59:08 -08002770 if (!hw) {
2771 IWL_ERROR("Failed to set rate: unable to get hw mode\n");
2772 return;
2773 }
Zhu Yib481de92007-09-25 17:54:57 -07002774
2775 priv->active_rate = 0;
2776 priv->active_rate_basic = 0;
2777
Johannes Berg8318d782008-01-24 19:38:38 +01002778 for (i = 0; i < hw->n_bitrates; i++) {
2779 rate = &(hw->bitrates[i]);
2780 if (rate->hw_value < IWL_RATE_COUNT)
2781 priv->active_rate |= (1 << rate->hw_value);
Zhu Yib481de92007-09-25 17:54:57 -07002782 }
2783
2784 IWL_DEBUG_RATE("Set active_rate = %0x, active_rate_basic = %0x\n",
2785 priv->active_rate, priv->active_rate_basic);
2786
2787 /*
2788 * If a basic rate is configured, then use it (adding IWL_RATE_1M_MASK)
2789 * otherwise set it to the default of all CCK rates and 6, 12, 24 for
2790 * OFDM
2791 */
2792 if (priv->active_rate_basic & IWL_CCK_BASIC_RATES_MASK)
2793 priv->staging_rxon.cck_basic_rates =
2794 ((priv->active_rate_basic &
2795 IWL_CCK_RATES_MASK) >> IWL_FIRST_CCK_RATE) & 0xF;
2796 else
2797 priv->staging_rxon.cck_basic_rates =
2798 (IWL_CCK_BASIC_RATES_MASK >> IWL_FIRST_CCK_RATE) & 0xF;
2799
2800 if (priv->active_rate_basic & IWL_OFDM_BASIC_RATES_MASK)
2801 priv->staging_rxon.ofdm_basic_rates =
2802 ((priv->active_rate_basic &
2803 (IWL_OFDM_BASIC_RATES_MASK | IWL_RATE_6M_MASK)) >>
2804 IWL_FIRST_OFDM_RATE) & 0xFF;
2805 else
2806 priv->staging_rxon.ofdm_basic_rates =
2807 (IWL_OFDM_BASIC_RATES_MASK >> IWL_FIRST_OFDM_RATE) & 0xFF;
2808}
2809
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002810static void iwl4965_radio_kill_sw(struct iwl_priv *priv, int disable_radio)
Zhu Yib481de92007-09-25 17:54:57 -07002811{
2812 unsigned long flags;
2813
2814 if (!!disable_radio == test_bit(STATUS_RF_KILL_SW, &priv->status))
2815 return;
2816
2817 IWL_DEBUG_RF_KILL("Manual SW RF KILL set to: RADIO %s\n",
2818 disable_radio ? "OFF" : "ON");
2819
2820 if (disable_radio) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002821 iwl4965_scan_cancel(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002822 /* FIXME: This is a workaround for AP */
2823 if (priv->iw_mode != IEEE80211_IF_TYPE_AP) {
2824 spin_lock_irqsave(&priv->lock, flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002825 iwl4965_write32(priv, CSR_UCODE_DRV_GP1_SET,
Zhu Yib481de92007-09-25 17:54:57 -07002826 CSR_UCODE_SW_BIT_RFKILL);
2827 spin_unlock_irqrestore(&priv->lock, flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002828 iwl4965_send_card_state(priv, CARD_STATE_CMD_DISABLE, 0);
Zhu Yib481de92007-09-25 17:54:57 -07002829 set_bit(STATUS_RF_KILL_SW, &priv->status);
2830 }
2831 return;
2832 }
2833
2834 spin_lock_irqsave(&priv->lock, flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002835 iwl4965_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
Zhu Yib481de92007-09-25 17:54:57 -07002836
2837 clear_bit(STATUS_RF_KILL_SW, &priv->status);
2838 spin_unlock_irqrestore(&priv->lock, flags);
2839
2840 /* wake up ucode */
2841 msleep(10);
2842
2843 spin_lock_irqsave(&priv->lock, flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002844 iwl4965_read32(priv, CSR_UCODE_DRV_GP1);
2845 if (!iwl4965_grab_nic_access(priv))
2846 iwl4965_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002847 spin_unlock_irqrestore(&priv->lock, flags);
2848
2849 if (test_bit(STATUS_RF_KILL_HW, &priv->status)) {
2850 IWL_DEBUG_RF_KILL("Can not turn radio back on - "
2851 "disabled by HW switch\n");
2852 return;
2853 }
2854
2855 queue_work(priv->workqueue, &priv->restart);
2856 return;
2857}
2858
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002859void iwl4965_set_decrypted_flag(struct iwl_priv *priv, struct sk_buff *skb,
Zhu Yib481de92007-09-25 17:54:57 -07002860 u32 decrypt_res, struct ieee80211_rx_status *stats)
2861{
2862 u16 fc =
2863 le16_to_cpu(((struct ieee80211_hdr *)skb->data)->frame_control);
2864
2865 if (priv->active_rxon.filter_flags & RXON_FILTER_DIS_DECRYPT_MSK)
2866 return;
2867
2868 if (!(fc & IEEE80211_FCTL_PROTECTED))
2869 return;
2870
2871 IWL_DEBUG_RX("decrypt_res:0x%x\n", decrypt_res);
2872 switch (decrypt_res & RX_RES_STATUS_SEC_TYPE_MSK) {
2873 case RX_RES_STATUS_SEC_TYPE_TKIP:
Emmanuel Grumbach17e476b2008-03-19 16:41:42 -07002874 /* The uCode has got a bad phase 1 Key, pushes the packet.
2875 * Decryption will be done in SW. */
2876 if ((decrypt_res & RX_RES_STATUS_DECRYPT_TYPE_MSK) ==
2877 RX_RES_STATUS_BAD_KEY_TTAK)
2878 break;
2879
Zhu Yib481de92007-09-25 17:54:57 -07002880 if ((decrypt_res & RX_RES_STATUS_DECRYPT_TYPE_MSK) ==
2881 RX_RES_STATUS_BAD_ICV_MIC)
2882 stats->flag |= RX_FLAG_MMIC_ERROR;
2883 case RX_RES_STATUS_SEC_TYPE_WEP:
2884 case RX_RES_STATUS_SEC_TYPE_CCMP:
2885 if ((decrypt_res & RX_RES_STATUS_DECRYPT_TYPE_MSK) ==
2886 RX_RES_STATUS_DECRYPT_OK) {
2887 IWL_DEBUG_RX("hw decrypt successfully!!!\n");
2888 stats->flag |= RX_FLAG_DECRYPTED;
2889 }
2890 break;
2891
2892 default:
2893 break;
2894 }
2895}
2896
Zhu Yib481de92007-09-25 17:54:57 -07002897
2898#define IWL_PACKET_RETRY_TIME HZ
2899
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002900int iwl4965_is_duplicate_packet(struct iwl_priv *priv, struct ieee80211_hdr *header)
Zhu Yib481de92007-09-25 17:54:57 -07002901{
2902 u16 sc = le16_to_cpu(header->seq_ctrl);
2903 u16 seq = (sc & IEEE80211_SCTL_SEQ) >> 4;
2904 u16 frag = sc & IEEE80211_SCTL_FRAG;
2905 u16 *last_seq, *last_frag;
2906 unsigned long *last_time;
2907
2908 switch (priv->iw_mode) {
2909 case IEEE80211_IF_TYPE_IBSS:{
2910 struct list_head *p;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002911 struct iwl4965_ibss_seq *entry = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07002912 u8 *mac = header->addr2;
2913 int index = mac[5] & (IWL_IBSS_MAC_HASH_SIZE - 1);
2914
2915 __list_for_each(p, &priv->ibss_mac_hash[index]) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002916 entry = list_entry(p, struct iwl4965_ibss_seq, list);
Zhu Yib481de92007-09-25 17:54:57 -07002917 if (!compare_ether_addr(entry->mac, mac))
2918 break;
2919 }
2920 if (p == &priv->ibss_mac_hash[index]) {
2921 entry = kzalloc(sizeof(*entry), GFP_ATOMIC);
2922 if (!entry) {
Ian Schrambc434dd2007-10-25 17:15:29 +08002923 IWL_ERROR("Cannot malloc new mac entry\n");
Zhu Yib481de92007-09-25 17:54:57 -07002924 return 0;
2925 }
2926 memcpy(entry->mac, mac, ETH_ALEN);
2927 entry->seq_num = seq;
2928 entry->frag_num = frag;
2929 entry->packet_time = jiffies;
Ian Schrambc434dd2007-10-25 17:15:29 +08002930 list_add(&entry->list, &priv->ibss_mac_hash[index]);
Zhu Yib481de92007-09-25 17:54:57 -07002931 return 0;
2932 }
2933 last_seq = &entry->seq_num;
2934 last_frag = &entry->frag_num;
2935 last_time = &entry->packet_time;
2936 break;
2937 }
2938 case IEEE80211_IF_TYPE_STA:
2939 last_seq = &priv->last_seq_num;
2940 last_frag = &priv->last_frag_num;
2941 last_time = &priv->last_packet_time;
2942 break;
2943 default:
2944 return 0;
2945 }
2946 if ((*last_seq == seq) &&
2947 time_after(*last_time + IWL_PACKET_RETRY_TIME, jiffies)) {
2948 if (*last_frag == frag)
2949 goto drop;
2950 if (*last_frag + 1 != frag)
2951 /* out-of-order fragment */
2952 goto drop;
2953 } else
2954 *last_seq = seq;
2955
2956 *last_frag = frag;
2957 *last_time = jiffies;
2958 return 0;
2959
2960 drop:
2961 return 1;
2962}
2963
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08002964#ifdef CONFIG_IWL4965_SPECTRUM_MEASUREMENT
Zhu Yib481de92007-09-25 17:54:57 -07002965
2966#include "iwl-spectrum.h"
2967
2968#define BEACON_TIME_MASK_LOW 0x00FFFFFF
2969#define BEACON_TIME_MASK_HIGH 0xFF000000
2970#define TIME_UNIT 1024
2971
2972/*
2973 * extended beacon time format
2974 * time in usec will be changed into a 32-bit value in 8:24 format
2975 * the high 1 byte is the beacon counts
2976 * the lower 3 bytes is the time in usec within one beacon interval
2977 */
2978
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002979static u32 iwl4965_usecs_to_beacons(u32 usec, u32 beacon_interval)
Zhu Yib481de92007-09-25 17:54:57 -07002980{
2981 u32 quot;
2982 u32 rem;
2983 u32 interval = beacon_interval * 1024;
2984
2985 if (!interval || !usec)
2986 return 0;
2987
2988 quot = (usec / interval) & (BEACON_TIME_MASK_HIGH >> 24);
2989 rem = (usec % interval) & BEACON_TIME_MASK_LOW;
2990
2991 return (quot << 24) + rem;
2992}
2993
2994/* base is usually what we get from ucode with each received frame,
2995 * the same as HW timer counter counting down
2996 */
2997
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002998static __le32 iwl4965_add_beacon_time(u32 base, u32 addon, u32 beacon_interval)
Zhu Yib481de92007-09-25 17:54:57 -07002999{
3000 u32 base_low = base & BEACON_TIME_MASK_LOW;
3001 u32 addon_low = addon & BEACON_TIME_MASK_LOW;
3002 u32 interval = beacon_interval * TIME_UNIT;
3003 u32 res = (base & BEACON_TIME_MASK_HIGH) +
3004 (addon & BEACON_TIME_MASK_HIGH);
3005
3006 if (base_low > addon_low)
3007 res += base_low - addon_low;
3008 else if (base_low < addon_low) {
3009 res += interval + base_low - addon_low;
3010 res += (1 << 24);
3011 } else
3012 res += (1 << 24);
3013
3014 return cpu_to_le32(res);
3015}
3016
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003017static int iwl4965_get_measurement(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07003018 struct ieee80211_measurement_params *params,
3019 u8 type)
3020{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003021 struct iwl4965_spectrum_cmd spectrum;
3022 struct iwl4965_rx_packet *res;
3023 struct iwl4965_host_cmd cmd = {
Zhu Yib481de92007-09-25 17:54:57 -07003024 .id = REPLY_SPECTRUM_MEASUREMENT_CMD,
3025 .data = (void *)&spectrum,
3026 .meta.flags = CMD_WANT_SKB,
3027 };
3028 u32 add_time = le64_to_cpu(params->start_time);
3029 int rc;
3030 int spectrum_resp_status;
3031 int duration = le16_to_cpu(params->duration);
3032
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003033 if (iwl4965_is_associated(priv))
Zhu Yib481de92007-09-25 17:54:57 -07003034 add_time =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003035 iwl4965_usecs_to_beacons(
Zhu Yib481de92007-09-25 17:54:57 -07003036 le64_to_cpu(params->start_time) - priv->last_tsf,
3037 le16_to_cpu(priv->rxon_timing.beacon_interval));
3038
3039 memset(&spectrum, 0, sizeof(spectrum));
3040
3041 spectrum.channel_count = cpu_to_le16(1);
3042 spectrum.flags =
3043 RXON_FLG_TSF2HOST_MSK | RXON_FLG_ANT_A_MSK | RXON_FLG_DIS_DIV_MSK;
3044 spectrum.filter_flags = MEASUREMENT_FILTER_FLAG;
3045 cmd.len = sizeof(spectrum);
3046 spectrum.len = cpu_to_le16(cmd.len - sizeof(spectrum.len));
3047
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003048 if (iwl4965_is_associated(priv))
Zhu Yib481de92007-09-25 17:54:57 -07003049 spectrum.start_time =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003050 iwl4965_add_beacon_time(priv->last_beacon_time,
Zhu Yib481de92007-09-25 17:54:57 -07003051 add_time,
3052 le16_to_cpu(priv->rxon_timing.beacon_interval));
3053 else
3054 spectrum.start_time = 0;
3055
3056 spectrum.channels[0].duration = cpu_to_le32(duration * TIME_UNIT);
3057 spectrum.channels[0].channel = params->channel;
3058 spectrum.channels[0].type = type;
3059 if (priv->active_rxon.flags & RXON_FLG_BAND_24G_MSK)
3060 spectrum.flags |= RXON_FLG_BAND_24G_MSK |
3061 RXON_FLG_AUTO_DETECT_MSK | RXON_FLG_TGG_PROTECT_MSK;
3062
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003063 rc = iwl4965_send_cmd_sync(priv, &cmd);
Zhu Yib481de92007-09-25 17:54:57 -07003064 if (rc)
3065 return rc;
3066
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003067 res = (struct iwl4965_rx_packet *)cmd.meta.u.skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07003068 if (res->hdr.flags & IWL_CMD_FAILED_MSK) {
3069 IWL_ERROR("Bad return from REPLY_RX_ON_ASSOC command\n");
3070 rc = -EIO;
3071 }
3072
3073 spectrum_resp_status = le16_to_cpu(res->u.spectrum.status);
3074 switch (spectrum_resp_status) {
3075 case 0: /* Command will be handled */
3076 if (res->u.spectrum.id != 0xff) {
3077 IWL_DEBUG_INFO
3078 ("Replaced existing measurement: %d\n",
3079 res->u.spectrum.id);
3080 priv->measurement_status &= ~MEASUREMENT_READY;
3081 }
3082 priv->measurement_status |= MEASUREMENT_ACTIVE;
3083 rc = 0;
3084 break;
3085
3086 case 1: /* Command will not be handled */
3087 rc = -EAGAIN;
3088 break;
3089 }
3090
3091 dev_kfree_skb_any(cmd.meta.u.skb);
3092
3093 return rc;
3094}
3095#endif
3096
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003097static void iwl4965_txstatus_to_ieee(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003098 struct iwl4965_tx_info *tx_sta)
Zhu Yib481de92007-09-25 17:54:57 -07003099{
3100
3101 tx_sta->status.ack_signal = 0;
3102 tx_sta->status.excessive_retries = 0;
3103 tx_sta->status.queue_length = 0;
3104 tx_sta->status.queue_number = 0;
3105
3106 if (in_interrupt())
3107 ieee80211_tx_status_irqsafe(priv->hw,
3108 tx_sta->skb[0], &(tx_sta->status));
3109 else
3110 ieee80211_tx_status(priv->hw,
3111 tx_sta->skb[0], &(tx_sta->status));
3112
3113 tx_sta->skb[0] = NULL;
3114}
3115
3116/**
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003117 * iwl4965_tx_queue_reclaim - Reclaim Tx queue entries already Tx'd
Zhu Yib481de92007-09-25 17:54:57 -07003118 *
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003119 * When FW advances 'R' index, all entries between old and new 'R' index
3120 * need to be reclaimed. As result, some free space forms. If there is
3121 * enough free space (> low mark), wake the stack that feeds us.
Zhu Yib481de92007-09-25 17:54:57 -07003122 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003123int iwl4965_tx_queue_reclaim(struct iwl_priv *priv, int txq_id, int index)
Zhu Yib481de92007-09-25 17:54:57 -07003124{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003125 struct iwl4965_tx_queue *txq = &priv->txq[txq_id];
3126 struct iwl4965_queue *q = &txq->q;
Zhu Yib481de92007-09-25 17:54:57 -07003127 int nfreed = 0;
3128
3129 if ((index >= q->n_bd) || (x2_queue_used(q, index) == 0)) {
3130 IWL_ERROR("Read index for DMA queue txq id (%d), index %d, "
3131 "is out of range [0-%d] %d %d.\n", txq_id,
Tomas Winklerfc4b6852007-10-25 17:15:24 +08003132 index, q->n_bd, q->write_ptr, q->read_ptr);
Zhu Yib481de92007-09-25 17:54:57 -07003133 return 0;
3134 }
3135
Tomas Winklerc54b6792008-03-06 17:36:53 -08003136 for (index = iwl_queue_inc_wrap(index, q->n_bd);
Tomas Winklerfc4b6852007-10-25 17:15:24 +08003137 q->read_ptr != index;
Tomas Winklerc54b6792008-03-06 17:36:53 -08003138 q->read_ptr = iwl_queue_inc_wrap(q->read_ptr, q->n_bd)) {
Zhu Yib481de92007-09-25 17:54:57 -07003139 if (txq_id != IWL_CMD_QUEUE_NUM) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003140 iwl4965_txstatus_to_ieee(priv,
Tomas Winklerfc4b6852007-10-25 17:15:24 +08003141 &(txq->txb[txq->q.read_ptr]));
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003142 iwl4965_hw_txq_free_tfd(priv, txq);
Zhu Yib481de92007-09-25 17:54:57 -07003143 } else if (nfreed > 1) {
3144 IWL_ERROR("HCMD skipped: index (%d) %d %d\n", index,
Tomas Winklerfc4b6852007-10-25 17:15:24 +08003145 q->write_ptr, q->read_ptr);
Zhu Yib481de92007-09-25 17:54:57 -07003146 queue_work(priv->workqueue, &priv->restart);
3147 }
3148 nfreed++;
3149 }
3150
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003151/* if (iwl4965_queue_space(q) > q->low_mark && (txq_id >= 0) &&
Zhu Yib481de92007-09-25 17:54:57 -07003152 (txq_id != IWL_CMD_QUEUE_NUM) &&
3153 priv->mac80211_registered)
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003154 ieee80211_wake_queue(priv->hw, txq_id); */
Zhu Yib481de92007-09-25 17:54:57 -07003155
3156
3157 return nfreed;
3158}
3159
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003160static int iwl4965_is_tx_success(u32 status)
Zhu Yib481de92007-09-25 17:54:57 -07003161{
3162 status &= TX_STATUS_MSK;
3163 return (status == TX_STATUS_SUCCESS)
3164 || (status == TX_STATUS_DIRECT_DONE);
3165}
3166
3167/******************************************************************************
3168 *
3169 * Generic RX handler implementations
3170 *
3171 ******************************************************************************/
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08003172#ifdef CONFIG_IWL4965_HT
Zhu Yib481de92007-09-25 17:54:57 -07003173
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003174static inline int iwl4965_get_ra_sta_id(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07003175 struct ieee80211_hdr *hdr)
3176{
3177 if (priv->iw_mode == IEEE80211_IF_TYPE_STA)
3178 return IWL_AP_ID;
3179 else {
3180 u8 *da = ieee80211_get_DA(hdr);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003181 return iwl4965_hw_find_station(priv, da);
Zhu Yib481de92007-09-25 17:54:57 -07003182 }
3183}
3184
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003185static struct ieee80211_hdr *iwl4965_tx_queue_get_hdr(
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003186 struct iwl_priv *priv, int txq_id, int idx)
Zhu Yib481de92007-09-25 17:54:57 -07003187{
3188 if (priv->txq[txq_id].txb[idx].skb[0])
3189 return (struct ieee80211_hdr *)priv->txq[txq_id].
3190 txb[idx].skb[0]->data;
3191 return NULL;
3192}
3193
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003194static inline u32 iwl4965_get_scd_ssn(struct iwl4965_tx_resp *tx_resp)
Zhu Yib481de92007-09-25 17:54:57 -07003195{
3196 __le32 *scd_ssn = (__le32 *)((u32 *)&tx_resp->status +
3197 tx_resp->frame_count);
3198 return le32_to_cpu(*scd_ssn) & MAX_SN;
3199
3200}
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003201
3202/**
3203 * iwl4965_tx_status_reply_tx - Handle Tx rspnse for frames in aggregation queue
3204 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003205static int iwl4965_tx_status_reply_tx(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003206 struct iwl4965_ht_agg *agg,
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003207 struct iwl4965_tx_resp_agg *tx_resp,
Zhu Yib481de92007-09-25 17:54:57 -07003208 u16 start_idx)
3209{
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003210 u16 status;
3211 struct agg_tx_status *frame_status = &tx_resp->status;
Zhu Yib481de92007-09-25 17:54:57 -07003212 struct ieee80211_tx_status *tx_status = NULL;
3213 struct ieee80211_hdr *hdr = NULL;
3214 int i, sh;
3215 int txq_id, idx;
3216 u16 seq;
3217
3218 if (agg->wait_for_ba)
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003219 IWL_DEBUG_TX_REPLY("got tx response w/o block-ack\n");
Zhu Yib481de92007-09-25 17:54:57 -07003220
3221 agg->frame_count = tx_resp->frame_count;
3222 agg->start_idx = start_idx;
3223 agg->rate_n_flags = le32_to_cpu(tx_resp->rate_n_flags);
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003224 agg->bitmap = 0;
Zhu Yib481de92007-09-25 17:54:57 -07003225
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003226 /* # frames attempted by Tx command */
Zhu Yib481de92007-09-25 17:54:57 -07003227 if (agg->frame_count == 1) {
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003228 /* Only one frame was attempted; no block-ack will arrive */
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003229 status = le16_to_cpu(frame_status[0].status);
3230 seq = le16_to_cpu(frame_status[0].sequence);
3231 idx = SEQ_TO_INDEX(seq);
3232 txq_id = SEQ_TO_QUEUE(seq);
Zhu Yib481de92007-09-25 17:54:57 -07003233
Zhu Yib481de92007-09-25 17:54:57 -07003234 /* FIXME: code repetition */
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003235 IWL_DEBUG_TX_REPLY("FrameCnt = %d, StartIdx=%d idx=%d\n",
3236 agg->frame_count, agg->start_idx, idx);
Zhu Yib481de92007-09-25 17:54:57 -07003237
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003238 tx_status = &(priv->txq[txq_id].txb[idx].status);
Zhu Yib481de92007-09-25 17:54:57 -07003239 tx_status->retry_count = tx_resp->failure_frame;
3240 tx_status->queue_number = status & 0xff;
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003241 tx_status->queue_length = tx_resp->failure_rts;
3242 tx_status->control.flags &= ~IEEE80211_TXCTL_AMPDU;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003243 tx_status->flags = iwl4965_is_tx_success(status)?
Zhu Yib481de92007-09-25 17:54:57 -07003244 IEEE80211_TX_STATUS_ACK : 0;
Ron Rindjunsky4c424e42008-03-04 18:09:27 -08003245 iwl4965_hwrate_to_tx_control(priv,
3246 le32_to_cpu(tx_resp->rate_n_flags),
3247 &tx_status->control);
Zhu Yib481de92007-09-25 17:54:57 -07003248 /* FIXME: code repetition end */
3249
3250 IWL_DEBUG_TX_REPLY("1 Frame 0x%x failure :%d\n",
3251 status & 0xff, tx_resp->failure_frame);
3252 IWL_DEBUG_TX_REPLY("Rate Info rate_n_flags=%x\n",
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003253 iwl4965_hw_get_rate_n_flags(tx_resp->rate_n_flags));
Zhu Yib481de92007-09-25 17:54:57 -07003254
3255 agg->wait_for_ba = 0;
3256 } else {
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003257 /* Two or more frames were attempted; expect block-ack */
Zhu Yib481de92007-09-25 17:54:57 -07003258 u64 bitmap = 0;
3259 int start = agg->start_idx;
3260
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003261 /* Construct bit-map of pending frames within Tx window */
Zhu Yib481de92007-09-25 17:54:57 -07003262 for (i = 0; i < agg->frame_count; i++) {
3263 u16 sc;
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003264 status = le16_to_cpu(frame_status[i].status);
3265 seq = le16_to_cpu(frame_status[i].sequence);
Zhu Yib481de92007-09-25 17:54:57 -07003266 idx = SEQ_TO_INDEX(seq);
3267 txq_id = SEQ_TO_QUEUE(seq);
3268
3269 if (status & (AGG_TX_STATE_FEW_BYTES_MSK |
3270 AGG_TX_STATE_ABORT_MSK))
3271 continue;
3272
3273 IWL_DEBUG_TX_REPLY("FrameCnt = %d, txq_id=%d idx=%d\n",
3274 agg->frame_count, txq_id, idx);
3275
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003276 hdr = iwl4965_tx_queue_get_hdr(priv, txq_id, idx);
Zhu Yib481de92007-09-25 17:54:57 -07003277
3278 sc = le16_to_cpu(hdr->seq_ctrl);
3279 if (idx != (SEQ_TO_SN(sc) & 0xff)) {
3280 IWL_ERROR("BUG_ON idx doesn't match seq control"
3281 " idx=%d, seq_idx=%d, seq=%d\n",
3282 idx, SEQ_TO_SN(sc),
3283 hdr->seq_ctrl);
3284 return -1;
3285 }
3286
3287 IWL_DEBUG_TX_REPLY("AGG Frame i=%d idx %d seq=%d\n",
3288 i, idx, SEQ_TO_SN(sc));
3289
3290 sh = idx - start;
3291 if (sh > 64) {
3292 sh = (start - idx) + 0xff;
3293 bitmap = bitmap << sh;
3294 sh = 0;
3295 start = idx;
3296 } else if (sh < -64)
3297 sh = 0xff - (start - idx);
3298 else if (sh < 0) {
3299 sh = start - idx;
3300 start = idx;
3301 bitmap = bitmap << sh;
3302 sh = 0;
3303 }
3304 bitmap |= (1 << sh);
3305 IWL_DEBUG_TX_REPLY("start=%d bitmap=0x%x\n",
3306 start, (u32)(bitmap & 0xFFFFFFFF));
3307 }
3308
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003309 agg->bitmap = bitmap;
Zhu Yib481de92007-09-25 17:54:57 -07003310 agg->start_idx = start;
3311 agg->rate_n_flags = le32_to_cpu(tx_resp->rate_n_flags);
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003312 IWL_DEBUG_TX_REPLY("Frames %d start_idx=%d bitmap=0x%llx\n",
Zhu Yib481de92007-09-25 17:54:57 -07003313 agg->frame_count, agg->start_idx,
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003314 agg->bitmap);
Zhu Yib481de92007-09-25 17:54:57 -07003315
3316 if (bitmap)
3317 agg->wait_for_ba = 1;
3318 }
3319 return 0;
3320}
3321#endif
Zhu Yib481de92007-09-25 17:54:57 -07003322
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003323/**
3324 * iwl4965_rx_reply_tx - Handle standard (non-aggregation) Tx response
3325 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003326static void iwl4965_rx_reply_tx(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003327 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003328{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003329 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07003330 u16 sequence = le16_to_cpu(pkt->hdr.sequence);
3331 int txq_id = SEQ_TO_QUEUE(sequence);
3332 int index = SEQ_TO_INDEX(sequence);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003333 struct iwl4965_tx_queue *txq = &priv->txq[txq_id];
Zhu Yib481de92007-09-25 17:54:57 -07003334 struct ieee80211_tx_status *tx_status;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003335 struct iwl4965_tx_resp *tx_resp = (void *)&pkt->u.raw[0];
Zhu Yib481de92007-09-25 17:54:57 -07003336 u32 status = le32_to_cpu(tx_resp->status);
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08003337#ifdef CONFIG_IWL4965_HT
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003338 int tid = MAX_TID_COUNT, sta_id = IWL_INVALID_STATION;
3339 struct ieee80211_hdr *hdr;
3340 __le16 *qc;
Zhu Yib481de92007-09-25 17:54:57 -07003341#endif
3342
3343 if ((index >= txq->q.n_bd) || (x2_queue_used(&txq->q, index) == 0)) {
3344 IWL_ERROR("Read index for DMA queue txq_id (%d) index %d "
3345 "is out of range [0-%d] %d %d\n", txq_id,
Tomas Winklerfc4b6852007-10-25 17:15:24 +08003346 index, txq->q.n_bd, txq->q.write_ptr,
3347 txq->q.read_ptr);
Zhu Yib481de92007-09-25 17:54:57 -07003348 return;
3349 }
3350
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08003351#ifdef CONFIG_IWL4965_HT
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003352 hdr = iwl4965_tx_queue_get_hdr(priv, txq_id, index);
3353 qc = ieee80211_get_qos_ctrl(hdr);
Zhu Yib481de92007-09-25 17:54:57 -07003354
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003355 if (qc)
Zhu Yib481de92007-09-25 17:54:57 -07003356 tid = le16_to_cpu(*qc) & 0xf;
3357
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003358 sta_id = iwl4965_get_ra_sta_id(priv, hdr);
3359 if (txq->sched_retry && unlikely(sta_id == IWL_INVALID_STATION)) {
3360 IWL_ERROR("Station not known\n");
3361 return;
3362 }
3363
3364 if (txq->sched_retry) {
3365 const u32 scd_ssn = iwl4965_get_scd_ssn(tx_resp);
3366 struct iwl4965_ht_agg *agg = NULL;
3367
3368 if (!qc)
Zhu Yib481de92007-09-25 17:54:57 -07003369 return;
Zhu Yib481de92007-09-25 17:54:57 -07003370
3371 agg = &priv->stations[sta_id].tid[tid].agg;
3372
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003373 iwl4965_tx_status_reply_tx(priv, agg,
3374 (struct iwl4965_tx_resp_agg *)tx_resp, index);
Zhu Yib481de92007-09-25 17:54:57 -07003375
3376 if ((tx_resp->frame_count == 1) &&
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003377 !iwl4965_is_tx_success(status)) {
Zhu Yib481de92007-09-25 17:54:57 -07003378 /* TODO: send BAR */
3379 }
3380
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003381 if (txq->q.read_ptr != (scd_ssn & 0xff)) {
3382 int freed;
Tomas Winklerc54b6792008-03-06 17:36:53 -08003383 index = iwl_queue_dec_wrap(scd_ssn & 0xff, txq->q.n_bd);
Zhu Yib481de92007-09-25 17:54:57 -07003384 IWL_DEBUG_TX_REPLY("Retry scheduler reclaim scd_ssn "
3385 "%d index %d\n", scd_ssn , index);
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003386 freed = iwl4965_tx_queue_reclaim(priv, txq_id, index);
3387 priv->stations[sta_id].tid[tid].tfds_in_queue -= freed;
3388
3389 if (iwl4965_queue_space(&txq->q) > txq->q.low_mark &&
3390 txq_id >= 0 && priv->mac80211_registered &&
3391 agg->state != IWL_EMPTYING_HW_QUEUE_DELBA)
3392 ieee80211_wake_queue(priv->hw, txq_id);
3393
3394 iwl4965_check_empty_hw_queue(priv, sta_id, tid, txq_id);
Zhu Yib481de92007-09-25 17:54:57 -07003395 }
3396 } else {
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08003397#endif /* CONFIG_IWL4965_HT */
Tomas Winklerfc4b6852007-10-25 17:15:24 +08003398 tx_status = &(txq->txb[txq->q.read_ptr].status);
Zhu Yib481de92007-09-25 17:54:57 -07003399
3400 tx_status->retry_count = tx_resp->failure_frame;
3401 tx_status->queue_number = status;
3402 tx_status->queue_length = tx_resp->bt_kill_count;
3403 tx_status->queue_length |= tx_resp->failure_rts;
Zhu Yib481de92007-09-25 17:54:57 -07003404 tx_status->flags =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003405 iwl4965_is_tx_success(status) ? IEEE80211_TX_STATUS_ACK : 0;
Ron Rindjunsky4c424e42008-03-04 18:09:27 -08003406 iwl4965_hwrate_to_tx_control(priv, le32_to_cpu(tx_resp->rate_n_flags),
3407 &tx_status->control);
Zhu Yib481de92007-09-25 17:54:57 -07003408
Zhu Yib481de92007-09-25 17:54:57 -07003409 IWL_DEBUG_TX("Tx queue %d Status %s (0x%08x) rate_n_flags 0x%x "
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003410 "retries %d\n", txq_id, iwl4965_get_tx_fail_reason(status),
Zhu Yib481de92007-09-25 17:54:57 -07003411 status, le32_to_cpu(tx_resp->rate_n_flags),
3412 tx_resp->failure_frame);
3413
3414 IWL_DEBUG_TX_REPLY("Tx queue reclaim %d\n", index);
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003415 if (index != -1) {
3416 int freed = iwl4965_tx_queue_reclaim(priv, txq_id, index);
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08003417#ifdef CONFIG_IWL4965_HT
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003418 if (tid != MAX_TID_COUNT)
3419 priv->stations[sta_id].tid[tid].tfds_in_queue -= freed;
3420 if (iwl4965_queue_space(&txq->q) > txq->q.low_mark &&
3421 (txq_id >= 0) &&
3422 priv->mac80211_registered)
3423 ieee80211_wake_queue(priv->hw, txq_id);
3424 if (tid != MAX_TID_COUNT)
3425 iwl4965_check_empty_hw_queue(priv, sta_id, tid, txq_id);
3426#endif
Zhu Yib481de92007-09-25 17:54:57 -07003427 }
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003428#ifdef CONFIG_IWL4965_HT
3429 }
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08003430#endif /* CONFIG_IWL4965_HT */
Zhu Yib481de92007-09-25 17:54:57 -07003431
3432 if (iwl_check_bits(status, TX_ABORT_REQUIRED_MSK))
3433 IWL_ERROR("TODO: Implement Tx ABORT REQUIRED!!!\n");
3434}
3435
3436
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003437static void iwl4965_rx_reply_alive(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003438 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003439{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003440 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
3441 struct iwl4965_alive_resp *palive;
Zhu Yib481de92007-09-25 17:54:57 -07003442 struct delayed_work *pwork;
3443
3444 palive = &pkt->u.alive_frame;
3445
3446 IWL_DEBUG_INFO("Alive ucode status 0x%08X revision "
3447 "0x%01X 0x%01X\n",
3448 palive->is_valid, palive->ver_type,
3449 palive->ver_subtype);
3450
3451 if (palive->ver_subtype == INITIALIZE_SUBTYPE) {
3452 IWL_DEBUG_INFO("Initialization Alive received.\n");
3453 memcpy(&priv->card_alive_init,
3454 &pkt->u.alive_frame,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003455 sizeof(struct iwl4965_init_alive_resp));
Zhu Yib481de92007-09-25 17:54:57 -07003456 pwork = &priv->init_alive_start;
3457 } else {
3458 IWL_DEBUG_INFO("Runtime Alive received.\n");
3459 memcpy(&priv->card_alive, &pkt->u.alive_frame,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003460 sizeof(struct iwl4965_alive_resp));
Zhu Yib481de92007-09-25 17:54:57 -07003461 pwork = &priv->alive_start;
3462 }
3463
3464 /* We delay the ALIVE response by 5ms to
3465 * give the HW RF Kill time to activate... */
3466 if (palive->is_valid == UCODE_VALID_OK)
3467 queue_delayed_work(priv->workqueue, pwork,
3468 msecs_to_jiffies(5));
3469 else
3470 IWL_WARNING("uCode did not respond OK.\n");
3471}
3472
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003473static void iwl4965_rx_reply_add_sta(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003474 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003475{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003476 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07003477
3478 IWL_DEBUG_RX("Received REPLY_ADD_STA: 0x%02X\n", pkt->u.status);
3479 return;
3480}
3481
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003482static void iwl4965_rx_reply_error(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003483 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;
Zhu Yib481de92007-09-25 17:54:57 -07003486
3487 IWL_ERROR("Error Reply type 0x%08X cmd %s (0x%02X) "
3488 "seq 0x%04X ser 0x%08X\n",
3489 le32_to_cpu(pkt->u.err_resp.error_type),
3490 get_cmd_string(pkt->u.err_resp.cmd_id),
3491 pkt->u.err_resp.cmd_id,
3492 le16_to_cpu(pkt->u.err_resp.bad_cmd_seq_num),
3493 le32_to_cpu(pkt->u.err_resp.error_info));
3494}
3495
3496#define TX_STATUS_ENTRY(x) case TX_STATUS_FAIL_ ## x: return #x
3497
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003498static void iwl4965_rx_csa(struct iwl_priv *priv, struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003499{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003500 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
3501 struct iwl4965_rxon_cmd *rxon = (void *)&priv->active_rxon;
3502 struct iwl4965_csa_notification *csa = &(pkt->u.csa_notif);
Zhu Yib481de92007-09-25 17:54:57 -07003503 IWL_DEBUG_11H("CSA notif: channel %d, status %d\n",
3504 le16_to_cpu(csa->channel), le32_to_cpu(csa->status));
3505 rxon->channel = csa->channel;
3506 priv->staging_rxon.channel = csa->channel;
3507}
3508
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003509static void iwl4965_rx_spectrum_measure_notif(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003510 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003511{
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08003512#ifdef CONFIG_IWL4965_SPECTRUM_MEASUREMENT
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003513 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
3514 struct iwl4965_spectrum_notification *report = &(pkt->u.spectrum_notif);
Zhu Yib481de92007-09-25 17:54:57 -07003515
3516 if (!report->state) {
3517 IWL_DEBUG(IWL_DL_11H | IWL_DL_INFO,
3518 "Spectrum Measure Notification: Start\n");
3519 return;
3520 }
3521
3522 memcpy(&priv->measure_report, report, sizeof(*report));
3523 priv->measurement_status |= MEASUREMENT_READY;
3524#endif
3525}
3526
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003527static void iwl4965_rx_pm_sleep_notif(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003528 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003529{
Tomas Winkler0a6857e2008-03-12 16:58:49 -07003530#ifdef CONFIG_IWLWIFI_DEBUG
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003531 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
3532 struct iwl4965_sleep_notification *sleep = &(pkt->u.sleep_notif);
Zhu Yib481de92007-09-25 17:54:57 -07003533 IWL_DEBUG_RX("sleep mode: %d, src: %d\n",
3534 sleep->pm_sleep_mode, sleep->pm_wakeup_src);
3535#endif
3536}
3537
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003538static void iwl4965_rx_pm_debug_statistics_notif(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003539 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003540{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003541 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07003542 IWL_DEBUG_RADIO("Dumping %d bytes of unhandled "
3543 "notification for %s:\n",
3544 le32_to_cpu(pkt->len), get_cmd_string(pkt->hdr.cmd));
Tomas Winkler0a6857e2008-03-12 16:58:49 -07003545 iwl_print_hex_dump(IWL_DL_RADIO, pkt->u.raw, le32_to_cpu(pkt->len));
Zhu Yib481de92007-09-25 17:54:57 -07003546}
3547
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003548static void iwl4965_bg_beacon_update(struct work_struct *work)
Zhu Yib481de92007-09-25 17:54:57 -07003549{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003550 struct iwl_priv *priv =
3551 container_of(work, struct iwl_priv, beacon_update);
Zhu Yib481de92007-09-25 17:54:57 -07003552 struct sk_buff *beacon;
3553
3554 /* Pull updated AP beacon from mac80211. will fail if not in AP mode */
Johannes Berg32bfd352007-12-19 01:31:26 +01003555 beacon = ieee80211_beacon_get(priv->hw, priv->vif, NULL);
Zhu Yib481de92007-09-25 17:54:57 -07003556
3557 if (!beacon) {
3558 IWL_ERROR("update beacon failed\n");
3559 return;
3560 }
3561
3562 mutex_lock(&priv->mutex);
3563 /* new beacon skb is allocated every time; dispose previous.*/
3564 if (priv->ibss_beacon)
3565 dev_kfree_skb(priv->ibss_beacon);
3566
3567 priv->ibss_beacon = beacon;
3568 mutex_unlock(&priv->mutex);
3569
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003570 iwl4965_send_beacon_cmd(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003571}
3572
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003573static void iwl4965_rx_beacon_notif(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003574 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003575{
Tomas Winkler0a6857e2008-03-12 16:58:49 -07003576#ifdef CONFIG_IWLWIFI_DEBUG
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003577 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
3578 struct iwl4965_beacon_notif *beacon = &(pkt->u.beacon_status);
3579 u8 rate = iwl4965_hw_get_rate(beacon->beacon_notify_hdr.rate_n_flags);
Zhu Yib481de92007-09-25 17:54:57 -07003580
3581 IWL_DEBUG_RX("beacon status %x retries %d iss %d "
3582 "tsf %d %d rate %d\n",
3583 le32_to_cpu(beacon->beacon_notify_hdr.status) & TX_STATUS_MSK,
3584 beacon->beacon_notify_hdr.failure_frame,
3585 le32_to_cpu(beacon->ibss_mgr_status),
3586 le32_to_cpu(beacon->high_tsf),
3587 le32_to_cpu(beacon->low_tsf), rate);
3588#endif
3589
3590 if ((priv->iw_mode == IEEE80211_IF_TYPE_AP) &&
3591 (!test_bit(STATUS_EXIT_PENDING, &priv->status)))
3592 queue_work(priv->workqueue, &priv->beacon_update);
3593}
3594
3595/* Service response to REPLY_SCAN_CMD (0x80) */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003596static void iwl4965_rx_reply_scan(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003597 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003598{
Tomas Winkler0a6857e2008-03-12 16:58:49 -07003599#ifdef CONFIG_IWLWIFI_DEBUG
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003600 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
3601 struct iwl4965_scanreq_notification *notif =
3602 (struct iwl4965_scanreq_notification *)pkt->u.raw;
Zhu Yib481de92007-09-25 17:54:57 -07003603
3604 IWL_DEBUG_RX("Scan request status = 0x%x\n", notif->status);
3605#endif
3606}
3607
3608/* Service SCAN_START_NOTIFICATION (0x82) */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003609static void iwl4965_rx_scan_start_notif(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003610 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003611{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003612 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
3613 struct iwl4965_scanstart_notification *notif =
3614 (struct iwl4965_scanstart_notification *)pkt->u.raw;
Zhu Yib481de92007-09-25 17:54:57 -07003615 priv->scan_start_tsf = le32_to_cpu(notif->tsf_low);
3616 IWL_DEBUG_SCAN("Scan start: "
3617 "%d [802.11%s] "
3618 "(TSF: 0x%08X:%08X) - %d (beacon timer %u)\n",
3619 notif->channel,
3620 notif->band ? "bg" : "a",
3621 notif->tsf_high,
3622 notif->tsf_low, notif->status, notif->beacon_timer);
3623}
3624
3625/* Service SCAN_RESULTS_NOTIFICATION (0x83) */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003626static void iwl4965_rx_scan_results_notif(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003627 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003628{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003629 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
3630 struct iwl4965_scanresults_notification *notif =
3631 (struct iwl4965_scanresults_notification *)pkt->u.raw;
Zhu Yib481de92007-09-25 17:54:57 -07003632
3633 IWL_DEBUG_SCAN("Scan ch.res: "
3634 "%d [802.11%s] "
3635 "(TSF: 0x%08X:%08X) - %d "
3636 "elapsed=%lu usec (%dms since last)\n",
3637 notif->channel,
3638 notif->band ? "bg" : "a",
3639 le32_to_cpu(notif->tsf_high),
3640 le32_to_cpu(notif->tsf_low),
3641 le32_to_cpu(notif->statistics[0]),
3642 le32_to_cpu(notif->tsf_low) - priv->scan_start_tsf,
3643 jiffies_to_msecs(elapsed_jiffies
3644 (priv->last_scan_jiffies, jiffies)));
3645
3646 priv->last_scan_jiffies = jiffies;
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08003647 priv->next_scan_jiffies = 0;
Zhu Yib481de92007-09-25 17:54:57 -07003648}
3649
3650/* Service SCAN_COMPLETE_NOTIFICATION (0x84) */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003651static void iwl4965_rx_scan_complete_notif(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003652 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003653{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003654 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
3655 struct iwl4965_scancomplete_notification *scan_notif = (void *)pkt->u.raw;
Zhu Yib481de92007-09-25 17:54:57 -07003656
3657 IWL_DEBUG_SCAN("Scan complete: %d channels (TSF 0x%08X:%08X) - %d\n",
3658 scan_notif->scanned_channels,
3659 scan_notif->tsf_low,
3660 scan_notif->tsf_high, scan_notif->status);
3661
3662 /* The HW is no longer scanning */
3663 clear_bit(STATUS_SCAN_HW, &priv->status);
3664
3665 /* The scan completion notification came in, so kill that timer... */
3666 cancel_delayed_work(&priv->scan_check);
3667
3668 IWL_DEBUG_INFO("Scan pass on %sGHz took %dms\n",
3669 (priv->scan_bands == 2) ? "2.4" : "5.2",
3670 jiffies_to_msecs(elapsed_jiffies
3671 (priv->scan_pass_start, jiffies)));
3672
3673 /* Remove this scanned band from the list
3674 * of pending bands to scan */
3675 priv->scan_bands--;
3676
3677 /* If a request to abort was given, or the scan did not succeed
3678 * then we reset the scan state machine and terminate,
3679 * re-queuing another scan if one has been requested */
3680 if (test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
3681 IWL_DEBUG_INFO("Aborted scan completed.\n");
3682 clear_bit(STATUS_SCAN_ABORTING, &priv->status);
3683 } else {
3684 /* If there are more bands on this scan pass reschedule */
3685 if (priv->scan_bands > 0)
3686 goto reschedule;
3687 }
3688
3689 priv->last_scan_jiffies = jiffies;
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08003690 priv->next_scan_jiffies = 0;
Zhu Yib481de92007-09-25 17:54:57 -07003691 IWL_DEBUG_INFO("Setting scan to off\n");
3692
3693 clear_bit(STATUS_SCANNING, &priv->status);
3694
3695 IWL_DEBUG_INFO("Scan took %dms\n",
3696 jiffies_to_msecs(elapsed_jiffies(priv->scan_start, jiffies)));
3697
3698 queue_work(priv->workqueue, &priv->scan_completed);
3699
3700 return;
3701
3702reschedule:
3703 priv->scan_pass_start = jiffies;
3704 queue_work(priv->workqueue, &priv->request_scan);
3705}
3706
3707/* Handle notification from uCode that card's power state is changing
3708 * due to software, hardware, or critical temperature RFKILL */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003709static void iwl4965_rx_card_state_notif(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003710 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003711{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003712 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07003713 u32 flags = le32_to_cpu(pkt->u.card_state_notif.flags);
3714 unsigned long status = priv->status;
3715
3716 IWL_DEBUG_RF_KILL("Card state received: HW:%s SW:%s\n",
3717 (flags & HW_CARD_DISABLED) ? "Kill" : "On",
3718 (flags & SW_CARD_DISABLED) ? "Kill" : "On");
3719
3720 if (flags & (SW_CARD_DISABLED | HW_CARD_DISABLED |
3721 RF_CARD_DISABLED)) {
3722
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003723 iwl4965_write32(priv, CSR_UCODE_DRV_GP1_SET,
Zhu Yib481de92007-09-25 17:54:57 -07003724 CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
3725
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003726 if (!iwl4965_grab_nic_access(priv)) {
3727 iwl4965_write_direct32(
Zhu Yib481de92007-09-25 17:54:57 -07003728 priv, HBUS_TARG_MBX_C,
3729 HBUS_TARG_MBX_C_REG_BIT_CMD_BLOCKED);
3730
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003731 iwl4965_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003732 }
3733
3734 if (!(flags & RXON_CARD_DISABLED)) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003735 iwl4965_write32(priv, CSR_UCODE_DRV_GP1_CLR,
Zhu Yib481de92007-09-25 17:54:57 -07003736 CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003737 if (!iwl4965_grab_nic_access(priv)) {
3738 iwl4965_write_direct32(
Zhu Yib481de92007-09-25 17:54:57 -07003739 priv, HBUS_TARG_MBX_C,
3740 HBUS_TARG_MBX_C_REG_BIT_CMD_BLOCKED);
3741
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003742 iwl4965_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003743 }
3744 }
3745
3746 if (flags & RF_CARD_DISABLED) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003747 iwl4965_write32(priv, CSR_UCODE_DRV_GP1_SET,
Zhu Yib481de92007-09-25 17:54:57 -07003748 CSR_UCODE_DRV_GP1_REG_BIT_CT_KILL_EXIT);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003749 iwl4965_read32(priv, CSR_UCODE_DRV_GP1);
3750 if (!iwl4965_grab_nic_access(priv))
3751 iwl4965_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003752 }
3753 }
3754
3755 if (flags & HW_CARD_DISABLED)
3756 set_bit(STATUS_RF_KILL_HW, &priv->status);
3757 else
3758 clear_bit(STATUS_RF_KILL_HW, &priv->status);
3759
3760
3761 if (flags & SW_CARD_DISABLED)
3762 set_bit(STATUS_RF_KILL_SW, &priv->status);
3763 else
3764 clear_bit(STATUS_RF_KILL_SW, &priv->status);
3765
3766 if (!(flags & RXON_CARD_DISABLED))
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003767 iwl4965_scan_cancel(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003768
3769 if ((test_bit(STATUS_RF_KILL_HW, &status) !=
3770 test_bit(STATUS_RF_KILL_HW, &priv->status)) ||
3771 (test_bit(STATUS_RF_KILL_SW, &status) !=
3772 test_bit(STATUS_RF_KILL_SW, &priv->status)))
3773 queue_work(priv->workqueue, &priv->rf_kill);
3774 else
3775 wake_up_interruptible(&priv->wait_command_queue);
3776}
3777
3778/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003779 * iwl4965_setup_rx_handlers - Initialize Rx handler callbacks
Zhu Yib481de92007-09-25 17:54:57 -07003780 *
3781 * Setup the RX handlers for each of the reply types sent from the uCode
3782 * to the host.
3783 *
3784 * This function chains into the hardware specific files for them to setup
3785 * any hardware specific handlers as well.
3786 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003787static void iwl4965_setup_rx_handlers(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003788{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003789 priv->rx_handlers[REPLY_ALIVE] = iwl4965_rx_reply_alive;
3790 priv->rx_handlers[REPLY_ADD_STA] = iwl4965_rx_reply_add_sta;
3791 priv->rx_handlers[REPLY_ERROR] = iwl4965_rx_reply_error;
3792 priv->rx_handlers[CHANNEL_SWITCH_NOTIFICATION] = iwl4965_rx_csa;
Zhu Yib481de92007-09-25 17:54:57 -07003793 priv->rx_handlers[SPECTRUM_MEASURE_NOTIFICATION] =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003794 iwl4965_rx_spectrum_measure_notif;
3795 priv->rx_handlers[PM_SLEEP_NOTIFICATION] = iwl4965_rx_pm_sleep_notif;
Zhu Yib481de92007-09-25 17:54:57 -07003796 priv->rx_handlers[PM_DEBUG_STATISTIC_NOTIFIC] =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003797 iwl4965_rx_pm_debug_statistics_notif;
3798 priv->rx_handlers[BEACON_NOTIFICATION] = iwl4965_rx_beacon_notif;
Zhu Yib481de92007-09-25 17:54:57 -07003799
Ben Cahill9fbab512007-11-29 11:09:47 +08003800 /*
3801 * The same handler is used for both the REPLY to a discrete
3802 * statistics request from the host as well as for the periodic
3803 * statistics notifications (after received beacons) from the uCode.
Zhu Yib481de92007-09-25 17:54:57 -07003804 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003805 priv->rx_handlers[REPLY_STATISTICS_CMD] = iwl4965_hw_rx_statistics;
3806 priv->rx_handlers[STATISTICS_NOTIFICATION] = iwl4965_hw_rx_statistics;
Zhu Yib481de92007-09-25 17:54:57 -07003807
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003808 priv->rx_handlers[REPLY_SCAN_CMD] = iwl4965_rx_reply_scan;
3809 priv->rx_handlers[SCAN_START_NOTIFICATION] = iwl4965_rx_scan_start_notif;
Zhu Yib481de92007-09-25 17:54:57 -07003810 priv->rx_handlers[SCAN_RESULTS_NOTIFICATION] =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003811 iwl4965_rx_scan_results_notif;
Zhu Yib481de92007-09-25 17:54:57 -07003812 priv->rx_handlers[SCAN_COMPLETE_NOTIFICATION] =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003813 iwl4965_rx_scan_complete_notif;
3814 priv->rx_handlers[CARD_STATE_NOTIFICATION] = iwl4965_rx_card_state_notif;
3815 priv->rx_handlers[REPLY_TX] = iwl4965_rx_reply_tx;
Zhu Yib481de92007-09-25 17:54:57 -07003816
Ben Cahill9fbab512007-11-29 11:09:47 +08003817 /* Set up hardware specific Rx handlers */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003818 iwl4965_hw_rx_handler_setup(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003819}
3820
3821/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003822 * iwl4965_tx_cmd_complete - Pull unused buffers off the queue and reclaim them
Zhu Yib481de92007-09-25 17:54:57 -07003823 * @rxb: Rx buffer to reclaim
3824 *
3825 * If an Rx buffer has an async callback associated with it the callback
3826 * will be executed. The attached skb (if present) will only be freed
3827 * if the callback returns 1
3828 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003829static void iwl4965_tx_cmd_complete(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003830 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003831{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003832 struct iwl4965_rx_packet *pkt = (struct iwl4965_rx_packet *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07003833 u16 sequence = le16_to_cpu(pkt->hdr.sequence);
3834 int txq_id = SEQ_TO_QUEUE(sequence);
3835 int index = SEQ_TO_INDEX(sequence);
3836 int huge = sequence & SEQ_HUGE_FRAME;
3837 int cmd_index;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003838 struct iwl4965_cmd *cmd;
Zhu Yib481de92007-09-25 17:54:57 -07003839
3840 /* If a Tx command is being handled and it isn't in the actual
3841 * command queue then there a command routing bug has been introduced
3842 * in the queue management code. */
3843 if (txq_id != IWL_CMD_QUEUE_NUM)
3844 IWL_ERROR("Error wrong command queue %d command id 0x%X\n",
3845 txq_id, pkt->hdr.cmd);
3846 BUG_ON(txq_id != IWL_CMD_QUEUE_NUM);
3847
3848 cmd_index = get_cmd_index(&priv->txq[IWL_CMD_QUEUE_NUM].q, index, huge);
3849 cmd = &priv->txq[IWL_CMD_QUEUE_NUM].cmd[cmd_index];
3850
3851 /* Input error checking is done when commands are added to queue. */
3852 if (cmd->meta.flags & CMD_WANT_SKB) {
3853 cmd->meta.source->u.skb = rxb->skb;
3854 rxb->skb = NULL;
3855 } else if (cmd->meta.u.callback &&
3856 !cmd->meta.u.callback(priv, cmd, rxb->skb))
3857 rxb->skb = NULL;
3858
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003859 iwl4965_tx_queue_reclaim(priv, txq_id, index);
Zhu Yib481de92007-09-25 17:54:57 -07003860
3861 if (!(cmd->meta.flags & CMD_ASYNC)) {
3862 clear_bit(STATUS_HCMD_ACTIVE, &priv->status);
3863 wake_up_interruptible(&priv->wait_command_queue);
3864 }
3865}
3866
3867/************************** RX-FUNCTIONS ****************************/
3868/*
3869 * Rx theory of operation
3870 *
Ben Cahill9fbab512007-11-29 11:09:47 +08003871 * Driver allocates a circular buffer of Receive Buffer Descriptors (RBDs),
3872 * each of which point to Receive Buffers to be filled by 4965. These get
3873 * used not only for Rx frames, but for any command response or notification
3874 * from the 4965. The driver and 4965 manage the Rx buffers by means
3875 * of indexes into the circular buffer.
Zhu Yib481de92007-09-25 17:54:57 -07003876 *
3877 * Rx Queue Indexes
3878 * The host/firmware share two index registers for managing the Rx buffers.
3879 *
3880 * The READ index maps to the first position that the firmware may be writing
3881 * to -- the driver can read up to (but not including) this position and get
3882 * good data.
3883 * The READ index is managed by the firmware once the card is enabled.
3884 *
3885 * The WRITE index maps to the last position the driver has read from -- the
3886 * position preceding WRITE is the last slot the firmware can place a packet.
3887 *
3888 * The queue is empty (no good data) if WRITE = READ - 1, and is full if
3889 * WRITE = READ.
3890 *
Ben Cahill9fbab512007-11-29 11:09:47 +08003891 * During initialization, the host sets up the READ queue position to the first
Zhu Yib481de92007-09-25 17:54:57 -07003892 * INDEX position, and WRITE to the last (READ - 1 wrapped)
3893 *
Ben Cahill9fbab512007-11-29 11:09:47 +08003894 * When the firmware places a packet in a buffer, it will advance the READ index
Zhu Yib481de92007-09-25 17:54:57 -07003895 * and fire the RX interrupt. The driver can then query the READ index and
3896 * process as many packets as possible, moving the WRITE index forward as it
3897 * resets the Rx queue buffers with new memory.
3898 *
3899 * The management in the driver is as follows:
3900 * + A list of pre-allocated SKBs is stored in iwl->rxq->rx_free. When
3901 * iwl->rxq->free_count drops to or below RX_LOW_WATERMARK, work is scheduled
Ian Schram01ebd062007-10-25 17:15:22 +08003902 * to replenish the iwl->rxq->rx_free.
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003903 * + In iwl4965_rx_replenish (scheduled) if 'processed' != 'read' then the
Zhu Yib481de92007-09-25 17:54:57 -07003904 * iwl->rxq is replenished and the READ INDEX is updated (updating the
3905 * 'processed' and 'read' driver indexes as well)
3906 * + A received packet is processed and handed to the kernel network stack,
3907 * detached from the iwl->rxq. The driver 'processed' index is updated.
3908 * + The Host/Firmware iwl->rxq is replenished at tasklet time from the rx_free
3909 * list. If there are no allocated buffers in iwl->rxq->rx_free, the READ
3910 * INDEX is not incremented and iwl->status(RX_STALLED) is set. If there
3911 * were enough free buffers and RX_STALLED is set it is cleared.
3912 *
3913 *
3914 * Driver sequence:
3915 *
Ben Cahill9fbab512007-11-29 11:09:47 +08003916 * iwl4965_rx_queue_alloc() Allocates rx_free
3917 * iwl4965_rx_replenish() Replenishes rx_free list from rx_used, and calls
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003918 * iwl4965_rx_queue_restock
Ben Cahill9fbab512007-11-29 11:09:47 +08003919 * iwl4965_rx_queue_restock() Moves available buffers from rx_free into Rx
Zhu Yib481de92007-09-25 17:54:57 -07003920 * queue, updates firmware pointers, and updates
3921 * the WRITE index. If insufficient rx_free buffers
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003922 * are available, schedules iwl4965_rx_replenish
Zhu Yib481de92007-09-25 17:54:57 -07003923 *
3924 * -- enable interrupts --
Ben Cahill9fbab512007-11-29 11:09:47 +08003925 * ISR - iwl4965_rx() Detach iwl4965_rx_mem_buffers from pool up to the
Zhu Yib481de92007-09-25 17:54:57 -07003926 * READ INDEX, detaching the SKB from the pool.
3927 * Moves the packet buffer from queue to rx_used.
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003928 * Calls iwl4965_rx_queue_restock to refill any empty
Zhu Yib481de92007-09-25 17:54:57 -07003929 * slots.
3930 * ...
3931 *
3932 */
3933
3934/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003935 * iwl4965_rx_queue_space - Return number of free slots available in queue.
Zhu Yib481de92007-09-25 17:54:57 -07003936 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003937static int iwl4965_rx_queue_space(const struct iwl4965_rx_queue *q)
Zhu Yib481de92007-09-25 17:54:57 -07003938{
3939 int s = q->read - q->write;
3940 if (s <= 0)
3941 s += RX_QUEUE_SIZE;
3942 /* keep some buffer to not confuse full and empty queue */
3943 s -= 2;
3944 if (s < 0)
3945 s = 0;
3946 return s;
3947}
3948
3949/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003950 * iwl4965_rx_queue_update_write_ptr - Update the write pointer for the RX queue
Zhu Yib481de92007-09-25 17:54:57 -07003951 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003952int iwl4965_rx_queue_update_write_ptr(struct iwl_priv *priv, struct iwl4965_rx_queue *q)
Zhu Yib481de92007-09-25 17:54:57 -07003953{
3954 u32 reg = 0;
3955 int rc = 0;
3956 unsigned long flags;
3957
3958 spin_lock_irqsave(&q->lock, flags);
3959
3960 if (q->need_update == 0)
3961 goto exit_unlock;
3962
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003963 /* If power-saving is in use, make sure device is awake */
Zhu Yib481de92007-09-25 17:54:57 -07003964 if (test_bit(STATUS_POWER_PMI, &priv->status)) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003965 reg = iwl4965_read32(priv, CSR_UCODE_DRV_GP1);
Zhu Yib481de92007-09-25 17:54:57 -07003966
3967 if (reg & CSR_UCODE_DRV_GP1_BIT_MAC_SLEEP) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003968 iwl4965_set_bit(priv, CSR_GP_CNTRL,
Zhu Yib481de92007-09-25 17:54:57 -07003969 CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
3970 goto exit_unlock;
3971 }
3972
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003973 rc = iwl4965_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003974 if (rc)
3975 goto exit_unlock;
3976
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003977 /* Device expects a multiple of 8 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003978 iwl4965_write_direct32(priv, FH_RSCSR_CHNL0_WPTR,
Zhu Yib481de92007-09-25 17:54:57 -07003979 q->write & ~0x7);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003980 iwl4965_release_nic_access(priv);
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003981
3982 /* Else device is assumed to be awake */
Zhu Yib481de92007-09-25 17:54:57 -07003983 } else
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003984 /* Device expects a multiple of 8 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003985 iwl4965_write32(priv, FH_RSCSR_CHNL0_WPTR, q->write & ~0x7);
Zhu Yib481de92007-09-25 17:54:57 -07003986
3987
3988 q->need_update = 0;
3989
3990 exit_unlock:
3991 spin_unlock_irqrestore(&q->lock, flags);
3992 return rc;
3993}
3994
3995/**
Ben Cahill9fbab512007-11-29 11:09:47 +08003996 * iwl4965_dma_addr2rbd_ptr - convert a DMA address to a uCode read buffer ptr
Zhu Yib481de92007-09-25 17:54:57 -07003997 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003998static inline __le32 iwl4965_dma_addr2rbd_ptr(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07003999 dma_addr_t dma_addr)
4000{
4001 return cpu_to_le32((u32)(dma_addr >> 8));
4002}
4003
4004
4005/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004006 * iwl4965_rx_queue_restock - refill RX queue from pre-allocated pool
Zhu Yib481de92007-09-25 17:54:57 -07004007 *
Ben Cahill9fbab512007-11-29 11:09:47 +08004008 * If there are slots in the RX queue that need to be restocked,
Zhu Yib481de92007-09-25 17:54:57 -07004009 * and we have free pre-allocated buffers, fill the ranks as much
Ben Cahill9fbab512007-11-29 11:09:47 +08004010 * as we can, pulling from rx_free.
Zhu Yib481de92007-09-25 17:54:57 -07004011 *
4012 * This moves the 'write' index forward to catch up with 'processed', and
4013 * also updates the memory address in the firmware to reference the new
4014 * target buffer.
4015 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004016static int iwl4965_rx_queue_restock(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004017{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004018 struct iwl4965_rx_queue *rxq = &priv->rxq;
Zhu Yib481de92007-09-25 17:54:57 -07004019 struct list_head *element;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004020 struct iwl4965_rx_mem_buffer *rxb;
Zhu Yib481de92007-09-25 17:54:57 -07004021 unsigned long flags;
4022 int write, rc;
4023
4024 spin_lock_irqsave(&rxq->lock, flags);
4025 write = rxq->write & ~0x7;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004026 while ((iwl4965_rx_queue_space(rxq) > 0) && (rxq->free_count)) {
Cahill, Ben M6440adb2007-11-29 11:09:55 +08004027 /* Get next free Rx buffer, remove from free list */
Zhu Yib481de92007-09-25 17:54:57 -07004028 element = rxq->rx_free.next;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004029 rxb = list_entry(element, struct iwl4965_rx_mem_buffer, list);
Zhu Yib481de92007-09-25 17:54:57 -07004030 list_del(element);
Cahill, Ben M6440adb2007-11-29 11:09:55 +08004031
4032 /* Point to Rx buffer via next RBD in circular buffer */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004033 rxq->bd[rxq->write] = iwl4965_dma_addr2rbd_ptr(priv, rxb->dma_addr);
Zhu Yib481de92007-09-25 17:54:57 -07004034 rxq->queue[rxq->write] = rxb;
4035 rxq->write = (rxq->write + 1) & RX_QUEUE_MASK;
4036 rxq->free_count--;
4037 }
4038 spin_unlock_irqrestore(&rxq->lock, flags);
4039 /* If the pre-allocated buffer pool is dropping low, schedule to
4040 * refill it */
4041 if (rxq->free_count <= RX_LOW_WATERMARK)
4042 queue_work(priv->workqueue, &priv->rx_replenish);
4043
4044
Cahill, Ben M6440adb2007-11-29 11:09:55 +08004045 /* If we've added more space for the firmware to place data, tell it.
4046 * Increment device's write pointer in multiples of 8. */
Zhu Yib481de92007-09-25 17:54:57 -07004047 if ((write != (rxq->write & ~0x7))
4048 || (abs(rxq->write - rxq->read) > 7)) {
4049 spin_lock_irqsave(&rxq->lock, flags);
4050 rxq->need_update = 1;
4051 spin_unlock_irqrestore(&rxq->lock, flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004052 rc = iwl4965_rx_queue_update_write_ptr(priv, rxq);
Zhu Yib481de92007-09-25 17:54:57 -07004053 if (rc)
4054 return rc;
4055 }
4056
4057 return 0;
4058}
4059
4060/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004061 * iwl4965_rx_replenish - Move all used packet from rx_used to rx_free
Zhu Yib481de92007-09-25 17:54:57 -07004062 *
4063 * When moving to rx_free an SKB is allocated for the slot.
4064 *
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004065 * Also restock the Rx queue via iwl4965_rx_queue_restock.
Ian Schram01ebd062007-10-25 17:15:22 +08004066 * This is called as a scheduled work item (except for during initialization)
Zhu Yib481de92007-09-25 17:54:57 -07004067 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004068static void iwl4965_rx_allocate(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004069{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004070 struct iwl4965_rx_queue *rxq = &priv->rxq;
Zhu Yib481de92007-09-25 17:54:57 -07004071 struct list_head *element;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004072 struct iwl4965_rx_mem_buffer *rxb;
Zhu Yib481de92007-09-25 17:54:57 -07004073 unsigned long flags;
4074 spin_lock_irqsave(&rxq->lock, flags);
4075 while (!list_empty(&rxq->rx_used)) {
4076 element = rxq->rx_used.next;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004077 rxb = list_entry(element, struct iwl4965_rx_mem_buffer, list);
Cahill, Ben M6440adb2007-11-29 11:09:55 +08004078
4079 /* Alloc a new receive buffer */
Zhu Yib481de92007-09-25 17:54:57 -07004080 rxb->skb =
Ron Rindjunsky9ee1ba42007-11-26 16:14:42 +02004081 alloc_skb(priv->hw_setting.rx_buf_size,
4082 __GFP_NOWARN | GFP_ATOMIC);
Zhu Yib481de92007-09-25 17:54:57 -07004083 if (!rxb->skb) {
4084 if (net_ratelimit())
4085 printk(KERN_CRIT DRV_NAME
4086 ": Can not allocate SKB buffers\n");
4087 /* We don't reschedule replenish work here -- we will
4088 * call the restock method and if it still needs
4089 * more buffers it will schedule replenish */
4090 break;
4091 }
4092 priv->alloc_rxb_skb++;
4093 list_del(element);
Cahill, Ben M6440adb2007-11-29 11:09:55 +08004094
4095 /* Get physical address of RB/SKB */
Zhu Yib481de92007-09-25 17:54:57 -07004096 rxb->dma_addr =
4097 pci_map_single(priv->pci_dev, rxb->skb->data,
Ron Rindjunsky9ee1ba42007-11-26 16:14:42 +02004098 priv->hw_setting.rx_buf_size, PCI_DMA_FROMDEVICE);
Zhu Yib481de92007-09-25 17:54:57 -07004099 list_add_tail(&rxb->list, &rxq->rx_free);
4100 rxq->free_count++;
4101 }
4102 spin_unlock_irqrestore(&rxq->lock, flags);
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08004103}
4104
4105/*
4106 * this should be called while priv->lock is locked
4107*/
Tomas Winkler4fd1f842007-12-05 20:59:58 +02004108static void __iwl4965_rx_replenish(void *data)
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08004109{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004110 struct iwl_priv *priv = data;
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08004111
4112 iwl4965_rx_allocate(priv);
4113 iwl4965_rx_queue_restock(priv);
4114}
4115
4116
4117void iwl4965_rx_replenish(void *data)
4118{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004119 struct iwl_priv *priv = data;
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08004120 unsigned long flags;
4121
4122 iwl4965_rx_allocate(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004123
4124 spin_lock_irqsave(&priv->lock, flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004125 iwl4965_rx_queue_restock(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004126 spin_unlock_irqrestore(&priv->lock, flags);
4127}
4128
4129/* Assumes that the skb field of the buffers in 'pool' is kept accurate.
Ben Cahill9fbab512007-11-29 11:09:47 +08004130 * If an SKB has been detached, the POOL needs to have its SKB set to NULL
Zhu Yib481de92007-09-25 17:54:57 -07004131 * This free routine walks the list of POOL entries and if SKB is set to
4132 * non NULL it is unmapped and freed
4133 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004134static void iwl4965_rx_queue_free(struct iwl_priv *priv, struct iwl4965_rx_queue *rxq)
Zhu Yib481de92007-09-25 17:54:57 -07004135{
4136 int i;
4137 for (i = 0; i < RX_QUEUE_SIZE + RX_FREE_BUFFERS; i++) {
4138 if (rxq->pool[i].skb != NULL) {
4139 pci_unmap_single(priv->pci_dev,
4140 rxq->pool[i].dma_addr,
Ron Rindjunsky9ee1ba42007-11-26 16:14:42 +02004141 priv->hw_setting.rx_buf_size,
4142 PCI_DMA_FROMDEVICE);
Zhu Yib481de92007-09-25 17:54:57 -07004143 dev_kfree_skb(rxq->pool[i].skb);
4144 }
4145 }
4146
4147 pci_free_consistent(priv->pci_dev, 4 * RX_QUEUE_SIZE, rxq->bd,
4148 rxq->dma_addr);
4149 rxq->bd = NULL;
4150}
4151
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004152int iwl4965_rx_queue_alloc(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004153{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004154 struct iwl4965_rx_queue *rxq = &priv->rxq;
Zhu Yib481de92007-09-25 17:54:57 -07004155 struct pci_dev *dev = priv->pci_dev;
4156 int i;
4157
4158 spin_lock_init(&rxq->lock);
4159 INIT_LIST_HEAD(&rxq->rx_free);
4160 INIT_LIST_HEAD(&rxq->rx_used);
Cahill, Ben M6440adb2007-11-29 11:09:55 +08004161
4162 /* Alloc the circular buffer of Read Buffer Descriptors (RBDs) */
Zhu Yib481de92007-09-25 17:54:57 -07004163 rxq->bd = pci_alloc_consistent(dev, 4 * RX_QUEUE_SIZE, &rxq->dma_addr);
4164 if (!rxq->bd)
4165 return -ENOMEM;
Cahill, Ben M6440adb2007-11-29 11:09:55 +08004166
Zhu Yib481de92007-09-25 17:54:57 -07004167 /* Fill the rx_used queue with _all_ of the Rx buffers */
4168 for (i = 0; i < RX_FREE_BUFFERS + RX_QUEUE_SIZE; i++)
4169 list_add_tail(&rxq->pool[i].list, &rxq->rx_used);
Cahill, Ben M6440adb2007-11-29 11:09:55 +08004170
Zhu Yib481de92007-09-25 17:54:57 -07004171 /* Set us so that we have processed and used all buffers, but have
4172 * not restocked the Rx queue with fresh buffers */
4173 rxq->read = rxq->write = 0;
4174 rxq->free_count = 0;
4175 rxq->need_update = 0;
4176 return 0;
4177}
4178
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004179void iwl4965_rx_queue_reset(struct iwl_priv *priv, struct iwl4965_rx_queue *rxq)
Zhu Yib481de92007-09-25 17:54:57 -07004180{
4181 unsigned long flags;
4182 int i;
4183 spin_lock_irqsave(&rxq->lock, flags);
4184 INIT_LIST_HEAD(&rxq->rx_free);
4185 INIT_LIST_HEAD(&rxq->rx_used);
4186 /* Fill the rx_used queue with _all_ of the Rx buffers */
4187 for (i = 0; i < RX_FREE_BUFFERS + RX_QUEUE_SIZE; i++) {
4188 /* In the reset function, these buffers may have been allocated
4189 * to an SKB, so we need to unmap and free potential storage */
4190 if (rxq->pool[i].skb != NULL) {
4191 pci_unmap_single(priv->pci_dev,
4192 rxq->pool[i].dma_addr,
Ron Rindjunsky9ee1ba42007-11-26 16:14:42 +02004193 priv->hw_setting.rx_buf_size,
4194 PCI_DMA_FROMDEVICE);
Zhu Yib481de92007-09-25 17:54:57 -07004195 priv->alloc_rxb_skb--;
4196 dev_kfree_skb(rxq->pool[i].skb);
4197 rxq->pool[i].skb = NULL;
4198 }
4199 list_add_tail(&rxq->pool[i].list, &rxq->rx_used);
4200 }
4201
4202 /* Set us so that we have processed and used all buffers, but have
4203 * not restocked the Rx queue with fresh buffers */
4204 rxq->read = rxq->write = 0;
4205 rxq->free_count = 0;
4206 spin_unlock_irqrestore(&rxq->lock, flags);
4207}
4208
4209/* Convert linear signal-to-noise ratio into dB */
4210static u8 ratio2dB[100] = {
4211/* 0 1 2 3 4 5 6 7 8 9 */
4212 0, 0, 6, 10, 12, 14, 16, 17, 18, 19, /* 00 - 09 */
4213 20, 21, 22, 22, 23, 23, 24, 25, 26, 26, /* 10 - 19 */
4214 26, 26, 26, 27, 27, 28, 28, 28, 29, 29, /* 20 - 29 */
4215 29, 30, 30, 30, 31, 31, 31, 31, 32, 32, /* 30 - 39 */
4216 32, 32, 32, 33, 33, 33, 33, 33, 34, 34, /* 40 - 49 */
4217 34, 34, 34, 34, 35, 35, 35, 35, 35, 35, /* 50 - 59 */
4218 36, 36, 36, 36, 36, 36, 36, 37, 37, 37, /* 60 - 69 */
4219 37, 37, 37, 37, 37, 38, 38, 38, 38, 38, /* 70 - 79 */
4220 38, 38, 38, 38, 38, 39, 39, 39, 39, 39, /* 80 - 89 */
4221 39, 39, 39, 39, 39, 40, 40, 40, 40, 40 /* 90 - 99 */
4222};
4223
4224/* Calculates a relative dB value from a ratio of linear
4225 * (i.e. not dB) signal levels.
4226 * Conversion assumes that levels are voltages (20*log), not powers (10*log). */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004227int iwl4965_calc_db_from_ratio(int sig_ratio)
Zhu Yib481de92007-09-25 17:54:57 -07004228{
Adrian Bunkc899a572007-10-14 19:51:15 +02004229 /* 1000:1 or higher just report as 60 dB */
4230 if (sig_ratio >= 1000)
Zhu Yib481de92007-09-25 17:54:57 -07004231 return 60;
4232
Adrian Bunkc899a572007-10-14 19:51:15 +02004233 /* 100:1 or higher, divide by 10 and use table,
Zhu Yib481de92007-09-25 17:54:57 -07004234 * add 20 dB to make up for divide by 10 */
Adrian Bunkc899a572007-10-14 19:51:15 +02004235 if (sig_ratio >= 100)
Zhu Yib481de92007-09-25 17:54:57 -07004236 return (20 + (int)ratio2dB[sig_ratio/10]);
4237
4238 /* We shouldn't see this */
4239 if (sig_ratio < 1)
4240 return 0;
4241
4242 /* Use table for ratios 1:1 - 99:1 */
4243 return (int)ratio2dB[sig_ratio];
4244}
4245
4246#define PERFECT_RSSI (-20) /* dBm */
4247#define WORST_RSSI (-95) /* dBm */
4248#define RSSI_RANGE (PERFECT_RSSI - WORST_RSSI)
4249
4250/* Calculate an indication of rx signal quality (a percentage, not dBm!).
4251 * See http://www.ces.clemson.edu/linux/signal_quality.shtml for info
4252 * about formulas used below. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004253int iwl4965_calc_sig_qual(int rssi_dbm, int noise_dbm)
Zhu Yib481de92007-09-25 17:54:57 -07004254{
4255 int sig_qual;
4256 int degradation = PERFECT_RSSI - rssi_dbm;
4257
4258 /* If we get a noise measurement, use signal-to-noise ratio (SNR)
4259 * as indicator; formula is (signal dbm - noise dbm).
4260 * SNR at or above 40 is a great signal (100%).
4261 * Below that, scale to fit SNR of 0 - 40 dB within 0 - 100% indicator.
4262 * Weakest usable signal is usually 10 - 15 dB SNR. */
4263 if (noise_dbm) {
4264 if (rssi_dbm - noise_dbm >= 40)
4265 return 100;
4266 else if (rssi_dbm < noise_dbm)
4267 return 0;
4268 sig_qual = ((rssi_dbm - noise_dbm) * 5) / 2;
4269
4270 /* Else use just the signal level.
4271 * This formula is a least squares fit of data points collected and
4272 * compared with a reference system that had a percentage (%) display
4273 * for signal quality. */
4274 } else
4275 sig_qual = (100 * (RSSI_RANGE * RSSI_RANGE) - degradation *
4276 (15 * RSSI_RANGE + 62 * degradation)) /
4277 (RSSI_RANGE * RSSI_RANGE);
4278
4279 if (sig_qual > 100)
4280 sig_qual = 100;
4281 else if (sig_qual < 1)
4282 sig_qual = 0;
4283
4284 return sig_qual;
4285}
4286
4287/**
Ben Cahill9fbab512007-11-29 11:09:47 +08004288 * iwl4965_rx_handle - Main entry function for receiving responses from uCode
Zhu Yib481de92007-09-25 17:54:57 -07004289 *
4290 * Uses the priv->rx_handlers callback function array to invoke
4291 * the appropriate handlers, including command responses,
4292 * frame-received notifications, and other notifications.
4293 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004294static void iwl4965_rx_handle(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004295{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004296 struct iwl4965_rx_mem_buffer *rxb;
4297 struct iwl4965_rx_packet *pkt;
4298 struct iwl4965_rx_queue *rxq = &priv->rxq;
Zhu Yib481de92007-09-25 17:54:57 -07004299 u32 r, i;
4300 int reclaim;
4301 unsigned long flags;
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08004302 u8 fill_rx = 0;
Mohamed Abbasd68ab682008-02-07 13:16:33 -08004303 u32 count = 8;
Zhu Yib481de92007-09-25 17:54:57 -07004304
Cahill, Ben M6440adb2007-11-29 11:09:55 +08004305 /* uCode's read index (stored in shared DRAM) indicates the last Rx
4306 * buffer that the driver may process (last buffer filled by ucode). */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004307 r = iwl4965_hw_get_rx_read(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004308 i = rxq->read;
4309
4310 /* Rx interrupt, but nothing sent from uCode */
4311 if (i == r)
4312 IWL_DEBUG(IWL_DL_RX | IWL_DL_ISR, "r = %d, i = %d\n", r, i);
4313
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08004314 if (iwl4965_rx_queue_space(rxq) > (RX_QUEUE_SIZE / 2))
4315 fill_rx = 1;
4316
Zhu Yib481de92007-09-25 17:54:57 -07004317 while (i != r) {
4318 rxb = rxq->queue[i];
4319
Ben Cahill9fbab512007-11-29 11:09:47 +08004320 /* If an RXB doesn't have a Rx queue slot associated with it,
Zhu Yib481de92007-09-25 17:54:57 -07004321 * then a bug has been introduced in the queue refilling
4322 * routines -- catch it here */
4323 BUG_ON(rxb == NULL);
4324
4325 rxq->queue[i] = NULL;
4326
4327 pci_dma_sync_single_for_cpu(priv->pci_dev, rxb->dma_addr,
Ron Rindjunsky9ee1ba42007-11-26 16:14:42 +02004328 priv->hw_setting.rx_buf_size,
Zhu Yib481de92007-09-25 17:54:57 -07004329 PCI_DMA_FROMDEVICE);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004330 pkt = (struct iwl4965_rx_packet *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07004331
4332 /* Reclaim a command buffer only if this packet is a response
4333 * to a (driver-originated) command.
4334 * If the packet (e.g. Rx frame) originated from uCode,
4335 * there is no command buffer to reclaim.
4336 * Ucode should set SEQ_RX_FRAME bit if ucode-originated,
4337 * but apparently a few don't get set; catch them here. */
4338 reclaim = !(pkt->hdr.sequence & SEQ_RX_FRAME) &&
4339 (pkt->hdr.cmd != REPLY_RX_PHY_CMD) &&
4340 (pkt->hdr.cmd != REPLY_4965_RX) &&
Zhu Yicfe01702007-09-27 11:27:31 +08004341 (pkt->hdr.cmd != REPLY_COMPRESSED_BA) &&
Zhu Yib481de92007-09-25 17:54:57 -07004342 (pkt->hdr.cmd != STATISTICS_NOTIFICATION) &&
4343 (pkt->hdr.cmd != REPLY_TX);
4344
4345 /* Based on type of command response or notification,
4346 * handle those that need handling via function in
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004347 * rx_handlers table. See iwl4965_setup_rx_handlers() */
Zhu Yib481de92007-09-25 17:54:57 -07004348 if (priv->rx_handlers[pkt->hdr.cmd]) {
4349 IWL_DEBUG(IWL_DL_HOST_COMMAND | IWL_DL_RX | IWL_DL_ISR,
4350 "r = %d, i = %d, %s, 0x%02x\n", r, i,
4351 get_cmd_string(pkt->hdr.cmd), pkt->hdr.cmd);
4352 priv->rx_handlers[pkt->hdr.cmd] (priv, rxb);
4353 } else {
4354 /* No handling needed */
4355 IWL_DEBUG(IWL_DL_HOST_COMMAND | IWL_DL_RX | IWL_DL_ISR,
4356 "r %d i %d No handler needed for %s, 0x%02x\n",
4357 r, i, get_cmd_string(pkt->hdr.cmd),
4358 pkt->hdr.cmd);
4359 }
4360
4361 if (reclaim) {
Ben Cahill9fbab512007-11-29 11:09:47 +08004362 /* Invoke any callbacks, transfer the skb to caller, and
4363 * fire off the (possibly) blocking iwl4965_send_cmd()
Zhu Yib481de92007-09-25 17:54:57 -07004364 * as we reclaim the driver command queue */
4365 if (rxb && rxb->skb)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004366 iwl4965_tx_cmd_complete(priv, rxb);
Zhu Yib481de92007-09-25 17:54:57 -07004367 else
4368 IWL_WARNING("Claim null rxb?\n");
4369 }
4370
4371 /* For now we just don't re-use anything. We can tweak this
4372 * later to try and re-use notification packets and SKBs that
4373 * fail to Rx correctly */
4374 if (rxb->skb != NULL) {
4375 priv->alloc_rxb_skb--;
4376 dev_kfree_skb_any(rxb->skb);
4377 rxb->skb = NULL;
4378 }
4379
4380 pci_unmap_single(priv->pci_dev, rxb->dma_addr,
Ron Rindjunsky9ee1ba42007-11-26 16:14:42 +02004381 priv->hw_setting.rx_buf_size,
4382 PCI_DMA_FROMDEVICE);
Zhu Yib481de92007-09-25 17:54:57 -07004383 spin_lock_irqsave(&rxq->lock, flags);
4384 list_add_tail(&rxb->list, &priv->rxq.rx_used);
4385 spin_unlock_irqrestore(&rxq->lock, flags);
4386 i = (i + 1) & RX_QUEUE_MASK;
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08004387 /* If there are a lot of unused frames,
4388 * restock the Rx queue so ucode wont assert. */
4389 if (fill_rx) {
4390 count++;
4391 if (count >= 8) {
4392 priv->rxq.read = i;
4393 __iwl4965_rx_replenish(priv);
4394 count = 0;
4395 }
4396 }
Zhu Yib481de92007-09-25 17:54:57 -07004397 }
4398
4399 /* Backtrack one entry */
4400 priv->rxq.read = i;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004401 iwl4965_rx_queue_restock(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004402}
4403
Cahill, Ben M6440adb2007-11-29 11:09:55 +08004404/**
4405 * iwl4965_tx_queue_update_write_ptr - Send new write index to hardware
4406 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004407static int iwl4965_tx_queue_update_write_ptr(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004408 struct iwl4965_tx_queue *txq)
Zhu Yib481de92007-09-25 17:54:57 -07004409{
4410 u32 reg = 0;
4411 int rc = 0;
4412 int txq_id = txq->q.id;
4413
4414 if (txq->need_update == 0)
4415 return rc;
4416
4417 /* if we're trying to save power */
4418 if (test_bit(STATUS_POWER_PMI, &priv->status)) {
4419 /* wake up nic if it's powered down ...
4420 * uCode will wake up, and interrupt us again, so next
4421 * time we'll skip this part. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004422 reg = iwl4965_read32(priv, CSR_UCODE_DRV_GP1);
Zhu Yib481de92007-09-25 17:54:57 -07004423
4424 if (reg & CSR_UCODE_DRV_GP1_BIT_MAC_SLEEP) {
4425 IWL_DEBUG_INFO("Requesting wakeup, GP1 = 0x%x\n", reg);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004426 iwl4965_set_bit(priv, CSR_GP_CNTRL,
Zhu Yib481de92007-09-25 17:54:57 -07004427 CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
4428 return rc;
4429 }
4430
4431 /* restore this queue's parameters in nic hardware. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004432 rc = iwl4965_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004433 if (rc)
4434 return rc;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004435 iwl4965_write_direct32(priv, HBUS_TARG_WRPTR,
Tomas Winklerfc4b6852007-10-25 17:15:24 +08004436 txq->q.write_ptr | (txq_id << 8));
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004437 iwl4965_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004438
4439 /* else not in power-save mode, uCode will never sleep when we're
4440 * trying to tx (during RFKILL, we're not trying to tx). */
4441 } else
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004442 iwl4965_write32(priv, HBUS_TARG_WRPTR,
Tomas Winklerfc4b6852007-10-25 17:15:24 +08004443 txq->q.write_ptr | (txq_id << 8));
Zhu Yib481de92007-09-25 17:54:57 -07004444
4445 txq->need_update = 0;
4446
4447 return rc;
4448}
4449
Tomas Winkler0a6857e2008-03-12 16:58:49 -07004450#ifdef CONFIG_IWLWIFI_DEBUG
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004451static void iwl4965_print_rx_config_cmd(struct iwl4965_rxon_cmd *rxon)
Zhu Yib481de92007-09-25 17:54:57 -07004452{
Joe Perches0795af52007-10-03 17:59:30 -07004453 DECLARE_MAC_BUF(mac);
4454
Zhu Yib481de92007-09-25 17:54:57 -07004455 IWL_DEBUG_RADIO("RX CONFIG:\n");
Tomas Winkler0a6857e2008-03-12 16:58:49 -07004456 iwl_print_hex_dump(IWL_DL_RADIO, (u8 *) rxon, sizeof(*rxon));
Zhu Yib481de92007-09-25 17:54:57 -07004457 IWL_DEBUG_RADIO("u16 channel: 0x%x\n", le16_to_cpu(rxon->channel));
4458 IWL_DEBUG_RADIO("u32 flags: 0x%08X\n", le32_to_cpu(rxon->flags));
4459 IWL_DEBUG_RADIO("u32 filter_flags: 0x%08x\n",
4460 le32_to_cpu(rxon->filter_flags));
4461 IWL_DEBUG_RADIO("u8 dev_type: 0x%x\n", rxon->dev_type);
4462 IWL_DEBUG_RADIO("u8 ofdm_basic_rates: 0x%02x\n",
4463 rxon->ofdm_basic_rates);
4464 IWL_DEBUG_RADIO("u8 cck_basic_rates: 0x%02x\n", rxon->cck_basic_rates);
Joe Perches0795af52007-10-03 17:59:30 -07004465 IWL_DEBUG_RADIO("u8[6] node_addr: %s\n",
4466 print_mac(mac, rxon->node_addr));
4467 IWL_DEBUG_RADIO("u8[6] bssid_addr: %s\n",
4468 print_mac(mac, rxon->bssid_addr));
Zhu Yib481de92007-09-25 17:54:57 -07004469 IWL_DEBUG_RADIO("u16 assoc_id: 0x%x\n", le16_to_cpu(rxon->assoc_id));
4470}
4471#endif
4472
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004473static void iwl4965_enable_interrupts(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004474{
4475 IWL_DEBUG_ISR("Enabling interrupts\n");
4476 set_bit(STATUS_INT_ENABLED, &priv->status);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004477 iwl4965_write32(priv, CSR_INT_MASK, CSR_INI_SET_MASK);
Zhu Yib481de92007-09-25 17:54:57 -07004478}
4479
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004480static inline void iwl4965_disable_interrupts(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004481{
4482 clear_bit(STATUS_INT_ENABLED, &priv->status);
4483
4484 /* disable interrupts from uCode/NIC to host */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004485 iwl4965_write32(priv, CSR_INT_MASK, 0x00000000);
Zhu Yib481de92007-09-25 17:54:57 -07004486
4487 /* acknowledge/clear/reset any interrupts still pending
4488 * from uCode or flow handler (Rx/Tx DMA) */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004489 iwl4965_write32(priv, CSR_INT, 0xffffffff);
4490 iwl4965_write32(priv, CSR_FH_INT_STATUS, 0xffffffff);
Zhu Yib481de92007-09-25 17:54:57 -07004491 IWL_DEBUG_ISR("Disabled interrupts\n");
4492}
4493
4494static const char *desc_lookup(int i)
4495{
4496 switch (i) {
4497 case 1:
4498 return "FAIL";
4499 case 2:
4500 return "BAD_PARAM";
4501 case 3:
4502 return "BAD_CHECKSUM";
4503 case 4:
4504 return "NMI_INTERRUPT";
4505 case 5:
4506 return "SYSASSERT";
4507 case 6:
4508 return "FATAL_ERROR";
4509 }
4510
4511 return "UNKNOWN";
4512}
4513
4514#define ERROR_START_OFFSET (1 * sizeof(u32))
4515#define ERROR_ELEM_SIZE (7 * sizeof(u32))
4516
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004517static void iwl4965_dump_nic_error_log(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004518{
4519 u32 data2, line;
4520 u32 desc, time, count, base, data1;
4521 u32 blink1, blink2, ilink1, ilink2;
4522 int rc;
4523
4524 base = le32_to_cpu(priv->card_alive.error_event_table_ptr);
4525
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004526 if (!iwl4965_hw_valid_rtc_data_addr(base)) {
Zhu Yib481de92007-09-25 17:54:57 -07004527 IWL_ERROR("Not valid error log pointer 0x%08X\n", base);
4528 return;
4529 }
4530
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004531 rc = iwl4965_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004532 if (rc) {
4533 IWL_WARNING("Can not read from adapter at this time.\n");
4534 return;
4535 }
4536
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004537 count = iwl4965_read_targ_mem(priv, base);
Zhu Yib481de92007-09-25 17:54:57 -07004538
4539 if (ERROR_START_OFFSET <= count * ERROR_ELEM_SIZE) {
4540 IWL_ERROR("Start IWL Error Log Dump:\n");
Tomas Winkler2acae162008-03-02 01:25:59 +02004541 IWL_ERROR("Status: 0x%08lX, count: %d\n", priv->status, count);
Zhu Yib481de92007-09-25 17:54:57 -07004542 }
4543
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004544 desc = iwl4965_read_targ_mem(priv, base + 1 * sizeof(u32));
4545 blink1 = iwl4965_read_targ_mem(priv, base + 3 * sizeof(u32));
4546 blink2 = iwl4965_read_targ_mem(priv, base + 4 * sizeof(u32));
4547 ilink1 = iwl4965_read_targ_mem(priv, base + 5 * sizeof(u32));
4548 ilink2 = iwl4965_read_targ_mem(priv, base + 6 * sizeof(u32));
4549 data1 = iwl4965_read_targ_mem(priv, base + 7 * sizeof(u32));
4550 data2 = iwl4965_read_targ_mem(priv, base + 8 * sizeof(u32));
4551 line = iwl4965_read_targ_mem(priv, base + 9 * sizeof(u32));
4552 time = iwl4965_read_targ_mem(priv, base + 11 * sizeof(u32));
Zhu Yib481de92007-09-25 17:54:57 -07004553
4554 IWL_ERROR("Desc Time "
4555 "data1 data2 line\n");
4556 IWL_ERROR("%-13s (#%d) %010u 0x%08X 0x%08X %u\n",
4557 desc_lookup(desc), desc, time, data1, data2, line);
4558 IWL_ERROR("blink1 blink2 ilink1 ilink2\n");
4559 IWL_ERROR("0x%05X 0x%05X 0x%05X 0x%05X\n", blink1, blink2,
4560 ilink1, ilink2);
4561
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004562 iwl4965_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004563}
4564
4565#define EVENT_START_OFFSET (4 * sizeof(u32))
4566
4567/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004568 * iwl4965_print_event_log - Dump error event log to syslog
Zhu Yib481de92007-09-25 17:54:57 -07004569 *
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004570 * NOTE: Must be called with iwl4965_grab_nic_access() already obtained!
Zhu Yib481de92007-09-25 17:54:57 -07004571 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004572static void iwl4965_print_event_log(struct iwl_priv *priv, u32 start_idx,
Zhu Yib481de92007-09-25 17:54:57 -07004573 u32 num_events, u32 mode)
4574{
4575 u32 i;
4576 u32 base; /* SRAM byte address of event log header */
4577 u32 event_size; /* 2 u32s, or 3 u32s if timestamp recorded */
4578 u32 ptr; /* SRAM byte address of log data */
4579 u32 ev, time, data; /* event log data */
4580
4581 if (num_events == 0)
4582 return;
4583
4584 base = le32_to_cpu(priv->card_alive.log_event_table_ptr);
4585
4586 if (mode == 0)
4587 event_size = 2 * sizeof(u32);
4588 else
4589 event_size = 3 * sizeof(u32);
4590
4591 ptr = base + EVENT_START_OFFSET + (start_idx * event_size);
4592
4593 /* "time" is actually "data" for mode 0 (no timestamp).
4594 * place event id # at far right for easier visual parsing. */
4595 for (i = 0; i < num_events; i++) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004596 ev = iwl4965_read_targ_mem(priv, ptr);
Zhu Yib481de92007-09-25 17:54:57 -07004597 ptr += sizeof(u32);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004598 time = iwl4965_read_targ_mem(priv, ptr);
Zhu Yib481de92007-09-25 17:54:57 -07004599 ptr += sizeof(u32);
4600 if (mode == 0)
4601 IWL_ERROR("0x%08x\t%04u\n", time, ev); /* data, ev */
4602 else {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004603 data = iwl4965_read_targ_mem(priv, ptr);
Zhu Yib481de92007-09-25 17:54:57 -07004604 ptr += sizeof(u32);
4605 IWL_ERROR("%010u\t0x%08x\t%04u\n", time, data, ev);
4606 }
4607 }
4608}
4609
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004610static void iwl4965_dump_nic_event_log(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004611{
4612 int rc;
4613 u32 base; /* SRAM byte address of event log header */
4614 u32 capacity; /* event log capacity in # entries */
4615 u32 mode; /* 0 - no timestamp, 1 - timestamp recorded */
4616 u32 num_wraps; /* # times uCode wrapped to top of log */
4617 u32 next_entry; /* index of next entry to be written by uCode */
4618 u32 size; /* # entries that we'll print */
4619
4620 base = le32_to_cpu(priv->card_alive.log_event_table_ptr);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004621 if (!iwl4965_hw_valid_rtc_data_addr(base)) {
Zhu Yib481de92007-09-25 17:54:57 -07004622 IWL_ERROR("Invalid event log pointer 0x%08X\n", base);
4623 return;
4624 }
4625
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004626 rc = iwl4965_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004627 if (rc) {
4628 IWL_WARNING("Can not read from adapter at this time.\n");
4629 return;
4630 }
4631
4632 /* event log header */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004633 capacity = iwl4965_read_targ_mem(priv, base);
4634 mode = iwl4965_read_targ_mem(priv, base + (1 * sizeof(u32)));
4635 num_wraps = iwl4965_read_targ_mem(priv, base + (2 * sizeof(u32)));
4636 next_entry = iwl4965_read_targ_mem(priv, base + (3 * sizeof(u32)));
Zhu Yib481de92007-09-25 17:54:57 -07004637
4638 size = num_wraps ? capacity : next_entry;
4639
4640 /* bail out if nothing in log */
4641 if (size == 0) {
Zhu Yi583fab32007-09-27 11:27:30 +08004642 IWL_ERROR("Start IWL Event Log Dump: nothing in log\n");
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004643 iwl4965_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004644 return;
4645 }
4646
Zhu Yi583fab32007-09-27 11:27:30 +08004647 IWL_ERROR("Start IWL Event Log Dump: display count %d, wraps %d\n",
Zhu Yib481de92007-09-25 17:54:57 -07004648 size, num_wraps);
4649
4650 /* if uCode has wrapped back to top of log, start at the oldest entry,
4651 * i.e the next one that uCode would fill. */
4652 if (num_wraps)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004653 iwl4965_print_event_log(priv, next_entry,
Zhu Yib481de92007-09-25 17:54:57 -07004654 capacity - next_entry, mode);
4655
4656 /* (then/else) start at top of log */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004657 iwl4965_print_event_log(priv, 0, next_entry, mode);
Zhu Yib481de92007-09-25 17:54:57 -07004658
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004659 iwl4965_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004660}
4661
4662/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004663 * iwl4965_irq_handle_error - called for HW or SW error interrupt from card
Zhu Yib481de92007-09-25 17:54:57 -07004664 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004665static void iwl4965_irq_handle_error(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004666{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004667 /* Set the FW error flag -- cleared on iwl4965_down */
Zhu Yib481de92007-09-25 17:54:57 -07004668 set_bit(STATUS_FW_ERROR, &priv->status);
4669
4670 /* Cancel currently queued command. */
4671 clear_bit(STATUS_HCMD_ACTIVE, &priv->status);
4672
Tomas Winkler0a6857e2008-03-12 16:58:49 -07004673#ifdef CONFIG_IWLWIFI_DEBUG
4674 if (iwl_debug_level & IWL_DL_FW_ERRORS) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004675 iwl4965_dump_nic_error_log(priv);
4676 iwl4965_dump_nic_event_log(priv);
4677 iwl4965_print_rx_config_cmd(&priv->staging_rxon);
Zhu Yib481de92007-09-25 17:54:57 -07004678 }
4679#endif
4680
4681 wake_up_interruptible(&priv->wait_command_queue);
4682
4683 /* Keep the restart process from trying to send host
4684 * commands by clearing the INIT status bit */
4685 clear_bit(STATUS_READY, &priv->status);
4686
4687 if (!test_bit(STATUS_EXIT_PENDING, &priv->status)) {
4688 IWL_DEBUG(IWL_DL_INFO | IWL_DL_FW_ERRORS,
4689 "Restarting adapter due to uCode error.\n");
4690
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004691 if (iwl4965_is_associated(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07004692 memcpy(&priv->recovery_rxon, &priv->active_rxon,
4693 sizeof(priv->recovery_rxon));
4694 priv->error_recovering = 1;
4695 }
4696 queue_work(priv->workqueue, &priv->restart);
4697 }
4698}
4699
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004700static void iwl4965_error_recovery(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004701{
4702 unsigned long flags;
4703
4704 memcpy(&priv->staging_rxon, &priv->recovery_rxon,
4705 sizeof(priv->staging_rxon));
4706 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004707 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004708
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004709 iwl4965_rxon_add_station(priv, priv->bssid, 1);
Zhu Yib481de92007-09-25 17:54:57 -07004710
4711 spin_lock_irqsave(&priv->lock, flags);
4712 priv->assoc_id = le16_to_cpu(priv->staging_rxon.assoc_id);
4713 priv->error_recovering = 0;
4714 spin_unlock_irqrestore(&priv->lock, flags);
4715}
4716
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004717static void iwl4965_irq_tasklet(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004718{
4719 u32 inta, handled = 0;
4720 u32 inta_fh;
4721 unsigned long flags;
Tomas Winkler0a6857e2008-03-12 16:58:49 -07004722#ifdef CONFIG_IWLWIFI_DEBUG
Zhu Yib481de92007-09-25 17:54:57 -07004723 u32 inta_mask;
4724#endif
4725
4726 spin_lock_irqsave(&priv->lock, flags);
4727
4728 /* Ack/clear/reset pending uCode interrupts.
4729 * Note: Some bits in CSR_INT are "OR" of bits in CSR_FH_INT_STATUS,
4730 * and will clear only when CSR_FH_INT_STATUS gets cleared. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004731 inta = iwl4965_read32(priv, CSR_INT);
4732 iwl4965_write32(priv, CSR_INT, inta);
Zhu Yib481de92007-09-25 17:54:57 -07004733
4734 /* Ack/clear/reset pending flow-handler (DMA) interrupts.
4735 * Any new interrupts that happen after this, either while we're
4736 * in this tasklet, or later, will show up in next ISR/tasklet. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004737 inta_fh = iwl4965_read32(priv, CSR_FH_INT_STATUS);
4738 iwl4965_write32(priv, CSR_FH_INT_STATUS, inta_fh);
Zhu Yib481de92007-09-25 17:54:57 -07004739
Tomas Winkler0a6857e2008-03-12 16:58:49 -07004740#ifdef CONFIG_IWLWIFI_DEBUG
4741 if (iwl_debug_level & IWL_DL_ISR) {
Ben Cahill9fbab512007-11-29 11:09:47 +08004742 /* just for debug */
4743 inta_mask = iwl4965_read32(priv, CSR_INT_MASK);
Zhu Yib481de92007-09-25 17:54:57 -07004744 IWL_DEBUG_ISR("inta 0x%08x, enabled 0x%08x, fh 0x%08x\n",
4745 inta, inta_mask, inta_fh);
4746 }
4747#endif
4748
4749 /* Since CSR_INT and CSR_FH_INT_STATUS reads and clears are not
4750 * atomic, make sure that inta covers all the interrupts that
4751 * we've discovered, even if FH interrupt came in just after
4752 * reading CSR_INT. */
Tomas Winkler6f83eaa2008-03-04 18:09:28 -08004753 if (inta_fh & CSR49_FH_INT_RX_MASK)
Zhu Yib481de92007-09-25 17:54:57 -07004754 inta |= CSR_INT_BIT_FH_RX;
Tomas Winkler6f83eaa2008-03-04 18:09:28 -08004755 if (inta_fh & CSR49_FH_INT_TX_MASK)
Zhu Yib481de92007-09-25 17:54:57 -07004756 inta |= CSR_INT_BIT_FH_TX;
4757
4758 /* Now service all interrupt bits discovered above. */
4759 if (inta & CSR_INT_BIT_HW_ERR) {
4760 IWL_ERROR("Microcode HW error detected. Restarting.\n");
4761
4762 /* Tell the device to stop sending interrupts */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004763 iwl4965_disable_interrupts(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004764
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004765 iwl4965_irq_handle_error(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004766
4767 handled |= CSR_INT_BIT_HW_ERR;
4768
4769 spin_unlock_irqrestore(&priv->lock, flags);
4770
4771 return;
4772 }
4773
Tomas Winkler0a6857e2008-03-12 16:58:49 -07004774#ifdef CONFIG_IWLWIFI_DEBUG
4775 if (iwl_debug_level & (IWL_DL_ISR)) {
Zhu Yib481de92007-09-25 17:54:57 -07004776 /* NIC fires this, but we don't use it, redundant with WAKEUP */
Joonwoo Park25c03d82008-01-23 10:15:20 -08004777 if (inta & CSR_INT_BIT_SCD)
4778 IWL_DEBUG_ISR("Scheduler finished to transmit "
4779 "the frame/frames.\n");
Zhu Yib481de92007-09-25 17:54:57 -07004780
4781 /* Alive notification via Rx interrupt will do the real work */
4782 if (inta & CSR_INT_BIT_ALIVE)
4783 IWL_DEBUG_ISR("Alive interrupt\n");
4784 }
4785#endif
4786 /* Safely ignore these bits for debug checks below */
Joonwoo Park25c03d82008-01-23 10:15:20 -08004787 inta &= ~(CSR_INT_BIT_SCD | CSR_INT_BIT_ALIVE);
Zhu Yib481de92007-09-25 17:54:57 -07004788
Ben Cahill9fbab512007-11-29 11:09:47 +08004789 /* HW RF KILL switch toggled */
Zhu Yib481de92007-09-25 17:54:57 -07004790 if (inta & CSR_INT_BIT_RF_KILL) {
4791 int hw_rf_kill = 0;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004792 if (!(iwl4965_read32(priv, CSR_GP_CNTRL) &
Zhu Yib481de92007-09-25 17:54:57 -07004793 CSR_GP_CNTRL_REG_FLAG_HW_RF_KILL_SW))
4794 hw_rf_kill = 1;
4795
4796 IWL_DEBUG(IWL_DL_INFO | IWL_DL_RF_KILL | IWL_DL_ISR,
4797 "RF_KILL bit toggled to %s.\n",
4798 hw_rf_kill ? "disable radio":"enable radio");
4799
4800 /* Queue restart only if RF_KILL switch was set to "kill"
4801 * when we loaded driver, and is now set to "enable".
4802 * After we're Alive, RF_KILL gets handled by
Reinette Chatre32304552008-02-15 14:34:37 -08004803 * iwl4965_rx_card_state_notif() */
Zhu Yi53e49092007-12-06 16:08:44 +08004804 if (!hw_rf_kill && !test_bit(STATUS_ALIVE, &priv->status)) {
4805 clear_bit(STATUS_RF_KILL_HW, &priv->status);
Zhu Yib481de92007-09-25 17:54:57 -07004806 queue_work(priv->workqueue, &priv->restart);
Zhu Yi53e49092007-12-06 16:08:44 +08004807 }
Zhu Yib481de92007-09-25 17:54:57 -07004808
4809 handled |= CSR_INT_BIT_RF_KILL;
4810 }
4811
Ben Cahill9fbab512007-11-29 11:09:47 +08004812 /* Chip got too hot and stopped itself */
Zhu Yib481de92007-09-25 17:54:57 -07004813 if (inta & CSR_INT_BIT_CT_KILL) {
4814 IWL_ERROR("Microcode CT kill error detected.\n");
4815 handled |= CSR_INT_BIT_CT_KILL;
4816 }
4817
4818 /* Error detected by uCode */
4819 if (inta & CSR_INT_BIT_SW_ERR) {
4820 IWL_ERROR("Microcode SW error detected. Restarting 0x%X.\n",
4821 inta);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004822 iwl4965_irq_handle_error(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004823 handled |= CSR_INT_BIT_SW_ERR;
4824 }
4825
4826 /* uCode wakes up after power-down sleep */
4827 if (inta & CSR_INT_BIT_WAKEUP) {
4828 IWL_DEBUG_ISR("Wakeup interrupt\n");
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004829 iwl4965_rx_queue_update_write_ptr(priv, &priv->rxq);
4830 iwl4965_tx_queue_update_write_ptr(priv, &priv->txq[0]);
4831 iwl4965_tx_queue_update_write_ptr(priv, &priv->txq[1]);
4832 iwl4965_tx_queue_update_write_ptr(priv, &priv->txq[2]);
4833 iwl4965_tx_queue_update_write_ptr(priv, &priv->txq[3]);
4834 iwl4965_tx_queue_update_write_ptr(priv, &priv->txq[4]);
4835 iwl4965_tx_queue_update_write_ptr(priv, &priv->txq[5]);
Zhu Yib481de92007-09-25 17:54:57 -07004836
4837 handled |= CSR_INT_BIT_WAKEUP;
4838 }
4839
4840 /* All uCode command responses, including Tx command responses,
4841 * Rx "responses" (frame-received notification), and other
4842 * notifications from uCode come through here*/
4843 if (inta & (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX)) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004844 iwl4965_rx_handle(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004845 handled |= (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX);
4846 }
4847
4848 if (inta & CSR_INT_BIT_FH_TX) {
4849 IWL_DEBUG_ISR("Tx interrupt\n");
4850 handled |= CSR_INT_BIT_FH_TX;
4851 }
4852
4853 if (inta & ~handled)
4854 IWL_ERROR("Unhandled INTA bits 0x%08x\n", inta & ~handled);
4855
4856 if (inta & ~CSR_INI_SET_MASK) {
4857 IWL_WARNING("Disabled INTA bits 0x%08x were pending\n",
4858 inta & ~CSR_INI_SET_MASK);
4859 IWL_WARNING(" with FH_INT = 0x%08x\n", inta_fh);
4860 }
4861
4862 /* Re-enable all interrupts */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004863 iwl4965_enable_interrupts(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004864
Tomas Winkler0a6857e2008-03-12 16:58:49 -07004865#ifdef CONFIG_IWLWIFI_DEBUG
4866 if (iwl_debug_level & (IWL_DL_ISR)) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004867 inta = iwl4965_read32(priv, CSR_INT);
4868 inta_mask = iwl4965_read32(priv, CSR_INT_MASK);
4869 inta_fh = iwl4965_read32(priv, CSR_FH_INT_STATUS);
Zhu Yib481de92007-09-25 17:54:57 -07004870 IWL_DEBUG_ISR("End inta 0x%08x, enabled 0x%08x, fh 0x%08x, "
4871 "flags 0x%08lx\n", inta, inta_mask, inta_fh, flags);
4872 }
4873#endif
4874 spin_unlock_irqrestore(&priv->lock, flags);
4875}
4876
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004877static irqreturn_t iwl4965_isr(int irq, void *data)
Zhu Yib481de92007-09-25 17:54:57 -07004878{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004879 struct iwl_priv *priv = data;
Zhu Yib481de92007-09-25 17:54:57 -07004880 u32 inta, inta_mask;
4881 u32 inta_fh;
4882 if (!priv)
4883 return IRQ_NONE;
4884
4885 spin_lock(&priv->lock);
4886
4887 /* Disable (but don't clear!) interrupts here to avoid
4888 * back-to-back ISRs and sporadic interrupts from our NIC.
4889 * If we have something to service, the tasklet will re-enable ints.
4890 * If we *don't* have something, we'll re-enable before leaving here. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004891 inta_mask = iwl4965_read32(priv, CSR_INT_MASK); /* just for debug */
4892 iwl4965_write32(priv, CSR_INT_MASK, 0x00000000);
Zhu Yib481de92007-09-25 17:54:57 -07004893
4894 /* Discover which interrupts are active/pending */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004895 inta = iwl4965_read32(priv, CSR_INT);
4896 inta_fh = iwl4965_read32(priv, CSR_FH_INT_STATUS);
Zhu Yib481de92007-09-25 17:54:57 -07004897
4898 /* Ignore interrupt if there's nothing in NIC to service.
4899 * This may be due to IRQ shared with another device,
4900 * or due to sporadic interrupts thrown from our NIC. */
4901 if (!inta && !inta_fh) {
4902 IWL_DEBUG_ISR("Ignore interrupt, inta == 0, inta_fh == 0\n");
4903 goto none;
4904 }
4905
4906 if ((inta == 0xFFFFFFFF) || ((inta & 0xFFFFFFF0) == 0xa5a5a5a0)) {
Oliver Neukum66fbb542007-11-15 09:31:10 +08004907 /* Hardware disappeared. It might have already raised
4908 * an interrupt */
Zhu Yib481de92007-09-25 17:54:57 -07004909 IWL_WARNING("HARDWARE GONE?? INTA == 0x%080x\n", inta);
Oliver Neukum66fbb542007-11-15 09:31:10 +08004910 goto unplugged;
Zhu Yib481de92007-09-25 17:54:57 -07004911 }
4912
4913 IWL_DEBUG_ISR("ISR inta 0x%08x, enabled 0x%08x, fh 0x%08x\n",
4914 inta, inta_mask, inta_fh);
4915
Joonwoo Park25c03d82008-01-23 10:15:20 -08004916 inta &= ~CSR_INT_BIT_SCD;
4917
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004918 /* iwl4965_irq_tasklet() will service interrupts and re-enable them */
Joonwoo Park25c03d82008-01-23 10:15:20 -08004919 if (likely(inta || inta_fh))
4920 tasklet_schedule(&priv->irq_tasklet);
Zhu Yib481de92007-09-25 17:54:57 -07004921
Oliver Neukum66fbb542007-11-15 09:31:10 +08004922 unplugged:
4923 spin_unlock(&priv->lock);
Zhu Yib481de92007-09-25 17:54:57 -07004924 return IRQ_HANDLED;
4925
4926 none:
4927 /* re-enable interrupts here since we don't have anything to service. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004928 iwl4965_enable_interrupts(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004929 spin_unlock(&priv->lock);
4930 return IRQ_NONE;
4931}
4932
Zhu Yib481de92007-09-25 17:54:57 -07004933/* For active scan, listen ACTIVE_DWELL_TIME (msec) on each channel after
4934 * sending probe req. This should be set long enough to hear probe responses
4935 * from more than one AP. */
4936#define IWL_ACTIVE_DWELL_TIME_24 (20) /* all times in msec */
4937#define IWL_ACTIVE_DWELL_TIME_52 (10)
4938
4939/* For faster active scanning, scan will move to the next channel if fewer than
4940 * PLCP_QUIET_THRESH packets are heard on this channel within
4941 * ACTIVE_QUIET_TIME after sending probe request. This shortens the dwell
4942 * time if it's a quiet channel (nothing responded to our probe, and there's
4943 * no other traffic).
4944 * Disable "quiet" feature by setting PLCP_QUIET_THRESH to 0. */
4945#define IWL_PLCP_QUIET_THRESH __constant_cpu_to_le16(1) /* packets */
4946#define IWL_ACTIVE_QUIET_TIME __constant_cpu_to_le16(5) /* msec */
4947
4948/* For passive scan, listen PASSIVE_DWELL_TIME (msec) on each channel.
4949 * Must be set longer than active dwell time.
4950 * For the most reliable scan, set > AP beacon interval (typically 100msec). */
4951#define IWL_PASSIVE_DWELL_TIME_24 (20) /* all times in msec */
4952#define IWL_PASSIVE_DWELL_TIME_52 (10)
4953#define IWL_PASSIVE_DWELL_BASE (100)
4954#define IWL_CHANNEL_TUNE_TIME 5
4955
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004956static inline u16 iwl4965_get_active_dwell_time(struct iwl_priv *priv,
Johannes Berg8318d782008-01-24 19:38:38 +01004957 enum ieee80211_band band)
Zhu Yib481de92007-09-25 17:54:57 -07004958{
Johannes Berg8318d782008-01-24 19:38:38 +01004959 if (band == IEEE80211_BAND_5GHZ)
Zhu Yib481de92007-09-25 17:54:57 -07004960 return IWL_ACTIVE_DWELL_TIME_52;
4961 else
4962 return IWL_ACTIVE_DWELL_TIME_24;
4963}
4964
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004965static u16 iwl4965_get_passive_dwell_time(struct iwl_priv *priv,
Johannes Berg8318d782008-01-24 19:38:38 +01004966 enum ieee80211_band band)
Zhu Yib481de92007-09-25 17:54:57 -07004967{
Johannes Berg8318d782008-01-24 19:38:38 +01004968 u16 active = iwl4965_get_active_dwell_time(priv, band);
4969 u16 passive = (band != IEEE80211_BAND_5GHZ) ?
Zhu Yib481de92007-09-25 17:54:57 -07004970 IWL_PASSIVE_DWELL_BASE + IWL_PASSIVE_DWELL_TIME_24 :
4971 IWL_PASSIVE_DWELL_BASE + IWL_PASSIVE_DWELL_TIME_52;
4972
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004973 if (iwl4965_is_associated(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07004974 /* If we're associated, we clamp the maximum passive
4975 * dwell time to be 98% of the beacon interval (minus
4976 * 2 * channel tune time) */
4977 passive = priv->beacon_int;
4978 if ((passive > IWL_PASSIVE_DWELL_BASE) || !passive)
4979 passive = IWL_PASSIVE_DWELL_BASE;
4980 passive = (passive * 98) / 100 - IWL_CHANNEL_TUNE_TIME * 2;
4981 }
4982
4983 if (passive <= active)
4984 passive = active + 1;
4985
4986 return passive;
4987}
4988
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004989static int iwl4965_get_channels_for_scan(struct iwl_priv *priv,
Johannes Berg8318d782008-01-24 19:38:38 +01004990 enum ieee80211_band band,
Zhu Yib481de92007-09-25 17:54:57 -07004991 u8 is_active, u8 direct_mask,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004992 struct iwl4965_scan_channel *scan_ch)
Zhu Yib481de92007-09-25 17:54:57 -07004993{
4994 const struct ieee80211_channel *channels = NULL;
Johannes Berg8318d782008-01-24 19:38:38 +01004995 const struct ieee80211_supported_band *sband;
Assaf Kraussbf85ea42008-03-14 10:38:49 -07004996 const struct iwl_channel_info *ch_info;
Zhu Yib481de92007-09-25 17:54:57 -07004997 u16 passive_dwell = 0;
4998 u16 active_dwell = 0;
4999 int added, i;
5000
Johannes Berg8318d782008-01-24 19:38:38 +01005001 sband = iwl4965_get_hw_mode(priv, band);
5002 if (!sband)
Zhu Yib481de92007-09-25 17:54:57 -07005003 return 0;
5004
Johannes Berg8318d782008-01-24 19:38:38 +01005005 channels = sband->channels;
Zhu Yib481de92007-09-25 17:54:57 -07005006
Johannes Berg8318d782008-01-24 19:38:38 +01005007 active_dwell = iwl4965_get_active_dwell_time(priv, band);
5008 passive_dwell = iwl4965_get_passive_dwell_time(priv, band);
Zhu Yib481de92007-09-25 17:54:57 -07005009
Johannes Berg8318d782008-01-24 19:38:38 +01005010 for (i = 0, added = 0; i < sband->n_channels; i++) {
5011 if (ieee80211_frequency_to_channel(channels[i].center_freq) ==
Zhu Yib481de92007-09-25 17:54:57 -07005012 le16_to_cpu(priv->active_rxon.channel)) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005013 if (iwl4965_is_associated(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07005014 IWL_DEBUG_SCAN
5015 ("Skipping current channel %d\n",
5016 le16_to_cpu(priv->active_rxon.channel));
5017 continue;
5018 }
5019 } else if (priv->only_active_channel)
5020 continue;
5021
Johannes Berg8318d782008-01-24 19:38:38 +01005022 scan_ch->channel = ieee80211_frequency_to_channel(channels[i].center_freq);
Zhu Yib481de92007-09-25 17:54:57 -07005023
Johannes Berg8318d782008-01-24 19:38:38 +01005024 ch_info = iwl4965_get_channel_info(priv, band,
Ben Cahill9fbab512007-11-29 11:09:47 +08005025 scan_ch->channel);
Zhu Yib481de92007-09-25 17:54:57 -07005026 if (!is_channel_valid(ch_info)) {
5027 IWL_DEBUG_SCAN("Channel %d is INVALID for this SKU.\n",
5028 scan_ch->channel);
5029 continue;
5030 }
5031
5032 if (!is_active || is_channel_passive(ch_info) ||
Johannes Berg8318d782008-01-24 19:38:38 +01005033 (channels[i].flags & IEEE80211_CHAN_PASSIVE_SCAN))
Zhu Yib481de92007-09-25 17:54:57 -07005034 scan_ch->type = 0; /* passive */
5035 else
5036 scan_ch->type = 1; /* active */
5037
5038 if (scan_ch->type & 1)
5039 scan_ch->type |= (direct_mask << 1);
5040
5041 if (is_channel_narrow(ch_info))
5042 scan_ch->type |= (1 << 7);
5043
5044 scan_ch->active_dwell = cpu_to_le16(active_dwell);
5045 scan_ch->passive_dwell = cpu_to_le16(passive_dwell);
5046
Ben Cahill9fbab512007-11-29 11:09:47 +08005047 /* Set txpower levels to defaults */
Zhu Yib481de92007-09-25 17:54:57 -07005048 scan_ch->tpc.dsp_atten = 110;
5049 /* scan_pwr_info->tpc.dsp_atten; */
5050
5051 /*scan_pwr_info->tpc.tx_gain; */
Johannes Berg8318d782008-01-24 19:38:38 +01005052 if (band == IEEE80211_BAND_5GHZ)
Zhu Yib481de92007-09-25 17:54:57 -07005053 scan_ch->tpc.tx_gain = ((1 << 5) | (3 << 3)) | 3;
5054 else {
5055 scan_ch->tpc.tx_gain = ((1 << 5) | (5 << 3));
5056 /* NOTE: if we were doing 6Mb OFDM for scans we'd use
Ben Cahill9fbab512007-11-29 11:09:47 +08005057 * power level:
Reinette Chatre8a1b0242008-01-14 17:46:25 -08005058 * scan_ch->tpc.tx_gain = ((1 << 5) | (2 << 3)) | 3;
Zhu Yib481de92007-09-25 17:54:57 -07005059 */
5060 }
5061
5062 IWL_DEBUG_SCAN("Scanning %d [%s %d]\n",
5063 scan_ch->channel,
5064 (scan_ch->type & 1) ? "ACTIVE" : "PASSIVE",
5065 (scan_ch->type & 1) ?
5066 active_dwell : passive_dwell);
5067
5068 scan_ch++;
5069 added++;
5070 }
5071
5072 IWL_DEBUG_SCAN("total channels to scan %d \n", added);
5073 return added;
5074}
5075
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005076static void iwl4965_init_hw_rates(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07005077 struct ieee80211_rate *rates)
5078{
5079 int i;
5080
5081 for (i = 0; i < IWL_RATE_COUNT; i++) {
Johannes Berg8318d782008-01-24 19:38:38 +01005082 rates[i].bitrate = iwl4965_rates[i].ieee * 5;
5083 rates[i].hw_value = i; /* Rate scaling will work on indexes */
5084 rates[i].hw_value_short = i;
5085 rates[i].flags = 0;
5086 if ((i > IWL_LAST_OFDM_RATE) || (i < IWL_FIRST_OFDM_RATE)) {
Zhu Yib481de92007-09-25 17:54:57 -07005087 /*
Johannes Berg8318d782008-01-24 19:38:38 +01005088 * If CCK != 1M then set short preamble rate flag.
Zhu Yib481de92007-09-25 17:54:57 -07005089 */
Tomas Winkler35cdeaf2008-03-11 16:17:20 -07005090 rates[i].flags |=
5091 (iwl4965_rates[i].plcp == IWL_RATE_1M_PLCP) ?
5092 0 : IEEE80211_RATE_SHORT_PREAMBLE;
Zhu Yib481de92007-09-25 17:54:57 -07005093 }
Zhu Yib481de92007-09-25 17:54:57 -07005094 }
Zhu Yib481de92007-09-25 17:54:57 -07005095}
5096
5097/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005098 * iwl4965_init_geos - Initialize mac80211's geo/channel info based from eeprom
Zhu Yib481de92007-09-25 17:54:57 -07005099 */
Assaf Kraussbf85ea42008-03-14 10:38:49 -07005100int iwl4965_init_geos(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005101{
Assaf Kraussbf85ea42008-03-14 10:38:49 -07005102 struct iwl_channel_info *ch;
Tomas Winkler8211ef72008-03-02 01:36:04 +02005103 struct ieee80211_supported_band *sband;
Zhu Yib481de92007-09-25 17:54:57 -07005104 struct ieee80211_channel *channels;
5105 struct ieee80211_channel *geo_ch;
5106 struct ieee80211_rate *rates;
5107 int i = 0;
Zhu Yib481de92007-09-25 17:54:57 -07005108
Johannes Berg8318d782008-01-24 19:38:38 +01005109 if (priv->bands[IEEE80211_BAND_2GHZ].n_bitrates ||
5110 priv->bands[IEEE80211_BAND_5GHZ].n_bitrates) {
Zhu Yib481de92007-09-25 17:54:57 -07005111 IWL_DEBUG_INFO("Geography modes already initialized.\n");
5112 set_bit(STATUS_GEO_CONFIGURED, &priv->status);
5113 return 0;
5114 }
5115
Zhu Yib481de92007-09-25 17:54:57 -07005116 channels = kzalloc(sizeof(struct ieee80211_channel) *
5117 priv->channel_count, GFP_KERNEL);
Johannes Berg8318d782008-01-24 19:38:38 +01005118 if (!channels)
Zhu Yib481de92007-09-25 17:54:57 -07005119 return -ENOMEM;
Zhu Yib481de92007-09-25 17:54:57 -07005120
Tomas Winkler8211ef72008-03-02 01:36:04 +02005121 rates = kzalloc((sizeof(struct ieee80211_rate) * (IWL_RATE_COUNT + 1)),
Zhu Yib481de92007-09-25 17:54:57 -07005122 GFP_KERNEL);
5123 if (!rates) {
Zhu Yib481de92007-09-25 17:54:57 -07005124 kfree(channels);
5125 return -ENOMEM;
5126 }
5127
Zhu Yib481de92007-09-25 17:54:57 -07005128 /* 5.2GHz channels start after the 2.4GHz channels */
Tomas Winkler8211ef72008-03-02 01:36:04 +02005129 sband = &priv->bands[IEEE80211_BAND_5GHZ];
Assaf Kraussbf85ea42008-03-14 10:38:49 -07005130 sband->channels = &channels[ARRAY_SIZE(iwl_eeprom_band_1)];
Tomas Winkler8211ef72008-03-02 01:36:04 +02005131 /* just OFDM */
5132 sband->bitrates = &rates[IWL_FIRST_OFDM_RATE];
5133 sband->n_bitrates = IWL_RATE_COUNT - IWL_FIRST_OFDM_RATE;
Johannes Berg8318d782008-01-24 19:38:38 +01005134
Tomas Winkler8211ef72008-03-02 01:36:04 +02005135 iwl4965_init_ht_hw_capab(&sband->ht_info, IEEE80211_BAND_5GHZ);
Tomas Winkler78330fd2008-02-06 02:37:18 +02005136
Tomas Winkler8211ef72008-03-02 01:36:04 +02005137 sband = &priv->bands[IEEE80211_BAND_2GHZ];
5138 sband->channels = channels;
5139 /* OFDM & CCK */
5140 sband->bitrates = rates;
5141 sband->n_bitrates = IWL_RATE_COUNT;
Zhu Yib481de92007-09-25 17:54:57 -07005142
Tomas Winkler8211ef72008-03-02 01:36:04 +02005143 iwl4965_init_ht_hw_capab(&sband->ht_info, IEEE80211_BAND_2GHZ);
Tomas Winkler78330fd2008-02-06 02:37:18 +02005144
Zhu Yib481de92007-09-25 17:54:57 -07005145 priv->ieee_channels = channels;
5146 priv->ieee_rates = rates;
5147
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005148 iwl4965_init_hw_rates(priv, rates);
Zhu Yib481de92007-09-25 17:54:57 -07005149
Tomas Winkler8211ef72008-03-02 01:36:04 +02005150 for (i = 0; i < priv->channel_count; i++) {
Zhu Yib481de92007-09-25 17:54:57 -07005151 ch = &priv->channel_info[i];
5152
Tomas Winkler8211ef72008-03-02 01:36:04 +02005153 /* FIXME: might be removed if scan is OK */
5154 if (!is_channel_valid(ch))
Zhu Yib481de92007-09-25 17:54:57 -07005155 continue;
Zhu Yib481de92007-09-25 17:54:57 -07005156
Tomas Winkler8211ef72008-03-02 01:36:04 +02005157 if (is_channel_a_band(ch))
5158 sband = &priv->bands[IEEE80211_BAND_5GHZ];
5159 else
5160 sband = &priv->bands[IEEE80211_BAND_2GHZ];
Zhu Yib481de92007-09-25 17:54:57 -07005161
Tomas Winkler8211ef72008-03-02 01:36:04 +02005162 geo_ch = &sband->channels[sband->n_channels++];
5163
5164 geo_ch->center_freq = ieee80211_channel_to_frequency(ch->channel);
Johannes Berg8318d782008-01-24 19:38:38 +01005165 geo_ch->max_power = ch->max_power_avg;
5166 geo_ch->max_antenna_gain = 0xff;
Mohamed Abbas7b723042008-01-31 21:46:40 -08005167 geo_ch->hw_value = ch->channel;
Zhu Yib481de92007-09-25 17:54:57 -07005168
5169 if (is_channel_valid(ch)) {
Johannes Berg8318d782008-01-24 19:38:38 +01005170 if (!(ch->flags & EEPROM_CHANNEL_IBSS))
5171 geo_ch->flags |= IEEE80211_CHAN_NO_IBSS;
Zhu Yib481de92007-09-25 17:54:57 -07005172
Johannes Berg8318d782008-01-24 19:38:38 +01005173 if (!(ch->flags & EEPROM_CHANNEL_ACTIVE))
5174 geo_ch->flags |= IEEE80211_CHAN_PASSIVE_SCAN;
Zhu Yib481de92007-09-25 17:54:57 -07005175
5176 if (ch->flags & EEPROM_CHANNEL_RADAR)
Johannes Berg8318d782008-01-24 19:38:38 +01005177 geo_ch->flags |= IEEE80211_CHAN_RADAR;
Zhu Yib481de92007-09-25 17:54:57 -07005178
5179 if (ch->max_power_avg > priv->max_channel_txpower_limit)
5180 priv->max_channel_txpower_limit =
5181 ch->max_power_avg;
Tomas Winkler8211ef72008-03-02 01:36:04 +02005182 } else {
Johannes Berg8318d782008-01-24 19:38:38 +01005183 geo_ch->flags |= IEEE80211_CHAN_DISABLED;
Tomas Winkler8211ef72008-03-02 01:36:04 +02005184 }
5185
5186 /* Save flags for reg domain usage */
5187 geo_ch->orig_flags = geo_ch->flags;
5188
5189 IWL_DEBUG_INFO("Channel %d Freq=%d[%sGHz] %s flag=0%X\n",
5190 ch->channel, geo_ch->center_freq,
5191 is_channel_a_band(ch) ? "5.2" : "2.4",
5192 geo_ch->flags & IEEE80211_CHAN_DISABLED ?
5193 "restricted" : "valid",
5194 geo_ch->flags);
Zhu Yib481de92007-09-25 17:54:57 -07005195 }
5196
Tomas Winkler82b9a122008-03-04 18:09:30 -08005197 if ((priv->bands[IEEE80211_BAND_5GHZ].n_channels == 0) &&
5198 priv->cfg->sku & IWL_SKU_A) {
Zhu Yib481de92007-09-25 17:54:57 -07005199 printk(KERN_INFO DRV_NAME
5200 ": Incorrectly detected BG card as ABG. Please send "
5201 "your PCI ID 0x%04X:0x%04X to maintainer.\n",
5202 priv->pci_dev->device, priv->pci_dev->subsystem_device);
Tomas Winkler82b9a122008-03-04 18:09:30 -08005203 priv->cfg->sku &= ~IWL_SKU_A;
Zhu Yib481de92007-09-25 17:54:57 -07005204 }
5205
5206 printk(KERN_INFO DRV_NAME
5207 ": Tunable channels: %d 802.11bg, %d 802.11a channels\n",
Johannes Berg8318d782008-01-24 19:38:38 +01005208 priv->bands[IEEE80211_BAND_2GHZ].n_channels,
5209 priv->bands[IEEE80211_BAND_5GHZ].n_channels);
Zhu Yib481de92007-09-25 17:54:57 -07005210
Johannes Berg8318d782008-01-24 19:38:38 +01005211 priv->hw->wiphy->bands[IEEE80211_BAND_2GHZ] = &priv->bands[IEEE80211_BAND_2GHZ];
5212 priv->hw->wiphy->bands[IEEE80211_BAND_5GHZ] = &priv->bands[IEEE80211_BAND_5GHZ];
Zhu Yib481de92007-09-25 17:54:57 -07005213
Zhu Yib481de92007-09-25 17:54:57 -07005214 set_bit(STATUS_GEO_CONFIGURED, &priv->status);
5215
5216 return 0;
5217}
5218
Reinette Chatre849e0dc2008-01-23 10:15:18 -08005219/*
5220 * iwl4965_free_geos - undo allocations in iwl4965_init_geos
5221 */
Assaf Kraussbf85ea42008-03-14 10:38:49 -07005222void iwl4965_free_geos(struct iwl_priv *priv)
Reinette Chatre849e0dc2008-01-23 10:15:18 -08005223{
Reinette Chatre849e0dc2008-01-23 10:15:18 -08005224 kfree(priv->ieee_channels);
5225 kfree(priv->ieee_rates);
5226 clear_bit(STATUS_GEO_CONFIGURED, &priv->status);
5227}
5228
Zhu Yib481de92007-09-25 17:54:57 -07005229/******************************************************************************
5230 *
5231 * uCode download functions
5232 *
5233 ******************************************************************************/
5234
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005235static void iwl4965_dealloc_ucode_pci(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005236{
Tomas Winkler98c92212008-01-14 17:46:20 -08005237 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_code);
5238 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_data);
5239 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_data_backup);
5240 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_init);
5241 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_init_data);
5242 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_boot);
Zhu Yib481de92007-09-25 17:54:57 -07005243}
5244
5245/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005246 * iwl4965_verify_inst_full - verify runtime uCode image in card vs. host,
Zhu Yib481de92007-09-25 17:54:57 -07005247 * looking at all data.
5248 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005249static int iwl4965_verify_inst_full(struct iwl_priv *priv, __le32 *image,
Ben Cahill9fbab512007-11-29 11:09:47 +08005250 u32 len)
Zhu Yib481de92007-09-25 17:54:57 -07005251{
5252 u32 val;
5253 u32 save_len = len;
5254 int rc = 0;
5255 u32 errcnt;
5256
5257 IWL_DEBUG_INFO("ucode inst image size is %u\n", len);
5258
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005259 rc = iwl4965_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005260 if (rc)
5261 return rc;
5262
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005263 iwl4965_write_direct32(priv, HBUS_TARG_MEM_RADDR, RTC_INST_LOWER_BOUND);
Zhu Yib481de92007-09-25 17:54:57 -07005264
5265 errcnt = 0;
5266 for (; len > 0; len -= sizeof(u32), image++) {
5267 /* read data comes through single port, auto-incr addr */
5268 /* NOTE: Use the debugless read so we don't flood kernel log
5269 * if IWL_DL_IO is set */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005270 val = _iwl4965_read_direct32(priv, HBUS_TARG_MEM_RDAT);
Zhu Yib481de92007-09-25 17:54:57 -07005271 if (val != le32_to_cpu(*image)) {
5272 IWL_ERROR("uCode INST section is invalid at "
5273 "offset 0x%x, is 0x%x, s/b 0x%x\n",
5274 save_len - len, val, le32_to_cpu(*image));
5275 rc = -EIO;
5276 errcnt++;
5277 if (errcnt >= 20)
5278 break;
5279 }
5280 }
5281
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005282 iwl4965_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005283
5284 if (!errcnt)
5285 IWL_DEBUG_INFO
5286 ("ucode image in INSTRUCTION memory is good\n");
5287
5288 return rc;
5289}
5290
5291
5292/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005293 * iwl4965_verify_inst_sparse - verify runtime uCode image in card vs. host,
Zhu Yib481de92007-09-25 17:54:57 -07005294 * using sample data 100 bytes apart. If these sample points are good,
5295 * it's a pretty good bet that everything between them is good, too.
5296 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005297static int iwl4965_verify_inst_sparse(struct iwl_priv *priv, __le32 *image, u32 len)
Zhu Yib481de92007-09-25 17:54:57 -07005298{
5299 u32 val;
5300 int rc = 0;
5301 u32 errcnt = 0;
5302 u32 i;
5303
5304 IWL_DEBUG_INFO("ucode inst image size is %u\n", len);
5305
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005306 rc = iwl4965_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005307 if (rc)
5308 return rc;
5309
5310 for (i = 0; i < len; i += 100, image += 100/sizeof(u32)) {
5311 /* read data comes through single port, auto-incr addr */
5312 /* NOTE: Use the debugless read so we don't flood kernel log
5313 * if IWL_DL_IO is set */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005314 iwl4965_write_direct32(priv, HBUS_TARG_MEM_RADDR,
Zhu Yib481de92007-09-25 17:54:57 -07005315 i + RTC_INST_LOWER_BOUND);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005316 val = _iwl4965_read_direct32(priv, HBUS_TARG_MEM_RDAT);
Zhu Yib481de92007-09-25 17:54:57 -07005317 if (val != le32_to_cpu(*image)) {
5318#if 0 /* Enable this if you want to see details */
5319 IWL_ERROR("uCode INST section is invalid at "
5320 "offset 0x%x, is 0x%x, s/b 0x%x\n",
5321 i, val, *image);
5322#endif
5323 rc = -EIO;
5324 errcnt++;
5325 if (errcnt >= 3)
5326 break;
5327 }
5328 }
5329
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005330 iwl4965_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005331
5332 return rc;
5333}
5334
5335
5336/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005337 * iwl4965_verify_ucode - determine which instruction image is in SRAM,
Zhu Yib481de92007-09-25 17:54:57 -07005338 * and verify its contents
5339 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005340static int iwl4965_verify_ucode(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005341{
5342 __le32 *image;
5343 u32 len;
5344 int rc = 0;
5345
5346 /* Try bootstrap */
5347 image = (__le32 *)priv->ucode_boot.v_addr;
5348 len = priv->ucode_boot.len;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005349 rc = iwl4965_verify_inst_sparse(priv, image, len);
Zhu Yib481de92007-09-25 17:54:57 -07005350 if (rc == 0) {
5351 IWL_DEBUG_INFO("Bootstrap uCode is good in inst SRAM\n");
5352 return 0;
5353 }
5354
5355 /* Try initialize */
5356 image = (__le32 *)priv->ucode_init.v_addr;
5357 len = priv->ucode_init.len;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005358 rc = iwl4965_verify_inst_sparse(priv, image, len);
Zhu Yib481de92007-09-25 17:54:57 -07005359 if (rc == 0) {
5360 IWL_DEBUG_INFO("Initialize uCode is good in inst SRAM\n");
5361 return 0;
5362 }
5363
5364 /* Try runtime/protocol */
5365 image = (__le32 *)priv->ucode_code.v_addr;
5366 len = priv->ucode_code.len;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005367 rc = iwl4965_verify_inst_sparse(priv, image, len);
Zhu Yib481de92007-09-25 17:54:57 -07005368 if (rc == 0) {
5369 IWL_DEBUG_INFO("Runtime uCode is good in inst SRAM\n");
5370 return 0;
5371 }
5372
5373 IWL_ERROR("NO VALID UCODE IMAGE IN INSTRUCTION SRAM!!\n");
5374
Ben Cahill9fbab512007-11-29 11:09:47 +08005375 /* Since nothing seems to match, show first several data entries in
5376 * instruction SRAM, so maybe visual inspection will give a clue.
5377 * Selection of bootstrap image (vs. other images) is arbitrary. */
Zhu Yib481de92007-09-25 17:54:57 -07005378 image = (__le32 *)priv->ucode_boot.v_addr;
5379 len = priv->ucode_boot.len;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005380 rc = iwl4965_verify_inst_full(priv, image, len);
Zhu Yib481de92007-09-25 17:54:57 -07005381
5382 return rc;
5383}
5384
5385
5386/* check contents of special bootstrap uCode SRAM */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005387static int iwl4965_verify_bsm(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005388{
5389 __le32 *image = priv->ucode_boot.v_addr;
5390 u32 len = priv->ucode_boot.len;
5391 u32 reg;
5392 u32 val;
5393
5394 IWL_DEBUG_INFO("Begin verify bsm\n");
5395
5396 /* verify BSM SRAM contents */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005397 val = iwl4965_read_prph(priv, BSM_WR_DWCOUNT_REG);
Zhu Yib481de92007-09-25 17:54:57 -07005398 for (reg = BSM_SRAM_LOWER_BOUND;
5399 reg < BSM_SRAM_LOWER_BOUND + len;
5400 reg += sizeof(u32), image ++) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005401 val = iwl4965_read_prph(priv, reg);
Zhu Yib481de92007-09-25 17:54:57 -07005402 if (val != le32_to_cpu(*image)) {
5403 IWL_ERROR("BSM uCode verification failed at "
5404 "addr 0x%08X+%u (of %u), is 0x%x, s/b 0x%x\n",
5405 BSM_SRAM_LOWER_BOUND,
5406 reg - BSM_SRAM_LOWER_BOUND, len,
5407 val, le32_to_cpu(*image));
5408 return -EIO;
5409 }
5410 }
5411
5412 IWL_DEBUG_INFO("BSM bootstrap uCode image OK\n");
5413
5414 return 0;
5415}
5416
5417/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005418 * iwl4965_load_bsm - Load bootstrap instructions
Zhu Yib481de92007-09-25 17:54:57 -07005419 *
5420 * BSM operation:
5421 *
5422 * The Bootstrap State Machine (BSM) stores a short bootstrap uCode program
5423 * in special SRAM that does not power down during RFKILL. When powering back
5424 * up after power-saving sleeps (or during initial uCode load), the BSM loads
5425 * the bootstrap program into the on-board processor, and starts it.
5426 *
5427 * The bootstrap program loads (via DMA) instructions and data for a new
5428 * program from host DRAM locations indicated by the host driver in the
5429 * BSM_DRAM_* registers. Once the new program is loaded, it starts
5430 * automatically.
5431 *
5432 * When initializing the NIC, the host driver points the BSM to the
5433 * "initialize" uCode image. This uCode sets up some internal data, then
5434 * notifies host via "initialize alive" that it is complete.
5435 *
5436 * The host then replaces the BSM_DRAM_* pointer values to point to the
5437 * normal runtime uCode instructions and a backup uCode data cache buffer
5438 * (filled initially with starting data values for the on-board processor),
5439 * then triggers the "initialize" uCode to load and launch the runtime uCode,
5440 * which begins normal operation.
5441 *
5442 * When doing a power-save shutdown, runtime uCode saves data SRAM into
5443 * the backup data cache in DRAM before SRAM is powered down.
5444 *
5445 * When powering back up, the BSM loads the bootstrap program. This reloads
5446 * the runtime uCode instructions and the backup data cache into SRAM,
5447 * and re-launches the runtime uCode from where it left off.
5448 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005449static int iwl4965_load_bsm(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005450{
5451 __le32 *image = priv->ucode_boot.v_addr;
5452 u32 len = priv->ucode_boot.len;
5453 dma_addr_t pinst;
5454 dma_addr_t pdata;
5455 u32 inst_len;
5456 u32 data_len;
5457 int rc;
5458 int i;
5459 u32 done;
5460 u32 reg_offset;
5461
5462 IWL_DEBUG_INFO("Begin load bsm\n");
5463
5464 /* make sure bootstrap program is no larger than BSM's SRAM size */
5465 if (len > IWL_MAX_BSM_SIZE)
5466 return -EINVAL;
5467
5468 /* Tell bootstrap uCode where to find the "Initialize" uCode
Ben Cahill9fbab512007-11-29 11:09:47 +08005469 * in host DRAM ... host DRAM physical address bits 35:4 for 4965.
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005470 * NOTE: iwl4965_initialize_alive_start() will replace these values,
Zhu Yib481de92007-09-25 17:54:57 -07005471 * after the "initialize" uCode has run, to point to
5472 * runtime/protocol instructions and backup data cache. */
5473 pinst = priv->ucode_init.p_addr >> 4;
5474 pdata = priv->ucode_init_data.p_addr >> 4;
5475 inst_len = priv->ucode_init.len;
5476 data_len = priv->ucode_init_data.len;
5477
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005478 rc = iwl4965_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005479 if (rc)
5480 return rc;
5481
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005482 iwl4965_write_prph(priv, BSM_DRAM_INST_PTR_REG, pinst);
5483 iwl4965_write_prph(priv, BSM_DRAM_DATA_PTR_REG, pdata);
5484 iwl4965_write_prph(priv, BSM_DRAM_INST_BYTECOUNT_REG, inst_len);
5485 iwl4965_write_prph(priv, BSM_DRAM_DATA_BYTECOUNT_REG, data_len);
Zhu Yib481de92007-09-25 17:54:57 -07005486
5487 /* Fill BSM memory with bootstrap instructions */
5488 for (reg_offset = BSM_SRAM_LOWER_BOUND;
5489 reg_offset < BSM_SRAM_LOWER_BOUND + len;
5490 reg_offset += sizeof(u32), image++)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005491 _iwl4965_write_prph(priv, reg_offset,
Zhu Yib481de92007-09-25 17:54:57 -07005492 le32_to_cpu(*image));
5493
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005494 rc = iwl4965_verify_bsm(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005495 if (rc) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005496 iwl4965_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005497 return rc;
5498 }
5499
5500 /* Tell BSM to copy from BSM SRAM into instruction SRAM, when asked */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005501 iwl4965_write_prph(priv, BSM_WR_MEM_SRC_REG, 0x0);
5502 iwl4965_write_prph(priv, BSM_WR_MEM_DST_REG,
Zhu Yib481de92007-09-25 17:54:57 -07005503 RTC_INST_LOWER_BOUND);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005504 iwl4965_write_prph(priv, BSM_WR_DWCOUNT_REG, len / sizeof(u32));
Zhu Yib481de92007-09-25 17:54:57 -07005505
5506 /* Load bootstrap code into instruction SRAM now,
5507 * to prepare to load "initialize" uCode */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005508 iwl4965_write_prph(priv, BSM_WR_CTRL_REG,
Zhu Yib481de92007-09-25 17:54:57 -07005509 BSM_WR_CTRL_REG_BIT_START);
5510
5511 /* Wait for load of bootstrap uCode to finish */
5512 for (i = 0; i < 100; i++) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005513 done = iwl4965_read_prph(priv, BSM_WR_CTRL_REG);
Zhu Yib481de92007-09-25 17:54:57 -07005514 if (!(done & BSM_WR_CTRL_REG_BIT_START))
5515 break;
5516 udelay(10);
5517 }
5518 if (i < 100)
5519 IWL_DEBUG_INFO("BSM write complete, poll %d iterations\n", i);
5520 else {
5521 IWL_ERROR("BSM write did not complete!\n");
5522 return -EIO;
5523 }
5524
5525 /* Enable future boot loads whenever power management unit triggers it
5526 * (e.g. when powering back up after power-save shutdown) */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005527 iwl4965_write_prph(priv, BSM_WR_CTRL_REG,
Zhu Yib481de92007-09-25 17:54:57 -07005528 BSM_WR_CTRL_REG_BIT_START_EN);
5529
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005530 iwl4965_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005531
5532 return 0;
5533}
5534
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005535static void iwl4965_nic_start(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005536{
5537 /* Remove all resets to allow NIC to operate */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005538 iwl4965_write32(priv, CSR_RESET, 0);
Zhu Yib481de92007-09-25 17:54:57 -07005539}
5540
Tomas Winkler90e759d2007-11-29 11:09:41 +08005541
Zhu Yib481de92007-09-25 17:54:57 -07005542/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005543 * iwl4965_read_ucode - Read uCode images from disk file.
Zhu Yib481de92007-09-25 17:54:57 -07005544 *
5545 * Copy into buffers for card to fetch via bus-mastering
5546 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005547static int iwl4965_read_ucode(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005548{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005549 struct iwl4965_ucode *ucode;
Tomas Winkler90e759d2007-11-29 11:09:41 +08005550 int ret;
Zhu Yib481de92007-09-25 17:54:57 -07005551 const struct firmware *ucode_raw;
Tomas Winkler4bf775c2008-03-04 18:09:31 -08005552 const char *name = priv->cfg->fw_name;
Zhu Yib481de92007-09-25 17:54:57 -07005553 u8 *src;
5554 size_t len;
5555 u32 ver, inst_size, data_size, init_size, init_data_size, boot_size;
5556
5557 /* Ask kernel firmware_class module to get the boot firmware off disk.
5558 * request_firmware() is synchronous, file is in memory on return. */
Tomas Winkler90e759d2007-11-29 11:09:41 +08005559 ret = request_firmware(&ucode_raw, name, &priv->pci_dev->dev);
5560 if (ret < 0) {
5561 IWL_ERROR("%s firmware file req failed: Reason %d\n",
5562 name, ret);
Zhu Yib481de92007-09-25 17:54:57 -07005563 goto error;
5564 }
5565
5566 IWL_DEBUG_INFO("Got firmware '%s' file (%zd bytes) from disk\n",
5567 name, ucode_raw->size);
5568
5569 /* Make sure that we got at least our header! */
5570 if (ucode_raw->size < sizeof(*ucode)) {
5571 IWL_ERROR("File size way too small!\n");
Tomas Winkler90e759d2007-11-29 11:09:41 +08005572 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07005573 goto err_release;
5574 }
5575
5576 /* Data from ucode file: header followed by uCode images */
5577 ucode = (void *)ucode_raw->data;
5578
5579 ver = le32_to_cpu(ucode->ver);
5580 inst_size = le32_to_cpu(ucode->inst_size);
5581 data_size = le32_to_cpu(ucode->data_size);
5582 init_size = le32_to_cpu(ucode->init_size);
5583 init_data_size = le32_to_cpu(ucode->init_data_size);
5584 boot_size = le32_to_cpu(ucode->boot_size);
5585
5586 IWL_DEBUG_INFO("f/w package hdr ucode version = 0x%x\n", ver);
5587 IWL_DEBUG_INFO("f/w package hdr runtime inst size = %u\n",
5588 inst_size);
5589 IWL_DEBUG_INFO("f/w package hdr runtime data size = %u\n",
5590 data_size);
5591 IWL_DEBUG_INFO("f/w package hdr init inst size = %u\n",
5592 init_size);
5593 IWL_DEBUG_INFO("f/w package hdr init data size = %u\n",
5594 init_data_size);
5595 IWL_DEBUG_INFO("f/w package hdr boot inst size = %u\n",
5596 boot_size);
5597
5598 /* Verify size of file vs. image size info in file's header */
5599 if (ucode_raw->size < sizeof(*ucode) +
5600 inst_size + data_size + init_size +
5601 init_data_size + boot_size) {
5602
5603 IWL_DEBUG_INFO("uCode file size %d too small\n",
5604 (int)ucode_raw->size);
Tomas Winkler90e759d2007-11-29 11:09:41 +08005605 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07005606 goto err_release;
5607 }
5608
5609 /* Verify that uCode images will fit in card's SRAM */
5610 if (inst_size > IWL_MAX_INST_SIZE) {
Tomas Winkler90e759d2007-11-29 11:09:41 +08005611 IWL_DEBUG_INFO("uCode instr len %d too large to fit in\n",
5612 inst_size);
5613 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07005614 goto err_release;
5615 }
5616
5617 if (data_size > IWL_MAX_DATA_SIZE) {
Tomas Winkler90e759d2007-11-29 11:09:41 +08005618 IWL_DEBUG_INFO("uCode data len %d too large to fit in\n",
5619 data_size);
5620 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07005621 goto err_release;
5622 }
5623 if (init_size > IWL_MAX_INST_SIZE) {
5624 IWL_DEBUG_INFO
Tomas Winkler90e759d2007-11-29 11:09:41 +08005625 ("uCode init instr len %d too large to fit in\n",
5626 init_size);
5627 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07005628 goto err_release;
5629 }
5630 if (init_data_size > IWL_MAX_DATA_SIZE) {
5631 IWL_DEBUG_INFO
Tomas Winkler90e759d2007-11-29 11:09:41 +08005632 ("uCode init data len %d too large to fit in\n",
5633 init_data_size);
5634 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07005635 goto err_release;
5636 }
5637 if (boot_size > IWL_MAX_BSM_SIZE) {
5638 IWL_DEBUG_INFO
Tomas Winkler90e759d2007-11-29 11:09:41 +08005639 ("uCode boot instr len %d too large to fit in\n",
5640 boot_size);
5641 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07005642 goto err_release;
5643 }
5644
5645 /* Allocate ucode buffers for card's bus-master loading ... */
5646
5647 /* Runtime instructions and 2 copies of data:
5648 * 1) unmodified from disk
5649 * 2) backup cache for save/restore during power-downs */
5650 priv->ucode_code.len = inst_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08005651 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_code);
Zhu Yib481de92007-09-25 17:54:57 -07005652
5653 priv->ucode_data.len = data_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08005654 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_data);
Zhu Yib481de92007-09-25 17:54:57 -07005655
5656 priv->ucode_data_backup.len = data_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08005657 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_data_backup);
Zhu Yib481de92007-09-25 17:54:57 -07005658
5659 /* Initialization instructions and data */
Tomas Winkler90e759d2007-11-29 11:09:41 +08005660 if (init_size && init_data_size) {
5661 priv->ucode_init.len = init_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08005662 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_init);
Zhu Yib481de92007-09-25 17:54:57 -07005663
Tomas Winkler90e759d2007-11-29 11:09:41 +08005664 priv->ucode_init_data.len = init_data_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08005665 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_init_data);
Tomas Winkler90e759d2007-11-29 11:09:41 +08005666
5667 if (!priv->ucode_init.v_addr || !priv->ucode_init_data.v_addr)
5668 goto err_pci_alloc;
5669 }
Zhu Yib481de92007-09-25 17:54:57 -07005670
5671 /* Bootstrap (instructions only, no data) */
Tomas Winkler90e759d2007-11-29 11:09:41 +08005672 if (boot_size) {
5673 priv->ucode_boot.len = boot_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08005674 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_boot);
Zhu Yib481de92007-09-25 17:54:57 -07005675
Tomas Winkler90e759d2007-11-29 11:09:41 +08005676 if (!priv->ucode_boot.v_addr)
5677 goto err_pci_alloc;
5678 }
Zhu Yib481de92007-09-25 17:54:57 -07005679
5680 /* Copy images into buffers for card's bus-master reads ... */
5681
5682 /* Runtime instructions (first block of data in file) */
5683 src = &ucode->data[0];
5684 len = priv->ucode_code.len;
Tomas Winkler90e759d2007-11-29 11:09:41 +08005685 IWL_DEBUG_INFO("Copying (but not loading) uCode instr len %Zd\n", len);
Zhu Yib481de92007-09-25 17:54:57 -07005686 memcpy(priv->ucode_code.v_addr, src, len);
5687 IWL_DEBUG_INFO("uCode instr buf vaddr = 0x%p, paddr = 0x%08x\n",
5688 priv->ucode_code.v_addr, (u32)priv->ucode_code.p_addr);
5689
5690 /* Runtime data (2nd block)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005691 * NOTE: Copy into backup buffer will be done in iwl4965_up() */
Zhu Yib481de92007-09-25 17:54:57 -07005692 src = &ucode->data[inst_size];
5693 len = priv->ucode_data.len;
Tomas Winkler90e759d2007-11-29 11:09:41 +08005694 IWL_DEBUG_INFO("Copying (but not loading) uCode data len %Zd\n", len);
Zhu Yib481de92007-09-25 17:54:57 -07005695 memcpy(priv->ucode_data.v_addr, src, len);
5696 memcpy(priv->ucode_data_backup.v_addr, src, len);
5697
5698 /* Initialization instructions (3rd block) */
5699 if (init_size) {
5700 src = &ucode->data[inst_size + data_size];
5701 len = priv->ucode_init.len;
Tomas Winkler90e759d2007-11-29 11:09:41 +08005702 IWL_DEBUG_INFO("Copying (but not loading) init instr len %Zd\n",
5703 len);
Zhu Yib481de92007-09-25 17:54:57 -07005704 memcpy(priv->ucode_init.v_addr, src, len);
5705 }
5706
5707 /* Initialization data (4th block) */
5708 if (init_data_size) {
5709 src = &ucode->data[inst_size + data_size + init_size];
5710 len = priv->ucode_init_data.len;
Tomas Winkler90e759d2007-11-29 11:09:41 +08005711 IWL_DEBUG_INFO("Copying (but not loading) init data len %Zd\n",
5712 len);
Zhu Yib481de92007-09-25 17:54:57 -07005713 memcpy(priv->ucode_init_data.v_addr, src, len);
5714 }
5715
5716 /* Bootstrap instructions (5th block) */
5717 src = &ucode->data[inst_size + data_size + init_size + init_data_size];
5718 len = priv->ucode_boot.len;
Tomas Winkler90e759d2007-11-29 11:09:41 +08005719 IWL_DEBUG_INFO("Copying (but not loading) boot instr len %Zd\n", len);
Zhu Yib481de92007-09-25 17:54:57 -07005720 memcpy(priv->ucode_boot.v_addr, src, len);
5721
5722 /* We have our copies now, allow OS release its copies */
5723 release_firmware(ucode_raw);
5724 return 0;
5725
5726 err_pci_alloc:
5727 IWL_ERROR("failed to allocate pci memory\n");
Tomas Winkler90e759d2007-11-29 11:09:41 +08005728 ret = -ENOMEM;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005729 iwl4965_dealloc_ucode_pci(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005730
5731 err_release:
5732 release_firmware(ucode_raw);
5733
5734 error:
Tomas Winkler90e759d2007-11-29 11:09:41 +08005735 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07005736}
5737
5738
5739/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005740 * iwl4965_set_ucode_ptrs - Set uCode address location
Zhu Yib481de92007-09-25 17:54:57 -07005741 *
5742 * Tell initialization uCode where to find runtime uCode.
5743 *
5744 * BSM registers initially contain pointers to initialization uCode.
5745 * We need to replace them to load runtime uCode inst and data,
5746 * and to save runtime data when powering down.
5747 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005748static int iwl4965_set_ucode_ptrs(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005749{
5750 dma_addr_t pinst;
5751 dma_addr_t pdata;
5752 int rc = 0;
5753 unsigned long flags;
5754
5755 /* bits 35:4 for 4965 */
5756 pinst = priv->ucode_code.p_addr >> 4;
5757 pdata = priv->ucode_data_backup.p_addr >> 4;
5758
5759 spin_lock_irqsave(&priv->lock, flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005760 rc = iwl4965_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005761 if (rc) {
5762 spin_unlock_irqrestore(&priv->lock, flags);
5763 return rc;
5764 }
5765
5766 /* Tell bootstrap uCode where to find image to load */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005767 iwl4965_write_prph(priv, BSM_DRAM_INST_PTR_REG, pinst);
5768 iwl4965_write_prph(priv, BSM_DRAM_DATA_PTR_REG, pdata);
5769 iwl4965_write_prph(priv, BSM_DRAM_DATA_BYTECOUNT_REG,
Zhu Yib481de92007-09-25 17:54:57 -07005770 priv->ucode_data.len);
5771
5772 /* Inst bytecount must be last to set up, bit 31 signals uCode
5773 * that all new ptr/size info is in place */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005774 iwl4965_write_prph(priv, BSM_DRAM_INST_BYTECOUNT_REG,
Zhu Yib481de92007-09-25 17:54:57 -07005775 priv->ucode_code.len | BSM_DRAM_INST_LOAD);
5776
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005777 iwl4965_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005778
5779 spin_unlock_irqrestore(&priv->lock, flags);
5780
5781 IWL_DEBUG_INFO("Runtime uCode pointers are set.\n");
5782
5783 return rc;
5784}
5785
5786/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005787 * iwl4965_init_alive_start - Called after REPLY_ALIVE notification received
Zhu Yib481de92007-09-25 17:54:57 -07005788 *
5789 * Called after REPLY_ALIVE notification received from "initialize" uCode.
5790 *
5791 * The 4965 "initialize" ALIVE reply contains calibration data for:
5792 * Voltage, temperature, and MIMO tx gain correction, now stored in priv
5793 * (3945 does not contain this data).
5794 *
5795 * Tell "initialize" uCode to go ahead and load the runtime uCode.
5796*/
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005797static void iwl4965_init_alive_start(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005798{
5799 /* Check alive response for "valid" sign from uCode */
5800 if (priv->card_alive_init.is_valid != UCODE_VALID_OK) {
5801 /* We had an error bringing up the hardware, so take it
5802 * all the way back down so we can try again */
5803 IWL_DEBUG_INFO("Initialize Alive failed.\n");
5804 goto restart;
5805 }
5806
5807 /* Bootstrap uCode has loaded initialize uCode ... verify inst image.
5808 * This is a paranoid check, because we would not have gotten the
5809 * "initialize" alive if code weren't properly loaded. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005810 if (iwl4965_verify_ucode(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07005811 /* Runtime instruction load was bad;
5812 * take it all the way back down so we can try again */
5813 IWL_DEBUG_INFO("Bad \"initialize\" uCode load.\n");
5814 goto restart;
5815 }
5816
5817 /* Calculate temperature */
5818 priv->temperature = iwl4965_get_temperature(priv);
5819
5820 /* Send pointers to protocol/runtime uCode image ... init code will
5821 * load and launch runtime uCode, which will send us another "Alive"
5822 * notification. */
5823 IWL_DEBUG_INFO("Initialization Alive received.\n");
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005824 if (iwl4965_set_ucode_ptrs(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07005825 /* Runtime instruction load won't happen;
5826 * take it all the way back down so we can try again */
5827 IWL_DEBUG_INFO("Couldn't set up uCode pointers.\n");
5828 goto restart;
5829 }
5830 return;
5831
5832 restart:
5833 queue_work(priv->workqueue, &priv->restart);
5834}
5835
5836
5837/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005838 * iwl4965_alive_start - called after REPLY_ALIVE notification received
Zhu Yib481de92007-09-25 17:54:57 -07005839 * from protocol/runtime uCode (initialization uCode's
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005840 * Alive gets handled by iwl4965_init_alive_start()).
Zhu Yib481de92007-09-25 17:54:57 -07005841 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005842static void iwl4965_alive_start(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005843{
5844 int rc = 0;
5845
5846 IWL_DEBUG_INFO("Runtime Alive received.\n");
5847
5848 if (priv->card_alive.is_valid != UCODE_VALID_OK) {
5849 /* We had an error bringing up the hardware, so take it
5850 * all the way back down so we can try again */
5851 IWL_DEBUG_INFO("Alive failed.\n");
5852 goto restart;
5853 }
5854
5855 /* Initialize uCode has loaded Runtime uCode ... verify inst image.
5856 * This is a paranoid check, because we would not have gotten the
5857 * "runtime" alive if code weren't properly loaded. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005858 if (iwl4965_verify_ucode(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07005859 /* Runtime instruction load was bad;
5860 * take it all the way back down so we can try again */
5861 IWL_DEBUG_INFO("Bad runtime uCode load.\n");
5862 goto restart;
5863 }
5864
Assaf Kraussbf85ea42008-03-14 10:38:49 -07005865 iwlcore_clear_stations_table(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005866
5867 rc = iwl4965_alive_notify(priv);
5868 if (rc) {
5869 IWL_WARNING("Could not complete ALIVE transition [ntf]: %d\n",
5870 rc);
5871 goto restart;
5872 }
5873
Ben Cahill9fbab512007-11-29 11:09:47 +08005874 /* After the ALIVE response, we can send host commands to 4965 uCode */
Zhu Yib481de92007-09-25 17:54:57 -07005875 set_bit(STATUS_ALIVE, &priv->status);
5876
5877 /* Clear out the uCode error bit if it is set */
5878 clear_bit(STATUS_FW_ERROR, &priv->status);
5879
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005880 if (iwl4965_is_rfkill(priv))
Zhu Yib481de92007-09-25 17:54:57 -07005881 return;
5882
Zhu Yi5a66926a2008-01-14 17:46:18 -08005883 ieee80211_start_queues(priv->hw);
Zhu Yib481de92007-09-25 17:54:57 -07005884
5885 priv->active_rate = priv->rates_mask;
5886 priv->active_rate_basic = priv->rates_mask & IWL_BASIC_RATES_MASK;
5887
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005888 iwl4965_send_power_mode(priv, IWL_POWER_LEVEL(priv->power_mode));
Zhu Yib481de92007-09-25 17:54:57 -07005889
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005890 if (iwl4965_is_associated(priv)) {
5891 struct iwl4965_rxon_cmd *active_rxon =
5892 (struct iwl4965_rxon_cmd *)(&priv->active_rxon);
Zhu Yib481de92007-09-25 17:54:57 -07005893
5894 memcpy(&priv->staging_rxon, &priv->active_rxon,
5895 sizeof(priv->staging_rxon));
5896 active_rxon->filter_flags &= ~RXON_FILTER_ASSOC_MSK;
5897 } else {
5898 /* Initialize our rx_config data */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005899 iwl4965_connection_init_rx_config(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005900 memcpy(priv->staging_rxon.node_addr, priv->mac_addr, ETH_ALEN);
5901 }
5902
Ben Cahill9fbab512007-11-29 11:09:47 +08005903 /* Configure Bluetooth device coexistence support */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005904 iwl4965_send_bt_config(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005905
5906 /* Configure the adapter for unassociated operation */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005907 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005908
5909 /* At this point, the NIC is initialized and operational */
5910 priv->notif_missed_beacons = 0;
5911 set_bit(STATUS_READY, &priv->status);
5912
5913 iwl4965_rf_kill_ct_config(priv);
Zhu Yi5a66926a2008-01-14 17:46:18 -08005914
Zhu Yib481de92007-09-25 17:54:57 -07005915 IWL_DEBUG_INFO("ALIVE processing complete.\n");
Zhu Yi5a66926a2008-01-14 17:46:18 -08005916 wake_up_interruptible(&priv->wait_command_queue);
Zhu Yib481de92007-09-25 17:54:57 -07005917
5918 if (priv->error_recovering)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005919 iwl4965_error_recovery(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005920
5921 return;
5922
5923 restart:
5924 queue_work(priv->workqueue, &priv->restart);
5925}
5926
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005927static void iwl4965_cancel_deferred_work(struct iwl_priv *priv);
Zhu Yib481de92007-09-25 17:54:57 -07005928
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005929static void __iwl4965_down(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005930{
5931 unsigned long flags;
5932 int exit_pending = test_bit(STATUS_EXIT_PENDING, &priv->status);
5933 struct ieee80211_conf *conf = NULL;
5934
5935 IWL_DEBUG_INFO(DRV_NAME " is going down\n");
5936
5937 conf = ieee80211_get_hw_conf(priv->hw);
5938
5939 if (!exit_pending)
5940 set_bit(STATUS_EXIT_PENDING, &priv->status);
5941
Assaf Kraussbf85ea42008-03-14 10:38:49 -07005942 iwlcore_clear_stations_table(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005943
5944 /* Unblock any waiting calls */
5945 wake_up_interruptible_all(&priv->wait_command_queue);
5946
Zhu Yib481de92007-09-25 17:54:57 -07005947 /* Wipe out the EXIT_PENDING status bit if we are not actually
5948 * exiting the module */
5949 if (!exit_pending)
5950 clear_bit(STATUS_EXIT_PENDING, &priv->status);
5951
5952 /* stop and reset the on-board processor */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005953 iwl4965_write32(priv, CSR_RESET, CSR_RESET_REG_FLAG_NEVO_RESET);
Zhu Yib481de92007-09-25 17:54:57 -07005954
5955 /* tell the device to stop sending interrupts */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005956 iwl4965_disable_interrupts(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005957
5958 if (priv->mac80211_registered)
5959 ieee80211_stop_queues(priv->hw);
5960
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005961 /* If we have not previously called iwl4965_init() then
Zhu Yib481de92007-09-25 17:54:57 -07005962 * clear all bits but the RF Kill and SUSPEND bits and return */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005963 if (!iwl4965_is_init(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07005964 priv->status = test_bit(STATUS_RF_KILL_HW, &priv->status) <<
5965 STATUS_RF_KILL_HW |
5966 test_bit(STATUS_RF_KILL_SW, &priv->status) <<
5967 STATUS_RF_KILL_SW |
Reinette Chatre97888642008-02-06 11:20:38 -08005968 test_bit(STATUS_GEO_CONFIGURED, &priv->status) <<
5969 STATUS_GEO_CONFIGURED |
Zhu Yib481de92007-09-25 17:54:57 -07005970 test_bit(STATUS_IN_SUSPEND, &priv->status) <<
5971 STATUS_IN_SUSPEND;
5972 goto exit;
5973 }
5974
5975 /* ...otherwise clear out all the status bits but the RF Kill and
5976 * SUSPEND bits and continue taking the NIC down. */
5977 priv->status &= test_bit(STATUS_RF_KILL_HW, &priv->status) <<
5978 STATUS_RF_KILL_HW |
5979 test_bit(STATUS_RF_KILL_SW, &priv->status) <<
5980 STATUS_RF_KILL_SW |
Reinette Chatre97888642008-02-06 11:20:38 -08005981 test_bit(STATUS_GEO_CONFIGURED, &priv->status) <<
5982 STATUS_GEO_CONFIGURED |
Zhu Yib481de92007-09-25 17:54:57 -07005983 test_bit(STATUS_IN_SUSPEND, &priv->status) <<
5984 STATUS_IN_SUSPEND |
5985 test_bit(STATUS_FW_ERROR, &priv->status) <<
5986 STATUS_FW_ERROR;
5987
5988 spin_lock_irqsave(&priv->lock, flags);
Ben Cahill9fbab512007-11-29 11:09:47 +08005989 iwl4965_clear_bit(priv, CSR_GP_CNTRL,
5990 CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
Zhu Yib481de92007-09-25 17:54:57 -07005991 spin_unlock_irqrestore(&priv->lock, flags);
5992
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005993 iwl4965_hw_txq_ctx_stop(priv);
5994 iwl4965_hw_rxq_stop(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005995
5996 spin_lock_irqsave(&priv->lock, flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005997 if (!iwl4965_grab_nic_access(priv)) {
5998 iwl4965_write_prph(priv, APMG_CLK_DIS_REG,
Zhu Yib481de92007-09-25 17:54:57 -07005999 APMG_CLK_VAL_DMA_CLK_RQT);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006000 iwl4965_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006001 }
6002 spin_unlock_irqrestore(&priv->lock, flags);
6003
6004 udelay(5);
6005
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006006 iwl4965_hw_nic_stop_master(priv);
6007 iwl4965_set_bit(priv, CSR_RESET, CSR_RESET_REG_FLAG_SW_RESET);
6008 iwl4965_hw_nic_reset(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006009
6010 exit:
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006011 memset(&priv->card_alive, 0, sizeof(struct iwl4965_alive_resp));
Zhu Yib481de92007-09-25 17:54:57 -07006012
6013 if (priv->ibss_beacon)
6014 dev_kfree_skb(priv->ibss_beacon);
6015 priv->ibss_beacon = NULL;
6016
6017 /* clear out any free frames */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006018 iwl4965_clear_free_frames(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006019}
6020
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006021static void iwl4965_down(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07006022{
6023 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006024 __iwl4965_down(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006025 mutex_unlock(&priv->mutex);
Zhu Yib24d22b2007-12-19 13:59:52 +08006026
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006027 iwl4965_cancel_deferred_work(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006028}
6029
6030#define MAX_HW_RESTARTS 5
6031
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006032static int __iwl4965_up(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07006033{
6034 int rc, i;
Zhu Yib481de92007-09-25 17:54:57 -07006035
6036 if (test_bit(STATUS_EXIT_PENDING, &priv->status)) {
6037 IWL_WARNING("Exit pending; will not bring the NIC up\n");
6038 return -EIO;
6039 }
6040
6041 if (test_bit(STATUS_RF_KILL_SW, &priv->status)) {
6042 IWL_WARNING("Radio disabled by SW RF kill (module "
6043 "parameter)\n");
Zhu Yie655b9f2008-01-24 02:19:38 -08006044 return -ENODEV;
6045 }
6046
Reinette Chatree903fbd2008-01-30 22:05:15 -08006047 if (!priv->ucode_data_backup.v_addr || !priv->ucode_data.v_addr) {
6048 IWL_ERROR("ucode not available for device bringup\n");
6049 return -EIO;
6050 }
6051
Zhu Yie655b9f2008-01-24 02:19:38 -08006052 /* If platform's RF_KILL switch is NOT set to KILL */
6053 if (iwl4965_read32(priv, CSR_GP_CNTRL) &
6054 CSR_GP_CNTRL_REG_FLAG_HW_RF_KILL_SW)
6055 clear_bit(STATUS_RF_KILL_HW, &priv->status);
6056 else {
6057 set_bit(STATUS_RF_KILL_HW, &priv->status);
6058 if (!test_bit(STATUS_IN_SUSPEND, &priv->status)) {
6059 IWL_WARNING("Radio disabled by HW RF Kill switch\n");
6060 return -ENODEV;
6061 }
Zhu Yib481de92007-09-25 17:54:57 -07006062 }
6063
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006064 iwl4965_write32(priv, CSR_INT, 0xFFFFFFFF);
Zhu Yib481de92007-09-25 17:54:57 -07006065
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006066 rc = iwl4965_hw_nic_init(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006067 if (rc) {
6068 IWL_ERROR("Unable to int nic\n");
6069 return rc;
6070 }
6071
6072 /* make sure rfkill handshake bits are cleared */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006073 iwl4965_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
6074 iwl4965_write32(priv, CSR_UCODE_DRV_GP1_CLR,
Zhu Yib481de92007-09-25 17:54:57 -07006075 CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
6076
6077 /* clear (again), then enable host interrupts */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006078 iwl4965_write32(priv, CSR_INT, 0xFFFFFFFF);
6079 iwl4965_enable_interrupts(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006080
6081 /* really make sure rfkill handshake bits are cleared */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006082 iwl4965_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
6083 iwl4965_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
Zhu Yib481de92007-09-25 17:54:57 -07006084
6085 /* Copy original ucode data image from disk into backup cache.
6086 * This will be used to initialize the on-board processor's
6087 * data SRAM for a clean start when the runtime program first loads. */
6088 memcpy(priv->ucode_data_backup.v_addr, priv->ucode_data.v_addr,
Zhu Yi5a66926a2008-01-14 17:46:18 -08006089 priv->ucode_data.len);
Zhu Yib481de92007-09-25 17:54:57 -07006090
Zhu Yie655b9f2008-01-24 02:19:38 -08006091 /* We return success when we resume from suspend and rf_kill is on. */
6092 if (test_bit(STATUS_RF_KILL_HW, &priv->status))
Zhu Yib481de92007-09-25 17:54:57 -07006093 return 0;
Zhu Yib481de92007-09-25 17:54:57 -07006094
6095 for (i = 0; i < MAX_HW_RESTARTS; i++) {
6096
Assaf Kraussbf85ea42008-03-14 10:38:49 -07006097 iwlcore_clear_stations_table(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006098
6099 /* load bootstrap state machine,
6100 * load bootstrap program into processor's memory,
6101 * prepare to load the "initialize" uCode */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006102 rc = iwl4965_load_bsm(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006103
6104 if (rc) {
6105 IWL_ERROR("Unable to set up bootstrap uCode: %d\n", rc);
6106 continue;
6107 }
6108
6109 /* start card; "initialize" will load runtime ucode */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006110 iwl4965_nic_start(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006111
Zhu Yib481de92007-09-25 17:54:57 -07006112 IWL_DEBUG_INFO(DRV_NAME " is coming up\n");
6113
6114 return 0;
6115 }
6116
6117 set_bit(STATUS_EXIT_PENDING, &priv->status);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006118 __iwl4965_down(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006119
6120 /* tried to restart and config the device for as long as our
6121 * patience could withstand */
6122 IWL_ERROR("Unable to initialize device after %d attempts.\n", i);
6123 return -EIO;
6124}
6125
6126
6127/*****************************************************************************
6128 *
6129 * Workqueue callbacks
6130 *
6131 *****************************************************************************/
6132
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006133static void iwl4965_bg_init_alive_start(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07006134{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006135 struct iwl_priv *priv =
6136 container_of(data, struct iwl_priv, init_alive_start.work);
Zhu Yib481de92007-09-25 17:54:57 -07006137
6138 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
6139 return;
6140
6141 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006142 iwl4965_init_alive_start(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006143 mutex_unlock(&priv->mutex);
6144}
6145
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006146static void iwl4965_bg_alive_start(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07006147{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006148 struct iwl_priv *priv =
6149 container_of(data, struct iwl_priv, alive_start.work);
Zhu Yib481de92007-09-25 17:54:57 -07006150
6151 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
6152 return;
6153
6154 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006155 iwl4965_alive_start(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006156 mutex_unlock(&priv->mutex);
6157}
6158
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006159static void iwl4965_bg_rf_kill(struct work_struct *work)
Zhu Yib481de92007-09-25 17:54:57 -07006160{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006161 struct iwl_priv *priv = container_of(work, struct iwl_priv, rf_kill);
Zhu Yib481de92007-09-25 17:54:57 -07006162
6163 wake_up_interruptible(&priv->wait_command_queue);
6164
6165 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
6166 return;
6167
6168 mutex_lock(&priv->mutex);
6169
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006170 if (!iwl4965_is_rfkill(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07006171 IWL_DEBUG(IWL_DL_INFO | IWL_DL_RF_KILL,
6172 "HW and/or SW RF Kill no longer active, restarting "
6173 "device\n");
6174 if (!test_bit(STATUS_EXIT_PENDING, &priv->status))
6175 queue_work(priv->workqueue, &priv->restart);
6176 } else {
6177
6178 if (!test_bit(STATUS_RF_KILL_HW, &priv->status))
6179 IWL_DEBUG_RF_KILL("Can not turn radio back on - "
6180 "disabled by SW switch\n");
6181 else
6182 IWL_WARNING("Radio Frequency Kill Switch is On:\n"
6183 "Kill switch must be turned off for "
6184 "wireless networking to work.\n");
6185 }
6186 mutex_unlock(&priv->mutex);
6187}
6188
6189#define IWL_SCAN_CHECK_WATCHDOG (7 * HZ)
6190
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006191static void iwl4965_bg_scan_check(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07006192{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006193 struct iwl_priv *priv =
6194 container_of(data, struct iwl_priv, scan_check.work);
Zhu Yib481de92007-09-25 17:54:57 -07006195
6196 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
6197 return;
6198
6199 mutex_lock(&priv->mutex);
6200 if (test_bit(STATUS_SCANNING, &priv->status) ||
6201 test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
6202 IWL_DEBUG(IWL_DL_INFO | IWL_DL_SCAN,
6203 "Scan completion watchdog resetting adapter (%dms)\n",
6204 jiffies_to_msecs(IWL_SCAN_CHECK_WATCHDOG));
mabbas052c4b92007-10-25 17:15:43 +08006205
Zhu Yib481de92007-09-25 17:54:57 -07006206 if (!test_bit(STATUS_EXIT_PENDING, &priv->status))
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006207 iwl4965_send_scan_abort(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006208 }
6209 mutex_unlock(&priv->mutex);
6210}
6211
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006212static void iwl4965_bg_request_scan(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07006213{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006214 struct iwl_priv *priv =
6215 container_of(data, struct iwl_priv, request_scan);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006216 struct iwl4965_host_cmd cmd = {
Zhu Yib481de92007-09-25 17:54:57 -07006217 .id = REPLY_SCAN_CMD,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006218 .len = sizeof(struct iwl4965_scan_cmd),
Zhu Yib481de92007-09-25 17:54:57 -07006219 .meta.flags = CMD_SIZE_HUGE,
6220 };
6221 int rc = 0;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006222 struct iwl4965_scan_cmd *scan;
Zhu Yib481de92007-09-25 17:54:57 -07006223 struct ieee80211_conf *conf = NULL;
Tomas Winkler78330fd2008-02-06 02:37:18 +02006224 u16 cmd_len;
Johannes Berg8318d782008-01-24 19:38:38 +01006225 enum ieee80211_band band;
Tomas Winkler78330fd2008-02-06 02:37:18 +02006226 u8 direct_mask;
Zhu Yib481de92007-09-25 17:54:57 -07006227
6228 conf = ieee80211_get_hw_conf(priv->hw);
6229
6230 mutex_lock(&priv->mutex);
6231
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006232 if (!iwl4965_is_ready(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07006233 IWL_WARNING("request scan called when driver not ready.\n");
6234 goto done;
6235 }
6236
6237 /* Make sure the scan wasn't cancelled before this queued work
6238 * was given the chance to run... */
6239 if (!test_bit(STATUS_SCANNING, &priv->status))
6240 goto done;
6241
6242 /* This should never be called or scheduled if there is currently
6243 * a scan active in the hardware. */
6244 if (test_bit(STATUS_SCAN_HW, &priv->status)) {
6245 IWL_DEBUG_INFO("Multiple concurrent scan requests in parallel. "
6246 "Ignoring second request.\n");
6247 rc = -EIO;
6248 goto done;
6249 }
6250
6251 if (test_bit(STATUS_EXIT_PENDING, &priv->status)) {
6252 IWL_DEBUG_SCAN("Aborting scan due to device shutdown\n");
6253 goto done;
6254 }
6255
6256 if (test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
6257 IWL_DEBUG_HC("Scan request while abort pending. Queuing.\n");
6258 goto done;
6259 }
6260
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006261 if (iwl4965_is_rfkill(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07006262 IWL_DEBUG_HC("Aborting scan due to RF Kill activation\n");
6263 goto done;
6264 }
6265
6266 if (!test_bit(STATUS_READY, &priv->status)) {
6267 IWL_DEBUG_HC("Scan request while uninitialized. Queuing.\n");
6268 goto done;
6269 }
6270
6271 if (!priv->scan_bands) {
6272 IWL_DEBUG_HC("Aborting scan due to no requested bands\n");
6273 goto done;
6274 }
6275
6276 if (!priv->scan) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006277 priv->scan = kmalloc(sizeof(struct iwl4965_scan_cmd) +
Zhu Yib481de92007-09-25 17:54:57 -07006278 IWL_MAX_SCAN_SIZE, GFP_KERNEL);
6279 if (!priv->scan) {
6280 rc = -ENOMEM;
6281 goto done;
6282 }
6283 }
6284 scan = priv->scan;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006285 memset(scan, 0, sizeof(struct iwl4965_scan_cmd) + IWL_MAX_SCAN_SIZE);
Zhu Yib481de92007-09-25 17:54:57 -07006286
6287 scan->quiet_plcp_th = IWL_PLCP_QUIET_THRESH;
6288 scan->quiet_time = IWL_ACTIVE_QUIET_TIME;
6289
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006290 if (iwl4965_is_associated(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07006291 u16 interval = 0;
6292 u32 extra;
6293 u32 suspend_time = 100;
6294 u32 scan_suspend_time = 100;
6295 unsigned long flags;
6296
6297 IWL_DEBUG_INFO("Scanning while associated...\n");
6298
6299 spin_lock_irqsave(&priv->lock, flags);
6300 interval = priv->beacon_int;
6301 spin_unlock_irqrestore(&priv->lock, flags);
6302
6303 scan->suspend_time = 0;
mabbas052c4b92007-10-25 17:15:43 +08006304 scan->max_out_time = cpu_to_le32(200 * 1024);
Zhu Yib481de92007-09-25 17:54:57 -07006305 if (!interval)
6306 interval = suspend_time;
6307
6308 extra = (suspend_time / interval) << 22;
6309 scan_suspend_time = (extra |
6310 ((suspend_time % interval) * 1024));
6311 scan->suspend_time = cpu_to_le32(scan_suspend_time);
6312 IWL_DEBUG_SCAN("suspend_time 0x%X beacon interval %d\n",
6313 scan_suspend_time, interval);
6314 }
6315
6316 /* We should add the ability for user to lock to PASSIVE ONLY */
6317 if (priv->one_direct_scan) {
6318 IWL_DEBUG_SCAN
6319 ("Kicking off one direct scan for '%s'\n",
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006320 iwl4965_escape_essid(priv->direct_ssid,
Zhu Yib481de92007-09-25 17:54:57 -07006321 priv->direct_ssid_len));
6322 scan->direct_scan[0].id = WLAN_EID_SSID;
6323 scan->direct_scan[0].len = priv->direct_ssid_len;
6324 memcpy(scan->direct_scan[0].ssid,
6325 priv->direct_ssid, priv->direct_ssid_len);
6326 direct_mask = 1;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006327 } else if (!iwl4965_is_associated(priv) && priv->essid_len) {
Zhu Yib481de92007-09-25 17:54:57 -07006328 scan->direct_scan[0].id = WLAN_EID_SSID;
6329 scan->direct_scan[0].len = priv->essid_len;
6330 memcpy(scan->direct_scan[0].ssid, priv->essid, priv->essid_len);
6331 direct_mask = 1;
6332 } else
6333 direct_mask = 0;
6334
Zhu Yib481de92007-09-25 17:54:57 -07006335 scan->tx_cmd.tx_flags = TX_CMD_FLG_SEQ_CTL_MSK;
6336 scan->tx_cmd.sta_id = priv->hw_setting.bcast_sta_id;
6337 scan->tx_cmd.stop_time.life_time = TX_CMD_LIFE_TIME_INFINITE;
6338
Zhu Yib481de92007-09-25 17:54:57 -07006339
6340 switch (priv->scan_bands) {
6341 case 2:
6342 scan->flags = RXON_FLG_BAND_24G_MSK | RXON_FLG_AUTO_DETECT_MSK;
6343 scan->tx_cmd.rate_n_flags =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006344 iwl4965_hw_set_rate_n_flags(IWL_RATE_1M_PLCP,
Zhu Yib481de92007-09-25 17:54:57 -07006345 RATE_MCS_ANT_B_MSK|RATE_MCS_CCK_MSK);
6346
6347 scan->good_CRC_th = 0;
Johannes Berg8318d782008-01-24 19:38:38 +01006348 band = IEEE80211_BAND_2GHZ;
Zhu Yib481de92007-09-25 17:54:57 -07006349 break;
6350
6351 case 1:
6352 scan->tx_cmd.rate_n_flags =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006353 iwl4965_hw_set_rate_n_flags(IWL_RATE_6M_PLCP,
Zhu Yib481de92007-09-25 17:54:57 -07006354 RATE_MCS_ANT_B_MSK);
6355 scan->good_CRC_th = IWL_GOOD_CRC_TH;
Johannes Berg8318d782008-01-24 19:38:38 +01006356 band = IEEE80211_BAND_5GHZ;
Zhu Yib481de92007-09-25 17:54:57 -07006357 break;
6358
6359 default:
6360 IWL_WARNING("Invalid scan band count\n");
6361 goto done;
6362 }
6363
Tomas Winkler78330fd2008-02-06 02:37:18 +02006364 /* We don't build a direct scan probe request; the uCode will do
6365 * that based on the direct_mask added to each channel entry */
6366 cmd_len = iwl4965_fill_probe_req(priv, band,
6367 (struct ieee80211_mgmt *)scan->data,
6368 IWL_MAX_SCAN_SIZE - sizeof(*scan), 0);
6369
6370 scan->tx_cmd.len = cpu_to_le16(cmd_len);
Zhu Yib481de92007-09-25 17:54:57 -07006371 /* select Rx chains */
6372
6373 /* Force use of chains B and C (0x6) for scan Rx.
6374 * Avoid A (0x1) because of its off-channel reception on A-band.
6375 * MIMO is not used here, but value is required to make uCode happy. */
6376 scan->rx_chain = RXON_RX_CHAIN_DRIVER_FORCE_MSK |
6377 cpu_to_le16((0x7 << RXON_RX_CHAIN_VALID_POS) |
6378 (0x6 << RXON_RX_CHAIN_FORCE_SEL_POS) |
6379 (0x7 << RXON_RX_CHAIN_FORCE_MIMO_SEL_POS));
6380
6381 if (priv->iw_mode == IEEE80211_IF_TYPE_MNTR)
6382 scan->filter_flags = RXON_FILTER_PROMISC_MSK;
6383
Reinette Chatre26c0f032008-03-11 16:17:15 -07006384 if (direct_mask) {
Zhu Yib481de92007-09-25 17:54:57 -07006385 IWL_DEBUG_SCAN
6386 ("Initiating direct scan for %s.\n",
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006387 iwl4965_escape_essid(priv->essid, priv->essid_len));
Reinette Chatre26c0f032008-03-11 16:17:15 -07006388 scan->channel_count =
6389 iwl4965_get_channels_for_scan(
6390 priv, band, 1, /* active */
6391 direct_mask,
6392 (void *)&scan->data[le16_to_cpu(scan->tx_cmd.len)]);
6393 } else {
Zhu Yib481de92007-09-25 17:54:57 -07006394 IWL_DEBUG_SCAN("Initiating indirect scan.\n");
Reinette Chatre26c0f032008-03-11 16:17:15 -07006395 scan->channel_count =
6396 iwl4965_get_channels_for_scan(
6397 priv, band, 0, /* passive */
6398 direct_mask,
6399 (void *)&scan->data[le16_to_cpu(scan->tx_cmd.len)]);
6400 }
Zhu Yib481de92007-09-25 17:54:57 -07006401
6402 cmd.len += le16_to_cpu(scan->tx_cmd.len) +
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006403 scan->channel_count * sizeof(struct iwl4965_scan_channel);
Zhu Yib481de92007-09-25 17:54:57 -07006404 cmd.data = scan;
6405 scan->len = cpu_to_le16(cmd.len);
6406
6407 set_bit(STATUS_SCAN_HW, &priv->status);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006408 rc = iwl4965_send_cmd_sync(priv, &cmd);
Zhu Yib481de92007-09-25 17:54:57 -07006409 if (rc)
6410 goto done;
6411
6412 queue_delayed_work(priv->workqueue, &priv->scan_check,
6413 IWL_SCAN_CHECK_WATCHDOG);
6414
6415 mutex_unlock(&priv->mutex);
6416 return;
6417
6418 done:
Ian Schram01ebd062007-10-25 17:15:22 +08006419 /* inform mac80211 scan aborted */
Zhu Yib481de92007-09-25 17:54:57 -07006420 queue_work(priv->workqueue, &priv->scan_completed);
6421 mutex_unlock(&priv->mutex);
6422}
6423
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006424static void iwl4965_bg_up(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07006425{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006426 struct iwl_priv *priv = container_of(data, struct iwl_priv, up);
Zhu Yib481de92007-09-25 17:54:57 -07006427
6428 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
6429 return;
6430
6431 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006432 __iwl4965_up(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006433 mutex_unlock(&priv->mutex);
6434}
6435
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006436static void iwl4965_bg_restart(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07006437{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006438 struct iwl_priv *priv = container_of(data, struct iwl_priv, restart);
Zhu Yib481de92007-09-25 17:54:57 -07006439
6440 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
6441 return;
6442
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006443 iwl4965_down(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006444 queue_work(priv->workqueue, &priv->up);
6445}
6446
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006447static void iwl4965_bg_rx_replenish(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07006448{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006449 struct iwl_priv *priv =
6450 container_of(data, struct iwl_priv, rx_replenish);
Zhu Yib481de92007-09-25 17:54:57 -07006451
6452 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
6453 return;
6454
6455 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006456 iwl4965_rx_replenish(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006457 mutex_unlock(&priv->mutex);
6458}
6459
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08006460#define IWL_DELAY_NEXT_SCAN (HZ*2)
6461
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006462static void iwl4965_bg_post_associate(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07006463{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006464 struct iwl_priv *priv = container_of(data, struct iwl_priv,
Zhu Yib481de92007-09-25 17:54:57 -07006465 post_associate.work);
6466
6467 int rc = 0;
6468 struct ieee80211_conf *conf = NULL;
Joe Perches0795af52007-10-03 17:59:30 -07006469 DECLARE_MAC_BUF(mac);
Zhu Yib481de92007-09-25 17:54:57 -07006470
6471 if (priv->iw_mode == IEEE80211_IF_TYPE_AP) {
6472 IWL_ERROR("%s Should not be called in AP mode\n", __FUNCTION__);
6473 return;
6474 }
6475
Joe Perches0795af52007-10-03 17:59:30 -07006476 IWL_DEBUG_ASSOC("Associated as %d to: %s\n",
6477 priv->assoc_id,
6478 print_mac(mac, priv->active_rxon.bssid_addr));
Zhu Yib481de92007-09-25 17:54:57 -07006479
6480
6481 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
6482 return;
6483
6484 mutex_lock(&priv->mutex);
6485
Johannes Berg32bfd352007-12-19 01:31:26 +01006486 if (!priv->vif || !priv->is_open) {
Mohamed Abbas948c1712007-10-25 17:15:45 +08006487 mutex_unlock(&priv->mutex);
6488 return;
6489 }
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006490 iwl4965_scan_cancel_timeout(priv, 200);
mabbas052c4b92007-10-25 17:15:43 +08006491
Zhu Yib481de92007-09-25 17:54:57 -07006492 conf = ieee80211_get_hw_conf(priv->hw);
6493
6494 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006495 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006496
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006497 memset(&priv->rxon_timing, 0, sizeof(struct iwl4965_rxon_time_cmd));
6498 iwl4965_setup_rxon_timing(priv);
6499 rc = iwl4965_send_cmd_pdu(priv, REPLY_RXON_TIMING,
Zhu Yib481de92007-09-25 17:54:57 -07006500 sizeof(priv->rxon_timing), &priv->rxon_timing);
6501 if (rc)
6502 IWL_WARNING("REPLY_RXON_TIMING failed - "
6503 "Attempting to continue.\n");
6504
6505 priv->staging_rxon.filter_flags |= RXON_FILTER_ASSOC_MSK;
6506
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08006507#ifdef CONFIG_IWL4965_HT
Ron Rindjunskyfd105e72007-11-26 16:14:39 +02006508 if (priv->current_ht_config.is_ht)
6509 iwl4965_set_rxon_ht(priv, &priv->current_ht_config);
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08006510#endif /* CONFIG_IWL4965_HT*/
Zhu Yib481de92007-09-25 17:54:57 -07006511 iwl4965_set_rxon_chain(priv);
6512 priv->staging_rxon.assoc_id = cpu_to_le16(priv->assoc_id);
6513
6514 IWL_DEBUG_ASSOC("assoc id %d beacon interval %d\n",
6515 priv->assoc_id, priv->beacon_int);
6516
6517 if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_PREAMBLE)
6518 priv->staging_rxon.flags |= RXON_FLG_SHORT_PREAMBLE_MSK;
6519 else
6520 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_PREAMBLE_MSK;
6521
6522 if (priv->staging_rxon.flags & RXON_FLG_BAND_24G_MSK) {
6523 if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_SLOT_TIME)
6524 priv->staging_rxon.flags |= RXON_FLG_SHORT_SLOT_MSK;
6525 else
6526 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
6527
6528 if (priv->iw_mode == IEEE80211_IF_TYPE_IBSS)
6529 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
6530
6531 }
6532
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006533 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006534
6535 switch (priv->iw_mode) {
6536 case IEEE80211_IF_TYPE_STA:
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006537 iwl4965_rate_scale_init(priv->hw, IWL_AP_ID);
Zhu Yib481de92007-09-25 17:54:57 -07006538 break;
6539
6540 case IEEE80211_IF_TYPE_IBSS:
6541
6542 /* clear out the station table */
Assaf Kraussbf85ea42008-03-14 10:38:49 -07006543 iwlcore_clear_stations_table(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006544
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006545 iwl4965_rxon_add_station(priv, iwl4965_broadcast_addr, 0);
6546 iwl4965_rxon_add_station(priv, priv->bssid, 0);
6547 iwl4965_rate_scale_init(priv->hw, IWL_STA_ID);
6548 iwl4965_send_beacon_cmd(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006549
6550 break;
6551
6552 default:
6553 IWL_ERROR("%s Should not be called in %d mode\n",
6554 __FUNCTION__, priv->iw_mode);
6555 break;
6556 }
6557
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006558 iwl4965_sequence_reset(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006559
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08006560#ifdef CONFIG_IWL4965_SENSITIVITY
Zhu Yib481de92007-09-25 17:54:57 -07006561 /* Enable Rx differential gain and sensitivity calibrations */
6562 iwl4965_chain_noise_reset(priv);
6563 priv->start_calib = 1;
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08006564#endif /* CONFIG_IWL4965_SENSITIVITY */
Zhu Yib481de92007-09-25 17:54:57 -07006565
6566 if (priv->iw_mode == IEEE80211_IF_TYPE_IBSS)
6567 priv->assoc_station_added = 1;
6568
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006569 iwl4965_activate_qos(priv, 0);
Ron Rindjunsky292ae172008-02-06 11:20:39 -08006570
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08006571 /* we have just associated, don't start scan too early */
6572 priv->next_scan_jiffies = jiffies + IWL_DELAY_NEXT_SCAN;
Zhu Yib481de92007-09-25 17:54:57 -07006573 mutex_unlock(&priv->mutex);
6574}
6575
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006576static void iwl4965_bg_abort_scan(struct work_struct *work)
Zhu Yib481de92007-09-25 17:54:57 -07006577{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006578 struct iwl_priv *priv = container_of(work, struct iwl_priv, abort_scan);
Zhu Yib481de92007-09-25 17:54:57 -07006579
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006580 if (!iwl4965_is_ready(priv))
Zhu Yib481de92007-09-25 17:54:57 -07006581 return;
6582
6583 mutex_lock(&priv->mutex);
6584
6585 set_bit(STATUS_SCAN_ABORTING, &priv->status);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006586 iwl4965_send_scan_abort(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006587
6588 mutex_unlock(&priv->mutex);
6589}
6590
Zhu Yi76bb77e2007-11-22 10:53:22 +08006591static int iwl4965_mac_config(struct ieee80211_hw *hw, struct ieee80211_conf *conf);
6592
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006593static void iwl4965_bg_scan_completed(struct work_struct *work)
Zhu Yib481de92007-09-25 17:54:57 -07006594{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006595 struct iwl_priv *priv =
6596 container_of(work, struct iwl_priv, scan_completed);
Zhu Yib481de92007-09-25 17:54:57 -07006597
6598 IWL_DEBUG(IWL_DL_INFO | IWL_DL_SCAN, "SCAN complete scan\n");
6599
6600 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
6601 return;
6602
Zhu Yia0646472007-12-20 14:10:01 +08006603 if (test_bit(STATUS_CONF_PENDING, &priv->status))
6604 iwl4965_mac_config(priv->hw, ieee80211_get_hw_conf(priv->hw));
Zhu Yi76bb77e2007-11-22 10:53:22 +08006605
Zhu Yib481de92007-09-25 17:54:57 -07006606 ieee80211_scan_completed(priv->hw);
6607
6608 /* Since setting the TXPOWER may have been deferred while
6609 * performing the scan, fire one off */
6610 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006611 iwl4965_hw_reg_send_txpower(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006612 mutex_unlock(&priv->mutex);
6613}
6614
6615/*****************************************************************************
6616 *
6617 * mac80211 entry point functions
6618 *
6619 *****************************************************************************/
6620
Zhu Yi5a66926a2008-01-14 17:46:18 -08006621#define UCODE_READY_TIMEOUT (2 * HZ)
6622
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006623static int iwl4965_mac_start(struct ieee80211_hw *hw)
Zhu Yib481de92007-09-25 17:54:57 -07006624{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006625 struct iwl_priv *priv = hw->priv;
Zhu Yi5a66926a2008-01-14 17:46:18 -08006626 int ret;
Zhu Yib481de92007-09-25 17:54:57 -07006627
6628 IWL_DEBUG_MAC80211("enter\n");
6629
Zhu Yi5a66926a2008-01-14 17:46:18 -08006630 if (pci_enable_device(priv->pci_dev)) {
6631 IWL_ERROR("Fail to pci_enable_device\n");
6632 return -ENODEV;
6633 }
6634 pci_restore_state(priv->pci_dev);
6635 pci_enable_msi(priv->pci_dev);
6636
6637 ret = request_irq(priv->pci_dev->irq, iwl4965_isr, IRQF_SHARED,
6638 DRV_NAME, priv);
6639 if (ret) {
6640 IWL_ERROR("Error allocating IRQ %d\n", priv->pci_dev->irq);
6641 goto out_disable_msi;
6642 }
6643
Zhu Yib481de92007-09-25 17:54:57 -07006644 /* we should be verifying the device is ready to be opened */
6645 mutex_lock(&priv->mutex);
6646
Zhu Yi5a66926a2008-01-14 17:46:18 -08006647 memset(&priv->staging_rxon, 0, sizeof(struct iwl4965_rxon_cmd));
6648 /* fetch ucode file from disk, alloc and copy to bus-master buffers ...
6649 * ucode filename and max sizes are card-specific. */
6650
6651 if (!priv->ucode_code.len) {
6652 ret = iwl4965_read_ucode(priv);
6653 if (ret) {
6654 IWL_ERROR("Could not read microcode: %d\n", ret);
6655 mutex_unlock(&priv->mutex);
6656 goto out_release_irq;
6657 }
6658 }
6659
Zhu Yie655b9f2008-01-24 02:19:38 -08006660 ret = __iwl4965_up(priv);
Zhu Yi5a66926a2008-01-14 17:46:18 -08006661
Zhu Yib481de92007-09-25 17:54:57 -07006662 mutex_unlock(&priv->mutex);
Zhu Yi5a66926a2008-01-14 17:46:18 -08006663
Zhu Yie655b9f2008-01-24 02:19:38 -08006664 if (ret)
6665 goto out_release_irq;
6666
6667 IWL_DEBUG_INFO("Start UP work done.\n");
6668
6669 if (test_bit(STATUS_IN_SUSPEND, &priv->status))
6670 return 0;
6671
Zhu Yi5a66926a2008-01-14 17:46:18 -08006672 /* Wait for START_ALIVE from ucode. Otherwise callbacks from
6673 * mac80211 will not be run successfully. */
6674 ret = wait_event_interruptible_timeout(priv->wait_command_queue,
6675 test_bit(STATUS_READY, &priv->status),
6676 UCODE_READY_TIMEOUT);
6677 if (!ret) {
6678 if (!test_bit(STATUS_READY, &priv->status)) {
6679 IWL_ERROR("Wait for START_ALIVE timeout after %dms.\n",
6680 jiffies_to_msecs(UCODE_READY_TIMEOUT));
6681 ret = -ETIMEDOUT;
6682 goto out_release_irq;
6683 }
6684 }
6685
Zhu Yie655b9f2008-01-24 02:19:38 -08006686 priv->is_open = 1;
Zhu Yib481de92007-09-25 17:54:57 -07006687 IWL_DEBUG_MAC80211("leave\n");
6688 return 0;
Zhu Yi5a66926a2008-01-14 17:46:18 -08006689
6690out_release_irq:
6691 free_irq(priv->pci_dev->irq, priv);
6692out_disable_msi:
6693 pci_disable_msi(priv->pci_dev);
Zhu Yie655b9f2008-01-24 02:19:38 -08006694 pci_disable_device(priv->pci_dev);
6695 priv->is_open = 0;
6696 IWL_DEBUG_MAC80211("leave - failed\n");
Zhu Yi5a66926a2008-01-14 17:46:18 -08006697 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07006698}
6699
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006700static void iwl4965_mac_stop(struct ieee80211_hw *hw)
Zhu Yib481de92007-09-25 17:54:57 -07006701{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006702 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07006703
6704 IWL_DEBUG_MAC80211("enter\n");
Mohamed Abbas948c1712007-10-25 17:15:45 +08006705
Zhu Yie655b9f2008-01-24 02:19:38 -08006706 if (!priv->is_open) {
6707 IWL_DEBUG_MAC80211("leave - skip\n");
6708 return;
6709 }
6710
Zhu Yib481de92007-09-25 17:54:57 -07006711 priv->is_open = 0;
Zhu Yi5a66926a2008-01-14 17:46:18 -08006712
6713 if (iwl4965_is_ready_rf(priv)) {
Zhu Yie655b9f2008-01-24 02:19:38 -08006714 /* stop mac, cancel any scan request and clear
6715 * RXON_FILTER_ASSOC_MSK BIT
6716 */
Zhu Yi5a66926a2008-01-14 17:46:18 -08006717 mutex_lock(&priv->mutex);
6718 iwl4965_scan_cancel_timeout(priv, 100);
6719 cancel_delayed_work(&priv->post_associate);
Mohamed Abbasfde35712007-11-29 11:10:15 +08006720 mutex_unlock(&priv->mutex);
Mohamed Abbasfde35712007-11-29 11:10:15 +08006721 }
6722
Zhu Yi5a66926a2008-01-14 17:46:18 -08006723 iwl4965_down(priv);
6724
6725 flush_workqueue(priv->workqueue);
6726 free_irq(priv->pci_dev->irq, priv);
6727 pci_disable_msi(priv->pci_dev);
6728 pci_save_state(priv->pci_dev);
6729 pci_disable_device(priv->pci_dev);
Mohamed Abbas948c1712007-10-25 17:15:45 +08006730
Zhu Yib481de92007-09-25 17:54:57 -07006731 IWL_DEBUG_MAC80211("leave\n");
Zhu Yib481de92007-09-25 17:54:57 -07006732}
6733
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006734static int iwl4965_mac_tx(struct ieee80211_hw *hw, struct sk_buff *skb,
Zhu Yib481de92007-09-25 17:54:57 -07006735 struct ieee80211_tx_control *ctl)
6736{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006737 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07006738
6739 IWL_DEBUG_MAC80211("enter\n");
6740
6741 if (priv->iw_mode == IEEE80211_IF_TYPE_MNTR) {
6742 IWL_DEBUG_MAC80211("leave - monitor\n");
6743 return -1;
6744 }
6745
6746 IWL_DEBUG_TX("dev->xmit(%d bytes) at rate 0x%02x\n", skb->len,
Johannes Berg8318d782008-01-24 19:38:38 +01006747 ctl->tx_rate->bitrate);
Zhu Yib481de92007-09-25 17:54:57 -07006748
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006749 if (iwl4965_tx_skb(priv, skb, ctl))
Zhu Yib481de92007-09-25 17:54:57 -07006750 dev_kfree_skb_any(skb);
6751
6752 IWL_DEBUG_MAC80211("leave\n");
6753 return 0;
6754}
6755
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006756static int iwl4965_mac_add_interface(struct ieee80211_hw *hw,
Zhu Yib481de92007-09-25 17:54:57 -07006757 struct ieee80211_if_init_conf *conf)
6758{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006759 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07006760 unsigned long flags;
Joe Perches0795af52007-10-03 17:59:30 -07006761 DECLARE_MAC_BUF(mac);
Zhu Yib481de92007-09-25 17:54:57 -07006762
Johannes Berg32bfd352007-12-19 01:31:26 +01006763 IWL_DEBUG_MAC80211("enter: type %d\n", conf->type);
Zhu Yib481de92007-09-25 17:54:57 -07006764
Johannes Berg32bfd352007-12-19 01:31:26 +01006765 if (priv->vif) {
6766 IWL_DEBUG_MAC80211("leave - vif != NULL\n");
Reinette Chatre75849d22008-01-23 10:15:17 -08006767 return -EOPNOTSUPP;
Zhu Yib481de92007-09-25 17:54:57 -07006768 }
6769
6770 spin_lock_irqsave(&priv->lock, flags);
Johannes Berg32bfd352007-12-19 01:31:26 +01006771 priv->vif = conf->vif;
Zhu Yib481de92007-09-25 17:54:57 -07006772
6773 spin_unlock_irqrestore(&priv->lock, flags);
6774
6775 mutex_lock(&priv->mutex);
Tomas Winkler864792e2007-11-27 21:00:52 +02006776
6777 if (conf->mac_addr) {
6778 IWL_DEBUG_MAC80211("Set %s\n", print_mac(mac, conf->mac_addr));
6779 memcpy(priv->mac_addr, conf->mac_addr, ETH_ALEN);
6780 }
Zhu Yib481de92007-09-25 17:54:57 -07006781
Zhu Yi5a66926a2008-01-14 17:46:18 -08006782 if (iwl4965_is_ready(priv))
6783 iwl4965_set_mode(priv, conf->type);
6784
Zhu Yib481de92007-09-25 17:54:57 -07006785 mutex_unlock(&priv->mutex);
6786
Zhu Yi5a66926a2008-01-14 17:46:18 -08006787 IWL_DEBUG_MAC80211("leave\n");
Zhu Yib481de92007-09-25 17:54:57 -07006788 return 0;
6789}
6790
6791/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006792 * iwl4965_mac_config - mac80211 config callback
Zhu Yib481de92007-09-25 17:54:57 -07006793 *
6794 * We ignore conf->flags & IEEE80211_CONF_SHORT_SLOT_TIME since it seems to
6795 * be set inappropriately and the driver currently sets the hardware up to
6796 * use it whenever needed.
6797 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006798static int iwl4965_mac_config(struct ieee80211_hw *hw, struct ieee80211_conf *conf)
Zhu Yib481de92007-09-25 17:54:57 -07006799{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006800 struct iwl_priv *priv = hw->priv;
Assaf Kraussbf85ea42008-03-14 10:38:49 -07006801 const struct iwl_channel_info *ch_info;
Zhu Yib481de92007-09-25 17:54:57 -07006802 unsigned long flags;
Zhu Yi76bb77e2007-11-22 10:53:22 +08006803 int ret = 0;
Zhu Yib481de92007-09-25 17:54:57 -07006804
6805 mutex_lock(&priv->mutex);
Johannes Berg8318d782008-01-24 19:38:38 +01006806 IWL_DEBUG_MAC80211("enter to channel %d\n", conf->channel->hw_value);
Zhu Yib481de92007-09-25 17:54:57 -07006807
Zhu Yi12342c42007-12-20 11:27:32 +08006808 priv->add_radiotap = !!(conf->flags & IEEE80211_CONF_RADIOTAP);
6809
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006810 if (!iwl4965_is_ready(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07006811 IWL_DEBUG_MAC80211("leave - not ready\n");
Zhu Yi76bb77e2007-11-22 10:53:22 +08006812 ret = -EIO;
6813 goto out;
Zhu Yib481de92007-09-25 17:54:57 -07006814 }
6815
Assaf Krauss00acbc92008-03-14 10:38:47 -07006816 if (unlikely(!iwl4965_mod_params.disable_hw_scan &&
Zhu Yib481de92007-09-25 17:54:57 -07006817 test_bit(STATUS_SCANNING, &priv->status))) {
Zhu Yia0646472007-12-20 14:10:01 +08006818 IWL_DEBUG_MAC80211("leave - scanning\n");
6819 set_bit(STATUS_CONF_PENDING, &priv->status);
Zhu Yib481de92007-09-25 17:54:57 -07006820 mutex_unlock(&priv->mutex);
Zhu Yia0646472007-12-20 14:10:01 +08006821 return 0;
Zhu Yib481de92007-09-25 17:54:57 -07006822 }
6823
6824 spin_lock_irqsave(&priv->lock, flags);
6825
Johannes Berg8318d782008-01-24 19:38:38 +01006826 ch_info = iwl4965_get_channel_info(priv, conf->channel->band,
6827 ieee80211_frequency_to_channel(conf->channel->center_freq));
Zhu Yib481de92007-09-25 17:54:57 -07006828 if (!is_channel_valid(ch_info)) {
Zhu Yib481de92007-09-25 17:54:57 -07006829 IWL_DEBUG_MAC80211("leave - invalid channel\n");
6830 spin_unlock_irqrestore(&priv->lock, flags);
Zhu Yi76bb77e2007-11-22 10:53:22 +08006831 ret = -EINVAL;
6832 goto out;
Zhu Yib481de92007-09-25 17:54:57 -07006833 }
6834
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08006835#ifdef CONFIG_IWL4965_HT
Tomas Winkler78330fd2008-02-06 02:37:18 +02006836 /* if we are switching from ht to 2.4 clear flags
Zhu Yib481de92007-09-25 17:54:57 -07006837 * from any ht related info since 2.4 does not
6838 * support ht */
Tomas Winkler78330fd2008-02-06 02:37:18 +02006839 if ((le16_to_cpu(priv->staging_rxon.channel) != conf->channel->hw_value)
Zhu Yib481de92007-09-25 17:54:57 -07006840#ifdef IEEE80211_CONF_CHANNEL_SWITCH
6841 && !(conf->flags & IEEE80211_CONF_CHANNEL_SWITCH)
6842#endif
6843 )
6844 priv->staging_rxon.flags = 0;
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08006845#endif /* CONFIG_IWL4965_HT */
Zhu Yib481de92007-09-25 17:54:57 -07006846
Assaf Kraussbf85ea42008-03-14 10:38:49 -07006847 iwlcore_set_rxon_channel(priv, conf->channel->band,
Johannes Berg8318d782008-01-24 19:38:38 +01006848 ieee80211_frequency_to_channel(conf->channel->center_freq));
Zhu Yib481de92007-09-25 17:54:57 -07006849
Johannes Berg8318d782008-01-24 19:38:38 +01006850 iwl4965_set_flags_for_phymode(priv, conf->channel->band);
Zhu Yib481de92007-09-25 17:54:57 -07006851
6852 /* The list of supported rates and rate mask can be different
Johannes Berg8318d782008-01-24 19:38:38 +01006853 * for each band; since the band may have changed, reset
Zhu Yib481de92007-09-25 17:54:57 -07006854 * the rate mask to what mac80211 lists */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006855 iwl4965_set_rate(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006856
6857 spin_unlock_irqrestore(&priv->lock, flags);
6858
6859#ifdef IEEE80211_CONF_CHANNEL_SWITCH
6860 if (conf->flags & IEEE80211_CONF_CHANNEL_SWITCH) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006861 iwl4965_hw_channel_switch(priv, conf->channel);
Zhu Yi76bb77e2007-11-22 10:53:22 +08006862 goto out;
Zhu Yib481de92007-09-25 17:54:57 -07006863 }
6864#endif
6865
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006866 iwl4965_radio_kill_sw(priv, !conf->radio_enabled);
Zhu Yib481de92007-09-25 17:54:57 -07006867
6868 if (!conf->radio_enabled) {
6869 IWL_DEBUG_MAC80211("leave - radio disabled\n");
Zhu Yi76bb77e2007-11-22 10:53:22 +08006870 goto out;
Zhu Yib481de92007-09-25 17:54:57 -07006871 }
6872
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006873 if (iwl4965_is_rfkill(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07006874 IWL_DEBUG_MAC80211("leave - RF kill\n");
Zhu Yi76bb77e2007-11-22 10:53:22 +08006875 ret = -EIO;
6876 goto out;
Zhu Yib481de92007-09-25 17:54:57 -07006877 }
6878
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006879 iwl4965_set_rate(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006880
6881 if (memcmp(&priv->active_rxon,
6882 &priv->staging_rxon, sizeof(priv->staging_rxon)))
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006883 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006884 else
6885 IWL_DEBUG_INFO("No re-sending same RXON configuration.\n");
6886
6887 IWL_DEBUG_MAC80211("leave\n");
6888
Zhu Yia0646472007-12-20 14:10:01 +08006889out:
6890 clear_bit(STATUS_CONF_PENDING, &priv->status);
Zhu Yi5a66926a2008-01-14 17:46:18 -08006891 mutex_unlock(&priv->mutex);
Zhu Yi76bb77e2007-11-22 10:53:22 +08006892 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07006893}
6894
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006895static void iwl4965_config_ap(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07006896{
6897 int rc = 0;
6898
Maarten Lankhorstd986bcd2008-01-23 10:15:16 -08006899 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
Zhu Yib481de92007-09-25 17:54:57 -07006900 return;
6901
6902 /* The following should be done only at AP bring up */
6903 if ((priv->active_rxon.filter_flags & RXON_FILTER_ASSOC_MSK) == 0) {
6904
6905 /* RXON - unassoc (to set timing command) */
6906 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006907 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006908
6909 /* RXON Timing */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006910 memset(&priv->rxon_timing, 0, sizeof(struct iwl4965_rxon_time_cmd));
6911 iwl4965_setup_rxon_timing(priv);
6912 rc = iwl4965_send_cmd_pdu(priv, REPLY_RXON_TIMING,
Zhu Yib481de92007-09-25 17:54:57 -07006913 sizeof(priv->rxon_timing), &priv->rxon_timing);
6914 if (rc)
6915 IWL_WARNING("REPLY_RXON_TIMING failed - "
6916 "Attempting to continue.\n");
6917
6918 iwl4965_set_rxon_chain(priv);
6919
6920 /* FIXME: what should be the assoc_id for AP? */
6921 priv->staging_rxon.assoc_id = cpu_to_le16(priv->assoc_id);
6922 if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_PREAMBLE)
6923 priv->staging_rxon.flags |=
6924 RXON_FLG_SHORT_PREAMBLE_MSK;
6925 else
6926 priv->staging_rxon.flags &=
6927 ~RXON_FLG_SHORT_PREAMBLE_MSK;
6928
6929 if (priv->staging_rxon.flags & RXON_FLG_BAND_24G_MSK) {
6930 if (priv->assoc_capability &
6931 WLAN_CAPABILITY_SHORT_SLOT_TIME)
6932 priv->staging_rxon.flags |=
6933 RXON_FLG_SHORT_SLOT_MSK;
6934 else
6935 priv->staging_rxon.flags &=
6936 ~RXON_FLG_SHORT_SLOT_MSK;
6937
6938 if (priv->iw_mode == IEEE80211_IF_TYPE_IBSS)
6939 priv->staging_rxon.flags &=
6940 ~RXON_FLG_SHORT_SLOT_MSK;
6941 }
6942 /* restore RXON assoc */
6943 priv->staging_rxon.filter_flags |= RXON_FILTER_ASSOC_MSK;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006944 iwl4965_commit_rxon(priv);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006945 iwl4965_activate_qos(priv, 1);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006946 iwl4965_rxon_add_station(priv, iwl4965_broadcast_addr, 0);
Zhu Yie1493de2007-09-27 11:27:32 +08006947 }
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006948 iwl4965_send_beacon_cmd(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006949
6950 /* FIXME - we need to add code here to detect a totally new
6951 * configuration, reset the AP, unassoc, rxon timing, assoc,
6952 * clear sta table, add BCAST sta... */
6953}
6954
Johannes Berg32bfd352007-12-19 01:31:26 +01006955static int iwl4965_mac_config_interface(struct ieee80211_hw *hw,
6956 struct ieee80211_vif *vif,
Zhu Yib481de92007-09-25 17:54:57 -07006957 struct ieee80211_if_conf *conf)
6958{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006959 struct iwl_priv *priv = hw->priv;
Joe Perches0795af52007-10-03 17:59:30 -07006960 DECLARE_MAC_BUF(mac);
Zhu Yib481de92007-09-25 17:54:57 -07006961 unsigned long flags;
6962 int rc;
6963
6964 if (conf == NULL)
6965 return -EIO;
6966
Emmanuel Grumbachb716bb92008-03-04 18:09:32 -08006967 if (priv->vif != vif) {
6968 IWL_DEBUG_MAC80211("leave - priv->vif != vif\n");
6969 mutex_unlock(&priv->mutex);
6970 return 0;
6971 }
6972
Zhu Yib481de92007-09-25 17:54:57 -07006973 if ((priv->iw_mode == IEEE80211_IF_TYPE_AP) &&
6974 (!conf->beacon || !conf->ssid_len)) {
6975 IWL_DEBUG_MAC80211
6976 ("Leaving in AP mode because HostAPD is not ready.\n");
6977 return 0;
6978 }
6979
Zhu Yi5a66926a2008-01-14 17:46:18 -08006980 if (!iwl4965_is_alive(priv))
6981 return -EAGAIN;
6982
Zhu Yib481de92007-09-25 17:54:57 -07006983 mutex_lock(&priv->mutex);
6984
Zhu Yib481de92007-09-25 17:54:57 -07006985 if (conf->bssid)
Joe Perches0795af52007-10-03 17:59:30 -07006986 IWL_DEBUG_MAC80211("bssid: %s\n",
6987 print_mac(mac, conf->bssid));
Zhu Yib481de92007-09-25 17:54:57 -07006988
Johannes Berg4150c572007-09-17 01:29:23 -04006989/*
6990 * very dubious code was here; the probe filtering flag is never set:
6991 *
Zhu Yib481de92007-09-25 17:54:57 -07006992 if (unlikely(test_bit(STATUS_SCANNING, &priv->status)) &&
6993 !(priv->hw->flags & IEEE80211_HW_NO_PROBE_FILTERING)) {
Johannes Berg4150c572007-09-17 01:29:23 -04006994 */
Zhu Yib481de92007-09-25 17:54:57 -07006995
6996 if (priv->iw_mode == IEEE80211_IF_TYPE_AP) {
6997 if (!conf->bssid) {
6998 conf->bssid = priv->mac_addr;
6999 memcpy(priv->bssid, priv->mac_addr, ETH_ALEN);
Joe Perches0795af52007-10-03 17:59:30 -07007000 IWL_DEBUG_MAC80211("bssid was set to: %s\n",
7001 print_mac(mac, conf->bssid));
Zhu Yib481de92007-09-25 17:54:57 -07007002 }
7003 if (priv->ibss_beacon)
7004 dev_kfree_skb(priv->ibss_beacon);
7005
7006 priv->ibss_beacon = conf->beacon;
7007 }
7008
Mohamed Abbasfde35712007-11-29 11:10:15 +08007009 if (iwl4965_is_rfkill(priv))
7010 goto done;
7011
Zhu Yib481de92007-09-25 17:54:57 -07007012 if (conf->bssid && !is_zero_ether_addr(conf->bssid) &&
7013 !is_multicast_ether_addr(conf->bssid)) {
7014 /* If there is currently a HW scan going on in the background
7015 * then we need to cancel it else the RXON below will fail. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007016 if (iwl4965_scan_cancel_timeout(priv, 100)) {
Zhu Yib481de92007-09-25 17:54:57 -07007017 IWL_WARNING("Aborted scan still in progress "
7018 "after 100ms\n");
7019 IWL_DEBUG_MAC80211("leaving - scan abort failed.\n");
7020 mutex_unlock(&priv->mutex);
7021 return -EAGAIN;
7022 }
7023 memcpy(priv->staging_rxon.bssid_addr, conf->bssid, ETH_ALEN);
7024
7025 /* TODO: Audit driver for usage of these members and see
7026 * if mac80211 deprecates them (priv->bssid looks like it
7027 * shouldn't be there, but I haven't scanned the IBSS code
7028 * to verify) - jpk */
7029 memcpy(priv->bssid, conf->bssid, ETH_ALEN);
7030
7031 if (priv->iw_mode == IEEE80211_IF_TYPE_AP)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007032 iwl4965_config_ap(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007033 else {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007034 rc = iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007035 if ((priv->iw_mode == IEEE80211_IF_TYPE_STA) && rc)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007036 iwl4965_rxon_add_station(
Zhu Yib481de92007-09-25 17:54:57 -07007037 priv, priv->active_rxon.bssid_addr, 1);
7038 }
7039
7040 } else {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007041 iwl4965_scan_cancel_timeout(priv, 100);
Zhu Yib481de92007-09-25 17:54:57 -07007042 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007043 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007044 }
7045
Mohamed Abbasfde35712007-11-29 11:10:15 +08007046 done:
Zhu Yib481de92007-09-25 17:54:57 -07007047 spin_lock_irqsave(&priv->lock, flags);
7048 if (!conf->ssid_len)
7049 memset(priv->essid, 0, IW_ESSID_MAX_SIZE);
7050 else
7051 memcpy(priv->essid, conf->ssid, conf->ssid_len);
7052
7053 priv->essid_len = conf->ssid_len;
7054 spin_unlock_irqrestore(&priv->lock, flags);
7055
7056 IWL_DEBUG_MAC80211("leave\n");
7057 mutex_unlock(&priv->mutex);
7058
7059 return 0;
7060}
7061
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007062static void iwl4965_configure_filter(struct ieee80211_hw *hw,
Johannes Berg4150c572007-09-17 01:29:23 -04007063 unsigned int changed_flags,
7064 unsigned int *total_flags,
7065 int mc_count, struct dev_addr_list *mc_list)
7066{
7067 /*
7068 * XXX: dummy
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007069 * see also iwl4965_connection_init_rx_config
Johannes Berg4150c572007-09-17 01:29:23 -04007070 */
7071 *total_flags = 0;
7072}
7073
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007074static void iwl4965_mac_remove_interface(struct ieee80211_hw *hw,
Zhu Yib481de92007-09-25 17:54:57 -07007075 struct ieee80211_if_init_conf *conf)
7076{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007077 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07007078
7079 IWL_DEBUG_MAC80211("enter\n");
7080
7081 mutex_lock(&priv->mutex);
Mohamed Abbas948c1712007-10-25 17:15:45 +08007082
Mohamed Abbasfde35712007-11-29 11:10:15 +08007083 if (iwl4965_is_ready_rf(priv)) {
7084 iwl4965_scan_cancel_timeout(priv, 100);
7085 cancel_delayed_work(&priv->post_associate);
7086 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
7087 iwl4965_commit_rxon(priv);
7088 }
Johannes Berg32bfd352007-12-19 01:31:26 +01007089 if (priv->vif == conf->vif) {
7090 priv->vif = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07007091 memset(priv->bssid, 0, ETH_ALEN);
7092 memset(priv->essid, 0, IW_ESSID_MAX_SIZE);
7093 priv->essid_len = 0;
7094 }
7095 mutex_unlock(&priv->mutex);
7096
7097 IWL_DEBUG_MAC80211("leave\n");
7098
7099}
Johannes Berg471b3ef2007-12-28 14:32:58 +01007100
7101static void iwl4965_bss_info_changed(struct ieee80211_hw *hw,
7102 struct ieee80211_vif *vif,
7103 struct ieee80211_bss_conf *bss_conf,
7104 u32 changes)
Tomas Winkler220173b2007-10-16 00:50:25 +02007105{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007106 struct iwl_priv *priv = hw->priv;
Tomas Winkler220173b2007-10-16 00:50:25 +02007107
Johannes Berg471b3ef2007-12-28 14:32:58 +01007108 if (changes & BSS_CHANGED_ERP_PREAMBLE) {
7109 if (bss_conf->use_short_preamble)
Tomas Winkler220173b2007-10-16 00:50:25 +02007110 priv->staging_rxon.flags |= RXON_FLG_SHORT_PREAMBLE_MSK;
7111 else
7112 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_PREAMBLE_MSK;
7113 }
7114
Johannes Berg471b3ef2007-12-28 14:32:58 +01007115 if (changes & BSS_CHANGED_ERP_CTS_PROT) {
Johannes Berg8318d782008-01-24 19:38:38 +01007116 if (bss_conf->use_cts_prot && (priv->band != IEEE80211_BAND_5GHZ))
Tomas Winkler220173b2007-10-16 00:50:25 +02007117 priv->staging_rxon.flags |= RXON_FLG_TGG_PROTECT_MSK;
7118 else
7119 priv->staging_rxon.flags &= ~RXON_FLG_TGG_PROTECT_MSK;
7120 }
7121
Johannes Berg471b3ef2007-12-28 14:32:58 +01007122 if (changes & BSS_CHANGED_ASSOC) {
7123 /*
7124 * TODO:
7125 * do stuff instead of sniffing assoc resp
7126 */
7127 }
7128
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007129 if (iwl4965_is_associated(priv))
7130 iwl4965_send_rxon_assoc(priv);
Tomas Winkler220173b2007-10-16 00:50:25 +02007131}
Zhu Yib481de92007-09-25 17:54:57 -07007132
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007133static int iwl4965_mac_hw_scan(struct ieee80211_hw *hw, u8 *ssid, size_t len)
Zhu Yib481de92007-09-25 17:54:57 -07007134{
7135 int rc = 0;
7136 unsigned long flags;
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007137 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07007138
7139 IWL_DEBUG_MAC80211("enter\n");
7140
mabbas052c4b92007-10-25 17:15:43 +08007141 mutex_lock(&priv->mutex);
Zhu Yib481de92007-09-25 17:54:57 -07007142 spin_lock_irqsave(&priv->lock, flags);
7143
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007144 if (!iwl4965_is_ready_rf(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07007145 rc = -EIO;
7146 IWL_DEBUG_MAC80211("leave - not ready or exit pending\n");
7147 goto out_unlock;
7148 }
7149
7150 if (priv->iw_mode == IEEE80211_IF_TYPE_AP) { /* APs don't scan */
7151 rc = -EIO;
7152 IWL_ERROR("ERROR: APs don't scan\n");
7153 goto out_unlock;
7154 }
7155
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08007156 /* we don't schedule scan within next_scan_jiffies period */
7157 if (priv->next_scan_jiffies &&
7158 time_after(priv->next_scan_jiffies, jiffies)) {
7159 rc = -EAGAIN;
7160 goto out_unlock;
7161 }
Zhu Yib481de92007-09-25 17:54:57 -07007162 /* if we just finished scan ask for delay */
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08007163 if (priv->last_scan_jiffies && time_after(priv->last_scan_jiffies +
7164 IWL_DELAY_NEXT_SCAN, jiffies)) {
Zhu Yib481de92007-09-25 17:54:57 -07007165 rc = -EAGAIN;
7166 goto out_unlock;
7167 }
7168 if (len) {
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08007169 IWL_DEBUG_SCAN("direct scan for %s [%d]\n ",
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007170 iwl4965_escape_essid(ssid, len), (int)len);
Zhu Yib481de92007-09-25 17:54:57 -07007171
7172 priv->one_direct_scan = 1;
7173 priv->direct_ssid_len = (u8)
7174 min((u8) len, (u8) IW_ESSID_MAX_SIZE);
7175 memcpy(priv->direct_ssid, ssid, priv->direct_ssid_len);
Mohamed Abbas948c1712007-10-25 17:15:45 +08007176 } else
7177 priv->one_direct_scan = 0;
Zhu Yib481de92007-09-25 17:54:57 -07007178
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007179 rc = iwl4965_scan_initiate(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007180
7181 IWL_DEBUG_MAC80211("leave\n");
7182
7183out_unlock:
7184 spin_unlock_irqrestore(&priv->lock, flags);
mabbas052c4b92007-10-25 17:15:43 +08007185 mutex_unlock(&priv->mutex);
Zhu Yib481de92007-09-25 17:54:57 -07007186
7187 return rc;
7188}
7189
Emmanuel Grumbachab885f82008-03-20 15:06:43 +02007190static void iwl4965_mac_update_tkip_key(struct ieee80211_hw *hw,
7191 struct ieee80211_key_conf *keyconf, const u8 *addr,
7192 u32 iv32, u16 *phase1key)
7193{
7194 struct iwl_priv *priv = hw->priv;
7195 u8 sta_id = IWL_INVALID_STATION;
7196 unsigned long flags;
7197 __le16 key_flags = 0;
7198 int i;
7199 DECLARE_MAC_BUF(mac);
7200
7201 IWL_DEBUG_MAC80211("enter\n");
7202
7203 sta_id = iwl4965_hw_find_station(priv, addr);
7204 if (sta_id == IWL_INVALID_STATION) {
7205 IWL_DEBUG_MAC80211("leave - %s not in station map.\n",
7206 print_mac(mac, addr));
7207 return;
7208 }
7209
7210 iwl4965_scan_cancel_timeout(priv, 100);
7211
7212 key_flags |= (STA_KEY_FLG_TKIP | STA_KEY_FLG_MAP_KEY_MSK);
7213 key_flags |= cpu_to_le16(keyconf->keyidx << STA_KEY_FLG_KEYID_POS);
7214 key_flags &= ~STA_KEY_FLG_INVALID;
7215
7216 if (sta_id == priv->hw_setting.bcast_sta_id)
7217 key_flags |= STA_KEY_MULTICAST_MSK;
7218
7219 spin_lock_irqsave(&priv->sta_lock, flags);
7220
7221 priv->stations[sta_id].sta.key.key_offset =
7222 (sta_id % STA_KEY_MAX_NUM);/* FIXME */
7223 priv->stations[sta_id].sta.key.key_flags = key_flags;
7224 priv->stations[sta_id].sta.key.tkip_rx_tsc_byte2 = (u8) iv32;
7225
7226 for (i = 0; i < 5; i++)
7227 priv->stations[sta_id].sta.key.tkip_rx_ttak[i] =
7228 cpu_to_le16(phase1key[i]);
7229
7230 priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
7231 priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
7232
7233 iwl4965_send_add_station(priv, &priv->stations[sta_id].sta, CMD_ASYNC);
7234
7235 spin_unlock_irqrestore(&priv->sta_lock, flags);
7236
7237 IWL_DEBUG_MAC80211("leave\n");
7238}
7239
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007240static int iwl4965_mac_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
Zhu Yib481de92007-09-25 17:54:57 -07007241 const u8 *local_addr, const u8 *addr,
7242 struct ieee80211_key_conf *key)
7243{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007244 struct iwl_priv *priv = hw->priv;
Joe Perches0795af52007-10-03 17:59:30 -07007245 DECLARE_MAC_BUF(mac);
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07007246 int ret = 0;
7247 u8 sta_id = IWL_INVALID_STATION;
7248 u8 static_key;
Zhu Yib481de92007-09-25 17:54:57 -07007249
7250 IWL_DEBUG_MAC80211("enter\n");
7251
Assaf Krauss00acbc92008-03-14 10:38:47 -07007252 if (!iwl4965_mod_params.hw_crypto) {
Zhu Yib481de92007-09-25 17:54:57 -07007253 IWL_DEBUG_MAC80211("leave - hwcrypto disabled\n");
7254 return -EOPNOTSUPP;
7255 }
7256
7257 if (is_zero_ether_addr(addr))
7258 /* only support pairwise keys */
7259 return -EOPNOTSUPP;
7260
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07007261 /* FIXME: need to differenciate between static and dynamic key
7262 * in the level of mac80211 */
7263 static_key = !iwl4965_is_associated(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007264
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07007265 if (!static_key) {
7266 sta_id = iwl4965_hw_find_station(priv, addr);
7267 if (sta_id == IWL_INVALID_STATION) {
7268 IWL_DEBUG_MAC80211("leave - %s not in station map.\n",
7269 print_mac(mac, addr));
7270 return -EINVAL;
7271 }
7272 }
Zhu Yib481de92007-09-25 17:54:57 -07007273
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007274 iwl4965_scan_cancel_timeout(priv, 100);
mabbas052c4b92007-10-25 17:15:43 +08007275
Zhu Yib481de92007-09-25 17:54:57 -07007276 switch (cmd) {
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07007277 case SET_KEY:
7278 if (static_key)
7279 ret = iwl4965_set_static_key(priv, key);
7280 else
7281 ret = iwl4965_set_dynamic_key(priv, key, sta_id);
7282
7283 IWL_DEBUG_MAC80211("enable hwcrypto key\n");
Zhu Yib481de92007-09-25 17:54:57 -07007284 break;
7285 case DISABLE_KEY:
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07007286 if (static_key)
7287 ret = iwl4965_remove_static_key(priv);
7288 else
7289 ret = iwl4965_clear_sta_key_info(priv, sta_id);
7290
7291 IWL_DEBUG_MAC80211("disable hwcrypto key\n");
Zhu Yib481de92007-09-25 17:54:57 -07007292 break;
7293 default:
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07007294 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07007295 }
7296
7297 IWL_DEBUG_MAC80211("leave\n");
Zhu Yib481de92007-09-25 17:54:57 -07007298
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07007299 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07007300}
7301
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007302static int iwl4965_mac_conf_tx(struct ieee80211_hw *hw, int queue,
Zhu Yib481de92007-09-25 17:54:57 -07007303 const struct ieee80211_tx_queue_params *params)
7304{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007305 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07007306 unsigned long flags;
7307 int q;
Zhu Yib481de92007-09-25 17:54:57 -07007308
7309 IWL_DEBUG_MAC80211("enter\n");
7310
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007311 if (!iwl4965_is_ready_rf(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07007312 IWL_DEBUG_MAC80211("leave - RF not ready\n");
7313 return -EIO;
7314 }
7315
7316 if (queue >= AC_NUM) {
7317 IWL_DEBUG_MAC80211("leave - queue >= AC_NUM %d\n", queue);
7318 return 0;
7319 }
7320
Zhu Yib481de92007-09-25 17:54:57 -07007321 if (!priv->qos_data.qos_enable) {
7322 priv->qos_data.qos_active = 0;
7323 IWL_DEBUG_MAC80211("leave - qos not enabled\n");
7324 return 0;
7325 }
7326 q = AC_NUM - 1 - queue;
7327
7328 spin_lock_irqsave(&priv->lock, flags);
7329
7330 priv->qos_data.def_qos_parm.ac[q].cw_min = cpu_to_le16(params->cw_min);
7331 priv->qos_data.def_qos_parm.ac[q].cw_max = cpu_to_le16(params->cw_max);
7332 priv->qos_data.def_qos_parm.ac[q].aifsn = params->aifs;
7333 priv->qos_data.def_qos_parm.ac[q].edca_txop =
Johannes Berg3330d7b2008-02-10 16:49:38 +01007334 cpu_to_le16((params->txop * 32));
Zhu Yib481de92007-09-25 17:54:57 -07007335
7336 priv->qos_data.def_qos_parm.ac[q].reserved1 = 0;
7337 priv->qos_data.qos_active = 1;
7338
7339 spin_unlock_irqrestore(&priv->lock, flags);
7340
7341 mutex_lock(&priv->mutex);
7342 if (priv->iw_mode == IEEE80211_IF_TYPE_AP)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007343 iwl4965_activate_qos(priv, 1);
7344 else if (priv->assoc_id && iwl4965_is_associated(priv))
7345 iwl4965_activate_qos(priv, 0);
Zhu Yib481de92007-09-25 17:54:57 -07007346
7347 mutex_unlock(&priv->mutex);
7348
Zhu Yib481de92007-09-25 17:54:57 -07007349 IWL_DEBUG_MAC80211("leave\n");
7350 return 0;
7351}
7352
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007353static int iwl4965_mac_get_tx_stats(struct ieee80211_hw *hw,
Zhu Yib481de92007-09-25 17:54:57 -07007354 struct ieee80211_tx_queue_stats *stats)
7355{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007356 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07007357 int i, avail;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007358 struct iwl4965_tx_queue *txq;
7359 struct iwl4965_queue *q;
Zhu Yib481de92007-09-25 17:54:57 -07007360 unsigned long flags;
7361
7362 IWL_DEBUG_MAC80211("enter\n");
7363
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007364 if (!iwl4965_is_ready_rf(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07007365 IWL_DEBUG_MAC80211("leave - RF not ready\n");
7366 return -EIO;
7367 }
7368
7369 spin_lock_irqsave(&priv->lock, flags);
7370
7371 for (i = 0; i < AC_NUM; i++) {
7372 txq = &priv->txq[i];
7373 q = &txq->q;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007374 avail = iwl4965_queue_space(q);
Zhu Yib481de92007-09-25 17:54:57 -07007375
7376 stats->data[i].len = q->n_window - avail;
7377 stats->data[i].limit = q->n_window - q->high_mark;
7378 stats->data[i].count = q->n_window;
7379
7380 }
7381 spin_unlock_irqrestore(&priv->lock, flags);
7382
7383 IWL_DEBUG_MAC80211("leave\n");
7384
7385 return 0;
7386}
7387
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007388static int iwl4965_mac_get_stats(struct ieee80211_hw *hw,
Zhu Yib481de92007-09-25 17:54:57 -07007389 struct ieee80211_low_level_stats *stats)
7390{
7391 IWL_DEBUG_MAC80211("enter\n");
7392 IWL_DEBUG_MAC80211("leave\n");
7393
7394 return 0;
7395}
7396
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007397static u64 iwl4965_mac_get_tsf(struct ieee80211_hw *hw)
Zhu Yib481de92007-09-25 17:54:57 -07007398{
7399 IWL_DEBUG_MAC80211("enter\n");
7400 IWL_DEBUG_MAC80211("leave\n");
7401
7402 return 0;
7403}
7404
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007405static void iwl4965_mac_reset_tsf(struct ieee80211_hw *hw)
Zhu Yib481de92007-09-25 17:54:57 -07007406{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007407 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07007408 unsigned long flags;
7409
7410 mutex_lock(&priv->mutex);
7411 IWL_DEBUG_MAC80211("enter\n");
7412
7413 priv->lq_mngr.lq_ready = 0;
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08007414#ifdef CONFIG_IWL4965_HT
Zhu Yib481de92007-09-25 17:54:57 -07007415 spin_lock_irqsave(&priv->lock, flags);
Ron Rindjunskyfd105e72007-11-26 16:14:39 +02007416 memset(&priv->current_ht_config, 0, sizeof(struct iwl_ht_info));
Zhu Yib481de92007-09-25 17:54:57 -07007417 spin_unlock_irqrestore(&priv->lock, flags);
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08007418#endif /* CONFIG_IWL4965_HT */
Zhu Yib481de92007-09-25 17:54:57 -07007419
Assaf Kraussbf85ea42008-03-14 10:38:49 -07007420 iwlcore_reset_qos(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007421
7422 cancel_delayed_work(&priv->post_associate);
7423
7424 spin_lock_irqsave(&priv->lock, flags);
7425 priv->assoc_id = 0;
7426 priv->assoc_capability = 0;
7427 priv->call_post_assoc_from_beacon = 0;
7428 priv->assoc_station_added = 0;
7429
7430 /* new association get rid of ibss beacon skb */
7431 if (priv->ibss_beacon)
7432 dev_kfree_skb(priv->ibss_beacon);
7433
7434 priv->ibss_beacon = NULL;
7435
7436 priv->beacon_int = priv->hw->conf.beacon_int;
7437 priv->timestamp1 = 0;
7438 priv->timestamp0 = 0;
7439 if ((priv->iw_mode == IEEE80211_IF_TYPE_STA))
7440 priv->beacon_int = 0;
7441
7442 spin_unlock_irqrestore(&priv->lock, flags);
7443
Mohamed Abbasfde35712007-11-29 11:10:15 +08007444 if (!iwl4965_is_ready_rf(priv)) {
7445 IWL_DEBUG_MAC80211("leave - not ready\n");
7446 mutex_unlock(&priv->mutex);
7447 return;
7448 }
7449
mabbas052c4b92007-10-25 17:15:43 +08007450 /* we are restarting association process
7451 * clear RXON_FILTER_ASSOC_MSK bit
7452 */
7453 if (priv->iw_mode != IEEE80211_IF_TYPE_AP) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007454 iwl4965_scan_cancel_timeout(priv, 100);
mabbas052c4b92007-10-25 17:15:43 +08007455 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007456 iwl4965_commit_rxon(priv);
mabbas052c4b92007-10-25 17:15:43 +08007457 }
7458
Zhu Yib481de92007-09-25 17:54:57 -07007459 /* Per mac80211.h: This is only used in IBSS mode... */
7460 if (priv->iw_mode != IEEE80211_IF_TYPE_IBSS) {
mabbas052c4b92007-10-25 17:15:43 +08007461
Zhu Yib481de92007-09-25 17:54:57 -07007462 IWL_DEBUG_MAC80211("leave - not in IBSS\n");
7463 mutex_unlock(&priv->mutex);
7464 return;
7465 }
7466
Zhu Yib481de92007-09-25 17:54:57 -07007467 priv->only_active_channel = 0;
7468
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007469 iwl4965_set_rate(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007470
7471 mutex_unlock(&priv->mutex);
7472
7473 IWL_DEBUG_MAC80211("leave\n");
Zhu Yib481de92007-09-25 17:54:57 -07007474}
7475
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007476static int iwl4965_mac_beacon_update(struct ieee80211_hw *hw, struct sk_buff *skb,
Zhu Yib481de92007-09-25 17:54:57 -07007477 struct ieee80211_tx_control *control)
7478{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007479 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07007480 unsigned long flags;
7481
7482 mutex_lock(&priv->mutex);
7483 IWL_DEBUG_MAC80211("enter\n");
7484
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007485 if (!iwl4965_is_ready_rf(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07007486 IWL_DEBUG_MAC80211("leave - RF not ready\n");
7487 mutex_unlock(&priv->mutex);
7488 return -EIO;
7489 }
7490
7491 if (priv->iw_mode != IEEE80211_IF_TYPE_IBSS) {
7492 IWL_DEBUG_MAC80211("leave - not IBSS\n");
7493 mutex_unlock(&priv->mutex);
7494 return -EIO;
7495 }
7496
7497 spin_lock_irqsave(&priv->lock, flags);
7498
7499 if (priv->ibss_beacon)
7500 dev_kfree_skb(priv->ibss_beacon);
7501
7502 priv->ibss_beacon = skb;
7503
7504 priv->assoc_id = 0;
7505
7506 IWL_DEBUG_MAC80211("leave\n");
7507 spin_unlock_irqrestore(&priv->lock, flags);
7508
Assaf Kraussbf85ea42008-03-14 10:38:49 -07007509 iwlcore_reset_qos(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007510
7511 queue_work(priv->workqueue, &priv->post_associate.work);
7512
7513 mutex_unlock(&priv->mutex);
7514
7515 return 0;
7516}
7517
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08007518#ifdef CONFIG_IWL4965_HT
Zhu Yib481de92007-09-25 17:54:57 -07007519
Ron Rindjunskyfd105e72007-11-26 16:14:39 +02007520static void iwl4965_ht_info_fill(struct ieee80211_conf *conf,
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007521 struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07007522{
Ron Rindjunskyfd105e72007-11-26 16:14:39 +02007523 struct iwl_ht_info *iwl_conf = &priv->current_ht_config;
7524 struct ieee80211_ht_info *ht_conf = &conf->ht_conf;
7525 struct ieee80211_ht_bss_info *ht_bss_conf = &conf->ht_bss_conf;
Zhu Yib481de92007-09-25 17:54:57 -07007526
7527 IWL_DEBUG_MAC80211("enter: \n");
7528
Ron Rindjunskyfd105e72007-11-26 16:14:39 +02007529 if (!(conf->flags & IEEE80211_CONF_SUPPORT_HT_MODE)) {
7530 iwl_conf->is_ht = 0;
7531 return;
Zhu Yib481de92007-09-25 17:54:57 -07007532 }
7533
Ron Rindjunskyfd105e72007-11-26 16:14:39 +02007534 iwl_conf->is_ht = 1;
7535 priv->ps_mode = (u8)((ht_conf->cap & IEEE80211_HT_CAP_MIMO_PS) >> 2);
Zhu Yib481de92007-09-25 17:54:57 -07007536
Ron Rindjunskyfd105e72007-11-26 16:14:39 +02007537 if (ht_conf->cap & IEEE80211_HT_CAP_SGI_20)
7538 iwl_conf->sgf |= 0x1;
7539 if (ht_conf->cap & IEEE80211_HT_CAP_SGI_40)
7540 iwl_conf->sgf |= 0x2;
Zhu Yib481de92007-09-25 17:54:57 -07007541
Ron Rindjunskyfd105e72007-11-26 16:14:39 +02007542 iwl_conf->is_green_field = !!(ht_conf->cap & IEEE80211_HT_CAP_GRN_FLD);
7543 iwl_conf->max_amsdu_size =
7544 !!(ht_conf->cap & IEEE80211_HT_CAP_MAX_AMSDU);
Guy Cohen134eb5d2008-03-04 18:09:25 -08007545
Ron Rindjunskyfd105e72007-11-26 16:14:39 +02007546 iwl_conf->supported_chan_width =
7547 !!(ht_conf->cap & IEEE80211_HT_CAP_SUP_WIDTH);
Guy Cohen134eb5d2008-03-04 18:09:25 -08007548 iwl_conf->extension_chan_offset =
7549 ht_bss_conf->bss_cap & IEEE80211_HT_IE_CHA_SEC_OFFSET;
7550 /* If no above or below channel supplied disable FAT channel */
7551 if (iwl_conf->extension_chan_offset != IWL_EXT_CHANNEL_OFFSET_ABOVE &&
7552 iwl_conf->extension_chan_offset != IWL_EXT_CHANNEL_OFFSET_BELOW)
7553 iwl_conf->supported_chan_width = 0;
7554
Ron Rindjunskyfd105e72007-11-26 16:14:39 +02007555 iwl_conf->tx_mimo_ps_mode =
7556 (u8)((ht_conf->cap & IEEE80211_HT_CAP_MIMO_PS) >> 2);
7557 memcpy(iwl_conf->supp_mcs_set, ht_conf->supp_mcs_set, 16);
Zhu Yib481de92007-09-25 17:54:57 -07007558
Ron Rindjunskyfd105e72007-11-26 16:14:39 +02007559 iwl_conf->control_channel = ht_bss_conf->primary_channel;
Ron Rindjunskyfd105e72007-11-26 16:14:39 +02007560 iwl_conf->tx_chan_width =
7561 !!(ht_bss_conf->bss_cap & IEEE80211_HT_IE_CHA_WIDTH);
7562 iwl_conf->ht_protection =
7563 ht_bss_conf->bss_op_mode & IEEE80211_HT_IE_HT_PROTECTION;
7564 iwl_conf->non_GF_STA_present =
7565 !!(ht_bss_conf->bss_op_mode & IEEE80211_HT_IE_NON_GF_STA_PRSNT);
Zhu Yib481de92007-09-25 17:54:57 -07007566
Ron Rindjunskyfd105e72007-11-26 16:14:39 +02007567 IWL_DEBUG_MAC80211("control channel %d\n",
7568 iwl_conf->control_channel);
Zhu Yib481de92007-09-25 17:54:57 -07007569 IWL_DEBUG_MAC80211("leave\n");
Zhu Yib481de92007-09-25 17:54:57 -07007570}
7571
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007572static int iwl4965_mac_conf_ht(struct ieee80211_hw *hw,
Ron Rindjunskyfd105e72007-11-26 16:14:39 +02007573 struct ieee80211_conf *conf)
Zhu Yib481de92007-09-25 17:54:57 -07007574{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007575 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07007576
7577 IWL_DEBUG_MAC80211("enter: \n");
7578
Ron Rindjunskyfd105e72007-11-26 16:14:39 +02007579 iwl4965_ht_info_fill(conf, priv);
Zhu Yib481de92007-09-25 17:54:57 -07007580 iwl4965_set_rxon_chain(priv);
7581
7582 if (priv && priv->assoc_id &&
7583 (priv->iw_mode == IEEE80211_IF_TYPE_STA)) {
7584 unsigned long flags;
7585
7586 spin_lock_irqsave(&priv->lock, flags);
7587 if (priv->beacon_int)
7588 queue_work(priv->workqueue, &priv->post_associate.work);
7589 else
7590 priv->call_post_assoc_from_beacon = 1;
7591 spin_unlock_irqrestore(&priv->lock, flags);
7592 }
7593
Ron Rindjunskyfd105e72007-11-26 16:14:39 +02007594 IWL_DEBUG_MAC80211("leave:\n");
7595 return 0;
Zhu Yib481de92007-09-25 17:54:57 -07007596}
7597
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08007598#endif /*CONFIG_IWL4965_HT*/
Zhu Yib481de92007-09-25 17:54:57 -07007599
7600/*****************************************************************************
7601 *
7602 * sysfs attributes
7603 *
7604 *****************************************************************************/
7605
Tomas Winkler0a6857e2008-03-12 16:58:49 -07007606#ifdef CONFIG_IWLWIFI_DEBUG
Zhu Yib481de92007-09-25 17:54:57 -07007607
7608/*
7609 * The following adds a new attribute to the sysfs representation
7610 * of this device driver (i.e. a new file in /sys/bus/pci/drivers/iwl/)
7611 * used for controlling the debug level.
7612 *
7613 * See the level definitions in iwl for details.
7614 */
7615
7616static ssize_t show_debug_level(struct device_driver *d, char *buf)
7617{
Tomas Winkler0a6857e2008-03-12 16:58:49 -07007618 return sprintf(buf, "0x%08X\n", iwl_debug_level);
Zhu Yib481de92007-09-25 17:54:57 -07007619}
7620static ssize_t store_debug_level(struct device_driver *d,
7621 const char *buf, size_t count)
7622{
7623 char *p = (char *)buf;
7624 u32 val;
7625
7626 val = simple_strtoul(p, &p, 0);
7627 if (p == buf)
7628 printk(KERN_INFO DRV_NAME
7629 ": %s is not in hex or decimal form.\n", buf);
7630 else
Tomas Winkler0a6857e2008-03-12 16:58:49 -07007631 iwl_debug_level = val;
Zhu Yib481de92007-09-25 17:54:57 -07007632
7633 return strnlen(buf, count);
7634}
7635
7636static DRIVER_ATTR(debug_level, S_IWUSR | S_IRUGO,
7637 show_debug_level, store_debug_level);
7638
Tomas Winkler0a6857e2008-03-12 16:58:49 -07007639#endif /* CONFIG_IWLWIFI_DEBUG */
Zhu Yib481de92007-09-25 17:54:57 -07007640
7641static ssize_t show_rf_kill(struct device *d,
7642 struct device_attribute *attr, char *buf)
7643{
7644 /*
7645 * 0 - RF kill not enabled
7646 * 1 - SW based RF kill active (sysfs)
7647 * 2 - HW based RF kill active
7648 * 3 - Both HW and SW based RF kill active
7649 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007650 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07007651 int val = (test_bit(STATUS_RF_KILL_SW, &priv->status) ? 0x1 : 0x0) |
7652 (test_bit(STATUS_RF_KILL_HW, &priv->status) ? 0x2 : 0x0);
7653
7654 return sprintf(buf, "%i\n", val);
7655}
7656
7657static ssize_t store_rf_kill(struct device *d,
7658 struct device_attribute *attr,
7659 const char *buf, size_t count)
7660{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007661 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07007662
7663 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007664 iwl4965_radio_kill_sw(priv, buf[0] == '1');
Zhu Yib481de92007-09-25 17:54:57 -07007665 mutex_unlock(&priv->mutex);
7666
7667 return count;
7668}
7669
7670static DEVICE_ATTR(rf_kill, S_IWUSR | S_IRUGO, show_rf_kill, store_rf_kill);
7671
7672static ssize_t show_temperature(struct device *d,
7673 struct device_attribute *attr, char *buf)
7674{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007675 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07007676
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007677 if (!iwl4965_is_alive(priv))
Zhu Yib481de92007-09-25 17:54:57 -07007678 return -EAGAIN;
7679
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007680 return sprintf(buf, "%d\n", iwl4965_hw_get_temperature(priv));
Zhu Yib481de92007-09-25 17:54:57 -07007681}
7682
7683static DEVICE_ATTR(temperature, S_IRUGO, show_temperature, NULL);
7684
7685static ssize_t show_rs_window(struct device *d,
7686 struct device_attribute *attr,
7687 char *buf)
7688{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007689 struct iwl_priv *priv = d->driver_data;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007690 return iwl4965_fill_rs_info(priv->hw, buf, IWL_AP_ID);
Zhu Yib481de92007-09-25 17:54:57 -07007691}
7692static DEVICE_ATTR(rs_window, S_IRUGO, show_rs_window, NULL);
7693
7694static ssize_t show_tx_power(struct device *d,
7695 struct device_attribute *attr, char *buf)
7696{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007697 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07007698 return sprintf(buf, "%d\n", priv->user_txpower_limit);
7699}
7700
7701static ssize_t store_tx_power(struct device *d,
7702 struct device_attribute *attr,
7703 const char *buf, size_t count)
7704{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007705 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07007706 char *p = (char *)buf;
7707 u32 val;
7708
7709 val = simple_strtoul(p, &p, 10);
7710 if (p == buf)
7711 printk(KERN_INFO DRV_NAME
7712 ": %s is not in decimal form.\n", buf);
7713 else
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007714 iwl4965_hw_reg_set_txpower(priv, val);
Zhu Yib481de92007-09-25 17:54:57 -07007715
7716 return count;
7717}
7718
7719static DEVICE_ATTR(tx_power, S_IWUSR | S_IRUGO, show_tx_power, store_tx_power);
7720
7721static ssize_t show_flags(struct device *d,
7722 struct device_attribute *attr, char *buf)
7723{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007724 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07007725
7726 return sprintf(buf, "0x%04X\n", priv->active_rxon.flags);
7727}
7728
7729static ssize_t store_flags(struct device *d,
7730 struct device_attribute *attr,
7731 const char *buf, size_t count)
7732{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007733 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07007734 u32 flags = simple_strtoul(buf, NULL, 0);
7735
7736 mutex_lock(&priv->mutex);
7737 if (le32_to_cpu(priv->staging_rxon.flags) != flags) {
7738 /* Cancel any currently running scans... */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007739 if (iwl4965_scan_cancel_timeout(priv, 100))
Zhu Yib481de92007-09-25 17:54:57 -07007740 IWL_WARNING("Could not cancel scan.\n");
7741 else {
7742 IWL_DEBUG_INFO("Committing rxon.flags = 0x%04X\n",
7743 flags);
7744 priv->staging_rxon.flags = cpu_to_le32(flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007745 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007746 }
7747 }
7748 mutex_unlock(&priv->mutex);
7749
7750 return count;
7751}
7752
7753static DEVICE_ATTR(flags, S_IWUSR | S_IRUGO, show_flags, store_flags);
7754
7755static ssize_t show_filter_flags(struct device *d,
7756 struct device_attribute *attr, char *buf)
7757{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007758 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07007759
7760 return sprintf(buf, "0x%04X\n",
7761 le32_to_cpu(priv->active_rxon.filter_flags));
7762}
7763
7764static ssize_t store_filter_flags(struct device *d,
7765 struct device_attribute *attr,
7766 const char *buf, size_t count)
7767{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007768 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07007769 u32 filter_flags = simple_strtoul(buf, NULL, 0);
7770
7771 mutex_lock(&priv->mutex);
7772 if (le32_to_cpu(priv->staging_rxon.filter_flags) != filter_flags) {
7773 /* Cancel any currently running scans... */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007774 if (iwl4965_scan_cancel_timeout(priv, 100))
Zhu Yib481de92007-09-25 17:54:57 -07007775 IWL_WARNING("Could not cancel scan.\n");
7776 else {
7777 IWL_DEBUG_INFO("Committing rxon.filter_flags = "
7778 "0x%04X\n", filter_flags);
7779 priv->staging_rxon.filter_flags =
7780 cpu_to_le32(filter_flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007781 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007782 }
7783 }
7784 mutex_unlock(&priv->mutex);
7785
7786 return count;
7787}
7788
7789static DEVICE_ATTR(filter_flags, S_IWUSR | S_IRUGO, show_filter_flags,
7790 store_filter_flags);
7791
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08007792#ifdef CONFIG_IWL4965_SPECTRUM_MEASUREMENT
Zhu Yib481de92007-09-25 17:54:57 -07007793
7794static ssize_t show_measurement(struct device *d,
7795 struct device_attribute *attr, char *buf)
7796{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007797 struct iwl_priv *priv = dev_get_drvdata(d);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007798 struct iwl4965_spectrum_notification measure_report;
Zhu Yib481de92007-09-25 17:54:57 -07007799 u32 size = sizeof(measure_report), len = 0, ofs = 0;
7800 u8 *data = (u8 *) & measure_report;
7801 unsigned long flags;
7802
7803 spin_lock_irqsave(&priv->lock, flags);
7804 if (!(priv->measurement_status & MEASUREMENT_READY)) {
7805 spin_unlock_irqrestore(&priv->lock, flags);
7806 return 0;
7807 }
7808 memcpy(&measure_report, &priv->measure_report, size);
7809 priv->measurement_status = 0;
7810 spin_unlock_irqrestore(&priv->lock, flags);
7811
7812 while (size && (PAGE_SIZE - len)) {
7813 hex_dump_to_buffer(data + ofs, size, 16, 1, buf + len,
7814 PAGE_SIZE - len, 1);
7815 len = strlen(buf);
7816 if (PAGE_SIZE - len)
7817 buf[len++] = '\n';
7818
7819 ofs += 16;
7820 size -= min(size, 16U);
7821 }
7822
7823 return len;
7824}
7825
7826static ssize_t store_measurement(struct device *d,
7827 struct device_attribute *attr,
7828 const char *buf, size_t count)
7829{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007830 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07007831 struct ieee80211_measurement_params params = {
7832 .channel = le16_to_cpu(priv->active_rxon.channel),
7833 .start_time = cpu_to_le64(priv->last_tsf),
7834 .duration = cpu_to_le16(1),
7835 };
7836 u8 type = IWL_MEASURE_BASIC;
7837 u8 buffer[32];
7838 u8 channel;
7839
7840 if (count) {
7841 char *p = buffer;
7842 strncpy(buffer, buf, min(sizeof(buffer), count));
7843 channel = simple_strtoul(p, NULL, 0);
7844 if (channel)
7845 params.channel = channel;
7846
7847 p = buffer;
7848 while (*p && *p != ' ')
7849 p++;
7850 if (*p)
7851 type = simple_strtoul(p + 1, NULL, 0);
7852 }
7853
7854 IWL_DEBUG_INFO("Invoking measurement of type %d on "
7855 "channel %d (for '%s')\n", type, params.channel, buf);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007856 iwl4965_get_measurement(priv, &params, type);
Zhu Yib481de92007-09-25 17:54:57 -07007857
7858 return count;
7859}
7860
7861static DEVICE_ATTR(measurement, S_IRUSR | S_IWUSR,
7862 show_measurement, store_measurement);
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08007863#endif /* CONFIG_IWL4965_SPECTRUM_MEASUREMENT */
Zhu Yib481de92007-09-25 17:54:57 -07007864
7865static ssize_t store_retry_rate(struct device *d,
7866 struct device_attribute *attr,
7867 const char *buf, size_t count)
7868{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007869 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07007870
7871 priv->retry_rate = simple_strtoul(buf, NULL, 0);
7872 if (priv->retry_rate <= 0)
7873 priv->retry_rate = 1;
7874
7875 return count;
7876}
7877
7878static ssize_t show_retry_rate(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 return sprintf(buf, "%d", priv->retry_rate);
7883}
7884
7885static DEVICE_ATTR(retry_rate, S_IWUSR | S_IRUSR, show_retry_rate,
7886 store_retry_rate);
7887
7888static ssize_t store_power_level(struct device *d,
7889 struct device_attribute *attr,
7890 const char *buf, size_t count)
7891{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007892 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07007893 int rc;
7894 int mode;
7895
7896 mode = simple_strtoul(buf, NULL, 0);
7897 mutex_lock(&priv->mutex);
7898
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007899 if (!iwl4965_is_ready(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07007900 rc = -EAGAIN;
7901 goto out;
7902 }
7903
7904 if ((mode < 1) || (mode > IWL_POWER_LIMIT) || (mode == IWL_POWER_AC))
7905 mode = IWL_POWER_AC;
7906 else
7907 mode |= IWL_POWER_ENABLED;
7908
7909 if (mode != priv->power_mode) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007910 rc = iwl4965_send_power_mode(priv, IWL_POWER_LEVEL(mode));
Zhu Yib481de92007-09-25 17:54:57 -07007911 if (rc) {
7912 IWL_DEBUG_MAC80211("failed setting power mode.\n");
7913 goto out;
7914 }
7915 priv->power_mode = mode;
7916 }
7917
7918 rc = count;
7919
7920 out:
7921 mutex_unlock(&priv->mutex);
7922 return rc;
7923}
7924
7925#define MAX_WX_STRING 80
7926
7927/* Values are in microsecond */
7928static const s32 timeout_duration[] = {
7929 350000,
7930 250000,
7931 75000,
7932 37000,
7933 25000,
7934};
7935static const s32 period_duration[] = {
7936 400000,
7937 700000,
7938 1000000,
7939 1000000,
7940 1000000
7941};
7942
7943static ssize_t show_power_level(struct device *d,
7944 struct device_attribute *attr, char *buf)
7945{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007946 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07007947 int level = IWL_POWER_LEVEL(priv->power_mode);
7948 char *p = buf;
7949
7950 p += sprintf(p, "%d ", level);
7951 switch (level) {
7952 case IWL_POWER_MODE_CAM:
7953 case IWL_POWER_AC:
7954 p += sprintf(p, "(AC)");
7955 break;
7956 case IWL_POWER_BATTERY:
7957 p += sprintf(p, "(BATTERY)");
7958 break;
7959 default:
7960 p += sprintf(p,
7961 "(Timeout %dms, Period %dms)",
7962 timeout_duration[level - 1] / 1000,
7963 period_duration[level - 1] / 1000);
7964 }
7965
7966 if (!(priv->power_mode & IWL_POWER_ENABLED))
7967 p += sprintf(p, " OFF\n");
7968 else
7969 p += sprintf(p, " \n");
7970
7971 return (p - buf + 1);
7972
7973}
7974
7975static DEVICE_ATTR(power_level, S_IWUSR | S_IRUSR, show_power_level,
7976 store_power_level);
7977
7978static ssize_t show_channels(struct device *d,
7979 struct device_attribute *attr, char *buf)
7980{
Johannes Berg8318d782008-01-24 19:38:38 +01007981 /* all this shit doesn't belong into sysfs anyway */
7982 return 0;
Zhu Yib481de92007-09-25 17:54:57 -07007983}
7984
7985static DEVICE_ATTR(channels, S_IRUSR, show_channels, NULL);
7986
7987static ssize_t show_statistics(struct device *d,
7988 struct device_attribute *attr, char *buf)
7989{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007990 struct iwl_priv *priv = dev_get_drvdata(d);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007991 u32 size = sizeof(struct iwl4965_notif_statistics);
Zhu Yib481de92007-09-25 17:54:57 -07007992 u32 len = 0, ofs = 0;
7993 u8 *data = (u8 *) & priv->statistics;
7994 int rc = 0;
7995
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007996 if (!iwl4965_is_alive(priv))
Zhu Yib481de92007-09-25 17:54:57 -07007997 return -EAGAIN;
7998
7999 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008000 rc = iwl4965_send_statistics_request(priv);
Zhu Yib481de92007-09-25 17:54:57 -07008001 mutex_unlock(&priv->mutex);
8002
8003 if (rc) {
8004 len = sprintf(buf,
8005 "Error sending statistics request: 0x%08X\n", rc);
8006 return len;
8007 }
8008
8009 while (size && (PAGE_SIZE - len)) {
8010 hex_dump_to_buffer(data + ofs, size, 16, 1, buf + len,
8011 PAGE_SIZE - len, 1);
8012 len = strlen(buf);
8013 if (PAGE_SIZE - len)
8014 buf[len++] = '\n';
8015
8016 ofs += 16;
8017 size -= min(size, 16U);
8018 }
8019
8020 return len;
8021}
8022
8023static DEVICE_ATTR(statistics, S_IRUGO, show_statistics, NULL);
8024
8025static ssize_t show_antenna(struct device *d,
8026 struct device_attribute *attr, char *buf)
8027{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07008028 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07008029
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008030 if (!iwl4965_is_alive(priv))
Zhu Yib481de92007-09-25 17:54:57 -07008031 return -EAGAIN;
8032
8033 return sprintf(buf, "%d\n", priv->antenna);
8034}
8035
8036static ssize_t store_antenna(struct device *d,
8037 struct device_attribute *attr,
8038 const char *buf, size_t count)
8039{
8040 int ant;
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07008041 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07008042
8043 if (count == 0)
8044 return 0;
8045
8046 if (sscanf(buf, "%1i", &ant) != 1) {
8047 IWL_DEBUG_INFO("not in hex or decimal form.\n");
8048 return count;
8049 }
8050
8051 if ((ant >= 0) && (ant <= 2)) {
8052 IWL_DEBUG_INFO("Setting antenna select to %d.\n", ant);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008053 priv->antenna = (enum iwl4965_antenna)ant;
Zhu Yib481de92007-09-25 17:54:57 -07008054 } else
8055 IWL_DEBUG_INFO("Bad antenna select value %d.\n", ant);
8056
8057
8058 return count;
8059}
8060
8061static DEVICE_ATTR(antenna, S_IWUSR | S_IRUGO, show_antenna, store_antenna);
8062
8063static ssize_t show_status(struct device *d,
8064 struct device_attribute *attr, char *buf)
8065{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07008066 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008067 if (!iwl4965_is_alive(priv))
Zhu Yib481de92007-09-25 17:54:57 -07008068 return -EAGAIN;
8069 return sprintf(buf, "0x%08x\n", (int)priv->status);
8070}
8071
8072static DEVICE_ATTR(status, S_IRUGO, show_status, NULL);
8073
8074static ssize_t dump_error_log(struct device *d,
8075 struct device_attribute *attr,
8076 const char *buf, size_t count)
8077{
8078 char *p = (char *)buf;
8079
8080 if (p[0] == '1')
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07008081 iwl4965_dump_nic_error_log((struct iwl_priv *)d->driver_data);
Zhu Yib481de92007-09-25 17:54:57 -07008082
8083 return strnlen(buf, count);
8084}
8085
8086static DEVICE_ATTR(dump_errors, S_IWUSR, NULL, dump_error_log);
8087
8088static ssize_t dump_event_log(struct device *d,
8089 struct device_attribute *attr,
8090 const char *buf, size_t count)
8091{
8092 char *p = (char *)buf;
8093
8094 if (p[0] == '1')
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07008095 iwl4965_dump_nic_event_log((struct iwl_priv *)d->driver_data);
Zhu Yib481de92007-09-25 17:54:57 -07008096
8097 return strnlen(buf, count);
8098}
8099
8100static DEVICE_ATTR(dump_events, S_IWUSR, NULL, dump_event_log);
8101
8102/*****************************************************************************
8103 *
8104 * driver setup and teardown
8105 *
8106 *****************************************************************************/
8107
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07008108static void iwl4965_setup_deferred_work(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07008109{
8110 priv->workqueue = create_workqueue(DRV_NAME);
8111
8112 init_waitqueue_head(&priv->wait_command_queue);
8113
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008114 INIT_WORK(&priv->up, iwl4965_bg_up);
8115 INIT_WORK(&priv->restart, iwl4965_bg_restart);
8116 INIT_WORK(&priv->rx_replenish, iwl4965_bg_rx_replenish);
8117 INIT_WORK(&priv->scan_completed, iwl4965_bg_scan_completed);
8118 INIT_WORK(&priv->request_scan, iwl4965_bg_request_scan);
8119 INIT_WORK(&priv->abort_scan, iwl4965_bg_abort_scan);
8120 INIT_WORK(&priv->rf_kill, iwl4965_bg_rf_kill);
8121 INIT_WORK(&priv->beacon_update, iwl4965_bg_beacon_update);
8122 INIT_DELAYED_WORK(&priv->post_associate, iwl4965_bg_post_associate);
8123 INIT_DELAYED_WORK(&priv->init_alive_start, iwl4965_bg_init_alive_start);
8124 INIT_DELAYED_WORK(&priv->alive_start, iwl4965_bg_alive_start);
8125 INIT_DELAYED_WORK(&priv->scan_check, iwl4965_bg_scan_check);
Zhu Yib481de92007-09-25 17:54:57 -07008126
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008127 iwl4965_hw_setup_deferred_work(priv);
Zhu Yib481de92007-09-25 17:54:57 -07008128
8129 tasklet_init(&priv->irq_tasklet, (void (*)(unsigned long))
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008130 iwl4965_irq_tasklet, (unsigned long)priv);
Zhu Yib481de92007-09-25 17:54:57 -07008131}
8132
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07008133static void iwl4965_cancel_deferred_work(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07008134{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008135 iwl4965_hw_cancel_deferred_work(priv);
Zhu Yib481de92007-09-25 17:54:57 -07008136
Joonwoo Park3ae6a052007-11-29 10:43:16 +09008137 cancel_delayed_work_sync(&priv->init_alive_start);
Zhu Yib481de92007-09-25 17:54:57 -07008138 cancel_delayed_work(&priv->scan_check);
8139 cancel_delayed_work(&priv->alive_start);
8140 cancel_delayed_work(&priv->post_associate);
8141 cancel_work_sync(&priv->beacon_update);
8142}
8143
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008144static struct attribute *iwl4965_sysfs_entries[] = {
Zhu Yib481de92007-09-25 17:54:57 -07008145 &dev_attr_antenna.attr,
8146 &dev_attr_channels.attr,
8147 &dev_attr_dump_errors.attr,
8148 &dev_attr_dump_events.attr,
8149 &dev_attr_flags.attr,
8150 &dev_attr_filter_flags.attr,
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08008151#ifdef CONFIG_IWL4965_SPECTRUM_MEASUREMENT
Zhu Yib481de92007-09-25 17:54:57 -07008152 &dev_attr_measurement.attr,
8153#endif
8154 &dev_attr_power_level.attr,
8155 &dev_attr_retry_rate.attr,
8156 &dev_attr_rf_kill.attr,
8157 &dev_attr_rs_window.attr,
8158 &dev_attr_statistics.attr,
8159 &dev_attr_status.attr,
8160 &dev_attr_temperature.attr,
Zhu Yib481de92007-09-25 17:54:57 -07008161 &dev_attr_tx_power.attr,
8162
8163 NULL
8164};
8165
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008166static struct attribute_group iwl4965_attribute_group = {
Zhu Yib481de92007-09-25 17:54:57 -07008167 .name = NULL, /* put in device directory */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008168 .attrs = iwl4965_sysfs_entries,
Zhu Yib481de92007-09-25 17:54:57 -07008169};
8170
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008171static struct ieee80211_ops iwl4965_hw_ops = {
8172 .tx = iwl4965_mac_tx,
8173 .start = iwl4965_mac_start,
8174 .stop = iwl4965_mac_stop,
8175 .add_interface = iwl4965_mac_add_interface,
8176 .remove_interface = iwl4965_mac_remove_interface,
8177 .config = iwl4965_mac_config,
8178 .config_interface = iwl4965_mac_config_interface,
8179 .configure_filter = iwl4965_configure_filter,
8180 .set_key = iwl4965_mac_set_key,
Emmanuel Grumbachab885f82008-03-20 15:06:43 +02008181 .update_tkip_key = iwl4965_mac_update_tkip_key,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008182 .get_stats = iwl4965_mac_get_stats,
8183 .get_tx_stats = iwl4965_mac_get_tx_stats,
8184 .conf_tx = iwl4965_mac_conf_tx,
8185 .get_tsf = iwl4965_mac_get_tsf,
8186 .reset_tsf = iwl4965_mac_reset_tsf,
8187 .beacon_update = iwl4965_mac_beacon_update,
Johannes Berg471b3ef2007-12-28 14:32:58 +01008188 .bss_info_changed = iwl4965_bss_info_changed,
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08008189#ifdef CONFIG_IWL4965_HT
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008190 .conf_ht = iwl4965_mac_conf_ht,
Ron Rindjunsky9ab46172007-12-25 17:00:38 +02008191 .ampdu_action = iwl4965_mac_ampdu_action,
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08008192#endif /* CONFIG_IWL4965_HT */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008193 .hw_scan = iwl4965_mac_hw_scan
Zhu Yib481de92007-09-25 17:54:57 -07008194};
8195
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008196static int iwl4965_pci_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
Zhu Yib481de92007-09-25 17:54:57 -07008197{
8198 int err = 0;
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07008199 struct iwl_priv *priv;
Zhu Yib481de92007-09-25 17:54:57 -07008200 struct ieee80211_hw *hw;
Tomas Winkler82b9a122008-03-04 18:09:30 -08008201 struct iwl_cfg *cfg = (struct iwl_cfg *)(ent->driver_data);
Zhu Yi5a66926a2008-01-14 17:46:18 -08008202 DECLARE_MAC_BUF(mac);
Zhu Yib481de92007-09-25 17:54:57 -07008203
Assaf Krauss316c30d2008-03-14 10:38:46 -07008204 /************************
8205 * 1. Allocating HW data
8206 ************************/
8207
Cahill, Ben M6440adb2007-11-29 11:09:55 +08008208 /* Disabling hardware scan means that mac80211 will perform scans
8209 * "the hard way", rather than using device's scan. */
Assaf Krauss00acbc92008-03-14 10:38:47 -07008210 if (iwl4965_mod_params.disable_hw_scan) {
Zhu Yib481de92007-09-25 17:54:57 -07008211 IWL_DEBUG_INFO("Disabling hw_scan\n");
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008212 iwl4965_hw_ops.hw_scan = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07008213 }
8214
Assaf Krauss1d0a0822008-03-14 10:38:48 -07008215 hw = iwl_alloc_all(cfg, &iwl4965_hw_ops);
8216 if (!hw) {
Zhu Yib481de92007-09-25 17:54:57 -07008217 err = -ENOMEM;
8218 goto out;
8219 }
Assaf Krauss1d0a0822008-03-14 10:38:48 -07008220 priv = hw->priv;
8221 /* At this point both hw and priv are allocated. */
8222
Zhu Yib481de92007-09-25 17:54:57 -07008223 SET_IEEE80211_DEV(hw, &pdev->dev);
8224
8225 IWL_DEBUG_INFO("*** LOAD DRIVER ***\n");
Tomas Winkler82b9a122008-03-04 18:09:30 -08008226 priv->cfg = cfg;
Zhu Yib481de92007-09-25 17:54:57 -07008227 priv->pci_dev = pdev;
Assaf Krauss316c30d2008-03-14 10:38:46 -07008228
Tomas Winkler0a6857e2008-03-12 16:58:49 -07008229#ifdef CONFIG_IWLWIFI_DEBUG
Assaf Krauss00acbc92008-03-14 10:38:47 -07008230 iwl_debug_level = iwl4965_mod_params.debug;
Zhu Yib481de92007-09-25 17:54:57 -07008231 atomic_set(&priv->restrict_refcnt, 0);
8232#endif
Zhu Yib481de92007-09-25 17:54:57 -07008233
Assaf Krauss316c30d2008-03-14 10:38:46 -07008234 /**************************
8235 * 2. Initializing PCI bus
8236 **************************/
8237 if (pci_enable_device(pdev)) {
8238 err = -ENODEV;
8239 goto out_ieee80211_free_hw;
8240 }
8241
8242 pci_set_master(pdev);
8243
8244 err = pci_set_dma_mask(pdev, DMA_32BIT_MASK);
8245 if (!err)
8246 err = pci_set_consistent_dma_mask(pdev, DMA_32BIT_MASK);
8247 if (err) {
8248 printk(KERN_WARNING DRV_NAME
8249 ": No suitable DMA available.\n");
8250 goto out_pci_disable_device;
8251 }
8252
8253 err = pci_request_regions(pdev, DRV_NAME);
8254 if (err)
8255 goto out_pci_disable_device;
8256
8257 pci_set_drvdata(pdev, priv);
8258
8259 /* We disable the RETRY_TIMEOUT register (0x41) to keep
8260 * PCI Tx retries from interfering with C3 CPU state */
8261 pci_write_config_byte(pdev, 0x41, 0x00);
8262
8263 /***********************
8264 * 3. Read REV register
8265 ***********************/
8266 priv->hw_base = pci_iomap(pdev, 0, 0);
8267 if (!priv->hw_base) {
8268 err = -ENODEV;
8269 goto out_pci_release_regions;
8270 }
8271
8272 IWL_DEBUG_INFO("pci_resource_len = 0x%08llx\n",
8273 (unsigned long long) pci_resource_len(pdev, 0));
8274 IWL_DEBUG_INFO("pci_resource_base = %p\n", priv->hw_base);
8275
8276 printk(KERN_INFO DRV_NAME
8277 ": Detected Intel Wireless WiFi Link %s\n", priv->cfg->name);
8278
8279 /*****************
8280 * 4. Read EEPROM
8281 *****************/
8282 /* nic init */
8283 iwl4965_set_bit(priv, CSR_GIO_CHICKEN_BITS,
8284 CSR_GIO_CHICKEN_BITS_REG_BIT_DIS_L0S_EXIT_TIMER);
8285
8286 iwl4965_set_bit(priv, CSR_GP_CNTRL, CSR_GP_CNTRL_REG_FLAG_INIT_DONE);
8287 err = iwl4965_poll_bit(priv, CSR_GP_CNTRL,
8288 CSR_GP_CNTRL_REG_FLAG_MAC_CLOCK_READY,
8289 CSR_GP_CNTRL_REG_FLAG_MAC_CLOCK_READY, 25000);
8290 if (err < 0) {
8291 IWL_DEBUG_INFO("Failed to init the card\n");
8292 goto out_iounmap;
8293 }
8294 /* Read the EEPROM */
8295 err = iwl_eeprom_init(priv);
8296 if (err) {
8297 IWL_ERROR("Unable to init EEPROM\n");
8298 goto out_iounmap;
8299 }
8300 /* MAC Address location in EEPROM same for 3945/4965 */
8301 iwl_eeprom_get_mac(priv, priv->mac_addr);
8302 IWL_DEBUG_INFO("MAC address: %s\n", print_mac(mac, priv->mac_addr));
8303 SET_IEEE80211_PERM_ADDR(priv->hw, priv->mac_addr);
8304
8305 /************************
8306 * 5. Setup HW constants
8307 ************************/
8308 /* Device-specific setup */
8309 if (iwl4965_hw_set_hw_setting(priv)) {
8310 IWL_ERROR("failed to set hw settings\n");
8311 goto out_iounmap;
8312 }
8313
8314 /*******************
8315 * 6. Setup hw/priv
8316 *******************/
Zhu Yib481de92007-09-25 17:54:57 -07008317
Assaf Kraussbf85ea42008-03-14 10:38:49 -07008318 err = iwl_setup(priv);
8319 if (err)
Assaf Krauss316c30d2008-03-14 10:38:46 -07008320 goto out_unset_hw_settings;
Assaf Kraussbf85ea42008-03-14 10:38:49 -07008321 /* At this point both hw and priv are initialized. */
Assaf Krauss316c30d2008-03-14 10:38:46 -07008322
8323 /**********************************
8324 * 7. Initialize module parameters
8325 **********************************/
8326
8327 /* Disable radio (SW RF KILL) via parameter when loading driver */
Assaf Krauss00acbc92008-03-14 10:38:47 -07008328 if (iwl4965_mod_params.disable) {
Assaf Krauss316c30d2008-03-14 10:38:46 -07008329 set_bit(STATUS_RF_KILL_SW, &priv->status);
8330 IWL_DEBUG_INFO("Radio disabled.\n");
8331 }
8332
Assaf Krauss00acbc92008-03-14 10:38:47 -07008333 if (iwl4965_mod_params.enable_qos)
Assaf Krauss316c30d2008-03-14 10:38:46 -07008334 priv->qos_data.qos_enable = 1;
8335
8336 /********************
8337 * 8. Setup services
8338 ********************/
8339 iwl4965_disable_interrupts(priv);
8340
8341 err = sysfs_create_group(&pdev->dev.kobj, &iwl4965_attribute_group);
8342 if (err) {
8343 IWL_ERROR("failed to create sysfs device attributes\n");
Assaf Kraussbf85ea42008-03-14 10:38:49 -07008344 goto out_unset_hw_settings;
Assaf Krauss316c30d2008-03-14 10:38:46 -07008345 }
8346
8347 err = iwl_dbgfs_register(priv, DRV_NAME);
8348 if (err) {
8349 IWL_ERROR("failed to create debugfs files\n");
8350 goto out_remove_sysfs;
8351 }
8352
8353 iwl4965_setup_deferred_work(priv);
8354 iwl4965_setup_rx_handlers(priv);
8355
8356 /********************
8357 * 9. Conclude
8358 ********************/
Zhu Yi5a66926a2008-01-14 17:46:18 -08008359 pci_save_state(pdev);
8360 pci_disable_device(pdev);
Zhu Yib481de92007-09-25 17:54:57 -07008361
8362 return 0;
8363
Assaf Krauss316c30d2008-03-14 10:38:46 -07008364 out_remove_sysfs:
8365 sysfs_remove_group(&pdev->dev.kobj, &iwl4965_attribute_group);
Assaf Krauss316c30d2008-03-14 10:38:46 -07008366 out_unset_hw_settings:
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008367 iwl4965_unset_hw_setting(priv);
Zhu Yib481de92007-09-25 17:54:57 -07008368 out_iounmap:
8369 pci_iounmap(pdev, priv->hw_base);
8370 out_pci_release_regions:
8371 pci_release_regions(pdev);
Assaf Krauss316c30d2008-03-14 10:38:46 -07008372 pci_set_drvdata(pdev, NULL);
Zhu Yib481de92007-09-25 17:54:57 -07008373 out_pci_disable_device:
8374 pci_disable_device(pdev);
Zhu Yib481de92007-09-25 17:54:57 -07008375 out_ieee80211_free_hw:
8376 ieee80211_free_hw(priv->hw);
8377 out:
8378 return err;
8379}
8380
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008381static void iwl4965_pci_remove(struct pci_dev *pdev)
Zhu Yib481de92007-09-25 17:54:57 -07008382{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07008383 struct iwl_priv *priv = pci_get_drvdata(pdev);
Zhu Yib481de92007-09-25 17:54:57 -07008384 struct list_head *p, *q;
8385 int i;
8386
8387 if (!priv)
8388 return;
8389
8390 IWL_DEBUG_INFO("*** UNLOAD DRIVER ***\n");
8391
Zhu Yib481de92007-09-25 17:54:57 -07008392 set_bit(STATUS_EXIT_PENDING, &priv->status);
Zhu Yib24d22b2007-12-19 13:59:52 +08008393
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008394 iwl4965_down(priv);
Zhu Yib481de92007-09-25 17:54:57 -07008395
8396 /* Free MAC hash list for ADHOC */
8397 for (i = 0; i < IWL_IBSS_MAC_HASH_SIZE; i++) {
8398 list_for_each_safe(p, q, &priv->ibss_mac_hash[i]) {
8399 list_del(p);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008400 kfree(list_entry(p, struct iwl4965_ibss_seq, list));
Zhu Yib481de92007-09-25 17:54:57 -07008401 }
8402 }
8403
Tomas Winkler712b6cf2008-03-12 16:58:52 -07008404 iwl_dbgfs_unregister(priv);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008405 sysfs_remove_group(&pdev->dev.kobj, &iwl4965_attribute_group);
Zhu Yib481de92007-09-25 17:54:57 -07008406
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008407 iwl4965_dealloc_ucode_pci(priv);
Zhu Yib481de92007-09-25 17:54:57 -07008408
8409 if (priv->rxq.bd)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008410 iwl4965_rx_queue_free(priv, &priv->rxq);
8411 iwl4965_hw_txq_ctx_free(priv);
Zhu Yib481de92007-09-25 17:54:57 -07008412
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008413 iwl4965_unset_hw_setting(priv);
Assaf Kraussbf85ea42008-03-14 10:38:49 -07008414 iwlcore_clear_stations_table(priv);
Zhu Yib481de92007-09-25 17:54:57 -07008415
8416 if (priv->mac80211_registered) {
8417 ieee80211_unregister_hw(priv->hw);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008418 iwl4965_rate_control_unregister(priv->hw);
Zhu Yib481de92007-09-25 17:54:57 -07008419 }
8420
Mohamed Abbas948c1712007-10-25 17:15:45 +08008421 /*netif_stop_queue(dev); */
8422 flush_workqueue(priv->workqueue);
8423
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008424 /* ieee80211_unregister_hw calls iwl4965_mac_stop, which flushes
Zhu Yib481de92007-09-25 17:54:57 -07008425 * priv->workqueue... so we can't take down the workqueue
8426 * until now... */
8427 destroy_workqueue(priv->workqueue);
8428 priv->workqueue = NULL;
8429
Zhu Yib481de92007-09-25 17:54:57 -07008430 pci_iounmap(pdev, priv->hw_base);
8431 pci_release_regions(pdev);
8432 pci_disable_device(pdev);
8433 pci_set_drvdata(pdev, NULL);
8434
Assaf Kraussbf85ea42008-03-14 10:38:49 -07008435 iwl_free_channel_map(priv);
Reinette Chatre849e0dc2008-01-23 10:15:18 -08008436 iwl4965_free_geos(priv);
Zhu Yib481de92007-09-25 17:54:57 -07008437
8438 if (priv->ibss_beacon)
8439 dev_kfree_skb(priv->ibss_beacon);
8440
8441 ieee80211_free_hw(priv->hw);
8442}
8443
8444#ifdef CONFIG_PM
8445
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008446static int iwl4965_pci_suspend(struct pci_dev *pdev, pm_message_t state)
Zhu Yib481de92007-09-25 17:54:57 -07008447{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07008448 struct iwl_priv *priv = pci_get_drvdata(pdev);
Zhu Yib481de92007-09-25 17:54:57 -07008449
Zhu Yie655b9f2008-01-24 02:19:38 -08008450 if (priv->is_open) {
8451 set_bit(STATUS_IN_SUSPEND, &priv->status);
8452 iwl4965_mac_stop(priv->hw);
8453 priv->is_open = 1;
8454 }
Zhu Yib481de92007-09-25 17:54:57 -07008455
Zhu Yib481de92007-09-25 17:54:57 -07008456 pci_set_power_state(pdev, PCI_D3hot);
8457
Zhu Yib481de92007-09-25 17:54:57 -07008458 return 0;
8459}
8460
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008461static int iwl4965_pci_resume(struct pci_dev *pdev)
Zhu Yib481de92007-09-25 17:54:57 -07008462{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07008463 struct iwl_priv *priv = pci_get_drvdata(pdev);
Zhu Yib481de92007-09-25 17:54:57 -07008464
Zhu Yib481de92007-09-25 17:54:57 -07008465 pci_set_power_state(pdev, PCI_D0);
Zhu Yib481de92007-09-25 17:54:57 -07008466
Zhu Yie655b9f2008-01-24 02:19:38 -08008467 if (priv->is_open)
8468 iwl4965_mac_start(priv->hw);
Zhu Yib481de92007-09-25 17:54:57 -07008469
Zhu Yie655b9f2008-01-24 02:19:38 -08008470 clear_bit(STATUS_IN_SUSPEND, &priv->status);
Zhu Yib481de92007-09-25 17:54:57 -07008471 return 0;
8472}
8473
8474#endif /* CONFIG_PM */
8475
8476/*****************************************************************************
8477 *
8478 * driver and module entry point
8479 *
8480 *****************************************************************************/
8481
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008482static struct pci_driver iwl4965_driver = {
Zhu Yib481de92007-09-25 17:54:57 -07008483 .name = DRV_NAME,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008484 .id_table = iwl4965_hw_card_ids,
8485 .probe = iwl4965_pci_probe,
8486 .remove = __devexit_p(iwl4965_pci_remove),
Zhu Yib481de92007-09-25 17:54:57 -07008487#ifdef CONFIG_PM
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008488 .suspend = iwl4965_pci_suspend,
8489 .resume = iwl4965_pci_resume,
Zhu Yib481de92007-09-25 17:54:57 -07008490#endif
8491};
8492
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008493static int __init iwl4965_init(void)
Zhu Yib481de92007-09-25 17:54:57 -07008494{
8495
8496 int ret;
8497 printk(KERN_INFO DRV_NAME ": " DRV_DESCRIPTION ", " DRV_VERSION "\n");
8498 printk(KERN_INFO DRV_NAME ": " DRV_COPYRIGHT "\n");
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008499 ret = pci_register_driver(&iwl4965_driver);
Zhu Yib481de92007-09-25 17:54:57 -07008500 if (ret) {
8501 IWL_ERROR("Unable to initialize PCI module\n");
8502 return ret;
8503 }
Tomas Winkler0a6857e2008-03-12 16:58:49 -07008504#ifdef CONFIG_IWLWIFI_DEBUG
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008505 ret = driver_create_file(&iwl4965_driver.driver, &driver_attr_debug_level);
Zhu Yib481de92007-09-25 17:54:57 -07008506 if (ret) {
8507 IWL_ERROR("Unable to create driver sysfs file\n");
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008508 pci_unregister_driver(&iwl4965_driver);
Zhu Yib481de92007-09-25 17:54:57 -07008509 return ret;
8510 }
8511#endif
8512
8513 return ret;
8514}
8515
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008516static void __exit iwl4965_exit(void)
Zhu Yib481de92007-09-25 17:54:57 -07008517{
Tomas Winkler0a6857e2008-03-12 16:58:49 -07008518#ifdef CONFIG_IWLWIFI_DEBUG
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008519 driver_remove_file(&iwl4965_driver.driver, &driver_attr_debug_level);
Zhu Yib481de92007-09-25 17:54:57 -07008520#endif
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008521 pci_unregister_driver(&iwl4965_driver);
Zhu Yib481de92007-09-25 17:54:57 -07008522}
8523
Assaf Krauss00acbc92008-03-14 10:38:47 -07008524module_param_named(antenna, iwl4965_mod_params.antenna, int, 0444);
Zhu Yib481de92007-09-25 17:54:57 -07008525MODULE_PARM_DESC(antenna, "select antenna (1=Main, 2=Aux, default 0 [both])");
Assaf Krauss00acbc92008-03-14 10:38:47 -07008526module_param_named(disable, iwl4965_mod_params.disable, int, 0444);
Zhu Yib481de92007-09-25 17:54:57 -07008527MODULE_PARM_DESC(disable, "manually disable the radio (default 0 [radio on])");
Assaf Krauss00acbc92008-03-14 10:38:47 -07008528module_param_named(hwcrypto, iwl4965_mod_params.hw_crypto, int, 0444);
Zhu Yib481de92007-09-25 17:54:57 -07008529MODULE_PARM_DESC(hwcrypto,
8530 "using hardware crypto engine (default 0 [software])\n");
Assaf Krauss00acbc92008-03-14 10:38:47 -07008531module_param_named(debug, iwl4965_mod_params.debug, int, 0444);
Zhu Yib481de92007-09-25 17:54:57 -07008532MODULE_PARM_DESC(debug, "debug output mask");
Assaf Krauss00acbc92008-03-14 10:38:47 -07008533module_param_named(
8534 disable_hw_scan, iwl4965_mod_params.disable_hw_scan, int, 0444);
Zhu Yib481de92007-09-25 17:54:57 -07008535MODULE_PARM_DESC(disable_hw_scan, "disable hardware scanning (default 0)");
8536
Assaf Krauss00acbc92008-03-14 10:38:47 -07008537module_param_named(queues_num, iwl4965_mod_params.num_of_queues, int, 0444);
Zhu Yib481de92007-09-25 17:54:57 -07008538MODULE_PARM_DESC(queues_num, "number of hw queues.");
8539
8540/* QoS */
Assaf Krauss00acbc92008-03-14 10:38:47 -07008541module_param_named(qos_enable, iwl4965_mod_params.enable_qos, int, 0444);
Zhu Yib481de92007-09-25 17:54:57 -07008542MODULE_PARM_DESC(qos_enable, "enable all QoS functionality");
Assaf Krauss00acbc92008-03-14 10:38:47 -07008543module_param_named(amsdu_size_8K, iwl4965_mod_params.amsdu_size_8K, int, 0444);
Ron Rindjunsky9ee1ba42007-11-26 16:14:42 +02008544MODULE_PARM_DESC(amsdu_size_8K, "enable 8K amsdu size");
Zhu Yib481de92007-09-25 17:54:57 -07008545
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008546module_exit(iwl4965_exit);
8547module_init(iwl4965_init);