blob: 1f343409c904155729e1c42d2e257a1b804a8188 [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))
401 index = priv->hw_setting.bcast_sta_id;
402 else
403 for (i = IWL_STA_ID; i < priv->hw_setting.max_stations; i++)
404 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))
443 index = priv->hw_setting.bcast_sta_id;
444 else
445 for (i = IWL_STA_ID; i < priv->hw_setting.max_stations; i++) {
446 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 */
486 if (index != priv->hw_setting.bcast_sta_id &&
487 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
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700599int iwl4965_send_statistics_request(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700600{
Tomas Winkler857485c2008-03-21 13:53:44 -0700601 u32 flags = 0;
602 return iwl_send_cmd_pdu(priv, REPLY_STATISTICS_CMD,
603 sizeof(flags), &flags);
Zhu Yib481de92007-09-25 17:54:57 -0700604}
605
606/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800607 * iwl4965_rxon_add_station - add station into station table.
Zhu Yib481de92007-09-25 17:54:57 -0700608 *
609 * there is only one AP station with id= IWL_AP_ID
Ben Cahill9fbab512007-11-29 11:09:47 +0800610 * NOTE: mutex must be held before calling this fnction
611 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700612static int iwl4965_rxon_add_station(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -0700613 const u8 *addr, int is_ap)
614{
Zhu Yi556f8db2007-09-27 11:27:33 +0800615 u8 sta_id;
Zhu Yib481de92007-09-25 17:54:57 -0700616
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800617 /* Add station to device's station table */
Ron Rindjunsky67d62032007-11-26 16:14:40 +0200618#ifdef CONFIG_IWL4965_HT
619 struct ieee80211_conf *conf = &priv->hw->conf;
620 struct ieee80211_ht_info *cur_ht_config = &conf->ht_conf;
621
622 if ((is_ap) &&
623 (conf->flags & IEEE80211_CONF_SUPPORT_HT_MODE) &&
624 (priv->iw_mode == IEEE80211_IF_TYPE_STA))
625 sta_id = iwl4965_add_station_flags(priv, addr, is_ap,
626 0, cur_ht_config);
627 else
628#endif /* CONFIG_IWL4965_HT */
629 sta_id = iwl4965_add_station_flags(priv, addr, is_ap,
630 0, NULL);
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800631
632 /* Set up default rate scaling table in device's station table */
Zhu Yib481de92007-09-25 17:54:57 -0700633 iwl4965_add_station(priv, addr, is_ap);
634
Zhu Yi556f8db2007-09-27 11:27:33 +0800635 return sta_id;
Zhu Yib481de92007-09-25 17:54:57 -0700636}
637
638/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800639 * iwl4965_check_rxon_cmd - validate RXON structure is valid
Zhu Yib481de92007-09-25 17:54:57 -0700640 *
641 * NOTE: This is really only useful during development and can eventually
642 * be #ifdef'd out once the driver is stable and folks aren't actively
643 * making changes
644 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800645static int iwl4965_check_rxon_cmd(struct iwl4965_rxon_cmd *rxon)
Zhu Yib481de92007-09-25 17:54:57 -0700646{
647 int error = 0;
648 int counter = 1;
649
650 if (rxon->flags & RXON_FLG_BAND_24G_MSK) {
651 error |= le32_to_cpu(rxon->flags &
652 (RXON_FLG_TGJ_NARROW_BAND_MSK |
653 RXON_FLG_RADAR_DETECT_MSK));
654 if (error)
655 IWL_WARNING("check 24G fields %d | %d\n",
656 counter++, error);
657 } else {
658 error |= (rxon->flags & RXON_FLG_SHORT_SLOT_MSK) ?
659 0 : le32_to_cpu(RXON_FLG_SHORT_SLOT_MSK);
660 if (error)
661 IWL_WARNING("check 52 fields %d | %d\n",
662 counter++, error);
663 error |= le32_to_cpu(rxon->flags & RXON_FLG_CCK_MSK);
664 if (error)
665 IWL_WARNING("check 52 CCK %d | %d\n",
666 counter++, error);
667 }
668 error |= (rxon->node_addr[0] | rxon->bssid_addr[0]) & 0x1;
669 if (error)
670 IWL_WARNING("check mac addr %d | %d\n", counter++, error);
671
672 /* make sure basic rates 6Mbps and 1Mbps are supported */
673 error |= (((rxon->ofdm_basic_rates & IWL_RATE_6M_MASK) == 0) &&
674 ((rxon->cck_basic_rates & IWL_RATE_1M_MASK) == 0));
675 if (error)
676 IWL_WARNING("check basic rate %d | %d\n", counter++, error);
677
678 error |= (le16_to_cpu(rxon->assoc_id) > 2007);
679 if (error)
680 IWL_WARNING("check assoc id %d | %d\n", counter++, error);
681
682 error |= ((rxon->flags & (RXON_FLG_CCK_MSK | RXON_FLG_SHORT_SLOT_MSK))
683 == (RXON_FLG_CCK_MSK | RXON_FLG_SHORT_SLOT_MSK));
684 if (error)
685 IWL_WARNING("check CCK and short slot %d | %d\n",
686 counter++, error);
687
688 error |= ((rxon->flags & (RXON_FLG_CCK_MSK | RXON_FLG_AUTO_DETECT_MSK))
689 == (RXON_FLG_CCK_MSK | RXON_FLG_AUTO_DETECT_MSK));
690 if (error)
691 IWL_WARNING("check CCK & auto detect %d | %d\n",
692 counter++, error);
693
694 error |= ((rxon->flags & (RXON_FLG_AUTO_DETECT_MSK |
695 RXON_FLG_TGG_PROTECT_MSK)) == RXON_FLG_TGG_PROTECT_MSK);
696 if (error)
697 IWL_WARNING("check TGG and auto detect %d | %d\n",
698 counter++, error);
699
700 if (error)
701 IWL_WARNING("Tuning to channel %d\n",
702 le16_to_cpu(rxon->channel));
703
704 if (error) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800705 IWL_ERROR("Not a valid iwl4965_rxon_assoc_cmd field values\n");
Zhu Yib481de92007-09-25 17:54:57 -0700706 return -1;
707 }
708 return 0;
709}
710
711/**
Ben Cahill9fbab512007-11-29 11:09:47 +0800712 * iwl4965_full_rxon_required - check if full RXON (vs RXON_ASSOC) cmd is needed
Ian Schram01ebd062007-10-25 17:15:22 +0800713 * @priv: staging_rxon is compared to active_rxon
Zhu Yib481de92007-09-25 17:54:57 -0700714 *
Ben Cahill9fbab512007-11-29 11:09:47 +0800715 * If the RXON structure is changing enough to require a new tune,
716 * or is clearing the RXON_FILTER_ASSOC_MSK, then return 1 to indicate that
717 * a new tune (full RXON command, rather than RXON_ASSOC cmd) is required.
Zhu Yib481de92007-09-25 17:54:57 -0700718 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700719static int iwl4965_full_rxon_required(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700720{
721
722 /* These items are only settable from the full RXON command */
723 if (!(priv->active_rxon.filter_flags & RXON_FILTER_ASSOC_MSK) ||
724 compare_ether_addr(priv->staging_rxon.bssid_addr,
725 priv->active_rxon.bssid_addr) ||
726 compare_ether_addr(priv->staging_rxon.node_addr,
727 priv->active_rxon.node_addr) ||
728 compare_ether_addr(priv->staging_rxon.wlap_bssid_addr,
729 priv->active_rxon.wlap_bssid_addr) ||
730 (priv->staging_rxon.dev_type != priv->active_rxon.dev_type) ||
731 (priv->staging_rxon.channel != priv->active_rxon.channel) ||
732 (priv->staging_rxon.air_propagation !=
733 priv->active_rxon.air_propagation) ||
734 (priv->staging_rxon.ofdm_ht_single_stream_basic_rates !=
735 priv->active_rxon.ofdm_ht_single_stream_basic_rates) ||
736 (priv->staging_rxon.ofdm_ht_dual_stream_basic_rates !=
737 priv->active_rxon.ofdm_ht_dual_stream_basic_rates) ||
738 (priv->staging_rxon.rx_chain != priv->active_rxon.rx_chain) ||
739 (priv->staging_rxon.assoc_id != priv->active_rxon.assoc_id))
740 return 1;
741
742 /* flags, filter_flags, ofdm_basic_rates, and cck_basic_rates can
743 * be updated with the RXON_ASSOC command -- however only some
744 * flag transitions are allowed using RXON_ASSOC */
745
746 /* Check if we are not switching bands */
747 if ((priv->staging_rxon.flags & RXON_FLG_BAND_24G_MSK) !=
748 (priv->active_rxon.flags & RXON_FLG_BAND_24G_MSK))
749 return 1;
750
751 /* Check if we are switching association toggle */
752 if ((priv->staging_rxon.filter_flags & RXON_FILTER_ASSOC_MSK) !=
753 (priv->active_rxon.filter_flags & RXON_FILTER_ASSOC_MSK))
754 return 1;
755
756 return 0;
757}
758
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700759static int iwl4965_send_rxon_assoc(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700760{
Reinette Chatre51dbfd12008-04-14 21:16:11 -0700761 int ret = 0;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800762 struct iwl4965_rxon_assoc_cmd rxon_assoc;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800763 const struct iwl4965_rxon_cmd *rxon1 = &priv->staging_rxon;
764 const struct iwl4965_rxon_cmd *rxon2 = &priv->active_rxon;
Zhu Yib481de92007-09-25 17:54:57 -0700765
766 if ((rxon1->flags == rxon2->flags) &&
767 (rxon1->filter_flags == rxon2->filter_flags) &&
768 (rxon1->cck_basic_rates == rxon2->cck_basic_rates) &&
769 (rxon1->ofdm_ht_single_stream_basic_rates ==
770 rxon2->ofdm_ht_single_stream_basic_rates) &&
771 (rxon1->ofdm_ht_dual_stream_basic_rates ==
772 rxon2->ofdm_ht_dual_stream_basic_rates) &&
773 (rxon1->rx_chain == rxon2->rx_chain) &&
774 (rxon1->ofdm_basic_rates == rxon2->ofdm_basic_rates)) {
775 IWL_DEBUG_INFO("Using current RXON_ASSOC. Not resending.\n");
776 return 0;
777 }
778
779 rxon_assoc.flags = priv->staging_rxon.flags;
780 rxon_assoc.filter_flags = priv->staging_rxon.filter_flags;
781 rxon_assoc.ofdm_basic_rates = priv->staging_rxon.ofdm_basic_rates;
782 rxon_assoc.cck_basic_rates = priv->staging_rxon.cck_basic_rates;
783 rxon_assoc.reserved = 0;
784 rxon_assoc.ofdm_ht_single_stream_basic_rates =
785 priv->staging_rxon.ofdm_ht_single_stream_basic_rates;
786 rxon_assoc.ofdm_ht_dual_stream_basic_rates =
787 priv->staging_rxon.ofdm_ht_dual_stream_basic_rates;
788 rxon_assoc.rx_chain_select_flags = priv->staging_rxon.rx_chain;
789
Reinette Chatre51dbfd12008-04-14 21:16:11 -0700790 ret = iwl_send_cmd_pdu_async(priv, REPLY_RXON_ASSOC,
791 sizeof(rxon_assoc), &rxon_assoc, NULL);
792 if (ret)
793 return ret;
Zhu Yib481de92007-09-25 17:54:57 -0700794
Reinette Chatre51dbfd12008-04-14 21:16:11 -0700795 return ret;
Zhu Yib481de92007-09-25 17:54:57 -0700796}
797
798/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800799 * iwl4965_commit_rxon - commit staging_rxon to hardware
Zhu Yib481de92007-09-25 17:54:57 -0700800 *
Ian Schram01ebd062007-10-25 17:15:22 +0800801 * The RXON command in staging_rxon is committed to the hardware and
Zhu Yib481de92007-09-25 17:54:57 -0700802 * the active_rxon structure is updated with the new data. This
803 * function correctly transitions out of the RXON_ASSOC_MSK state if
804 * a HW tune is required based on the RXON structure changes.
805 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700806static int iwl4965_commit_rxon(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700807{
808 /* cast away the const for active_rxon in this function */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800809 struct iwl4965_rxon_cmd *active_rxon = (void *)&priv->active_rxon;
Joe Perches0795af52007-10-03 17:59:30 -0700810 DECLARE_MAC_BUF(mac);
Zhu Yib481de92007-09-25 17:54:57 -0700811 int rc = 0;
812
Tomas Winklerfee12472008-04-03 16:05:21 -0700813 if (!iwl_is_alive(priv))
Zhu Yib481de92007-09-25 17:54:57 -0700814 return -1;
815
816 /* always get timestamp with Rx frame */
817 priv->staging_rxon.flags |= RXON_FLG_TSF2HOST_MSK;
818
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800819 rc = iwl4965_check_rxon_cmd(&priv->staging_rxon);
Zhu Yib481de92007-09-25 17:54:57 -0700820 if (rc) {
821 IWL_ERROR("Invalid RXON configuration. Not committing.\n");
822 return -EINVAL;
823 }
824
825 /* If we don't need to send a full RXON, we can use
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800826 * iwl4965_rxon_assoc_cmd which is used to reconfigure filter
Zhu Yib481de92007-09-25 17:54:57 -0700827 * and other flags for the current radio configuration. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800828 if (!iwl4965_full_rxon_required(priv)) {
829 rc = iwl4965_send_rxon_assoc(priv);
Zhu Yib481de92007-09-25 17:54:57 -0700830 if (rc) {
831 IWL_ERROR("Error setting RXON_ASSOC "
832 "configuration (%d).\n", rc);
833 return rc;
834 }
835
836 memcpy(active_rxon, &priv->staging_rxon, sizeof(*active_rxon));
837
838 return 0;
839 }
840
841 /* station table will be cleared */
842 priv->assoc_station_added = 0;
843
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +0800844#ifdef CONFIG_IWL4965_SENSITIVITY
Zhu Yib481de92007-09-25 17:54:57 -0700845 priv->sensitivity_data.state = IWL_SENS_CALIB_NEED_REINIT;
846 if (!priv->error_recovering)
847 priv->start_calib = 0;
848
849 iwl4965_init_sensitivity(priv, CMD_ASYNC, 1);
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +0800850#endif /* CONFIG_IWL4965_SENSITIVITY */
Zhu Yib481de92007-09-25 17:54:57 -0700851
852 /* If we are currently associated and the new config requires
853 * an RXON_ASSOC and the new config wants the associated mask enabled,
854 * we must clear the associated from the active configuration
855 * before we apply the new config */
Tomas Winkler3109ece2008-03-28 16:33:35 -0700856 if (iwl_is_associated(priv) &&
Zhu Yib481de92007-09-25 17:54:57 -0700857 (priv->staging_rxon.filter_flags & RXON_FILTER_ASSOC_MSK)) {
858 IWL_DEBUG_INFO("Toggling associated bit on current RXON\n");
859 active_rxon->filter_flags &= ~RXON_FILTER_ASSOC_MSK;
860
Tomas Winkler857485c2008-03-21 13:53:44 -0700861 rc = iwl_send_cmd_pdu(priv, REPLY_RXON,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800862 sizeof(struct iwl4965_rxon_cmd),
Zhu Yib481de92007-09-25 17:54:57 -0700863 &priv->active_rxon);
864
865 /* If the mask clearing failed then we set
866 * active_rxon back to what it was previously */
867 if (rc) {
868 active_rxon->filter_flags |= RXON_FILTER_ASSOC_MSK;
869 IWL_ERROR("Error clearing ASSOC_MSK on current "
870 "configuration (%d).\n", rc);
871 return rc;
872 }
Zhu Yib481de92007-09-25 17:54:57 -0700873 }
874
875 IWL_DEBUG_INFO("Sending RXON\n"
876 "* with%s RXON_FILTER_ASSOC_MSK\n"
877 "* channel = %d\n"
Joe Perches0795af52007-10-03 17:59:30 -0700878 "* bssid = %s\n",
Zhu Yib481de92007-09-25 17:54:57 -0700879 ((priv->staging_rxon.filter_flags &
880 RXON_FILTER_ASSOC_MSK) ? "" : "out"),
881 le16_to_cpu(priv->staging_rxon.channel),
Joe Perches0795af52007-10-03 17:59:30 -0700882 print_mac(mac, priv->staging_rxon.bssid_addr));
Zhu Yib481de92007-09-25 17:54:57 -0700883
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -0700884 iwl4965_set_rxon_hwcrypto(priv, priv->cfg->mod_params->hw_crypto);
Zhu Yib481de92007-09-25 17:54:57 -0700885 /* Apply the new configuration */
Tomas Winkler857485c2008-03-21 13:53:44 -0700886 rc = iwl_send_cmd_pdu(priv, REPLY_RXON,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800887 sizeof(struct iwl4965_rxon_cmd), &priv->staging_rxon);
Zhu Yib481de92007-09-25 17:54:57 -0700888 if (rc) {
889 IWL_ERROR("Error setting new configuration (%d).\n", rc);
890 return rc;
891 }
892
Assaf Kraussbf85ea42008-03-14 10:38:49 -0700893 iwlcore_clear_stations_table(priv);
Zhu Yi556f8db2007-09-27 11:27:33 +0800894
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +0800895#ifdef CONFIG_IWL4965_SENSITIVITY
Zhu Yib481de92007-09-25 17:54:57 -0700896 if (!priv->error_recovering)
897 priv->start_calib = 0;
898
899 priv->sensitivity_data.state = IWL_SENS_CALIB_NEED_REINIT;
900 iwl4965_init_sensitivity(priv, CMD_ASYNC, 1);
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +0800901#endif /* CONFIG_IWL4965_SENSITIVITY */
Zhu Yib481de92007-09-25 17:54:57 -0700902
903 memcpy(active_rxon, &priv->staging_rxon, sizeof(*active_rxon));
904
905 /* If we issue a new RXON command which required a tune then we must
906 * send a new TXPOWER command or we won't be able to Tx any frames */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800907 rc = iwl4965_hw_reg_send_txpower(priv);
Zhu Yib481de92007-09-25 17:54:57 -0700908 if (rc) {
909 IWL_ERROR("Error setting Tx power (%d).\n", rc);
910 return rc;
911 }
912
913 /* Add the broadcast address so we can send broadcast frames */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800914 if (iwl4965_rxon_add_station(priv, iwl4965_broadcast_addr, 0) ==
Zhu Yib481de92007-09-25 17:54:57 -0700915 IWL_INVALID_STATION) {
916 IWL_ERROR("Error adding BROADCAST address for transmit.\n");
917 return -EIO;
918 }
919
920 /* If we have set the ASSOC_MSK and we are in BSS mode then
921 * add the IWL_AP_ID to the station rate table */
Tomas Winkler3109ece2008-03-28 16:33:35 -0700922 if (iwl_is_associated(priv) &&
Zhu Yib481de92007-09-25 17:54:57 -0700923 (priv->iw_mode == IEEE80211_IF_TYPE_STA)) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800924 if (iwl4965_rxon_add_station(priv, priv->active_rxon.bssid_addr, 1)
Zhu Yib481de92007-09-25 17:54:57 -0700925 == IWL_INVALID_STATION) {
926 IWL_ERROR("Error adding AP address for transmit.\n");
927 return -EIO;
928 }
929 priv->assoc_station_added = 1;
Emmanuel Grumbach6974e362008-04-14 21:16:06 -0700930 if (priv->default_wep_key &&
931 iwl_send_static_wepkey_cmd(priv, 0))
932 IWL_ERROR("Could not send WEP static key.\n");
Zhu Yib481de92007-09-25 17:54:57 -0700933 }
934
935 return 0;
936}
937
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700938static int iwl4965_send_bt_config(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700939{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800940 struct iwl4965_bt_cmd bt_cmd = {
Zhu Yib481de92007-09-25 17:54:57 -0700941 .flags = 3,
942 .lead_time = 0xAA,
943 .max_kill = 1,
944 .kill_ack_mask = 0,
945 .kill_cts_mask = 0,
946 };
947
Tomas Winkler857485c2008-03-21 13:53:44 -0700948 return iwl_send_cmd_pdu(priv, REPLY_BT_CONFIG,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800949 sizeof(struct iwl4965_bt_cmd), &bt_cmd);
Zhu Yib481de92007-09-25 17:54:57 -0700950}
951
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700952static int iwl4965_send_scan_abort(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700953{
954 int rc = 0;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800955 struct iwl4965_rx_packet *res;
Tomas Winkler857485c2008-03-21 13:53:44 -0700956 struct iwl_host_cmd cmd = {
Zhu Yib481de92007-09-25 17:54:57 -0700957 .id = REPLY_SCAN_ABORT_CMD,
958 .meta.flags = CMD_WANT_SKB,
959 };
960
961 /* If there isn't a scan actively going on in the hardware
962 * then we are in between scan bands and not actually
963 * actively scanning, so don't send the abort command */
964 if (!test_bit(STATUS_SCAN_HW, &priv->status)) {
965 clear_bit(STATUS_SCAN_ABORTING, &priv->status);
966 return 0;
967 }
968
Tomas Winkler857485c2008-03-21 13:53:44 -0700969 rc = iwl_send_cmd_sync(priv, &cmd);
Zhu Yib481de92007-09-25 17:54:57 -0700970 if (rc) {
971 clear_bit(STATUS_SCAN_ABORTING, &priv->status);
972 return rc;
973 }
974
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800975 res = (struct iwl4965_rx_packet *)cmd.meta.u.skb->data;
Zhu Yib481de92007-09-25 17:54:57 -0700976 if (res->u.status != CAN_ABORT_STATUS) {
977 /* The scan abort will return 1 for success or
978 * 2 for "failure". A failure condition can be
979 * due to simply not being in an active scan which
980 * can occur if we send the scan abort before we
981 * the microcode has notified us that a scan is
982 * completed. */
983 IWL_DEBUG_INFO("SCAN_ABORT returned %d.\n", res->u.status);
984 clear_bit(STATUS_SCAN_ABORTING, &priv->status);
985 clear_bit(STATUS_SCAN_HW, &priv->status);
986 }
987
988 dev_kfree_skb_any(cmd.meta.u.skb);
989
990 return rc;
991}
992
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700993static int iwl4965_card_state_sync_callback(struct iwl_priv *priv,
Tomas Winkler857485c2008-03-21 13:53:44 -0700994 struct iwl_cmd *cmd,
Zhu Yib481de92007-09-25 17:54:57 -0700995 struct sk_buff *skb)
996{
997 return 1;
998}
999
1000/*
1001 * CARD_STATE_CMD
1002 *
Ben Cahill9fbab512007-11-29 11:09:47 +08001003 * Use: Sets the device's internal card state to enable, disable, or halt
Zhu Yib481de92007-09-25 17:54:57 -07001004 *
1005 * When in the 'enable' state the card operates as normal.
1006 * When in the 'disable' state, the card enters into a low power mode.
1007 * When in the 'halt' state, the card is shut down and must be fully
1008 * restarted to come back on.
1009 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001010static int iwl4965_send_card_state(struct iwl_priv *priv, u32 flags, u8 meta_flag)
Zhu Yib481de92007-09-25 17:54:57 -07001011{
Tomas Winkler857485c2008-03-21 13:53:44 -07001012 struct iwl_host_cmd cmd = {
Zhu Yib481de92007-09-25 17:54:57 -07001013 .id = REPLY_CARD_STATE_CMD,
1014 .len = sizeof(u32),
1015 .data = &flags,
1016 .meta.flags = meta_flag,
1017 };
1018
1019 if (meta_flag & CMD_ASYNC)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001020 cmd.meta.u.callback = iwl4965_card_state_sync_callback;
Zhu Yib481de92007-09-25 17:54:57 -07001021
Tomas Winkler857485c2008-03-21 13:53:44 -07001022 return iwl_send_cmd(priv, &cmd);
Zhu Yib481de92007-09-25 17:54:57 -07001023}
1024
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001025static int iwl4965_add_sta_sync_callback(struct iwl_priv *priv,
Tomas Winkler857485c2008-03-21 13:53:44 -07001026 struct iwl_cmd *cmd, struct sk_buff *skb)
Zhu Yib481de92007-09-25 17:54:57 -07001027{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001028 struct iwl4965_rx_packet *res = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07001029
1030 if (!skb) {
1031 IWL_ERROR("Error: Response NULL in REPLY_ADD_STA.\n");
1032 return 1;
1033 }
1034
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001035 res = (struct iwl4965_rx_packet *)skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07001036 if (res->hdr.flags & IWL_CMD_FAILED_MSK) {
1037 IWL_ERROR("Bad return from REPLY_ADD_STA (0x%08X)\n",
1038 res->hdr.flags);
1039 return 1;
1040 }
1041
1042 switch (res->u.add_sta.status) {
1043 case ADD_STA_SUCCESS_MSK:
1044 break;
1045 default:
1046 break;
1047 }
1048
1049 /* We didn't cache the SKB; let the caller free it */
1050 return 1;
1051}
1052
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001053int iwl4965_send_add_station(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001054 struct iwl4965_addsta_cmd *sta, u8 flags)
Zhu Yib481de92007-09-25 17:54:57 -07001055{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001056 struct iwl4965_rx_packet *res = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07001057 int rc = 0;
Tomas Winkler857485c2008-03-21 13:53:44 -07001058 struct iwl_host_cmd cmd = {
Zhu Yib481de92007-09-25 17:54:57 -07001059 .id = REPLY_ADD_STA,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001060 .len = sizeof(struct iwl4965_addsta_cmd),
Zhu Yib481de92007-09-25 17:54:57 -07001061 .meta.flags = flags,
1062 .data = sta,
1063 };
1064
1065 if (flags & CMD_ASYNC)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001066 cmd.meta.u.callback = iwl4965_add_sta_sync_callback;
Zhu Yib481de92007-09-25 17:54:57 -07001067 else
1068 cmd.meta.flags |= CMD_WANT_SKB;
1069
Tomas Winkler857485c2008-03-21 13:53:44 -07001070 rc = iwl_send_cmd(priv, &cmd);
Zhu Yib481de92007-09-25 17:54:57 -07001071
1072 if (rc || (flags & CMD_ASYNC))
1073 return rc;
1074
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001075 res = (struct iwl4965_rx_packet *)cmd.meta.u.skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07001076 if (res->hdr.flags & IWL_CMD_FAILED_MSK) {
1077 IWL_ERROR("Bad return from REPLY_ADD_STA (0x%08X)\n",
1078 res->hdr.flags);
1079 rc = -EIO;
1080 }
1081
1082 if (rc == 0) {
1083 switch (res->u.add_sta.status) {
1084 case ADD_STA_SUCCESS_MSK:
1085 IWL_DEBUG_INFO("REPLY_ADD_STA PASSED\n");
1086 break;
1087 default:
1088 rc = -EIO;
1089 IWL_WARNING("REPLY_ADD_STA failed\n");
1090 break;
1091 }
1092 }
1093
1094 priv->alloc_rxb_skb--;
1095 dev_kfree_skb_any(cmd.meta.u.skb);
1096
1097 return rc;
1098}
1099
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001100static void iwl4965_clear_free_frames(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001101{
1102 struct list_head *element;
1103
1104 IWL_DEBUG_INFO("%d frames on pre-allocated heap on clear.\n",
1105 priv->frames_count);
1106
1107 while (!list_empty(&priv->free_frames)) {
1108 element = priv->free_frames.next;
1109 list_del(element);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001110 kfree(list_entry(element, struct iwl4965_frame, list));
Zhu Yib481de92007-09-25 17:54:57 -07001111 priv->frames_count--;
1112 }
1113
1114 if (priv->frames_count) {
1115 IWL_WARNING("%d frames still in use. Did we lose one?\n",
1116 priv->frames_count);
1117 priv->frames_count = 0;
1118 }
1119}
1120
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001121static struct iwl4965_frame *iwl4965_get_free_frame(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001122{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001123 struct iwl4965_frame *frame;
Zhu Yib481de92007-09-25 17:54:57 -07001124 struct list_head *element;
1125 if (list_empty(&priv->free_frames)) {
1126 frame = kzalloc(sizeof(*frame), GFP_KERNEL);
1127 if (!frame) {
1128 IWL_ERROR("Could not allocate frame!\n");
1129 return NULL;
1130 }
1131
1132 priv->frames_count++;
1133 return frame;
1134 }
1135
1136 element = priv->free_frames.next;
1137 list_del(element);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001138 return list_entry(element, struct iwl4965_frame, list);
Zhu Yib481de92007-09-25 17:54:57 -07001139}
1140
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001141static void iwl4965_free_frame(struct iwl_priv *priv, struct iwl4965_frame *frame)
Zhu Yib481de92007-09-25 17:54:57 -07001142{
1143 memset(frame, 0, sizeof(*frame));
1144 list_add(&frame->list, &priv->free_frames);
1145}
1146
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001147unsigned int iwl4965_fill_beacon_frame(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07001148 struct ieee80211_hdr *hdr,
1149 const u8 *dest, int left)
1150{
1151
Tomas Winkler3109ece2008-03-28 16:33:35 -07001152 if (!iwl_is_associated(priv) || !priv->ibss_beacon ||
Zhu Yib481de92007-09-25 17:54:57 -07001153 ((priv->iw_mode != IEEE80211_IF_TYPE_IBSS) &&
1154 (priv->iw_mode != IEEE80211_IF_TYPE_AP)))
1155 return 0;
1156
1157 if (priv->ibss_beacon->len > left)
1158 return 0;
1159
1160 memcpy(hdr, priv->ibss_beacon->data, priv->ibss_beacon->len);
1161
1162 return priv->ibss_beacon->len;
1163}
1164
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001165static u8 iwl4965_rate_get_lowest_plcp(int rate_mask)
Zhu Yib481de92007-09-25 17:54:57 -07001166{
1167 u8 i;
1168
1169 for (i = IWL_RATE_1M_INDEX; i != IWL_RATE_INVALID;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001170 i = iwl4965_rates[i].next_ieee) {
Zhu Yib481de92007-09-25 17:54:57 -07001171 if (rate_mask & (1 << i))
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001172 return iwl4965_rates[i].plcp;
Zhu Yib481de92007-09-25 17:54:57 -07001173 }
1174
1175 return IWL_RATE_INVALID;
1176}
1177
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001178static int iwl4965_send_beacon_cmd(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001179{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001180 struct iwl4965_frame *frame;
Zhu Yib481de92007-09-25 17:54:57 -07001181 unsigned int frame_size;
1182 int rc;
1183 u8 rate;
1184
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001185 frame = iwl4965_get_free_frame(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001186
1187 if (!frame) {
1188 IWL_ERROR("Could not obtain free frame buffer for beacon "
1189 "command.\n");
1190 return -ENOMEM;
1191 }
1192
1193 if (!(priv->staging_rxon.flags & RXON_FLG_BAND_24G_MSK)) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001194 rate = iwl4965_rate_get_lowest_plcp(priv->active_rate_basic &
Zhu Yib481de92007-09-25 17:54:57 -07001195 0xFF0);
1196 if (rate == IWL_INVALID_RATE)
1197 rate = IWL_RATE_6M_PLCP;
1198 } else {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001199 rate = iwl4965_rate_get_lowest_plcp(priv->active_rate_basic & 0xF);
Zhu Yib481de92007-09-25 17:54:57 -07001200 if (rate == IWL_INVALID_RATE)
1201 rate = IWL_RATE_1M_PLCP;
1202 }
1203
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001204 frame_size = iwl4965_hw_get_beacon_cmd(priv, frame, rate);
Zhu Yib481de92007-09-25 17:54:57 -07001205
Tomas Winkler857485c2008-03-21 13:53:44 -07001206 rc = iwl_send_cmd_pdu(priv, REPLY_TX_BEACON, frame_size,
Zhu Yib481de92007-09-25 17:54:57 -07001207 &frame->u.cmd[0]);
1208
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001209 iwl4965_free_frame(priv, frame);
Zhu Yib481de92007-09-25 17:54:57 -07001210
1211 return rc;
1212}
1213
1214/******************************************************************************
1215 *
Zhu Yib481de92007-09-25 17:54:57 -07001216 * Misc. internal state and helper functions
1217 *
1218 ******************************************************************************/
Zhu Yib481de92007-09-25 17:54:57 -07001219
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001220static void iwl4965_unset_hw_setting(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001221{
1222 if (priv->hw_setting.shared_virt)
1223 pci_free_consistent(priv->pci_dev,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001224 sizeof(struct iwl4965_shared),
Zhu Yib481de92007-09-25 17:54:57 -07001225 priv->hw_setting.shared_virt,
1226 priv->hw_setting.shared_phys);
1227}
1228
1229/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001230 * iwl4965_supported_rate_to_ie - fill in the supported rate in IE field
Zhu Yib481de92007-09-25 17:54:57 -07001231 *
1232 * return : set the bit for each supported rate insert in ie
1233 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001234static u16 iwl4965_supported_rate_to_ie(u8 *ie, u16 supported_rate,
Tomas Winklerc7c46672007-10-18 02:04:15 +02001235 u16 basic_rate, int *left)
Zhu Yib481de92007-09-25 17:54:57 -07001236{
1237 u16 ret_rates = 0, bit;
1238 int i;
Tomas Winklerc7c46672007-10-18 02:04:15 +02001239 u8 *cnt = ie;
1240 u8 *rates = ie + 1;
Zhu Yib481de92007-09-25 17:54:57 -07001241
1242 for (bit = 1, i = 0; i < IWL_RATE_COUNT; i++, bit <<= 1) {
1243 if (bit & supported_rate) {
1244 ret_rates |= bit;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001245 rates[*cnt] = iwl4965_rates[i].ieee |
Tomas Winklerc7c46672007-10-18 02:04:15 +02001246 ((bit & basic_rate) ? 0x80 : 0x00);
1247 (*cnt)++;
1248 (*left)--;
1249 if ((*left <= 0) ||
1250 (*cnt >= IWL_SUPPORTED_RATES_IE_LEN))
Zhu Yib481de92007-09-25 17:54:57 -07001251 break;
1252 }
1253 }
1254
1255 return ret_rates;
1256}
1257
Zhu Yib481de92007-09-25 17:54:57 -07001258/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001259 * iwl4965_fill_probe_req - fill in all required fields and IE for probe request
Zhu Yib481de92007-09-25 17:54:57 -07001260 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001261static u16 iwl4965_fill_probe_req(struct iwl_priv *priv,
Tomas Winkler78330fd2008-02-06 02:37:18 +02001262 enum ieee80211_band band,
1263 struct ieee80211_mgmt *frame,
1264 int left, int is_direct)
Zhu Yib481de92007-09-25 17:54:57 -07001265{
1266 int len = 0;
1267 u8 *pos = NULL;
mabbasbee488d2007-10-25 17:15:42 +08001268 u16 active_rates, ret_rates, cck_rates, active_rate_basic;
Ron Rindjunsky8fb88032007-11-26 16:14:38 +02001269#ifdef CONFIG_IWL4965_HT
Tomas Winkler78330fd2008-02-06 02:37:18 +02001270 const struct ieee80211_supported_band *sband =
1271 iwl4965_get_hw_mode(priv, band);
Ron Rindjunsky8fb88032007-11-26 16:14:38 +02001272#endif /* CONFIG_IWL4965_HT */
Zhu Yib481de92007-09-25 17:54:57 -07001273
1274 /* Make sure there is enough space for the probe request,
1275 * two mandatory IEs and the data */
1276 left -= 24;
1277 if (left < 0)
1278 return 0;
1279 len += 24;
1280
1281 frame->frame_control = cpu_to_le16(IEEE80211_STYPE_PROBE_REQ);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001282 memcpy(frame->da, iwl4965_broadcast_addr, ETH_ALEN);
Zhu Yib481de92007-09-25 17:54:57 -07001283 memcpy(frame->sa, priv->mac_addr, ETH_ALEN);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001284 memcpy(frame->bssid, iwl4965_broadcast_addr, ETH_ALEN);
Zhu Yib481de92007-09-25 17:54:57 -07001285 frame->seq_ctrl = 0;
1286
1287 /* fill in our indirect SSID IE */
1288 /* ...next IE... */
1289
1290 left -= 2;
1291 if (left < 0)
1292 return 0;
1293 len += 2;
1294 pos = &(frame->u.probe_req.variable[0]);
1295 *pos++ = WLAN_EID_SSID;
1296 *pos++ = 0;
1297
1298 /* fill in our direct SSID IE... */
1299 if (is_direct) {
1300 /* ...next IE... */
1301 left -= 2 + priv->essid_len;
1302 if (left < 0)
1303 return 0;
1304 /* ... fill it in... */
1305 *pos++ = WLAN_EID_SSID;
1306 *pos++ = priv->essid_len;
1307 memcpy(pos, priv->essid, priv->essid_len);
1308 pos += priv->essid_len;
1309 len += 2 + priv->essid_len;
1310 }
1311
1312 /* fill in supported rate */
1313 /* ...next IE... */
1314 left -= 2;
1315 if (left < 0)
1316 return 0;
Tomas Winklerc7c46672007-10-18 02:04:15 +02001317
Zhu Yib481de92007-09-25 17:54:57 -07001318 /* ... fill it in... */
1319 *pos++ = WLAN_EID_SUPP_RATES;
1320 *pos = 0;
Tomas Winklerc7c46672007-10-18 02:04:15 +02001321
mabbasbee488d2007-10-25 17:15:42 +08001322 /* exclude 60M rate */
1323 active_rates = priv->rates_mask;
1324 active_rates &= ~IWL_RATE_60M_MASK;
1325
1326 active_rate_basic = active_rates & IWL_BASIC_RATES_MASK;
Zhu Yib481de92007-09-25 17:54:57 -07001327
Tomas Winklerc7c46672007-10-18 02:04:15 +02001328 cck_rates = IWL_CCK_RATES_MASK & active_rates;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001329 ret_rates = iwl4965_supported_rate_to_ie(pos, cck_rates,
mabbasbee488d2007-10-25 17:15:42 +08001330 active_rate_basic, &left);
Tomas Winklerc7c46672007-10-18 02:04:15 +02001331 active_rates &= ~ret_rates;
1332
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001333 ret_rates = iwl4965_supported_rate_to_ie(pos, active_rates,
mabbasbee488d2007-10-25 17:15:42 +08001334 active_rate_basic, &left);
Tomas Winklerc7c46672007-10-18 02:04:15 +02001335 active_rates &= ~ret_rates;
1336
Zhu Yib481de92007-09-25 17:54:57 -07001337 len += 2 + *pos;
1338 pos += (*pos) + 1;
Tomas Winklerc7c46672007-10-18 02:04:15 +02001339 if (active_rates == 0)
Zhu Yib481de92007-09-25 17:54:57 -07001340 goto fill_end;
1341
1342 /* fill in supported extended rate */
1343 /* ...next IE... */
1344 left -= 2;
1345 if (left < 0)
1346 return 0;
1347 /* ... fill it in... */
1348 *pos++ = WLAN_EID_EXT_SUPP_RATES;
1349 *pos = 0;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001350 iwl4965_supported_rate_to_ie(pos, active_rates,
mabbasbee488d2007-10-25 17:15:42 +08001351 active_rate_basic, &left);
Zhu Yib481de92007-09-25 17:54:57 -07001352 if (*pos > 0)
1353 len += 2 + *pos;
1354
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08001355#ifdef CONFIG_IWL4965_HT
Tomas Winkler78330fd2008-02-06 02:37:18 +02001356 if (sband && sband->ht_info.ht_supported) {
1357 struct ieee80211_ht_cap *ht_cap;
Zhu Yib481de92007-09-25 17:54:57 -07001358 pos += (*pos) + 1;
1359 *pos++ = WLAN_EID_HT_CAPABILITY;
Ron Rindjunsky8fb88032007-11-26 16:14:38 +02001360 *pos++ = sizeof(struct ieee80211_ht_cap);
Tomas Winkler78330fd2008-02-06 02:37:18 +02001361 ht_cap = (struct ieee80211_ht_cap *)pos;
1362 ht_cap->cap_info = cpu_to_le16(sband->ht_info.cap);
1363 memcpy(ht_cap->supp_mcs_set, sband->ht_info.supp_mcs_set, 16);
1364 ht_cap->ampdu_params_info =(sband->ht_info.ampdu_factor &
1365 IEEE80211_HT_CAP_AMPDU_FACTOR) |
1366 ((sband->ht_info.ampdu_density << 2) &
1367 IEEE80211_HT_CAP_AMPDU_DENSITY);
Ron Rindjunsky8fb88032007-11-26 16:14:38 +02001368 len += 2 + sizeof(struct ieee80211_ht_cap);
Zhu Yib481de92007-09-25 17:54:57 -07001369 }
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08001370#endif /*CONFIG_IWL4965_HT */
Zhu Yib481de92007-09-25 17:54:57 -07001371
1372 fill_end:
1373 return (u16)len;
1374}
1375
1376/*
1377 * QoS support
1378*/
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001379static int iwl4965_send_qos_params_command(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001380 struct iwl4965_qosparam_cmd *qos)
Zhu Yib481de92007-09-25 17:54:57 -07001381{
1382
Tomas Winkler857485c2008-03-21 13:53:44 -07001383 return iwl_send_cmd_pdu(priv, REPLY_QOS_PARAM,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001384 sizeof(struct iwl4965_qosparam_cmd), qos);
Zhu Yib481de92007-09-25 17:54:57 -07001385}
1386
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001387static void iwl4965_activate_qos(struct iwl_priv *priv, u8 force)
Zhu Yib481de92007-09-25 17:54:57 -07001388{
1389 unsigned long flags;
1390
Zhu Yib481de92007-09-25 17:54:57 -07001391 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
1392 return;
1393
1394 if (!priv->qos_data.qos_enable)
1395 return;
1396
1397 spin_lock_irqsave(&priv->lock, flags);
1398 priv->qos_data.def_qos_parm.qos_flags = 0;
1399
1400 if (priv->qos_data.qos_cap.q_AP.queue_request &&
1401 !priv->qos_data.qos_cap.q_AP.txop_request)
1402 priv->qos_data.def_qos_parm.qos_flags |=
1403 QOS_PARAM_FLG_TXOP_TYPE_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07001404 if (priv->qos_data.qos_active)
1405 priv->qos_data.def_qos_parm.qos_flags |=
1406 QOS_PARAM_FLG_UPDATE_EDCA_MSK;
1407
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08001408#ifdef CONFIG_IWL4965_HT
Ron Rindjunskyfd105e72007-11-26 16:14:39 +02001409 if (priv->current_ht_config.is_ht)
Tomas Winklerf1f1f5c2007-10-25 17:15:26 +08001410 priv->qos_data.def_qos_parm.qos_flags |= QOS_PARAM_FLG_TGN_MSK;
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08001411#endif /* CONFIG_IWL4965_HT */
Tomas Winklerf1f1f5c2007-10-25 17:15:26 +08001412
Zhu Yib481de92007-09-25 17:54:57 -07001413 spin_unlock_irqrestore(&priv->lock, flags);
1414
Tomas Winkler3109ece2008-03-28 16:33:35 -07001415 if (force || iwl_is_associated(priv)) {
Tomas Winklerf1f1f5c2007-10-25 17:15:26 +08001416 IWL_DEBUG_QOS("send QoS cmd with Qos active=%d FLAGS=0x%X\n",
1417 priv->qos_data.qos_active,
1418 priv->qos_data.def_qos_parm.qos_flags);
Zhu Yib481de92007-09-25 17:54:57 -07001419
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001420 iwl4965_send_qos_params_command(priv,
Zhu Yib481de92007-09-25 17:54:57 -07001421 &(priv->qos_data.def_qos_parm));
1422 }
1423}
1424
Zhu Yib481de92007-09-25 17:54:57 -07001425/*
1426 * Power management (not Tx power!) functions
1427 */
1428#define MSEC_TO_USEC 1024
1429
1430#define NOSLP __constant_cpu_to_le16(0), 0, 0
1431#define SLP IWL_POWER_DRIVER_ALLOW_SLEEP_MSK, 0, 0
1432#define SLP_TIMEOUT(T) __constant_cpu_to_le32((T) * MSEC_TO_USEC)
1433#define SLP_VEC(X0, X1, X2, X3, X4) {__constant_cpu_to_le32(X0), \
1434 __constant_cpu_to_le32(X1), \
1435 __constant_cpu_to_le32(X2), \
1436 __constant_cpu_to_le32(X3), \
1437 __constant_cpu_to_le32(X4)}
1438
1439
1440/* default power management (not Tx power) table values */
1441/* for tim 0-10 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001442static struct iwl4965_power_vec_entry range_0[IWL_POWER_AC] = {
Zhu Yib481de92007-09-25 17:54:57 -07001443 {{NOSLP, SLP_TIMEOUT(0), SLP_TIMEOUT(0), SLP_VEC(0, 0, 0, 0, 0)}, 0},
1444 {{SLP, SLP_TIMEOUT(200), SLP_TIMEOUT(500), SLP_VEC(1, 2, 3, 4, 4)}, 0},
1445 {{SLP, SLP_TIMEOUT(200), SLP_TIMEOUT(300), SLP_VEC(2, 4, 6, 7, 7)}, 0},
1446 {{SLP, SLP_TIMEOUT(50), SLP_TIMEOUT(100), SLP_VEC(2, 6, 9, 9, 10)}, 0},
1447 {{SLP, SLP_TIMEOUT(50), SLP_TIMEOUT(25), SLP_VEC(2, 7, 9, 9, 10)}, 1},
1448 {{SLP, SLP_TIMEOUT(25), SLP_TIMEOUT(25), SLP_VEC(4, 7, 10, 10, 10)}, 1}
1449};
1450
1451/* for tim > 10 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001452static struct iwl4965_power_vec_entry range_1[IWL_POWER_AC] = {
Zhu Yib481de92007-09-25 17:54:57 -07001453 {{NOSLP, SLP_TIMEOUT(0), SLP_TIMEOUT(0), SLP_VEC(0, 0, 0, 0, 0)}, 0},
1454 {{SLP, SLP_TIMEOUT(200), SLP_TIMEOUT(500),
1455 SLP_VEC(1, 2, 3, 4, 0xFF)}, 0},
1456 {{SLP, SLP_TIMEOUT(200), SLP_TIMEOUT(300),
1457 SLP_VEC(2, 4, 6, 7, 0xFF)}, 0},
1458 {{SLP, SLP_TIMEOUT(50), SLP_TIMEOUT(100),
1459 SLP_VEC(2, 6, 9, 9, 0xFF)}, 0},
1460 {{SLP, SLP_TIMEOUT(50), SLP_TIMEOUT(25), SLP_VEC(2, 7, 9, 9, 0xFF)}, 0},
1461 {{SLP, SLP_TIMEOUT(25), SLP_TIMEOUT(25),
1462 SLP_VEC(4, 7, 10, 10, 0xFF)}, 0}
1463};
1464
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001465int iwl4965_power_init_handle(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001466{
1467 int rc = 0, i;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001468 struct iwl4965_power_mgr *pow_data;
1469 int size = sizeof(struct iwl4965_power_vec_entry) * IWL_POWER_AC;
Zhu Yib481de92007-09-25 17:54:57 -07001470 u16 pci_pm;
1471
1472 IWL_DEBUG_POWER("Initialize power \n");
1473
1474 pow_data = &(priv->power_data);
1475
1476 memset(pow_data, 0, sizeof(*pow_data));
1477
1478 pow_data->active_index = IWL_POWER_RANGE_0;
1479 pow_data->dtim_val = 0xffff;
1480
1481 memcpy(&pow_data->pwr_range_0[0], &range_0[0], size);
1482 memcpy(&pow_data->pwr_range_1[0], &range_1[0], size);
1483
1484 rc = pci_read_config_word(priv->pci_dev, PCI_LINK_CTRL, &pci_pm);
1485 if (rc != 0)
1486 return 0;
1487 else {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001488 struct iwl4965_powertable_cmd *cmd;
Zhu Yib481de92007-09-25 17:54:57 -07001489
1490 IWL_DEBUG_POWER("adjust power command flags\n");
1491
1492 for (i = 0; i < IWL_POWER_AC; i++) {
1493 cmd = &pow_data->pwr_range_0[i].cmd;
1494
1495 if (pci_pm & 0x1)
1496 cmd->flags &= ~IWL_POWER_PCI_PM_MSK;
1497 else
1498 cmd->flags |= IWL_POWER_PCI_PM_MSK;
1499 }
1500 }
1501 return rc;
1502}
1503
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001504static int iwl4965_update_power_cmd(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001505 struct iwl4965_powertable_cmd *cmd, u32 mode)
Zhu Yib481de92007-09-25 17:54:57 -07001506{
1507 int rc = 0, i;
1508 u8 skip;
1509 u32 max_sleep = 0;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001510 struct iwl4965_power_vec_entry *range;
Zhu Yib481de92007-09-25 17:54:57 -07001511 u8 period = 0;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001512 struct iwl4965_power_mgr *pow_data;
Zhu Yib481de92007-09-25 17:54:57 -07001513
1514 if (mode > IWL_POWER_INDEX_5) {
1515 IWL_DEBUG_POWER("Error invalid power mode \n");
1516 return -1;
1517 }
1518 pow_data = &(priv->power_data);
1519
1520 if (pow_data->active_index == IWL_POWER_RANGE_0)
1521 range = &pow_data->pwr_range_0[0];
1522 else
1523 range = &pow_data->pwr_range_1[1];
1524
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001525 memcpy(cmd, &range[mode].cmd, sizeof(struct iwl4965_powertable_cmd));
Zhu Yib481de92007-09-25 17:54:57 -07001526
1527#ifdef IWL_MAC80211_DISABLE
1528 if (priv->assoc_network != NULL) {
1529 unsigned long flags;
1530
1531 period = priv->assoc_network->tim.tim_period;
1532 }
1533#endif /*IWL_MAC80211_DISABLE */
1534 skip = range[mode].no_dtim;
1535
1536 if (period == 0) {
1537 period = 1;
1538 skip = 0;
1539 }
1540
1541 if (skip == 0) {
1542 max_sleep = period;
1543 cmd->flags &= ~IWL_POWER_SLEEP_OVER_DTIM_MSK;
1544 } else {
1545 __le32 slp_itrvl = cmd->sleep_interval[IWL_POWER_VEC_SIZE - 1];
1546 max_sleep = (le32_to_cpu(slp_itrvl) / period) * period;
1547 cmd->flags |= IWL_POWER_SLEEP_OVER_DTIM_MSK;
1548 }
1549
1550 for (i = 0; i < IWL_POWER_VEC_SIZE; i++) {
1551 if (le32_to_cpu(cmd->sleep_interval[i]) > max_sleep)
1552 cmd->sleep_interval[i] = cpu_to_le32(max_sleep);
1553 }
1554
1555 IWL_DEBUG_POWER("Flags value = 0x%08X\n", cmd->flags);
1556 IWL_DEBUG_POWER("Tx timeout = %u\n", le32_to_cpu(cmd->tx_data_timeout));
1557 IWL_DEBUG_POWER("Rx timeout = %u\n", le32_to_cpu(cmd->rx_data_timeout));
1558 IWL_DEBUG_POWER("Sleep interval vector = { %d , %d , %d , %d , %d }\n",
1559 le32_to_cpu(cmd->sleep_interval[0]),
1560 le32_to_cpu(cmd->sleep_interval[1]),
1561 le32_to_cpu(cmd->sleep_interval[2]),
1562 le32_to_cpu(cmd->sleep_interval[3]),
1563 le32_to_cpu(cmd->sleep_interval[4]));
1564
1565 return rc;
1566}
1567
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001568static int iwl4965_send_power_mode(struct iwl_priv *priv, u32 mode)
Zhu Yib481de92007-09-25 17:54:57 -07001569{
John W. Linville9a62f732007-11-15 16:27:36 -05001570 u32 uninitialized_var(final_mode);
Zhu Yib481de92007-09-25 17:54:57 -07001571 int rc;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001572 struct iwl4965_powertable_cmd cmd;
Zhu Yib481de92007-09-25 17:54:57 -07001573
1574 /* If on battery, set to 3,
Ian Schram01ebd062007-10-25 17:15:22 +08001575 * if plugged into AC power, set to CAM ("continuously aware mode"),
Zhu Yib481de92007-09-25 17:54:57 -07001576 * else user level */
1577 switch (mode) {
1578 case IWL_POWER_BATTERY:
1579 final_mode = IWL_POWER_INDEX_3;
1580 break;
1581 case IWL_POWER_AC:
1582 final_mode = IWL_POWER_MODE_CAM;
1583 break;
1584 default:
1585 final_mode = mode;
1586 break;
1587 }
1588
1589 cmd.keep_alive_beacons = 0;
1590
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001591 iwl4965_update_power_cmd(priv, &cmd, final_mode);
Zhu Yib481de92007-09-25 17:54:57 -07001592
Tomas Winkler857485c2008-03-21 13:53:44 -07001593 rc = iwl_send_cmd_pdu(priv, POWER_TABLE_CMD, sizeof(cmd), &cmd);
Zhu Yib481de92007-09-25 17:54:57 -07001594
1595 if (final_mode == IWL_POWER_MODE_CAM)
1596 clear_bit(STATUS_POWER_PMI, &priv->status);
1597 else
1598 set_bit(STATUS_POWER_PMI, &priv->status);
1599
1600 return rc;
1601}
1602
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001603int iwl4965_is_network_packet(struct iwl_priv *priv, struct ieee80211_hdr *header)
Zhu Yib481de92007-09-25 17:54:57 -07001604{
1605 /* Filter incoming packets to determine if they are targeted toward
1606 * this network, discarding packets coming from ourselves */
1607 switch (priv->iw_mode) {
1608 case IEEE80211_IF_TYPE_IBSS: /* Header: Dest. | Source | BSSID */
1609 /* packets from our adapter are dropped (echo) */
1610 if (!compare_ether_addr(header->addr2, priv->mac_addr))
1611 return 0;
1612 /* {broad,multi}cast packets to our IBSS go through */
1613 if (is_multicast_ether_addr(header->addr1))
1614 return !compare_ether_addr(header->addr3, priv->bssid);
1615 /* packets to our adapter go through */
1616 return !compare_ether_addr(header->addr1, priv->mac_addr);
1617 case IEEE80211_IF_TYPE_STA: /* Header: Dest. | AP{BSSID} | Source */
1618 /* packets from our adapter are dropped (echo) */
1619 if (!compare_ether_addr(header->addr3, priv->mac_addr))
1620 return 0;
1621 /* {broad,multi}cast packets to our BSS go through */
1622 if (is_multicast_ether_addr(header->addr1))
1623 return !compare_ether_addr(header->addr2, priv->bssid);
1624 /* packets to our adapter go through */
1625 return !compare_ether_addr(header->addr1, priv->mac_addr);
Tomas Winkler69dc5d92008-03-25 16:33:41 -07001626 default:
1627 break;
Zhu Yib481de92007-09-25 17:54:57 -07001628 }
1629
1630 return 1;
1631}
1632
1633#define TX_STATUS_ENTRY(x) case TX_STATUS_FAIL_ ## x: return #x
1634
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001635static const char *iwl4965_get_tx_fail_reason(u32 status)
Zhu Yib481de92007-09-25 17:54:57 -07001636{
1637 switch (status & TX_STATUS_MSK) {
1638 case TX_STATUS_SUCCESS:
1639 return "SUCCESS";
1640 TX_STATUS_ENTRY(SHORT_LIMIT);
1641 TX_STATUS_ENTRY(LONG_LIMIT);
1642 TX_STATUS_ENTRY(FIFO_UNDERRUN);
1643 TX_STATUS_ENTRY(MGMNT_ABORT);
1644 TX_STATUS_ENTRY(NEXT_FRAG);
1645 TX_STATUS_ENTRY(LIFE_EXPIRE);
1646 TX_STATUS_ENTRY(DEST_PS);
1647 TX_STATUS_ENTRY(ABORTED);
1648 TX_STATUS_ENTRY(BT_RETRY);
1649 TX_STATUS_ENTRY(STA_INVALID);
1650 TX_STATUS_ENTRY(FRAG_DROPPED);
1651 TX_STATUS_ENTRY(TID_DISABLE);
1652 TX_STATUS_ENTRY(FRAME_FLUSHED);
1653 TX_STATUS_ENTRY(INSUFFICIENT_CF_POLL);
1654 TX_STATUS_ENTRY(TX_LOCKED);
1655 TX_STATUS_ENTRY(NO_BEACON_ON_RADAR);
1656 }
1657
1658 return "UNKNOWN";
1659}
1660
1661/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001662 * iwl4965_scan_cancel - Cancel any currently executing HW scan
Zhu Yib481de92007-09-25 17:54:57 -07001663 *
1664 * NOTE: priv->mutex is not required before calling this function
1665 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001666static int iwl4965_scan_cancel(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001667{
1668 if (!test_bit(STATUS_SCAN_HW, &priv->status)) {
1669 clear_bit(STATUS_SCANNING, &priv->status);
1670 return 0;
1671 }
1672
1673 if (test_bit(STATUS_SCANNING, &priv->status)) {
1674 if (!test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
1675 IWL_DEBUG_SCAN("Queuing scan abort.\n");
1676 set_bit(STATUS_SCAN_ABORTING, &priv->status);
1677 queue_work(priv->workqueue, &priv->abort_scan);
1678
1679 } else
1680 IWL_DEBUG_SCAN("Scan abort already in progress.\n");
1681
1682 return test_bit(STATUS_SCANNING, &priv->status);
1683 }
1684
1685 return 0;
1686}
1687
1688/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001689 * iwl4965_scan_cancel_timeout - Cancel any currently executing HW scan
Zhu Yib481de92007-09-25 17:54:57 -07001690 * @ms: amount of time to wait (in milliseconds) for scan to abort
1691 *
1692 * NOTE: priv->mutex must be held before calling this function
1693 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001694static int iwl4965_scan_cancel_timeout(struct iwl_priv *priv, unsigned long ms)
Zhu Yib481de92007-09-25 17:54:57 -07001695{
1696 unsigned long now = jiffies;
1697 int ret;
1698
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001699 ret = iwl4965_scan_cancel(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001700 if (ret && ms) {
1701 mutex_unlock(&priv->mutex);
1702 while (!time_after(jiffies, now + msecs_to_jiffies(ms)) &&
1703 test_bit(STATUS_SCANNING, &priv->status))
1704 msleep(1);
1705 mutex_lock(&priv->mutex);
1706
1707 return test_bit(STATUS_SCANNING, &priv->status);
1708 }
1709
1710 return ret;
1711}
1712
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001713static void iwl4965_sequence_reset(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001714{
1715 /* Reset ieee stats */
1716
1717 /* We don't reset the net_device_stats (ieee->stats) on
1718 * re-association */
1719
1720 priv->last_seq_num = -1;
1721 priv->last_frag_num = -1;
1722 priv->last_packet_time = 0;
1723
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001724 iwl4965_scan_cancel(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001725}
1726
1727#define MAX_UCODE_BEACON_INTERVAL 4096
1728#define INTEL_CONN_LISTEN_INTERVAL __constant_cpu_to_le16(0xA)
1729
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001730static __le16 iwl4965_adjust_beacon_interval(u16 beacon_val)
Zhu Yib481de92007-09-25 17:54:57 -07001731{
1732 u16 new_val = 0;
1733 u16 beacon_factor = 0;
1734
1735 beacon_factor =
1736 (beacon_val + MAX_UCODE_BEACON_INTERVAL)
1737 / MAX_UCODE_BEACON_INTERVAL;
1738 new_val = beacon_val / beacon_factor;
1739
1740 return cpu_to_le16(new_val);
1741}
1742
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001743static void iwl4965_setup_rxon_timing(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001744{
1745 u64 interval_tm_unit;
1746 u64 tsf, result;
1747 unsigned long flags;
1748 struct ieee80211_conf *conf = NULL;
1749 u16 beacon_int = 0;
1750
1751 conf = ieee80211_get_hw_conf(priv->hw);
1752
1753 spin_lock_irqsave(&priv->lock, flags);
Tomas Winkler3109ece2008-03-28 16:33:35 -07001754 priv->rxon_timing.timestamp.dw[1] = cpu_to_le32(priv->timestamp >> 32);
1755 priv->rxon_timing.timestamp.dw[0] =
1756 cpu_to_le32(priv->timestamp & 0xFFFFFFFF);
Zhu Yib481de92007-09-25 17:54:57 -07001757
1758 priv->rxon_timing.listen_interval = INTEL_CONN_LISTEN_INTERVAL;
1759
Tomas Winkler3109ece2008-03-28 16:33:35 -07001760 tsf = priv->timestamp;
Zhu Yib481de92007-09-25 17:54:57 -07001761
1762 beacon_int = priv->beacon_int;
1763 spin_unlock_irqrestore(&priv->lock, flags);
1764
1765 if (priv->iw_mode == IEEE80211_IF_TYPE_STA) {
1766 if (beacon_int == 0) {
1767 priv->rxon_timing.beacon_interval = cpu_to_le16(100);
1768 priv->rxon_timing.beacon_init_val = cpu_to_le32(102400);
1769 } else {
1770 priv->rxon_timing.beacon_interval =
1771 cpu_to_le16(beacon_int);
1772 priv->rxon_timing.beacon_interval =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001773 iwl4965_adjust_beacon_interval(
Zhu Yib481de92007-09-25 17:54:57 -07001774 le16_to_cpu(priv->rxon_timing.beacon_interval));
1775 }
1776
1777 priv->rxon_timing.atim_window = 0;
1778 } else {
1779 priv->rxon_timing.beacon_interval =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001780 iwl4965_adjust_beacon_interval(conf->beacon_int);
Zhu Yib481de92007-09-25 17:54:57 -07001781 /* TODO: we need to get atim_window from upper stack
1782 * for now we set to 0 */
1783 priv->rxon_timing.atim_window = 0;
1784 }
1785
1786 interval_tm_unit =
1787 (le16_to_cpu(priv->rxon_timing.beacon_interval) * 1024);
1788 result = do_div(tsf, interval_tm_unit);
1789 priv->rxon_timing.beacon_init_val =
1790 cpu_to_le32((u32) ((u64) interval_tm_unit - result));
1791
1792 IWL_DEBUG_ASSOC
1793 ("beacon interval %d beacon timer %d beacon tim %d\n",
1794 le16_to_cpu(priv->rxon_timing.beacon_interval),
1795 le32_to_cpu(priv->rxon_timing.beacon_init_val),
1796 le16_to_cpu(priv->rxon_timing.atim_window));
1797}
1798
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001799static int iwl4965_scan_initiate(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001800{
1801 if (priv->iw_mode == IEEE80211_IF_TYPE_AP) {
1802 IWL_ERROR("APs don't scan.\n");
1803 return 0;
1804 }
1805
Tomas Winklerfee12472008-04-03 16:05:21 -07001806 if (!iwl_is_ready_rf(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07001807 IWL_DEBUG_SCAN("Aborting scan due to not ready.\n");
1808 return -EIO;
1809 }
1810
1811 if (test_bit(STATUS_SCANNING, &priv->status)) {
1812 IWL_DEBUG_SCAN("Scan already in progress.\n");
1813 return -EAGAIN;
1814 }
1815
1816 if (test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
1817 IWL_DEBUG_SCAN("Scan request while abort pending. "
1818 "Queuing.\n");
1819 return -EAGAIN;
1820 }
1821
1822 IWL_DEBUG_INFO("Starting scan...\n");
1823 priv->scan_bands = 2;
1824 set_bit(STATUS_SCANNING, &priv->status);
1825 priv->scan_start = jiffies;
1826 priv->scan_pass_start = priv->scan_start;
1827
1828 queue_work(priv->workqueue, &priv->request_scan);
1829
1830 return 0;
1831}
1832
Zhu Yib481de92007-09-25 17:54:57 -07001833
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001834static void iwl4965_set_flags_for_phymode(struct iwl_priv *priv,
Johannes Berg8318d782008-01-24 19:38:38 +01001835 enum ieee80211_band band)
Zhu Yib481de92007-09-25 17:54:57 -07001836{
Johannes Berg8318d782008-01-24 19:38:38 +01001837 if (band == IEEE80211_BAND_5GHZ) {
Zhu Yib481de92007-09-25 17:54:57 -07001838 priv->staging_rxon.flags &=
1839 ~(RXON_FLG_BAND_24G_MSK | RXON_FLG_AUTO_DETECT_MSK
1840 | RXON_FLG_CCK_MSK);
1841 priv->staging_rxon.flags |= RXON_FLG_SHORT_SLOT_MSK;
1842 } else {
Reinette Chatre508e32e2008-04-14 21:16:13 -07001843 /* Copied from iwl4965_post_associate() */
Zhu Yib481de92007-09-25 17:54:57 -07001844 if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_SLOT_TIME)
1845 priv->staging_rxon.flags |= RXON_FLG_SHORT_SLOT_MSK;
1846 else
1847 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
1848
1849 if (priv->iw_mode == IEEE80211_IF_TYPE_IBSS)
1850 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
1851
1852 priv->staging_rxon.flags |= RXON_FLG_BAND_24G_MSK;
1853 priv->staging_rxon.flags |= RXON_FLG_AUTO_DETECT_MSK;
1854 priv->staging_rxon.flags &= ~RXON_FLG_CCK_MSK;
1855 }
1856}
1857
1858/*
Ian Schram01ebd062007-10-25 17:15:22 +08001859 * initialize rxon structure with default values from eeprom
Zhu Yib481de92007-09-25 17:54:57 -07001860 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001861static void iwl4965_connection_init_rx_config(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001862{
Assaf Kraussbf85ea42008-03-14 10:38:49 -07001863 const struct iwl_channel_info *ch_info;
Zhu Yib481de92007-09-25 17:54:57 -07001864
1865 memset(&priv->staging_rxon, 0, sizeof(priv->staging_rxon));
1866
1867 switch (priv->iw_mode) {
1868 case IEEE80211_IF_TYPE_AP:
1869 priv->staging_rxon.dev_type = RXON_DEV_TYPE_AP;
1870 break;
1871
1872 case IEEE80211_IF_TYPE_STA:
1873 priv->staging_rxon.dev_type = RXON_DEV_TYPE_ESS;
1874 priv->staging_rxon.filter_flags = RXON_FILTER_ACCEPT_GRP_MSK;
1875 break;
1876
1877 case IEEE80211_IF_TYPE_IBSS:
1878 priv->staging_rxon.dev_type = RXON_DEV_TYPE_IBSS;
1879 priv->staging_rxon.flags = RXON_FLG_SHORT_PREAMBLE_MSK;
1880 priv->staging_rxon.filter_flags = RXON_FILTER_BCON_AWARE_MSK |
1881 RXON_FILTER_ACCEPT_GRP_MSK;
1882 break;
1883
1884 case IEEE80211_IF_TYPE_MNTR:
1885 priv->staging_rxon.dev_type = RXON_DEV_TYPE_SNIFFER;
1886 priv->staging_rxon.filter_flags = RXON_FILTER_PROMISC_MSK |
1887 RXON_FILTER_CTL2HOST_MSK | RXON_FILTER_ACCEPT_GRP_MSK;
1888 break;
Tomas Winkler69dc5d92008-03-25 16:33:41 -07001889 default:
1890 IWL_ERROR("Unsupported interface type %d\n", priv->iw_mode);
1891 break;
Zhu Yib481de92007-09-25 17:54:57 -07001892 }
1893
1894#if 0
1895 /* TODO: Figure out when short_preamble would be set and cache from
1896 * that */
1897 if (!hw_to_local(priv->hw)->short_preamble)
1898 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_PREAMBLE_MSK;
1899 else
1900 priv->staging_rxon.flags |= RXON_FLG_SHORT_PREAMBLE_MSK;
1901#endif
1902
Assaf Krauss8622e702008-03-21 13:53:43 -07001903 ch_info = iwl_get_channel_info(priv, priv->band,
Zhu Yib481de92007-09-25 17:54:57 -07001904 le16_to_cpu(priv->staging_rxon.channel));
1905
1906 if (!ch_info)
1907 ch_info = &priv->channel_info[0];
1908
1909 /*
1910 * in some case A channels are all non IBSS
1911 * in this case force B/G channel
1912 */
1913 if ((priv->iw_mode == IEEE80211_IF_TYPE_IBSS) &&
1914 !(is_channel_ibss(ch_info)))
1915 ch_info = &priv->channel_info[0];
1916
1917 priv->staging_rxon.channel = cpu_to_le16(ch_info->channel);
Johannes Berg8318d782008-01-24 19:38:38 +01001918 priv->band = ch_info->band;
Zhu Yib481de92007-09-25 17:54:57 -07001919
Johannes Berg8318d782008-01-24 19:38:38 +01001920 iwl4965_set_flags_for_phymode(priv, priv->band);
Zhu Yib481de92007-09-25 17:54:57 -07001921
1922 priv->staging_rxon.ofdm_basic_rates =
1923 (IWL_OFDM_RATES_MASK >> IWL_FIRST_OFDM_RATE) & 0xFF;
1924 priv->staging_rxon.cck_basic_rates =
1925 (IWL_CCK_RATES_MASK >> IWL_FIRST_CCK_RATE) & 0xF;
1926
1927 priv->staging_rxon.flags &= ~(RXON_FLG_CHANNEL_MODE_MIXED_MSK |
1928 RXON_FLG_CHANNEL_MODE_PURE_40_MSK);
1929 memcpy(priv->staging_rxon.node_addr, priv->mac_addr, ETH_ALEN);
1930 memcpy(priv->staging_rxon.wlap_bssid_addr, priv->mac_addr, ETH_ALEN);
1931 priv->staging_rxon.ofdm_ht_single_stream_basic_rates = 0xff;
1932 priv->staging_rxon.ofdm_ht_dual_stream_basic_rates = 0xff;
1933 iwl4965_set_rxon_chain(priv);
1934}
1935
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001936static int iwl4965_set_mode(struct iwl_priv *priv, int mode)
Zhu Yib481de92007-09-25 17:54:57 -07001937{
Zhu Yib481de92007-09-25 17:54:57 -07001938 if (mode == IEEE80211_IF_TYPE_IBSS) {
Assaf Kraussbf85ea42008-03-14 10:38:49 -07001939 const struct iwl_channel_info *ch_info;
Zhu Yib481de92007-09-25 17:54:57 -07001940
Assaf Krauss8622e702008-03-21 13:53:43 -07001941 ch_info = iwl_get_channel_info(priv,
Johannes Berg8318d782008-01-24 19:38:38 +01001942 priv->band,
Zhu Yib481de92007-09-25 17:54:57 -07001943 le16_to_cpu(priv->staging_rxon.channel));
1944
1945 if (!ch_info || !is_channel_ibss(ch_info)) {
1946 IWL_ERROR("channel %d not IBSS channel\n",
1947 le16_to_cpu(priv->staging_rxon.channel));
1948 return -EINVAL;
1949 }
1950 }
1951
Zhu Yib481de92007-09-25 17:54:57 -07001952 priv->iw_mode = mode;
1953
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001954 iwl4965_connection_init_rx_config(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001955 memcpy(priv->staging_rxon.node_addr, priv->mac_addr, ETH_ALEN);
1956
Assaf Kraussbf85ea42008-03-14 10:38:49 -07001957 iwlcore_clear_stations_table(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001958
Mohamed Abbasfde35712007-11-29 11:10:15 +08001959 /* dont commit rxon if rf-kill is on*/
Tomas Winklerfee12472008-04-03 16:05:21 -07001960 if (!iwl_is_ready_rf(priv))
Mohamed Abbasfde35712007-11-29 11:10:15 +08001961 return -EAGAIN;
1962
1963 cancel_delayed_work(&priv->scan_check);
1964 if (iwl4965_scan_cancel_timeout(priv, 100)) {
1965 IWL_WARNING("Aborted scan still in progress after 100ms\n");
1966 IWL_DEBUG_MAC80211("leaving - scan abort failed.\n");
1967 return -EAGAIN;
1968 }
1969
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001970 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001971
1972 return 0;
1973}
1974
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001975static void iwl4965_build_tx_cmd_hwcrypto(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07001976 struct ieee80211_tx_control *ctl,
Tomas Winkler857485c2008-03-21 13:53:44 -07001977 struct iwl_cmd *cmd,
Zhu Yib481de92007-09-25 17:54:57 -07001978 struct sk_buff *skb_frag,
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07001979 int sta_id)
Zhu Yib481de92007-09-25 17:54:57 -07001980{
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07001981 struct iwl4965_hw_key *keyinfo = &priv->stations[sta_id].keyinfo;
Emmanuel Grumbach6974e362008-04-14 21:16:06 -07001982 struct iwl_wep_key *wepkey;
1983 int keyidx = 0;
1984
1985 BUG_ON(ctl->key_idx > 3);
Zhu Yib481de92007-09-25 17:54:57 -07001986
1987 switch (keyinfo->alg) {
1988 case ALG_CCMP:
1989 cmd->cmd.tx.sec_ctl = TX_CMD_SEC_CCM;
1990 memcpy(cmd->cmd.tx.key, keyinfo->key, keyinfo->keylen);
Max Stepanov8236e182008-03-12 16:58:48 -07001991 if (ctl->flags & IEEE80211_TXCTL_AMPDU)
1992 cmd->cmd.tx.tx_flags |= TX_CMD_FLG_AGG_CCMP_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07001993 IWL_DEBUG_TX("tx_cmd with aes hwcrypto\n");
1994 break;
1995
1996 case ALG_TKIP:
Zhu Yib481de92007-09-25 17:54:57 -07001997 cmd->cmd.tx.sec_ctl = TX_CMD_SEC_TKIP;
Emmanuel Grumbach2bc75082008-03-19 16:41:45 -07001998 ieee80211_get_tkip_key(keyinfo->conf, skb_frag,
1999 IEEE80211_TKIP_P2_KEY, cmd->cmd.tx.key);
2000 IWL_DEBUG_TX("tx_cmd with tkip hwcrypto\n");
Zhu Yib481de92007-09-25 17:54:57 -07002001 break;
2002
2003 case ALG_WEP:
Emmanuel Grumbach6974e362008-04-14 21:16:06 -07002004 wepkey = &priv->wep_keys[ctl->key_idx];
2005 cmd->cmd.tx.sec_ctl = 0;
2006 if (priv->default_wep_key) {
2007 /* the WEP key was sent as static */
2008 keyidx = ctl->key_idx;
2009 memcpy(&cmd->cmd.tx.key[3], wepkey->key,
2010 wepkey->key_size);
2011 if (wepkey->key_size == WEP_KEY_LEN_128)
2012 cmd->cmd.tx.sec_ctl |= TX_CMD_SEC_KEY128;
2013 } else {
Emmanuel Grumbach0211ddd2008-04-14 21:16:07 -07002014 /* the WEP key was sent as dynamic */
2015 keyidx = keyinfo->keyidx;
2016 memcpy(&cmd->cmd.tx.key[3], keyinfo->key,
2017 keyinfo->keylen);
2018 if (keyinfo->keylen == WEP_KEY_LEN_128)
2019 cmd->cmd.tx.sec_ctl |= TX_CMD_SEC_KEY128;
Emmanuel Grumbach6974e362008-04-14 21:16:06 -07002020 }
Zhu Yib481de92007-09-25 17:54:57 -07002021
Emmanuel Grumbach6974e362008-04-14 21:16:06 -07002022 cmd->cmd.tx.sec_ctl |= (TX_CMD_SEC_WEP |
2023 (keyidx & TX_CMD_SEC_MSK) << TX_CMD_SEC_SHIFT);
Zhu Yib481de92007-09-25 17:54:57 -07002024
2025 IWL_DEBUG_TX("Configuring packet for WEP encryption "
Emmanuel Grumbach6974e362008-04-14 21:16:06 -07002026 "with key %d\n", keyidx);
Zhu Yib481de92007-09-25 17:54:57 -07002027 break;
2028
Zhu Yib481de92007-09-25 17:54:57 -07002029 default:
2030 printk(KERN_ERR "Unknown encode alg %d\n", keyinfo->alg);
2031 break;
2032 }
2033}
2034
2035/*
2036 * handle build REPLY_TX command notification.
2037 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002038static void iwl4965_build_tx_cmd_basic(struct iwl_priv *priv,
Tomas Winkler857485c2008-03-21 13:53:44 -07002039 struct iwl_cmd *cmd,
Zhu Yib481de92007-09-25 17:54:57 -07002040 struct ieee80211_tx_control *ctrl,
2041 struct ieee80211_hdr *hdr,
2042 int is_unicast, u8 std_id)
2043{
2044 __le16 *qc;
2045 u16 fc = le16_to_cpu(hdr->frame_control);
2046 __le32 tx_flags = cmd->cmd.tx.tx_flags;
2047
2048 cmd->cmd.tx.stop_time.life_time = TX_CMD_LIFE_TIME_INFINITE;
2049 if (!(ctrl->flags & IEEE80211_TXCTL_NO_ACK)) {
2050 tx_flags |= TX_CMD_FLG_ACK_MSK;
2051 if ((fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_MGMT)
2052 tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
2053 if (ieee80211_is_probe_response(fc) &&
2054 !(le16_to_cpu(hdr->seq_ctrl) & 0xf))
2055 tx_flags |= TX_CMD_FLG_TSF_MSK;
2056 } else {
2057 tx_flags &= (~TX_CMD_FLG_ACK_MSK);
2058 tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
2059 }
2060
Tomas Winkler87e4f7d2008-01-14 17:46:16 -08002061 if (ieee80211_is_back_request(fc))
2062 tx_flags |= TX_CMD_FLG_ACK_MSK | TX_CMD_FLG_IMM_BA_RSP_MASK;
2063
2064
Zhu Yib481de92007-09-25 17:54:57 -07002065 cmd->cmd.tx.sta_id = std_id;
2066 if (ieee80211_get_morefrag(hdr))
2067 tx_flags |= TX_CMD_FLG_MORE_FRAG_MSK;
2068
2069 qc = ieee80211_get_qos_ctrl(hdr);
2070 if (qc) {
2071 cmd->cmd.tx.tid_tspec = (u8) (le16_to_cpu(*qc) & 0xf);
2072 tx_flags &= ~TX_CMD_FLG_SEQ_CTL_MSK;
2073 } else
2074 tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
2075
2076 if (ctrl->flags & IEEE80211_TXCTL_USE_RTS_CTS) {
2077 tx_flags |= TX_CMD_FLG_RTS_MSK;
2078 tx_flags &= ~TX_CMD_FLG_CTS_MSK;
2079 } else if (ctrl->flags & IEEE80211_TXCTL_USE_CTS_PROTECT) {
2080 tx_flags &= ~TX_CMD_FLG_RTS_MSK;
2081 tx_flags |= TX_CMD_FLG_CTS_MSK;
2082 }
2083
2084 if ((tx_flags & TX_CMD_FLG_RTS_MSK) || (tx_flags & TX_CMD_FLG_CTS_MSK))
2085 tx_flags |= TX_CMD_FLG_FULL_TXOP_PROT_MSK;
2086
2087 tx_flags &= ~(TX_CMD_FLG_ANT_SEL_MSK);
2088 if ((fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_MGMT) {
2089 if ((fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_ASSOC_REQ ||
2090 (fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_REASSOC_REQ)
Ian Schrambc434dd2007-10-25 17:15:29 +08002091 cmd->cmd.tx.timeout.pm_frame_timeout = cpu_to_le16(3);
Zhu Yib481de92007-09-25 17:54:57 -07002092 else
Ian Schrambc434dd2007-10-25 17:15:29 +08002093 cmd->cmd.tx.timeout.pm_frame_timeout = cpu_to_le16(2);
Mohamed Abbasab53d8a2008-03-25 16:33:36 -07002094 } else {
Zhu Yib481de92007-09-25 17:54:57 -07002095 cmd->cmd.tx.timeout.pm_frame_timeout = 0;
Mohamed Abbasab53d8a2008-03-25 16:33:36 -07002096 }
Zhu Yib481de92007-09-25 17:54:57 -07002097
2098 cmd->cmd.tx.driver_txop = 0;
2099 cmd->cmd.tx.tx_flags = tx_flags;
2100 cmd->cmd.tx.next_frame_len = 0;
2101}
Tomas Winkler19758be2008-03-12 16:58:51 -07002102static void iwl_update_tx_stats(struct iwl_priv *priv, u16 fc, u16 len)
2103{
2104 /* 0 - mgmt, 1 - cnt, 2 - data */
2105 int idx = (fc & IEEE80211_FCTL_FTYPE) >> 2;
2106 priv->tx_stats[idx].cnt++;
2107 priv->tx_stats[idx].bytes += len;
2108}
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002109/**
2110 * iwl4965_get_sta_id - Find station's index within station table
2111 *
2112 * If new IBSS station, create new entry in station table
2113 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002114static int iwl4965_get_sta_id(struct iwl_priv *priv,
Ben Cahill9fbab512007-11-29 11:09:47 +08002115 struct ieee80211_hdr *hdr)
Zhu Yib481de92007-09-25 17:54:57 -07002116{
2117 int sta_id;
2118 u16 fc = le16_to_cpu(hdr->frame_control);
Joe Perches0795af52007-10-03 17:59:30 -07002119 DECLARE_MAC_BUF(mac);
Zhu Yib481de92007-09-25 17:54:57 -07002120
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002121 /* If this frame is broadcast or management, use broadcast station id */
Zhu Yib481de92007-09-25 17:54:57 -07002122 if (((fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_DATA) ||
2123 is_multicast_ether_addr(hdr->addr1))
2124 return priv->hw_setting.bcast_sta_id;
2125
2126 switch (priv->iw_mode) {
2127
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002128 /* If we are a client station in a BSS network, use the special
2129 * AP station entry (that's the only station we communicate with) */
Zhu Yib481de92007-09-25 17:54:57 -07002130 case IEEE80211_IF_TYPE_STA:
2131 return IWL_AP_ID;
2132
2133 /* If we are an AP, then find the station, or use BCAST */
2134 case IEEE80211_IF_TYPE_AP:
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002135 sta_id = iwl4965_hw_find_station(priv, hdr->addr1);
Zhu Yib481de92007-09-25 17:54:57 -07002136 if (sta_id != IWL_INVALID_STATION)
2137 return sta_id;
2138 return priv->hw_setting.bcast_sta_id;
2139
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002140 /* If this frame is going out to an IBSS network, find the station,
2141 * or create a new station table entry */
Zhu Yib481de92007-09-25 17:54:57 -07002142 case IEEE80211_IF_TYPE_IBSS:
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002143 sta_id = iwl4965_hw_find_station(priv, hdr->addr1);
Zhu Yib481de92007-09-25 17:54:57 -07002144 if (sta_id != IWL_INVALID_STATION)
2145 return sta_id;
2146
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002147 /* Create new station table entry */
Ron Rindjunsky67d62032007-11-26 16:14:40 +02002148 sta_id = iwl4965_add_station_flags(priv, hdr->addr1,
2149 0, CMD_ASYNC, NULL);
Zhu Yib481de92007-09-25 17:54:57 -07002150
2151 if (sta_id != IWL_INVALID_STATION)
2152 return sta_id;
2153
Joe Perches0795af52007-10-03 17:59:30 -07002154 IWL_DEBUG_DROP("Station %s not in station map. "
Zhu Yib481de92007-09-25 17:54:57 -07002155 "Defaulting to broadcast...\n",
Joe Perches0795af52007-10-03 17:59:30 -07002156 print_mac(mac, hdr->addr1));
Tomas Winkler0a6857e2008-03-12 16:58:49 -07002157 iwl_print_hex_dump(IWL_DL_DROP, (u8 *) hdr, sizeof(*hdr));
Zhu Yib481de92007-09-25 17:54:57 -07002158 return priv->hw_setting.bcast_sta_id;
2159
2160 default:
Ian Schram01ebd062007-10-25 17:15:22 +08002161 IWL_WARNING("Unknown mode of operation: %d", priv->iw_mode);
Zhu Yib481de92007-09-25 17:54:57 -07002162 return priv->hw_setting.bcast_sta_id;
2163 }
2164}
2165
2166/*
2167 * start REPLY_TX command process
2168 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002169static int iwl4965_tx_skb(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07002170 struct sk_buff *skb, struct ieee80211_tx_control *ctl)
2171{
2172 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002173 struct iwl4965_tfd_frame *tfd;
Zhu Yib481de92007-09-25 17:54:57 -07002174 u32 *control_flags;
2175 int txq_id = ctl->queue;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002176 struct iwl4965_tx_queue *txq = NULL;
2177 struct iwl4965_queue *q = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07002178 dma_addr_t phys_addr;
2179 dma_addr_t txcmd_phys;
Tomas Winkler87e4f7d2008-01-14 17:46:16 -08002180 dma_addr_t scratch_phys;
Tomas Winkler857485c2008-03-21 13:53:44 -07002181 struct iwl_cmd *out_cmd = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07002182 u16 len, idx, len_org;
2183 u8 id, hdr_len, unicast;
2184 u8 sta_id;
2185 u16 seq_number = 0;
2186 u16 fc;
2187 __le16 *qc;
2188 u8 wait_write_ptr = 0;
2189 unsigned long flags;
2190 int rc;
2191
2192 spin_lock_irqsave(&priv->lock, flags);
Tomas Winklerfee12472008-04-03 16:05:21 -07002193 if (iwl_is_rfkill(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07002194 IWL_DEBUG_DROP("Dropping - RF KILL\n");
2195 goto drop_unlock;
2196 }
2197
Johannes Berg32bfd352007-12-19 01:31:26 +01002198 if (!priv->vif) {
2199 IWL_DEBUG_DROP("Dropping - !priv->vif\n");
Zhu Yib481de92007-09-25 17:54:57 -07002200 goto drop_unlock;
2201 }
2202
Johannes Berg8318d782008-01-24 19:38:38 +01002203 if ((ctl->tx_rate->hw_value & 0xFF) == IWL_INVALID_RATE) {
Zhu Yib481de92007-09-25 17:54:57 -07002204 IWL_ERROR("ERROR: No TX rate available.\n");
2205 goto drop_unlock;
2206 }
2207
2208 unicast = !is_multicast_ether_addr(hdr->addr1);
2209 id = 0;
2210
2211 fc = le16_to_cpu(hdr->frame_control);
2212
Tomas Winkler0a6857e2008-03-12 16:58:49 -07002213#ifdef CONFIG_IWLWIFI_DEBUG
Zhu Yib481de92007-09-25 17:54:57 -07002214 if (ieee80211_is_auth(fc))
2215 IWL_DEBUG_TX("Sending AUTH frame\n");
2216 else if (ieee80211_is_assoc_request(fc))
2217 IWL_DEBUG_TX("Sending ASSOC frame\n");
2218 else if (ieee80211_is_reassoc_request(fc))
2219 IWL_DEBUG_TX("Sending REASSOC frame\n");
2220#endif
2221
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08002222 /* drop all data frame if we are not associated */
Gregory Greenman76f39152008-01-23 10:15:21 -08002223 if (((fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_DATA) &&
Tomas Winkler3109ece2008-03-28 16:33:35 -07002224 (!iwl_is_associated(priv) ||
Reinette Chatrea6477242008-02-14 10:40:28 -08002225 ((priv->iw_mode == IEEE80211_IF_TYPE_STA) && !priv->assoc_id) ||
Gregory Greenman76f39152008-01-23 10:15:21 -08002226 !priv->assoc_station_added)) {
Tomas Winkler3109ece2008-03-28 16:33:35 -07002227 IWL_DEBUG_DROP("Dropping - !iwl_is_associated\n");
Zhu Yib481de92007-09-25 17:54:57 -07002228 goto drop_unlock;
2229 }
2230
2231 spin_unlock_irqrestore(&priv->lock, flags);
2232
2233 hdr_len = ieee80211_get_hdrlen(fc);
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002234
2235 /* Find (or create) index into station table for destination station */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002236 sta_id = iwl4965_get_sta_id(priv, hdr);
Zhu Yib481de92007-09-25 17:54:57 -07002237 if (sta_id == IWL_INVALID_STATION) {
Joe Perches0795af52007-10-03 17:59:30 -07002238 DECLARE_MAC_BUF(mac);
2239
2240 IWL_DEBUG_DROP("Dropping - INVALID STATION: %s\n",
2241 print_mac(mac, hdr->addr1));
Zhu Yib481de92007-09-25 17:54:57 -07002242 goto drop;
2243 }
2244
2245 IWL_DEBUG_RATE("station Id %d\n", sta_id);
2246
2247 qc = ieee80211_get_qos_ctrl(hdr);
2248 if (qc) {
2249 u8 tid = (u8)(le16_to_cpu(*qc) & 0xf);
2250 seq_number = priv->stations[sta_id].tid[tid].seq_number &
2251 IEEE80211_SCTL_SEQ;
2252 hdr->seq_ctrl = cpu_to_le16(seq_number) |
2253 (hdr->seq_ctrl &
2254 __constant_cpu_to_le16(IEEE80211_SCTL_FRAG));
2255 seq_number += 0x10;
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08002256#ifdef CONFIG_IWL4965_HT
Zhu Yib481de92007-09-25 17:54:57 -07002257 /* aggregation is on for this <sta,tid> */
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02002258 if (ctl->flags & IEEE80211_TXCTL_AMPDU)
Zhu Yib481de92007-09-25 17:54:57 -07002259 txq_id = priv->stations[sta_id].tid[tid].agg.txq_id;
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02002260 priv->stations[sta_id].tid[tid].tfds_in_queue++;
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08002261#endif /* CONFIG_IWL4965_HT */
Zhu Yib481de92007-09-25 17:54:57 -07002262 }
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002263
2264 /* Descriptor for chosen Tx queue */
Zhu Yib481de92007-09-25 17:54:57 -07002265 txq = &priv->txq[txq_id];
2266 q = &txq->q;
2267
2268 spin_lock_irqsave(&priv->lock, flags);
2269
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002270 /* Set up first empty TFD within this queue's circular TFD buffer */
Tomas Winklerfc4b6852007-10-25 17:15:24 +08002271 tfd = &txq->bd[q->write_ptr];
Zhu Yib481de92007-09-25 17:54:57 -07002272 memset(tfd, 0, sizeof(*tfd));
2273 control_flags = (u32 *) tfd;
Tomas Winklerfc4b6852007-10-25 17:15:24 +08002274 idx = get_cmd_index(q, q->write_ptr, 0);
Zhu Yib481de92007-09-25 17:54:57 -07002275
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002276 /* Set up driver data for this TFD */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002277 memset(&(txq->txb[q->write_ptr]), 0, sizeof(struct iwl4965_tx_info));
Tomas Winklerfc4b6852007-10-25 17:15:24 +08002278 txq->txb[q->write_ptr].skb[0] = skb;
2279 memcpy(&(txq->txb[q->write_ptr].status.control),
Zhu Yib481de92007-09-25 17:54:57 -07002280 ctl, sizeof(struct ieee80211_tx_control));
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002281
2282 /* Set up first empty entry in queue's array of Tx/cmd buffers */
Zhu Yib481de92007-09-25 17:54:57 -07002283 out_cmd = &txq->cmd[idx];
2284 memset(&out_cmd->hdr, 0, sizeof(out_cmd->hdr));
2285 memset(&out_cmd->cmd.tx, 0, sizeof(out_cmd->cmd.tx));
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002286
2287 /*
2288 * Set up the Tx-command (not MAC!) header.
2289 * Store the chosen Tx queue and TFD index within the sequence field;
2290 * after Tx, uCode's Tx response will return this value so driver can
2291 * locate the frame within the tx queue and do post-tx processing.
2292 */
Zhu Yib481de92007-09-25 17:54:57 -07002293 out_cmd->hdr.cmd = REPLY_TX;
2294 out_cmd->hdr.sequence = cpu_to_le16((u16)(QUEUE_TO_SEQ(txq_id) |
Tomas Winklerfc4b6852007-10-25 17:15:24 +08002295 INDEX_TO_SEQ(q->write_ptr)));
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002296
2297 /* Copy MAC header from skb into command buffer */
Zhu Yib481de92007-09-25 17:54:57 -07002298 memcpy(out_cmd->cmd.tx.hdr, hdr, hdr_len);
2299
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002300 /*
2301 * Use the first empty entry in this queue's command buffer array
2302 * to contain the Tx command and MAC header concatenated together
2303 * (payload data will be in another buffer).
2304 * Size of this varies, due to varying MAC header length.
2305 * If end is not dword aligned, we'll have 2 extra bytes at the end
2306 * of the MAC header (device reads on dword boundaries).
2307 * We'll tell device about this padding later.
2308 */
Zhu Yib481de92007-09-25 17:54:57 -07002309 len = priv->hw_setting.tx_cmd_len +
Tomas Winkler857485c2008-03-21 13:53:44 -07002310 sizeof(struct iwl_cmd_header) + hdr_len;
Zhu Yib481de92007-09-25 17:54:57 -07002311
2312 len_org = len;
2313 len = (len + 3) & ~3;
2314
2315 if (len_org != len)
2316 len_org = 1;
2317 else
2318 len_org = 0;
2319
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002320 /* Physical address of this Tx command's header (not MAC header!),
2321 * within command buffer array. */
Tomas Winkler857485c2008-03-21 13:53:44 -07002322 txcmd_phys = txq->dma_addr_cmd + sizeof(struct iwl_cmd) * idx +
2323 offsetof(struct iwl_cmd, hdr);
Zhu Yib481de92007-09-25 17:54:57 -07002324
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002325 /* Add buffer containing Tx command and MAC(!) header to TFD's
2326 * first entry */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002327 iwl4965_hw_txq_attach_buf_to_tfd(priv, tfd, txcmd_phys, len);
Zhu Yib481de92007-09-25 17:54:57 -07002328
2329 if (!(ctl->flags & IEEE80211_TXCTL_DO_NOT_ENCRYPT))
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07002330 iwl4965_build_tx_cmd_hwcrypto(priv, ctl, out_cmd, skb, sta_id);
Zhu Yib481de92007-09-25 17:54:57 -07002331
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002332 /* Set up TFD's 2nd entry to point directly to remainder of skb,
2333 * if any (802.11 null frames have no payload). */
Zhu Yib481de92007-09-25 17:54:57 -07002334 len = skb->len - hdr_len;
2335 if (len) {
2336 phys_addr = pci_map_single(priv->pci_dev, skb->data + hdr_len,
2337 len, PCI_DMA_TODEVICE);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002338 iwl4965_hw_txq_attach_buf_to_tfd(priv, tfd, phys_addr, len);
Zhu Yib481de92007-09-25 17:54:57 -07002339 }
2340
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002341 /* Tell 4965 about any 2-byte padding after MAC header */
Zhu Yib481de92007-09-25 17:54:57 -07002342 if (len_org)
2343 out_cmd->cmd.tx.tx_flags |= TX_CMD_FLG_MH_PAD_MSK;
2344
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002345 /* Total # bytes to be transmitted */
Zhu Yib481de92007-09-25 17:54:57 -07002346 len = (u16)skb->len;
2347 out_cmd->cmd.tx.len = cpu_to_le16(len);
2348
2349 /* TODO need this for burst mode later on */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002350 iwl4965_build_tx_cmd_basic(priv, out_cmd, ctl, hdr, unicast, sta_id);
Zhu Yib481de92007-09-25 17:54:57 -07002351
2352 /* set is_hcca to 0; it probably will never be implemented */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002353 iwl4965_hw_build_tx_cmd_rate(priv, out_cmd, ctl, hdr, sta_id, 0);
Zhu Yib481de92007-09-25 17:54:57 -07002354
Tomas Winkler19758be2008-03-12 16:58:51 -07002355 iwl_update_tx_stats(priv, fc, len);
2356
Tomas Winkler857485c2008-03-21 13:53:44 -07002357 scratch_phys = txcmd_phys + sizeof(struct iwl_cmd_header) +
Tomas Winkler87e4f7d2008-01-14 17:46:16 -08002358 offsetof(struct iwl4965_tx_cmd, scratch);
2359 out_cmd->cmd.tx.dram_lsb_ptr = cpu_to_le32(scratch_phys);
2360 out_cmd->cmd.tx.dram_msb_ptr = iwl_get_dma_hi_address(scratch_phys);
2361
Zhu Yib481de92007-09-25 17:54:57 -07002362 if (!ieee80211_get_morefrag(hdr)) {
2363 txq->need_update = 1;
2364 if (qc) {
2365 u8 tid = (u8)(le16_to_cpu(*qc) & 0xf);
2366 priv->stations[sta_id].tid[tid].seq_number = seq_number;
2367 }
2368 } else {
2369 wait_write_ptr = 1;
2370 txq->need_update = 0;
2371 }
2372
Tomas Winkler0a6857e2008-03-12 16:58:49 -07002373 iwl_print_hex_dump(IWL_DL_TX, out_cmd->cmd.payload,
Zhu Yib481de92007-09-25 17:54:57 -07002374 sizeof(out_cmd->cmd.tx));
2375
Tomas Winkler0a6857e2008-03-12 16:58:49 -07002376 iwl_print_hex_dump(IWL_DL_TX, (u8 *)out_cmd->cmd.tx.hdr,
Zhu Yib481de92007-09-25 17:54:57 -07002377 ieee80211_get_hdrlen(fc));
2378
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002379 /* Set up entry for this TFD in Tx byte-count array */
Tomas Winklere2a722e2008-04-14 21:16:10 -07002380 priv->cfg->ops->lib->txq_update_byte_cnt_tbl(priv, txq, len);
Zhu Yib481de92007-09-25 17:54:57 -07002381
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002382 /* Tell device the write index *just past* this latest filled TFD */
Tomas Winklerc54b6792008-03-06 17:36:53 -08002383 q->write_ptr = iwl_queue_inc_wrap(q->write_ptr, q->n_bd);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002384 rc = iwl4965_tx_queue_update_write_ptr(priv, txq);
Zhu Yib481de92007-09-25 17:54:57 -07002385 spin_unlock_irqrestore(&priv->lock, flags);
2386
2387 if (rc)
2388 return rc;
2389
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002390 if ((iwl4965_queue_space(q) < q->high_mark)
Zhu Yib481de92007-09-25 17:54:57 -07002391 && priv->mac80211_registered) {
2392 if (wait_write_ptr) {
2393 spin_lock_irqsave(&priv->lock, flags);
2394 txq->need_update = 1;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002395 iwl4965_tx_queue_update_write_ptr(priv, txq);
Zhu Yib481de92007-09-25 17:54:57 -07002396 spin_unlock_irqrestore(&priv->lock, flags);
2397 }
2398
2399 ieee80211_stop_queue(priv->hw, ctl->queue);
2400 }
2401
2402 return 0;
2403
2404drop_unlock:
2405 spin_unlock_irqrestore(&priv->lock, flags);
2406drop:
2407 return -1;
2408}
2409
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002410static void iwl4965_set_rate(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002411{
Johannes Berg8318d782008-01-24 19:38:38 +01002412 const struct ieee80211_supported_band *hw = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07002413 struct ieee80211_rate *rate;
2414 int i;
2415
Johannes Berg8318d782008-01-24 19:38:38 +01002416 hw = iwl4965_get_hw_mode(priv, priv->band);
Saleem Abdulrasoolc4ba9622007-11-18 23:59:08 -08002417 if (!hw) {
2418 IWL_ERROR("Failed to set rate: unable to get hw mode\n");
2419 return;
2420 }
Zhu Yib481de92007-09-25 17:54:57 -07002421
2422 priv->active_rate = 0;
2423 priv->active_rate_basic = 0;
2424
Johannes Berg8318d782008-01-24 19:38:38 +01002425 for (i = 0; i < hw->n_bitrates; i++) {
2426 rate = &(hw->bitrates[i]);
2427 if (rate->hw_value < IWL_RATE_COUNT)
2428 priv->active_rate |= (1 << rate->hw_value);
Zhu Yib481de92007-09-25 17:54:57 -07002429 }
2430
2431 IWL_DEBUG_RATE("Set active_rate = %0x, active_rate_basic = %0x\n",
2432 priv->active_rate, priv->active_rate_basic);
2433
2434 /*
2435 * If a basic rate is configured, then use it (adding IWL_RATE_1M_MASK)
2436 * otherwise set it to the default of all CCK rates and 6, 12, 24 for
2437 * OFDM
2438 */
2439 if (priv->active_rate_basic & IWL_CCK_BASIC_RATES_MASK)
2440 priv->staging_rxon.cck_basic_rates =
2441 ((priv->active_rate_basic &
2442 IWL_CCK_RATES_MASK) >> IWL_FIRST_CCK_RATE) & 0xF;
2443 else
2444 priv->staging_rxon.cck_basic_rates =
2445 (IWL_CCK_BASIC_RATES_MASK >> IWL_FIRST_CCK_RATE) & 0xF;
2446
2447 if (priv->active_rate_basic & IWL_OFDM_BASIC_RATES_MASK)
2448 priv->staging_rxon.ofdm_basic_rates =
2449 ((priv->active_rate_basic &
2450 (IWL_OFDM_BASIC_RATES_MASK | IWL_RATE_6M_MASK)) >>
2451 IWL_FIRST_OFDM_RATE) & 0xFF;
2452 else
2453 priv->staging_rxon.ofdm_basic_rates =
2454 (IWL_OFDM_BASIC_RATES_MASK >> IWL_FIRST_OFDM_RATE) & 0xFF;
2455}
2456
Mohamed Abbasad97edd2008-03-28 16:21:06 -07002457void iwl4965_radio_kill_sw(struct iwl_priv *priv, int disable_radio)
Zhu Yib481de92007-09-25 17:54:57 -07002458{
2459 unsigned long flags;
2460
2461 if (!!disable_radio == test_bit(STATUS_RF_KILL_SW, &priv->status))
2462 return;
2463
2464 IWL_DEBUG_RF_KILL("Manual SW RF KILL set to: RADIO %s\n",
2465 disable_radio ? "OFF" : "ON");
2466
2467 if (disable_radio) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002468 iwl4965_scan_cancel(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002469 /* FIXME: This is a workaround for AP */
2470 if (priv->iw_mode != IEEE80211_IF_TYPE_AP) {
2471 spin_lock_irqsave(&priv->lock, flags);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07002472 iwl_write32(priv, CSR_UCODE_DRV_GP1_SET,
Zhu Yib481de92007-09-25 17:54:57 -07002473 CSR_UCODE_SW_BIT_RFKILL);
2474 spin_unlock_irqrestore(&priv->lock, flags);
Mohamed Abbasad97edd2008-03-28 16:21:06 -07002475 /* call the host command only if no hw rf-kill set */
2476 if (!test_bit(STATUS_RF_KILL_HW, &priv->status))
2477 iwl4965_send_card_state(priv,
2478 CARD_STATE_CMD_DISABLE,
2479 0);
Zhu Yib481de92007-09-25 17:54:57 -07002480 set_bit(STATUS_RF_KILL_SW, &priv->status);
Mohamed Abbasad97edd2008-03-28 16:21:06 -07002481
2482 /* make sure mac80211 stop sending Tx frame */
2483 if (priv->mac80211_registered)
2484 ieee80211_stop_queues(priv->hw);
Zhu Yib481de92007-09-25 17:54:57 -07002485 }
2486 return;
2487 }
2488
2489 spin_lock_irqsave(&priv->lock, flags);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07002490 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
Zhu Yib481de92007-09-25 17:54:57 -07002491
2492 clear_bit(STATUS_RF_KILL_SW, &priv->status);
2493 spin_unlock_irqrestore(&priv->lock, flags);
2494
2495 /* wake up ucode */
2496 msleep(10);
2497
2498 spin_lock_irqsave(&priv->lock, flags);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07002499 iwl_read32(priv, CSR_UCODE_DRV_GP1);
2500 if (!iwl_grab_nic_access(priv))
2501 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002502 spin_unlock_irqrestore(&priv->lock, flags);
2503
2504 if (test_bit(STATUS_RF_KILL_HW, &priv->status)) {
2505 IWL_DEBUG_RF_KILL("Can not turn radio back on - "
2506 "disabled by HW switch\n");
2507 return;
2508 }
2509
2510 queue_work(priv->workqueue, &priv->restart);
2511 return;
2512}
2513
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002514void iwl4965_set_decrypted_flag(struct iwl_priv *priv, struct sk_buff *skb,
Zhu Yib481de92007-09-25 17:54:57 -07002515 u32 decrypt_res, struct ieee80211_rx_status *stats)
2516{
2517 u16 fc =
2518 le16_to_cpu(((struct ieee80211_hdr *)skb->data)->frame_control);
2519
2520 if (priv->active_rxon.filter_flags & RXON_FILTER_DIS_DECRYPT_MSK)
2521 return;
2522
2523 if (!(fc & IEEE80211_FCTL_PROTECTED))
2524 return;
2525
2526 IWL_DEBUG_RX("decrypt_res:0x%x\n", decrypt_res);
2527 switch (decrypt_res & RX_RES_STATUS_SEC_TYPE_MSK) {
2528 case RX_RES_STATUS_SEC_TYPE_TKIP:
Emmanuel Grumbach17e476b2008-03-19 16:41:42 -07002529 /* The uCode has got a bad phase 1 Key, pushes the packet.
2530 * Decryption will be done in SW. */
2531 if ((decrypt_res & RX_RES_STATUS_DECRYPT_TYPE_MSK) ==
2532 RX_RES_STATUS_BAD_KEY_TTAK)
2533 break;
2534
Zhu Yib481de92007-09-25 17:54:57 -07002535 if ((decrypt_res & RX_RES_STATUS_DECRYPT_TYPE_MSK) ==
2536 RX_RES_STATUS_BAD_ICV_MIC)
2537 stats->flag |= RX_FLAG_MMIC_ERROR;
2538 case RX_RES_STATUS_SEC_TYPE_WEP:
2539 case RX_RES_STATUS_SEC_TYPE_CCMP:
2540 if ((decrypt_res & RX_RES_STATUS_DECRYPT_TYPE_MSK) ==
2541 RX_RES_STATUS_DECRYPT_OK) {
2542 IWL_DEBUG_RX("hw decrypt successfully!!!\n");
2543 stats->flag |= RX_FLAG_DECRYPTED;
2544 }
2545 break;
2546
2547 default:
2548 break;
2549 }
2550}
2551
Zhu Yib481de92007-09-25 17:54:57 -07002552
2553#define IWL_PACKET_RETRY_TIME HZ
2554
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002555int iwl4965_is_duplicate_packet(struct iwl_priv *priv, struct ieee80211_hdr *header)
Zhu Yib481de92007-09-25 17:54:57 -07002556{
2557 u16 sc = le16_to_cpu(header->seq_ctrl);
2558 u16 seq = (sc & IEEE80211_SCTL_SEQ) >> 4;
2559 u16 frag = sc & IEEE80211_SCTL_FRAG;
2560 u16 *last_seq, *last_frag;
2561 unsigned long *last_time;
2562
2563 switch (priv->iw_mode) {
2564 case IEEE80211_IF_TYPE_IBSS:{
2565 struct list_head *p;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002566 struct iwl4965_ibss_seq *entry = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07002567 u8 *mac = header->addr2;
2568 int index = mac[5] & (IWL_IBSS_MAC_HASH_SIZE - 1);
2569
2570 __list_for_each(p, &priv->ibss_mac_hash[index]) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002571 entry = list_entry(p, struct iwl4965_ibss_seq, list);
Zhu Yib481de92007-09-25 17:54:57 -07002572 if (!compare_ether_addr(entry->mac, mac))
2573 break;
2574 }
2575 if (p == &priv->ibss_mac_hash[index]) {
2576 entry = kzalloc(sizeof(*entry), GFP_ATOMIC);
2577 if (!entry) {
Ian Schrambc434dd2007-10-25 17:15:29 +08002578 IWL_ERROR("Cannot malloc new mac entry\n");
Zhu Yib481de92007-09-25 17:54:57 -07002579 return 0;
2580 }
2581 memcpy(entry->mac, mac, ETH_ALEN);
2582 entry->seq_num = seq;
2583 entry->frag_num = frag;
2584 entry->packet_time = jiffies;
Ian Schrambc434dd2007-10-25 17:15:29 +08002585 list_add(&entry->list, &priv->ibss_mac_hash[index]);
Zhu Yib481de92007-09-25 17:54:57 -07002586 return 0;
2587 }
2588 last_seq = &entry->seq_num;
2589 last_frag = &entry->frag_num;
2590 last_time = &entry->packet_time;
2591 break;
2592 }
2593 case IEEE80211_IF_TYPE_STA:
2594 last_seq = &priv->last_seq_num;
2595 last_frag = &priv->last_frag_num;
2596 last_time = &priv->last_packet_time;
2597 break;
2598 default:
2599 return 0;
2600 }
2601 if ((*last_seq == seq) &&
2602 time_after(*last_time + IWL_PACKET_RETRY_TIME, jiffies)) {
2603 if (*last_frag == frag)
2604 goto drop;
2605 if (*last_frag + 1 != frag)
2606 /* out-of-order fragment */
2607 goto drop;
2608 } else
2609 *last_seq = seq;
2610
2611 *last_frag = frag;
2612 *last_time = jiffies;
2613 return 0;
2614
2615 drop:
2616 return 1;
2617}
2618
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08002619#ifdef CONFIG_IWL4965_SPECTRUM_MEASUREMENT
Zhu Yib481de92007-09-25 17:54:57 -07002620
2621#include "iwl-spectrum.h"
2622
2623#define BEACON_TIME_MASK_LOW 0x00FFFFFF
2624#define BEACON_TIME_MASK_HIGH 0xFF000000
2625#define TIME_UNIT 1024
2626
2627/*
2628 * extended beacon time format
2629 * time in usec will be changed into a 32-bit value in 8:24 format
2630 * the high 1 byte is the beacon counts
2631 * the lower 3 bytes is the time in usec within one beacon interval
2632 */
2633
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002634static u32 iwl4965_usecs_to_beacons(u32 usec, u32 beacon_interval)
Zhu Yib481de92007-09-25 17:54:57 -07002635{
2636 u32 quot;
2637 u32 rem;
2638 u32 interval = beacon_interval * 1024;
2639
2640 if (!interval || !usec)
2641 return 0;
2642
2643 quot = (usec / interval) & (BEACON_TIME_MASK_HIGH >> 24);
2644 rem = (usec % interval) & BEACON_TIME_MASK_LOW;
2645
2646 return (quot << 24) + rem;
2647}
2648
2649/* base is usually what we get from ucode with each received frame,
2650 * the same as HW timer counter counting down
2651 */
2652
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002653static __le32 iwl4965_add_beacon_time(u32 base, u32 addon, u32 beacon_interval)
Zhu Yib481de92007-09-25 17:54:57 -07002654{
2655 u32 base_low = base & BEACON_TIME_MASK_LOW;
2656 u32 addon_low = addon & BEACON_TIME_MASK_LOW;
2657 u32 interval = beacon_interval * TIME_UNIT;
2658 u32 res = (base & BEACON_TIME_MASK_HIGH) +
2659 (addon & BEACON_TIME_MASK_HIGH);
2660
2661 if (base_low > addon_low)
2662 res += base_low - addon_low;
2663 else if (base_low < addon_low) {
2664 res += interval + base_low - addon_low;
2665 res += (1 << 24);
2666 } else
2667 res += (1 << 24);
2668
2669 return cpu_to_le32(res);
2670}
2671
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002672static int iwl4965_get_measurement(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07002673 struct ieee80211_measurement_params *params,
2674 u8 type)
2675{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002676 struct iwl4965_spectrum_cmd spectrum;
2677 struct iwl4965_rx_packet *res;
Tomas Winkler857485c2008-03-21 13:53:44 -07002678 struct iwl_host_cmd cmd = {
Zhu Yib481de92007-09-25 17:54:57 -07002679 .id = REPLY_SPECTRUM_MEASUREMENT_CMD,
2680 .data = (void *)&spectrum,
2681 .meta.flags = CMD_WANT_SKB,
2682 };
2683 u32 add_time = le64_to_cpu(params->start_time);
2684 int rc;
2685 int spectrum_resp_status;
2686 int duration = le16_to_cpu(params->duration);
2687
Tomas Winkler3109ece2008-03-28 16:33:35 -07002688 if (iwl_is_associated(priv))
Zhu Yib481de92007-09-25 17:54:57 -07002689 add_time =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002690 iwl4965_usecs_to_beacons(
Zhu Yib481de92007-09-25 17:54:57 -07002691 le64_to_cpu(params->start_time) - priv->last_tsf,
2692 le16_to_cpu(priv->rxon_timing.beacon_interval));
2693
2694 memset(&spectrum, 0, sizeof(spectrum));
2695
2696 spectrum.channel_count = cpu_to_le16(1);
2697 spectrum.flags =
2698 RXON_FLG_TSF2HOST_MSK | RXON_FLG_ANT_A_MSK | RXON_FLG_DIS_DIV_MSK;
2699 spectrum.filter_flags = MEASUREMENT_FILTER_FLAG;
2700 cmd.len = sizeof(spectrum);
2701 spectrum.len = cpu_to_le16(cmd.len - sizeof(spectrum.len));
2702
Tomas Winkler3109ece2008-03-28 16:33:35 -07002703 if (iwl_is_associated(priv))
Zhu Yib481de92007-09-25 17:54:57 -07002704 spectrum.start_time =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002705 iwl4965_add_beacon_time(priv->last_beacon_time,
Zhu Yib481de92007-09-25 17:54:57 -07002706 add_time,
2707 le16_to_cpu(priv->rxon_timing.beacon_interval));
2708 else
2709 spectrum.start_time = 0;
2710
2711 spectrum.channels[0].duration = cpu_to_le32(duration * TIME_UNIT);
2712 spectrum.channels[0].channel = params->channel;
2713 spectrum.channels[0].type = type;
2714 if (priv->active_rxon.flags & RXON_FLG_BAND_24G_MSK)
2715 spectrum.flags |= RXON_FLG_BAND_24G_MSK |
2716 RXON_FLG_AUTO_DETECT_MSK | RXON_FLG_TGG_PROTECT_MSK;
2717
Tomas Winkler857485c2008-03-21 13:53:44 -07002718 rc = iwl_send_cmd_sync(priv, &cmd);
Zhu Yib481de92007-09-25 17:54:57 -07002719 if (rc)
2720 return rc;
2721
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002722 res = (struct iwl4965_rx_packet *)cmd.meta.u.skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07002723 if (res->hdr.flags & IWL_CMD_FAILED_MSK) {
2724 IWL_ERROR("Bad return from REPLY_RX_ON_ASSOC command\n");
2725 rc = -EIO;
2726 }
2727
2728 spectrum_resp_status = le16_to_cpu(res->u.spectrum.status);
2729 switch (spectrum_resp_status) {
2730 case 0: /* Command will be handled */
2731 if (res->u.spectrum.id != 0xff) {
2732 IWL_DEBUG_INFO
2733 ("Replaced existing measurement: %d\n",
2734 res->u.spectrum.id);
2735 priv->measurement_status &= ~MEASUREMENT_READY;
2736 }
2737 priv->measurement_status |= MEASUREMENT_ACTIVE;
2738 rc = 0;
2739 break;
2740
2741 case 1: /* Command will not be handled */
2742 rc = -EAGAIN;
2743 break;
2744 }
2745
2746 dev_kfree_skb_any(cmd.meta.u.skb);
2747
2748 return rc;
2749}
2750#endif
2751
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002752static void iwl4965_txstatus_to_ieee(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002753 struct iwl4965_tx_info *tx_sta)
Zhu Yib481de92007-09-25 17:54:57 -07002754{
2755
2756 tx_sta->status.ack_signal = 0;
2757 tx_sta->status.excessive_retries = 0;
2758 tx_sta->status.queue_length = 0;
2759 tx_sta->status.queue_number = 0;
2760
2761 if (in_interrupt())
2762 ieee80211_tx_status_irqsafe(priv->hw,
2763 tx_sta->skb[0], &(tx_sta->status));
2764 else
2765 ieee80211_tx_status(priv->hw,
2766 tx_sta->skb[0], &(tx_sta->status));
2767
2768 tx_sta->skb[0] = NULL;
2769}
2770
2771/**
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002772 * iwl4965_tx_queue_reclaim - Reclaim Tx queue entries already Tx'd
Zhu Yib481de92007-09-25 17:54:57 -07002773 *
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002774 * When FW advances 'R' index, all entries between old and new 'R' index
2775 * need to be reclaimed. As result, some free space forms. If there is
2776 * enough free space (> low mark), wake the stack that feeds us.
Zhu Yib481de92007-09-25 17:54:57 -07002777 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002778int iwl4965_tx_queue_reclaim(struct iwl_priv *priv, int txq_id, int index)
Zhu Yib481de92007-09-25 17:54:57 -07002779{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002780 struct iwl4965_tx_queue *txq = &priv->txq[txq_id];
2781 struct iwl4965_queue *q = &txq->q;
Zhu Yib481de92007-09-25 17:54:57 -07002782 int nfreed = 0;
2783
2784 if ((index >= q->n_bd) || (x2_queue_used(q, index) == 0)) {
2785 IWL_ERROR("Read index for DMA queue txq id (%d), index %d, "
2786 "is out of range [0-%d] %d %d.\n", txq_id,
Tomas Winklerfc4b6852007-10-25 17:15:24 +08002787 index, q->n_bd, q->write_ptr, q->read_ptr);
Zhu Yib481de92007-09-25 17:54:57 -07002788 return 0;
2789 }
2790
Tomas Winklerc54b6792008-03-06 17:36:53 -08002791 for (index = iwl_queue_inc_wrap(index, q->n_bd);
Tomas Winklerfc4b6852007-10-25 17:15:24 +08002792 q->read_ptr != index;
Tomas Winklerc54b6792008-03-06 17:36:53 -08002793 q->read_ptr = iwl_queue_inc_wrap(q->read_ptr, q->n_bd)) {
Zhu Yib481de92007-09-25 17:54:57 -07002794 if (txq_id != IWL_CMD_QUEUE_NUM) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002795 iwl4965_txstatus_to_ieee(priv,
Tomas Winklerfc4b6852007-10-25 17:15:24 +08002796 &(txq->txb[txq->q.read_ptr]));
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002797 iwl4965_hw_txq_free_tfd(priv, txq);
Zhu Yib481de92007-09-25 17:54:57 -07002798 } else if (nfreed > 1) {
2799 IWL_ERROR("HCMD skipped: index (%d) %d %d\n", index,
Tomas Winklerfc4b6852007-10-25 17:15:24 +08002800 q->write_ptr, q->read_ptr);
Zhu Yib481de92007-09-25 17:54:57 -07002801 queue_work(priv->workqueue, &priv->restart);
2802 }
2803 nfreed++;
2804 }
2805
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02002806/* if (iwl4965_queue_space(q) > q->low_mark && (txq_id >= 0) &&
Zhu Yib481de92007-09-25 17:54:57 -07002807 (txq_id != IWL_CMD_QUEUE_NUM) &&
2808 priv->mac80211_registered)
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02002809 ieee80211_wake_queue(priv->hw, txq_id); */
Zhu Yib481de92007-09-25 17:54:57 -07002810
2811
2812 return nfreed;
2813}
2814
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002815static int iwl4965_is_tx_success(u32 status)
Zhu Yib481de92007-09-25 17:54:57 -07002816{
2817 status &= TX_STATUS_MSK;
2818 return (status == TX_STATUS_SUCCESS)
2819 || (status == TX_STATUS_DIRECT_DONE);
2820}
2821
2822/******************************************************************************
2823 *
2824 * Generic RX handler implementations
2825 *
2826 ******************************************************************************/
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08002827#ifdef CONFIG_IWL4965_HT
Zhu Yib481de92007-09-25 17:54:57 -07002828
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002829static inline int iwl4965_get_ra_sta_id(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07002830 struct ieee80211_hdr *hdr)
2831{
2832 if (priv->iw_mode == IEEE80211_IF_TYPE_STA)
2833 return IWL_AP_ID;
2834 else {
2835 u8 *da = ieee80211_get_DA(hdr);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002836 return iwl4965_hw_find_station(priv, da);
Zhu Yib481de92007-09-25 17:54:57 -07002837 }
2838}
2839
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002840static struct ieee80211_hdr *iwl4965_tx_queue_get_hdr(
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002841 struct iwl_priv *priv, int txq_id, int idx)
Zhu Yib481de92007-09-25 17:54:57 -07002842{
2843 if (priv->txq[txq_id].txb[idx].skb[0])
2844 return (struct ieee80211_hdr *)priv->txq[txq_id].
2845 txb[idx].skb[0]->data;
2846 return NULL;
2847}
2848
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002849static inline u32 iwl4965_get_scd_ssn(struct iwl4965_tx_resp *tx_resp)
Zhu Yib481de92007-09-25 17:54:57 -07002850{
2851 __le32 *scd_ssn = (__le32 *)((u32 *)&tx_resp->status +
2852 tx_resp->frame_count);
2853 return le32_to_cpu(*scd_ssn) & MAX_SN;
2854
2855}
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002856
2857/**
2858 * iwl4965_tx_status_reply_tx - Handle Tx rspnse for frames in aggregation queue
2859 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002860static int iwl4965_tx_status_reply_tx(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002861 struct iwl4965_ht_agg *agg,
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02002862 struct iwl4965_tx_resp_agg *tx_resp,
Zhu Yib481de92007-09-25 17:54:57 -07002863 u16 start_idx)
2864{
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02002865 u16 status;
2866 struct agg_tx_status *frame_status = &tx_resp->status;
Zhu Yib481de92007-09-25 17:54:57 -07002867 struct ieee80211_tx_status *tx_status = NULL;
2868 struct ieee80211_hdr *hdr = NULL;
2869 int i, sh;
2870 int txq_id, idx;
2871 u16 seq;
2872
2873 if (agg->wait_for_ba)
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002874 IWL_DEBUG_TX_REPLY("got tx response w/o block-ack\n");
Zhu Yib481de92007-09-25 17:54:57 -07002875
2876 agg->frame_count = tx_resp->frame_count;
2877 agg->start_idx = start_idx;
2878 agg->rate_n_flags = le32_to_cpu(tx_resp->rate_n_flags);
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02002879 agg->bitmap = 0;
Zhu Yib481de92007-09-25 17:54:57 -07002880
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002881 /* # frames attempted by Tx command */
Zhu Yib481de92007-09-25 17:54:57 -07002882 if (agg->frame_count == 1) {
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002883 /* Only one frame was attempted; no block-ack will arrive */
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02002884 status = le16_to_cpu(frame_status[0].status);
2885 seq = le16_to_cpu(frame_status[0].sequence);
2886 idx = SEQ_TO_INDEX(seq);
2887 txq_id = SEQ_TO_QUEUE(seq);
Zhu Yib481de92007-09-25 17:54:57 -07002888
Zhu Yib481de92007-09-25 17:54:57 -07002889 /* FIXME: code repetition */
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02002890 IWL_DEBUG_TX_REPLY("FrameCnt = %d, StartIdx=%d idx=%d\n",
2891 agg->frame_count, agg->start_idx, idx);
Zhu Yib481de92007-09-25 17:54:57 -07002892
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02002893 tx_status = &(priv->txq[txq_id].txb[idx].status);
Zhu Yib481de92007-09-25 17:54:57 -07002894 tx_status->retry_count = tx_resp->failure_frame;
2895 tx_status->queue_number = status & 0xff;
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02002896 tx_status->queue_length = tx_resp->failure_rts;
2897 tx_status->control.flags &= ~IEEE80211_TXCTL_AMPDU;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002898 tx_status->flags = iwl4965_is_tx_success(status)?
Zhu Yib481de92007-09-25 17:54:57 -07002899 IEEE80211_TX_STATUS_ACK : 0;
Ron Rindjunsky4c424e42008-03-04 18:09:27 -08002900 iwl4965_hwrate_to_tx_control(priv,
2901 le32_to_cpu(tx_resp->rate_n_flags),
2902 &tx_status->control);
Zhu Yib481de92007-09-25 17:54:57 -07002903 /* FIXME: code repetition end */
2904
2905 IWL_DEBUG_TX_REPLY("1 Frame 0x%x failure :%d\n",
2906 status & 0xff, tx_resp->failure_frame);
2907 IWL_DEBUG_TX_REPLY("Rate Info rate_n_flags=%x\n",
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002908 iwl4965_hw_get_rate_n_flags(tx_resp->rate_n_flags));
Zhu Yib481de92007-09-25 17:54:57 -07002909
2910 agg->wait_for_ba = 0;
2911 } else {
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002912 /* Two or more frames were attempted; expect block-ack */
Zhu Yib481de92007-09-25 17:54:57 -07002913 u64 bitmap = 0;
2914 int start = agg->start_idx;
2915
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002916 /* Construct bit-map of pending frames within Tx window */
Zhu Yib481de92007-09-25 17:54:57 -07002917 for (i = 0; i < agg->frame_count; i++) {
2918 u16 sc;
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02002919 status = le16_to_cpu(frame_status[i].status);
2920 seq = le16_to_cpu(frame_status[i].sequence);
Zhu Yib481de92007-09-25 17:54:57 -07002921 idx = SEQ_TO_INDEX(seq);
2922 txq_id = SEQ_TO_QUEUE(seq);
2923
2924 if (status & (AGG_TX_STATE_FEW_BYTES_MSK |
2925 AGG_TX_STATE_ABORT_MSK))
2926 continue;
2927
2928 IWL_DEBUG_TX_REPLY("FrameCnt = %d, txq_id=%d idx=%d\n",
2929 agg->frame_count, txq_id, idx);
2930
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002931 hdr = iwl4965_tx_queue_get_hdr(priv, txq_id, idx);
Zhu Yib481de92007-09-25 17:54:57 -07002932
2933 sc = le16_to_cpu(hdr->seq_ctrl);
2934 if (idx != (SEQ_TO_SN(sc) & 0xff)) {
2935 IWL_ERROR("BUG_ON idx doesn't match seq control"
2936 " idx=%d, seq_idx=%d, seq=%d\n",
2937 idx, SEQ_TO_SN(sc),
2938 hdr->seq_ctrl);
2939 return -1;
2940 }
2941
2942 IWL_DEBUG_TX_REPLY("AGG Frame i=%d idx %d seq=%d\n",
2943 i, idx, SEQ_TO_SN(sc));
2944
2945 sh = idx - start;
2946 if (sh > 64) {
2947 sh = (start - idx) + 0xff;
2948 bitmap = bitmap << sh;
2949 sh = 0;
2950 start = idx;
2951 } else if (sh < -64)
2952 sh = 0xff - (start - idx);
2953 else if (sh < 0) {
2954 sh = start - idx;
2955 start = idx;
2956 bitmap = bitmap << sh;
2957 sh = 0;
2958 }
2959 bitmap |= (1 << sh);
2960 IWL_DEBUG_TX_REPLY("start=%d bitmap=0x%x\n",
2961 start, (u32)(bitmap & 0xFFFFFFFF));
2962 }
2963
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02002964 agg->bitmap = bitmap;
Zhu Yib481de92007-09-25 17:54:57 -07002965 agg->start_idx = start;
2966 agg->rate_n_flags = le32_to_cpu(tx_resp->rate_n_flags);
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02002967 IWL_DEBUG_TX_REPLY("Frames %d start_idx=%d bitmap=0x%llx\n",
Zhu Yib481de92007-09-25 17:54:57 -07002968 agg->frame_count, agg->start_idx,
John W. Linville06501d22008-04-01 17:38:47 -04002969 (unsigned long long)agg->bitmap);
Zhu Yib481de92007-09-25 17:54:57 -07002970
2971 if (bitmap)
2972 agg->wait_for_ba = 1;
2973 }
2974 return 0;
2975}
2976#endif
Zhu Yib481de92007-09-25 17:54:57 -07002977
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002978/**
2979 * iwl4965_rx_reply_tx - Handle standard (non-aggregation) Tx response
2980 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002981static void iwl4965_rx_reply_tx(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002982 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07002983{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002984 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07002985 u16 sequence = le16_to_cpu(pkt->hdr.sequence);
2986 int txq_id = SEQ_TO_QUEUE(sequence);
2987 int index = SEQ_TO_INDEX(sequence);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002988 struct iwl4965_tx_queue *txq = &priv->txq[txq_id];
Zhu Yib481de92007-09-25 17:54:57 -07002989 struct ieee80211_tx_status *tx_status;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002990 struct iwl4965_tx_resp *tx_resp = (void *)&pkt->u.raw[0];
Zhu Yib481de92007-09-25 17:54:57 -07002991 u32 status = le32_to_cpu(tx_resp->status);
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08002992#ifdef CONFIG_IWL4965_HT
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02002993 int tid = MAX_TID_COUNT, sta_id = IWL_INVALID_STATION;
2994 struct ieee80211_hdr *hdr;
2995 __le16 *qc;
Zhu Yib481de92007-09-25 17:54:57 -07002996#endif
2997
2998 if ((index >= txq->q.n_bd) || (x2_queue_used(&txq->q, index) == 0)) {
2999 IWL_ERROR("Read index for DMA queue txq_id (%d) index %d "
3000 "is out of range [0-%d] %d %d\n", txq_id,
Tomas Winklerfc4b6852007-10-25 17:15:24 +08003001 index, txq->q.n_bd, txq->q.write_ptr,
3002 txq->q.read_ptr);
Zhu Yib481de92007-09-25 17:54:57 -07003003 return;
3004 }
3005
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08003006#ifdef CONFIG_IWL4965_HT
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003007 hdr = iwl4965_tx_queue_get_hdr(priv, txq_id, index);
3008 qc = ieee80211_get_qos_ctrl(hdr);
Zhu Yib481de92007-09-25 17:54:57 -07003009
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003010 if (qc)
Zhu Yib481de92007-09-25 17:54:57 -07003011 tid = le16_to_cpu(*qc) & 0xf;
3012
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003013 sta_id = iwl4965_get_ra_sta_id(priv, hdr);
3014 if (txq->sched_retry && unlikely(sta_id == IWL_INVALID_STATION)) {
3015 IWL_ERROR("Station not known\n");
3016 return;
3017 }
3018
3019 if (txq->sched_retry) {
3020 const u32 scd_ssn = iwl4965_get_scd_ssn(tx_resp);
3021 struct iwl4965_ht_agg *agg = NULL;
3022
3023 if (!qc)
Zhu Yib481de92007-09-25 17:54:57 -07003024 return;
Zhu Yib481de92007-09-25 17:54:57 -07003025
3026 agg = &priv->stations[sta_id].tid[tid].agg;
3027
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003028 iwl4965_tx_status_reply_tx(priv, agg,
3029 (struct iwl4965_tx_resp_agg *)tx_resp, index);
Zhu Yib481de92007-09-25 17:54:57 -07003030
3031 if ((tx_resp->frame_count == 1) &&
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003032 !iwl4965_is_tx_success(status)) {
Zhu Yib481de92007-09-25 17:54:57 -07003033 /* TODO: send BAR */
3034 }
3035
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003036 if (txq->q.read_ptr != (scd_ssn & 0xff)) {
3037 int freed;
Tomas Winklerc54b6792008-03-06 17:36:53 -08003038 index = iwl_queue_dec_wrap(scd_ssn & 0xff, txq->q.n_bd);
Zhu Yib481de92007-09-25 17:54:57 -07003039 IWL_DEBUG_TX_REPLY("Retry scheduler reclaim scd_ssn "
3040 "%d index %d\n", scd_ssn , index);
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003041 freed = iwl4965_tx_queue_reclaim(priv, txq_id, index);
3042 priv->stations[sta_id].tid[tid].tfds_in_queue -= freed;
3043
3044 if (iwl4965_queue_space(&txq->q) > txq->q.low_mark &&
3045 txq_id >= 0 && priv->mac80211_registered &&
3046 agg->state != IWL_EMPTYING_HW_QUEUE_DELBA)
3047 ieee80211_wake_queue(priv->hw, txq_id);
3048
3049 iwl4965_check_empty_hw_queue(priv, sta_id, tid, txq_id);
Zhu Yib481de92007-09-25 17:54:57 -07003050 }
3051 } else {
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08003052#endif /* CONFIG_IWL4965_HT */
Tomas Winklerfc4b6852007-10-25 17:15:24 +08003053 tx_status = &(txq->txb[txq->q.read_ptr].status);
Zhu Yib481de92007-09-25 17:54:57 -07003054
3055 tx_status->retry_count = tx_resp->failure_frame;
3056 tx_status->queue_number = status;
3057 tx_status->queue_length = tx_resp->bt_kill_count;
3058 tx_status->queue_length |= tx_resp->failure_rts;
Zhu Yib481de92007-09-25 17:54:57 -07003059 tx_status->flags =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003060 iwl4965_is_tx_success(status) ? IEEE80211_TX_STATUS_ACK : 0;
Ron Rindjunsky4c424e42008-03-04 18:09:27 -08003061 iwl4965_hwrate_to_tx_control(priv, le32_to_cpu(tx_resp->rate_n_flags),
3062 &tx_status->control);
Zhu Yib481de92007-09-25 17:54:57 -07003063
Zhu Yib481de92007-09-25 17:54:57 -07003064 IWL_DEBUG_TX("Tx queue %d Status %s (0x%08x) rate_n_flags 0x%x "
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003065 "retries %d\n", txq_id, iwl4965_get_tx_fail_reason(status),
Zhu Yib481de92007-09-25 17:54:57 -07003066 status, le32_to_cpu(tx_resp->rate_n_flags),
3067 tx_resp->failure_frame);
3068
3069 IWL_DEBUG_TX_REPLY("Tx queue reclaim %d\n", index);
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003070 if (index != -1) {
3071 int freed = iwl4965_tx_queue_reclaim(priv, txq_id, index);
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08003072#ifdef CONFIG_IWL4965_HT
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003073 if (tid != MAX_TID_COUNT)
3074 priv->stations[sta_id].tid[tid].tfds_in_queue -= freed;
3075 if (iwl4965_queue_space(&txq->q) > txq->q.low_mark &&
3076 (txq_id >= 0) &&
3077 priv->mac80211_registered)
3078 ieee80211_wake_queue(priv->hw, txq_id);
3079 if (tid != MAX_TID_COUNT)
3080 iwl4965_check_empty_hw_queue(priv, sta_id, tid, txq_id);
3081#endif
Zhu Yib481de92007-09-25 17:54:57 -07003082 }
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003083#ifdef CONFIG_IWL4965_HT
3084 }
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08003085#endif /* CONFIG_IWL4965_HT */
Zhu Yib481de92007-09-25 17:54:57 -07003086
3087 if (iwl_check_bits(status, TX_ABORT_REQUIRED_MSK))
3088 IWL_ERROR("TODO: Implement Tx ABORT REQUIRED!!!\n");
3089}
3090
3091
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003092static void iwl4965_rx_reply_alive(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003093 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003094{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003095 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
3096 struct iwl4965_alive_resp *palive;
Zhu Yib481de92007-09-25 17:54:57 -07003097 struct delayed_work *pwork;
3098
3099 palive = &pkt->u.alive_frame;
3100
3101 IWL_DEBUG_INFO("Alive ucode status 0x%08X revision "
3102 "0x%01X 0x%01X\n",
3103 palive->is_valid, palive->ver_type,
3104 palive->ver_subtype);
3105
3106 if (palive->ver_subtype == INITIALIZE_SUBTYPE) {
3107 IWL_DEBUG_INFO("Initialization Alive received.\n");
3108 memcpy(&priv->card_alive_init,
3109 &pkt->u.alive_frame,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003110 sizeof(struct iwl4965_init_alive_resp));
Zhu Yib481de92007-09-25 17:54:57 -07003111 pwork = &priv->init_alive_start;
3112 } else {
3113 IWL_DEBUG_INFO("Runtime Alive received.\n");
3114 memcpy(&priv->card_alive, &pkt->u.alive_frame,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003115 sizeof(struct iwl4965_alive_resp));
Zhu Yib481de92007-09-25 17:54:57 -07003116 pwork = &priv->alive_start;
3117 }
3118
3119 /* We delay the ALIVE response by 5ms to
3120 * give the HW RF Kill time to activate... */
3121 if (palive->is_valid == UCODE_VALID_OK)
3122 queue_delayed_work(priv->workqueue, pwork,
3123 msecs_to_jiffies(5));
3124 else
3125 IWL_WARNING("uCode did not respond OK.\n");
3126}
3127
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003128static void iwl4965_rx_reply_add_sta(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003129 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003130{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003131 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07003132
3133 IWL_DEBUG_RX("Received REPLY_ADD_STA: 0x%02X\n", pkt->u.status);
3134 return;
3135}
3136
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003137static void iwl4965_rx_reply_error(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003138 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003139{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003140 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07003141
3142 IWL_ERROR("Error Reply type 0x%08X cmd %s (0x%02X) "
3143 "seq 0x%04X ser 0x%08X\n",
3144 le32_to_cpu(pkt->u.err_resp.error_type),
3145 get_cmd_string(pkt->u.err_resp.cmd_id),
3146 pkt->u.err_resp.cmd_id,
3147 le16_to_cpu(pkt->u.err_resp.bad_cmd_seq_num),
3148 le32_to_cpu(pkt->u.err_resp.error_info));
3149}
3150
3151#define TX_STATUS_ENTRY(x) case TX_STATUS_FAIL_ ## x: return #x
3152
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003153static void iwl4965_rx_csa(struct iwl_priv *priv, 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;
3156 struct iwl4965_rxon_cmd *rxon = (void *)&priv->active_rxon;
3157 struct iwl4965_csa_notification *csa = &(pkt->u.csa_notif);
Zhu Yib481de92007-09-25 17:54:57 -07003158 IWL_DEBUG_11H("CSA notif: channel %d, status %d\n",
3159 le16_to_cpu(csa->channel), le32_to_cpu(csa->status));
3160 rxon->channel = csa->channel;
3161 priv->staging_rxon.channel = csa->channel;
3162}
3163
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003164static void iwl4965_rx_spectrum_measure_notif(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003165 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003166{
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08003167#ifdef CONFIG_IWL4965_SPECTRUM_MEASUREMENT
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003168 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
3169 struct iwl4965_spectrum_notification *report = &(pkt->u.spectrum_notif);
Zhu Yib481de92007-09-25 17:54:57 -07003170
3171 if (!report->state) {
3172 IWL_DEBUG(IWL_DL_11H | IWL_DL_INFO,
3173 "Spectrum Measure Notification: Start\n");
3174 return;
3175 }
3176
3177 memcpy(&priv->measure_report, report, sizeof(*report));
3178 priv->measurement_status |= MEASUREMENT_READY;
3179#endif
3180}
3181
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003182static void iwl4965_rx_pm_sleep_notif(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003183 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003184{
Tomas Winkler0a6857e2008-03-12 16:58:49 -07003185#ifdef CONFIG_IWLWIFI_DEBUG
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003186 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
3187 struct iwl4965_sleep_notification *sleep = &(pkt->u.sleep_notif);
Zhu Yib481de92007-09-25 17:54:57 -07003188 IWL_DEBUG_RX("sleep mode: %d, src: %d\n",
3189 sleep->pm_sleep_mode, sleep->pm_wakeup_src);
3190#endif
3191}
3192
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003193static void iwl4965_rx_pm_debug_statistics_notif(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003194 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003195{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003196 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07003197 IWL_DEBUG_RADIO("Dumping %d bytes of unhandled "
3198 "notification for %s:\n",
3199 le32_to_cpu(pkt->len), get_cmd_string(pkt->hdr.cmd));
Tomas Winkler0a6857e2008-03-12 16:58:49 -07003200 iwl_print_hex_dump(IWL_DL_RADIO, pkt->u.raw, le32_to_cpu(pkt->len));
Zhu Yib481de92007-09-25 17:54:57 -07003201}
3202
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003203static void iwl4965_bg_beacon_update(struct work_struct *work)
Zhu Yib481de92007-09-25 17:54:57 -07003204{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003205 struct iwl_priv *priv =
3206 container_of(work, struct iwl_priv, beacon_update);
Zhu Yib481de92007-09-25 17:54:57 -07003207 struct sk_buff *beacon;
3208
3209 /* Pull updated AP beacon from mac80211. will fail if not in AP mode */
Johannes Berg32bfd352007-12-19 01:31:26 +01003210 beacon = ieee80211_beacon_get(priv->hw, priv->vif, NULL);
Zhu Yib481de92007-09-25 17:54:57 -07003211
3212 if (!beacon) {
3213 IWL_ERROR("update beacon failed\n");
3214 return;
3215 }
3216
3217 mutex_lock(&priv->mutex);
3218 /* new beacon skb is allocated every time; dispose previous.*/
3219 if (priv->ibss_beacon)
3220 dev_kfree_skb(priv->ibss_beacon);
3221
3222 priv->ibss_beacon = beacon;
3223 mutex_unlock(&priv->mutex);
3224
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003225 iwl4965_send_beacon_cmd(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003226}
3227
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003228static void iwl4965_rx_beacon_notif(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003229 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003230{
Tomas Winkler0a6857e2008-03-12 16:58:49 -07003231#ifdef CONFIG_IWLWIFI_DEBUG
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003232 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
3233 struct iwl4965_beacon_notif *beacon = &(pkt->u.beacon_status);
3234 u8 rate = iwl4965_hw_get_rate(beacon->beacon_notify_hdr.rate_n_flags);
Zhu Yib481de92007-09-25 17:54:57 -07003235
3236 IWL_DEBUG_RX("beacon status %x retries %d iss %d "
3237 "tsf %d %d rate %d\n",
3238 le32_to_cpu(beacon->beacon_notify_hdr.status) & TX_STATUS_MSK,
3239 beacon->beacon_notify_hdr.failure_frame,
3240 le32_to_cpu(beacon->ibss_mgr_status),
3241 le32_to_cpu(beacon->high_tsf),
3242 le32_to_cpu(beacon->low_tsf), rate);
3243#endif
3244
3245 if ((priv->iw_mode == IEEE80211_IF_TYPE_AP) &&
3246 (!test_bit(STATUS_EXIT_PENDING, &priv->status)))
3247 queue_work(priv->workqueue, &priv->beacon_update);
3248}
3249
3250/* Service response to REPLY_SCAN_CMD (0x80) */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003251static void iwl4965_rx_reply_scan(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003252 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003253{
Tomas Winkler0a6857e2008-03-12 16:58:49 -07003254#ifdef CONFIG_IWLWIFI_DEBUG
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003255 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
3256 struct iwl4965_scanreq_notification *notif =
3257 (struct iwl4965_scanreq_notification *)pkt->u.raw;
Zhu Yib481de92007-09-25 17:54:57 -07003258
3259 IWL_DEBUG_RX("Scan request status = 0x%x\n", notif->status);
3260#endif
3261}
3262
3263/* Service SCAN_START_NOTIFICATION (0x82) */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003264static void iwl4965_rx_scan_start_notif(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003265 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003266{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003267 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
3268 struct iwl4965_scanstart_notification *notif =
3269 (struct iwl4965_scanstart_notification *)pkt->u.raw;
Zhu Yib481de92007-09-25 17:54:57 -07003270 priv->scan_start_tsf = le32_to_cpu(notif->tsf_low);
3271 IWL_DEBUG_SCAN("Scan start: "
3272 "%d [802.11%s] "
3273 "(TSF: 0x%08X:%08X) - %d (beacon timer %u)\n",
3274 notif->channel,
3275 notif->band ? "bg" : "a",
3276 notif->tsf_high,
3277 notif->tsf_low, notif->status, notif->beacon_timer);
3278}
3279
3280/* Service SCAN_RESULTS_NOTIFICATION (0x83) */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003281static void iwl4965_rx_scan_results_notif(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003282 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003283{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003284 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
3285 struct iwl4965_scanresults_notification *notif =
3286 (struct iwl4965_scanresults_notification *)pkt->u.raw;
Zhu Yib481de92007-09-25 17:54:57 -07003287
3288 IWL_DEBUG_SCAN("Scan ch.res: "
3289 "%d [802.11%s] "
3290 "(TSF: 0x%08X:%08X) - %d "
3291 "elapsed=%lu usec (%dms since last)\n",
3292 notif->channel,
3293 notif->band ? "bg" : "a",
3294 le32_to_cpu(notif->tsf_high),
3295 le32_to_cpu(notif->tsf_low),
3296 le32_to_cpu(notif->statistics[0]),
3297 le32_to_cpu(notif->tsf_low) - priv->scan_start_tsf,
3298 jiffies_to_msecs(elapsed_jiffies
3299 (priv->last_scan_jiffies, jiffies)));
3300
3301 priv->last_scan_jiffies = jiffies;
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08003302 priv->next_scan_jiffies = 0;
Zhu Yib481de92007-09-25 17:54:57 -07003303}
3304
3305/* Service SCAN_COMPLETE_NOTIFICATION (0x84) */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003306static void iwl4965_rx_scan_complete_notif(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003307 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003308{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003309 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
3310 struct iwl4965_scancomplete_notification *scan_notif = (void *)pkt->u.raw;
Zhu Yib481de92007-09-25 17:54:57 -07003311
3312 IWL_DEBUG_SCAN("Scan complete: %d channels (TSF 0x%08X:%08X) - %d\n",
3313 scan_notif->scanned_channels,
3314 scan_notif->tsf_low,
3315 scan_notif->tsf_high, scan_notif->status);
3316
3317 /* The HW is no longer scanning */
3318 clear_bit(STATUS_SCAN_HW, &priv->status);
3319
3320 /* The scan completion notification came in, so kill that timer... */
3321 cancel_delayed_work(&priv->scan_check);
3322
3323 IWL_DEBUG_INFO("Scan pass on %sGHz took %dms\n",
3324 (priv->scan_bands == 2) ? "2.4" : "5.2",
3325 jiffies_to_msecs(elapsed_jiffies
3326 (priv->scan_pass_start, jiffies)));
3327
3328 /* Remove this scanned band from the list
3329 * of pending bands to scan */
3330 priv->scan_bands--;
3331
3332 /* If a request to abort was given, or the scan did not succeed
3333 * then we reset the scan state machine and terminate,
3334 * re-queuing another scan if one has been requested */
3335 if (test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
3336 IWL_DEBUG_INFO("Aborted scan completed.\n");
3337 clear_bit(STATUS_SCAN_ABORTING, &priv->status);
3338 } else {
3339 /* If there are more bands on this scan pass reschedule */
3340 if (priv->scan_bands > 0)
3341 goto reschedule;
3342 }
3343
3344 priv->last_scan_jiffies = jiffies;
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08003345 priv->next_scan_jiffies = 0;
Zhu Yib481de92007-09-25 17:54:57 -07003346 IWL_DEBUG_INFO("Setting scan to off\n");
3347
3348 clear_bit(STATUS_SCANNING, &priv->status);
3349
3350 IWL_DEBUG_INFO("Scan took %dms\n",
3351 jiffies_to_msecs(elapsed_jiffies(priv->scan_start, jiffies)));
3352
3353 queue_work(priv->workqueue, &priv->scan_completed);
3354
3355 return;
3356
3357reschedule:
3358 priv->scan_pass_start = jiffies;
3359 queue_work(priv->workqueue, &priv->request_scan);
3360}
3361
3362/* Handle notification from uCode that card's power state is changing
3363 * due to software, hardware, or critical temperature RFKILL */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003364static void iwl4965_rx_card_state_notif(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003365 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003366{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003367 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07003368 u32 flags = le32_to_cpu(pkt->u.card_state_notif.flags);
3369 unsigned long status = priv->status;
3370
3371 IWL_DEBUG_RF_KILL("Card state received: HW:%s SW:%s\n",
3372 (flags & HW_CARD_DISABLED) ? "Kill" : "On",
3373 (flags & SW_CARD_DISABLED) ? "Kill" : "On");
3374
3375 if (flags & (SW_CARD_DISABLED | HW_CARD_DISABLED |
3376 RF_CARD_DISABLED)) {
3377
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003378 iwl_write32(priv, CSR_UCODE_DRV_GP1_SET,
Zhu Yib481de92007-09-25 17:54:57 -07003379 CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
3380
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003381 if (!iwl_grab_nic_access(priv)) {
3382 iwl_write_direct32(
Zhu Yib481de92007-09-25 17:54:57 -07003383 priv, HBUS_TARG_MBX_C,
3384 HBUS_TARG_MBX_C_REG_BIT_CMD_BLOCKED);
3385
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003386 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003387 }
3388
3389 if (!(flags & RXON_CARD_DISABLED)) {
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003390 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR,
Zhu Yib481de92007-09-25 17:54:57 -07003391 CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003392 if (!iwl_grab_nic_access(priv)) {
3393 iwl_write_direct32(
Zhu Yib481de92007-09-25 17:54:57 -07003394 priv, HBUS_TARG_MBX_C,
3395 HBUS_TARG_MBX_C_REG_BIT_CMD_BLOCKED);
3396
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003397 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003398 }
3399 }
3400
3401 if (flags & RF_CARD_DISABLED) {
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003402 iwl_write32(priv, CSR_UCODE_DRV_GP1_SET,
Zhu Yib481de92007-09-25 17:54:57 -07003403 CSR_UCODE_DRV_GP1_REG_BIT_CT_KILL_EXIT);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003404 iwl_read32(priv, CSR_UCODE_DRV_GP1);
3405 if (!iwl_grab_nic_access(priv))
3406 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003407 }
3408 }
3409
3410 if (flags & HW_CARD_DISABLED)
3411 set_bit(STATUS_RF_KILL_HW, &priv->status);
3412 else
3413 clear_bit(STATUS_RF_KILL_HW, &priv->status);
3414
3415
3416 if (flags & SW_CARD_DISABLED)
3417 set_bit(STATUS_RF_KILL_SW, &priv->status);
3418 else
3419 clear_bit(STATUS_RF_KILL_SW, &priv->status);
3420
3421 if (!(flags & RXON_CARD_DISABLED))
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003422 iwl4965_scan_cancel(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003423
3424 if ((test_bit(STATUS_RF_KILL_HW, &status) !=
3425 test_bit(STATUS_RF_KILL_HW, &priv->status)) ||
3426 (test_bit(STATUS_RF_KILL_SW, &status) !=
3427 test_bit(STATUS_RF_KILL_SW, &priv->status)))
3428 queue_work(priv->workqueue, &priv->rf_kill);
3429 else
3430 wake_up_interruptible(&priv->wait_command_queue);
3431}
3432
3433/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003434 * iwl4965_setup_rx_handlers - Initialize Rx handler callbacks
Zhu Yib481de92007-09-25 17:54:57 -07003435 *
3436 * Setup the RX handlers for each of the reply types sent from the uCode
3437 * to the host.
3438 *
3439 * This function chains into the hardware specific files for them to setup
3440 * any hardware specific handlers as well.
3441 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003442static void iwl4965_setup_rx_handlers(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003443{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003444 priv->rx_handlers[REPLY_ALIVE] = iwl4965_rx_reply_alive;
3445 priv->rx_handlers[REPLY_ADD_STA] = iwl4965_rx_reply_add_sta;
3446 priv->rx_handlers[REPLY_ERROR] = iwl4965_rx_reply_error;
3447 priv->rx_handlers[CHANNEL_SWITCH_NOTIFICATION] = iwl4965_rx_csa;
Zhu Yib481de92007-09-25 17:54:57 -07003448 priv->rx_handlers[SPECTRUM_MEASURE_NOTIFICATION] =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003449 iwl4965_rx_spectrum_measure_notif;
3450 priv->rx_handlers[PM_SLEEP_NOTIFICATION] = iwl4965_rx_pm_sleep_notif;
Zhu Yib481de92007-09-25 17:54:57 -07003451 priv->rx_handlers[PM_DEBUG_STATISTIC_NOTIFIC] =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003452 iwl4965_rx_pm_debug_statistics_notif;
3453 priv->rx_handlers[BEACON_NOTIFICATION] = iwl4965_rx_beacon_notif;
Zhu Yib481de92007-09-25 17:54:57 -07003454
Ben Cahill9fbab512007-11-29 11:09:47 +08003455 /*
3456 * The same handler is used for both the REPLY to a discrete
3457 * statistics request from the host as well as for the periodic
3458 * statistics notifications (after received beacons) from the uCode.
Zhu Yib481de92007-09-25 17:54:57 -07003459 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003460 priv->rx_handlers[REPLY_STATISTICS_CMD] = iwl4965_hw_rx_statistics;
3461 priv->rx_handlers[STATISTICS_NOTIFICATION] = iwl4965_hw_rx_statistics;
Zhu Yib481de92007-09-25 17:54:57 -07003462
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003463 priv->rx_handlers[REPLY_SCAN_CMD] = iwl4965_rx_reply_scan;
3464 priv->rx_handlers[SCAN_START_NOTIFICATION] = iwl4965_rx_scan_start_notif;
Zhu Yib481de92007-09-25 17:54:57 -07003465 priv->rx_handlers[SCAN_RESULTS_NOTIFICATION] =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003466 iwl4965_rx_scan_results_notif;
Zhu Yib481de92007-09-25 17:54:57 -07003467 priv->rx_handlers[SCAN_COMPLETE_NOTIFICATION] =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003468 iwl4965_rx_scan_complete_notif;
3469 priv->rx_handlers[CARD_STATE_NOTIFICATION] = iwl4965_rx_card_state_notif;
3470 priv->rx_handlers[REPLY_TX] = iwl4965_rx_reply_tx;
Zhu Yib481de92007-09-25 17:54:57 -07003471
Ben Cahill9fbab512007-11-29 11:09:47 +08003472 /* Set up hardware specific Rx handlers */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003473 iwl4965_hw_rx_handler_setup(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003474}
3475
3476/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003477 * iwl4965_tx_cmd_complete - Pull unused buffers off the queue and reclaim them
Zhu Yib481de92007-09-25 17:54:57 -07003478 * @rxb: Rx buffer to reclaim
3479 *
3480 * If an Rx buffer has an async callback associated with it the callback
3481 * will be executed. The attached skb (if present) will only be freed
3482 * if the callback returns 1
3483 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003484static void iwl4965_tx_cmd_complete(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003485 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003486{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003487 struct iwl4965_rx_packet *pkt = (struct iwl4965_rx_packet *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07003488 u16 sequence = le16_to_cpu(pkt->hdr.sequence);
3489 int txq_id = SEQ_TO_QUEUE(sequence);
3490 int index = SEQ_TO_INDEX(sequence);
3491 int huge = sequence & SEQ_HUGE_FRAME;
3492 int cmd_index;
Tomas Winkler857485c2008-03-21 13:53:44 -07003493 struct iwl_cmd *cmd;
Zhu Yib481de92007-09-25 17:54:57 -07003494
3495 /* If a Tx command is being handled and it isn't in the actual
3496 * command queue then there a command routing bug has been introduced
3497 * in the queue management code. */
3498 if (txq_id != IWL_CMD_QUEUE_NUM)
3499 IWL_ERROR("Error wrong command queue %d command id 0x%X\n",
3500 txq_id, pkt->hdr.cmd);
3501 BUG_ON(txq_id != IWL_CMD_QUEUE_NUM);
3502
3503 cmd_index = get_cmd_index(&priv->txq[IWL_CMD_QUEUE_NUM].q, index, huge);
3504 cmd = &priv->txq[IWL_CMD_QUEUE_NUM].cmd[cmd_index];
3505
3506 /* Input error checking is done when commands are added to queue. */
3507 if (cmd->meta.flags & CMD_WANT_SKB) {
3508 cmd->meta.source->u.skb = rxb->skb;
3509 rxb->skb = NULL;
3510 } else if (cmd->meta.u.callback &&
3511 !cmd->meta.u.callback(priv, cmd, rxb->skb))
3512 rxb->skb = NULL;
3513
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003514 iwl4965_tx_queue_reclaim(priv, txq_id, index);
Zhu Yib481de92007-09-25 17:54:57 -07003515
3516 if (!(cmd->meta.flags & CMD_ASYNC)) {
3517 clear_bit(STATUS_HCMD_ACTIVE, &priv->status);
3518 wake_up_interruptible(&priv->wait_command_queue);
3519 }
3520}
3521
3522/************************** RX-FUNCTIONS ****************************/
3523/*
3524 * Rx theory of operation
3525 *
Ben Cahill9fbab512007-11-29 11:09:47 +08003526 * Driver allocates a circular buffer of Receive Buffer Descriptors (RBDs),
3527 * each of which point to Receive Buffers to be filled by 4965. These get
3528 * used not only for Rx frames, but for any command response or notification
3529 * from the 4965. The driver and 4965 manage the Rx buffers by means
3530 * of indexes into the circular buffer.
Zhu Yib481de92007-09-25 17:54:57 -07003531 *
3532 * Rx Queue Indexes
3533 * The host/firmware share two index registers for managing the Rx buffers.
3534 *
3535 * The READ index maps to the first position that the firmware may be writing
3536 * to -- the driver can read up to (but not including) this position and get
3537 * good data.
3538 * The READ index is managed by the firmware once the card is enabled.
3539 *
3540 * The WRITE index maps to the last position the driver has read from -- the
3541 * position preceding WRITE is the last slot the firmware can place a packet.
3542 *
3543 * The queue is empty (no good data) if WRITE = READ - 1, and is full if
3544 * WRITE = READ.
3545 *
Ben Cahill9fbab512007-11-29 11:09:47 +08003546 * During initialization, the host sets up the READ queue position to the first
Zhu Yib481de92007-09-25 17:54:57 -07003547 * INDEX position, and WRITE to the last (READ - 1 wrapped)
3548 *
Ben Cahill9fbab512007-11-29 11:09:47 +08003549 * When the firmware places a packet in a buffer, it will advance the READ index
Zhu Yib481de92007-09-25 17:54:57 -07003550 * and fire the RX interrupt. The driver can then query the READ index and
3551 * process as many packets as possible, moving the WRITE index forward as it
3552 * resets the Rx queue buffers with new memory.
3553 *
3554 * The management in the driver is as follows:
3555 * + A list of pre-allocated SKBs is stored in iwl->rxq->rx_free. When
3556 * iwl->rxq->free_count drops to or below RX_LOW_WATERMARK, work is scheduled
Ian Schram01ebd062007-10-25 17:15:22 +08003557 * to replenish the iwl->rxq->rx_free.
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003558 * + In iwl4965_rx_replenish (scheduled) if 'processed' != 'read' then the
Zhu Yib481de92007-09-25 17:54:57 -07003559 * iwl->rxq is replenished and the READ INDEX is updated (updating the
3560 * 'processed' and 'read' driver indexes as well)
3561 * + A received packet is processed and handed to the kernel network stack,
3562 * detached from the iwl->rxq. The driver 'processed' index is updated.
3563 * + The Host/Firmware iwl->rxq is replenished at tasklet time from the rx_free
3564 * list. If there are no allocated buffers in iwl->rxq->rx_free, the READ
3565 * INDEX is not incremented and iwl->status(RX_STALLED) is set. If there
3566 * were enough free buffers and RX_STALLED is set it is cleared.
3567 *
3568 *
3569 * Driver sequence:
3570 *
Ben Cahill9fbab512007-11-29 11:09:47 +08003571 * iwl4965_rx_queue_alloc() Allocates rx_free
3572 * iwl4965_rx_replenish() Replenishes rx_free list from rx_used, and calls
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003573 * iwl4965_rx_queue_restock
Ben Cahill9fbab512007-11-29 11:09:47 +08003574 * iwl4965_rx_queue_restock() Moves available buffers from rx_free into Rx
Zhu Yib481de92007-09-25 17:54:57 -07003575 * queue, updates firmware pointers, and updates
3576 * the WRITE index. If insufficient rx_free buffers
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003577 * are available, schedules iwl4965_rx_replenish
Zhu Yib481de92007-09-25 17:54:57 -07003578 *
3579 * -- enable interrupts --
Ben Cahill9fbab512007-11-29 11:09:47 +08003580 * ISR - iwl4965_rx() Detach iwl4965_rx_mem_buffers from pool up to the
Zhu Yib481de92007-09-25 17:54:57 -07003581 * READ INDEX, detaching the SKB from the pool.
3582 * Moves the packet buffer from queue to rx_used.
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003583 * Calls iwl4965_rx_queue_restock to refill any empty
Zhu Yib481de92007-09-25 17:54:57 -07003584 * slots.
3585 * ...
3586 *
3587 */
3588
3589/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003590 * iwl4965_rx_queue_space - Return number of free slots available in queue.
Zhu Yib481de92007-09-25 17:54:57 -07003591 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003592static int iwl4965_rx_queue_space(const struct iwl4965_rx_queue *q)
Zhu Yib481de92007-09-25 17:54:57 -07003593{
3594 int s = q->read - q->write;
3595 if (s <= 0)
3596 s += RX_QUEUE_SIZE;
3597 /* keep some buffer to not confuse full and empty queue */
3598 s -= 2;
3599 if (s < 0)
3600 s = 0;
3601 return s;
3602}
3603
3604/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003605 * iwl4965_rx_queue_update_write_ptr - Update the write pointer for the RX queue
Zhu Yib481de92007-09-25 17:54:57 -07003606 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003607int iwl4965_rx_queue_update_write_ptr(struct iwl_priv *priv, struct iwl4965_rx_queue *q)
Zhu Yib481de92007-09-25 17:54:57 -07003608{
3609 u32 reg = 0;
3610 int rc = 0;
3611 unsigned long flags;
3612
3613 spin_lock_irqsave(&q->lock, flags);
3614
3615 if (q->need_update == 0)
3616 goto exit_unlock;
3617
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003618 /* If power-saving is in use, make sure device is awake */
Zhu Yib481de92007-09-25 17:54:57 -07003619 if (test_bit(STATUS_POWER_PMI, &priv->status)) {
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003620 reg = iwl_read32(priv, CSR_UCODE_DRV_GP1);
Zhu Yib481de92007-09-25 17:54:57 -07003621
3622 if (reg & CSR_UCODE_DRV_GP1_BIT_MAC_SLEEP) {
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003623 iwl_set_bit(priv, CSR_GP_CNTRL,
Zhu Yib481de92007-09-25 17:54:57 -07003624 CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
3625 goto exit_unlock;
3626 }
3627
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003628 rc = iwl_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003629 if (rc)
3630 goto exit_unlock;
3631
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003632 /* Device expects a multiple of 8 */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003633 iwl_write_direct32(priv, FH_RSCSR_CHNL0_WPTR,
Zhu Yib481de92007-09-25 17:54:57 -07003634 q->write & ~0x7);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003635 iwl_release_nic_access(priv);
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003636
3637 /* Else device is assumed to be awake */
Zhu Yib481de92007-09-25 17:54:57 -07003638 } else
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003639 /* Device expects a multiple of 8 */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003640 iwl_write32(priv, FH_RSCSR_CHNL0_WPTR, q->write & ~0x7);
Zhu Yib481de92007-09-25 17:54:57 -07003641
3642
3643 q->need_update = 0;
3644
3645 exit_unlock:
3646 spin_unlock_irqrestore(&q->lock, flags);
3647 return rc;
3648}
3649
3650/**
Ben Cahill9fbab512007-11-29 11:09:47 +08003651 * iwl4965_dma_addr2rbd_ptr - convert a DMA address to a uCode read buffer ptr
Zhu Yib481de92007-09-25 17:54:57 -07003652 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003653static inline __le32 iwl4965_dma_addr2rbd_ptr(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07003654 dma_addr_t dma_addr)
3655{
3656 return cpu_to_le32((u32)(dma_addr >> 8));
3657}
3658
3659
3660/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003661 * iwl4965_rx_queue_restock - refill RX queue from pre-allocated pool
Zhu Yib481de92007-09-25 17:54:57 -07003662 *
Ben Cahill9fbab512007-11-29 11:09:47 +08003663 * If there are slots in the RX queue that need to be restocked,
Zhu Yib481de92007-09-25 17:54:57 -07003664 * and we have free pre-allocated buffers, fill the ranks as much
Ben Cahill9fbab512007-11-29 11:09:47 +08003665 * as we can, pulling from rx_free.
Zhu Yib481de92007-09-25 17:54:57 -07003666 *
3667 * This moves the 'write' index forward to catch up with 'processed', and
3668 * also updates the memory address in the firmware to reference the new
3669 * target buffer.
3670 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003671static int iwl4965_rx_queue_restock(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003672{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003673 struct iwl4965_rx_queue *rxq = &priv->rxq;
Zhu Yib481de92007-09-25 17:54:57 -07003674 struct list_head *element;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003675 struct iwl4965_rx_mem_buffer *rxb;
Zhu Yib481de92007-09-25 17:54:57 -07003676 unsigned long flags;
3677 int write, rc;
3678
3679 spin_lock_irqsave(&rxq->lock, flags);
3680 write = rxq->write & ~0x7;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003681 while ((iwl4965_rx_queue_space(rxq) > 0) && (rxq->free_count)) {
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003682 /* Get next free Rx buffer, remove from free list */
Zhu Yib481de92007-09-25 17:54:57 -07003683 element = rxq->rx_free.next;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003684 rxb = list_entry(element, struct iwl4965_rx_mem_buffer, list);
Zhu Yib481de92007-09-25 17:54:57 -07003685 list_del(element);
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003686
3687 /* Point to Rx buffer via next RBD in circular buffer */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003688 rxq->bd[rxq->write] = iwl4965_dma_addr2rbd_ptr(priv, rxb->dma_addr);
Zhu Yib481de92007-09-25 17:54:57 -07003689 rxq->queue[rxq->write] = rxb;
3690 rxq->write = (rxq->write + 1) & RX_QUEUE_MASK;
3691 rxq->free_count--;
3692 }
3693 spin_unlock_irqrestore(&rxq->lock, flags);
3694 /* If the pre-allocated buffer pool is dropping low, schedule to
3695 * refill it */
3696 if (rxq->free_count <= RX_LOW_WATERMARK)
3697 queue_work(priv->workqueue, &priv->rx_replenish);
3698
3699
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003700 /* If we've added more space for the firmware to place data, tell it.
3701 * Increment device's write pointer in multiples of 8. */
Zhu Yib481de92007-09-25 17:54:57 -07003702 if ((write != (rxq->write & ~0x7))
3703 || (abs(rxq->write - rxq->read) > 7)) {
3704 spin_lock_irqsave(&rxq->lock, flags);
3705 rxq->need_update = 1;
3706 spin_unlock_irqrestore(&rxq->lock, flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003707 rc = iwl4965_rx_queue_update_write_ptr(priv, rxq);
Zhu Yib481de92007-09-25 17:54:57 -07003708 if (rc)
3709 return rc;
3710 }
3711
3712 return 0;
3713}
3714
3715/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003716 * iwl4965_rx_replenish - Move all used packet from rx_used to rx_free
Zhu Yib481de92007-09-25 17:54:57 -07003717 *
3718 * When moving to rx_free an SKB is allocated for the slot.
3719 *
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003720 * Also restock the Rx queue via iwl4965_rx_queue_restock.
Ian Schram01ebd062007-10-25 17:15:22 +08003721 * This is called as a scheduled work item (except for during initialization)
Zhu Yib481de92007-09-25 17:54:57 -07003722 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003723static void iwl4965_rx_allocate(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003724{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003725 struct iwl4965_rx_queue *rxq = &priv->rxq;
Zhu Yib481de92007-09-25 17:54:57 -07003726 struct list_head *element;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003727 struct iwl4965_rx_mem_buffer *rxb;
Zhu Yib481de92007-09-25 17:54:57 -07003728 unsigned long flags;
3729 spin_lock_irqsave(&rxq->lock, flags);
3730 while (!list_empty(&rxq->rx_used)) {
3731 element = rxq->rx_used.next;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003732 rxb = list_entry(element, struct iwl4965_rx_mem_buffer, list);
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003733
3734 /* Alloc a new receive buffer */
Zhu Yib481de92007-09-25 17:54:57 -07003735 rxb->skb =
Ron Rindjunsky9ee1ba42007-11-26 16:14:42 +02003736 alloc_skb(priv->hw_setting.rx_buf_size,
3737 __GFP_NOWARN | GFP_ATOMIC);
Zhu Yib481de92007-09-25 17:54:57 -07003738 if (!rxb->skb) {
3739 if (net_ratelimit())
3740 printk(KERN_CRIT DRV_NAME
3741 ": Can not allocate SKB buffers\n");
3742 /* We don't reschedule replenish work here -- we will
3743 * call the restock method and if it still needs
3744 * more buffers it will schedule replenish */
3745 break;
3746 }
3747 priv->alloc_rxb_skb++;
3748 list_del(element);
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003749
3750 /* Get physical address of RB/SKB */
Zhu Yib481de92007-09-25 17:54:57 -07003751 rxb->dma_addr =
3752 pci_map_single(priv->pci_dev, rxb->skb->data,
Ron Rindjunsky9ee1ba42007-11-26 16:14:42 +02003753 priv->hw_setting.rx_buf_size, PCI_DMA_FROMDEVICE);
Zhu Yib481de92007-09-25 17:54:57 -07003754 list_add_tail(&rxb->list, &rxq->rx_free);
3755 rxq->free_count++;
3756 }
3757 spin_unlock_irqrestore(&rxq->lock, flags);
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08003758}
3759
3760/*
3761 * this should be called while priv->lock is locked
3762*/
Tomas Winkler4fd1f842007-12-05 20:59:58 +02003763static void __iwl4965_rx_replenish(void *data)
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08003764{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003765 struct iwl_priv *priv = data;
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08003766
3767 iwl4965_rx_allocate(priv);
3768 iwl4965_rx_queue_restock(priv);
3769}
3770
3771
3772void iwl4965_rx_replenish(void *data)
3773{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003774 struct iwl_priv *priv = data;
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08003775 unsigned long flags;
3776
3777 iwl4965_rx_allocate(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003778
3779 spin_lock_irqsave(&priv->lock, flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003780 iwl4965_rx_queue_restock(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003781 spin_unlock_irqrestore(&priv->lock, flags);
3782}
3783
3784/* Assumes that the skb field of the buffers in 'pool' is kept accurate.
Ben Cahill9fbab512007-11-29 11:09:47 +08003785 * If an SKB has been detached, the POOL needs to have its SKB set to NULL
Zhu Yib481de92007-09-25 17:54:57 -07003786 * This free routine walks the list of POOL entries and if SKB is set to
3787 * non NULL it is unmapped and freed
3788 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003789static void iwl4965_rx_queue_free(struct iwl_priv *priv, struct iwl4965_rx_queue *rxq)
Zhu Yib481de92007-09-25 17:54:57 -07003790{
3791 int i;
3792 for (i = 0; i < RX_QUEUE_SIZE + RX_FREE_BUFFERS; i++) {
3793 if (rxq->pool[i].skb != NULL) {
3794 pci_unmap_single(priv->pci_dev,
3795 rxq->pool[i].dma_addr,
Ron Rindjunsky9ee1ba42007-11-26 16:14:42 +02003796 priv->hw_setting.rx_buf_size,
3797 PCI_DMA_FROMDEVICE);
Zhu Yib481de92007-09-25 17:54:57 -07003798 dev_kfree_skb(rxq->pool[i].skb);
3799 }
3800 }
3801
3802 pci_free_consistent(priv->pci_dev, 4 * RX_QUEUE_SIZE, rxq->bd,
3803 rxq->dma_addr);
3804 rxq->bd = NULL;
3805}
3806
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003807int iwl4965_rx_queue_alloc(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003808{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003809 struct iwl4965_rx_queue *rxq = &priv->rxq;
Zhu Yib481de92007-09-25 17:54:57 -07003810 struct pci_dev *dev = priv->pci_dev;
3811 int i;
3812
3813 spin_lock_init(&rxq->lock);
3814 INIT_LIST_HEAD(&rxq->rx_free);
3815 INIT_LIST_HEAD(&rxq->rx_used);
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003816
3817 /* Alloc the circular buffer of Read Buffer Descriptors (RBDs) */
Zhu Yib481de92007-09-25 17:54:57 -07003818 rxq->bd = pci_alloc_consistent(dev, 4 * RX_QUEUE_SIZE, &rxq->dma_addr);
3819 if (!rxq->bd)
3820 return -ENOMEM;
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003821
Zhu Yib481de92007-09-25 17:54:57 -07003822 /* Fill the rx_used queue with _all_ of the Rx buffers */
3823 for (i = 0; i < RX_FREE_BUFFERS + RX_QUEUE_SIZE; i++)
3824 list_add_tail(&rxq->pool[i].list, &rxq->rx_used);
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003825
Zhu Yib481de92007-09-25 17:54:57 -07003826 /* Set us so that we have processed and used all buffers, but have
3827 * not restocked the Rx queue with fresh buffers */
3828 rxq->read = rxq->write = 0;
3829 rxq->free_count = 0;
3830 rxq->need_update = 0;
3831 return 0;
3832}
3833
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003834void iwl4965_rx_queue_reset(struct iwl_priv *priv, struct iwl4965_rx_queue *rxq)
Zhu Yib481de92007-09-25 17:54:57 -07003835{
3836 unsigned long flags;
3837 int i;
3838 spin_lock_irqsave(&rxq->lock, flags);
3839 INIT_LIST_HEAD(&rxq->rx_free);
3840 INIT_LIST_HEAD(&rxq->rx_used);
3841 /* Fill the rx_used queue with _all_ of the Rx buffers */
3842 for (i = 0; i < RX_FREE_BUFFERS + RX_QUEUE_SIZE; i++) {
3843 /* In the reset function, these buffers may have been allocated
3844 * to an SKB, so we need to unmap and free potential storage */
3845 if (rxq->pool[i].skb != NULL) {
3846 pci_unmap_single(priv->pci_dev,
3847 rxq->pool[i].dma_addr,
Ron Rindjunsky9ee1ba42007-11-26 16:14:42 +02003848 priv->hw_setting.rx_buf_size,
3849 PCI_DMA_FROMDEVICE);
Zhu Yib481de92007-09-25 17:54:57 -07003850 priv->alloc_rxb_skb--;
3851 dev_kfree_skb(rxq->pool[i].skb);
3852 rxq->pool[i].skb = NULL;
3853 }
3854 list_add_tail(&rxq->pool[i].list, &rxq->rx_used);
3855 }
3856
3857 /* Set us so that we have processed and used all buffers, but have
3858 * not restocked the Rx queue with fresh buffers */
3859 rxq->read = rxq->write = 0;
3860 rxq->free_count = 0;
3861 spin_unlock_irqrestore(&rxq->lock, flags);
3862}
3863
3864/* Convert linear signal-to-noise ratio into dB */
3865static u8 ratio2dB[100] = {
3866/* 0 1 2 3 4 5 6 7 8 9 */
3867 0, 0, 6, 10, 12, 14, 16, 17, 18, 19, /* 00 - 09 */
3868 20, 21, 22, 22, 23, 23, 24, 25, 26, 26, /* 10 - 19 */
3869 26, 26, 26, 27, 27, 28, 28, 28, 29, 29, /* 20 - 29 */
3870 29, 30, 30, 30, 31, 31, 31, 31, 32, 32, /* 30 - 39 */
3871 32, 32, 32, 33, 33, 33, 33, 33, 34, 34, /* 40 - 49 */
3872 34, 34, 34, 34, 35, 35, 35, 35, 35, 35, /* 50 - 59 */
3873 36, 36, 36, 36, 36, 36, 36, 37, 37, 37, /* 60 - 69 */
3874 37, 37, 37, 37, 37, 38, 38, 38, 38, 38, /* 70 - 79 */
3875 38, 38, 38, 38, 38, 39, 39, 39, 39, 39, /* 80 - 89 */
3876 39, 39, 39, 39, 39, 40, 40, 40, 40, 40 /* 90 - 99 */
3877};
3878
3879/* Calculates a relative dB value from a ratio of linear
3880 * (i.e. not dB) signal levels.
3881 * Conversion assumes that levels are voltages (20*log), not powers (10*log). */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003882int iwl4965_calc_db_from_ratio(int sig_ratio)
Zhu Yib481de92007-09-25 17:54:57 -07003883{
Adrian Bunkc899a572007-10-14 19:51:15 +02003884 /* 1000:1 or higher just report as 60 dB */
3885 if (sig_ratio >= 1000)
Zhu Yib481de92007-09-25 17:54:57 -07003886 return 60;
3887
Adrian Bunkc899a572007-10-14 19:51:15 +02003888 /* 100:1 or higher, divide by 10 and use table,
Zhu Yib481de92007-09-25 17:54:57 -07003889 * add 20 dB to make up for divide by 10 */
Adrian Bunkc899a572007-10-14 19:51:15 +02003890 if (sig_ratio >= 100)
Zhu Yib481de92007-09-25 17:54:57 -07003891 return (20 + (int)ratio2dB[sig_ratio/10]);
3892
3893 /* We shouldn't see this */
3894 if (sig_ratio < 1)
3895 return 0;
3896
3897 /* Use table for ratios 1:1 - 99:1 */
3898 return (int)ratio2dB[sig_ratio];
3899}
3900
3901#define PERFECT_RSSI (-20) /* dBm */
3902#define WORST_RSSI (-95) /* dBm */
3903#define RSSI_RANGE (PERFECT_RSSI - WORST_RSSI)
3904
3905/* Calculate an indication of rx signal quality (a percentage, not dBm!).
3906 * See http://www.ces.clemson.edu/linux/signal_quality.shtml for info
3907 * about formulas used below. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003908int iwl4965_calc_sig_qual(int rssi_dbm, int noise_dbm)
Zhu Yib481de92007-09-25 17:54:57 -07003909{
3910 int sig_qual;
3911 int degradation = PERFECT_RSSI - rssi_dbm;
3912
3913 /* If we get a noise measurement, use signal-to-noise ratio (SNR)
3914 * as indicator; formula is (signal dbm - noise dbm).
3915 * SNR at or above 40 is a great signal (100%).
3916 * Below that, scale to fit SNR of 0 - 40 dB within 0 - 100% indicator.
3917 * Weakest usable signal is usually 10 - 15 dB SNR. */
3918 if (noise_dbm) {
3919 if (rssi_dbm - noise_dbm >= 40)
3920 return 100;
3921 else if (rssi_dbm < noise_dbm)
3922 return 0;
3923 sig_qual = ((rssi_dbm - noise_dbm) * 5) / 2;
3924
3925 /* Else use just the signal level.
3926 * This formula is a least squares fit of data points collected and
3927 * compared with a reference system that had a percentage (%) display
3928 * for signal quality. */
3929 } else
3930 sig_qual = (100 * (RSSI_RANGE * RSSI_RANGE) - degradation *
3931 (15 * RSSI_RANGE + 62 * degradation)) /
3932 (RSSI_RANGE * RSSI_RANGE);
3933
3934 if (sig_qual > 100)
3935 sig_qual = 100;
3936 else if (sig_qual < 1)
3937 sig_qual = 0;
3938
3939 return sig_qual;
3940}
3941
3942/**
Ben Cahill9fbab512007-11-29 11:09:47 +08003943 * iwl4965_rx_handle - Main entry function for receiving responses from uCode
Zhu Yib481de92007-09-25 17:54:57 -07003944 *
3945 * Uses the priv->rx_handlers callback function array to invoke
3946 * the appropriate handlers, including command responses,
3947 * frame-received notifications, and other notifications.
3948 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003949static void iwl4965_rx_handle(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003950{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003951 struct iwl4965_rx_mem_buffer *rxb;
3952 struct iwl4965_rx_packet *pkt;
3953 struct iwl4965_rx_queue *rxq = &priv->rxq;
Zhu Yib481de92007-09-25 17:54:57 -07003954 u32 r, i;
3955 int reclaim;
3956 unsigned long flags;
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08003957 u8 fill_rx = 0;
Mohamed Abbasd68ab682008-02-07 13:16:33 -08003958 u32 count = 8;
Zhu Yib481de92007-09-25 17:54:57 -07003959
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003960 /* uCode's read index (stored in shared DRAM) indicates the last Rx
3961 * buffer that the driver may process (last buffer filled by ucode). */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003962 r = iwl4965_hw_get_rx_read(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003963 i = rxq->read;
3964
3965 /* Rx interrupt, but nothing sent from uCode */
3966 if (i == r)
3967 IWL_DEBUG(IWL_DL_RX | IWL_DL_ISR, "r = %d, i = %d\n", r, i);
3968
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08003969 if (iwl4965_rx_queue_space(rxq) > (RX_QUEUE_SIZE / 2))
3970 fill_rx = 1;
3971
Zhu Yib481de92007-09-25 17:54:57 -07003972 while (i != r) {
3973 rxb = rxq->queue[i];
3974
Ben Cahill9fbab512007-11-29 11:09:47 +08003975 /* If an RXB doesn't have a Rx queue slot associated with it,
Zhu Yib481de92007-09-25 17:54:57 -07003976 * then a bug has been introduced in the queue refilling
3977 * routines -- catch it here */
3978 BUG_ON(rxb == NULL);
3979
3980 rxq->queue[i] = NULL;
3981
3982 pci_dma_sync_single_for_cpu(priv->pci_dev, rxb->dma_addr,
Ron Rindjunsky9ee1ba42007-11-26 16:14:42 +02003983 priv->hw_setting.rx_buf_size,
Zhu Yib481de92007-09-25 17:54:57 -07003984 PCI_DMA_FROMDEVICE);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003985 pkt = (struct iwl4965_rx_packet *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07003986
3987 /* Reclaim a command buffer only if this packet is a response
3988 * to a (driver-originated) command.
3989 * If the packet (e.g. Rx frame) originated from uCode,
3990 * there is no command buffer to reclaim.
3991 * Ucode should set SEQ_RX_FRAME bit if ucode-originated,
3992 * but apparently a few don't get set; catch them here. */
3993 reclaim = !(pkt->hdr.sequence & SEQ_RX_FRAME) &&
3994 (pkt->hdr.cmd != REPLY_RX_PHY_CMD) &&
Tomas Winkler857485c2008-03-21 13:53:44 -07003995 (pkt->hdr.cmd != REPLY_RX) &&
Zhu Yicfe01702007-09-27 11:27:31 +08003996 (pkt->hdr.cmd != REPLY_COMPRESSED_BA) &&
Zhu Yib481de92007-09-25 17:54:57 -07003997 (pkt->hdr.cmd != STATISTICS_NOTIFICATION) &&
3998 (pkt->hdr.cmd != REPLY_TX);
3999
4000 /* Based on type of command response or notification,
4001 * handle those that need handling via function in
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004002 * rx_handlers table. See iwl4965_setup_rx_handlers() */
Zhu Yib481de92007-09-25 17:54:57 -07004003 if (priv->rx_handlers[pkt->hdr.cmd]) {
4004 IWL_DEBUG(IWL_DL_HOST_COMMAND | IWL_DL_RX | IWL_DL_ISR,
4005 "r = %d, i = %d, %s, 0x%02x\n", r, i,
4006 get_cmd_string(pkt->hdr.cmd), pkt->hdr.cmd);
4007 priv->rx_handlers[pkt->hdr.cmd] (priv, rxb);
4008 } else {
4009 /* No handling needed */
4010 IWL_DEBUG(IWL_DL_HOST_COMMAND | IWL_DL_RX | IWL_DL_ISR,
4011 "r %d i %d No handler needed for %s, 0x%02x\n",
4012 r, i, get_cmd_string(pkt->hdr.cmd),
4013 pkt->hdr.cmd);
4014 }
4015
4016 if (reclaim) {
Ben Cahill9fbab512007-11-29 11:09:47 +08004017 /* Invoke any callbacks, transfer the skb to caller, and
Tomas Winkler857485c2008-03-21 13:53:44 -07004018 * fire off the (possibly) blocking iwl_send_cmd()
Zhu Yib481de92007-09-25 17:54:57 -07004019 * as we reclaim the driver command queue */
4020 if (rxb && rxb->skb)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004021 iwl4965_tx_cmd_complete(priv, rxb);
Zhu Yib481de92007-09-25 17:54:57 -07004022 else
4023 IWL_WARNING("Claim null rxb?\n");
4024 }
4025
4026 /* For now we just don't re-use anything. We can tweak this
4027 * later to try and re-use notification packets and SKBs that
4028 * fail to Rx correctly */
4029 if (rxb->skb != NULL) {
4030 priv->alloc_rxb_skb--;
4031 dev_kfree_skb_any(rxb->skb);
4032 rxb->skb = NULL;
4033 }
4034
4035 pci_unmap_single(priv->pci_dev, rxb->dma_addr,
Ron Rindjunsky9ee1ba42007-11-26 16:14:42 +02004036 priv->hw_setting.rx_buf_size,
4037 PCI_DMA_FROMDEVICE);
Zhu Yib481de92007-09-25 17:54:57 -07004038 spin_lock_irqsave(&rxq->lock, flags);
4039 list_add_tail(&rxb->list, &priv->rxq.rx_used);
4040 spin_unlock_irqrestore(&rxq->lock, flags);
4041 i = (i + 1) & RX_QUEUE_MASK;
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08004042 /* If there are a lot of unused frames,
4043 * restock the Rx queue so ucode wont assert. */
4044 if (fill_rx) {
4045 count++;
4046 if (count >= 8) {
4047 priv->rxq.read = i;
4048 __iwl4965_rx_replenish(priv);
4049 count = 0;
4050 }
4051 }
Zhu Yib481de92007-09-25 17:54:57 -07004052 }
4053
4054 /* Backtrack one entry */
4055 priv->rxq.read = i;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004056 iwl4965_rx_queue_restock(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004057}
4058
Cahill, Ben M6440adb2007-11-29 11:09:55 +08004059/**
4060 * iwl4965_tx_queue_update_write_ptr - Send new write index to hardware
4061 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004062static int iwl4965_tx_queue_update_write_ptr(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004063 struct iwl4965_tx_queue *txq)
Zhu Yib481de92007-09-25 17:54:57 -07004064{
4065 u32 reg = 0;
4066 int rc = 0;
4067 int txq_id = txq->q.id;
4068
4069 if (txq->need_update == 0)
4070 return rc;
4071
4072 /* if we're trying to save power */
4073 if (test_bit(STATUS_POWER_PMI, &priv->status)) {
4074 /* wake up nic if it's powered down ...
4075 * uCode will wake up, and interrupt us again, so next
4076 * time we'll skip this part. */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004077 reg = iwl_read32(priv, CSR_UCODE_DRV_GP1);
Zhu Yib481de92007-09-25 17:54:57 -07004078
4079 if (reg & CSR_UCODE_DRV_GP1_BIT_MAC_SLEEP) {
4080 IWL_DEBUG_INFO("Requesting wakeup, GP1 = 0x%x\n", reg);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004081 iwl_set_bit(priv, CSR_GP_CNTRL,
Zhu Yib481de92007-09-25 17:54:57 -07004082 CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
4083 return rc;
4084 }
4085
4086 /* restore this queue's parameters in nic hardware. */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004087 rc = iwl_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004088 if (rc)
4089 return rc;
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004090 iwl_write_direct32(priv, HBUS_TARG_WRPTR,
Tomas Winklerfc4b6852007-10-25 17:15:24 +08004091 txq->q.write_ptr | (txq_id << 8));
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004092 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004093
4094 /* else not in power-save mode, uCode will never sleep when we're
4095 * trying to tx (during RFKILL, we're not trying to tx). */
4096 } else
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004097 iwl_write32(priv, HBUS_TARG_WRPTR,
Tomas Winklerfc4b6852007-10-25 17:15:24 +08004098 txq->q.write_ptr | (txq_id << 8));
Zhu Yib481de92007-09-25 17:54:57 -07004099
4100 txq->need_update = 0;
4101
4102 return rc;
4103}
4104
Tomas Winkler0a6857e2008-03-12 16:58:49 -07004105#ifdef CONFIG_IWLWIFI_DEBUG
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004106static void iwl4965_print_rx_config_cmd(struct iwl4965_rxon_cmd *rxon)
Zhu Yib481de92007-09-25 17:54:57 -07004107{
Joe Perches0795af52007-10-03 17:59:30 -07004108 DECLARE_MAC_BUF(mac);
4109
Zhu Yib481de92007-09-25 17:54:57 -07004110 IWL_DEBUG_RADIO("RX CONFIG:\n");
Tomas Winkler0a6857e2008-03-12 16:58:49 -07004111 iwl_print_hex_dump(IWL_DL_RADIO, (u8 *) rxon, sizeof(*rxon));
Zhu Yib481de92007-09-25 17:54:57 -07004112 IWL_DEBUG_RADIO("u16 channel: 0x%x\n", le16_to_cpu(rxon->channel));
4113 IWL_DEBUG_RADIO("u32 flags: 0x%08X\n", le32_to_cpu(rxon->flags));
4114 IWL_DEBUG_RADIO("u32 filter_flags: 0x%08x\n",
4115 le32_to_cpu(rxon->filter_flags));
4116 IWL_DEBUG_RADIO("u8 dev_type: 0x%x\n", rxon->dev_type);
4117 IWL_DEBUG_RADIO("u8 ofdm_basic_rates: 0x%02x\n",
4118 rxon->ofdm_basic_rates);
4119 IWL_DEBUG_RADIO("u8 cck_basic_rates: 0x%02x\n", rxon->cck_basic_rates);
Joe Perches0795af52007-10-03 17:59:30 -07004120 IWL_DEBUG_RADIO("u8[6] node_addr: %s\n",
4121 print_mac(mac, rxon->node_addr));
4122 IWL_DEBUG_RADIO("u8[6] bssid_addr: %s\n",
4123 print_mac(mac, rxon->bssid_addr));
Zhu Yib481de92007-09-25 17:54:57 -07004124 IWL_DEBUG_RADIO("u16 assoc_id: 0x%x\n", le16_to_cpu(rxon->assoc_id));
4125}
4126#endif
4127
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004128static void iwl4965_enable_interrupts(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004129{
4130 IWL_DEBUG_ISR("Enabling interrupts\n");
4131 set_bit(STATUS_INT_ENABLED, &priv->status);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004132 iwl_write32(priv, CSR_INT_MASK, CSR_INI_SET_MASK);
Zhu Yib481de92007-09-25 17:54:57 -07004133}
4134
Mohamed Abbas0359fac2008-03-28 16:21:08 -07004135/* call this function to flush any scheduled tasklet */
4136static inline void iwl_synchronize_irq(struct iwl_priv *priv)
4137{
4138 /* wait to make sure we flush pedding tasklet*/
4139 synchronize_irq(priv->pci_dev->irq);
4140 tasklet_kill(&priv->irq_tasklet);
4141}
4142
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004143static inline void iwl4965_disable_interrupts(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004144{
4145 clear_bit(STATUS_INT_ENABLED, &priv->status);
4146
4147 /* disable interrupts from uCode/NIC to host */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004148 iwl_write32(priv, CSR_INT_MASK, 0x00000000);
Zhu Yib481de92007-09-25 17:54:57 -07004149
4150 /* acknowledge/clear/reset any interrupts still pending
4151 * from uCode or flow handler (Rx/Tx DMA) */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004152 iwl_write32(priv, CSR_INT, 0xffffffff);
4153 iwl_write32(priv, CSR_FH_INT_STATUS, 0xffffffff);
Zhu Yib481de92007-09-25 17:54:57 -07004154 IWL_DEBUG_ISR("Disabled interrupts\n");
4155}
4156
4157static const char *desc_lookup(int i)
4158{
4159 switch (i) {
4160 case 1:
4161 return "FAIL";
4162 case 2:
4163 return "BAD_PARAM";
4164 case 3:
4165 return "BAD_CHECKSUM";
4166 case 4:
4167 return "NMI_INTERRUPT";
4168 case 5:
4169 return "SYSASSERT";
4170 case 6:
4171 return "FATAL_ERROR";
4172 }
4173
4174 return "UNKNOWN";
4175}
4176
4177#define ERROR_START_OFFSET (1 * sizeof(u32))
4178#define ERROR_ELEM_SIZE (7 * sizeof(u32))
4179
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004180static void iwl4965_dump_nic_error_log(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004181{
4182 u32 data2, line;
4183 u32 desc, time, count, base, data1;
4184 u32 blink1, blink2, ilink1, ilink2;
4185 int rc;
4186
4187 base = le32_to_cpu(priv->card_alive.error_event_table_ptr);
4188
Tomas Winkler57aab752008-04-14 21:16:03 -07004189 if (!priv->cfg->ops->lib->is_valid_rtc_data_addr(base)) {
Zhu Yib481de92007-09-25 17:54:57 -07004190 IWL_ERROR("Not valid error log pointer 0x%08X\n", base);
4191 return;
4192 }
4193
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004194 rc = iwl_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004195 if (rc) {
4196 IWL_WARNING("Can not read from adapter at this time.\n");
4197 return;
4198 }
4199
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004200 count = iwl_read_targ_mem(priv, base);
Zhu Yib481de92007-09-25 17:54:57 -07004201
4202 if (ERROR_START_OFFSET <= count * ERROR_ELEM_SIZE) {
4203 IWL_ERROR("Start IWL Error Log Dump:\n");
Tomas Winkler2acae162008-03-02 01:25:59 +02004204 IWL_ERROR("Status: 0x%08lX, count: %d\n", priv->status, count);
Zhu Yib481de92007-09-25 17:54:57 -07004205 }
4206
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004207 desc = iwl_read_targ_mem(priv, base + 1 * sizeof(u32));
4208 blink1 = iwl_read_targ_mem(priv, base + 3 * sizeof(u32));
4209 blink2 = iwl_read_targ_mem(priv, base + 4 * sizeof(u32));
4210 ilink1 = iwl_read_targ_mem(priv, base + 5 * sizeof(u32));
4211 ilink2 = iwl_read_targ_mem(priv, base + 6 * sizeof(u32));
4212 data1 = iwl_read_targ_mem(priv, base + 7 * sizeof(u32));
4213 data2 = iwl_read_targ_mem(priv, base + 8 * sizeof(u32));
4214 line = iwl_read_targ_mem(priv, base + 9 * sizeof(u32));
4215 time = iwl_read_targ_mem(priv, base + 11 * sizeof(u32));
Zhu Yib481de92007-09-25 17:54:57 -07004216
4217 IWL_ERROR("Desc Time "
4218 "data1 data2 line\n");
4219 IWL_ERROR("%-13s (#%d) %010u 0x%08X 0x%08X %u\n",
4220 desc_lookup(desc), desc, time, data1, data2, line);
4221 IWL_ERROR("blink1 blink2 ilink1 ilink2\n");
4222 IWL_ERROR("0x%05X 0x%05X 0x%05X 0x%05X\n", blink1, blink2,
4223 ilink1, ilink2);
4224
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004225 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004226}
4227
4228#define EVENT_START_OFFSET (4 * sizeof(u32))
4229
4230/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004231 * iwl4965_print_event_log - Dump error event log to syslog
Zhu Yib481de92007-09-25 17:54:57 -07004232 *
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004233 * NOTE: Must be called with iwl_grab_nic_access() already obtained!
Zhu Yib481de92007-09-25 17:54:57 -07004234 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004235static void iwl4965_print_event_log(struct iwl_priv *priv, u32 start_idx,
Zhu Yib481de92007-09-25 17:54:57 -07004236 u32 num_events, u32 mode)
4237{
4238 u32 i;
4239 u32 base; /* SRAM byte address of event log header */
4240 u32 event_size; /* 2 u32s, or 3 u32s if timestamp recorded */
4241 u32 ptr; /* SRAM byte address of log data */
4242 u32 ev, time, data; /* event log data */
4243
4244 if (num_events == 0)
4245 return;
4246
4247 base = le32_to_cpu(priv->card_alive.log_event_table_ptr);
4248
4249 if (mode == 0)
4250 event_size = 2 * sizeof(u32);
4251 else
4252 event_size = 3 * sizeof(u32);
4253
4254 ptr = base + EVENT_START_OFFSET + (start_idx * event_size);
4255
4256 /* "time" is actually "data" for mode 0 (no timestamp).
4257 * place event id # at far right for easier visual parsing. */
4258 for (i = 0; i < num_events; i++) {
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004259 ev = iwl_read_targ_mem(priv, ptr);
Zhu Yib481de92007-09-25 17:54:57 -07004260 ptr += sizeof(u32);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004261 time = iwl_read_targ_mem(priv, ptr);
Zhu Yib481de92007-09-25 17:54:57 -07004262 ptr += sizeof(u32);
4263 if (mode == 0)
4264 IWL_ERROR("0x%08x\t%04u\n", time, ev); /* data, ev */
4265 else {
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004266 data = iwl_read_targ_mem(priv, ptr);
Zhu Yib481de92007-09-25 17:54:57 -07004267 ptr += sizeof(u32);
4268 IWL_ERROR("%010u\t0x%08x\t%04u\n", time, data, ev);
4269 }
4270 }
4271}
4272
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004273static void iwl4965_dump_nic_event_log(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004274{
4275 int rc;
4276 u32 base; /* SRAM byte address of event log header */
4277 u32 capacity; /* event log capacity in # entries */
4278 u32 mode; /* 0 - no timestamp, 1 - timestamp recorded */
4279 u32 num_wraps; /* # times uCode wrapped to top of log */
4280 u32 next_entry; /* index of next entry to be written by uCode */
4281 u32 size; /* # entries that we'll print */
4282
4283 base = le32_to_cpu(priv->card_alive.log_event_table_ptr);
Tomas Winkler57aab752008-04-14 21:16:03 -07004284 if (!priv->cfg->ops->lib->is_valid_rtc_data_addr(base)) {
Zhu Yib481de92007-09-25 17:54:57 -07004285 IWL_ERROR("Invalid event log pointer 0x%08X\n", base);
4286 return;
4287 }
4288
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004289 rc = iwl_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004290 if (rc) {
4291 IWL_WARNING("Can not read from adapter at this time.\n");
4292 return;
4293 }
4294
4295 /* event log header */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004296 capacity = iwl_read_targ_mem(priv, base);
4297 mode = iwl_read_targ_mem(priv, base + (1 * sizeof(u32)));
4298 num_wraps = iwl_read_targ_mem(priv, base + (2 * sizeof(u32)));
4299 next_entry = iwl_read_targ_mem(priv, base + (3 * sizeof(u32)));
Zhu Yib481de92007-09-25 17:54:57 -07004300
4301 size = num_wraps ? capacity : next_entry;
4302
4303 /* bail out if nothing in log */
4304 if (size == 0) {
Zhu Yi583fab32007-09-27 11:27:30 +08004305 IWL_ERROR("Start IWL Event Log Dump: nothing in log\n");
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004306 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004307 return;
4308 }
4309
Zhu Yi583fab32007-09-27 11:27:30 +08004310 IWL_ERROR("Start IWL Event Log Dump: display count %d, wraps %d\n",
Zhu Yib481de92007-09-25 17:54:57 -07004311 size, num_wraps);
4312
4313 /* if uCode has wrapped back to top of log, start at the oldest entry,
4314 * i.e the next one that uCode would fill. */
4315 if (num_wraps)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004316 iwl4965_print_event_log(priv, next_entry,
Zhu Yib481de92007-09-25 17:54:57 -07004317 capacity - next_entry, mode);
4318
4319 /* (then/else) start at top of log */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004320 iwl4965_print_event_log(priv, 0, next_entry, mode);
Zhu Yib481de92007-09-25 17:54:57 -07004321
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004322 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004323}
4324
4325/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004326 * iwl4965_irq_handle_error - called for HW or SW error interrupt from card
Zhu Yib481de92007-09-25 17:54:57 -07004327 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004328static void iwl4965_irq_handle_error(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004329{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004330 /* Set the FW error flag -- cleared on iwl4965_down */
Zhu Yib481de92007-09-25 17:54:57 -07004331 set_bit(STATUS_FW_ERROR, &priv->status);
4332
4333 /* Cancel currently queued command. */
4334 clear_bit(STATUS_HCMD_ACTIVE, &priv->status);
4335
Tomas Winkler0a6857e2008-03-12 16:58:49 -07004336#ifdef CONFIG_IWLWIFI_DEBUG
4337 if (iwl_debug_level & IWL_DL_FW_ERRORS) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004338 iwl4965_dump_nic_error_log(priv);
4339 iwl4965_dump_nic_event_log(priv);
4340 iwl4965_print_rx_config_cmd(&priv->staging_rxon);
Zhu Yib481de92007-09-25 17:54:57 -07004341 }
4342#endif
4343
4344 wake_up_interruptible(&priv->wait_command_queue);
4345
4346 /* Keep the restart process from trying to send host
4347 * commands by clearing the INIT status bit */
4348 clear_bit(STATUS_READY, &priv->status);
4349
4350 if (!test_bit(STATUS_EXIT_PENDING, &priv->status)) {
4351 IWL_DEBUG(IWL_DL_INFO | IWL_DL_FW_ERRORS,
4352 "Restarting adapter due to uCode error.\n");
4353
Tomas Winkler3109ece2008-03-28 16:33:35 -07004354 if (iwl_is_associated(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07004355 memcpy(&priv->recovery_rxon, &priv->active_rxon,
4356 sizeof(priv->recovery_rxon));
4357 priv->error_recovering = 1;
4358 }
4359 queue_work(priv->workqueue, &priv->restart);
4360 }
4361}
4362
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004363static void iwl4965_error_recovery(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004364{
4365 unsigned long flags;
4366
4367 memcpy(&priv->staging_rxon, &priv->recovery_rxon,
4368 sizeof(priv->staging_rxon));
4369 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004370 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004371
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004372 iwl4965_rxon_add_station(priv, priv->bssid, 1);
Zhu Yib481de92007-09-25 17:54:57 -07004373
4374 spin_lock_irqsave(&priv->lock, flags);
4375 priv->assoc_id = le16_to_cpu(priv->staging_rxon.assoc_id);
4376 priv->error_recovering = 0;
4377 spin_unlock_irqrestore(&priv->lock, flags);
4378}
4379
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004380static void iwl4965_irq_tasklet(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004381{
4382 u32 inta, handled = 0;
4383 u32 inta_fh;
4384 unsigned long flags;
Tomas Winkler0a6857e2008-03-12 16:58:49 -07004385#ifdef CONFIG_IWLWIFI_DEBUG
Zhu Yib481de92007-09-25 17:54:57 -07004386 u32 inta_mask;
4387#endif
4388
4389 spin_lock_irqsave(&priv->lock, flags);
4390
4391 /* Ack/clear/reset pending uCode interrupts.
4392 * Note: Some bits in CSR_INT are "OR" of bits in CSR_FH_INT_STATUS,
4393 * and will clear only when CSR_FH_INT_STATUS gets cleared. */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004394 inta = iwl_read32(priv, CSR_INT);
4395 iwl_write32(priv, CSR_INT, inta);
Zhu Yib481de92007-09-25 17:54:57 -07004396
4397 /* Ack/clear/reset pending flow-handler (DMA) interrupts.
4398 * Any new interrupts that happen after this, either while we're
4399 * in this tasklet, or later, will show up in next ISR/tasklet. */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004400 inta_fh = iwl_read32(priv, CSR_FH_INT_STATUS);
4401 iwl_write32(priv, CSR_FH_INT_STATUS, inta_fh);
Zhu Yib481de92007-09-25 17:54:57 -07004402
Tomas Winkler0a6857e2008-03-12 16:58:49 -07004403#ifdef CONFIG_IWLWIFI_DEBUG
4404 if (iwl_debug_level & IWL_DL_ISR) {
Ben Cahill9fbab512007-11-29 11:09:47 +08004405 /* just for debug */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004406 inta_mask = iwl_read32(priv, CSR_INT_MASK);
Zhu Yib481de92007-09-25 17:54:57 -07004407 IWL_DEBUG_ISR("inta 0x%08x, enabled 0x%08x, fh 0x%08x\n",
4408 inta, inta_mask, inta_fh);
4409 }
4410#endif
4411
4412 /* Since CSR_INT and CSR_FH_INT_STATUS reads and clears are not
4413 * atomic, make sure that inta covers all the interrupts that
4414 * we've discovered, even if FH interrupt came in just after
4415 * reading CSR_INT. */
Tomas Winkler6f83eaa2008-03-04 18:09:28 -08004416 if (inta_fh & CSR49_FH_INT_RX_MASK)
Zhu Yib481de92007-09-25 17:54:57 -07004417 inta |= CSR_INT_BIT_FH_RX;
Tomas Winkler6f83eaa2008-03-04 18:09:28 -08004418 if (inta_fh & CSR49_FH_INT_TX_MASK)
Zhu Yib481de92007-09-25 17:54:57 -07004419 inta |= CSR_INT_BIT_FH_TX;
4420
4421 /* Now service all interrupt bits discovered above. */
4422 if (inta & CSR_INT_BIT_HW_ERR) {
4423 IWL_ERROR("Microcode HW error detected. Restarting.\n");
4424
4425 /* Tell the device to stop sending interrupts */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004426 iwl4965_disable_interrupts(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004427
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004428 iwl4965_irq_handle_error(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004429
4430 handled |= CSR_INT_BIT_HW_ERR;
4431
4432 spin_unlock_irqrestore(&priv->lock, flags);
4433
4434 return;
4435 }
4436
Tomas Winkler0a6857e2008-03-12 16:58:49 -07004437#ifdef CONFIG_IWLWIFI_DEBUG
4438 if (iwl_debug_level & (IWL_DL_ISR)) {
Zhu Yib481de92007-09-25 17:54:57 -07004439 /* NIC fires this, but we don't use it, redundant with WAKEUP */
Joonwoo Park25c03d82008-01-23 10:15:20 -08004440 if (inta & CSR_INT_BIT_SCD)
4441 IWL_DEBUG_ISR("Scheduler finished to transmit "
4442 "the frame/frames.\n");
Zhu Yib481de92007-09-25 17:54:57 -07004443
4444 /* Alive notification via Rx interrupt will do the real work */
4445 if (inta & CSR_INT_BIT_ALIVE)
4446 IWL_DEBUG_ISR("Alive interrupt\n");
4447 }
4448#endif
4449 /* Safely ignore these bits for debug checks below */
Joonwoo Park25c03d82008-01-23 10:15:20 -08004450 inta &= ~(CSR_INT_BIT_SCD | CSR_INT_BIT_ALIVE);
Zhu Yib481de92007-09-25 17:54:57 -07004451
Ben Cahill9fbab512007-11-29 11:09:47 +08004452 /* HW RF KILL switch toggled */
Zhu Yib481de92007-09-25 17:54:57 -07004453 if (inta & CSR_INT_BIT_RF_KILL) {
4454 int hw_rf_kill = 0;
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004455 if (!(iwl_read32(priv, CSR_GP_CNTRL) &
Zhu Yib481de92007-09-25 17:54:57 -07004456 CSR_GP_CNTRL_REG_FLAG_HW_RF_KILL_SW))
4457 hw_rf_kill = 1;
4458
4459 IWL_DEBUG(IWL_DL_INFO | IWL_DL_RF_KILL | IWL_DL_ISR,
4460 "RF_KILL bit toggled to %s.\n",
4461 hw_rf_kill ? "disable radio":"enable radio");
4462
4463 /* Queue restart only if RF_KILL switch was set to "kill"
4464 * when we loaded driver, and is now set to "enable".
4465 * After we're Alive, RF_KILL gets handled by
Reinette Chatre32304552008-02-15 14:34:37 -08004466 * iwl4965_rx_card_state_notif() */
Zhu Yi53e49092007-12-06 16:08:44 +08004467 if (!hw_rf_kill && !test_bit(STATUS_ALIVE, &priv->status)) {
4468 clear_bit(STATUS_RF_KILL_HW, &priv->status);
Zhu Yib481de92007-09-25 17:54:57 -07004469 queue_work(priv->workqueue, &priv->restart);
Zhu Yi53e49092007-12-06 16:08:44 +08004470 }
Zhu Yib481de92007-09-25 17:54:57 -07004471
4472 handled |= CSR_INT_BIT_RF_KILL;
4473 }
4474
Ben Cahill9fbab512007-11-29 11:09:47 +08004475 /* Chip got too hot and stopped itself */
Zhu Yib481de92007-09-25 17:54:57 -07004476 if (inta & CSR_INT_BIT_CT_KILL) {
4477 IWL_ERROR("Microcode CT kill error detected.\n");
4478 handled |= CSR_INT_BIT_CT_KILL;
4479 }
4480
4481 /* Error detected by uCode */
4482 if (inta & CSR_INT_BIT_SW_ERR) {
4483 IWL_ERROR("Microcode SW error detected. Restarting 0x%X.\n",
4484 inta);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004485 iwl4965_irq_handle_error(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004486 handled |= CSR_INT_BIT_SW_ERR;
4487 }
4488
4489 /* uCode wakes up after power-down sleep */
4490 if (inta & CSR_INT_BIT_WAKEUP) {
4491 IWL_DEBUG_ISR("Wakeup interrupt\n");
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004492 iwl4965_rx_queue_update_write_ptr(priv, &priv->rxq);
4493 iwl4965_tx_queue_update_write_ptr(priv, &priv->txq[0]);
4494 iwl4965_tx_queue_update_write_ptr(priv, &priv->txq[1]);
4495 iwl4965_tx_queue_update_write_ptr(priv, &priv->txq[2]);
4496 iwl4965_tx_queue_update_write_ptr(priv, &priv->txq[3]);
4497 iwl4965_tx_queue_update_write_ptr(priv, &priv->txq[4]);
4498 iwl4965_tx_queue_update_write_ptr(priv, &priv->txq[5]);
Zhu Yib481de92007-09-25 17:54:57 -07004499
4500 handled |= CSR_INT_BIT_WAKEUP;
4501 }
4502
4503 /* All uCode command responses, including Tx command responses,
4504 * Rx "responses" (frame-received notification), and other
4505 * notifications from uCode come through here*/
4506 if (inta & (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX)) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004507 iwl4965_rx_handle(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004508 handled |= (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX);
4509 }
4510
4511 if (inta & CSR_INT_BIT_FH_TX) {
4512 IWL_DEBUG_ISR("Tx interrupt\n");
4513 handled |= CSR_INT_BIT_FH_TX;
4514 }
4515
4516 if (inta & ~handled)
4517 IWL_ERROR("Unhandled INTA bits 0x%08x\n", inta & ~handled);
4518
4519 if (inta & ~CSR_INI_SET_MASK) {
4520 IWL_WARNING("Disabled INTA bits 0x%08x were pending\n",
4521 inta & ~CSR_INI_SET_MASK);
4522 IWL_WARNING(" with FH_INT = 0x%08x\n", inta_fh);
4523 }
4524
4525 /* Re-enable all interrupts */
Mohamed Abbas0359fac2008-03-28 16:21:08 -07004526 /* only Re-enable if diabled by irq */
4527 if (test_bit(STATUS_INT_ENABLED, &priv->status))
4528 iwl4965_enable_interrupts(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004529
Tomas Winkler0a6857e2008-03-12 16:58:49 -07004530#ifdef CONFIG_IWLWIFI_DEBUG
4531 if (iwl_debug_level & (IWL_DL_ISR)) {
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004532 inta = iwl_read32(priv, CSR_INT);
4533 inta_mask = iwl_read32(priv, CSR_INT_MASK);
4534 inta_fh = iwl_read32(priv, CSR_FH_INT_STATUS);
Zhu Yib481de92007-09-25 17:54:57 -07004535 IWL_DEBUG_ISR("End inta 0x%08x, enabled 0x%08x, fh 0x%08x, "
4536 "flags 0x%08lx\n", inta, inta_mask, inta_fh, flags);
4537 }
4538#endif
4539 spin_unlock_irqrestore(&priv->lock, flags);
4540}
4541
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004542static irqreturn_t iwl4965_isr(int irq, void *data)
Zhu Yib481de92007-09-25 17:54:57 -07004543{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004544 struct iwl_priv *priv = data;
Zhu Yib481de92007-09-25 17:54:57 -07004545 u32 inta, inta_mask;
4546 u32 inta_fh;
4547 if (!priv)
4548 return IRQ_NONE;
4549
4550 spin_lock(&priv->lock);
4551
4552 /* Disable (but don't clear!) interrupts here to avoid
4553 * back-to-back ISRs and sporadic interrupts from our NIC.
4554 * If we have something to service, the tasklet will re-enable ints.
4555 * If we *don't* have something, we'll re-enable before leaving here. */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004556 inta_mask = iwl_read32(priv, CSR_INT_MASK); /* just for debug */
4557 iwl_write32(priv, CSR_INT_MASK, 0x00000000);
Zhu Yib481de92007-09-25 17:54:57 -07004558
4559 /* Discover which interrupts are active/pending */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004560 inta = iwl_read32(priv, CSR_INT);
4561 inta_fh = iwl_read32(priv, CSR_FH_INT_STATUS);
Zhu Yib481de92007-09-25 17:54:57 -07004562
4563 /* Ignore interrupt if there's nothing in NIC to service.
4564 * This may be due to IRQ shared with another device,
4565 * or due to sporadic interrupts thrown from our NIC. */
4566 if (!inta && !inta_fh) {
4567 IWL_DEBUG_ISR("Ignore interrupt, inta == 0, inta_fh == 0\n");
4568 goto none;
4569 }
4570
4571 if ((inta == 0xFFFFFFFF) || ((inta & 0xFFFFFFF0) == 0xa5a5a5a0)) {
Oliver Neukum66fbb542007-11-15 09:31:10 +08004572 /* Hardware disappeared. It might have already raised
4573 * an interrupt */
Zhu Yib481de92007-09-25 17:54:57 -07004574 IWL_WARNING("HARDWARE GONE?? INTA == 0x%080x\n", inta);
Oliver Neukum66fbb542007-11-15 09:31:10 +08004575 goto unplugged;
Zhu Yib481de92007-09-25 17:54:57 -07004576 }
4577
4578 IWL_DEBUG_ISR("ISR inta 0x%08x, enabled 0x%08x, fh 0x%08x\n",
4579 inta, inta_mask, inta_fh);
4580
Joonwoo Park25c03d82008-01-23 10:15:20 -08004581 inta &= ~CSR_INT_BIT_SCD;
4582
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004583 /* iwl4965_irq_tasklet() will service interrupts and re-enable them */
Joonwoo Park25c03d82008-01-23 10:15:20 -08004584 if (likely(inta || inta_fh))
4585 tasklet_schedule(&priv->irq_tasklet);
Zhu Yib481de92007-09-25 17:54:57 -07004586
Oliver Neukum66fbb542007-11-15 09:31:10 +08004587 unplugged:
4588 spin_unlock(&priv->lock);
Zhu Yib481de92007-09-25 17:54:57 -07004589 return IRQ_HANDLED;
4590
4591 none:
4592 /* re-enable interrupts here since we don't have anything to service. */
Mohamed Abbas0359fac2008-03-28 16:21:08 -07004593 /* only Re-enable if diabled by irq */
4594 if (test_bit(STATUS_INT_ENABLED, &priv->status))
4595 iwl4965_enable_interrupts(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004596 spin_unlock(&priv->lock);
4597 return IRQ_NONE;
4598}
4599
Zhu Yib481de92007-09-25 17:54:57 -07004600/* For active scan, listen ACTIVE_DWELL_TIME (msec) on each channel after
4601 * sending probe req. This should be set long enough to hear probe responses
4602 * from more than one AP. */
4603#define IWL_ACTIVE_DWELL_TIME_24 (20) /* all times in msec */
4604#define IWL_ACTIVE_DWELL_TIME_52 (10)
4605
4606/* For faster active scanning, scan will move to the next channel if fewer than
4607 * PLCP_QUIET_THRESH packets are heard on this channel within
4608 * ACTIVE_QUIET_TIME after sending probe request. This shortens the dwell
4609 * time if it's a quiet channel (nothing responded to our probe, and there's
4610 * no other traffic).
4611 * Disable "quiet" feature by setting PLCP_QUIET_THRESH to 0. */
4612#define IWL_PLCP_QUIET_THRESH __constant_cpu_to_le16(1) /* packets */
4613#define IWL_ACTIVE_QUIET_TIME __constant_cpu_to_le16(5) /* msec */
4614
4615/* For passive scan, listen PASSIVE_DWELL_TIME (msec) on each channel.
4616 * Must be set longer than active dwell time.
4617 * For the most reliable scan, set > AP beacon interval (typically 100msec). */
4618#define IWL_PASSIVE_DWELL_TIME_24 (20) /* all times in msec */
4619#define IWL_PASSIVE_DWELL_TIME_52 (10)
4620#define IWL_PASSIVE_DWELL_BASE (100)
4621#define IWL_CHANNEL_TUNE_TIME 5
4622
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004623static inline u16 iwl4965_get_active_dwell_time(struct iwl_priv *priv,
Johannes Berg8318d782008-01-24 19:38:38 +01004624 enum ieee80211_band band)
Zhu Yib481de92007-09-25 17:54:57 -07004625{
Johannes Berg8318d782008-01-24 19:38:38 +01004626 if (band == IEEE80211_BAND_5GHZ)
Zhu Yib481de92007-09-25 17:54:57 -07004627 return IWL_ACTIVE_DWELL_TIME_52;
4628 else
4629 return IWL_ACTIVE_DWELL_TIME_24;
4630}
4631
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004632static u16 iwl4965_get_passive_dwell_time(struct iwl_priv *priv,
Johannes Berg8318d782008-01-24 19:38:38 +01004633 enum ieee80211_band band)
Zhu Yib481de92007-09-25 17:54:57 -07004634{
Johannes Berg8318d782008-01-24 19:38:38 +01004635 u16 active = iwl4965_get_active_dwell_time(priv, band);
4636 u16 passive = (band != IEEE80211_BAND_5GHZ) ?
Zhu Yib481de92007-09-25 17:54:57 -07004637 IWL_PASSIVE_DWELL_BASE + IWL_PASSIVE_DWELL_TIME_24 :
4638 IWL_PASSIVE_DWELL_BASE + IWL_PASSIVE_DWELL_TIME_52;
4639
Tomas Winkler3109ece2008-03-28 16:33:35 -07004640 if (iwl_is_associated(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07004641 /* If we're associated, we clamp the maximum passive
4642 * dwell time to be 98% of the beacon interval (minus
4643 * 2 * channel tune time) */
4644 passive = priv->beacon_int;
4645 if ((passive > IWL_PASSIVE_DWELL_BASE) || !passive)
4646 passive = IWL_PASSIVE_DWELL_BASE;
4647 passive = (passive * 98) / 100 - IWL_CHANNEL_TUNE_TIME * 2;
4648 }
4649
4650 if (passive <= active)
4651 passive = active + 1;
4652
4653 return passive;
4654}
4655
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004656static int iwl4965_get_channels_for_scan(struct iwl_priv *priv,
Johannes Berg8318d782008-01-24 19:38:38 +01004657 enum ieee80211_band band,
Zhu Yib481de92007-09-25 17:54:57 -07004658 u8 is_active, u8 direct_mask,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004659 struct iwl4965_scan_channel *scan_ch)
Zhu Yib481de92007-09-25 17:54:57 -07004660{
4661 const struct ieee80211_channel *channels = NULL;
Johannes Berg8318d782008-01-24 19:38:38 +01004662 const struct ieee80211_supported_band *sband;
Assaf Kraussbf85ea42008-03-14 10:38:49 -07004663 const struct iwl_channel_info *ch_info;
Zhu Yib481de92007-09-25 17:54:57 -07004664 u16 passive_dwell = 0;
4665 u16 active_dwell = 0;
4666 int added, i;
4667
Johannes Berg8318d782008-01-24 19:38:38 +01004668 sband = iwl4965_get_hw_mode(priv, band);
4669 if (!sband)
Zhu Yib481de92007-09-25 17:54:57 -07004670 return 0;
4671
Johannes Berg8318d782008-01-24 19:38:38 +01004672 channels = sband->channels;
Zhu Yib481de92007-09-25 17:54:57 -07004673
Johannes Berg8318d782008-01-24 19:38:38 +01004674 active_dwell = iwl4965_get_active_dwell_time(priv, band);
4675 passive_dwell = iwl4965_get_passive_dwell_time(priv, band);
Zhu Yib481de92007-09-25 17:54:57 -07004676
Johannes Berg8318d782008-01-24 19:38:38 +01004677 for (i = 0, added = 0; i < sband->n_channels; i++) {
Johannes Berg182e2e62008-04-04 10:41:56 +02004678 if (channels[i].flags & IEEE80211_CHAN_DISABLED)
4679 continue;
4680
Johannes Berg8318d782008-01-24 19:38:38 +01004681 if (ieee80211_frequency_to_channel(channels[i].center_freq) ==
Zhu Yib481de92007-09-25 17:54:57 -07004682 le16_to_cpu(priv->active_rxon.channel)) {
Tomas Winkler3109ece2008-03-28 16:33:35 -07004683 if (iwl_is_associated(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07004684 IWL_DEBUG_SCAN
4685 ("Skipping current channel %d\n",
4686 le16_to_cpu(priv->active_rxon.channel));
4687 continue;
4688 }
4689 } else if (priv->only_active_channel)
4690 continue;
4691
Johannes Berg8318d782008-01-24 19:38:38 +01004692 scan_ch->channel = ieee80211_frequency_to_channel(channels[i].center_freq);
Zhu Yib481de92007-09-25 17:54:57 -07004693
Assaf Krauss8622e702008-03-21 13:53:43 -07004694 ch_info = iwl_get_channel_info(priv, band,
Ben Cahill9fbab512007-11-29 11:09:47 +08004695 scan_ch->channel);
Zhu Yib481de92007-09-25 17:54:57 -07004696 if (!is_channel_valid(ch_info)) {
4697 IWL_DEBUG_SCAN("Channel %d is INVALID for this SKU.\n",
4698 scan_ch->channel);
4699 continue;
4700 }
4701
4702 if (!is_active || is_channel_passive(ch_info) ||
Johannes Berg8318d782008-01-24 19:38:38 +01004703 (channels[i].flags & IEEE80211_CHAN_PASSIVE_SCAN))
Zhu Yib481de92007-09-25 17:54:57 -07004704 scan_ch->type = 0; /* passive */
4705 else
4706 scan_ch->type = 1; /* active */
4707
4708 if (scan_ch->type & 1)
4709 scan_ch->type |= (direct_mask << 1);
4710
4711 if (is_channel_narrow(ch_info))
4712 scan_ch->type |= (1 << 7);
4713
4714 scan_ch->active_dwell = cpu_to_le16(active_dwell);
4715 scan_ch->passive_dwell = cpu_to_le16(passive_dwell);
4716
Ben Cahill9fbab512007-11-29 11:09:47 +08004717 /* Set txpower levels to defaults */
Zhu Yib481de92007-09-25 17:54:57 -07004718 scan_ch->tpc.dsp_atten = 110;
4719 /* scan_pwr_info->tpc.dsp_atten; */
4720
4721 /*scan_pwr_info->tpc.tx_gain; */
Johannes Berg8318d782008-01-24 19:38:38 +01004722 if (band == IEEE80211_BAND_5GHZ)
Zhu Yib481de92007-09-25 17:54:57 -07004723 scan_ch->tpc.tx_gain = ((1 << 5) | (3 << 3)) | 3;
4724 else {
4725 scan_ch->tpc.tx_gain = ((1 << 5) | (5 << 3));
4726 /* NOTE: if we were doing 6Mb OFDM for scans we'd use
Ben Cahill9fbab512007-11-29 11:09:47 +08004727 * power level:
Reinette Chatre8a1b0242008-01-14 17:46:25 -08004728 * scan_ch->tpc.tx_gain = ((1 << 5) | (2 << 3)) | 3;
Zhu Yib481de92007-09-25 17:54:57 -07004729 */
4730 }
4731
4732 IWL_DEBUG_SCAN("Scanning %d [%s %d]\n",
4733 scan_ch->channel,
4734 (scan_ch->type & 1) ? "ACTIVE" : "PASSIVE",
4735 (scan_ch->type & 1) ?
4736 active_dwell : passive_dwell);
4737
4738 scan_ch++;
4739 added++;
4740 }
4741
4742 IWL_DEBUG_SCAN("total channels to scan %d \n", added);
4743 return added;
4744}
4745
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004746static void iwl4965_init_hw_rates(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07004747 struct ieee80211_rate *rates)
4748{
4749 int i;
4750
4751 for (i = 0; i < IWL_RATE_COUNT; i++) {
Johannes Berg8318d782008-01-24 19:38:38 +01004752 rates[i].bitrate = iwl4965_rates[i].ieee * 5;
4753 rates[i].hw_value = i; /* Rate scaling will work on indexes */
4754 rates[i].hw_value_short = i;
4755 rates[i].flags = 0;
4756 if ((i > IWL_LAST_OFDM_RATE) || (i < IWL_FIRST_OFDM_RATE)) {
Zhu Yib481de92007-09-25 17:54:57 -07004757 /*
Johannes Berg8318d782008-01-24 19:38:38 +01004758 * If CCK != 1M then set short preamble rate flag.
Zhu Yib481de92007-09-25 17:54:57 -07004759 */
Tomas Winkler35cdeaf2008-03-11 16:17:20 -07004760 rates[i].flags |=
4761 (iwl4965_rates[i].plcp == IWL_RATE_1M_PLCP) ?
4762 0 : IEEE80211_RATE_SHORT_PREAMBLE;
Zhu Yib481de92007-09-25 17:54:57 -07004763 }
Zhu Yib481de92007-09-25 17:54:57 -07004764 }
Zhu Yib481de92007-09-25 17:54:57 -07004765}
4766
4767/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004768 * iwl4965_init_geos - Initialize mac80211's geo/channel info based from eeprom
Zhu Yib481de92007-09-25 17:54:57 -07004769 */
Assaf Kraussbf85ea42008-03-14 10:38:49 -07004770int iwl4965_init_geos(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004771{
Assaf Kraussbf85ea42008-03-14 10:38:49 -07004772 struct iwl_channel_info *ch;
Tomas Winkler8211ef72008-03-02 01:36:04 +02004773 struct ieee80211_supported_band *sband;
Zhu Yib481de92007-09-25 17:54:57 -07004774 struct ieee80211_channel *channels;
4775 struct ieee80211_channel *geo_ch;
4776 struct ieee80211_rate *rates;
4777 int i = 0;
Zhu Yib481de92007-09-25 17:54:57 -07004778
Johannes Berg8318d782008-01-24 19:38:38 +01004779 if (priv->bands[IEEE80211_BAND_2GHZ].n_bitrates ||
4780 priv->bands[IEEE80211_BAND_5GHZ].n_bitrates) {
Zhu Yib481de92007-09-25 17:54:57 -07004781 IWL_DEBUG_INFO("Geography modes already initialized.\n");
4782 set_bit(STATUS_GEO_CONFIGURED, &priv->status);
4783 return 0;
4784 }
4785
Zhu Yib481de92007-09-25 17:54:57 -07004786 channels = kzalloc(sizeof(struct ieee80211_channel) *
4787 priv->channel_count, GFP_KERNEL);
Johannes Berg8318d782008-01-24 19:38:38 +01004788 if (!channels)
Zhu Yib481de92007-09-25 17:54:57 -07004789 return -ENOMEM;
Zhu Yib481de92007-09-25 17:54:57 -07004790
Tomas Winkler8211ef72008-03-02 01:36:04 +02004791 rates = kzalloc((sizeof(struct ieee80211_rate) * (IWL_RATE_COUNT + 1)),
Zhu Yib481de92007-09-25 17:54:57 -07004792 GFP_KERNEL);
4793 if (!rates) {
Zhu Yib481de92007-09-25 17:54:57 -07004794 kfree(channels);
4795 return -ENOMEM;
4796 }
4797
Zhu Yib481de92007-09-25 17:54:57 -07004798 /* 5.2GHz channels start after the 2.4GHz channels */
Tomas Winkler8211ef72008-03-02 01:36:04 +02004799 sband = &priv->bands[IEEE80211_BAND_5GHZ];
Assaf Kraussbf85ea42008-03-14 10:38:49 -07004800 sband->channels = &channels[ARRAY_SIZE(iwl_eeprom_band_1)];
Tomas Winkler8211ef72008-03-02 01:36:04 +02004801 /* just OFDM */
4802 sband->bitrates = &rates[IWL_FIRST_OFDM_RATE];
4803 sband->n_bitrates = IWL_RATE_COUNT - IWL_FIRST_OFDM_RATE;
Johannes Berg8318d782008-01-24 19:38:38 +01004804
Assaf Krauss1ea87392008-03-18 14:57:50 -07004805 iwl4965_init_ht_hw_capab(priv, &sband->ht_info, IEEE80211_BAND_5GHZ);
Tomas Winkler78330fd2008-02-06 02:37:18 +02004806
Tomas Winkler8211ef72008-03-02 01:36:04 +02004807 sband = &priv->bands[IEEE80211_BAND_2GHZ];
4808 sband->channels = channels;
4809 /* OFDM & CCK */
4810 sband->bitrates = rates;
4811 sband->n_bitrates = IWL_RATE_COUNT;
Zhu Yib481de92007-09-25 17:54:57 -07004812
Assaf Krauss1ea87392008-03-18 14:57:50 -07004813 iwl4965_init_ht_hw_capab(priv, &sband->ht_info, IEEE80211_BAND_2GHZ);
Tomas Winkler78330fd2008-02-06 02:37:18 +02004814
Zhu Yib481de92007-09-25 17:54:57 -07004815 priv->ieee_channels = channels;
4816 priv->ieee_rates = rates;
4817
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004818 iwl4965_init_hw_rates(priv, rates);
Zhu Yib481de92007-09-25 17:54:57 -07004819
Tomas Winkler8211ef72008-03-02 01:36:04 +02004820 for (i = 0; i < priv->channel_count; i++) {
Zhu Yib481de92007-09-25 17:54:57 -07004821 ch = &priv->channel_info[i];
4822
Tomas Winkler8211ef72008-03-02 01:36:04 +02004823 /* FIXME: might be removed if scan is OK */
4824 if (!is_channel_valid(ch))
Zhu Yib481de92007-09-25 17:54:57 -07004825 continue;
Zhu Yib481de92007-09-25 17:54:57 -07004826
Tomas Winkler8211ef72008-03-02 01:36:04 +02004827 if (is_channel_a_band(ch))
4828 sband = &priv->bands[IEEE80211_BAND_5GHZ];
4829 else
4830 sband = &priv->bands[IEEE80211_BAND_2GHZ];
Zhu Yib481de92007-09-25 17:54:57 -07004831
Tomas Winkler8211ef72008-03-02 01:36:04 +02004832 geo_ch = &sband->channels[sband->n_channels++];
4833
4834 geo_ch->center_freq = ieee80211_channel_to_frequency(ch->channel);
Johannes Berg8318d782008-01-24 19:38:38 +01004835 geo_ch->max_power = ch->max_power_avg;
4836 geo_ch->max_antenna_gain = 0xff;
Mohamed Abbas7b723042008-01-31 21:46:40 -08004837 geo_ch->hw_value = ch->channel;
Zhu Yib481de92007-09-25 17:54:57 -07004838
4839 if (is_channel_valid(ch)) {
Johannes Berg8318d782008-01-24 19:38:38 +01004840 if (!(ch->flags & EEPROM_CHANNEL_IBSS))
4841 geo_ch->flags |= IEEE80211_CHAN_NO_IBSS;
Zhu Yib481de92007-09-25 17:54:57 -07004842
Johannes Berg8318d782008-01-24 19:38:38 +01004843 if (!(ch->flags & EEPROM_CHANNEL_ACTIVE))
4844 geo_ch->flags |= IEEE80211_CHAN_PASSIVE_SCAN;
Zhu Yib481de92007-09-25 17:54:57 -07004845
4846 if (ch->flags & EEPROM_CHANNEL_RADAR)
Johannes Berg8318d782008-01-24 19:38:38 +01004847 geo_ch->flags |= IEEE80211_CHAN_RADAR;
Zhu Yib481de92007-09-25 17:54:57 -07004848
4849 if (ch->max_power_avg > priv->max_channel_txpower_limit)
4850 priv->max_channel_txpower_limit =
4851 ch->max_power_avg;
Tomas Winkler8211ef72008-03-02 01:36:04 +02004852 } else {
Johannes Berg8318d782008-01-24 19:38:38 +01004853 geo_ch->flags |= IEEE80211_CHAN_DISABLED;
Tomas Winkler8211ef72008-03-02 01:36:04 +02004854 }
4855
4856 /* Save flags for reg domain usage */
4857 geo_ch->orig_flags = geo_ch->flags;
4858
4859 IWL_DEBUG_INFO("Channel %d Freq=%d[%sGHz] %s flag=0%X\n",
4860 ch->channel, geo_ch->center_freq,
4861 is_channel_a_band(ch) ? "5.2" : "2.4",
4862 geo_ch->flags & IEEE80211_CHAN_DISABLED ?
4863 "restricted" : "valid",
4864 geo_ch->flags);
Zhu Yib481de92007-09-25 17:54:57 -07004865 }
4866
Tomas Winkler82b9a122008-03-04 18:09:30 -08004867 if ((priv->bands[IEEE80211_BAND_5GHZ].n_channels == 0) &&
4868 priv->cfg->sku & IWL_SKU_A) {
Zhu Yib481de92007-09-25 17:54:57 -07004869 printk(KERN_INFO DRV_NAME
4870 ": Incorrectly detected BG card as ABG. Please send "
4871 "your PCI ID 0x%04X:0x%04X to maintainer.\n",
4872 priv->pci_dev->device, priv->pci_dev->subsystem_device);
Tomas Winkler82b9a122008-03-04 18:09:30 -08004873 priv->cfg->sku &= ~IWL_SKU_A;
Zhu Yib481de92007-09-25 17:54:57 -07004874 }
4875
4876 printk(KERN_INFO DRV_NAME
4877 ": Tunable channels: %d 802.11bg, %d 802.11a channels\n",
Johannes Berg8318d782008-01-24 19:38:38 +01004878 priv->bands[IEEE80211_BAND_2GHZ].n_channels,
4879 priv->bands[IEEE80211_BAND_5GHZ].n_channels);
Zhu Yib481de92007-09-25 17:54:57 -07004880
John W. Linvillee0e0a672008-03-25 15:58:40 -04004881 if (priv->bands[IEEE80211_BAND_2GHZ].n_channels)
4882 priv->hw->wiphy->bands[IEEE80211_BAND_2GHZ] =
4883 &priv->bands[IEEE80211_BAND_2GHZ];
4884 if (priv->bands[IEEE80211_BAND_5GHZ].n_channels)
4885 priv->hw->wiphy->bands[IEEE80211_BAND_5GHZ] =
4886 &priv->bands[IEEE80211_BAND_5GHZ];
Zhu Yib481de92007-09-25 17:54:57 -07004887
Zhu Yib481de92007-09-25 17:54:57 -07004888 set_bit(STATUS_GEO_CONFIGURED, &priv->status);
4889
4890 return 0;
4891}
4892
Reinette Chatre849e0dc2008-01-23 10:15:18 -08004893/*
4894 * iwl4965_free_geos - undo allocations in iwl4965_init_geos
4895 */
Assaf Kraussbf85ea42008-03-14 10:38:49 -07004896void iwl4965_free_geos(struct iwl_priv *priv)
Reinette Chatre849e0dc2008-01-23 10:15:18 -08004897{
Reinette Chatre849e0dc2008-01-23 10:15:18 -08004898 kfree(priv->ieee_channels);
4899 kfree(priv->ieee_rates);
4900 clear_bit(STATUS_GEO_CONFIGURED, &priv->status);
4901}
4902
Zhu Yib481de92007-09-25 17:54:57 -07004903/******************************************************************************
4904 *
4905 * uCode download functions
4906 *
4907 ******************************************************************************/
4908
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004909static void iwl4965_dealloc_ucode_pci(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004910{
Tomas Winkler98c92212008-01-14 17:46:20 -08004911 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_code);
4912 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_data);
4913 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_data_backup);
4914 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_init);
4915 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_init_data);
4916 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_boot);
Zhu Yib481de92007-09-25 17:54:57 -07004917}
4918
4919/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004920 * iwl4965_verify_inst_full - verify runtime uCode image in card vs. host,
Zhu Yib481de92007-09-25 17:54:57 -07004921 * looking at all data.
4922 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004923static int iwl4965_verify_inst_full(struct iwl_priv *priv, __le32 *image,
Ben Cahill9fbab512007-11-29 11:09:47 +08004924 u32 len)
Zhu Yib481de92007-09-25 17:54:57 -07004925{
4926 u32 val;
4927 u32 save_len = len;
4928 int rc = 0;
4929 u32 errcnt;
4930
4931 IWL_DEBUG_INFO("ucode inst image size is %u\n", len);
4932
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004933 rc = iwl_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004934 if (rc)
4935 return rc;
4936
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004937 iwl_write_direct32(priv, HBUS_TARG_MEM_RADDR, RTC_INST_LOWER_BOUND);
Zhu Yib481de92007-09-25 17:54:57 -07004938
4939 errcnt = 0;
4940 for (; len > 0; len -= sizeof(u32), image++) {
4941 /* read data comes through single port, auto-incr addr */
4942 /* NOTE: Use the debugless read so we don't flood kernel log
4943 * if IWL_DL_IO is set */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004944 val = _iwl_read_direct32(priv, HBUS_TARG_MEM_RDAT);
Zhu Yib481de92007-09-25 17:54:57 -07004945 if (val != le32_to_cpu(*image)) {
4946 IWL_ERROR("uCode INST section is invalid at "
4947 "offset 0x%x, is 0x%x, s/b 0x%x\n",
4948 save_len - len, val, le32_to_cpu(*image));
4949 rc = -EIO;
4950 errcnt++;
4951 if (errcnt >= 20)
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 if (!errcnt)
4959 IWL_DEBUG_INFO
4960 ("ucode image in INSTRUCTION memory is good\n");
4961
4962 return rc;
4963}
4964
4965
4966/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004967 * iwl4965_verify_inst_sparse - verify runtime uCode image in card vs. host,
Zhu Yib481de92007-09-25 17:54:57 -07004968 * using sample data 100 bytes apart. If these sample points are good,
4969 * it's a pretty good bet that everything between them is good, too.
4970 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004971static int iwl4965_verify_inst_sparse(struct iwl_priv *priv, __le32 *image, u32 len)
Zhu Yib481de92007-09-25 17:54:57 -07004972{
4973 u32 val;
4974 int rc = 0;
4975 u32 errcnt = 0;
4976 u32 i;
4977
4978 IWL_DEBUG_INFO("ucode inst image size is %u\n", len);
4979
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004980 rc = iwl_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004981 if (rc)
4982 return rc;
4983
4984 for (i = 0; i < len; i += 100, image += 100/sizeof(u32)) {
4985 /* read data comes through single port, auto-incr addr */
4986 /* NOTE: Use the debugless read so we don't flood kernel log
4987 * if IWL_DL_IO is set */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004988 iwl_write_direct32(priv, HBUS_TARG_MEM_RADDR,
Zhu Yib481de92007-09-25 17:54:57 -07004989 i + RTC_INST_LOWER_BOUND);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004990 val = _iwl_read_direct32(priv, HBUS_TARG_MEM_RDAT);
Zhu Yib481de92007-09-25 17:54:57 -07004991 if (val != le32_to_cpu(*image)) {
4992#if 0 /* Enable this if you want to see details */
4993 IWL_ERROR("uCode INST section is invalid at "
4994 "offset 0x%x, is 0x%x, s/b 0x%x\n",
4995 i, val, *image);
4996#endif
4997 rc = -EIO;
4998 errcnt++;
4999 if (errcnt >= 3)
5000 break;
5001 }
5002 }
5003
Tomas Winkler3395f6e2008-03-25 16:33:37 -07005004 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005005
5006 return rc;
5007}
5008
5009
5010/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005011 * iwl4965_verify_ucode - determine which instruction image is in SRAM,
Zhu Yib481de92007-09-25 17:54:57 -07005012 * and verify its contents
5013 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005014static int iwl4965_verify_ucode(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005015{
5016 __le32 *image;
5017 u32 len;
5018 int rc = 0;
5019
5020 /* Try bootstrap */
5021 image = (__le32 *)priv->ucode_boot.v_addr;
5022 len = priv->ucode_boot.len;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005023 rc = iwl4965_verify_inst_sparse(priv, image, len);
Zhu Yib481de92007-09-25 17:54:57 -07005024 if (rc == 0) {
5025 IWL_DEBUG_INFO("Bootstrap uCode is good in inst SRAM\n");
5026 return 0;
5027 }
5028
5029 /* Try initialize */
5030 image = (__le32 *)priv->ucode_init.v_addr;
5031 len = priv->ucode_init.len;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005032 rc = iwl4965_verify_inst_sparse(priv, image, len);
Zhu Yib481de92007-09-25 17:54:57 -07005033 if (rc == 0) {
5034 IWL_DEBUG_INFO("Initialize uCode is good in inst SRAM\n");
5035 return 0;
5036 }
5037
5038 /* Try runtime/protocol */
5039 image = (__le32 *)priv->ucode_code.v_addr;
5040 len = priv->ucode_code.len;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005041 rc = iwl4965_verify_inst_sparse(priv, image, len);
Zhu Yib481de92007-09-25 17:54:57 -07005042 if (rc == 0) {
5043 IWL_DEBUG_INFO("Runtime uCode is good in inst SRAM\n");
5044 return 0;
5045 }
5046
5047 IWL_ERROR("NO VALID UCODE IMAGE IN INSTRUCTION SRAM!!\n");
5048
Ben Cahill9fbab512007-11-29 11:09:47 +08005049 /* Since nothing seems to match, show first several data entries in
5050 * instruction SRAM, so maybe visual inspection will give a clue.
5051 * Selection of bootstrap image (vs. other images) is arbitrary. */
Zhu Yib481de92007-09-25 17:54:57 -07005052 image = (__le32 *)priv->ucode_boot.v_addr;
5053 len = priv->ucode_boot.len;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005054 rc = iwl4965_verify_inst_full(priv, image, len);
Zhu Yib481de92007-09-25 17:54:57 -07005055
5056 return rc;
5057}
5058
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005059static void iwl4965_nic_start(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005060{
5061 /* Remove all resets to allow NIC to operate */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07005062 iwl_write32(priv, CSR_RESET, 0);
Zhu Yib481de92007-09-25 17:54:57 -07005063}
5064
Tomas Winkler90e759d2007-11-29 11:09:41 +08005065
Zhu Yib481de92007-09-25 17:54:57 -07005066/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005067 * iwl4965_read_ucode - Read uCode images from disk file.
Zhu Yib481de92007-09-25 17:54:57 -07005068 *
5069 * Copy into buffers for card to fetch via bus-mastering
5070 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005071static int iwl4965_read_ucode(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005072{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005073 struct iwl4965_ucode *ucode;
Tomas Winkler90e759d2007-11-29 11:09:41 +08005074 int ret;
Zhu Yib481de92007-09-25 17:54:57 -07005075 const struct firmware *ucode_raw;
Tomas Winkler4bf775c2008-03-04 18:09:31 -08005076 const char *name = priv->cfg->fw_name;
Zhu Yib481de92007-09-25 17:54:57 -07005077 u8 *src;
5078 size_t len;
5079 u32 ver, inst_size, data_size, init_size, init_data_size, boot_size;
5080
5081 /* Ask kernel firmware_class module to get the boot firmware off disk.
5082 * request_firmware() is synchronous, file is in memory on return. */
Tomas Winkler90e759d2007-11-29 11:09:41 +08005083 ret = request_firmware(&ucode_raw, name, &priv->pci_dev->dev);
5084 if (ret < 0) {
5085 IWL_ERROR("%s firmware file req failed: Reason %d\n",
5086 name, ret);
Zhu Yib481de92007-09-25 17:54:57 -07005087 goto error;
5088 }
5089
5090 IWL_DEBUG_INFO("Got firmware '%s' file (%zd bytes) from disk\n",
5091 name, ucode_raw->size);
5092
5093 /* Make sure that we got at least our header! */
5094 if (ucode_raw->size < sizeof(*ucode)) {
5095 IWL_ERROR("File size way too small!\n");
Tomas Winkler90e759d2007-11-29 11:09:41 +08005096 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07005097 goto err_release;
5098 }
5099
5100 /* Data from ucode file: header followed by uCode images */
5101 ucode = (void *)ucode_raw->data;
5102
5103 ver = le32_to_cpu(ucode->ver);
5104 inst_size = le32_to_cpu(ucode->inst_size);
5105 data_size = le32_to_cpu(ucode->data_size);
5106 init_size = le32_to_cpu(ucode->init_size);
5107 init_data_size = le32_to_cpu(ucode->init_data_size);
5108 boot_size = le32_to_cpu(ucode->boot_size);
5109
5110 IWL_DEBUG_INFO("f/w package hdr ucode version = 0x%x\n", ver);
5111 IWL_DEBUG_INFO("f/w package hdr runtime inst size = %u\n",
5112 inst_size);
5113 IWL_DEBUG_INFO("f/w package hdr runtime data size = %u\n",
5114 data_size);
5115 IWL_DEBUG_INFO("f/w package hdr init inst size = %u\n",
5116 init_size);
5117 IWL_DEBUG_INFO("f/w package hdr init data size = %u\n",
5118 init_data_size);
5119 IWL_DEBUG_INFO("f/w package hdr boot inst size = %u\n",
5120 boot_size);
5121
5122 /* Verify size of file vs. image size info in file's header */
5123 if (ucode_raw->size < sizeof(*ucode) +
5124 inst_size + data_size + init_size +
5125 init_data_size + boot_size) {
5126
5127 IWL_DEBUG_INFO("uCode file size %d too small\n",
5128 (int)ucode_raw->size);
Tomas Winkler90e759d2007-11-29 11:09:41 +08005129 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07005130 goto err_release;
5131 }
5132
5133 /* Verify that uCode images will fit in card's SRAM */
5134 if (inst_size > IWL_MAX_INST_SIZE) {
Tomas Winkler90e759d2007-11-29 11:09:41 +08005135 IWL_DEBUG_INFO("uCode instr len %d too large to fit in\n",
5136 inst_size);
5137 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07005138 goto err_release;
5139 }
5140
5141 if (data_size > IWL_MAX_DATA_SIZE) {
Tomas Winkler90e759d2007-11-29 11:09:41 +08005142 IWL_DEBUG_INFO("uCode data len %d too large to fit in\n",
5143 data_size);
5144 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07005145 goto err_release;
5146 }
5147 if (init_size > IWL_MAX_INST_SIZE) {
5148 IWL_DEBUG_INFO
Tomas Winkler90e759d2007-11-29 11:09:41 +08005149 ("uCode init instr len %d too large to fit in\n",
5150 init_size);
5151 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07005152 goto err_release;
5153 }
5154 if (init_data_size > IWL_MAX_DATA_SIZE) {
5155 IWL_DEBUG_INFO
Tomas Winkler90e759d2007-11-29 11:09:41 +08005156 ("uCode init data len %d too large to fit in\n",
5157 init_data_size);
5158 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07005159 goto err_release;
5160 }
5161 if (boot_size > IWL_MAX_BSM_SIZE) {
5162 IWL_DEBUG_INFO
Tomas Winkler90e759d2007-11-29 11:09:41 +08005163 ("uCode boot instr len %d too large to fit in\n",
5164 boot_size);
5165 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07005166 goto err_release;
5167 }
5168
5169 /* Allocate ucode buffers for card's bus-master loading ... */
5170
5171 /* Runtime instructions and 2 copies of data:
5172 * 1) unmodified from disk
5173 * 2) backup cache for save/restore during power-downs */
5174 priv->ucode_code.len = inst_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08005175 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_code);
Zhu Yib481de92007-09-25 17:54:57 -07005176
5177 priv->ucode_data.len = data_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08005178 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_data);
Zhu Yib481de92007-09-25 17:54:57 -07005179
5180 priv->ucode_data_backup.len = data_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08005181 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_data_backup);
Zhu Yib481de92007-09-25 17:54:57 -07005182
5183 /* Initialization instructions and data */
Tomas Winkler90e759d2007-11-29 11:09:41 +08005184 if (init_size && init_data_size) {
5185 priv->ucode_init.len = init_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08005186 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_init);
Zhu Yib481de92007-09-25 17:54:57 -07005187
Tomas Winkler90e759d2007-11-29 11:09:41 +08005188 priv->ucode_init_data.len = init_data_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08005189 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_init_data);
Tomas Winkler90e759d2007-11-29 11:09:41 +08005190
5191 if (!priv->ucode_init.v_addr || !priv->ucode_init_data.v_addr)
5192 goto err_pci_alloc;
5193 }
Zhu Yib481de92007-09-25 17:54:57 -07005194
5195 /* Bootstrap (instructions only, no data) */
Tomas Winkler90e759d2007-11-29 11:09:41 +08005196 if (boot_size) {
5197 priv->ucode_boot.len = boot_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08005198 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_boot);
Zhu Yib481de92007-09-25 17:54:57 -07005199
Tomas Winkler90e759d2007-11-29 11:09:41 +08005200 if (!priv->ucode_boot.v_addr)
5201 goto err_pci_alloc;
5202 }
Zhu Yib481de92007-09-25 17:54:57 -07005203
5204 /* Copy images into buffers for card's bus-master reads ... */
5205
5206 /* Runtime instructions (first block of data in file) */
5207 src = &ucode->data[0];
5208 len = priv->ucode_code.len;
Tomas Winkler90e759d2007-11-29 11:09:41 +08005209 IWL_DEBUG_INFO("Copying (but not loading) uCode instr len %Zd\n", len);
Zhu Yib481de92007-09-25 17:54:57 -07005210 memcpy(priv->ucode_code.v_addr, src, len);
5211 IWL_DEBUG_INFO("uCode instr buf vaddr = 0x%p, paddr = 0x%08x\n",
5212 priv->ucode_code.v_addr, (u32)priv->ucode_code.p_addr);
5213
5214 /* Runtime data (2nd block)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005215 * NOTE: Copy into backup buffer will be done in iwl4965_up() */
Zhu Yib481de92007-09-25 17:54:57 -07005216 src = &ucode->data[inst_size];
5217 len = priv->ucode_data.len;
Tomas Winkler90e759d2007-11-29 11:09:41 +08005218 IWL_DEBUG_INFO("Copying (but not loading) uCode data len %Zd\n", len);
Zhu Yib481de92007-09-25 17:54:57 -07005219 memcpy(priv->ucode_data.v_addr, src, len);
5220 memcpy(priv->ucode_data_backup.v_addr, src, len);
5221
5222 /* Initialization instructions (3rd block) */
5223 if (init_size) {
5224 src = &ucode->data[inst_size + data_size];
5225 len = priv->ucode_init.len;
Tomas Winkler90e759d2007-11-29 11:09:41 +08005226 IWL_DEBUG_INFO("Copying (but not loading) init instr len %Zd\n",
5227 len);
Zhu Yib481de92007-09-25 17:54:57 -07005228 memcpy(priv->ucode_init.v_addr, src, len);
5229 }
5230
5231 /* Initialization data (4th block) */
5232 if (init_data_size) {
5233 src = &ucode->data[inst_size + data_size + init_size];
5234 len = priv->ucode_init_data.len;
Tomas Winkler90e759d2007-11-29 11:09:41 +08005235 IWL_DEBUG_INFO("Copying (but not loading) init data len %Zd\n",
5236 len);
Zhu Yib481de92007-09-25 17:54:57 -07005237 memcpy(priv->ucode_init_data.v_addr, src, len);
5238 }
5239
5240 /* Bootstrap instructions (5th block) */
5241 src = &ucode->data[inst_size + data_size + init_size + init_data_size];
5242 len = priv->ucode_boot.len;
Tomas Winkler90e759d2007-11-29 11:09:41 +08005243 IWL_DEBUG_INFO("Copying (but not loading) boot instr len %Zd\n", len);
Zhu Yib481de92007-09-25 17:54:57 -07005244 memcpy(priv->ucode_boot.v_addr, src, len);
5245
5246 /* We have our copies now, allow OS release its copies */
5247 release_firmware(ucode_raw);
5248 return 0;
5249
5250 err_pci_alloc:
5251 IWL_ERROR("failed to allocate pci memory\n");
Tomas Winkler90e759d2007-11-29 11:09:41 +08005252 ret = -ENOMEM;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005253 iwl4965_dealloc_ucode_pci(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005254
5255 err_release:
5256 release_firmware(ucode_raw);
5257
5258 error:
Tomas Winkler90e759d2007-11-29 11:09:41 +08005259 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07005260}
5261
5262
5263/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005264 * iwl4965_set_ucode_ptrs - Set uCode address location
Zhu Yib481de92007-09-25 17:54:57 -07005265 *
5266 * Tell initialization uCode where to find runtime uCode.
5267 *
5268 * BSM registers initially contain pointers to initialization uCode.
5269 * We need to replace them to load runtime uCode inst and data,
5270 * and to save runtime data when powering down.
5271 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005272static int iwl4965_set_ucode_ptrs(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005273{
5274 dma_addr_t pinst;
5275 dma_addr_t pdata;
5276 int rc = 0;
5277 unsigned long flags;
5278
5279 /* bits 35:4 for 4965 */
5280 pinst = priv->ucode_code.p_addr >> 4;
5281 pdata = priv->ucode_data_backup.p_addr >> 4;
5282
5283 spin_lock_irqsave(&priv->lock, flags);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07005284 rc = iwl_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005285 if (rc) {
5286 spin_unlock_irqrestore(&priv->lock, flags);
5287 return rc;
5288 }
5289
5290 /* Tell bootstrap uCode where to find image to load */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07005291 iwl_write_prph(priv, BSM_DRAM_INST_PTR_REG, pinst);
5292 iwl_write_prph(priv, BSM_DRAM_DATA_PTR_REG, pdata);
5293 iwl_write_prph(priv, BSM_DRAM_DATA_BYTECOUNT_REG,
Zhu Yib481de92007-09-25 17:54:57 -07005294 priv->ucode_data.len);
5295
5296 /* Inst bytecount must be last to set up, bit 31 signals uCode
5297 * that all new ptr/size info is in place */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07005298 iwl_write_prph(priv, BSM_DRAM_INST_BYTECOUNT_REG,
Zhu Yib481de92007-09-25 17:54:57 -07005299 priv->ucode_code.len | BSM_DRAM_INST_LOAD);
5300
Tomas Winkler3395f6e2008-03-25 16:33:37 -07005301 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005302
5303 spin_unlock_irqrestore(&priv->lock, flags);
5304
5305 IWL_DEBUG_INFO("Runtime uCode pointers are set.\n");
5306
5307 return rc;
5308}
5309
5310/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005311 * iwl4965_init_alive_start - Called after REPLY_ALIVE notification received
Zhu Yib481de92007-09-25 17:54:57 -07005312 *
5313 * Called after REPLY_ALIVE notification received from "initialize" uCode.
5314 *
5315 * The 4965 "initialize" ALIVE reply contains calibration data for:
5316 * Voltage, temperature, and MIMO tx gain correction, now stored in priv
5317 * (3945 does not contain this data).
5318 *
5319 * Tell "initialize" uCode to go ahead and load the runtime uCode.
5320*/
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005321static void iwl4965_init_alive_start(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005322{
5323 /* Check alive response for "valid" sign from uCode */
5324 if (priv->card_alive_init.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("Initialize Alive failed.\n");
5328 goto restart;
5329 }
5330
5331 /* Bootstrap uCode has loaded initialize uCode ... verify inst image.
5332 * This is a paranoid check, because we would not have gotten the
5333 * "initialize" 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 \"initialize\" uCode load.\n");
5338 goto restart;
5339 }
5340
5341 /* Calculate temperature */
5342 priv->temperature = iwl4965_get_temperature(priv);
5343
5344 /* Send pointers to protocol/runtime uCode image ... init code will
5345 * load and launch runtime uCode, which will send us another "Alive"
5346 * notification. */
5347 IWL_DEBUG_INFO("Initialization Alive received.\n");
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005348 if (iwl4965_set_ucode_ptrs(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07005349 /* Runtime instruction load won't happen;
5350 * take it all the way back down so we can try again */
5351 IWL_DEBUG_INFO("Couldn't set up uCode pointers.\n");
5352 goto restart;
5353 }
5354 return;
5355
5356 restart:
5357 queue_work(priv->workqueue, &priv->restart);
5358}
5359
5360
5361/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005362 * iwl4965_alive_start - called after REPLY_ALIVE notification received
Zhu Yib481de92007-09-25 17:54:57 -07005363 * from protocol/runtime uCode (initialization uCode's
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005364 * Alive gets handled by iwl4965_init_alive_start()).
Zhu Yib481de92007-09-25 17:54:57 -07005365 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005366static void iwl4965_alive_start(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005367{
Tomas Winkler57aab752008-04-14 21:16:03 -07005368 int ret = 0;
Zhu Yib481de92007-09-25 17:54:57 -07005369
5370 IWL_DEBUG_INFO("Runtime Alive received.\n");
5371
5372 if (priv->card_alive.is_valid != UCODE_VALID_OK) {
5373 /* We had an error bringing up the hardware, so take it
5374 * all the way back down so we can try again */
5375 IWL_DEBUG_INFO("Alive failed.\n");
5376 goto restart;
5377 }
5378
5379 /* Initialize uCode has loaded Runtime uCode ... verify inst image.
5380 * This is a paranoid check, because we would not have gotten the
5381 * "runtime" alive if code weren't properly loaded. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005382 if (iwl4965_verify_ucode(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07005383 /* Runtime instruction load was bad;
5384 * take it all the way back down so we can try again */
5385 IWL_DEBUG_INFO("Bad runtime uCode load.\n");
5386 goto restart;
5387 }
5388
Assaf Kraussbf85ea42008-03-14 10:38:49 -07005389 iwlcore_clear_stations_table(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005390
Tomas Winkler57aab752008-04-14 21:16:03 -07005391 ret = priv->cfg->ops->lib->alive_notify(priv);
5392 if (ret) {
Zhu Yib481de92007-09-25 17:54:57 -07005393 IWL_WARNING("Could not complete ALIVE transition [ntf]: %d\n",
Tomas Winkler57aab752008-04-14 21:16:03 -07005394 ret);
Zhu Yib481de92007-09-25 17:54:57 -07005395 goto restart;
5396 }
5397
Ben Cahill9fbab512007-11-29 11:09:47 +08005398 /* After the ALIVE response, we can send host commands to 4965 uCode */
Zhu Yib481de92007-09-25 17:54:57 -07005399 set_bit(STATUS_ALIVE, &priv->status);
5400
5401 /* Clear out the uCode error bit if it is set */
5402 clear_bit(STATUS_FW_ERROR, &priv->status);
5403
Tomas Winklerfee12472008-04-03 16:05:21 -07005404 if (iwl_is_rfkill(priv))
Zhu Yib481de92007-09-25 17:54:57 -07005405 return;
5406
Zhu Yi5a66926a2008-01-14 17:46:18 -08005407 ieee80211_start_queues(priv->hw);
Zhu Yib481de92007-09-25 17:54:57 -07005408
5409 priv->active_rate = priv->rates_mask;
5410 priv->active_rate_basic = priv->rates_mask & IWL_BASIC_RATES_MASK;
5411
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005412 iwl4965_send_power_mode(priv, IWL_POWER_LEVEL(priv->power_mode));
Zhu Yib481de92007-09-25 17:54:57 -07005413
Tomas Winkler3109ece2008-03-28 16:33:35 -07005414 if (iwl_is_associated(priv)) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005415 struct iwl4965_rxon_cmd *active_rxon =
5416 (struct iwl4965_rxon_cmd *)(&priv->active_rxon);
Zhu Yib481de92007-09-25 17:54:57 -07005417
5418 memcpy(&priv->staging_rxon, &priv->active_rxon,
5419 sizeof(priv->staging_rxon));
5420 active_rxon->filter_flags &= ~RXON_FILTER_ASSOC_MSK;
5421 } else {
5422 /* Initialize our rx_config data */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005423 iwl4965_connection_init_rx_config(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005424 memcpy(priv->staging_rxon.node_addr, priv->mac_addr, ETH_ALEN);
5425 }
5426
Ben Cahill9fbab512007-11-29 11:09:47 +08005427 /* Configure Bluetooth device coexistence support */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005428 iwl4965_send_bt_config(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005429
5430 /* Configure the adapter for unassociated operation */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005431 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005432
5433 /* At this point, the NIC is initialized and operational */
5434 priv->notif_missed_beacons = 0;
Zhu Yib481de92007-09-25 17:54:57 -07005435
5436 iwl4965_rf_kill_ct_config(priv);
Zhu Yi5a66926a2008-01-14 17:46:18 -08005437
Reinette Chatrefe00b5a2008-04-03 16:05:23 -07005438 iwl_leds_register(priv);
5439
Zhu Yib481de92007-09-25 17:54:57 -07005440 IWL_DEBUG_INFO("ALIVE processing complete.\n");
Rick Farringtona9f46782008-03-18 14:57:49 -07005441 set_bit(STATUS_READY, &priv->status);
Zhu Yi5a66926a2008-01-14 17:46:18 -08005442 wake_up_interruptible(&priv->wait_command_queue);
Zhu Yib481de92007-09-25 17:54:57 -07005443
5444 if (priv->error_recovering)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005445 iwl4965_error_recovery(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005446
Mohamed Abbasc8381fd2008-03-28 16:21:05 -07005447 iwlcore_low_level_notify(priv, IWLCORE_START_EVT);
Mohamed Abbas84363e62008-04-04 16:59:58 -07005448 ieee80211_notify_mac(priv->hw, IEEE80211_NOTIFY_RE_ASSOC);
Zhu Yib481de92007-09-25 17:54:57 -07005449 return;
5450
5451 restart:
5452 queue_work(priv->workqueue, &priv->restart);
5453}
5454
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005455static void iwl4965_cancel_deferred_work(struct iwl_priv *priv);
Zhu Yib481de92007-09-25 17:54:57 -07005456
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005457static void __iwl4965_down(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005458{
5459 unsigned long flags;
5460 int exit_pending = test_bit(STATUS_EXIT_PENDING, &priv->status);
5461 struct ieee80211_conf *conf = NULL;
5462
5463 IWL_DEBUG_INFO(DRV_NAME " is going down\n");
5464
5465 conf = ieee80211_get_hw_conf(priv->hw);
5466
5467 if (!exit_pending)
5468 set_bit(STATUS_EXIT_PENDING, &priv->status);
5469
Mohamed Abbasab53d8a2008-03-25 16:33:36 -07005470 iwl_leds_unregister(priv);
5471
Mohamed Abbasc8381fd2008-03-28 16:21:05 -07005472 iwlcore_low_level_notify(priv, IWLCORE_STOP_EVT);
5473
Assaf Kraussbf85ea42008-03-14 10:38:49 -07005474 iwlcore_clear_stations_table(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005475
5476 /* Unblock any waiting calls */
5477 wake_up_interruptible_all(&priv->wait_command_queue);
5478
Zhu Yib481de92007-09-25 17:54:57 -07005479 /* Wipe out the EXIT_PENDING status bit if we are not actually
5480 * exiting the module */
5481 if (!exit_pending)
5482 clear_bit(STATUS_EXIT_PENDING, &priv->status);
5483
5484 /* stop and reset the on-board processor */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07005485 iwl_write32(priv, CSR_RESET, CSR_RESET_REG_FLAG_NEVO_RESET);
Zhu Yib481de92007-09-25 17:54:57 -07005486
5487 /* tell the device to stop sending interrupts */
Mohamed Abbas0359fac2008-03-28 16:21:08 -07005488 spin_lock_irqsave(&priv->lock, flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005489 iwl4965_disable_interrupts(priv);
Mohamed Abbas0359fac2008-03-28 16:21:08 -07005490 spin_unlock_irqrestore(&priv->lock, flags);
5491 iwl_synchronize_irq(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005492
5493 if (priv->mac80211_registered)
5494 ieee80211_stop_queues(priv->hw);
5495
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005496 /* If we have not previously called iwl4965_init() then
Zhu Yib481de92007-09-25 17:54:57 -07005497 * clear all bits but the RF Kill and SUSPEND bits and return */
Tomas Winklerfee12472008-04-03 16:05:21 -07005498 if (!iwl_is_init(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07005499 priv->status = test_bit(STATUS_RF_KILL_HW, &priv->status) <<
5500 STATUS_RF_KILL_HW |
5501 test_bit(STATUS_RF_KILL_SW, &priv->status) <<
5502 STATUS_RF_KILL_SW |
Reinette Chatre97888642008-02-06 11:20:38 -08005503 test_bit(STATUS_GEO_CONFIGURED, &priv->status) <<
5504 STATUS_GEO_CONFIGURED |
Zhu Yib481de92007-09-25 17:54:57 -07005505 test_bit(STATUS_IN_SUSPEND, &priv->status) <<
5506 STATUS_IN_SUSPEND;
5507 goto exit;
5508 }
5509
5510 /* ...otherwise clear out all the status bits but the RF Kill and
5511 * SUSPEND bits and continue taking the NIC down. */
5512 priv->status &= test_bit(STATUS_RF_KILL_HW, &priv->status) <<
5513 STATUS_RF_KILL_HW |
5514 test_bit(STATUS_RF_KILL_SW, &priv->status) <<
5515 STATUS_RF_KILL_SW |
Reinette Chatre97888642008-02-06 11:20:38 -08005516 test_bit(STATUS_GEO_CONFIGURED, &priv->status) <<
5517 STATUS_GEO_CONFIGURED |
Zhu Yib481de92007-09-25 17:54:57 -07005518 test_bit(STATUS_IN_SUSPEND, &priv->status) <<
5519 STATUS_IN_SUSPEND |
5520 test_bit(STATUS_FW_ERROR, &priv->status) <<
5521 STATUS_FW_ERROR;
5522
5523 spin_lock_irqsave(&priv->lock, flags);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07005524 iwl_clear_bit(priv, CSR_GP_CNTRL,
Ben Cahill9fbab512007-11-29 11:09:47 +08005525 CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
Zhu Yib481de92007-09-25 17:54:57 -07005526 spin_unlock_irqrestore(&priv->lock, flags);
5527
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005528 iwl4965_hw_txq_ctx_stop(priv);
5529 iwl4965_hw_rxq_stop(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005530
5531 spin_lock_irqsave(&priv->lock, flags);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07005532 if (!iwl_grab_nic_access(priv)) {
5533 iwl_write_prph(priv, APMG_CLK_DIS_REG,
Zhu Yib481de92007-09-25 17:54:57 -07005534 APMG_CLK_VAL_DMA_CLK_RQT);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07005535 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005536 }
5537 spin_unlock_irqrestore(&priv->lock, flags);
5538
5539 udelay(5);
5540
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005541 iwl4965_hw_nic_stop_master(priv);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07005542 iwl_set_bit(priv, CSR_RESET, CSR_RESET_REG_FLAG_SW_RESET);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005543 iwl4965_hw_nic_reset(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005544
5545 exit:
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005546 memset(&priv->card_alive, 0, sizeof(struct iwl4965_alive_resp));
Zhu Yib481de92007-09-25 17:54:57 -07005547
5548 if (priv->ibss_beacon)
5549 dev_kfree_skb(priv->ibss_beacon);
5550 priv->ibss_beacon = NULL;
5551
5552 /* clear out any free frames */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005553 iwl4965_clear_free_frames(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005554}
5555
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005556static void iwl4965_down(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005557{
5558 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005559 __iwl4965_down(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005560 mutex_unlock(&priv->mutex);
Zhu Yib24d22b2007-12-19 13:59:52 +08005561
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005562 iwl4965_cancel_deferred_work(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005563}
5564
5565#define MAX_HW_RESTARTS 5
5566
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005567static int __iwl4965_up(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005568{
Tomas Winkler57aab752008-04-14 21:16:03 -07005569 int i;
5570 int ret;
Zhu Yib481de92007-09-25 17:54:57 -07005571
5572 if (test_bit(STATUS_EXIT_PENDING, &priv->status)) {
5573 IWL_WARNING("Exit pending; will not bring the NIC up\n");
5574 return -EIO;
5575 }
5576
5577 if (test_bit(STATUS_RF_KILL_SW, &priv->status)) {
5578 IWL_WARNING("Radio disabled by SW RF kill (module "
5579 "parameter)\n");
Mohamed Abbasad97edd2008-03-28 16:21:06 -07005580 iwl_rfkill_set_hw_state(priv);
Zhu Yie655b9f2008-01-24 02:19:38 -08005581 return -ENODEV;
5582 }
5583
Reinette Chatree903fbd2008-01-30 22:05:15 -08005584 if (!priv->ucode_data_backup.v_addr || !priv->ucode_data.v_addr) {
5585 IWL_ERROR("ucode not available for device bringup\n");
5586 return -EIO;
5587 }
5588
Zhu Yie655b9f2008-01-24 02:19:38 -08005589 /* If platform's RF_KILL switch is NOT set to KILL */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07005590 if (iwl_read32(priv, CSR_GP_CNTRL) &
Zhu Yie655b9f2008-01-24 02:19:38 -08005591 CSR_GP_CNTRL_REG_FLAG_HW_RF_KILL_SW)
5592 clear_bit(STATUS_RF_KILL_HW, &priv->status);
5593 else {
5594 set_bit(STATUS_RF_KILL_HW, &priv->status);
5595 if (!test_bit(STATUS_IN_SUSPEND, &priv->status)) {
Mohamed Abbasad97edd2008-03-28 16:21:06 -07005596 iwl_rfkill_set_hw_state(priv);
Zhu Yie655b9f2008-01-24 02:19:38 -08005597 IWL_WARNING("Radio disabled by HW RF Kill switch\n");
5598 return -ENODEV;
5599 }
Zhu Yib481de92007-09-25 17:54:57 -07005600 }
5601
Mohamed Abbasad97edd2008-03-28 16:21:06 -07005602 iwl_rfkill_set_hw_state(priv);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07005603 iwl_write32(priv, CSR_INT, 0xFFFFFFFF);
Zhu Yib481de92007-09-25 17:54:57 -07005604
Tomas Winkler57aab752008-04-14 21:16:03 -07005605 ret = priv->cfg->ops->lib->hw_nic_init(priv);
5606 if (ret) {
5607 IWL_ERROR("Unable to init nic\n");
5608 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07005609 }
5610
5611 /* make sure rfkill handshake bits are cleared */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07005612 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
5613 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR,
Zhu Yib481de92007-09-25 17:54:57 -07005614 CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
5615
5616 /* clear (again), then enable host interrupts */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07005617 iwl_write32(priv, CSR_INT, 0xFFFFFFFF);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005618 iwl4965_enable_interrupts(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005619
5620 /* really make sure rfkill handshake bits are cleared */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07005621 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
5622 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
Zhu Yib481de92007-09-25 17:54:57 -07005623
5624 /* Copy original ucode data image from disk into backup cache.
5625 * This will be used to initialize the on-board processor's
5626 * data SRAM for a clean start when the runtime program first loads. */
5627 memcpy(priv->ucode_data_backup.v_addr, priv->ucode_data.v_addr,
Zhu Yi5a66926a2008-01-14 17:46:18 -08005628 priv->ucode_data.len);
Zhu Yib481de92007-09-25 17:54:57 -07005629
Zhu Yie655b9f2008-01-24 02:19:38 -08005630 /* We return success when we resume from suspend and rf_kill is on. */
5631 if (test_bit(STATUS_RF_KILL_HW, &priv->status))
Zhu Yib481de92007-09-25 17:54:57 -07005632 return 0;
Zhu Yib481de92007-09-25 17:54:57 -07005633
5634 for (i = 0; i < MAX_HW_RESTARTS; i++) {
5635
Assaf Kraussbf85ea42008-03-14 10:38:49 -07005636 iwlcore_clear_stations_table(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005637
5638 /* load bootstrap state machine,
5639 * load bootstrap program into processor's memory,
5640 * prepare to load the "initialize" uCode */
Tomas Winkler57aab752008-04-14 21:16:03 -07005641 ret = priv->cfg->ops->lib->load_ucode(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005642
Tomas Winkler57aab752008-04-14 21:16:03 -07005643 if (ret) {
5644 IWL_ERROR("Unable to set up bootstrap uCode: %d\n", ret);
Zhu Yib481de92007-09-25 17:54:57 -07005645 continue;
5646 }
5647
5648 /* start card; "initialize" will load runtime ucode */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005649 iwl4965_nic_start(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005650
Zhu Yib481de92007-09-25 17:54:57 -07005651 IWL_DEBUG_INFO(DRV_NAME " is coming up\n");
5652
5653 return 0;
5654 }
5655
5656 set_bit(STATUS_EXIT_PENDING, &priv->status);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005657 __iwl4965_down(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005658
5659 /* tried to restart and config the device for as long as our
5660 * patience could withstand */
5661 IWL_ERROR("Unable to initialize device after %d attempts.\n", i);
5662 return -EIO;
5663}
5664
5665
5666/*****************************************************************************
5667 *
5668 * Workqueue callbacks
5669 *
5670 *****************************************************************************/
5671
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005672static void iwl4965_bg_init_alive_start(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07005673{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005674 struct iwl_priv *priv =
5675 container_of(data, struct iwl_priv, init_alive_start.work);
Zhu Yib481de92007-09-25 17:54:57 -07005676
5677 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
5678 return;
5679
5680 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005681 iwl4965_init_alive_start(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005682 mutex_unlock(&priv->mutex);
5683}
5684
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005685static void iwl4965_bg_alive_start(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07005686{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005687 struct iwl_priv *priv =
5688 container_of(data, struct iwl_priv, alive_start.work);
Zhu Yib481de92007-09-25 17:54:57 -07005689
5690 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
5691 return;
5692
5693 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005694 iwl4965_alive_start(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005695 mutex_unlock(&priv->mutex);
5696}
5697
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005698static void iwl4965_bg_rf_kill(struct work_struct *work)
Zhu Yib481de92007-09-25 17:54:57 -07005699{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005700 struct iwl_priv *priv = container_of(work, struct iwl_priv, rf_kill);
Zhu Yib481de92007-09-25 17:54:57 -07005701
5702 wake_up_interruptible(&priv->wait_command_queue);
5703
5704 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
5705 return;
5706
5707 mutex_lock(&priv->mutex);
5708
Tomas Winklerfee12472008-04-03 16:05:21 -07005709 if (!iwl_is_rfkill(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07005710 IWL_DEBUG(IWL_DL_INFO | IWL_DL_RF_KILL,
5711 "HW and/or SW RF Kill no longer active, restarting "
5712 "device\n");
5713 if (!test_bit(STATUS_EXIT_PENDING, &priv->status))
5714 queue_work(priv->workqueue, &priv->restart);
5715 } else {
Mohamed Abbasad97edd2008-03-28 16:21:06 -07005716 /* make sure mac80211 stop sending Tx frame */
5717 if (priv->mac80211_registered)
5718 ieee80211_stop_queues(priv->hw);
Zhu Yib481de92007-09-25 17:54:57 -07005719
5720 if (!test_bit(STATUS_RF_KILL_HW, &priv->status))
5721 IWL_DEBUG_RF_KILL("Can not turn radio back on - "
5722 "disabled by SW switch\n");
5723 else
5724 IWL_WARNING("Radio Frequency Kill Switch is On:\n"
5725 "Kill switch must be turned off for "
5726 "wireless networking to work.\n");
5727 }
Mohamed Abbasad97edd2008-03-28 16:21:06 -07005728 iwl_rfkill_set_hw_state(priv);
5729
Zhu Yib481de92007-09-25 17:54:57 -07005730 mutex_unlock(&priv->mutex);
5731}
5732
5733#define IWL_SCAN_CHECK_WATCHDOG (7 * HZ)
5734
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005735static void iwl4965_bg_scan_check(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07005736{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005737 struct iwl_priv *priv =
5738 container_of(data, struct iwl_priv, scan_check.work);
Zhu Yib481de92007-09-25 17:54:57 -07005739
5740 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
5741 return;
5742
5743 mutex_lock(&priv->mutex);
5744 if (test_bit(STATUS_SCANNING, &priv->status) ||
5745 test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
5746 IWL_DEBUG(IWL_DL_INFO | IWL_DL_SCAN,
5747 "Scan completion watchdog resetting adapter (%dms)\n",
5748 jiffies_to_msecs(IWL_SCAN_CHECK_WATCHDOG));
mabbas052c4b92007-10-25 17:15:43 +08005749
Zhu Yib481de92007-09-25 17:54:57 -07005750 if (!test_bit(STATUS_EXIT_PENDING, &priv->status))
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005751 iwl4965_send_scan_abort(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005752 }
5753 mutex_unlock(&priv->mutex);
5754}
5755
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005756static void iwl4965_bg_request_scan(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07005757{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005758 struct iwl_priv *priv =
5759 container_of(data, struct iwl_priv, request_scan);
Tomas Winkler857485c2008-03-21 13:53:44 -07005760 struct iwl_host_cmd cmd = {
Zhu Yib481de92007-09-25 17:54:57 -07005761 .id = REPLY_SCAN_CMD,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005762 .len = sizeof(struct iwl4965_scan_cmd),
Zhu Yib481de92007-09-25 17:54:57 -07005763 .meta.flags = CMD_SIZE_HUGE,
5764 };
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005765 struct iwl4965_scan_cmd *scan;
Zhu Yib481de92007-09-25 17:54:57 -07005766 struct ieee80211_conf *conf = NULL;
Tomas Winkler78330fd2008-02-06 02:37:18 +02005767 u16 cmd_len;
Johannes Berg8318d782008-01-24 19:38:38 +01005768 enum ieee80211_band band;
Tomas Winkler78330fd2008-02-06 02:37:18 +02005769 u8 direct_mask;
Tomas Winkler857485c2008-03-21 13:53:44 -07005770 int ret = 0;
Zhu Yib481de92007-09-25 17:54:57 -07005771
5772 conf = ieee80211_get_hw_conf(priv->hw);
5773
5774 mutex_lock(&priv->mutex);
5775
Tomas Winklerfee12472008-04-03 16:05:21 -07005776 if (!iwl_is_ready(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07005777 IWL_WARNING("request scan called when driver not ready.\n");
5778 goto done;
5779 }
5780
5781 /* Make sure the scan wasn't cancelled before this queued work
5782 * was given the chance to run... */
5783 if (!test_bit(STATUS_SCANNING, &priv->status))
5784 goto done;
5785
5786 /* This should never be called or scheduled if there is currently
5787 * a scan active in the hardware. */
5788 if (test_bit(STATUS_SCAN_HW, &priv->status)) {
5789 IWL_DEBUG_INFO("Multiple concurrent scan requests in parallel. "
5790 "Ignoring second request.\n");
Tomas Winkler857485c2008-03-21 13:53:44 -07005791 ret = -EIO;
Zhu Yib481de92007-09-25 17:54:57 -07005792 goto done;
5793 }
5794
5795 if (test_bit(STATUS_EXIT_PENDING, &priv->status)) {
5796 IWL_DEBUG_SCAN("Aborting scan due to device shutdown\n");
5797 goto done;
5798 }
5799
5800 if (test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
5801 IWL_DEBUG_HC("Scan request while abort pending. Queuing.\n");
5802 goto done;
5803 }
5804
Tomas Winklerfee12472008-04-03 16:05:21 -07005805 if (iwl_is_rfkill(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07005806 IWL_DEBUG_HC("Aborting scan due to RF Kill activation\n");
5807 goto done;
5808 }
5809
5810 if (!test_bit(STATUS_READY, &priv->status)) {
5811 IWL_DEBUG_HC("Scan request while uninitialized. Queuing.\n");
5812 goto done;
5813 }
5814
5815 if (!priv->scan_bands) {
5816 IWL_DEBUG_HC("Aborting scan due to no requested bands\n");
5817 goto done;
5818 }
5819
5820 if (!priv->scan) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005821 priv->scan = kmalloc(sizeof(struct iwl4965_scan_cmd) +
Zhu Yib481de92007-09-25 17:54:57 -07005822 IWL_MAX_SCAN_SIZE, GFP_KERNEL);
5823 if (!priv->scan) {
Tomas Winkler857485c2008-03-21 13:53:44 -07005824 ret = -ENOMEM;
Zhu Yib481de92007-09-25 17:54:57 -07005825 goto done;
5826 }
5827 }
5828 scan = priv->scan;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005829 memset(scan, 0, sizeof(struct iwl4965_scan_cmd) + IWL_MAX_SCAN_SIZE);
Zhu Yib481de92007-09-25 17:54:57 -07005830
5831 scan->quiet_plcp_th = IWL_PLCP_QUIET_THRESH;
5832 scan->quiet_time = IWL_ACTIVE_QUIET_TIME;
5833
Tomas Winkler3109ece2008-03-28 16:33:35 -07005834 if (iwl_is_associated(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07005835 u16 interval = 0;
5836 u32 extra;
5837 u32 suspend_time = 100;
5838 u32 scan_suspend_time = 100;
5839 unsigned long flags;
5840
5841 IWL_DEBUG_INFO("Scanning while associated...\n");
5842
5843 spin_lock_irqsave(&priv->lock, flags);
5844 interval = priv->beacon_int;
5845 spin_unlock_irqrestore(&priv->lock, flags);
5846
5847 scan->suspend_time = 0;
mabbas052c4b92007-10-25 17:15:43 +08005848 scan->max_out_time = cpu_to_le32(200 * 1024);
Zhu Yib481de92007-09-25 17:54:57 -07005849 if (!interval)
5850 interval = suspend_time;
5851
5852 extra = (suspend_time / interval) << 22;
5853 scan_suspend_time = (extra |
5854 ((suspend_time % interval) * 1024));
5855 scan->suspend_time = cpu_to_le32(scan_suspend_time);
5856 IWL_DEBUG_SCAN("suspend_time 0x%X beacon interval %d\n",
5857 scan_suspend_time, interval);
5858 }
5859
5860 /* We should add the ability for user to lock to PASSIVE ONLY */
5861 if (priv->one_direct_scan) {
5862 IWL_DEBUG_SCAN
5863 ("Kicking off one direct scan for '%s'\n",
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005864 iwl4965_escape_essid(priv->direct_ssid,
Zhu Yib481de92007-09-25 17:54:57 -07005865 priv->direct_ssid_len));
5866 scan->direct_scan[0].id = WLAN_EID_SSID;
5867 scan->direct_scan[0].len = priv->direct_ssid_len;
5868 memcpy(scan->direct_scan[0].ssid,
5869 priv->direct_ssid, priv->direct_ssid_len);
5870 direct_mask = 1;
Tomas Winkler3109ece2008-03-28 16:33:35 -07005871 } else if (!iwl_is_associated(priv) && priv->essid_len) {
Zhu Yib481de92007-09-25 17:54:57 -07005872 scan->direct_scan[0].id = WLAN_EID_SSID;
5873 scan->direct_scan[0].len = priv->essid_len;
5874 memcpy(scan->direct_scan[0].ssid, priv->essid, priv->essid_len);
5875 direct_mask = 1;
Tomas Winkler857485c2008-03-21 13:53:44 -07005876 } else {
Zhu Yib481de92007-09-25 17:54:57 -07005877 direct_mask = 0;
Tomas Winkler857485c2008-03-21 13:53:44 -07005878 }
Zhu Yib481de92007-09-25 17:54:57 -07005879
Zhu Yib481de92007-09-25 17:54:57 -07005880 scan->tx_cmd.tx_flags = TX_CMD_FLG_SEQ_CTL_MSK;
5881 scan->tx_cmd.sta_id = priv->hw_setting.bcast_sta_id;
5882 scan->tx_cmd.stop_time.life_time = TX_CMD_LIFE_TIME_INFINITE;
5883
Zhu Yib481de92007-09-25 17:54:57 -07005884
5885 switch (priv->scan_bands) {
5886 case 2:
5887 scan->flags = RXON_FLG_BAND_24G_MSK | RXON_FLG_AUTO_DETECT_MSK;
5888 scan->tx_cmd.rate_n_flags =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005889 iwl4965_hw_set_rate_n_flags(IWL_RATE_1M_PLCP,
Zhu Yib481de92007-09-25 17:54:57 -07005890 RATE_MCS_ANT_B_MSK|RATE_MCS_CCK_MSK);
5891
5892 scan->good_CRC_th = 0;
Johannes Berg8318d782008-01-24 19:38:38 +01005893 band = IEEE80211_BAND_2GHZ;
Zhu Yib481de92007-09-25 17:54:57 -07005894 break;
5895
5896 case 1:
5897 scan->tx_cmd.rate_n_flags =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005898 iwl4965_hw_set_rate_n_flags(IWL_RATE_6M_PLCP,
Zhu Yib481de92007-09-25 17:54:57 -07005899 RATE_MCS_ANT_B_MSK);
5900 scan->good_CRC_th = IWL_GOOD_CRC_TH;
Johannes Berg8318d782008-01-24 19:38:38 +01005901 band = IEEE80211_BAND_5GHZ;
Zhu Yib481de92007-09-25 17:54:57 -07005902 break;
5903
5904 default:
5905 IWL_WARNING("Invalid scan band count\n");
5906 goto done;
5907 }
5908
Tomas Winkler78330fd2008-02-06 02:37:18 +02005909 /* We don't build a direct scan probe request; the uCode will do
5910 * that based on the direct_mask added to each channel entry */
5911 cmd_len = iwl4965_fill_probe_req(priv, band,
5912 (struct ieee80211_mgmt *)scan->data,
5913 IWL_MAX_SCAN_SIZE - sizeof(*scan), 0);
5914
5915 scan->tx_cmd.len = cpu_to_le16(cmd_len);
Zhu Yib481de92007-09-25 17:54:57 -07005916 /* select Rx chains */
5917
5918 /* Force use of chains B and C (0x6) for scan Rx.
5919 * Avoid A (0x1) because of its off-channel reception on A-band.
5920 * MIMO is not used here, but value is required to make uCode happy. */
5921 scan->rx_chain = RXON_RX_CHAIN_DRIVER_FORCE_MSK |
5922 cpu_to_le16((0x7 << RXON_RX_CHAIN_VALID_POS) |
5923 (0x6 << RXON_RX_CHAIN_FORCE_SEL_POS) |
5924 (0x7 << RXON_RX_CHAIN_FORCE_MIMO_SEL_POS));
5925
5926 if (priv->iw_mode == IEEE80211_IF_TYPE_MNTR)
5927 scan->filter_flags = RXON_FILTER_PROMISC_MSK;
5928
Reinette Chatre26c0f032008-03-11 16:17:15 -07005929 if (direct_mask) {
Zhu Yib481de92007-09-25 17:54:57 -07005930 IWL_DEBUG_SCAN
5931 ("Initiating direct scan for %s.\n",
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005932 iwl4965_escape_essid(priv->essid, priv->essid_len));
Reinette Chatre26c0f032008-03-11 16:17:15 -07005933 scan->channel_count =
5934 iwl4965_get_channels_for_scan(
5935 priv, band, 1, /* active */
5936 direct_mask,
5937 (void *)&scan->data[le16_to_cpu(scan->tx_cmd.len)]);
5938 } else {
Zhu Yib481de92007-09-25 17:54:57 -07005939 IWL_DEBUG_SCAN("Initiating indirect scan.\n");
Reinette Chatre26c0f032008-03-11 16:17:15 -07005940 scan->channel_count =
5941 iwl4965_get_channels_for_scan(
5942 priv, band, 0, /* passive */
5943 direct_mask,
5944 (void *)&scan->data[le16_to_cpu(scan->tx_cmd.len)]);
5945 }
Zhu Yib481de92007-09-25 17:54:57 -07005946
5947 cmd.len += le16_to_cpu(scan->tx_cmd.len) +
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005948 scan->channel_count * sizeof(struct iwl4965_scan_channel);
Zhu Yib481de92007-09-25 17:54:57 -07005949 cmd.data = scan;
5950 scan->len = cpu_to_le16(cmd.len);
5951
5952 set_bit(STATUS_SCAN_HW, &priv->status);
Tomas Winkler857485c2008-03-21 13:53:44 -07005953 ret = iwl_send_cmd_sync(priv, &cmd);
5954 if (ret)
Zhu Yib481de92007-09-25 17:54:57 -07005955 goto done;
5956
5957 queue_delayed_work(priv->workqueue, &priv->scan_check,
5958 IWL_SCAN_CHECK_WATCHDOG);
5959
5960 mutex_unlock(&priv->mutex);
5961 return;
5962
5963 done:
Ian Schram01ebd062007-10-25 17:15:22 +08005964 /* inform mac80211 scan aborted */
Zhu Yib481de92007-09-25 17:54:57 -07005965 queue_work(priv->workqueue, &priv->scan_completed);
5966 mutex_unlock(&priv->mutex);
5967}
5968
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005969static void iwl4965_bg_up(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07005970{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005971 struct iwl_priv *priv = container_of(data, struct iwl_priv, up);
Zhu Yib481de92007-09-25 17:54:57 -07005972
5973 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
5974 return;
5975
5976 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005977 __iwl4965_up(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005978 mutex_unlock(&priv->mutex);
5979}
5980
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005981static void iwl4965_bg_restart(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07005982{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005983 struct iwl_priv *priv = container_of(data, struct iwl_priv, restart);
Zhu Yib481de92007-09-25 17:54:57 -07005984
5985 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
5986 return;
5987
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005988 iwl4965_down(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005989 queue_work(priv->workqueue, &priv->up);
5990}
5991
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005992static void iwl4965_bg_rx_replenish(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07005993{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005994 struct iwl_priv *priv =
5995 container_of(data, struct iwl_priv, rx_replenish);
Zhu Yib481de92007-09-25 17:54:57 -07005996
5997 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
5998 return;
5999
6000 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006001 iwl4965_rx_replenish(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006002 mutex_unlock(&priv->mutex);
6003}
6004
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08006005#define IWL_DELAY_NEXT_SCAN (HZ*2)
6006
Reinette Chatre508e32e2008-04-14 21:16:13 -07006007static void iwl4965_post_associate(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07006008{
Zhu Yib481de92007-09-25 17:54:57 -07006009 struct ieee80211_conf *conf = NULL;
Tomas Winkler857485c2008-03-21 13:53:44 -07006010 int ret = 0;
Joe Perches0795af52007-10-03 17:59:30 -07006011 DECLARE_MAC_BUF(mac);
Zhu Yib481de92007-09-25 17:54:57 -07006012
6013 if (priv->iw_mode == IEEE80211_IF_TYPE_AP) {
6014 IWL_ERROR("%s Should not be called in AP mode\n", __FUNCTION__);
6015 return;
6016 }
6017
Joe Perches0795af52007-10-03 17:59:30 -07006018 IWL_DEBUG_ASSOC("Associated as %d to: %s\n",
6019 priv->assoc_id,
6020 print_mac(mac, priv->active_rxon.bssid_addr));
Zhu Yib481de92007-09-25 17:54:57 -07006021
6022
6023 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
6024 return;
6025
Zhu Yib481de92007-09-25 17:54:57 -07006026
Reinette Chatre508e32e2008-04-14 21:16:13 -07006027 if (!priv->vif || !priv->is_open)
Mohamed Abbas948c1712007-10-25 17:15:45 +08006028 return;
Reinette Chatre508e32e2008-04-14 21:16:13 -07006029
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006030 iwl4965_scan_cancel_timeout(priv, 200);
mabbas052c4b92007-10-25 17:15:43 +08006031
Zhu Yib481de92007-09-25 17:54:57 -07006032 conf = ieee80211_get_hw_conf(priv->hw);
6033
6034 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006035 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006036
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006037 memset(&priv->rxon_timing, 0, sizeof(struct iwl4965_rxon_time_cmd));
6038 iwl4965_setup_rxon_timing(priv);
Tomas Winkler857485c2008-03-21 13:53:44 -07006039 ret = iwl_send_cmd_pdu(priv, REPLY_RXON_TIMING,
Zhu Yib481de92007-09-25 17:54:57 -07006040 sizeof(priv->rxon_timing), &priv->rxon_timing);
Tomas Winkler857485c2008-03-21 13:53:44 -07006041 if (ret)
Zhu Yib481de92007-09-25 17:54:57 -07006042 IWL_WARNING("REPLY_RXON_TIMING failed - "
6043 "Attempting to continue.\n");
6044
6045 priv->staging_rxon.filter_flags |= RXON_FILTER_ASSOC_MSK;
6046
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08006047#ifdef CONFIG_IWL4965_HT
Ron Rindjunskyfd105e72007-11-26 16:14:39 +02006048 if (priv->current_ht_config.is_ht)
6049 iwl4965_set_rxon_ht(priv, &priv->current_ht_config);
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08006050#endif /* CONFIG_IWL4965_HT*/
Zhu Yib481de92007-09-25 17:54:57 -07006051 iwl4965_set_rxon_chain(priv);
6052 priv->staging_rxon.assoc_id = cpu_to_le16(priv->assoc_id);
6053
6054 IWL_DEBUG_ASSOC("assoc id %d beacon interval %d\n",
6055 priv->assoc_id, priv->beacon_int);
6056
6057 if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_PREAMBLE)
6058 priv->staging_rxon.flags |= RXON_FLG_SHORT_PREAMBLE_MSK;
6059 else
6060 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_PREAMBLE_MSK;
6061
6062 if (priv->staging_rxon.flags & RXON_FLG_BAND_24G_MSK) {
6063 if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_SLOT_TIME)
6064 priv->staging_rxon.flags |= RXON_FLG_SHORT_SLOT_MSK;
6065 else
6066 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
6067
6068 if (priv->iw_mode == IEEE80211_IF_TYPE_IBSS)
6069 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
6070
6071 }
6072
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006073 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006074
6075 switch (priv->iw_mode) {
6076 case IEEE80211_IF_TYPE_STA:
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006077 iwl4965_rate_scale_init(priv->hw, IWL_AP_ID);
Zhu Yib481de92007-09-25 17:54:57 -07006078 break;
6079
6080 case IEEE80211_IF_TYPE_IBSS:
6081
6082 /* clear out the station table */
Assaf Kraussbf85ea42008-03-14 10:38:49 -07006083 iwlcore_clear_stations_table(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006084
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006085 iwl4965_rxon_add_station(priv, iwl4965_broadcast_addr, 0);
6086 iwl4965_rxon_add_station(priv, priv->bssid, 0);
6087 iwl4965_rate_scale_init(priv->hw, IWL_STA_ID);
6088 iwl4965_send_beacon_cmd(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006089
6090 break;
6091
6092 default:
6093 IWL_ERROR("%s Should not be called in %d mode\n",
6094 __FUNCTION__, priv->iw_mode);
6095 break;
6096 }
6097
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006098 iwl4965_sequence_reset(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006099
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08006100#ifdef CONFIG_IWL4965_SENSITIVITY
Zhu Yib481de92007-09-25 17:54:57 -07006101 /* Enable Rx differential gain and sensitivity calibrations */
6102 iwl4965_chain_noise_reset(priv);
6103 priv->start_calib = 1;
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08006104#endif /* CONFIG_IWL4965_SENSITIVITY */
Zhu Yib481de92007-09-25 17:54:57 -07006105
6106 if (priv->iw_mode == IEEE80211_IF_TYPE_IBSS)
6107 priv->assoc_station_added = 1;
6108
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006109 iwl4965_activate_qos(priv, 0);
Ron Rindjunsky292ae172008-02-06 11:20:39 -08006110
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08006111 /* we have just associated, don't start scan too early */
6112 priv->next_scan_jiffies = jiffies + IWL_DELAY_NEXT_SCAN;
Reinette Chatre508e32e2008-04-14 21:16:13 -07006113}
6114
6115
6116static void iwl4965_bg_post_associate(struct work_struct *data)
6117{
6118 struct iwl_priv *priv = container_of(data, struct iwl_priv,
6119 post_associate.work);
6120
6121 mutex_lock(&priv->mutex);
6122 iwl4965_post_associate(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006123 mutex_unlock(&priv->mutex);
Reinette Chatre508e32e2008-04-14 21:16:13 -07006124
Zhu Yib481de92007-09-25 17:54:57 -07006125}
6126
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006127static void iwl4965_bg_abort_scan(struct work_struct *work)
Zhu Yib481de92007-09-25 17:54:57 -07006128{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006129 struct iwl_priv *priv = container_of(work, struct iwl_priv, abort_scan);
Zhu Yib481de92007-09-25 17:54:57 -07006130
Tomas Winklerfee12472008-04-03 16:05:21 -07006131 if (!iwl_is_ready(priv))
Zhu Yib481de92007-09-25 17:54:57 -07006132 return;
6133
6134 mutex_lock(&priv->mutex);
6135
6136 set_bit(STATUS_SCAN_ABORTING, &priv->status);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006137 iwl4965_send_scan_abort(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006138
6139 mutex_unlock(&priv->mutex);
6140}
6141
Zhu Yi76bb77e2007-11-22 10:53:22 +08006142static int iwl4965_mac_config(struct ieee80211_hw *hw, struct ieee80211_conf *conf);
6143
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006144static void iwl4965_bg_scan_completed(struct work_struct *work)
Zhu Yib481de92007-09-25 17:54:57 -07006145{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006146 struct iwl_priv *priv =
6147 container_of(work, struct iwl_priv, scan_completed);
Zhu Yib481de92007-09-25 17:54:57 -07006148
6149 IWL_DEBUG(IWL_DL_INFO | IWL_DL_SCAN, "SCAN complete scan\n");
6150
6151 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
6152 return;
6153
Zhu Yia0646472007-12-20 14:10:01 +08006154 if (test_bit(STATUS_CONF_PENDING, &priv->status))
6155 iwl4965_mac_config(priv->hw, ieee80211_get_hw_conf(priv->hw));
Zhu Yi76bb77e2007-11-22 10:53:22 +08006156
Zhu Yib481de92007-09-25 17:54:57 -07006157 ieee80211_scan_completed(priv->hw);
6158
6159 /* Since setting the TXPOWER may have been deferred while
6160 * performing the scan, fire one off */
6161 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006162 iwl4965_hw_reg_send_txpower(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006163 mutex_unlock(&priv->mutex);
6164}
6165
6166/*****************************************************************************
6167 *
6168 * mac80211 entry point functions
6169 *
6170 *****************************************************************************/
6171
Zhu Yi5a66926a2008-01-14 17:46:18 -08006172#define UCODE_READY_TIMEOUT (2 * HZ)
6173
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006174static int iwl4965_mac_start(struct ieee80211_hw *hw)
Zhu Yib481de92007-09-25 17:54:57 -07006175{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006176 struct iwl_priv *priv = hw->priv;
Zhu Yi5a66926a2008-01-14 17:46:18 -08006177 int ret;
Zhu Yib481de92007-09-25 17:54:57 -07006178
6179 IWL_DEBUG_MAC80211("enter\n");
6180
Zhu Yi5a66926a2008-01-14 17:46:18 -08006181 if (pci_enable_device(priv->pci_dev)) {
6182 IWL_ERROR("Fail to pci_enable_device\n");
6183 return -ENODEV;
6184 }
6185 pci_restore_state(priv->pci_dev);
6186 pci_enable_msi(priv->pci_dev);
6187
6188 ret = request_irq(priv->pci_dev->irq, iwl4965_isr, IRQF_SHARED,
6189 DRV_NAME, priv);
6190 if (ret) {
6191 IWL_ERROR("Error allocating IRQ %d\n", priv->pci_dev->irq);
6192 goto out_disable_msi;
6193 }
6194
Zhu Yib481de92007-09-25 17:54:57 -07006195 /* we should be verifying the device is ready to be opened */
6196 mutex_lock(&priv->mutex);
6197
Zhu Yi5a66926a2008-01-14 17:46:18 -08006198 memset(&priv->staging_rxon, 0, sizeof(struct iwl4965_rxon_cmd));
6199 /* fetch ucode file from disk, alloc and copy to bus-master buffers ...
6200 * ucode filename and max sizes are card-specific. */
6201
6202 if (!priv->ucode_code.len) {
6203 ret = iwl4965_read_ucode(priv);
6204 if (ret) {
6205 IWL_ERROR("Could not read microcode: %d\n", ret);
6206 mutex_unlock(&priv->mutex);
6207 goto out_release_irq;
6208 }
6209 }
6210
Zhu Yie655b9f2008-01-24 02:19:38 -08006211 ret = __iwl4965_up(priv);
Zhu Yi5a66926a2008-01-14 17:46:18 -08006212
Zhu Yib481de92007-09-25 17:54:57 -07006213 mutex_unlock(&priv->mutex);
Zhu Yi5a66926a2008-01-14 17:46:18 -08006214
Zhu Yie655b9f2008-01-24 02:19:38 -08006215 if (ret)
6216 goto out_release_irq;
6217
6218 IWL_DEBUG_INFO("Start UP work done.\n");
6219
6220 if (test_bit(STATUS_IN_SUSPEND, &priv->status))
6221 return 0;
6222
Zhu Yi5a66926a2008-01-14 17:46:18 -08006223 /* Wait for START_ALIVE from ucode. Otherwise callbacks from
6224 * mac80211 will not be run successfully. */
6225 ret = wait_event_interruptible_timeout(priv->wait_command_queue,
6226 test_bit(STATUS_READY, &priv->status),
6227 UCODE_READY_TIMEOUT);
6228 if (!ret) {
6229 if (!test_bit(STATUS_READY, &priv->status)) {
6230 IWL_ERROR("Wait for START_ALIVE timeout after %dms.\n",
6231 jiffies_to_msecs(UCODE_READY_TIMEOUT));
6232 ret = -ETIMEDOUT;
6233 goto out_release_irq;
6234 }
6235 }
6236
Zhu Yie655b9f2008-01-24 02:19:38 -08006237 priv->is_open = 1;
Zhu Yib481de92007-09-25 17:54:57 -07006238 IWL_DEBUG_MAC80211("leave\n");
6239 return 0;
Zhu Yi5a66926a2008-01-14 17:46:18 -08006240
6241out_release_irq:
6242 free_irq(priv->pci_dev->irq, priv);
6243out_disable_msi:
6244 pci_disable_msi(priv->pci_dev);
Zhu Yie655b9f2008-01-24 02:19:38 -08006245 pci_disable_device(priv->pci_dev);
6246 priv->is_open = 0;
6247 IWL_DEBUG_MAC80211("leave - failed\n");
Zhu Yi5a66926a2008-01-14 17:46:18 -08006248 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07006249}
6250
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006251static void iwl4965_mac_stop(struct ieee80211_hw *hw)
Zhu Yib481de92007-09-25 17:54:57 -07006252{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006253 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07006254
6255 IWL_DEBUG_MAC80211("enter\n");
Mohamed Abbas948c1712007-10-25 17:15:45 +08006256
Zhu Yie655b9f2008-01-24 02:19:38 -08006257 if (!priv->is_open) {
6258 IWL_DEBUG_MAC80211("leave - skip\n");
6259 return;
6260 }
6261
Zhu Yib481de92007-09-25 17:54:57 -07006262 priv->is_open = 0;
Zhu Yi5a66926a2008-01-14 17:46:18 -08006263
Tomas Winklerfee12472008-04-03 16:05:21 -07006264 if (iwl_is_ready_rf(priv)) {
Zhu Yie655b9f2008-01-24 02:19:38 -08006265 /* stop mac, cancel any scan request and clear
6266 * RXON_FILTER_ASSOC_MSK BIT
6267 */
Zhu Yi5a66926a2008-01-14 17:46:18 -08006268 mutex_lock(&priv->mutex);
6269 iwl4965_scan_cancel_timeout(priv, 100);
6270 cancel_delayed_work(&priv->post_associate);
Mohamed Abbasfde35712007-11-29 11:10:15 +08006271 mutex_unlock(&priv->mutex);
Mohamed Abbasfde35712007-11-29 11:10:15 +08006272 }
6273
Zhu Yi5a66926a2008-01-14 17:46:18 -08006274 iwl4965_down(priv);
6275
6276 flush_workqueue(priv->workqueue);
6277 free_irq(priv->pci_dev->irq, priv);
6278 pci_disable_msi(priv->pci_dev);
6279 pci_save_state(priv->pci_dev);
6280 pci_disable_device(priv->pci_dev);
Mohamed Abbas948c1712007-10-25 17:15:45 +08006281
Zhu Yib481de92007-09-25 17:54:57 -07006282 IWL_DEBUG_MAC80211("leave\n");
Zhu Yib481de92007-09-25 17:54:57 -07006283}
6284
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006285static int iwl4965_mac_tx(struct ieee80211_hw *hw, struct sk_buff *skb,
Zhu Yib481de92007-09-25 17:54:57 -07006286 struct ieee80211_tx_control *ctl)
6287{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006288 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07006289
6290 IWL_DEBUG_MAC80211("enter\n");
6291
6292 if (priv->iw_mode == IEEE80211_IF_TYPE_MNTR) {
6293 IWL_DEBUG_MAC80211("leave - monitor\n");
6294 return -1;
6295 }
6296
6297 IWL_DEBUG_TX("dev->xmit(%d bytes) at rate 0x%02x\n", skb->len,
Johannes Berg8318d782008-01-24 19:38:38 +01006298 ctl->tx_rate->bitrate);
Zhu Yib481de92007-09-25 17:54:57 -07006299
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006300 if (iwl4965_tx_skb(priv, skb, ctl))
Zhu Yib481de92007-09-25 17:54:57 -07006301 dev_kfree_skb_any(skb);
6302
6303 IWL_DEBUG_MAC80211("leave\n");
6304 return 0;
6305}
6306
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006307static int iwl4965_mac_add_interface(struct ieee80211_hw *hw,
Zhu Yib481de92007-09-25 17:54:57 -07006308 struct ieee80211_if_init_conf *conf)
6309{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006310 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07006311 unsigned long flags;
Joe Perches0795af52007-10-03 17:59:30 -07006312 DECLARE_MAC_BUF(mac);
Zhu Yib481de92007-09-25 17:54:57 -07006313
Johannes Berg32bfd352007-12-19 01:31:26 +01006314 IWL_DEBUG_MAC80211("enter: type %d\n", conf->type);
Zhu Yib481de92007-09-25 17:54:57 -07006315
Johannes Berg32bfd352007-12-19 01:31:26 +01006316 if (priv->vif) {
6317 IWL_DEBUG_MAC80211("leave - vif != NULL\n");
Reinette Chatre75849d22008-01-23 10:15:17 -08006318 return -EOPNOTSUPP;
Zhu Yib481de92007-09-25 17:54:57 -07006319 }
6320
6321 spin_lock_irqsave(&priv->lock, flags);
Johannes Berg32bfd352007-12-19 01:31:26 +01006322 priv->vif = conf->vif;
Zhu Yib481de92007-09-25 17:54:57 -07006323
6324 spin_unlock_irqrestore(&priv->lock, flags);
6325
6326 mutex_lock(&priv->mutex);
Tomas Winkler864792e2007-11-27 21:00:52 +02006327
6328 if (conf->mac_addr) {
6329 IWL_DEBUG_MAC80211("Set %s\n", print_mac(mac, conf->mac_addr));
6330 memcpy(priv->mac_addr, conf->mac_addr, ETH_ALEN);
6331 }
Zhu Yib481de92007-09-25 17:54:57 -07006332
Tomas Winklerfee12472008-04-03 16:05:21 -07006333 if (iwl_is_ready(priv))
Zhu Yi5a66926a2008-01-14 17:46:18 -08006334 iwl4965_set_mode(priv, conf->type);
6335
Zhu Yib481de92007-09-25 17:54:57 -07006336 mutex_unlock(&priv->mutex);
6337
Zhu Yi5a66926a2008-01-14 17:46:18 -08006338 IWL_DEBUG_MAC80211("leave\n");
Zhu Yib481de92007-09-25 17:54:57 -07006339 return 0;
6340}
6341
6342/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006343 * iwl4965_mac_config - mac80211 config callback
Zhu Yib481de92007-09-25 17:54:57 -07006344 *
6345 * We ignore conf->flags & IEEE80211_CONF_SHORT_SLOT_TIME since it seems to
6346 * be set inappropriately and the driver currently sets the hardware up to
6347 * use it whenever needed.
6348 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006349static int iwl4965_mac_config(struct ieee80211_hw *hw, struct ieee80211_conf *conf)
Zhu Yib481de92007-09-25 17:54:57 -07006350{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006351 struct iwl_priv *priv = hw->priv;
Assaf Kraussbf85ea42008-03-14 10:38:49 -07006352 const struct iwl_channel_info *ch_info;
Zhu Yib481de92007-09-25 17:54:57 -07006353 unsigned long flags;
Zhu Yi76bb77e2007-11-22 10:53:22 +08006354 int ret = 0;
Zhu Yib481de92007-09-25 17:54:57 -07006355
6356 mutex_lock(&priv->mutex);
Johannes Berg8318d782008-01-24 19:38:38 +01006357 IWL_DEBUG_MAC80211("enter to channel %d\n", conf->channel->hw_value);
Zhu Yib481de92007-09-25 17:54:57 -07006358
Zhu Yi12342c42007-12-20 11:27:32 +08006359 priv->add_radiotap = !!(conf->flags & IEEE80211_CONF_RADIOTAP);
6360
Tomas Winklerfee12472008-04-03 16:05:21 -07006361 if (!iwl_is_ready(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07006362 IWL_DEBUG_MAC80211("leave - not ready\n");
Zhu Yi76bb77e2007-11-22 10:53:22 +08006363 ret = -EIO;
6364 goto out;
Zhu Yib481de92007-09-25 17:54:57 -07006365 }
6366
Assaf Krauss1ea87392008-03-18 14:57:50 -07006367 if (unlikely(!priv->cfg->mod_params->disable_hw_scan &&
Zhu Yib481de92007-09-25 17:54:57 -07006368 test_bit(STATUS_SCANNING, &priv->status))) {
Zhu Yia0646472007-12-20 14:10:01 +08006369 IWL_DEBUG_MAC80211("leave - scanning\n");
6370 set_bit(STATUS_CONF_PENDING, &priv->status);
Zhu Yib481de92007-09-25 17:54:57 -07006371 mutex_unlock(&priv->mutex);
Zhu Yia0646472007-12-20 14:10:01 +08006372 return 0;
Zhu Yib481de92007-09-25 17:54:57 -07006373 }
6374
6375 spin_lock_irqsave(&priv->lock, flags);
6376
Assaf Krauss8622e702008-03-21 13:53:43 -07006377 ch_info = iwl_get_channel_info(priv, conf->channel->band,
Johannes Berg8318d782008-01-24 19:38:38 +01006378 ieee80211_frequency_to_channel(conf->channel->center_freq));
Zhu Yib481de92007-09-25 17:54:57 -07006379 if (!is_channel_valid(ch_info)) {
Zhu Yib481de92007-09-25 17:54:57 -07006380 IWL_DEBUG_MAC80211("leave - invalid channel\n");
6381 spin_unlock_irqrestore(&priv->lock, flags);
Zhu Yi76bb77e2007-11-22 10:53:22 +08006382 ret = -EINVAL;
6383 goto out;
Zhu Yib481de92007-09-25 17:54:57 -07006384 }
6385
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08006386#ifdef CONFIG_IWL4965_HT
Tomas Winkler78330fd2008-02-06 02:37:18 +02006387 /* if we are switching from ht to 2.4 clear flags
Zhu Yib481de92007-09-25 17:54:57 -07006388 * from any ht related info since 2.4 does not
6389 * support ht */
Tomas Winkler78330fd2008-02-06 02:37:18 +02006390 if ((le16_to_cpu(priv->staging_rxon.channel) != conf->channel->hw_value)
Zhu Yib481de92007-09-25 17:54:57 -07006391#ifdef IEEE80211_CONF_CHANNEL_SWITCH
6392 && !(conf->flags & IEEE80211_CONF_CHANNEL_SWITCH)
6393#endif
6394 )
6395 priv->staging_rxon.flags = 0;
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08006396#endif /* CONFIG_IWL4965_HT */
Zhu Yib481de92007-09-25 17:54:57 -07006397
Assaf Kraussbf85ea42008-03-14 10:38:49 -07006398 iwlcore_set_rxon_channel(priv, conf->channel->band,
Johannes Berg8318d782008-01-24 19:38:38 +01006399 ieee80211_frequency_to_channel(conf->channel->center_freq));
Zhu Yib481de92007-09-25 17:54:57 -07006400
Johannes Berg8318d782008-01-24 19:38:38 +01006401 iwl4965_set_flags_for_phymode(priv, conf->channel->band);
Zhu Yib481de92007-09-25 17:54:57 -07006402
6403 /* The list of supported rates and rate mask can be different
Johannes Berg8318d782008-01-24 19:38:38 +01006404 * for each band; since the band may have changed, reset
Zhu Yib481de92007-09-25 17:54:57 -07006405 * the rate mask to what mac80211 lists */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006406 iwl4965_set_rate(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006407
6408 spin_unlock_irqrestore(&priv->lock, flags);
6409
6410#ifdef IEEE80211_CONF_CHANNEL_SWITCH
6411 if (conf->flags & IEEE80211_CONF_CHANNEL_SWITCH) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006412 iwl4965_hw_channel_switch(priv, conf->channel);
Zhu Yi76bb77e2007-11-22 10:53:22 +08006413 goto out;
Zhu Yib481de92007-09-25 17:54:57 -07006414 }
6415#endif
6416
Mohamed Abbasad97edd2008-03-28 16:21:06 -07006417 if (priv->cfg->ops->lib->radio_kill_sw)
6418 priv->cfg->ops->lib->radio_kill_sw(priv, !conf->radio_enabled);
Zhu Yib481de92007-09-25 17:54:57 -07006419
6420 if (!conf->radio_enabled) {
6421 IWL_DEBUG_MAC80211("leave - radio disabled\n");
Zhu Yi76bb77e2007-11-22 10:53:22 +08006422 goto out;
Zhu Yib481de92007-09-25 17:54:57 -07006423 }
6424
Tomas Winklerfee12472008-04-03 16:05:21 -07006425 if (iwl_is_rfkill(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07006426 IWL_DEBUG_MAC80211("leave - RF kill\n");
Zhu Yi76bb77e2007-11-22 10:53:22 +08006427 ret = -EIO;
6428 goto out;
Zhu Yib481de92007-09-25 17:54:57 -07006429 }
6430
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006431 iwl4965_set_rate(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006432
6433 if (memcmp(&priv->active_rxon,
6434 &priv->staging_rxon, sizeof(priv->staging_rxon)))
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006435 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006436 else
6437 IWL_DEBUG_INFO("No re-sending same RXON configuration.\n");
6438
6439 IWL_DEBUG_MAC80211("leave\n");
6440
Zhu Yia0646472007-12-20 14:10:01 +08006441out:
6442 clear_bit(STATUS_CONF_PENDING, &priv->status);
Zhu Yi5a66926a2008-01-14 17:46:18 -08006443 mutex_unlock(&priv->mutex);
Zhu Yi76bb77e2007-11-22 10:53:22 +08006444 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07006445}
6446
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006447static void iwl4965_config_ap(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07006448{
Tomas Winkler857485c2008-03-21 13:53:44 -07006449 int ret = 0;
Zhu Yib481de92007-09-25 17:54:57 -07006450
Maarten Lankhorstd986bcd2008-01-23 10:15:16 -08006451 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
Zhu Yib481de92007-09-25 17:54:57 -07006452 return;
6453
6454 /* The following should be done only at AP bring up */
6455 if ((priv->active_rxon.filter_flags & RXON_FILTER_ASSOC_MSK) == 0) {
6456
6457 /* RXON - unassoc (to set timing command) */
6458 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006459 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006460
6461 /* RXON Timing */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006462 memset(&priv->rxon_timing, 0, sizeof(struct iwl4965_rxon_time_cmd));
6463 iwl4965_setup_rxon_timing(priv);
Tomas Winkler857485c2008-03-21 13:53:44 -07006464 ret = iwl_send_cmd_pdu(priv, REPLY_RXON_TIMING,
Zhu Yib481de92007-09-25 17:54:57 -07006465 sizeof(priv->rxon_timing), &priv->rxon_timing);
Tomas Winkler857485c2008-03-21 13:53:44 -07006466 if (ret)
Zhu Yib481de92007-09-25 17:54:57 -07006467 IWL_WARNING("REPLY_RXON_TIMING failed - "
6468 "Attempting to continue.\n");
6469
6470 iwl4965_set_rxon_chain(priv);
6471
6472 /* FIXME: what should be the assoc_id for AP? */
6473 priv->staging_rxon.assoc_id = cpu_to_le16(priv->assoc_id);
6474 if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_PREAMBLE)
6475 priv->staging_rxon.flags |=
6476 RXON_FLG_SHORT_PREAMBLE_MSK;
6477 else
6478 priv->staging_rxon.flags &=
6479 ~RXON_FLG_SHORT_PREAMBLE_MSK;
6480
6481 if (priv->staging_rxon.flags & RXON_FLG_BAND_24G_MSK) {
6482 if (priv->assoc_capability &
6483 WLAN_CAPABILITY_SHORT_SLOT_TIME)
6484 priv->staging_rxon.flags |=
6485 RXON_FLG_SHORT_SLOT_MSK;
6486 else
6487 priv->staging_rxon.flags &=
6488 ~RXON_FLG_SHORT_SLOT_MSK;
6489
6490 if (priv->iw_mode == IEEE80211_IF_TYPE_IBSS)
6491 priv->staging_rxon.flags &=
6492 ~RXON_FLG_SHORT_SLOT_MSK;
6493 }
6494 /* restore RXON assoc */
6495 priv->staging_rxon.filter_flags |= RXON_FILTER_ASSOC_MSK;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006496 iwl4965_commit_rxon(priv);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006497 iwl4965_activate_qos(priv, 1);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006498 iwl4965_rxon_add_station(priv, iwl4965_broadcast_addr, 0);
Zhu Yie1493de2007-09-27 11:27:32 +08006499 }
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006500 iwl4965_send_beacon_cmd(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006501
6502 /* FIXME - we need to add code here to detect a totally new
6503 * configuration, reset the AP, unassoc, rxon timing, assoc,
6504 * clear sta table, add BCAST sta... */
6505}
6506
Johannes Berg32bfd352007-12-19 01:31:26 +01006507static int iwl4965_mac_config_interface(struct ieee80211_hw *hw,
6508 struct ieee80211_vif *vif,
Zhu Yib481de92007-09-25 17:54:57 -07006509 struct ieee80211_if_conf *conf)
6510{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006511 struct iwl_priv *priv = hw->priv;
Joe Perches0795af52007-10-03 17:59:30 -07006512 DECLARE_MAC_BUF(mac);
Zhu Yib481de92007-09-25 17:54:57 -07006513 unsigned long flags;
6514 int rc;
6515
6516 if (conf == NULL)
6517 return -EIO;
6518
Emmanuel Grumbachb716bb92008-03-04 18:09:32 -08006519 if (priv->vif != vif) {
6520 IWL_DEBUG_MAC80211("leave - priv->vif != vif\n");
6521 mutex_unlock(&priv->mutex);
6522 return 0;
6523 }
6524
Zhu Yib481de92007-09-25 17:54:57 -07006525 if ((priv->iw_mode == IEEE80211_IF_TYPE_AP) &&
6526 (!conf->beacon || !conf->ssid_len)) {
6527 IWL_DEBUG_MAC80211
6528 ("Leaving in AP mode because HostAPD is not ready.\n");
6529 return 0;
6530 }
6531
Tomas Winklerfee12472008-04-03 16:05:21 -07006532 if (!iwl_is_alive(priv))
Zhu Yi5a66926a2008-01-14 17:46:18 -08006533 return -EAGAIN;
6534
Zhu Yib481de92007-09-25 17:54:57 -07006535 mutex_lock(&priv->mutex);
6536
Zhu Yib481de92007-09-25 17:54:57 -07006537 if (conf->bssid)
Joe Perches0795af52007-10-03 17:59:30 -07006538 IWL_DEBUG_MAC80211("bssid: %s\n",
6539 print_mac(mac, conf->bssid));
Zhu Yib481de92007-09-25 17:54:57 -07006540
Johannes Berg4150c572007-09-17 01:29:23 -04006541/*
6542 * very dubious code was here; the probe filtering flag is never set:
6543 *
Zhu Yib481de92007-09-25 17:54:57 -07006544 if (unlikely(test_bit(STATUS_SCANNING, &priv->status)) &&
6545 !(priv->hw->flags & IEEE80211_HW_NO_PROBE_FILTERING)) {
Johannes Berg4150c572007-09-17 01:29:23 -04006546 */
Zhu Yib481de92007-09-25 17:54:57 -07006547
6548 if (priv->iw_mode == IEEE80211_IF_TYPE_AP) {
6549 if (!conf->bssid) {
6550 conf->bssid = priv->mac_addr;
6551 memcpy(priv->bssid, priv->mac_addr, ETH_ALEN);
Joe Perches0795af52007-10-03 17:59:30 -07006552 IWL_DEBUG_MAC80211("bssid was set to: %s\n",
6553 print_mac(mac, conf->bssid));
Zhu Yib481de92007-09-25 17:54:57 -07006554 }
6555 if (priv->ibss_beacon)
6556 dev_kfree_skb(priv->ibss_beacon);
6557
6558 priv->ibss_beacon = conf->beacon;
6559 }
6560
Tomas Winklerfee12472008-04-03 16:05:21 -07006561 if (iwl_is_rfkill(priv))
Mohamed Abbasfde35712007-11-29 11:10:15 +08006562 goto done;
6563
Zhu Yib481de92007-09-25 17:54:57 -07006564 if (conf->bssid && !is_zero_ether_addr(conf->bssid) &&
6565 !is_multicast_ether_addr(conf->bssid)) {
6566 /* If there is currently a HW scan going on in the background
6567 * then we need to cancel it else the RXON below will fail. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006568 if (iwl4965_scan_cancel_timeout(priv, 100)) {
Zhu Yib481de92007-09-25 17:54:57 -07006569 IWL_WARNING("Aborted scan still in progress "
6570 "after 100ms\n");
6571 IWL_DEBUG_MAC80211("leaving - scan abort failed.\n");
6572 mutex_unlock(&priv->mutex);
6573 return -EAGAIN;
6574 }
6575 memcpy(priv->staging_rxon.bssid_addr, conf->bssid, ETH_ALEN);
6576
6577 /* TODO: Audit driver for usage of these members and see
6578 * if mac80211 deprecates them (priv->bssid looks like it
6579 * shouldn't be there, but I haven't scanned the IBSS code
6580 * to verify) - jpk */
6581 memcpy(priv->bssid, conf->bssid, ETH_ALEN);
6582
6583 if (priv->iw_mode == IEEE80211_IF_TYPE_AP)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006584 iwl4965_config_ap(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006585 else {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006586 rc = iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006587 if ((priv->iw_mode == IEEE80211_IF_TYPE_STA) && rc)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006588 iwl4965_rxon_add_station(
Zhu Yib481de92007-09-25 17:54:57 -07006589 priv, priv->active_rxon.bssid_addr, 1);
6590 }
6591
6592 } else {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006593 iwl4965_scan_cancel_timeout(priv, 100);
Zhu Yib481de92007-09-25 17:54:57 -07006594 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006595 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006596 }
6597
Mohamed Abbasfde35712007-11-29 11:10:15 +08006598 done:
Zhu Yib481de92007-09-25 17:54:57 -07006599 spin_lock_irqsave(&priv->lock, flags);
6600 if (!conf->ssid_len)
6601 memset(priv->essid, 0, IW_ESSID_MAX_SIZE);
6602 else
6603 memcpy(priv->essid, conf->ssid, conf->ssid_len);
6604
6605 priv->essid_len = conf->ssid_len;
6606 spin_unlock_irqrestore(&priv->lock, flags);
6607
6608 IWL_DEBUG_MAC80211("leave\n");
6609 mutex_unlock(&priv->mutex);
6610
6611 return 0;
6612}
6613
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006614static void iwl4965_configure_filter(struct ieee80211_hw *hw,
Johannes Berg4150c572007-09-17 01:29:23 -04006615 unsigned int changed_flags,
6616 unsigned int *total_flags,
6617 int mc_count, struct dev_addr_list *mc_list)
6618{
6619 /*
6620 * XXX: dummy
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006621 * see also iwl4965_connection_init_rx_config
Johannes Berg4150c572007-09-17 01:29:23 -04006622 */
6623 *total_flags = 0;
6624}
6625
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006626static void iwl4965_mac_remove_interface(struct ieee80211_hw *hw,
Zhu Yib481de92007-09-25 17:54:57 -07006627 struct ieee80211_if_init_conf *conf)
6628{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006629 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07006630
6631 IWL_DEBUG_MAC80211("enter\n");
6632
6633 mutex_lock(&priv->mutex);
Mohamed Abbas948c1712007-10-25 17:15:45 +08006634
Tomas Winklerfee12472008-04-03 16:05:21 -07006635 if (iwl_is_ready_rf(priv)) {
Mohamed Abbasfde35712007-11-29 11:10:15 +08006636 iwl4965_scan_cancel_timeout(priv, 100);
6637 cancel_delayed_work(&priv->post_associate);
6638 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
6639 iwl4965_commit_rxon(priv);
6640 }
Johannes Berg32bfd352007-12-19 01:31:26 +01006641 if (priv->vif == conf->vif) {
6642 priv->vif = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07006643 memset(priv->bssid, 0, ETH_ALEN);
6644 memset(priv->essid, 0, IW_ESSID_MAX_SIZE);
6645 priv->essid_len = 0;
6646 }
6647 mutex_unlock(&priv->mutex);
6648
6649 IWL_DEBUG_MAC80211("leave\n");
6650
6651}
Johannes Berg471b3ef2007-12-28 14:32:58 +01006652
Tomas Winkler98952d52008-03-28 16:33:33 -07006653
6654#ifdef CONFIG_IWL4965_HT
6655static void iwl4965_ht_conf(struct iwl_priv *priv,
6656 struct ieee80211_bss_conf *bss_conf)
6657{
6658 struct ieee80211_ht_info *ht_conf = bss_conf->ht_conf;
6659 struct ieee80211_ht_bss_info *ht_bss_conf = bss_conf->ht_bss_conf;
6660 struct iwl_ht_info *iwl_conf = &priv->current_ht_config;
6661
6662 IWL_DEBUG_MAC80211("enter: \n");
6663
6664 iwl_conf->is_ht = bss_conf->assoc_ht;
6665
6666 if (!iwl_conf->is_ht)
6667 return;
6668
6669 priv->ps_mode = (u8)((ht_conf->cap & IEEE80211_HT_CAP_MIMO_PS) >> 2);
6670
6671 if (ht_conf->cap & IEEE80211_HT_CAP_SGI_20)
6672 iwl_conf->sgf |= 0x1;
6673 if (ht_conf->cap & IEEE80211_HT_CAP_SGI_40)
6674 iwl_conf->sgf |= 0x2;
6675
6676 iwl_conf->is_green_field = !!(ht_conf->cap & IEEE80211_HT_CAP_GRN_FLD);
6677 iwl_conf->max_amsdu_size =
6678 !!(ht_conf->cap & IEEE80211_HT_CAP_MAX_AMSDU);
6679
6680 iwl_conf->supported_chan_width =
6681 !!(ht_conf->cap & IEEE80211_HT_CAP_SUP_WIDTH);
6682 iwl_conf->extension_chan_offset =
6683 ht_bss_conf->bss_cap & IEEE80211_HT_IE_CHA_SEC_OFFSET;
6684 /* If no above or below channel supplied disable FAT channel */
6685 if (iwl_conf->extension_chan_offset != IWL_EXT_CHANNEL_OFFSET_ABOVE &&
6686 iwl_conf->extension_chan_offset != IWL_EXT_CHANNEL_OFFSET_BELOW)
6687 iwl_conf->supported_chan_width = 0;
6688
6689 iwl_conf->tx_mimo_ps_mode =
6690 (u8)((ht_conf->cap & IEEE80211_HT_CAP_MIMO_PS) >> 2);
6691 memcpy(iwl_conf->supp_mcs_set, ht_conf->supp_mcs_set, 16);
6692
6693 iwl_conf->control_channel = ht_bss_conf->primary_channel;
6694 iwl_conf->tx_chan_width =
6695 !!(ht_bss_conf->bss_cap & IEEE80211_HT_IE_CHA_WIDTH);
6696 iwl_conf->ht_protection =
6697 ht_bss_conf->bss_op_mode & IEEE80211_HT_IE_HT_PROTECTION;
6698 iwl_conf->non_GF_STA_present =
6699 !!(ht_bss_conf->bss_op_mode & IEEE80211_HT_IE_NON_GF_STA_PRSNT);
6700
6701 IWL_DEBUG_MAC80211("control channel %d\n", iwl_conf->control_channel);
6702 IWL_DEBUG_MAC80211("leave\n");
6703}
6704#else
6705static inline void iwl4965_ht_conf(struct iwl_priv *priv,
6706 struct ieee80211_bss_conf *bss_conf)
6707{
6708}
6709#endif
6710
Tomas Winkler3109ece2008-03-28 16:33:35 -07006711#define IWL_DELAY_NEXT_SCAN_AFTER_ASSOC (HZ*6)
Johannes Berg471b3ef2007-12-28 14:32:58 +01006712static void iwl4965_bss_info_changed(struct ieee80211_hw *hw,
6713 struct ieee80211_vif *vif,
6714 struct ieee80211_bss_conf *bss_conf,
6715 u32 changes)
Tomas Winkler220173b2007-10-16 00:50:25 +02006716{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006717 struct iwl_priv *priv = hw->priv;
Tomas Winkler220173b2007-10-16 00:50:25 +02006718
Tomas Winkler3109ece2008-03-28 16:33:35 -07006719 IWL_DEBUG_MAC80211("changes = 0x%X\n", changes);
6720
Johannes Berg471b3ef2007-12-28 14:32:58 +01006721 if (changes & BSS_CHANGED_ERP_PREAMBLE) {
Tomas Winkler3109ece2008-03-28 16:33:35 -07006722 IWL_DEBUG_MAC80211("ERP_PREAMBLE %d\n",
6723 bss_conf->use_short_preamble);
Johannes Berg471b3ef2007-12-28 14:32:58 +01006724 if (bss_conf->use_short_preamble)
Tomas Winkler220173b2007-10-16 00:50:25 +02006725 priv->staging_rxon.flags |= RXON_FLG_SHORT_PREAMBLE_MSK;
6726 else
6727 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_PREAMBLE_MSK;
6728 }
6729
Johannes Berg471b3ef2007-12-28 14:32:58 +01006730 if (changes & BSS_CHANGED_ERP_CTS_PROT) {
Tomas Winkler3109ece2008-03-28 16:33:35 -07006731 IWL_DEBUG_MAC80211("ERP_CTS %d\n", bss_conf->use_cts_prot);
Johannes Berg8318d782008-01-24 19:38:38 +01006732 if (bss_conf->use_cts_prot && (priv->band != IEEE80211_BAND_5GHZ))
Tomas Winkler220173b2007-10-16 00:50:25 +02006733 priv->staging_rxon.flags |= RXON_FLG_TGG_PROTECT_MSK;
6734 else
6735 priv->staging_rxon.flags &= ~RXON_FLG_TGG_PROTECT_MSK;
6736 }
6737
Tomas Winkler98952d52008-03-28 16:33:33 -07006738 if (changes & BSS_CHANGED_HT) {
Tomas Winkler3109ece2008-03-28 16:33:35 -07006739 IWL_DEBUG_MAC80211("HT %d\n", bss_conf->assoc_ht);
Tomas Winkler98952d52008-03-28 16:33:33 -07006740 iwl4965_ht_conf(priv, bss_conf);
6741 iwl4965_set_rxon_chain(priv);
6742 }
6743
Johannes Berg471b3ef2007-12-28 14:32:58 +01006744 if (changes & BSS_CHANGED_ASSOC) {
Tomas Winkler3109ece2008-03-28 16:33:35 -07006745 IWL_DEBUG_MAC80211("ASSOC %d\n", bss_conf->assoc);
Reinette Chatre508e32e2008-04-14 21:16:13 -07006746 /* This should never happen as this function should
6747 * never be called from interrupt context. */
6748 if (WARN_ON_ONCE(in_interrupt()))
6749 return;
Tomas Winkler3109ece2008-03-28 16:33:35 -07006750 if (bss_conf->assoc) {
6751 priv->assoc_id = bss_conf->aid;
6752 priv->beacon_int = bss_conf->beacon_int;
6753 priv->timestamp = bss_conf->timestamp;
6754 priv->assoc_capability = bss_conf->assoc_capability;
6755 priv->next_scan_jiffies = jiffies +
6756 IWL_DELAY_NEXT_SCAN_AFTER_ASSOC;
Reinette Chatre508e32e2008-04-14 21:16:13 -07006757 mutex_lock(&priv->mutex);
6758 iwl4965_post_associate(priv);
6759 mutex_unlock(&priv->mutex);
Tomas Winkler3109ece2008-03-28 16:33:35 -07006760 } else {
6761 priv->assoc_id = 0;
6762 IWL_DEBUG_MAC80211("DISASSOC %d\n", bss_conf->assoc);
6763 }
6764 } else if (changes && iwl_is_associated(priv) && priv->assoc_id) {
6765 IWL_DEBUG_MAC80211("Associated Changes %d\n", changes);
6766 iwl4965_send_rxon_assoc(priv);
Johannes Berg471b3ef2007-12-28 14:32:58 +01006767 }
6768
Tomas Winkler220173b2007-10-16 00:50:25 +02006769}
Zhu Yib481de92007-09-25 17:54:57 -07006770
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006771static int iwl4965_mac_hw_scan(struct ieee80211_hw *hw, u8 *ssid, size_t len)
Zhu Yib481de92007-09-25 17:54:57 -07006772{
6773 int rc = 0;
6774 unsigned long flags;
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006775 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07006776
6777 IWL_DEBUG_MAC80211("enter\n");
6778
mabbas052c4b92007-10-25 17:15:43 +08006779 mutex_lock(&priv->mutex);
Zhu Yib481de92007-09-25 17:54:57 -07006780 spin_lock_irqsave(&priv->lock, flags);
6781
Tomas Winklerfee12472008-04-03 16:05:21 -07006782 if (!iwl_is_ready_rf(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07006783 rc = -EIO;
6784 IWL_DEBUG_MAC80211("leave - not ready or exit pending\n");
6785 goto out_unlock;
6786 }
6787
6788 if (priv->iw_mode == IEEE80211_IF_TYPE_AP) { /* APs don't scan */
6789 rc = -EIO;
6790 IWL_ERROR("ERROR: APs don't scan\n");
6791 goto out_unlock;
6792 }
6793
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08006794 /* we don't schedule scan within next_scan_jiffies period */
6795 if (priv->next_scan_jiffies &&
6796 time_after(priv->next_scan_jiffies, jiffies)) {
6797 rc = -EAGAIN;
6798 goto out_unlock;
6799 }
Zhu Yib481de92007-09-25 17:54:57 -07006800 /* if we just finished scan ask for delay */
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08006801 if (priv->last_scan_jiffies && time_after(priv->last_scan_jiffies +
6802 IWL_DELAY_NEXT_SCAN, jiffies)) {
Zhu Yib481de92007-09-25 17:54:57 -07006803 rc = -EAGAIN;
6804 goto out_unlock;
6805 }
6806 if (len) {
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08006807 IWL_DEBUG_SCAN("direct scan for %s [%d]\n ",
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006808 iwl4965_escape_essid(ssid, len), (int)len);
Zhu Yib481de92007-09-25 17:54:57 -07006809
6810 priv->one_direct_scan = 1;
6811 priv->direct_ssid_len = (u8)
6812 min((u8) len, (u8) IW_ESSID_MAX_SIZE);
6813 memcpy(priv->direct_ssid, ssid, priv->direct_ssid_len);
Mohamed Abbas948c1712007-10-25 17:15:45 +08006814 } else
6815 priv->one_direct_scan = 0;
Zhu Yib481de92007-09-25 17:54:57 -07006816
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006817 rc = iwl4965_scan_initiate(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006818
6819 IWL_DEBUG_MAC80211("leave\n");
6820
6821out_unlock:
6822 spin_unlock_irqrestore(&priv->lock, flags);
mabbas052c4b92007-10-25 17:15:43 +08006823 mutex_unlock(&priv->mutex);
Zhu Yib481de92007-09-25 17:54:57 -07006824
6825 return rc;
6826}
6827
Emmanuel Grumbachab885f82008-03-20 15:06:43 +02006828static void iwl4965_mac_update_tkip_key(struct ieee80211_hw *hw,
6829 struct ieee80211_key_conf *keyconf, const u8 *addr,
6830 u32 iv32, u16 *phase1key)
6831{
6832 struct iwl_priv *priv = hw->priv;
6833 u8 sta_id = IWL_INVALID_STATION;
6834 unsigned long flags;
6835 __le16 key_flags = 0;
6836 int i;
6837 DECLARE_MAC_BUF(mac);
6838
6839 IWL_DEBUG_MAC80211("enter\n");
6840
6841 sta_id = iwl4965_hw_find_station(priv, addr);
6842 if (sta_id == IWL_INVALID_STATION) {
6843 IWL_DEBUG_MAC80211("leave - %s not in station map.\n",
6844 print_mac(mac, addr));
6845 return;
6846 }
6847
6848 iwl4965_scan_cancel_timeout(priv, 100);
6849
6850 key_flags |= (STA_KEY_FLG_TKIP | STA_KEY_FLG_MAP_KEY_MSK);
6851 key_flags |= cpu_to_le16(keyconf->keyidx << STA_KEY_FLG_KEYID_POS);
6852 key_flags &= ~STA_KEY_FLG_INVALID;
6853
6854 if (sta_id == priv->hw_setting.bcast_sta_id)
6855 key_flags |= STA_KEY_MULTICAST_MSK;
6856
6857 spin_lock_irqsave(&priv->sta_lock, flags);
6858
6859 priv->stations[sta_id].sta.key.key_offset =
Emmanuel Grumbach80fb47a2008-04-14 21:16:08 -07006860 iwl_get_free_ucode_key_index(priv);
Emmanuel Grumbachab885f82008-03-20 15:06:43 +02006861 priv->stations[sta_id].sta.key.key_flags = key_flags;
6862 priv->stations[sta_id].sta.key.tkip_rx_tsc_byte2 = (u8) iv32;
6863
6864 for (i = 0; i < 5; i++)
6865 priv->stations[sta_id].sta.key.tkip_rx_ttak[i] =
6866 cpu_to_le16(phase1key[i]);
6867
6868 priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
6869 priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
6870
6871 iwl4965_send_add_station(priv, &priv->stations[sta_id].sta, CMD_ASYNC);
6872
6873 spin_unlock_irqrestore(&priv->sta_lock, flags);
6874
6875 IWL_DEBUG_MAC80211("leave\n");
6876}
6877
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006878static int iwl4965_mac_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
Zhu Yib481de92007-09-25 17:54:57 -07006879 const u8 *local_addr, const u8 *addr,
6880 struct ieee80211_key_conf *key)
6881{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006882 struct iwl_priv *priv = hw->priv;
Joe Perches0795af52007-10-03 17:59:30 -07006883 DECLARE_MAC_BUF(mac);
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07006884 int ret = 0;
6885 u8 sta_id = IWL_INVALID_STATION;
Emmanuel Grumbach6974e362008-04-14 21:16:06 -07006886 u8 is_default_wep_key = 0;
Zhu Yib481de92007-09-25 17:54:57 -07006887
6888 IWL_DEBUG_MAC80211("enter\n");
6889
Assaf Krauss1ea87392008-03-18 14:57:50 -07006890 if (!priv->cfg->mod_params->hw_crypto) {
Zhu Yib481de92007-09-25 17:54:57 -07006891 IWL_DEBUG_MAC80211("leave - hwcrypto disabled\n");
6892 return -EOPNOTSUPP;
6893 }
6894
6895 if (is_zero_ether_addr(addr))
6896 /* only support pairwise keys */
6897 return -EOPNOTSUPP;
6898
Emmanuel Grumbach6974e362008-04-14 21:16:06 -07006899 sta_id = iwl4965_hw_find_station(priv, addr);
6900 if (sta_id == IWL_INVALID_STATION) {
6901 IWL_DEBUG_MAC80211("leave - %s not in station map.\n",
6902 print_mac(mac, addr));
6903 return -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07006904
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07006905 }
Zhu Yib481de92007-09-25 17:54:57 -07006906
Emmanuel Grumbach6974e362008-04-14 21:16:06 -07006907 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006908 iwl4965_scan_cancel_timeout(priv, 100);
Emmanuel Grumbach6974e362008-04-14 21:16:06 -07006909 mutex_unlock(&priv->mutex);
6910
6911 /* If we are getting WEP group key and we didn't receive any key mapping
6912 * so far, we are in legacy wep mode (group key only), otherwise we are
6913 * in 1X mode.
6914 * In legacy wep mode, we use another host command to the uCode */
6915 if (key->alg == ALG_WEP && sta_id == priv->hw_setting.bcast_sta_id &&
6916 priv->iw_mode != IEEE80211_IF_TYPE_AP) {
6917 if (cmd == SET_KEY)
6918 is_default_wep_key = !priv->key_mapping_key;
6919 else
6920 is_default_wep_key = priv->default_wep_key;
6921 }
mabbas052c4b92007-10-25 17:15:43 +08006922
Zhu Yib481de92007-09-25 17:54:57 -07006923 switch (cmd) {
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07006924 case SET_KEY:
Emmanuel Grumbach6974e362008-04-14 21:16:06 -07006925 if (is_default_wep_key)
6926 ret = iwl_set_default_wep_key(priv, key);
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07006927 else
Emmanuel Grumbach74805132008-04-14 21:16:09 -07006928 ret = iwl_set_dynamic_key(priv, key, sta_id);
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07006929
6930 IWL_DEBUG_MAC80211("enable hwcrypto key\n");
Zhu Yib481de92007-09-25 17:54:57 -07006931 break;
6932 case DISABLE_KEY:
Emmanuel Grumbach6974e362008-04-14 21:16:06 -07006933 if (is_default_wep_key)
6934 ret = iwl_remove_default_wep_key(priv, key);
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07006935 else
Emmanuel Grumbach74805132008-04-14 21:16:09 -07006936 ret = iwl_remove_dynamic_key(priv, sta_id);
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07006937
6938 IWL_DEBUG_MAC80211("disable hwcrypto key\n");
Zhu Yib481de92007-09-25 17:54:57 -07006939 break;
6940 default:
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07006941 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07006942 }
6943
6944 IWL_DEBUG_MAC80211("leave\n");
Zhu Yib481de92007-09-25 17:54:57 -07006945
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07006946 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07006947}
6948
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006949static int iwl4965_mac_conf_tx(struct ieee80211_hw *hw, int queue,
Zhu Yib481de92007-09-25 17:54:57 -07006950 const struct ieee80211_tx_queue_params *params)
6951{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006952 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07006953 unsigned long flags;
6954 int q;
Zhu Yib481de92007-09-25 17:54:57 -07006955
6956 IWL_DEBUG_MAC80211("enter\n");
6957
Tomas Winklerfee12472008-04-03 16:05:21 -07006958 if (!iwl_is_ready_rf(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07006959 IWL_DEBUG_MAC80211("leave - RF not ready\n");
6960 return -EIO;
6961 }
6962
6963 if (queue >= AC_NUM) {
6964 IWL_DEBUG_MAC80211("leave - queue >= AC_NUM %d\n", queue);
6965 return 0;
6966 }
6967
Zhu Yib481de92007-09-25 17:54:57 -07006968 if (!priv->qos_data.qos_enable) {
6969 priv->qos_data.qos_active = 0;
6970 IWL_DEBUG_MAC80211("leave - qos not enabled\n");
6971 return 0;
6972 }
6973 q = AC_NUM - 1 - queue;
6974
6975 spin_lock_irqsave(&priv->lock, flags);
6976
6977 priv->qos_data.def_qos_parm.ac[q].cw_min = cpu_to_le16(params->cw_min);
6978 priv->qos_data.def_qos_parm.ac[q].cw_max = cpu_to_le16(params->cw_max);
6979 priv->qos_data.def_qos_parm.ac[q].aifsn = params->aifs;
6980 priv->qos_data.def_qos_parm.ac[q].edca_txop =
Johannes Berg3330d7b2008-02-10 16:49:38 +01006981 cpu_to_le16((params->txop * 32));
Zhu Yib481de92007-09-25 17:54:57 -07006982
6983 priv->qos_data.def_qos_parm.ac[q].reserved1 = 0;
6984 priv->qos_data.qos_active = 1;
6985
6986 spin_unlock_irqrestore(&priv->lock, flags);
6987
6988 mutex_lock(&priv->mutex);
6989 if (priv->iw_mode == IEEE80211_IF_TYPE_AP)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006990 iwl4965_activate_qos(priv, 1);
Tomas Winkler3109ece2008-03-28 16:33:35 -07006991 else if (priv->assoc_id && iwl_is_associated(priv))
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006992 iwl4965_activate_qos(priv, 0);
Zhu Yib481de92007-09-25 17:54:57 -07006993
6994 mutex_unlock(&priv->mutex);
6995
Zhu Yib481de92007-09-25 17:54:57 -07006996 IWL_DEBUG_MAC80211("leave\n");
6997 return 0;
6998}
6999
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007000static int iwl4965_mac_get_tx_stats(struct ieee80211_hw *hw,
Zhu Yib481de92007-09-25 17:54:57 -07007001 struct ieee80211_tx_queue_stats *stats)
7002{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007003 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07007004 int i, avail;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007005 struct iwl4965_tx_queue *txq;
7006 struct iwl4965_queue *q;
Zhu Yib481de92007-09-25 17:54:57 -07007007 unsigned long flags;
7008
7009 IWL_DEBUG_MAC80211("enter\n");
7010
Tomas Winklerfee12472008-04-03 16:05:21 -07007011 if (!iwl_is_ready_rf(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07007012 IWL_DEBUG_MAC80211("leave - RF not ready\n");
7013 return -EIO;
7014 }
7015
7016 spin_lock_irqsave(&priv->lock, flags);
7017
7018 for (i = 0; i < AC_NUM; i++) {
7019 txq = &priv->txq[i];
7020 q = &txq->q;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007021 avail = iwl4965_queue_space(q);
Zhu Yib481de92007-09-25 17:54:57 -07007022
7023 stats->data[i].len = q->n_window - avail;
7024 stats->data[i].limit = q->n_window - q->high_mark;
7025 stats->data[i].count = q->n_window;
7026
7027 }
7028 spin_unlock_irqrestore(&priv->lock, flags);
7029
7030 IWL_DEBUG_MAC80211("leave\n");
7031
7032 return 0;
7033}
7034
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007035static int iwl4965_mac_get_stats(struct ieee80211_hw *hw,
Zhu Yib481de92007-09-25 17:54:57 -07007036 struct ieee80211_low_level_stats *stats)
7037{
7038 IWL_DEBUG_MAC80211("enter\n");
7039 IWL_DEBUG_MAC80211("leave\n");
7040
7041 return 0;
7042}
7043
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007044static u64 iwl4965_mac_get_tsf(struct ieee80211_hw *hw)
Zhu Yib481de92007-09-25 17:54:57 -07007045{
7046 IWL_DEBUG_MAC80211("enter\n");
7047 IWL_DEBUG_MAC80211("leave\n");
7048
7049 return 0;
7050}
7051
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007052static void iwl4965_mac_reset_tsf(struct ieee80211_hw *hw)
Zhu Yib481de92007-09-25 17:54:57 -07007053{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007054 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07007055 unsigned long flags;
7056
7057 mutex_lock(&priv->mutex);
7058 IWL_DEBUG_MAC80211("enter\n");
7059
7060 priv->lq_mngr.lq_ready = 0;
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08007061#ifdef CONFIG_IWL4965_HT
Zhu Yib481de92007-09-25 17:54:57 -07007062 spin_lock_irqsave(&priv->lock, flags);
Ron Rindjunskyfd105e72007-11-26 16:14:39 +02007063 memset(&priv->current_ht_config, 0, sizeof(struct iwl_ht_info));
Zhu Yib481de92007-09-25 17:54:57 -07007064 spin_unlock_irqrestore(&priv->lock, flags);
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08007065#endif /* CONFIG_IWL4965_HT */
Zhu Yib481de92007-09-25 17:54:57 -07007066
Assaf Kraussbf85ea42008-03-14 10:38:49 -07007067 iwlcore_reset_qos(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007068
7069 cancel_delayed_work(&priv->post_associate);
7070
7071 spin_lock_irqsave(&priv->lock, flags);
7072 priv->assoc_id = 0;
7073 priv->assoc_capability = 0;
Zhu Yib481de92007-09-25 17:54:57 -07007074 priv->assoc_station_added = 0;
7075
7076 /* new association get rid of ibss beacon skb */
7077 if (priv->ibss_beacon)
7078 dev_kfree_skb(priv->ibss_beacon);
7079
7080 priv->ibss_beacon = NULL;
7081
7082 priv->beacon_int = priv->hw->conf.beacon_int;
Tomas Winkler3109ece2008-03-28 16:33:35 -07007083 priv->timestamp = 0;
Zhu Yib481de92007-09-25 17:54:57 -07007084 if ((priv->iw_mode == IEEE80211_IF_TYPE_STA))
7085 priv->beacon_int = 0;
7086
7087 spin_unlock_irqrestore(&priv->lock, flags);
7088
Tomas Winklerfee12472008-04-03 16:05:21 -07007089 if (!iwl_is_ready_rf(priv)) {
Mohamed Abbasfde35712007-11-29 11:10:15 +08007090 IWL_DEBUG_MAC80211("leave - not ready\n");
7091 mutex_unlock(&priv->mutex);
7092 return;
7093 }
7094
mabbas052c4b92007-10-25 17:15:43 +08007095 /* we are restarting association process
7096 * clear RXON_FILTER_ASSOC_MSK bit
7097 */
7098 if (priv->iw_mode != IEEE80211_IF_TYPE_AP) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007099 iwl4965_scan_cancel_timeout(priv, 100);
mabbas052c4b92007-10-25 17:15:43 +08007100 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007101 iwl4965_commit_rxon(priv);
mabbas052c4b92007-10-25 17:15:43 +08007102 }
7103
Zhu Yib481de92007-09-25 17:54:57 -07007104 /* Per mac80211.h: This is only used in IBSS mode... */
7105 if (priv->iw_mode != IEEE80211_IF_TYPE_IBSS) {
mabbas052c4b92007-10-25 17:15:43 +08007106
Zhu Yib481de92007-09-25 17:54:57 -07007107 IWL_DEBUG_MAC80211("leave - not in IBSS\n");
7108 mutex_unlock(&priv->mutex);
7109 return;
7110 }
7111
Zhu Yib481de92007-09-25 17:54:57 -07007112 priv->only_active_channel = 0;
7113
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007114 iwl4965_set_rate(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007115
7116 mutex_unlock(&priv->mutex);
7117
7118 IWL_DEBUG_MAC80211("leave\n");
Zhu Yib481de92007-09-25 17:54:57 -07007119}
7120
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007121static int iwl4965_mac_beacon_update(struct ieee80211_hw *hw, struct sk_buff *skb,
Zhu Yib481de92007-09-25 17:54:57 -07007122 struct ieee80211_tx_control *control)
7123{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007124 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07007125 unsigned long flags;
7126
7127 mutex_lock(&priv->mutex);
7128 IWL_DEBUG_MAC80211("enter\n");
7129
Tomas Winklerfee12472008-04-03 16:05:21 -07007130 if (!iwl_is_ready_rf(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07007131 IWL_DEBUG_MAC80211("leave - RF not ready\n");
7132 mutex_unlock(&priv->mutex);
7133 return -EIO;
7134 }
7135
7136 if (priv->iw_mode != IEEE80211_IF_TYPE_IBSS) {
7137 IWL_DEBUG_MAC80211("leave - not IBSS\n");
7138 mutex_unlock(&priv->mutex);
7139 return -EIO;
7140 }
7141
7142 spin_lock_irqsave(&priv->lock, flags);
7143
7144 if (priv->ibss_beacon)
7145 dev_kfree_skb(priv->ibss_beacon);
7146
7147 priv->ibss_beacon = skb;
7148
7149 priv->assoc_id = 0;
7150
7151 IWL_DEBUG_MAC80211("leave\n");
7152 spin_unlock_irqrestore(&priv->lock, flags);
7153
Assaf Kraussbf85ea42008-03-14 10:38:49 -07007154 iwlcore_reset_qos(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007155
7156 queue_work(priv->workqueue, &priv->post_associate.work);
7157
7158 mutex_unlock(&priv->mutex);
7159
7160 return 0;
7161}
7162
Zhu Yib481de92007-09-25 17:54:57 -07007163/*****************************************************************************
7164 *
7165 * sysfs attributes
7166 *
7167 *****************************************************************************/
7168
Tomas Winkler0a6857e2008-03-12 16:58:49 -07007169#ifdef CONFIG_IWLWIFI_DEBUG
Zhu Yib481de92007-09-25 17:54:57 -07007170
7171/*
7172 * The following adds a new attribute to the sysfs representation
7173 * of this device driver (i.e. a new file in /sys/bus/pci/drivers/iwl/)
7174 * used for controlling the debug level.
7175 *
7176 * See the level definitions in iwl for details.
7177 */
7178
7179static ssize_t show_debug_level(struct device_driver *d, char *buf)
7180{
Tomas Winkler0a6857e2008-03-12 16:58:49 -07007181 return sprintf(buf, "0x%08X\n", iwl_debug_level);
Zhu Yib481de92007-09-25 17:54:57 -07007182}
7183static ssize_t store_debug_level(struct device_driver *d,
7184 const char *buf, size_t count)
7185{
7186 char *p = (char *)buf;
7187 u32 val;
7188
7189 val = simple_strtoul(p, &p, 0);
7190 if (p == buf)
7191 printk(KERN_INFO DRV_NAME
7192 ": %s is not in hex or decimal form.\n", buf);
7193 else
Tomas Winkler0a6857e2008-03-12 16:58:49 -07007194 iwl_debug_level = val;
Zhu Yib481de92007-09-25 17:54:57 -07007195
7196 return strnlen(buf, count);
7197}
7198
7199static DRIVER_ATTR(debug_level, S_IWUSR | S_IRUGO,
7200 show_debug_level, store_debug_level);
7201
Tomas Winkler0a6857e2008-03-12 16:58:49 -07007202#endif /* CONFIG_IWLWIFI_DEBUG */
Zhu Yib481de92007-09-25 17:54:57 -07007203
Zhu Yib481de92007-09-25 17:54:57 -07007204
7205static ssize_t show_temperature(struct device *d,
7206 struct device_attribute *attr, char *buf)
7207{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007208 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07007209
Tomas Winklerfee12472008-04-03 16:05:21 -07007210 if (!iwl_is_alive(priv))
Zhu Yib481de92007-09-25 17:54:57 -07007211 return -EAGAIN;
7212
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007213 return sprintf(buf, "%d\n", iwl4965_hw_get_temperature(priv));
Zhu Yib481de92007-09-25 17:54:57 -07007214}
7215
7216static DEVICE_ATTR(temperature, S_IRUGO, show_temperature, NULL);
7217
7218static ssize_t show_rs_window(struct device *d,
7219 struct device_attribute *attr,
7220 char *buf)
7221{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007222 struct iwl_priv *priv = d->driver_data;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007223 return iwl4965_fill_rs_info(priv->hw, buf, IWL_AP_ID);
Zhu Yib481de92007-09-25 17:54:57 -07007224}
7225static DEVICE_ATTR(rs_window, S_IRUGO, show_rs_window, NULL);
7226
7227static ssize_t show_tx_power(struct device *d,
7228 struct device_attribute *attr, char *buf)
7229{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007230 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07007231 return sprintf(buf, "%d\n", priv->user_txpower_limit);
7232}
7233
7234static ssize_t store_tx_power(struct device *d,
7235 struct device_attribute *attr,
7236 const char *buf, size_t count)
7237{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007238 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07007239 char *p = (char *)buf;
7240 u32 val;
7241
7242 val = simple_strtoul(p, &p, 10);
7243 if (p == buf)
7244 printk(KERN_INFO DRV_NAME
7245 ": %s is not in decimal form.\n", buf);
7246 else
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007247 iwl4965_hw_reg_set_txpower(priv, val);
Zhu Yib481de92007-09-25 17:54:57 -07007248
7249 return count;
7250}
7251
7252static DEVICE_ATTR(tx_power, S_IWUSR | S_IRUGO, show_tx_power, store_tx_power);
7253
7254static ssize_t show_flags(struct device *d,
7255 struct device_attribute *attr, char *buf)
7256{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007257 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07007258
7259 return sprintf(buf, "0x%04X\n", priv->active_rxon.flags);
7260}
7261
7262static ssize_t store_flags(struct device *d,
7263 struct device_attribute *attr,
7264 const char *buf, size_t count)
7265{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007266 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07007267 u32 flags = simple_strtoul(buf, NULL, 0);
7268
7269 mutex_lock(&priv->mutex);
7270 if (le32_to_cpu(priv->staging_rxon.flags) != flags) {
7271 /* Cancel any currently running scans... */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007272 if (iwl4965_scan_cancel_timeout(priv, 100))
Zhu Yib481de92007-09-25 17:54:57 -07007273 IWL_WARNING("Could not cancel scan.\n");
7274 else {
7275 IWL_DEBUG_INFO("Committing rxon.flags = 0x%04X\n",
7276 flags);
7277 priv->staging_rxon.flags = cpu_to_le32(flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007278 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007279 }
7280 }
7281 mutex_unlock(&priv->mutex);
7282
7283 return count;
7284}
7285
7286static DEVICE_ATTR(flags, S_IWUSR | S_IRUGO, show_flags, store_flags);
7287
7288static ssize_t show_filter_flags(struct device *d,
7289 struct device_attribute *attr, char *buf)
7290{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007291 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07007292
7293 return sprintf(buf, "0x%04X\n",
7294 le32_to_cpu(priv->active_rxon.filter_flags));
7295}
7296
7297static ssize_t store_filter_flags(struct device *d,
7298 struct device_attribute *attr,
7299 const char *buf, size_t count)
7300{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007301 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07007302 u32 filter_flags = simple_strtoul(buf, NULL, 0);
7303
7304 mutex_lock(&priv->mutex);
7305 if (le32_to_cpu(priv->staging_rxon.filter_flags) != filter_flags) {
7306 /* Cancel any currently running scans... */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007307 if (iwl4965_scan_cancel_timeout(priv, 100))
Zhu Yib481de92007-09-25 17:54:57 -07007308 IWL_WARNING("Could not cancel scan.\n");
7309 else {
7310 IWL_DEBUG_INFO("Committing rxon.filter_flags = "
7311 "0x%04X\n", filter_flags);
7312 priv->staging_rxon.filter_flags =
7313 cpu_to_le32(filter_flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007314 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007315 }
7316 }
7317 mutex_unlock(&priv->mutex);
7318
7319 return count;
7320}
7321
7322static DEVICE_ATTR(filter_flags, S_IWUSR | S_IRUGO, show_filter_flags,
7323 store_filter_flags);
7324
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08007325#ifdef CONFIG_IWL4965_SPECTRUM_MEASUREMENT
Zhu Yib481de92007-09-25 17:54:57 -07007326
7327static ssize_t show_measurement(struct device *d,
7328 struct device_attribute *attr, char *buf)
7329{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007330 struct iwl_priv *priv = dev_get_drvdata(d);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007331 struct iwl4965_spectrum_notification measure_report;
Zhu Yib481de92007-09-25 17:54:57 -07007332 u32 size = sizeof(measure_report), len = 0, ofs = 0;
7333 u8 *data = (u8 *) & measure_report;
7334 unsigned long flags;
7335
7336 spin_lock_irqsave(&priv->lock, flags);
7337 if (!(priv->measurement_status & MEASUREMENT_READY)) {
7338 spin_unlock_irqrestore(&priv->lock, flags);
7339 return 0;
7340 }
7341 memcpy(&measure_report, &priv->measure_report, size);
7342 priv->measurement_status = 0;
7343 spin_unlock_irqrestore(&priv->lock, flags);
7344
7345 while (size && (PAGE_SIZE - len)) {
7346 hex_dump_to_buffer(data + ofs, size, 16, 1, buf + len,
7347 PAGE_SIZE - len, 1);
7348 len = strlen(buf);
7349 if (PAGE_SIZE - len)
7350 buf[len++] = '\n';
7351
7352 ofs += 16;
7353 size -= min(size, 16U);
7354 }
7355
7356 return len;
7357}
7358
7359static ssize_t store_measurement(struct device *d,
7360 struct device_attribute *attr,
7361 const char *buf, size_t count)
7362{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007363 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07007364 struct ieee80211_measurement_params params = {
7365 .channel = le16_to_cpu(priv->active_rxon.channel),
7366 .start_time = cpu_to_le64(priv->last_tsf),
7367 .duration = cpu_to_le16(1),
7368 };
7369 u8 type = IWL_MEASURE_BASIC;
7370 u8 buffer[32];
7371 u8 channel;
7372
7373 if (count) {
7374 char *p = buffer;
7375 strncpy(buffer, buf, min(sizeof(buffer), count));
7376 channel = simple_strtoul(p, NULL, 0);
7377 if (channel)
7378 params.channel = channel;
7379
7380 p = buffer;
7381 while (*p && *p != ' ')
7382 p++;
7383 if (*p)
7384 type = simple_strtoul(p + 1, NULL, 0);
7385 }
7386
7387 IWL_DEBUG_INFO("Invoking measurement of type %d on "
7388 "channel %d (for '%s')\n", type, params.channel, buf);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007389 iwl4965_get_measurement(priv, &params, type);
Zhu Yib481de92007-09-25 17:54:57 -07007390
7391 return count;
7392}
7393
7394static DEVICE_ATTR(measurement, S_IRUSR | S_IWUSR,
7395 show_measurement, store_measurement);
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08007396#endif /* CONFIG_IWL4965_SPECTRUM_MEASUREMENT */
Zhu Yib481de92007-09-25 17:54:57 -07007397
7398static ssize_t store_retry_rate(struct device *d,
7399 struct device_attribute *attr,
7400 const char *buf, size_t count)
7401{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007402 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07007403
7404 priv->retry_rate = simple_strtoul(buf, NULL, 0);
7405 if (priv->retry_rate <= 0)
7406 priv->retry_rate = 1;
7407
7408 return count;
7409}
7410
7411static ssize_t show_retry_rate(struct device *d,
7412 struct device_attribute *attr, char *buf)
7413{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007414 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07007415 return sprintf(buf, "%d", priv->retry_rate);
7416}
7417
7418static DEVICE_ATTR(retry_rate, S_IWUSR | S_IRUSR, show_retry_rate,
7419 store_retry_rate);
7420
7421static ssize_t store_power_level(struct device *d,
7422 struct device_attribute *attr,
7423 const char *buf, size_t count)
7424{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007425 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07007426 int rc;
7427 int mode;
7428
7429 mode = simple_strtoul(buf, NULL, 0);
7430 mutex_lock(&priv->mutex);
7431
Tomas Winklerfee12472008-04-03 16:05:21 -07007432 if (!iwl_is_ready(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07007433 rc = -EAGAIN;
7434 goto out;
7435 }
7436
7437 if ((mode < 1) || (mode > IWL_POWER_LIMIT) || (mode == IWL_POWER_AC))
7438 mode = IWL_POWER_AC;
7439 else
7440 mode |= IWL_POWER_ENABLED;
7441
7442 if (mode != priv->power_mode) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007443 rc = iwl4965_send_power_mode(priv, IWL_POWER_LEVEL(mode));
Zhu Yib481de92007-09-25 17:54:57 -07007444 if (rc) {
7445 IWL_DEBUG_MAC80211("failed setting power mode.\n");
7446 goto out;
7447 }
7448 priv->power_mode = mode;
7449 }
7450
7451 rc = count;
7452
7453 out:
7454 mutex_unlock(&priv->mutex);
7455 return rc;
7456}
7457
7458#define MAX_WX_STRING 80
7459
7460/* Values are in microsecond */
7461static const s32 timeout_duration[] = {
7462 350000,
7463 250000,
7464 75000,
7465 37000,
7466 25000,
7467};
7468static const s32 period_duration[] = {
7469 400000,
7470 700000,
7471 1000000,
7472 1000000,
7473 1000000
7474};
7475
7476static ssize_t show_power_level(struct device *d,
7477 struct device_attribute *attr, char *buf)
7478{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007479 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07007480 int level = IWL_POWER_LEVEL(priv->power_mode);
7481 char *p = buf;
7482
7483 p += sprintf(p, "%d ", level);
7484 switch (level) {
7485 case IWL_POWER_MODE_CAM:
7486 case IWL_POWER_AC:
7487 p += sprintf(p, "(AC)");
7488 break;
7489 case IWL_POWER_BATTERY:
7490 p += sprintf(p, "(BATTERY)");
7491 break;
7492 default:
7493 p += sprintf(p,
7494 "(Timeout %dms, Period %dms)",
7495 timeout_duration[level - 1] / 1000,
7496 period_duration[level - 1] / 1000);
7497 }
7498
7499 if (!(priv->power_mode & IWL_POWER_ENABLED))
7500 p += sprintf(p, " OFF\n");
7501 else
7502 p += sprintf(p, " \n");
7503
7504 return (p - buf + 1);
7505
7506}
7507
7508static DEVICE_ATTR(power_level, S_IWUSR | S_IRUSR, show_power_level,
7509 store_power_level);
7510
7511static ssize_t show_channels(struct device *d,
7512 struct device_attribute *attr, char *buf)
7513{
Johannes Berg8318d782008-01-24 19:38:38 +01007514 /* all this shit doesn't belong into sysfs anyway */
7515 return 0;
Zhu Yib481de92007-09-25 17:54:57 -07007516}
7517
7518static DEVICE_ATTR(channels, S_IRUSR, show_channels, NULL);
7519
7520static ssize_t show_statistics(struct device *d,
7521 struct device_attribute *attr, char *buf)
7522{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007523 struct iwl_priv *priv = dev_get_drvdata(d);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007524 u32 size = sizeof(struct iwl4965_notif_statistics);
Zhu Yib481de92007-09-25 17:54:57 -07007525 u32 len = 0, ofs = 0;
7526 u8 *data = (u8 *) & priv->statistics;
7527 int rc = 0;
7528
Tomas Winklerfee12472008-04-03 16:05:21 -07007529 if (!iwl_is_alive(priv))
Zhu Yib481de92007-09-25 17:54:57 -07007530 return -EAGAIN;
7531
7532 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007533 rc = iwl4965_send_statistics_request(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007534 mutex_unlock(&priv->mutex);
7535
7536 if (rc) {
7537 len = sprintf(buf,
7538 "Error sending statistics request: 0x%08X\n", rc);
7539 return len;
7540 }
7541
7542 while (size && (PAGE_SIZE - len)) {
7543 hex_dump_to_buffer(data + ofs, size, 16, 1, buf + len,
7544 PAGE_SIZE - len, 1);
7545 len = strlen(buf);
7546 if (PAGE_SIZE - len)
7547 buf[len++] = '\n';
7548
7549 ofs += 16;
7550 size -= min(size, 16U);
7551 }
7552
7553 return len;
7554}
7555
7556static DEVICE_ATTR(statistics, S_IRUGO, show_statistics, NULL);
7557
7558static ssize_t show_antenna(struct device *d,
7559 struct device_attribute *attr, char *buf)
7560{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007561 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07007562
Tomas Winklerfee12472008-04-03 16:05:21 -07007563 if (!iwl_is_alive(priv))
Zhu Yib481de92007-09-25 17:54:57 -07007564 return -EAGAIN;
7565
7566 return sprintf(buf, "%d\n", priv->antenna);
7567}
7568
7569static ssize_t store_antenna(struct device *d,
7570 struct device_attribute *attr,
7571 const char *buf, size_t count)
7572{
7573 int ant;
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007574 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07007575
7576 if (count == 0)
7577 return 0;
7578
7579 if (sscanf(buf, "%1i", &ant) != 1) {
7580 IWL_DEBUG_INFO("not in hex or decimal form.\n");
7581 return count;
7582 }
7583
7584 if ((ant >= 0) && (ant <= 2)) {
7585 IWL_DEBUG_INFO("Setting antenna select to %d.\n", ant);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007586 priv->antenna = (enum iwl4965_antenna)ant;
Zhu Yib481de92007-09-25 17:54:57 -07007587 } else
7588 IWL_DEBUG_INFO("Bad antenna select value %d.\n", ant);
7589
7590
7591 return count;
7592}
7593
7594static DEVICE_ATTR(antenna, S_IWUSR | S_IRUGO, show_antenna, store_antenna);
7595
7596static ssize_t show_status(struct device *d,
7597 struct device_attribute *attr, char *buf)
7598{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007599 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Tomas Winklerfee12472008-04-03 16:05:21 -07007600 if (!iwl_is_alive(priv))
Zhu Yib481de92007-09-25 17:54:57 -07007601 return -EAGAIN;
7602 return sprintf(buf, "0x%08x\n", (int)priv->status);
7603}
7604
7605static DEVICE_ATTR(status, S_IRUGO, show_status, NULL);
7606
7607static ssize_t dump_error_log(struct device *d,
7608 struct device_attribute *attr,
7609 const char *buf, size_t count)
7610{
7611 char *p = (char *)buf;
7612
7613 if (p[0] == '1')
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007614 iwl4965_dump_nic_error_log((struct iwl_priv *)d->driver_data);
Zhu Yib481de92007-09-25 17:54:57 -07007615
7616 return strnlen(buf, count);
7617}
7618
7619static DEVICE_ATTR(dump_errors, S_IWUSR, NULL, dump_error_log);
7620
7621static ssize_t dump_event_log(struct device *d,
7622 struct device_attribute *attr,
7623 const char *buf, size_t count)
7624{
7625 char *p = (char *)buf;
7626
7627 if (p[0] == '1')
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007628 iwl4965_dump_nic_event_log((struct iwl_priv *)d->driver_data);
Zhu Yib481de92007-09-25 17:54:57 -07007629
7630 return strnlen(buf, count);
7631}
7632
7633static DEVICE_ATTR(dump_events, S_IWUSR, NULL, dump_event_log);
7634
7635/*****************************************************************************
7636 *
7637 * driver setup and teardown
7638 *
7639 *****************************************************************************/
7640
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007641static void iwl4965_setup_deferred_work(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07007642{
7643 priv->workqueue = create_workqueue(DRV_NAME);
7644
7645 init_waitqueue_head(&priv->wait_command_queue);
7646
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007647 INIT_WORK(&priv->up, iwl4965_bg_up);
7648 INIT_WORK(&priv->restart, iwl4965_bg_restart);
7649 INIT_WORK(&priv->rx_replenish, iwl4965_bg_rx_replenish);
7650 INIT_WORK(&priv->scan_completed, iwl4965_bg_scan_completed);
7651 INIT_WORK(&priv->request_scan, iwl4965_bg_request_scan);
7652 INIT_WORK(&priv->abort_scan, iwl4965_bg_abort_scan);
7653 INIT_WORK(&priv->rf_kill, iwl4965_bg_rf_kill);
7654 INIT_WORK(&priv->beacon_update, iwl4965_bg_beacon_update);
7655 INIT_DELAYED_WORK(&priv->post_associate, iwl4965_bg_post_associate);
7656 INIT_DELAYED_WORK(&priv->init_alive_start, iwl4965_bg_init_alive_start);
7657 INIT_DELAYED_WORK(&priv->alive_start, iwl4965_bg_alive_start);
7658 INIT_DELAYED_WORK(&priv->scan_check, iwl4965_bg_scan_check);
Zhu Yib481de92007-09-25 17:54:57 -07007659
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007660 iwl4965_hw_setup_deferred_work(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007661
7662 tasklet_init(&priv->irq_tasklet, (void (*)(unsigned long))
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007663 iwl4965_irq_tasklet, (unsigned long)priv);
Zhu Yib481de92007-09-25 17:54:57 -07007664}
7665
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007666static void iwl4965_cancel_deferred_work(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07007667{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007668 iwl4965_hw_cancel_deferred_work(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007669
Joonwoo Park3ae6a052007-11-29 10:43:16 +09007670 cancel_delayed_work_sync(&priv->init_alive_start);
Zhu Yib481de92007-09-25 17:54:57 -07007671 cancel_delayed_work(&priv->scan_check);
7672 cancel_delayed_work(&priv->alive_start);
7673 cancel_delayed_work(&priv->post_associate);
7674 cancel_work_sync(&priv->beacon_update);
7675}
7676
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007677static struct attribute *iwl4965_sysfs_entries[] = {
Zhu Yib481de92007-09-25 17:54:57 -07007678 &dev_attr_antenna.attr,
7679 &dev_attr_channels.attr,
7680 &dev_attr_dump_errors.attr,
7681 &dev_attr_dump_events.attr,
7682 &dev_attr_flags.attr,
7683 &dev_attr_filter_flags.attr,
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08007684#ifdef CONFIG_IWL4965_SPECTRUM_MEASUREMENT
Zhu Yib481de92007-09-25 17:54:57 -07007685 &dev_attr_measurement.attr,
7686#endif
7687 &dev_attr_power_level.attr,
7688 &dev_attr_retry_rate.attr,
Zhu Yib481de92007-09-25 17:54:57 -07007689 &dev_attr_rs_window.attr,
7690 &dev_attr_statistics.attr,
7691 &dev_attr_status.attr,
7692 &dev_attr_temperature.attr,
Zhu Yib481de92007-09-25 17:54:57 -07007693 &dev_attr_tx_power.attr,
7694
7695 NULL
7696};
7697
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007698static struct attribute_group iwl4965_attribute_group = {
Zhu Yib481de92007-09-25 17:54:57 -07007699 .name = NULL, /* put in device directory */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007700 .attrs = iwl4965_sysfs_entries,
Zhu Yib481de92007-09-25 17:54:57 -07007701};
7702
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007703static struct ieee80211_ops iwl4965_hw_ops = {
7704 .tx = iwl4965_mac_tx,
7705 .start = iwl4965_mac_start,
7706 .stop = iwl4965_mac_stop,
7707 .add_interface = iwl4965_mac_add_interface,
7708 .remove_interface = iwl4965_mac_remove_interface,
7709 .config = iwl4965_mac_config,
7710 .config_interface = iwl4965_mac_config_interface,
7711 .configure_filter = iwl4965_configure_filter,
7712 .set_key = iwl4965_mac_set_key,
Emmanuel Grumbachab885f82008-03-20 15:06:43 +02007713 .update_tkip_key = iwl4965_mac_update_tkip_key,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007714 .get_stats = iwl4965_mac_get_stats,
7715 .get_tx_stats = iwl4965_mac_get_tx_stats,
7716 .conf_tx = iwl4965_mac_conf_tx,
7717 .get_tsf = iwl4965_mac_get_tsf,
7718 .reset_tsf = iwl4965_mac_reset_tsf,
7719 .beacon_update = iwl4965_mac_beacon_update,
Johannes Berg471b3ef2007-12-28 14:32:58 +01007720 .bss_info_changed = iwl4965_bss_info_changed,
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08007721#ifdef CONFIG_IWL4965_HT
Ron Rindjunsky9ab46172007-12-25 17:00:38 +02007722 .ampdu_action = iwl4965_mac_ampdu_action,
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08007723#endif /* CONFIG_IWL4965_HT */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007724 .hw_scan = iwl4965_mac_hw_scan
Zhu Yib481de92007-09-25 17:54:57 -07007725};
7726
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007727static int iwl4965_pci_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
Zhu Yib481de92007-09-25 17:54:57 -07007728{
7729 int err = 0;
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007730 struct iwl_priv *priv;
Zhu Yib481de92007-09-25 17:54:57 -07007731 struct ieee80211_hw *hw;
Tomas Winkler82b9a122008-03-04 18:09:30 -08007732 struct iwl_cfg *cfg = (struct iwl_cfg *)(ent->driver_data);
Mohamed Abbas0359fac2008-03-28 16:21:08 -07007733 unsigned long flags;
Zhu Yi5a66926a2008-01-14 17:46:18 -08007734 DECLARE_MAC_BUF(mac);
Zhu Yib481de92007-09-25 17:54:57 -07007735
Assaf Krauss316c30d2008-03-14 10:38:46 -07007736 /************************
7737 * 1. Allocating HW data
7738 ************************/
7739
Cahill, Ben M6440adb2007-11-29 11:09:55 +08007740 /* Disabling hardware scan means that mac80211 will perform scans
7741 * "the hard way", rather than using device's scan. */
Assaf Krauss1ea87392008-03-18 14:57:50 -07007742 if (cfg->mod_params->disable_hw_scan) {
Zhu Yib481de92007-09-25 17:54:57 -07007743 IWL_DEBUG_INFO("Disabling hw_scan\n");
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007744 iwl4965_hw_ops.hw_scan = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07007745 }
7746
Assaf Krauss1d0a0822008-03-14 10:38:48 -07007747 hw = iwl_alloc_all(cfg, &iwl4965_hw_ops);
7748 if (!hw) {
Zhu Yib481de92007-09-25 17:54:57 -07007749 err = -ENOMEM;
7750 goto out;
7751 }
Assaf Krauss1d0a0822008-03-14 10:38:48 -07007752 priv = hw->priv;
7753 /* At this point both hw and priv are allocated. */
7754
Zhu Yib481de92007-09-25 17:54:57 -07007755 SET_IEEE80211_DEV(hw, &pdev->dev);
7756
7757 IWL_DEBUG_INFO("*** LOAD DRIVER ***\n");
Tomas Winkler82b9a122008-03-04 18:09:30 -08007758 priv->cfg = cfg;
Zhu Yib481de92007-09-25 17:54:57 -07007759 priv->pci_dev = pdev;
Assaf Krauss316c30d2008-03-14 10:38:46 -07007760
Tomas Winkler0a6857e2008-03-12 16:58:49 -07007761#ifdef CONFIG_IWLWIFI_DEBUG
Assaf Krauss1ea87392008-03-18 14:57:50 -07007762 iwl_debug_level = priv->cfg->mod_params->debug;
Zhu Yib481de92007-09-25 17:54:57 -07007763 atomic_set(&priv->restrict_refcnt, 0);
7764#endif
Zhu Yib481de92007-09-25 17:54:57 -07007765
Assaf Krauss316c30d2008-03-14 10:38:46 -07007766 /**************************
7767 * 2. Initializing PCI bus
7768 **************************/
7769 if (pci_enable_device(pdev)) {
7770 err = -ENODEV;
7771 goto out_ieee80211_free_hw;
7772 }
7773
7774 pci_set_master(pdev);
7775
7776 err = pci_set_dma_mask(pdev, DMA_32BIT_MASK);
7777 if (!err)
7778 err = pci_set_consistent_dma_mask(pdev, DMA_32BIT_MASK);
7779 if (err) {
7780 printk(KERN_WARNING DRV_NAME
7781 ": No suitable DMA available.\n");
7782 goto out_pci_disable_device;
7783 }
7784
7785 err = pci_request_regions(pdev, DRV_NAME);
7786 if (err)
7787 goto out_pci_disable_device;
7788
7789 pci_set_drvdata(pdev, priv);
7790
7791 /* We disable the RETRY_TIMEOUT register (0x41) to keep
7792 * PCI Tx retries from interfering with C3 CPU state */
7793 pci_write_config_byte(pdev, 0x41, 0x00);
7794
7795 /***********************
7796 * 3. Read REV register
7797 ***********************/
7798 priv->hw_base = pci_iomap(pdev, 0, 0);
7799 if (!priv->hw_base) {
7800 err = -ENODEV;
7801 goto out_pci_release_regions;
7802 }
7803
7804 IWL_DEBUG_INFO("pci_resource_len = 0x%08llx\n",
7805 (unsigned long long) pci_resource_len(pdev, 0));
7806 IWL_DEBUG_INFO("pci_resource_base = %p\n", priv->hw_base);
7807
7808 printk(KERN_INFO DRV_NAME
7809 ": Detected Intel Wireless WiFi Link %s\n", priv->cfg->name);
7810
7811 /*****************
7812 * 4. Read EEPROM
7813 *****************/
7814 /* nic init */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07007815 iwl_set_bit(priv, CSR_GIO_CHICKEN_BITS,
Assaf Krauss316c30d2008-03-14 10:38:46 -07007816 CSR_GIO_CHICKEN_BITS_REG_BIT_DIS_L0S_EXIT_TIMER);
7817
Tomas Winkler3395f6e2008-03-25 16:33:37 -07007818 iwl_set_bit(priv, CSR_GP_CNTRL, CSR_GP_CNTRL_REG_FLAG_INIT_DONE);
7819 err = iwl_poll_bit(priv, CSR_GP_CNTRL,
Assaf Krauss316c30d2008-03-14 10:38:46 -07007820 CSR_GP_CNTRL_REG_FLAG_MAC_CLOCK_READY,
7821 CSR_GP_CNTRL_REG_FLAG_MAC_CLOCK_READY, 25000);
7822 if (err < 0) {
7823 IWL_DEBUG_INFO("Failed to init the card\n");
7824 goto out_iounmap;
7825 }
7826 /* Read the EEPROM */
7827 err = iwl_eeprom_init(priv);
7828 if (err) {
7829 IWL_ERROR("Unable to init EEPROM\n");
7830 goto out_iounmap;
7831 }
7832 /* MAC Address location in EEPROM same for 3945/4965 */
7833 iwl_eeprom_get_mac(priv, priv->mac_addr);
7834 IWL_DEBUG_INFO("MAC address: %s\n", print_mac(mac, priv->mac_addr));
7835 SET_IEEE80211_PERM_ADDR(priv->hw, priv->mac_addr);
7836
7837 /************************
7838 * 5. Setup HW constants
7839 ************************/
7840 /* Device-specific setup */
7841 if (iwl4965_hw_set_hw_setting(priv)) {
7842 IWL_ERROR("failed to set hw settings\n");
7843 goto out_iounmap;
7844 }
7845
7846 /*******************
7847 * 6. Setup hw/priv
7848 *******************/
Zhu Yib481de92007-09-25 17:54:57 -07007849
Assaf Kraussbf85ea42008-03-14 10:38:49 -07007850 err = iwl_setup(priv);
7851 if (err)
Assaf Krauss316c30d2008-03-14 10:38:46 -07007852 goto out_unset_hw_settings;
Assaf Kraussbf85ea42008-03-14 10:38:49 -07007853 /* At this point both hw and priv are initialized. */
Assaf Krauss316c30d2008-03-14 10:38:46 -07007854
7855 /**********************************
7856 * 7. Initialize module parameters
7857 **********************************/
7858
7859 /* Disable radio (SW RF KILL) via parameter when loading driver */
Assaf Krauss1ea87392008-03-18 14:57:50 -07007860 if (priv->cfg->mod_params->disable) {
Assaf Krauss316c30d2008-03-14 10:38:46 -07007861 set_bit(STATUS_RF_KILL_SW, &priv->status);
7862 IWL_DEBUG_INFO("Radio disabled.\n");
7863 }
7864
Assaf Krauss1ea87392008-03-18 14:57:50 -07007865 if (priv->cfg->mod_params->enable_qos)
Assaf Krauss316c30d2008-03-14 10:38:46 -07007866 priv->qos_data.qos_enable = 1;
7867
7868 /********************
7869 * 8. Setup services
7870 ********************/
Mohamed Abbas0359fac2008-03-28 16:21:08 -07007871 spin_lock_irqsave(&priv->lock, flags);
Assaf Krauss316c30d2008-03-14 10:38:46 -07007872 iwl4965_disable_interrupts(priv);
Mohamed Abbas0359fac2008-03-28 16:21:08 -07007873 spin_unlock_irqrestore(&priv->lock, flags);
Assaf Krauss316c30d2008-03-14 10:38:46 -07007874
7875 err = sysfs_create_group(&pdev->dev.kobj, &iwl4965_attribute_group);
7876 if (err) {
7877 IWL_ERROR("failed to create sysfs device attributes\n");
Assaf Kraussbf85ea42008-03-14 10:38:49 -07007878 goto out_unset_hw_settings;
Assaf Krauss316c30d2008-03-14 10:38:46 -07007879 }
7880
7881 err = iwl_dbgfs_register(priv, DRV_NAME);
7882 if (err) {
7883 IWL_ERROR("failed to create debugfs files\n");
7884 goto out_remove_sysfs;
7885 }
7886
7887 iwl4965_setup_deferred_work(priv);
7888 iwl4965_setup_rx_handlers(priv);
7889
7890 /********************
7891 * 9. Conclude
7892 ********************/
Zhu Yi5a66926a2008-01-14 17:46:18 -08007893 pci_save_state(pdev);
7894 pci_disable_device(pdev);
Zhu Yib481de92007-09-25 17:54:57 -07007895
Mohamed Abbasc8381fd2008-03-28 16:21:05 -07007896 /* notify iwlcore to init */
7897 iwlcore_low_level_notify(priv, IWLCORE_INIT_EVT);
Zhu Yib481de92007-09-25 17:54:57 -07007898 return 0;
7899
Assaf Krauss316c30d2008-03-14 10:38:46 -07007900 out_remove_sysfs:
7901 sysfs_remove_group(&pdev->dev.kobj, &iwl4965_attribute_group);
Assaf Krauss316c30d2008-03-14 10:38:46 -07007902 out_unset_hw_settings:
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007903 iwl4965_unset_hw_setting(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007904 out_iounmap:
7905 pci_iounmap(pdev, priv->hw_base);
7906 out_pci_release_regions:
7907 pci_release_regions(pdev);
Assaf Krauss316c30d2008-03-14 10:38:46 -07007908 pci_set_drvdata(pdev, NULL);
Zhu Yib481de92007-09-25 17:54:57 -07007909 out_pci_disable_device:
7910 pci_disable_device(pdev);
Zhu Yib481de92007-09-25 17:54:57 -07007911 out_ieee80211_free_hw:
7912 ieee80211_free_hw(priv->hw);
7913 out:
7914 return err;
7915}
7916
Reinette Chatrec83dbf62008-03-21 13:53:41 -07007917static void __devexit iwl4965_pci_remove(struct pci_dev *pdev)
Zhu Yib481de92007-09-25 17:54:57 -07007918{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007919 struct iwl_priv *priv = pci_get_drvdata(pdev);
Zhu Yib481de92007-09-25 17:54:57 -07007920 struct list_head *p, *q;
7921 int i;
Mohamed Abbas0359fac2008-03-28 16:21:08 -07007922 unsigned long flags;
Zhu Yib481de92007-09-25 17:54:57 -07007923
7924 if (!priv)
7925 return;
7926
7927 IWL_DEBUG_INFO("*** UNLOAD DRIVER ***\n");
7928
Ron Rindjunskyc4f55232008-03-28 16:21:10 -07007929 if (priv->mac80211_registered) {
7930 ieee80211_unregister_hw(priv->hw);
7931 priv->mac80211_registered = 0;
7932 }
7933
Zhu Yib481de92007-09-25 17:54:57 -07007934 set_bit(STATUS_EXIT_PENDING, &priv->status);
Zhu Yib24d22b2007-12-19 13:59:52 +08007935
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007936 iwl4965_down(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007937
Mohamed Abbas0359fac2008-03-28 16:21:08 -07007938 /* make sure we flush any pending irq or
7939 * tasklet for the driver
7940 */
7941 spin_lock_irqsave(&priv->lock, flags);
7942 iwl4965_disable_interrupts(priv);
7943 spin_unlock_irqrestore(&priv->lock, flags);
7944
7945 iwl_synchronize_irq(priv);
7946
Zhu Yib481de92007-09-25 17:54:57 -07007947 /* Free MAC hash list for ADHOC */
7948 for (i = 0; i < IWL_IBSS_MAC_HASH_SIZE; i++) {
7949 list_for_each_safe(p, q, &priv->ibss_mac_hash[i]) {
7950 list_del(p);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007951 kfree(list_entry(p, struct iwl4965_ibss_seq, list));
Zhu Yib481de92007-09-25 17:54:57 -07007952 }
7953 }
7954
Mohamed Abbasc8381fd2008-03-28 16:21:05 -07007955 iwlcore_low_level_notify(priv, IWLCORE_REMOVE_EVT);
Tomas Winkler712b6cf2008-03-12 16:58:52 -07007956 iwl_dbgfs_unregister(priv);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007957 sysfs_remove_group(&pdev->dev.kobj, &iwl4965_attribute_group);
Zhu Yib481de92007-09-25 17:54:57 -07007958
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007959 iwl4965_dealloc_ucode_pci(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007960
7961 if (priv->rxq.bd)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007962 iwl4965_rx_queue_free(priv, &priv->rxq);
7963 iwl4965_hw_txq_ctx_free(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007964
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007965 iwl4965_unset_hw_setting(priv);
Assaf Kraussbf85ea42008-03-14 10:38:49 -07007966 iwlcore_clear_stations_table(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007967
Zhu Yib481de92007-09-25 17:54:57 -07007968
Mohamed Abbas948c1712007-10-25 17:15:45 +08007969 /*netif_stop_queue(dev); */
7970 flush_workqueue(priv->workqueue);
7971
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007972 /* ieee80211_unregister_hw calls iwl4965_mac_stop, which flushes
Zhu Yib481de92007-09-25 17:54:57 -07007973 * priv->workqueue... so we can't take down the workqueue
7974 * until now... */
7975 destroy_workqueue(priv->workqueue);
7976 priv->workqueue = NULL;
7977
Zhu Yib481de92007-09-25 17:54:57 -07007978 pci_iounmap(pdev, priv->hw_base);
7979 pci_release_regions(pdev);
7980 pci_disable_device(pdev);
7981 pci_set_drvdata(pdev, NULL);
7982
Assaf Kraussbf85ea42008-03-14 10:38:49 -07007983 iwl_free_channel_map(priv);
Reinette Chatre849e0dc2008-01-23 10:15:18 -08007984 iwl4965_free_geos(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007985
7986 if (priv->ibss_beacon)
7987 dev_kfree_skb(priv->ibss_beacon);
7988
7989 ieee80211_free_hw(priv->hw);
7990}
7991
7992#ifdef CONFIG_PM
7993
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007994static int iwl4965_pci_suspend(struct pci_dev *pdev, pm_message_t state)
Zhu Yib481de92007-09-25 17:54:57 -07007995{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007996 struct iwl_priv *priv = pci_get_drvdata(pdev);
Zhu Yib481de92007-09-25 17:54:57 -07007997
Zhu Yie655b9f2008-01-24 02:19:38 -08007998 if (priv->is_open) {
7999 set_bit(STATUS_IN_SUSPEND, &priv->status);
8000 iwl4965_mac_stop(priv->hw);
8001 priv->is_open = 1;
8002 }
Zhu Yib481de92007-09-25 17:54:57 -07008003
Zhu Yib481de92007-09-25 17:54:57 -07008004 pci_set_power_state(pdev, PCI_D3hot);
8005
Zhu Yib481de92007-09-25 17:54:57 -07008006 return 0;
8007}
8008
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008009static int iwl4965_pci_resume(struct pci_dev *pdev)
Zhu Yib481de92007-09-25 17:54:57 -07008010{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07008011 struct iwl_priv *priv = pci_get_drvdata(pdev);
Zhu Yib481de92007-09-25 17:54:57 -07008012
Zhu Yib481de92007-09-25 17:54:57 -07008013 pci_set_power_state(pdev, PCI_D0);
Zhu Yib481de92007-09-25 17:54:57 -07008014
Zhu Yie655b9f2008-01-24 02:19:38 -08008015 if (priv->is_open)
8016 iwl4965_mac_start(priv->hw);
Zhu Yib481de92007-09-25 17:54:57 -07008017
Zhu Yie655b9f2008-01-24 02:19:38 -08008018 clear_bit(STATUS_IN_SUSPEND, &priv->status);
Zhu Yib481de92007-09-25 17:54:57 -07008019 return 0;
8020}
8021
8022#endif /* CONFIG_PM */
8023
8024/*****************************************************************************
8025 *
8026 * driver and module entry point
8027 *
8028 *****************************************************************************/
8029
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008030static struct pci_driver iwl4965_driver = {
Zhu Yib481de92007-09-25 17:54:57 -07008031 .name = DRV_NAME,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008032 .id_table = iwl4965_hw_card_ids,
8033 .probe = iwl4965_pci_probe,
8034 .remove = __devexit_p(iwl4965_pci_remove),
Zhu Yib481de92007-09-25 17:54:57 -07008035#ifdef CONFIG_PM
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008036 .suspend = iwl4965_pci_suspend,
8037 .resume = iwl4965_pci_resume,
Zhu Yib481de92007-09-25 17:54:57 -07008038#endif
8039};
8040
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008041static int __init iwl4965_init(void)
Zhu Yib481de92007-09-25 17:54:57 -07008042{
8043
8044 int ret;
8045 printk(KERN_INFO DRV_NAME ": " DRV_DESCRIPTION ", " DRV_VERSION "\n");
8046 printk(KERN_INFO DRV_NAME ": " DRV_COPYRIGHT "\n");
Reinette Chatre897e1cf2008-03-28 16:21:09 -07008047
8048 ret = iwl4965_rate_control_register();
8049 if (ret) {
8050 IWL_ERROR("Unable to register rate control algorithm: %d\n", ret);
8051 return ret;
8052 }
8053
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008054 ret = pci_register_driver(&iwl4965_driver);
Zhu Yib481de92007-09-25 17:54:57 -07008055 if (ret) {
8056 IWL_ERROR("Unable to initialize PCI module\n");
Reinette Chatre897e1cf2008-03-28 16:21:09 -07008057 goto error_register;
Zhu Yib481de92007-09-25 17:54:57 -07008058 }
Tomas Winkler0a6857e2008-03-12 16:58:49 -07008059#ifdef CONFIG_IWLWIFI_DEBUG
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008060 ret = driver_create_file(&iwl4965_driver.driver, &driver_attr_debug_level);
Zhu Yib481de92007-09-25 17:54:57 -07008061 if (ret) {
8062 IWL_ERROR("Unable to create driver sysfs file\n");
Reinette Chatre897e1cf2008-03-28 16:21:09 -07008063 goto error_debug;
Zhu Yib481de92007-09-25 17:54:57 -07008064 }
8065#endif
8066
8067 return ret;
Reinette Chatre897e1cf2008-03-28 16:21:09 -07008068
8069#ifdef CONFIG_IWLWIFI_DEBUG
8070error_debug:
8071 pci_unregister_driver(&iwl4965_driver);
8072#endif
8073error_register:
8074 iwl4965_rate_control_unregister();
8075 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07008076}
8077
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008078static void __exit iwl4965_exit(void)
Zhu Yib481de92007-09-25 17:54:57 -07008079{
Tomas Winkler0a6857e2008-03-12 16:58:49 -07008080#ifdef CONFIG_IWLWIFI_DEBUG
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008081 driver_remove_file(&iwl4965_driver.driver, &driver_attr_debug_level);
Zhu Yib481de92007-09-25 17:54:57 -07008082#endif
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008083 pci_unregister_driver(&iwl4965_driver);
Reinette Chatre897e1cf2008-03-28 16:21:09 -07008084 iwl4965_rate_control_unregister();
Zhu Yib481de92007-09-25 17:54:57 -07008085}
8086
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008087module_exit(iwl4965_exit);
8088module_init(iwl4965_init);