blob: c532584b11e61d0523cb01319ebfb5cc10b22b8a [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
Emmanuel Grumbachd1141df2008-04-21 15:42:00 -0700102static const struct ieee80211_supported_band *iwl_get_hw_mode(
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700103 struct iwl_priv *priv, enum ieee80211_band band)
Zhu Yib481de92007-09-25 17:54:57 -0700104{
Johannes Berg8318d782008-01-24 19:38:38 +0100105 return priv->hw->wiphy->bands[band];
Zhu Yib481de92007-09-25 17:54:57 -0700106}
107
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800108static int iwl4965_is_empty_essid(const char *essid, int essid_len)
Zhu Yib481de92007-09-25 17:54:57 -0700109{
110 /* Single white space is for Linksys APs */
111 if (essid_len == 1 && essid[0] == ' ')
112 return 1;
113
114 /* Otherwise, if the entire essid is 0, we assume it is hidden */
115 while (essid_len) {
116 essid_len--;
117 if (essid[essid_len] != '\0')
118 return 0;
119 }
120
121 return 1;
122}
123
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800124static const char *iwl4965_escape_essid(const char *essid, u8 essid_len)
Zhu Yib481de92007-09-25 17:54:57 -0700125{
126 static char escaped[IW_ESSID_MAX_SIZE * 2 + 1];
127 const char *s = essid;
128 char *d = escaped;
129
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800130 if (iwl4965_is_empty_essid(essid, essid_len)) {
Zhu Yib481de92007-09-25 17:54:57 -0700131 memcpy(escaped, "<hidden>", sizeof("<hidden>"));
132 return escaped;
133 }
134
135 essid_len = min(essid_len, (u8) IW_ESSID_MAX_SIZE);
136 while (essid_len--) {
137 if (*s == '\0') {
138 *d++ = '\\';
139 *d++ = '0';
140 s++;
141 } else
142 *d++ = *s++;
143 }
144 *d = '\0';
145 return escaped;
146}
147
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
Ron Rindjunsky099b40b2008-04-21 15:41:53 -0700831 iwl4965_set_rxon_hwcrypto(priv, !priv->hw_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
Zhu Yib481de92007-09-25 17:54:57 -0700944/*
945 * CARD_STATE_CMD
946 *
Ben Cahill9fbab512007-11-29 11:09:47 +0800947 * Use: Sets the device's internal card state to enable, disable, or halt
Zhu Yib481de92007-09-25 17:54:57 -0700948 *
949 * When in the 'enable' state the card operates as normal.
950 * When in the 'disable' state, the card enters into a low power mode.
951 * When in the 'halt' state, the card is shut down and must be fully
952 * restarted to come back on.
953 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700954static int iwl4965_send_card_state(struct iwl_priv *priv, u32 flags, u8 meta_flag)
Zhu Yib481de92007-09-25 17:54:57 -0700955{
Tomas Winkler857485c2008-03-21 13:53:44 -0700956 struct iwl_host_cmd cmd = {
Zhu Yib481de92007-09-25 17:54:57 -0700957 .id = REPLY_CARD_STATE_CMD,
958 .len = sizeof(u32),
959 .data = &flags,
960 .meta.flags = meta_flag,
961 };
962
Tomas Winkler857485c2008-03-21 13:53:44 -0700963 return iwl_send_cmd(priv, &cmd);
Zhu Yib481de92007-09-25 17:54:57 -0700964}
965
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700966int iwl4965_send_add_station(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800967 struct iwl4965_addsta_cmd *sta, u8 flags)
Zhu Yib481de92007-09-25 17:54:57 -0700968{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800969 struct iwl4965_rx_packet *res = NULL;
Zhu Yib481de92007-09-25 17:54:57 -0700970 int rc = 0;
Tomas Winkler857485c2008-03-21 13:53:44 -0700971 struct iwl_host_cmd cmd = {
Zhu Yib481de92007-09-25 17:54:57 -0700972 .id = REPLY_ADD_STA,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800973 .len = sizeof(struct iwl4965_addsta_cmd),
Zhu Yib481de92007-09-25 17:54:57 -0700974 .meta.flags = flags,
975 .data = sta,
976 };
977
Emmanuel Grumbach9e5b8062008-04-21 15:41:55 -0700978 if (!(flags & CMD_ASYNC))
Zhu Yib481de92007-09-25 17:54:57 -0700979 cmd.meta.flags |= CMD_WANT_SKB;
980
Tomas Winkler857485c2008-03-21 13:53:44 -0700981 rc = iwl_send_cmd(priv, &cmd);
Zhu Yib481de92007-09-25 17:54:57 -0700982
983 if (rc || (flags & CMD_ASYNC))
984 return rc;
985
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800986 res = (struct iwl4965_rx_packet *)cmd.meta.u.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 rc = -EIO;
991 }
992
993 if (rc == 0) {
994 switch (res->u.add_sta.status) {
995 case ADD_STA_SUCCESS_MSK:
996 IWL_DEBUG_INFO("REPLY_ADD_STA PASSED\n");
997 break;
998 default:
999 rc = -EIO;
1000 IWL_WARNING("REPLY_ADD_STA failed\n");
1001 break;
1002 }
1003 }
1004
1005 priv->alloc_rxb_skb--;
1006 dev_kfree_skb_any(cmd.meta.u.skb);
1007
1008 return rc;
1009}
1010
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001011static void iwl4965_clear_free_frames(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001012{
1013 struct list_head *element;
1014
1015 IWL_DEBUG_INFO("%d frames on pre-allocated heap on clear.\n",
1016 priv->frames_count);
1017
1018 while (!list_empty(&priv->free_frames)) {
1019 element = priv->free_frames.next;
1020 list_del(element);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001021 kfree(list_entry(element, struct iwl4965_frame, list));
Zhu Yib481de92007-09-25 17:54:57 -07001022 priv->frames_count--;
1023 }
1024
1025 if (priv->frames_count) {
1026 IWL_WARNING("%d frames still in use. Did we lose one?\n",
1027 priv->frames_count);
1028 priv->frames_count = 0;
1029 }
1030}
1031
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001032static struct iwl4965_frame *iwl4965_get_free_frame(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001033{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001034 struct iwl4965_frame *frame;
Zhu Yib481de92007-09-25 17:54:57 -07001035 struct list_head *element;
1036 if (list_empty(&priv->free_frames)) {
1037 frame = kzalloc(sizeof(*frame), GFP_KERNEL);
1038 if (!frame) {
1039 IWL_ERROR("Could not allocate frame!\n");
1040 return NULL;
1041 }
1042
1043 priv->frames_count++;
1044 return frame;
1045 }
1046
1047 element = priv->free_frames.next;
1048 list_del(element);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001049 return list_entry(element, struct iwl4965_frame, list);
Zhu Yib481de92007-09-25 17:54:57 -07001050}
1051
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001052static void iwl4965_free_frame(struct iwl_priv *priv, struct iwl4965_frame *frame)
Zhu Yib481de92007-09-25 17:54:57 -07001053{
1054 memset(frame, 0, sizeof(*frame));
1055 list_add(&frame->list, &priv->free_frames);
1056}
1057
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001058unsigned int iwl4965_fill_beacon_frame(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07001059 struct ieee80211_hdr *hdr,
1060 const u8 *dest, int left)
1061{
1062
Tomas Winkler3109ece2008-03-28 16:33:35 -07001063 if (!iwl_is_associated(priv) || !priv->ibss_beacon ||
Zhu Yib481de92007-09-25 17:54:57 -07001064 ((priv->iw_mode != IEEE80211_IF_TYPE_IBSS) &&
1065 (priv->iw_mode != IEEE80211_IF_TYPE_AP)))
1066 return 0;
1067
1068 if (priv->ibss_beacon->len > left)
1069 return 0;
1070
1071 memcpy(hdr, priv->ibss_beacon->data, priv->ibss_beacon->len);
1072
1073 return priv->ibss_beacon->len;
1074}
1075
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001076static u8 iwl4965_rate_get_lowest_plcp(int rate_mask)
Zhu Yib481de92007-09-25 17:54:57 -07001077{
1078 u8 i;
1079
1080 for (i = IWL_RATE_1M_INDEX; i != IWL_RATE_INVALID;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001081 i = iwl4965_rates[i].next_ieee) {
Zhu Yib481de92007-09-25 17:54:57 -07001082 if (rate_mask & (1 << i))
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001083 return iwl4965_rates[i].plcp;
Zhu Yib481de92007-09-25 17:54:57 -07001084 }
1085
1086 return IWL_RATE_INVALID;
1087}
1088
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001089static int iwl4965_send_beacon_cmd(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001090{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001091 struct iwl4965_frame *frame;
Zhu Yib481de92007-09-25 17:54:57 -07001092 unsigned int frame_size;
1093 int rc;
1094 u8 rate;
1095
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001096 frame = iwl4965_get_free_frame(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001097
1098 if (!frame) {
1099 IWL_ERROR("Could not obtain free frame buffer for beacon "
1100 "command.\n");
1101 return -ENOMEM;
1102 }
1103
1104 if (!(priv->staging_rxon.flags & RXON_FLG_BAND_24G_MSK)) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001105 rate = iwl4965_rate_get_lowest_plcp(priv->active_rate_basic &
Zhu Yib481de92007-09-25 17:54:57 -07001106 0xFF0);
1107 if (rate == IWL_INVALID_RATE)
1108 rate = IWL_RATE_6M_PLCP;
1109 } else {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001110 rate = iwl4965_rate_get_lowest_plcp(priv->active_rate_basic & 0xF);
Zhu Yib481de92007-09-25 17:54:57 -07001111 if (rate == IWL_INVALID_RATE)
1112 rate = IWL_RATE_1M_PLCP;
1113 }
1114
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001115 frame_size = iwl4965_hw_get_beacon_cmd(priv, frame, rate);
Zhu Yib481de92007-09-25 17:54:57 -07001116
Tomas Winkler857485c2008-03-21 13:53:44 -07001117 rc = iwl_send_cmd_pdu(priv, REPLY_TX_BEACON, frame_size,
Zhu Yib481de92007-09-25 17:54:57 -07001118 &frame->u.cmd[0]);
1119
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001120 iwl4965_free_frame(priv, frame);
Zhu Yib481de92007-09-25 17:54:57 -07001121
1122 return rc;
1123}
1124
1125/******************************************************************************
1126 *
Zhu Yib481de92007-09-25 17:54:57 -07001127 * Misc. internal state and helper functions
1128 *
1129 ******************************************************************************/
Zhu Yib481de92007-09-25 17:54:57 -07001130
Tomas Winkler5425e492008-04-15 16:01:38 -07001131static void iwl4965_unset_hw_params(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001132{
Tomas Winkler059ff822008-04-14 21:16:14 -07001133 if (priv->shared_virt)
Zhu Yib481de92007-09-25 17:54:57 -07001134 pci_free_consistent(priv->pci_dev,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001135 sizeof(struct iwl4965_shared),
Tomas Winkler059ff822008-04-14 21:16:14 -07001136 priv->shared_virt,
1137 priv->shared_phys);
Zhu Yib481de92007-09-25 17:54:57 -07001138}
1139
1140/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001141 * iwl4965_supported_rate_to_ie - fill in the supported rate in IE field
Zhu Yib481de92007-09-25 17:54:57 -07001142 *
1143 * return : set the bit for each supported rate insert in ie
1144 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001145static u16 iwl4965_supported_rate_to_ie(u8 *ie, u16 supported_rate,
Tomas Winklerc7c46672007-10-18 02:04:15 +02001146 u16 basic_rate, int *left)
Zhu Yib481de92007-09-25 17:54:57 -07001147{
1148 u16 ret_rates = 0, bit;
1149 int i;
Tomas Winklerc7c46672007-10-18 02:04:15 +02001150 u8 *cnt = ie;
1151 u8 *rates = ie + 1;
Zhu Yib481de92007-09-25 17:54:57 -07001152
1153 for (bit = 1, i = 0; i < IWL_RATE_COUNT; i++, bit <<= 1) {
1154 if (bit & supported_rate) {
1155 ret_rates |= bit;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001156 rates[*cnt] = iwl4965_rates[i].ieee |
Tomas Winklerc7c46672007-10-18 02:04:15 +02001157 ((bit & basic_rate) ? 0x80 : 0x00);
1158 (*cnt)++;
1159 (*left)--;
1160 if ((*left <= 0) ||
1161 (*cnt >= IWL_SUPPORTED_RATES_IE_LEN))
Zhu Yib481de92007-09-25 17:54:57 -07001162 break;
1163 }
1164 }
1165
1166 return ret_rates;
1167}
1168
Emmanuel Grumbachd1141df2008-04-21 15:42:00 -07001169#ifdef CONFIG_IWL4965_HT
1170static void iwl4965_ht_conf(struct iwl_priv *priv,
1171 struct ieee80211_bss_conf *bss_conf)
1172{
1173 struct ieee80211_ht_info *ht_conf = bss_conf->ht_conf;
1174 struct ieee80211_ht_bss_info *ht_bss_conf = bss_conf->ht_bss_conf;
1175 struct iwl_ht_info *iwl_conf = &priv->current_ht_config;
1176
1177 IWL_DEBUG_MAC80211("enter: \n");
1178
1179 iwl_conf->is_ht = bss_conf->assoc_ht;
1180
1181 if (!iwl_conf->is_ht)
1182 return;
1183
1184 priv->ps_mode = (u8)((ht_conf->cap & IEEE80211_HT_CAP_MIMO_PS) >> 2);
1185
1186 if (ht_conf->cap & IEEE80211_HT_CAP_SGI_20)
1187 iwl_conf->sgf |= 0x1;
1188 if (ht_conf->cap & IEEE80211_HT_CAP_SGI_40)
1189 iwl_conf->sgf |= 0x2;
1190
1191 iwl_conf->is_green_field = !!(ht_conf->cap & IEEE80211_HT_CAP_GRN_FLD);
1192 iwl_conf->max_amsdu_size =
1193 !!(ht_conf->cap & IEEE80211_HT_CAP_MAX_AMSDU);
1194
1195 iwl_conf->supported_chan_width =
1196 !!(ht_conf->cap & IEEE80211_HT_CAP_SUP_WIDTH);
1197 iwl_conf->extension_chan_offset =
1198 ht_bss_conf->bss_cap & IEEE80211_HT_IE_CHA_SEC_OFFSET;
1199 /* If no above or below channel supplied disable FAT channel */
1200 if (iwl_conf->extension_chan_offset != IWL_EXT_CHANNEL_OFFSET_ABOVE &&
1201 iwl_conf->extension_chan_offset != IWL_EXT_CHANNEL_OFFSET_BELOW)
1202 iwl_conf->supported_chan_width = 0;
1203
1204 iwl_conf->tx_mimo_ps_mode =
1205 (u8)((ht_conf->cap & IEEE80211_HT_CAP_MIMO_PS) >> 2);
1206 memcpy(iwl_conf->supp_mcs_set, ht_conf->supp_mcs_set, 16);
1207
1208 iwl_conf->control_channel = ht_bss_conf->primary_channel;
1209 iwl_conf->tx_chan_width =
1210 !!(ht_bss_conf->bss_cap & IEEE80211_HT_IE_CHA_WIDTH);
1211 iwl_conf->ht_protection =
1212 ht_bss_conf->bss_op_mode & IEEE80211_HT_IE_HT_PROTECTION;
1213 iwl_conf->non_GF_STA_present =
1214 !!(ht_bss_conf->bss_op_mode & IEEE80211_HT_IE_NON_GF_STA_PRSNT);
1215
1216 IWL_DEBUG_MAC80211("control channel %d\n", iwl_conf->control_channel);
1217 IWL_DEBUG_MAC80211("leave\n");
1218}
1219
1220static void iwl_ht_cap_to_ie(const struct ieee80211_supported_band *sband,
1221 u8 *pos, int *left)
1222{
1223 struct ieee80211_ht_cap *ht_cap;
1224
1225 if (!sband || !sband->ht_info.ht_supported)
1226 return;
1227
1228 if (*left < sizeof(struct ieee80211_ht_cap))
1229 return;
1230
1231 *pos++ = sizeof(struct ieee80211_ht_cap);
1232 ht_cap = (struct ieee80211_ht_cap *) pos;
1233
1234 ht_cap->cap_info = cpu_to_le16(sband->ht_info.cap);
1235 memcpy(ht_cap->supp_mcs_set, sband->ht_info.supp_mcs_set, 16);
1236 ht_cap->ampdu_params_info =
1237 (sband->ht_info.ampdu_factor & IEEE80211_HT_CAP_AMPDU_FACTOR) |
1238 ((sband->ht_info.ampdu_density << 2) &
1239 IEEE80211_HT_CAP_AMPDU_DENSITY);
1240 *left -= sizeof(struct ieee80211_ht_cap);
1241}
1242#else
1243static inline void iwl4965_ht_conf(struct iwl_priv *priv,
1244 struct ieee80211_bss_conf *bss_conf)
1245{
1246}
1247static void iwl_ht_cap_to_ie(const struct ieee80211_supported_band *sband,
1248 u8 *pos, int *left)
1249{
1250}
1251#endif
1252
1253
Zhu Yib481de92007-09-25 17:54:57 -07001254/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001255 * iwl4965_fill_probe_req - fill in all required fields and IE for probe request
Zhu Yib481de92007-09-25 17:54:57 -07001256 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001257static u16 iwl4965_fill_probe_req(struct iwl_priv *priv,
Tomas Winkler78330fd2008-02-06 02:37:18 +02001258 enum ieee80211_band band,
1259 struct ieee80211_mgmt *frame,
1260 int left, int is_direct)
Zhu Yib481de92007-09-25 17:54:57 -07001261{
1262 int len = 0;
1263 u8 *pos = NULL;
mabbasbee488d2007-10-25 17:15:42 +08001264 u16 active_rates, ret_rates, cck_rates, active_rate_basic;
Tomas Winkler78330fd2008-02-06 02:37:18 +02001265 const struct ieee80211_supported_band *sband =
Emmanuel Grumbachd1141df2008-04-21 15:42:00 -07001266 iwl_get_hw_mode(priv, band);
Zhu Yib481de92007-09-25 17:54:57 -07001267
1268 /* Make sure there is enough space for the probe request,
1269 * two mandatory IEs and the data */
1270 left -= 24;
1271 if (left < 0)
1272 return 0;
1273 len += 24;
1274
1275 frame->frame_control = cpu_to_le16(IEEE80211_STYPE_PROBE_REQ);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001276 memcpy(frame->da, iwl4965_broadcast_addr, ETH_ALEN);
Zhu Yib481de92007-09-25 17:54:57 -07001277 memcpy(frame->sa, priv->mac_addr, ETH_ALEN);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001278 memcpy(frame->bssid, iwl4965_broadcast_addr, ETH_ALEN);
Zhu Yib481de92007-09-25 17:54:57 -07001279 frame->seq_ctrl = 0;
1280
1281 /* fill in our indirect SSID IE */
1282 /* ...next IE... */
1283
1284 left -= 2;
1285 if (left < 0)
1286 return 0;
1287 len += 2;
1288 pos = &(frame->u.probe_req.variable[0]);
1289 *pos++ = WLAN_EID_SSID;
1290 *pos++ = 0;
1291
1292 /* fill in our direct SSID IE... */
1293 if (is_direct) {
1294 /* ...next IE... */
1295 left -= 2 + priv->essid_len;
1296 if (left < 0)
1297 return 0;
1298 /* ... fill it in... */
1299 *pos++ = WLAN_EID_SSID;
1300 *pos++ = priv->essid_len;
1301 memcpy(pos, priv->essid, priv->essid_len);
1302 pos += priv->essid_len;
1303 len += 2 + priv->essid_len;
1304 }
1305
1306 /* fill in supported rate */
1307 /* ...next IE... */
1308 left -= 2;
1309 if (left < 0)
1310 return 0;
Tomas Winklerc7c46672007-10-18 02:04:15 +02001311
Zhu Yib481de92007-09-25 17:54:57 -07001312 /* ... fill it in... */
1313 *pos++ = WLAN_EID_SUPP_RATES;
1314 *pos = 0;
Tomas Winklerc7c46672007-10-18 02:04:15 +02001315
mabbasbee488d2007-10-25 17:15:42 +08001316 /* exclude 60M rate */
1317 active_rates = priv->rates_mask;
1318 active_rates &= ~IWL_RATE_60M_MASK;
1319
1320 active_rate_basic = active_rates & IWL_BASIC_RATES_MASK;
Zhu Yib481de92007-09-25 17:54:57 -07001321
Tomas Winklerc7c46672007-10-18 02:04:15 +02001322 cck_rates = IWL_CCK_RATES_MASK & active_rates;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001323 ret_rates = iwl4965_supported_rate_to_ie(pos, cck_rates,
mabbasbee488d2007-10-25 17:15:42 +08001324 active_rate_basic, &left);
Tomas Winklerc7c46672007-10-18 02:04:15 +02001325 active_rates &= ~ret_rates;
1326
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001327 ret_rates = iwl4965_supported_rate_to_ie(pos, active_rates,
mabbasbee488d2007-10-25 17:15:42 +08001328 active_rate_basic, &left);
Tomas Winklerc7c46672007-10-18 02:04:15 +02001329 active_rates &= ~ret_rates;
1330
Zhu Yib481de92007-09-25 17:54:57 -07001331 len += 2 + *pos;
1332 pos += (*pos) + 1;
Tomas Winklerc7c46672007-10-18 02:04:15 +02001333 if (active_rates == 0)
Zhu Yib481de92007-09-25 17:54:57 -07001334 goto fill_end;
1335
1336 /* fill in supported extended rate */
1337 /* ...next IE... */
1338 left -= 2;
1339 if (left < 0)
1340 return 0;
1341 /* ... fill it in... */
1342 *pos++ = WLAN_EID_EXT_SUPP_RATES;
1343 *pos = 0;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001344 iwl4965_supported_rate_to_ie(pos, active_rates,
mabbasbee488d2007-10-25 17:15:42 +08001345 active_rate_basic, &left);
Zhu Yib481de92007-09-25 17:54:57 -07001346 if (*pos > 0)
1347 len += 2 + *pos;
1348
Zhu Yib481de92007-09-25 17:54:57 -07001349 fill_end:
Emmanuel Grumbachd1141df2008-04-21 15:42:00 -07001350 /* fill in HT IE */
1351 left -= 2;
1352 if (left < 0)
1353 return 0;
1354
1355 *pos++ = WLAN_EID_HT_CAPABILITY;
1356 *pos = 0;
1357
1358 iwl_ht_cap_to_ie(sband, pos, &left);
1359
1360 if (*pos > 0)
1361 len += 2 + *pos;
Zhu Yib481de92007-09-25 17:54:57 -07001362 return (u16)len;
1363}
1364
1365/*
1366 * QoS support
1367*/
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001368static int iwl4965_send_qos_params_command(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001369 struct iwl4965_qosparam_cmd *qos)
Zhu Yib481de92007-09-25 17:54:57 -07001370{
1371
Tomas Winkler857485c2008-03-21 13:53:44 -07001372 return iwl_send_cmd_pdu(priv, REPLY_QOS_PARAM,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001373 sizeof(struct iwl4965_qosparam_cmd), qos);
Zhu Yib481de92007-09-25 17:54:57 -07001374}
1375
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001376static void iwl4965_activate_qos(struct iwl_priv *priv, u8 force)
Zhu Yib481de92007-09-25 17:54:57 -07001377{
1378 unsigned long flags;
1379
Zhu Yib481de92007-09-25 17:54:57 -07001380 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
1381 return;
1382
1383 if (!priv->qos_data.qos_enable)
1384 return;
1385
1386 spin_lock_irqsave(&priv->lock, flags);
1387 priv->qos_data.def_qos_parm.qos_flags = 0;
1388
1389 if (priv->qos_data.qos_cap.q_AP.queue_request &&
1390 !priv->qos_data.qos_cap.q_AP.txop_request)
1391 priv->qos_data.def_qos_parm.qos_flags |=
1392 QOS_PARAM_FLG_TXOP_TYPE_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07001393 if (priv->qos_data.qos_active)
1394 priv->qos_data.def_qos_parm.qos_flags |=
1395 QOS_PARAM_FLG_UPDATE_EDCA_MSK;
1396
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08001397#ifdef CONFIG_IWL4965_HT
Ron Rindjunskyfd105e72007-11-26 16:14:39 +02001398 if (priv->current_ht_config.is_ht)
Tomas Winklerf1f1f5c2007-10-25 17:15:26 +08001399 priv->qos_data.def_qos_parm.qos_flags |= QOS_PARAM_FLG_TGN_MSK;
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08001400#endif /* CONFIG_IWL4965_HT */
Tomas Winklerf1f1f5c2007-10-25 17:15:26 +08001401
Zhu Yib481de92007-09-25 17:54:57 -07001402 spin_unlock_irqrestore(&priv->lock, flags);
1403
Tomas Winkler3109ece2008-03-28 16:33:35 -07001404 if (force || iwl_is_associated(priv)) {
Tomas Winklerf1f1f5c2007-10-25 17:15:26 +08001405 IWL_DEBUG_QOS("send QoS cmd with Qos active=%d FLAGS=0x%X\n",
1406 priv->qos_data.qos_active,
1407 priv->qos_data.def_qos_parm.qos_flags);
Zhu Yib481de92007-09-25 17:54:57 -07001408
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001409 iwl4965_send_qos_params_command(priv,
Zhu Yib481de92007-09-25 17:54:57 -07001410 &(priv->qos_data.def_qos_parm));
1411 }
1412}
1413
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001414int iwl4965_is_network_packet(struct iwl_priv *priv, struct ieee80211_hdr *header)
Zhu Yib481de92007-09-25 17:54:57 -07001415{
1416 /* Filter incoming packets to determine if they are targeted toward
1417 * this network, discarding packets coming from ourselves */
1418 switch (priv->iw_mode) {
1419 case IEEE80211_IF_TYPE_IBSS: /* Header: Dest. | Source | BSSID */
1420 /* packets from our adapter are dropped (echo) */
1421 if (!compare_ether_addr(header->addr2, priv->mac_addr))
1422 return 0;
1423 /* {broad,multi}cast packets to our IBSS go through */
1424 if (is_multicast_ether_addr(header->addr1))
1425 return !compare_ether_addr(header->addr3, priv->bssid);
1426 /* packets to our adapter go through */
1427 return !compare_ether_addr(header->addr1, priv->mac_addr);
1428 case IEEE80211_IF_TYPE_STA: /* Header: Dest. | AP{BSSID} | Source */
1429 /* packets from our adapter are dropped (echo) */
1430 if (!compare_ether_addr(header->addr3, priv->mac_addr))
1431 return 0;
1432 /* {broad,multi}cast packets to our BSS go through */
1433 if (is_multicast_ether_addr(header->addr1))
1434 return !compare_ether_addr(header->addr2, priv->bssid);
1435 /* packets to our adapter go through */
1436 return !compare_ether_addr(header->addr1, priv->mac_addr);
Tomas Winkler69dc5d92008-03-25 16:33:41 -07001437 default:
1438 break;
Zhu Yib481de92007-09-25 17:54:57 -07001439 }
1440
1441 return 1;
1442}
1443
1444#define TX_STATUS_ENTRY(x) case TX_STATUS_FAIL_ ## x: return #x
1445
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001446static const char *iwl4965_get_tx_fail_reason(u32 status)
Zhu Yib481de92007-09-25 17:54:57 -07001447{
1448 switch (status & TX_STATUS_MSK) {
1449 case TX_STATUS_SUCCESS:
1450 return "SUCCESS";
1451 TX_STATUS_ENTRY(SHORT_LIMIT);
1452 TX_STATUS_ENTRY(LONG_LIMIT);
1453 TX_STATUS_ENTRY(FIFO_UNDERRUN);
1454 TX_STATUS_ENTRY(MGMNT_ABORT);
1455 TX_STATUS_ENTRY(NEXT_FRAG);
1456 TX_STATUS_ENTRY(LIFE_EXPIRE);
1457 TX_STATUS_ENTRY(DEST_PS);
1458 TX_STATUS_ENTRY(ABORTED);
1459 TX_STATUS_ENTRY(BT_RETRY);
1460 TX_STATUS_ENTRY(STA_INVALID);
1461 TX_STATUS_ENTRY(FRAG_DROPPED);
1462 TX_STATUS_ENTRY(TID_DISABLE);
1463 TX_STATUS_ENTRY(FRAME_FLUSHED);
1464 TX_STATUS_ENTRY(INSUFFICIENT_CF_POLL);
1465 TX_STATUS_ENTRY(TX_LOCKED);
1466 TX_STATUS_ENTRY(NO_BEACON_ON_RADAR);
1467 }
1468
1469 return "UNKNOWN";
1470}
1471
1472/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001473 * iwl4965_scan_cancel - Cancel any currently executing HW scan
Zhu Yib481de92007-09-25 17:54:57 -07001474 *
1475 * NOTE: priv->mutex is not required before calling this function
1476 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001477static int iwl4965_scan_cancel(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001478{
1479 if (!test_bit(STATUS_SCAN_HW, &priv->status)) {
1480 clear_bit(STATUS_SCANNING, &priv->status);
1481 return 0;
1482 }
1483
1484 if (test_bit(STATUS_SCANNING, &priv->status)) {
1485 if (!test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
1486 IWL_DEBUG_SCAN("Queuing scan abort.\n");
1487 set_bit(STATUS_SCAN_ABORTING, &priv->status);
1488 queue_work(priv->workqueue, &priv->abort_scan);
1489
1490 } else
1491 IWL_DEBUG_SCAN("Scan abort already in progress.\n");
1492
1493 return test_bit(STATUS_SCANNING, &priv->status);
1494 }
1495
1496 return 0;
1497}
1498
1499/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001500 * iwl4965_scan_cancel_timeout - Cancel any currently executing HW scan
Zhu Yib481de92007-09-25 17:54:57 -07001501 * @ms: amount of time to wait (in milliseconds) for scan to abort
1502 *
1503 * NOTE: priv->mutex must be held before calling this function
1504 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001505static int iwl4965_scan_cancel_timeout(struct iwl_priv *priv, unsigned long ms)
Zhu Yib481de92007-09-25 17:54:57 -07001506{
1507 unsigned long now = jiffies;
1508 int ret;
1509
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001510 ret = iwl4965_scan_cancel(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001511 if (ret && ms) {
1512 mutex_unlock(&priv->mutex);
1513 while (!time_after(jiffies, now + msecs_to_jiffies(ms)) &&
1514 test_bit(STATUS_SCANNING, &priv->status))
1515 msleep(1);
1516 mutex_lock(&priv->mutex);
1517
1518 return test_bit(STATUS_SCANNING, &priv->status);
1519 }
1520
1521 return ret;
1522}
1523
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001524static void iwl4965_sequence_reset(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001525{
1526 /* Reset ieee stats */
1527
1528 /* We don't reset the net_device_stats (ieee->stats) on
1529 * re-association */
1530
1531 priv->last_seq_num = -1;
1532 priv->last_frag_num = -1;
1533 priv->last_packet_time = 0;
1534
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001535 iwl4965_scan_cancel(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001536}
1537
1538#define MAX_UCODE_BEACON_INTERVAL 4096
1539#define INTEL_CONN_LISTEN_INTERVAL __constant_cpu_to_le16(0xA)
1540
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001541static __le16 iwl4965_adjust_beacon_interval(u16 beacon_val)
Zhu Yib481de92007-09-25 17:54:57 -07001542{
1543 u16 new_val = 0;
1544 u16 beacon_factor = 0;
1545
1546 beacon_factor =
1547 (beacon_val + MAX_UCODE_BEACON_INTERVAL)
1548 / MAX_UCODE_BEACON_INTERVAL;
1549 new_val = beacon_val / beacon_factor;
1550
1551 return cpu_to_le16(new_val);
1552}
1553
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001554static void iwl4965_setup_rxon_timing(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001555{
1556 u64 interval_tm_unit;
1557 u64 tsf, result;
1558 unsigned long flags;
1559 struct ieee80211_conf *conf = NULL;
1560 u16 beacon_int = 0;
1561
1562 conf = ieee80211_get_hw_conf(priv->hw);
1563
1564 spin_lock_irqsave(&priv->lock, flags);
Tomas Winkler3109ece2008-03-28 16:33:35 -07001565 priv->rxon_timing.timestamp.dw[1] = cpu_to_le32(priv->timestamp >> 32);
1566 priv->rxon_timing.timestamp.dw[0] =
1567 cpu_to_le32(priv->timestamp & 0xFFFFFFFF);
Zhu Yib481de92007-09-25 17:54:57 -07001568
1569 priv->rxon_timing.listen_interval = INTEL_CONN_LISTEN_INTERVAL;
1570
Tomas Winkler3109ece2008-03-28 16:33:35 -07001571 tsf = priv->timestamp;
Zhu Yib481de92007-09-25 17:54:57 -07001572
1573 beacon_int = priv->beacon_int;
1574 spin_unlock_irqrestore(&priv->lock, flags);
1575
1576 if (priv->iw_mode == IEEE80211_IF_TYPE_STA) {
1577 if (beacon_int == 0) {
1578 priv->rxon_timing.beacon_interval = cpu_to_le16(100);
1579 priv->rxon_timing.beacon_init_val = cpu_to_le32(102400);
1580 } else {
1581 priv->rxon_timing.beacon_interval =
1582 cpu_to_le16(beacon_int);
1583 priv->rxon_timing.beacon_interval =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001584 iwl4965_adjust_beacon_interval(
Zhu Yib481de92007-09-25 17:54:57 -07001585 le16_to_cpu(priv->rxon_timing.beacon_interval));
1586 }
1587
1588 priv->rxon_timing.atim_window = 0;
1589 } else {
1590 priv->rxon_timing.beacon_interval =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001591 iwl4965_adjust_beacon_interval(conf->beacon_int);
Zhu Yib481de92007-09-25 17:54:57 -07001592 /* TODO: we need to get atim_window from upper stack
1593 * for now we set to 0 */
1594 priv->rxon_timing.atim_window = 0;
1595 }
1596
1597 interval_tm_unit =
1598 (le16_to_cpu(priv->rxon_timing.beacon_interval) * 1024);
1599 result = do_div(tsf, interval_tm_unit);
1600 priv->rxon_timing.beacon_init_val =
1601 cpu_to_le32((u32) ((u64) interval_tm_unit - result));
1602
1603 IWL_DEBUG_ASSOC
1604 ("beacon interval %d beacon timer %d beacon tim %d\n",
1605 le16_to_cpu(priv->rxon_timing.beacon_interval),
1606 le32_to_cpu(priv->rxon_timing.beacon_init_val),
1607 le16_to_cpu(priv->rxon_timing.atim_window));
1608}
1609
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001610static int iwl4965_scan_initiate(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001611{
1612 if (priv->iw_mode == IEEE80211_IF_TYPE_AP) {
1613 IWL_ERROR("APs don't scan.\n");
1614 return 0;
1615 }
1616
Tomas Winklerfee12472008-04-03 16:05:21 -07001617 if (!iwl_is_ready_rf(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07001618 IWL_DEBUG_SCAN("Aborting scan due to not ready.\n");
1619 return -EIO;
1620 }
1621
1622 if (test_bit(STATUS_SCANNING, &priv->status)) {
1623 IWL_DEBUG_SCAN("Scan already in progress.\n");
1624 return -EAGAIN;
1625 }
1626
1627 if (test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
1628 IWL_DEBUG_SCAN("Scan request while abort pending. "
1629 "Queuing.\n");
1630 return -EAGAIN;
1631 }
1632
1633 IWL_DEBUG_INFO("Starting scan...\n");
1634 priv->scan_bands = 2;
1635 set_bit(STATUS_SCANNING, &priv->status);
1636 priv->scan_start = jiffies;
1637 priv->scan_pass_start = priv->scan_start;
1638
1639 queue_work(priv->workqueue, &priv->request_scan);
1640
1641 return 0;
1642}
1643
Zhu Yib481de92007-09-25 17:54:57 -07001644
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001645static void iwl4965_set_flags_for_phymode(struct iwl_priv *priv,
Johannes Berg8318d782008-01-24 19:38:38 +01001646 enum ieee80211_band band)
Zhu Yib481de92007-09-25 17:54:57 -07001647{
Johannes Berg8318d782008-01-24 19:38:38 +01001648 if (band == IEEE80211_BAND_5GHZ) {
Zhu Yib481de92007-09-25 17:54:57 -07001649 priv->staging_rxon.flags &=
1650 ~(RXON_FLG_BAND_24G_MSK | RXON_FLG_AUTO_DETECT_MSK
1651 | RXON_FLG_CCK_MSK);
1652 priv->staging_rxon.flags |= RXON_FLG_SHORT_SLOT_MSK;
1653 } else {
Reinette Chatre508e32e2008-04-14 21:16:13 -07001654 /* Copied from iwl4965_post_associate() */
Zhu Yib481de92007-09-25 17:54:57 -07001655 if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_SLOT_TIME)
1656 priv->staging_rxon.flags |= RXON_FLG_SHORT_SLOT_MSK;
1657 else
1658 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
1659
1660 if (priv->iw_mode == IEEE80211_IF_TYPE_IBSS)
1661 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
1662
1663 priv->staging_rxon.flags |= RXON_FLG_BAND_24G_MSK;
1664 priv->staging_rxon.flags |= RXON_FLG_AUTO_DETECT_MSK;
1665 priv->staging_rxon.flags &= ~RXON_FLG_CCK_MSK;
1666 }
1667}
1668
1669/*
Ian Schram01ebd062007-10-25 17:15:22 +08001670 * initialize rxon structure with default values from eeprom
Zhu Yib481de92007-09-25 17:54:57 -07001671 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001672static void iwl4965_connection_init_rx_config(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001673{
Assaf Kraussbf85ea42008-03-14 10:38:49 -07001674 const struct iwl_channel_info *ch_info;
Zhu Yib481de92007-09-25 17:54:57 -07001675
1676 memset(&priv->staging_rxon, 0, sizeof(priv->staging_rxon));
1677
1678 switch (priv->iw_mode) {
1679 case IEEE80211_IF_TYPE_AP:
1680 priv->staging_rxon.dev_type = RXON_DEV_TYPE_AP;
1681 break;
1682
1683 case IEEE80211_IF_TYPE_STA:
1684 priv->staging_rxon.dev_type = RXON_DEV_TYPE_ESS;
1685 priv->staging_rxon.filter_flags = RXON_FILTER_ACCEPT_GRP_MSK;
1686 break;
1687
1688 case IEEE80211_IF_TYPE_IBSS:
1689 priv->staging_rxon.dev_type = RXON_DEV_TYPE_IBSS;
1690 priv->staging_rxon.flags = RXON_FLG_SHORT_PREAMBLE_MSK;
1691 priv->staging_rxon.filter_flags = RXON_FILTER_BCON_AWARE_MSK |
1692 RXON_FILTER_ACCEPT_GRP_MSK;
1693 break;
1694
1695 case IEEE80211_IF_TYPE_MNTR:
1696 priv->staging_rxon.dev_type = RXON_DEV_TYPE_SNIFFER;
1697 priv->staging_rxon.filter_flags = RXON_FILTER_PROMISC_MSK |
1698 RXON_FILTER_CTL2HOST_MSK | RXON_FILTER_ACCEPT_GRP_MSK;
1699 break;
Tomas Winkler69dc5d92008-03-25 16:33:41 -07001700 default:
1701 IWL_ERROR("Unsupported interface type %d\n", priv->iw_mode);
1702 break;
Zhu Yib481de92007-09-25 17:54:57 -07001703 }
1704
1705#if 0
1706 /* TODO: Figure out when short_preamble would be set and cache from
1707 * that */
1708 if (!hw_to_local(priv->hw)->short_preamble)
1709 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_PREAMBLE_MSK;
1710 else
1711 priv->staging_rxon.flags |= RXON_FLG_SHORT_PREAMBLE_MSK;
1712#endif
1713
Assaf Krauss8622e702008-03-21 13:53:43 -07001714 ch_info = iwl_get_channel_info(priv, priv->band,
Zhu Yib481de92007-09-25 17:54:57 -07001715 le16_to_cpu(priv->staging_rxon.channel));
1716
1717 if (!ch_info)
1718 ch_info = &priv->channel_info[0];
1719
1720 /*
1721 * in some case A channels are all non IBSS
1722 * in this case force B/G channel
1723 */
1724 if ((priv->iw_mode == IEEE80211_IF_TYPE_IBSS) &&
1725 !(is_channel_ibss(ch_info)))
1726 ch_info = &priv->channel_info[0];
1727
1728 priv->staging_rxon.channel = cpu_to_le16(ch_info->channel);
Johannes Berg8318d782008-01-24 19:38:38 +01001729 priv->band = ch_info->band;
Zhu Yib481de92007-09-25 17:54:57 -07001730
Johannes Berg8318d782008-01-24 19:38:38 +01001731 iwl4965_set_flags_for_phymode(priv, priv->band);
Zhu Yib481de92007-09-25 17:54:57 -07001732
1733 priv->staging_rxon.ofdm_basic_rates =
1734 (IWL_OFDM_RATES_MASK >> IWL_FIRST_OFDM_RATE) & 0xFF;
1735 priv->staging_rxon.cck_basic_rates =
1736 (IWL_CCK_RATES_MASK >> IWL_FIRST_CCK_RATE) & 0xF;
1737
1738 priv->staging_rxon.flags &= ~(RXON_FLG_CHANNEL_MODE_MIXED_MSK |
1739 RXON_FLG_CHANNEL_MODE_PURE_40_MSK);
1740 memcpy(priv->staging_rxon.node_addr, priv->mac_addr, ETH_ALEN);
1741 memcpy(priv->staging_rxon.wlap_bssid_addr, priv->mac_addr, ETH_ALEN);
1742 priv->staging_rxon.ofdm_ht_single_stream_basic_rates = 0xff;
1743 priv->staging_rxon.ofdm_ht_dual_stream_basic_rates = 0xff;
1744 iwl4965_set_rxon_chain(priv);
1745}
1746
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001747static int iwl4965_set_mode(struct iwl_priv *priv, int mode)
Zhu Yib481de92007-09-25 17:54:57 -07001748{
Zhu Yib481de92007-09-25 17:54:57 -07001749 if (mode == IEEE80211_IF_TYPE_IBSS) {
Assaf Kraussbf85ea42008-03-14 10:38:49 -07001750 const struct iwl_channel_info *ch_info;
Zhu Yib481de92007-09-25 17:54:57 -07001751
Assaf Krauss8622e702008-03-21 13:53:43 -07001752 ch_info = iwl_get_channel_info(priv,
Johannes Berg8318d782008-01-24 19:38:38 +01001753 priv->band,
Zhu Yib481de92007-09-25 17:54:57 -07001754 le16_to_cpu(priv->staging_rxon.channel));
1755
1756 if (!ch_info || !is_channel_ibss(ch_info)) {
1757 IWL_ERROR("channel %d not IBSS channel\n",
1758 le16_to_cpu(priv->staging_rxon.channel));
1759 return -EINVAL;
1760 }
1761 }
1762
Zhu Yib481de92007-09-25 17:54:57 -07001763 priv->iw_mode = mode;
1764
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001765 iwl4965_connection_init_rx_config(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001766 memcpy(priv->staging_rxon.node_addr, priv->mac_addr, ETH_ALEN);
1767
Assaf Kraussbf85ea42008-03-14 10:38:49 -07001768 iwlcore_clear_stations_table(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001769
Mohamed Abbasfde35712007-11-29 11:10:15 +08001770 /* dont commit rxon if rf-kill is on*/
Tomas Winklerfee12472008-04-03 16:05:21 -07001771 if (!iwl_is_ready_rf(priv))
Mohamed Abbasfde35712007-11-29 11:10:15 +08001772 return -EAGAIN;
1773
1774 cancel_delayed_work(&priv->scan_check);
1775 if (iwl4965_scan_cancel_timeout(priv, 100)) {
1776 IWL_WARNING("Aborted scan still in progress after 100ms\n");
1777 IWL_DEBUG_MAC80211("leaving - scan abort failed.\n");
1778 return -EAGAIN;
1779 }
1780
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001781 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001782
1783 return 0;
1784}
1785
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001786static void iwl4965_build_tx_cmd_hwcrypto(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07001787 struct ieee80211_tx_control *ctl,
Tomas Winkler857485c2008-03-21 13:53:44 -07001788 struct iwl_cmd *cmd,
Zhu Yib481de92007-09-25 17:54:57 -07001789 struct sk_buff *skb_frag,
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07001790 int sta_id)
Zhu Yib481de92007-09-25 17:54:57 -07001791{
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07001792 struct iwl4965_hw_key *keyinfo = &priv->stations[sta_id].keyinfo;
Emmanuel Grumbach6974e362008-04-14 21:16:06 -07001793 struct iwl_wep_key *wepkey;
1794 int keyidx = 0;
1795
Ivo van Doorn1c014422008-04-17 19:41:02 +02001796 BUG_ON(ctl->hw_key->hw_key_idx > 3);
Zhu Yib481de92007-09-25 17:54:57 -07001797
1798 switch (keyinfo->alg) {
1799 case ALG_CCMP:
1800 cmd->cmd.tx.sec_ctl = TX_CMD_SEC_CCM;
1801 memcpy(cmd->cmd.tx.key, keyinfo->key, keyinfo->keylen);
Max Stepanov8236e182008-03-12 16:58:48 -07001802 if (ctl->flags & IEEE80211_TXCTL_AMPDU)
1803 cmd->cmd.tx.tx_flags |= TX_CMD_FLG_AGG_CCMP_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07001804 IWL_DEBUG_TX("tx_cmd with aes hwcrypto\n");
1805 break;
1806
1807 case ALG_TKIP:
Zhu Yib481de92007-09-25 17:54:57 -07001808 cmd->cmd.tx.sec_ctl = TX_CMD_SEC_TKIP;
Emmanuel Grumbach2bc75082008-03-19 16:41:45 -07001809 ieee80211_get_tkip_key(keyinfo->conf, skb_frag,
1810 IEEE80211_TKIP_P2_KEY, cmd->cmd.tx.key);
1811 IWL_DEBUG_TX("tx_cmd with tkip hwcrypto\n");
Zhu Yib481de92007-09-25 17:54:57 -07001812 break;
1813
1814 case ALG_WEP:
Ivo van Doorn1c014422008-04-17 19:41:02 +02001815 wepkey = &priv->wep_keys[ctl->hw_key->hw_key_idx];
Emmanuel Grumbach6974e362008-04-14 21:16:06 -07001816 cmd->cmd.tx.sec_ctl = 0;
1817 if (priv->default_wep_key) {
1818 /* the WEP key was sent as static */
Ivo van Doorn1c014422008-04-17 19:41:02 +02001819 keyidx = ctl->hw_key->hw_key_idx;
Emmanuel Grumbach6974e362008-04-14 21:16:06 -07001820 memcpy(&cmd->cmd.tx.key[3], wepkey->key,
1821 wepkey->key_size);
1822 if (wepkey->key_size == WEP_KEY_LEN_128)
1823 cmd->cmd.tx.sec_ctl |= TX_CMD_SEC_KEY128;
1824 } else {
Emmanuel Grumbach0211ddd2008-04-14 21:16:07 -07001825 /* the WEP key was sent as dynamic */
1826 keyidx = keyinfo->keyidx;
1827 memcpy(&cmd->cmd.tx.key[3], keyinfo->key,
1828 keyinfo->keylen);
1829 if (keyinfo->keylen == WEP_KEY_LEN_128)
1830 cmd->cmd.tx.sec_ctl |= TX_CMD_SEC_KEY128;
Emmanuel Grumbach6974e362008-04-14 21:16:06 -07001831 }
Zhu Yib481de92007-09-25 17:54:57 -07001832
Emmanuel Grumbach6974e362008-04-14 21:16:06 -07001833 cmd->cmd.tx.sec_ctl |= (TX_CMD_SEC_WEP |
1834 (keyidx & TX_CMD_SEC_MSK) << TX_CMD_SEC_SHIFT);
Zhu Yib481de92007-09-25 17:54:57 -07001835
1836 IWL_DEBUG_TX("Configuring packet for WEP encryption "
Emmanuel Grumbach6974e362008-04-14 21:16:06 -07001837 "with key %d\n", keyidx);
Zhu Yib481de92007-09-25 17:54:57 -07001838 break;
1839
Zhu Yib481de92007-09-25 17:54:57 -07001840 default:
1841 printk(KERN_ERR "Unknown encode alg %d\n", keyinfo->alg);
1842 break;
1843 }
1844}
1845
1846/*
1847 * handle build REPLY_TX command notification.
1848 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001849static void iwl4965_build_tx_cmd_basic(struct iwl_priv *priv,
Tomas Winkler857485c2008-03-21 13:53:44 -07001850 struct iwl_cmd *cmd,
Zhu Yib481de92007-09-25 17:54:57 -07001851 struct ieee80211_tx_control *ctrl,
1852 struct ieee80211_hdr *hdr,
1853 int is_unicast, u8 std_id)
1854{
1855 __le16 *qc;
1856 u16 fc = le16_to_cpu(hdr->frame_control);
1857 __le32 tx_flags = cmd->cmd.tx.tx_flags;
1858
1859 cmd->cmd.tx.stop_time.life_time = TX_CMD_LIFE_TIME_INFINITE;
1860 if (!(ctrl->flags & IEEE80211_TXCTL_NO_ACK)) {
1861 tx_flags |= TX_CMD_FLG_ACK_MSK;
1862 if ((fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_MGMT)
1863 tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
1864 if (ieee80211_is_probe_response(fc) &&
1865 !(le16_to_cpu(hdr->seq_ctrl) & 0xf))
1866 tx_flags |= TX_CMD_FLG_TSF_MSK;
1867 } else {
1868 tx_flags &= (~TX_CMD_FLG_ACK_MSK);
1869 tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
1870 }
1871
Tomas Winkler87e4f7d2008-01-14 17:46:16 -08001872 if (ieee80211_is_back_request(fc))
1873 tx_flags |= TX_CMD_FLG_ACK_MSK | TX_CMD_FLG_IMM_BA_RSP_MASK;
1874
1875
Zhu Yib481de92007-09-25 17:54:57 -07001876 cmd->cmd.tx.sta_id = std_id;
1877 if (ieee80211_get_morefrag(hdr))
1878 tx_flags |= TX_CMD_FLG_MORE_FRAG_MSK;
1879
1880 qc = ieee80211_get_qos_ctrl(hdr);
1881 if (qc) {
1882 cmd->cmd.tx.tid_tspec = (u8) (le16_to_cpu(*qc) & 0xf);
1883 tx_flags &= ~TX_CMD_FLG_SEQ_CTL_MSK;
1884 } else
1885 tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
1886
1887 if (ctrl->flags & IEEE80211_TXCTL_USE_RTS_CTS) {
1888 tx_flags |= TX_CMD_FLG_RTS_MSK;
1889 tx_flags &= ~TX_CMD_FLG_CTS_MSK;
1890 } else if (ctrl->flags & IEEE80211_TXCTL_USE_CTS_PROTECT) {
1891 tx_flags &= ~TX_CMD_FLG_RTS_MSK;
1892 tx_flags |= TX_CMD_FLG_CTS_MSK;
1893 }
1894
1895 if ((tx_flags & TX_CMD_FLG_RTS_MSK) || (tx_flags & TX_CMD_FLG_CTS_MSK))
1896 tx_flags |= TX_CMD_FLG_FULL_TXOP_PROT_MSK;
1897
1898 tx_flags &= ~(TX_CMD_FLG_ANT_SEL_MSK);
1899 if ((fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_MGMT) {
1900 if ((fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_ASSOC_REQ ||
1901 (fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_REASSOC_REQ)
Ian Schrambc434dd2007-10-25 17:15:29 +08001902 cmd->cmd.tx.timeout.pm_frame_timeout = cpu_to_le16(3);
Zhu Yib481de92007-09-25 17:54:57 -07001903 else
Ian Schrambc434dd2007-10-25 17:15:29 +08001904 cmd->cmd.tx.timeout.pm_frame_timeout = cpu_to_le16(2);
Mohamed Abbasab53d8a2008-03-25 16:33:36 -07001905 } else {
Zhu Yib481de92007-09-25 17:54:57 -07001906 cmd->cmd.tx.timeout.pm_frame_timeout = 0;
Mohamed Abbasab53d8a2008-03-25 16:33:36 -07001907 }
Zhu Yib481de92007-09-25 17:54:57 -07001908
1909 cmd->cmd.tx.driver_txop = 0;
1910 cmd->cmd.tx.tx_flags = tx_flags;
1911 cmd->cmd.tx.next_frame_len = 0;
1912}
Tomas Winkler19758be2008-03-12 16:58:51 -07001913static void iwl_update_tx_stats(struct iwl_priv *priv, u16 fc, u16 len)
1914{
1915 /* 0 - mgmt, 1 - cnt, 2 - data */
1916 int idx = (fc & IEEE80211_FCTL_FTYPE) >> 2;
1917 priv->tx_stats[idx].cnt++;
1918 priv->tx_stats[idx].bytes += len;
1919}
Cahill, Ben M6440adb2007-11-29 11:09:55 +08001920/**
1921 * iwl4965_get_sta_id - Find station's index within station table
1922 *
1923 * If new IBSS station, create new entry in station table
1924 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001925static int iwl4965_get_sta_id(struct iwl_priv *priv,
Ben Cahill9fbab512007-11-29 11:09:47 +08001926 struct ieee80211_hdr *hdr)
Zhu Yib481de92007-09-25 17:54:57 -07001927{
1928 int sta_id;
1929 u16 fc = le16_to_cpu(hdr->frame_control);
Joe Perches0795af52007-10-03 17:59:30 -07001930 DECLARE_MAC_BUF(mac);
Zhu Yib481de92007-09-25 17:54:57 -07001931
Cahill, Ben M6440adb2007-11-29 11:09:55 +08001932 /* If this frame is broadcast or management, use broadcast station id */
Zhu Yib481de92007-09-25 17:54:57 -07001933 if (((fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_DATA) ||
1934 is_multicast_ether_addr(hdr->addr1))
Tomas Winkler5425e492008-04-15 16:01:38 -07001935 return priv->hw_params.bcast_sta_id;
Zhu Yib481de92007-09-25 17:54:57 -07001936
1937 switch (priv->iw_mode) {
1938
Cahill, Ben M6440adb2007-11-29 11:09:55 +08001939 /* If we are a client station in a BSS network, use the special
1940 * AP station entry (that's the only station we communicate with) */
Zhu Yib481de92007-09-25 17:54:57 -07001941 case IEEE80211_IF_TYPE_STA:
1942 return IWL_AP_ID;
1943
1944 /* If we are an AP, then find the station, or use BCAST */
1945 case IEEE80211_IF_TYPE_AP:
Tomas Winkler947b13a2008-04-16 16:34:48 -07001946 sta_id = iwl_find_station(priv, hdr->addr1);
Zhu Yib481de92007-09-25 17:54:57 -07001947 if (sta_id != IWL_INVALID_STATION)
1948 return sta_id;
Tomas Winkler5425e492008-04-15 16:01:38 -07001949 return priv->hw_params.bcast_sta_id;
Zhu Yib481de92007-09-25 17:54:57 -07001950
Cahill, Ben M6440adb2007-11-29 11:09:55 +08001951 /* If this frame is going out to an IBSS network, find the station,
1952 * or create a new station table entry */
Zhu Yib481de92007-09-25 17:54:57 -07001953 case IEEE80211_IF_TYPE_IBSS:
Tomas Winkler947b13a2008-04-16 16:34:48 -07001954 sta_id = iwl_find_station(priv, hdr->addr1);
Zhu Yib481de92007-09-25 17:54:57 -07001955 if (sta_id != IWL_INVALID_STATION)
1956 return sta_id;
1957
Cahill, Ben M6440adb2007-11-29 11:09:55 +08001958 /* Create new station table entry */
Ron Rindjunsky67d62032007-11-26 16:14:40 +02001959 sta_id = iwl4965_add_station_flags(priv, hdr->addr1,
1960 0, CMD_ASYNC, NULL);
Zhu Yib481de92007-09-25 17:54:57 -07001961
1962 if (sta_id != IWL_INVALID_STATION)
1963 return sta_id;
1964
Joe Perches0795af52007-10-03 17:59:30 -07001965 IWL_DEBUG_DROP("Station %s not in station map. "
Zhu Yib481de92007-09-25 17:54:57 -07001966 "Defaulting to broadcast...\n",
Joe Perches0795af52007-10-03 17:59:30 -07001967 print_mac(mac, hdr->addr1));
Tomas Winkler0a6857e2008-03-12 16:58:49 -07001968 iwl_print_hex_dump(IWL_DL_DROP, (u8 *) hdr, sizeof(*hdr));
Tomas Winkler5425e492008-04-15 16:01:38 -07001969 return priv->hw_params.bcast_sta_id;
Zhu Yib481de92007-09-25 17:54:57 -07001970
1971 default:
Ian Schram01ebd062007-10-25 17:15:22 +08001972 IWL_WARNING("Unknown mode of operation: %d", priv->iw_mode);
Tomas Winkler5425e492008-04-15 16:01:38 -07001973 return priv->hw_params.bcast_sta_id;
Zhu Yib481de92007-09-25 17:54:57 -07001974 }
1975}
1976
1977/*
1978 * start REPLY_TX command process
1979 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001980static int iwl4965_tx_skb(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07001981 struct sk_buff *skb, struct ieee80211_tx_control *ctl)
1982{
1983 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001984 struct iwl4965_tfd_frame *tfd;
Zhu Yib481de92007-09-25 17:54:57 -07001985 u32 *control_flags;
1986 int txq_id = ctl->queue;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001987 struct iwl4965_tx_queue *txq = NULL;
1988 struct iwl4965_queue *q = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07001989 dma_addr_t phys_addr;
1990 dma_addr_t txcmd_phys;
Tomas Winkler87e4f7d2008-01-14 17:46:16 -08001991 dma_addr_t scratch_phys;
Tomas Winkler857485c2008-03-21 13:53:44 -07001992 struct iwl_cmd *out_cmd = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07001993 u16 len, idx, len_org;
1994 u8 id, hdr_len, unicast;
1995 u8 sta_id;
1996 u16 seq_number = 0;
1997 u16 fc;
1998 __le16 *qc;
1999 u8 wait_write_ptr = 0;
2000 unsigned long flags;
2001 int rc;
2002
2003 spin_lock_irqsave(&priv->lock, flags);
Tomas Winklerfee12472008-04-03 16:05:21 -07002004 if (iwl_is_rfkill(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07002005 IWL_DEBUG_DROP("Dropping - RF KILL\n");
2006 goto drop_unlock;
2007 }
2008
Johannes Berg32bfd352007-12-19 01:31:26 +01002009 if (!priv->vif) {
2010 IWL_DEBUG_DROP("Dropping - !priv->vif\n");
Zhu Yib481de92007-09-25 17:54:57 -07002011 goto drop_unlock;
2012 }
2013
Johannes Berg8318d782008-01-24 19:38:38 +01002014 if ((ctl->tx_rate->hw_value & 0xFF) == IWL_INVALID_RATE) {
Zhu Yib481de92007-09-25 17:54:57 -07002015 IWL_ERROR("ERROR: No TX rate available.\n");
2016 goto drop_unlock;
2017 }
2018
2019 unicast = !is_multicast_ether_addr(hdr->addr1);
2020 id = 0;
2021
2022 fc = le16_to_cpu(hdr->frame_control);
2023
Tomas Winkler0a6857e2008-03-12 16:58:49 -07002024#ifdef CONFIG_IWLWIFI_DEBUG
Zhu Yib481de92007-09-25 17:54:57 -07002025 if (ieee80211_is_auth(fc))
2026 IWL_DEBUG_TX("Sending AUTH frame\n");
2027 else if (ieee80211_is_assoc_request(fc))
2028 IWL_DEBUG_TX("Sending ASSOC frame\n");
2029 else if (ieee80211_is_reassoc_request(fc))
2030 IWL_DEBUG_TX("Sending REASSOC frame\n");
2031#endif
2032
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08002033 /* drop all data frame if we are not associated */
Gregory Greenman76f39152008-01-23 10:15:21 -08002034 if (((fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_DATA) &&
Tomas Winkler3109ece2008-03-28 16:33:35 -07002035 (!iwl_is_associated(priv) ||
Reinette Chatrea6477242008-02-14 10:40:28 -08002036 ((priv->iw_mode == IEEE80211_IF_TYPE_STA) && !priv->assoc_id) ||
Gregory Greenman76f39152008-01-23 10:15:21 -08002037 !priv->assoc_station_added)) {
Tomas Winkler3109ece2008-03-28 16:33:35 -07002038 IWL_DEBUG_DROP("Dropping - !iwl_is_associated\n");
Zhu Yib481de92007-09-25 17:54:57 -07002039 goto drop_unlock;
2040 }
2041
2042 spin_unlock_irqrestore(&priv->lock, flags);
2043
2044 hdr_len = ieee80211_get_hdrlen(fc);
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002045
2046 /* Find (or create) index into station table for destination station */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002047 sta_id = iwl4965_get_sta_id(priv, hdr);
Zhu Yib481de92007-09-25 17:54:57 -07002048 if (sta_id == IWL_INVALID_STATION) {
Joe Perches0795af52007-10-03 17:59:30 -07002049 DECLARE_MAC_BUF(mac);
2050
2051 IWL_DEBUG_DROP("Dropping - INVALID STATION: %s\n",
2052 print_mac(mac, hdr->addr1));
Zhu Yib481de92007-09-25 17:54:57 -07002053 goto drop;
2054 }
2055
2056 IWL_DEBUG_RATE("station Id %d\n", sta_id);
2057
2058 qc = ieee80211_get_qos_ctrl(hdr);
2059 if (qc) {
2060 u8 tid = (u8)(le16_to_cpu(*qc) & 0xf);
2061 seq_number = priv->stations[sta_id].tid[tid].seq_number &
2062 IEEE80211_SCTL_SEQ;
2063 hdr->seq_ctrl = cpu_to_le16(seq_number) |
2064 (hdr->seq_ctrl &
2065 __constant_cpu_to_le16(IEEE80211_SCTL_FRAG));
2066 seq_number += 0x10;
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08002067#ifdef CONFIG_IWL4965_HT
Zhu Yib481de92007-09-25 17:54:57 -07002068 /* aggregation is on for this <sta,tid> */
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02002069 if (ctl->flags & IEEE80211_TXCTL_AMPDU)
Zhu Yib481de92007-09-25 17:54:57 -07002070 txq_id = priv->stations[sta_id].tid[tid].agg.txq_id;
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02002071 priv->stations[sta_id].tid[tid].tfds_in_queue++;
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08002072#endif /* CONFIG_IWL4965_HT */
Zhu Yib481de92007-09-25 17:54:57 -07002073 }
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002074
2075 /* Descriptor for chosen Tx queue */
Zhu Yib481de92007-09-25 17:54:57 -07002076 txq = &priv->txq[txq_id];
2077 q = &txq->q;
2078
2079 spin_lock_irqsave(&priv->lock, flags);
2080
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002081 /* Set up first empty TFD within this queue's circular TFD buffer */
Tomas Winklerfc4b6852007-10-25 17:15:24 +08002082 tfd = &txq->bd[q->write_ptr];
Zhu Yib481de92007-09-25 17:54:57 -07002083 memset(tfd, 0, sizeof(*tfd));
2084 control_flags = (u32 *) tfd;
Tomas Winklerfc4b6852007-10-25 17:15:24 +08002085 idx = get_cmd_index(q, q->write_ptr, 0);
Zhu Yib481de92007-09-25 17:54:57 -07002086
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002087 /* Set up driver data for this TFD */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002088 memset(&(txq->txb[q->write_ptr]), 0, sizeof(struct iwl4965_tx_info));
Tomas Winklerfc4b6852007-10-25 17:15:24 +08002089 txq->txb[q->write_ptr].skb[0] = skb;
2090 memcpy(&(txq->txb[q->write_ptr].status.control),
Zhu Yib481de92007-09-25 17:54:57 -07002091 ctl, sizeof(struct ieee80211_tx_control));
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002092
2093 /* Set up first empty entry in queue's array of Tx/cmd buffers */
Zhu Yib481de92007-09-25 17:54:57 -07002094 out_cmd = &txq->cmd[idx];
2095 memset(&out_cmd->hdr, 0, sizeof(out_cmd->hdr));
2096 memset(&out_cmd->cmd.tx, 0, sizeof(out_cmd->cmd.tx));
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002097
2098 /*
2099 * Set up the Tx-command (not MAC!) header.
2100 * Store the chosen Tx queue and TFD index within the sequence field;
2101 * after Tx, uCode's Tx response will return this value so driver can
2102 * locate the frame within the tx queue and do post-tx processing.
2103 */
Zhu Yib481de92007-09-25 17:54:57 -07002104 out_cmd->hdr.cmd = REPLY_TX;
2105 out_cmd->hdr.sequence = cpu_to_le16((u16)(QUEUE_TO_SEQ(txq_id) |
Tomas Winklerfc4b6852007-10-25 17:15:24 +08002106 INDEX_TO_SEQ(q->write_ptr)));
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002107
2108 /* Copy MAC header from skb into command buffer */
Zhu Yib481de92007-09-25 17:54:57 -07002109 memcpy(out_cmd->cmd.tx.hdr, hdr, hdr_len);
2110
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002111 /*
2112 * Use the first empty entry in this queue's command buffer array
2113 * to contain the Tx command and MAC header concatenated together
2114 * (payload data will be in another buffer).
2115 * Size of this varies, due to varying MAC header length.
2116 * If end is not dword aligned, we'll have 2 extra bytes at the end
2117 * of the MAC header (device reads on dword boundaries).
2118 * We'll tell device about this padding later.
2119 */
Tomas Winkler5425e492008-04-15 16:01:38 -07002120 len = priv->hw_params.tx_cmd_len +
Tomas Winkler857485c2008-03-21 13:53:44 -07002121 sizeof(struct iwl_cmd_header) + hdr_len;
Zhu Yib481de92007-09-25 17:54:57 -07002122
2123 len_org = len;
2124 len = (len + 3) & ~3;
2125
2126 if (len_org != len)
2127 len_org = 1;
2128 else
2129 len_org = 0;
2130
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002131 /* Physical address of this Tx command's header (not MAC header!),
2132 * within command buffer array. */
Tomas Winkler857485c2008-03-21 13:53:44 -07002133 txcmd_phys = txq->dma_addr_cmd + sizeof(struct iwl_cmd) * idx +
2134 offsetof(struct iwl_cmd, hdr);
Zhu Yib481de92007-09-25 17:54:57 -07002135
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002136 /* Add buffer containing Tx command and MAC(!) header to TFD's
2137 * first entry */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002138 iwl4965_hw_txq_attach_buf_to_tfd(priv, tfd, txcmd_phys, len);
Zhu Yib481de92007-09-25 17:54:57 -07002139
2140 if (!(ctl->flags & IEEE80211_TXCTL_DO_NOT_ENCRYPT))
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07002141 iwl4965_build_tx_cmd_hwcrypto(priv, ctl, out_cmd, skb, sta_id);
Zhu Yib481de92007-09-25 17:54:57 -07002142
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002143 /* Set up TFD's 2nd entry to point directly to remainder of skb,
2144 * if any (802.11 null frames have no payload). */
Zhu Yib481de92007-09-25 17:54:57 -07002145 len = skb->len - hdr_len;
2146 if (len) {
2147 phys_addr = pci_map_single(priv->pci_dev, skb->data + hdr_len,
2148 len, PCI_DMA_TODEVICE);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002149 iwl4965_hw_txq_attach_buf_to_tfd(priv, tfd, phys_addr, len);
Zhu Yib481de92007-09-25 17:54:57 -07002150 }
2151
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002152 /* Tell 4965 about any 2-byte padding after MAC header */
Zhu Yib481de92007-09-25 17:54:57 -07002153 if (len_org)
2154 out_cmd->cmd.tx.tx_flags |= TX_CMD_FLG_MH_PAD_MSK;
2155
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002156 /* Total # bytes to be transmitted */
Zhu Yib481de92007-09-25 17:54:57 -07002157 len = (u16)skb->len;
2158 out_cmd->cmd.tx.len = cpu_to_le16(len);
2159
2160 /* TODO need this for burst mode later on */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002161 iwl4965_build_tx_cmd_basic(priv, out_cmd, ctl, hdr, unicast, sta_id);
Zhu Yib481de92007-09-25 17:54:57 -07002162
2163 /* set is_hcca to 0; it probably will never be implemented */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002164 iwl4965_hw_build_tx_cmd_rate(priv, out_cmd, ctl, hdr, sta_id, 0);
Zhu Yib481de92007-09-25 17:54:57 -07002165
Tomas Winkler19758be2008-03-12 16:58:51 -07002166 iwl_update_tx_stats(priv, fc, len);
2167
Tomas Winkler857485c2008-03-21 13:53:44 -07002168 scratch_phys = txcmd_phys + sizeof(struct iwl_cmd_header) +
Tomas Winkler87e4f7d2008-01-14 17:46:16 -08002169 offsetof(struct iwl4965_tx_cmd, scratch);
2170 out_cmd->cmd.tx.dram_lsb_ptr = cpu_to_le32(scratch_phys);
2171 out_cmd->cmd.tx.dram_msb_ptr = iwl_get_dma_hi_address(scratch_phys);
2172
Zhu Yib481de92007-09-25 17:54:57 -07002173 if (!ieee80211_get_morefrag(hdr)) {
2174 txq->need_update = 1;
2175 if (qc) {
2176 u8 tid = (u8)(le16_to_cpu(*qc) & 0xf);
2177 priv->stations[sta_id].tid[tid].seq_number = seq_number;
2178 }
2179 } else {
2180 wait_write_ptr = 1;
2181 txq->need_update = 0;
2182 }
2183
Tomas Winkler0a6857e2008-03-12 16:58:49 -07002184 iwl_print_hex_dump(IWL_DL_TX, out_cmd->cmd.payload,
Zhu Yib481de92007-09-25 17:54:57 -07002185 sizeof(out_cmd->cmd.tx));
2186
Tomas Winkler0a6857e2008-03-12 16:58:49 -07002187 iwl_print_hex_dump(IWL_DL_TX, (u8 *)out_cmd->cmd.tx.hdr,
Zhu Yib481de92007-09-25 17:54:57 -07002188 ieee80211_get_hdrlen(fc));
2189
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002190 /* Set up entry for this TFD in Tx byte-count array */
Tomas Winklere2a722e2008-04-14 21:16:10 -07002191 priv->cfg->ops->lib->txq_update_byte_cnt_tbl(priv, txq, len);
Zhu Yib481de92007-09-25 17:54:57 -07002192
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002193 /* Tell device the write index *just past* this latest filled TFD */
Tomas Winklerc54b6792008-03-06 17:36:53 -08002194 q->write_ptr = iwl_queue_inc_wrap(q->write_ptr, q->n_bd);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002195 rc = iwl4965_tx_queue_update_write_ptr(priv, txq);
Zhu Yib481de92007-09-25 17:54:57 -07002196 spin_unlock_irqrestore(&priv->lock, flags);
2197
2198 if (rc)
2199 return rc;
2200
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002201 if ((iwl4965_queue_space(q) < q->high_mark)
Zhu Yib481de92007-09-25 17:54:57 -07002202 && priv->mac80211_registered) {
2203 if (wait_write_ptr) {
2204 spin_lock_irqsave(&priv->lock, flags);
2205 txq->need_update = 1;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002206 iwl4965_tx_queue_update_write_ptr(priv, txq);
Zhu Yib481de92007-09-25 17:54:57 -07002207 spin_unlock_irqrestore(&priv->lock, flags);
2208 }
2209
2210 ieee80211_stop_queue(priv->hw, ctl->queue);
2211 }
2212
2213 return 0;
2214
2215drop_unlock:
2216 spin_unlock_irqrestore(&priv->lock, flags);
2217drop:
2218 return -1;
2219}
2220
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002221static void iwl4965_set_rate(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002222{
Johannes Berg8318d782008-01-24 19:38:38 +01002223 const struct ieee80211_supported_band *hw = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07002224 struct ieee80211_rate *rate;
2225 int i;
2226
Emmanuel Grumbachd1141df2008-04-21 15:42:00 -07002227 hw = iwl_get_hw_mode(priv, priv->band);
Saleem Abdulrasoolc4ba9622007-11-18 23:59:08 -08002228 if (!hw) {
2229 IWL_ERROR("Failed to set rate: unable to get hw mode\n");
2230 return;
2231 }
Zhu Yib481de92007-09-25 17:54:57 -07002232
2233 priv->active_rate = 0;
2234 priv->active_rate_basic = 0;
2235
Johannes Berg8318d782008-01-24 19:38:38 +01002236 for (i = 0; i < hw->n_bitrates; i++) {
2237 rate = &(hw->bitrates[i]);
2238 if (rate->hw_value < IWL_RATE_COUNT)
2239 priv->active_rate |= (1 << rate->hw_value);
Zhu Yib481de92007-09-25 17:54:57 -07002240 }
2241
2242 IWL_DEBUG_RATE("Set active_rate = %0x, active_rate_basic = %0x\n",
2243 priv->active_rate, priv->active_rate_basic);
2244
2245 /*
2246 * If a basic rate is configured, then use it (adding IWL_RATE_1M_MASK)
2247 * otherwise set it to the default of all CCK rates and 6, 12, 24 for
2248 * OFDM
2249 */
2250 if (priv->active_rate_basic & IWL_CCK_BASIC_RATES_MASK)
2251 priv->staging_rxon.cck_basic_rates =
2252 ((priv->active_rate_basic &
2253 IWL_CCK_RATES_MASK) >> IWL_FIRST_CCK_RATE) & 0xF;
2254 else
2255 priv->staging_rxon.cck_basic_rates =
2256 (IWL_CCK_BASIC_RATES_MASK >> IWL_FIRST_CCK_RATE) & 0xF;
2257
2258 if (priv->active_rate_basic & IWL_OFDM_BASIC_RATES_MASK)
2259 priv->staging_rxon.ofdm_basic_rates =
2260 ((priv->active_rate_basic &
2261 (IWL_OFDM_BASIC_RATES_MASK | IWL_RATE_6M_MASK)) >>
2262 IWL_FIRST_OFDM_RATE) & 0xFF;
2263 else
2264 priv->staging_rxon.ofdm_basic_rates =
2265 (IWL_OFDM_BASIC_RATES_MASK >> IWL_FIRST_OFDM_RATE) & 0xFF;
2266}
2267
Mohamed Abbasad97edd2008-03-28 16:21:06 -07002268void iwl4965_radio_kill_sw(struct iwl_priv *priv, int disable_radio)
Zhu Yib481de92007-09-25 17:54:57 -07002269{
2270 unsigned long flags;
2271
2272 if (!!disable_radio == test_bit(STATUS_RF_KILL_SW, &priv->status))
2273 return;
2274
2275 IWL_DEBUG_RF_KILL("Manual SW RF KILL set to: RADIO %s\n",
2276 disable_radio ? "OFF" : "ON");
2277
2278 if (disable_radio) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002279 iwl4965_scan_cancel(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002280 /* FIXME: This is a workaround for AP */
2281 if (priv->iw_mode != IEEE80211_IF_TYPE_AP) {
2282 spin_lock_irqsave(&priv->lock, flags);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07002283 iwl_write32(priv, CSR_UCODE_DRV_GP1_SET,
Zhu Yib481de92007-09-25 17:54:57 -07002284 CSR_UCODE_SW_BIT_RFKILL);
2285 spin_unlock_irqrestore(&priv->lock, flags);
Mohamed Abbasad97edd2008-03-28 16:21:06 -07002286 /* call the host command only if no hw rf-kill set */
Mohamed Abbas59003832008-04-15 16:01:46 -07002287 if (!test_bit(STATUS_RF_KILL_HW, &priv->status) &&
2288 iwl_is_ready(priv))
Mohamed Abbasad97edd2008-03-28 16:21:06 -07002289 iwl4965_send_card_state(priv,
2290 CARD_STATE_CMD_DISABLE,
2291 0);
Zhu Yib481de92007-09-25 17:54:57 -07002292 set_bit(STATUS_RF_KILL_SW, &priv->status);
Mohamed Abbasad97edd2008-03-28 16:21:06 -07002293
2294 /* make sure mac80211 stop sending Tx frame */
2295 if (priv->mac80211_registered)
2296 ieee80211_stop_queues(priv->hw);
Zhu Yib481de92007-09-25 17:54:57 -07002297 }
2298 return;
2299 }
2300
2301 spin_lock_irqsave(&priv->lock, flags);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07002302 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
Zhu Yib481de92007-09-25 17:54:57 -07002303
2304 clear_bit(STATUS_RF_KILL_SW, &priv->status);
2305 spin_unlock_irqrestore(&priv->lock, flags);
2306
2307 /* wake up ucode */
2308 msleep(10);
2309
2310 spin_lock_irqsave(&priv->lock, flags);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07002311 iwl_read32(priv, CSR_UCODE_DRV_GP1);
2312 if (!iwl_grab_nic_access(priv))
2313 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002314 spin_unlock_irqrestore(&priv->lock, flags);
2315
2316 if (test_bit(STATUS_RF_KILL_HW, &priv->status)) {
2317 IWL_DEBUG_RF_KILL("Can not turn radio back on - "
2318 "disabled by HW switch\n");
2319 return;
2320 }
2321
2322 queue_work(priv->workqueue, &priv->restart);
2323 return;
2324}
2325
Zhu Yib481de92007-09-25 17:54:57 -07002326#define IWL_PACKET_RETRY_TIME HZ
2327
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002328int iwl4965_is_duplicate_packet(struct iwl_priv *priv, struct ieee80211_hdr *header)
Zhu Yib481de92007-09-25 17:54:57 -07002329{
2330 u16 sc = le16_to_cpu(header->seq_ctrl);
2331 u16 seq = (sc & IEEE80211_SCTL_SEQ) >> 4;
2332 u16 frag = sc & IEEE80211_SCTL_FRAG;
2333 u16 *last_seq, *last_frag;
2334 unsigned long *last_time;
2335
2336 switch (priv->iw_mode) {
2337 case IEEE80211_IF_TYPE_IBSS:{
2338 struct list_head *p;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002339 struct iwl4965_ibss_seq *entry = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07002340 u8 *mac = header->addr2;
2341 int index = mac[5] & (IWL_IBSS_MAC_HASH_SIZE - 1);
2342
2343 __list_for_each(p, &priv->ibss_mac_hash[index]) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002344 entry = list_entry(p, struct iwl4965_ibss_seq, list);
Zhu Yib481de92007-09-25 17:54:57 -07002345 if (!compare_ether_addr(entry->mac, mac))
2346 break;
2347 }
2348 if (p == &priv->ibss_mac_hash[index]) {
2349 entry = kzalloc(sizeof(*entry), GFP_ATOMIC);
2350 if (!entry) {
Ian Schrambc434dd2007-10-25 17:15:29 +08002351 IWL_ERROR("Cannot malloc new mac entry\n");
Zhu Yib481de92007-09-25 17:54:57 -07002352 return 0;
2353 }
2354 memcpy(entry->mac, mac, ETH_ALEN);
2355 entry->seq_num = seq;
2356 entry->frag_num = frag;
2357 entry->packet_time = jiffies;
Ian Schrambc434dd2007-10-25 17:15:29 +08002358 list_add(&entry->list, &priv->ibss_mac_hash[index]);
Zhu Yib481de92007-09-25 17:54:57 -07002359 return 0;
2360 }
2361 last_seq = &entry->seq_num;
2362 last_frag = &entry->frag_num;
2363 last_time = &entry->packet_time;
2364 break;
2365 }
2366 case IEEE80211_IF_TYPE_STA:
2367 last_seq = &priv->last_seq_num;
2368 last_frag = &priv->last_frag_num;
2369 last_time = &priv->last_packet_time;
2370 break;
2371 default:
2372 return 0;
2373 }
2374 if ((*last_seq == seq) &&
2375 time_after(*last_time + IWL_PACKET_RETRY_TIME, jiffies)) {
2376 if (*last_frag == frag)
2377 goto drop;
2378 if (*last_frag + 1 != frag)
2379 /* out-of-order fragment */
2380 goto drop;
2381 } else
2382 *last_seq = seq;
2383
2384 *last_frag = frag;
2385 *last_time = jiffies;
2386 return 0;
2387
2388 drop:
2389 return 1;
2390}
2391
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08002392#ifdef CONFIG_IWL4965_SPECTRUM_MEASUREMENT
Zhu Yib481de92007-09-25 17:54:57 -07002393
2394#include "iwl-spectrum.h"
2395
2396#define BEACON_TIME_MASK_LOW 0x00FFFFFF
2397#define BEACON_TIME_MASK_HIGH 0xFF000000
2398#define TIME_UNIT 1024
2399
2400/*
2401 * extended beacon time format
2402 * time in usec will be changed into a 32-bit value in 8:24 format
2403 * the high 1 byte is the beacon counts
2404 * the lower 3 bytes is the time in usec within one beacon interval
2405 */
2406
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002407static u32 iwl4965_usecs_to_beacons(u32 usec, u32 beacon_interval)
Zhu Yib481de92007-09-25 17:54:57 -07002408{
2409 u32 quot;
2410 u32 rem;
2411 u32 interval = beacon_interval * 1024;
2412
2413 if (!interval || !usec)
2414 return 0;
2415
2416 quot = (usec / interval) & (BEACON_TIME_MASK_HIGH >> 24);
2417 rem = (usec % interval) & BEACON_TIME_MASK_LOW;
2418
2419 return (quot << 24) + rem;
2420}
2421
2422/* base is usually what we get from ucode with each received frame,
2423 * the same as HW timer counter counting down
2424 */
2425
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002426static __le32 iwl4965_add_beacon_time(u32 base, u32 addon, u32 beacon_interval)
Zhu Yib481de92007-09-25 17:54:57 -07002427{
2428 u32 base_low = base & BEACON_TIME_MASK_LOW;
2429 u32 addon_low = addon & BEACON_TIME_MASK_LOW;
2430 u32 interval = beacon_interval * TIME_UNIT;
2431 u32 res = (base & BEACON_TIME_MASK_HIGH) +
2432 (addon & BEACON_TIME_MASK_HIGH);
2433
2434 if (base_low > addon_low)
2435 res += base_low - addon_low;
2436 else if (base_low < addon_low) {
2437 res += interval + base_low - addon_low;
2438 res += (1 << 24);
2439 } else
2440 res += (1 << 24);
2441
2442 return cpu_to_le32(res);
2443}
2444
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002445static int iwl4965_get_measurement(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07002446 struct ieee80211_measurement_params *params,
2447 u8 type)
2448{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002449 struct iwl4965_spectrum_cmd spectrum;
2450 struct iwl4965_rx_packet *res;
Tomas Winkler857485c2008-03-21 13:53:44 -07002451 struct iwl_host_cmd cmd = {
Zhu Yib481de92007-09-25 17:54:57 -07002452 .id = REPLY_SPECTRUM_MEASUREMENT_CMD,
2453 .data = (void *)&spectrum,
2454 .meta.flags = CMD_WANT_SKB,
2455 };
2456 u32 add_time = le64_to_cpu(params->start_time);
2457 int rc;
2458 int spectrum_resp_status;
2459 int duration = le16_to_cpu(params->duration);
2460
Tomas Winkler3109ece2008-03-28 16:33:35 -07002461 if (iwl_is_associated(priv))
Zhu Yib481de92007-09-25 17:54:57 -07002462 add_time =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002463 iwl4965_usecs_to_beacons(
Zhu Yib481de92007-09-25 17:54:57 -07002464 le64_to_cpu(params->start_time) - priv->last_tsf,
2465 le16_to_cpu(priv->rxon_timing.beacon_interval));
2466
2467 memset(&spectrum, 0, sizeof(spectrum));
2468
2469 spectrum.channel_count = cpu_to_le16(1);
2470 spectrum.flags =
2471 RXON_FLG_TSF2HOST_MSK | RXON_FLG_ANT_A_MSK | RXON_FLG_DIS_DIV_MSK;
2472 spectrum.filter_flags = MEASUREMENT_FILTER_FLAG;
2473 cmd.len = sizeof(spectrum);
2474 spectrum.len = cpu_to_le16(cmd.len - sizeof(spectrum.len));
2475
Tomas Winkler3109ece2008-03-28 16:33:35 -07002476 if (iwl_is_associated(priv))
Zhu Yib481de92007-09-25 17:54:57 -07002477 spectrum.start_time =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002478 iwl4965_add_beacon_time(priv->last_beacon_time,
Zhu Yib481de92007-09-25 17:54:57 -07002479 add_time,
2480 le16_to_cpu(priv->rxon_timing.beacon_interval));
2481 else
2482 spectrum.start_time = 0;
2483
2484 spectrum.channels[0].duration = cpu_to_le32(duration * TIME_UNIT);
2485 spectrum.channels[0].channel = params->channel;
2486 spectrum.channels[0].type = type;
2487 if (priv->active_rxon.flags & RXON_FLG_BAND_24G_MSK)
2488 spectrum.flags |= RXON_FLG_BAND_24G_MSK |
2489 RXON_FLG_AUTO_DETECT_MSK | RXON_FLG_TGG_PROTECT_MSK;
2490
Tomas Winkler857485c2008-03-21 13:53:44 -07002491 rc = iwl_send_cmd_sync(priv, &cmd);
Zhu Yib481de92007-09-25 17:54:57 -07002492 if (rc)
2493 return rc;
2494
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002495 res = (struct iwl4965_rx_packet *)cmd.meta.u.skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07002496 if (res->hdr.flags & IWL_CMD_FAILED_MSK) {
2497 IWL_ERROR("Bad return from REPLY_RX_ON_ASSOC command\n");
2498 rc = -EIO;
2499 }
2500
2501 spectrum_resp_status = le16_to_cpu(res->u.spectrum.status);
2502 switch (spectrum_resp_status) {
2503 case 0: /* Command will be handled */
2504 if (res->u.spectrum.id != 0xff) {
2505 IWL_DEBUG_INFO
2506 ("Replaced existing measurement: %d\n",
2507 res->u.spectrum.id);
2508 priv->measurement_status &= ~MEASUREMENT_READY;
2509 }
2510 priv->measurement_status |= MEASUREMENT_ACTIVE;
2511 rc = 0;
2512 break;
2513
2514 case 1: /* Command will not be handled */
2515 rc = -EAGAIN;
2516 break;
2517 }
2518
2519 dev_kfree_skb_any(cmd.meta.u.skb);
2520
2521 return rc;
2522}
2523#endif
2524
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002525static void iwl4965_txstatus_to_ieee(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002526 struct iwl4965_tx_info *tx_sta)
Zhu Yib481de92007-09-25 17:54:57 -07002527{
2528
2529 tx_sta->status.ack_signal = 0;
2530 tx_sta->status.excessive_retries = 0;
2531 tx_sta->status.queue_length = 0;
2532 tx_sta->status.queue_number = 0;
2533
2534 if (in_interrupt())
2535 ieee80211_tx_status_irqsafe(priv->hw,
2536 tx_sta->skb[0], &(tx_sta->status));
2537 else
2538 ieee80211_tx_status(priv->hw,
2539 tx_sta->skb[0], &(tx_sta->status));
2540
2541 tx_sta->skb[0] = NULL;
2542}
2543
2544/**
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002545 * iwl4965_tx_queue_reclaim - Reclaim Tx queue entries already Tx'd
Zhu Yib481de92007-09-25 17:54:57 -07002546 *
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002547 * When FW advances 'R' index, all entries between old and new 'R' index
2548 * need to be reclaimed. As result, some free space forms. If there is
2549 * enough free space (> low mark), wake the stack that feeds us.
Zhu Yib481de92007-09-25 17:54:57 -07002550 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002551int iwl4965_tx_queue_reclaim(struct iwl_priv *priv, int txq_id, int index)
Zhu Yib481de92007-09-25 17:54:57 -07002552{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002553 struct iwl4965_tx_queue *txq = &priv->txq[txq_id];
2554 struct iwl4965_queue *q = &txq->q;
Zhu Yib481de92007-09-25 17:54:57 -07002555 int nfreed = 0;
2556
2557 if ((index >= q->n_bd) || (x2_queue_used(q, index) == 0)) {
2558 IWL_ERROR("Read index for DMA queue txq id (%d), index %d, "
2559 "is out of range [0-%d] %d %d.\n", txq_id,
Tomas Winklerfc4b6852007-10-25 17:15:24 +08002560 index, q->n_bd, q->write_ptr, q->read_ptr);
Zhu Yib481de92007-09-25 17:54:57 -07002561 return 0;
2562 }
2563
Tomas Winklerc54b6792008-03-06 17:36:53 -08002564 for (index = iwl_queue_inc_wrap(index, q->n_bd);
Tomas Winklerfc4b6852007-10-25 17:15:24 +08002565 q->read_ptr != index;
Tomas Winklerc54b6792008-03-06 17:36:53 -08002566 q->read_ptr = iwl_queue_inc_wrap(q->read_ptr, q->n_bd)) {
Zhu Yib481de92007-09-25 17:54:57 -07002567 if (txq_id != IWL_CMD_QUEUE_NUM) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002568 iwl4965_txstatus_to_ieee(priv,
Tomas Winklerfc4b6852007-10-25 17:15:24 +08002569 &(txq->txb[txq->q.read_ptr]));
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002570 iwl4965_hw_txq_free_tfd(priv, txq);
Zhu Yib481de92007-09-25 17:54:57 -07002571 } else if (nfreed > 1) {
2572 IWL_ERROR("HCMD skipped: index (%d) %d %d\n", index,
Tomas Winklerfc4b6852007-10-25 17:15:24 +08002573 q->write_ptr, q->read_ptr);
Zhu Yib481de92007-09-25 17:54:57 -07002574 queue_work(priv->workqueue, &priv->restart);
2575 }
2576 nfreed++;
2577 }
2578
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02002579/* if (iwl4965_queue_space(q) > q->low_mark && (txq_id >= 0) &&
Zhu Yib481de92007-09-25 17:54:57 -07002580 (txq_id != IWL_CMD_QUEUE_NUM) &&
2581 priv->mac80211_registered)
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02002582 ieee80211_wake_queue(priv->hw, txq_id); */
Zhu Yib481de92007-09-25 17:54:57 -07002583
2584
2585 return nfreed;
2586}
2587
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002588static int iwl4965_is_tx_success(u32 status)
Zhu Yib481de92007-09-25 17:54:57 -07002589{
2590 status &= TX_STATUS_MSK;
2591 return (status == TX_STATUS_SUCCESS)
2592 || (status == TX_STATUS_DIRECT_DONE);
2593}
2594
2595/******************************************************************************
2596 *
2597 * Generic RX handler implementations
2598 *
2599 ******************************************************************************/
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08002600#ifdef CONFIG_IWL4965_HT
Zhu Yib481de92007-09-25 17:54:57 -07002601
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002602static inline int iwl4965_get_ra_sta_id(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07002603 struct ieee80211_hdr *hdr)
2604{
2605 if (priv->iw_mode == IEEE80211_IF_TYPE_STA)
2606 return IWL_AP_ID;
2607 else {
2608 u8 *da = ieee80211_get_DA(hdr);
Tomas Winkler947b13a2008-04-16 16:34:48 -07002609 return iwl_find_station(priv, da);
Zhu Yib481de92007-09-25 17:54:57 -07002610 }
2611}
2612
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002613static struct ieee80211_hdr *iwl4965_tx_queue_get_hdr(
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002614 struct iwl_priv *priv, int txq_id, int idx)
Zhu Yib481de92007-09-25 17:54:57 -07002615{
2616 if (priv->txq[txq_id].txb[idx].skb[0])
2617 return (struct ieee80211_hdr *)priv->txq[txq_id].
2618 txb[idx].skb[0]->data;
2619 return NULL;
2620}
2621
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002622static inline u32 iwl4965_get_scd_ssn(struct iwl4965_tx_resp *tx_resp)
Zhu Yib481de92007-09-25 17:54:57 -07002623{
2624 __le32 *scd_ssn = (__le32 *)((u32 *)&tx_resp->status +
2625 tx_resp->frame_count);
2626 return le32_to_cpu(*scd_ssn) & MAX_SN;
2627
2628}
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002629
2630/**
2631 * iwl4965_tx_status_reply_tx - Handle Tx rspnse for frames in aggregation queue
2632 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002633static int iwl4965_tx_status_reply_tx(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002634 struct iwl4965_ht_agg *agg,
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02002635 struct iwl4965_tx_resp_agg *tx_resp,
Zhu Yib481de92007-09-25 17:54:57 -07002636 u16 start_idx)
2637{
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02002638 u16 status;
2639 struct agg_tx_status *frame_status = &tx_resp->status;
Zhu Yib481de92007-09-25 17:54:57 -07002640 struct ieee80211_tx_status *tx_status = NULL;
2641 struct ieee80211_hdr *hdr = NULL;
2642 int i, sh;
2643 int txq_id, idx;
2644 u16 seq;
2645
2646 if (agg->wait_for_ba)
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002647 IWL_DEBUG_TX_REPLY("got tx response w/o block-ack\n");
Zhu Yib481de92007-09-25 17:54:57 -07002648
2649 agg->frame_count = tx_resp->frame_count;
2650 agg->start_idx = start_idx;
2651 agg->rate_n_flags = le32_to_cpu(tx_resp->rate_n_flags);
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02002652 agg->bitmap = 0;
Zhu Yib481de92007-09-25 17:54:57 -07002653
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002654 /* # frames attempted by Tx command */
Zhu Yib481de92007-09-25 17:54:57 -07002655 if (agg->frame_count == 1) {
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002656 /* Only one frame was attempted; no block-ack will arrive */
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02002657 status = le16_to_cpu(frame_status[0].status);
2658 seq = le16_to_cpu(frame_status[0].sequence);
2659 idx = SEQ_TO_INDEX(seq);
2660 txq_id = SEQ_TO_QUEUE(seq);
Zhu Yib481de92007-09-25 17:54:57 -07002661
Zhu Yib481de92007-09-25 17:54:57 -07002662 /* FIXME: code repetition */
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02002663 IWL_DEBUG_TX_REPLY("FrameCnt = %d, StartIdx=%d idx=%d\n",
2664 agg->frame_count, agg->start_idx, idx);
Zhu Yib481de92007-09-25 17:54:57 -07002665
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02002666 tx_status = &(priv->txq[txq_id].txb[idx].status);
Zhu Yib481de92007-09-25 17:54:57 -07002667 tx_status->retry_count = tx_resp->failure_frame;
2668 tx_status->queue_number = status & 0xff;
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02002669 tx_status->queue_length = tx_resp->failure_rts;
2670 tx_status->control.flags &= ~IEEE80211_TXCTL_AMPDU;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002671 tx_status->flags = iwl4965_is_tx_success(status)?
Zhu Yib481de92007-09-25 17:54:57 -07002672 IEEE80211_TX_STATUS_ACK : 0;
Ron Rindjunsky4c424e42008-03-04 18:09:27 -08002673 iwl4965_hwrate_to_tx_control(priv,
2674 le32_to_cpu(tx_resp->rate_n_flags),
2675 &tx_status->control);
Zhu Yib481de92007-09-25 17:54:57 -07002676 /* FIXME: code repetition end */
2677
2678 IWL_DEBUG_TX_REPLY("1 Frame 0x%x failure :%d\n",
2679 status & 0xff, tx_resp->failure_frame);
2680 IWL_DEBUG_TX_REPLY("Rate Info rate_n_flags=%x\n",
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002681 iwl4965_hw_get_rate_n_flags(tx_resp->rate_n_flags));
Zhu Yib481de92007-09-25 17:54:57 -07002682
2683 agg->wait_for_ba = 0;
2684 } else {
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002685 /* Two or more frames were attempted; expect block-ack */
Zhu Yib481de92007-09-25 17:54:57 -07002686 u64 bitmap = 0;
2687 int start = agg->start_idx;
2688
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002689 /* Construct bit-map of pending frames within Tx window */
Zhu Yib481de92007-09-25 17:54:57 -07002690 for (i = 0; i < agg->frame_count; i++) {
2691 u16 sc;
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02002692 status = le16_to_cpu(frame_status[i].status);
2693 seq = le16_to_cpu(frame_status[i].sequence);
Zhu Yib481de92007-09-25 17:54:57 -07002694 idx = SEQ_TO_INDEX(seq);
2695 txq_id = SEQ_TO_QUEUE(seq);
2696
2697 if (status & (AGG_TX_STATE_FEW_BYTES_MSK |
2698 AGG_TX_STATE_ABORT_MSK))
2699 continue;
2700
2701 IWL_DEBUG_TX_REPLY("FrameCnt = %d, txq_id=%d idx=%d\n",
2702 agg->frame_count, txq_id, idx);
2703
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002704 hdr = iwl4965_tx_queue_get_hdr(priv, txq_id, idx);
Zhu Yib481de92007-09-25 17:54:57 -07002705
2706 sc = le16_to_cpu(hdr->seq_ctrl);
2707 if (idx != (SEQ_TO_SN(sc) & 0xff)) {
2708 IWL_ERROR("BUG_ON idx doesn't match seq control"
2709 " idx=%d, seq_idx=%d, seq=%d\n",
2710 idx, SEQ_TO_SN(sc),
2711 hdr->seq_ctrl);
2712 return -1;
2713 }
2714
2715 IWL_DEBUG_TX_REPLY("AGG Frame i=%d idx %d seq=%d\n",
2716 i, idx, SEQ_TO_SN(sc));
2717
2718 sh = idx - start;
2719 if (sh > 64) {
2720 sh = (start - idx) + 0xff;
2721 bitmap = bitmap << sh;
2722 sh = 0;
2723 start = idx;
2724 } else if (sh < -64)
2725 sh = 0xff - (start - idx);
2726 else if (sh < 0) {
2727 sh = start - idx;
2728 start = idx;
2729 bitmap = bitmap << sh;
2730 sh = 0;
2731 }
2732 bitmap |= (1 << sh);
2733 IWL_DEBUG_TX_REPLY("start=%d bitmap=0x%x\n",
2734 start, (u32)(bitmap & 0xFFFFFFFF));
2735 }
2736
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02002737 agg->bitmap = bitmap;
Zhu Yib481de92007-09-25 17:54:57 -07002738 agg->start_idx = start;
2739 agg->rate_n_flags = le32_to_cpu(tx_resp->rate_n_flags);
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02002740 IWL_DEBUG_TX_REPLY("Frames %d start_idx=%d bitmap=0x%llx\n",
Zhu Yib481de92007-09-25 17:54:57 -07002741 agg->frame_count, agg->start_idx,
John W. Linville06501d22008-04-01 17:38:47 -04002742 (unsigned long long)agg->bitmap);
Zhu Yib481de92007-09-25 17:54:57 -07002743
2744 if (bitmap)
2745 agg->wait_for_ba = 1;
2746 }
2747 return 0;
2748}
2749#endif
Zhu Yib481de92007-09-25 17:54:57 -07002750
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002751/**
2752 * iwl4965_rx_reply_tx - Handle standard (non-aggregation) Tx response
2753 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002754static void iwl4965_rx_reply_tx(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002755 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07002756{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002757 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07002758 u16 sequence = le16_to_cpu(pkt->hdr.sequence);
2759 int txq_id = SEQ_TO_QUEUE(sequence);
2760 int index = SEQ_TO_INDEX(sequence);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002761 struct iwl4965_tx_queue *txq = &priv->txq[txq_id];
Zhu Yib481de92007-09-25 17:54:57 -07002762 struct ieee80211_tx_status *tx_status;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002763 struct iwl4965_tx_resp *tx_resp = (void *)&pkt->u.raw[0];
Zhu Yib481de92007-09-25 17:54:57 -07002764 u32 status = le32_to_cpu(tx_resp->status);
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08002765#ifdef CONFIG_IWL4965_HT
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02002766 int tid = MAX_TID_COUNT, sta_id = IWL_INVALID_STATION;
2767 struct ieee80211_hdr *hdr;
2768 __le16 *qc;
Zhu Yib481de92007-09-25 17:54:57 -07002769#endif
2770
2771 if ((index >= txq->q.n_bd) || (x2_queue_used(&txq->q, index) == 0)) {
2772 IWL_ERROR("Read index for DMA queue txq_id (%d) index %d "
2773 "is out of range [0-%d] %d %d\n", txq_id,
Tomas Winklerfc4b6852007-10-25 17:15:24 +08002774 index, txq->q.n_bd, txq->q.write_ptr,
2775 txq->q.read_ptr);
Zhu Yib481de92007-09-25 17:54:57 -07002776 return;
2777 }
2778
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08002779#ifdef CONFIG_IWL4965_HT
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02002780 hdr = iwl4965_tx_queue_get_hdr(priv, txq_id, index);
2781 qc = ieee80211_get_qos_ctrl(hdr);
Zhu Yib481de92007-09-25 17:54:57 -07002782
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02002783 if (qc)
Zhu Yib481de92007-09-25 17:54:57 -07002784 tid = le16_to_cpu(*qc) & 0xf;
2785
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02002786 sta_id = iwl4965_get_ra_sta_id(priv, hdr);
2787 if (txq->sched_retry && unlikely(sta_id == IWL_INVALID_STATION)) {
2788 IWL_ERROR("Station not known\n");
2789 return;
2790 }
2791
2792 if (txq->sched_retry) {
2793 const u32 scd_ssn = iwl4965_get_scd_ssn(tx_resp);
2794 struct iwl4965_ht_agg *agg = NULL;
2795
2796 if (!qc)
Zhu Yib481de92007-09-25 17:54:57 -07002797 return;
Zhu Yib481de92007-09-25 17:54:57 -07002798
2799 agg = &priv->stations[sta_id].tid[tid].agg;
2800
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02002801 iwl4965_tx_status_reply_tx(priv, agg,
2802 (struct iwl4965_tx_resp_agg *)tx_resp, index);
Zhu Yib481de92007-09-25 17:54:57 -07002803
2804 if ((tx_resp->frame_count == 1) &&
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002805 !iwl4965_is_tx_success(status)) {
Zhu Yib481de92007-09-25 17:54:57 -07002806 /* TODO: send BAR */
2807 }
2808
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02002809 if (txq->q.read_ptr != (scd_ssn & 0xff)) {
2810 int freed;
Tomas Winklerc54b6792008-03-06 17:36:53 -08002811 index = iwl_queue_dec_wrap(scd_ssn & 0xff, txq->q.n_bd);
Zhu Yib481de92007-09-25 17:54:57 -07002812 IWL_DEBUG_TX_REPLY("Retry scheduler reclaim scd_ssn "
2813 "%d index %d\n", scd_ssn , index);
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02002814 freed = iwl4965_tx_queue_reclaim(priv, txq_id, index);
2815 priv->stations[sta_id].tid[tid].tfds_in_queue -= freed;
2816
2817 if (iwl4965_queue_space(&txq->q) > txq->q.low_mark &&
2818 txq_id >= 0 && priv->mac80211_registered &&
2819 agg->state != IWL_EMPTYING_HW_QUEUE_DELBA)
2820 ieee80211_wake_queue(priv->hw, txq_id);
2821
2822 iwl4965_check_empty_hw_queue(priv, sta_id, tid, txq_id);
Zhu Yib481de92007-09-25 17:54:57 -07002823 }
2824 } else {
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08002825#endif /* CONFIG_IWL4965_HT */
Tomas Winklerfc4b6852007-10-25 17:15:24 +08002826 tx_status = &(txq->txb[txq->q.read_ptr].status);
Zhu Yib481de92007-09-25 17:54:57 -07002827
2828 tx_status->retry_count = tx_resp->failure_frame;
2829 tx_status->queue_number = status;
2830 tx_status->queue_length = tx_resp->bt_kill_count;
2831 tx_status->queue_length |= tx_resp->failure_rts;
Zhu Yib481de92007-09-25 17:54:57 -07002832 tx_status->flags =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002833 iwl4965_is_tx_success(status) ? IEEE80211_TX_STATUS_ACK : 0;
Ron Rindjunsky4c424e42008-03-04 18:09:27 -08002834 iwl4965_hwrate_to_tx_control(priv, le32_to_cpu(tx_resp->rate_n_flags),
2835 &tx_status->control);
Zhu Yib481de92007-09-25 17:54:57 -07002836
Zhu Yib481de92007-09-25 17:54:57 -07002837 IWL_DEBUG_TX("Tx queue %d Status %s (0x%08x) rate_n_flags 0x%x "
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002838 "retries %d\n", txq_id, iwl4965_get_tx_fail_reason(status),
Zhu Yib481de92007-09-25 17:54:57 -07002839 status, le32_to_cpu(tx_resp->rate_n_flags),
2840 tx_resp->failure_frame);
2841
2842 IWL_DEBUG_TX_REPLY("Tx queue reclaim %d\n", index);
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02002843 if (index != -1) {
2844 int freed = iwl4965_tx_queue_reclaim(priv, txq_id, index);
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08002845#ifdef CONFIG_IWL4965_HT
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02002846 if (tid != MAX_TID_COUNT)
2847 priv->stations[sta_id].tid[tid].tfds_in_queue -= freed;
2848 if (iwl4965_queue_space(&txq->q) > txq->q.low_mark &&
2849 (txq_id >= 0) &&
2850 priv->mac80211_registered)
2851 ieee80211_wake_queue(priv->hw, txq_id);
2852 if (tid != MAX_TID_COUNT)
2853 iwl4965_check_empty_hw_queue(priv, sta_id, tid, txq_id);
2854#endif
Zhu Yib481de92007-09-25 17:54:57 -07002855 }
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02002856#ifdef CONFIG_IWL4965_HT
2857 }
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08002858#endif /* CONFIG_IWL4965_HT */
Zhu Yib481de92007-09-25 17:54:57 -07002859
2860 if (iwl_check_bits(status, TX_ABORT_REQUIRED_MSK))
2861 IWL_ERROR("TODO: Implement Tx ABORT REQUIRED!!!\n");
2862}
2863
2864
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002865static void iwl4965_rx_reply_alive(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002866 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07002867{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002868 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
2869 struct iwl4965_alive_resp *palive;
Zhu Yib481de92007-09-25 17:54:57 -07002870 struct delayed_work *pwork;
2871
2872 palive = &pkt->u.alive_frame;
2873
2874 IWL_DEBUG_INFO("Alive ucode status 0x%08X revision "
2875 "0x%01X 0x%01X\n",
2876 palive->is_valid, palive->ver_type,
2877 palive->ver_subtype);
2878
2879 if (palive->ver_subtype == INITIALIZE_SUBTYPE) {
2880 IWL_DEBUG_INFO("Initialization Alive received.\n");
2881 memcpy(&priv->card_alive_init,
2882 &pkt->u.alive_frame,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002883 sizeof(struct iwl4965_init_alive_resp));
Zhu Yib481de92007-09-25 17:54:57 -07002884 pwork = &priv->init_alive_start;
2885 } else {
2886 IWL_DEBUG_INFO("Runtime Alive received.\n");
2887 memcpy(&priv->card_alive, &pkt->u.alive_frame,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002888 sizeof(struct iwl4965_alive_resp));
Zhu Yib481de92007-09-25 17:54:57 -07002889 pwork = &priv->alive_start;
2890 }
2891
2892 /* We delay the ALIVE response by 5ms to
2893 * give the HW RF Kill time to activate... */
2894 if (palive->is_valid == UCODE_VALID_OK)
2895 queue_delayed_work(priv->workqueue, pwork,
2896 msecs_to_jiffies(5));
2897 else
2898 IWL_WARNING("uCode did not respond OK.\n");
2899}
2900
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002901static void iwl4965_rx_reply_add_sta(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002902 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07002903{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002904 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07002905
2906 IWL_DEBUG_RX("Received REPLY_ADD_STA: 0x%02X\n", pkt->u.status);
2907 return;
2908}
2909
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002910static void iwl4965_rx_reply_error(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002911 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07002912{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002913 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07002914
2915 IWL_ERROR("Error Reply type 0x%08X cmd %s (0x%02X) "
2916 "seq 0x%04X ser 0x%08X\n",
2917 le32_to_cpu(pkt->u.err_resp.error_type),
2918 get_cmd_string(pkt->u.err_resp.cmd_id),
2919 pkt->u.err_resp.cmd_id,
2920 le16_to_cpu(pkt->u.err_resp.bad_cmd_seq_num),
2921 le32_to_cpu(pkt->u.err_resp.error_info));
2922}
2923
2924#define TX_STATUS_ENTRY(x) case TX_STATUS_FAIL_ ## x: return #x
2925
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002926static void iwl4965_rx_csa(struct iwl_priv *priv, struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07002927{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002928 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
2929 struct iwl4965_rxon_cmd *rxon = (void *)&priv->active_rxon;
2930 struct iwl4965_csa_notification *csa = &(pkt->u.csa_notif);
Zhu Yib481de92007-09-25 17:54:57 -07002931 IWL_DEBUG_11H("CSA notif: channel %d, status %d\n",
2932 le16_to_cpu(csa->channel), le32_to_cpu(csa->status));
2933 rxon->channel = csa->channel;
2934 priv->staging_rxon.channel = csa->channel;
2935}
2936
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002937static void iwl4965_rx_spectrum_measure_notif(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002938 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07002939{
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08002940#ifdef CONFIG_IWL4965_SPECTRUM_MEASUREMENT
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002941 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
2942 struct iwl4965_spectrum_notification *report = &(pkt->u.spectrum_notif);
Zhu Yib481de92007-09-25 17:54:57 -07002943
2944 if (!report->state) {
2945 IWL_DEBUG(IWL_DL_11H | IWL_DL_INFO,
2946 "Spectrum Measure Notification: Start\n");
2947 return;
2948 }
2949
2950 memcpy(&priv->measure_report, report, sizeof(*report));
2951 priv->measurement_status |= MEASUREMENT_READY;
2952#endif
2953}
2954
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002955static void iwl4965_rx_pm_sleep_notif(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002956 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07002957{
Tomas Winkler0a6857e2008-03-12 16:58:49 -07002958#ifdef CONFIG_IWLWIFI_DEBUG
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002959 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
2960 struct iwl4965_sleep_notification *sleep = &(pkt->u.sleep_notif);
Zhu Yib481de92007-09-25 17:54:57 -07002961 IWL_DEBUG_RX("sleep mode: %d, src: %d\n",
2962 sleep->pm_sleep_mode, sleep->pm_wakeup_src);
2963#endif
2964}
2965
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002966static void iwl4965_rx_pm_debug_statistics_notif(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002967 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07002968{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002969 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07002970 IWL_DEBUG_RADIO("Dumping %d bytes of unhandled "
2971 "notification for %s:\n",
2972 le32_to_cpu(pkt->len), get_cmd_string(pkt->hdr.cmd));
Tomas Winkler0a6857e2008-03-12 16:58:49 -07002973 iwl_print_hex_dump(IWL_DL_RADIO, pkt->u.raw, le32_to_cpu(pkt->len));
Zhu Yib481de92007-09-25 17:54:57 -07002974}
2975
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002976static void iwl4965_bg_beacon_update(struct work_struct *work)
Zhu Yib481de92007-09-25 17:54:57 -07002977{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002978 struct iwl_priv *priv =
2979 container_of(work, struct iwl_priv, beacon_update);
Zhu Yib481de92007-09-25 17:54:57 -07002980 struct sk_buff *beacon;
2981
2982 /* Pull updated AP beacon from mac80211. will fail if not in AP mode */
Johannes Berg32bfd352007-12-19 01:31:26 +01002983 beacon = ieee80211_beacon_get(priv->hw, priv->vif, NULL);
Zhu Yib481de92007-09-25 17:54:57 -07002984
2985 if (!beacon) {
2986 IWL_ERROR("update beacon failed\n");
2987 return;
2988 }
2989
2990 mutex_lock(&priv->mutex);
2991 /* new beacon skb is allocated every time; dispose previous.*/
2992 if (priv->ibss_beacon)
2993 dev_kfree_skb(priv->ibss_beacon);
2994
2995 priv->ibss_beacon = beacon;
2996 mutex_unlock(&priv->mutex);
2997
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002998 iwl4965_send_beacon_cmd(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002999}
3000
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003001static void iwl4965_rx_beacon_notif(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003002 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003003{
Tomas Winkler0a6857e2008-03-12 16:58:49 -07003004#ifdef CONFIG_IWLWIFI_DEBUG
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003005 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
3006 struct iwl4965_beacon_notif *beacon = &(pkt->u.beacon_status);
3007 u8 rate = iwl4965_hw_get_rate(beacon->beacon_notify_hdr.rate_n_flags);
Zhu Yib481de92007-09-25 17:54:57 -07003008
3009 IWL_DEBUG_RX("beacon status %x retries %d iss %d "
3010 "tsf %d %d rate %d\n",
3011 le32_to_cpu(beacon->beacon_notify_hdr.status) & TX_STATUS_MSK,
3012 beacon->beacon_notify_hdr.failure_frame,
3013 le32_to_cpu(beacon->ibss_mgr_status),
3014 le32_to_cpu(beacon->high_tsf),
3015 le32_to_cpu(beacon->low_tsf), rate);
3016#endif
3017
3018 if ((priv->iw_mode == IEEE80211_IF_TYPE_AP) &&
3019 (!test_bit(STATUS_EXIT_PENDING, &priv->status)))
3020 queue_work(priv->workqueue, &priv->beacon_update);
3021}
3022
3023/* Service response to REPLY_SCAN_CMD (0x80) */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003024static void iwl4965_rx_reply_scan(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003025 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003026{
Tomas Winkler0a6857e2008-03-12 16:58:49 -07003027#ifdef CONFIG_IWLWIFI_DEBUG
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003028 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
3029 struct iwl4965_scanreq_notification *notif =
3030 (struct iwl4965_scanreq_notification *)pkt->u.raw;
Zhu Yib481de92007-09-25 17:54:57 -07003031
3032 IWL_DEBUG_RX("Scan request status = 0x%x\n", notif->status);
3033#endif
3034}
3035
3036/* Service SCAN_START_NOTIFICATION (0x82) */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003037static void iwl4965_rx_scan_start_notif(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003038 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003039{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003040 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
3041 struct iwl4965_scanstart_notification *notif =
3042 (struct iwl4965_scanstart_notification *)pkt->u.raw;
Zhu Yib481de92007-09-25 17:54:57 -07003043 priv->scan_start_tsf = le32_to_cpu(notif->tsf_low);
3044 IWL_DEBUG_SCAN("Scan start: "
3045 "%d [802.11%s] "
3046 "(TSF: 0x%08X:%08X) - %d (beacon timer %u)\n",
3047 notif->channel,
3048 notif->band ? "bg" : "a",
3049 notif->tsf_high,
3050 notif->tsf_low, notif->status, notif->beacon_timer);
3051}
3052
3053/* Service SCAN_RESULTS_NOTIFICATION (0x83) */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003054static void iwl4965_rx_scan_results_notif(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003055 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003056{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003057 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
3058 struct iwl4965_scanresults_notification *notif =
3059 (struct iwl4965_scanresults_notification *)pkt->u.raw;
Zhu Yib481de92007-09-25 17:54:57 -07003060
3061 IWL_DEBUG_SCAN("Scan ch.res: "
3062 "%d [802.11%s] "
3063 "(TSF: 0x%08X:%08X) - %d "
3064 "elapsed=%lu usec (%dms since last)\n",
3065 notif->channel,
3066 notif->band ? "bg" : "a",
3067 le32_to_cpu(notif->tsf_high),
3068 le32_to_cpu(notif->tsf_low),
3069 le32_to_cpu(notif->statistics[0]),
3070 le32_to_cpu(notif->tsf_low) - priv->scan_start_tsf,
3071 jiffies_to_msecs(elapsed_jiffies
3072 (priv->last_scan_jiffies, jiffies)));
3073
3074 priv->last_scan_jiffies = jiffies;
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08003075 priv->next_scan_jiffies = 0;
Zhu Yib481de92007-09-25 17:54:57 -07003076}
3077
3078/* Service SCAN_COMPLETE_NOTIFICATION (0x84) */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003079static void iwl4965_rx_scan_complete_notif(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003080 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003081{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003082 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
3083 struct iwl4965_scancomplete_notification *scan_notif = (void *)pkt->u.raw;
Zhu Yib481de92007-09-25 17:54:57 -07003084
3085 IWL_DEBUG_SCAN("Scan complete: %d channels (TSF 0x%08X:%08X) - %d\n",
3086 scan_notif->scanned_channels,
3087 scan_notif->tsf_low,
3088 scan_notif->tsf_high, scan_notif->status);
3089
3090 /* The HW is no longer scanning */
3091 clear_bit(STATUS_SCAN_HW, &priv->status);
3092
3093 /* The scan completion notification came in, so kill that timer... */
3094 cancel_delayed_work(&priv->scan_check);
3095
3096 IWL_DEBUG_INFO("Scan pass on %sGHz took %dms\n",
3097 (priv->scan_bands == 2) ? "2.4" : "5.2",
3098 jiffies_to_msecs(elapsed_jiffies
3099 (priv->scan_pass_start, jiffies)));
3100
3101 /* Remove this scanned band from the list
3102 * of pending bands to scan */
3103 priv->scan_bands--;
3104
3105 /* If a request to abort was given, or the scan did not succeed
3106 * then we reset the scan state machine and terminate,
3107 * re-queuing another scan if one has been requested */
3108 if (test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
3109 IWL_DEBUG_INFO("Aborted scan completed.\n");
3110 clear_bit(STATUS_SCAN_ABORTING, &priv->status);
3111 } else {
3112 /* If there are more bands on this scan pass reschedule */
3113 if (priv->scan_bands > 0)
3114 goto reschedule;
3115 }
3116
3117 priv->last_scan_jiffies = jiffies;
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08003118 priv->next_scan_jiffies = 0;
Zhu Yib481de92007-09-25 17:54:57 -07003119 IWL_DEBUG_INFO("Setting scan to off\n");
3120
3121 clear_bit(STATUS_SCANNING, &priv->status);
3122
3123 IWL_DEBUG_INFO("Scan took %dms\n",
3124 jiffies_to_msecs(elapsed_jiffies(priv->scan_start, jiffies)));
3125
3126 queue_work(priv->workqueue, &priv->scan_completed);
3127
3128 return;
3129
3130reschedule:
3131 priv->scan_pass_start = jiffies;
3132 queue_work(priv->workqueue, &priv->request_scan);
3133}
3134
3135/* Handle notification from uCode that card's power state is changing
3136 * due to software, hardware, or critical temperature RFKILL */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003137static void iwl4965_rx_card_state_notif(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003138 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003139{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003140 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07003141 u32 flags = le32_to_cpu(pkt->u.card_state_notif.flags);
3142 unsigned long status = priv->status;
3143
3144 IWL_DEBUG_RF_KILL("Card state received: HW:%s SW:%s\n",
3145 (flags & HW_CARD_DISABLED) ? "Kill" : "On",
3146 (flags & SW_CARD_DISABLED) ? "Kill" : "On");
3147
3148 if (flags & (SW_CARD_DISABLED | HW_CARD_DISABLED |
3149 RF_CARD_DISABLED)) {
3150
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003151 iwl_write32(priv, CSR_UCODE_DRV_GP1_SET,
Zhu Yib481de92007-09-25 17:54:57 -07003152 CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
3153
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003154 if (!iwl_grab_nic_access(priv)) {
3155 iwl_write_direct32(
Zhu Yib481de92007-09-25 17:54:57 -07003156 priv, HBUS_TARG_MBX_C,
3157 HBUS_TARG_MBX_C_REG_BIT_CMD_BLOCKED);
3158
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003159 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003160 }
3161
3162 if (!(flags & RXON_CARD_DISABLED)) {
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003163 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR,
Zhu Yib481de92007-09-25 17:54:57 -07003164 CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003165 if (!iwl_grab_nic_access(priv)) {
3166 iwl_write_direct32(
Zhu Yib481de92007-09-25 17:54:57 -07003167 priv, HBUS_TARG_MBX_C,
3168 HBUS_TARG_MBX_C_REG_BIT_CMD_BLOCKED);
3169
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003170 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003171 }
3172 }
3173
3174 if (flags & RF_CARD_DISABLED) {
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003175 iwl_write32(priv, CSR_UCODE_DRV_GP1_SET,
Zhu Yib481de92007-09-25 17:54:57 -07003176 CSR_UCODE_DRV_GP1_REG_BIT_CT_KILL_EXIT);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003177 iwl_read32(priv, CSR_UCODE_DRV_GP1);
3178 if (!iwl_grab_nic_access(priv))
3179 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003180 }
3181 }
3182
3183 if (flags & HW_CARD_DISABLED)
3184 set_bit(STATUS_RF_KILL_HW, &priv->status);
3185 else
3186 clear_bit(STATUS_RF_KILL_HW, &priv->status);
3187
3188
3189 if (flags & SW_CARD_DISABLED)
3190 set_bit(STATUS_RF_KILL_SW, &priv->status);
3191 else
3192 clear_bit(STATUS_RF_KILL_SW, &priv->status);
3193
3194 if (!(flags & RXON_CARD_DISABLED))
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003195 iwl4965_scan_cancel(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003196
3197 if ((test_bit(STATUS_RF_KILL_HW, &status) !=
3198 test_bit(STATUS_RF_KILL_HW, &priv->status)) ||
3199 (test_bit(STATUS_RF_KILL_SW, &status) !=
3200 test_bit(STATUS_RF_KILL_SW, &priv->status)))
3201 queue_work(priv->workqueue, &priv->rf_kill);
3202 else
3203 wake_up_interruptible(&priv->wait_command_queue);
3204}
3205
3206/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003207 * iwl4965_setup_rx_handlers - Initialize Rx handler callbacks
Zhu Yib481de92007-09-25 17:54:57 -07003208 *
3209 * Setup the RX handlers for each of the reply types sent from the uCode
3210 * to the host.
3211 *
3212 * This function chains into the hardware specific files for them to setup
3213 * any hardware specific handlers as well.
3214 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003215static void iwl4965_setup_rx_handlers(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003216{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003217 priv->rx_handlers[REPLY_ALIVE] = iwl4965_rx_reply_alive;
3218 priv->rx_handlers[REPLY_ADD_STA] = iwl4965_rx_reply_add_sta;
3219 priv->rx_handlers[REPLY_ERROR] = iwl4965_rx_reply_error;
3220 priv->rx_handlers[CHANNEL_SWITCH_NOTIFICATION] = iwl4965_rx_csa;
Zhu Yib481de92007-09-25 17:54:57 -07003221 priv->rx_handlers[SPECTRUM_MEASURE_NOTIFICATION] =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003222 iwl4965_rx_spectrum_measure_notif;
3223 priv->rx_handlers[PM_SLEEP_NOTIFICATION] = iwl4965_rx_pm_sleep_notif;
Zhu Yib481de92007-09-25 17:54:57 -07003224 priv->rx_handlers[PM_DEBUG_STATISTIC_NOTIFIC] =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003225 iwl4965_rx_pm_debug_statistics_notif;
3226 priv->rx_handlers[BEACON_NOTIFICATION] = iwl4965_rx_beacon_notif;
Zhu Yib481de92007-09-25 17:54:57 -07003227
Ben Cahill9fbab512007-11-29 11:09:47 +08003228 /*
3229 * The same handler is used for both the REPLY to a discrete
3230 * statistics request from the host as well as for the periodic
3231 * statistics notifications (after received beacons) from the uCode.
Zhu Yib481de92007-09-25 17:54:57 -07003232 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003233 priv->rx_handlers[REPLY_STATISTICS_CMD] = iwl4965_hw_rx_statistics;
3234 priv->rx_handlers[STATISTICS_NOTIFICATION] = iwl4965_hw_rx_statistics;
Zhu Yib481de92007-09-25 17:54:57 -07003235
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003236 priv->rx_handlers[REPLY_SCAN_CMD] = iwl4965_rx_reply_scan;
3237 priv->rx_handlers[SCAN_START_NOTIFICATION] = iwl4965_rx_scan_start_notif;
Zhu Yib481de92007-09-25 17:54:57 -07003238 priv->rx_handlers[SCAN_RESULTS_NOTIFICATION] =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003239 iwl4965_rx_scan_results_notif;
Zhu Yib481de92007-09-25 17:54:57 -07003240 priv->rx_handlers[SCAN_COMPLETE_NOTIFICATION] =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003241 iwl4965_rx_scan_complete_notif;
3242 priv->rx_handlers[CARD_STATE_NOTIFICATION] = iwl4965_rx_card_state_notif;
3243 priv->rx_handlers[REPLY_TX] = iwl4965_rx_reply_tx;
Zhu Yib481de92007-09-25 17:54:57 -07003244
Ben Cahill9fbab512007-11-29 11:09:47 +08003245 /* Set up hardware specific Rx handlers */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003246 iwl4965_hw_rx_handler_setup(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003247}
3248
3249/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003250 * iwl4965_tx_cmd_complete - Pull unused buffers off the queue and reclaim them
Zhu Yib481de92007-09-25 17:54:57 -07003251 * @rxb: Rx buffer to reclaim
3252 *
3253 * If an Rx buffer has an async callback associated with it the callback
3254 * will be executed. The attached skb (if present) will only be freed
3255 * if the callback returns 1
3256 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003257static void iwl4965_tx_cmd_complete(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003258 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003259{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003260 struct iwl4965_rx_packet *pkt = (struct iwl4965_rx_packet *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07003261 u16 sequence = le16_to_cpu(pkt->hdr.sequence);
3262 int txq_id = SEQ_TO_QUEUE(sequence);
3263 int index = SEQ_TO_INDEX(sequence);
3264 int huge = sequence & SEQ_HUGE_FRAME;
3265 int cmd_index;
Tomas Winkler857485c2008-03-21 13:53:44 -07003266 struct iwl_cmd *cmd;
Zhu Yib481de92007-09-25 17:54:57 -07003267
3268 /* If a Tx command is being handled and it isn't in the actual
3269 * command queue then there a command routing bug has been introduced
3270 * in the queue management code. */
3271 if (txq_id != IWL_CMD_QUEUE_NUM)
3272 IWL_ERROR("Error wrong command queue %d command id 0x%X\n",
3273 txq_id, pkt->hdr.cmd);
3274 BUG_ON(txq_id != IWL_CMD_QUEUE_NUM);
3275
3276 cmd_index = get_cmd_index(&priv->txq[IWL_CMD_QUEUE_NUM].q, index, huge);
3277 cmd = &priv->txq[IWL_CMD_QUEUE_NUM].cmd[cmd_index];
3278
3279 /* Input error checking is done when commands are added to queue. */
3280 if (cmd->meta.flags & CMD_WANT_SKB) {
3281 cmd->meta.source->u.skb = rxb->skb;
3282 rxb->skb = NULL;
3283 } else if (cmd->meta.u.callback &&
3284 !cmd->meta.u.callback(priv, cmd, rxb->skb))
3285 rxb->skb = NULL;
3286
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003287 iwl4965_tx_queue_reclaim(priv, txq_id, index);
Zhu Yib481de92007-09-25 17:54:57 -07003288
3289 if (!(cmd->meta.flags & CMD_ASYNC)) {
3290 clear_bit(STATUS_HCMD_ACTIVE, &priv->status);
3291 wake_up_interruptible(&priv->wait_command_queue);
3292 }
3293}
3294
3295/************************** RX-FUNCTIONS ****************************/
3296/*
3297 * Rx theory of operation
3298 *
Ben Cahill9fbab512007-11-29 11:09:47 +08003299 * Driver allocates a circular buffer of Receive Buffer Descriptors (RBDs),
3300 * each of which point to Receive Buffers to be filled by 4965. These get
3301 * used not only for Rx frames, but for any command response or notification
3302 * from the 4965. The driver and 4965 manage the Rx buffers by means
3303 * of indexes into the circular buffer.
Zhu Yib481de92007-09-25 17:54:57 -07003304 *
3305 * Rx Queue Indexes
3306 * The host/firmware share two index registers for managing the Rx buffers.
3307 *
3308 * The READ index maps to the first position that the firmware may be writing
3309 * to -- the driver can read up to (but not including) this position and get
3310 * good data.
3311 * The READ index is managed by the firmware once the card is enabled.
3312 *
3313 * The WRITE index maps to the last position the driver has read from -- the
3314 * position preceding WRITE is the last slot the firmware can place a packet.
3315 *
3316 * The queue is empty (no good data) if WRITE = READ - 1, and is full if
3317 * WRITE = READ.
3318 *
Ben Cahill9fbab512007-11-29 11:09:47 +08003319 * During initialization, the host sets up the READ queue position to the first
Zhu Yib481de92007-09-25 17:54:57 -07003320 * INDEX position, and WRITE to the last (READ - 1 wrapped)
3321 *
Ben Cahill9fbab512007-11-29 11:09:47 +08003322 * When the firmware places a packet in a buffer, it will advance the READ index
Zhu Yib481de92007-09-25 17:54:57 -07003323 * and fire the RX interrupt. The driver can then query the READ index and
3324 * process as many packets as possible, moving the WRITE index forward as it
3325 * resets the Rx queue buffers with new memory.
3326 *
3327 * The management in the driver is as follows:
3328 * + A list of pre-allocated SKBs is stored in iwl->rxq->rx_free. When
3329 * iwl->rxq->free_count drops to or below RX_LOW_WATERMARK, work is scheduled
Ian Schram01ebd062007-10-25 17:15:22 +08003330 * to replenish the iwl->rxq->rx_free.
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003331 * + In iwl4965_rx_replenish (scheduled) if 'processed' != 'read' then the
Zhu Yib481de92007-09-25 17:54:57 -07003332 * iwl->rxq is replenished and the READ INDEX is updated (updating the
3333 * 'processed' and 'read' driver indexes as well)
3334 * + A received packet is processed and handed to the kernel network stack,
3335 * detached from the iwl->rxq. The driver 'processed' index is updated.
3336 * + The Host/Firmware iwl->rxq is replenished at tasklet time from the rx_free
3337 * list. If there are no allocated buffers in iwl->rxq->rx_free, the READ
3338 * INDEX is not incremented and iwl->status(RX_STALLED) is set. If there
3339 * were enough free buffers and RX_STALLED is set it is cleared.
3340 *
3341 *
3342 * Driver sequence:
3343 *
Ben Cahill9fbab512007-11-29 11:09:47 +08003344 * iwl4965_rx_queue_alloc() Allocates rx_free
3345 * iwl4965_rx_replenish() Replenishes rx_free list from rx_used, and calls
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003346 * iwl4965_rx_queue_restock
Ben Cahill9fbab512007-11-29 11:09:47 +08003347 * iwl4965_rx_queue_restock() Moves available buffers from rx_free into Rx
Zhu Yib481de92007-09-25 17:54:57 -07003348 * queue, updates firmware pointers, and updates
3349 * the WRITE index. If insufficient rx_free buffers
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003350 * are available, schedules iwl4965_rx_replenish
Zhu Yib481de92007-09-25 17:54:57 -07003351 *
3352 * -- enable interrupts --
Ben Cahill9fbab512007-11-29 11:09:47 +08003353 * ISR - iwl4965_rx() Detach iwl4965_rx_mem_buffers from pool up to the
Zhu Yib481de92007-09-25 17:54:57 -07003354 * READ INDEX, detaching the SKB from the pool.
3355 * Moves the packet buffer from queue to rx_used.
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003356 * Calls iwl4965_rx_queue_restock to refill any empty
Zhu Yib481de92007-09-25 17:54:57 -07003357 * slots.
3358 * ...
3359 *
3360 */
3361
3362/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003363 * iwl4965_rx_queue_space - Return number of free slots available in queue.
Zhu Yib481de92007-09-25 17:54:57 -07003364 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003365static int iwl4965_rx_queue_space(const struct iwl4965_rx_queue *q)
Zhu Yib481de92007-09-25 17:54:57 -07003366{
3367 int s = q->read - q->write;
3368 if (s <= 0)
3369 s += RX_QUEUE_SIZE;
3370 /* keep some buffer to not confuse full and empty queue */
3371 s -= 2;
3372 if (s < 0)
3373 s = 0;
3374 return s;
3375}
3376
3377/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003378 * iwl4965_rx_queue_update_write_ptr - Update the write pointer for the RX queue
Zhu Yib481de92007-09-25 17:54:57 -07003379 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003380int iwl4965_rx_queue_update_write_ptr(struct iwl_priv *priv, struct iwl4965_rx_queue *q)
Zhu Yib481de92007-09-25 17:54:57 -07003381{
3382 u32 reg = 0;
3383 int rc = 0;
3384 unsigned long flags;
3385
3386 spin_lock_irqsave(&q->lock, flags);
3387
3388 if (q->need_update == 0)
3389 goto exit_unlock;
3390
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003391 /* If power-saving is in use, make sure device is awake */
Zhu Yib481de92007-09-25 17:54:57 -07003392 if (test_bit(STATUS_POWER_PMI, &priv->status)) {
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003393 reg = iwl_read32(priv, CSR_UCODE_DRV_GP1);
Zhu Yib481de92007-09-25 17:54:57 -07003394
3395 if (reg & CSR_UCODE_DRV_GP1_BIT_MAC_SLEEP) {
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003396 iwl_set_bit(priv, CSR_GP_CNTRL,
Zhu Yib481de92007-09-25 17:54:57 -07003397 CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
3398 goto exit_unlock;
3399 }
3400
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003401 rc = iwl_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003402 if (rc)
3403 goto exit_unlock;
3404
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003405 /* Device expects a multiple of 8 */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003406 iwl_write_direct32(priv, FH_RSCSR_CHNL0_WPTR,
Zhu Yib481de92007-09-25 17:54:57 -07003407 q->write & ~0x7);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003408 iwl_release_nic_access(priv);
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003409
3410 /* Else device is assumed to be awake */
Zhu Yib481de92007-09-25 17:54:57 -07003411 } else
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003412 /* Device expects a multiple of 8 */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003413 iwl_write32(priv, FH_RSCSR_CHNL0_WPTR, q->write & ~0x7);
Zhu Yib481de92007-09-25 17:54:57 -07003414
3415
3416 q->need_update = 0;
3417
3418 exit_unlock:
3419 spin_unlock_irqrestore(&q->lock, flags);
3420 return rc;
3421}
3422
3423/**
Ben Cahill9fbab512007-11-29 11:09:47 +08003424 * iwl4965_dma_addr2rbd_ptr - convert a DMA address to a uCode read buffer ptr
Zhu Yib481de92007-09-25 17:54:57 -07003425 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003426static inline __le32 iwl4965_dma_addr2rbd_ptr(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07003427 dma_addr_t dma_addr)
3428{
3429 return cpu_to_le32((u32)(dma_addr >> 8));
3430}
3431
3432
3433/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003434 * iwl4965_rx_queue_restock - refill RX queue from pre-allocated pool
Zhu Yib481de92007-09-25 17:54:57 -07003435 *
Ben Cahill9fbab512007-11-29 11:09:47 +08003436 * If there are slots in the RX queue that need to be restocked,
Zhu Yib481de92007-09-25 17:54:57 -07003437 * and we have free pre-allocated buffers, fill the ranks as much
Ben Cahill9fbab512007-11-29 11:09:47 +08003438 * as we can, pulling from rx_free.
Zhu Yib481de92007-09-25 17:54:57 -07003439 *
3440 * This moves the 'write' index forward to catch up with 'processed', and
3441 * also updates the memory address in the firmware to reference the new
3442 * target buffer.
3443 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003444static int iwl4965_rx_queue_restock(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003445{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003446 struct iwl4965_rx_queue *rxq = &priv->rxq;
Zhu Yib481de92007-09-25 17:54:57 -07003447 struct list_head *element;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003448 struct iwl4965_rx_mem_buffer *rxb;
Zhu Yib481de92007-09-25 17:54:57 -07003449 unsigned long flags;
3450 int write, rc;
3451
3452 spin_lock_irqsave(&rxq->lock, flags);
3453 write = rxq->write & ~0x7;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003454 while ((iwl4965_rx_queue_space(rxq) > 0) && (rxq->free_count)) {
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003455 /* Get next free Rx buffer, remove from free list */
Zhu Yib481de92007-09-25 17:54:57 -07003456 element = rxq->rx_free.next;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003457 rxb = list_entry(element, struct iwl4965_rx_mem_buffer, list);
Zhu Yib481de92007-09-25 17:54:57 -07003458 list_del(element);
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003459
3460 /* Point to Rx buffer via next RBD in circular buffer */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003461 rxq->bd[rxq->write] = iwl4965_dma_addr2rbd_ptr(priv, rxb->dma_addr);
Zhu Yib481de92007-09-25 17:54:57 -07003462 rxq->queue[rxq->write] = rxb;
3463 rxq->write = (rxq->write + 1) & RX_QUEUE_MASK;
3464 rxq->free_count--;
3465 }
3466 spin_unlock_irqrestore(&rxq->lock, flags);
3467 /* If the pre-allocated buffer pool is dropping low, schedule to
3468 * refill it */
3469 if (rxq->free_count <= RX_LOW_WATERMARK)
3470 queue_work(priv->workqueue, &priv->rx_replenish);
3471
3472
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003473 /* If we've added more space for the firmware to place data, tell it.
3474 * Increment device's write pointer in multiples of 8. */
Zhu Yib481de92007-09-25 17:54:57 -07003475 if ((write != (rxq->write & ~0x7))
3476 || (abs(rxq->write - rxq->read) > 7)) {
3477 spin_lock_irqsave(&rxq->lock, flags);
3478 rxq->need_update = 1;
3479 spin_unlock_irqrestore(&rxq->lock, flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003480 rc = iwl4965_rx_queue_update_write_ptr(priv, rxq);
Zhu Yib481de92007-09-25 17:54:57 -07003481 if (rc)
3482 return rc;
3483 }
3484
3485 return 0;
3486}
3487
3488/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003489 * iwl4965_rx_replenish - Move all used packet from rx_used to rx_free
Zhu Yib481de92007-09-25 17:54:57 -07003490 *
3491 * When moving to rx_free an SKB is allocated for the slot.
3492 *
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003493 * Also restock the Rx queue via iwl4965_rx_queue_restock.
Ian Schram01ebd062007-10-25 17:15:22 +08003494 * This is called as a scheduled work item (except for during initialization)
Zhu Yib481de92007-09-25 17:54:57 -07003495 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003496static void iwl4965_rx_allocate(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003497{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003498 struct iwl4965_rx_queue *rxq = &priv->rxq;
Zhu Yib481de92007-09-25 17:54:57 -07003499 struct list_head *element;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003500 struct iwl4965_rx_mem_buffer *rxb;
Zhu Yib481de92007-09-25 17:54:57 -07003501 unsigned long flags;
3502 spin_lock_irqsave(&rxq->lock, flags);
3503 while (!list_empty(&rxq->rx_used)) {
3504 element = rxq->rx_used.next;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003505 rxb = list_entry(element, struct iwl4965_rx_mem_buffer, list);
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003506
3507 /* Alloc a new receive buffer */
Zhu Yib481de92007-09-25 17:54:57 -07003508 rxb->skb =
Tomas Winkler5425e492008-04-15 16:01:38 -07003509 alloc_skb(priv->hw_params.rx_buf_size,
Ron Rindjunsky9ee1ba42007-11-26 16:14:42 +02003510 __GFP_NOWARN | GFP_ATOMIC);
Zhu Yib481de92007-09-25 17:54:57 -07003511 if (!rxb->skb) {
3512 if (net_ratelimit())
3513 printk(KERN_CRIT DRV_NAME
3514 ": Can not allocate SKB buffers\n");
3515 /* We don't reschedule replenish work here -- we will
3516 * call the restock method and if it still needs
3517 * more buffers it will schedule replenish */
3518 break;
3519 }
3520 priv->alloc_rxb_skb++;
3521 list_del(element);
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003522
3523 /* Get physical address of RB/SKB */
Zhu Yib481de92007-09-25 17:54:57 -07003524 rxb->dma_addr =
3525 pci_map_single(priv->pci_dev, rxb->skb->data,
Tomas Winkler5425e492008-04-15 16:01:38 -07003526 priv->hw_params.rx_buf_size, PCI_DMA_FROMDEVICE);
Zhu Yib481de92007-09-25 17:54:57 -07003527 list_add_tail(&rxb->list, &rxq->rx_free);
3528 rxq->free_count++;
3529 }
3530 spin_unlock_irqrestore(&rxq->lock, flags);
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08003531}
3532
3533/*
3534 * this should be called while priv->lock is locked
3535*/
Tomas Winkler4fd1f842007-12-05 20:59:58 +02003536static void __iwl4965_rx_replenish(void *data)
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08003537{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003538 struct iwl_priv *priv = data;
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08003539
3540 iwl4965_rx_allocate(priv);
3541 iwl4965_rx_queue_restock(priv);
3542}
3543
3544
3545void iwl4965_rx_replenish(void *data)
3546{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003547 struct iwl_priv *priv = data;
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08003548 unsigned long flags;
3549
3550 iwl4965_rx_allocate(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003551
3552 spin_lock_irqsave(&priv->lock, flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003553 iwl4965_rx_queue_restock(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003554 spin_unlock_irqrestore(&priv->lock, flags);
3555}
3556
3557/* Assumes that the skb field of the buffers in 'pool' is kept accurate.
Ben Cahill9fbab512007-11-29 11:09:47 +08003558 * If an SKB has been detached, the POOL needs to have its SKB set to NULL
Zhu Yib481de92007-09-25 17:54:57 -07003559 * This free routine walks the list of POOL entries and if SKB is set to
3560 * non NULL it is unmapped and freed
3561 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003562static void iwl4965_rx_queue_free(struct iwl_priv *priv, struct iwl4965_rx_queue *rxq)
Zhu Yib481de92007-09-25 17:54:57 -07003563{
3564 int i;
3565 for (i = 0; i < RX_QUEUE_SIZE + RX_FREE_BUFFERS; i++) {
3566 if (rxq->pool[i].skb != NULL) {
3567 pci_unmap_single(priv->pci_dev,
3568 rxq->pool[i].dma_addr,
Tomas Winkler5425e492008-04-15 16:01:38 -07003569 priv->hw_params.rx_buf_size,
Ron Rindjunsky9ee1ba42007-11-26 16:14:42 +02003570 PCI_DMA_FROMDEVICE);
Zhu Yib481de92007-09-25 17:54:57 -07003571 dev_kfree_skb(rxq->pool[i].skb);
3572 }
3573 }
3574
3575 pci_free_consistent(priv->pci_dev, 4 * RX_QUEUE_SIZE, rxq->bd,
3576 rxq->dma_addr);
3577 rxq->bd = NULL;
3578}
3579
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003580int iwl4965_rx_queue_alloc(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003581{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003582 struct iwl4965_rx_queue *rxq = &priv->rxq;
Zhu Yib481de92007-09-25 17:54:57 -07003583 struct pci_dev *dev = priv->pci_dev;
3584 int i;
3585
3586 spin_lock_init(&rxq->lock);
3587 INIT_LIST_HEAD(&rxq->rx_free);
3588 INIT_LIST_HEAD(&rxq->rx_used);
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003589
3590 /* Alloc the circular buffer of Read Buffer Descriptors (RBDs) */
Zhu Yib481de92007-09-25 17:54:57 -07003591 rxq->bd = pci_alloc_consistent(dev, 4 * RX_QUEUE_SIZE, &rxq->dma_addr);
3592 if (!rxq->bd)
3593 return -ENOMEM;
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003594
Zhu Yib481de92007-09-25 17:54:57 -07003595 /* Fill the rx_used queue with _all_ of the Rx buffers */
3596 for (i = 0; i < RX_FREE_BUFFERS + RX_QUEUE_SIZE; i++)
3597 list_add_tail(&rxq->pool[i].list, &rxq->rx_used);
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003598
Zhu Yib481de92007-09-25 17:54:57 -07003599 /* Set us so that we have processed and used all buffers, but have
3600 * not restocked the Rx queue with fresh buffers */
3601 rxq->read = rxq->write = 0;
3602 rxq->free_count = 0;
3603 rxq->need_update = 0;
3604 return 0;
3605}
3606
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003607void iwl4965_rx_queue_reset(struct iwl_priv *priv, struct iwl4965_rx_queue *rxq)
Zhu Yib481de92007-09-25 17:54:57 -07003608{
3609 unsigned long flags;
3610 int i;
3611 spin_lock_irqsave(&rxq->lock, flags);
3612 INIT_LIST_HEAD(&rxq->rx_free);
3613 INIT_LIST_HEAD(&rxq->rx_used);
3614 /* Fill the rx_used queue with _all_ of the Rx buffers */
3615 for (i = 0; i < RX_FREE_BUFFERS + RX_QUEUE_SIZE; i++) {
3616 /* In the reset function, these buffers may have been allocated
3617 * to an SKB, so we need to unmap and free potential storage */
3618 if (rxq->pool[i].skb != NULL) {
3619 pci_unmap_single(priv->pci_dev,
3620 rxq->pool[i].dma_addr,
Tomas Winkler5425e492008-04-15 16:01:38 -07003621 priv->hw_params.rx_buf_size,
Ron Rindjunsky9ee1ba42007-11-26 16:14:42 +02003622 PCI_DMA_FROMDEVICE);
Zhu Yib481de92007-09-25 17:54:57 -07003623 priv->alloc_rxb_skb--;
3624 dev_kfree_skb(rxq->pool[i].skb);
3625 rxq->pool[i].skb = NULL;
3626 }
3627 list_add_tail(&rxq->pool[i].list, &rxq->rx_used);
3628 }
3629
3630 /* Set us so that we have processed and used all buffers, but have
3631 * not restocked the Rx queue with fresh buffers */
3632 rxq->read = rxq->write = 0;
3633 rxq->free_count = 0;
3634 spin_unlock_irqrestore(&rxq->lock, flags);
3635}
3636
3637/* Convert linear signal-to-noise ratio into dB */
3638static u8 ratio2dB[100] = {
3639/* 0 1 2 3 4 5 6 7 8 9 */
3640 0, 0, 6, 10, 12, 14, 16, 17, 18, 19, /* 00 - 09 */
3641 20, 21, 22, 22, 23, 23, 24, 25, 26, 26, /* 10 - 19 */
3642 26, 26, 26, 27, 27, 28, 28, 28, 29, 29, /* 20 - 29 */
3643 29, 30, 30, 30, 31, 31, 31, 31, 32, 32, /* 30 - 39 */
3644 32, 32, 32, 33, 33, 33, 33, 33, 34, 34, /* 40 - 49 */
3645 34, 34, 34, 34, 35, 35, 35, 35, 35, 35, /* 50 - 59 */
3646 36, 36, 36, 36, 36, 36, 36, 37, 37, 37, /* 60 - 69 */
3647 37, 37, 37, 37, 37, 38, 38, 38, 38, 38, /* 70 - 79 */
3648 38, 38, 38, 38, 38, 39, 39, 39, 39, 39, /* 80 - 89 */
3649 39, 39, 39, 39, 39, 40, 40, 40, 40, 40 /* 90 - 99 */
3650};
3651
3652/* Calculates a relative dB value from a ratio of linear
3653 * (i.e. not dB) signal levels.
3654 * Conversion assumes that levels are voltages (20*log), not powers (10*log). */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003655int iwl4965_calc_db_from_ratio(int sig_ratio)
Zhu Yib481de92007-09-25 17:54:57 -07003656{
Adrian Bunkc899a572007-10-14 19:51:15 +02003657 /* 1000:1 or higher just report as 60 dB */
3658 if (sig_ratio >= 1000)
Zhu Yib481de92007-09-25 17:54:57 -07003659 return 60;
3660
Adrian Bunkc899a572007-10-14 19:51:15 +02003661 /* 100:1 or higher, divide by 10 and use table,
Zhu Yib481de92007-09-25 17:54:57 -07003662 * add 20 dB to make up for divide by 10 */
Adrian Bunkc899a572007-10-14 19:51:15 +02003663 if (sig_ratio >= 100)
Zhu Yib481de92007-09-25 17:54:57 -07003664 return (20 + (int)ratio2dB[sig_ratio/10]);
3665
3666 /* We shouldn't see this */
3667 if (sig_ratio < 1)
3668 return 0;
3669
3670 /* Use table for ratios 1:1 - 99:1 */
3671 return (int)ratio2dB[sig_ratio];
3672}
3673
3674#define PERFECT_RSSI (-20) /* dBm */
3675#define WORST_RSSI (-95) /* dBm */
3676#define RSSI_RANGE (PERFECT_RSSI - WORST_RSSI)
3677
3678/* Calculate an indication of rx signal quality (a percentage, not dBm!).
3679 * See http://www.ces.clemson.edu/linux/signal_quality.shtml for info
3680 * about formulas used below. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003681int iwl4965_calc_sig_qual(int rssi_dbm, int noise_dbm)
Zhu Yib481de92007-09-25 17:54:57 -07003682{
3683 int sig_qual;
3684 int degradation = PERFECT_RSSI - rssi_dbm;
3685
3686 /* If we get a noise measurement, use signal-to-noise ratio (SNR)
3687 * as indicator; formula is (signal dbm - noise dbm).
3688 * SNR at or above 40 is a great signal (100%).
3689 * Below that, scale to fit SNR of 0 - 40 dB within 0 - 100% indicator.
3690 * Weakest usable signal is usually 10 - 15 dB SNR. */
3691 if (noise_dbm) {
3692 if (rssi_dbm - noise_dbm >= 40)
3693 return 100;
3694 else if (rssi_dbm < noise_dbm)
3695 return 0;
3696 sig_qual = ((rssi_dbm - noise_dbm) * 5) / 2;
3697
3698 /* Else use just the signal level.
3699 * This formula is a least squares fit of data points collected and
3700 * compared with a reference system that had a percentage (%) display
3701 * for signal quality. */
3702 } else
3703 sig_qual = (100 * (RSSI_RANGE * RSSI_RANGE) - degradation *
3704 (15 * RSSI_RANGE + 62 * degradation)) /
3705 (RSSI_RANGE * RSSI_RANGE);
3706
3707 if (sig_qual > 100)
3708 sig_qual = 100;
3709 else if (sig_qual < 1)
3710 sig_qual = 0;
3711
3712 return sig_qual;
3713}
3714
3715/**
Ben Cahill9fbab512007-11-29 11:09:47 +08003716 * iwl4965_rx_handle - Main entry function for receiving responses from uCode
Zhu Yib481de92007-09-25 17:54:57 -07003717 *
3718 * Uses the priv->rx_handlers callback function array to invoke
3719 * the appropriate handlers, including command responses,
3720 * frame-received notifications, and other notifications.
3721 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003722static void iwl4965_rx_handle(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003723{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003724 struct iwl4965_rx_mem_buffer *rxb;
3725 struct iwl4965_rx_packet *pkt;
3726 struct iwl4965_rx_queue *rxq = &priv->rxq;
Zhu Yib481de92007-09-25 17:54:57 -07003727 u32 r, i;
3728 int reclaim;
3729 unsigned long flags;
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08003730 u8 fill_rx = 0;
Mohamed Abbasd68ab682008-02-07 13:16:33 -08003731 u32 count = 8;
Zhu Yib481de92007-09-25 17:54:57 -07003732
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003733 /* uCode's read index (stored in shared DRAM) indicates the last Rx
3734 * buffer that the driver may process (last buffer filled by ucode). */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003735 r = iwl4965_hw_get_rx_read(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003736 i = rxq->read;
3737
3738 /* Rx interrupt, but nothing sent from uCode */
3739 if (i == r)
3740 IWL_DEBUG(IWL_DL_RX | IWL_DL_ISR, "r = %d, i = %d\n", r, i);
3741
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08003742 if (iwl4965_rx_queue_space(rxq) > (RX_QUEUE_SIZE / 2))
3743 fill_rx = 1;
3744
Zhu Yib481de92007-09-25 17:54:57 -07003745 while (i != r) {
3746 rxb = rxq->queue[i];
3747
Ben Cahill9fbab512007-11-29 11:09:47 +08003748 /* If an RXB doesn't have a Rx queue slot associated with it,
Zhu Yib481de92007-09-25 17:54:57 -07003749 * then a bug has been introduced in the queue refilling
3750 * routines -- catch it here */
3751 BUG_ON(rxb == NULL);
3752
3753 rxq->queue[i] = NULL;
3754
3755 pci_dma_sync_single_for_cpu(priv->pci_dev, rxb->dma_addr,
Tomas Winkler5425e492008-04-15 16:01:38 -07003756 priv->hw_params.rx_buf_size,
Zhu Yib481de92007-09-25 17:54:57 -07003757 PCI_DMA_FROMDEVICE);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003758 pkt = (struct iwl4965_rx_packet *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07003759
3760 /* Reclaim a command buffer only if this packet is a response
3761 * to a (driver-originated) command.
3762 * If the packet (e.g. Rx frame) originated from uCode,
3763 * there is no command buffer to reclaim.
3764 * Ucode should set SEQ_RX_FRAME bit if ucode-originated,
3765 * but apparently a few don't get set; catch them here. */
3766 reclaim = !(pkt->hdr.sequence & SEQ_RX_FRAME) &&
3767 (pkt->hdr.cmd != REPLY_RX_PHY_CMD) &&
Tomas Winkler857485c2008-03-21 13:53:44 -07003768 (pkt->hdr.cmd != REPLY_RX) &&
Zhu Yicfe01702007-09-27 11:27:31 +08003769 (pkt->hdr.cmd != REPLY_COMPRESSED_BA) &&
Zhu Yib481de92007-09-25 17:54:57 -07003770 (pkt->hdr.cmd != STATISTICS_NOTIFICATION) &&
3771 (pkt->hdr.cmd != REPLY_TX);
3772
3773 /* Based on type of command response or notification,
3774 * handle those that need handling via function in
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003775 * rx_handlers table. See iwl4965_setup_rx_handlers() */
Zhu Yib481de92007-09-25 17:54:57 -07003776 if (priv->rx_handlers[pkt->hdr.cmd]) {
3777 IWL_DEBUG(IWL_DL_HOST_COMMAND | IWL_DL_RX | IWL_DL_ISR,
3778 "r = %d, i = %d, %s, 0x%02x\n", r, i,
3779 get_cmd_string(pkt->hdr.cmd), pkt->hdr.cmd);
3780 priv->rx_handlers[pkt->hdr.cmd] (priv, rxb);
3781 } else {
3782 /* No handling needed */
3783 IWL_DEBUG(IWL_DL_HOST_COMMAND | IWL_DL_RX | IWL_DL_ISR,
3784 "r %d i %d No handler needed for %s, 0x%02x\n",
3785 r, i, get_cmd_string(pkt->hdr.cmd),
3786 pkt->hdr.cmd);
3787 }
3788
3789 if (reclaim) {
Ben Cahill9fbab512007-11-29 11:09:47 +08003790 /* Invoke any callbacks, transfer the skb to caller, and
Tomas Winkler857485c2008-03-21 13:53:44 -07003791 * fire off the (possibly) blocking iwl_send_cmd()
Zhu Yib481de92007-09-25 17:54:57 -07003792 * as we reclaim the driver command queue */
3793 if (rxb && rxb->skb)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003794 iwl4965_tx_cmd_complete(priv, rxb);
Zhu Yib481de92007-09-25 17:54:57 -07003795 else
3796 IWL_WARNING("Claim null rxb?\n");
3797 }
3798
3799 /* For now we just don't re-use anything. We can tweak this
3800 * later to try and re-use notification packets and SKBs that
3801 * fail to Rx correctly */
3802 if (rxb->skb != NULL) {
3803 priv->alloc_rxb_skb--;
3804 dev_kfree_skb_any(rxb->skb);
3805 rxb->skb = NULL;
3806 }
3807
3808 pci_unmap_single(priv->pci_dev, rxb->dma_addr,
Tomas Winkler5425e492008-04-15 16:01:38 -07003809 priv->hw_params.rx_buf_size,
Ron Rindjunsky9ee1ba42007-11-26 16:14:42 +02003810 PCI_DMA_FROMDEVICE);
Zhu Yib481de92007-09-25 17:54:57 -07003811 spin_lock_irqsave(&rxq->lock, flags);
3812 list_add_tail(&rxb->list, &priv->rxq.rx_used);
3813 spin_unlock_irqrestore(&rxq->lock, flags);
3814 i = (i + 1) & RX_QUEUE_MASK;
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08003815 /* If there are a lot of unused frames,
3816 * restock the Rx queue so ucode wont assert. */
3817 if (fill_rx) {
3818 count++;
3819 if (count >= 8) {
3820 priv->rxq.read = i;
3821 __iwl4965_rx_replenish(priv);
3822 count = 0;
3823 }
3824 }
Zhu Yib481de92007-09-25 17:54:57 -07003825 }
3826
3827 /* Backtrack one entry */
3828 priv->rxq.read = i;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003829 iwl4965_rx_queue_restock(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003830}
3831
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003832/**
3833 * iwl4965_tx_queue_update_write_ptr - Send new write index to hardware
3834 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003835static int iwl4965_tx_queue_update_write_ptr(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003836 struct iwl4965_tx_queue *txq)
Zhu Yib481de92007-09-25 17:54:57 -07003837{
3838 u32 reg = 0;
3839 int rc = 0;
3840 int txq_id = txq->q.id;
3841
3842 if (txq->need_update == 0)
3843 return rc;
3844
3845 /* if we're trying to save power */
3846 if (test_bit(STATUS_POWER_PMI, &priv->status)) {
3847 /* wake up nic if it's powered down ...
3848 * uCode will wake up, and interrupt us again, so next
3849 * time we'll skip this part. */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003850 reg = iwl_read32(priv, CSR_UCODE_DRV_GP1);
Zhu Yib481de92007-09-25 17:54:57 -07003851
3852 if (reg & CSR_UCODE_DRV_GP1_BIT_MAC_SLEEP) {
3853 IWL_DEBUG_INFO("Requesting wakeup, GP1 = 0x%x\n", reg);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003854 iwl_set_bit(priv, CSR_GP_CNTRL,
Zhu Yib481de92007-09-25 17:54:57 -07003855 CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
3856 return rc;
3857 }
3858
3859 /* restore this queue's parameters in nic hardware. */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003860 rc = iwl_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003861 if (rc)
3862 return rc;
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003863 iwl_write_direct32(priv, HBUS_TARG_WRPTR,
Tomas Winklerfc4b6852007-10-25 17:15:24 +08003864 txq->q.write_ptr | (txq_id << 8));
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003865 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003866
3867 /* else not in power-save mode, uCode will never sleep when we're
3868 * trying to tx (during RFKILL, we're not trying to tx). */
3869 } else
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003870 iwl_write32(priv, HBUS_TARG_WRPTR,
Tomas Winklerfc4b6852007-10-25 17:15:24 +08003871 txq->q.write_ptr | (txq_id << 8));
Zhu Yib481de92007-09-25 17:54:57 -07003872
3873 txq->need_update = 0;
3874
3875 return rc;
3876}
3877
Tomas Winkler0a6857e2008-03-12 16:58:49 -07003878#ifdef CONFIG_IWLWIFI_DEBUG
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003879static void iwl4965_print_rx_config_cmd(struct iwl4965_rxon_cmd *rxon)
Zhu Yib481de92007-09-25 17:54:57 -07003880{
Joe Perches0795af52007-10-03 17:59:30 -07003881 DECLARE_MAC_BUF(mac);
3882
Zhu Yib481de92007-09-25 17:54:57 -07003883 IWL_DEBUG_RADIO("RX CONFIG:\n");
Tomas Winkler0a6857e2008-03-12 16:58:49 -07003884 iwl_print_hex_dump(IWL_DL_RADIO, (u8 *) rxon, sizeof(*rxon));
Zhu Yib481de92007-09-25 17:54:57 -07003885 IWL_DEBUG_RADIO("u16 channel: 0x%x\n", le16_to_cpu(rxon->channel));
3886 IWL_DEBUG_RADIO("u32 flags: 0x%08X\n", le32_to_cpu(rxon->flags));
3887 IWL_DEBUG_RADIO("u32 filter_flags: 0x%08x\n",
3888 le32_to_cpu(rxon->filter_flags));
3889 IWL_DEBUG_RADIO("u8 dev_type: 0x%x\n", rxon->dev_type);
3890 IWL_DEBUG_RADIO("u8 ofdm_basic_rates: 0x%02x\n",
3891 rxon->ofdm_basic_rates);
3892 IWL_DEBUG_RADIO("u8 cck_basic_rates: 0x%02x\n", rxon->cck_basic_rates);
Joe Perches0795af52007-10-03 17:59:30 -07003893 IWL_DEBUG_RADIO("u8[6] node_addr: %s\n",
3894 print_mac(mac, rxon->node_addr));
3895 IWL_DEBUG_RADIO("u8[6] bssid_addr: %s\n",
3896 print_mac(mac, rxon->bssid_addr));
Zhu Yib481de92007-09-25 17:54:57 -07003897 IWL_DEBUG_RADIO("u16 assoc_id: 0x%x\n", le16_to_cpu(rxon->assoc_id));
3898}
3899#endif
3900
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003901static void iwl4965_enable_interrupts(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003902{
3903 IWL_DEBUG_ISR("Enabling interrupts\n");
3904 set_bit(STATUS_INT_ENABLED, &priv->status);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003905 iwl_write32(priv, CSR_INT_MASK, CSR_INI_SET_MASK);
Zhu Yib481de92007-09-25 17:54:57 -07003906}
3907
Mohamed Abbas0359fac2008-03-28 16:21:08 -07003908/* call this function to flush any scheduled tasklet */
3909static inline void iwl_synchronize_irq(struct iwl_priv *priv)
3910{
3911 /* wait to make sure we flush pedding tasklet*/
3912 synchronize_irq(priv->pci_dev->irq);
3913 tasklet_kill(&priv->irq_tasklet);
3914}
3915
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003916static inline void iwl4965_disable_interrupts(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003917{
3918 clear_bit(STATUS_INT_ENABLED, &priv->status);
3919
3920 /* disable interrupts from uCode/NIC to host */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003921 iwl_write32(priv, CSR_INT_MASK, 0x00000000);
Zhu Yib481de92007-09-25 17:54:57 -07003922
3923 /* acknowledge/clear/reset any interrupts still pending
3924 * from uCode or flow handler (Rx/Tx DMA) */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003925 iwl_write32(priv, CSR_INT, 0xffffffff);
3926 iwl_write32(priv, CSR_FH_INT_STATUS, 0xffffffff);
Zhu Yib481de92007-09-25 17:54:57 -07003927 IWL_DEBUG_ISR("Disabled interrupts\n");
3928}
3929
3930static const char *desc_lookup(int i)
3931{
3932 switch (i) {
3933 case 1:
3934 return "FAIL";
3935 case 2:
3936 return "BAD_PARAM";
3937 case 3:
3938 return "BAD_CHECKSUM";
3939 case 4:
3940 return "NMI_INTERRUPT";
3941 case 5:
3942 return "SYSASSERT";
3943 case 6:
3944 return "FATAL_ERROR";
3945 }
3946
3947 return "UNKNOWN";
3948}
3949
3950#define ERROR_START_OFFSET (1 * sizeof(u32))
3951#define ERROR_ELEM_SIZE (7 * sizeof(u32))
3952
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003953static void iwl4965_dump_nic_error_log(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003954{
3955 u32 data2, line;
3956 u32 desc, time, count, base, data1;
3957 u32 blink1, blink2, ilink1, ilink2;
3958 int rc;
3959
3960 base = le32_to_cpu(priv->card_alive.error_event_table_ptr);
3961
Tomas Winkler57aab752008-04-14 21:16:03 -07003962 if (!priv->cfg->ops->lib->is_valid_rtc_data_addr(base)) {
Zhu Yib481de92007-09-25 17:54:57 -07003963 IWL_ERROR("Not valid error log pointer 0x%08X\n", base);
3964 return;
3965 }
3966
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003967 rc = iwl_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003968 if (rc) {
3969 IWL_WARNING("Can not read from adapter at this time.\n");
3970 return;
3971 }
3972
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003973 count = iwl_read_targ_mem(priv, base);
Zhu Yib481de92007-09-25 17:54:57 -07003974
3975 if (ERROR_START_OFFSET <= count * ERROR_ELEM_SIZE) {
3976 IWL_ERROR("Start IWL Error Log Dump:\n");
Tomas Winkler2acae162008-03-02 01:25:59 +02003977 IWL_ERROR("Status: 0x%08lX, count: %d\n", priv->status, count);
Zhu Yib481de92007-09-25 17:54:57 -07003978 }
3979
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003980 desc = iwl_read_targ_mem(priv, base + 1 * sizeof(u32));
3981 blink1 = iwl_read_targ_mem(priv, base + 3 * sizeof(u32));
3982 blink2 = iwl_read_targ_mem(priv, base + 4 * sizeof(u32));
3983 ilink1 = iwl_read_targ_mem(priv, base + 5 * sizeof(u32));
3984 ilink2 = iwl_read_targ_mem(priv, base + 6 * sizeof(u32));
3985 data1 = iwl_read_targ_mem(priv, base + 7 * sizeof(u32));
3986 data2 = iwl_read_targ_mem(priv, base + 8 * sizeof(u32));
3987 line = iwl_read_targ_mem(priv, base + 9 * sizeof(u32));
3988 time = iwl_read_targ_mem(priv, base + 11 * sizeof(u32));
Zhu Yib481de92007-09-25 17:54:57 -07003989
3990 IWL_ERROR("Desc Time "
3991 "data1 data2 line\n");
3992 IWL_ERROR("%-13s (#%d) %010u 0x%08X 0x%08X %u\n",
3993 desc_lookup(desc), desc, time, data1, data2, line);
3994 IWL_ERROR("blink1 blink2 ilink1 ilink2\n");
3995 IWL_ERROR("0x%05X 0x%05X 0x%05X 0x%05X\n", blink1, blink2,
3996 ilink1, ilink2);
3997
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003998 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003999}
4000
4001#define EVENT_START_OFFSET (4 * sizeof(u32))
4002
4003/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004004 * iwl4965_print_event_log - Dump error event log to syslog
Zhu Yib481de92007-09-25 17:54:57 -07004005 *
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004006 * NOTE: Must be called with iwl_grab_nic_access() already obtained!
Zhu Yib481de92007-09-25 17:54:57 -07004007 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004008static void iwl4965_print_event_log(struct iwl_priv *priv, u32 start_idx,
Zhu Yib481de92007-09-25 17:54:57 -07004009 u32 num_events, u32 mode)
4010{
4011 u32 i;
4012 u32 base; /* SRAM byte address of event log header */
4013 u32 event_size; /* 2 u32s, or 3 u32s if timestamp recorded */
4014 u32 ptr; /* SRAM byte address of log data */
4015 u32 ev, time, data; /* event log data */
4016
4017 if (num_events == 0)
4018 return;
4019
4020 base = le32_to_cpu(priv->card_alive.log_event_table_ptr);
4021
4022 if (mode == 0)
4023 event_size = 2 * sizeof(u32);
4024 else
4025 event_size = 3 * sizeof(u32);
4026
4027 ptr = base + EVENT_START_OFFSET + (start_idx * event_size);
4028
4029 /* "time" is actually "data" for mode 0 (no timestamp).
4030 * place event id # at far right for easier visual parsing. */
4031 for (i = 0; i < num_events; i++) {
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004032 ev = iwl_read_targ_mem(priv, ptr);
Zhu Yib481de92007-09-25 17:54:57 -07004033 ptr += sizeof(u32);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004034 time = iwl_read_targ_mem(priv, ptr);
Zhu Yib481de92007-09-25 17:54:57 -07004035 ptr += sizeof(u32);
4036 if (mode == 0)
4037 IWL_ERROR("0x%08x\t%04u\n", time, ev); /* data, ev */
4038 else {
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004039 data = iwl_read_targ_mem(priv, ptr);
Zhu Yib481de92007-09-25 17:54:57 -07004040 ptr += sizeof(u32);
4041 IWL_ERROR("%010u\t0x%08x\t%04u\n", time, data, ev);
4042 }
4043 }
4044}
4045
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004046static void iwl4965_dump_nic_event_log(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004047{
4048 int rc;
4049 u32 base; /* SRAM byte address of event log header */
4050 u32 capacity; /* event log capacity in # entries */
4051 u32 mode; /* 0 - no timestamp, 1 - timestamp recorded */
4052 u32 num_wraps; /* # times uCode wrapped to top of log */
4053 u32 next_entry; /* index of next entry to be written by uCode */
4054 u32 size; /* # entries that we'll print */
4055
4056 base = le32_to_cpu(priv->card_alive.log_event_table_ptr);
Tomas Winkler57aab752008-04-14 21:16:03 -07004057 if (!priv->cfg->ops->lib->is_valid_rtc_data_addr(base)) {
Zhu Yib481de92007-09-25 17:54:57 -07004058 IWL_ERROR("Invalid event log pointer 0x%08X\n", base);
4059 return;
4060 }
4061
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004062 rc = iwl_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004063 if (rc) {
4064 IWL_WARNING("Can not read from adapter at this time.\n");
4065 return;
4066 }
4067
4068 /* event log header */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004069 capacity = iwl_read_targ_mem(priv, base);
4070 mode = iwl_read_targ_mem(priv, base + (1 * sizeof(u32)));
4071 num_wraps = iwl_read_targ_mem(priv, base + (2 * sizeof(u32)));
4072 next_entry = iwl_read_targ_mem(priv, base + (3 * sizeof(u32)));
Zhu Yib481de92007-09-25 17:54:57 -07004073
4074 size = num_wraps ? capacity : next_entry;
4075
4076 /* bail out if nothing in log */
4077 if (size == 0) {
Zhu Yi583fab32007-09-27 11:27:30 +08004078 IWL_ERROR("Start IWL Event Log Dump: nothing in log\n");
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004079 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004080 return;
4081 }
4082
Zhu Yi583fab32007-09-27 11:27:30 +08004083 IWL_ERROR("Start IWL Event Log Dump: display count %d, wraps %d\n",
Zhu Yib481de92007-09-25 17:54:57 -07004084 size, num_wraps);
4085
4086 /* if uCode has wrapped back to top of log, start at the oldest entry,
4087 * i.e the next one that uCode would fill. */
4088 if (num_wraps)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004089 iwl4965_print_event_log(priv, next_entry,
Zhu Yib481de92007-09-25 17:54:57 -07004090 capacity - next_entry, mode);
4091
4092 /* (then/else) start at top of log */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004093 iwl4965_print_event_log(priv, 0, next_entry, mode);
Zhu Yib481de92007-09-25 17:54:57 -07004094
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004095 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004096}
4097
4098/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004099 * iwl4965_irq_handle_error - called for HW or SW error interrupt from card
Zhu Yib481de92007-09-25 17:54:57 -07004100 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004101static void iwl4965_irq_handle_error(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004102{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004103 /* Set the FW error flag -- cleared on iwl4965_down */
Zhu Yib481de92007-09-25 17:54:57 -07004104 set_bit(STATUS_FW_ERROR, &priv->status);
4105
4106 /* Cancel currently queued command. */
4107 clear_bit(STATUS_HCMD_ACTIVE, &priv->status);
4108
Tomas Winkler0a6857e2008-03-12 16:58:49 -07004109#ifdef CONFIG_IWLWIFI_DEBUG
4110 if (iwl_debug_level & IWL_DL_FW_ERRORS) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004111 iwl4965_dump_nic_error_log(priv);
4112 iwl4965_dump_nic_event_log(priv);
4113 iwl4965_print_rx_config_cmd(&priv->staging_rxon);
Zhu Yib481de92007-09-25 17:54:57 -07004114 }
4115#endif
4116
4117 wake_up_interruptible(&priv->wait_command_queue);
4118
4119 /* Keep the restart process from trying to send host
4120 * commands by clearing the INIT status bit */
4121 clear_bit(STATUS_READY, &priv->status);
4122
4123 if (!test_bit(STATUS_EXIT_PENDING, &priv->status)) {
4124 IWL_DEBUG(IWL_DL_INFO | IWL_DL_FW_ERRORS,
4125 "Restarting adapter due to uCode error.\n");
4126
Tomas Winkler3109ece2008-03-28 16:33:35 -07004127 if (iwl_is_associated(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07004128 memcpy(&priv->recovery_rxon, &priv->active_rxon,
4129 sizeof(priv->recovery_rxon));
4130 priv->error_recovering = 1;
4131 }
4132 queue_work(priv->workqueue, &priv->restart);
4133 }
4134}
4135
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004136static void iwl4965_error_recovery(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004137{
4138 unsigned long flags;
4139
4140 memcpy(&priv->staging_rxon, &priv->recovery_rxon,
4141 sizeof(priv->staging_rxon));
4142 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004143 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004144
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004145 iwl4965_rxon_add_station(priv, priv->bssid, 1);
Zhu Yib481de92007-09-25 17:54:57 -07004146
4147 spin_lock_irqsave(&priv->lock, flags);
4148 priv->assoc_id = le16_to_cpu(priv->staging_rxon.assoc_id);
4149 priv->error_recovering = 0;
4150 spin_unlock_irqrestore(&priv->lock, flags);
4151}
4152
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004153static void iwl4965_irq_tasklet(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004154{
4155 u32 inta, handled = 0;
4156 u32 inta_fh;
4157 unsigned long flags;
Tomas Winkler0a6857e2008-03-12 16:58:49 -07004158#ifdef CONFIG_IWLWIFI_DEBUG
Zhu Yib481de92007-09-25 17:54:57 -07004159 u32 inta_mask;
4160#endif
4161
4162 spin_lock_irqsave(&priv->lock, flags);
4163
4164 /* Ack/clear/reset pending uCode interrupts.
4165 * Note: Some bits in CSR_INT are "OR" of bits in CSR_FH_INT_STATUS,
4166 * and will clear only when CSR_FH_INT_STATUS gets cleared. */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004167 inta = iwl_read32(priv, CSR_INT);
4168 iwl_write32(priv, CSR_INT, inta);
Zhu Yib481de92007-09-25 17:54:57 -07004169
4170 /* Ack/clear/reset pending flow-handler (DMA) interrupts.
4171 * Any new interrupts that happen after this, either while we're
4172 * in this tasklet, or later, will show up in next ISR/tasklet. */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004173 inta_fh = iwl_read32(priv, CSR_FH_INT_STATUS);
4174 iwl_write32(priv, CSR_FH_INT_STATUS, inta_fh);
Zhu Yib481de92007-09-25 17:54:57 -07004175
Tomas Winkler0a6857e2008-03-12 16:58:49 -07004176#ifdef CONFIG_IWLWIFI_DEBUG
4177 if (iwl_debug_level & IWL_DL_ISR) {
Ben Cahill9fbab512007-11-29 11:09:47 +08004178 /* just for debug */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004179 inta_mask = iwl_read32(priv, CSR_INT_MASK);
Zhu Yib481de92007-09-25 17:54:57 -07004180 IWL_DEBUG_ISR("inta 0x%08x, enabled 0x%08x, fh 0x%08x\n",
4181 inta, inta_mask, inta_fh);
4182 }
4183#endif
4184
4185 /* Since CSR_INT and CSR_FH_INT_STATUS reads and clears are not
4186 * atomic, make sure that inta covers all the interrupts that
4187 * we've discovered, even if FH interrupt came in just after
4188 * reading CSR_INT. */
Tomas Winkler6f83eaa2008-03-04 18:09:28 -08004189 if (inta_fh & CSR49_FH_INT_RX_MASK)
Zhu Yib481de92007-09-25 17:54:57 -07004190 inta |= CSR_INT_BIT_FH_RX;
Tomas Winkler6f83eaa2008-03-04 18:09:28 -08004191 if (inta_fh & CSR49_FH_INT_TX_MASK)
Zhu Yib481de92007-09-25 17:54:57 -07004192 inta |= CSR_INT_BIT_FH_TX;
4193
4194 /* Now service all interrupt bits discovered above. */
4195 if (inta & CSR_INT_BIT_HW_ERR) {
4196 IWL_ERROR("Microcode HW error detected. Restarting.\n");
4197
4198 /* Tell the device to stop sending interrupts */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004199 iwl4965_disable_interrupts(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004200
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004201 iwl4965_irq_handle_error(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004202
4203 handled |= CSR_INT_BIT_HW_ERR;
4204
4205 spin_unlock_irqrestore(&priv->lock, flags);
4206
4207 return;
4208 }
4209
Tomas Winkler0a6857e2008-03-12 16:58:49 -07004210#ifdef CONFIG_IWLWIFI_DEBUG
4211 if (iwl_debug_level & (IWL_DL_ISR)) {
Zhu Yib481de92007-09-25 17:54:57 -07004212 /* NIC fires this, but we don't use it, redundant with WAKEUP */
Joonwoo Park25c03d82008-01-23 10:15:20 -08004213 if (inta & CSR_INT_BIT_SCD)
4214 IWL_DEBUG_ISR("Scheduler finished to transmit "
4215 "the frame/frames.\n");
Zhu Yib481de92007-09-25 17:54:57 -07004216
4217 /* Alive notification via Rx interrupt will do the real work */
4218 if (inta & CSR_INT_BIT_ALIVE)
4219 IWL_DEBUG_ISR("Alive interrupt\n");
4220 }
4221#endif
4222 /* Safely ignore these bits for debug checks below */
Joonwoo Park25c03d82008-01-23 10:15:20 -08004223 inta &= ~(CSR_INT_BIT_SCD | CSR_INT_BIT_ALIVE);
Zhu Yib481de92007-09-25 17:54:57 -07004224
Ben Cahill9fbab512007-11-29 11:09:47 +08004225 /* HW RF KILL switch toggled */
Zhu Yib481de92007-09-25 17:54:57 -07004226 if (inta & CSR_INT_BIT_RF_KILL) {
4227 int hw_rf_kill = 0;
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004228 if (!(iwl_read32(priv, CSR_GP_CNTRL) &
Zhu Yib481de92007-09-25 17:54:57 -07004229 CSR_GP_CNTRL_REG_FLAG_HW_RF_KILL_SW))
4230 hw_rf_kill = 1;
4231
4232 IWL_DEBUG(IWL_DL_INFO | IWL_DL_RF_KILL | IWL_DL_ISR,
4233 "RF_KILL bit toggled to %s.\n",
4234 hw_rf_kill ? "disable radio":"enable radio");
4235
4236 /* Queue restart only if RF_KILL switch was set to "kill"
4237 * when we loaded driver, and is now set to "enable".
4238 * After we're Alive, RF_KILL gets handled by
Reinette Chatre32304552008-02-15 14:34:37 -08004239 * iwl4965_rx_card_state_notif() */
Zhu Yi53e49092007-12-06 16:08:44 +08004240 if (!hw_rf_kill && !test_bit(STATUS_ALIVE, &priv->status)) {
4241 clear_bit(STATUS_RF_KILL_HW, &priv->status);
Zhu Yib481de92007-09-25 17:54:57 -07004242 queue_work(priv->workqueue, &priv->restart);
Zhu Yi53e49092007-12-06 16:08:44 +08004243 }
Zhu Yib481de92007-09-25 17:54:57 -07004244
4245 handled |= CSR_INT_BIT_RF_KILL;
4246 }
4247
Ben Cahill9fbab512007-11-29 11:09:47 +08004248 /* Chip got too hot and stopped itself */
Zhu Yib481de92007-09-25 17:54:57 -07004249 if (inta & CSR_INT_BIT_CT_KILL) {
4250 IWL_ERROR("Microcode CT kill error detected.\n");
4251 handled |= CSR_INT_BIT_CT_KILL;
4252 }
4253
4254 /* Error detected by uCode */
4255 if (inta & CSR_INT_BIT_SW_ERR) {
4256 IWL_ERROR("Microcode SW error detected. Restarting 0x%X.\n",
4257 inta);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004258 iwl4965_irq_handle_error(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004259 handled |= CSR_INT_BIT_SW_ERR;
4260 }
4261
4262 /* uCode wakes up after power-down sleep */
4263 if (inta & CSR_INT_BIT_WAKEUP) {
4264 IWL_DEBUG_ISR("Wakeup interrupt\n");
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004265 iwl4965_rx_queue_update_write_ptr(priv, &priv->rxq);
4266 iwl4965_tx_queue_update_write_ptr(priv, &priv->txq[0]);
4267 iwl4965_tx_queue_update_write_ptr(priv, &priv->txq[1]);
4268 iwl4965_tx_queue_update_write_ptr(priv, &priv->txq[2]);
4269 iwl4965_tx_queue_update_write_ptr(priv, &priv->txq[3]);
4270 iwl4965_tx_queue_update_write_ptr(priv, &priv->txq[4]);
4271 iwl4965_tx_queue_update_write_ptr(priv, &priv->txq[5]);
Zhu Yib481de92007-09-25 17:54:57 -07004272
4273 handled |= CSR_INT_BIT_WAKEUP;
4274 }
4275
4276 /* All uCode command responses, including Tx command responses,
4277 * Rx "responses" (frame-received notification), and other
4278 * notifications from uCode come through here*/
4279 if (inta & (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX)) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004280 iwl4965_rx_handle(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004281 handled |= (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX);
4282 }
4283
4284 if (inta & CSR_INT_BIT_FH_TX) {
4285 IWL_DEBUG_ISR("Tx interrupt\n");
4286 handled |= CSR_INT_BIT_FH_TX;
4287 }
4288
4289 if (inta & ~handled)
4290 IWL_ERROR("Unhandled INTA bits 0x%08x\n", inta & ~handled);
4291
4292 if (inta & ~CSR_INI_SET_MASK) {
4293 IWL_WARNING("Disabled INTA bits 0x%08x were pending\n",
4294 inta & ~CSR_INI_SET_MASK);
4295 IWL_WARNING(" with FH_INT = 0x%08x\n", inta_fh);
4296 }
4297
4298 /* Re-enable all interrupts */
Mohamed Abbas0359fac2008-03-28 16:21:08 -07004299 /* only Re-enable if diabled by irq */
4300 if (test_bit(STATUS_INT_ENABLED, &priv->status))
4301 iwl4965_enable_interrupts(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004302
Tomas Winkler0a6857e2008-03-12 16:58:49 -07004303#ifdef CONFIG_IWLWIFI_DEBUG
4304 if (iwl_debug_level & (IWL_DL_ISR)) {
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004305 inta = iwl_read32(priv, CSR_INT);
4306 inta_mask = iwl_read32(priv, CSR_INT_MASK);
4307 inta_fh = iwl_read32(priv, CSR_FH_INT_STATUS);
Zhu Yib481de92007-09-25 17:54:57 -07004308 IWL_DEBUG_ISR("End inta 0x%08x, enabled 0x%08x, fh 0x%08x, "
4309 "flags 0x%08lx\n", inta, inta_mask, inta_fh, flags);
4310 }
4311#endif
4312 spin_unlock_irqrestore(&priv->lock, flags);
4313}
4314
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004315static irqreturn_t iwl4965_isr(int irq, void *data)
Zhu Yib481de92007-09-25 17:54:57 -07004316{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004317 struct iwl_priv *priv = data;
Zhu Yib481de92007-09-25 17:54:57 -07004318 u32 inta, inta_mask;
4319 u32 inta_fh;
4320 if (!priv)
4321 return IRQ_NONE;
4322
4323 spin_lock(&priv->lock);
4324
4325 /* Disable (but don't clear!) interrupts here to avoid
4326 * back-to-back ISRs and sporadic interrupts from our NIC.
4327 * If we have something to service, the tasklet will re-enable ints.
4328 * If we *don't* have something, we'll re-enable before leaving here. */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004329 inta_mask = iwl_read32(priv, CSR_INT_MASK); /* just for debug */
4330 iwl_write32(priv, CSR_INT_MASK, 0x00000000);
Zhu Yib481de92007-09-25 17:54:57 -07004331
4332 /* Discover which interrupts are active/pending */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004333 inta = iwl_read32(priv, CSR_INT);
4334 inta_fh = iwl_read32(priv, CSR_FH_INT_STATUS);
Zhu Yib481de92007-09-25 17:54:57 -07004335
4336 /* Ignore interrupt if there's nothing in NIC to service.
4337 * This may be due to IRQ shared with another device,
4338 * or due to sporadic interrupts thrown from our NIC. */
4339 if (!inta && !inta_fh) {
4340 IWL_DEBUG_ISR("Ignore interrupt, inta == 0, inta_fh == 0\n");
4341 goto none;
4342 }
4343
4344 if ((inta == 0xFFFFFFFF) || ((inta & 0xFFFFFFF0) == 0xa5a5a5a0)) {
Oliver Neukum66fbb542007-11-15 09:31:10 +08004345 /* Hardware disappeared. It might have already raised
4346 * an interrupt */
Zhu Yib481de92007-09-25 17:54:57 -07004347 IWL_WARNING("HARDWARE GONE?? INTA == 0x%080x\n", inta);
Oliver Neukum66fbb542007-11-15 09:31:10 +08004348 goto unplugged;
Zhu Yib481de92007-09-25 17:54:57 -07004349 }
4350
4351 IWL_DEBUG_ISR("ISR inta 0x%08x, enabled 0x%08x, fh 0x%08x\n",
4352 inta, inta_mask, inta_fh);
4353
Joonwoo Park25c03d82008-01-23 10:15:20 -08004354 inta &= ~CSR_INT_BIT_SCD;
4355
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004356 /* iwl4965_irq_tasklet() will service interrupts and re-enable them */
Joonwoo Park25c03d82008-01-23 10:15:20 -08004357 if (likely(inta || inta_fh))
4358 tasklet_schedule(&priv->irq_tasklet);
Zhu Yib481de92007-09-25 17:54:57 -07004359
Oliver Neukum66fbb542007-11-15 09:31:10 +08004360 unplugged:
4361 spin_unlock(&priv->lock);
Zhu Yib481de92007-09-25 17:54:57 -07004362 return IRQ_HANDLED;
4363
4364 none:
4365 /* re-enable interrupts here since we don't have anything to service. */
Mohamed Abbas0359fac2008-03-28 16:21:08 -07004366 /* only Re-enable if diabled by irq */
4367 if (test_bit(STATUS_INT_ENABLED, &priv->status))
4368 iwl4965_enable_interrupts(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004369 spin_unlock(&priv->lock);
4370 return IRQ_NONE;
4371}
4372
Zhu Yib481de92007-09-25 17:54:57 -07004373/* For active scan, listen ACTIVE_DWELL_TIME (msec) on each channel after
4374 * sending probe req. This should be set long enough to hear probe responses
4375 * from more than one AP. */
4376#define IWL_ACTIVE_DWELL_TIME_24 (20) /* all times in msec */
4377#define IWL_ACTIVE_DWELL_TIME_52 (10)
4378
4379/* For faster active scanning, scan will move to the next channel if fewer than
4380 * PLCP_QUIET_THRESH packets are heard on this channel within
4381 * ACTIVE_QUIET_TIME after sending probe request. This shortens the dwell
4382 * time if it's a quiet channel (nothing responded to our probe, and there's
4383 * no other traffic).
4384 * Disable "quiet" feature by setting PLCP_QUIET_THRESH to 0. */
4385#define IWL_PLCP_QUIET_THRESH __constant_cpu_to_le16(1) /* packets */
4386#define IWL_ACTIVE_QUIET_TIME __constant_cpu_to_le16(5) /* msec */
4387
4388/* For passive scan, listen PASSIVE_DWELL_TIME (msec) on each channel.
4389 * Must be set longer than active dwell time.
4390 * For the most reliable scan, set > AP beacon interval (typically 100msec). */
4391#define IWL_PASSIVE_DWELL_TIME_24 (20) /* all times in msec */
4392#define IWL_PASSIVE_DWELL_TIME_52 (10)
4393#define IWL_PASSIVE_DWELL_BASE (100)
4394#define IWL_CHANNEL_TUNE_TIME 5
4395
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004396static inline u16 iwl4965_get_active_dwell_time(struct iwl_priv *priv,
Johannes Berg8318d782008-01-24 19:38:38 +01004397 enum ieee80211_band band)
Zhu Yib481de92007-09-25 17:54:57 -07004398{
Johannes Berg8318d782008-01-24 19:38:38 +01004399 if (band == IEEE80211_BAND_5GHZ)
Zhu Yib481de92007-09-25 17:54:57 -07004400 return IWL_ACTIVE_DWELL_TIME_52;
4401 else
4402 return IWL_ACTIVE_DWELL_TIME_24;
4403}
4404
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004405static u16 iwl4965_get_passive_dwell_time(struct iwl_priv *priv,
Johannes Berg8318d782008-01-24 19:38:38 +01004406 enum ieee80211_band band)
Zhu Yib481de92007-09-25 17:54:57 -07004407{
Johannes Berg8318d782008-01-24 19:38:38 +01004408 u16 active = iwl4965_get_active_dwell_time(priv, band);
4409 u16 passive = (band != IEEE80211_BAND_5GHZ) ?
Zhu Yib481de92007-09-25 17:54:57 -07004410 IWL_PASSIVE_DWELL_BASE + IWL_PASSIVE_DWELL_TIME_24 :
4411 IWL_PASSIVE_DWELL_BASE + IWL_PASSIVE_DWELL_TIME_52;
4412
Tomas Winkler3109ece2008-03-28 16:33:35 -07004413 if (iwl_is_associated(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07004414 /* If we're associated, we clamp the maximum passive
4415 * dwell time to be 98% of the beacon interval (minus
4416 * 2 * channel tune time) */
4417 passive = priv->beacon_int;
4418 if ((passive > IWL_PASSIVE_DWELL_BASE) || !passive)
4419 passive = IWL_PASSIVE_DWELL_BASE;
4420 passive = (passive * 98) / 100 - IWL_CHANNEL_TUNE_TIME * 2;
4421 }
4422
4423 if (passive <= active)
4424 passive = active + 1;
4425
4426 return passive;
4427}
4428
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004429static int iwl4965_get_channels_for_scan(struct iwl_priv *priv,
Johannes Berg8318d782008-01-24 19:38:38 +01004430 enum ieee80211_band band,
Zhu Yib481de92007-09-25 17:54:57 -07004431 u8 is_active, u8 direct_mask,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004432 struct iwl4965_scan_channel *scan_ch)
Zhu Yib481de92007-09-25 17:54:57 -07004433{
4434 const struct ieee80211_channel *channels = NULL;
Johannes Berg8318d782008-01-24 19:38:38 +01004435 const struct ieee80211_supported_band *sband;
Assaf Kraussbf85ea42008-03-14 10:38:49 -07004436 const struct iwl_channel_info *ch_info;
Zhu Yib481de92007-09-25 17:54:57 -07004437 u16 passive_dwell = 0;
4438 u16 active_dwell = 0;
4439 int added, i;
4440
Emmanuel Grumbachd1141df2008-04-21 15:42:00 -07004441 sband = iwl_get_hw_mode(priv, band);
Johannes Berg8318d782008-01-24 19:38:38 +01004442 if (!sband)
Zhu Yib481de92007-09-25 17:54:57 -07004443 return 0;
4444
Johannes Berg8318d782008-01-24 19:38:38 +01004445 channels = sband->channels;
Zhu Yib481de92007-09-25 17:54:57 -07004446
Johannes Berg8318d782008-01-24 19:38:38 +01004447 active_dwell = iwl4965_get_active_dwell_time(priv, band);
4448 passive_dwell = iwl4965_get_passive_dwell_time(priv, band);
Zhu Yib481de92007-09-25 17:54:57 -07004449
Johannes Berg8318d782008-01-24 19:38:38 +01004450 for (i = 0, added = 0; i < sband->n_channels; i++) {
Johannes Berg182e2e62008-04-04 10:41:56 +02004451 if (channels[i].flags & IEEE80211_CHAN_DISABLED)
4452 continue;
4453
Johannes Berg8318d782008-01-24 19:38:38 +01004454 scan_ch->channel = ieee80211_frequency_to_channel(channels[i].center_freq);
Zhu Yib481de92007-09-25 17:54:57 -07004455
Assaf Krauss8622e702008-03-21 13:53:43 -07004456 ch_info = iwl_get_channel_info(priv, band,
Ben Cahill9fbab512007-11-29 11:09:47 +08004457 scan_ch->channel);
Zhu Yib481de92007-09-25 17:54:57 -07004458 if (!is_channel_valid(ch_info)) {
4459 IWL_DEBUG_SCAN("Channel %d is INVALID for this SKU.\n",
4460 scan_ch->channel);
4461 continue;
4462 }
4463
4464 if (!is_active || is_channel_passive(ch_info) ||
Johannes Berg8318d782008-01-24 19:38:38 +01004465 (channels[i].flags & IEEE80211_CHAN_PASSIVE_SCAN))
Zhu Yib481de92007-09-25 17:54:57 -07004466 scan_ch->type = 0; /* passive */
4467 else
4468 scan_ch->type = 1; /* active */
4469
4470 if (scan_ch->type & 1)
4471 scan_ch->type |= (direct_mask << 1);
4472
Zhu Yib481de92007-09-25 17:54:57 -07004473 scan_ch->active_dwell = cpu_to_le16(active_dwell);
4474 scan_ch->passive_dwell = cpu_to_le16(passive_dwell);
4475
Ben Cahill9fbab512007-11-29 11:09:47 +08004476 /* Set txpower levels to defaults */
Zhu Yib481de92007-09-25 17:54:57 -07004477 scan_ch->tpc.dsp_atten = 110;
4478 /* scan_pwr_info->tpc.dsp_atten; */
4479
4480 /*scan_pwr_info->tpc.tx_gain; */
Johannes Berg8318d782008-01-24 19:38:38 +01004481 if (band == IEEE80211_BAND_5GHZ)
Zhu Yib481de92007-09-25 17:54:57 -07004482 scan_ch->tpc.tx_gain = ((1 << 5) | (3 << 3)) | 3;
4483 else {
4484 scan_ch->tpc.tx_gain = ((1 << 5) | (5 << 3));
4485 /* NOTE: if we were doing 6Mb OFDM for scans we'd use
Ben Cahill9fbab512007-11-29 11:09:47 +08004486 * power level:
Reinette Chatre8a1b0242008-01-14 17:46:25 -08004487 * scan_ch->tpc.tx_gain = ((1 << 5) | (2 << 3)) | 3;
Zhu Yib481de92007-09-25 17:54:57 -07004488 */
4489 }
4490
4491 IWL_DEBUG_SCAN("Scanning %d [%s %d]\n",
4492 scan_ch->channel,
4493 (scan_ch->type & 1) ? "ACTIVE" : "PASSIVE",
4494 (scan_ch->type & 1) ?
4495 active_dwell : passive_dwell);
4496
4497 scan_ch++;
4498 added++;
4499 }
4500
4501 IWL_DEBUG_SCAN("total channels to scan %d \n", added);
4502 return added;
4503}
4504
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004505static void iwl4965_init_hw_rates(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07004506 struct ieee80211_rate *rates)
4507{
4508 int i;
4509
4510 for (i = 0; i < IWL_RATE_COUNT; i++) {
Johannes Berg8318d782008-01-24 19:38:38 +01004511 rates[i].bitrate = iwl4965_rates[i].ieee * 5;
4512 rates[i].hw_value = i; /* Rate scaling will work on indexes */
4513 rates[i].hw_value_short = i;
4514 rates[i].flags = 0;
4515 if ((i > IWL_LAST_OFDM_RATE) || (i < IWL_FIRST_OFDM_RATE)) {
Zhu Yib481de92007-09-25 17:54:57 -07004516 /*
Johannes Berg8318d782008-01-24 19:38:38 +01004517 * If CCK != 1M then set short preamble rate flag.
Zhu Yib481de92007-09-25 17:54:57 -07004518 */
Tomas Winkler35cdeaf2008-03-11 16:17:20 -07004519 rates[i].flags |=
4520 (iwl4965_rates[i].plcp == IWL_RATE_1M_PLCP) ?
4521 0 : IEEE80211_RATE_SHORT_PREAMBLE;
Zhu Yib481de92007-09-25 17:54:57 -07004522 }
Zhu Yib481de92007-09-25 17:54:57 -07004523 }
Zhu Yib481de92007-09-25 17:54:57 -07004524}
4525
4526/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004527 * iwl4965_init_geos - Initialize mac80211's geo/channel info based from eeprom
Zhu Yib481de92007-09-25 17:54:57 -07004528 */
Assaf Kraussbf85ea42008-03-14 10:38:49 -07004529int iwl4965_init_geos(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004530{
Assaf Kraussbf85ea42008-03-14 10:38:49 -07004531 struct iwl_channel_info *ch;
Tomas Winkler8211ef72008-03-02 01:36:04 +02004532 struct ieee80211_supported_band *sband;
Zhu Yib481de92007-09-25 17:54:57 -07004533 struct ieee80211_channel *channels;
4534 struct ieee80211_channel *geo_ch;
4535 struct ieee80211_rate *rates;
4536 int i = 0;
Zhu Yib481de92007-09-25 17:54:57 -07004537
Johannes Berg8318d782008-01-24 19:38:38 +01004538 if (priv->bands[IEEE80211_BAND_2GHZ].n_bitrates ||
4539 priv->bands[IEEE80211_BAND_5GHZ].n_bitrates) {
Zhu Yib481de92007-09-25 17:54:57 -07004540 IWL_DEBUG_INFO("Geography modes already initialized.\n");
4541 set_bit(STATUS_GEO_CONFIGURED, &priv->status);
4542 return 0;
4543 }
4544
Zhu Yib481de92007-09-25 17:54:57 -07004545 channels = kzalloc(sizeof(struct ieee80211_channel) *
4546 priv->channel_count, GFP_KERNEL);
Johannes Berg8318d782008-01-24 19:38:38 +01004547 if (!channels)
Zhu Yib481de92007-09-25 17:54:57 -07004548 return -ENOMEM;
Zhu Yib481de92007-09-25 17:54:57 -07004549
Tomas Winkler8211ef72008-03-02 01:36:04 +02004550 rates = kzalloc((sizeof(struct ieee80211_rate) * (IWL_RATE_COUNT + 1)),
Zhu Yib481de92007-09-25 17:54:57 -07004551 GFP_KERNEL);
4552 if (!rates) {
Zhu Yib481de92007-09-25 17:54:57 -07004553 kfree(channels);
4554 return -ENOMEM;
4555 }
4556
Zhu Yib481de92007-09-25 17:54:57 -07004557 /* 5.2GHz channels start after the 2.4GHz channels */
Tomas Winkler8211ef72008-03-02 01:36:04 +02004558 sband = &priv->bands[IEEE80211_BAND_5GHZ];
Assaf Kraussbf85ea42008-03-14 10:38:49 -07004559 sband->channels = &channels[ARRAY_SIZE(iwl_eeprom_band_1)];
Tomas Winkler8211ef72008-03-02 01:36:04 +02004560 /* just OFDM */
4561 sband->bitrates = &rates[IWL_FIRST_OFDM_RATE];
4562 sband->n_bitrates = IWL_RATE_COUNT - IWL_FIRST_OFDM_RATE;
Johannes Berg8318d782008-01-24 19:38:38 +01004563
Assaf Krauss1ea87392008-03-18 14:57:50 -07004564 iwl4965_init_ht_hw_capab(priv, &sband->ht_info, IEEE80211_BAND_5GHZ);
Tomas Winkler78330fd2008-02-06 02:37:18 +02004565
Tomas Winkler8211ef72008-03-02 01:36:04 +02004566 sband = &priv->bands[IEEE80211_BAND_2GHZ];
4567 sband->channels = channels;
4568 /* OFDM & CCK */
4569 sband->bitrates = rates;
4570 sband->n_bitrates = IWL_RATE_COUNT;
Zhu Yib481de92007-09-25 17:54:57 -07004571
Assaf Krauss1ea87392008-03-18 14:57:50 -07004572 iwl4965_init_ht_hw_capab(priv, &sband->ht_info, IEEE80211_BAND_2GHZ);
Tomas Winkler78330fd2008-02-06 02:37:18 +02004573
Zhu Yib481de92007-09-25 17:54:57 -07004574 priv->ieee_channels = channels;
4575 priv->ieee_rates = rates;
4576
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004577 iwl4965_init_hw_rates(priv, rates);
Zhu Yib481de92007-09-25 17:54:57 -07004578
Tomas Winkler8211ef72008-03-02 01:36:04 +02004579 for (i = 0; i < priv->channel_count; i++) {
Zhu Yib481de92007-09-25 17:54:57 -07004580 ch = &priv->channel_info[i];
4581
Tomas Winkler8211ef72008-03-02 01:36:04 +02004582 /* FIXME: might be removed if scan is OK */
4583 if (!is_channel_valid(ch))
Zhu Yib481de92007-09-25 17:54:57 -07004584 continue;
Zhu Yib481de92007-09-25 17:54:57 -07004585
Tomas Winkler8211ef72008-03-02 01:36:04 +02004586 if (is_channel_a_band(ch))
4587 sband = &priv->bands[IEEE80211_BAND_5GHZ];
4588 else
4589 sband = &priv->bands[IEEE80211_BAND_2GHZ];
Zhu Yib481de92007-09-25 17:54:57 -07004590
Tomas Winkler8211ef72008-03-02 01:36:04 +02004591 geo_ch = &sband->channels[sband->n_channels++];
4592
4593 geo_ch->center_freq = ieee80211_channel_to_frequency(ch->channel);
Johannes Berg8318d782008-01-24 19:38:38 +01004594 geo_ch->max_power = ch->max_power_avg;
4595 geo_ch->max_antenna_gain = 0xff;
Mohamed Abbas7b723042008-01-31 21:46:40 -08004596 geo_ch->hw_value = ch->channel;
Zhu Yib481de92007-09-25 17:54:57 -07004597
4598 if (is_channel_valid(ch)) {
Johannes Berg8318d782008-01-24 19:38:38 +01004599 if (!(ch->flags & EEPROM_CHANNEL_IBSS))
4600 geo_ch->flags |= IEEE80211_CHAN_NO_IBSS;
Zhu Yib481de92007-09-25 17:54:57 -07004601
Johannes Berg8318d782008-01-24 19:38:38 +01004602 if (!(ch->flags & EEPROM_CHANNEL_ACTIVE))
4603 geo_ch->flags |= IEEE80211_CHAN_PASSIVE_SCAN;
Zhu Yib481de92007-09-25 17:54:57 -07004604
4605 if (ch->flags & EEPROM_CHANNEL_RADAR)
Johannes Berg8318d782008-01-24 19:38:38 +01004606 geo_ch->flags |= IEEE80211_CHAN_RADAR;
Zhu Yib481de92007-09-25 17:54:57 -07004607
4608 if (ch->max_power_avg > priv->max_channel_txpower_limit)
4609 priv->max_channel_txpower_limit =
4610 ch->max_power_avg;
Tomas Winkler8211ef72008-03-02 01:36:04 +02004611 } else {
Johannes Berg8318d782008-01-24 19:38:38 +01004612 geo_ch->flags |= IEEE80211_CHAN_DISABLED;
Tomas Winkler8211ef72008-03-02 01:36:04 +02004613 }
4614
4615 /* Save flags for reg domain usage */
4616 geo_ch->orig_flags = geo_ch->flags;
4617
4618 IWL_DEBUG_INFO("Channel %d Freq=%d[%sGHz] %s flag=0%X\n",
4619 ch->channel, geo_ch->center_freq,
4620 is_channel_a_band(ch) ? "5.2" : "2.4",
4621 geo_ch->flags & IEEE80211_CHAN_DISABLED ?
4622 "restricted" : "valid",
4623 geo_ch->flags);
Zhu Yib481de92007-09-25 17:54:57 -07004624 }
4625
Tomas Winkler82b9a122008-03-04 18:09:30 -08004626 if ((priv->bands[IEEE80211_BAND_5GHZ].n_channels == 0) &&
4627 priv->cfg->sku & IWL_SKU_A) {
Zhu Yib481de92007-09-25 17:54:57 -07004628 printk(KERN_INFO DRV_NAME
4629 ": Incorrectly detected BG card as ABG. Please send "
4630 "your PCI ID 0x%04X:0x%04X to maintainer.\n",
4631 priv->pci_dev->device, priv->pci_dev->subsystem_device);
Tomas Winkler82b9a122008-03-04 18:09:30 -08004632 priv->cfg->sku &= ~IWL_SKU_A;
Zhu Yib481de92007-09-25 17:54:57 -07004633 }
4634
4635 printk(KERN_INFO DRV_NAME
4636 ": Tunable channels: %d 802.11bg, %d 802.11a channels\n",
Johannes Berg8318d782008-01-24 19:38:38 +01004637 priv->bands[IEEE80211_BAND_2GHZ].n_channels,
4638 priv->bands[IEEE80211_BAND_5GHZ].n_channels);
Zhu Yib481de92007-09-25 17:54:57 -07004639
John W. Linvillee0e0a672008-03-25 15:58:40 -04004640 if (priv->bands[IEEE80211_BAND_2GHZ].n_channels)
4641 priv->hw->wiphy->bands[IEEE80211_BAND_2GHZ] =
4642 &priv->bands[IEEE80211_BAND_2GHZ];
4643 if (priv->bands[IEEE80211_BAND_5GHZ].n_channels)
4644 priv->hw->wiphy->bands[IEEE80211_BAND_5GHZ] =
4645 &priv->bands[IEEE80211_BAND_5GHZ];
Zhu Yib481de92007-09-25 17:54:57 -07004646
Zhu Yib481de92007-09-25 17:54:57 -07004647 set_bit(STATUS_GEO_CONFIGURED, &priv->status);
4648
4649 return 0;
4650}
4651
Reinette Chatre849e0dc2008-01-23 10:15:18 -08004652/*
4653 * iwl4965_free_geos - undo allocations in iwl4965_init_geos
4654 */
Assaf Kraussbf85ea42008-03-14 10:38:49 -07004655void iwl4965_free_geos(struct iwl_priv *priv)
Reinette Chatre849e0dc2008-01-23 10:15:18 -08004656{
Reinette Chatre849e0dc2008-01-23 10:15:18 -08004657 kfree(priv->ieee_channels);
4658 kfree(priv->ieee_rates);
4659 clear_bit(STATUS_GEO_CONFIGURED, &priv->status);
4660}
4661
Zhu Yib481de92007-09-25 17:54:57 -07004662/******************************************************************************
4663 *
4664 * uCode download functions
4665 *
4666 ******************************************************************************/
4667
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004668static void iwl4965_dealloc_ucode_pci(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004669{
Tomas Winkler98c92212008-01-14 17:46:20 -08004670 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_code);
4671 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_data);
4672 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_data_backup);
4673 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_init);
4674 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_init_data);
4675 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_boot);
Zhu Yib481de92007-09-25 17:54:57 -07004676}
4677
4678/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004679 * iwl4965_verify_inst_full - verify runtime uCode image in card vs. host,
Zhu Yib481de92007-09-25 17:54:57 -07004680 * looking at all data.
4681 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004682static int iwl4965_verify_inst_full(struct iwl_priv *priv, __le32 *image,
Ben Cahill9fbab512007-11-29 11:09:47 +08004683 u32 len)
Zhu Yib481de92007-09-25 17:54:57 -07004684{
4685 u32 val;
4686 u32 save_len = len;
4687 int rc = 0;
4688 u32 errcnt;
4689
4690 IWL_DEBUG_INFO("ucode inst image size is %u\n", len);
4691
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004692 rc = iwl_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004693 if (rc)
4694 return rc;
4695
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004696 iwl_write_direct32(priv, HBUS_TARG_MEM_RADDR, RTC_INST_LOWER_BOUND);
Zhu Yib481de92007-09-25 17:54:57 -07004697
4698 errcnt = 0;
4699 for (; len > 0; len -= sizeof(u32), image++) {
4700 /* read data comes through single port, auto-incr addr */
4701 /* NOTE: Use the debugless read so we don't flood kernel log
4702 * if IWL_DL_IO is set */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004703 val = _iwl_read_direct32(priv, HBUS_TARG_MEM_RDAT);
Zhu Yib481de92007-09-25 17:54:57 -07004704 if (val != le32_to_cpu(*image)) {
4705 IWL_ERROR("uCode INST section is invalid at "
4706 "offset 0x%x, is 0x%x, s/b 0x%x\n",
4707 save_len - len, val, le32_to_cpu(*image));
4708 rc = -EIO;
4709 errcnt++;
4710 if (errcnt >= 20)
4711 break;
4712 }
4713 }
4714
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004715 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004716
4717 if (!errcnt)
4718 IWL_DEBUG_INFO
4719 ("ucode image in INSTRUCTION memory is good\n");
4720
4721 return rc;
4722}
4723
4724
4725/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004726 * iwl4965_verify_inst_sparse - verify runtime uCode image in card vs. host,
Zhu Yib481de92007-09-25 17:54:57 -07004727 * using sample data 100 bytes apart. If these sample points are good,
4728 * it's a pretty good bet that everything between them is good, too.
4729 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004730static int iwl4965_verify_inst_sparse(struct iwl_priv *priv, __le32 *image, u32 len)
Zhu Yib481de92007-09-25 17:54:57 -07004731{
4732 u32 val;
4733 int rc = 0;
4734 u32 errcnt = 0;
4735 u32 i;
4736
4737 IWL_DEBUG_INFO("ucode inst image size is %u\n", len);
4738
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004739 rc = iwl_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004740 if (rc)
4741 return rc;
4742
4743 for (i = 0; i < len; i += 100, image += 100/sizeof(u32)) {
4744 /* read data comes through single port, auto-incr addr */
4745 /* NOTE: Use the debugless read so we don't flood kernel log
4746 * if IWL_DL_IO is set */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004747 iwl_write_direct32(priv, HBUS_TARG_MEM_RADDR,
Zhu Yib481de92007-09-25 17:54:57 -07004748 i + RTC_INST_LOWER_BOUND);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004749 val = _iwl_read_direct32(priv, HBUS_TARG_MEM_RDAT);
Zhu Yib481de92007-09-25 17:54:57 -07004750 if (val != le32_to_cpu(*image)) {
4751#if 0 /* Enable this if you want to see details */
4752 IWL_ERROR("uCode INST section is invalid at "
4753 "offset 0x%x, is 0x%x, s/b 0x%x\n",
4754 i, val, *image);
4755#endif
4756 rc = -EIO;
4757 errcnt++;
4758 if (errcnt >= 3)
4759 break;
4760 }
4761 }
4762
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004763 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004764
4765 return rc;
4766}
4767
4768
4769/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004770 * iwl4965_verify_ucode - determine which instruction image is in SRAM,
Zhu Yib481de92007-09-25 17:54:57 -07004771 * and verify its contents
4772 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004773static int iwl4965_verify_ucode(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004774{
4775 __le32 *image;
4776 u32 len;
4777 int rc = 0;
4778
4779 /* Try bootstrap */
4780 image = (__le32 *)priv->ucode_boot.v_addr;
4781 len = priv->ucode_boot.len;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004782 rc = iwl4965_verify_inst_sparse(priv, image, len);
Zhu Yib481de92007-09-25 17:54:57 -07004783 if (rc == 0) {
4784 IWL_DEBUG_INFO("Bootstrap uCode is good in inst SRAM\n");
4785 return 0;
4786 }
4787
4788 /* Try initialize */
4789 image = (__le32 *)priv->ucode_init.v_addr;
4790 len = priv->ucode_init.len;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004791 rc = iwl4965_verify_inst_sparse(priv, image, len);
Zhu Yib481de92007-09-25 17:54:57 -07004792 if (rc == 0) {
4793 IWL_DEBUG_INFO("Initialize uCode is good in inst SRAM\n");
4794 return 0;
4795 }
4796
4797 /* Try runtime/protocol */
4798 image = (__le32 *)priv->ucode_code.v_addr;
4799 len = priv->ucode_code.len;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004800 rc = iwl4965_verify_inst_sparse(priv, image, len);
Zhu Yib481de92007-09-25 17:54:57 -07004801 if (rc == 0) {
4802 IWL_DEBUG_INFO("Runtime uCode is good in inst SRAM\n");
4803 return 0;
4804 }
4805
4806 IWL_ERROR("NO VALID UCODE IMAGE IN INSTRUCTION SRAM!!\n");
4807
Ben Cahill9fbab512007-11-29 11:09:47 +08004808 /* Since nothing seems to match, show first several data entries in
4809 * instruction SRAM, so maybe visual inspection will give a clue.
4810 * Selection of bootstrap image (vs. other images) is arbitrary. */
Zhu Yib481de92007-09-25 17:54:57 -07004811 image = (__le32 *)priv->ucode_boot.v_addr;
4812 len = priv->ucode_boot.len;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004813 rc = iwl4965_verify_inst_full(priv, image, len);
Zhu Yib481de92007-09-25 17:54:57 -07004814
4815 return rc;
4816}
4817
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004818static void iwl4965_nic_start(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004819{
4820 /* Remove all resets to allow NIC to operate */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004821 iwl_write32(priv, CSR_RESET, 0);
Zhu Yib481de92007-09-25 17:54:57 -07004822}
4823
Tomas Winkler90e759d2007-11-29 11:09:41 +08004824
Zhu Yib481de92007-09-25 17:54:57 -07004825/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004826 * iwl4965_read_ucode - Read uCode images from disk file.
Zhu Yib481de92007-09-25 17:54:57 -07004827 *
4828 * Copy into buffers for card to fetch via bus-mastering
4829 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004830static int iwl4965_read_ucode(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004831{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004832 struct iwl4965_ucode *ucode;
Tomas Winkler90e759d2007-11-29 11:09:41 +08004833 int ret;
Zhu Yib481de92007-09-25 17:54:57 -07004834 const struct firmware *ucode_raw;
Tomas Winkler4bf775c2008-03-04 18:09:31 -08004835 const char *name = priv->cfg->fw_name;
Zhu Yib481de92007-09-25 17:54:57 -07004836 u8 *src;
4837 size_t len;
4838 u32 ver, inst_size, data_size, init_size, init_data_size, boot_size;
4839
4840 /* Ask kernel firmware_class module to get the boot firmware off disk.
4841 * request_firmware() is synchronous, file is in memory on return. */
Tomas Winkler90e759d2007-11-29 11:09:41 +08004842 ret = request_firmware(&ucode_raw, name, &priv->pci_dev->dev);
4843 if (ret < 0) {
4844 IWL_ERROR("%s firmware file req failed: Reason %d\n",
4845 name, ret);
Zhu Yib481de92007-09-25 17:54:57 -07004846 goto error;
4847 }
4848
4849 IWL_DEBUG_INFO("Got firmware '%s' file (%zd bytes) from disk\n",
4850 name, ucode_raw->size);
4851
4852 /* Make sure that we got at least our header! */
4853 if (ucode_raw->size < sizeof(*ucode)) {
4854 IWL_ERROR("File size way too small!\n");
Tomas Winkler90e759d2007-11-29 11:09:41 +08004855 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07004856 goto err_release;
4857 }
4858
4859 /* Data from ucode file: header followed by uCode images */
4860 ucode = (void *)ucode_raw->data;
4861
4862 ver = le32_to_cpu(ucode->ver);
4863 inst_size = le32_to_cpu(ucode->inst_size);
4864 data_size = le32_to_cpu(ucode->data_size);
4865 init_size = le32_to_cpu(ucode->init_size);
4866 init_data_size = le32_to_cpu(ucode->init_data_size);
4867 boot_size = le32_to_cpu(ucode->boot_size);
4868
4869 IWL_DEBUG_INFO("f/w package hdr ucode version = 0x%x\n", ver);
4870 IWL_DEBUG_INFO("f/w package hdr runtime inst size = %u\n",
4871 inst_size);
4872 IWL_DEBUG_INFO("f/w package hdr runtime data size = %u\n",
4873 data_size);
4874 IWL_DEBUG_INFO("f/w package hdr init inst size = %u\n",
4875 init_size);
4876 IWL_DEBUG_INFO("f/w package hdr init data size = %u\n",
4877 init_data_size);
4878 IWL_DEBUG_INFO("f/w package hdr boot inst size = %u\n",
4879 boot_size);
4880
4881 /* Verify size of file vs. image size info in file's header */
4882 if (ucode_raw->size < sizeof(*ucode) +
4883 inst_size + data_size + init_size +
4884 init_data_size + boot_size) {
4885
4886 IWL_DEBUG_INFO("uCode file size %d too small\n",
4887 (int)ucode_raw->size);
Tomas Winkler90e759d2007-11-29 11:09:41 +08004888 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07004889 goto err_release;
4890 }
4891
4892 /* Verify that uCode images will fit in card's SRAM */
Ron Rindjunsky099b40b2008-04-21 15:41:53 -07004893 if (inst_size > priv->hw_params.max_inst_size) {
Tomas Winkler90e759d2007-11-29 11:09:41 +08004894 IWL_DEBUG_INFO("uCode instr len %d too large to fit in\n",
4895 inst_size);
4896 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07004897 goto err_release;
4898 }
4899
Ron Rindjunsky099b40b2008-04-21 15:41:53 -07004900 if (data_size > priv->hw_params.max_data_size) {
Tomas Winkler90e759d2007-11-29 11:09:41 +08004901 IWL_DEBUG_INFO("uCode data len %d too large to fit in\n",
4902 data_size);
4903 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07004904 goto err_release;
4905 }
Ron Rindjunsky099b40b2008-04-21 15:41:53 -07004906 if (init_size > priv->hw_params.max_inst_size) {
Zhu Yib481de92007-09-25 17:54:57 -07004907 IWL_DEBUG_INFO
Tomas Winkler90e759d2007-11-29 11:09:41 +08004908 ("uCode init instr len %d too large to fit in\n",
4909 init_size);
4910 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07004911 goto err_release;
4912 }
Ron Rindjunsky099b40b2008-04-21 15:41:53 -07004913 if (init_data_size > priv->hw_params.max_data_size) {
Zhu Yib481de92007-09-25 17:54:57 -07004914 IWL_DEBUG_INFO
Tomas Winkler90e759d2007-11-29 11:09:41 +08004915 ("uCode init data len %d too large to fit in\n",
4916 init_data_size);
4917 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07004918 goto err_release;
4919 }
Ron Rindjunsky099b40b2008-04-21 15:41:53 -07004920 if (boot_size > priv->hw_params.max_bsm_size) {
Zhu Yib481de92007-09-25 17:54:57 -07004921 IWL_DEBUG_INFO
Tomas Winkler90e759d2007-11-29 11:09:41 +08004922 ("uCode boot instr len %d too large to fit in\n",
4923 boot_size);
4924 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07004925 goto err_release;
4926 }
4927
4928 /* Allocate ucode buffers for card's bus-master loading ... */
4929
4930 /* Runtime instructions and 2 copies of data:
4931 * 1) unmodified from disk
4932 * 2) backup cache for save/restore during power-downs */
4933 priv->ucode_code.len = inst_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08004934 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_code);
Zhu Yib481de92007-09-25 17:54:57 -07004935
4936 priv->ucode_data.len = data_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08004937 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_data);
Zhu Yib481de92007-09-25 17:54:57 -07004938
4939 priv->ucode_data_backup.len = data_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08004940 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_data_backup);
Zhu Yib481de92007-09-25 17:54:57 -07004941
4942 /* Initialization instructions and data */
Tomas Winkler90e759d2007-11-29 11:09:41 +08004943 if (init_size && init_data_size) {
4944 priv->ucode_init.len = init_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08004945 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_init);
Zhu Yib481de92007-09-25 17:54:57 -07004946
Tomas Winkler90e759d2007-11-29 11:09:41 +08004947 priv->ucode_init_data.len = init_data_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08004948 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_init_data);
Tomas Winkler90e759d2007-11-29 11:09:41 +08004949
4950 if (!priv->ucode_init.v_addr || !priv->ucode_init_data.v_addr)
4951 goto err_pci_alloc;
4952 }
Zhu Yib481de92007-09-25 17:54:57 -07004953
4954 /* Bootstrap (instructions only, no data) */
Tomas Winkler90e759d2007-11-29 11:09:41 +08004955 if (boot_size) {
4956 priv->ucode_boot.len = boot_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08004957 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_boot);
Zhu Yib481de92007-09-25 17:54:57 -07004958
Tomas Winkler90e759d2007-11-29 11:09:41 +08004959 if (!priv->ucode_boot.v_addr)
4960 goto err_pci_alloc;
4961 }
Zhu Yib481de92007-09-25 17:54:57 -07004962
4963 /* Copy images into buffers for card's bus-master reads ... */
4964
4965 /* Runtime instructions (first block of data in file) */
4966 src = &ucode->data[0];
4967 len = priv->ucode_code.len;
Tomas Winkler90e759d2007-11-29 11:09:41 +08004968 IWL_DEBUG_INFO("Copying (but not loading) uCode instr len %Zd\n", len);
Zhu Yib481de92007-09-25 17:54:57 -07004969 memcpy(priv->ucode_code.v_addr, src, len);
4970 IWL_DEBUG_INFO("uCode instr buf vaddr = 0x%p, paddr = 0x%08x\n",
4971 priv->ucode_code.v_addr, (u32)priv->ucode_code.p_addr);
4972
4973 /* Runtime data (2nd block)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004974 * NOTE: Copy into backup buffer will be done in iwl4965_up() */
Zhu Yib481de92007-09-25 17:54:57 -07004975 src = &ucode->data[inst_size];
4976 len = priv->ucode_data.len;
Tomas Winkler90e759d2007-11-29 11:09:41 +08004977 IWL_DEBUG_INFO("Copying (but not loading) uCode data len %Zd\n", len);
Zhu Yib481de92007-09-25 17:54:57 -07004978 memcpy(priv->ucode_data.v_addr, src, len);
4979 memcpy(priv->ucode_data_backup.v_addr, src, len);
4980
4981 /* Initialization instructions (3rd block) */
4982 if (init_size) {
4983 src = &ucode->data[inst_size + data_size];
4984 len = priv->ucode_init.len;
Tomas Winkler90e759d2007-11-29 11:09:41 +08004985 IWL_DEBUG_INFO("Copying (but not loading) init instr len %Zd\n",
4986 len);
Zhu Yib481de92007-09-25 17:54:57 -07004987 memcpy(priv->ucode_init.v_addr, src, len);
4988 }
4989
4990 /* Initialization data (4th block) */
4991 if (init_data_size) {
4992 src = &ucode->data[inst_size + data_size + init_size];
4993 len = priv->ucode_init_data.len;
Tomas Winkler90e759d2007-11-29 11:09:41 +08004994 IWL_DEBUG_INFO("Copying (but not loading) init data len %Zd\n",
4995 len);
Zhu Yib481de92007-09-25 17:54:57 -07004996 memcpy(priv->ucode_init_data.v_addr, src, len);
4997 }
4998
4999 /* Bootstrap instructions (5th block) */
5000 src = &ucode->data[inst_size + data_size + init_size + init_data_size];
5001 len = priv->ucode_boot.len;
Tomas Winkler90e759d2007-11-29 11:09:41 +08005002 IWL_DEBUG_INFO("Copying (but not loading) boot instr len %Zd\n", len);
Zhu Yib481de92007-09-25 17:54:57 -07005003 memcpy(priv->ucode_boot.v_addr, src, len);
5004
5005 /* We have our copies now, allow OS release its copies */
5006 release_firmware(ucode_raw);
5007 return 0;
5008
5009 err_pci_alloc:
5010 IWL_ERROR("failed to allocate pci memory\n");
Tomas Winkler90e759d2007-11-29 11:09:41 +08005011 ret = -ENOMEM;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005012 iwl4965_dealloc_ucode_pci(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005013
5014 err_release:
5015 release_firmware(ucode_raw);
5016
5017 error:
Tomas Winkler90e759d2007-11-29 11:09:41 +08005018 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07005019}
5020
5021
5022/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005023 * iwl4965_set_ucode_ptrs - Set uCode address location
Zhu Yib481de92007-09-25 17:54:57 -07005024 *
5025 * Tell initialization uCode where to find runtime uCode.
5026 *
5027 * BSM registers initially contain pointers to initialization uCode.
5028 * We need to replace them to load runtime uCode inst and data,
5029 * and to save runtime data when powering down.
5030 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005031static int iwl4965_set_ucode_ptrs(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005032{
5033 dma_addr_t pinst;
5034 dma_addr_t pdata;
5035 int rc = 0;
5036 unsigned long flags;
5037
5038 /* bits 35:4 for 4965 */
5039 pinst = priv->ucode_code.p_addr >> 4;
5040 pdata = priv->ucode_data_backup.p_addr >> 4;
5041
5042 spin_lock_irqsave(&priv->lock, flags);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07005043 rc = iwl_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005044 if (rc) {
5045 spin_unlock_irqrestore(&priv->lock, flags);
5046 return rc;
5047 }
5048
5049 /* Tell bootstrap uCode where to find image to load */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07005050 iwl_write_prph(priv, BSM_DRAM_INST_PTR_REG, pinst);
5051 iwl_write_prph(priv, BSM_DRAM_DATA_PTR_REG, pdata);
5052 iwl_write_prph(priv, BSM_DRAM_DATA_BYTECOUNT_REG,
Zhu Yib481de92007-09-25 17:54:57 -07005053 priv->ucode_data.len);
5054
5055 /* Inst bytecount must be last to set up, bit 31 signals uCode
5056 * that all new ptr/size info is in place */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07005057 iwl_write_prph(priv, BSM_DRAM_INST_BYTECOUNT_REG,
Zhu Yib481de92007-09-25 17:54:57 -07005058 priv->ucode_code.len | BSM_DRAM_INST_LOAD);
5059
Tomas Winkler3395f6e2008-03-25 16:33:37 -07005060 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005061
5062 spin_unlock_irqrestore(&priv->lock, flags);
5063
5064 IWL_DEBUG_INFO("Runtime uCode pointers are set.\n");
5065
5066 return rc;
5067}
5068
5069/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005070 * iwl4965_init_alive_start - Called after REPLY_ALIVE notification received
Zhu Yib481de92007-09-25 17:54:57 -07005071 *
5072 * Called after REPLY_ALIVE notification received from "initialize" uCode.
5073 *
5074 * The 4965 "initialize" ALIVE reply contains calibration data for:
5075 * Voltage, temperature, and MIMO tx gain correction, now stored in priv
5076 * (3945 does not contain this data).
5077 *
5078 * Tell "initialize" uCode to go ahead and load the runtime uCode.
5079*/
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005080static void iwl4965_init_alive_start(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005081{
5082 /* Check alive response for "valid" sign from uCode */
5083 if (priv->card_alive_init.is_valid != UCODE_VALID_OK) {
5084 /* We had an error bringing up the hardware, so take it
5085 * all the way back down so we can try again */
5086 IWL_DEBUG_INFO("Initialize Alive failed.\n");
5087 goto restart;
5088 }
5089
5090 /* Bootstrap uCode has loaded initialize uCode ... verify inst image.
5091 * This is a paranoid check, because we would not have gotten the
5092 * "initialize" alive if code weren't properly loaded. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005093 if (iwl4965_verify_ucode(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07005094 /* Runtime instruction load was bad;
5095 * take it all the way back down so we can try again */
5096 IWL_DEBUG_INFO("Bad \"initialize\" uCode load.\n");
5097 goto restart;
5098 }
5099
5100 /* Calculate temperature */
5101 priv->temperature = iwl4965_get_temperature(priv);
5102
5103 /* Send pointers to protocol/runtime uCode image ... init code will
5104 * load and launch runtime uCode, which will send us another "Alive"
5105 * notification. */
5106 IWL_DEBUG_INFO("Initialization Alive received.\n");
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005107 if (iwl4965_set_ucode_ptrs(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07005108 /* Runtime instruction load won't happen;
5109 * take it all the way back down so we can try again */
5110 IWL_DEBUG_INFO("Couldn't set up uCode pointers.\n");
5111 goto restart;
5112 }
5113 return;
5114
5115 restart:
5116 queue_work(priv->workqueue, &priv->restart);
5117}
5118
5119
5120/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005121 * iwl4965_alive_start - called after REPLY_ALIVE notification received
Zhu Yib481de92007-09-25 17:54:57 -07005122 * from protocol/runtime uCode (initialization uCode's
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005123 * Alive gets handled by iwl4965_init_alive_start()).
Zhu Yib481de92007-09-25 17:54:57 -07005124 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005125static void iwl4965_alive_start(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005126{
Tomas Winkler57aab752008-04-14 21:16:03 -07005127 int ret = 0;
Zhu Yib481de92007-09-25 17:54:57 -07005128
5129 IWL_DEBUG_INFO("Runtime Alive received.\n");
5130
5131 if (priv->card_alive.is_valid != UCODE_VALID_OK) {
5132 /* We had an error bringing up the hardware, so take it
5133 * all the way back down so we can try again */
5134 IWL_DEBUG_INFO("Alive failed.\n");
5135 goto restart;
5136 }
5137
5138 /* Initialize uCode has loaded Runtime uCode ... verify inst image.
5139 * This is a paranoid check, because we would not have gotten the
5140 * "runtime" alive if code weren't properly loaded. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005141 if (iwl4965_verify_ucode(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07005142 /* Runtime instruction load was bad;
5143 * take it all the way back down so we can try again */
5144 IWL_DEBUG_INFO("Bad runtime uCode load.\n");
5145 goto restart;
5146 }
5147
Assaf Kraussbf85ea42008-03-14 10:38:49 -07005148 iwlcore_clear_stations_table(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005149
Tomas Winkler57aab752008-04-14 21:16:03 -07005150 ret = priv->cfg->ops->lib->alive_notify(priv);
5151 if (ret) {
Zhu Yib481de92007-09-25 17:54:57 -07005152 IWL_WARNING("Could not complete ALIVE transition [ntf]: %d\n",
Tomas Winkler57aab752008-04-14 21:16:03 -07005153 ret);
Zhu Yib481de92007-09-25 17:54:57 -07005154 goto restart;
5155 }
5156
Ben Cahill9fbab512007-11-29 11:09:47 +08005157 /* After the ALIVE response, we can send host commands to 4965 uCode */
Zhu Yib481de92007-09-25 17:54:57 -07005158 set_bit(STATUS_ALIVE, &priv->status);
5159
5160 /* Clear out the uCode error bit if it is set */
5161 clear_bit(STATUS_FW_ERROR, &priv->status);
5162
Tomas Winklerfee12472008-04-03 16:05:21 -07005163 if (iwl_is_rfkill(priv))
Zhu Yib481de92007-09-25 17:54:57 -07005164 return;
5165
Zhu Yi5a66926a2008-01-14 17:46:18 -08005166 ieee80211_start_queues(priv->hw);
Zhu Yib481de92007-09-25 17:54:57 -07005167
5168 priv->active_rate = priv->rates_mask;
5169 priv->active_rate_basic = priv->rates_mask & IWL_BASIC_RATES_MASK;
5170
Tomas Winkler3109ece2008-03-28 16:33:35 -07005171 if (iwl_is_associated(priv)) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005172 struct iwl4965_rxon_cmd *active_rxon =
5173 (struct iwl4965_rxon_cmd *)(&priv->active_rxon);
Zhu Yib481de92007-09-25 17:54:57 -07005174
5175 memcpy(&priv->staging_rxon, &priv->active_rxon,
5176 sizeof(priv->staging_rxon));
5177 active_rxon->filter_flags &= ~RXON_FILTER_ASSOC_MSK;
5178 } else {
5179 /* Initialize our rx_config data */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005180 iwl4965_connection_init_rx_config(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005181 memcpy(priv->staging_rxon.node_addr, priv->mac_addr, ETH_ALEN);
5182 }
5183
Ben Cahill9fbab512007-11-29 11:09:47 +08005184 /* Configure Bluetooth device coexistence support */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005185 iwl4965_send_bt_config(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005186
5187 /* Configure the adapter for unassociated operation */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005188 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005189
5190 /* At this point, the NIC is initialized and operational */
5191 priv->notif_missed_beacons = 0;
Zhu Yib481de92007-09-25 17:54:57 -07005192
5193 iwl4965_rf_kill_ct_config(priv);
Zhu Yi5a66926a2008-01-14 17:46:18 -08005194
Reinette Chatrefe00b5a2008-04-03 16:05:23 -07005195 iwl_leds_register(priv);
5196
Zhu Yib481de92007-09-25 17:54:57 -07005197 IWL_DEBUG_INFO("ALIVE processing complete.\n");
Rick Farringtona9f46782008-03-18 14:57:49 -07005198 set_bit(STATUS_READY, &priv->status);
Zhu Yi5a66926a2008-01-14 17:46:18 -08005199 wake_up_interruptible(&priv->wait_command_queue);
Zhu Yib481de92007-09-25 17:54:57 -07005200
5201 if (priv->error_recovering)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005202 iwl4965_error_recovery(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005203
Mohamed Abbasc8381fd2008-03-28 16:21:05 -07005204 iwlcore_low_level_notify(priv, IWLCORE_START_EVT);
Mohamed Abbas84363e62008-04-04 16:59:58 -07005205 ieee80211_notify_mac(priv->hw, IEEE80211_NOTIFY_RE_ASSOC);
Zhu Yib481de92007-09-25 17:54:57 -07005206 return;
5207
5208 restart:
5209 queue_work(priv->workqueue, &priv->restart);
5210}
5211
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005212static void iwl4965_cancel_deferred_work(struct iwl_priv *priv);
Zhu Yib481de92007-09-25 17:54:57 -07005213
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005214static void __iwl4965_down(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005215{
5216 unsigned long flags;
5217 int exit_pending = test_bit(STATUS_EXIT_PENDING, &priv->status);
5218 struct ieee80211_conf *conf = NULL;
5219
5220 IWL_DEBUG_INFO(DRV_NAME " is going down\n");
5221
5222 conf = ieee80211_get_hw_conf(priv->hw);
5223
5224 if (!exit_pending)
5225 set_bit(STATUS_EXIT_PENDING, &priv->status);
5226
Mohamed Abbasab53d8a2008-03-25 16:33:36 -07005227 iwl_leds_unregister(priv);
5228
Mohamed Abbasc8381fd2008-03-28 16:21:05 -07005229 iwlcore_low_level_notify(priv, IWLCORE_STOP_EVT);
5230
Assaf Kraussbf85ea42008-03-14 10:38:49 -07005231 iwlcore_clear_stations_table(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005232
5233 /* Unblock any waiting calls */
5234 wake_up_interruptible_all(&priv->wait_command_queue);
5235
Zhu Yib481de92007-09-25 17:54:57 -07005236 /* Wipe out the EXIT_PENDING status bit if we are not actually
5237 * exiting the module */
5238 if (!exit_pending)
5239 clear_bit(STATUS_EXIT_PENDING, &priv->status);
5240
5241 /* stop and reset the on-board processor */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07005242 iwl_write32(priv, CSR_RESET, CSR_RESET_REG_FLAG_NEVO_RESET);
Zhu Yib481de92007-09-25 17:54:57 -07005243
5244 /* tell the device to stop sending interrupts */
Mohamed Abbas0359fac2008-03-28 16:21:08 -07005245 spin_lock_irqsave(&priv->lock, flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005246 iwl4965_disable_interrupts(priv);
Mohamed Abbas0359fac2008-03-28 16:21:08 -07005247 spin_unlock_irqrestore(&priv->lock, flags);
5248 iwl_synchronize_irq(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005249
5250 if (priv->mac80211_registered)
5251 ieee80211_stop_queues(priv->hw);
5252
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005253 /* If we have not previously called iwl4965_init() then
Zhu Yib481de92007-09-25 17:54:57 -07005254 * clear all bits but the RF Kill and SUSPEND bits and return */
Tomas Winklerfee12472008-04-03 16:05:21 -07005255 if (!iwl_is_init(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07005256 priv->status = test_bit(STATUS_RF_KILL_HW, &priv->status) <<
5257 STATUS_RF_KILL_HW |
5258 test_bit(STATUS_RF_KILL_SW, &priv->status) <<
5259 STATUS_RF_KILL_SW |
Reinette Chatre97888642008-02-06 11:20:38 -08005260 test_bit(STATUS_GEO_CONFIGURED, &priv->status) <<
5261 STATUS_GEO_CONFIGURED |
Zhu Yib481de92007-09-25 17:54:57 -07005262 test_bit(STATUS_IN_SUSPEND, &priv->status) <<
5263 STATUS_IN_SUSPEND;
5264 goto exit;
5265 }
5266
5267 /* ...otherwise clear out all the status bits but the RF Kill and
5268 * SUSPEND bits and continue taking the NIC down. */
5269 priv->status &= test_bit(STATUS_RF_KILL_HW, &priv->status) <<
5270 STATUS_RF_KILL_HW |
5271 test_bit(STATUS_RF_KILL_SW, &priv->status) <<
5272 STATUS_RF_KILL_SW |
Reinette Chatre97888642008-02-06 11:20:38 -08005273 test_bit(STATUS_GEO_CONFIGURED, &priv->status) <<
5274 STATUS_GEO_CONFIGURED |
Zhu Yib481de92007-09-25 17:54:57 -07005275 test_bit(STATUS_IN_SUSPEND, &priv->status) <<
5276 STATUS_IN_SUSPEND |
5277 test_bit(STATUS_FW_ERROR, &priv->status) <<
5278 STATUS_FW_ERROR;
5279
5280 spin_lock_irqsave(&priv->lock, flags);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07005281 iwl_clear_bit(priv, CSR_GP_CNTRL,
Ben Cahill9fbab512007-11-29 11:09:47 +08005282 CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
Zhu Yib481de92007-09-25 17:54:57 -07005283 spin_unlock_irqrestore(&priv->lock, flags);
5284
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005285 iwl4965_hw_txq_ctx_stop(priv);
5286 iwl4965_hw_rxq_stop(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005287
5288 spin_lock_irqsave(&priv->lock, flags);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07005289 if (!iwl_grab_nic_access(priv)) {
5290 iwl_write_prph(priv, APMG_CLK_DIS_REG,
Zhu Yib481de92007-09-25 17:54:57 -07005291 APMG_CLK_VAL_DMA_CLK_RQT);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07005292 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005293 }
5294 spin_unlock_irqrestore(&priv->lock, flags);
5295
5296 udelay(5);
5297
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005298 iwl4965_hw_nic_stop_master(priv);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07005299 iwl_set_bit(priv, CSR_RESET, CSR_RESET_REG_FLAG_SW_RESET);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005300 iwl4965_hw_nic_reset(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005301
5302 exit:
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005303 memset(&priv->card_alive, 0, sizeof(struct iwl4965_alive_resp));
Zhu Yib481de92007-09-25 17:54:57 -07005304
5305 if (priv->ibss_beacon)
5306 dev_kfree_skb(priv->ibss_beacon);
5307 priv->ibss_beacon = NULL;
5308
5309 /* clear out any free frames */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005310 iwl4965_clear_free_frames(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005311}
5312
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005313static void iwl4965_down(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005314{
5315 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005316 __iwl4965_down(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005317 mutex_unlock(&priv->mutex);
Zhu Yib24d22b2007-12-19 13:59:52 +08005318
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005319 iwl4965_cancel_deferred_work(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005320}
5321
5322#define MAX_HW_RESTARTS 5
5323
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005324static int __iwl4965_up(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005325{
Tomas Winkler57aab752008-04-14 21:16:03 -07005326 int i;
5327 int ret;
Zhu Yib481de92007-09-25 17:54:57 -07005328
5329 if (test_bit(STATUS_EXIT_PENDING, &priv->status)) {
5330 IWL_WARNING("Exit pending; will not bring the NIC up\n");
5331 return -EIO;
5332 }
5333
5334 if (test_bit(STATUS_RF_KILL_SW, &priv->status)) {
5335 IWL_WARNING("Radio disabled by SW RF kill (module "
5336 "parameter)\n");
Mohamed Abbasad97edd2008-03-28 16:21:06 -07005337 iwl_rfkill_set_hw_state(priv);
Zhu Yie655b9f2008-01-24 02:19:38 -08005338 return -ENODEV;
5339 }
5340
Reinette Chatree903fbd2008-01-30 22:05:15 -08005341 if (!priv->ucode_data_backup.v_addr || !priv->ucode_data.v_addr) {
5342 IWL_ERROR("ucode not available for device bringup\n");
5343 return -EIO;
5344 }
5345
Zhu Yie655b9f2008-01-24 02:19:38 -08005346 /* If platform's RF_KILL switch is NOT set to KILL */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07005347 if (iwl_read32(priv, CSR_GP_CNTRL) &
Zhu Yie655b9f2008-01-24 02:19:38 -08005348 CSR_GP_CNTRL_REG_FLAG_HW_RF_KILL_SW)
5349 clear_bit(STATUS_RF_KILL_HW, &priv->status);
5350 else {
5351 set_bit(STATUS_RF_KILL_HW, &priv->status);
5352 if (!test_bit(STATUS_IN_SUSPEND, &priv->status)) {
Mohamed Abbasad97edd2008-03-28 16:21:06 -07005353 iwl_rfkill_set_hw_state(priv);
Zhu Yie655b9f2008-01-24 02:19:38 -08005354 IWL_WARNING("Radio disabled by HW RF Kill switch\n");
5355 return -ENODEV;
5356 }
Zhu Yib481de92007-09-25 17:54:57 -07005357 }
5358
Mohamed Abbasad97edd2008-03-28 16:21:06 -07005359 iwl_rfkill_set_hw_state(priv);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07005360 iwl_write32(priv, CSR_INT, 0xFFFFFFFF);
Zhu Yib481de92007-09-25 17:54:57 -07005361
Tomas Winkler57aab752008-04-14 21:16:03 -07005362 ret = priv->cfg->ops->lib->hw_nic_init(priv);
5363 if (ret) {
5364 IWL_ERROR("Unable to init nic\n");
5365 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07005366 }
5367
5368 /* make sure rfkill handshake bits are cleared */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07005369 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
5370 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR,
Zhu Yib481de92007-09-25 17:54:57 -07005371 CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
5372
5373 /* clear (again), then enable host interrupts */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07005374 iwl_write32(priv, CSR_INT, 0xFFFFFFFF);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005375 iwl4965_enable_interrupts(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005376
5377 /* really make sure rfkill handshake bits are cleared */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07005378 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
5379 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
Zhu Yib481de92007-09-25 17:54:57 -07005380
5381 /* Copy original ucode data image from disk into backup cache.
5382 * This will be used to initialize the on-board processor's
5383 * data SRAM for a clean start when the runtime program first loads. */
5384 memcpy(priv->ucode_data_backup.v_addr, priv->ucode_data.v_addr,
Zhu Yi5a66926a2008-01-14 17:46:18 -08005385 priv->ucode_data.len);
Zhu Yib481de92007-09-25 17:54:57 -07005386
Zhu Yie655b9f2008-01-24 02:19:38 -08005387 /* We return success when we resume from suspend and rf_kill is on. */
5388 if (test_bit(STATUS_RF_KILL_HW, &priv->status))
Zhu Yib481de92007-09-25 17:54:57 -07005389 return 0;
Zhu Yib481de92007-09-25 17:54:57 -07005390
5391 for (i = 0; i < MAX_HW_RESTARTS; i++) {
5392
Assaf Kraussbf85ea42008-03-14 10:38:49 -07005393 iwlcore_clear_stations_table(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005394
5395 /* load bootstrap state machine,
5396 * load bootstrap program into processor's memory,
5397 * prepare to load the "initialize" uCode */
Tomas Winkler57aab752008-04-14 21:16:03 -07005398 ret = priv->cfg->ops->lib->load_ucode(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005399
Tomas Winkler57aab752008-04-14 21:16:03 -07005400 if (ret) {
5401 IWL_ERROR("Unable to set up bootstrap uCode: %d\n", ret);
Zhu Yib481de92007-09-25 17:54:57 -07005402 continue;
5403 }
5404
5405 /* start card; "initialize" will load runtime ucode */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005406 iwl4965_nic_start(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005407
Zhu Yib481de92007-09-25 17:54:57 -07005408 IWL_DEBUG_INFO(DRV_NAME " is coming up\n");
5409
5410 return 0;
5411 }
5412
5413 set_bit(STATUS_EXIT_PENDING, &priv->status);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005414 __iwl4965_down(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005415
5416 /* tried to restart and config the device for as long as our
5417 * patience could withstand */
5418 IWL_ERROR("Unable to initialize device after %d attempts.\n", i);
5419 return -EIO;
5420}
5421
5422
5423/*****************************************************************************
5424 *
5425 * Workqueue callbacks
5426 *
5427 *****************************************************************************/
5428
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005429static void iwl4965_bg_init_alive_start(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07005430{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005431 struct iwl_priv *priv =
5432 container_of(data, struct iwl_priv, init_alive_start.work);
Zhu Yib481de92007-09-25 17:54:57 -07005433
5434 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
5435 return;
5436
5437 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005438 iwl4965_init_alive_start(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005439 mutex_unlock(&priv->mutex);
5440}
5441
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005442static void iwl4965_bg_alive_start(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07005443{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005444 struct iwl_priv *priv =
5445 container_of(data, struct iwl_priv, alive_start.work);
Zhu Yib481de92007-09-25 17:54:57 -07005446
5447 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
5448 return;
5449
5450 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005451 iwl4965_alive_start(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005452 mutex_unlock(&priv->mutex);
5453}
5454
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005455static void iwl4965_bg_rf_kill(struct work_struct *work)
Zhu Yib481de92007-09-25 17:54:57 -07005456{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005457 struct iwl_priv *priv = container_of(work, struct iwl_priv, rf_kill);
Zhu Yib481de92007-09-25 17:54:57 -07005458
5459 wake_up_interruptible(&priv->wait_command_queue);
5460
5461 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
5462 return;
5463
5464 mutex_lock(&priv->mutex);
5465
Tomas Winklerfee12472008-04-03 16:05:21 -07005466 if (!iwl_is_rfkill(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07005467 IWL_DEBUG(IWL_DL_INFO | IWL_DL_RF_KILL,
5468 "HW and/or SW RF Kill no longer active, restarting "
5469 "device\n");
5470 if (!test_bit(STATUS_EXIT_PENDING, &priv->status))
5471 queue_work(priv->workqueue, &priv->restart);
5472 } else {
Mohamed Abbasad97edd2008-03-28 16:21:06 -07005473 /* make sure mac80211 stop sending Tx frame */
5474 if (priv->mac80211_registered)
5475 ieee80211_stop_queues(priv->hw);
Zhu Yib481de92007-09-25 17:54:57 -07005476
5477 if (!test_bit(STATUS_RF_KILL_HW, &priv->status))
5478 IWL_DEBUG_RF_KILL("Can not turn radio back on - "
5479 "disabled by SW switch\n");
5480 else
5481 IWL_WARNING("Radio Frequency Kill Switch is On:\n"
5482 "Kill switch must be turned off for "
5483 "wireless networking to work.\n");
5484 }
Mohamed Abbasad97edd2008-03-28 16:21:06 -07005485 iwl_rfkill_set_hw_state(priv);
5486
Zhu Yib481de92007-09-25 17:54:57 -07005487 mutex_unlock(&priv->mutex);
5488}
5489
5490#define IWL_SCAN_CHECK_WATCHDOG (7 * HZ)
5491
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005492static void iwl4965_bg_scan_check(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07005493{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005494 struct iwl_priv *priv =
5495 container_of(data, struct iwl_priv, scan_check.work);
Zhu Yib481de92007-09-25 17:54:57 -07005496
5497 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
5498 return;
5499
5500 mutex_lock(&priv->mutex);
5501 if (test_bit(STATUS_SCANNING, &priv->status) ||
5502 test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
5503 IWL_DEBUG(IWL_DL_INFO | IWL_DL_SCAN,
5504 "Scan completion watchdog resetting adapter (%dms)\n",
5505 jiffies_to_msecs(IWL_SCAN_CHECK_WATCHDOG));
mabbas052c4b92007-10-25 17:15:43 +08005506
Zhu Yib481de92007-09-25 17:54:57 -07005507 if (!test_bit(STATUS_EXIT_PENDING, &priv->status))
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005508 iwl4965_send_scan_abort(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005509 }
5510 mutex_unlock(&priv->mutex);
5511}
5512
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005513static void iwl4965_bg_request_scan(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07005514{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005515 struct iwl_priv *priv =
5516 container_of(data, struct iwl_priv, request_scan);
Tomas Winkler857485c2008-03-21 13:53:44 -07005517 struct iwl_host_cmd cmd = {
Zhu Yib481de92007-09-25 17:54:57 -07005518 .id = REPLY_SCAN_CMD,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005519 .len = sizeof(struct iwl4965_scan_cmd),
Zhu Yib481de92007-09-25 17:54:57 -07005520 .meta.flags = CMD_SIZE_HUGE,
5521 };
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005522 struct iwl4965_scan_cmd *scan;
Zhu Yib481de92007-09-25 17:54:57 -07005523 struct ieee80211_conf *conf = NULL;
Tomas Winkler78330fd2008-02-06 02:37:18 +02005524 u16 cmd_len;
Johannes Berg8318d782008-01-24 19:38:38 +01005525 enum ieee80211_band band;
Tomas Winkler78330fd2008-02-06 02:37:18 +02005526 u8 direct_mask;
Tomas Winkler857485c2008-03-21 13:53:44 -07005527 int ret = 0;
Zhu Yib481de92007-09-25 17:54:57 -07005528
5529 conf = ieee80211_get_hw_conf(priv->hw);
5530
5531 mutex_lock(&priv->mutex);
5532
Tomas Winklerfee12472008-04-03 16:05:21 -07005533 if (!iwl_is_ready(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07005534 IWL_WARNING("request scan called when driver not ready.\n");
5535 goto done;
5536 }
5537
5538 /* Make sure the scan wasn't cancelled before this queued work
5539 * was given the chance to run... */
5540 if (!test_bit(STATUS_SCANNING, &priv->status))
5541 goto done;
5542
5543 /* This should never be called or scheduled if there is currently
5544 * a scan active in the hardware. */
5545 if (test_bit(STATUS_SCAN_HW, &priv->status)) {
5546 IWL_DEBUG_INFO("Multiple concurrent scan requests in parallel. "
5547 "Ignoring second request.\n");
Tomas Winkler857485c2008-03-21 13:53:44 -07005548 ret = -EIO;
Zhu Yib481de92007-09-25 17:54:57 -07005549 goto done;
5550 }
5551
5552 if (test_bit(STATUS_EXIT_PENDING, &priv->status)) {
5553 IWL_DEBUG_SCAN("Aborting scan due to device shutdown\n");
5554 goto done;
5555 }
5556
5557 if (test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
5558 IWL_DEBUG_HC("Scan request while abort pending. Queuing.\n");
5559 goto done;
5560 }
5561
Tomas Winklerfee12472008-04-03 16:05:21 -07005562 if (iwl_is_rfkill(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07005563 IWL_DEBUG_HC("Aborting scan due to RF Kill activation\n");
5564 goto done;
5565 }
5566
5567 if (!test_bit(STATUS_READY, &priv->status)) {
5568 IWL_DEBUG_HC("Scan request while uninitialized. Queuing.\n");
5569 goto done;
5570 }
5571
5572 if (!priv->scan_bands) {
5573 IWL_DEBUG_HC("Aborting scan due to no requested bands\n");
5574 goto done;
5575 }
5576
5577 if (!priv->scan) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005578 priv->scan = kmalloc(sizeof(struct iwl4965_scan_cmd) +
Zhu Yib481de92007-09-25 17:54:57 -07005579 IWL_MAX_SCAN_SIZE, GFP_KERNEL);
5580 if (!priv->scan) {
Tomas Winkler857485c2008-03-21 13:53:44 -07005581 ret = -ENOMEM;
Zhu Yib481de92007-09-25 17:54:57 -07005582 goto done;
5583 }
5584 }
5585 scan = priv->scan;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005586 memset(scan, 0, sizeof(struct iwl4965_scan_cmd) + IWL_MAX_SCAN_SIZE);
Zhu Yib481de92007-09-25 17:54:57 -07005587
5588 scan->quiet_plcp_th = IWL_PLCP_QUIET_THRESH;
5589 scan->quiet_time = IWL_ACTIVE_QUIET_TIME;
5590
Tomas Winkler3109ece2008-03-28 16:33:35 -07005591 if (iwl_is_associated(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07005592 u16 interval = 0;
5593 u32 extra;
5594 u32 suspend_time = 100;
5595 u32 scan_suspend_time = 100;
5596 unsigned long flags;
5597
5598 IWL_DEBUG_INFO("Scanning while associated...\n");
5599
5600 spin_lock_irqsave(&priv->lock, flags);
5601 interval = priv->beacon_int;
5602 spin_unlock_irqrestore(&priv->lock, flags);
5603
5604 scan->suspend_time = 0;
mabbas052c4b92007-10-25 17:15:43 +08005605 scan->max_out_time = cpu_to_le32(200 * 1024);
Zhu Yib481de92007-09-25 17:54:57 -07005606 if (!interval)
5607 interval = suspend_time;
5608
5609 extra = (suspend_time / interval) << 22;
5610 scan_suspend_time = (extra |
5611 ((suspend_time % interval) * 1024));
5612 scan->suspend_time = cpu_to_le32(scan_suspend_time);
5613 IWL_DEBUG_SCAN("suspend_time 0x%X beacon interval %d\n",
5614 scan_suspend_time, interval);
5615 }
5616
5617 /* We should add the ability for user to lock to PASSIVE ONLY */
5618 if (priv->one_direct_scan) {
5619 IWL_DEBUG_SCAN
5620 ("Kicking off one direct scan for '%s'\n",
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005621 iwl4965_escape_essid(priv->direct_ssid,
Zhu Yib481de92007-09-25 17:54:57 -07005622 priv->direct_ssid_len));
5623 scan->direct_scan[0].id = WLAN_EID_SSID;
5624 scan->direct_scan[0].len = priv->direct_ssid_len;
5625 memcpy(scan->direct_scan[0].ssid,
5626 priv->direct_ssid, priv->direct_ssid_len);
5627 direct_mask = 1;
Tomas Winkler3109ece2008-03-28 16:33:35 -07005628 } else if (!iwl_is_associated(priv) && priv->essid_len) {
Bill Moss786b4552008-04-17 16:03:40 -07005629 IWL_DEBUG_SCAN
5630 ("Kicking off one direct scan for '%s' when not associated\n",
5631 iwl4965_escape_essid(priv->essid, priv->essid_len));
Zhu Yib481de92007-09-25 17:54:57 -07005632 scan->direct_scan[0].id = WLAN_EID_SSID;
5633 scan->direct_scan[0].len = priv->essid_len;
5634 memcpy(scan->direct_scan[0].ssid, priv->essid, priv->essid_len);
5635 direct_mask = 1;
Tomas Winkler857485c2008-03-21 13:53:44 -07005636 } else {
Bill Moss786b4552008-04-17 16:03:40 -07005637 IWL_DEBUG_SCAN("Kicking off one indirect scan.\n");
Zhu Yib481de92007-09-25 17:54:57 -07005638 direct_mask = 0;
Tomas Winkler857485c2008-03-21 13:53:44 -07005639 }
Zhu Yib481de92007-09-25 17:54:57 -07005640
Zhu Yib481de92007-09-25 17:54:57 -07005641 scan->tx_cmd.tx_flags = TX_CMD_FLG_SEQ_CTL_MSK;
Tomas Winkler5425e492008-04-15 16:01:38 -07005642 scan->tx_cmd.sta_id = priv->hw_params.bcast_sta_id;
Zhu Yib481de92007-09-25 17:54:57 -07005643 scan->tx_cmd.stop_time.life_time = TX_CMD_LIFE_TIME_INFINITE;
5644
Zhu Yib481de92007-09-25 17:54:57 -07005645
5646 switch (priv->scan_bands) {
5647 case 2:
5648 scan->flags = RXON_FLG_BAND_24G_MSK | RXON_FLG_AUTO_DETECT_MSK;
5649 scan->tx_cmd.rate_n_flags =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005650 iwl4965_hw_set_rate_n_flags(IWL_RATE_1M_PLCP,
Zhu Yib481de92007-09-25 17:54:57 -07005651 RATE_MCS_ANT_B_MSK|RATE_MCS_CCK_MSK);
5652
5653 scan->good_CRC_th = 0;
Johannes Berg8318d782008-01-24 19:38:38 +01005654 band = IEEE80211_BAND_2GHZ;
Zhu Yib481de92007-09-25 17:54:57 -07005655 break;
5656
5657 case 1:
5658 scan->tx_cmd.rate_n_flags =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005659 iwl4965_hw_set_rate_n_flags(IWL_RATE_6M_PLCP,
Zhu Yib481de92007-09-25 17:54:57 -07005660 RATE_MCS_ANT_B_MSK);
5661 scan->good_CRC_th = IWL_GOOD_CRC_TH;
Johannes Berg8318d782008-01-24 19:38:38 +01005662 band = IEEE80211_BAND_5GHZ;
Zhu Yib481de92007-09-25 17:54:57 -07005663 break;
5664
5665 default:
5666 IWL_WARNING("Invalid scan band count\n");
5667 goto done;
5668 }
5669
Tomas Winkler78330fd2008-02-06 02:37:18 +02005670 /* We don't build a direct scan probe request; the uCode will do
5671 * that based on the direct_mask added to each channel entry */
5672 cmd_len = iwl4965_fill_probe_req(priv, band,
5673 (struct ieee80211_mgmt *)scan->data,
5674 IWL_MAX_SCAN_SIZE - sizeof(*scan), 0);
5675
5676 scan->tx_cmd.len = cpu_to_le16(cmd_len);
Zhu Yib481de92007-09-25 17:54:57 -07005677 /* select Rx chains */
5678
5679 /* Force use of chains B and C (0x6) for scan Rx.
5680 * Avoid A (0x1) because of its off-channel reception on A-band.
5681 * MIMO is not used here, but value is required to make uCode happy. */
5682 scan->rx_chain = RXON_RX_CHAIN_DRIVER_FORCE_MSK |
5683 cpu_to_le16((0x7 << RXON_RX_CHAIN_VALID_POS) |
5684 (0x6 << RXON_RX_CHAIN_FORCE_SEL_POS) |
5685 (0x7 << RXON_RX_CHAIN_FORCE_MIMO_SEL_POS));
5686
5687 if (priv->iw_mode == IEEE80211_IF_TYPE_MNTR)
5688 scan->filter_flags = RXON_FILTER_PROMISC_MSK;
5689
Bill Moss786b4552008-04-17 16:03:40 -07005690 if (direct_mask)
Reinette Chatre26c0f032008-03-11 16:17:15 -07005691 scan->channel_count =
5692 iwl4965_get_channels_for_scan(
5693 priv, band, 1, /* active */
5694 direct_mask,
5695 (void *)&scan->data[le16_to_cpu(scan->tx_cmd.len)]);
Bill Moss786b4552008-04-17 16:03:40 -07005696 else
Reinette Chatre26c0f032008-03-11 16:17:15 -07005697 scan->channel_count =
5698 iwl4965_get_channels_for_scan(
5699 priv, band, 0, /* passive */
5700 direct_mask,
5701 (void *)&scan->data[le16_to_cpu(scan->tx_cmd.len)]);
Zhu Yib481de92007-09-25 17:54:57 -07005702
Mohamed Abbas5da4b552008-04-21 15:41:51 -07005703 scan->filter_flags |= (RXON_FILTER_ACCEPT_GRP_MSK |
5704 RXON_FILTER_BCON_AWARE_MSK);
Zhu Yib481de92007-09-25 17:54:57 -07005705 cmd.len += le16_to_cpu(scan->tx_cmd.len) +
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005706 scan->channel_count * sizeof(struct iwl4965_scan_channel);
Zhu Yib481de92007-09-25 17:54:57 -07005707 cmd.data = scan;
5708 scan->len = cpu_to_le16(cmd.len);
5709
5710 set_bit(STATUS_SCAN_HW, &priv->status);
Tomas Winkler857485c2008-03-21 13:53:44 -07005711 ret = iwl_send_cmd_sync(priv, &cmd);
5712 if (ret)
Zhu Yib481de92007-09-25 17:54:57 -07005713 goto done;
5714
5715 queue_delayed_work(priv->workqueue, &priv->scan_check,
5716 IWL_SCAN_CHECK_WATCHDOG);
5717
5718 mutex_unlock(&priv->mutex);
5719 return;
5720
5721 done:
Ian Schram01ebd062007-10-25 17:15:22 +08005722 /* inform mac80211 scan aborted */
Zhu Yib481de92007-09-25 17:54:57 -07005723 queue_work(priv->workqueue, &priv->scan_completed);
5724 mutex_unlock(&priv->mutex);
5725}
5726
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005727static void iwl4965_bg_up(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07005728{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005729 struct iwl_priv *priv = container_of(data, struct iwl_priv, up);
Zhu Yib481de92007-09-25 17:54:57 -07005730
5731 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
5732 return;
5733
5734 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005735 __iwl4965_up(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005736 mutex_unlock(&priv->mutex);
5737}
5738
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005739static void iwl4965_bg_restart(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07005740{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005741 struct iwl_priv *priv = container_of(data, struct iwl_priv, restart);
Zhu Yib481de92007-09-25 17:54:57 -07005742
5743 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
5744 return;
5745
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005746 iwl4965_down(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005747 queue_work(priv->workqueue, &priv->up);
5748}
5749
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005750static void iwl4965_bg_rx_replenish(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07005751{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005752 struct iwl_priv *priv =
5753 container_of(data, struct iwl_priv, rx_replenish);
Zhu Yib481de92007-09-25 17:54:57 -07005754
5755 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
5756 return;
5757
5758 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005759 iwl4965_rx_replenish(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005760 mutex_unlock(&priv->mutex);
5761}
5762
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08005763#define IWL_DELAY_NEXT_SCAN (HZ*2)
5764
Reinette Chatre508e32e2008-04-14 21:16:13 -07005765static void iwl4965_post_associate(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005766{
Zhu Yib481de92007-09-25 17:54:57 -07005767 struct ieee80211_conf *conf = NULL;
Tomas Winkler857485c2008-03-21 13:53:44 -07005768 int ret = 0;
Joe Perches0795af52007-10-03 17:59:30 -07005769 DECLARE_MAC_BUF(mac);
Zhu Yib481de92007-09-25 17:54:57 -07005770
5771 if (priv->iw_mode == IEEE80211_IF_TYPE_AP) {
5772 IWL_ERROR("%s Should not be called in AP mode\n", __FUNCTION__);
5773 return;
5774 }
5775
Joe Perches0795af52007-10-03 17:59:30 -07005776 IWL_DEBUG_ASSOC("Associated as %d to: %s\n",
5777 priv->assoc_id,
5778 print_mac(mac, priv->active_rxon.bssid_addr));
Zhu Yib481de92007-09-25 17:54:57 -07005779
5780
5781 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
5782 return;
5783
Zhu Yib481de92007-09-25 17:54:57 -07005784
Reinette Chatre508e32e2008-04-14 21:16:13 -07005785 if (!priv->vif || !priv->is_open)
Mohamed Abbas948c1712007-10-25 17:15:45 +08005786 return;
Reinette Chatre508e32e2008-04-14 21:16:13 -07005787
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005788 iwl4965_scan_cancel_timeout(priv, 200);
mabbas052c4b92007-10-25 17:15:43 +08005789
Zhu Yib481de92007-09-25 17:54:57 -07005790 conf = ieee80211_get_hw_conf(priv->hw);
5791
5792 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005793 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005794
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005795 memset(&priv->rxon_timing, 0, sizeof(struct iwl4965_rxon_time_cmd));
5796 iwl4965_setup_rxon_timing(priv);
Tomas Winkler857485c2008-03-21 13:53:44 -07005797 ret = iwl_send_cmd_pdu(priv, REPLY_RXON_TIMING,
Zhu Yib481de92007-09-25 17:54:57 -07005798 sizeof(priv->rxon_timing), &priv->rxon_timing);
Tomas Winkler857485c2008-03-21 13:53:44 -07005799 if (ret)
Zhu Yib481de92007-09-25 17:54:57 -07005800 IWL_WARNING("REPLY_RXON_TIMING failed - "
5801 "Attempting to continue.\n");
5802
5803 priv->staging_rxon.filter_flags |= RXON_FILTER_ASSOC_MSK;
5804
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08005805#ifdef CONFIG_IWL4965_HT
Ron Rindjunskyfd105e72007-11-26 16:14:39 +02005806 if (priv->current_ht_config.is_ht)
5807 iwl4965_set_rxon_ht(priv, &priv->current_ht_config);
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08005808#endif /* CONFIG_IWL4965_HT*/
Zhu Yib481de92007-09-25 17:54:57 -07005809 iwl4965_set_rxon_chain(priv);
5810 priv->staging_rxon.assoc_id = cpu_to_le16(priv->assoc_id);
5811
5812 IWL_DEBUG_ASSOC("assoc id %d beacon interval %d\n",
5813 priv->assoc_id, priv->beacon_int);
5814
5815 if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_PREAMBLE)
5816 priv->staging_rxon.flags |= RXON_FLG_SHORT_PREAMBLE_MSK;
5817 else
5818 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_PREAMBLE_MSK;
5819
5820 if (priv->staging_rxon.flags & RXON_FLG_BAND_24G_MSK) {
5821 if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_SLOT_TIME)
5822 priv->staging_rxon.flags |= RXON_FLG_SHORT_SLOT_MSK;
5823 else
5824 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
5825
5826 if (priv->iw_mode == IEEE80211_IF_TYPE_IBSS)
5827 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
5828
5829 }
5830
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005831 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005832
5833 switch (priv->iw_mode) {
5834 case IEEE80211_IF_TYPE_STA:
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005835 iwl4965_rate_scale_init(priv->hw, IWL_AP_ID);
Zhu Yib481de92007-09-25 17:54:57 -07005836 break;
5837
5838 case IEEE80211_IF_TYPE_IBSS:
5839
5840 /* clear out the station table */
Assaf Kraussbf85ea42008-03-14 10:38:49 -07005841 iwlcore_clear_stations_table(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005842
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005843 iwl4965_rxon_add_station(priv, iwl4965_broadcast_addr, 0);
5844 iwl4965_rxon_add_station(priv, priv->bssid, 0);
5845 iwl4965_rate_scale_init(priv->hw, IWL_STA_ID);
5846 iwl4965_send_beacon_cmd(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005847
5848 break;
5849
5850 default:
5851 IWL_ERROR("%s Should not be called in %d mode\n",
5852 __FUNCTION__, priv->iw_mode);
5853 break;
5854 }
5855
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005856 iwl4965_sequence_reset(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005857
Zhu Yib481de92007-09-25 17:54:57 -07005858 /* Enable Rx differential gain and sensitivity calibrations */
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -07005859 iwl_chain_noise_reset(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005860 priv->start_calib = 1;
Zhu Yib481de92007-09-25 17:54:57 -07005861
5862 if (priv->iw_mode == IEEE80211_IF_TYPE_IBSS)
5863 priv->assoc_station_added = 1;
5864
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005865 iwl4965_activate_qos(priv, 0);
Ron Rindjunsky292ae172008-02-06 11:20:39 -08005866
Mohamed Abbas5da4b552008-04-21 15:41:51 -07005867 iwl_power_update_mode(priv, 0);
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08005868 /* we have just associated, don't start scan too early */
5869 priv->next_scan_jiffies = jiffies + IWL_DELAY_NEXT_SCAN;
Reinette Chatre508e32e2008-04-14 21:16:13 -07005870}
5871
5872
5873static void iwl4965_bg_post_associate(struct work_struct *data)
5874{
5875 struct iwl_priv *priv = container_of(data, struct iwl_priv,
5876 post_associate.work);
5877
5878 mutex_lock(&priv->mutex);
5879 iwl4965_post_associate(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005880 mutex_unlock(&priv->mutex);
Reinette Chatre508e32e2008-04-14 21:16:13 -07005881
Zhu Yib481de92007-09-25 17:54:57 -07005882}
5883
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005884static void iwl4965_bg_abort_scan(struct work_struct *work)
Zhu Yib481de92007-09-25 17:54:57 -07005885{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005886 struct iwl_priv *priv = container_of(work, struct iwl_priv, abort_scan);
Zhu Yib481de92007-09-25 17:54:57 -07005887
Tomas Winklerfee12472008-04-03 16:05:21 -07005888 if (!iwl_is_ready(priv))
Zhu Yib481de92007-09-25 17:54:57 -07005889 return;
5890
5891 mutex_lock(&priv->mutex);
5892
5893 set_bit(STATUS_SCAN_ABORTING, &priv->status);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005894 iwl4965_send_scan_abort(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005895
5896 mutex_unlock(&priv->mutex);
5897}
5898
Zhu Yi76bb77e2007-11-22 10:53:22 +08005899static int iwl4965_mac_config(struct ieee80211_hw *hw, struct ieee80211_conf *conf);
5900
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005901static void iwl4965_bg_scan_completed(struct work_struct *work)
Zhu Yib481de92007-09-25 17:54:57 -07005902{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005903 struct iwl_priv *priv =
5904 container_of(work, struct iwl_priv, scan_completed);
Zhu Yib481de92007-09-25 17:54:57 -07005905
5906 IWL_DEBUG(IWL_DL_INFO | IWL_DL_SCAN, "SCAN complete scan\n");
5907
5908 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
5909 return;
5910
Zhu Yia0646472007-12-20 14:10:01 +08005911 if (test_bit(STATUS_CONF_PENDING, &priv->status))
5912 iwl4965_mac_config(priv->hw, ieee80211_get_hw_conf(priv->hw));
Zhu Yi76bb77e2007-11-22 10:53:22 +08005913
Zhu Yib481de92007-09-25 17:54:57 -07005914 ieee80211_scan_completed(priv->hw);
5915
5916 /* Since setting the TXPOWER may have been deferred while
5917 * performing the scan, fire one off */
5918 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005919 iwl4965_hw_reg_send_txpower(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005920 mutex_unlock(&priv->mutex);
5921}
5922
5923/*****************************************************************************
5924 *
5925 * mac80211 entry point functions
5926 *
5927 *****************************************************************************/
5928
Zhu Yi5a66926a2008-01-14 17:46:18 -08005929#define UCODE_READY_TIMEOUT (2 * HZ)
5930
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005931static int iwl4965_mac_start(struct ieee80211_hw *hw)
Zhu Yib481de92007-09-25 17:54:57 -07005932{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005933 struct iwl_priv *priv = hw->priv;
Zhu Yi5a66926a2008-01-14 17:46:18 -08005934 int ret;
Zhu Yib481de92007-09-25 17:54:57 -07005935
5936 IWL_DEBUG_MAC80211("enter\n");
5937
Zhu Yi5a66926a2008-01-14 17:46:18 -08005938 if (pci_enable_device(priv->pci_dev)) {
5939 IWL_ERROR("Fail to pci_enable_device\n");
5940 return -ENODEV;
5941 }
5942 pci_restore_state(priv->pci_dev);
5943 pci_enable_msi(priv->pci_dev);
5944
5945 ret = request_irq(priv->pci_dev->irq, iwl4965_isr, IRQF_SHARED,
5946 DRV_NAME, priv);
5947 if (ret) {
5948 IWL_ERROR("Error allocating IRQ %d\n", priv->pci_dev->irq);
5949 goto out_disable_msi;
5950 }
5951
Zhu Yib481de92007-09-25 17:54:57 -07005952 /* we should be verifying the device is ready to be opened */
5953 mutex_lock(&priv->mutex);
5954
Zhu Yi5a66926a2008-01-14 17:46:18 -08005955 memset(&priv->staging_rxon, 0, sizeof(struct iwl4965_rxon_cmd));
5956 /* fetch ucode file from disk, alloc and copy to bus-master buffers ...
5957 * ucode filename and max sizes are card-specific. */
5958
5959 if (!priv->ucode_code.len) {
5960 ret = iwl4965_read_ucode(priv);
5961 if (ret) {
5962 IWL_ERROR("Could not read microcode: %d\n", ret);
5963 mutex_unlock(&priv->mutex);
5964 goto out_release_irq;
5965 }
5966 }
5967
Zhu Yie655b9f2008-01-24 02:19:38 -08005968 ret = __iwl4965_up(priv);
Zhu Yi5a66926a2008-01-14 17:46:18 -08005969
Zhu Yib481de92007-09-25 17:54:57 -07005970 mutex_unlock(&priv->mutex);
Zhu Yi5a66926a2008-01-14 17:46:18 -08005971
Zhu Yie655b9f2008-01-24 02:19:38 -08005972 if (ret)
5973 goto out_release_irq;
5974
5975 IWL_DEBUG_INFO("Start UP work done.\n");
5976
5977 if (test_bit(STATUS_IN_SUSPEND, &priv->status))
5978 return 0;
5979
Zhu Yi5a66926a2008-01-14 17:46:18 -08005980 /* Wait for START_ALIVE from ucode. Otherwise callbacks from
5981 * mac80211 will not be run successfully. */
5982 ret = wait_event_interruptible_timeout(priv->wait_command_queue,
5983 test_bit(STATUS_READY, &priv->status),
5984 UCODE_READY_TIMEOUT);
5985 if (!ret) {
5986 if (!test_bit(STATUS_READY, &priv->status)) {
5987 IWL_ERROR("Wait for START_ALIVE timeout after %dms.\n",
5988 jiffies_to_msecs(UCODE_READY_TIMEOUT));
5989 ret = -ETIMEDOUT;
5990 goto out_release_irq;
5991 }
5992 }
5993
Zhu Yie655b9f2008-01-24 02:19:38 -08005994 priv->is_open = 1;
Zhu Yib481de92007-09-25 17:54:57 -07005995 IWL_DEBUG_MAC80211("leave\n");
5996 return 0;
Zhu Yi5a66926a2008-01-14 17:46:18 -08005997
5998out_release_irq:
5999 free_irq(priv->pci_dev->irq, priv);
6000out_disable_msi:
6001 pci_disable_msi(priv->pci_dev);
Zhu Yie655b9f2008-01-24 02:19:38 -08006002 pci_disable_device(priv->pci_dev);
6003 priv->is_open = 0;
6004 IWL_DEBUG_MAC80211("leave - failed\n");
Zhu Yi5a66926a2008-01-14 17:46:18 -08006005 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07006006}
6007
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006008static void iwl4965_mac_stop(struct ieee80211_hw *hw)
Zhu Yib481de92007-09-25 17:54:57 -07006009{
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");
Mohamed Abbas948c1712007-10-25 17:15:45 +08006013
Zhu Yie655b9f2008-01-24 02:19:38 -08006014 if (!priv->is_open) {
6015 IWL_DEBUG_MAC80211("leave - skip\n");
6016 return;
6017 }
6018
Zhu Yib481de92007-09-25 17:54:57 -07006019 priv->is_open = 0;
Zhu Yi5a66926a2008-01-14 17:46:18 -08006020
Tomas Winklerfee12472008-04-03 16:05:21 -07006021 if (iwl_is_ready_rf(priv)) {
Zhu Yie655b9f2008-01-24 02:19:38 -08006022 /* stop mac, cancel any scan request and clear
6023 * RXON_FILTER_ASSOC_MSK BIT
6024 */
Zhu Yi5a66926a2008-01-14 17:46:18 -08006025 mutex_lock(&priv->mutex);
6026 iwl4965_scan_cancel_timeout(priv, 100);
6027 cancel_delayed_work(&priv->post_associate);
Mohamed Abbasfde35712007-11-29 11:10:15 +08006028 mutex_unlock(&priv->mutex);
Mohamed Abbasfde35712007-11-29 11:10:15 +08006029 }
6030
Zhu Yi5a66926a2008-01-14 17:46:18 -08006031 iwl4965_down(priv);
6032
6033 flush_workqueue(priv->workqueue);
6034 free_irq(priv->pci_dev->irq, priv);
6035 pci_disable_msi(priv->pci_dev);
6036 pci_save_state(priv->pci_dev);
6037 pci_disable_device(priv->pci_dev);
Mohamed Abbas948c1712007-10-25 17:15:45 +08006038
Zhu Yib481de92007-09-25 17:54:57 -07006039 IWL_DEBUG_MAC80211("leave\n");
Zhu Yib481de92007-09-25 17:54:57 -07006040}
6041
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006042static int iwl4965_mac_tx(struct ieee80211_hw *hw, struct sk_buff *skb,
Zhu Yib481de92007-09-25 17:54:57 -07006043 struct ieee80211_tx_control *ctl)
6044{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006045 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07006046
6047 IWL_DEBUG_MAC80211("enter\n");
6048
6049 if (priv->iw_mode == IEEE80211_IF_TYPE_MNTR) {
6050 IWL_DEBUG_MAC80211("leave - monitor\n");
6051 return -1;
6052 }
6053
6054 IWL_DEBUG_TX("dev->xmit(%d bytes) at rate 0x%02x\n", skb->len,
Johannes Berg8318d782008-01-24 19:38:38 +01006055 ctl->tx_rate->bitrate);
Zhu Yib481de92007-09-25 17:54:57 -07006056
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006057 if (iwl4965_tx_skb(priv, skb, ctl))
Zhu Yib481de92007-09-25 17:54:57 -07006058 dev_kfree_skb_any(skb);
6059
6060 IWL_DEBUG_MAC80211("leave\n");
6061 return 0;
6062}
6063
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006064static int iwl4965_mac_add_interface(struct ieee80211_hw *hw,
Zhu Yib481de92007-09-25 17:54:57 -07006065 struct ieee80211_if_init_conf *conf)
6066{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006067 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07006068 unsigned long flags;
Joe Perches0795af52007-10-03 17:59:30 -07006069 DECLARE_MAC_BUF(mac);
Zhu Yib481de92007-09-25 17:54:57 -07006070
Johannes Berg32bfd352007-12-19 01:31:26 +01006071 IWL_DEBUG_MAC80211("enter: type %d\n", conf->type);
Zhu Yib481de92007-09-25 17:54:57 -07006072
Johannes Berg32bfd352007-12-19 01:31:26 +01006073 if (priv->vif) {
6074 IWL_DEBUG_MAC80211("leave - vif != NULL\n");
Reinette Chatre75849d22008-01-23 10:15:17 -08006075 return -EOPNOTSUPP;
Zhu Yib481de92007-09-25 17:54:57 -07006076 }
6077
6078 spin_lock_irqsave(&priv->lock, flags);
Johannes Berg32bfd352007-12-19 01:31:26 +01006079 priv->vif = conf->vif;
Zhu Yib481de92007-09-25 17:54:57 -07006080
6081 spin_unlock_irqrestore(&priv->lock, flags);
6082
6083 mutex_lock(&priv->mutex);
Tomas Winkler864792e2007-11-27 21:00:52 +02006084
6085 if (conf->mac_addr) {
6086 IWL_DEBUG_MAC80211("Set %s\n", print_mac(mac, conf->mac_addr));
6087 memcpy(priv->mac_addr, conf->mac_addr, ETH_ALEN);
6088 }
Zhu Yib481de92007-09-25 17:54:57 -07006089
Tomas Winklerfee12472008-04-03 16:05:21 -07006090 if (iwl_is_ready(priv))
Zhu Yi5a66926a2008-01-14 17:46:18 -08006091 iwl4965_set_mode(priv, conf->type);
6092
Zhu Yib481de92007-09-25 17:54:57 -07006093 mutex_unlock(&priv->mutex);
6094
Zhu Yi5a66926a2008-01-14 17:46:18 -08006095 IWL_DEBUG_MAC80211("leave\n");
Zhu Yib481de92007-09-25 17:54:57 -07006096 return 0;
6097}
6098
6099/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006100 * iwl4965_mac_config - mac80211 config callback
Zhu Yib481de92007-09-25 17:54:57 -07006101 *
6102 * We ignore conf->flags & IEEE80211_CONF_SHORT_SLOT_TIME since it seems to
6103 * be set inappropriately and the driver currently sets the hardware up to
6104 * use it whenever needed.
6105 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006106static int iwl4965_mac_config(struct ieee80211_hw *hw, struct ieee80211_conf *conf)
Zhu Yib481de92007-09-25 17:54:57 -07006107{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006108 struct iwl_priv *priv = hw->priv;
Assaf Kraussbf85ea42008-03-14 10:38:49 -07006109 const struct iwl_channel_info *ch_info;
Zhu Yib481de92007-09-25 17:54:57 -07006110 unsigned long flags;
Zhu Yi76bb77e2007-11-22 10:53:22 +08006111 int ret = 0;
Zhu Yib481de92007-09-25 17:54:57 -07006112
6113 mutex_lock(&priv->mutex);
Johannes Berg8318d782008-01-24 19:38:38 +01006114 IWL_DEBUG_MAC80211("enter to channel %d\n", conf->channel->hw_value);
Zhu Yib481de92007-09-25 17:54:57 -07006115
Zhu Yi12342c42007-12-20 11:27:32 +08006116 priv->add_radiotap = !!(conf->flags & IEEE80211_CONF_RADIOTAP);
6117
Tomas Winklerfee12472008-04-03 16:05:21 -07006118 if (!iwl_is_ready(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07006119 IWL_DEBUG_MAC80211("leave - not ready\n");
Zhu Yi76bb77e2007-11-22 10:53:22 +08006120 ret = -EIO;
6121 goto out;
Zhu Yib481de92007-09-25 17:54:57 -07006122 }
6123
Assaf Krauss1ea87392008-03-18 14:57:50 -07006124 if (unlikely(!priv->cfg->mod_params->disable_hw_scan &&
Zhu Yib481de92007-09-25 17:54:57 -07006125 test_bit(STATUS_SCANNING, &priv->status))) {
Zhu Yia0646472007-12-20 14:10:01 +08006126 IWL_DEBUG_MAC80211("leave - scanning\n");
6127 set_bit(STATUS_CONF_PENDING, &priv->status);
Zhu Yib481de92007-09-25 17:54:57 -07006128 mutex_unlock(&priv->mutex);
Zhu Yia0646472007-12-20 14:10:01 +08006129 return 0;
Zhu Yib481de92007-09-25 17:54:57 -07006130 }
6131
6132 spin_lock_irqsave(&priv->lock, flags);
6133
Assaf Krauss8622e702008-03-21 13:53:43 -07006134 ch_info = iwl_get_channel_info(priv, conf->channel->band,
Johannes Berg8318d782008-01-24 19:38:38 +01006135 ieee80211_frequency_to_channel(conf->channel->center_freq));
Zhu Yib481de92007-09-25 17:54:57 -07006136 if (!is_channel_valid(ch_info)) {
Zhu Yib481de92007-09-25 17:54:57 -07006137 IWL_DEBUG_MAC80211("leave - invalid channel\n");
6138 spin_unlock_irqrestore(&priv->lock, flags);
Zhu Yi76bb77e2007-11-22 10:53:22 +08006139 ret = -EINVAL;
6140 goto out;
Zhu Yib481de92007-09-25 17:54:57 -07006141 }
6142
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08006143#ifdef CONFIG_IWL4965_HT
Tomas Winkler78330fd2008-02-06 02:37:18 +02006144 /* if we are switching from ht to 2.4 clear flags
Zhu Yib481de92007-09-25 17:54:57 -07006145 * from any ht related info since 2.4 does not
6146 * support ht */
Tomas Winkler78330fd2008-02-06 02:37:18 +02006147 if ((le16_to_cpu(priv->staging_rxon.channel) != conf->channel->hw_value)
Zhu Yib481de92007-09-25 17:54:57 -07006148#ifdef IEEE80211_CONF_CHANNEL_SWITCH
6149 && !(conf->flags & IEEE80211_CONF_CHANNEL_SWITCH)
6150#endif
6151 )
6152 priv->staging_rxon.flags = 0;
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08006153#endif /* CONFIG_IWL4965_HT */
Zhu Yib481de92007-09-25 17:54:57 -07006154
Assaf Kraussbf85ea42008-03-14 10:38:49 -07006155 iwlcore_set_rxon_channel(priv, conf->channel->band,
Johannes Berg8318d782008-01-24 19:38:38 +01006156 ieee80211_frequency_to_channel(conf->channel->center_freq));
Zhu Yib481de92007-09-25 17:54:57 -07006157
Johannes Berg8318d782008-01-24 19:38:38 +01006158 iwl4965_set_flags_for_phymode(priv, conf->channel->band);
Zhu Yib481de92007-09-25 17:54:57 -07006159
6160 /* The list of supported rates and rate mask can be different
Johannes Berg8318d782008-01-24 19:38:38 +01006161 * for each band; since the band may have changed, reset
Zhu Yib481de92007-09-25 17:54:57 -07006162 * the rate mask to what mac80211 lists */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006163 iwl4965_set_rate(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006164
6165 spin_unlock_irqrestore(&priv->lock, flags);
6166
6167#ifdef IEEE80211_CONF_CHANNEL_SWITCH
6168 if (conf->flags & IEEE80211_CONF_CHANNEL_SWITCH) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006169 iwl4965_hw_channel_switch(priv, conf->channel);
Zhu Yi76bb77e2007-11-22 10:53:22 +08006170 goto out;
Zhu Yib481de92007-09-25 17:54:57 -07006171 }
6172#endif
6173
Mohamed Abbasad97edd2008-03-28 16:21:06 -07006174 if (priv->cfg->ops->lib->radio_kill_sw)
6175 priv->cfg->ops->lib->radio_kill_sw(priv, !conf->radio_enabled);
Zhu Yib481de92007-09-25 17:54:57 -07006176
6177 if (!conf->radio_enabled) {
6178 IWL_DEBUG_MAC80211("leave - radio disabled\n");
Zhu Yi76bb77e2007-11-22 10:53:22 +08006179 goto out;
Zhu Yib481de92007-09-25 17:54:57 -07006180 }
6181
Tomas Winklerfee12472008-04-03 16:05:21 -07006182 if (iwl_is_rfkill(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07006183 IWL_DEBUG_MAC80211("leave - RF kill\n");
Zhu Yi76bb77e2007-11-22 10:53:22 +08006184 ret = -EIO;
6185 goto out;
Zhu Yib481de92007-09-25 17:54:57 -07006186 }
6187
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006188 iwl4965_set_rate(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006189
6190 if (memcmp(&priv->active_rxon,
6191 &priv->staging_rxon, sizeof(priv->staging_rxon)))
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006192 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006193 else
6194 IWL_DEBUG_INFO("No re-sending same RXON configuration.\n");
6195
6196 IWL_DEBUG_MAC80211("leave\n");
6197
Zhu Yia0646472007-12-20 14:10:01 +08006198out:
6199 clear_bit(STATUS_CONF_PENDING, &priv->status);
Zhu Yi5a66926a2008-01-14 17:46:18 -08006200 mutex_unlock(&priv->mutex);
Zhu Yi76bb77e2007-11-22 10:53:22 +08006201 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07006202}
6203
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006204static void iwl4965_config_ap(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07006205{
Tomas Winkler857485c2008-03-21 13:53:44 -07006206 int ret = 0;
Zhu Yib481de92007-09-25 17:54:57 -07006207
Maarten Lankhorstd986bcd2008-01-23 10:15:16 -08006208 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
Zhu Yib481de92007-09-25 17:54:57 -07006209 return;
6210
6211 /* The following should be done only at AP bring up */
6212 if ((priv->active_rxon.filter_flags & RXON_FILTER_ASSOC_MSK) == 0) {
6213
6214 /* RXON - unassoc (to set timing command) */
6215 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006216 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006217
6218 /* RXON Timing */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006219 memset(&priv->rxon_timing, 0, sizeof(struct iwl4965_rxon_time_cmd));
6220 iwl4965_setup_rxon_timing(priv);
Tomas Winkler857485c2008-03-21 13:53:44 -07006221 ret = iwl_send_cmd_pdu(priv, REPLY_RXON_TIMING,
Zhu Yib481de92007-09-25 17:54:57 -07006222 sizeof(priv->rxon_timing), &priv->rxon_timing);
Tomas Winkler857485c2008-03-21 13:53:44 -07006223 if (ret)
Zhu Yib481de92007-09-25 17:54:57 -07006224 IWL_WARNING("REPLY_RXON_TIMING failed - "
6225 "Attempting to continue.\n");
6226
6227 iwl4965_set_rxon_chain(priv);
6228
6229 /* FIXME: what should be the assoc_id for AP? */
6230 priv->staging_rxon.assoc_id = cpu_to_le16(priv->assoc_id);
6231 if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_PREAMBLE)
6232 priv->staging_rxon.flags |=
6233 RXON_FLG_SHORT_PREAMBLE_MSK;
6234 else
6235 priv->staging_rxon.flags &=
6236 ~RXON_FLG_SHORT_PREAMBLE_MSK;
6237
6238 if (priv->staging_rxon.flags & RXON_FLG_BAND_24G_MSK) {
6239 if (priv->assoc_capability &
6240 WLAN_CAPABILITY_SHORT_SLOT_TIME)
6241 priv->staging_rxon.flags |=
6242 RXON_FLG_SHORT_SLOT_MSK;
6243 else
6244 priv->staging_rxon.flags &=
6245 ~RXON_FLG_SHORT_SLOT_MSK;
6246
6247 if (priv->iw_mode == IEEE80211_IF_TYPE_IBSS)
6248 priv->staging_rxon.flags &=
6249 ~RXON_FLG_SHORT_SLOT_MSK;
6250 }
6251 /* restore RXON assoc */
6252 priv->staging_rxon.filter_flags |= RXON_FILTER_ASSOC_MSK;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006253 iwl4965_commit_rxon(priv);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006254 iwl4965_activate_qos(priv, 1);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006255 iwl4965_rxon_add_station(priv, iwl4965_broadcast_addr, 0);
Zhu Yie1493de2007-09-27 11:27:32 +08006256 }
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006257 iwl4965_send_beacon_cmd(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006258
6259 /* FIXME - we need to add code here to detect a totally new
6260 * configuration, reset the AP, unassoc, rxon timing, assoc,
6261 * clear sta table, add BCAST sta... */
6262}
6263
Johannes Berg32bfd352007-12-19 01:31:26 +01006264static int iwl4965_mac_config_interface(struct ieee80211_hw *hw,
6265 struct ieee80211_vif *vif,
Zhu Yib481de92007-09-25 17:54:57 -07006266 struct ieee80211_if_conf *conf)
6267{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006268 struct iwl_priv *priv = hw->priv;
Joe Perches0795af52007-10-03 17:59:30 -07006269 DECLARE_MAC_BUF(mac);
Zhu Yib481de92007-09-25 17:54:57 -07006270 unsigned long flags;
6271 int rc;
6272
6273 if (conf == NULL)
6274 return -EIO;
6275
Emmanuel Grumbachb716bb92008-03-04 18:09:32 -08006276 if (priv->vif != vif) {
6277 IWL_DEBUG_MAC80211("leave - priv->vif != vif\n");
Emmanuel Grumbachb716bb92008-03-04 18:09:32 -08006278 return 0;
6279 }
6280
Zhu Yib481de92007-09-25 17:54:57 -07006281 if ((priv->iw_mode == IEEE80211_IF_TYPE_AP) &&
6282 (!conf->beacon || !conf->ssid_len)) {
6283 IWL_DEBUG_MAC80211
6284 ("Leaving in AP mode because HostAPD is not ready.\n");
6285 return 0;
6286 }
6287
Tomas Winklerfee12472008-04-03 16:05:21 -07006288 if (!iwl_is_alive(priv))
Zhu Yi5a66926a2008-01-14 17:46:18 -08006289 return -EAGAIN;
6290
Zhu Yib481de92007-09-25 17:54:57 -07006291 mutex_lock(&priv->mutex);
6292
Zhu Yib481de92007-09-25 17:54:57 -07006293 if (conf->bssid)
Joe Perches0795af52007-10-03 17:59:30 -07006294 IWL_DEBUG_MAC80211("bssid: %s\n",
6295 print_mac(mac, conf->bssid));
Zhu Yib481de92007-09-25 17:54:57 -07006296
Johannes Berg4150c572007-09-17 01:29:23 -04006297/*
6298 * very dubious code was here; the probe filtering flag is never set:
6299 *
Zhu Yib481de92007-09-25 17:54:57 -07006300 if (unlikely(test_bit(STATUS_SCANNING, &priv->status)) &&
6301 !(priv->hw->flags & IEEE80211_HW_NO_PROBE_FILTERING)) {
Johannes Berg4150c572007-09-17 01:29:23 -04006302 */
Zhu Yib481de92007-09-25 17:54:57 -07006303
6304 if (priv->iw_mode == IEEE80211_IF_TYPE_AP) {
6305 if (!conf->bssid) {
6306 conf->bssid = priv->mac_addr;
6307 memcpy(priv->bssid, priv->mac_addr, ETH_ALEN);
Joe Perches0795af52007-10-03 17:59:30 -07006308 IWL_DEBUG_MAC80211("bssid was set to: %s\n",
6309 print_mac(mac, conf->bssid));
Zhu Yib481de92007-09-25 17:54:57 -07006310 }
6311 if (priv->ibss_beacon)
6312 dev_kfree_skb(priv->ibss_beacon);
6313
6314 priv->ibss_beacon = conf->beacon;
6315 }
6316
Tomas Winklerfee12472008-04-03 16:05:21 -07006317 if (iwl_is_rfkill(priv))
Mohamed Abbasfde35712007-11-29 11:10:15 +08006318 goto done;
6319
Zhu Yib481de92007-09-25 17:54:57 -07006320 if (conf->bssid && !is_zero_ether_addr(conf->bssid) &&
6321 !is_multicast_ether_addr(conf->bssid)) {
6322 /* If there is currently a HW scan going on in the background
6323 * then we need to cancel it else the RXON below will fail. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006324 if (iwl4965_scan_cancel_timeout(priv, 100)) {
Zhu Yib481de92007-09-25 17:54:57 -07006325 IWL_WARNING("Aborted scan still in progress "
6326 "after 100ms\n");
6327 IWL_DEBUG_MAC80211("leaving - scan abort failed.\n");
6328 mutex_unlock(&priv->mutex);
6329 return -EAGAIN;
6330 }
6331 memcpy(priv->staging_rxon.bssid_addr, conf->bssid, ETH_ALEN);
6332
6333 /* TODO: Audit driver for usage of these members and see
6334 * if mac80211 deprecates them (priv->bssid looks like it
6335 * shouldn't be there, but I haven't scanned the IBSS code
6336 * to verify) - jpk */
6337 memcpy(priv->bssid, conf->bssid, ETH_ALEN);
6338
6339 if (priv->iw_mode == IEEE80211_IF_TYPE_AP)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006340 iwl4965_config_ap(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006341 else {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006342 rc = iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006343 if ((priv->iw_mode == IEEE80211_IF_TYPE_STA) && rc)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006344 iwl4965_rxon_add_station(
Zhu Yib481de92007-09-25 17:54:57 -07006345 priv, priv->active_rxon.bssid_addr, 1);
6346 }
6347
6348 } else {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006349 iwl4965_scan_cancel_timeout(priv, 100);
Zhu Yib481de92007-09-25 17:54:57 -07006350 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006351 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006352 }
6353
Mohamed Abbasfde35712007-11-29 11:10:15 +08006354 done:
Zhu Yib481de92007-09-25 17:54:57 -07006355 spin_lock_irqsave(&priv->lock, flags);
6356 if (!conf->ssid_len)
6357 memset(priv->essid, 0, IW_ESSID_MAX_SIZE);
6358 else
6359 memcpy(priv->essid, conf->ssid, conf->ssid_len);
6360
6361 priv->essid_len = conf->ssid_len;
6362 spin_unlock_irqrestore(&priv->lock, flags);
6363
6364 IWL_DEBUG_MAC80211("leave\n");
6365 mutex_unlock(&priv->mutex);
6366
6367 return 0;
6368}
6369
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006370static void iwl4965_configure_filter(struct ieee80211_hw *hw,
Johannes Berg4150c572007-09-17 01:29:23 -04006371 unsigned int changed_flags,
6372 unsigned int *total_flags,
6373 int mc_count, struct dev_addr_list *mc_list)
6374{
6375 /*
6376 * XXX: dummy
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006377 * see also iwl4965_connection_init_rx_config
Johannes Berg4150c572007-09-17 01:29:23 -04006378 */
6379 *total_flags = 0;
6380}
6381
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006382static void iwl4965_mac_remove_interface(struct ieee80211_hw *hw,
Zhu Yib481de92007-09-25 17:54:57 -07006383 struct ieee80211_if_init_conf *conf)
6384{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006385 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07006386
6387 IWL_DEBUG_MAC80211("enter\n");
6388
6389 mutex_lock(&priv->mutex);
Mohamed Abbas948c1712007-10-25 17:15:45 +08006390
Tomas Winklerfee12472008-04-03 16:05:21 -07006391 if (iwl_is_ready_rf(priv)) {
Mohamed Abbasfde35712007-11-29 11:10:15 +08006392 iwl4965_scan_cancel_timeout(priv, 100);
6393 cancel_delayed_work(&priv->post_associate);
6394 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
6395 iwl4965_commit_rxon(priv);
6396 }
Johannes Berg32bfd352007-12-19 01:31:26 +01006397 if (priv->vif == conf->vif) {
6398 priv->vif = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07006399 memset(priv->bssid, 0, ETH_ALEN);
6400 memset(priv->essid, 0, IW_ESSID_MAX_SIZE);
6401 priv->essid_len = 0;
6402 }
6403 mutex_unlock(&priv->mutex);
6404
6405 IWL_DEBUG_MAC80211("leave\n");
6406
6407}
Johannes Berg471b3ef2007-12-28 14:32:58 +01006408
Tomas Winkler3109ece2008-03-28 16:33:35 -07006409#define IWL_DELAY_NEXT_SCAN_AFTER_ASSOC (HZ*6)
Johannes Berg471b3ef2007-12-28 14:32:58 +01006410static void iwl4965_bss_info_changed(struct ieee80211_hw *hw,
6411 struct ieee80211_vif *vif,
6412 struct ieee80211_bss_conf *bss_conf,
6413 u32 changes)
Tomas Winkler220173b2007-10-16 00:50:25 +02006414{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006415 struct iwl_priv *priv = hw->priv;
Tomas Winkler220173b2007-10-16 00:50:25 +02006416
Tomas Winkler3109ece2008-03-28 16:33:35 -07006417 IWL_DEBUG_MAC80211("changes = 0x%X\n", changes);
6418
Johannes Berg471b3ef2007-12-28 14:32:58 +01006419 if (changes & BSS_CHANGED_ERP_PREAMBLE) {
Tomas Winkler3109ece2008-03-28 16:33:35 -07006420 IWL_DEBUG_MAC80211("ERP_PREAMBLE %d\n",
6421 bss_conf->use_short_preamble);
Johannes Berg471b3ef2007-12-28 14:32:58 +01006422 if (bss_conf->use_short_preamble)
Tomas Winkler220173b2007-10-16 00:50:25 +02006423 priv->staging_rxon.flags |= RXON_FLG_SHORT_PREAMBLE_MSK;
6424 else
6425 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_PREAMBLE_MSK;
6426 }
6427
Johannes Berg471b3ef2007-12-28 14:32:58 +01006428 if (changes & BSS_CHANGED_ERP_CTS_PROT) {
Tomas Winkler3109ece2008-03-28 16:33:35 -07006429 IWL_DEBUG_MAC80211("ERP_CTS %d\n", bss_conf->use_cts_prot);
Johannes Berg8318d782008-01-24 19:38:38 +01006430 if (bss_conf->use_cts_prot && (priv->band != IEEE80211_BAND_5GHZ))
Tomas Winkler220173b2007-10-16 00:50:25 +02006431 priv->staging_rxon.flags |= RXON_FLG_TGG_PROTECT_MSK;
6432 else
6433 priv->staging_rxon.flags &= ~RXON_FLG_TGG_PROTECT_MSK;
6434 }
6435
Tomas Winkler98952d52008-03-28 16:33:33 -07006436 if (changes & BSS_CHANGED_HT) {
Tomas Winkler3109ece2008-03-28 16:33:35 -07006437 IWL_DEBUG_MAC80211("HT %d\n", bss_conf->assoc_ht);
Tomas Winkler98952d52008-03-28 16:33:33 -07006438 iwl4965_ht_conf(priv, bss_conf);
6439 iwl4965_set_rxon_chain(priv);
6440 }
6441
Johannes Berg471b3ef2007-12-28 14:32:58 +01006442 if (changes & BSS_CHANGED_ASSOC) {
Tomas Winkler3109ece2008-03-28 16:33:35 -07006443 IWL_DEBUG_MAC80211("ASSOC %d\n", bss_conf->assoc);
Reinette Chatre508e32e2008-04-14 21:16:13 -07006444 /* This should never happen as this function should
6445 * never be called from interrupt context. */
6446 if (WARN_ON_ONCE(in_interrupt()))
6447 return;
Tomas Winkler3109ece2008-03-28 16:33:35 -07006448 if (bss_conf->assoc) {
6449 priv->assoc_id = bss_conf->aid;
6450 priv->beacon_int = bss_conf->beacon_int;
6451 priv->timestamp = bss_conf->timestamp;
6452 priv->assoc_capability = bss_conf->assoc_capability;
6453 priv->next_scan_jiffies = jiffies +
6454 IWL_DELAY_NEXT_SCAN_AFTER_ASSOC;
Reinette Chatre508e32e2008-04-14 21:16:13 -07006455 mutex_lock(&priv->mutex);
6456 iwl4965_post_associate(priv);
6457 mutex_unlock(&priv->mutex);
Tomas Winkler3109ece2008-03-28 16:33:35 -07006458 } else {
6459 priv->assoc_id = 0;
6460 IWL_DEBUG_MAC80211("DISASSOC %d\n", bss_conf->assoc);
6461 }
6462 } else if (changes && iwl_is_associated(priv) && priv->assoc_id) {
6463 IWL_DEBUG_MAC80211("Associated Changes %d\n", changes);
Tomas Winkler7e8c5192008-04-15 16:01:43 -07006464 iwl_send_rxon_assoc(priv);
Johannes Berg471b3ef2007-12-28 14:32:58 +01006465 }
6466
Tomas Winkler220173b2007-10-16 00:50:25 +02006467}
Zhu Yib481de92007-09-25 17:54:57 -07006468
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006469static int iwl4965_mac_hw_scan(struct ieee80211_hw *hw, u8 *ssid, size_t len)
Zhu Yib481de92007-09-25 17:54:57 -07006470{
6471 int rc = 0;
6472 unsigned long flags;
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006473 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07006474
6475 IWL_DEBUG_MAC80211("enter\n");
6476
mabbas052c4b92007-10-25 17:15:43 +08006477 mutex_lock(&priv->mutex);
Zhu Yib481de92007-09-25 17:54:57 -07006478 spin_lock_irqsave(&priv->lock, flags);
6479
Tomas Winklerfee12472008-04-03 16:05:21 -07006480 if (!iwl_is_ready_rf(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07006481 rc = -EIO;
6482 IWL_DEBUG_MAC80211("leave - not ready or exit pending\n");
6483 goto out_unlock;
6484 }
6485
6486 if (priv->iw_mode == IEEE80211_IF_TYPE_AP) { /* APs don't scan */
6487 rc = -EIO;
6488 IWL_ERROR("ERROR: APs don't scan\n");
6489 goto out_unlock;
6490 }
6491
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08006492 /* we don't schedule scan within next_scan_jiffies period */
6493 if (priv->next_scan_jiffies &&
6494 time_after(priv->next_scan_jiffies, jiffies)) {
6495 rc = -EAGAIN;
6496 goto out_unlock;
6497 }
Zhu Yib481de92007-09-25 17:54:57 -07006498 /* if we just finished scan ask for delay */
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08006499 if (priv->last_scan_jiffies && time_after(priv->last_scan_jiffies +
6500 IWL_DELAY_NEXT_SCAN, jiffies)) {
Zhu Yib481de92007-09-25 17:54:57 -07006501 rc = -EAGAIN;
6502 goto out_unlock;
6503 }
6504 if (len) {
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08006505 IWL_DEBUG_SCAN("direct scan for %s [%d]\n ",
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006506 iwl4965_escape_essid(ssid, len), (int)len);
Zhu Yib481de92007-09-25 17:54:57 -07006507
6508 priv->one_direct_scan = 1;
6509 priv->direct_ssid_len = (u8)
6510 min((u8) len, (u8) IW_ESSID_MAX_SIZE);
6511 memcpy(priv->direct_ssid, ssid, priv->direct_ssid_len);
Mohamed Abbas948c1712007-10-25 17:15:45 +08006512 } else
6513 priv->one_direct_scan = 0;
Zhu Yib481de92007-09-25 17:54:57 -07006514
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006515 rc = iwl4965_scan_initiate(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006516
6517 IWL_DEBUG_MAC80211("leave\n");
6518
6519out_unlock:
6520 spin_unlock_irqrestore(&priv->lock, flags);
mabbas052c4b92007-10-25 17:15:43 +08006521 mutex_unlock(&priv->mutex);
Zhu Yib481de92007-09-25 17:54:57 -07006522
6523 return rc;
6524}
6525
Emmanuel Grumbachab885f82008-03-20 15:06:43 +02006526static void iwl4965_mac_update_tkip_key(struct ieee80211_hw *hw,
6527 struct ieee80211_key_conf *keyconf, const u8 *addr,
6528 u32 iv32, u16 *phase1key)
6529{
6530 struct iwl_priv *priv = hw->priv;
6531 u8 sta_id = IWL_INVALID_STATION;
6532 unsigned long flags;
6533 __le16 key_flags = 0;
6534 int i;
6535 DECLARE_MAC_BUF(mac);
6536
6537 IWL_DEBUG_MAC80211("enter\n");
6538
Tomas Winkler947b13a2008-04-16 16:34:48 -07006539 sta_id = iwl_find_station(priv, addr);
Emmanuel Grumbachab885f82008-03-20 15:06:43 +02006540 if (sta_id == IWL_INVALID_STATION) {
6541 IWL_DEBUG_MAC80211("leave - %s not in station map.\n",
6542 print_mac(mac, addr));
6543 return;
6544 }
6545
6546 iwl4965_scan_cancel_timeout(priv, 100);
6547
6548 key_flags |= (STA_KEY_FLG_TKIP | STA_KEY_FLG_MAP_KEY_MSK);
6549 key_flags |= cpu_to_le16(keyconf->keyidx << STA_KEY_FLG_KEYID_POS);
6550 key_flags &= ~STA_KEY_FLG_INVALID;
6551
Tomas Winkler5425e492008-04-15 16:01:38 -07006552 if (sta_id == priv->hw_params.bcast_sta_id)
Emmanuel Grumbachab885f82008-03-20 15:06:43 +02006553 key_flags |= STA_KEY_MULTICAST_MSK;
6554
6555 spin_lock_irqsave(&priv->sta_lock, flags);
6556
Emmanuel Grumbachab885f82008-03-20 15:06:43 +02006557 priv->stations[sta_id].sta.key.key_flags = key_flags;
6558 priv->stations[sta_id].sta.key.tkip_rx_tsc_byte2 = (u8) iv32;
6559
6560 for (i = 0; i < 5; i++)
6561 priv->stations[sta_id].sta.key.tkip_rx_ttak[i] =
6562 cpu_to_le16(phase1key[i]);
6563
6564 priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
6565 priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
6566
6567 iwl4965_send_add_station(priv, &priv->stations[sta_id].sta, CMD_ASYNC);
6568
6569 spin_unlock_irqrestore(&priv->sta_lock, flags);
6570
6571 IWL_DEBUG_MAC80211("leave\n");
6572}
6573
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006574static int iwl4965_mac_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
Zhu Yib481de92007-09-25 17:54:57 -07006575 const u8 *local_addr, const u8 *addr,
6576 struct ieee80211_key_conf *key)
6577{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006578 struct iwl_priv *priv = hw->priv;
Joe Perches0795af52007-10-03 17:59:30 -07006579 DECLARE_MAC_BUF(mac);
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07006580 int ret = 0;
6581 u8 sta_id = IWL_INVALID_STATION;
Emmanuel Grumbach6974e362008-04-14 21:16:06 -07006582 u8 is_default_wep_key = 0;
Zhu Yib481de92007-09-25 17:54:57 -07006583
6584 IWL_DEBUG_MAC80211("enter\n");
6585
Ron Rindjunsky099b40b2008-04-21 15:41:53 -07006586 if (priv->hw_params.sw_crypto) {
Zhu Yib481de92007-09-25 17:54:57 -07006587 IWL_DEBUG_MAC80211("leave - hwcrypto disabled\n");
6588 return -EOPNOTSUPP;
6589 }
6590
6591 if (is_zero_ether_addr(addr))
6592 /* only support pairwise keys */
6593 return -EOPNOTSUPP;
6594
Tomas Winkler947b13a2008-04-16 16:34:48 -07006595 sta_id = iwl_find_station(priv, addr);
Emmanuel Grumbach6974e362008-04-14 21:16:06 -07006596 if (sta_id == IWL_INVALID_STATION) {
6597 IWL_DEBUG_MAC80211("leave - %s not in station map.\n",
6598 print_mac(mac, addr));
6599 return -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07006600
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07006601 }
Zhu Yib481de92007-09-25 17:54:57 -07006602
Emmanuel Grumbach6974e362008-04-14 21:16:06 -07006603 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006604 iwl4965_scan_cancel_timeout(priv, 100);
Emmanuel Grumbach6974e362008-04-14 21:16:06 -07006605 mutex_unlock(&priv->mutex);
6606
6607 /* If we are getting WEP group key and we didn't receive any key mapping
6608 * so far, we are in legacy wep mode (group key only), otherwise we are
6609 * in 1X mode.
6610 * In legacy wep mode, we use another host command to the uCode */
Tomas Winkler5425e492008-04-15 16:01:38 -07006611 if (key->alg == ALG_WEP && sta_id == priv->hw_params.bcast_sta_id &&
Emmanuel Grumbach6974e362008-04-14 21:16:06 -07006612 priv->iw_mode != IEEE80211_IF_TYPE_AP) {
6613 if (cmd == SET_KEY)
6614 is_default_wep_key = !priv->key_mapping_key;
6615 else
6616 is_default_wep_key = priv->default_wep_key;
6617 }
mabbas052c4b92007-10-25 17:15:43 +08006618
Zhu Yib481de92007-09-25 17:54:57 -07006619 switch (cmd) {
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07006620 case SET_KEY:
Emmanuel Grumbach6974e362008-04-14 21:16:06 -07006621 if (is_default_wep_key)
6622 ret = iwl_set_default_wep_key(priv, key);
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07006623 else
Emmanuel Grumbach74805132008-04-14 21:16:09 -07006624 ret = iwl_set_dynamic_key(priv, key, sta_id);
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07006625
6626 IWL_DEBUG_MAC80211("enable hwcrypto key\n");
Zhu Yib481de92007-09-25 17:54:57 -07006627 break;
6628 case DISABLE_KEY:
Emmanuel Grumbach6974e362008-04-14 21:16:06 -07006629 if (is_default_wep_key)
6630 ret = iwl_remove_default_wep_key(priv, key);
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07006631 else
Emmanuel Grumbach3ec47732008-04-17 16:03:36 -07006632 ret = iwl_remove_dynamic_key(priv, key, sta_id);
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07006633
6634 IWL_DEBUG_MAC80211("disable hwcrypto key\n");
Zhu Yib481de92007-09-25 17:54:57 -07006635 break;
6636 default:
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07006637 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07006638 }
6639
6640 IWL_DEBUG_MAC80211("leave\n");
Zhu Yib481de92007-09-25 17:54:57 -07006641
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07006642 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07006643}
6644
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006645static int iwl4965_mac_conf_tx(struct ieee80211_hw *hw, int queue,
Zhu Yib481de92007-09-25 17:54:57 -07006646 const struct ieee80211_tx_queue_params *params)
6647{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006648 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07006649 unsigned long flags;
6650 int q;
Zhu Yib481de92007-09-25 17:54:57 -07006651
6652 IWL_DEBUG_MAC80211("enter\n");
6653
Tomas Winklerfee12472008-04-03 16:05:21 -07006654 if (!iwl_is_ready_rf(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07006655 IWL_DEBUG_MAC80211("leave - RF not ready\n");
6656 return -EIO;
6657 }
6658
6659 if (queue >= AC_NUM) {
6660 IWL_DEBUG_MAC80211("leave - queue >= AC_NUM %d\n", queue);
6661 return 0;
6662 }
6663
Zhu Yib481de92007-09-25 17:54:57 -07006664 if (!priv->qos_data.qos_enable) {
6665 priv->qos_data.qos_active = 0;
6666 IWL_DEBUG_MAC80211("leave - qos not enabled\n");
6667 return 0;
6668 }
6669 q = AC_NUM - 1 - queue;
6670
6671 spin_lock_irqsave(&priv->lock, flags);
6672
6673 priv->qos_data.def_qos_parm.ac[q].cw_min = cpu_to_le16(params->cw_min);
6674 priv->qos_data.def_qos_parm.ac[q].cw_max = cpu_to_le16(params->cw_max);
6675 priv->qos_data.def_qos_parm.ac[q].aifsn = params->aifs;
6676 priv->qos_data.def_qos_parm.ac[q].edca_txop =
Johannes Berg3330d7b2008-02-10 16:49:38 +01006677 cpu_to_le16((params->txop * 32));
Zhu Yib481de92007-09-25 17:54:57 -07006678
6679 priv->qos_data.def_qos_parm.ac[q].reserved1 = 0;
6680 priv->qos_data.qos_active = 1;
6681
6682 spin_unlock_irqrestore(&priv->lock, flags);
6683
6684 mutex_lock(&priv->mutex);
6685 if (priv->iw_mode == IEEE80211_IF_TYPE_AP)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006686 iwl4965_activate_qos(priv, 1);
Tomas Winkler3109ece2008-03-28 16:33:35 -07006687 else if (priv->assoc_id && iwl_is_associated(priv))
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006688 iwl4965_activate_qos(priv, 0);
Zhu Yib481de92007-09-25 17:54:57 -07006689
6690 mutex_unlock(&priv->mutex);
6691
Zhu Yib481de92007-09-25 17:54:57 -07006692 IWL_DEBUG_MAC80211("leave\n");
6693 return 0;
6694}
6695
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006696static int iwl4965_mac_get_tx_stats(struct ieee80211_hw *hw,
Zhu Yib481de92007-09-25 17:54:57 -07006697 struct ieee80211_tx_queue_stats *stats)
6698{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006699 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07006700 int i, avail;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006701 struct iwl4965_tx_queue *txq;
6702 struct iwl4965_queue *q;
Zhu Yib481de92007-09-25 17:54:57 -07006703 unsigned long flags;
6704
6705 IWL_DEBUG_MAC80211("enter\n");
6706
Tomas Winklerfee12472008-04-03 16:05:21 -07006707 if (!iwl_is_ready_rf(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07006708 IWL_DEBUG_MAC80211("leave - RF not ready\n");
6709 return -EIO;
6710 }
6711
6712 spin_lock_irqsave(&priv->lock, flags);
6713
6714 for (i = 0; i < AC_NUM; i++) {
6715 txq = &priv->txq[i];
6716 q = &txq->q;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006717 avail = iwl4965_queue_space(q);
Zhu Yib481de92007-09-25 17:54:57 -07006718
6719 stats->data[i].len = q->n_window - avail;
6720 stats->data[i].limit = q->n_window - q->high_mark;
6721 stats->data[i].count = q->n_window;
6722
6723 }
6724 spin_unlock_irqrestore(&priv->lock, flags);
6725
6726 IWL_DEBUG_MAC80211("leave\n");
6727
6728 return 0;
6729}
6730
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006731static int iwl4965_mac_get_stats(struct ieee80211_hw *hw,
Zhu Yib481de92007-09-25 17:54:57 -07006732 struct ieee80211_low_level_stats *stats)
6733{
6734 IWL_DEBUG_MAC80211("enter\n");
6735 IWL_DEBUG_MAC80211("leave\n");
6736
6737 return 0;
6738}
6739
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006740static u64 iwl4965_mac_get_tsf(struct ieee80211_hw *hw)
Zhu Yib481de92007-09-25 17:54:57 -07006741{
6742 IWL_DEBUG_MAC80211("enter\n");
6743 IWL_DEBUG_MAC80211("leave\n");
6744
6745 return 0;
6746}
6747
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006748static void iwl4965_mac_reset_tsf(struct ieee80211_hw *hw)
Zhu Yib481de92007-09-25 17:54:57 -07006749{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006750 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07006751 unsigned long flags;
6752
6753 mutex_lock(&priv->mutex);
6754 IWL_DEBUG_MAC80211("enter\n");
6755
6756 priv->lq_mngr.lq_ready = 0;
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08006757#ifdef CONFIG_IWL4965_HT
Zhu Yib481de92007-09-25 17:54:57 -07006758 spin_lock_irqsave(&priv->lock, flags);
Ron Rindjunskyfd105e72007-11-26 16:14:39 +02006759 memset(&priv->current_ht_config, 0, sizeof(struct iwl_ht_info));
Zhu Yib481de92007-09-25 17:54:57 -07006760 spin_unlock_irqrestore(&priv->lock, flags);
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08006761#endif /* CONFIG_IWL4965_HT */
Zhu Yib481de92007-09-25 17:54:57 -07006762
Assaf Kraussbf85ea42008-03-14 10:38:49 -07006763 iwlcore_reset_qos(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006764
6765 cancel_delayed_work(&priv->post_associate);
6766
6767 spin_lock_irqsave(&priv->lock, flags);
6768 priv->assoc_id = 0;
6769 priv->assoc_capability = 0;
Zhu Yib481de92007-09-25 17:54:57 -07006770 priv->assoc_station_added = 0;
6771
6772 /* new association get rid of ibss beacon skb */
6773 if (priv->ibss_beacon)
6774 dev_kfree_skb(priv->ibss_beacon);
6775
6776 priv->ibss_beacon = NULL;
6777
6778 priv->beacon_int = priv->hw->conf.beacon_int;
Tomas Winkler3109ece2008-03-28 16:33:35 -07006779 priv->timestamp = 0;
Zhu Yib481de92007-09-25 17:54:57 -07006780 if ((priv->iw_mode == IEEE80211_IF_TYPE_STA))
6781 priv->beacon_int = 0;
6782
6783 spin_unlock_irqrestore(&priv->lock, flags);
6784
Tomas Winklerfee12472008-04-03 16:05:21 -07006785 if (!iwl_is_ready_rf(priv)) {
Mohamed Abbasfde35712007-11-29 11:10:15 +08006786 IWL_DEBUG_MAC80211("leave - not ready\n");
6787 mutex_unlock(&priv->mutex);
6788 return;
6789 }
6790
mabbas052c4b92007-10-25 17:15:43 +08006791 /* we are restarting association process
6792 * clear RXON_FILTER_ASSOC_MSK bit
6793 */
6794 if (priv->iw_mode != IEEE80211_IF_TYPE_AP) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006795 iwl4965_scan_cancel_timeout(priv, 100);
mabbas052c4b92007-10-25 17:15:43 +08006796 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006797 iwl4965_commit_rxon(priv);
mabbas052c4b92007-10-25 17:15:43 +08006798 }
6799
Mohamed Abbas5da4b552008-04-21 15:41:51 -07006800 iwl_power_update_mode(priv, 0);
6801
Zhu Yib481de92007-09-25 17:54:57 -07006802 /* Per mac80211.h: This is only used in IBSS mode... */
6803 if (priv->iw_mode != IEEE80211_IF_TYPE_IBSS) {
mabbas052c4b92007-10-25 17:15:43 +08006804
Zhu Yib481de92007-09-25 17:54:57 -07006805 IWL_DEBUG_MAC80211("leave - not in IBSS\n");
6806 mutex_unlock(&priv->mutex);
6807 return;
6808 }
6809
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006810 iwl4965_set_rate(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006811
6812 mutex_unlock(&priv->mutex);
6813
6814 IWL_DEBUG_MAC80211("leave\n");
Zhu Yib481de92007-09-25 17:54:57 -07006815}
6816
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006817static int iwl4965_mac_beacon_update(struct ieee80211_hw *hw, struct sk_buff *skb,
Zhu Yib481de92007-09-25 17:54:57 -07006818 struct ieee80211_tx_control *control)
6819{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006820 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07006821 unsigned long flags;
6822
6823 mutex_lock(&priv->mutex);
6824 IWL_DEBUG_MAC80211("enter\n");
6825
Tomas Winklerfee12472008-04-03 16:05:21 -07006826 if (!iwl_is_ready_rf(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07006827 IWL_DEBUG_MAC80211("leave - RF not ready\n");
6828 mutex_unlock(&priv->mutex);
6829 return -EIO;
6830 }
6831
6832 if (priv->iw_mode != IEEE80211_IF_TYPE_IBSS) {
6833 IWL_DEBUG_MAC80211("leave - not IBSS\n");
6834 mutex_unlock(&priv->mutex);
6835 return -EIO;
6836 }
6837
6838 spin_lock_irqsave(&priv->lock, flags);
6839
6840 if (priv->ibss_beacon)
6841 dev_kfree_skb(priv->ibss_beacon);
6842
6843 priv->ibss_beacon = skb;
6844
6845 priv->assoc_id = 0;
6846
6847 IWL_DEBUG_MAC80211("leave\n");
6848 spin_unlock_irqrestore(&priv->lock, flags);
6849
Assaf Kraussbf85ea42008-03-14 10:38:49 -07006850 iwlcore_reset_qos(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006851
6852 queue_work(priv->workqueue, &priv->post_associate.work);
6853
6854 mutex_unlock(&priv->mutex);
6855
6856 return 0;
6857}
6858
Zhu Yib481de92007-09-25 17:54:57 -07006859/*****************************************************************************
6860 *
6861 * sysfs attributes
6862 *
6863 *****************************************************************************/
6864
Tomas Winkler0a6857e2008-03-12 16:58:49 -07006865#ifdef CONFIG_IWLWIFI_DEBUG
Zhu Yib481de92007-09-25 17:54:57 -07006866
6867/*
6868 * The following adds a new attribute to the sysfs representation
6869 * of this device driver (i.e. a new file in /sys/bus/pci/drivers/iwl/)
6870 * used for controlling the debug level.
6871 *
6872 * See the level definitions in iwl for details.
6873 */
6874
6875static ssize_t show_debug_level(struct device_driver *d, char *buf)
6876{
Tomas Winkler0a6857e2008-03-12 16:58:49 -07006877 return sprintf(buf, "0x%08X\n", iwl_debug_level);
Zhu Yib481de92007-09-25 17:54:57 -07006878}
6879static ssize_t store_debug_level(struct device_driver *d,
6880 const char *buf, size_t count)
6881{
6882 char *p = (char *)buf;
6883 u32 val;
6884
6885 val = simple_strtoul(p, &p, 0);
6886 if (p == buf)
6887 printk(KERN_INFO DRV_NAME
6888 ": %s is not in hex or decimal form.\n", buf);
6889 else
Tomas Winkler0a6857e2008-03-12 16:58:49 -07006890 iwl_debug_level = val;
Zhu Yib481de92007-09-25 17:54:57 -07006891
6892 return strnlen(buf, count);
6893}
6894
6895static DRIVER_ATTR(debug_level, S_IWUSR | S_IRUGO,
6896 show_debug_level, store_debug_level);
6897
Tomas Winkler0a6857e2008-03-12 16:58:49 -07006898#endif /* CONFIG_IWLWIFI_DEBUG */
Zhu Yib481de92007-09-25 17:54:57 -07006899
Zhu Yib481de92007-09-25 17:54:57 -07006900
6901static ssize_t show_temperature(struct device *d,
6902 struct device_attribute *attr, char *buf)
6903{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006904 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07006905
Tomas Winklerfee12472008-04-03 16:05:21 -07006906 if (!iwl_is_alive(priv))
Zhu Yib481de92007-09-25 17:54:57 -07006907 return -EAGAIN;
6908
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006909 return sprintf(buf, "%d\n", iwl4965_hw_get_temperature(priv));
Zhu Yib481de92007-09-25 17:54:57 -07006910}
6911
6912static DEVICE_ATTR(temperature, S_IRUGO, show_temperature, NULL);
6913
6914static ssize_t show_rs_window(struct device *d,
6915 struct device_attribute *attr,
6916 char *buf)
6917{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006918 struct iwl_priv *priv = d->driver_data;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006919 return iwl4965_fill_rs_info(priv->hw, buf, IWL_AP_ID);
Zhu Yib481de92007-09-25 17:54:57 -07006920}
6921static DEVICE_ATTR(rs_window, S_IRUGO, show_rs_window, NULL);
6922
6923static ssize_t show_tx_power(struct device *d,
6924 struct device_attribute *attr, char *buf)
6925{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006926 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07006927 return sprintf(buf, "%d\n", priv->user_txpower_limit);
6928}
6929
6930static ssize_t store_tx_power(struct device *d,
6931 struct device_attribute *attr,
6932 const char *buf, size_t count)
6933{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006934 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07006935 char *p = (char *)buf;
6936 u32 val;
6937
6938 val = simple_strtoul(p, &p, 10);
6939 if (p == buf)
6940 printk(KERN_INFO DRV_NAME
6941 ": %s is not in decimal form.\n", buf);
6942 else
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006943 iwl4965_hw_reg_set_txpower(priv, val);
Zhu Yib481de92007-09-25 17:54:57 -07006944
6945 return count;
6946}
6947
6948static DEVICE_ATTR(tx_power, S_IWUSR | S_IRUGO, show_tx_power, store_tx_power);
6949
6950static ssize_t show_flags(struct device *d,
6951 struct device_attribute *attr, char *buf)
6952{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006953 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07006954
6955 return sprintf(buf, "0x%04X\n", priv->active_rxon.flags);
6956}
6957
6958static ssize_t store_flags(struct device *d,
6959 struct device_attribute *attr,
6960 const char *buf, size_t count)
6961{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006962 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07006963 u32 flags = simple_strtoul(buf, NULL, 0);
6964
6965 mutex_lock(&priv->mutex);
6966 if (le32_to_cpu(priv->staging_rxon.flags) != flags) {
6967 /* Cancel any currently running scans... */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006968 if (iwl4965_scan_cancel_timeout(priv, 100))
Zhu Yib481de92007-09-25 17:54:57 -07006969 IWL_WARNING("Could not cancel scan.\n");
6970 else {
6971 IWL_DEBUG_INFO("Committing rxon.flags = 0x%04X\n",
6972 flags);
6973 priv->staging_rxon.flags = cpu_to_le32(flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006974 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006975 }
6976 }
6977 mutex_unlock(&priv->mutex);
6978
6979 return count;
6980}
6981
6982static DEVICE_ATTR(flags, S_IWUSR | S_IRUGO, show_flags, store_flags);
6983
6984static ssize_t show_filter_flags(struct device *d,
6985 struct device_attribute *attr, char *buf)
6986{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006987 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07006988
6989 return sprintf(buf, "0x%04X\n",
6990 le32_to_cpu(priv->active_rxon.filter_flags));
6991}
6992
6993static ssize_t store_filter_flags(struct device *d,
6994 struct device_attribute *attr,
6995 const char *buf, size_t count)
6996{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006997 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07006998 u32 filter_flags = simple_strtoul(buf, NULL, 0);
6999
7000 mutex_lock(&priv->mutex);
7001 if (le32_to_cpu(priv->staging_rxon.filter_flags) != filter_flags) {
7002 /* Cancel any currently running scans... */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007003 if (iwl4965_scan_cancel_timeout(priv, 100))
Zhu Yib481de92007-09-25 17:54:57 -07007004 IWL_WARNING("Could not cancel scan.\n");
7005 else {
7006 IWL_DEBUG_INFO("Committing rxon.filter_flags = "
7007 "0x%04X\n", filter_flags);
7008 priv->staging_rxon.filter_flags =
7009 cpu_to_le32(filter_flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007010 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007011 }
7012 }
7013 mutex_unlock(&priv->mutex);
7014
7015 return count;
7016}
7017
7018static DEVICE_ATTR(filter_flags, S_IWUSR | S_IRUGO, show_filter_flags,
7019 store_filter_flags);
7020
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08007021#ifdef CONFIG_IWL4965_SPECTRUM_MEASUREMENT
Zhu Yib481de92007-09-25 17:54:57 -07007022
7023static ssize_t show_measurement(struct device *d,
7024 struct device_attribute *attr, char *buf)
7025{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007026 struct iwl_priv *priv = dev_get_drvdata(d);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007027 struct iwl4965_spectrum_notification measure_report;
Zhu Yib481de92007-09-25 17:54:57 -07007028 u32 size = sizeof(measure_report), len = 0, ofs = 0;
7029 u8 *data = (u8 *) & measure_report;
7030 unsigned long flags;
7031
7032 spin_lock_irqsave(&priv->lock, flags);
7033 if (!(priv->measurement_status & MEASUREMENT_READY)) {
7034 spin_unlock_irqrestore(&priv->lock, flags);
7035 return 0;
7036 }
7037 memcpy(&measure_report, &priv->measure_report, size);
7038 priv->measurement_status = 0;
7039 spin_unlock_irqrestore(&priv->lock, flags);
7040
7041 while (size && (PAGE_SIZE - len)) {
7042 hex_dump_to_buffer(data + ofs, size, 16, 1, buf + len,
7043 PAGE_SIZE - len, 1);
7044 len = strlen(buf);
7045 if (PAGE_SIZE - len)
7046 buf[len++] = '\n';
7047
7048 ofs += 16;
7049 size -= min(size, 16U);
7050 }
7051
7052 return len;
7053}
7054
7055static ssize_t store_measurement(struct device *d,
7056 struct device_attribute *attr,
7057 const char *buf, size_t count)
7058{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007059 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07007060 struct ieee80211_measurement_params params = {
7061 .channel = le16_to_cpu(priv->active_rxon.channel),
7062 .start_time = cpu_to_le64(priv->last_tsf),
7063 .duration = cpu_to_le16(1),
7064 };
7065 u8 type = IWL_MEASURE_BASIC;
7066 u8 buffer[32];
7067 u8 channel;
7068
7069 if (count) {
7070 char *p = buffer;
7071 strncpy(buffer, buf, min(sizeof(buffer), count));
7072 channel = simple_strtoul(p, NULL, 0);
7073 if (channel)
7074 params.channel = channel;
7075
7076 p = buffer;
7077 while (*p && *p != ' ')
7078 p++;
7079 if (*p)
7080 type = simple_strtoul(p + 1, NULL, 0);
7081 }
7082
7083 IWL_DEBUG_INFO("Invoking measurement of type %d on "
7084 "channel %d (for '%s')\n", type, params.channel, buf);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007085 iwl4965_get_measurement(priv, &params, type);
Zhu Yib481de92007-09-25 17:54:57 -07007086
7087 return count;
7088}
7089
7090static DEVICE_ATTR(measurement, S_IRUSR | S_IWUSR,
7091 show_measurement, store_measurement);
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08007092#endif /* CONFIG_IWL4965_SPECTRUM_MEASUREMENT */
Zhu Yib481de92007-09-25 17:54:57 -07007093
7094static ssize_t store_retry_rate(struct device *d,
7095 struct device_attribute *attr,
7096 const char *buf, size_t count)
7097{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007098 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07007099
7100 priv->retry_rate = simple_strtoul(buf, NULL, 0);
7101 if (priv->retry_rate <= 0)
7102 priv->retry_rate = 1;
7103
7104 return count;
7105}
7106
7107static ssize_t show_retry_rate(struct device *d,
7108 struct device_attribute *attr, char *buf)
7109{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007110 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07007111 return sprintf(buf, "%d", priv->retry_rate);
7112}
7113
7114static DEVICE_ATTR(retry_rate, S_IWUSR | S_IRUSR, show_retry_rate,
7115 store_retry_rate);
7116
7117static ssize_t store_power_level(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 int rc;
7123 int mode;
7124
7125 mode = simple_strtoul(buf, NULL, 0);
7126 mutex_lock(&priv->mutex);
7127
Tomas Winklerfee12472008-04-03 16:05:21 -07007128 if (!iwl_is_ready(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07007129 rc = -EAGAIN;
7130 goto out;
7131 }
7132
Mohamed Abbas5da4b552008-04-21 15:41:51 -07007133 rc = iwl_power_set_user_mode(priv, mode);
7134 if (rc) {
7135 IWL_DEBUG_MAC80211("failed setting power mode.\n");
7136 goto out;
Zhu Yib481de92007-09-25 17:54:57 -07007137 }
Zhu Yib481de92007-09-25 17:54:57 -07007138 rc = count;
7139
7140 out:
7141 mutex_unlock(&priv->mutex);
7142 return rc;
7143}
7144
7145#define MAX_WX_STRING 80
7146
7147/* Values are in microsecond */
7148static const s32 timeout_duration[] = {
7149 350000,
7150 250000,
7151 75000,
7152 37000,
7153 25000,
7154};
7155static const s32 period_duration[] = {
7156 400000,
7157 700000,
7158 1000000,
7159 1000000,
7160 1000000
7161};
7162
7163static ssize_t show_power_level(struct device *d,
7164 struct device_attribute *attr, char *buf)
7165{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007166 struct iwl_priv *priv = dev_get_drvdata(d);
Mohamed Abbas5da4b552008-04-21 15:41:51 -07007167 int level = priv->power_data.power_mode;
Zhu Yib481de92007-09-25 17:54:57 -07007168 char *p = buf;
7169
7170 p += sprintf(p, "%d ", level);
7171 switch (level) {
7172 case IWL_POWER_MODE_CAM:
7173 case IWL_POWER_AC:
7174 p += sprintf(p, "(AC)");
7175 break;
7176 case IWL_POWER_BATTERY:
7177 p += sprintf(p, "(BATTERY)");
7178 break;
7179 default:
7180 p += sprintf(p,
7181 "(Timeout %dms, Period %dms)",
7182 timeout_duration[level - 1] / 1000,
7183 period_duration[level - 1] / 1000);
7184 }
Mohamed Abbas5da4b552008-04-21 15:41:51 -07007185/*
Zhu Yib481de92007-09-25 17:54:57 -07007186 if (!(priv->power_mode & IWL_POWER_ENABLED))
7187 p += sprintf(p, " OFF\n");
7188 else
7189 p += sprintf(p, " \n");
Mohamed Abbas5da4b552008-04-21 15:41:51 -07007190*/
7191 p += sprintf(p, " \n");
Zhu Yib481de92007-09-25 17:54:57 -07007192 return (p - buf + 1);
Zhu Yib481de92007-09-25 17:54:57 -07007193}
7194
7195static DEVICE_ATTR(power_level, S_IWUSR | S_IRUSR, show_power_level,
7196 store_power_level);
7197
7198static ssize_t show_channels(struct device *d,
7199 struct device_attribute *attr, char *buf)
7200{
Johannes Berg8318d782008-01-24 19:38:38 +01007201 /* all this shit doesn't belong into sysfs anyway */
7202 return 0;
Zhu Yib481de92007-09-25 17:54:57 -07007203}
7204
7205static DEVICE_ATTR(channels, S_IRUSR, show_channels, NULL);
7206
7207static ssize_t show_statistics(struct device *d,
7208 struct device_attribute *attr, char *buf)
7209{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007210 struct iwl_priv *priv = dev_get_drvdata(d);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007211 u32 size = sizeof(struct iwl4965_notif_statistics);
Zhu Yib481de92007-09-25 17:54:57 -07007212 u32 len = 0, ofs = 0;
7213 u8 *data = (u8 *) & priv->statistics;
7214 int rc = 0;
7215
Tomas Winklerfee12472008-04-03 16:05:21 -07007216 if (!iwl_is_alive(priv))
Zhu Yib481de92007-09-25 17:54:57 -07007217 return -EAGAIN;
7218
7219 mutex_lock(&priv->mutex);
Emmanuel Grumbach49ea8592008-04-15 16:01:37 -07007220 rc = iwl_send_statistics_request(priv, 0);
Zhu Yib481de92007-09-25 17:54:57 -07007221 mutex_unlock(&priv->mutex);
7222
7223 if (rc) {
7224 len = sprintf(buf,
7225 "Error sending statistics request: 0x%08X\n", rc);
7226 return len;
7227 }
7228
7229 while (size && (PAGE_SIZE - len)) {
7230 hex_dump_to_buffer(data + ofs, size, 16, 1, buf + len,
7231 PAGE_SIZE - len, 1);
7232 len = strlen(buf);
7233 if (PAGE_SIZE - len)
7234 buf[len++] = '\n';
7235
7236 ofs += 16;
7237 size -= min(size, 16U);
7238 }
7239
7240 return len;
7241}
7242
7243static DEVICE_ATTR(statistics, S_IRUGO, show_statistics, NULL);
7244
Zhu Yib481de92007-09-25 17:54:57 -07007245static ssize_t show_status(struct device *d,
7246 struct device_attribute *attr, char *buf)
7247{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007248 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Tomas Winklerfee12472008-04-03 16:05:21 -07007249 if (!iwl_is_alive(priv))
Zhu Yib481de92007-09-25 17:54:57 -07007250 return -EAGAIN;
7251 return sprintf(buf, "0x%08x\n", (int)priv->status);
7252}
7253
7254static DEVICE_ATTR(status, S_IRUGO, show_status, NULL);
7255
7256static ssize_t dump_error_log(struct device *d,
7257 struct device_attribute *attr,
7258 const char *buf, size_t count)
7259{
7260 char *p = (char *)buf;
7261
7262 if (p[0] == '1')
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007263 iwl4965_dump_nic_error_log((struct iwl_priv *)d->driver_data);
Zhu Yib481de92007-09-25 17:54:57 -07007264
7265 return strnlen(buf, count);
7266}
7267
7268static DEVICE_ATTR(dump_errors, S_IWUSR, NULL, dump_error_log);
7269
7270static ssize_t dump_event_log(struct device *d,
7271 struct device_attribute *attr,
7272 const char *buf, size_t count)
7273{
7274 char *p = (char *)buf;
7275
7276 if (p[0] == '1')
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007277 iwl4965_dump_nic_event_log((struct iwl_priv *)d->driver_data);
Zhu Yib481de92007-09-25 17:54:57 -07007278
7279 return strnlen(buf, count);
7280}
7281
7282static DEVICE_ATTR(dump_events, S_IWUSR, NULL, dump_event_log);
7283
7284/*****************************************************************************
7285 *
7286 * driver setup and teardown
7287 *
7288 *****************************************************************************/
7289
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007290static void iwl4965_setup_deferred_work(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07007291{
7292 priv->workqueue = create_workqueue(DRV_NAME);
7293
7294 init_waitqueue_head(&priv->wait_command_queue);
7295
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007296 INIT_WORK(&priv->up, iwl4965_bg_up);
7297 INIT_WORK(&priv->restart, iwl4965_bg_restart);
7298 INIT_WORK(&priv->rx_replenish, iwl4965_bg_rx_replenish);
7299 INIT_WORK(&priv->scan_completed, iwl4965_bg_scan_completed);
7300 INIT_WORK(&priv->request_scan, iwl4965_bg_request_scan);
7301 INIT_WORK(&priv->abort_scan, iwl4965_bg_abort_scan);
7302 INIT_WORK(&priv->rf_kill, iwl4965_bg_rf_kill);
7303 INIT_WORK(&priv->beacon_update, iwl4965_bg_beacon_update);
7304 INIT_DELAYED_WORK(&priv->post_associate, iwl4965_bg_post_associate);
7305 INIT_DELAYED_WORK(&priv->init_alive_start, iwl4965_bg_init_alive_start);
7306 INIT_DELAYED_WORK(&priv->alive_start, iwl4965_bg_alive_start);
7307 INIT_DELAYED_WORK(&priv->scan_check, iwl4965_bg_scan_check);
Zhu Yib481de92007-09-25 17:54:57 -07007308
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007309 iwl4965_hw_setup_deferred_work(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007310
7311 tasklet_init(&priv->irq_tasklet, (void (*)(unsigned long))
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007312 iwl4965_irq_tasklet, (unsigned long)priv);
Zhu Yib481de92007-09-25 17:54:57 -07007313}
7314
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007315static void iwl4965_cancel_deferred_work(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07007316{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007317 iwl4965_hw_cancel_deferred_work(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007318
Joonwoo Park3ae6a052007-11-29 10:43:16 +09007319 cancel_delayed_work_sync(&priv->init_alive_start);
Zhu Yib481de92007-09-25 17:54:57 -07007320 cancel_delayed_work(&priv->scan_check);
7321 cancel_delayed_work(&priv->alive_start);
7322 cancel_delayed_work(&priv->post_associate);
7323 cancel_work_sync(&priv->beacon_update);
7324}
7325
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007326static struct attribute *iwl4965_sysfs_entries[] = {
Zhu Yib481de92007-09-25 17:54:57 -07007327 &dev_attr_channels.attr,
7328 &dev_attr_dump_errors.attr,
7329 &dev_attr_dump_events.attr,
7330 &dev_attr_flags.attr,
7331 &dev_attr_filter_flags.attr,
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08007332#ifdef CONFIG_IWL4965_SPECTRUM_MEASUREMENT
Zhu Yib481de92007-09-25 17:54:57 -07007333 &dev_attr_measurement.attr,
7334#endif
7335 &dev_attr_power_level.attr,
7336 &dev_attr_retry_rate.attr,
Zhu Yib481de92007-09-25 17:54:57 -07007337 &dev_attr_rs_window.attr,
7338 &dev_attr_statistics.attr,
7339 &dev_attr_status.attr,
7340 &dev_attr_temperature.attr,
Zhu Yib481de92007-09-25 17:54:57 -07007341 &dev_attr_tx_power.attr,
7342
7343 NULL
7344};
7345
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007346static struct attribute_group iwl4965_attribute_group = {
Zhu Yib481de92007-09-25 17:54:57 -07007347 .name = NULL, /* put in device directory */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007348 .attrs = iwl4965_sysfs_entries,
Zhu Yib481de92007-09-25 17:54:57 -07007349};
7350
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007351static struct ieee80211_ops iwl4965_hw_ops = {
7352 .tx = iwl4965_mac_tx,
7353 .start = iwl4965_mac_start,
7354 .stop = iwl4965_mac_stop,
7355 .add_interface = iwl4965_mac_add_interface,
7356 .remove_interface = iwl4965_mac_remove_interface,
7357 .config = iwl4965_mac_config,
7358 .config_interface = iwl4965_mac_config_interface,
7359 .configure_filter = iwl4965_configure_filter,
7360 .set_key = iwl4965_mac_set_key,
Emmanuel Grumbachab885f82008-03-20 15:06:43 +02007361 .update_tkip_key = iwl4965_mac_update_tkip_key,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007362 .get_stats = iwl4965_mac_get_stats,
7363 .get_tx_stats = iwl4965_mac_get_tx_stats,
7364 .conf_tx = iwl4965_mac_conf_tx,
7365 .get_tsf = iwl4965_mac_get_tsf,
7366 .reset_tsf = iwl4965_mac_reset_tsf,
7367 .beacon_update = iwl4965_mac_beacon_update,
Johannes Berg471b3ef2007-12-28 14:32:58 +01007368 .bss_info_changed = iwl4965_bss_info_changed,
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08007369#ifdef CONFIG_IWL4965_HT
Ron Rindjunsky9ab46172007-12-25 17:00:38 +02007370 .ampdu_action = iwl4965_mac_ampdu_action,
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08007371#endif /* CONFIG_IWL4965_HT */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007372 .hw_scan = iwl4965_mac_hw_scan
Zhu Yib481de92007-09-25 17:54:57 -07007373};
7374
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007375static int iwl4965_pci_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
Zhu Yib481de92007-09-25 17:54:57 -07007376{
7377 int err = 0;
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007378 struct iwl_priv *priv;
Zhu Yib481de92007-09-25 17:54:57 -07007379 struct ieee80211_hw *hw;
Tomas Winkler82b9a122008-03-04 18:09:30 -08007380 struct iwl_cfg *cfg = (struct iwl_cfg *)(ent->driver_data);
Mohamed Abbas0359fac2008-03-28 16:21:08 -07007381 unsigned long flags;
Zhu Yi5a66926a2008-01-14 17:46:18 -08007382 DECLARE_MAC_BUF(mac);
Zhu Yib481de92007-09-25 17:54:57 -07007383
Assaf Krauss316c30d2008-03-14 10:38:46 -07007384 /************************
7385 * 1. Allocating HW data
7386 ************************/
7387
Cahill, Ben M6440adb2007-11-29 11:09:55 +08007388 /* Disabling hardware scan means that mac80211 will perform scans
7389 * "the hard way", rather than using device's scan. */
Assaf Krauss1ea87392008-03-18 14:57:50 -07007390 if (cfg->mod_params->disable_hw_scan) {
Zhu Yib481de92007-09-25 17:54:57 -07007391 IWL_DEBUG_INFO("Disabling hw_scan\n");
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007392 iwl4965_hw_ops.hw_scan = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07007393 }
7394
Assaf Krauss1d0a0822008-03-14 10:38:48 -07007395 hw = iwl_alloc_all(cfg, &iwl4965_hw_ops);
7396 if (!hw) {
Zhu Yib481de92007-09-25 17:54:57 -07007397 err = -ENOMEM;
7398 goto out;
7399 }
Assaf Krauss1d0a0822008-03-14 10:38:48 -07007400 priv = hw->priv;
7401 /* At this point both hw and priv are allocated. */
7402
Zhu Yib481de92007-09-25 17:54:57 -07007403 SET_IEEE80211_DEV(hw, &pdev->dev);
7404
7405 IWL_DEBUG_INFO("*** LOAD DRIVER ***\n");
Tomas Winkler82b9a122008-03-04 18:09:30 -08007406 priv->cfg = cfg;
Zhu Yib481de92007-09-25 17:54:57 -07007407 priv->pci_dev = pdev;
Assaf Krauss316c30d2008-03-14 10:38:46 -07007408
Tomas Winkler0a6857e2008-03-12 16:58:49 -07007409#ifdef CONFIG_IWLWIFI_DEBUG
Assaf Krauss1ea87392008-03-18 14:57:50 -07007410 iwl_debug_level = priv->cfg->mod_params->debug;
Zhu Yib481de92007-09-25 17:54:57 -07007411 atomic_set(&priv->restrict_refcnt, 0);
7412#endif
Zhu Yib481de92007-09-25 17:54:57 -07007413
Assaf Krauss316c30d2008-03-14 10:38:46 -07007414 /**************************
7415 * 2. Initializing PCI bus
7416 **************************/
7417 if (pci_enable_device(pdev)) {
7418 err = -ENODEV;
7419 goto out_ieee80211_free_hw;
7420 }
7421
7422 pci_set_master(pdev);
7423
Ron Rindjunskycc2a8ea2008-04-21 15:41:59 -07007424 err = pci_set_dma_mask(pdev, DMA_64BIT_MASK);
Assaf Krauss316c30d2008-03-14 10:38:46 -07007425 if (!err)
Ron Rindjunskycc2a8ea2008-04-21 15:41:59 -07007426 err = pci_set_consistent_dma_mask(pdev, DMA_64BIT_MASK);
7427 if (err) {
7428 err = pci_set_dma_mask(pdev, DMA_32BIT_MASK);
7429 if (!err)
7430 err = pci_set_consistent_dma_mask(pdev, DMA_32BIT_MASK);
7431 /* both attempts failed: */
Assaf Krauss316c30d2008-03-14 10:38:46 -07007432 if (err) {
Ron Rindjunskycc2a8ea2008-04-21 15:41:59 -07007433 printk(KERN_WARNING "%s: No suitable DMA available.\n",
7434 DRV_NAME);
Assaf Krauss316c30d2008-03-14 10:38:46 -07007435 goto out_pci_disable_device;
Ron Rindjunskycc2a8ea2008-04-21 15:41:59 -07007436 }
Assaf Krauss316c30d2008-03-14 10:38:46 -07007437 }
7438
7439 err = pci_request_regions(pdev, DRV_NAME);
7440 if (err)
7441 goto out_pci_disable_device;
7442
7443 pci_set_drvdata(pdev, priv);
7444
7445 /* We disable the RETRY_TIMEOUT register (0x41) to keep
7446 * PCI Tx retries from interfering with C3 CPU state */
7447 pci_write_config_byte(pdev, 0x41, 0x00);
7448
7449 /***********************
7450 * 3. Read REV register
7451 ***********************/
7452 priv->hw_base = pci_iomap(pdev, 0, 0);
7453 if (!priv->hw_base) {
7454 err = -ENODEV;
7455 goto out_pci_release_regions;
7456 }
7457
7458 IWL_DEBUG_INFO("pci_resource_len = 0x%08llx\n",
7459 (unsigned long long) pci_resource_len(pdev, 0));
7460 IWL_DEBUG_INFO("pci_resource_base = %p\n", priv->hw_base);
7461
Tomas Winklerb661c812008-04-23 17:14:54 -07007462 iwl_hw_detect(priv);
Assaf Krauss316c30d2008-03-14 10:38:46 -07007463 printk(KERN_INFO DRV_NAME
Tomas Winklerb661c812008-04-23 17:14:54 -07007464 ": Detected Intel Wireless WiFi Link %s REV=0x%X\n",
7465 priv->cfg->name, priv->hw_rev);
Assaf Krauss316c30d2008-03-14 10:38:46 -07007466
Tomas Winkler91238712008-04-23 17:14:53 -07007467 /* amp init */
7468 err = priv->cfg->ops->lib->apm_ops.init(priv);
7469 if (err < 0) {
7470 IWL_DEBUG_INFO("Failed to init APMG\n");
7471 goto out_iounmap;
7472 }
Assaf Krauss316c30d2008-03-14 10:38:46 -07007473 /*****************
7474 * 4. Read EEPROM
7475 *****************/
Assaf Krauss316c30d2008-03-14 10:38:46 -07007476 /* Read the EEPROM */
7477 err = iwl_eeprom_init(priv);
7478 if (err) {
7479 IWL_ERROR("Unable to init EEPROM\n");
7480 goto out_iounmap;
7481 }
Tomas Winkler8614f362008-04-23 17:14:55 -07007482 err = iwl_eeprom_check_version(priv);
7483 if (err)
7484 goto out_iounmap;
7485
Assaf Krauss316c30d2008-03-14 10:38:46 -07007486 /* MAC Address location in EEPROM same for 3945/4965 */
7487 iwl_eeprom_get_mac(priv, priv->mac_addr);
7488 IWL_DEBUG_INFO("MAC address: %s\n", print_mac(mac, priv->mac_addr));
7489 SET_IEEE80211_PERM_ADDR(priv->hw, priv->mac_addr);
7490
7491 /************************
7492 * 5. Setup HW constants
7493 ************************/
7494 /* Device-specific setup */
Tomas Winkler5425e492008-04-15 16:01:38 -07007495 if (priv->cfg->ops->lib->set_hw_params(priv)) {
7496 IWL_ERROR("failed to set hw parameters\n");
Tomas Winkler073d3f52008-04-21 15:41:52 -07007497 goto out_free_eeprom;
Assaf Krauss316c30d2008-03-14 10:38:46 -07007498 }
7499
7500 /*******************
7501 * 6. Setup hw/priv
7502 *******************/
Zhu Yib481de92007-09-25 17:54:57 -07007503
Assaf Kraussbf85ea42008-03-14 10:38:49 -07007504 err = iwl_setup(priv);
7505 if (err)
Tomas Winkler5425e492008-04-15 16:01:38 -07007506 goto out_unset_hw_params;
Assaf Kraussbf85ea42008-03-14 10:38:49 -07007507 /* At this point both hw and priv are initialized. */
Assaf Krauss316c30d2008-03-14 10:38:46 -07007508
7509 /**********************************
7510 * 7. Initialize module parameters
7511 **********************************/
7512
7513 /* Disable radio (SW RF KILL) via parameter when loading driver */
Assaf Krauss1ea87392008-03-18 14:57:50 -07007514 if (priv->cfg->mod_params->disable) {
Assaf Krauss316c30d2008-03-14 10:38:46 -07007515 set_bit(STATUS_RF_KILL_SW, &priv->status);
7516 IWL_DEBUG_INFO("Radio disabled.\n");
7517 }
7518
Assaf Krauss1ea87392008-03-18 14:57:50 -07007519 if (priv->cfg->mod_params->enable_qos)
Assaf Krauss316c30d2008-03-14 10:38:46 -07007520 priv->qos_data.qos_enable = 1;
7521
7522 /********************
7523 * 8. Setup services
7524 ********************/
Mohamed Abbas0359fac2008-03-28 16:21:08 -07007525 spin_lock_irqsave(&priv->lock, flags);
Assaf Krauss316c30d2008-03-14 10:38:46 -07007526 iwl4965_disable_interrupts(priv);
Mohamed Abbas0359fac2008-03-28 16:21:08 -07007527 spin_unlock_irqrestore(&priv->lock, flags);
Assaf Krauss316c30d2008-03-14 10:38:46 -07007528
7529 err = sysfs_create_group(&pdev->dev.kobj, &iwl4965_attribute_group);
7530 if (err) {
7531 IWL_ERROR("failed to create sysfs device attributes\n");
Tomas Winkler5425e492008-04-15 16:01:38 -07007532 goto out_unset_hw_params;
Assaf Krauss316c30d2008-03-14 10:38:46 -07007533 }
7534
7535 err = iwl_dbgfs_register(priv, DRV_NAME);
7536 if (err) {
7537 IWL_ERROR("failed to create debugfs files\n");
7538 goto out_remove_sysfs;
7539 }
7540
7541 iwl4965_setup_deferred_work(priv);
7542 iwl4965_setup_rx_handlers(priv);
7543
7544 /********************
7545 * 9. Conclude
7546 ********************/
Zhu Yi5a66926a2008-01-14 17:46:18 -08007547 pci_save_state(pdev);
7548 pci_disable_device(pdev);
Zhu Yib481de92007-09-25 17:54:57 -07007549
Mohamed Abbasc8381fd2008-03-28 16:21:05 -07007550 /* notify iwlcore to init */
7551 iwlcore_low_level_notify(priv, IWLCORE_INIT_EVT);
Zhu Yib481de92007-09-25 17:54:57 -07007552 return 0;
7553
Assaf Krauss316c30d2008-03-14 10:38:46 -07007554 out_remove_sysfs:
7555 sysfs_remove_group(&pdev->dev.kobj, &iwl4965_attribute_group);
Tomas Winkler5425e492008-04-15 16:01:38 -07007556 out_unset_hw_params:
7557 iwl4965_unset_hw_params(priv);
Tomas Winkler073d3f52008-04-21 15:41:52 -07007558 out_free_eeprom:
7559 iwl_eeprom_free(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007560 out_iounmap:
7561 pci_iounmap(pdev, priv->hw_base);
7562 out_pci_release_regions:
7563 pci_release_regions(pdev);
Assaf Krauss316c30d2008-03-14 10:38:46 -07007564 pci_set_drvdata(pdev, NULL);
Zhu Yib481de92007-09-25 17:54:57 -07007565 out_pci_disable_device:
7566 pci_disable_device(pdev);
Zhu Yib481de92007-09-25 17:54:57 -07007567 out_ieee80211_free_hw:
7568 ieee80211_free_hw(priv->hw);
7569 out:
7570 return err;
7571}
7572
Reinette Chatrec83dbf62008-03-21 13:53:41 -07007573static void __devexit iwl4965_pci_remove(struct pci_dev *pdev)
Zhu Yib481de92007-09-25 17:54:57 -07007574{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007575 struct iwl_priv *priv = pci_get_drvdata(pdev);
Zhu Yib481de92007-09-25 17:54:57 -07007576 struct list_head *p, *q;
7577 int i;
Mohamed Abbas0359fac2008-03-28 16:21:08 -07007578 unsigned long flags;
Zhu Yib481de92007-09-25 17:54:57 -07007579
7580 if (!priv)
7581 return;
7582
7583 IWL_DEBUG_INFO("*** UNLOAD DRIVER ***\n");
7584
Ron Rindjunskyc4f55232008-03-28 16:21:10 -07007585 if (priv->mac80211_registered) {
7586 ieee80211_unregister_hw(priv->hw);
7587 priv->mac80211_registered = 0;
7588 }
7589
Zhu Yib481de92007-09-25 17:54:57 -07007590 set_bit(STATUS_EXIT_PENDING, &priv->status);
Zhu Yib24d22b2007-12-19 13:59:52 +08007591
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007592 iwl4965_down(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007593
Mohamed Abbas0359fac2008-03-28 16:21:08 -07007594 /* make sure we flush any pending irq or
7595 * tasklet for the driver
7596 */
7597 spin_lock_irqsave(&priv->lock, flags);
7598 iwl4965_disable_interrupts(priv);
7599 spin_unlock_irqrestore(&priv->lock, flags);
7600
7601 iwl_synchronize_irq(priv);
7602
Zhu Yib481de92007-09-25 17:54:57 -07007603 /* Free MAC hash list for ADHOC */
7604 for (i = 0; i < IWL_IBSS_MAC_HASH_SIZE; i++) {
7605 list_for_each_safe(p, q, &priv->ibss_mac_hash[i]) {
7606 list_del(p);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007607 kfree(list_entry(p, struct iwl4965_ibss_seq, list));
Zhu Yib481de92007-09-25 17:54:57 -07007608 }
7609 }
7610
Mohamed Abbasc8381fd2008-03-28 16:21:05 -07007611 iwlcore_low_level_notify(priv, IWLCORE_REMOVE_EVT);
Tomas Winkler712b6cf2008-03-12 16:58:52 -07007612 iwl_dbgfs_unregister(priv);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007613 sysfs_remove_group(&pdev->dev.kobj, &iwl4965_attribute_group);
Zhu Yib481de92007-09-25 17:54:57 -07007614
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007615 iwl4965_dealloc_ucode_pci(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007616
7617 if (priv->rxq.bd)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007618 iwl4965_rx_queue_free(priv, &priv->rxq);
7619 iwl4965_hw_txq_ctx_free(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007620
Tomas Winkler5425e492008-04-15 16:01:38 -07007621 iwl4965_unset_hw_params(priv);
Assaf Kraussbf85ea42008-03-14 10:38:49 -07007622 iwlcore_clear_stations_table(priv);
Tomas Winkler073d3f52008-04-21 15:41:52 -07007623 iwl_eeprom_free(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007624
Zhu Yib481de92007-09-25 17:54:57 -07007625
Mohamed Abbas948c1712007-10-25 17:15:45 +08007626 /*netif_stop_queue(dev); */
7627 flush_workqueue(priv->workqueue);
7628
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007629 /* ieee80211_unregister_hw calls iwl4965_mac_stop, which flushes
Zhu Yib481de92007-09-25 17:54:57 -07007630 * priv->workqueue... so we can't take down the workqueue
7631 * until now... */
7632 destroy_workqueue(priv->workqueue);
7633 priv->workqueue = NULL;
7634
Zhu Yib481de92007-09-25 17:54:57 -07007635 pci_iounmap(pdev, priv->hw_base);
7636 pci_release_regions(pdev);
7637 pci_disable_device(pdev);
7638 pci_set_drvdata(pdev, NULL);
7639
Assaf Kraussbf85ea42008-03-14 10:38:49 -07007640 iwl_free_channel_map(priv);
Reinette Chatre849e0dc2008-01-23 10:15:18 -08007641 iwl4965_free_geos(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007642
7643 if (priv->ibss_beacon)
7644 dev_kfree_skb(priv->ibss_beacon);
7645
7646 ieee80211_free_hw(priv->hw);
7647}
7648
7649#ifdef CONFIG_PM
7650
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007651static int iwl4965_pci_suspend(struct pci_dev *pdev, pm_message_t state)
Zhu Yib481de92007-09-25 17:54:57 -07007652{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007653 struct iwl_priv *priv = pci_get_drvdata(pdev);
Zhu Yib481de92007-09-25 17:54:57 -07007654
Zhu Yie655b9f2008-01-24 02:19:38 -08007655 if (priv->is_open) {
7656 set_bit(STATUS_IN_SUSPEND, &priv->status);
7657 iwl4965_mac_stop(priv->hw);
7658 priv->is_open = 1;
7659 }
Zhu Yib481de92007-09-25 17:54:57 -07007660
Zhu Yib481de92007-09-25 17:54:57 -07007661 pci_set_power_state(pdev, PCI_D3hot);
7662
Zhu Yib481de92007-09-25 17:54:57 -07007663 return 0;
7664}
7665
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007666static int iwl4965_pci_resume(struct pci_dev *pdev)
Zhu Yib481de92007-09-25 17:54:57 -07007667{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007668 struct iwl_priv *priv = pci_get_drvdata(pdev);
Zhu Yib481de92007-09-25 17:54:57 -07007669
Zhu Yib481de92007-09-25 17:54:57 -07007670 pci_set_power_state(pdev, PCI_D0);
Zhu Yib481de92007-09-25 17:54:57 -07007671
Zhu Yie655b9f2008-01-24 02:19:38 -08007672 if (priv->is_open)
7673 iwl4965_mac_start(priv->hw);
Zhu Yib481de92007-09-25 17:54:57 -07007674
Zhu Yie655b9f2008-01-24 02:19:38 -08007675 clear_bit(STATUS_IN_SUSPEND, &priv->status);
Zhu Yib481de92007-09-25 17:54:57 -07007676 return 0;
7677}
7678
7679#endif /* CONFIG_PM */
7680
7681/*****************************************************************************
7682 *
7683 * driver and module entry point
7684 *
7685 *****************************************************************************/
7686
Ron Rindjunskyfed90172008-04-15 16:01:41 -07007687/* Hardware specific file defines the PCI IDs table for that hardware module */
7688static struct pci_device_id iwl_hw_card_ids[] = {
7689 {IWL_PCI_DEVICE(0x4229, PCI_ANY_ID, iwl4965_agn_cfg)},
7690 {IWL_PCI_DEVICE(0x4230, PCI_ANY_ID, iwl4965_agn_cfg)},
7691 {0}
7692};
7693MODULE_DEVICE_TABLE(pci, iwl_hw_card_ids);
7694
7695static struct pci_driver iwl_driver = {
Zhu Yib481de92007-09-25 17:54:57 -07007696 .name = DRV_NAME,
Ron Rindjunskyfed90172008-04-15 16:01:41 -07007697 .id_table = iwl_hw_card_ids,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007698 .probe = iwl4965_pci_probe,
7699 .remove = __devexit_p(iwl4965_pci_remove),
Zhu Yib481de92007-09-25 17:54:57 -07007700#ifdef CONFIG_PM
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007701 .suspend = iwl4965_pci_suspend,
7702 .resume = iwl4965_pci_resume,
Zhu Yib481de92007-09-25 17:54:57 -07007703#endif
7704};
7705
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007706static int __init iwl4965_init(void)
Zhu Yib481de92007-09-25 17:54:57 -07007707{
7708
7709 int ret;
7710 printk(KERN_INFO DRV_NAME ": " DRV_DESCRIPTION ", " DRV_VERSION "\n");
7711 printk(KERN_INFO DRV_NAME ": " DRV_COPYRIGHT "\n");
Reinette Chatre897e1cf2008-03-28 16:21:09 -07007712
7713 ret = iwl4965_rate_control_register();
7714 if (ret) {
7715 IWL_ERROR("Unable to register rate control algorithm: %d\n", ret);
7716 return ret;
7717 }
7718
Ron Rindjunskyfed90172008-04-15 16:01:41 -07007719 ret = pci_register_driver(&iwl_driver);
Zhu Yib481de92007-09-25 17:54:57 -07007720 if (ret) {
7721 IWL_ERROR("Unable to initialize PCI module\n");
Reinette Chatre897e1cf2008-03-28 16:21:09 -07007722 goto error_register;
Zhu Yib481de92007-09-25 17:54:57 -07007723 }
Tomas Winkler0a6857e2008-03-12 16:58:49 -07007724#ifdef CONFIG_IWLWIFI_DEBUG
Ron Rindjunskyfed90172008-04-15 16:01:41 -07007725 ret = driver_create_file(&iwl_driver.driver, &driver_attr_debug_level);
Zhu Yib481de92007-09-25 17:54:57 -07007726 if (ret) {
7727 IWL_ERROR("Unable to create driver sysfs file\n");
Reinette Chatre897e1cf2008-03-28 16:21:09 -07007728 goto error_debug;
Zhu Yib481de92007-09-25 17:54:57 -07007729 }
7730#endif
7731
7732 return ret;
Reinette Chatre897e1cf2008-03-28 16:21:09 -07007733
7734#ifdef CONFIG_IWLWIFI_DEBUG
7735error_debug:
Ron Rindjunskyfed90172008-04-15 16:01:41 -07007736 pci_unregister_driver(&iwl_driver);
Reinette Chatre897e1cf2008-03-28 16:21:09 -07007737#endif
7738error_register:
7739 iwl4965_rate_control_unregister();
7740 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07007741}
7742
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007743static void __exit iwl4965_exit(void)
Zhu Yib481de92007-09-25 17:54:57 -07007744{
Tomas Winkler0a6857e2008-03-12 16:58:49 -07007745#ifdef CONFIG_IWLWIFI_DEBUG
Ron Rindjunskyfed90172008-04-15 16:01:41 -07007746 driver_remove_file(&iwl_driver.driver, &driver_attr_debug_level);
Zhu Yib481de92007-09-25 17:54:57 -07007747#endif
Ron Rindjunskyfed90172008-04-15 16:01:41 -07007748 pci_unregister_driver(&iwl_driver);
Reinette Chatre897e1cf2008-03-28 16:21:09 -07007749 iwl4965_rate_control_unregister();
Zhu Yib481de92007-09-25 17:54:57 -07007750}
7751
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007752module_exit(iwl4965_exit);
7753module_init(iwl4965_init);