blob: e24527313ab6a3a7d451329207bc783eb6aafd1b [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
Ron Rindjunskyc7de35c2008-04-23 17:15:05 -0700885 iwl_set_rxon_chain(priv);
Mohamed Abbas5da4b552008-04-21 15:41:51 -0700886 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
Guy Cohen39e88502008-04-23 17:14:57 -07001076static u8 iwl4965_rate_get_lowest_plcp(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001077{
Guy Cohen39e88502008-04-23 17:14:57 -07001078 int i;
1079 int rate_mask;
Zhu Yib481de92007-09-25 17:54:57 -07001080
Guy Cohen39e88502008-04-23 17:14:57 -07001081 /* Set rate mask*/
1082 if (priv->staging_rxon.flags & RXON_FLG_BAND_24G_MSK)
1083 rate_mask = priv->active_rate_basic & 0xF;
1084 else
1085 rate_mask = priv->active_rate_basic & 0xFF0;
1086
1087 /* Find lowest valid rate */
Zhu Yib481de92007-09-25 17:54:57 -07001088 for (i = IWL_RATE_1M_INDEX; i != IWL_RATE_INVALID;
Guy Cohen39e88502008-04-23 17:14:57 -07001089 i = iwl4965_rates[i].next_ieee) {
Zhu Yib481de92007-09-25 17:54:57 -07001090 if (rate_mask & (1 << i))
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001091 return iwl4965_rates[i].plcp;
Zhu Yib481de92007-09-25 17:54:57 -07001092 }
1093
Guy Cohen39e88502008-04-23 17:14:57 -07001094 /* No valid rate was found. Assign the lowest one */
1095 if (priv->staging_rxon.flags & RXON_FLG_BAND_24G_MSK)
1096 return IWL_RATE_1M_PLCP;
1097 else
1098 return IWL_RATE_6M_PLCP;
Zhu Yib481de92007-09-25 17:54:57 -07001099}
1100
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001101static int iwl4965_send_beacon_cmd(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001102{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001103 struct iwl4965_frame *frame;
Zhu Yib481de92007-09-25 17:54:57 -07001104 unsigned int frame_size;
1105 int rc;
1106 u8 rate;
1107
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001108 frame = iwl4965_get_free_frame(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001109
1110 if (!frame) {
1111 IWL_ERROR("Could not obtain free frame buffer for beacon "
1112 "command.\n");
1113 return -ENOMEM;
1114 }
1115
Guy Cohen39e88502008-04-23 17:14:57 -07001116 rate = iwl4965_rate_get_lowest_plcp(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001117
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001118 frame_size = iwl4965_hw_get_beacon_cmd(priv, frame, rate);
Zhu Yib481de92007-09-25 17:54:57 -07001119
Tomas Winkler857485c2008-03-21 13:53:44 -07001120 rc = iwl_send_cmd_pdu(priv, REPLY_TX_BEACON, frame_size,
Zhu Yib481de92007-09-25 17:54:57 -07001121 &frame->u.cmd[0]);
1122
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001123 iwl4965_free_frame(priv, frame);
Zhu Yib481de92007-09-25 17:54:57 -07001124
1125 return rc;
1126}
1127
1128/******************************************************************************
1129 *
Zhu Yib481de92007-09-25 17:54:57 -07001130 * Misc. internal state and helper functions
1131 *
1132 ******************************************************************************/
Zhu Yib481de92007-09-25 17:54:57 -07001133
Zhu Yib481de92007-09-25 17:54:57 -07001134/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001135 * iwl4965_supported_rate_to_ie - fill in the supported rate in IE field
Zhu Yib481de92007-09-25 17:54:57 -07001136 *
1137 * return : set the bit for each supported rate insert in ie
1138 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001139static u16 iwl4965_supported_rate_to_ie(u8 *ie, u16 supported_rate,
Tomas Winklerc7c46672007-10-18 02:04:15 +02001140 u16 basic_rate, int *left)
Zhu Yib481de92007-09-25 17:54:57 -07001141{
1142 u16 ret_rates = 0, bit;
1143 int i;
Tomas Winklerc7c46672007-10-18 02:04:15 +02001144 u8 *cnt = ie;
1145 u8 *rates = ie + 1;
Zhu Yib481de92007-09-25 17:54:57 -07001146
1147 for (bit = 1, i = 0; i < IWL_RATE_COUNT; i++, bit <<= 1) {
1148 if (bit & supported_rate) {
1149 ret_rates |= bit;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001150 rates[*cnt] = iwl4965_rates[i].ieee |
Tomas Winklerc7c46672007-10-18 02:04:15 +02001151 ((bit & basic_rate) ? 0x80 : 0x00);
1152 (*cnt)++;
1153 (*left)--;
1154 if ((*left <= 0) ||
1155 (*cnt >= IWL_SUPPORTED_RATES_IE_LEN))
Zhu Yib481de92007-09-25 17:54:57 -07001156 break;
1157 }
1158 }
1159
1160 return ret_rates;
1161}
1162
Emmanuel Grumbachd1141df2008-04-21 15:42:00 -07001163#ifdef CONFIG_IWL4965_HT
1164static void iwl4965_ht_conf(struct iwl_priv *priv,
1165 struct ieee80211_bss_conf *bss_conf)
1166{
1167 struct ieee80211_ht_info *ht_conf = bss_conf->ht_conf;
1168 struct ieee80211_ht_bss_info *ht_bss_conf = bss_conf->ht_bss_conf;
1169 struct iwl_ht_info *iwl_conf = &priv->current_ht_config;
1170
1171 IWL_DEBUG_MAC80211("enter: \n");
1172
1173 iwl_conf->is_ht = bss_conf->assoc_ht;
1174
1175 if (!iwl_conf->is_ht)
1176 return;
1177
1178 priv->ps_mode = (u8)((ht_conf->cap & IEEE80211_HT_CAP_MIMO_PS) >> 2);
1179
1180 if (ht_conf->cap & IEEE80211_HT_CAP_SGI_20)
1181 iwl_conf->sgf |= 0x1;
1182 if (ht_conf->cap & IEEE80211_HT_CAP_SGI_40)
1183 iwl_conf->sgf |= 0x2;
1184
1185 iwl_conf->is_green_field = !!(ht_conf->cap & IEEE80211_HT_CAP_GRN_FLD);
1186 iwl_conf->max_amsdu_size =
1187 !!(ht_conf->cap & IEEE80211_HT_CAP_MAX_AMSDU);
1188
1189 iwl_conf->supported_chan_width =
1190 !!(ht_conf->cap & IEEE80211_HT_CAP_SUP_WIDTH);
1191 iwl_conf->extension_chan_offset =
1192 ht_bss_conf->bss_cap & IEEE80211_HT_IE_CHA_SEC_OFFSET;
1193 /* If no above or below channel supplied disable FAT channel */
1194 if (iwl_conf->extension_chan_offset != IWL_EXT_CHANNEL_OFFSET_ABOVE &&
1195 iwl_conf->extension_chan_offset != IWL_EXT_CHANNEL_OFFSET_BELOW)
1196 iwl_conf->supported_chan_width = 0;
1197
1198 iwl_conf->tx_mimo_ps_mode =
1199 (u8)((ht_conf->cap & IEEE80211_HT_CAP_MIMO_PS) >> 2);
1200 memcpy(iwl_conf->supp_mcs_set, ht_conf->supp_mcs_set, 16);
1201
1202 iwl_conf->control_channel = ht_bss_conf->primary_channel;
1203 iwl_conf->tx_chan_width =
1204 !!(ht_bss_conf->bss_cap & IEEE80211_HT_IE_CHA_WIDTH);
1205 iwl_conf->ht_protection =
1206 ht_bss_conf->bss_op_mode & IEEE80211_HT_IE_HT_PROTECTION;
1207 iwl_conf->non_GF_STA_present =
1208 !!(ht_bss_conf->bss_op_mode & IEEE80211_HT_IE_NON_GF_STA_PRSNT);
1209
1210 IWL_DEBUG_MAC80211("control channel %d\n", iwl_conf->control_channel);
1211 IWL_DEBUG_MAC80211("leave\n");
1212}
1213
1214static void iwl_ht_cap_to_ie(const struct ieee80211_supported_band *sband,
1215 u8 *pos, int *left)
1216{
1217 struct ieee80211_ht_cap *ht_cap;
1218
1219 if (!sband || !sband->ht_info.ht_supported)
1220 return;
1221
1222 if (*left < sizeof(struct ieee80211_ht_cap))
1223 return;
1224
1225 *pos++ = sizeof(struct ieee80211_ht_cap);
1226 ht_cap = (struct ieee80211_ht_cap *) pos;
1227
1228 ht_cap->cap_info = cpu_to_le16(sband->ht_info.cap);
1229 memcpy(ht_cap->supp_mcs_set, sband->ht_info.supp_mcs_set, 16);
1230 ht_cap->ampdu_params_info =
1231 (sband->ht_info.ampdu_factor & IEEE80211_HT_CAP_AMPDU_FACTOR) |
1232 ((sband->ht_info.ampdu_density << 2) &
1233 IEEE80211_HT_CAP_AMPDU_DENSITY);
1234 *left -= sizeof(struct ieee80211_ht_cap);
1235}
1236#else
1237static inline void iwl4965_ht_conf(struct iwl_priv *priv,
1238 struct ieee80211_bss_conf *bss_conf)
1239{
1240}
1241static void iwl_ht_cap_to_ie(const struct ieee80211_supported_band *sband,
1242 u8 *pos, int *left)
1243{
1244}
1245#endif
1246
1247
Zhu Yib481de92007-09-25 17:54:57 -07001248/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001249 * iwl4965_fill_probe_req - fill in all required fields and IE for probe request
Zhu Yib481de92007-09-25 17:54:57 -07001250 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001251static u16 iwl4965_fill_probe_req(struct iwl_priv *priv,
Tomas Winkler78330fd2008-02-06 02:37:18 +02001252 enum ieee80211_band band,
1253 struct ieee80211_mgmt *frame,
1254 int left, int is_direct)
Zhu Yib481de92007-09-25 17:54:57 -07001255{
1256 int len = 0;
1257 u8 *pos = NULL;
mabbasbee488d2007-10-25 17:15:42 +08001258 u16 active_rates, ret_rates, cck_rates, active_rate_basic;
Tomas Winkler78330fd2008-02-06 02:37:18 +02001259 const struct ieee80211_supported_band *sband =
Emmanuel Grumbachd1141df2008-04-21 15:42:00 -07001260 iwl_get_hw_mode(priv, band);
Zhu Yib481de92007-09-25 17:54:57 -07001261
1262 /* Make sure there is enough space for the probe request,
1263 * two mandatory IEs and the data */
1264 left -= 24;
1265 if (left < 0)
1266 return 0;
1267 len += 24;
1268
1269 frame->frame_control = cpu_to_le16(IEEE80211_STYPE_PROBE_REQ);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001270 memcpy(frame->da, iwl4965_broadcast_addr, ETH_ALEN);
Zhu Yib481de92007-09-25 17:54:57 -07001271 memcpy(frame->sa, priv->mac_addr, ETH_ALEN);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001272 memcpy(frame->bssid, iwl4965_broadcast_addr, ETH_ALEN);
Zhu Yib481de92007-09-25 17:54:57 -07001273 frame->seq_ctrl = 0;
1274
1275 /* fill in our indirect SSID IE */
1276 /* ...next IE... */
1277
1278 left -= 2;
1279 if (left < 0)
1280 return 0;
1281 len += 2;
1282 pos = &(frame->u.probe_req.variable[0]);
1283 *pos++ = WLAN_EID_SSID;
1284 *pos++ = 0;
1285
1286 /* fill in our direct SSID IE... */
1287 if (is_direct) {
1288 /* ...next IE... */
1289 left -= 2 + priv->essid_len;
1290 if (left < 0)
1291 return 0;
1292 /* ... fill it in... */
1293 *pos++ = WLAN_EID_SSID;
1294 *pos++ = priv->essid_len;
1295 memcpy(pos, priv->essid, priv->essid_len);
1296 pos += priv->essid_len;
1297 len += 2 + priv->essid_len;
1298 }
1299
1300 /* fill in supported rate */
1301 /* ...next IE... */
1302 left -= 2;
1303 if (left < 0)
1304 return 0;
Tomas Winklerc7c46672007-10-18 02:04:15 +02001305
Zhu Yib481de92007-09-25 17:54:57 -07001306 /* ... fill it in... */
1307 *pos++ = WLAN_EID_SUPP_RATES;
1308 *pos = 0;
Tomas Winklerc7c46672007-10-18 02:04:15 +02001309
mabbasbee488d2007-10-25 17:15:42 +08001310 /* exclude 60M rate */
1311 active_rates = priv->rates_mask;
1312 active_rates &= ~IWL_RATE_60M_MASK;
1313
1314 active_rate_basic = active_rates & IWL_BASIC_RATES_MASK;
Zhu Yib481de92007-09-25 17:54:57 -07001315
Tomas Winklerc7c46672007-10-18 02:04:15 +02001316 cck_rates = IWL_CCK_RATES_MASK & active_rates;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001317 ret_rates = iwl4965_supported_rate_to_ie(pos, cck_rates,
mabbasbee488d2007-10-25 17:15:42 +08001318 active_rate_basic, &left);
Tomas Winklerc7c46672007-10-18 02:04:15 +02001319 active_rates &= ~ret_rates;
1320
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001321 ret_rates = iwl4965_supported_rate_to_ie(pos, active_rates,
mabbasbee488d2007-10-25 17:15:42 +08001322 active_rate_basic, &left);
Tomas Winklerc7c46672007-10-18 02:04:15 +02001323 active_rates &= ~ret_rates;
1324
Zhu Yib481de92007-09-25 17:54:57 -07001325 len += 2 + *pos;
1326 pos += (*pos) + 1;
Tomas Winklerc7c46672007-10-18 02:04:15 +02001327 if (active_rates == 0)
Zhu Yib481de92007-09-25 17:54:57 -07001328 goto fill_end;
1329
1330 /* fill in supported extended rate */
1331 /* ...next IE... */
1332 left -= 2;
1333 if (left < 0)
1334 return 0;
1335 /* ... fill it in... */
1336 *pos++ = WLAN_EID_EXT_SUPP_RATES;
1337 *pos = 0;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001338 iwl4965_supported_rate_to_ie(pos, active_rates,
mabbasbee488d2007-10-25 17:15:42 +08001339 active_rate_basic, &left);
Zhu Yib481de92007-09-25 17:54:57 -07001340 if (*pos > 0)
1341 len += 2 + *pos;
1342
Zhu Yib481de92007-09-25 17:54:57 -07001343 fill_end:
Emmanuel Grumbachd1141df2008-04-21 15:42:00 -07001344 /* fill in HT IE */
1345 left -= 2;
1346 if (left < 0)
1347 return 0;
1348
1349 *pos++ = WLAN_EID_HT_CAPABILITY;
1350 *pos = 0;
1351
1352 iwl_ht_cap_to_ie(sband, pos, &left);
1353
1354 if (*pos > 0)
1355 len += 2 + *pos;
Zhu Yib481de92007-09-25 17:54:57 -07001356 return (u16)len;
1357}
1358
1359/*
1360 * QoS support
1361*/
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001362static int iwl4965_send_qos_params_command(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001363 struct iwl4965_qosparam_cmd *qos)
Zhu Yib481de92007-09-25 17:54:57 -07001364{
1365
Tomas Winkler857485c2008-03-21 13:53:44 -07001366 return iwl_send_cmd_pdu(priv, REPLY_QOS_PARAM,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001367 sizeof(struct iwl4965_qosparam_cmd), qos);
Zhu Yib481de92007-09-25 17:54:57 -07001368}
1369
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001370static void iwl4965_activate_qos(struct iwl_priv *priv, u8 force)
Zhu Yib481de92007-09-25 17:54:57 -07001371{
1372 unsigned long flags;
1373
Zhu Yib481de92007-09-25 17:54:57 -07001374 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
1375 return;
1376
1377 if (!priv->qos_data.qos_enable)
1378 return;
1379
1380 spin_lock_irqsave(&priv->lock, flags);
1381 priv->qos_data.def_qos_parm.qos_flags = 0;
1382
1383 if (priv->qos_data.qos_cap.q_AP.queue_request &&
1384 !priv->qos_data.qos_cap.q_AP.txop_request)
1385 priv->qos_data.def_qos_parm.qos_flags |=
1386 QOS_PARAM_FLG_TXOP_TYPE_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07001387 if (priv->qos_data.qos_active)
1388 priv->qos_data.def_qos_parm.qos_flags |=
1389 QOS_PARAM_FLG_UPDATE_EDCA_MSK;
1390
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08001391#ifdef CONFIG_IWL4965_HT
Ron Rindjunskyfd105e72007-11-26 16:14:39 +02001392 if (priv->current_ht_config.is_ht)
Tomas Winklerf1f1f5c2007-10-25 17:15:26 +08001393 priv->qos_data.def_qos_parm.qos_flags |= QOS_PARAM_FLG_TGN_MSK;
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08001394#endif /* CONFIG_IWL4965_HT */
Tomas Winklerf1f1f5c2007-10-25 17:15:26 +08001395
Zhu Yib481de92007-09-25 17:54:57 -07001396 spin_unlock_irqrestore(&priv->lock, flags);
1397
Tomas Winkler3109ece2008-03-28 16:33:35 -07001398 if (force || iwl_is_associated(priv)) {
Tomas Winklerf1f1f5c2007-10-25 17:15:26 +08001399 IWL_DEBUG_QOS("send QoS cmd with Qos active=%d FLAGS=0x%X\n",
1400 priv->qos_data.qos_active,
1401 priv->qos_data.def_qos_parm.qos_flags);
Zhu Yib481de92007-09-25 17:54:57 -07001402
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001403 iwl4965_send_qos_params_command(priv,
Zhu Yib481de92007-09-25 17:54:57 -07001404 &(priv->qos_data.def_qos_parm));
1405 }
1406}
1407
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001408int iwl4965_is_network_packet(struct iwl_priv *priv, struct ieee80211_hdr *header)
Zhu Yib481de92007-09-25 17:54:57 -07001409{
1410 /* Filter incoming packets to determine if they are targeted toward
1411 * this network, discarding packets coming from ourselves */
1412 switch (priv->iw_mode) {
1413 case IEEE80211_IF_TYPE_IBSS: /* Header: Dest. | Source | BSSID */
1414 /* packets from our adapter are dropped (echo) */
1415 if (!compare_ether_addr(header->addr2, priv->mac_addr))
1416 return 0;
1417 /* {broad,multi}cast packets to our IBSS go through */
1418 if (is_multicast_ether_addr(header->addr1))
1419 return !compare_ether_addr(header->addr3, priv->bssid);
1420 /* packets to our adapter go through */
1421 return !compare_ether_addr(header->addr1, priv->mac_addr);
1422 case IEEE80211_IF_TYPE_STA: /* Header: Dest. | AP{BSSID} | Source */
1423 /* packets from our adapter are dropped (echo) */
1424 if (!compare_ether_addr(header->addr3, priv->mac_addr))
1425 return 0;
1426 /* {broad,multi}cast packets to our BSS go through */
1427 if (is_multicast_ether_addr(header->addr1))
1428 return !compare_ether_addr(header->addr2, priv->bssid);
1429 /* packets to our adapter go through */
1430 return !compare_ether_addr(header->addr1, priv->mac_addr);
Tomas Winkler69dc5d92008-03-25 16:33:41 -07001431 default:
1432 break;
Zhu Yib481de92007-09-25 17:54:57 -07001433 }
1434
1435 return 1;
1436}
1437
1438#define TX_STATUS_ENTRY(x) case TX_STATUS_FAIL_ ## x: return #x
1439
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001440static const char *iwl4965_get_tx_fail_reason(u32 status)
Zhu Yib481de92007-09-25 17:54:57 -07001441{
1442 switch (status & TX_STATUS_MSK) {
1443 case TX_STATUS_SUCCESS:
1444 return "SUCCESS";
1445 TX_STATUS_ENTRY(SHORT_LIMIT);
1446 TX_STATUS_ENTRY(LONG_LIMIT);
1447 TX_STATUS_ENTRY(FIFO_UNDERRUN);
1448 TX_STATUS_ENTRY(MGMNT_ABORT);
1449 TX_STATUS_ENTRY(NEXT_FRAG);
1450 TX_STATUS_ENTRY(LIFE_EXPIRE);
1451 TX_STATUS_ENTRY(DEST_PS);
1452 TX_STATUS_ENTRY(ABORTED);
1453 TX_STATUS_ENTRY(BT_RETRY);
1454 TX_STATUS_ENTRY(STA_INVALID);
1455 TX_STATUS_ENTRY(FRAG_DROPPED);
1456 TX_STATUS_ENTRY(TID_DISABLE);
1457 TX_STATUS_ENTRY(FRAME_FLUSHED);
1458 TX_STATUS_ENTRY(INSUFFICIENT_CF_POLL);
1459 TX_STATUS_ENTRY(TX_LOCKED);
1460 TX_STATUS_ENTRY(NO_BEACON_ON_RADAR);
1461 }
1462
1463 return "UNKNOWN";
1464}
1465
1466/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001467 * iwl4965_scan_cancel - Cancel any currently executing HW scan
Zhu Yib481de92007-09-25 17:54:57 -07001468 *
1469 * NOTE: priv->mutex is not required before calling this function
1470 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001471static int iwl4965_scan_cancel(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001472{
1473 if (!test_bit(STATUS_SCAN_HW, &priv->status)) {
1474 clear_bit(STATUS_SCANNING, &priv->status);
1475 return 0;
1476 }
1477
1478 if (test_bit(STATUS_SCANNING, &priv->status)) {
1479 if (!test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
1480 IWL_DEBUG_SCAN("Queuing scan abort.\n");
1481 set_bit(STATUS_SCAN_ABORTING, &priv->status);
1482 queue_work(priv->workqueue, &priv->abort_scan);
1483
1484 } else
1485 IWL_DEBUG_SCAN("Scan abort already in progress.\n");
1486
1487 return test_bit(STATUS_SCANNING, &priv->status);
1488 }
1489
1490 return 0;
1491}
1492
1493/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001494 * iwl4965_scan_cancel_timeout - Cancel any currently executing HW scan
Zhu Yib481de92007-09-25 17:54:57 -07001495 * @ms: amount of time to wait (in milliseconds) for scan to abort
1496 *
1497 * NOTE: priv->mutex must be held before calling this function
1498 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001499static int iwl4965_scan_cancel_timeout(struct iwl_priv *priv, unsigned long ms)
Zhu Yib481de92007-09-25 17:54:57 -07001500{
1501 unsigned long now = jiffies;
1502 int ret;
1503
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001504 ret = iwl4965_scan_cancel(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001505 if (ret && ms) {
1506 mutex_unlock(&priv->mutex);
1507 while (!time_after(jiffies, now + msecs_to_jiffies(ms)) &&
1508 test_bit(STATUS_SCANNING, &priv->status))
1509 msleep(1);
1510 mutex_lock(&priv->mutex);
1511
1512 return test_bit(STATUS_SCANNING, &priv->status);
1513 }
1514
1515 return ret;
1516}
1517
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001518static void iwl4965_sequence_reset(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001519{
1520 /* Reset ieee stats */
1521
1522 /* We don't reset the net_device_stats (ieee->stats) on
1523 * re-association */
1524
1525 priv->last_seq_num = -1;
1526 priv->last_frag_num = -1;
1527 priv->last_packet_time = 0;
1528
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001529 iwl4965_scan_cancel(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001530}
1531
1532#define MAX_UCODE_BEACON_INTERVAL 4096
1533#define INTEL_CONN_LISTEN_INTERVAL __constant_cpu_to_le16(0xA)
1534
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001535static __le16 iwl4965_adjust_beacon_interval(u16 beacon_val)
Zhu Yib481de92007-09-25 17:54:57 -07001536{
1537 u16 new_val = 0;
1538 u16 beacon_factor = 0;
1539
1540 beacon_factor =
1541 (beacon_val + MAX_UCODE_BEACON_INTERVAL)
1542 / MAX_UCODE_BEACON_INTERVAL;
1543 new_val = beacon_val / beacon_factor;
1544
1545 return cpu_to_le16(new_val);
1546}
1547
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001548static void iwl4965_setup_rxon_timing(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001549{
1550 u64 interval_tm_unit;
1551 u64 tsf, result;
1552 unsigned long flags;
1553 struct ieee80211_conf *conf = NULL;
1554 u16 beacon_int = 0;
1555
1556 conf = ieee80211_get_hw_conf(priv->hw);
1557
1558 spin_lock_irqsave(&priv->lock, flags);
Tomas Winkler3109ece2008-03-28 16:33:35 -07001559 priv->rxon_timing.timestamp.dw[1] = cpu_to_le32(priv->timestamp >> 32);
1560 priv->rxon_timing.timestamp.dw[0] =
1561 cpu_to_le32(priv->timestamp & 0xFFFFFFFF);
Zhu Yib481de92007-09-25 17:54:57 -07001562
1563 priv->rxon_timing.listen_interval = INTEL_CONN_LISTEN_INTERVAL;
1564
Tomas Winkler3109ece2008-03-28 16:33:35 -07001565 tsf = priv->timestamp;
Zhu Yib481de92007-09-25 17:54:57 -07001566
1567 beacon_int = priv->beacon_int;
1568 spin_unlock_irqrestore(&priv->lock, flags);
1569
1570 if (priv->iw_mode == IEEE80211_IF_TYPE_STA) {
1571 if (beacon_int == 0) {
1572 priv->rxon_timing.beacon_interval = cpu_to_le16(100);
1573 priv->rxon_timing.beacon_init_val = cpu_to_le32(102400);
1574 } else {
1575 priv->rxon_timing.beacon_interval =
1576 cpu_to_le16(beacon_int);
1577 priv->rxon_timing.beacon_interval =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001578 iwl4965_adjust_beacon_interval(
Zhu Yib481de92007-09-25 17:54:57 -07001579 le16_to_cpu(priv->rxon_timing.beacon_interval));
1580 }
1581
1582 priv->rxon_timing.atim_window = 0;
1583 } else {
1584 priv->rxon_timing.beacon_interval =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001585 iwl4965_adjust_beacon_interval(conf->beacon_int);
Zhu Yib481de92007-09-25 17:54:57 -07001586 /* TODO: we need to get atim_window from upper stack
1587 * for now we set to 0 */
1588 priv->rxon_timing.atim_window = 0;
1589 }
1590
1591 interval_tm_unit =
1592 (le16_to_cpu(priv->rxon_timing.beacon_interval) * 1024);
1593 result = do_div(tsf, interval_tm_unit);
1594 priv->rxon_timing.beacon_init_val =
1595 cpu_to_le32((u32) ((u64) interval_tm_unit - result));
1596
1597 IWL_DEBUG_ASSOC
1598 ("beacon interval %d beacon timer %d beacon tim %d\n",
1599 le16_to_cpu(priv->rxon_timing.beacon_interval),
1600 le32_to_cpu(priv->rxon_timing.beacon_init_val),
1601 le16_to_cpu(priv->rxon_timing.atim_window));
1602}
1603
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001604static int iwl4965_scan_initiate(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001605{
1606 if (priv->iw_mode == IEEE80211_IF_TYPE_AP) {
1607 IWL_ERROR("APs don't scan.\n");
1608 return 0;
1609 }
1610
Tomas Winklerfee12472008-04-03 16:05:21 -07001611 if (!iwl_is_ready_rf(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07001612 IWL_DEBUG_SCAN("Aborting scan due to not ready.\n");
1613 return -EIO;
1614 }
1615
1616 if (test_bit(STATUS_SCANNING, &priv->status)) {
1617 IWL_DEBUG_SCAN("Scan already in progress.\n");
1618 return -EAGAIN;
1619 }
1620
1621 if (test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
1622 IWL_DEBUG_SCAN("Scan request while abort pending. "
1623 "Queuing.\n");
1624 return -EAGAIN;
1625 }
1626
1627 IWL_DEBUG_INFO("Starting scan...\n");
1628 priv->scan_bands = 2;
1629 set_bit(STATUS_SCANNING, &priv->status);
1630 priv->scan_start = jiffies;
1631 priv->scan_pass_start = priv->scan_start;
1632
1633 queue_work(priv->workqueue, &priv->request_scan);
1634
1635 return 0;
1636}
1637
Zhu Yib481de92007-09-25 17:54:57 -07001638
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001639static void iwl4965_set_flags_for_phymode(struct iwl_priv *priv,
Johannes Berg8318d782008-01-24 19:38:38 +01001640 enum ieee80211_band band)
Zhu Yib481de92007-09-25 17:54:57 -07001641{
Johannes Berg8318d782008-01-24 19:38:38 +01001642 if (band == IEEE80211_BAND_5GHZ) {
Zhu Yib481de92007-09-25 17:54:57 -07001643 priv->staging_rxon.flags &=
1644 ~(RXON_FLG_BAND_24G_MSK | RXON_FLG_AUTO_DETECT_MSK
1645 | RXON_FLG_CCK_MSK);
1646 priv->staging_rxon.flags |= RXON_FLG_SHORT_SLOT_MSK;
1647 } else {
Reinette Chatre508e32e2008-04-14 21:16:13 -07001648 /* Copied from iwl4965_post_associate() */
Zhu Yib481de92007-09-25 17:54:57 -07001649 if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_SLOT_TIME)
1650 priv->staging_rxon.flags |= RXON_FLG_SHORT_SLOT_MSK;
1651 else
1652 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
1653
1654 if (priv->iw_mode == IEEE80211_IF_TYPE_IBSS)
1655 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
1656
1657 priv->staging_rxon.flags |= RXON_FLG_BAND_24G_MSK;
1658 priv->staging_rxon.flags |= RXON_FLG_AUTO_DETECT_MSK;
1659 priv->staging_rxon.flags &= ~RXON_FLG_CCK_MSK;
1660 }
1661}
1662
1663/*
Ian Schram01ebd062007-10-25 17:15:22 +08001664 * initialize rxon structure with default values from eeprom
Zhu Yib481de92007-09-25 17:54:57 -07001665 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001666static void iwl4965_connection_init_rx_config(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001667{
Assaf Kraussbf85ea42008-03-14 10:38:49 -07001668 const struct iwl_channel_info *ch_info;
Zhu Yib481de92007-09-25 17:54:57 -07001669
1670 memset(&priv->staging_rxon, 0, sizeof(priv->staging_rxon));
1671
1672 switch (priv->iw_mode) {
1673 case IEEE80211_IF_TYPE_AP:
1674 priv->staging_rxon.dev_type = RXON_DEV_TYPE_AP;
1675 break;
1676
1677 case IEEE80211_IF_TYPE_STA:
1678 priv->staging_rxon.dev_type = RXON_DEV_TYPE_ESS;
1679 priv->staging_rxon.filter_flags = RXON_FILTER_ACCEPT_GRP_MSK;
1680 break;
1681
1682 case IEEE80211_IF_TYPE_IBSS:
1683 priv->staging_rxon.dev_type = RXON_DEV_TYPE_IBSS;
1684 priv->staging_rxon.flags = RXON_FLG_SHORT_PREAMBLE_MSK;
1685 priv->staging_rxon.filter_flags = RXON_FILTER_BCON_AWARE_MSK |
1686 RXON_FILTER_ACCEPT_GRP_MSK;
1687 break;
1688
1689 case IEEE80211_IF_TYPE_MNTR:
1690 priv->staging_rxon.dev_type = RXON_DEV_TYPE_SNIFFER;
1691 priv->staging_rxon.filter_flags = RXON_FILTER_PROMISC_MSK |
1692 RXON_FILTER_CTL2HOST_MSK | RXON_FILTER_ACCEPT_GRP_MSK;
1693 break;
Tomas Winkler69dc5d92008-03-25 16:33:41 -07001694 default:
1695 IWL_ERROR("Unsupported interface type %d\n", priv->iw_mode);
1696 break;
Zhu Yib481de92007-09-25 17:54:57 -07001697 }
1698
1699#if 0
1700 /* TODO: Figure out when short_preamble would be set and cache from
1701 * that */
1702 if (!hw_to_local(priv->hw)->short_preamble)
1703 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_PREAMBLE_MSK;
1704 else
1705 priv->staging_rxon.flags |= RXON_FLG_SHORT_PREAMBLE_MSK;
1706#endif
1707
Assaf Krauss8622e702008-03-21 13:53:43 -07001708 ch_info = iwl_get_channel_info(priv, priv->band,
Zhu Yib481de92007-09-25 17:54:57 -07001709 le16_to_cpu(priv->staging_rxon.channel));
1710
1711 if (!ch_info)
1712 ch_info = &priv->channel_info[0];
1713
1714 /*
1715 * in some case A channels are all non IBSS
1716 * in this case force B/G channel
1717 */
1718 if ((priv->iw_mode == IEEE80211_IF_TYPE_IBSS) &&
1719 !(is_channel_ibss(ch_info)))
1720 ch_info = &priv->channel_info[0];
1721
1722 priv->staging_rxon.channel = cpu_to_le16(ch_info->channel);
Johannes Berg8318d782008-01-24 19:38:38 +01001723 priv->band = ch_info->band;
Zhu Yib481de92007-09-25 17:54:57 -07001724
Johannes Berg8318d782008-01-24 19:38:38 +01001725 iwl4965_set_flags_for_phymode(priv, priv->band);
Zhu Yib481de92007-09-25 17:54:57 -07001726
1727 priv->staging_rxon.ofdm_basic_rates =
1728 (IWL_OFDM_RATES_MASK >> IWL_FIRST_OFDM_RATE) & 0xFF;
1729 priv->staging_rxon.cck_basic_rates =
1730 (IWL_CCK_RATES_MASK >> IWL_FIRST_CCK_RATE) & 0xF;
1731
1732 priv->staging_rxon.flags &= ~(RXON_FLG_CHANNEL_MODE_MIXED_MSK |
1733 RXON_FLG_CHANNEL_MODE_PURE_40_MSK);
1734 memcpy(priv->staging_rxon.node_addr, priv->mac_addr, ETH_ALEN);
1735 memcpy(priv->staging_rxon.wlap_bssid_addr, priv->mac_addr, ETH_ALEN);
1736 priv->staging_rxon.ofdm_ht_single_stream_basic_rates = 0xff;
1737 priv->staging_rxon.ofdm_ht_dual_stream_basic_rates = 0xff;
Ron Rindjunskyc7de35c2008-04-23 17:15:05 -07001738 iwl_set_rxon_chain(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001739}
1740
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001741static int iwl4965_set_mode(struct iwl_priv *priv, int mode)
Zhu Yib481de92007-09-25 17:54:57 -07001742{
Zhu Yib481de92007-09-25 17:54:57 -07001743 if (mode == IEEE80211_IF_TYPE_IBSS) {
Assaf Kraussbf85ea42008-03-14 10:38:49 -07001744 const struct iwl_channel_info *ch_info;
Zhu Yib481de92007-09-25 17:54:57 -07001745
Assaf Krauss8622e702008-03-21 13:53:43 -07001746 ch_info = iwl_get_channel_info(priv,
Johannes Berg8318d782008-01-24 19:38:38 +01001747 priv->band,
Zhu Yib481de92007-09-25 17:54:57 -07001748 le16_to_cpu(priv->staging_rxon.channel));
1749
1750 if (!ch_info || !is_channel_ibss(ch_info)) {
1751 IWL_ERROR("channel %d not IBSS channel\n",
1752 le16_to_cpu(priv->staging_rxon.channel));
1753 return -EINVAL;
1754 }
1755 }
1756
Zhu Yib481de92007-09-25 17:54:57 -07001757 priv->iw_mode = mode;
1758
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001759 iwl4965_connection_init_rx_config(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001760 memcpy(priv->staging_rxon.node_addr, priv->mac_addr, ETH_ALEN);
1761
Assaf Kraussbf85ea42008-03-14 10:38:49 -07001762 iwlcore_clear_stations_table(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001763
Mohamed Abbasfde35712007-11-29 11:10:15 +08001764 /* dont commit rxon if rf-kill is on*/
Tomas Winklerfee12472008-04-03 16:05:21 -07001765 if (!iwl_is_ready_rf(priv))
Mohamed Abbasfde35712007-11-29 11:10:15 +08001766 return -EAGAIN;
1767
1768 cancel_delayed_work(&priv->scan_check);
1769 if (iwl4965_scan_cancel_timeout(priv, 100)) {
1770 IWL_WARNING("Aborted scan still in progress after 100ms\n");
1771 IWL_DEBUG_MAC80211("leaving - scan abort failed.\n");
1772 return -EAGAIN;
1773 }
1774
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001775 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001776
1777 return 0;
1778}
1779
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001780static void iwl4965_build_tx_cmd_hwcrypto(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07001781 struct ieee80211_tx_control *ctl,
Tomas Winkler857485c2008-03-21 13:53:44 -07001782 struct iwl_cmd *cmd,
Zhu Yib481de92007-09-25 17:54:57 -07001783 struct sk_buff *skb_frag,
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07001784 int sta_id)
Zhu Yib481de92007-09-25 17:54:57 -07001785{
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07001786 struct iwl4965_hw_key *keyinfo = &priv->stations[sta_id].keyinfo;
Emmanuel Grumbach6974e362008-04-14 21:16:06 -07001787 struct iwl_wep_key *wepkey;
1788 int keyidx = 0;
1789
Ivo van Doorn1c014422008-04-17 19:41:02 +02001790 BUG_ON(ctl->hw_key->hw_key_idx > 3);
Zhu Yib481de92007-09-25 17:54:57 -07001791
1792 switch (keyinfo->alg) {
1793 case ALG_CCMP:
1794 cmd->cmd.tx.sec_ctl = TX_CMD_SEC_CCM;
1795 memcpy(cmd->cmd.tx.key, keyinfo->key, keyinfo->keylen);
Max Stepanov8236e182008-03-12 16:58:48 -07001796 if (ctl->flags & IEEE80211_TXCTL_AMPDU)
1797 cmd->cmd.tx.tx_flags |= TX_CMD_FLG_AGG_CCMP_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07001798 IWL_DEBUG_TX("tx_cmd with aes hwcrypto\n");
1799 break;
1800
1801 case ALG_TKIP:
Zhu Yib481de92007-09-25 17:54:57 -07001802 cmd->cmd.tx.sec_ctl = TX_CMD_SEC_TKIP;
Emmanuel Grumbach2bc75082008-03-19 16:41:45 -07001803 ieee80211_get_tkip_key(keyinfo->conf, skb_frag,
1804 IEEE80211_TKIP_P2_KEY, cmd->cmd.tx.key);
1805 IWL_DEBUG_TX("tx_cmd with tkip hwcrypto\n");
Zhu Yib481de92007-09-25 17:54:57 -07001806 break;
1807
1808 case ALG_WEP:
Ivo van Doorn1c014422008-04-17 19:41:02 +02001809 wepkey = &priv->wep_keys[ctl->hw_key->hw_key_idx];
Emmanuel Grumbach6974e362008-04-14 21:16:06 -07001810 cmd->cmd.tx.sec_ctl = 0;
1811 if (priv->default_wep_key) {
1812 /* the WEP key was sent as static */
Ivo van Doorn1c014422008-04-17 19:41:02 +02001813 keyidx = ctl->hw_key->hw_key_idx;
Emmanuel Grumbach6974e362008-04-14 21:16:06 -07001814 memcpy(&cmd->cmd.tx.key[3], wepkey->key,
1815 wepkey->key_size);
1816 if (wepkey->key_size == WEP_KEY_LEN_128)
1817 cmd->cmd.tx.sec_ctl |= TX_CMD_SEC_KEY128;
1818 } else {
Emmanuel Grumbach0211ddd2008-04-14 21:16:07 -07001819 /* the WEP key was sent as dynamic */
1820 keyidx = keyinfo->keyidx;
1821 memcpy(&cmd->cmd.tx.key[3], keyinfo->key,
1822 keyinfo->keylen);
1823 if (keyinfo->keylen == WEP_KEY_LEN_128)
1824 cmd->cmd.tx.sec_ctl |= TX_CMD_SEC_KEY128;
Emmanuel Grumbach6974e362008-04-14 21:16:06 -07001825 }
Zhu Yib481de92007-09-25 17:54:57 -07001826
Emmanuel Grumbach6974e362008-04-14 21:16:06 -07001827 cmd->cmd.tx.sec_ctl |= (TX_CMD_SEC_WEP |
1828 (keyidx & TX_CMD_SEC_MSK) << TX_CMD_SEC_SHIFT);
Zhu Yib481de92007-09-25 17:54:57 -07001829
1830 IWL_DEBUG_TX("Configuring packet for WEP encryption "
Emmanuel Grumbach6974e362008-04-14 21:16:06 -07001831 "with key %d\n", keyidx);
Zhu Yib481de92007-09-25 17:54:57 -07001832 break;
1833
Zhu Yib481de92007-09-25 17:54:57 -07001834 default:
1835 printk(KERN_ERR "Unknown encode alg %d\n", keyinfo->alg);
1836 break;
1837 }
1838}
1839
1840/*
1841 * handle build REPLY_TX command notification.
1842 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001843static void iwl4965_build_tx_cmd_basic(struct iwl_priv *priv,
Tomas Winkler857485c2008-03-21 13:53:44 -07001844 struct iwl_cmd *cmd,
Zhu Yib481de92007-09-25 17:54:57 -07001845 struct ieee80211_tx_control *ctrl,
1846 struct ieee80211_hdr *hdr,
1847 int is_unicast, u8 std_id)
1848{
1849 __le16 *qc;
1850 u16 fc = le16_to_cpu(hdr->frame_control);
1851 __le32 tx_flags = cmd->cmd.tx.tx_flags;
1852
1853 cmd->cmd.tx.stop_time.life_time = TX_CMD_LIFE_TIME_INFINITE;
1854 if (!(ctrl->flags & IEEE80211_TXCTL_NO_ACK)) {
1855 tx_flags |= TX_CMD_FLG_ACK_MSK;
1856 if ((fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_MGMT)
1857 tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
1858 if (ieee80211_is_probe_response(fc) &&
1859 !(le16_to_cpu(hdr->seq_ctrl) & 0xf))
1860 tx_flags |= TX_CMD_FLG_TSF_MSK;
1861 } else {
1862 tx_flags &= (~TX_CMD_FLG_ACK_MSK);
1863 tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
1864 }
1865
Tomas Winkler87e4f7d2008-01-14 17:46:16 -08001866 if (ieee80211_is_back_request(fc))
1867 tx_flags |= TX_CMD_FLG_ACK_MSK | TX_CMD_FLG_IMM_BA_RSP_MASK;
1868
1869
Zhu Yib481de92007-09-25 17:54:57 -07001870 cmd->cmd.tx.sta_id = std_id;
1871 if (ieee80211_get_morefrag(hdr))
1872 tx_flags |= TX_CMD_FLG_MORE_FRAG_MSK;
1873
1874 qc = ieee80211_get_qos_ctrl(hdr);
1875 if (qc) {
1876 cmd->cmd.tx.tid_tspec = (u8) (le16_to_cpu(*qc) & 0xf);
1877 tx_flags &= ~TX_CMD_FLG_SEQ_CTL_MSK;
1878 } else
1879 tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
1880
1881 if (ctrl->flags & IEEE80211_TXCTL_USE_RTS_CTS) {
1882 tx_flags |= TX_CMD_FLG_RTS_MSK;
1883 tx_flags &= ~TX_CMD_FLG_CTS_MSK;
1884 } else if (ctrl->flags & IEEE80211_TXCTL_USE_CTS_PROTECT) {
1885 tx_flags &= ~TX_CMD_FLG_RTS_MSK;
1886 tx_flags |= TX_CMD_FLG_CTS_MSK;
1887 }
1888
1889 if ((tx_flags & TX_CMD_FLG_RTS_MSK) || (tx_flags & TX_CMD_FLG_CTS_MSK))
1890 tx_flags |= TX_CMD_FLG_FULL_TXOP_PROT_MSK;
1891
1892 tx_flags &= ~(TX_CMD_FLG_ANT_SEL_MSK);
1893 if ((fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_MGMT) {
1894 if ((fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_ASSOC_REQ ||
1895 (fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_REASSOC_REQ)
Ian Schrambc434dd2007-10-25 17:15:29 +08001896 cmd->cmd.tx.timeout.pm_frame_timeout = cpu_to_le16(3);
Zhu Yib481de92007-09-25 17:54:57 -07001897 else
Ian Schrambc434dd2007-10-25 17:15:29 +08001898 cmd->cmd.tx.timeout.pm_frame_timeout = cpu_to_le16(2);
Mohamed Abbasab53d8a2008-03-25 16:33:36 -07001899 } else {
Zhu Yib481de92007-09-25 17:54:57 -07001900 cmd->cmd.tx.timeout.pm_frame_timeout = 0;
Mohamed Abbasab53d8a2008-03-25 16:33:36 -07001901 }
Zhu Yib481de92007-09-25 17:54:57 -07001902
1903 cmd->cmd.tx.driver_txop = 0;
1904 cmd->cmd.tx.tx_flags = tx_flags;
1905 cmd->cmd.tx.next_frame_len = 0;
1906}
Tomas Winkler19758be2008-03-12 16:58:51 -07001907static void iwl_update_tx_stats(struct iwl_priv *priv, u16 fc, u16 len)
1908{
1909 /* 0 - mgmt, 1 - cnt, 2 - data */
1910 int idx = (fc & IEEE80211_FCTL_FTYPE) >> 2;
1911 priv->tx_stats[idx].cnt++;
1912 priv->tx_stats[idx].bytes += len;
1913}
Cahill, Ben M6440adb2007-11-29 11:09:55 +08001914/**
1915 * iwl4965_get_sta_id - Find station's index within station table
1916 *
1917 * If new IBSS station, create new entry in station table
1918 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001919static int iwl4965_get_sta_id(struct iwl_priv *priv,
Ben Cahill9fbab512007-11-29 11:09:47 +08001920 struct ieee80211_hdr *hdr)
Zhu Yib481de92007-09-25 17:54:57 -07001921{
1922 int sta_id;
1923 u16 fc = le16_to_cpu(hdr->frame_control);
Joe Perches0795af52007-10-03 17:59:30 -07001924 DECLARE_MAC_BUF(mac);
Zhu Yib481de92007-09-25 17:54:57 -07001925
Cahill, Ben M6440adb2007-11-29 11:09:55 +08001926 /* If this frame is broadcast or management, use broadcast station id */
Zhu Yib481de92007-09-25 17:54:57 -07001927 if (((fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_DATA) ||
1928 is_multicast_ether_addr(hdr->addr1))
Tomas Winkler5425e492008-04-15 16:01:38 -07001929 return priv->hw_params.bcast_sta_id;
Zhu Yib481de92007-09-25 17:54:57 -07001930
1931 switch (priv->iw_mode) {
1932
Cahill, Ben M6440adb2007-11-29 11:09:55 +08001933 /* If we are a client station in a BSS network, use the special
1934 * AP station entry (that's the only station we communicate with) */
Zhu Yib481de92007-09-25 17:54:57 -07001935 case IEEE80211_IF_TYPE_STA:
1936 return IWL_AP_ID;
1937
1938 /* If we are an AP, then find the station, or use BCAST */
1939 case IEEE80211_IF_TYPE_AP:
Tomas Winkler947b13a2008-04-16 16:34:48 -07001940 sta_id = iwl_find_station(priv, hdr->addr1);
Zhu Yib481de92007-09-25 17:54:57 -07001941 if (sta_id != IWL_INVALID_STATION)
1942 return sta_id;
Tomas Winkler5425e492008-04-15 16:01:38 -07001943 return priv->hw_params.bcast_sta_id;
Zhu Yib481de92007-09-25 17:54:57 -07001944
Cahill, Ben M6440adb2007-11-29 11:09:55 +08001945 /* If this frame is going out to an IBSS network, find the station,
1946 * or create a new station table entry */
Zhu Yib481de92007-09-25 17:54:57 -07001947 case IEEE80211_IF_TYPE_IBSS:
Tomas Winkler947b13a2008-04-16 16:34:48 -07001948 sta_id = iwl_find_station(priv, hdr->addr1);
Zhu Yib481de92007-09-25 17:54:57 -07001949 if (sta_id != IWL_INVALID_STATION)
1950 return sta_id;
1951
Cahill, Ben M6440adb2007-11-29 11:09:55 +08001952 /* Create new station table entry */
Ron Rindjunsky67d62032007-11-26 16:14:40 +02001953 sta_id = iwl4965_add_station_flags(priv, hdr->addr1,
1954 0, CMD_ASYNC, NULL);
Zhu Yib481de92007-09-25 17:54:57 -07001955
1956 if (sta_id != IWL_INVALID_STATION)
1957 return sta_id;
1958
Joe Perches0795af52007-10-03 17:59:30 -07001959 IWL_DEBUG_DROP("Station %s not in station map. "
Zhu Yib481de92007-09-25 17:54:57 -07001960 "Defaulting to broadcast...\n",
Joe Perches0795af52007-10-03 17:59:30 -07001961 print_mac(mac, hdr->addr1));
Tomas Winkler0a6857e2008-03-12 16:58:49 -07001962 iwl_print_hex_dump(IWL_DL_DROP, (u8 *) hdr, sizeof(*hdr));
Tomas Winkler5425e492008-04-15 16:01:38 -07001963 return priv->hw_params.bcast_sta_id;
Zhu Yib481de92007-09-25 17:54:57 -07001964
1965 default:
Ian Schram01ebd062007-10-25 17:15:22 +08001966 IWL_WARNING("Unknown mode of operation: %d", priv->iw_mode);
Tomas Winkler5425e492008-04-15 16:01:38 -07001967 return priv->hw_params.bcast_sta_id;
Zhu Yib481de92007-09-25 17:54:57 -07001968 }
1969}
1970
1971/*
1972 * start REPLY_TX command process
1973 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001974static int iwl4965_tx_skb(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07001975 struct sk_buff *skb, struct ieee80211_tx_control *ctl)
1976{
1977 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001978 struct iwl4965_tfd_frame *tfd;
Zhu Yib481de92007-09-25 17:54:57 -07001979 u32 *control_flags;
1980 int txq_id = ctl->queue;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001981 struct iwl4965_tx_queue *txq = NULL;
1982 struct iwl4965_queue *q = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07001983 dma_addr_t phys_addr;
1984 dma_addr_t txcmd_phys;
Tomas Winkler87e4f7d2008-01-14 17:46:16 -08001985 dma_addr_t scratch_phys;
Tomas Winkler857485c2008-03-21 13:53:44 -07001986 struct iwl_cmd *out_cmd = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07001987 u16 len, idx, len_org;
1988 u8 id, hdr_len, unicast;
1989 u8 sta_id;
1990 u16 seq_number = 0;
1991 u16 fc;
1992 __le16 *qc;
1993 u8 wait_write_ptr = 0;
1994 unsigned long flags;
1995 int rc;
1996
1997 spin_lock_irqsave(&priv->lock, flags);
Tomas Winklerfee12472008-04-03 16:05:21 -07001998 if (iwl_is_rfkill(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07001999 IWL_DEBUG_DROP("Dropping - RF KILL\n");
2000 goto drop_unlock;
2001 }
2002
Johannes Berg32bfd352007-12-19 01:31:26 +01002003 if (!priv->vif) {
2004 IWL_DEBUG_DROP("Dropping - !priv->vif\n");
Zhu Yib481de92007-09-25 17:54:57 -07002005 goto drop_unlock;
2006 }
2007
Johannes Berg8318d782008-01-24 19:38:38 +01002008 if ((ctl->tx_rate->hw_value & 0xFF) == IWL_INVALID_RATE) {
Zhu Yib481de92007-09-25 17:54:57 -07002009 IWL_ERROR("ERROR: No TX rate available.\n");
2010 goto drop_unlock;
2011 }
2012
2013 unicast = !is_multicast_ether_addr(hdr->addr1);
2014 id = 0;
2015
2016 fc = le16_to_cpu(hdr->frame_control);
2017
Tomas Winkler0a6857e2008-03-12 16:58:49 -07002018#ifdef CONFIG_IWLWIFI_DEBUG
Zhu Yib481de92007-09-25 17:54:57 -07002019 if (ieee80211_is_auth(fc))
2020 IWL_DEBUG_TX("Sending AUTH frame\n");
2021 else if (ieee80211_is_assoc_request(fc))
2022 IWL_DEBUG_TX("Sending ASSOC frame\n");
2023 else if (ieee80211_is_reassoc_request(fc))
2024 IWL_DEBUG_TX("Sending REASSOC frame\n");
2025#endif
2026
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08002027 /* drop all data frame if we are not associated */
Gregory Greenman76f39152008-01-23 10:15:21 -08002028 if (((fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_DATA) &&
Tomas Winkler3109ece2008-03-28 16:33:35 -07002029 (!iwl_is_associated(priv) ||
Reinette Chatrea6477242008-02-14 10:40:28 -08002030 ((priv->iw_mode == IEEE80211_IF_TYPE_STA) && !priv->assoc_id) ||
Gregory Greenman76f39152008-01-23 10:15:21 -08002031 !priv->assoc_station_added)) {
Tomas Winkler3109ece2008-03-28 16:33:35 -07002032 IWL_DEBUG_DROP("Dropping - !iwl_is_associated\n");
Zhu Yib481de92007-09-25 17:54:57 -07002033 goto drop_unlock;
2034 }
2035
2036 spin_unlock_irqrestore(&priv->lock, flags);
2037
2038 hdr_len = ieee80211_get_hdrlen(fc);
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002039
2040 /* Find (or create) index into station table for destination station */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002041 sta_id = iwl4965_get_sta_id(priv, hdr);
Zhu Yib481de92007-09-25 17:54:57 -07002042 if (sta_id == IWL_INVALID_STATION) {
Joe Perches0795af52007-10-03 17:59:30 -07002043 DECLARE_MAC_BUF(mac);
2044
2045 IWL_DEBUG_DROP("Dropping - INVALID STATION: %s\n",
2046 print_mac(mac, hdr->addr1));
Zhu Yib481de92007-09-25 17:54:57 -07002047 goto drop;
2048 }
2049
Guy Cohen07bc28e2008-04-23 17:15:03 -07002050 IWL_DEBUG_TX("station Id %d\n", sta_id);
Zhu Yib481de92007-09-25 17:54:57 -07002051
2052 qc = ieee80211_get_qos_ctrl(hdr);
2053 if (qc) {
2054 u8 tid = (u8)(le16_to_cpu(*qc) & 0xf);
2055 seq_number = priv->stations[sta_id].tid[tid].seq_number &
2056 IEEE80211_SCTL_SEQ;
2057 hdr->seq_ctrl = cpu_to_le16(seq_number) |
2058 (hdr->seq_ctrl &
2059 __constant_cpu_to_le16(IEEE80211_SCTL_FRAG));
2060 seq_number += 0x10;
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08002061#ifdef CONFIG_IWL4965_HT
Zhu Yib481de92007-09-25 17:54:57 -07002062 /* aggregation is on for this <sta,tid> */
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02002063 if (ctl->flags & IEEE80211_TXCTL_AMPDU)
Zhu Yib481de92007-09-25 17:54:57 -07002064 txq_id = priv->stations[sta_id].tid[tid].agg.txq_id;
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02002065 priv->stations[sta_id].tid[tid].tfds_in_queue++;
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08002066#endif /* CONFIG_IWL4965_HT */
Zhu Yib481de92007-09-25 17:54:57 -07002067 }
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002068
2069 /* Descriptor for chosen Tx queue */
Zhu Yib481de92007-09-25 17:54:57 -07002070 txq = &priv->txq[txq_id];
2071 q = &txq->q;
2072
2073 spin_lock_irqsave(&priv->lock, flags);
2074
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002075 /* Set up first empty TFD within this queue's circular TFD buffer */
Tomas Winklerfc4b6852007-10-25 17:15:24 +08002076 tfd = &txq->bd[q->write_ptr];
Zhu Yib481de92007-09-25 17:54:57 -07002077 memset(tfd, 0, sizeof(*tfd));
2078 control_flags = (u32 *) tfd;
Tomas Winklerfc4b6852007-10-25 17:15:24 +08002079 idx = get_cmd_index(q, q->write_ptr, 0);
Zhu Yib481de92007-09-25 17:54:57 -07002080
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002081 /* Set up driver data for this TFD */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002082 memset(&(txq->txb[q->write_ptr]), 0, sizeof(struct iwl4965_tx_info));
Tomas Winklerfc4b6852007-10-25 17:15:24 +08002083 txq->txb[q->write_ptr].skb[0] = skb;
2084 memcpy(&(txq->txb[q->write_ptr].status.control),
Zhu Yib481de92007-09-25 17:54:57 -07002085 ctl, sizeof(struct ieee80211_tx_control));
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002086
2087 /* Set up first empty entry in queue's array of Tx/cmd buffers */
Zhu Yib481de92007-09-25 17:54:57 -07002088 out_cmd = &txq->cmd[idx];
2089 memset(&out_cmd->hdr, 0, sizeof(out_cmd->hdr));
2090 memset(&out_cmd->cmd.tx, 0, sizeof(out_cmd->cmd.tx));
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002091
2092 /*
2093 * Set up the Tx-command (not MAC!) header.
2094 * Store the chosen Tx queue and TFD index within the sequence field;
2095 * after Tx, uCode's Tx response will return this value so driver can
2096 * locate the frame within the tx queue and do post-tx processing.
2097 */
Zhu Yib481de92007-09-25 17:54:57 -07002098 out_cmd->hdr.cmd = REPLY_TX;
2099 out_cmd->hdr.sequence = cpu_to_le16((u16)(QUEUE_TO_SEQ(txq_id) |
Tomas Winklerfc4b6852007-10-25 17:15:24 +08002100 INDEX_TO_SEQ(q->write_ptr)));
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002101
2102 /* Copy MAC header from skb into command buffer */
Zhu Yib481de92007-09-25 17:54:57 -07002103 memcpy(out_cmd->cmd.tx.hdr, hdr, hdr_len);
2104
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002105 /*
2106 * Use the first empty entry in this queue's command buffer array
2107 * to contain the Tx command and MAC header concatenated together
2108 * (payload data will be in another buffer).
2109 * Size of this varies, due to varying MAC header length.
2110 * If end is not dword aligned, we'll have 2 extra bytes at the end
2111 * of the MAC header (device reads on dword boundaries).
2112 * We'll tell device about this padding later.
2113 */
Tomas Winkler5425e492008-04-15 16:01:38 -07002114 len = priv->hw_params.tx_cmd_len +
Tomas Winkler857485c2008-03-21 13:53:44 -07002115 sizeof(struct iwl_cmd_header) + hdr_len;
Zhu Yib481de92007-09-25 17:54:57 -07002116
2117 len_org = len;
2118 len = (len + 3) & ~3;
2119
2120 if (len_org != len)
2121 len_org = 1;
2122 else
2123 len_org = 0;
2124
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002125 /* Physical address of this Tx command's header (not MAC header!),
2126 * within command buffer array. */
Tomas Winkler857485c2008-03-21 13:53:44 -07002127 txcmd_phys = txq->dma_addr_cmd + sizeof(struct iwl_cmd) * idx +
2128 offsetof(struct iwl_cmd, hdr);
Zhu Yib481de92007-09-25 17:54:57 -07002129
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002130 /* Add buffer containing Tx command and MAC(!) header to TFD's
2131 * first entry */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002132 iwl4965_hw_txq_attach_buf_to_tfd(priv, tfd, txcmd_phys, len);
Zhu Yib481de92007-09-25 17:54:57 -07002133
2134 if (!(ctl->flags & IEEE80211_TXCTL_DO_NOT_ENCRYPT))
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07002135 iwl4965_build_tx_cmd_hwcrypto(priv, ctl, out_cmd, skb, sta_id);
Zhu Yib481de92007-09-25 17:54:57 -07002136
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002137 /* Set up TFD's 2nd entry to point directly to remainder of skb,
2138 * if any (802.11 null frames have no payload). */
Zhu Yib481de92007-09-25 17:54:57 -07002139 len = skb->len - hdr_len;
2140 if (len) {
2141 phys_addr = pci_map_single(priv->pci_dev, skb->data + hdr_len,
2142 len, PCI_DMA_TODEVICE);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002143 iwl4965_hw_txq_attach_buf_to_tfd(priv, tfd, phys_addr, len);
Zhu Yib481de92007-09-25 17:54:57 -07002144 }
2145
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002146 /* Tell 4965 about any 2-byte padding after MAC header */
Zhu Yib481de92007-09-25 17:54:57 -07002147 if (len_org)
2148 out_cmd->cmd.tx.tx_flags |= TX_CMD_FLG_MH_PAD_MSK;
2149
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002150 /* Total # bytes to be transmitted */
Zhu Yib481de92007-09-25 17:54:57 -07002151 len = (u16)skb->len;
2152 out_cmd->cmd.tx.len = cpu_to_le16(len);
2153
2154 /* TODO need this for burst mode later on */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002155 iwl4965_build_tx_cmd_basic(priv, out_cmd, ctl, hdr, unicast, sta_id);
Zhu Yib481de92007-09-25 17:54:57 -07002156
2157 /* set is_hcca to 0; it probably will never be implemented */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002158 iwl4965_hw_build_tx_cmd_rate(priv, out_cmd, ctl, hdr, sta_id, 0);
Zhu Yib481de92007-09-25 17:54:57 -07002159
Tomas Winkler19758be2008-03-12 16:58:51 -07002160 iwl_update_tx_stats(priv, fc, len);
2161
Tomas Winkler857485c2008-03-21 13:53:44 -07002162 scratch_phys = txcmd_phys + sizeof(struct iwl_cmd_header) +
Tomas Winkler87e4f7d2008-01-14 17:46:16 -08002163 offsetof(struct iwl4965_tx_cmd, scratch);
2164 out_cmd->cmd.tx.dram_lsb_ptr = cpu_to_le32(scratch_phys);
2165 out_cmd->cmd.tx.dram_msb_ptr = iwl_get_dma_hi_address(scratch_phys);
2166
Zhu Yib481de92007-09-25 17:54:57 -07002167 if (!ieee80211_get_morefrag(hdr)) {
2168 txq->need_update = 1;
2169 if (qc) {
2170 u8 tid = (u8)(le16_to_cpu(*qc) & 0xf);
2171 priv->stations[sta_id].tid[tid].seq_number = seq_number;
2172 }
2173 } else {
2174 wait_write_ptr = 1;
2175 txq->need_update = 0;
2176 }
2177
Tomas Winkler0a6857e2008-03-12 16:58:49 -07002178 iwl_print_hex_dump(IWL_DL_TX, out_cmd->cmd.payload,
Zhu Yib481de92007-09-25 17:54:57 -07002179 sizeof(out_cmd->cmd.tx));
2180
Tomas Winkler0a6857e2008-03-12 16:58:49 -07002181 iwl_print_hex_dump(IWL_DL_TX, (u8 *)out_cmd->cmd.tx.hdr,
Zhu Yib481de92007-09-25 17:54:57 -07002182 ieee80211_get_hdrlen(fc));
2183
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002184 /* Set up entry for this TFD in Tx byte-count array */
Tomas Winklere2a722e2008-04-14 21:16:10 -07002185 priv->cfg->ops->lib->txq_update_byte_cnt_tbl(priv, txq, len);
Zhu Yib481de92007-09-25 17:54:57 -07002186
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002187 /* Tell device the write index *just past* this latest filled TFD */
Tomas Winklerc54b6792008-03-06 17:36:53 -08002188 q->write_ptr = iwl_queue_inc_wrap(q->write_ptr, q->n_bd);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002189 rc = iwl4965_tx_queue_update_write_ptr(priv, txq);
Zhu Yib481de92007-09-25 17:54:57 -07002190 spin_unlock_irqrestore(&priv->lock, flags);
2191
2192 if (rc)
2193 return rc;
2194
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002195 if ((iwl4965_queue_space(q) < q->high_mark)
Zhu Yib481de92007-09-25 17:54:57 -07002196 && priv->mac80211_registered) {
2197 if (wait_write_ptr) {
2198 spin_lock_irqsave(&priv->lock, flags);
2199 txq->need_update = 1;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002200 iwl4965_tx_queue_update_write_ptr(priv, txq);
Zhu Yib481de92007-09-25 17:54:57 -07002201 spin_unlock_irqrestore(&priv->lock, flags);
2202 }
2203
2204 ieee80211_stop_queue(priv->hw, ctl->queue);
2205 }
2206
2207 return 0;
2208
2209drop_unlock:
2210 spin_unlock_irqrestore(&priv->lock, flags);
2211drop:
2212 return -1;
2213}
2214
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002215static void iwl4965_set_rate(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002216{
Johannes Berg8318d782008-01-24 19:38:38 +01002217 const struct ieee80211_supported_band *hw = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07002218 struct ieee80211_rate *rate;
2219 int i;
2220
Emmanuel Grumbachd1141df2008-04-21 15:42:00 -07002221 hw = iwl_get_hw_mode(priv, priv->band);
Saleem Abdulrasoolc4ba9622007-11-18 23:59:08 -08002222 if (!hw) {
2223 IWL_ERROR("Failed to set rate: unable to get hw mode\n");
2224 return;
2225 }
Zhu Yib481de92007-09-25 17:54:57 -07002226
2227 priv->active_rate = 0;
2228 priv->active_rate_basic = 0;
2229
Johannes Berg8318d782008-01-24 19:38:38 +01002230 for (i = 0; i < hw->n_bitrates; i++) {
2231 rate = &(hw->bitrates[i]);
2232 if (rate->hw_value < IWL_RATE_COUNT)
2233 priv->active_rate |= (1 << rate->hw_value);
Zhu Yib481de92007-09-25 17:54:57 -07002234 }
2235
2236 IWL_DEBUG_RATE("Set active_rate = %0x, active_rate_basic = %0x\n",
2237 priv->active_rate, priv->active_rate_basic);
2238
2239 /*
2240 * If a basic rate is configured, then use it (adding IWL_RATE_1M_MASK)
2241 * otherwise set it to the default of all CCK rates and 6, 12, 24 for
2242 * OFDM
2243 */
2244 if (priv->active_rate_basic & IWL_CCK_BASIC_RATES_MASK)
2245 priv->staging_rxon.cck_basic_rates =
2246 ((priv->active_rate_basic &
2247 IWL_CCK_RATES_MASK) >> IWL_FIRST_CCK_RATE) & 0xF;
2248 else
2249 priv->staging_rxon.cck_basic_rates =
2250 (IWL_CCK_BASIC_RATES_MASK >> IWL_FIRST_CCK_RATE) & 0xF;
2251
2252 if (priv->active_rate_basic & IWL_OFDM_BASIC_RATES_MASK)
2253 priv->staging_rxon.ofdm_basic_rates =
2254 ((priv->active_rate_basic &
2255 (IWL_OFDM_BASIC_RATES_MASK | IWL_RATE_6M_MASK)) >>
2256 IWL_FIRST_OFDM_RATE) & 0xFF;
2257 else
2258 priv->staging_rxon.ofdm_basic_rates =
2259 (IWL_OFDM_BASIC_RATES_MASK >> IWL_FIRST_OFDM_RATE) & 0xFF;
2260}
2261
Mohamed Abbasad97edd2008-03-28 16:21:06 -07002262void iwl4965_radio_kill_sw(struct iwl_priv *priv, int disable_radio)
Zhu Yib481de92007-09-25 17:54:57 -07002263{
2264 unsigned long flags;
2265
2266 if (!!disable_radio == test_bit(STATUS_RF_KILL_SW, &priv->status))
2267 return;
2268
2269 IWL_DEBUG_RF_KILL("Manual SW RF KILL set to: RADIO %s\n",
2270 disable_radio ? "OFF" : "ON");
2271
2272 if (disable_radio) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002273 iwl4965_scan_cancel(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002274 /* FIXME: This is a workaround for AP */
2275 if (priv->iw_mode != IEEE80211_IF_TYPE_AP) {
2276 spin_lock_irqsave(&priv->lock, flags);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07002277 iwl_write32(priv, CSR_UCODE_DRV_GP1_SET,
Zhu Yib481de92007-09-25 17:54:57 -07002278 CSR_UCODE_SW_BIT_RFKILL);
2279 spin_unlock_irqrestore(&priv->lock, flags);
Mohamed Abbasad97edd2008-03-28 16:21:06 -07002280 /* call the host command only if no hw rf-kill set */
Mohamed Abbas59003832008-04-15 16:01:46 -07002281 if (!test_bit(STATUS_RF_KILL_HW, &priv->status) &&
2282 iwl_is_ready(priv))
Mohamed Abbasad97edd2008-03-28 16:21:06 -07002283 iwl4965_send_card_state(priv,
2284 CARD_STATE_CMD_DISABLE,
2285 0);
Zhu Yib481de92007-09-25 17:54:57 -07002286 set_bit(STATUS_RF_KILL_SW, &priv->status);
Mohamed Abbasad97edd2008-03-28 16:21:06 -07002287
2288 /* make sure mac80211 stop sending Tx frame */
2289 if (priv->mac80211_registered)
2290 ieee80211_stop_queues(priv->hw);
Zhu Yib481de92007-09-25 17:54:57 -07002291 }
2292 return;
2293 }
2294
2295 spin_lock_irqsave(&priv->lock, flags);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07002296 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
Zhu Yib481de92007-09-25 17:54:57 -07002297
2298 clear_bit(STATUS_RF_KILL_SW, &priv->status);
2299 spin_unlock_irqrestore(&priv->lock, flags);
2300
2301 /* wake up ucode */
2302 msleep(10);
2303
2304 spin_lock_irqsave(&priv->lock, flags);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07002305 iwl_read32(priv, CSR_UCODE_DRV_GP1);
2306 if (!iwl_grab_nic_access(priv))
2307 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002308 spin_unlock_irqrestore(&priv->lock, flags);
2309
2310 if (test_bit(STATUS_RF_KILL_HW, &priv->status)) {
2311 IWL_DEBUG_RF_KILL("Can not turn radio back on - "
2312 "disabled by HW switch\n");
2313 return;
2314 }
2315
2316 queue_work(priv->workqueue, &priv->restart);
2317 return;
2318}
2319
Zhu Yib481de92007-09-25 17:54:57 -07002320#define IWL_PACKET_RETRY_TIME HZ
2321
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002322int iwl4965_is_duplicate_packet(struct iwl_priv *priv, struct ieee80211_hdr *header)
Zhu Yib481de92007-09-25 17:54:57 -07002323{
2324 u16 sc = le16_to_cpu(header->seq_ctrl);
2325 u16 seq = (sc & IEEE80211_SCTL_SEQ) >> 4;
2326 u16 frag = sc & IEEE80211_SCTL_FRAG;
2327 u16 *last_seq, *last_frag;
2328 unsigned long *last_time;
2329
2330 switch (priv->iw_mode) {
2331 case IEEE80211_IF_TYPE_IBSS:{
2332 struct list_head *p;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002333 struct iwl4965_ibss_seq *entry = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07002334 u8 *mac = header->addr2;
2335 int index = mac[5] & (IWL_IBSS_MAC_HASH_SIZE - 1);
2336
2337 __list_for_each(p, &priv->ibss_mac_hash[index]) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002338 entry = list_entry(p, struct iwl4965_ibss_seq, list);
Zhu Yib481de92007-09-25 17:54:57 -07002339 if (!compare_ether_addr(entry->mac, mac))
2340 break;
2341 }
2342 if (p == &priv->ibss_mac_hash[index]) {
2343 entry = kzalloc(sizeof(*entry), GFP_ATOMIC);
2344 if (!entry) {
Ian Schrambc434dd2007-10-25 17:15:29 +08002345 IWL_ERROR("Cannot malloc new mac entry\n");
Zhu Yib481de92007-09-25 17:54:57 -07002346 return 0;
2347 }
2348 memcpy(entry->mac, mac, ETH_ALEN);
2349 entry->seq_num = seq;
2350 entry->frag_num = frag;
2351 entry->packet_time = jiffies;
Ian Schrambc434dd2007-10-25 17:15:29 +08002352 list_add(&entry->list, &priv->ibss_mac_hash[index]);
Zhu Yib481de92007-09-25 17:54:57 -07002353 return 0;
2354 }
2355 last_seq = &entry->seq_num;
2356 last_frag = &entry->frag_num;
2357 last_time = &entry->packet_time;
2358 break;
2359 }
2360 case IEEE80211_IF_TYPE_STA:
2361 last_seq = &priv->last_seq_num;
2362 last_frag = &priv->last_frag_num;
2363 last_time = &priv->last_packet_time;
2364 break;
2365 default:
2366 return 0;
2367 }
2368 if ((*last_seq == seq) &&
2369 time_after(*last_time + IWL_PACKET_RETRY_TIME, jiffies)) {
2370 if (*last_frag == frag)
2371 goto drop;
2372 if (*last_frag + 1 != frag)
2373 /* out-of-order fragment */
2374 goto drop;
2375 } else
2376 *last_seq = seq;
2377
2378 *last_frag = frag;
2379 *last_time = jiffies;
2380 return 0;
2381
2382 drop:
2383 return 1;
2384}
2385
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08002386#ifdef CONFIG_IWL4965_SPECTRUM_MEASUREMENT
Zhu Yib481de92007-09-25 17:54:57 -07002387
2388#include "iwl-spectrum.h"
2389
2390#define BEACON_TIME_MASK_LOW 0x00FFFFFF
2391#define BEACON_TIME_MASK_HIGH 0xFF000000
2392#define TIME_UNIT 1024
2393
2394/*
2395 * extended beacon time format
2396 * time in usec will be changed into a 32-bit value in 8:24 format
2397 * the high 1 byte is the beacon counts
2398 * the lower 3 bytes is the time in usec within one beacon interval
2399 */
2400
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002401static u32 iwl4965_usecs_to_beacons(u32 usec, u32 beacon_interval)
Zhu Yib481de92007-09-25 17:54:57 -07002402{
2403 u32 quot;
2404 u32 rem;
2405 u32 interval = beacon_interval * 1024;
2406
2407 if (!interval || !usec)
2408 return 0;
2409
2410 quot = (usec / interval) & (BEACON_TIME_MASK_HIGH >> 24);
2411 rem = (usec % interval) & BEACON_TIME_MASK_LOW;
2412
2413 return (quot << 24) + rem;
2414}
2415
2416/* base is usually what we get from ucode with each received frame,
2417 * the same as HW timer counter counting down
2418 */
2419
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002420static __le32 iwl4965_add_beacon_time(u32 base, u32 addon, u32 beacon_interval)
Zhu Yib481de92007-09-25 17:54:57 -07002421{
2422 u32 base_low = base & BEACON_TIME_MASK_LOW;
2423 u32 addon_low = addon & BEACON_TIME_MASK_LOW;
2424 u32 interval = beacon_interval * TIME_UNIT;
2425 u32 res = (base & BEACON_TIME_MASK_HIGH) +
2426 (addon & BEACON_TIME_MASK_HIGH);
2427
2428 if (base_low > addon_low)
2429 res += base_low - addon_low;
2430 else if (base_low < addon_low) {
2431 res += interval + base_low - addon_low;
2432 res += (1 << 24);
2433 } else
2434 res += (1 << 24);
2435
2436 return cpu_to_le32(res);
2437}
2438
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002439static int iwl4965_get_measurement(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07002440 struct ieee80211_measurement_params *params,
2441 u8 type)
2442{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002443 struct iwl4965_spectrum_cmd spectrum;
2444 struct iwl4965_rx_packet *res;
Tomas Winkler857485c2008-03-21 13:53:44 -07002445 struct iwl_host_cmd cmd = {
Zhu Yib481de92007-09-25 17:54:57 -07002446 .id = REPLY_SPECTRUM_MEASUREMENT_CMD,
2447 .data = (void *)&spectrum,
2448 .meta.flags = CMD_WANT_SKB,
2449 };
2450 u32 add_time = le64_to_cpu(params->start_time);
2451 int rc;
2452 int spectrum_resp_status;
2453 int duration = le16_to_cpu(params->duration);
2454
Tomas Winkler3109ece2008-03-28 16:33:35 -07002455 if (iwl_is_associated(priv))
Zhu Yib481de92007-09-25 17:54:57 -07002456 add_time =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002457 iwl4965_usecs_to_beacons(
Zhu Yib481de92007-09-25 17:54:57 -07002458 le64_to_cpu(params->start_time) - priv->last_tsf,
2459 le16_to_cpu(priv->rxon_timing.beacon_interval));
2460
2461 memset(&spectrum, 0, sizeof(spectrum));
2462
2463 spectrum.channel_count = cpu_to_le16(1);
2464 spectrum.flags =
2465 RXON_FLG_TSF2HOST_MSK | RXON_FLG_ANT_A_MSK | RXON_FLG_DIS_DIV_MSK;
2466 spectrum.filter_flags = MEASUREMENT_FILTER_FLAG;
2467 cmd.len = sizeof(spectrum);
2468 spectrum.len = cpu_to_le16(cmd.len - sizeof(spectrum.len));
2469
Tomas Winkler3109ece2008-03-28 16:33:35 -07002470 if (iwl_is_associated(priv))
Zhu Yib481de92007-09-25 17:54:57 -07002471 spectrum.start_time =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002472 iwl4965_add_beacon_time(priv->last_beacon_time,
Zhu Yib481de92007-09-25 17:54:57 -07002473 add_time,
2474 le16_to_cpu(priv->rxon_timing.beacon_interval));
2475 else
2476 spectrum.start_time = 0;
2477
2478 spectrum.channels[0].duration = cpu_to_le32(duration * TIME_UNIT);
2479 spectrum.channels[0].channel = params->channel;
2480 spectrum.channels[0].type = type;
2481 if (priv->active_rxon.flags & RXON_FLG_BAND_24G_MSK)
2482 spectrum.flags |= RXON_FLG_BAND_24G_MSK |
2483 RXON_FLG_AUTO_DETECT_MSK | RXON_FLG_TGG_PROTECT_MSK;
2484
Tomas Winkler857485c2008-03-21 13:53:44 -07002485 rc = iwl_send_cmd_sync(priv, &cmd);
Zhu Yib481de92007-09-25 17:54:57 -07002486 if (rc)
2487 return rc;
2488
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002489 res = (struct iwl4965_rx_packet *)cmd.meta.u.skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07002490 if (res->hdr.flags & IWL_CMD_FAILED_MSK) {
2491 IWL_ERROR("Bad return from REPLY_RX_ON_ASSOC command\n");
2492 rc = -EIO;
2493 }
2494
2495 spectrum_resp_status = le16_to_cpu(res->u.spectrum.status);
2496 switch (spectrum_resp_status) {
2497 case 0: /* Command will be handled */
2498 if (res->u.spectrum.id != 0xff) {
2499 IWL_DEBUG_INFO
2500 ("Replaced existing measurement: %d\n",
2501 res->u.spectrum.id);
2502 priv->measurement_status &= ~MEASUREMENT_READY;
2503 }
2504 priv->measurement_status |= MEASUREMENT_ACTIVE;
2505 rc = 0;
2506 break;
2507
2508 case 1: /* Command will not be handled */
2509 rc = -EAGAIN;
2510 break;
2511 }
2512
2513 dev_kfree_skb_any(cmd.meta.u.skb);
2514
2515 return rc;
2516}
2517#endif
2518
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002519static void iwl4965_txstatus_to_ieee(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002520 struct iwl4965_tx_info *tx_sta)
Zhu Yib481de92007-09-25 17:54:57 -07002521{
2522
2523 tx_sta->status.ack_signal = 0;
2524 tx_sta->status.excessive_retries = 0;
2525 tx_sta->status.queue_length = 0;
2526 tx_sta->status.queue_number = 0;
2527
2528 if (in_interrupt())
2529 ieee80211_tx_status_irqsafe(priv->hw,
2530 tx_sta->skb[0], &(tx_sta->status));
2531 else
2532 ieee80211_tx_status(priv->hw,
2533 tx_sta->skb[0], &(tx_sta->status));
2534
2535 tx_sta->skb[0] = NULL;
2536}
2537
2538/**
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002539 * iwl4965_tx_queue_reclaim - Reclaim Tx queue entries already Tx'd
Zhu Yib481de92007-09-25 17:54:57 -07002540 *
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002541 * When FW advances 'R' index, all entries between old and new 'R' index
2542 * need to be reclaimed. As result, some free space forms. If there is
2543 * enough free space (> low mark), wake the stack that feeds us.
Zhu Yib481de92007-09-25 17:54:57 -07002544 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002545int iwl4965_tx_queue_reclaim(struct iwl_priv *priv, int txq_id, int index)
Zhu Yib481de92007-09-25 17:54:57 -07002546{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002547 struct iwl4965_tx_queue *txq = &priv->txq[txq_id];
2548 struct iwl4965_queue *q = &txq->q;
Zhu Yib481de92007-09-25 17:54:57 -07002549 int nfreed = 0;
2550
2551 if ((index >= q->n_bd) || (x2_queue_used(q, index) == 0)) {
2552 IWL_ERROR("Read index for DMA queue txq id (%d), index %d, "
2553 "is out of range [0-%d] %d %d.\n", txq_id,
Tomas Winklerfc4b6852007-10-25 17:15:24 +08002554 index, q->n_bd, q->write_ptr, q->read_ptr);
Zhu Yib481de92007-09-25 17:54:57 -07002555 return 0;
2556 }
2557
Tomas Winklerc54b6792008-03-06 17:36:53 -08002558 for (index = iwl_queue_inc_wrap(index, q->n_bd);
Tomas Winklerfc4b6852007-10-25 17:15:24 +08002559 q->read_ptr != index;
Tomas Winklerc54b6792008-03-06 17:36:53 -08002560 q->read_ptr = iwl_queue_inc_wrap(q->read_ptr, q->n_bd)) {
Zhu Yib481de92007-09-25 17:54:57 -07002561 if (txq_id != IWL_CMD_QUEUE_NUM) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002562 iwl4965_txstatus_to_ieee(priv,
Tomas Winklerfc4b6852007-10-25 17:15:24 +08002563 &(txq->txb[txq->q.read_ptr]));
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002564 iwl4965_hw_txq_free_tfd(priv, txq);
Zhu Yib481de92007-09-25 17:54:57 -07002565 } else if (nfreed > 1) {
2566 IWL_ERROR("HCMD skipped: index (%d) %d %d\n", index,
Tomas Winklerfc4b6852007-10-25 17:15:24 +08002567 q->write_ptr, q->read_ptr);
Zhu Yib481de92007-09-25 17:54:57 -07002568 queue_work(priv->workqueue, &priv->restart);
2569 }
2570 nfreed++;
2571 }
2572
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02002573/* if (iwl4965_queue_space(q) > q->low_mark && (txq_id >= 0) &&
Zhu Yib481de92007-09-25 17:54:57 -07002574 (txq_id != IWL_CMD_QUEUE_NUM) &&
2575 priv->mac80211_registered)
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02002576 ieee80211_wake_queue(priv->hw, txq_id); */
Zhu Yib481de92007-09-25 17:54:57 -07002577
2578
2579 return nfreed;
2580}
2581
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002582static int iwl4965_is_tx_success(u32 status)
Zhu Yib481de92007-09-25 17:54:57 -07002583{
2584 status &= TX_STATUS_MSK;
2585 return (status == TX_STATUS_SUCCESS)
2586 || (status == TX_STATUS_DIRECT_DONE);
2587}
2588
2589/******************************************************************************
2590 *
2591 * Generic RX handler implementations
2592 *
2593 ******************************************************************************/
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08002594#ifdef CONFIG_IWL4965_HT
Zhu Yib481de92007-09-25 17:54:57 -07002595
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002596static inline int iwl4965_get_ra_sta_id(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07002597 struct ieee80211_hdr *hdr)
2598{
2599 if (priv->iw_mode == IEEE80211_IF_TYPE_STA)
2600 return IWL_AP_ID;
2601 else {
2602 u8 *da = ieee80211_get_DA(hdr);
Tomas Winkler947b13a2008-04-16 16:34:48 -07002603 return iwl_find_station(priv, da);
Zhu Yib481de92007-09-25 17:54:57 -07002604 }
2605}
2606
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002607static struct ieee80211_hdr *iwl4965_tx_queue_get_hdr(
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002608 struct iwl_priv *priv, int txq_id, int idx)
Zhu Yib481de92007-09-25 17:54:57 -07002609{
2610 if (priv->txq[txq_id].txb[idx].skb[0])
2611 return (struct ieee80211_hdr *)priv->txq[txq_id].
2612 txb[idx].skb[0]->data;
2613 return NULL;
2614}
2615
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002616static inline u32 iwl4965_get_scd_ssn(struct iwl4965_tx_resp *tx_resp)
Zhu Yib481de92007-09-25 17:54:57 -07002617{
2618 __le32 *scd_ssn = (__le32 *)((u32 *)&tx_resp->status +
2619 tx_resp->frame_count);
2620 return le32_to_cpu(*scd_ssn) & MAX_SN;
2621
2622}
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002623
2624/**
2625 * iwl4965_tx_status_reply_tx - Handle Tx rspnse for frames in aggregation queue
2626 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002627static int iwl4965_tx_status_reply_tx(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002628 struct iwl4965_ht_agg *agg,
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02002629 struct iwl4965_tx_resp_agg *tx_resp,
Zhu Yib481de92007-09-25 17:54:57 -07002630 u16 start_idx)
2631{
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02002632 u16 status;
2633 struct agg_tx_status *frame_status = &tx_resp->status;
Zhu Yib481de92007-09-25 17:54:57 -07002634 struct ieee80211_tx_status *tx_status = NULL;
2635 struct ieee80211_hdr *hdr = NULL;
2636 int i, sh;
2637 int txq_id, idx;
2638 u16 seq;
2639
2640 if (agg->wait_for_ba)
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002641 IWL_DEBUG_TX_REPLY("got tx response w/o block-ack\n");
Zhu Yib481de92007-09-25 17:54:57 -07002642
2643 agg->frame_count = tx_resp->frame_count;
2644 agg->start_idx = start_idx;
2645 agg->rate_n_flags = le32_to_cpu(tx_resp->rate_n_flags);
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02002646 agg->bitmap = 0;
Zhu Yib481de92007-09-25 17:54:57 -07002647
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002648 /* # frames attempted by Tx command */
Zhu Yib481de92007-09-25 17:54:57 -07002649 if (agg->frame_count == 1) {
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002650 /* Only one frame was attempted; no block-ack will arrive */
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02002651 status = le16_to_cpu(frame_status[0].status);
2652 seq = le16_to_cpu(frame_status[0].sequence);
2653 idx = SEQ_TO_INDEX(seq);
2654 txq_id = SEQ_TO_QUEUE(seq);
Zhu Yib481de92007-09-25 17:54:57 -07002655
Zhu Yib481de92007-09-25 17:54:57 -07002656 /* FIXME: code repetition */
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02002657 IWL_DEBUG_TX_REPLY("FrameCnt = %d, StartIdx=%d idx=%d\n",
2658 agg->frame_count, agg->start_idx, idx);
Zhu Yib481de92007-09-25 17:54:57 -07002659
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02002660 tx_status = &(priv->txq[txq_id].txb[idx].status);
Zhu Yib481de92007-09-25 17:54:57 -07002661 tx_status->retry_count = tx_resp->failure_frame;
2662 tx_status->queue_number = status & 0xff;
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02002663 tx_status->queue_length = tx_resp->failure_rts;
2664 tx_status->control.flags &= ~IEEE80211_TXCTL_AMPDU;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002665 tx_status->flags = iwl4965_is_tx_success(status)?
Zhu Yib481de92007-09-25 17:54:57 -07002666 IEEE80211_TX_STATUS_ACK : 0;
Ron Rindjunsky4c424e42008-03-04 18:09:27 -08002667 iwl4965_hwrate_to_tx_control(priv,
2668 le32_to_cpu(tx_resp->rate_n_flags),
2669 &tx_status->control);
Zhu Yib481de92007-09-25 17:54:57 -07002670 /* FIXME: code repetition end */
2671
2672 IWL_DEBUG_TX_REPLY("1 Frame 0x%x failure :%d\n",
2673 status & 0xff, tx_resp->failure_frame);
2674 IWL_DEBUG_TX_REPLY("Rate Info rate_n_flags=%x\n",
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002675 iwl4965_hw_get_rate_n_flags(tx_resp->rate_n_flags));
Zhu Yib481de92007-09-25 17:54:57 -07002676
2677 agg->wait_for_ba = 0;
2678 } else {
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002679 /* Two or more frames were attempted; expect block-ack */
Zhu Yib481de92007-09-25 17:54:57 -07002680 u64 bitmap = 0;
2681 int start = agg->start_idx;
2682
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002683 /* Construct bit-map of pending frames within Tx window */
Zhu Yib481de92007-09-25 17:54:57 -07002684 for (i = 0; i < agg->frame_count; i++) {
2685 u16 sc;
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02002686 status = le16_to_cpu(frame_status[i].status);
2687 seq = le16_to_cpu(frame_status[i].sequence);
Zhu Yib481de92007-09-25 17:54:57 -07002688 idx = SEQ_TO_INDEX(seq);
2689 txq_id = SEQ_TO_QUEUE(seq);
2690
2691 if (status & (AGG_TX_STATE_FEW_BYTES_MSK |
2692 AGG_TX_STATE_ABORT_MSK))
2693 continue;
2694
2695 IWL_DEBUG_TX_REPLY("FrameCnt = %d, txq_id=%d idx=%d\n",
2696 agg->frame_count, txq_id, idx);
2697
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002698 hdr = iwl4965_tx_queue_get_hdr(priv, txq_id, idx);
Zhu Yib481de92007-09-25 17:54:57 -07002699
2700 sc = le16_to_cpu(hdr->seq_ctrl);
2701 if (idx != (SEQ_TO_SN(sc) & 0xff)) {
2702 IWL_ERROR("BUG_ON idx doesn't match seq control"
2703 " idx=%d, seq_idx=%d, seq=%d\n",
2704 idx, SEQ_TO_SN(sc),
2705 hdr->seq_ctrl);
2706 return -1;
2707 }
2708
2709 IWL_DEBUG_TX_REPLY("AGG Frame i=%d idx %d seq=%d\n",
2710 i, idx, SEQ_TO_SN(sc));
2711
2712 sh = idx - start;
2713 if (sh > 64) {
2714 sh = (start - idx) + 0xff;
2715 bitmap = bitmap << sh;
2716 sh = 0;
2717 start = idx;
2718 } else if (sh < -64)
2719 sh = 0xff - (start - idx);
2720 else if (sh < 0) {
2721 sh = start - idx;
2722 start = idx;
2723 bitmap = bitmap << sh;
2724 sh = 0;
2725 }
2726 bitmap |= (1 << sh);
2727 IWL_DEBUG_TX_REPLY("start=%d bitmap=0x%x\n",
2728 start, (u32)(bitmap & 0xFFFFFFFF));
2729 }
2730
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02002731 agg->bitmap = bitmap;
Zhu Yib481de92007-09-25 17:54:57 -07002732 agg->start_idx = start;
2733 agg->rate_n_flags = le32_to_cpu(tx_resp->rate_n_flags);
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02002734 IWL_DEBUG_TX_REPLY("Frames %d start_idx=%d bitmap=0x%llx\n",
Zhu Yib481de92007-09-25 17:54:57 -07002735 agg->frame_count, agg->start_idx,
John W. Linville06501d22008-04-01 17:38:47 -04002736 (unsigned long long)agg->bitmap);
Zhu Yib481de92007-09-25 17:54:57 -07002737
2738 if (bitmap)
2739 agg->wait_for_ba = 1;
2740 }
2741 return 0;
2742}
2743#endif
Zhu Yib481de92007-09-25 17:54:57 -07002744
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002745/**
2746 * iwl4965_rx_reply_tx - Handle standard (non-aggregation) Tx response
2747 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002748static void iwl4965_rx_reply_tx(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002749 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07002750{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002751 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07002752 u16 sequence = le16_to_cpu(pkt->hdr.sequence);
2753 int txq_id = SEQ_TO_QUEUE(sequence);
2754 int index = SEQ_TO_INDEX(sequence);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002755 struct iwl4965_tx_queue *txq = &priv->txq[txq_id];
Zhu Yib481de92007-09-25 17:54:57 -07002756 struct ieee80211_tx_status *tx_status;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002757 struct iwl4965_tx_resp *tx_resp = (void *)&pkt->u.raw[0];
Zhu Yib481de92007-09-25 17:54:57 -07002758 u32 status = le32_to_cpu(tx_resp->status);
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08002759#ifdef CONFIG_IWL4965_HT
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02002760 int tid = MAX_TID_COUNT, sta_id = IWL_INVALID_STATION;
2761 struct ieee80211_hdr *hdr;
2762 __le16 *qc;
Zhu Yib481de92007-09-25 17:54:57 -07002763#endif
2764
2765 if ((index >= txq->q.n_bd) || (x2_queue_used(&txq->q, index) == 0)) {
2766 IWL_ERROR("Read index for DMA queue txq_id (%d) index %d "
2767 "is out of range [0-%d] %d %d\n", txq_id,
Tomas Winklerfc4b6852007-10-25 17:15:24 +08002768 index, txq->q.n_bd, txq->q.write_ptr,
2769 txq->q.read_ptr);
Zhu Yib481de92007-09-25 17:54:57 -07002770 return;
2771 }
2772
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08002773#ifdef CONFIG_IWL4965_HT
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02002774 hdr = iwl4965_tx_queue_get_hdr(priv, txq_id, index);
2775 qc = ieee80211_get_qos_ctrl(hdr);
Zhu Yib481de92007-09-25 17:54:57 -07002776
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02002777 if (qc)
Zhu Yib481de92007-09-25 17:54:57 -07002778 tid = le16_to_cpu(*qc) & 0xf;
2779
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02002780 sta_id = iwl4965_get_ra_sta_id(priv, hdr);
2781 if (txq->sched_retry && unlikely(sta_id == IWL_INVALID_STATION)) {
2782 IWL_ERROR("Station not known\n");
2783 return;
2784 }
2785
2786 if (txq->sched_retry) {
2787 const u32 scd_ssn = iwl4965_get_scd_ssn(tx_resp);
2788 struct iwl4965_ht_agg *agg = NULL;
2789
2790 if (!qc)
Zhu Yib481de92007-09-25 17:54:57 -07002791 return;
Zhu Yib481de92007-09-25 17:54:57 -07002792
2793 agg = &priv->stations[sta_id].tid[tid].agg;
2794
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02002795 iwl4965_tx_status_reply_tx(priv, agg,
2796 (struct iwl4965_tx_resp_agg *)tx_resp, index);
Zhu Yib481de92007-09-25 17:54:57 -07002797
2798 if ((tx_resp->frame_count == 1) &&
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002799 !iwl4965_is_tx_success(status)) {
Zhu Yib481de92007-09-25 17:54:57 -07002800 /* TODO: send BAR */
2801 }
2802
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02002803 if (txq->q.read_ptr != (scd_ssn & 0xff)) {
2804 int freed;
Tomas Winklerc54b6792008-03-06 17:36:53 -08002805 index = iwl_queue_dec_wrap(scd_ssn & 0xff, txq->q.n_bd);
Zhu Yib481de92007-09-25 17:54:57 -07002806 IWL_DEBUG_TX_REPLY("Retry scheduler reclaim scd_ssn "
2807 "%d index %d\n", scd_ssn , index);
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02002808 freed = iwl4965_tx_queue_reclaim(priv, txq_id, index);
2809 priv->stations[sta_id].tid[tid].tfds_in_queue -= freed;
2810
2811 if (iwl4965_queue_space(&txq->q) > txq->q.low_mark &&
2812 txq_id >= 0 && priv->mac80211_registered &&
2813 agg->state != IWL_EMPTYING_HW_QUEUE_DELBA)
2814 ieee80211_wake_queue(priv->hw, txq_id);
2815
2816 iwl4965_check_empty_hw_queue(priv, sta_id, tid, txq_id);
Zhu Yib481de92007-09-25 17:54:57 -07002817 }
2818 } else {
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08002819#endif /* CONFIG_IWL4965_HT */
Tomas Winklerfc4b6852007-10-25 17:15:24 +08002820 tx_status = &(txq->txb[txq->q.read_ptr].status);
Zhu Yib481de92007-09-25 17:54:57 -07002821
2822 tx_status->retry_count = tx_resp->failure_frame;
2823 tx_status->queue_number = status;
2824 tx_status->queue_length = tx_resp->bt_kill_count;
2825 tx_status->queue_length |= tx_resp->failure_rts;
Zhu Yib481de92007-09-25 17:54:57 -07002826 tx_status->flags =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002827 iwl4965_is_tx_success(status) ? IEEE80211_TX_STATUS_ACK : 0;
Ron Rindjunsky4c424e42008-03-04 18:09:27 -08002828 iwl4965_hwrate_to_tx_control(priv, le32_to_cpu(tx_resp->rate_n_flags),
2829 &tx_status->control);
Zhu Yib481de92007-09-25 17:54:57 -07002830
Zhu Yib481de92007-09-25 17:54:57 -07002831 IWL_DEBUG_TX("Tx queue %d Status %s (0x%08x) rate_n_flags 0x%x "
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002832 "retries %d\n", txq_id, iwl4965_get_tx_fail_reason(status),
Zhu Yib481de92007-09-25 17:54:57 -07002833 status, le32_to_cpu(tx_resp->rate_n_flags),
2834 tx_resp->failure_frame);
2835
2836 IWL_DEBUG_TX_REPLY("Tx queue reclaim %d\n", index);
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02002837 if (index != -1) {
2838 int freed = iwl4965_tx_queue_reclaim(priv, txq_id, index);
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08002839#ifdef CONFIG_IWL4965_HT
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02002840 if (tid != MAX_TID_COUNT)
2841 priv->stations[sta_id].tid[tid].tfds_in_queue -= freed;
2842 if (iwl4965_queue_space(&txq->q) > txq->q.low_mark &&
2843 (txq_id >= 0) &&
2844 priv->mac80211_registered)
2845 ieee80211_wake_queue(priv->hw, txq_id);
2846 if (tid != MAX_TID_COUNT)
2847 iwl4965_check_empty_hw_queue(priv, sta_id, tid, txq_id);
2848#endif
Zhu Yib481de92007-09-25 17:54:57 -07002849 }
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02002850#ifdef CONFIG_IWL4965_HT
2851 }
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08002852#endif /* CONFIG_IWL4965_HT */
Zhu Yib481de92007-09-25 17:54:57 -07002853
2854 if (iwl_check_bits(status, TX_ABORT_REQUIRED_MSK))
2855 IWL_ERROR("TODO: Implement Tx ABORT REQUIRED!!!\n");
2856}
2857
2858
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002859static void iwl4965_rx_reply_alive(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002860 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07002861{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002862 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
2863 struct iwl4965_alive_resp *palive;
Zhu Yib481de92007-09-25 17:54:57 -07002864 struct delayed_work *pwork;
2865
2866 palive = &pkt->u.alive_frame;
2867
2868 IWL_DEBUG_INFO("Alive ucode status 0x%08X revision "
2869 "0x%01X 0x%01X\n",
2870 palive->is_valid, palive->ver_type,
2871 palive->ver_subtype);
2872
2873 if (palive->ver_subtype == INITIALIZE_SUBTYPE) {
2874 IWL_DEBUG_INFO("Initialization Alive received.\n");
2875 memcpy(&priv->card_alive_init,
2876 &pkt->u.alive_frame,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002877 sizeof(struct iwl4965_init_alive_resp));
Zhu Yib481de92007-09-25 17:54:57 -07002878 pwork = &priv->init_alive_start;
2879 } else {
2880 IWL_DEBUG_INFO("Runtime Alive received.\n");
2881 memcpy(&priv->card_alive, &pkt->u.alive_frame,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002882 sizeof(struct iwl4965_alive_resp));
Zhu Yib481de92007-09-25 17:54:57 -07002883 pwork = &priv->alive_start;
2884 }
2885
2886 /* We delay the ALIVE response by 5ms to
2887 * give the HW RF Kill time to activate... */
2888 if (palive->is_valid == UCODE_VALID_OK)
2889 queue_delayed_work(priv->workqueue, pwork,
2890 msecs_to_jiffies(5));
2891 else
2892 IWL_WARNING("uCode did not respond OK.\n");
2893}
2894
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002895static void iwl4965_rx_reply_add_sta(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002896 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07002897{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002898 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07002899
2900 IWL_DEBUG_RX("Received REPLY_ADD_STA: 0x%02X\n", pkt->u.status);
2901 return;
2902}
2903
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002904static void iwl4965_rx_reply_error(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002905 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07002906{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002907 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07002908
2909 IWL_ERROR("Error Reply type 0x%08X cmd %s (0x%02X) "
2910 "seq 0x%04X ser 0x%08X\n",
2911 le32_to_cpu(pkt->u.err_resp.error_type),
2912 get_cmd_string(pkt->u.err_resp.cmd_id),
2913 pkt->u.err_resp.cmd_id,
2914 le16_to_cpu(pkt->u.err_resp.bad_cmd_seq_num),
2915 le32_to_cpu(pkt->u.err_resp.error_info));
2916}
2917
2918#define TX_STATUS_ENTRY(x) case TX_STATUS_FAIL_ ## x: return #x
2919
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002920static void iwl4965_rx_csa(struct iwl_priv *priv, struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07002921{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002922 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
2923 struct iwl4965_rxon_cmd *rxon = (void *)&priv->active_rxon;
2924 struct iwl4965_csa_notification *csa = &(pkt->u.csa_notif);
Zhu Yib481de92007-09-25 17:54:57 -07002925 IWL_DEBUG_11H("CSA notif: channel %d, status %d\n",
2926 le16_to_cpu(csa->channel), le32_to_cpu(csa->status));
2927 rxon->channel = csa->channel;
2928 priv->staging_rxon.channel = csa->channel;
2929}
2930
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002931static void iwl4965_rx_spectrum_measure_notif(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002932 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07002933{
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08002934#ifdef CONFIG_IWL4965_SPECTRUM_MEASUREMENT
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002935 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
2936 struct iwl4965_spectrum_notification *report = &(pkt->u.spectrum_notif);
Zhu Yib481de92007-09-25 17:54:57 -07002937
2938 if (!report->state) {
2939 IWL_DEBUG(IWL_DL_11H | IWL_DL_INFO,
2940 "Spectrum Measure Notification: Start\n");
2941 return;
2942 }
2943
2944 memcpy(&priv->measure_report, report, sizeof(*report));
2945 priv->measurement_status |= MEASUREMENT_READY;
2946#endif
2947}
2948
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002949static void iwl4965_rx_pm_sleep_notif(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002950 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07002951{
Tomas Winkler0a6857e2008-03-12 16:58:49 -07002952#ifdef CONFIG_IWLWIFI_DEBUG
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002953 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
2954 struct iwl4965_sleep_notification *sleep = &(pkt->u.sleep_notif);
Zhu Yib481de92007-09-25 17:54:57 -07002955 IWL_DEBUG_RX("sleep mode: %d, src: %d\n",
2956 sleep->pm_sleep_mode, sleep->pm_wakeup_src);
2957#endif
2958}
2959
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002960static void iwl4965_rx_pm_debug_statistics_notif(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002961 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07002962{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002963 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07002964 IWL_DEBUG_RADIO("Dumping %d bytes of unhandled "
2965 "notification for %s:\n",
2966 le32_to_cpu(pkt->len), get_cmd_string(pkt->hdr.cmd));
Tomas Winkler0a6857e2008-03-12 16:58:49 -07002967 iwl_print_hex_dump(IWL_DL_RADIO, pkt->u.raw, le32_to_cpu(pkt->len));
Zhu Yib481de92007-09-25 17:54:57 -07002968}
2969
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002970static void iwl4965_bg_beacon_update(struct work_struct *work)
Zhu Yib481de92007-09-25 17:54:57 -07002971{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002972 struct iwl_priv *priv =
2973 container_of(work, struct iwl_priv, beacon_update);
Zhu Yib481de92007-09-25 17:54:57 -07002974 struct sk_buff *beacon;
2975
2976 /* Pull updated AP beacon from mac80211. will fail if not in AP mode */
Johannes Berg32bfd352007-12-19 01:31:26 +01002977 beacon = ieee80211_beacon_get(priv->hw, priv->vif, NULL);
Zhu Yib481de92007-09-25 17:54:57 -07002978
2979 if (!beacon) {
2980 IWL_ERROR("update beacon failed\n");
2981 return;
2982 }
2983
2984 mutex_lock(&priv->mutex);
2985 /* new beacon skb is allocated every time; dispose previous.*/
2986 if (priv->ibss_beacon)
2987 dev_kfree_skb(priv->ibss_beacon);
2988
2989 priv->ibss_beacon = beacon;
2990 mutex_unlock(&priv->mutex);
2991
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002992 iwl4965_send_beacon_cmd(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002993}
2994
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002995static void iwl4965_rx_beacon_notif(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002996 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07002997{
Tomas Winkler0a6857e2008-03-12 16:58:49 -07002998#ifdef CONFIG_IWLWIFI_DEBUG
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002999 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
3000 struct iwl4965_beacon_notif *beacon = &(pkt->u.beacon_status);
3001 u8 rate = iwl4965_hw_get_rate(beacon->beacon_notify_hdr.rate_n_flags);
Zhu Yib481de92007-09-25 17:54:57 -07003002
3003 IWL_DEBUG_RX("beacon status %x retries %d iss %d "
3004 "tsf %d %d rate %d\n",
3005 le32_to_cpu(beacon->beacon_notify_hdr.status) & TX_STATUS_MSK,
3006 beacon->beacon_notify_hdr.failure_frame,
3007 le32_to_cpu(beacon->ibss_mgr_status),
3008 le32_to_cpu(beacon->high_tsf),
3009 le32_to_cpu(beacon->low_tsf), rate);
3010#endif
3011
3012 if ((priv->iw_mode == IEEE80211_IF_TYPE_AP) &&
3013 (!test_bit(STATUS_EXIT_PENDING, &priv->status)))
3014 queue_work(priv->workqueue, &priv->beacon_update);
3015}
3016
3017/* Service response to REPLY_SCAN_CMD (0x80) */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003018static void iwl4965_rx_reply_scan(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003019 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003020{
Tomas Winkler0a6857e2008-03-12 16:58:49 -07003021#ifdef CONFIG_IWLWIFI_DEBUG
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003022 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
3023 struct iwl4965_scanreq_notification *notif =
3024 (struct iwl4965_scanreq_notification *)pkt->u.raw;
Zhu Yib481de92007-09-25 17:54:57 -07003025
3026 IWL_DEBUG_RX("Scan request status = 0x%x\n", notif->status);
3027#endif
3028}
3029
3030/* Service SCAN_START_NOTIFICATION (0x82) */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003031static void iwl4965_rx_scan_start_notif(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003032 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003033{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003034 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
3035 struct iwl4965_scanstart_notification *notif =
3036 (struct iwl4965_scanstart_notification *)pkt->u.raw;
Zhu Yib481de92007-09-25 17:54:57 -07003037 priv->scan_start_tsf = le32_to_cpu(notif->tsf_low);
3038 IWL_DEBUG_SCAN("Scan start: "
3039 "%d [802.11%s] "
3040 "(TSF: 0x%08X:%08X) - %d (beacon timer %u)\n",
3041 notif->channel,
3042 notif->band ? "bg" : "a",
3043 notif->tsf_high,
3044 notif->tsf_low, notif->status, notif->beacon_timer);
3045}
3046
3047/* Service SCAN_RESULTS_NOTIFICATION (0x83) */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003048static void iwl4965_rx_scan_results_notif(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003049 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003050{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003051 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
3052 struct iwl4965_scanresults_notification *notif =
3053 (struct iwl4965_scanresults_notification *)pkt->u.raw;
Zhu Yib481de92007-09-25 17:54:57 -07003054
3055 IWL_DEBUG_SCAN("Scan ch.res: "
3056 "%d [802.11%s] "
3057 "(TSF: 0x%08X:%08X) - %d "
3058 "elapsed=%lu usec (%dms since last)\n",
3059 notif->channel,
3060 notif->band ? "bg" : "a",
3061 le32_to_cpu(notif->tsf_high),
3062 le32_to_cpu(notif->tsf_low),
3063 le32_to_cpu(notif->statistics[0]),
3064 le32_to_cpu(notif->tsf_low) - priv->scan_start_tsf,
3065 jiffies_to_msecs(elapsed_jiffies
3066 (priv->last_scan_jiffies, jiffies)));
3067
3068 priv->last_scan_jiffies = jiffies;
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08003069 priv->next_scan_jiffies = 0;
Zhu Yib481de92007-09-25 17:54:57 -07003070}
3071
3072/* Service SCAN_COMPLETE_NOTIFICATION (0x84) */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003073static void iwl4965_rx_scan_complete_notif(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003074 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003075{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003076 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
3077 struct iwl4965_scancomplete_notification *scan_notif = (void *)pkt->u.raw;
Zhu Yib481de92007-09-25 17:54:57 -07003078
3079 IWL_DEBUG_SCAN("Scan complete: %d channels (TSF 0x%08X:%08X) - %d\n",
3080 scan_notif->scanned_channels,
3081 scan_notif->tsf_low,
3082 scan_notif->tsf_high, scan_notif->status);
3083
3084 /* The HW is no longer scanning */
3085 clear_bit(STATUS_SCAN_HW, &priv->status);
3086
3087 /* The scan completion notification came in, so kill that timer... */
3088 cancel_delayed_work(&priv->scan_check);
3089
3090 IWL_DEBUG_INFO("Scan pass on %sGHz took %dms\n",
3091 (priv->scan_bands == 2) ? "2.4" : "5.2",
3092 jiffies_to_msecs(elapsed_jiffies
3093 (priv->scan_pass_start, jiffies)));
3094
3095 /* Remove this scanned band from the list
3096 * of pending bands to scan */
3097 priv->scan_bands--;
3098
3099 /* If a request to abort was given, or the scan did not succeed
3100 * then we reset the scan state machine and terminate,
3101 * re-queuing another scan if one has been requested */
3102 if (test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
3103 IWL_DEBUG_INFO("Aborted scan completed.\n");
3104 clear_bit(STATUS_SCAN_ABORTING, &priv->status);
3105 } else {
3106 /* If there are more bands on this scan pass reschedule */
3107 if (priv->scan_bands > 0)
3108 goto reschedule;
3109 }
3110
3111 priv->last_scan_jiffies = jiffies;
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08003112 priv->next_scan_jiffies = 0;
Zhu Yib481de92007-09-25 17:54:57 -07003113 IWL_DEBUG_INFO("Setting scan to off\n");
3114
3115 clear_bit(STATUS_SCANNING, &priv->status);
3116
3117 IWL_DEBUG_INFO("Scan took %dms\n",
3118 jiffies_to_msecs(elapsed_jiffies(priv->scan_start, jiffies)));
3119
3120 queue_work(priv->workqueue, &priv->scan_completed);
3121
3122 return;
3123
3124reschedule:
3125 priv->scan_pass_start = jiffies;
3126 queue_work(priv->workqueue, &priv->request_scan);
3127}
3128
3129/* Handle notification from uCode that card's power state is changing
3130 * due to software, hardware, or critical temperature RFKILL */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003131static void iwl4965_rx_card_state_notif(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003132 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003133{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003134 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07003135 u32 flags = le32_to_cpu(pkt->u.card_state_notif.flags);
3136 unsigned long status = priv->status;
3137
3138 IWL_DEBUG_RF_KILL("Card state received: HW:%s SW:%s\n",
3139 (flags & HW_CARD_DISABLED) ? "Kill" : "On",
3140 (flags & SW_CARD_DISABLED) ? "Kill" : "On");
3141
3142 if (flags & (SW_CARD_DISABLED | HW_CARD_DISABLED |
3143 RF_CARD_DISABLED)) {
3144
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003145 iwl_write32(priv, CSR_UCODE_DRV_GP1_SET,
Zhu Yib481de92007-09-25 17:54:57 -07003146 CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
3147
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003148 if (!iwl_grab_nic_access(priv)) {
3149 iwl_write_direct32(
Zhu Yib481de92007-09-25 17:54:57 -07003150 priv, HBUS_TARG_MBX_C,
3151 HBUS_TARG_MBX_C_REG_BIT_CMD_BLOCKED);
3152
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003153 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003154 }
3155
3156 if (!(flags & RXON_CARD_DISABLED)) {
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003157 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR,
Zhu Yib481de92007-09-25 17:54:57 -07003158 CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003159 if (!iwl_grab_nic_access(priv)) {
3160 iwl_write_direct32(
Zhu Yib481de92007-09-25 17:54:57 -07003161 priv, HBUS_TARG_MBX_C,
3162 HBUS_TARG_MBX_C_REG_BIT_CMD_BLOCKED);
3163
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003164 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003165 }
3166 }
3167
3168 if (flags & RF_CARD_DISABLED) {
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003169 iwl_write32(priv, CSR_UCODE_DRV_GP1_SET,
Zhu Yib481de92007-09-25 17:54:57 -07003170 CSR_UCODE_DRV_GP1_REG_BIT_CT_KILL_EXIT);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003171 iwl_read32(priv, CSR_UCODE_DRV_GP1);
3172 if (!iwl_grab_nic_access(priv))
3173 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003174 }
3175 }
3176
3177 if (flags & HW_CARD_DISABLED)
3178 set_bit(STATUS_RF_KILL_HW, &priv->status);
3179 else
3180 clear_bit(STATUS_RF_KILL_HW, &priv->status);
3181
3182
3183 if (flags & SW_CARD_DISABLED)
3184 set_bit(STATUS_RF_KILL_SW, &priv->status);
3185 else
3186 clear_bit(STATUS_RF_KILL_SW, &priv->status);
3187
3188 if (!(flags & RXON_CARD_DISABLED))
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003189 iwl4965_scan_cancel(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003190
3191 if ((test_bit(STATUS_RF_KILL_HW, &status) !=
3192 test_bit(STATUS_RF_KILL_HW, &priv->status)) ||
3193 (test_bit(STATUS_RF_KILL_SW, &status) !=
3194 test_bit(STATUS_RF_KILL_SW, &priv->status)))
3195 queue_work(priv->workqueue, &priv->rf_kill);
3196 else
3197 wake_up_interruptible(&priv->wait_command_queue);
3198}
3199
3200/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003201 * iwl4965_setup_rx_handlers - Initialize Rx handler callbacks
Zhu Yib481de92007-09-25 17:54:57 -07003202 *
3203 * Setup the RX handlers for each of the reply types sent from the uCode
3204 * to the host.
3205 *
3206 * This function chains into the hardware specific files for them to setup
3207 * any hardware specific handlers as well.
3208 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003209static void iwl4965_setup_rx_handlers(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003210{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003211 priv->rx_handlers[REPLY_ALIVE] = iwl4965_rx_reply_alive;
3212 priv->rx_handlers[REPLY_ADD_STA] = iwl4965_rx_reply_add_sta;
3213 priv->rx_handlers[REPLY_ERROR] = iwl4965_rx_reply_error;
3214 priv->rx_handlers[CHANNEL_SWITCH_NOTIFICATION] = iwl4965_rx_csa;
Zhu Yib481de92007-09-25 17:54:57 -07003215 priv->rx_handlers[SPECTRUM_MEASURE_NOTIFICATION] =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003216 iwl4965_rx_spectrum_measure_notif;
3217 priv->rx_handlers[PM_SLEEP_NOTIFICATION] = iwl4965_rx_pm_sleep_notif;
Zhu Yib481de92007-09-25 17:54:57 -07003218 priv->rx_handlers[PM_DEBUG_STATISTIC_NOTIFIC] =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003219 iwl4965_rx_pm_debug_statistics_notif;
3220 priv->rx_handlers[BEACON_NOTIFICATION] = iwl4965_rx_beacon_notif;
Zhu Yib481de92007-09-25 17:54:57 -07003221
Ben Cahill9fbab512007-11-29 11:09:47 +08003222 /*
3223 * The same handler is used for both the REPLY to a discrete
3224 * statistics request from the host as well as for the periodic
3225 * statistics notifications (after received beacons) from the uCode.
Zhu Yib481de92007-09-25 17:54:57 -07003226 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003227 priv->rx_handlers[REPLY_STATISTICS_CMD] = iwl4965_hw_rx_statistics;
3228 priv->rx_handlers[STATISTICS_NOTIFICATION] = iwl4965_hw_rx_statistics;
Zhu Yib481de92007-09-25 17:54:57 -07003229
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003230 priv->rx_handlers[REPLY_SCAN_CMD] = iwl4965_rx_reply_scan;
3231 priv->rx_handlers[SCAN_START_NOTIFICATION] = iwl4965_rx_scan_start_notif;
Zhu Yib481de92007-09-25 17:54:57 -07003232 priv->rx_handlers[SCAN_RESULTS_NOTIFICATION] =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003233 iwl4965_rx_scan_results_notif;
Zhu Yib481de92007-09-25 17:54:57 -07003234 priv->rx_handlers[SCAN_COMPLETE_NOTIFICATION] =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003235 iwl4965_rx_scan_complete_notif;
3236 priv->rx_handlers[CARD_STATE_NOTIFICATION] = iwl4965_rx_card_state_notif;
3237 priv->rx_handlers[REPLY_TX] = iwl4965_rx_reply_tx;
Zhu Yib481de92007-09-25 17:54:57 -07003238
Ben Cahill9fbab512007-11-29 11:09:47 +08003239 /* Set up hardware specific Rx handlers */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003240 iwl4965_hw_rx_handler_setup(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003241}
3242
3243/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003244 * iwl4965_tx_cmd_complete - Pull unused buffers off the queue and reclaim them
Zhu Yib481de92007-09-25 17:54:57 -07003245 * @rxb: Rx buffer to reclaim
3246 *
3247 * If an Rx buffer has an async callback associated with it the callback
3248 * will be executed. The attached skb (if present) will only be freed
3249 * if the callback returns 1
3250 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003251static void iwl4965_tx_cmd_complete(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003252 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003253{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003254 struct iwl4965_rx_packet *pkt = (struct iwl4965_rx_packet *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07003255 u16 sequence = le16_to_cpu(pkt->hdr.sequence);
3256 int txq_id = SEQ_TO_QUEUE(sequence);
3257 int index = SEQ_TO_INDEX(sequence);
3258 int huge = sequence & SEQ_HUGE_FRAME;
3259 int cmd_index;
Tomas Winkler857485c2008-03-21 13:53:44 -07003260 struct iwl_cmd *cmd;
Zhu Yib481de92007-09-25 17:54:57 -07003261
3262 /* If a Tx command is being handled and it isn't in the actual
3263 * command queue then there a command routing bug has been introduced
3264 * in the queue management code. */
3265 if (txq_id != IWL_CMD_QUEUE_NUM)
3266 IWL_ERROR("Error wrong command queue %d command id 0x%X\n",
3267 txq_id, pkt->hdr.cmd);
3268 BUG_ON(txq_id != IWL_CMD_QUEUE_NUM);
3269
3270 cmd_index = get_cmd_index(&priv->txq[IWL_CMD_QUEUE_NUM].q, index, huge);
3271 cmd = &priv->txq[IWL_CMD_QUEUE_NUM].cmd[cmd_index];
3272
3273 /* Input error checking is done when commands are added to queue. */
3274 if (cmd->meta.flags & CMD_WANT_SKB) {
3275 cmd->meta.source->u.skb = rxb->skb;
3276 rxb->skb = NULL;
3277 } else if (cmd->meta.u.callback &&
3278 !cmd->meta.u.callback(priv, cmd, rxb->skb))
3279 rxb->skb = NULL;
3280
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003281 iwl4965_tx_queue_reclaim(priv, txq_id, index);
Zhu Yib481de92007-09-25 17:54:57 -07003282
3283 if (!(cmd->meta.flags & CMD_ASYNC)) {
3284 clear_bit(STATUS_HCMD_ACTIVE, &priv->status);
3285 wake_up_interruptible(&priv->wait_command_queue);
3286 }
3287}
3288
3289/************************** RX-FUNCTIONS ****************************/
3290/*
3291 * Rx theory of operation
3292 *
Ben Cahill9fbab512007-11-29 11:09:47 +08003293 * Driver allocates a circular buffer of Receive Buffer Descriptors (RBDs),
3294 * each of which point to Receive Buffers to be filled by 4965. These get
3295 * used not only for Rx frames, but for any command response or notification
3296 * from the 4965. The driver and 4965 manage the Rx buffers by means
3297 * of indexes into the circular buffer.
Zhu Yib481de92007-09-25 17:54:57 -07003298 *
3299 * Rx Queue Indexes
3300 * The host/firmware share two index registers for managing the Rx buffers.
3301 *
3302 * The READ index maps to the first position that the firmware may be writing
3303 * to -- the driver can read up to (but not including) this position and get
3304 * good data.
3305 * The READ index is managed by the firmware once the card is enabled.
3306 *
3307 * The WRITE index maps to the last position the driver has read from -- the
3308 * position preceding WRITE is the last slot the firmware can place a packet.
3309 *
3310 * The queue is empty (no good data) if WRITE = READ - 1, and is full if
3311 * WRITE = READ.
3312 *
Ben Cahill9fbab512007-11-29 11:09:47 +08003313 * During initialization, the host sets up the READ queue position to the first
Zhu Yib481de92007-09-25 17:54:57 -07003314 * INDEX position, and WRITE to the last (READ - 1 wrapped)
3315 *
Ben Cahill9fbab512007-11-29 11:09:47 +08003316 * When the firmware places a packet in a buffer, it will advance the READ index
Zhu Yib481de92007-09-25 17:54:57 -07003317 * and fire the RX interrupt. The driver can then query the READ index and
3318 * process as many packets as possible, moving the WRITE index forward as it
3319 * resets the Rx queue buffers with new memory.
3320 *
3321 * The management in the driver is as follows:
3322 * + A list of pre-allocated SKBs is stored in iwl->rxq->rx_free. When
3323 * iwl->rxq->free_count drops to or below RX_LOW_WATERMARK, work is scheduled
Ian Schram01ebd062007-10-25 17:15:22 +08003324 * to replenish the iwl->rxq->rx_free.
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003325 * + In iwl4965_rx_replenish (scheduled) if 'processed' != 'read' then the
Zhu Yib481de92007-09-25 17:54:57 -07003326 * iwl->rxq is replenished and the READ INDEX is updated (updating the
3327 * 'processed' and 'read' driver indexes as well)
3328 * + A received packet is processed and handed to the kernel network stack,
3329 * detached from the iwl->rxq. The driver 'processed' index is updated.
3330 * + The Host/Firmware iwl->rxq is replenished at tasklet time from the rx_free
3331 * list. If there are no allocated buffers in iwl->rxq->rx_free, the READ
3332 * INDEX is not incremented and iwl->status(RX_STALLED) is set. If there
3333 * were enough free buffers and RX_STALLED is set it is cleared.
3334 *
3335 *
3336 * Driver sequence:
3337 *
Ben Cahill9fbab512007-11-29 11:09:47 +08003338 * iwl4965_rx_queue_alloc() Allocates rx_free
3339 * iwl4965_rx_replenish() Replenishes rx_free list from rx_used, and calls
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003340 * iwl4965_rx_queue_restock
Ben Cahill9fbab512007-11-29 11:09:47 +08003341 * iwl4965_rx_queue_restock() Moves available buffers from rx_free into Rx
Zhu Yib481de92007-09-25 17:54:57 -07003342 * queue, updates firmware pointers, and updates
3343 * the WRITE index. If insufficient rx_free buffers
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003344 * are available, schedules iwl4965_rx_replenish
Zhu Yib481de92007-09-25 17:54:57 -07003345 *
3346 * -- enable interrupts --
Ben Cahill9fbab512007-11-29 11:09:47 +08003347 * ISR - iwl4965_rx() Detach iwl4965_rx_mem_buffers from pool up to the
Zhu Yib481de92007-09-25 17:54:57 -07003348 * READ INDEX, detaching the SKB from the pool.
3349 * Moves the packet buffer from queue to rx_used.
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003350 * Calls iwl4965_rx_queue_restock to refill any empty
Zhu Yib481de92007-09-25 17:54:57 -07003351 * slots.
3352 * ...
3353 *
3354 */
3355
3356/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003357 * iwl4965_rx_queue_space - Return number of free slots available in queue.
Zhu Yib481de92007-09-25 17:54:57 -07003358 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003359static int iwl4965_rx_queue_space(const struct iwl4965_rx_queue *q)
Zhu Yib481de92007-09-25 17:54:57 -07003360{
3361 int s = q->read - q->write;
3362 if (s <= 0)
3363 s += RX_QUEUE_SIZE;
3364 /* keep some buffer to not confuse full and empty queue */
3365 s -= 2;
3366 if (s < 0)
3367 s = 0;
3368 return s;
3369}
3370
3371/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003372 * iwl4965_rx_queue_update_write_ptr - Update the write pointer for the RX queue
Zhu Yib481de92007-09-25 17:54:57 -07003373 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003374int iwl4965_rx_queue_update_write_ptr(struct iwl_priv *priv, struct iwl4965_rx_queue *q)
Zhu Yib481de92007-09-25 17:54:57 -07003375{
3376 u32 reg = 0;
3377 int rc = 0;
3378 unsigned long flags;
3379
3380 spin_lock_irqsave(&q->lock, flags);
3381
3382 if (q->need_update == 0)
3383 goto exit_unlock;
3384
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003385 /* If power-saving is in use, make sure device is awake */
Zhu Yib481de92007-09-25 17:54:57 -07003386 if (test_bit(STATUS_POWER_PMI, &priv->status)) {
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003387 reg = iwl_read32(priv, CSR_UCODE_DRV_GP1);
Zhu Yib481de92007-09-25 17:54:57 -07003388
3389 if (reg & CSR_UCODE_DRV_GP1_BIT_MAC_SLEEP) {
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003390 iwl_set_bit(priv, CSR_GP_CNTRL,
Zhu Yib481de92007-09-25 17:54:57 -07003391 CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
3392 goto exit_unlock;
3393 }
3394
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003395 rc = iwl_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003396 if (rc)
3397 goto exit_unlock;
3398
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003399 /* Device expects a multiple of 8 */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003400 iwl_write_direct32(priv, FH_RSCSR_CHNL0_WPTR,
Zhu Yib481de92007-09-25 17:54:57 -07003401 q->write & ~0x7);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003402 iwl_release_nic_access(priv);
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003403
3404 /* Else device is assumed to be awake */
Zhu Yib481de92007-09-25 17:54:57 -07003405 } else
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003406 /* Device expects a multiple of 8 */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003407 iwl_write32(priv, FH_RSCSR_CHNL0_WPTR, q->write & ~0x7);
Zhu Yib481de92007-09-25 17:54:57 -07003408
3409
3410 q->need_update = 0;
3411
3412 exit_unlock:
3413 spin_unlock_irqrestore(&q->lock, flags);
3414 return rc;
3415}
3416
3417/**
Ben Cahill9fbab512007-11-29 11:09:47 +08003418 * iwl4965_dma_addr2rbd_ptr - convert a DMA address to a uCode read buffer ptr
Zhu Yib481de92007-09-25 17:54:57 -07003419 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003420static inline __le32 iwl4965_dma_addr2rbd_ptr(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07003421 dma_addr_t dma_addr)
3422{
3423 return cpu_to_le32((u32)(dma_addr >> 8));
3424}
3425
3426
3427/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003428 * iwl4965_rx_queue_restock - refill RX queue from pre-allocated pool
Zhu Yib481de92007-09-25 17:54:57 -07003429 *
Ben Cahill9fbab512007-11-29 11:09:47 +08003430 * If there are slots in the RX queue that need to be restocked,
Zhu Yib481de92007-09-25 17:54:57 -07003431 * and we have free pre-allocated buffers, fill the ranks as much
Ben Cahill9fbab512007-11-29 11:09:47 +08003432 * as we can, pulling from rx_free.
Zhu Yib481de92007-09-25 17:54:57 -07003433 *
3434 * This moves the 'write' index forward to catch up with 'processed', and
3435 * also updates the memory address in the firmware to reference the new
3436 * target buffer.
3437 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003438static int iwl4965_rx_queue_restock(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003439{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003440 struct iwl4965_rx_queue *rxq = &priv->rxq;
Zhu Yib481de92007-09-25 17:54:57 -07003441 struct list_head *element;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003442 struct iwl4965_rx_mem_buffer *rxb;
Zhu Yib481de92007-09-25 17:54:57 -07003443 unsigned long flags;
3444 int write, rc;
3445
3446 spin_lock_irqsave(&rxq->lock, flags);
3447 write = rxq->write & ~0x7;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003448 while ((iwl4965_rx_queue_space(rxq) > 0) && (rxq->free_count)) {
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003449 /* Get next free Rx buffer, remove from free list */
Zhu Yib481de92007-09-25 17:54:57 -07003450 element = rxq->rx_free.next;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003451 rxb = list_entry(element, struct iwl4965_rx_mem_buffer, list);
Zhu Yib481de92007-09-25 17:54:57 -07003452 list_del(element);
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003453
3454 /* Point to Rx buffer via next RBD in circular buffer */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003455 rxq->bd[rxq->write] = iwl4965_dma_addr2rbd_ptr(priv, rxb->dma_addr);
Zhu Yib481de92007-09-25 17:54:57 -07003456 rxq->queue[rxq->write] = rxb;
3457 rxq->write = (rxq->write + 1) & RX_QUEUE_MASK;
3458 rxq->free_count--;
3459 }
3460 spin_unlock_irqrestore(&rxq->lock, flags);
3461 /* If the pre-allocated buffer pool is dropping low, schedule to
3462 * refill it */
3463 if (rxq->free_count <= RX_LOW_WATERMARK)
3464 queue_work(priv->workqueue, &priv->rx_replenish);
3465
3466
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003467 /* If we've added more space for the firmware to place data, tell it.
3468 * Increment device's write pointer in multiples of 8. */
Zhu Yib481de92007-09-25 17:54:57 -07003469 if ((write != (rxq->write & ~0x7))
3470 || (abs(rxq->write - rxq->read) > 7)) {
3471 spin_lock_irqsave(&rxq->lock, flags);
3472 rxq->need_update = 1;
3473 spin_unlock_irqrestore(&rxq->lock, flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003474 rc = iwl4965_rx_queue_update_write_ptr(priv, rxq);
Zhu Yib481de92007-09-25 17:54:57 -07003475 if (rc)
3476 return rc;
3477 }
3478
3479 return 0;
3480}
3481
3482/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003483 * iwl4965_rx_replenish - Move all used packet from rx_used to rx_free
Zhu Yib481de92007-09-25 17:54:57 -07003484 *
3485 * When moving to rx_free an SKB is allocated for the slot.
3486 *
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003487 * Also restock the Rx queue via iwl4965_rx_queue_restock.
Ian Schram01ebd062007-10-25 17:15:22 +08003488 * This is called as a scheduled work item (except for during initialization)
Zhu Yib481de92007-09-25 17:54:57 -07003489 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003490static void iwl4965_rx_allocate(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003491{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003492 struct iwl4965_rx_queue *rxq = &priv->rxq;
Zhu Yib481de92007-09-25 17:54:57 -07003493 struct list_head *element;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003494 struct iwl4965_rx_mem_buffer *rxb;
Zhu Yib481de92007-09-25 17:54:57 -07003495 unsigned long flags;
3496 spin_lock_irqsave(&rxq->lock, flags);
3497 while (!list_empty(&rxq->rx_used)) {
3498 element = rxq->rx_used.next;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003499 rxb = list_entry(element, struct iwl4965_rx_mem_buffer, list);
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003500
3501 /* Alloc a new receive buffer */
Zhu Yib481de92007-09-25 17:54:57 -07003502 rxb->skb =
Tomas Winkler5425e492008-04-15 16:01:38 -07003503 alloc_skb(priv->hw_params.rx_buf_size,
Ron Rindjunsky9ee1ba42007-11-26 16:14:42 +02003504 __GFP_NOWARN | GFP_ATOMIC);
Zhu Yib481de92007-09-25 17:54:57 -07003505 if (!rxb->skb) {
3506 if (net_ratelimit())
3507 printk(KERN_CRIT DRV_NAME
3508 ": Can not allocate SKB buffers\n");
3509 /* We don't reschedule replenish work here -- we will
3510 * call the restock method and if it still needs
3511 * more buffers it will schedule replenish */
3512 break;
3513 }
3514 priv->alloc_rxb_skb++;
3515 list_del(element);
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003516
3517 /* Get physical address of RB/SKB */
Zhu Yib481de92007-09-25 17:54:57 -07003518 rxb->dma_addr =
3519 pci_map_single(priv->pci_dev, rxb->skb->data,
Tomas Winkler5425e492008-04-15 16:01:38 -07003520 priv->hw_params.rx_buf_size, PCI_DMA_FROMDEVICE);
Zhu Yib481de92007-09-25 17:54:57 -07003521 list_add_tail(&rxb->list, &rxq->rx_free);
3522 rxq->free_count++;
3523 }
3524 spin_unlock_irqrestore(&rxq->lock, flags);
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08003525}
3526
3527/*
3528 * this should be called while priv->lock is locked
3529*/
Tomas Winkler4fd1f842007-12-05 20:59:58 +02003530static void __iwl4965_rx_replenish(void *data)
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08003531{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003532 struct iwl_priv *priv = data;
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08003533
3534 iwl4965_rx_allocate(priv);
3535 iwl4965_rx_queue_restock(priv);
3536}
3537
3538
3539void iwl4965_rx_replenish(void *data)
3540{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003541 struct iwl_priv *priv = data;
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08003542 unsigned long flags;
3543
3544 iwl4965_rx_allocate(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003545
3546 spin_lock_irqsave(&priv->lock, flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003547 iwl4965_rx_queue_restock(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003548 spin_unlock_irqrestore(&priv->lock, flags);
3549}
3550
3551/* Assumes that the skb field of the buffers in 'pool' is kept accurate.
Ben Cahill9fbab512007-11-29 11:09:47 +08003552 * If an SKB has been detached, the POOL needs to have its SKB set to NULL
Zhu Yib481de92007-09-25 17:54:57 -07003553 * This free routine walks the list of POOL entries and if SKB is set to
3554 * non NULL it is unmapped and freed
3555 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003556static void iwl4965_rx_queue_free(struct iwl_priv *priv, struct iwl4965_rx_queue *rxq)
Zhu Yib481de92007-09-25 17:54:57 -07003557{
3558 int i;
3559 for (i = 0; i < RX_QUEUE_SIZE + RX_FREE_BUFFERS; i++) {
3560 if (rxq->pool[i].skb != NULL) {
3561 pci_unmap_single(priv->pci_dev,
3562 rxq->pool[i].dma_addr,
Tomas Winkler5425e492008-04-15 16:01:38 -07003563 priv->hw_params.rx_buf_size,
Ron Rindjunsky9ee1ba42007-11-26 16:14:42 +02003564 PCI_DMA_FROMDEVICE);
Zhu Yib481de92007-09-25 17:54:57 -07003565 dev_kfree_skb(rxq->pool[i].skb);
3566 }
3567 }
3568
3569 pci_free_consistent(priv->pci_dev, 4 * RX_QUEUE_SIZE, rxq->bd,
3570 rxq->dma_addr);
3571 rxq->bd = NULL;
3572}
3573
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003574int iwl4965_rx_queue_alloc(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003575{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003576 struct iwl4965_rx_queue *rxq = &priv->rxq;
Zhu Yib481de92007-09-25 17:54:57 -07003577 struct pci_dev *dev = priv->pci_dev;
3578 int i;
3579
3580 spin_lock_init(&rxq->lock);
3581 INIT_LIST_HEAD(&rxq->rx_free);
3582 INIT_LIST_HEAD(&rxq->rx_used);
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003583
3584 /* Alloc the circular buffer of Read Buffer Descriptors (RBDs) */
Zhu Yib481de92007-09-25 17:54:57 -07003585 rxq->bd = pci_alloc_consistent(dev, 4 * RX_QUEUE_SIZE, &rxq->dma_addr);
3586 if (!rxq->bd)
3587 return -ENOMEM;
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003588
Zhu Yib481de92007-09-25 17:54:57 -07003589 /* Fill the rx_used queue with _all_ of the Rx buffers */
3590 for (i = 0; i < RX_FREE_BUFFERS + RX_QUEUE_SIZE; i++)
3591 list_add_tail(&rxq->pool[i].list, &rxq->rx_used);
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003592
Zhu Yib481de92007-09-25 17:54:57 -07003593 /* Set us so that we have processed and used all buffers, but have
3594 * not restocked the Rx queue with fresh buffers */
3595 rxq->read = rxq->write = 0;
3596 rxq->free_count = 0;
3597 rxq->need_update = 0;
3598 return 0;
3599}
3600
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003601void iwl4965_rx_queue_reset(struct iwl_priv *priv, struct iwl4965_rx_queue *rxq)
Zhu Yib481de92007-09-25 17:54:57 -07003602{
3603 unsigned long flags;
3604 int i;
3605 spin_lock_irqsave(&rxq->lock, flags);
3606 INIT_LIST_HEAD(&rxq->rx_free);
3607 INIT_LIST_HEAD(&rxq->rx_used);
3608 /* Fill the rx_used queue with _all_ of the Rx buffers */
3609 for (i = 0; i < RX_FREE_BUFFERS + RX_QUEUE_SIZE; i++) {
3610 /* In the reset function, these buffers may have been allocated
3611 * to an SKB, so we need to unmap and free potential storage */
3612 if (rxq->pool[i].skb != NULL) {
3613 pci_unmap_single(priv->pci_dev,
3614 rxq->pool[i].dma_addr,
Tomas Winkler5425e492008-04-15 16:01:38 -07003615 priv->hw_params.rx_buf_size,
Ron Rindjunsky9ee1ba42007-11-26 16:14:42 +02003616 PCI_DMA_FROMDEVICE);
Zhu Yib481de92007-09-25 17:54:57 -07003617 priv->alloc_rxb_skb--;
3618 dev_kfree_skb(rxq->pool[i].skb);
3619 rxq->pool[i].skb = NULL;
3620 }
3621 list_add_tail(&rxq->pool[i].list, &rxq->rx_used);
3622 }
3623
3624 /* Set us so that we have processed and used all buffers, but have
3625 * not restocked the Rx queue with fresh buffers */
3626 rxq->read = rxq->write = 0;
3627 rxq->free_count = 0;
3628 spin_unlock_irqrestore(&rxq->lock, flags);
3629}
3630
3631/* Convert linear signal-to-noise ratio into dB */
3632static u8 ratio2dB[100] = {
3633/* 0 1 2 3 4 5 6 7 8 9 */
3634 0, 0, 6, 10, 12, 14, 16, 17, 18, 19, /* 00 - 09 */
3635 20, 21, 22, 22, 23, 23, 24, 25, 26, 26, /* 10 - 19 */
3636 26, 26, 26, 27, 27, 28, 28, 28, 29, 29, /* 20 - 29 */
3637 29, 30, 30, 30, 31, 31, 31, 31, 32, 32, /* 30 - 39 */
3638 32, 32, 32, 33, 33, 33, 33, 33, 34, 34, /* 40 - 49 */
3639 34, 34, 34, 34, 35, 35, 35, 35, 35, 35, /* 50 - 59 */
3640 36, 36, 36, 36, 36, 36, 36, 37, 37, 37, /* 60 - 69 */
3641 37, 37, 37, 37, 37, 38, 38, 38, 38, 38, /* 70 - 79 */
3642 38, 38, 38, 38, 38, 39, 39, 39, 39, 39, /* 80 - 89 */
3643 39, 39, 39, 39, 39, 40, 40, 40, 40, 40 /* 90 - 99 */
3644};
3645
3646/* Calculates a relative dB value from a ratio of linear
3647 * (i.e. not dB) signal levels.
3648 * Conversion assumes that levels are voltages (20*log), not powers (10*log). */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003649int iwl4965_calc_db_from_ratio(int sig_ratio)
Zhu Yib481de92007-09-25 17:54:57 -07003650{
Adrian Bunkc899a572007-10-14 19:51:15 +02003651 /* 1000:1 or higher just report as 60 dB */
3652 if (sig_ratio >= 1000)
Zhu Yib481de92007-09-25 17:54:57 -07003653 return 60;
3654
Adrian Bunkc899a572007-10-14 19:51:15 +02003655 /* 100:1 or higher, divide by 10 and use table,
Zhu Yib481de92007-09-25 17:54:57 -07003656 * add 20 dB to make up for divide by 10 */
Adrian Bunkc899a572007-10-14 19:51:15 +02003657 if (sig_ratio >= 100)
Zhu Yib481de92007-09-25 17:54:57 -07003658 return (20 + (int)ratio2dB[sig_ratio/10]);
3659
3660 /* We shouldn't see this */
3661 if (sig_ratio < 1)
3662 return 0;
3663
3664 /* Use table for ratios 1:1 - 99:1 */
3665 return (int)ratio2dB[sig_ratio];
3666}
3667
3668#define PERFECT_RSSI (-20) /* dBm */
3669#define WORST_RSSI (-95) /* dBm */
3670#define RSSI_RANGE (PERFECT_RSSI - WORST_RSSI)
3671
3672/* Calculate an indication of rx signal quality (a percentage, not dBm!).
3673 * See http://www.ces.clemson.edu/linux/signal_quality.shtml for info
3674 * about formulas used below. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003675int iwl4965_calc_sig_qual(int rssi_dbm, int noise_dbm)
Zhu Yib481de92007-09-25 17:54:57 -07003676{
3677 int sig_qual;
3678 int degradation = PERFECT_RSSI - rssi_dbm;
3679
3680 /* If we get a noise measurement, use signal-to-noise ratio (SNR)
3681 * as indicator; formula is (signal dbm - noise dbm).
3682 * SNR at or above 40 is a great signal (100%).
3683 * Below that, scale to fit SNR of 0 - 40 dB within 0 - 100% indicator.
3684 * Weakest usable signal is usually 10 - 15 dB SNR. */
3685 if (noise_dbm) {
3686 if (rssi_dbm - noise_dbm >= 40)
3687 return 100;
3688 else if (rssi_dbm < noise_dbm)
3689 return 0;
3690 sig_qual = ((rssi_dbm - noise_dbm) * 5) / 2;
3691
3692 /* Else use just the signal level.
3693 * This formula is a least squares fit of data points collected and
3694 * compared with a reference system that had a percentage (%) display
3695 * for signal quality. */
3696 } else
3697 sig_qual = (100 * (RSSI_RANGE * RSSI_RANGE) - degradation *
3698 (15 * RSSI_RANGE + 62 * degradation)) /
3699 (RSSI_RANGE * RSSI_RANGE);
3700
3701 if (sig_qual > 100)
3702 sig_qual = 100;
3703 else if (sig_qual < 1)
3704 sig_qual = 0;
3705
3706 return sig_qual;
3707}
3708
3709/**
Ben Cahill9fbab512007-11-29 11:09:47 +08003710 * iwl4965_rx_handle - Main entry function for receiving responses from uCode
Zhu Yib481de92007-09-25 17:54:57 -07003711 *
3712 * Uses the priv->rx_handlers callback function array to invoke
3713 * the appropriate handlers, including command responses,
3714 * frame-received notifications, and other notifications.
3715 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003716static void iwl4965_rx_handle(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003717{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003718 struct iwl4965_rx_mem_buffer *rxb;
3719 struct iwl4965_rx_packet *pkt;
3720 struct iwl4965_rx_queue *rxq = &priv->rxq;
Zhu Yib481de92007-09-25 17:54:57 -07003721 u32 r, i;
3722 int reclaim;
3723 unsigned long flags;
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08003724 u8 fill_rx = 0;
Mohamed Abbasd68ab682008-02-07 13:16:33 -08003725 u32 count = 8;
Zhu Yib481de92007-09-25 17:54:57 -07003726
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003727 /* uCode's read index (stored in shared DRAM) indicates the last Rx
3728 * buffer that the driver may process (last buffer filled by ucode). */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003729 r = iwl4965_hw_get_rx_read(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003730 i = rxq->read;
3731
3732 /* Rx interrupt, but nothing sent from uCode */
3733 if (i == r)
3734 IWL_DEBUG(IWL_DL_RX | IWL_DL_ISR, "r = %d, i = %d\n", r, i);
3735
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08003736 if (iwl4965_rx_queue_space(rxq) > (RX_QUEUE_SIZE / 2))
3737 fill_rx = 1;
3738
Zhu Yib481de92007-09-25 17:54:57 -07003739 while (i != r) {
3740 rxb = rxq->queue[i];
3741
Ben Cahill9fbab512007-11-29 11:09:47 +08003742 /* If an RXB doesn't have a Rx queue slot associated with it,
Zhu Yib481de92007-09-25 17:54:57 -07003743 * then a bug has been introduced in the queue refilling
3744 * routines -- catch it here */
3745 BUG_ON(rxb == NULL);
3746
3747 rxq->queue[i] = NULL;
3748
3749 pci_dma_sync_single_for_cpu(priv->pci_dev, rxb->dma_addr,
Tomas Winkler5425e492008-04-15 16:01:38 -07003750 priv->hw_params.rx_buf_size,
Zhu Yib481de92007-09-25 17:54:57 -07003751 PCI_DMA_FROMDEVICE);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003752 pkt = (struct iwl4965_rx_packet *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07003753
3754 /* Reclaim a command buffer only if this packet is a response
3755 * to a (driver-originated) command.
3756 * If the packet (e.g. Rx frame) originated from uCode,
3757 * there is no command buffer to reclaim.
3758 * Ucode should set SEQ_RX_FRAME bit if ucode-originated,
3759 * but apparently a few don't get set; catch them here. */
3760 reclaim = !(pkt->hdr.sequence & SEQ_RX_FRAME) &&
3761 (pkt->hdr.cmd != REPLY_RX_PHY_CMD) &&
Tomas Winkler857485c2008-03-21 13:53:44 -07003762 (pkt->hdr.cmd != REPLY_RX) &&
Zhu Yicfe01702007-09-27 11:27:31 +08003763 (pkt->hdr.cmd != REPLY_COMPRESSED_BA) &&
Zhu Yib481de92007-09-25 17:54:57 -07003764 (pkt->hdr.cmd != STATISTICS_NOTIFICATION) &&
3765 (pkt->hdr.cmd != REPLY_TX);
3766
3767 /* Based on type of command response or notification,
3768 * handle those that need handling via function in
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003769 * rx_handlers table. See iwl4965_setup_rx_handlers() */
Zhu Yib481de92007-09-25 17:54:57 -07003770 if (priv->rx_handlers[pkt->hdr.cmd]) {
3771 IWL_DEBUG(IWL_DL_HOST_COMMAND | IWL_DL_RX | IWL_DL_ISR,
3772 "r = %d, i = %d, %s, 0x%02x\n", r, i,
3773 get_cmd_string(pkt->hdr.cmd), pkt->hdr.cmd);
3774 priv->rx_handlers[pkt->hdr.cmd] (priv, rxb);
3775 } else {
3776 /* No handling needed */
3777 IWL_DEBUG(IWL_DL_HOST_COMMAND | IWL_DL_RX | IWL_DL_ISR,
3778 "r %d i %d No handler needed for %s, 0x%02x\n",
3779 r, i, get_cmd_string(pkt->hdr.cmd),
3780 pkt->hdr.cmd);
3781 }
3782
3783 if (reclaim) {
Ben Cahill9fbab512007-11-29 11:09:47 +08003784 /* Invoke any callbacks, transfer the skb to caller, and
Tomas Winkler857485c2008-03-21 13:53:44 -07003785 * fire off the (possibly) blocking iwl_send_cmd()
Zhu Yib481de92007-09-25 17:54:57 -07003786 * as we reclaim the driver command queue */
3787 if (rxb && rxb->skb)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003788 iwl4965_tx_cmd_complete(priv, rxb);
Zhu Yib481de92007-09-25 17:54:57 -07003789 else
3790 IWL_WARNING("Claim null rxb?\n");
3791 }
3792
3793 /* For now we just don't re-use anything. We can tweak this
3794 * later to try and re-use notification packets and SKBs that
3795 * fail to Rx correctly */
3796 if (rxb->skb != NULL) {
3797 priv->alloc_rxb_skb--;
3798 dev_kfree_skb_any(rxb->skb);
3799 rxb->skb = NULL;
3800 }
3801
3802 pci_unmap_single(priv->pci_dev, rxb->dma_addr,
Tomas Winkler5425e492008-04-15 16:01:38 -07003803 priv->hw_params.rx_buf_size,
Ron Rindjunsky9ee1ba42007-11-26 16:14:42 +02003804 PCI_DMA_FROMDEVICE);
Zhu Yib481de92007-09-25 17:54:57 -07003805 spin_lock_irqsave(&rxq->lock, flags);
3806 list_add_tail(&rxb->list, &priv->rxq.rx_used);
3807 spin_unlock_irqrestore(&rxq->lock, flags);
3808 i = (i + 1) & RX_QUEUE_MASK;
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08003809 /* If there are a lot of unused frames,
3810 * restock the Rx queue so ucode wont assert. */
3811 if (fill_rx) {
3812 count++;
3813 if (count >= 8) {
3814 priv->rxq.read = i;
3815 __iwl4965_rx_replenish(priv);
3816 count = 0;
3817 }
3818 }
Zhu Yib481de92007-09-25 17:54:57 -07003819 }
3820
3821 /* Backtrack one entry */
3822 priv->rxq.read = i;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003823 iwl4965_rx_queue_restock(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003824}
3825
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003826/**
3827 * iwl4965_tx_queue_update_write_ptr - Send new write index to hardware
3828 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003829static int iwl4965_tx_queue_update_write_ptr(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003830 struct iwl4965_tx_queue *txq)
Zhu Yib481de92007-09-25 17:54:57 -07003831{
3832 u32 reg = 0;
3833 int rc = 0;
3834 int txq_id = txq->q.id;
3835
3836 if (txq->need_update == 0)
3837 return rc;
3838
3839 /* if we're trying to save power */
3840 if (test_bit(STATUS_POWER_PMI, &priv->status)) {
3841 /* wake up nic if it's powered down ...
3842 * uCode will wake up, and interrupt us again, so next
3843 * time we'll skip this part. */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003844 reg = iwl_read32(priv, CSR_UCODE_DRV_GP1);
Zhu Yib481de92007-09-25 17:54:57 -07003845
3846 if (reg & CSR_UCODE_DRV_GP1_BIT_MAC_SLEEP) {
3847 IWL_DEBUG_INFO("Requesting wakeup, GP1 = 0x%x\n", reg);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003848 iwl_set_bit(priv, CSR_GP_CNTRL,
Zhu Yib481de92007-09-25 17:54:57 -07003849 CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
3850 return rc;
3851 }
3852
3853 /* restore this queue's parameters in nic hardware. */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003854 rc = iwl_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003855 if (rc)
3856 return rc;
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003857 iwl_write_direct32(priv, HBUS_TARG_WRPTR,
Tomas Winklerfc4b6852007-10-25 17:15:24 +08003858 txq->q.write_ptr | (txq_id << 8));
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003859 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003860
3861 /* else not in power-save mode, uCode will never sleep when we're
3862 * trying to tx (during RFKILL, we're not trying to tx). */
3863 } else
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003864 iwl_write32(priv, HBUS_TARG_WRPTR,
Tomas Winklerfc4b6852007-10-25 17:15:24 +08003865 txq->q.write_ptr | (txq_id << 8));
Zhu Yib481de92007-09-25 17:54:57 -07003866
3867 txq->need_update = 0;
3868
3869 return rc;
3870}
3871
Tomas Winkler0a6857e2008-03-12 16:58:49 -07003872#ifdef CONFIG_IWLWIFI_DEBUG
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003873static void iwl4965_print_rx_config_cmd(struct iwl4965_rxon_cmd *rxon)
Zhu Yib481de92007-09-25 17:54:57 -07003874{
Joe Perches0795af52007-10-03 17:59:30 -07003875 DECLARE_MAC_BUF(mac);
3876
Zhu Yib481de92007-09-25 17:54:57 -07003877 IWL_DEBUG_RADIO("RX CONFIG:\n");
Tomas Winkler0a6857e2008-03-12 16:58:49 -07003878 iwl_print_hex_dump(IWL_DL_RADIO, (u8 *) rxon, sizeof(*rxon));
Zhu Yib481de92007-09-25 17:54:57 -07003879 IWL_DEBUG_RADIO("u16 channel: 0x%x\n", le16_to_cpu(rxon->channel));
3880 IWL_DEBUG_RADIO("u32 flags: 0x%08X\n", le32_to_cpu(rxon->flags));
3881 IWL_DEBUG_RADIO("u32 filter_flags: 0x%08x\n",
3882 le32_to_cpu(rxon->filter_flags));
3883 IWL_DEBUG_RADIO("u8 dev_type: 0x%x\n", rxon->dev_type);
3884 IWL_DEBUG_RADIO("u8 ofdm_basic_rates: 0x%02x\n",
3885 rxon->ofdm_basic_rates);
3886 IWL_DEBUG_RADIO("u8 cck_basic_rates: 0x%02x\n", rxon->cck_basic_rates);
Joe Perches0795af52007-10-03 17:59:30 -07003887 IWL_DEBUG_RADIO("u8[6] node_addr: %s\n",
3888 print_mac(mac, rxon->node_addr));
3889 IWL_DEBUG_RADIO("u8[6] bssid_addr: %s\n",
3890 print_mac(mac, rxon->bssid_addr));
Zhu Yib481de92007-09-25 17:54:57 -07003891 IWL_DEBUG_RADIO("u16 assoc_id: 0x%x\n", le16_to_cpu(rxon->assoc_id));
3892}
3893#endif
3894
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003895static void iwl4965_enable_interrupts(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003896{
3897 IWL_DEBUG_ISR("Enabling interrupts\n");
3898 set_bit(STATUS_INT_ENABLED, &priv->status);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003899 iwl_write32(priv, CSR_INT_MASK, CSR_INI_SET_MASK);
Zhu Yib481de92007-09-25 17:54:57 -07003900}
3901
Mohamed Abbas0359fac2008-03-28 16:21:08 -07003902/* call this function to flush any scheduled tasklet */
3903static inline void iwl_synchronize_irq(struct iwl_priv *priv)
3904{
3905 /* wait to make sure we flush pedding tasklet*/
3906 synchronize_irq(priv->pci_dev->irq);
3907 tasklet_kill(&priv->irq_tasklet);
3908}
3909
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003910static inline void iwl4965_disable_interrupts(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003911{
3912 clear_bit(STATUS_INT_ENABLED, &priv->status);
3913
3914 /* disable interrupts from uCode/NIC to host */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003915 iwl_write32(priv, CSR_INT_MASK, 0x00000000);
Zhu Yib481de92007-09-25 17:54:57 -07003916
3917 /* acknowledge/clear/reset any interrupts still pending
3918 * from uCode or flow handler (Rx/Tx DMA) */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003919 iwl_write32(priv, CSR_INT, 0xffffffff);
3920 iwl_write32(priv, CSR_FH_INT_STATUS, 0xffffffff);
Zhu Yib481de92007-09-25 17:54:57 -07003921 IWL_DEBUG_ISR("Disabled interrupts\n");
3922}
3923
3924static const char *desc_lookup(int i)
3925{
3926 switch (i) {
3927 case 1:
3928 return "FAIL";
3929 case 2:
3930 return "BAD_PARAM";
3931 case 3:
3932 return "BAD_CHECKSUM";
3933 case 4:
3934 return "NMI_INTERRUPT";
3935 case 5:
3936 return "SYSASSERT";
3937 case 6:
3938 return "FATAL_ERROR";
3939 }
3940
3941 return "UNKNOWN";
3942}
3943
3944#define ERROR_START_OFFSET (1 * sizeof(u32))
3945#define ERROR_ELEM_SIZE (7 * sizeof(u32))
3946
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003947static void iwl4965_dump_nic_error_log(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003948{
3949 u32 data2, line;
3950 u32 desc, time, count, base, data1;
3951 u32 blink1, blink2, ilink1, ilink2;
3952 int rc;
3953
3954 base = le32_to_cpu(priv->card_alive.error_event_table_ptr);
3955
Tomas Winkler57aab752008-04-14 21:16:03 -07003956 if (!priv->cfg->ops->lib->is_valid_rtc_data_addr(base)) {
Zhu Yib481de92007-09-25 17:54:57 -07003957 IWL_ERROR("Not valid error log pointer 0x%08X\n", base);
3958 return;
3959 }
3960
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003961 rc = iwl_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003962 if (rc) {
3963 IWL_WARNING("Can not read from adapter at this time.\n");
3964 return;
3965 }
3966
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003967 count = iwl_read_targ_mem(priv, base);
Zhu Yib481de92007-09-25 17:54:57 -07003968
3969 if (ERROR_START_OFFSET <= count * ERROR_ELEM_SIZE) {
3970 IWL_ERROR("Start IWL Error Log Dump:\n");
Tomas Winkler2acae162008-03-02 01:25:59 +02003971 IWL_ERROR("Status: 0x%08lX, count: %d\n", priv->status, count);
Zhu Yib481de92007-09-25 17:54:57 -07003972 }
3973
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003974 desc = iwl_read_targ_mem(priv, base + 1 * sizeof(u32));
3975 blink1 = iwl_read_targ_mem(priv, base + 3 * sizeof(u32));
3976 blink2 = iwl_read_targ_mem(priv, base + 4 * sizeof(u32));
3977 ilink1 = iwl_read_targ_mem(priv, base + 5 * sizeof(u32));
3978 ilink2 = iwl_read_targ_mem(priv, base + 6 * sizeof(u32));
3979 data1 = iwl_read_targ_mem(priv, base + 7 * sizeof(u32));
3980 data2 = iwl_read_targ_mem(priv, base + 8 * sizeof(u32));
3981 line = iwl_read_targ_mem(priv, base + 9 * sizeof(u32));
3982 time = iwl_read_targ_mem(priv, base + 11 * sizeof(u32));
Zhu Yib481de92007-09-25 17:54:57 -07003983
3984 IWL_ERROR("Desc Time "
3985 "data1 data2 line\n");
3986 IWL_ERROR("%-13s (#%d) %010u 0x%08X 0x%08X %u\n",
3987 desc_lookup(desc), desc, time, data1, data2, line);
3988 IWL_ERROR("blink1 blink2 ilink1 ilink2\n");
3989 IWL_ERROR("0x%05X 0x%05X 0x%05X 0x%05X\n", blink1, blink2,
3990 ilink1, ilink2);
3991
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003992 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003993}
3994
3995#define EVENT_START_OFFSET (4 * sizeof(u32))
3996
3997/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003998 * iwl4965_print_event_log - Dump error event log to syslog
Zhu Yib481de92007-09-25 17:54:57 -07003999 *
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004000 * NOTE: Must be called with iwl_grab_nic_access() already obtained!
Zhu Yib481de92007-09-25 17:54:57 -07004001 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004002static void iwl4965_print_event_log(struct iwl_priv *priv, u32 start_idx,
Zhu Yib481de92007-09-25 17:54:57 -07004003 u32 num_events, u32 mode)
4004{
4005 u32 i;
4006 u32 base; /* SRAM byte address of event log header */
4007 u32 event_size; /* 2 u32s, or 3 u32s if timestamp recorded */
4008 u32 ptr; /* SRAM byte address of log data */
4009 u32 ev, time, data; /* event log data */
4010
4011 if (num_events == 0)
4012 return;
4013
4014 base = le32_to_cpu(priv->card_alive.log_event_table_ptr);
4015
4016 if (mode == 0)
4017 event_size = 2 * sizeof(u32);
4018 else
4019 event_size = 3 * sizeof(u32);
4020
4021 ptr = base + EVENT_START_OFFSET + (start_idx * event_size);
4022
4023 /* "time" is actually "data" for mode 0 (no timestamp).
4024 * place event id # at far right for easier visual parsing. */
4025 for (i = 0; i < num_events; i++) {
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004026 ev = iwl_read_targ_mem(priv, ptr);
Zhu Yib481de92007-09-25 17:54:57 -07004027 ptr += sizeof(u32);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004028 time = iwl_read_targ_mem(priv, ptr);
Zhu Yib481de92007-09-25 17:54:57 -07004029 ptr += sizeof(u32);
4030 if (mode == 0)
4031 IWL_ERROR("0x%08x\t%04u\n", time, ev); /* data, ev */
4032 else {
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004033 data = iwl_read_targ_mem(priv, ptr);
Zhu Yib481de92007-09-25 17:54:57 -07004034 ptr += sizeof(u32);
4035 IWL_ERROR("%010u\t0x%08x\t%04u\n", time, data, ev);
4036 }
4037 }
4038}
4039
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004040static void iwl4965_dump_nic_event_log(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004041{
4042 int rc;
4043 u32 base; /* SRAM byte address of event log header */
4044 u32 capacity; /* event log capacity in # entries */
4045 u32 mode; /* 0 - no timestamp, 1 - timestamp recorded */
4046 u32 num_wraps; /* # times uCode wrapped to top of log */
4047 u32 next_entry; /* index of next entry to be written by uCode */
4048 u32 size; /* # entries that we'll print */
4049
4050 base = le32_to_cpu(priv->card_alive.log_event_table_ptr);
Tomas Winkler57aab752008-04-14 21:16:03 -07004051 if (!priv->cfg->ops->lib->is_valid_rtc_data_addr(base)) {
Zhu Yib481de92007-09-25 17:54:57 -07004052 IWL_ERROR("Invalid event log pointer 0x%08X\n", base);
4053 return;
4054 }
4055
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004056 rc = iwl_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004057 if (rc) {
4058 IWL_WARNING("Can not read from adapter at this time.\n");
4059 return;
4060 }
4061
4062 /* event log header */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004063 capacity = iwl_read_targ_mem(priv, base);
4064 mode = iwl_read_targ_mem(priv, base + (1 * sizeof(u32)));
4065 num_wraps = iwl_read_targ_mem(priv, base + (2 * sizeof(u32)));
4066 next_entry = iwl_read_targ_mem(priv, base + (3 * sizeof(u32)));
Zhu Yib481de92007-09-25 17:54:57 -07004067
4068 size = num_wraps ? capacity : next_entry;
4069
4070 /* bail out if nothing in log */
4071 if (size == 0) {
Zhu Yi583fab32007-09-27 11:27:30 +08004072 IWL_ERROR("Start IWL Event Log Dump: nothing in log\n");
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004073 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004074 return;
4075 }
4076
Zhu Yi583fab32007-09-27 11:27:30 +08004077 IWL_ERROR("Start IWL Event Log Dump: display count %d, wraps %d\n",
Zhu Yib481de92007-09-25 17:54:57 -07004078 size, num_wraps);
4079
4080 /* if uCode has wrapped back to top of log, start at the oldest entry,
4081 * i.e the next one that uCode would fill. */
4082 if (num_wraps)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004083 iwl4965_print_event_log(priv, next_entry,
Zhu Yib481de92007-09-25 17:54:57 -07004084 capacity - next_entry, mode);
4085
4086 /* (then/else) start at top of log */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004087 iwl4965_print_event_log(priv, 0, next_entry, mode);
Zhu Yib481de92007-09-25 17:54:57 -07004088
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004089 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004090}
4091
4092/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004093 * iwl4965_irq_handle_error - called for HW or SW error interrupt from card
Zhu Yib481de92007-09-25 17:54:57 -07004094 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004095static void iwl4965_irq_handle_error(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004096{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004097 /* Set the FW error flag -- cleared on iwl4965_down */
Zhu Yib481de92007-09-25 17:54:57 -07004098 set_bit(STATUS_FW_ERROR, &priv->status);
4099
4100 /* Cancel currently queued command. */
4101 clear_bit(STATUS_HCMD_ACTIVE, &priv->status);
4102
Tomas Winkler0a6857e2008-03-12 16:58:49 -07004103#ifdef CONFIG_IWLWIFI_DEBUG
4104 if (iwl_debug_level & IWL_DL_FW_ERRORS) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004105 iwl4965_dump_nic_error_log(priv);
4106 iwl4965_dump_nic_event_log(priv);
4107 iwl4965_print_rx_config_cmd(&priv->staging_rxon);
Zhu Yib481de92007-09-25 17:54:57 -07004108 }
4109#endif
4110
4111 wake_up_interruptible(&priv->wait_command_queue);
4112
4113 /* Keep the restart process from trying to send host
4114 * commands by clearing the INIT status bit */
4115 clear_bit(STATUS_READY, &priv->status);
4116
4117 if (!test_bit(STATUS_EXIT_PENDING, &priv->status)) {
4118 IWL_DEBUG(IWL_DL_INFO | IWL_DL_FW_ERRORS,
4119 "Restarting adapter due to uCode error.\n");
4120
Tomas Winkler3109ece2008-03-28 16:33:35 -07004121 if (iwl_is_associated(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07004122 memcpy(&priv->recovery_rxon, &priv->active_rxon,
4123 sizeof(priv->recovery_rxon));
4124 priv->error_recovering = 1;
4125 }
4126 queue_work(priv->workqueue, &priv->restart);
4127 }
4128}
4129
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004130static void iwl4965_error_recovery(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004131{
4132 unsigned long flags;
4133
4134 memcpy(&priv->staging_rxon, &priv->recovery_rxon,
4135 sizeof(priv->staging_rxon));
4136 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004137 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004138
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004139 iwl4965_rxon_add_station(priv, priv->bssid, 1);
Zhu Yib481de92007-09-25 17:54:57 -07004140
4141 spin_lock_irqsave(&priv->lock, flags);
4142 priv->assoc_id = le16_to_cpu(priv->staging_rxon.assoc_id);
4143 priv->error_recovering = 0;
4144 spin_unlock_irqrestore(&priv->lock, flags);
4145}
4146
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004147static void iwl4965_irq_tasklet(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004148{
4149 u32 inta, handled = 0;
4150 u32 inta_fh;
4151 unsigned long flags;
Tomas Winkler0a6857e2008-03-12 16:58:49 -07004152#ifdef CONFIG_IWLWIFI_DEBUG
Zhu Yib481de92007-09-25 17:54:57 -07004153 u32 inta_mask;
4154#endif
4155
4156 spin_lock_irqsave(&priv->lock, flags);
4157
4158 /* Ack/clear/reset pending uCode interrupts.
4159 * Note: Some bits in CSR_INT are "OR" of bits in CSR_FH_INT_STATUS,
4160 * and will clear only when CSR_FH_INT_STATUS gets cleared. */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004161 inta = iwl_read32(priv, CSR_INT);
4162 iwl_write32(priv, CSR_INT, inta);
Zhu Yib481de92007-09-25 17:54:57 -07004163
4164 /* Ack/clear/reset pending flow-handler (DMA) interrupts.
4165 * Any new interrupts that happen after this, either while we're
4166 * in this tasklet, or later, will show up in next ISR/tasklet. */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004167 inta_fh = iwl_read32(priv, CSR_FH_INT_STATUS);
4168 iwl_write32(priv, CSR_FH_INT_STATUS, inta_fh);
Zhu Yib481de92007-09-25 17:54:57 -07004169
Tomas Winkler0a6857e2008-03-12 16:58:49 -07004170#ifdef CONFIG_IWLWIFI_DEBUG
4171 if (iwl_debug_level & IWL_DL_ISR) {
Ben Cahill9fbab512007-11-29 11:09:47 +08004172 /* just for debug */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004173 inta_mask = iwl_read32(priv, CSR_INT_MASK);
Zhu Yib481de92007-09-25 17:54:57 -07004174 IWL_DEBUG_ISR("inta 0x%08x, enabled 0x%08x, fh 0x%08x\n",
4175 inta, inta_mask, inta_fh);
4176 }
4177#endif
4178
4179 /* Since CSR_INT and CSR_FH_INT_STATUS reads and clears are not
4180 * atomic, make sure that inta covers all the interrupts that
4181 * we've discovered, even if FH interrupt came in just after
4182 * reading CSR_INT. */
Tomas Winkler6f83eaa2008-03-04 18:09:28 -08004183 if (inta_fh & CSR49_FH_INT_RX_MASK)
Zhu Yib481de92007-09-25 17:54:57 -07004184 inta |= CSR_INT_BIT_FH_RX;
Tomas Winkler6f83eaa2008-03-04 18:09:28 -08004185 if (inta_fh & CSR49_FH_INT_TX_MASK)
Zhu Yib481de92007-09-25 17:54:57 -07004186 inta |= CSR_INT_BIT_FH_TX;
4187
4188 /* Now service all interrupt bits discovered above. */
4189 if (inta & CSR_INT_BIT_HW_ERR) {
4190 IWL_ERROR("Microcode HW error detected. Restarting.\n");
4191
4192 /* Tell the device to stop sending interrupts */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004193 iwl4965_disable_interrupts(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004194
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004195 iwl4965_irq_handle_error(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004196
4197 handled |= CSR_INT_BIT_HW_ERR;
4198
4199 spin_unlock_irqrestore(&priv->lock, flags);
4200
4201 return;
4202 }
4203
Tomas Winkler0a6857e2008-03-12 16:58:49 -07004204#ifdef CONFIG_IWLWIFI_DEBUG
4205 if (iwl_debug_level & (IWL_DL_ISR)) {
Zhu Yib481de92007-09-25 17:54:57 -07004206 /* NIC fires this, but we don't use it, redundant with WAKEUP */
Joonwoo Park25c03d82008-01-23 10:15:20 -08004207 if (inta & CSR_INT_BIT_SCD)
4208 IWL_DEBUG_ISR("Scheduler finished to transmit "
4209 "the frame/frames.\n");
Zhu Yib481de92007-09-25 17:54:57 -07004210
4211 /* Alive notification via Rx interrupt will do the real work */
4212 if (inta & CSR_INT_BIT_ALIVE)
4213 IWL_DEBUG_ISR("Alive interrupt\n");
4214 }
4215#endif
4216 /* Safely ignore these bits for debug checks below */
Joonwoo Park25c03d82008-01-23 10:15:20 -08004217 inta &= ~(CSR_INT_BIT_SCD | CSR_INT_BIT_ALIVE);
Zhu Yib481de92007-09-25 17:54:57 -07004218
Ben Cahill9fbab512007-11-29 11:09:47 +08004219 /* HW RF KILL switch toggled */
Zhu Yib481de92007-09-25 17:54:57 -07004220 if (inta & CSR_INT_BIT_RF_KILL) {
4221 int hw_rf_kill = 0;
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004222 if (!(iwl_read32(priv, CSR_GP_CNTRL) &
Zhu Yib481de92007-09-25 17:54:57 -07004223 CSR_GP_CNTRL_REG_FLAG_HW_RF_KILL_SW))
4224 hw_rf_kill = 1;
4225
4226 IWL_DEBUG(IWL_DL_INFO | IWL_DL_RF_KILL | IWL_DL_ISR,
4227 "RF_KILL bit toggled to %s.\n",
4228 hw_rf_kill ? "disable radio":"enable radio");
4229
4230 /* Queue restart only if RF_KILL switch was set to "kill"
4231 * when we loaded driver, and is now set to "enable".
4232 * After we're Alive, RF_KILL gets handled by
Reinette Chatre32304552008-02-15 14:34:37 -08004233 * iwl4965_rx_card_state_notif() */
Zhu Yi53e49092007-12-06 16:08:44 +08004234 if (!hw_rf_kill && !test_bit(STATUS_ALIVE, &priv->status)) {
4235 clear_bit(STATUS_RF_KILL_HW, &priv->status);
Zhu Yib481de92007-09-25 17:54:57 -07004236 queue_work(priv->workqueue, &priv->restart);
Zhu Yi53e49092007-12-06 16:08:44 +08004237 }
Zhu Yib481de92007-09-25 17:54:57 -07004238
4239 handled |= CSR_INT_BIT_RF_KILL;
4240 }
4241
Ben Cahill9fbab512007-11-29 11:09:47 +08004242 /* Chip got too hot and stopped itself */
Zhu Yib481de92007-09-25 17:54:57 -07004243 if (inta & CSR_INT_BIT_CT_KILL) {
4244 IWL_ERROR("Microcode CT kill error detected.\n");
4245 handled |= CSR_INT_BIT_CT_KILL;
4246 }
4247
4248 /* Error detected by uCode */
4249 if (inta & CSR_INT_BIT_SW_ERR) {
4250 IWL_ERROR("Microcode SW error detected. Restarting 0x%X.\n",
4251 inta);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004252 iwl4965_irq_handle_error(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004253 handled |= CSR_INT_BIT_SW_ERR;
4254 }
4255
4256 /* uCode wakes up after power-down sleep */
4257 if (inta & CSR_INT_BIT_WAKEUP) {
4258 IWL_DEBUG_ISR("Wakeup interrupt\n");
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004259 iwl4965_rx_queue_update_write_ptr(priv, &priv->rxq);
4260 iwl4965_tx_queue_update_write_ptr(priv, &priv->txq[0]);
4261 iwl4965_tx_queue_update_write_ptr(priv, &priv->txq[1]);
4262 iwl4965_tx_queue_update_write_ptr(priv, &priv->txq[2]);
4263 iwl4965_tx_queue_update_write_ptr(priv, &priv->txq[3]);
4264 iwl4965_tx_queue_update_write_ptr(priv, &priv->txq[4]);
4265 iwl4965_tx_queue_update_write_ptr(priv, &priv->txq[5]);
Zhu Yib481de92007-09-25 17:54:57 -07004266
4267 handled |= CSR_INT_BIT_WAKEUP;
4268 }
4269
4270 /* All uCode command responses, including Tx command responses,
4271 * Rx "responses" (frame-received notification), and other
4272 * notifications from uCode come through here*/
4273 if (inta & (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX)) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004274 iwl4965_rx_handle(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004275 handled |= (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX);
4276 }
4277
4278 if (inta & CSR_INT_BIT_FH_TX) {
4279 IWL_DEBUG_ISR("Tx interrupt\n");
4280 handled |= CSR_INT_BIT_FH_TX;
4281 }
4282
4283 if (inta & ~handled)
4284 IWL_ERROR("Unhandled INTA bits 0x%08x\n", inta & ~handled);
4285
4286 if (inta & ~CSR_INI_SET_MASK) {
4287 IWL_WARNING("Disabled INTA bits 0x%08x were pending\n",
4288 inta & ~CSR_INI_SET_MASK);
4289 IWL_WARNING(" with FH_INT = 0x%08x\n", inta_fh);
4290 }
4291
4292 /* Re-enable all interrupts */
Mohamed Abbas0359fac2008-03-28 16:21:08 -07004293 /* only Re-enable if diabled by irq */
4294 if (test_bit(STATUS_INT_ENABLED, &priv->status))
4295 iwl4965_enable_interrupts(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004296
Tomas Winkler0a6857e2008-03-12 16:58:49 -07004297#ifdef CONFIG_IWLWIFI_DEBUG
4298 if (iwl_debug_level & (IWL_DL_ISR)) {
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004299 inta = iwl_read32(priv, CSR_INT);
4300 inta_mask = iwl_read32(priv, CSR_INT_MASK);
4301 inta_fh = iwl_read32(priv, CSR_FH_INT_STATUS);
Zhu Yib481de92007-09-25 17:54:57 -07004302 IWL_DEBUG_ISR("End inta 0x%08x, enabled 0x%08x, fh 0x%08x, "
4303 "flags 0x%08lx\n", inta, inta_mask, inta_fh, flags);
4304 }
4305#endif
4306 spin_unlock_irqrestore(&priv->lock, flags);
4307}
4308
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004309static irqreturn_t iwl4965_isr(int irq, void *data)
Zhu Yib481de92007-09-25 17:54:57 -07004310{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004311 struct iwl_priv *priv = data;
Zhu Yib481de92007-09-25 17:54:57 -07004312 u32 inta, inta_mask;
4313 u32 inta_fh;
4314 if (!priv)
4315 return IRQ_NONE;
4316
4317 spin_lock(&priv->lock);
4318
4319 /* Disable (but don't clear!) interrupts here to avoid
4320 * back-to-back ISRs and sporadic interrupts from our NIC.
4321 * If we have something to service, the tasklet will re-enable ints.
4322 * If we *don't* have something, we'll re-enable before leaving here. */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004323 inta_mask = iwl_read32(priv, CSR_INT_MASK); /* just for debug */
4324 iwl_write32(priv, CSR_INT_MASK, 0x00000000);
Zhu Yib481de92007-09-25 17:54:57 -07004325
4326 /* Discover which interrupts are active/pending */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004327 inta = iwl_read32(priv, CSR_INT);
4328 inta_fh = iwl_read32(priv, CSR_FH_INT_STATUS);
Zhu Yib481de92007-09-25 17:54:57 -07004329
4330 /* Ignore interrupt if there's nothing in NIC to service.
4331 * This may be due to IRQ shared with another device,
4332 * or due to sporadic interrupts thrown from our NIC. */
4333 if (!inta && !inta_fh) {
4334 IWL_DEBUG_ISR("Ignore interrupt, inta == 0, inta_fh == 0\n");
4335 goto none;
4336 }
4337
4338 if ((inta == 0xFFFFFFFF) || ((inta & 0xFFFFFFF0) == 0xa5a5a5a0)) {
Oliver Neukum66fbb542007-11-15 09:31:10 +08004339 /* Hardware disappeared. It might have already raised
4340 * an interrupt */
Zhu Yib481de92007-09-25 17:54:57 -07004341 IWL_WARNING("HARDWARE GONE?? INTA == 0x%080x\n", inta);
Oliver Neukum66fbb542007-11-15 09:31:10 +08004342 goto unplugged;
Zhu Yib481de92007-09-25 17:54:57 -07004343 }
4344
4345 IWL_DEBUG_ISR("ISR inta 0x%08x, enabled 0x%08x, fh 0x%08x\n",
4346 inta, inta_mask, inta_fh);
4347
Joonwoo Park25c03d82008-01-23 10:15:20 -08004348 inta &= ~CSR_INT_BIT_SCD;
4349
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004350 /* iwl4965_irq_tasklet() will service interrupts and re-enable them */
Joonwoo Park25c03d82008-01-23 10:15:20 -08004351 if (likely(inta || inta_fh))
4352 tasklet_schedule(&priv->irq_tasklet);
Zhu Yib481de92007-09-25 17:54:57 -07004353
Oliver Neukum66fbb542007-11-15 09:31:10 +08004354 unplugged:
4355 spin_unlock(&priv->lock);
Zhu Yib481de92007-09-25 17:54:57 -07004356 return IRQ_HANDLED;
4357
4358 none:
4359 /* re-enable interrupts here since we don't have anything to service. */
Mohamed Abbas0359fac2008-03-28 16:21:08 -07004360 /* only Re-enable if diabled by irq */
4361 if (test_bit(STATUS_INT_ENABLED, &priv->status))
4362 iwl4965_enable_interrupts(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004363 spin_unlock(&priv->lock);
4364 return IRQ_NONE;
4365}
4366
Zhu Yib481de92007-09-25 17:54:57 -07004367/* For active scan, listen ACTIVE_DWELL_TIME (msec) on each channel after
4368 * sending probe req. This should be set long enough to hear probe responses
4369 * from more than one AP. */
4370#define IWL_ACTIVE_DWELL_TIME_24 (20) /* all times in msec */
4371#define IWL_ACTIVE_DWELL_TIME_52 (10)
4372
4373/* For faster active scanning, scan will move to the next channel if fewer than
4374 * PLCP_QUIET_THRESH packets are heard on this channel within
4375 * ACTIVE_QUIET_TIME after sending probe request. This shortens the dwell
4376 * time if it's a quiet channel (nothing responded to our probe, and there's
4377 * no other traffic).
4378 * Disable "quiet" feature by setting PLCP_QUIET_THRESH to 0. */
4379#define IWL_PLCP_QUIET_THRESH __constant_cpu_to_le16(1) /* packets */
4380#define IWL_ACTIVE_QUIET_TIME __constant_cpu_to_le16(5) /* msec */
4381
4382/* For passive scan, listen PASSIVE_DWELL_TIME (msec) on each channel.
4383 * Must be set longer than active dwell time.
4384 * For the most reliable scan, set > AP beacon interval (typically 100msec). */
4385#define IWL_PASSIVE_DWELL_TIME_24 (20) /* all times in msec */
4386#define IWL_PASSIVE_DWELL_TIME_52 (10)
4387#define IWL_PASSIVE_DWELL_BASE (100)
4388#define IWL_CHANNEL_TUNE_TIME 5
4389
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004390static inline u16 iwl4965_get_active_dwell_time(struct iwl_priv *priv,
Johannes Berg8318d782008-01-24 19:38:38 +01004391 enum ieee80211_band band)
Zhu Yib481de92007-09-25 17:54:57 -07004392{
Johannes Berg8318d782008-01-24 19:38:38 +01004393 if (band == IEEE80211_BAND_5GHZ)
Zhu Yib481de92007-09-25 17:54:57 -07004394 return IWL_ACTIVE_DWELL_TIME_52;
4395 else
4396 return IWL_ACTIVE_DWELL_TIME_24;
4397}
4398
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004399static u16 iwl4965_get_passive_dwell_time(struct iwl_priv *priv,
Johannes Berg8318d782008-01-24 19:38:38 +01004400 enum ieee80211_band band)
Zhu Yib481de92007-09-25 17:54:57 -07004401{
Johannes Berg8318d782008-01-24 19:38:38 +01004402 u16 active = iwl4965_get_active_dwell_time(priv, band);
4403 u16 passive = (band != IEEE80211_BAND_5GHZ) ?
Zhu Yib481de92007-09-25 17:54:57 -07004404 IWL_PASSIVE_DWELL_BASE + IWL_PASSIVE_DWELL_TIME_24 :
4405 IWL_PASSIVE_DWELL_BASE + IWL_PASSIVE_DWELL_TIME_52;
4406
Tomas Winkler3109ece2008-03-28 16:33:35 -07004407 if (iwl_is_associated(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07004408 /* If we're associated, we clamp the maximum passive
4409 * dwell time to be 98% of the beacon interval (minus
4410 * 2 * channel tune time) */
4411 passive = priv->beacon_int;
4412 if ((passive > IWL_PASSIVE_DWELL_BASE) || !passive)
4413 passive = IWL_PASSIVE_DWELL_BASE;
4414 passive = (passive * 98) / 100 - IWL_CHANNEL_TUNE_TIME * 2;
4415 }
4416
4417 if (passive <= active)
4418 passive = active + 1;
4419
4420 return passive;
4421}
4422
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004423static int iwl4965_get_channels_for_scan(struct iwl_priv *priv,
Johannes Berg8318d782008-01-24 19:38:38 +01004424 enum ieee80211_band band,
Zhu Yib481de92007-09-25 17:54:57 -07004425 u8 is_active, u8 direct_mask,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004426 struct iwl4965_scan_channel *scan_ch)
Zhu Yib481de92007-09-25 17:54:57 -07004427{
4428 const struct ieee80211_channel *channels = NULL;
Johannes Berg8318d782008-01-24 19:38:38 +01004429 const struct ieee80211_supported_band *sband;
Assaf Kraussbf85ea42008-03-14 10:38:49 -07004430 const struct iwl_channel_info *ch_info;
Zhu Yib481de92007-09-25 17:54:57 -07004431 u16 passive_dwell = 0;
4432 u16 active_dwell = 0;
4433 int added, i;
4434
Emmanuel Grumbachd1141df2008-04-21 15:42:00 -07004435 sband = iwl_get_hw_mode(priv, band);
Johannes Berg8318d782008-01-24 19:38:38 +01004436 if (!sband)
Zhu Yib481de92007-09-25 17:54:57 -07004437 return 0;
4438
Johannes Berg8318d782008-01-24 19:38:38 +01004439 channels = sband->channels;
Zhu Yib481de92007-09-25 17:54:57 -07004440
Johannes Berg8318d782008-01-24 19:38:38 +01004441 active_dwell = iwl4965_get_active_dwell_time(priv, band);
4442 passive_dwell = iwl4965_get_passive_dwell_time(priv, band);
Zhu Yib481de92007-09-25 17:54:57 -07004443
Johannes Berg8318d782008-01-24 19:38:38 +01004444 for (i = 0, added = 0; i < sband->n_channels; i++) {
Johannes Berg182e2e62008-04-04 10:41:56 +02004445 if (channels[i].flags & IEEE80211_CHAN_DISABLED)
4446 continue;
4447
Johannes Berg8318d782008-01-24 19:38:38 +01004448 scan_ch->channel = ieee80211_frequency_to_channel(channels[i].center_freq);
Zhu Yib481de92007-09-25 17:54:57 -07004449
Assaf Krauss8622e702008-03-21 13:53:43 -07004450 ch_info = iwl_get_channel_info(priv, band,
Ben Cahill9fbab512007-11-29 11:09:47 +08004451 scan_ch->channel);
Zhu Yib481de92007-09-25 17:54:57 -07004452 if (!is_channel_valid(ch_info)) {
4453 IWL_DEBUG_SCAN("Channel %d is INVALID for this SKU.\n",
4454 scan_ch->channel);
4455 continue;
4456 }
4457
4458 if (!is_active || is_channel_passive(ch_info) ||
Johannes Berg8318d782008-01-24 19:38:38 +01004459 (channels[i].flags & IEEE80211_CHAN_PASSIVE_SCAN))
Zhu Yib481de92007-09-25 17:54:57 -07004460 scan_ch->type = 0; /* passive */
4461 else
4462 scan_ch->type = 1; /* active */
4463
4464 if (scan_ch->type & 1)
4465 scan_ch->type |= (direct_mask << 1);
4466
Zhu Yib481de92007-09-25 17:54:57 -07004467 scan_ch->active_dwell = cpu_to_le16(active_dwell);
4468 scan_ch->passive_dwell = cpu_to_le16(passive_dwell);
4469
Ben Cahill9fbab512007-11-29 11:09:47 +08004470 /* Set txpower levels to defaults */
Zhu Yib481de92007-09-25 17:54:57 -07004471 scan_ch->tpc.dsp_atten = 110;
4472 /* scan_pwr_info->tpc.dsp_atten; */
4473
4474 /*scan_pwr_info->tpc.tx_gain; */
Johannes Berg8318d782008-01-24 19:38:38 +01004475 if (band == IEEE80211_BAND_5GHZ)
Zhu Yib481de92007-09-25 17:54:57 -07004476 scan_ch->tpc.tx_gain = ((1 << 5) | (3 << 3)) | 3;
4477 else {
4478 scan_ch->tpc.tx_gain = ((1 << 5) | (5 << 3));
4479 /* NOTE: if we were doing 6Mb OFDM for scans we'd use
Ben Cahill9fbab512007-11-29 11:09:47 +08004480 * power level:
Reinette Chatre8a1b0242008-01-14 17:46:25 -08004481 * scan_ch->tpc.tx_gain = ((1 << 5) | (2 << 3)) | 3;
Zhu Yib481de92007-09-25 17:54:57 -07004482 */
4483 }
4484
4485 IWL_DEBUG_SCAN("Scanning %d [%s %d]\n",
4486 scan_ch->channel,
4487 (scan_ch->type & 1) ? "ACTIVE" : "PASSIVE",
4488 (scan_ch->type & 1) ?
4489 active_dwell : passive_dwell);
4490
4491 scan_ch++;
4492 added++;
4493 }
4494
4495 IWL_DEBUG_SCAN("total channels to scan %d \n", added);
4496 return added;
4497}
4498
Zhu Yib481de92007-09-25 17:54:57 -07004499/******************************************************************************
4500 *
4501 * uCode download functions
4502 *
4503 ******************************************************************************/
4504
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004505static void iwl4965_dealloc_ucode_pci(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004506{
Tomas Winkler98c92212008-01-14 17:46:20 -08004507 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_code);
4508 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_data);
4509 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_data_backup);
4510 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_init);
4511 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_init_data);
4512 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_boot);
Zhu Yib481de92007-09-25 17:54:57 -07004513}
4514
4515/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004516 * iwl4965_verify_inst_full - verify runtime uCode image in card vs. host,
Zhu Yib481de92007-09-25 17:54:57 -07004517 * looking at all data.
4518 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004519static int iwl4965_verify_inst_full(struct iwl_priv *priv, __le32 *image,
Ben Cahill9fbab512007-11-29 11:09:47 +08004520 u32 len)
Zhu Yib481de92007-09-25 17:54:57 -07004521{
4522 u32 val;
4523 u32 save_len = len;
4524 int rc = 0;
4525 u32 errcnt;
4526
4527 IWL_DEBUG_INFO("ucode inst image size is %u\n", len);
4528
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004529 rc = iwl_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004530 if (rc)
4531 return rc;
4532
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004533 iwl_write_direct32(priv, HBUS_TARG_MEM_RADDR, RTC_INST_LOWER_BOUND);
Zhu Yib481de92007-09-25 17:54:57 -07004534
4535 errcnt = 0;
4536 for (; len > 0; len -= sizeof(u32), image++) {
4537 /* read data comes through single port, auto-incr addr */
4538 /* NOTE: Use the debugless read so we don't flood kernel log
4539 * if IWL_DL_IO is set */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004540 val = _iwl_read_direct32(priv, HBUS_TARG_MEM_RDAT);
Zhu Yib481de92007-09-25 17:54:57 -07004541 if (val != le32_to_cpu(*image)) {
4542 IWL_ERROR("uCode INST section is invalid at "
4543 "offset 0x%x, is 0x%x, s/b 0x%x\n",
4544 save_len - len, val, le32_to_cpu(*image));
4545 rc = -EIO;
4546 errcnt++;
4547 if (errcnt >= 20)
4548 break;
4549 }
4550 }
4551
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004552 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004553
4554 if (!errcnt)
4555 IWL_DEBUG_INFO
4556 ("ucode image in INSTRUCTION memory is good\n");
4557
4558 return rc;
4559}
4560
4561
4562/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004563 * iwl4965_verify_inst_sparse - verify runtime uCode image in card vs. host,
Zhu Yib481de92007-09-25 17:54:57 -07004564 * using sample data 100 bytes apart. If these sample points are good,
4565 * it's a pretty good bet that everything between them is good, too.
4566 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004567static int iwl4965_verify_inst_sparse(struct iwl_priv *priv, __le32 *image, u32 len)
Zhu Yib481de92007-09-25 17:54:57 -07004568{
4569 u32 val;
4570 int rc = 0;
4571 u32 errcnt = 0;
4572 u32 i;
4573
4574 IWL_DEBUG_INFO("ucode inst image size is %u\n", len);
4575
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004576 rc = iwl_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004577 if (rc)
4578 return rc;
4579
4580 for (i = 0; i < len; i += 100, image += 100/sizeof(u32)) {
4581 /* read data comes through single port, auto-incr addr */
4582 /* NOTE: Use the debugless read so we don't flood kernel log
4583 * if IWL_DL_IO is set */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004584 iwl_write_direct32(priv, HBUS_TARG_MEM_RADDR,
Zhu Yib481de92007-09-25 17:54:57 -07004585 i + RTC_INST_LOWER_BOUND);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004586 val = _iwl_read_direct32(priv, HBUS_TARG_MEM_RDAT);
Zhu Yib481de92007-09-25 17:54:57 -07004587 if (val != le32_to_cpu(*image)) {
4588#if 0 /* Enable this if you want to see details */
4589 IWL_ERROR("uCode INST section is invalid at "
4590 "offset 0x%x, is 0x%x, s/b 0x%x\n",
4591 i, val, *image);
4592#endif
4593 rc = -EIO;
4594 errcnt++;
4595 if (errcnt >= 3)
4596 break;
4597 }
4598 }
4599
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004600 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004601
4602 return rc;
4603}
4604
4605
4606/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004607 * iwl4965_verify_ucode - determine which instruction image is in SRAM,
Zhu Yib481de92007-09-25 17:54:57 -07004608 * and verify its contents
4609 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004610static int iwl4965_verify_ucode(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004611{
4612 __le32 *image;
4613 u32 len;
4614 int rc = 0;
4615
4616 /* Try bootstrap */
4617 image = (__le32 *)priv->ucode_boot.v_addr;
4618 len = priv->ucode_boot.len;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004619 rc = iwl4965_verify_inst_sparse(priv, image, len);
Zhu Yib481de92007-09-25 17:54:57 -07004620 if (rc == 0) {
4621 IWL_DEBUG_INFO("Bootstrap uCode is good in inst SRAM\n");
4622 return 0;
4623 }
4624
4625 /* Try initialize */
4626 image = (__le32 *)priv->ucode_init.v_addr;
4627 len = priv->ucode_init.len;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004628 rc = iwl4965_verify_inst_sparse(priv, image, len);
Zhu Yib481de92007-09-25 17:54:57 -07004629 if (rc == 0) {
4630 IWL_DEBUG_INFO("Initialize uCode is good in inst SRAM\n");
4631 return 0;
4632 }
4633
4634 /* Try runtime/protocol */
4635 image = (__le32 *)priv->ucode_code.v_addr;
4636 len = priv->ucode_code.len;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004637 rc = iwl4965_verify_inst_sparse(priv, image, len);
Zhu Yib481de92007-09-25 17:54:57 -07004638 if (rc == 0) {
4639 IWL_DEBUG_INFO("Runtime uCode is good in inst SRAM\n");
4640 return 0;
4641 }
4642
4643 IWL_ERROR("NO VALID UCODE IMAGE IN INSTRUCTION SRAM!!\n");
4644
Ben Cahill9fbab512007-11-29 11:09:47 +08004645 /* Since nothing seems to match, show first several data entries in
4646 * instruction SRAM, so maybe visual inspection will give a clue.
4647 * Selection of bootstrap image (vs. other images) is arbitrary. */
Zhu Yib481de92007-09-25 17:54:57 -07004648 image = (__le32 *)priv->ucode_boot.v_addr;
4649 len = priv->ucode_boot.len;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004650 rc = iwl4965_verify_inst_full(priv, image, len);
Zhu Yib481de92007-09-25 17:54:57 -07004651
4652 return rc;
4653}
4654
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004655static void iwl4965_nic_start(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004656{
4657 /* Remove all resets to allow NIC to operate */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004658 iwl_write32(priv, CSR_RESET, 0);
Zhu Yib481de92007-09-25 17:54:57 -07004659}
4660
Tomas Winkler90e759d2007-11-29 11:09:41 +08004661
Zhu Yib481de92007-09-25 17:54:57 -07004662/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004663 * iwl4965_read_ucode - Read uCode images from disk file.
Zhu Yib481de92007-09-25 17:54:57 -07004664 *
4665 * Copy into buffers for card to fetch via bus-mastering
4666 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004667static int iwl4965_read_ucode(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004668{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004669 struct iwl4965_ucode *ucode;
Tomas Winkler90e759d2007-11-29 11:09:41 +08004670 int ret;
Zhu Yib481de92007-09-25 17:54:57 -07004671 const struct firmware *ucode_raw;
Tomas Winkler4bf775c2008-03-04 18:09:31 -08004672 const char *name = priv->cfg->fw_name;
Zhu Yib481de92007-09-25 17:54:57 -07004673 u8 *src;
4674 size_t len;
4675 u32 ver, inst_size, data_size, init_size, init_data_size, boot_size;
4676
4677 /* Ask kernel firmware_class module to get the boot firmware off disk.
4678 * request_firmware() is synchronous, file is in memory on return. */
Tomas Winkler90e759d2007-11-29 11:09:41 +08004679 ret = request_firmware(&ucode_raw, name, &priv->pci_dev->dev);
4680 if (ret < 0) {
4681 IWL_ERROR("%s firmware file req failed: Reason %d\n",
4682 name, ret);
Zhu Yib481de92007-09-25 17:54:57 -07004683 goto error;
4684 }
4685
4686 IWL_DEBUG_INFO("Got firmware '%s' file (%zd bytes) from disk\n",
4687 name, ucode_raw->size);
4688
4689 /* Make sure that we got at least our header! */
4690 if (ucode_raw->size < sizeof(*ucode)) {
4691 IWL_ERROR("File size way too small!\n");
Tomas Winkler90e759d2007-11-29 11:09:41 +08004692 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07004693 goto err_release;
4694 }
4695
4696 /* Data from ucode file: header followed by uCode images */
4697 ucode = (void *)ucode_raw->data;
4698
4699 ver = le32_to_cpu(ucode->ver);
4700 inst_size = le32_to_cpu(ucode->inst_size);
4701 data_size = le32_to_cpu(ucode->data_size);
4702 init_size = le32_to_cpu(ucode->init_size);
4703 init_data_size = le32_to_cpu(ucode->init_data_size);
4704 boot_size = le32_to_cpu(ucode->boot_size);
4705
4706 IWL_DEBUG_INFO("f/w package hdr ucode version = 0x%x\n", ver);
4707 IWL_DEBUG_INFO("f/w package hdr runtime inst size = %u\n",
4708 inst_size);
4709 IWL_DEBUG_INFO("f/w package hdr runtime data size = %u\n",
4710 data_size);
4711 IWL_DEBUG_INFO("f/w package hdr init inst size = %u\n",
4712 init_size);
4713 IWL_DEBUG_INFO("f/w package hdr init data size = %u\n",
4714 init_data_size);
4715 IWL_DEBUG_INFO("f/w package hdr boot inst size = %u\n",
4716 boot_size);
4717
4718 /* Verify size of file vs. image size info in file's header */
4719 if (ucode_raw->size < sizeof(*ucode) +
4720 inst_size + data_size + init_size +
4721 init_data_size + boot_size) {
4722
4723 IWL_DEBUG_INFO("uCode file size %d too small\n",
4724 (int)ucode_raw->size);
Tomas Winkler90e759d2007-11-29 11:09:41 +08004725 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07004726 goto err_release;
4727 }
4728
4729 /* Verify that uCode images will fit in card's SRAM */
Ron Rindjunsky099b40b2008-04-21 15:41:53 -07004730 if (inst_size > priv->hw_params.max_inst_size) {
Tomas Winkler90e759d2007-11-29 11:09:41 +08004731 IWL_DEBUG_INFO("uCode instr len %d too large to fit in\n",
4732 inst_size);
4733 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07004734 goto err_release;
4735 }
4736
Ron Rindjunsky099b40b2008-04-21 15:41:53 -07004737 if (data_size > priv->hw_params.max_data_size) {
Tomas Winkler90e759d2007-11-29 11:09:41 +08004738 IWL_DEBUG_INFO("uCode data len %d too large to fit in\n",
4739 data_size);
4740 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07004741 goto err_release;
4742 }
Ron Rindjunsky099b40b2008-04-21 15:41:53 -07004743 if (init_size > priv->hw_params.max_inst_size) {
Zhu Yib481de92007-09-25 17:54:57 -07004744 IWL_DEBUG_INFO
Tomas Winkler90e759d2007-11-29 11:09:41 +08004745 ("uCode init instr len %d too large to fit in\n",
4746 init_size);
4747 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07004748 goto err_release;
4749 }
Ron Rindjunsky099b40b2008-04-21 15:41:53 -07004750 if (init_data_size > priv->hw_params.max_data_size) {
Zhu Yib481de92007-09-25 17:54:57 -07004751 IWL_DEBUG_INFO
Tomas Winkler90e759d2007-11-29 11:09:41 +08004752 ("uCode init data len %d too large to fit in\n",
4753 init_data_size);
4754 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07004755 goto err_release;
4756 }
Ron Rindjunsky099b40b2008-04-21 15:41:53 -07004757 if (boot_size > priv->hw_params.max_bsm_size) {
Zhu Yib481de92007-09-25 17:54:57 -07004758 IWL_DEBUG_INFO
Tomas Winkler90e759d2007-11-29 11:09:41 +08004759 ("uCode boot instr len %d too large to fit in\n",
4760 boot_size);
4761 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07004762 goto err_release;
4763 }
4764
4765 /* Allocate ucode buffers for card's bus-master loading ... */
4766
4767 /* Runtime instructions and 2 copies of data:
4768 * 1) unmodified from disk
4769 * 2) backup cache for save/restore during power-downs */
4770 priv->ucode_code.len = inst_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08004771 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_code);
Zhu Yib481de92007-09-25 17:54:57 -07004772
4773 priv->ucode_data.len = data_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08004774 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_data);
Zhu Yib481de92007-09-25 17:54:57 -07004775
4776 priv->ucode_data_backup.len = data_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08004777 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_data_backup);
Zhu Yib481de92007-09-25 17:54:57 -07004778
4779 /* Initialization instructions and data */
Tomas Winkler90e759d2007-11-29 11:09:41 +08004780 if (init_size && init_data_size) {
4781 priv->ucode_init.len = init_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08004782 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_init);
Zhu Yib481de92007-09-25 17:54:57 -07004783
Tomas Winkler90e759d2007-11-29 11:09:41 +08004784 priv->ucode_init_data.len = init_data_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08004785 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_init_data);
Tomas Winkler90e759d2007-11-29 11:09:41 +08004786
4787 if (!priv->ucode_init.v_addr || !priv->ucode_init_data.v_addr)
4788 goto err_pci_alloc;
4789 }
Zhu Yib481de92007-09-25 17:54:57 -07004790
4791 /* Bootstrap (instructions only, no data) */
Tomas Winkler90e759d2007-11-29 11:09:41 +08004792 if (boot_size) {
4793 priv->ucode_boot.len = boot_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08004794 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_boot);
Zhu Yib481de92007-09-25 17:54:57 -07004795
Tomas Winkler90e759d2007-11-29 11:09:41 +08004796 if (!priv->ucode_boot.v_addr)
4797 goto err_pci_alloc;
4798 }
Zhu Yib481de92007-09-25 17:54:57 -07004799
4800 /* Copy images into buffers for card's bus-master reads ... */
4801
4802 /* Runtime instructions (first block of data in file) */
4803 src = &ucode->data[0];
4804 len = priv->ucode_code.len;
Tomas Winkler90e759d2007-11-29 11:09:41 +08004805 IWL_DEBUG_INFO("Copying (but not loading) uCode instr len %Zd\n", len);
Zhu Yib481de92007-09-25 17:54:57 -07004806 memcpy(priv->ucode_code.v_addr, src, len);
4807 IWL_DEBUG_INFO("uCode instr buf vaddr = 0x%p, paddr = 0x%08x\n",
4808 priv->ucode_code.v_addr, (u32)priv->ucode_code.p_addr);
4809
4810 /* Runtime data (2nd block)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004811 * NOTE: Copy into backup buffer will be done in iwl4965_up() */
Zhu Yib481de92007-09-25 17:54:57 -07004812 src = &ucode->data[inst_size];
4813 len = priv->ucode_data.len;
Tomas Winkler90e759d2007-11-29 11:09:41 +08004814 IWL_DEBUG_INFO("Copying (but not loading) uCode data len %Zd\n", len);
Zhu Yib481de92007-09-25 17:54:57 -07004815 memcpy(priv->ucode_data.v_addr, src, len);
4816 memcpy(priv->ucode_data_backup.v_addr, src, len);
4817
4818 /* Initialization instructions (3rd block) */
4819 if (init_size) {
4820 src = &ucode->data[inst_size + data_size];
4821 len = priv->ucode_init.len;
Tomas Winkler90e759d2007-11-29 11:09:41 +08004822 IWL_DEBUG_INFO("Copying (but not loading) init instr len %Zd\n",
4823 len);
Zhu Yib481de92007-09-25 17:54:57 -07004824 memcpy(priv->ucode_init.v_addr, src, len);
4825 }
4826
4827 /* Initialization data (4th block) */
4828 if (init_data_size) {
4829 src = &ucode->data[inst_size + data_size + init_size];
4830 len = priv->ucode_init_data.len;
Tomas Winkler90e759d2007-11-29 11:09:41 +08004831 IWL_DEBUG_INFO("Copying (but not loading) init data len %Zd\n",
4832 len);
Zhu Yib481de92007-09-25 17:54:57 -07004833 memcpy(priv->ucode_init_data.v_addr, src, len);
4834 }
4835
4836 /* Bootstrap instructions (5th block) */
4837 src = &ucode->data[inst_size + data_size + init_size + init_data_size];
4838 len = priv->ucode_boot.len;
Tomas Winkler90e759d2007-11-29 11:09:41 +08004839 IWL_DEBUG_INFO("Copying (but not loading) boot instr len %Zd\n", len);
Zhu Yib481de92007-09-25 17:54:57 -07004840 memcpy(priv->ucode_boot.v_addr, src, len);
4841
4842 /* We have our copies now, allow OS release its copies */
4843 release_firmware(ucode_raw);
4844 return 0;
4845
4846 err_pci_alloc:
4847 IWL_ERROR("failed to allocate pci memory\n");
Tomas Winkler90e759d2007-11-29 11:09:41 +08004848 ret = -ENOMEM;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004849 iwl4965_dealloc_ucode_pci(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004850
4851 err_release:
4852 release_firmware(ucode_raw);
4853
4854 error:
Tomas Winkler90e759d2007-11-29 11:09:41 +08004855 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07004856}
4857
4858
4859/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004860 * iwl4965_set_ucode_ptrs - Set uCode address location
Zhu Yib481de92007-09-25 17:54:57 -07004861 *
4862 * Tell initialization uCode where to find runtime uCode.
4863 *
4864 * BSM registers initially contain pointers to initialization uCode.
4865 * We need to replace them to load runtime uCode inst and data,
4866 * and to save runtime data when powering down.
4867 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004868static int iwl4965_set_ucode_ptrs(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004869{
4870 dma_addr_t pinst;
4871 dma_addr_t pdata;
4872 int rc = 0;
4873 unsigned long flags;
4874
4875 /* bits 35:4 for 4965 */
4876 pinst = priv->ucode_code.p_addr >> 4;
4877 pdata = priv->ucode_data_backup.p_addr >> 4;
4878
4879 spin_lock_irqsave(&priv->lock, flags);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004880 rc = iwl_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004881 if (rc) {
4882 spin_unlock_irqrestore(&priv->lock, flags);
4883 return rc;
4884 }
4885
4886 /* Tell bootstrap uCode where to find image to load */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004887 iwl_write_prph(priv, BSM_DRAM_INST_PTR_REG, pinst);
4888 iwl_write_prph(priv, BSM_DRAM_DATA_PTR_REG, pdata);
4889 iwl_write_prph(priv, BSM_DRAM_DATA_BYTECOUNT_REG,
Zhu Yib481de92007-09-25 17:54:57 -07004890 priv->ucode_data.len);
4891
4892 /* Inst bytecount must be last to set up, bit 31 signals uCode
4893 * that all new ptr/size info is in place */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004894 iwl_write_prph(priv, BSM_DRAM_INST_BYTECOUNT_REG,
Zhu Yib481de92007-09-25 17:54:57 -07004895 priv->ucode_code.len | BSM_DRAM_INST_LOAD);
4896
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004897 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004898
4899 spin_unlock_irqrestore(&priv->lock, flags);
4900
4901 IWL_DEBUG_INFO("Runtime uCode pointers are set.\n");
4902
4903 return rc;
4904}
4905
4906/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004907 * iwl4965_init_alive_start - Called after REPLY_ALIVE notification received
Zhu Yib481de92007-09-25 17:54:57 -07004908 *
4909 * Called after REPLY_ALIVE notification received from "initialize" uCode.
4910 *
4911 * The 4965 "initialize" ALIVE reply contains calibration data for:
4912 * Voltage, temperature, and MIMO tx gain correction, now stored in priv
4913 * (3945 does not contain this data).
4914 *
4915 * Tell "initialize" uCode to go ahead and load the runtime uCode.
4916*/
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004917static void iwl4965_init_alive_start(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004918{
4919 /* Check alive response for "valid" sign from uCode */
4920 if (priv->card_alive_init.is_valid != UCODE_VALID_OK) {
4921 /* We had an error bringing up the hardware, so take it
4922 * all the way back down so we can try again */
4923 IWL_DEBUG_INFO("Initialize Alive failed.\n");
4924 goto restart;
4925 }
4926
4927 /* Bootstrap uCode has loaded initialize uCode ... verify inst image.
4928 * This is a paranoid check, because we would not have gotten the
4929 * "initialize" alive if code weren't properly loaded. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004930 if (iwl4965_verify_ucode(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07004931 /* Runtime instruction load was bad;
4932 * take it all the way back down so we can try again */
4933 IWL_DEBUG_INFO("Bad \"initialize\" uCode load.\n");
4934 goto restart;
4935 }
4936
4937 /* Calculate temperature */
4938 priv->temperature = iwl4965_get_temperature(priv);
4939
4940 /* Send pointers to protocol/runtime uCode image ... init code will
4941 * load and launch runtime uCode, which will send us another "Alive"
4942 * notification. */
4943 IWL_DEBUG_INFO("Initialization Alive received.\n");
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004944 if (iwl4965_set_ucode_ptrs(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07004945 /* Runtime instruction load won't happen;
4946 * take it all the way back down so we can try again */
4947 IWL_DEBUG_INFO("Couldn't set up uCode pointers.\n");
4948 goto restart;
4949 }
4950 return;
4951
4952 restart:
4953 queue_work(priv->workqueue, &priv->restart);
4954}
4955
4956
4957/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004958 * iwl4965_alive_start - called after REPLY_ALIVE notification received
Zhu Yib481de92007-09-25 17:54:57 -07004959 * from protocol/runtime uCode (initialization uCode's
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004960 * Alive gets handled by iwl4965_init_alive_start()).
Zhu Yib481de92007-09-25 17:54:57 -07004961 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004962static void iwl4965_alive_start(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004963{
Tomas Winkler57aab752008-04-14 21:16:03 -07004964 int ret = 0;
Zhu Yib481de92007-09-25 17:54:57 -07004965
4966 IWL_DEBUG_INFO("Runtime Alive received.\n");
4967
4968 if (priv->card_alive.is_valid != UCODE_VALID_OK) {
4969 /* We had an error bringing up the hardware, so take it
4970 * all the way back down so we can try again */
4971 IWL_DEBUG_INFO("Alive failed.\n");
4972 goto restart;
4973 }
4974
4975 /* Initialize uCode has loaded Runtime uCode ... verify inst image.
4976 * This is a paranoid check, because we would not have gotten the
4977 * "runtime" alive if code weren't properly loaded. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004978 if (iwl4965_verify_ucode(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07004979 /* Runtime instruction load was bad;
4980 * take it all the way back down so we can try again */
4981 IWL_DEBUG_INFO("Bad runtime uCode load.\n");
4982 goto restart;
4983 }
4984
Assaf Kraussbf85ea42008-03-14 10:38:49 -07004985 iwlcore_clear_stations_table(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004986
Tomas Winkler57aab752008-04-14 21:16:03 -07004987 ret = priv->cfg->ops->lib->alive_notify(priv);
4988 if (ret) {
Zhu Yib481de92007-09-25 17:54:57 -07004989 IWL_WARNING("Could not complete ALIVE transition [ntf]: %d\n",
Tomas Winkler57aab752008-04-14 21:16:03 -07004990 ret);
Zhu Yib481de92007-09-25 17:54:57 -07004991 goto restart;
4992 }
4993
Ben Cahill9fbab512007-11-29 11:09:47 +08004994 /* After the ALIVE response, we can send host commands to 4965 uCode */
Zhu Yib481de92007-09-25 17:54:57 -07004995 set_bit(STATUS_ALIVE, &priv->status);
4996
4997 /* Clear out the uCode error bit if it is set */
4998 clear_bit(STATUS_FW_ERROR, &priv->status);
4999
Tomas Winklerfee12472008-04-03 16:05:21 -07005000 if (iwl_is_rfkill(priv))
Zhu Yib481de92007-09-25 17:54:57 -07005001 return;
5002
Zhu Yi5a66926a2008-01-14 17:46:18 -08005003 ieee80211_start_queues(priv->hw);
Zhu Yib481de92007-09-25 17:54:57 -07005004
5005 priv->active_rate = priv->rates_mask;
5006 priv->active_rate_basic = priv->rates_mask & IWL_BASIC_RATES_MASK;
5007
Tomas Winkler3109ece2008-03-28 16:33:35 -07005008 if (iwl_is_associated(priv)) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005009 struct iwl4965_rxon_cmd *active_rxon =
5010 (struct iwl4965_rxon_cmd *)(&priv->active_rxon);
Zhu Yib481de92007-09-25 17:54:57 -07005011
5012 memcpy(&priv->staging_rxon, &priv->active_rxon,
5013 sizeof(priv->staging_rxon));
5014 active_rxon->filter_flags &= ~RXON_FILTER_ASSOC_MSK;
5015 } else {
5016 /* Initialize our rx_config data */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005017 iwl4965_connection_init_rx_config(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005018 memcpy(priv->staging_rxon.node_addr, priv->mac_addr, ETH_ALEN);
5019 }
5020
Ben Cahill9fbab512007-11-29 11:09:47 +08005021 /* Configure Bluetooth device coexistence support */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005022 iwl4965_send_bt_config(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005023
5024 /* Configure the adapter for unassociated operation */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005025 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005026
5027 /* At this point, the NIC is initialized and operational */
5028 priv->notif_missed_beacons = 0;
Zhu Yib481de92007-09-25 17:54:57 -07005029
5030 iwl4965_rf_kill_ct_config(priv);
Zhu Yi5a66926a2008-01-14 17:46:18 -08005031
Reinette Chatrefe00b5a2008-04-03 16:05:23 -07005032 iwl_leds_register(priv);
5033
Zhu Yib481de92007-09-25 17:54:57 -07005034 IWL_DEBUG_INFO("ALIVE processing complete.\n");
Rick Farringtona9f46782008-03-18 14:57:49 -07005035 set_bit(STATUS_READY, &priv->status);
Zhu Yi5a66926a2008-01-14 17:46:18 -08005036 wake_up_interruptible(&priv->wait_command_queue);
Zhu Yib481de92007-09-25 17:54:57 -07005037
5038 if (priv->error_recovering)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005039 iwl4965_error_recovery(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005040
Mohamed Abbasc8381fd2008-03-28 16:21:05 -07005041 iwlcore_low_level_notify(priv, IWLCORE_START_EVT);
Mohamed Abbas84363e62008-04-04 16:59:58 -07005042 ieee80211_notify_mac(priv->hw, IEEE80211_NOTIFY_RE_ASSOC);
Zhu Yib481de92007-09-25 17:54:57 -07005043 return;
5044
5045 restart:
5046 queue_work(priv->workqueue, &priv->restart);
5047}
5048
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005049static void iwl4965_cancel_deferred_work(struct iwl_priv *priv);
Zhu Yib481de92007-09-25 17:54:57 -07005050
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005051static void __iwl4965_down(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005052{
5053 unsigned long flags;
5054 int exit_pending = test_bit(STATUS_EXIT_PENDING, &priv->status);
5055 struct ieee80211_conf *conf = NULL;
5056
5057 IWL_DEBUG_INFO(DRV_NAME " is going down\n");
5058
5059 conf = ieee80211_get_hw_conf(priv->hw);
5060
5061 if (!exit_pending)
5062 set_bit(STATUS_EXIT_PENDING, &priv->status);
5063
Mohamed Abbasab53d8a2008-03-25 16:33:36 -07005064 iwl_leds_unregister(priv);
5065
Mohamed Abbasc8381fd2008-03-28 16:21:05 -07005066 iwlcore_low_level_notify(priv, IWLCORE_STOP_EVT);
5067
Assaf Kraussbf85ea42008-03-14 10:38:49 -07005068 iwlcore_clear_stations_table(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005069
5070 /* Unblock any waiting calls */
5071 wake_up_interruptible_all(&priv->wait_command_queue);
5072
Zhu Yib481de92007-09-25 17:54:57 -07005073 /* Wipe out the EXIT_PENDING status bit if we are not actually
5074 * exiting the module */
5075 if (!exit_pending)
5076 clear_bit(STATUS_EXIT_PENDING, &priv->status);
5077
5078 /* stop and reset the on-board processor */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07005079 iwl_write32(priv, CSR_RESET, CSR_RESET_REG_FLAG_NEVO_RESET);
Zhu Yib481de92007-09-25 17:54:57 -07005080
5081 /* tell the device to stop sending interrupts */
Mohamed Abbas0359fac2008-03-28 16:21:08 -07005082 spin_lock_irqsave(&priv->lock, flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005083 iwl4965_disable_interrupts(priv);
Mohamed Abbas0359fac2008-03-28 16:21:08 -07005084 spin_unlock_irqrestore(&priv->lock, flags);
5085 iwl_synchronize_irq(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005086
5087 if (priv->mac80211_registered)
5088 ieee80211_stop_queues(priv->hw);
5089
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005090 /* If we have not previously called iwl4965_init() then
Zhu Yib481de92007-09-25 17:54:57 -07005091 * clear all bits but the RF Kill and SUSPEND bits and return */
Tomas Winklerfee12472008-04-03 16:05:21 -07005092 if (!iwl_is_init(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07005093 priv->status = test_bit(STATUS_RF_KILL_HW, &priv->status) <<
5094 STATUS_RF_KILL_HW |
5095 test_bit(STATUS_RF_KILL_SW, &priv->status) <<
5096 STATUS_RF_KILL_SW |
Reinette Chatre97888642008-02-06 11:20:38 -08005097 test_bit(STATUS_GEO_CONFIGURED, &priv->status) <<
5098 STATUS_GEO_CONFIGURED |
Zhu Yib481de92007-09-25 17:54:57 -07005099 test_bit(STATUS_IN_SUSPEND, &priv->status) <<
5100 STATUS_IN_SUSPEND;
5101 goto exit;
5102 }
5103
5104 /* ...otherwise clear out all the status bits but the RF Kill and
5105 * SUSPEND bits and continue taking the NIC down. */
5106 priv->status &= test_bit(STATUS_RF_KILL_HW, &priv->status) <<
5107 STATUS_RF_KILL_HW |
5108 test_bit(STATUS_RF_KILL_SW, &priv->status) <<
5109 STATUS_RF_KILL_SW |
Reinette Chatre97888642008-02-06 11:20:38 -08005110 test_bit(STATUS_GEO_CONFIGURED, &priv->status) <<
5111 STATUS_GEO_CONFIGURED |
Zhu Yib481de92007-09-25 17:54:57 -07005112 test_bit(STATUS_IN_SUSPEND, &priv->status) <<
5113 STATUS_IN_SUSPEND |
5114 test_bit(STATUS_FW_ERROR, &priv->status) <<
5115 STATUS_FW_ERROR;
5116
5117 spin_lock_irqsave(&priv->lock, flags);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07005118 iwl_clear_bit(priv, CSR_GP_CNTRL,
Ben Cahill9fbab512007-11-29 11:09:47 +08005119 CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
Zhu Yib481de92007-09-25 17:54:57 -07005120 spin_unlock_irqrestore(&priv->lock, flags);
5121
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005122 iwl4965_hw_txq_ctx_stop(priv);
5123 iwl4965_hw_rxq_stop(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005124
5125 spin_lock_irqsave(&priv->lock, flags);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07005126 if (!iwl_grab_nic_access(priv)) {
5127 iwl_write_prph(priv, APMG_CLK_DIS_REG,
Zhu Yib481de92007-09-25 17:54:57 -07005128 APMG_CLK_VAL_DMA_CLK_RQT);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07005129 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005130 }
5131 spin_unlock_irqrestore(&priv->lock, flags);
5132
5133 udelay(5);
5134
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005135 iwl4965_hw_nic_stop_master(priv);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07005136 iwl_set_bit(priv, CSR_RESET, CSR_RESET_REG_FLAG_SW_RESET);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005137 iwl4965_hw_nic_reset(priv);
Ron Rindjunsky399f4902008-04-23 17:14:56 -07005138 priv->cfg->ops->lib->free_shared_mem(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005139
5140 exit:
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005141 memset(&priv->card_alive, 0, sizeof(struct iwl4965_alive_resp));
Zhu Yib481de92007-09-25 17:54:57 -07005142
5143 if (priv->ibss_beacon)
5144 dev_kfree_skb(priv->ibss_beacon);
5145 priv->ibss_beacon = NULL;
5146
5147 /* clear out any free frames */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005148 iwl4965_clear_free_frames(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005149}
5150
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005151static void iwl4965_down(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005152{
5153 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005154 __iwl4965_down(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005155 mutex_unlock(&priv->mutex);
Zhu Yib24d22b2007-12-19 13:59:52 +08005156
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005157 iwl4965_cancel_deferred_work(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005158}
5159
5160#define MAX_HW_RESTARTS 5
5161
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005162static int __iwl4965_up(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005163{
Tomas Winkler57aab752008-04-14 21:16:03 -07005164 int i;
5165 int ret;
Zhu Yib481de92007-09-25 17:54:57 -07005166
5167 if (test_bit(STATUS_EXIT_PENDING, &priv->status)) {
5168 IWL_WARNING("Exit pending; will not bring the NIC up\n");
5169 return -EIO;
5170 }
5171
5172 if (test_bit(STATUS_RF_KILL_SW, &priv->status)) {
5173 IWL_WARNING("Radio disabled by SW RF kill (module "
5174 "parameter)\n");
Mohamed Abbasad97edd2008-03-28 16:21:06 -07005175 iwl_rfkill_set_hw_state(priv);
Zhu Yie655b9f2008-01-24 02:19:38 -08005176 return -ENODEV;
5177 }
5178
Reinette Chatree903fbd2008-01-30 22:05:15 -08005179 if (!priv->ucode_data_backup.v_addr || !priv->ucode_data.v_addr) {
5180 IWL_ERROR("ucode not available for device bringup\n");
5181 return -EIO;
5182 }
5183
Zhu Yie655b9f2008-01-24 02:19:38 -08005184 /* If platform's RF_KILL switch is NOT set to KILL */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07005185 if (iwl_read32(priv, CSR_GP_CNTRL) &
Zhu Yie655b9f2008-01-24 02:19:38 -08005186 CSR_GP_CNTRL_REG_FLAG_HW_RF_KILL_SW)
5187 clear_bit(STATUS_RF_KILL_HW, &priv->status);
5188 else {
5189 set_bit(STATUS_RF_KILL_HW, &priv->status);
5190 if (!test_bit(STATUS_IN_SUSPEND, &priv->status)) {
Mohamed Abbasad97edd2008-03-28 16:21:06 -07005191 iwl_rfkill_set_hw_state(priv);
Zhu Yie655b9f2008-01-24 02:19:38 -08005192 IWL_WARNING("Radio disabled by HW RF Kill switch\n");
5193 return -ENODEV;
5194 }
Zhu Yib481de92007-09-25 17:54:57 -07005195 }
5196
Mohamed Abbasad97edd2008-03-28 16:21:06 -07005197 iwl_rfkill_set_hw_state(priv);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07005198 iwl_write32(priv, CSR_INT, 0xFFFFFFFF);
Zhu Yib481de92007-09-25 17:54:57 -07005199
Ron Rindjunsky399f4902008-04-23 17:14:56 -07005200 ret = priv->cfg->ops->lib->alloc_shared_mem(priv);
5201 if (ret) {
5202 IWL_ERROR("Unable to allocate shared memory\n");
5203 return ret;
5204 }
5205
Tomas Winkler57aab752008-04-14 21:16:03 -07005206 ret = priv->cfg->ops->lib->hw_nic_init(priv);
5207 if (ret) {
5208 IWL_ERROR("Unable to init nic\n");
5209 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07005210 }
5211
5212 /* make sure rfkill handshake bits are cleared */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07005213 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
5214 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR,
Zhu Yib481de92007-09-25 17:54:57 -07005215 CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
5216
5217 /* clear (again), then enable host interrupts */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07005218 iwl_write32(priv, CSR_INT, 0xFFFFFFFF);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005219 iwl4965_enable_interrupts(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005220
5221 /* really make sure rfkill handshake bits are cleared */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07005222 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
5223 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
Zhu Yib481de92007-09-25 17:54:57 -07005224
5225 /* Copy original ucode data image from disk into backup cache.
5226 * This will be used to initialize the on-board processor's
5227 * data SRAM for a clean start when the runtime program first loads. */
5228 memcpy(priv->ucode_data_backup.v_addr, priv->ucode_data.v_addr,
Zhu Yi5a66926a2008-01-14 17:46:18 -08005229 priv->ucode_data.len);
Zhu Yib481de92007-09-25 17:54:57 -07005230
Zhu Yie655b9f2008-01-24 02:19:38 -08005231 /* We return success when we resume from suspend and rf_kill is on. */
5232 if (test_bit(STATUS_RF_KILL_HW, &priv->status))
Zhu Yib481de92007-09-25 17:54:57 -07005233 return 0;
Zhu Yib481de92007-09-25 17:54:57 -07005234
5235 for (i = 0; i < MAX_HW_RESTARTS; i++) {
5236
Assaf Kraussbf85ea42008-03-14 10:38:49 -07005237 iwlcore_clear_stations_table(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005238
5239 /* load bootstrap state machine,
5240 * load bootstrap program into processor's memory,
5241 * prepare to load the "initialize" uCode */
Tomas Winkler57aab752008-04-14 21:16:03 -07005242 ret = priv->cfg->ops->lib->load_ucode(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005243
Tomas Winkler57aab752008-04-14 21:16:03 -07005244 if (ret) {
5245 IWL_ERROR("Unable to set up bootstrap uCode: %d\n", ret);
Zhu Yib481de92007-09-25 17:54:57 -07005246 continue;
5247 }
5248
5249 /* start card; "initialize" will load runtime ucode */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005250 iwl4965_nic_start(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005251
Zhu Yib481de92007-09-25 17:54:57 -07005252 IWL_DEBUG_INFO(DRV_NAME " is coming up\n");
5253
5254 return 0;
5255 }
5256
5257 set_bit(STATUS_EXIT_PENDING, &priv->status);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005258 __iwl4965_down(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005259
5260 /* tried to restart and config the device for as long as our
5261 * patience could withstand */
5262 IWL_ERROR("Unable to initialize device after %d attempts.\n", i);
5263 return -EIO;
5264}
5265
5266
5267/*****************************************************************************
5268 *
5269 * Workqueue callbacks
5270 *
5271 *****************************************************************************/
5272
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005273static void iwl4965_bg_init_alive_start(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07005274{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005275 struct iwl_priv *priv =
5276 container_of(data, struct iwl_priv, init_alive_start.work);
Zhu Yib481de92007-09-25 17:54:57 -07005277
5278 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
5279 return;
5280
5281 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005282 iwl4965_init_alive_start(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005283 mutex_unlock(&priv->mutex);
5284}
5285
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005286static void iwl4965_bg_alive_start(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07005287{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005288 struct iwl_priv *priv =
5289 container_of(data, struct iwl_priv, alive_start.work);
Zhu Yib481de92007-09-25 17:54:57 -07005290
5291 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
5292 return;
5293
5294 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005295 iwl4965_alive_start(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005296 mutex_unlock(&priv->mutex);
5297}
5298
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005299static void iwl4965_bg_rf_kill(struct work_struct *work)
Zhu Yib481de92007-09-25 17:54:57 -07005300{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005301 struct iwl_priv *priv = container_of(work, struct iwl_priv, rf_kill);
Zhu Yib481de92007-09-25 17:54:57 -07005302
5303 wake_up_interruptible(&priv->wait_command_queue);
5304
5305 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
5306 return;
5307
5308 mutex_lock(&priv->mutex);
5309
Tomas Winklerfee12472008-04-03 16:05:21 -07005310 if (!iwl_is_rfkill(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07005311 IWL_DEBUG(IWL_DL_INFO | IWL_DL_RF_KILL,
5312 "HW and/or SW RF Kill no longer active, restarting "
5313 "device\n");
5314 if (!test_bit(STATUS_EXIT_PENDING, &priv->status))
5315 queue_work(priv->workqueue, &priv->restart);
5316 } else {
Mohamed Abbasad97edd2008-03-28 16:21:06 -07005317 /* make sure mac80211 stop sending Tx frame */
5318 if (priv->mac80211_registered)
5319 ieee80211_stop_queues(priv->hw);
Zhu Yib481de92007-09-25 17:54:57 -07005320
5321 if (!test_bit(STATUS_RF_KILL_HW, &priv->status))
5322 IWL_DEBUG_RF_KILL("Can not turn radio back on - "
5323 "disabled by SW switch\n");
5324 else
5325 IWL_WARNING("Radio Frequency Kill Switch is On:\n"
5326 "Kill switch must be turned off for "
5327 "wireless networking to work.\n");
5328 }
Mohamed Abbasad97edd2008-03-28 16:21:06 -07005329 iwl_rfkill_set_hw_state(priv);
5330
Zhu Yib481de92007-09-25 17:54:57 -07005331 mutex_unlock(&priv->mutex);
5332}
5333
5334#define IWL_SCAN_CHECK_WATCHDOG (7 * HZ)
5335
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005336static void iwl4965_bg_scan_check(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07005337{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005338 struct iwl_priv *priv =
5339 container_of(data, struct iwl_priv, scan_check.work);
Zhu Yib481de92007-09-25 17:54:57 -07005340
5341 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
5342 return;
5343
5344 mutex_lock(&priv->mutex);
5345 if (test_bit(STATUS_SCANNING, &priv->status) ||
5346 test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
5347 IWL_DEBUG(IWL_DL_INFO | IWL_DL_SCAN,
5348 "Scan completion watchdog resetting adapter (%dms)\n",
5349 jiffies_to_msecs(IWL_SCAN_CHECK_WATCHDOG));
mabbas052c4b92007-10-25 17:15:43 +08005350
Zhu Yib481de92007-09-25 17:54:57 -07005351 if (!test_bit(STATUS_EXIT_PENDING, &priv->status))
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005352 iwl4965_send_scan_abort(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005353 }
5354 mutex_unlock(&priv->mutex);
5355}
5356
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005357static void iwl4965_bg_request_scan(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07005358{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005359 struct iwl_priv *priv =
5360 container_of(data, struct iwl_priv, request_scan);
Tomas Winkler857485c2008-03-21 13:53:44 -07005361 struct iwl_host_cmd cmd = {
Zhu Yib481de92007-09-25 17:54:57 -07005362 .id = REPLY_SCAN_CMD,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005363 .len = sizeof(struct iwl4965_scan_cmd),
Zhu Yib481de92007-09-25 17:54:57 -07005364 .meta.flags = CMD_SIZE_HUGE,
5365 };
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005366 struct iwl4965_scan_cmd *scan;
Zhu Yib481de92007-09-25 17:54:57 -07005367 struct ieee80211_conf *conf = NULL;
Tomas Winkler78330fd2008-02-06 02:37:18 +02005368 u16 cmd_len;
Johannes Berg8318d782008-01-24 19:38:38 +01005369 enum ieee80211_band band;
Tomas Winkler78330fd2008-02-06 02:37:18 +02005370 u8 direct_mask;
Tomas Winkler857485c2008-03-21 13:53:44 -07005371 int ret = 0;
Zhu Yib481de92007-09-25 17:54:57 -07005372
5373 conf = ieee80211_get_hw_conf(priv->hw);
5374
5375 mutex_lock(&priv->mutex);
5376
Tomas Winklerfee12472008-04-03 16:05:21 -07005377 if (!iwl_is_ready(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07005378 IWL_WARNING("request scan called when driver not ready.\n");
5379 goto done;
5380 }
5381
5382 /* Make sure the scan wasn't cancelled before this queued work
5383 * was given the chance to run... */
5384 if (!test_bit(STATUS_SCANNING, &priv->status))
5385 goto done;
5386
5387 /* This should never be called or scheduled if there is currently
5388 * a scan active in the hardware. */
5389 if (test_bit(STATUS_SCAN_HW, &priv->status)) {
5390 IWL_DEBUG_INFO("Multiple concurrent scan requests in parallel. "
5391 "Ignoring second request.\n");
Tomas Winkler857485c2008-03-21 13:53:44 -07005392 ret = -EIO;
Zhu Yib481de92007-09-25 17:54:57 -07005393 goto done;
5394 }
5395
5396 if (test_bit(STATUS_EXIT_PENDING, &priv->status)) {
5397 IWL_DEBUG_SCAN("Aborting scan due to device shutdown\n");
5398 goto done;
5399 }
5400
5401 if (test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
5402 IWL_DEBUG_HC("Scan request while abort pending. Queuing.\n");
5403 goto done;
5404 }
5405
Tomas Winklerfee12472008-04-03 16:05:21 -07005406 if (iwl_is_rfkill(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07005407 IWL_DEBUG_HC("Aborting scan due to RF Kill activation\n");
5408 goto done;
5409 }
5410
5411 if (!test_bit(STATUS_READY, &priv->status)) {
5412 IWL_DEBUG_HC("Scan request while uninitialized. Queuing.\n");
5413 goto done;
5414 }
5415
5416 if (!priv->scan_bands) {
5417 IWL_DEBUG_HC("Aborting scan due to no requested bands\n");
5418 goto done;
5419 }
5420
5421 if (!priv->scan) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005422 priv->scan = kmalloc(sizeof(struct iwl4965_scan_cmd) +
Zhu Yib481de92007-09-25 17:54:57 -07005423 IWL_MAX_SCAN_SIZE, GFP_KERNEL);
5424 if (!priv->scan) {
Tomas Winkler857485c2008-03-21 13:53:44 -07005425 ret = -ENOMEM;
Zhu Yib481de92007-09-25 17:54:57 -07005426 goto done;
5427 }
5428 }
5429 scan = priv->scan;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005430 memset(scan, 0, sizeof(struct iwl4965_scan_cmd) + IWL_MAX_SCAN_SIZE);
Zhu Yib481de92007-09-25 17:54:57 -07005431
5432 scan->quiet_plcp_th = IWL_PLCP_QUIET_THRESH;
5433 scan->quiet_time = IWL_ACTIVE_QUIET_TIME;
5434
Tomas Winkler3109ece2008-03-28 16:33:35 -07005435 if (iwl_is_associated(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07005436 u16 interval = 0;
5437 u32 extra;
5438 u32 suspend_time = 100;
5439 u32 scan_suspend_time = 100;
5440 unsigned long flags;
5441
5442 IWL_DEBUG_INFO("Scanning while associated...\n");
5443
5444 spin_lock_irqsave(&priv->lock, flags);
5445 interval = priv->beacon_int;
5446 spin_unlock_irqrestore(&priv->lock, flags);
5447
5448 scan->suspend_time = 0;
mabbas052c4b92007-10-25 17:15:43 +08005449 scan->max_out_time = cpu_to_le32(200 * 1024);
Zhu Yib481de92007-09-25 17:54:57 -07005450 if (!interval)
5451 interval = suspend_time;
5452
5453 extra = (suspend_time / interval) << 22;
5454 scan_suspend_time = (extra |
5455 ((suspend_time % interval) * 1024));
5456 scan->suspend_time = cpu_to_le32(scan_suspend_time);
5457 IWL_DEBUG_SCAN("suspend_time 0x%X beacon interval %d\n",
5458 scan_suspend_time, interval);
5459 }
5460
5461 /* We should add the ability for user to lock to PASSIVE ONLY */
5462 if (priv->one_direct_scan) {
5463 IWL_DEBUG_SCAN
5464 ("Kicking off one direct scan for '%s'\n",
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005465 iwl4965_escape_essid(priv->direct_ssid,
Zhu Yib481de92007-09-25 17:54:57 -07005466 priv->direct_ssid_len));
5467 scan->direct_scan[0].id = WLAN_EID_SSID;
5468 scan->direct_scan[0].len = priv->direct_ssid_len;
5469 memcpy(scan->direct_scan[0].ssid,
5470 priv->direct_ssid, priv->direct_ssid_len);
5471 direct_mask = 1;
Tomas Winkler3109ece2008-03-28 16:33:35 -07005472 } else if (!iwl_is_associated(priv) && priv->essid_len) {
Bill Moss786b4552008-04-17 16:03:40 -07005473 IWL_DEBUG_SCAN
5474 ("Kicking off one direct scan for '%s' when not associated\n",
5475 iwl4965_escape_essid(priv->essid, priv->essid_len));
Zhu Yib481de92007-09-25 17:54:57 -07005476 scan->direct_scan[0].id = WLAN_EID_SSID;
5477 scan->direct_scan[0].len = priv->essid_len;
5478 memcpy(scan->direct_scan[0].ssid, priv->essid, priv->essid_len);
5479 direct_mask = 1;
Tomas Winkler857485c2008-03-21 13:53:44 -07005480 } else {
Bill Moss786b4552008-04-17 16:03:40 -07005481 IWL_DEBUG_SCAN("Kicking off one indirect scan.\n");
Zhu Yib481de92007-09-25 17:54:57 -07005482 direct_mask = 0;
Tomas Winkler857485c2008-03-21 13:53:44 -07005483 }
Zhu Yib481de92007-09-25 17:54:57 -07005484
Zhu Yib481de92007-09-25 17:54:57 -07005485 scan->tx_cmd.tx_flags = TX_CMD_FLG_SEQ_CTL_MSK;
Tomas Winkler5425e492008-04-15 16:01:38 -07005486 scan->tx_cmd.sta_id = priv->hw_params.bcast_sta_id;
Zhu Yib481de92007-09-25 17:54:57 -07005487 scan->tx_cmd.stop_time.life_time = TX_CMD_LIFE_TIME_INFINITE;
5488
Zhu Yib481de92007-09-25 17:54:57 -07005489
5490 switch (priv->scan_bands) {
5491 case 2:
5492 scan->flags = RXON_FLG_BAND_24G_MSK | RXON_FLG_AUTO_DETECT_MSK;
5493 scan->tx_cmd.rate_n_flags =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005494 iwl4965_hw_set_rate_n_flags(IWL_RATE_1M_PLCP,
Zhu Yib481de92007-09-25 17:54:57 -07005495 RATE_MCS_ANT_B_MSK|RATE_MCS_CCK_MSK);
5496
5497 scan->good_CRC_th = 0;
Johannes Berg8318d782008-01-24 19:38:38 +01005498 band = IEEE80211_BAND_2GHZ;
Zhu Yib481de92007-09-25 17:54:57 -07005499 break;
5500
5501 case 1:
5502 scan->tx_cmd.rate_n_flags =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005503 iwl4965_hw_set_rate_n_flags(IWL_RATE_6M_PLCP,
Zhu Yib481de92007-09-25 17:54:57 -07005504 RATE_MCS_ANT_B_MSK);
5505 scan->good_CRC_th = IWL_GOOD_CRC_TH;
Johannes Berg8318d782008-01-24 19:38:38 +01005506 band = IEEE80211_BAND_5GHZ;
Zhu Yib481de92007-09-25 17:54:57 -07005507 break;
5508
5509 default:
5510 IWL_WARNING("Invalid scan band count\n");
5511 goto done;
5512 }
5513
Tomas Winkler78330fd2008-02-06 02:37:18 +02005514 /* We don't build a direct scan probe request; the uCode will do
5515 * that based on the direct_mask added to each channel entry */
5516 cmd_len = iwl4965_fill_probe_req(priv, band,
5517 (struct ieee80211_mgmt *)scan->data,
5518 IWL_MAX_SCAN_SIZE - sizeof(*scan), 0);
5519
5520 scan->tx_cmd.len = cpu_to_le16(cmd_len);
Zhu Yib481de92007-09-25 17:54:57 -07005521 /* select Rx chains */
5522
5523 /* Force use of chains B and C (0x6) for scan Rx.
5524 * Avoid A (0x1) because of its off-channel reception on A-band.
5525 * MIMO is not used here, but value is required to make uCode happy. */
5526 scan->rx_chain = RXON_RX_CHAIN_DRIVER_FORCE_MSK |
5527 cpu_to_le16((0x7 << RXON_RX_CHAIN_VALID_POS) |
5528 (0x6 << RXON_RX_CHAIN_FORCE_SEL_POS) |
5529 (0x7 << RXON_RX_CHAIN_FORCE_MIMO_SEL_POS));
5530
5531 if (priv->iw_mode == IEEE80211_IF_TYPE_MNTR)
5532 scan->filter_flags = RXON_FILTER_PROMISC_MSK;
5533
Bill Moss786b4552008-04-17 16:03:40 -07005534 if (direct_mask)
Reinette Chatre26c0f032008-03-11 16:17:15 -07005535 scan->channel_count =
5536 iwl4965_get_channels_for_scan(
5537 priv, band, 1, /* active */
5538 direct_mask,
5539 (void *)&scan->data[le16_to_cpu(scan->tx_cmd.len)]);
Bill Moss786b4552008-04-17 16:03:40 -07005540 else
Reinette Chatre26c0f032008-03-11 16:17:15 -07005541 scan->channel_count =
5542 iwl4965_get_channels_for_scan(
5543 priv, band, 0, /* passive */
5544 direct_mask,
5545 (void *)&scan->data[le16_to_cpu(scan->tx_cmd.len)]);
Zhu Yib481de92007-09-25 17:54:57 -07005546
Mohamed Abbas5da4b552008-04-21 15:41:51 -07005547 scan->filter_flags |= (RXON_FILTER_ACCEPT_GRP_MSK |
5548 RXON_FILTER_BCON_AWARE_MSK);
Zhu Yib481de92007-09-25 17:54:57 -07005549 cmd.len += le16_to_cpu(scan->tx_cmd.len) +
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005550 scan->channel_count * sizeof(struct iwl4965_scan_channel);
Zhu Yib481de92007-09-25 17:54:57 -07005551 cmd.data = scan;
5552 scan->len = cpu_to_le16(cmd.len);
5553
5554 set_bit(STATUS_SCAN_HW, &priv->status);
Tomas Winkler857485c2008-03-21 13:53:44 -07005555 ret = iwl_send_cmd_sync(priv, &cmd);
5556 if (ret)
Zhu Yib481de92007-09-25 17:54:57 -07005557 goto done;
5558
5559 queue_delayed_work(priv->workqueue, &priv->scan_check,
5560 IWL_SCAN_CHECK_WATCHDOG);
5561
5562 mutex_unlock(&priv->mutex);
5563 return;
5564
5565 done:
Ian Schram01ebd062007-10-25 17:15:22 +08005566 /* inform mac80211 scan aborted */
Zhu Yib481de92007-09-25 17:54:57 -07005567 queue_work(priv->workqueue, &priv->scan_completed);
5568 mutex_unlock(&priv->mutex);
5569}
5570
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005571static void iwl4965_bg_up(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07005572{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005573 struct iwl_priv *priv = container_of(data, struct iwl_priv, up);
Zhu Yib481de92007-09-25 17:54:57 -07005574
5575 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
5576 return;
5577
5578 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005579 __iwl4965_up(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005580 mutex_unlock(&priv->mutex);
5581}
5582
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005583static void iwl4965_bg_restart(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07005584{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005585 struct iwl_priv *priv = container_of(data, struct iwl_priv, restart);
Zhu Yib481de92007-09-25 17:54:57 -07005586
5587 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
5588 return;
5589
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005590 iwl4965_down(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005591 queue_work(priv->workqueue, &priv->up);
5592}
5593
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005594static void iwl4965_bg_rx_replenish(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07005595{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005596 struct iwl_priv *priv =
5597 container_of(data, struct iwl_priv, rx_replenish);
Zhu Yib481de92007-09-25 17:54:57 -07005598
5599 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
5600 return;
5601
5602 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005603 iwl4965_rx_replenish(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005604 mutex_unlock(&priv->mutex);
5605}
5606
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08005607#define IWL_DELAY_NEXT_SCAN (HZ*2)
5608
Reinette Chatre508e32e2008-04-14 21:16:13 -07005609static void iwl4965_post_associate(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005610{
Zhu Yib481de92007-09-25 17:54:57 -07005611 struct ieee80211_conf *conf = NULL;
Tomas Winkler857485c2008-03-21 13:53:44 -07005612 int ret = 0;
Joe Perches0795af52007-10-03 17:59:30 -07005613 DECLARE_MAC_BUF(mac);
Zhu Yib481de92007-09-25 17:54:57 -07005614
5615 if (priv->iw_mode == IEEE80211_IF_TYPE_AP) {
5616 IWL_ERROR("%s Should not be called in AP mode\n", __FUNCTION__);
5617 return;
5618 }
5619
Joe Perches0795af52007-10-03 17:59:30 -07005620 IWL_DEBUG_ASSOC("Associated as %d to: %s\n",
5621 priv->assoc_id,
5622 print_mac(mac, priv->active_rxon.bssid_addr));
Zhu Yib481de92007-09-25 17:54:57 -07005623
5624
5625 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
5626 return;
5627
Zhu Yib481de92007-09-25 17:54:57 -07005628
Reinette Chatre508e32e2008-04-14 21:16:13 -07005629 if (!priv->vif || !priv->is_open)
Mohamed Abbas948c1712007-10-25 17:15:45 +08005630 return;
Reinette Chatre508e32e2008-04-14 21:16:13 -07005631
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005632 iwl4965_scan_cancel_timeout(priv, 200);
mabbas052c4b92007-10-25 17:15:43 +08005633
Zhu Yib481de92007-09-25 17:54:57 -07005634 conf = ieee80211_get_hw_conf(priv->hw);
5635
5636 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005637 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005638
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005639 memset(&priv->rxon_timing, 0, sizeof(struct iwl4965_rxon_time_cmd));
5640 iwl4965_setup_rxon_timing(priv);
Tomas Winkler857485c2008-03-21 13:53:44 -07005641 ret = iwl_send_cmd_pdu(priv, REPLY_RXON_TIMING,
Zhu Yib481de92007-09-25 17:54:57 -07005642 sizeof(priv->rxon_timing), &priv->rxon_timing);
Tomas Winkler857485c2008-03-21 13:53:44 -07005643 if (ret)
Zhu Yib481de92007-09-25 17:54:57 -07005644 IWL_WARNING("REPLY_RXON_TIMING failed - "
5645 "Attempting to continue.\n");
5646
5647 priv->staging_rxon.filter_flags |= RXON_FILTER_ASSOC_MSK;
5648
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08005649#ifdef CONFIG_IWL4965_HT
Ron Rindjunskyfd105e72007-11-26 16:14:39 +02005650 if (priv->current_ht_config.is_ht)
5651 iwl4965_set_rxon_ht(priv, &priv->current_ht_config);
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08005652#endif /* CONFIG_IWL4965_HT*/
Ron Rindjunskyc7de35c2008-04-23 17:15:05 -07005653 iwl_set_rxon_chain(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005654 priv->staging_rxon.assoc_id = cpu_to_le16(priv->assoc_id);
5655
5656 IWL_DEBUG_ASSOC("assoc id %d beacon interval %d\n",
5657 priv->assoc_id, priv->beacon_int);
5658
5659 if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_PREAMBLE)
5660 priv->staging_rxon.flags |= RXON_FLG_SHORT_PREAMBLE_MSK;
5661 else
5662 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_PREAMBLE_MSK;
5663
5664 if (priv->staging_rxon.flags & RXON_FLG_BAND_24G_MSK) {
5665 if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_SLOT_TIME)
5666 priv->staging_rxon.flags |= RXON_FLG_SHORT_SLOT_MSK;
5667 else
5668 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
5669
5670 if (priv->iw_mode == IEEE80211_IF_TYPE_IBSS)
5671 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
5672
5673 }
5674
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005675 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005676
5677 switch (priv->iw_mode) {
5678 case IEEE80211_IF_TYPE_STA:
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005679 iwl4965_rate_scale_init(priv->hw, IWL_AP_ID);
Zhu Yib481de92007-09-25 17:54:57 -07005680 break;
5681
5682 case IEEE80211_IF_TYPE_IBSS:
5683
5684 /* clear out the station table */
Assaf Kraussbf85ea42008-03-14 10:38:49 -07005685 iwlcore_clear_stations_table(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005686
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005687 iwl4965_rxon_add_station(priv, iwl4965_broadcast_addr, 0);
5688 iwl4965_rxon_add_station(priv, priv->bssid, 0);
5689 iwl4965_rate_scale_init(priv->hw, IWL_STA_ID);
5690 iwl4965_send_beacon_cmd(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005691
5692 break;
5693
5694 default:
5695 IWL_ERROR("%s Should not be called in %d mode\n",
5696 __FUNCTION__, priv->iw_mode);
5697 break;
5698 }
5699
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005700 iwl4965_sequence_reset(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005701
Zhu Yib481de92007-09-25 17:54:57 -07005702 /* Enable Rx differential gain and sensitivity calibrations */
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -07005703 iwl_chain_noise_reset(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005704 priv->start_calib = 1;
Zhu Yib481de92007-09-25 17:54:57 -07005705
5706 if (priv->iw_mode == IEEE80211_IF_TYPE_IBSS)
5707 priv->assoc_station_added = 1;
5708
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005709 iwl4965_activate_qos(priv, 0);
Ron Rindjunsky292ae172008-02-06 11:20:39 -08005710
Mohamed Abbas5da4b552008-04-21 15:41:51 -07005711 iwl_power_update_mode(priv, 0);
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08005712 /* we have just associated, don't start scan too early */
5713 priv->next_scan_jiffies = jiffies + IWL_DELAY_NEXT_SCAN;
Reinette Chatre508e32e2008-04-14 21:16:13 -07005714}
5715
5716
5717static void iwl4965_bg_post_associate(struct work_struct *data)
5718{
5719 struct iwl_priv *priv = container_of(data, struct iwl_priv,
5720 post_associate.work);
5721
5722 mutex_lock(&priv->mutex);
5723 iwl4965_post_associate(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005724 mutex_unlock(&priv->mutex);
Reinette Chatre508e32e2008-04-14 21:16:13 -07005725
Zhu Yib481de92007-09-25 17:54:57 -07005726}
5727
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005728static void iwl4965_bg_abort_scan(struct work_struct *work)
Zhu Yib481de92007-09-25 17:54:57 -07005729{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005730 struct iwl_priv *priv = container_of(work, struct iwl_priv, abort_scan);
Zhu Yib481de92007-09-25 17:54:57 -07005731
Tomas Winklerfee12472008-04-03 16:05:21 -07005732 if (!iwl_is_ready(priv))
Zhu Yib481de92007-09-25 17:54:57 -07005733 return;
5734
5735 mutex_lock(&priv->mutex);
5736
5737 set_bit(STATUS_SCAN_ABORTING, &priv->status);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005738 iwl4965_send_scan_abort(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005739
5740 mutex_unlock(&priv->mutex);
5741}
5742
Zhu Yi76bb77e2007-11-22 10:53:22 +08005743static int iwl4965_mac_config(struct ieee80211_hw *hw, struct ieee80211_conf *conf);
5744
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005745static void iwl4965_bg_scan_completed(struct work_struct *work)
Zhu Yib481de92007-09-25 17:54:57 -07005746{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005747 struct iwl_priv *priv =
5748 container_of(work, struct iwl_priv, scan_completed);
Zhu Yib481de92007-09-25 17:54:57 -07005749
5750 IWL_DEBUG(IWL_DL_INFO | IWL_DL_SCAN, "SCAN complete scan\n");
5751
5752 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
5753 return;
5754
Zhu Yia0646472007-12-20 14:10:01 +08005755 if (test_bit(STATUS_CONF_PENDING, &priv->status))
5756 iwl4965_mac_config(priv->hw, ieee80211_get_hw_conf(priv->hw));
Zhu Yi76bb77e2007-11-22 10:53:22 +08005757
Zhu Yib481de92007-09-25 17:54:57 -07005758 ieee80211_scan_completed(priv->hw);
5759
5760 /* Since setting the TXPOWER may have been deferred while
5761 * performing the scan, fire one off */
5762 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005763 iwl4965_hw_reg_send_txpower(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005764 mutex_unlock(&priv->mutex);
5765}
5766
5767/*****************************************************************************
5768 *
5769 * mac80211 entry point functions
5770 *
5771 *****************************************************************************/
5772
Zhu Yi5a66926a2008-01-14 17:46:18 -08005773#define UCODE_READY_TIMEOUT (2 * HZ)
5774
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005775static int iwl4965_mac_start(struct ieee80211_hw *hw)
Zhu Yib481de92007-09-25 17:54:57 -07005776{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005777 struct iwl_priv *priv = hw->priv;
Zhu Yi5a66926a2008-01-14 17:46:18 -08005778 int ret;
Zhu Yib481de92007-09-25 17:54:57 -07005779
5780 IWL_DEBUG_MAC80211("enter\n");
5781
Zhu Yi5a66926a2008-01-14 17:46:18 -08005782 if (pci_enable_device(priv->pci_dev)) {
5783 IWL_ERROR("Fail to pci_enable_device\n");
5784 return -ENODEV;
5785 }
5786 pci_restore_state(priv->pci_dev);
5787 pci_enable_msi(priv->pci_dev);
5788
5789 ret = request_irq(priv->pci_dev->irq, iwl4965_isr, IRQF_SHARED,
5790 DRV_NAME, priv);
5791 if (ret) {
5792 IWL_ERROR("Error allocating IRQ %d\n", priv->pci_dev->irq);
5793 goto out_disable_msi;
5794 }
5795
Zhu Yib481de92007-09-25 17:54:57 -07005796 /* we should be verifying the device is ready to be opened */
5797 mutex_lock(&priv->mutex);
5798
Zhu Yi5a66926a2008-01-14 17:46:18 -08005799 memset(&priv->staging_rxon, 0, sizeof(struct iwl4965_rxon_cmd));
5800 /* fetch ucode file from disk, alloc and copy to bus-master buffers ...
5801 * ucode filename and max sizes are card-specific. */
5802
5803 if (!priv->ucode_code.len) {
5804 ret = iwl4965_read_ucode(priv);
5805 if (ret) {
5806 IWL_ERROR("Could not read microcode: %d\n", ret);
5807 mutex_unlock(&priv->mutex);
5808 goto out_release_irq;
5809 }
5810 }
5811
Zhu Yie655b9f2008-01-24 02:19:38 -08005812 ret = __iwl4965_up(priv);
Zhu Yi5a66926a2008-01-14 17:46:18 -08005813
Zhu Yib481de92007-09-25 17:54:57 -07005814 mutex_unlock(&priv->mutex);
Zhu Yi5a66926a2008-01-14 17:46:18 -08005815
Zhu Yie655b9f2008-01-24 02:19:38 -08005816 if (ret)
5817 goto out_release_irq;
5818
5819 IWL_DEBUG_INFO("Start UP work done.\n");
5820
5821 if (test_bit(STATUS_IN_SUSPEND, &priv->status))
5822 return 0;
5823
Zhu Yi5a66926a2008-01-14 17:46:18 -08005824 /* Wait for START_ALIVE from ucode. Otherwise callbacks from
5825 * mac80211 will not be run successfully. */
5826 ret = wait_event_interruptible_timeout(priv->wait_command_queue,
5827 test_bit(STATUS_READY, &priv->status),
5828 UCODE_READY_TIMEOUT);
5829 if (!ret) {
5830 if (!test_bit(STATUS_READY, &priv->status)) {
5831 IWL_ERROR("Wait for START_ALIVE timeout after %dms.\n",
5832 jiffies_to_msecs(UCODE_READY_TIMEOUT));
5833 ret = -ETIMEDOUT;
5834 goto out_release_irq;
5835 }
5836 }
5837
Zhu Yie655b9f2008-01-24 02:19:38 -08005838 priv->is_open = 1;
Zhu Yib481de92007-09-25 17:54:57 -07005839 IWL_DEBUG_MAC80211("leave\n");
5840 return 0;
Zhu Yi5a66926a2008-01-14 17:46:18 -08005841
5842out_release_irq:
5843 free_irq(priv->pci_dev->irq, priv);
5844out_disable_msi:
5845 pci_disable_msi(priv->pci_dev);
Zhu Yie655b9f2008-01-24 02:19:38 -08005846 pci_disable_device(priv->pci_dev);
5847 priv->is_open = 0;
5848 IWL_DEBUG_MAC80211("leave - failed\n");
Zhu Yi5a66926a2008-01-14 17:46:18 -08005849 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07005850}
5851
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005852static void iwl4965_mac_stop(struct ieee80211_hw *hw)
Zhu Yib481de92007-09-25 17:54:57 -07005853{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005854 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07005855
5856 IWL_DEBUG_MAC80211("enter\n");
Mohamed Abbas948c1712007-10-25 17:15:45 +08005857
Zhu Yie655b9f2008-01-24 02:19:38 -08005858 if (!priv->is_open) {
5859 IWL_DEBUG_MAC80211("leave - skip\n");
5860 return;
5861 }
5862
Zhu Yib481de92007-09-25 17:54:57 -07005863 priv->is_open = 0;
Zhu Yi5a66926a2008-01-14 17:46:18 -08005864
Tomas Winklerfee12472008-04-03 16:05:21 -07005865 if (iwl_is_ready_rf(priv)) {
Zhu Yie655b9f2008-01-24 02:19:38 -08005866 /* stop mac, cancel any scan request and clear
5867 * RXON_FILTER_ASSOC_MSK BIT
5868 */
Zhu Yi5a66926a2008-01-14 17:46:18 -08005869 mutex_lock(&priv->mutex);
5870 iwl4965_scan_cancel_timeout(priv, 100);
5871 cancel_delayed_work(&priv->post_associate);
Mohamed Abbasfde35712007-11-29 11:10:15 +08005872 mutex_unlock(&priv->mutex);
Mohamed Abbasfde35712007-11-29 11:10:15 +08005873 }
5874
Zhu Yi5a66926a2008-01-14 17:46:18 -08005875 iwl4965_down(priv);
5876
5877 flush_workqueue(priv->workqueue);
5878 free_irq(priv->pci_dev->irq, priv);
5879 pci_disable_msi(priv->pci_dev);
5880 pci_save_state(priv->pci_dev);
5881 pci_disable_device(priv->pci_dev);
Mohamed Abbas948c1712007-10-25 17:15:45 +08005882
Zhu Yib481de92007-09-25 17:54:57 -07005883 IWL_DEBUG_MAC80211("leave\n");
Zhu Yib481de92007-09-25 17:54:57 -07005884}
5885
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005886static int iwl4965_mac_tx(struct ieee80211_hw *hw, struct sk_buff *skb,
Zhu Yib481de92007-09-25 17:54:57 -07005887 struct ieee80211_tx_control *ctl)
5888{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005889 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07005890
5891 IWL_DEBUG_MAC80211("enter\n");
5892
5893 if (priv->iw_mode == IEEE80211_IF_TYPE_MNTR) {
5894 IWL_DEBUG_MAC80211("leave - monitor\n");
5895 return -1;
5896 }
5897
5898 IWL_DEBUG_TX("dev->xmit(%d bytes) at rate 0x%02x\n", skb->len,
Johannes Berg8318d782008-01-24 19:38:38 +01005899 ctl->tx_rate->bitrate);
Zhu Yib481de92007-09-25 17:54:57 -07005900
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005901 if (iwl4965_tx_skb(priv, skb, ctl))
Zhu Yib481de92007-09-25 17:54:57 -07005902 dev_kfree_skb_any(skb);
5903
5904 IWL_DEBUG_MAC80211("leave\n");
5905 return 0;
5906}
5907
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005908static int iwl4965_mac_add_interface(struct ieee80211_hw *hw,
Zhu Yib481de92007-09-25 17:54:57 -07005909 struct ieee80211_if_init_conf *conf)
5910{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005911 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07005912 unsigned long flags;
Joe Perches0795af52007-10-03 17:59:30 -07005913 DECLARE_MAC_BUF(mac);
Zhu Yib481de92007-09-25 17:54:57 -07005914
Johannes Berg32bfd352007-12-19 01:31:26 +01005915 IWL_DEBUG_MAC80211("enter: type %d\n", conf->type);
Zhu Yib481de92007-09-25 17:54:57 -07005916
Johannes Berg32bfd352007-12-19 01:31:26 +01005917 if (priv->vif) {
5918 IWL_DEBUG_MAC80211("leave - vif != NULL\n");
Reinette Chatre75849d22008-01-23 10:15:17 -08005919 return -EOPNOTSUPP;
Zhu Yib481de92007-09-25 17:54:57 -07005920 }
5921
5922 spin_lock_irqsave(&priv->lock, flags);
Johannes Berg32bfd352007-12-19 01:31:26 +01005923 priv->vif = conf->vif;
Zhu Yib481de92007-09-25 17:54:57 -07005924
5925 spin_unlock_irqrestore(&priv->lock, flags);
5926
5927 mutex_lock(&priv->mutex);
Tomas Winkler864792e2007-11-27 21:00:52 +02005928
5929 if (conf->mac_addr) {
5930 IWL_DEBUG_MAC80211("Set %s\n", print_mac(mac, conf->mac_addr));
5931 memcpy(priv->mac_addr, conf->mac_addr, ETH_ALEN);
5932 }
Zhu Yib481de92007-09-25 17:54:57 -07005933
Tomas Winklerfee12472008-04-03 16:05:21 -07005934 if (iwl_is_ready(priv))
Zhu Yi5a66926a2008-01-14 17:46:18 -08005935 iwl4965_set_mode(priv, conf->type);
5936
Zhu Yib481de92007-09-25 17:54:57 -07005937 mutex_unlock(&priv->mutex);
5938
Zhu Yi5a66926a2008-01-14 17:46:18 -08005939 IWL_DEBUG_MAC80211("leave\n");
Zhu Yib481de92007-09-25 17:54:57 -07005940 return 0;
5941}
5942
5943/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005944 * iwl4965_mac_config - mac80211 config callback
Zhu Yib481de92007-09-25 17:54:57 -07005945 *
5946 * We ignore conf->flags & IEEE80211_CONF_SHORT_SLOT_TIME since it seems to
5947 * be set inappropriately and the driver currently sets the hardware up to
5948 * use it whenever needed.
5949 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005950static int iwl4965_mac_config(struct ieee80211_hw *hw, struct ieee80211_conf *conf)
Zhu Yib481de92007-09-25 17:54:57 -07005951{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005952 struct iwl_priv *priv = hw->priv;
Assaf Kraussbf85ea42008-03-14 10:38:49 -07005953 const struct iwl_channel_info *ch_info;
Zhu Yib481de92007-09-25 17:54:57 -07005954 unsigned long flags;
Zhu Yi76bb77e2007-11-22 10:53:22 +08005955 int ret = 0;
Zhu Yib481de92007-09-25 17:54:57 -07005956
5957 mutex_lock(&priv->mutex);
Johannes Berg8318d782008-01-24 19:38:38 +01005958 IWL_DEBUG_MAC80211("enter to channel %d\n", conf->channel->hw_value);
Zhu Yib481de92007-09-25 17:54:57 -07005959
Zhu Yi12342c42007-12-20 11:27:32 +08005960 priv->add_radiotap = !!(conf->flags & IEEE80211_CONF_RADIOTAP);
5961
Tomas Winklerfee12472008-04-03 16:05:21 -07005962 if (!iwl_is_ready(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07005963 IWL_DEBUG_MAC80211("leave - not ready\n");
Zhu Yi76bb77e2007-11-22 10:53:22 +08005964 ret = -EIO;
5965 goto out;
Zhu Yib481de92007-09-25 17:54:57 -07005966 }
5967
Assaf Krauss1ea87392008-03-18 14:57:50 -07005968 if (unlikely(!priv->cfg->mod_params->disable_hw_scan &&
Zhu Yib481de92007-09-25 17:54:57 -07005969 test_bit(STATUS_SCANNING, &priv->status))) {
Zhu Yia0646472007-12-20 14:10:01 +08005970 IWL_DEBUG_MAC80211("leave - scanning\n");
5971 set_bit(STATUS_CONF_PENDING, &priv->status);
Zhu Yib481de92007-09-25 17:54:57 -07005972 mutex_unlock(&priv->mutex);
Zhu Yia0646472007-12-20 14:10:01 +08005973 return 0;
Zhu Yib481de92007-09-25 17:54:57 -07005974 }
5975
5976 spin_lock_irqsave(&priv->lock, flags);
5977
Assaf Krauss8622e702008-03-21 13:53:43 -07005978 ch_info = iwl_get_channel_info(priv, conf->channel->band,
Johannes Berg8318d782008-01-24 19:38:38 +01005979 ieee80211_frequency_to_channel(conf->channel->center_freq));
Zhu Yib481de92007-09-25 17:54:57 -07005980 if (!is_channel_valid(ch_info)) {
Zhu Yib481de92007-09-25 17:54:57 -07005981 IWL_DEBUG_MAC80211("leave - invalid channel\n");
5982 spin_unlock_irqrestore(&priv->lock, flags);
Zhu Yi76bb77e2007-11-22 10:53:22 +08005983 ret = -EINVAL;
5984 goto out;
Zhu Yib481de92007-09-25 17:54:57 -07005985 }
5986
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08005987#ifdef CONFIG_IWL4965_HT
Tomas Winkler78330fd2008-02-06 02:37:18 +02005988 /* if we are switching from ht to 2.4 clear flags
Zhu Yib481de92007-09-25 17:54:57 -07005989 * from any ht related info since 2.4 does not
5990 * support ht */
Tomas Winkler78330fd2008-02-06 02:37:18 +02005991 if ((le16_to_cpu(priv->staging_rxon.channel) != conf->channel->hw_value)
Zhu Yib481de92007-09-25 17:54:57 -07005992#ifdef IEEE80211_CONF_CHANNEL_SWITCH
5993 && !(conf->flags & IEEE80211_CONF_CHANNEL_SWITCH)
5994#endif
5995 )
5996 priv->staging_rxon.flags = 0;
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08005997#endif /* CONFIG_IWL4965_HT */
Zhu Yib481de92007-09-25 17:54:57 -07005998
Ron Rindjunskyc7de35c2008-04-23 17:15:05 -07005999 iwl_set_rxon_channel(priv, conf->channel->band,
Johannes Berg8318d782008-01-24 19:38:38 +01006000 ieee80211_frequency_to_channel(conf->channel->center_freq));
Zhu Yib481de92007-09-25 17:54:57 -07006001
Johannes Berg8318d782008-01-24 19:38:38 +01006002 iwl4965_set_flags_for_phymode(priv, conf->channel->band);
Zhu Yib481de92007-09-25 17:54:57 -07006003
6004 /* The list of supported rates and rate mask can be different
Johannes Berg8318d782008-01-24 19:38:38 +01006005 * for each band; since the band may have changed, reset
Zhu Yib481de92007-09-25 17:54:57 -07006006 * the rate mask to what mac80211 lists */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006007 iwl4965_set_rate(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006008
6009 spin_unlock_irqrestore(&priv->lock, flags);
6010
6011#ifdef IEEE80211_CONF_CHANNEL_SWITCH
6012 if (conf->flags & IEEE80211_CONF_CHANNEL_SWITCH) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006013 iwl4965_hw_channel_switch(priv, conf->channel);
Zhu Yi76bb77e2007-11-22 10:53:22 +08006014 goto out;
Zhu Yib481de92007-09-25 17:54:57 -07006015 }
6016#endif
6017
Mohamed Abbasad97edd2008-03-28 16:21:06 -07006018 if (priv->cfg->ops->lib->radio_kill_sw)
6019 priv->cfg->ops->lib->radio_kill_sw(priv, !conf->radio_enabled);
Zhu Yib481de92007-09-25 17:54:57 -07006020
6021 if (!conf->radio_enabled) {
6022 IWL_DEBUG_MAC80211("leave - radio disabled\n");
Zhu Yi76bb77e2007-11-22 10:53:22 +08006023 goto out;
Zhu Yib481de92007-09-25 17:54:57 -07006024 }
6025
Tomas Winklerfee12472008-04-03 16:05:21 -07006026 if (iwl_is_rfkill(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07006027 IWL_DEBUG_MAC80211("leave - RF kill\n");
Zhu Yi76bb77e2007-11-22 10:53:22 +08006028 ret = -EIO;
6029 goto out;
Zhu Yib481de92007-09-25 17:54:57 -07006030 }
6031
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006032 iwl4965_set_rate(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006033
6034 if (memcmp(&priv->active_rxon,
6035 &priv->staging_rxon, sizeof(priv->staging_rxon)))
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006036 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006037 else
6038 IWL_DEBUG_INFO("No re-sending same RXON configuration.\n");
6039
6040 IWL_DEBUG_MAC80211("leave\n");
6041
Zhu Yia0646472007-12-20 14:10:01 +08006042out:
6043 clear_bit(STATUS_CONF_PENDING, &priv->status);
Zhu Yi5a66926a2008-01-14 17:46:18 -08006044 mutex_unlock(&priv->mutex);
Zhu Yi76bb77e2007-11-22 10:53:22 +08006045 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07006046}
6047
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006048static void iwl4965_config_ap(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07006049{
Tomas Winkler857485c2008-03-21 13:53:44 -07006050 int ret = 0;
Zhu Yib481de92007-09-25 17:54:57 -07006051
Maarten Lankhorstd986bcd2008-01-23 10:15:16 -08006052 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
Zhu Yib481de92007-09-25 17:54:57 -07006053 return;
6054
6055 /* The following should be done only at AP bring up */
6056 if ((priv->active_rxon.filter_flags & RXON_FILTER_ASSOC_MSK) == 0) {
6057
6058 /* RXON - unassoc (to set timing command) */
6059 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006060 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006061
6062 /* RXON Timing */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006063 memset(&priv->rxon_timing, 0, sizeof(struct iwl4965_rxon_time_cmd));
6064 iwl4965_setup_rxon_timing(priv);
Tomas Winkler857485c2008-03-21 13:53:44 -07006065 ret = iwl_send_cmd_pdu(priv, REPLY_RXON_TIMING,
Zhu Yib481de92007-09-25 17:54:57 -07006066 sizeof(priv->rxon_timing), &priv->rxon_timing);
Tomas Winkler857485c2008-03-21 13:53:44 -07006067 if (ret)
Zhu Yib481de92007-09-25 17:54:57 -07006068 IWL_WARNING("REPLY_RXON_TIMING failed - "
6069 "Attempting to continue.\n");
6070
Ron Rindjunskyc7de35c2008-04-23 17:15:05 -07006071 iwl_set_rxon_chain(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006072
6073 /* FIXME: what should be the assoc_id for AP? */
6074 priv->staging_rxon.assoc_id = cpu_to_le16(priv->assoc_id);
6075 if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_PREAMBLE)
6076 priv->staging_rxon.flags |=
6077 RXON_FLG_SHORT_PREAMBLE_MSK;
6078 else
6079 priv->staging_rxon.flags &=
6080 ~RXON_FLG_SHORT_PREAMBLE_MSK;
6081
6082 if (priv->staging_rxon.flags & RXON_FLG_BAND_24G_MSK) {
6083 if (priv->assoc_capability &
6084 WLAN_CAPABILITY_SHORT_SLOT_TIME)
6085 priv->staging_rxon.flags |=
6086 RXON_FLG_SHORT_SLOT_MSK;
6087 else
6088 priv->staging_rxon.flags &=
6089 ~RXON_FLG_SHORT_SLOT_MSK;
6090
6091 if (priv->iw_mode == IEEE80211_IF_TYPE_IBSS)
6092 priv->staging_rxon.flags &=
6093 ~RXON_FLG_SHORT_SLOT_MSK;
6094 }
6095 /* restore RXON assoc */
6096 priv->staging_rxon.filter_flags |= RXON_FILTER_ASSOC_MSK;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006097 iwl4965_commit_rxon(priv);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006098 iwl4965_activate_qos(priv, 1);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006099 iwl4965_rxon_add_station(priv, iwl4965_broadcast_addr, 0);
Zhu Yie1493de2007-09-27 11:27:32 +08006100 }
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006101 iwl4965_send_beacon_cmd(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006102
6103 /* FIXME - we need to add code here to detect a totally new
6104 * configuration, reset the AP, unassoc, rxon timing, assoc,
6105 * clear sta table, add BCAST sta... */
6106}
6107
Johannes Berg32bfd352007-12-19 01:31:26 +01006108static int iwl4965_mac_config_interface(struct ieee80211_hw *hw,
6109 struct ieee80211_vif *vif,
Zhu Yib481de92007-09-25 17:54:57 -07006110 struct ieee80211_if_conf *conf)
6111{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006112 struct iwl_priv *priv = hw->priv;
Joe Perches0795af52007-10-03 17:59:30 -07006113 DECLARE_MAC_BUF(mac);
Zhu Yib481de92007-09-25 17:54:57 -07006114 unsigned long flags;
6115 int rc;
6116
6117 if (conf == NULL)
6118 return -EIO;
6119
Emmanuel Grumbachb716bb92008-03-04 18:09:32 -08006120 if (priv->vif != vif) {
6121 IWL_DEBUG_MAC80211("leave - priv->vif != vif\n");
Emmanuel Grumbachb716bb92008-03-04 18:09:32 -08006122 return 0;
6123 }
6124
Zhu Yib481de92007-09-25 17:54:57 -07006125 if ((priv->iw_mode == IEEE80211_IF_TYPE_AP) &&
6126 (!conf->beacon || !conf->ssid_len)) {
6127 IWL_DEBUG_MAC80211
6128 ("Leaving in AP mode because HostAPD is not ready.\n");
6129 return 0;
6130 }
6131
Tomas Winklerfee12472008-04-03 16:05:21 -07006132 if (!iwl_is_alive(priv))
Zhu Yi5a66926a2008-01-14 17:46:18 -08006133 return -EAGAIN;
6134
Zhu Yib481de92007-09-25 17:54:57 -07006135 mutex_lock(&priv->mutex);
6136
Zhu Yib481de92007-09-25 17:54:57 -07006137 if (conf->bssid)
Joe Perches0795af52007-10-03 17:59:30 -07006138 IWL_DEBUG_MAC80211("bssid: %s\n",
6139 print_mac(mac, conf->bssid));
Zhu Yib481de92007-09-25 17:54:57 -07006140
Johannes Berg4150c572007-09-17 01:29:23 -04006141/*
6142 * very dubious code was here; the probe filtering flag is never set:
6143 *
Zhu Yib481de92007-09-25 17:54:57 -07006144 if (unlikely(test_bit(STATUS_SCANNING, &priv->status)) &&
6145 !(priv->hw->flags & IEEE80211_HW_NO_PROBE_FILTERING)) {
Johannes Berg4150c572007-09-17 01:29:23 -04006146 */
Zhu Yib481de92007-09-25 17:54:57 -07006147
6148 if (priv->iw_mode == IEEE80211_IF_TYPE_AP) {
6149 if (!conf->bssid) {
6150 conf->bssid = priv->mac_addr;
6151 memcpy(priv->bssid, priv->mac_addr, ETH_ALEN);
Joe Perches0795af52007-10-03 17:59:30 -07006152 IWL_DEBUG_MAC80211("bssid was set to: %s\n",
6153 print_mac(mac, conf->bssid));
Zhu Yib481de92007-09-25 17:54:57 -07006154 }
6155 if (priv->ibss_beacon)
6156 dev_kfree_skb(priv->ibss_beacon);
6157
6158 priv->ibss_beacon = conf->beacon;
6159 }
6160
Tomas Winklerfee12472008-04-03 16:05:21 -07006161 if (iwl_is_rfkill(priv))
Mohamed Abbasfde35712007-11-29 11:10:15 +08006162 goto done;
6163
Zhu Yib481de92007-09-25 17:54:57 -07006164 if (conf->bssid && !is_zero_ether_addr(conf->bssid) &&
6165 !is_multicast_ether_addr(conf->bssid)) {
6166 /* If there is currently a HW scan going on in the background
6167 * then we need to cancel it else the RXON below will fail. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006168 if (iwl4965_scan_cancel_timeout(priv, 100)) {
Zhu Yib481de92007-09-25 17:54:57 -07006169 IWL_WARNING("Aborted scan still in progress "
6170 "after 100ms\n");
6171 IWL_DEBUG_MAC80211("leaving - scan abort failed.\n");
6172 mutex_unlock(&priv->mutex);
6173 return -EAGAIN;
6174 }
6175 memcpy(priv->staging_rxon.bssid_addr, conf->bssid, ETH_ALEN);
6176
6177 /* TODO: Audit driver for usage of these members and see
6178 * if mac80211 deprecates them (priv->bssid looks like it
6179 * shouldn't be there, but I haven't scanned the IBSS code
6180 * to verify) - jpk */
6181 memcpy(priv->bssid, conf->bssid, ETH_ALEN);
6182
6183 if (priv->iw_mode == IEEE80211_IF_TYPE_AP)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006184 iwl4965_config_ap(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006185 else {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006186 rc = iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006187 if ((priv->iw_mode == IEEE80211_IF_TYPE_STA) && rc)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006188 iwl4965_rxon_add_station(
Zhu Yib481de92007-09-25 17:54:57 -07006189 priv, priv->active_rxon.bssid_addr, 1);
6190 }
6191
6192 } else {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006193 iwl4965_scan_cancel_timeout(priv, 100);
Zhu Yib481de92007-09-25 17:54:57 -07006194 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006195 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006196 }
6197
Mohamed Abbasfde35712007-11-29 11:10:15 +08006198 done:
Zhu Yib481de92007-09-25 17:54:57 -07006199 spin_lock_irqsave(&priv->lock, flags);
6200 if (!conf->ssid_len)
6201 memset(priv->essid, 0, IW_ESSID_MAX_SIZE);
6202 else
6203 memcpy(priv->essid, conf->ssid, conf->ssid_len);
6204
6205 priv->essid_len = conf->ssid_len;
6206 spin_unlock_irqrestore(&priv->lock, flags);
6207
6208 IWL_DEBUG_MAC80211("leave\n");
6209 mutex_unlock(&priv->mutex);
6210
6211 return 0;
6212}
6213
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006214static void iwl4965_configure_filter(struct ieee80211_hw *hw,
Johannes Berg4150c572007-09-17 01:29:23 -04006215 unsigned int changed_flags,
6216 unsigned int *total_flags,
6217 int mc_count, struct dev_addr_list *mc_list)
6218{
6219 /*
6220 * XXX: dummy
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006221 * see also iwl4965_connection_init_rx_config
Johannes Berg4150c572007-09-17 01:29:23 -04006222 */
6223 *total_flags = 0;
6224}
6225
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006226static void iwl4965_mac_remove_interface(struct ieee80211_hw *hw,
Zhu Yib481de92007-09-25 17:54:57 -07006227 struct ieee80211_if_init_conf *conf)
6228{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006229 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07006230
6231 IWL_DEBUG_MAC80211("enter\n");
6232
6233 mutex_lock(&priv->mutex);
Mohamed Abbas948c1712007-10-25 17:15:45 +08006234
Tomas Winklerfee12472008-04-03 16:05:21 -07006235 if (iwl_is_ready_rf(priv)) {
Mohamed Abbasfde35712007-11-29 11:10:15 +08006236 iwl4965_scan_cancel_timeout(priv, 100);
6237 cancel_delayed_work(&priv->post_associate);
6238 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
6239 iwl4965_commit_rxon(priv);
6240 }
Johannes Berg32bfd352007-12-19 01:31:26 +01006241 if (priv->vif == conf->vif) {
6242 priv->vif = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07006243 memset(priv->bssid, 0, ETH_ALEN);
6244 memset(priv->essid, 0, IW_ESSID_MAX_SIZE);
6245 priv->essid_len = 0;
6246 }
6247 mutex_unlock(&priv->mutex);
6248
6249 IWL_DEBUG_MAC80211("leave\n");
6250
6251}
Johannes Berg471b3ef2007-12-28 14:32:58 +01006252
Tomas Winkler3109ece2008-03-28 16:33:35 -07006253#define IWL_DELAY_NEXT_SCAN_AFTER_ASSOC (HZ*6)
Johannes Berg471b3ef2007-12-28 14:32:58 +01006254static void iwl4965_bss_info_changed(struct ieee80211_hw *hw,
6255 struct ieee80211_vif *vif,
6256 struct ieee80211_bss_conf *bss_conf,
6257 u32 changes)
Tomas Winkler220173b2007-10-16 00:50:25 +02006258{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006259 struct iwl_priv *priv = hw->priv;
Tomas Winkler220173b2007-10-16 00:50:25 +02006260
Tomas Winkler3109ece2008-03-28 16:33:35 -07006261 IWL_DEBUG_MAC80211("changes = 0x%X\n", changes);
6262
Johannes Berg471b3ef2007-12-28 14:32:58 +01006263 if (changes & BSS_CHANGED_ERP_PREAMBLE) {
Tomas Winkler3109ece2008-03-28 16:33:35 -07006264 IWL_DEBUG_MAC80211("ERP_PREAMBLE %d\n",
6265 bss_conf->use_short_preamble);
Johannes Berg471b3ef2007-12-28 14:32:58 +01006266 if (bss_conf->use_short_preamble)
Tomas Winkler220173b2007-10-16 00:50:25 +02006267 priv->staging_rxon.flags |= RXON_FLG_SHORT_PREAMBLE_MSK;
6268 else
6269 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_PREAMBLE_MSK;
6270 }
6271
Johannes Berg471b3ef2007-12-28 14:32:58 +01006272 if (changes & BSS_CHANGED_ERP_CTS_PROT) {
Tomas Winkler3109ece2008-03-28 16:33:35 -07006273 IWL_DEBUG_MAC80211("ERP_CTS %d\n", bss_conf->use_cts_prot);
Johannes Berg8318d782008-01-24 19:38:38 +01006274 if (bss_conf->use_cts_prot && (priv->band != IEEE80211_BAND_5GHZ))
Tomas Winkler220173b2007-10-16 00:50:25 +02006275 priv->staging_rxon.flags |= RXON_FLG_TGG_PROTECT_MSK;
6276 else
6277 priv->staging_rxon.flags &= ~RXON_FLG_TGG_PROTECT_MSK;
6278 }
6279
Tomas Winkler98952d52008-03-28 16:33:33 -07006280 if (changes & BSS_CHANGED_HT) {
Tomas Winkler3109ece2008-03-28 16:33:35 -07006281 IWL_DEBUG_MAC80211("HT %d\n", bss_conf->assoc_ht);
Tomas Winkler98952d52008-03-28 16:33:33 -07006282 iwl4965_ht_conf(priv, bss_conf);
Ron Rindjunskyc7de35c2008-04-23 17:15:05 -07006283 iwl_set_rxon_chain(priv);
Tomas Winkler98952d52008-03-28 16:33:33 -07006284 }
6285
Johannes Berg471b3ef2007-12-28 14:32:58 +01006286 if (changes & BSS_CHANGED_ASSOC) {
Tomas Winkler3109ece2008-03-28 16:33:35 -07006287 IWL_DEBUG_MAC80211("ASSOC %d\n", bss_conf->assoc);
Reinette Chatre508e32e2008-04-14 21:16:13 -07006288 /* This should never happen as this function should
6289 * never be called from interrupt context. */
6290 if (WARN_ON_ONCE(in_interrupt()))
6291 return;
Tomas Winkler3109ece2008-03-28 16:33:35 -07006292 if (bss_conf->assoc) {
6293 priv->assoc_id = bss_conf->aid;
6294 priv->beacon_int = bss_conf->beacon_int;
6295 priv->timestamp = bss_conf->timestamp;
6296 priv->assoc_capability = bss_conf->assoc_capability;
6297 priv->next_scan_jiffies = jiffies +
6298 IWL_DELAY_NEXT_SCAN_AFTER_ASSOC;
Reinette Chatre508e32e2008-04-14 21:16:13 -07006299 mutex_lock(&priv->mutex);
6300 iwl4965_post_associate(priv);
6301 mutex_unlock(&priv->mutex);
Tomas Winkler3109ece2008-03-28 16:33:35 -07006302 } else {
6303 priv->assoc_id = 0;
6304 IWL_DEBUG_MAC80211("DISASSOC %d\n", bss_conf->assoc);
6305 }
6306 } else if (changes && iwl_is_associated(priv) && priv->assoc_id) {
6307 IWL_DEBUG_MAC80211("Associated Changes %d\n", changes);
Tomas Winkler7e8c5192008-04-15 16:01:43 -07006308 iwl_send_rxon_assoc(priv);
Johannes Berg471b3ef2007-12-28 14:32:58 +01006309 }
6310
Tomas Winkler220173b2007-10-16 00:50:25 +02006311}
Zhu Yib481de92007-09-25 17:54:57 -07006312
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006313static int iwl4965_mac_hw_scan(struct ieee80211_hw *hw, u8 *ssid, size_t len)
Zhu Yib481de92007-09-25 17:54:57 -07006314{
6315 int rc = 0;
6316 unsigned long flags;
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006317 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07006318
6319 IWL_DEBUG_MAC80211("enter\n");
6320
mabbas052c4b92007-10-25 17:15:43 +08006321 mutex_lock(&priv->mutex);
Zhu Yib481de92007-09-25 17:54:57 -07006322 spin_lock_irqsave(&priv->lock, flags);
6323
Tomas Winklerfee12472008-04-03 16:05:21 -07006324 if (!iwl_is_ready_rf(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07006325 rc = -EIO;
6326 IWL_DEBUG_MAC80211("leave - not ready or exit pending\n");
6327 goto out_unlock;
6328 }
6329
6330 if (priv->iw_mode == IEEE80211_IF_TYPE_AP) { /* APs don't scan */
6331 rc = -EIO;
6332 IWL_ERROR("ERROR: APs don't scan\n");
6333 goto out_unlock;
6334 }
6335
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08006336 /* we don't schedule scan within next_scan_jiffies period */
6337 if (priv->next_scan_jiffies &&
6338 time_after(priv->next_scan_jiffies, jiffies)) {
6339 rc = -EAGAIN;
6340 goto out_unlock;
6341 }
Zhu Yib481de92007-09-25 17:54:57 -07006342 /* if we just finished scan ask for delay */
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08006343 if (priv->last_scan_jiffies && time_after(priv->last_scan_jiffies +
6344 IWL_DELAY_NEXT_SCAN, jiffies)) {
Zhu Yib481de92007-09-25 17:54:57 -07006345 rc = -EAGAIN;
6346 goto out_unlock;
6347 }
6348 if (len) {
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08006349 IWL_DEBUG_SCAN("direct scan for %s [%d]\n ",
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006350 iwl4965_escape_essid(ssid, len), (int)len);
Zhu Yib481de92007-09-25 17:54:57 -07006351
6352 priv->one_direct_scan = 1;
6353 priv->direct_ssid_len = (u8)
6354 min((u8) len, (u8) IW_ESSID_MAX_SIZE);
6355 memcpy(priv->direct_ssid, ssid, priv->direct_ssid_len);
Mohamed Abbas948c1712007-10-25 17:15:45 +08006356 } else
6357 priv->one_direct_scan = 0;
Zhu Yib481de92007-09-25 17:54:57 -07006358
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006359 rc = iwl4965_scan_initiate(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006360
6361 IWL_DEBUG_MAC80211("leave\n");
6362
6363out_unlock:
6364 spin_unlock_irqrestore(&priv->lock, flags);
mabbas052c4b92007-10-25 17:15:43 +08006365 mutex_unlock(&priv->mutex);
Zhu Yib481de92007-09-25 17:54:57 -07006366
6367 return rc;
6368}
6369
Emmanuel Grumbachab885f82008-03-20 15:06:43 +02006370static void iwl4965_mac_update_tkip_key(struct ieee80211_hw *hw,
6371 struct ieee80211_key_conf *keyconf, const u8 *addr,
6372 u32 iv32, u16 *phase1key)
6373{
6374 struct iwl_priv *priv = hw->priv;
6375 u8 sta_id = IWL_INVALID_STATION;
6376 unsigned long flags;
6377 __le16 key_flags = 0;
6378 int i;
6379 DECLARE_MAC_BUF(mac);
6380
6381 IWL_DEBUG_MAC80211("enter\n");
6382
Tomas Winkler947b13a2008-04-16 16:34:48 -07006383 sta_id = iwl_find_station(priv, addr);
Emmanuel Grumbachab885f82008-03-20 15:06:43 +02006384 if (sta_id == IWL_INVALID_STATION) {
6385 IWL_DEBUG_MAC80211("leave - %s not in station map.\n",
6386 print_mac(mac, addr));
6387 return;
6388 }
6389
6390 iwl4965_scan_cancel_timeout(priv, 100);
6391
6392 key_flags |= (STA_KEY_FLG_TKIP | STA_KEY_FLG_MAP_KEY_MSK);
6393 key_flags |= cpu_to_le16(keyconf->keyidx << STA_KEY_FLG_KEYID_POS);
6394 key_flags &= ~STA_KEY_FLG_INVALID;
6395
Tomas Winkler5425e492008-04-15 16:01:38 -07006396 if (sta_id == priv->hw_params.bcast_sta_id)
Emmanuel Grumbachab885f82008-03-20 15:06:43 +02006397 key_flags |= STA_KEY_MULTICAST_MSK;
6398
6399 spin_lock_irqsave(&priv->sta_lock, flags);
6400
Emmanuel Grumbachab885f82008-03-20 15:06:43 +02006401 priv->stations[sta_id].sta.key.key_flags = key_flags;
6402 priv->stations[sta_id].sta.key.tkip_rx_tsc_byte2 = (u8) iv32;
6403
6404 for (i = 0; i < 5; i++)
6405 priv->stations[sta_id].sta.key.tkip_rx_ttak[i] =
6406 cpu_to_le16(phase1key[i]);
6407
6408 priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
6409 priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
6410
6411 iwl4965_send_add_station(priv, &priv->stations[sta_id].sta, CMD_ASYNC);
6412
6413 spin_unlock_irqrestore(&priv->sta_lock, flags);
6414
6415 IWL_DEBUG_MAC80211("leave\n");
6416}
6417
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006418static int iwl4965_mac_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
Zhu Yib481de92007-09-25 17:54:57 -07006419 const u8 *local_addr, const u8 *addr,
6420 struct ieee80211_key_conf *key)
6421{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006422 struct iwl_priv *priv = hw->priv;
Joe Perches0795af52007-10-03 17:59:30 -07006423 DECLARE_MAC_BUF(mac);
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07006424 int ret = 0;
6425 u8 sta_id = IWL_INVALID_STATION;
Emmanuel Grumbach6974e362008-04-14 21:16:06 -07006426 u8 is_default_wep_key = 0;
Zhu Yib481de92007-09-25 17:54:57 -07006427
6428 IWL_DEBUG_MAC80211("enter\n");
6429
Ron Rindjunsky099b40b2008-04-21 15:41:53 -07006430 if (priv->hw_params.sw_crypto) {
Zhu Yib481de92007-09-25 17:54:57 -07006431 IWL_DEBUG_MAC80211("leave - hwcrypto disabled\n");
6432 return -EOPNOTSUPP;
6433 }
6434
6435 if (is_zero_ether_addr(addr))
6436 /* only support pairwise keys */
6437 return -EOPNOTSUPP;
6438
Tomas Winkler947b13a2008-04-16 16:34:48 -07006439 sta_id = iwl_find_station(priv, addr);
Emmanuel Grumbach6974e362008-04-14 21:16:06 -07006440 if (sta_id == IWL_INVALID_STATION) {
6441 IWL_DEBUG_MAC80211("leave - %s not in station map.\n",
6442 print_mac(mac, addr));
6443 return -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07006444
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07006445 }
Zhu Yib481de92007-09-25 17:54:57 -07006446
Emmanuel Grumbach6974e362008-04-14 21:16:06 -07006447 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006448 iwl4965_scan_cancel_timeout(priv, 100);
Emmanuel Grumbach6974e362008-04-14 21:16:06 -07006449 mutex_unlock(&priv->mutex);
6450
6451 /* If we are getting WEP group key and we didn't receive any key mapping
6452 * so far, we are in legacy wep mode (group key only), otherwise we are
6453 * in 1X mode.
6454 * In legacy wep mode, we use another host command to the uCode */
Tomas Winkler5425e492008-04-15 16:01:38 -07006455 if (key->alg == ALG_WEP && sta_id == priv->hw_params.bcast_sta_id &&
Emmanuel Grumbach6974e362008-04-14 21:16:06 -07006456 priv->iw_mode != IEEE80211_IF_TYPE_AP) {
6457 if (cmd == SET_KEY)
6458 is_default_wep_key = !priv->key_mapping_key;
6459 else
6460 is_default_wep_key = priv->default_wep_key;
6461 }
mabbas052c4b92007-10-25 17:15:43 +08006462
Zhu Yib481de92007-09-25 17:54:57 -07006463 switch (cmd) {
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07006464 case SET_KEY:
Emmanuel Grumbach6974e362008-04-14 21:16:06 -07006465 if (is_default_wep_key)
6466 ret = iwl_set_default_wep_key(priv, key);
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07006467 else
Emmanuel Grumbach74805132008-04-14 21:16:09 -07006468 ret = iwl_set_dynamic_key(priv, key, sta_id);
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07006469
6470 IWL_DEBUG_MAC80211("enable hwcrypto key\n");
Zhu Yib481de92007-09-25 17:54:57 -07006471 break;
6472 case DISABLE_KEY:
Emmanuel Grumbach6974e362008-04-14 21:16:06 -07006473 if (is_default_wep_key)
6474 ret = iwl_remove_default_wep_key(priv, key);
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07006475 else
Emmanuel Grumbach3ec47732008-04-17 16:03:36 -07006476 ret = iwl_remove_dynamic_key(priv, key, sta_id);
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07006477
6478 IWL_DEBUG_MAC80211("disable hwcrypto key\n");
Zhu Yib481de92007-09-25 17:54:57 -07006479 break;
6480 default:
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07006481 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07006482 }
6483
6484 IWL_DEBUG_MAC80211("leave\n");
Zhu Yib481de92007-09-25 17:54:57 -07006485
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07006486 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07006487}
6488
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006489static int iwl4965_mac_conf_tx(struct ieee80211_hw *hw, int queue,
Zhu Yib481de92007-09-25 17:54:57 -07006490 const struct ieee80211_tx_queue_params *params)
6491{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006492 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07006493 unsigned long flags;
6494 int q;
Zhu Yib481de92007-09-25 17:54:57 -07006495
6496 IWL_DEBUG_MAC80211("enter\n");
6497
Tomas Winklerfee12472008-04-03 16:05:21 -07006498 if (!iwl_is_ready_rf(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07006499 IWL_DEBUG_MAC80211("leave - RF not ready\n");
6500 return -EIO;
6501 }
6502
6503 if (queue >= AC_NUM) {
6504 IWL_DEBUG_MAC80211("leave - queue >= AC_NUM %d\n", queue);
6505 return 0;
6506 }
6507
Zhu Yib481de92007-09-25 17:54:57 -07006508 if (!priv->qos_data.qos_enable) {
6509 priv->qos_data.qos_active = 0;
6510 IWL_DEBUG_MAC80211("leave - qos not enabled\n");
6511 return 0;
6512 }
6513 q = AC_NUM - 1 - queue;
6514
6515 spin_lock_irqsave(&priv->lock, flags);
6516
6517 priv->qos_data.def_qos_parm.ac[q].cw_min = cpu_to_le16(params->cw_min);
6518 priv->qos_data.def_qos_parm.ac[q].cw_max = cpu_to_le16(params->cw_max);
6519 priv->qos_data.def_qos_parm.ac[q].aifsn = params->aifs;
6520 priv->qos_data.def_qos_parm.ac[q].edca_txop =
Johannes Berg3330d7b2008-02-10 16:49:38 +01006521 cpu_to_le16((params->txop * 32));
Zhu Yib481de92007-09-25 17:54:57 -07006522
6523 priv->qos_data.def_qos_parm.ac[q].reserved1 = 0;
6524 priv->qos_data.qos_active = 1;
6525
6526 spin_unlock_irqrestore(&priv->lock, flags);
6527
6528 mutex_lock(&priv->mutex);
6529 if (priv->iw_mode == IEEE80211_IF_TYPE_AP)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006530 iwl4965_activate_qos(priv, 1);
Tomas Winkler3109ece2008-03-28 16:33:35 -07006531 else if (priv->assoc_id && iwl_is_associated(priv))
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006532 iwl4965_activate_qos(priv, 0);
Zhu Yib481de92007-09-25 17:54:57 -07006533
6534 mutex_unlock(&priv->mutex);
6535
Zhu Yib481de92007-09-25 17:54:57 -07006536 IWL_DEBUG_MAC80211("leave\n");
6537 return 0;
6538}
6539
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006540static int iwl4965_mac_get_tx_stats(struct ieee80211_hw *hw,
Zhu Yib481de92007-09-25 17:54:57 -07006541 struct ieee80211_tx_queue_stats *stats)
6542{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006543 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07006544 int i, avail;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006545 struct iwl4965_tx_queue *txq;
6546 struct iwl4965_queue *q;
Zhu Yib481de92007-09-25 17:54:57 -07006547 unsigned long flags;
6548
6549 IWL_DEBUG_MAC80211("enter\n");
6550
Tomas Winklerfee12472008-04-03 16:05:21 -07006551 if (!iwl_is_ready_rf(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07006552 IWL_DEBUG_MAC80211("leave - RF not ready\n");
6553 return -EIO;
6554 }
6555
6556 spin_lock_irqsave(&priv->lock, flags);
6557
6558 for (i = 0; i < AC_NUM; i++) {
6559 txq = &priv->txq[i];
6560 q = &txq->q;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006561 avail = iwl4965_queue_space(q);
Zhu Yib481de92007-09-25 17:54:57 -07006562
6563 stats->data[i].len = q->n_window - avail;
6564 stats->data[i].limit = q->n_window - q->high_mark;
6565 stats->data[i].count = q->n_window;
6566
6567 }
6568 spin_unlock_irqrestore(&priv->lock, flags);
6569
6570 IWL_DEBUG_MAC80211("leave\n");
6571
6572 return 0;
6573}
6574
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006575static int iwl4965_mac_get_stats(struct ieee80211_hw *hw,
Zhu Yib481de92007-09-25 17:54:57 -07006576 struct ieee80211_low_level_stats *stats)
6577{
6578 IWL_DEBUG_MAC80211("enter\n");
6579 IWL_DEBUG_MAC80211("leave\n");
6580
6581 return 0;
6582}
6583
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006584static u64 iwl4965_mac_get_tsf(struct ieee80211_hw *hw)
Zhu Yib481de92007-09-25 17:54:57 -07006585{
6586 IWL_DEBUG_MAC80211("enter\n");
6587 IWL_DEBUG_MAC80211("leave\n");
6588
6589 return 0;
6590}
6591
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006592static void iwl4965_mac_reset_tsf(struct ieee80211_hw *hw)
Zhu Yib481de92007-09-25 17:54:57 -07006593{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006594 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07006595 unsigned long flags;
6596
6597 mutex_lock(&priv->mutex);
6598 IWL_DEBUG_MAC80211("enter\n");
6599
6600 priv->lq_mngr.lq_ready = 0;
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08006601#ifdef CONFIG_IWL4965_HT
Zhu Yib481de92007-09-25 17:54:57 -07006602 spin_lock_irqsave(&priv->lock, flags);
Ron Rindjunskyfd105e72007-11-26 16:14:39 +02006603 memset(&priv->current_ht_config, 0, sizeof(struct iwl_ht_info));
Zhu Yib481de92007-09-25 17:54:57 -07006604 spin_unlock_irqrestore(&priv->lock, flags);
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08006605#endif /* CONFIG_IWL4965_HT */
Zhu Yib481de92007-09-25 17:54:57 -07006606
Ron Rindjunskyc7de35c2008-04-23 17:15:05 -07006607 iwl_reset_qos(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006608
6609 cancel_delayed_work(&priv->post_associate);
6610
6611 spin_lock_irqsave(&priv->lock, flags);
6612 priv->assoc_id = 0;
6613 priv->assoc_capability = 0;
Zhu Yib481de92007-09-25 17:54:57 -07006614 priv->assoc_station_added = 0;
6615
6616 /* new association get rid of ibss beacon skb */
6617 if (priv->ibss_beacon)
6618 dev_kfree_skb(priv->ibss_beacon);
6619
6620 priv->ibss_beacon = NULL;
6621
6622 priv->beacon_int = priv->hw->conf.beacon_int;
Tomas Winkler3109ece2008-03-28 16:33:35 -07006623 priv->timestamp = 0;
Zhu Yib481de92007-09-25 17:54:57 -07006624 if ((priv->iw_mode == IEEE80211_IF_TYPE_STA))
6625 priv->beacon_int = 0;
6626
6627 spin_unlock_irqrestore(&priv->lock, flags);
6628
Tomas Winklerfee12472008-04-03 16:05:21 -07006629 if (!iwl_is_ready_rf(priv)) {
Mohamed Abbasfde35712007-11-29 11:10:15 +08006630 IWL_DEBUG_MAC80211("leave - not ready\n");
6631 mutex_unlock(&priv->mutex);
6632 return;
6633 }
6634
mabbas052c4b92007-10-25 17:15:43 +08006635 /* we are restarting association process
6636 * clear RXON_FILTER_ASSOC_MSK bit
6637 */
6638 if (priv->iw_mode != IEEE80211_IF_TYPE_AP) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006639 iwl4965_scan_cancel_timeout(priv, 100);
mabbas052c4b92007-10-25 17:15:43 +08006640 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006641 iwl4965_commit_rxon(priv);
mabbas052c4b92007-10-25 17:15:43 +08006642 }
6643
Mohamed Abbas5da4b552008-04-21 15:41:51 -07006644 iwl_power_update_mode(priv, 0);
6645
Zhu Yib481de92007-09-25 17:54:57 -07006646 /* Per mac80211.h: This is only used in IBSS mode... */
6647 if (priv->iw_mode != IEEE80211_IF_TYPE_IBSS) {
mabbas052c4b92007-10-25 17:15:43 +08006648
Zhu Yib481de92007-09-25 17:54:57 -07006649 IWL_DEBUG_MAC80211("leave - not in IBSS\n");
6650 mutex_unlock(&priv->mutex);
6651 return;
6652 }
6653
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006654 iwl4965_set_rate(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006655
6656 mutex_unlock(&priv->mutex);
6657
6658 IWL_DEBUG_MAC80211("leave\n");
Zhu Yib481de92007-09-25 17:54:57 -07006659}
6660
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006661static int iwl4965_mac_beacon_update(struct ieee80211_hw *hw, struct sk_buff *skb,
Zhu Yib481de92007-09-25 17:54:57 -07006662 struct ieee80211_tx_control *control)
6663{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006664 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07006665 unsigned long flags;
6666
6667 mutex_lock(&priv->mutex);
6668 IWL_DEBUG_MAC80211("enter\n");
6669
Tomas Winklerfee12472008-04-03 16:05:21 -07006670 if (!iwl_is_ready_rf(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07006671 IWL_DEBUG_MAC80211("leave - RF not ready\n");
6672 mutex_unlock(&priv->mutex);
6673 return -EIO;
6674 }
6675
6676 if (priv->iw_mode != IEEE80211_IF_TYPE_IBSS) {
6677 IWL_DEBUG_MAC80211("leave - not IBSS\n");
6678 mutex_unlock(&priv->mutex);
6679 return -EIO;
6680 }
6681
6682 spin_lock_irqsave(&priv->lock, flags);
6683
6684 if (priv->ibss_beacon)
6685 dev_kfree_skb(priv->ibss_beacon);
6686
6687 priv->ibss_beacon = skb;
6688
6689 priv->assoc_id = 0;
6690
6691 IWL_DEBUG_MAC80211("leave\n");
6692 spin_unlock_irqrestore(&priv->lock, flags);
6693
Ron Rindjunskyc7de35c2008-04-23 17:15:05 -07006694 iwl_reset_qos(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006695
6696 queue_work(priv->workqueue, &priv->post_associate.work);
6697
6698 mutex_unlock(&priv->mutex);
6699
6700 return 0;
6701}
6702
Zhu Yib481de92007-09-25 17:54:57 -07006703/*****************************************************************************
6704 *
6705 * sysfs attributes
6706 *
6707 *****************************************************************************/
6708
Tomas Winkler0a6857e2008-03-12 16:58:49 -07006709#ifdef CONFIG_IWLWIFI_DEBUG
Zhu Yib481de92007-09-25 17:54:57 -07006710
6711/*
6712 * The following adds a new attribute to the sysfs representation
6713 * of this device driver (i.e. a new file in /sys/bus/pci/drivers/iwl/)
6714 * used for controlling the debug level.
6715 *
6716 * See the level definitions in iwl for details.
6717 */
6718
6719static ssize_t show_debug_level(struct device_driver *d, char *buf)
6720{
Tomas Winkler0a6857e2008-03-12 16:58:49 -07006721 return sprintf(buf, "0x%08X\n", iwl_debug_level);
Zhu Yib481de92007-09-25 17:54:57 -07006722}
6723static ssize_t store_debug_level(struct device_driver *d,
6724 const char *buf, size_t count)
6725{
6726 char *p = (char *)buf;
6727 u32 val;
6728
6729 val = simple_strtoul(p, &p, 0);
6730 if (p == buf)
6731 printk(KERN_INFO DRV_NAME
6732 ": %s is not in hex or decimal form.\n", buf);
6733 else
Tomas Winkler0a6857e2008-03-12 16:58:49 -07006734 iwl_debug_level = val;
Zhu Yib481de92007-09-25 17:54:57 -07006735
6736 return strnlen(buf, count);
6737}
6738
6739static DRIVER_ATTR(debug_level, S_IWUSR | S_IRUGO,
6740 show_debug_level, store_debug_level);
6741
Tomas Winkler0a6857e2008-03-12 16:58:49 -07006742#endif /* CONFIG_IWLWIFI_DEBUG */
Zhu Yib481de92007-09-25 17:54:57 -07006743
Zhu Yib481de92007-09-25 17:54:57 -07006744
6745static ssize_t show_temperature(struct device *d,
6746 struct device_attribute *attr, char *buf)
6747{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006748 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07006749
Tomas Winklerfee12472008-04-03 16:05:21 -07006750 if (!iwl_is_alive(priv))
Zhu Yib481de92007-09-25 17:54:57 -07006751 return -EAGAIN;
6752
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006753 return sprintf(buf, "%d\n", iwl4965_hw_get_temperature(priv));
Zhu Yib481de92007-09-25 17:54:57 -07006754}
6755
6756static DEVICE_ATTR(temperature, S_IRUGO, show_temperature, NULL);
6757
6758static ssize_t show_rs_window(struct device *d,
6759 struct device_attribute *attr,
6760 char *buf)
6761{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006762 struct iwl_priv *priv = d->driver_data;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006763 return iwl4965_fill_rs_info(priv->hw, buf, IWL_AP_ID);
Zhu Yib481de92007-09-25 17:54:57 -07006764}
6765static DEVICE_ATTR(rs_window, S_IRUGO, show_rs_window, NULL);
6766
6767static ssize_t show_tx_power(struct device *d,
6768 struct device_attribute *attr, char *buf)
6769{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006770 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07006771 return sprintf(buf, "%d\n", priv->user_txpower_limit);
6772}
6773
6774static ssize_t store_tx_power(struct device *d,
6775 struct device_attribute *attr,
6776 const char *buf, size_t count)
6777{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006778 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07006779 char *p = (char *)buf;
6780 u32 val;
6781
6782 val = simple_strtoul(p, &p, 10);
6783 if (p == buf)
6784 printk(KERN_INFO DRV_NAME
6785 ": %s is not in decimal form.\n", buf);
6786 else
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006787 iwl4965_hw_reg_set_txpower(priv, val);
Zhu Yib481de92007-09-25 17:54:57 -07006788
6789 return count;
6790}
6791
6792static DEVICE_ATTR(tx_power, S_IWUSR | S_IRUGO, show_tx_power, store_tx_power);
6793
6794static ssize_t show_flags(struct device *d,
6795 struct device_attribute *attr, char *buf)
6796{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006797 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07006798
6799 return sprintf(buf, "0x%04X\n", priv->active_rxon.flags);
6800}
6801
6802static ssize_t store_flags(struct device *d,
6803 struct device_attribute *attr,
6804 const char *buf, size_t count)
6805{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006806 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07006807 u32 flags = simple_strtoul(buf, NULL, 0);
6808
6809 mutex_lock(&priv->mutex);
6810 if (le32_to_cpu(priv->staging_rxon.flags) != flags) {
6811 /* Cancel any currently running scans... */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006812 if (iwl4965_scan_cancel_timeout(priv, 100))
Zhu Yib481de92007-09-25 17:54:57 -07006813 IWL_WARNING("Could not cancel scan.\n");
6814 else {
6815 IWL_DEBUG_INFO("Committing rxon.flags = 0x%04X\n",
6816 flags);
6817 priv->staging_rxon.flags = cpu_to_le32(flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006818 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006819 }
6820 }
6821 mutex_unlock(&priv->mutex);
6822
6823 return count;
6824}
6825
6826static DEVICE_ATTR(flags, S_IWUSR | S_IRUGO, show_flags, store_flags);
6827
6828static ssize_t show_filter_flags(struct device *d,
6829 struct device_attribute *attr, char *buf)
6830{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006831 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07006832
6833 return sprintf(buf, "0x%04X\n",
6834 le32_to_cpu(priv->active_rxon.filter_flags));
6835}
6836
6837static ssize_t store_filter_flags(struct device *d,
6838 struct device_attribute *attr,
6839 const char *buf, size_t count)
6840{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006841 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07006842 u32 filter_flags = simple_strtoul(buf, NULL, 0);
6843
6844 mutex_lock(&priv->mutex);
6845 if (le32_to_cpu(priv->staging_rxon.filter_flags) != filter_flags) {
6846 /* Cancel any currently running scans... */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006847 if (iwl4965_scan_cancel_timeout(priv, 100))
Zhu Yib481de92007-09-25 17:54:57 -07006848 IWL_WARNING("Could not cancel scan.\n");
6849 else {
6850 IWL_DEBUG_INFO("Committing rxon.filter_flags = "
6851 "0x%04X\n", filter_flags);
6852 priv->staging_rxon.filter_flags =
6853 cpu_to_le32(filter_flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006854 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006855 }
6856 }
6857 mutex_unlock(&priv->mutex);
6858
6859 return count;
6860}
6861
6862static DEVICE_ATTR(filter_flags, S_IWUSR | S_IRUGO, show_filter_flags,
6863 store_filter_flags);
6864
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08006865#ifdef CONFIG_IWL4965_SPECTRUM_MEASUREMENT
Zhu Yib481de92007-09-25 17:54:57 -07006866
6867static ssize_t show_measurement(struct device *d,
6868 struct device_attribute *attr, char *buf)
6869{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006870 struct iwl_priv *priv = dev_get_drvdata(d);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006871 struct iwl4965_spectrum_notification measure_report;
Zhu Yib481de92007-09-25 17:54:57 -07006872 u32 size = sizeof(measure_report), len = 0, ofs = 0;
6873 u8 *data = (u8 *) & measure_report;
6874 unsigned long flags;
6875
6876 spin_lock_irqsave(&priv->lock, flags);
6877 if (!(priv->measurement_status & MEASUREMENT_READY)) {
6878 spin_unlock_irqrestore(&priv->lock, flags);
6879 return 0;
6880 }
6881 memcpy(&measure_report, &priv->measure_report, size);
6882 priv->measurement_status = 0;
6883 spin_unlock_irqrestore(&priv->lock, flags);
6884
6885 while (size && (PAGE_SIZE - len)) {
6886 hex_dump_to_buffer(data + ofs, size, 16, 1, buf + len,
6887 PAGE_SIZE - len, 1);
6888 len = strlen(buf);
6889 if (PAGE_SIZE - len)
6890 buf[len++] = '\n';
6891
6892 ofs += 16;
6893 size -= min(size, 16U);
6894 }
6895
6896 return len;
6897}
6898
6899static ssize_t store_measurement(struct device *d,
6900 struct device_attribute *attr,
6901 const char *buf, size_t count)
6902{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006903 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07006904 struct ieee80211_measurement_params params = {
6905 .channel = le16_to_cpu(priv->active_rxon.channel),
6906 .start_time = cpu_to_le64(priv->last_tsf),
6907 .duration = cpu_to_le16(1),
6908 };
6909 u8 type = IWL_MEASURE_BASIC;
6910 u8 buffer[32];
6911 u8 channel;
6912
6913 if (count) {
6914 char *p = buffer;
6915 strncpy(buffer, buf, min(sizeof(buffer), count));
6916 channel = simple_strtoul(p, NULL, 0);
6917 if (channel)
6918 params.channel = channel;
6919
6920 p = buffer;
6921 while (*p && *p != ' ')
6922 p++;
6923 if (*p)
6924 type = simple_strtoul(p + 1, NULL, 0);
6925 }
6926
6927 IWL_DEBUG_INFO("Invoking measurement of type %d on "
6928 "channel %d (for '%s')\n", type, params.channel, buf);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006929 iwl4965_get_measurement(priv, &params, type);
Zhu Yib481de92007-09-25 17:54:57 -07006930
6931 return count;
6932}
6933
6934static DEVICE_ATTR(measurement, S_IRUSR | S_IWUSR,
6935 show_measurement, store_measurement);
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08006936#endif /* CONFIG_IWL4965_SPECTRUM_MEASUREMENT */
Zhu Yib481de92007-09-25 17:54:57 -07006937
6938static ssize_t store_retry_rate(struct device *d,
6939 struct device_attribute *attr,
6940 const char *buf, size_t count)
6941{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006942 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07006943
6944 priv->retry_rate = simple_strtoul(buf, NULL, 0);
6945 if (priv->retry_rate <= 0)
6946 priv->retry_rate = 1;
6947
6948 return count;
6949}
6950
6951static ssize_t show_retry_rate(struct device *d,
6952 struct device_attribute *attr, char *buf)
6953{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006954 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07006955 return sprintf(buf, "%d", priv->retry_rate);
6956}
6957
6958static DEVICE_ATTR(retry_rate, S_IWUSR | S_IRUSR, show_retry_rate,
6959 store_retry_rate);
6960
6961static ssize_t store_power_level(struct device *d,
6962 struct device_attribute *attr,
6963 const char *buf, size_t count)
6964{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006965 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07006966 int rc;
6967 int mode;
6968
6969 mode = simple_strtoul(buf, NULL, 0);
6970 mutex_lock(&priv->mutex);
6971
Tomas Winklerfee12472008-04-03 16:05:21 -07006972 if (!iwl_is_ready(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07006973 rc = -EAGAIN;
6974 goto out;
6975 }
6976
Mohamed Abbas5da4b552008-04-21 15:41:51 -07006977 rc = iwl_power_set_user_mode(priv, mode);
6978 if (rc) {
6979 IWL_DEBUG_MAC80211("failed setting power mode.\n");
6980 goto out;
Zhu Yib481de92007-09-25 17:54:57 -07006981 }
Zhu Yib481de92007-09-25 17:54:57 -07006982 rc = count;
6983
6984 out:
6985 mutex_unlock(&priv->mutex);
6986 return rc;
6987}
6988
6989#define MAX_WX_STRING 80
6990
6991/* Values are in microsecond */
6992static const s32 timeout_duration[] = {
6993 350000,
6994 250000,
6995 75000,
6996 37000,
6997 25000,
6998};
6999static const s32 period_duration[] = {
7000 400000,
7001 700000,
7002 1000000,
7003 1000000,
7004 1000000
7005};
7006
7007static ssize_t show_power_level(struct device *d,
7008 struct device_attribute *attr, char *buf)
7009{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007010 struct iwl_priv *priv = dev_get_drvdata(d);
Mohamed Abbas5da4b552008-04-21 15:41:51 -07007011 int level = priv->power_data.power_mode;
Zhu Yib481de92007-09-25 17:54:57 -07007012 char *p = buf;
7013
7014 p += sprintf(p, "%d ", level);
7015 switch (level) {
7016 case IWL_POWER_MODE_CAM:
7017 case IWL_POWER_AC:
7018 p += sprintf(p, "(AC)");
7019 break;
7020 case IWL_POWER_BATTERY:
7021 p += sprintf(p, "(BATTERY)");
7022 break;
7023 default:
7024 p += sprintf(p,
7025 "(Timeout %dms, Period %dms)",
7026 timeout_duration[level - 1] / 1000,
7027 period_duration[level - 1] / 1000);
7028 }
Mohamed Abbas5da4b552008-04-21 15:41:51 -07007029/*
Zhu Yib481de92007-09-25 17:54:57 -07007030 if (!(priv->power_mode & IWL_POWER_ENABLED))
7031 p += sprintf(p, " OFF\n");
7032 else
7033 p += sprintf(p, " \n");
Mohamed Abbas5da4b552008-04-21 15:41:51 -07007034*/
7035 p += sprintf(p, " \n");
Zhu Yib481de92007-09-25 17:54:57 -07007036 return (p - buf + 1);
Zhu Yib481de92007-09-25 17:54:57 -07007037}
7038
7039static DEVICE_ATTR(power_level, S_IWUSR | S_IRUSR, show_power_level,
7040 store_power_level);
7041
7042static ssize_t show_channels(struct device *d,
7043 struct device_attribute *attr, char *buf)
7044{
Johannes Berg8318d782008-01-24 19:38:38 +01007045 /* all this shit doesn't belong into sysfs anyway */
7046 return 0;
Zhu Yib481de92007-09-25 17:54:57 -07007047}
7048
7049static DEVICE_ATTR(channels, S_IRUSR, show_channels, NULL);
7050
7051static ssize_t show_statistics(struct device *d,
7052 struct device_attribute *attr, char *buf)
7053{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007054 struct iwl_priv *priv = dev_get_drvdata(d);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007055 u32 size = sizeof(struct iwl4965_notif_statistics);
Zhu Yib481de92007-09-25 17:54:57 -07007056 u32 len = 0, ofs = 0;
7057 u8 *data = (u8 *) & priv->statistics;
7058 int rc = 0;
7059
Tomas Winklerfee12472008-04-03 16:05:21 -07007060 if (!iwl_is_alive(priv))
Zhu Yib481de92007-09-25 17:54:57 -07007061 return -EAGAIN;
7062
7063 mutex_lock(&priv->mutex);
Emmanuel Grumbach49ea8592008-04-15 16:01:37 -07007064 rc = iwl_send_statistics_request(priv, 0);
Zhu Yib481de92007-09-25 17:54:57 -07007065 mutex_unlock(&priv->mutex);
7066
7067 if (rc) {
7068 len = sprintf(buf,
7069 "Error sending statistics request: 0x%08X\n", rc);
7070 return len;
7071 }
7072
7073 while (size && (PAGE_SIZE - len)) {
7074 hex_dump_to_buffer(data + ofs, size, 16, 1, buf + len,
7075 PAGE_SIZE - len, 1);
7076 len = strlen(buf);
7077 if (PAGE_SIZE - len)
7078 buf[len++] = '\n';
7079
7080 ofs += 16;
7081 size -= min(size, 16U);
7082 }
7083
7084 return len;
7085}
7086
7087static DEVICE_ATTR(statistics, S_IRUGO, show_statistics, NULL);
7088
Zhu Yib481de92007-09-25 17:54:57 -07007089static ssize_t show_status(struct device *d,
7090 struct device_attribute *attr, char *buf)
7091{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007092 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Tomas Winklerfee12472008-04-03 16:05:21 -07007093 if (!iwl_is_alive(priv))
Zhu Yib481de92007-09-25 17:54:57 -07007094 return -EAGAIN;
7095 return sprintf(buf, "0x%08x\n", (int)priv->status);
7096}
7097
7098static DEVICE_ATTR(status, S_IRUGO, show_status, NULL);
7099
7100static ssize_t dump_error_log(struct device *d,
7101 struct device_attribute *attr,
7102 const char *buf, size_t count)
7103{
7104 char *p = (char *)buf;
7105
7106 if (p[0] == '1')
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007107 iwl4965_dump_nic_error_log((struct iwl_priv *)d->driver_data);
Zhu Yib481de92007-09-25 17:54:57 -07007108
7109 return strnlen(buf, count);
7110}
7111
7112static DEVICE_ATTR(dump_errors, S_IWUSR, NULL, dump_error_log);
7113
7114static ssize_t dump_event_log(struct device *d,
7115 struct device_attribute *attr,
7116 const char *buf, size_t count)
7117{
7118 char *p = (char *)buf;
7119
7120 if (p[0] == '1')
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007121 iwl4965_dump_nic_event_log((struct iwl_priv *)d->driver_data);
Zhu Yib481de92007-09-25 17:54:57 -07007122
7123 return strnlen(buf, count);
7124}
7125
7126static DEVICE_ATTR(dump_events, S_IWUSR, NULL, dump_event_log);
7127
7128/*****************************************************************************
7129 *
7130 * driver setup and teardown
7131 *
7132 *****************************************************************************/
7133
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007134static void iwl4965_setup_deferred_work(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07007135{
7136 priv->workqueue = create_workqueue(DRV_NAME);
7137
7138 init_waitqueue_head(&priv->wait_command_queue);
7139
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007140 INIT_WORK(&priv->up, iwl4965_bg_up);
7141 INIT_WORK(&priv->restart, iwl4965_bg_restart);
7142 INIT_WORK(&priv->rx_replenish, iwl4965_bg_rx_replenish);
7143 INIT_WORK(&priv->scan_completed, iwl4965_bg_scan_completed);
7144 INIT_WORK(&priv->request_scan, iwl4965_bg_request_scan);
7145 INIT_WORK(&priv->abort_scan, iwl4965_bg_abort_scan);
7146 INIT_WORK(&priv->rf_kill, iwl4965_bg_rf_kill);
7147 INIT_WORK(&priv->beacon_update, iwl4965_bg_beacon_update);
7148 INIT_DELAYED_WORK(&priv->post_associate, iwl4965_bg_post_associate);
7149 INIT_DELAYED_WORK(&priv->init_alive_start, iwl4965_bg_init_alive_start);
7150 INIT_DELAYED_WORK(&priv->alive_start, iwl4965_bg_alive_start);
7151 INIT_DELAYED_WORK(&priv->scan_check, iwl4965_bg_scan_check);
Zhu Yib481de92007-09-25 17:54:57 -07007152
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007153 iwl4965_hw_setup_deferred_work(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007154
7155 tasklet_init(&priv->irq_tasklet, (void (*)(unsigned long))
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007156 iwl4965_irq_tasklet, (unsigned long)priv);
Zhu Yib481de92007-09-25 17:54:57 -07007157}
7158
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007159static void iwl4965_cancel_deferred_work(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07007160{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007161 iwl4965_hw_cancel_deferred_work(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007162
Joonwoo Park3ae6a052007-11-29 10:43:16 +09007163 cancel_delayed_work_sync(&priv->init_alive_start);
Zhu Yib481de92007-09-25 17:54:57 -07007164 cancel_delayed_work(&priv->scan_check);
7165 cancel_delayed_work(&priv->alive_start);
7166 cancel_delayed_work(&priv->post_associate);
7167 cancel_work_sync(&priv->beacon_update);
7168}
7169
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007170static struct attribute *iwl4965_sysfs_entries[] = {
Zhu Yib481de92007-09-25 17:54:57 -07007171 &dev_attr_channels.attr,
7172 &dev_attr_dump_errors.attr,
7173 &dev_attr_dump_events.attr,
7174 &dev_attr_flags.attr,
7175 &dev_attr_filter_flags.attr,
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08007176#ifdef CONFIG_IWL4965_SPECTRUM_MEASUREMENT
Zhu Yib481de92007-09-25 17:54:57 -07007177 &dev_attr_measurement.attr,
7178#endif
7179 &dev_attr_power_level.attr,
7180 &dev_attr_retry_rate.attr,
Zhu Yib481de92007-09-25 17:54:57 -07007181 &dev_attr_rs_window.attr,
7182 &dev_attr_statistics.attr,
7183 &dev_attr_status.attr,
7184 &dev_attr_temperature.attr,
Zhu Yib481de92007-09-25 17:54:57 -07007185 &dev_attr_tx_power.attr,
7186
7187 NULL
7188};
7189
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007190static struct attribute_group iwl4965_attribute_group = {
Zhu Yib481de92007-09-25 17:54:57 -07007191 .name = NULL, /* put in device directory */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007192 .attrs = iwl4965_sysfs_entries,
Zhu Yib481de92007-09-25 17:54:57 -07007193};
7194
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007195static struct ieee80211_ops iwl4965_hw_ops = {
7196 .tx = iwl4965_mac_tx,
7197 .start = iwl4965_mac_start,
7198 .stop = iwl4965_mac_stop,
7199 .add_interface = iwl4965_mac_add_interface,
7200 .remove_interface = iwl4965_mac_remove_interface,
7201 .config = iwl4965_mac_config,
7202 .config_interface = iwl4965_mac_config_interface,
7203 .configure_filter = iwl4965_configure_filter,
7204 .set_key = iwl4965_mac_set_key,
Emmanuel Grumbachab885f82008-03-20 15:06:43 +02007205 .update_tkip_key = iwl4965_mac_update_tkip_key,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007206 .get_stats = iwl4965_mac_get_stats,
7207 .get_tx_stats = iwl4965_mac_get_tx_stats,
7208 .conf_tx = iwl4965_mac_conf_tx,
7209 .get_tsf = iwl4965_mac_get_tsf,
7210 .reset_tsf = iwl4965_mac_reset_tsf,
7211 .beacon_update = iwl4965_mac_beacon_update,
Johannes Berg471b3ef2007-12-28 14:32:58 +01007212 .bss_info_changed = iwl4965_bss_info_changed,
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08007213#ifdef CONFIG_IWL4965_HT
Ron Rindjunsky9ab46172007-12-25 17:00:38 +02007214 .ampdu_action = iwl4965_mac_ampdu_action,
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08007215#endif /* CONFIG_IWL4965_HT */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007216 .hw_scan = iwl4965_mac_hw_scan
Zhu Yib481de92007-09-25 17:54:57 -07007217};
7218
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007219static int iwl4965_pci_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
Zhu Yib481de92007-09-25 17:54:57 -07007220{
7221 int err = 0;
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007222 struct iwl_priv *priv;
Zhu Yib481de92007-09-25 17:54:57 -07007223 struct ieee80211_hw *hw;
Tomas Winkler82b9a122008-03-04 18:09:30 -08007224 struct iwl_cfg *cfg = (struct iwl_cfg *)(ent->driver_data);
Mohamed Abbas0359fac2008-03-28 16:21:08 -07007225 unsigned long flags;
Zhu Yi5a66926a2008-01-14 17:46:18 -08007226 DECLARE_MAC_BUF(mac);
Zhu Yib481de92007-09-25 17:54:57 -07007227
Assaf Krauss316c30d2008-03-14 10:38:46 -07007228 /************************
7229 * 1. Allocating HW data
7230 ************************/
7231
Cahill, Ben M6440adb2007-11-29 11:09:55 +08007232 /* Disabling hardware scan means that mac80211 will perform scans
7233 * "the hard way", rather than using device's scan. */
Assaf Krauss1ea87392008-03-18 14:57:50 -07007234 if (cfg->mod_params->disable_hw_scan) {
Zhu Yib481de92007-09-25 17:54:57 -07007235 IWL_DEBUG_INFO("Disabling hw_scan\n");
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007236 iwl4965_hw_ops.hw_scan = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07007237 }
7238
Assaf Krauss1d0a0822008-03-14 10:38:48 -07007239 hw = iwl_alloc_all(cfg, &iwl4965_hw_ops);
7240 if (!hw) {
Zhu Yib481de92007-09-25 17:54:57 -07007241 err = -ENOMEM;
7242 goto out;
7243 }
Assaf Krauss1d0a0822008-03-14 10:38:48 -07007244 priv = hw->priv;
7245 /* At this point both hw and priv are allocated. */
7246
Zhu Yib481de92007-09-25 17:54:57 -07007247 SET_IEEE80211_DEV(hw, &pdev->dev);
7248
7249 IWL_DEBUG_INFO("*** LOAD DRIVER ***\n");
Tomas Winkler82b9a122008-03-04 18:09:30 -08007250 priv->cfg = cfg;
Zhu Yib481de92007-09-25 17:54:57 -07007251 priv->pci_dev = pdev;
Assaf Krauss316c30d2008-03-14 10:38:46 -07007252
Tomas Winkler0a6857e2008-03-12 16:58:49 -07007253#ifdef CONFIG_IWLWIFI_DEBUG
Assaf Krauss1ea87392008-03-18 14:57:50 -07007254 iwl_debug_level = priv->cfg->mod_params->debug;
Zhu Yib481de92007-09-25 17:54:57 -07007255 atomic_set(&priv->restrict_refcnt, 0);
7256#endif
Zhu Yib481de92007-09-25 17:54:57 -07007257
Assaf Krauss316c30d2008-03-14 10:38:46 -07007258 /**************************
7259 * 2. Initializing PCI bus
7260 **************************/
7261 if (pci_enable_device(pdev)) {
7262 err = -ENODEV;
7263 goto out_ieee80211_free_hw;
7264 }
7265
7266 pci_set_master(pdev);
7267
Ron Rindjunskycc2a8ea2008-04-21 15:41:59 -07007268 err = pci_set_dma_mask(pdev, DMA_64BIT_MASK);
Assaf Krauss316c30d2008-03-14 10:38:46 -07007269 if (!err)
Ron Rindjunskycc2a8ea2008-04-21 15:41:59 -07007270 err = pci_set_consistent_dma_mask(pdev, DMA_64BIT_MASK);
7271 if (err) {
7272 err = pci_set_dma_mask(pdev, DMA_32BIT_MASK);
7273 if (!err)
7274 err = pci_set_consistent_dma_mask(pdev, DMA_32BIT_MASK);
7275 /* both attempts failed: */
Assaf Krauss316c30d2008-03-14 10:38:46 -07007276 if (err) {
Ron Rindjunskycc2a8ea2008-04-21 15:41:59 -07007277 printk(KERN_WARNING "%s: No suitable DMA available.\n",
7278 DRV_NAME);
Assaf Krauss316c30d2008-03-14 10:38:46 -07007279 goto out_pci_disable_device;
Ron Rindjunskycc2a8ea2008-04-21 15:41:59 -07007280 }
Assaf Krauss316c30d2008-03-14 10:38:46 -07007281 }
7282
7283 err = pci_request_regions(pdev, DRV_NAME);
7284 if (err)
7285 goto out_pci_disable_device;
7286
7287 pci_set_drvdata(pdev, priv);
7288
7289 /* We disable the RETRY_TIMEOUT register (0x41) to keep
7290 * PCI Tx retries from interfering with C3 CPU state */
7291 pci_write_config_byte(pdev, 0x41, 0x00);
7292
7293 /***********************
7294 * 3. Read REV register
7295 ***********************/
7296 priv->hw_base = pci_iomap(pdev, 0, 0);
7297 if (!priv->hw_base) {
7298 err = -ENODEV;
7299 goto out_pci_release_regions;
7300 }
7301
7302 IWL_DEBUG_INFO("pci_resource_len = 0x%08llx\n",
7303 (unsigned long long) pci_resource_len(pdev, 0));
7304 IWL_DEBUG_INFO("pci_resource_base = %p\n", priv->hw_base);
7305
Tomas Winklerb661c812008-04-23 17:14:54 -07007306 iwl_hw_detect(priv);
Assaf Krauss316c30d2008-03-14 10:38:46 -07007307 printk(KERN_INFO DRV_NAME
Tomas Winklerb661c812008-04-23 17:14:54 -07007308 ": Detected Intel Wireless WiFi Link %s REV=0x%X\n",
7309 priv->cfg->name, priv->hw_rev);
Assaf Krauss316c30d2008-03-14 10:38:46 -07007310
Tomas Winkler91238712008-04-23 17:14:53 -07007311 /* amp init */
7312 err = priv->cfg->ops->lib->apm_ops.init(priv);
7313 if (err < 0) {
7314 IWL_DEBUG_INFO("Failed to init APMG\n");
7315 goto out_iounmap;
7316 }
Assaf Krauss316c30d2008-03-14 10:38:46 -07007317 /*****************
7318 * 4. Read EEPROM
7319 *****************/
Assaf Krauss316c30d2008-03-14 10:38:46 -07007320 /* Read the EEPROM */
7321 err = iwl_eeprom_init(priv);
7322 if (err) {
7323 IWL_ERROR("Unable to init EEPROM\n");
7324 goto out_iounmap;
7325 }
Tomas Winkler8614f362008-04-23 17:14:55 -07007326 err = iwl_eeprom_check_version(priv);
7327 if (err)
7328 goto out_iounmap;
7329
Assaf Krauss316c30d2008-03-14 10:38:46 -07007330 /* MAC Address location in EEPROM same for 3945/4965 */
7331 iwl_eeprom_get_mac(priv, priv->mac_addr);
7332 IWL_DEBUG_INFO("MAC address: %s\n", print_mac(mac, priv->mac_addr));
7333 SET_IEEE80211_PERM_ADDR(priv->hw, priv->mac_addr);
7334
7335 /************************
7336 * 5. Setup HW constants
7337 ************************/
7338 /* Device-specific setup */
Tomas Winkler5425e492008-04-15 16:01:38 -07007339 if (priv->cfg->ops->lib->set_hw_params(priv)) {
7340 IWL_ERROR("failed to set hw parameters\n");
Tomas Winkler073d3f52008-04-21 15:41:52 -07007341 goto out_free_eeprom;
Assaf Krauss316c30d2008-03-14 10:38:46 -07007342 }
7343
7344 /*******************
7345 * 6. Setup hw/priv
7346 *******************/
Zhu Yib481de92007-09-25 17:54:57 -07007347
Assaf Kraussbf85ea42008-03-14 10:38:49 -07007348 err = iwl_setup(priv);
7349 if (err)
Ron Rindjunsky399f4902008-04-23 17:14:56 -07007350 goto out_free_eeprom;
Assaf Kraussbf85ea42008-03-14 10:38:49 -07007351 /* At this point both hw and priv are initialized. */
Assaf Krauss316c30d2008-03-14 10:38:46 -07007352
7353 /**********************************
7354 * 7. Initialize module parameters
7355 **********************************/
7356
7357 /* Disable radio (SW RF KILL) via parameter when loading driver */
Assaf Krauss1ea87392008-03-18 14:57:50 -07007358 if (priv->cfg->mod_params->disable) {
Assaf Krauss316c30d2008-03-14 10:38:46 -07007359 set_bit(STATUS_RF_KILL_SW, &priv->status);
7360 IWL_DEBUG_INFO("Radio disabled.\n");
7361 }
7362
Assaf Krauss1ea87392008-03-18 14:57:50 -07007363 if (priv->cfg->mod_params->enable_qos)
Assaf Krauss316c30d2008-03-14 10:38:46 -07007364 priv->qos_data.qos_enable = 1;
7365
7366 /********************
7367 * 8. Setup services
7368 ********************/
Mohamed Abbas0359fac2008-03-28 16:21:08 -07007369 spin_lock_irqsave(&priv->lock, flags);
Assaf Krauss316c30d2008-03-14 10:38:46 -07007370 iwl4965_disable_interrupts(priv);
Mohamed Abbas0359fac2008-03-28 16:21:08 -07007371 spin_unlock_irqrestore(&priv->lock, flags);
Assaf Krauss316c30d2008-03-14 10:38:46 -07007372
7373 err = sysfs_create_group(&pdev->dev.kobj, &iwl4965_attribute_group);
7374 if (err) {
7375 IWL_ERROR("failed to create sysfs device attributes\n");
Ron Rindjunsky399f4902008-04-23 17:14:56 -07007376 goto out_free_eeprom;
Assaf Krauss316c30d2008-03-14 10:38:46 -07007377 }
7378
7379 err = iwl_dbgfs_register(priv, DRV_NAME);
7380 if (err) {
7381 IWL_ERROR("failed to create debugfs files\n");
7382 goto out_remove_sysfs;
7383 }
7384
7385 iwl4965_setup_deferred_work(priv);
7386 iwl4965_setup_rx_handlers(priv);
7387
7388 /********************
7389 * 9. Conclude
7390 ********************/
Zhu Yi5a66926a2008-01-14 17:46:18 -08007391 pci_save_state(pdev);
7392 pci_disable_device(pdev);
Zhu Yib481de92007-09-25 17:54:57 -07007393
Mohamed Abbasc8381fd2008-03-28 16:21:05 -07007394 /* notify iwlcore to init */
7395 iwlcore_low_level_notify(priv, IWLCORE_INIT_EVT);
Zhu Yib481de92007-09-25 17:54:57 -07007396 return 0;
7397
Assaf Krauss316c30d2008-03-14 10:38:46 -07007398 out_remove_sysfs:
7399 sysfs_remove_group(&pdev->dev.kobj, &iwl4965_attribute_group);
Tomas Winkler073d3f52008-04-21 15:41:52 -07007400 out_free_eeprom:
7401 iwl_eeprom_free(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007402 out_iounmap:
7403 pci_iounmap(pdev, priv->hw_base);
7404 out_pci_release_regions:
7405 pci_release_regions(pdev);
Assaf Krauss316c30d2008-03-14 10:38:46 -07007406 pci_set_drvdata(pdev, NULL);
Zhu Yib481de92007-09-25 17:54:57 -07007407 out_pci_disable_device:
7408 pci_disable_device(pdev);
Zhu Yib481de92007-09-25 17:54:57 -07007409 out_ieee80211_free_hw:
7410 ieee80211_free_hw(priv->hw);
7411 out:
7412 return err;
7413}
7414
Reinette Chatrec83dbf62008-03-21 13:53:41 -07007415static void __devexit iwl4965_pci_remove(struct pci_dev *pdev)
Zhu Yib481de92007-09-25 17:54:57 -07007416{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007417 struct iwl_priv *priv = pci_get_drvdata(pdev);
Zhu Yib481de92007-09-25 17:54:57 -07007418 struct list_head *p, *q;
7419 int i;
Mohamed Abbas0359fac2008-03-28 16:21:08 -07007420 unsigned long flags;
Zhu Yib481de92007-09-25 17:54:57 -07007421
7422 if (!priv)
7423 return;
7424
7425 IWL_DEBUG_INFO("*** UNLOAD DRIVER ***\n");
7426
Ron Rindjunskyc4f55232008-03-28 16:21:10 -07007427 if (priv->mac80211_registered) {
7428 ieee80211_unregister_hw(priv->hw);
7429 priv->mac80211_registered = 0;
7430 }
7431
Zhu Yib481de92007-09-25 17:54:57 -07007432 set_bit(STATUS_EXIT_PENDING, &priv->status);
Zhu Yib24d22b2007-12-19 13:59:52 +08007433
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007434 iwl4965_down(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007435
Mohamed Abbas0359fac2008-03-28 16:21:08 -07007436 /* make sure we flush any pending irq or
7437 * tasklet for the driver
7438 */
7439 spin_lock_irqsave(&priv->lock, flags);
7440 iwl4965_disable_interrupts(priv);
7441 spin_unlock_irqrestore(&priv->lock, flags);
7442
7443 iwl_synchronize_irq(priv);
7444
Zhu Yib481de92007-09-25 17:54:57 -07007445 /* Free MAC hash list for ADHOC */
7446 for (i = 0; i < IWL_IBSS_MAC_HASH_SIZE; i++) {
7447 list_for_each_safe(p, q, &priv->ibss_mac_hash[i]) {
7448 list_del(p);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007449 kfree(list_entry(p, struct iwl4965_ibss_seq, list));
Zhu Yib481de92007-09-25 17:54:57 -07007450 }
7451 }
7452
Mohamed Abbasc8381fd2008-03-28 16:21:05 -07007453 iwlcore_low_level_notify(priv, IWLCORE_REMOVE_EVT);
Tomas Winkler712b6cf2008-03-12 16:58:52 -07007454 iwl_dbgfs_unregister(priv);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007455 sysfs_remove_group(&pdev->dev.kobj, &iwl4965_attribute_group);
Zhu Yib481de92007-09-25 17:54:57 -07007456
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007457 iwl4965_dealloc_ucode_pci(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007458
7459 if (priv->rxq.bd)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007460 iwl4965_rx_queue_free(priv, &priv->rxq);
7461 iwl4965_hw_txq_ctx_free(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007462
Assaf Kraussbf85ea42008-03-14 10:38:49 -07007463 iwlcore_clear_stations_table(priv);
Tomas Winkler073d3f52008-04-21 15:41:52 -07007464 iwl_eeprom_free(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007465
Zhu Yib481de92007-09-25 17:54:57 -07007466
Mohamed Abbas948c1712007-10-25 17:15:45 +08007467 /*netif_stop_queue(dev); */
7468 flush_workqueue(priv->workqueue);
7469
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007470 /* ieee80211_unregister_hw calls iwl4965_mac_stop, which flushes
Zhu Yib481de92007-09-25 17:54:57 -07007471 * priv->workqueue... so we can't take down the workqueue
7472 * until now... */
7473 destroy_workqueue(priv->workqueue);
7474 priv->workqueue = NULL;
7475
Zhu Yib481de92007-09-25 17:54:57 -07007476 pci_iounmap(pdev, priv->hw_base);
7477 pci_release_regions(pdev);
7478 pci_disable_device(pdev);
7479 pci_set_drvdata(pdev, NULL);
7480
Assaf Kraussbf85ea42008-03-14 10:38:49 -07007481 iwl_free_channel_map(priv);
Ron Rindjunskyc7de35c2008-04-23 17:15:05 -07007482 iwlcore_free_geos(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007483
7484 if (priv->ibss_beacon)
7485 dev_kfree_skb(priv->ibss_beacon);
7486
7487 ieee80211_free_hw(priv->hw);
7488}
7489
7490#ifdef CONFIG_PM
7491
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007492static int iwl4965_pci_suspend(struct pci_dev *pdev, pm_message_t state)
Zhu Yib481de92007-09-25 17:54:57 -07007493{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007494 struct iwl_priv *priv = pci_get_drvdata(pdev);
Zhu Yib481de92007-09-25 17:54:57 -07007495
Zhu Yie655b9f2008-01-24 02:19:38 -08007496 if (priv->is_open) {
7497 set_bit(STATUS_IN_SUSPEND, &priv->status);
7498 iwl4965_mac_stop(priv->hw);
7499 priv->is_open = 1;
7500 }
Zhu Yib481de92007-09-25 17:54:57 -07007501
Zhu Yib481de92007-09-25 17:54:57 -07007502 pci_set_power_state(pdev, PCI_D3hot);
7503
Zhu Yib481de92007-09-25 17:54:57 -07007504 return 0;
7505}
7506
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007507static int iwl4965_pci_resume(struct pci_dev *pdev)
Zhu Yib481de92007-09-25 17:54:57 -07007508{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007509 struct iwl_priv *priv = pci_get_drvdata(pdev);
Zhu Yib481de92007-09-25 17:54:57 -07007510
Zhu Yib481de92007-09-25 17:54:57 -07007511 pci_set_power_state(pdev, PCI_D0);
Zhu Yib481de92007-09-25 17:54:57 -07007512
Zhu Yie655b9f2008-01-24 02:19:38 -08007513 if (priv->is_open)
7514 iwl4965_mac_start(priv->hw);
Zhu Yib481de92007-09-25 17:54:57 -07007515
Zhu Yie655b9f2008-01-24 02:19:38 -08007516 clear_bit(STATUS_IN_SUSPEND, &priv->status);
Zhu Yib481de92007-09-25 17:54:57 -07007517 return 0;
7518}
7519
7520#endif /* CONFIG_PM */
7521
7522/*****************************************************************************
7523 *
7524 * driver and module entry point
7525 *
7526 *****************************************************************************/
7527
Ron Rindjunskyfed90172008-04-15 16:01:41 -07007528/* Hardware specific file defines the PCI IDs table for that hardware module */
7529static struct pci_device_id iwl_hw_card_ids[] = {
7530 {IWL_PCI_DEVICE(0x4229, PCI_ANY_ID, iwl4965_agn_cfg)},
7531 {IWL_PCI_DEVICE(0x4230, PCI_ANY_ID, iwl4965_agn_cfg)},
7532 {0}
7533};
7534MODULE_DEVICE_TABLE(pci, iwl_hw_card_ids);
7535
7536static struct pci_driver iwl_driver = {
Zhu Yib481de92007-09-25 17:54:57 -07007537 .name = DRV_NAME,
Ron Rindjunskyfed90172008-04-15 16:01:41 -07007538 .id_table = iwl_hw_card_ids,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007539 .probe = iwl4965_pci_probe,
7540 .remove = __devexit_p(iwl4965_pci_remove),
Zhu Yib481de92007-09-25 17:54:57 -07007541#ifdef CONFIG_PM
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007542 .suspend = iwl4965_pci_suspend,
7543 .resume = iwl4965_pci_resume,
Zhu Yib481de92007-09-25 17:54:57 -07007544#endif
7545};
7546
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007547static int __init iwl4965_init(void)
Zhu Yib481de92007-09-25 17:54:57 -07007548{
7549
7550 int ret;
7551 printk(KERN_INFO DRV_NAME ": " DRV_DESCRIPTION ", " DRV_VERSION "\n");
7552 printk(KERN_INFO DRV_NAME ": " DRV_COPYRIGHT "\n");
Reinette Chatre897e1cf2008-03-28 16:21:09 -07007553
7554 ret = iwl4965_rate_control_register();
7555 if (ret) {
7556 IWL_ERROR("Unable to register rate control algorithm: %d\n", ret);
7557 return ret;
7558 }
7559
Ron Rindjunskyfed90172008-04-15 16:01:41 -07007560 ret = pci_register_driver(&iwl_driver);
Zhu Yib481de92007-09-25 17:54:57 -07007561 if (ret) {
7562 IWL_ERROR("Unable to initialize PCI module\n");
Reinette Chatre897e1cf2008-03-28 16:21:09 -07007563 goto error_register;
Zhu Yib481de92007-09-25 17:54:57 -07007564 }
Tomas Winkler0a6857e2008-03-12 16:58:49 -07007565#ifdef CONFIG_IWLWIFI_DEBUG
Ron Rindjunskyfed90172008-04-15 16:01:41 -07007566 ret = driver_create_file(&iwl_driver.driver, &driver_attr_debug_level);
Zhu Yib481de92007-09-25 17:54:57 -07007567 if (ret) {
7568 IWL_ERROR("Unable to create driver sysfs file\n");
Reinette Chatre897e1cf2008-03-28 16:21:09 -07007569 goto error_debug;
Zhu Yib481de92007-09-25 17:54:57 -07007570 }
7571#endif
7572
7573 return ret;
Reinette Chatre897e1cf2008-03-28 16:21:09 -07007574
7575#ifdef CONFIG_IWLWIFI_DEBUG
7576error_debug:
Ron Rindjunskyfed90172008-04-15 16:01:41 -07007577 pci_unregister_driver(&iwl_driver);
Reinette Chatre897e1cf2008-03-28 16:21:09 -07007578#endif
7579error_register:
7580 iwl4965_rate_control_unregister();
7581 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07007582}
7583
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007584static void __exit iwl4965_exit(void)
Zhu Yib481de92007-09-25 17:54:57 -07007585{
Tomas Winkler0a6857e2008-03-12 16:58:49 -07007586#ifdef CONFIG_IWLWIFI_DEBUG
Ron Rindjunskyfed90172008-04-15 16:01:41 -07007587 driver_remove_file(&iwl_driver.driver, &driver_attr_debug_level);
Zhu Yib481de92007-09-25 17:54:57 -07007588#endif
Ron Rindjunskyfed90172008-04-15 16:01:41 -07007589 pci_unregister_driver(&iwl_driver);
Reinette Chatre897e1cf2008-03-28 16:21:09 -07007590 iwl4965_rate_control_unregister();
Zhu Yib481de92007-09-25 17:54:57 -07007591}
7592
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007593module_exit(iwl4965_exit);
7594module_init(iwl4965_init);