blob: 5ed16ce784689b9c1439d4fe96a98bd032a03e3f [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"
Zhu Yib481de92007-09-25 17:54:57 -070054
Tomas Winklerc79dd5b2008-03-12 16:58:50 -070055static int iwl4965_tx_queue_update_write_ptr(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -080056 struct iwl4965_tx_queue *txq);
Christoph Hellwig416e1432007-10-25 17:15:49 +080057
Zhu Yib481de92007-09-25 17:54:57 -070058/******************************************************************************
59 *
60 * module boiler plate
61 *
62 ******************************************************************************/
63
Zhu Yib481de92007-09-25 17:54:57 -070064/*
65 * module name, copyright, version, etc.
66 * NOTE: DRV_NAME is defined in iwlwifi.h for use by iwl-debug.h and printk
67 */
68
69#define DRV_DESCRIPTION "Intel(R) Wireless WiFi Link 4965AGN driver for Linux"
70
Tomas Winkler0a6857e2008-03-12 16:58:49 -070071#ifdef CONFIG_IWLWIFI_DEBUG
Zhu Yib481de92007-09-25 17:54:57 -070072#define VD "d"
73#else
74#define VD
75#endif
76
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +080077#ifdef CONFIG_IWL4965_SPECTRUM_MEASUREMENT
Zhu Yib481de92007-09-25 17:54:57 -070078#define VS "s"
79#else
80#define VS
81#endif
82
Tomas Winklerdf48c322008-03-06 10:40:19 -080083#define DRV_VERSION IWLWIFI_VERSION VD VS
Zhu Yib481de92007-09-25 17:54:57 -070084
Zhu Yib481de92007-09-25 17:54:57 -070085
86MODULE_DESCRIPTION(DRV_DESCRIPTION);
87MODULE_VERSION(DRV_VERSION);
88MODULE_AUTHOR(DRV_COPYRIGHT);
89MODULE_LICENSE("GPL");
90
91__le16 *ieee80211_get_qos_ctrl(struct ieee80211_hdr *hdr)
92{
93 u16 fc = le16_to_cpu(hdr->frame_control);
94 int hdr_len = ieee80211_get_hdrlen(fc);
95
96 if ((fc & 0x00cc) == (IEEE80211_STYPE_QOS_DATA | IEEE80211_FTYPE_DATA))
97 return (__le16 *) ((u8 *) hdr + hdr_len - QOS_CONTROL_LEN);
98 return NULL;
99}
100
Johannes Berg8318d782008-01-24 19:38:38 +0100101static const struct ieee80211_supported_band *iwl4965_get_hw_mode(
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700102 struct iwl_priv *priv, enum ieee80211_band band)
Zhu Yib481de92007-09-25 17:54:57 -0700103{
Johannes Berg8318d782008-01-24 19:38:38 +0100104 return priv->hw->wiphy->bands[band];
Zhu Yib481de92007-09-25 17:54:57 -0700105}
106
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800107static int iwl4965_is_empty_essid(const char *essid, int essid_len)
Zhu Yib481de92007-09-25 17:54:57 -0700108{
109 /* Single white space is for Linksys APs */
110 if (essid_len == 1 && essid[0] == ' ')
111 return 1;
112
113 /* Otherwise, if the entire essid is 0, we assume it is hidden */
114 while (essid_len) {
115 essid_len--;
116 if (essid[essid_len] != '\0')
117 return 0;
118 }
119
120 return 1;
121}
122
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800123static const char *iwl4965_escape_essid(const char *essid, u8 essid_len)
Zhu Yib481de92007-09-25 17:54:57 -0700124{
125 static char escaped[IW_ESSID_MAX_SIZE * 2 + 1];
126 const char *s = essid;
127 char *d = escaped;
128
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800129 if (iwl4965_is_empty_essid(essid, essid_len)) {
Zhu Yib481de92007-09-25 17:54:57 -0700130 memcpy(escaped, "<hidden>", sizeof("<hidden>"));
131 return escaped;
132 }
133
134 essid_len = min(essid_len, (u8) IW_ESSID_MAX_SIZE);
135 while (essid_len--) {
136 if (*s == '\0') {
137 *d++ = '\\';
138 *d++ = '0';
139 s++;
140 } else
141 *d++ = *s++;
142 }
143 *d = '\0';
144 return escaped;
145}
146
Zhu Yib481de92007-09-25 17:54:57 -0700147/*************** DMA-QUEUE-GENERAL-FUNCTIONS *****
148 * DMA services
149 *
150 * Theory of operation
151 *
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800152 * A Tx or Rx queue resides in host DRAM, and is comprised of a circular buffer
153 * of buffer descriptors, each of which points to one or more data buffers for
154 * the device to read from or fill. Driver and device exchange status of each
155 * queue via "read" and "write" pointers. Driver keeps minimum of 2 empty
156 * entries in each circular buffer, to protect against confusing empty and full
157 * queue states.
158 *
159 * The device reads or writes the data in the queues via the device's several
160 * DMA/FIFO channels. Each queue is mapped to a single DMA channel.
Zhu Yib481de92007-09-25 17:54:57 -0700161 *
162 * For Tx queue, there are low mark and high mark limits. If, after queuing
163 * the packet for Tx, free space become < low mark, Tx queue stopped. When
164 * reclaiming packets (on 'tx done IRQ), if free space become > high mark,
165 * Tx queue resumed.
166 *
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800167 * The 4965 operates with up to 17 queues: One receive queue, one transmit
168 * queue (#4) for sending commands to the device firmware, and 15 other
169 * Tx queues that may be mapped to prioritized Tx DMA/FIFO channels.
Ben Cahille3851442007-11-29 11:10:07 +0800170 *
171 * See more detailed info in iwl-4965-hw.h.
Zhu Yib481de92007-09-25 17:54:57 -0700172 ***************************************************/
173
Ron Rindjunskyfe01b472008-01-28 14:07:24 +0200174int iwl4965_queue_space(const struct iwl4965_queue *q)
Zhu Yib481de92007-09-25 17:54:57 -0700175{
Tomas Winklerfc4b6852007-10-25 17:15:24 +0800176 int s = q->read_ptr - q->write_ptr;
Zhu Yib481de92007-09-25 17:54:57 -0700177
Tomas Winklerfc4b6852007-10-25 17:15:24 +0800178 if (q->read_ptr > q->write_ptr)
Zhu Yib481de92007-09-25 17:54:57 -0700179 s -= q->n_bd;
180
181 if (s <= 0)
182 s += q->n_window;
183 /* keep some reserve to not confuse empty and full situations */
184 s -= 2;
185 if (s < 0)
186 s = 0;
187 return s;
188}
189
Zhu Yib481de92007-09-25 17:54:57 -0700190
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800191static inline int x2_queue_used(const struct iwl4965_queue *q, int i)
Zhu Yib481de92007-09-25 17:54:57 -0700192{
Tomas Winklerfc4b6852007-10-25 17:15:24 +0800193 return q->write_ptr > q->read_ptr ?
194 (i >= q->read_ptr && i < q->write_ptr) :
195 !(i < q->read_ptr && i >= q->write_ptr);
Zhu Yib481de92007-09-25 17:54:57 -0700196}
197
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800198static inline u8 get_cmd_index(struct iwl4965_queue *q, u32 index, int is_huge)
Zhu Yib481de92007-09-25 17:54:57 -0700199{
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800200 /* This is for scan command, the big buffer at end of command array */
Zhu Yib481de92007-09-25 17:54:57 -0700201 if (is_huge)
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800202 return q->n_window; /* must be power of 2 */
Zhu Yib481de92007-09-25 17:54:57 -0700203
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800204 /* Otherwise, use normal size buffers */
Zhu Yib481de92007-09-25 17:54:57 -0700205 return index & (q->n_window - 1);
206}
207
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800208/**
209 * iwl4965_queue_init - Initialize queue's high/low-water and read/write indexes
210 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700211static int iwl4965_queue_init(struct iwl_priv *priv, struct iwl4965_queue *q,
Zhu Yib481de92007-09-25 17:54:57 -0700212 int count, int slots_num, u32 id)
213{
214 q->n_bd = count;
215 q->n_window = slots_num;
216 q->id = id;
217
Tomas Winklerc54b6792008-03-06 17:36:53 -0800218 /* count must be power-of-two size, otherwise iwl_queue_inc_wrap
219 * and iwl_queue_dec_wrap are broken. */
Zhu Yib481de92007-09-25 17:54:57 -0700220 BUG_ON(!is_power_of_2(count));
221
222 /* slots_num must be power-of-two size, otherwise
223 * get_cmd_index is broken. */
224 BUG_ON(!is_power_of_2(slots_num));
225
226 q->low_mark = q->n_window / 4;
227 if (q->low_mark < 4)
228 q->low_mark = 4;
229
230 q->high_mark = q->n_window / 8;
231 if (q->high_mark < 2)
232 q->high_mark = 2;
233
Tomas Winklerfc4b6852007-10-25 17:15:24 +0800234 q->write_ptr = q->read_ptr = 0;
Zhu Yib481de92007-09-25 17:54:57 -0700235
236 return 0;
237}
238
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800239/**
240 * iwl4965_tx_queue_alloc - Alloc driver data and TFD CB for one Tx/cmd queue
241 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700242static int iwl4965_tx_queue_alloc(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800243 struct iwl4965_tx_queue *txq, u32 id)
Zhu Yib481de92007-09-25 17:54:57 -0700244{
245 struct pci_dev *dev = priv->pci_dev;
246
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800247 /* Driver private data, only for Tx (not command) queues,
248 * not shared with device. */
Zhu Yib481de92007-09-25 17:54:57 -0700249 if (id != IWL_CMD_QUEUE_NUM) {
250 txq->txb = kmalloc(sizeof(txq->txb[0]) *
251 TFD_QUEUE_SIZE_MAX, GFP_KERNEL);
252 if (!txq->txb) {
Ian Schram01ebd062007-10-25 17:15:22 +0800253 IWL_ERROR("kmalloc for auxiliary BD "
Zhu Yib481de92007-09-25 17:54:57 -0700254 "structures failed\n");
255 goto error;
256 }
257 } else
258 txq->txb = NULL;
259
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800260 /* Circular buffer of transmit frame descriptors (TFDs),
261 * shared with device */
Zhu Yib481de92007-09-25 17:54:57 -0700262 txq->bd = pci_alloc_consistent(dev,
263 sizeof(txq->bd[0]) * TFD_QUEUE_SIZE_MAX,
264 &txq->q.dma_addr);
265
266 if (!txq->bd) {
267 IWL_ERROR("pci_alloc_consistent(%zd) failed\n",
268 sizeof(txq->bd[0]) * TFD_QUEUE_SIZE_MAX);
269 goto error;
270 }
271 txq->q.id = id;
272
273 return 0;
274
275 error:
276 if (txq->txb) {
277 kfree(txq->txb);
278 txq->txb = NULL;
279 }
280
281 return -ENOMEM;
282}
283
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +0800284/**
285 * iwl4965_tx_queue_init - Allocate and initialize one tx/cmd queue
286 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700287int iwl4965_tx_queue_init(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800288 struct iwl4965_tx_queue *txq, int slots_num, u32 txq_id)
Zhu Yib481de92007-09-25 17:54:57 -0700289{
290 struct pci_dev *dev = priv->pci_dev;
291 int len;
292 int rc = 0;
293
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +0800294 /*
295 * Alloc buffer array for commands (Tx or other types of commands).
296 * For the command queue (#4), allocate command space + one big
297 * command for scan, since scan command is very huge; the system will
298 * not have two scans at the same time, so only one is needed.
Tomas Winklerbb542442007-12-05 20:59:59 +0200299 * For normal Tx queues (all other queues), no super-size command
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +0800300 * space is needed.
301 */
Tomas Winkler857485c2008-03-21 13:53:44 -0700302 len = sizeof(struct iwl_cmd) * slots_num;
Zhu Yib481de92007-09-25 17:54:57 -0700303 if (txq_id == IWL_CMD_QUEUE_NUM)
304 len += IWL_MAX_SCAN_SIZE;
305 txq->cmd = pci_alloc_consistent(dev, len, &txq->dma_addr_cmd);
306 if (!txq->cmd)
307 return -ENOMEM;
308
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +0800309 /* Alloc driver data array and TFD circular buffer */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800310 rc = iwl4965_tx_queue_alloc(priv, txq, txq_id);
Zhu Yib481de92007-09-25 17:54:57 -0700311 if (rc) {
312 pci_free_consistent(dev, len, txq->cmd, txq->dma_addr_cmd);
313
314 return -ENOMEM;
315 }
316 txq->need_update = 0;
317
318 /* TFD_QUEUE_SIZE_MAX must be power-of-two size, otherwise
Tomas Winklerc54b6792008-03-06 17:36:53 -0800319 * iwl_queue_inc_wrap and iwl_queue_dec_wrap are broken. */
Zhu Yib481de92007-09-25 17:54:57 -0700320 BUILD_BUG_ON(TFD_QUEUE_SIZE_MAX & (TFD_QUEUE_SIZE_MAX - 1));
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +0800321
322 /* Initialize queue's high/low-water marks, and head/tail indexes */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800323 iwl4965_queue_init(priv, &txq->q, TFD_QUEUE_SIZE_MAX, slots_num, txq_id);
Zhu Yib481de92007-09-25 17:54:57 -0700324
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +0800325 /* Tell device where to find queue */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800326 iwl4965_hw_tx_queue_init(priv, txq);
Zhu Yib481de92007-09-25 17:54:57 -0700327
328 return 0;
329}
330
331/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800332 * iwl4965_tx_queue_free - Deallocate DMA queue.
Zhu Yib481de92007-09-25 17:54:57 -0700333 * @txq: Transmit queue to deallocate.
334 *
335 * Empty queue by removing and destroying all BD's.
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800336 * Free all buffers.
337 * 0-fill, but do not free "txq" descriptor structure.
Zhu Yib481de92007-09-25 17:54:57 -0700338 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700339void iwl4965_tx_queue_free(struct iwl_priv *priv, struct iwl4965_tx_queue *txq)
Zhu Yib481de92007-09-25 17:54:57 -0700340{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800341 struct iwl4965_queue *q = &txq->q;
Zhu Yib481de92007-09-25 17:54:57 -0700342 struct pci_dev *dev = priv->pci_dev;
343 int len;
344
345 if (q->n_bd == 0)
346 return;
347
348 /* first, empty all BD's */
Tomas Winklerfc4b6852007-10-25 17:15:24 +0800349 for (; q->write_ptr != q->read_ptr;
Tomas Winklerc54b6792008-03-06 17:36:53 -0800350 q->read_ptr = iwl_queue_inc_wrap(q->read_ptr, q->n_bd))
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800351 iwl4965_hw_txq_free_tfd(priv, txq);
Zhu Yib481de92007-09-25 17:54:57 -0700352
Tomas Winkler857485c2008-03-21 13:53:44 -0700353 len = sizeof(struct iwl_cmd) * q->n_window;
Zhu Yib481de92007-09-25 17:54:57 -0700354 if (q->id == IWL_CMD_QUEUE_NUM)
355 len += IWL_MAX_SCAN_SIZE;
356
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800357 /* De-alloc array of command/tx buffers */
Zhu Yib481de92007-09-25 17:54:57 -0700358 pci_free_consistent(dev, len, txq->cmd, txq->dma_addr_cmd);
359
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800360 /* De-alloc circular buffer of TFDs */
Zhu Yib481de92007-09-25 17:54:57 -0700361 if (txq->q.n_bd)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800362 pci_free_consistent(dev, sizeof(struct iwl4965_tfd_frame) *
Zhu Yib481de92007-09-25 17:54:57 -0700363 txq->q.n_bd, txq->bd, txq->q.dma_addr);
364
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800365 /* De-alloc array of per-TFD driver data */
Zhu Yib481de92007-09-25 17:54:57 -0700366 if (txq->txb) {
367 kfree(txq->txb);
368 txq->txb = NULL;
369 }
370
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800371 /* 0-fill queue descriptor structure */
Zhu Yib481de92007-09-25 17:54:57 -0700372 memset(txq, 0, sizeof(*txq));
373}
374
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800375const u8 iwl4965_broadcast_addr[ETH_ALEN] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
Zhu Yib481de92007-09-25 17:54:57 -0700376
377/*************** STATION TABLE MANAGEMENT ****
Ben Cahill9fbab512007-11-29 11:09:47 +0800378 * mac80211 should be examined to determine if sta_info is duplicating
Zhu Yib481de92007-09-25 17:54:57 -0700379 * the functionality provided here
380 */
381
382/**************************************************************/
383
Ian Schram01ebd062007-10-25 17:15:22 +0800384#if 0 /* temporary disable till we add real remove station */
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800385/**
386 * iwl4965_remove_station - Remove driver's knowledge of station.
387 *
388 * NOTE: This does not remove station from device's station table.
389 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700390static u8 iwl4965_remove_station(struct iwl_priv *priv, const u8 *addr, int is_ap)
Zhu Yib481de92007-09-25 17:54:57 -0700391{
392 int index = IWL_INVALID_STATION;
393 int i;
394 unsigned long flags;
395
396 spin_lock_irqsave(&priv->sta_lock, flags);
397
398 if (is_ap)
399 index = IWL_AP_ID;
400 else if (is_broadcast_ether_addr(addr))
Tomas Winkler5425e492008-04-15 16:01:38 -0700401 index = priv->hw_params.bcast_sta_id;
Zhu Yib481de92007-09-25 17:54:57 -0700402 else
Tomas Winkler5425e492008-04-15 16:01:38 -0700403 for (i = IWL_STA_ID; i < priv->hw_params.max_stations; i++)
Zhu Yib481de92007-09-25 17:54:57 -0700404 if (priv->stations[i].used &&
405 !compare_ether_addr(priv->stations[i].sta.sta.addr,
406 addr)) {
407 index = i;
408 break;
409 }
410
411 if (unlikely(index == IWL_INVALID_STATION))
412 goto out;
413
414 if (priv->stations[index].used) {
415 priv->stations[index].used = 0;
416 priv->num_stations--;
417 }
418
419 BUG_ON(priv->num_stations < 0);
420
421out:
422 spin_unlock_irqrestore(&priv->sta_lock, flags);
423 return 0;
424}
Zhu Yi556f8db2007-09-27 11:27:33 +0800425#endif
Zhu Yib481de92007-09-25 17:54:57 -0700426
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800427/**
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800428 * iwl4965_add_station_flags - Add station to tables in driver and device
429 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700430u8 iwl4965_add_station_flags(struct iwl_priv *priv, const u8 *addr,
Ron Rindjunsky67d62032007-11-26 16:14:40 +0200431 int is_ap, u8 flags, void *ht_data)
Zhu Yib481de92007-09-25 17:54:57 -0700432{
433 int i;
434 int index = IWL_INVALID_STATION;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800435 struct iwl4965_station_entry *station;
Zhu Yib481de92007-09-25 17:54:57 -0700436 unsigned long flags_spin;
Joe Perches0795af52007-10-03 17:59:30 -0700437 DECLARE_MAC_BUF(mac);
Zhu Yib481de92007-09-25 17:54:57 -0700438
439 spin_lock_irqsave(&priv->sta_lock, flags_spin);
440 if (is_ap)
441 index = IWL_AP_ID;
442 else if (is_broadcast_ether_addr(addr))
Tomas Winkler5425e492008-04-15 16:01:38 -0700443 index = priv->hw_params.bcast_sta_id;
Zhu Yib481de92007-09-25 17:54:57 -0700444 else
Tomas Winkler5425e492008-04-15 16:01:38 -0700445 for (i = IWL_STA_ID; i < priv->hw_params.max_stations; i++) {
Zhu Yib481de92007-09-25 17:54:57 -0700446 if (!compare_ether_addr(priv->stations[i].sta.sta.addr,
447 addr)) {
448 index = i;
449 break;
450 }
451
452 if (!priv->stations[i].used &&
453 index == IWL_INVALID_STATION)
454 index = i;
455 }
456
457
Ben Cahill9fbab512007-11-29 11:09:47 +0800458 /* These two conditions have the same outcome, but keep them separate
459 since they have different meanings */
Zhu Yib481de92007-09-25 17:54:57 -0700460 if (unlikely(index == IWL_INVALID_STATION)) {
461 spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
462 return index;
463 }
464
465 if (priv->stations[index].used &&
466 !compare_ether_addr(priv->stations[index].sta.sta.addr, addr)) {
467 spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
468 return index;
469 }
470
471
Joe Perches0795af52007-10-03 17:59:30 -0700472 IWL_DEBUG_ASSOC("Add STA ID %d: %s\n", index, print_mac(mac, addr));
Zhu Yib481de92007-09-25 17:54:57 -0700473 station = &priv->stations[index];
474 station->used = 1;
475 priv->num_stations++;
476
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800477 /* Set up the REPLY_ADD_STA command to send to device */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800478 memset(&station->sta, 0, sizeof(struct iwl4965_addsta_cmd));
Zhu Yib481de92007-09-25 17:54:57 -0700479 memcpy(station->sta.sta.addr, addr, ETH_ALEN);
480 station->sta.mode = 0;
481 station->sta.sta.sta_id = index;
482 station->sta.station_flags = 0;
483
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +0800484#ifdef CONFIG_IWL4965_HT
Zhu Yib481de92007-09-25 17:54:57 -0700485 /* BCAST station and IBSS stations do not work in HT mode */
Tomas Winkler5425e492008-04-15 16:01:38 -0700486 if (index != priv->hw_params.bcast_sta_id &&
Zhu Yib481de92007-09-25 17:54:57 -0700487 priv->iw_mode != IEEE80211_IF_TYPE_IBSS)
Ron Rindjunsky67d62032007-11-26 16:14:40 +0200488 iwl4965_set_ht_add_station(priv, index,
489 (struct ieee80211_ht_info *) ht_data);
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +0800490#endif /*CONFIG_IWL4965_HT*/
Zhu Yib481de92007-09-25 17:54:57 -0700491
492 spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800493
494 /* Add station to device's station table */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800495 iwl4965_send_add_station(priv, &station->sta, flags);
Zhu Yib481de92007-09-25 17:54:57 -0700496 return index;
497
498}
499
Zhu Yib481de92007-09-25 17:54:57 -0700500
Zhu Yib481de92007-09-25 17:54:57 -0700501
502/*************** HOST COMMAND QUEUE FUNCTIONS *****/
503
Zhu Yib481de92007-09-25 17:54:57 -0700504/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800505 * iwl4965_enqueue_hcmd - enqueue a uCode command
Zhu Yib481de92007-09-25 17:54:57 -0700506 * @priv: device private data point
507 * @cmd: a point to the ucode command structure
508 *
509 * The function returns < 0 values to indicate the operation is
510 * failed. On success, it turns the index (> 0) of command in the
511 * command queue.
512 */
Tomas Winkler857485c2008-03-21 13:53:44 -0700513int iwl4965_enqueue_hcmd(struct iwl_priv *priv, struct iwl_host_cmd *cmd)
Zhu Yib481de92007-09-25 17:54:57 -0700514{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800515 struct iwl4965_tx_queue *txq = &priv->txq[IWL_CMD_QUEUE_NUM];
516 struct iwl4965_queue *q = &txq->q;
517 struct iwl4965_tfd_frame *tfd;
Zhu Yib481de92007-09-25 17:54:57 -0700518 u32 *control_flags;
Tomas Winkler857485c2008-03-21 13:53:44 -0700519 struct iwl_cmd *out_cmd;
Zhu Yib481de92007-09-25 17:54:57 -0700520 u32 idx;
521 u16 fix_size = (u16)(cmd->len + sizeof(out_cmd->hdr));
522 dma_addr_t phys_addr;
523 int ret;
524 unsigned long flags;
525
526 /* If any of the command structures end up being larger than
527 * the TFD_MAX_PAYLOAD_SIZE, and it sent as a 'small' command then
528 * we will need to increase the size of the TFD entries */
529 BUG_ON((fix_size > TFD_MAX_PAYLOAD_SIZE) &&
530 !(cmd->meta.flags & CMD_SIZE_HUGE));
531
Tomas Winklerfee12472008-04-03 16:05:21 -0700532 if (iwl_is_rfkill(priv)) {
Gregory Greenmanc342a1b2008-02-06 11:20:40 -0800533 IWL_DEBUG_INFO("Not sending command - RF KILL");
534 return -EIO;
535 }
536
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800537 if (iwl4965_queue_space(q) < ((cmd->meta.flags & CMD_ASYNC) ? 2 : 1)) {
Zhu Yib481de92007-09-25 17:54:57 -0700538 IWL_ERROR("No space for Tx\n");
539 return -ENOSPC;
540 }
541
542 spin_lock_irqsave(&priv->hcmd_lock, flags);
543
Tomas Winklerfc4b6852007-10-25 17:15:24 +0800544 tfd = &txq->bd[q->write_ptr];
Zhu Yib481de92007-09-25 17:54:57 -0700545 memset(tfd, 0, sizeof(*tfd));
546
547 control_flags = (u32 *) tfd;
548
Tomas Winklerfc4b6852007-10-25 17:15:24 +0800549 idx = get_cmd_index(q, q->write_ptr, cmd->meta.flags & CMD_SIZE_HUGE);
Zhu Yib481de92007-09-25 17:54:57 -0700550 out_cmd = &txq->cmd[idx];
551
552 out_cmd->hdr.cmd = cmd->id;
553 memcpy(&out_cmd->meta, &cmd->meta, sizeof(cmd->meta));
554 memcpy(&out_cmd->cmd.payload, cmd->data, cmd->len);
555
556 /* At this point, the out_cmd now has all of the incoming cmd
557 * information */
558
559 out_cmd->hdr.flags = 0;
560 out_cmd->hdr.sequence = cpu_to_le16(QUEUE_TO_SEQ(IWL_CMD_QUEUE_NUM) |
Tomas Winklerfc4b6852007-10-25 17:15:24 +0800561 INDEX_TO_SEQ(q->write_ptr));
Zhu Yib481de92007-09-25 17:54:57 -0700562 if (out_cmd->meta.flags & CMD_SIZE_HUGE)
563 out_cmd->hdr.sequence |= cpu_to_le16(SEQ_HUGE_FRAME);
564
565 phys_addr = txq->dma_addr_cmd + sizeof(txq->cmd[0]) * idx +
Tomas Winkler857485c2008-03-21 13:53:44 -0700566 offsetof(struct iwl_cmd, hdr);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800567 iwl4965_hw_txq_attach_buf_to_tfd(priv, tfd, phys_addr, fix_size);
Zhu Yib481de92007-09-25 17:54:57 -0700568
569 IWL_DEBUG_HC("Sending command %s (#%x), seq: 0x%04X, "
570 "%d bytes at %d[%d]:%d\n",
571 get_cmd_string(out_cmd->hdr.cmd),
572 out_cmd->hdr.cmd, le16_to_cpu(out_cmd->hdr.sequence),
Tomas Winklerfc4b6852007-10-25 17:15:24 +0800573 fix_size, q->write_ptr, idx, IWL_CMD_QUEUE_NUM);
Zhu Yib481de92007-09-25 17:54:57 -0700574
575 txq->need_update = 1;
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800576
577 /* Set up entry in queue's byte count circular buffer */
Tomas Winklere2a722e2008-04-14 21:16:10 -0700578 priv->cfg->ops->lib->txq_update_byte_cnt_tbl(priv, txq, 0);
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800579
580 /* Increment and update queue's write index */
Tomas Winklerc54b6792008-03-06 17:36:53 -0800581 q->write_ptr = iwl_queue_inc_wrap(q->write_ptr, q->n_bd);
Tomas Winklere2a722e2008-04-14 21:16:10 -0700582 ret = iwl4965_tx_queue_update_write_ptr(priv, txq);
Zhu Yib481de92007-09-25 17:54:57 -0700583
584 spin_unlock_irqrestore(&priv->hcmd_lock, flags);
585 return ret ? ret : idx;
586}
587
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -0700588static void iwl4965_set_rxon_hwcrypto(struct iwl_priv *priv, int hw_decrypt)
589{
590 struct iwl4965_rxon_cmd *rxon = &priv->staging_rxon;
591
592 if (hw_decrypt)
593 rxon->filter_flags &= ~RXON_FILTER_DIS_DECRYPT_MSK;
594 else
595 rxon->filter_flags |= RXON_FILTER_DIS_DECRYPT_MSK;
596
597}
598
Zhu Yib481de92007-09-25 17:54:57 -0700599/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800600 * iwl4965_rxon_add_station - add station into station table.
Zhu Yib481de92007-09-25 17:54:57 -0700601 *
602 * there is only one AP station with id= IWL_AP_ID
Ben Cahill9fbab512007-11-29 11:09:47 +0800603 * NOTE: mutex must be held before calling this fnction
604 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700605static int iwl4965_rxon_add_station(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -0700606 const u8 *addr, int is_ap)
607{
Zhu Yi556f8db2007-09-27 11:27:33 +0800608 u8 sta_id;
Zhu Yib481de92007-09-25 17:54:57 -0700609
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800610 /* Add station to device's station table */
Ron Rindjunsky67d62032007-11-26 16:14:40 +0200611#ifdef CONFIG_IWL4965_HT
612 struct ieee80211_conf *conf = &priv->hw->conf;
613 struct ieee80211_ht_info *cur_ht_config = &conf->ht_conf;
614
615 if ((is_ap) &&
616 (conf->flags & IEEE80211_CONF_SUPPORT_HT_MODE) &&
617 (priv->iw_mode == IEEE80211_IF_TYPE_STA))
618 sta_id = iwl4965_add_station_flags(priv, addr, is_ap,
619 0, cur_ht_config);
620 else
621#endif /* CONFIG_IWL4965_HT */
622 sta_id = iwl4965_add_station_flags(priv, addr, is_ap,
623 0, NULL);
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800624
625 /* Set up default rate scaling table in device's station table */
Zhu Yib481de92007-09-25 17:54:57 -0700626 iwl4965_add_station(priv, addr, is_ap);
627
Zhu Yi556f8db2007-09-27 11:27:33 +0800628 return sta_id;
Zhu Yib481de92007-09-25 17:54:57 -0700629}
630
631/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800632 * iwl4965_check_rxon_cmd - validate RXON structure is valid
Zhu Yib481de92007-09-25 17:54:57 -0700633 *
634 * NOTE: This is really only useful during development and can eventually
635 * be #ifdef'd out once the driver is stable and folks aren't actively
636 * making changes
637 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800638static int iwl4965_check_rxon_cmd(struct iwl4965_rxon_cmd *rxon)
Zhu Yib481de92007-09-25 17:54:57 -0700639{
640 int error = 0;
641 int counter = 1;
642
643 if (rxon->flags & RXON_FLG_BAND_24G_MSK) {
644 error |= le32_to_cpu(rxon->flags &
645 (RXON_FLG_TGJ_NARROW_BAND_MSK |
646 RXON_FLG_RADAR_DETECT_MSK));
647 if (error)
648 IWL_WARNING("check 24G fields %d | %d\n",
649 counter++, error);
650 } else {
651 error |= (rxon->flags & RXON_FLG_SHORT_SLOT_MSK) ?
652 0 : le32_to_cpu(RXON_FLG_SHORT_SLOT_MSK);
653 if (error)
654 IWL_WARNING("check 52 fields %d | %d\n",
655 counter++, error);
656 error |= le32_to_cpu(rxon->flags & RXON_FLG_CCK_MSK);
657 if (error)
658 IWL_WARNING("check 52 CCK %d | %d\n",
659 counter++, error);
660 }
661 error |= (rxon->node_addr[0] | rxon->bssid_addr[0]) & 0x1;
662 if (error)
663 IWL_WARNING("check mac addr %d | %d\n", counter++, error);
664
665 /* make sure basic rates 6Mbps and 1Mbps are supported */
666 error |= (((rxon->ofdm_basic_rates & IWL_RATE_6M_MASK) == 0) &&
667 ((rxon->cck_basic_rates & IWL_RATE_1M_MASK) == 0));
668 if (error)
669 IWL_WARNING("check basic rate %d | %d\n", counter++, error);
670
671 error |= (le16_to_cpu(rxon->assoc_id) > 2007);
672 if (error)
673 IWL_WARNING("check assoc id %d | %d\n", counter++, error);
674
675 error |= ((rxon->flags & (RXON_FLG_CCK_MSK | RXON_FLG_SHORT_SLOT_MSK))
676 == (RXON_FLG_CCK_MSK | RXON_FLG_SHORT_SLOT_MSK));
677 if (error)
678 IWL_WARNING("check CCK and short slot %d | %d\n",
679 counter++, error);
680
681 error |= ((rxon->flags & (RXON_FLG_CCK_MSK | RXON_FLG_AUTO_DETECT_MSK))
682 == (RXON_FLG_CCK_MSK | RXON_FLG_AUTO_DETECT_MSK));
683 if (error)
684 IWL_WARNING("check CCK & auto detect %d | %d\n",
685 counter++, error);
686
687 error |= ((rxon->flags & (RXON_FLG_AUTO_DETECT_MSK |
688 RXON_FLG_TGG_PROTECT_MSK)) == RXON_FLG_TGG_PROTECT_MSK);
689 if (error)
690 IWL_WARNING("check TGG and auto detect %d | %d\n",
691 counter++, error);
692
693 if (error)
694 IWL_WARNING("Tuning to channel %d\n",
695 le16_to_cpu(rxon->channel));
696
697 if (error) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800698 IWL_ERROR("Not a valid iwl4965_rxon_assoc_cmd field values\n");
Zhu Yib481de92007-09-25 17:54:57 -0700699 return -1;
700 }
701 return 0;
702}
703
704/**
Ben Cahill9fbab512007-11-29 11:09:47 +0800705 * iwl4965_full_rxon_required - check if full RXON (vs RXON_ASSOC) cmd is needed
Ian Schram01ebd062007-10-25 17:15:22 +0800706 * @priv: staging_rxon is compared to active_rxon
Zhu Yib481de92007-09-25 17:54:57 -0700707 *
Ben Cahill9fbab512007-11-29 11:09:47 +0800708 * If the RXON structure is changing enough to require a new tune,
709 * or is clearing the RXON_FILTER_ASSOC_MSK, then return 1 to indicate that
710 * a new tune (full RXON command, rather than RXON_ASSOC cmd) is required.
Zhu Yib481de92007-09-25 17:54:57 -0700711 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700712static int iwl4965_full_rxon_required(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700713{
714
715 /* These items are only settable from the full RXON command */
716 if (!(priv->active_rxon.filter_flags & RXON_FILTER_ASSOC_MSK) ||
717 compare_ether_addr(priv->staging_rxon.bssid_addr,
718 priv->active_rxon.bssid_addr) ||
719 compare_ether_addr(priv->staging_rxon.node_addr,
720 priv->active_rxon.node_addr) ||
721 compare_ether_addr(priv->staging_rxon.wlap_bssid_addr,
722 priv->active_rxon.wlap_bssid_addr) ||
723 (priv->staging_rxon.dev_type != priv->active_rxon.dev_type) ||
724 (priv->staging_rxon.channel != priv->active_rxon.channel) ||
725 (priv->staging_rxon.air_propagation !=
726 priv->active_rxon.air_propagation) ||
727 (priv->staging_rxon.ofdm_ht_single_stream_basic_rates !=
728 priv->active_rxon.ofdm_ht_single_stream_basic_rates) ||
729 (priv->staging_rxon.ofdm_ht_dual_stream_basic_rates !=
730 priv->active_rxon.ofdm_ht_dual_stream_basic_rates) ||
731 (priv->staging_rxon.rx_chain != priv->active_rxon.rx_chain) ||
732 (priv->staging_rxon.assoc_id != priv->active_rxon.assoc_id))
733 return 1;
734
735 /* flags, filter_flags, ofdm_basic_rates, and cck_basic_rates can
736 * be updated with the RXON_ASSOC command -- however only some
737 * flag transitions are allowed using RXON_ASSOC */
738
739 /* Check if we are not switching bands */
740 if ((priv->staging_rxon.flags & RXON_FLG_BAND_24G_MSK) !=
741 (priv->active_rxon.flags & RXON_FLG_BAND_24G_MSK))
742 return 1;
743
744 /* Check if we are switching association toggle */
745 if ((priv->staging_rxon.filter_flags & RXON_FILTER_ASSOC_MSK) !=
746 (priv->active_rxon.filter_flags & RXON_FILTER_ASSOC_MSK))
747 return 1;
748
749 return 0;
750}
751
Zhu Yib481de92007-09-25 17:54:57 -0700752/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800753 * iwl4965_commit_rxon - commit staging_rxon to hardware
Zhu Yib481de92007-09-25 17:54:57 -0700754 *
Ian Schram01ebd062007-10-25 17:15:22 +0800755 * The RXON command in staging_rxon is committed to the hardware and
Zhu Yib481de92007-09-25 17:54:57 -0700756 * the active_rxon structure is updated with the new data. This
757 * function correctly transitions out of the RXON_ASSOC_MSK state if
758 * a HW tune is required based on the RXON structure changes.
759 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700760static int iwl4965_commit_rxon(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700761{
762 /* cast away the const for active_rxon in this function */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800763 struct iwl4965_rxon_cmd *active_rxon = (void *)&priv->active_rxon;
Joe Perches0795af52007-10-03 17:59:30 -0700764 DECLARE_MAC_BUF(mac);
Zhu Yib481de92007-09-25 17:54:57 -0700765 int rc = 0;
766
Tomas Winklerfee12472008-04-03 16:05:21 -0700767 if (!iwl_is_alive(priv))
Zhu Yib481de92007-09-25 17:54:57 -0700768 return -1;
769
770 /* always get timestamp with Rx frame */
771 priv->staging_rxon.flags |= RXON_FLG_TSF2HOST_MSK;
772
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800773 rc = iwl4965_check_rxon_cmd(&priv->staging_rxon);
Zhu Yib481de92007-09-25 17:54:57 -0700774 if (rc) {
775 IWL_ERROR("Invalid RXON configuration. Not committing.\n");
776 return -EINVAL;
777 }
778
779 /* If we don't need to send a full RXON, we can use
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800780 * iwl4965_rxon_assoc_cmd which is used to reconfigure filter
Zhu Yib481de92007-09-25 17:54:57 -0700781 * and other flags for the current radio configuration. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800782 if (!iwl4965_full_rxon_required(priv)) {
Tomas Winkler7e8c5192008-04-15 16:01:43 -0700783 rc = iwl_send_rxon_assoc(priv);
Zhu Yib481de92007-09-25 17:54:57 -0700784 if (rc) {
785 IWL_ERROR("Error setting RXON_ASSOC "
786 "configuration (%d).\n", rc);
787 return rc;
788 }
789
790 memcpy(active_rxon, &priv->staging_rxon, sizeof(*active_rxon));
791
792 return 0;
793 }
794
795 /* station table will be cleared */
796 priv->assoc_station_added = 0;
797
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +0800798#ifdef CONFIG_IWL4965_SENSITIVITY
Zhu Yib481de92007-09-25 17:54:57 -0700799 priv->sensitivity_data.state = IWL_SENS_CALIB_NEED_REINIT;
800 if (!priv->error_recovering)
801 priv->start_calib = 0;
802
803 iwl4965_init_sensitivity(priv, CMD_ASYNC, 1);
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +0800804#endif /* CONFIG_IWL4965_SENSITIVITY */
Zhu Yib481de92007-09-25 17:54:57 -0700805
806 /* If we are currently associated and the new config requires
807 * an RXON_ASSOC and the new config wants the associated mask enabled,
808 * we must clear the associated from the active configuration
809 * before we apply the new config */
Tomas Winkler3109ece2008-03-28 16:33:35 -0700810 if (iwl_is_associated(priv) &&
Zhu Yib481de92007-09-25 17:54:57 -0700811 (priv->staging_rxon.filter_flags & RXON_FILTER_ASSOC_MSK)) {
812 IWL_DEBUG_INFO("Toggling associated bit on current RXON\n");
813 active_rxon->filter_flags &= ~RXON_FILTER_ASSOC_MSK;
814
Tomas Winkler857485c2008-03-21 13:53:44 -0700815 rc = iwl_send_cmd_pdu(priv, REPLY_RXON,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800816 sizeof(struct iwl4965_rxon_cmd),
Zhu Yib481de92007-09-25 17:54:57 -0700817 &priv->active_rxon);
818
819 /* If the mask clearing failed then we set
820 * active_rxon back to what it was previously */
821 if (rc) {
822 active_rxon->filter_flags |= RXON_FILTER_ASSOC_MSK;
823 IWL_ERROR("Error clearing ASSOC_MSK on current "
824 "configuration (%d).\n", rc);
825 return rc;
826 }
Zhu Yib481de92007-09-25 17:54:57 -0700827 }
828
829 IWL_DEBUG_INFO("Sending RXON\n"
830 "* with%s RXON_FILTER_ASSOC_MSK\n"
831 "* channel = %d\n"
Joe Perches0795af52007-10-03 17:59:30 -0700832 "* bssid = %s\n",
Zhu Yib481de92007-09-25 17:54:57 -0700833 ((priv->staging_rxon.filter_flags &
834 RXON_FILTER_ASSOC_MSK) ? "" : "out"),
835 le16_to_cpu(priv->staging_rxon.channel),
Joe Perches0795af52007-10-03 17:59:30 -0700836 print_mac(mac, priv->staging_rxon.bssid_addr));
Zhu Yib481de92007-09-25 17:54:57 -0700837
Emmanuel Grumbachfcc76c62008-04-15 16:01:47 -0700838 iwl4965_set_rxon_hwcrypto(priv, !priv->cfg->mod_params->sw_crypto);
Zhu Yib481de92007-09-25 17:54:57 -0700839 /* Apply the new configuration */
Tomas Winkler857485c2008-03-21 13:53:44 -0700840 rc = iwl_send_cmd_pdu(priv, REPLY_RXON,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800841 sizeof(struct iwl4965_rxon_cmd), &priv->staging_rxon);
Zhu Yib481de92007-09-25 17:54:57 -0700842 if (rc) {
843 IWL_ERROR("Error setting new configuration (%d).\n", rc);
844 return rc;
845 }
846
Assaf Kraussbf85ea42008-03-14 10:38:49 -0700847 iwlcore_clear_stations_table(priv);
Zhu Yi556f8db2007-09-27 11:27:33 +0800848
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +0800849#ifdef CONFIG_IWL4965_SENSITIVITY
Zhu Yib481de92007-09-25 17:54:57 -0700850 if (!priv->error_recovering)
851 priv->start_calib = 0;
852
853 priv->sensitivity_data.state = IWL_SENS_CALIB_NEED_REINIT;
854 iwl4965_init_sensitivity(priv, CMD_ASYNC, 1);
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +0800855#endif /* CONFIG_IWL4965_SENSITIVITY */
Zhu Yib481de92007-09-25 17:54:57 -0700856
857 memcpy(active_rxon, &priv->staging_rxon, sizeof(*active_rxon));
858
859 /* If we issue a new RXON command which required a tune then we must
860 * send a new TXPOWER command or we won't be able to Tx any frames */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800861 rc = iwl4965_hw_reg_send_txpower(priv);
Zhu Yib481de92007-09-25 17:54:57 -0700862 if (rc) {
863 IWL_ERROR("Error setting Tx power (%d).\n", rc);
864 return rc;
865 }
866
867 /* Add the broadcast address so we can send broadcast frames */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800868 if (iwl4965_rxon_add_station(priv, iwl4965_broadcast_addr, 0) ==
Zhu Yib481de92007-09-25 17:54:57 -0700869 IWL_INVALID_STATION) {
870 IWL_ERROR("Error adding BROADCAST address for transmit.\n");
871 return -EIO;
872 }
873
874 /* If we have set the ASSOC_MSK and we are in BSS mode then
875 * add the IWL_AP_ID to the station rate table */
Tomas Winkler3109ece2008-03-28 16:33:35 -0700876 if (iwl_is_associated(priv) &&
Zhu Yib481de92007-09-25 17:54:57 -0700877 (priv->iw_mode == IEEE80211_IF_TYPE_STA)) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800878 if (iwl4965_rxon_add_station(priv, priv->active_rxon.bssid_addr, 1)
Zhu Yib481de92007-09-25 17:54:57 -0700879 == IWL_INVALID_STATION) {
880 IWL_ERROR("Error adding AP address for transmit.\n");
881 return -EIO;
882 }
883 priv->assoc_station_added = 1;
Emmanuel Grumbach6974e362008-04-14 21:16:06 -0700884 if (priv->default_wep_key &&
885 iwl_send_static_wepkey_cmd(priv, 0))
886 IWL_ERROR("Could not send WEP static key.\n");
Zhu Yib481de92007-09-25 17:54:57 -0700887 }
888
889 return 0;
890}
891
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700892static int iwl4965_send_bt_config(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700893{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800894 struct iwl4965_bt_cmd bt_cmd = {
Zhu Yib481de92007-09-25 17:54:57 -0700895 .flags = 3,
896 .lead_time = 0xAA,
897 .max_kill = 1,
898 .kill_ack_mask = 0,
899 .kill_cts_mask = 0,
900 };
901
Tomas Winkler857485c2008-03-21 13:53:44 -0700902 return iwl_send_cmd_pdu(priv, REPLY_BT_CONFIG,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800903 sizeof(struct iwl4965_bt_cmd), &bt_cmd);
Zhu Yib481de92007-09-25 17:54:57 -0700904}
905
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700906static int iwl4965_send_scan_abort(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700907{
908 int rc = 0;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800909 struct iwl4965_rx_packet *res;
Tomas Winkler857485c2008-03-21 13:53:44 -0700910 struct iwl_host_cmd cmd = {
Zhu Yib481de92007-09-25 17:54:57 -0700911 .id = REPLY_SCAN_ABORT_CMD,
912 .meta.flags = CMD_WANT_SKB,
913 };
914
915 /* If there isn't a scan actively going on in the hardware
916 * then we are in between scan bands and not actually
917 * actively scanning, so don't send the abort command */
918 if (!test_bit(STATUS_SCAN_HW, &priv->status)) {
919 clear_bit(STATUS_SCAN_ABORTING, &priv->status);
920 return 0;
921 }
922
Tomas Winkler857485c2008-03-21 13:53:44 -0700923 rc = iwl_send_cmd_sync(priv, &cmd);
Zhu Yib481de92007-09-25 17:54:57 -0700924 if (rc) {
925 clear_bit(STATUS_SCAN_ABORTING, &priv->status);
926 return rc;
927 }
928
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800929 res = (struct iwl4965_rx_packet *)cmd.meta.u.skb->data;
Zhu Yib481de92007-09-25 17:54:57 -0700930 if (res->u.status != CAN_ABORT_STATUS) {
931 /* The scan abort will return 1 for success or
932 * 2 for "failure". A failure condition can be
933 * due to simply not being in an active scan which
934 * can occur if we send the scan abort before we
935 * the microcode has notified us that a scan is
936 * completed. */
937 IWL_DEBUG_INFO("SCAN_ABORT returned %d.\n", res->u.status);
938 clear_bit(STATUS_SCAN_ABORTING, &priv->status);
939 clear_bit(STATUS_SCAN_HW, &priv->status);
940 }
941
942 dev_kfree_skb_any(cmd.meta.u.skb);
943
944 return rc;
945}
946
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700947static int iwl4965_card_state_sync_callback(struct iwl_priv *priv,
Tomas Winkler857485c2008-03-21 13:53:44 -0700948 struct iwl_cmd *cmd,
Zhu Yib481de92007-09-25 17:54:57 -0700949 struct sk_buff *skb)
950{
951 return 1;
952}
953
954/*
955 * CARD_STATE_CMD
956 *
Ben Cahill9fbab512007-11-29 11:09:47 +0800957 * Use: Sets the device's internal card state to enable, disable, or halt
Zhu Yib481de92007-09-25 17:54:57 -0700958 *
959 * When in the 'enable' state the card operates as normal.
960 * When in the 'disable' state, the card enters into a low power mode.
961 * When in the 'halt' state, the card is shut down and must be fully
962 * restarted to come back on.
963 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700964static int iwl4965_send_card_state(struct iwl_priv *priv, u32 flags, u8 meta_flag)
Zhu Yib481de92007-09-25 17:54:57 -0700965{
Tomas Winkler857485c2008-03-21 13:53:44 -0700966 struct iwl_host_cmd cmd = {
Zhu Yib481de92007-09-25 17:54:57 -0700967 .id = REPLY_CARD_STATE_CMD,
968 .len = sizeof(u32),
969 .data = &flags,
970 .meta.flags = meta_flag,
971 };
972
973 if (meta_flag & CMD_ASYNC)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800974 cmd.meta.u.callback = iwl4965_card_state_sync_callback;
Zhu Yib481de92007-09-25 17:54:57 -0700975
Tomas Winkler857485c2008-03-21 13:53:44 -0700976 return iwl_send_cmd(priv, &cmd);
Zhu Yib481de92007-09-25 17:54:57 -0700977}
978
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700979static int iwl4965_add_sta_sync_callback(struct iwl_priv *priv,
Tomas Winkler857485c2008-03-21 13:53:44 -0700980 struct iwl_cmd *cmd, struct sk_buff *skb)
Zhu Yib481de92007-09-25 17:54:57 -0700981{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800982 struct iwl4965_rx_packet *res = NULL;
Zhu Yib481de92007-09-25 17:54:57 -0700983
984 if (!skb) {
985 IWL_ERROR("Error: Response NULL in REPLY_ADD_STA.\n");
986 return 1;
987 }
988
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800989 res = (struct iwl4965_rx_packet *)skb->data;
Zhu Yib481de92007-09-25 17:54:57 -0700990 if (res->hdr.flags & IWL_CMD_FAILED_MSK) {
991 IWL_ERROR("Bad return from REPLY_ADD_STA (0x%08X)\n",
992 res->hdr.flags);
993 return 1;
994 }
995
996 switch (res->u.add_sta.status) {
997 case ADD_STA_SUCCESS_MSK:
998 break;
999 default:
1000 break;
1001 }
1002
1003 /* We didn't cache the SKB; let the caller free it */
1004 return 1;
1005}
1006
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001007int iwl4965_send_add_station(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001008 struct iwl4965_addsta_cmd *sta, u8 flags)
Zhu Yib481de92007-09-25 17:54:57 -07001009{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001010 struct iwl4965_rx_packet *res = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07001011 int rc = 0;
Tomas Winkler857485c2008-03-21 13:53:44 -07001012 struct iwl_host_cmd cmd = {
Zhu Yib481de92007-09-25 17:54:57 -07001013 .id = REPLY_ADD_STA,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001014 .len = sizeof(struct iwl4965_addsta_cmd),
Zhu Yib481de92007-09-25 17:54:57 -07001015 .meta.flags = flags,
1016 .data = sta,
1017 };
1018
1019 if (flags & CMD_ASYNC)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001020 cmd.meta.u.callback = iwl4965_add_sta_sync_callback;
Zhu Yib481de92007-09-25 17:54:57 -07001021 else
1022 cmd.meta.flags |= CMD_WANT_SKB;
1023
Tomas Winkler857485c2008-03-21 13:53:44 -07001024 rc = iwl_send_cmd(priv, &cmd);
Zhu Yib481de92007-09-25 17:54:57 -07001025
1026 if (rc || (flags & CMD_ASYNC))
1027 return rc;
1028
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001029 res = (struct iwl4965_rx_packet *)cmd.meta.u.skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07001030 if (res->hdr.flags & IWL_CMD_FAILED_MSK) {
1031 IWL_ERROR("Bad return from REPLY_ADD_STA (0x%08X)\n",
1032 res->hdr.flags);
1033 rc = -EIO;
1034 }
1035
1036 if (rc == 0) {
1037 switch (res->u.add_sta.status) {
1038 case ADD_STA_SUCCESS_MSK:
1039 IWL_DEBUG_INFO("REPLY_ADD_STA PASSED\n");
1040 break;
1041 default:
1042 rc = -EIO;
1043 IWL_WARNING("REPLY_ADD_STA failed\n");
1044 break;
1045 }
1046 }
1047
1048 priv->alloc_rxb_skb--;
1049 dev_kfree_skb_any(cmd.meta.u.skb);
1050
1051 return rc;
1052}
1053
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001054static void iwl4965_clear_free_frames(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001055{
1056 struct list_head *element;
1057
1058 IWL_DEBUG_INFO("%d frames on pre-allocated heap on clear.\n",
1059 priv->frames_count);
1060
1061 while (!list_empty(&priv->free_frames)) {
1062 element = priv->free_frames.next;
1063 list_del(element);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001064 kfree(list_entry(element, struct iwl4965_frame, list));
Zhu Yib481de92007-09-25 17:54:57 -07001065 priv->frames_count--;
1066 }
1067
1068 if (priv->frames_count) {
1069 IWL_WARNING("%d frames still in use. Did we lose one?\n",
1070 priv->frames_count);
1071 priv->frames_count = 0;
1072 }
1073}
1074
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001075static struct iwl4965_frame *iwl4965_get_free_frame(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001076{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001077 struct iwl4965_frame *frame;
Zhu Yib481de92007-09-25 17:54:57 -07001078 struct list_head *element;
1079 if (list_empty(&priv->free_frames)) {
1080 frame = kzalloc(sizeof(*frame), GFP_KERNEL);
1081 if (!frame) {
1082 IWL_ERROR("Could not allocate frame!\n");
1083 return NULL;
1084 }
1085
1086 priv->frames_count++;
1087 return frame;
1088 }
1089
1090 element = priv->free_frames.next;
1091 list_del(element);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001092 return list_entry(element, struct iwl4965_frame, list);
Zhu Yib481de92007-09-25 17:54:57 -07001093}
1094
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001095static void iwl4965_free_frame(struct iwl_priv *priv, struct iwl4965_frame *frame)
Zhu Yib481de92007-09-25 17:54:57 -07001096{
1097 memset(frame, 0, sizeof(*frame));
1098 list_add(&frame->list, &priv->free_frames);
1099}
1100
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001101unsigned int iwl4965_fill_beacon_frame(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07001102 struct ieee80211_hdr *hdr,
1103 const u8 *dest, int left)
1104{
1105
Tomas Winkler3109ece2008-03-28 16:33:35 -07001106 if (!iwl_is_associated(priv) || !priv->ibss_beacon ||
Zhu Yib481de92007-09-25 17:54:57 -07001107 ((priv->iw_mode != IEEE80211_IF_TYPE_IBSS) &&
1108 (priv->iw_mode != IEEE80211_IF_TYPE_AP)))
1109 return 0;
1110
1111 if (priv->ibss_beacon->len > left)
1112 return 0;
1113
1114 memcpy(hdr, priv->ibss_beacon->data, priv->ibss_beacon->len);
1115
1116 return priv->ibss_beacon->len;
1117}
1118
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001119static u8 iwl4965_rate_get_lowest_plcp(int rate_mask)
Zhu Yib481de92007-09-25 17:54:57 -07001120{
1121 u8 i;
1122
1123 for (i = IWL_RATE_1M_INDEX; i != IWL_RATE_INVALID;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001124 i = iwl4965_rates[i].next_ieee) {
Zhu Yib481de92007-09-25 17:54:57 -07001125 if (rate_mask & (1 << i))
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001126 return iwl4965_rates[i].plcp;
Zhu Yib481de92007-09-25 17:54:57 -07001127 }
1128
1129 return IWL_RATE_INVALID;
1130}
1131
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001132static int iwl4965_send_beacon_cmd(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001133{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001134 struct iwl4965_frame *frame;
Zhu Yib481de92007-09-25 17:54:57 -07001135 unsigned int frame_size;
1136 int rc;
1137 u8 rate;
1138
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001139 frame = iwl4965_get_free_frame(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001140
1141 if (!frame) {
1142 IWL_ERROR("Could not obtain free frame buffer for beacon "
1143 "command.\n");
1144 return -ENOMEM;
1145 }
1146
1147 if (!(priv->staging_rxon.flags & RXON_FLG_BAND_24G_MSK)) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001148 rate = iwl4965_rate_get_lowest_plcp(priv->active_rate_basic &
Zhu Yib481de92007-09-25 17:54:57 -07001149 0xFF0);
1150 if (rate == IWL_INVALID_RATE)
1151 rate = IWL_RATE_6M_PLCP;
1152 } else {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001153 rate = iwl4965_rate_get_lowest_plcp(priv->active_rate_basic & 0xF);
Zhu Yib481de92007-09-25 17:54:57 -07001154 if (rate == IWL_INVALID_RATE)
1155 rate = IWL_RATE_1M_PLCP;
1156 }
1157
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001158 frame_size = iwl4965_hw_get_beacon_cmd(priv, frame, rate);
Zhu Yib481de92007-09-25 17:54:57 -07001159
Tomas Winkler857485c2008-03-21 13:53:44 -07001160 rc = iwl_send_cmd_pdu(priv, REPLY_TX_BEACON, frame_size,
Zhu Yib481de92007-09-25 17:54:57 -07001161 &frame->u.cmd[0]);
1162
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001163 iwl4965_free_frame(priv, frame);
Zhu Yib481de92007-09-25 17:54:57 -07001164
1165 return rc;
1166}
1167
1168/******************************************************************************
1169 *
Zhu Yib481de92007-09-25 17:54:57 -07001170 * Misc. internal state and helper functions
1171 *
1172 ******************************************************************************/
Zhu Yib481de92007-09-25 17:54:57 -07001173
Tomas Winkler5425e492008-04-15 16:01:38 -07001174static void iwl4965_unset_hw_params(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001175{
Tomas Winkler059ff822008-04-14 21:16:14 -07001176 if (priv->shared_virt)
Zhu Yib481de92007-09-25 17:54:57 -07001177 pci_free_consistent(priv->pci_dev,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001178 sizeof(struct iwl4965_shared),
Tomas Winkler059ff822008-04-14 21:16:14 -07001179 priv->shared_virt,
1180 priv->shared_phys);
Zhu Yib481de92007-09-25 17:54:57 -07001181}
1182
1183/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001184 * iwl4965_supported_rate_to_ie - fill in the supported rate in IE field
Zhu Yib481de92007-09-25 17:54:57 -07001185 *
1186 * return : set the bit for each supported rate insert in ie
1187 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001188static u16 iwl4965_supported_rate_to_ie(u8 *ie, u16 supported_rate,
Tomas Winklerc7c46672007-10-18 02:04:15 +02001189 u16 basic_rate, int *left)
Zhu Yib481de92007-09-25 17:54:57 -07001190{
1191 u16 ret_rates = 0, bit;
1192 int i;
Tomas Winklerc7c46672007-10-18 02:04:15 +02001193 u8 *cnt = ie;
1194 u8 *rates = ie + 1;
Zhu Yib481de92007-09-25 17:54:57 -07001195
1196 for (bit = 1, i = 0; i < IWL_RATE_COUNT; i++, bit <<= 1) {
1197 if (bit & supported_rate) {
1198 ret_rates |= bit;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001199 rates[*cnt] = iwl4965_rates[i].ieee |
Tomas Winklerc7c46672007-10-18 02:04:15 +02001200 ((bit & basic_rate) ? 0x80 : 0x00);
1201 (*cnt)++;
1202 (*left)--;
1203 if ((*left <= 0) ||
1204 (*cnt >= IWL_SUPPORTED_RATES_IE_LEN))
Zhu Yib481de92007-09-25 17:54:57 -07001205 break;
1206 }
1207 }
1208
1209 return ret_rates;
1210}
1211
Zhu Yib481de92007-09-25 17:54:57 -07001212/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001213 * iwl4965_fill_probe_req - fill in all required fields and IE for probe request
Zhu Yib481de92007-09-25 17:54:57 -07001214 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001215static u16 iwl4965_fill_probe_req(struct iwl_priv *priv,
Tomas Winkler78330fd2008-02-06 02:37:18 +02001216 enum ieee80211_band band,
1217 struct ieee80211_mgmt *frame,
1218 int left, int is_direct)
Zhu Yib481de92007-09-25 17:54:57 -07001219{
1220 int len = 0;
1221 u8 *pos = NULL;
mabbasbee488d2007-10-25 17:15:42 +08001222 u16 active_rates, ret_rates, cck_rates, active_rate_basic;
Ron Rindjunsky8fb88032007-11-26 16:14:38 +02001223#ifdef CONFIG_IWL4965_HT
Tomas Winkler78330fd2008-02-06 02:37:18 +02001224 const struct ieee80211_supported_band *sband =
1225 iwl4965_get_hw_mode(priv, band);
Ron Rindjunsky8fb88032007-11-26 16:14:38 +02001226#endif /* CONFIG_IWL4965_HT */
Zhu Yib481de92007-09-25 17:54:57 -07001227
1228 /* Make sure there is enough space for the probe request,
1229 * two mandatory IEs and the data */
1230 left -= 24;
1231 if (left < 0)
1232 return 0;
1233 len += 24;
1234
1235 frame->frame_control = cpu_to_le16(IEEE80211_STYPE_PROBE_REQ);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001236 memcpy(frame->da, iwl4965_broadcast_addr, ETH_ALEN);
Zhu Yib481de92007-09-25 17:54:57 -07001237 memcpy(frame->sa, priv->mac_addr, ETH_ALEN);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001238 memcpy(frame->bssid, iwl4965_broadcast_addr, ETH_ALEN);
Zhu Yib481de92007-09-25 17:54:57 -07001239 frame->seq_ctrl = 0;
1240
1241 /* fill in our indirect SSID IE */
1242 /* ...next IE... */
1243
1244 left -= 2;
1245 if (left < 0)
1246 return 0;
1247 len += 2;
1248 pos = &(frame->u.probe_req.variable[0]);
1249 *pos++ = WLAN_EID_SSID;
1250 *pos++ = 0;
1251
1252 /* fill in our direct SSID IE... */
1253 if (is_direct) {
1254 /* ...next IE... */
1255 left -= 2 + priv->essid_len;
1256 if (left < 0)
1257 return 0;
1258 /* ... fill it in... */
1259 *pos++ = WLAN_EID_SSID;
1260 *pos++ = priv->essid_len;
1261 memcpy(pos, priv->essid, priv->essid_len);
1262 pos += priv->essid_len;
1263 len += 2 + priv->essid_len;
1264 }
1265
1266 /* fill in supported rate */
1267 /* ...next IE... */
1268 left -= 2;
1269 if (left < 0)
1270 return 0;
Tomas Winklerc7c46672007-10-18 02:04:15 +02001271
Zhu Yib481de92007-09-25 17:54:57 -07001272 /* ... fill it in... */
1273 *pos++ = WLAN_EID_SUPP_RATES;
1274 *pos = 0;
Tomas Winklerc7c46672007-10-18 02:04:15 +02001275
mabbasbee488d2007-10-25 17:15:42 +08001276 /* exclude 60M rate */
1277 active_rates = priv->rates_mask;
1278 active_rates &= ~IWL_RATE_60M_MASK;
1279
1280 active_rate_basic = active_rates & IWL_BASIC_RATES_MASK;
Zhu Yib481de92007-09-25 17:54:57 -07001281
Tomas Winklerc7c46672007-10-18 02:04:15 +02001282 cck_rates = IWL_CCK_RATES_MASK & active_rates;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001283 ret_rates = iwl4965_supported_rate_to_ie(pos, cck_rates,
mabbasbee488d2007-10-25 17:15:42 +08001284 active_rate_basic, &left);
Tomas Winklerc7c46672007-10-18 02:04:15 +02001285 active_rates &= ~ret_rates;
1286
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001287 ret_rates = iwl4965_supported_rate_to_ie(pos, active_rates,
mabbasbee488d2007-10-25 17:15:42 +08001288 active_rate_basic, &left);
Tomas Winklerc7c46672007-10-18 02:04:15 +02001289 active_rates &= ~ret_rates;
1290
Zhu Yib481de92007-09-25 17:54:57 -07001291 len += 2 + *pos;
1292 pos += (*pos) + 1;
Tomas Winklerc7c46672007-10-18 02:04:15 +02001293 if (active_rates == 0)
Zhu Yib481de92007-09-25 17:54:57 -07001294 goto fill_end;
1295
1296 /* fill in supported extended rate */
1297 /* ...next IE... */
1298 left -= 2;
1299 if (left < 0)
1300 return 0;
1301 /* ... fill it in... */
1302 *pos++ = WLAN_EID_EXT_SUPP_RATES;
1303 *pos = 0;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001304 iwl4965_supported_rate_to_ie(pos, active_rates,
mabbasbee488d2007-10-25 17:15:42 +08001305 active_rate_basic, &left);
Zhu Yib481de92007-09-25 17:54:57 -07001306 if (*pos > 0)
1307 len += 2 + *pos;
1308
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08001309#ifdef CONFIG_IWL4965_HT
Tomas Winkler78330fd2008-02-06 02:37:18 +02001310 if (sband && sband->ht_info.ht_supported) {
1311 struct ieee80211_ht_cap *ht_cap;
Zhu Yib481de92007-09-25 17:54:57 -07001312 pos += (*pos) + 1;
1313 *pos++ = WLAN_EID_HT_CAPABILITY;
Ron Rindjunsky8fb88032007-11-26 16:14:38 +02001314 *pos++ = sizeof(struct ieee80211_ht_cap);
Tomas Winkler78330fd2008-02-06 02:37:18 +02001315 ht_cap = (struct ieee80211_ht_cap *)pos;
1316 ht_cap->cap_info = cpu_to_le16(sband->ht_info.cap);
1317 memcpy(ht_cap->supp_mcs_set, sband->ht_info.supp_mcs_set, 16);
1318 ht_cap->ampdu_params_info =(sband->ht_info.ampdu_factor &
1319 IEEE80211_HT_CAP_AMPDU_FACTOR) |
1320 ((sband->ht_info.ampdu_density << 2) &
1321 IEEE80211_HT_CAP_AMPDU_DENSITY);
Ron Rindjunsky8fb88032007-11-26 16:14:38 +02001322 len += 2 + sizeof(struct ieee80211_ht_cap);
Zhu Yib481de92007-09-25 17:54:57 -07001323 }
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08001324#endif /*CONFIG_IWL4965_HT */
Zhu Yib481de92007-09-25 17:54:57 -07001325
1326 fill_end:
1327 return (u16)len;
1328}
1329
1330/*
1331 * QoS support
1332*/
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001333static int iwl4965_send_qos_params_command(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001334 struct iwl4965_qosparam_cmd *qos)
Zhu Yib481de92007-09-25 17:54:57 -07001335{
1336
Tomas Winkler857485c2008-03-21 13:53:44 -07001337 return iwl_send_cmd_pdu(priv, REPLY_QOS_PARAM,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001338 sizeof(struct iwl4965_qosparam_cmd), qos);
Zhu Yib481de92007-09-25 17:54:57 -07001339}
1340
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001341static void iwl4965_activate_qos(struct iwl_priv *priv, u8 force)
Zhu Yib481de92007-09-25 17:54:57 -07001342{
1343 unsigned long flags;
1344
Zhu Yib481de92007-09-25 17:54:57 -07001345 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
1346 return;
1347
1348 if (!priv->qos_data.qos_enable)
1349 return;
1350
1351 spin_lock_irqsave(&priv->lock, flags);
1352 priv->qos_data.def_qos_parm.qos_flags = 0;
1353
1354 if (priv->qos_data.qos_cap.q_AP.queue_request &&
1355 !priv->qos_data.qos_cap.q_AP.txop_request)
1356 priv->qos_data.def_qos_parm.qos_flags |=
1357 QOS_PARAM_FLG_TXOP_TYPE_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07001358 if (priv->qos_data.qos_active)
1359 priv->qos_data.def_qos_parm.qos_flags |=
1360 QOS_PARAM_FLG_UPDATE_EDCA_MSK;
1361
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08001362#ifdef CONFIG_IWL4965_HT
Ron Rindjunskyfd105e72007-11-26 16:14:39 +02001363 if (priv->current_ht_config.is_ht)
Tomas Winklerf1f1f5c2007-10-25 17:15:26 +08001364 priv->qos_data.def_qos_parm.qos_flags |= QOS_PARAM_FLG_TGN_MSK;
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08001365#endif /* CONFIG_IWL4965_HT */
Tomas Winklerf1f1f5c2007-10-25 17:15:26 +08001366
Zhu Yib481de92007-09-25 17:54:57 -07001367 spin_unlock_irqrestore(&priv->lock, flags);
1368
Tomas Winkler3109ece2008-03-28 16:33:35 -07001369 if (force || iwl_is_associated(priv)) {
Tomas Winklerf1f1f5c2007-10-25 17:15:26 +08001370 IWL_DEBUG_QOS("send QoS cmd with Qos active=%d FLAGS=0x%X\n",
1371 priv->qos_data.qos_active,
1372 priv->qos_data.def_qos_parm.qos_flags);
Zhu Yib481de92007-09-25 17:54:57 -07001373
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001374 iwl4965_send_qos_params_command(priv,
Zhu Yib481de92007-09-25 17:54:57 -07001375 &(priv->qos_data.def_qos_parm));
1376 }
1377}
1378
Zhu Yib481de92007-09-25 17:54:57 -07001379/*
1380 * Power management (not Tx power!) functions
1381 */
1382#define MSEC_TO_USEC 1024
1383
1384#define NOSLP __constant_cpu_to_le16(0), 0, 0
1385#define SLP IWL_POWER_DRIVER_ALLOW_SLEEP_MSK, 0, 0
1386#define SLP_TIMEOUT(T) __constant_cpu_to_le32((T) * MSEC_TO_USEC)
1387#define SLP_VEC(X0, X1, X2, X3, X4) {__constant_cpu_to_le32(X0), \
1388 __constant_cpu_to_le32(X1), \
1389 __constant_cpu_to_le32(X2), \
1390 __constant_cpu_to_le32(X3), \
1391 __constant_cpu_to_le32(X4)}
1392
1393
1394/* default power management (not Tx power) table values */
1395/* for tim 0-10 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001396static struct iwl4965_power_vec_entry range_0[IWL_POWER_AC] = {
Zhu Yib481de92007-09-25 17:54:57 -07001397 {{NOSLP, SLP_TIMEOUT(0), SLP_TIMEOUT(0), SLP_VEC(0, 0, 0, 0, 0)}, 0},
1398 {{SLP, SLP_TIMEOUT(200), SLP_TIMEOUT(500), SLP_VEC(1, 2, 3, 4, 4)}, 0},
1399 {{SLP, SLP_TIMEOUT(200), SLP_TIMEOUT(300), SLP_VEC(2, 4, 6, 7, 7)}, 0},
1400 {{SLP, SLP_TIMEOUT(50), SLP_TIMEOUT(100), SLP_VEC(2, 6, 9, 9, 10)}, 0},
1401 {{SLP, SLP_TIMEOUT(50), SLP_TIMEOUT(25), SLP_VEC(2, 7, 9, 9, 10)}, 1},
1402 {{SLP, SLP_TIMEOUT(25), SLP_TIMEOUT(25), SLP_VEC(4, 7, 10, 10, 10)}, 1}
1403};
1404
1405/* for tim > 10 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001406static struct iwl4965_power_vec_entry range_1[IWL_POWER_AC] = {
Zhu Yib481de92007-09-25 17:54:57 -07001407 {{NOSLP, SLP_TIMEOUT(0), SLP_TIMEOUT(0), SLP_VEC(0, 0, 0, 0, 0)}, 0},
1408 {{SLP, SLP_TIMEOUT(200), SLP_TIMEOUT(500),
1409 SLP_VEC(1, 2, 3, 4, 0xFF)}, 0},
1410 {{SLP, SLP_TIMEOUT(200), SLP_TIMEOUT(300),
1411 SLP_VEC(2, 4, 6, 7, 0xFF)}, 0},
1412 {{SLP, SLP_TIMEOUT(50), SLP_TIMEOUT(100),
1413 SLP_VEC(2, 6, 9, 9, 0xFF)}, 0},
1414 {{SLP, SLP_TIMEOUT(50), SLP_TIMEOUT(25), SLP_VEC(2, 7, 9, 9, 0xFF)}, 0},
1415 {{SLP, SLP_TIMEOUT(25), SLP_TIMEOUT(25),
1416 SLP_VEC(4, 7, 10, 10, 0xFF)}, 0}
1417};
1418
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001419int iwl4965_power_init_handle(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001420{
1421 int rc = 0, i;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001422 struct iwl4965_power_mgr *pow_data;
1423 int size = sizeof(struct iwl4965_power_vec_entry) * IWL_POWER_AC;
Zhu Yib481de92007-09-25 17:54:57 -07001424 u16 pci_pm;
1425
1426 IWL_DEBUG_POWER("Initialize power \n");
1427
1428 pow_data = &(priv->power_data);
1429
1430 memset(pow_data, 0, sizeof(*pow_data));
1431
1432 pow_data->active_index = IWL_POWER_RANGE_0;
1433 pow_data->dtim_val = 0xffff;
1434
1435 memcpy(&pow_data->pwr_range_0[0], &range_0[0], size);
1436 memcpy(&pow_data->pwr_range_1[0], &range_1[0], size);
1437
1438 rc = pci_read_config_word(priv->pci_dev, PCI_LINK_CTRL, &pci_pm);
1439 if (rc != 0)
1440 return 0;
1441 else {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001442 struct iwl4965_powertable_cmd *cmd;
Zhu Yib481de92007-09-25 17:54:57 -07001443
1444 IWL_DEBUG_POWER("adjust power command flags\n");
1445
1446 for (i = 0; i < IWL_POWER_AC; i++) {
1447 cmd = &pow_data->pwr_range_0[i].cmd;
1448
1449 if (pci_pm & 0x1)
1450 cmd->flags &= ~IWL_POWER_PCI_PM_MSK;
1451 else
1452 cmd->flags |= IWL_POWER_PCI_PM_MSK;
1453 }
1454 }
1455 return rc;
1456}
1457
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001458static int iwl4965_update_power_cmd(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001459 struct iwl4965_powertable_cmd *cmd, u32 mode)
Zhu Yib481de92007-09-25 17:54:57 -07001460{
1461 int rc = 0, i;
1462 u8 skip;
1463 u32 max_sleep = 0;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001464 struct iwl4965_power_vec_entry *range;
Zhu Yib481de92007-09-25 17:54:57 -07001465 u8 period = 0;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001466 struct iwl4965_power_mgr *pow_data;
Zhu Yib481de92007-09-25 17:54:57 -07001467
1468 if (mode > IWL_POWER_INDEX_5) {
1469 IWL_DEBUG_POWER("Error invalid power mode \n");
1470 return -1;
1471 }
1472 pow_data = &(priv->power_data);
1473
1474 if (pow_data->active_index == IWL_POWER_RANGE_0)
1475 range = &pow_data->pwr_range_0[0];
1476 else
1477 range = &pow_data->pwr_range_1[1];
1478
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001479 memcpy(cmd, &range[mode].cmd, sizeof(struct iwl4965_powertable_cmd));
Zhu Yib481de92007-09-25 17:54:57 -07001480
1481#ifdef IWL_MAC80211_DISABLE
1482 if (priv->assoc_network != NULL) {
1483 unsigned long flags;
1484
1485 period = priv->assoc_network->tim.tim_period;
1486 }
1487#endif /*IWL_MAC80211_DISABLE */
1488 skip = range[mode].no_dtim;
1489
1490 if (period == 0) {
1491 period = 1;
1492 skip = 0;
1493 }
1494
1495 if (skip == 0) {
1496 max_sleep = period;
1497 cmd->flags &= ~IWL_POWER_SLEEP_OVER_DTIM_MSK;
1498 } else {
1499 __le32 slp_itrvl = cmd->sleep_interval[IWL_POWER_VEC_SIZE - 1];
1500 max_sleep = (le32_to_cpu(slp_itrvl) / period) * period;
1501 cmd->flags |= IWL_POWER_SLEEP_OVER_DTIM_MSK;
1502 }
1503
1504 for (i = 0; i < IWL_POWER_VEC_SIZE; i++) {
1505 if (le32_to_cpu(cmd->sleep_interval[i]) > max_sleep)
1506 cmd->sleep_interval[i] = cpu_to_le32(max_sleep);
1507 }
1508
1509 IWL_DEBUG_POWER("Flags value = 0x%08X\n", cmd->flags);
1510 IWL_DEBUG_POWER("Tx timeout = %u\n", le32_to_cpu(cmd->tx_data_timeout));
1511 IWL_DEBUG_POWER("Rx timeout = %u\n", le32_to_cpu(cmd->rx_data_timeout));
1512 IWL_DEBUG_POWER("Sleep interval vector = { %d , %d , %d , %d , %d }\n",
1513 le32_to_cpu(cmd->sleep_interval[0]),
1514 le32_to_cpu(cmd->sleep_interval[1]),
1515 le32_to_cpu(cmd->sleep_interval[2]),
1516 le32_to_cpu(cmd->sleep_interval[3]),
1517 le32_to_cpu(cmd->sleep_interval[4]));
1518
1519 return rc;
1520}
1521
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001522static int iwl4965_send_power_mode(struct iwl_priv *priv, u32 mode)
Zhu Yib481de92007-09-25 17:54:57 -07001523{
John W. Linville9a62f732007-11-15 16:27:36 -05001524 u32 uninitialized_var(final_mode);
Zhu Yib481de92007-09-25 17:54:57 -07001525 int rc;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001526 struct iwl4965_powertable_cmd cmd;
Zhu Yib481de92007-09-25 17:54:57 -07001527
1528 /* If on battery, set to 3,
Ian Schram01ebd062007-10-25 17:15:22 +08001529 * if plugged into AC power, set to CAM ("continuously aware mode"),
Zhu Yib481de92007-09-25 17:54:57 -07001530 * else user level */
1531 switch (mode) {
1532 case IWL_POWER_BATTERY:
1533 final_mode = IWL_POWER_INDEX_3;
1534 break;
1535 case IWL_POWER_AC:
1536 final_mode = IWL_POWER_MODE_CAM;
1537 break;
1538 default:
1539 final_mode = mode;
1540 break;
1541 }
1542
1543 cmd.keep_alive_beacons = 0;
1544
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001545 iwl4965_update_power_cmd(priv, &cmd, final_mode);
Zhu Yib481de92007-09-25 17:54:57 -07001546
Tomas Winkler857485c2008-03-21 13:53:44 -07001547 rc = iwl_send_cmd_pdu(priv, POWER_TABLE_CMD, sizeof(cmd), &cmd);
Zhu Yib481de92007-09-25 17:54:57 -07001548
1549 if (final_mode == IWL_POWER_MODE_CAM)
1550 clear_bit(STATUS_POWER_PMI, &priv->status);
1551 else
1552 set_bit(STATUS_POWER_PMI, &priv->status);
1553
1554 return rc;
1555}
1556
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001557int iwl4965_is_network_packet(struct iwl_priv *priv, struct ieee80211_hdr *header)
Zhu Yib481de92007-09-25 17:54:57 -07001558{
1559 /* Filter incoming packets to determine if they are targeted toward
1560 * this network, discarding packets coming from ourselves */
1561 switch (priv->iw_mode) {
1562 case IEEE80211_IF_TYPE_IBSS: /* Header: Dest. | Source | BSSID */
1563 /* packets from our adapter are dropped (echo) */
1564 if (!compare_ether_addr(header->addr2, priv->mac_addr))
1565 return 0;
1566 /* {broad,multi}cast packets to our IBSS go through */
1567 if (is_multicast_ether_addr(header->addr1))
1568 return !compare_ether_addr(header->addr3, priv->bssid);
1569 /* packets to our adapter go through */
1570 return !compare_ether_addr(header->addr1, priv->mac_addr);
1571 case IEEE80211_IF_TYPE_STA: /* Header: Dest. | AP{BSSID} | Source */
1572 /* packets from our adapter are dropped (echo) */
1573 if (!compare_ether_addr(header->addr3, priv->mac_addr))
1574 return 0;
1575 /* {broad,multi}cast packets to our BSS go through */
1576 if (is_multicast_ether_addr(header->addr1))
1577 return !compare_ether_addr(header->addr2, priv->bssid);
1578 /* packets to our adapter go through */
1579 return !compare_ether_addr(header->addr1, priv->mac_addr);
Tomas Winkler69dc5d92008-03-25 16:33:41 -07001580 default:
1581 break;
Zhu Yib481de92007-09-25 17:54:57 -07001582 }
1583
1584 return 1;
1585}
1586
1587#define TX_STATUS_ENTRY(x) case TX_STATUS_FAIL_ ## x: return #x
1588
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001589static const char *iwl4965_get_tx_fail_reason(u32 status)
Zhu Yib481de92007-09-25 17:54:57 -07001590{
1591 switch (status & TX_STATUS_MSK) {
1592 case TX_STATUS_SUCCESS:
1593 return "SUCCESS";
1594 TX_STATUS_ENTRY(SHORT_LIMIT);
1595 TX_STATUS_ENTRY(LONG_LIMIT);
1596 TX_STATUS_ENTRY(FIFO_UNDERRUN);
1597 TX_STATUS_ENTRY(MGMNT_ABORT);
1598 TX_STATUS_ENTRY(NEXT_FRAG);
1599 TX_STATUS_ENTRY(LIFE_EXPIRE);
1600 TX_STATUS_ENTRY(DEST_PS);
1601 TX_STATUS_ENTRY(ABORTED);
1602 TX_STATUS_ENTRY(BT_RETRY);
1603 TX_STATUS_ENTRY(STA_INVALID);
1604 TX_STATUS_ENTRY(FRAG_DROPPED);
1605 TX_STATUS_ENTRY(TID_DISABLE);
1606 TX_STATUS_ENTRY(FRAME_FLUSHED);
1607 TX_STATUS_ENTRY(INSUFFICIENT_CF_POLL);
1608 TX_STATUS_ENTRY(TX_LOCKED);
1609 TX_STATUS_ENTRY(NO_BEACON_ON_RADAR);
1610 }
1611
1612 return "UNKNOWN";
1613}
1614
1615/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001616 * iwl4965_scan_cancel - Cancel any currently executing HW scan
Zhu Yib481de92007-09-25 17:54:57 -07001617 *
1618 * NOTE: priv->mutex is not required before calling this function
1619 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001620static int iwl4965_scan_cancel(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001621{
1622 if (!test_bit(STATUS_SCAN_HW, &priv->status)) {
1623 clear_bit(STATUS_SCANNING, &priv->status);
1624 return 0;
1625 }
1626
1627 if (test_bit(STATUS_SCANNING, &priv->status)) {
1628 if (!test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
1629 IWL_DEBUG_SCAN("Queuing scan abort.\n");
1630 set_bit(STATUS_SCAN_ABORTING, &priv->status);
1631 queue_work(priv->workqueue, &priv->abort_scan);
1632
1633 } else
1634 IWL_DEBUG_SCAN("Scan abort already in progress.\n");
1635
1636 return test_bit(STATUS_SCANNING, &priv->status);
1637 }
1638
1639 return 0;
1640}
1641
1642/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001643 * iwl4965_scan_cancel_timeout - Cancel any currently executing HW scan
Zhu Yib481de92007-09-25 17:54:57 -07001644 * @ms: amount of time to wait (in milliseconds) for scan to abort
1645 *
1646 * NOTE: priv->mutex must be held before calling this function
1647 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001648static int iwl4965_scan_cancel_timeout(struct iwl_priv *priv, unsigned long ms)
Zhu Yib481de92007-09-25 17:54:57 -07001649{
1650 unsigned long now = jiffies;
1651 int ret;
1652
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001653 ret = iwl4965_scan_cancel(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001654 if (ret && ms) {
1655 mutex_unlock(&priv->mutex);
1656 while (!time_after(jiffies, now + msecs_to_jiffies(ms)) &&
1657 test_bit(STATUS_SCANNING, &priv->status))
1658 msleep(1);
1659 mutex_lock(&priv->mutex);
1660
1661 return test_bit(STATUS_SCANNING, &priv->status);
1662 }
1663
1664 return ret;
1665}
1666
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001667static void iwl4965_sequence_reset(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001668{
1669 /* Reset ieee stats */
1670
1671 /* We don't reset the net_device_stats (ieee->stats) on
1672 * re-association */
1673
1674 priv->last_seq_num = -1;
1675 priv->last_frag_num = -1;
1676 priv->last_packet_time = 0;
1677
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001678 iwl4965_scan_cancel(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001679}
1680
1681#define MAX_UCODE_BEACON_INTERVAL 4096
1682#define INTEL_CONN_LISTEN_INTERVAL __constant_cpu_to_le16(0xA)
1683
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001684static __le16 iwl4965_adjust_beacon_interval(u16 beacon_val)
Zhu Yib481de92007-09-25 17:54:57 -07001685{
1686 u16 new_val = 0;
1687 u16 beacon_factor = 0;
1688
1689 beacon_factor =
1690 (beacon_val + MAX_UCODE_BEACON_INTERVAL)
1691 / MAX_UCODE_BEACON_INTERVAL;
1692 new_val = beacon_val / beacon_factor;
1693
1694 return cpu_to_le16(new_val);
1695}
1696
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001697static void iwl4965_setup_rxon_timing(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001698{
1699 u64 interval_tm_unit;
1700 u64 tsf, result;
1701 unsigned long flags;
1702 struct ieee80211_conf *conf = NULL;
1703 u16 beacon_int = 0;
1704
1705 conf = ieee80211_get_hw_conf(priv->hw);
1706
1707 spin_lock_irqsave(&priv->lock, flags);
Tomas Winkler3109ece2008-03-28 16:33:35 -07001708 priv->rxon_timing.timestamp.dw[1] = cpu_to_le32(priv->timestamp >> 32);
1709 priv->rxon_timing.timestamp.dw[0] =
1710 cpu_to_le32(priv->timestamp & 0xFFFFFFFF);
Zhu Yib481de92007-09-25 17:54:57 -07001711
1712 priv->rxon_timing.listen_interval = INTEL_CONN_LISTEN_INTERVAL;
1713
Tomas Winkler3109ece2008-03-28 16:33:35 -07001714 tsf = priv->timestamp;
Zhu Yib481de92007-09-25 17:54:57 -07001715
1716 beacon_int = priv->beacon_int;
1717 spin_unlock_irqrestore(&priv->lock, flags);
1718
1719 if (priv->iw_mode == IEEE80211_IF_TYPE_STA) {
1720 if (beacon_int == 0) {
1721 priv->rxon_timing.beacon_interval = cpu_to_le16(100);
1722 priv->rxon_timing.beacon_init_val = cpu_to_le32(102400);
1723 } else {
1724 priv->rxon_timing.beacon_interval =
1725 cpu_to_le16(beacon_int);
1726 priv->rxon_timing.beacon_interval =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001727 iwl4965_adjust_beacon_interval(
Zhu Yib481de92007-09-25 17:54:57 -07001728 le16_to_cpu(priv->rxon_timing.beacon_interval));
1729 }
1730
1731 priv->rxon_timing.atim_window = 0;
1732 } else {
1733 priv->rxon_timing.beacon_interval =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001734 iwl4965_adjust_beacon_interval(conf->beacon_int);
Zhu Yib481de92007-09-25 17:54:57 -07001735 /* TODO: we need to get atim_window from upper stack
1736 * for now we set to 0 */
1737 priv->rxon_timing.atim_window = 0;
1738 }
1739
1740 interval_tm_unit =
1741 (le16_to_cpu(priv->rxon_timing.beacon_interval) * 1024);
1742 result = do_div(tsf, interval_tm_unit);
1743 priv->rxon_timing.beacon_init_val =
1744 cpu_to_le32((u32) ((u64) interval_tm_unit - result));
1745
1746 IWL_DEBUG_ASSOC
1747 ("beacon interval %d beacon timer %d beacon tim %d\n",
1748 le16_to_cpu(priv->rxon_timing.beacon_interval),
1749 le32_to_cpu(priv->rxon_timing.beacon_init_val),
1750 le16_to_cpu(priv->rxon_timing.atim_window));
1751}
1752
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001753static int iwl4965_scan_initiate(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001754{
1755 if (priv->iw_mode == IEEE80211_IF_TYPE_AP) {
1756 IWL_ERROR("APs don't scan.\n");
1757 return 0;
1758 }
1759
Tomas Winklerfee12472008-04-03 16:05:21 -07001760 if (!iwl_is_ready_rf(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07001761 IWL_DEBUG_SCAN("Aborting scan due to not ready.\n");
1762 return -EIO;
1763 }
1764
1765 if (test_bit(STATUS_SCANNING, &priv->status)) {
1766 IWL_DEBUG_SCAN("Scan already in progress.\n");
1767 return -EAGAIN;
1768 }
1769
1770 if (test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
1771 IWL_DEBUG_SCAN("Scan request while abort pending. "
1772 "Queuing.\n");
1773 return -EAGAIN;
1774 }
1775
1776 IWL_DEBUG_INFO("Starting scan...\n");
Ron Rindjunsky66b50042008-06-25 16:46:31 +08001777 if (priv->cfg->sku & IWL_SKU_G)
1778 priv->scan_bands |= BIT(IEEE80211_BAND_2GHZ);
1779 if (priv->cfg->sku & IWL_SKU_A)
1780 priv->scan_bands |= BIT(IEEE80211_BAND_5GHZ);
Zhu Yib481de92007-09-25 17:54:57 -07001781 set_bit(STATUS_SCANNING, &priv->status);
1782 priv->scan_start = jiffies;
1783 priv->scan_pass_start = priv->scan_start;
1784
1785 queue_work(priv->workqueue, &priv->request_scan);
1786
1787 return 0;
1788}
1789
Zhu Yib481de92007-09-25 17:54:57 -07001790
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001791static void iwl4965_set_flags_for_phymode(struct iwl_priv *priv,
Johannes Berg8318d782008-01-24 19:38:38 +01001792 enum ieee80211_band band)
Zhu Yib481de92007-09-25 17:54:57 -07001793{
Johannes Berg8318d782008-01-24 19:38:38 +01001794 if (band == IEEE80211_BAND_5GHZ) {
Zhu Yib481de92007-09-25 17:54:57 -07001795 priv->staging_rxon.flags &=
1796 ~(RXON_FLG_BAND_24G_MSK | RXON_FLG_AUTO_DETECT_MSK
1797 | RXON_FLG_CCK_MSK);
1798 priv->staging_rxon.flags |= RXON_FLG_SHORT_SLOT_MSK;
1799 } else {
Reinette Chatre508e32e2008-04-14 21:16:13 -07001800 /* Copied from iwl4965_post_associate() */
Zhu Yib481de92007-09-25 17:54:57 -07001801 if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_SLOT_TIME)
1802 priv->staging_rxon.flags |= RXON_FLG_SHORT_SLOT_MSK;
1803 else
1804 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
1805
1806 if (priv->iw_mode == IEEE80211_IF_TYPE_IBSS)
1807 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
1808
1809 priv->staging_rxon.flags |= RXON_FLG_BAND_24G_MSK;
1810 priv->staging_rxon.flags |= RXON_FLG_AUTO_DETECT_MSK;
1811 priv->staging_rxon.flags &= ~RXON_FLG_CCK_MSK;
1812 }
1813}
1814
1815/*
Ian Schram01ebd062007-10-25 17:15:22 +08001816 * initialize rxon structure with default values from eeprom
Zhu Yib481de92007-09-25 17:54:57 -07001817 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001818static void iwl4965_connection_init_rx_config(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001819{
Assaf Kraussbf85ea42008-03-14 10:38:49 -07001820 const struct iwl_channel_info *ch_info;
Zhu Yib481de92007-09-25 17:54:57 -07001821
1822 memset(&priv->staging_rxon, 0, sizeof(priv->staging_rxon));
1823
1824 switch (priv->iw_mode) {
1825 case IEEE80211_IF_TYPE_AP:
1826 priv->staging_rxon.dev_type = RXON_DEV_TYPE_AP;
1827 break;
1828
1829 case IEEE80211_IF_TYPE_STA:
1830 priv->staging_rxon.dev_type = RXON_DEV_TYPE_ESS;
1831 priv->staging_rxon.filter_flags = RXON_FILTER_ACCEPT_GRP_MSK;
1832 break;
1833
1834 case IEEE80211_IF_TYPE_IBSS:
1835 priv->staging_rxon.dev_type = RXON_DEV_TYPE_IBSS;
1836 priv->staging_rxon.flags = RXON_FLG_SHORT_PREAMBLE_MSK;
1837 priv->staging_rxon.filter_flags = RXON_FILTER_BCON_AWARE_MSK |
1838 RXON_FILTER_ACCEPT_GRP_MSK;
1839 break;
1840
1841 case IEEE80211_IF_TYPE_MNTR:
1842 priv->staging_rxon.dev_type = RXON_DEV_TYPE_SNIFFER;
1843 priv->staging_rxon.filter_flags = RXON_FILTER_PROMISC_MSK |
1844 RXON_FILTER_CTL2HOST_MSK | RXON_FILTER_ACCEPT_GRP_MSK;
1845 break;
Tomas Winkler69dc5d92008-03-25 16:33:41 -07001846 default:
1847 IWL_ERROR("Unsupported interface type %d\n", priv->iw_mode);
1848 break;
Zhu Yib481de92007-09-25 17:54:57 -07001849 }
1850
1851#if 0
1852 /* TODO: Figure out when short_preamble would be set and cache from
1853 * that */
1854 if (!hw_to_local(priv->hw)->short_preamble)
1855 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_PREAMBLE_MSK;
1856 else
1857 priv->staging_rxon.flags |= RXON_FLG_SHORT_PREAMBLE_MSK;
1858#endif
1859
Assaf Krauss8622e702008-03-21 13:53:43 -07001860 ch_info = iwl_get_channel_info(priv, priv->band,
Zhu Yib481de92007-09-25 17:54:57 -07001861 le16_to_cpu(priv->staging_rxon.channel));
1862
1863 if (!ch_info)
1864 ch_info = &priv->channel_info[0];
1865
1866 /*
1867 * in some case A channels are all non IBSS
1868 * in this case force B/G channel
1869 */
1870 if ((priv->iw_mode == IEEE80211_IF_TYPE_IBSS) &&
1871 !(is_channel_ibss(ch_info)))
1872 ch_info = &priv->channel_info[0];
1873
1874 priv->staging_rxon.channel = cpu_to_le16(ch_info->channel);
Johannes Berg8318d782008-01-24 19:38:38 +01001875 priv->band = ch_info->band;
Zhu Yib481de92007-09-25 17:54:57 -07001876
Johannes Berg8318d782008-01-24 19:38:38 +01001877 iwl4965_set_flags_for_phymode(priv, priv->band);
Zhu Yib481de92007-09-25 17:54:57 -07001878
1879 priv->staging_rxon.ofdm_basic_rates =
1880 (IWL_OFDM_RATES_MASK >> IWL_FIRST_OFDM_RATE) & 0xFF;
1881 priv->staging_rxon.cck_basic_rates =
1882 (IWL_CCK_RATES_MASK >> IWL_FIRST_CCK_RATE) & 0xF;
1883
1884 priv->staging_rxon.flags &= ~(RXON_FLG_CHANNEL_MODE_MIXED_MSK |
1885 RXON_FLG_CHANNEL_MODE_PURE_40_MSK);
1886 memcpy(priv->staging_rxon.node_addr, priv->mac_addr, ETH_ALEN);
1887 memcpy(priv->staging_rxon.wlap_bssid_addr, priv->mac_addr, ETH_ALEN);
1888 priv->staging_rxon.ofdm_ht_single_stream_basic_rates = 0xff;
1889 priv->staging_rxon.ofdm_ht_dual_stream_basic_rates = 0xff;
1890 iwl4965_set_rxon_chain(priv);
1891}
1892
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001893static int iwl4965_set_mode(struct iwl_priv *priv, int mode)
Zhu Yib481de92007-09-25 17:54:57 -07001894{
Zhu Yib481de92007-09-25 17:54:57 -07001895 if (mode == IEEE80211_IF_TYPE_IBSS) {
Assaf Kraussbf85ea42008-03-14 10:38:49 -07001896 const struct iwl_channel_info *ch_info;
Zhu Yib481de92007-09-25 17:54:57 -07001897
Assaf Krauss8622e702008-03-21 13:53:43 -07001898 ch_info = iwl_get_channel_info(priv,
Johannes Berg8318d782008-01-24 19:38:38 +01001899 priv->band,
Zhu Yib481de92007-09-25 17:54:57 -07001900 le16_to_cpu(priv->staging_rxon.channel));
1901
1902 if (!ch_info || !is_channel_ibss(ch_info)) {
1903 IWL_ERROR("channel %d not IBSS channel\n",
1904 le16_to_cpu(priv->staging_rxon.channel));
1905 return -EINVAL;
1906 }
1907 }
1908
Zhu Yib481de92007-09-25 17:54:57 -07001909 priv->iw_mode = mode;
1910
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001911 iwl4965_connection_init_rx_config(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001912 memcpy(priv->staging_rxon.node_addr, priv->mac_addr, ETH_ALEN);
1913
Assaf Kraussbf85ea42008-03-14 10:38:49 -07001914 iwlcore_clear_stations_table(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001915
Mohamed Abbasfde35712007-11-29 11:10:15 +08001916 /* dont commit rxon if rf-kill is on*/
Tomas Winklerfee12472008-04-03 16:05:21 -07001917 if (!iwl_is_ready_rf(priv))
Mohamed Abbasfde35712007-11-29 11:10:15 +08001918 return -EAGAIN;
1919
1920 cancel_delayed_work(&priv->scan_check);
1921 if (iwl4965_scan_cancel_timeout(priv, 100)) {
1922 IWL_WARNING("Aborted scan still in progress after 100ms\n");
1923 IWL_DEBUG_MAC80211("leaving - scan abort failed.\n");
1924 return -EAGAIN;
1925 }
1926
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001927 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001928
1929 return 0;
1930}
1931
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001932static void iwl4965_build_tx_cmd_hwcrypto(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07001933 struct ieee80211_tx_control *ctl,
Tomas Winkler857485c2008-03-21 13:53:44 -07001934 struct iwl_cmd *cmd,
Zhu Yib481de92007-09-25 17:54:57 -07001935 struct sk_buff *skb_frag,
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07001936 int sta_id)
Zhu Yib481de92007-09-25 17:54:57 -07001937{
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07001938 struct iwl4965_hw_key *keyinfo = &priv->stations[sta_id].keyinfo;
Emmanuel Grumbach6974e362008-04-14 21:16:06 -07001939 struct iwl_wep_key *wepkey;
1940 int keyidx = 0;
1941
1942 BUG_ON(ctl->key_idx > 3);
Zhu Yib481de92007-09-25 17:54:57 -07001943
1944 switch (keyinfo->alg) {
1945 case ALG_CCMP:
1946 cmd->cmd.tx.sec_ctl = TX_CMD_SEC_CCM;
1947 memcpy(cmd->cmd.tx.key, keyinfo->key, keyinfo->keylen);
Max Stepanov8236e182008-03-12 16:58:48 -07001948 if (ctl->flags & IEEE80211_TXCTL_AMPDU)
1949 cmd->cmd.tx.tx_flags |= TX_CMD_FLG_AGG_CCMP_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07001950 IWL_DEBUG_TX("tx_cmd with aes hwcrypto\n");
1951 break;
1952
1953 case ALG_TKIP:
Zhu Yib481de92007-09-25 17:54:57 -07001954 cmd->cmd.tx.sec_ctl = TX_CMD_SEC_TKIP;
Emmanuel Grumbach2bc75082008-03-19 16:41:45 -07001955 ieee80211_get_tkip_key(keyinfo->conf, skb_frag,
1956 IEEE80211_TKIP_P2_KEY, cmd->cmd.tx.key);
1957 IWL_DEBUG_TX("tx_cmd with tkip hwcrypto\n");
Zhu Yib481de92007-09-25 17:54:57 -07001958 break;
1959
1960 case ALG_WEP:
Emmanuel Grumbach6974e362008-04-14 21:16:06 -07001961 wepkey = &priv->wep_keys[ctl->key_idx];
1962 cmd->cmd.tx.sec_ctl = 0;
1963 if (priv->default_wep_key) {
1964 /* the WEP key was sent as static */
1965 keyidx = ctl->key_idx;
1966 memcpy(&cmd->cmd.tx.key[3], wepkey->key,
1967 wepkey->key_size);
1968 if (wepkey->key_size == WEP_KEY_LEN_128)
1969 cmd->cmd.tx.sec_ctl |= TX_CMD_SEC_KEY128;
1970 } else {
Emmanuel Grumbach0211ddd2008-04-14 21:16:07 -07001971 /* the WEP key was sent as dynamic */
1972 keyidx = keyinfo->keyidx;
1973 memcpy(&cmd->cmd.tx.key[3], keyinfo->key,
1974 keyinfo->keylen);
1975 if (keyinfo->keylen == WEP_KEY_LEN_128)
1976 cmd->cmd.tx.sec_ctl |= TX_CMD_SEC_KEY128;
Emmanuel Grumbach6974e362008-04-14 21:16:06 -07001977 }
Zhu Yib481de92007-09-25 17:54:57 -07001978
Emmanuel Grumbach6974e362008-04-14 21:16:06 -07001979 cmd->cmd.tx.sec_ctl |= (TX_CMD_SEC_WEP |
1980 (keyidx & TX_CMD_SEC_MSK) << TX_CMD_SEC_SHIFT);
Zhu Yib481de92007-09-25 17:54:57 -07001981
1982 IWL_DEBUG_TX("Configuring packet for WEP encryption "
Emmanuel Grumbach6974e362008-04-14 21:16:06 -07001983 "with key %d\n", keyidx);
Zhu Yib481de92007-09-25 17:54:57 -07001984 break;
1985
Zhu Yib481de92007-09-25 17:54:57 -07001986 default:
1987 printk(KERN_ERR "Unknown encode alg %d\n", keyinfo->alg);
1988 break;
1989 }
1990}
1991
1992/*
1993 * handle build REPLY_TX command notification.
1994 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001995static void iwl4965_build_tx_cmd_basic(struct iwl_priv *priv,
Tomas Winkler857485c2008-03-21 13:53:44 -07001996 struct iwl_cmd *cmd,
Zhu Yib481de92007-09-25 17:54:57 -07001997 struct ieee80211_tx_control *ctrl,
1998 struct ieee80211_hdr *hdr,
1999 int is_unicast, u8 std_id)
2000{
2001 __le16 *qc;
2002 u16 fc = le16_to_cpu(hdr->frame_control);
2003 __le32 tx_flags = cmd->cmd.tx.tx_flags;
2004
2005 cmd->cmd.tx.stop_time.life_time = TX_CMD_LIFE_TIME_INFINITE;
2006 if (!(ctrl->flags & IEEE80211_TXCTL_NO_ACK)) {
2007 tx_flags |= TX_CMD_FLG_ACK_MSK;
2008 if ((fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_MGMT)
2009 tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
2010 if (ieee80211_is_probe_response(fc) &&
2011 !(le16_to_cpu(hdr->seq_ctrl) & 0xf))
2012 tx_flags |= TX_CMD_FLG_TSF_MSK;
2013 } else {
2014 tx_flags &= (~TX_CMD_FLG_ACK_MSK);
2015 tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
2016 }
2017
Tomas Winkler87e4f7d2008-01-14 17:46:16 -08002018 if (ieee80211_is_back_request(fc))
2019 tx_flags |= TX_CMD_FLG_ACK_MSK | TX_CMD_FLG_IMM_BA_RSP_MASK;
2020
2021
Zhu Yib481de92007-09-25 17:54:57 -07002022 cmd->cmd.tx.sta_id = std_id;
2023 if (ieee80211_get_morefrag(hdr))
2024 tx_flags |= TX_CMD_FLG_MORE_FRAG_MSK;
2025
2026 qc = ieee80211_get_qos_ctrl(hdr);
2027 if (qc) {
2028 cmd->cmd.tx.tid_tspec = (u8) (le16_to_cpu(*qc) & 0xf);
2029 tx_flags &= ~TX_CMD_FLG_SEQ_CTL_MSK;
2030 } else
2031 tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
2032
2033 if (ctrl->flags & IEEE80211_TXCTL_USE_RTS_CTS) {
2034 tx_flags |= TX_CMD_FLG_RTS_MSK;
2035 tx_flags &= ~TX_CMD_FLG_CTS_MSK;
2036 } else if (ctrl->flags & IEEE80211_TXCTL_USE_CTS_PROTECT) {
2037 tx_flags &= ~TX_CMD_FLG_RTS_MSK;
2038 tx_flags |= TX_CMD_FLG_CTS_MSK;
2039 }
2040
2041 if ((tx_flags & TX_CMD_FLG_RTS_MSK) || (tx_flags & TX_CMD_FLG_CTS_MSK))
2042 tx_flags |= TX_CMD_FLG_FULL_TXOP_PROT_MSK;
2043
2044 tx_flags &= ~(TX_CMD_FLG_ANT_SEL_MSK);
2045 if ((fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_MGMT) {
2046 if ((fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_ASSOC_REQ ||
2047 (fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_REASSOC_REQ)
Ian Schrambc434dd2007-10-25 17:15:29 +08002048 cmd->cmd.tx.timeout.pm_frame_timeout = cpu_to_le16(3);
Zhu Yib481de92007-09-25 17:54:57 -07002049 else
Ian Schrambc434dd2007-10-25 17:15:29 +08002050 cmd->cmd.tx.timeout.pm_frame_timeout = cpu_to_le16(2);
Mohamed Abbasab53d8a2008-03-25 16:33:36 -07002051 } else {
Zhu Yib481de92007-09-25 17:54:57 -07002052 cmd->cmd.tx.timeout.pm_frame_timeout = 0;
Mohamed Abbasab53d8a2008-03-25 16:33:36 -07002053 }
Zhu Yib481de92007-09-25 17:54:57 -07002054
2055 cmd->cmd.tx.driver_txop = 0;
2056 cmd->cmd.tx.tx_flags = tx_flags;
2057 cmd->cmd.tx.next_frame_len = 0;
2058}
Tomas Winkler19758be2008-03-12 16:58:51 -07002059static void iwl_update_tx_stats(struct iwl_priv *priv, u16 fc, u16 len)
2060{
2061 /* 0 - mgmt, 1 - cnt, 2 - data */
2062 int idx = (fc & IEEE80211_FCTL_FTYPE) >> 2;
2063 priv->tx_stats[idx].cnt++;
2064 priv->tx_stats[idx].bytes += len;
2065}
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002066/**
2067 * iwl4965_get_sta_id - Find station's index within station table
2068 *
2069 * If new IBSS station, create new entry in station table
2070 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002071static int iwl4965_get_sta_id(struct iwl_priv *priv,
Ben Cahill9fbab512007-11-29 11:09:47 +08002072 struct ieee80211_hdr *hdr)
Zhu Yib481de92007-09-25 17:54:57 -07002073{
2074 int sta_id;
2075 u16 fc = le16_to_cpu(hdr->frame_control);
Joe Perches0795af52007-10-03 17:59:30 -07002076 DECLARE_MAC_BUF(mac);
Zhu Yib481de92007-09-25 17:54:57 -07002077
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002078 /* If this frame is broadcast or management, use broadcast station id */
Zhu Yib481de92007-09-25 17:54:57 -07002079 if (((fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_DATA) ||
2080 is_multicast_ether_addr(hdr->addr1))
Tomas Winkler5425e492008-04-15 16:01:38 -07002081 return priv->hw_params.bcast_sta_id;
Zhu Yib481de92007-09-25 17:54:57 -07002082
2083 switch (priv->iw_mode) {
2084
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002085 /* If we are a client station in a BSS network, use the special
2086 * AP station entry (that's the only station we communicate with) */
Zhu Yib481de92007-09-25 17:54:57 -07002087 case IEEE80211_IF_TYPE_STA:
2088 return IWL_AP_ID;
2089
2090 /* If we are an AP, then find the station, or use BCAST */
2091 case IEEE80211_IF_TYPE_AP:
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002092 sta_id = iwl4965_hw_find_station(priv, hdr->addr1);
Zhu Yib481de92007-09-25 17:54:57 -07002093 if (sta_id != IWL_INVALID_STATION)
2094 return sta_id;
Tomas Winkler5425e492008-04-15 16:01:38 -07002095 return priv->hw_params.bcast_sta_id;
Zhu Yib481de92007-09-25 17:54:57 -07002096
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002097 /* If this frame is going out to an IBSS network, find the station,
2098 * or create a new station table entry */
Zhu Yib481de92007-09-25 17:54:57 -07002099 case IEEE80211_IF_TYPE_IBSS:
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002100 sta_id = iwl4965_hw_find_station(priv, hdr->addr1);
Zhu Yib481de92007-09-25 17:54:57 -07002101 if (sta_id != IWL_INVALID_STATION)
2102 return sta_id;
2103
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002104 /* Create new station table entry */
Ron Rindjunsky67d62032007-11-26 16:14:40 +02002105 sta_id = iwl4965_add_station_flags(priv, hdr->addr1,
2106 0, CMD_ASYNC, NULL);
Zhu Yib481de92007-09-25 17:54:57 -07002107
2108 if (sta_id != IWL_INVALID_STATION)
2109 return sta_id;
2110
Joe Perches0795af52007-10-03 17:59:30 -07002111 IWL_DEBUG_DROP("Station %s not in station map. "
Zhu Yib481de92007-09-25 17:54:57 -07002112 "Defaulting to broadcast...\n",
Joe Perches0795af52007-10-03 17:59:30 -07002113 print_mac(mac, hdr->addr1));
Tomas Winkler0a6857e2008-03-12 16:58:49 -07002114 iwl_print_hex_dump(IWL_DL_DROP, (u8 *) hdr, sizeof(*hdr));
Tomas Winkler5425e492008-04-15 16:01:38 -07002115 return priv->hw_params.bcast_sta_id;
Zhu Yib481de92007-09-25 17:54:57 -07002116
2117 default:
Ian Schram01ebd062007-10-25 17:15:22 +08002118 IWL_WARNING("Unknown mode of operation: %d", priv->iw_mode);
Tomas Winkler5425e492008-04-15 16:01:38 -07002119 return priv->hw_params.bcast_sta_id;
Zhu Yib481de92007-09-25 17:54:57 -07002120 }
2121}
2122
2123/*
2124 * start REPLY_TX command process
2125 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002126static int iwl4965_tx_skb(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07002127 struct sk_buff *skb, struct ieee80211_tx_control *ctl)
2128{
2129 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002130 struct iwl4965_tfd_frame *tfd;
Zhu Yib481de92007-09-25 17:54:57 -07002131 u32 *control_flags;
2132 int txq_id = ctl->queue;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002133 struct iwl4965_tx_queue *txq = NULL;
2134 struct iwl4965_queue *q = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07002135 dma_addr_t phys_addr;
2136 dma_addr_t txcmd_phys;
Tomas Winkler87e4f7d2008-01-14 17:46:16 -08002137 dma_addr_t scratch_phys;
Tomas Winkler857485c2008-03-21 13:53:44 -07002138 struct iwl_cmd *out_cmd = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07002139 u16 len, idx, len_org;
2140 u8 id, hdr_len, unicast;
2141 u8 sta_id;
2142 u16 seq_number = 0;
2143 u16 fc;
2144 __le16 *qc;
2145 u8 wait_write_ptr = 0;
2146 unsigned long flags;
2147 int rc;
2148
2149 spin_lock_irqsave(&priv->lock, flags);
Tomas Winklerfee12472008-04-03 16:05:21 -07002150 if (iwl_is_rfkill(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07002151 IWL_DEBUG_DROP("Dropping - RF KILL\n");
2152 goto drop_unlock;
2153 }
2154
Johannes Berg32bfd352007-12-19 01:31:26 +01002155 if (!priv->vif) {
2156 IWL_DEBUG_DROP("Dropping - !priv->vif\n");
Zhu Yib481de92007-09-25 17:54:57 -07002157 goto drop_unlock;
2158 }
2159
Johannes Berg8318d782008-01-24 19:38:38 +01002160 if ((ctl->tx_rate->hw_value & 0xFF) == IWL_INVALID_RATE) {
Zhu Yib481de92007-09-25 17:54:57 -07002161 IWL_ERROR("ERROR: No TX rate available.\n");
2162 goto drop_unlock;
2163 }
2164
2165 unicast = !is_multicast_ether_addr(hdr->addr1);
2166 id = 0;
2167
2168 fc = le16_to_cpu(hdr->frame_control);
2169
Tomas Winkler0a6857e2008-03-12 16:58:49 -07002170#ifdef CONFIG_IWLWIFI_DEBUG
Zhu Yib481de92007-09-25 17:54:57 -07002171 if (ieee80211_is_auth(fc))
2172 IWL_DEBUG_TX("Sending AUTH frame\n");
2173 else if (ieee80211_is_assoc_request(fc))
2174 IWL_DEBUG_TX("Sending ASSOC frame\n");
2175 else if (ieee80211_is_reassoc_request(fc))
2176 IWL_DEBUG_TX("Sending REASSOC frame\n");
2177#endif
2178
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08002179 /* drop all data frame if we are not associated */
Gregory Greenman76f39152008-01-23 10:15:21 -08002180 if (((fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_DATA) &&
Tomas Winkler3109ece2008-03-28 16:33:35 -07002181 (!iwl_is_associated(priv) ||
Reinette Chatrea6477242008-02-14 10:40:28 -08002182 ((priv->iw_mode == IEEE80211_IF_TYPE_STA) && !priv->assoc_id) ||
Gregory Greenman76f39152008-01-23 10:15:21 -08002183 !priv->assoc_station_added)) {
Tomas Winkler3109ece2008-03-28 16:33:35 -07002184 IWL_DEBUG_DROP("Dropping - !iwl_is_associated\n");
Zhu Yib481de92007-09-25 17:54:57 -07002185 goto drop_unlock;
2186 }
2187
2188 spin_unlock_irqrestore(&priv->lock, flags);
2189
2190 hdr_len = ieee80211_get_hdrlen(fc);
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002191
2192 /* Find (or create) index into station table for destination station */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002193 sta_id = iwl4965_get_sta_id(priv, hdr);
Zhu Yib481de92007-09-25 17:54:57 -07002194 if (sta_id == IWL_INVALID_STATION) {
Joe Perches0795af52007-10-03 17:59:30 -07002195 DECLARE_MAC_BUF(mac);
2196
2197 IWL_DEBUG_DROP("Dropping - INVALID STATION: %s\n",
2198 print_mac(mac, hdr->addr1));
Zhu Yib481de92007-09-25 17:54:57 -07002199 goto drop;
2200 }
2201
2202 IWL_DEBUG_RATE("station Id %d\n", sta_id);
2203
2204 qc = ieee80211_get_qos_ctrl(hdr);
2205 if (qc) {
2206 u8 tid = (u8)(le16_to_cpu(*qc) & 0xf);
2207 seq_number = priv->stations[sta_id].tid[tid].seq_number &
2208 IEEE80211_SCTL_SEQ;
2209 hdr->seq_ctrl = cpu_to_le16(seq_number) |
2210 (hdr->seq_ctrl &
2211 __constant_cpu_to_le16(IEEE80211_SCTL_FRAG));
2212 seq_number += 0x10;
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08002213#ifdef CONFIG_IWL4965_HT
Zhu Yib481de92007-09-25 17:54:57 -07002214 /* aggregation is on for this <sta,tid> */
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02002215 if (ctl->flags & IEEE80211_TXCTL_AMPDU)
Zhu Yib481de92007-09-25 17:54:57 -07002216 txq_id = priv->stations[sta_id].tid[tid].agg.txq_id;
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02002217 priv->stations[sta_id].tid[tid].tfds_in_queue++;
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08002218#endif /* CONFIG_IWL4965_HT */
Zhu Yib481de92007-09-25 17:54:57 -07002219 }
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002220
2221 /* Descriptor for chosen Tx queue */
Zhu Yib481de92007-09-25 17:54:57 -07002222 txq = &priv->txq[txq_id];
2223 q = &txq->q;
2224
2225 spin_lock_irqsave(&priv->lock, flags);
2226
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002227 /* Set up first empty TFD within this queue's circular TFD buffer */
Tomas Winklerfc4b6852007-10-25 17:15:24 +08002228 tfd = &txq->bd[q->write_ptr];
Zhu Yib481de92007-09-25 17:54:57 -07002229 memset(tfd, 0, sizeof(*tfd));
2230 control_flags = (u32 *) tfd;
Tomas Winklerfc4b6852007-10-25 17:15:24 +08002231 idx = get_cmd_index(q, q->write_ptr, 0);
Zhu Yib481de92007-09-25 17:54:57 -07002232
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002233 /* Set up driver data for this TFD */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002234 memset(&(txq->txb[q->write_ptr]), 0, sizeof(struct iwl4965_tx_info));
Tomas Winklerfc4b6852007-10-25 17:15:24 +08002235 txq->txb[q->write_ptr].skb[0] = skb;
2236 memcpy(&(txq->txb[q->write_ptr].status.control),
Zhu Yib481de92007-09-25 17:54:57 -07002237 ctl, sizeof(struct ieee80211_tx_control));
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002238
2239 /* Set up first empty entry in queue's array of Tx/cmd buffers */
Zhu Yib481de92007-09-25 17:54:57 -07002240 out_cmd = &txq->cmd[idx];
2241 memset(&out_cmd->hdr, 0, sizeof(out_cmd->hdr));
2242 memset(&out_cmd->cmd.tx, 0, sizeof(out_cmd->cmd.tx));
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002243
2244 /*
2245 * Set up the Tx-command (not MAC!) header.
2246 * Store the chosen Tx queue and TFD index within the sequence field;
2247 * after Tx, uCode's Tx response will return this value so driver can
2248 * locate the frame within the tx queue and do post-tx processing.
2249 */
Zhu Yib481de92007-09-25 17:54:57 -07002250 out_cmd->hdr.cmd = REPLY_TX;
2251 out_cmd->hdr.sequence = cpu_to_le16((u16)(QUEUE_TO_SEQ(txq_id) |
Tomas Winklerfc4b6852007-10-25 17:15:24 +08002252 INDEX_TO_SEQ(q->write_ptr)));
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002253
2254 /* Copy MAC header from skb into command buffer */
Zhu Yib481de92007-09-25 17:54:57 -07002255 memcpy(out_cmd->cmd.tx.hdr, hdr, hdr_len);
2256
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002257 /*
2258 * Use the first empty entry in this queue's command buffer array
2259 * to contain the Tx command and MAC header concatenated together
2260 * (payload data will be in another buffer).
2261 * Size of this varies, due to varying MAC header length.
2262 * If end is not dword aligned, we'll have 2 extra bytes at the end
2263 * of the MAC header (device reads on dword boundaries).
2264 * We'll tell device about this padding later.
2265 */
Tomas Winkler5425e492008-04-15 16:01:38 -07002266 len = priv->hw_params.tx_cmd_len +
Tomas Winkler857485c2008-03-21 13:53:44 -07002267 sizeof(struct iwl_cmd_header) + hdr_len;
Zhu Yib481de92007-09-25 17:54:57 -07002268
2269 len_org = len;
2270 len = (len + 3) & ~3;
2271
2272 if (len_org != len)
2273 len_org = 1;
2274 else
2275 len_org = 0;
2276
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002277 /* Physical address of this Tx command's header (not MAC header!),
2278 * within command buffer array. */
Tomas Winkler857485c2008-03-21 13:53:44 -07002279 txcmd_phys = txq->dma_addr_cmd + sizeof(struct iwl_cmd) * idx +
2280 offsetof(struct iwl_cmd, hdr);
Zhu Yib481de92007-09-25 17:54:57 -07002281
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002282 /* Add buffer containing Tx command and MAC(!) header to TFD's
2283 * first entry */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002284 iwl4965_hw_txq_attach_buf_to_tfd(priv, tfd, txcmd_phys, len);
Zhu Yib481de92007-09-25 17:54:57 -07002285
2286 if (!(ctl->flags & IEEE80211_TXCTL_DO_NOT_ENCRYPT))
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07002287 iwl4965_build_tx_cmd_hwcrypto(priv, ctl, out_cmd, skb, sta_id);
Zhu Yib481de92007-09-25 17:54:57 -07002288
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002289 /* Set up TFD's 2nd entry to point directly to remainder of skb,
2290 * if any (802.11 null frames have no payload). */
Zhu Yib481de92007-09-25 17:54:57 -07002291 len = skb->len - hdr_len;
2292 if (len) {
2293 phys_addr = pci_map_single(priv->pci_dev, skb->data + hdr_len,
2294 len, PCI_DMA_TODEVICE);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002295 iwl4965_hw_txq_attach_buf_to_tfd(priv, tfd, phys_addr, len);
Zhu Yib481de92007-09-25 17:54:57 -07002296 }
2297
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002298 /* Tell 4965 about any 2-byte padding after MAC header */
Zhu Yib481de92007-09-25 17:54:57 -07002299 if (len_org)
2300 out_cmd->cmd.tx.tx_flags |= TX_CMD_FLG_MH_PAD_MSK;
2301
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002302 /* Total # bytes to be transmitted */
Zhu Yib481de92007-09-25 17:54:57 -07002303 len = (u16)skb->len;
2304 out_cmd->cmd.tx.len = cpu_to_le16(len);
2305
2306 /* TODO need this for burst mode later on */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002307 iwl4965_build_tx_cmd_basic(priv, out_cmd, ctl, hdr, unicast, sta_id);
Zhu Yib481de92007-09-25 17:54:57 -07002308
2309 /* set is_hcca to 0; it probably will never be implemented */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002310 iwl4965_hw_build_tx_cmd_rate(priv, out_cmd, ctl, hdr, sta_id, 0);
Zhu Yib481de92007-09-25 17:54:57 -07002311
Tomas Winkler19758be2008-03-12 16:58:51 -07002312 iwl_update_tx_stats(priv, fc, len);
2313
Tomas Winkler857485c2008-03-21 13:53:44 -07002314 scratch_phys = txcmd_phys + sizeof(struct iwl_cmd_header) +
Tomas Winkler87e4f7d2008-01-14 17:46:16 -08002315 offsetof(struct iwl4965_tx_cmd, scratch);
2316 out_cmd->cmd.tx.dram_lsb_ptr = cpu_to_le32(scratch_phys);
2317 out_cmd->cmd.tx.dram_msb_ptr = iwl_get_dma_hi_address(scratch_phys);
2318
Zhu Yib481de92007-09-25 17:54:57 -07002319 if (!ieee80211_get_morefrag(hdr)) {
2320 txq->need_update = 1;
2321 if (qc) {
2322 u8 tid = (u8)(le16_to_cpu(*qc) & 0xf);
2323 priv->stations[sta_id].tid[tid].seq_number = seq_number;
2324 }
2325 } else {
2326 wait_write_ptr = 1;
2327 txq->need_update = 0;
2328 }
2329
Tomas Winkler0a6857e2008-03-12 16:58:49 -07002330 iwl_print_hex_dump(IWL_DL_TX, out_cmd->cmd.payload,
Zhu Yib481de92007-09-25 17:54:57 -07002331 sizeof(out_cmd->cmd.tx));
2332
Tomas Winkler0a6857e2008-03-12 16:58:49 -07002333 iwl_print_hex_dump(IWL_DL_TX, (u8 *)out_cmd->cmd.tx.hdr,
Zhu Yib481de92007-09-25 17:54:57 -07002334 ieee80211_get_hdrlen(fc));
2335
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002336 /* Set up entry for this TFD in Tx byte-count array */
Tomas Winklere2a722e2008-04-14 21:16:10 -07002337 priv->cfg->ops->lib->txq_update_byte_cnt_tbl(priv, txq, len);
Zhu Yib481de92007-09-25 17:54:57 -07002338
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002339 /* Tell device the write index *just past* this latest filled TFD */
Tomas Winklerc54b6792008-03-06 17:36:53 -08002340 q->write_ptr = iwl_queue_inc_wrap(q->write_ptr, q->n_bd);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002341 rc = iwl4965_tx_queue_update_write_ptr(priv, txq);
Zhu Yib481de92007-09-25 17:54:57 -07002342 spin_unlock_irqrestore(&priv->lock, flags);
2343
2344 if (rc)
2345 return rc;
2346
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002347 if ((iwl4965_queue_space(q) < q->high_mark)
Zhu Yib481de92007-09-25 17:54:57 -07002348 && priv->mac80211_registered) {
2349 if (wait_write_ptr) {
2350 spin_lock_irqsave(&priv->lock, flags);
2351 txq->need_update = 1;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002352 iwl4965_tx_queue_update_write_ptr(priv, txq);
Zhu Yib481de92007-09-25 17:54:57 -07002353 spin_unlock_irqrestore(&priv->lock, flags);
2354 }
2355
2356 ieee80211_stop_queue(priv->hw, ctl->queue);
2357 }
2358
2359 return 0;
2360
2361drop_unlock:
2362 spin_unlock_irqrestore(&priv->lock, flags);
2363drop:
2364 return -1;
2365}
2366
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002367static void iwl4965_set_rate(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002368{
Johannes Berg8318d782008-01-24 19:38:38 +01002369 const struct ieee80211_supported_band *hw = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07002370 struct ieee80211_rate *rate;
2371 int i;
2372
Johannes Berg8318d782008-01-24 19:38:38 +01002373 hw = iwl4965_get_hw_mode(priv, priv->band);
Saleem Abdulrasoolc4ba9622007-11-18 23:59:08 -08002374 if (!hw) {
2375 IWL_ERROR("Failed to set rate: unable to get hw mode\n");
2376 return;
2377 }
Zhu Yib481de92007-09-25 17:54:57 -07002378
2379 priv->active_rate = 0;
2380 priv->active_rate_basic = 0;
2381
Johannes Berg8318d782008-01-24 19:38:38 +01002382 for (i = 0; i < hw->n_bitrates; i++) {
2383 rate = &(hw->bitrates[i]);
2384 if (rate->hw_value < IWL_RATE_COUNT)
2385 priv->active_rate |= (1 << rate->hw_value);
Zhu Yib481de92007-09-25 17:54:57 -07002386 }
2387
2388 IWL_DEBUG_RATE("Set active_rate = %0x, active_rate_basic = %0x\n",
2389 priv->active_rate, priv->active_rate_basic);
2390
2391 /*
2392 * If a basic rate is configured, then use it (adding IWL_RATE_1M_MASK)
2393 * otherwise set it to the default of all CCK rates and 6, 12, 24 for
2394 * OFDM
2395 */
2396 if (priv->active_rate_basic & IWL_CCK_BASIC_RATES_MASK)
2397 priv->staging_rxon.cck_basic_rates =
2398 ((priv->active_rate_basic &
2399 IWL_CCK_RATES_MASK) >> IWL_FIRST_CCK_RATE) & 0xF;
2400 else
2401 priv->staging_rxon.cck_basic_rates =
2402 (IWL_CCK_BASIC_RATES_MASK >> IWL_FIRST_CCK_RATE) & 0xF;
2403
2404 if (priv->active_rate_basic & IWL_OFDM_BASIC_RATES_MASK)
2405 priv->staging_rxon.ofdm_basic_rates =
2406 ((priv->active_rate_basic &
2407 (IWL_OFDM_BASIC_RATES_MASK | IWL_RATE_6M_MASK)) >>
2408 IWL_FIRST_OFDM_RATE) & 0xFF;
2409 else
2410 priv->staging_rxon.ofdm_basic_rates =
2411 (IWL_OFDM_BASIC_RATES_MASK >> IWL_FIRST_OFDM_RATE) & 0xFF;
2412}
2413
Mohamed Abbasad97edd2008-03-28 16:21:06 -07002414void iwl4965_radio_kill_sw(struct iwl_priv *priv, int disable_radio)
Zhu Yib481de92007-09-25 17:54:57 -07002415{
2416 unsigned long flags;
2417
2418 if (!!disable_radio == test_bit(STATUS_RF_KILL_SW, &priv->status))
2419 return;
2420
2421 IWL_DEBUG_RF_KILL("Manual SW RF KILL set to: RADIO %s\n",
2422 disable_radio ? "OFF" : "ON");
2423
2424 if (disable_radio) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002425 iwl4965_scan_cancel(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002426 /* FIXME: This is a workaround for AP */
2427 if (priv->iw_mode != IEEE80211_IF_TYPE_AP) {
2428 spin_lock_irqsave(&priv->lock, flags);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07002429 iwl_write32(priv, CSR_UCODE_DRV_GP1_SET,
Zhu Yib481de92007-09-25 17:54:57 -07002430 CSR_UCODE_SW_BIT_RFKILL);
2431 spin_unlock_irqrestore(&priv->lock, flags);
Mohamed Abbasad97edd2008-03-28 16:21:06 -07002432 /* call the host command only if no hw rf-kill set */
Mohamed Abbas59003832008-04-15 16:01:46 -07002433 if (!test_bit(STATUS_RF_KILL_HW, &priv->status) &&
2434 iwl_is_ready(priv))
Mohamed Abbasad97edd2008-03-28 16:21:06 -07002435 iwl4965_send_card_state(priv,
2436 CARD_STATE_CMD_DISABLE,
2437 0);
Zhu Yib481de92007-09-25 17:54:57 -07002438 set_bit(STATUS_RF_KILL_SW, &priv->status);
Mohamed Abbasad97edd2008-03-28 16:21:06 -07002439
2440 /* make sure mac80211 stop sending Tx frame */
2441 if (priv->mac80211_registered)
2442 ieee80211_stop_queues(priv->hw);
Zhu Yib481de92007-09-25 17:54:57 -07002443 }
2444 return;
2445 }
2446
2447 spin_lock_irqsave(&priv->lock, flags);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07002448 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
Zhu Yib481de92007-09-25 17:54:57 -07002449
2450 clear_bit(STATUS_RF_KILL_SW, &priv->status);
2451 spin_unlock_irqrestore(&priv->lock, flags);
2452
2453 /* wake up ucode */
2454 msleep(10);
2455
2456 spin_lock_irqsave(&priv->lock, flags);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07002457 iwl_read32(priv, CSR_UCODE_DRV_GP1);
2458 if (!iwl_grab_nic_access(priv))
2459 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002460 spin_unlock_irqrestore(&priv->lock, flags);
2461
2462 if (test_bit(STATUS_RF_KILL_HW, &priv->status)) {
2463 IWL_DEBUG_RF_KILL("Can not turn radio back on - "
2464 "disabled by HW switch\n");
2465 return;
2466 }
2467
2468 queue_work(priv->workqueue, &priv->restart);
2469 return;
2470}
2471
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002472void iwl4965_set_decrypted_flag(struct iwl_priv *priv, struct sk_buff *skb,
Zhu Yib481de92007-09-25 17:54:57 -07002473 u32 decrypt_res, struct ieee80211_rx_status *stats)
2474{
2475 u16 fc =
2476 le16_to_cpu(((struct ieee80211_hdr *)skb->data)->frame_control);
2477
2478 if (priv->active_rxon.filter_flags & RXON_FILTER_DIS_DECRYPT_MSK)
2479 return;
2480
2481 if (!(fc & IEEE80211_FCTL_PROTECTED))
2482 return;
2483
2484 IWL_DEBUG_RX("decrypt_res:0x%x\n", decrypt_res);
2485 switch (decrypt_res & RX_RES_STATUS_SEC_TYPE_MSK) {
2486 case RX_RES_STATUS_SEC_TYPE_TKIP:
Emmanuel Grumbach17e476b2008-03-19 16:41:42 -07002487 /* The uCode has got a bad phase 1 Key, pushes the packet.
2488 * Decryption will be done in SW. */
2489 if ((decrypt_res & RX_RES_STATUS_DECRYPT_TYPE_MSK) ==
2490 RX_RES_STATUS_BAD_KEY_TTAK)
2491 break;
2492
Zhu Yib481de92007-09-25 17:54:57 -07002493 if ((decrypt_res & RX_RES_STATUS_DECRYPT_TYPE_MSK) ==
2494 RX_RES_STATUS_BAD_ICV_MIC)
2495 stats->flag |= RX_FLAG_MMIC_ERROR;
2496 case RX_RES_STATUS_SEC_TYPE_WEP:
2497 case RX_RES_STATUS_SEC_TYPE_CCMP:
2498 if ((decrypt_res & RX_RES_STATUS_DECRYPT_TYPE_MSK) ==
2499 RX_RES_STATUS_DECRYPT_OK) {
2500 IWL_DEBUG_RX("hw decrypt successfully!!!\n");
2501 stats->flag |= RX_FLAG_DECRYPTED;
2502 }
2503 break;
2504
2505 default:
2506 break;
2507 }
2508}
2509
Zhu Yib481de92007-09-25 17:54:57 -07002510
2511#define IWL_PACKET_RETRY_TIME HZ
2512
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002513int iwl4965_is_duplicate_packet(struct iwl_priv *priv, struct ieee80211_hdr *header)
Zhu Yib481de92007-09-25 17:54:57 -07002514{
2515 u16 sc = le16_to_cpu(header->seq_ctrl);
2516 u16 seq = (sc & IEEE80211_SCTL_SEQ) >> 4;
2517 u16 frag = sc & IEEE80211_SCTL_FRAG;
2518 u16 *last_seq, *last_frag;
2519 unsigned long *last_time;
2520
2521 switch (priv->iw_mode) {
2522 case IEEE80211_IF_TYPE_IBSS:{
2523 struct list_head *p;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002524 struct iwl4965_ibss_seq *entry = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07002525 u8 *mac = header->addr2;
2526 int index = mac[5] & (IWL_IBSS_MAC_HASH_SIZE - 1);
2527
2528 __list_for_each(p, &priv->ibss_mac_hash[index]) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002529 entry = list_entry(p, struct iwl4965_ibss_seq, list);
Zhu Yib481de92007-09-25 17:54:57 -07002530 if (!compare_ether_addr(entry->mac, mac))
2531 break;
2532 }
2533 if (p == &priv->ibss_mac_hash[index]) {
2534 entry = kzalloc(sizeof(*entry), GFP_ATOMIC);
2535 if (!entry) {
Ian Schrambc434dd2007-10-25 17:15:29 +08002536 IWL_ERROR("Cannot malloc new mac entry\n");
Zhu Yib481de92007-09-25 17:54:57 -07002537 return 0;
2538 }
2539 memcpy(entry->mac, mac, ETH_ALEN);
2540 entry->seq_num = seq;
2541 entry->frag_num = frag;
2542 entry->packet_time = jiffies;
Ian Schrambc434dd2007-10-25 17:15:29 +08002543 list_add(&entry->list, &priv->ibss_mac_hash[index]);
Zhu Yib481de92007-09-25 17:54:57 -07002544 return 0;
2545 }
2546 last_seq = &entry->seq_num;
2547 last_frag = &entry->frag_num;
2548 last_time = &entry->packet_time;
2549 break;
2550 }
2551 case IEEE80211_IF_TYPE_STA:
2552 last_seq = &priv->last_seq_num;
2553 last_frag = &priv->last_frag_num;
2554 last_time = &priv->last_packet_time;
2555 break;
2556 default:
2557 return 0;
2558 }
2559 if ((*last_seq == seq) &&
2560 time_after(*last_time + IWL_PACKET_RETRY_TIME, jiffies)) {
2561 if (*last_frag == frag)
2562 goto drop;
2563 if (*last_frag + 1 != frag)
2564 /* out-of-order fragment */
2565 goto drop;
2566 } else
2567 *last_seq = seq;
2568
2569 *last_frag = frag;
2570 *last_time = jiffies;
2571 return 0;
2572
2573 drop:
2574 return 1;
2575}
2576
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08002577#ifdef CONFIG_IWL4965_SPECTRUM_MEASUREMENT
Zhu Yib481de92007-09-25 17:54:57 -07002578
2579#include "iwl-spectrum.h"
2580
2581#define BEACON_TIME_MASK_LOW 0x00FFFFFF
2582#define BEACON_TIME_MASK_HIGH 0xFF000000
2583#define TIME_UNIT 1024
2584
2585/*
2586 * extended beacon time format
2587 * time in usec will be changed into a 32-bit value in 8:24 format
2588 * the high 1 byte is the beacon counts
2589 * the lower 3 bytes is the time in usec within one beacon interval
2590 */
2591
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002592static u32 iwl4965_usecs_to_beacons(u32 usec, u32 beacon_interval)
Zhu Yib481de92007-09-25 17:54:57 -07002593{
2594 u32 quot;
2595 u32 rem;
2596 u32 interval = beacon_interval * 1024;
2597
2598 if (!interval || !usec)
2599 return 0;
2600
2601 quot = (usec / interval) & (BEACON_TIME_MASK_HIGH >> 24);
2602 rem = (usec % interval) & BEACON_TIME_MASK_LOW;
2603
2604 return (quot << 24) + rem;
2605}
2606
2607/* base is usually what we get from ucode with each received frame,
2608 * the same as HW timer counter counting down
2609 */
2610
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002611static __le32 iwl4965_add_beacon_time(u32 base, u32 addon, u32 beacon_interval)
Zhu Yib481de92007-09-25 17:54:57 -07002612{
2613 u32 base_low = base & BEACON_TIME_MASK_LOW;
2614 u32 addon_low = addon & BEACON_TIME_MASK_LOW;
2615 u32 interval = beacon_interval * TIME_UNIT;
2616 u32 res = (base & BEACON_TIME_MASK_HIGH) +
2617 (addon & BEACON_TIME_MASK_HIGH);
2618
2619 if (base_low > addon_low)
2620 res += base_low - addon_low;
2621 else if (base_low < addon_low) {
2622 res += interval + base_low - addon_low;
2623 res += (1 << 24);
2624 } else
2625 res += (1 << 24);
2626
2627 return cpu_to_le32(res);
2628}
2629
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002630static int iwl4965_get_measurement(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07002631 struct ieee80211_measurement_params *params,
2632 u8 type)
2633{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002634 struct iwl4965_spectrum_cmd spectrum;
2635 struct iwl4965_rx_packet *res;
Tomas Winkler857485c2008-03-21 13:53:44 -07002636 struct iwl_host_cmd cmd = {
Zhu Yib481de92007-09-25 17:54:57 -07002637 .id = REPLY_SPECTRUM_MEASUREMENT_CMD,
2638 .data = (void *)&spectrum,
2639 .meta.flags = CMD_WANT_SKB,
2640 };
2641 u32 add_time = le64_to_cpu(params->start_time);
2642 int rc;
2643 int spectrum_resp_status;
2644 int duration = le16_to_cpu(params->duration);
2645
Tomas Winkler3109ece2008-03-28 16:33:35 -07002646 if (iwl_is_associated(priv))
Zhu Yib481de92007-09-25 17:54:57 -07002647 add_time =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002648 iwl4965_usecs_to_beacons(
Zhu Yib481de92007-09-25 17:54:57 -07002649 le64_to_cpu(params->start_time) - priv->last_tsf,
2650 le16_to_cpu(priv->rxon_timing.beacon_interval));
2651
2652 memset(&spectrum, 0, sizeof(spectrum));
2653
2654 spectrum.channel_count = cpu_to_le16(1);
2655 spectrum.flags =
2656 RXON_FLG_TSF2HOST_MSK | RXON_FLG_ANT_A_MSK | RXON_FLG_DIS_DIV_MSK;
2657 spectrum.filter_flags = MEASUREMENT_FILTER_FLAG;
2658 cmd.len = sizeof(spectrum);
2659 spectrum.len = cpu_to_le16(cmd.len - sizeof(spectrum.len));
2660
Tomas Winkler3109ece2008-03-28 16:33:35 -07002661 if (iwl_is_associated(priv))
Zhu Yib481de92007-09-25 17:54:57 -07002662 spectrum.start_time =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002663 iwl4965_add_beacon_time(priv->last_beacon_time,
Zhu Yib481de92007-09-25 17:54:57 -07002664 add_time,
2665 le16_to_cpu(priv->rxon_timing.beacon_interval));
2666 else
2667 spectrum.start_time = 0;
2668
2669 spectrum.channels[0].duration = cpu_to_le32(duration * TIME_UNIT);
2670 spectrum.channels[0].channel = params->channel;
2671 spectrum.channels[0].type = type;
2672 if (priv->active_rxon.flags & RXON_FLG_BAND_24G_MSK)
2673 spectrum.flags |= RXON_FLG_BAND_24G_MSK |
2674 RXON_FLG_AUTO_DETECT_MSK | RXON_FLG_TGG_PROTECT_MSK;
2675
Tomas Winkler857485c2008-03-21 13:53:44 -07002676 rc = iwl_send_cmd_sync(priv, &cmd);
Zhu Yib481de92007-09-25 17:54:57 -07002677 if (rc)
2678 return rc;
2679
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002680 res = (struct iwl4965_rx_packet *)cmd.meta.u.skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07002681 if (res->hdr.flags & IWL_CMD_FAILED_MSK) {
2682 IWL_ERROR("Bad return from REPLY_RX_ON_ASSOC command\n");
2683 rc = -EIO;
2684 }
2685
2686 spectrum_resp_status = le16_to_cpu(res->u.spectrum.status);
2687 switch (spectrum_resp_status) {
2688 case 0: /* Command will be handled */
2689 if (res->u.spectrum.id != 0xff) {
2690 IWL_DEBUG_INFO
2691 ("Replaced existing measurement: %d\n",
2692 res->u.spectrum.id);
2693 priv->measurement_status &= ~MEASUREMENT_READY;
2694 }
2695 priv->measurement_status |= MEASUREMENT_ACTIVE;
2696 rc = 0;
2697 break;
2698
2699 case 1: /* Command will not be handled */
2700 rc = -EAGAIN;
2701 break;
2702 }
2703
2704 dev_kfree_skb_any(cmd.meta.u.skb);
2705
2706 return rc;
2707}
2708#endif
2709
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002710static void iwl4965_txstatus_to_ieee(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002711 struct iwl4965_tx_info *tx_sta)
Zhu Yib481de92007-09-25 17:54:57 -07002712{
2713
2714 tx_sta->status.ack_signal = 0;
2715 tx_sta->status.excessive_retries = 0;
2716 tx_sta->status.queue_length = 0;
2717 tx_sta->status.queue_number = 0;
2718
2719 if (in_interrupt())
2720 ieee80211_tx_status_irqsafe(priv->hw,
2721 tx_sta->skb[0], &(tx_sta->status));
2722 else
2723 ieee80211_tx_status(priv->hw,
2724 tx_sta->skb[0], &(tx_sta->status));
2725
2726 tx_sta->skb[0] = NULL;
2727}
2728
2729/**
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002730 * iwl4965_tx_queue_reclaim - Reclaim Tx queue entries already Tx'd
Zhu Yib481de92007-09-25 17:54:57 -07002731 *
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002732 * When FW advances 'R' index, all entries between old and new 'R' index
2733 * need to be reclaimed. As result, some free space forms. If there is
2734 * enough free space (> low mark), wake the stack that feeds us.
Zhu Yib481de92007-09-25 17:54:57 -07002735 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002736int iwl4965_tx_queue_reclaim(struct iwl_priv *priv, int txq_id, int index)
Zhu Yib481de92007-09-25 17:54:57 -07002737{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002738 struct iwl4965_tx_queue *txq = &priv->txq[txq_id];
2739 struct iwl4965_queue *q = &txq->q;
Zhu Yib481de92007-09-25 17:54:57 -07002740 int nfreed = 0;
2741
2742 if ((index >= q->n_bd) || (x2_queue_used(q, index) == 0)) {
2743 IWL_ERROR("Read index for DMA queue txq id (%d), index %d, "
2744 "is out of range [0-%d] %d %d.\n", txq_id,
Tomas Winklerfc4b6852007-10-25 17:15:24 +08002745 index, q->n_bd, q->write_ptr, q->read_ptr);
Zhu Yib481de92007-09-25 17:54:57 -07002746 return 0;
2747 }
2748
Tomas Winklerc54b6792008-03-06 17:36:53 -08002749 for (index = iwl_queue_inc_wrap(index, q->n_bd);
Tomas Winklerfc4b6852007-10-25 17:15:24 +08002750 q->read_ptr != index;
Tomas Winklerc54b6792008-03-06 17:36:53 -08002751 q->read_ptr = iwl_queue_inc_wrap(q->read_ptr, q->n_bd)) {
Zhu Yib481de92007-09-25 17:54:57 -07002752 if (txq_id != IWL_CMD_QUEUE_NUM) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002753 iwl4965_txstatus_to_ieee(priv,
Tomas Winklerfc4b6852007-10-25 17:15:24 +08002754 &(txq->txb[txq->q.read_ptr]));
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002755 iwl4965_hw_txq_free_tfd(priv, txq);
Zhu Yib481de92007-09-25 17:54:57 -07002756 } else if (nfreed > 1) {
2757 IWL_ERROR("HCMD skipped: index (%d) %d %d\n", index,
Tomas Winklerfc4b6852007-10-25 17:15:24 +08002758 q->write_ptr, q->read_ptr);
Zhu Yib481de92007-09-25 17:54:57 -07002759 queue_work(priv->workqueue, &priv->restart);
2760 }
2761 nfreed++;
2762 }
2763
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02002764/* if (iwl4965_queue_space(q) > q->low_mark && (txq_id >= 0) &&
Zhu Yib481de92007-09-25 17:54:57 -07002765 (txq_id != IWL_CMD_QUEUE_NUM) &&
2766 priv->mac80211_registered)
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02002767 ieee80211_wake_queue(priv->hw, txq_id); */
Zhu Yib481de92007-09-25 17:54:57 -07002768
2769
2770 return nfreed;
2771}
2772
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002773static int iwl4965_is_tx_success(u32 status)
Zhu Yib481de92007-09-25 17:54:57 -07002774{
2775 status &= TX_STATUS_MSK;
2776 return (status == TX_STATUS_SUCCESS)
2777 || (status == TX_STATUS_DIRECT_DONE);
2778}
2779
2780/******************************************************************************
2781 *
2782 * Generic RX handler implementations
2783 *
2784 ******************************************************************************/
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08002785#ifdef CONFIG_IWL4965_HT
Zhu Yib481de92007-09-25 17:54:57 -07002786
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002787static inline int iwl4965_get_ra_sta_id(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07002788 struct ieee80211_hdr *hdr)
2789{
2790 if (priv->iw_mode == IEEE80211_IF_TYPE_STA)
2791 return IWL_AP_ID;
2792 else {
2793 u8 *da = ieee80211_get_DA(hdr);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002794 return iwl4965_hw_find_station(priv, da);
Zhu Yib481de92007-09-25 17:54:57 -07002795 }
2796}
2797
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002798static struct ieee80211_hdr *iwl4965_tx_queue_get_hdr(
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002799 struct iwl_priv *priv, int txq_id, int idx)
Zhu Yib481de92007-09-25 17:54:57 -07002800{
2801 if (priv->txq[txq_id].txb[idx].skb[0])
2802 return (struct ieee80211_hdr *)priv->txq[txq_id].
2803 txb[idx].skb[0]->data;
2804 return NULL;
2805}
2806
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002807static inline u32 iwl4965_get_scd_ssn(struct iwl4965_tx_resp *tx_resp)
Zhu Yib481de92007-09-25 17:54:57 -07002808{
2809 __le32 *scd_ssn = (__le32 *)((u32 *)&tx_resp->status +
2810 tx_resp->frame_count);
2811 return le32_to_cpu(*scd_ssn) & MAX_SN;
2812
2813}
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002814
2815/**
2816 * iwl4965_tx_status_reply_tx - Handle Tx rspnse for frames in aggregation queue
2817 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002818static int iwl4965_tx_status_reply_tx(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002819 struct iwl4965_ht_agg *agg,
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02002820 struct iwl4965_tx_resp_agg *tx_resp,
Zhu Yib481de92007-09-25 17:54:57 -07002821 u16 start_idx)
2822{
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02002823 u16 status;
2824 struct agg_tx_status *frame_status = &tx_resp->status;
Zhu Yib481de92007-09-25 17:54:57 -07002825 struct ieee80211_tx_status *tx_status = NULL;
2826 struct ieee80211_hdr *hdr = NULL;
2827 int i, sh;
2828 int txq_id, idx;
2829 u16 seq;
2830
2831 if (agg->wait_for_ba)
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002832 IWL_DEBUG_TX_REPLY("got tx response w/o block-ack\n");
Zhu Yib481de92007-09-25 17:54:57 -07002833
2834 agg->frame_count = tx_resp->frame_count;
2835 agg->start_idx = start_idx;
2836 agg->rate_n_flags = le32_to_cpu(tx_resp->rate_n_flags);
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02002837 agg->bitmap = 0;
Zhu Yib481de92007-09-25 17:54:57 -07002838
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002839 /* # frames attempted by Tx command */
Zhu Yib481de92007-09-25 17:54:57 -07002840 if (agg->frame_count == 1) {
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002841 /* Only one frame was attempted; no block-ack will arrive */
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02002842 status = le16_to_cpu(frame_status[0].status);
2843 seq = le16_to_cpu(frame_status[0].sequence);
2844 idx = SEQ_TO_INDEX(seq);
2845 txq_id = SEQ_TO_QUEUE(seq);
Zhu Yib481de92007-09-25 17:54:57 -07002846
Zhu Yib481de92007-09-25 17:54:57 -07002847 /* FIXME: code repetition */
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02002848 IWL_DEBUG_TX_REPLY("FrameCnt = %d, StartIdx=%d idx=%d\n",
2849 agg->frame_count, agg->start_idx, idx);
Zhu Yib481de92007-09-25 17:54:57 -07002850
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02002851 tx_status = &(priv->txq[txq_id].txb[idx].status);
Zhu Yib481de92007-09-25 17:54:57 -07002852 tx_status->retry_count = tx_resp->failure_frame;
2853 tx_status->queue_number = status & 0xff;
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02002854 tx_status->queue_length = tx_resp->failure_rts;
2855 tx_status->control.flags &= ~IEEE80211_TXCTL_AMPDU;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002856 tx_status->flags = iwl4965_is_tx_success(status)?
Zhu Yib481de92007-09-25 17:54:57 -07002857 IEEE80211_TX_STATUS_ACK : 0;
Ron Rindjunsky4c424e42008-03-04 18:09:27 -08002858 iwl4965_hwrate_to_tx_control(priv,
2859 le32_to_cpu(tx_resp->rate_n_flags),
2860 &tx_status->control);
Zhu Yib481de92007-09-25 17:54:57 -07002861 /* FIXME: code repetition end */
2862
2863 IWL_DEBUG_TX_REPLY("1 Frame 0x%x failure :%d\n",
2864 status & 0xff, tx_resp->failure_frame);
2865 IWL_DEBUG_TX_REPLY("Rate Info rate_n_flags=%x\n",
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002866 iwl4965_hw_get_rate_n_flags(tx_resp->rate_n_flags));
Zhu Yib481de92007-09-25 17:54:57 -07002867
2868 agg->wait_for_ba = 0;
2869 } else {
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002870 /* Two or more frames were attempted; expect block-ack */
Zhu Yib481de92007-09-25 17:54:57 -07002871 u64 bitmap = 0;
2872 int start = agg->start_idx;
2873
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002874 /* Construct bit-map of pending frames within Tx window */
Zhu Yib481de92007-09-25 17:54:57 -07002875 for (i = 0; i < agg->frame_count; i++) {
2876 u16 sc;
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02002877 status = le16_to_cpu(frame_status[i].status);
2878 seq = le16_to_cpu(frame_status[i].sequence);
Zhu Yib481de92007-09-25 17:54:57 -07002879 idx = SEQ_TO_INDEX(seq);
2880 txq_id = SEQ_TO_QUEUE(seq);
2881
2882 if (status & (AGG_TX_STATE_FEW_BYTES_MSK |
2883 AGG_TX_STATE_ABORT_MSK))
2884 continue;
2885
2886 IWL_DEBUG_TX_REPLY("FrameCnt = %d, txq_id=%d idx=%d\n",
2887 agg->frame_count, txq_id, idx);
2888
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002889 hdr = iwl4965_tx_queue_get_hdr(priv, txq_id, idx);
Zhu Yib481de92007-09-25 17:54:57 -07002890
2891 sc = le16_to_cpu(hdr->seq_ctrl);
2892 if (idx != (SEQ_TO_SN(sc) & 0xff)) {
2893 IWL_ERROR("BUG_ON idx doesn't match seq control"
2894 " idx=%d, seq_idx=%d, seq=%d\n",
2895 idx, SEQ_TO_SN(sc),
2896 hdr->seq_ctrl);
2897 return -1;
2898 }
2899
2900 IWL_DEBUG_TX_REPLY("AGG Frame i=%d idx %d seq=%d\n",
2901 i, idx, SEQ_TO_SN(sc));
2902
2903 sh = idx - start;
2904 if (sh > 64) {
2905 sh = (start - idx) + 0xff;
2906 bitmap = bitmap << sh;
2907 sh = 0;
2908 start = idx;
2909 } else if (sh < -64)
2910 sh = 0xff - (start - idx);
2911 else if (sh < 0) {
2912 sh = start - idx;
2913 start = idx;
2914 bitmap = bitmap << sh;
2915 sh = 0;
2916 }
2917 bitmap |= (1 << sh);
2918 IWL_DEBUG_TX_REPLY("start=%d bitmap=0x%x\n",
2919 start, (u32)(bitmap & 0xFFFFFFFF));
2920 }
2921
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02002922 agg->bitmap = bitmap;
Zhu Yib481de92007-09-25 17:54:57 -07002923 agg->start_idx = start;
2924 agg->rate_n_flags = le32_to_cpu(tx_resp->rate_n_flags);
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02002925 IWL_DEBUG_TX_REPLY("Frames %d start_idx=%d bitmap=0x%llx\n",
Zhu Yib481de92007-09-25 17:54:57 -07002926 agg->frame_count, agg->start_idx,
John W. Linville06501d22008-04-01 17:38:47 -04002927 (unsigned long long)agg->bitmap);
Zhu Yib481de92007-09-25 17:54:57 -07002928
2929 if (bitmap)
2930 agg->wait_for_ba = 1;
2931 }
2932 return 0;
2933}
2934#endif
Zhu Yib481de92007-09-25 17:54:57 -07002935
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002936/**
2937 * iwl4965_rx_reply_tx - Handle standard (non-aggregation) Tx response
2938 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002939static void iwl4965_rx_reply_tx(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002940 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07002941{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002942 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07002943 u16 sequence = le16_to_cpu(pkt->hdr.sequence);
2944 int txq_id = SEQ_TO_QUEUE(sequence);
2945 int index = SEQ_TO_INDEX(sequence);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002946 struct iwl4965_tx_queue *txq = &priv->txq[txq_id];
Zhu Yib481de92007-09-25 17:54:57 -07002947 struct ieee80211_tx_status *tx_status;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002948 struct iwl4965_tx_resp *tx_resp = (void *)&pkt->u.raw[0];
Zhu Yib481de92007-09-25 17:54:57 -07002949 u32 status = le32_to_cpu(tx_resp->status);
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08002950#ifdef CONFIG_IWL4965_HT
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02002951 int tid = MAX_TID_COUNT, sta_id = IWL_INVALID_STATION;
2952 struct ieee80211_hdr *hdr;
2953 __le16 *qc;
Zhu Yib481de92007-09-25 17:54:57 -07002954#endif
2955
2956 if ((index >= txq->q.n_bd) || (x2_queue_used(&txq->q, index) == 0)) {
2957 IWL_ERROR("Read index for DMA queue txq_id (%d) index %d "
2958 "is out of range [0-%d] %d %d\n", txq_id,
Tomas Winklerfc4b6852007-10-25 17:15:24 +08002959 index, txq->q.n_bd, txq->q.write_ptr,
2960 txq->q.read_ptr);
Zhu Yib481de92007-09-25 17:54:57 -07002961 return;
2962 }
2963
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08002964#ifdef CONFIG_IWL4965_HT
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02002965 hdr = iwl4965_tx_queue_get_hdr(priv, txq_id, index);
2966 qc = ieee80211_get_qos_ctrl(hdr);
Zhu Yib481de92007-09-25 17:54:57 -07002967
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02002968 if (qc)
Zhu Yib481de92007-09-25 17:54:57 -07002969 tid = le16_to_cpu(*qc) & 0xf;
2970
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02002971 sta_id = iwl4965_get_ra_sta_id(priv, hdr);
2972 if (txq->sched_retry && unlikely(sta_id == IWL_INVALID_STATION)) {
2973 IWL_ERROR("Station not known\n");
2974 return;
2975 }
2976
2977 if (txq->sched_retry) {
2978 const u32 scd_ssn = iwl4965_get_scd_ssn(tx_resp);
2979 struct iwl4965_ht_agg *agg = NULL;
2980
2981 if (!qc)
Zhu Yib481de92007-09-25 17:54:57 -07002982 return;
Zhu Yib481de92007-09-25 17:54:57 -07002983
2984 agg = &priv->stations[sta_id].tid[tid].agg;
2985
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02002986 iwl4965_tx_status_reply_tx(priv, agg,
2987 (struct iwl4965_tx_resp_agg *)tx_resp, index);
Zhu Yib481de92007-09-25 17:54:57 -07002988
2989 if ((tx_resp->frame_count == 1) &&
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002990 !iwl4965_is_tx_success(status)) {
Zhu Yib481de92007-09-25 17:54:57 -07002991 /* TODO: send BAR */
2992 }
2993
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02002994 if (txq->q.read_ptr != (scd_ssn & 0xff)) {
2995 int freed;
Tomas Winklerc54b6792008-03-06 17:36:53 -08002996 index = iwl_queue_dec_wrap(scd_ssn & 0xff, txq->q.n_bd);
Zhu Yib481de92007-09-25 17:54:57 -07002997 IWL_DEBUG_TX_REPLY("Retry scheduler reclaim scd_ssn "
2998 "%d index %d\n", scd_ssn , index);
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02002999 freed = iwl4965_tx_queue_reclaim(priv, txq_id, index);
3000 priv->stations[sta_id].tid[tid].tfds_in_queue -= freed;
3001
3002 if (iwl4965_queue_space(&txq->q) > txq->q.low_mark &&
3003 txq_id >= 0 && priv->mac80211_registered &&
3004 agg->state != IWL_EMPTYING_HW_QUEUE_DELBA)
3005 ieee80211_wake_queue(priv->hw, txq_id);
3006
3007 iwl4965_check_empty_hw_queue(priv, sta_id, tid, txq_id);
Zhu Yib481de92007-09-25 17:54:57 -07003008 }
3009 } else {
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08003010#endif /* CONFIG_IWL4965_HT */
Tomas Winklerfc4b6852007-10-25 17:15:24 +08003011 tx_status = &(txq->txb[txq->q.read_ptr].status);
Zhu Yib481de92007-09-25 17:54:57 -07003012
3013 tx_status->retry_count = tx_resp->failure_frame;
3014 tx_status->queue_number = status;
3015 tx_status->queue_length = tx_resp->bt_kill_count;
3016 tx_status->queue_length |= tx_resp->failure_rts;
Zhu Yib481de92007-09-25 17:54:57 -07003017 tx_status->flags =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003018 iwl4965_is_tx_success(status) ? IEEE80211_TX_STATUS_ACK : 0;
Ron Rindjunsky4c424e42008-03-04 18:09:27 -08003019 iwl4965_hwrate_to_tx_control(priv, le32_to_cpu(tx_resp->rate_n_flags),
3020 &tx_status->control);
Zhu Yib481de92007-09-25 17:54:57 -07003021
Zhu Yib481de92007-09-25 17:54:57 -07003022 IWL_DEBUG_TX("Tx queue %d Status %s (0x%08x) rate_n_flags 0x%x "
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003023 "retries %d\n", txq_id, iwl4965_get_tx_fail_reason(status),
Zhu Yib481de92007-09-25 17:54:57 -07003024 status, le32_to_cpu(tx_resp->rate_n_flags),
3025 tx_resp->failure_frame);
3026
3027 IWL_DEBUG_TX_REPLY("Tx queue reclaim %d\n", index);
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003028 if (index != -1) {
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08003029#ifdef CONFIG_IWL4965_HT
Ron Rindjunsky66b50042008-06-25 16:46:31 +08003030 int freed = iwl4965_tx_queue_reclaim(priv, txq_id, index);
3031
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003032 if (tid != MAX_TID_COUNT)
3033 priv->stations[sta_id].tid[tid].tfds_in_queue -= freed;
3034 if (iwl4965_queue_space(&txq->q) > txq->q.low_mark &&
3035 (txq_id >= 0) &&
3036 priv->mac80211_registered)
3037 ieee80211_wake_queue(priv->hw, txq_id);
3038 if (tid != MAX_TID_COUNT)
3039 iwl4965_check_empty_hw_queue(priv, sta_id, tid, txq_id);
3040#endif
Zhu Yib481de92007-09-25 17:54:57 -07003041 }
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003042#ifdef CONFIG_IWL4965_HT
3043 }
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08003044#endif /* CONFIG_IWL4965_HT */
Zhu Yib481de92007-09-25 17:54:57 -07003045
3046 if (iwl_check_bits(status, TX_ABORT_REQUIRED_MSK))
3047 IWL_ERROR("TODO: Implement Tx ABORT REQUIRED!!!\n");
3048}
3049
3050
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003051static void iwl4965_rx_reply_alive(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003052 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003053{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003054 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
3055 struct iwl4965_alive_resp *palive;
Zhu Yib481de92007-09-25 17:54:57 -07003056 struct delayed_work *pwork;
3057
3058 palive = &pkt->u.alive_frame;
3059
3060 IWL_DEBUG_INFO("Alive ucode status 0x%08X revision "
3061 "0x%01X 0x%01X\n",
3062 palive->is_valid, palive->ver_type,
3063 palive->ver_subtype);
3064
3065 if (palive->ver_subtype == INITIALIZE_SUBTYPE) {
3066 IWL_DEBUG_INFO("Initialization Alive received.\n");
3067 memcpy(&priv->card_alive_init,
3068 &pkt->u.alive_frame,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003069 sizeof(struct iwl4965_init_alive_resp));
Zhu Yib481de92007-09-25 17:54:57 -07003070 pwork = &priv->init_alive_start;
3071 } else {
3072 IWL_DEBUG_INFO("Runtime Alive received.\n");
3073 memcpy(&priv->card_alive, &pkt->u.alive_frame,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003074 sizeof(struct iwl4965_alive_resp));
Zhu Yib481de92007-09-25 17:54:57 -07003075 pwork = &priv->alive_start;
3076 }
3077
3078 /* We delay the ALIVE response by 5ms to
3079 * give the HW RF Kill time to activate... */
3080 if (palive->is_valid == UCODE_VALID_OK)
3081 queue_delayed_work(priv->workqueue, pwork,
3082 msecs_to_jiffies(5));
3083 else
3084 IWL_WARNING("uCode did not respond OK.\n");
3085}
3086
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003087static void iwl4965_rx_reply_add_sta(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003088 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003089{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003090 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07003091
3092 IWL_DEBUG_RX("Received REPLY_ADD_STA: 0x%02X\n", pkt->u.status);
3093 return;
3094}
3095
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003096static void iwl4965_rx_reply_error(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003097 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003098{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003099 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07003100
3101 IWL_ERROR("Error Reply type 0x%08X cmd %s (0x%02X) "
3102 "seq 0x%04X ser 0x%08X\n",
3103 le32_to_cpu(pkt->u.err_resp.error_type),
3104 get_cmd_string(pkt->u.err_resp.cmd_id),
3105 pkt->u.err_resp.cmd_id,
3106 le16_to_cpu(pkt->u.err_resp.bad_cmd_seq_num),
3107 le32_to_cpu(pkt->u.err_resp.error_info));
3108}
3109
3110#define TX_STATUS_ENTRY(x) case TX_STATUS_FAIL_ ## x: return #x
3111
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003112static void iwl4965_rx_csa(struct iwl_priv *priv, struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003113{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003114 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
3115 struct iwl4965_rxon_cmd *rxon = (void *)&priv->active_rxon;
3116 struct iwl4965_csa_notification *csa = &(pkt->u.csa_notif);
Zhu Yib481de92007-09-25 17:54:57 -07003117 IWL_DEBUG_11H("CSA notif: channel %d, status %d\n",
3118 le16_to_cpu(csa->channel), le32_to_cpu(csa->status));
3119 rxon->channel = csa->channel;
3120 priv->staging_rxon.channel = csa->channel;
3121}
3122
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003123static void iwl4965_rx_spectrum_measure_notif(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003124 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003125{
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08003126#ifdef CONFIG_IWL4965_SPECTRUM_MEASUREMENT
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003127 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
3128 struct iwl4965_spectrum_notification *report = &(pkt->u.spectrum_notif);
Zhu Yib481de92007-09-25 17:54:57 -07003129
3130 if (!report->state) {
3131 IWL_DEBUG(IWL_DL_11H | IWL_DL_INFO,
3132 "Spectrum Measure Notification: Start\n");
3133 return;
3134 }
3135
3136 memcpy(&priv->measure_report, report, sizeof(*report));
3137 priv->measurement_status |= MEASUREMENT_READY;
3138#endif
3139}
3140
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003141static void iwl4965_rx_pm_sleep_notif(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003142 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003143{
Tomas Winkler0a6857e2008-03-12 16:58:49 -07003144#ifdef CONFIG_IWLWIFI_DEBUG
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003145 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
3146 struct iwl4965_sleep_notification *sleep = &(pkt->u.sleep_notif);
Zhu Yib481de92007-09-25 17:54:57 -07003147 IWL_DEBUG_RX("sleep mode: %d, src: %d\n",
3148 sleep->pm_sleep_mode, sleep->pm_wakeup_src);
3149#endif
3150}
3151
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003152static void iwl4965_rx_pm_debug_statistics_notif(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003153 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003154{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003155 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07003156 IWL_DEBUG_RADIO("Dumping %d bytes of unhandled "
3157 "notification for %s:\n",
3158 le32_to_cpu(pkt->len), get_cmd_string(pkt->hdr.cmd));
Tomas Winkler0a6857e2008-03-12 16:58:49 -07003159 iwl_print_hex_dump(IWL_DL_RADIO, pkt->u.raw, le32_to_cpu(pkt->len));
Zhu Yib481de92007-09-25 17:54:57 -07003160}
3161
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003162static void iwl4965_bg_beacon_update(struct work_struct *work)
Zhu Yib481de92007-09-25 17:54:57 -07003163{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003164 struct iwl_priv *priv =
3165 container_of(work, struct iwl_priv, beacon_update);
Zhu Yib481de92007-09-25 17:54:57 -07003166 struct sk_buff *beacon;
3167
3168 /* Pull updated AP beacon from mac80211. will fail if not in AP mode */
Johannes Berg32bfd352007-12-19 01:31:26 +01003169 beacon = ieee80211_beacon_get(priv->hw, priv->vif, NULL);
Zhu Yib481de92007-09-25 17:54:57 -07003170
3171 if (!beacon) {
3172 IWL_ERROR("update beacon failed\n");
3173 return;
3174 }
3175
3176 mutex_lock(&priv->mutex);
3177 /* new beacon skb is allocated every time; dispose previous.*/
3178 if (priv->ibss_beacon)
3179 dev_kfree_skb(priv->ibss_beacon);
3180
3181 priv->ibss_beacon = beacon;
3182 mutex_unlock(&priv->mutex);
3183
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003184 iwl4965_send_beacon_cmd(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003185}
3186
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003187static void iwl4965_rx_beacon_notif(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003188 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003189{
Tomas Winkler0a6857e2008-03-12 16:58:49 -07003190#ifdef CONFIG_IWLWIFI_DEBUG
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003191 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
3192 struct iwl4965_beacon_notif *beacon = &(pkt->u.beacon_status);
3193 u8 rate = iwl4965_hw_get_rate(beacon->beacon_notify_hdr.rate_n_flags);
Zhu Yib481de92007-09-25 17:54:57 -07003194
3195 IWL_DEBUG_RX("beacon status %x retries %d iss %d "
3196 "tsf %d %d rate %d\n",
3197 le32_to_cpu(beacon->beacon_notify_hdr.status) & TX_STATUS_MSK,
3198 beacon->beacon_notify_hdr.failure_frame,
3199 le32_to_cpu(beacon->ibss_mgr_status),
3200 le32_to_cpu(beacon->high_tsf),
3201 le32_to_cpu(beacon->low_tsf), rate);
3202#endif
3203
3204 if ((priv->iw_mode == IEEE80211_IF_TYPE_AP) &&
3205 (!test_bit(STATUS_EXIT_PENDING, &priv->status)))
3206 queue_work(priv->workqueue, &priv->beacon_update);
3207}
3208
3209/* Service response to REPLY_SCAN_CMD (0x80) */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003210static void iwl4965_rx_reply_scan(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003211 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003212{
Tomas Winkler0a6857e2008-03-12 16:58:49 -07003213#ifdef CONFIG_IWLWIFI_DEBUG
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003214 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
3215 struct iwl4965_scanreq_notification *notif =
3216 (struct iwl4965_scanreq_notification *)pkt->u.raw;
Zhu Yib481de92007-09-25 17:54:57 -07003217
3218 IWL_DEBUG_RX("Scan request status = 0x%x\n", notif->status);
3219#endif
3220}
3221
3222/* Service SCAN_START_NOTIFICATION (0x82) */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003223static void iwl4965_rx_scan_start_notif(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003224 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003225{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003226 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
3227 struct iwl4965_scanstart_notification *notif =
3228 (struct iwl4965_scanstart_notification *)pkt->u.raw;
Zhu Yib481de92007-09-25 17:54:57 -07003229 priv->scan_start_tsf = le32_to_cpu(notif->tsf_low);
3230 IWL_DEBUG_SCAN("Scan start: "
3231 "%d [802.11%s] "
3232 "(TSF: 0x%08X:%08X) - %d (beacon timer %u)\n",
3233 notif->channel,
3234 notif->band ? "bg" : "a",
3235 notif->tsf_high,
3236 notif->tsf_low, notif->status, notif->beacon_timer);
3237}
3238
3239/* Service SCAN_RESULTS_NOTIFICATION (0x83) */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003240static void iwl4965_rx_scan_results_notif(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003241 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003242{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003243 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
3244 struct iwl4965_scanresults_notification *notif =
3245 (struct iwl4965_scanresults_notification *)pkt->u.raw;
Zhu Yib481de92007-09-25 17:54:57 -07003246
3247 IWL_DEBUG_SCAN("Scan ch.res: "
3248 "%d [802.11%s] "
3249 "(TSF: 0x%08X:%08X) - %d "
3250 "elapsed=%lu usec (%dms since last)\n",
3251 notif->channel,
3252 notif->band ? "bg" : "a",
3253 le32_to_cpu(notif->tsf_high),
3254 le32_to_cpu(notif->tsf_low),
3255 le32_to_cpu(notif->statistics[0]),
3256 le32_to_cpu(notif->tsf_low) - priv->scan_start_tsf,
3257 jiffies_to_msecs(elapsed_jiffies
3258 (priv->last_scan_jiffies, jiffies)));
3259
3260 priv->last_scan_jiffies = jiffies;
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08003261 priv->next_scan_jiffies = 0;
Zhu Yib481de92007-09-25 17:54:57 -07003262}
3263
3264/* Service SCAN_COMPLETE_NOTIFICATION (0x84) */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003265static void iwl4965_rx_scan_complete_notif(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003266 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003267{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003268 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
3269 struct iwl4965_scancomplete_notification *scan_notif = (void *)pkt->u.raw;
Zhu Yib481de92007-09-25 17:54:57 -07003270
3271 IWL_DEBUG_SCAN("Scan complete: %d channels (TSF 0x%08X:%08X) - %d\n",
3272 scan_notif->scanned_channels,
3273 scan_notif->tsf_low,
3274 scan_notif->tsf_high, scan_notif->status);
3275
3276 /* The HW is no longer scanning */
3277 clear_bit(STATUS_SCAN_HW, &priv->status);
3278
3279 /* The scan completion notification came in, so kill that timer... */
3280 cancel_delayed_work(&priv->scan_check);
3281
3282 IWL_DEBUG_INFO("Scan pass on %sGHz took %dms\n",
Ron Rindjunsky66b50042008-06-25 16:46:31 +08003283 (priv->scan_bands & BIT(IEEE80211_BAND_2GHZ)) ?
3284 "2.4" : "5.2",
Zhu Yib481de92007-09-25 17:54:57 -07003285 jiffies_to_msecs(elapsed_jiffies
3286 (priv->scan_pass_start, jiffies)));
3287
Ron Rindjunsky66b50042008-06-25 16:46:31 +08003288 /* Remove this scanned band from the list of pending
3289 * bands to scan, band G precedes A in order of scanning
3290 * as seen in iwl_bg_request_scan */
3291 if (priv->scan_bands & BIT(IEEE80211_BAND_2GHZ))
3292 priv->scan_bands &= ~BIT(IEEE80211_BAND_2GHZ);
3293 else if (priv->scan_bands & BIT(IEEE80211_BAND_5GHZ))
3294 priv->scan_bands &= ~BIT(IEEE80211_BAND_5GHZ);
Zhu Yib481de92007-09-25 17:54:57 -07003295
3296 /* If a request to abort was given, or the scan did not succeed
3297 * then we reset the scan state machine and terminate,
3298 * re-queuing another scan if one has been requested */
3299 if (test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
3300 IWL_DEBUG_INFO("Aborted scan completed.\n");
3301 clear_bit(STATUS_SCAN_ABORTING, &priv->status);
3302 } else {
3303 /* If there are more bands on this scan pass reschedule */
Ron Rindjunsky66b50042008-06-25 16:46:31 +08003304 if (priv->scan_bands)
Zhu Yib481de92007-09-25 17:54:57 -07003305 goto reschedule;
3306 }
3307
3308 priv->last_scan_jiffies = jiffies;
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08003309 priv->next_scan_jiffies = 0;
Zhu Yib481de92007-09-25 17:54:57 -07003310 IWL_DEBUG_INFO("Setting scan to off\n");
3311
3312 clear_bit(STATUS_SCANNING, &priv->status);
3313
3314 IWL_DEBUG_INFO("Scan took %dms\n",
3315 jiffies_to_msecs(elapsed_jiffies(priv->scan_start, jiffies)));
3316
3317 queue_work(priv->workqueue, &priv->scan_completed);
3318
3319 return;
3320
3321reschedule:
3322 priv->scan_pass_start = jiffies;
3323 queue_work(priv->workqueue, &priv->request_scan);
3324}
3325
3326/* Handle notification from uCode that card's power state is changing
3327 * due to software, hardware, or critical temperature RFKILL */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003328static void iwl4965_rx_card_state_notif(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003329 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003330{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003331 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07003332 u32 flags = le32_to_cpu(pkt->u.card_state_notif.flags);
3333 unsigned long status = priv->status;
3334
3335 IWL_DEBUG_RF_KILL("Card state received: HW:%s SW:%s\n",
3336 (flags & HW_CARD_DISABLED) ? "Kill" : "On",
3337 (flags & SW_CARD_DISABLED) ? "Kill" : "On");
3338
3339 if (flags & (SW_CARD_DISABLED | HW_CARD_DISABLED |
3340 RF_CARD_DISABLED)) {
3341
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003342 iwl_write32(priv, CSR_UCODE_DRV_GP1_SET,
Zhu Yib481de92007-09-25 17:54:57 -07003343 CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
3344
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003345 if (!iwl_grab_nic_access(priv)) {
3346 iwl_write_direct32(
Zhu Yib481de92007-09-25 17:54:57 -07003347 priv, HBUS_TARG_MBX_C,
3348 HBUS_TARG_MBX_C_REG_BIT_CMD_BLOCKED);
3349
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003350 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003351 }
3352
3353 if (!(flags & RXON_CARD_DISABLED)) {
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003354 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR,
Zhu Yib481de92007-09-25 17:54:57 -07003355 CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003356 if (!iwl_grab_nic_access(priv)) {
3357 iwl_write_direct32(
Zhu Yib481de92007-09-25 17:54:57 -07003358 priv, HBUS_TARG_MBX_C,
3359 HBUS_TARG_MBX_C_REG_BIT_CMD_BLOCKED);
3360
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003361 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003362 }
3363 }
3364
3365 if (flags & RF_CARD_DISABLED) {
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003366 iwl_write32(priv, CSR_UCODE_DRV_GP1_SET,
Zhu Yib481de92007-09-25 17:54:57 -07003367 CSR_UCODE_DRV_GP1_REG_BIT_CT_KILL_EXIT);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003368 iwl_read32(priv, CSR_UCODE_DRV_GP1);
3369 if (!iwl_grab_nic_access(priv))
3370 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003371 }
3372 }
3373
3374 if (flags & HW_CARD_DISABLED)
3375 set_bit(STATUS_RF_KILL_HW, &priv->status);
3376 else
3377 clear_bit(STATUS_RF_KILL_HW, &priv->status);
3378
3379
3380 if (flags & SW_CARD_DISABLED)
3381 set_bit(STATUS_RF_KILL_SW, &priv->status);
3382 else
3383 clear_bit(STATUS_RF_KILL_SW, &priv->status);
3384
3385 if (!(flags & RXON_CARD_DISABLED))
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003386 iwl4965_scan_cancel(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003387
3388 if ((test_bit(STATUS_RF_KILL_HW, &status) !=
3389 test_bit(STATUS_RF_KILL_HW, &priv->status)) ||
3390 (test_bit(STATUS_RF_KILL_SW, &status) !=
3391 test_bit(STATUS_RF_KILL_SW, &priv->status)))
3392 queue_work(priv->workqueue, &priv->rf_kill);
3393 else
3394 wake_up_interruptible(&priv->wait_command_queue);
3395}
3396
3397/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003398 * iwl4965_setup_rx_handlers - Initialize Rx handler callbacks
Zhu Yib481de92007-09-25 17:54:57 -07003399 *
3400 * Setup the RX handlers for each of the reply types sent from the uCode
3401 * to the host.
3402 *
3403 * This function chains into the hardware specific files for them to setup
3404 * any hardware specific handlers as well.
3405 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003406static void iwl4965_setup_rx_handlers(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003407{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003408 priv->rx_handlers[REPLY_ALIVE] = iwl4965_rx_reply_alive;
3409 priv->rx_handlers[REPLY_ADD_STA] = iwl4965_rx_reply_add_sta;
3410 priv->rx_handlers[REPLY_ERROR] = iwl4965_rx_reply_error;
3411 priv->rx_handlers[CHANNEL_SWITCH_NOTIFICATION] = iwl4965_rx_csa;
Zhu Yib481de92007-09-25 17:54:57 -07003412 priv->rx_handlers[SPECTRUM_MEASURE_NOTIFICATION] =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003413 iwl4965_rx_spectrum_measure_notif;
3414 priv->rx_handlers[PM_SLEEP_NOTIFICATION] = iwl4965_rx_pm_sleep_notif;
Zhu Yib481de92007-09-25 17:54:57 -07003415 priv->rx_handlers[PM_DEBUG_STATISTIC_NOTIFIC] =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003416 iwl4965_rx_pm_debug_statistics_notif;
3417 priv->rx_handlers[BEACON_NOTIFICATION] = iwl4965_rx_beacon_notif;
Zhu Yib481de92007-09-25 17:54:57 -07003418
Ben Cahill9fbab512007-11-29 11:09:47 +08003419 /*
3420 * The same handler is used for both the REPLY to a discrete
3421 * statistics request from the host as well as for the periodic
3422 * statistics notifications (after received beacons) from the uCode.
Zhu Yib481de92007-09-25 17:54:57 -07003423 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003424 priv->rx_handlers[REPLY_STATISTICS_CMD] = iwl4965_hw_rx_statistics;
3425 priv->rx_handlers[STATISTICS_NOTIFICATION] = iwl4965_hw_rx_statistics;
Zhu Yib481de92007-09-25 17:54:57 -07003426
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003427 priv->rx_handlers[REPLY_SCAN_CMD] = iwl4965_rx_reply_scan;
3428 priv->rx_handlers[SCAN_START_NOTIFICATION] = iwl4965_rx_scan_start_notif;
Zhu Yib481de92007-09-25 17:54:57 -07003429 priv->rx_handlers[SCAN_RESULTS_NOTIFICATION] =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003430 iwl4965_rx_scan_results_notif;
Zhu Yib481de92007-09-25 17:54:57 -07003431 priv->rx_handlers[SCAN_COMPLETE_NOTIFICATION] =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003432 iwl4965_rx_scan_complete_notif;
3433 priv->rx_handlers[CARD_STATE_NOTIFICATION] = iwl4965_rx_card_state_notif;
3434 priv->rx_handlers[REPLY_TX] = iwl4965_rx_reply_tx;
Zhu Yib481de92007-09-25 17:54:57 -07003435
Ben Cahill9fbab512007-11-29 11:09:47 +08003436 /* Set up hardware specific Rx handlers */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003437 iwl4965_hw_rx_handler_setup(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003438}
3439
3440/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003441 * iwl4965_tx_cmd_complete - Pull unused buffers off the queue and reclaim them
Zhu Yib481de92007-09-25 17:54:57 -07003442 * @rxb: Rx buffer to reclaim
3443 *
3444 * If an Rx buffer has an async callback associated with it the callback
3445 * will be executed. The attached skb (if present) will only be freed
3446 * if the callback returns 1
3447 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003448static void iwl4965_tx_cmd_complete(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003449 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003450{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003451 struct iwl4965_rx_packet *pkt = (struct iwl4965_rx_packet *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07003452 u16 sequence = le16_to_cpu(pkt->hdr.sequence);
3453 int txq_id = SEQ_TO_QUEUE(sequence);
3454 int index = SEQ_TO_INDEX(sequence);
3455 int huge = sequence & SEQ_HUGE_FRAME;
3456 int cmd_index;
Tomas Winkler857485c2008-03-21 13:53:44 -07003457 struct iwl_cmd *cmd;
Zhu Yib481de92007-09-25 17:54:57 -07003458
3459 /* If a Tx command is being handled and it isn't in the actual
3460 * command queue then there a command routing bug has been introduced
3461 * in the queue management code. */
3462 if (txq_id != IWL_CMD_QUEUE_NUM)
3463 IWL_ERROR("Error wrong command queue %d command id 0x%X\n",
3464 txq_id, pkt->hdr.cmd);
3465 BUG_ON(txq_id != IWL_CMD_QUEUE_NUM);
3466
3467 cmd_index = get_cmd_index(&priv->txq[IWL_CMD_QUEUE_NUM].q, index, huge);
3468 cmd = &priv->txq[IWL_CMD_QUEUE_NUM].cmd[cmd_index];
3469
3470 /* Input error checking is done when commands are added to queue. */
3471 if (cmd->meta.flags & CMD_WANT_SKB) {
3472 cmd->meta.source->u.skb = rxb->skb;
3473 rxb->skb = NULL;
3474 } else if (cmd->meta.u.callback &&
3475 !cmd->meta.u.callback(priv, cmd, rxb->skb))
3476 rxb->skb = NULL;
3477
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003478 iwl4965_tx_queue_reclaim(priv, txq_id, index);
Zhu Yib481de92007-09-25 17:54:57 -07003479
3480 if (!(cmd->meta.flags & CMD_ASYNC)) {
3481 clear_bit(STATUS_HCMD_ACTIVE, &priv->status);
3482 wake_up_interruptible(&priv->wait_command_queue);
3483 }
3484}
3485
3486/************************** RX-FUNCTIONS ****************************/
3487/*
3488 * Rx theory of operation
3489 *
Ben Cahill9fbab512007-11-29 11:09:47 +08003490 * Driver allocates a circular buffer of Receive Buffer Descriptors (RBDs),
3491 * each of which point to Receive Buffers to be filled by 4965. These get
3492 * used not only for Rx frames, but for any command response or notification
3493 * from the 4965. The driver and 4965 manage the Rx buffers by means
3494 * of indexes into the circular buffer.
Zhu Yib481de92007-09-25 17:54:57 -07003495 *
3496 * Rx Queue Indexes
3497 * The host/firmware share two index registers for managing the Rx buffers.
3498 *
3499 * The READ index maps to the first position that the firmware may be writing
3500 * to -- the driver can read up to (but not including) this position and get
3501 * good data.
3502 * The READ index is managed by the firmware once the card is enabled.
3503 *
3504 * The WRITE index maps to the last position the driver has read from -- the
3505 * position preceding WRITE is the last slot the firmware can place a packet.
3506 *
3507 * The queue is empty (no good data) if WRITE = READ - 1, and is full if
3508 * WRITE = READ.
3509 *
Ben Cahill9fbab512007-11-29 11:09:47 +08003510 * During initialization, the host sets up the READ queue position to the first
Zhu Yib481de92007-09-25 17:54:57 -07003511 * INDEX position, and WRITE to the last (READ - 1 wrapped)
3512 *
Ben Cahill9fbab512007-11-29 11:09:47 +08003513 * When the firmware places a packet in a buffer, it will advance the READ index
Zhu Yib481de92007-09-25 17:54:57 -07003514 * and fire the RX interrupt. The driver can then query the READ index and
3515 * process as many packets as possible, moving the WRITE index forward as it
3516 * resets the Rx queue buffers with new memory.
3517 *
3518 * The management in the driver is as follows:
3519 * + A list of pre-allocated SKBs is stored in iwl->rxq->rx_free. When
3520 * iwl->rxq->free_count drops to or below RX_LOW_WATERMARK, work is scheduled
Ian Schram01ebd062007-10-25 17:15:22 +08003521 * to replenish the iwl->rxq->rx_free.
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003522 * + In iwl4965_rx_replenish (scheduled) if 'processed' != 'read' then the
Zhu Yib481de92007-09-25 17:54:57 -07003523 * iwl->rxq is replenished and the READ INDEX is updated (updating the
3524 * 'processed' and 'read' driver indexes as well)
3525 * + A received packet is processed and handed to the kernel network stack,
3526 * detached from the iwl->rxq. The driver 'processed' index is updated.
3527 * + The Host/Firmware iwl->rxq is replenished at tasklet time from the rx_free
3528 * list. If there are no allocated buffers in iwl->rxq->rx_free, the READ
3529 * INDEX is not incremented and iwl->status(RX_STALLED) is set. If there
3530 * were enough free buffers and RX_STALLED is set it is cleared.
3531 *
3532 *
3533 * Driver sequence:
3534 *
Ben Cahill9fbab512007-11-29 11:09:47 +08003535 * iwl4965_rx_queue_alloc() Allocates rx_free
3536 * iwl4965_rx_replenish() Replenishes rx_free list from rx_used, and calls
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003537 * iwl4965_rx_queue_restock
Ben Cahill9fbab512007-11-29 11:09:47 +08003538 * iwl4965_rx_queue_restock() Moves available buffers from rx_free into Rx
Zhu Yib481de92007-09-25 17:54:57 -07003539 * queue, updates firmware pointers, and updates
3540 * the WRITE index. If insufficient rx_free buffers
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003541 * are available, schedules iwl4965_rx_replenish
Zhu Yib481de92007-09-25 17:54:57 -07003542 *
3543 * -- enable interrupts --
Ben Cahill9fbab512007-11-29 11:09:47 +08003544 * ISR - iwl4965_rx() Detach iwl4965_rx_mem_buffers from pool up to the
Zhu Yib481de92007-09-25 17:54:57 -07003545 * READ INDEX, detaching the SKB from the pool.
3546 * Moves the packet buffer from queue to rx_used.
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003547 * Calls iwl4965_rx_queue_restock to refill any empty
Zhu Yib481de92007-09-25 17:54:57 -07003548 * slots.
3549 * ...
3550 *
3551 */
3552
3553/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003554 * iwl4965_rx_queue_space - Return number of free slots available in queue.
Zhu Yib481de92007-09-25 17:54:57 -07003555 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003556static int iwl4965_rx_queue_space(const struct iwl4965_rx_queue *q)
Zhu Yib481de92007-09-25 17:54:57 -07003557{
3558 int s = q->read - q->write;
3559 if (s <= 0)
3560 s += RX_QUEUE_SIZE;
3561 /* keep some buffer to not confuse full and empty queue */
3562 s -= 2;
3563 if (s < 0)
3564 s = 0;
3565 return s;
3566}
3567
3568/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003569 * iwl4965_rx_queue_update_write_ptr - Update the write pointer for the RX queue
Zhu Yib481de92007-09-25 17:54:57 -07003570 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003571int iwl4965_rx_queue_update_write_ptr(struct iwl_priv *priv, struct iwl4965_rx_queue *q)
Zhu Yib481de92007-09-25 17:54:57 -07003572{
3573 u32 reg = 0;
3574 int rc = 0;
3575 unsigned long flags;
3576
3577 spin_lock_irqsave(&q->lock, flags);
3578
3579 if (q->need_update == 0)
3580 goto exit_unlock;
3581
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003582 /* If power-saving is in use, make sure device is awake */
Zhu Yib481de92007-09-25 17:54:57 -07003583 if (test_bit(STATUS_POWER_PMI, &priv->status)) {
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003584 reg = iwl_read32(priv, CSR_UCODE_DRV_GP1);
Zhu Yib481de92007-09-25 17:54:57 -07003585
3586 if (reg & CSR_UCODE_DRV_GP1_BIT_MAC_SLEEP) {
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003587 iwl_set_bit(priv, CSR_GP_CNTRL,
Zhu Yib481de92007-09-25 17:54:57 -07003588 CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
3589 goto exit_unlock;
3590 }
3591
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003592 rc = iwl_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003593 if (rc)
3594 goto exit_unlock;
3595
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003596 /* Device expects a multiple of 8 */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003597 iwl_write_direct32(priv, FH_RSCSR_CHNL0_WPTR,
Zhu Yib481de92007-09-25 17:54:57 -07003598 q->write & ~0x7);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003599 iwl_release_nic_access(priv);
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003600
3601 /* Else device is assumed to be awake */
Zhu Yib481de92007-09-25 17:54:57 -07003602 } else
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003603 /* Device expects a multiple of 8 */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003604 iwl_write32(priv, FH_RSCSR_CHNL0_WPTR, q->write & ~0x7);
Zhu Yib481de92007-09-25 17:54:57 -07003605
3606
3607 q->need_update = 0;
3608
3609 exit_unlock:
3610 spin_unlock_irqrestore(&q->lock, flags);
3611 return rc;
3612}
3613
3614/**
Ben Cahill9fbab512007-11-29 11:09:47 +08003615 * iwl4965_dma_addr2rbd_ptr - convert a DMA address to a uCode read buffer ptr
Zhu Yib481de92007-09-25 17:54:57 -07003616 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003617static inline __le32 iwl4965_dma_addr2rbd_ptr(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07003618 dma_addr_t dma_addr)
3619{
3620 return cpu_to_le32((u32)(dma_addr >> 8));
3621}
3622
3623
3624/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003625 * iwl4965_rx_queue_restock - refill RX queue from pre-allocated pool
Zhu Yib481de92007-09-25 17:54:57 -07003626 *
Ben Cahill9fbab512007-11-29 11:09:47 +08003627 * If there are slots in the RX queue that need to be restocked,
Zhu Yib481de92007-09-25 17:54:57 -07003628 * and we have free pre-allocated buffers, fill the ranks as much
Ben Cahill9fbab512007-11-29 11:09:47 +08003629 * as we can, pulling from rx_free.
Zhu Yib481de92007-09-25 17:54:57 -07003630 *
3631 * This moves the 'write' index forward to catch up with 'processed', and
3632 * also updates the memory address in the firmware to reference the new
3633 * target buffer.
3634 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003635static int iwl4965_rx_queue_restock(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003636{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003637 struct iwl4965_rx_queue *rxq = &priv->rxq;
Zhu Yib481de92007-09-25 17:54:57 -07003638 struct list_head *element;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003639 struct iwl4965_rx_mem_buffer *rxb;
Zhu Yib481de92007-09-25 17:54:57 -07003640 unsigned long flags;
3641 int write, rc;
3642
3643 spin_lock_irqsave(&rxq->lock, flags);
3644 write = rxq->write & ~0x7;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003645 while ((iwl4965_rx_queue_space(rxq) > 0) && (rxq->free_count)) {
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003646 /* Get next free Rx buffer, remove from free list */
Zhu Yib481de92007-09-25 17:54:57 -07003647 element = rxq->rx_free.next;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003648 rxb = list_entry(element, struct iwl4965_rx_mem_buffer, list);
Zhu Yib481de92007-09-25 17:54:57 -07003649 list_del(element);
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003650
3651 /* Point to Rx buffer via next RBD in circular buffer */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003652 rxq->bd[rxq->write] = iwl4965_dma_addr2rbd_ptr(priv, rxb->dma_addr);
Zhu Yib481de92007-09-25 17:54:57 -07003653 rxq->queue[rxq->write] = rxb;
3654 rxq->write = (rxq->write + 1) & RX_QUEUE_MASK;
3655 rxq->free_count--;
3656 }
3657 spin_unlock_irqrestore(&rxq->lock, flags);
3658 /* If the pre-allocated buffer pool is dropping low, schedule to
3659 * refill it */
3660 if (rxq->free_count <= RX_LOW_WATERMARK)
3661 queue_work(priv->workqueue, &priv->rx_replenish);
3662
3663
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003664 /* If we've added more space for the firmware to place data, tell it.
3665 * Increment device's write pointer in multiples of 8. */
Zhu Yib481de92007-09-25 17:54:57 -07003666 if ((write != (rxq->write & ~0x7))
3667 || (abs(rxq->write - rxq->read) > 7)) {
3668 spin_lock_irqsave(&rxq->lock, flags);
3669 rxq->need_update = 1;
3670 spin_unlock_irqrestore(&rxq->lock, flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003671 rc = iwl4965_rx_queue_update_write_ptr(priv, rxq);
Zhu Yib481de92007-09-25 17:54:57 -07003672 if (rc)
3673 return rc;
3674 }
3675
3676 return 0;
3677}
3678
3679/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003680 * iwl4965_rx_replenish - Move all used packet from rx_used to rx_free
Zhu Yib481de92007-09-25 17:54:57 -07003681 *
3682 * When moving to rx_free an SKB is allocated for the slot.
3683 *
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003684 * Also restock the Rx queue via iwl4965_rx_queue_restock.
Ian Schram01ebd062007-10-25 17:15:22 +08003685 * This is called as a scheduled work item (except for during initialization)
Zhu Yib481de92007-09-25 17:54:57 -07003686 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003687static void iwl4965_rx_allocate(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003688{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003689 struct iwl4965_rx_queue *rxq = &priv->rxq;
Zhu Yib481de92007-09-25 17:54:57 -07003690 struct list_head *element;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003691 struct iwl4965_rx_mem_buffer *rxb;
Zhu Yib481de92007-09-25 17:54:57 -07003692 unsigned long flags;
3693 spin_lock_irqsave(&rxq->lock, flags);
3694 while (!list_empty(&rxq->rx_used)) {
3695 element = rxq->rx_used.next;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003696 rxb = list_entry(element, struct iwl4965_rx_mem_buffer, list);
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003697
3698 /* Alloc a new receive buffer */
Zhu Yib481de92007-09-25 17:54:57 -07003699 rxb->skb =
Tomas Winkler5425e492008-04-15 16:01:38 -07003700 alloc_skb(priv->hw_params.rx_buf_size,
Ron Rindjunsky9ee1ba42007-11-26 16:14:42 +02003701 __GFP_NOWARN | GFP_ATOMIC);
Zhu Yib481de92007-09-25 17:54:57 -07003702 if (!rxb->skb) {
3703 if (net_ratelimit())
3704 printk(KERN_CRIT DRV_NAME
3705 ": Can not allocate SKB buffers\n");
3706 /* We don't reschedule replenish work here -- we will
3707 * call the restock method and if it still needs
3708 * more buffers it will schedule replenish */
3709 break;
3710 }
3711 priv->alloc_rxb_skb++;
3712 list_del(element);
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003713
3714 /* Get physical address of RB/SKB */
Zhu Yib481de92007-09-25 17:54:57 -07003715 rxb->dma_addr =
3716 pci_map_single(priv->pci_dev, rxb->skb->data,
Tomas Winkler5425e492008-04-15 16:01:38 -07003717 priv->hw_params.rx_buf_size, PCI_DMA_FROMDEVICE);
Zhu Yib481de92007-09-25 17:54:57 -07003718 list_add_tail(&rxb->list, &rxq->rx_free);
3719 rxq->free_count++;
3720 }
3721 spin_unlock_irqrestore(&rxq->lock, flags);
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08003722}
3723
3724/*
3725 * this should be called while priv->lock is locked
3726*/
Tomas Winkler4fd1f842007-12-05 20:59:58 +02003727static void __iwl4965_rx_replenish(void *data)
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08003728{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003729 struct iwl_priv *priv = data;
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08003730
3731 iwl4965_rx_allocate(priv);
3732 iwl4965_rx_queue_restock(priv);
3733}
3734
3735
3736void iwl4965_rx_replenish(void *data)
3737{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003738 struct iwl_priv *priv = data;
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08003739 unsigned long flags;
3740
3741 iwl4965_rx_allocate(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003742
3743 spin_lock_irqsave(&priv->lock, flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003744 iwl4965_rx_queue_restock(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003745 spin_unlock_irqrestore(&priv->lock, flags);
3746}
3747
3748/* Assumes that the skb field of the buffers in 'pool' is kept accurate.
Ben Cahill9fbab512007-11-29 11:09:47 +08003749 * If an SKB has been detached, the POOL needs to have its SKB set to NULL
Zhu Yib481de92007-09-25 17:54:57 -07003750 * This free routine walks the list of POOL entries and if SKB is set to
3751 * non NULL it is unmapped and freed
3752 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003753static void iwl4965_rx_queue_free(struct iwl_priv *priv, struct iwl4965_rx_queue *rxq)
Zhu Yib481de92007-09-25 17:54:57 -07003754{
3755 int i;
3756 for (i = 0; i < RX_QUEUE_SIZE + RX_FREE_BUFFERS; i++) {
3757 if (rxq->pool[i].skb != NULL) {
3758 pci_unmap_single(priv->pci_dev,
3759 rxq->pool[i].dma_addr,
Tomas Winkler5425e492008-04-15 16:01:38 -07003760 priv->hw_params.rx_buf_size,
Ron Rindjunsky9ee1ba42007-11-26 16:14:42 +02003761 PCI_DMA_FROMDEVICE);
Zhu Yib481de92007-09-25 17:54:57 -07003762 dev_kfree_skb(rxq->pool[i].skb);
3763 }
3764 }
3765
3766 pci_free_consistent(priv->pci_dev, 4 * RX_QUEUE_SIZE, rxq->bd,
3767 rxq->dma_addr);
3768 rxq->bd = NULL;
3769}
3770
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003771int iwl4965_rx_queue_alloc(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003772{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003773 struct iwl4965_rx_queue *rxq = &priv->rxq;
Zhu Yib481de92007-09-25 17:54:57 -07003774 struct pci_dev *dev = priv->pci_dev;
3775 int i;
3776
3777 spin_lock_init(&rxq->lock);
3778 INIT_LIST_HEAD(&rxq->rx_free);
3779 INIT_LIST_HEAD(&rxq->rx_used);
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003780
3781 /* Alloc the circular buffer of Read Buffer Descriptors (RBDs) */
Zhu Yib481de92007-09-25 17:54:57 -07003782 rxq->bd = pci_alloc_consistent(dev, 4 * RX_QUEUE_SIZE, &rxq->dma_addr);
3783 if (!rxq->bd)
3784 return -ENOMEM;
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003785
Zhu Yib481de92007-09-25 17:54:57 -07003786 /* Fill the rx_used queue with _all_ of the Rx buffers */
3787 for (i = 0; i < RX_FREE_BUFFERS + RX_QUEUE_SIZE; i++)
3788 list_add_tail(&rxq->pool[i].list, &rxq->rx_used);
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003789
Zhu Yib481de92007-09-25 17:54:57 -07003790 /* Set us so that we have processed and used all buffers, but have
3791 * not restocked the Rx queue with fresh buffers */
3792 rxq->read = rxq->write = 0;
3793 rxq->free_count = 0;
3794 rxq->need_update = 0;
3795 return 0;
3796}
3797
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003798void iwl4965_rx_queue_reset(struct iwl_priv *priv, struct iwl4965_rx_queue *rxq)
Zhu Yib481de92007-09-25 17:54:57 -07003799{
3800 unsigned long flags;
3801 int i;
3802 spin_lock_irqsave(&rxq->lock, flags);
3803 INIT_LIST_HEAD(&rxq->rx_free);
3804 INIT_LIST_HEAD(&rxq->rx_used);
3805 /* Fill the rx_used queue with _all_ of the Rx buffers */
3806 for (i = 0; i < RX_FREE_BUFFERS + RX_QUEUE_SIZE; i++) {
3807 /* In the reset function, these buffers may have been allocated
3808 * to an SKB, so we need to unmap and free potential storage */
3809 if (rxq->pool[i].skb != NULL) {
3810 pci_unmap_single(priv->pci_dev,
3811 rxq->pool[i].dma_addr,
Tomas Winkler5425e492008-04-15 16:01:38 -07003812 priv->hw_params.rx_buf_size,
Ron Rindjunsky9ee1ba42007-11-26 16:14:42 +02003813 PCI_DMA_FROMDEVICE);
Zhu Yib481de92007-09-25 17:54:57 -07003814 priv->alloc_rxb_skb--;
3815 dev_kfree_skb(rxq->pool[i].skb);
3816 rxq->pool[i].skb = NULL;
3817 }
3818 list_add_tail(&rxq->pool[i].list, &rxq->rx_used);
3819 }
3820
3821 /* Set us so that we have processed and used all buffers, but have
3822 * not restocked the Rx queue with fresh buffers */
3823 rxq->read = rxq->write = 0;
3824 rxq->free_count = 0;
3825 spin_unlock_irqrestore(&rxq->lock, flags);
3826}
3827
3828/* Convert linear signal-to-noise ratio into dB */
3829static u8 ratio2dB[100] = {
3830/* 0 1 2 3 4 5 6 7 8 9 */
3831 0, 0, 6, 10, 12, 14, 16, 17, 18, 19, /* 00 - 09 */
3832 20, 21, 22, 22, 23, 23, 24, 25, 26, 26, /* 10 - 19 */
3833 26, 26, 26, 27, 27, 28, 28, 28, 29, 29, /* 20 - 29 */
3834 29, 30, 30, 30, 31, 31, 31, 31, 32, 32, /* 30 - 39 */
3835 32, 32, 32, 33, 33, 33, 33, 33, 34, 34, /* 40 - 49 */
3836 34, 34, 34, 34, 35, 35, 35, 35, 35, 35, /* 50 - 59 */
3837 36, 36, 36, 36, 36, 36, 36, 37, 37, 37, /* 60 - 69 */
3838 37, 37, 37, 37, 37, 38, 38, 38, 38, 38, /* 70 - 79 */
3839 38, 38, 38, 38, 38, 39, 39, 39, 39, 39, /* 80 - 89 */
3840 39, 39, 39, 39, 39, 40, 40, 40, 40, 40 /* 90 - 99 */
3841};
3842
3843/* Calculates a relative dB value from a ratio of linear
3844 * (i.e. not dB) signal levels.
3845 * Conversion assumes that levels are voltages (20*log), not powers (10*log). */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003846int iwl4965_calc_db_from_ratio(int sig_ratio)
Zhu Yib481de92007-09-25 17:54:57 -07003847{
Adrian Bunkc899a572007-10-14 19:51:15 +02003848 /* 1000:1 or higher just report as 60 dB */
3849 if (sig_ratio >= 1000)
Zhu Yib481de92007-09-25 17:54:57 -07003850 return 60;
3851
Adrian Bunkc899a572007-10-14 19:51:15 +02003852 /* 100:1 or higher, divide by 10 and use table,
Zhu Yib481de92007-09-25 17:54:57 -07003853 * add 20 dB to make up for divide by 10 */
Adrian Bunkc899a572007-10-14 19:51:15 +02003854 if (sig_ratio >= 100)
Zhu Yib481de92007-09-25 17:54:57 -07003855 return (20 + (int)ratio2dB[sig_ratio/10]);
3856
3857 /* We shouldn't see this */
3858 if (sig_ratio < 1)
3859 return 0;
3860
3861 /* Use table for ratios 1:1 - 99:1 */
3862 return (int)ratio2dB[sig_ratio];
3863}
3864
3865#define PERFECT_RSSI (-20) /* dBm */
3866#define WORST_RSSI (-95) /* dBm */
3867#define RSSI_RANGE (PERFECT_RSSI - WORST_RSSI)
3868
3869/* Calculate an indication of rx signal quality (a percentage, not dBm!).
3870 * See http://www.ces.clemson.edu/linux/signal_quality.shtml for info
3871 * about formulas used below. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003872int iwl4965_calc_sig_qual(int rssi_dbm, int noise_dbm)
Zhu Yib481de92007-09-25 17:54:57 -07003873{
3874 int sig_qual;
3875 int degradation = PERFECT_RSSI - rssi_dbm;
3876
3877 /* If we get a noise measurement, use signal-to-noise ratio (SNR)
3878 * as indicator; formula is (signal dbm - noise dbm).
3879 * SNR at or above 40 is a great signal (100%).
3880 * Below that, scale to fit SNR of 0 - 40 dB within 0 - 100% indicator.
3881 * Weakest usable signal is usually 10 - 15 dB SNR. */
3882 if (noise_dbm) {
3883 if (rssi_dbm - noise_dbm >= 40)
3884 return 100;
3885 else if (rssi_dbm < noise_dbm)
3886 return 0;
3887 sig_qual = ((rssi_dbm - noise_dbm) * 5) / 2;
3888
3889 /* Else use just the signal level.
3890 * This formula is a least squares fit of data points collected and
3891 * compared with a reference system that had a percentage (%) display
3892 * for signal quality. */
3893 } else
3894 sig_qual = (100 * (RSSI_RANGE * RSSI_RANGE) - degradation *
3895 (15 * RSSI_RANGE + 62 * degradation)) /
3896 (RSSI_RANGE * RSSI_RANGE);
3897
3898 if (sig_qual > 100)
3899 sig_qual = 100;
3900 else if (sig_qual < 1)
3901 sig_qual = 0;
3902
3903 return sig_qual;
3904}
3905
3906/**
Ben Cahill9fbab512007-11-29 11:09:47 +08003907 * iwl4965_rx_handle - Main entry function for receiving responses from uCode
Zhu Yib481de92007-09-25 17:54:57 -07003908 *
3909 * Uses the priv->rx_handlers callback function array to invoke
3910 * the appropriate handlers, including command responses,
3911 * frame-received notifications, and other notifications.
3912 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003913static void iwl4965_rx_handle(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003914{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003915 struct iwl4965_rx_mem_buffer *rxb;
3916 struct iwl4965_rx_packet *pkt;
3917 struct iwl4965_rx_queue *rxq = &priv->rxq;
Zhu Yib481de92007-09-25 17:54:57 -07003918 u32 r, i;
3919 int reclaim;
3920 unsigned long flags;
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08003921 u8 fill_rx = 0;
Mohamed Abbasd68ab682008-02-07 13:16:33 -08003922 u32 count = 8;
Zhu Yib481de92007-09-25 17:54:57 -07003923
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003924 /* uCode's read index (stored in shared DRAM) indicates the last Rx
3925 * buffer that the driver may process (last buffer filled by ucode). */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003926 r = iwl4965_hw_get_rx_read(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003927 i = rxq->read;
3928
3929 /* Rx interrupt, but nothing sent from uCode */
3930 if (i == r)
3931 IWL_DEBUG(IWL_DL_RX | IWL_DL_ISR, "r = %d, i = %d\n", r, i);
3932
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08003933 if (iwl4965_rx_queue_space(rxq) > (RX_QUEUE_SIZE / 2))
3934 fill_rx = 1;
3935
Zhu Yib481de92007-09-25 17:54:57 -07003936 while (i != r) {
3937 rxb = rxq->queue[i];
3938
Ben Cahill9fbab512007-11-29 11:09:47 +08003939 /* If an RXB doesn't have a Rx queue slot associated with it,
Zhu Yib481de92007-09-25 17:54:57 -07003940 * then a bug has been introduced in the queue refilling
3941 * routines -- catch it here */
3942 BUG_ON(rxb == NULL);
3943
3944 rxq->queue[i] = NULL;
3945
3946 pci_dma_sync_single_for_cpu(priv->pci_dev, rxb->dma_addr,
Tomas Winkler5425e492008-04-15 16:01:38 -07003947 priv->hw_params.rx_buf_size,
Zhu Yib481de92007-09-25 17:54:57 -07003948 PCI_DMA_FROMDEVICE);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003949 pkt = (struct iwl4965_rx_packet *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07003950
3951 /* Reclaim a command buffer only if this packet is a response
3952 * to a (driver-originated) command.
3953 * If the packet (e.g. Rx frame) originated from uCode,
3954 * there is no command buffer to reclaim.
3955 * Ucode should set SEQ_RX_FRAME bit if ucode-originated,
3956 * but apparently a few don't get set; catch them here. */
3957 reclaim = !(pkt->hdr.sequence & SEQ_RX_FRAME) &&
3958 (pkt->hdr.cmd != REPLY_RX_PHY_CMD) &&
Tomas Winkler857485c2008-03-21 13:53:44 -07003959 (pkt->hdr.cmd != REPLY_RX) &&
Zhu Yicfe01702007-09-27 11:27:31 +08003960 (pkt->hdr.cmd != REPLY_COMPRESSED_BA) &&
Zhu Yib481de92007-09-25 17:54:57 -07003961 (pkt->hdr.cmd != STATISTICS_NOTIFICATION) &&
3962 (pkt->hdr.cmd != REPLY_TX);
3963
3964 /* Based on type of command response or notification,
3965 * handle those that need handling via function in
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003966 * rx_handlers table. See iwl4965_setup_rx_handlers() */
Zhu Yib481de92007-09-25 17:54:57 -07003967 if (priv->rx_handlers[pkt->hdr.cmd]) {
3968 IWL_DEBUG(IWL_DL_HOST_COMMAND | IWL_DL_RX | IWL_DL_ISR,
3969 "r = %d, i = %d, %s, 0x%02x\n", r, i,
3970 get_cmd_string(pkt->hdr.cmd), pkt->hdr.cmd);
3971 priv->rx_handlers[pkt->hdr.cmd] (priv, rxb);
3972 } else {
3973 /* No handling needed */
3974 IWL_DEBUG(IWL_DL_HOST_COMMAND | IWL_DL_RX | IWL_DL_ISR,
3975 "r %d i %d No handler needed for %s, 0x%02x\n",
3976 r, i, get_cmd_string(pkt->hdr.cmd),
3977 pkt->hdr.cmd);
3978 }
3979
3980 if (reclaim) {
Ben Cahill9fbab512007-11-29 11:09:47 +08003981 /* Invoke any callbacks, transfer the skb to caller, and
Tomas Winkler857485c2008-03-21 13:53:44 -07003982 * fire off the (possibly) blocking iwl_send_cmd()
Zhu Yib481de92007-09-25 17:54:57 -07003983 * as we reclaim the driver command queue */
3984 if (rxb && rxb->skb)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003985 iwl4965_tx_cmd_complete(priv, rxb);
Zhu Yib481de92007-09-25 17:54:57 -07003986 else
3987 IWL_WARNING("Claim null rxb?\n");
3988 }
3989
3990 /* For now we just don't re-use anything. We can tweak this
3991 * later to try and re-use notification packets and SKBs that
3992 * fail to Rx correctly */
3993 if (rxb->skb != NULL) {
3994 priv->alloc_rxb_skb--;
3995 dev_kfree_skb_any(rxb->skb);
3996 rxb->skb = NULL;
3997 }
3998
3999 pci_unmap_single(priv->pci_dev, rxb->dma_addr,
Tomas Winkler5425e492008-04-15 16:01:38 -07004000 priv->hw_params.rx_buf_size,
Ron Rindjunsky9ee1ba42007-11-26 16:14:42 +02004001 PCI_DMA_FROMDEVICE);
Zhu Yib481de92007-09-25 17:54:57 -07004002 spin_lock_irqsave(&rxq->lock, flags);
4003 list_add_tail(&rxb->list, &priv->rxq.rx_used);
4004 spin_unlock_irqrestore(&rxq->lock, flags);
4005 i = (i + 1) & RX_QUEUE_MASK;
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08004006 /* If there are a lot of unused frames,
4007 * restock the Rx queue so ucode wont assert. */
4008 if (fill_rx) {
4009 count++;
4010 if (count >= 8) {
4011 priv->rxq.read = i;
4012 __iwl4965_rx_replenish(priv);
4013 count = 0;
4014 }
4015 }
Zhu Yib481de92007-09-25 17:54:57 -07004016 }
4017
4018 /* Backtrack one entry */
4019 priv->rxq.read = i;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004020 iwl4965_rx_queue_restock(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004021}
4022
Cahill, Ben M6440adb2007-11-29 11:09:55 +08004023/**
4024 * iwl4965_tx_queue_update_write_ptr - Send new write index to hardware
4025 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004026static int iwl4965_tx_queue_update_write_ptr(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004027 struct iwl4965_tx_queue *txq)
Zhu Yib481de92007-09-25 17:54:57 -07004028{
4029 u32 reg = 0;
4030 int rc = 0;
4031 int txq_id = txq->q.id;
4032
4033 if (txq->need_update == 0)
4034 return rc;
4035
4036 /* if we're trying to save power */
4037 if (test_bit(STATUS_POWER_PMI, &priv->status)) {
4038 /* wake up nic if it's powered down ...
4039 * uCode will wake up, and interrupt us again, so next
4040 * time we'll skip this part. */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004041 reg = iwl_read32(priv, CSR_UCODE_DRV_GP1);
Zhu Yib481de92007-09-25 17:54:57 -07004042
4043 if (reg & CSR_UCODE_DRV_GP1_BIT_MAC_SLEEP) {
4044 IWL_DEBUG_INFO("Requesting wakeup, GP1 = 0x%x\n", reg);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004045 iwl_set_bit(priv, CSR_GP_CNTRL,
Zhu Yib481de92007-09-25 17:54:57 -07004046 CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
4047 return rc;
4048 }
4049
4050 /* restore this queue's parameters in nic hardware. */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004051 rc = iwl_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004052 if (rc)
4053 return rc;
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004054 iwl_write_direct32(priv, HBUS_TARG_WRPTR,
Tomas Winklerfc4b6852007-10-25 17:15:24 +08004055 txq->q.write_ptr | (txq_id << 8));
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004056 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004057
4058 /* else not in power-save mode, uCode will never sleep when we're
4059 * trying to tx (during RFKILL, we're not trying to tx). */
4060 } else
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004061 iwl_write32(priv, HBUS_TARG_WRPTR,
Tomas Winklerfc4b6852007-10-25 17:15:24 +08004062 txq->q.write_ptr | (txq_id << 8));
Zhu Yib481de92007-09-25 17:54:57 -07004063
4064 txq->need_update = 0;
4065
4066 return rc;
4067}
4068
Tomas Winkler0a6857e2008-03-12 16:58:49 -07004069#ifdef CONFIG_IWLWIFI_DEBUG
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004070static void iwl4965_print_rx_config_cmd(struct iwl4965_rxon_cmd *rxon)
Zhu Yib481de92007-09-25 17:54:57 -07004071{
Joe Perches0795af52007-10-03 17:59:30 -07004072 DECLARE_MAC_BUF(mac);
4073
Zhu Yib481de92007-09-25 17:54:57 -07004074 IWL_DEBUG_RADIO("RX CONFIG:\n");
Tomas Winkler0a6857e2008-03-12 16:58:49 -07004075 iwl_print_hex_dump(IWL_DL_RADIO, (u8 *) rxon, sizeof(*rxon));
Zhu Yib481de92007-09-25 17:54:57 -07004076 IWL_DEBUG_RADIO("u16 channel: 0x%x\n", le16_to_cpu(rxon->channel));
4077 IWL_DEBUG_RADIO("u32 flags: 0x%08X\n", le32_to_cpu(rxon->flags));
4078 IWL_DEBUG_RADIO("u32 filter_flags: 0x%08x\n",
4079 le32_to_cpu(rxon->filter_flags));
4080 IWL_DEBUG_RADIO("u8 dev_type: 0x%x\n", rxon->dev_type);
4081 IWL_DEBUG_RADIO("u8 ofdm_basic_rates: 0x%02x\n",
4082 rxon->ofdm_basic_rates);
4083 IWL_DEBUG_RADIO("u8 cck_basic_rates: 0x%02x\n", rxon->cck_basic_rates);
Joe Perches0795af52007-10-03 17:59:30 -07004084 IWL_DEBUG_RADIO("u8[6] node_addr: %s\n",
4085 print_mac(mac, rxon->node_addr));
4086 IWL_DEBUG_RADIO("u8[6] bssid_addr: %s\n",
4087 print_mac(mac, rxon->bssid_addr));
Zhu Yib481de92007-09-25 17:54:57 -07004088 IWL_DEBUG_RADIO("u16 assoc_id: 0x%x\n", le16_to_cpu(rxon->assoc_id));
4089}
4090#endif
4091
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004092static void iwl4965_enable_interrupts(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004093{
4094 IWL_DEBUG_ISR("Enabling interrupts\n");
4095 set_bit(STATUS_INT_ENABLED, &priv->status);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004096 iwl_write32(priv, CSR_INT_MASK, CSR_INI_SET_MASK);
Zhu Yib481de92007-09-25 17:54:57 -07004097}
4098
Mohamed Abbas0359fac2008-03-28 16:21:08 -07004099/* call this function to flush any scheduled tasklet */
4100static inline void iwl_synchronize_irq(struct iwl_priv *priv)
4101{
4102 /* wait to make sure we flush pedding tasklet*/
4103 synchronize_irq(priv->pci_dev->irq);
4104 tasklet_kill(&priv->irq_tasklet);
4105}
4106
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004107static inline void iwl4965_disable_interrupts(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004108{
4109 clear_bit(STATUS_INT_ENABLED, &priv->status);
4110
4111 /* disable interrupts from uCode/NIC to host */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004112 iwl_write32(priv, CSR_INT_MASK, 0x00000000);
Zhu Yib481de92007-09-25 17:54:57 -07004113
4114 /* acknowledge/clear/reset any interrupts still pending
4115 * from uCode or flow handler (Rx/Tx DMA) */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004116 iwl_write32(priv, CSR_INT, 0xffffffff);
4117 iwl_write32(priv, CSR_FH_INT_STATUS, 0xffffffff);
Zhu Yib481de92007-09-25 17:54:57 -07004118 IWL_DEBUG_ISR("Disabled interrupts\n");
4119}
4120
4121static const char *desc_lookup(int i)
4122{
4123 switch (i) {
4124 case 1:
4125 return "FAIL";
4126 case 2:
4127 return "BAD_PARAM";
4128 case 3:
4129 return "BAD_CHECKSUM";
4130 case 4:
4131 return "NMI_INTERRUPT";
4132 case 5:
4133 return "SYSASSERT";
4134 case 6:
4135 return "FATAL_ERROR";
4136 }
4137
4138 return "UNKNOWN";
4139}
4140
4141#define ERROR_START_OFFSET (1 * sizeof(u32))
4142#define ERROR_ELEM_SIZE (7 * sizeof(u32))
4143
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004144static void iwl4965_dump_nic_error_log(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004145{
4146 u32 data2, line;
4147 u32 desc, time, count, base, data1;
4148 u32 blink1, blink2, ilink1, ilink2;
4149 int rc;
4150
4151 base = le32_to_cpu(priv->card_alive.error_event_table_ptr);
4152
Tomas Winkler57aab752008-04-14 21:16:03 -07004153 if (!priv->cfg->ops->lib->is_valid_rtc_data_addr(base)) {
Zhu Yib481de92007-09-25 17:54:57 -07004154 IWL_ERROR("Not valid error log pointer 0x%08X\n", base);
4155 return;
4156 }
4157
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004158 rc = iwl_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004159 if (rc) {
4160 IWL_WARNING("Can not read from adapter at this time.\n");
4161 return;
4162 }
4163
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004164 count = iwl_read_targ_mem(priv, base);
Zhu Yib481de92007-09-25 17:54:57 -07004165
4166 if (ERROR_START_OFFSET <= count * ERROR_ELEM_SIZE) {
4167 IWL_ERROR("Start IWL Error Log Dump:\n");
Tomas Winkler2acae162008-03-02 01:25:59 +02004168 IWL_ERROR("Status: 0x%08lX, count: %d\n", priv->status, count);
Zhu Yib481de92007-09-25 17:54:57 -07004169 }
4170
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004171 desc = iwl_read_targ_mem(priv, base + 1 * sizeof(u32));
4172 blink1 = iwl_read_targ_mem(priv, base + 3 * sizeof(u32));
4173 blink2 = iwl_read_targ_mem(priv, base + 4 * sizeof(u32));
4174 ilink1 = iwl_read_targ_mem(priv, base + 5 * sizeof(u32));
4175 ilink2 = iwl_read_targ_mem(priv, base + 6 * sizeof(u32));
4176 data1 = iwl_read_targ_mem(priv, base + 7 * sizeof(u32));
4177 data2 = iwl_read_targ_mem(priv, base + 8 * sizeof(u32));
4178 line = iwl_read_targ_mem(priv, base + 9 * sizeof(u32));
4179 time = iwl_read_targ_mem(priv, base + 11 * sizeof(u32));
Zhu Yib481de92007-09-25 17:54:57 -07004180
4181 IWL_ERROR("Desc Time "
4182 "data1 data2 line\n");
4183 IWL_ERROR("%-13s (#%d) %010u 0x%08X 0x%08X %u\n",
4184 desc_lookup(desc), desc, time, data1, data2, line);
4185 IWL_ERROR("blink1 blink2 ilink1 ilink2\n");
4186 IWL_ERROR("0x%05X 0x%05X 0x%05X 0x%05X\n", blink1, blink2,
4187 ilink1, ilink2);
4188
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004189 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004190}
4191
4192#define EVENT_START_OFFSET (4 * sizeof(u32))
4193
4194/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004195 * iwl4965_print_event_log - Dump error event log to syslog
Zhu Yib481de92007-09-25 17:54:57 -07004196 *
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004197 * NOTE: Must be called with iwl_grab_nic_access() already obtained!
Zhu Yib481de92007-09-25 17:54:57 -07004198 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004199static void iwl4965_print_event_log(struct iwl_priv *priv, u32 start_idx,
Zhu Yib481de92007-09-25 17:54:57 -07004200 u32 num_events, u32 mode)
4201{
4202 u32 i;
4203 u32 base; /* SRAM byte address of event log header */
4204 u32 event_size; /* 2 u32s, or 3 u32s if timestamp recorded */
4205 u32 ptr; /* SRAM byte address of log data */
4206 u32 ev, time, data; /* event log data */
4207
4208 if (num_events == 0)
4209 return;
4210
4211 base = le32_to_cpu(priv->card_alive.log_event_table_ptr);
4212
4213 if (mode == 0)
4214 event_size = 2 * sizeof(u32);
4215 else
4216 event_size = 3 * sizeof(u32);
4217
4218 ptr = base + EVENT_START_OFFSET + (start_idx * event_size);
4219
4220 /* "time" is actually "data" for mode 0 (no timestamp).
4221 * place event id # at far right for easier visual parsing. */
4222 for (i = 0; i < num_events; i++) {
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004223 ev = iwl_read_targ_mem(priv, ptr);
Zhu Yib481de92007-09-25 17:54:57 -07004224 ptr += sizeof(u32);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004225 time = iwl_read_targ_mem(priv, ptr);
Zhu Yib481de92007-09-25 17:54:57 -07004226 ptr += sizeof(u32);
4227 if (mode == 0)
4228 IWL_ERROR("0x%08x\t%04u\n", time, ev); /* data, ev */
4229 else {
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004230 data = iwl_read_targ_mem(priv, ptr);
Zhu Yib481de92007-09-25 17:54:57 -07004231 ptr += sizeof(u32);
4232 IWL_ERROR("%010u\t0x%08x\t%04u\n", time, data, ev);
4233 }
4234 }
4235}
4236
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004237static void iwl4965_dump_nic_event_log(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004238{
4239 int rc;
4240 u32 base; /* SRAM byte address of event log header */
4241 u32 capacity; /* event log capacity in # entries */
4242 u32 mode; /* 0 - no timestamp, 1 - timestamp recorded */
4243 u32 num_wraps; /* # times uCode wrapped to top of log */
4244 u32 next_entry; /* index of next entry to be written by uCode */
4245 u32 size; /* # entries that we'll print */
4246
4247 base = le32_to_cpu(priv->card_alive.log_event_table_ptr);
Tomas Winkler57aab752008-04-14 21:16:03 -07004248 if (!priv->cfg->ops->lib->is_valid_rtc_data_addr(base)) {
Zhu Yib481de92007-09-25 17:54:57 -07004249 IWL_ERROR("Invalid event log pointer 0x%08X\n", base);
4250 return;
4251 }
4252
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004253 rc = iwl_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004254 if (rc) {
4255 IWL_WARNING("Can not read from adapter at this time.\n");
4256 return;
4257 }
4258
4259 /* event log header */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004260 capacity = iwl_read_targ_mem(priv, base);
4261 mode = iwl_read_targ_mem(priv, base + (1 * sizeof(u32)));
4262 num_wraps = iwl_read_targ_mem(priv, base + (2 * sizeof(u32)));
4263 next_entry = iwl_read_targ_mem(priv, base + (3 * sizeof(u32)));
Zhu Yib481de92007-09-25 17:54:57 -07004264
4265 size = num_wraps ? capacity : next_entry;
4266
4267 /* bail out if nothing in log */
4268 if (size == 0) {
Zhu Yi583fab32007-09-27 11:27:30 +08004269 IWL_ERROR("Start IWL Event Log Dump: nothing in log\n");
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004270 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004271 return;
4272 }
4273
Zhu Yi583fab32007-09-27 11:27:30 +08004274 IWL_ERROR("Start IWL Event Log Dump: display count %d, wraps %d\n",
Zhu Yib481de92007-09-25 17:54:57 -07004275 size, num_wraps);
4276
4277 /* if uCode has wrapped back to top of log, start at the oldest entry,
4278 * i.e the next one that uCode would fill. */
4279 if (num_wraps)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004280 iwl4965_print_event_log(priv, next_entry,
Zhu Yib481de92007-09-25 17:54:57 -07004281 capacity - next_entry, mode);
4282
4283 /* (then/else) start at top of log */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004284 iwl4965_print_event_log(priv, 0, next_entry, mode);
Zhu Yib481de92007-09-25 17:54:57 -07004285
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004286 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004287}
4288
4289/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004290 * iwl4965_irq_handle_error - called for HW or SW error interrupt from card
Zhu Yib481de92007-09-25 17:54:57 -07004291 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004292static void iwl4965_irq_handle_error(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004293{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004294 /* Set the FW error flag -- cleared on iwl4965_down */
Zhu Yib481de92007-09-25 17:54:57 -07004295 set_bit(STATUS_FW_ERROR, &priv->status);
4296
4297 /* Cancel currently queued command. */
4298 clear_bit(STATUS_HCMD_ACTIVE, &priv->status);
4299
Tomas Winkler0a6857e2008-03-12 16:58:49 -07004300#ifdef CONFIG_IWLWIFI_DEBUG
4301 if (iwl_debug_level & IWL_DL_FW_ERRORS) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004302 iwl4965_dump_nic_error_log(priv);
4303 iwl4965_dump_nic_event_log(priv);
4304 iwl4965_print_rx_config_cmd(&priv->staging_rxon);
Zhu Yib481de92007-09-25 17:54:57 -07004305 }
4306#endif
4307
4308 wake_up_interruptible(&priv->wait_command_queue);
4309
4310 /* Keep the restart process from trying to send host
4311 * commands by clearing the INIT status bit */
4312 clear_bit(STATUS_READY, &priv->status);
4313
4314 if (!test_bit(STATUS_EXIT_PENDING, &priv->status)) {
4315 IWL_DEBUG(IWL_DL_INFO | IWL_DL_FW_ERRORS,
4316 "Restarting adapter due to uCode error.\n");
4317
Tomas Winkler3109ece2008-03-28 16:33:35 -07004318 if (iwl_is_associated(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07004319 memcpy(&priv->recovery_rxon, &priv->active_rxon,
4320 sizeof(priv->recovery_rxon));
4321 priv->error_recovering = 1;
4322 }
4323 queue_work(priv->workqueue, &priv->restart);
4324 }
4325}
4326
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004327static void iwl4965_error_recovery(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004328{
4329 unsigned long flags;
4330
4331 memcpy(&priv->staging_rxon, &priv->recovery_rxon,
4332 sizeof(priv->staging_rxon));
4333 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004334 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004335
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004336 iwl4965_rxon_add_station(priv, priv->bssid, 1);
Zhu Yib481de92007-09-25 17:54:57 -07004337
4338 spin_lock_irqsave(&priv->lock, flags);
4339 priv->assoc_id = le16_to_cpu(priv->staging_rxon.assoc_id);
4340 priv->error_recovering = 0;
4341 spin_unlock_irqrestore(&priv->lock, flags);
4342}
4343
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004344static void iwl4965_irq_tasklet(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004345{
4346 u32 inta, handled = 0;
4347 u32 inta_fh;
4348 unsigned long flags;
Tomas Winkler0a6857e2008-03-12 16:58:49 -07004349#ifdef CONFIG_IWLWIFI_DEBUG
Zhu Yib481de92007-09-25 17:54:57 -07004350 u32 inta_mask;
4351#endif
4352
4353 spin_lock_irqsave(&priv->lock, flags);
4354
4355 /* Ack/clear/reset pending uCode interrupts.
4356 * Note: Some bits in CSR_INT are "OR" of bits in CSR_FH_INT_STATUS,
4357 * and will clear only when CSR_FH_INT_STATUS gets cleared. */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004358 inta = iwl_read32(priv, CSR_INT);
4359 iwl_write32(priv, CSR_INT, inta);
Zhu Yib481de92007-09-25 17:54:57 -07004360
4361 /* Ack/clear/reset pending flow-handler (DMA) interrupts.
4362 * Any new interrupts that happen after this, either while we're
4363 * in this tasklet, or later, will show up in next ISR/tasklet. */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004364 inta_fh = iwl_read32(priv, CSR_FH_INT_STATUS);
4365 iwl_write32(priv, CSR_FH_INT_STATUS, inta_fh);
Zhu Yib481de92007-09-25 17:54:57 -07004366
Tomas Winkler0a6857e2008-03-12 16:58:49 -07004367#ifdef CONFIG_IWLWIFI_DEBUG
4368 if (iwl_debug_level & IWL_DL_ISR) {
Ben Cahill9fbab512007-11-29 11:09:47 +08004369 /* just for debug */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004370 inta_mask = iwl_read32(priv, CSR_INT_MASK);
Zhu Yib481de92007-09-25 17:54:57 -07004371 IWL_DEBUG_ISR("inta 0x%08x, enabled 0x%08x, fh 0x%08x\n",
4372 inta, inta_mask, inta_fh);
4373 }
4374#endif
4375
4376 /* Since CSR_INT and CSR_FH_INT_STATUS reads and clears are not
4377 * atomic, make sure that inta covers all the interrupts that
4378 * we've discovered, even if FH interrupt came in just after
4379 * reading CSR_INT. */
Tomas Winkler6f83eaa2008-03-04 18:09:28 -08004380 if (inta_fh & CSR49_FH_INT_RX_MASK)
Zhu Yib481de92007-09-25 17:54:57 -07004381 inta |= CSR_INT_BIT_FH_RX;
Tomas Winkler6f83eaa2008-03-04 18:09:28 -08004382 if (inta_fh & CSR49_FH_INT_TX_MASK)
Zhu Yib481de92007-09-25 17:54:57 -07004383 inta |= CSR_INT_BIT_FH_TX;
4384
4385 /* Now service all interrupt bits discovered above. */
4386 if (inta & CSR_INT_BIT_HW_ERR) {
4387 IWL_ERROR("Microcode HW error detected. Restarting.\n");
4388
4389 /* Tell the device to stop sending interrupts */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004390 iwl4965_disable_interrupts(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004391
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004392 iwl4965_irq_handle_error(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004393
4394 handled |= CSR_INT_BIT_HW_ERR;
4395
4396 spin_unlock_irqrestore(&priv->lock, flags);
4397
4398 return;
4399 }
4400
Tomas Winkler0a6857e2008-03-12 16:58:49 -07004401#ifdef CONFIG_IWLWIFI_DEBUG
4402 if (iwl_debug_level & (IWL_DL_ISR)) {
Zhu Yib481de92007-09-25 17:54:57 -07004403 /* NIC fires this, but we don't use it, redundant with WAKEUP */
Joonwoo Park25c03d82008-01-23 10:15:20 -08004404 if (inta & CSR_INT_BIT_SCD)
4405 IWL_DEBUG_ISR("Scheduler finished to transmit "
4406 "the frame/frames.\n");
Zhu Yib481de92007-09-25 17:54:57 -07004407
4408 /* Alive notification via Rx interrupt will do the real work */
4409 if (inta & CSR_INT_BIT_ALIVE)
4410 IWL_DEBUG_ISR("Alive interrupt\n");
4411 }
4412#endif
4413 /* Safely ignore these bits for debug checks below */
Joonwoo Park25c03d82008-01-23 10:15:20 -08004414 inta &= ~(CSR_INT_BIT_SCD | CSR_INT_BIT_ALIVE);
Zhu Yib481de92007-09-25 17:54:57 -07004415
Ben Cahill9fbab512007-11-29 11:09:47 +08004416 /* HW RF KILL switch toggled */
Zhu Yib481de92007-09-25 17:54:57 -07004417 if (inta & CSR_INT_BIT_RF_KILL) {
4418 int hw_rf_kill = 0;
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004419 if (!(iwl_read32(priv, CSR_GP_CNTRL) &
Zhu Yib481de92007-09-25 17:54:57 -07004420 CSR_GP_CNTRL_REG_FLAG_HW_RF_KILL_SW))
4421 hw_rf_kill = 1;
4422
4423 IWL_DEBUG(IWL_DL_INFO | IWL_DL_RF_KILL | IWL_DL_ISR,
4424 "RF_KILL bit toggled to %s.\n",
4425 hw_rf_kill ? "disable radio":"enable radio");
4426
4427 /* Queue restart only if RF_KILL switch was set to "kill"
4428 * when we loaded driver, and is now set to "enable".
4429 * After we're Alive, RF_KILL gets handled by
Reinette Chatre32304552008-02-15 14:34:37 -08004430 * iwl4965_rx_card_state_notif() */
Zhu Yi53e49092007-12-06 16:08:44 +08004431 if (!hw_rf_kill && !test_bit(STATUS_ALIVE, &priv->status)) {
4432 clear_bit(STATUS_RF_KILL_HW, &priv->status);
Zhu Yib481de92007-09-25 17:54:57 -07004433 queue_work(priv->workqueue, &priv->restart);
Zhu Yi53e49092007-12-06 16:08:44 +08004434 }
Zhu Yib481de92007-09-25 17:54:57 -07004435
4436 handled |= CSR_INT_BIT_RF_KILL;
4437 }
4438
Ben Cahill9fbab512007-11-29 11:09:47 +08004439 /* Chip got too hot and stopped itself */
Zhu Yib481de92007-09-25 17:54:57 -07004440 if (inta & CSR_INT_BIT_CT_KILL) {
4441 IWL_ERROR("Microcode CT kill error detected.\n");
4442 handled |= CSR_INT_BIT_CT_KILL;
4443 }
4444
4445 /* Error detected by uCode */
4446 if (inta & CSR_INT_BIT_SW_ERR) {
4447 IWL_ERROR("Microcode SW error detected. Restarting 0x%X.\n",
4448 inta);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004449 iwl4965_irq_handle_error(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004450 handled |= CSR_INT_BIT_SW_ERR;
4451 }
4452
4453 /* uCode wakes up after power-down sleep */
4454 if (inta & CSR_INT_BIT_WAKEUP) {
4455 IWL_DEBUG_ISR("Wakeup interrupt\n");
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004456 iwl4965_rx_queue_update_write_ptr(priv, &priv->rxq);
4457 iwl4965_tx_queue_update_write_ptr(priv, &priv->txq[0]);
4458 iwl4965_tx_queue_update_write_ptr(priv, &priv->txq[1]);
4459 iwl4965_tx_queue_update_write_ptr(priv, &priv->txq[2]);
4460 iwl4965_tx_queue_update_write_ptr(priv, &priv->txq[3]);
4461 iwl4965_tx_queue_update_write_ptr(priv, &priv->txq[4]);
4462 iwl4965_tx_queue_update_write_ptr(priv, &priv->txq[5]);
Zhu Yib481de92007-09-25 17:54:57 -07004463
4464 handled |= CSR_INT_BIT_WAKEUP;
4465 }
4466
4467 /* All uCode command responses, including Tx command responses,
4468 * Rx "responses" (frame-received notification), and other
4469 * notifications from uCode come through here*/
4470 if (inta & (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX)) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004471 iwl4965_rx_handle(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004472 handled |= (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX);
4473 }
4474
4475 if (inta & CSR_INT_BIT_FH_TX) {
4476 IWL_DEBUG_ISR("Tx interrupt\n");
4477 handled |= CSR_INT_BIT_FH_TX;
4478 }
4479
4480 if (inta & ~handled)
4481 IWL_ERROR("Unhandled INTA bits 0x%08x\n", inta & ~handled);
4482
4483 if (inta & ~CSR_INI_SET_MASK) {
4484 IWL_WARNING("Disabled INTA bits 0x%08x were pending\n",
4485 inta & ~CSR_INI_SET_MASK);
4486 IWL_WARNING(" with FH_INT = 0x%08x\n", inta_fh);
4487 }
4488
4489 /* Re-enable all interrupts */
Mohamed Abbas0359fac2008-03-28 16:21:08 -07004490 /* only Re-enable if diabled by irq */
4491 if (test_bit(STATUS_INT_ENABLED, &priv->status))
4492 iwl4965_enable_interrupts(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004493
Tomas Winkler0a6857e2008-03-12 16:58:49 -07004494#ifdef CONFIG_IWLWIFI_DEBUG
4495 if (iwl_debug_level & (IWL_DL_ISR)) {
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004496 inta = iwl_read32(priv, CSR_INT);
4497 inta_mask = iwl_read32(priv, CSR_INT_MASK);
4498 inta_fh = iwl_read32(priv, CSR_FH_INT_STATUS);
Zhu Yib481de92007-09-25 17:54:57 -07004499 IWL_DEBUG_ISR("End inta 0x%08x, enabled 0x%08x, fh 0x%08x, "
4500 "flags 0x%08lx\n", inta, inta_mask, inta_fh, flags);
4501 }
4502#endif
4503 spin_unlock_irqrestore(&priv->lock, flags);
4504}
4505
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004506static irqreturn_t iwl4965_isr(int irq, void *data)
Zhu Yib481de92007-09-25 17:54:57 -07004507{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004508 struct iwl_priv *priv = data;
Zhu Yib481de92007-09-25 17:54:57 -07004509 u32 inta, inta_mask;
4510 u32 inta_fh;
4511 if (!priv)
4512 return IRQ_NONE;
4513
4514 spin_lock(&priv->lock);
4515
4516 /* Disable (but don't clear!) interrupts here to avoid
4517 * back-to-back ISRs and sporadic interrupts from our NIC.
4518 * If we have something to service, the tasklet will re-enable ints.
4519 * If we *don't* have something, we'll re-enable before leaving here. */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004520 inta_mask = iwl_read32(priv, CSR_INT_MASK); /* just for debug */
4521 iwl_write32(priv, CSR_INT_MASK, 0x00000000);
Zhu Yib481de92007-09-25 17:54:57 -07004522
4523 /* Discover which interrupts are active/pending */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004524 inta = iwl_read32(priv, CSR_INT);
4525 inta_fh = iwl_read32(priv, CSR_FH_INT_STATUS);
Zhu Yib481de92007-09-25 17:54:57 -07004526
4527 /* Ignore interrupt if there's nothing in NIC to service.
4528 * This may be due to IRQ shared with another device,
4529 * or due to sporadic interrupts thrown from our NIC. */
4530 if (!inta && !inta_fh) {
4531 IWL_DEBUG_ISR("Ignore interrupt, inta == 0, inta_fh == 0\n");
4532 goto none;
4533 }
4534
4535 if ((inta == 0xFFFFFFFF) || ((inta & 0xFFFFFFF0) == 0xa5a5a5a0)) {
Oliver Neukum66fbb542007-11-15 09:31:10 +08004536 /* Hardware disappeared. It might have already raised
4537 * an interrupt */
Zhu Yib481de92007-09-25 17:54:57 -07004538 IWL_WARNING("HARDWARE GONE?? INTA == 0x%080x\n", inta);
Oliver Neukum66fbb542007-11-15 09:31:10 +08004539 goto unplugged;
Zhu Yib481de92007-09-25 17:54:57 -07004540 }
4541
4542 IWL_DEBUG_ISR("ISR inta 0x%08x, enabled 0x%08x, fh 0x%08x\n",
4543 inta, inta_mask, inta_fh);
4544
Joonwoo Park25c03d82008-01-23 10:15:20 -08004545 inta &= ~CSR_INT_BIT_SCD;
4546
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004547 /* iwl4965_irq_tasklet() will service interrupts and re-enable them */
Joonwoo Park25c03d82008-01-23 10:15:20 -08004548 if (likely(inta || inta_fh))
4549 tasklet_schedule(&priv->irq_tasklet);
Zhu Yib481de92007-09-25 17:54:57 -07004550
Oliver Neukum66fbb542007-11-15 09:31:10 +08004551 unplugged:
4552 spin_unlock(&priv->lock);
Zhu Yib481de92007-09-25 17:54:57 -07004553 return IRQ_HANDLED;
4554
4555 none:
4556 /* re-enable interrupts here since we don't have anything to service. */
Mohamed Abbas0359fac2008-03-28 16:21:08 -07004557 /* only Re-enable if diabled by irq */
4558 if (test_bit(STATUS_INT_ENABLED, &priv->status))
4559 iwl4965_enable_interrupts(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004560 spin_unlock(&priv->lock);
4561 return IRQ_NONE;
4562}
4563
Zhu Yib481de92007-09-25 17:54:57 -07004564/* For active scan, listen ACTIVE_DWELL_TIME (msec) on each channel after
4565 * sending probe req. This should be set long enough to hear probe responses
4566 * from more than one AP. */
4567#define IWL_ACTIVE_DWELL_TIME_24 (20) /* all times in msec */
4568#define IWL_ACTIVE_DWELL_TIME_52 (10)
4569
4570/* For faster active scanning, scan will move to the next channel if fewer than
4571 * PLCP_QUIET_THRESH packets are heard on this channel within
4572 * ACTIVE_QUIET_TIME after sending probe request. This shortens the dwell
4573 * time if it's a quiet channel (nothing responded to our probe, and there's
4574 * no other traffic).
4575 * Disable "quiet" feature by setting PLCP_QUIET_THRESH to 0. */
4576#define IWL_PLCP_QUIET_THRESH __constant_cpu_to_le16(1) /* packets */
4577#define IWL_ACTIVE_QUIET_TIME __constant_cpu_to_le16(5) /* msec */
4578
4579/* For passive scan, listen PASSIVE_DWELL_TIME (msec) on each channel.
4580 * Must be set longer than active dwell time.
4581 * For the most reliable scan, set > AP beacon interval (typically 100msec). */
4582#define IWL_PASSIVE_DWELL_TIME_24 (20) /* all times in msec */
4583#define IWL_PASSIVE_DWELL_TIME_52 (10)
4584#define IWL_PASSIVE_DWELL_BASE (100)
4585#define IWL_CHANNEL_TUNE_TIME 5
4586
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004587static inline u16 iwl4965_get_active_dwell_time(struct iwl_priv *priv,
Johannes Berg8318d782008-01-24 19:38:38 +01004588 enum ieee80211_band band)
Zhu Yib481de92007-09-25 17:54:57 -07004589{
Johannes Berg8318d782008-01-24 19:38:38 +01004590 if (band == IEEE80211_BAND_5GHZ)
Zhu Yib481de92007-09-25 17:54:57 -07004591 return IWL_ACTIVE_DWELL_TIME_52;
4592 else
4593 return IWL_ACTIVE_DWELL_TIME_24;
4594}
4595
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004596static u16 iwl4965_get_passive_dwell_time(struct iwl_priv *priv,
Johannes Berg8318d782008-01-24 19:38:38 +01004597 enum ieee80211_band band)
Zhu Yib481de92007-09-25 17:54:57 -07004598{
Johannes Berg8318d782008-01-24 19:38:38 +01004599 u16 active = iwl4965_get_active_dwell_time(priv, band);
4600 u16 passive = (band != IEEE80211_BAND_5GHZ) ?
Zhu Yib481de92007-09-25 17:54:57 -07004601 IWL_PASSIVE_DWELL_BASE + IWL_PASSIVE_DWELL_TIME_24 :
4602 IWL_PASSIVE_DWELL_BASE + IWL_PASSIVE_DWELL_TIME_52;
4603
Tomas Winkler3109ece2008-03-28 16:33:35 -07004604 if (iwl_is_associated(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07004605 /* If we're associated, we clamp the maximum passive
4606 * dwell time to be 98% of the beacon interval (minus
4607 * 2 * channel tune time) */
4608 passive = priv->beacon_int;
4609 if ((passive > IWL_PASSIVE_DWELL_BASE) || !passive)
4610 passive = IWL_PASSIVE_DWELL_BASE;
4611 passive = (passive * 98) / 100 - IWL_CHANNEL_TUNE_TIME * 2;
4612 }
4613
4614 if (passive <= active)
4615 passive = active + 1;
4616
4617 return passive;
4618}
4619
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004620static int iwl4965_get_channels_for_scan(struct iwl_priv *priv,
Johannes Berg8318d782008-01-24 19:38:38 +01004621 enum ieee80211_band band,
Zhu Yib481de92007-09-25 17:54:57 -07004622 u8 is_active, u8 direct_mask,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004623 struct iwl4965_scan_channel *scan_ch)
Zhu Yib481de92007-09-25 17:54:57 -07004624{
4625 const struct ieee80211_channel *channels = NULL;
Johannes Berg8318d782008-01-24 19:38:38 +01004626 const struct ieee80211_supported_band *sband;
Assaf Kraussbf85ea42008-03-14 10:38:49 -07004627 const struct iwl_channel_info *ch_info;
Zhu Yib481de92007-09-25 17:54:57 -07004628 u16 passive_dwell = 0;
4629 u16 active_dwell = 0;
4630 int added, i;
4631
Johannes Berg8318d782008-01-24 19:38:38 +01004632 sband = iwl4965_get_hw_mode(priv, band);
4633 if (!sband)
Zhu Yib481de92007-09-25 17:54:57 -07004634 return 0;
4635
Johannes Berg8318d782008-01-24 19:38:38 +01004636 channels = sband->channels;
Zhu Yib481de92007-09-25 17:54:57 -07004637
Johannes Berg8318d782008-01-24 19:38:38 +01004638 active_dwell = iwl4965_get_active_dwell_time(priv, band);
4639 passive_dwell = iwl4965_get_passive_dwell_time(priv, band);
Zhu Yib481de92007-09-25 17:54:57 -07004640
Johannes Berg8318d782008-01-24 19:38:38 +01004641 for (i = 0, added = 0; i < sband->n_channels; i++) {
Johannes Berg182e2e62008-04-04 10:41:56 +02004642 if (channels[i].flags & IEEE80211_CHAN_DISABLED)
4643 continue;
4644
Johannes Berg8318d782008-01-24 19:38:38 +01004645 scan_ch->channel = ieee80211_frequency_to_channel(channels[i].center_freq);
Zhu Yib481de92007-09-25 17:54:57 -07004646
Ron Rindjunsky66b50042008-06-25 16:46:31 +08004647 ch_info = iwl_get_channel_info(priv, band, scan_ch->channel);
Zhu Yib481de92007-09-25 17:54:57 -07004648 if (!is_channel_valid(ch_info)) {
Ron Rindjunsky66b50042008-06-25 16:46:31 +08004649 IWL_DEBUG_SCAN("Channel %d is INVALID for this band.\n",
Zhu Yib481de92007-09-25 17:54:57 -07004650 scan_ch->channel);
4651 continue;
4652 }
4653
4654 if (!is_active || is_channel_passive(ch_info) ||
Johannes Berg8318d782008-01-24 19:38:38 +01004655 (channels[i].flags & IEEE80211_CHAN_PASSIVE_SCAN))
Zhu Yib481de92007-09-25 17:54:57 -07004656 scan_ch->type = 0; /* passive */
4657 else
4658 scan_ch->type = 1; /* active */
4659
4660 if (scan_ch->type & 1)
4661 scan_ch->type |= (direct_mask << 1);
4662
4663 if (is_channel_narrow(ch_info))
4664 scan_ch->type |= (1 << 7);
4665
4666 scan_ch->active_dwell = cpu_to_le16(active_dwell);
4667 scan_ch->passive_dwell = cpu_to_le16(passive_dwell);
4668
Ben Cahill9fbab512007-11-29 11:09:47 +08004669 /* Set txpower levels to defaults */
Zhu Yib481de92007-09-25 17:54:57 -07004670 scan_ch->tpc.dsp_atten = 110;
4671 /* scan_pwr_info->tpc.dsp_atten; */
4672
4673 /*scan_pwr_info->tpc.tx_gain; */
Johannes Berg8318d782008-01-24 19:38:38 +01004674 if (band == IEEE80211_BAND_5GHZ)
Zhu Yib481de92007-09-25 17:54:57 -07004675 scan_ch->tpc.tx_gain = ((1 << 5) | (3 << 3)) | 3;
4676 else {
4677 scan_ch->tpc.tx_gain = ((1 << 5) | (5 << 3));
4678 /* NOTE: if we were doing 6Mb OFDM for scans we'd use
Ben Cahill9fbab512007-11-29 11:09:47 +08004679 * power level:
Reinette Chatre8a1b0242008-01-14 17:46:25 -08004680 * scan_ch->tpc.tx_gain = ((1 << 5) | (2 << 3)) | 3;
Zhu Yib481de92007-09-25 17:54:57 -07004681 */
4682 }
4683
4684 IWL_DEBUG_SCAN("Scanning %d [%s %d]\n",
4685 scan_ch->channel,
4686 (scan_ch->type & 1) ? "ACTIVE" : "PASSIVE",
4687 (scan_ch->type & 1) ?
4688 active_dwell : passive_dwell);
4689
4690 scan_ch++;
4691 added++;
4692 }
4693
4694 IWL_DEBUG_SCAN("total channels to scan %d \n", added);
4695 return added;
4696}
4697
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004698static void iwl4965_init_hw_rates(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07004699 struct ieee80211_rate *rates)
4700{
4701 int i;
4702
4703 for (i = 0; i < IWL_RATE_COUNT; i++) {
Johannes Berg8318d782008-01-24 19:38:38 +01004704 rates[i].bitrate = iwl4965_rates[i].ieee * 5;
4705 rates[i].hw_value = i; /* Rate scaling will work on indexes */
4706 rates[i].hw_value_short = i;
4707 rates[i].flags = 0;
4708 if ((i > IWL_LAST_OFDM_RATE) || (i < IWL_FIRST_OFDM_RATE)) {
Zhu Yib481de92007-09-25 17:54:57 -07004709 /*
Johannes Berg8318d782008-01-24 19:38:38 +01004710 * If CCK != 1M then set short preamble rate flag.
Zhu Yib481de92007-09-25 17:54:57 -07004711 */
Tomas Winkler35cdeaf2008-03-11 16:17:20 -07004712 rates[i].flags |=
4713 (iwl4965_rates[i].plcp == IWL_RATE_1M_PLCP) ?
4714 0 : IEEE80211_RATE_SHORT_PREAMBLE;
Zhu Yib481de92007-09-25 17:54:57 -07004715 }
Zhu Yib481de92007-09-25 17:54:57 -07004716 }
Zhu Yib481de92007-09-25 17:54:57 -07004717}
4718
4719/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004720 * iwl4965_init_geos - Initialize mac80211's geo/channel info based from eeprom
Zhu Yib481de92007-09-25 17:54:57 -07004721 */
Assaf Kraussbf85ea42008-03-14 10:38:49 -07004722int iwl4965_init_geos(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004723{
Assaf Kraussbf85ea42008-03-14 10:38:49 -07004724 struct iwl_channel_info *ch;
Tomas Winkler8211ef72008-03-02 01:36:04 +02004725 struct ieee80211_supported_band *sband;
Zhu Yib481de92007-09-25 17:54:57 -07004726 struct ieee80211_channel *channels;
4727 struct ieee80211_channel *geo_ch;
4728 struct ieee80211_rate *rates;
4729 int i = 0;
Zhu Yib481de92007-09-25 17:54:57 -07004730
Johannes Berg8318d782008-01-24 19:38:38 +01004731 if (priv->bands[IEEE80211_BAND_2GHZ].n_bitrates ||
4732 priv->bands[IEEE80211_BAND_5GHZ].n_bitrates) {
Zhu Yib481de92007-09-25 17:54:57 -07004733 IWL_DEBUG_INFO("Geography modes already initialized.\n");
4734 set_bit(STATUS_GEO_CONFIGURED, &priv->status);
4735 return 0;
4736 }
4737
Zhu Yib481de92007-09-25 17:54:57 -07004738 channels = kzalloc(sizeof(struct ieee80211_channel) *
4739 priv->channel_count, GFP_KERNEL);
Johannes Berg8318d782008-01-24 19:38:38 +01004740 if (!channels)
Zhu Yib481de92007-09-25 17:54:57 -07004741 return -ENOMEM;
Zhu Yib481de92007-09-25 17:54:57 -07004742
Tomas Winkler8211ef72008-03-02 01:36:04 +02004743 rates = kzalloc((sizeof(struct ieee80211_rate) * (IWL_RATE_COUNT + 1)),
Zhu Yib481de92007-09-25 17:54:57 -07004744 GFP_KERNEL);
4745 if (!rates) {
Zhu Yib481de92007-09-25 17:54:57 -07004746 kfree(channels);
4747 return -ENOMEM;
4748 }
4749
Zhu Yib481de92007-09-25 17:54:57 -07004750 /* 5.2GHz channels start after the 2.4GHz channels */
Tomas Winkler8211ef72008-03-02 01:36:04 +02004751 sband = &priv->bands[IEEE80211_BAND_5GHZ];
Assaf Kraussbf85ea42008-03-14 10:38:49 -07004752 sband->channels = &channels[ARRAY_SIZE(iwl_eeprom_band_1)];
Tomas Winkler8211ef72008-03-02 01:36:04 +02004753 /* just OFDM */
4754 sband->bitrates = &rates[IWL_FIRST_OFDM_RATE];
4755 sband->n_bitrates = IWL_RATE_COUNT - IWL_FIRST_OFDM_RATE;
Johannes Berg8318d782008-01-24 19:38:38 +01004756
Assaf Krauss1ea87392008-03-18 14:57:50 -07004757 iwl4965_init_ht_hw_capab(priv, &sband->ht_info, IEEE80211_BAND_5GHZ);
Tomas Winkler78330fd2008-02-06 02:37:18 +02004758
Tomas Winkler8211ef72008-03-02 01:36:04 +02004759 sband = &priv->bands[IEEE80211_BAND_2GHZ];
4760 sband->channels = channels;
4761 /* OFDM & CCK */
4762 sband->bitrates = rates;
4763 sband->n_bitrates = IWL_RATE_COUNT;
Zhu Yib481de92007-09-25 17:54:57 -07004764
Assaf Krauss1ea87392008-03-18 14:57:50 -07004765 iwl4965_init_ht_hw_capab(priv, &sband->ht_info, IEEE80211_BAND_2GHZ);
Tomas Winkler78330fd2008-02-06 02:37:18 +02004766
Zhu Yib481de92007-09-25 17:54:57 -07004767 priv->ieee_channels = channels;
4768 priv->ieee_rates = rates;
4769
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004770 iwl4965_init_hw_rates(priv, rates);
Zhu Yib481de92007-09-25 17:54:57 -07004771
Tomas Winkler8211ef72008-03-02 01:36:04 +02004772 for (i = 0; i < priv->channel_count; i++) {
Zhu Yib481de92007-09-25 17:54:57 -07004773 ch = &priv->channel_info[i];
4774
Tomas Winkler8211ef72008-03-02 01:36:04 +02004775 /* FIXME: might be removed if scan is OK */
4776 if (!is_channel_valid(ch))
Zhu Yib481de92007-09-25 17:54:57 -07004777 continue;
Zhu Yib481de92007-09-25 17:54:57 -07004778
Tomas Winkler8211ef72008-03-02 01:36:04 +02004779 if (is_channel_a_band(ch))
4780 sband = &priv->bands[IEEE80211_BAND_5GHZ];
4781 else
4782 sband = &priv->bands[IEEE80211_BAND_2GHZ];
Zhu Yib481de92007-09-25 17:54:57 -07004783
Tomas Winkler8211ef72008-03-02 01:36:04 +02004784 geo_ch = &sband->channels[sband->n_channels++];
4785
4786 geo_ch->center_freq = ieee80211_channel_to_frequency(ch->channel);
Johannes Berg8318d782008-01-24 19:38:38 +01004787 geo_ch->max_power = ch->max_power_avg;
4788 geo_ch->max_antenna_gain = 0xff;
Mohamed Abbas7b723042008-01-31 21:46:40 -08004789 geo_ch->hw_value = ch->channel;
Zhu Yib481de92007-09-25 17:54:57 -07004790
4791 if (is_channel_valid(ch)) {
Johannes Berg8318d782008-01-24 19:38:38 +01004792 if (!(ch->flags & EEPROM_CHANNEL_IBSS))
4793 geo_ch->flags |= IEEE80211_CHAN_NO_IBSS;
Zhu Yib481de92007-09-25 17:54:57 -07004794
Johannes Berg8318d782008-01-24 19:38:38 +01004795 if (!(ch->flags & EEPROM_CHANNEL_ACTIVE))
4796 geo_ch->flags |= IEEE80211_CHAN_PASSIVE_SCAN;
Zhu Yib481de92007-09-25 17:54:57 -07004797
4798 if (ch->flags & EEPROM_CHANNEL_RADAR)
Johannes Berg8318d782008-01-24 19:38:38 +01004799 geo_ch->flags |= IEEE80211_CHAN_RADAR;
Zhu Yib481de92007-09-25 17:54:57 -07004800
4801 if (ch->max_power_avg > priv->max_channel_txpower_limit)
4802 priv->max_channel_txpower_limit =
4803 ch->max_power_avg;
Tomas Winkler8211ef72008-03-02 01:36:04 +02004804 } else {
Johannes Berg8318d782008-01-24 19:38:38 +01004805 geo_ch->flags |= IEEE80211_CHAN_DISABLED;
Tomas Winkler8211ef72008-03-02 01:36:04 +02004806 }
4807
4808 /* Save flags for reg domain usage */
4809 geo_ch->orig_flags = geo_ch->flags;
4810
4811 IWL_DEBUG_INFO("Channel %d Freq=%d[%sGHz] %s flag=0%X\n",
4812 ch->channel, geo_ch->center_freq,
4813 is_channel_a_band(ch) ? "5.2" : "2.4",
4814 geo_ch->flags & IEEE80211_CHAN_DISABLED ?
4815 "restricted" : "valid",
4816 geo_ch->flags);
Zhu Yib481de92007-09-25 17:54:57 -07004817 }
4818
Tomas Winkler82b9a122008-03-04 18:09:30 -08004819 if ((priv->bands[IEEE80211_BAND_5GHZ].n_channels == 0) &&
4820 priv->cfg->sku & IWL_SKU_A) {
Zhu Yib481de92007-09-25 17:54:57 -07004821 printk(KERN_INFO DRV_NAME
4822 ": Incorrectly detected BG card as ABG. Please send "
4823 "your PCI ID 0x%04X:0x%04X to maintainer.\n",
4824 priv->pci_dev->device, priv->pci_dev->subsystem_device);
Tomas Winkler82b9a122008-03-04 18:09:30 -08004825 priv->cfg->sku &= ~IWL_SKU_A;
Zhu Yib481de92007-09-25 17:54:57 -07004826 }
4827
4828 printk(KERN_INFO DRV_NAME
4829 ": Tunable channels: %d 802.11bg, %d 802.11a channels\n",
Johannes Berg8318d782008-01-24 19:38:38 +01004830 priv->bands[IEEE80211_BAND_2GHZ].n_channels,
4831 priv->bands[IEEE80211_BAND_5GHZ].n_channels);
Zhu Yib481de92007-09-25 17:54:57 -07004832
John W. Linvillee0e0a672008-03-25 15:58:40 -04004833 if (priv->bands[IEEE80211_BAND_2GHZ].n_channels)
4834 priv->hw->wiphy->bands[IEEE80211_BAND_2GHZ] =
4835 &priv->bands[IEEE80211_BAND_2GHZ];
4836 if (priv->bands[IEEE80211_BAND_5GHZ].n_channels)
4837 priv->hw->wiphy->bands[IEEE80211_BAND_5GHZ] =
4838 &priv->bands[IEEE80211_BAND_5GHZ];
Zhu Yib481de92007-09-25 17:54:57 -07004839
Zhu Yib481de92007-09-25 17:54:57 -07004840 set_bit(STATUS_GEO_CONFIGURED, &priv->status);
4841
4842 return 0;
4843}
4844
Reinette Chatre849e0dc2008-01-23 10:15:18 -08004845/*
4846 * iwl4965_free_geos - undo allocations in iwl4965_init_geos
4847 */
Assaf Kraussbf85ea42008-03-14 10:38:49 -07004848void iwl4965_free_geos(struct iwl_priv *priv)
Reinette Chatre849e0dc2008-01-23 10:15:18 -08004849{
Reinette Chatre849e0dc2008-01-23 10:15:18 -08004850 kfree(priv->ieee_channels);
4851 kfree(priv->ieee_rates);
4852 clear_bit(STATUS_GEO_CONFIGURED, &priv->status);
4853}
4854
Zhu Yib481de92007-09-25 17:54:57 -07004855/******************************************************************************
4856 *
4857 * uCode download functions
4858 *
4859 ******************************************************************************/
4860
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004861static void iwl4965_dealloc_ucode_pci(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004862{
Tomas Winkler98c92212008-01-14 17:46:20 -08004863 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_code);
4864 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_data);
4865 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_data_backup);
4866 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_init);
4867 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_init_data);
4868 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_boot);
Zhu Yib481de92007-09-25 17:54:57 -07004869}
4870
4871/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004872 * iwl4965_verify_inst_full - verify runtime uCode image in card vs. host,
Zhu Yib481de92007-09-25 17:54:57 -07004873 * looking at all data.
4874 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004875static int iwl4965_verify_inst_full(struct iwl_priv *priv, __le32 *image,
Ben Cahill9fbab512007-11-29 11:09:47 +08004876 u32 len)
Zhu Yib481de92007-09-25 17:54:57 -07004877{
4878 u32 val;
4879 u32 save_len = len;
4880 int rc = 0;
4881 u32 errcnt;
4882
4883 IWL_DEBUG_INFO("ucode inst image size is %u\n", len);
4884
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004885 rc = iwl_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004886 if (rc)
4887 return rc;
4888
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004889 iwl_write_direct32(priv, HBUS_TARG_MEM_RADDR, RTC_INST_LOWER_BOUND);
Zhu Yib481de92007-09-25 17:54:57 -07004890
4891 errcnt = 0;
4892 for (; len > 0; len -= sizeof(u32), image++) {
4893 /* read data comes through single port, auto-incr addr */
4894 /* NOTE: Use the debugless read so we don't flood kernel log
4895 * if IWL_DL_IO is set */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004896 val = _iwl_read_direct32(priv, HBUS_TARG_MEM_RDAT);
Zhu Yib481de92007-09-25 17:54:57 -07004897 if (val != le32_to_cpu(*image)) {
4898 IWL_ERROR("uCode INST section is invalid at "
4899 "offset 0x%x, is 0x%x, s/b 0x%x\n",
4900 save_len - len, val, le32_to_cpu(*image));
4901 rc = -EIO;
4902 errcnt++;
4903 if (errcnt >= 20)
4904 break;
4905 }
4906 }
4907
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004908 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004909
4910 if (!errcnt)
4911 IWL_DEBUG_INFO
4912 ("ucode image in INSTRUCTION memory is good\n");
4913
4914 return rc;
4915}
4916
4917
4918/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004919 * iwl4965_verify_inst_sparse - verify runtime uCode image in card vs. host,
Zhu Yib481de92007-09-25 17:54:57 -07004920 * using sample data 100 bytes apart. If these sample points are good,
4921 * it's a pretty good bet that everything between them is good, too.
4922 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004923static int iwl4965_verify_inst_sparse(struct iwl_priv *priv, __le32 *image, u32 len)
Zhu Yib481de92007-09-25 17:54:57 -07004924{
4925 u32 val;
4926 int rc = 0;
4927 u32 errcnt = 0;
4928 u32 i;
4929
4930 IWL_DEBUG_INFO("ucode inst image size is %u\n", len);
4931
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004932 rc = iwl_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004933 if (rc)
4934 return rc;
4935
4936 for (i = 0; i < len; i += 100, image += 100/sizeof(u32)) {
4937 /* read data comes through single port, auto-incr addr */
4938 /* NOTE: Use the debugless read so we don't flood kernel log
4939 * if IWL_DL_IO is set */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004940 iwl_write_direct32(priv, HBUS_TARG_MEM_RADDR,
Zhu Yib481de92007-09-25 17:54:57 -07004941 i + RTC_INST_LOWER_BOUND);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004942 val = _iwl_read_direct32(priv, HBUS_TARG_MEM_RDAT);
Zhu Yib481de92007-09-25 17:54:57 -07004943 if (val != le32_to_cpu(*image)) {
4944#if 0 /* Enable this if you want to see details */
4945 IWL_ERROR("uCode INST section is invalid at "
4946 "offset 0x%x, is 0x%x, s/b 0x%x\n",
4947 i, val, *image);
4948#endif
4949 rc = -EIO;
4950 errcnt++;
4951 if (errcnt >= 3)
4952 break;
4953 }
4954 }
4955
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004956 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004957
4958 return rc;
4959}
4960
4961
4962/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004963 * iwl4965_verify_ucode - determine which instruction image is in SRAM,
Zhu Yib481de92007-09-25 17:54:57 -07004964 * and verify its contents
4965 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004966static int iwl4965_verify_ucode(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004967{
4968 __le32 *image;
4969 u32 len;
4970 int rc = 0;
4971
4972 /* Try bootstrap */
4973 image = (__le32 *)priv->ucode_boot.v_addr;
4974 len = priv->ucode_boot.len;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004975 rc = iwl4965_verify_inst_sparse(priv, image, len);
Zhu Yib481de92007-09-25 17:54:57 -07004976 if (rc == 0) {
4977 IWL_DEBUG_INFO("Bootstrap uCode is good in inst SRAM\n");
4978 return 0;
4979 }
4980
4981 /* Try initialize */
4982 image = (__le32 *)priv->ucode_init.v_addr;
4983 len = priv->ucode_init.len;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004984 rc = iwl4965_verify_inst_sparse(priv, image, len);
Zhu Yib481de92007-09-25 17:54:57 -07004985 if (rc == 0) {
4986 IWL_DEBUG_INFO("Initialize uCode is good in inst SRAM\n");
4987 return 0;
4988 }
4989
4990 /* Try runtime/protocol */
4991 image = (__le32 *)priv->ucode_code.v_addr;
4992 len = priv->ucode_code.len;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004993 rc = iwl4965_verify_inst_sparse(priv, image, len);
Zhu Yib481de92007-09-25 17:54:57 -07004994 if (rc == 0) {
4995 IWL_DEBUG_INFO("Runtime uCode is good in inst SRAM\n");
4996 return 0;
4997 }
4998
4999 IWL_ERROR("NO VALID UCODE IMAGE IN INSTRUCTION SRAM!!\n");
5000
Ben Cahill9fbab512007-11-29 11:09:47 +08005001 /* Since nothing seems to match, show first several data entries in
5002 * instruction SRAM, so maybe visual inspection will give a clue.
5003 * Selection of bootstrap image (vs. other images) is arbitrary. */
Zhu Yib481de92007-09-25 17:54:57 -07005004 image = (__le32 *)priv->ucode_boot.v_addr;
5005 len = priv->ucode_boot.len;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005006 rc = iwl4965_verify_inst_full(priv, image, len);
Zhu Yib481de92007-09-25 17:54:57 -07005007
5008 return rc;
5009}
5010
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005011static void iwl4965_nic_start(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005012{
5013 /* Remove all resets to allow NIC to operate */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07005014 iwl_write32(priv, CSR_RESET, 0);
Zhu Yib481de92007-09-25 17:54:57 -07005015}
5016
Tomas Winkler90e759d2007-11-29 11:09:41 +08005017
Zhu Yib481de92007-09-25 17:54:57 -07005018/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005019 * iwl4965_read_ucode - Read uCode images from disk file.
Zhu Yib481de92007-09-25 17:54:57 -07005020 *
5021 * Copy into buffers for card to fetch via bus-mastering
5022 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005023static int iwl4965_read_ucode(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005024{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005025 struct iwl4965_ucode *ucode;
Tomas Winkler90e759d2007-11-29 11:09:41 +08005026 int ret;
Zhu Yib481de92007-09-25 17:54:57 -07005027 const struct firmware *ucode_raw;
Tomas Winkler4bf775c2008-03-04 18:09:31 -08005028 const char *name = priv->cfg->fw_name;
Zhu Yib481de92007-09-25 17:54:57 -07005029 u8 *src;
5030 size_t len;
5031 u32 ver, inst_size, data_size, init_size, init_data_size, boot_size;
5032
5033 /* Ask kernel firmware_class module to get the boot firmware off disk.
5034 * request_firmware() is synchronous, file is in memory on return. */
Tomas Winkler90e759d2007-11-29 11:09:41 +08005035 ret = request_firmware(&ucode_raw, name, &priv->pci_dev->dev);
5036 if (ret < 0) {
5037 IWL_ERROR("%s firmware file req failed: Reason %d\n",
5038 name, ret);
Zhu Yib481de92007-09-25 17:54:57 -07005039 goto error;
5040 }
5041
5042 IWL_DEBUG_INFO("Got firmware '%s' file (%zd bytes) from disk\n",
5043 name, ucode_raw->size);
5044
5045 /* Make sure that we got at least our header! */
5046 if (ucode_raw->size < sizeof(*ucode)) {
5047 IWL_ERROR("File size way too small!\n");
Tomas Winkler90e759d2007-11-29 11:09:41 +08005048 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07005049 goto err_release;
5050 }
5051
5052 /* Data from ucode file: header followed by uCode images */
5053 ucode = (void *)ucode_raw->data;
5054
5055 ver = le32_to_cpu(ucode->ver);
5056 inst_size = le32_to_cpu(ucode->inst_size);
5057 data_size = le32_to_cpu(ucode->data_size);
5058 init_size = le32_to_cpu(ucode->init_size);
5059 init_data_size = le32_to_cpu(ucode->init_data_size);
5060 boot_size = le32_to_cpu(ucode->boot_size);
5061
5062 IWL_DEBUG_INFO("f/w package hdr ucode version = 0x%x\n", ver);
5063 IWL_DEBUG_INFO("f/w package hdr runtime inst size = %u\n",
5064 inst_size);
5065 IWL_DEBUG_INFO("f/w package hdr runtime data size = %u\n",
5066 data_size);
5067 IWL_DEBUG_INFO("f/w package hdr init inst size = %u\n",
5068 init_size);
5069 IWL_DEBUG_INFO("f/w package hdr init data size = %u\n",
5070 init_data_size);
5071 IWL_DEBUG_INFO("f/w package hdr boot inst size = %u\n",
5072 boot_size);
5073
5074 /* Verify size of file vs. image size info in file's header */
5075 if (ucode_raw->size < sizeof(*ucode) +
5076 inst_size + data_size + init_size +
5077 init_data_size + boot_size) {
5078
5079 IWL_DEBUG_INFO("uCode file size %d too small\n",
5080 (int)ucode_raw->size);
Tomas Winkler90e759d2007-11-29 11:09:41 +08005081 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07005082 goto err_release;
5083 }
5084
5085 /* Verify that uCode images will fit in card's SRAM */
5086 if (inst_size > IWL_MAX_INST_SIZE) {
Tomas Winkler90e759d2007-11-29 11:09:41 +08005087 IWL_DEBUG_INFO("uCode instr len %d too large to fit in\n",
5088 inst_size);
5089 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07005090 goto err_release;
5091 }
5092
5093 if (data_size > IWL_MAX_DATA_SIZE) {
Tomas Winkler90e759d2007-11-29 11:09:41 +08005094 IWL_DEBUG_INFO("uCode data len %d too large to fit in\n",
5095 data_size);
5096 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07005097 goto err_release;
5098 }
5099 if (init_size > IWL_MAX_INST_SIZE) {
5100 IWL_DEBUG_INFO
Tomas Winkler90e759d2007-11-29 11:09:41 +08005101 ("uCode init instr len %d too large to fit in\n",
5102 init_size);
5103 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07005104 goto err_release;
5105 }
5106 if (init_data_size > IWL_MAX_DATA_SIZE) {
5107 IWL_DEBUG_INFO
Tomas Winkler90e759d2007-11-29 11:09:41 +08005108 ("uCode init data len %d too large to fit in\n",
5109 init_data_size);
5110 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07005111 goto err_release;
5112 }
5113 if (boot_size > IWL_MAX_BSM_SIZE) {
5114 IWL_DEBUG_INFO
Tomas Winkler90e759d2007-11-29 11:09:41 +08005115 ("uCode boot instr len %d too large to fit in\n",
5116 boot_size);
5117 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07005118 goto err_release;
5119 }
5120
5121 /* Allocate ucode buffers for card's bus-master loading ... */
5122
5123 /* Runtime instructions and 2 copies of data:
5124 * 1) unmodified from disk
5125 * 2) backup cache for save/restore during power-downs */
5126 priv->ucode_code.len = inst_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08005127 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_code);
Zhu Yib481de92007-09-25 17:54:57 -07005128
5129 priv->ucode_data.len = data_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08005130 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_data);
Zhu Yib481de92007-09-25 17:54:57 -07005131
5132 priv->ucode_data_backup.len = data_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08005133 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_data_backup);
Zhu Yib481de92007-09-25 17:54:57 -07005134
5135 /* Initialization instructions and data */
Tomas Winkler90e759d2007-11-29 11:09:41 +08005136 if (init_size && init_data_size) {
5137 priv->ucode_init.len = init_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08005138 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_init);
Zhu Yib481de92007-09-25 17:54:57 -07005139
Tomas Winkler90e759d2007-11-29 11:09:41 +08005140 priv->ucode_init_data.len = init_data_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08005141 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_init_data);
Tomas Winkler90e759d2007-11-29 11:09:41 +08005142
5143 if (!priv->ucode_init.v_addr || !priv->ucode_init_data.v_addr)
5144 goto err_pci_alloc;
5145 }
Zhu Yib481de92007-09-25 17:54:57 -07005146
5147 /* Bootstrap (instructions only, no data) */
Tomas Winkler90e759d2007-11-29 11:09:41 +08005148 if (boot_size) {
5149 priv->ucode_boot.len = boot_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08005150 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_boot);
Zhu Yib481de92007-09-25 17:54:57 -07005151
Tomas Winkler90e759d2007-11-29 11:09:41 +08005152 if (!priv->ucode_boot.v_addr)
5153 goto err_pci_alloc;
5154 }
Zhu Yib481de92007-09-25 17:54:57 -07005155
5156 /* Copy images into buffers for card's bus-master reads ... */
5157
5158 /* Runtime instructions (first block of data in file) */
5159 src = &ucode->data[0];
5160 len = priv->ucode_code.len;
Tomas Winkler90e759d2007-11-29 11:09:41 +08005161 IWL_DEBUG_INFO("Copying (but not loading) uCode instr len %Zd\n", len);
Zhu Yib481de92007-09-25 17:54:57 -07005162 memcpy(priv->ucode_code.v_addr, src, len);
5163 IWL_DEBUG_INFO("uCode instr buf vaddr = 0x%p, paddr = 0x%08x\n",
5164 priv->ucode_code.v_addr, (u32)priv->ucode_code.p_addr);
5165
5166 /* Runtime data (2nd block)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005167 * NOTE: Copy into backup buffer will be done in iwl4965_up() */
Zhu Yib481de92007-09-25 17:54:57 -07005168 src = &ucode->data[inst_size];
5169 len = priv->ucode_data.len;
Tomas Winkler90e759d2007-11-29 11:09:41 +08005170 IWL_DEBUG_INFO("Copying (but not loading) uCode data len %Zd\n", len);
Zhu Yib481de92007-09-25 17:54:57 -07005171 memcpy(priv->ucode_data.v_addr, src, len);
5172 memcpy(priv->ucode_data_backup.v_addr, src, len);
5173
5174 /* Initialization instructions (3rd block) */
5175 if (init_size) {
5176 src = &ucode->data[inst_size + data_size];
5177 len = priv->ucode_init.len;
Tomas Winkler90e759d2007-11-29 11:09:41 +08005178 IWL_DEBUG_INFO("Copying (but not loading) init instr len %Zd\n",
5179 len);
Zhu Yib481de92007-09-25 17:54:57 -07005180 memcpy(priv->ucode_init.v_addr, src, len);
5181 }
5182
5183 /* Initialization data (4th block) */
5184 if (init_data_size) {
5185 src = &ucode->data[inst_size + data_size + init_size];
5186 len = priv->ucode_init_data.len;
Tomas Winkler90e759d2007-11-29 11:09:41 +08005187 IWL_DEBUG_INFO("Copying (but not loading) init data len %Zd\n",
5188 len);
Zhu Yib481de92007-09-25 17:54:57 -07005189 memcpy(priv->ucode_init_data.v_addr, src, len);
5190 }
5191
5192 /* Bootstrap instructions (5th block) */
5193 src = &ucode->data[inst_size + data_size + init_size + init_data_size];
5194 len = priv->ucode_boot.len;
Tomas Winkler90e759d2007-11-29 11:09:41 +08005195 IWL_DEBUG_INFO("Copying (but not loading) boot instr len %Zd\n", len);
Zhu Yib481de92007-09-25 17:54:57 -07005196 memcpy(priv->ucode_boot.v_addr, src, len);
5197
5198 /* We have our copies now, allow OS release its copies */
5199 release_firmware(ucode_raw);
5200 return 0;
5201
5202 err_pci_alloc:
5203 IWL_ERROR("failed to allocate pci memory\n");
Tomas Winkler90e759d2007-11-29 11:09:41 +08005204 ret = -ENOMEM;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005205 iwl4965_dealloc_ucode_pci(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005206
5207 err_release:
5208 release_firmware(ucode_raw);
5209
5210 error:
Tomas Winkler90e759d2007-11-29 11:09:41 +08005211 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07005212}
5213
5214
5215/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005216 * iwl4965_set_ucode_ptrs - Set uCode address location
Zhu Yib481de92007-09-25 17:54:57 -07005217 *
5218 * Tell initialization uCode where to find runtime uCode.
5219 *
5220 * BSM registers initially contain pointers to initialization uCode.
5221 * We need to replace them to load runtime uCode inst and data,
5222 * and to save runtime data when powering down.
5223 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005224static int iwl4965_set_ucode_ptrs(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005225{
5226 dma_addr_t pinst;
5227 dma_addr_t pdata;
5228 int rc = 0;
5229 unsigned long flags;
5230
5231 /* bits 35:4 for 4965 */
5232 pinst = priv->ucode_code.p_addr >> 4;
5233 pdata = priv->ucode_data_backup.p_addr >> 4;
5234
5235 spin_lock_irqsave(&priv->lock, flags);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07005236 rc = iwl_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005237 if (rc) {
5238 spin_unlock_irqrestore(&priv->lock, flags);
5239 return rc;
5240 }
5241
5242 /* Tell bootstrap uCode where to find image to load */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07005243 iwl_write_prph(priv, BSM_DRAM_INST_PTR_REG, pinst);
5244 iwl_write_prph(priv, BSM_DRAM_DATA_PTR_REG, pdata);
5245 iwl_write_prph(priv, BSM_DRAM_DATA_BYTECOUNT_REG,
Zhu Yib481de92007-09-25 17:54:57 -07005246 priv->ucode_data.len);
5247
5248 /* Inst bytecount must be last to set up, bit 31 signals uCode
5249 * that all new ptr/size info is in place */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07005250 iwl_write_prph(priv, BSM_DRAM_INST_BYTECOUNT_REG,
Zhu Yib481de92007-09-25 17:54:57 -07005251 priv->ucode_code.len | BSM_DRAM_INST_LOAD);
5252
Tomas Winkler3395f6e2008-03-25 16:33:37 -07005253 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005254
5255 spin_unlock_irqrestore(&priv->lock, flags);
5256
5257 IWL_DEBUG_INFO("Runtime uCode pointers are set.\n");
5258
5259 return rc;
5260}
5261
5262/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005263 * iwl4965_init_alive_start - Called after REPLY_ALIVE notification received
Zhu Yib481de92007-09-25 17:54:57 -07005264 *
5265 * Called after REPLY_ALIVE notification received from "initialize" uCode.
5266 *
5267 * The 4965 "initialize" ALIVE reply contains calibration data for:
5268 * Voltage, temperature, and MIMO tx gain correction, now stored in priv
5269 * (3945 does not contain this data).
5270 *
5271 * Tell "initialize" uCode to go ahead and load the runtime uCode.
5272*/
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005273static void iwl4965_init_alive_start(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005274{
5275 /* Check alive response for "valid" sign from uCode */
5276 if (priv->card_alive_init.is_valid != UCODE_VALID_OK) {
5277 /* We had an error bringing up the hardware, so take it
5278 * all the way back down so we can try again */
5279 IWL_DEBUG_INFO("Initialize Alive failed.\n");
5280 goto restart;
5281 }
5282
5283 /* Bootstrap uCode has loaded initialize uCode ... verify inst image.
5284 * This is a paranoid check, because we would not have gotten the
5285 * "initialize" alive if code weren't properly loaded. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005286 if (iwl4965_verify_ucode(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07005287 /* Runtime instruction load was bad;
5288 * take it all the way back down so we can try again */
5289 IWL_DEBUG_INFO("Bad \"initialize\" uCode load.\n");
5290 goto restart;
5291 }
5292
5293 /* Calculate temperature */
5294 priv->temperature = iwl4965_get_temperature(priv);
5295
5296 /* Send pointers to protocol/runtime uCode image ... init code will
5297 * load and launch runtime uCode, which will send us another "Alive"
5298 * notification. */
5299 IWL_DEBUG_INFO("Initialization Alive received.\n");
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005300 if (iwl4965_set_ucode_ptrs(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07005301 /* Runtime instruction load won't happen;
5302 * take it all the way back down so we can try again */
5303 IWL_DEBUG_INFO("Couldn't set up uCode pointers.\n");
5304 goto restart;
5305 }
5306 return;
5307
5308 restart:
5309 queue_work(priv->workqueue, &priv->restart);
5310}
5311
5312
5313/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005314 * iwl4965_alive_start - called after REPLY_ALIVE notification received
Zhu Yib481de92007-09-25 17:54:57 -07005315 * from protocol/runtime uCode (initialization uCode's
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005316 * Alive gets handled by iwl4965_init_alive_start()).
Zhu Yib481de92007-09-25 17:54:57 -07005317 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005318static void iwl4965_alive_start(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005319{
Tomas Winkler57aab752008-04-14 21:16:03 -07005320 int ret = 0;
Zhu Yib481de92007-09-25 17:54:57 -07005321
5322 IWL_DEBUG_INFO("Runtime Alive received.\n");
5323
5324 if (priv->card_alive.is_valid != UCODE_VALID_OK) {
5325 /* We had an error bringing up the hardware, so take it
5326 * all the way back down so we can try again */
5327 IWL_DEBUG_INFO("Alive failed.\n");
5328 goto restart;
5329 }
5330
5331 /* Initialize uCode has loaded Runtime uCode ... verify inst image.
5332 * This is a paranoid check, because we would not have gotten the
5333 * "runtime" alive if code weren't properly loaded. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005334 if (iwl4965_verify_ucode(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07005335 /* Runtime instruction load was bad;
5336 * take it all the way back down so we can try again */
5337 IWL_DEBUG_INFO("Bad runtime uCode load.\n");
5338 goto restart;
5339 }
5340
Assaf Kraussbf85ea42008-03-14 10:38:49 -07005341 iwlcore_clear_stations_table(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005342
Tomas Winkler57aab752008-04-14 21:16:03 -07005343 ret = priv->cfg->ops->lib->alive_notify(priv);
5344 if (ret) {
Zhu Yib481de92007-09-25 17:54:57 -07005345 IWL_WARNING("Could not complete ALIVE transition [ntf]: %d\n",
Tomas Winkler57aab752008-04-14 21:16:03 -07005346 ret);
Zhu Yib481de92007-09-25 17:54:57 -07005347 goto restart;
5348 }
5349
Ben Cahill9fbab512007-11-29 11:09:47 +08005350 /* After the ALIVE response, we can send host commands to 4965 uCode */
Zhu Yib481de92007-09-25 17:54:57 -07005351 set_bit(STATUS_ALIVE, &priv->status);
5352
5353 /* Clear out the uCode error bit if it is set */
5354 clear_bit(STATUS_FW_ERROR, &priv->status);
5355
Tomas Winklerfee12472008-04-03 16:05:21 -07005356 if (iwl_is_rfkill(priv))
Zhu Yib481de92007-09-25 17:54:57 -07005357 return;
5358
Zhu Yi5a669262008-01-14 17:46:18 -08005359 ieee80211_start_queues(priv->hw);
Zhu Yib481de92007-09-25 17:54:57 -07005360
5361 priv->active_rate = priv->rates_mask;
5362 priv->active_rate_basic = priv->rates_mask & IWL_BASIC_RATES_MASK;
5363
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005364 iwl4965_send_power_mode(priv, IWL_POWER_LEVEL(priv->power_mode));
Zhu Yib481de92007-09-25 17:54:57 -07005365
Tomas Winkler3109ece2008-03-28 16:33:35 -07005366 if (iwl_is_associated(priv)) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005367 struct iwl4965_rxon_cmd *active_rxon =
5368 (struct iwl4965_rxon_cmd *)(&priv->active_rxon);
Zhu Yib481de92007-09-25 17:54:57 -07005369
5370 memcpy(&priv->staging_rxon, &priv->active_rxon,
5371 sizeof(priv->staging_rxon));
5372 active_rxon->filter_flags &= ~RXON_FILTER_ASSOC_MSK;
5373 } else {
5374 /* Initialize our rx_config data */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005375 iwl4965_connection_init_rx_config(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005376 memcpy(priv->staging_rxon.node_addr, priv->mac_addr, ETH_ALEN);
5377 }
5378
Ben Cahill9fbab512007-11-29 11:09:47 +08005379 /* Configure Bluetooth device coexistence support */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005380 iwl4965_send_bt_config(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005381
5382 /* Configure the adapter for unassociated operation */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005383 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005384
5385 /* At this point, the NIC is initialized and operational */
5386 priv->notif_missed_beacons = 0;
Zhu Yib481de92007-09-25 17:54:57 -07005387
5388 iwl4965_rf_kill_ct_config(priv);
Zhu Yi5a669262008-01-14 17:46:18 -08005389
Reinette Chatrefe00b5a2008-04-03 16:05:23 -07005390 iwl_leds_register(priv);
5391
Zhu Yib481de92007-09-25 17:54:57 -07005392 IWL_DEBUG_INFO("ALIVE processing complete.\n");
Rick Farringtona9f46782008-03-18 14:57:49 -07005393 set_bit(STATUS_READY, &priv->status);
Zhu Yi5a669262008-01-14 17:46:18 -08005394 wake_up_interruptible(&priv->wait_command_queue);
Zhu Yib481de92007-09-25 17:54:57 -07005395
5396 if (priv->error_recovering)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005397 iwl4965_error_recovery(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005398
Mohamed Abbasc8381fd2008-03-28 16:21:05 -07005399 iwlcore_low_level_notify(priv, IWLCORE_START_EVT);
Mohamed Abbas84363e62008-04-04 16:59:58 -07005400 ieee80211_notify_mac(priv->hw, IEEE80211_NOTIFY_RE_ASSOC);
Zhu Yib481de92007-09-25 17:54:57 -07005401 return;
5402
5403 restart:
5404 queue_work(priv->workqueue, &priv->restart);
5405}
5406
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005407static void iwl4965_cancel_deferred_work(struct iwl_priv *priv);
Zhu Yib481de92007-09-25 17:54:57 -07005408
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005409static void __iwl4965_down(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005410{
5411 unsigned long flags;
5412 int exit_pending = test_bit(STATUS_EXIT_PENDING, &priv->status);
5413 struct ieee80211_conf *conf = NULL;
5414
5415 IWL_DEBUG_INFO(DRV_NAME " is going down\n");
5416
5417 conf = ieee80211_get_hw_conf(priv->hw);
5418
5419 if (!exit_pending)
5420 set_bit(STATUS_EXIT_PENDING, &priv->status);
5421
Mohamed Abbasab53d8a2008-03-25 16:33:36 -07005422 iwl_leds_unregister(priv);
5423
Mohamed Abbasc8381fd2008-03-28 16:21:05 -07005424 iwlcore_low_level_notify(priv, IWLCORE_STOP_EVT);
5425
Assaf Kraussbf85ea42008-03-14 10:38:49 -07005426 iwlcore_clear_stations_table(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005427
5428 /* Unblock any waiting calls */
5429 wake_up_interruptible_all(&priv->wait_command_queue);
5430
Zhu Yib481de92007-09-25 17:54:57 -07005431 /* Wipe out the EXIT_PENDING status bit if we are not actually
5432 * exiting the module */
5433 if (!exit_pending)
5434 clear_bit(STATUS_EXIT_PENDING, &priv->status);
5435
5436 /* stop and reset the on-board processor */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07005437 iwl_write32(priv, CSR_RESET, CSR_RESET_REG_FLAG_NEVO_RESET);
Zhu Yib481de92007-09-25 17:54:57 -07005438
5439 /* tell the device to stop sending interrupts */
Mohamed Abbas0359fac2008-03-28 16:21:08 -07005440 spin_lock_irqsave(&priv->lock, flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005441 iwl4965_disable_interrupts(priv);
Mohamed Abbas0359fac2008-03-28 16:21:08 -07005442 spin_unlock_irqrestore(&priv->lock, flags);
5443 iwl_synchronize_irq(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005444
5445 if (priv->mac80211_registered)
5446 ieee80211_stop_queues(priv->hw);
5447
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005448 /* If we have not previously called iwl4965_init() then
Zhu Yib481de92007-09-25 17:54:57 -07005449 * clear all bits but the RF Kill and SUSPEND bits and return */
Tomas Winklerfee12472008-04-03 16:05:21 -07005450 if (!iwl_is_init(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07005451 priv->status = test_bit(STATUS_RF_KILL_HW, &priv->status) <<
5452 STATUS_RF_KILL_HW |
5453 test_bit(STATUS_RF_KILL_SW, &priv->status) <<
5454 STATUS_RF_KILL_SW |
Reinette Chatre97888642008-02-06 11:20:38 -08005455 test_bit(STATUS_GEO_CONFIGURED, &priv->status) <<
5456 STATUS_GEO_CONFIGURED |
Zhu Yib481de92007-09-25 17:54:57 -07005457 test_bit(STATUS_IN_SUSPEND, &priv->status) <<
5458 STATUS_IN_SUSPEND;
5459 goto exit;
5460 }
5461
5462 /* ...otherwise clear out all the status bits but the RF Kill and
5463 * SUSPEND bits and continue taking the NIC down. */
5464 priv->status &= test_bit(STATUS_RF_KILL_HW, &priv->status) <<
5465 STATUS_RF_KILL_HW |
5466 test_bit(STATUS_RF_KILL_SW, &priv->status) <<
5467 STATUS_RF_KILL_SW |
Reinette Chatre97888642008-02-06 11:20:38 -08005468 test_bit(STATUS_GEO_CONFIGURED, &priv->status) <<
5469 STATUS_GEO_CONFIGURED |
Zhu Yib481de92007-09-25 17:54:57 -07005470 test_bit(STATUS_IN_SUSPEND, &priv->status) <<
5471 STATUS_IN_SUSPEND |
5472 test_bit(STATUS_FW_ERROR, &priv->status) <<
5473 STATUS_FW_ERROR;
5474
5475 spin_lock_irqsave(&priv->lock, flags);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07005476 iwl_clear_bit(priv, CSR_GP_CNTRL,
Ben Cahill9fbab512007-11-29 11:09:47 +08005477 CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
Zhu Yib481de92007-09-25 17:54:57 -07005478 spin_unlock_irqrestore(&priv->lock, flags);
5479
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005480 iwl4965_hw_txq_ctx_stop(priv);
5481 iwl4965_hw_rxq_stop(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005482
5483 spin_lock_irqsave(&priv->lock, flags);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07005484 if (!iwl_grab_nic_access(priv)) {
5485 iwl_write_prph(priv, APMG_CLK_DIS_REG,
Zhu Yib481de92007-09-25 17:54:57 -07005486 APMG_CLK_VAL_DMA_CLK_RQT);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07005487 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005488 }
5489 spin_unlock_irqrestore(&priv->lock, flags);
5490
5491 udelay(5);
5492
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005493 iwl4965_hw_nic_stop_master(priv);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07005494 iwl_set_bit(priv, CSR_RESET, CSR_RESET_REG_FLAG_SW_RESET);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005495 iwl4965_hw_nic_reset(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005496
5497 exit:
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005498 memset(&priv->card_alive, 0, sizeof(struct iwl4965_alive_resp));
Zhu Yib481de92007-09-25 17:54:57 -07005499
5500 if (priv->ibss_beacon)
5501 dev_kfree_skb(priv->ibss_beacon);
5502 priv->ibss_beacon = NULL;
5503
5504 /* clear out any free frames */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005505 iwl4965_clear_free_frames(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005506}
5507
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005508static void iwl4965_down(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005509{
5510 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005511 __iwl4965_down(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005512 mutex_unlock(&priv->mutex);
Zhu Yib24d22b2007-12-19 13:59:52 +08005513
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005514 iwl4965_cancel_deferred_work(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005515}
5516
5517#define MAX_HW_RESTARTS 5
5518
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005519static int __iwl4965_up(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005520{
Tomas Winkler57aab752008-04-14 21:16:03 -07005521 int i;
5522 int ret;
Zhu Yib481de92007-09-25 17:54:57 -07005523
5524 if (test_bit(STATUS_EXIT_PENDING, &priv->status)) {
5525 IWL_WARNING("Exit pending; will not bring the NIC up\n");
5526 return -EIO;
5527 }
5528
5529 if (test_bit(STATUS_RF_KILL_SW, &priv->status)) {
5530 IWL_WARNING("Radio disabled by SW RF kill (module "
5531 "parameter)\n");
Mohamed Abbasad97edd2008-03-28 16:21:06 -07005532 iwl_rfkill_set_hw_state(priv);
Zhu Yie655b9f2008-01-24 02:19:38 -08005533 return -ENODEV;
5534 }
5535
Reinette Chatree903fbd2008-01-30 22:05:15 -08005536 if (!priv->ucode_data_backup.v_addr || !priv->ucode_data.v_addr) {
5537 IWL_ERROR("ucode not available for device bringup\n");
5538 return -EIO;
5539 }
5540
Zhu Yie655b9f2008-01-24 02:19:38 -08005541 /* If platform's RF_KILL switch is NOT set to KILL */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07005542 if (iwl_read32(priv, CSR_GP_CNTRL) &
Zhu Yie655b9f2008-01-24 02:19:38 -08005543 CSR_GP_CNTRL_REG_FLAG_HW_RF_KILL_SW)
5544 clear_bit(STATUS_RF_KILL_HW, &priv->status);
5545 else {
5546 set_bit(STATUS_RF_KILL_HW, &priv->status);
5547 if (!test_bit(STATUS_IN_SUSPEND, &priv->status)) {
Mohamed Abbasad97edd2008-03-28 16:21:06 -07005548 iwl_rfkill_set_hw_state(priv);
Zhu Yie655b9f2008-01-24 02:19:38 -08005549 IWL_WARNING("Radio disabled by HW RF Kill switch\n");
5550 return -ENODEV;
5551 }
Zhu Yib481de92007-09-25 17:54:57 -07005552 }
5553
Mohamed Abbasad97edd2008-03-28 16:21:06 -07005554 iwl_rfkill_set_hw_state(priv);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07005555 iwl_write32(priv, CSR_INT, 0xFFFFFFFF);
Zhu Yib481de92007-09-25 17:54:57 -07005556
Tomas Winkler57aab752008-04-14 21:16:03 -07005557 ret = priv->cfg->ops->lib->hw_nic_init(priv);
5558 if (ret) {
5559 IWL_ERROR("Unable to init nic\n");
5560 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07005561 }
5562
5563 /* make sure rfkill handshake bits are cleared */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07005564 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
5565 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR,
Zhu Yib481de92007-09-25 17:54:57 -07005566 CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
5567
5568 /* clear (again), then enable host interrupts */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07005569 iwl_write32(priv, CSR_INT, 0xFFFFFFFF);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005570 iwl4965_enable_interrupts(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005571
5572 /* really make sure rfkill handshake bits are cleared */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07005573 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
5574 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
Zhu Yib481de92007-09-25 17:54:57 -07005575
5576 /* Copy original ucode data image from disk into backup cache.
5577 * This will be used to initialize the on-board processor's
5578 * data SRAM for a clean start when the runtime program first loads. */
5579 memcpy(priv->ucode_data_backup.v_addr, priv->ucode_data.v_addr,
Zhu Yi5a669262008-01-14 17:46:18 -08005580 priv->ucode_data.len);
Zhu Yib481de92007-09-25 17:54:57 -07005581
Zhu Yie655b9f2008-01-24 02:19:38 -08005582 /* We return success when we resume from suspend and rf_kill is on. */
5583 if (test_bit(STATUS_RF_KILL_HW, &priv->status))
Zhu Yib481de92007-09-25 17:54:57 -07005584 return 0;
Zhu Yib481de92007-09-25 17:54:57 -07005585
5586 for (i = 0; i < MAX_HW_RESTARTS; i++) {
5587
Assaf Kraussbf85ea42008-03-14 10:38:49 -07005588 iwlcore_clear_stations_table(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005589
5590 /* load bootstrap state machine,
5591 * load bootstrap program into processor's memory,
5592 * prepare to load the "initialize" uCode */
Tomas Winkler57aab752008-04-14 21:16:03 -07005593 ret = priv->cfg->ops->lib->load_ucode(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005594
Tomas Winkler57aab752008-04-14 21:16:03 -07005595 if (ret) {
5596 IWL_ERROR("Unable to set up bootstrap uCode: %d\n", ret);
Zhu Yib481de92007-09-25 17:54:57 -07005597 continue;
5598 }
5599
5600 /* start card; "initialize" will load runtime ucode */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005601 iwl4965_nic_start(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005602
Zhu Yib481de92007-09-25 17:54:57 -07005603 IWL_DEBUG_INFO(DRV_NAME " is coming up\n");
5604
5605 return 0;
5606 }
5607
5608 set_bit(STATUS_EXIT_PENDING, &priv->status);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005609 __iwl4965_down(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005610
5611 /* tried to restart and config the device for as long as our
5612 * patience could withstand */
5613 IWL_ERROR("Unable to initialize device after %d attempts.\n", i);
5614 return -EIO;
5615}
5616
5617
5618/*****************************************************************************
5619 *
5620 * Workqueue callbacks
5621 *
5622 *****************************************************************************/
5623
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005624static void iwl4965_bg_init_alive_start(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07005625{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005626 struct iwl_priv *priv =
5627 container_of(data, struct iwl_priv, init_alive_start.work);
Zhu Yib481de92007-09-25 17:54:57 -07005628
5629 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
5630 return;
5631
5632 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005633 iwl4965_init_alive_start(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005634 mutex_unlock(&priv->mutex);
5635}
5636
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005637static void iwl4965_bg_alive_start(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07005638{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005639 struct iwl_priv *priv =
5640 container_of(data, struct iwl_priv, alive_start.work);
Zhu Yib481de92007-09-25 17:54:57 -07005641
5642 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
5643 return;
5644
5645 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005646 iwl4965_alive_start(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005647 mutex_unlock(&priv->mutex);
5648}
5649
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005650static void iwl4965_bg_rf_kill(struct work_struct *work)
Zhu Yib481de92007-09-25 17:54:57 -07005651{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005652 struct iwl_priv *priv = container_of(work, struct iwl_priv, rf_kill);
Zhu Yib481de92007-09-25 17:54:57 -07005653
5654 wake_up_interruptible(&priv->wait_command_queue);
5655
5656 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
5657 return;
5658
5659 mutex_lock(&priv->mutex);
5660
Tomas Winklerfee12472008-04-03 16:05:21 -07005661 if (!iwl_is_rfkill(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07005662 IWL_DEBUG(IWL_DL_INFO | IWL_DL_RF_KILL,
5663 "HW and/or SW RF Kill no longer active, restarting "
5664 "device\n");
5665 if (!test_bit(STATUS_EXIT_PENDING, &priv->status))
5666 queue_work(priv->workqueue, &priv->restart);
5667 } else {
Mohamed Abbasad97edd2008-03-28 16:21:06 -07005668 /* make sure mac80211 stop sending Tx frame */
5669 if (priv->mac80211_registered)
5670 ieee80211_stop_queues(priv->hw);
Zhu Yib481de92007-09-25 17:54:57 -07005671
5672 if (!test_bit(STATUS_RF_KILL_HW, &priv->status))
5673 IWL_DEBUG_RF_KILL("Can not turn radio back on - "
5674 "disabled by SW switch\n");
5675 else
5676 IWL_WARNING("Radio Frequency Kill Switch is On:\n"
5677 "Kill switch must be turned off for "
5678 "wireless networking to work.\n");
5679 }
Mohamed Abbasad97edd2008-03-28 16:21:06 -07005680 iwl_rfkill_set_hw_state(priv);
5681
Zhu Yib481de92007-09-25 17:54:57 -07005682 mutex_unlock(&priv->mutex);
5683}
5684
5685#define IWL_SCAN_CHECK_WATCHDOG (7 * HZ)
5686
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005687static void iwl4965_bg_scan_check(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07005688{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005689 struct iwl_priv *priv =
5690 container_of(data, struct iwl_priv, scan_check.work);
Zhu Yib481de92007-09-25 17:54:57 -07005691
5692 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
5693 return;
5694
5695 mutex_lock(&priv->mutex);
5696 if (test_bit(STATUS_SCANNING, &priv->status) ||
5697 test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
5698 IWL_DEBUG(IWL_DL_INFO | IWL_DL_SCAN,
5699 "Scan completion watchdog resetting adapter (%dms)\n",
5700 jiffies_to_msecs(IWL_SCAN_CHECK_WATCHDOG));
mabbas052c4b92007-10-25 17:15:43 +08005701
Zhu Yib481de92007-09-25 17:54:57 -07005702 if (!test_bit(STATUS_EXIT_PENDING, &priv->status))
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005703 iwl4965_send_scan_abort(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005704 }
5705 mutex_unlock(&priv->mutex);
5706}
5707
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005708static void iwl4965_bg_request_scan(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07005709{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005710 struct iwl_priv *priv =
5711 container_of(data, struct iwl_priv, request_scan);
Tomas Winkler857485c2008-03-21 13:53:44 -07005712 struct iwl_host_cmd cmd = {
Zhu Yib481de92007-09-25 17:54:57 -07005713 .id = REPLY_SCAN_CMD,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005714 .len = sizeof(struct iwl4965_scan_cmd),
Zhu Yib481de92007-09-25 17:54:57 -07005715 .meta.flags = CMD_SIZE_HUGE,
5716 };
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005717 struct iwl4965_scan_cmd *scan;
Zhu Yib481de92007-09-25 17:54:57 -07005718 struct ieee80211_conf *conf = NULL;
Tomas Winkler78330fd2008-02-06 02:37:18 +02005719 u16 cmd_len;
Johannes Berg8318d782008-01-24 19:38:38 +01005720 enum ieee80211_band band;
Tomas Winkler78330fd2008-02-06 02:37:18 +02005721 u8 direct_mask;
Tomas Winkler857485c2008-03-21 13:53:44 -07005722 int ret = 0;
Zhu Yib481de92007-09-25 17:54:57 -07005723
5724 conf = ieee80211_get_hw_conf(priv->hw);
5725
5726 mutex_lock(&priv->mutex);
5727
Tomas Winklerfee12472008-04-03 16:05:21 -07005728 if (!iwl_is_ready(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07005729 IWL_WARNING("request scan called when driver not ready.\n");
5730 goto done;
5731 }
5732
5733 /* Make sure the scan wasn't cancelled before this queued work
5734 * was given the chance to run... */
5735 if (!test_bit(STATUS_SCANNING, &priv->status))
5736 goto done;
5737
5738 /* This should never be called or scheduled if there is currently
5739 * a scan active in the hardware. */
5740 if (test_bit(STATUS_SCAN_HW, &priv->status)) {
5741 IWL_DEBUG_INFO("Multiple concurrent scan requests in parallel. "
5742 "Ignoring second request.\n");
Tomas Winkler857485c2008-03-21 13:53:44 -07005743 ret = -EIO;
Zhu Yib481de92007-09-25 17:54:57 -07005744 goto done;
5745 }
5746
5747 if (test_bit(STATUS_EXIT_PENDING, &priv->status)) {
5748 IWL_DEBUG_SCAN("Aborting scan due to device shutdown\n");
5749 goto done;
5750 }
5751
5752 if (test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
5753 IWL_DEBUG_HC("Scan request while abort pending. Queuing.\n");
5754 goto done;
5755 }
5756
Tomas Winklerfee12472008-04-03 16:05:21 -07005757 if (iwl_is_rfkill(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07005758 IWL_DEBUG_HC("Aborting scan due to RF Kill activation\n");
5759 goto done;
5760 }
5761
5762 if (!test_bit(STATUS_READY, &priv->status)) {
5763 IWL_DEBUG_HC("Scan request while uninitialized. Queuing.\n");
5764 goto done;
5765 }
5766
5767 if (!priv->scan_bands) {
5768 IWL_DEBUG_HC("Aborting scan due to no requested bands\n");
5769 goto done;
5770 }
5771
5772 if (!priv->scan) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005773 priv->scan = kmalloc(sizeof(struct iwl4965_scan_cmd) +
Zhu Yib481de92007-09-25 17:54:57 -07005774 IWL_MAX_SCAN_SIZE, GFP_KERNEL);
5775 if (!priv->scan) {
Tomas Winkler857485c2008-03-21 13:53:44 -07005776 ret = -ENOMEM;
Zhu Yib481de92007-09-25 17:54:57 -07005777 goto done;
5778 }
5779 }
5780 scan = priv->scan;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005781 memset(scan, 0, sizeof(struct iwl4965_scan_cmd) + IWL_MAX_SCAN_SIZE);
Zhu Yib481de92007-09-25 17:54:57 -07005782
5783 scan->quiet_plcp_th = IWL_PLCP_QUIET_THRESH;
5784 scan->quiet_time = IWL_ACTIVE_QUIET_TIME;
5785
Tomas Winkler3109ece2008-03-28 16:33:35 -07005786 if (iwl_is_associated(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07005787 u16 interval = 0;
5788 u32 extra;
5789 u32 suspend_time = 100;
5790 u32 scan_suspend_time = 100;
5791 unsigned long flags;
5792
5793 IWL_DEBUG_INFO("Scanning while associated...\n");
5794
5795 spin_lock_irqsave(&priv->lock, flags);
5796 interval = priv->beacon_int;
5797 spin_unlock_irqrestore(&priv->lock, flags);
5798
5799 scan->suspend_time = 0;
mabbas052c4b92007-10-25 17:15:43 +08005800 scan->max_out_time = cpu_to_le32(200 * 1024);
Zhu Yib481de92007-09-25 17:54:57 -07005801 if (!interval)
5802 interval = suspend_time;
5803
5804 extra = (suspend_time / interval) << 22;
5805 scan_suspend_time = (extra |
5806 ((suspend_time % interval) * 1024));
5807 scan->suspend_time = cpu_to_le32(scan_suspend_time);
5808 IWL_DEBUG_SCAN("suspend_time 0x%X beacon interval %d\n",
5809 scan_suspend_time, interval);
5810 }
5811
5812 /* We should add the ability for user to lock to PASSIVE ONLY */
5813 if (priv->one_direct_scan) {
5814 IWL_DEBUG_SCAN
5815 ("Kicking off one direct scan for '%s'\n",
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005816 iwl4965_escape_essid(priv->direct_ssid,
Zhu Yib481de92007-09-25 17:54:57 -07005817 priv->direct_ssid_len));
5818 scan->direct_scan[0].id = WLAN_EID_SSID;
5819 scan->direct_scan[0].len = priv->direct_ssid_len;
5820 memcpy(scan->direct_scan[0].ssid,
5821 priv->direct_ssid, priv->direct_ssid_len);
5822 direct_mask = 1;
Tomas Winkler3109ece2008-03-28 16:33:35 -07005823 } else if (!iwl_is_associated(priv) && priv->essid_len) {
Bill Moss786b4552008-04-17 16:03:40 -07005824 IWL_DEBUG_SCAN
5825 ("Kicking off one direct scan for '%s' when not associated\n",
5826 iwl4965_escape_essid(priv->essid, priv->essid_len));
Zhu Yib481de92007-09-25 17:54:57 -07005827 scan->direct_scan[0].id = WLAN_EID_SSID;
5828 scan->direct_scan[0].len = priv->essid_len;
5829 memcpy(scan->direct_scan[0].ssid, priv->essid, priv->essid_len);
5830 direct_mask = 1;
Tomas Winkler857485c2008-03-21 13:53:44 -07005831 } else {
Bill Moss786b4552008-04-17 16:03:40 -07005832 IWL_DEBUG_SCAN("Kicking off one indirect scan.\n");
Zhu Yib481de92007-09-25 17:54:57 -07005833 direct_mask = 0;
Tomas Winkler857485c2008-03-21 13:53:44 -07005834 }
Zhu Yib481de92007-09-25 17:54:57 -07005835
Zhu Yib481de92007-09-25 17:54:57 -07005836 scan->tx_cmd.tx_flags = TX_CMD_FLG_SEQ_CTL_MSK;
Tomas Winkler5425e492008-04-15 16:01:38 -07005837 scan->tx_cmd.sta_id = priv->hw_params.bcast_sta_id;
Zhu Yib481de92007-09-25 17:54:57 -07005838 scan->tx_cmd.stop_time.life_time = TX_CMD_LIFE_TIME_INFINITE;
5839
Zhu Yib481de92007-09-25 17:54:57 -07005840
Ron Rindjunsky66b50042008-06-25 16:46:31 +08005841 if (priv->scan_bands & BIT(IEEE80211_BAND_2GHZ)) {
Zhu Yib481de92007-09-25 17:54:57 -07005842 scan->flags = RXON_FLG_BAND_24G_MSK | RXON_FLG_AUTO_DETECT_MSK;
5843 scan->tx_cmd.rate_n_flags =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005844 iwl4965_hw_set_rate_n_flags(IWL_RATE_1M_PLCP,
Zhu Yib481de92007-09-25 17:54:57 -07005845 RATE_MCS_ANT_B_MSK|RATE_MCS_CCK_MSK);
5846
5847 scan->good_CRC_th = 0;
Johannes Berg8318d782008-01-24 19:38:38 +01005848 band = IEEE80211_BAND_2GHZ;
Ron Rindjunsky66b50042008-06-25 16:46:31 +08005849 } else if (priv->scan_bands & BIT(IEEE80211_BAND_5GHZ)) {
Zhu Yib481de92007-09-25 17:54:57 -07005850 scan->tx_cmd.rate_n_flags =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005851 iwl4965_hw_set_rate_n_flags(IWL_RATE_6M_PLCP,
Zhu Yib481de92007-09-25 17:54:57 -07005852 RATE_MCS_ANT_B_MSK);
5853 scan->good_CRC_th = IWL_GOOD_CRC_TH;
Johannes Berg8318d782008-01-24 19:38:38 +01005854 band = IEEE80211_BAND_5GHZ;
Ron Rindjunsky66b50042008-06-25 16:46:31 +08005855 } else {
Zhu Yib481de92007-09-25 17:54:57 -07005856 IWL_WARNING("Invalid scan band count\n");
5857 goto done;
5858 }
5859
Tomas Winkler78330fd2008-02-06 02:37:18 +02005860 /* We don't build a direct scan probe request; the uCode will do
5861 * that based on the direct_mask added to each channel entry */
5862 cmd_len = iwl4965_fill_probe_req(priv, band,
5863 (struct ieee80211_mgmt *)scan->data,
5864 IWL_MAX_SCAN_SIZE - sizeof(*scan), 0);
5865
5866 scan->tx_cmd.len = cpu_to_le16(cmd_len);
Zhu Yib481de92007-09-25 17:54:57 -07005867 /* select Rx chains */
5868
5869 /* Force use of chains B and C (0x6) for scan Rx.
5870 * Avoid A (0x1) because of its off-channel reception on A-band.
5871 * MIMO is not used here, but value is required to make uCode happy. */
5872 scan->rx_chain = RXON_RX_CHAIN_DRIVER_FORCE_MSK |
5873 cpu_to_le16((0x7 << RXON_RX_CHAIN_VALID_POS) |
5874 (0x6 << RXON_RX_CHAIN_FORCE_SEL_POS) |
5875 (0x7 << RXON_RX_CHAIN_FORCE_MIMO_SEL_POS));
5876
5877 if (priv->iw_mode == IEEE80211_IF_TYPE_MNTR)
5878 scan->filter_flags = RXON_FILTER_PROMISC_MSK;
5879
Bill Moss786b4552008-04-17 16:03:40 -07005880 if (direct_mask)
Reinette Chatre26c0f032008-03-11 16:17:15 -07005881 scan->channel_count =
5882 iwl4965_get_channels_for_scan(
5883 priv, band, 1, /* active */
5884 direct_mask,
5885 (void *)&scan->data[le16_to_cpu(scan->tx_cmd.len)]);
Bill Moss786b4552008-04-17 16:03:40 -07005886 else
Reinette Chatre26c0f032008-03-11 16:17:15 -07005887 scan->channel_count =
5888 iwl4965_get_channels_for_scan(
5889 priv, band, 0, /* passive */
5890 direct_mask,
5891 (void *)&scan->data[le16_to_cpu(scan->tx_cmd.len)]);
Zhu Yib481de92007-09-25 17:54:57 -07005892
5893 cmd.len += le16_to_cpu(scan->tx_cmd.len) +
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005894 scan->channel_count * sizeof(struct iwl4965_scan_channel);
Zhu Yib481de92007-09-25 17:54:57 -07005895 cmd.data = scan;
5896 scan->len = cpu_to_le16(cmd.len);
5897
5898 set_bit(STATUS_SCAN_HW, &priv->status);
Tomas Winkler857485c2008-03-21 13:53:44 -07005899 ret = iwl_send_cmd_sync(priv, &cmd);
5900 if (ret)
Zhu Yib481de92007-09-25 17:54:57 -07005901 goto done;
5902
5903 queue_delayed_work(priv->workqueue, &priv->scan_check,
5904 IWL_SCAN_CHECK_WATCHDOG);
5905
5906 mutex_unlock(&priv->mutex);
5907 return;
5908
5909 done:
Ian Schram01ebd062007-10-25 17:15:22 +08005910 /* inform mac80211 scan aborted */
Zhu Yib481de92007-09-25 17:54:57 -07005911 queue_work(priv->workqueue, &priv->scan_completed);
5912 mutex_unlock(&priv->mutex);
5913}
5914
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005915static void iwl4965_bg_up(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07005916{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005917 struct iwl_priv *priv = container_of(data, struct iwl_priv, up);
Zhu Yib481de92007-09-25 17:54:57 -07005918
5919 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
5920 return;
5921
5922 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005923 __iwl4965_up(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005924 mutex_unlock(&priv->mutex);
5925}
5926
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005927static void iwl4965_bg_restart(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07005928{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005929 struct iwl_priv *priv = container_of(data, struct iwl_priv, restart);
Zhu Yib481de92007-09-25 17:54:57 -07005930
5931 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
5932 return;
5933
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005934 iwl4965_down(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005935 queue_work(priv->workqueue, &priv->up);
5936}
5937
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005938static void iwl4965_bg_rx_replenish(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07005939{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005940 struct iwl_priv *priv =
5941 container_of(data, struct iwl_priv, rx_replenish);
Zhu Yib481de92007-09-25 17:54:57 -07005942
5943 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
5944 return;
5945
5946 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005947 iwl4965_rx_replenish(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005948 mutex_unlock(&priv->mutex);
5949}
5950
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08005951#define IWL_DELAY_NEXT_SCAN (HZ*2)
5952
Reinette Chatre508e32e2008-04-14 21:16:13 -07005953static void iwl4965_post_associate(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005954{
Zhu Yib481de92007-09-25 17:54:57 -07005955 struct ieee80211_conf *conf = NULL;
Tomas Winkler857485c2008-03-21 13:53:44 -07005956 int ret = 0;
Joe Perches0795af52007-10-03 17:59:30 -07005957 DECLARE_MAC_BUF(mac);
Zhu Yib481de92007-09-25 17:54:57 -07005958
5959 if (priv->iw_mode == IEEE80211_IF_TYPE_AP) {
5960 IWL_ERROR("%s Should not be called in AP mode\n", __FUNCTION__);
5961 return;
5962 }
5963
Joe Perches0795af52007-10-03 17:59:30 -07005964 IWL_DEBUG_ASSOC("Associated as %d to: %s\n",
5965 priv->assoc_id,
5966 print_mac(mac, priv->active_rxon.bssid_addr));
Zhu Yib481de92007-09-25 17:54:57 -07005967
5968
5969 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
5970 return;
5971
Zhu Yib481de92007-09-25 17:54:57 -07005972
Reinette Chatre508e32e2008-04-14 21:16:13 -07005973 if (!priv->vif || !priv->is_open)
Mohamed Abbas948c1712007-10-25 17:15:45 +08005974 return;
Reinette Chatre508e32e2008-04-14 21:16:13 -07005975
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005976 iwl4965_scan_cancel_timeout(priv, 200);
mabbas052c4b92007-10-25 17:15:43 +08005977
Zhu Yib481de92007-09-25 17:54:57 -07005978 conf = ieee80211_get_hw_conf(priv->hw);
5979
5980 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005981 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005982
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005983 memset(&priv->rxon_timing, 0, sizeof(struct iwl4965_rxon_time_cmd));
5984 iwl4965_setup_rxon_timing(priv);
Tomas Winkler857485c2008-03-21 13:53:44 -07005985 ret = iwl_send_cmd_pdu(priv, REPLY_RXON_TIMING,
Zhu Yib481de92007-09-25 17:54:57 -07005986 sizeof(priv->rxon_timing), &priv->rxon_timing);
Tomas Winkler857485c2008-03-21 13:53:44 -07005987 if (ret)
Zhu Yib481de92007-09-25 17:54:57 -07005988 IWL_WARNING("REPLY_RXON_TIMING failed - "
5989 "Attempting to continue.\n");
5990
5991 priv->staging_rxon.filter_flags |= RXON_FILTER_ASSOC_MSK;
5992
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08005993#ifdef CONFIG_IWL4965_HT
Ron Rindjunskyfd105e72007-11-26 16:14:39 +02005994 if (priv->current_ht_config.is_ht)
5995 iwl4965_set_rxon_ht(priv, &priv->current_ht_config);
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08005996#endif /* CONFIG_IWL4965_HT*/
Zhu Yib481de92007-09-25 17:54:57 -07005997 iwl4965_set_rxon_chain(priv);
5998 priv->staging_rxon.assoc_id = cpu_to_le16(priv->assoc_id);
5999
6000 IWL_DEBUG_ASSOC("assoc id %d beacon interval %d\n",
6001 priv->assoc_id, priv->beacon_int);
6002
6003 if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_PREAMBLE)
6004 priv->staging_rxon.flags |= RXON_FLG_SHORT_PREAMBLE_MSK;
6005 else
6006 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_PREAMBLE_MSK;
6007
6008 if (priv->staging_rxon.flags & RXON_FLG_BAND_24G_MSK) {
6009 if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_SLOT_TIME)
6010 priv->staging_rxon.flags |= RXON_FLG_SHORT_SLOT_MSK;
6011 else
6012 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
6013
6014 if (priv->iw_mode == IEEE80211_IF_TYPE_IBSS)
6015 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
6016
6017 }
6018
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006019 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006020
6021 switch (priv->iw_mode) {
6022 case IEEE80211_IF_TYPE_STA:
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006023 iwl4965_rate_scale_init(priv->hw, IWL_AP_ID);
Zhu Yib481de92007-09-25 17:54:57 -07006024 break;
6025
6026 case IEEE80211_IF_TYPE_IBSS:
6027
6028 /* clear out the station table */
Assaf Kraussbf85ea42008-03-14 10:38:49 -07006029 iwlcore_clear_stations_table(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006030
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006031 iwl4965_rxon_add_station(priv, iwl4965_broadcast_addr, 0);
6032 iwl4965_rxon_add_station(priv, priv->bssid, 0);
6033 iwl4965_rate_scale_init(priv->hw, IWL_STA_ID);
6034 iwl4965_send_beacon_cmd(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006035
6036 break;
6037
6038 default:
6039 IWL_ERROR("%s Should not be called in %d mode\n",
6040 __FUNCTION__, priv->iw_mode);
6041 break;
6042 }
6043
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006044 iwl4965_sequence_reset(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006045
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08006046#ifdef CONFIG_IWL4965_SENSITIVITY
Zhu Yib481de92007-09-25 17:54:57 -07006047 /* Enable Rx differential gain and sensitivity calibrations */
6048 iwl4965_chain_noise_reset(priv);
6049 priv->start_calib = 1;
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08006050#endif /* CONFIG_IWL4965_SENSITIVITY */
Zhu Yib481de92007-09-25 17:54:57 -07006051
6052 if (priv->iw_mode == IEEE80211_IF_TYPE_IBSS)
6053 priv->assoc_station_added = 1;
6054
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006055 iwl4965_activate_qos(priv, 0);
Ron Rindjunsky292ae172008-02-06 11:20:39 -08006056
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08006057 /* we have just associated, don't start scan too early */
6058 priv->next_scan_jiffies = jiffies + IWL_DELAY_NEXT_SCAN;
Reinette Chatre508e32e2008-04-14 21:16:13 -07006059}
6060
6061
6062static void iwl4965_bg_post_associate(struct work_struct *data)
6063{
6064 struct iwl_priv *priv = container_of(data, struct iwl_priv,
6065 post_associate.work);
6066
6067 mutex_lock(&priv->mutex);
6068 iwl4965_post_associate(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006069 mutex_unlock(&priv->mutex);
Reinette Chatre508e32e2008-04-14 21:16:13 -07006070
Zhu Yib481de92007-09-25 17:54:57 -07006071}
6072
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006073static void iwl4965_bg_abort_scan(struct work_struct *work)
Zhu Yib481de92007-09-25 17:54:57 -07006074{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006075 struct iwl_priv *priv = container_of(work, struct iwl_priv, abort_scan);
Zhu Yib481de92007-09-25 17:54:57 -07006076
Tomas Winklerfee12472008-04-03 16:05:21 -07006077 if (!iwl_is_ready(priv))
Zhu Yib481de92007-09-25 17:54:57 -07006078 return;
6079
6080 mutex_lock(&priv->mutex);
6081
6082 set_bit(STATUS_SCAN_ABORTING, &priv->status);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006083 iwl4965_send_scan_abort(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006084
6085 mutex_unlock(&priv->mutex);
6086}
6087
Zhu Yi76bb77e2007-11-22 10:53:22 +08006088static int iwl4965_mac_config(struct ieee80211_hw *hw, struct ieee80211_conf *conf);
6089
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006090static void iwl4965_bg_scan_completed(struct work_struct *work)
Zhu Yib481de92007-09-25 17:54:57 -07006091{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006092 struct iwl_priv *priv =
6093 container_of(work, struct iwl_priv, scan_completed);
Zhu Yib481de92007-09-25 17:54:57 -07006094
6095 IWL_DEBUG(IWL_DL_INFO | IWL_DL_SCAN, "SCAN complete scan\n");
6096
6097 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
6098 return;
6099
Zhu Yia0646472007-12-20 14:10:01 +08006100 if (test_bit(STATUS_CONF_PENDING, &priv->status))
6101 iwl4965_mac_config(priv->hw, ieee80211_get_hw_conf(priv->hw));
Zhu Yi76bb77e2007-11-22 10:53:22 +08006102
Zhu Yib481de92007-09-25 17:54:57 -07006103 ieee80211_scan_completed(priv->hw);
6104
6105 /* Since setting the TXPOWER may have been deferred while
6106 * performing the scan, fire one off */
6107 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006108 iwl4965_hw_reg_send_txpower(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006109 mutex_unlock(&priv->mutex);
6110}
6111
6112/*****************************************************************************
6113 *
6114 * mac80211 entry point functions
6115 *
6116 *****************************************************************************/
6117
Zhu Yi5a669262008-01-14 17:46:18 -08006118#define UCODE_READY_TIMEOUT (2 * HZ)
6119
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006120static int iwl4965_mac_start(struct ieee80211_hw *hw)
Zhu Yib481de92007-09-25 17:54:57 -07006121{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006122 struct iwl_priv *priv = hw->priv;
Zhu Yi5a669262008-01-14 17:46:18 -08006123 int ret;
Zhu Yib481de92007-09-25 17:54:57 -07006124
6125 IWL_DEBUG_MAC80211("enter\n");
6126
Zhu Yi5a669262008-01-14 17:46:18 -08006127 if (pci_enable_device(priv->pci_dev)) {
6128 IWL_ERROR("Fail to pci_enable_device\n");
6129 return -ENODEV;
6130 }
6131 pci_restore_state(priv->pci_dev);
6132 pci_enable_msi(priv->pci_dev);
6133
6134 ret = request_irq(priv->pci_dev->irq, iwl4965_isr, IRQF_SHARED,
6135 DRV_NAME, priv);
6136 if (ret) {
6137 IWL_ERROR("Error allocating IRQ %d\n", priv->pci_dev->irq);
6138 goto out_disable_msi;
6139 }
6140
Zhu Yib481de92007-09-25 17:54:57 -07006141 /* we should be verifying the device is ready to be opened */
6142 mutex_lock(&priv->mutex);
6143
Zhu Yi5a669262008-01-14 17:46:18 -08006144 memset(&priv->staging_rxon, 0, sizeof(struct iwl4965_rxon_cmd));
6145 /* fetch ucode file from disk, alloc and copy to bus-master buffers ...
6146 * ucode filename and max sizes are card-specific. */
6147
6148 if (!priv->ucode_code.len) {
6149 ret = iwl4965_read_ucode(priv);
6150 if (ret) {
6151 IWL_ERROR("Could not read microcode: %d\n", ret);
6152 mutex_unlock(&priv->mutex);
6153 goto out_release_irq;
6154 }
6155 }
6156
Zhu Yie655b9f2008-01-24 02:19:38 -08006157 ret = __iwl4965_up(priv);
Zhu Yi5a669262008-01-14 17:46:18 -08006158
Zhu Yib481de92007-09-25 17:54:57 -07006159 mutex_unlock(&priv->mutex);
Zhu Yi5a669262008-01-14 17:46:18 -08006160
Zhu Yie655b9f2008-01-24 02:19:38 -08006161 if (ret)
6162 goto out_release_irq;
6163
6164 IWL_DEBUG_INFO("Start UP work done.\n");
6165
6166 if (test_bit(STATUS_IN_SUSPEND, &priv->status))
6167 return 0;
6168
Zhu Yi5a669262008-01-14 17:46:18 -08006169 /* Wait for START_ALIVE from ucode. Otherwise callbacks from
6170 * mac80211 will not be run successfully. */
6171 ret = wait_event_interruptible_timeout(priv->wait_command_queue,
6172 test_bit(STATUS_READY, &priv->status),
6173 UCODE_READY_TIMEOUT);
6174 if (!ret) {
6175 if (!test_bit(STATUS_READY, &priv->status)) {
6176 IWL_ERROR("Wait for START_ALIVE timeout after %dms.\n",
6177 jiffies_to_msecs(UCODE_READY_TIMEOUT));
6178 ret = -ETIMEDOUT;
6179 goto out_release_irq;
6180 }
6181 }
6182
Zhu Yie655b9f2008-01-24 02:19:38 -08006183 priv->is_open = 1;
Zhu Yib481de92007-09-25 17:54:57 -07006184 IWL_DEBUG_MAC80211("leave\n");
6185 return 0;
Zhu Yi5a669262008-01-14 17:46:18 -08006186
6187out_release_irq:
6188 free_irq(priv->pci_dev->irq, priv);
6189out_disable_msi:
6190 pci_disable_msi(priv->pci_dev);
Zhu Yie655b9f2008-01-24 02:19:38 -08006191 pci_disable_device(priv->pci_dev);
6192 priv->is_open = 0;
6193 IWL_DEBUG_MAC80211("leave - failed\n");
Zhu Yi5a669262008-01-14 17:46:18 -08006194 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07006195}
6196
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006197static void iwl4965_mac_stop(struct ieee80211_hw *hw)
Zhu Yib481de92007-09-25 17:54:57 -07006198{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006199 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07006200
6201 IWL_DEBUG_MAC80211("enter\n");
Mohamed Abbas948c1712007-10-25 17:15:45 +08006202
Zhu Yie655b9f2008-01-24 02:19:38 -08006203 if (!priv->is_open) {
6204 IWL_DEBUG_MAC80211("leave - skip\n");
6205 return;
6206 }
6207
Zhu Yib481de92007-09-25 17:54:57 -07006208 priv->is_open = 0;
Zhu Yi5a669262008-01-14 17:46:18 -08006209
Tomas Winklerfee12472008-04-03 16:05:21 -07006210 if (iwl_is_ready_rf(priv)) {
Zhu Yie655b9f2008-01-24 02:19:38 -08006211 /* stop mac, cancel any scan request and clear
6212 * RXON_FILTER_ASSOC_MSK BIT
6213 */
Zhu Yi5a669262008-01-14 17:46:18 -08006214 mutex_lock(&priv->mutex);
6215 iwl4965_scan_cancel_timeout(priv, 100);
6216 cancel_delayed_work(&priv->post_associate);
Mohamed Abbasfde35712007-11-29 11:10:15 +08006217 mutex_unlock(&priv->mutex);
Mohamed Abbasfde35712007-11-29 11:10:15 +08006218 }
6219
Zhu Yi5a669262008-01-14 17:46:18 -08006220 iwl4965_down(priv);
6221
6222 flush_workqueue(priv->workqueue);
6223 free_irq(priv->pci_dev->irq, priv);
6224 pci_disable_msi(priv->pci_dev);
6225 pci_save_state(priv->pci_dev);
6226 pci_disable_device(priv->pci_dev);
Mohamed Abbas948c1712007-10-25 17:15:45 +08006227
Zhu Yib481de92007-09-25 17:54:57 -07006228 IWL_DEBUG_MAC80211("leave\n");
Zhu Yib481de92007-09-25 17:54:57 -07006229}
6230
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006231static int iwl4965_mac_tx(struct ieee80211_hw *hw, struct sk_buff *skb,
Zhu Yib481de92007-09-25 17:54:57 -07006232 struct ieee80211_tx_control *ctl)
6233{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006234 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07006235
6236 IWL_DEBUG_MAC80211("enter\n");
6237
6238 if (priv->iw_mode == IEEE80211_IF_TYPE_MNTR) {
6239 IWL_DEBUG_MAC80211("leave - monitor\n");
6240 return -1;
6241 }
6242
6243 IWL_DEBUG_TX("dev->xmit(%d bytes) at rate 0x%02x\n", skb->len,
Johannes Berg8318d782008-01-24 19:38:38 +01006244 ctl->tx_rate->bitrate);
Zhu Yib481de92007-09-25 17:54:57 -07006245
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006246 if (iwl4965_tx_skb(priv, skb, ctl))
Zhu Yib481de92007-09-25 17:54:57 -07006247 dev_kfree_skb_any(skb);
6248
6249 IWL_DEBUG_MAC80211("leave\n");
6250 return 0;
6251}
6252
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006253static int iwl4965_mac_add_interface(struct ieee80211_hw *hw,
Zhu Yib481de92007-09-25 17:54:57 -07006254 struct ieee80211_if_init_conf *conf)
6255{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006256 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07006257 unsigned long flags;
Joe Perches0795af52007-10-03 17:59:30 -07006258 DECLARE_MAC_BUF(mac);
Zhu Yib481de92007-09-25 17:54:57 -07006259
Johannes Berg32bfd352007-12-19 01:31:26 +01006260 IWL_DEBUG_MAC80211("enter: type %d\n", conf->type);
Zhu Yib481de92007-09-25 17:54:57 -07006261
Johannes Berg32bfd352007-12-19 01:31:26 +01006262 if (priv->vif) {
6263 IWL_DEBUG_MAC80211("leave - vif != NULL\n");
Reinette Chatre75849d22008-01-23 10:15:17 -08006264 return -EOPNOTSUPP;
Zhu Yib481de92007-09-25 17:54:57 -07006265 }
6266
6267 spin_lock_irqsave(&priv->lock, flags);
Johannes Berg32bfd352007-12-19 01:31:26 +01006268 priv->vif = conf->vif;
Zhu Yib481de92007-09-25 17:54:57 -07006269
6270 spin_unlock_irqrestore(&priv->lock, flags);
6271
6272 mutex_lock(&priv->mutex);
Tomas Winkler864792e2007-11-27 21:00:52 +02006273
6274 if (conf->mac_addr) {
6275 IWL_DEBUG_MAC80211("Set %s\n", print_mac(mac, conf->mac_addr));
6276 memcpy(priv->mac_addr, conf->mac_addr, ETH_ALEN);
6277 }
Zhu Yib481de92007-09-25 17:54:57 -07006278
Tomas Winklerfee12472008-04-03 16:05:21 -07006279 if (iwl_is_ready(priv))
Zhu Yi5a669262008-01-14 17:46:18 -08006280 iwl4965_set_mode(priv, conf->type);
6281
Zhu Yib481de92007-09-25 17:54:57 -07006282 mutex_unlock(&priv->mutex);
6283
Zhu Yi5a669262008-01-14 17:46:18 -08006284 IWL_DEBUG_MAC80211("leave\n");
Zhu Yib481de92007-09-25 17:54:57 -07006285 return 0;
6286}
6287
6288/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006289 * iwl4965_mac_config - mac80211 config callback
Zhu Yib481de92007-09-25 17:54:57 -07006290 *
6291 * We ignore conf->flags & IEEE80211_CONF_SHORT_SLOT_TIME since it seems to
6292 * be set inappropriately and the driver currently sets the hardware up to
6293 * use it whenever needed.
6294 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006295static int iwl4965_mac_config(struct ieee80211_hw *hw, struct ieee80211_conf *conf)
Zhu Yib481de92007-09-25 17:54:57 -07006296{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006297 struct iwl_priv *priv = hw->priv;
Assaf Kraussbf85ea42008-03-14 10:38:49 -07006298 const struct iwl_channel_info *ch_info;
Zhu Yib481de92007-09-25 17:54:57 -07006299 unsigned long flags;
Zhu Yi76bb77e2007-11-22 10:53:22 +08006300 int ret = 0;
Zhu Yib481de92007-09-25 17:54:57 -07006301
6302 mutex_lock(&priv->mutex);
Johannes Berg8318d782008-01-24 19:38:38 +01006303 IWL_DEBUG_MAC80211("enter to channel %d\n", conf->channel->hw_value);
Zhu Yib481de92007-09-25 17:54:57 -07006304
Zhu Yi12342c42007-12-20 11:27:32 +08006305 priv->add_radiotap = !!(conf->flags & IEEE80211_CONF_RADIOTAP);
6306
Tomas Winklerfee12472008-04-03 16:05:21 -07006307 if (!iwl_is_ready(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07006308 IWL_DEBUG_MAC80211("leave - not ready\n");
Zhu Yi76bb77e2007-11-22 10:53:22 +08006309 ret = -EIO;
6310 goto out;
Zhu Yib481de92007-09-25 17:54:57 -07006311 }
6312
Assaf Krauss1ea87392008-03-18 14:57:50 -07006313 if (unlikely(!priv->cfg->mod_params->disable_hw_scan &&
Zhu Yib481de92007-09-25 17:54:57 -07006314 test_bit(STATUS_SCANNING, &priv->status))) {
Zhu Yia0646472007-12-20 14:10:01 +08006315 IWL_DEBUG_MAC80211("leave - scanning\n");
6316 set_bit(STATUS_CONF_PENDING, &priv->status);
Zhu Yib481de92007-09-25 17:54:57 -07006317 mutex_unlock(&priv->mutex);
Zhu Yia0646472007-12-20 14:10:01 +08006318 return 0;
Zhu Yib481de92007-09-25 17:54:57 -07006319 }
6320
6321 spin_lock_irqsave(&priv->lock, flags);
6322
Assaf Krauss8622e702008-03-21 13:53:43 -07006323 ch_info = iwl_get_channel_info(priv, conf->channel->band,
Johannes Berg8318d782008-01-24 19:38:38 +01006324 ieee80211_frequency_to_channel(conf->channel->center_freq));
Zhu Yib481de92007-09-25 17:54:57 -07006325 if (!is_channel_valid(ch_info)) {
Zhu Yib481de92007-09-25 17:54:57 -07006326 IWL_DEBUG_MAC80211("leave - invalid channel\n");
6327 spin_unlock_irqrestore(&priv->lock, flags);
Zhu Yi76bb77e2007-11-22 10:53:22 +08006328 ret = -EINVAL;
6329 goto out;
Zhu Yib481de92007-09-25 17:54:57 -07006330 }
6331
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08006332#ifdef CONFIG_IWL4965_HT
Tomas Winkler78330fd2008-02-06 02:37:18 +02006333 /* if we are switching from ht to 2.4 clear flags
Zhu Yib481de92007-09-25 17:54:57 -07006334 * from any ht related info since 2.4 does not
6335 * support ht */
Tomas Winkler78330fd2008-02-06 02:37:18 +02006336 if ((le16_to_cpu(priv->staging_rxon.channel) != conf->channel->hw_value)
Zhu Yib481de92007-09-25 17:54:57 -07006337#ifdef IEEE80211_CONF_CHANNEL_SWITCH
6338 && !(conf->flags & IEEE80211_CONF_CHANNEL_SWITCH)
6339#endif
6340 )
6341 priv->staging_rxon.flags = 0;
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08006342#endif /* CONFIG_IWL4965_HT */
Zhu Yib481de92007-09-25 17:54:57 -07006343
Assaf Kraussbf85ea42008-03-14 10:38:49 -07006344 iwlcore_set_rxon_channel(priv, conf->channel->band,
Johannes Berg8318d782008-01-24 19:38:38 +01006345 ieee80211_frequency_to_channel(conf->channel->center_freq));
Zhu Yib481de92007-09-25 17:54:57 -07006346
Johannes Berg8318d782008-01-24 19:38:38 +01006347 iwl4965_set_flags_for_phymode(priv, conf->channel->band);
Zhu Yib481de92007-09-25 17:54:57 -07006348
6349 /* The list of supported rates and rate mask can be different
Johannes Berg8318d782008-01-24 19:38:38 +01006350 * for each band; since the band may have changed, reset
Zhu Yib481de92007-09-25 17:54:57 -07006351 * the rate mask to what mac80211 lists */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006352 iwl4965_set_rate(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006353
6354 spin_unlock_irqrestore(&priv->lock, flags);
6355
6356#ifdef IEEE80211_CONF_CHANNEL_SWITCH
6357 if (conf->flags & IEEE80211_CONF_CHANNEL_SWITCH) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006358 iwl4965_hw_channel_switch(priv, conf->channel);
Zhu Yi76bb77e2007-11-22 10:53:22 +08006359 goto out;
Zhu Yib481de92007-09-25 17:54:57 -07006360 }
6361#endif
6362
Mohamed Abbasad97edd2008-03-28 16:21:06 -07006363 if (priv->cfg->ops->lib->radio_kill_sw)
6364 priv->cfg->ops->lib->radio_kill_sw(priv, !conf->radio_enabled);
Zhu Yib481de92007-09-25 17:54:57 -07006365
6366 if (!conf->radio_enabled) {
6367 IWL_DEBUG_MAC80211("leave - radio disabled\n");
Zhu Yi76bb77e2007-11-22 10:53:22 +08006368 goto out;
Zhu Yib481de92007-09-25 17:54:57 -07006369 }
6370
Tomas Winklerfee12472008-04-03 16:05:21 -07006371 if (iwl_is_rfkill(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07006372 IWL_DEBUG_MAC80211("leave - RF kill\n");
Zhu Yi76bb77e2007-11-22 10:53:22 +08006373 ret = -EIO;
6374 goto out;
Zhu Yib481de92007-09-25 17:54:57 -07006375 }
6376
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006377 iwl4965_set_rate(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006378
6379 if (memcmp(&priv->active_rxon,
6380 &priv->staging_rxon, sizeof(priv->staging_rxon)))
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006381 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006382 else
6383 IWL_DEBUG_INFO("No re-sending same RXON configuration.\n");
6384
6385 IWL_DEBUG_MAC80211("leave\n");
6386
Zhu Yia0646472007-12-20 14:10:01 +08006387out:
6388 clear_bit(STATUS_CONF_PENDING, &priv->status);
Zhu Yi5a669262008-01-14 17:46:18 -08006389 mutex_unlock(&priv->mutex);
Zhu Yi76bb77e2007-11-22 10:53:22 +08006390 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07006391}
6392
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006393static void iwl4965_config_ap(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07006394{
Tomas Winkler857485c2008-03-21 13:53:44 -07006395 int ret = 0;
Zhu Yib481de92007-09-25 17:54:57 -07006396
Maarten Lankhorstd986bcd2008-01-23 10:15:16 -08006397 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
Zhu Yib481de92007-09-25 17:54:57 -07006398 return;
6399
6400 /* The following should be done only at AP bring up */
6401 if ((priv->active_rxon.filter_flags & RXON_FILTER_ASSOC_MSK) == 0) {
6402
6403 /* RXON - unassoc (to set timing command) */
6404 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006405 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006406
6407 /* RXON Timing */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006408 memset(&priv->rxon_timing, 0, sizeof(struct iwl4965_rxon_time_cmd));
6409 iwl4965_setup_rxon_timing(priv);
Tomas Winkler857485c2008-03-21 13:53:44 -07006410 ret = iwl_send_cmd_pdu(priv, REPLY_RXON_TIMING,
Zhu Yib481de92007-09-25 17:54:57 -07006411 sizeof(priv->rxon_timing), &priv->rxon_timing);
Tomas Winkler857485c2008-03-21 13:53:44 -07006412 if (ret)
Zhu Yib481de92007-09-25 17:54:57 -07006413 IWL_WARNING("REPLY_RXON_TIMING failed - "
6414 "Attempting to continue.\n");
6415
6416 iwl4965_set_rxon_chain(priv);
6417
6418 /* FIXME: what should be the assoc_id for AP? */
6419 priv->staging_rxon.assoc_id = cpu_to_le16(priv->assoc_id);
6420 if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_PREAMBLE)
6421 priv->staging_rxon.flags |=
6422 RXON_FLG_SHORT_PREAMBLE_MSK;
6423 else
6424 priv->staging_rxon.flags &=
6425 ~RXON_FLG_SHORT_PREAMBLE_MSK;
6426
6427 if (priv->staging_rxon.flags & RXON_FLG_BAND_24G_MSK) {
6428 if (priv->assoc_capability &
6429 WLAN_CAPABILITY_SHORT_SLOT_TIME)
6430 priv->staging_rxon.flags |=
6431 RXON_FLG_SHORT_SLOT_MSK;
6432 else
6433 priv->staging_rxon.flags &=
6434 ~RXON_FLG_SHORT_SLOT_MSK;
6435
6436 if (priv->iw_mode == IEEE80211_IF_TYPE_IBSS)
6437 priv->staging_rxon.flags &=
6438 ~RXON_FLG_SHORT_SLOT_MSK;
6439 }
6440 /* restore RXON assoc */
6441 priv->staging_rxon.filter_flags |= RXON_FILTER_ASSOC_MSK;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006442 iwl4965_commit_rxon(priv);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006443 iwl4965_activate_qos(priv, 1);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006444 iwl4965_rxon_add_station(priv, iwl4965_broadcast_addr, 0);
Zhu Yie1493de2007-09-27 11:27:32 +08006445 }
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006446 iwl4965_send_beacon_cmd(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006447
6448 /* FIXME - we need to add code here to detect a totally new
6449 * configuration, reset the AP, unassoc, rxon timing, assoc,
6450 * clear sta table, add BCAST sta... */
6451}
6452
Johannes Berg32bfd352007-12-19 01:31:26 +01006453static int iwl4965_mac_config_interface(struct ieee80211_hw *hw,
6454 struct ieee80211_vif *vif,
Zhu Yib481de92007-09-25 17:54:57 -07006455 struct ieee80211_if_conf *conf)
6456{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006457 struct iwl_priv *priv = hw->priv;
Joe Perches0795af52007-10-03 17:59:30 -07006458 DECLARE_MAC_BUF(mac);
Zhu Yib481de92007-09-25 17:54:57 -07006459 unsigned long flags;
6460 int rc;
6461
6462 if (conf == NULL)
6463 return -EIO;
6464
Emmanuel Grumbachb716bb92008-03-04 18:09:32 -08006465 if (priv->vif != vif) {
6466 IWL_DEBUG_MAC80211("leave - priv->vif != vif\n");
Emmanuel Grumbachb716bb92008-03-04 18:09:32 -08006467 return 0;
6468 }
6469
Zhu Yib481de92007-09-25 17:54:57 -07006470 if ((priv->iw_mode == IEEE80211_IF_TYPE_AP) &&
6471 (!conf->beacon || !conf->ssid_len)) {
6472 IWL_DEBUG_MAC80211
6473 ("Leaving in AP mode because HostAPD is not ready.\n");
6474 return 0;
6475 }
6476
Tomas Winklerfee12472008-04-03 16:05:21 -07006477 if (!iwl_is_alive(priv))
Zhu Yi5a669262008-01-14 17:46:18 -08006478 return -EAGAIN;
6479
Zhu Yib481de92007-09-25 17:54:57 -07006480 mutex_lock(&priv->mutex);
6481
Zhu Yib481de92007-09-25 17:54:57 -07006482 if (conf->bssid)
Joe Perches0795af52007-10-03 17:59:30 -07006483 IWL_DEBUG_MAC80211("bssid: %s\n",
6484 print_mac(mac, conf->bssid));
Zhu Yib481de92007-09-25 17:54:57 -07006485
Johannes Berg4150c572007-09-17 01:29:23 -04006486/*
6487 * very dubious code was here; the probe filtering flag is never set:
6488 *
Zhu Yib481de92007-09-25 17:54:57 -07006489 if (unlikely(test_bit(STATUS_SCANNING, &priv->status)) &&
6490 !(priv->hw->flags & IEEE80211_HW_NO_PROBE_FILTERING)) {
Johannes Berg4150c572007-09-17 01:29:23 -04006491 */
Zhu Yib481de92007-09-25 17:54:57 -07006492
6493 if (priv->iw_mode == IEEE80211_IF_TYPE_AP) {
6494 if (!conf->bssid) {
6495 conf->bssid = priv->mac_addr;
6496 memcpy(priv->bssid, priv->mac_addr, ETH_ALEN);
Joe Perches0795af52007-10-03 17:59:30 -07006497 IWL_DEBUG_MAC80211("bssid was set to: %s\n",
6498 print_mac(mac, conf->bssid));
Zhu Yib481de92007-09-25 17:54:57 -07006499 }
6500 if (priv->ibss_beacon)
6501 dev_kfree_skb(priv->ibss_beacon);
6502
6503 priv->ibss_beacon = conf->beacon;
6504 }
6505
Tomas Winklerfee12472008-04-03 16:05:21 -07006506 if (iwl_is_rfkill(priv))
Mohamed Abbasfde35712007-11-29 11:10:15 +08006507 goto done;
6508
Zhu Yib481de92007-09-25 17:54:57 -07006509 if (conf->bssid && !is_zero_ether_addr(conf->bssid) &&
6510 !is_multicast_ether_addr(conf->bssid)) {
6511 /* If there is currently a HW scan going on in the background
6512 * then we need to cancel it else the RXON below will fail. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006513 if (iwl4965_scan_cancel_timeout(priv, 100)) {
Zhu Yib481de92007-09-25 17:54:57 -07006514 IWL_WARNING("Aborted scan still in progress "
6515 "after 100ms\n");
6516 IWL_DEBUG_MAC80211("leaving - scan abort failed.\n");
6517 mutex_unlock(&priv->mutex);
6518 return -EAGAIN;
6519 }
6520 memcpy(priv->staging_rxon.bssid_addr, conf->bssid, ETH_ALEN);
6521
6522 /* TODO: Audit driver for usage of these members and see
6523 * if mac80211 deprecates them (priv->bssid looks like it
6524 * shouldn't be there, but I haven't scanned the IBSS code
6525 * to verify) - jpk */
6526 memcpy(priv->bssid, conf->bssid, ETH_ALEN);
6527
6528 if (priv->iw_mode == IEEE80211_IF_TYPE_AP)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006529 iwl4965_config_ap(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006530 else {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006531 rc = iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006532 if ((priv->iw_mode == IEEE80211_IF_TYPE_STA) && rc)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006533 iwl4965_rxon_add_station(
Zhu Yib481de92007-09-25 17:54:57 -07006534 priv, priv->active_rxon.bssid_addr, 1);
6535 }
6536
6537 } else {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006538 iwl4965_scan_cancel_timeout(priv, 100);
Zhu Yib481de92007-09-25 17:54:57 -07006539 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006540 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006541 }
6542
Mohamed Abbasfde35712007-11-29 11:10:15 +08006543 done:
Zhu Yib481de92007-09-25 17:54:57 -07006544 spin_lock_irqsave(&priv->lock, flags);
6545 if (!conf->ssid_len)
6546 memset(priv->essid, 0, IW_ESSID_MAX_SIZE);
6547 else
6548 memcpy(priv->essid, conf->ssid, conf->ssid_len);
6549
6550 priv->essid_len = conf->ssid_len;
6551 spin_unlock_irqrestore(&priv->lock, flags);
6552
6553 IWL_DEBUG_MAC80211("leave\n");
6554 mutex_unlock(&priv->mutex);
6555
6556 return 0;
6557}
6558
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006559static void iwl4965_configure_filter(struct ieee80211_hw *hw,
Johannes Berg4150c572007-09-17 01:29:23 -04006560 unsigned int changed_flags,
6561 unsigned int *total_flags,
6562 int mc_count, struct dev_addr_list *mc_list)
6563{
6564 /*
6565 * XXX: dummy
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006566 * see also iwl4965_connection_init_rx_config
Johannes Berg4150c572007-09-17 01:29:23 -04006567 */
6568 *total_flags = 0;
6569}
6570
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006571static void iwl4965_mac_remove_interface(struct ieee80211_hw *hw,
Zhu Yib481de92007-09-25 17:54:57 -07006572 struct ieee80211_if_init_conf *conf)
6573{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006574 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07006575
6576 IWL_DEBUG_MAC80211("enter\n");
6577
6578 mutex_lock(&priv->mutex);
Mohamed Abbas948c1712007-10-25 17:15:45 +08006579
Tomas Winklerfee12472008-04-03 16:05:21 -07006580 if (iwl_is_ready_rf(priv)) {
Mohamed Abbasfde35712007-11-29 11:10:15 +08006581 iwl4965_scan_cancel_timeout(priv, 100);
6582 cancel_delayed_work(&priv->post_associate);
6583 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
6584 iwl4965_commit_rxon(priv);
6585 }
Johannes Berg32bfd352007-12-19 01:31:26 +01006586 if (priv->vif == conf->vif) {
6587 priv->vif = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07006588 memset(priv->bssid, 0, ETH_ALEN);
6589 memset(priv->essid, 0, IW_ESSID_MAX_SIZE);
6590 priv->essid_len = 0;
6591 }
6592 mutex_unlock(&priv->mutex);
6593
6594 IWL_DEBUG_MAC80211("leave\n");
6595
6596}
Johannes Berg471b3ef2007-12-28 14:32:58 +01006597
Tomas Winkler98952d52008-03-28 16:33:33 -07006598
6599#ifdef CONFIG_IWL4965_HT
6600static void iwl4965_ht_conf(struct iwl_priv *priv,
6601 struct ieee80211_bss_conf *bss_conf)
6602{
6603 struct ieee80211_ht_info *ht_conf = bss_conf->ht_conf;
6604 struct ieee80211_ht_bss_info *ht_bss_conf = bss_conf->ht_bss_conf;
6605 struct iwl_ht_info *iwl_conf = &priv->current_ht_config;
6606
6607 IWL_DEBUG_MAC80211("enter: \n");
6608
6609 iwl_conf->is_ht = bss_conf->assoc_ht;
6610
6611 if (!iwl_conf->is_ht)
6612 return;
6613
6614 priv->ps_mode = (u8)((ht_conf->cap & IEEE80211_HT_CAP_MIMO_PS) >> 2);
6615
6616 if (ht_conf->cap & IEEE80211_HT_CAP_SGI_20)
6617 iwl_conf->sgf |= 0x1;
6618 if (ht_conf->cap & IEEE80211_HT_CAP_SGI_40)
6619 iwl_conf->sgf |= 0x2;
6620
6621 iwl_conf->is_green_field = !!(ht_conf->cap & IEEE80211_HT_CAP_GRN_FLD);
6622 iwl_conf->max_amsdu_size =
6623 !!(ht_conf->cap & IEEE80211_HT_CAP_MAX_AMSDU);
6624
6625 iwl_conf->supported_chan_width =
6626 !!(ht_conf->cap & IEEE80211_HT_CAP_SUP_WIDTH);
6627 iwl_conf->extension_chan_offset =
6628 ht_bss_conf->bss_cap & IEEE80211_HT_IE_CHA_SEC_OFFSET;
6629 /* If no above or below channel supplied disable FAT channel */
6630 if (iwl_conf->extension_chan_offset != IWL_EXT_CHANNEL_OFFSET_ABOVE &&
6631 iwl_conf->extension_chan_offset != IWL_EXT_CHANNEL_OFFSET_BELOW)
6632 iwl_conf->supported_chan_width = 0;
6633
6634 iwl_conf->tx_mimo_ps_mode =
6635 (u8)((ht_conf->cap & IEEE80211_HT_CAP_MIMO_PS) >> 2);
6636 memcpy(iwl_conf->supp_mcs_set, ht_conf->supp_mcs_set, 16);
6637
6638 iwl_conf->control_channel = ht_bss_conf->primary_channel;
6639 iwl_conf->tx_chan_width =
6640 !!(ht_bss_conf->bss_cap & IEEE80211_HT_IE_CHA_WIDTH);
6641 iwl_conf->ht_protection =
6642 ht_bss_conf->bss_op_mode & IEEE80211_HT_IE_HT_PROTECTION;
6643 iwl_conf->non_GF_STA_present =
6644 !!(ht_bss_conf->bss_op_mode & IEEE80211_HT_IE_NON_GF_STA_PRSNT);
6645
6646 IWL_DEBUG_MAC80211("control channel %d\n", iwl_conf->control_channel);
6647 IWL_DEBUG_MAC80211("leave\n");
6648}
6649#else
6650static inline void iwl4965_ht_conf(struct iwl_priv *priv,
6651 struct ieee80211_bss_conf *bss_conf)
6652{
6653}
6654#endif
6655
Tomas Winkler3109ece2008-03-28 16:33:35 -07006656#define IWL_DELAY_NEXT_SCAN_AFTER_ASSOC (HZ*6)
Johannes Berg471b3ef2007-12-28 14:32:58 +01006657static void iwl4965_bss_info_changed(struct ieee80211_hw *hw,
6658 struct ieee80211_vif *vif,
6659 struct ieee80211_bss_conf *bss_conf,
6660 u32 changes)
Tomas Winkler220173b2007-10-16 00:50:25 +02006661{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006662 struct iwl_priv *priv = hw->priv;
Tomas Winkler220173b2007-10-16 00:50:25 +02006663
Tomas Winkler3109ece2008-03-28 16:33:35 -07006664 IWL_DEBUG_MAC80211("changes = 0x%X\n", changes);
6665
Johannes Berg471b3ef2007-12-28 14:32:58 +01006666 if (changes & BSS_CHANGED_ERP_PREAMBLE) {
Tomas Winkler3109ece2008-03-28 16:33:35 -07006667 IWL_DEBUG_MAC80211("ERP_PREAMBLE %d\n",
6668 bss_conf->use_short_preamble);
Johannes Berg471b3ef2007-12-28 14:32:58 +01006669 if (bss_conf->use_short_preamble)
Tomas Winkler220173b2007-10-16 00:50:25 +02006670 priv->staging_rxon.flags |= RXON_FLG_SHORT_PREAMBLE_MSK;
6671 else
6672 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_PREAMBLE_MSK;
6673 }
6674
Johannes Berg471b3ef2007-12-28 14:32:58 +01006675 if (changes & BSS_CHANGED_ERP_CTS_PROT) {
Tomas Winkler3109ece2008-03-28 16:33:35 -07006676 IWL_DEBUG_MAC80211("ERP_CTS %d\n", bss_conf->use_cts_prot);
Johannes Berg8318d782008-01-24 19:38:38 +01006677 if (bss_conf->use_cts_prot && (priv->band != IEEE80211_BAND_5GHZ))
Tomas Winkler220173b2007-10-16 00:50:25 +02006678 priv->staging_rxon.flags |= RXON_FLG_TGG_PROTECT_MSK;
6679 else
6680 priv->staging_rxon.flags &= ~RXON_FLG_TGG_PROTECT_MSK;
6681 }
6682
Tomas Winkler98952d52008-03-28 16:33:33 -07006683 if (changes & BSS_CHANGED_HT) {
Tomas Winkler3109ece2008-03-28 16:33:35 -07006684 IWL_DEBUG_MAC80211("HT %d\n", bss_conf->assoc_ht);
Tomas Winkler98952d52008-03-28 16:33:33 -07006685 iwl4965_ht_conf(priv, bss_conf);
6686 iwl4965_set_rxon_chain(priv);
6687 }
6688
Johannes Berg471b3ef2007-12-28 14:32:58 +01006689 if (changes & BSS_CHANGED_ASSOC) {
Tomas Winkler3109ece2008-03-28 16:33:35 -07006690 IWL_DEBUG_MAC80211("ASSOC %d\n", bss_conf->assoc);
Reinette Chatre508e32e2008-04-14 21:16:13 -07006691 /* This should never happen as this function should
6692 * never be called from interrupt context. */
6693 if (WARN_ON_ONCE(in_interrupt()))
6694 return;
Tomas Winkler3109ece2008-03-28 16:33:35 -07006695 if (bss_conf->assoc) {
6696 priv->assoc_id = bss_conf->aid;
6697 priv->beacon_int = bss_conf->beacon_int;
6698 priv->timestamp = bss_conf->timestamp;
6699 priv->assoc_capability = bss_conf->assoc_capability;
6700 priv->next_scan_jiffies = jiffies +
6701 IWL_DELAY_NEXT_SCAN_AFTER_ASSOC;
Reinette Chatre508e32e2008-04-14 21:16:13 -07006702 mutex_lock(&priv->mutex);
6703 iwl4965_post_associate(priv);
6704 mutex_unlock(&priv->mutex);
Tomas Winkler3109ece2008-03-28 16:33:35 -07006705 } else {
6706 priv->assoc_id = 0;
6707 IWL_DEBUG_MAC80211("DISASSOC %d\n", bss_conf->assoc);
6708 }
6709 } else if (changes && iwl_is_associated(priv) && priv->assoc_id) {
6710 IWL_DEBUG_MAC80211("Associated Changes %d\n", changes);
Tomas Winkler7e8c5192008-04-15 16:01:43 -07006711 iwl_send_rxon_assoc(priv);
Johannes Berg471b3ef2007-12-28 14:32:58 +01006712 }
6713
Tomas Winkler220173b2007-10-16 00:50:25 +02006714}
Zhu Yib481de92007-09-25 17:54:57 -07006715
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006716static int iwl4965_mac_hw_scan(struct ieee80211_hw *hw, u8 *ssid, size_t len)
Zhu Yib481de92007-09-25 17:54:57 -07006717{
6718 int rc = 0;
6719 unsigned long flags;
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006720 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07006721
6722 IWL_DEBUG_MAC80211("enter\n");
6723
mabbas052c4b92007-10-25 17:15:43 +08006724 mutex_lock(&priv->mutex);
Zhu Yib481de92007-09-25 17:54:57 -07006725 spin_lock_irqsave(&priv->lock, flags);
6726
Tomas Winklerfee12472008-04-03 16:05:21 -07006727 if (!iwl_is_ready_rf(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07006728 rc = -EIO;
6729 IWL_DEBUG_MAC80211("leave - not ready or exit pending\n");
6730 goto out_unlock;
6731 }
6732
6733 if (priv->iw_mode == IEEE80211_IF_TYPE_AP) { /* APs don't scan */
6734 rc = -EIO;
6735 IWL_ERROR("ERROR: APs don't scan\n");
6736 goto out_unlock;
6737 }
6738
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08006739 /* we don't schedule scan within next_scan_jiffies period */
6740 if (priv->next_scan_jiffies &&
6741 time_after(priv->next_scan_jiffies, jiffies)) {
6742 rc = -EAGAIN;
6743 goto out_unlock;
6744 }
Zhu Yib481de92007-09-25 17:54:57 -07006745 /* if we just finished scan ask for delay */
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08006746 if (priv->last_scan_jiffies && time_after(priv->last_scan_jiffies +
6747 IWL_DELAY_NEXT_SCAN, jiffies)) {
Zhu Yib481de92007-09-25 17:54:57 -07006748 rc = -EAGAIN;
6749 goto out_unlock;
6750 }
6751 if (len) {
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08006752 IWL_DEBUG_SCAN("direct scan for %s [%d]\n ",
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006753 iwl4965_escape_essid(ssid, len), (int)len);
Zhu Yib481de92007-09-25 17:54:57 -07006754
6755 priv->one_direct_scan = 1;
6756 priv->direct_ssid_len = (u8)
6757 min((u8) len, (u8) IW_ESSID_MAX_SIZE);
6758 memcpy(priv->direct_ssid, ssid, priv->direct_ssid_len);
Mohamed Abbas948c1712007-10-25 17:15:45 +08006759 } else
6760 priv->one_direct_scan = 0;
Zhu Yib481de92007-09-25 17:54:57 -07006761
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006762 rc = iwl4965_scan_initiate(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006763
6764 IWL_DEBUG_MAC80211("leave\n");
6765
6766out_unlock:
6767 spin_unlock_irqrestore(&priv->lock, flags);
mabbas052c4b92007-10-25 17:15:43 +08006768 mutex_unlock(&priv->mutex);
Zhu Yib481de92007-09-25 17:54:57 -07006769
6770 return rc;
6771}
6772
Emmanuel Grumbachab885f82008-03-20 15:06:43 +02006773static void iwl4965_mac_update_tkip_key(struct ieee80211_hw *hw,
6774 struct ieee80211_key_conf *keyconf, const u8 *addr,
6775 u32 iv32, u16 *phase1key)
6776{
6777 struct iwl_priv *priv = hw->priv;
6778 u8 sta_id = IWL_INVALID_STATION;
6779 unsigned long flags;
6780 __le16 key_flags = 0;
6781 int i;
6782 DECLARE_MAC_BUF(mac);
6783
6784 IWL_DEBUG_MAC80211("enter\n");
6785
6786 sta_id = iwl4965_hw_find_station(priv, addr);
6787 if (sta_id == IWL_INVALID_STATION) {
6788 IWL_DEBUG_MAC80211("leave - %s not in station map.\n",
6789 print_mac(mac, addr));
6790 return;
6791 }
6792
6793 iwl4965_scan_cancel_timeout(priv, 100);
6794
6795 key_flags |= (STA_KEY_FLG_TKIP | STA_KEY_FLG_MAP_KEY_MSK);
6796 key_flags |= cpu_to_le16(keyconf->keyidx << STA_KEY_FLG_KEYID_POS);
6797 key_flags &= ~STA_KEY_FLG_INVALID;
6798
Tomas Winkler5425e492008-04-15 16:01:38 -07006799 if (sta_id == priv->hw_params.bcast_sta_id)
Emmanuel Grumbachab885f82008-03-20 15:06:43 +02006800 key_flags |= STA_KEY_MULTICAST_MSK;
6801
6802 spin_lock_irqsave(&priv->sta_lock, flags);
6803
Emmanuel Grumbachab885f82008-03-20 15:06:43 +02006804 priv->stations[sta_id].sta.key.key_flags = key_flags;
6805 priv->stations[sta_id].sta.key.tkip_rx_tsc_byte2 = (u8) iv32;
6806
6807 for (i = 0; i < 5; i++)
6808 priv->stations[sta_id].sta.key.tkip_rx_ttak[i] =
6809 cpu_to_le16(phase1key[i]);
6810
6811 priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
6812 priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
6813
6814 iwl4965_send_add_station(priv, &priv->stations[sta_id].sta, CMD_ASYNC);
6815
6816 spin_unlock_irqrestore(&priv->sta_lock, flags);
6817
6818 IWL_DEBUG_MAC80211("leave\n");
6819}
6820
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006821static int iwl4965_mac_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
Zhu Yib481de92007-09-25 17:54:57 -07006822 const u8 *local_addr, const u8 *addr,
6823 struct ieee80211_key_conf *key)
6824{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006825 struct iwl_priv *priv = hw->priv;
Joe Perches0795af52007-10-03 17:59:30 -07006826 DECLARE_MAC_BUF(mac);
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07006827 int ret = 0;
6828 u8 sta_id = IWL_INVALID_STATION;
Emmanuel Grumbach6974e362008-04-14 21:16:06 -07006829 u8 is_default_wep_key = 0;
Zhu Yib481de92007-09-25 17:54:57 -07006830
6831 IWL_DEBUG_MAC80211("enter\n");
6832
Emmanuel Grumbachfcc76c62008-04-15 16:01:47 -07006833 if (priv->cfg->mod_params->sw_crypto) {
Zhu Yib481de92007-09-25 17:54:57 -07006834 IWL_DEBUG_MAC80211("leave - hwcrypto disabled\n");
6835 return -EOPNOTSUPP;
6836 }
6837
6838 if (is_zero_ether_addr(addr))
6839 /* only support pairwise keys */
6840 return -EOPNOTSUPP;
6841
Emmanuel Grumbach6974e362008-04-14 21:16:06 -07006842 sta_id = iwl4965_hw_find_station(priv, addr);
6843 if (sta_id == IWL_INVALID_STATION) {
6844 IWL_DEBUG_MAC80211("leave - %s not in station map.\n",
6845 print_mac(mac, addr));
6846 return -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07006847
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07006848 }
Zhu Yib481de92007-09-25 17:54:57 -07006849
Emmanuel Grumbach6974e362008-04-14 21:16:06 -07006850 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006851 iwl4965_scan_cancel_timeout(priv, 100);
Emmanuel Grumbach6974e362008-04-14 21:16:06 -07006852 mutex_unlock(&priv->mutex);
6853
6854 /* If we are getting WEP group key and we didn't receive any key mapping
6855 * so far, we are in legacy wep mode (group key only), otherwise we are
6856 * in 1X mode.
6857 * In legacy wep mode, we use another host command to the uCode */
Tomas Winkler5425e492008-04-15 16:01:38 -07006858 if (key->alg == ALG_WEP && sta_id == priv->hw_params.bcast_sta_id &&
Emmanuel Grumbach6974e362008-04-14 21:16:06 -07006859 priv->iw_mode != IEEE80211_IF_TYPE_AP) {
6860 if (cmd == SET_KEY)
6861 is_default_wep_key = !priv->key_mapping_key;
6862 else
6863 is_default_wep_key = priv->default_wep_key;
6864 }
mabbas052c4b92007-10-25 17:15:43 +08006865
Zhu Yib481de92007-09-25 17:54:57 -07006866 switch (cmd) {
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07006867 case SET_KEY:
Emmanuel Grumbach6974e362008-04-14 21:16:06 -07006868 if (is_default_wep_key)
6869 ret = iwl_set_default_wep_key(priv, key);
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07006870 else
Emmanuel Grumbach74805132008-04-14 21:16:09 -07006871 ret = iwl_set_dynamic_key(priv, key, sta_id);
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07006872
6873 IWL_DEBUG_MAC80211("enable hwcrypto key\n");
Zhu Yib481de92007-09-25 17:54:57 -07006874 break;
6875 case DISABLE_KEY:
Emmanuel Grumbach6974e362008-04-14 21:16:06 -07006876 if (is_default_wep_key)
6877 ret = iwl_remove_default_wep_key(priv, key);
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07006878 else
Emmanuel Grumbach74805132008-04-14 21:16:09 -07006879 ret = iwl_remove_dynamic_key(priv, sta_id);
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07006880
6881 IWL_DEBUG_MAC80211("disable hwcrypto key\n");
Zhu Yib481de92007-09-25 17:54:57 -07006882 break;
6883 default:
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07006884 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07006885 }
6886
6887 IWL_DEBUG_MAC80211("leave\n");
Zhu Yib481de92007-09-25 17:54:57 -07006888
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07006889 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07006890}
6891
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006892static int iwl4965_mac_conf_tx(struct ieee80211_hw *hw, int queue,
Zhu Yib481de92007-09-25 17:54:57 -07006893 const struct ieee80211_tx_queue_params *params)
6894{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006895 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07006896 unsigned long flags;
6897 int q;
Zhu Yib481de92007-09-25 17:54:57 -07006898
6899 IWL_DEBUG_MAC80211("enter\n");
6900
Tomas Winklerfee12472008-04-03 16:05:21 -07006901 if (!iwl_is_ready_rf(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07006902 IWL_DEBUG_MAC80211("leave - RF not ready\n");
6903 return -EIO;
6904 }
6905
6906 if (queue >= AC_NUM) {
6907 IWL_DEBUG_MAC80211("leave - queue >= AC_NUM %d\n", queue);
6908 return 0;
6909 }
6910
Zhu Yib481de92007-09-25 17:54:57 -07006911 if (!priv->qos_data.qos_enable) {
6912 priv->qos_data.qos_active = 0;
6913 IWL_DEBUG_MAC80211("leave - qos not enabled\n");
6914 return 0;
6915 }
6916 q = AC_NUM - 1 - queue;
6917
6918 spin_lock_irqsave(&priv->lock, flags);
6919
6920 priv->qos_data.def_qos_parm.ac[q].cw_min = cpu_to_le16(params->cw_min);
6921 priv->qos_data.def_qos_parm.ac[q].cw_max = cpu_to_le16(params->cw_max);
6922 priv->qos_data.def_qos_parm.ac[q].aifsn = params->aifs;
6923 priv->qos_data.def_qos_parm.ac[q].edca_txop =
Johannes Berg3330d7be2008-02-10 16:49:38 +01006924 cpu_to_le16((params->txop * 32));
Zhu Yib481de92007-09-25 17:54:57 -07006925
6926 priv->qos_data.def_qos_parm.ac[q].reserved1 = 0;
6927 priv->qos_data.qos_active = 1;
6928
6929 spin_unlock_irqrestore(&priv->lock, flags);
6930
6931 mutex_lock(&priv->mutex);
6932 if (priv->iw_mode == IEEE80211_IF_TYPE_AP)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006933 iwl4965_activate_qos(priv, 1);
Tomas Winkler3109ece2008-03-28 16:33:35 -07006934 else if (priv->assoc_id && iwl_is_associated(priv))
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006935 iwl4965_activate_qos(priv, 0);
Zhu Yib481de92007-09-25 17:54:57 -07006936
6937 mutex_unlock(&priv->mutex);
6938
Zhu Yib481de92007-09-25 17:54:57 -07006939 IWL_DEBUG_MAC80211("leave\n");
6940 return 0;
6941}
6942
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006943static int iwl4965_mac_get_tx_stats(struct ieee80211_hw *hw,
Zhu Yib481de92007-09-25 17:54:57 -07006944 struct ieee80211_tx_queue_stats *stats)
6945{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006946 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07006947 int i, avail;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006948 struct iwl4965_tx_queue *txq;
6949 struct iwl4965_queue *q;
Zhu Yib481de92007-09-25 17:54:57 -07006950 unsigned long flags;
6951
6952 IWL_DEBUG_MAC80211("enter\n");
6953
Tomas Winklerfee12472008-04-03 16:05:21 -07006954 if (!iwl_is_ready_rf(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07006955 IWL_DEBUG_MAC80211("leave - RF not ready\n");
6956 return -EIO;
6957 }
6958
6959 spin_lock_irqsave(&priv->lock, flags);
6960
6961 for (i = 0; i < AC_NUM; i++) {
6962 txq = &priv->txq[i];
6963 q = &txq->q;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006964 avail = iwl4965_queue_space(q);
Zhu Yib481de92007-09-25 17:54:57 -07006965
6966 stats->data[i].len = q->n_window - avail;
6967 stats->data[i].limit = q->n_window - q->high_mark;
6968 stats->data[i].count = q->n_window;
6969
6970 }
6971 spin_unlock_irqrestore(&priv->lock, flags);
6972
6973 IWL_DEBUG_MAC80211("leave\n");
6974
6975 return 0;
6976}
6977
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006978static int iwl4965_mac_get_stats(struct ieee80211_hw *hw,
Zhu Yib481de92007-09-25 17:54:57 -07006979 struct ieee80211_low_level_stats *stats)
6980{
6981 IWL_DEBUG_MAC80211("enter\n");
6982 IWL_DEBUG_MAC80211("leave\n");
6983
6984 return 0;
6985}
6986
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006987static u64 iwl4965_mac_get_tsf(struct ieee80211_hw *hw)
Zhu Yib481de92007-09-25 17:54:57 -07006988{
6989 IWL_DEBUG_MAC80211("enter\n");
6990 IWL_DEBUG_MAC80211("leave\n");
6991
6992 return 0;
6993}
6994
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006995static void iwl4965_mac_reset_tsf(struct ieee80211_hw *hw)
Zhu Yib481de92007-09-25 17:54:57 -07006996{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006997 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07006998 unsigned long flags;
6999
7000 mutex_lock(&priv->mutex);
7001 IWL_DEBUG_MAC80211("enter\n");
7002
7003 priv->lq_mngr.lq_ready = 0;
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08007004#ifdef CONFIG_IWL4965_HT
Zhu Yib481de92007-09-25 17:54:57 -07007005 spin_lock_irqsave(&priv->lock, flags);
Ron Rindjunskyfd105e72007-11-26 16:14:39 +02007006 memset(&priv->current_ht_config, 0, sizeof(struct iwl_ht_info));
Zhu Yib481de92007-09-25 17:54:57 -07007007 spin_unlock_irqrestore(&priv->lock, flags);
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08007008#endif /* CONFIG_IWL4965_HT */
Zhu Yib481de92007-09-25 17:54:57 -07007009
Assaf Kraussbf85ea42008-03-14 10:38:49 -07007010 iwlcore_reset_qos(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007011
7012 cancel_delayed_work(&priv->post_associate);
7013
7014 spin_lock_irqsave(&priv->lock, flags);
7015 priv->assoc_id = 0;
7016 priv->assoc_capability = 0;
Zhu Yib481de92007-09-25 17:54:57 -07007017 priv->assoc_station_added = 0;
7018
7019 /* new association get rid of ibss beacon skb */
7020 if (priv->ibss_beacon)
7021 dev_kfree_skb(priv->ibss_beacon);
7022
7023 priv->ibss_beacon = NULL;
7024
7025 priv->beacon_int = priv->hw->conf.beacon_int;
Tomas Winkler3109ece2008-03-28 16:33:35 -07007026 priv->timestamp = 0;
Zhu Yib481de92007-09-25 17:54:57 -07007027 if ((priv->iw_mode == IEEE80211_IF_TYPE_STA))
7028 priv->beacon_int = 0;
7029
7030 spin_unlock_irqrestore(&priv->lock, flags);
7031
Tomas Winklerfee12472008-04-03 16:05:21 -07007032 if (!iwl_is_ready_rf(priv)) {
Mohamed Abbasfde35712007-11-29 11:10:15 +08007033 IWL_DEBUG_MAC80211("leave - not ready\n");
7034 mutex_unlock(&priv->mutex);
7035 return;
7036 }
7037
mabbas052c4b92007-10-25 17:15:43 +08007038 /* we are restarting association process
7039 * clear RXON_FILTER_ASSOC_MSK bit
7040 */
7041 if (priv->iw_mode != IEEE80211_IF_TYPE_AP) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007042 iwl4965_scan_cancel_timeout(priv, 100);
mabbas052c4b92007-10-25 17:15:43 +08007043 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007044 iwl4965_commit_rxon(priv);
mabbas052c4b92007-10-25 17:15:43 +08007045 }
7046
Zhu Yib481de92007-09-25 17:54:57 -07007047 /* Per mac80211.h: This is only used in IBSS mode... */
7048 if (priv->iw_mode != IEEE80211_IF_TYPE_IBSS) {
mabbas052c4b92007-10-25 17:15:43 +08007049
Zhu Yib481de92007-09-25 17:54:57 -07007050 IWL_DEBUG_MAC80211("leave - not in IBSS\n");
7051 mutex_unlock(&priv->mutex);
7052 return;
7053 }
7054
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007055 iwl4965_set_rate(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007056
7057 mutex_unlock(&priv->mutex);
7058
7059 IWL_DEBUG_MAC80211("leave\n");
Zhu Yib481de92007-09-25 17:54:57 -07007060}
7061
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007062static int iwl4965_mac_beacon_update(struct ieee80211_hw *hw, struct sk_buff *skb,
Zhu Yib481de92007-09-25 17:54:57 -07007063 struct ieee80211_tx_control *control)
7064{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007065 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07007066 unsigned long flags;
7067
7068 mutex_lock(&priv->mutex);
7069 IWL_DEBUG_MAC80211("enter\n");
7070
Tomas Winklerfee12472008-04-03 16:05:21 -07007071 if (!iwl_is_ready_rf(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07007072 IWL_DEBUG_MAC80211("leave - RF not ready\n");
7073 mutex_unlock(&priv->mutex);
7074 return -EIO;
7075 }
7076
7077 if (priv->iw_mode != IEEE80211_IF_TYPE_IBSS) {
7078 IWL_DEBUG_MAC80211("leave - not IBSS\n");
7079 mutex_unlock(&priv->mutex);
7080 return -EIO;
7081 }
7082
7083 spin_lock_irqsave(&priv->lock, flags);
7084
7085 if (priv->ibss_beacon)
7086 dev_kfree_skb(priv->ibss_beacon);
7087
7088 priv->ibss_beacon = skb;
7089
7090 priv->assoc_id = 0;
7091
7092 IWL_DEBUG_MAC80211("leave\n");
7093 spin_unlock_irqrestore(&priv->lock, flags);
7094
Assaf Kraussbf85ea42008-03-14 10:38:49 -07007095 iwlcore_reset_qos(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007096
7097 queue_work(priv->workqueue, &priv->post_associate.work);
7098
7099 mutex_unlock(&priv->mutex);
7100
7101 return 0;
7102}
7103
Zhu Yib481de92007-09-25 17:54:57 -07007104/*****************************************************************************
7105 *
7106 * sysfs attributes
7107 *
7108 *****************************************************************************/
7109
Tomas Winkler0a6857e2008-03-12 16:58:49 -07007110#ifdef CONFIG_IWLWIFI_DEBUG
Zhu Yib481de92007-09-25 17:54:57 -07007111
7112/*
7113 * The following adds a new attribute to the sysfs representation
7114 * of this device driver (i.e. a new file in /sys/bus/pci/drivers/iwl/)
7115 * used for controlling the debug level.
7116 *
7117 * See the level definitions in iwl for details.
7118 */
7119
7120static ssize_t show_debug_level(struct device_driver *d, char *buf)
7121{
Tomas Winkler0a6857e2008-03-12 16:58:49 -07007122 return sprintf(buf, "0x%08X\n", iwl_debug_level);
Zhu Yib481de92007-09-25 17:54:57 -07007123}
7124static ssize_t store_debug_level(struct device_driver *d,
7125 const char *buf, size_t count)
7126{
7127 char *p = (char *)buf;
7128 u32 val;
7129
7130 val = simple_strtoul(p, &p, 0);
7131 if (p == buf)
7132 printk(KERN_INFO DRV_NAME
7133 ": %s is not in hex or decimal form.\n", buf);
7134 else
Tomas Winkler0a6857e2008-03-12 16:58:49 -07007135 iwl_debug_level = val;
Zhu Yib481de92007-09-25 17:54:57 -07007136
7137 return strnlen(buf, count);
7138}
7139
7140static DRIVER_ATTR(debug_level, S_IWUSR | S_IRUGO,
7141 show_debug_level, store_debug_level);
7142
Tomas Winkler0a6857e2008-03-12 16:58:49 -07007143#endif /* CONFIG_IWLWIFI_DEBUG */
Zhu Yib481de92007-09-25 17:54:57 -07007144
Zhu Yib481de92007-09-25 17:54:57 -07007145
7146static ssize_t show_temperature(struct device *d,
7147 struct device_attribute *attr, char *buf)
7148{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007149 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07007150
Tomas Winklerfee12472008-04-03 16:05:21 -07007151 if (!iwl_is_alive(priv))
Zhu Yib481de92007-09-25 17:54:57 -07007152 return -EAGAIN;
7153
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007154 return sprintf(buf, "%d\n", iwl4965_hw_get_temperature(priv));
Zhu Yib481de92007-09-25 17:54:57 -07007155}
7156
7157static DEVICE_ATTR(temperature, S_IRUGO, show_temperature, NULL);
7158
7159static ssize_t show_rs_window(struct device *d,
7160 struct device_attribute *attr,
7161 char *buf)
7162{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007163 struct iwl_priv *priv = d->driver_data;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007164 return iwl4965_fill_rs_info(priv->hw, buf, IWL_AP_ID);
Zhu Yib481de92007-09-25 17:54:57 -07007165}
7166static DEVICE_ATTR(rs_window, S_IRUGO, show_rs_window, NULL);
7167
7168static ssize_t show_tx_power(struct device *d,
7169 struct device_attribute *attr, char *buf)
7170{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007171 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07007172 return sprintf(buf, "%d\n", priv->user_txpower_limit);
7173}
7174
7175static ssize_t store_tx_power(struct device *d,
7176 struct device_attribute *attr,
7177 const char *buf, size_t count)
7178{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007179 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07007180 char *p = (char *)buf;
7181 u32 val;
7182
7183 val = simple_strtoul(p, &p, 10);
7184 if (p == buf)
7185 printk(KERN_INFO DRV_NAME
7186 ": %s is not in decimal form.\n", buf);
7187 else
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007188 iwl4965_hw_reg_set_txpower(priv, val);
Zhu Yib481de92007-09-25 17:54:57 -07007189
7190 return count;
7191}
7192
7193static DEVICE_ATTR(tx_power, S_IWUSR | S_IRUGO, show_tx_power, store_tx_power);
7194
7195static ssize_t show_flags(struct device *d,
7196 struct device_attribute *attr, char *buf)
7197{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007198 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07007199
7200 return sprintf(buf, "0x%04X\n", priv->active_rxon.flags);
7201}
7202
7203static ssize_t store_flags(struct device *d,
7204 struct device_attribute *attr,
7205 const char *buf, size_t count)
7206{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007207 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07007208 u32 flags = simple_strtoul(buf, NULL, 0);
7209
7210 mutex_lock(&priv->mutex);
7211 if (le32_to_cpu(priv->staging_rxon.flags) != flags) {
7212 /* Cancel any currently running scans... */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007213 if (iwl4965_scan_cancel_timeout(priv, 100))
Zhu Yib481de92007-09-25 17:54:57 -07007214 IWL_WARNING("Could not cancel scan.\n");
7215 else {
7216 IWL_DEBUG_INFO("Committing rxon.flags = 0x%04X\n",
7217 flags);
7218 priv->staging_rxon.flags = cpu_to_le32(flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007219 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007220 }
7221 }
7222 mutex_unlock(&priv->mutex);
7223
7224 return count;
7225}
7226
7227static DEVICE_ATTR(flags, S_IWUSR | S_IRUGO, show_flags, store_flags);
7228
7229static ssize_t show_filter_flags(struct device *d,
7230 struct device_attribute *attr, char *buf)
7231{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007232 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07007233
7234 return sprintf(buf, "0x%04X\n",
7235 le32_to_cpu(priv->active_rxon.filter_flags));
7236}
7237
7238static ssize_t store_filter_flags(struct device *d,
7239 struct device_attribute *attr,
7240 const char *buf, size_t count)
7241{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007242 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07007243 u32 filter_flags = simple_strtoul(buf, NULL, 0);
7244
7245 mutex_lock(&priv->mutex);
7246 if (le32_to_cpu(priv->staging_rxon.filter_flags) != filter_flags) {
7247 /* Cancel any currently running scans... */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007248 if (iwl4965_scan_cancel_timeout(priv, 100))
Zhu Yib481de92007-09-25 17:54:57 -07007249 IWL_WARNING("Could not cancel scan.\n");
7250 else {
7251 IWL_DEBUG_INFO("Committing rxon.filter_flags = "
7252 "0x%04X\n", filter_flags);
7253 priv->staging_rxon.filter_flags =
7254 cpu_to_le32(filter_flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007255 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007256 }
7257 }
7258 mutex_unlock(&priv->mutex);
7259
7260 return count;
7261}
7262
7263static DEVICE_ATTR(filter_flags, S_IWUSR | S_IRUGO, show_filter_flags,
7264 store_filter_flags);
7265
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08007266#ifdef CONFIG_IWL4965_SPECTRUM_MEASUREMENT
Zhu Yib481de92007-09-25 17:54:57 -07007267
7268static ssize_t show_measurement(struct device *d,
7269 struct device_attribute *attr, char *buf)
7270{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007271 struct iwl_priv *priv = dev_get_drvdata(d);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007272 struct iwl4965_spectrum_notification measure_report;
Zhu Yib481de92007-09-25 17:54:57 -07007273 u32 size = sizeof(measure_report), len = 0, ofs = 0;
7274 u8 *data = (u8 *) & measure_report;
7275 unsigned long flags;
7276
7277 spin_lock_irqsave(&priv->lock, flags);
7278 if (!(priv->measurement_status & MEASUREMENT_READY)) {
7279 spin_unlock_irqrestore(&priv->lock, flags);
7280 return 0;
7281 }
7282 memcpy(&measure_report, &priv->measure_report, size);
7283 priv->measurement_status = 0;
7284 spin_unlock_irqrestore(&priv->lock, flags);
7285
7286 while (size && (PAGE_SIZE - len)) {
7287 hex_dump_to_buffer(data + ofs, size, 16, 1, buf + len,
7288 PAGE_SIZE - len, 1);
7289 len = strlen(buf);
7290 if (PAGE_SIZE - len)
7291 buf[len++] = '\n';
7292
7293 ofs += 16;
7294 size -= min(size, 16U);
7295 }
7296
7297 return len;
7298}
7299
7300static ssize_t store_measurement(struct device *d,
7301 struct device_attribute *attr,
7302 const char *buf, size_t count)
7303{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007304 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07007305 struct ieee80211_measurement_params params = {
7306 .channel = le16_to_cpu(priv->active_rxon.channel),
7307 .start_time = cpu_to_le64(priv->last_tsf),
7308 .duration = cpu_to_le16(1),
7309 };
7310 u8 type = IWL_MEASURE_BASIC;
7311 u8 buffer[32];
7312 u8 channel;
7313
7314 if (count) {
7315 char *p = buffer;
7316 strncpy(buffer, buf, min(sizeof(buffer), count));
7317 channel = simple_strtoul(p, NULL, 0);
7318 if (channel)
7319 params.channel = channel;
7320
7321 p = buffer;
7322 while (*p && *p != ' ')
7323 p++;
7324 if (*p)
7325 type = simple_strtoul(p + 1, NULL, 0);
7326 }
7327
7328 IWL_DEBUG_INFO("Invoking measurement of type %d on "
7329 "channel %d (for '%s')\n", type, params.channel, buf);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007330 iwl4965_get_measurement(priv, &params, type);
Zhu Yib481de92007-09-25 17:54:57 -07007331
7332 return count;
7333}
7334
7335static DEVICE_ATTR(measurement, S_IRUSR | S_IWUSR,
7336 show_measurement, store_measurement);
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08007337#endif /* CONFIG_IWL4965_SPECTRUM_MEASUREMENT */
Zhu Yib481de92007-09-25 17:54:57 -07007338
7339static ssize_t store_retry_rate(struct device *d,
7340 struct device_attribute *attr,
7341 const char *buf, size_t count)
7342{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007343 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07007344
7345 priv->retry_rate = simple_strtoul(buf, NULL, 0);
7346 if (priv->retry_rate <= 0)
7347 priv->retry_rate = 1;
7348
7349 return count;
7350}
7351
7352static ssize_t show_retry_rate(struct device *d,
7353 struct device_attribute *attr, char *buf)
7354{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007355 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07007356 return sprintf(buf, "%d", priv->retry_rate);
7357}
7358
7359static DEVICE_ATTR(retry_rate, S_IWUSR | S_IRUSR, show_retry_rate,
7360 store_retry_rate);
7361
7362static ssize_t store_power_level(struct device *d,
7363 struct device_attribute *attr,
7364 const char *buf, size_t count)
7365{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007366 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07007367 int rc;
7368 int mode;
7369
7370 mode = simple_strtoul(buf, NULL, 0);
7371 mutex_lock(&priv->mutex);
7372
Tomas Winklerfee12472008-04-03 16:05:21 -07007373 if (!iwl_is_ready(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07007374 rc = -EAGAIN;
7375 goto out;
7376 }
7377
7378 if ((mode < 1) || (mode > IWL_POWER_LIMIT) || (mode == IWL_POWER_AC))
7379 mode = IWL_POWER_AC;
7380 else
7381 mode |= IWL_POWER_ENABLED;
7382
7383 if (mode != priv->power_mode) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007384 rc = iwl4965_send_power_mode(priv, IWL_POWER_LEVEL(mode));
Zhu Yib481de92007-09-25 17:54:57 -07007385 if (rc) {
7386 IWL_DEBUG_MAC80211("failed setting power mode.\n");
7387 goto out;
7388 }
7389 priv->power_mode = mode;
7390 }
7391
7392 rc = count;
7393
7394 out:
7395 mutex_unlock(&priv->mutex);
7396 return rc;
7397}
7398
7399#define MAX_WX_STRING 80
7400
7401/* Values are in microsecond */
7402static const s32 timeout_duration[] = {
7403 350000,
7404 250000,
7405 75000,
7406 37000,
7407 25000,
7408};
7409static const s32 period_duration[] = {
7410 400000,
7411 700000,
7412 1000000,
7413 1000000,
7414 1000000
7415};
7416
7417static ssize_t show_power_level(struct device *d,
7418 struct device_attribute *attr, char *buf)
7419{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007420 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07007421 int level = IWL_POWER_LEVEL(priv->power_mode);
7422 char *p = buf;
7423
7424 p += sprintf(p, "%d ", level);
7425 switch (level) {
7426 case IWL_POWER_MODE_CAM:
7427 case IWL_POWER_AC:
7428 p += sprintf(p, "(AC)");
7429 break;
7430 case IWL_POWER_BATTERY:
7431 p += sprintf(p, "(BATTERY)");
7432 break;
7433 default:
7434 p += sprintf(p,
7435 "(Timeout %dms, Period %dms)",
7436 timeout_duration[level - 1] / 1000,
7437 period_duration[level - 1] / 1000);
7438 }
7439
7440 if (!(priv->power_mode & IWL_POWER_ENABLED))
7441 p += sprintf(p, " OFF\n");
7442 else
7443 p += sprintf(p, " \n");
7444
7445 return (p - buf + 1);
7446
7447}
7448
7449static DEVICE_ATTR(power_level, S_IWUSR | S_IRUSR, show_power_level,
7450 store_power_level);
7451
7452static ssize_t show_channels(struct device *d,
7453 struct device_attribute *attr, char *buf)
7454{
Johannes Berg8318d782008-01-24 19:38:38 +01007455 /* all this shit doesn't belong into sysfs anyway */
7456 return 0;
Zhu Yib481de92007-09-25 17:54:57 -07007457}
7458
7459static DEVICE_ATTR(channels, S_IRUSR, show_channels, NULL);
7460
7461static ssize_t show_statistics(struct device *d,
7462 struct device_attribute *attr, char *buf)
7463{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007464 struct iwl_priv *priv = dev_get_drvdata(d);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007465 u32 size = sizeof(struct iwl4965_notif_statistics);
Zhu Yib481de92007-09-25 17:54:57 -07007466 u32 len = 0, ofs = 0;
7467 u8 *data = (u8 *) & priv->statistics;
7468 int rc = 0;
7469
Tomas Winklerfee12472008-04-03 16:05:21 -07007470 if (!iwl_is_alive(priv))
Zhu Yib481de92007-09-25 17:54:57 -07007471 return -EAGAIN;
7472
7473 mutex_lock(&priv->mutex);
Emmanuel Grumbach49ea8592008-04-15 16:01:37 -07007474 rc = iwl_send_statistics_request(priv, 0);
Zhu Yib481de92007-09-25 17:54:57 -07007475 mutex_unlock(&priv->mutex);
7476
7477 if (rc) {
7478 len = sprintf(buf,
7479 "Error sending statistics request: 0x%08X\n", rc);
7480 return len;
7481 }
7482
7483 while (size && (PAGE_SIZE - len)) {
7484 hex_dump_to_buffer(data + ofs, size, 16, 1, buf + len,
7485 PAGE_SIZE - len, 1);
7486 len = strlen(buf);
7487 if (PAGE_SIZE - len)
7488 buf[len++] = '\n';
7489
7490 ofs += 16;
7491 size -= min(size, 16U);
7492 }
7493
7494 return len;
7495}
7496
7497static DEVICE_ATTR(statistics, S_IRUGO, show_statistics, NULL);
7498
7499static ssize_t show_antenna(struct device *d,
7500 struct device_attribute *attr, char *buf)
7501{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007502 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07007503
Tomas Winklerfee12472008-04-03 16:05:21 -07007504 if (!iwl_is_alive(priv))
Zhu Yib481de92007-09-25 17:54:57 -07007505 return -EAGAIN;
7506
7507 return sprintf(buf, "%d\n", priv->antenna);
7508}
7509
7510static ssize_t store_antenna(struct device *d,
7511 struct device_attribute *attr,
7512 const char *buf, size_t count)
7513{
7514 int ant;
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007515 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07007516
7517 if (count == 0)
7518 return 0;
7519
7520 if (sscanf(buf, "%1i", &ant) != 1) {
7521 IWL_DEBUG_INFO("not in hex or decimal form.\n");
7522 return count;
7523 }
7524
7525 if ((ant >= 0) && (ant <= 2)) {
7526 IWL_DEBUG_INFO("Setting antenna select to %d.\n", ant);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007527 priv->antenna = (enum iwl4965_antenna)ant;
Zhu Yib481de92007-09-25 17:54:57 -07007528 } else
7529 IWL_DEBUG_INFO("Bad antenna select value %d.\n", ant);
7530
7531
7532 return count;
7533}
7534
7535static DEVICE_ATTR(antenna, S_IWUSR | S_IRUGO, show_antenna, store_antenna);
7536
7537static ssize_t show_status(struct device *d,
7538 struct device_attribute *attr, char *buf)
7539{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007540 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Tomas Winklerfee12472008-04-03 16:05:21 -07007541 if (!iwl_is_alive(priv))
Zhu Yib481de92007-09-25 17:54:57 -07007542 return -EAGAIN;
7543 return sprintf(buf, "0x%08x\n", (int)priv->status);
7544}
7545
7546static DEVICE_ATTR(status, S_IRUGO, show_status, NULL);
7547
7548static ssize_t dump_error_log(struct device *d,
7549 struct device_attribute *attr,
7550 const char *buf, size_t count)
7551{
7552 char *p = (char *)buf;
7553
7554 if (p[0] == '1')
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007555 iwl4965_dump_nic_error_log((struct iwl_priv *)d->driver_data);
Zhu Yib481de92007-09-25 17:54:57 -07007556
7557 return strnlen(buf, count);
7558}
7559
7560static DEVICE_ATTR(dump_errors, S_IWUSR, NULL, dump_error_log);
7561
7562static ssize_t dump_event_log(struct device *d,
7563 struct device_attribute *attr,
7564 const char *buf, size_t count)
7565{
7566 char *p = (char *)buf;
7567
7568 if (p[0] == '1')
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007569 iwl4965_dump_nic_event_log((struct iwl_priv *)d->driver_data);
Zhu Yib481de92007-09-25 17:54:57 -07007570
7571 return strnlen(buf, count);
7572}
7573
7574static DEVICE_ATTR(dump_events, S_IWUSR, NULL, dump_event_log);
7575
7576/*****************************************************************************
7577 *
7578 * driver setup and teardown
7579 *
7580 *****************************************************************************/
7581
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007582static void iwl4965_setup_deferred_work(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07007583{
7584 priv->workqueue = create_workqueue(DRV_NAME);
7585
7586 init_waitqueue_head(&priv->wait_command_queue);
7587
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007588 INIT_WORK(&priv->up, iwl4965_bg_up);
7589 INIT_WORK(&priv->restart, iwl4965_bg_restart);
7590 INIT_WORK(&priv->rx_replenish, iwl4965_bg_rx_replenish);
7591 INIT_WORK(&priv->scan_completed, iwl4965_bg_scan_completed);
7592 INIT_WORK(&priv->request_scan, iwl4965_bg_request_scan);
7593 INIT_WORK(&priv->abort_scan, iwl4965_bg_abort_scan);
7594 INIT_WORK(&priv->rf_kill, iwl4965_bg_rf_kill);
7595 INIT_WORK(&priv->beacon_update, iwl4965_bg_beacon_update);
7596 INIT_DELAYED_WORK(&priv->post_associate, iwl4965_bg_post_associate);
7597 INIT_DELAYED_WORK(&priv->init_alive_start, iwl4965_bg_init_alive_start);
7598 INIT_DELAYED_WORK(&priv->alive_start, iwl4965_bg_alive_start);
7599 INIT_DELAYED_WORK(&priv->scan_check, iwl4965_bg_scan_check);
Zhu Yib481de92007-09-25 17:54:57 -07007600
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007601 iwl4965_hw_setup_deferred_work(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007602
7603 tasklet_init(&priv->irq_tasklet, (void (*)(unsigned long))
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007604 iwl4965_irq_tasklet, (unsigned long)priv);
Zhu Yib481de92007-09-25 17:54:57 -07007605}
7606
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007607static void iwl4965_cancel_deferred_work(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07007608{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007609 iwl4965_hw_cancel_deferred_work(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007610
Joonwoo Park3ae6a052007-11-29 10:43:16 +09007611 cancel_delayed_work_sync(&priv->init_alive_start);
Zhu Yib481de92007-09-25 17:54:57 -07007612 cancel_delayed_work(&priv->scan_check);
7613 cancel_delayed_work(&priv->alive_start);
7614 cancel_delayed_work(&priv->post_associate);
7615 cancel_work_sync(&priv->beacon_update);
7616}
7617
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007618static struct attribute *iwl4965_sysfs_entries[] = {
Zhu Yib481de92007-09-25 17:54:57 -07007619 &dev_attr_antenna.attr,
7620 &dev_attr_channels.attr,
7621 &dev_attr_dump_errors.attr,
7622 &dev_attr_dump_events.attr,
7623 &dev_attr_flags.attr,
7624 &dev_attr_filter_flags.attr,
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08007625#ifdef CONFIG_IWL4965_SPECTRUM_MEASUREMENT
Zhu Yib481de92007-09-25 17:54:57 -07007626 &dev_attr_measurement.attr,
7627#endif
7628 &dev_attr_power_level.attr,
7629 &dev_attr_retry_rate.attr,
Zhu Yib481de92007-09-25 17:54:57 -07007630 &dev_attr_rs_window.attr,
7631 &dev_attr_statistics.attr,
7632 &dev_attr_status.attr,
7633 &dev_attr_temperature.attr,
Zhu Yib481de92007-09-25 17:54:57 -07007634 &dev_attr_tx_power.attr,
7635
7636 NULL
7637};
7638
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007639static struct attribute_group iwl4965_attribute_group = {
Zhu Yib481de92007-09-25 17:54:57 -07007640 .name = NULL, /* put in device directory */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007641 .attrs = iwl4965_sysfs_entries,
Zhu Yib481de92007-09-25 17:54:57 -07007642};
7643
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007644static struct ieee80211_ops iwl4965_hw_ops = {
7645 .tx = iwl4965_mac_tx,
7646 .start = iwl4965_mac_start,
7647 .stop = iwl4965_mac_stop,
7648 .add_interface = iwl4965_mac_add_interface,
7649 .remove_interface = iwl4965_mac_remove_interface,
7650 .config = iwl4965_mac_config,
7651 .config_interface = iwl4965_mac_config_interface,
7652 .configure_filter = iwl4965_configure_filter,
7653 .set_key = iwl4965_mac_set_key,
Emmanuel Grumbachab885f82008-03-20 15:06:43 +02007654 .update_tkip_key = iwl4965_mac_update_tkip_key,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007655 .get_stats = iwl4965_mac_get_stats,
7656 .get_tx_stats = iwl4965_mac_get_tx_stats,
7657 .conf_tx = iwl4965_mac_conf_tx,
7658 .get_tsf = iwl4965_mac_get_tsf,
7659 .reset_tsf = iwl4965_mac_reset_tsf,
7660 .beacon_update = iwl4965_mac_beacon_update,
Johannes Berg471b3ef2007-12-28 14:32:58 +01007661 .bss_info_changed = iwl4965_bss_info_changed,
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08007662#ifdef CONFIG_IWL4965_HT
Ron Rindjunsky9ab46172007-12-25 17:00:38 +02007663 .ampdu_action = iwl4965_mac_ampdu_action,
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08007664#endif /* CONFIG_IWL4965_HT */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007665 .hw_scan = iwl4965_mac_hw_scan
Zhu Yib481de92007-09-25 17:54:57 -07007666};
7667
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007668static int iwl4965_pci_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
Zhu Yib481de92007-09-25 17:54:57 -07007669{
7670 int err = 0;
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007671 struct iwl_priv *priv;
Zhu Yib481de92007-09-25 17:54:57 -07007672 struct ieee80211_hw *hw;
Tomas Winkler82b9a122008-03-04 18:09:30 -08007673 struct iwl_cfg *cfg = (struct iwl_cfg *)(ent->driver_data);
Mohamed Abbas0359fac2008-03-28 16:21:08 -07007674 unsigned long flags;
Zhu Yi5a669262008-01-14 17:46:18 -08007675 DECLARE_MAC_BUF(mac);
Zhu Yib481de92007-09-25 17:54:57 -07007676
Assaf Krauss316c30d2008-03-14 10:38:46 -07007677 /************************
7678 * 1. Allocating HW data
7679 ************************/
7680
Cahill, Ben M6440adb2007-11-29 11:09:55 +08007681 /* Disabling hardware scan means that mac80211 will perform scans
7682 * "the hard way", rather than using device's scan. */
Assaf Krauss1ea87392008-03-18 14:57:50 -07007683 if (cfg->mod_params->disable_hw_scan) {
Zhu Yib481de92007-09-25 17:54:57 -07007684 IWL_DEBUG_INFO("Disabling hw_scan\n");
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007685 iwl4965_hw_ops.hw_scan = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07007686 }
7687
Assaf Krauss1d0a0822008-03-14 10:38:48 -07007688 hw = iwl_alloc_all(cfg, &iwl4965_hw_ops);
7689 if (!hw) {
Zhu Yib481de92007-09-25 17:54:57 -07007690 err = -ENOMEM;
7691 goto out;
7692 }
Assaf Krauss1d0a0822008-03-14 10:38:48 -07007693 priv = hw->priv;
7694 /* At this point both hw and priv are allocated. */
7695
Zhu Yib481de92007-09-25 17:54:57 -07007696 SET_IEEE80211_DEV(hw, &pdev->dev);
7697
7698 IWL_DEBUG_INFO("*** LOAD DRIVER ***\n");
Tomas Winkler82b9a122008-03-04 18:09:30 -08007699 priv->cfg = cfg;
Zhu Yib481de92007-09-25 17:54:57 -07007700 priv->pci_dev = pdev;
Assaf Krauss316c30d2008-03-14 10:38:46 -07007701
Tomas Winkler0a6857e2008-03-12 16:58:49 -07007702#ifdef CONFIG_IWLWIFI_DEBUG
Assaf Krauss1ea87392008-03-18 14:57:50 -07007703 iwl_debug_level = priv->cfg->mod_params->debug;
Zhu Yib481de92007-09-25 17:54:57 -07007704 atomic_set(&priv->restrict_refcnt, 0);
7705#endif
Zhu Yib481de92007-09-25 17:54:57 -07007706
Assaf Krauss316c30d2008-03-14 10:38:46 -07007707 /**************************
7708 * 2. Initializing PCI bus
7709 **************************/
7710 if (pci_enable_device(pdev)) {
7711 err = -ENODEV;
7712 goto out_ieee80211_free_hw;
7713 }
7714
7715 pci_set_master(pdev);
7716
7717 err = pci_set_dma_mask(pdev, DMA_32BIT_MASK);
7718 if (!err)
7719 err = pci_set_consistent_dma_mask(pdev, DMA_32BIT_MASK);
7720 if (err) {
7721 printk(KERN_WARNING DRV_NAME
7722 ": No suitable DMA available.\n");
7723 goto out_pci_disable_device;
7724 }
7725
7726 err = pci_request_regions(pdev, DRV_NAME);
7727 if (err)
7728 goto out_pci_disable_device;
7729
7730 pci_set_drvdata(pdev, priv);
7731
7732 /* We disable the RETRY_TIMEOUT register (0x41) to keep
7733 * PCI Tx retries from interfering with C3 CPU state */
7734 pci_write_config_byte(pdev, 0x41, 0x00);
7735
7736 /***********************
7737 * 3. Read REV register
7738 ***********************/
7739 priv->hw_base = pci_iomap(pdev, 0, 0);
7740 if (!priv->hw_base) {
7741 err = -ENODEV;
7742 goto out_pci_release_regions;
7743 }
7744
7745 IWL_DEBUG_INFO("pci_resource_len = 0x%08llx\n",
7746 (unsigned long long) pci_resource_len(pdev, 0));
7747 IWL_DEBUG_INFO("pci_resource_base = %p\n", priv->hw_base);
7748
7749 printk(KERN_INFO DRV_NAME
7750 ": Detected Intel Wireless WiFi Link %s\n", priv->cfg->name);
7751
7752 /*****************
7753 * 4. Read EEPROM
7754 *****************/
7755 /* nic init */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07007756 iwl_set_bit(priv, CSR_GIO_CHICKEN_BITS,
Assaf Krauss316c30d2008-03-14 10:38:46 -07007757 CSR_GIO_CHICKEN_BITS_REG_BIT_DIS_L0S_EXIT_TIMER);
7758
Tomas Winkler3395f6e2008-03-25 16:33:37 -07007759 iwl_set_bit(priv, CSR_GP_CNTRL, CSR_GP_CNTRL_REG_FLAG_INIT_DONE);
7760 err = iwl_poll_bit(priv, CSR_GP_CNTRL,
Assaf Krauss316c30d2008-03-14 10:38:46 -07007761 CSR_GP_CNTRL_REG_FLAG_MAC_CLOCK_READY,
7762 CSR_GP_CNTRL_REG_FLAG_MAC_CLOCK_READY, 25000);
7763 if (err < 0) {
7764 IWL_DEBUG_INFO("Failed to init the card\n");
7765 goto out_iounmap;
7766 }
7767 /* Read the EEPROM */
7768 err = iwl_eeprom_init(priv);
7769 if (err) {
7770 IWL_ERROR("Unable to init EEPROM\n");
7771 goto out_iounmap;
7772 }
7773 /* MAC Address location in EEPROM same for 3945/4965 */
7774 iwl_eeprom_get_mac(priv, priv->mac_addr);
7775 IWL_DEBUG_INFO("MAC address: %s\n", print_mac(mac, priv->mac_addr));
7776 SET_IEEE80211_PERM_ADDR(priv->hw, priv->mac_addr);
7777
7778 /************************
7779 * 5. Setup HW constants
7780 ************************/
7781 /* Device-specific setup */
Tomas Winkler5425e492008-04-15 16:01:38 -07007782 if (priv->cfg->ops->lib->set_hw_params(priv)) {
7783 IWL_ERROR("failed to set hw parameters\n");
Assaf Krauss316c30d2008-03-14 10:38:46 -07007784 goto out_iounmap;
7785 }
7786
7787 /*******************
7788 * 6. Setup hw/priv
7789 *******************/
Zhu Yib481de92007-09-25 17:54:57 -07007790
Assaf Kraussbf85ea42008-03-14 10:38:49 -07007791 err = iwl_setup(priv);
7792 if (err)
Tomas Winkler5425e492008-04-15 16:01:38 -07007793 goto out_unset_hw_params;
Assaf Kraussbf85ea42008-03-14 10:38:49 -07007794 /* At this point both hw and priv are initialized. */
Assaf Krauss316c30d2008-03-14 10:38:46 -07007795
7796 /**********************************
7797 * 7. Initialize module parameters
7798 **********************************/
7799
7800 /* Disable radio (SW RF KILL) via parameter when loading driver */
Assaf Krauss1ea87392008-03-18 14:57:50 -07007801 if (priv->cfg->mod_params->disable) {
Assaf Krauss316c30d2008-03-14 10:38:46 -07007802 set_bit(STATUS_RF_KILL_SW, &priv->status);
7803 IWL_DEBUG_INFO("Radio disabled.\n");
7804 }
7805
Assaf Krauss1ea87392008-03-18 14:57:50 -07007806 if (priv->cfg->mod_params->enable_qos)
Assaf Krauss316c30d2008-03-14 10:38:46 -07007807 priv->qos_data.qos_enable = 1;
7808
7809 /********************
7810 * 8. Setup services
7811 ********************/
Mohamed Abbas0359fac2008-03-28 16:21:08 -07007812 spin_lock_irqsave(&priv->lock, flags);
Assaf Krauss316c30d2008-03-14 10:38:46 -07007813 iwl4965_disable_interrupts(priv);
Mohamed Abbas0359fac2008-03-28 16:21:08 -07007814 spin_unlock_irqrestore(&priv->lock, flags);
Assaf Krauss316c30d2008-03-14 10:38:46 -07007815
7816 err = sysfs_create_group(&pdev->dev.kobj, &iwl4965_attribute_group);
7817 if (err) {
7818 IWL_ERROR("failed to create sysfs device attributes\n");
Tomas Winkler5425e492008-04-15 16:01:38 -07007819 goto out_unset_hw_params;
Assaf Krauss316c30d2008-03-14 10:38:46 -07007820 }
7821
7822 err = iwl_dbgfs_register(priv, DRV_NAME);
7823 if (err) {
7824 IWL_ERROR("failed to create debugfs files\n");
7825 goto out_remove_sysfs;
7826 }
7827
7828 iwl4965_setup_deferred_work(priv);
7829 iwl4965_setup_rx_handlers(priv);
7830
7831 /********************
7832 * 9. Conclude
7833 ********************/
Zhu Yi5a669262008-01-14 17:46:18 -08007834 pci_save_state(pdev);
7835 pci_disable_device(pdev);
Zhu Yib481de92007-09-25 17:54:57 -07007836
Mohamed Abbasc8381fd2008-03-28 16:21:05 -07007837 /* notify iwlcore to init */
7838 iwlcore_low_level_notify(priv, IWLCORE_INIT_EVT);
Zhu Yib481de92007-09-25 17:54:57 -07007839 return 0;
7840
Assaf Krauss316c30d2008-03-14 10:38:46 -07007841 out_remove_sysfs:
7842 sysfs_remove_group(&pdev->dev.kobj, &iwl4965_attribute_group);
Tomas Winkler5425e492008-04-15 16:01:38 -07007843 out_unset_hw_params:
7844 iwl4965_unset_hw_params(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007845 out_iounmap:
7846 pci_iounmap(pdev, priv->hw_base);
7847 out_pci_release_regions:
7848 pci_release_regions(pdev);
Assaf Krauss316c30d2008-03-14 10:38:46 -07007849 pci_set_drvdata(pdev, NULL);
Zhu Yib481de92007-09-25 17:54:57 -07007850 out_pci_disable_device:
7851 pci_disable_device(pdev);
Zhu Yib481de92007-09-25 17:54:57 -07007852 out_ieee80211_free_hw:
7853 ieee80211_free_hw(priv->hw);
7854 out:
7855 return err;
7856}
7857
Reinette Chatrec83dbf62008-03-21 13:53:41 -07007858static void __devexit iwl4965_pci_remove(struct pci_dev *pdev)
Zhu Yib481de92007-09-25 17:54:57 -07007859{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007860 struct iwl_priv *priv = pci_get_drvdata(pdev);
Zhu Yib481de92007-09-25 17:54:57 -07007861 struct list_head *p, *q;
7862 int i;
Mohamed Abbas0359fac2008-03-28 16:21:08 -07007863 unsigned long flags;
Zhu Yib481de92007-09-25 17:54:57 -07007864
7865 if (!priv)
7866 return;
7867
7868 IWL_DEBUG_INFO("*** UNLOAD DRIVER ***\n");
7869
Ron Rindjunskyc4f55232008-03-28 16:21:10 -07007870 if (priv->mac80211_registered) {
7871 ieee80211_unregister_hw(priv->hw);
7872 priv->mac80211_registered = 0;
7873 }
7874
Zhu Yib481de92007-09-25 17:54:57 -07007875 set_bit(STATUS_EXIT_PENDING, &priv->status);
Zhu Yib24d22b2007-12-19 13:59:52 +08007876
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007877 iwl4965_down(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007878
Mohamed Abbas0359fac2008-03-28 16:21:08 -07007879 /* make sure we flush any pending irq or
7880 * tasklet for the driver
7881 */
7882 spin_lock_irqsave(&priv->lock, flags);
7883 iwl4965_disable_interrupts(priv);
7884 spin_unlock_irqrestore(&priv->lock, flags);
7885
7886 iwl_synchronize_irq(priv);
7887
Zhu Yib481de92007-09-25 17:54:57 -07007888 /* Free MAC hash list for ADHOC */
7889 for (i = 0; i < IWL_IBSS_MAC_HASH_SIZE; i++) {
7890 list_for_each_safe(p, q, &priv->ibss_mac_hash[i]) {
7891 list_del(p);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007892 kfree(list_entry(p, struct iwl4965_ibss_seq, list));
Zhu Yib481de92007-09-25 17:54:57 -07007893 }
7894 }
7895
Mohamed Abbasc8381fd2008-03-28 16:21:05 -07007896 iwlcore_low_level_notify(priv, IWLCORE_REMOVE_EVT);
Tomas Winkler712b6cf2008-03-12 16:58:52 -07007897 iwl_dbgfs_unregister(priv);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007898 sysfs_remove_group(&pdev->dev.kobj, &iwl4965_attribute_group);
Zhu Yib481de92007-09-25 17:54:57 -07007899
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007900 iwl4965_dealloc_ucode_pci(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007901
7902 if (priv->rxq.bd)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007903 iwl4965_rx_queue_free(priv, &priv->rxq);
7904 iwl4965_hw_txq_ctx_free(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007905
Tomas Winkler5425e492008-04-15 16:01:38 -07007906 iwl4965_unset_hw_params(priv);
Assaf Kraussbf85ea42008-03-14 10:38:49 -07007907 iwlcore_clear_stations_table(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007908
Zhu Yib481de92007-09-25 17:54:57 -07007909
Mohamed Abbas948c1712007-10-25 17:15:45 +08007910 /*netif_stop_queue(dev); */
7911 flush_workqueue(priv->workqueue);
7912
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007913 /* ieee80211_unregister_hw calls iwl4965_mac_stop, which flushes
Zhu Yib481de92007-09-25 17:54:57 -07007914 * priv->workqueue... so we can't take down the workqueue
7915 * until now... */
7916 destroy_workqueue(priv->workqueue);
7917 priv->workqueue = NULL;
7918
Zhu Yib481de92007-09-25 17:54:57 -07007919 pci_iounmap(pdev, priv->hw_base);
7920 pci_release_regions(pdev);
7921 pci_disable_device(pdev);
7922 pci_set_drvdata(pdev, NULL);
7923
Assaf Kraussbf85ea42008-03-14 10:38:49 -07007924 iwl_free_channel_map(priv);
Reinette Chatre849e0dc2008-01-23 10:15:18 -08007925 iwl4965_free_geos(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007926
7927 if (priv->ibss_beacon)
7928 dev_kfree_skb(priv->ibss_beacon);
7929
7930 ieee80211_free_hw(priv->hw);
7931}
7932
7933#ifdef CONFIG_PM
7934
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007935static int iwl4965_pci_suspend(struct pci_dev *pdev, pm_message_t state)
Zhu Yib481de92007-09-25 17:54:57 -07007936{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007937 struct iwl_priv *priv = pci_get_drvdata(pdev);
Zhu Yib481de92007-09-25 17:54:57 -07007938
Zhu Yie655b9f2008-01-24 02:19:38 -08007939 if (priv->is_open) {
7940 set_bit(STATUS_IN_SUSPEND, &priv->status);
7941 iwl4965_mac_stop(priv->hw);
7942 priv->is_open = 1;
7943 }
Zhu Yib481de92007-09-25 17:54:57 -07007944
Zhu Yib481de92007-09-25 17:54:57 -07007945 pci_set_power_state(pdev, PCI_D3hot);
7946
Zhu Yib481de92007-09-25 17:54:57 -07007947 return 0;
7948}
7949
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007950static int iwl4965_pci_resume(struct pci_dev *pdev)
Zhu Yib481de92007-09-25 17:54:57 -07007951{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007952 struct iwl_priv *priv = pci_get_drvdata(pdev);
Zhu Yib481de92007-09-25 17:54:57 -07007953
Zhu Yib481de92007-09-25 17:54:57 -07007954 pci_set_power_state(pdev, PCI_D0);
Zhu Yib481de92007-09-25 17:54:57 -07007955
Zhu Yie655b9f2008-01-24 02:19:38 -08007956 if (priv->is_open)
7957 iwl4965_mac_start(priv->hw);
Zhu Yib481de92007-09-25 17:54:57 -07007958
Zhu Yie655b9f2008-01-24 02:19:38 -08007959 clear_bit(STATUS_IN_SUSPEND, &priv->status);
Zhu Yib481de92007-09-25 17:54:57 -07007960 return 0;
7961}
7962
7963#endif /* CONFIG_PM */
7964
7965/*****************************************************************************
7966 *
7967 * driver and module entry point
7968 *
7969 *****************************************************************************/
7970
Ron Rindjunskyfed90172008-04-15 16:01:41 -07007971/* Hardware specific file defines the PCI IDs table for that hardware module */
7972static struct pci_device_id iwl_hw_card_ids[] = {
7973 {IWL_PCI_DEVICE(0x4229, PCI_ANY_ID, iwl4965_agn_cfg)},
7974 {IWL_PCI_DEVICE(0x4230, PCI_ANY_ID, iwl4965_agn_cfg)},
7975 {0}
7976};
7977MODULE_DEVICE_TABLE(pci, iwl_hw_card_ids);
7978
7979static struct pci_driver iwl_driver = {
Zhu Yib481de92007-09-25 17:54:57 -07007980 .name = DRV_NAME,
Ron Rindjunskyfed90172008-04-15 16:01:41 -07007981 .id_table = iwl_hw_card_ids,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007982 .probe = iwl4965_pci_probe,
7983 .remove = __devexit_p(iwl4965_pci_remove),
Zhu Yib481de92007-09-25 17:54:57 -07007984#ifdef CONFIG_PM
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007985 .suspend = iwl4965_pci_suspend,
7986 .resume = iwl4965_pci_resume,
Zhu Yib481de92007-09-25 17:54:57 -07007987#endif
7988};
7989
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007990static int __init iwl4965_init(void)
Zhu Yib481de92007-09-25 17:54:57 -07007991{
7992
7993 int ret;
7994 printk(KERN_INFO DRV_NAME ": " DRV_DESCRIPTION ", " DRV_VERSION "\n");
7995 printk(KERN_INFO DRV_NAME ": " DRV_COPYRIGHT "\n");
Reinette Chatre897e1cf2008-03-28 16:21:09 -07007996
7997 ret = iwl4965_rate_control_register();
7998 if (ret) {
7999 IWL_ERROR("Unable to register rate control algorithm: %d\n", ret);
8000 return ret;
8001 }
8002
Ron Rindjunskyfed90172008-04-15 16:01:41 -07008003 ret = pci_register_driver(&iwl_driver);
Zhu Yib481de92007-09-25 17:54:57 -07008004 if (ret) {
8005 IWL_ERROR("Unable to initialize PCI module\n");
Reinette Chatre897e1cf2008-03-28 16:21:09 -07008006 goto error_register;
Zhu Yib481de92007-09-25 17:54:57 -07008007 }
Tomas Winkler0a6857e2008-03-12 16:58:49 -07008008#ifdef CONFIG_IWLWIFI_DEBUG
Ron Rindjunskyfed90172008-04-15 16:01:41 -07008009 ret = driver_create_file(&iwl_driver.driver, &driver_attr_debug_level);
Zhu Yib481de92007-09-25 17:54:57 -07008010 if (ret) {
8011 IWL_ERROR("Unable to create driver sysfs file\n");
Reinette Chatre897e1cf2008-03-28 16:21:09 -07008012 goto error_debug;
Zhu Yib481de92007-09-25 17:54:57 -07008013 }
8014#endif
8015
8016 return ret;
Reinette Chatre897e1cf2008-03-28 16:21:09 -07008017
8018#ifdef CONFIG_IWLWIFI_DEBUG
8019error_debug:
Ron Rindjunskyfed90172008-04-15 16:01:41 -07008020 pci_unregister_driver(&iwl_driver);
Reinette Chatre897e1cf2008-03-28 16:21:09 -07008021#endif
8022error_register:
8023 iwl4965_rate_control_unregister();
8024 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07008025}
8026
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008027static void __exit iwl4965_exit(void)
Zhu Yib481de92007-09-25 17:54:57 -07008028{
Tomas Winkler0a6857e2008-03-12 16:58:49 -07008029#ifdef CONFIG_IWLWIFI_DEBUG
Ron Rindjunskyfed90172008-04-15 16:01:41 -07008030 driver_remove_file(&iwl_driver.driver, &driver_attr_debug_level);
Zhu Yib481de92007-09-25 17:54:57 -07008031#endif
Ron Rindjunskyfed90172008-04-15 16:01:41 -07008032 pci_unregister_driver(&iwl_driver);
Reinette Chatre897e1cf2008-03-28 16:21:09 -07008033 iwl4965_rate_control_unregister();
Zhu Yib481de92007-09-25 17:54:57 -07008034}
8035
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008036module_exit(iwl4965_exit);
8037module_init(iwl4965_init);