blob: c5ac82c4aeb478b5f8e75951a6814233fb7df905 [file] [log] [blame]
Zhu Yib481de92007-09-25 17:54:57 -07001/******************************************************************************
2 *
Reinette Chatreeb7ae892008-03-11 16:17:17 -07003 * Copyright(c) 2003 - 2008 Intel Corporation. All rights reserved.
Zhu Yib481de92007-09-25 17:54:57 -07004 *
5 * Portions of this file are derived from the ipw3945 project, as well
6 * as portions of the ieee80211 subsystem header files.
7 *
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of version 2 of the GNU General Public License as
10 * published by the Free Software Foundation.
11 *
12 * This program is distributed in the hope that it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 * more details.
16 *
17 * You should have received a copy of the GNU General Public License along with
18 * this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
20 *
21 * The full GNU General Public License is included in this distribution in the
22 * file called LICENSE.
23 *
24 * Contact Information:
25 * James P. Ketrenos <ipw2100-admin@linux.intel.com>
26 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
27 *
28 *****************************************************************************/
29
Zhu Yib481de92007-09-25 17:54:57 -070030#include <linux/kernel.h>
31#include <linux/module.h>
32#include <linux/version.h>
33#include <linux/init.h>
34#include <linux/pci.h>
35#include <linux/dma-mapping.h>
36#include <linux/delay.h>
37#include <linux/skbuff.h>
38#include <linux/netdevice.h>
39#include <linux/wireless.h>
40#include <linux/firmware.h>
Zhu Yib481de92007-09-25 17:54:57 -070041#include <linux/etherdevice.h>
42#include <linux/if_arp.h>
43
Zhu Yib481de92007-09-25 17:54:57 -070044#include <net/mac80211.h>
45
46#include <asm/div64.h>
47
Assaf Krauss6bc913b2008-03-11 16:17:18 -070048#include "iwl-eeprom.h"
Zhu Yib481de92007-09-25 17:54:57 -070049#include "iwl-4965.h"
Tomas Winklerfee12472008-04-03 16:05:21 -070050#include "iwl-core.h"
Tomas Winkler3395f6e2008-03-25 16:33:37 -070051#include "iwl-io.h"
Zhu Yib481de92007-09-25 17:54:57 -070052#include "iwl-helpers.h"
Emmanuel Grumbach6974e362008-04-14 21:16:06 -070053#include "iwl-sta.h"
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
Johannes Berg8318d782008-01-24 19:38:38 +0100102static const struct ieee80211_supported_band *iwl4965_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
Zhu Yib481de92007-09-25 17:54:57 -0700148/*************** DMA-QUEUE-GENERAL-FUNCTIONS *****
149 * DMA services
150 *
151 * Theory of operation
152 *
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800153 * A Tx or Rx queue resides in host DRAM, and is comprised of a circular buffer
154 * of buffer descriptors, each of which points to one or more data buffers for
155 * the device to read from or fill. Driver and device exchange status of each
156 * queue via "read" and "write" pointers. Driver keeps minimum of 2 empty
157 * entries in each circular buffer, to protect against confusing empty and full
158 * queue states.
159 *
160 * The device reads or writes the data in the queues via the device's several
161 * DMA/FIFO channels. Each queue is mapped to a single DMA channel.
Zhu Yib481de92007-09-25 17:54:57 -0700162 *
163 * For Tx queue, there are low mark and high mark limits. If, after queuing
164 * the packet for Tx, free space become < low mark, Tx queue stopped. When
165 * reclaiming packets (on 'tx done IRQ), if free space become > high mark,
166 * Tx queue resumed.
167 *
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800168 * The 4965 operates with up to 17 queues: One receive queue, one transmit
169 * queue (#4) for sending commands to the device firmware, and 15 other
170 * Tx queues that may be mapped to prioritized Tx DMA/FIFO channels.
Ben Cahille3851442007-11-29 11:10:07 +0800171 *
172 * See more detailed info in iwl-4965-hw.h.
Zhu Yib481de92007-09-25 17:54:57 -0700173 ***************************************************/
174
Ron Rindjunskyfe01b472008-01-28 14:07:24 +0200175int iwl4965_queue_space(const struct iwl4965_queue *q)
Zhu Yib481de92007-09-25 17:54:57 -0700176{
Tomas Winklerfc4b6852007-10-25 17:15:24 +0800177 int s = q->read_ptr - q->write_ptr;
Zhu Yib481de92007-09-25 17:54:57 -0700178
Tomas Winklerfc4b6852007-10-25 17:15:24 +0800179 if (q->read_ptr > q->write_ptr)
Zhu Yib481de92007-09-25 17:54:57 -0700180 s -= q->n_bd;
181
182 if (s <= 0)
183 s += q->n_window;
184 /* keep some reserve to not confuse empty and full situations */
185 s -= 2;
186 if (s < 0)
187 s = 0;
188 return s;
189}
190
Zhu Yib481de92007-09-25 17:54:57 -0700191
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800192static inline int x2_queue_used(const struct iwl4965_queue *q, int i)
Zhu Yib481de92007-09-25 17:54:57 -0700193{
Tomas Winklerfc4b6852007-10-25 17:15:24 +0800194 return q->write_ptr > q->read_ptr ?
195 (i >= q->read_ptr && i < q->write_ptr) :
196 !(i < q->read_ptr && i >= q->write_ptr);
Zhu Yib481de92007-09-25 17:54:57 -0700197}
198
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800199static inline u8 get_cmd_index(struct iwl4965_queue *q, u32 index, int is_huge)
Zhu Yib481de92007-09-25 17:54:57 -0700200{
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800201 /* This is for scan command, the big buffer at end of command array */
Zhu Yib481de92007-09-25 17:54:57 -0700202 if (is_huge)
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800203 return q->n_window; /* must be power of 2 */
Zhu Yib481de92007-09-25 17:54:57 -0700204
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800205 /* Otherwise, use normal size buffers */
Zhu Yib481de92007-09-25 17:54:57 -0700206 return index & (q->n_window - 1);
207}
208
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800209/**
210 * iwl4965_queue_init - Initialize queue's high/low-water and read/write indexes
211 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700212static int iwl4965_queue_init(struct iwl_priv *priv, struct iwl4965_queue *q,
Zhu Yib481de92007-09-25 17:54:57 -0700213 int count, int slots_num, u32 id)
214{
215 q->n_bd = count;
216 q->n_window = slots_num;
217 q->id = id;
218
Tomas Winklerc54b6792008-03-06 17:36:53 -0800219 /* count must be power-of-two size, otherwise iwl_queue_inc_wrap
220 * and iwl_queue_dec_wrap are broken. */
Zhu Yib481de92007-09-25 17:54:57 -0700221 BUG_ON(!is_power_of_2(count));
222
223 /* slots_num must be power-of-two size, otherwise
224 * get_cmd_index is broken. */
225 BUG_ON(!is_power_of_2(slots_num));
226
227 q->low_mark = q->n_window / 4;
228 if (q->low_mark < 4)
229 q->low_mark = 4;
230
231 q->high_mark = q->n_window / 8;
232 if (q->high_mark < 2)
233 q->high_mark = 2;
234
Tomas Winklerfc4b6852007-10-25 17:15:24 +0800235 q->write_ptr = q->read_ptr = 0;
Zhu Yib481de92007-09-25 17:54:57 -0700236
237 return 0;
238}
239
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800240/**
241 * iwl4965_tx_queue_alloc - Alloc driver data and TFD CB for one Tx/cmd queue
242 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700243static int iwl4965_tx_queue_alloc(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800244 struct iwl4965_tx_queue *txq, u32 id)
Zhu Yib481de92007-09-25 17:54:57 -0700245{
246 struct pci_dev *dev = priv->pci_dev;
247
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800248 /* Driver private data, only for Tx (not command) queues,
249 * not shared with device. */
Zhu Yib481de92007-09-25 17:54:57 -0700250 if (id != IWL_CMD_QUEUE_NUM) {
251 txq->txb = kmalloc(sizeof(txq->txb[0]) *
252 TFD_QUEUE_SIZE_MAX, GFP_KERNEL);
253 if (!txq->txb) {
Ian Schram01ebd062007-10-25 17:15:22 +0800254 IWL_ERROR("kmalloc for auxiliary BD "
Zhu Yib481de92007-09-25 17:54:57 -0700255 "structures failed\n");
256 goto error;
257 }
258 } else
259 txq->txb = NULL;
260
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800261 /* Circular buffer of transmit frame descriptors (TFDs),
262 * shared with device */
Zhu Yib481de92007-09-25 17:54:57 -0700263 txq->bd = pci_alloc_consistent(dev,
264 sizeof(txq->bd[0]) * TFD_QUEUE_SIZE_MAX,
265 &txq->q.dma_addr);
266
267 if (!txq->bd) {
268 IWL_ERROR("pci_alloc_consistent(%zd) failed\n",
269 sizeof(txq->bd[0]) * TFD_QUEUE_SIZE_MAX);
270 goto error;
271 }
272 txq->q.id = id;
273
274 return 0;
275
276 error:
277 if (txq->txb) {
278 kfree(txq->txb);
279 txq->txb = NULL;
280 }
281
282 return -ENOMEM;
283}
284
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +0800285/**
286 * iwl4965_tx_queue_init - Allocate and initialize one tx/cmd queue
287 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700288int iwl4965_tx_queue_init(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800289 struct iwl4965_tx_queue *txq, int slots_num, u32 txq_id)
Zhu Yib481de92007-09-25 17:54:57 -0700290{
291 struct pci_dev *dev = priv->pci_dev;
292 int len;
293 int rc = 0;
294
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +0800295 /*
296 * Alloc buffer array for commands (Tx or other types of commands).
297 * For the command queue (#4), allocate command space + one big
298 * command for scan, since scan command is very huge; the system will
299 * not have two scans at the same time, so only one is needed.
Tomas Winklerbb542442007-12-05 20:59:59 +0200300 * For normal Tx queues (all other queues), no super-size command
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +0800301 * space is needed.
302 */
Tomas Winkler857485c2008-03-21 13:53:44 -0700303 len = sizeof(struct iwl_cmd) * slots_num;
Zhu Yib481de92007-09-25 17:54:57 -0700304 if (txq_id == IWL_CMD_QUEUE_NUM)
305 len += IWL_MAX_SCAN_SIZE;
306 txq->cmd = pci_alloc_consistent(dev, len, &txq->dma_addr_cmd);
307 if (!txq->cmd)
308 return -ENOMEM;
309
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +0800310 /* Alloc driver data array and TFD circular buffer */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800311 rc = iwl4965_tx_queue_alloc(priv, txq, txq_id);
Zhu Yib481de92007-09-25 17:54:57 -0700312 if (rc) {
313 pci_free_consistent(dev, len, txq->cmd, txq->dma_addr_cmd);
314
315 return -ENOMEM;
316 }
317 txq->need_update = 0;
318
319 /* TFD_QUEUE_SIZE_MAX must be power-of-two size, otherwise
Tomas Winklerc54b6792008-03-06 17:36:53 -0800320 * iwl_queue_inc_wrap and iwl_queue_dec_wrap are broken. */
Zhu Yib481de92007-09-25 17:54:57 -0700321 BUILD_BUG_ON(TFD_QUEUE_SIZE_MAX & (TFD_QUEUE_SIZE_MAX - 1));
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +0800322
323 /* Initialize queue's high/low-water marks, and head/tail indexes */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800324 iwl4965_queue_init(priv, &txq->q, TFD_QUEUE_SIZE_MAX, slots_num, txq_id);
Zhu Yib481de92007-09-25 17:54:57 -0700325
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +0800326 /* Tell device where to find queue */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800327 iwl4965_hw_tx_queue_init(priv, txq);
Zhu Yib481de92007-09-25 17:54:57 -0700328
329 return 0;
330}
331
332/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800333 * iwl4965_tx_queue_free - Deallocate DMA queue.
Zhu Yib481de92007-09-25 17:54:57 -0700334 * @txq: Transmit queue to deallocate.
335 *
336 * Empty queue by removing and destroying all BD's.
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800337 * Free all buffers.
338 * 0-fill, but do not free "txq" descriptor structure.
Zhu Yib481de92007-09-25 17:54:57 -0700339 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700340void iwl4965_tx_queue_free(struct iwl_priv *priv, struct iwl4965_tx_queue *txq)
Zhu Yib481de92007-09-25 17:54:57 -0700341{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800342 struct iwl4965_queue *q = &txq->q;
Zhu Yib481de92007-09-25 17:54:57 -0700343 struct pci_dev *dev = priv->pci_dev;
344 int len;
345
346 if (q->n_bd == 0)
347 return;
348
349 /* first, empty all BD's */
Tomas Winklerfc4b6852007-10-25 17:15:24 +0800350 for (; q->write_ptr != q->read_ptr;
Tomas Winklerc54b6792008-03-06 17:36:53 -0800351 q->read_ptr = iwl_queue_inc_wrap(q->read_ptr, q->n_bd))
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800352 iwl4965_hw_txq_free_tfd(priv, txq);
Zhu Yib481de92007-09-25 17:54:57 -0700353
Tomas Winkler857485c2008-03-21 13:53:44 -0700354 len = sizeof(struct iwl_cmd) * q->n_window;
Zhu Yib481de92007-09-25 17:54:57 -0700355 if (q->id == IWL_CMD_QUEUE_NUM)
356 len += IWL_MAX_SCAN_SIZE;
357
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800358 /* De-alloc array of command/tx buffers */
Zhu Yib481de92007-09-25 17:54:57 -0700359 pci_free_consistent(dev, len, txq->cmd, txq->dma_addr_cmd);
360
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800361 /* De-alloc circular buffer of TFDs */
Zhu Yib481de92007-09-25 17:54:57 -0700362 if (txq->q.n_bd)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800363 pci_free_consistent(dev, sizeof(struct iwl4965_tfd_frame) *
Zhu Yib481de92007-09-25 17:54:57 -0700364 txq->q.n_bd, txq->bd, txq->q.dma_addr);
365
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800366 /* De-alloc array of per-TFD driver data */
Zhu Yib481de92007-09-25 17:54:57 -0700367 if (txq->txb) {
368 kfree(txq->txb);
369 txq->txb = NULL;
370 }
371
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800372 /* 0-fill queue descriptor structure */
Zhu Yib481de92007-09-25 17:54:57 -0700373 memset(txq, 0, sizeof(*txq));
374}
375
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800376const u8 iwl4965_broadcast_addr[ETH_ALEN] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
Zhu Yib481de92007-09-25 17:54:57 -0700377
378/*************** STATION TABLE MANAGEMENT ****
Ben Cahill9fbab512007-11-29 11:09:47 +0800379 * mac80211 should be examined to determine if sta_info is duplicating
Zhu Yib481de92007-09-25 17:54:57 -0700380 * the functionality provided here
381 */
382
383/**************************************************************/
384
Ian Schram01ebd062007-10-25 17:15:22 +0800385#if 0 /* temporary disable till we add real remove station */
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800386/**
387 * iwl4965_remove_station - Remove driver's knowledge of station.
388 *
389 * NOTE: This does not remove station from device's station table.
390 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700391static u8 iwl4965_remove_station(struct iwl_priv *priv, const u8 *addr, int is_ap)
Zhu Yib481de92007-09-25 17:54:57 -0700392{
393 int index = IWL_INVALID_STATION;
394 int i;
395 unsigned long flags;
396
397 spin_lock_irqsave(&priv->sta_lock, flags);
398
399 if (is_ap)
400 index = IWL_AP_ID;
401 else if (is_broadcast_ether_addr(addr))
Tomas Winkler5425e492008-04-15 16:01:38 -0700402 index = priv->hw_params.bcast_sta_id;
Zhu Yib481de92007-09-25 17:54:57 -0700403 else
Tomas Winkler5425e492008-04-15 16:01:38 -0700404 for (i = IWL_STA_ID; i < priv->hw_params.max_stations; i++)
Zhu Yib481de92007-09-25 17:54:57 -0700405 if (priv->stations[i].used &&
406 !compare_ether_addr(priv->stations[i].sta.sta.addr,
407 addr)) {
408 index = i;
409 break;
410 }
411
412 if (unlikely(index == IWL_INVALID_STATION))
413 goto out;
414
415 if (priv->stations[index].used) {
416 priv->stations[index].used = 0;
417 priv->num_stations--;
418 }
419
420 BUG_ON(priv->num_stations < 0);
421
422out:
423 spin_unlock_irqrestore(&priv->sta_lock, flags);
424 return 0;
425}
Zhu Yi556f8db2007-09-27 11:27:33 +0800426#endif
Zhu Yib481de92007-09-25 17:54:57 -0700427
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800428/**
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800429 * iwl4965_add_station_flags - Add station to tables in driver and device
430 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700431u8 iwl4965_add_station_flags(struct iwl_priv *priv, const u8 *addr,
Ron Rindjunsky67d62032007-11-26 16:14:40 +0200432 int is_ap, u8 flags, void *ht_data)
Zhu Yib481de92007-09-25 17:54:57 -0700433{
434 int i;
435 int index = IWL_INVALID_STATION;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800436 struct iwl4965_station_entry *station;
Zhu Yib481de92007-09-25 17:54:57 -0700437 unsigned long flags_spin;
Joe Perches0795af52007-10-03 17:59:30 -0700438 DECLARE_MAC_BUF(mac);
Zhu Yib481de92007-09-25 17:54:57 -0700439
440 spin_lock_irqsave(&priv->sta_lock, flags_spin);
441 if (is_ap)
442 index = IWL_AP_ID;
443 else if (is_broadcast_ether_addr(addr))
Tomas Winkler5425e492008-04-15 16:01:38 -0700444 index = priv->hw_params.bcast_sta_id;
Zhu Yib481de92007-09-25 17:54:57 -0700445 else
Tomas Winkler5425e492008-04-15 16:01:38 -0700446 for (i = IWL_STA_ID; i < priv->hw_params.max_stations; i++) {
Zhu Yib481de92007-09-25 17:54:57 -0700447 if (!compare_ether_addr(priv->stations[i].sta.sta.addr,
448 addr)) {
449 index = i;
450 break;
451 }
452
453 if (!priv->stations[i].used &&
454 index == IWL_INVALID_STATION)
455 index = i;
456 }
457
458
Ben Cahill9fbab512007-11-29 11:09:47 +0800459 /* These two conditions have the same outcome, but keep them separate
460 since they have different meanings */
Zhu Yib481de92007-09-25 17:54:57 -0700461 if (unlikely(index == IWL_INVALID_STATION)) {
462 spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
463 return index;
464 }
465
466 if (priv->stations[index].used &&
467 !compare_ether_addr(priv->stations[index].sta.sta.addr, addr)) {
468 spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
469 return index;
470 }
471
472
Joe Perches0795af52007-10-03 17:59:30 -0700473 IWL_DEBUG_ASSOC("Add STA ID %d: %s\n", index, print_mac(mac, addr));
Zhu Yib481de92007-09-25 17:54:57 -0700474 station = &priv->stations[index];
475 station->used = 1;
476 priv->num_stations++;
477
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800478 /* Set up the REPLY_ADD_STA command to send to device */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800479 memset(&station->sta, 0, sizeof(struct iwl4965_addsta_cmd));
Zhu Yib481de92007-09-25 17:54:57 -0700480 memcpy(station->sta.sta.addr, addr, ETH_ALEN);
481 station->sta.mode = 0;
482 station->sta.sta.sta_id = index;
483 station->sta.station_flags = 0;
484
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +0800485#ifdef CONFIG_IWL4965_HT
Zhu Yib481de92007-09-25 17:54:57 -0700486 /* BCAST station and IBSS stations do not work in HT mode */
Tomas Winkler5425e492008-04-15 16:01:38 -0700487 if (index != priv->hw_params.bcast_sta_id &&
Zhu Yib481de92007-09-25 17:54:57 -0700488 priv->iw_mode != IEEE80211_IF_TYPE_IBSS)
Ron Rindjunsky67d62032007-11-26 16:14:40 +0200489 iwl4965_set_ht_add_station(priv, index,
490 (struct ieee80211_ht_info *) ht_data);
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +0800491#endif /*CONFIG_IWL4965_HT*/
Zhu Yib481de92007-09-25 17:54:57 -0700492
493 spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800494
495 /* Add station to device's station table */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800496 iwl4965_send_add_station(priv, &station->sta, flags);
Zhu Yib481de92007-09-25 17:54:57 -0700497 return index;
498
499}
500
Zhu Yib481de92007-09-25 17:54:57 -0700501
Zhu Yib481de92007-09-25 17:54:57 -0700502
503/*************** HOST COMMAND QUEUE FUNCTIONS *****/
504
Zhu Yib481de92007-09-25 17:54:57 -0700505/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800506 * iwl4965_enqueue_hcmd - enqueue a uCode command
Zhu Yib481de92007-09-25 17:54:57 -0700507 * @priv: device private data point
508 * @cmd: a point to the ucode command structure
509 *
510 * The function returns < 0 values to indicate the operation is
511 * failed. On success, it turns the index (> 0) of command in the
512 * command queue.
513 */
Tomas Winkler857485c2008-03-21 13:53:44 -0700514int iwl4965_enqueue_hcmd(struct iwl_priv *priv, struct iwl_host_cmd *cmd)
Zhu Yib481de92007-09-25 17:54:57 -0700515{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800516 struct iwl4965_tx_queue *txq = &priv->txq[IWL_CMD_QUEUE_NUM];
517 struct iwl4965_queue *q = &txq->q;
518 struct iwl4965_tfd_frame *tfd;
Zhu Yib481de92007-09-25 17:54:57 -0700519 u32 *control_flags;
Tomas Winkler857485c2008-03-21 13:53:44 -0700520 struct iwl_cmd *out_cmd;
Zhu Yib481de92007-09-25 17:54:57 -0700521 u32 idx;
522 u16 fix_size = (u16)(cmd->len + sizeof(out_cmd->hdr));
523 dma_addr_t phys_addr;
524 int ret;
525 unsigned long flags;
526
527 /* If any of the command structures end up being larger than
528 * the TFD_MAX_PAYLOAD_SIZE, and it sent as a 'small' command then
529 * we will need to increase the size of the TFD entries */
530 BUG_ON((fix_size > TFD_MAX_PAYLOAD_SIZE) &&
531 !(cmd->meta.flags & CMD_SIZE_HUGE));
532
Tomas Winklerfee12472008-04-03 16:05:21 -0700533 if (iwl_is_rfkill(priv)) {
Gregory Greenmanc342a1b2008-02-06 11:20:40 -0800534 IWL_DEBUG_INFO("Not sending command - RF KILL");
535 return -EIO;
536 }
537
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800538 if (iwl4965_queue_space(q) < ((cmd->meta.flags & CMD_ASYNC) ? 2 : 1)) {
Zhu Yib481de92007-09-25 17:54:57 -0700539 IWL_ERROR("No space for Tx\n");
540 return -ENOSPC;
541 }
542
543 spin_lock_irqsave(&priv->hcmd_lock, flags);
544
Tomas Winklerfc4b6852007-10-25 17:15:24 +0800545 tfd = &txq->bd[q->write_ptr];
Zhu Yib481de92007-09-25 17:54:57 -0700546 memset(tfd, 0, sizeof(*tfd));
547
548 control_flags = (u32 *) tfd;
549
Tomas Winklerfc4b6852007-10-25 17:15:24 +0800550 idx = get_cmd_index(q, q->write_ptr, cmd->meta.flags & CMD_SIZE_HUGE);
Zhu Yib481de92007-09-25 17:54:57 -0700551 out_cmd = &txq->cmd[idx];
552
553 out_cmd->hdr.cmd = cmd->id;
554 memcpy(&out_cmd->meta, &cmd->meta, sizeof(cmd->meta));
555 memcpy(&out_cmd->cmd.payload, cmd->data, cmd->len);
556
557 /* At this point, the out_cmd now has all of the incoming cmd
558 * information */
559
560 out_cmd->hdr.flags = 0;
561 out_cmd->hdr.sequence = cpu_to_le16(QUEUE_TO_SEQ(IWL_CMD_QUEUE_NUM) |
Tomas Winklerfc4b6852007-10-25 17:15:24 +0800562 INDEX_TO_SEQ(q->write_ptr));
Zhu Yib481de92007-09-25 17:54:57 -0700563 if (out_cmd->meta.flags & CMD_SIZE_HUGE)
564 out_cmd->hdr.sequence |= cpu_to_le16(SEQ_HUGE_FRAME);
565
566 phys_addr = txq->dma_addr_cmd + sizeof(txq->cmd[0]) * idx +
Tomas Winkler857485c2008-03-21 13:53:44 -0700567 offsetof(struct iwl_cmd, hdr);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800568 iwl4965_hw_txq_attach_buf_to_tfd(priv, tfd, phys_addr, fix_size);
Zhu Yib481de92007-09-25 17:54:57 -0700569
570 IWL_DEBUG_HC("Sending command %s (#%x), seq: 0x%04X, "
571 "%d bytes at %d[%d]:%d\n",
572 get_cmd_string(out_cmd->hdr.cmd),
573 out_cmd->hdr.cmd, le16_to_cpu(out_cmd->hdr.sequence),
Tomas Winklerfc4b6852007-10-25 17:15:24 +0800574 fix_size, q->write_ptr, idx, IWL_CMD_QUEUE_NUM);
Zhu Yib481de92007-09-25 17:54:57 -0700575
576 txq->need_update = 1;
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800577
578 /* Set up entry in queue's byte count circular buffer */
Tomas Winklere2a722e2008-04-14 21:16:10 -0700579 priv->cfg->ops->lib->txq_update_byte_cnt_tbl(priv, txq, 0);
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800580
581 /* Increment and update queue's write index */
Tomas Winklerc54b6792008-03-06 17:36:53 -0800582 q->write_ptr = iwl_queue_inc_wrap(q->write_ptr, q->n_bd);
Tomas Winklere2a722e2008-04-14 21:16:10 -0700583 ret = iwl4965_tx_queue_update_write_ptr(priv, txq);
Zhu Yib481de92007-09-25 17:54:57 -0700584
585 spin_unlock_irqrestore(&priv->hcmd_lock, flags);
586 return ret ? ret : idx;
587}
588
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -0700589static void iwl4965_set_rxon_hwcrypto(struct iwl_priv *priv, int hw_decrypt)
590{
591 struct iwl4965_rxon_cmd *rxon = &priv->staging_rxon;
592
593 if (hw_decrypt)
594 rxon->filter_flags &= ~RXON_FILTER_DIS_DECRYPT_MSK;
595 else
596 rxon->filter_flags |= RXON_FILTER_DIS_DECRYPT_MSK;
597
598}
599
Zhu Yib481de92007-09-25 17:54:57 -0700600/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800601 * iwl4965_rxon_add_station - add station into station table.
Zhu Yib481de92007-09-25 17:54:57 -0700602 *
603 * there is only one AP station with id= IWL_AP_ID
Ben Cahill9fbab512007-11-29 11:09:47 +0800604 * NOTE: mutex must be held before calling this fnction
605 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700606static int iwl4965_rxon_add_station(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -0700607 const u8 *addr, int is_ap)
608{
Zhu Yi556f8db2007-09-27 11:27:33 +0800609 u8 sta_id;
Zhu Yib481de92007-09-25 17:54:57 -0700610
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800611 /* Add station to device's station table */
Ron Rindjunsky67d62032007-11-26 16:14:40 +0200612#ifdef CONFIG_IWL4965_HT
613 struct ieee80211_conf *conf = &priv->hw->conf;
614 struct ieee80211_ht_info *cur_ht_config = &conf->ht_conf;
615
616 if ((is_ap) &&
617 (conf->flags & IEEE80211_CONF_SUPPORT_HT_MODE) &&
618 (priv->iw_mode == IEEE80211_IF_TYPE_STA))
619 sta_id = iwl4965_add_station_flags(priv, addr, is_ap,
620 0, cur_ht_config);
621 else
622#endif /* CONFIG_IWL4965_HT */
623 sta_id = iwl4965_add_station_flags(priv, addr, is_ap,
624 0, NULL);
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800625
626 /* Set up default rate scaling table in device's station table */
Zhu Yib481de92007-09-25 17:54:57 -0700627 iwl4965_add_station(priv, addr, is_ap);
628
Zhu Yi556f8db2007-09-27 11:27:33 +0800629 return sta_id;
Zhu Yib481de92007-09-25 17:54:57 -0700630}
631
632/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800633 * iwl4965_check_rxon_cmd - validate RXON structure is valid
Zhu Yib481de92007-09-25 17:54:57 -0700634 *
635 * NOTE: This is really only useful during development and can eventually
636 * be #ifdef'd out once the driver is stable and folks aren't actively
637 * making changes
638 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800639static int iwl4965_check_rxon_cmd(struct iwl4965_rxon_cmd *rxon)
Zhu Yib481de92007-09-25 17:54:57 -0700640{
641 int error = 0;
642 int counter = 1;
643
644 if (rxon->flags & RXON_FLG_BAND_24G_MSK) {
645 error |= le32_to_cpu(rxon->flags &
646 (RXON_FLG_TGJ_NARROW_BAND_MSK |
647 RXON_FLG_RADAR_DETECT_MSK));
648 if (error)
649 IWL_WARNING("check 24G fields %d | %d\n",
650 counter++, error);
651 } else {
652 error |= (rxon->flags & RXON_FLG_SHORT_SLOT_MSK) ?
653 0 : le32_to_cpu(RXON_FLG_SHORT_SLOT_MSK);
654 if (error)
655 IWL_WARNING("check 52 fields %d | %d\n",
656 counter++, error);
657 error |= le32_to_cpu(rxon->flags & RXON_FLG_CCK_MSK);
658 if (error)
659 IWL_WARNING("check 52 CCK %d | %d\n",
660 counter++, error);
661 }
662 error |= (rxon->node_addr[0] | rxon->bssid_addr[0]) & 0x1;
663 if (error)
664 IWL_WARNING("check mac addr %d | %d\n", counter++, error);
665
666 /* make sure basic rates 6Mbps and 1Mbps are supported */
667 error |= (((rxon->ofdm_basic_rates & IWL_RATE_6M_MASK) == 0) &&
668 ((rxon->cck_basic_rates & IWL_RATE_1M_MASK) == 0));
669 if (error)
670 IWL_WARNING("check basic rate %d | %d\n", counter++, error);
671
672 error |= (le16_to_cpu(rxon->assoc_id) > 2007);
673 if (error)
674 IWL_WARNING("check assoc id %d | %d\n", counter++, error);
675
676 error |= ((rxon->flags & (RXON_FLG_CCK_MSK | RXON_FLG_SHORT_SLOT_MSK))
677 == (RXON_FLG_CCK_MSK | RXON_FLG_SHORT_SLOT_MSK));
678 if (error)
679 IWL_WARNING("check CCK and short slot %d | %d\n",
680 counter++, error);
681
682 error |= ((rxon->flags & (RXON_FLG_CCK_MSK | RXON_FLG_AUTO_DETECT_MSK))
683 == (RXON_FLG_CCK_MSK | RXON_FLG_AUTO_DETECT_MSK));
684 if (error)
685 IWL_WARNING("check CCK & auto detect %d | %d\n",
686 counter++, error);
687
688 error |= ((rxon->flags & (RXON_FLG_AUTO_DETECT_MSK |
689 RXON_FLG_TGG_PROTECT_MSK)) == RXON_FLG_TGG_PROTECT_MSK);
690 if (error)
691 IWL_WARNING("check TGG and auto detect %d | %d\n",
692 counter++, error);
693
694 if (error)
695 IWL_WARNING("Tuning to channel %d\n",
696 le16_to_cpu(rxon->channel));
697
698 if (error) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800699 IWL_ERROR("Not a valid iwl4965_rxon_assoc_cmd field values\n");
Zhu Yib481de92007-09-25 17:54:57 -0700700 return -1;
701 }
702 return 0;
703}
704
705/**
Ben Cahill9fbab512007-11-29 11:09:47 +0800706 * iwl4965_full_rxon_required - check if full RXON (vs RXON_ASSOC) cmd is needed
Ian Schram01ebd062007-10-25 17:15:22 +0800707 * @priv: staging_rxon is compared to active_rxon
Zhu Yib481de92007-09-25 17:54:57 -0700708 *
Ben Cahill9fbab512007-11-29 11:09:47 +0800709 * If the RXON structure is changing enough to require a new tune,
710 * or is clearing the RXON_FILTER_ASSOC_MSK, then return 1 to indicate that
711 * a new tune (full RXON command, rather than RXON_ASSOC cmd) is required.
Zhu Yib481de92007-09-25 17:54:57 -0700712 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700713static int iwl4965_full_rxon_required(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700714{
715
716 /* These items are only settable from the full RXON command */
717 if (!(priv->active_rxon.filter_flags & RXON_FILTER_ASSOC_MSK) ||
718 compare_ether_addr(priv->staging_rxon.bssid_addr,
719 priv->active_rxon.bssid_addr) ||
720 compare_ether_addr(priv->staging_rxon.node_addr,
721 priv->active_rxon.node_addr) ||
722 compare_ether_addr(priv->staging_rxon.wlap_bssid_addr,
723 priv->active_rxon.wlap_bssid_addr) ||
724 (priv->staging_rxon.dev_type != priv->active_rxon.dev_type) ||
725 (priv->staging_rxon.channel != priv->active_rxon.channel) ||
726 (priv->staging_rxon.air_propagation !=
727 priv->active_rxon.air_propagation) ||
728 (priv->staging_rxon.ofdm_ht_single_stream_basic_rates !=
729 priv->active_rxon.ofdm_ht_single_stream_basic_rates) ||
730 (priv->staging_rxon.ofdm_ht_dual_stream_basic_rates !=
731 priv->active_rxon.ofdm_ht_dual_stream_basic_rates) ||
732 (priv->staging_rxon.rx_chain != priv->active_rxon.rx_chain) ||
733 (priv->staging_rxon.assoc_id != priv->active_rxon.assoc_id))
734 return 1;
735
736 /* flags, filter_flags, ofdm_basic_rates, and cck_basic_rates can
737 * be updated with the RXON_ASSOC command -- however only some
738 * flag transitions are allowed using RXON_ASSOC */
739
740 /* Check if we are not switching bands */
741 if ((priv->staging_rxon.flags & RXON_FLG_BAND_24G_MSK) !=
742 (priv->active_rxon.flags & RXON_FLG_BAND_24G_MSK))
743 return 1;
744
745 /* Check if we are switching association toggle */
746 if ((priv->staging_rxon.filter_flags & RXON_FILTER_ASSOC_MSK) !=
747 (priv->active_rxon.filter_flags & RXON_FILTER_ASSOC_MSK))
748 return 1;
749
750 return 0;
751}
752
Zhu Yib481de92007-09-25 17:54:57 -0700753/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800754 * iwl4965_commit_rxon - commit staging_rxon to hardware
Zhu Yib481de92007-09-25 17:54:57 -0700755 *
Ian Schram01ebd062007-10-25 17:15:22 +0800756 * The RXON command in staging_rxon is committed to the hardware and
Zhu Yib481de92007-09-25 17:54:57 -0700757 * the active_rxon structure is updated with the new data. This
758 * function correctly transitions out of the RXON_ASSOC_MSK state if
759 * a HW tune is required based on the RXON structure changes.
760 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700761static int iwl4965_commit_rxon(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700762{
763 /* cast away the const for active_rxon in this function */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800764 struct iwl4965_rxon_cmd *active_rxon = (void *)&priv->active_rxon;
Joe Perches0795af52007-10-03 17:59:30 -0700765 DECLARE_MAC_BUF(mac);
Zhu Yib481de92007-09-25 17:54:57 -0700766 int rc = 0;
767
Tomas Winklerfee12472008-04-03 16:05:21 -0700768 if (!iwl_is_alive(priv))
Zhu Yib481de92007-09-25 17:54:57 -0700769 return -1;
770
771 /* always get timestamp with Rx frame */
772 priv->staging_rxon.flags |= RXON_FLG_TSF2HOST_MSK;
773
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800774 rc = iwl4965_check_rxon_cmd(&priv->staging_rxon);
Zhu Yib481de92007-09-25 17:54:57 -0700775 if (rc) {
776 IWL_ERROR("Invalid RXON configuration. Not committing.\n");
777 return -EINVAL;
778 }
779
780 /* If we don't need to send a full RXON, we can use
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800781 * iwl4965_rxon_assoc_cmd which is used to reconfigure filter
Zhu Yib481de92007-09-25 17:54:57 -0700782 * and other flags for the current radio configuration. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800783 if (!iwl4965_full_rxon_required(priv)) {
Tomas Winkler7e8c5192008-04-15 16:01:43 -0700784 rc = iwl_send_rxon_assoc(priv);
Zhu Yib481de92007-09-25 17:54:57 -0700785 if (rc) {
786 IWL_ERROR("Error setting RXON_ASSOC "
787 "configuration (%d).\n", rc);
788 return rc;
789 }
790
791 memcpy(active_rxon, &priv->staging_rxon, sizeof(*active_rxon));
792
793 return 0;
794 }
795
796 /* station table will be cleared */
797 priv->assoc_station_added = 0;
798
Zhu Yib481de92007-09-25 17:54:57 -0700799 /* If we are currently associated and the new config requires
800 * an RXON_ASSOC and the new config wants the associated mask enabled,
801 * we must clear the associated from the active configuration
802 * before we apply the new config */
Tomas Winkler3109ece2008-03-28 16:33:35 -0700803 if (iwl_is_associated(priv) &&
Zhu Yib481de92007-09-25 17:54:57 -0700804 (priv->staging_rxon.filter_flags & RXON_FILTER_ASSOC_MSK)) {
805 IWL_DEBUG_INFO("Toggling associated bit on current RXON\n");
806 active_rxon->filter_flags &= ~RXON_FILTER_ASSOC_MSK;
807
Tomas Winkler857485c2008-03-21 13:53:44 -0700808 rc = iwl_send_cmd_pdu(priv, REPLY_RXON,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800809 sizeof(struct iwl4965_rxon_cmd),
Zhu Yib481de92007-09-25 17:54:57 -0700810 &priv->active_rxon);
811
812 /* If the mask clearing failed then we set
813 * active_rxon back to what it was previously */
814 if (rc) {
815 active_rxon->filter_flags |= RXON_FILTER_ASSOC_MSK;
816 IWL_ERROR("Error clearing ASSOC_MSK on current "
817 "configuration (%d).\n", rc);
818 return rc;
819 }
Zhu Yib481de92007-09-25 17:54:57 -0700820 }
821
822 IWL_DEBUG_INFO("Sending RXON\n"
823 "* with%s RXON_FILTER_ASSOC_MSK\n"
824 "* channel = %d\n"
Joe Perches0795af52007-10-03 17:59:30 -0700825 "* bssid = %s\n",
Zhu Yib481de92007-09-25 17:54:57 -0700826 ((priv->staging_rxon.filter_flags &
827 RXON_FILTER_ASSOC_MSK) ? "" : "out"),
828 le16_to_cpu(priv->staging_rxon.channel),
Joe Perches0795af52007-10-03 17:59:30 -0700829 print_mac(mac, priv->staging_rxon.bssid_addr));
Zhu Yib481de92007-09-25 17:54:57 -0700830
Emmanuel Grumbachfcc76c62008-04-15 16:01:47 -0700831 iwl4965_set_rxon_hwcrypto(priv, !priv->cfg->mod_params->sw_crypto);
Zhu Yib481de92007-09-25 17:54:57 -0700832 /* Apply the new configuration */
Tomas Winkler857485c2008-03-21 13:53:44 -0700833 rc = iwl_send_cmd_pdu(priv, REPLY_RXON,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800834 sizeof(struct iwl4965_rxon_cmd), &priv->staging_rxon);
Zhu Yib481de92007-09-25 17:54:57 -0700835 if (rc) {
836 IWL_ERROR("Error setting new configuration (%d).\n", rc);
837 return rc;
838 }
839
Assaf Kraussbf85ea42008-03-14 10:38:49 -0700840 iwlcore_clear_stations_table(priv);
Zhu Yi556f8db2007-09-27 11:27:33 +0800841
Zhu Yib481de92007-09-25 17:54:57 -0700842 if (!priv->error_recovering)
843 priv->start_calib = 0;
844
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -0700845 iwl_init_sensitivity(priv);
Zhu Yib481de92007-09-25 17:54:57 -0700846
847 memcpy(active_rxon, &priv->staging_rxon, sizeof(*active_rxon));
848
849 /* If we issue a new RXON command which required a tune then we must
850 * send a new TXPOWER command or we won't be able to Tx any frames */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800851 rc = iwl4965_hw_reg_send_txpower(priv);
Zhu Yib481de92007-09-25 17:54:57 -0700852 if (rc) {
853 IWL_ERROR("Error setting Tx power (%d).\n", rc);
854 return rc;
855 }
856
857 /* Add the broadcast address so we can send broadcast frames */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800858 if (iwl4965_rxon_add_station(priv, iwl4965_broadcast_addr, 0) ==
Zhu Yib481de92007-09-25 17:54:57 -0700859 IWL_INVALID_STATION) {
860 IWL_ERROR("Error adding BROADCAST address for transmit.\n");
861 return -EIO;
862 }
863
864 /* If we have set the ASSOC_MSK and we are in BSS mode then
865 * add the IWL_AP_ID to the station rate table */
Tomas Winkler3109ece2008-03-28 16:33:35 -0700866 if (iwl_is_associated(priv) &&
Zhu Yib481de92007-09-25 17:54:57 -0700867 (priv->iw_mode == IEEE80211_IF_TYPE_STA)) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800868 if (iwl4965_rxon_add_station(priv, priv->active_rxon.bssid_addr, 1)
Zhu Yib481de92007-09-25 17:54:57 -0700869 == IWL_INVALID_STATION) {
870 IWL_ERROR("Error adding AP address for transmit.\n");
871 return -EIO;
872 }
873 priv->assoc_station_added = 1;
Emmanuel Grumbach6974e362008-04-14 21:16:06 -0700874 if (priv->default_wep_key &&
875 iwl_send_static_wepkey_cmd(priv, 0))
876 IWL_ERROR("Could not send WEP static key.\n");
Zhu Yib481de92007-09-25 17:54:57 -0700877 }
878
879 return 0;
880}
881
Mohamed Abbas5da4b552008-04-21 15:41:51 -0700882void iwl4965_update_chain_flags(struct iwl_priv *priv)
883{
884
885 iwl4965_set_rxon_chain(priv);
886 iwl4965_commit_rxon(priv);
887}
888
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700889static int iwl4965_send_bt_config(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700890{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800891 struct iwl4965_bt_cmd bt_cmd = {
Zhu Yib481de92007-09-25 17:54:57 -0700892 .flags = 3,
893 .lead_time = 0xAA,
894 .max_kill = 1,
895 .kill_ack_mask = 0,
896 .kill_cts_mask = 0,
897 };
898
Tomas Winkler857485c2008-03-21 13:53:44 -0700899 return iwl_send_cmd_pdu(priv, REPLY_BT_CONFIG,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800900 sizeof(struct iwl4965_bt_cmd), &bt_cmd);
Zhu Yib481de92007-09-25 17:54:57 -0700901}
902
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700903static int iwl4965_send_scan_abort(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700904{
905 int rc = 0;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800906 struct iwl4965_rx_packet *res;
Tomas Winkler857485c2008-03-21 13:53:44 -0700907 struct iwl_host_cmd cmd = {
Zhu Yib481de92007-09-25 17:54:57 -0700908 .id = REPLY_SCAN_ABORT_CMD,
909 .meta.flags = CMD_WANT_SKB,
910 };
911
912 /* If there isn't a scan actively going on in the hardware
913 * then we are in between scan bands and not actually
914 * actively scanning, so don't send the abort command */
915 if (!test_bit(STATUS_SCAN_HW, &priv->status)) {
916 clear_bit(STATUS_SCAN_ABORTING, &priv->status);
917 return 0;
918 }
919
Tomas Winkler857485c2008-03-21 13:53:44 -0700920 rc = iwl_send_cmd_sync(priv, &cmd);
Zhu Yib481de92007-09-25 17:54:57 -0700921 if (rc) {
922 clear_bit(STATUS_SCAN_ABORTING, &priv->status);
923 return rc;
924 }
925
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800926 res = (struct iwl4965_rx_packet *)cmd.meta.u.skb->data;
Zhu Yib481de92007-09-25 17:54:57 -0700927 if (res->u.status != CAN_ABORT_STATUS) {
928 /* The scan abort will return 1 for success or
929 * 2 for "failure". A failure condition can be
930 * due to simply not being in an active scan which
931 * can occur if we send the scan abort before we
932 * the microcode has notified us that a scan is
933 * completed. */
934 IWL_DEBUG_INFO("SCAN_ABORT returned %d.\n", res->u.status);
935 clear_bit(STATUS_SCAN_ABORTING, &priv->status);
936 clear_bit(STATUS_SCAN_HW, &priv->status);
937 }
938
939 dev_kfree_skb_any(cmd.meta.u.skb);
940
941 return rc;
942}
943
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700944static int iwl4965_card_state_sync_callback(struct iwl_priv *priv,
Tomas Winkler857485c2008-03-21 13:53:44 -0700945 struct iwl_cmd *cmd,
Zhu Yib481de92007-09-25 17:54:57 -0700946 struct sk_buff *skb)
947{
948 return 1;
949}
950
951/*
952 * CARD_STATE_CMD
953 *
Ben Cahill9fbab512007-11-29 11:09:47 +0800954 * Use: Sets the device's internal card state to enable, disable, or halt
Zhu Yib481de92007-09-25 17:54:57 -0700955 *
956 * When in the 'enable' state the card operates as normal.
957 * When in the 'disable' state, the card enters into a low power mode.
958 * When in the 'halt' state, the card is shut down and must be fully
959 * restarted to come back on.
960 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700961static int iwl4965_send_card_state(struct iwl_priv *priv, u32 flags, u8 meta_flag)
Zhu Yib481de92007-09-25 17:54:57 -0700962{
Tomas Winkler857485c2008-03-21 13:53:44 -0700963 struct iwl_host_cmd cmd = {
Zhu Yib481de92007-09-25 17:54:57 -0700964 .id = REPLY_CARD_STATE_CMD,
965 .len = sizeof(u32),
966 .data = &flags,
967 .meta.flags = meta_flag,
968 };
969
970 if (meta_flag & CMD_ASYNC)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800971 cmd.meta.u.callback = iwl4965_card_state_sync_callback;
Zhu Yib481de92007-09-25 17:54:57 -0700972
Tomas Winkler857485c2008-03-21 13:53:44 -0700973 return iwl_send_cmd(priv, &cmd);
Zhu Yib481de92007-09-25 17:54:57 -0700974}
975
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700976static int iwl4965_add_sta_sync_callback(struct iwl_priv *priv,
Tomas Winkler857485c2008-03-21 13:53:44 -0700977 struct iwl_cmd *cmd, struct sk_buff *skb)
Zhu Yib481de92007-09-25 17:54:57 -0700978{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800979 struct iwl4965_rx_packet *res = NULL;
Zhu Yib481de92007-09-25 17:54:57 -0700980
981 if (!skb) {
982 IWL_ERROR("Error: Response NULL in REPLY_ADD_STA.\n");
983 return 1;
984 }
985
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800986 res = (struct iwl4965_rx_packet *)skb->data;
Zhu Yib481de92007-09-25 17:54:57 -0700987 if (res->hdr.flags & IWL_CMD_FAILED_MSK) {
988 IWL_ERROR("Bad return from REPLY_ADD_STA (0x%08X)\n",
989 res->hdr.flags);
990 return 1;
991 }
992
993 switch (res->u.add_sta.status) {
994 case ADD_STA_SUCCESS_MSK:
995 break;
996 default:
997 break;
998 }
999
1000 /* We didn't cache the SKB; let the caller free it */
1001 return 1;
1002}
1003
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001004int iwl4965_send_add_station(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001005 struct iwl4965_addsta_cmd *sta, u8 flags)
Zhu Yib481de92007-09-25 17:54:57 -07001006{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001007 struct iwl4965_rx_packet *res = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07001008 int rc = 0;
Tomas Winkler857485c2008-03-21 13:53:44 -07001009 struct iwl_host_cmd cmd = {
Zhu Yib481de92007-09-25 17:54:57 -07001010 .id = REPLY_ADD_STA,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001011 .len = sizeof(struct iwl4965_addsta_cmd),
Zhu Yib481de92007-09-25 17:54:57 -07001012 .meta.flags = flags,
1013 .data = sta,
1014 };
1015
1016 if (flags & CMD_ASYNC)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001017 cmd.meta.u.callback = iwl4965_add_sta_sync_callback;
Zhu Yib481de92007-09-25 17:54:57 -07001018 else
1019 cmd.meta.flags |= CMD_WANT_SKB;
1020
Tomas Winkler857485c2008-03-21 13:53:44 -07001021 rc = iwl_send_cmd(priv, &cmd);
Zhu Yib481de92007-09-25 17:54:57 -07001022
1023 if (rc || (flags & CMD_ASYNC))
1024 return rc;
1025
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001026 res = (struct iwl4965_rx_packet *)cmd.meta.u.skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07001027 if (res->hdr.flags & IWL_CMD_FAILED_MSK) {
1028 IWL_ERROR("Bad return from REPLY_ADD_STA (0x%08X)\n",
1029 res->hdr.flags);
1030 rc = -EIO;
1031 }
1032
1033 if (rc == 0) {
1034 switch (res->u.add_sta.status) {
1035 case ADD_STA_SUCCESS_MSK:
1036 IWL_DEBUG_INFO("REPLY_ADD_STA PASSED\n");
1037 break;
1038 default:
1039 rc = -EIO;
1040 IWL_WARNING("REPLY_ADD_STA failed\n");
1041 break;
1042 }
1043 }
1044
1045 priv->alloc_rxb_skb--;
1046 dev_kfree_skb_any(cmd.meta.u.skb);
1047
1048 return rc;
1049}
1050
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001051static void iwl4965_clear_free_frames(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001052{
1053 struct list_head *element;
1054
1055 IWL_DEBUG_INFO("%d frames on pre-allocated heap on clear.\n",
1056 priv->frames_count);
1057
1058 while (!list_empty(&priv->free_frames)) {
1059 element = priv->free_frames.next;
1060 list_del(element);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001061 kfree(list_entry(element, struct iwl4965_frame, list));
Zhu Yib481de92007-09-25 17:54:57 -07001062 priv->frames_count--;
1063 }
1064
1065 if (priv->frames_count) {
1066 IWL_WARNING("%d frames still in use. Did we lose one?\n",
1067 priv->frames_count);
1068 priv->frames_count = 0;
1069 }
1070}
1071
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001072static struct iwl4965_frame *iwl4965_get_free_frame(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001073{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001074 struct iwl4965_frame *frame;
Zhu Yib481de92007-09-25 17:54:57 -07001075 struct list_head *element;
1076 if (list_empty(&priv->free_frames)) {
1077 frame = kzalloc(sizeof(*frame), GFP_KERNEL);
1078 if (!frame) {
1079 IWL_ERROR("Could not allocate frame!\n");
1080 return NULL;
1081 }
1082
1083 priv->frames_count++;
1084 return frame;
1085 }
1086
1087 element = priv->free_frames.next;
1088 list_del(element);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001089 return list_entry(element, struct iwl4965_frame, list);
Zhu Yib481de92007-09-25 17:54:57 -07001090}
1091
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001092static void iwl4965_free_frame(struct iwl_priv *priv, struct iwl4965_frame *frame)
Zhu Yib481de92007-09-25 17:54:57 -07001093{
1094 memset(frame, 0, sizeof(*frame));
1095 list_add(&frame->list, &priv->free_frames);
1096}
1097
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001098unsigned int iwl4965_fill_beacon_frame(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07001099 struct ieee80211_hdr *hdr,
1100 const u8 *dest, int left)
1101{
1102
Tomas Winkler3109ece2008-03-28 16:33:35 -07001103 if (!iwl_is_associated(priv) || !priv->ibss_beacon ||
Zhu Yib481de92007-09-25 17:54:57 -07001104 ((priv->iw_mode != IEEE80211_IF_TYPE_IBSS) &&
1105 (priv->iw_mode != IEEE80211_IF_TYPE_AP)))
1106 return 0;
1107
1108 if (priv->ibss_beacon->len > left)
1109 return 0;
1110
1111 memcpy(hdr, priv->ibss_beacon->data, priv->ibss_beacon->len);
1112
1113 return priv->ibss_beacon->len;
1114}
1115
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001116static u8 iwl4965_rate_get_lowest_plcp(int rate_mask)
Zhu Yib481de92007-09-25 17:54:57 -07001117{
1118 u8 i;
1119
1120 for (i = IWL_RATE_1M_INDEX; i != IWL_RATE_INVALID;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001121 i = iwl4965_rates[i].next_ieee) {
Zhu Yib481de92007-09-25 17:54:57 -07001122 if (rate_mask & (1 << i))
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001123 return iwl4965_rates[i].plcp;
Zhu Yib481de92007-09-25 17:54:57 -07001124 }
1125
1126 return IWL_RATE_INVALID;
1127}
1128
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001129static int iwl4965_send_beacon_cmd(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001130{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001131 struct iwl4965_frame *frame;
Zhu Yib481de92007-09-25 17:54:57 -07001132 unsigned int frame_size;
1133 int rc;
1134 u8 rate;
1135
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001136 frame = iwl4965_get_free_frame(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001137
1138 if (!frame) {
1139 IWL_ERROR("Could not obtain free frame buffer for beacon "
1140 "command.\n");
1141 return -ENOMEM;
1142 }
1143
1144 if (!(priv->staging_rxon.flags & RXON_FLG_BAND_24G_MSK)) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001145 rate = iwl4965_rate_get_lowest_plcp(priv->active_rate_basic &
Zhu Yib481de92007-09-25 17:54:57 -07001146 0xFF0);
1147 if (rate == IWL_INVALID_RATE)
1148 rate = IWL_RATE_6M_PLCP;
1149 } else {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001150 rate = iwl4965_rate_get_lowest_plcp(priv->active_rate_basic & 0xF);
Zhu Yib481de92007-09-25 17:54:57 -07001151 if (rate == IWL_INVALID_RATE)
1152 rate = IWL_RATE_1M_PLCP;
1153 }
1154
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001155 frame_size = iwl4965_hw_get_beacon_cmd(priv, frame, rate);
Zhu Yib481de92007-09-25 17:54:57 -07001156
Tomas Winkler857485c2008-03-21 13:53:44 -07001157 rc = iwl_send_cmd_pdu(priv, REPLY_TX_BEACON, frame_size,
Zhu Yib481de92007-09-25 17:54:57 -07001158 &frame->u.cmd[0]);
1159
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001160 iwl4965_free_frame(priv, frame);
Zhu Yib481de92007-09-25 17:54:57 -07001161
1162 return rc;
1163}
1164
1165/******************************************************************************
1166 *
Zhu Yib481de92007-09-25 17:54:57 -07001167 * Misc. internal state and helper functions
1168 *
1169 ******************************************************************************/
Zhu Yib481de92007-09-25 17:54:57 -07001170
Tomas Winkler5425e492008-04-15 16:01:38 -07001171static void iwl4965_unset_hw_params(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001172{
Tomas Winkler059ff822008-04-14 21:16:14 -07001173 if (priv->shared_virt)
Zhu Yib481de92007-09-25 17:54:57 -07001174 pci_free_consistent(priv->pci_dev,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001175 sizeof(struct iwl4965_shared),
Tomas Winkler059ff822008-04-14 21:16:14 -07001176 priv->shared_virt,
1177 priv->shared_phys);
Zhu Yib481de92007-09-25 17:54:57 -07001178}
1179
1180/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001181 * iwl4965_supported_rate_to_ie - fill in the supported rate in IE field
Zhu Yib481de92007-09-25 17:54:57 -07001182 *
1183 * return : set the bit for each supported rate insert in ie
1184 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001185static u16 iwl4965_supported_rate_to_ie(u8 *ie, u16 supported_rate,
Tomas Winklerc7c46672007-10-18 02:04:15 +02001186 u16 basic_rate, int *left)
Zhu Yib481de92007-09-25 17:54:57 -07001187{
1188 u16 ret_rates = 0, bit;
1189 int i;
Tomas Winklerc7c46672007-10-18 02:04:15 +02001190 u8 *cnt = ie;
1191 u8 *rates = ie + 1;
Zhu Yib481de92007-09-25 17:54:57 -07001192
1193 for (bit = 1, i = 0; i < IWL_RATE_COUNT; i++, bit <<= 1) {
1194 if (bit & supported_rate) {
1195 ret_rates |= bit;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001196 rates[*cnt] = iwl4965_rates[i].ieee |
Tomas Winklerc7c46672007-10-18 02:04:15 +02001197 ((bit & basic_rate) ? 0x80 : 0x00);
1198 (*cnt)++;
1199 (*left)--;
1200 if ((*left <= 0) ||
1201 (*cnt >= IWL_SUPPORTED_RATES_IE_LEN))
Zhu Yib481de92007-09-25 17:54:57 -07001202 break;
1203 }
1204 }
1205
1206 return ret_rates;
1207}
1208
Zhu Yib481de92007-09-25 17:54:57 -07001209/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001210 * iwl4965_fill_probe_req - fill in all required fields and IE for probe request
Zhu Yib481de92007-09-25 17:54:57 -07001211 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001212static u16 iwl4965_fill_probe_req(struct iwl_priv *priv,
Tomas Winkler78330fd2008-02-06 02:37:18 +02001213 enum ieee80211_band band,
1214 struct ieee80211_mgmt *frame,
1215 int left, int is_direct)
Zhu Yib481de92007-09-25 17:54:57 -07001216{
1217 int len = 0;
1218 u8 *pos = NULL;
mabbasbee488d2007-10-25 17:15:42 +08001219 u16 active_rates, ret_rates, cck_rates, active_rate_basic;
Ron Rindjunsky8fb880322007-11-26 16:14:38 +02001220#ifdef CONFIG_IWL4965_HT
Tomas Winkler78330fd2008-02-06 02:37:18 +02001221 const struct ieee80211_supported_band *sband =
1222 iwl4965_get_hw_mode(priv, band);
Ron Rindjunsky8fb880322007-11-26 16:14:38 +02001223#endif /* CONFIG_IWL4965_HT */
Zhu Yib481de92007-09-25 17:54:57 -07001224
1225 /* Make sure there is enough space for the probe request,
1226 * two mandatory IEs and the data */
1227 left -= 24;
1228 if (left < 0)
1229 return 0;
1230 len += 24;
1231
1232 frame->frame_control = cpu_to_le16(IEEE80211_STYPE_PROBE_REQ);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001233 memcpy(frame->da, iwl4965_broadcast_addr, ETH_ALEN);
Zhu Yib481de92007-09-25 17:54:57 -07001234 memcpy(frame->sa, priv->mac_addr, ETH_ALEN);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001235 memcpy(frame->bssid, iwl4965_broadcast_addr, ETH_ALEN);
Zhu Yib481de92007-09-25 17:54:57 -07001236 frame->seq_ctrl = 0;
1237
1238 /* fill in our indirect SSID IE */
1239 /* ...next IE... */
1240
1241 left -= 2;
1242 if (left < 0)
1243 return 0;
1244 len += 2;
1245 pos = &(frame->u.probe_req.variable[0]);
1246 *pos++ = WLAN_EID_SSID;
1247 *pos++ = 0;
1248
1249 /* fill in our direct SSID IE... */
1250 if (is_direct) {
1251 /* ...next IE... */
1252 left -= 2 + priv->essid_len;
1253 if (left < 0)
1254 return 0;
1255 /* ... fill it in... */
1256 *pos++ = WLAN_EID_SSID;
1257 *pos++ = priv->essid_len;
1258 memcpy(pos, priv->essid, priv->essid_len);
1259 pos += priv->essid_len;
1260 len += 2 + priv->essid_len;
1261 }
1262
1263 /* fill in supported rate */
1264 /* ...next IE... */
1265 left -= 2;
1266 if (left < 0)
1267 return 0;
Tomas Winklerc7c46672007-10-18 02:04:15 +02001268
Zhu Yib481de92007-09-25 17:54:57 -07001269 /* ... fill it in... */
1270 *pos++ = WLAN_EID_SUPP_RATES;
1271 *pos = 0;
Tomas Winklerc7c46672007-10-18 02:04:15 +02001272
mabbasbee488d2007-10-25 17:15:42 +08001273 /* exclude 60M rate */
1274 active_rates = priv->rates_mask;
1275 active_rates &= ~IWL_RATE_60M_MASK;
1276
1277 active_rate_basic = active_rates & IWL_BASIC_RATES_MASK;
Zhu Yib481de92007-09-25 17:54:57 -07001278
Tomas Winklerc7c46672007-10-18 02:04:15 +02001279 cck_rates = IWL_CCK_RATES_MASK & active_rates;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001280 ret_rates = iwl4965_supported_rate_to_ie(pos, cck_rates,
mabbasbee488d2007-10-25 17:15:42 +08001281 active_rate_basic, &left);
Tomas Winklerc7c46672007-10-18 02:04:15 +02001282 active_rates &= ~ret_rates;
1283
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001284 ret_rates = iwl4965_supported_rate_to_ie(pos, active_rates,
mabbasbee488d2007-10-25 17:15:42 +08001285 active_rate_basic, &left);
Tomas Winklerc7c46672007-10-18 02:04:15 +02001286 active_rates &= ~ret_rates;
1287
Zhu Yib481de92007-09-25 17:54:57 -07001288 len += 2 + *pos;
1289 pos += (*pos) + 1;
Tomas Winklerc7c46672007-10-18 02:04:15 +02001290 if (active_rates == 0)
Zhu Yib481de92007-09-25 17:54:57 -07001291 goto fill_end;
1292
1293 /* fill in supported extended rate */
1294 /* ...next IE... */
1295 left -= 2;
1296 if (left < 0)
1297 return 0;
1298 /* ... fill it in... */
1299 *pos++ = WLAN_EID_EXT_SUPP_RATES;
1300 *pos = 0;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001301 iwl4965_supported_rate_to_ie(pos, active_rates,
mabbasbee488d2007-10-25 17:15:42 +08001302 active_rate_basic, &left);
Zhu Yib481de92007-09-25 17:54:57 -07001303 if (*pos > 0)
1304 len += 2 + *pos;
1305
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08001306#ifdef CONFIG_IWL4965_HT
Tomas Winkler78330fd2008-02-06 02:37:18 +02001307 if (sband && sband->ht_info.ht_supported) {
1308 struct ieee80211_ht_cap *ht_cap;
Zhu Yib481de92007-09-25 17:54:57 -07001309 pos += (*pos) + 1;
1310 *pos++ = WLAN_EID_HT_CAPABILITY;
Ron Rindjunsky8fb880322007-11-26 16:14:38 +02001311 *pos++ = sizeof(struct ieee80211_ht_cap);
Tomas Winkler78330fd2008-02-06 02:37:18 +02001312 ht_cap = (struct ieee80211_ht_cap *)pos;
1313 ht_cap->cap_info = cpu_to_le16(sband->ht_info.cap);
1314 memcpy(ht_cap->supp_mcs_set, sband->ht_info.supp_mcs_set, 16);
1315 ht_cap->ampdu_params_info =(sband->ht_info.ampdu_factor &
1316 IEEE80211_HT_CAP_AMPDU_FACTOR) |
1317 ((sband->ht_info.ampdu_density << 2) &
1318 IEEE80211_HT_CAP_AMPDU_DENSITY);
Ron Rindjunsky8fb880322007-11-26 16:14:38 +02001319 len += 2 + sizeof(struct ieee80211_ht_cap);
Zhu Yib481de92007-09-25 17:54:57 -07001320 }
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08001321#endif /*CONFIG_IWL4965_HT */
Zhu Yib481de92007-09-25 17:54:57 -07001322
1323 fill_end:
1324 return (u16)len;
1325}
1326
1327/*
1328 * QoS support
1329*/
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001330static int iwl4965_send_qos_params_command(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001331 struct iwl4965_qosparam_cmd *qos)
Zhu Yib481de92007-09-25 17:54:57 -07001332{
1333
Tomas Winkler857485c2008-03-21 13:53:44 -07001334 return iwl_send_cmd_pdu(priv, REPLY_QOS_PARAM,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001335 sizeof(struct iwl4965_qosparam_cmd), qos);
Zhu Yib481de92007-09-25 17:54:57 -07001336}
1337
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001338static void iwl4965_activate_qos(struct iwl_priv *priv, u8 force)
Zhu Yib481de92007-09-25 17:54:57 -07001339{
1340 unsigned long flags;
1341
Zhu Yib481de92007-09-25 17:54:57 -07001342 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
1343 return;
1344
1345 if (!priv->qos_data.qos_enable)
1346 return;
1347
1348 spin_lock_irqsave(&priv->lock, flags);
1349 priv->qos_data.def_qos_parm.qos_flags = 0;
1350
1351 if (priv->qos_data.qos_cap.q_AP.queue_request &&
1352 !priv->qos_data.qos_cap.q_AP.txop_request)
1353 priv->qos_data.def_qos_parm.qos_flags |=
1354 QOS_PARAM_FLG_TXOP_TYPE_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07001355 if (priv->qos_data.qos_active)
1356 priv->qos_data.def_qos_parm.qos_flags |=
1357 QOS_PARAM_FLG_UPDATE_EDCA_MSK;
1358
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08001359#ifdef CONFIG_IWL4965_HT
Ron Rindjunskyfd105e72007-11-26 16:14:39 +02001360 if (priv->current_ht_config.is_ht)
Tomas Winklerf1f1f5c2007-10-25 17:15:26 +08001361 priv->qos_data.def_qos_parm.qos_flags |= QOS_PARAM_FLG_TGN_MSK;
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08001362#endif /* CONFIG_IWL4965_HT */
Tomas Winklerf1f1f5c2007-10-25 17:15:26 +08001363
Zhu Yib481de92007-09-25 17:54:57 -07001364 spin_unlock_irqrestore(&priv->lock, flags);
1365
Tomas Winkler3109ece2008-03-28 16:33:35 -07001366 if (force || iwl_is_associated(priv)) {
Tomas Winklerf1f1f5c2007-10-25 17:15:26 +08001367 IWL_DEBUG_QOS("send QoS cmd with Qos active=%d FLAGS=0x%X\n",
1368 priv->qos_data.qos_active,
1369 priv->qos_data.def_qos_parm.qos_flags);
Zhu Yib481de92007-09-25 17:54:57 -07001370
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001371 iwl4965_send_qos_params_command(priv,
Zhu Yib481de92007-09-25 17:54:57 -07001372 &(priv->qos_data.def_qos_parm));
1373 }
1374}
1375
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001376int iwl4965_is_network_packet(struct iwl_priv *priv, struct ieee80211_hdr *header)
Zhu Yib481de92007-09-25 17:54:57 -07001377{
1378 /* Filter incoming packets to determine if they are targeted toward
1379 * this network, discarding packets coming from ourselves */
1380 switch (priv->iw_mode) {
1381 case IEEE80211_IF_TYPE_IBSS: /* Header: Dest. | Source | BSSID */
1382 /* packets from our adapter are dropped (echo) */
1383 if (!compare_ether_addr(header->addr2, priv->mac_addr))
1384 return 0;
1385 /* {broad,multi}cast packets to our IBSS go through */
1386 if (is_multicast_ether_addr(header->addr1))
1387 return !compare_ether_addr(header->addr3, priv->bssid);
1388 /* packets to our adapter go through */
1389 return !compare_ether_addr(header->addr1, priv->mac_addr);
1390 case IEEE80211_IF_TYPE_STA: /* Header: Dest. | AP{BSSID} | Source */
1391 /* packets from our adapter are dropped (echo) */
1392 if (!compare_ether_addr(header->addr3, priv->mac_addr))
1393 return 0;
1394 /* {broad,multi}cast packets to our BSS go through */
1395 if (is_multicast_ether_addr(header->addr1))
1396 return !compare_ether_addr(header->addr2, priv->bssid);
1397 /* packets to our adapter go through */
1398 return !compare_ether_addr(header->addr1, priv->mac_addr);
Tomas Winkler69dc5d92008-03-25 16:33:41 -07001399 default:
1400 break;
Zhu Yib481de92007-09-25 17:54:57 -07001401 }
1402
1403 return 1;
1404}
1405
1406#define TX_STATUS_ENTRY(x) case TX_STATUS_FAIL_ ## x: return #x
1407
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001408static const char *iwl4965_get_tx_fail_reason(u32 status)
Zhu Yib481de92007-09-25 17:54:57 -07001409{
1410 switch (status & TX_STATUS_MSK) {
1411 case TX_STATUS_SUCCESS:
1412 return "SUCCESS";
1413 TX_STATUS_ENTRY(SHORT_LIMIT);
1414 TX_STATUS_ENTRY(LONG_LIMIT);
1415 TX_STATUS_ENTRY(FIFO_UNDERRUN);
1416 TX_STATUS_ENTRY(MGMNT_ABORT);
1417 TX_STATUS_ENTRY(NEXT_FRAG);
1418 TX_STATUS_ENTRY(LIFE_EXPIRE);
1419 TX_STATUS_ENTRY(DEST_PS);
1420 TX_STATUS_ENTRY(ABORTED);
1421 TX_STATUS_ENTRY(BT_RETRY);
1422 TX_STATUS_ENTRY(STA_INVALID);
1423 TX_STATUS_ENTRY(FRAG_DROPPED);
1424 TX_STATUS_ENTRY(TID_DISABLE);
1425 TX_STATUS_ENTRY(FRAME_FLUSHED);
1426 TX_STATUS_ENTRY(INSUFFICIENT_CF_POLL);
1427 TX_STATUS_ENTRY(TX_LOCKED);
1428 TX_STATUS_ENTRY(NO_BEACON_ON_RADAR);
1429 }
1430
1431 return "UNKNOWN";
1432}
1433
1434/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001435 * iwl4965_scan_cancel - Cancel any currently executing HW scan
Zhu Yib481de92007-09-25 17:54:57 -07001436 *
1437 * NOTE: priv->mutex is not required before calling this function
1438 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001439static int iwl4965_scan_cancel(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001440{
1441 if (!test_bit(STATUS_SCAN_HW, &priv->status)) {
1442 clear_bit(STATUS_SCANNING, &priv->status);
1443 return 0;
1444 }
1445
1446 if (test_bit(STATUS_SCANNING, &priv->status)) {
1447 if (!test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
1448 IWL_DEBUG_SCAN("Queuing scan abort.\n");
1449 set_bit(STATUS_SCAN_ABORTING, &priv->status);
1450 queue_work(priv->workqueue, &priv->abort_scan);
1451
1452 } else
1453 IWL_DEBUG_SCAN("Scan abort already in progress.\n");
1454
1455 return test_bit(STATUS_SCANNING, &priv->status);
1456 }
1457
1458 return 0;
1459}
1460
1461/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001462 * iwl4965_scan_cancel_timeout - Cancel any currently executing HW scan
Zhu Yib481de92007-09-25 17:54:57 -07001463 * @ms: amount of time to wait (in milliseconds) for scan to abort
1464 *
1465 * NOTE: priv->mutex must be held before calling this function
1466 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001467static int iwl4965_scan_cancel_timeout(struct iwl_priv *priv, unsigned long ms)
Zhu Yib481de92007-09-25 17:54:57 -07001468{
1469 unsigned long now = jiffies;
1470 int ret;
1471
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001472 ret = iwl4965_scan_cancel(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001473 if (ret && ms) {
1474 mutex_unlock(&priv->mutex);
1475 while (!time_after(jiffies, now + msecs_to_jiffies(ms)) &&
1476 test_bit(STATUS_SCANNING, &priv->status))
1477 msleep(1);
1478 mutex_lock(&priv->mutex);
1479
1480 return test_bit(STATUS_SCANNING, &priv->status);
1481 }
1482
1483 return ret;
1484}
1485
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001486static void iwl4965_sequence_reset(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001487{
1488 /* Reset ieee stats */
1489
1490 /* We don't reset the net_device_stats (ieee->stats) on
1491 * re-association */
1492
1493 priv->last_seq_num = -1;
1494 priv->last_frag_num = -1;
1495 priv->last_packet_time = 0;
1496
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001497 iwl4965_scan_cancel(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001498}
1499
1500#define MAX_UCODE_BEACON_INTERVAL 4096
1501#define INTEL_CONN_LISTEN_INTERVAL __constant_cpu_to_le16(0xA)
1502
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001503static __le16 iwl4965_adjust_beacon_interval(u16 beacon_val)
Zhu Yib481de92007-09-25 17:54:57 -07001504{
1505 u16 new_val = 0;
1506 u16 beacon_factor = 0;
1507
1508 beacon_factor =
1509 (beacon_val + MAX_UCODE_BEACON_INTERVAL)
1510 / MAX_UCODE_BEACON_INTERVAL;
1511 new_val = beacon_val / beacon_factor;
1512
1513 return cpu_to_le16(new_val);
1514}
1515
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001516static void iwl4965_setup_rxon_timing(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001517{
1518 u64 interval_tm_unit;
1519 u64 tsf, result;
1520 unsigned long flags;
1521 struct ieee80211_conf *conf = NULL;
1522 u16 beacon_int = 0;
1523
1524 conf = ieee80211_get_hw_conf(priv->hw);
1525
1526 spin_lock_irqsave(&priv->lock, flags);
Tomas Winkler3109ece2008-03-28 16:33:35 -07001527 priv->rxon_timing.timestamp.dw[1] = cpu_to_le32(priv->timestamp >> 32);
1528 priv->rxon_timing.timestamp.dw[0] =
1529 cpu_to_le32(priv->timestamp & 0xFFFFFFFF);
Zhu Yib481de92007-09-25 17:54:57 -07001530
1531 priv->rxon_timing.listen_interval = INTEL_CONN_LISTEN_INTERVAL;
1532
Tomas Winkler3109ece2008-03-28 16:33:35 -07001533 tsf = priv->timestamp;
Zhu Yib481de92007-09-25 17:54:57 -07001534
1535 beacon_int = priv->beacon_int;
1536 spin_unlock_irqrestore(&priv->lock, flags);
1537
1538 if (priv->iw_mode == IEEE80211_IF_TYPE_STA) {
1539 if (beacon_int == 0) {
1540 priv->rxon_timing.beacon_interval = cpu_to_le16(100);
1541 priv->rxon_timing.beacon_init_val = cpu_to_le32(102400);
1542 } else {
1543 priv->rxon_timing.beacon_interval =
1544 cpu_to_le16(beacon_int);
1545 priv->rxon_timing.beacon_interval =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001546 iwl4965_adjust_beacon_interval(
Zhu Yib481de92007-09-25 17:54:57 -07001547 le16_to_cpu(priv->rxon_timing.beacon_interval));
1548 }
1549
1550 priv->rxon_timing.atim_window = 0;
1551 } else {
1552 priv->rxon_timing.beacon_interval =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001553 iwl4965_adjust_beacon_interval(conf->beacon_int);
Zhu Yib481de92007-09-25 17:54:57 -07001554 /* TODO: we need to get atim_window from upper stack
1555 * for now we set to 0 */
1556 priv->rxon_timing.atim_window = 0;
1557 }
1558
1559 interval_tm_unit =
1560 (le16_to_cpu(priv->rxon_timing.beacon_interval) * 1024);
1561 result = do_div(tsf, interval_tm_unit);
1562 priv->rxon_timing.beacon_init_val =
1563 cpu_to_le32((u32) ((u64) interval_tm_unit - result));
1564
1565 IWL_DEBUG_ASSOC
1566 ("beacon interval %d beacon timer %d beacon tim %d\n",
1567 le16_to_cpu(priv->rxon_timing.beacon_interval),
1568 le32_to_cpu(priv->rxon_timing.beacon_init_val),
1569 le16_to_cpu(priv->rxon_timing.atim_window));
1570}
1571
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001572static int iwl4965_scan_initiate(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001573{
1574 if (priv->iw_mode == IEEE80211_IF_TYPE_AP) {
1575 IWL_ERROR("APs don't scan.\n");
1576 return 0;
1577 }
1578
Tomas Winklerfee12472008-04-03 16:05:21 -07001579 if (!iwl_is_ready_rf(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07001580 IWL_DEBUG_SCAN("Aborting scan due to not ready.\n");
1581 return -EIO;
1582 }
1583
1584 if (test_bit(STATUS_SCANNING, &priv->status)) {
1585 IWL_DEBUG_SCAN("Scan already in progress.\n");
1586 return -EAGAIN;
1587 }
1588
1589 if (test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
1590 IWL_DEBUG_SCAN("Scan request while abort pending. "
1591 "Queuing.\n");
1592 return -EAGAIN;
1593 }
1594
1595 IWL_DEBUG_INFO("Starting scan...\n");
1596 priv->scan_bands = 2;
1597 set_bit(STATUS_SCANNING, &priv->status);
1598 priv->scan_start = jiffies;
1599 priv->scan_pass_start = priv->scan_start;
1600
1601 queue_work(priv->workqueue, &priv->request_scan);
1602
1603 return 0;
1604}
1605
Zhu Yib481de92007-09-25 17:54:57 -07001606
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001607static void iwl4965_set_flags_for_phymode(struct iwl_priv *priv,
Johannes Berg8318d782008-01-24 19:38:38 +01001608 enum ieee80211_band band)
Zhu Yib481de92007-09-25 17:54:57 -07001609{
Johannes Berg8318d782008-01-24 19:38:38 +01001610 if (band == IEEE80211_BAND_5GHZ) {
Zhu Yib481de92007-09-25 17:54:57 -07001611 priv->staging_rxon.flags &=
1612 ~(RXON_FLG_BAND_24G_MSK | RXON_FLG_AUTO_DETECT_MSK
1613 | RXON_FLG_CCK_MSK);
1614 priv->staging_rxon.flags |= RXON_FLG_SHORT_SLOT_MSK;
1615 } else {
Reinette Chatre508e32e2008-04-14 21:16:13 -07001616 /* Copied from iwl4965_post_associate() */
Zhu Yib481de92007-09-25 17:54:57 -07001617 if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_SLOT_TIME)
1618 priv->staging_rxon.flags |= RXON_FLG_SHORT_SLOT_MSK;
1619 else
1620 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
1621
1622 if (priv->iw_mode == IEEE80211_IF_TYPE_IBSS)
1623 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
1624
1625 priv->staging_rxon.flags |= RXON_FLG_BAND_24G_MSK;
1626 priv->staging_rxon.flags |= RXON_FLG_AUTO_DETECT_MSK;
1627 priv->staging_rxon.flags &= ~RXON_FLG_CCK_MSK;
1628 }
1629}
1630
1631/*
Ian Schram01ebd062007-10-25 17:15:22 +08001632 * initialize rxon structure with default values from eeprom
Zhu Yib481de92007-09-25 17:54:57 -07001633 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001634static void iwl4965_connection_init_rx_config(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001635{
Assaf Kraussbf85ea42008-03-14 10:38:49 -07001636 const struct iwl_channel_info *ch_info;
Zhu Yib481de92007-09-25 17:54:57 -07001637
1638 memset(&priv->staging_rxon, 0, sizeof(priv->staging_rxon));
1639
1640 switch (priv->iw_mode) {
1641 case IEEE80211_IF_TYPE_AP:
1642 priv->staging_rxon.dev_type = RXON_DEV_TYPE_AP;
1643 break;
1644
1645 case IEEE80211_IF_TYPE_STA:
1646 priv->staging_rxon.dev_type = RXON_DEV_TYPE_ESS;
1647 priv->staging_rxon.filter_flags = RXON_FILTER_ACCEPT_GRP_MSK;
1648 break;
1649
1650 case IEEE80211_IF_TYPE_IBSS:
1651 priv->staging_rxon.dev_type = RXON_DEV_TYPE_IBSS;
1652 priv->staging_rxon.flags = RXON_FLG_SHORT_PREAMBLE_MSK;
1653 priv->staging_rxon.filter_flags = RXON_FILTER_BCON_AWARE_MSK |
1654 RXON_FILTER_ACCEPT_GRP_MSK;
1655 break;
1656
1657 case IEEE80211_IF_TYPE_MNTR:
1658 priv->staging_rxon.dev_type = RXON_DEV_TYPE_SNIFFER;
1659 priv->staging_rxon.filter_flags = RXON_FILTER_PROMISC_MSK |
1660 RXON_FILTER_CTL2HOST_MSK | RXON_FILTER_ACCEPT_GRP_MSK;
1661 break;
Tomas Winkler69dc5d92008-03-25 16:33:41 -07001662 default:
1663 IWL_ERROR("Unsupported interface type %d\n", priv->iw_mode);
1664 break;
Zhu Yib481de92007-09-25 17:54:57 -07001665 }
1666
1667#if 0
1668 /* TODO: Figure out when short_preamble would be set and cache from
1669 * that */
1670 if (!hw_to_local(priv->hw)->short_preamble)
1671 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_PREAMBLE_MSK;
1672 else
1673 priv->staging_rxon.flags |= RXON_FLG_SHORT_PREAMBLE_MSK;
1674#endif
1675
Assaf Krauss8622e702008-03-21 13:53:43 -07001676 ch_info = iwl_get_channel_info(priv, priv->band,
Zhu Yib481de92007-09-25 17:54:57 -07001677 le16_to_cpu(priv->staging_rxon.channel));
1678
1679 if (!ch_info)
1680 ch_info = &priv->channel_info[0];
1681
1682 /*
1683 * in some case A channels are all non IBSS
1684 * in this case force B/G channel
1685 */
1686 if ((priv->iw_mode == IEEE80211_IF_TYPE_IBSS) &&
1687 !(is_channel_ibss(ch_info)))
1688 ch_info = &priv->channel_info[0];
1689
1690 priv->staging_rxon.channel = cpu_to_le16(ch_info->channel);
Johannes Berg8318d782008-01-24 19:38:38 +01001691 priv->band = ch_info->band;
Zhu Yib481de92007-09-25 17:54:57 -07001692
Johannes Berg8318d782008-01-24 19:38:38 +01001693 iwl4965_set_flags_for_phymode(priv, priv->band);
Zhu Yib481de92007-09-25 17:54:57 -07001694
1695 priv->staging_rxon.ofdm_basic_rates =
1696 (IWL_OFDM_RATES_MASK >> IWL_FIRST_OFDM_RATE) & 0xFF;
1697 priv->staging_rxon.cck_basic_rates =
1698 (IWL_CCK_RATES_MASK >> IWL_FIRST_CCK_RATE) & 0xF;
1699
1700 priv->staging_rxon.flags &= ~(RXON_FLG_CHANNEL_MODE_MIXED_MSK |
1701 RXON_FLG_CHANNEL_MODE_PURE_40_MSK);
1702 memcpy(priv->staging_rxon.node_addr, priv->mac_addr, ETH_ALEN);
1703 memcpy(priv->staging_rxon.wlap_bssid_addr, priv->mac_addr, ETH_ALEN);
1704 priv->staging_rxon.ofdm_ht_single_stream_basic_rates = 0xff;
1705 priv->staging_rxon.ofdm_ht_dual_stream_basic_rates = 0xff;
1706 iwl4965_set_rxon_chain(priv);
1707}
1708
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001709static int iwl4965_set_mode(struct iwl_priv *priv, int mode)
Zhu Yib481de92007-09-25 17:54:57 -07001710{
Zhu Yib481de92007-09-25 17:54:57 -07001711 if (mode == IEEE80211_IF_TYPE_IBSS) {
Assaf Kraussbf85ea42008-03-14 10:38:49 -07001712 const struct iwl_channel_info *ch_info;
Zhu Yib481de92007-09-25 17:54:57 -07001713
Assaf Krauss8622e702008-03-21 13:53:43 -07001714 ch_info = iwl_get_channel_info(priv,
Johannes Berg8318d782008-01-24 19:38:38 +01001715 priv->band,
Zhu Yib481de92007-09-25 17:54:57 -07001716 le16_to_cpu(priv->staging_rxon.channel));
1717
1718 if (!ch_info || !is_channel_ibss(ch_info)) {
1719 IWL_ERROR("channel %d not IBSS channel\n",
1720 le16_to_cpu(priv->staging_rxon.channel));
1721 return -EINVAL;
1722 }
1723 }
1724
Zhu Yib481de92007-09-25 17:54:57 -07001725 priv->iw_mode = mode;
1726
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001727 iwl4965_connection_init_rx_config(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001728 memcpy(priv->staging_rxon.node_addr, priv->mac_addr, ETH_ALEN);
1729
Assaf Kraussbf85ea42008-03-14 10:38:49 -07001730 iwlcore_clear_stations_table(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001731
Mohamed Abbasfde35712007-11-29 11:10:15 +08001732 /* dont commit rxon if rf-kill is on*/
Tomas Winklerfee12472008-04-03 16:05:21 -07001733 if (!iwl_is_ready_rf(priv))
Mohamed Abbasfde35712007-11-29 11:10:15 +08001734 return -EAGAIN;
1735
1736 cancel_delayed_work(&priv->scan_check);
1737 if (iwl4965_scan_cancel_timeout(priv, 100)) {
1738 IWL_WARNING("Aborted scan still in progress after 100ms\n");
1739 IWL_DEBUG_MAC80211("leaving - scan abort failed.\n");
1740 return -EAGAIN;
1741 }
1742
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001743 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001744
1745 return 0;
1746}
1747
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001748static void iwl4965_build_tx_cmd_hwcrypto(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07001749 struct ieee80211_tx_control *ctl,
Tomas Winkler857485c2008-03-21 13:53:44 -07001750 struct iwl_cmd *cmd,
Zhu Yib481de92007-09-25 17:54:57 -07001751 struct sk_buff *skb_frag,
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07001752 int sta_id)
Zhu Yib481de92007-09-25 17:54:57 -07001753{
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07001754 struct iwl4965_hw_key *keyinfo = &priv->stations[sta_id].keyinfo;
Emmanuel Grumbach6974e362008-04-14 21:16:06 -07001755 struct iwl_wep_key *wepkey;
1756 int keyidx = 0;
1757
Ivo van Doorn1c014422008-04-17 19:41:02 +02001758 BUG_ON(ctl->hw_key->hw_key_idx > 3);
Zhu Yib481de92007-09-25 17:54:57 -07001759
1760 switch (keyinfo->alg) {
1761 case ALG_CCMP:
1762 cmd->cmd.tx.sec_ctl = TX_CMD_SEC_CCM;
1763 memcpy(cmd->cmd.tx.key, keyinfo->key, keyinfo->keylen);
Max Stepanov8236e182008-03-12 16:58:48 -07001764 if (ctl->flags & IEEE80211_TXCTL_AMPDU)
1765 cmd->cmd.tx.tx_flags |= TX_CMD_FLG_AGG_CCMP_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07001766 IWL_DEBUG_TX("tx_cmd with aes hwcrypto\n");
1767 break;
1768
1769 case ALG_TKIP:
Zhu Yib481de92007-09-25 17:54:57 -07001770 cmd->cmd.tx.sec_ctl = TX_CMD_SEC_TKIP;
Emmanuel Grumbach2bc75082008-03-19 16:41:45 -07001771 ieee80211_get_tkip_key(keyinfo->conf, skb_frag,
1772 IEEE80211_TKIP_P2_KEY, cmd->cmd.tx.key);
1773 IWL_DEBUG_TX("tx_cmd with tkip hwcrypto\n");
Zhu Yib481de92007-09-25 17:54:57 -07001774 break;
1775
1776 case ALG_WEP:
Ivo van Doorn1c014422008-04-17 19:41:02 +02001777 wepkey = &priv->wep_keys[ctl->hw_key->hw_key_idx];
Emmanuel Grumbach6974e362008-04-14 21:16:06 -07001778 cmd->cmd.tx.sec_ctl = 0;
1779 if (priv->default_wep_key) {
1780 /* the WEP key was sent as static */
Ivo van Doorn1c014422008-04-17 19:41:02 +02001781 keyidx = ctl->hw_key->hw_key_idx;
Emmanuel Grumbach6974e362008-04-14 21:16:06 -07001782 memcpy(&cmd->cmd.tx.key[3], wepkey->key,
1783 wepkey->key_size);
1784 if (wepkey->key_size == WEP_KEY_LEN_128)
1785 cmd->cmd.tx.sec_ctl |= TX_CMD_SEC_KEY128;
1786 } else {
Emmanuel Grumbach0211ddd2008-04-14 21:16:07 -07001787 /* the WEP key was sent as dynamic */
1788 keyidx = keyinfo->keyidx;
1789 memcpy(&cmd->cmd.tx.key[3], keyinfo->key,
1790 keyinfo->keylen);
1791 if (keyinfo->keylen == WEP_KEY_LEN_128)
1792 cmd->cmd.tx.sec_ctl |= TX_CMD_SEC_KEY128;
Emmanuel Grumbach6974e362008-04-14 21:16:06 -07001793 }
Zhu Yib481de92007-09-25 17:54:57 -07001794
Emmanuel Grumbach6974e362008-04-14 21:16:06 -07001795 cmd->cmd.tx.sec_ctl |= (TX_CMD_SEC_WEP |
1796 (keyidx & TX_CMD_SEC_MSK) << TX_CMD_SEC_SHIFT);
Zhu Yib481de92007-09-25 17:54:57 -07001797
1798 IWL_DEBUG_TX("Configuring packet for WEP encryption "
Emmanuel Grumbach6974e362008-04-14 21:16:06 -07001799 "with key %d\n", keyidx);
Zhu Yib481de92007-09-25 17:54:57 -07001800 break;
1801
Zhu Yib481de92007-09-25 17:54:57 -07001802 default:
1803 printk(KERN_ERR "Unknown encode alg %d\n", keyinfo->alg);
1804 break;
1805 }
1806}
1807
1808/*
1809 * handle build REPLY_TX command notification.
1810 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001811static void iwl4965_build_tx_cmd_basic(struct iwl_priv *priv,
Tomas Winkler857485c2008-03-21 13:53:44 -07001812 struct iwl_cmd *cmd,
Zhu Yib481de92007-09-25 17:54:57 -07001813 struct ieee80211_tx_control *ctrl,
1814 struct ieee80211_hdr *hdr,
1815 int is_unicast, u8 std_id)
1816{
1817 __le16 *qc;
1818 u16 fc = le16_to_cpu(hdr->frame_control);
1819 __le32 tx_flags = cmd->cmd.tx.tx_flags;
1820
1821 cmd->cmd.tx.stop_time.life_time = TX_CMD_LIFE_TIME_INFINITE;
1822 if (!(ctrl->flags & IEEE80211_TXCTL_NO_ACK)) {
1823 tx_flags |= TX_CMD_FLG_ACK_MSK;
1824 if ((fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_MGMT)
1825 tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
1826 if (ieee80211_is_probe_response(fc) &&
1827 !(le16_to_cpu(hdr->seq_ctrl) & 0xf))
1828 tx_flags |= TX_CMD_FLG_TSF_MSK;
1829 } else {
1830 tx_flags &= (~TX_CMD_FLG_ACK_MSK);
1831 tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
1832 }
1833
Tomas Winkler87e4f7d2008-01-14 17:46:16 -08001834 if (ieee80211_is_back_request(fc))
1835 tx_flags |= TX_CMD_FLG_ACK_MSK | TX_CMD_FLG_IMM_BA_RSP_MASK;
1836
1837
Zhu Yib481de92007-09-25 17:54:57 -07001838 cmd->cmd.tx.sta_id = std_id;
1839 if (ieee80211_get_morefrag(hdr))
1840 tx_flags |= TX_CMD_FLG_MORE_FRAG_MSK;
1841
1842 qc = ieee80211_get_qos_ctrl(hdr);
1843 if (qc) {
1844 cmd->cmd.tx.tid_tspec = (u8) (le16_to_cpu(*qc) & 0xf);
1845 tx_flags &= ~TX_CMD_FLG_SEQ_CTL_MSK;
1846 } else
1847 tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
1848
1849 if (ctrl->flags & IEEE80211_TXCTL_USE_RTS_CTS) {
1850 tx_flags |= TX_CMD_FLG_RTS_MSK;
1851 tx_flags &= ~TX_CMD_FLG_CTS_MSK;
1852 } else if (ctrl->flags & IEEE80211_TXCTL_USE_CTS_PROTECT) {
1853 tx_flags &= ~TX_CMD_FLG_RTS_MSK;
1854 tx_flags |= TX_CMD_FLG_CTS_MSK;
1855 }
1856
1857 if ((tx_flags & TX_CMD_FLG_RTS_MSK) || (tx_flags & TX_CMD_FLG_CTS_MSK))
1858 tx_flags |= TX_CMD_FLG_FULL_TXOP_PROT_MSK;
1859
1860 tx_flags &= ~(TX_CMD_FLG_ANT_SEL_MSK);
1861 if ((fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_MGMT) {
1862 if ((fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_ASSOC_REQ ||
1863 (fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_REASSOC_REQ)
Ian Schrambc434dd2007-10-25 17:15:29 +08001864 cmd->cmd.tx.timeout.pm_frame_timeout = cpu_to_le16(3);
Zhu Yib481de92007-09-25 17:54:57 -07001865 else
Ian Schrambc434dd2007-10-25 17:15:29 +08001866 cmd->cmd.tx.timeout.pm_frame_timeout = cpu_to_le16(2);
Mohamed Abbasab53d8a2008-03-25 16:33:36 -07001867 } else {
Zhu Yib481de92007-09-25 17:54:57 -07001868 cmd->cmd.tx.timeout.pm_frame_timeout = 0;
Mohamed Abbasab53d8a2008-03-25 16:33:36 -07001869 }
Zhu Yib481de92007-09-25 17:54:57 -07001870
1871 cmd->cmd.tx.driver_txop = 0;
1872 cmd->cmd.tx.tx_flags = tx_flags;
1873 cmd->cmd.tx.next_frame_len = 0;
1874}
Tomas Winkler19758be2008-03-12 16:58:51 -07001875static void iwl_update_tx_stats(struct iwl_priv *priv, u16 fc, u16 len)
1876{
1877 /* 0 - mgmt, 1 - cnt, 2 - data */
1878 int idx = (fc & IEEE80211_FCTL_FTYPE) >> 2;
1879 priv->tx_stats[idx].cnt++;
1880 priv->tx_stats[idx].bytes += len;
1881}
Cahill, Ben M6440adb2007-11-29 11:09:55 +08001882/**
1883 * iwl4965_get_sta_id - Find station's index within station table
1884 *
1885 * If new IBSS station, create new entry in station table
1886 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001887static int iwl4965_get_sta_id(struct iwl_priv *priv,
Ben Cahill9fbab512007-11-29 11:09:47 +08001888 struct ieee80211_hdr *hdr)
Zhu Yib481de92007-09-25 17:54:57 -07001889{
1890 int sta_id;
1891 u16 fc = le16_to_cpu(hdr->frame_control);
Joe Perches0795af52007-10-03 17:59:30 -07001892 DECLARE_MAC_BUF(mac);
Zhu Yib481de92007-09-25 17:54:57 -07001893
Cahill, Ben M6440adb2007-11-29 11:09:55 +08001894 /* If this frame is broadcast or management, use broadcast station id */
Zhu Yib481de92007-09-25 17:54:57 -07001895 if (((fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_DATA) ||
1896 is_multicast_ether_addr(hdr->addr1))
Tomas Winkler5425e492008-04-15 16:01:38 -07001897 return priv->hw_params.bcast_sta_id;
Zhu Yib481de92007-09-25 17:54:57 -07001898
1899 switch (priv->iw_mode) {
1900
Cahill, Ben M6440adb2007-11-29 11:09:55 +08001901 /* If we are a client station in a BSS network, use the special
1902 * AP station entry (that's the only station we communicate with) */
Zhu Yib481de92007-09-25 17:54:57 -07001903 case IEEE80211_IF_TYPE_STA:
1904 return IWL_AP_ID;
1905
1906 /* If we are an AP, then find the station, or use BCAST */
1907 case IEEE80211_IF_TYPE_AP:
Tomas Winkler947b13a2008-04-16 16:34:48 -07001908 sta_id = iwl_find_station(priv, hdr->addr1);
Zhu Yib481de92007-09-25 17:54:57 -07001909 if (sta_id != IWL_INVALID_STATION)
1910 return sta_id;
Tomas Winkler5425e492008-04-15 16:01:38 -07001911 return priv->hw_params.bcast_sta_id;
Zhu Yib481de92007-09-25 17:54:57 -07001912
Cahill, Ben M6440adb2007-11-29 11:09:55 +08001913 /* If this frame is going out to an IBSS network, find the station,
1914 * or create a new station table entry */
Zhu Yib481de92007-09-25 17:54:57 -07001915 case IEEE80211_IF_TYPE_IBSS:
Tomas Winkler947b13a2008-04-16 16:34:48 -07001916 sta_id = iwl_find_station(priv, hdr->addr1);
Zhu Yib481de92007-09-25 17:54:57 -07001917 if (sta_id != IWL_INVALID_STATION)
1918 return sta_id;
1919
Cahill, Ben M6440adb2007-11-29 11:09:55 +08001920 /* Create new station table entry */
Ron Rindjunsky67d62032007-11-26 16:14:40 +02001921 sta_id = iwl4965_add_station_flags(priv, hdr->addr1,
1922 0, CMD_ASYNC, NULL);
Zhu Yib481de92007-09-25 17:54:57 -07001923
1924 if (sta_id != IWL_INVALID_STATION)
1925 return sta_id;
1926
Joe Perches0795af52007-10-03 17:59:30 -07001927 IWL_DEBUG_DROP("Station %s not in station map. "
Zhu Yib481de92007-09-25 17:54:57 -07001928 "Defaulting to broadcast...\n",
Joe Perches0795af52007-10-03 17:59:30 -07001929 print_mac(mac, hdr->addr1));
Tomas Winkler0a6857e2008-03-12 16:58:49 -07001930 iwl_print_hex_dump(IWL_DL_DROP, (u8 *) hdr, sizeof(*hdr));
Tomas Winkler5425e492008-04-15 16:01:38 -07001931 return priv->hw_params.bcast_sta_id;
Zhu Yib481de92007-09-25 17:54:57 -07001932
1933 default:
Ian Schram01ebd062007-10-25 17:15:22 +08001934 IWL_WARNING("Unknown mode of operation: %d", priv->iw_mode);
Tomas Winkler5425e492008-04-15 16:01:38 -07001935 return priv->hw_params.bcast_sta_id;
Zhu Yib481de92007-09-25 17:54:57 -07001936 }
1937}
1938
1939/*
1940 * start REPLY_TX command process
1941 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001942static int iwl4965_tx_skb(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07001943 struct sk_buff *skb, struct ieee80211_tx_control *ctl)
1944{
1945 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001946 struct iwl4965_tfd_frame *tfd;
Zhu Yib481de92007-09-25 17:54:57 -07001947 u32 *control_flags;
1948 int txq_id = ctl->queue;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001949 struct iwl4965_tx_queue *txq = NULL;
1950 struct iwl4965_queue *q = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07001951 dma_addr_t phys_addr;
1952 dma_addr_t txcmd_phys;
Tomas Winkler87e4f7d2008-01-14 17:46:16 -08001953 dma_addr_t scratch_phys;
Tomas Winkler857485c2008-03-21 13:53:44 -07001954 struct iwl_cmd *out_cmd = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07001955 u16 len, idx, len_org;
1956 u8 id, hdr_len, unicast;
1957 u8 sta_id;
1958 u16 seq_number = 0;
1959 u16 fc;
1960 __le16 *qc;
1961 u8 wait_write_ptr = 0;
1962 unsigned long flags;
1963 int rc;
1964
1965 spin_lock_irqsave(&priv->lock, flags);
Tomas Winklerfee12472008-04-03 16:05:21 -07001966 if (iwl_is_rfkill(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07001967 IWL_DEBUG_DROP("Dropping - RF KILL\n");
1968 goto drop_unlock;
1969 }
1970
Johannes Berg32bfd352007-12-19 01:31:26 +01001971 if (!priv->vif) {
1972 IWL_DEBUG_DROP("Dropping - !priv->vif\n");
Zhu Yib481de92007-09-25 17:54:57 -07001973 goto drop_unlock;
1974 }
1975
Johannes Berg8318d782008-01-24 19:38:38 +01001976 if ((ctl->tx_rate->hw_value & 0xFF) == IWL_INVALID_RATE) {
Zhu Yib481de92007-09-25 17:54:57 -07001977 IWL_ERROR("ERROR: No TX rate available.\n");
1978 goto drop_unlock;
1979 }
1980
1981 unicast = !is_multicast_ether_addr(hdr->addr1);
1982 id = 0;
1983
1984 fc = le16_to_cpu(hdr->frame_control);
1985
Tomas Winkler0a6857e2008-03-12 16:58:49 -07001986#ifdef CONFIG_IWLWIFI_DEBUG
Zhu Yib481de92007-09-25 17:54:57 -07001987 if (ieee80211_is_auth(fc))
1988 IWL_DEBUG_TX("Sending AUTH frame\n");
1989 else if (ieee80211_is_assoc_request(fc))
1990 IWL_DEBUG_TX("Sending ASSOC frame\n");
1991 else if (ieee80211_is_reassoc_request(fc))
1992 IWL_DEBUG_TX("Sending REASSOC frame\n");
1993#endif
1994
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08001995 /* drop all data frame if we are not associated */
Gregory Greenman76f39152008-01-23 10:15:21 -08001996 if (((fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_DATA) &&
Tomas Winkler3109ece2008-03-28 16:33:35 -07001997 (!iwl_is_associated(priv) ||
Reinette Chatrea6477242008-02-14 10:40:28 -08001998 ((priv->iw_mode == IEEE80211_IF_TYPE_STA) && !priv->assoc_id) ||
Gregory Greenman76f39152008-01-23 10:15:21 -08001999 !priv->assoc_station_added)) {
Tomas Winkler3109ece2008-03-28 16:33:35 -07002000 IWL_DEBUG_DROP("Dropping - !iwl_is_associated\n");
Zhu Yib481de92007-09-25 17:54:57 -07002001 goto drop_unlock;
2002 }
2003
2004 spin_unlock_irqrestore(&priv->lock, flags);
2005
2006 hdr_len = ieee80211_get_hdrlen(fc);
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002007
2008 /* Find (or create) index into station table for destination station */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002009 sta_id = iwl4965_get_sta_id(priv, hdr);
Zhu Yib481de92007-09-25 17:54:57 -07002010 if (sta_id == IWL_INVALID_STATION) {
Joe Perches0795af52007-10-03 17:59:30 -07002011 DECLARE_MAC_BUF(mac);
2012
2013 IWL_DEBUG_DROP("Dropping - INVALID STATION: %s\n",
2014 print_mac(mac, hdr->addr1));
Zhu Yib481de92007-09-25 17:54:57 -07002015 goto drop;
2016 }
2017
2018 IWL_DEBUG_RATE("station Id %d\n", sta_id);
2019
2020 qc = ieee80211_get_qos_ctrl(hdr);
2021 if (qc) {
2022 u8 tid = (u8)(le16_to_cpu(*qc) & 0xf);
2023 seq_number = priv->stations[sta_id].tid[tid].seq_number &
2024 IEEE80211_SCTL_SEQ;
2025 hdr->seq_ctrl = cpu_to_le16(seq_number) |
2026 (hdr->seq_ctrl &
2027 __constant_cpu_to_le16(IEEE80211_SCTL_FRAG));
2028 seq_number += 0x10;
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08002029#ifdef CONFIG_IWL4965_HT
Zhu Yib481de92007-09-25 17:54:57 -07002030 /* aggregation is on for this <sta,tid> */
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02002031 if (ctl->flags & IEEE80211_TXCTL_AMPDU)
Zhu Yib481de92007-09-25 17:54:57 -07002032 txq_id = priv->stations[sta_id].tid[tid].agg.txq_id;
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02002033 priv->stations[sta_id].tid[tid].tfds_in_queue++;
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08002034#endif /* CONFIG_IWL4965_HT */
Zhu Yib481de92007-09-25 17:54:57 -07002035 }
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002036
2037 /* Descriptor for chosen Tx queue */
Zhu Yib481de92007-09-25 17:54:57 -07002038 txq = &priv->txq[txq_id];
2039 q = &txq->q;
2040
2041 spin_lock_irqsave(&priv->lock, flags);
2042
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002043 /* Set up first empty TFD within this queue's circular TFD buffer */
Tomas Winklerfc4b6852007-10-25 17:15:24 +08002044 tfd = &txq->bd[q->write_ptr];
Zhu Yib481de92007-09-25 17:54:57 -07002045 memset(tfd, 0, sizeof(*tfd));
2046 control_flags = (u32 *) tfd;
Tomas Winklerfc4b6852007-10-25 17:15:24 +08002047 idx = get_cmd_index(q, q->write_ptr, 0);
Zhu Yib481de92007-09-25 17:54:57 -07002048
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002049 /* Set up driver data for this TFD */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002050 memset(&(txq->txb[q->write_ptr]), 0, sizeof(struct iwl4965_tx_info));
Tomas Winklerfc4b6852007-10-25 17:15:24 +08002051 txq->txb[q->write_ptr].skb[0] = skb;
2052 memcpy(&(txq->txb[q->write_ptr].status.control),
Zhu Yib481de92007-09-25 17:54:57 -07002053 ctl, sizeof(struct ieee80211_tx_control));
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002054
2055 /* Set up first empty entry in queue's array of Tx/cmd buffers */
Zhu Yib481de92007-09-25 17:54:57 -07002056 out_cmd = &txq->cmd[idx];
2057 memset(&out_cmd->hdr, 0, sizeof(out_cmd->hdr));
2058 memset(&out_cmd->cmd.tx, 0, sizeof(out_cmd->cmd.tx));
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002059
2060 /*
2061 * Set up the Tx-command (not MAC!) header.
2062 * Store the chosen Tx queue and TFD index within the sequence field;
2063 * after Tx, uCode's Tx response will return this value so driver can
2064 * locate the frame within the tx queue and do post-tx processing.
2065 */
Zhu Yib481de92007-09-25 17:54:57 -07002066 out_cmd->hdr.cmd = REPLY_TX;
2067 out_cmd->hdr.sequence = cpu_to_le16((u16)(QUEUE_TO_SEQ(txq_id) |
Tomas Winklerfc4b6852007-10-25 17:15:24 +08002068 INDEX_TO_SEQ(q->write_ptr)));
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002069
2070 /* Copy MAC header from skb into command buffer */
Zhu Yib481de92007-09-25 17:54:57 -07002071 memcpy(out_cmd->cmd.tx.hdr, hdr, hdr_len);
2072
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002073 /*
2074 * Use the first empty entry in this queue's command buffer array
2075 * to contain the Tx command and MAC header concatenated together
2076 * (payload data will be in another buffer).
2077 * Size of this varies, due to varying MAC header length.
2078 * If end is not dword aligned, we'll have 2 extra bytes at the end
2079 * of the MAC header (device reads on dword boundaries).
2080 * We'll tell device about this padding later.
2081 */
Tomas Winkler5425e492008-04-15 16:01:38 -07002082 len = priv->hw_params.tx_cmd_len +
Tomas Winkler857485c2008-03-21 13:53:44 -07002083 sizeof(struct iwl_cmd_header) + hdr_len;
Zhu Yib481de92007-09-25 17:54:57 -07002084
2085 len_org = len;
2086 len = (len + 3) & ~3;
2087
2088 if (len_org != len)
2089 len_org = 1;
2090 else
2091 len_org = 0;
2092
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002093 /* Physical address of this Tx command's header (not MAC header!),
2094 * within command buffer array. */
Tomas Winkler857485c2008-03-21 13:53:44 -07002095 txcmd_phys = txq->dma_addr_cmd + sizeof(struct iwl_cmd) * idx +
2096 offsetof(struct iwl_cmd, hdr);
Zhu Yib481de92007-09-25 17:54:57 -07002097
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002098 /* Add buffer containing Tx command and MAC(!) header to TFD's
2099 * first entry */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002100 iwl4965_hw_txq_attach_buf_to_tfd(priv, tfd, txcmd_phys, len);
Zhu Yib481de92007-09-25 17:54:57 -07002101
2102 if (!(ctl->flags & IEEE80211_TXCTL_DO_NOT_ENCRYPT))
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07002103 iwl4965_build_tx_cmd_hwcrypto(priv, ctl, out_cmd, skb, sta_id);
Zhu Yib481de92007-09-25 17:54:57 -07002104
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002105 /* Set up TFD's 2nd entry to point directly to remainder of skb,
2106 * if any (802.11 null frames have no payload). */
Zhu Yib481de92007-09-25 17:54:57 -07002107 len = skb->len - hdr_len;
2108 if (len) {
2109 phys_addr = pci_map_single(priv->pci_dev, skb->data + hdr_len,
2110 len, PCI_DMA_TODEVICE);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002111 iwl4965_hw_txq_attach_buf_to_tfd(priv, tfd, phys_addr, len);
Zhu Yib481de92007-09-25 17:54:57 -07002112 }
2113
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002114 /* Tell 4965 about any 2-byte padding after MAC header */
Zhu Yib481de92007-09-25 17:54:57 -07002115 if (len_org)
2116 out_cmd->cmd.tx.tx_flags |= TX_CMD_FLG_MH_PAD_MSK;
2117
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002118 /* Total # bytes to be transmitted */
Zhu Yib481de92007-09-25 17:54:57 -07002119 len = (u16)skb->len;
2120 out_cmd->cmd.tx.len = cpu_to_le16(len);
2121
2122 /* TODO need this for burst mode later on */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002123 iwl4965_build_tx_cmd_basic(priv, out_cmd, ctl, hdr, unicast, sta_id);
Zhu Yib481de92007-09-25 17:54:57 -07002124
2125 /* set is_hcca to 0; it probably will never be implemented */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002126 iwl4965_hw_build_tx_cmd_rate(priv, out_cmd, ctl, hdr, sta_id, 0);
Zhu Yib481de92007-09-25 17:54:57 -07002127
Tomas Winkler19758be2008-03-12 16:58:51 -07002128 iwl_update_tx_stats(priv, fc, len);
2129
Tomas Winkler857485c2008-03-21 13:53:44 -07002130 scratch_phys = txcmd_phys + sizeof(struct iwl_cmd_header) +
Tomas Winkler87e4f7d2008-01-14 17:46:16 -08002131 offsetof(struct iwl4965_tx_cmd, scratch);
2132 out_cmd->cmd.tx.dram_lsb_ptr = cpu_to_le32(scratch_phys);
2133 out_cmd->cmd.tx.dram_msb_ptr = iwl_get_dma_hi_address(scratch_phys);
2134
Zhu Yib481de92007-09-25 17:54:57 -07002135 if (!ieee80211_get_morefrag(hdr)) {
2136 txq->need_update = 1;
2137 if (qc) {
2138 u8 tid = (u8)(le16_to_cpu(*qc) & 0xf);
2139 priv->stations[sta_id].tid[tid].seq_number = seq_number;
2140 }
2141 } else {
2142 wait_write_ptr = 1;
2143 txq->need_update = 0;
2144 }
2145
Tomas Winkler0a6857e2008-03-12 16:58:49 -07002146 iwl_print_hex_dump(IWL_DL_TX, out_cmd->cmd.payload,
Zhu Yib481de92007-09-25 17:54:57 -07002147 sizeof(out_cmd->cmd.tx));
2148
Tomas Winkler0a6857e2008-03-12 16:58:49 -07002149 iwl_print_hex_dump(IWL_DL_TX, (u8 *)out_cmd->cmd.tx.hdr,
Zhu Yib481de92007-09-25 17:54:57 -07002150 ieee80211_get_hdrlen(fc));
2151
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002152 /* Set up entry for this TFD in Tx byte-count array */
Tomas Winklere2a722e2008-04-14 21:16:10 -07002153 priv->cfg->ops->lib->txq_update_byte_cnt_tbl(priv, txq, len);
Zhu Yib481de92007-09-25 17:54:57 -07002154
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002155 /* Tell device the write index *just past* this latest filled TFD */
Tomas Winklerc54b6792008-03-06 17:36:53 -08002156 q->write_ptr = iwl_queue_inc_wrap(q->write_ptr, q->n_bd);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002157 rc = iwl4965_tx_queue_update_write_ptr(priv, txq);
Zhu Yib481de92007-09-25 17:54:57 -07002158 spin_unlock_irqrestore(&priv->lock, flags);
2159
2160 if (rc)
2161 return rc;
2162
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002163 if ((iwl4965_queue_space(q) < q->high_mark)
Zhu Yib481de92007-09-25 17:54:57 -07002164 && priv->mac80211_registered) {
2165 if (wait_write_ptr) {
2166 spin_lock_irqsave(&priv->lock, flags);
2167 txq->need_update = 1;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002168 iwl4965_tx_queue_update_write_ptr(priv, txq);
Zhu Yib481de92007-09-25 17:54:57 -07002169 spin_unlock_irqrestore(&priv->lock, flags);
2170 }
2171
2172 ieee80211_stop_queue(priv->hw, ctl->queue);
2173 }
2174
2175 return 0;
2176
2177drop_unlock:
2178 spin_unlock_irqrestore(&priv->lock, flags);
2179drop:
2180 return -1;
2181}
2182
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002183static void iwl4965_set_rate(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002184{
Johannes Berg8318d782008-01-24 19:38:38 +01002185 const struct ieee80211_supported_band *hw = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07002186 struct ieee80211_rate *rate;
2187 int i;
2188
Johannes Berg8318d782008-01-24 19:38:38 +01002189 hw = iwl4965_get_hw_mode(priv, priv->band);
Saleem Abdulrasoolc4ba9622007-11-18 23:59:08 -08002190 if (!hw) {
2191 IWL_ERROR("Failed to set rate: unable to get hw mode\n");
2192 return;
2193 }
Zhu Yib481de92007-09-25 17:54:57 -07002194
2195 priv->active_rate = 0;
2196 priv->active_rate_basic = 0;
2197
Johannes Berg8318d782008-01-24 19:38:38 +01002198 for (i = 0; i < hw->n_bitrates; i++) {
2199 rate = &(hw->bitrates[i]);
2200 if (rate->hw_value < IWL_RATE_COUNT)
2201 priv->active_rate |= (1 << rate->hw_value);
Zhu Yib481de92007-09-25 17:54:57 -07002202 }
2203
2204 IWL_DEBUG_RATE("Set active_rate = %0x, active_rate_basic = %0x\n",
2205 priv->active_rate, priv->active_rate_basic);
2206
2207 /*
2208 * If a basic rate is configured, then use it (adding IWL_RATE_1M_MASK)
2209 * otherwise set it to the default of all CCK rates and 6, 12, 24 for
2210 * OFDM
2211 */
2212 if (priv->active_rate_basic & IWL_CCK_BASIC_RATES_MASK)
2213 priv->staging_rxon.cck_basic_rates =
2214 ((priv->active_rate_basic &
2215 IWL_CCK_RATES_MASK) >> IWL_FIRST_CCK_RATE) & 0xF;
2216 else
2217 priv->staging_rxon.cck_basic_rates =
2218 (IWL_CCK_BASIC_RATES_MASK >> IWL_FIRST_CCK_RATE) & 0xF;
2219
2220 if (priv->active_rate_basic & IWL_OFDM_BASIC_RATES_MASK)
2221 priv->staging_rxon.ofdm_basic_rates =
2222 ((priv->active_rate_basic &
2223 (IWL_OFDM_BASIC_RATES_MASK | IWL_RATE_6M_MASK)) >>
2224 IWL_FIRST_OFDM_RATE) & 0xFF;
2225 else
2226 priv->staging_rxon.ofdm_basic_rates =
2227 (IWL_OFDM_BASIC_RATES_MASK >> IWL_FIRST_OFDM_RATE) & 0xFF;
2228}
2229
Mohamed Abbasad97edd2008-03-28 16:21:06 -07002230void iwl4965_radio_kill_sw(struct iwl_priv *priv, int disable_radio)
Zhu Yib481de92007-09-25 17:54:57 -07002231{
2232 unsigned long flags;
2233
2234 if (!!disable_radio == test_bit(STATUS_RF_KILL_SW, &priv->status))
2235 return;
2236
2237 IWL_DEBUG_RF_KILL("Manual SW RF KILL set to: RADIO %s\n",
2238 disable_radio ? "OFF" : "ON");
2239
2240 if (disable_radio) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002241 iwl4965_scan_cancel(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002242 /* FIXME: This is a workaround for AP */
2243 if (priv->iw_mode != IEEE80211_IF_TYPE_AP) {
2244 spin_lock_irqsave(&priv->lock, flags);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07002245 iwl_write32(priv, CSR_UCODE_DRV_GP1_SET,
Zhu Yib481de92007-09-25 17:54:57 -07002246 CSR_UCODE_SW_BIT_RFKILL);
2247 spin_unlock_irqrestore(&priv->lock, flags);
Mohamed Abbasad97edd2008-03-28 16:21:06 -07002248 /* call the host command only if no hw rf-kill set */
Mohamed Abbas59003832008-04-15 16:01:46 -07002249 if (!test_bit(STATUS_RF_KILL_HW, &priv->status) &&
2250 iwl_is_ready(priv))
Mohamed Abbasad97edd2008-03-28 16:21:06 -07002251 iwl4965_send_card_state(priv,
2252 CARD_STATE_CMD_DISABLE,
2253 0);
Zhu Yib481de92007-09-25 17:54:57 -07002254 set_bit(STATUS_RF_KILL_SW, &priv->status);
Mohamed Abbasad97edd2008-03-28 16:21:06 -07002255
2256 /* make sure mac80211 stop sending Tx frame */
2257 if (priv->mac80211_registered)
2258 ieee80211_stop_queues(priv->hw);
Zhu Yib481de92007-09-25 17:54:57 -07002259 }
2260 return;
2261 }
2262
2263 spin_lock_irqsave(&priv->lock, flags);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07002264 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
Zhu Yib481de92007-09-25 17:54:57 -07002265
2266 clear_bit(STATUS_RF_KILL_SW, &priv->status);
2267 spin_unlock_irqrestore(&priv->lock, flags);
2268
2269 /* wake up ucode */
2270 msleep(10);
2271
2272 spin_lock_irqsave(&priv->lock, flags);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07002273 iwl_read32(priv, CSR_UCODE_DRV_GP1);
2274 if (!iwl_grab_nic_access(priv))
2275 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002276 spin_unlock_irqrestore(&priv->lock, flags);
2277
2278 if (test_bit(STATUS_RF_KILL_HW, &priv->status)) {
2279 IWL_DEBUG_RF_KILL("Can not turn radio back on - "
2280 "disabled by HW switch\n");
2281 return;
2282 }
2283
2284 queue_work(priv->workqueue, &priv->restart);
2285 return;
2286}
2287
Zhu Yib481de92007-09-25 17:54:57 -07002288#define IWL_PACKET_RETRY_TIME HZ
2289
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002290int iwl4965_is_duplicate_packet(struct iwl_priv *priv, struct ieee80211_hdr *header)
Zhu Yib481de92007-09-25 17:54:57 -07002291{
2292 u16 sc = le16_to_cpu(header->seq_ctrl);
2293 u16 seq = (sc & IEEE80211_SCTL_SEQ) >> 4;
2294 u16 frag = sc & IEEE80211_SCTL_FRAG;
2295 u16 *last_seq, *last_frag;
2296 unsigned long *last_time;
2297
2298 switch (priv->iw_mode) {
2299 case IEEE80211_IF_TYPE_IBSS:{
2300 struct list_head *p;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002301 struct iwl4965_ibss_seq *entry = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07002302 u8 *mac = header->addr2;
2303 int index = mac[5] & (IWL_IBSS_MAC_HASH_SIZE - 1);
2304
2305 __list_for_each(p, &priv->ibss_mac_hash[index]) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002306 entry = list_entry(p, struct iwl4965_ibss_seq, list);
Zhu Yib481de92007-09-25 17:54:57 -07002307 if (!compare_ether_addr(entry->mac, mac))
2308 break;
2309 }
2310 if (p == &priv->ibss_mac_hash[index]) {
2311 entry = kzalloc(sizeof(*entry), GFP_ATOMIC);
2312 if (!entry) {
Ian Schrambc434dd2007-10-25 17:15:29 +08002313 IWL_ERROR("Cannot malloc new mac entry\n");
Zhu Yib481de92007-09-25 17:54:57 -07002314 return 0;
2315 }
2316 memcpy(entry->mac, mac, ETH_ALEN);
2317 entry->seq_num = seq;
2318 entry->frag_num = frag;
2319 entry->packet_time = jiffies;
Ian Schrambc434dd2007-10-25 17:15:29 +08002320 list_add(&entry->list, &priv->ibss_mac_hash[index]);
Zhu Yib481de92007-09-25 17:54:57 -07002321 return 0;
2322 }
2323 last_seq = &entry->seq_num;
2324 last_frag = &entry->frag_num;
2325 last_time = &entry->packet_time;
2326 break;
2327 }
2328 case IEEE80211_IF_TYPE_STA:
2329 last_seq = &priv->last_seq_num;
2330 last_frag = &priv->last_frag_num;
2331 last_time = &priv->last_packet_time;
2332 break;
2333 default:
2334 return 0;
2335 }
2336 if ((*last_seq == seq) &&
2337 time_after(*last_time + IWL_PACKET_RETRY_TIME, jiffies)) {
2338 if (*last_frag == frag)
2339 goto drop;
2340 if (*last_frag + 1 != frag)
2341 /* out-of-order fragment */
2342 goto drop;
2343 } else
2344 *last_seq = seq;
2345
2346 *last_frag = frag;
2347 *last_time = jiffies;
2348 return 0;
2349
2350 drop:
2351 return 1;
2352}
2353
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08002354#ifdef CONFIG_IWL4965_SPECTRUM_MEASUREMENT
Zhu Yib481de92007-09-25 17:54:57 -07002355
2356#include "iwl-spectrum.h"
2357
2358#define BEACON_TIME_MASK_LOW 0x00FFFFFF
2359#define BEACON_TIME_MASK_HIGH 0xFF000000
2360#define TIME_UNIT 1024
2361
2362/*
2363 * extended beacon time format
2364 * time in usec will be changed into a 32-bit value in 8:24 format
2365 * the high 1 byte is the beacon counts
2366 * the lower 3 bytes is the time in usec within one beacon interval
2367 */
2368
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002369static u32 iwl4965_usecs_to_beacons(u32 usec, u32 beacon_interval)
Zhu Yib481de92007-09-25 17:54:57 -07002370{
2371 u32 quot;
2372 u32 rem;
2373 u32 interval = beacon_interval * 1024;
2374
2375 if (!interval || !usec)
2376 return 0;
2377
2378 quot = (usec / interval) & (BEACON_TIME_MASK_HIGH >> 24);
2379 rem = (usec % interval) & BEACON_TIME_MASK_LOW;
2380
2381 return (quot << 24) + rem;
2382}
2383
2384/* base is usually what we get from ucode with each received frame,
2385 * the same as HW timer counter counting down
2386 */
2387
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002388static __le32 iwl4965_add_beacon_time(u32 base, u32 addon, u32 beacon_interval)
Zhu Yib481de92007-09-25 17:54:57 -07002389{
2390 u32 base_low = base & BEACON_TIME_MASK_LOW;
2391 u32 addon_low = addon & BEACON_TIME_MASK_LOW;
2392 u32 interval = beacon_interval * TIME_UNIT;
2393 u32 res = (base & BEACON_TIME_MASK_HIGH) +
2394 (addon & BEACON_TIME_MASK_HIGH);
2395
2396 if (base_low > addon_low)
2397 res += base_low - addon_low;
2398 else if (base_low < addon_low) {
2399 res += interval + base_low - addon_low;
2400 res += (1 << 24);
2401 } else
2402 res += (1 << 24);
2403
2404 return cpu_to_le32(res);
2405}
2406
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002407static int iwl4965_get_measurement(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07002408 struct ieee80211_measurement_params *params,
2409 u8 type)
2410{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002411 struct iwl4965_spectrum_cmd spectrum;
2412 struct iwl4965_rx_packet *res;
Tomas Winkler857485c2008-03-21 13:53:44 -07002413 struct iwl_host_cmd cmd = {
Zhu Yib481de92007-09-25 17:54:57 -07002414 .id = REPLY_SPECTRUM_MEASUREMENT_CMD,
2415 .data = (void *)&spectrum,
2416 .meta.flags = CMD_WANT_SKB,
2417 };
2418 u32 add_time = le64_to_cpu(params->start_time);
2419 int rc;
2420 int spectrum_resp_status;
2421 int duration = le16_to_cpu(params->duration);
2422
Tomas Winkler3109ece2008-03-28 16:33:35 -07002423 if (iwl_is_associated(priv))
Zhu Yib481de92007-09-25 17:54:57 -07002424 add_time =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002425 iwl4965_usecs_to_beacons(
Zhu Yib481de92007-09-25 17:54:57 -07002426 le64_to_cpu(params->start_time) - priv->last_tsf,
2427 le16_to_cpu(priv->rxon_timing.beacon_interval));
2428
2429 memset(&spectrum, 0, sizeof(spectrum));
2430
2431 spectrum.channel_count = cpu_to_le16(1);
2432 spectrum.flags =
2433 RXON_FLG_TSF2HOST_MSK | RXON_FLG_ANT_A_MSK | RXON_FLG_DIS_DIV_MSK;
2434 spectrum.filter_flags = MEASUREMENT_FILTER_FLAG;
2435 cmd.len = sizeof(spectrum);
2436 spectrum.len = cpu_to_le16(cmd.len - sizeof(spectrum.len));
2437
Tomas Winkler3109ece2008-03-28 16:33:35 -07002438 if (iwl_is_associated(priv))
Zhu Yib481de92007-09-25 17:54:57 -07002439 spectrum.start_time =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002440 iwl4965_add_beacon_time(priv->last_beacon_time,
Zhu Yib481de92007-09-25 17:54:57 -07002441 add_time,
2442 le16_to_cpu(priv->rxon_timing.beacon_interval));
2443 else
2444 spectrum.start_time = 0;
2445
2446 spectrum.channels[0].duration = cpu_to_le32(duration * TIME_UNIT);
2447 spectrum.channels[0].channel = params->channel;
2448 spectrum.channels[0].type = type;
2449 if (priv->active_rxon.flags & RXON_FLG_BAND_24G_MSK)
2450 spectrum.flags |= RXON_FLG_BAND_24G_MSK |
2451 RXON_FLG_AUTO_DETECT_MSK | RXON_FLG_TGG_PROTECT_MSK;
2452
Tomas Winkler857485c2008-03-21 13:53:44 -07002453 rc = iwl_send_cmd_sync(priv, &cmd);
Zhu Yib481de92007-09-25 17:54:57 -07002454 if (rc)
2455 return rc;
2456
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002457 res = (struct iwl4965_rx_packet *)cmd.meta.u.skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07002458 if (res->hdr.flags & IWL_CMD_FAILED_MSK) {
2459 IWL_ERROR("Bad return from REPLY_RX_ON_ASSOC command\n");
2460 rc = -EIO;
2461 }
2462
2463 spectrum_resp_status = le16_to_cpu(res->u.spectrum.status);
2464 switch (spectrum_resp_status) {
2465 case 0: /* Command will be handled */
2466 if (res->u.spectrum.id != 0xff) {
2467 IWL_DEBUG_INFO
2468 ("Replaced existing measurement: %d\n",
2469 res->u.spectrum.id);
2470 priv->measurement_status &= ~MEASUREMENT_READY;
2471 }
2472 priv->measurement_status |= MEASUREMENT_ACTIVE;
2473 rc = 0;
2474 break;
2475
2476 case 1: /* Command will not be handled */
2477 rc = -EAGAIN;
2478 break;
2479 }
2480
2481 dev_kfree_skb_any(cmd.meta.u.skb);
2482
2483 return rc;
2484}
2485#endif
2486
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002487static void iwl4965_txstatus_to_ieee(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002488 struct iwl4965_tx_info *tx_sta)
Zhu Yib481de92007-09-25 17:54:57 -07002489{
2490
2491 tx_sta->status.ack_signal = 0;
2492 tx_sta->status.excessive_retries = 0;
2493 tx_sta->status.queue_length = 0;
2494 tx_sta->status.queue_number = 0;
2495
2496 if (in_interrupt())
2497 ieee80211_tx_status_irqsafe(priv->hw,
2498 tx_sta->skb[0], &(tx_sta->status));
2499 else
2500 ieee80211_tx_status(priv->hw,
2501 tx_sta->skb[0], &(tx_sta->status));
2502
2503 tx_sta->skb[0] = NULL;
2504}
2505
2506/**
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002507 * iwl4965_tx_queue_reclaim - Reclaim Tx queue entries already Tx'd
Zhu Yib481de92007-09-25 17:54:57 -07002508 *
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002509 * When FW advances 'R' index, all entries between old and new 'R' index
2510 * need to be reclaimed. As result, some free space forms. If there is
2511 * enough free space (> low mark), wake the stack that feeds us.
Zhu Yib481de92007-09-25 17:54:57 -07002512 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002513int iwl4965_tx_queue_reclaim(struct iwl_priv *priv, int txq_id, int index)
Zhu Yib481de92007-09-25 17:54:57 -07002514{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002515 struct iwl4965_tx_queue *txq = &priv->txq[txq_id];
2516 struct iwl4965_queue *q = &txq->q;
Zhu Yib481de92007-09-25 17:54:57 -07002517 int nfreed = 0;
2518
2519 if ((index >= q->n_bd) || (x2_queue_used(q, index) == 0)) {
2520 IWL_ERROR("Read index for DMA queue txq id (%d), index %d, "
2521 "is out of range [0-%d] %d %d.\n", txq_id,
Tomas Winklerfc4b6852007-10-25 17:15:24 +08002522 index, q->n_bd, q->write_ptr, q->read_ptr);
Zhu Yib481de92007-09-25 17:54:57 -07002523 return 0;
2524 }
2525
Tomas Winklerc54b6792008-03-06 17:36:53 -08002526 for (index = iwl_queue_inc_wrap(index, q->n_bd);
Tomas Winklerfc4b6852007-10-25 17:15:24 +08002527 q->read_ptr != index;
Tomas Winklerc54b6792008-03-06 17:36:53 -08002528 q->read_ptr = iwl_queue_inc_wrap(q->read_ptr, q->n_bd)) {
Zhu Yib481de92007-09-25 17:54:57 -07002529 if (txq_id != IWL_CMD_QUEUE_NUM) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002530 iwl4965_txstatus_to_ieee(priv,
Tomas Winklerfc4b6852007-10-25 17:15:24 +08002531 &(txq->txb[txq->q.read_ptr]));
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002532 iwl4965_hw_txq_free_tfd(priv, txq);
Zhu Yib481de92007-09-25 17:54:57 -07002533 } else if (nfreed > 1) {
2534 IWL_ERROR("HCMD skipped: index (%d) %d %d\n", index,
Tomas Winklerfc4b6852007-10-25 17:15:24 +08002535 q->write_ptr, q->read_ptr);
Zhu Yib481de92007-09-25 17:54:57 -07002536 queue_work(priv->workqueue, &priv->restart);
2537 }
2538 nfreed++;
2539 }
2540
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02002541/* if (iwl4965_queue_space(q) > q->low_mark && (txq_id >= 0) &&
Zhu Yib481de92007-09-25 17:54:57 -07002542 (txq_id != IWL_CMD_QUEUE_NUM) &&
2543 priv->mac80211_registered)
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02002544 ieee80211_wake_queue(priv->hw, txq_id); */
Zhu Yib481de92007-09-25 17:54:57 -07002545
2546
2547 return nfreed;
2548}
2549
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002550static int iwl4965_is_tx_success(u32 status)
Zhu Yib481de92007-09-25 17:54:57 -07002551{
2552 status &= TX_STATUS_MSK;
2553 return (status == TX_STATUS_SUCCESS)
2554 || (status == TX_STATUS_DIRECT_DONE);
2555}
2556
2557/******************************************************************************
2558 *
2559 * Generic RX handler implementations
2560 *
2561 ******************************************************************************/
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08002562#ifdef CONFIG_IWL4965_HT
Zhu Yib481de92007-09-25 17:54:57 -07002563
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002564static inline int iwl4965_get_ra_sta_id(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07002565 struct ieee80211_hdr *hdr)
2566{
2567 if (priv->iw_mode == IEEE80211_IF_TYPE_STA)
2568 return IWL_AP_ID;
2569 else {
2570 u8 *da = ieee80211_get_DA(hdr);
Tomas Winkler947b13a2008-04-16 16:34:48 -07002571 return iwl_find_station(priv, da);
Zhu Yib481de92007-09-25 17:54:57 -07002572 }
2573}
2574
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002575static struct ieee80211_hdr *iwl4965_tx_queue_get_hdr(
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002576 struct iwl_priv *priv, int txq_id, int idx)
Zhu Yib481de92007-09-25 17:54:57 -07002577{
2578 if (priv->txq[txq_id].txb[idx].skb[0])
2579 return (struct ieee80211_hdr *)priv->txq[txq_id].
2580 txb[idx].skb[0]->data;
2581 return NULL;
2582}
2583
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002584static inline u32 iwl4965_get_scd_ssn(struct iwl4965_tx_resp *tx_resp)
Zhu Yib481de92007-09-25 17:54:57 -07002585{
2586 __le32 *scd_ssn = (__le32 *)((u32 *)&tx_resp->status +
2587 tx_resp->frame_count);
2588 return le32_to_cpu(*scd_ssn) & MAX_SN;
2589
2590}
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002591
2592/**
2593 * iwl4965_tx_status_reply_tx - Handle Tx rspnse for frames in aggregation queue
2594 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002595static int iwl4965_tx_status_reply_tx(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002596 struct iwl4965_ht_agg *agg,
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02002597 struct iwl4965_tx_resp_agg *tx_resp,
Zhu Yib481de92007-09-25 17:54:57 -07002598 u16 start_idx)
2599{
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02002600 u16 status;
2601 struct agg_tx_status *frame_status = &tx_resp->status;
Zhu Yib481de92007-09-25 17:54:57 -07002602 struct ieee80211_tx_status *tx_status = NULL;
2603 struct ieee80211_hdr *hdr = NULL;
2604 int i, sh;
2605 int txq_id, idx;
2606 u16 seq;
2607
2608 if (agg->wait_for_ba)
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002609 IWL_DEBUG_TX_REPLY("got tx response w/o block-ack\n");
Zhu Yib481de92007-09-25 17:54:57 -07002610
2611 agg->frame_count = tx_resp->frame_count;
2612 agg->start_idx = start_idx;
2613 agg->rate_n_flags = le32_to_cpu(tx_resp->rate_n_flags);
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02002614 agg->bitmap = 0;
Zhu Yib481de92007-09-25 17:54:57 -07002615
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002616 /* # frames attempted by Tx command */
Zhu Yib481de92007-09-25 17:54:57 -07002617 if (agg->frame_count == 1) {
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002618 /* Only one frame was attempted; no block-ack will arrive */
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02002619 status = le16_to_cpu(frame_status[0].status);
2620 seq = le16_to_cpu(frame_status[0].sequence);
2621 idx = SEQ_TO_INDEX(seq);
2622 txq_id = SEQ_TO_QUEUE(seq);
Zhu Yib481de92007-09-25 17:54:57 -07002623
Zhu Yib481de92007-09-25 17:54:57 -07002624 /* FIXME: code repetition */
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02002625 IWL_DEBUG_TX_REPLY("FrameCnt = %d, StartIdx=%d idx=%d\n",
2626 agg->frame_count, agg->start_idx, idx);
Zhu Yib481de92007-09-25 17:54:57 -07002627
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02002628 tx_status = &(priv->txq[txq_id].txb[idx].status);
Zhu Yib481de92007-09-25 17:54:57 -07002629 tx_status->retry_count = tx_resp->failure_frame;
2630 tx_status->queue_number = status & 0xff;
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02002631 tx_status->queue_length = tx_resp->failure_rts;
2632 tx_status->control.flags &= ~IEEE80211_TXCTL_AMPDU;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002633 tx_status->flags = iwl4965_is_tx_success(status)?
Zhu Yib481de92007-09-25 17:54:57 -07002634 IEEE80211_TX_STATUS_ACK : 0;
Ron Rindjunsky4c424e42008-03-04 18:09:27 -08002635 iwl4965_hwrate_to_tx_control(priv,
2636 le32_to_cpu(tx_resp->rate_n_flags),
2637 &tx_status->control);
Zhu Yib481de92007-09-25 17:54:57 -07002638 /* FIXME: code repetition end */
2639
2640 IWL_DEBUG_TX_REPLY("1 Frame 0x%x failure :%d\n",
2641 status & 0xff, tx_resp->failure_frame);
2642 IWL_DEBUG_TX_REPLY("Rate Info rate_n_flags=%x\n",
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002643 iwl4965_hw_get_rate_n_flags(tx_resp->rate_n_flags));
Zhu Yib481de92007-09-25 17:54:57 -07002644
2645 agg->wait_for_ba = 0;
2646 } else {
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002647 /* Two or more frames were attempted; expect block-ack */
Zhu Yib481de92007-09-25 17:54:57 -07002648 u64 bitmap = 0;
2649 int start = agg->start_idx;
2650
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002651 /* Construct bit-map of pending frames within Tx window */
Zhu Yib481de92007-09-25 17:54:57 -07002652 for (i = 0; i < agg->frame_count; i++) {
2653 u16 sc;
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02002654 status = le16_to_cpu(frame_status[i].status);
2655 seq = le16_to_cpu(frame_status[i].sequence);
Zhu Yib481de92007-09-25 17:54:57 -07002656 idx = SEQ_TO_INDEX(seq);
2657 txq_id = SEQ_TO_QUEUE(seq);
2658
2659 if (status & (AGG_TX_STATE_FEW_BYTES_MSK |
2660 AGG_TX_STATE_ABORT_MSK))
2661 continue;
2662
2663 IWL_DEBUG_TX_REPLY("FrameCnt = %d, txq_id=%d idx=%d\n",
2664 agg->frame_count, txq_id, idx);
2665
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002666 hdr = iwl4965_tx_queue_get_hdr(priv, txq_id, idx);
Zhu Yib481de92007-09-25 17:54:57 -07002667
2668 sc = le16_to_cpu(hdr->seq_ctrl);
2669 if (idx != (SEQ_TO_SN(sc) & 0xff)) {
2670 IWL_ERROR("BUG_ON idx doesn't match seq control"
2671 " idx=%d, seq_idx=%d, seq=%d\n",
2672 idx, SEQ_TO_SN(sc),
2673 hdr->seq_ctrl);
2674 return -1;
2675 }
2676
2677 IWL_DEBUG_TX_REPLY("AGG Frame i=%d idx %d seq=%d\n",
2678 i, idx, SEQ_TO_SN(sc));
2679
2680 sh = idx - start;
2681 if (sh > 64) {
2682 sh = (start - idx) + 0xff;
2683 bitmap = bitmap << sh;
2684 sh = 0;
2685 start = idx;
2686 } else if (sh < -64)
2687 sh = 0xff - (start - idx);
2688 else if (sh < 0) {
2689 sh = start - idx;
2690 start = idx;
2691 bitmap = bitmap << sh;
2692 sh = 0;
2693 }
2694 bitmap |= (1 << sh);
2695 IWL_DEBUG_TX_REPLY("start=%d bitmap=0x%x\n",
2696 start, (u32)(bitmap & 0xFFFFFFFF));
2697 }
2698
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02002699 agg->bitmap = bitmap;
Zhu Yib481de92007-09-25 17:54:57 -07002700 agg->start_idx = start;
2701 agg->rate_n_flags = le32_to_cpu(tx_resp->rate_n_flags);
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02002702 IWL_DEBUG_TX_REPLY("Frames %d start_idx=%d bitmap=0x%llx\n",
Zhu Yib481de92007-09-25 17:54:57 -07002703 agg->frame_count, agg->start_idx,
John W. Linville06501d22008-04-01 17:38:47 -04002704 (unsigned long long)agg->bitmap);
Zhu Yib481de92007-09-25 17:54:57 -07002705
2706 if (bitmap)
2707 agg->wait_for_ba = 1;
2708 }
2709 return 0;
2710}
2711#endif
Zhu Yib481de92007-09-25 17:54:57 -07002712
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002713/**
2714 * iwl4965_rx_reply_tx - Handle standard (non-aggregation) Tx response
2715 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002716static void iwl4965_rx_reply_tx(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002717 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07002718{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002719 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07002720 u16 sequence = le16_to_cpu(pkt->hdr.sequence);
2721 int txq_id = SEQ_TO_QUEUE(sequence);
2722 int index = SEQ_TO_INDEX(sequence);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002723 struct iwl4965_tx_queue *txq = &priv->txq[txq_id];
Zhu Yib481de92007-09-25 17:54:57 -07002724 struct ieee80211_tx_status *tx_status;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002725 struct iwl4965_tx_resp *tx_resp = (void *)&pkt->u.raw[0];
Zhu Yib481de92007-09-25 17:54:57 -07002726 u32 status = le32_to_cpu(tx_resp->status);
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08002727#ifdef CONFIG_IWL4965_HT
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02002728 int tid = MAX_TID_COUNT, sta_id = IWL_INVALID_STATION;
2729 struct ieee80211_hdr *hdr;
2730 __le16 *qc;
Zhu Yib481de92007-09-25 17:54:57 -07002731#endif
2732
2733 if ((index >= txq->q.n_bd) || (x2_queue_used(&txq->q, index) == 0)) {
2734 IWL_ERROR("Read index for DMA queue txq_id (%d) index %d "
2735 "is out of range [0-%d] %d %d\n", txq_id,
Tomas Winklerfc4b6852007-10-25 17:15:24 +08002736 index, txq->q.n_bd, txq->q.write_ptr,
2737 txq->q.read_ptr);
Zhu Yib481de92007-09-25 17:54:57 -07002738 return;
2739 }
2740
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08002741#ifdef CONFIG_IWL4965_HT
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02002742 hdr = iwl4965_tx_queue_get_hdr(priv, txq_id, index);
2743 qc = ieee80211_get_qos_ctrl(hdr);
Zhu Yib481de92007-09-25 17:54:57 -07002744
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02002745 if (qc)
Zhu Yib481de92007-09-25 17:54:57 -07002746 tid = le16_to_cpu(*qc) & 0xf;
2747
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02002748 sta_id = iwl4965_get_ra_sta_id(priv, hdr);
2749 if (txq->sched_retry && unlikely(sta_id == IWL_INVALID_STATION)) {
2750 IWL_ERROR("Station not known\n");
2751 return;
2752 }
2753
2754 if (txq->sched_retry) {
2755 const u32 scd_ssn = iwl4965_get_scd_ssn(tx_resp);
2756 struct iwl4965_ht_agg *agg = NULL;
2757
2758 if (!qc)
Zhu Yib481de92007-09-25 17:54:57 -07002759 return;
Zhu Yib481de92007-09-25 17:54:57 -07002760
2761 agg = &priv->stations[sta_id].tid[tid].agg;
2762
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02002763 iwl4965_tx_status_reply_tx(priv, agg,
2764 (struct iwl4965_tx_resp_agg *)tx_resp, index);
Zhu Yib481de92007-09-25 17:54:57 -07002765
2766 if ((tx_resp->frame_count == 1) &&
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002767 !iwl4965_is_tx_success(status)) {
Zhu Yib481de92007-09-25 17:54:57 -07002768 /* TODO: send BAR */
2769 }
2770
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02002771 if (txq->q.read_ptr != (scd_ssn & 0xff)) {
2772 int freed;
Tomas Winklerc54b6792008-03-06 17:36:53 -08002773 index = iwl_queue_dec_wrap(scd_ssn & 0xff, txq->q.n_bd);
Zhu Yib481de92007-09-25 17:54:57 -07002774 IWL_DEBUG_TX_REPLY("Retry scheduler reclaim scd_ssn "
2775 "%d index %d\n", scd_ssn , index);
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02002776 freed = iwl4965_tx_queue_reclaim(priv, txq_id, index);
2777 priv->stations[sta_id].tid[tid].tfds_in_queue -= freed;
2778
2779 if (iwl4965_queue_space(&txq->q) > txq->q.low_mark &&
2780 txq_id >= 0 && priv->mac80211_registered &&
2781 agg->state != IWL_EMPTYING_HW_QUEUE_DELBA)
2782 ieee80211_wake_queue(priv->hw, txq_id);
2783
2784 iwl4965_check_empty_hw_queue(priv, sta_id, tid, txq_id);
Zhu Yib481de92007-09-25 17:54:57 -07002785 }
2786 } else {
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08002787#endif /* CONFIG_IWL4965_HT */
Tomas Winklerfc4b6852007-10-25 17:15:24 +08002788 tx_status = &(txq->txb[txq->q.read_ptr].status);
Zhu Yib481de92007-09-25 17:54:57 -07002789
2790 tx_status->retry_count = tx_resp->failure_frame;
2791 tx_status->queue_number = status;
2792 tx_status->queue_length = tx_resp->bt_kill_count;
2793 tx_status->queue_length |= tx_resp->failure_rts;
Zhu Yib481de92007-09-25 17:54:57 -07002794 tx_status->flags =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002795 iwl4965_is_tx_success(status) ? IEEE80211_TX_STATUS_ACK : 0;
Ron Rindjunsky4c424e42008-03-04 18:09:27 -08002796 iwl4965_hwrate_to_tx_control(priv, le32_to_cpu(tx_resp->rate_n_flags),
2797 &tx_status->control);
Zhu Yib481de92007-09-25 17:54:57 -07002798
Zhu Yib481de92007-09-25 17:54:57 -07002799 IWL_DEBUG_TX("Tx queue %d Status %s (0x%08x) rate_n_flags 0x%x "
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002800 "retries %d\n", txq_id, iwl4965_get_tx_fail_reason(status),
Zhu Yib481de92007-09-25 17:54:57 -07002801 status, le32_to_cpu(tx_resp->rate_n_flags),
2802 tx_resp->failure_frame);
2803
2804 IWL_DEBUG_TX_REPLY("Tx queue reclaim %d\n", index);
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02002805 if (index != -1) {
2806 int freed = iwl4965_tx_queue_reclaim(priv, txq_id, index);
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08002807#ifdef CONFIG_IWL4965_HT
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02002808 if (tid != MAX_TID_COUNT)
2809 priv->stations[sta_id].tid[tid].tfds_in_queue -= freed;
2810 if (iwl4965_queue_space(&txq->q) > txq->q.low_mark &&
2811 (txq_id >= 0) &&
2812 priv->mac80211_registered)
2813 ieee80211_wake_queue(priv->hw, txq_id);
2814 if (tid != MAX_TID_COUNT)
2815 iwl4965_check_empty_hw_queue(priv, sta_id, tid, txq_id);
2816#endif
Zhu Yib481de92007-09-25 17:54:57 -07002817 }
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02002818#ifdef CONFIG_IWL4965_HT
2819 }
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08002820#endif /* CONFIG_IWL4965_HT */
Zhu Yib481de92007-09-25 17:54:57 -07002821
2822 if (iwl_check_bits(status, TX_ABORT_REQUIRED_MSK))
2823 IWL_ERROR("TODO: Implement Tx ABORT REQUIRED!!!\n");
2824}
2825
2826
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002827static void iwl4965_rx_reply_alive(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002828 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07002829{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002830 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
2831 struct iwl4965_alive_resp *palive;
Zhu Yib481de92007-09-25 17:54:57 -07002832 struct delayed_work *pwork;
2833
2834 palive = &pkt->u.alive_frame;
2835
2836 IWL_DEBUG_INFO("Alive ucode status 0x%08X revision "
2837 "0x%01X 0x%01X\n",
2838 palive->is_valid, palive->ver_type,
2839 palive->ver_subtype);
2840
2841 if (palive->ver_subtype == INITIALIZE_SUBTYPE) {
2842 IWL_DEBUG_INFO("Initialization Alive received.\n");
2843 memcpy(&priv->card_alive_init,
2844 &pkt->u.alive_frame,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002845 sizeof(struct iwl4965_init_alive_resp));
Zhu Yib481de92007-09-25 17:54:57 -07002846 pwork = &priv->init_alive_start;
2847 } else {
2848 IWL_DEBUG_INFO("Runtime Alive received.\n");
2849 memcpy(&priv->card_alive, &pkt->u.alive_frame,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002850 sizeof(struct iwl4965_alive_resp));
Zhu Yib481de92007-09-25 17:54:57 -07002851 pwork = &priv->alive_start;
2852 }
2853
2854 /* We delay the ALIVE response by 5ms to
2855 * give the HW RF Kill time to activate... */
2856 if (palive->is_valid == UCODE_VALID_OK)
2857 queue_delayed_work(priv->workqueue, pwork,
2858 msecs_to_jiffies(5));
2859 else
2860 IWL_WARNING("uCode did not respond OK.\n");
2861}
2862
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002863static void iwl4965_rx_reply_add_sta(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002864 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07002865{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002866 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07002867
2868 IWL_DEBUG_RX("Received REPLY_ADD_STA: 0x%02X\n", pkt->u.status);
2869 return;
2870}
2871
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002872static void iwl4965_rx_reply_error(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002873 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07002874{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002875 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07002876
2877 IWL_ERROR("Error Reply type 0x%08X cmd %s (0x%02X) "
2878 "seq 0x%04X ser 0x%08X\n",
2879 le32_to_cpu(pkt->u.err_resp.error_type),
2880 get_cmd_string(pkt->u.err_resp.cmd_id),
2881 pkt->u.err_resp.cmd_id,
2882 le16_to_cpu(pkt->u.err_resp.bad_cmd_seq_num),
2883 le32_to_cpu(pkt->u.err_resp.error_info));
2884}
2885
2886#define TX_STATUS_ENTRY(x) case TX_STATUS_FAIL_ ## x: return #x
2887
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002888static void iwl4965_rx_csa(struct iwl_priv *priv, struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07002889{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002890 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
2891 struct iwl4965_rxon_cmd *rxon = (void *)&priv->active_rxon;
2892 struct iwl4965_csa_notification *csa = &(pkt->u.csa_notif);
Zhu Yib481de92007-09-25 17:54:57 -07002893 IWL_DEBUG_11H("CSA notif: channel %d, status %d\n",
2894 le16_to_cpu(csa->channel), le32_to_cpu(csa->status));
2895 rxon->channel = csa->channel;
2896 priv->staging_rxon.channel = csa->channel;
2897}
2898
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002899static void iwl4965_rx_spectrum_measure_notif(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002900 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07002901{
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08002902#ifdef CONFIG_IWL4965_SPECTRUM_MEASUREMENT
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002903 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
2904 struct iwl4965_spectrum_notification *report = &(pkt->u.spectrum_notif);
Zhu Yib481de92007-09-25 17:54:57 -07002905
2906 if (!report->state) {
2907 IWL_DEBUG(IWL_DL_11H | IWL_DL_INFO,
2908 "Spectrum Measure Notification: Start\n");
2909 return;
2910 }
2911
2912 memcpy(&priv->measure_report, report, sizeof(*report));
2913 priv->measurement_status |= MEASUREMENT_READY;
2914#endif
2915}
2916
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002917static void iwl4965_rx_pm_sleep_notif(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002918 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07002919{
Tomas Winkler0a6857e2008-03-12 16:58:49 -07002920#ifdef CONFIG_IWLWIFI_DEBUG
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002921 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
2922 struct iwl4965_sleep_notification *sleep = &(pkt->u.sleep_notif);
Zhu Yib481de92007-09-25 17:54:57 -07002923 IWL_DEBUG_RX("sleep mode: %d, src: %d\n",
2924 sleep->pm_sleep_mode, sleep->pm_wakeup_src);
2925#endif
2926}
2927
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002928static void iwl4965_rx_pm_debug_statistics_notif(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002929 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07002930{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002931 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07002932 IWL_DEBUG_RADIO("Dumping %d bytes of unhandled "
2933 "notification for %s:\n",
2934 le32_to_cpu(pkt->len), get_cmd_string(pkt->hdr.cmd));
Tomas Winkler0a6857e2008-03-12 16:58:49 -07002935 iwl_print_hex_dump(IWL_DL_RADIO, pkt->u.raw, le32_to_cpu(pkt->len));
Zhu Yib481de92007-09-25 17:54:57 -07002936}
2937
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002938static void iwl4965_bg_beacon_update(struct work_struct *work)
Zhu Yib481de92007-09-25 17:54:57 -07002939{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002940 struct iwl_priv *priv =
2941 container_of(work, struct iwl_priv, beacon_update);
Zhu Yib481de92007-09-25 17:54:57 -07002942 struct sk_buff *beacon;
2943
2944 /* Pull updated AP beacon from mac80211. will fail if not in AP mode */
Johannes Berg32bfd352007-12-19 01:31:26 +01002945 beacon = ieee80211_beacon_get(priv->hw, priv->vif, NULL);
Zhu Yib481de92007-09-25 17:54:57 -07002946
2947 if (!beacon) {
2948 IWL_ERROR("update beacon failed\n");
2949 return;
2950 }
2951
2952 mutex_lock(&priv->mutex);
2953 /* new beacon skb is allocated every time; dispose previous.*/
2954 if (priv->ibss_beacon)
2955 dev_kfree_skb(priv->ibss_beacon);
2956
2957 priv->ibss_beacon = beacon;
2958 mutex_unlock(&priv->mutex);
2959
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002960 iwl4965_send_beacon_cmd(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002961}
2962
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002963static void iwl4965_rx_beacon_notif(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002964 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07002965{
Tomas Winkler0a6857e2008-03-12 16:58:49 -07002966#ifdef CONFIG_IWLWIFI_DEBUG
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002967 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
2968 struct iwl4965_beacon_notif *beacon = &(pkt->u.beacon_status);
2969 u8 rate = iwl4965_hw_get_rate(beacon->beacon_notify_hdr.rate_n_flags);
Zhu Yib481de92007-09-25 17:54:57 -07002970
2971 IWL_DEBUG_RX("beacon status %x retries %d iss %d "
2972 "tsf %d %d rate %d\n",
2973 le32_to_cpu(beacon->beacon_notify_hdr.status) & TX_STATUS_MSK,
2974 beacon->beacon_notify_hdr.failure_frame,
2975 le32_to_cpu(beacon->ibss_mgr_status),
2976 le32_to_cpu(beacon->high_tsf),
2977 le32_to_cpu(beacon->low_tsf), rate);
2978#endif
2979
2980 if ((priv->iw_mode == IEEE80211_IF_TYPE_AP) &&
2981 (!test_bit(STATUS_EXIT_PENDING, &priv->status)))
2982 queue_work(priv->workqueue, &priv->beacon_update);
2983}
2984
2985/* Service response to REPLY_SCAN_CMD (0x80) */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002986static void iwl4965_rx_reply_scan(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002987 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07002988{
Tomas Winkler0a6857e2008-03-12 16:58:49 -07002989#ifdef CONFIG_IWLWIFI_DEBUG
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002990 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
2991 struct iwl4965_scanreq_notification *notif =
2992 (struct iwl4965_scanreq_notification *)pkt->u.raw;
Zhu Yib481de92007-09-25 17:54:57 -07002993
2994 IWL_DEBUG_RX("Scan request status = 0x%x\n", notif->status);
2995#endif
2996}
2997
2998/* Service SCAN_START_NOTIFICATION (0x82) */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002999static void iwl4965_rx_scan_start_notif(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003000 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003001{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003002 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
3003 struct iwl4965_scanstart_notification *notif =
3004 (struct iwl4965_scanstart_notification *)pkt->u.raw;
Zhu Yib481de92007-09-25 17:54:57 -07003005 priv->scan_start_tsf = le32_to_cpu(notif->tsf_low);
3006 IWL_DEBUG_SCAN("Scan start: "
3007 "%d [802.11%s] "
3008 "(TSF: 0x%08X:%08X) - %d (beacon timer %u)\n",
3009 notif->channel,
3010 notif->band ? "bg" : "a",
3011 notif->tsf_high,
3012 notif->tsf_low, notif->status, notif->beacon_timer);
3013}
3014
3015/* Service SCAN_RESULTS_NOTIFICATION (0x83) */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003016static void iwl4965_rx_scan_results_notif(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003017 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003018{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003019 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
3020 struct iwl4965_scanresults_notification *notif =
3021 (struct iwl4965_scanresults_notification *)pkt->u.raw;
Zhu Yib481de92007-09-25 17:54:57 -07003022
3023 IWL_DEBUG_SCAN("Scan ch.res: "
3024 "%d [802.11%s] "
3025 "(TSF: 0x%08X:%08X) - %d "
3026 "elapsed=%lu usec (%dms since last)\n",
3027 notif->channel,
3028 notif->band ? "bg" : "a",
3029 le32_to_cpu(notif->tsf_high),
3030 le32_to_cpu(notif->tsf_low),
3031 le32_to_cpu(notif->statistics[0]),
3032 le32_to_cpu(notif->tsf_low) - priv->scan_start_tsf,
3033 jiffies_to_msecs(elapsed_jiffies
3034 (priv->last_scan_jiffies, jiffies)));
3035
3036 priv->last_scan_jiffies = jiffies;
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08003037 priv->next_scan_jiffies = 0;
Zhu Yib481de92007-09-25 17:54:57 -07003038}
3039
3040/* Service SCAN_COMPLETE_NOTIFICATION (0x84) */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003041static void iwl4965_rx_scan_complete_notif(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003042 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003043{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003044 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
3045 struct iwl4965_scancomplete_notification *scan_notif = (void *)pkt->u.raw;
Zhu Yib481de92007-09-25 17:54:57 -07003046
3047 IWL_DEBUG_SCAN("Scan complete: %d channels (TSF 0x%08X:%08X) - %d\n",
3048 scan_notif->scanned_channels,
3049 scan_notif->tsf_low,
3050 scan_notif->tsf_high, scan_notif->status);
3051
3052 /* The HW is no longer scanning */
3053 clear_bit(STATUS_SCAN_HW, &priv->status);
3054
3055 /* The scan completion notification came in, so kill that timer... */
3056 cancel_delayed_work(&priv->scan_check);
3057
3058 IWL_DEBUG_INFO("Scan pass on %sGHz took %dms\n",
3059 (priv->scan_bands == 2) ? "2.4" : "5.2",
3060 jiffies_to_msecs(elapsed_jiffies
3061 (priv->scan_pass_start, jiffies)));
3062
3063 /* Remove this scanned band from the list
3064 * of pending bands to scan */
3065 priv->scan_bands--;
3066
3067 /* If a request to abort was given, or the scan did not succeed
3068 * then we reset the scan state machine and terminate,
3069 * re-queuing another scan if one has been requested */
3070 if (test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
3071 IWL_DEBUG_INFO("Aborted scan completed.\n");
3072 clear_bit(STATUS_SCAN_ABORTING, &priv->status);
3073 } else {
3074 /* If there are more bands on this scan pass reschedule */
3075 if (priv->scan_bands > 0)
3076 goto reschedule;
3077 }
3078
3079 priv->last_scan_jiffies = jiffies;
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08003080 priv->next_scan_jiffies = 0;
Zhu Yib481de92007-09-25 17:54:57 -07003081 IWL_DEBUG_INFO("Setting scan to off\n");
3082
3083 clear_bit(STATUS_SCANNING, &priv->status);
3084
3085 IWL_DEBUG_INFO("Scan took %dms\n",
3086 jiffies_to_msecs(elapsed_jiffies(priv->scan_start, jiffies)));
3087
3088 queue_work(priv->workqueue, &priv->scan_completed);
3089
3090 return;
3091
3092reschedule:
3093 priv->scan_pass_start = jiffies;
3094 queue_work(priv->workqueue, &priv->request_scan);
3095}
3096
3097/* Handle notification from uCode that card's power state is changing
3098 * due to software, hardware, or critical temperature RFKILL */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003099static void iwl4965_rx_card_state_notif(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003100 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003101{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003102 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07003103 u32 flags = le32_to_cpu(pkt->u.card_state_notif.flags);
3104 unsigned long status = priv->status;
3105
3106 IWL_DEBUG_RF_KILL("Card state received: HW:%s SW:%s\n",
3107 (flags & HW_CARD_DISABLED) ? "Kill" : "On",
3108 (flags & SW_CARD_DISABLED) ? "Kill" : "On");
3109
3110 if (flags & (SW_CARD_DISABLED | HW_CARD_DISABLED |
3111 RF_CARD_DISABLED)) {
3112
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003113 iwl_write32(priv, CSR_UCODE_DRV_GP1_SET,
Zhu Yib481de92007-09-25 17:54:57 -07003114 CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
3115
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003116 if (!iwl_grab_nic_access(priv)) {
3117 iwl_write_direct32(
Zhu Yib481de92007-09-25 17:54:57 -07003118 priv, HBUS_TARG_MBX_C,
3119 HBUS_TARG_MBX_C_REG_BIT_CMD_BLOCKED);
3120
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003121 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003122 }
3123
3124 if (!(flags & RXON_CARD_DISABLED)) {
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003125 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR,
Zhu Yib481de92007-09-25 17:54:57 -07003126 CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003127 if (!iwl_grab_nic_access(priv)) {
3128 iwl_write_direct32(
Zhu Yib481de92007-09-25 17:54:57 -07003129 priv, HBUS_TARG_MBX_C,
3130 HBUS_TARG_MBX_C_REG_BIT_CMD_BLOCKED);
3131
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003132 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003133 }
3134 }
3135
3136 if (flags & RF_CARD_DISABLED) {
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003137 iwl_write32(priv, CSR_UCODE_DRV_GP1_SET,
Zhu Yib481de92007-09-25 17:54:57 -07003138 CSR_UCODE_DRV_GP1_REG_BIT_CT_KILL_EXIT);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003139 iwl_read32(priv, CSR_UCODE_DRV_GP1);
3140 if (!iwl_grab_nic_access(priv))
3141 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003142 }
3143 }
3144
3145 if (flags & HW_CARD_DISABLED)
3146 set_bit(STATUS_RF_KILL_HW, &priv->status);
3147 else
3148 clear_bit(STATUS_RF_KILL_HW, &priv->status);
3149
3150
3151 if (flags & SW_CARD_DISABLED)
3152 set_bit(STATUS_RF_KILL_SW, &priv->status);
3153 else
3154 clear_bit(STATUS_RF_KILL_SW, &priv->status);
3155
3156 if (!(flags & RXON_CARD_DISABLED))
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003157 iwl4965_scan_cancel(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003158
3159 if ((test_bit(STATUS_RF_KILL_HW, &status) !=
3160 test_bit(STATUS_RF_KILL_HW, &priv->status)) ||
3161 (test_bit(STATUS_RF_KILL_SW, &status) !=
3162 test_bit(STATUS_RF_KILL_SW, &priv->status)))
3163 queue_work(priv->workqueue, &priv->rf_kill);
3164 else
3165 wake_up_interruptible(&priv->wait_command_queue);
3166}
3167
3168/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003169 * iwl4965_setup_rx_handlers - Initialize Rx handler callbacks
Zhu Yib481de92007-09-25 17:54:57 -07003170 *
3171 * Setup the RX handlers for each of the reply types sent from the uCode
3172 * to the host.
3173 *
3174 * This function chains into the hardware specific files for them to setup
3175 * any hardware specific handlers as well.
3176 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003177static void iwl4965_setup_rx_handlers(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003178{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003179 priv->rx_handlers[REPLY_ALIVE] = iwl4965_rx_reply_alive;
3180 priv->rx_handlers[REPLY_ADD_STA] = iwl4965_rx_reply_add_sta;
3181 priv->rx_handlers[REPLY_ERROR] = iwl4965_rx_reply_error;
3182 priv->rx_handlers[CHANNEL_SWITCH_NOTIFICATION] = iwl4965_rx_csa;
Zhu Yib481de92007-09-25 17:54:57 -07003183 priv->rx_handlers[SPECTRUM_MEASURE_NOTIFICATION] =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003184 iwl4965_rx_spectrum_measure_notif;
3185 priv->rx_handlers[PM_SLEEP_NOTIFICATION] = iwl4965_rx_pm_sleep_notif;
Zhu Yib481de92007-09-25 17:54:57 -07003186 priv->rx_handlers[PM_DEBUG_STATISTIC_NOTIFIC] =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003187 iwl4965_rx_pm_debug_statistics_notif;
3188 priv->rx_handlers[BEACON_NOTIFICATION] = iwl4965_rx_beacon_notif;
Zhu Yib481de92007-09-25 17:54:57 -07003189
Ben Cahill9fbab512007-11-29 11:09:47 +08003190 /*
3191 * The same handler is used for both the REPLY to a discrete
3192 * statistics request from the host as well as for the periodic
3193 * statistics notifications (after received beacons) from the uCode.
Zhu Yib481de92007-09-25 17:54:57 -07003194 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003195 priv->rx_handlers[REPLY_STATISTICS_CMD] = iwl4965_hw_rx_statistics;
3196 priv->rx_handlers[STATISTICS_NOTIFICATION] = iwl4965_hw_rx_statistics;
Zhu Yib481de92007-09-25 17:54:57 -07003197
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003198 priv->rx_handlers[REPLY_SCAN_CMD] = iwl4965_rx_reply_scan;
3199 priv->rx_handlers[SCAN_START_NOTIFICATION] = iwl4965_rx_scan_start_notif;
Zhu Yib481de92007-09-25 17:54:57 -07003200 priv->rx_handlers[SCAN_RESULTS_NOTIFICATION] =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003201 iwl4965_rx_scan_results_notif;
Zhu Yib481de92007-09-25 17:54:57 -07003202 priv->rx_handlers[SCAN_COMPLETE_NOTIFICATION] =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003203 iwl4965_rx_scan_complete_notif;
3204 priv->rx_handlers[CARD_STATE_NOTIFICATION] = iwl4965_rx_card_state_notif;
3205 priv->rx_handlers[REPLY_TX] = iwl4965_rx_reply_tx;
Zhu Yib481de92007-09-25 17:54:57 -07003206
Ben Cahill9fbab512007-11-29 11:09:47 +08003207 /* Set up hardware specific Rx handlers */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003208 iwl4965_hw_rx_handler_setup(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003209}
3210
3211/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003212 * iwl4965_tx_cmd_complete - Pull unused buffers off the queue and reclaim them
Zhu Yib481de92007-09-25 17:54:57 -07003213 * @rxb: Rx buffer to reclaim
3214 *
3215 * If an Rx buffer has an async callback associated with it the callback
3216 * will be executed. The attached skb (if present) will only be freed
3217 * if the callback returns 1
3218 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003219static void iwl4965_tx_cmd_complete(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003220 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003221{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003222 struct iwl4965_rx_packet *pkt = (struct iwl4965_rx_packet *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07003223 u16 sequence = le16_to_cpu(pkt->hdr.sequence);
3224 int txq_id = SEQ_TO_QUEUE(sequence);
3225 int index = SEQ_TO_INDEX(sequence);
3226 int huge = sequence & SEQ_HUGE_FRAME;
3227 int cmd_index;
Tomas Winkler857485c2008-03-21 13:53:44 -07003228 struct iwl_cmd *cmd;
Zhu Yib481de92007-09-25 17:54:57 -07003229
3230 /* If a Tx command is being handled and it isn't in the actual
3231 * command queue then there a command routing bug has been introduced
3232 * in the queue management code. */
3233 if (txq_id != IWL_CMD_QUEUE_NUM)
3234 IWL_ERROR("Error wrong command queue %d command id 0x%X\n",
3235 txq_id, pkt->hdr.cmd);
3236 BUG_ON(txq_id != IWL_CMD_QUEUE_NUM);
3237
3238 cmd_index = get_cmd_index(&priv->txq[IWL_CMD_QUEUE_NUM].q, index, huge);
3239 cmd = &priv->txq[IWL_CMD_QUEUE_NUM].cmd[cmd_index];
3240
3241 /* Input error checking is done when commands are added to queue. */
3242 if (cmd->meta.flags & CMD_WANT_SKB) {
3243 cmd->meta.source->u.skb = rxb->skb;
3244 rxb->skb = NULL;
3245 } else if (cmd->meta.u.callback &&
3246 !cmd->meta.u.callback(priv, cmd, rxb->skb))
3247 rxb->skb = NULL;
3248
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003249 iwl4965_tx_queue_reclaim(priv, txq_id, index);
Zhu Yib481de92007-09-25 17:54:57 -07003250
3251 if (!(cmd->meta.flags & CMD_ASYNC)) {
3252 clear_bit(STATUS_HCMD_ACTIVE, &priv->status);
3253 wake_up_interruptible(&priv->wait_command_queue);
3254 }
3255}
3256
3257/************************** RX-FUNCTIONS ****************************/
3258/*
3259 * Rx theory of operation
3260 *
Ben Cahill9fbab512007-11-29 11:09:47 +08003261 * Driver allocates a circular buffer of Receive Buffer Descriptors (RBDs),
3262 * each of which point to Receive Buffers to be filled by 4965. These get
3263 * used not only for Rx frames, but for any command response or notification
3264 * from the 4965. The driver and 4965 manage the Rx buffers by means
3265 * of indexes into the circular buffer.
Zhu Yib481de92007-09-25 17:54:57 -07003266 *
3267 * Rx Queue Indexes
3268 * The host/firmware share two index registers for managing the Rx buffers.
3269 *
3270 * The READ index maps to the first position that the firmware may be writing
3271 * to -- the driver can read up to (but not including) this position and get
3272 * good data.
3273 * The READ index is managed by the firmware once the card is enabled.
3274 *
3275 * The WRITE index maps to the last position the driver has read from -- the
3276 * position preceding WRITE is the last slot the firmware can place a packet.
3277 *
3278 * The queue is empty (no good data) if WRITE = READ - 1, and is full if
3279 * WRITE = READ.
3280 *
Ben Cahill9fbab512007-11-29 11:09:47 +08003281 * During initialization, the host sets up the READ queue position to the first
Zhu Yib481de92007-09-25 17:54:57 -07003282 * INDEX position, and WRITE to the last (READ - 1 wrapped)
3283 *
Ben Cahill9fbab512007-11-29 11:09:47 +08003284 * When the firmware places a packet in a buffer, it will advance the READ index
Zhu Yib481de92007-09-25 17:54:57 -07003285 * and fire the RX interrupt. The driver can then query the READ index and
3286 * process as many packets as possible, moving the WRITE index forward as it
3287 * resets the Rx queue buffers with new memory.
3288 *
3289 * The management in the driver is as follows:
3290 * + A list of pre-allocated SKBs is stored in iwl->rxq->rx_free. When
3291 * iwl->rxq->free_count drops to or below RX_LOW_WATERMARK, work is scheduled
Ian Schram01ebd062007-10-25 17:15:22 +08003292 * to replenish the iwl->rxq->rx_free.
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003293 * + In iwl4965_rx_replenish (scheduled) if 'processed' != 'read' then the
Zhu Yib481de92007-09-25 17:54:57 -07003294 * iwl->rxq is replenished and the READ INDEX is updated (updating the
3295 * 'processed' and 'read' driver indexes as well)
3296 * + A received packet is processed and handed to the kernel network stack,
3297 * detached from the iwl->rxq. The driver 'processed' index is updated.
3298 * + The Host/Firmware iwl->rxq is replenished at tasklet time from the rx_free
3299 * list. If there are no allocated buffers in iwl->rxq->rx_free, the READ
3300 * INDEX is not incremented and iwl->status(RX_STALLED) is set. If there
3301 * were enough free buffers and RX_STALLED is set it is cleared.
3302 *
3303 *
3304 * Driver sequence:
3305 *
Ben Cahill9fbab512007-11-29 11:09:47 +08003306 * iwl4965_rx_queue_alloc() Allocates rx_free
3307 * iwl4965_rx_replenish() Replenishes rx_free list from rx_used, and calls
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003308 * iwl4965_rx_queue_restock
Ben Cahill9fbab512007-11-29 11:09:47 +08003309 * iwl4965_rx_queue_restock() Moves available buffers from rx_free into Rx
Zhu Yib481de92007-09-25 17:54:57 -07003310 * queue, updates firmware pointers, and updates
3311 * the WRITE index. If insufficient rx_free buffers
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003312 * are available, schedules iwl4965_rx_replenish
Zhu Yib481de92007-09-25 17:54:57 -07003313 *
3314 * -- enable interrupts --
Ben Cahill9fbab512007-11-29 11:09:47 +08003315 * ISR - iwl4965_rx() Detach iwl4965_rx_mem_buffers from pool up to the
Zhu Yib481de92007-09-25 17:54:57 -07003316 * READ INDEX, detaching the SKB from the pool.
3317 * Moves the packet buffer from queue to rx_used.
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003318 * Calls iwl4965_rx_queue_restock to refill any empty
Zhu Yib481de92007-09-25 17:54:57 -07003319 * slots.
3320 * ...
3321 *
3322 */
3323
3324/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003325 * iwl4965_rx_queue_space - Return number of free slots available in queue.
Zhu Yib481de92007-09-25 17:54:57 -07003326 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003327static int iwl4965_rx_queue_space(const struct iwl4965_rx_queue *q)
Zhu Yib481de92007-09-25 17:54:57 -07003328{
3329 int s = q->read - q->write;
3330 if (s <= 0)
3331 s += RX_QUEUE_SIZE;
3332 /* keep some buffer to not confuse full and empty queue */
3333 s -= 2;
3334 if (s < 0)
3335 s = 0;
3336 return s;
3337}
3338
3339/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003340 * iwl4965_rx_queue_update_write_ptr - Update the write pointer for the RX queue
Zhu Yib481de92007-09-25 17:54:57 -07003341 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003342int iwl4965_rx_queue_update_write_ptr(struct iwl_priv *priv, struct iwl4965_rx_queue *q)
Zhu Yib481de92007-09-25 17:54:57 -07003343{
3344 u32 reg = 0;
3345 int rc = 0;
3346 unsigned long flags;
3347
3348 spin_lock_irqsave(&q->lock, flags);
3349
3350 if (q->need_update == 0)
3351 goto exit_unlock;
3352
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003353 /* If power-saving is in use, make sure device is awake */
Zhu Yib481de92007-09-25 17:54:57 -07003354 if (test_bit(STATUS_POWER_PMI, &priv->status)) {
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003355 reg = iwl_read32(priv, CSR_UCODE_DRV_GP1);
Zhu Yib481de92007-09-25 17:54:57 -07003356
3357 if (reg & CSR_UCODE_DRV_GP1_BIT_MAC_SLEEP) {
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003358 iwl_set_bit(priv, CSR_GP_CNTRL,
Zhu Yib481de92007-09-25 17:54:57 -07003359 CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
3360 goto exit_unlock;
3361 }
3362
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003363 rc = iwl_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003364 if (rc)
3365 goto exit_unlock;
3366
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003367 /* Device expects a multiple of 8 */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003368 iwl_write_direct32(priv, FH_RSCSR_CHNL0_WPTR,
Zhu Yib481de92007-09-25 17:54:57 -07003369 q->write & ~0x7);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003370 iwl_release_nic_access(priv);
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003371
3372 /* Else device is assumed to be awake */
Zhu Yib481de92007-09-25 17:54:57 -07003373 } else
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003374 /* Device expects a multiple of 8 */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003375 iwl_write32(priv, FH_RSCSR_CHNL0_WPTR, q->write & ~0x7);
Zhu Yib481de92007-09-25 17:54:57 -07003376
3377
3378 q->need_update = 0;
3379
3380 exit_unlock:
3381 spin_unlock_irqrestore(&q->lock, flags);
3382 return rc;
3383}
3384
3385/**
Ben Cahill9fbab512007-11-29 11:09:47 +08003386 * iwl4965_dma_addr2rbd_ptr - convert a DMA address to a uCode read buffer ptr
Zhu Yib481de92007-09-25 17:54:57 -07003387 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003388static inline __le32 iwl4965_dma_addr2rbd_ptr(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07003389 dma_addr_t dma_addr)
3390{
3391 return cpu_to_le32((u32)(dma_addr >> 8));
3392}
3393
3394
3395/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003396 * iwl4965_rx_queue_restock - refill RX queue from pre-allocated pool
Zhu Yib481de92007-09-25 17:54:57 -07003397 *
Ben Cahill9fbab512007-11-29 11:09:47 +08003398 * If there are slots in the RX queue that need to be restocked,
Zhu Yib481de92007-09-25 17:54:57 -07003399 * and we have free pre-allocated buffers, fill the ranks as much
Ben Cahill9fbab512007-11-29 11:09:47 +08003400 * as we can, pulling from rx_free.
Zhu Yib481de92007-09-25 17:54:57 -07003401 *
3402 * This moves the 'write' index forward to catch up with 'processed', and
3403 * also updates the memory address in the firmware to reference the new
3404 * target buffer.
3405 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003406static int iwl4965_rx_queue_restock(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003407{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003408 struct iwl4965_rx_queue *rxq = &priv->rxq;
Zhu Yib481de92007-09-25 17:54:57 -07003409 struct list_head *element;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003410 struct iwl4965_rx_mem_buffer *rxb;
Zhu Yib481de92007-09-25 17:54:57 -07003411 unsigned long flags;
3412 int write, rc;
3413
3414 spin_lock_irqsave(&rxq->lock, flags);
3415 write = rxq->write & ~0x7;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003416 while ((iwl4965_rx_queue_space(rxq) > 0) && (rxq->free_count)) {
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003417 /* Get next free Rx buffer, remove from free list */
Zhu Yib481de92007-09-25 17:54:57 -07003418 element = rxq->rx_free.next;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003419 rxb = list_entry(element, struct iwl4965_rx_mem_buffer, list);
Zhu Yib481de92007-09-25 17:54:57 -07003420 list_del(element);
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003421
3422 /* Point to Rx buffer via next RBD in circular buffer */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003423 rxq->bd[rxq->write] = iwl4965_dma_addr2rbd_ptr(priv, rxb->dma_addr);
Zhu Yib481de92007-09-25 17:54:57 -07003424 rxq->queue[rxq->write] = rxb;
3425 rxq->write = (rxq->write + 1) & RX_QUEUE_MASK;
3426 rxq->free_count--;
3427 }
3428 spin_unlock_irqrestore(&rxq->lock, flags);
3429 /* If the pre-allocated buffer pool is dropping low, schedule to
3430 * refill it */
3431 if (rxq->free_count <= RX_LOW_WATERMARK)
3432 queue_work(priv->workqueue, &priv->rx_replenish);
3433
3434
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003435 /* If we've added more space for the firmware to place data, tell it.
3436 * Increment device's write pointer in multiples of 8. */
Zhu Yib481de92007-09-25 17:54:57 -07003437 if ((write != (rxq->write & ~0x7))
3438 || (abs(rxq->write - rxq->read) > 7)) {
3439 spin_lock_irqsave(&rxq->lock, flags);
3440 rxq->need_update = 1;
3441 spin_unlock_irqrestore(&rxq->lock, flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003442 rc = iwl4965_rx_queue_update_write_ptr(priv, rxq);
Zhu Yib481de92007-09-25 17:54:57 -07003443 if (rc)
3444 return rc;
3445 }
3446
3447 return 0;
3448}
3449
3450/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003451 * iwl4965_rx_replenish - Move all used packet from rx_used to rx_free
Zhu Yib481de92007-09-25 17:54:57 -07003452 *
3453 * When moving to rx_free an SKB is allocated for the slot.
3454 *
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003455 * Also restock the Rx queue via iwl4965_rx_queue_restock.
Ian Schram01ebd062007-10-25 17:15:22 +08003456 * This is called as a scheduled work item (except for during initialization)
Zhu Yib481de92007-09-25 17:54:57 -07003457 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003458static void iwl4965_rx_allocate(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003459{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003460 struct iwl4965_rx_queue *rxq = &priv->rxq;
Zhu Yib481de92007-09-25 17:54:57 -07003461 struct list_head *element;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003462 struct iwl4965_rx_mem_buffer *rxb;
Zhu Yib481de92007-09-25 17:54:57 -07003463 unsigned long flags;
3464 spin_lock_irqsave(&rxq->lock, flags);
3465 while (!list_empty(&rxq->rx_used)) {
3466 element = rxq->rx_used.next;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003467 rxb = list_entry(element, struct iwl4965_rx_mem_buffer, list);
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003468
3469 /* Alloc a new receive buffer */
Zhu Yib481de92007-09-25 17:54:57 -07003470 rxb->skb =
Tomas Winkler5425e492008-04-15 16:01:38 -07003471 alloc_skb(priv->hw_params.rx_buf_size,
Ron Rindjunsky9ee1ba42007-11-26 16:14:42 +02003472 __GFP_NOWARN | GFP_ATOMIC);
Zhu Yib481de92007-09-25 17:54:57 -07003473 if (!rxb->skb) {
3474 if (net_ratelimit())
3475 printk(KERN_CRIT DRV_NAME
3476 ": Can not allocate SKB buffers\n");
3477 /* We don't reschedule replenish work here -- we will
3478 * call the restock method and if it still needs
3479 * more buffers it will schedule replenish */
3480 break;
3481 }
3482 priv->alloc_rxb_skb++;
3483 list_del(element);
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003484
3485 /* Get physical address of RB/SKB */
Zhu Yib481de92007-09-25 17:54:57 -07003486 rxb->dma_addr =
3487 pci_map_single(priv->pci_dev, rxb->skb->data,
Tomas Winkler5425e492008-04-15 16:01:38 -07003488 priv->hw_params.rx_buf_size, PCI_DMA_FROMDEVICE);
Zhu Yib481de92007-09-25 17:54:57 -07003489 list_add_tail(&rxb->list, &rxq->rx_free);
3490 rxq->free_count++;
3491 }
3492 spin_unlock_irqrestore(&rxq->lock, flags);
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08003493}
3494
3495/*
3496 * this should be called while priv->lock is locked
3497*/
Tomas Winkler4fd1f842007-12-05 20:59:58 +02003498static void __iwl4965_rx_replenish(void *data)
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08003499{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003500 struct iwl_priv *priv = data;
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08003501
3502 iwl4965_rx_allocate(priv);
3503 iwl4965_rx_queue_restock(priv);
3504}
3505
3506
3507void iwl4965_rx_replenish(void *data)
3508{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003509 struct iwl_priv *priv = data;
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08003510 unsigned long flags;
3511
3512 iwl4965_rx_allocate(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003513
3514 spin_lock_irqsave(&priv->lock, flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003515 iwl4965_rx_queue_restock(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003516 spin_unlock_irqrestore(&priv->lock, flags);
3517}
3518
3519/* Assumes that the skb field of the buffers in 'pool' is kept accurate.
Ben Cahill9fbab512007-11-29 11:09:47 +08003520 * If an SKB has been detached, the POOL needs to have its SKB set to NULL
Zhu Yib481de92007-09-25 17:54:57 -07003521 * This free routine walks the list of POOL entries and if SKB is set to
3522 * non NULL it is unmapped and freed
3523 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003524static void iwl4965_rx_queue_free(struct iwl_priv *priv, struct iwl4965_rx_queue *rxq)
Zhu Yib481de92007-09-25 17:54:57 -07003525{
3526 int i;
3527 for (i = 0; i < RX_QUEUE_SIZE + RX_FREE_BUFFERS; i++) {
3528 if (rxq->pool[i].skb != NULL) {
3529 pci_unmap_single(priv->pci_dev,
3530 rxq->pool[i].dma_addr,
Tomas Winkler5425e492008-04-15 16:01:38 -07003531 priv->hw_params.rx_buf_size,
Ron Rindjunsky9ee1ba42007-11-26 16:14:42 +02003532 PCI_DMA_FROMDEVICE);
Zhu Yib481de92007-09-25 17:54:57 -07003533 dev_kfree_skb(rxq->pool[i].skb);
3534 }
3535 }
3536
3537 pci_free_consistent(priv->pci_dev, 4 * RX_QUEUE_SIZE, rxq->bd,
3538 rxq->dma_addr);
3539 rxq->bd = NULL;
3540}
3541
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003542int iwl4965_rx_queue_alloc(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003543{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003544 struct iwl4965_rx_queue *rxq = &priv->rxq;
Zhu Yib481de92007-09-25 17:54:57 -07003545 struct pci_dev *dev = priv->pci_dev;
3546 int i;
3547
3548 spin_lock_init(&rxq->lock);
3549 INIT_LIST_HEAD(&rxq->rx_free);
3550 INIT_LIST_HEAD(&rxq->rx_used);
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003551
3552 /* Alloc the circular buffer of Read Buffer Descriptors (RBDs) */
Zhu Yib481de92007-09-25 17:54:57 -07003553 rxq->bd = pci_alloc_consistent(dev, 4 * RX_QUEUE_SIZE, &rxq->dma_addr);
3554 if (!rxq->bd)
3555 return -ENOMEM;
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003556
Zhu Yib481de92007-09-25 17:54:57 -07003557 /* Fill the rx_used queue with _all_ of the Rx buffers */
3558 for (i = 0; i < RX_FREE_BUFFERS + RX_QUEUE_SIZE; i++)
3559 list_add_tail(&rxq->pool[i].list, &rxq->rx_used);
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003560
Zhu Yib481de92007-09-25 17:54:57 -07003561 /* Set us so that we have processed and used all buffers, but have
3562 * not restocked the Rx queue with fresh buffers */
3563 rxq->read = rxq->write = 0;
3564 rxq->free_count = 0;
3565 rxq->need_update = 0;
3566 return 0;
3567}
3568
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003569void iwl4965_rx_queue_reset(struct iwl_priv *priv, struct iwl4965_rx_queue *rxq)
Zhu Yib481de92007-09-25 17:54:57 -07003570{
3571 unsigned long flags;
3572 int i;
3573 spin_lock_irqsave(&rxq->lock, flags);
3574 INIT_LIST_HEAD(&rxq->rx_free);
3575 INIT_LIST_HEAD(&rxq->rx_used);
3576 /* Fill the rx_used queue with _all_ of the Rx buffers */
3577 for (i = 0; i < RX_FREE_BUFFERS + RX_QUEUE_SIZE; i++) {
3578 /* In the reset function, these buffers may have been allocated
3579 * to an SKB, so we need to unmap and free potential storage */
3580 if (rxq->pool[i].skb != NULL) {
3581 pci_unmap_single(priv->pci_dev,
3582 rxq->pool[i].dma_addr,
Tomas Winkler5425e492008-04-15 16:01:38 -07003583 priv->hw_params.rx_buf_size,
Ron Rindjunsky9ee1ba42007-11-26 16:14:42 +02003584 PCI_DMA_FROMDEVICE);
Zhu Yib481de92007-09-25 17:54:57 -07003585 priv->alloc_rxb_skb--;
3586 dev_kfree_skb(rxq->pool[i].skb);
3587 rxq->pool[i].skb = NULL;
3588 }
3589 list_add_tail(&rxq->pool[i].list, &rxq->rx_used);
3590 }
3591
3592 /* Set us so that we have processed and used all buffers, but have
3593 * not restocked the Rx queue with fresh buffers */
3594 rxq->read = rxq->write = 0;
3595 rxq->free_count = 0;
3596 spin_unlock_irqrestore(&rxq->lock, flags);
3597}
3598
3599/* Convert linear signal-to-noise ratio into dB */
3600static u8 ratio2dB[100] = {
3601/* 0 1 2 3 4 5 6 7 8 9 */
3602 0, 0, 6, 10, 12, 14, 16, 17, 18, 19, /* 00 - 09 */
3603 20, 21, 22, 22, 23, 23, 24, 25, 26, 26, /* 10 - 19 */
3604 26, 26, 26, 27, 27, 28, 28, 28, 29, 29, /* 20 - 29 */
3605 29, 30, 30, 30, 31, 31, 31, 31, 32, 32, /* 30 - 39 */
3606 32, 32, 32, 33, 33, 33, 33, 33, 34, 34, /* 40 - 49 */
3607 34, 34, 34, 34, 35, 35, 35, 35, 35, 35, /* 50 - 59 */
3608 36, 36, 36, 36, 36, 36, 36, 37, 37, 37, /* 60 - 69 */
3609 37, 37, 37, 37, 37, 38, 38, 38, 38, 38, /* 70 - 79 */
3610 38, 38, 38, 38, 38, 39, 39, 39, 39, 39, /* 80 - 89 */
3611 39, 39, 39, 39, 39, 40, 40, 40, 40, 40 /* 90 - 99 */
3612};
3613
3614/* Calculates a relative dB value from a ratio of linear
3615 * (i.e. not dB) signal levels.
3616 * Conversion assumes that levels are voltages (20*log), not powers (10*log). */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003617int iwl4965_calc_db_from_ratio(int sig_ratio)
Zhu Yib481de92007-09-25 17:54:57 -07003618{
Adrian Bunkc899a572007-10-14 19:51:15 +02003619 /* 1000:1 or higher just report as 60 dB */
3620 if (sig_ratio >= 1000)
Zhu Yib481de92007-09-25 17:54:57 -07003621 return 60;
3622
Adrian Bunkc899a572007-10-14 19:51:15 +02003623 /* 100:1 or higher, divide by 10 and use table,
Zhu Yib481de92007-09-25 17:54:57 -07003624 * add 20 dB to make up for divide by 10 */
Adrian Bunkc899a572007-10-14 19:51:15 +02003625 if (sig_ratio >= 100)
Zhu Yib481de92007-09-25 17:54:57 -07003626 return (20 + (int)ratio2dB[sig_ratio/10]);
3627
3628 /* We shouldn't see this */
3629 if (sig_ratio < 1)
3630 return 0;
3631
3632 /* Use table for ratios 1:1 - 99:1 */
3633 return (int)ratio2dB[sig_ratio];
3634}
3635
3636#define PERFECT_RSSI (-20) /* dBm */
3637#define WORST_RSSI (-95) /* dBm */
3638#define RSSI_RANGE (PERFECT_RSSI - WORST_RSSI)
3639
3640/* Calculate an indication of rx signal quality (a percentage, not dBm!).
3641 * See http://www.ces.clemson.edu/linux/signal_quality.shtml for info
3642 * about formulas used below. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003643int iwl4965_calc_sig_qual(int rssi_dbm, int noise_dbm)
Zhu Yib481de92007-09-25 17:54:57 -07003644{
3645 int sig_qual;
3646 int degradation = PERFECT_RSSI - rssi_dbm;
3647
3648 /* If we get a noise measurement, use signal-to-noise ratio (SNR)
3649 * as indicator; formula is (signal dbm - noise dbm).
3650 * SNR at or above 40 is a great signal (100%).
3651 * Below that, scale to fit SNR of 0 - 40 dB within 0 - 100% indicator.
3652 * Weakest usable signal is usually 10 - 15 dB SNR. */
3653 if (noise_dbm) {
3654 if (rssi_dbm - noise_dbm >= 40)
3655 return 100;
3656 else if (rssi_dbm < noise_dbm)
3657 return 0;
3658 sig_qual = ((rssi_dbm - noise_dbm) * 5) / 2;
3659
3660 /* Else use just the signal level.
3661 * This formula is a least squares fit of data points collected and
3662 * compared with a reference system that had a percentage (%) display
3663 * for signal quality. */
3664 } else
3665 sig_qual = (100 * (RSSI_RANGE * RSSI_RANGE) - degradation *
3666 (15 * RSSI_RANGE + 62 * degradation)) /
3667 (RSSI_RANGE * RSSI_RANGE);
3668
3669 if (sig_qual > 100)
3670 sig_qual = 100;
3671 else if (sig_qual < 1)
3672 sig_qual = 0;
3673
3674 return sig_qual;
3675}
3676
3677/**
Ben Cahill9fbab512007-11-29 11:09:47 +08003678 * iwl4965_rx_handle - Main entry function for receiving responses from uCode
Zhu Yib481de92007-09-25 17:54:57 -07003679 *
3680 * Uses the priv->rx_handlers callback function array to invoke
3681 * the appropriate handlers, including command responses,
3682 * frame-received notifications, and other notifications.
3683 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003684static void iwl4965_rx_handle(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003685{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003686 struct iwl4965_rx_mem_buffer *rxb;
3687 struct iwl4965_rx_packet *pkt;
3688 struct iwl4965_rx_queue *rxq = &priv->rxq;
Zhu Yib481de92007-09-25 17:54:57 -07003689 u32 r, i;
3690 int reclaim;
3691 unsigned long flags;
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08003692 u8 fill_rx = 0;
Mohamed Abbasd68ab682008-02-07 13:16:33 -08003693 u32 count = 8;
Zhu Yib481de92007-09-25 17:54:57 -07003694
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003695 /* uCode's read index (stored in shared DRAM) indicates the last Rx
3696 * buffer that the driver may process (last buffer filled by ucode). */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003697 r = iwl4965_hw_get_rx_read(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003698 i = rxq->read;
3699
3700 /* Rx interrupt, but nothing sent from uCode */
3701 if (i == r)
3702 IWL_DEBUG(IWL_DL_RX | IWL_DL_ISR, "r = %d, i = %d\n", r, i);
3703
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08003704 if (iwl4965_rx_queue_space(rxq) > (RX_QUEUE_SIZE / 2))
3705 fill_rx = 1;
3706
Zhu Yib481de92007-09-25 17:54:57 -07003707 while (i != r) {
3708 rxb = rxq->queue[i];
3709
Ben Cahill9fbab512007-11-29 11:09:47 +08003710 /* If an RXB doesn't have a Rx queue slot associated with it,
Zhu Yib481de92007-09-25 17:54:57 -07003711 * then a bug has been introduced in the queue refilling
3712 * routines -- catch it here */
3713 BUG_ON(rxb == NULL);
3714
3715 rxq->queue[i] = NULL;
3716
3717 pci_dma_sync_single_for_cpu(priv->pci_dev, rxb->dma_addr,
Tomas Winkler5425e492008-04-15 16:01:38 -07003718 priv->hw_params.rx_buf_size,
Zhu Yib481de92007-09-25 17:54:57 -07003719 PCI_DMA_FROMDEVICE);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003720 pkt = (struct iwl4965_rx_packet *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07003721
3722 /* Reclaim a command buffer only if this packet is a response
3723 * to a (driver-originated) command.
3724 * If the packet (e.g. Rx frame) originated from uCode,
3725 * there is no command buffer to reclaim.
3726 * Ucode should set SEQ_RX_FRAME bit if ucode-originated,
3727 * but apparently a few don't get set; catch them here. */
3728 reclaim = !(pkt->hdr.sequence & SEQ_RX_FRAME) &&
3729 (pkt->hdr.cmd != REPLY_RX_PHY_CMD) &&
Tomas Winkler857485c2008-03-21 13:53:44 -07003730 (pkt->hdr.cmd != REPLY_RX) &&
Zhu Yicfe01702007-09-27 11:27:31 +08003731 (pkt->hdr.cmd != REPLY_COMPRESSED_BA) &&
Zhu Yib481de92007-09-25 17:54:57 -07003732 (pkt->hdr.cmd != STATISTICS_NOTIFICATION) &&
3733 (pkt->hdr.cmd != REPLY_TX);
3734
3735 /* Based on type of command response or notification,
3736 * handle those that need handling via function in
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003737 * rx_handlers table. See iwl4965_setup_rx_handlers() */
Zhu Yib481de92007-09-25 17:54:57 -07003738 if (priv->rx_handlers[pkt->hdr.cmd]) {
3739 IWL_DEBUG(IWL_DL_HOST_COMMAND | IWL_DL_RX | IWL_DL_ISR,
3740 "r = %d, i = %d, %s, 0x%02x\n", r, i,
3741 get_cmd_string(pkt->hdr.cmd), pkt->hdr.cmd);
3742 priv->rx_handlers[pkt->hdr.cmd] (priv, rxb);
3743 } else {
3744 /* No handling needed */
3745 IWL_DEBUG(IWL_DL_HOST_COMMAND | IWL_DL_RX | IWL_DL_ISR,
3746 "r %d i %d No handler needed for %s, 0x%02x\n",
3747 r, i, get_cmd_string(pkt->hdr.cmd),
3748 pkt->hdr.cmd);
3749 }
3750
3751 if (reclaim) {
Ben Cahill9fbab512007-11-29 11:09:47 +08003752 /* Invoke any callbacks, transfer the skb to caller, and
Tomas Winkler857485c2008-03-21 13:53:44 -07003753 * fire off the (possibly) blocking iwl_send_cmd()
Zhu Yib481de92007-09-25 17:54:57 -07003754 * as we reclaim the driver command queue */
3755 if (rxb && rxb->skb)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003756 iwl4965_tx_cmd_complete(priv, rxb);
Zhu Yib481de92007-09-25 17:54:57 -07003757 else
3758 IWL_WARNING("Claim null rxb?\n");
3759 }
3760
3761 /* For now we just don't re-use anything. We can tweak this
3762 * later to try and re-use notification packets and SKBs that
3763 * fail to Rx correctly */
3764 if (rxb->skb != NULL) {
3765 priv->alloc_rxb_skb--;
3766 dev_kfree_skb_any(rxb->skb);
3767 rxb->skb = NULL;
3768 }
3769
3770 pci_unmap_single(priv->pci_dev, rxb->dma_addr,
Tomas Winkler5425e492008-04-15 16:01:38 -07003771 priv->hw_params.rx_buf_size,
Ron Rindjunsky9ee1ba42007-11-26 16:14:42 +02003772 PCI_DMA_FROMDEVICE);
Zhu Yib481de92007-09-25 17:54:57 -07003773 spin_lock_irqsave(&rxq->lock, flags);
3774 list_add_tail(&rxb->list, &priv->rxq.rx_used);
3775 spin_unlock_irqrestore(&rxq->lock, flags);
3776 i = (i + 1) & RX_QUEUE_MASK;
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08003777 /* If there are a lot of unused frames,
3778 * restock the Rx queue so ucode wont assert. */
3779 if (fill_rx) {
3780 count++;
3781 if (count >= 8) {
3782 priv->rxq.read = i;
3783 __iwl4965_rx_replenish(priv);
3784 count = 0;
3785 }
3786 }
Zhu Yib481de92007-09-25 17:54:57 -07003787 }
3788
3789 /* Backtrack one entry */
3790 priv->rxq.read = i;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003791 iwl4965_rx_queue_restock(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003792}
3793
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003794/**
3795 * iwl4965_tx_queue_update_write_ptr - Send new write index to hardware
3796 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003797static int iwl4965_tx_queue_update_write_ptr(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003798 struct iwl4965_tx_queue *txq)
Zhu Yib481de92007-09-25 17:54:57 -07003799{
3800 u32 reg = 0;
3801 int rc = 0;
3802 int txq_id = txq->q.id;
3803
3804 if (txq->need_update == 0)
3805 return rc;
3806
3807 /* if we're trying to save power */
3808 if (test_bit(STATUS_POWER_PMI, &priv->status)) {
3809 /* wake up nic if it's powered down ...
3810 * uCode will wake up, and interrupt us again, so next
3811 * time we'll skip this part. */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003812 reg = iwl_read32(priv, CSR_UCODE_DRV_GP1);
Zhu Yib481de92007-09-25 17:54:57 -07003813
3814 if (reg & CSR_UCODE_DRV_GP1_BIT_MAC_SLEEP) {
3815 IWL_DEBUG_INFO("Requesting wakeup, GP1 = 0x%x\n", reg);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003816 iwl_set_bit(priv, CSR_GP_CNTRL,
Zhu Yib481de92007-09-25 17:54:57 -07003817 CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
3818 return rc;
3819 }
3820
3821 /* restore this queue's parameters in nic hardware. */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003822 rc = iwl_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003823 if (rc)
3824 return rc;
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003825 iwl_write_direct32(priv, HBUS_TARG_WRPTR,
Tomas Winklerfc4b6852007-10-25 17:15:24 +08003826 txq->q.write_ptr | (txq_id << 8));
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003827 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003828
3829 /* else not in power-save mode, uCode will never sleep when we're
3830 * trying to tx (during RFKILL, we're not trying to tx). */
3831 } else
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003832 iwl_write32(priv, HBUS_TARG_WRPTR,
Tomas Winklerfc4b6852007-10-25 17:15:24 +08003833 txq->q.write_ptr | (txq_id << 8));
Zhu Yib481de92007-09-25 17:54:57 -07003834
3835 txq->need_update = 0;
3836
3837 return rc;
3838}
3839
Tomas Winkler0a6857e2008-03-12 16:58:49 -07003840#ifdef CONFIG_IWLWIFI_DEBUG
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003841static void iwl4965_print_rx_config_cmd(struct iwl4965_rxon_cmd *rxon)
Zhu Yib481de92007-09-25 17:54:57 -07003842{
Joe Perches0795af52007-10-03 17:59:30 -07003843 DECLARE_MAC_BUF(mac);
3844
Zhu Yib481de92007-09-25 17:54:57 -07003845 IWL_DEBUG_RADIO("RX CONFIG:\n");
Tomas Winkler0a6857e2008-03-12 16:58:49 -07003846 iwl_print_hex_dump(IWL_DL_RADIO, (u8 *) rxon, sizeof(*rxon));
Zhu Yib481de92007-09-25 17:54:57 -07003847 IWL_DEBUG_RADIO("u16 channel: 0x%x\n", le16_to_cpu(rxon->channel));
3848 IWL_DEBUG_RADIO("u32 flags: 0x%08X\n", le32_to_cpu(rxon->flags));
3849 IWL_DEBUG_RADIO("u32 filter_flags: 0x%08x\n",
3850 le32_to_cpu(rxon->filter_flags));
3851 IWL_DEBUG_RADIO("u8 dev_type: 0x%x\n", rxon->dev_type);
3852 IWL_DEBUG_RADIO("u8 ofdm_basic_rates: 0x%02x\n",
3853 rxon->ofdm_basic_rates);
3854 IWL_DEBUG_RADIO("u8 cck_basic_rates: 0x%02x\n", rxon->cck_basic_rates);
Joe Perches0795af52007-10-03 17:59:30 -07003855 IWL_DEBUG_RADIO("u8[6] node_addr: %s\n",
3856 print_mac(mac, rxon->node_addr));
3857 IWL_DEBUG_RADIO("u8[6] bssid_addr: %s\n",
3858 print_mac(mac, rxon->bssid_addr));
Zhu Yib481de92007-09-25 17:54:57 -07003859 IWL_DEBUG_RADIO("u16 assoc_id: 0x%x\n", le16_to_cpu(rxon->assoc_id));
3860}
3861#endif
3862
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003863static void iwl4965_enable_interrupts(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003864{
3865 IWL_DEBUG_ISR("Enabling interrupts\n");
3866 set_bit(STATUS_INT_ENABLED, &priv->status);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003867 iwl_write32(priv, CSR_INT_MASK, CSR_INI_SET_MASK);
Zhu Yib481de92007-09-25 17:54:57 -07003868}
3869
Mohamed Abbas0359fac2008-03-28 16:21:08 -07003870/* call this function to flush any scheduled tasklet */
3871static inline void iwl_synchronize_irq(struct iwl_priv *priv)
3872{
3873 /* wait to make sure we flush pedding tasklet*/
3874 synchronize_irq(priv->pci_dev->irq);
3875 tasklet_kill(&priv->irq_tasklet);
3876}
3877
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003878static inline void iwl4965_disable_interrupts(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003879{
3880 clear_bit(STATUS_INT_ENABLED, &priv->status);
3881
3882 /* disable interrupts from uCode/NIC to host */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003883 iwl_write32(priv, CSR_INT_MASK, 0x00000000);
Zhu Yib481de92007-09-25 17:54:57 -07003884
3885 /* acknowledge/clear/reset any interrupts still pending
3886 * from uCode or flow handler (Rx/Tx DMA) */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003887 iwl_write32(priv, CSR_INT, 0xffffffff);
3888 iwl_write32(priv, CSR_FH_INT_STATUS, 0xffffffff);
Zhu Yib481de92007-09-25 17:54:57 -07003889 IWL_DEBUG_ISR("Disabled interrupts\n");
3890}
3891
3892static const char *desc_lookup(int i)
3893{
3894 switch (i) {
3895 case 1:
3896 return "FAIL";
3897 case 2:
3898 return "BAD_PARAM";
3899 case 3:
3900 return "BAD_CHECKSUM";
3901 case 4:
3902 return "NMI_INTERRUPT";
3903 case 5:
3904 return "SYSASSERT";
3905 case 6:
3906 return "FATAL_ERROR";
3907 }
3908
3909 return "UNKNOWN";
3910}
3911
3912#define ERROR_START_OFFSET (1 * sizeof(u32))
3913#define ERROR_ELEM_SIZE (7 * sizeof(u32))
3914
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003915static void iwl4965_dump_nic_error_log(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003916{
3917 u32 data2, line;
3918 u32 desc, time, count, base, data1;
3919 u32 blink1, blink2, ilink1, ilink2;
3920 int rc;
3921
3922 base = le32_to_cpu(priv->card_alive.error_event_table_ptr);
3923
Tomas Winkler57aab752008-04-14 21:16:03 -07003924 if (!priv->cfg->ops->lib->is_valid_rtc_data_addr(base)) {
Zhu Yib481de92007-09-25 17:54:57 -07003925 IWL_ERROR("Not valid error log pointer 0x%08X\n", base);
3926 return;
3927 }
3928
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003929 rc = iwl_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003930 if (rc) {
3931 IWL_WARNING("Can not read from adapter at this time.\n");
3932 return;
3933 }
3934
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003935 count = iwl_read_targ_mem(priv, base);
Zhu Yib481de92007-09-25 17:54:57 -07003936
3937 if (ERROR_START_OFFSET <= count * ERROR_ELEM_SIZE) {
3938 IWL_ERROR("Start IWL Error Log Dump:\n");
Tomas Winkler2acae162008-03-02 01:25:59 +02003939 IWL_ERROR("Status: 0x%08lX, count: %d\n", priv->status, count);
Zhu Yib481de92007-09-25 17:54:57 -07003940 }
3941
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003942 desc = iwl_read_targ_mem(priv, base + 1 * sizeof(u32));
3943 blink1 = iwl_read_targ_mem(priv, base + 3 * sizeof(u32));
3944 blink2 = iwl_read_targ_mem(priv, base + 4 * sizeof(u32));
3945 ilink1 = iwl_read_targ_mem(priv, base + 5 * sizeof(u32));
3946 ilink2 = iwl_read_targ_mem(priv, base + 6 * sizeof(u32));
3947 data1 = iwl_read_targ_mem(priv, base + 7 * sizeof(u32));
3948 data2 = iwl_read_targ_mem(priv, base + 8 * sizeof(u32));
3949 line = iwl_read_targ_mem(priv, base + 9 * sizeof(u32));
3950 time = iwl_read_targ_mem(priv, base + 11 * sizeof(u32));
Zhu Yib481de92007-09-25 17:54:57 -07003951
3952 IWL_ERROR("Desc Time "
3953 "data1 data2 line\n");
3954 IWL_ERROR("%-13s (#%d) %010u 0x%08X 0x%08X %u\n",
3955 desc_lookup(desc), desc, time, data1, data2, line);
3956 IWL_ERROR("blink1 blink2 ilink1 ilink2\n");
3957 IWL_ERROR("0x%05X 0x%05X 0x%05X 0x%05X\n", blink1, blink2,
3958 ilink1, ilink2);
3959
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003960 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003961}
3962
3963#define EVENT_START_OFFSET (4 * sizeof(u32))
3964
3965/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003966 * iwl4965_print_event_log - Dump error event log to syslog
Zhu Yib481de92007-09-25 17:54:57 -07003967 *
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003968 * NOTE: Must be called with iwl_grab_nic_access() already obtained!
Zhu Yib481de92007-09-25 17:54:57 -07003969 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003970static void iwl4965_print_event_log(struct iwl_priv *priv, u32 start_idx,
Zhu Yib481de92007-09-25 17:54:57 -07003971 u32 num_events, u32 mode)
3972{
3973 u32 i;
3974 u32 base; /* SRAM byte address of event log header */
3975 u32 event_size; /* 2 u32s, or 3 u32s if timestamp recorded */
3976 u32 ptr; /* SRAM byte address of log data */
3977 u32 ev, time, data; /* event log data */
3978
3979 if (num_events == 0)
3980 return;
3981
3982 base = le32_to_cpu(priv->card_alive.log_event_table_ptr);
3983
3984 if (mode == 0)
3985 event_size = 2 * sizeof(u32);
3986 else
3987 event_size = 3 * sizeof(u32);
3988
3989 ptr = base + EVENT_START_OFFSET + (start_idx * event_size);
3990
3991 /* "time" is actually "data" for mode 0 (no timestamp).
3992 * place event id # at far right for easier visual parsing. */
3993 for (i = 0; i < num_events; i++) {
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003994 ev = iwl_read_targ_mem(priv, ptr);
Zhu Yib481de92007-09-25 17:54:57 -07003995 ptr += sizeof(u32);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003996 time = iwl_read_targ_mem(priv, ptr);
Zhu Yib481de92007-09-25 17:54:57 -07003997 ptr += sizeof(u32);
3998 if (mode == 0)
3999 IWL_ERROR("0x%08x\t%04u\n", time, ev); /* data, ev */
4000 else {
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004001 data = iwl_read_targ_mem(priv, ptr);
Zhu Yib481de92007-09-25 17:54:57 -07004002 ptr += sizeof(u32);
4003 IWL_ERROR("%010u\t0x%08x\t%04u\n", time, data, ev);
4004 }
4005 }
4006}
4007
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004008static void iwl4965_dump_nic_event_log(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004009{
4010 int rc;
4011 u32 base; /* SRAM byte address of event log header */
4012 u32 capacity; /* event log capacity in # entries */
4013 u32 mode; /* 0 - no timestamp, 1 - timestamp recorded */
4014 u32 num_wraps; /* # times uCode wrapped to top of log */
4015 u32 next_entry; /* index of next entry to be written by uCode */
4016 u32 size; /* # entries that we'll print */
4017
4018 base = le32_to_cpu(priv->card_alive.log_event_table_ptr);
Tomas Winkler57aab752008-04-14 21:16:03 -07004019 if (!priv->cfg->ops->lib->is_valid_rtc_data_addr(base)) {
Zhu Yib481de92007-09-25 17:54:57 -07004020 IWL_ERROR("Invalid event log pointer 0x%08X\n", base);
4021 return;
4022 }
4023
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004024 rc = iwl_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004025 if (rc) {
4026 IWL_WARNING("Can not read from adapter at this time.\n");
4027 return;
4028 }
4029
4030 /* event log header */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004031 capacity = iwl_read_targ_mem(priv, base);
4032 mode = iwl_read_targ_mem(priv, base + (1 * sizeof(u32)));
4033 num_wraps = iwl_read_targ_mem(priv, base + (2 * sizeof(u32)));
4034 next_entry = iwl_read_targ_mem(priv, base + (3 * sizeof(u32)));
Zhu Yib481de92007-09-25 17:54:57 -07004035
4036 size = num_wraps ? capacity : next_entry;
4037
4038 /* bail out if nothing in log */
4039 if (size == 0) {
Zhu Yi583fab32007-09-27 11:27:30 +08004040 IWL_ERROR("Start IWL Event Log Dump: nothing in log\n");
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004041 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004042 return;
4043 }
4044
Zhu Yi583fab32007-09-27 11:27:30 +08004045 IWL_ERROR("Start IWL Event Log Dump: display count %d, wraps %d\n",
Zhu Yib481de92007-09-25 17:54:57 -07004046 size, num_wraps);
4047
4048 /* if uCode has wrapped back to top of log, start at the oldest entry,
4049 * i.e the next one that uCode would fill. */
4050 if (num_wraps)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004051 iwl4965_print_event_log(priv, next_entry,
Zhu Yib481de92007-09-25 17:54:57 -07004052 capacity - next_entry, mode);
4053
4054 /* (then/else) start at top of log */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004055 iwl4965_print_event_log(priv, 0, next_entry, mode);
Zhu Yib481de92007-09-25 17:54:57 -07004056
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004057 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004058}
4059
4060/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004061 * iwl4965_irq_handle_error - called for HW or SW error interrupt from card
Zhu Yib481de92007-09-25 17:54:57 -07004062 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004063static void iwl4965_irq_handle_error(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004064{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004065 /* Set the FW error flag -- cleared on iwl4965_down */
Zhu Yib481de92007-09-25 17:54:57 -07004066 set_bit(STATUS_FW_ERROR, &priv->status);
4067
4068 /* Cancel currently queued command. */
4069 clear_bit(STATUS_HCMD_ACTIVE, &priv->status);
4070
Tomas Winkler0a6857e2008-03-12 16:58:49 -07004071#ifdef CONFIG_IWLWIFI_DEBUG
4072 if (iwl_debug_level & IWL_DL_FW_ERRORS) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004073 iwl4965_dump_nic_error_log(priv);
4074 iwl4965_dump_nic_event_log(priv);
4075 iwl4965_print_rx_config_cmd(&priv->staging_rxon);
Zhu Yib481de92007-09-25 17:54:57 -07004076 }
4077#endif
4078
4079 wake_up_interruptible(&priv->wait_command_queue);
4080
4081 /* Keep the restart process from trying to send host
4082 * commands by clearing the INIT status bit */
4083 clear_bit(STATUS_READY, &priv->status);
4084
4085 if (!test_bit(STATUS_EXIT_PENDING, &priv->status)) {
4086 IWL_DEBUG(IWL_DL_INFO | IWL_DL_FW_ERRORS,
4087 "Restarting adapter due to uCode error.\n");
4088
Tomas Winkler3109ece2008-03-28 16:33:35 -07004089 if (iwl_is_associated(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07004090 memcpy(&priv->recovery_rxon, &priv->active_rxon,
4091 sizeof(priv->recovery_rxon));
4092 priv->error_recovering = 1;
4093 }
4094 queue_work(priv->workqueue, &priv->restart);
4095 }
4096}
4097
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004098static void iwl4965_error_recovery(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004099{
4100 unsigned long flags;
4101
4102 memcpy(&priv->staging_rxon, &priv->recovery_rxon,
4103 sizeof(priv->staging_rxon));
4104 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004105 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004106
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004107 iwl4965_rxon_add_station(priv, priv->bssid, 1);
Zhu Yib481de92007-09-25 17:54:57 -07004108
4109 spin_lock_irqsave(&priv->lock, flags);
4110 priv->assoc_id = le16_to_cpu(priv->staging_rxon.assoc_id);
4111 priv->error_recovering = 0;
4112 spin_unlock_irqrestore(&priv->lock, flags);
4113}
4114
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004115static void iwl4965_irq_tasklet(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004116{
4117 u32 inta, handled = 0;
4118 u32 inta_fh;
4119 unsigned long flags;
Tomas Winkler0a6857e2008-03-12 16:58:49 -07004120#ifdef CONFIG_IWLWIFI_DEBUG
Zhu Yib481de92007-09-25 17:54:57 -07004121 u32 inta_mask;
4122#endif
4123
4124 spin_lock_irqsave(&priv->lock, flags);
4125
4126 /* Ack/clear/reset pending uCode interrupts.
4127 * Note: Some bits in CSR_INT are "OR" of bits in CSR_FH_INT_STATUS,
4128 * and will clear only when CSR_FH_INT_STATUS gets cleared. */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004129 inta = iwl_read32(priv, CSR_INT);
4130 iwl_write32(priv, CSR_INT, inta);
Zhu Yib481de92007-09-25 17:54:57 -07004131
4132 /* Ack/clear/reset pending flow-handler (DMA) interrupts.
4133 * Any new interrupts that happen after this, either while we're
4134 * in this tasklet, or later, will show up in next ISR/tasklet. */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004135 inta_fh = iwl_read32(priv, CSR_FH_INT_STATUS);
4136 iwl_write32(priv, CSR_FH_INT_STATUS, inta_fh);
Zhu Yib481de92007-09-25 17:54:57 -07004137
Tomas Winkler0a6857e2008-03-12 16:58:49 -07004138#ifdef CONFIG_IWLWIFI_DEBUG
4139 if (iwl_debug_level & IWL_DL_ISR) {
Ben Cahill9fbab512007-11-29 11:09:47 +08004140 /* just for debug */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004141 inta_mask = iwl_read32(priv, CSR_INT_MASK);
Zhu Yib481de92007-09-25 17:54:57 -07004142 IWL_DEBUG_ISR("inta 0x%08x, enabled 0x%08x, fh 0x%08x\n",
4143 inta, inta_mask, inta_fh);
4144 }
4145#endif
4146
4147 /* Since CSR_INT and CSR_FH_INT_STATUS reads and clears are not
4148 * atomic, make sure that inta covers all the interrupts that
4149 * we've discovered, even if FH interrupt came in just after
4150 * reading CSR_INT. */
Tomas Winkler6f83eaa2008-03-04 18:09:28 -08004151 if (inta_fh & CSR49_FH_INT_RX_MASK)
Zhu Yib481de92007-09-25 17:54:57 -07004152 inta |= CSR_INT_BIT_FH_RX;
Tomas Winkler6f83eaa2008-03-04 18:09:28 -08004153 if (inta_fh & CSR49_FH_INT_TX_MASK)
Zhu Yib481de92007-09-25 17:54:57 -07004154 inta |= CSR_INT_BIT_FH_TX;
4155
4156 /* Now service all interrupt bits discovered above. */
4157 if (inta & CSR_INT_BIT_HW_ERR) {
4158 IWL_ERROR("Microcode HW error detected. Restarting.\n");
4159
4160 /* Tell the device to stop sending interrupts */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004161 iwl4965_disable_interrupts(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004162
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004163 iwl4965_irq_handle_error(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004164
4165 handled |= CSR_INT_BIT_HW_ERR;
4166
4167 spin_unlock_irqrestore(&priv->lock, flags);
4168
4169 return;
4170 }
4171
Tomas Winkler0a6857e2008-03-12 16:58:49 -07004172#ifdef CONFIG_IWLWIFI_DEBUG
4173 if (iwl_debug_level & (IWL_DL_ISR)) {
Zhu Yib481de92007-09-25 17:54:57 -07004174 /* NIC fires this, but we don't use it, redundant with WAKEUP */
Joonwoo Park25c03d82008-01-23 10:15:20 -08004175 if (inta & CSR_INT_BIT_SCD)
4176 IWL_DEBUG_ISR("Scheduler finished to transmit "
4177 "the frame/frames.\n");
Zhu Yib481de92007-09-25 17:54:57 -07004178
4179 /* Alive notification via Rx interrupt will do the real work */
4180 if (inta & CSR_INT_BIT_ALIVE)
4181 IWL_DEBUG_ISR("Alive interrupt\n");
4182 }
4183#endif
4184 /* Safely ignore these bits for debug checks below */
Joonwoo Park25c03d82008-01-23 10:15:20 -08004185 inta &= ~(CSR_INT_BIT_SCD | CSR_INT_BIT_ALIVE);
Zhu Yib481de92007-09-25 17:54:57 -07004186
Ben Cahill9fbab512007-11-29 11:09:47 +08004187 /* HW RF KILL switch toggled */
Zhu Yib481de92007-09-25 17:54:57 -07004188 if (inta & CSR_INT_BIT_RF_KILL) {
4189 int hw_rf_kill = 0;
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004190 if (!(iwl_read32(priv, CSR_GP_CNTRL) &
Zhu Yib481de92007-09-25 17:54:57 -07004191 CSR_GP_CNTRL_REG_FLAG_HW_RF_KILL_SW))
4192 hw_rf_kill = 1;
4193
4194 IWL_DEBUG(IWL_DL_INFO | IWL_DL_RF_KILL | IWL_DL_ISR,
4195 "RF_KILL bit toggled to %s.\n",
4196 hw_rf_kill ? "disable radio":"enable radio");
4197
4198 /* Queue restart only if RF_KILL switch was set to "kill"
4199 * when we loaded driver, and is now set to "enable".
4200 * After we're Alive, RF_KILL gets handled by
Reinette Chatre32304552008-02-15 14:34:37 -08004201 * iwl4965_rx_card_state_notif() */
Zhu Yi53e49092007-12-06 16:08:44 +08004202 if (!hw_rf_kill && !test_bit(STATUS_ALIVE, &priv->status)) {
4203 clear_bit(STATUS_RF_KILL_HW, &priv->status);
Zhu Yib481de92007-09-25 17:54:57 -07004204 queue_work(priv->workqueue, &priv->restart);
Zhu Yi53e49092007-12-06 16:08:44 +08004205 }
Zhu Yib481de92007-09-25 17:54:57 -07004206
4207 handled |= CSR_INT_BIT_RF_KILL;
4208 }
4209
Ben Cahill9fbab512007-11-29 11:09:47 +08004210 /* Chip got too hot and stopped itself */
Zhu Yib481de92007-09-25 17:54:57 -07004211 if (inta & CSR_INT_BIT_CT_KILL) {
4212 IWL_ERROR("Microcode CT kill error detected.\n");
4213 handled |= CSR_INT_BIT_CT_KILL;
4214 }
4215
4216 /* Error detected by uCode */
4217 if (inta & CSR_INT_BIT_SW_ERR) {
4218 IWL_ERROR("Microcode SW error detected. Restarting 0x%X.\n",
4219 inta);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004220 iwl4965_irq_handle_error(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004221 handled |= CSR_INT_BIT_SW_ERR;
4222 }
4223
4224 /* uCode wakes up after power-down sleep */
4225 if (inta & CSR_INT_BIT_WAKEUP) {
4226 IWL_DEBUG_ISR("Wakeup interrupt\n");
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004227 iwl4965_rx_queue_update_write_ptr(priv, &priv->rxq);
4228 iwl4965_tx_queue_update_write_ptr(priv, &priv->txq[0]);
4229 iwl4965_tx_queue_update_write_ptr(priv, &priv->txq[1]);
4230 iwl4965_tx_queue_update_write_ptr(priv, &priv->txq[2]);
4231 iwl4965_tx_queue_update_write_ptr(priv, &priv->txq[3]);
4232 iwl4965_tx_queue_update_write_ptr(priv, &priv->txq[4]);
4233 iwl4965_tx_queue_update_write_ptr(priv, &priv->txq[5]);
Zhu Yib481de92007-09-25 17:54:57 -07004234
4235 handled |= CSR_INT_BIT_WAKEUP;
4236 }
4237
4238 /* All uCode command responses, including Tx command responses,
4239 * Rx "responses" (frame-received notification), and other
4240 * notifications from uCode come through here*/
4241 if (inta & (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX)) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004242 iwl4965_rx_handle(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004243 handled |= (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX);
4244 }
4245
4246 if (inta & CSR_INT_BIT_FH_TX) {
4247 IWL_DEBUG_ISR("Tx interrupt\n");
4248 handled |= CSR_INT_BIT_FH_TX;
4249 }
4250
4251 if (inta & ~handled)
4252 IWL_ERROR("Unhandled INTA bits 0x%08x\n", inta & ~handled);
4253
4254 if (inta & ~CSR_INI_SET_MASK) {
4255 IWL_WARNING("Disabled INTA bits 0x%08x were pending\n",
4256 inta & ~CSR_INI_SET_MASK);
4257 IWL_WARNING(" with FH_INT = 0x%08x\n", inta_fh);
4258 }
4259
4260 /* Re-enable all interrupts */
Mohamed Abbas0359fac2008-03-28 16:21:08 -07004261 /* only Re-enable if diabled by irq */
4262 if (test_bit(STATUS_INT_ENABLED, &priv->status))
4263 iwl4965_enable_interrupts(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004264
Tomas Winkler0a6857e2008-03-12 16:58:49 -07004265#ifdef CONFIG_IWLWIFI_DEBUG
4266 if (iwl_debug_level & (IWL_DL_ISR)) {
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004267 inta = iwl_read32(priv, CSR_INT);
4268 inta_mask = iwl_read32(priv, CSR_INT_MASK);
4269 inta_fh = iwl_read32(priv, CSR_FH_INT_STATUS);
Zhu Yib481de92007-09-25 17:54:57 -07004270 IWL_DEBUG_ISR("End inta 0x%08x, enabled 0x%08x, fh 0x%08x, "
4271 "flags 0x%08lx\n", inta, inta_mask, inta_fh, flags);
4272 }
4273#endif
4274 spin_unlock_irqrestore(&priv->lock, flags);
4275}
4276
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004277static irqreturn_t iwl4965_isr(int irq, void *data)
Zhu Yib481de92007-09-25 17:54:57 -07004278{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004279 struct iwl_priv *priv = data;
Zhu Yib481de92007-09-25 17:54:57 -07004280 u32 inta, inta_mask;
4281 u32 inta_fh;
4282 if (!priv)
4283 return IRQ_NONE;
4284
4285 spin_lock(&priv->lock);
4286
4287 /* Disable (but don't clear!) interrupts here to avoid
4288 * back-to-back ISRs and sporadic interrupts from our NIC.
4289 * If we have something to service, the tasklet will re-enable ints.
4290 * If we *don't* have something, we'll re-enable before leaving here. */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004291 inta_mask = iwl_read32(priv, CSR_INT_MASK); /* just for debug */
4292 iwl_write32(priv, CSR_INT_MASK, 0x00000000);
Zhu Yib481de92007-09-25 17:54:57 -07004293
4294 /* Discover which interrupts are active/pending */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004295 inta = iwl_read32(priv, CSR_INT);
4296 inta_fh = iwl_read32(priv, CSR_FH_INT_STATUS);
Zhu Yib481de92007-09-25 17:54:57 -07004297
4298 /* Ignore interrupt if there's nothing in NIC to service.
4299 * This may be due to IRQ shared with another device,
4300 * or due to sporadic interrupts thrown from our NIC. */
4301 if (!inta && !inta_fh) {
4302 IWL_DEBUG_ISR("Ignore interrupt, inta == 0, inta_fh == 0\n");
4303 goto none;
4304 }
4305
4306 if ((inta == 0xFFFFFFFF) || ((inta & 0xFFFFFFF0) == 0xa5a5a5a0)) {
Oliver Neukum66fbb542007-11-15 09:31:10 +08004307 /* Hardware disappeared. It might have already raised
4308 * an interrupt */
Zhu Yib481de92007-09-25 17:54:57 -07004309 IWL_WARNING("HARDWARE GONE?? INTA == 0x%080x\n", inta);
Oliver Neukum66fbb542007-11-15 09:31:10 +08004310 goto unplugged;
Zhu Yib481de92007-09-25 17:54:57 -07004311 }
4312
4313 IWL_DEBUG_ISR("ISR inta 0x%08x, enabled 0x%08x, fh 0x%08x\n",
4314 inta, inta_mask, inta_fh);
4315
Joonwoo Park25c03d82008-01-23 10:15:20 -08004316 inta &= ~CSR_INT_BIT_SCD;
4317
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004318 /* iwl4965_irq_tasklet() will service interrupts and re-enable them */
Joonwoo Park25c03d82008-01-23 10:15:20 -08004319 if (likely(inta || inta_fh))
4320 tasklet_schedule(&priv->irq_tasklet);
Zhu Yib481de92007-09-25 17:54:57 -07004321
Oliver Neukum66fbb542007-11-15 09:31:10 +08004322 unplugged:
4323 spin_unlock(&priv->lock);
Zhu Yib481de92007-09-25 17:54:57 -07004324 return IRQ_HANDLED;
4325
4326 none:
4327 /* re-enable interrupts here since we don't have anything to service. */
Mohamed Abbas0359fac2008-03-28 16:21:08 -07004328 /* only Re-enable if diabled by irq */
4329 if (test_bit(STATUS_INT_ENABLED, &priv->status))
4330 iwl4965_enable_interrupts(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004331 spin_unlock(&priv->lock);
4332 return IRQ_NONE;
4333}
4334
Zhu Yib481de92007-09-25 17:54:57 -07004335/* For active scan, listen ACTIVE_DWELL_TIME (msec) on each channel after
4336 * sending probe req. This should be set long enough to hear probe responses
4337 * from more than one AP. */
4338#define IWL_ACTIVE_DWELL_TIME_24 (20) /* all times in msec */
4339#define IWL_ACTIVE_DWELL_TIME_52 (10)
4340
4341/* For faster active scanning, scan will move to the next channel if fewer than
4342 * PLCP_QUIET_THRESH packets are heard on this channel within
4343 * ACTIVE_QUIET_TIME after sending probe request. This shortens the dwell
4344 * time if it's a quiet channel (nothing responded to our probe, and there's
4345 * no other traffic).
4346 * Disable "quiet" feature by setting PLCP_QUIET_THRESH to 0. */
4347#define IWL_PLCP_QUIET_THRESH __constant_cpu_to_le16(1) /* packets */
4348#define IWL_ACTIVE_QUIET_TIME __constant_cpu_to_le16(5) /* msec */
4349
4350/* For passive scan, listen PASSIVE_DWELL_TIME (msec) on each channel.
4351 * Must be set longer than active dwell time.
4352 * For the most reliable scan, set > AP beacon interval (typically 100msec). */
4353#define IWL_PASSIVE_DWELL_TIME_24 (20) /* all times in msec */
4354#define IWL_PASSIVE_DWELL_TIME_52 (10)
4355#define IWL_PASSIVE_DWELL_BASE (100)
4356#define IWL_CHANNEL_TUNE_TIME 5
4357
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004358static inline u16 iwl4965_get_active_dwell_time(struct iwl_priv *priv,
Johannes Berg8318d782008-01-24 19:38:38 +01004359 enum ieee80211_band band)
Zhu Yib481de92007-09-25 17:54:57 -07004360{
Johannes Berg8318d782008-01-24 19:38:38 +01004361 if (band == IEEE80211_BAND_5GHZ)
Zhu Yib481de92007-09-25 17:54:57 -07004362 return IWL_ACTIVE_DWELL_TIME_52;
4363 else
4364 return IWL_ACTIVE_DWELL_TIME_24;
4365}
4366
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004367static u16 iwl4965_get_passive_dwell_time(struct iwl_priv *priv,
Johannes Berg8318d782008-01-24 19:38:38 +01004368 enum ieee80211_band band)
Zhu Yib481de92007-09-25 17:54:57 -07004369{
Johannes Berg8318d782008-01-24 19:38:38 +01004370 u16 active = iwl4965_get_active_dwell_time(priv, band);
4371 u16 passive = (band != IEEE80211_BAND_5GHZ) ?
Zhu Yib481de92007-09-25 17:54:57 -07004372 IWL_PASSIVE_DWELL_BASE + IWL_PASSIVE_DWELL_TIME_24 :
4373 IWL_PASSIVE_DWELL_BASE + IWL_PASSIVE_DWELL_TIME_52;
4374
Tomas Winkler3109ece2008-03-28 16:33:35 -07004375 if (iwl_is_associated(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07004376 /* If we're associated, we clamp the maximum passive
4377 * dwell time to be 98% of the beacon interval (minus
4378 * 2 * channel tune time) */
4379 passive = priv->beacon_int;
4380 if ((passive > IWL_PASSIVE_DWELL_BASE) || !passive)
4381 passive = IWL_PASSIVE_DWELL_BASE;
4382 passive = (passive * 98) / 100 - IWL_CHANNEL_TUNE_TIME * 2;
4383 }
4384
4385 if (passive <= active)
4386 passive = active + 1;
4387
4388 return passive;
4389}
4390
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004391static int iwl4965_get_channels_for_scan(struct iwl_priv *priv,
Johannes Berg8318d782008-01-24 19:38:38 +01004392 enum ieee80211_band band,
Zhu Yib481de92007-09-25 17:54:57 -07004393 u8 is_active, u8 direct_mask,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004394 struct iwl4965_scan_channel *scan_ch)
Zhu Yib481de92007-09-25 17:54:57 -07004395{
4396 const struct ieee80211_channel *channels = NULL;
Johannes Berg8318d782008-01-24 19:38:38 +01004397 const struct ieee80211_supported_band *sband;
Assaf Kraussbf85ea42008-03-14 10:38:49 -07004398 const struct iwl_channel_info *ch_info;
Zhu Yib481de92007-09-25 17:54:57 -07004399 u16 passive_dwell = 0;
4400 u16 active_dwell = 0;
4401 int added, i;
4402
Johannes Berg8318d782008-01-24 19:38:38 +01004403 sband = iwl4965_get_hw_mode(priv, band);
4404 if (!sband)
Zhu Yib481de92007-09-25 17:54:57 -07004405 return 0;
4406
Johannes Berg8318d782008-01-24 19:38:38 +01004407 channels = sband->channels;
Zhu Yib481de92007-09-25 17:54:57 -07004408
Johannes Berg8318d782008-01-24 19:38:38 +01004409 active_dwell = iwl4965_get_active_dwell_time(priv, band);
4410 passive_dwell = iwl4965_get_passive_dwell_time(priv, band);
Zhu Yib481de92007-09-25 17:54:57 -07004411
Johannes Berg8318d782008-01-24 19:38:38 +01004412 for (i = 0, added = 0; i < sband->n_channels; i++) {
Johannes Berg182e2e62008-04-04 10:41:56 +02004413 if (channels[i].flags & IEEE80211_CHAN_DISABLED)
4414 continue;
4415
Johannes Berg8318d782008-01-24 19:38:38 +01004416 scan_ch->channel = ieee80211_frequency_to_channel(channels[i].center_freq);
Zhu Yib481de92007-09-25 17:54:57 -07004417
Assaf Krauss8622e702008-03-21 13:53:43 -07004418 ch_info = iwl_get_channel_info(priv, band,
Ben Cahill9fbab512007-11-29 11:09:47 +08004419 scan_ch->channel);
Zhu Yib481de92007-09-25 17:54:57 -07004420 if (!is_channel_valid(ch_info)) {
4421 IWL_DEBUG_SCAN("Channel %d is INVALID for this SKU.\n",
4422 scan_ch->channel);
4423 continue;
4424 }
4425
4426 if (!is_active || is_channel_passive(ch_info) ||
Johannes Berg8318d782008-01-24 19:38:38 +01004427 (channels[i].flags & IEEE80211_CHAN_PASSIVE_SCAN))
Zhu Yib481de92007-09-25 17:54:57 -07004428 scan_ch->type = 0; /* passive */
4429 else
4430 scan_ch->type = 1; /* active */
4431
4432 if (scan_ch->type & 1)
4433 scan_ch->type |= (direct_mask << 1);
4434
4435 if (is_channel_narrow(ch_info))
4436 scan_ch->type |= (1 << 7);
4437
4438 scan_ch->active_dwell = cpu_to_le16(active_dwell);
4439 scan_ch->passive_dwell = cpu_to_le16(passive_dwell);
4440
Ben Cahill9fbab512007-11-29 11:09:47 +08004441 /* Set txpower levels to defaults */
Zhu Yib481de92007-09-25 17:54:57 -07004442 scan_ch->tpc.dsp_atten = 110;
4443 /* scan_pwr_info->tpc.dsp_atten; */
4444
4445 /*scan_pwr_info->tpc.tx_gain; */
Johannes Berg8318d782008-01-24 19:38:38 +01004446 if (band == IEEE80211_BAND_5GHZ)
Zhu Yib481de92007-09-25 17:54:57 -07004447 scan_ch->tpc.tx_gain = ((1 << 5) | (3 << 3)) | 3;
4448 else {
4449 scan_ch->tpc.tx_gain = ((1 << 5) | (5 << 3));
4450 /* NOTE: if we were doing 6Mb OFDM for scans we'd use
Ben Cahill9fbab512007-11-29 11:09:47 +08004451 * power level:
Reinette Chatre8a1b0242008-01-14 17:46:25 -08004452 * scan_ch->tpc.tx_gain = ((1 << 5) | (2 << 3)) | 3;
Zhu Yib481de92007-09-25 17:54:57 -07004453 */
4454 }
4455
4456 IWL_DEBUG_SCAN("Scanning %d [%s %d]\n",
4457 scan_ch->channel,
4458 (scan_ch->type & 1) ? "ACTIVE" : "PASSIVE",
4459 (scan_ch->type & 1) ?
4460 active_dwell : passive_dwell);
4461
4462 scan_ch++;
4463 added++;
4464 }
4465
4466 IWL_DEBUG_SCAN("total channels to scan %d \n", added);
4467 return added;
4468}
4469
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004470static void iwl4965_init_hw_rates(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07004471 struct ieee80211_rate *rates)
4472{
4473 int i;
4474
4475 for (i = 0; i < IWL_RATE_COUNT; i++) {
Johannes Berg8318d782008-01-24 19:38:38 +01004476 rates[i].bitrate = iwl4965_rates[i].ieee * 5;
4477 rates[i].hw_value = i; /* Rate scaling will work on indexes */
4478 rates[i].hw_value_short = i;
4479 rates[i].flags = 0;
4480 if ((i > IWL_LAST_OFDM_RATE) || (i < IWL_FIRST_OFDM_RATE)) {
Zhu Yib481de92007-09-25 17:54:57 -07004481 /*
Johannes Berg8318d782008-01-24 19:38:38 +01004482 * If CCK != 1M then set short preamble rate flag.
Zhu Yib481de92007-09-25 17:54:57 -07004483 */
Tomas Winkler35cdeaf2008-03-11 16:17:20 -07004484 rates[i].flags |=
4485 (iwl4965_rates[i].plcp == IWL_RATE_1M_PLCP) ?
4486 0 : IEEE80211_RATE_SHORT_PREAMBLE;
Zhu Yib481de92007-09-25 17:54:57 -07004487 }
Zhu Yib481de92007-09-25 17:54:57 -07004488 }
Zhu Yib481de92007-09-25 17:54:57 -07004489}
4490
4491/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004492 * iwl4965_init_geos - Initialize mac80211's geo/channel info based from eeprom
Zhu Yib481de92007-09-25 17:54:57 -07004493 */
Assaf Kraussbf85ea42008-03-14 10:38:49 -07004494int iwl4965_init_geos(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004495{
Assaf Kraussbf85ea42008-03-14 10:38:49 -07004496 struct iwl_channel_info *ch;
Tomas Winkler8211ef72008-03-02 01:36:04 +02004497 struct ieee80211_supported_band *sband;
Zhu Yib481de92007-09-25 17:54:57 -07004498 struct ieee80211_channel *channels;
4499 struct ieee80211_channel *geo_ch;
4500 struct ieee80211_rate *rates;
4501 int i = 0;
Zhu Yib481de92007-09-25 17:54:57 -07004502
Johannes Berg8318d782008-01-24 19:38:38 +01004503 if (priv->bands[IEEE80211_BAND_2GHZ].n_bitrates ||
4504 priv->bands[IEEE80211_BAND_5GHZ].n_bitrates) {
Zhu Yib481de92007-09-25 17:54:57 -07004505 IWL_DEBUG_INFO("Geography modes already initialized.\n");
4506 set_bit(STATUS_GEO_CONFIGURED, &priv->status);
4507 return 0;
4508 }
4509
Zhu Yib481de92007-09-25 17:54:57 -07004510 channels = kzalloc(sizeof(struct ieee80211_channel) *
4511 priv->channel_count, GFP_KERNEL);
Johannes Berg8318d782008-01-24 19:38:38 +01004512 if (!channels)
Zhu Yib481de92007-09-25 17:54:57 -07004513 return -ENOMEM;
Zhu Yib481de92007-09-25 17:54:57 -07004514
Tomas Winkler8211ef72008-03-02 01:36:04 +02004515 rates = kzalloc((sizeof(struct ieee80211_rate) * (IWL_RATE_COUNT + 1)),
Zhu Yib481de92007-09-25 17:54:57 -07004516 GFP_KERNEL);
4517 if (!rates) {
Zhu Yib481de92007-09-25 17:54:57 -07004518 kfree(channels);
4519 return -ENOMEM;
4520 }
4521
Zhu Yib481de92007-09-25 17:54:57 -07004522 /* 5.2GHz channels start after the 2.4GHz channels */
Tomas Winkler8211ef72008-03-02 01:36:04 +02004523 sband = &priv->bands[IEEE80211_BAND_5GHZ];
Assaf Kraussbf85ea42008-03-14 10:38:49 -07004524 sband->channels = &channels[ARRAY_SIZE(iwl_eeprom_band_1)];
Tomas Winkler8211ef72008-03-02 01:36:04 +02004525 /* just OFDM */
4526 sband->bitrates = &rates[IWL_FIRST_OFDM_RATE];
4527 sband->n_bitrates = IWL_RATE_COUNT - IWL_FIRST_OFDM_RATE;
Johannes Berg8318d782008-01-24 19:38:38 +01004528
Assaf Krauss1ea87392008-03-18 14:57:50 -07004529 iwl4965_init_ht_hw_capab(priv, &sband->ht_info, IEEE80211_BAND_5GHZ);
Tomas Winkler78330fd2008-02-06 02:37:18 +02004530
Tomas Winkler8211ef72008-03-02 01:36:04 +02004531 sband = &priv->bands[IEEE80211_BAND_2GHZ];
4532 sband->channels = channels;
4533 /* OFDM & CCK */
4534 sband->bitrates = rates;
4535 sband->n_bitrates = IWL_RATE_COUNT;
Zhu Yib481de92007-09-25 17:54:57 -07004536
Assaf Krauss1ea87392008-03-18 14:57:50 -07004537 iwl4965_init_ht_hw_capab(priv, &sband->ht_info, IEEE80211_BAND_2GHZ);
Tomas Winkler78330fd2008-02-06 02:37:18 +02004538
Zhu Yib481de92007-09-25 17:54:57 -07004539 priv->ieee_channels = channels;
4540 priv->ieee_rates = rates;
4541
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004542 iwl4965_init_hw_rates(priv, rates);
Zhu Yib481de92007-09-25 17:54:57 -07004543
Tomas Winkler8211ef72008-03-02 01:36:04 +02004544 for (i = 0; i < priv->channel_count; i++) {
Zhu Yib481de92007-09-25 17:54:57 -07004545 ch = &priv->channel_info[i];
4546
Tomas Winkler8211ef72008-03-02 01:36:04 +02004547 /* FIXME: might be removed if scan is OK */
4548 if (!is_channel_valid(ch))
Zhu Yib481de92007-09-25 17:54:57 -07004549 continue;
Zhu Yib481de92007-09-25 17:54:57 -07004550
Tomas Winkler8211ef72008-03-02 01:36:04 +02004551 if (is_channel_a_band(ch))
4552 sband = &priv->bands[IEEE80211_BAND_5GHZ];
4553 else
4554 sband = &priv->bands[IEEE80211_BAND_2GHZ];
Zhu Yib481de92007-09-25 17:54:57 -07004555
Tomas Winkler8211ef72008-03-02 01:36:04 +02004556 geo_ch = &sband->channels[sband->n_channels++];
4557
4558 geo_ch->center_freq = ieee80211_channel_to_frequency(ch->channel);
Johannes Berg8318d782008-01-24 19:38:38 +01004559 geo_ch->max_power = ch->max_power_avg;
4560 geo_ch->max_antenna_gain = 0xff;
Mohamed Abbas7b723042008-01-31 21:46:40 -08004561 geo_ch->hw_value = ch->channel;
Zhu Yib481de92007-09-25 17:54:57 -07004562
4563 if (is_channel_valid(ch)) {
Johannes Berg8318d782008-01-24 19:38:38 +01004564 if (!(ch->flags & EEPROM_CHANNEL_IBSS))
4565 geo_ch->flags |= IEEE80211_CHAN_NO_IBSS;
Zhu Yib481de92007-09-25 17:54:57 -07004566
Johannes Berg8318d782008-01-24 19:38:38 +01004567 if (!(ch->flags & EEPROM_CHANNEL_ACTIVE))
4568 geo_ch->flags |= IEEE80211_CHAN_PASSIVE_SCAN;
Zhu Yib481de92007-09-25 17:54:57 -07004569
4570 if (ch->flags & EEPROM_CHANNEL_RADAR)
Johannes Berg8318d782008-01-24 19:38:38 +01004571 geo_ch->flags |= IEEE80211_CHAN_RADAR;
Zhu Yib481de92007-09-25 17:54:57 -07004572
4573 if (ch->max_power_avg > priv->max_channel_txpower_limit)
4574 priv->max_channel_txpower_limit =
4575 ch->max_power_avg;
Tomas Winkler8211ef72008-03-02 01:36:04 +02004576 } else {
Johannes Berg8318d782008-01-24 19:38:38 +01004577 geo_ch->flags |= IEEE80211_CHAN_DISABLED;
Tomas Winkler8211ef72008-03-02 01:36:04 +02004578 }
4579
4580 /* Save flags for reg domain usage */
4581 geo_ch->orig_flags = geo_ch->flags;
4582
4583 IWL_DEBUG_INFO("Channel %d Freq=%d[%sGHz] %s flag=0%X\n",
4584 ch->channel, geo_ch->center_freq,
4585 is_channel_a_band(ch) ? "5.2" : "2.4",
4586 geo_ch->flags & IEEE80211_CHAN_DISABLED ?
4587 "restricted" : "valid",
4588 geo_ch->flags);
Zhu Yib481de92007-09-25 17:54:57 -07004589 }
4590
Tomas Winkler82b9a122008-03-04 18:09:30 -08004591 if ((priv->bands[IEEE80211_BAND_5GHZ].n_channels == 0) &&
4592 priv->cfg->sku & IWL_SKU_A) {
Zhu Yib481de92007-09-25 17:54:57 -07004593 printk(KERN_INFO DRV_NAME
4594 ": Incorrectly detected BG card as ABG. Please send "
4595 "your PCI ID 0x%04X:0x%04X to maintainer.\n",
4596 priv->pci_dev->device, priv->pci_dev->subsystem_device);
Tomas Winkler82b9a122008-03-04 18:09:30 -08004597 priv->cfg->sku &= ~IWL_SKU_A;
Zhu Yib481de92007-09-25 17:54:57 -07004598 }
4599
4600 printk(KERN_INFO DRV_NAME
4601 ": Tunable channels: %d 802.11bg, %d 802.11a channels\n",
Johannes Berg8318d782008-01-24 19:38:38 +01004602 priv->bands[IEEE80211_BAND_2GHZ].n_channels,
4603 priv->bands[IEEE80211_BAND_5GHZ].n_channels);
Zhu Yib481de92007-09-25 17:54:57 -07004604
John W. Linvillee0e0a672008-03-25 15:58:40 -04004605 if (priv->bands[IEEE80211_BAND_2GHZ].n_channels)
4606 priv->hw->wiphy->bands[IEEE80211_BAND_2GHZ] =
4607 &priv->bands[IEEE80211_BAND_2GHZ];
4608 if (priv->bands[IEEE80211_BAND_5GHZ].n_channels)
4609 priv->hw->wiphy->bands[IEEE80211_BAND_5GHZ] =
4610 &priv->bands[IEEE80211_BAND_5GHZ];
Zhu Yib481de92007-09-25 17:54:57 -07004611
Zhu Yib481de92007-09-25 17:54:57 -07004612 set_bit(STATUS_GEO_CONFIGURED, &priv->status);
4613
4614 return 0;
4615}
4616
Reinette Chatre849e0dc2008-01-23 10:15:18 -08004617/*
4618 * iwl4965_free_geos - undo allocations in iwl4965_init_geos
4619 */
Assaf Kraussbf85ea42008-03-14 10:38:49 -07004620void iwl4965_free_geos(struct iwl_priv *priv)
Reinette Chatre849e0dc2008-01-23 10:15:18 -08004621{
Reinette Chatre849e0dc2008-01-23 10:15:18 -08004622 kfree(priv->ieee_channels);
4623 kfree(priv->ieee_rates);
4624 clear_bit(STATUS_GEO_CONFIGURED, &priv->status);
4625}
4626
Zhu Yib481de92007-09-25 17:54:57 -07004627/******************************************************************************
4628 *
4629 * uCode download functions
4630 *
4631 ******************************************************************************/
4632
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004633static void iwl4965_dealloc_ucode_pci(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004634{
Tomas Winkler98c92212008-01-14 17:46:20 -08004635 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_code);
4636 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_data);
4637 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_data_backup);
4638 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_init);
4639 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_init_data);
4640 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_boot);
Zhu Yib481de92007-09-25 17:54:57 -07004641}
4642
4643/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004644 * iwl4965_verify_inst_full - verify runtime uCode image in card vs. host,
Zhu Yib481de92007-09-25 17:54:57 -07004645 * looking at all data.
4646 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004647static int iwl4965_verify_inst_full(struct iwl_priv *priv, __le32 *image,
Ben Cahill9fbab512007-11-29 11:09:47 +08004648 u32 len)
Zhu Yib481de92007-09-25 17:54:57 -07004649{
4650 u32 val;
4651 u32 save_len = len;
4652 int rc = 0;
4653 u32 errcnt;
4654
4655 IWL_DEBUG_INFO("ucode inst image size is %u\n", len);
4656
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004657 rc = iwl_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004658 if (rc)
4659 return rc;
4660
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004661 iwl_write_direct32(priv, HBUS_TARG_MEM_RADDR, RTC_INST_LOWER_BOUND);
Zhu Yib481de92007-09-25 17:54:57 -07004662
4663 errcnt = 0;
4664 for (; len > 0; len -= sizeof(u32), image++) {
4665 /* read data comes through single port, auto-incr addr */
4666 /* NOTE: Use the debugless read so we don't flood kernel log
4667 * if IWL_DL_IO is set */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004668 val = _iwl_read_direct32(priv, HBUS_TARG_MEM_RDAT);
Zhu Yib481de92007-09-25 17:54:57 -07004669 if (val != le32_to_cpu(*image)) {
4670 IWL_ERROR("uCode INST section is invalid at "
4671 "offset 0x%x, is 0x%x, s/b 0x%x\n",
4672 save_len - len, val, le32_to_cpu(*image));
4673 rc = -EIO;
4674 errcnt++;
4675 if (errcnt >= 20)
4676 break;
4677 }
4678 }
4679
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004680 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004681
4682 if (!errcnt)
4683 IWL_DEBUG_INFO
4684 ("ucode image in INSTRUCTION memory is good\n");
4685
4686 return rc;
4687}
4688
4689
4690/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004691 * iwl4965_verify_inst_sparse - verify runtime uCode image in card vs. host,
Zhu Yib481de92007-09-25 17:54:57 -07004692 * using sample data 100 bytes apart. If these sample points are good,
4693 * it's a pretty good bet that everything between them is good, too.
4694 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004695static int iwl4965_verify_inst_sparse(struct iwl_priv *priv, __le32 *image, u32 len)
Zhu Yib481de92007-09-25 17:54:57 -07004696{
4697 u32 val;
4698 int rc = 0;
4699 u32 errcnt = 0;
4700 u32 i;
4701
4702 IWL_DEBUG_INFO("ucode inst image size is %u\n", len);
4703
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004704 rc = iwl_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004705 if (rc)
4706 return rc;
4707
4708 for (i = 0; i < len; i += 100, image += 100/sizeof(u32)) {
4709 /* read data comes through single port, auto-incr addr */
4710 /* NOTE: Use the debugless read so we don't flood kernel log
4711 * if IWL_DL_IO is set */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004712 iwl_write_direct32(priv, HBUS_TARG_MEM_RADDR,
Zhu Yib481de92007-09-25 17:54:57 -07004713 i + RTC_INST_LOWER_BOUND);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004714 val = _iwl_read_direct32(priv, HBUS_TARG_MEM_RDAT);
Zhu Yib481de92007-09-25 17:54:57 -07004715 if (val != le32_to_cpu(*image)) {
4716#if 0 /* Enable this if you want to see details */
4717 IWL_ERROR("uCode INST section is invalid at "
4718 "offset 0x%x, is 0x%x, s/b 0x%x\n",
4719 i, val, *image);
4720#endif
4721 rc = -EIO;
4722 errcnt++;
4723 if (errcnt >= 3)
4724 break;
4725 }
4726 }
4727
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004728 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004729
4730 return rc;
4731}
4732
4733
4734/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004735 * iwl4965_verify_ucode - determine which instruction image is in SRAM,
Zhu Yib481de92007-09-25 17:54:57 -07004736 * and verify its contents
4737 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004738static int iwl4965_verify_ucode(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004739{
4740 __le32 *image;
4741 u32 len;
4742 int rc = 0;
4743
4744 /* Try bootstrap */
4745 image = (__le32 *)priv->ucode_boot.v_addr;
4746 len = priv->ucode_boot.len;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004747 rc = iwl4965_verify_inst_sparse(priv, image, len);
Zhu Yib481de92007-09-25 17:54:57 -07004748 if (rc == 0) {
4749 IWL_DEBUG_INFO("Bootstrap uCode is good in inst SRAM\n");
4750 return 0;
4751 }
4752
4753 /* Try initialize */
4754 image = (__le32 *)priv->ucode_init.v_addr;
4755 len = priv->ucode_init.len;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004756 rc = iwl4965_verify_inst_sparse(priv, image, len);
Zhu Yib481de92007-09-25 17:54:57 -07004757 if (rc == 0) {
4758 IWL_DEBUG_INFO("Initialize uCode is good in inst SRAM\n");
4759 return 0;
4760 }
4761
4762 /* Try runtime/protocol */
4763 image = (__le32 *)priv->ucode_code.v_addr;
4764 len = priv->ucode_code.len;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004765 rc = iwl4965_verify_inst_sparse(priv, image, len);
Zhu Yib481de92007-09-25 17:54:57 -07004766 if (rc == 0) {
4767 IWL_DEBUG_INFO("Runtime uCode is good in inst SRAM\n");
4768 return 0;
4769 }
4770
4771 IWL_ERROR("NO VALID UCODE IMAGE IN INSTRUCTION SRAM!!\n");
4772
Ben Cahill9fbab512007-11-29 11:09:47 +08004773 /* Since nothing seems to match, show first several data entries in
4774 * instruction SRAM, so maybe visual inspection will give a clue.
4775 * Selection of bootstrap image (vs. other images) is arbitrary. */
Zhu Yib481de92007-09-25 17:54:57 -07004776 image = (__le32 *)priv->ucode_boot.v_addr;
4777 len = priv->ucode_boot.len;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004778 rc = iwl4965_verify_inst_full(priv, image, len);
Zhu Yib481de92007-09-25 17:54:57 -07004779
4780 return rc;
4781}
4782
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004783static void iwl4965_nic_start(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004784{
4785 /* Remove all resets to allow NIC to operate */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004786 iwl_write32(priv, CSR_RESET, 0);
Zhu Yib481de92007-09-25 17:54:57 -07004787}
4788
Tomas Winkler90e759d2007-11-29 11:09:41 +08004789
Zhu Yib481de92007-09-25 17:54:57 -07004790/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004791 * iwl4965_read_ucode - Read uCode images from disk file.
Zhu Yib481de92007-09-25 17:54:57 -07004792 *
4793 * Copy into buffers for card to fetch via bus-mastering
4794 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004795static int iwl4965_read_ucode(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004796{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004797 struct iwl4965_ucode *ucode;
Tomas Winkler90e759d2007-11-29 11:09:41 +08004798 int ret;
Zhu Yib481de92007-09-25 17:54:57 -07004799 const struct firmware *ucode_raw;
Tomas Winkler4bf775c2008-03-04 18:09:31 -08004800 const char *name = priv->cfg->fw_name;
Zhu Yib481de92007-09-25 17:54:57 -07004801 u8 *src;
4802 size_t len;
4803 u32 ver, inst_size, data_size, init_size, init_data_size, boot_size;
4804
4805 /* Ask kernel firmware_class module to get the boot firmware off disk.
4806 * request_firmware() is synchronous, file is in memory on return. */
Tomas Winkler90e759d2007-11-29 11:09:41 +08004807 ret = request_firmware(&ucode_raw, name, &priv->pci_dev->dev);
4808 if (ret < 0) {
4809 IWL_ERROR("%s firmware file req failed: Reason %d\n",
4810 name, ret);
Zhu Yib481de92007-09-25 17:54:57 -07004811 goto error;
4812 }
4813
4814 IWL_DEBUG_INFO("Got firmware '%s' file (%zd bytes) from disk\n",
4815 name, ucode_raw->size);
4816
4817 /* Make sure that we got at least our header! */
4818 if (ucode_raw->size < sizeof(*ucode)) {
4819 IWL_ERROR("File size way too small!\n");
Tomas Winkler90e759d2007-11-29 11:09:41 +08004820 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07004821 goto err_release;
4822 }
4823
4824 /* Data from ucode file: header followed by uCode images */
4825 ucode = (void *)ucode_raw->data;
4826
4827 ver = le32_to_cpu(ucode->ver);
4828 inst_size = le32_to_cpu(ucode->inst_size);
4829 data_size = le32_to_cpu(ucode->data_size);
4830 init_size = le32_to_cpu(ucode->init_size);
4831 init_data_size = le32_to_cpu(ucode->init_data_size);
4832 boot_size = le32_to_cpu(ucode->boot_size);
4833
4834 IWL_DEBUG_INFO("f/w package hdr ucode version = 0x%x\n", ver);
4835 IWL_DEBUG_INFO("f/w package hdr runtime inst size = %u\n",
4836 inst_size);
4837 IWL_DEBUG_INFO("f/w package hdr runtime data size = %u\n",
4838 data_size);
4839 IWL_DEBUG_INFO("f/w package hdr init inst size = %u\n",
4840 init_size);
4841 IWL_DEBUG_INFO("f/w package hdr init data size = %u\n",
4842 init_data_size);
4843 IWL_DEBUG_INFO("f/w package hdr boot inst size = %u\n",
4844 boot_size);
4845
4846 /* Verify size of file vs. image size info in file's header */
4847 if (ucode_raw->size < sizeof(*ucode) +
4848 inst_size + data_size + init_size +
4849 init_data_size + boot_size) {
4850
4851 IWL_DEBUG_INFO("uCode file size %d too small\n",
4852 (int)ucode_raw->size);
Tomas Winkler90e759d2007-11-29 11:09:41 +08004853 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07004854 goto err_release;
4855 }
4856
4857 /* Verify that uCode images will fit in card's SRAM */
4858 if (inst_size > IWL_MAX_INST_SIZE) {
Tomas Winkler90e759d2007-11-29 11:09:41 +08004859 IWL_DEBUG_INFO("uCode instr len %d too large to fit in\n",
4860 inst_size);
4861 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07004862 goto err_release;
4863 }
4864
4865 if (data_size > IWL_MAX_DATA_SIZE) {
Tomas Winkler90e759d2007-11-29 11:09:41 +08004866 IWL_DEBUG_INFO("uCode data len %d too large to fit in\n",
4867 data_size);
4868 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07004869 goto err_release;
4870 }
4871 if (init_size > IWL_MAX_INST_SIZE) {
4872 IWL_DEBUG_INFO
Tomas Winkler90e759d2007-11-29 11:09:41 +08004873 ("uCode init instr len %d too large to fit in\n",
4874 init_size);
4875 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07004876 goto err_release;
4877 }
4878 if (init_data_size > IWL_MAX_DATA_SIZE) {
4879 IWL_DEBUG_INFO
Tomas Winkler90e759d2007-11-29 11:09:41 +08004880 ("uCode init data len %d too large to fit in\n",
4881 init_data_size);
4882 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07004883 goto err_release;
4884 }
4885 if (boot_size > IWL_MAX_BSM_SIZE) {
4886 IWL_DEBUG_INFO
Tomas Winkler90e759d2007-11-29 11:09:41 +08004887 ("uCode boot instr len %d too large to fit in\n",
4888 boot_size);
4889 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07004890 goto err_release;
4891 }
4892
4893 /* Allocate ucode buffers for card's bus-master loading ... */
4894
4895 /* Runtime instructions and 2 copies of data:
4896 * 1) unmodified from disk
4897 * 2) backup cache for save/restore during power-downs */
4898 priv->ucode_code.len = inst_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08004899 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_code);
Zhu Yib481de92007-09-25 17:54:57 -07004900
4901 priv->ucode_data.len = data_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08004902 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_data);
Zhu Yib481de92007-09-25 17:54:57 -07004903
4904 priv->ucode_data_backup.len = data_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08004905 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_data_backup);
Zhu Yib481de92007-09-25 17:54:57 -07004906
4907 /* Initialization instructions and data */
Tomas Winkler90e759d2007-11-29 11:09:41 +08004908 if (init_size && init_data_size) {
4909 priv->ucode_init.len = init_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08004910 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_init);
Zhu Yib481de92007-09-25 17:54:57 -07004911
Tomas Winkler90e759d2007-11-29 11:09:41 +08004912 priv->ucode_init_data.len = init_data_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08004913 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_init_data);
Tomas Winkler90e759d2007-11-29 11:09:41 +08004914
4915 if (!priv->ucode_init.v_addr || !priv->ucode_init_data.v_addr)
4916 goto err_pci_alloc;
4917 }
Zhu Yib481de92007-09-25 17:54:57 -07004918
4919 /* Bootstrap (instructions only, no data) */
Tomas Winkler90e759d2007-11-29 11:09:41 +08004920 if (boot_size) {
4921 priv->ucode_boot.len = boot_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08004922 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_boot);
Zhu Yib481de92007-09-25 17:54:57 -07004923
Tomas Winkler90e759d2007-11-29 11:09:41 +08004924 if (!priv->ucode_boot.v_addr)
4925 goto err_pci_alloc;
4926 }
Zhu Yib481de92007-09-25 17:54:57 -07004927
4928 /* Copy images into buffers for card's bus-master reads ... */
4929
4930 /* Runtime instructions (first block of data in file) */
4931 src = &ucode->data[0];
4932 len = priv->ucode_code.len;
Tomas Winkler90e759d2007-11-29 11:09:41 +08004933 IWL_DEBUG_INFO("Copying (but not loading) uCode instr len %Zd\n", len);
Zhu Yib481de92007-09-25 17:54:57 -07004934 memcpy(priv->ucode_code.v_addr, src, len);
4935 IWL_DEBUG_INFO("uCode instr buf vaddr = 0x%p, paddr = 0x%08x\n",
4936 priv->ucode_code.v_addr, (u32)priv->ucode_code.p_addr);
4937
4938 /* Runtime data (2nd block)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004939 * NOTE: Copy into backup buffer will be done in iwl4965_up() */
Zhu Yib481de92007-09-25 17:54:57 -07004940 src = &ucode->data[inst_size];
4941 len = priv->ucode_data.len;
Tomas Winkler90e759d2007-11-29 11:09:41 +08004942 IWL_DEBUG_INFO("Copying (but not loading) uCode data len %Zd\n", len);
Zhu Yib481de92007-09-25 17:54:57 -07004943 memcpy(priv->ucode_data.v_addr, src, len);
4944 memcpy(priv->ucode_data_backup.v_addr, src, len);
4945
4946 /* Initialization instructions (3rd block) */
4947 if (init_size) {
4948 src = &ucode->data[inst_size + data_size];
4949 len = priv->ucode_init.len;
Tomas Winkler90e759d2007-11-29 11:09:41 +08004950 IWL_DEBUG_INFO("Copying (but not loading) init instr len %Zd\n",
4951 len);
Zhu Yib481de92007-09-25 17:54:57 -07004952 memcpy(priv->ucode_init.v_addr, src, len);
4953 }
4954
4955 /* Initialization data (4th block) */
4956 if (init_data_size) {
4957 src = &ucode->data[inst_size + data_size + init_size];
4958 len = priv->ucode_init_data.len;
Tomas Winkler90e759d2007-11-29 11:09:41 +08004959 IWL_DEBUG_INFO("Copying (but not loading) init data len %Zd\n",
4960 len);
Zhu Yib481de92007-09-25 17:54:57 -07004961 memcpy(priv->ucode_init_data.v_addr, src, len);
4962 }
4963
4964 /* Bootstrap instructions (5th block) */
4965 src = &ucode->data[inst_size + data_size + init_size + init_data_size];
4966 len = priv->ucode_boot.len;
Tomas Winkler90e759d2007-11-29 11:09:41 +08004967 IWL_DEBUG_INFO("Copying (but not loading) boot instr len %Zd\n", len);
Zhu Yib481de92007-09-25 17:54:57 -07004968 memcpy(priv->ucode_boot.v_addr, src, len);
4969
4970 /* We have our copies now, allow OS release its copies */
4971 release_firmware(ucode_raw);
4972 return 0;
4973
4974 err_pci_alloc:
4975 IWL_ERROR("failed to allocate pci memory\n");
Tomas Winkler90e759d2007-11-29 11:09:41 +08004976 ret = -ENOMEM;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004977 iwl4965_dealloc_ucode_pci(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004978
4979 err_release:
4980 release_firmware(ucode_raw);
4981
4982 error:
Tomas Winkler90e759d2007-11-29 11:09:41 +08004983 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07004984}
4985
4986
4987/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004988 * iwl4965_set_ucode_ptrs - Set uCode address location
Zhu Yib481de92007-09-25 17:54:57 -07004989 *
4990 * Tell initialization uCode where to find runtime uCode.
4991 *
4992 * BSM registers initially contain pointers to initialization uCode.
4993 * We need to replace them to load runtime uCode inst and data,
4994 * and to save runtime data when powering down.
4995 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004996static int iwl4965_set_ucode_ptrs(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004997{
4998 dma_addr_t pinst;
4999 dma_addr_t pdata;
5000 int rc = 0;
5001 unsigned long flags;
5002
5003 /* bits 35:4 for 4965 */
5004 pinst = priv->ucode_code.p_addr >> 4;
5005 pdata = priv->ucode_data_backup.p_addr >> 4;
5006
5007 spin_lock_irqsave(&priv->lock, flags);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07005008 rc = iwl_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005009 if (rc) {
5010 spin_unlock_irqrestore(&priv->lock, flags);
5011 return rc;
5012 }
5013
5014 /* Tell bootstrap uCode where to find image to load */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07005015 iwl_write_prph(priv, BSM_DRAM_INST_PTR_REG, pinst);
5016 iwl_write_prph(priv, BSM_DRAM_DATA_PTR_REG, pdata);
5017 iwl_write_prph(priv, BSM_DRAM_DATA_BYTECOUNT_REG,
Zhu Yib481de92007-09-25 17:54:57 -07005018 priv->ucode_data.len);
5019
5020 /* Inst bytecount must be last to set up, bit 31 signals uCode
5021 * that all new ptr/size info is in place */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07005022 iwl_write_prph(priv, BSM_DRAM_INST_BYTECOUNT_REG,
Zhu Yib481de92007-09-25 17:54:57 -07005023 priv->ucode_code.len | BSM_DRAM_INST_LOAD);
5024
Tomas Winkler3395f6e2008-03-25 16:33:37 -07005025 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005026
5027 spin_unlock_irqrestore(&priv->lock, flags);
5028
5029 IWL_DEBUG_INFO("Runtime uCode pointers are set.\n");
5030
5031 return rc;
5032}
5033
5034/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005035 * iwl4965_init_alive_start - Called after REPLY_ALIVE notification received
Zhu Yib481de92007-09-25 17:54:57 -07005036 *
5037 * Called after REPLY_ALIVE notification received from "initialize" uCode.
5038 *
5039 * The 4965 "initialize" ALIVE reply contains calibration data for:
5040 * Voltage, temperature, and MIMO tx gain correction, now stored in priv
5041 * (3945 does not contain this data).
5042 *
5043 * Tell "initialize" uCode to go ahead and load the runtime uCode.
5044*/
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005045static void iwl4965_init_alive_start(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005046{
5047 /* Check alive response for "valid" sign from uCode */
5048 if (priv->card_alive_init.is_valid != UCODE_VALID_OK) {
5049 /* We had an error bringing up the hardware, so take it
5050 * all the way back down so we can try again */
5051 IWL_DEBUG_INFO("Initialize Alive failed.\n");
5052 goto restart;
5053 }
5054
5055 /* Bootstrap uCode has loaded initialize uCode ... verify inst image.
5056 * This is a paranoid check, because we would not have gotten the
5057 * "initialize" alive if code weren't properly loaded. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005058 if (iwl4965_verify_ucode(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07005059 /* Runtime instruction load was bad;
5060 * take it all the way back down so we can try again */
5061 IWL_DEBUG_INFO("Bad \"initialize\" uCode load.\n");
5062 goto restart;
5063 }
5064
5065 /* Calculate temperature */
5066 priv->temperature = iwl4965_get_temperature(priv);
5067
5068 /* Send pointers to protocol/runtime uCode image ... init code will
5069 * load and launch runtime uCode, which will send us another "Alive"
5070 * notification. */
5071 IWL_DEBUG_INFO("Initialization Alive received.\n");
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005072 if (iwl4965_set_ucode_ptrs(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07005073 /* Runtime instruction load won't happen;
5074 * take it all the way back down so we can try again */
5075 IWL_DEBUG_INFO("Couldn't set up uCode pointers.\n");
5076 goto restart;
5077 }
5078 return;
5079
5080 restart:
5081 queue_work(priv->workqueue, &priv->restart);
5082}
5083
5084
5085/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005086 * iwl4965_alive_start - called after REPLY_ALIVE notification received
Zhu Yib481de92007-09-25 17:54:57 -07005087 * from protocol/runtime uCode (initialization uCode's
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005088 * Alive gets handled by iwl4965_init_alive_start()).
Zhu Yib481de92007-09-25 17:54:57 -07005089 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005090static void iwl4965_alive_start(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005091{
Tomas Winkler57aab752008-04-14 21:16:03 -07005092 int ret = 0;
Zhu Yib481de92007-09-25 17:54:57 -07005093
5094 IWL_DEBUG_INFO("Runtime Alive received.\n");
5095
5096 if (priv->card_alive.is_valid != UCODE_VALID_OK) {
5097 /* We had an error bringing up the hardware, so take it
5098 * all the way back down so we can try again */
5099 IWL_DEBUG_INFO("Alive failed.\n");
5100 goto restart;
5101 }
5102
5103 /* Initialize uCode has loaded Runtime uCode ... verify inst image.
5104 * This is a paranoid check, because we would not have gotten the
5105 * "runtime" alive if code weren't properly loaded. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005106 if (iwl4965_verify_ucode(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07005107 /* Runtime instruction load was bad;
5108 * take it all the way back down so we can try again */
5109 IWL_DEBUG_INFO("Bad runtime uCode load.\n");
5110 goto restart;
5111 }
5112
Assaf Kraussbf85ea42008-03-14 10:38:49 -07005113 iwlcore_clear_stations_table(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005114
Tomas Winkler57aab752008-04-14 21:16:03 -07005115 ret = priv->cfg->ops->lib->alive_notify(priv);
5116 if (ret) {
Zhu Yib481de92007-09-25 17:54:57 -07005117 IWL_WARNING("Could not complete ALIVE transition [ntf]: %d\n",
Tomas Winkler57aab752008-04-14 21:16:03 -07005118 ret);
Zhu Yib481de92007-09-25 17:54:57 -07005119 goto restart;
5120 }
5121
Ben Cahill9fbab512007-11-29 11:09:47 +08005122 /* After the ALIVE response, we can send host commands to 4965 uCode */
Zhu Yib481de92007-09-25 17:54:57 -07005123 set_bit(STATUS_ALIVE, &priv->status);
5124
5125 /* Clear out the uCode error bit if it is set */
5126 clear_bit(STATUS_FW_ERROR, &priv->status);
5127
Tomas Winklerfee12472008-04-03 16:05:21 -07005128 if (iwl_is_rfkill(priv))
Zhu Yib481de92007-09-25 17:54:57 -07005129 return;
5130
Zhu Yi5a66926a2008-01-14 17:46:18 -08005131 ieee80211_start_queues(priv->hw);
Zhu Yib481de92007-09-25 17:54:57 -07005132
5133 priv->active_rate = priv->rates_mask;
5134 priv->active_rate_basic = priv->rates_mask & IWL_BASIC_RATES_MASK;
5135
Tomas Winkler3109ece2008-03-28 16:33:35 -07005136 if (iwl_is_associated(priv)) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005137 struct iwl4965_rxon_cmd *active_rxon =
5138 (struct iwl4965_rxon_cmd *)(&priv->active_rxon);
Zhu Yib481de92007-09-25 17:54:57 -07005139
5140 memcpy(&priv->staging_rxon, &priv->active_rxon,
5141 sizeof(priv->staging_rxon));
5142 active_rxon->filter_flags &= ~RXON_FILTER_ASSOC_MSK;
5143 } else {
5144 /* Initialize our rx_config data */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005145 iwl4965_connection_init_rx_config(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005146 memcpy(priv->staging_rxon.node_addr, priv->mac_addr, ETH_ALEN);
5147 }
5148
Ben Cahill9fbab512007-11-29 11:09:47 +08005149 /* Configure Bluetooth device coexistence support */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005150 iwl4965_send_bt_config(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005151
5152 /* Configure the adapter for unassociated operation */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005153 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005154
5155 /* At this point, the NIC is initialized and operational */
5156 priv->notif_missed_beacons = 0;
Zhu Yib481de92007-09-25 17:54:57 -07005157
5158 iwl4965_rf_kill_ct_config(priv);
Zhu Yi5a66926a2008-01-14 17:46:18 -08005159
Reinette Chatrefe00b5a2008-04-03 16:05:23 -07005160 iwl_leds_register(priv);
5161
Zhu Yib481de92007-09-25 17:54:57 -07005162 IWL_DEBUG_INFO("ALIVE processing complete.\n");
Rick Farringtona9f46782008-03-18 14:57:49 -07005163 set_bit(STATUS_READY, &priv->status);
Zhu Yi5a66926a2008-01-14 17:46:18 -08005164 wake_up_interruptible(&priv->wait_command_queue);
Zhu Yib481de92007-09-25 17:54:57 -07005165
5166 if (priv->error_recovering)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005167 iwl4965_error_recovery(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005168
Mohamed Abbasc8381fd2008-03-28 16:21:05 -07005169 iwlcore_low_level_notify(priv, IWLCORE_START_EVT);
Mohamed Abbas84363e62008-04-04 16:59:58 -07005170 ieee80211_notify_mac(priv->hw, IEEE80211_NOTIFY_RE_ASSOC);
Zhu Yib481de92007-09-25 17:54:57 -07005171 return;
5172
5173 restart:
5174 queue_work(priv->workqueue, &priv->restart);
5175}
5176
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005177static void iwl4965_cancel_deferred_work(struct iwl_priv *priv);
Zhu Yib481de92007-09-25 17:54:57 -07005178
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005179static void __iwl4965_down(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005180{
5181 unsigned long flags;
5182 int exit_pending = test_bit(STATUS_EXIT_PENDING, &priv->status);
5183 struct ieee80211_conf *conf = NULL;
5184
5185 IWL_DEBUG_INFO(DRV_NAME " is going down\n");
5186
5187 conf = ieee80211_get_hw_conf(priv->hw);
5188
5189 if (!exit_pending)
5190 set_bit(STATUS_EXIT_PENDING, &priv->status);
5191
Mohamed Abbasab53d8a2008-03-25 16:33:36 -07005192 iwl_leds_unregister(priv);
5193
Mohamed Abbasc8381fd2008-03-28 16:21:05 -07005194 iwlcore_low_level_notify(priv, IWLCORE_STOP_EVT);
5195
Assaf Kraussbf85ea42008-03-14 10:38:49 -07005196 iwlcore_clear_stations_table(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005197
5198 /* Unblock any waiting calls */
5199 wake_up_interruptible_all(&priv->wait_command_queue);
5200
Zhu Yib481de92007-09-25 17:54:57 -07005201 /* Wipe out the EXIT_PENDING status bit if we are not actually
5202 * exiting the module */
5203 if (!exit_pending)
5204 clear_bit(STATUS_EXIT_PENDING, &priv->status);
5205
5206 /* stop and reset the on-board processor */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07005207 iwl_write32(priv, CSR_RESET, CSR_RESET_REG_FLAG_NEVO_RESET);
Zhu Yib481de92007-09-25 17:54:57 -07005208
5209 /* tell the device to stop sending interrupts */
Mohamed Abbas0359fac2008-03-28 16:21:08 -07005210 spin_lock_irqsave(&priv->lock, flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005211 iwl4965_disable_interrupts(priv);
Mohamed Abbas0359fac2008-03-28 16:21:08 -07005212 spin_unlock_irqrestore(&priv->lock, flags);
5213 iwl_synchronize_irq(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005214
5215 if (priv->mac80211_registered)
5216 ieee80211_stop_queues(priv->hw);
5217
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005218 /* If we have not previously called iwl4965_init() then
Zhu Yib481de92007-09-25 17:54:57 -07005219 * clear all bits but the RF Kill and SUSPEND bits and return */
Tomas Winklerfee12472008-04-03 16:05:21 -07005220 if (!iwl_is_init(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07005221 priv->status = test_bit(STATUS_RF_KILL_HW, &priv->status) <<
5222 STATUS_RF_KILL_HW |
5223 test_bit(STATUS_RF_KILL_SW, &priv->status) <<
5224 STATUS_RF_KILL_SW |
Reinette Chatre97888642008-02-06 11:20:38 -08005225 test_bit(STATUS_GEO_CONFIGURED, &priv->status) <<
5226 STATUS_GEO_CONFIGURED |
Zhu Yib481de92007-09-25 17:54:57 -07005227 test_bit(STATUS_IN_SUSPEND, &priv->status) <<
5228 STATUS_IN_SUSPEND;
5229 goto exit;
5230 }
5231
5232 /* ...otherwise clear out all the status bits but the RF Kill and
5233 * SUSPEND bits and continue taking the NIC down. */
5234 priv->status &= test_bit(STATUS_RF_KILL_HW, &priv->status) <<
5235 STATUS_RF_KILL_HW |
5236 test_bit(STATUS_RF_KILL_SW, &priv->status) <<
5237 STATUS_RF_KILL_SW |
Reinette Chatre97888642008-02-06 11:20:38 -08005238 test_bit(STATUS_GEO_CONFIGURED, &priv->status) <<
5239 STATUS_GEO_CONFIGURED |
Zhu Yib481de92007-09-25 17:54:57 -07005240 test_bit(STATUS_IN_SUSPEND, &priv->status) <<
5241 STATUS_IN_SUSPEND |
5242 test_bit(STATUS_FW_ERROR, &priv->status) <<
5243 STATUS_FW_ERROR;
5244
5245 spin_lock_irqsave(&priv->lock, flags);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07005246 iwl_clear_bit(priv, CSR_GP_CNTRL,
Ben Cahill9fbab512007-11-29 11:09:47 +08005247 CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
Zhu Yib481de92007-09-25 17:54:57 -07005248 spin_unlock_irqrestore(&priv->lock, flags);
5249
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005250 iwl4965_hw_txq_ctx_stop(priv);
5251 iwl4965_hw_rxq_stop(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005252
5253 spin_lock_irqsave(&priv->lock, flags);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07005254 if (!iwl_grab_nic_access(priv)) {
5255 iwl_write_prph(priv, APMG_CLK_DIS_REG,
Zhu Yib481de92007-09-25 17:54:57 -07005256 APMG_CLK_VAL_DMA_CLK_RQT);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07005257 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005258 }
5259 spin_unlock_irqrestore(&priv->lock, flags);
5260
5261 udelay(5);
5262
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005263 iwl4965_hw_nic_stop_master(priv);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07005264 iwl_set_bit(priv, CSR_RESET, CSR_RESET_REG_FLAG_SW_RESET);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005265 iwl4965_hw_nic_reset(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005266
5267 exit:
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005268 memset(&priv->card_alive, 0, sizeof(struct iwl4965_alive_resp));
Zhu Yib481de92007-09-25 17:54:57 -07005269
5270 if (priv->ibss_beacon)
5271 dev_kfree_skb(priv->ibss_beacon);
5272 priv->ibss_beacon = NULL;
5273
5274 /* clear out any free frames */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005275 iwl4965_clear_free_frames(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005276}
5277
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005278static void iwl4965_down(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005279{
5280 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005281 __iwl4965_down(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005282 mutex_unlock(&priv->mutex);
Zhu Yib24d22b2007-12-19 13:59:52 +08005283
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005284 iwl4965_cancel_deferred_work(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005285}
5286
5287#define MAX_HW_RESTARTS 5
5288
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005289static int __iwl4965_up(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005290{
Tomas Winkler57aab752008-04-14 21:16:03 -07005291 int i;
5292 int ret;
Zhu Yib481de92007-09-25 17:54:57 -07005293
5294 if (test_bit(STATUS_EXIT_PENDING, &priv->status)) {
5295 IWL_WARNING("Exit pending; will not bring the NIC up\n");
5296 return -EIO;
5297 }
5298
5299 if (test_bit(STATUS_RF_KILL_SW, &priv->status)) {
5300 IWL_WARNING("Radio disabled by SW RF kill (module "
5301 "parameter)\n");
Mohamed Abbasad97edd2008-03-28 16:21:06 -07005302 iwl_rfkill_set_hw_state(priv);
Zhu Yie655b9f2008-01-24 02:19:38 -08005303 return -ENODEV;
5304 }
5305
Reinette Chatree903fbd2008-01-30 22:05:15 -08005306 if (!priv->ucode_data_backup.v_addr || !priv->ucode_data.v_addr) {
5307 IWL_ERROR("ucode not available for device bringup\n");
5308 return -EIO;
5309 }
5310
Zhu Yie655b9f2008-01-24 02:19:38 -08005311 /* If platform's RF_KILL switch is NOT set to KILL */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07005312 if (iwl_read32(priv, CSR_GP_CNTRL) &
Zhu Yie655b9f2008-01-24 02:19:38 -08005313 CSR_GP_CNTRL_REG_FLAG_HW_RF_KILL_SW)
5314 clear_bit(STATUS_RF_KILL_HW, &priv->status);
5315 else {
5316 set_bit(STATUS_RF_KILL_HW, &priv->status);
5317 if (!test_bit(STATUS_IN_SUSPEND, &priv->status)) {
Mohamed Abbasad97edd2008-03-28 16:21:06 -07005318 iwl_rfkill_set_hw_state(priv);
Zhu Yie655b9f2008-01-24 02:19:38 -08005319 IWL_WARNING("Radio disabled by HW RF Kill switch\n");
5320 return -ENODEV;
5321 }
Zhu Yib481de92007-09-25 17:54:57 -07005322 }
5323
Mohamed Abbasad97edd2008-03-28 16:21:06 -07005324 iwl_rfkill_set_hw_state(priv);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07005325 iwl_write32(priv, CSR_INT, 0xFFFFFFFF);
Zhu Yib481de92007-09-25 17:54:57 -07005326
Tomas Winkler57aab752008-04-14 21:16:03 -07005327 ret = priv->cfg->ops->lib->hw_nic_init(priv);
5328 if (ret) {
5329 IWL_ERROR("Unable to init nic\n");
5330 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07005331 }
5332
5333 /* make sure rfkill handshake bits are cleared */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07005334 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
5335 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR,
Zhu Yib481de92007-09-25 17:54:57 -07005336 CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
5337
5338 /* clear (again), then enable host interrupts */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07005339 iwl_write32(priv, CSR_INT, 0xFFFFFFFF);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005340 iwl4965_enable_interrupts(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005341
5342 /* really make sure rfkill handshake bits are cleared */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07005343 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
5344 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
Zhu Yib481de92007-09-25 17:54:57 -07005345
5346 /* Copy original ucode data image from disk into backup cache.
5347 * This will be used to initialize the on-board processor's
5348 * data SRAM for a clean start when the runtime program first loads. */
5349 memcpy(priv->ucode_data_backup.v_addr, priv->ucode_data.v_addr,
Zhu Yi5a66926a2008-01-14 17:46:18 -08005350 priv->ucode_data.len);
Zhu Yib481de92007-09-25 17:54:57 -07005351
Zhu Yie655b9f2008-01-24 02:19:38 -08005352 /* We return success when we resume from suspend and rf_kill is on. */
5353 if (test_bit(STATUS_RF_KILL_HW, &priv->status))
Zhu Yib481de92007-09-25 17:54:57 -07005354 return 0;
Zhu Yib481de92007-09-25 17:54:57 -07005355
5356 for (i = 0; i < MAX_HW_RESTARTS; i++) {
5357
Assaf Kraussbf85ea42008-03-14 10:38:49 -07005358 iwlcore_clear_stations_table(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005359
5360 /* load bootstrap state machine,
5361 * load bootstrap program into processor's memory,
5362 * prepare to load the "initialize" uCode */
Tomas Winkler57aab752008-04-14 21:16:03 -07005363 ret = priv->cfg->ops->lib->load_ucode(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005364
Tomas Winkler57aab752008-04-14 21:16:03 -07005365 if (ret) {
5366 IWL_ERROR("Unable to set up bootstrap uCode: %d\n", ret);
Zhu Yib481de92007-09-25 17:54:57 -07005367 continue;
5368 }
5369
5370 /* start card; "initialize" will load runtime ucode */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005371 iwl4965_nic_start(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005372
Zhu Yib481de92007-09-25 17:54:57 -07005373 IWL_DEBUG_INFO(DRV_NAME " is coming up\n");
5374
5375 return 0;
5376 }
5377
5378 set_bit(STATUS_EXIT_PENDING, &priv->status);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005379 __iwl4965_down(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005380
5381 /* tried to restart and config the device for as long as our
5382 * patience could withstand */
5383 IWL_ERROR("Unable to initialize device after %d attempts.\n", i);
5384 return -EIO;
5385}
5386
5387
5388/*****************************************************************************
5389 *
5390 * Workqueue callbacks
5391 *
5392 *****************************************************************************/
5393
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005394static void iwl4965_bg_init_alive_start(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07005395{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005396 struct iwl_priv *priv =
5397 container_of(data, struct iwl_priv, init_alive_start.work);
Zhu Yib481de92007-09-25 17:54:57 -07005398
5399 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
5400 return;
5401
5402 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005403 iwl4965_init_alive_start(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005404 mutex_unlock(&priv->mutex);
5405}
5406
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005407static void iwl4965_bg_alive_start(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07005408{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005409 struct iwl_priv *priv =
5410 container_of(data, struct iwl_priv, alive_start.work);
Zhu Yib481de92007-09-25 17:54:57 -07005411
5412 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
5413 return;
5414
5415 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005416 iwl4965_alive_start(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005417 mutex_unlock(&priv->mutex);
5418}
5419
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005420static void iwl4965_bg_rf_kill(struct work_struct *work)
Zhu Yib481de92007-09-25 17:54:57 -07005421{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005422 struct iwl_priv *priv = container_of(work, struct iwl_priv, rf_kill);
Zhu Yib481de92007-09-25 17:54:57 -07005423
5424 wake_up_interruptible(&priv->wait_command_queue);
5425
5426 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
5427 return;
5428
5429 mutex_lock(&priv->mutex);
5430
Tomas Winklerfee12472008-04-03 16:05:21 -07005431 if (!iwl_is_rfkill(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07005432 IWL_DEBUG(IWL_DL_INFO | IWL_DL_RF_KILL,
5433 "HW and/or SW RF Kill no longer active, restarting "
5434 "device\n");
5435 if (!test_bit(STATUS_EXIT_PENDING, &priv->status))
5436 queue_work(priv->workqueue, &priv->restart);
5437 } else {
Mohamed Abbasad97edd2008-03-28 16:21:06 -07005438 /* make sure mac80211 stop sending Tx frame */
5439 if (priv->mac80211_registered)
5440 ieee80211_stop_queues(priv->hw);
Zhu Yib481de92007-09-25 17:54:57 -07005441
5442 if (!test_bit(STATUS_RF_KILL_HW, &priv->status))
5443 IWL_DEBUG_RF_KILL("Can not turn radio back on - "
5444 "disabled by SW switch\n");
5445 else
5446 IWL_WARNING("Radio Frequency Kill Switch is On:\n"
5447 "Kill switch must be turned off for "
5448 "wireless networking to work.\n");
5449 }
Mohamed Abbasad97edd2008-03-28 16:21:06 -07005450 iwl_rfkill_set_hw_state(priv);
5451
Zhu Yib481de92007-09-25 17:54:57 -07005452 mutex_unlock(&priv->mutex);
5453}
5454
5455#define IWL_SCAN_CHECK_WATCHDOG (7 * HZ)
5456
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005457static void iwl4965_bg_scan_check(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07005458{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005459 struct iwl_priv *priv =
5460 container_of(data, struct iwl_priv, scan_check.work);
Zhu Yib481de92007-09-25 17:54:57 -07005461
5462 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
5463 return;
5464
5465 mutex_lock(&priv->mutex);
5466 if (test_bit(STATUS_SCANNING, &priv->status) ||
5467 test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
5468 IWL_DEBUG(IWL_DL_INFO | IWL_DL_SCAN,
5469 "Scan completion watchdog resetting adapter (%dms)\n",
5470 jiffies_to_msecs(IWL_SCAN_CHECK_WATCHDOG));
mabbas052c4b92007-10-25 17:15:43 +08005471
Zhu Yib481de92007-09-25 17:54:57 -07005472 if (!test_bit(STATUS_EXIT_PENDING, &priv->status))
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005473 iwl4965_send_scan_abort(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005474 }
5475 mutex_unlock(&priv->mutex);
5476}
5477
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005478static void iwl4965_bg_request_scan(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07005479{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005480 struct iwl_priv *priv =
5481 container_of(data, struct iwl_priv, request_scan);
Tomas Winkler857485c2008-03-21 13:53:44 -07005482 struct iwl_host_cmd cmd = {
Zhu Yib481de92007-09-25 17:54:57 -07005483 .id = REPLY_SCAN_CMD,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005484 .len = sizeof(struct iwl4965_scan_cmd),
Zhu Yib481de92007-09-25 17:54:57 -07005485 .meta.flags = CMD_SIZE_HUGE,
5486 };
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005487 struct iwl4965_scan_cmd *scan;
Zhu Yib481de92007-09-25 17:54:57 -07005488 struct ieee80211_conf *conf = NULL;
Tomas Winkler78330fd2008-02-06 02:37:18 +02005489 u16 cmd_len;
Johannes Berg8318d782008-01-24 19:38:38 +01005490 enum ieee80211_band band;
Tomas Winkler78330fd2008-02-06 02:37:18 +02005491 u8 direct_mask;
Tomas Winkler857485c2008-03-21 13:53:44 -07005492 int ret = 0;
Zhu Yib481de92007-09-25 17:54:57 -07005493
5494 conf = ieee80211_get_hw_conf(priv->hw);
5495
5496 mutex_lock(&priv->mutex);
5497
Tomas Winklerfee12472008-04-03 16:05:21 -07005498 if (!iwl_is_ready(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07005499 IWL_WARNING("request scan called when driver not ready.\n");
5500 goto done;
5501 }
5502
5503 /* Make sure the scan wasn't cancelled before this queued work
5504 * was given the chance to run... */
5505 if (!test_bit(STATUS_SCANNING, &priv->status))
5506 goto done;
5507
5508 /* This should never be called or scheduled if there is currently
5509 * a scan active in the hardware. */
5510 if (test_bit(STATUS_SCAN_HW, &priv->status)) {
5511 IWL_DEBUG_INFO("Multiple concurrent scan requests in parallel. "
5512 "Ignoring second request.\n");
Tomas Winkler857485c2008-03-21 13:53:44 -07005513 ret = -EIO;
Zhu Yib481de92007-09-25 17:54:57 -07005514 goto done;
5515 }
5516
5517 if (test_bit(STATUS_EXIT_PENDING, &priv->status)) {
5518 IWL_DEBUG_SCAN("Aborting scan due to device shutdown\n");
5519 goto done;
5520 }
5521
5522 if (test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
5523 IWL_DEBUG_HC("Scan request while abort pending. Queuing.\n");
5524 goto done;
5525 }
5526
Tomas Winklerfee12472008-04-03 16:05:21 -07005527 if (iwl_is_rfkill(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07005528 IWL_DEBUG_HC("Aborting scan due to RF Kill activation\n");
5529 goto done;
5530 }
5531
5532 if (!test_bit(STATUS_READY, &priv->status)) {
5533 IWL_DEBUG_HC("Scan request while uninitialized. Queuing.\n");
5534 goto done;
5535 }
5536
5537 if (!priv->scan_bands) {
5538 IWL_DEBUG_HC("Aborting scan due to no requested bands\n");
5539 goto done;
5540 }
5541
5542 if (!priv->scan) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005543 priv->scan = kmalloc(sizeof(struct iwl4965_scan_cmd) +
Zhu Yib481de92007-09-25 17:54:57 -07005544 IWL_MAX_SCAN_SIZE, GFP_KERNEL);
5545 if (!priv->scan) {
Tomas Winkler857485c2008-03-21 13:53:44 -07005546 ret = -ENOMEM;
Zhu Yib481de92007-09-25 17:54:57 -07005547 goto done;
5548 }
5549 }
5550 scan = priv->scan;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005551 memset(scan, 0, sizeof(struct iwl4965_scan_cmd) + IWL_MAX_SCAN_SIZE);
Zhu Yib481de92007-09-25 17:54:57 -07005552
5553 scan->quiet_plcp_th = IWL_PLCP_QUIET_THRESH;
5554 scan->quiet_time = IWL_ACTIVE_QUIET_TIME;
5555
Tomas Winkler3109ece2008-03-28 16:33:35 -07005556 if (iwl_is_associated(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07005557 u16 interval = 0;
5558 u32 extra;
5559 u32 suspend_time = 100;
5560 u32 scan_suspend_time = 100;
5561 unsigned long flags;
5562
5563 IWL_DEBUG_INFO("Scanning while associated...\n");
5564
5565 spin_lock_irqsave(&priv->lock, flags);
5566 interval = priv->beacon_int;
5567 spin_unlock_irqrestore(&priv->lock, flags);
5568
5569 scan->suspend_time = 0;
mabbas052c4b92007-10-25 17:15:43 +08005570 scan->max_out_time = cpu_to_le32(200 * 1024);
Zhu Yib481de92007-09-25 17:54:57 -07005571 if (!interval)
5572 interval = suspend_time;
5573
5574 extra = (suspend_time / interval) << 22;
5575 scan_suspend_time = (extra |
5576 ((suspend_time % interval) * 1024));
5577 scan->suspend_time = cpu_to_le32(scan_suspend_time);
5578 IWL_DEBUG_SCAN("suspend_time 0x%X beacon interval %d\n",
5579 scan_suspend_time, interval);
5580 }
5581
5582 /* We should add the ability for user to lock to PASSIVE ONLY */
5583 if (priv->one_direct_scan) {
5584 IWL_DEBUG_SCAN
5585 ("Kicking off one direct scan for '%s'\n",
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005586 iwl4965_escape_essid(priv->direct_ssid,
Zhu Yib481de92007-09-25 17:54:57 -07005587 priv->direct_ssid_len));
5588 scan->direct_scan[0].id = WLAN_EID_SSID;
5589 scan->direct_scan[0].len = priv->direct_ssid_len;
5590 memcpy(scan->direct_scan[0].ssid,
5591 priv->direct_ssid, priv->direct_ssid_len);
5592 direct_mask = 1;
Tomas Winkler3109ece2008-03-28 16:33:35 -07005593 } else if (!iwl_is_associated(priv) && priv->essid_len) {
Bill Moss786b4552008-04-17 16:03:40 -07005594 IWL_DEBUG_SCAN
5595 ("Kicking off one direct scan for '%s' when not associated\n",
5596 iwl4965_escape_essid(priv->essid, priv->essid_len));
Zhu Yib481de92007-09-25 17:54:57 -07005597 scan->direct_scan[0].id = WLAN_EID_SSID;
5598 scan->direct_scan[0].len = priv->essid_len;
5599 memcpy(scan->direct_scan[0].ssid, priv->essid, priv->essid_len);
5600 direct_mask = 1;
Tomas Winkler857485c2008-03-21 13:53:44 -07005601 } else {
Bill Moss786b4552008-04-17 16:03:40 -07005602 IWL_DEBUG_SCAN("Kicking off one indirect scan.\n");
Zhu Yib481de92007-09-25 17:54:57 -07005603 direct_mask = 0;
Tomas Winkler857485c2008-03-21 13:53:44 -07005604 }
Zhu Yib481de92007-09-25 17:54:57 -07005605
Zhu Yib481de92007-09-25 17:54:57 -07005606 scan->tx_cmd.tx_flags = TX_CMD_FLG_SEQ_CTL_MSK;
Tomas Winkler5425e492008-04-15 16:01:38 -07005607 scan->tx_cmd.sta_id = priv->hw_params.bcast_sta_id;
Zhu Yib481de92007-09-25 17:54:57 -07005608 scan->tx_cmd.stop_time.life_time = TX_CMD_LIFE_TIME_INFINITE;
5609
Zhu Yib481de92007-09-25 17:54:57 -07005610
5611 switch (priv->scan_bands) {
5612 case 2:
5613 scan->flags = RXON_FLG_BAND_24G_MSK | RXON_FLG_AUTO_DETECT_MSK;
5614 scan->tx_cmd.rate_n_flags =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005615 iwl4965_hw_set_rate_n_flags(IWL_RATE_1M_PLCP,
Zhu Yib481de92007-09-25 17:54:57 -07005616 RATE_MCS_ANT_B_MSK|RATE_MCS_CCK_MSK);
5617
5618 scan->good_CRC_th = 0;
Johannes Berg8318d782008-01-24 19:38:38 +01005619 band = IEEE80211_BAND_2GHZ;
Zhu Yib481de92007-09-25 17:54:57 -07005620 break;
5621
5622 case 1:
5623 scan->tx_cmd.rate_n_flags =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005624 iwl4965_hw_set_rate_n_flags(IWL_RATE_6M_PLCP,
Zhu Yib481de92007-09-25 17:54:57 -07005625 RATE_MCS_ANT_B_MSK);
5626 scan->good_CRC_th = IWL_GOOD_CRC_TH;
Johannes Berg8318d782008-01-24 19:38:38 +01005627 band = IEEE80211_BAND_5GHZ;
Zhu Yib481de92007-09-25 17:54:57 -07005628 break;
5629
5630 default:
5631 IWL_WARNING("Invalid scan band count\n");
5632 goto done;
5633 }
5634
Tomas Winkler78330fd2008-02-06 02:37:18 +02005635 /* We don't build a direct scan probe request; the uCode will do
5636 * that based on the direct_mask added to each channel entry */
5637 cmd_len = iwl4965_fill_probe_req(priv, band,
5638 (struct ieee80211_mgmt *)scan->data,
5639 IWL_MAX_SCAN_SIZE - sizeof(*scan), 0);
5640
5641 scan->tx_cmd.len = cpu_to_le16(cmd_len);
Zhu Yib481de92007-09-25 17:54:57 -07005642 /* select Rx chains */
5643
5644 /* Force use of chains B and C (0x6) for scan Rx.
5645 * Avoid A (0x1) because of its off-channel reception on A-band.
5646 * MIMO is not used here, but value is required to make uCode happy. */
5647 scan->rx_chain = RXON_RX_CHAIN_DRIVER_FORCE_MSK |
5648 cpu_to_le16((0x7 << RXON_RX_CHAIN_VALID_POS) |
5649 (0x6 << RXON_RX_CHAIN_FORCE_SEL_POS) |
5650 (0x7 << RXON_RX_CHAIN_FORCE_MIMO_SEL_POS));
5651
5652 if (priv->iw_mode == IEEE80211_IF_TYPE_MNTR)
5653 scan->filter_flags = RXON_FILTER_PROMISC_MSK;
5654
Bill Moss786b4552008-04-17 16:03:40 -07005655 if (direct_mask)
Reinette Chatre26c0f032008-03-11 16:17:15 -07005656 scan->channel_count =
5657 iwl4965_get_channels_for_scan(
5658 priv, band, 1, /* active */
5659 direct_mask,
5660 (void *)&scan->data[le16_to_cpu(scan->tx_cmd.len)]);
Bill Moss786b4552008-04-17 16:03:40 -07005661 else
Reinette Chatre26c0f032008-03-11 16:17:15 -07005662 scan->channel_count =
5663 iwl4965_get_channels_for_scan(
5664 priv, band, 0, /* passive */
5665 direct_mask,
5666 (void *)&scan->data[le16_to_cpu(scan->tx_cmd.len)]);
Zhu Yib481de92007-09-25 17:54:57 -07005667
Mohamed Abbas5da4b552008-04-21 15:41:51 -07005668 scan->filter_flags |= (RXON_FILTER_ACCEPT_GRP_MSK |
5669 RXON_FILTER_BCON_AWARE_MSK);
Zhu Yib481de92007-09-25 17:54:57 -07005670 cmd.len += le16_to_cpu(scan->tx_cmd.len) +
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005671 scan->channel_count * sizeof(struct iwl4965_scan_channel);
Zhu Yib481de92007-09-25 17:54:57 -07005672 cmd.data = scan;
5673 scan->len = cpu_to_le16(cmd.len);
5674
5675 set_bit(STATUS_SCAN_HW, &priv->status);
Tomas Winkler857485c2008-03-21 13:53:44 -07005676 ret = iwl_send_cmd_sync(priv, &cmd);
5677 if (ret)
Zhu Yib481de92007-09-25 17:54:57 -07005678 goto done;
5679
5680 queue_delayed_work(priv->workqueue, &priv->scan_check,
5681 IWL_SCAN_CHECK_WATCHDOG);
5682
5683 mutex_unlock(&priv->mutex);
5684 return;
5685
5686 done:
Ian Schram01ebd062007-10-25 17:15:22 +08005687 /* inform mac80211 scan aborted */
Zhu Yib481de92007-09-25 17:54:57 -07005688 queue_work(priv->workqueue, &priv->scan_completed);
5689 mutex_unlock(&priv->mutex);
5690}
5691
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005692static void iwl4965_bg_up(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07005693{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005694 struct iwl_priv *priv = container_of(data, struct iwl_priv, up);
Zhu Yib481de92007-09-25 17:54:57 -07005695
5696 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
5697 return;
5698
5699 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005700 __iwl4965_up(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005701 mutex_unlock(&priv->mutex);
5702}
5703
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005704static void iwl4965_bg_restart(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07005705{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005706 struct iwl_priv *priv = container_of(data, struct iwl_priv, restart);
Zhu Yib481de92007-09-25 17:54:57 -07005707
5708 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
5709 return;
5710
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005711 iwl4965_down(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005712 queue_work(priv->workqueue, &priv->up);
5713}
5714
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005715static void iwl4965_bg_rx_replenish(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07005716{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005717 struct iwl_priv *priv =
5718 container_of(data, struct iwl_priv, rx_replenish);
Zhu Yib481de92007-09-25 17:54:57 -07005719
5720 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
5721 return;
5722
5723 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005724 iwl4965_rx_replenish(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005725 mutex_unlock(&priv->mutex);
5726}
5727
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08005728#define IWL_DELAY_NEXT_SCAN (HZ*2)
5729
Reinette Chatre508e32e2008-04-14 21:16:13 -07005730static void iwl4965_post_associate(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005731{
Zhu Yib481de92007-09-25 17:54:57 -07005732 struct ieee80211_conf *conf = NULL;
Tomas Winkler857485c2008-03-21 13:53:44 -07005733 int ret = 0;
Joe Perches0795af52007-10-03 17:59:30 -07005734 DECLARE_MAC_BUF(mac);
Zhu Yib481de92007-09-25 17:54:57 -07005735
5736 if (priv->iw_mode == IEEE80211_IF_TYPE_AP) {
5737 IWL_ERROR("%s Should not be called in AP mode\n", __FUNCTION__);
5738 return;
5739 }
5740
Joe Perches0795af52007-10-03 17:59:30 -07005741 IWL_DEBUG_ASSOC("Associated as %d to: %s\n",
5742 priv->assoc_id,
5743 print_mac(mac, priv->active_rxon.bssid_addr));
Zhu Yib481de92007-09-25 17:54:57 -07005744
5745
5746 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
5747 return;
5748
Zhu Yib481de92007-09-25 17:54:57 -07005749
Reinette Chatre508e32e2008-04-14 21:16:13 -07005750 if (!priv->vif || !priv->is_open)
Mohamed Abbas948c1712007-10-25 17:15:45 +08005751 return;
Reinette Chatre508e32e2008-04-14 21:16:13 -07005752
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005753 iwl4965_scan_cancel_timeout(priv, 200);
mabbas052c4b92007-10-25 17:15:43 +08005754
Zhu Yib481de92007-09-25 17:54:57 -07005755 conf = ieee80211_get_hw_conf(priv->hw);
5756
5757 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005758 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005759
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005760 memset(&priv->rxon_timing, 0, sizeof(struct iwl4965_rxon_time_cmd));
5761 iwl4965_setup_rxon_timing(priv);
Tomas Winkler857485c2008-03-21 13:53:44 -07005762 ret = iwl_send_cmd_pdu(priv, REPLY_RXON_TIMING,
Zhu Yib481de92007-09-25 17:54:57 -07005763 sizeof(priv->rxon_timing), &priv->rxon_timing);
Tomas Winkler857485c2008-03-21 13:53:44 -07005764 if (ret)
Zhu Yib481de92007-09-25 17:54:57 -07005765 IWL_WARNING("REPLY_RXON_TIMING failed - "
5766 "Attempting to continue.\n");
5767
5768 priv->staging_rxon.filter_flags |= RXON_FILTER_ASSOC_MSK;
5769
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08005770#ifdef CONFIG_IWL4965_HT
Ron Rindjunskyfd105e72007-11-26 16:14:39 +02005771 if (priv->current_ht_config.is_ht)
5772 iwl4965_set_rxon_ht(priv, &priv->current_ht_config);
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08005773#endif /* CONFIG_IWL4965_HT*/
Zhu Yib481de92007-09-25 17:54:57 -07005774 iwl4965_set_rxon_chain(priv);
5775 priv->staging_rxon.assoc_id = cpu_to_le16(priv->assoc_id);
5776
5777 IWL_DEBUG_ASSOC("assoc id %d beacon interval %d\n",
5778 priv->assoc_id, priv->beacon_int);
5779
5780 if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_PREAMBLE)
5781 priv->staging_rxon.flags |= RXON_FLG_SHORT_PREAMBLE_MSK;
5782 else
5783 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_PREAMBLE_MSK;
5784
5785 if (priv->staging_rxon.flags & RXON_FLG_BAND_24G_MSK) {
5786 if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_SLOT_TIME)
5787 priv->staging_rxon.flags |= RXON_FLG_SHORT_SLOT_MSK;
5788 else
5789 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
5790
5791 if (priv->iw_mode == IEEE80211_IF_TYPE_IBSS)
5792 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
5793
5794 }
5795
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005796 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005797
5798 switch (priv->iw_mode) {
5799 case IEEE80211_IF_TYPE_STA:
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005800 iwl4965_rate_scale_init(priv->hw, IWL_AP_ID);
Zhu Yib481de92007-09-25 17:54:57 -07005801 break;
5802
5803 case IEEE80211_IF_TYPE_IBSS:
5804
5805 /* clear out the station table */
Assaf Kraussbf85ea42008-03-14 10:38:49 -07005806 iwlcore_clear_stations_table(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005807
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005808 iwl4965_rxon_add_station(priv, iwl4965_broadcast_addr, 0);
5809 iwl4965_rxon_add_station(priv, priv->bssid, 0);
5810 iwl4965_rate_scale_init(priv->hw, IWL_STA_ID);
5811 iwl4965_send_beacon_cmd(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005812
5813 break;
5814
5815 default:
5816 IWL_ERROR("%s Should not be called in %d mode\n",
5817 __FUNCTION__, priv->iw_mode);
5818 break;
5819 }
5820
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005821 iwl4965_sequence_reset(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005822
Zhu Yib481de92007-09-25 17:54:57 -07005823 /* Enable Rx differential gain and sensitivity calibrations */
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -07005824 iwl_chain_noise_reset(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005825 priv->start_calib = 1;
Zhu Yib481de92007-09-25 17:54:57 -07005826
5827 if (priv->iw_mode == IEEE80211_IF_TYPE_IBSS)
5828 priv->assoc_station_added = 1;
5829
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005830 iwl4965_activate_qos(priv, 0);
Ron Rindjunsky292ae172008-02-06 11:20:39 -08005831
Mohamed Abbas5da4b552008-04-21 15:41:51 -07005832 iwl_power_update_mode(priv, 0);
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08005833 /* we have just associated, don't start scan too early */
5834 priv->next_scan_jiffies = jiffies + IWL_DELAY_NEXT_SCAN;
Reinette Chatre508e32e2008-04-14 21:16:13 -07005835}
5836
5837
5838static void iwl4965_bg_post_associate(struct work_struct *data)
5839{
5840 struct iwl_priv *priv = container_of(data, struct iwl_priv,
5841 post_associate.work);
5842
5843 mutex_lock(&priv->mutex);
5844 iwl4965_post_associate(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005845 mutex_unlock(&priv->mutex);
Reinette Chatre508e32e2008-04-14 21:16:13 -07005846
Zhu Yib481de92007-09-25 17:54:57 -07005847}
5848
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005849static void iwl4965_bg_abort_scan(struct work_struct *work)
Zhu Yib481de92007-09-25 17:54:57 -07005850{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005851 struct iwl_priv *priv = container_of(work, struct iwl_priv, abort_scan);
Zhu Yib481de92007-09-25 17:54:57 -07005852
Tomas Winklerfee12472008-04-03 16:05:21 -07005853 if (!iwl_is_ready(priv))
Zhu Yib481de92007-09-25 17:54:57 -07005854 return;
5855
5856 mutex_lock(&priv->mutex);
5857
5858 set_bit(STATUS_SCAN_ABORTING, &priv->status);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005859 iwl4965_send_scan_abort(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005860
5861 mutex_unlock(&priv->mutex);
5862}
5863
Zhu Yi76bb77e2007-11-22 10:53:22 +08005864static int iwl4965_mac_config(struct ieee80211_hw *hw, struct ieee80211_conf *conf);
5865
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005866static void iwl4965_bg_scan_completed(struct work_struct *work)
Zhu Yib481de92007-09-25 17:54:57 -07005867{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005868 struct iwl_priv *priv =
5869 container_of(work, struct iwl_priv, scan_completed);
Zhu Yib481de92007-09-25 17:54:57 -07005870
5871 IWL_DEBUG(IWL_DL_INFO | IWL_DL_SCAN, "SCAN complete scan\n");
5872
5873 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
5874 return;
5875
Zhu Yia0646472007-12-20 14:10:01 +08005876 if (test_bit(STATUS_CONF_PENDING, &priv->status))
5877 iwl4965_mac_config(priv->hw, ieee80211_get_hw_conf(priv->hw));
Zhu Yi76bb77e2007-11-22 10:53:22 +08005878
Zhu Yib481de92007-09-25 17:54:57 -07005879 ieee80211_scan_completed(priv->hw);
5880
5881 /* Since setting the TXPOWER may have been deferred while
5882 * performing the scan, fire one off */
5883 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005884 iwl4965_hw_reg_send_txpower(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005885 mutex_unlock(&priv->mutex);
5886}
5887
5888/*****************************************************************************
5889 *
5890 * mac80211 entry point functions
5891 *
5892 *****************************************************************************/
5893
Zhu Yi5a66926a2008-01-14 17:46:18 -08005894#define UCODE_READY_TIMEOUT (2 * HZ)
5895
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005896static int iwl4965_mac_start(struct ieee80211_hw *hw)
Zhu Yib481de92007-09-25 17:54:57 -07005897{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005898 struct iwl_priv *priv = hw->priv;
Zhu Yi5a66926a2008-01-14 17:46:18 -08005899 int ret;
Zhu Yib481de92007-09-25 17:54:57 -07005900
5901 IWL_DEBUG_MAC80211("enter\n");
5902
Zhu Yi5a66926a2008-01-14 17:46:18 -08005903 if (pci_enable_device(priv->pci_dev)) {
5904 IWL_ERROR("Fail to pci_enable_device\n");
5905 return -ENODEV;
5906 }
5907 pci_restore_state(priv->pci_dev);
5908 pci_enable_msi(priv->pci_dev);
5909
5910 ret = request_irq(priv->pci_dev->irq, iwl4965_isr, IRQF_SHARED,
5911 DRV_NAME, priv);
5912 if (ret) {
5913 IWL_ERROR("Error allocating IRQ %d\n", priv->pci_dev->irq);
5914 goto out_disable_msi;
5915 }
5916
Zhu Yib481de92007-09-25 17:54:57 -07005917 /* we should be verifying the device is ready to be opened */
5918 mutex_lock(&priv->mutex);
5919
Zhu Yi5a66926a2008-01-14 17:46:18 -08005920 memset(&priv->staging_rxon, 0, sizeof(struct iwl4965_rxon_cmd));
5921 /* fetch ucode file from disk, alloc and copy to bus-master buffers ...
5922 * ucode filename and max sizes are card-specific. */
5923
5924 if (!priv->ucode_code.len) {
5925 ret = iwl4965_read_ucode(priv);
5926 if (ret) {
5927 IWL_ERROR("Could not read microcode: %d\n", ret);
5928 mutex_unlock(&priv->mutex);
5929 goto out_release_irq;
5930 }
5931 }
5932
Zhu Yie655b9f2008-01-24 02:19:38 -08005933 ret = __iwl4965_up(priv);
Zhu Yi5a66926a2008-01-14 17:46:18 -08005934
Zhu Yib481de92007-09-25 17:54:57 -07005935 mutex_unlock(&priv->mutex);
Zhu Yi5a66926a2008-01-14 17:46:18 -08005936
Zhu Yie655b9f2008-01-24 02:19:38 -08005937 if (ret)
5938 goto out_release_irq;
5939
5940 IWL_DEBUG_INFO("Start UP work done.\n");
5941
5942 if (test_bit(STATUS_IN_SUSPEND, &priv->status))
5943 return 0;
5944
Zhu Yi5a66926a2008-01-14 17:46:18 -08005945 /* Wait for START_ALIVE from ucode. Otherwise callbacks from
5946 * mac80211 will not be run successfully. */
5947 ret = wait_event_interruptible_timeout(priv->wait_command_queue,
5948 test_bit(STATUS_READY, &priv->status),
5949 UCODE_READY_TIMEOUT);
5950 if (!ret) {
5951 if (!test_bit(STATUS_READY, &priv->status)) {
5952 IWL_ERROR("Wait for START_ALIVE timeout after %dms.\n",
5953 jiffies_to_msecs(UCODE_READY_TIMEOUT));
5954 ret = -ETIMEDOUT;
5955 goto out_release_irq;
5956 }
5957 }
5958
Zhu Yie655b9f2008-01-24 02:19:38 -08005959 priv->is_open = 1;
Zhu Yib481de92007-09-25 17:54:57 -07005960 IWL_DEBUG_MAC80211("leave\n");
5961 return 0;
Zhu Yi5a66926a2008-01-14 17:46:18 -08005962
5963out_release_irq:
5964 free_irq(priv->pci_dev->irq, priv);
5965out_disable_msi:
5966 pci_disable_msi(priv->pci_dev);
Zhu Yie655b9f2008-01-24 02:19:38 -08005967 pci_disable_device(priv->pci_dev);
5968 priv->is_open = 0;
5969 IWL_DEBUG_MAC80211("leave - failed\n");
Zhu Yi5a66926a2008-01-14 17:46:18 -08005970 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07005971}
5972
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005973static void iwl4965_mac_stop(struct ieee80211_hw *hw)
Zhu Yib481de92007-09-25 17:54:57 -07005974{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005975 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07005976
5977 IWL_DEBUG_MAC80211("enter\n");
Mohamed Abbas948c1712007-10-25 17:15:45 +08005978
Zhu Yie655b9f2008-01-24 02:19:38 -08005979 if (!priv->is_open) {
5980 IWL_DEBUG_MAC80211("leave - skip\n");
5981 return;
5982 }
5983
Zhu Yib481de92007-09-25 17:54:57 -07005984 priv->is_open = 0;
Zhu Yi5a66926a2008-01-14 17:46:18 -08005985
Tomas Winklerfee12472008-04-03 16:05:21 -07005986 if (iwl_is_ready_rf(priv)) {
Zhu Yie655b9f2008-01-24 02:19:38 -08005987 /* stop mac, cancel any scan request and clear
5988 * RXON_FILTER_ASSOC_MSK BIT
5989 */
Zhu Yi5a66926a2008-01-14 17:46:18 -08005990 mutex_lock(&priv->mutex);
5991 iwl4965_scan_cancel_timeout(priv, 100);
5992 cancel_delayed_work(&priv->post_associate);
Mohamed Abbasfde35712007-11-29 11:10:15 +08005993 mutex_unlock(&priv->mutex);
Mohamed Abbasfde35712007-11-29 11:10:15 +08005994 }
5995
Zhu Yi5a66926a2008-01-14 17:46:18 -08005996 iwl4965_down(priv);
5997
5998 flush_workqueue(priv->workqueue);
5999 free_irq(priv->pci_dev->irq, priv);
6000 pci_disable_msi(priv->pci_dev);
6001 pci_save_state(priv->pci_dev);
6002 pci_disable_device(priv->pci_dev);
Mohamed Abbas948c1712007-10-25 17:15:45 +08006003
Zhu Yib481de92007-09-25 17:54:57 -07006004 IWL_DEBUG_MAC80211("leave\n");
Zhu Yib481de92007-09-25 17:54:57 -07006005}
6006
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006007static int iwl4965_mac_tx(struct ieee80211_hw *hw, struct sk_buff *skb,
Zhu Yib481de92007-09-25 17:54:57 -07006008 struct ieee80211_tx_control *ctl)
6009{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006010 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07006011
6012 IWL_DEBUG_MAC80211("enter\n");
6013
6014 if (priv->iw_mode == IEEE80211_IF_TYPE_MNTR) {
6015 IWL_DEBUG_MAC80211("leave - monitor\n");
6016 return -1;
6017 }
6018
6019 IWL_DEBUG_TX("dev->xmit(%d bytes) at rate 0x%02x\n", skb->len,
Johannes Berg8318d782008-01-24 19:38:38 +01006020 ctl->tx_rate->bitrate);
Zhu Yib481de92007-09-25 17:54:57 -07006021
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006022 if (iwl4965_tx_skb(priv, skb, ctl))
Zhu Yib481de92007-09-25 17:54:57 -07006023 dev_kfree_skb_any(skb);
6024
6025 IWL_DEBUG_MAC80211("leave\n");
6026 return 0;
6027}
6028
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006029static int iwl4965_mac_add_interface(struct ieee80211_hw *hw,
Zhu Yib481de92007-09-25 17:54:57 -07006030 struct ieee80211_if_init_conf *conf)
6031{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006032 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07006033 unsigned long flags;
Joe Perches0795af52007-10-03 17:59:30 -07006034 DECLARE_MAC_BUF(mac);
Zhu Yib481de92007-09-25 17:54:57 -07006035
Johannes Berg32bfd352007-12-19 01:31:26 +01006036 IWL_DEBUG_MAC80211("enter: type %d\n", conf->type);
Zhu Yib481de92007-09-25 17:54:57 -07006037
Johannes Berg32bfd352007-12-19 01:31:26 +01006038 if (priv->vif) {
6039 IWL_DEBUG_MAC80211("leave - vif != NULL\n");
Reinette Chatre75849d22008-01-23 10:15:17 -08006040 return -EOPNOTSUPP;
Zhu Yib481de92007-09-25 17:54:57 -07006041 }
6042
6043 spin_lock_irqsave(&priv->lock, flags);
Johannes Berg32bfd352007-12-19 01:31:26 +01006044 priv->vif = conf->vif;
Zhu Yib481de92007-09-25 17:54:57 -07006045
6046 spin_unlock_irqrestore(&priv->lock, flags);
6047
6048 mutex_lock(&priv->mutex);
Tomas Winkler864792e2007-11-27 21:00:52 +02006049
6050 if (conf->mac_addr) {
6051 IWL_DEBUG_MAC80211("Set %s\n", print_mac(mac, conf->mac_addr));
6052 memcpy(priv->mac_addr, conf->mac_addr, ETH_ALEN);
6053 }
Zhu Yib481de92007-09-25 17:54:57 -07006054
Tomas Winklerfee12472008-04-03 16:05:21 -07006055 if (iwl_is_ready(priv))
Zhu Yi5a66926a2008-01-14 17:46:18 -08006056 iwl4965_set_mode(priv, conf->type);
6057
Zhu Yib481de92007-09-25 17:54:57 -07006058 mutex_unlock(&priv->mutex);
6059
Zhu Yi5a66926a2008-01-14 17:46:18 -08006060 IWL_DEBUG_MAC80211("leave\n");
Zhu Yib481de92007-09-25 17:54:57 -07006061 return 0;
6062}
6063
6064/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006065 * iwl4965_mac_config - mac80211 config callback
Zhu Yib481de92007-09-25 17:54:57 -07006066 *
6067 * We ignore conf->flags & IEEE80211_CONF_SHORT_SLOT_TIME since it seems to
6068 * be set inappropriately and the driver currently sets the hardware up to
6069 * use it whenever needed.
6070 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006071static int iwl4965_mac_config(struct ieee80211_hw *hw, struct ieee80211_conf *conf)
Zhu Yib481de92007-09-25 17:54:57 -07006072{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006073 struct iwl_priv *priv = hw->priv;
Assaf Kraussbf85ea42008-03-14 10:38:49 -07006074 const struct iwl_channel_info *ch_info;
Zhu Yib481de92007-09-25 17:54:57 -07006075 unsigned long flags;
Zhu Yi76bb77e2007-11-22 10:53:22 +08006076 int ret = 0;
Zhu Yib481de92007-09-25 17:54:57 -07006077
6078 mutex_lock(&priv->mutex);
Johannes Berg8318d782008-01-24 19:38:38 +01006079 IWL_DEBUG_MAC80211("enter to channel %d\n", conf->channel->hw_value);
Zhu Yib481de92007-09-25 17:54:57 -07006080
Zhu Yi12342c42007-12-20 11:27:32 +08006081 priv->add_radiotap = !!(conf->flags & IEEE80211_CONF_RADIOTAP);
6082
Tomas Winklerfee12472008-04-03 16:05:21 -07006083 if (!iwl_is_ready(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07006084 IWL_DEBUG_MAC80211("leave - not ready\n");
Zhu Yi76bb77e2007-11-22 10:53:22 +08006085 ret = -EIO;
6086 goto out;
Zhu Yib481de92007-09-25 17:54:57 -07006087 }
6088
Assaf Krauss1ea87392008-03-18 14:57:50 -07006089 if (unlikely(!priv->cfg->mod_params->disable_hw_scan &&
Zhu Yib481de92007-09-25 17:54:57 -07006090 test_bit(STATUS_SCANNING, &priv->status))) {
Zhu Yia0646472007-12-20 14:10:01 +08006091 IWL_DEBUG_MAC80211("leave - scanning\n");
6092 set_bit(STATUS_CONF_PENDING, &priv->status);
Zhu Yib481de92007-09-25 17:54:57 -07006093 mutex_unlock(&priv->mutex);
Zhu Yia0646472007-12-20 14:10:01 +08006094 return 0;
Zhu Yib481de92007-09-25 17:54:57 -07006095 }
6096
6097 spin_lock_irqsave(&priv->lock, flags);
6098
Assaf Krauss8622e702008-03-21 13:53:43 -07006099 ch_info = iwl_get_channel_info(priv, conf->channel->band,
Johannes Berg8318d782008-01-24 19:38:38 +01006100 ieee80211_frequency_to_channel(conf->channel->center_freq));
Zhu Yib481de92007-09-25 17:54:57 -07006101 if (!is_channel_valid(ch_info)) {
Zhu Yib481de92007-09-25 17:54:57 -07006102 IWL_DEBUG_MAC80211("leave - invalid channel\n");
6103 spin_unlock_irqrestore(&priv->lock, flags);
Zhu Yi76bb77e2007-11-22 10:53:22 +08006104 ret = -EINVAL;
6105 goto out;
Zhu Yib481de92007-09-25 17:54:57 -07006106 }
6107
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08006108#ifdef CONFIG_IWL4965_HT
Tomas Winkler78330fd2008-02-06 02:37:18 +02006109 /* if we are switching from ht to 2.4 clear flags
Zhu Yib481de92007-09-25 17:54:57 -07006110 * from any ht related info since 2.4 does not
6111 * support ht */
Tomas Winkler78330fd2008-02-06 02:37:18 +02006112 if ((le16_to_cpu(priv->staging_rxon.channel) != conf->channel->hw_value)
Zhu Yib481de92007-09-25 17:54:57 -07006113#ifdef IEEE80211_CONF_CHANNEL_SWITCH
6114 && !(conf->flags & IEEE80211_CONF_CHANNEL_SWITCH)
6115#endif
6116 )
6117 priv->staging_rxon.flags = 0;
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08006118#endif /* CONFIG_IWL4965_HT */
Zhu Yib481de92007-09-25 17:54:57 -07006119
Assaf Kraussbf85ea42008-03-14 10:38:49 -07006120 iwlcore_set_rxon_channel(priv, conf->channel->band,
Johannes Berg8318d782008-01-24 19:38:38 +01006121 ieee80211_frequency_to_channel(conf->channel->center_freq));
Zhu Yib481de92007-09-25 17:54:57 -07006122
Johannes Berg8318d782008-01-24 19:38:38 +01006123 iwl4965_set_flags_for_phymode(priv, conf->channel->band);
Zhu Yib481de92007-09-25 17:54:57 -07006124
6125 /* The list of supported rates and rate mask can be different
Johannes Berg8318d782008-01-24 19:38:38 +01006126 * for each band; since the band may have changed, reset
Zhu Yib481de92007-09-25 17:54:57 -07006127 * the rate mask to what mac80211 lists */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006128 iwl4965_set_rate(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006129
6130 spin_unlock_irqrestore(&priv->lock, flags);
6131
6132#ifdef IEEE80211_CONF_CHANNEL_SWITCH
6133 if (conf->flags & IEEE80211_CONF_CHANNEL_SWITCH) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006134 iwl4965_hw_channel_switch(priv, conf->channel);
Zhu Yi76bb77e2007-11-22 10:53:22 +08006135 goto out;
Zhu Yib481de92007-09-25 17:54:57 -07006136 }
6137#endif
6138
Mohamed Abbasad97edd2008-03-28 16:21:06 -07006139 if (priv->cfg->ops->lib->radio_kill_sw)
6140 priv->cfg->ops->lib->radio_kill_sw(priv, !conf->radio_enabled);
Zhu Yib481de92007-09-25 17:54:57 -07006141
6142 if (!conf->radio_enabled) {
6143 IWL_DEBUG_MAC80211("leave - radio disabled\n");
Zhu Yi76bb77e2007-11-22 10:53:22 +08006144 goto out;
Zhu Yib481de92007-09-25 17:54:57 -07006145 }
6146
Tomas Winklerfee12472008-04-03 16:05:21 -07006147 if (iwl_is_rfkill(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07006148 IWL_DEBUG_MAC80211("leave - RF kill\n");
Zhu Yi76bb77e2007-11-22 10:53:22 +08006149 ret = -EIO;
6150 goto out;
Zhu Yib481de92007-09-25 17:54:57 -07006151 }
6152
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006153 iwl4965_set_rate(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006154
6155 if (memcmp(&priv->active_rxon,
6156 &priv->staging_rxon, sizeof(priv->staging_rxon)))
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006157 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006158 else
6159 IWL_DEBUG_INFO("No re-sending same RXON configuration.\n");
6160
6161 IWL_DEBUG_MAC80211("leave\n");
6162
Zhu Yia0646472007-12-20 14:10:01 +08006163out:
6164 clear_bit(STATUS_CONF_PENDING, &priv->status);
Zhu Yi5a66926a2008-01-14 17:46:18 -08006165 mutex_unlock(&priv->mutex);
Zhu Yi76bb77e2007-11-22 10:53:22 +08006166 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07006167}
6168
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006169static void iwl4965_config_ap(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07006170{
Tomas Winkler857485c2008-03-21 13:53:44 -07006171 int ret = 0;
Zhu Yib481de92007-09-25 17:54:57 -07006172
Maarten Lankhorstd986bcd2008-01-23 10:15:16 -08006173 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
Zhu Yib481de92007-09-25 17:54:57 -07006174 return;
6175
6176 /* The following should be done only at AP bring up */
6177 if ((priv->active_rxon.filter_flags & RXON_FILTER_ASSOC_MSK) == 0) {
6178
6179 /* RXON - unassoc (to set timing command) */
6180 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006181 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006182
6183 /* RXON Timing */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006184 memset(&priv->rxon_timing, 0, sizeof(struct iwl4965_rxon_time_cmd));
6185 iwl4965_setup_rxon_timing(priv);
Tomas Winkler857485c2008-03-21 13:53:44 -07006186 ret = iwl_send_cmd_pdu(priv, REPLY_RXON_TIMING,
Zhu Yib481de92007-09-25 17:54:57 -07006187 sizeof(priv->rxon_timing), &priv->rxon_timing);
Tomas Winkler857485c2008-03-21 13:53:44 -07006188 if (ret)
Zhu Yib481de92007-09-25 17:54:57 -07006189 IWL_WARNING("REPLY_RXON_TIMING failed - "
6190 "Attempting to continue.\n");
6191
6192 iwl4965_set_rxon_chain(priv);
6193
6194 /* FIXME: what should be the assoc_id for AP? */
6195 priv->staging_rxon.assoc_id = cpu_to_le16(priv->assoc_id);
6196 if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_PREAMBLE)
6197 priv->staging_rxon.flags |=
6198 RXON_FLG_SHORT_PREAMBLE_MSK;
6199 else
6200 priv->staging_rxon.flags &=
6201 ~RXON_FLG_SHORT_PREAMBLE_MSK;
6202
6203 if (priv->staging_rxon.flags & RXON_FLG_BAND_24G_MSK) {
6204 if (priv->assoc_capability &
6205 WLAN_CAPABILITY_SHORT_SLOT_TIME)
6206 priv->staging_rxon.flags |=
6207 RXON_FLG_SHORT_SLOT_MSK;
6208 else
6209 priv->staging_rxon.flags &=
6210 ~RXON_FLG_SHORT_SLOT_MSK;
6211
6212 if (priv->iw_mode == IEEE80211_IF_TYPE_IBSS)
6213 priv->staging_rxon.flags &=
6214 ~RXON_FLG_SHORT_SLOT_MSK;
6215 }
6216 /* restore RXON assoc */
6217 priv->staging_rxon.filter_flags |= RXON_FILTER_ASSOC_MSK;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006218 iwl4965_commit_rxon(priv);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006219 iwl4965_activate_qos(priv, 1);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006220 iwl4965_rxon_add_station(priv, iwl4965_broadcast_addr, 0);
Zhu Yie1493de2007-09-27 11:27:32 +08006221 }
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006222 iwl4965_send_beacon_cmd(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006223
6224 /* FIXME - we need to add code here to detect a totally new
6225 * configuration, reset the AP, unassoc, rxon timing, assoc,
6226 * clear sta table, add BCAST sta... */
6227}
6228
Johannes Berg32bfd352007-12-19 01:31:26 +01006229static int iwl4965_mac_config_interface(struct ieee80211_hw *hw,
6230 struct ieee80211_vif *vif,
Zhu Yib481de92007-09-25 17:54:57 -07006231 struct ieee80211_if_conf *conf)
6232{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006233 struct iwl_priv *priv = hw->priv;
Joe Perches0795af52007-10-03 17:59:30 -07006234 DECLARE_MAC_BUF(mac);
Zhu Yib481de92007-09-25 17:54:57 -07006235 unsigned long flags;
6236 int rc;
6237
6238 if (conf == NULL)
6239 return -EIO;
6240
Emmanuel Grumbachb716bb92008-03-04 18:09:32 -08006241 if (priv->vif != vif) {
6242 IWL_DEBUG_MAC80211("leave - priv->vif != vif\n");
Emmanuel Grumbachb716bb92008-03-04 18:09:32 -08006243 return 0;
6244 }
6245
Zhu Yib481de92007-09-25 17:54:57 -07006246 if ((priv->iw_mode == IEEE80211_IF_TYPE_AP) &&
6247 (!conf->beacon || !conf->ssid_len)) {
6248 IWL_DEBUG_MAC80211
6249 ("Leaving in AP mode because HostAPD is not ready.\n");
6250 return 0;
6251 }
6252
Tomas Winklerfee12472008-04-03 16:05:21 -07006253 if (!iwl_is_alive(priv))
Zhu Yi5a66926a2008-01-14 17:46:18 -08006254 return -EAGAIN;
6255
Zhu Yib481de92007-09-25 17:54:57 -07006256 mutex_lock(&priv->mutex);
6257
Zhu Yib481de92007-09-25 17:54:57 -07006258 if (conf->bssid)
Joe Perches0795af52007-10-03 17:59:30 -07006259 IWL_DEBUG_MAC80211("bssid: %s\n",
6260 print_mac(mac, conf->bssid));
Zhu Yib481de92007-09-25 17:54:57 -07006261
Johannes Berg4150c572007-09-17 01:29:23 -04006262/*
6263 * very dubious code was here; the probe filtering flag is never set:
6264 *
Zhu Yib481de92007-09-25 17:54:57 -07006265 if (unlikely(test_bit(STATUS_SCANNING, &priv->status)) &&
6266 !(priv->hw->flags & IEEE80211_HW_NO_PROBE_FILTERING)) {
Johannes Berg4150c572007-09-17 01:29:23 -04006267 */
Zhu Yib481de92007-09-25 17:54:57 -07006268
6269 if (priv->iw_mode == IEEE80211_IF_TYPE_AP) {
6270 if (!conf->bssid) {
6271 conf->bssid = priv->mac_addr;
6272 memcpy(priv->bssid, priv->mac_addr, ETH_ALEN);
Joe Perches0795af52007-10-03 17:59:30 -07006273 IWL_DEBUG_MAC80211("bssid was set to: %s\n",
6274 print_mac(mac, conf->bssid));
Zhu Yib481de92007-09-25 17:54:57 -07006275 }
6276 if (priv->ibss_beacon)
6277 dev_kfree_skb(priv->ibss_beacon);
6278
6279 priv->ibss_beacon = conf->beacon;
6280 }
6281
Tomas Winklerfee12472008-04-03 16:05:21 -07006282 if (iwl_is_rfkill(priv))
Mohamed Abbasfde35712007-11-29 11:10:15 +08006283 goto done;
6284
Zhu Yib481de92007-09-25 17:54:57 -07006285 if (conf->bssid && !is_zero_ether_addr(conf->bssid) &&
6286 !is_multicast_ether_addr(conf->bssid)) {
6287 /* If there is currently a HW scan going on in the background
6288 * then we need to cancel it else the RXON below will fail. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006289 if (iwl4965_scan_cancel_timeout(priv, 100)) {
Zhu Yib481de92007-09-25 17:54:57 -07006290 IWL_WARNING("Aborted scan still in progress "
6291 "after 100ms\n");
6292 IWL_DEBUG_MAC80211("leaving - scan abort failed.\n");
6293 mutex_unlock(&priv->mutex);
6294 return -EAGAIN;
6295 }
6296 memcpy(priv->staging_rxon.bssid_addr, conf->bssid, ETH_ALEN);
6297
6298 /* TODO: Audit driver for usage of these members and see
6299 * if mac80211 deprecates them (priv->bssid looks like it
6300 * shouldn't be there, but I haven't scanned the IBSS code
6301 * to verify) - jpk */
6302 memcpy(priv->bssid, conf->bssid, ETH_ALEN);
6303
6304 if (priv->iw_mode == IEEE80211_IF_TYPE_AP)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006305 iwl4965_config_ap(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006306 else {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006307 rc = iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006308 if ((priv->iw_mode == IEEE80211_IF_TYPE_STA) && rc)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006309 iwl4965_rxon_add_station(
Zhu Yib481de92007-09-25 17:54:57 -07006310 priv, priv->active_rxon.bssid_addr, 1);
6311 }
6312
6313 } else {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006314 iwl4965_scan_cancel_timeout(priv, 100);
Zhu Yib481de92007-09-25 17:54:57 -07006315 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006316 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006317 }
6318
Mohamed Abbasfde35712007-11-29 11:10:15 +08006319 done:
Zhu Yib481de92007-09-25 17:54:57 -07006320 spin_lock_irqsave(&priv->lock, flags);
6321 if (!conf->ssid_len)
6322 memset(priv->essid, 0, IW_ESSID_MAX_SIZE);
6323 else
6324 memcpy(priv->essid, conf->ssid, conf->ssid_len);
6325
6326 priv->essid_len = conf->ssid_len;
6327 spin_unlock_irqrestore(&priv->lock, flags);
6328
6329 IWL_DEBUG_MAC80211("leave\n");
6330 mutex_unlock(&priv->mutex);
6331
6332 return 0;
6333}
6334
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006335static void iwl4965_configure_filter(struct ieee80211_hw *hw,
Johannes Berg4150c572007-09-17 01:29:23 -04006336 unsigned int changed_flags,
6337 unsigned int *total_flags,
6338 int mc_count, struct dev_addr_list *mc_list)
6339{
6340 /*
6341 * XXX: dummy
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006342 * see also iwl4965_connection_init_rx_config
Johannes Berg4150c572007-09-17 01:29:23 -04006343 */
6344 *total_flags = 0;
6345}
6346
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006347static void iwl4965_mac_remove_interface(struct ieee80211_hw *hw,
Zhu Yib481de92007-09-25 17:54:57 -07006348 struct ieee80211_if_init_conf *conf)
6349{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006350 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07006351
6352 IWL_DEBUG_MAC80211("enter\n");
6353
6354 mutex_lock(&priv->mutex);
Mohamed Abbas948c1712007-10-25 17:15:45 +08006355
Tomas Winklerfee12472008-04-03 16:05:21 -07006356 if (iwl_is_ready_rf(priv)) {
Mohamed Abbasfde35712007-11-29 11:10:15 +08006357 iwl4965_scan_cancel_timeout(priv, 100);
6358 cancel_delayed_work(&priv->post_associate);
6359 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
6360 iwl4965_commit_rxon(priv);
6361 }
Johannes Berg32bfd352007-12-19 01:31:26 +01006362 if (priv->vif == conf->vif) {
6363 priv->vif = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07006364 memset(priv->bssid, 0, ETH_ALEN);
6365 memset(priv->essid, 0, IW_ESSID_MAX_SIZE);
6366 priv->essid_len = 0;
6367 }
6368 mutex_unlock(&priv->mutex);
6369
6370 IWL_DEBUG_MAC80211("leave\n");
6371
6372}
Johannes Berg471b3ef2007-12-28 14:32:58 +01006373
Tomas Winkler98952d52008-03-28 16:33:33 -07006374
6375#ifdef CONFIG_IWL4965_HT
6376static void iwl4965_ht_conf(struct iwl_priv *priv,
6377 struct ieee80211_bss_conf *bss_conf)
6378{
6379 struct ieee80211_ht_info *ht_conf = bss_conf->ht_conf;
6380 struct ieee80211_ht_bss_info *ht_bss_conf = bss_conf->ht_bss_conf;
6381 struct iwl_ht_info *iwl_conf = &priv->current_ht_config;
6382
6383 IWL_DEBUG_MAC80211("enter: \n");
6384
6385 iwl_conf->is_ht = bss_conf->assoc_ht;
6386
6387 if (!iwl_conf->is_ht)
6388 return;
6389
6390 priv->ps_mode = (u8)((ht_conf->cap & IEEE80211_HT_CAP_MIMO_PS) >> 2);
6391
6392 if (ht_conf->cap & IEEE80211_HT_CAP_SGI_20)
6393 iwl_conf->sgf |= 0x1;
6394 if (ht_conf->cap & IEEE80211_HT_CAP_SGI_40)
6395 iwl_conf->sgf |= 0x2;
6396
6397 iwl_conf->is_green_field = !!(ht_conf->cap & IEEE80211_HT_CAP_GRN_FLD);
6398 iwl_conf->max_amsdu_size =
6399 !!(ht_conf->cap & IEEE80211_HT_CAP_MAX_AMSDU);
6400
6401 iwl_conf->supported_chan_width =
6402 !!(ht_conf->cap & IEEE80211_HT_CAP_SUP_WIDTH);
6403 iwl_conf->extension_chan_offset =
6404 ht_bss_conf->bss_cap & IEEE80211_HT_IE_CHA_SEC_OFFSET;
6405 /* If no above or below channel supplied disable FAT channel */
6406 if (iwl_conf->extension_chan_offset != IWL_EXT_CHANNEL_OFFSET_ABOVE &&
6407 iwl_conf->extension_chan_offset != IWL_EXT_CHANNEL_OFFSET_BELOW)
6408 iwl_conf->supported_chan_width = 0;
6409
6410 iwl_conf->tx_mimo_ps_mode =
6411 (u8)((ht_conf->cap & IEEE80211_HT_CAP_MIMO_PS) >> 2);
6412 memcpy(iwl_conf->supp_mcs_set, ht_conf->supp_mcs_set, 16);
6413
6414 iwl_conf->control_channel = ht_bss_conf->primary_channel;
6415 iwl_conf->tx_chan_width =
6416 !!(ht_bss_conf->bss_cap & IEEE80211_HT_IE_CHA_WIDTH);
6417 iwl_conf->ht_protection =
6418 ht_bss_conf->bss_op_mode & IEEE80211_HT_IE_HT_PROTECTION;
6419 iwl_conf->non_GF_STA_present =
6420 !!(ht_bss_conf->bss_op_mode & IEEE80211_HT_IE_NON_GF_STA_PRSNT);
6421
6422 IWL_DEBUG_MAC80211("control channel %d\n", iwl_conf->control_channel);
6423 IWL_DEBUG_MAC80211("leave\n");
6424}
6425#else
6426static inline void iwl4965_ht_conf(struct iwl_priv *priv,
6427 struct ieee80211_bss_conf *bss_conf)
6428{
6429}
6430#endif
6431
Tomas Winkler3109ece2008-03-28 16:33:35 -07006432#define IWL_DELAY_NEXT_SCAN_AFTER_ASSOC (HZ*6)
Johannes Berg471b3ef2007-12-28 14:32:58 +01006433static void iwl4965_bss_info_changed(struct ieee80211_hw *hw,
6434 struct ieee80211_vif *vif,
6435 struct ieee80211_bss_conf *bss_conf,
6436 u32 changes)
Tomas Winkler220173b2007-10-16 00:50:25 +02006437{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006438 struct iwl_priv *priv = hw->priv;
Tomas Winkler220173b2007-10-16 00:50:25 +02006439
Tomas Winkler3109ece2008-03-28 16:33:35 -07006440 IWL_DEBUG_MAC80211("changes = 0x%X\n", changes);
6441
Johannes Berg471b3ef2007-12-28 14:32:58 +01006442 if (changes & BSS_CHANGED_ERP_PREAMBLE) {
Tomas Winkler3109ece2008-03-28 16:33:35 -07006443 IWL_DEBUG_MAC80211("ERP_PREAMBLE %d\n",
6444 bss_conf->use_short_preamble);
Johannes Berg471b3ef2007-12-28 14:32:58 +01006445 if (bss_conf->use_short_preamble)
Tomas Winkler220173b2007-10-16 00:50:25 +02006446 priv->staging_rxon.flags |= RXON_FLG_SHORT_PREAMBLE_MSK;
6447 else
6448 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_PREAMBLE_MSK;
6449 }
6450
Johannes Berg471b3ef2007-12-28 14:32:58 +01006451 if (changes & BSS_CHANGED_ERP_CTS_PROT) {
Tomas Winkler3109ece2008-03-28 16:33:35 -07006452 IWL_DEBUG_MAC80211("ERP_CTS %d\n", bss_conf->use_cts_prot);
Johannes Berg8318d782008-01-24 19:38:38 +01006453 if (bss_conf->use_cts_prot && (priv->band != IEEE80211_BAND_5GHZ))
Tomas Winkler220173b2007-10-16 00:50:25 +02006454 priv->staging_rxon.flags |= RXON_FLG_TGG_PROTECT_MSK;
6455 else
6456 priv->staging_rxon.flags &= ~RXON_FLG_TGG_PROTECT_MSK;
6457 }
6458
Tomas Winkler98952d52008-03-28 16:33:33 -07006459 if (changes & BSS_CHANGED_HT) {
Tomas Winkler3109ece2008-03-28 16:33:35 -07006460 IWL_DEBUG_MAC80211("HT %d\n", bss_conf->assoc_ht);
Tomas Winkler98952d52008-03-28 16:33:33 -07006461 iwl4965_ht_conf(priv, bss_conf);
6462 iwl4965_set_rxon_chain(priv);
6463 }
6464
Johannes Berg471b3ef2007-12-28 14:32:58 +01006465 if (changes & BSS_CHANGED_ASSOC) {
Tomas Winkler3109ece2008-03-28 16:33:35 -07006466 IWL_DEBUG_MAC80211("ASSOC %d\n", bss_conf->assoc);
Reinette Chatre508e32e2008-04-14 21:16:13 -07006467 /* This should never happen as this function should
6468 * never be called from interrupt context. */
6469 if (WARN_ON_ONCE(in_interrupt()))
6470 return;
Tomas Winkler3109ece2008-03-28 16:33:35 -07006471 if (bss_conf->assoc) {
6472 priv->assoc_id = bss_conf->aid;
6473 priv->beacon_int = bss_conf->beacon_int;
6474 priv->timestamp = bss_conf->timestamp;
6475 priv->assoc_capability = bss_conf->assoc_capability;
6476 priv->next_scan_jiffies = jiffies +
6477 IWL_DELAY_NEXT_SCAN_AFTER_ASSOC;
Reinette Chatre508e32e2008-04-14 21:16:13 -07006478 mutex_lock(&priv->mutex);
6479 iwl4965_post_associate(priv);
6480 mutex_unlock(&priv->mutex);
Tomas Winkler3109ece2008-03-28 16:33:35 -07006481 } else {
6482 priv->assoc_id = 0;
6483 IWL_DEBUG_MAC80211("DISASSOC %d\n", bss_conf->assoc);
6484 }
6485 } else if (changes && iwl_is_associated(priv) && priv->assoc_id) {
6486 IWL_DEBUG_MAC80211("Associated Changes %d\n", changes);
Tomas Winkler7e8c5192008-04-15 16:01:43 -07006487 iwl_send_rxon_assoc(priv);
Johannes Berg471b3ef2007-12-28 14:32:58 +01006488 }
6489
Tomas Winkler220173b2007-10-16 00:50:25 +02006490}
Zhu Yib481de92007-09-25 17:54:57 -07006491
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006492static int iwl4965_mac_hw_scan(struct ieee80211_hw *hw, u8 *ssid, size_t len)
Zhu Yib481de92007-09-25 17:54:57 -07006493{
6494 int rc = 0;
6495 unsigned long flags;
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006496 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07006497
6498 IWL_DEBUG_MAC80211("enter\n");
6499
mabbas052c4b92007-10-25 17:15:43 +08006500 mutex_lock(&priv->mutex);
Zhu Yib481de92007-09-25 17:54:57 -07006501 spin_lock_irqsave(&priv->lock, flags);
6502
Tomas Winklerfee12472008-04-03 16:05:21 -07006503 if (!iwl_is_ready_rf(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07006504 rc = -EIO;
6505 IWL_DEBUG_MAC80211("leave - not ready or exit pending\n");
6506 goto out_unlock;
6507 }
6508
6509 if (priv->iw_mode == IEEE80211_IF_TYPE_AP) { /* APs don't scan */
6510 rc = -EIO;
6511 IWL_ERROR("ERROR: APs don't scan\n");
6512 goto out_unlock;
6513 }
6514
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08006515 /* we don't schedule scan within next_scan_jiffies period */
6516 if (priv->next_scan_jiffies &&
6517 time_after(priv->next_scan_jiffies, jiffies)) {
6518 rc = -EAGAIN;
6519 goto out_unlock;
6520 }
Zhu Yib481de92007-09-25 17:54:57 -07006521 /* if we just finished scan ask for delay */
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08006522 if (priv->last_scan_jiffies && time_after(priv->last_scan_jiffies +
6523 IWL_DELAY_NEXT_SCAN, jiffies)) {
Zhu Yib481de92007-09-25 17:54:57 -07006524 rc = -EAGAIN;
6525 goto out_unlock;
6526 }
6527 if (len) {
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08006528 IWL_DEBUG_SCAN("direct scan for %s [%d]\n ",
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006529 iwl4965_escape_essid(ssid, len), (int)len);
Zhu Yib481de92007-09-25 17:54:57 -07006530
6531 priv->one_direct_scan = 1;
6532 priv->direct_ssid_len = (u8)
6533 min((u8) len, (u8) IW_ESSID_MAX_SIZE);
6534 memcpy(priv->direct_ssid, ssid, priv->direct_ssid_len);
Mohamed Abbas948c1712007-10-25 17:15:45 +08006535 } else
6536 priv->one_direct_scan = 0;
Zhu Yib481de92007-09-25 17:54:57 -07006537
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006538 rc = iwl4965_scan_initiate(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006539
6540 IWL_DEBUG_MAC80211("leave\n");
6541
6542out_unlock:
6543 spin_unlock_irqrestore(&priv->lock, flags);
mabbas052c4b92007-10-25 17:15:43 +08006544 mutex_unlock(&priv->mutex);
Zhu Yib481de92007-09-25 17:54:57 -07006545
6546 return rc;
6547}
6548
Emmanuel Grumbachab885f82008-03-20 15:06:43 +02006549static void iwl4965_mac_update_tkip_key(struct ieee80211_hw *hw,
6550 struct ieee80211_key_conf *keyconf, const u8 *addr,
6551 u32 iv32, u16 *phase1key)
6552{
6553 struct iwl_priv *priv = hw->priv;
6554 u8 sta_id = IWL_INVALID_STATION;
6555 unsigned long flags;
6556 __le16 key_flags = 0;
6557 int i;
6558 DECLARE_MAC_BUF(mac);
6559
6560 IWL_DEBUG_MAC80211("enter\n");
6561
Tomas Winkler947b13a2008-04-16 16:34:48 -07006562 sta_id = iwl_find_station(priv, addr);
Emmanuel Grumbachab885f82008-03-20 15:06:43 +02006563 if (sta_id == IWL_INVALID_STATION) {
6564 IWL_DEBUG_MAC80211("leave - %s not in station map.\n",
6565 print_mac(mac, addr));
6566 return;
6567 }
6568
6569 iwl4965_scan_cancel_timeout(priv, 100);
6570
6571 key_flags |= (STA_KEY_FLG_TKIP | STA_KEY_FLG_MAP_KEY_MSK);
6572 key_flags |= cpu_to_le16(keyconf->keyidx << STA_KEY_FLG_KEYID_POS);
6573 key_flags &= ~STA_KEY_FLG_INVALID;
6574
Tomas Winkler5425e492008-04-15 16:01:38 -07006575 if (sta_id == priv->hw_params.bcast_sta_id)
Emmanuel Grumbachab885f82008-03-20 15:06:43 +02006576 key_flags |= STA_KEY_MULTICAST_MSK;
6577
6578 spin_lock_irqsave(&priv->sta_lock, flags);
6579
Emmanuel Grumbachab885f82008-03-20 15:06:43 +02006580 priv->stations[sta_id].sta.key.key_flags = key_flags;
6581 priv->stations[sta_id].sta.key.tkip_rx_tsc_byte2 = (u8) iv32;
6582
6583 for (i = 0; i < 5; i++)
6584 priv->stations[sta_id].sta.key.tkip_rx_ttak[i] =
6585 cpu_to_le16(phase1key[i]);
6586
6587 priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
6588 priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
6589
6590 iwl4965_send_add_station(priv, &priv->stations[sta_id].sta, CMD_ASYNC);
6591
6592 spin_unlock_irqrestore(&priv->sta_lock, flags);
6593
6594 IWL_DEBUG_MAC80211("leave\n");
6595}
6596
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006597static int iwl4965_mac_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
Zhu Yib481de92007-09-25 17:54:57 -07006598 const u8 *local_addr, const u8 *addr,
6599 struct ieee80211_key_conf *key)
6600{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006601 struct iwl_priv *priv = hw->priv;
Joe Perches0795af52007-10-03 17:59:30 -07006602 DECLARE_MAC_BUF(mac);
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07006603 int ret = 0;
6604 u8 sta_id = IWL_INVALID_STATION;
Emmanuel Grumbach6974e362008-04-14 21:16:06 -07006605 u8 is_default_wep_key = 0;
Zhu Yib481de92007-09-25 17:54:57 -07006606
6607 IWL_DEBUG_MAC80211("enter\n");
6608
Emmanuel Grumbachfcc76c62008-04-15 16:01:47 -07006609 if (priv->cfg->mod_params->sw_crypto) {
Zhu Yib481de92007-09-25 17:54:57 -07006610 IWL_DEBUG_MAC80211("leave - hwcrypto disabled\n");
6611 return -EOPNOTSUPP;
6612 }
6613
6614 if (is_zero_ether_addr(addr))
6615 /* only support pairwise keys */
6616 return -EOPNOTSUPP;
6617
Tomas Winkler947b13a2008-04-16 16:34:48 -07006618 sta_id = iwl_find_station(priv, addr);
Emmanuel Grumbach6974e362008-04-14 21:16:06 -07006619 if (sta_id == IWL_INVALID_STATION) {
6620 IWL_DEBUG_MAC80211("leave - %s not in station map.\n",
6621 print_mac(mac, addr));
6622 return -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07006623
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07006624 }
Zhu Yib481de92007-09-25 17:54:57 -07006625
Emmanuel Grumbach6974e362008-04-14 21:16:06 -07006626 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006627 iwl4965_scan_cancel_timeout(priv, 100);
Emmanuel Grumbach6974e362008-04-14 21:16:06 -07006628 mutex_unlock(&priv->mutex);
6629
6630 /* If we are getting WEP group key and we didn't receive any key mapping
6631 * so far, we are in legacy wep mode (group key only), otherwise we are
6632 * in 1X mode.
6633 * In legacy wep mode, we use another host command to the uCode */
Tomas Winkler5425e492008-04-15 16:01:38 -07006634 if (key->alg == ALG_WEP && sta_id == priv->hw_params.bcast_sta_id &&
Emmanuel Grumbach6974e362008-04-14 21:16:06 -07006635 priv->iw_mode != IEEE80211_IF_TYPE_AP) {
6636 if (cmd == SET_KEY)
6637 is_default_wep_key = !priv->key_mapping_key;
6638 else
6639 is_default_wep_key = priv->default_wep_key;
6640 }
mabbas052c4b92007-10-25 17:15:43 +08006641
Zhu Yib481de92007-09-25 17:54:57 -07006642 switch (cmd) {
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07006643 case SET_KEY:
Emmanuel Grumbach6974e362008-04-14 21:16:06 -07006644 if (is_default_wep_key)
6645 ret = iwl_set_default_wep_key(priv, key);
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07006646 else
Emmanuel Grumbach74805132008-04-14 21:16:09 -07006647 ret = iwl_set_dynamic_key(priv, key, sta_id);
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07006648
6649 IWL_DEBUG_MAC80211("enable hwcrypto key\n");
Zhu Yib481de92007-09-25 17:54:57 -07006650 break;
6651 case DISABLE_KEY:
Emmanuel Grumbach6974e362008-04-14 21:16:06 -07006652 if (is_default_wep_key)
6653 ret = iwl_remove_default_wep_key(priv, key);
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07006654 else
Emmanuel Grumbach3ec47732008-04-17 16:03:36 -07006655 ret = iwl_remove_dynamic_key(priv, key, sta_id);
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07006656
6657 IWL_DEBUG_MAC80211("disable hwcrypto key\n");
Zhu Yib481de92007-09-25 17:54:57 -07006658 break;
6659 default:
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07006660 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07006661 }
6662
6663 IWL_DEBUG_MAC80211("leave\n");
Zhu Yib481de92007-09-25 17:54:57 -07006664
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07006665 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07006666}
6667
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006668static int iwl4965_mac_conf_tx(struct ieee80211_hw *hw, int queue,
Zhu Yib481de92007-09-25 17:54:57 -07006669 const struct ieee80211_tx_queue_params *params)
6670{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006671 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07006672 unsigned long flags;
6673 int q;
Zhu Yib481de92007-09-25 17:54:57 -07006674
6675 IWL_DEBUG_MAC80211("enter\n");
6676
Tomas Winklerfee12472008-04-03 16:05:21 -07006677 if (!iwl_is_ready_rf(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07006678 IWL_DEBUG_MAC80211("leave - RF not ready\n");
6679 return -EIO;
6680 }
6681
6682 if (queue >= AC_NUM) {
6683 IWL_DEBUG_MAC80211("leave - queue >= AC_NUM %d\n", queue);
6684 return 0;
6685 }
6686
Zhu Yib481de92007-09-25 17:54:57 -07006687 if (!priv->qos_data.qos_enable) {
6688 priv->qos_data.qos_active = 0;
6689 IWL_DEBUG_MAC80211("leave - qos not enabled\n");
6690 return 0;
6691 }
6692 q = AC_NUM - 1 - queue;
6693
6694 spin_lock_irqsave(&priv->lock, flags);
6695
6696 priv->qos_data.def_qos_parm.ac[q].cw_min = cpu_to_le16(params->cw_min);
6697 priv->qos_data.def_qos_parm.ac[q].cw_max = cpu_to_le16(params->cw_max);
6698 priv->qos_data.def_qos_parm.ac[q].aifsn = params->aifs;
6699 priv->qos_data.def_qos_parm.ac[q].edca_txop =
Johannes Berg3330d7b2008-02-10 16:49:38 +01006700 cpu_to_le16((params->txop * 32));
Zhu Yib481de92007-09-25 17:54:57 -07006701
6702 priv->qos_data.def_qos_parm.ac[q].reserved1 = 0;
6703 priv->qos_data.qos_active = 1;
6704
6705 spin_unlock_irqrestore(&priv->lock, flags);
6706
6707 mutex_lock(&priv->mutex);
6708 if (priv->iw_mode == IEEE80211_IF_TYPE_AP)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006709 iwl4965_activate_qos(priv, 1);
Tomas Winkler3109ece2008-03-28 16:33:35 -07006710 else if (priv->assoc_id && iwl_is_associated(priv))
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006711 iwl4965_activate_qos(priv, 0);
Zhu Yib481de92007-09-25 17:54:57 -07006712
6713 mutex_unlock(&priv->mutex);
6714
Zhu Yib481de92007-09-25 17:54:57 -07006715 IWL_DEBUG_MAC80211("leave\n");
6716 return 0;
6717}
6718
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006719static int iwl4965_mac_get_tx_stats(struct ieee80211_hw *hw,
Zhu Yib481de92007-09-25 17:54:57 -07006720 struct ieee80211_tx_queue_stats *stats)
6721{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006722 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07006723 int i, avail;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006724 struct iwl4965_tx_queue *txq;
6725 struct iwl4965_queue *q;
Zhu Yib481de92007-09-25 17:54:57 -07006726 unsigned long flags;
6727
6728 IWL_DEBUG_MAC80211("enter\n");
6729
Tomas Winklerfee12472008-04-03 16:05:21 -07006730 if (!iwl_is_ready_rf(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07006731 IWL_DEBUG_MAC80211("leave - RF not ready\n");
6732 return -EIO;
6733 }
6734
6735 spin_lock_irqsave(&priv->lock, flags);
6736
6737 for (i = 0; i < AC_NUM; i++) {
6738 txq = &priv->txq[i];
6739 q = &txq->q;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006740 avail = iwl4965_queue_space(q);
Zhu Yib481de92007-09-25 17:54:57 -07006741
6742 stats->data[i].len = q->n_window - avail;
6743 stats->data[i].limit = q->n_window - q->high_mark;
6744 stats->data[i].count = q->n_window;
6745
6746 }
6747 spin_unlock_irqrestore(&priv->lock, flags);
6748
6749 IWL_DEBUG_MAC80211("leave\n");
6750
6751 return 0;
6752}
6753
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006754static int iwl4965_mac_get_stats(struct ieee80211_hw *hw,
Zhu Yib481de92007-09-25 17:54:57 -07006755 struct ieee80211_low_level_stats *stats)
6756{
6757 IWL_DEBUG_MAC80211("enter\n");
6758 IWL_DEBUG_MAC80211("leave\n");
6759
6760 return 0;
6761}
6762
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006763static u64 iwl4965_mac_get_tsf(struct ieee80211_hw *hw)
Zhu Yib481de92007-09-25 17:54:57 -07006764{
6765 IWL_DEBUG_MAC80211("enter\n");
6766 IWL_DEBUG_MAC80211("leave\n");
6767
6768 return 0;
6769}
6770
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006771static void iwl4965_mac_reset_tsf(struct ieee80211_hw *hw)
Zhu Yib481de92007-09-25 17:54:57 -07006772{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006773 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07006774 unsigned long flags;
6775
6776 mutex_lock(&priv->mutex);
6777 IWL_DEBUG_MAC80211("enter\n");
6778
6779 priv->lq_mngr.lq_ready = 0;
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08006780#ifdef CONFIG_IWL4965_HT
Zhu Yib481de92007-09-25 17:54:57 -07006781 spin_lock_irqsave(&priv->lock, flags);
Ron Rindjunskyfd105e72007-11-26 16:14:39 +02006782 memset(&priv->current_ht_config, 0, sizeof(struct iwl_ht_info));
Zhu Yib481de92007-09-25 17:54:57 -07006783 spin_unlock_irqrestore(&priv->lock, flags);
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08006784#endif /* CONFIG_IWL4965_HT */
Zhu Yib481de92007-09-25 17:54:57 -07006785
Assaf Kraussbf85ea42008-03-14 10:38:49 -07006786 iwlcore_reset_qos(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006787
6788 cancel_delayed_work(&priv->post_associate);
6789
6790 spin_lock_irqsave(&priv->lock, flags);
6791 priv->assoc_id = 0;
6792 priv->assoc_capability = 0;
Zhu Yib481de92007-09-25 17:54:57 -07006793 priv->assoc_station_added = 0;
6794
6795 /* new association get rid of ibss beacon skb */
6796 if (priv->ibss_beacon)
6797 dev_kfree_skb(priv->ibss_beacon);
6798
6799 priv->ibss_beacon = NULL;
6800
6801 priv->beacon_int = priv->hw->conf.beacon_int;
Tomas Winkler3109ece2008-03-28 16:33:35 -07006802 priv->timestamp = 0;
Zhu Yib481de92007-09-25 17:54:57 -07006803 if ((priv->iw_mode == IEEE80211_IF_TYPE_STA))
6804 priv->beacon_int = 0;
6805
6806 spin_unlock_irqrestore(&priv->lock, flags);
6807
Tomas Winklerfee12472008-04-03 16:05:21 -07006808 if (!iwl_is_ready_rf(priv)) {
Mohamed Abbasfde35712007-11-29 11:10:15 +08006809 IWL_DEBUG_MAC80211("leave - not ready\n");
6810 mutex_unlock(&priv->mutex);
6811 return;
6812 }
6813
mabbas052c4b92007-10-25 17:15:43 +08006814 /* we are restarting association process
6815 * clear RXON_FILTER_ASSOC_MSK bit
6816 */
6817 if (priv->iw_mode != IEEE80211_IF_TYPE_AP) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006818 iwl4965_scan_cancel_timeout(priv, 100);
mabbas052c4b92007-10-25 17:15:43 +08006819 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006820 iwl4965_commit_rxon(priv);
mabbas052c4b92007-10-25 17:15:43 +08006821 }
6822
Mohamed Abbas5da4b552008-04-21 15:41:51 -07006823 iwl_power_update_mode(priv, 0);
6824
Zhu Yib481de92007-09-25 17:54:57 -07006825 /* Per mac80211.h: This is only used in IBSS mode... */
6826 if (priv->iw_mode != IEEE80211_IF_TYPE_IBSS) {
mabbas052c4b92007-10-25 17:15:43 +08006827
Zhu Yib481de92007-09-25 17:54:57 -07006828 IWL_DEBUG_MAC80211("leave - not in IBSS\n");
6829 mutex_unlock(&priv->mutex);
6830 return;
6831 }
6832
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006833 iwl4965_set_rate(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006834
6835 mutex_unlock(&priv->mutex);
6836
6837 IWL_DEBUG_MAC80211("leave\n");
Zhu Yib481de92007-09-25 17:54:57 -07006838}
6839
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006840static int iwl4965_mac_beacon_update(struct ieee80211_hw *hw, struct sk_buff *skb,
Zhu Yib481de92007-09-25 17:54:57 -07006841 struct ieee80211_tx_control *control)
6842{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006843 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07006844 unsigned long flags;
6845
6846 mutex_lock(&priv->mutex);
6847 IWL_DEBUG_MAC80211("enter\n");
6848
Tomas Winklerfee12472008-04-03 16:05:21 -07006849 if (!iwl_is_ready_rf(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07006850 IWL_DEBUG_MAC80211("leave - RF not ready\n");
6851 mutex_unlock(&priv->mutex);
6852 return -EIO;
6853 }
6854
6855 if (priv->iw_mode != IEEE80211_IF_TYPE_IBSS) {
6856 IWL_DEBUG_MAC80211("leave - not IBSS\n");
6857 mutex_unlock(&priv->mutex);
6858 return -EIO;
6859 }
6860
6861 spin_lock_irqsave(&priv->lock, flags);
6862
6863 if (priv->ibss_beacon)
6864 dev_kfree_skb(priv->ibss_beacon);
6865
6866 priv->ibss_beacon = skb;
6867
6868 priv->assoc_id = 0;
6869
6870 IWL_DEBUG_MAC80211("leave\n");
6871 spin_unlock_irqrestore(&priv->lock, flags);
6872
Assaf Kraussbf85ea42008-03-14 10:38:49 -07006873 iwlcore_reset_qos(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006874
6875 queue_work(priv->workqueue, &priv->post_associate.work);
6876
6877 mutex_unlock(&priv->mutex);
6878
6879 return 0;
6880}
6881
Zhu Yib481de92007-09-25 17:54:57 -07006882/*****************************************************************************
6883 *
6884 * sysfs attributes
6885 *
6886 *****************************************************************************/
6887
Tomas Winkler0a6857e2008-03-12 16:58:49 -07006888#ifdef CONFIG_IWLWIFI_DEBUG
Zhu Yib481de92007-09-25 17:54:57 -07006889
6890/*
6891 * The following adds a new attribute to the sysfs representation
6892 * of this device driver (i.e. a new file in /sys/bus/pci/drivers/iwl/)
6893 * used for controlling the debug level.
6894 *
6895 * See the level definitions in iwl for details.
6896 */
6897
6898static ssize_t show_debug_level(struct device_driver *d, char *buf)
6899{
Tomas Winkler0a6857e2008-03-12 16:58:49 -07006900 return sprintf(buf, "0x%08X\n", iwl_debug_level);
Zhu Yib481de92007-09-25 17:54:57 -07006901}
6902static ssize_t store_debug_level(struct device_driver *d,
6903 const char *buf, size_t count)
6904{
6905 char *p = (char *)buf;
6906 u32 val;
6907
6908 val = simple_strtoul(p, &p, 0);
6909 if (p == buf)
6910 printk(KERN_INFO DRV_NAME
6911 ": %s is not in hex or decimal form.\n", buf);
6912 else
Tomas Winkler0a6857e2008-03-12 16:58:49 -07006913 iwl_debug_level = val;
Zhu Yib481de92007-09-25 17:54:57 -07006914
6915 return strnlen(buf, count);
6916}
6917
6918static DRIVER_ATTR(debug_level, S_IWUSR | S_IRUGO,
6919 show_debug_level, store_debug_level);
6920
Tomas Winkler0a6857e2008-03-12 16:58:49 -07006921#endif /* CONFIG_IWLWIFI_DEBUG */
Zhu Yib481de92007-09-25 17:54:57 -07006922
Zhu Yib481de92007-09-25 17:54:57 -07006923
6924static ssize_t show_temperature(struct device *d,
6925 struct device_attribute *attr, char *buf)
6926{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006927 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07006928
Tomas Winklerfee12472008-04-03 16:05:21 -07006929 if (!iwl_is_alive(priv))
Zhu Yib481de92007-09-25 17:54:57 -07006930 return -EAGAIN;
6931
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006932 return sprintf(buf, "%d\n", iwl4965_hw_get_temperature(priv));
Zhu Yib481de92007-09-25 17:54:57 -07006933}
6934
6935static DEVICE_ATTR(temperature, S_IRUGO, show_temperature, NULL);
6936
6937static ssize_t show_rs_window(struct device *d,
6938 struct device_attribute *attr,
6939 char *buf)
6940{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006941 struct iwl_priv *priv = d->driver_data;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006942 return iwl4965_fill_rs_info(priv->hw, buf, IWL_AP_ID);
Zhu Yib481de92007-09-25 17:54:57 -07006943}
6944static DEVICE_ATTR(rs_window, S_IRUGO, show_rs_window, NULL);
6945
6946static ssize_t show_tx_power(struct device *d,
6947 struct device_attribute *attr, char *buf)
6948{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006949 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07006950 return sprintf(buf, "%d\n", priv->user_txpower_limit);
6951}
6952
6953static ssize_t store_tx_power(struct device *d,
6954 struct device_attribute *attr,
6955 const char *buf, size_t count)
6956{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006957 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07006958 char *p = (char *)buf;
6959 u32 val;
6960
6961 val = simple_strtoul(p, &p, 10);
6962 if (p == buf)
6963 printk(KERN_INFO DRV_NAME
6964 ": %s is not in decimal form.\n", buf);
6965 else
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006966 iwl4965_hw_reg_set_txpower(priv, val);
Zhu Yib481de92007-09-25 17:54:57 -07006967
6968 return count;
6969}
6970
6971static DEVICE_ATTR(tx_power, S_IWUSR | S_IRUGO, show_tx_power, store_tx_power);
6972
6973static ssize_t show_flags(struct device *d,
6974 struct device_attribute *attr, char *buf)
6975{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006976 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07006977
6978 return sprintf(buf, "0x%04X\n", priv->active_rxon.flags);
6979}
6980
6981static ssize_t store_flags(struct device *d,
6982 struct device_attribute *attr,
6983 const char *buf, size_t count)
6984{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006985 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07006986 u32 flags = simple_strtoul(buf, NULL, 0);
6987
6988 mutex_lock(&priv->mutex);
6989 if (le32_to_cpu(priv->staging_rxon.flags) != flags) {
6990 /* Cancel any currently running scans... */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006991 if (iwl4965_scan_cancel_timeout(priv, 100))
Zhu Yib481de92007-09-25 17:54:57 -07006992 IWL_WARNING("Could not cancel scan.\n");
6993 else {
6994 IWL_DEBUG_INFO("Committing rxon.flags = 0x%04X\n",
6995 flags);
6996 priv->staging_rxon.flags = cpu_to_le32(flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006997 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006998 }
6999 }
7000 mutex_unlock(&priv->mutex);
7001
7002 return count;
7003}
7004
7005static DEVICE_ATTR(flags, S_IWUSR | S_IRUGO, show_flags, store_flags);
7006
7007static ssize_t show_filter_flags(struct device *d,
7008 struct device_attribute *attr, char *buf)
7009{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007010 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07007011
7012 return sprintf(buf, "0x%04X\n",
7013 le32_to_cpu(priv->active_rxon.filter_flags));
7014}
7015
7016static ssize_t store_filter_flags(struct device *d,
7017 struct device_attribute *attr,
7018 const char *buf, size_t count)
7019{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007020 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07007021 u32 filter_flags = simple_strtoul(buf, NULL, 0);
7022
7023 mutex_lock(&priv->mutex);
7024 if (le32_to_cpu(priv->staging_rxon.filter_flags) != filter_flags) {
7025 /* Cancel any currently running scans... */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007026 if (iwl4965_scan_cancel_timeout(priv, 100))
Zhu Yib481de92007-09-25 17:54:57 -07007027 IWL_WARNING("Could not cancel scan.\n");
7028 else {
7029 IWL_DEBUG_INFO("Committing rxon.filter_flags = "
7030 "0x%04X\n", filter_flags);
7031 priv->staging_rxon.filter_flags =
7032 cpu_to_le32(filter_flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007033 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007034 }
7035 }
7036 mutex_unlock(&priv->mutex);
7037
7038 return count;
7039}
7040
7041static DEVICE_ATTR(filter_flags, S_IWUSR | S_IRUGO, show_filter_flags,
7042 store_filter_flags);
7043
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08007044#ifdef CONFIG_IWL4965_SPECTRUM_MEASUREMENT
Zhu Yib481de92007-09-25 17:54:57 -07007045
7046static ssize_t show_measurement(struct device *d,
7047 struct device_attribute *attr, char *buf)
7048{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007049 struct iwl_priv *priv = dev_get_drvdata(d);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007050 struct iwl4965_spectrum_notification measure_report;
Zhu Yib481de92007-09-25 17:54:57 -07007051 u32 size = sizeof(measure_report), len = 0, ofs = 0;
7052 u8 *data = (u8 *) & measure_report;
7053 unsigned long flags;
7054
7055 spin_lock_irqsave(&priv->lock, flags);
7056 if (!(priv->measurement_status & MEASUREMENT_READY)) {
7057 spin_unlock_irqrestore(&priv->lock, flags);
7058 return 0;
7059 }
7060 memcpy(&measure_report, &priv->measure_report, size);
7061 priv->measurement_status = 0;
7062 spin_unlock_irqrestore(&priv->lock, flags);
7063
7064 while (size && (PAGE_SIZE - len)) {
7065 hex_dump_to_buffer(data + ofs, size, 16, 1, buf + len,
7066 PAGE_SIZE - len, 1);
7067 len = strlen(buf);
7068 if (PAGE_SIZE - len)
7069 buf[len++] = '\n';
7070
7071 ofs += 16;
7072 size -= min(size, 16U);
7073 }
7074
7075 return len;
7076}
7077
7078static ssize_t store_measurement(struct device *d,
7079 struct device_attribute *attr,
7080 const char *buf, size_t count)
7081{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007082 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07007083 struct ieee80211_measurement_params params = {
7084 .channel = le16_to_cpu(priv->active_rxon.channel),
7085 .start_time = cpu_to_le64(priv->last_tsf),
7086 .duration = cpu_to_le16(1),
7087 };
7088 u8 type = IWL_MEASURE_BASIC;
7089 u8 buffer[32];
7090 u8 channel;
7091
7092 if (count) {
7093 char *p = buffer;
7094 strncpy(buffer, buf, min(sizeof(buffer), count));
7095 channel = simple_strtoul(p, NULL, 0);
7096 if (channel)
7097 params.channel = channel;
7098
7099 p = buffer;
7100 while (*p && *p != ' ')
7101 p++;
7102 if (*p)
7103 type = simple_strtoul(p + 1, NULL, 0);
7104 }
7105
7106 IWL_DEBUG_INFO("Invoking measurement of type %d on "
7107 "channel %d (for '%s')\n", type, params.channel, buf);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007108 iwl4965_get_measurement(priv, &params, type);
Zhu Yib481de92007-09-25 17:54:57 -07007109
7110 return count;
7111}
7112
7113static DEVICE_ATTR(measurement, S_IRUSR | S_IWUSR,
7114 show_measurement, store_measurement);
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08007115#endif /* CONFIG_IWL4965_SPECTRUM_MEASUREMENT */
Zhu Yib481de92007-09-25 17:54:57 -07007116
7117static ssize_t store_retry_rate(struct device *d,
7118 struct device_attribute *attr,
7119 const char *buf, size_t count)
7120{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007121 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07007122
7123 priv->retry_rate = simple_strtoul(buf, NULL, 0);
7124 if (priv->retry_rate <= 0)
7125 priv->retry_rate = 1;
7126
7127 return count;
7128}
7129
7130static ssize_t show_retry_rate(struct device *d,
7131 struct device_attribute *attr, char *buf)
7132{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007133 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07007134 return sprintf(buf, "%d", priv->retry_rate);
7135}
7136
7137static DEVICE_ATTR(retry_rate, S_IWUSR | S_IRUSR, show_retry_rate,
7138 store_retry_rate);
7139
7140static ssize_t store_power_level(struct device *d,
7141 struct device_attribute *attr,
7142 const char *buf, size_t count)
7143{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007144 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07007145 int rc;
7146 int mode;
7147
7148 mode = simple_strtoul(buf, NULL, 0);
7149 mutex_lock(&priv->mutex);
7150
Tomas Winklerfee12472008-04-03 16:05:21 -07007151 if (!iwl_is_ready(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07007152 rc = -EAGAIN;
7153 goto out;
7154 }
7155
Mohamed Abbas5da4b552008-04-21 15:41:51 -07007156 rc = iwl_power_set_user_mode(priv, mode);
7157 if (rc) {
7158 IWL_DEBUG_MAC80211("failed setting power mode.\n");
7159 goto out;
Zhu Yib481de92007-09-25 17:54:57 -07007160 }
Zhu Yib481de92007-09-25 17:54:57 -07007161 rc = count;
7162
7163 out:
7164 mutex_unlock(&priv->mutex);
7165 return rc;
7166}
7167
7168#define MAX_WX_STRING 80
7169
7170/* Values are in microsecond */
7171static const s32 timeout_duration[] = {
7172 350000,
7173 250000,
7174 75000,
7175 37000,
7176 25000,
7177};
7178static const s32 period_duration[] = {
7179 400000,
7180 700000,
7181 1000000,
7182 1000000,
7183 1000000
7184};
7185
7186static ssize_t show_power_level(struct device *d,
7187 struct device_attribute *attr, char *buf)
7188{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007189 struct iwl_priv *priv = dev_get_drvdata(d);
Mohamed Abbas5da4b552008-04-21 15:41:51 -07007190 int level = priv->power_data.power_mode;
Zhu Yib481de92007-09-25 17:54:57 -07007191 char *p = buf;
7192
7193 p += sprintf(p, "%d ", level);
7194 switch (level) {
7195 case IWL_POWER_MODE_CAM:
7196 case IWL_POWER_AC:
7197 p += sprintf(p, "(AC)");
7198 break;
7199 case IWL_POWER_BATTERY:
7200 p += sprintf(p, "(BATTERY)");
7201 break;
7202 default:
7203 p += sprintf(p,
7204 "(Timeout %dms, Period %dms)",
7205 timeout_duration[level - 1] / 1000,
7206 period_duration[level - 1] / 1000);
7207 }
Mohamed Abbas5da4b552008-04-21 15:41:51 -07007208/*
Zhu Yib481de92007-09-25 17:54:57 -07007209 if (!(priv->power_mode & IWL_POWER_ENABLED))
7210 p += sprintf(p, " OFF\n");
7211 else
7212 p += sprintf(p, " \n");
Mohamed Abbas5da4b552008-04-21 15:41:51 -07007213*/
7214 p += sprintf(p, " \n");
Zhu Yib481de92007-09-25 17:54:57 -07007215 return (p - buf + 1);
Zhu Yib481de92007-09-25 17:54:57 -07007216}
7217
7218static DEVICE_ATTR(power_level, S_IWUSR | S_IRUSR, show_power_level,
7219 store_power_level);
7220
7221static ssize_t show_channels(struct device *d,
7222 struct device_attribute *attr, char *buf)
7223{
Johannes Berg8318d782008-01-24 19:38:38 +01007224 /* all this shit doesn't belong into sysfs anyway */
7225 return 0;
Zhu Yib481de92007-09-25 17:54:57 -07007226}
7227
7228static DEVICE_ATTR(channels, S_IRUSR, show_channels, NULL);
7229
7230static ssize_t show_statistics(struct device *d,
7231 struct device_attribute *attr, char *buf)
7232{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007233 struct iwl_priv *priv = dev_get_drvdata(d);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007234 u32 size = sizeof(struct iwl4965_notif_statistics);
Zhu Yib481de92007-09-25 17:54:57 -07007235 u32 len = 0, ofs = 0;
7236 u8 *data = (u8 *) & priv->statistics;
7237 int rc = 0;
7238
Tomas Winklerfee12472008-04-03 16:05:21 -07007239 if (!iwl_is_alive(priv))
Zhu Yib481de92007-09-25 17:54:57 -07007240 return -EAGAIN;
7241
7242 mutex_lock(&priv->mutex);
Emmanuel Grumbach49ea8592008-04-15 16:01:37 -07007243 rc = iwl_send_statistics_request(priv, 0);
Zhu Yib481de92007-09-25 17:54:57 -07007244 mutex_unlock(&priv->mutex);
7245
7246 if (rc) {
7247 len = sprintf(buf,
7248 "Error sending statistics request: 0x%08X\n", rc);
7249 return len;
7250 }
7251
7252 while (size && (PAGE_SIZE - len)) {
7253 hex_dump_to_buffer(data + ofs, size, 16, 1, buf + len,
7254 PAGE_SIZE - len, 1);
7255 len = strlen(buf);
7256 if (PAGE_SIZE - len)
7257 buf[len++] = '\n';
7258
7259 ofs += 16;
7260 size -= min(size, 16U);
7261 }
7262
7263 return len;
7264}
7265
7266static DEVICE_ATTR(statistics, S_IRUGO, show_statistics, NULL);
7267
7268static ssize_t show_antenna(struct device *d,
7269 struct device_attribute *attr, char *buf)
7270{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007271 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07007272
Tomas Winklerfee12472008-04-03 16:05:21 -07007273 if (!iwl_is_alive(priv))
Zhu Yib481de92007-09-25 17:54:57 -07007274 return -EAGAIN;
7275
7276 return sprintf(buf, "%d\n", priv->antenna);
7277}
7278
7279static ssize_t store_antenna(struct device *d,
7280 struct device_attribute *attr,
7281 const char *buf, size_t count)
7282{
7283 int ant;
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007284 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07007285
7286 if (count == 0)
7287 return 0;
7288
7289 if (sscanf(buf, "%1i", &ant) != 1) {
7290 IWL_DEBUG_INFO("not in hex or decimal form.\n");
7291 return count;
7292 }
7293
7294 if ((ant >= 0) && (ant <= 2)) {
7295 IWL_DEBUG_INFO("Setting antenna select to %d.\n", ant);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007296 priv->antenna = (enum iwl4965_antenna)ant;
Zhu Yib481de92007-09-25 17:54:57 -07007297 } else
7298 IWL_DEBUG_INFO("Bad antenna select value %d.\n", ant);
7299
7300
7301 return count;
7302}
7303
7304static DEVICE_ATTR(antenna, S_IWUSR | S_IRUGO, show_antenna, store_antenna);
7305
7306static ssize_t show_status(struct device *d,
7307 struct device_attribute *attr, char *buf)
7308{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007309 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Tomas Winklerfee12472008-04-03 16:05:21 -07007310 if (!iwl_is_alive(priv))
Zhu Yib481de92007-09-25 17:54:57 -07007311 return -EAGAIN;
7312 return sprintf(buf, "0x%08x\n", (int)priv->status);
7313}
7314
7315static DEVICE_ATTR(status, S_IRUGO, show_status, NULL);
7316
7317static ssize_t dump_error_log(struct device *d,
7318 struct device_attribute *attr,
7319 const char *buf, size_t count)
7320{
7321 char *p = (char *)buf;
7322
7323 if (p[0] == '1')
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007324 iwl4965_dump_nic_error_log((struct iwl_priv *)d->driver_data);
Zhu Yib481de92007-09-25 17:54:57 -07007325
7326 return strnlen(buf, count);
7327}
7328
7329static DEVICE_ATTR(dump_errors, S_IWUSR, NULL, dump_error_log);
7330
7331static ssize_t dump_event_log(struct device *d,
7332 struct device_attribute *attr,
7333 const char *buf, size_t count)
7334{
7335 char *p = (char *)buf;
7336
7337 if (p[0] == '1')
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007338 iwl4965_dump_nic_event_log((struct iwl_priv *)d->driver_data);
Zhu Yib481de92007-09-25 17:54:57 -07007339
7340 return strnlen(buf, count);
7341}
7342
7343static DEVICE_ATTR(dump_events, S_IWUSR, NULL, dump_event_log);
7344
7345/*****************************************************************************
7346 *
7347 * driver setup and teardown
7348 *
7349 *****************************************************************************/
7350
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007351static void iwl4965_setup_deferred_work(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07007352{
7353 priv->workqueue = create_workqueue(DRV_NAME);
7354
7355 init_waitqueue_head(&priv->wait_command_queue);
7356
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007357 INIT_WORK(&priv->up, iwl4965_bg_up);
7358 INIT_WORK(&priv->restart, iwl4965_bg_restart);
7359 INIT_WORK(&priv->rx_replenish, iwl4965_bg_rx_replenish);
7360 INIT_WORK(&priv->scan_completed, iwl4965_bg_scan_completed);
7361 INIT_WORK(&priv->request_scan, iwl4965_bg_request_scan);
7362 INIT_WORK(&priv->abort_scan, iwl4965_bg_abort_scan);
7363 INIT_WORK(&priv->rf_kill, iwl4965_bg_rf_kill);
7364 INIT_WORK(&priv->beacon_update, iwl4965_bg_beacon_update);
7365 INIT_DELAYED_WORK(&priv->post_associate, iwl4965_bg_post_associate);
7366 INIT_DELAYED_WORK(&priv->init_alive_start, iwl4965_bg_init_alive_start);
7367 INIT_DELAYED_WORK(&priv->alive_start, iwl4965_bg_alive_start);
7368 INIT_DELAYED_WORK(&priv->scan_check, iwl4965_bg_scan_check);
Zhu Yib481de92007-09-25 17:54:57 -07007369
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007370 iwl4965_hw_setup_deferred_work(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007371
7372 tasklet_init(&priv->irq_tasklet, (void (*)(unsigned long))
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007373 iwl4965_irq_tasklet, (unsigned long)priv);
Zhu Yib481de92007-09-25 17:54:57 -07007374}
7375
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007376static void iwl4965_cancel_deferred_work(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07007377{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007378 iwl4965_hw_cancel_deferred_work(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007379
Joonwoo Park3ae6a052007-11-29 10:43:16 +09007380 cancel_delayed_work_sync(&priv->init_alive_start);
Zhu Yib481de92007-09-25 17:54:57 -07007381 cancel_delayed_work(&priv->scan_check);
7382 cancel_delayed_work(&priv->alive_start);
7383 cancel_delayed_work(&priv->post_associate);
7384 cancel_work_sync(&priv->beacon_update);
7385}
7386
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007387static struct attribute *iwl4965_sysfs_entries[] = {
Zhu Yib481de92007-09-25 17:54:57 -07007388 &dev_attr_antenna.attr,
7389 &dev_attr_channels.attr,
7390 &dev_attr_dump_errors.attr,
7391 &dev_attr_dump_events.attr,
7392 &dev_attr_flags.attr,
7393 &dev_attr_filter_flags.attr,
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08007394#ifdef CONFIG_IWL4965_SPECTRUM_MEASUREMENT
Zhu Yib481de92007-09-25 17:54:57 -07007395 &dev_attr_measurement.attr,
7396#endif
7397 &dev_attr_power_level.attr,
7398 &dev_attr_retry_rate.attr,
Zhu Yib481de92007-09-25 17:54:57 -07007399 &dev_attr_rs_window.attr,
7400 &dev_attr_statistics.attr,
7401 &dev_attr_status.attr,
7402 &dev_attr_temperature.attr,
Zhu Yib481de92007-09-25 17:54:57 -07007403 &dev_attr_tx_power.attr,
7404
7405 NULL
7406};
7407
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007408static struct attribute_group iwl4965_attribute_group = {
Zhu Yib481de92007-09-25 17:54:57 -07007409 .name = NULL, /* put in device directory */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007410 .attrs = iwl4965_sysfs_entries,
Zhu Yib481de92007-09-25 17:54:57 -07007411};
7412
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007413static struct ieee80211_ops iwl4965_hw_ops = {
7414 .tx = iwl4965_mac_tx,
7415 .start = iwl4965_mac_start,
7416 .stop = iwl4965_mac_stop,
7417 .add_interface = iwl4965_mac_add_interface,
7418 .remove_interface = iwl4965_mac_remove_interface,
7419 .config = iwl4965_mac_config,
7420 .config_interface = iwl4965_mac_config_interface,
7421 .configure_filter = iwl4965_configure_filter,
7422 .set_key = iwl4965_mac_set_key,
Emmanuel Grumbachab885f82008-03-20 15:06:43 +02007423 .update_tkip_key = iwl4965_mac_update_tkip_key,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007424 .get_stats = iwl4965_mac_get_stats,
7425 .get_tx_stats = iwl4965_mac_get_tx_stats,
7426 .conf_tx = iwl4965_mac_conf_tx,
7427 .get_tsf = iwl4965_mac_get_tsf,
7428 .reset_tsf = iwl4965_mac_reset_tsf,
7429 .beacon_update = iwl4965_mac_beacon_update,
Johannes Berg471b3ef2007-12-28 14:32:58 +01007430 .bss_info_changed = iwl4965_bss_info_changed,
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08007431#ifdef CONFIG_IWL4965_HT
Ron Rindjunsky9ab46172007-12-25 17:00:38 +02007432 .ampdu_action = iwl4965_mac_ampdu_action,
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08007433#endif /* CONFIG_IWL4965_HT */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007434 .hw_scan = iwl4965_mac_hw_scan
Zhu Yib481de92007-09-25 17:54:57 -07007435};
7436
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007437static int iwl4965_pci_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
Zhu Yib481de92007-09-25 17:54:57 -07007438{
7439 int err = 0;
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007440 struct iwl_priv *priv;
Zhu Yib481de92007-09-25 17:54:57 -07007441 struct ieee80211_hw *hw;
Tomas Winkler82b9a122008-03-04 18:09:30 -08007442 struct iwl_cfg *cfg = (struct iwl_cfg *)(ent->driver_data);
Mohamed Abbas0359fac2008-03-28 16:21:08 -07007443 unsigned long flags;
Zhu Yi5a66926a2008-01-14 17:46:18 -08007444 DECLARE_MAC_BUF(mac);
Zhu Yib481de92007-09-25 17:54:57 -07007445
Assaf Krauss316c30d2008-03-14 10:38:46 -07007446 /************************
7447 * 1. Allocating HW data
7448 ************************/
7449
Cahill, Ben M6440adb2007-11-29 11:09:55 +08007450 /* Disabling hardware scan means that mac80211 will perform scans
7451 * "the hard way", rather than using device's scan. */
Assaf Krauss1ea87392008-03-18 14:57:50 -07007452 if (cfg->mod_params->disable_hw_scan) {
Zhu Yib481de92007-09-25 17:54:57 -07007453 IWL_DEBUG_INFO("Disabling hw_scan\n");
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007454 iwl4965_hw_ops.hw_scan = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07007455 }
7456
Assaf Krauss1d0a0822008-03-14 10:38:48 -07007457 hw = iwl_alloc_all(cfg, &iwl4965_hw_ops);
7458 if (!hw) {
Zhu Yib481de92007-09-25 17:54:57 -07007459 err = -ENOMEM;
7460 goto out;
7461 }
Assaf Krauss1d0a0822008-03-14 10:38:48 -07007462 priv = hw->priv;
7463 /* At this point both hw and priv are allocated. */
7464
Zhu Yib481de92007-09-25 17:54:57 -07007465 SET_IEEE80211_DEV(hw, &pdev->dev);
7466
7467 IWL_DEBUG_INFO("*** LOAD DRIVER ***\n");
Tomas Winkler82b9a122008-03-04 18:09:30 -08007468 priv->cfg = cfg;
Zhu Yib481de92007-09-25 17:54:57 -07007469 priv->pci_dev = pdev;
Assaf Krauss316c30d2008-03-14 10:38:46 -07007470
Tomas Winkler0a6857e2008-03-12 16:58:49 -07007471#ifdef CONFIG_IWLWIFI_DEBUG
Assaf Krauss1ea87392008-03-18 14:57:50 -07007472 iwl_debug_level = priv->cfg->mod_params->debug;
Zhu Yib481de92007-09-25 17:54:57 -07007473 atomic_set(&priv->restrict_refcnt, 0);
7474#endif
Zhu Yib481de92007-09-25 17:54:57 -07007475
Assaf Krauss316c30d2008-03-14 10:38:46 -07007476 /**************************
7477 * 2. Initializing PCI bus
7478 **************************/
7479 if (pci_enable_device(pdev)) {
7480 err = -ENODEV;
7481 goto out_ieee80211_free_hw;
7482 }
7483
7484 pci_set_master(pdev);
7485
7486 err = pci_set_dma_mask(pdev, DMA_32BIT_MASK);
7487 if (!err)
7488 err = pci_set_consistent_dma_mask(pdev, DMA_32BIT_MASK);
7489 if (err) {
7490 printk(KERN_WARNING DRV_NAME
7491 ": No suitable DMA available.\n");
7492 goto out_pci_disable_device;
7493 }
7494
7495 err = pci_request_regions(pdev, DRV_NAME);
7496 if (err)
7497 goto out_pci_disable_device;
7498
7499 pci_set_drvdata(pdev, priv);
7500
7501 /* We disable the RETRY_TIMEOUT register (0x41) to keep
7502 * PCI Tx retries from interfering with C3 CPU state */
7503 pci_write_config_byte(pdev, 0x41, 0x00);
7504
7505 /***********************
7506 * 3. Read REV register
7507 ***********************/
7508 priv->hw_base = pci_iomap(pdev, 0, 0);
7509 if (!priv->hw_base) {
7510 err = -ENODEV;
7511 goto out_pci_release_regions;
7512 }
7513
7514 IWL_DEBUG_INFO("pci_resource_len = 0x%08llx\n",
7515 (unsigned long long) pci_resource_len(pdev, 0));
7516 IWL_DEBUG_INFO("pci_resource_base = %p\n", priv->hw_base);
7517
7518 printk(KERN_INFO DRV_NAME
7519 ": Detected Intel Wireless WiFi Link %s\n", priv->cfg->name);
7520
7521 /*****************
7522 * 4. Read EEPROM
7523 *****************/
7524 /* nic init */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07007525 iwl_set_bit(priv, CSR_GIO_CHICKEN_BITS,
Assaf Krauss316c30d2008-03-14 10:38:46 -07007526 CSR_GIO_CHICKEN_BITS_REG_BIT_DIS_L0S_EXIT_TIMER);
7527
Tomas Winkler3395f6e2008-03-25 16:33:37 -07007528 iwl_set_bit(priv, CSR_GP_CNTRL, CSR_GP_CNTRL_REG_FLAG_INIT_DONE);
7529 err = iwl_poll_bit(priv, CSR_GP_CNTRL,
Assaf Krauss316c30d2008-03-14 10:38:46 -07007530 CSR_GP_CNTRL_REG_FLAG_MAC_CLOCK_READY,
7531 CSR_GP_CNTRL_REG_FLAG_MAC_CLOCK_READY, 25000);
7532 if (err < 0) {
7533 IWL_DEBUG_INFO("Failed to init the card\n");
7534 goto out_iounmap;
7535 }
7536 /* Read the EEPROM */
7537 err = iwl_eeprom_init(priv);
7538 if (err) {
7539 IWL_ERROR("Unable to init EEPROM\n");
7540 goto out_iounmap;
7541 }
7542 /* MAC Address location in EEPROM same for 3945/4965 */
7543 iwl_eeprom_get_mac(priv, priv->mac_addr);
7544 IWL_DEBUG_INFO("MAC address: %s\n", print_mac(mac, priv->mac_addr));
7545 SET_IEEE80211_PERM_ADDR(priv->hw, priv->mac_addr);
7546
7547 /************************
7548 * 5. Setup HW constants
7549 ************************/
7550 /* Device-specific setup */
Tomas Winkler5425e492008-04-15 16:01:38 -07007551 if (priv->cfg->ops->lib->set_hw_params(priv)) {
7552 IWL_ERROR("failed to set hw parameters\n");
Tomas Winkler073d3f52008-04-21 15:41:52 -07007553 goto out_free_eeprom;
Assaf Krauss316c30d2008-03-14 10:38:46 -07007554 }
7555
7556 /*******************
7557 * 6. Setup hw/priv
7558 *******************/
Zhu Yib481de92007-09-25 17:54:57 -07007559
Assaf Kraussbf85ea42008-03-14 10:38:49 -07007560 err = iwl_setup(priv);
7561 if (err)
Tomas Winkler5425e492008-04-15 16:01:38 -07007562 goto out_unset_hw_params;
Assaf Kraussbf85ea42008-03-14 10:38:49 -07007563 /* At this point both hw and priv are initialized. */
Assaf Krauss316c30d2008-03-14 10:38:46 -07007564
7565 /**********************************
7566 * 7. Initialize module parameters
7567 **********************************/
7568
7569 /* Disable radio (SW RF KILL) via parameter when loading driver */
Assaf Krauss1ea87392008-03-18 14:57:50 -07007570 if (priv->cfg->mod_params->disable) {
Assaf Krauss316c30d2008-03-14 10:38:46 -07007571 set_bit(STATUS_RF_KILL_SW, &priv->status);
7572 IWL_DEBUG_INFO("Radio disabled.\n");
7573 }
7574
Assaf Krauss1ea87392008-03-18 14:57:50 -07007575 if (priv->cfg->mod_params->enable_qos)
Assaf Krauss316c30d2008-03-14 10:38:46 -07007576 priv->qos_data.qos_enable = 1;
7577
7578 /********************
7579 * 8. Setup services
7580 ********************/
Mohamed Abbas0359fac2008-03-28 16:21:08 -07007581 spin_lock_irqsave(&priv->lock, flags);
Assaf Krauss316c30d2008-03-14 10:38:46 -07007582 iwl4965_disable_interrupts(priv);
Mohamed Abbas0359fac2008-03-28 16:21:08 -07007583 spin_unlock_irqrestore(&priv->lock, flags);
Assaf Krauss316c30d2008-03-14 10:38:46 -07007584
7585 err = sysfs_create_group(&pdev->dev.kobj, &iwl4965_attribute_group);
7586 if (err) {
7587 IWL_ERROR("failed to create sysfs device attributes\n");
Tomas Winkler5425e492008-04-15 16:01:38 -07007588 goto out_unset_hw_params;
Assaf Krauss316c30d2008-03-14 10:38:46 -07007589 }
7590
7591 err = iwl_dbgfs_register(priv, DRV_NAME);
7592 if (err) {
7593 IWL_ERROR("failed to create debugfs files\n");
7594 goto out_remove_sysfs;
7595 }
7596
7597 iwl4965_setup_deferred_work(priv);
7598 iwl4965_setup_rx_handlers(priv);
7599
7600 /********************
7601 * 9. Conclude
7602 ********************/
Zhu Yi5a66926a2008-01-14 17:46:18 -08007603 pci_save_state(pdev);
7604 pci_disable_device(pdev);
Zhu Yib481de92007-09-25 17:54:57 -07007605
Mohamed Abbasc8381fd2008-03-28 16:21:05 -07007606 /* notify iwlcore to init */
7607 iwlcore_low_level_notify(priv, IWLCORE_INIT_EVT);
Zhu Yib481de92007-09-25 17:54:57 -07007608 return 0;
7609
Assaf Krauss316c30d2008-03-14 10:38:46 -07007610 out_remove_sysfs:
7611 sysfs_remove_group(&pdev->dev.kobj, &iwl4965_attribute_group);
Tomas Winkler5425e492008-04-15 16:01:38 -07007612 out_unset_hw_params:
7613 iwl4965_unset_hw_params(priv);
Tomas Winkler073d3f52008-04-21 15:41:52 -07007614 out_free_eeprom:
7615 iwl_eeprom_free(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007616 out_iounmap:
7617 pci_iounmap(pdev, priv->hw_base);
7618 out_pci_release_regions:
7619 pci_release_regions(pdev);
Assaf Krauss316c30d2008-03-14 10:38:46 -07007620 pci_set_drvdata(pdev, NULL);
Zhu Yib481de92007-09-25 17:54:57 -07007621 out_pci_disable_device:
7622 pci_disable_device(pdev);
Zhu Yib481de92007-09-25 17:54:57 -07007623 out_ieee80211_free_hw:
7624 ieee80211_free_hw(priv->hw);
7625 out:
7626 return err;
7627}
7628
Reinette Chatrec83dbf62008-03-21 13:53:41 -07007629static void __devexit iwl4965_pci_remove(struct pci_dev *pdev)
Zhu Yib481de92007-09-25 17:54:57 -07007630{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007631 struct iwl_priv *priv = pci_get_drvdata(pdev);
Zhu Yib481de92007-09-25 17:54:57 -07007632 struct list_head *p, *q;
7633 int i;
Mohamed Abbas0359fac2008-03-28 16:21:08 -07007634 unsigned long flags;
Zhu Yib481de92007-09-25 17:54:57 -07007635
7636 if (!priv)
7637 return;
7638
7639 IWL_DEBUG_INFO("*** UNLOAD DRIVER ***\n");
7640
Ron Rindjunskyc4f55232008-03-28 16:21:10 -07007641 if (priv->mac80211_registered) {
7642 ieee80211_unregister_hw(priv->hw);
7643 priv->mac80211_registered = 0;
7644 }
7645
Zhu Yib481de92007-09-25 17:54:57 -07007646 set_bit(STATUS_EXIT_PENDING, &priv->status);
Zhu Yib24d22b2007-12-19 13:59:52 +08007647
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007648 iwl4965_down(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007649
Mohamed Abbas0359fac2008-03-28 16:21:08 -07007650 /* make sure we flush any pending irq or
7651 * tasklet for the driver
7652 */
7653 spin_lock_irqsave(&priv->lock, flags);
7654 iwl4965_disable_interrupts(priv);
7655 spin_unlock_irqrestore(&priv->lock, flags);
7656
7657 iwl_synchronize_irq(priv);
7658
Zhu Yib481de92007-09-25 17:54:57 -07007659 /* Free MAC hash list for ADHOC */
7660 for (i = 0; i < IWL_IBSS_MAC_HASH_SIZE; i++) {
7661 list_for_each_safe(p, q, &priv->ibss_mac_hash[i]) {
7662 list_del(p);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007663 kfree(list_entry(p, struct iwl4965_ibss_seq, list));
Zhu Yib481de92007-09-25 17:54:57 -07007664 }
7665 }
7666
Mohamed Abbasc8381fd2008-03-28 16:21:05 -07007667 iwlcore_low_level_notify(priv, IWLCORE_REMOVE_EVT);
Tomas Winkler712b6cf2008-03-12 16:58:52 -07007668 iwl_dbgfs_unregister(priv);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007669 sysfs_remove_group(&pdev->dev.kobj, &iwl4965_attribute_group);
Zhu Yib481de92007-09-25 17:54:57 -07007670
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007671 iwl4965_dealloc_ucode_pci(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007672
7673 if (priv->rxq.bd)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007674 iwl4965_rx_queue_free(priv, &priv->rxq);
7675 iwl4965_hw_txq_ctx_free(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007676
Tomas Winkler5425e492008-04-15 16:01:38 -07007677 iwl4965_unset_hw_params(priv);
Assaf Kraussbf85ea42008-03-14 10:38:49 -07007678 iwlcore_clear_stations_table(priv);
Tomas Winkler073d3f52008-04-21 15:41:52 -07007679 iwl_eeprom_free(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007680
Zhu Yib481de92007-09-25 17:54:57 -07007681
Mohamed Abbas948c1712007-10-25 17:15:45 +08007682 /*netif_stop_queue(dev); */
7683 flush_workqueue(priv->workqueue);
7684
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007685 /* ieee80211_unregister_hw calls iwl4965_mac_stop, which flushes
Zhu Yib481de92007-09-25 17:54:57 -07007686 * priv->workqueue... so we can't take down the workqueue
7687 * until now... */
7688 destroy_workqueue(priv->workqueue);
7689 priv->workqueue = NULL;
7690
Zhu Yib481de92007-09-25 17:54:57 -07007691 pci_iounmap(pdev, priv->hw_base);
7692 pci_release_regions(pdev);
7693 pci_disable_device(pdev);
7694 pci_set_drvdata(pdev, NULL);
7695
Assaf Kraussbf85ea42008-03-14 10:38:49 -07007696 iwl_free_channel_map(priv);
Reinette Chatre849e0dc2008-01-23 10:15:18 -08007697 iwl4965_free_geos(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007698
7699 if (priv->ibss_beacon)
7700 dev_kfree_skb(priv->ibss_beacon);
7701
7702 ieee80211_free_hw(priv->hw);
7703}
7704
7705#ifdef CONFIG_PM
7706
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007707static int iwl4965_pci_suspend(struct pci_dev *pdev, pm_message_t state)
Zhu Yib481de92007-09-25 17:54:57 -07007708{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007709 struct iwl_priv *priv = pci_get_drvdata(pdev);
Zhu Yib481de92007-09-25 17:54:57 -07007710
Zhu Yie655b9f2008-01-24 02:19:38 -08007711 if (priv->is_open) {
7712 set_bit(STATUS_IN_SUSPEND, &priv->status);
7713 iwl4965_mac_stop(priv->hw);
7714 priv->is_open = 1;
7715 }
Zhu Yib481de92007-09-25 17:54:57 -07007716
Zhu Yib481de92007-09-25 17:54:57 -07007717 pci_set_power_state(pdev, PCI_D3hot);
7718
Zhu Yib481de92007-09-25 17:54:57 -07007719 return 0;
7720}
7721
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007722static int iwl4965_pci_resume(struct pci_dev *pdev)
Zhu Yib481de92007-09-25 17:54:57 -07007723{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007724 struct iwl_priv *priv = pci_get_drvdata(pdev);
Zhu Yib481de92007-09-25 17:54:57 -07007725
Zhu Yib481de92007-09-25 17:54:57 -07007726 pci_set_power_state(pdev, PCI_D0);
Zhu Yib481de92007-09-25 17:54:57 -07007727
Zhu Yie655b9f2008-01-24 02:19:38 -08007728 if (priv->is_open)
7729 iwl4965_mac_start(priv->hw);
Zhu Yib481de92007-09-25 17:54:57 -07007730
Zhu Yie655b9f2008-01-24 02:19:38 -08007731 clear_bit(STATUS_IN_SUSPEND, &priv->status);
Zhu Yib481de92007-09-25 17:54:57 -07007732 return 0;
7733}
7734
7735#endif /* CONFIG_PM */
7736
7737/*****************************************************************************
7738 *
7739 * driver and module entry point
7740 *
7741 *****************************************************************************/
7742
Ron Rindjunskyfed90172008-04-15 16:01:41 -07007743/* Hardware specific file defines the PCI IDs table for that hardware module */
7744static struct pci_device_id iwl_hw_card_ids[] = {
7745 {IWL_PCI_DEVICE(0x4229, PCI_ANY_ID, iwl4965_agn_cfg)},
7746 {IWL_PCI_DEVICE(0x4230, PCI_ANY_ID, iwl4965_agn_cfg)},
7747 {0}
7748};
7749MODULE_DEVICE_TABLE(pci, iwl_hw_card_ids);
7750
7751static struct pci_driver iwl_driver = {
Zhu Yib481de92007-09-25 17:54:57 -07007752 .name = DRV_NAME,
Ron Rindjunskyfed90172008-04-15 16:01:41 -07007753 .id_table = iwl_hw_card_ids,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007754 .probe = iwl4965_pci_probe,
7755 .remove = __devexit_p(iwl4965_pci_remove),
Zhu Yib481de92007-09-25 17:54:57 -07007756#ifdef CONFIG_PM
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007757 .suspend = iwl4965_pci_suspend,
7758 .resume = iwl4965_pci_resume,
Zhu Yib481de92007-09-25 17:54:57 -07007759#endif
7760};
7761
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007762static int __init iwl4965_init(void)
Zhu Yib481de92007-09-25 17:54:57 -07007763{
7764
7765 int ret;
7766 printk(KERN_INFO DRV_NAME ": " DRV_DESCRIPTION ", " DRV_VERSION "\n");
7767 printk(KERN_INFO DRV_NAME ": " DRV_COPYRIGHT "\n");
Reinette Chatre897e1cf2008-03-28 16:21:09 -07007768
7769 ret = iwl4965_rate_control_register();
7770 if (ret) {
7771 IWL_ERROR("Unable to register rate control algorithm: %d\n", ret);
7772 return ret;
7773 }
7774
Ron Rindjunskyfed90172008-04-15 16:01:41 -07007775 ret = pci_register_driver(&iwl_driver);
Zhu Yib481de92007-09-25 17:54:57 -07007776 if (ret) {
7777 IWL_ERROR("Unable to initialize PCI module\n");
Reinette Chatre897e1cf2008-03-28 16:21:09 -07007778 goto error_register;
Zhu Yib481de92007-09-25 17:54:57 -07007779 }
Tomas Winkler0a6857e2008-03-12 16:58:49 -07007780#ifdef CONFIG_IWLWIFI_DEBUG
Ron Rindjunskyfed90172008-04-15 16:01:41 -07007781 ret = driver_create_file(&iwl_driver.driver, &driver_attr_debug_level);
Zhu Yib481de92007-09-25 17:54:57 -07007782 if (ret) {
7783 IWL_ERROR("Unable to create driver sysfs file\n");
Reinette Chatre897e1cf2008-03-28 16:21:09 -07007784 goto error_debug;
Zhu Yib481de92007-09-25 17:54:57 -07007785 }
7786#endif
7787
7788 return ret;
Reinette Chatre897e1cf2008-03-28 16:21:09 -07007789
7790#ifdef CONFIG_IWLWIFI_DEBUG
7791error_debug:
Ron Rindjunskyfed90172008-04-15 16:01:41 -07007792 pci_unregister_driver(&iwl_driver);
Reinette Chatre897e1cf2008-03-28 16:21:09 -07007793#endif
7794error_register:
7795 iwl4965_rate_control_unregister();
7796 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07007797}
7798
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007799static void __exit iwl4965_exit(void)
Zhu Yib481de92007-09-25 17:54:57 -07007800{
Tomas Winkler0a6857e2008-03-12 16:58:49 -07007801#ifdef CONFIG_IWLWIFI_DEBUG
Ron Rindjunskyfed90172008-04-15 16:01:41 -07007802 driver_remove_file(&iwl_driver.driver, &driver_attr_debug_level);
Zhu Yib481de92007-09-25 17:54:57 -07007803#endif
Ron Rindjunskyfed90172008-04-15 16:01:41 -07007804 pci_unregister_driver(&iwl_driver);
Reinette Chatre897e1cf2008-03-28 16:21:09 -07007805 iwl4965_rate_control_unregister();
Zhu Yib481de92007-09-25 17:54:57 -07007806}
7807
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007808module_exit(iwl4965_exit);
7809module_init(iwl4965_init);