blob: 9f51d3a61f365a14755827b8b30682a33c7be0e0 [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 Winkler3e0d4cb2008-04-24 11:55:38 -070049#include "iwl-dev.h"
Tomas Winklerfee12472008-04-03 16:05:21 -070050#include "iwl-core.h"
Tomas Winkler3395f6e2008-03-25 16:33:37 -070051#include "iwl-io.h"
Zhu Yib481de92007-09-25 17:54:57 -070052#include "iwl-helpers.h"
Emmanuel Grumbach6974e362008-04-14 21:16:06 -070053#include "iwl-sta.h"
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -070054#include "iwl-calib.h"
Zhu Yib481de92007-09-25 17:54:57 -070055
Tomas Winklerc79dd5b2008-03-12 16:58:50 -070056static int iwl4965_tx_queue_update_write_ptr(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -080057 struct iwl4965_tx_queue *txq);
Christoph Hellwig416e1432007-10-25 17:15:49 +080058
Zhu Yib481de92007-09-25 17:54:57 -070059/******************************************************************************
60 *
61 * module boiler plate
62 *
63 ******************************************************************************/
64
Zhu Yib481de92007-09-25 17:54:57 -070065/*
66 * module name, copyright, version, etc.
67 * NOTE: DRV_NAME is defined in iwlwifi.h for use by iwl-debug.h and printk
68 */
69
70#define DRV_DESCRIPTION "Intel(R) Wireless WiFi Link 4965AGN driver for Linux"
71
Tomas Winkler0a6857e2008-03-12 16:58:49 -070072#ifdef CONFIG_IWLWIFI_DEBUG
Zhu Yib481de92007-09-25 17:54:57 -070073#define VD "d"
74#else
75#define VD
76#endif
77
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +080078#ifdef CONFIG_IWL4965_SPECTRUM_MEASUREMENT
Zhu Yib481de92007-09-25 17:54:57 -070079#define VS "s"
80#else
81#define VS
82#endif
83
Tomas Winklerdf48c322008-03-06 10:40:19 -080084#define DRV_VERSION IWLWIFI_VERSION VD VS
Zhu Yib481de92007-09-25 17:54:57 -070085
Zhu Yib481de92007-09-25 17:54:57 -070086
87MODULE_DESCRIPTION(DRV_DESCRIPTION);
88MODULE_VERSION(DRV_VERSION);
89MODULE_AUTHOR(DRV_COPYRIGHT);
90MODULE_LICENSE("GPL");
91
92__le16 *ieee80211_get_qos_ctrl(struct ieee80211_hdr *hdr)
93{
94 u16 fc = le16_to_cpu(hdr->frame_control);
95 int hdr_len = ieee80211_get_hdrlen(fc);
96
97 if ((fc & 0x00cc) == (IEEE80211_STYPE_QOS_DATA | IEEE80211_FTYPE_DATA))
98 return (__le16 *) ((u8 *) hdr + hdr_len - QOS_CONTROL_LEN);
99 return NULL;
100}
101
Emmanuel Grumbachd1141df2008-04-21 15:42:00 -0700102static const struct ieee80211_supported_band *iwl_get_hw_mode(
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700103 struct iwl_priv *priv, enum ieee80211_band band)
Zhu Yib481de92007-09-25 17:54:57 -0700104{
Johannes Berg8318d782008-01-24 19:38:38 +0100105 return priv->hw->wiphy->bands[band];
Zhu Yib481de92007-09-25 17:54:57 -0700106}
107
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800108static int iwl4965_is_empty_essid(const char *essid, int essid_len)
Zhu Yib481de92007-09-25 17:54:57 -0700109{
110 /* Single white space is for Linksys APs */
111 if (essid_len == 1 && essid[0] == ' ')
112 return 1;
113
114 /* Otherwise, if the entire essid is 0, we assume it is hidden */
115 while (essid_len) {
116 essid_len--;
117 if (essid[essid_len] != '\0')
118 return 0;
119 }
120
121 return 1;
122}
123
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800124static const char *iwl4965_escape_essid(const char *essid, u8 essid_len)
Zhu Yib481de92007-09-25 17:54:57 -0700125{
126 static char escaped[IW_ESSID_MAX_SIZE * 2 + 1];
127 const char *s = essid;
128 char *d = escaped;
129
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800130 if (iwl4965_is_empty_essid(essid, essid_len)) {
Zhu Yib481de92007-09-25 17:54:57 -0700131 memcpy(escaped, "<hidden>", sizeof("<hidden>"));
132 return escaped;
133 }
134
135 essid_len = min(essid_len, (u8) IW_ESSID_MAX_SIZE);
136 while (essid_len--) {
137 if (*s == '\0') {
138 *d++ = '\\';
139 *d++ = '0';
140 s++;
141 } else
142 *d++ = *s++;
143 }
144 *d = '\0';
145 return escaped;
146}
147
Ester Kummerbf403db2008-05-05 10:22:40 +0800148
Zhu Yib481de92007-09-25 17:54:57 -0700149/*************** DMA-QUEUE-GENERAL-FUNCTIONS *****
150 * DMA services
151 *
152 * Theory of operation
153 *
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800154 * A Tx or Rx queue resides in host DRAM, and is comprised of a circular buffer
155 * of buffer descriptors, each of which points to one or more data buffers for
156 * the device to read from or fill. Driver and device exchange status of each
157 * queue via "read" and "write" pointers. Driver keeps minimum of 2 empty
158 * entries in each circular buffer, to protect against confusing empty and full
159 * queue states.
160 *
161 * The device reads or writes the data in the queues via the device's several
162 * DMA/FIFO channels. Each queue is mapped to a single DMA channel.
Zhu Yib481de92007-09-25 17:54:57 -0700163 *
164 * For Tx queue, there are low mark and high mark limits. If, after queuing
165 * the packet for Tx, free space become < low mark, Tx queue stopped. When
166 * reclaiming packets (on 'tx done IRQ), if free space become > high mark,
167 * Tx queue resumed.
168 *
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800169 * The 4965 operates with up to 17 queues: One receive queue, one transmit
170 * queue (#4) for sending commands to the device firmware, and 15 other
171 * Tx queues that may be mapped to prioritized Tx DMA/FIFO channels.
Ben Cahille3851442007-11-29 11:10:07 +0800172 *
173 * See more detailed info in iwl-4965-hw.h.
Zhu Yib481de92007-09-25 17:54:57 -0700174 ***************************************************/
175
Ron Rindjunskyfe01b472008-01-28 14:07:24 +0200176int iwl4965_queue_space(const struct iwl4965_queue *q)
Zhu Yib481de92007-09-25 17:54:57 -0700177{
Tomas Winklerfc4b6852007-10-25 17:15:24 +0800178 int s = q->read_ptr - q->write_ptr;
Zhu Yib481de92007-09-25 17:54:57 -0700179
Tomas Winklerfc4b6852007-10-25 17:15:24 +0800180 if (q->read_ptr > q->write_ptr)
Zhu Yib481de92007-09-25 17:54:57 -0700181 s -= q->n_bd;
182
183 if (s <= 0)
184 s += q->n_window;
185 /* keep some reserve to not confuse empty and full situations */
186 s -= 2;
187 if (s < 0)
188 s = 0;
189 return s;
190}
191
Zhu Yib481de92007-09-25 17:54:57 -0700192
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800193static inline int x2_queue_used(const struct iwl4965_queue *q, int i)
Zhu Yib481de92007-09-25 17:54:57 -0700194{
Tomas Winklerfc4b6852007-10-25 17:15:24 +0800195 return q->write_ptr > q->read_ptr ?
196 (i >= q->read_ptr && i < q->write_ptr) :
197 !(i < q->read_ptr && i >= q->write_ptr);
Zhu Yib481de92007-09-25 17:54:57 -0700198}
199
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800200static inline u8 get_cmd_index(struct iwl4965_queue *q, u32 index, int is_huge)
Zhu Yib481de92007-09-25 17:54:57 -0700201{
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800202 /* This is for scan command, the big buffer at end of command array */
Zhu Yib481de92007-09-25 17:54:57 -0700203 if (is_huge)
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800204 return q->n_window; /* must be power of 2 */
Zhu Yib481de92007-09-25 17:54:57 -0700205
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800206 /* Otherwise, use normal size buffers */
Zhu Yib481de92007-09-25 17:54:57 -0700207 return index & (q->n_window - 1);
208}
209
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800210/**
211 * iwl4965_queue_init - Initialize queue's high/low-water and read/write indexes
212 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700213static int iwl4965_queue_init(struct iwl_priv *priv, struct iwl4965_queue *q,
Zhu Yib481de92007-09-25 17:54:57 -0700214 int count, int slots_num, u32 id)
215{
216 q->n_bd = count;
217 q->n_window = slots_num;
218 q->id = id;
219
Tomas Winklerc54b6792008-03-06 17:36:53 -0800220 /* count must be power-of-two size, otherwise iwl_queue_inc_wrap
221 * and iwl_queue_dec_wrap are broken. */
Zhu Yib481de92007-09-25 17:54:57 -0700222 BUG_ON(!is_power_of_2(count));
223
224 /* slots_num must be power-of-two size, otherwise
225 * get_cmd_index is broken. */
226 BUG_ON(!is_power_of_2(slots_num));
227
228 q->low_mark = q->n_window / 4;
229 if (q->low_mark < 4)
230 q->low_mark = 4;
231
232 q->high_mark = q->n_window / 8;
233 if (q->high_mark < 2)
234 q->high_mark = 2;
235
Tomas Winklerfc4b6852007-10-25 17:15:24 +0800236 q->write_ptr = q->read_ptr = 0;
Zhu Yib481de92007-09-25 17:54:57 -0700237
238 return 0;
239}
240
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800241/**
242 * iwl4965_tx_queue_alloc - Alloc driver data and TFD CB for one Tx/cmd queue
243 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700244static int iwl4965_tx_queue_alloc(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800245 struct iwl4965_tx_queue *txq, u32 id)
Zhu Yib481de92007-09-25 17:54:57 -0700246{
247 struct pci_dev *dev = priv->pci_dev;
248
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800249 /* Driver private data, only for Tx (not command) queues,
250 * not shared with device. */
Zhu Yib481de92007-09-25 17:54:57 -0700251 if (id != IWL_CMD_QUEUE_NUM) {
252 txq->txb = kmalloc(sizeof(txq->txb[0]) *
253 TFD_QUEUE_SIZE_MAX, GFP_KERNEL);
254 if (!txq->txb) {
Ian Schram01ebd062007-10-25 17:15:22 +0800255 IWL_ERROR("kmalloc for auxiliary BD "
Zhu Yib481de92007-09-25 17:54:57 -0700256 "structures failed\n");
257 goto error;
258 }
259 } else
260 txq->txb = NULL;
261
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800262 /* Circular buffer of transmit frame descriptors (TFDs),
263 * shared with device */
Zhu Yib481de92007-09-25 17:54:57 -0700264 txq->bd = pci_alloc_consistent(dev,
265 sizeof(txq->bd[0]) * TFD_QUEUE_SIZE_MAX,
266 &txq->q.dma_addr);
267
268 if (!txq->bd) {
269 IWL_ERROR("pci_alloc_consistent(%zd) failed\n",
270 sizeof(txq->bd[0]) * TFD_QUEUE_SIZE_MAX);
271 goto error;
272 }
273 txq->q.id = id;
274
275 return 0;
276
277 error:
278 if (txq->txb) {
279 kfree(txq->txb);
280 txq->txb = NULL;
281 }
282
283 return -ENOMEM;
284}
285
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +0800286/**
287 * iwl4965_tx_queue_init - Allocate and initialize one tx/cmd queue
288 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700289int iwl4965_tx_queue_init(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800290 struct iwl4965_tx_queue *txq, int slots_num, u32 txq_id)
Zhu Yib481de92007-09-25 17:54:57 -0700291{
292 struct pci_dev *dev = priv->pci_dev;
293 int len;
294 int rc = 0;
295
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +0800296 /*
297 * Alloc buffer array for commands (Tx or other types of commands).
298 * For the command queue (#4), allocate command space + one big
299 * command for scan, since scan command is very huge; the system will
300 * not have two scans at the same time, so only one is needed.
Tomas Winklerbb542442007-12-05 20:59:59 +0200301 * For normal Tx queues (all other queues), no super-size command
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +0800302 * space is needed.
303 */
Tomas Winkler857485c2008-03-21 13:53:44 -0700304 len = sizeof(struct iwl_cmd) * slots_num;
Zhu Yib481de92007-09-25 17:54:57 -0700305 if (txq_id == IWL_CMD_QUEUE_NUM)
306 len += IWL_MAX_SCAN_SIZE;
307 txq->cmd = pci_alloc_consistent(dev, len, &txq->dma_addr_cmd);
308 if (!txq->cmd)
309 return -ENOMEM;
310
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +0800311 /* Alloc driver data array and TFD circular buffer */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800312 rc = iwl4965_tx_queue_alloc(priv, txq, txq_id);
Zhu Yib481de92007-09-25 17:54:57 -0700313 if (rc) {
314 pci_free_consistent(dev, len, txq->cmd, txq->dma_addr_cmd);
315
316 return -ENOMEM;
317 }
318 txq->need_update = 0;
319
320 /* TFD_QUEUE_SIZE_MAX must be power-of-two size, otherwise
Tomas Winklerc54b6792008-03-06 17:36:53 -0800321 * iwl_queue_inc_wrap and iwl_queue_dec_wrap are broken. */
Zhu Yib481de92007-09-25 17:54:57 -0700322 BUILD_BUG_ON(TFD_QUEUE_SIZE_MAX & (TFD_QUEUE_SIZE_MAX - 1));
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +0800323
324 /* Initialize queue's high/low-water marks, and head/tail indexes */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800325 iwl4965_queue_init(priv, &txq->q, TFD_QUEUE_SIZE_MAX, slots_num, txq_id);
Zhu Yib481de92007-09-25 17:54:57 -0700326
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +0800327 /* Tell device where to find queue */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800328 iwl4965_hw_tx_queue_init(priv, txq);
Zhu Yib481de92007-09-25 17:54:57 -0700329
330 return 0;
331}
332
333/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800334 * iwl4965_tx_queue_free - Deallocate DMA queue.
Zhu Yib481de92007-09-25 17:54:57 -0700335 * @txq: Transmit queue to deallocate.
336 *
337 * Empty queue by removing and destroying all BD's.
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800338 * Free all buffers.
339 * 0-fill, but do not free "txq" descriptor structure.
Zhu Yib481de92007-09-25 17:54:57 -0700340 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700341void iwl4965_tx_queue_free(struct iwl_priv *priv, struct iwl4965_tx_queue *txq)
Zhu Yib481de92007-09-25 17:54:57 -0700342{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800343 struct iwl4965_queue *q = &txq->q;
Zhu Yib481de92007-09-25 17:54:57 -0700344 struct pci_dev *dev = priv->pci_dev;
345 int len;
346
347 if (q->n_bd == 0)
348 return;
349
350 /* first, empty all BD's */
Tomas Winklerfc4b6852007-10-25 17:15:24 +0800351 for (; q->write_ptr != q->read_ptr;
Tomas Winklerc54b6792008-03-06 17:36:53 -0800352 q->read_ptr = iwl_queue_inc_wrap(q->read_ptr, q->n_bd))
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800353 iwl4965_hw_txq_free_tfd(priv, txq);
Zhu Yib481de92007-09-25 17:54:57 -0700354
Tomas Winkler857485c2008-03-21 13:53:44 -0700355 len = sizeof(struct iwl_cmd) * q->n_window;
Zhu Yib481de92007-09-25 17:54:57 -0700356 if (q->id == IWL_CMD_QUEUE_NUM)
357 len += IWL_MAX_SCAN_SIZE;
358
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800359 /* De-alloc array of command/tx buffers */
Zhu Yib481de92007-09-25 17:54:57 -0700360 pci_free_consistent(dev, len, txq->cmd, txq->dma_addr_cmd);
361
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800362 /* De-alloc circular buffer of TFDs */
Zhu Yib481de92007-09-25 17:54:57 -0700363 if (txq->q.n_bd)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800364 pci_free_consistent(dev, sizeof(struct iwl4965_tfd_frame) *
Zhu Yib481de92007-09-25 17:54:57 -0700365 txq->q.n_bd, txq->bd, txq->q.dma_addr);
366
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800367 /* De-alloc array of per-TFD driver data */
Zhu Yib481de92007-09-25 17:54:57 -0700368 if (txq->txb) {
369 kfree(txq->txb);
370 txq->txb = NULL;
371 }
372
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800373 /* 0-fill queue descriptor structure */
Zhu Yib481de92007-09-25 17:54:57 -0700374 memset(txq, 0, sizeof(*txq));
375}
376
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800377const u8 iwl4965_broadcast_addr[ETH_ALEN] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
Zhu Yib481de92007-09-25 17:54:57 -0700378
379/*************** STATION TABLE MANAGEMENT ****
Ben Cahill9fbab512007-11-29 11:09:47 +0800380 * mac80211 should be examined to determine if sta_info is duplicating
Zhu Yib481de92007-09-25 17:54:57 -0700381 * the functionality provided here
382 */
383
384/**************************************************************/
385
Ian Schram01ebd062007-10-25 17:15:22 +0800386#if 0 /* temporary disable till we add real remove station */
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800387/**
388 * iwl4965_remove_station - Remove driver's knowledge of station.
389 *
390 * NOTE: This does not remove station from device's station table.
391 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700392static u8 iwl4965_remove_station(struct iwl_priv *priv, const u8 *addr, int is_ap)
Zhu Yib481de92007-09-25 17:54:57 -0700393{
394 int index = IWL_INVALID_STATION;
395 int i;
396 unsigned long flags;
397
398 spin_lock_irqsave(&priv->sta_lock, flags);
399
400 if (is_ap)
401 index = IWL_AP_ID;
402 else if (is_broadcast_ether_addr(addr))
Tomas Winkler5425e492008-04-15 16:01:38 -0700403 index = priv->hw_params.bcast_sta_id;
Zhu Yib481de92007-09-25 17:54:57 -0700404 else
Tomas Winkler5425e492008-04-15 16:01:38 -0700405 for (i = IWL_STA_ID; i < priv->hw_params.max_stations; i++)
Zhu Yib481de92007-09-25 17:54:57 -0700406 if (priv->stations[i].used &&
407 !compare_ether_addr(priv->stations[i].sta.sta.addr,
408 addr)) {
409 index = i;
410 break;
411 }
412
413 if (unlikely(index == IWL_INVALID_STATION))
414 goto out;
415
416 if (priv->stations[index].used) {
417 priv->stations[index].used = 0;
418 priv->num_stations--;
419 }
420
421 BUG_ON(priv->num_stations < 0);
422
423out:
424 spin_unlock_irqrestore(&priv->sta_lock, flags);
425 return 0;
426}
Zhu Yi556f8db2007-09-27 11:27:33 +0800427#endif
Zhu Yib481de92007-09-25 17:54:57 -0700428
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800429/**
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800430 * iwl4965_add_station_flags - Add station to tables in driver and device
431 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700432u8 iwl4965_add_station_flags(struct iwl_priv *priv, const u8 *addr,
Ron Rindjunsky67d62032007-11-26 16:14:40 +0200433 int is_ap, u8 flags, void *ht_data)
Zhu Yib481de92007-09-25 17:54:57 -0700434{
435 int i;
436 int index = IWL_INVALID_STATION;
Tomas Winkler6def9762008-05-05 10:22:31 +0800437 struct iwl_station_entry *station;
Zhu Yib481de92007-09-25 17:54:57 -0700438 unsigned long flags_spin;
Joe Perches0795af52007-10-03 17:59:30 -0700439 DECLARE_MAC_BUF(mac);
Zhu Yib481de92007-09-25 17:54:57 -0700440
441 spin_lock_irqsave(&priv->sta_lock, flags_spin);
442 if (is_ap)
443 index = IWL_AP_ID;
444 else if (is_broadcast_ether_addr(addr))
Tomas Winkler5425e492008-04-15 16:01:38 -0700445 index = priv->hw_params.bcast_sta_id;
Zhu Yib481de92007-09-25 17:54:57 -0700446 else
Tomas Winkler5425e492008-04-15 16:01:38 -0700447 for (i = IWL_STA_ID; i < priv->hw_params.max_stations; i++) {
Zhu Yib481de92007-09-25 17:54:57 -0700448 if (!compare_ether_addr(priv->stations[i].sta.sta.addr,
449 addr)) {
450 index = i;
451 break;
452 }
453
454 if (!priv->stations[i].used &&
455 index == IWL_INVALID_STATION)
456 index = i;
457 }
458
459
Ben Cahill9fbab512007-11-29 11:09:47 +0800460 /* These two conditions have the same outcome, but keep them separate
461 since they have different meanings */
Zhu Yib481de92007-09-25 17:54:57 -0700462 if (unlikely(index == IWL_INVALID_STATION)) {
463 spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
464 return index;
465 }
466
467 if (priv->stations[index].used &&
468 !compare_ether_addr(priv->stations[index].sta.sta.addr, addr)) {
469 spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
470 return index;
471 }
472
473
Joe Perches0795af52007-10-03 17:59:30 -0700474 IWL_DEBUG_ASSOC("Add STA ID %d: %s\n", index, print_mac(mac, addr));
Zhu Yib481de92007-09-25 17:54:57 -0700475 station = &priv->stations[index];
476 station->used = 1;
477 priv->num_stations++;
478
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800479 /* Set up the REPLY_ADD_STA command to send to device */
Tomas Winkler133636d2008-05-05 10:22:34 +0800480 memset(&station->sta, 0, sizeof(struct iwl_addsta_cmd));
Zhu Yib481de92007-09-25 17:54:57 -0700481 memcpy(station->sta.sta.addr, addr, ETH_ALEN);
482 station->sta.mode = 0;
483 station->sta.sta.sta_id = index;
484 station->sta.station_flags = 0;
485
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +0800486#ifdef CONFIG_IWL4965_HT
Zhu Yib481de92007-09-25 17:54:57 -0700487 /* BCAST station and IBSS stations do not work in HT mode */
Tomas Winkler5425e492008-04-15 16:01:38 -0700488 if (index != priv->hw_params.bcast_sta_id &&
Zhu Yib481de92007-09-25 17:54:57 -0700489 priv->iw_mode != IEEE80211_IF_TYPE_IBSS)
Ron Rindjunsky67d62032007-11-26 16:14:40 +0200490 iwl4965_set_ht_add_station(priv, index,
491 (struct ieee80211_ht_info *) ht_data);
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +0800492#endif /*CONFIG_IWL4965_HT*/
Zhu Yib481de92007-09-25 17:54:57 -0700493
494 spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800495
496 /* Add station to device's station table */
Tomas Winkler133636d2008-05-05 10:22:34 +0800497 iwl_send_add_sta(priv, &station->sta, flags);
Zhu Yib481de92007-09-25 17:54:57 -0700498 return index;
499
500}
501
Zhu Yib481de92007-09-25 17:54:57 -0700502
Zhu Yib481de92007-09-25 17:54:57 -0700503
504/*************** HOST COMMAND QUEUE FUNCTIONS *****/
505
Zhu Yib481de92007-09-25 17:54:57 -0700506/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800507 * iwl4965_enqueue_hcmd - enqueue a uCode command
Zhu Yib481de92007-09-25 17:54:57 -0700508 * @priv: device private data point
509 * @cmd: a point to the ucode command structure
510 *
511 * The function returns < 0 values to indicate the operation is
512 * failed. On success, it turns the index (> 0) of command in the
513 * command queue.
514 */
Tomas Winkler857485c2008-03-21 13:53:44 -0700515int iwl4965_enqueue_hcmd(struct iwl_priv *priv, struct iwl_host_cmd *cmd)
Zhu Yib481de92007-09-25 17:54:57 -0700516{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800517 struct iwl4965_tx_queue *txq = &priv->txq[IWL_CMD_QUEUE_NUM];
518 struct iwl4965_queue *q = &txq->q;
519 struct iwl4965_tfd_frame *tfd;
Zhu Yib481de92007-09-25 17:54:57 -0700520 u32 *control_flags;
Tomas Winkler857485c2008-03-21 13:53:44 -0700521 struct iwl_cmd *out_cmd;
Zhu Yib481de92007-09-25 17:54:57 -0700522 u32 idx;
523 u16 fix_size = (u16)(cmd->len + sizeof(out_cmd->hdr));
524 dma_addr_t phys_addr;
525 int ret;
526 unsigned long flags;
527
528 /* If any of the command structures end up being larger than
529 * the TFD_MAX_PAYLOAD_SIZE, and it sent as a 'small' command then
530 * we will need to increase the size of the TFD entries */
531 BUG_ON((fix_size > TFD_MAX_PAYLOAD_SIZE) &&
532 !(cmd->meta.flags & CMD_SIZE_HUGE));
533
Tomas Winklerfee12472008-04-03 16:05:21 -0700534 if (iwl_is_rfkill(priv)) {
Gregory Greenmanc342a1b2008-02-06 11:20:40 -0800535 IWL_DEBUG_INFO("Not sending command - RF KILL");
536 return -EIO;
537 }
538
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800539 if (iwl4965_queue_space(q) < ((cmd->meta.flags & CMD_ASYNC) ? 2 : 1)) {
Zhu Yib481de92007-09-25 17:54:57 -0700540 IWL_ERROR("No space for Tx\n");
541 return -ENOSPC;
542 }
543
544 spin_lock_irqsave(&priv->hcmd_lock, flags);
545
Tomas Winklerfc4b6852007-10-25 17:15:24 +0800546 tfd = &txq->bd[q->write_ptr];
Zhu Yib481de92007-09-25 17:54:57 -0700547 memset(tfd, 0, sizeof(*tfd));
548
549 control_flags = (u32 *) tfd;
550
Tomas Winklerfc4b6852007-10-25 17:15:24 +0800551 idx = get_cmd_index(q, q->write_ptr, cmd->meta.flags & CMD_SIZE_HUGE);
Zhu Yib481de92007-09-25 17:54:57 -0700552 out_cmd = &txq->cmd[idx];
553
554 out_cmd->hdr.cmd = cmd->id;
555 memcpy(&out_cmd->meta, &cmd->meta, sizeof(cmd->meta));
556 memcpy(&out_cmd->cmd.payload, cmd->data, cmd->len);
557
558 /* At this point, the out_cmd now has all of the incoming cmd
559 * information */
560
561 out_cmd->hdr.flags = 0;
562 out_cmd->hdr.sequence = cpu_to_le16(QUEUE_TO_SEQ(IWL_CMD_QUEUE_NUM) |
Tomas Winklerfc4b6852007-10-25 17:15:24 +0800563 INDEX_TO_SEQ(q->write_ptr));
Zhu Yib481de92007-09-25 17:54:57 -0700564 if (out_cmd->meta.flags & CMD_SIZE_HUGE)
565 out_cmd->hdr.sequence |= cpu_to_le16(SEQ_HUGE_FRAME);
566
567 phys_addr = txq->dma_addr_cmd + sizeof(txq->cmd[0]) * idx +
Tomas Winkler857485c2008-03-21 13:53:44 -0700568 offsetof(struct iwl_cmd, hdr);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800569 iwl4965_hw_txq_attach_buf_to_tfd(priv, tfd, phys_addr, fix_size);
Zhu Yib481de92007-09-25 17:54:57 -0700570
571 IWL_DEBUG_HC("Sending command %s (#%x), seq: 0x%04X, "
572 "%d bytes at %d[%d]:%d\n",
573 get_cmd_string(out_cmd->hdr.cmd),
574 out_cmd->hdr.cmd, le16_to_cpu(out_cmd->hdr.sequence),
Tomas Winklerfc4b6852007-10-25 17:15:24 +0800575 fix_size, q->write_ptr, idx, IWL_CMD_QUEUE_NUM);
Zhu Yib481de92007-09-25 17:54:57 -0700576
577 txq->need_update = 1;
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800578
579 /* Set up entry in queue's byte count circular buffer */
Tomas Winklere2a722e2008-04-14 21:16:10 -0700580 priv->cfg->ops->lib->txq_update_byte_cnt_tbl(priv, txq, 0);
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800581
582 /* Increment and update queue's write index */
Tomas Winklerc54b6792008-03-06 17:36:53 -0800583 q->write_ptr = iwl_queue_inc_wrap(q->write_ptr, q->n_bd);
Tomas Winklere2a722e2008-04-14 21:16:10 -0700584 ret = iwl4965_tx_queue_update_write_ptr(priv, txq);
Zhu Yib481de92007-09-25 17:54:57 -0700585
586 spin_unlock_irqrestore(&priv->hcmd_lock, flags);
587 return ret ? ret : idx;
588}
589
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -0700590static void iwl4965_set_rxon_hwcrypto(struct iwl_priv *priv, int hw_decrypt)
591{
592 struct iwl4965_rxon_cmd *rxon = &priv->staging_rxon;
593
594 if (hw_decrypt)
595 rxon->filter_flags &= ~RXON_FILTER_DIS_DECRYPT_MSK;
596 else
597 rxon->filter_flags |= RXON_FILTER_DIS_DECRYPT_MSK;
598
599}
600
Zhu Yib481de92007-09-25 17:54:57 -0700601/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800602 * iwl4965_rxon_add_station - add station into station table.
Zhu Yib481de92007-09-25 17:54:57 -0700603 *
604 * there is only one AP station with id= IWL_AP_ID
Ben Cahill9fbab512007-11-29 11:09:47 +0800605 * NOTE: mutex must be held before calling this fnction
606 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700607static int iwl4965_rxon_add_station(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -0700608 const u8 *addr, int is_ap)
609{
Zhu Yi556f8db2007-09-27 11:27:33 +0800610 u8 sta_id;
Zhu Yib481de92007-09-25 17:54:57 -0700611
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800612 /* Add station to device's station table */
Ron Rindjunsky67d62032007-11-26 16:14:40 +0200613#ifdef CONFIG_IWL4965_HT
614 struct ieee80211_conf *conf = &priv->hw->conf;
615 struct ieee80211_ht_info *cur_ht_config = &conf->ht_conf;
616
617 if ((is_ap) &&
618 (conf->flags & IEEE80211_CONF_SUPPORT_HT_MODE) &&
619 (priv->iw_mode == IEEE80211_IF_TYPE_STA))
620 sta_id = iwl4965_add_station_flags(priv, addr, is_ap,
621 0, cur_ht_config);
622 else
623#endif /* CONFIG_IWL4965_HT */
624 sta_id = iwl4965_add_station_flags(priv, addr, is_ap,
625 0, NULL);
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800626
627 /* Set up default rate scaling table in device's station table */
Zhu Yib481de92007-09-25 17:54:57 -0700628 iwl4965_add_station(priv, addr, is_ap);
629
Zhu Yi556f8db2007-09-27 11:27:33 +0800630 return sta_id;
Zhu Yib481de92007-09-25 17:54:57 -0700631}
632
633/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800634 * iwl4965_check_rxon_cmd - validate RXON structure is valid
Zhu Yib481de92007-09-25 17:54:57 -0700635 *
636 * NOTE: This is really only useful during development and can eventually
637 * be #ifdef'd out once the driver is stable and folks aren't actively
638 * making changes
639 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800640static int iwl4965_check_rxon_cmd(struct iwl4965_rxon_cmd *rxon)
Zhu Yib481de92007-09-25 17:54:57 -0700641{
642 int error = 0;
643 int counter = 1;
644
645 if (rxon->flags & RXON_FLG_BAND_24G_MSK) {
646 error |= le32_to_cpu(rxon->flags &
647 (RXON_FLG_TGJ_NARROW_BAND_MSK |
648 RXON_FLG_RADAR_DETECT_MSK));
649 if (error)
650 IWL_WARNING("check 24G fields %d | %d\n",
651 counter++, error);
652 } else {
653 error |= (rxon->flags & RXON_FLG_SHORT_SLOT_MSK) ?
654 0 : le32_to_cpu(RXON_FLG_SHORT_SLOT_MSK);
655 if (error)
656 IWL_WARNING("check 52 fields %d | %d\n",
657 counter++, error);
658 error |= le32_to_cpu(rxon->flags & RXON_FLG_CCK_MSK);
659 if (error)
660 IWL_WARNING("check 52 CCK %d | %d\n",
661 counter++, error);
662 }
663 error |= (rxon->node_addr[0] | rxon->bssid_addr[0]) & 0x1;
664 if (error)
665 IWL_WARNING("check mac addr %d | %d\n", counter++, error);
666
667 /* make sure basic rates 6Mbps and 1Mbps are supported */
668 error |= (((rxon->ofdm_basic_rates & IWL_RATE_6M_MASK) == 0) &&
669 ((rxon->cck_basic_rates & IWL_RATE_1M_MASK) == 0));
670 if (error)
671 IWL_WARNING("check basic rate %d | %d\n", counter++, error);
672
673 error |= (le16_to_cpu(rxon->assoc_id) > 2007);
674 if (error)
675 IWL_WARNING("check assoc id %d | %d\n", counter++, error);
676
677 error |= ((rxon->flags & (RXON_FLG_CCK_MSK | RXON_FLG_SHORT_SLOT_MSK))
678 == (RXON_FLG_CCK_MSK | RXON_FLG_SHORT_SLOT_MSK));
679 if (error)
680 IWL_WARNING("check CCK and short slot %d | %d\n",
681 counter++, error);
682
683 error |= ((rxon->flags & (RXON_FLG_CCK_MSK | RXON_FLG_AUTO_DETECT_MSK))
684 == (RXON_FLG_CCK_MSK | RXON_FLG_AUTO_DETECT_MSK));
685 if (error)
686 IWL_WARNING("check CCK & auto detect %d | %d\n",
687 counter++, error);
688
689 error |= ((rxon->flags & (RXON_FLG_AUTO_DETECT_MSK |
690 RXON_FLG_TGG_PROTECT_MSK)) == RXON_FLG_TGG_PROTECT_MSK);
691 if (error)
692 IWL_WARNING("check TGG and auto detect %d | %d\n",
693 counter++, error);
694
695 if (error)
696 IWL_WARNING("Tuning to channel %d\n",
697 le16_to_cpu(rxon->channel));
698
699 if (error) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800700 IWL_ERROR("Not a valid iwl4965_rxon_assoc_cmd field values\n");
Zhu Yib481de92007-09-25 17:54:57 -0700701 return -1;
702 }
703 return 0;
704}
705
706/**
Ben Cahill9fbab512007-11-29 11:09:47 +0800707 * iwl4965_full_rxon_required - check if full RXON (vs RXON_ASSOC) cmd is needed
Ian Schram01ebd062007-10-25 17:15:22 +0800708 * @priv: staging_rxon is compared to active_rxon
Zhu Yib481de92007-09-25 17:54:57 -0700709 *
Ben Cahill9fbab512007-11-29 11:09:47 +0800710 * If the RXON structure is changing enough to require a new tune,
711 * or is clearing the RXON_FILTER_ASSOC_MSK, then return 1 to indicate that
712 * a new tune (full RXON command, rather than RXON_ASSOC cmd) is required.
Zhu Yib481de92007-09-25 17:54:57 -0700713 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700714static int iwl4965_full_rxon_required(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700715{
716
717 /* These items are only settable from the full RXON command */
718 if (!(priv->active_rxon.filter_flags & RXON_FILTER_ASSOC_MSK) ||
719 compare_ether_addr(priv->staging_rxon.bssid_addr,
720 priv->active_rxon.bssid_addr) ||
721 compare_ether_addr(priv->staging_rxon.node_addr,
722 priv->active_rxon.node_addr) ||
723 compare_ether_addr(priv->staging_rxon.wlap_bssid_addr,
724 priv->active_rxon.wlap_bssid_addr) ||
725 (priv->staging_rxon.dev_type != priv->active_rxon.dev_type) ||
726 (priv->staging_rxon.channel != priv->active_rxon.channel) ||
727 (priv->staging_rxon.air_propagation !=
728 priv->active_rxon.air_propagation) ||
729 (priv->staging_rxon.ofdm_ht_single_stream_basic_rates !=
730 priv->active_rxon.ofdm_ht_single_stream_basic_rates) ||
731 (priv->staging_rxon.ofdm_ht_dual_stream_basic_rates !=
732 priv->active_rxon.ofdm_ht_dual_stream_basic_rates) ||
733 (priv->staging_rxon.rx_chain != priv->active_rxon.rx_chain) ||
734 (priv->staging_rxon.assoc_id != priv->active_rxon.assoc_id))
735 return 1;
736
737 /* flags, filter_flags, ofdm_basic_rates, and cck_basic_rates can
738 * be updated with the RXON_ASSOC command -- however only some
739 * flag transitions are allowed using RXON_ASSOC */
740
741 /* Check if we are not switching bands */
742 if ((priv->staging_rxon.flags & RXON_FLG_BAND_24G_MSK) !=
743 (priv->active_rxon.flags & RXON_FLG_BAND_24G_MSK))
744 return 1;
745
746 /* Check if we are switching association toggle */
747 if ((priv->staging_rxon.filter_flags & RXON_FILTER_ASSOC_MSK) !=
748 (priv->active_rxon.filter_flags & RXON_FILTER_ASSOC_MSK))
749 return 1;
750
751 return 0;
752}
753
Zhu Yib481de92007-09-25 17:54:57 -0700754/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800755 * iwl4965_commit_rxon - commit staging_rxon to hardware
Zhu Yib481de92007-09-25 17:54:57 -0700756 *
Ian Schram01ebd062007-10-25 17:15:22 +0800757 * The RXON command in staging_rxon is committed to the hardware and
Zhu Yib481de92007-09-25 17:54:57 -0700758 * the active_rxon structure is updated with the new data. This
759 * function correctly transitions out of the RXON_ASSOC_MSK state if
760 * a HW tune is required based on the RXON structure changes.
761 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700762static int iwl4965_commit_rxon(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700763{
764 /* cast away the const for active_rxon in this function */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800765 struct iwl4965_rxon_cmd *active_rxon = (void *)&priv->active_rxon;
Joe Perches0795af52007-10-03 17:59:30 -0700766 DECLARE_MAC_BUF(mac);
Zhu Yib481de92007-09-25 17:54:57 -0700767 int rc = 0;
768
Tomas Winklerfee12472008-04-03 16:05:21 -0700769 if (!iwl_is_alive(priv))
Zhu Yib481de92007-09-25 17:54:57 -0700770 return -1;
771
772 /* always get timestamp with Rx frame */
773 priv->staging_rxon.flags |= RXON_FLG_TSF2HOST_MSK;
774
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800775 rc = iwl4965_check_rxon_cmd(&priv->staging_rxon);
Zhu Yib481de92007-09-25 17:54:57 -0700776 if (rc) {
777 IWL_ERROR("Invalid RXON configuration. Not committing.\n");
778 return -EINVAL;
779 }
780
781 /* If we don't need to send a full RXON, we can use
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800782 * iwl4965_rxon_assoc_cmd which is used to reconfigure filter
Zhu Yib481de92007-09-25 17:54:57 -0700783 * and other flags for the current radio configuration. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800784 if (!iwl4965_full_rxon_required(priv)) {
Tomas Winkler7e8c5192008-04-15 16:01:43 -0700785 rc = iwl_send_rxon_assoc(priv);
Zhu Yib481de92007-09-25 17:54:57 -0700786 if (rc) {
787 IWL_ERROR("Error setting RXON_ASSOC "
788 "configuration (%d).\n", rc);
789 return rc;
790 }
791
792 memcpy(active_rxon, &priv->staging_rxon, sizeof(*active_rxon));
793
794 return 0;
795 }
796
797 /* station table will be cleared */
798 priv->assoc_station_added = 0;
799
Zhu Yib481de92007-09-25 17:54:57 -0700800 /* If we are currently associated and the new config requires
801 * an RXON_ASSOC and the new config wants the associated mask enabled,
802 * we must clear the associated from the active configuration
803 * before we apply the new config */
Tomas Winkler3109ece2008-03-28 16:33:35 -0700804 if (iwl_is_associated(priv) &&
Zhu Yib481de92007-09-25 17:54:57 -0700805 (priv->staging_rxon.filter_flags & RXON_FILTER_ASSOC_MSK)) {
806 IWL_DEBUG_INFO("Toggling associated bit on current RXON\n");
807 active_rxon->filter_flags &= ~RXON_FILTER_ASSOC_MSK;
808
Tomas Winkler857485c2008-03-21 13:53:44 -0700809 rc = iwl_send_cmd_pdu(priv, REPLY_RXON,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800810 sizeof(struct iwl4965_rxon_cmd),
Zhu Yib481de92007-09-25 17:54:57 -0700811 &priv->active_rxon);
812
813 /* If the mask clearing failed then we set
814 * active_rxon back to what it was previously */
815 if (rc) {
816 active_rxon->filter_flags |= RXON_FILTER_ASSOC_MSK;
817 IWL_ERROR("Error clearing ASSOC_MSK on current "
818 "configuration (%d).\n", rc);
819 return rc;
820 }
Zhu Yib481de92007-09-25 17:54:57 -0700821 }
822
823 IWL_DEBUG_INFO("Sending RXON\n"
824 "* with%s RXON_FILTER_ASSOC_MSK\n"
825 "* channel = %d\n"
Joe Perches0795af52007-10-03 17:59:30 -0700826 "* bssid = %s\n",
Zhu Yib481de92007-09-25 17:54:57 -0700827 ((priv->staging_rxon.filter_flags &
828 RXON_FILTER_ASSOC_MSK) ? "" : "out"),
829 le16_to_cpu(priv->staging_rxon.channel),
Joe Perches0795af52007-10-03 17:59:30 -0700830 print_mac(mac, priv->staging_rxon.bssid_addr));
Zhu Yib481de92007-09-25 17:54:57 -0700831
Ron Rindjunsky099b40b2008-04-21 15:41:53 -0700832 iwl4965_set_rxon_hwcrypto(priv, !priv->hw_params.sw_crypto);
Zhu Yib481de92007-09-25 17:54:57 -0700833 /* Apply the new configuration */
Tomas Winkler857485c2008-03-21 13:53:44 -0700834 rc = iwl_send_cmd_pdu(priv, REPLY_RXON,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800835 sizeof(struct iwl4965_rxon_cmd), &priv->staging_rxon);
Zhu Yib481de92007-09-25 17:54:57 -0700836 if (rc) {
837 IWL_ERROR("Error setting new configuration (%d).\n", rc);
838 return rc;
839 }
840
Assaf Kraussbf85ea42008-03-14 10:38:49 -0700841 iwlcore_clear_stations_table(priv);
Zhu Yi556f8db2007-09-27 11:27:33 +0800842
Zhu Yib481de92007-09-25 17:54:57 -0700843 if (!priv->error_recovering)
844 priv->start_calib = 0;
845
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -0700846 iwl_init_sensitivity(priv);
Zhu Yib481de92007-09-25 17:54:57 -0700847
848 memcpy(active_rxon, &priv->staging_rxon, sizeof(*active_rxon));
849
850 /* If we issue a new RXON command which required a tune then we must
851 * send a new TXPOWER command or we won't be able to Tx any frames */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800852 rc = iwl4965_hw_reg_send_txpower(priv);
Zhu Yib481de92007-09-25 17:54:57 -0700853 if (rc) {
854 IWL_ERROR("Error setting Tx power (%d).\n", rc);
855 return rc;
856 }
857
858 /* Add the broadcast address so we can send broadcast frames */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800859 if (iwl4965_rxon_add_station(priv, iwl4965_broadcast_addr, 0) ==
Zhu Yib481de92007-09-25 17:54:57 -0700860 IWL_INVALID_STATION) {
861 IWL_ERROR("Error adding BROADCAST address for transmit.\n");
862 return -EIO;
863 }
864
865 /* If we have set the ASSOC_MSK and we are in BSS mode then
866 * add the IWL_AP_ID to the station rate table */
Tomas Winkler3109ece2008-03-28 16:33:35 -0700867 if (iwl_is_associated(priv) &&
Zhu Yib481de92007-09-25 17:54:57 -0700868 (priv->iw_mode == IEEE80211_IF_TYPE_STA)) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800869 if (iwl4965_rxon_add_station(priv, priv->active_rxon.bssid_addr, 1)
Zhu Yib481de92007-09-25 17:54:57 -0700870 == IWL_INVALID_STATION) {
871 IWL_ERROR("Error adding AP address for transmit.\n");
872 return -EIO;
873 }
874 priv->assoc_station_added = 1;
Emmanuel Grumbach6974e362008-04-14 21:16:06 -0700875 if (priv->default_wep_key &&
876 iwl_send_static_wepkey_cmd(priv, 0))
877 IWL_ERROR("Could not send WEP static key.\n");
Zhu Yib481de92007-09-25 17:54:57 -0700878 }
879
880 return 0;
881}
882
Mohamed Abbas5da4b552008-04-21 15:41:51 -0700883void iwl4965_update_chain_flags(struct iwl_priv *priv)
884{
885
Ron Rindjunskyc7de35c2008-04-23 17:15:05 -0700886 iwl_set_rxon_chain(priv);
Mohamed Abbas5da4b552008-04-21 15:41:51 -0700887 iwl4965_commit_rxon(priv);
888}
889
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700890static int iwl4965_send_bt_config(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700891{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800892 struct iwl4965_bt_cmd bt_cmd = {
Zhu Yib481de92007-09-25 17:54:57 -0700893 .flags = 3,
894 .lead_time = 0xAA,
895 .max_kill = 1,
896 .kill_ack_mask = 0,
897 .kill_cts_mask = 0,
898 };
899
Tomas Winkler857485c2008-03-21 13:53:44 -0700900 return iwl_send_cmd_pdu(priv, REPLY_BT_CONFIG,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800901 sizeof(struct iwl4965_bt_cmd), &bt_cmd);
Zhu Yib481de92007-09-25 17:54:57 -0700902}
903
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700904static int iwl4965_send_scan_abort(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700905{
Tomas Winklerdb11d632008-05-05 10:22:33 +0800906 int ret = 0;
907 struct iwl_rx_packet *res;
Tomas Winkler857485c2008-03-21 13:53:44 -0700908 struct iwl_host_cmd cmd = {
Zhu Yib481de92007-09-25 17:54:57 -0700909 .id = REPLY_SCAN_ABORT_CMD,
910 .meta.flags = CMD_WANT_SKB,
911 };
912
913 /* If there isn't a scan actively going on in the hardware
914 * then we are in between scan bands and not actually
915 * actively scanning, so don't send the abort command */
916 if (!test_bit(STATUS_SCAN_HW, &priv->status)) {
917 clear_bit(STATUS_SCAN_ABORTING, &priv->status);
918 return 0;
919 }
920
Tomas Winklerdb11d632008-05-05 10:22:33 +0800921 ret = iwl_send_cmd_sync(priv, &cmd);
922 if (ret) {
Zhu Yib481de92007-09-25 17:54:57 -0700923 clear_bit(STATUS_SCAN_ABORTING, &priv->status);
Tomas Winklerdb11d632008-05-05 10:22:33 +0800924 return ret;
Zhu Yib481de92007-09-25 17:54:57 -0700925 }
926
Tomas Winklerdb11d632008-05-05 10:22:33 +0800927 res = (struct iwl_rx_packet *)cmd.meta.u.skb->data;
Zhu Yib481de92007-09-25 17:54:57 -0700928 if (res->u.status != CAN_ABORT_STATUS) {
929 /* The scan abort will return 1 for success or
930 * 2 for "failure". A failure condition can be
931 * due to simply not being in an active scan which
932 * can occur if we send the scan abort before we
933 * the microcode has notified us that a scan is
934 * completed. */
935 IWL_DEBUG_INFO("SCAN_ABORT returned %d.\n", res->u.status);
936 clear_bit(STATUS_SCAN_ABORTING, &priv->status);
937 clear_bit(STATUS_SCAN_HW, &priv->status);
938 }
939
940 dev_kfree_skb_any(cmd.meta.u.skb);
941
Tomas Winklerdb11d632008-05-05 10:22:33 +0800942 return ret;
Zhu Yib481de92007-09-25 17:54:57 -0700943}
944
Zhu Yib481de92007-09-25 17:54:57 -0700945/*
946 * CARD_STATE_CMD
947 *
Ben Cahill9fbab512007-11-29 11:09:47 +0800948 * Use: Sets the device's internal card state to enable, disable, or halt
Zhu Yib481de92007-09-25 17:54:57 -0700949 *
950 * When in the 'enable' state the card operates as normal.
951 * When in the 'disable' state, the card enters into a low power mode.
952 * When in the 'halt' state, the card is shut down and must be fully
953 * restarted to come back on.
954 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700955static int iwl4965_send_card_state(struct iwl_priv *priv, u32 flags, u8 meta_flag)
Zhu Yib481de92007-09-25 17:54:57 -0700956{
Tomas Winkler857485c2008-03-21 13:53:44 -0700957 struct iwl_host_cmd cmd = {
Zhu Yib481de92007-09-25 17:54:57 -0700958 .id = REPLY_CARD_STATE_CMD,
959 .len = sizeof(u32),
960 .data = &flags,
961 .meta.flags = meta_flag,
962 };
963
Tomas Winkler857485c2008-03-21 13:53:44 -0700964 return iwl_send_cmd(priv, &cmd);
Zhu Yib481de92007-09-25 17:54:57 -0700965}
966
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700967static void iwl4965_clear_free_frames(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700968{
969 struct list_head *element;
970
971 IWL_DEBUG_INFO("%d frames on pre-allocated heap on clear.\n",
972 priv->frames_count);
973
974 while (!list_empty(&priv->free_frames)) {
975 element = priv->free_frames.next;
976 list_del(element);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800977 kfree(list_entry(element, struct iwl4965_frame, list));
Zhu Yib481de92007-09-25 17:54:57 -0700978 priv->frames_count--;
979 }
980
981 if (priv->frames_count) {
982 IWL_WARNING("%d frames still in use. Did we lose one?\n",
983 priv->frames_count);
984 priv->frames_count = 0;
985 }
986}
987
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700988static struct iwl4965_frame *iwl4965_get_free_frame(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700989{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800990 struct iwl4965_frame *frame;
Zhu Yib481de92007-09-25 17:54:57 -0700991 struct list_head *element;
992 if (list_empty(&priv->free_frames)) {
993 frame = kzalloc(sizeof(*frame), GFP_KERNEL);
994 if (!frame) {
995 IWL_ERROR("Could not allocate frame!\n");
996 return NULL;
997 }
998
999 priv->frames_count++;
1000 return frame;
1001 }
1002
1003 element = priv->free_frames.next;
1004 list_del(element);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001005 return list_entry(element, struct iwl4965_frame, list);
Zhu Yib481de92007-09-25 17:54:57 -07001006}
1007
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001008static void iwl4965_free_frame(struct iwl_priv *priv, struct iwl4965_frame *frame)
Zhu Yib481de92007-09-25 17:54:57 -07001009{
1010 memset(frame, 0, sizeof(*frame));
1011 list_add(&frame->list, &priv->free_frames);
1012}
1013
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001014unsigned int iwl4965_fill_beacon_frame(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07001015 struct ieee80211_hdr *hdr,
1016 const u8 *dest, int left)
1017{
1018
Tomas Winkler3109ece2008-03-28 16:33:35 -07001019 if (!iwl_is_associated(priv) || !priv->ibss_beacon ||
Zhu Yib481de92007-09-25 17:54:57 -07001020 ((priv->iw_mode != IEEE80211_IF_TYPE_IBSS) &&
1021 (priv->iw_mode != IEEE80211_IF_TYPE_AP)))
1022 return 0;
1023
1024 if (priv->ibss_beacon->len > left)
1025 return 0;
1026
1027 memcpy(hdr, priv->ibss_beacon->data, priv->ibss_beacon->len);
1028
1029 return priv->ibss_beacon->len;
1030}
1031
Guy Cohen39e88502008-04-23 17:14:57 -07001032static u8 iwl4965_rate_get_lowest_plcp(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001033{
Guy Cohen39e88502008-04-23 17:14:57 -07001034 int i;
1035 int rate_mask;
Zhu Yib481de92007-09-25 17:54:57 -07001036
Guy Cohen39e88502008-04-23 17:14:57 -07001037 /* Set rate mask*/
1038 if (priv->staging_rxon.flags & RXON_FLG_BAND_24G_MSK)
1039 rate_mask = priv->active_rate_basic & 0xF;
1040 else
1041 rate_mask = priv->active_rate_basic & 0xFF0;
1042
1043 /* Find lowest valid rate */
Zhu Yib481de92007-09-25 17:54:57 -07001044 for (i = IWL_RATE_1M_INDEX; i != IWL_RATE_INVALID;
Guy Cohen39e88502008-04-23 17:14:57 -07001045 i = iwl4965_rates[i].next_ieee) {
Zhu Yib481de92007-09-25 17:54:57 -07001046 if (rate_mask & (1 << i))
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001047 return iwl4965_rates[i].plcp;
Zhu Yib481de92007-09-25 17:54:57 -07001048 }
1049
Guy Cohen39e88502008-04-23 17:14:57 -07001050 /* No valid rate was found. Assign the lowest one */
1051 if (priv->staging_rxon.flags & RXON_FLG_BAND_24G_MSK)
1052 return IWL_RATE_1M_PLCP;
1053 else
1054 return IWL_RATE_6M_PLCP;
Zhu Yib481de92007-09-25 17:54:57 -07001055}
1056
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001057static int iwl4965_send_beacon_cmd(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001058{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001059 struct iwl4965_frame *frame;
Zhu Yib481de92007-09-25 17:54:57 -07001060 unsigned int frame_size;
1061 int rc;
1062 u8 rate;
1063
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001064 frame = iwl4965_get_free_frame(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001065
1066 if (!frame) {
1067 IWL_ERROR("Could not obtain free frame buffer for beacon "
1068 "command.\n");
1069 return -ENOMEM;
1070 }
1071
Guy Cohen39e88502008-04-23 17:14:57 -07001072 rate = iwl4965_rate_get_lowest_plcp(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001073
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001074 frame_size = iwl4965_hw_get_beacon_cmd(priv, frame, rate);
Zhu Yib481de92007-09-25 17:54:57 -07001075
Tomas Winkler857485c2008-03-21 13:53:44 -07001076 rc = iwl_send_cmd_pdu(priv, REPLY_TX_BEACON, frame_size,
Zhu Yib481de92007-09-25 17:54:57 -07001077 &frame->u.cmd[0]);
1078
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001079 iwl4965_free_frame(priv, frame);
Zhu Yib481de92007-09-25 17:54:57 -07001080
1081 return rc;
1082}
1083
1084/******************************************************************************
1085 *
Zhu Yib481de92007-09-25 17:54:57 -07001086 * Misc. internal state and helper functions
1087 *
1088 ******************************************************************************/
Zhu Yib481de92007-09-25 17:54:57 -07001089
Zhu Yib481de92007-09-25 17:54:57 -07001090/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001091 * iwl4965_supported_rate_to_ie - fill in the supported rate in IE field
Zhu Yib481de92007-09-25 17:54:57 -07001092 *
1093 * return : set the bit for each supported rate insert in ie
1094 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001095static u16 iwl4965_supported_rate_to_ie(u8 *ie, u16 supported_rate,
Tomas Winklerc7c46672007-10-18 02:04:15 +02001096 u16 basic_rate, int *left)
Zhu Yib481de92007-09-25 17:54:57 -07001097{
1098 u16 ret_rates = 0, bit;
1099 int i;
Tomas Winklerc7c46672007-10-18 02:04:15 +02001100 u8 *cnt = ie;
1101 u8 *rates = ie + 1;
Zhu Yib481de92007-09-25 17:54:57 -07001102
1103 for (bit = 1, i = 0; i < IWL_RATE_COUNT; i++, bit <<= 1) {
1104 if (bit & supported_rate) {
1105 ret_rates |= bit;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001106 rates[*cnt] = iwl4965_rates[i].ieee |
Tomas Winklerc7c46672007-10-18 02:04:15 +02001107 ((bit & basic_rate) ? 0x80 : 0x00);
1108 (*cnt)++;
1109 (*left)--;
1110 if ((*left <= 0) ||
1111 (*cnt >= IWL_SUPPORTED_RATES_IE_LEN))
Zhu Yib481de92007-09-25 17:54:57 -07001112 break;
1113 }
1114 }
1115
1116 return ret_rates;
1117}
1118
Emmanuel Grumbachd1141df2008-04-21 15:42:00 -07001119#ifdef CONFIG_IWL4965_HT
1120static void iwl4965_ht_conf(struct iwl_priv *priv,
1121 struct ieee80211_bss_conf *bss_conf)
1122{
1123 struct ieee80211_ht_info *ht_conf = bss_conf->ht_conf;
1124 struct ieee80211_ht_bss_info *ht_bss_conf = bss_conf->ht_bss_conf;
1125 struct iwl_ht_info *iwl_conf = &priv->current_ht_config;
1126
1127 IWL_DEBUG_MAC80211("enter: \n");
1128
1129 iwl_conf->is_ht = bss_conf->assoc_ht;
1130
1131 if (!iwl_conf->is_ht)
1132 return;
1133
1134 priv->ps_mode = (u8)((ht_conf->cap & IEEE80211_HT_CAP_MIMO_PS) >> 2);
1135
1136 if (ht_conf->cap & IEEE80211_HT_CAP_SGI_20)
1137 iwl_conf->sgf |= 0x1;
1138 if (ht_conf->cap & IEEE80211_HT_CAP_SGI_40)
1139 iwl_conf->sgf |= 0x2;
1140
1141 iwl_conf->is_green_field = !!(ht_conf->cap & IEEE80211_HT_CAP_GRN_FLD);
1142 iwl_conf->max_amsdu_size =
1143 !!(ht_conf->cap & IEEE80211_HT_CAP_MAX_AMSDU);
1144
1145 iwl_conf->supported_chan_width =
1146 !!(ht_conf->cap & IEEE80211_HT_CAP_SUP_WIDTH);
1147 iwl_conf->extension_chan_offset =
1148 ht_bss_conf->bss_cap & IEEE80211_HT_IE_CHA_SEC_OFFSET;
1149 /* If no above or below channel supplied disable FAT channel */
1150 if (iwl_conf->extension_chan_offset != IWL_EXT_CHANNEL_OFFSET_ABOVE &&
1151 iwl_conf->extension_chan_offset != IWL_EXT_CHANNEL_OFFSET_BELOW)
1152 iwl_conf->supported_chan_width = 0;
1153
1154 iwl_conf->tx_mimo_ps_mode =
1155 (u8)((ht_conf->cap & IEEE80211_HT_CAP_MIMO_PS) >> 2);
1156 memcpy(iwl_conf->supp_mcs_set, ht_conf->supp_mcs_set, 16);
1157
1158 iwl_conf->control_channel = ht_bss_conf->primary_channel;
1159 iwl_conf->tx_chan_width =
1160 !!(ht_bss_conf->bss_cap & IEEE80211_HT_IE_CHA_WIDTH);
1161 iwl_conf->ht_protection =
1162 ht_bss_conf->bss_op_mode & IEEE80211_HT_IE_HT_PROTECTION;
1163 iwl_conf->non_GF_STA_present =
1164 !!(ht_bss_conf->bss_op_mode & IEEE80211_HT_IE_NON_GF_STA_PRSNT);
1165
1166 IWL_DEBUG_MAC80211("control channel %d\n", iwl_conf->control_channel);
1167 IWL_DEBUG_MAC80211("leave\n");
1168}
1169
1170static void iwl_ht_cap_to_ie(const struct ieee80211_supported_band *sband,
1171 u8 *pos, int *left)
1172{
1173 struct ieee80211_ht_cap *ht_cap;
1174
1175 if (!sband || !sband->ht_info.ht_supported)
1176 return;
1177
1178 if (*left < sizeof(struct ieee80211_ht_cap))
1179 return;
1180
1181 *pos++ = sizeof(struct ieee80211_ht_cap);
1182 ht_cap = (struct ieee80211_ht_cap *) pos;
1183
1184 ht_cap->cap_info = cpu_to_le16(sband->ht_info.cap);
1185 memcpy(ht_cap->supp_mcs_set, sband->ht_info.supp_mcs_set, 16);
1186 ht_cap->ampdu_params_info =
1187 (sband->ht_info.ampdu_factor & IEEE80211_HT_CAP_AMPDU_FACTOR) |
1188 ((sband->ht_info.ampdu_density << 2) &
1189 IEEE80211_HT_CAP_AMPDU_DENSITY);
1190 *left -= sizeof(struct ieee80211_ht_cap);
1191}
1192#else
1193static inline void iwl4965_ht_conf(struct iwl_priv *priv,
1194 struct ieee80211_bss_conf *bss_conf)
1195{
1196}
1197static void iwl_ht_cap_to_ie(const struct ieee80211_supported_band *sband,
1198 u8 *pos, int *left)
1199{
1200}
1201#endif
1202
1203
Zhu Yib481de92007-09-25 17:54:57 -07001204/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001205 * iwl4965_fill_probe_req - fill in all required fields and IE for probe request
Zhu Yib481de92007-09-25 17:54:57 -07001206 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001207static u16 iwl4965_fill_probe_req(struct iwl_priv *priv,
Tomas Winkler78330fd2008-02-06 02:37:18 +02001208 enum ieee80211_band band,
1209 struct ieee80211_mgmt *frame,
1210 int left, int is_direct)
Zhu Yib481de92007-09-25 17:54:57 -07001211{
1212 int len = 0;
1213 u8 *pos = NULL;
mabbasbee488d2007-10-25 17:15:42 +08001214 u16 active_rates, ret_rates, cck_rates, active_rate_basic;
Tomas Winkler78330fd2008-02-06 02:37:18 +02001215 const struct ieee80211_supported_band *sband =
Emmanuel Grumbachd1141df2008-04-21 15:42:00 -07001216 iwl_get_hw_mode(priv, band);
Zhu Yib481de92007-09-25 17:54:57 -07001217
1218 /* Make sure there is enough space for the probe request,
1219 * two mandatory IEs and the data */
1220 left -= 24;
1221 if (left < 0)
1222 return 0;
1223 len += 24;
1224
1225 frame->frame_control = cpu_to_le16(IEEE80211_STYPE_PROBE_REQ);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001226 memcpy(frame->da, iwl4965_broadcast_addr, ETH_ALEN);
Zhu Yib481de92007-09-25 17:54:57 -07001227 memcpy(frame->sa, priv->mac_addr, ETH_ALEN);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001228 memcpy(frame->bssid, iwl4965_broadcast_addr, ETH_ALEN);
Zhu Yib481de92007-09-25 17:54:57 -07001229 frame->seq_ctrl = 0;
1230
1231 /* fill in our indirect SSID IE */
1232 /* ...next IE... */
1233
1234 left -= 2;
1235 if (left < 0)
1236 return 0;
1237 len += 2;
1238 pos = &(frame->u.probe_req.variable[0]);
1239 *pos++ = WLAN_EID_SSID;
1240 *pos++ = 0;
1241
1242 /* fill in our direct SSID IE... */
1243 if (is_direct) {
1244 /* ...next IE... */
1245 left -= 2 + priv->essid_len;
1246 if (left < 0)
1247 return 0;
1248 /* ... fill it in... */
1249 *pos++ = WLAN_EID_SSID;
1250 *pos++ = priv->essid_len;
1251 memcpy(pos, priv->essid, priv->essid_len);
1252 pos += priv->essid_len;
1253 len += 2 + priv->essid_len;
1254 }
1255
1256 /* fill in supported rate */
1257 /* ...next IE... */
1258 left -= 2;
1259 if (left < 0)
1260 return 0;
Tomas Winklerc7c46672007-10-18 02:04:15 +02001261
Zhu Yib481de92007-09-25 17:54:57 -07001262 /* ... fill it in... */
1263 *pos++ = WLAN_EID_SUPP_RATES;
1264 *pos = 0;
Tomas Winklerc7c46672007-10-18 02:04:15 +02001265
mabbasbee488d2007-10-25 17:15:42 +08001266 /* exclude 60M rate */
1267 active_rates = priv->rates_mask;
1268 active_rates &= ~IWL_RATE_60M_MASK;
1269
1270 active_rate_basic = active_rates & IWL_BASIC_RATES_MASK;
Zhu Yib481de92007-09-25 17:54:57 -07001271
Tomas Winklerc7c46672007-10-18 02:04:15 +02001272 cck_rates = IWL_CCK_RATES_MASK & active_rates;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001273 ret_rates = iwl4965_supported_rate_to_ie(pos, cck_rates,
mabbasbee488d2007-10-25 17:15:42 +08001274 active_rate_basic, &left);
Tomas Winklerc7c46672007-10-18 02:04:15 +02001275 active_rates &= ~ret_rates;
1276
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001277 ret_rates = iwl4965_supported_rate_to_ie(pos, active_rates,
mabbasbee488d2007-10-25 17:15:42 +08001278 active_rate_basic, &left);
Tomas Winklerc7c46672007-10-18 02:04:15 +02001279 active_rates &= ~ret_rates;
1280
Zhu Yib481de92007-09-25 17:54:57 -07001281 len += 2 + *pos;
1282 pos += (*pos) + 1;
Tomas Winklerc7c46672007-10-18 02:04:15 +02001283 if (active_rates == 0)
Zhu Yib481de92007-09-25 17:54:57 -07001284 goto fill_end;
1285
1286 /* fill in supported extended rate */
1287 /* ...next IE... */
1288 left -= 2;
1289 if (left < 0)
1290 return 0;
1291 /* ... fill it in... */
1292 *pos++ = WLAN_EID_EXT_SUPP_RATES;
1293 *pos = 0;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001294 iwl4965_supported_rate_to_ie(pos, active_rates,
mabbasbee488d2007-10-25 17:15:42 +08001295 active_rate_basic, &left);
Zhu Yib481de92007-09-25 17:54:57 -07001296 if (*pos > 0)
1297 len += 2 + *pos;
1298
Zhu Yib481de92007-09-25 17:54:57 -07001299 fill_end:
Emmanuel Grumbachd1141df2008-04-21 15:42:00 -07001300 /* fill in HT IE */
1301 left -= 2;
1302 if (left < 0)
1303 return 0;
1304
1305 *pos++ = WLAN_EID_HT_CAPABILITY;
1306 *pos = 0;
1307
1308 iwl_ht_cap_to_ie(sband, pos, &left);
1309
1310 if (*pos > 0)
1311 len += 2 + *pos;
Zhu Yib481de92007-09-25 17:54:57 -07001312 return (u16)len;
1313}
1314
1315/*
1316 * QoS support
1317*/
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001318static int iwl4965_send_qos_params_command(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001319 struct iwl4965_qosparam_cmd *qos)
Zhu Yib481de92007-09-25 17:54:57 -07001320{
1321
Tomas Winkler857485c2008-03-21 13:53:44 -07001322 return iwl_send_cmd_pdu(priv, REPLY_QOS_PARAM,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001323 sizeof(struct iwl4965_qosparam_cmd), qos);
Zhu Yib481de92007-09-25 17:54:57 -07001324}
1325
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001326static void iwl4965_activate_qos(struct iwl_priv *priv, u8 force)
Zhu Yib481de92007-09-25 17:54:57 -07001327{
1328 unsigned long flags;
1329
Zhu Yib481de92007-09-25 17:54:57 -07001330 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
1331 return;
1332
1333 if (!priv->qos_data.qos_enable)
1334 return;
1335
1336 spin_lock_irqsave(&priv->lock, flags);
1337 priv->qos_data.def_qos_parm.qos_flags = 0;
1338
1339 if (priv->qos_data.qos_cap.q_AP.queue_request &&
1340 !priv->qos_data.qos_cap.q_AP.txop_request)
1341 priv->qos_data.def_qos_parm.qos_flags |=
1342 QOS_PARAM_FLG_TXOP_TYPE_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07001343 if (priv->qos_data.qos_active)
1344 priv->qos_data.def_qos_parm.qos_flags |=
1345 QOS_PARAM_FLG_UPDATE_EDCA_MSK;
1346
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08001347#ifdef CONFIG_IWL4965_HT
Ron Rindjunskyfd105e72007-11-26 16:14:39 +02001348 if (priv->current_ht_config.is_ht)
Tomas Winklerf1f1f5c2007-10-25 17:15:26 +08001349 priv->qos_data.def_qos_parm.qos_flags |= QOS_PARAM_FLG_TGN_MSK;
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08001350#endif /* CONFIG_IWL4965_HT */
Tomas Winklerf1f1f5c2007-10-25 17:15:26 +08001351
Zhu Yib481de92007-09-25 17:54:57 -07001352 spin_unlock_irqrestore(&priv->lock, flags);
1353
Tomas Winkler3109ece2008-03-28 16:33:35 -07001354 if (force || iwl_is_associated(priv)) {
Tomas Winklerf1f1f5c2007-10-25 17:15:26 +08001355 IWL_DEBUG_QOS("send QoS cmd with Qos active=%d FLAGS=0x%X\n",
1356 priv->qos_data.qos_active,
1357 priv->qos_data.def_qos_parm.qos_flags);
Zhu Yib481de92007-09-25 17:54:57 -07001358
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001359 iwl4965_send_qos_params_command(priv,
Zhu Yib481de92007-09-25 17:54:57 -07001360 &(priv->qos_data.def_qos_parm));
1361 }
1362}
1363
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001364int iwl4965_is_network_packet(struct iwl_priv *priv, struct ieee80211_hdr *header)
Zhu Yib481de92007-09-25 17:54:57 -07001365{
1366 /* Filter incoming packets to determine if they are targeted toward
1367 * this network, discarding packets coming from ourselves */
1368 switch (priv->iw_mode) {
1369 case IEEE80211_IF_TYPE_IBSS: /* Header: Dest. | Source | BSSID */
1370 /* packets from our adapter are dropped (echo) */
1371 if (!compare_ether_addr(header->addr2, priv->mac_addr))
1372 return 0;
1373 /* {broad,multi}cast packets to our IBSS go through */
1374 if (is_multicast_ether_addr(header->addr1))
1375 return !compare_ether_addr(header->addr3, priv->bssid);
1376 /* packets to our adapter go through */
1377 return !compare_ether_addr(header->addr1, priv->mac_addr);
1378 case IEEE80211_IF_TYPE_STA: /* Header: Dest. | AP{BSSID} | Source */
1379 /* packets from our adapter are dropped (echo) */
1380 if (!compare_ether_addr(header->addr3, priv->mac_addr))
1381 return 0;
1382 /* {broad,multi}cast packets to our BSS go through */
1383 if (is_multicast_ether_addr(header->addr1))
1384 return !compare_ether_addr(header->addr2, priv->bssid);
1385 /* packets to our adapter go through */
1386 return !compare_ether_addr(header->addr1, priv->mac_addr);
Tomas Winkler69dc5d92008-03-25 16:33:41 -07001387 default:
1388 break;
Zhu Yib481de92007-09-25 17:54:57 -07001389 }
1390
1391 return 1;
1392}
1393
1394#define TX_STATUS_ENTRY(x) case TX_STATUS_FAIL_ ## x: return #x
1395
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001396static const char *iwl4965_get_tx_fail_reason(u32 status)
Zhu Yib481de92007-09-25 17:54:57 -07001397{
1398 switch (status & TX_STATUS_MSK) {
1399 case TX_STATUS_SUCCESS:
1400 return "SUCCESS";
1401 TX_STATUS_ENTRY(SHORT_LIMIT);
1402 TX_STATUS_ENTRY(LONG_LIMIT);
1403 TX_STATUS_ENTRY(FIFO_UNDERRUN);
1404 TX_STATUS_ENTRY(MGMNT_ABORT);
1405 TX_STATUS_ENTRY(NEXT_FRAG);
1406 TX_STATUS_ENTRY(LIFE_EXPIRE);
1407 TX_STATUS_ENTRY(DEST_PS);
1408 TX_STATUS_ENTRY(ABORTED);
1409 TX_STATUS_ENTRY(BT_RETRY);
1410 TX_STATUS_ENTRY(STA_INVALID);
1411 TX_STATUS_ENTRY(FRAG_DROPPED);
1412 TX_STATUS_ENTRY(TID_DISABLE);
1413 TX_STATUS_ENTRY(FRAME_FLUSHED);
1414 TX_STATUS_ENTRY(INSUFFICIENT_CF_POLL);
1415 TX_STATUS_ENTRY(TX_LOCKED);
1416 TX_STATUS_ENTRY(NO_BEACON_ON_RADAR);
1417 }
1418
1419 return "UNKNOWN";
1420}
1421
1422/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001423 * iwl4965_scan_cancel - Cancel any currently executing HW scan
Zhu Yib481de92007-09-25 17:54:57 -07001424 *
1425 * NOTE: priv->mutex is not required before calling this function
1426 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001427static int iwl4965_scan_cancel(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001428{
1429 if (!test_bit(STATUS_SCAN_HW, &priv->status)) {
1430 clear_bit(STATUS_SCANNING, &priv->status);
1431 return 0;
1432 }
1433
1434 if (test_bit(STATUS_SCANNING, &priv->status)) {
1435 if (!test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
1436 IWL_DEBUG_SCAN("Queuing scan abort.\n");
1437 set_bit(STATUS_SCAN_ABORTING, &priv->status);
1438 queue_work(priv->workqueue, &priv->abort_scan);
1439
1440 } else
1441 IWL_DEBUG_SCAN("Scan abort already in progress.\n");
1442
1443 return test_bit(STATUS_SCANNING, &priv->status);
1444 }
1445
1446 return 0;
1447}
1448
1449/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001450 * iwl4965_scan_cancel_timeout - Cancel any currently executing HW scan
Zhu Yib481de92007-09-25 17:54:57 -07001451 * @ms: amount of time to wait (in milliseconds) for scan to abort
1452 *
1453 * NOTE: priv->mutex must be held before calling this function
1454 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001455static int iwl4965_scan_cancel_timeout(struct iwl_priv *priv, unsigned long ms)
Zhu Yib481de92007-09-25 17:54:57 -07001456{
1457 unsigned long now = jiffies;
1458 int ret;
1459
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001460 ret = iwl4965_scan_cancel(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001461 if (ret && ms) {
1462 mutex_unlock(&priv->mutex);
1463 while (!time_after(jiffies, now + msecs_to_jiffies(ms)) &&
1464 test_bit(STATUS_SCANNING, &priv->status))
1465 msleep(1);
1466 mutex_lock(&priv->mutex);
1467
1468 return test_bit(STATUS_SCANNING, &priv->status);
1469 }
1470
1471 return ret;
1472}
1473
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001474static void iwl4965_sequence_reset(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001475{
1476 /* Reset ieee stats */
1477
1478 /* We don't reset the net_device_stats (ieee->stats) on
1479 * re-association */
1480
1481 priv->last_seq_num = -1;
1482 priv->last_frag_num = -1;
1483 priv->last_packet_time = 0;
1484
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001485 iwl4965_scan_cancel(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001486}
1487
1488#define MAX_UCODE_BEACON_INTERVAL 4096
1489#define INTEL_CONN_LISTEN_INTERVAL __constant_cpu_to_le16(0xA)
1490
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001491static __le16 iwl4965_adjust_beacon_interval(u16 beacon_val)
Zhu Yib481de92007-09-25 17:54:57 -07001492{
1493 u16 new_val = 0;
1494 u16 beacon_factor = 0;
1495
1496 beacon_factor =
1497 (beacon_val + MAX_UCODE_BEACON_INTERVAL)
1498 / MAX_UCODE_BEACON_INTERVAL;
1499 new_val = beacon_val / beacon_factor;
1500
1501 return cpu_to_le16(new_val);
1502}
1503
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001504static void iwl4965_setup_rxon_timing(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001505{
1506 u64 interval_tm_unit;
1507 u64 tsf, result;
1508 unsigned long flags;
1509 struct ieee80211_conf *conf = NULL;
1510 u16 beacon_int = 0;
1511
1512 conf = ieee80211_get_hw_conf(priv->hw);
1513
1514 spin_lock_irqsave(&priv->lock, flags);
Tomas Winkler3109ece2008-03-28 16:33:35 -07001515 priv->rxon_timing.timestamp.dw[1] = cpu_to_le32(priv->timestamp >> 32);
1516 priv->rxon_timing.timestamp.dw[0] =
1517 cpu_to_le32(priv->timestamp & 0xFFFFFFFF);
Zhu Yib481de92007-09-25 17:54:57 -07001518
1519 priv->rxon_timing.listen_interval = INTEL_CONN_LISTEN_INTERVAL;
1520
Tomas Winkler3109ece2008-03-28 16:33:35 -07001521 tsf = priv->timestamp;
Zhu Yib481de92007-09-25 17:54:57 -07001522
1523 beacon_int = priv->beacon_int;
1524 spin_unlock_irqrestore(&priv->lock, flags);
1525
1526 if (priv->iw_mode == IEEE80211_IF_TYPE_STA) {
1527 if (beacon_int == 0) {
1528 priv->rxon_timing.beacon_interval = cpu_to_le16(100);
1529 priv->rxon_timing.beacon_init_val = cpu_to_le32(102400);
1530 } else {
1531 priv->rxon_timing.beacon_interval =
1532 cpu_to_le16(beacon_int);
1533 priv->rxon_timing.beacon_interval =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001534 iwl4965_adjust_beacon_interval(
Zhu Yib481de92007-09-25 17:54:57 -07001535 le16_to_cpu(priv->rxon_timing.beacon_interval));
1536 }
1537
1538 priv->rxon_timing.atim_window = 0;
1539 } else {
1540 priv->rxon_timing.beacon_interval =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001541 iwl4965_adjust_beacon_interval(conf->beacon_int);
Zhu Yib481de92007-09-25 17:54:57 -07001542 /* TODO: we need to get atim_window from upper stack
1543 * for now we set to 0 */
1544 priv->rxon_timing.atim_window = 0;
1545 }
1546
1547 interval_tm_unit =
1548 (le16_to_cpu(priv->rxon_timing.beacon_interval) * 1024);
1549 result = do_div(tsf, interval_tm_unit);
1550 priv->rxon_timing.beacon_init_val =
1551 cpu_to_le32((u32) ((u64) interval_tm_unit - result));
1552
1553 IWL_DEBUG_ASSOC
1554 ("beacon interval %d beacon timer %d beacon tim %d\n",
1555 le16_to_cpu(priv->rxon_timing.beacon_interval),
1556 le32_to_cpu(priv->rxon_timing.beacon_init_val),
1557 le16_to_cpu(priv->rxon_timing.atim_window));
1558}
1559
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001560static int iwl4965_scan_initiate(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001561{
1562 if (priv->iw_mode == IEEE80211_IF_TYPE_AP) {
1563 IWL_ERROR("APs don't scan.\n");
1564 return 0;
1565 }
1566
Tomas Winklerfee12472008-04-03 16:05:21 -07001567 if (!iwl_is_ready_rf(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07001568 IWL_DEBUG_SCAN("Aborting scan due to not ready.\n");
1569 return -EIO;
1570 }
1571
1572 if (test_bit(STATUS_SCANNING, &priv->status)) {
1573 IWL_DEBUG_SCAN("Scan already in progress.\n");
1574 return -EAGAIN;
1575 }
1576
1577 if (test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
1578 IWL_DEBUG_SCAN("Scan request while abort pending. "
1579 "Queuing.\n");
1580 return -EAGAIN;
1581 }
1582
1583 IWL_DEBUG_INFO("Starting scan...\n");
1584 priv->scan_bands = 2;
1585 set_bit(STATUS_SCANNING, &priv->status);
1586 priv->scan_start = jiffies;
1587 priv->scan_pass_start = priv->scan_start;
1588
1589 queue_work(priv->workqueue, &priv->request_scan);
1590
1591 return 0;
1592}
1593
Zhu Yib481de92007-09-25 17:54:57 -07001594
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001595static void iwl4965_set_flags_for_phymode(struct iwl_priv *priv,
Johannes Berg8318d782008-01-24 19:38:38 +01001596 enum ieee80211_band band)
Zhu Yib481de92007-09-25 17:54:57 -07001597{
Johannes Berg8318d782008-01-24 19:38:38 +01001598 if (band == IEEE80211_BAND_5GHZ) {
Zhu Yib481de92007-09-25 17:54:57 -07001599 priv->staging_rxon.flags &=
1600 ~(RXON_FLG_BAND_24G_MSK | RXON_FLG_AUTO_DETECT_MSK
1601 | RXON_FLG_CCK_MSK);
1602 priv->staging_rxon.flags |= RXON_FLG_SHORT_SLOT_MSK;
1603 } else {
Reinette Chatre508e32e2008-04-14 21:16:13 -07001604 /* Copied from iwl4965_post_associate() */
Zhu Yib481de92007-09-25 17:54:57 -07001605 if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_SLOT_TIME)
1606 priv->staging_rxon.flags |= RXON_FLG_SHORT_SLOT_MSK;
1607 else
1608 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
1609
1610 if (priv->iw_mode == IEEE80211_IF_TYPE_IBSS)
1611 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
1612
1613 priv->staging_rxon.flags |= RXON_FLG_BAND_24G_MSK;
1614 priv->staging_rxon.flags |= RXON_FLG_AUTO_DETECT_MSK;
1615 priv->staging_rxon.flags &= ~RXON_FLG_CCK_MSK;
1616 }
1617}
1618
1619/*
Ian Schram01ebd062007-10-25 17:15:22 +08001620 * initialize rxon structure with default values from eeprom
Zhu Yib481de92007-09-25 17:54:57 -07001621 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001622static void iwl4965_connection_init_rx_config(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001623{
Assaf Kraussbf85ea42008-03-14 10:38:49 -07001624 const struct iwl_channel_info *ch_info;
Zhu Yib481de92007-09-25 17:54:57 -07001625
1626 memset(&priv->staging_rxon, 0, sizeof(priv->staging_rxon));
1627
1628 switch (priv->iw_mode) {
1629 case IEEE80211_IF_TYPE_AP:
1630 priv->staging_rxon.dev_type = RXON_DEV_TYPE_AP;
1631 break;
1632
1633 case IEEE80211_IF_TYPE_STA:
1634 priv->staging_rxon.dev_type = RXON_DEV_TYPE_ESS;
1635 priv->staging_rxon.filter_flags = RXON_FILTER_ACCEPT_GRP_MSK;
1636 break;
1637
1638 case IEEE80211_IF_TYPE_IBSS:
1639 priv->staging_rxon.dev_type = RXON_DEV_TYPE_IBSS;
1640 priv->staging_rxon.flags = RXON_FLG_SHORT_PREAMBLE_MSK;
1641 priv->staging_rxon.filter_flags = RXON_FILTER_BCON_AWARE_MSK |
1642 RXON_FILTER_ACCEPT_GRP_MSK;
1643 break;
1644
1645 case IEEE80211_IF_TYPE_MNTR:
1646 priv->staging_rxon.dev_type = RXON_DEV_TYPE_SNIFFER;
1647 priv->staging_rxon.filter_flags = RXON_FILTER_PROMISC_MSK |
1648 RXON_FILTER_CTL2HOST_MSK | RXON_FILTER_ACCEPT_GRP_MSK;
1649 break;
Tomas Winkler69dc5d92008-03-25 16:33:41 -07001650 default:
1651 IWL_ERROR("Unsupported interface type %d\n", priv->iw_mode);
1652 break;
Zhu Yib481de92007-09-25 17:54:57 -07001653 }
1654
1655#if 0
1656 /* TODO: Figure out when short_preamble would be set and cache from
1657 * that */
1658 if (!hw_to_local(priv->hw)->short_preamble)
1659 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_PREAMBLE_MSK;
1660 else
1661 priv->staging_rxon.flags |= RXON_FLG_SHORT_PREAMBLE_MSK;
1662#endif
1663
Assaf Krauss8622e702008-03-21 13:53:43 -07001664 ch_info = iwl_get_channel_info(priv, priv->band,
Zhu Yib481de92007-09-25 17:54:57 -07001665 le16_to_cpu(priv->staging_rxon.channel));
1666
1667 if (!ch_info)
1668 ch_info = &priv->channel_info[0];
1669
1670 /*
1671 * in some case A channels are all non IBSS
1672 * in this case force B/G channel
1673 */
1674 if ((priv->iw_mode == IEEE80211_IF_TYPE_IBSS) &&
1675 !(is_channel_ibss(ch_info)))
1676 ch_info = &priv->channel_info[0];
1677
1678 priv->staging_rxon.channel = cpu_to_le16(ch_info->channel);
Johannes Berg8318d782008-01-24 19:38:38 +01001679 priv->band = ch_info->band;
Zhu Yib481de92007-09-25 17:54:57 -07001680
Johannes Berg8318d782008-01-24 19:38:38 +01001681 iwl4965_set_flags_for_phymode(priv, priv->band);
Zhu Yib481de92007-09-25 17:54:57 -07001682
1683 priv->staging_rxon.ofdm_basic_rates =
1684 (IWL_OFDM_RATES_MASK >> IWL_FIRST_OFDM_RATE) & 0xFF;
1685 priv->staging_rxon.cck_basic_rates =
1686 (IWL_CCK_RATES_MASK >> IWL_FIRST_CCK_RATE) & 0xF;
1687
1688 priv->staging_rxon.flags &= ~(RXON_FLG_CHANNEL_MODE_MIXED_MSK |
1689 RXON_FLG_CHANNEL_MODE_PURE_40_MSK);
1690 memcpy(priv->staging_rxon.node_addr, priv->mac_addr, ETH_ALEN);
1691 memcpy(priv->staging_rxon.wlap_bssid_addr, priv->mac_addr, ETH_ALEN);
1692 priv->staging_rxon.ofdm_ht_single_stream_basic_rates = 0xff;
1693 priv->staging_rxon.ofdm_ht_dual_stream_basic_rates = 0xff;
Ron Rindjunskyc7de35c2008-04-23 17:15:05 -07001694 iwl_set_rxon_chain(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001695}
1696
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001697static int iwl4965_set_mode(struct iwl_priv *priv, int mode)
Zhu Yib481de92007-09-25 17:54:57 -07001698{
Zhu Yib481de92007-09-25 17:54:57 -07001699 if (mode == IEEE80211_IF_TYPE_IBSS) {
Assaf Kraussbf85ea42008-03-14 10:38:49 -07001700 const struct iwl_channel_info *ch_info;
Zhu Yib481de92007-09-25 17:54:57 -07001701
Assaf Krauss8622e702008-03-21 13:53:43 -07001702 ch_info = iwl_get_channel_info(priv,
Johannes Berg8318d782008-01-24 19:38:38 +01001703 priv->band,
Zhu Yib481de92007-09-25 17:54:57 -07001704 le16_to_cpu(priv->staging_rxon.channel));
1705
1706 if (!ch_info || !is_channel_ibss(ch_info)) {
1707 IWL_ERROR("channel %d not IBSS channel\n",
1708 le16_to_cpu(priv->staging_rxon.channel));
1709 return -EINVAL;
1710 }
1711 }
1712
Zhu Yib481de92007-09-25 17:54:57 -07001713 priv->iw_mode = mode;
1714
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001715 iwl4965_connection_init_rx_config(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001716 memcpy(priv->staging_rxon.node_addr, priv->mac_addr, ETH_ALEN);
1717
Assaf Kraussbf85ea42008-03-14 10:38:49 -07001718 iwlcore_clear_stations_table(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001719
Mohamed Abbasfde35712007-11-29 11:10:15 +08001720 /* dont commit rxon if rf-kill is on*/
Tomas Winklerfee12472008-04-03 16:05:21 -07001721 if (!iwl_is_ready_rf(priv))
Mohamed Abbasfde35712007-11-29 11:10:15 +08001722 return -EAGAIN;
1723
1724 cancel_delayed_work(&priv->scan_check);
1725 if (iwl4965_scan_cancel_timeout(priv, 100)) {
1726 IWL_WARNING("Aborted scan still in progress after 100ms\n");
1727 IWL_DEBUG_MAC80211("leaving - scan abort failed.\n");
1728 return -EAGAIN;
1729 }
1730
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001731 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001732
1733 return 0;
1734}
1735
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001736static void iwl4965_build_tx_cmd_hwcrypto(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07001737 struct ieee80211_tx_control *ctl,
Tomas Winkler857485c2008-03-21 13:53:44 -07001738 struct iwl_cmd *cmd,
Zhu Yib481de92007-09-25 17:54:57 -07001739 struct sk_buff *skb_frag,
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07001740 int sta_id)
Zhu Yib481de92007-09-25 17:54:57 -07001741{
Tomas Winkler6def9762008-05-05 10:22:31 +08001742 struct iwl_hw_key *keyinfo = &priv->stations[sta_id].keyinfo;
Emmanuel Grumbach6974e362008-04-14 21:16:06 -07001743 struct iwl_wep_key *wepkey;
1744 int keyidx = 0;
1745
Ivo van Doorn1c014422008-04-17 19:41:02 +02001746 BUG_ON(ctl->hw_key->hw_key_idx > 3);
Zhu Yib481de92007-09-25 17:54:57 -07001747
1748 switch (keyinfo->alg) {
1749 case ALG_CCMP:
1750 cmd->cmd.tx.sec_ctl = TX_CMD_SEC_CCM;
1751 memcpy(cmd->cmd.tx.key, keyinfo->key, keyinfo->keylen);
Max Stepanov8236e182008-03-12 16:58:48 -07001752 if (ctl->flags & IEEE80211_TXCTL_AMPDU)
1753 cmd->cmd.tx.tx_flags |= TX_CMD_FLG_AGG_CCMP_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07001754 IWL_DEBUG_TX("tx_cmd with aes hwcrypto\n");
1755 break;
1756
1757 case ALG_TKIP:
Zhu Yib481de92007-09-25 17:54:57 -07001758 cmd->cmd.tx.sec_ctl = TX_CMD_SEC_TKIP;
Emmanuel Grumbach2bc75082008-03-19 16:41:45 -07001759 ieee80211_get_tkip_key(keyinfo->conf, skb_frag,
1760 IEEE80211_TKIP_P2_KEY, cmd->cmd.tx.key);
1761 IWL_DEBUG_TX("tx_cmd with tkip hwcrypto\n");
Zhu Yib481de92007-09-25 17:54:57 -07001762 break;
1763
1764 case ALG_WEP:
Ivo van Doorn1c014422008-04-17 19:41:02 +02001765 wepkey = &priv->wep_keys[ctl->hw_key->hw_key_idx];
Emmanuel Grumbach6974e362008-04-14 21:16:06 -07001766 cmd->cmd.tx.sec_ctl = 0;
1767 if (priv->default_wep_key) {
1768 /* the WEP key was sent as static */
Ivo van Doorn1c014422008-04-17 19:41:02 +02001769 keyidx = ctl->hw_key->hw_key_idx;
Emmanuel Grumbach6974e362008-04-14 21:16:06 -07001770 memcpy(&cmd->cmd.tx.key[3], wepkey->key,
1771 wepkey->key_size);
1772 if (wepkey->key_size == WEP_KEY_LEN_128)
1773 cmd->cmd.tx.sec_ctl |= TX_CMD_SEC_KEY128;
1774 } else {
Emmanuel Grumbach0211ddd2008-04-14 21:16:07 -07001775 /* the WEP key was sent as dynamic */
1776 keyidx = keyinfo->keyidx;
1777 memcpy(&cmd->cmd.tx.key[3], keyinfo->key,
1778 keyinfo->keylen);
1779 if (keyinfo->keylen == WEP_KEY_LEN_128)
1780 cmd->cmd.tx.sec_ctl |= TX_CMD_SEC_KEY128;
Emmanuel Grumbach6974e362008-04-14 21:16:06 -07001781 }
Zhu Yib481de92007-09-25 17:54:57 -07001782
Emmanuel Grumbach6974e362008-04-14 21:16:06 -07001783 cmd->cmd.tx.sec_ctl |= (TX_CMD_SEC_WEP |
1784 (keyidx & TX_CMD_SEC_MSK) << TX_CMD_SEC_SHIFT);
Zhu Yib481de92007-09-25 17:54:57 -07001785
1786 IWL_DEBUG_TX("Configuring packet for WEP encryption "
Emmanuel Grumbach6974e362008-04-14 21:16:06 -07001787 "with key %d\n", keyidx);
Zhu Yib481de92007-09-25 17:54:57 -07001788 break;
1789
Zhu Yib481de92007-09-25 17:54:57 -07001790 default:
1791 printk(KERN_ERR "Unknown encode alg %d\n", keyinfo->alg);
1792 break;
1793 }
1794}
1795
1796/*
1797 * handle build REPLY_TX command notification.
1798 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001799static void iwl4965_build_tx_cmd_basic(struct iwl_priv *priv,
Tomas Winkler857485c2008-03-21 13:53:44 -07001800 struct iwl_cmd *cmd,
Zhu Yib481de92007-09-25 17:54:57 -07001801 struct ieee80211_tx_control *ctrl,
1802 struct ieee80211_hdr *hdr,
1803 int is_unicast, u8 std_id)
1804{
1805 __le16 *qc;
1806 u16 fc = le16_to_cpu(hdr->frame_control);
1807 __le32 tx_flags = cmd->cmd.tx.tx_flags;
1808
1809 cmd->cmd.tx.stop_time.life_time = TX_CMD_LIFE_TIME_INFINITE;
1810 if (!(ctrl->flags & IEEE80211_TXCTL_NO_ACK)) {
1811 tx_flags |= TX_CMD_FLG_ACK_MSK;
1812 if ((fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_MGMT)
1813 tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
1814 if (ieee80211_is_probe_response(fc) &&
1815 !(le16_to_cpu(hdr->seq_ctrl) & 0xf))
1816 tx_flags |= TX_CMD_FLG_TSF_MSK;
1817 } else {
1818 tx_flags &= (~TX_CMD_FLG_ACK_MSK);
1819 tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
1820 }
1821
Tomas Winkler87e4f7d2008-01-14 17:46:16 -08001822 if (ieee80211_is_back_request(fc))
1823 tx_flags |= TX_CMD_FLG_ACK_MSK | TX_CMD_FLG_IMM_BA_RSP_MASK;
1824
1825
Zhu Yib481de92007-09-25 17:54:57 -07001826 cmd->cmd.tx.sta_id = std_id;
1827 if (ieee80211_get_morefrag(hdr))
1828 tx_flags |= TX_CMD_FLG_MORE_FRAG_MSK;
1829
1830 qc = ieee80211_get_qos_ctrl(hdr);
1831 if (qc) {
1832 cmd->cmd.tx.tid_tspec = (u8) (le16_to_cpu(*qc) & 0xf);
1833 tx_flags &= ~TX_CMD_FLG_SEQ_CTL_MSK;
1834 } else
1835 tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
1836
1837 if (ctrl->flags & IEEE80211_TXCTL_USE_RTS_CTS) {
1838 tx_flags |= TX_CMD_FLG_RTS_MSK;
1839 tx_flags &= ~TX_CMD_FLG_CTS_MSK;
1840 } else if (ctrl->flags & IEEE80211_TXCTL_USE_CTS_PROTECT) {
1841 tx_flags &= ~TX_CMD_FLG_RTS_MSK;
1842 tx_flags |= TX_CMD_FLG_CTS_MSK;
1843 }
1844
1845 if ((tx_flags & TX_CMD_FLG_RTS_MSK) || (tx_flags & TX_CMD_FLG_CTS_MSK))
1846 tx_flags |= TX_CMD_FLG_FULL_TXOP_PROT_MSK;
1847
1848 tx_flags &= ~(TX_CMD_FLG_ANT_SEL_MSK);
1849 if ((fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_MGMT) {
1850 if ((fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_ASSOC_REQ ||
1851 (fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_REASSOC_REQ)
Ian Schrambc434dd2007-10-25 17:15:29 +08001852 cmd->cmd.tx.timeout.pm_frame_timeout = cpu_to_le16(3);
Zhu Yib481de92007-09-25 17:54:57 -07001853 else
Ian Schrambc434dd2007-10-25 17:15:29 +08001854 cmd->cmd.tx.timeout.pm_frame_timeout = cpu_to_le16(2);
Mohamed Abbasab53d8a2008-03-25 16:33:36 -07001855 } else {
Zhu Yib481de92007-09-25 17:54:57 -07001856 cmd->cmd.tx.timeout.pm_frame_timeout = 0;
Mohamed Abbasab53d8a2008-03-25 16:33:36 -07001857 }
Zhu Yib481de92007-09-25 17:54:57 -07001858
1859 cmd->cmd.tx.driver_txop = 0;
1860 cmd->cmd.tx.tx_flags = tx_flags;
1861 cmd->cmd.tx.next_frame_len = 0;
1862}
Tomas Winkler19758be2008-03-12 16:58:51 -07001863static void iwl_update_tx_stats(struct iwl_priv *priv, u16 fc, u16 len)
1864{
1865 /* 0 - mgmt, 1 - cnt, 2 - data */
1866 int idx = (fc & IEEE80211_FCTL_FTYPE) >> 2;
1867 priv->tx_stats[idx].cnt++;
1868 priv->tx_stats[idx].bytes += len;
1869}
Cahill, Ben M6440adb2007-11-29 11:09:55 +08001870/**
1871 * iwl4965_get_sta_id - Find station's index within station table
1872 *
1873 * If new IBSS station, create new entry in station table
1874 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001875static int iwl4965_get_sta_id(struct iwl_priv *priv,
Ben Cahill9fbab512007-11-29 11:09:47 +08001876 struct ieee80211_hdr *hdr)
Zhu Yib481de92007-09-25 17:54:57 -07001877{
1878 int sta_id;
1879 u16 fc = le16_to_cpu(hdr->frame_control);
Joe Perches0795af52007-10-03 17:59:30 -07001880 DECLARE_MAC_BUF(mac);
Zhu Yib481de92007-09-25 17:54:57 -07001881
Cahill, Ben M6440adb2007-11-29 11:09:55 +08001882 /* If this frame is broadcast or management, use broadcast station id */
Zhu Yib481de92007-09-25 17:54:57 -07001883 if (((fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_DATA) ||
1884 is_multicast_ether_addr(hdr->addr1))
Tomas Winkler5425e492008-04-15 16:01:38 -07001885 return priv->hw_params.bcast_sta_id;
Zhu Yib481de92007-09-25 17:54:57 -07001886
1887 switch (priv->iw_mode) {
1888
Cahill, Ben M6440adb2007-11-29 11:09:55 +08001889 /* If we are a client station in a BSS network, use the special
1890 * AP station entry (that's the only station we communicate with) */
Zhu Yib481de92007-09-25 17:54:57 -07001891 case IEEE80211_IF_TYPE_STA:
1892 return IWL_AP_ID;
1893
1894 /* If we are an AP, then find the station, or use BCAST */
1895 case IEEE80211_IF_TYPE_AP:
Tomas Winkler947b13a2008-04-16 16:34:48 -07001896 sta_id = iwl_find_station(priv, hdr->addr1);
Zhu Yib481de92007-09-25 17:54:57 -07001897 if (sta_id != IWL_INVALID_STATION)
1898 return sta_id;
Tomas Winkler5425e492008-04-15 16:01:38 -07001899 return priv->hw_params.bcast_sta_id;
Zhu Yib481de92007-09-25 17:54:57 -07001900
Cahill, Ben M6440adb2007-11-29 11:09:55 +08001901 /* If this frame is going out to an IBSS network, find the station,
1902 * or create a new station table entry */
Zhu Yib481de92007-09-25 17:54:57 -07001903 case IEEE80211_IF_TYPE_IBSS:
Tomas Winkler947b13a2008-04-16 16:34:48 -07001904 sta_id = iwl_find_station(priv, hdr->addr1);
Zhu Yib481de92007-09-25 17:54:57 -07001905 if (sta_id != IWL_INVALID_STATION)
1906 return sta_id;
1907
Cahill, Ben M6440adb2007-11-29 11:09:55 +08001908 /* Create new station table entry */
Ron Rindjunsky67d62032007-11-26 16:14:40 +02001909 sta_id = iwl4965_add_station_flags(priv, hdr->addr1,
1910 0, CMD_ASYNC, NULL);
Zhu Yib481de92007-09-25 17:54:57 -07001911
1912 if (sta_id != IWL_INVALID_STATION)
1913 return sta_id;
1914
Joe Perches0795af52007-10-03 17:59:30 -07001915 IWL_DEBUG_DROP("Station %s not in station map. "
Zhu Yib481de92007-09-25 17:54:57 -07001916 "Defaulting to broadcast...\n",
Joe Perches0795af52007-10-03 17:59:30 -07001917 print_mac(mac, hdr->addr1));
Ester Kummerbf403db2008-05-05 10:22:40 +08001918 iwl_print_hex_dump(priv, IWL_DL_DROP, (u8 *) hdr, sizeof(*hdr));
Tomas Winkler5425e492008-04-15 16:01:38 -07001919 return priv->hw_params.bcast_sta_id;
Zhu Yib481de92007-09-25 17:54:57 -07001920
1921 default:
Ian Schram01ebd062007-10-25 17:15:22 +08001922 IWL_WARNING("Unknown mode of operation: %d", priv->iw_mode);
Tomas Winkler5425e492008-04-15 16:01:38 -07001923 return priv->hw_params.bcast_sta_id;
Zhu Yib481de92007-09-25 17:54:57 -07001924 }
1925}
1926
1927/*
1928 * start REPLY_TX command process
1929 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001930static int iwl4965_tx_skb(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07001931 struct sk_buff *skb, struct ieee80211_tx_control *ctl)
1932{
1933 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001934 struct iwl4965_tfd_frame *tfd;
Zhu Yib481de92007-09-25 17:54:57 -07001935 u32 *control_flags;
1936 int txq_id = ctl->queue;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001937 struct iwl4965_tx_queue *txq = NULL;
1938 struct iwl4965_queue *q = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07001939 dma_addr_t phys_addr;
1940 dma_addr_t txcmd_phys;
Tomas Winkler87e4f7d2008-01-14 17:46:16 -08001941 dma_addr_t scratch_phys;
Tomas Winkler857485c2008-03-21 13:53:44 -07001942 struct iwl_cmd *out_cmd = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07001943 u16 len, idx, len_org;
1944 u8 id, hdr_len, unicast;
1945 u8 sta_id;
1946 u16 seq_number = 0;
1947 u16 fc;
1948 __le16 *qc;
1949 u8 wait_write_ptr = 0;
1950 unsigned long flags;
1951 int rc;
1952
1953 spin_lock_irqsave(&priv->lock, flags);
Tomas Winklerfee12472008-04-03 16:05:21 -07001954 if (iwl_is_rfkill(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07001955 IWL_DEBUG_DROP("Dropping - RF KILL\n");
1956 goto drop_unlock;
1957 }
1958
Johannes Berg32bfd352007-12-19 01:31:26 +01001959 if (!priv->vif) {
1960 IWL_DEBUG_DROP("Dropping - !priv->vif\n");
Zhu Yib481de92007-09-25 17:54:57 -07001961 goto drop_unlock;
1962 }
1963
Johannes Berg8318d782008-01-24 19:38:38 +01001964 if ((ctl->tx_rate->hw_value & 0xFF) == IWL_INVALID_RATE) {
Zhu Yib481de92007-09-25 17:54:57 -07001965 IWL_ERROR("ERROR: No TX rate available.\n");
1966 goto drop_unlock;
1967 }
1968
1969 unicast = !is_multicast_ether_addr(hdr->addr1);
1970 id = 0;
1971
1972 fc = le16_to_cpu(hdr->frame_control);
1973
Tomas Winkler0a6857e2008-03-12 16:58:49 -07001974#ifdef CONFIG_IWLWIFI_DEBUG
Zhu Yib481de92007-09-25 17:54:57 -07001975 if (ieee80211_is_auth(fc))
1976 IWL_DEBUG_TX("Sending AUTH frame\n");
1977 else if (ieee80211_is_assoc_request(fc))
1978 IWL_DEBUG_TX("Sending ASSOC frame\n");
1979 else if (ieee80211_is_reassoc_request(fc))
1980 IWL_DEBUG_TX("Sending REASSOC frame\n");
1981#endif
1982
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08001983 /* drop all data frame if we are not associated */
Gregory Greenman76f39152008-01-23 10:15:21 -08001984 if (((fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_DATA) &&
Tomas Winkler3109ece2008-03-28 16:33:35 -07001985 (!iwl_is_associated(priv) ||
Reinette Chatrea6477242008-02-14 10:40:28 -08001986 ((priv->iw_mode == IEEE80211_IF_TYPE_STA) && !priv->assoc_id) ||
Gregory Greenman76f39152008-01-23 10:15:21 -08001987 !priv->assoc_station_added)) {
Tomas Winkler3109ece2008-03-28 16:33:35 -07001988 IWL_DEBUG_DROP("Dropping - !iwl_is_associated\n");
Zhu Yib481de92007-09-25 17:54:57 -07001989 goto drop_unlock;
1990 }
1991
1992 spin_unlock_irqrestore(&priv->lock, flags);
1993
1994 hdr_len = ieee80211_get_hdrlen(fc);
Cahill, Ben M6440adb2007-11-29 11:09:55 +08001995
1996 /* Find (or create) index into station table for destination station */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001997 sta_id = iwl4965_get_sta_id(priv, hdr);
Zhu Yib481de92007-09-25 17:54:57 -07001998 if (sta_id == IWL_INVALID_STATION) {
Joe Perches0795af52007-10-03 17:59:30 -07001999 DECLARE_MAC_BUF(mac);
2000
2001 IWL_DEBUG_DROP("Dropping - INVALID STATION: %s\n",
2002 print_mac(mac, hdr->addr1));
Zhu Yib481de92007-09-25 17:54:57 -07002003 goto drop;
2004 }
2005
Guy Cohen07bc28e2008-04-23 17:15:03 -07002006 IWL_DEBUG_TX("station Id %d\n", sta_id);
Zhu Yib481de92007-09-25 17:54:57 -07002007
2008 qc = ieee80211_get_qos_ctrl(hdr);
2009 if (qc) {
2010 u8 tid = (u8)(le16_to_cpu(*qc) & 0xf);
2011 seq_number = priv->stations[sta_id].tid[tid].seq_number &
2012 IEEE80211_SCTL_SEQ;
2013 hdr->seq_ctrl = cpu_to_le16(seq_number) |
2014 (hdr->seq_ctrl &
2015 __constant_cpu_to_le16(IEEE80211_SCTL_FRAG));
2016 seq_number += 0x10;
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08002017#ifdef CONFIG_IWL4965_HT
Zhu Yib481de92007-09-25 17:54:57 -07002018 /* aggregation is on for this <sta,tid> */
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02002019 if (ctl->flags & IEEE80211_TXCTL_AMPDU)
Zhu Yib481de92007-09-25 17:54:57 -07002020 txq_id = priv->stations[sta_id].tid[tid].agg.txq_id;
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02002021 priv->stations[sta_id].tid[tid].tfds_in_queue++;
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08002022#endif /* CONFIG_IWL4965_HT */
Zhu Yib481de92007-09-25 17:54:57 -07002023 }
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002024
2025 /* Descriptor for chosen Tx queue */
Zhu Yib481de92007-09-25 17:54:57 -07002026 txq = &priv->txq[txq_id];
2027 q = &txq->q;
2028
2029 spin_lock_irqsave(&priv->lock, flags);
2030
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002031 /* Set up first empty TFD within this queue's circular TFD buffer */
Tomas Winklerfc4b6852007-10-25 17:15:24 +08002032 tfd = &txq->bd[q->write_ptr];
Zhu Yib481de92007-09-25 17:54:57 -07002033 memset(tfd, 0, sizeof(*tfd));
2034 control_flags = (u32 *) tfd;
Tomas Winklerfc4b6852007-10-25 17:15:24 +08002035 idx = get_cmd_index(q, q->write_ptr, 0);
Zhu Yib481de92007-09-25 17:54:57 -07002036
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002037 /* Set up driver data for this TFD */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002038 memset(&(txq->txb[q->write_ptr]), 0, sizeof(struct iwl4965_tx_info));
Tomas Winklerfc4b6852007-10-25 17:15:24 +08002039 txq->txb[q->write_ptr].skb[0] = skb;
2040 memcpy(&(txq->txb[q->write_ptr].status.control),
Zhu Yib481de92007-09-25 17:54:57 -07002041 ctl, sizeof(struct ieee80211_tx_control));
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002042
2043 /* Set up first empty entry in queue's array of Tx/cmd buffers */
Zhu Yib481de92007-09-25 17:54:57 -07002044 out_cmd = &txq->cmd[idx];
2045 memset(&out_cmd->hdr, 0, sizeof(out_cmd->hdr));
2046 memset(&out_cmd->cmd.tx, 0, sizeof(out_cmd->cmd.tx));
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002047
2048 /*
2049 * Set up the Tx-command (not MAC!) header.
2050 * Store the chosen Tx queue and TFD index within the sequence field;
2051 * after Tx, uCode's Tx response will return this value so driver can
2052 * locate the frame within the tx queue and do post-tx processing.
2053 */
Zhu Yib481de92007-09-25 17:54:57 -07002054 out_cmd->hdr.cmd = REPLY_TX;
2055 out_cmd->hdr.sequence = cpu_to_le16((u16)(QUEUE_TO_SEQ(txq_id) |
Tomas Winklerfc4b6852007-10-25 17:15:24 +08002056 INDEX_TO_SEQ(q->write_ptr)));
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002057
2058 /* Copy MAC header from skb into command buffer */
Zhu Yib481de92007-09-25 17:54:57 -07002059 memcpy(out_cmd->cmd.tx.hdr, hdr, hdr_len);
2060
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002061 /*
2062 * Use the first empty entry in this queue's command buffer array
2063 * to contain the Tx command and MAC header concatenated together
2064 * (payload data will be in another buffer).
2065 * Size of this varies, due to varying MAC header length.
2066 * If end is not dword aligned, we'll have 2 extra bytes at the end
2067 * of the MAC header (device reads on dword boundaries).
2068 * We'll tell device about this padding later.
2069 */
Tomas Winkler5425e492008-04-15 16:01:38 -07002070 len = priv->hw_params.tx_cmd_len +
Tomas Winkler857485c2008-03-21 13:53:44 -07002071 sizeof(struct iwl_cmd_header) + hdr_len;
Zhu Yib481de92007-09-25 17:54:57 -07002072
2073 len_org = len;
2074 len = (len + 3) & ~3;
2075
2076 if (len_org != len)
2077 len_org = 1;
2078 else
2079 len_org = 0;
2080
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002081 /* Physical address of this Tx command's header (not MAC header!),
2082 * within command buffer array. */
Tomas Winkler857485c2008-03-21 13:53:44 -07002083 txcmd_phys = txq->dma_addr_cmd + sizeof(struct iwl_cmd) * idx +
2084 offsetof(struct iwl_cmd, hdr);
Zhu Yib481de92007-09-25 17:54:57 -07002085
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002086 /* Add buffer containing Tx command and MAC(!) header to TFD's
2087 * first entry */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002088 iwl4965_hw_txq_attach_buf_to_tfd(priv, tfd, txcmd_phys, len);
Zhu Yib481de92007-09-25 17:54:57 -07002089
2090 if (!(ctl->flags & IEEE80211_TXCTL_DO_NOT_ENCRYPT))
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07002091 iwl4965_build_tx_cmd_hwcrypto(priv, ctl, out_cmd, skb, sta_id);
Zhu Yib481de92007-09-25 17:54:57 -07002092
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002093 /* Set up TFD's 2nd entry to point directly to remainder of skb,
2094 * if any (802.11 null frames have no payload). */
Zhu Yib481de92007-09-25 17:54:57 -07002095 len = skb->len - hdr_len;
2096 if (len) {
2097 phys_addr = pci_map_single(priv->pci_dev, skb->data + hdr_len,
2098 len, PCI_DMA_TODEVICE);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002099 iwl4965_hw_txq_attach_buf_to_tfd(priv, tfd, phys_addr, len);
Zhu Yib481de92007-09-25 17:54:57 -07002100 }
2101
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002102 /* Tell 4965 about any 2-byte padding after MAC header */
Zhu Yib481de92007-09-25 17:54:57 -07002103 if (len_org)
2104 out_cmd->cmd.tx.tx_flags |= TX_CMD_FLG_MH_PAD_MSK;
2105
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002106 /* Total # bytes to be transmitted */
Zhu Yib481de92007-09-25 17:54:57 -07002107 len = (u16)skb->len;
2108 out_cmd->cmd.tx.len = cpu_to_le16(len);
2109
2110 /* TODO need this for burst mode later on */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002111 iwl4965_build_tx_cmd_basic(priv, out_cmd, ctl, hdr, unicast, sta_id);
Zhu Yib481de92007-09-25 17:54:57 -07002112
2113 /* set is_hcca to 0; it probably will never be implemented */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002114 iwl4965_hw_build_tx_cmd_rate(priv, out_cmd, ctl, hdr, sta_id, 0);
Zhu Yib481de92007-09-25 17:54:57 -07002115
Tomas Winkler19758be2008-03-12 16:58:51 -07002116 iwl_update_tx_stats(priv, fc, len);
2117
Tomas Winkler857485c2008-03-21 13:53:44 -07002118 scratch_phys = txcmd_phys + sizeof(struct iwl_cmd_header) +
Tomas Winkler87e4f7d2008-01-14 17:46:16 -08002119 offsetof(struct iwl4965_tx_cmd, scratch);
2120 out_cmd->cmd.tx.dram_lsb_ptr = cpu_to_le32(scratch_phys);
2121 out_cmd->cmd.tx.dram_msb_ptr = iwl_get_dma_hi_address(scratch_phys);
2122
Zhu Yib481de92007-09-25 17:54:57 -07002123 if (!ieee80211_get_morefrag(hdr)) {
2124 txq->need_update = 1;
2125 if (qc) {
2126 u8 tid = (u8)(le16_to_cpu(*qc) & 0xf);
2127 priv->stations[sta_id].tid[tid].seq_number = seq_number;
2128 }
2129 } else {
2130 wait_write_ptr = 1;
2131 txq->need_update = 0;
2132 }
2133
Ester Kummerbf403db2008-05-05 10:22:40 +08002134 iwl_print_hex_dump(priv, IWL_DL_TX, out_cmd->cmd.payload,
Zhu Yib481de92007-09-25 17:54:57 -07002135 sizeof(out_cmd->cmd.tx));
2136
Ester Kummerbf403db2008-05-05 10:22:40 +08002137 iwl_print_hex_dump(priv, IWL_DL_TX, (u8 *)out_cmd->cmd.tx.hdr,
Zhu Yib481de92007-09-25 17:54:57 -07002138 ieee80211_get_hdrlen(fc));
2139
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002140 /* Set up entry for this TFD in Tx byte-count array */
Tomas Winklere2a722e2008-04-14 21:16:10 -07002141 priv->cfg->ops->lib->txq_update_byte_cnt_tbl(priv, txq, len);
Zhu Yib481de92007-09-25 17:54:57 -07002142
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002143 /* Tell device the write index *just past* this latest filled TFD */
Tomas Winklerc54b6792008-03-06 17:36:53 -08002144 q->write_ptr = iwl_queue_inc_wrap(q->write_ptr, q->n_bd);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002145 rc = iwl4965_tx_queue_update_write_ptr(priv, txq);
Zhu Yib481de92007-09-25 17:54:57 -07002146 spin_unlock_irqrestore(&priv->lock, flags);
2147
2148 if (rc)
2149 return rc;
2150
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002151 if ((iwl4965_queue_space(q) < q->high_mark)
Zhu Yib481de92007-09-25 17:54:57 -07002152 && priv->mac80211_registered) {
2153 if (wait_write_ptr) {
2154 spin_lock_irqsave(&priv->lock, flags);
2155 txq->need_update = 1;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002156 iwl4965_tx_queue_update_write_ptr(priv, txq);
Zhu Yib481de92007-09-25 17:54:57 -07002157 spin_unlock_irqrestore(&priv->lock, flags);
2158 }
2159
2160 ieee80211_stop_queue(priv->hw, ctl->queue);
2161 }
2162
2163 return 0;
2164
2165drop_unlock:
2166 spin_unlock_irqrestore(&priv->lock, flags);
2167drop:
2168 return -1;
2169}
2170
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002171static void iwl4965_set_rate(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002172{
Johannes Berg8318d782008-01-24 19:38:38 +01002173 const struct ieee80211_supported_band *hw = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07002174 struct ieee80211_rate *rate;
2175 int i;
2176
Emmanuel Grumbachd1141df2008-04-21 15:42:00 -07002177 hw = iwl_get_hw_mode(priv, priv->band);
Saleem Abdulrasoolc4ba9622007-11-18 23:59:08 -08002178 if (!hw) {
2179 IWL_ERROR("Failed to set rate: unable to get hw mode\n");
2180 return;
2181 }
Zhu Yib481de92007-09-25 17:54:57 -07002182
2183 priv->active_rate = 0;
2184 priv->active_rate_basic = 0;
2185
Johannes Berg8318d782008-01-24 19:38:38 +01002186 for (i = 0; i < hw->n_bitrates; i++) {
2187 rate = &(hw->bitrates[i]);
2188 if (rate->hw_value < IWL_RATE_COUNT)
2189 priv->active_rate |= (1 << rate->hw_value);
Zhu Yib481de92007-09-25 17:54:57 -07002190 }
2191
2192 IWL_DEBUG_RATE("Set active_rate = %0x, active_rate_basic = %0x\n",
2193 priv->active_rate, priv->active_rate_basic);
2194
2195 /*
2196 * If a basic rate is configured, then use it (adding IWL_RATE_1M_MASK)
2197 * otherwise set it to the default of all CCK rates and 6, 12, 24 for
2198 * OFDM
2199 */
2200 if (priv->active_rate_basic & IWL_CCK_BASIC_RATES_MASK)
2201 priv->staging_rxon.cck_basic_rates =
2202 ((priv->active_rate_basic &
2203 IWL_CCK_RATES_MASK) >> IWL_FIRST_CCK_RATE) & 0xF;
2204 else
2205 priv->staging_rxon.cck_basic_rates =
2206 (IWL_CCK_BASIC_RATES_MASK >> IWL_FIRST_CCK_RATE) & 0xF;
2207
2208 if (priv->active_rate_basic & IWL_OFDM_BASIC_RATES_MASK)
2209 priv->staging_rxon.ofdm_basic_rates =
2210 ((priv->active_rate_basic &
2211 (IWL_OFDM_BASIC_RATES_MASK | IWL_RATE_6M_MASK)) >>
2212 IWL_FIRST_OFDM_RATE) & 0xFF;
2213 else
2214 priv->staging_rxon.ofdm_basic_rates =
2215 (IWL_OFDM_BASIC_RATES_MASK >> IWL_FIRST_OFDM_RATE) & 0xFF;
2216}
2217
Mohamed Abbasad97edd2008-03-28 16:21:06 -07002218void iwl4965_radio_kill_sw(struct iwl_priv *priv, int disable_radio)
Zhu Yib481de92007-09-25 17:54:57 -07002219{
2220 unsigned long flags;
2221
2222 if (!!disable_radio == test_bit(STATUS_RF_KILL_SW, &priv->status))
2223 return;
2224
2225 IWL_DEBUG_RF_KILL("Manual SW RF KILL set to: RADIO %s\n",
2226 disable_radio ? "OFF" : "ON");
2227
2228 if (disable_radio) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002229 iwl4965_scan_cancel(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002230 /* FIXME: This is a workaround for AP */
2231 if (priv->iw_mode != IEEE80211_IF_TYPE_AP) {
2232 spin_lock_irqsave(&priv->lock, flags);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07002233 iwl_write32(priv, CSR_UCODE_DRV_GP1_SET,
Zhu Yib481de92007-09-25 17:54:57 -07002234 CSR_UCODE_SW_BIT_RFKILL);
2235 spin_unlock_irqrestore(&priv->lock, flags);
Mohamed Abbasad97edd2008-03-28 16:21:06 -07002236 /* call the host command only if no hw rf-kill set */
Mohamed Abbas59003832008-04-15 16:01:46 -07002237 if (!test_bit(STATUS_RF_KILL_HW, &priv->status) &&
2238 iwl_is_ready(priv))
Mohamed Abbasad97edd2008-03-28 16:21:06 -07002239 iwl4965_send_card_state(priv,
2240 CARD_STATE_CMD_DISABLE,
2241 0);
Zhu Yib481de92007-09-25 17:54:57 -07002242 set_bit(STATUS_RF_KILL_SW, &priv->status);
Mohamed Abbasad97edd2008-03-28 16:21:06 -07002243
2244 /* make sure mac80211 stop sending Tx frame */
2245 if (priv->mac80211_registered)
2246 ieee80211_stop_queues(priv->hw);
Zhu Yib481de92007-09-25 17:54:57 -07002247 }
2248 return;
2249 }
2250
2251 spin_lock_irqsave(&priv->lock, flags);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07002252 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
Zhu Yib481de92007-09-25 17:54:57 -07002253
2254 clear_bit(STATUS_RF_KILL_SW, &priv->status);
2255 spin_unlock_irqrestore(&priv->lock, flags);
2256
2257 /* wake up ucode */
2258 msleep(10);
2259
2260 spin_lock_irqsave(&priv->lock, flags);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07002261 iwl_read32(priv, CSR_UCODE_DRV_GP1);
2262 if (!iwl_grab_nic_access(priv))
2263 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002264 spin_unlock_irqrestore(&priv->lock, flags);
2265
2266 if (test_bit(STATUS_RF_KILL_HW, &priv->status)) {
2267 IWL_DEBUG_RF_KILL("Can not turn radio back on - "
2268 "disabled by HW switch\n");
2269 return;
2270 }
2271
2272 queue_work(priv->workqueue, &priv->restart);
2273 return;
2274}
2275
Zhu Yib481de92007-09-25 17:54:57 -07002276#define IWL_PACKET_RETRY_TIME HZ
2277
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002278int iwl4965_is_duplicate_packet(struct iwl_priv *priv, struct ieee80211_hdr *header)
Zhu Yib481de92007-09-25 17:54:57 -07002279{
2280 u16 sc = le16_to_cpu(header->seq_ctrl);
2281 u16 seq = (sc & IEEE80211_SCTL_SEQ) >> 4;
2282 u16 frag = sc & IEEE80211_SCTL_FRAG;
2283 u16 *last_seq, *last_frag;
2284 unsigned long *last_time;
2285
2286 switch (priv->iw_mode) {
2287 case IEEE80211_IF_TYPE_IBSS:{
2288 struct list_head *p;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002289 struct iwl4965_ibss_seq *entry = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07002290 u8 *mac = header->addr2;
2291 int index = mac[5] & (IWL_IBSS_MAC_HASH_SIZE - 1);
2292
2293 __list_for_each(p, &priv->ibss_mac_hash[index]) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002294 entry = list_entry(p, struct iwl4965_ibss_seq, list);
Zhu Yib481de92007-09-25 17:54:57 -07002295 if (!compare_ether_addr(entry->mac, mac))
2296 break;
2297 }
2298 if (p == &priv->ibss_mac_hash[index]) {
2299 entry = kzalloc(sizeof(*entry), GFP_ATOMIC);
2300 if (!entry) {
Ian Schrambc434dd2007-10-25 17:15:29 +08002301 IWL_ERROR("Cannot malloc new mac entry\n");
Zhu Yib481de92007-09-25 17:54:57 -07002302 return 0;
2303 }
2304 memcpy(entry->mac, mac, ETH_ALEN);
2305 entry->seq_num = seq;
2306 entry->frag_num = frag;
2307 entry->packet_time = jiffies;
Ian Schrambc434dd2007-10-25 17:15:29 +08002308 list_add(&entry->list, &priv->ibss_mac_hash[index]);
Zhu Yib481de92007-09-25 17:54:57 -07002309 return 0;
2310 }
2311 last_seq = &entry->seq_num;
2312 last_frag = &entry->frag_num;
2313 last_time = &entry->packet_time;
2314 break;
2315 }
2316 case IEEE80211_IF_TYPE_STA:
2317 last_seq = &priv->last_seq_num;
2318 last_frag = &priv->last_frag_num;
2319 last_time = &priv->last_packet_time;
2320 break;
2321 default:
2322 return 0;
2323 }
2324 if ((*last_seq == seq) &&
2325 time_after(*last_time + IWL_PACKET_RETRY_TIME, jiffies)) {
2326 if (*last_frag == frag)
2327 goto drop;
2328 if (*last_frag + 1 != frag)
2329 /* out-of-order fragment */
2330 goto drop;
2331 } else
2332 *last_seq = seq;
2333
2334 *last_frag = frag;
2335 *last_time = jiffies;
2336 return 0;
2337
2338 drop:
2339 return 1;
2340}
2341
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08002342#ifdef CONFIG_IWL4965_SPECTRUM_MEASUREMENT
Zhu Yib481de92007-09-25 17:54:57 -07002343
2344#include "iwl-spectrum.h"
2345
2346#define BEACON_TIME_MASK_LOW 0x00FFFFFF
2347#define BEACON_TIME_MASK_HIGH 0xFF000000
2348#define TIME_UNIT 1024
2349
2350/*
2351 * extended beacon time format
2352 * time in usec will be changed into a 32-bit value in 8:24 format
2353 * the high 1 byte is the beacon counts
2354 * the lower 3 bytes is the time in usec within one beacon interval
2355 */
2356
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002357static u32 iwl4965_usecs_to_beacons(u32 usec, u32 beacon_interval)
Zhu Yib481de92007-09-25 17:54:57 -07002358{
2359 u32 quot;
2360 u32 rem;
2361 u32 interval = beacon_interval * 1024;
2362
2363 if (!interval || !usec)
2364 return 0;
2365
2366 quot = (usec / interval) & (BEACON_TIME_MASK_HIGH >> 24);
2367 rem = (usec % interval) & BEACON_TIME_MASK_LOW;
2368
2369 return (quot << 24) + rem;
2370}
2371
2372/* base is usually what we get from ucode with each received frame,
2373 * the same as HW timer counter counting down
2374 */
2375
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002376static __le32 iwl4965_add_beacon_time(u32 base, u32 addon, u32 beacon_interval)
Zhu Yib481de92007-09-25 17:54:57 -07002377{
2378 u32 base_low = base & BEACON_TIME_MASK_LOW;
2379 u32 addon_low = addon & BEACON_TIME_MASK_LOW;
2380 u32 interval = beacon_interval * TIME_UNIT;
2381 u32 res = (base & BEACON_TIME_MASK_HIGH) +
2382 (addon & BEACON_TIME_MASK_HIGH);
2383
2384 if (base_low > addon_low)
2385 res += base_low - addon_low;
2386 else if (base_low < addon_low) {
2387 res += interval + base_low - addon_low;
2388 res += (1 << 24);
2389 } else
2390 res += (1 << 24);
2391
2392 return cpu_to_le32(res);
2393}
2394
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002395static int iwl4965_get_measurement(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07002396 struct ieee80211_measurement_params *params,
2397 u8 type)
2398{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002399 struct iwl4965_spectrum_cmd spectrum;
Tomas Winklerdb11d632008-05-05 10:22:33 +08002400 struct iwl_rx_packet *res;
Tomas Winkler857485c2008-03-21 13:53:44 -07002401 struct iwl_host_cmd cmd = {
Zhu Yib481de92007-09-25 17:54:57 -07002402 .id = REPLY_SPECTRUM_MEASUREMENT_CMD,
2403 .data = (void *)&spectrum,
2404 .meta.flags = CMD_WANT_SKB,
2405 };
2406 u32 add_time = le64_to_cpu(params->start_time);
2407 int rc;
2408 int spectrum_resp_status;
2409 int duration = le16_to_cpu(params->duration);
2410
Tomas Winkler3109ece2008-03-28 16:33:35 -07002411 if (iwl_is_associated(priv))
Zhu Yib481de92007-09-25 17:54:57 -07002412 add_time =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002413 iwl4965_usecs_to_beacons(
Zhu Yib481de92007-09-25 17:54:57 -07002414 le64_to_cpu(params->start_time) - priv->last_tsf,
2415 le16_to_cpu(priv->rxon_timing.beacon_interval));
2416
2417 memset(&spectrum, 0, sizeof(spectrum));
2418
2419 spectrum.channel_count = cpu_to_le16(1);
2420 spectrum.flags =
2421 RXON_FLG_TSF2HOST_MSK | RXON_FLG_ANT_A_MSK | RXON_FLG_DIS_DIV_MSK;
2422 spectrum.filter_flags = MEASUREMENT_FILTER_FLAG;
2423 cmd.len = sizeof(spectrum);
2424 spectrum.len = cpu_to_le16(cmd.len - sizeof(spectrum.len));
2425
Tomas Winkler3109ece2008-03-28 16:33:35 -07002426 if (iwl_is_associated(priv))
Zhu Yib481de92007-09-25 17:54:57 -07002427 spectrum.start_time =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002428 iwl4965_add_beacon_time(priv->last_beacon_time,
Zhu Yib481de92007-09-25 17:54:57 -07002429 add_time,
2430 le16_to_cpu(priv->rxon_timing.beacon_interval));
2431 else
2432 spectrum.start_time = 0;
2433
2434 spectrum.channels[0].duration = cpu_to_le32(duration * TIME_UNIT);
2435 spectrum.channels[0].channel = params->channel;
2436 spectrum.channels[0].type = type;
2437 if (priv->active_rxon.flags & RXON_FLG_BAND_24G_MSK)
2438 spectrum.flags |= RXON_FLG_BAND_24G_MSK |
2439 RXON_FLG_AUTO_DETECT_MSK | RXON_FLG_TGG_PROTECT_MSK;
2440
Tomas Winkler857485c2008-03-21 13:53:44 -07002441 rc = iwl_send_cmd_sync(priv, &cmd);
Zhu Yib481de92007-09-25 17:54:57 -07002442 if (rc)
2443 return rc;
2444
Tomas Winklerdb11d632008-05-05 10:22:33 +08002445 res = (struct iwl_rx_packet *)cmd.meta.u.skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07002446 if (res->hdr.flags & IWL_CMD_FAILED_MSK) {
2447 IWL_ERROR("Bad return from REPLY_RX_ON_ASSOC command\n");
2448 rc = -EIO;
2449 }
2450
2451 spectrum_resp_status = le16_to_cpu(res->u.spectrum.status);
2452 switch (spectrum_resp_status) {
2453 case 0: /* Command will be handled */
2454 if (res->u.spectrum.id != 0xff) {
2455 IWL_DEBUG_INFO
2456 ("Replaced existing measurement: %d\n",
2457 res->u.spectrum.id);
2458 priv->measurement_status &= ~MEASUREMENT_READY;
2459 }
2460 priv->measurement_status |= MEASUREMENT_ACTIVE;
2461 rc = 0;
2462 break;
2463
2464 case 1: /* Command will not be handled */
2465 rc = -EAGAIN;
2466 break;
2467 }
2468
2469 dev_kfree_skb_any(cmd.meta.u.skb);
2470
2471 return rc;
2472}
2473#endif
2474
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002475static void iwl4965_txstatus_to_ieee(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002476 struct iwl4965_tx_info *tx_sta)
Zhu Yib481de92007-09-25 17:54:57 -07002477{
2478
2479 tx_sta->status.ack_signal = 0;
2480 tx_sta->status.excessive_retries = 0;
Zhu Yib481de92007-09-25 17:54:57 -07002481
2482 if (in_interrupt())
2483 ieee80211_tx_status_irqsafe(priv->hw,
2484 tx_sta->skb[0], &(tx_sta->status));
2485 else
2486 ieee80211_tx_status(priv->hw,
2487 tx_sta->skb[0], &(tx_sta->status));
2488
2489 tx_sta->skb[0] = NULL;
2490}
2491
2492/**
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002493 * iwl4965_tx_queue_reclaim - Reclaim Tx queue entries already Tx'd
Zhu Yib481de92007-09-25 17:54:57 -07002494 *
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002495 * When FW advances 'R' index, all entries between old and new 'R' index
2496 * need to be reclaimed. As result, some free space forms. If there is
2497 * enough free space (> low mark), wake the stack that feeds us.
Zhu Yib481de92007-09-25 17:54:57 -07002498 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002499int iwl4965_tx_queue_reclaim(struct iwl_priv *priv, int txq_id, int index)
Zhu Yib481de92007-09-25 17:54:57 -07002500{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002501 struct iwl4965_tx_queue *txq = &priv->txq[txq_id];
2502 struct iwl4965_queue *q = &txq->q;
Zhu Yib481de92007-09-25 17:54:57 -07002503 int nfreed = 0;
2504
2505 if ((index >= q->n_bd) || (x2_queue_used(q, index) == 0)) {
2506 IWL_ERROR("Read index for DMA queue txq id (%d), index %d, "
2507 "is out of range [0-%d] %d %d.\n", txq_id,
Tomas Winklerfc4b6852007-10-25 17:15:24 +08002508 index, q->n_bd, q->write_ptr, q->read_ptr);
Zhu Yib481de92007-09-25 17:54:57 -07002509 return 0;
2510 }
2511
Tomas Winklerc54b6792008-03-06 17:36:53 -08002512 for (index = iwl_queue_inc_wrap(index, q->n_bd);
Tomas Winklerfc4b6852007-10-25 17:15:24 +08002513 q->read_ptr != index;
Tomas Winklerc54b6792008-03-06 17:36:53 -08002514 q->read_ptr = iwl_queue_inc_wrap(q->read_ptr, q->n_bd)) {
Zhu Yib481de92007-09-25 17:54:57 -07002515 if (txq_id != IWL_CMD_QUEUE_NUM) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002516 iwl4965_txstatus_to_ieee(priv,
Tomas Winklerfc4b6852007-10-25 17:15:24 +08002517 &(txq->txb[txq->q.read_ptr]));
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002518 iwl4965_hw_txq_free_tfd(priv, txq);
Zhu Yib481de92007-09-25 17:54:57 -07002519 } else if (nfreed > 1) {
2520 IWL_ERROR("HCMD skipped: index (%d) %d %d\n", index,
Tomas Winklerfc4b6852007-10-25 17:15:24 +08002521 q->write_ptr, q->read_ptr);
Zhu Yib481de92007-09-25 17:54:57 -07002522 queue_work(priv->workqueue, &priv->restart);
2523 }
2524 nfreed++;
2525 }
2526
Zhu Yib481de92007-09-25 17:54:57 -07002527 return nfreed;
2528}
2529
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002530static int iwl4965_is_tx_success(u32 status)
Zhu Yib481de92007-09-25 17:54:57 -07002531{
2532 status &= TX_STATUS_MSK;
2533 return (status == TX_STATUS_SUCCESS)
2534 || (status == TX_STATUS_DIRECT_DONE);
2535}
2536
2537/******************************************************************************
2538 *
2539 * Generic RX handler implementations
2540 *
2541 ******************************************************************************/
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08002542#ifdef CONFIG_IWL4965_HT
Zhu Yib481de92007-09-25 17:54:57 -07002543
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002544static inline int iwl4965_get_ra_sta_id(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07002545 struct ieee80211_hdr *hdr)
2546{
2547 if (priv->iw_mode == IEEE80211_IF_TYPE_STA)
2548 return IWL_AP_ID;
2549 else {
2550 u8 *da = ieee80211_get_DA(hdr);
Tomas Winkler947b13a2008-04-16 16:34:48 -07002551 return iwl_find_station(priv, da);
Zhu Yib481de92007-09-25 17:54:57 -07002552 }
2553}
2554
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002555static struct ieee80211_hdr *iwl4965_tx_queue_get_hdr(
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002556 struct iwl_priv *priv, int txq_id, int idx)
Zhu Yib481de92007-09-25 17:54:57 -07002557{
2558 if (priv->txq[txq_id].txb[idx].skb[0])
2559 return (struct ieee80211_hdr *)priv->txq[txq_id].
2560 txb[idx].skb[0]->data;
2561 return NULL;
2562}
2563
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002564static inline u32 iwl4965_get_scd_ssn(struct iwl4965_tx_resp *tx_resp)
Zhu Yib481de92007-09-25 17:54:57 -07002565{
2566 __le32 *scd_ssn = (__le32 *)((u32 *)&tx_resp->status +
2567 tx_resp->frame_count);
2568 return le32_to_cpu(*scd_ssn) & MAX_SN;
2569
2570}
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002571
2572/**
2573 * iwl4965_tx_status_reply_tx - Handle Tx rspnse for frames in aggregation queue
2574 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002575static int iwl4965_tx_status_reply_tx(struct iwl_priv *priv,
Tomas Winkler6def9762008-05-05 10:22:31 +08002576 struct iwl_ht_agg *agg,
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02002577 struct iwl4965_tx_resp_agg *tx_resp,
Zhu Yib481de92007-09-25 17:54:57 -07002578 u16 start_idx)
2579{
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02002580 u16 status;
2581 struct agg_tx_status *frame_status = &tx_resp->status;
Zhu Yib481de92007-09-25 17:54:57 -07002582 struct ieee80211_tx_status *tx_status = NULL;
2583 struct ieee80211_hdr *hdr = NULL;
2584 int i, sh;
2585 int txq_id, idx;
2586 u16 seq;
2587
2588 if (agg->wait_for_ba)
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002589 IWL_DEBUG_TX_REPLY("got tx response w/o block-ack\n");
Zhu Yib481de92007-09-25 17:54:57 -07002590
2591 agg->frame_count = tx_resp->frame_count;
2592 agg->start_idx = start_idx;
2593 agg->rate_n_flags = le32_to_cpu(tx_resp->rate_n_flags);
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02002594 agg->bitmap = 0;
Zhu Yib481de92007-09-25 17:54:57 -07002595
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002596 /* # frames attempted by Tx command */
Zhu Yib481de92007-09-25 17:54:57 -07002597 if (agg->frame_count == 1) {
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002598 /* Only one frame was attempted; no block-ack will arrive */
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02002599 status = le16_to_cpu(frame_status[0].status);
2600 seq = le16_to_cpu(frame_status[0].sequence);
2601 idx = SEQ_TO_INDEX(seq);
2602 txq_id = SEQ_TO_QUEUE(seq);
Zhu Yib481de92007-09-25 17:54:57 -07002603
Zhu Yib481de92007-09-25 17:54:57 -07002604 /* FIXME: code repetition */
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02002605 IWL_DEBUG_TX_REPLY("FrameCnt = %d, StartIdx=%d idx=%d\n",
2606 agg->frame_count, agg->start_idx, idx);
Zhu Yib481de92007-09-25 17:54:57 -07002607
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02002608 tx_status = &(priv->txq[txq_id].txb[idx].status);
Zhu Yib481de92007-09-25 17:54:57 -07002609 tx_status->retry_count = tx_resp->failure_frame;
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02002610 tx_status->control.flags &= ~IEEE80211_TXCTL_AMPDU;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002611 tx_status->flags = iwl4965_is_tx_success(status)?
Zhu Yib481de92007-09-25 17:54:57 -07002612 IEEE80211_TX_STATUS_ACK : 0;
Ron Rindjunsky4c424e42008-03-04 18:09:27 -08002613 iwl4965_hwrate_to_tx_control(priv,
2614 le32_to_cpu(tx_resp->rate_n_flags),
2615 &tx_status->control);
Zhu Yib481de92007-09-25 17:54:57 -07002616 /* FIXME: code repetition end */
2617
2618 IWL_DEBUG_TX_REPLY("1 Frame 0x%x failure :%d\n",
2619 status & 0xff, tx_resp->failure_frame);
2620 IWL_DEBUG_TX_REPLY("Rate Info rate_n_flags=%x\n",
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002621 iwl4965_hw_get_rate_n_flags(tx_resp->rate_n_flags));
Zhu Yib481de92007-09-25 17:54:57 -07002622
2623 agg->wait_for_ba = 0;
2624 } else {
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002625 /* Two or more frames were attempted; expect block-ack */
Zhu Yib481de92007-09-25 17:54:57 -07002626 u64 bitmap = 0;
2627 int start = agg->start_idx;
2628
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002629 /* Construct bit-map of pending frames within Tx window */
Zhu Yib481de92007-09-25 17:54:57 -07002630 for (i = 0; i < agg->frame_count; i++) {
2631 u16 sc;
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02002632 status = le16_to_cpu(frame_status[i].status);
2633 seq = le16_to_cpu(frame_status[i].sequence);
Zhu Yib481de92007-09-25 17:54:57 -07002634 idx = SEQ_TO_INDEX(seq);
2635 txq_id = SEQ_TO_QUEUE(seq);
2636
2637 if (status & (AGG_TX_STATE_FEW_BYTES_MSK |
2638 AGG_TX_STATE_ABORT_MSK))
2639 continue;
2640
2641 IWL_DEBUG_TX_REPLY("FrameCnt = %d, txq_id=%d idx=%d\n",
2642 agg->frame_count, txq_id, idx);
2643
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002644 hdr = iwl4965_tx_queue_get_hdr(priv, txq_id, idx);
Zhu Yib481de92007-09-25 17:54:57 -07002645
2646 sc = le16_to_cpu(hdr->seq_ctrl);
2647 if (idx != (SEQ_TO_SN(sc) & 0xff)) {
2648 IWL_ERROR("BUG_ON idx doesn't match seq control"
2649 " idx=%d, seq_idx=%d, seq=%d\n",
2650 idx, SEQ_TO_SN(sc),
2651 hdr->seq_ctrl);
2652 return -1;
2653 }
2654
2655 IWL_DEBUG_TX_REPLY("AGG Frame i=%d idx %d seq=%d\n",
2656 i, idx, SEQ_TO_SN(sc));
2657
2658 sh = idx - start;
2659 if (sh > 64) {
2660 sh = (start - idx) + 0xff;
2661 bitmap = bitmap << sh;
2662 sh = 0;
2663 start = idx;
2664 } else if (sh < -64)
2665 sh = 0xff - (start - idx);
2666 else if (sh < 0) {
2667 sh = start - idx;
2668 start = idx;
2669 bitmap = bitmap << sh;
2670 sh = 0;
2671 }
2672 bitmap |= (1 << sh);
2673 IWL_DEBUG_TX_REPLY("start=%d bitmap=0x%x\n",
2674 start, (u32)(bitmap & 0xFFFFFFFF));
2675 }
2676
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02002677 agg->bitmap = bitmap;
Zhu Yib481de92007-09-25 17:54:57 -07002678 agg->start_idx = start;
2679 agg->rate_n_flags = le32_to_cpu(tx_resp->rate_n_flags);
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02002680 IWL_DEBUG_TX_REPLY("Frames %d start_idx=%d bitmap=0x%llx\n",
Zhu Yib481de92007-09-25 17:54:57 -07002681 agg->frame_count, agg->start_idx,
John W. Linville06501d22008-04-01 17:38:47 -04002682 (unsigned long long)agg->bitmap);
Zhu Yib481de92007-09-25 17:54:57 -07002683
2684 if (bitmap)
2685 agg->wait_for_ba = 1;
2686 }
2687 return 0;
2688}
2689#endif
Zhu Yib481de92007-09-25 17:54:57 -07002690
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002691/**
2692 * iwl4965_rx_reply_tx - Handle standard (non-aggregation) Tx response
2693 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002694static void iwl4965_rx_reply_tx(struct iwl_priv *priv,
Tomas Winklera55360e2008-05-05 10:22:28 +08002695 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07002696{
Tomas Winklerdb11d632008-05-05 10:22:33 +08002697 struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07002698 u16 sequence = le16_to_cpu(pkt->hdr.sequence);
2699 int txq_id = SEQ_TO_QUEUE(sequence);
2700 int index = SEQ_TO_INDEX(sequence);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002701 struct iwl4965_tx_queue *txq = &priv->txq[txq_id];
Zhu Yib481de92007-09-25 17:54:57 -07002702 struct ieee80211_tx_status *tx_status;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002703 struct iwl4965_tx_resp *tx_resp = (void *)&pkt->u.raw[0];
Zhu Yib481de92007-09-25 17:54:57 -07002704 u32 status = le32_to_cpu(tx_resp->status);
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08002705#ifdef CONFIG_IWL4965_HT
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02002706 int tid = MAX_TID_COUNT, sta_id = IWL_INVALID_STATION;
2707 struct ieee80211_hdr *hdr;
2708 __le16 *qc;
Zhu Yib481de92007-09-25 17:54:57 -07002709#endif
2710
2711 if ((index >= txq->q.n_bd) || (x2_queue_used(&txq->q, index) == 0)) {
2712 IWL_ERROR("Read index for DMA queue txq_id (%d) index %d "
2713 "is out of range [0-%d] %d %d\n", txq_id,
Tomas Winklerfc4b6852007-10-25 17:15:24 +08002714 index, txq->q.n_bd, txq->q.write_ptr,
2715 txq->q.read_ptr);
Zhu Yib481de92007-09-25 17:54:57 -07002716 return;
2717 }
2718
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08002719#ifdef CONFIG_IWL4965_HT
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02002720 hdr = iwl4965_tx_queue_get_hdr(priv, txq_id, index);
2721 qc = ieee80211_get_qos_ctrl(hdr);
Zhu Yib481de92007-09-25 17:54:57 -07002722
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02002723 if (qc)
Zhu Yib481de92007-09-25 17:54:57 -07002724 tid = le16_to_cpu(*qc) & 0xf;
2725
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02002726 sta_id = iwl4965_get_ra_sta_id(priv, hdr);
2727 if (txq->sched_retry && unlikely(sta_id == IWL_INVALID_STATION)) {
2728 IWL_ERROR("Station not known\n");
2729 return;
2730 }
2731
2732 if (txq->sched_retry) {
2733 const u32 scd_ssn = iwl4965_get_scd_ssn(tx_resp);
Tomas Winkler6def9762008-05-05 10:22:31 +08002734 struct iwl_ht_agg *agg = NULL;
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02002735
2736 if (!qc)
Zhu Yib481de92007-09-25 17:54:57 -07002737 return;
Zhu Yib481de92007-09-25 17:54:57 -07002738
2739 agg = &priv->stations[sta_id].tid[tid].agg;
2740
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02002741 iwl4965_tx_status_reply_tx(priv, agg,
2742 (struct iwl4965_tx_resp_agg *)tx_resp, index);
Zhu Yib481de92007-09-25 17:54:57 -07002743
2744 if ((tx_resp->frame_count == 1) &&
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002745 !iwl4965_is_tx_success(status)) {
Zhu Yib481de92007-09-25 17:54:57 -07002746 /* TODO: send BAR */
2747 }
2748
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02002749 if (txq->q.read_ptr != (scd_ssn & 0xff)) {
Ron Rindjunsky0d0b2c12008-05-04 14:48:18 +03002750 int freed, ampdu_q;
Tomas Winklerc54b6792008-03-06 17:36:53 -08002751 index = iwl_queue_dec_wrap(scd_ssn & 0xff, txq->q.n_bd);
Zhu Yib481de92007-09-25 17:54:57 -07002752 IWL_DEBUG_TX_REPLY("Retry scheduler reclaim scd_ssn "
2753 "%d index %d\n", scd_ssn , index);
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02002754 freed = iwl4965_tx_queue_reclaim(priv, txq_id, index);
2755 priv->stations[sta_id].tid[tid].tfds_in_queue -= freed;
2756
2757 if (iwl4965_queue_space(&txq->q) > txq->q.low_mark &&
2758 txq_id >= 0 && priv->mac80211_registered &&
Ron Rindjunsky0d0b2c12008-05-04 14:48:18 +03002759 agg->state != IWL_EMPTYING_HW_QUEUE_DELBA) {
2760 /* calculate mac80211 ampdu sw queue to wake */
2761 ampdu_q = txq_id - IWL_BACK_QUEUE_FIRST_ID +
2762 priv->hw->queues;
2763 if (agg->state == IWL_AGG_OFF)
2764 ieee80211_wake_queue(priv->hw, txq_id);
2765 else
2766 ieee80211_wake_queue(priv->hw, ampdu_q);
2767 }
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02002768 iwl4965_check_empty_hw_queue(priv, sta_id, tid, txq_id);
Zhu Yib481de92007-09-25 17:54:57 -07002769 }
2770 } else {
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08002771#endif /* CONFIG_IWL4965_HT */
Tomas Winklerfc4b6852007-10-25 17:15:24 +08002772 tx_status = &(txq->txb[txq->q.read_ptr].status);
Zhu Yib481de92007-09-25 17:54:57 -07002773
2774 tx_status->retry_count = tx_resp->failure_frame;
Zhu Yib481de92007-09-25 17:54:57 -07002775 tx_status->flags =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002776 iwl4965_is_tx_success(status) ? IEEE80211_TX_STATUS_ACK : 0;
Ron Rindjunsky4c424e42008-03-04 18:09:27 -08002777 iwl4965_hwrate_to_tx_control(priv, le32_to_cpu(tx_resp->rate_n_flags),
2778 &tx_status->control);
Zhu Yib481de92007-09-25 17:54:57 -07002779
Zhu Yib481de92007-09-25 17:54:57 -07002780 IWL_DEBUG_TX("Tx queue %d Status %s (0x%08x) rate_n_flags 0x%x "
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002781 "retries %d\n", txq_id, iwl4965_get_tx_fail_reason(status),
Zhu Yib481de92007-09-25 17:54:57 -07002782 status, le32_to_cpu(tx_resp->rate_n_flags),
2783 tx_resp->failure_frame);
2784
2785 IWL_DEBUG_TX_REPLY("Tx queue reclaim %d\n", index);
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02002786 if (index != -1) {
2787 int freed = iwl4965_tx_queue_reclaim(priv, txq_id, index);
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08002788#ifdef CONFIG_IWL4965_HT
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02002789 if (tid != MAX_TID_COUNT)
2790 priv->stations[sta_id].tid[tid].tfds_in_queue -= freed;
2791 if (iwl4965_queue_space(&txq->q) > txq->q.low_mark &&
Ron Rindjunsky0d0b2c12008-05-04 14:48:18 +03002792 (txq_id >= 0) && priv->mac80211_registered)
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02002793 ieee80211_wake_queue(priv->hw, txq_id);
2794 if (tid != MAX_TID_COUNT)
2795 iwl4965_check_empty_hw_queue(priv, sta_id, tid, txq_id);
2796#endif
Zhu Yib481de92007-09-25 17:54:57 -07002797 }
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02002798#ifdef CONFIG_IWL4965_HT
2799 }
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08002800#endif /* CONFIG_IWL4965_HT */
Zhu Yib481de92007-09-25 17:54:57 -07002801
2802 if (iwl_check_bits(status, TX_ABORT_REQUIRED_MSK))
2803 IWL_ERROR("TODO: Implement Tx ABORT REQUIRED!!!\n");
2804}
2805
2806
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002807static void iwl4965_rx_reply_alive(struct iwl_priv *priv,
Tomas Winklera55360e2008-05-05 10:22:28 +08002808 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07002809{
Tomas Winklerdb11d632008-05-05 10:22:33 +08002810 struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002811 struct iwl4965_alive_resp *palive;
Zhu Yib481de92007-09-25 17:54:57 -07002812 struct delayed_work *pwork;
2813
2814 palive = &pkt->u.alive_frame;
2815
2816 IWL_DEBUG_INFO("Alive ucode status 0x%08X revision "
2817 "0x%01X 0x%01X\n",
2818 palive->is_valid, palive->ver_type,
2819 palive->ver_subtype);
2820
2821 if (palive->ver_subtype == INITIALIZE_SUBTYPE) {
2822 IWL_DEBUG_INFO("Initialization Alive received.\n");
2823 memcpy(&priv->card_alive_init,
2824 &pkt->u.alive_frame,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002825 sizeof(struct iwl4965_init_alive_resp));
Zhu Yib481de92007-09-25 17:54:57 -07002826 pwork = &priv->init_alive_start;
2827 } else {
2828 IWL_DEBUG_INFO("Runtime Alive received.\n");
2829 memcpy(&priv->card_alive, &pkt->u.alive_frame,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002830 sizeof(struct iwl4965_alive_resp));
Zhu Yib481de92007-09-25 17:54:57 -07002831 pwork = &priv->alive_start;
2832 }
2833
2834 /* We delay the ALIVE response by 5ms to
2835 * give the HW RF Kill time to activate... */
2836 if (palive->is_valid == UCODE_VALID_OK)
2837 queue_delayed_work(priv->workqueue, pwork,
2838 msecs_to_jiffies(5));
2839 else
2840 IWL_WARNING("uCode did not respond OK.\n");
2841}
2842
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002843static void iwl4965_rx_reply_add_sta(struct iwl_priv *priv,
Tomas Winklera55360e2008-05-05 10:22:28 +08002844 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07002845{
Tomas Winklerdb11d632008-05-05 10:22:33 +08002846 struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07002847
2848 IWL_DEBUG_RX("Received REPLY_ADD_STA: 0x%02X\n", pkt->u.status);
2849 return;
2850}
2851
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002852static void iwl4965_rx_reply_error(struct iwl_priv *priv,
Tomas Winklera55360e2008-05-05 10:22:28 +08002853 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07002854{
Tomas Winklerdb11d632008-05-05 10:22:33 +08002855 struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07002856
2857 IWL_ERROR("Error Reply type 0x%08X cmd %s (0x%02X) "
2858 "seq 0x%04X ser 0x%08X\n",
2859 le32_to_cpu(pkt->u.err_resp.error_type),
2860 get_cmd_string(pkt->u.err_resp.cmd_id),
2861 pkt->u.err_resp.cmd_id,
2862 le16_to_cpu(pkt->u.err_resp.bad_cmd_seq_num),
2863 le32_to_cpu(pkt->u.err_resp.error_info));
2864}
2865
2866#define TX_STATUS_ENTRY(x) case TX_STATUS_FAIL_ ## x: return #x
2867
Tomas Winklera55360e2008-05-05 10:22:28 +08002868static void iwl4965_rx_csa(struct iwl_priv *priv, struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07002869{
Tomas Winklerdb11d632008-05-05 10:22:33 +08002870 struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002871 struct iwl4965_rxon_cmd *rxon = (void *)&priv->active_rxon;
2872 struct iwl4965_csa_notification *csa = &(pkt->u.csa_notif);
Zhu Yib481de92007-09-25 17:54:57 -07002873 IWL_DEBUG_11H("CSA notif: channel %d, status %d\n",
2874 le16_to_cpu(csa->channel), le32_to_cpu(csa->status));
2875 rxon->channel = csa->channel;
2876 priv->staging_rxon.channel = csa->channel;
2877}
2878
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002879static void iwl4965_rx_spectrum_measure_notif(struct iwl_priv *priv,
Tomas Winklera55360e2008-05-05 10:22:28 +08002880 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07002881{
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08002882#ifdef CONFIG_IWL4965_SPECTRUM_MEASUREMENT
Tomas Winklerdb11d632008-05-05 10:22:33 +08002883 struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002884 struct iwl4965_spectrum_notification *report = &(pkt->u.spectrum_notif);
Zhu Yib481de92007-09-25 17:54:57 -07002885
2886 if (!report->state) {
2887 IWL_DEBUG(IWL_DL_11H | IWL_DL_INFO,
2888 "Spectrum Measure Notification: Start\n");
2889 return;
2890 }
2891
2892 memcpy(&priv->measure_report, report, sizeof(*report));
2893 priv->measurement_status |= MEASUREMENT_READY;
2894#endif
2895}
2896
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002897static void iwl4965_rx_pm_sleep_notif(struct iwl_priv *priv,
Tomas Winklera55360e2008-05-05 10:22:28 +08002898 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07002899{
Tomas Winkler0a6857e2008-03-12 16:58:49 -07002900#ifdef CONFIG_IWLWIFI_DEBUG
Tomas Winklerdb11d632008-05-05 10:22:33 +08002901 struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002902 struct iwl4965_sleep_notification *sleep = &(pkt->u.sleep_notif);
Zhu Yib481de92007-09-25 17:54:57 -07002903 IWL_DEBUG_RX("sleep mode: %d, src: %d\n",
2904 sleep->pm_sleep_mode, sleep->pm_wakeup_src);
2905#endif
2906}
2907
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002908static void iwl4965_rx_pm_debug_statistics_notif(struct iwl_priv *priv,
Tomas Winklera55360e2008-05-05 10:22:28 +08002909 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07002910{
Tomas Winklerdb11d632008-05-05 10:22:33 +08002911 struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07002912 IWL_DEBUG_RADIO("Dumping %d bytes of unhandled "
2913 "notification for %s:\n",
2914 le32_to_cpu(pkt->len), get_cmd_string(pkt->hdr.cmd));
Ester Kummerbf403db2008-05-05 10:22:40 +08002915 iwl_print_hex_dump(priv, IWL_DL_RADIO, pkt->u.raw, le32_to_cpu(pkt->len));
Zhu Yib481de92007-09-25 17:54:57 -07002916}
2917
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002918static void iwl4965_bg_beacon_update(struct work_struct *work)
Zhu Yib481de92007-09-25 17:54:57 -07002919{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002920 struct iwl_priv *priv =
2921 container_of(work, struct iwl_priv, beacon_update);
Zhu Yib481de92007-09-25 17:54:57 -07002922 struct sk_buff *beacon;
2923
2924 /* Pull updated AP beacon from mac80211. will fail if not in AP mode */
Johannes Berg32bfd352007-12-19 01:31:26 +01002925 beacon = ieee80211_beacon_get(priv->hw, priv->vif, NULL);
Zhu Yib481de92007-09-25 17:54:57 -07002926
2927 if (!beacon) {
2928 IWL_ERROR("update beacon failed\n");
2929 return;
2930 }
2931
2932 mutex_lock(&priv->mutex);
2933 /* new beacon skb is allocated every time; dispose previous.*/
2934 if (priv->ibss_beacon)
2935 dev_kfree_skb(priv->ibss_beacon);
2936
2937 priv->ibss_beacon = beacon;
2938 mutex_unlock(&priv->mutex);
2939
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002940 iwl4965_send_beacon_cmd(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002941}
2942
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002943static void iwl4965_rx_beacon_notif(struct iwl_priv *priv,
Tomas Winklera55360e2008-05-05 10:22:28 +08002944 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07002945{
Tomas Winkler0a6857e2008-03-12 16:58:49 -07002946#ifdef CONFIG_IWLWIFI_DEBUG
Tomas Winklerdb11d632008-05-05 10:22:33 +08002947 struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002948 struct iwl4965_beacon_notif *beacon = &(pkt->u.beacon_status);
2949 u8 rate = iwl4965_hw_get_rate(beacon->beacon_notify_hdr.rate_n_flags);
Zhu Yib481de92007-09-25 17:54:57 -07002950
2951 IWL_DEBUG_RX("beacon status %x retries %d iss %d "
2952 "tsf %d %d rate %d\n",
2953 le32_to_cpu(beacon->beacon_notify_hdr.status) & TX_STATUS_MSK,
2954 beacon->beacon_notify_hdr.failure_frame,
2955 le32_to_cpu(beacon->ibss_mgr_status),
2956 le32_to_cpu(beacon->high_tsf),
2957 le32_to_cpu(beacon->low_tsf), rate);
2958#endif
2959
2960 if ((priv->iw_mode == IEEE80211_IF_TYPE_AP) &&
2961 (!test_bit(STATUS_EXIT_PENDING, &priv->status)))
2962 queue_work(priv->workqueue, &priv->beacon_update);
2963}
2964
2965/* Service response to REPLY_SCAN_CMD (0x80) */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002966static void iwl4965_rx_reply_scan(struct iwl_priv *priv,
Tomas Winklera55360e2008-05-05 10:22:28 +08002967 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07002968{
Tomas Winkler0a6857e2008-03-12 16:58:49 -07002969#ifdef CONFIG_IWLWIFI_DEBUG
Tomas Winklerdb11d632008-05-05 10:22:33 +08002970 struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002971 struct iwl4965_scanreq_notification *notif =
2972 (struct iwl4965_scanreq_notification *)pkt->u.raw;
Zhu Yib481de92007-09-25 17:54:57 -07002973
2974 IWL_DEBUG_RX("Scan request status = 0x%x\n", notif->status);
2975#endif
2976}
2977
2978/* Service SCAN_START_NOTIFICATION (0x82) */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002979static void iwl4965_rx_scan_start_notif(struct iwl_priv *priv,
Tomas Winklera55360e2008-05-05 10:22:28 +08002980 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07002981{
Tomas Winklerdb11d632008-05-05 10:22:33 +08002982 struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002983 struct iwl4965_scanstart_notification *notif =
2984 (struct iwl4965_scanstart_notification *)pkt->u.raw;
Zhu Yib481de92007-09-25 17:54:57 -07002985 priv->scan_start_tsf = le32_to_cpu(notif->tsf_low);
2986 IWL_DEBUG_SCAN("Scan start: "
2987 "%d [802.11%s] "
2988 "(TSF: 0x%08X:%08X) - %d (beacon timer %u)\n",
2989 notif->channel,
2990 notif->band ? "bg" : "a",
2991 notif->tsf_high,
2992 notif->tsf_low, notif->status, notif->beacon_timer);
2993}
2994
2995/* Service SCAN_RESULTS_NOTIFICATION (0x83) */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002996static void iwl4965_rx_scan_results_notif(struct iwl_priv *priv,
Tomas Winklera55360e2008-05-05 10:22:28 +08002997 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07002998{
Tomas Winklerdb11d632008-05-05 10:22:33 +08002999 struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003000 struct iwl4965_scanresults_notification *notif =
3001 (struct iwl4965_scanresults_notification *)pkt->u.raw;
Zhu Yib481de92007-09-25 17:54:57 -07003002
3003 IWL_DEBUG_SCAN("Scan ch.res: "
3004 "%d [802.11%s] "
3005 "(TSF: 0x%08X:%08X) - %d "
3006 "elapsed=%lu usec (%dms since last)\n",
3007 notif->channel,
3008 notif->band ? "bg" : "a",
3009 le32_to_cpu(notif->tsf_high),
3010 le32_to_cpu(notif->tsf_low),
3011 le32_to_cpu(notif->statistics[0]),
3012 le32_to_cpu(notif->tsf_low) - priv->scan_start_tsf,
3013 jiffies_to_msecs(elapsed_jiffies
3014 (priv->last_scan_jiffies, jiffies)));
3015
3016 priv->last_scan_jiffies = jiffies;
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08003017 priv->next_scan_jiffies = 0;
Zhu Yib481de92007-09-25 17:54:57 -07003018}
3019
3020/* Service SCAN_COMPLETE_NOTIFICATION (0x84) */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003021static void iwl4965_rx_scan_complete_notif(struct iwl_priv *priv,
Tomas Winklera55360e2008-05-05 10:22:28 +08003022 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003023{
Tomas Winklerdb11d632008-05-05 10:22:33 +08003024 struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003025 struct iwl4965_scancomplete_notification *scan_notif = (void *)pkt->u.raw;
Zhu Yib481de92007-09-25 17:54:57 -07003026
3027 IWL_DEBUG_SCAN("Scan complete: %d channels (TSF 0x%08X:%08X) - %d\n",
3028 scan_notif->scanned_channels,
3029 scan_notif->tsf_low,
3030 scan_notif->tsf_high, scan_notif->status);
3031
3032 /* The HW is no longer scanning */
3033 clear_bit(STATUS_SCAN_HW, &priv->status);
3034
3035 /* The scan completion notification came in, so kill that timer... */
3036 cancel_delayed_work(&priv->scan_check);
3037
3038 IWL_DEBUG_INFO("Scan pass on %sGHz took %dms\n",
3039 (priv->scan_bands == 2) ? "2.4" : "5.2",
3040 jiffies_to_msecs(elapsed_jiffies
3041 (priv->scan_pass_start, jiffies)));
3042
3043 /* Remove this scanned band from the list
3044 * of pending bands to scan */
3045 priv->scan_bands--;
3046
3047 /* If a request to abort was given, or the scan did not succeed
3048 * then we reset the scan state machine and terminate,
3049 * re-queuing another scan if one has been requested */
3050 if (test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
3051 IWL_DEBUG_INFO("Aborted scan completed.\n");
3052 clear_bit(STATUS_SCAN_ABORTING, &priv->status);
3053 } else {
3054 /* If there are more bands on this scan pass reschedule */
3055 if (priv->scan_bands > 0)
3056 goto reschedule;
3057 }
3058
3059 priv->last_scan_jiffies = jiffies;
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08003060 priv->next_scan_jiffies = 0;
Zhu Yib481de92007-09-25 17:54:57 -07003061 IWL_DEBUG_INFO("Setting scan to off\n");
3062
3063 clear_bit(STATUS_SCANNING, &priv->status);
3064
3065 IWL_DEBUG_INFO("Scan took %dms\n",
3066 jiffies_to_msecs(elapsed_jiffies(priv->scan_start, jiffies)));
3067
3068 queue_work(priv->workqueue, &priv->scan_completed);
3069
3070 return;
3071
3072reschedule:
3073 priv->scan_pass_start = jiffies;
3074 queue_work(priv->workqueue, &priv->request_scan);
3075}
3076
3077/* Handle notification from uCode that card's power state is changing
3078 * due to software, hardware, or critical temperature RFKILL */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003079static void iwl4965_rx_card_state_notif(struct iwl_priv *priv,
Tomas Winklera55360e2008-05-05 10:22:28 +08003080 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003081{
Tomas Winklerdb11d632008-05-05 10:22:33 +08003082 struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07003083 u32 flags = le32_to_cpu(pkt->u.card_state_notif.flags);
3084 unsigned long status = priv->status;
3085
3086 IWL_DEBUG_RF_KILL("Card state received: HW:%s SW:%s\n",
3087 (flags & HW_CARD_DISABLED) ? "Kill" : "On",
3088 (flags & SW_CARD_DISABLED) ? "Kill" : "On");
3089
3090 if (flags & (SW_CARD_DISABLED | HW_CARD_DISABLED |
3091 RF_CARD_DISABLED)) {
3092
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003093 iwl_write32(priv, CSR_UCODE_DRV_GP1_SET,
Zhu Yib481de92007-09-25 17:54:57 -07003094 CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
3095
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003096 if (!iwl_grab_nic_access(priv)) {
3097 iwl_write_direct32(
Zhu Yib481de92007-09-25 17:54:57 -07003098 priv, HBUS_TARG_MBX_C,
3099 HBUS_TARG_MBX_C_REG_BIT_CMD_BLOCKED);
3100
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003101 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003102 }
3103
3104 if (!(flags & RXON_CARD_DISABLED)) {
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003105 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR,
Zhu Yib481de92007-09-25 17:54:57 -07003106 CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003107 if (!iwl_grab_nic_access(priv)) {
3108 iwl_write_direct32(
Zhu Yib481de92007-09-25 17:54:57 -07003109 priv, HBUS_TARG_MBX_C,
3110 HBUS_TARG_MBX_C_REG_BIT_CMD_BLOCKED);
3111
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003112 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003113 }
3114 }
3115
3116 if (flags & RF_CARD_DISABLED) {
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003117 iwl_write32(priv, CSR_UCODE_DRV_GP1_SET,
Zhu Yib481de92007-09-25 17:54:57 -07003118 CSR_UCODE_DRV_GP1_REG_BIT_CT_KILL_EXIT);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003119 iwl_read32(priv, CSR_UCODE_DRV_GP1);
3120 if (!iwl_grab_nic_access(priv))
3121 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003122 }
3123 }
3124
3125 if (flags & HW_CARD_DISABLED)
3126 set_bit(STATUS_RF_KILL_HW, &priv->status);
3127 else
3128 clear_bit(STATUS_RF_KILL_HW, &priv->status);
3129
3130
3131 if (flags & SW_CARD_DISABLED)
3132 set_bit(STATUS_RF_KILL_SW, &priv->status);
3133 else
3134 clear_bit(STATUS_RF_KILL_SW, &priv->status);
3135
3136 if (!(flags & RXON_CARD_DISABLED))
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003137 iwl4965_scan_cancel(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003138
3139 if ((test_bit(STATUS_RF_KILL_HW, &status) !=
3140 test_bit(STATUS_RF_KILL_HW, &priv->status)) ||
3141 (test_bit(STATUS_RF_KILL_SW, &status) !=
3142 test_bit(STATUS_RF_KILL_SW, &priv->status)))
3143 queue_work(priv->workqueue, &priv->rf_kill);
3144 else
3145 wake_up_interruptible(&priv->wait_command_queue);
3146}
3147
3148/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003149 * iwl4965_setup_rx_handlers - Initialize Rx handler callbacks
Zhu Yib481de92007-09-25 17:54:57 -07003150 *
3151 * Setup the RX handlers for each of the reply types sent from the uCode
3152 * to the host.
3153 *
3154 * This function chains into the hardware specific files for them to setup
3155 * any hardware specific handlers as well.
3156 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003157static void iwl4965_setup_rx_handlers(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003158{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003159 priv->rx_handlers[REPLY_ALIVE] = iwl4965_rx_reply_alive;
3160 priv->rx_handlers[REPLY_ADD_STA] = iwl4965_rx_reply_add_sta;
3161 priv->rx_handlers[REPLY_ERROR] = iwl4965_rx_reply_error;
3162 priv->rx_handlers[CHANNEL_SWITCH_NOTIFICATION] = iwl4965_rx_csa;
Zhu Yib481de92007-09-25 17:54:57 -07003163 priv->rx_handlers[SPECTRUM_MEASURE_NOTIFICATION] =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003164 iwl4965_rx_spectrum_measure_notif;
3165 priv->rx_handlers[PM_SLEEP_NOTIFICATION] = iwl4965_rx_pm_sleep_notif;
Zhu Yib481de92007-09-25 17:54:57 -07003166 priv->rx_handlers[PM_DEBUG_STATISTIC_NOTIFIC] =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003167 iwl4965_rx_pm_debug_statistics_notif;
3168 priv->rx_handlers[BEACON_NOTIFICATION] = iwl4965_rx_beacon_notif;
Zhu Yib481de92007-09-25 17:54:57 -07003169
Ben Cahill9fbab512007-11-29 11:09:47 +08003170 /*
3171 * The same handler is used for both the REPLY to a discrete
3172 * statistics request from the host as well as for the periodic
3173 * statistics notifications (after received beacons) from the uCode.
Zhu Yib481de92007-09-25 17:54:57 -07003174 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003175 priv->rx_handlers[REPLY_STATISTICS_CMD] = iwl4965_hw_rx_statistics;
3176 priv->rx_handlers[STATISTICS_NOTIFICATION] = iwl4965_hw_rx_statistics;
Zhu Yib481de92007-09-25 17:54:57 -07003177
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003178 priv->rx_handlers[REPLY_SCAN_CMD] = iwl4965_rx_reply_scan;
3179 priv->rx_handlers[SCAN_START_NOTIFICATION] = iwl4965_rx_scan_start_notif;
Zhu Yib481de92007-09-25 17:54:57 -07003180 priv->rx_handlers[SCAN_RESULTS_NOTIFICATION] =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003181 iwl4965_rx_scan_results_notif;
Zhu Yib481de92007-09-25 17:54:57 -07003182 priv->rx_handlers[SCAN_COMPLETE_NOTIFICATION] =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003183 iwl4965_rx_scan_complete_notif;
3184 priv->rx_handlers[CARD_STATE_NOTIFICATION] = iwl4965_rx_card_state_notif;
3185 priv->rx_handlers[REPLY_TX] = iwl4965_rx_reply_tx;
Zhu Yib481de92007-09-25 17:54:57 -07003186
Ben Cahill9fbab512007-11-29 11:09:47 +08003187 /* Set up hardware specific Rx handlers */
Emmanuel Grumbachd4789ef2008-04-24 11:55:20 -07003188 priv->cfg->ops->lib->rx_handler_setup(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003189}
3190
3191/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003192 * iwl4965_tx_cmd_complete - Pull unused buffers off the queue and reclaim them
Zhu Yib481de92007-09-25 17:54:57 -07003193 * @rxb: Rx buffer to reclaim
3194 *
3195 * If an Rx buffer has an async callback associated with it the callback
3196 * will be executed. The attached skb (if present) will only be freed
3197 * if the callback returns 1
3198 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003199static void iwl4965_tx_cmd_complete(struct iwl_priv *priv,
Tomas Winklera55360e2008-05-05 10:22:28 +08003200 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003201{
Tomas Winklerdb11d632008-05-05 10:22:33 +08003202 struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07003203 u16 sequence = le16_to_cpu(pkt->hdr.sequence);
3204 int txq_id = SEQ_TO_QUEUE(sequence);
3205 int index = SEQ_TO_INDEX(sequence);
3206 int huge = sequence & SEQ_HUGE_FRAME;
3207 int cmd_index;
Tomas Winkler857485c2008-03-21 13:53:44 -07003208 struct iwl_cmd *cmd;
Zhu Yib481de92007-09-25 17:54:57 -07003209
3210 /* If a Tx command is being handled and it isn't in the actual
3211 * command queue then there a command routing bug has been introduced
3212 * in the queue management code. */
3213 if (txq_id != IWL_CMD_QUEUE_NUM)
3214 IWL_ERROR("Error wrong command queue %d command id 0x%X\n",
3215 txq_id, pkt->hdr.cmd);
3216 BUG_ON(txq_id != IWL_CMD_QUEUE_NUM);
3217
3218 cmd_index = get_cmd_index(&priv->txq[IWL_CMD_QUEUE_NUM].q, index, huge);
3219 cmd = &priv->txq[IWL_CMD_QUEUE_NUM].cmd[cmd_index];
3220
3221 /* Input error checking is done when commands are added to queue. */
3222 if (cmd->meta.flags & CMD_WANT_SKB) {
3223 cmd->meta.source->u.skb = rxb->skb;
3224 rxb->skb = NULL;
3225 } else if (cmd->meta.u.callback &&
3226 !cmd->meta.u.callback(priv, cmd, rxb->skb))
3227 rxb->skb = NULL;
3228
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003229 iwl4965_tx_queue_reclaim(priv, txq_id, index);
Zhu Yib481de92007-09-25 17:54:57 -07003230
3231 if (!(cmd->meta.flags & CMD_ASYNC)) {
3232 clear_bit(STATUS_HCMD_ACTIVE, &priv->status);
3233 wake_up_interruptible(&priv->wait_command_queue);
3234 }
3235}
3236
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08003237/*
3238 * this should be called while priv->lock is locked
3239*/
Tomas Winklera55360e2008-05-05 10:22:28 +08003240static void __iwl_rx_replenish(struct iwl_priv *priv)
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08003241{
Tomas Winklera55360e2008-05-05 10:22:28 +08003242 iwl_rx_allocate(priv);
3243 iwl_rx_queue_restock(priv);
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08003244}
3245
3246
Zhu Yib481de92007-09-25 17:54:57 -07003247/**
Tomas Winklera55360e2008-05-05 10:22:28 +08003248 * iwl_rx_handle - Main entry function for receiving responses from uCode
Zhu Yib481de92007-09-25 17:54:57 -07003249 *
3250 * Uses the priv->rx_handlers callback function array to invoke
3251 * the appropriate handlers, including command responses,
3252 * frame-received notifications, and other notifications.
3253 */
Tomas Winklera55360e2008-05-05 10:22:28 +08003254void iwl_rx_handle(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003255{
Tomas Winklera55360e2008-05-05 10:22:28 +08003256 struct iwl_rx_mem_buffer *rxb;
Tomas Winklerdb11d632008-05-05 10:22:33 +08003257 struct iwl_rx_packet *pkt;
Tomas Winklera55360e2008-05-05 10:22:28 +08003258 struct iwl_rx_queue *rxq = &priv->rxq;
Zhu Yib481de92007-09-25 17:54:57 -07003259 u32 r, i;
3260 int reclaim;
3261 unsigned long flags;
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08003262 u8 fill_rx = 0;
Mohamed Abbasd68ab682008-02-07 13:16:33 -08003263 u32 count = 8;
Zhu Yib481de92007-09-25 17:54:57 -07003264
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003265 /* uCode's read index (stored in shared DRAM) indicates the last Rx
3266 * buffer that the driver may process (last buffer filled by ucode). */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003267 r = iwl4965_hw_get_rx_read(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003268 i = rxq->read;
3269
3270 /* Rx interrupt, but nothing sent from uCode */
3271 if (i == r)
3272 IWL_DEBUG(IWL_DL_RX | IWL_DL_ISR, "r = %d, i = %d\n", r, i);
3273
Tomas Winklera55360e2008-05-05 10:22:28 +08003274 if (iwl_rx_queue_space(rxq) > (RX_QUEUE_SIZE / 2))
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08003275 fill_rx = 1;
3276
Zhu Yib481de92007-09-25 17:54:57 -07003277 while (i != r) {
3278 rxb = rxq->queue[i];
3279
Ben Cahill9fbab512007-11-29 11:09:47 +08003280 /* If an RXB doesn't have a Rx queue slot associated with it,
Zhu Yib481de92007-09-25 17:54:57 -07003281 * then a bug has been introduced in the queue refilling
3282 * routines -- catch it here */
3283 BUG_ON(rxb == NULL);
3284
3285 rxq->queue[i] = NULL;
3286
3287 pci_dma_sync_single_for_cpu(priv->pci_dev, rxb->dma_addr,
Tomas Winkler5425e492008-04-15 16:01:38 -07003288 priv->hw_params.rx_buf_size,
Zhu Yib481de92007-09-25 17:54:57 -07003289 PCI_DMA_FROMDEVICE);
Tomas Winklerdb11d632008-05-05 10:22:33 +08003290 pkt = (struct iwl_rx_packet *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07003291
3292 /* Reclaim a command buffer only if this packet is a response
3293 * to a (driver-originated) command.
3294 * If the packet (e.g. Rx frame) originated from uCode,
3295 * there is no command buffer to reclaim.
3296 * Ucode should set SEQ_RX_FRAME bit if ucode-originated,
3297 * but apparently a few don't get set; catch them here. */
3298 reclaim = !(pkt->hdr.sequence & SEQ_RX_FRAME) &&
3299 (pkt->hdr.cmd != REPLY_RX_PHY_CMD) &&
Tomas Winkler857485c2008-03-21 13:53:44 -07003300 (pkt->hdr.cmd != REPLY_RX) &&
Zhu Yicfe01702007-09-27 11:27:31 +08003301 (pkt->hdr.cmd != REPLY_COMPRESSED_BA) &&
Zhu Yib481de92007-09-25 17:54:57 -07003302 (pkt->hdr.cmd != STATISTICS_NOTIFICATION) &&
3303 (pkt->hdr.cmd != REPLY_TX);
3304
3305 /* Based on type of command response or notification,
3306 * handle those that need handling via function in
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003307 * rx_handlers table. See iwl4965_setup_rx_handlers() */
Zhu Yib481de92007-09-25 17:54:57 -07003308 if (priv->rx_handlers[pkt->hdr.cmd]) {
3309 IWL_DEBUG(IWL_DL_HOST_COMMAND | IWL_DL_RX | IWL_DL_ISR,
3310 "r = %d, i = %d, %s, 0x%02x\n", r, i,
3311 get_cmd_string(pkt->hdr.cmd), pkt->hdr.cmd);
3312 priv->rx_handlers[pkt->hdr.cmd] (priv, rxb);
3313 } else {
3314 /* No handling needed */
3315 IWL_DEBUG(IWL_DL_HOST_COMMAND | IWL_DL_RX | IWL_DL_ISR,
3316 "r %d i %d No handler needed for %s, 0x%02x\n",
3317 r, i, get_cmd_string(pkt->hdr.cmd),
3318 pkt->hdr.cmd);
3319 }
3320
3321 if (reclaim) {
Ben Cahill9fbab512007-11-29 11:09:47 +08003322 /* Invoke any callbacks, transfer the skb to caller, and
Tomas Winkler857485c2008-03-21 13:53:44 -07003323 * fire off the (possibly) blocking iwl_send_cmd()
Zhu Yib481de92007-09-25 17:54:57 -07003324 * as we reclaim the driver command queue */
3325 if (rxb && rxb->skb)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003326 iwl4965_tx_cmd_complete(priv, rxb);
Zhu Yib481de92007-09-25 17:54:57 -07003327 else
3328 IWL_WARNING("Claim null rxb?\n");
3329 }
3330
3331 /* For now we just don't re-use anything. We can tweak this
3332 * later to try and re-use notification packets and SKBs that
3333 * fail to Rx correctly */
3334 if (rxb->skb != NULL) {
3335 priv->alloc_rxb_skb--;
3336 dev_kfree_skb_any(rxb->skb);
3337 rxb->skb = NULL;
3338 }
3339
3340 pci_unmap_single(priv->pci_dev, rxb->dma_addr,
Tomas Winkler5425e492008-04-15 16:01:38 -07003341 priv->hw_params.rx_buf_size,
Ron Rindjunsky9ee1ba42007-11-26 16:14:42 +02003342 PCI_DMA_FROMDEVICE);
Zhu Yib481de92007-09-25 17:54:57 -07003343 spin_lock_irqsave(&rxq->lock, flags);
3344 list_add_tail(&rxb->list, &priv->rxq.rx_used);
3345 spin_unlock_irqrestore(&rxq->lock, flags);
3346 i = (i + 1) & RX_QUEUE_MASK;
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08003347 /* If there are a lot of unused frames,
3348 * restock the Rx queue so ucode wont assert. */
3349 if (fill_rx) {
3350 count++;
3351 if (count >= 8) {
3352 priv->rxq.read = i;
Tomas Winklera55360e2008-05-05 10:22:28 +08003353 __iwl_rx_replenish(priv);
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08003354 count = 0;
3355 }
3356 }
Zhu Yib481de92007-09-25 17:54:57 -07003357 }
3358
3359 /* Backtrack one entry */
3360 priv->rxq.read = i;
Tomas Winklera55360e2008-05-05 10:22:28 +08003361 iwl_rx_queue_restock(priv);
3362}
3363/* Convert linear signal-to-noise ratio into dB */
3364static u8 ratio2dB[100] = {
3365/* 0 1 2 3 4 5 6 7 8 9 */
3366 0, 0, 6, 10, 12, 14, 16, 17, 18, 19, /* 00 - 09 */
3367 20, 21, 22, 22, 23, 23, 24, 25, 26, 26, /* 10 - 19 */
3368 26, 26, 26, 27, 27, 28, 28, 28, 29, 29, /* 20 - 29 */
3369 29, 30, 30, 30, 31, 31, 31, 31, 32, 32, /* 30 - 39 */
3370 32, 32, 32, 33, 33, 33, 33, 33, 34, 34, /* 40 - 49 */
3371 34, 34, 34, 34, 35, 35, 35, 35, 35, 35, /* 50 - 59 */
3372 36, 36, 36, 36, 36, 36, 36, 37, 37, 37, /* 60 - 69 */
3373 37, 37, 37, 37, 37, 38, 38, 38, 38, 38, /* 70 - 79 */
3374 38, 38, 38, 38, 38, 39, 39, 39, 39, 39, /* 80 - 89 */
3375 39, 39, 39, 39, 39, 40, 40, 40, 40, 40 /* 90 - 99 */
3376};
3377
3378/* Calculates a relative dB value from a ratio of linear
3379 * (i.e. not dB) signal levels.
3380 * Conversion assumes that levels are voltages (20*log), not powers (10*log). */
3381int iwl4965_calc_db_from_ratio(int sig_ratio)
3382{
3383 /* 1000:1 or higher just report as 60 dB */
3384 if (sig_ratio >= 1000)
3385 return 60;
3386
3387 /* 100:1 or higher, divide by 10 and use table,
3388 * add 20 dB to make up for divide by 10 */
3389 if (sig_ratio >= 100)
3390 return (20 + (int)ratio2dB[sig_ratio/10]);
3391
3392 /* We shouldn't see this */
3393 if (sig_ratio < 1)
3394 return 0;
3395
3396 /* Use table for ratios 1:1 - 99:1 */
3397 return (int)ratio2dB[sig_ratio];
3398}
3399
3400#define PERFECT_RSSI (-20) /* dBm */
3401#define WORST_RSSI (-95) /* dBm */
3402#define RSSI_RANGE (PERFECT_RSSI - WORST_RSSI)
3403
3404/* Calculate an indication of rx signal quality (a percentage, not dBm!).
3405 * See http://www.ces.clemson.edu/linux/signal_quality.shtml for info
3406 * about formulas used below. */
3407int iwl4965_calc_sig_qual(int rssi_dbm, int noise_dbm)
3408{
3409 int sig_qual;
3410 int degradation = PERFECT_RSSI - rssi_dbm;
3411
3412 /* If we get a noise measurement, use signal-to-noise ratio (SNR)
3413 * as indicator; formula is (signal dbm - noise dbm).
3414 * SNR at or above 40 is a great signal (100%).
3415 * Below that, scale to fit SNR of 0 - 40 dB within 0 - 100% indicator.
3416 * Weakest usable signal is usually 10 - 15 dB SNR. */
3417 if (noise_dbm) {
3418 if (rssi_dbm - noise_dbm >= 40)
3419 return 100;
3420 else if (rssi_dbm < noise_dbm)
3421 return 0;
3422 sig_qual = ((rssi_dbm - noise_dbm) * 5) / 2;
3423
3424 /* Else use just the signal level.
3425 * This formula is a least squares fit of data points collected and
3426 * compared with a reference system that had a percentage (%) display
3427 * for signal quality. */
3428 } else
3429 sig_qual = (100 * (RSSI_RANGE * RSSI_RANGE) - degradation *
3430 (15 * RSSI_RANGE + 62 * degradation)) /
3431 (RSSI_RANGE * RSSI_RANGE);
3432
3433 if (sig_qual > 100)
3434 sig_qual = 100;
3435 else if (sig_qual < 1)
3436 sig_qual = 0;
3437
3438 return sig_qual;
Zhu Yib481de92007-09-25 17:54:57 -07003439}
3440
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003441/**
3442 * iwl4965_tx_queue_update_write_ptr - Send new write index to hardware
3443 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003444static int iwl4965_tx_queue_update_write_ptr(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003445 struct iwl4965_tx_queue *txq)
Zhu Yib481de92007-09-25 17:54:57 -07003446{
3447 u32 reg = 0;
3448 int rc = 0;
3449 int txq_id = txq->q.id;
3450
3451 if (txq->need_update == 0)
3452 return rc;
3453
3454 /* if we're trying to save power */
3455 if (test_bit(STATUS_POWER_PMI, &priv->status)) {
3456 /* wake up nic if it's powered down ...
3457 * uCode will wake up, and interrupt us again, so next
3458 * time we'll skip this part. */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003459 reg = iwl_read32(priv, CSR_UCODE_DRV_GP1);
Zhu Yib481de92007-09-25 17:54:57 -07003460
3461 if (reg & CSR_UCODE_DRV_GP1_BIT_MAC_SLEEP) {
3462 IWL_DEBUG_INFO("Requesting wakeup, GP1 = 0x%x\n", reg);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003463 iwl_set_bit(priv, CSR_GP_CNTRL,
Zhu Yib481de92007-09-25 17:54:57 -07003464 CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
3465 return rc;
3466 }
3467
3468 /* restore this queue's parameters in nic hardware. */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003469 rc = iwl_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003470 if (rc)
3471 return rc;
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003472 iwl_write_direct32(priv, HBUS_TARG_WRPTR,
Tomas Winklerfc4b6852007-10-25 17:15:24 +08003473 txq->q.write_ptr | (txq_id << 8));
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003474 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003475
3476 /* else not in power-save mode, uCode will never sleep when we're
3477 * trying to tx (during RFKILL, we're not trying to tx). */
3478 } else
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003479 iwl_write32(priv, HBUS_TARG_WRPTR,
Tomas Winklerfc4b6852007-10-25 17:15:24 +08003480 txq->q.write_ptr | (txq_id << 8));
Zhu Yib481de92007-09-25 17:54:57 -07003481
3482 txq->need_update = 0;
3483
3484 return rc;
3485}
3486
Tomas Winkler0a6857e2008-03-12 16:58:49 -07003487#ifdef CONFIG_IWLWIFI_DEBUG
Ester Kummerbf403db2008-05-05 10:22:40 +08003488static void iwl4965_print_rx_config_cmd(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003489{
Ester Kummerbf403db2008-05-05 10:22:40 +08003490 struct iwl4965_rxon_cmd *rxon = &priv->staging_rxon;
Joe Perches0795af52007-10-03 17:59:30 -07003491 DECLARE_MAC_BUF(mac);
3492
Zhu Yib481de92007-09-25 17:54:57 -07003493 IWL_DEBUG_RADIO("RX CONFIG:\n");
Ester Kummerbf403db2008-05-05 10:22:40 +08003494 iwl_print_hex_dump(priv, IWL_DL_RADIO, (u8 *) rxon, sizeof(*rxon));
Zhu Yib481de92007-09-25 17:54:57 -07003495 IWL_DEBUG_RADIO("u16 channel: 0x%x\n", le16_to_cpu(rxon->channel));
3496 IWL_DEBUG_RADIO("u32 flags: 0x%08X\n", le32_to_cpu(rxon->flags));
3497 IWL_DEBUG_RADIO("u32 filter_flags: 0x%08x\n",
3498 le32_to_cpu(rxon->filter_flags));
3499 IWL_DEBUG_RADIO("u8 dev_type: 0x%x\n", rxon->dev_type);
3500 IWL_DEBUG_RADIO("u8 ofdm_basic_rates: 0x%02x\n",
3501 rxon->ofdm_basic_rates);
3502 IWL_DEBUG_RADIO("u8 cck_basic_rates: 0x%02x\n", rxon->cck_basic_rates);
Joe Perches0795af52007-10-03 17:59:30 -07003503 IWL_DEBUG_RADIO("u8[6] node_addr: %s\n",
3504 print_mac(mac, rxon->node_addr));
3505 IWL_DEBUG_RADIO("u8[6] bssid_addr: %s\n",
3506 print_mac(mac, rxon->bssid_addr));
Zhu Yib481de92007-09-25 17:54:57 -07003507 IWL_DEBUG_RADIO("u16 assoc_id: 0x%x\n", le16_to_cpu(rxon->assoc_id));
3508}
3509#endif
3510
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003511static void iwl4965_enable_interrupts(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003512{
3513 IWL_DEBUG_ISR("Enabling interrupts\n");
3514 set_bit(STATUS_INT_ENABLED, &priv->status);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003515 iwl_write32(priv, CSR_INT_MASK, CSR_INI_SET_MASK);
Zhu Yib481de92007-09-25 17:54:57 -07003516}
3517
Mohamed Abbas0359fac2008-03-28 16:21:08 -07003518/* call this function to flush any scheduled tasklet */
3519static inline void iwl_synchronize_irq(struct iwl_priv *priv)
3520{
3521 /* wait to make sure we flush pedding tasklet*/
3522 synchronize_irq(priv->pci_dev->irq);
3523 tasklet_kill(&priv->irq_tasklet);
3524}
3525
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003526static inline void iwl4965_disable_interrupts(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003527{
3528 clear_bit(STATUS_INT_ENABLED, &priv->status);
3529
3530 /* disable interrupts from uCode/NIC to host */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003531 iwl_write32(priv, CSR_INT_MASK, 0x00000000);
Zhu Yib481de92007-09-25 17:54:57 -07003532
3533 /* acknowledge/clear/reset any interrupts still pending
3534 * from uCode or flow handler (Rx/Tx DMA) */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003535 iwl_write32(priv, CSR_INT, 0xffffffff);
3536 iwl_write32(priv, CSR_FH_INT_STATUS, 0xffffffff);
Zhu Yib481de92007-09-25 17:54:57 -07003537 IWL_DEBUG_ISR("Disabled interrupts\n");
3538}
3539
3540static const char *desc_lookup(int i)
3541{
3542 switch (i) {
3543 case 1:
3544 return "FAIL";
3545 case 2:
3546 return "BAD_PARAM";
3547 case 3:
3548 return "BAD_CHECKSUM";
3549 case 4:
3550 return "NMI_INTERRUPT";
3551 case 5:
3552 return "SYSASSERT";
3553 case 6:
3554 return "FATAL_ERROR";
3555 }
3556
3557 return "UNKNOWN";
3558}
3559
3560#define ERROR_START_OFFSET (1 * sizeof(u32))
3561#define ERROR_ELEM_SIZE (7 * sizeof(u32))
3562
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003563static void iwl4965_dump_nic_error_log(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003564{
3565 u32 data2, line;
3566 u32 desc, time, count, base, data1;
3567 u32 blink1, blink2, ilink1, ilink2;
3568 int rc;
3569
3570 base = le32_to_cpu(priv->card_alive.error_event_table_ptr);
3571
Tomas Winkler57aab752008-04-14 21:16:03 -07003572 if (!priv->cfg->ops->lib->is_valid_rtc_data_addr(base)) {
Zhu Yib481de92007-09-25 17:54:57 -07003573 IWL_ERROR("Not valid error log pointer 0x%08X\n", base);
3574 return;
3575 }
3576
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003577 rc = iwl_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003578 if (rc) {
3579 IWL_WARNING("Can not read from adapter at this time.\n");
3580 return;
3581 }
3582
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003583 count = iwl_read_targ_mem(priv, base);
Zhu Yib481de92007-09-25 17:54:57 -07003584
3585 if (ERROR_START_OFFSET <= count * ERROR_ELEM_SIZE) {
3586 IWL_ERROR("Start IWL Error Log Dump:\n");
Tomas Winkler2acae162008-03-02 01:25:59 +02003587 IWL_ERROR("Status: 0x%08lX, count: %d\n", priv->status, count);
Zhu Yib481de92007-09-25 17:54:57 -07003588 }
3589
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003590 desc = iwl_read_targ_mem(priv, base + 1 * sizeof(u32));
3591 blink1 = iwl_read_targ_mem(priv, base + 3 * sizeof(u32));
3592 blink2 = iwl_read_targ_mem(priv, base + 4 * sizeof(u32));
3593 ilink1 = iwl_read_targ_mem(priv, base + 5 * sizeof(u32));
3594 ilink2 = iwl_read_targ_mem(priv, base + 6 * sizeof(u32));
3595 data1 = iwl_read_targ_mem(priv, base + 7 * sizeof(u32));
3596 data2 = iwl_read_targ_mem(priv, base + 8 * sizeof(u32));
3597 line = iwl_read_targ_mem(priv, base + 9 * sizeof(u32));
3598 time = iwl_read_targ_mem(priv, base + 11 * sizeof(u32));
Zhu Yib481de92007-09-25 17:54:57 -07003599
3600 IWL_ERROR("Desc Time "
3601 "data1 data2 line\n");
3602 IWL_ERROR("%-13s (#%d) %010u 0x%08X 0x%08X %u\n",
3603 desc_lookup(desc), desc, time, data1, data2, line);
3604 IWL_ERROR("blink1 blink2 ilink1 ilink2\n");
3605 IWL_ERROR("0x%05X 0x%05X 0x%05X 0x%05X\n", blink1, blink2,
3606 ilink1, ilink2);
3607
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003608 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003609}
3610
3611#define EVENT_START_OFFSET (4 * sizeof(u32))
3612
3613/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003614 * iwl4965_print_event_log - Dump error event log to syslog
Zhu Yib481de92007-09-25 17:54:57 -07003615 *
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003616 * NOTE: Must be called with iwl_grab_nic_access() already obtained!
Zhu Yib481de92007-09-25 17:54:57 -07003617 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003618static void iwl4965_print_event_log(struct iwl_priv *priv, u32 start_idx,
Zhu Yib481de92007-09-25 17:54:57 -07003619 u32 num_events, u32 mode)
3620{
3621 u32 i;
3622 u32 base; /* SRAM byte address of event log header */
3623 u32 event_size; /* 2 u32s, or 3 u32s if timestamp recorded */
3624 u32 ptr; /* SRAM byte address of log data */
3625 u32 ev, time, data; /* event log data */
3626
3627 if (num_events == 0)
3628 return;
3629
3630 base = le32_to_cpu(priv->card_alive.log_event_table_ptr);
3631
3632 if (mode == 0)
3633 event_size = 2 * sizeof(u32);
3634 else
3635 event_size = 3 * sizeof(u32);
3636
3637 ptr = base + EVENT_START_OFFSET + (start_idx * event_size);
3638
3639 /* "time" is actually "data" for mode 0 (no timestamp).
3640 * place event id # at far right for easier visual parsing. */
3641 for (i = 0; i < num_events; i++) {
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003642 ev = iwl_read_targ_mem(priv, ptr);
Zhu Yib481de92007-09-25 17:54:57 -07003643 ptr += sizeof(u32);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003644 time = iwl_read_targ_mem(priv, ptr);
Zhu Yib481de92007-09-25 17:54:57 -07003645 ptr += sizeof(u32);
3646 if (mode == 0)
3647 IWL_ERROR("0x%08x\t%04u\n", time, ev); /* data, ev */
3648 else {
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003649 data = iwl_read_targ_mem(priv, ptr);
Zhu Yib481de92007-09-25 17:54:57 -07003650 ptr += sizeof(u32);
3651 IWL_ERROR("%010u\t0x%08x\t%04u\n", time, data, ev);
3652 }
3653 }
3654}
3655
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003656static void iwl4965_dump_nic_event_log(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003657{
3658 int rc;
3659 u32 base; /* SRAM byte address of event log header */
3660 u32 capacity; /* event log capacity in # entries */
3661 u32 mode; /* 0 - no timestamp, 1 - timestamp recorded */
3662 u32 num_wraps; /* # times uCode wrapped to top of log */
3663 u32 next_entry; /* index of next entry to be written by uCode */
3664 u32 size; /* # entries that we'll print */
3665
3666 base = le32_to_cpu(priv->card_alive.log_event_table_ptr);
Tomas Winkler57aab752008-04-14 21:16:03 -07003667 if (!priv->cfg->ops->lib->is_valid_rtc_data_addr(base)) {
Zhu Yib481de92007-09-25 17:54:57 -07003668 IWL_ERROR("Invalid event log pointer 0x%08X\n", base);
3669 return;
3670 }
3671
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003672 rc = iwl_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003673 if (rc) {
3674 IWL_WARNING("Can not read from adapter at this time.\n");
3675 return;
3676 }
3677
3678 /* event log header */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003679 capacity = iwl_read_targ_mem(priv, base);
3680 mode = iwl_read_targ_mem(priv, base + (1 * sizeof(u32)));
3681 num_wraps = iwl_read_targ_mem(priv, base + (2 * sizeof(u32)));
3682 next_entry = iwl_read_targ_mem(priv, base + (3 * sizeof(u32)));
Zhu Yib481de92007-09-25 17:54:57 -07003683
3684 size = num_wraps ? capacity : next_entry;
3685
3686 /* bail out if nothing in log */
3687 if (size == 0) {
Zhu Yi583fab32007-09-27 11:27:30 +08003688 IWL_ERROR("Start IWL Event Log Dump: nothing in log\n");
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003689 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003690 return;
3691 }
3692
Zhu Yi583fab32007-09-27 11:27:30 +08003693 IWL_ERROR("Start IWL Event Log Dump: display count %d, wraps %d\n",
Zhu Yib481de92007-09-25 17:54:57 -07003694 size, num_wraps);
3695
3696 /* if uCode has wrapped back to top of log, start at the oldest entry,
3697 * i.e the next one that uCode would fill. */
3698 if (num_wraps)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003699 iwl4965_print_event_log(priv, next_entry,
Zhu Yib481de92007-09-25 17:54:57 -07003700 capacity - next_entry, mode);
3701
3702 /* (then/else) start at top of log */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003703 iwl4965_print_event_log(priv, 0, next_entry, mode);
Zhu Yib481de92007-09-25 17:54:57 -07003704
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003705 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003706}
3707
3708/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003709 * iwl4965_irq_handle_error - called for HW or SW error interrupt from card
Zhu Yib481de92007-09-25 17:54:57 -07003710 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003711static void iwl4965_irq_handle_error(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003712{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003713 /* Set the FW error flag -- cleared on iwl4965_down */
Zhu Yib481de92007-09-25 17:54:57 -07003714 set_bit(STATUS_FW_ERROR, &priv->status);
3715
3716 /* Cancel currently queued command. */
3717 clear_bit(STATUS_HCMD_ACTIVE, &priv->status);
3718
Tomas Winkler0a6857e2008-03-12 16:58:49 -07003719#ifdef CONFIG_IWLWIFI_DEBUG
Ester Kummerbf403db2008-05-05 10:22:40 +08003720 if (priv->debug_level & IWL_DL_FW_ERRORS) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003721 iwl4965_dump_nic_error_log(priv);
3722 iwl4965_dump_nic_event_log(priv);
Ester Kummerbf403db2008-05-05 10:22:40 +08003723 iwl4965_print_rx_config_cmd(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003724 }
3725#endif
3726
3727 wake_up_interruptible(&priv->wait_command_queue);
3728
3729 /* Keep the restart process from trying to send host
3730 * commands by clearing the INIT status bit */
3731 clear_bit(STATUS_READY, &priv->status);
3732
3733 if (!test_bit(STATUS_EXIT_PENDING, &priv->status)) {
3734 IWL_DEBUG(IWL_DL_INFO | IWL_DL_FW_ERRORS,
3735 "Restarting adapter due to uCode error.\n");
3736
Tomas Winkler3109ece2008-03-28 16:33:35 -07003737 if (iwl_is_associated(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07003738 memcpy(&priv->recovery_rxon, &priv->active_rxon,
3739 sizeof(priv->recovery_rxon));
3740 priv->error_recovering = 1;
3741 }
3742 queue_work(priv->workqueue, &priv->restart);
3743 }
3744}
3745
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003746static void iwl4965_error_recovery(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003747{
3748 unsigned long flags;
3749
3750 memcpy(&priv->staging_rxon, &priv->recovery_rxon,
3751 sizeof(priv->staging_rxon));
3752 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003753 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003754
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003755 iwl4965_rxon_add_station(priv, priv->bssid, 1);
Zhu Yib481de92007-09-25 17:54:57 -07003756
3757 spin_lock_irqsave(&priv->lock, flags);
3758 priv->assoc_id = le16_to_cpu(priv->staging_rxon.assoc_id);
3759 priv->error_recovering = 0;
3760 spin_unlock_irqrestore(&priv->lock, flags);
3761}
3762
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003763static void iwl4965_irq_tasklet(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003764{
3765 u32 inta, handled = 0;
3766 u32 inta_fh;
3767 unsigned long flags;
Tomas Winkler0a6857e2008-03-12 16:58:49 -07003768#ifdef CONFIG_IWLWIFI_DEBUG
Zhu Yib481de92007-09-25 17:54:57 -07003769 u32 inta_mask;
3770#endif
3771
3772 spin_lock_irqsave(&priv->lock, flags);
3773
3774 /* Ack/clear/reset pending uCode interrupts.
3775 * Note: Some bits in CSR_INT are "OR" of bits in CSR_FH_INT_STATUS,
3776 * and will clear only when CSR_FH_INT_STATUS gets cleared. */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003777 inta = iwl_read32(priv, CSR_INT);
3778 iwl_write32(priv, CSR_INT, inta);
Zhu Yib481de92007-09-25 17:54:57 -07003779
3780 /* Ack/clear/reset pending flow-handler (DMA) interrupts.
3781 * Any new interrupts that happen after this, either while we're
3782 * in this tasklet, or later, will show up in next ISR/tasklet. */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003783 inta_fh = iwl_read32(priv, CSR_FH_INT_STATUS);
3784 iwl_write32(priv, CSR_FH_INT_STATUS, inta_fh);
Zhu Yib481de92007-09-25 17:54:57 -07003785
Tomas Winkler0a6857e2008-03-12 16:58:49 -07003786#ifdef CONFIG_IWLWIFI_DEBUG
Ester Kummerbf403db2008-05-05 10:22:40 +08003787 if (priv->debug_level & IWL_DL_ISR) {
Ben Cahill9fbab512007-11-29 11:09:47 +08003788 /* just for debug */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003789 inta_mask = iwl_read32(priv, CSR_INT_MASK);
Zhu Yib481de92007-09-25 17:54:57 -07003790 IWL_DEBUG_ISR("inta 0x%08x, enabled 0x%08x, fh 0x%08x\n",
3791 inta, inta_mask, inta_fh);
3792 }
3793#endif
3794
3795 /* Since CSR_INT and CSR_FH_INT_STATUS reads and clears are not
3796 * atomic, make sure that inta covers all the interrupts that
3797 * we've discovered, even if FH interrupt came in just after
3798 * reading CSR_INT. */
Tomas Winkler6f83eaa2008-03-04 18:09:28 -08003799 if (inta_fh & CSR49_FH_INT_RX_MASK)
Zhu Yib481de92007-09-25 17:54:57 -07003800 inta |= CSR_INT_BIT_FH_RX;
Tomas Winkler6f83eaa2008-03-04 18:09:28 -08003801 if (inta_fh & CSR49_FH_INT_TX_MASK)
Zhu Yib481de92007-09-25 17:54:57 -07003802 inta |= CSR_INT_BIT_FH_TX;
3803
3804 /* Now service all interrupt bits discovered above. */
3805 if (inta & CSR_INT_BIT_HW_ERR) {
3806 IWL_ERROR("Microcode HW error detected. Restarting.\n");
3807
3808 /* Tell the device to stop sending interrupts */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003809 iwl4965_disable_interrupts(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003810
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003811 iwl4965_irq_handle_error(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003812
3813 handled |= CSR_INT_BIT_HW_ERR;
3814
3815 spin_unlock_irqrestore(&priv->lock, flags);
3816
3817 return;
3818 }
3819
Tomas Winkler0a6857e2008-03-12 16:58:49 -07003820#ifdef CONFIG_IWLWIFI_DEBUG
Ester Kummerbf403db2008-05-05 10:22:40 +08003821 if (priv->debug_level & (IWL_DL_ISR)) {
Zhu Yib481de92007-09-25 17:54:57 -07003822 /* NIC fires this, but we don't use it, redundant with WAKEUP */
Joonwoo Park25c03d82008-01-23 10:15:20 -08003823 if (inta & CSR_INT_BIT_SCD)
3824 IWL_DEBUG_ISR("Scheduler finished to transmit "
3825 "the frame/frames.\n");
Zhu Yib481de92007-09-25 17:54:57 -07003826
3827 /* Alive notification via Rx interrupt will do the real work */
3828 if (inta & CSR_INT_BIT_ALIVE)
3829 IWL_DEBUG_ISR("Alive interrupt\n");
3830 }
3831#endif
3832 /* Safely ignore these bits for debug checks below */
Joonwoo Park25c03d82008-01-23 10:15:20 -08003833 inta &= ~(CSR_INT_BIT_SCD | CSR_INT_BIT_ALIVE);
Zhu Yib481de92007-09-25 17:54:57 -07003834
Ben Cahill9fbab512007-11-29 11:09:47 +08003835 /* HW RF KILL switch toggled */
Zhu Yib481de92007-09-25 17:54:57 -07003836 if (inta & CSR_INT_BIT_RF_KILL) {
3837 int hw_rf_kill = 0;
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003838 if (!(iwl_read32(priv, CSR_GP_CNTRL) &
Zhu Yib481de92007-09-25 17:54:57 -07003839 CSR_GP_CNTRL_REG_FLAG_HW_RF_KILL_SW))
3840 hw_rf_kill = 1;
3841
3842 IWL_DEBUG(IWL_DL_INFO | IWL_DL_RF_KILL | IWL_DL_ISR,
3843 "RF_KILL bit toggled to %s.\n",
3844 hw_rf_kill ? "disable radio":"enable radio");
3845
3846 /* Queue restart only if RF_KILL switch was set to "kill"
3847 * when we loaded driver, and is now set to "enable".
3848 * After we're Alive, RF_KILL gets handled by
Reinette Chatre32304552008-02-15 14:34:37 -08003849 * iwl4965_rx_card_state_notif() */
Zhu Yi53e49092007-12-06 16:08:44 +08003850 if (!hw_rf_kill && !test_bit(STATUS_ALIVE, &priv->status)) {
3851 clear_bit(STATUS_RF_KILL_HW, &priv->status);
Zhu Yib481de92007-09-25 17:54:57 -07003852 queue_work(priv->workqueue, &priv->restart);
Zhu Yi53e49092007-12-06 16:08:44 +08003853 }
Zhu Yib481de92007-09-25 17:54:57 -07003854
3855 handled |= CSR_INT_BIT_RF_KILL;
3856 }
3857
Ben Cahill9fbab512007-11-29 11:09:47 +08003858 /* Chip got too hot and stopped itself */
Zhu Yib481de92007-09-25 17:54:57 -07003859 if (inta & CSR_INT_BIT_CT_KILL) {
3860 IWL_ERROR("Microcode CT kill error detected.\n");
3861 handled |= CSR_INT_BIT_CT_KILL;
3862 }
3863
3864 /* Error detected by uCode */
3865 if (inta & CSR_INT_BIT_SW_ERR) {
3866 IWL_ERROR("Microcode SW error detected. Restarting 0x%X.\n",
3867 inta);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003868 iwl4965_irq_handle_error(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003869 handled |= CSR_INT_BIT_SW_ERR;
3870 }
3871
3872 /* uCode wakes up after power-down sleep */
3873 if (inta & CSR_INT_BIT_WAKEUP) {
3874 IWL_DEBUG_ISR("Wakeup interrupt\n");
Tomas Winklera55360e2008-05-05 10:22:28 +08003875 iwl_rx_queue_update_write_ptr(priv, &priv->rxq);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003876 iwl4965_tx_queue_update_write_ptr(priv, &priv->txq[0]);
3877 iwl4965_tx_queue_update_write_ptr(priv, &priv->txq[1]);
3878 iwl4965_tx_queue_update_write_ptr(priv, &priv->txq[2]);
3879 iwl4965_tx_queue_update_write_ptr(priv, &priv->txq[3]);
3880 iwl4965_tx_queue_update_write_ptr(priv, &priv->txq[4]);
3881 iwl4965_tx_queue_update_write_ptr(priv, &priv->txq[5]);
Zhu Yib481de92007-09-25 17:54:57 -07003882
3883 handled |= CSR_INT_BIT_WAKEUP;
3884 }
3885
3886 /* All uCode command responses, including Tx command responses,
3887 * Rx "responses" (frame-received notification), and other
3888 * notifications from uCode come through here*/
3889 if (inta & (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX)) {
Tomas Winklera55360e2008-05-05 10:22:28 +08003890 iwl_rx_handle(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003891 handled |= (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX);
3892 }
3893
3894 if (inta & CSR_INT_BIT_FH_TX) {
3895 IWL_DEBUG_ISR("Tx interrupt\n");
3896 handled |= CSR_INT_BIT_FH_TX;
3897 }
3898
3899 if (inta & ~handled)
3900 IWL_ERROR("Unhandled INTA bits 0x%08x\n", inta & ~handled);
3901
3902 if (inta & ~CSR_INI_SET_MASK) {
3903 IWL_WARNING("Disabled INTA bits 0x%08x were pending\n",
3904 inta & ~CSR_INI_SET_MASK);
3905 IWL_WARNING(" with FH_INT = 0x%08x\n", inta_fh);
3906 }
3907
3908 /* Re-enable all interrupts */
Mohamed Abbas0359fac2008-03-28 16:21:08 -07003909 /* only Re-enable if diabled by irq */
3910 if (test_bit(STATUS_INT_ENABLED, &priv->status))
3911 iwl4965_enable_interrupts(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003912
Tomas Winkler0a6857e2008-03-12 16:58:49 -07003913#ifdef CONFIG_IWLWIFI_DEBUG
Ester Kummerbf403db2008-05-05 10:22:40 +08003914 if (priv->debug_level & (IWL_DL_ISR)) {
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003915 inta = iwl_read32(priv, CSR_INT);
3916 inta_mask = iwl_read32(priv, CSR_INT_MASK);
3917 inta_fh = iwl_read32(priv, CSR_FH_INT_STATUS);
Zhu Yib481de92007-09-25 17:54:57 -07003918 IWL_DEBUG_ISR("End inta 0x%08x, enabled 0x%08x, fh 0x%08x, "
3919 "flags 0x%08lx\n", inta, inta_mask, inta_fh, flags);
3920 }
3921#endif
3922 spin_unlock_irqrestore(&priv->lock, flags);
3923}
3924
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003925static irqreturn_t iwl4965_isr(int irq, void *data)
Zhu Yib481de92007-09-25 17:54:57 -07003926{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003927 struct iwl_priv *priv = data;
Zhu Yib481de92007-09-25 17:54:57 -07003928 u32 inta, inta_mask;
3929 u32 inta_fh;
3930 if (!priv)
3931 return IRQ_NONE;
3932
3933 spin_lock(&priv->lock);
3934
3935 /* Disable (but don't clear!) interrupts here to avoid
3936 * back-to-back ISRs and sporadic interrupts from our NIC.
3937 * If we have something to service, the tasklet will re-enable ints.
3938 * If we *don't* have something, we'll re-enable before leaving here. */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003939 inta_mask = iwl_read32(priv, CSR_INT_MASK); /* just for debug */
3940 iwl_write32(priv, CSR_INT_MASK, 0x00000000);
Zhu Yib481de92007-09-25 17:54:57 -07003941
3942 /* Discover which interrupts are active/pending */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003943 inta = iwl_read32(priv, CSR_INT);
3944 inta_fh = iwl_read32(priv, CSR_FH_INT_STATUS);
Zhu Yib481de92007-09-25 17:54:57 -07003945
3946 /* Ignore interrupt if there's nothing in NIC to service.
3947 * This may be due to IRQ shared with another device,
3948 * or due to sporadic interrupts thrown from our NIC. */
3949 if (!inta && !inta_fh) {
3950 IWL_DEBUG_ISR("Ignore interrupt, inta == 0, inta_fh == 0\n");
3951 goto none;
3952 }
3953
3954 if ((inta == 0xFFFFFFFF) || ((inta & 0xFFFFFFF0) == 0xa5a5a5a0)) {
Oliver Neukum66fbb542007-11-15 09:31:10 +08003955 /* Hardware disappeared. It might have already raised
3956 * an interrupt */
Zhu Yib481de92007-09-25 17:54:57 -07003957 IWL_WARNING("HARDWARE GONE?? INTA == 0x%080x\n", inta);
Oliver Neukum66fbb542007-11-15 09:31:10 +08003958 goto unplugged;
Zhu Yib481de92007-09-25 17:54:57 -07003959 }
3960
3961 IWL_DEBUG_ISR("ISR inta 0x%08x, enabled 0x%08x, fh 0x%08x\n",
3962 inta, inta_mask, inta_fh);
3963
Joonwoo Park25c03d82008-01-23 10:15:20 -08003964 inta &= ~CSR_INT_BIT_SCD;
3965
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003966 /* iwl4965_irq_tasklet() will service interrupts and re-enable them */
Joonwoo Park25c03d82008-01-23 10:15:20 -08003967 if (likely(inta || inta_fh))
3968 tasklet_schedule(&priv->irq_tasklet);
Zhu Yib481de92007-09-25 17:54:57 -07003969
Oliver Neukum66fbb542007-11-15 09:31:10 +08003970 unplugged:
3971 spin_unlock(&priv->lock);
Zhu Yib481de92007-09-25 17:54:57 -07003972 return IRQ_HANDLED;
3973
3974 none:
3975 /* re-enable interrupts here since we don't have anything to service. */
Mohamed Abbas0359fac2008-03-28 16:21:08 -07003976 /* only Re-enable if diabled by irq */
3977 if (test_bit(STATUS_INT_ENABLED, &priv->status))
3978 iwl4965_enable_interrupts(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003979 spin_unlock(&priv->lock);
3980 return IRQ_NONE;
3981}
3982
Zhu Yib481de92007-09-25 17:54:57 -07003983/* For active scan, listen ACTIVE_DWELL_TIME (msec) on each channel after
3984 * sending probe req. This should be set long enough to hear probe responses
3985 * from more than one AP. */
3986#define IWL_ACTIVE_DWELL_TIME_24 (20) /* all times in msec */
3987#define IWL_ACTIVE_DWELL_TIME_52 (10)
3988
3989/* For faster active scanning, scan will move to the next channel if fewer than
3990 * PLCP_QUIET_THRESH packets are heard on this channel within
3991 * ACTIVE_QUIET_TIME after sending probe request. This shortens the dwell
3992 * time if it's a quiet channel (nothing responded to our probe, and there's
3993 * no other traffic).
3994 * Disable "quiet" feature by setting PLCP_QUIET_THRESH to 0. */
3995#define IWL_PLCP_QUIET_THRESH __constant_cpu_to_le16(1) /* packets */
3996#define IWL_ACTIVE_QUIET_TIME __constant_cpu_to_le16(5) /* msec */
3997
3998/* For passive scan, listen PASSIVE_DWELL_TIME (msec) on each channel.
3999 * Must be set longer than active dwell time.
4000 * For the most reliable scan, set > AP beacon interval (typically 100msec). */
4001#define IWL_PASSIVE_DWELL_TIME_24 (20) /* all times in msec */
4002#define IWL_PASSIVE_DWELL_TIME_52 (10)
4003#define IWL_PASSIVE_DWELL_BASE (100)
4004#define IWL_CHANNEL_TUNE_TIME 5
4005
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004006static inline u16 iwl4965_get_active_dwell_time(struct iwl_priv *priv,
Johannes Berg8318d782008-01-24 19:38:38 +01004007 enum ieee80211_band band)
Zhu Yib481de92007-09-25 17:54:57 -07004008{
Johannes Berg8318d782008-01-24 19:38:38 +01004009 if (band == IEEE80211_BAND_5GHZ)
Zhu Yib481de92007-09-25 17:54:57 -07004010 return IWL_ACTIVE_DWELL_TIME_52;
4011 else
4012 return IWL_ACTIVE_DWELL_TIME_24;
4013}
4014
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004015static u16 iwl4965_get_passive_dwell_time(struct iwl_priv *priv,
Johannes Berg8318d782008-01-24 19:38:38 +01004016 enum ieee80211_band band)
Zhu Yib481de92007-09-25 17:54:57 -07004017{
Johannes Berg8318d782008-01-24 19:38:38 +01004018 u16 active = iwl4965_get_active_dwell_time(priv, band);
4019 u16 passive = (band != IEEE80211_BAND_5GHZ) ?
Zhu Yib481de92007-09-25 17:54:57 -07004020 IWL_PASSIVE_DWELL_BASE + IWL_PASSIVE_DWELL_TIME_24 :
4021 IWL_PASSIVE_DWELL_BASE + IWL_PASSIVE_DWELL_TIME_52;
4022
Tomas Winkler3109ece2008-03-28 16:33:35 -07004023 if (iwl_is_associated(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07004024 /* If we're associated, we clamp the maximum passive
4025 * dwell time to be 98% of the beacon interval (minus
4026 * 2 * channel tune time) */
4027 passive = priv->beacon_int;
4028 if ((passive > IWL_PASSIVE_DWELL_BASE) || !passive)
4029 passive = IWL_PASSIVE_DWELL_BASE;
4030 passive = (passive * 98) / 100 - IWL_CHANNEL_TUNE_TIME * 2;
4031 }
4032
4033 if (passive <= active)
4034 passive = active + 1;
4035
4036 return passive;
4037}
4038
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004039static int iwl4965_get_channels_for_scan(struct iwl_priv *priv,
Johannes Berg8318d782008-01-24 19:38:38 +01004040 enum ieee80211_band band,
Zhu Yib481de92007-09-25 17:54:57 -07004041 u8 is_active, u8 direct_mask,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004042 struct iwl4965_scan_channel *scan_ch)
Zhu Yib481de92007-09-25 17:54:57 -07004043{
4044 const struct ieee80211_channel *channels = NULL;
Johannes Berg8318d782008-01-24 19:38:38 +01004045 const struct ieee80211_supported_band *sband;
Assaf Kraussbf85ea42008-03-14 10:38:49 -07004046 const struct iwl_channel_info *ch_info;
Zhu Yib481de92007-09-25 17:54:57 -07004047 u16 passive_dwell = 0;
4048 u16 active_dwell = 0;
4049 int added, i;
4050
Emmanuel Grumbachd1141df2008-04-21 15:42:00 -07004051 sband = iwl_get_hw_mode(priv, band);
Johannes Berg8318d782008-01-24 19:38:38 +01004052 if (!sband)
Zhu Yib481de92007-09-25 17:54:57 -07004053 return 0;
4054
Johannes Berg8318d782008-01-24 19:38:38 +01004055 channels = sband->channels;
Zhu Yib481de92007-09-25 17:54:57 -07004056
Johannes Berg8318d782008-01-24 19:38:38 +01004057 active_dwell = iwl4965_get_active_dwell_time(priv, band);
4058 passive_dwell = iwl4965_get_passive_dwell_time(priv, band);
Zhu Yib481de92007-09-25 17:54:57 -07004059
Johannes Berg8318d782008-01-24 19:38:38 +01004060 for (i = 0, added = 0; i < sband->n_channels; i++) {
Johannes Berg182e2e62008-04-04 10:41:56 +02004061 if (channels[i].flags & IEEE80211_CHAN_DISABLED)
4062 continue;
4063
Johannes Berg8318d782008-01-24 19:38:38 +01004064 scan_ch->channel = ieee80211_frequency_to_channel(channels[i].center_freq);
Zhu Yib481de92007-09-25 17:54:57 -07004065
Assaf Krauss8622e702008-03-21 13:53:43 -07004066 ch_info = iwl_get_channel_info(priv, band,
Ben Cahill9fbab512007-11-29 11:09:47 +08004067 scan_ch->channel);
Zhu Yib481de92007-09-25 17:54:57 -07004068 if (!is_channel_valid(ch_info)) {
4069 IWL_DEBUG_SCAN("Channel %d is INVALID for this SKU.\n",
4070 scan_ch->channel);
4071 continue;
4072 }
4073
4074 if (!is_active || is_channel_passive(ch_info) ||
Johannes Berg8318d782008-01-24 19:38:38 +01004075 (channels[i].flags & IEEE80211_CHAN_PASSIVE_SCAN))
Zhu Yib481de92007-09-25 17:54:57 -07004076 scan_ch->type = 0; /* passive */
4077 else
4078 scan_ch->type = 1; /* active */
4079
4080 if (scan_ch->type & 1)
4081 scan_ch->type |= (direct_mask << 1);
4082
Zhu Yib481de92007-09-25 17:54:57 -07004083 scan_ch->active_dwell = cpu_to_le16(active_dwell);
4084 scan_ch->passive_dwell = cpu_to_le16(passive_dwell);
4085
Ben Cahill9fbab512007-11-29 11:09:47 +08004086 /* Set txpower levels to defaults */
Zhu Yib481de92007-09-25 17:54:57 -07004087 scan_ch->tpc.dsp_atten = 110;
4088 /* scan_pwr_info->tpc.dsp_atten; */
4089
4090 /*scan_pwr_info->tpc.tx_gain; */
Johannes Berg8318d782008-01-24 19:38:38 +01004091 if (band == IEEE80211_BAND_5GHZ)
Zhu Yib481de92007-09-25 17:54:57 -07004092 scan_ch->tpc.tx_gain = ((1 << 5) | (3 << 3)) | 3;
4093 else {
4094 scan_ch->tpc.tx_gain = ((1 << 5) | (5 << 3));
4095 /* NOTE: if we were doing 6Mb OFDM for scans we'd use
Ben Cahill9fbab512007-11-29 11:09:47 +08004096 * power level:
Reinette Chatre8a1b0242008-01-14 17:46:25 -08004097 * scan_ch->tpc.tx_gain = ((1 << 5) | (2 << 3)) | 3;
Zhu Yib481de92007-09-25 17:54:57 -07004098 */
4099 }
4100
4101 IWL_DEBUG_SCAN("Scanning %d [%s %d]\n",
4102 scan_ch->channel,
4103 (scan_ch->type & 1) ? "ACTIVE" : "PASSIVE",
4104 (scan_ch->type & 1) ?
4105 active_dwell : passive_dwell);
4106
4107 scan_ch++;
4108 added++;
4109 }
4110
4111 IWL_DEBUG_SCAN("total channels to scan %d \n", added);
4112 return added;
4113}
4114
Zhu Yib481de92007-09-25 17:54:57 -07004115/******************************************************************************
4116 *
4117 * uCode download functions
4118 *
4119 ******************************************************************************/
4120
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004121static void iwl4965_dealloc_ucode_pci(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004122{
Tomas Winkler98c92212008-01-14 17:46:20 -08004123 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_code);
4124 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_data);
4125 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_data_backup);
4126 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_init);
4127 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_init_data);
4128 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_boot);
Zhu Yib481de92007-09-25 17:54:57 -07004129}
4130
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004131static void iwl4965_nic_start(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004132{
4133 /* Remove all resets to allow NIC to operate */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004134 iwl_write32(priv, CSR_RESET, 0);
Zhu Yib481de92007-09-25 17:54:57 -07004135}
4136
Tomas Winkler90e759d2007-11-29 11:09:41 +08004137
Zhu Yib481de92007-09-25 17:54:57 -07004138/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004139 * iwl4965_read_ucode - Read uCode images from disk file.
Zhu Yib481de92007-09-25 17:54:57 -07004140 *
4141 * Copy into buffers for card to fetch via bus-mastering
4142 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004143static int iwl4965_read_ucode(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004144{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004145 struct iwl4965_ucode *ucode;
Tomas Winkler90e759d2007-11-29 11:09:41 +08004146 int ret;
Zhu Yib481de92007-09-25 17:54:57 -07004147 const struct firmware *ucode_raw;
Tomas Winkler4bf775c2008-03-04 18:09:31 -08004148 const char *name = priv->cfg->fw_name;
Zhu Yib481de92007-09-25 17:54:57 -07004149 u8 *src;
4150 size_t len;
4151 u32 ver, inst_size, data_size, init_size, init_data_size, boot_size;
4152
4153 /* Ask kernel firmware_class module to get the boot firmware off disk.
4154 * request_firmware() is synchronous, file is in memory on return. */
Tomas Winkler90e759d2007-11-29 11:09:41 +08004155 ret = request_firmware(&ucode_raw, name, &priv->pci_dev->dev);
4156 if (ret < 0) {
4157 IWL_ERROR("%s firmware file req failed: Reason %d\n",
4158 name, ret);
Zhu Yib481de92007-09-25 17:54:57 -07004159 goto error;
4160 }
4161
4162 IWL_DEBUG_INFO("Got firmware '%s' file (%zd bytes) from disk\n",
4163 name, ucode_raw->size);
4164
4165 /* Make sure that we got at least our header! */
4166 if (ucode_raw->size < sizeof(*ucode)) {
4167 IWL_ERROR("File size way too small!\n");
Tomas Winkler90e759d2007-11-29 11:09:41 +08004168 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07004169 goto err_release;
4170 }
4171
4172 /* Data from ucode file: header followed by uCode images */
4173 ucode = (void *)ucode_raw->data;
4174
4175 ver = le32_to_cpu(ucode->ver);
4176 inst_size = le32_to_cpu(ucode->inst_size);
4177 data_size = le32_to_cpu(ucode->data_size);
4178 init_size = le32_to_cpu(ucode->init_size);
4179 init_data_size = le32_to_cpu(ucode->init_data_size);
4180 boot_size = le32_to_cpu(ucode->boot_size);
4181
4182 IWL_DEBUG_INFO("f/w package hdr ucode version = 0x%x\n", ver);
4183 IWL_DEBUG_INFO("f/w package hdr runtime inst size = %u\n",
4184 inst_size);
4185 IWL_DEBUG_INFO("f/w package hdr runtime data size = %u\n",
4186 data_size);
4187 IWL_DEBUG_INFO("f/w package hdr init inst size = %u\n",
4188 init_size);
4189 IWL_DEBUG_INFO("f/w package hdr init data size = %u\n",
4190 init_data_size);
4191 IWL_DEBUG_INFO("f/w package hdr boot inst size = %u\n",
4192 boot_size);
4193
4194 /* Verify size of file vs. image size info in file's header */
4195 if (ucode_raw->size < sizeof(*ucode) +
4196 inst_size + data_size + init_size +
4197 init_data_size + boot_size) {
4198
4199 IWL_DEBUG_INFO("uCode file size %d too small\n",
4200 (int)ucode_raw->size);
Tomas Winkler90e759d2007-11-29 11:09:41 +08004201 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07004202 goto err_release;
4203 }
4204
4205 /* Verify that uCode images will fit in card's SRAM */
Ron Rindjunsky099b40b2008-04-21 15:41:53 -07004206 if (inst_size > priv->hw_params.max_inst_size) {
Tomas Winkler90e759d2007-11-29 11:09:41 +08004207 IWL_DEBUG_INFO("uCode instr len %d too large to fit in\n",
4208 inst_size);
4209 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07004210 goto err_release;
4211 }
4212
Ron Rindjunsky099b40b2008-04-21 15:41:53 -07004213 if (data_size > priv->hw_params.max_data_size) {
Tomas Winkler90e759d2007-11-29 11:09:41 +08004214 IWL_DEBUG_INFO("uCode data len %d too large to fit in\n",
4215 data_size);
4216 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07004217 goto err_release;
4218 }
Ron Rindjunsky099b40b2008-04-21 15:41:53 -07004219 if (init_size > priv->hw_params.max_inst_size) {
Zhu Yib481de92007-09-25 17:54:57 -07004220 IWL_DEBUG_INFO
Tomas Winkler90e759d2007-11-29 11:09:41 +08004221 ("uCode init instr len %d too large to fit in\n",
4222 init_size);
4223 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07004224 goto err_release;
4225 }
Ron Rindjunsky099b40b2008-04-21 15:41:53 -07004226 if (init_data_size > priv->hw_params.max_data_size) {
Zhu Yib481de92007-09-25 17:54:57 -07004227 IWL_DEBUG_INFO
Tomas Winkler90e759d2007-11-29 11:09:41 +08004228 ("uCode init data len %d too large to fit in\n",
4229 init_data_size);
4230 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07004231 goto err_release;
4232 }
Ron Rindjunsky099b40b2008-04-21 15:41:53 -07004233 if (boot_size > priv->hw_params.max_bsm_size) {
Zhu Yib481de92007-09-25 17:54:57 -07004234 IWL_DEBUG_INFO
Tomas Winkler90e759d2007-11-29 11:09:41 +08004235 ("uCode boot instr len %d too large to fit in\n",
4236 boot_size);
4237 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07004238 goto err_release;
4239 }
4240
4241 /* Allocate ucode buffers for card's bus-master loading ... */
4242
4243 /* Runtime instructions and 2 copies of data:
4244 * 1) unmodified from disk
4245 * 2) backup cache for save/restore during power-downs */
4246 priv->ucode_code.len = inst_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08004247 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_code);
Zhu Yib481de92007-09-25 17:54:57 -07004248
4249 priv->ucode_data.len = data_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08004250 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_data);
Zhu Yib481de92007-09-25 17:54:57 -07004251
4252 priv->ucode_data_backup.len = data_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08004253 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_data_backup);
Zhu Yib481de92007-09-25 17:54:57 -07004254
4255 /* Initialization instructions and data */
Tomas Winkler90e759d2007-11-29 11:09:41 +08004256 if (init_size && init_data_size) {
4257 priv->ucode_init.len = init_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08004258 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_init);
Zhu Yib481de92007-09-25 17:54:57 -07004259
Tomas Winkler90e759d2007-11-29 11:09:41 +08004260 priv->ucode_init_data.len = init_data_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08004261 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_init_data);
Tomas Winkler90e759d2007-11-29 11:09:41 +08004262
4263 if (!priv->ucode_init.v_addr || !priv->ucode_init_data.v_addr)
4264 goto err_pci_alloc;
4265 }
Zhu Yib481de92007-09-25 17:54:57 -07004266
4267 /* Bootstrap (instructions only, no data) */
Tomas Winkler90e759d2007-11-29 11:09:41 +08004268 if (boot_size) {
4269 priv->ucode_boot.len = boot_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08004270 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_boot);
Zhu Yib481de92007-09-25 17:54:57 -07004271
Tomas Winkler90e759d2007-11-29 11:09:41 +08004272 if (!priv->ucode_boot.v_addr)
4273 goto err_pci_alloc;
4274 }
Zhu Yib481de92007-09-25 17:54:57 -07004275
4276 /* Copy images into buffers for card's bus-master reads ... */
4277
4278 /* Runtime instructions (first block of data in file) */
4279 src = &ucode->data[0];
4280 len = priv->ucode_code.len;
Tomas Winkler90e759d2007-11-29 11:09:41 +08004281 IWL_DEBUG_INFO("Copying (but not loading) uCode instr len %Zd\n", len);
Zhu Yib481de92007-09-25 17:54:57 -07004282 memcpy(priv->ucode_code.v_addr, src, len);
4283 IWL_DEBUG_INFO("uCode instr buf vaddr = 0x%p, paddr = 0x%08x\n",
4284 priv->ucode_code.v_addr, (u32)priv->ucode_code.p_addr);
4285
4286 /* Runtime data (2nd block)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004287 * NOTE: Copy into backup buffer will be done in iwl4965_up() */
Zhu Yib481de92007-09-25 17:54:57 -07004288 src = &ucode->data[inst_size];
4289 len = priv->ucode_data.len;
Tomas Winkler90e759d2007-11-29 11:09:41 +08004290 IWL_DEBUG_INFO("Copying (but not loading) uCode data len %Zd\n", len);
Zhu Yib481de92007-09-25 17:54:57 -07004291 memcpy(priv->ucode_data.v_addr, src, len);
4292 memcpy(priv->ucode_data_backup.v_addr, src, len);
4293
4294 /* Initialization instructions (3rd block) */
4295 if (init_size) {
4296 src = &ucode->data[inst_size + data_size];
4297 len = priv->ucode_init.len;
Tomas Winkler90e759d2007-11-29 11:09:41 +08004298 IWL_DEBUG_INFO("Copying (but not loading) init instr len %Zd\n",
4299 len);
Zhu Yib481de92007-09-25 17:54:57 -07004300 memcpy(priv->ucode_init.v_addr, src, len);
4301 }
4302
4303 /* Initialization data (4th block) */
4304 if (init_data_size) {
4305 src = &ucode->data[inst_size + data_size + init_size];
4306 len = priv->ucode_init_data.len;
Tomas Winkler90e759d2007-11-29 11:09:41 +08004307 IWL_DEBUG_INFO("Copying (but not loading) init data len %Zd\n",
4308 len);
Zhu Yib481de92007-09-25 17:54:57 -07004309 memcpy(priv->ucode_init_data.v_addr, src, len);
4310 }
4311
4312 /* Bootstrap instructions (5th block) */
4313 src = &ucode->data[inst_size + data_size + init_size + init_data_size];
4314 len = priv->ucode_boot.len;
Tomas Winkler90e759d2007-11-29 11:09:41 +08004315 IWL_DEBUG_INFO("Copying (but not loading) boot instr len %Zd\n", len);
Zhu Yib481de92007-09-25 17:54:57 -07004316 memcpy(priv->ucode_boot.v_addr, src, len);
4317
4318 /* We have our copies now, allow OS release its copies */
4319 release_firmware(ucode_raw);
4320 return 0;
4321
4322 err_pci_alloc:
4323 IWL_ERROR("failed to allocate pci memory\n");
Tomas Winkler90e759d2007-11-29 11:09:41 +08004324 ret = -ENOMEM;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004325 iwl4965_dealloc_ucode_pci(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004326
4327 err_release:
4328 release_firmware(ucode_raw);
4329
4330 error:
Tomas Winkler90e759d2007-11-29 11:09:41 +08004331 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07004332}
4333
4334
4335/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004336 * iwl4965_set_ucode_ptrs - Set uCode address location
Zhu Yib481de92007-09-25 17:54:57 -07004337 *
4338 * Tell initialization uCode where to find runtime uCode.
4339 *
4340 * BSM registers initially contain pointers to initialization uCode.
4341 * We need to replace them to load runtime uCode inst and data,
4342 * and to save runtime data when powering down.
4343 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004344static int iwl4965_set_ucode_ptrs(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004345{
4346 dma_addr_t pinst;
4347 dma_addr_t pdata;
4348 int rc = 0;
4349 unsigned long flags;
4350
4351 /* bits 35:4 for 4965 */
4352 pinst = priv->ucode_code.p_addr >> 4;
4353 pdata = priv->ucode_data_backup.p_addr >> 4;
4354
4355 spin_lock_irqsave(&priv->lock, flags);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004356 rc = iwl_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004357 if (rc) {
4358 spin_unlock_irqrestore(&priv->lock, flags);
4359 return rc;
4360 }
4361
4362 /* Tell bootstrap uCode where to find image to load */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004363 iwl_write_prph(priv, BSM_DRAM_INST_PTR_REG, pinst);
4364 iwl_write_prph(priv, BSM_DRAM_DATA_PTR_REG, pdata);
4365 iwl_write_prph(priv, BSM_DRAM_DATA_BYTECOUNT_REG,
Zhu Yib481de92007-09-25 17:54:57 -07004366 priv->ucode_data.len);
4367
4368 /* Inst bytecount must be last to set up, bit 31 signals uCode
4369 * that all new ptr/size info is in place */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004370 iwl_write_prph(priv, BSM_DRAM_INST_BYTECOUNT_REG,
Zhu Yib481de92007-09-25 17:54:57 -07004371 priv->ucode_code.len | BSM_DRAM_INST_LOAD);
4372
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004373 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004374
4375 spin_unlock_irqrestore(&priv->lock, flags);
4376
4377 IWL_DEBUG_INFO("Runtime uCode pointers are set.\n");
4378
4379 return rc;
4380}
4381
4382/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004383 * iwl4965_init_alive_start - Called after REPLY_ALIVE notification received
Zhu Yib481de92007-09-25 17:54:57 -07004384 *
4385 * Called after REPLY_ALIVE notification received from "initialize" uCode.
4386 *
4387 * The 4965 "initialize" ALIVE reply contains calibration data for:
4388 * Voltage, temperature, and MIMO tx gain correction, now stored in priv
4389 * (3945 does not contain this data).
4390 *
4391 * Tell "initialize" uCode to go ahead and load the runtime uCode.
4392*/
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004393static void iwl4965_init_alive_start(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004394{
4395 /* Check alive response for "valid" sign from uCode */
4396 if (priv->card_alive_init.is_valid != UCODE_VALID_OK) {
4397 /* We had an error bringing up the hardware, so take it
4398 * all the way back down so we can try again */
4399 IWL_DEBUG_INFO("Initialize Alive failed.\n");
4400 goto restart;
4401 }
4402
4403 /* Bootstrap uCode has loaded initialize uCode ... verify inst image.
4404 * This is a paranoid check, because we would not have gotten the
4405 * "initialize" alive if code weren't properly loaded. */
Emmanuel Grumbachb0692f22008-04-24 11:55:18 -07004406 if (iwl_verify_ucode(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07004407 /* Runtime instruction load was bad;
4408 * take it all the way back down so we can try again */
4409 IWL_DEBUG_INFO("Bad \"initialize\" uCode load.\n");
4410 goto restart;
4411 }
4412
4413 /* Calculate temperature */
4414 priv->temperature = iwl4965_get_temperature(priv);
4415
4416 /* Send pointers to protocol/runtime uCode image ... init code will
4417 * load and launch runtime uCode, which will send us another "Alive"
4418 * notification. */
4419 IWL_DEBUG_INFO("Initialization Alive received.\n");
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004420 if (iwl4965_set_ucode_ptrs(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07004421 /* Runtime instruction load won't happen;
4422 * take it all the way back down so we can try again */
4423 IWL_DEBUG_INFO("Couldn't set up uCode pointers.\n");
4424 goto restart;
4425 }
4426 return;
4427
4428 restart:
4429 queue_work(priv->workqueue, &priv->restart);
4430}
4431
4432
4433/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004434 * iwl4965_alive_start - called after REPLY_ALIVE notification received
Zhu Yib481de92007-09-25 17:54:57 -07004435 * from protocol/runtime uCode (initialization uCode's
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004436 * Alive gets handled by iwl4965_init_alive_start()).
Zhu Yib481de92007-09-25 17:54:57 -07004437 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004438static void iwl4965_alive_start(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004439{
Tomas Winkler57aab752008-04-14 21:16:03 -07004440 int ret = 0;
Zhu Yib481de92007-09-25 17:54:57 -07004441
4442 IWL_DEBUG_INFO("Runtime Alive received.\n");
4443
4444 if (priv->card_alive.is_valid != UCODE_VALID_OK) {
4445 /* We had an error bringing up the hardware, so take it
4446 * all the way back down so we can try again */
4447 IWL_DEBUG_INFO("Alive failed.\n");
4448 goto restart;
4449 }
4450
4451 /* Initialize uCode has loaded Runtime uCode ... verify inst image.
4452 * This is a paranoid check, because we would not have gotten the
4453 * "runtime" alive if code weren't properly loaded. */
Emmanuel Grumbachb0692f22008-04-24 11:55:18 -07004454 if (iwl_verify_ucode(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07004455 /* Runtime instruction load was bad;
4456 * take it all the way back down so we can try again */
4457 IWL_DEBUG_INFO("Bad runtime uCode load.\n");
4458 goto restart;
4459 }
4460
Assaf Kraussbf85ea42008-03-14 10:38:49 -07004461 iwlcore_clear_stations_table(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004462
Tomas Winkler57aab752008-04-14 21:16:03 -07004463 ret = priv->cfg->ops->lib->alive_notify(priv);
4464 if (ret) {
Zhu Yib481de92007-09-25 17:54:57 -07004465 IWL_WARNING("Could not complete ALIVE transition [ntf]: %d\n",
Tomas Winkler57aab752008-04-14 21:16:03 -07004466 ret);
Zhu Yib481de92007-09-25 17:54:57 -07004467 goto restart;
4468 }
4469
Ben Cahill9fbab512007-11-29 11:09:47 +08004470 /* After the ALIVE response, we can send host commands to 4965 uCode */
Zhu Yib481de92007-09-25 17:54:57 -07004471 set_bit(STATUS_ALIVE, &priv->status);
4472
4473 /* Clear out the uCode error bit if it is set */
4474 clear_bit(STATUS_FW_ERROR, &priv->status);
4475
Tomas Winklerfee12472008-04-03 16:05:21 -07004476 if (iwl_is_rfkill(priv))
Zhu Yib481de92007-09-25 17:54:57 -07004477 return;
4478
Zhu Yi5a66926a2008-01-14 17:46:18 -08004479 ieee80211_start_queues(priv->hw);
Zhu Yib481de92007-09-25 17:54:57 -07004480
4481 priv->active_rate = priv->rates_mask;
4482 priv->active_rate_basic = priv->rates_mask & IWL_BASIC_RATES_MASK;
4483
Tomas Winkler3109ece2008-03-28 16:33:35 -07004484 if (iwl_is_associated(priv)) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004485 struct iwl4965_rxon_cmd *active_rxon =
4486 (struct iwl4965_rxon_cmd *)(&priv->active_rxon);
Zhu Yib481de92007-09-25 17:54:57 -07004487
4488 memcpy(&priv->staging_rxon, &priv->active_rxon,
4489 sizeof(priv->staging_rxon));
4490 active_rxon->filter_flags &= ~RXON_FILTER_ASSOC_MSK;
4491 } else {
4492 /* Initialize our rx_config data */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004493 iwl4965_connection_init_rx_config(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004494 memcpy(priv->staging_rxon.node_addr, priv->mac_addr, ETH_ALEN);
4495 }
4496
Ben Cahill9fbab512007-11-29 11:09:47 +08004497 /* Configure Bluetooth device coexistence support */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004498 iwl4965_send_bt_config(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004499
4500 /* Configure the adapter for unassociated operation */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004501 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004502
4503 /* At this point, the NIC is initialized and operational */
4504 priv->notif_missed_beacons = 0;
Zhu Yib481de92007-09-25 17:54:57 -07004505
4506 iwl4965_rf_kill_ct_config(priv);
Zhu Yi5a66926a2008-01-14 17:46:18 -08004507
Reinette Chatrefe00b5a2008-04-03 16:05:23 -07004508 iwl_leds_register(priv);
4509
Zhu Yib481de92007-09-25 17:54:57 -07004510 IWL_DEBUG_INFO("ALIVE processing complete.\n");
Rick Farringtona9f46782008-03-18 14:57:49 -07004511 set_bit(STATUS_READY, &priv->status);
Zhu Yi5a66926a2008-01-14 17:46:18 -08004512 wake_up_interruptible(&priv->wait_command_queue);
Zhu Yib481de92007-09-25 17:54:57 -07004513
4514 if (priv->error_recovering)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004515 iwl4965_error_recovery(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004516
Mohamed Abbasc8381fd2008-03-28 16:21:05 -07004517 iwlcore_low_level_notify(priv, IWLCORE_START_EVT);
Mohamed Abbas84363e62008-04-04 16:59:58 -07004518 ieee80211_notify_mac(priv->hw, IEEE80211_NOTIFY_RE_ASSOC);
Zhu Yib481de92007-09-25 17:54:57 -07004519 return;
4520
4521 restart:
4522 queue_work(priv->workqueue, &priv->restart);
4523}
4524
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004525static void iwl4965_cancel_deferred_work(struct iwl_priv *priv);
Zhu Yib481de92007-09-25 17:54:57 -07004526
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004527static void __iwl4965_down(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004528{
4529 unsigned long flags;
4530 int exit_pending = test_bit(STATUS_EXIT_PENDING, &priv->status);
4531 struct ieee80211_conf *conf = NULL;
4532
4533 IWL_DEBUG_INFO(DRV_NAME " is going down\n");
4534
4535 conf = ieee80211_get_hw_conf(priv->hw);
4536
4537 if (!exit_pending)
4538 set_bit(STATUS_EXIT_PENDING, &priv->status);
4539
Mohamed Abbasab53d8a2008-03-25 16:33:36 -07004540 iwl_leds_unregister(priv);
4541
Mohamed Abbasc8381fd2008-03-28 16:21:05 -07004542 iwlcore_low_level_notify(priv, IWLCORE_STOP_EVT);
4543
Assaf Kraussbf85ea42008-03-14 10:38:49 -07004544 iwlcore_clear_stations_table(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004545
4546 /* Unblock any waiting calls */
4547 wake_up_interruptible_all(&priv->wait_command_queue);
4548
Zhu Yib481de92007-09-25 17:54:57 -07004549 /* Wipe out the EXIT_PENDING status bit if we are not actually
4550 * exiting the module */
4551 if (!exit_pending)
4552 clear_bit(STATUS_EXIT_PENDING, &priv->status);
4553
4554 /* stop and reset the on-board processor */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004555 iwl_write32(priv, CSR_RESET, CSR_RESET_REG_FLAG_NEVO_RESET);
Zhu Yib481de92007-09-25 17:54:57 -07004556
4557 /* tell the device to stop sending interrupts */
Mohamed Abbas0359fac2008-03-28 16:21:08 -07004558 spin_lock_irqsave(&priv->lock, flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004559 iwl4965_disable_interrupts(priv);
Mohamed Abbas0359fac2008-03-28 16:21:08 -07004560 spin_unlock_irqrestore(&priv->lock, flags);
4561 iwl_synchronize_irq(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004562
4563 if (priv->mac80211_registered)
4564 ieee80211_stop_queues(priv->hw);
4565
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004566 /* If we have not previously called iwl4965_init() then
Zhu Yib481de92007-09-25 17:54:57 -07004567 * clear all bits but the RF Kill and SUSPEND bits and return */
Tomas Winklerfee12472008-04-03 16:05:21 -07004568 if (!iwl_is_init(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07004569 priv->status = test_bit(STATUS_RF_KILL_HW, &priv->status) <<
4570 STATUS_RF_KILL_HW |
4571 test_bit(STATUS_RF_KILL_SW, &priv->status) <<
4572 STATUS_RF_KILL_SW |
Reinette Chatre97888642008-02-06 11:20:38 -08004573 test_bit(STATUS_GEO_CONFIGURED, &priv->status) <<
4574 STATUS_GEO_CONFIGURED |
Zhu Yib481de92007-09-25 17:54:57 -07004575 test_bit(STATUS_IN_SUSPEND, &priv->status) <<
4576 STATUS_IN_SUSPEND;
4577 goto exit;
4578 }
4579
4580 /* ...otherwise clear out all the status bits but the RF Kill and
4581 * SUSPEND bits and continue taking the NIC down. */
4582 priv->status &= test_bit(STATUS_RF_KILL_HW, &priv->status) <<
4583 STATUS_RF_KILL_HW |
4584 test_bit(STATUS_RF_KILL_SW, &priv->status) <<
4585 STATUS_RF_KILL_SW |
Reinette Chatre97888642008-02-06 11:20:38 -08004586 test_bit(STATUS_GEO_CONFIGURED, &priv->status) <<
4587 STATUS_GEO_CONFIGURED |
Zhu Yib481de92007-09-25 17:54:57 -07004588 test_bit(STATUS_IN_SUSPEND, &priv->status) <<
4589 STATUS_IN_SUSPEND |
4590 test_bit(STATUS_FW_ERROR, &priv->status) <<
4591 STATUS_FW_ERROR;
4592
4593 spin_lock_irqsave(&priv->lock, flags);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004594 iwl_clear_bit(priv, CSR_GP_CNTRL,
Ben Cahill9fbab512007-11-29 11:09:47 +08004595 CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
Zhu Yib481de92007-09-25 17:54:57 -07004596 spin_unlock_irqrestore(&priv->lock, flags);
4597
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004598 iwl4965_hw_txq_ctx_stop(priv);
4599 iwl4965_hw_rxq_stop(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004600
4601 spin_lock_irqsave(&priv->lock, flags);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004602 if (!iwl_grab_nic_access(priv)) {
4603 iwl_write_prph(priv, APMG_CLK_DIS_REG,
Zhu Yib481de92007-09-25 17:54:57 -07004604 APMG_CLK_VAL_DMA_CLK_RQT);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004605 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004606 }
4607 spin_unlock_irqrestore(&priv->lock, flags);
4608
4609 udelay(5);
4610
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004611 iwl4965_hw_nic_stop_master(priv);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004612 iwl_set_bit(priv, CSR_RESET, CSR_RESET_REG_FLAG_SW_RESET);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004613 iwl4965_hw_nic_reset(priv);
Ron Rindjunsky399f4902008-04-23 17:14:56 -07004614 priv->cfg->ops->lib->free_shared_mem(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004615
4616 exit:
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004617 memset(&priv->card_alive, 0, sizeof(struct iwl4965_alive_resp));
Zhu Yib481de92007-09-25 17:54:57 -07004618
4619 if (priv->ibss_beacon)
4620 dev_kfree_skb(priv->ibss_beacon);
4621 priv->ibss_beacon = NULL;
4622
4623 /* clear out any free frames */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004624 iwl4965_clear_free_frames(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004625}
4626
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004627static void iwl4965_down(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004628{
4629 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004630 __iwl4965_down(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004631 mutex_unlock(&priv->mutex);
Zhu Yib24d22b2007-12-19 13:59:52 +08004632
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004633 iwl4965_cancel_deferred_work(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004634}
4635
4636#define MAX_HW_RESTARTS 5
4637
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004638static int __iwl4965_up(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004639{
Tomas Winkler57aab752008-04-14 21:16:03 -07004640 int i;
4641 int ret;
Zhu Yib481de92007-09-25 17:54:57 -07004642
4643 if (test_bit(STATUS_EXIT_PENDING, &priv->status)) {
4644 IWL_WARNING("Exit pending; will not bring the NIC up\n");
4645 return -EIO;
4646 }
4647
4648 if (test_bit(STATUS_RF_KILL_SW, &priv->status)) {
4649 IWL_WARNING("Radio disabled by SW RF kill (module "
4650 "parameter)\n");
Mohamed Abbasad97edd2008-03-28 16:21:06 -07004651 iwl_rfkill_set_hw_state(priv);
Zhu Yie655b9f2008-01-24 02:19:38 -08004652 return -ENODEV;
4653 }
4654
Reinette Chatree903fbd2008-01-30 22:05:15 -08004655 if (!priv->ucode_data_backup.v_addr || !priv->ucode_data.v_addr) {
4656 IWL_ERROR("ucode not available for device bringup\n");
4657 return -EIO;
4658 }
4659
Zhu Yie655b9f2008-01-24 02:19:38 -08004660 /* If platform's RF_KILL switch is NOT set to KILL */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004661 if (iwl_read32(priv, CSR_GP_CNTRL) &
Zhu Yie655b9f2008-01-24 02:19:38 -08004662 CSR_GP_CNTRL_REG_FLAG_HW_RF_KILL_SW)
4663 clear_bit(STATUS_RF_KILL_HW, &priv->status);
4664 else {
4665 set_bit(STATUS_RF_KILL_HW, &priv->status);
4666 if (!test_bit(STATUS_IN_SUSPEND, &priv->status)) {
Mohamed Abbasad97edd2008-03-28 16:21:06 -07004667 iwl_rfkill_set_hw_state(priv);
Zhu Yie655b9f2008-01-24 02:19:38 -08004668 IWL_WARNING("Radio disabled by HW RF Kill switch\n");
4669 return -ENODEV;
4670 }
Zhu Yib481de92007-09-25 17:54:57 -07004671 }
4672
Mohamed Abbasad97edd2008-03-28 16:21:06 -07004673 iwl_rfkill_set_hw_state(priv);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004674 iwl_write32(priv, CSR_INT, 0xFFFFFFFF);
Zhu Yib481de92007-09-25 17:54:57 -07004675
Ron Rindjunsky399f4902008-04-23 17:14:56 -07004676 ret = priv->cfg->ops->lib->alloc_shared_mem(priv);
4677 if (ret) {
4678 IWL_ERROR("Unable to allocate shared memory\n");
4679 return ret;
4680 }
4681
Tomas Winkler57aab752008-04-14 21:16:03 -07004682 ret = priv->cfg->ops->lib->hw_nic_init(priv);
4683 if (ret) {
4684 IWL_ERROR("Unable to init nic\n");
4685 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07004686 }
4687
4688 /* make sure rfkill handshake bits are cleared */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004689 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
4690 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR,
Zhu Yib481de92007-09-25 17:54:57 -07004691 CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
4692
4693 /* clear (again), then enable host interrupts */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004694 iwl_write32(priv, CSR_INT, 0xFFFFFFFF);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004695 iwl4965_enable_interrupts(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004696
4697 /* really make sure rfkill handshake bits are cleared */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004698 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
4699 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
Zhu Yib481de92007-09-25 17:54:57 -07004700
4701 /* Copy original ucode data image from disk into backup cache.
4702 * This will be used to initialize the on-board processor's
4703 * data SRAM for a clean start when the runtime program first loads. */
4704 memcpy(priv->ucode_data_backup.v_addr, priv->ucode_data.v_addr,
Zhu Yi5a66926a2008-01-14 17:46:18 -08004705 priv->ucode_data.len);
Zhu Yib481de92007-09-25 17:54:57 -07004706
Zhu Yie655b9f2008-01-24 02:19:38 -08004707 /* We return success when we resume from suspend and rf_kill is on. */
4708 if (test_bit(STATUS_RF_KILL_HW, &priv->status))
Zhu Yib481de92007-09-25 17:54:57 -07004709 return 0;
Zhu Yib481de92007-09-25 17:54:57 -07004710
4711 for (i = 0; i < MAX_HW_RESTARTS; i++) {
4712
Assaf Kraussbf85ea42008-03-14 10:38:49 -07004713 iwlcore_clear_stations_table(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004714
4715 /* load bootstrap state machine,
4716 * load bootstrap program into processor's memory,
4717 * prepare to load the "initialize" uCode */
Tomas Winkler57aab752008-04-14 21:16:03 -07004718 ret = priv->cfg->ops->lib->load_ucode(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004719
Tomas Winkler57aab752008-04-14 21:16:03 -07004720 if (ret) {
4721 IWL_ERROR("Unable to set up bootstrap uCode: %d\n", ret);
Zhu Yib481de92007-09-25 17:54:57 -07004722 continue;
4723 }
4724
4725 /* start card; "initialize" will load runtime ucode */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004726 iwl4965_nic_start(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004727
Zhu Yib481de92007-09-25 17:54:57 -07004728 IWL_DEBUG_INFO(DRV_NAME " is coming up\n");
4729
4730 return 0;
4731 }
4732
4733 set_bit(STATUS_EXIT_PENDING, &priv->status);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004734 __iwl4965_down(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004735
4736 /* tried to restart and config the device for as long as our
4737 * patience could withstand */
4738 IWL_ERROR("Unable to initialize device after %d attempts.\n", i);
4739 return -EIO;
4740}
4741
4742
4743/*****************************************************************************
4744 *
4745 * Workqueue callbacks
4746 *
4747 *****************************************************************************/
4748
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004749static void iwl4965_bg_init_alive_start(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07004750{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004751 struct iwl_priv *priv =
4752 container_of(data, struct iwl_priv, init_alive_start.work);
Zhu Yib481de92007-09-25 17:54:57 -07004753
4754 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
4755 return;
4756
4757 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004758 iwl4965_init_alive_start(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004759 mutex_unlock(&priv->mutex);
4760}
4761
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004762static void iwl4965_bg_alive_start(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07004763{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004764 struct iwl_priv *priv =
4765 container_of(data, struct iwl_priv, alive_start.work);
Zhu Yib481de92007-09-25 17:54:57 -07004766
4767 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
4768 return;
4769
4770 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004771 iwl4965_alive_start(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004772 mutex_unlock(&priv->mutex);
4773}
4774
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004775static void iwl4965_bg_rf_kill(struct work_struct *work)
Zhu Yib481de92007-09-25 17:54:57 -07004776{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004777 struct iwl_priv *priv = container_of(work, struct iwl_priv, rf_kill);
Zhu Yib481de92007-09-25 17:54:57 -07004778
4779 wake_up_interruptible(&priv->wait_command_queue);
4780
4781 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
4782 return;
4783
4784 mutex_lock(&priv->mutex);
4785
Tomas Winklerfee12472008-04-03 16:05:21 -07004786 if (!iwl_is_rfkill(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07004787 IWL_DEBUG(IWL_DL_INFO | IWL_DL_RF_KILL,
4788 "HW and/or SW RF Kill no longer active, restarting "
4789 "device\n");
4790 if (!test_bit(STATUS_EXIT_PENDING, &priv->status))
4791 queue_work(priv->workqueue, &priv->restart);
4792 } else {
Mohamed Abbasad97edd2008-03-28 16:21:06 -07004793 /* make sure mac80211 stop sending Tx frame */
4794 if (priv->mac80211_registered)
4795 ieee80211_stop_queues(priv->hw);
Zhu Yib481de92007-09-25 17:54:57 -07004796
4797 if (!test_bit(STATUS_RF_KILL_HW, &priv->status))
4798 IWL_DEBUG_RF_KILL("Can not turn radio back on - "
4799 "disabled by SW switch\n");
4800 else
4801 IWL_WARNING("Radio Frequency Kill Switch is On:\n"
4802 "Kill switch must be turned off for "
4803 "wireless networking to work.\n");
4804 }
Mohamed Abbasad97edd2008-03-28 16:21:06 -07004805 iwl_rfkill_set_hw_state(priv);
4806
Zhu Yib481de92007-09-25 17:54:57 -07004807 mutex_unlock(&priv->mutex);
4808}
4809
4810#define IWL_SCAN_CHECK_WATCHDOG (7 * HZ)
4811
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004812static void iwl4965_bg_scan_check(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07004813{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004814 struct iwl_priv *priv =
4815 container_of(data, struct iwl_priv, scan_check.work);
Zhu Yib481de92007-09-25 17:54:57 -07004816
4817 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
4818 return;
4819
4820 mutex_lock(&priv->mutex);
4821 if (test_bit(STATUS_SCANNING, &priv->status) ||
4822 test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
4823 IWL_DEBUG(IWL_DL_INFO | IWL_DL_SCAN,
4824 "Scan completion watchdog resetting adapter (%dms)\n",
4825 jiffies_to_msecs(IWL_SCAN_CHECK_WATCHDOG));
mabbas052c4b92007-10-25 17:15:43 +08004826
Zhu Yib481de92007-09-25 17:54:57 -07004827 if (!test_bit(STATUS_EXIT_PENDING, &priv->status))
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004828 iwl4965_send_scan_abort(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004829 }
4830 mutex_unlock(&priv->mutex);
4831}
4832
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004833static void iwl4965_bg_request_scan(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07004834{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004835 struct iwl_priv *priv =
4836 container_of(data, struct iwl_priv, request_scan);
Tomas Winkler857485c2008-03-21 13:53:44 -07004837 struct iwl_host_cmd cmd = {
Zhu Yib481de92007-09-25 17:54:57 -07004838 .id = REPLY_SCAN_CMD,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004839 .len = sizeof(struct iwl4965_scan_cmd),
Zhu Yib481de92007-09-25 17:54:57 -07004840 .meta.flags = CMD_SIZE_HUGE,
4841 };
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004842 struct iwl4965_scan_cmd *scan;
Zhu Yib481de92007-09-25 17:54:57 -07004843 struct ieee80211_conf *conf = NULL;
Tomas Winkler78330fd2008-02-06 02:37:18 +02004844 u16 cmd_len;
Johannes Berg8318d782008-01-24 19:38:38 +01004845 enum ieee80211_band band;
Tomas Winkler78330fd2008-02-06 02:37:18 +02004846 u8 direct_mask;
Tomas Winkler857485c2008-03-21 13:53:44 -07004847 int ret = 0;
Zhu Yib481de92007-09-25 17:54:57 -07004848
4849 conf = ieee80211_get_hw_conf(priv->hw);
4850
4851 mutex_lock(&priv->mutex);
4852
Tomas Winklerfee12472008-04-03 16:05:21 -07004853 if (!iwl_is_ready(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07004854 IWL_WARNING("request scan called when driver not ready.\n");
4855 goto done;
4856 }
4857
4858 /* Make sure the scan wasn't cancelled before this queued work
4859 * was given the chance to run... */
4860 if (!test_bit(STATUS_SCANNING, &priv->status))
4861 goto done;
4862
4863 /* This should never be called or scheduled if there is currently
4864 * a scan active in the hardware. */
4865 if (test_bit(STATUS_SCAN_HW, &priv->status)) {
4866 IWL_DEBUG_INFO("Multiple concurrent scan requests in parallel. "
4867 "Ignoring second request.\n");
Tomas Winkler857485c2008-03-21 13:53:44 -07004868 ret = -EIO;
Zhu Yib481de92007-09-25 17:54:57 -07004869 goto done;
4870 }
4871
4872 if (test_bit(STATUS_EXIT_PENDING, &priv->status)) {
4873 IWL_DEBUG_SCAN("Aborting scan due to device shutdown\n");
4874 goto done;
4875 }
4876
4877 if (test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
4878 IWL_DEBUG_HC("Scan request while abort pending. Queuing.\n");
4879 goto done;
4880 }
4881
Tomas Winklerfee12472008-04-03 16:05:21 -07004882 if (iwl_is_rfkill(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07004883 IWL_DEBUG_HC("Aborting scan due to RF Kill activation\n");
4884 goto done;
4885 }
4886
4887 if (!test_bit(STATUS_READY, &priv->status)) {
4888 IWL_DEBUG_HC("Scan request while uninitialized. Queuing.\n");
4889 goto done;
4890 }
4891
4892 if (!priv->scan_bands) {
4893 IWL_DEBUG_HC("Aborting scan due to no requested bands\n");
4894 goto done;
4895 }
4896
4897 if (!priv->scan) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004898 priv->scan = kmalloc(sizeof(struct iwl4965_scan_cmd) +
Zhu Yib481de92007-09-25 17:54:57 -07004899 IWL_MAX_SCAN_SIZE, GFP_KERNEL);
4900 if (!priv->scan) {
Tomas Winkler857485c2008-03-21 13:53:44 -07004901 ret = -ENOMEM;
Zhu Yib481de92007-09-25 17:54:57 -07004902 goto done;
4903 }
4904 }
4905 scan = priv->scan;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004906 memset(scan, 0, sizeof(struct iwl4965_scan_cmd) + IWL_MAX_SCAN_SIZE);
Zhu Yib481de92007-09-25 17:54:57 -07004907
4908 scan->quiet_plcp_th = IWL_PLCP_QUIET_THRESH;
4909 scan->quiet_time = IWL_ACTIVE_QUIET_TIME;
4910
Tomas Winkler3109ece2008-03-28 16:33:35 -07004911 if (iwl_is_associated(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07004912 u16 interval = 0;
4913 u32 extra;
4914 u32 suspend_time = 100;
4915 u32 scan_suspend_time = 100;
4916 unsigned long flags;
4917
4918 IWL_DEBUG_INFO("Scanning while associated...\n");
4919
4920 spin_lock_irqsave(&priv->lock, flags);
4921 interval = priv->beacon_int;
4922 spin_unlock_irqrestore(&priv->lock, flags);
4923
4924 scan->suspend_time = 0;
mabbas052c4b92007-10-25 17:15:43 +08004925 scan->max_out_time = cpu_to_le32(200 * 1024);
Zhu Yib481de92007-09-25 17:54:57 -07004926 if (!interval)
4927 interval = suspend_time;
4928
4929 extra = (suspend_time / interval) << 22;
4930 scan_suspend_time = (extra |
4931 ((suspend_time % interval) * 1024));
4932 scan->suspend_time = cpu_to_le32(scan_suspend_time);
4933 IWL_DEBUG_SCAN("suspend_time 0x%X beacon interval %d\n",
4934 scan_suspend_time, interval);
4935 }
4936
4937 /* We should add the ability for user to lock to PASSIVE ONLY */
4938 if (priv->one_direct_scan) {
4939 IWL_DEBUG_SCAN
4940 ("Kicking off one direct scan for '%s'\n",
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004941 iwl4965_escape_essid(priv->direct_ssid,
Zhu Yib481de92007-09-25 17:54:57 -07004942 priv->direct_ssid_len));
4943 scan->direct_scan[0].id = WLAN_EID_SSID;
4944 scan->direct_scan[0].len = priv->direct_ssid_len;
4945 memcpy(scan->direct_scan[0].ssid,
4946 priv->direct_ssid, priv->direct_ssid_len);
4947 direct_mask = 1;
Tomas Winkler3109ece2008-03-28 16:33:35 -07004948 } else if (!iwl_is_associated(priv) && priv->essid_len) {
Bill Moss786b4552008-04-17 16:03:40 -07004949 IWL_DEBUG_SCAN
4950 ("Kicking off one direct scan for '%s' when not associated\n",
4951 iwl4965_escape_essid(priv->essid, priv->essid_len));
Zhu Yib481de92007-09-25 17:54:57 -07004952 scan->direct_scan[0].id = WLAN_EID_SSID;
4953 scan->direct_scan[0].len = priv->essid_len;
4954 memcpy(scan->direct_scan[0].ssid, priv->essid, priv->essid_len);
4955 direct_mask = 1;
Tomas Winkler857485c2008-03-21 13:53:44 -07004956 } else {
Bill Moss786b4552008-04-17 16:03:40 -07004957 IWL_DEBUG_SCAN("Kicking off one indirect scan.\n");
Zhu Yib481de92007-09-25 17:54:57 -07004958 direct_mask = 0;
Tomas Winkler857485c2008-03-21 13:53:44 -07004959 }
Zhu Yib481de92007-09-25 17:54:57 -07004960
Zhu Yib481de92007-09-25 17:54:57 -07004961 scan->tx_cmd.tx_flags = TX_CMD_FLG_SEQ_CTL_MSK;
Tomas Winkler5425e492008-04-15 16:01:38 -07004962 scan->tx_cmd.sta_id = priv->hw_params.bcast_sta_id;
Zhu Yib481de92007-09-25 17:54:57 -07004963 scan->tx_cmd.stop_time.life_time = TX_CMD_LIFE_TIME_INFINITE;
4964
Zhu Yib481de92007-09-25 17:54:57 -07004965
4966 switch (priv->scan_bands) {
4967 case 2:
4968 scan->flags = RXON_FLG_BAND_24G_MSK | RXON_FLG_AUTO_DETECT_MSK;
4969 scan->tx_cmd.rate_n_flags =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004970 iwl4965_hw_set_rate_n_flags(IWL_RATE_1M_PLCP,
Zhu Yib481de92007-09-25 17:54:57 -07004971 RATE_MCS_ANT_B_MSK|RATE_MCS_CCK_MSK);
4972
4973 scan->good_CRC_th = 0;
Johannes Berg8318d782008-01-24 19:38:38 +01004974 band = IEEE80211_BAND_2GHZ;
Zhu Yib481de92007-09-25 17:54:57 -07004975 break;
4976
4977 case 1:
4978 scan->tx_cmd.rate_n_flags =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004979 iwl4965_hw_set_rate_n_flags(IWL_RATE_6M_PLCP,
Zhu Yib481de92007-09-25 17:54:57 -07004980 RATE_MCS_ANT_B_MSK);
4981 scan->good_CRC_th = IWL_GOOD_CRC_TH;
Johannes Berg8318d782008-01-24 19:38:38 +01004982 band = IEEE80211_BAND_5GHZ;
Zhu Yib481de92007-09-25 17:54:57 -07004983 break;
4984
4985 default:
4986 IWL_WARNING("Invalid scan band count\n");
4987 goto done;
4988 }
4989
Tomas Winkler78330fd2008-02-06 02:37:18 +02004990 /* We don't build a direct scan probe request; the uCode will do
4991 * that based on the direct_mask added to each channel entry */
4992 cmd_len = iwl4965_fill_probe_req(priv, band,
4993 (struct ieee80211_mgmt *)scan->data,
4994 IWL_MAX_SCAN_SIZE - sizeof(*scan), 0);
4995
4996 scan->tx_cmd.len = cpu_to_le16(cmd_len);
Zhu Yib481de92007-09-25 17:54:57 -07004997 /* select Rx chains */
4998
4999 /* Force use of chains B and C (0x6) for scan Rx.
5000 * Avoid A (0x1) because of its off-channel reception on A-band.
5001 * MIMO is not used here, but value is required to make uCode happy. */
5002 scan->rx_chain = RXON_RX_CHAIN_DRIVER_FORCE_MSK |
5003 cpu_to_le16((0x7 << RXON_RX_CHAIN_VALID_POS) |
5004 (0x6 << RXON_RX_CHAIN_FORCE_SEL_POS) |
5005 (0x7 << RXON_RX_CHAIN_FORCE_MIMO_SEL_POS));
5006
5007 if (priv->iw_mode == IEEE80211_IF_TYPE_MNTR)
5008 scan->filter_flags = RXON_FILTER_PROMISC_MSK;
5009
Bill Moss786b4552008-04-17 16:03:40 -07005010 if (direct_mask)
Reinette Chatre26c0f032008-03-11 16:17:15 -07005011 scan->channel_count =
5012 iwl4965_get_channels_for_scan(
5013 priv, band, 1, /* active */
5014 direct_mask,
5015 (void *)&scan->data[le16_to_cpu(scan->tx_cmd.len)]);
Bill Moss786b4552008-04-17 16:03:40 -07005016 else
Reinette Chatre26c0f032008-03-11 16:17:15 -07005017 scan->channel_count =
5018 iwl4965_get_channels_for_scan(
5019 priv, band, 0, /* passive */
5020 direct_mask,
5021 (void *)&scan->data[le16_to_cpu(scan->tx_cmd.len)]);
Zhu Yib481de92007-09-25 17:54:57 -07005022
Mohamed Abbas5da4b552008-04-21 15:41:51 -07005023 scan->filter_flags |= (RXON_FILTER_ACCEPT_GRP_MSK |
5024 RXON_FILTER_BCON_AWARE_MSK);
Zhu Yib481de92007-09-25 17:54:57 -07005025 cmd.len += le16_to_cpu(scan->tx_cmd.len) +
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005026 scan->channel_count * sizeof(struct iwl4965_scan_channel);
Zhu Yib481de92007-09-25 17:54:57 -07005027 cmd.data = scan;
5028 scan->len = cpu_to_le16(cmd.len);
5029
5030 set_bit(STATUS_SCAN_HW, &priv->status);
Tomas Winkler857485c2008-03-21 13:53:44 -07005031 ret = iwl_send_cmd_sync(priv, &cmd);
5032 if (ret)
Zhu Yib481de92007-09-25 17:54:57 -07005033 goto done;
5034
5035 queue_delayed_work(priv->workqueue, &priv->scan_check,
5036 IWL_SCAN_CHECK_WATCHDOG);
5037
5038 mutex_unlock(&priv->mutex);
5039 return;
5040
5041 done:
Ian Schram01ebd062007-10-25 17:15:22 +08005042 /* inform mac80211 scan aborted */
Zhu Yib481de92007-09-25 17:54:57 -07005043 queue_work(priv->workqueue, &priv->scan_completed);
5044 mutex_unlock(&priv->mutex);
5045}
5046
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005047static void iwl4965_bg_up(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07005048{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005049 struct iwl_priv *priv = container_of(data, struct iwl_priv, up);
Zhu Yib481de92007-09-25 17:54:57 -07005050
5051 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
5052 return;
5053
5054 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005055 __iwl4965_up(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005056 mutex_unlock(&priv->mutex);
5057}
5058
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005059static void iwl4965_bg_restart(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07005060{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005061 struct iwl_priv *priv = container_of(data, struct iwl_priv, restart);
Zhu Yib481de92007-09-25 17:54:57 -07005062
5063 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
5064 return;
5065
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005066 iwl4965_down(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005067 queue_work(priv->workqueue, &priv->up);
5068}
5069
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005070static void iwl4965_bg_rx_replenish(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07005071{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005072 struct iwl_priv *priv =
5073 container_of(data, struct iwl_priv, rx_replenish);
Zhu Yib481de92007-09-25 17:54:57 -07005074
5075 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
5076 return;
5077
5078 mutex_lock(&priv->mutex);
Tomas Winklera55360e2008-05-05 10:22:28 +08005079 iwl_rx_replenish(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005080 mutex_unlock(&priv->mutex);
5081}
5082
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08005083#define IWL_DELAY_NEXT_SCAN (HZ*2)
5084
Reinette Chatre508e32e2008-04-14 21:16:13 -07005085static void iwl4965_post_associate(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005086{
Zhu Yib481de92007-09-25 17:54:57 -07005087 struct ieee80211_conf *conf = NULL;
Tomas Winkler857485c2008-03-21 13:53:44 -07005088 int ret = 0;
Joe Perches0795af52007-10-03 17:59:30 -07005089 DECLARE_MAC_BUF(mac);
Zhu Yib481de92007-09-25 17:54:57 -07005090
5091 if (priv->iw_mode == IEEE80211_IF_TYPE_AP) {
5092 IWL_ERROR("%s Should not be called in AP mode\n", __FUNCTION__);
5093 return;
5094 }
5095
Joe Perches0795af52007-10-03 17:59:30 -07005096 IWL_DEBUG_ASSOC("Associated as %d to: %s\n",
5097 priv->assoc_id,
5098 print_mac(mac, priv->active_rxon.bssid_addr));
Zhu Yib481de92007-09-25 17:54:57 -07005099
5100
5101 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
5102 return;
5103
Zhu Yib481de92007-09-25 17:54:57 -07005104
Reinette Chatre508e32e2008-04-14 21:16:13 -07005105 if (!priv->vif || !priv->is_open)
Mohamed Abbas948c1712007-10-25 17:15:45 +08005106 return;
Reinette Chatre508e32e2008-04-14 21:16:13 -07005107
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005108 iwl4965_scan_cancel_timeout(priv, 200);
mabbas052c4b92007-10-25 17:15:43 +08005109
Zhu Yib481de92007-09-25 17:54:57 -07005110 conf = ieee80211_get_hw_conf(priv->hw);
5111
5112 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005113 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005114
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005115 memset(&priv->rxon_timing, 0, sizeof(struct iwl4965_rxon_time_cmd));
5116 iwl4965_setup_rxon_timing(priv);
Tomas Winkler857485c2008-03-21 13:53:44 -07005117 ret = iwl_send_cmd_pdu(priv, REPLY_RXON_TIMING,
Zhu Yib481de92007-09-25 17:54:57 -07005118 sizeof(priv->rxon_timing), &priv->rxon_timing);
Tomas Winkler857485c2008-03-21 13:53:44 -07005119 if (ret)
Zhu Yib481de92007-09-25 17:54:57 -07005120 IWL_WARNING("REPLY_RXON_TIMING failed - "
5121 "Attempting to continue.\n");
5122
5123 priv->staging_rxon.filter_flags |= RXON_FILTER_ASSOC_MSK;
5124
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08005125#ifdef CONFIG_IWL4965_HT
Ron Rindjunskyfd105e72007-11-26 16:14:39 +02005126 if (priv->current_ht_config.is_ht)
5127 iwl4965_set_rxon_ht(priv, &priv->current_ht_config);
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08005128#endif /* CONFIG_IWL4965_HT*/
Ron Rindjunskyc7de35c2008-04-23 17:15:05 -07005129 iwl_set_rxon_chain(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005130 priv->staging_rxon.assoc_id = cpu_to_le16(priv->assoc_id);
5131
5132 IWL_DEBUG_ASSOC("assoc id %d beacon interval %d\n",
5133 priv->assoc_id, priv->beacon_int);
5134
5135 if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_PREAMBLE)
5136 priv->staging_rxon.flags |= RXON_FLG_SHORT_PREAMBLE_MSK;
5137 else
5138 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_PREAMBLE_MSK;
5139
5140 if (priv->staging_rxon.flags & RXON_FLG_BAND_24G_MSK) {
5141 if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_SLOT_TIME)
5142 priv->staging_rxon.flags |= RXON_FLG_SHORT_SLOT_MSK;
5143 else
5144 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
5145
5146 if (priv->iw_mode == IEEE80211_IF_TYPE_IBSS)
5147 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
5148
5149 }
5150
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005151 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005152
5153 switch (priv->iw_mode) {
5154 case IEEE80211_IF_TYPE_STA:
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005155 iwl4965_rate_scale_init(priv->hw, IWL_AP_ID);
Zhu Yib481de92007-09-25 17:54:57 -07005156 break;
5157
5158 case IEEE80211_IF_TYPE_IBSS:
5159
5160 /* clear out the station table */
Assaf Kraussbf85ea42008-03-14 10:38:49 -07005161 iwlcore_clear_stations_table(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005162
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005163 iwl4965_rxon_add_station(priv, iwl4965_broadcast_addr, 0);
5164 iwl4965_rxon_add_station(priv, priv->bssid, 0);
5165 iwl4965_rate_scale_init(priv->hw, IWL_STA_ID);
5166 iwl4965_send_beacon_cmd(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005167
5168 break;
5169
5170 default:
5171 IWL_ERROR("%s Should not be called in %d mode\n",
5172 __FUNCTION__, priv->iw_mode);
5173 break;
5174 }
5175
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005176 iwl4965_sequence_reset(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005177
Zhu Yib481de92007-09-25 17:54:57 -07005178 /* Enable Rx differential gain and sensitivity calibrations */
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -07005179 iwl_chain_noise_reset(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005180 priv->start_calib = 1;
Zhu Yib481de92007-09-25 17:54:57 -07005181
5182 if (priv->iw_mode == IEEE80211_IF_TYPE_IBSS)
5183 priv->assoc_station_added = 1;
5184
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005185 iwl4965_activate_qos(priv, 0);
Ron Rindjunsky292ae172008-02-06 11:20:39 -08005186
Mohamed Abbas5da4b552008-04-21 15:41:51 -07005187 iwl_power_update_mode(priv, 0);
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08005188 /* we have just associated, don't start scan too early */
5189 priv->next_scan_jiffies = jiffies + IWL_DELAY_NEXT_SCAN;
Reinette Chatre508e32e2008-04-14 21:16:13 -07005190}
5191
5192
5193static void iwl4965_bg_post_associate(struct work_struct *data)
5194{
5195 struct iwl_priv *priv = container_of(data, struct iwl_priv,
5196 post_associate.work);
5197
5198 mutex_lock(&priv->mutex);
5199 iwl4965_post_associate(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005200 mutex_unlock(&priv->mutex);
Reinette Chatre508e32e2008-04-14 21:16:13 -07005201
Zhu Yib481de92007-09-25 17:54:57 -07005202}
5203
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005204static void iwl4965_bg_abort_scan(struct work_struct *work)
Zhu Yib481de92007-09-25 17:54:57 -07005205{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005206 struct iwl_priv *priv = container_of(work, struct iwl_priv, abort_scan);
Zhu Yib481de92007-09-25 17:54:57 -07005207
Tomas Winklerfee12472008-04-03 16:05:21 -07005208 if (!iwl_is_ready(priv))
Zhu Yib481de92007-09-25 17:54:57 -07005209 return;
5210
5211 mutex_lock(&priv->mutex);
5212
5213 set_bit(STATUS_SCAN_ABORTING, &priv->status);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005214 iwl4965_send_scan_abort(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005215
5216 mutex_unlock(&priv->mutex);
5217}
5218
Zhu Yi76bb77e2007-11-22 10:53:22 +08005219static int iwl4965_mac_config(struct ieee80211_hw *hw, struct ieee80211_conf *conf);
5220
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005221static void iwl4965_bg_scan_completed(struct work_struct *work)
Zhu Yib481de92007-09-25 17:54:57 -07005222{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005223 struct iwl_priv *priv =
5224 container_of(work, struct iwl_priv, scan_completed);
Zhu Yib481de92007-09-25 17:54:57 -07005225
5226 IWL_DEBUG(IWL_DL_INFO | IWL_DL_SCAN, "SCAN complete scan\n");
5227
5228 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
5229 return;
5230
Zhu Yia0646472007-12-20 14:10:01 +08005231 if (test_bit(STATUS_CONF_PENDING, &priv->status))
5232 iwl4965_mac_config(priv->hw, ieee80211_get_hw_conf(priv->hw));
Zhu Yi76bb77e2007-11-22 10:53:22 +08005233
Zhu Yib481de92007-09-25 17:54:57 -07005234 ieee80211_scan_completed(priv->hw);
5235
5236 /* Since setting the TXPOWER may have been deferred while
5237 * performing the scan, fire one off */
5238 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005239 iwl4965_hw_reg_send_txpower(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005240 mutex_unlock(&priv->mutex);
5241}
5242
5243/*****************************************************************************
5244 *
5245 * mac80211 entry point functions
5246 *
5247 *****************************************************************************/
5248
Zhu Yi5a66926a2008-01-14 17:46:18 -08005249#define UCODE_READY_TIMEOUT (2 * HZ)
5250
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005251static int iwl4965_mac_start(struct ieee80211_hw *hw)
Zhu Yib481de92007-09-25 17:54:57 -07005252{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005253 struct iwl_priv *priv = hw->priv;
Zhu Yi5a66926a2008-01-14 17:46:18 -08005254 int ret;
Zhu Yib481de92007-09-25 17:54:57 -07005255
5256 IWL_DEBUG_MAC80211("enter\n");
5257
Zhu Yi5a66926a2008-01-14 17:46:18 -08005258 if (pci_enable_device(priv->pci_dev)) {
5259 IWL_ERROR("Fail to pci_enable_device\n");
5260 return -ENODEV;
5261 }
5262 pci_restore_state(priv->pci_dev);
5263 pci_enable_msi(priv->pci_dev);
5264
5265 ret = request_irq(priv->pci_dev->irq, iwl4965_isr, IRQF_SHARED,
5266 DRV_NAME, priv);
5267 if (ret) {
5268 IWL_ERROR("Error allocating IRQ %d\n", priv->pci_dev->irq);
5269 goto out_disable_msi;
5270 }
5271
Zhu Yib481de92007-09-25 17:54:57 -07005272 /* we should be verifying the device is ready to be opened */
5273 mutex_lock(&priv->mutex);
5274
Zhu Yi5a66926a2008-01-14 17:46:18 -08005275 memset(&priv->staging_rxon, 0, sizeof(struct iwl4965_rxon_cmd));
5276 /* fetch ucode file from disk, alloc and copy to bus-master buffers ...
5277 * ucode filename and max sizes are card-specific. */
5278
5279 if (!priv->ucode_code.len) {
5280 ret = iwl4965_read_ucode(priv);
5281 if (ret) {
5282 IWL_ERROR("Could not read microcode: %d\n", ret);
5283 mutex_unlock(&priv->mutex);
5284 goto out_release_irq;
5285 }
5286 }
5287
Zhu Yie655b9f2008-01-24 02:19:38 -08005288 ret = __iwl4965_up(priv);
Zhu Yi5a66926a2008-01-14 17:46:18 -08005289
Zhu Yib481de92007-09-25 17:54:57 -07005290 mutex_unlock(&priv->mutex);
Zhu Yi5a66926a2008-01-14 17:46:18 -08005291
Zhu Yie655b9f2008-01-24 02:19:38 -08005292 if (ret)
5293 goto out_release_irq;
5294
5295 IWL_DEBUG_INFO("Start UP work done.\n");
5296
5297 if (test_bit(STATUS_IN_SUSPEND, &priv->status))
5298 return 0;
5299
Zhu Yi5a66926a2008-01-14 17:46:18 -08005300 /* Wait for START_ALIVE from ucode. Otherwise callbacks from
5301 * mac80211 will not be run successfully. */
5302 ret = wait_event_interruptible_timeout(priv->wait_command_queue,
5303 test_bit(STATUS_READY, &priv->status),
5304 UCODE_READY_TIMEOUT);
5305 if (!ret) {
5306 if (!test_bit(STATUS_READY, &priv->status)) {
5307 IWL_ERROR("Wait for START_ALIVE timeout after %dms.\n",
5308 jiffies_to_msecs(UCODE_READY_TIMEOUT));
5309 ret = -ETIMEDOUT;
5310 goto out_release_irq;
5311 }
5312 }
5313
Zhu Yie655b9f2008-01-24 02:19:38 -08005314 priv->is_open = 1;
Zhu Yib481de92007-09-25 17:54:57 -07005315 IWL_DEBUG_MAC80211("leave\n");
5316 return 0;
Zhu Yi5a66926a2008-01-14 17:46:18 -08005317
5318out_release_irq:
5319 free_irq(priv->pci_dev->irq, priv);
5320out_disable_msi:
5321 pci_disable_msi(priv->pci_dev);
Zhu Yie655b9f2008-01-24 02:19:38 -08005322 pci_disable_device(priv->pci_dev);
5323 priv->is_open = 0;
5324 IWL_DEBUG_MAC80211("leave - failed\n");
Zhu Yi5a66926a2008-01-14 17:46:18 -08005325 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07005326}
5327
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005328static void iwl4965_mac_stop(struct ieee80211_hw *hw)
Zhu Yib481de92007-09-25 17:54:57 -07005329{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005330 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07005331
5332 IWL_DEBUG_MAC80211("enter\n");
Mohamed Abbas948c1712007-10-25 17:15:45 +08005333
Zhu Yie655b9f2008-01-24 02:19:38 -08005334 if (!priv->is_open) {
5335 IWL_DEBUG_MAC80211("leave - skip\n");
5336 return;
5337 }
5338
Zhu Yib481de92007-09-25 17:54:57 -07005339 priv->is_open = 0;
Zhu Yi5a66926a2008-01-14 17:46:18 -08005340
Tomas Winklerfee12472008-04-03 16:05:21 -07005341 if (iwl_is_ready_rf(priv)) {
Zhu Yie655b9f2008-01-24 02:19:38 -08005342 /* stop mac, cancel any scan request and clear
5343 * RXON_FILTER_ASSOC_MSK BIT
5344 */
Zhu Yi5a66926a2008-01-14 17:46:18 -08005345 mutex_lock(&priv->mutex);
5346 iwl4965_scan_cancel_timeout(priv, 100);
5347 cancel_delayed_work(&priv->post_associate);
Mohamed Abbasfde35712007-11-29 11:10:15 +08005348 mutex_unlock(&priv->mutex);
Mohamed Abbasfde35712007-11-29 11:10:15 +08005349 }
5350
Zhu Yi5a66926a2008-01-14 17:46:18 -08005351 iwl4965_down(priv);
5352
5353 flush_workqueue(priv->workqueue);
5354 free_irq(priv->pci_dev->irq, priv);
5355 pci_disable_msi(priv->pci_dev);
5356 pci_save_state(priv->pci_dev);
5357 pci_disable_device(priv->pci_dev);
Mohamed Abbas948c1712007-10-25 17:15:45 +08005358
Zhu Yib481de92007-09-25 17:54:57 -07005359 IWL_DEBUG_MAC80211("leave\n");
Zhu Yib481de92007-09-25 17:54:57 -07005360}
5361
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005362static int iwl4965_mac_tx(struct ieee80211_hw *hw, struct sk_buff *skb,
Zhu Yib481de92007-09-25 17:54:57 -07005363 struct ieee80211_tx_control *ctl)
5364{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005365 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07005366
5367 IWL_DEBUG_MAC80211("enter\n");
5368
5369 if (priv->iw_mode == IEEE80211_IF_TYPE_MNTR) {
5370 IWL_DEBUG_MAC80211("leave - monitor\n");
5371 return -1;
5372 }
5373
5374 IWL_DEBUG_TX("dev->xmit(%d bytes) at rate 0x%02x\n", skb->len,
Johannes Berg8318d782008-01-24 19:38:38 +01005375 ctl->tx_rate->bitrate);
Zhu Yib481de92007-09-25 17:54:57 -07005376
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005377 if (iwl4965_tx_skb(priv, skb, ctl))
Zhu Yib481de92007-09-25 17:54:57 -07005378 dev_kfree_skb_any(skb);
5379
5380 IWL_DEBUG_MAC80211("leave\n");
5381 return 0;
5382}
5383
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005384static int iwl4965_mac_add_interface(struct ieee80211_hw *hw,
Zhu Yib481de92007-09-25 17:54:57 -07005385 struct ieee80211_if_init_conf *conf)
5386{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005387 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07005388 unsigned long flags;
Joe Perches0795af52007-10-03 17:59:30 -07005389 DECLARE_MAC_BUF(mac);
Zhu Yib481de92007-09-25 17:54:57 -07005390
Johannes Berg32bfd352007-12-19 01:31:26 +01005391 IWL_DEBUG_MAC80211("enter: type %d\n", conf->type);
Zhu Yib481de92007-09-25 17:54:57 -07005392
Johannes Berg32bfd352007-12-19 01:31:26 +01005393 if (priv->vif) {
5394 IWL_DEBUG_MAC80211("leave - vif != NULL\n");
Reinette Chatre75849d22008-01-23 10:15:17 -08005395 return -EOPNOTSUPP;
Zhu Yib481de92007-09-25 17:54:57 -07005396 }
5397
5398 spin_lock_irqsave(&priv->lock, flags);
Johannes Berg32bfd352007-12-19 01:31:26 +01005399 priv->vif = conf->vif;
Zhu Yib481de92007-09-25 17:54:57 -07005400
5401 spin_unlock_irqrestore(&priv->lock, flags);
5402
5403 mutex_lock(&priv->mutex);
Tomas Winkler864792e2007-11-27 21:00:52 +02005404
5405 if (conf->mac_addr) {
5406 IWL_DEBUG_MAC80211("Set %s\n", print_mac(mac, conf->mac_addr));
5407 memcpy(priv->mac_addr, conf->mac_addr, ETH_ALEN);
5408 }
Zhu Yib481de92007-09-25 17:54:57 -07005409
Tomas Winklerfee12472008-04-03 16:05:21 -07005410 if (iwl_is_ready(priv))
Zhu Yi5a66926a2008-01-14 17:46:18 -08005411 iwl4965_set_mode(priv, conf->type);
5412
Zhu Yib481de92007-09-25 17:54:57 -07005413 mutex_unlock(&priv->mutex);
5414
Zhu Yi5a66926a2008-01-14 17:46:18 -08005415 IWL_DEBUG_MAC80211("leave\n");
Zhu Yib481de92007-09-25 17:54:57 -07005416 return 0;
5417}
5418
5419/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005420 * iwl4965_mac_config - mac80211 config callback
Zhu Yib481de92007-09-25 17:54:57 -07005421 *
5422 * We ignore conf->flags & IEEE80211_CONF_SHORT_SLOT_TIME since it seems to
5423 * be set inappropriately and the driver currently sets the hardware up to
5424 * use it whenever needed.
5425 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005426static int iwl4965_mac_config(struct ieee80211_hw *hw, struct ieee80211_conf *conf)
Zhu Yib481de92007-09-25 17:54:57 -07005427{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005428 struct iwl_priv *priv = hw->priv;
Assaf Kraussbf85ea42008-03-14 10:38:49 -07005429 const struct iwl_channel_info *ch_info;
Zhu Yib481de92007-09-25 17:54:57 -07005430 unsigned long flags;
Zhu Yi76bb77e2007-11-22 10:53:22 +08005431 int ret = 0;
Zhu Yib481de92007-09-25 17:54:57 -07005432
5433 mutex_lock(&priv->mutex);
Johannes Berg8318d782008-01-24 19:38:38 +01005434 IWL_DEBUG_MAC80211("enter to channel %d\n", conf->channel->hw_value);
Zhu Yib481de92007-09-25 17:54:57 -07005435
Zhu Yi12342c42007-12-20 11:27:32 +08005436 priv->add_radiotap = !!(conf->flags & IEEE80211_CONF_RADIOTAP);
5437
Tomas Winklerfee12472008-04-03 16:05:21 -07005438 if (!iwl_is_ready(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07005439 IWL_DEBUG_MAC80211("leave - not ready\n");
Zhu Yi76bb77e2007-11-22 10:53:22 +08005440 ret = -EIO;
5441 goto out;
Zhu Yib481de92007-09-25 17:54:57 -07005442 }
5443
Assaf Krauss1ea87392008-03-18 14:57:50 -07005444 if (unlikely(!priv->cfg->mod_params->disable_hw_scan &&
Zhu Yib481de92007-09-25 17:54:57 -07005445 test_bit(STATUS_SCANNING, &priv->status))) {
Zhu Yia0646472007-12-20 14:10:01 +08005446 IWL_DEBUG_MAC80211("leave - scanning\n");
5447 set_bit(STATUS_CONF_PENDING, &priv->status);
Zhu Yib481de92007-09-25 17:54:57 -07005448 mutex_unlock(&priv->mutex);
Zhu Yia0646472007-12-20 14:10:01 +08005449 return 0;
Zhu Yib481de92007-09-25 17:54:57 -07005450 }
5451
5452 spin_lock_irqsave(&priv->lock, flags);
5453
Assaf Krauss8622e702008-03-21 13:53:43 -07005454 ch_info = iwl_get_channel_info(priv, conf->channel->band,
Johannes Berg8318d782008-01-24 19:38:38 +01005455 ieee80211_frequency_to_channel(conf->channel->center_freq));
Zhu Yib481de92007-09-25 17:54:57 -07005456 if (!is_channel_valid(ch_info)) {
Zhu Yib481de92007-09-25 17:54:57 -07005457 IWL_DEBUG_MAC80211("leave - invalid channel\n");
5458 spin_unlock_irqrestore(&priv->lock, flags);
Zhu Yi76bb77e2007-11-22 10:53:22 +08005459 ret = -EINVAL;
5460 goto out;
Zhu Yib481de92007-09-25 17:54:57 -07005461 }
5462
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08005463#ifdef CONFIG_IWL4965_HT
Tomas Winkler78330fd2008-02-06 02:37:18 +02005464 /* if we are switching from ht to 2.4 clear flags
Zhu Yib481de92007-09-25 17:54:57 -07005465 * from any ht related info since 2.4 does not
5466 * support ht */
Tomas Winkler78330fd2008-02-06 02:37:18 +02005467 if ((le16_to_cpu(priv->staging_rxon.channel) != conf->channel->hw_value)
Zhu Yib481de92007-09-25 17:54:57 -07005468#ifdef IEEE80211_CONF_CHANNEL_SWITCH
5469 && !(conf->flags & IEEE80211_CONF_CHANNEL_SWITCH)
5470#endif
5471 )
5472 priv->staging_rxon.flags = 0;
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08005473#endif /* CONFIG_IWL4965_HT */
Zhu Yib481de92007-09-25 17:54:57 -07005474
Ron Rindjunskyc7de35c2008-04-23 17:15:05 -07005475 iwl_set_rxon_channel(priv, conf->channel->band,
Johannes Berg8318d782008-01-24 19:38:38 +01005476 ieee80211_frequency_to_channel(conf->channel->center_freq));
Zhu Yib481de92007-09-25 17:54:57 -07005477
Johannes Berg8318d782008-01-24 19:38:38 +01005478 iwl4965_set_flags_for_phymode(priv, conf->channel->band);
Zhu Yib481de92007-09-25 17:54:57 -07005479
5480 /* The list of supported rates and rate mask can be different
Johannes Berg8318d782008-01-24 19:38:38 +01005481 * for each band; since the band may have changed, reset
Zhu Yib481de92007-09-25 17:54:57 -07005482 * the rate mask to what mac80211 lists */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005483 iwl4965_set_rate(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005484
5485 spin_unlock_irqrestore(&priv->lock, flags);
5486
5487#ifdef IEEE80211_CONF_CHANNEL_SWITCH
5488 if (conf->flags & IEEE80211_CONF_CHANNEL_SWITCH) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005489 iwl4965_hw_channel_switch(priv, conf->channel);
Zhu Yi76bb77e2007-11-22 10:53:22 +08005490 goto out;
Zhu Yib481de92007-09-25 17:54:57 -07005491 }
5492#endif
5493
Mohamed Abbasad97edd2008-03-28 16:21:06 -07005494 if (priv->cfg->ops->lib->radio_kill_sw)
5495 priv->cfg->ops->lib->radio_kill_sw(priv, !conf->radio_enabled);
Zhu Yib481de92007-09-25 17:54:57 -07005496
5497 if (!conf->radio_enabled) {
5498 IWL_DEBUG_MAC80211("leave - radio disabled\n");
Zhu Yi76bb77e2007-11-22 10:53:22 +08005499 goto out;
Zhu Yib481de92007-09-25 17:54:57 -07005500 }
5501
Tomas Winklerfee12472008-04-03 16:05:21 -07005502 if (iwl_is_rfkill(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07005503 IWL_DEBUG_MAC80211("leave - RF kill\n");
Zhu Yi76bb77e2007-11-22 10:53:22 +08005504 ret = -EIO;
5505 goto out;
Zhu Yib481de92007-09-25 17:54:57 -07005506 }
5507
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005508 iwl4965_set_rate(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005509
5510 if (memcmp(&priv->active_rxon,
5511 &priv->staging_rxon, sizeof(priv->staging_rxon)))
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005512 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005513 else
5514 IWL_DEBUG_INFO("No re-sending same RXON configuration.\n");
5515
5516 IWL_DEBUG_MAC80211("leave\n");
5517
Zhu Yia0646472007-12-20 14:10:01 +08005518out:
5519 clear_bit(STATUS_CONF_PENDING, &priv->status);
Zhu Yi5a66926a2008-01-14 17:46:18 -08005520 mutex_unlock(&priv->mutex);
Zhu Yi76bb77e2007-11-22 10:53:22 +08005521 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07005522}
5523
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005524static void iwl4965_config_ap(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005525{
Tomas Winkler857485c2008-03-21 13:53:44 -07005526 int ret = 0;
Zhu Yib481de92007-09-25 17:54:57 -07005527
Maarten Lankhorstd986bcd2008-01-23 10:15:16 -08005528 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
Zhu Yib481de92007-09-25 17:54:57 -07005529 return;
5530
5531 /* The following should be done only at AP bring up */
5532 if ((priv->active_rxon.filter_flags & RXON_FILTER_ASSOC_MSK) == 0) {
5533
5534 /* RXON - unassoc (to set timing command) */
5535 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005536 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005537
5538 /* RXON Timing */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005539 memset(&priv->rxon_timing, 0, sizeof(struct iwl4965_rxon_time_cmd));
5540 iwl4965_setup_rxon_timing(priv);
Tomas Winkler857485c2008-03-21 13:53:44 -07005541 ret = iwl_send_cmd_pdu(priv, REPLY_RXON_TIMING,
Zhu Yib481de92007-09-25 17:54:57 -07005542 sizeof(priv->rxon_timing), &priv->rxon_timing);
Tomas Winkler857485c2008-03-21 13:53:44 -07005543 if (ret)
Zhu Yib481de92007-09-25 17:54:57 -07005544 IWL_WARNING("REPLY_RXON_TIMING failed - "
5545 "Attempting to continue.\n");
5546
Ron Rindjunskyc7de35c2008-04-23 17:15:05 -07005547 iwl_set_rxon_chain(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005548
5549 /* FIXME: what should be the assoc_id for AP? */
5550 priv->staging_rxon.assoc_id = cpu_to_le16(priv->assoc_id);
5551 if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_PREAMBLE)
5552 priv->staging_rxon.flags |=
5553 RXON_FLG_SHORT_PREAMBLE_MSK;
5554 else
5555 priv->staging_rxon.flags &=
5556 ~RXON_FLG_SHORT_PREAMBLE_MSK;
5557
5558 if (priv->staging_rxon.flags & RXON_FLG_BAND_24G_MSK) {
5559 if (priv->assoc_capability &
5560 WLAN_CAPABILITY_SHORT_SLOT_TIME)
5561 priv->staging_rxon.flags |=
5562 RXON_FLG_SHORT_SLOT_MSK;
5563 else
5564 priv->staging_rxon.flags &=
5565 ~RXON_FLG_SHORT_SLOT_MSK;
5566
5567 if (priv->iw_mode == IEEE80211_IF_TYPE_IBSS)
5568 priv->staging_rxon.flags &=
5569 ~RXON_FLG_SHORT_SLOT_MSK;
5570 }
5571 /* restore RXON assoc */
5572 priv->staging_rxon.filter_flags |= RXON_FILTER_ASSOC_MSK;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005573 iwl4965_commit_rxon(priv);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005574 iwl4965_activate_qos(priv, 1);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005575 iwl4965_rxon_add_station(priv, iwl4965_broadcast_addr, 0);
Zhu Yie1493de2007-09-27 11:27:32 +08005576 }
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005577 iwl4965_send_beacon_cmd(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005578
5579 /* FIXME - we need to add code here to detect a totally new
5580 * configuration, reset the AP, unassoc, rxon timing, assoc,
5581 * clear sta table, add BCAST sta... */
5582}
5583
Johannes Berg32bfd352007-12-19 01:31:26 +01005584static int iwl4965_mac_config_interface(struct ieee80211_hw *hw,
5585 struct ieee80211_vif *vif,
Zhu Yib481de92007-09-25 17:54:57 -07005586 struct ieee80211_if_conf *conf)
5587{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005588 struct iwl_priv *priv = hw->priv;
Joe Perches0795af52007-10-03 17:59:30 -07005589 DECLARE_MAC_BUF(mac);
Zhu Yib481de92007-09-25 17:54:57 -07005590 unsigned long flags;
5591 int rc;
5592
5593 if (conf == NULL)
5594 return -EIO;
5595
Emmanuel Grumbachb716bb92008-03-04 18:09:32 -08005596 if (priv->vif != vif) {
5597 IWL_DEBUG_MAC80211("leave - priv->vif != vif\n");
Emmanuel Grumbachb716bb92008-03-04 18:09:32 -08005598 return 0;
5599 }
5600
Zhu Yib481de92007-09-25 17:54:57 -07005601 if ((priv->iw_mode == IEEE80211_IF_TYPE_AP) &&
5602 (!conf->beacon || !conf->ssid_len)) {
5603 IWL_DEBUG_MAC80211
5604 ("Leaving in AP mode because HostAPD is not ready.\n");
5605 return 0;
5606 }
5607
Tomas Winklerfee12472008-04-03 16:05:21 -07005608 if (!iwl_is_alive(priv))
Zhu Yi5a66926a2008-01-14 17:46:18 -08005609 return -EAGAIN;
5610
Zhu Yib481de92007-09-25 17:54:57 -07005611 mutex_lock(&priv->mutex);
5612
Zhu Yib481de92007-09-25 17:54:57 -07005613 if (conf->bssid)
Joe Perches0795af52007-10-03 17:59:30 -07005614 IWL_DEBUG_MAC80211("bssid: %s\n",
5615 print_mac(mac, conf->bssid));
Zhu Yib481de92007-09-25 17:54:57 -07005616
Johannes Berg4150c572007-09-17 01:29:23 -04005617/*
5618 * very dubious code was here; the probe filtering flag is never set:
5619 *
Zhu Yib481de92007-09-25 17:54:57 -07005620 if (unlikely(test_bit(STATUS_SCANNING, &priv->status)) &&
5621 !(priv->hw->flags & IEEE80211_HW_NO_PROBE_FILTERING)) {
Johannes Berg4150c572007-09-17 01:29:23 -04005622 */
Zhu Yib481de92007-09-25 17:54:57 -07005623
5624 if (priv->iw_mode == IEEE80211_IF_TYPE_AP) {
5625 if (!conf->bssid) {
5626 conf->bssid = priv->mac_addr;
5627 memcpy(priv->bssid, priv->mac_addr, ETH_ALEN);
Joe Perches0795af52007-10-03 17:59:30 -07005628 IWL_DEBUG_MAC80211("bssid was set to: %s\n",
5629 print_mac(mac, conf->bssid));
Zhu Yib481de92007-09-25 17:54:57 -07005630 }
5631 if (priv->ibss_beacon)
5632 dev_kfree_skb(priv->ibss_beacon);
5633
5634 priv->ibss_beacon = conf->beacon;
5635 }
5636
Tomas Winklerfee12472008-04-03 16:05:21 -07005637 if (iwl_is_rfkill(priv))
Mohamed Abbasfde35712007-11-29 11:10:15 +08005638 goto done;
5639
Zhu Yib481de92007-09-25 17:54:57 -07005640 if (conf->bssid && !is_zero_ether_addr(conf->bssid) &&
5641 !is_multicast_ether_addr(conf->bssid)) {
5642 /* If there is currently a HW scan going on in the background
5643 * then we need to cancel it else the RXON below will fail. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005644 if (iwl4965_scan_cancel_timeout(priv, 100)) {
Zhu Yib481de92007-09-25 17:54:57 -07005645 IWL_WARNING("Aborted scan still in progress "
5646 "after 100ms\n");
5647 IWL_DEBUG_MAC80211("leaving - scan abort failed.\n");
5648 mutex_unlock(&priv->mutex);
5649 return -EAGAIN;
5650 }
5651 memcpy(priv->staging_rxon.bssid_addr, conf->bssid, ETH_ALEN);
5652
5653 /* TODO: Audit driver for usage of these members and see
5654 * if mac80211 deprecates them (priv->bssid looks like it
5655 * shouldn't be there, but I haven't scanned the IBSS code
5656 * to verify) - jpk */
5657 memcpy(priv->bssid, conf->bssid, ETH_ALEN);
5658
5659 if (priv->iw_mode == IEEE80211_IF_TYPE_AP)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005660 iwl4965_config_ap(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005661 else {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005662 rc = iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005663 if ((priv->iw_mode == IEEE80211_IF_TYPE_STA) && rc)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005664 iwl4965_rxon_add_station(
Zhu Yib481de92007-09-25 17:54:57 -07005665 priv, priv->active_rxon.bssid_addr, 1);
5666 }
5667
5668 } else {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005669 iwl4965_scan_cancel_timeout(priv, 100);
Zhu Yib481de92007-09-25 17:54:57 -07005670 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005671 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005672 }
5673
Mohamed Abbasfde35712007-11-29 11:10:15 +08005674 done:
Zhu Yib481de92007-09-25 17:54:57 -07005675 spin_lock_irqsave(&priv->lock, flags);
5676 if (!conf->ssid_len)
5677 memset(priv->essid, 0, IW_ESSID_MAX_SIZE);
5678 else
5679 memcpy(priv->essid, conf->ssid, conf->ssid_len);
5680
5681 priv->essid_len = conf->ssid_len;
5682 spin_unlock_irqrestore(&priv->lock, flags);
5683
5684 IWL_DEBUG_MAC80211("leave\n");
5685 mutex_unlock(&priv->mutex);
5686
5687 return 0;
5688}
5689
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005690static void iwl4965_configure_filter(struct ieee80211_hw *hw,
Johannes Berg4150c572007-09-17 01:29:23 -04005691 unsigned int changed_flags,
5692 unsigned int *total_flags,
5693 int mc_count, struct dev_addr_list *mc_list)
5694{
5695 /*
5696 * XXX: dummy
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005697 * see also iwl4965_connection_init_rx_config
Johannes Berg4150c572007-09-17 01:29:23 -04005698 */
5699 *total_flags = 0;
5700}
5701
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005702static void iwl4965_mac_remove_interface(struct ieee80211_hw *hw,
Zhu Yib481de92007-09-25 17:54:57 -07005703 struct ieee80211_if_init_conf *conf)
5704{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005705 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07005706
5707 IWL_DEBUG_MAC80211("enter\n");
5708
5709 mutex_lock(&priv->mutex);
Mohamed Abbas948c1712007-10-25 17:15:45 +08005710
Tomas Winklerfee12472008-04-03 16:05:21 -07005711 if (iwl_is_ready_rf(priv)) {
Mohamed Abbasfde35712007-11-29 11:10:15 +08005712 iwl4965_scan_cancel_timeout(priv, 100);
5713 cancel_delayed_work(&priv->post_associate);
5714 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
5715 iwl4965_commit_rxon(priv);
5716 }
Johannes Berg32bfd352007-12-19 01:31:26 +01005717 if (priv->vif == conf->vif) {
5718 priv->vif = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07005719 memset(priv->bssid, 0, ETH_ALEN);
5720 memset(priv->essid, 0, IW_ESSID_MAX_SIZE);
5721 priv->essid_len = 0;
5722 }
5723 mutex_unlock(&priv->mutex);
5724
5725 IWL_DEBUG_MAC80211("leave\n");
5726
5727}
Johannes Berg471b3ef2007-12-28 14:32:58 +01005728
Tomas Winkler3109ece2008-03-28 16:33:35 -07005729#define IWL_DELAY_NEXT_SCAN_AFTER_ASSOC (HZ*6)
Johannes Berg471b3ef2007-12-28 14:32:58 +01005730static void iwl4965_bss_info_changed(struct ieee80211_hw *hw,
5731 struct ieee80211_vif *vif,
5732 struct ieee80211_bss_conf *bss_conf,
5733 u32 changes)
Tomas Winkler220173b2007-10-16 00:50:25 +02005734{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005735 struct iwl_priv *priv = hw->priv;
Tomas Winkler220173b2007-10-16 00:50:25 +02005736
Tomas Winkler3109ece2008-03-28 16:33:35 -07005737 IWL_DEBUG_MAC80211("changes = 0x%X\n", changes);
5738
Johannes Berg471b3ef2007-12-28 14:32:58 +01005739 if (changes & BSS_CHANGED_ERP_PREAMBLE) {
Tomas Winkler3109ece2008-03-28 16:33:35 -07005740 IWL_DEBUG_MAC80211("ERP_PREAMBLE %d\n",
5741 bss_conf->use_short_preamble);
Johannes Berg471b3ef2007-12-28 14:32:58 +01005742 if (bss_conf->use_short_preamble)
Tomas Winkler220173b2007-10-16 00:50:25 +02005743 priv->staging_rxon.flags |= RXON_FLG_SHORT_PREAMBLE_MSK;
5744 else
5745 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_PREAMBLE_MSK;
5746 }
5747
Johannes Berg471b3ef2007-12-28 14:32:58 +01005748 if (changes & BSS_CHANGED_ERP_CTS_PROT) {
Tomas Winkler3109ece2008-03-28 16:33:35 -07005749 IWL_DEBUG_MAC80211("ERP_CTS %d\n", bss_conf->use_cts_prot);
Johannes Berg8318d782008-01-24 19:38:38 +01005750 if (bss_conf->use_cts_prot && (priv->band != IEEE80211_BAND_5GHZ))
Tomas Winkler220173b2007-10-16 00:50:25 +02005751 priv->staging_rxon.flags |= RXON_FLG_TGG_PROTECT_MSK;
5752 else
5753 priv->staging_rxon.flags &= ~RXON_FLG_TGG_PROTECT_MSK;
5754 }
5755
Tomas Winkler98952d52008-03-28 16:33:33 -07005756 if (changes & BSS_CHANGED_HT) {
Tomas Winkler3109ece2008-03-28 16:33:35 -07005757 IWL_DEBUG_MAC80211("HT %d\n", bss_conf->assoc_ht);
Tomas Winkler98952d52008-03-28 16:33:33 -07005758 iwl4965_ht_conf(priv, bss_conf);
Ron Rindjunskyc7de35c2008-04-23 17:15:05 -07005759 iwl_set_rxon_chain(priv);
Tomas Winkler98952d52008-03-28 16:33:33 -07005760 }
5761
Johannes Berg471b3ef2007-12-28 14:32:58 +01005762 if (changes & BSS_CHANGED_ASSOC) {
Tomas Winkler3109ece2008-03-28 16:33:35 -07005763 IWL_DEBUG_MAC80211("ASSOC %d\n", bss_conf->assoc);
Reinette Chatre508e32e2008-04-14 21:16:13 -07005764 /* This should never happen as this function should
5765 * never be called from interrupt context. */
5766 if (WARN_ON_ONCE(in_interrupt()))
5767 return;
Tomas Winkler3109ece2008-03-28 16:33:35 -07005768 if (bss_conf->assoc) {
5769 priv->assoc_id = bss_conf->aid;
5770 priv->beacon_int = bss_conf->beacon_int;
5771 priv->timestamp = bss_conf->timestamp;
5772 priv->assoc_capability = bss_conf->assoc_capability;
5773 priv->next_scan_jiffies = jiffies +
5774 IWL_DELAY_NEXT_SCAN_AFTER_ASSOC;
Reinette Chatre508e32e2008-04-14 21:16:13 -07005775 mutex_lock(&priv->mutex);
5776 iwl4965_post_associate(priv);
5777 mutex_unlock(&priv->mutex);
Tomas Winkler3109ece2008-03-28 16:33:35 -07005778 } else {
5779 priv->assoc_id = 0;
5780 IWL_DEBUG_MAC80211("DISASSOC %d\n", bss_conf->assoc);
5781 }
5782 } else if (changes && iwl_is_associated(priv) && priv->assoc_id) {
5783 IWL_DEBUG_MAC80211("Associated Changes %d\n", changes);
Tomas Winkler7e8c5192008-04-15 16:01:43 -07005784 iwl_send_rxon_assoc(priv);
Johannes Berg471b3ef2007-12-28 14:32:58 +01005785 }
5786
Tomas Winkler220173b2007-10-16 00:50:25 +02005787}
Zhu Yib481de92007-09-25 17:54:57 -07005788
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005789static int iwl4965_mac_hw_scan(struct ieee80211_hw *hw, u8 *ssid, size_t len)
Zhu Yib481de92007-09-25 17:54:57 -07005790{
5791 int rc = 0;
5792 unsigned long flags;
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005793 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07005794
5795 IWL_DEBUG_MAC80211("enter\n");
5796
mabbas052c4b92007-10-25 17:15:43 +08005797 mutex_lock(&priv->mutex);
Zhu Yib481de92007-09-25 17:54:57 -07005798 spin_lock_irqsave(&priv->lock, flags);
5799
Tomas Winklerfee12472008-04-03 16:05:21 -07005800 if (!iwl_is_ready_rf(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07005801 rc = -EIO;
5802 IWL_DEBUG_MAC80211("leave - not ready or exit pending\n");
5803 goto out_unlock;
5804 }
5805
5806 if (priv->iw_mode == IEEE80211_IF_TYPE_AP) { /* APs don't scan */
5807 rc = -EIO;
5808 IWL_ERROR("ERROR: APs don't scan\n");
5809 goto out_unlock;
5810 }
5811
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08005812 /* we don't schedule scan within next_scan_jiffies period */
5813 if (priv->next_scan_jiffies &&
5814 time_after(priv->next_scan_jiffies, jiffies)) {
5815 rc = -EAGAIN;
5816 goto out_unlock;
5817 }
Zhu Yib481de92007-09-25 17:54:57 -07005818 /* if we just finished scan ask for delay */
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08005819 if (priv->last_scan_jiffies && time_after(priv->last_scan_jiffies +
5820 IWL_DELAY_NEXT_SCAN, jiffies)) {
Zhu Yib481de92007-09-25 17:54:57 -07005821 rc = -EAGAIN;
5822 goto out_unlock;
5823 }
5824 if (len) {
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08005825 IWL_DEBUG_SCAN("direct scan for %s [%d]\n ",
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005826 iwl4965_escape_essid(ssid, len), (int)len);
Zhu Yib481de92007-09-25 17:54:57 -07005827
5828 priv->one_direct_scan = 1;
5829 priv->direct_ssid_len = (u8)
5830 min((u8) len, (u8) IW_ESSID_MAX_SIZE);
5831 memcpy(priv->direct_ssid, ssid, priv->direct_ssid_len);
Mohamed Abbas948c1712007-10-25 17:15:45 +08005832 } else
5833 priv->one_direct_scan = 0;
Zhu Yib481de92007-09-25 17:54:57 -07005834
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005835 rc = iwl4965_scan_initiate(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005836
5837 IWL_DEBUG_MAC80211("leave\n");
5838
5839out_unlock:
5840 spin_unlock_irqrestore(&priv->lock, flags);
mabbas052c4b92007-10-25 17:15:43 +08005841 mutex_unlock(&priv->mutex);
Zhu Yib481de92007-09-25 17:54:57 -07005842
5843 return rc;
5844}
5845
Emmanuel Grumbachab885f82008-03-20 15:06:43 +02005846static void iwl4965_mac_update_tkip_key(struct ieee80211_hw *hw,
5847 struct ieee80211_key_conf *keyconf, const u8 *addr,
5848 u32 iv32, u16 *phase1key)
5849{
5850 struct iwl_priv *priv = hw->priv;
5851 u8 sta_id = IWL_INVALID_STATION;
5852 unsigned long flags;
5853 __le16 key_flags = 0;
5854 int i;
5855 DECLARE_MAC_BUF(mac);
5856
5857 IWL_DEBUG_MAC80211("enter\n");
5858
Tomas Winkler947b13a2008-04-16 16:34:48 -07005859 sta_id = iwl_find_station(priv, addr);
Emmanuel Grumbachab885f82008-03-20 15:06:43 +02005860 if (sta_id == IWL_INVALID_STATION) {
5861 IWL_DEBUG_MAC80211("leave - %s not in station map.\n",
5862 print_mac(mac, addr));
5863 return;
5864 }
5865
5866 iwl4965_scan_cancel_timeout(priv, 100);
5867
5868 key_flags |= (STA_KEY_FLG_TKIP | STA_KEY_FLG_MAP_KEY_MSK);
5869 key_flags |= cpu_to_le16(keyconf->keyidx << STA_KEY_FLG_KEYID_POS);
5870 key_flags &= ~STA_KEY_FLG_INVALID;
5871
Tomas Winkler5425e492008-04-15 16:01:38 -07005872 if (sta_id == priv->hw_params.bcast_sta_id)
Emmanuel Grumbachab885f82008-03-20 15:06:43 +02005873 key_flags |= STA_KEY_MULTICAST_MSK;
5874
5875 spin_lock_irqsave(&priv->sta_lock, flags);
5876
Emmanuel Grumbachab885f82008-03-20 15:06:43 +02005877 priv->stations[sta_id].sta.key.key_flags = key_flags;
5878 priv->stations[sta_id].sta.key.tkip_rx_tsc_byte2 = (u8) iv32;
5879
5880 for (i = 0; i < 5; i++)
5881 priv->stations[sta_id].sta.key.tkip_rx_ttak[i] =
5882 cpu_to_le16(phase1key[i]);
5883
5884 priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
5885 priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
5886
Tomas Winkler133636d2008-05-05 10:22:34 +08005887 iwl_send_add_sta(priv, &priv->stations[sta_id].sta, CMD_ASYNC);
Emmanuel Grumbachab885f82008-03-20 15:06:43 +02005888
5889 spin_unlock_irqrestore(&priv->sta_lock, flags);
5890
5891 IWL_DEBUG_MAC80211("leave\n");
5892}
5893
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005894static int iwl4965_mac_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
Zhu Yib481de92007-09-25 17:54:57 -07005895 const u8 *local_addr, const u8 *addr,
5896 struct ieee80211_key_conf *key)
5897{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005898 struct iwl_priv *priv = hw->priv;
Joe Perches0795af52007-10-03 17:59:30 -07005899 DECLARE_MAC_BUF(mac);
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07005900 int ret = 0;
5901 u8 sta_id = IWL_INVALID_STATION;
Emmanuel Grumbach6974e362008-04-14 21:16:06 -07005902 u8 is_default_wep_key = 0;
Zhu Yib481de92007-09-25 17:54:57 -07005903
5904 IWL_DEBUG_MAC80211("enter\n");
5905
Ron Rindjunsky099b40b2008-04-21 15:41:53 -07005906 if (priv->hw_params.sw_crypto) {
Zhu Yib481de92007-09-25 17:54:57 -07005907 IWL_DEBUG_MAC80211("leave - hwcrypto disabled\n");
5908 return -EOPNOTSUPP;
5909 }
5910
5911 if (is_zero_ether_addr(addr))
5912 /* only support pairwise keys */
5913 return -EOPNOTSUPP;
5914
Tomas Winkler947b13a2008-04-16 16:34:48 -07005915 sta_id = iwl_find_station(priv, addr);
Emmanuel Grumbach6974e362008-04-14 21:16:06 -07005916 if (sta_id == IWL_INVALID_STATION) {
5917 IWL_DEBUG_MAC80211("leave - %s not in station map.\n",
5918 print_mac(mac, addr));
5919 return -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07005920
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07005921 }
Zhu Yib481de92007-09-25 17:54:57 -07005922
Emmanuel Grumbach6974e362008-04-14 21:16:06 -07005923 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005924 iwl4965_scan_cancel_timeout(priv, 100);
Emmanuel Grumbach6974e362008-04-14 21:16:06 -07005925 mutex_unlock(&priv->mutex);
5926
5927 /* If we are getting WEP group key and we didn't receive any key mapping
5928 * so far, we are in legacy wep mode (group key only), otherwise we are
5929 * in 1X mode.
5930 * In legacy wep mode, we use another host command to the uCode */
Tomas Winkler5425e492008-04-15 16:01:38 -07005931 if (key->alg == ALG_WEP && sta_id == priv->hw_params.bcast_sta_id &&
Emmanuel Grumbach6974e362008-04-14 21:16:06 -07005932 priv->iw_mode != IEEE80211_IF_TYPE_AP) {
5933 if (cmd == SET_KEY)
5934 is_default_wep_key = !priv->key_mapping_key;
5935 else
5936 is_default_wep_key = priv->default_wep_key;
5937 }
mabbas052c4b92007-10-25 17:15:43 +08005938
Zhu Yib481de92007-09-25 17:54:57 -07005939 switch (cmd) {
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07005940 case SET_KEY:
Emmanuel Grumbach6974e362008-04-14 21:16:06 -07005941 if (is_default_wep_key)
5942 ret = iwl_set_default_wep_key(priv, key);
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07005943 else
Emmanuel Grumbach74805132008-04-14 21:16:09 -07005944 ret = iwl_set_dynamic_key(priv, key, sta_id);
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07005945
5946 IWL_DEBUG_MAC80211("enable hwcrypto key\n");
Zhu Yib481de92007-09-25 17:54:57 -07005947 break;
5948 case DISABLE_KEY:
Emmanuel Grumbach6974e362008-04-14 21:16:06 -07005949 if (is_default_wep_key)
5950 ret = iwl_remove_default_wep_key(priv, key);
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07005951 else
Emmanuel Grumbach3ec47732008-04-17 16:03:36 -07005952 ret = iwl_remove_dynamic_key(priv, key, sta_id);
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07005953
5954 IWL_DEBUG_MAC80211("disable hwcrypto key\n");
Zhu Yib481de92007-09-25 17:54:57 -07005955 break;
5956 default:
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07005957 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07005958 }
5959
5960 IWL_DEBUG_MAC80211("leave\n");
Zhu Yib481de92007-09-25 17:54:57 -07005961
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07005962 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07005963}
5964
Johannes Berge100bb62008-04-30 18:51:21 +02005965static int iwl4965_mac_conf_tx(struct ieee80211_hw *hw, u16 queue,
Zhu Yib481de92007-09-25 17:54:57 -07005966 const struct ieee80211_tx_queue_params *params)
5967{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005968 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07005969 unsigned long flags;
5970 int q;
Zhu Yib481de92007-09-25 17:54:57 -07005971
5972 IWL_DEBUG_MAC80211("enter\n");
5973
Tomas Winklerfee12472008-04-03 16:05:21 -07005974 if (!iwl_is_ready_rf(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07005975 IWL_DEBUG_MAC80211("leave - RF not ready\n");
5976 return -EIO;
5977 }
5978
5979 if (queue >= AC_NUM) {
5980 IWL_DEBUG_MAC80211("leave - queue >= AC_NUM %d\n", queue);
5981 return 0;
5982 }
5983
Zhu Yib481de92007-09-25 17:54:57 -07005984 if (!priv->qos_data.qos_enable) {
5985 priv->qos_data.qos_active = 0;
5986 IWL_DEBUG_MAC80211("leave - qos not enabled\n");
5987 return 0;
5988 }
5989 q = AC_NUM - 1 - queue;
5990
5991 spin_lock_irqsave(&priv->lock, flags);
5992
5993 priv->qos_data.def_qos_parm.ac[q].cw_min = cpu_to_le16(params->cw_min);
5994 priv->qos_data.def_qos_parm.ac[q].cw_max = cpu_to_le16(params->cw_max);
5995 priv->qos_data.def_qos_parm.ac[q].aifsn = params->aifs;
5996 priv->qos_data.def_qos_parm.ac[q].edca_txop =
Johannes Berg3330d7b2008-02-10 16:49:38 +01005997 cpu_to_le16((params->txop * 32));
Zhu Yib481de92007-09-25 17:54:57 -07005998
5999 priv->qos_data.def_qos_parm.ac[q].reserved1 = 0;
6000 priv->qos_data.qos_active = 1;
6001
6002 spin_unlock_irqrestore(&priv->lock, flags);
6003
6004 mutex_lock(&priv->mutex);
6005 if (priv->iw_mode == IEEE80211_IF_TYPE_AP)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006006 iwl4965_activate_qos(priv, 1);
Tomas Winkler3109ece2008-03-28 16:33:35 -07006007 else if (priv->assoc_id && iwl_is_associated(priv))
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006008 iwl4965_activate_qos(priv, 0);
Zhu Yib481de92007-09-25 17:54:57 -07006009
6010 mutex_unlock(&priv->mutex);
6011
Zhu Yib481de92007-09-25 17:54:57 -07006012 IWL_DEBUG_MAC80211("leave\n");
6013 return 0;
6014}
6015
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006016static int iwl4965_mac_get_tx_stats(struct ieee80211_hw *hw,
Zhu Yib481de92007-09-25 17:54:57 -07006017 struct ieee80211_tx_queue_stats *stats)
6018{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006019 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07006020 int i, avail;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006021 struct iwl4965_tx_queue *txq;
6022 struct iwl4965_queue *q;
Zhu Yib481de92007-09-25 17:54:57 -07006023 unsigned long flags;
6024
6025 IWL_DEBUG_MAC80211("enter\n");
6026
Tomas Winklerfee12472008-04-03 16:05:21 -07006027 if (!iwl_is_ready_rf(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07006028 IWL_DEBUG_MAC80211("leave - RF not ready\n");
6029 return -EIO;
6030 }
6031
6032 spin_lock_irqsave(&priv->lock, flags);
6033
6034 for (i = 0; i < AC_NUM; i++) {
6035 txq = &priv->txq[i];
6036 q = &txq->q;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006037 avail = iwl4965_queue_space(q);
Zhu Yib481de92007-09-25 17:54:57 -07006038
Johannes Berg57ffc582008-04-29 17:18:59 +02006039 stats[i].len = q->n_window - avail;
6040 stats[i].limit = q->n_window - q->high_mark;
6041 stats[i].count = q->n_window;
Zhu Yib481de92007-09-25 17:54:57 -07006042
6043 }
6044 spin_unlock_irqrestore(&priv->lock, flags);
6045
6046 IWL_DEBUG_MAC80211("leave\n");
6047
6048 return 0;
6049}
6050
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006051static int iwl4965_mac_get_stats(struct ieee80211_hw *hw,
Zhu Yib481de92007-09-25 17:54:57 -07006052 struct ieee80211_low_level_stats *stats)
6053{
Ester Kummerbf403db2008-05-05 10:22:40 +08006054 struct iwl_priv *priv = hw->priv;
6055
6056 priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07006057 IWL_DEBUG_MAC80211("enter\n");
6058 IWL_DEBUG_MAC80211("leave\n");
6059
6060 return 0;
6061}
6062
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006063static u64 iwl4965_mac_get_tsf(struct ieee80211_hw *hw)
Zhu Yib481de92007-09-25 17:54:57 -07006064{
Ester Kummerbf403db2008-05-05 10:22:40 +08006065 struct iwl_priv *priv;
6066
6067 priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07006068 IWL_DEBUG_MAC80211("enter\n");
6069 IWL_DEBUG_MAC80211("leave\n");
6070
6071 return 0;
6072}
6073
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006074static void iwl4965_mac_reset_tsf(struct ieee80211_hw *hw)
Zhu Yib481de92007-09-25 17:54:57 -07006075{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006076 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07006077 unsigned long flags;
6078
6079 mutex_lock(&priv->mutex);
6080 IWL_DEBUG_MAC80211("enter\n");
6081
6082 priv->lq_mngr.lq_ready = 0;
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08006083#ifdef CONFIG_IWL4965_HT
Zhu Yib481de92007-09-25 17:54:57 -07006084 spin_lock_irqsave(&priv->lock, flags);
Ron Rindjunskyfd105e72007-11-26 16:14:39 +02006085 memset(&priv->current_ht_config, 0, sizeof(struct iwl_ht_info));
Zhu Yib481de92007-09-25 17:54:57 -07006086 spin_unlock_irqrestore(&priv->lock, flags);
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08006087#endif /* CONFIG_IWL4965_HT */
Zhu Yib481de92007-09-25 17:54:57 -07006088
Ron Rindjunskyc7de35c2008-04-23 17:15:05 -07006089 iwl_reset_qos(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006090
6091 cancel_delayed_work(&priv->post_associate);
6092
6093 spin_lock_irqsave(&priv->lock, flags);
6094 priv->assoc_id = 0;
6095 priv->assoc_capability = 0;
Zhu Yib481de92007-09-25 17:54:57 -07006096 priv->assoc_station_added = 0;
6097
6098 /* new association get rid of ibss beacon skb */
6099 if (priv->ibss_beacon)
6100 dev_kfree_skb(priv->ibss_beacon);
6101
6102 priv->ibss_beacon = NULL;
6103
6104 priv->beacon_int = priv->hw->conf.beacon_int;
Tomas Winkler3109ece2008-03-28 16:33:35 -07006105 priv->timestamp = 0;
Zhu Yib481de92007-09-25 17:54:57 -07006106 if ((priv->iw_mode == IEEE80211_IF_TYPE_STA))
6107 priv->beacon_int = 0;
6108
6109 spin_unlock_irqrestore(&priv->lock, flags);
6110
Tomas Winklerfee12472008-04-03 16:05:21 -07006111 if (!iwl_is_ready_rf(priv)) {
Mohamed Abbasfde35712007-11-29 11:10:15 +08006112 IWL_DEBUG_MAC80211("leave - not ready\n");
6113 mutex_unlock(&priv->mutex);
6114 return;
6115 }
6116
mabbas052c4b92007-10-25 17:15:43 +08006117 /* we are restarting association process
6118 * clear RXON_FILTER_ASSOC_MSK bit
6119 */
6120 if (priv->iw_mode != IEEE80211_IF_TYPE_AP) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006121 iwl4965_scan_cancel_timeout(priv, 100);
mabbas052c4b92007-10-25 17:15:43 +08006122 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006123 iwl4965_commit_rxon(priv);
mabbas052c4b92007-10-25 17:15:43 +08006124 }
6125
Mohamed Abbas5da4b552008-04-21 15:41:51 -07006126 iwl_power_update_mode(priv, 0);
6127
Zhu Yib481de92007-09-25 17:54:57 -07006128 /* Per mac80211.h: This is only used in IBSS mode... */
6129 if (priv->iw_mode != IEEE80211_IF_TYPE_IBSS) {
mabbas052c4b92007-10-25 17:15:43 +08006130
Zhu Yib481de92007-09-25 17:54:57 -07006131 IWL_DEBUG_MAC80211("leave - not in IBSS\n");
6132 mutex_unlock(&priv->mutex);
6133 return;
6134 }
6135
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006136 iwl4965_set_rate(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006137
6138 mutex_unlock(&priv->mutex);
6139
6140 IWL_DEBUG_MAC80211("leave\n");
Zhu Yib481de92007-09-25 17:54:57 -07006141}
6142
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006143static int iwl4965_mac_beacon_update(struct ieee80211_hw *hw, struct sk_buff *skb,
Zhu Yib481de92007-09-25 17:54:57 -07006144 struct ieee80211_tx_control *control)
6145{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006146 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07006147 unsigned long flags;
6148
6149 mutex_lock(&priv->mutex);
6150 IWL_DEBUG_MAC80211("enter\n");
6151
Tomas Winklerfee12472008-04-03 16:05:21 -07006152 if (!iwl_is_ready_rf(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07006153 IWL_DEBUG_MAC80211("leave - RF not ready\n");
6154 mutex_unlock(&priv->mutex);
6155 return -EIO;
6156 }
6157
6158 if (priv->iw_mode != IEEE80211_IF_TYPE_IBSS) {
6159 IWL_DEBUG_MAC80211("leave - not IBSS\n");
6160 mutex_unlock(&priv->mutex);
6161 return -EIO;
6162 }
6163
6164 spin_lock_irqsave(&priv->lock, flags);
6165
6166 if (priv->ibss_beacon)
6167 dev_kfree_skb(priv->ibss_beacon);
6168
6169 priv->ibss_beacon = skb;
6170
6171 priv->assoc_id = 0;
6172
6173 IWL_DEBUG_MAC80211("leave\n");
6174 spin_unlock_irqrestore(&priv->lock, flags);
6175
Ron Rindjunskyc7de35c2008-04-23 17:15:05 -07006176 iwl_reset_qos(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006177
6178 queue_work(priv->workqueue, &priv->post_associate.work);
6179
6180 mutex_unlock(&priv->mutex);
6181
6182 return 0;
6183}
6184
Zhu Yib481de92007-09-25 17:54:57 -07006185/*****************************************************************************
6186 *
6187 * sysfs attributes
6188 *
6189 *****************************************************************************/
6190
Tomas Winkler0a6857e2008-03-12 16:58:49 -07006191#ifdef CONFIG_IWLWIFI_DEBUG
Zhu Yib481de92007-09-25 17:54:57 -07006192
6193/*
6194 * The following adds a new attribute to the sysfs representation
6195 * of this device driver (i.e. a new file in /sys/bus/pci/drivers/iwl/)
6196 * used for controlling the debug level.
6197 *
6198 * See the level definitions in iwl for details.
6199 */
6200
6201static ssize_t show_debug_level(struct device_driver *d, char *buf)
6202{
Tomas Winkler0a6857e2008-03-12 16:58:49 -07006203 return sprintf(buf, "0x%08X\n", iwl_debug_level);
Zhu Yib481de92007-09-25 17:54:57 -07006204}
6205static ssize_t store_debug_level(struct device_driver *d,
6206 const char *buf, size_t count)
6207{
6208 char *p = (char *)buf;
6209 u32 val;
6210
6211 val = simple_strtoul(p, &p, 0);
6212 if (p == buf)
6213 printk(KERN_INFO DRV_NAME
6214 ": %s is not in hex or decimal form.\n", buf);
6215 else
Tomas Winkler0a6857e2008-03-12 16:58:49 -07006216 iwl_debug_level = val;
Zhu Yib481de92007-09-25 17:54:57 -07006217
6218 return strnlen(buf, count);
6219}
6220
6221static DRIVER_ATTR(debug_level, S_IWUSR | S_IRUGO,
6222 show_debug_level, store_debug_level);
6223
Tomas Winkler0a6857e2008-03-12 16:58:49 -07006224#endif /* CONFIG_IWLWIFI_DEBUG */
Zhu Yib481de92007-09-25 17:54:57 -07006225
Zhu Yib481de92007-09-25 17:54:57 -07006226
6227static ssize_t show_temperature(struct device *d,
6228 struct device_attribute *attr, char *buf)
6229{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006230 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07006231
Tomas Winklerfee12472008-04-03 16:05:21 -07006232 if (!iwl_is_alive(priv))
Zhu Yib481de92007-09-25 17:54:57 -07006233 return -EAGAIN;
6234
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006235 return sprintf(buf, "%d\n", iwl4965_hw_get_temperature(priv));
Zhu Yib481de92007-09-25 17:54:57 -07006236}
6237
6238static DEVICE_ATTR(temperature, S_IRUGO, show_temperature, NULL);
6239
6240static ssize_t show_rs_window(struct device *d,
6241 struct device_attribute *attr,
6242 char *buf)
6243{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006244 struct iwl_priv *priv = d->driver_data;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006245 return iwl4965_fill_rs_info(priv->hw, buf, IWL_AP_ID);
Zhu Yib481de92007-09-25 17:54:57 -07006246}
6247static DEVICE_ATTR(rs_window, S_IRUGO, show_rs_window, NULL);
6248
6249static ssize_t show_tx_power(struct device *d,
6250 struct device_attribute *attr, char *buf)
6251{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006252 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07006253 return sprintf(buf, "%d\n", priv->user_txpower_limit);
6254}
6255
6256static ssize_t store_tx_power(struct device *d,
6257 struct device_attribute *attr,
6258 const char *buf, size_t count)
6259{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006260 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07006261 char *p = (char *)buf;
6262 u32 val;
6263
6264 val = simple_strtoul(p, &p, 10);
6265 if (p == buf)
6266 printk(KERN_INFO DRV_NAME
6267 ": %s is not in decimal form.\n", buf);
6268 else
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006269 iwl4965_hw_reg_set_txpower(priv, val);
Zhu Yib481de92007-09-25 17:54:57 -07006270
6271 return count;
6272}
6273
6274static DEVICE_ATTR(tx_power, S_IWUSR | S_IRUGO, show_tx_power, store_tx_power);
6275
6276static ssize_t show_flags(struct device *d,
6277 struct device_attribute *attr, char *buf)
6278{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006279 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07006280
6281 return sprintf(buf, "0x%04X\n", priv->active_rxon.flags);
6282}
6283
6284static ssize_t store_flags(struct device *d,
6285 struct device_attribute *attr,
6286 const char *buf, size_t count)
6287{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006288 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07006289 u32 flags = simple_strtoul(buf, NULL, 0);
6290
6291 mutex_lock(&priv->mutex);
6292 if (le32_to_cpu(priv->staging_rxon.flags) != flags) {
6293 /* Cancel any currently running scans... */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006294 if (iwl4965_scan_cancel_timeout(priv, 100))
Zhu Yib481de92007-09-25 17:54:57 -07006295 IWL_WARNING("Could not cancel scan.\n");
6296 else {
6297 IWL_DEBUG_INFO("Committing rxon.flags = 0x%04X\n",
6298 flags);
6299 priv->staging_rxon.flags = cpu_to_le32(flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006300 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006301 }
6302 }
6303 mutex_unlock(&priv->mutex);
6304
6305 return count;
6306}
6307
6308static DEVICE_ATTR(flags, S_IWUSR | S_IRUGO, show_flags, store_flags);
6309
6310static ssize_t show_filter_flags(struct device *d,
6311 struct device_attribute *attr, char *buf)
6312{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006313 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07006314
6315 return sprintf(buf, "0x%04X\n",
6316 le32_to_cpu(priv->active_rxon.filter_flags));
6317}
6318
6319static ssize_t store_filter_flags(struct device *d,
6320 struct device_attribute *attr,
6321 const char *buf, size_t count)
6322{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006323 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07006324 u32 filter_flags = simple_strtoul(buf, NULL, 0);
6325
6326 mutex_lock(&priv->mutex);
6327 if (le32_to_cpu(priv->staging_rxon.filter_flags) != filter_flags) {
6328 /* Cancel any currently running scans... */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006329 if (iwl4965_scan_cancel_timeout(priv, 100))
Zhu Yib481de92007-09-25 17:54:57 -07006330 IWL_WARNING("Could not cancel scan.\n");
6331 else {
6332 IWL_DEBUG_INFO("Committing rxon.filter_flags = "
6333 "0x%04X\n", filter_flags);
6334 priv->staging_rxon.filter_flags =
6335 cpu_to_le32(filter_flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006336 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006337 }
6338 }
6339 mutex_unlock(&priv->mutex);
6340
6341 return count;
6342}
6343
6344static DEVICE_ATTR(filter_flags, S_IWUSR | S_IRUGO, show_filter_flags,
6345 store_filter_flags);
6346
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08006347#ifdef CONFIG_IWL4965_SPECTRUM_MEASUREMENT
Zhu Yib481de92007-09-25 17:54:57 -07006348
6349static ssize_t show_measurement(struct device *d,
6350 struct device_attribute *attr, char *buf)
6351{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006352 struct iwl_priv *priv = dev_get_drvdata(d);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006353 struct iwl4965_spectrum_notification measure_report;
Zhu Yib481de92007-09-25 17:54:57 -07006354 u32 size = sizeof(measure_report), len = 0, ofs = 0;
6355 u8 *data = (u8 *) & measure_report;
6356 unsigned long flags;
6357
6358 spin_lock_irqsave(&priv->lock, flags);
6359 if (!(priv->measurement_status & MEASUREMENT_READY)) {
6360 spin_unlock_irqrestore(&priv->lock, flags);
6361 return 0;
6362 }
6363 memcpy(&measure_report, &priv->measure_report, size);
6364 priv->measurement_status = 0;
6365 spin_unlock_irqrestore(&priv->lock, flags);
6366
6367 while (size && (PAGE_SIZE - len)) {
6368 hex_dump_to_buffer(data + ofs, size, 16, 1, buf + len,
6369 PAGE_SIZE - len, 1);
6370 len = strlen(buf);
6371 if (PAGE_SIZE - len)
6372 buf[len++] = '\n';
6373
6374 ofs += 16;
6375 size -= min(size, 16U);
6376 }
6377
6378 return len;
6379}
6380
6381static ssize_t store_measurement(struct device *d,
6382 struct device_attribute *attr,
6383 const char *buf, size_t count)
6384{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006385 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07006386 struct ieee80211_measurement_params params = {
6387 .channel = le16_to_cpu(priv->active_rxon.channel),
6388 .start_time = cpu_to_le64(priv->last_tsf),
6389 .duration = cpu_to_le16(1),
6390 };
6391 u8 type = IWL_MEASURE_BASIC;
6392 u8 buffer[32];
6393 u8 channel;
6394
6395 if (count) {
6396 char *p = buffer;
6397 strncpy(buffer, buf, min(sizeof(buffer), count));
6398 channel = simple_strtoul(p, NULL, 0);
6399 if (channel)
6400 params.channel = channel;
6401
6402 p = buffer;
6403 while (*p && *p != ' ')
6404 p++;
6405 if (*p)
6406 type = simple_strtoul(p + 1, NULL, 0);
6407 }
6408
6409 IWL_DEBUG_INFO("Invoking measurement of type %d on "
6410 "channel %d (for '%s')\n", type, params.channel, buf);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006411 iwl4965_get_measurement(priv, &params, type);
Zhu Yib481de92007-09-25 17:54:57 -07006412
6413 return count;
6414}
6415
6416static DEVICE_ATTR(measurement, S_IRUSR | S_IWUSR,
6417 show_measurement, store_measurement);
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08006418#endif /* CONFIG_IWL4965_SPECTRUM_MEASUREMENT */
Zhu Yib481de92007-09-25 17:54:57 -07006419
6420static ssize_t store_retry_rate(struct device *d,
6421 struct device_attribute *attr,
6422 const char *buf, size_t count)
6423{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006424 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07006425
6426 priv->retry_rate = simple_strtoul(buf, NULL, 0);
6427 if (priv->retry_rate <= 0)
6428 priv->retry_rate = 1;
6429
6430 return count;
6431}
6432
6433static ssize_t show_retry_rate(struct device *d,
6434 struct device_attribute *attr, char *buf)
6435{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006436 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07006437 return sprintf(buf, "%d", priv->retry_rate);
6438}
6439
6440static DEVICE_ATTR(retry_rate, S_IWUSR | S_IRUSR, show_retry_rate,
6441 store_retry_rate);
6442
6443static ssize_t store_power_level(struct device *d,
6444 struct device_attribute *attr,
6445 const char *buf, size_t count)
6446{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006447 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07006448 int rc;
6449 int mode;
6450
6451 mode = simple_strtoul(buf, NULL, 0);
6452 mutex_lock(&priv->mutex);
6453
Tomas Winklerfee12472008-04-03 16:05:21 -07006454 if (!iwl_is_ready(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07006455 rc = -EAGAIN;
6456 goto out;
6457 }
6458
Mohamed Abbas5da4b552008-04-21 15:41:51 -07006459 rc = iwl_power_set_user_mode(priv, mode);
6460 if (rc) {
6461 IWL_DEBUG_MAC80211("failed setting power mode.\n");
6462 goto out;
Zhu Yib481de92007-09-25 17:54:57 -07006463 }
Zhu Yib481de92007-09-25 17:54:57 -07006464 rc = count;
6465
6466 out:
6467 mutex_unlock(&priv->mutex);
6468 return rc;
6469}
6470
6471#define MAX_WX_STRING 80
6472
6473/* Values are in microsecond */
6474static const s32 timeout_duration[] = {
6475 350000,
6476 250000,
6477 75000,
6478 37000,
6479 25000,
6480};
6481static const s32 period_duration[] = {
6482 400000,
6483 700000,
6484 1000000,
6485 1000000,
6486 1000000
6487};
6488
6489static ssize_t show_power_level(struct device *d,
6490 struct device_attribute *attr, char *buf)
6491{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006492 struct iwl_priv *priv = dev_get_drvdata(d);
Mohamed Abbas5da4b552008-04-21 15:41:51 -07006493 int level = priv->power_data.power_mode;
Zhu Yib481de92007-09-25 17:54:57 -07006494 char *p = buf;
6495
6496 p += sprintf(p, "%d ", level);
6497 switch (level) {
6498 case IWL_POWER_MODE_CAM:
6499 case IWL_POWER_AC:
6500 p += sprintf(p, "(AC)");
6501 break;
6502 case IWL_POWER_BATTERY:
6503 p += sprintf(p, "(BATTERY)");
6504 break;
6505 default:
6506 p += sprintf(p,
6507 "(Timeout %dms, Period %dms)",
6508 timeout_duration[level - 1] / 1000,
6509 period_duration[level - 1] / 1000);
6510 }
Mohamed Abbas5da4b552008-04-21 15:41:51 -07006511/*
Zhu Yib481de92007-09-25 17:54:57 -07006512 if (!(priv->power_mode & IWL_POWER_ENABLED))
6513 p += sprintf(p, " OFF\n");
6514 else
6515 p += sprintf(p, " \n");
Mohamed Abbas5da4b552008-04-21 15:41:51 -07006516*/
6517 p += sprintf(p, " \n");
Zhu Yib481de92007-09-25 17:54:57 -07006518 return (p - buf + 1);
Zhu Yib481de92007-09-25 17:54:57 -07006519}
6520
6521static DEVICE_ATTR(power_level, S_IWUSR | S_IRUSR, show_power_level,
6522 store_power_level);
6523
6524static ssize_t show_channels(struct device *d,
6525 struct device_attribute *attr, char *buf)
6526{
Johannes Berg8318d782008-01-24 19:38:38 +01006527 /* all this shit doesn't belong into sysfs anyway */
6528 return 0;
Zhu Yib481de92007-09-25 17:54:57 -07006529}
6530
6531static DEVICE_ATTR(channels, S_IRUSR, show_channels, NULL);
6532
6533static ssize_t show_statistics(struct device *d,
6534 struct device_attribute *attr, char *buf)
6535{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006536 struct iwl_priv *priv = dev_get_drvdata(d);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006537 u32 size = sizeof(struct iwl4965_notif_statistics);
Zhu Yib481de92007-09-25 17:54:57 -07006538 u32 len = 0, ofs = 0;
6539 u8 *data = (u8 *) & priv->statistics;
6540 int rc = 0;
6541
Tomas Winklerfee12472008-04-03 16:05:21 -07006542 if (!iwl_is_alive(priv))
Zhu Yib481de92007-09-25 17:54:57 -07006543 return -EAGAIN;
6544
6545 mutex_lock(&priv->mutex);
Emmanuel Grumbach49ea8592008-04-15 16:01:37 -07006546 rc = iwl_send_statistics_request(priv, 0);
Zhu Yib481de92007-09-25 17:54:57 -07006547 mutex_unlock(&priv->mutex);
6548
6549 if (rc) {
6550 len = sprintf(buf,
6551 "Error sending statistics request: 0x%08X\n", rc);
6552 return len;
6553 }
6554
6555 while (size && (PAGE_SIZE - len)) {
6556 hex_dump_to_buffer(data + ofs, size, 16, 1, buf + len,
6557 PAGE_SIZE - len, 1);
6558 len = strlen(buf);
6559 if (PAGE_SIZE - len)
6560 buf[len++] = '\n';
6561
6562 ofs += 16;
6563 size -= min(size, 16U);
6564 }
6565
6566 return len;
6567}
6568
6569static DEVICE_ATTR(statistics, S_IRUGO, show_statistics, NULL);
6570
Zhu Yib481de92007-09-25 17:54:57 -07006571static ssize_t show_status(struct device *d,
6572 struct device_attribute *attr, char *buf)
6573{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006574 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Tomas Winklerfee12472008-04-03 16:05:21 -07006575 if (!iwl_is_alive(priv))
Zhu Yib481de92007-09-25 17:54:57 -07006576 return -EAGAIN;
6577 return sprintf(buf, "0x%08x\n", (int)priv->status);
6578}
6579
6580static DEVICE_ATTR(status, S_IRUGO, show_status, NULL);
6581
6582static ssize_t dump_error_log(struct device *d,
6583 struct device_attribute *attr,
6584 const char *buf, size_t count)
6585{
6586 char *p = (char *)buf;
6587
6588 if (p[0] == '1')
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006589 iwl4965_dump_nic_error_log((struct iwl_priv *)d->driver_data);
Zhu Yib481de92007-09-25 17:54:57 -07006590
6591 return strnlen(buf, count);
6592}
6593
6594static DEVICE_ATTR(dump_errors, S_IWUSR, NULL, dump_error_log);
6595
6596static ssize_t dump_event_log(struct device *d,
6597 struct device_attribute *attr,
6598 const char *buf, size_t count)
6599{
6600 char *p = (char *)buf;
6601
6602 if (p[0] == '1')
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006603 iwl4965_dump_nic_event_log((struct iwl_priv *)d->driver_data);
Zhu Yib481de92007-09-25 17:54:57 -07006604
6605 return strnlen(buf, count);
6606}
6607
6608static DEVICE_ATTR(dump_events, S_IWUSR, NULL, dump_event_log);
6609
6610/*****************************************************************************
6611 *
6612 * driver setup and teardown
6613 *
6614 *****************************************************************************/
6615
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006616static void iwl4965_setup_deferred_work(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07006617{
6618 priv->workqueue = create_workqueue(DRV_NAME);
6619
6620 init_waitqueue_head(&priv->wait_command_queue);
6621
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006622 INIT_WORK(&priv->up, iwl4965_bg_up);
6623 INIT_WORK(&priv->restart, iwl4965_bg_restart);
6624 INIT_WORK(&priv->rx_replenish, iwl4965_bg_rx_replenish);
6625 INIT_WORK(&priv->scan_completed, iwl4965_bg_scan_completed);
6626 INIT_WORK(&priv->request_scan, iwl4965_bg_request_scan);
6627 INIT_WORK(&priv->abort_scan, iwl4965_bg_abort_scan);
6628 INIT_WORK(&priv->rf_kill, iwl4965_bg_rf_kill);
6629 INIT_WORK(&priv->beacon_update, iwl4965_bg_beacon_update);
6630 INIT_DELAYED_WORK(&priv->post_associate, iwl4965_bg_post_associate);
6631 INIT_DELAYED_WORK(&priv->init_alive_start, iwl4965_bg_init_alive_start);
6632 INIT_DELAYED_WORK(&priv->alive_start, iwl4965_bg_alive_start);
6633 INIT_DELAYED_WORK(&priv->scan_check, iwl4965_bg_scan_check);
Zhu Yib481de92007-09-25 17:54:57 -07006634
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006635 iwl4965_hw_setup_deferred_work(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006636
6637 tasklet_init(&priv->irq_tasklet, (void (*)(unsigned long))
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006638 iwl4965_irq_tasklet, (unsigned long)priv);
Zhu Yib481de92007-09-25 17:54:57 -07006639}
6640
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006641static void iwl4965_cancel_deferred_work(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07006642{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006643 iwl4965_hw_cancel_deferred_work(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006644
Joonwoo Park3ae6a052007-11-29 10:43:16 +09006645 cancel_delayed_work_sync(&priv->init_alive_start);
Zhu Yib481de92007-09-25 17:54:57 -07006646 cancel_delayed_work(&priv->scan_check);
6647 cancel_delayed_work(&priv->alive_start);
6648 cancel_delayed_work(&priv->post_associate);
6649 cancel_work_sync(&priv->beacon_update);
6650}
6651
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006652static struct attribute *iwl4965_sysfs_entries[] = {
Zhu Yib481de92007-09-25 17:54:57 -07006653 &dev_attr_channels.attr,
6654 &dev_attr_dump_errors.attr,
6655 &dev_attr_dump_events.attr,
6656 &dev_attr_flags.attr,
6657 &dev_attr_filter_flags.attr,
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08006658#ifdef CONFIG_IWL4965_SPECTRUM_MEASUREMENT
Zhu Yib481de92007-09-25 17:54:57 -07006659 &dev_attr_measurement.attr,
6660#endif
6661 &dev_attr_power_level.attr,
6662 &dev_attr_retry_rate.attr,
Zhu Yib481de92007-09-25 17:54:57 -07006663 &dev_attr_rs_window.attr,
6664 &dev_attr_statistics.attr,
6665 &dev_attr_status.attr,
6666 &dev_attr_temperature.attr,
Zhu Yib481de92007-09-25 17:54:57 -07006667 &dev_attr_tx_power.attr,
6668
6669 NULL
6670};
6671
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006672static struct attribute_group iwl4965_attribute_group = {
Zhu Yib481de92007-09-25 17:54:57 -07006673 .name = NULL, /* put in device directory */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006674 .attrs = iwl4965_sysfs_entries,
Zhu Yib481de92007-09-25 17:54:57 -07006675};
6676
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006677static struct ieee80211_ops iwl4965_hw_ops = {
6678 .tx = iwl4965_mac_tx,
6679 .start = iwl4965_mac_start,
6680 .stop = iwl4965_mac_stop,
6681 .add_interface = iwl4965_mac_add_interface,
6682 .remove_interface = iwl4965_mac_remove_interface,
6683 .config = iwl4965_mac_config,
6684 .config_interface = iwl4965_mac_config_interface,
6685 .configure_filter = iwl4965_configure_filter,
6686 .set_key = iwl4965_mac_set_key,
Emmanuel Grumbachab885f82008-03-20 15:06:43 +02006687 .update_tkip_key = iwl4965_mac_update_tkip_key,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006688 .get_stats = iwl4965_mac_get_stats,
6689 .get_tx_stats = iwl4965_mac_get_tx_stats,
6690 .conf_tx = iwl4965_mac_conf_tx,
6691 .get_tsf = iwl4965_mac_get_tsf,
6692 .reset_tsf = iwl4965_mac_reset_tsf,
6693 .beacon_update = iwl4965_mac_beacon_update,
Johannes Berg471b3ef2007-12-28 14:32:58 +01006694 .bss_info_changed = iwl4965_bss_info_changed,
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08006695#ifdef CONFIG_IWL4965_HT
Ron Rindjunsky9ab46172007-12-25 17:00:38 +02006696 .ampdu_action = iwl4965_mac_ampdu_action,
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08006697#endif /* CONFIG_IWL4965_HT */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006698 .hw_scan = iwl4965_mac_hw_scan
Zhu Yib481de92007-09-25 17:54:57 -07006699};
6700
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006701static int iwl4965_pci_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
Zhu Yib481de92007-09-25 17:54:57 -07006702{
6703 int err = 0;
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006704 struct iwl_priv *priv;
Zhu Yib481de92007-09-25 17:54:57 -07006705 struct ieee80211_hw *hw;
Tomas Winkler82b9a122008-03-04 18:09:30 -08006706 struct iwl_cfg *cfg = (struct iwl_cfg *)(ent->driver_data);
Mohamed Abbas0359fac2008-03-28 16:21:08 -07006707 unsigned long flags;
Zhu Yi5a66926a2008-01-14 17:46:18 -08006708 DECLARE_MAC_BUF(mac);
Zhu Yib481de92007-09-25 17:54:57 -07006709
Assaf Krauss316c30d2008-03-14 10:38:46 -07006710 /************************
6711 * 1. Allocating HW data
6712 ************************/
6713
Cahill, Ben M6440adb2007-11-29 11:09:55 +08006714 /* Disabling hardware scan means that mac80211 will perform scans
6715 * "the hard way", rather than using device's scan. */
Assaf Krauss1ea87392008-03-18 14:57:50 -07006716 if (cfg->mod_params->disable_hw_scan) {
Ester Kummerbf403db2008-05-05 10:22:40 +08006717 if (cfg->mod_params->debug & IWL_DL_INFO)
6718 dev_printk(KERN_DEBUG, &(pdev->dev),
6719 "Disabling hw_scan\n");
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006720 iwl4965_hw_ops.hw_scan = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07006721 }
6722
Assaf Krauss1d0a0822008-03-14 10:38:48 -07006723 hw = iwl_alloc_all(cfg, &iwl4965_hw_ops);
6724 if (!hw) {
Zhu Yib481de92007-09-25 17:54:57 -07006725 err = -ENOMEM;
6726 goto out;
6727 }
Assaf Krauss1d0a0822008-03-14 10:38:48 -07006728 priv = hw->priv;
6729 /* At this point both hw and priv are allocated. */
6730
Zhu Yib481de92007-09-25 17:54:57 -07006731 SET_IEEE80211_DEV(hw, &pdev->dev);
6732
6733 IWL_DEBUG_INFO("*** LOAD DRIVER ***\n");
Tomas Winkler82b9a122008-03-04 18:09:30 -08006734 priv->cfg = cfg;
Zhu Yib481de92007-09-25 17:54:57 -07006735 priv->pci_dev = pdev;
Assaf Krauss316c30d2008-03-14 10:38:46 -07006736
Tomas Winkler0a6857e2008-03-12 16:58:49 -07006737#ifdef CONFIG_IWLWIFI_DEBUG
Ester Kummerbf403db2008-05-05 10:22:40 +08006738 priv->debug_level = priv->cfg->mod_params->debug;
Zhu Yib481de92007-09-25 17:54:57 -07006739 atomic_set(&priv->restrict_refcnt, 0);
6740#endif
Zhu Yib481de92007-09-25 17:54:57 -07006741
Assaf Krauss316c30d2008-03-14 10:38:46 -07006742 /**************************
6743 * 2. Initializing PCI bus
6744 **************************/
6745 if (pci_enable_device(pdev)) {
6746 err = -ENODEV;
6747 goto out_ieee80211_free_hw;
6748 }
6749
6750 pci_set_master(pdev);
6751
Ron Rindjunskycc2a8ea2008-04-21 15:41:59 -07006752 err = pci_set_dma_mask(pdev, DMA_64BIT_MASK);
Assaf Krauss316c30d2008-03-14 10:38:46 -07006753 if (!err)
Ron Rindjunskycc2a8ea2008-04-21 15:41:59 -07006754 err = pci_set_consistent_dma_mask(pdev, DMA_64BIT_MASK);
6755 if (err) {
6756 err = pci_set_dma_mask(pdev, DMA_32BIT_MASK);
6757 if (!err)
6758 err = pci_set_consistent_dma_mask(pdev, DMA_32BIT_MASK);
6759 /* both attempts failed: */
Assaf Krauss316c30d2008-03-14 10:38:46 -07006760 if (err) {
Ron Rindjunskycc2a8ea2008-04-21 15:41:59 -07006761 printk(KERN_WARNING "%s: No suitable DMA available.\n",
6762 DRV_NAME);
Assaf Krauss316c30d2008-03-14 10:38:46 -07006763 goto out_pci_disable_device;
Ron Rindjunskycc2a8ea2008-04-21 15:41:59 -07006764 }
Assaf Krauss316c30d2008-03-14 10:38:46 -07006765 }
6766
6767 err = pci_request_regions(pdev, DRV_NAME);
6768 if (err)
6769 goto out_pci_disable_device;
6770
6771 pci_set_drvdata(pdev, priv);
6772
6773 /* We disable the RETRY_TIMEOUT register (0x41) to keep
6774 * PCI Tx retries from interfering with C3 CPU state */
6775 pci_write_config_byte(pdev, 0x41, 0x00);
6776
6777 /***********************
6778 * 3. Read REV register
6779 ***********************/
6780 priv->hw_base = pci_iomap(pdev, 0, 0);
6781 if (!priv->hw_base) {
6782 err = -ENODEV;
6783 goto out_pci_release_regions;
6784 }
6785
6786 IWL_DEBUG_INFO("pci_resource_len = 0x%08llx\n",
6787 (unsigned long long) pci_resource_len(pdev, 0));
6788 IWL_DEBUG_INFO("pci_resource_base = %p\n", priv->hw_base);
6789
Tomas Winklerb661c812008-04-23 17:14:54 -07006790 iwl_hw_detect(priv);
Assaf Krauss316c30d2008-03-14 10:38:46 -07006791 printk(KERN_INFO DRV_NAME
Tomas Winklerb661c812008-04-23 17:14:54 -07006792 ": Detected Intel Wireless WiFi Link %s REV=0x%X\n",
6793 priv->cfg->name, priv->hw_rev);
Assaf Krauss316c30d2008-03-14 10:38:46 -07006794
Tomas Winkler91238712008-04-23 17:14:53 -07006795 /* amp init */
6796 err = priv->cfg->ops->lib->apm_ops.init(priv);
6797 if (err < 0) {
6798 IWL_DEBUG_INFO("Failed to init APMG\n");
6799 goto out_iounmap;
6800 }
Assaf Krauss316c30d2008-03-14 10:38:46 -07006801 /*****************
6802 * 4. Read EEPROM
6803 *****************/
Assaf Krauss316c30d2008-03-14 10:38:46 -07006804 /* Read the EEPROM */
6805 err = iwl_eeprom_init(priv);
6806 if (err) {
6807 IWL_ERROR("Unable to init EEPROM\n");
6808 goto out_iounmap;
6809 }
Tomas Winkler8614f362008-04-23 17:14:55 -07006810 err = iwl_eeprom_check_version(priv);
6811 if (err)
6812 goto out_iounmap;
6813
Assaf Krauss316c30d2008-03-14 10:38:46 -07006814 /* MAC Address location in EEPROM same for 3945/4965 */
6815 iwl_eeprom_get_mac(priv, priv->mac_addr);
6816 IWL_DEBUG_INFO("MAC address: %s\n", print_mac(mac, priv->mac_addr));
6817 SET_IEEE80211_PERM_ADDR(priv->hw, priv->mac_addr);
6818
6819 /************************
6820 * 5. Setup HW constants
6821 ************************/
6822 /* Device-specific setup */
Tomas Winkler5425e492008-04-15 16:01:38 -07006823 if (priv->cfg->ops->lib->set_hw_params(priv)) {
6824 IWL_ERROR("failed to set hw parameters\n");
Tomas Winkler073d3f52008-04-21 15:41:52 -07006825 goto out_free_eeprom;
Assaf Krauss316c30d2008-03-14 10:38:46 -07006826 }
6827
6828 /*******************
6829 * 6. Setup hw/priv
6830 *******************/
Zhu Yib481de92007-09-25 17:54:57 -07006831
Assaf Kraussbf85ea42008-03-14 10:38:49 -07006832 err = iwl_setup(priv);
6833 if (err)
Ron Rindjunsky399f4902008-04-23 17:14:56 -07006834 goto out_free_eeprom;
Assaf Kraussbf85ea42008-03-14 10:38:49 -07006835 /* At this point both hw and priv are initialized. */
Assaf Krauss316c30d2008-03-14 10:38:46 -07006836
6837 /**********************************
6838 * 7. Initialize module parameters
6839 **********************************/
6840
6841 /* Disable radio (SW RF KILL) via parameter when loading driver */
Assaf Krauss1ea87392008-03-18 14:57:50 -07006842 if (priv->cfg->mod_params->disable) {
Assaf Krauss316c30d2008-03-14 10:38:46 -07006843 set_bit(STATUS_RF_KILL_SW, &priv->status);
6844 IWL_DEBUG_INFO("Radio disabled.\n");
6845 }
6846
Assaf Krauss1ea87392008-03-18 14:57:50 -07006847 if (priv->cfg->mod_params->enable_qos)
Assaf Krauss316c30d2008-03-14 10:38:46 -07006848 priv->qos_data.qos_enable = 1;
6849
6850 /********************
6851 * 8. Setup services
6852 ********************/
Mohamed Abbas0359fac2008-03-28 16:21:08 -07006853 spin_lock_irqsave(&priv->lock, flags);
Assaf Krauss316c30d2008-03-14 10:38:46 -07006854 iwl4965_disable_interrupts(priv);
Mohamed Abbas0359fac2008-03-28 16:21:08 -07006855 spin_unlock_irqrestore(&priv->lock, flags);
Assaf Krauss316c30d2008-03-14 10:38:46 -07006856
6857 err = sysfs_create_group(&pdev->dev.kobj, &iwl4965_attribute_group);
6858 if (err) {
6859 IWL_ERROR("failed to create sysfs device attributes\n");
Ron Rindjunsky399f4902008-04-23 17:14:56 -07006860 goto out_free_eeprom;
Assaf Krauss316c30d2008-03-14 10:38:46 -07006861 }
6862
6863 err = iwl_dbgfs_register(priv, DRV_NAME);
6864 if (err) {
6865 IWL_ERROR("failed to create debugfs files\n");
6866 goto out_remove_sysfs;
6867 }
6868
6869 iwl4965_setup_deferred_work(priv);
6870 iwl4965_setup_rx_handlers(priv);
6871
6872 /********************
6873 * 9. Conclude
6874 ********************/
Zhu Yi5a66926a2008-01-14 17:46:18 -08006875 pci_save_state(pdev);
6876 pci_disable_device(pdev);
Zhu Yib481de92007-09-25 17:54:57 -07006877
Mohamed Abbasc8381fd2008-03-28 16:21:05 -07006878 /* notify iwlcore to init */
6879 iwlcore_low_level_notify(priv, IWLCORE_INIT_EVT);
Zhu Yib481de92007-09-25 17:54:57 -07006880 return 0;
6881
Assaf Krauss316c30d2008-03-14 10:38:46 -07006882 out_remove_sysfs:
6883 sysfs_remove_group(&pdev->dev.kobj, &iwl4965_attribute_group);
Tomas Winkler073d3f52008-04-21 15:41:52 -07006884 out_free_eeprom:
6885 iwl_eeprom_free(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006886 out_iounmap:
6887 pci_iounmap(pdev, priv->hw_base);
6888 out_pci_release_regions:
6889 pci_release_regions(pdev);
Assaf Krauss316c30d2008-03-14 10:38:46 -07006890 pci_set_drvdata(pdev, NULL);
Zhu Yib481de92007-09-25 17:54:57 -07006891 out_pci_disable_device:
6892 pci_disable_device(pdev);
Zhu Yib481de92007-09-25 17:54:57 -07006893 out_ieee80211_free_hw:
6894 ieee80211_free_hw(priv->hw);
6895 out:
6896 return err;
6897}
6898
Reinette Chatrec83dbf62008-03-21 13:53:41 -07006899static void __devexit iwl4965_pci_remove(struct pci_dev *pdev)
Zhu Yib481de92007-09-25 17:54:57 -07006900{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006901 struct iwl_priv *priv = pci_get_drvdata(pdev);
Zhu Yib481de92007-09-25 17:54:57 -07006902 struct list_head *p, *q;
6903 int i;
Mohamed Abbas0359fac2008-03-28 16:21:08 -07006904 unsigned long flags;
Zhu Yib481de92007-09-25 17:54:57 -07006905
6906 if (!priv)
6907 return;
6908
6909 IWL_DEBUG_INFO("*** UNLOAD DRIVER ***\n");
6910
Ron Rindjunskyc4f55232008-03-28 16:21:10 -07006911 if (priv->mac80211_registered) {
6912 ieee80211_unregister_hw(priv->hw);
6913 priv->mac80211_registered = 0;
6914 }
6915
Zhu Yib481de92007-09-25 17:54:57 -07006916 set_bit(STATUS_EXIT_PENDING, &priv->status);
Zhu Yib24d22b2007-12-19 13:59:52 +08006917
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006918 iwl4965_down(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006919
Mohamed Abbas0359fac2008-03-28 16:21:08 -07006920 /* make sure we flush any pending irq or
6921 * tasklet for the driver
6922 */
6923 spin_lock_irqsave(&priv->lock, flags);
6924 iwl4965_disable_interrupts(priv);
6925 spin_unlock_irqrestore(&priv->lock, flags);
6926
6927 iwl_synchronize_irq(priv);
6928
Zhu Yib481de92007-09-25 17:54:57 -07006929 /* Free MAC hash list for ADHOC */
6930 for (i = 0; i < IWL_IBSS_MAC_HASH_SIZE; i++) {
6931 list_for_each_safe(p, q, &priv->ibss_mac_hash[i]) {
6932 list_del(p);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006933 kfree(list_entry(p, struct iwl4965_ibss_seq, list));
Zhu Yib481de92007-09-25 17:54:57 -07006934 }
6935 }
6936
Mohamed Abbasc8381fd2008-03-28 16:21:05 -07006937 iwlcore_low_level_notify(priv, IWLCORE_REMOVE_EVT);
Tomas Winkler712b6cf2008-03-12 16:58:52 -07006938 iwl_dbgfs_unregister(priv);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006939 sysfs_remove_group(&pdev->dev.kobj, &iwl4965_attribute_group);
Zhu Yib481de92007-09-25 17:54:57 -07006940
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006941 iwl4965_dealloc_ucode_pci(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006942
6943 if (priv->rxq.bd)
Tomas Winklera55360e2008-05-05 10:22:28 +08006944 iwl_rx_queue_free(priv, &priv->rxq);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006945 iwl4965_hw_txq_ctx_free(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006946
Assaf Kraussbf85ea42008-03-14 10:38:49 -07006947 iwlcore_clear_stations_table(priv);
Tomas Winkler073d3f52008-04-21 15:41:52 -07006948 iwl_eeprom_free(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006949
Zhu Yib481de92007-09-25 17:54:57 -07006950
Mohamed Abbas948c1712007-10-25 17:15:45 +08006951 /*netif_stop_queue(dev); */
6952 flush_workqueue(priv->workqueue);
6953
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006954 /* ieee80211_unregister_hw calls iwl4965_mac_stop, which flushes
Zhu Yib481de92007-09-25 17:54:57 -07006955 * priv->workqueue... so we can't take down the workqueue
6956 * until now... */
6957 destroy_workqueue(priv->workqueue);
6958 priv->workqueue = NULL;
6959
Zhu Yib481de92007-09-25 17:54:57 -07006960 pci_iounmap(pdev, priv->hw_base);
6961 pci_release_regions(pdev);
6962 pci_disable_device(pdev);
6963 pci_set_drvdata(pdev, NULL);
6964
Assaf Kraussbf85ea42008-03-14 10:38:49 -07006965 iwl_free_channel_map(priv);
Ron Rindjunskyc7de35c2008-04-23 17:15:05 -07006966 iwlcore_free_geos(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006967
6968 if (priv->ibss_beacon)
6969 dev_kfree_skb(priv->ibss_beacon);
6970
6971 ieee80211_free_hw(priv->hw);
6972}
6973
6974#ifdef CONFIG_PM
6975
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006976static int iwl4965_pci_suspend(struct pci_dev *pdev, pm_message_t state)
Zhu Yib481de92007-09-25 17:54:57 -07006977{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006978 struct iwl_priv *priv = pci_get_drvdata(pdev);
Zhu Yib481de92007-09-25 17:54:57 -07006979
Zhu Yie655b9f2008-01-24 02:19:38 -08006980 if (priv->is_open) {
6981 set_bit(STATUS_IN_SUSPEND, &priv->status);
6982 iwl4965_mac_stop(priv->hw);
6983 priv->is_open = 1;
6984 }
Zhu Yib481de92007-09-25 17:54:57 -07006985
Zhu Yib481de92007-09-25 17:54:57 -07006986 pci_set_power_state(pdev, PCI_D3hot);
6987
Zhu Yib481de92007-09-25 17:54:57 -07006988 return 0;
6989}
6990
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006991static int iwl4965_pci_resume(struct pci_dev *pdev)
Zhu Yib481de92007-09-25 17:54:57 -07006992{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006993 struct iwl_priv *priv = pci_get_drvdata(pdev);
Zhu Yib481de92007-09-25 17:54:57 -07006994
Zhu Yib481de92007-09-25 17:54:57 -07006995 pci_set_power_state(pdev, PCI_D0);
Zhu Yib481de92007-09-25 17:54:57 -07006996
Zhu Yie655b9f2008-01-24 02:19:38 -08006997 if (priv->is_open)
6998 iwl4965_mac_start(priv->hw);
Zhu Yib481de92007-09-25 17:54:57 -07006999
Zhu Yie655b9f2008-01-24 02:19:38 -08007000 clear_bit(STATUS_IN_SUSPEND, &priv->status);
Zhu Yib481de92007-09-25 17:54:57 -07007001 return 0;
7002}
7003
7004#endif /* CONFIG_PM */
7005
7006/*****************************************************************************
7007 *
7008 * driver and module entry point
7009 *
7010 *****************************************************************************/
7011
Ron Rindjunskyfed90172008-04-15 16:01:41 -07007012/* Hardware specific file defines the PCI IDs table for that hardware module */
7013static struct pci_device_id iwl_hw_card_ids[] = {
7014 {IWL_PCI_DEVICE(0x4229, PCI_ANY_ID, iwl4965_agn_cfg)},
7015 {IWL_PCI_DEVICE(0x4230, PCI_ANY_ID, iwl4965_agn_cfg)},
Tomas Winkler5a6a2562008-04-24 11:55:23 -07007016#ifdef CONFIG_IWL5000
7017 {IWL_PCI_DEVICE(0x4235, PCI_ANY_ID, iwl5300_agn_cfg)},
7018 {IWL_PCI_DEVICE(0x4232, PCI_ANY_ID, iwl5100_agn_cfg)},
7019 {IWL_PCI_DEVICE(0x423A, PCI_ANY_ID, iwl5350_agn_cfg)},
7020#endif /* CONFIG_IWL5000 */
Ron Rindjunskyfed90172008-04-15 16:01:41 -07007021 {0}
7022};
7023MODULE_DEVICE_TABLE(pci, iwl_hw_card_ids);
7024
7025static struct pci_driver iwl_driver = {
Zhu Yib481de92007-09-25 17:54:57 -07007026 .name = DRV_NAME,
Ron Rindjunskyfed90172008-04-15 16:01:41 -07007027 .id_table = iwl_hw_card_ids,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007028 .probe = iwl4965_pci_probe,
7029 .remove = __devexit_p(iwl4965_pci_remove),
Zhu Yib481de92007-09-25 17:54:57 -07007030#ifdef CONFIG_PM
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007031 .suspend = iwl4965_pci_suspend,
7032 .resume = iwl4965_pci_resume,
Zhu Yib481de92007-09-25 17:54:57 -07007033#endif
7034};
7035
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007036static int __init iwl4965_init(void)
Zhu Yib481de92007-09-25 17:54:57 -07007037{
7038
7039 int ret;
7040 printk(KERN_INFO DRV_NAME ": " DRV_DESCRIPTION ", " DRV_VERSION "\n");
7041 printk(KERN_INFO DRV_NAME ": " DRV_COPYRIGHT "\n");
Reinette Chatre897e1cf2008-03-28 16:21:09 -07007042
7043 ret = iwl4965_rate_control_register();
7044 if (ret) {
7045 IWL_ERROR("Unable to register rate control algorithm: %d\n", ret);
7046 return ret;
7047 }
7048
Ron Rindjunskyfed90172008-04-15 16:01:41 -07007049 ret = pci_register_driver(&iwl_driver);
Zhu Yib481de92007-09-25 17:54:57 -07007050 if (ret) {
7051 IWL_ERROR("Unable to initialize PCI module\n");
Reinette Chatre897e1cf2008-03-28 16:21:09 -07007052 goto error_register;
Zhu Yib481de92007-09-25 17:54:57 -07007053 }
Tomas Winkler0a6857e2008-03-12 16:58:49 -07007054#ifdef CONFIG_IWLWIFI_DEBUG
Ron Rindjunskyfed90172008-04-15 16:01:41 -07007055 ret = driver_create_file(&iwl_driver.driver, &driver_attr_debug_level);
Zhu Yib481de92007-09-25 17:54:57 -07007056 if (ret) {
7057 IWL_ERROR("Unable to create driver sysfs file\n");
Reinette Chatre897e1cf2008-03-28 16:21:09 -07007058 goto error_debug;
Zhu Yib481de92007-09-25 17:54:57 -07007059 }
7060#endif
7061
7062 return ret;
Reinette Chatre897e1cf2008-03-28 16:21:09 -07007063
7064#ifdef CONFIG_IWLWIFI_DEBUG
7065error_debug:
Ron Rindjunskyfed90172008-04-15 16:01:41 -07007066 pci_unregister_driver(&iwl_driver);
Reinette Chatre897e1cf2008-03-28 16:21:09 -07007067#endif
7068error_register:
7069 iwl4965_rate_control_unregister();
7070 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07007071}
7072
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007073static void __exit iwl4965_exit(void)
Zhu Yib481de92007-09-25 17:54:57 -07007074{
Tomas Winkler0a6857e2008-03-12 16:58:49 -07007075#ifdef CONFIG_IWLWIFI_DEBUG
Ron Rindjunskyfed90172008-04-15 16:01:41 -07007076 driver_remove_file(&iwl_driver.driver, &driver_attr_debug_level);
Zhu Yib481de92007-09-25 17:54:57 -07007077#endif
Ron Rindjunskyfed90172008-04-15 16:01:41 -07007078 pci_unregister_driver(&iwl_driver);
Reinette Chatre897e1cf2008-03-28 16:21:09 -07007079 iwl4965_rate_control_unregister();
Zhu Yib481de92007-09-25 17:54:57 -07007080}
7081
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007082module_exit(iwl4965_exit);
7083module_init(iwl4965_init);