blob: a04127a8acf7bb79463bd7e3fc82b069f38c381b [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"
Tomas Winkler82b9a122008-03-04 18:09:30 -080049#include "iwl-core.h"
Zhu Yib481de92007-09-25 17:54:57 -070050#include "iwl-4965.h"
51#include "iwl-helpers.h"
52
Tomas Winklerc79dd5b2008-03-12 16:58:50 -070053static int iwl4965_tx_queue_update_write_ptr(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -080054 struct iwl4965_tx_queue *txq);
Christoph Hellwig416e1432007-10-25 17:15:49 +080055
Zhu Yib481de92007-09-25 17:54:57 -070056/******************************************************************************
57 *
58 * module boiler plate
59 *
60 ******************************************************************************/
61
Zhu Yib481de92007-09-25 17:54:57 -070062/*
63 * module name, copyright, version, etc.
64 * NOTE: DRV_NAME is defined in iwlwifi.h for use by iwl-debug.h and printk
65 */
66
67#define DRV_DESCRIPTION "Intel(R) Wireless WiFi Link 4965AGN driver for Linux"
68
Tomas Winkler0a6857e2008-03-12 16:58:49 -070069#ifdef CONFIG_IWLWIFI_DEBUG
Zhu Yib481de92007-09-25 17:54:57 -070070#define VD "d"
71#else
72#define VD
73#endif
74
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +080075#ifdef CONFIG_IWL4965_SPECTRUM_MEASUREMENT
Zhu Yib481de92007-09-25 17:54:57 -070076#define VS "s"
77#else
78#define VS
79#endif
80
Tomas Winklerdf48c322008-03-06 10:40:19 -080081#define DRV_VERSION IWLWIFI_VERSION VD VS
Zhu Yib481de92007-09-25 17:54:57 -070082
Zhu Yib481de92007-09-25 17:54:57 -070083
84MODULE_DESCRIPTION(DRV_DESCRIPTION);
85MODULE_VERSION(DRV_VERSION);
86MODULE_AUTHOR(DRV_COPYRIGHT);
87MODULE_LICENSE("GPL");
88
89__le16 *ieee80211_get_qos_ctrl(struct ieee80211_hdr *hdr)
90{
91 u16 fc = le16_to_cpu(hdr->frame_control);
92 int hdr_len = ieee80211_get_hdrlen(fc);
93
94 if ((fc & 0x00cc) == (IEEE80211_STYPE_QOS_DATA | IEEE80211_FTYPE_DATA))
95 return (__le16 *) ((u8 *) hdr + hdr_len - QOS_CONTROL_LEN);
96 return NULL;
97}
98
Johannes Berg8318d782008-01-24 19:38:38 +010099static const struct ieee80211_supported_band *iwl4965_get_hw_mode(
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700100 struct iwl_priv *priv, enum ieee80211_band band)
Zhu Yib481de92007-09-25 17:54:57 -0700101{
Johannes Berg8318d782008-01-24 19:38:38 +0100102 return priv->hw->wiphy->bands[band];
Zhu Yib481de92007-09-25 17:54:57 -0700103}
104
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800105static int iwl4965_is_empty_essid(const char *essid, int essid_len)
Zhu Yib481de92007-09-25 17:54:57 -0700106{
107 /* Single white space is for Linksys APs */
108 if (essid_len == 1 && essid[0] == ' ')
109 return 1;
110
111 /* Otherwise, if the entire essid is 0, we assume it is hidden */
112 while (essid_len) {
113 essid_len--;
114 if (essid[essid_len] != '\0')
115 return 0;
116 }
117
118 return 1;
119}
120
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800121static const char *iwl4965_escape_essid(const char *essid, u8 essid_len)
Zhu Yib481de92007-09-25 17:54:57 -0700122{
123 static char escaped[IW_ESSID_MAX_SIZE * 2 + 1];
124 const char *s = essid;
125 char *d = escaped;
126
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800127 if (iwl4965_is_empty_essid(essid, essid_len)) {
Zhu Yib481de92007-09-25 17:54:57 -0700128 memcpy(escaped, "<hidden>", sizeof("<hidden>"));
129 return escaped;
130 }
131
132 essid_len = min(essid_len, (u8) IW_ESSID_MAX_SIZE);
133 while (essid_len--) {
134 if (*s == '\0') {
135 *d++ = '\\';
136 *d++ = '0';
137 s++;
138 } else
139 *d++ = *s++;
140 }
141 *d = '\0';
142 return escaped;
143}
144
Zhu Yib481de92007-09-25 17:54:57 -0700145/*************** DMA-QUEUE-GENERAL-FUNCTIONS *****
146 * DMA services
147 *
148 * Theory of operation
149 *
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800150 * A Tx or Rx queue resides in host DRAM, and is comprised of a circular buffer
151 * of buffer descriptors, each of which points to one or more data buffers for
152 * the device to read from or fill. Driver and device exchange status of each
153 * queue via "read" and "write" pointers. Driver keeps minimum of 2 empty
154 * entries in each circular buffer, to protect against confusing empty and full
155 * queue states.
156 *
157 * The device reads or writes the data in the queues via the device's several
158 * DMA/FIFO channels. Each queue is mapped to a single DMA channel.
Zhu Yib481de92007-09-25 17:54:57 -0700159 *
160 * For Tx queue, there are low mark and high mark limits. If, after queuing
161 * the packet for Tx, free space become < low mark, Tx queue stopped. When
162 * reclaiming packets (on 'tx done IRQ), if free space become > high mark,
163 * Tx queue resumed.
164 *
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800165 * The 4965 operates with up to 17 queues: One receive queue, one transmit
166 * queue (#4) for sending commands to the device firmware, and 15 other
167 * Tx queues that may be mapped to prioritized Tx DMA/FIFO channels.
Ben Cahille3851442007-11-29 11:10:07 +0800168 *
169 * See more detailed info in iwl-4965-hw.h.
Zhu Yib481de92007-09-25 17:54:57 -0700170 ***************************************************/
171
Ron Rindjunskyfe01b472008-01-28 14:07:24 +0200172int iwl4965_queue_space(const struct iwl4965_queue *q)
Zhu Yib481de92007-09-25 17:54:57 -0700173{
Tomas Winklerfc4b6852007-10-25 17:15:24 +0800174 int s = q->read_ptr - q->write_ptr;
Zhu Yib481de92007-09-25 17:54:57 -0700175
Tomas Winklerfc4b6852007-10-25 17:15:24 +0800176 if (q->read_ptr > q->write_ptr)
Zhu Yib481de92007-09-25 17:54:57 -0700177 s -= q->n_bd;
178
179 if (s <= 0)
180 s += q->n_window;
181 /* keep some reserve to not confuse empty and full situations */
182 s -= 2;
183 if (s < 0)
184 s = 0;
185 return s;
186}
187
Zhu Yib481de92007-09-25 17:54:57 -0700188
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800189static inline int x2_queue_used(const struct iwl4965_queue *q, int i)
Zhu Yib481de92007-09-25 17:54:57 -0700190{
Tomas Winklerfc4b6852007-10-25 17:15:24 +0800191 return q->write_ptr > q->read_ptr ?
192 (i >= q->read_ptr && i < q->write_ptr) :
193 !(i < q->read_ptr && i >= q->write_ptr);
Zhu Yib481de92007-09-25 17:54:57 -0700194}
195
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800196static inline u8 get_cmd_index(struct iwl4965_queue *q, u32 index, int is_huge)
Zhu Yib481de92007-09-25 17:54:57 -0700197{
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800198 /* This is for scan command, the big buffer at end of command array */
Zhu Yib481de92007-09-25 17:54:57 -0700199 if (is_huge)
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800200 return q->n_window; /* must be power of 2 */
Zhu Yib481de92007-09-25 17:54:57 -0700201
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800202 /* Otherwise, use normal size buffers */
Zhu Yib481de92007-09-25 17:54:57 -0700203 return index & (q->n_window - 1);
204}
205
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800206/**
207 * iwl4965_queue_init - Initialize queue's high/low-water and read/write indexes
208 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700209static int iwl4965_queue_init(struct iwl_priv *priv, struct iwl4965_queue *q,
Zhu Yib481de92007-09-25 17:54:57 -0700210 int count, int slots_num, u32 id)
211{
212 q->n_bd = count;
213 q->n_window = slots_num;
214 q->id = id;
215
Tomas Winklerc54b6792008-03-06 17:36:53 -0800216 /* count must be power-of-two size, otherwise iwl_queue_inc_wrap
217 * and iwl_queue_dec_wrap are broken. */
Zhu Yib481de92007-09-25 17:54:57 -0700218 BUG_ON(!is_power_of_2(count));
219
220 /* slots_num must be power-of-two size, otherwise
221 * get_cmd_index is broken. */
222 BUG_ON(!is_power_of_2(slots_num));
223
224 q->low_mark = q->n_window / 4;
225 if (q->low_mark < 4)
226 q->low_mark = 4;
227
228 q->high_mark = q->n_window / 8;
229 if (q->high_mark < 2)
230 q->high_mark = 2;
231
Tomas Winklerfc4b6852007-10-25 17:15:24 +0800232 q->write_ptr = q->read_ptr = 0;
Zhu Yib481de92007-09-25 17:54:57 -0700233
234 return 0;
235}
236
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800237/**
238 * iwl4965_tx_queue_alloc - Alloc driver data and TFD CB for one Tx/cmd queue
239 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700240static int iwl4965_tx_queue_alloc(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800241 struct iwl4965_tx_queue *txq, u32 id)
Zhu Yib481de92007-09-25 17:54:57 -0700242{
243 struct pci_dev *dev = priv->pci_dev;
244
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800245 /* Driver private data, only for Tx (not command) queues,
246 * not shared with device. */
Zhu Yib481de92007-09-25 17:54:57 -0700247 if (id != IWL_CMD_QUEUE_NUM) {
248 txq->txb = kmalloc(sizeof(txq->txb[0]) *
249 TFD_QUEUE_SIZE_MAX, GFP_KERNEL);
250 if (!txq->txb) {
Ian Schram01ebd062007-10-25 17:15:22 +0800251 IWL_ERROR("kmalloc for auxiliary BD "
Zhu Yib481de92007-09-25 17:54:57 -0700252 "structures failed\n");
253 goto error;
254 }
255 } else
256 txq->txb = NULL;
257
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800258 /* Circular buffer of transmit frame descriptors (TFDs),
259 * shared with device */
Zhu Yib481de92007-09-25 17:54:57 -0700260 txq->bd = pci_alloc_consistent(dev,
261 sizeof(txq->bd[0]) * TFD_QUEUE_SIZE_MAX,
262 &txq->q.dma_addr);
263
264 if (!txq->bd) {
265 IWL_ERROR("pci_alloc_consistent(%zd) failed\n",
266 sizeof(txq->bd[0]) * TFD_QUEUE_SIZE_MAX);
267 goto error;
268 }
269 txq->q.id = id;
270
271 return 0;
272
273 error:
274 if (txq->txb) {
275 kfree(txq->txb);
276 txq->txb = NULL;
277 }
278
279 return -ENOMEM;
280}
281
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +0800282/**
283 * iwl4965_tx_queue_init - Allocate and initialize one tx/cmd queue
284 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700285int iwl4965_tx_queue_init(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800286 struct iwl4965_tx_queue *txq, int slots_num, u32 txq_id)
Zhu Yib481de92007-09-25 17:54:57 -0700287{
288 struct pci_dev *dev = priv->pci_dev;
289 int len;
290 int rc = 0;
291
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +0800292 /*
293 * Alloc buffer array for commands (Tx or other types of commands).
294 * For the command queue (#4), allocate command space + one big
295 * command for scan, since scan command is very huge; the system will
296 * not have two scans at the same time, so only one is needed.
Tomas Winklerbb542442007-12-05 20:59:59 +0200297 * For normal Tx queues (all other queues), no super-size command
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +0800298 * space is needed.
299 */
Tomas Winkler857485c2008-03-21 13:53:44 -0700300 len = sizeof(struct iwl_cmd) * slots_num;
Zhu Yib481de92007-09-25 17:54:57 -0700301 if (txq_id == IWL_CMD_QUEUE_NUM)
302 len += IWL_MAX_SCAN_SIZE;
303 txq->cmd = pci_alloc_consistent(dev, len, &txq->dma_addr_cmd);
304 if (!txq->cmd)
305 return -ENOMEM;
306
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +0800307 /* Alloc driver data array and TFD circular buffer */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800308 rc = iwl4965_tx_queue_alloc(priv, txq, txq_id);
Zhu Yib481de92007-09-25 17:54:57 -0700309 if (rc) {
310 pci_free_consistent(dev, len, txq->cmd, txq->dma_addr_cmd);
311
312 return -ENOMEM;
313 }
314 txq->need_update = 0;
315
316 /* TFD_QUEUE_SIZE_MAX must be power-of-two size, otherwise
Tomas Winklerc54b6792008-03-06 17:36:53 -0800317 * iwl_queue_inc_wrap and iwl_queue_dec_wrap are broken. */
Zhu Yib481de92007-09-25 17:54:57 -0700318 BUILD_BUG_ON(TFD_QUEUE_SIZE_MAX & (TFD_QUEUE_SIZE_MAX - 1));
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +0800319
320 /* Initialize queue's high/low-water marks, and head/tail indexes */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800321 iwl4965_queue_init(priv, &txq->q, TFD_QUEUE_SIZE_MAX, slots_num, txq_id);
Zhu Yib481de92007-09-25 17:54:57 -0700322
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +0800323 /* Tell device where to find queue */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800324 iwl4965_hw_tx_queue_init(priv, txq);
Zhu Yib481de92007-09-25 17:54:57 -0700325
326 return 0;
327}
328
329/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800330 * iwl4965_tx_queue_free - Deallocate DMA queue.
Zhu Yib481de92007-09-25 17:54:57 -0700331 * @txq: Transmit queue to deallocate.
332 *
333 * Empty queue by removing and destroying all BD's.
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800334 * Free all buffers.
335 * 0-fill, but do not free "txq" descriptor structure.
Zhu Yib481de92007-09-25 17:54:57 -0700336 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700337void iwl4965_tx_queue_free(struct iwl_priv *priv, struct iwl4965_tx_queue *txq)
Zhu Yib481de92007-09-25 17:54:57 -0700338{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800339 struct iwl4965_queue *q = &txq->q;
Zhu Yib481de92007-09-25 17:54:57 -0700340 struct pci_dev *dev = priv->pci_dev;
341 int len;
342
343 if (q->n_bd == 0)
344 return;
345
346 /* first, empty all BD's */
Tomas Winklerfc4b6852007-10-25 17:15:24 +0800347 for (; q->write_ptr != q->read_ptr;
Tomas Winklerc54b6792008-03-06 17:36:53 -0800348 q->read_ptr = iwl_queue_inc_wrap(q->read_ptr, q->n_bd))
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800349 iwl4965_hw_txq_free_tfd(priv, txq);
Zhu Yib481de92007-09-25 17:54:57 -0700350
Tomas Winkler857485c2008-03-21 13:53:44 -0700351 len = sizeof(struct iwl_cmd) * q->n_window;
Zhu Yib481de92007-09-25 17:54:57 -0700352 if (q->id == IWL_CMD_QUEUE_NUM)
353 len += IWL_MAX_SCAN_SIZE;
354
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800355 /* De-alloc array of command/tx buffers */
Zhu Yib481de92007-09-25 17:54:57 -0700356 pci_free_consistent(dev, len, txq->cmd, txq->dma_addr_cmd);
357
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800358 /* De-alloc circular buffer of TFDs */
Zhu Yib481de92007-09-25 17:54:57 -0700359 if (txq->q.n_bd)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800360 pci_free_consistent(dev, sizeof(struct iwl4965_tfd_frame) *
Zhu Yib481de92007-09-25 17:54:57 -0700361 txq->q.n_bd, txq->bd, txq->q.dma_addr);
362
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800363 /* De-alloc array of per-TFD driver data */
Zhu Yib481de92007-09-25 17:54:57 -0700364 if (txq->txb) {
365 kfree(txq->txb);
366 txq->txb = NULL;
367 }
368
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800369 /* 0-fill queue descriptor structure */
Zhu Yib481de92007-09-25 17:54:57 -0700370 memset(txq, 0, sizeof(*txq));
371}
372
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800373const u8 iwl4965_broadcast_addr[ETH_ALEN] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
Zhu Yib481de92007-09-25 17:54:57 -0700374
375/*************** STATION TABLE MANAGEMENT ****
Ben Cahill9fbab512007-11-29 11:09:47 +0800376 * mac80211 should be examined to determine if sta_info is duplicating
Zhu Yib481de92007-09-25 17:54:57 -0700377 * the functionality provided here
378 */
379
380/**************************************************************/
381
Ian Schram01ebd062007-10-25 17:15:22 +0800382#if 0 /* temporary disable till we add real remove station */
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800383/**
384 * iwl4965_remove_station - Remove driver's knowledge of station.
385 *
386 * NOTE: This does not remove station from device's station table.
387 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700388static u8 iwl4965_remove_station(struct iwl_priv *priv, const u8 *addr, int is_ap)
Zhu Yib481de92007-09-25 17:54:57 -0700389{
390 int index = IWL_INVALID_STATION;
391 int i;
392 unsigned long flags;
393
394 spin_lock_irqsave(&priv->sta_lock, flags);
395
396 if (is_ap)
397 index = IWL_AP_ID;
398 else if (is_broadcast_ether_addr(addr))
399 index = priv->hw_setting.bcast_sta_id;
400 else
401 for (i = IWL_STA_ID; i < priv->hw_setting.max_stations; i++)
402 if (priv->stations[i].used &&
403 !compare_ether_addr(priv->stations[i].sta.sta.addr,
404 addr)) {
405 index = i;
406 break;
407 }
408
409 if (unlikely(index == IWL_INVALID_STATION))
410 goto out;
411
412 if (priv->stations[index].used) {
413 priv->stations[index].used = 0;
414 priv->num_stations--;
415 }
416
417 BUG_ON(priv->num_stations < 0);
418
419out:
420 spin_unlock_irqrestore(&priv->sta_lock, flags);
421 return 0;
422}
Zhu Yi556f8db2007-09-27 11:27:33 +0800423#endif
Zhu Yib481de92007-09-25 17:54:57 -0700424
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800425/**
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800426 * iwl4965_add_station_flags - Add station to tables in driver and device
427 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700428u8 iwl4965_add_station_flags(struct iwl_priv *priv, const u8 *addr,
Ron Rindjunsky67d62032007-11-26 16:14:40 +0200429 int is_ap, u8 flags, void *ht_data)
Zhu Yib481de92007-09-25 17:54:57 -0700430{
431 int i;
432 int index = IWL_INVALID_STATION;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800433 struct iwl4965_station_entry *station;
Zhu Yib481de92007-09-25 17:54:57 -0700434 unsigned long flags_spin;
Joe Perches0795af52007-10-03 17:59:30 -0700435 DECLARE_MAC_BUF(mac);
Zhu Yib481de92007-09-25 17:54:57 -0700436
437 spin_lock_irqsave(&priv->sta_lock, flags_spin);
438 if (is_ap)
439 index = IWL_AP_ID;
440 else if (is_broadcast_ether_addr(addr))
441 index = priv->hw_setting.bcast_sta_id;
442 else
443 for (i = IWL_STA_ID; i < priv->hw_setting.max_stations; i++) {
444 if (!compare_ether_addr(priv->stations[i].sta.sta.addr,
445 addr)) {
446 index = i;
447 break;
448 }
449
450 if (!priv->stations[i].used &&
451 index == IWL_INVALID_STATION)
452 index = i;
453 }
454
455
Ben Cahill9fbab512007-11-29 11:09:47 +0800456 /* These two conditions have the same outcome, but keep them separate
457 since they have different meanings */
Zhu Yib481de92007-09-25 17:54:57 -0700458 if (unlikely(index == IWL_INVALID_STATION)) {
459 spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
460 return index;
461 }
462
463 if (priv->stations[index].used &&
464 !compare_ether_addr(priv->stations[index].sta.sta.addr, addr)) {
465 spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
466 return index;
467 }
468
469
Joe Perches0795af52007-10-03 17:59:30 -0700470 IWL_DEBUG_ASSOC("Add STA ID %d: %s\n", index, print_mac(mac, addr));
Zhu Yib481de92007-09-25 17:54:57 -0700471 station = &priv->stations[index];
472 station->used = 1;
473 priv->num_stations++;
474
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800475 /* Set up the REPLY_ADD_STA command to send to device */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800476 memset(&station->sta, 0, sizeof(struct iwl4965_addsta_cmd));
Zhu Yib481de92007-09-25 17:54:57 -0700477 memcpy(station->sta.sta.addr, addr, ETH_ALEN);
478 station->sta.mode = 0;
479 station->sta.sta.sta_id = index;
480 station->sta.station_flags = 0;
481
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +0800482#ifdef CONFIG_IWL4965_HT
Zhu Yib481de92007-09-25 17:54:57 -0700483 /* BCAST station and IBSS stations do not work in HT mode */
484 if (index != priv->hw_setting.bcast_sta_id &&
485 priv->iw_mode != IEEE80211_IF_TYPE_IBSS)
Ron Rindjunsky67d62032007-11-26 16:14:40 +0200486 iwl4965_set_ht_add_station(priv, index,
487 (struct ieee80211_ht_info *) ht_data);
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +0800488#endif /*CONFIG_IWL4965_HT*/
Zhu Yib481de92007-09-25 17:54:57 -0700489
490 spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800491
492 /* Add station to device's station table */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800493 iwl4965_send_add_station(priv, &station->sta, flags);
Zhu Yib481de92007-09-25 17:54:57 -0700494 return index;
495
496}
497
498/*************** DRIVER STATUS FUNCTIONS *****/
499
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700500static inline int iwl4965_is_ready(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700501{
502 /* The adapter is 'ready' if READY and GEO_CONFIGURED bits are
503 * set but EXIT_PENDING is not */
504 return test_bit(STATUS_READY, &priv->status) &&
505 test_bit(STATUS_GEO_CONFIGURED, &priv->status) &&
506 !test_bit(STATUS_EXIT_PENDING, &priv->status);
507}
508
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700509static inline int iwl4965_is_alive(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700510{
511 return test_bit(STATUS_ALIVE, &priv->status);
512}
513
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700514static inline int iwl4965_is_init(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700515{
516 return test_bit(STATUS_INIT, &priv->status);
517}
518
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700519static inline int iwl4965_is_rfkill(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700520{
521 return test_bit(STATUS_RF_KILL_HW, &priv->status) ||
522 test_bit(STATUS_RF_KILL_SW, &priv->status);
523}
524
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700525static inline int iwl4965_is_ready_rf(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700526{
527
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800528 if (iwl4965_is_rfkill(priv))
Zhu Yib481de92007-09-25 17:54:57 -0700529 return 0;
530
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800531 return iwl4965_is_ready(priv);
Zhu Yib481de92007-09-25 17:54:57 -0700532}
533
534/*************** HOST COMMAND QUEUE FUNCTIONS *****/
535
Zhu Yib481de92007-09-25 17:54:57 -0700536/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800537 * iwl4965_enqueue_hcmd - enqueue a uCode command
Zhu Yib481de92007-09-25 17:54:57 -0700538 * @priv: device private data point
539 * @cmd: a point to the ucode command structure
540 *
541 * The function returns < 0 values to indicate the operation is
542 * failed. On success, it turns the index (> 0) of command in the
543 * command queue.
544 */
Tomas Winkler857485c2008-03-21 13:53:44 -0700545int iwl4965_enqueue_hcmd(struct iwl_priv *priv, struct iwl_host_cmd *cmd)
Zhu Yib481de92007-09-25 17:54:57 -0700546{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800547 struct iwl4965_tx_queue *txq = &priv->txq[IWL_CMD_QUEUE_NUM];
548 struct iwl4965_queue *q = &txq->q;
549 struct iwl4965_tfd_frame *tfd;
Zhu Yib481de92007-09-25 17:54:57 -0700550 u32 *control_flags;
Tomas Winkler857485c2008-03-21 13:53:44 -0700551 struct iwl_cmd *out_cmd;
Zhu Yib481de92007-09-25 17:54:57 -0700552 u32 idx;
553 u16 fix_size = (u16)(cmd->len + sizeof(out_cmd->hdr));
554 dma_addr_t phys_addr;
555 int ret;
556 unsigned long flags;
557
558 /* If any of the command structures end up being larger than
559 * the TFD_MAX_PAYLOAD_SIZE, and it sent as a 'small' command then
560 * we will need to increase the size of the TFD entries */
561 BUG_ON((fix_size > TFD_MAX_PAYLOAD_SIZE) &&
562 !(cmd->meta.flags & CMD_SIZE_HUGE));
563
Gregory Greenmanc342a1b2008-02-06 11:20:40 -0800564 if (iwl4965_is_rfkill(priv)) {
565 IWL_DEBUG_INFO("Not sending command - RF KILL");
566 return -EIO;
567 }
568
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800569 if (iwl4965_queue_space(q) < ((cmd->meta.flags & CMD_ASYNC) ? 2 : 1)) {
Zhu Yib481de92007-09-25 17:54:57 -0700570 IWL_ERROR("No space for Tx\n");
571 return -ENOSPC;
572 }
573
574 spin_lock_irqsave(&priv->hcmd_lock, flags);
575
Tomas Winklerfc4b6852007-10-25 17:15:24 +0800576 tfd = &txq->bd[q->write_ptr];
Zhu Yib481de92007-09-25 17:54:57 -0700577 memset(tfd, 0, sizeof(*tfd));
578
579 control_flags = (u32 *) tfd;
580
Tomas Winklerfc4b6852007-10-25 17:15:24 +0800581 idx = get_cmd_index(q, q->write_ptr, cmd->meta.flags & CMD_SIZE_HUGE);
Zhu Yib481de92007-09-25 17:54:57 -0700582 out_cmd = &txq->cmd[idx];
583
584 out_cmd->hdr.cmd = cmd->id;
585 memcpy(&out_cmd->meta, &cmd->meta, sizeof(cmd->meta));
586 memcpy(&out_cmd->cmd.payload, cmd->data, cmd->len);
587
588 /* At this point, the out_cmd now has all of the incoming cmd
589 * information */
590
591 out_cmd->hdr.flags = 0;
592 out_cmd->hdr.sequence = cpu_to_le16(QUEUE_TO_SEQ(IWL_CMD_QUEUE_NUM) |
Tomas Winklerfc4b6852007-10-25 17:15:24 +0800593 INDEX_TO_SEQ(q->write_ptr));
Zhu Yib481de92007-09-25 17:54:57 -0700594 if (out_cmd->meta.flags & CMD_SIZE_HUGE)
595 out_cmd->hdr.sequence |= cpu_to_le16(SEQ_HUGE_FRAME);
596
597 phys_addr = txq->dma_addr_cmd + sizeof(txq->cmd[0]) * idx +
Tomas Winkler857485c2008-03-21 13:53:44 -0700598 offsetof(struct iwl_cmd, hdr);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800599 iwl4965_hw_txq_attach_buf_to_tfd(priv, tfd, phys_addr, fix_size);
Zhu Yib481de92007-09-25 17:54:57 -0700600
601 IWL_DEBUG_HC("Sending command %s (#%x), seq: 0x%04X, "
602 "%d bytes at %d[%d]:%d\n",
603 get_cmd_string(out_cmd->hdr.cmd),
604 out_cmd->hdr.cmd, le16_to_cpu(out_cmd->hdr.sequence),
Tomas Winklerfc4b6852007-10-25 17:15:24 +0800605 fix_size, q->write_ptr, idx, IWL_CMD_QUEUE_NUM);
Zhu Yib481de92007-09-25 17:54:57 -0700606
607 txq->need_update = 1;
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800608
609 /* Set up entry in queue's byte count circular buffer */
Zhu Yib481de92007-09-25 17:54:57 -0700610 ret = iwl4965_tx_queue_update_wr_ptr(priv, txq, 0);
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800611
612 /* Increment and update queue's write index */
Tomas Winklerc54b6792008-03-06 17:36:53 -0800613 q->write_ptr = iwl_queue_inc_wrap(q->write_ptr, q->n_bd);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800614 iwl4965_tx_queue_update_write_ptr(priv, txq);
Zhu Yib481de92007-09-25 17:54:57 -0700615
616 spin_unlock_irqrestore(&priv->hcmd_lock, flags);
617 return ret ? ret : idx;
618}
619
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -0700620static void iwl4965_set_rxon_hwcrypto(struct iwl_priv *priv, int hw_decrypt)
621{
622 struct iwl4965_rxon_cmd *rxon = &priv->staging_rxon;
623
624 if (hw_decrypt)
625 rxon->filter_flags &= ~RXON_FILTER_DIS_DECRYPT_MSK;
626 else
627 rxon->filter_flags |= RXON_FILTER_DIS_DECRYPT_MSK;
628
629}
630
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700631int iwl4965_send_statistics_request(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700632{
Tomas Winkler857485c2008-03-21 13:53:44 -0700633 u32 flags = 0;
634 return iwl_send_cmd_pdu(priv, REPLY_STATISTICS_CMD,
635 sizeof(flags), &flags);
Zhu Yib481de92007-09-25 17:54:57 -0700636}
637
638/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800639 * iwl4965_rxon_add_station - add station into station table.
Zhu Yib481de92007-09-25 17:54:57 -0700640 *
641 * there is only one AP station with id= IWL_AP_ID
Ben Cahill9fbab512007-11-29 11:09:47 +0800642 * NOTE: mutex must be held before calling this fnction
643 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700644static int iwl4965_rxon_add_station(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -0700645 const u8 *addr, int is_ap)
646{
Zhu Yi556f8db2007-09-27 11:27:33 +0800647 u8 sta_id;
Zhu Yib481de92007-09-25 17:54:57 -0700648
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800649 /* Add station to device's station table */
Ron Rindjunsky67d62032007-11-26 16:14:40 +0200650#ifdef CONFIG_IWL4965_HT
651 struct ieee80211_conf *conf = &priv->hw->conf;
652 struct ieee80211_ht_info *cur_ht_config = &conf->ht_conf;
653
654 if ((is_ap) &&
655 (conf->flags & IEEE80211_CONF_SUPPORT_HT_MODE) &&
656 (priv->iw_mode == IEEE80211_IF_TYPE_STA))
657 sta_id = iwl4965_add_station_flags(priv, addr, is_ap,
658 0, cur_ht_config);
659 else
660#endif /* CONFIG_IWL4965_HT */
661 sta_id = iwl4965_add_station_flags(priv, addr, is_ap,
662 0, NULL);
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800663
664 /* Set up default rate scaling table in device's station table */
Zhu Yib481de92007-09-25 17:54:57 -0700665 iwl4965_add_station(priv, addr, is_ap);
666
Zhu Yi556f8db2007-09-27 11:27:33 +0800667 return sta_id;
Zhu Yib481de92007-09-25 17:54:57 -0700668}
669
670/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800671 * iwl4965_check_rxon_cmd - validate RXON structure is valid
Zhu Yib481de92007-09-25 17:54:57 -0700672 *
673 * NOTE: This is really only useful during development and can eventually
674 * be #ifdef'd out once the driver is stable and folks aren't actively
675 * making changes
676 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800677static int iwl4965_check_rxon_cmd(struct iwl4965_rxon_cmd *rxon)
Zhu Yib481de92007-09-25 17:54:57 -0700678{
679 int error = 0;
680 int counter = 1;
681
682 if (rxon->flags & RXON_FLG_BAND_24G_MSK) {
683 error |= le32_to_cpu(rxon->flags &
684 (RXON_FLG_TGJ_NARROW_BAND_MSK |
685 RXON_FLG_RADAR_DETECT_MSK));
686 if (error)
687 IWL_WARNING("check 24G fields %d | %d\n",
688 counter++, error);
689 } else {
690 error |= (rxon->flags & RXON_FLG_SHORT_SLOT_MSK) ?
691 0 : le32_to_cpu(RXON_FLG_SHORT_SLOT_MSK);
692 if (error)
693 IWL_WARNING("check 52 fields %d | %d\n",
694 counter++, error);
695 error |= le32_to_cpu(rxon->flags & RXON_FLG_CCK_MSK);
696 if (error)
697 IWL_WARNING("check 52 CCK %d | %d\n",
698 counter++, error);
699 }
700 error |= (rxon->node_addr[0] | rxon->bssid_addr[0]) & 0x1;
701 if (error)
702 IWL_WARNING("check mac addr %d | %d\n", counter++, error);
703
704 /* make sure basic rates 6Mbps and 1Mbps are supported */
705 error |= (((rxon->ofdm_basic_rates & IWL_RATE_6M_MASK) == 0) &&
706 ((rxon->cck_basic_rates & IWL_RATE_1M_MASK) == 0));
707 if (error)
708 IWL_WARNING("check basic rate %d | %d\n", counter++, error);
709
710 error |= (le16_to_cpu(rxon->assoc_id) > 2007);
711 if (error)
712 IWL_WARNING("check assoc id %d | %d\n", counter++, error);
713
714 error |= ((rxon->flags & (RXON_FLG_CCK_MSK | RXON_FLG_SHORT_SLOT_MSK))
715 == (RXON_FLG_CCK_MSK | RXON_FLG_SHORT_SLOT_MSK));
716 if (error)
717 IWL_WARNING("check CCK and short slot %d | %d\n",
718 counter++, error);
719
720 error |= ((rxon->flags & (RXON_FLG_CCK_MSK | RXON_FLG_AUTO_DETECT_MSK))
721 == (RXON_FLG_CCK_MSK | RXON_FLG_AUTO_DETECT_MSK));
722 if (error)
723 IWL_WARNING("check CCK & auto detect %d | %d\n",
724 counter++, error);
725
726 error |= ((rxon->flags & (RXON_FLG_AUTO_DETECT_MSK |
727 RXON_FLG_TGG_PROTECT_MSK)) == RXON_FLG_TGG_PROTECT_MSK);
728 if (error)
729 IWL_WARNING("check TGG and auto detect %d | %d\n",
730 counter++, error);
731
732 if (error)
733 IWL_WARNING("Tuning to channel %d\n",
734 le16_to_cpu(rxon->channel));
735
736 if (error) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800737 IWL_ERROR("Not a valid iwl4965_rxon_assoc_cmd field values\n");
Zhu Yib481de92007-09-25 17:54:57 -0700738 return -1;
739 }
740 return 0;
741}
742
743/**
Ben Cahill9fbab512007-11-29 11:09:47 +0800744 * iwl4965_full_rxon_required - check if full RXON (vs RXON_ASSOC) cmd is needed
Ian Schram01ebd062007-10-25 17:15:22 +0800745 * @priv: staging_rxon is compared to active_rxon
Zhu Yib481de92007-09-25 17:54:57 -0700746 *
Ben Cahill9fbab512007-11-29 11:09:47 +0800747 * If the RXON structure is changing enough to require a new tune,
748 * or is clearing the RXON_FILTER_ASSOC_MSK, then return 1 to indicate that
749 * a new tune (full RXON command, rather than RXON_ASSOC cmd) is required.
Zhu Yib481de92007-09-25 17:54:57 -0700750 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700751static int iwl4965_full_rxon_required(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700752{
753
754 /* These items are only settable from the full RXON command */
755 if (!(priv->active_rxon.filter_flags & RXON_FILTER_ASSOC_MSK) ||
756 compare_ether_addr(priv->staging_rxon.bssid_addr,
757 priv->active_rxon.bssid_addr) ||
758 compare_ether_addr(priv->staging_rxon.node_addr,
759 priv->active_rxon.node_addr) ||
760 compare_ether_addr(priv->staging_rxon.wlap_bssid_addr,
761 priv->active_rxon.wlap_bssid_addr) ||
762 (priv->staging_rxon.dev_type != priv->active_rxon.dev_type) ||
763 (priv->staging_rxon.channel != priv->active_rxon.channel) ||
764 (priv->staging_rxon.air_propagation !=
765 priv->active_rxon.air_propagation) ||
766 (priv->staging_rxon.ofdm_ht_single_stream_basic_rates !=
767 priv->active_rxon.ofdm_ht_single_stream_basic_rates) ||
768 (priv->staging_rxon.ofdm_ht_dual_stream_basic_rates !=
769 priv->active_rxon.ofdm_ht_dual_stream_basic_rates) ||
770 (priv->staging_rxon.rx_chain != priv->active_rxon.rx_chain) ||
771 (priv->staging_rxon.assoc_id != priv->active_rxon.assoc_id))
772 return 1;
773
774 /* flags, filter_flags, ofdm_basic_rates, and cck_basic_rates can
775 * be updated with the RXON_ASSOC command -- however only some
776 * flag transitions are allowed using RXON_ASSOC */
777
778 /* Check if we are not switching bands */
779 if ((priv->staging_rxon.flags & RXON_FLG_BAND_24G_MSK) !=
780 (priv->active_rxon.flags & RXON_FLG_BAND_24G_MSK))
781 return 1;
782
783 /* Check if we are switching association toggle */
784 if ((priv->staging_rxon.filter_flags & RXON_FILTER_ASSOC_MSK) !=
785 (priv->active_rxon.filter_flags & RXON_FILTER_ASSOC_MSK))
786 return 1;
787
788 return 0;
789}
790
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700791static int iwl4965_send_rxon_assoc(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700792{
793 int rc = 0;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800794 struct iwl4965_rx_packet *res = NULL;
795 struct iwl4965_rxon_assoc_cmd rxon_assoc;
Tomas Winkler857485c2008-03-21 13:53:44 -0700796 struct iwl_host_cmd cmd = {
Zhu Yib481de92007-09-25 17:54:57 -0700797 .id = REPLY_RXON_ASSOC,
798 .len = sizeof(rxon_assoc),
799 .meta.flags = CMD_WANT_SKB,
800 .data = &rxon_assoc,
801 };
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800802 const struct iwl4965_rxon_cmd *rxon1 = &priv->staging_rxon;
803 const struct iwl4965_rxon_cmd *rxon2 = &priv->active_rxon;
Zhu Yib481de92007-09-25 17:54:57 -0700804
805 if ((rxon1->flags == rxon2->flags) &&
806 (rxon1->filter_flags == rxon2->filter_flags) &&
807 (rxon1->cck_basic_rates == rxon2->cck_basic_rates) &&
808 (rxon1->ofdm_ht_single_stream_basic_rates ==
809 rxon2->ofdm_ht_single_stream_basic_rates) &&
810 (rxon1->ofdm_ht_dual_stream_basic_rates ==
811 rxon2->ofdm_ht_dual_stream_basic_rates) &&
812 (rxon1->rx_chain == rxon2->rx_chain) &&
813 (rxon1->ofdm_basic_rates == rxon2->ofdm_basic_rates)) {
814 IWL_DEBUG_INFO("Using current RXON_ASSOC. Not resending.\n");
815 return 0;
816 }
817
818 rxon_assoc.flags = priv->staging_rxon.flags;
819 rxon_assoc.filter_flags = priv->staging_rxon.filter_flags;
820 rxon_assoc.ofdm_basic_rates = priv->staging_rxon.ofdm_basic_rates;
821 rxon_assoc.cck_basic_rates = priv->staging_rxon.cck_basic_rates;
822 rxon_assoc.reserved = 0;
823 rxon_assoc.ofdm_ht_single_stream_basic_rates =
824 priv->staging_rxon.ofdm_ht_single_stream_basic_rates;
825 rxon_assoc.ofdm_ht_dual_stream_basic_rates =
826 priv->staging_rxon.ofdm_ht_dual_stream_basic_rates;
827 rxon_assoc.rx_chain_select_flags = priv->staging_rxon.rx_chain;
828
Tomas Winkler857485c2008-03-21 13:53:44 -0700829 rc = iwl_send_cmd_sync(priv, &cmd);
Zhu Yib481de92007-09-25 17:54:57 -0700830 if (rc)
831 return rc;
832
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800833 res = (struct iwl4965_rx_packet *)cmd.meta.u.skb->data;
Zhu Yib481de92007-09-25 17:54:57 -0700834 if (res->hdr.flags & IWL_CMD_FAILED_MSK) {
835 IWL_ERROR("Bad return from REPLY_RXON_ASSOC command\n");
836 rc = -EIO;
837 }
838
839 priv->alloc_rxb_skb--;
840 dev_kfree_skb_any(cmd.meta.u.skb);
841
842 return rc;
843}
844
845/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800846 * iwl4965_commit_rxon - commit staging_rxon to hardware
Zhu Yib481de92007-09-25 17:54:57 -0700847 *
Ian Schram01ebd062007-10-25 17:15:22 +0800848 * The RXON command in staging_rxon is committed to the hardware and
Zhu Yib481de92007-09-25 17:54:57 -0700849 * the active_rxon structure is updated with the new data. This
850 * function correctly transitions out of the RXON_ASSOC_MSK state if
851 * a HW tune is required based on the RXON structure changes.
852 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700853static int iwl4965_commit_rxon(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700854{
855 /* cast away the const for active_rxon in this function */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800856 struct iwl4965_rxon_cmd *active_rxon = (void *)&priv->active_rxon;
Joe Perches0795af52007-10-03 17:59:30 -0700857 DECLARE_MAC_BUF(mac);
Zhu Yib481de92007-09-25 17:54:57 -0700858 int rc = 0;
859
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800860 if (!iwl4965_is_alive(priv))
Zhu Yib481de92007-09-25 17:54:57 -0700861 return -1;
862
863 /* always get timestamp with Rx frame */
864 priv->staging_rxon.flags |= RXON_FLG_TSF2HOST_MSK;
865
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800866 rc = iwl4965_check_rxon_cmd(&priv->staging_rxon);
Zhu Yib481de92007-09-25 17:54:57 -0700867 if (rc) {
868 IWL_ERROR("Invalid RXON configuration. Not committing.\n");
869 return -EINVAL;
870 }
871
872 /* If we don't need to send a full RXON, we can use
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800873 * iwl4965_rxon_assoc_cmd which is used to reconfigure filter
Zhu Yib481de92007-09-25 17:54:57 -0700874 * and other flags for the current radio configuration. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800875 if (!iwl4965_full_rxon_required(priv)) {
876 rc = iwl4965_send_rxon_assoc(priv);
Zhu Yib481de92007-09-25 17:54:57 -0700877 if (rc) {
878 IWL_ERROR("Error setting RXON_ASSOC "
879 "configuration (%d).\n", rc);
880 return rc;
881 }
882
883 memcpy(active_rxon, &priv->staging_rxon, sizeof(*active_rxon));
884
885 return 0;
886 }
887
888 /* station table will be cleared */
889 priv->assoc_station_added = 0;
890
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +0800891#ifdef CONFIG_IWL4965_SENSITIVITY
Zhu Yib481de92007-09-25 17:54:57 -0700892 priv->sensitivity_data.state = IWL_SENS_CALIB_NEED_REINIT;
893 if (!priv->error_recovering)
894 priv->start_calib = 0;
895
896 iwl4965_init_sensitivity(priv, CMD_ASYNC, 1);
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +0800897#endif /* CONFIG_IWL4965_SENSITIVITY */
Zhu Yib481de92007-09-25 17:54:57 -0700898
899 /* If we are currently associated and the new config requires
900 * an RXON_ASSOC and the new config wants the associated mask enabled,
901 * we must clear the associated from the active configuration
902 * before we apply the new config */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800903 if (iwl4965_is_associated(priv) &&
Zhu Yib481de92007-09-25 17:54:57 -0700904 (priv->staging_rxon.filter_flags & RXON_FILTER_ASSOC_MSK)) {
905 IWL_DEBUG_INFO("Toggling associated bit on current RXON\n");
906 active_rxon->filter_flags &= ~RXON_FILTER_ASSOC_MSK;
907
Tomas Winkler857485c2008-03-21 13:53:44 -0700908 rc = iwl_send_cmd_pdu(priv, REPLY_RXON,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800909 sizeof(struct iwl4965_rxon_cmd),
Zhu Yib481de92007-09-25 17:54:57 -0700910 &priv->active_rxon);
911
912 /* If the mask clearing failed then we set
913 * active_rxon back to what it was previously */
914 if (rc) {
915 active_rxon->filter_flags |= RXON_FILTER_ASSOC_MSK;
916 IWL_ERROR("Error clearing ASSOC_MSK on current "
917 "configuration (%d).\n", rc);
918 return rc;
919 }
Zhu Yib481de92007-09-25 17:54:57 -0700920 }
921
922 IWL_DEBUG_INFO("Sending RXON\n"
923 "* with%s RXON_FILTER_ASSOC_MSK\n"
924 "* channel = %d\n"
Joe Perches0795af52007-10-03 17:59:30 -0700925 "* bssid = %s\n",
Zhu Yib481de92007-09-25 17:54:57 -0700926 ((priv->staging_rxon.filter_flags &
927 RXON_FILTER_ASSOC_MSK) ? "" : "out"),
928 le16_to_cpu(priv->staging_rxon.channel),
Joe Perches0795af52007-10-03 17:59:30 -0700929 print_mac(mac, priv->staging_rxon.bssid_addr));
Zhu Yib481de92007-09-25 17:54:57 -0700930
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -0700931 iwl4965_set_rxon_hwcrypto(priv, priv->cfg->mod_params->hw_crypto);
Zhu Yib481de92007-09-25 17:54:57 -0700932 /* Apply the new configuration */
Tomas Winkler857485c2008-03-21 13:53:44 -0700933 rc = iwl_send_cmd_pdu(priv, REPLY_RXON,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800934 sizeof(struct iwl4965_rxon_cmd), &priv->staging_rxon);
Zhu Yib481de92007-09-25 17:54:57 -0700935 if (rc) {
936 IWL_ERROR("Error setting new configuration (%d).\n", rc);
937 return rc;
938 }
939
Assaf Kraussbf85ea42008-03-14 10:38:49 -0700940 iwlcore_clear_stations_table(priv);
Zhu Yi556f8db2007-09-27 11:27:33 +0800941
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +0800942#ifdef CONFIG_IWL4965_SENSITIVITY
Zhu Yib481de92007-09-25 17:54:57 -0700943 if (!priv->error_recovering)
944 priv->start_calib = 0;
945
946 priv->sensitivity_data.state = IWL_SENS_CALIB_NEED_REINIT;
947 iwl4965_init_sensitivity(priv, CMD_ASYNC, 1);
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +0800948#endif /* CONFIG_IWL4965_SENSITIVITY */
Zhu Yib481de92007-09-25 17:54:57 -0700949
950 memcpy(active_rxon, &priv->staging_rxon, sizeof(*active_rxon));
951
952 /* If we issue a new RXON command which required a tune then we must
953 * send a new TXPOWER command or we won't be able to Tx any frames */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800954 rc = iwl4965_hw_reg_send_txpower(priv);
Zhu Yib481de92007-09-25 17:54:57 -0700955 if (rc) {
956 IWL_ERROR("Error setting Tx power (%d).\n", rc);
957 return rc;
958 }
959
960 /* Add the broadcast address so we can send broadcast frames */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800961 if (iwl4965_rxon_add_station(priv, iwl4965_broadcast_addr, 0) ==
Zhu Yib481de92007-09-25 17:54:57 -0700962 IWL_INVALID_STATION) {
963 IWL_ERROR("Error adding BROADCAST address for transmit.\n");
964 return -EIO;
965 }
966
967 /* If we have set the ASSOC_MSK and we are in BSS mode then
968 * add the IWL_AP_ID to the station rate table */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800969 if (iwl4965_is_associated(priv) &&
Zhu Yib481de92007-09-25 17:54:57 -0700970 (priv->iw_mode == IEEE80211_IF_TYPE_STA)) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800971 if (iwl4965_rxon_add_station(priv, priv->active_rxon.bssid_addr, 1)
Zhu Yib481de92007-09-25 17:54:57 -0700972 == IWL_INVALID_STATION) {
973 IWL_ERROR("Error adding AP address for transmit.\n");
974 return -EIO;
975 }
976 priv->assoc_station_added = 1;
977 }
978
979 return 0;
980}
981
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700982static int iwl4965_send_bt_config(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700983{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800984 struct iwl4965_bt_cmd bt_cmd = {
Zhu Yib481de92007-09-25 17:54:57 -0700985 .flags = 3,
986 .lead_time = 0xAA,
987 .max_kill = 1,
988 .kill_ack_mask = 0,
989 .kill_cts_mask = 0,
990 };
991
Tomas Winkler857485c2008-03-21 13:53:44 -0700992 return iwl_send_cmd_pdu(priv, REPLY_BT_CONFIG,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800993 sizeof(struct iwl4965_bt_cmd), &bt_cmd);
Zhu Yib481de92007-09-25 17:54:57 -0700994}
995
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700996static int iwl4965_send_scan_abort(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700997{
998 int rc = 0;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800999 struct iwl4965_rx_packet *res;
Tomas Winkler857485c2008-03-21 13:53:44 -07001000 struct iwl_host_cmd cmd = {
Zhu Yib481de92007-09-25 17:54:57 -07001001 .id = REPLY_SCAN_ABORT_CMD,
1002 .meta.flags = CMD_WANT_SKB,
1003 };
1004
1005 /* If there isn't a scan actively going on in the hardware
1006 * then we are in between scan bands and not actually
1007 * actively scanning, so don't send the abort command */
1008 if (!test_bit(STATUS_SCAN_HW, &priv->status)) {
1009 clear_bit(STATUS_SCAN_ABORTING, &priv->status);
1010 return 0;
1011 }
1012
Tomas Winkler857485c2008-03-21 13:53:44 -07001013 rc = iwl_send_cmd_sync(priv, &cmd);
Zhu Yib481de92007-09-25 17:54:57 -07001014 if (rc) {
1015 clear_bit(STATUS_SCAN_ABORTING, &priv->status);
1016 return rc;
1017 }
1018
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001019 res = (struct iwl4965_rx_packet *)cmd.meta.u.skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07001020 if (res->u.status != CAN_ABORT_STATUS) {
1021 /* The scan abort will return 1 for success or
1022 * 2 for "failure". A failure condition can be
1023 * due to simply not being in an active scan which
1024 * can occur if we send the scan abort before we
1025 * the microcode has notified us that a scan is
1026 * completed. */
1027 IWL_DEBUG_INFO("SCAN_ABORT returned %d.\n", res->u.status);
1028 clear_bit(STATUS_SCAN_ABORTING, &priv->status);
1029 clear_bit(STATUS_SCAN_HW, &priv->status);
1030 }
1031
1032 dev_kfree_skb_any(cmd.meta.u.skb);
1033
1034 return rc;
1035}
1036
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001037static int iwl4965_card_state_sync_callback(struct iwl_priv *priv,
Tomas Winkler857485c2008-03-21 13:53:44 -07001038 struct iwl_cmd *cmd,
Zhu Yib481de92007-09-25 17:54:57 -07001039 struct sk_buff *skb)
1040{
1041 return 1;
1042}
1043
1044/*
1045 * CARD_STATE_CMD
1046 *
Ben Cahill9fbab512007-11-29 11:09:47 +08001047 * Use: Sets the device's internal card state to enable, disable, or halt
Zhu Yib481de92007-09-25 17:54:57 -07001048 *
1049 * When in the 'enable' state the card operates as normal.
1050 * When in the 'disable' state, the card enters into a low power mode.
1051 * When in the 'halt' state, the card is shut down and must be fully
1052 * restarted to come back on.
1053 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001054static int iwl4965_send_card_state(struct iwl_priv *priv, u32 flags, u8 meta_flag)
Zhu Yib481de92007-09-25 17:54:57 -07001055{
Tomas Winkler857485c2008-03-21 13:53:44 -07001056 struct iwl_host_cmd cmd = {
Zhu Yib481de92007-09-25 17:54:57 -07001057 .id = REPLY_CARD_STATE_CMD,
1058 .len = sizeof(u32),
1059 .data = &flags,
1060 .meta.flags = meta_flag,
1061 };
1062
1063 if (meta_flag & CMD_ASYNC)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001064 cmd.meta.u.callback = iwl4965_card_state_sync_callback;
Zhu Yib481de92007-09-25 17:54:57 -07001065
Tomas Winkler857485c2008-03-21 13:53:44 -07001066 return iwl_send_cmd(priv, &cmd);
Zhu Yib481de92007-09-25 17:54:57 -07001067}
1068
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001069static int iwl4965_add_sta_sync_callback(struct iwl_priv *priv,
Tomas Winkler857485c2008-03-21 13:53:44 -07001070 struct iwl_cmd *cmd, struct sk_buff *skb)
Zhu Yib481de92007-09-25 17:54:57 -07001071{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001072 struct iwl4965_rx_packet *res = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07001073
1074 if (!skb) {
1075 IWL_ERROR("Error: Response NULL in REPLY_ADD_STA.\n");
1076 return 1;
1077 }
1078
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001079 res = (struct iwl4965_rx_packet *)skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07001080 if (res->hdr.flags & IWL_CMD_FAILED_MSK) {
1081 IWL_ERROR("Bad return from REPLY_ADD_STA (0x%08X)\n",
1082 res->hdr.flags);
1083 return 1;
1084 }
1085
1086 switch (res->u.add_sta.status) {
1087 case ADD_STA_SUCCESS_MSK:
1088 break;
1089 default:
1090 break;
1091 }
1092
1093 /* We didn't cache the SKB; let the caller free it */
1094 return 1;
1095}
1096
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001097int iwl4965_send_add_station(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001098 struct iwl4965_addsta_cmd *sta, u8 flags)
Zhu Yib481de92007-09-25 17:54:57 -07001099{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001100 struct iwl4965_rx_packet *res = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07001101 int rc = 0;
Tomas Winkler857485c2008-03-21 13:53:44 -07001102 struct iwl_host_cmd cmd = {
Zhu Yib481de92007-09-25 17:54:57 -07001103 .id = REPLY_ADD_STA,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001104 .len = sizeof(struct iwl4965_addsta_cmd),
Zhu Yib481de92007-09-25 17:54:57 -07001105 .meta.flags = flags,
1106 .data = sta,
1107 };
1108
1109 if (flags & CMD_ASYNC)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001110 cmd.meta.u.callback = iwl4965_add_sta_sync_callback;
Zhu Yib481de92007-09-25 17:54:57 -07001111 else
1112 cmd.meta.flags |= CMD_WANT_SKB;
1113
Tomas Winkler857485c2008-03-21 13:53:44 -07001114 rc = iwl_send_cmd(priv, &cmd);
Zhu Yib481de92007-09-25 17:54:57 -07001115
1116 if (rc || (flags & CMD_ASYNC))
1117 return rc;
1118
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001119 res = (struct iwl4965_rx_packet *)cmd.meta.u.skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07001120 if (res->hdr.flags & IWL_CMD_FAILED_MSK) {
1121 IWL_ERROR("Bad return from REPLY_ADD_STA (0x%08X)\n",
1122 res->hdr.flags);
1123 rc = -EIO;
1124 }
1125
1126 if (rc == 0) {
1127 switch (res->u.add_sta.status) {
1128 case ADD_STA_SUCCESS_MSK:
1129 IWL_DEBUG_INFO("REPLY_ADD_STA PASSED\n");
1130 break;
1131 default:
1132 rc = -EIO;
1133 IWL_WARNING("REPLY_ADD_STA failed\n");
1134 break;
1135 }
1136 }
1137
1138 priv->alloc_rxb_skb--;
1139 dev_kfree_skb_any(cmd.meta.u.skb);
1140
1141 return rc;
1142}
1143
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07001144static int iwl4965_set_ccmp_dynamic_key_info(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07001145 struct ieee80211_key_conf *keyconf,
1146 u8 sta_id)
1147{
1148 unsigned long flags;
1149 __le16 key_flags = 0;
1150
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07001151 key_flags |= (STA_KEY_FLG_CCMP | STA_KEY_FLG_MAP_KEY_MSK);
1152 key_flags |= cpu_to_le16(keyconf->keyidx << STA_KEY_FLG_KEYID_POS);
1153
1154 if (sta_id == priv->hw_setting.bcast_sta_id)
1155 key_flags |= STA_KEY_MULTICAST_MSK;
1156
1157 keyconf->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
1158 keyconf->hw_key_idx = keyconf->keyidx;
1159
1160 key_flags &= ~STA_KEY_FLG_INVALID;
1161
Zhu Yib481de92007-09-25 17:54:57 -07001162 spin_lock_irqsave(&priv->sta_lock, flags);
1163 priv->stations[sta_id].keyinfo.alg = keyconf->alg;
1164 priv->stations[sta_id].keyinfo.keylen = keyconf->keylen;
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07001165
Zhu Yib481de92007-09-25 17:54:57 -07001166 memcpy(priv->stations[sta_id].keyinfo.key, keyconf->key,
1167 keyconf->keylen);
1168
1169 memcpy(priv->stations[sta_id].sta.key.key, keyconf->key,
1170 keyconf->keylen);
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07001171
1172 priv->stations[sta_id].sta.key.key_offset
1173 = (sta_id % STA_KEY_MAX_NUM);/*FIXME*/
Zhu Yib481de92007-09-25 17:54:57 -07001174 priv->stations[sta_id].sta.key.key_flags = key_flags;
1175 priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
1176 priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
1177
1178 spin_unlock_irqrestore(&priv->sta_lock, flags);
1179
1180 IWL_DEBUG_INFO("hwcrypto: modify ucode station key info\n");
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07001181 return iwl4965_send_add_station(priv,
1182 &priv->stations[sta_id].sta, CMD_ASYNC);
1183}
1184
1185static int iwl4965_set_tkip_dynamic_key_info(struct iwl_priv *priv,
1186 struct ieee80211_key_conf *keyconf,
1187 u8 sta_id)
1188{
Emmanuel Grumbach2bc75082008-03-19 16:41:45 -07001189 unsigned long flags;
1190 int ret = 0;
1191
1192 keyconf->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
1193 keyconf->flags |= IEEE80211_KEY_FLAG_GENERATE_MMIC;
1194 keyconf->hw_key_idx = keyconf->keyidx;
1195
1196 spin_lock_irqsave(&priv->sta_lock, flags);
1197
1198 priv->stations[sta_id].keyinfo.alg = keyconf->alg;
1199 priv->stations[sta_id].keyinfo.conf = keyconf;
1200 priv->stations[sta_id].keyinfo.keylen = 16;
1201
1202 /* This copy is acutally not needed: we get the key with each TX */
1203 memcpy(priv->stations[sta_id].keyinfo.key, keyconf->key, 16);
1204
1205 memcpy(priv->stations[sta_id].sta.key.key, keyconf->key, 16);
1206
1207 spin_unlock_irqrestore(&priv->sta_lock, flags);
1208
1209 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07001210}
1211
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001212static int iwl4965_clear_sta_key_info(struct iwl_priv *priv, u8 sta_id)
Zhu Yib481de92007-09-25 17:54:57 -07001213{
1214 unsigned long flags;
1215
1216 spin_lock_irqsave(&priv->sta_lock, flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001217 memset(&priv->stations[sta_id].keyinfo, 0, sizeof(struct iwl4965_hw_key));
1218 memset(&priv->stations[sta_id].sta.key, 0, sizeof(struct iwl4965_keyinfo));
Zhu Yib481de92007-09-25 17:54:57 -07001219 priv->stations[sta_id].sta.key.key_flags = STA_KEY_FLG_NO_ENC;
1220 priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
1221 priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
1222 spin_unlock_irqrestore(&priv->sta_lock, flags);
1223
1224 IWL_DEBUG_INFO("hwcrypto: clear ucode station key info\n");
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001225 iwl4965_send_add_station(priv, &priv->stations[sta_id].sta, 0);
Zhu Yib481de92007-09-25 17:54:57 -07001226 return 0;
1227}
1228
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07001229static int iwl4965_set_dynamic_key(struct iwl_priv *priv,
1230 struct ieee80211_key_conf *key, u8 sta_id)
1231{
1232 int ret;
1233
1234 switch (key->alg) {
1235 case ALG_CCMP:
1236 ret = iwl4965_set_ccmp_dynamic_key_info(priv, key, sta_id);
1237 break;
1238 case ALG_TKIP:
1239 ret = iwl4965_set_tkip_dynamic_key_info(priv, key, sta_id);
1240 break;
1241 case ALG_WEP:
1242 ret = -EOPNOTSUPP;
1243 break;
1244 default:
1245 IWL_ERROR("Unknown alg: %s alg = %d\n", __func__, key->alg);
1246 ret = -EINVAL;
1247 }
1248
1249 return ret;
1250}
1251
1252static int iwl4965_remove_static_key(struct iwl_priv *priv)
1253{
1254 int ret = -EOPNOTSUPP;
1255
1256 return ret;
1257}
1258
1259static int iwl4965_set_static_key(struct iwl_priv *priv,
1260 struct ieee80211_key_conf *key)
1261{
1262 if (key->alg == ALG_WEP)
1263 return -EOPNOTSUPP;
1264
1265 IWL_ERROR("Static key invalid: alg %d\n", key->alg);
1266 return -EINVAL;
1267}
1268
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001269static void iwl4965_clear_free_frames(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001270{
1271 struct list_head *element;
1272
1273 IWL_DEBUG_INFO("%d frames on pre-allocated heap on clear.\n",
1274 priv->frames_count);
1275
1276 while (!list_empty(&priv->free_frames)) {
1277 element = priv->free_frames.next;
1278 list_del(element);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001279 kfree(list_entry(element, struct iwl4965_frame, list));
Zhu Yib481de92007-09-25 17:54:57 -07001280 priv->frames_count--;
1281 }
1282
1283 if (priv->frames_count) {
1284 IWL_WARNING("%d frames still in use. Did we lose one?\n",
1285 priv->frames_count);
1286 priv->frames_count = 0;
1287 }
1288}
1289
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001290static struct iwl4965_frame *iwl4965_get_free_frame(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001291{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001292 struct iwl4965_frame *frame;
Zhu Yib481de92007-09-25 17:54:57 -07001293 struct list_head *element;
1294 if (list_empty(&priv->free_frames)) {
1295 frame = kzalloc(sizeof(*frame), GFP_KERNEL);
1296 if (!frame) {
1297 IWL_ERROR("Could not allocate frame!\n");
1298 return NULL;
1299 }
1300
1301 priv->frames_count++;
1302 return frame;
1303 }
1304
1305 element = priv->free_frames.next;
1306 list_del(element);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001307 return list_entry(element, struct iwl4965_frame, list);
Zhu Yib481de92007-09-25 17:54:57 -07001308}
1309
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001310static void iwl4965_free_frame(struct iwl_priv *priv, struct iwl4965_frame *frame)
Zhu Yib481de92007-09-25 17:54:57 -07001311{
1312 memset(frame, 0, sizeof(*frame));
1313 list_add(&frame->list, &priv->free_frames);
1314}
1315
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001316unsigned int iwl4965_fill_beacon_frame(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07001317 struct ieee80211_hdr *hdr,
1318 const u8 *dest, int left)
1319{
1320
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001321 if (!iwl4965_is_associated(priv) || !priv->ibss_beacon ||
Zhu Yib481de92007-09-25 17:54:57 -07001322 ((priv->iw_mode != IEEE80211_IF_TYPE_IBSS) &&
1323 (priv->iw_mode != IEEE80211_IF_TYPE_AP)))
1324 return 0;
1325
1326 if (priv->ibss_beacon->len > left)
1327 return 0;
1328
1329 memcpy(hdr, priv->ibss_beacon->data, priv->ibss_beacon->len);
1330
1331 return priv->ibss_beacon->len;
1332}
1333
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001334static u8 iwl4965_rate_get_lowest_plcp(int rate_mask)
Zhu Yib481de92007-09-25 17:54:57 -07001335{
1336 u8 i;
1337
1338 for (i = IWL_RATE_1M_INDEX; i != IWL_RATE_INVALID;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001339 i = iwl4965_rates[i].next_ieee) {
Zhu Yib481de92007-09-25 17:54:57 -07001340 if (rate_mask & (1 << i))
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001341 return iwl4965_rates[i].plcp;
Zhu Yib481de92007-09-25 17:54:57 -07001342 }
1343
1344 return IWL_RATE_INVALID;
1345}
1346
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001347static int iwl4965_send_beacon_cmd(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001348{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001349 struct iwl4965_frame *frame;
Zhu Yib481de92007-09-25 17:54:57 -07001350 unsigned int frame_size;
1351 int rc;
1352 u8 rate;
1353
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001354 frame = iwl4965_get_free_frame(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001355
1356 if (!frame) {
1357 IWL_ERROR("Could not obtain free frame buffer for beacon "
1358 "command.\n");
1359 return -ENOMEM;
1360 }
1361
1362 if (!(priv->staging_rxon.flags & RXON_FLG_BAND_24G_MSK)) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001363 rate = iwl4965_rate_get_lowest_plcp(priv->active_rate_basic &
Zhu Yib481de92007-09-25 17:54:57 -07001364 0xFF0);
1365 if (rate == IWL_INVALID_RATE)
1366 rate = IWL_RATE_6M_PLCP;
1367 } else {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001368 rate = iwl4965_rate_get_lowest_plcp(priv->active_rate_basic & 0xF);
Zhu Yib481de92007-09-25 17:54:57 -07001369 if (rate == IWL_INVALID_RATE)
1370 rate = IWL_RATE_1M_PLCP;
1371 }
1372
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001373 frame_size = iwl4965_hw_get_beacon_cmd(priv, frame, rate);
Zhu Yib481de92007-09-25 17:54:57 -07001374
Tomas Winkler857485c2008-03-21 13:53:44 -07001375 rc = iwl_send_cmd_pdu(priv, REPLY_TX_BEACON, frame_size,
Zhu Yib481de92007-09-25 17:54:57 -07001376 &frame->u.cmd[0]);
1377
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001378 iwl4965_free_frame(priv, frame);
Zhu Yib481de92007-09-25 17:54:57 -07001379
1380 return rc;
1381}
1382
1383/******************************************************************************
1384 *
Zhu Yib481de92007-09-25 17:54:57 -07001385 * Misc. internal state and helper functions
1386 *
1387 ******************************************************************************/
Zhu Yib481de92007-09-25 17:54:57 -07001388
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001389static void iwl4965_unset_hw_setting(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001390{
1391 if (priv->hw_setting.shared_virt)
1392 pci_free_consistent(priv->pci_dev,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001393 sizeof(struct iwl4965_shared),
Zhu Yib481de92007-09-25 17:54:57 -07001394 priv->hw_setting.shared_virt,
1395 priv->hw_setting.shared_phys);
1396}
1397
1398/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001399 * iwl4965_supported_rate_to_ie - fill in the supported rate in IE field
Zhu Yib481de92007-09-25 17:54:57 -07001400 *
1401 * return : set the bit for each supported rate insert in ie
1402 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001403static u16 iwl4965_supported_rate_to_ie(u8 *ie, u16 supported_rate,
Tomas Winklerc7c46672007-10-18 02:04:15 +02001404 u16 basic_rate, int *left)
Zhu Yib481de92007-09-25 17:54:57 -07001405{
1406 u16 ret_rates = 0, bit;
1407 int i;
Tomas Winklerc7c46672007-10-18 02:04:15 +02001408 u8 *cnt = ie;
1409 u8 *rates = ie + 1;
Zhu Yib481de92007-09-25 17:54:57 -07001410
1411 for (bit = 1, i = 0; i < IWL_RATE_COUNT; i++, bit <<= 1) {
1412 if (bit & supported_rate) {
1413 ret_rates |= bit;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001414 rates[*cnt] = iwl4965_rates[i].ieee |
Tomas Winklerc7c46672007-10-18 02:04:15 +02001415 ((bit & basic_rate) ? 0x80 : 0x00);
1416 (*cnt)++;
1417 (*left)--;
1418 if ((*left <= 0) ||
1419 (*cnt >= IWL_SUPPORTED_RATES_IE_LEN))
Zhu Yib481de92007-09-25 17:54:57 -07001420 break;
1421 }
1422 }
1423
1424 return ret_rates;
1425}
1426
Zhu Yib481de92007-09-25 17:54:57 -07001427/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001428 * iwl4965_fill_probe_req - fill in all required fields and IE for probe request
Zhu Yib481de92007-09-25 17:54:57 -07001429 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001430static u16 iwl4965_fill_probe_req(struct iwl_priv *priv,
Tomas Winkler78330fd2008-02-06 02:37:18 +02001431 enum ieee80211_band band,
1432 struct ieee80211_mgmt *frame,
1433 int left, int is_direct)
Zhu Yib481de92007-09-25 17:54:57 -07001434{
1435 int len = 0;
1436 u8 *pos = NULL;
mabbasbee488d2007-10-25 17:15:42 +08001437 u16 active_rates, ret_rates, cck_rates, active_rate_basic;
Ron Rindjunsky8fb88032007-11-26 16:14:38 +02001438#ifdef CONFIG_IWL4965_HT
Tomas Winkler78330fd2008-02-06 02:37:18 +02001439 const struct ieee80211_supported_band *sband =
1440 iwl4965_get_hw_mode(priv, band);
Ron Rindjunsky8fb88032007-11-26 16:14:38 +02001441#endif /* CONFIG_IWL4965_HT */
Zhu Yib481de92007-09-25 17:54:57 -07001442
1443 /* Make sure there is enough space for the probe request,
1444 * two mandatory IEs and the data */
1445 left -= 24;
1446 if (left < 0)
1447 return 0;
1448 len += 24;
1449
1450 frame->frame_control = cpu_to_le16(IEEE80211_STYPE_PROBE_REQ);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001451 memcpy(frame->da, iwl4965_broadcast_addr, ETH_ALEN);
Zhu Yib481de92007-09-25 17:54:57 -07001452 memcpy(frame->sa, priv->mac_addr, ETH_ALEN);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001453 memcpy(frame->bssid, iwl4965_broadcast_addr, ETH_ALEN);
Zhu Yib481de92007-09-25 17:54:57 -07001454 frame->seq_ctrl = 0;
1455
1456 /* fill in our indirect SSID IE */
1457 /* ...next IE... */
1458
1459 left -= 2;
1460 if (left < 0)
1461 return 0;
1462 len += 2;
1463 pos = &(frame->u.probe_req.variable[0]);
1464 *pos++ = WLAN_EID_SSID;
1465 *pos++ = 0;
1466
1467 /* fill in our direct SSID IE... */
1468 if (is_direct) {
1469 /* ...next IE... */
1470 left -= 2 + priv->essid_len;
1471 if (left < 0)
1472 return 0;
1473 /* ... fill it in... */
1474 *pos++ = WLAN_EID_SSID;
1475 *pos++ = priv->essid_len;
1476 memcpy(pos, priv->essid, priv->essid_len);
1477 pos += priv->essid_len;
1478 len += 2 + priv->essid_len;
1479 }
1480
1481 /* fill in supported rate */
1482 /* ...next IE... */
1483 left -= 2;
1484 if (left < 0)
1485 return 0;
Tomas Winklerc7c46672007-10-18 02:04:15 +02001486
Zhu Yib481de92007-09-25 17:54:57 -07001487 /* ... fill it in... */
1488 *pos++ = WLAN_EID_SUPP_RATES;
1489 *pos = 0;
Tomas Winklerc7c46672007-10-18 02:04:15 +02001490
mabbasbee488d2007-10-25 17:15:42 +08001491 /* exclude 60M rate */
1492 active_rates = priv->rates_mask;
1493 active_rates &= ~IWL_RATE_60M_MASK;
1494
1495 active_rate_basic = active_rates & IWL_BASIC_RATES_MASK;
Zhu Yib481de92007-09-25 17:54:57 -07001496
Tomas Winklerc7c46672007-10-18 02:04:15 +02001497 cck_rates = IWL_CCK_RATES_MASK & active_rates;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001498 ret_rates = iwl4965_supported_rate_to_ie(pos, cck_rates,
mabbasbee488d2007-10-25 17:15:42 +08001499 active_rate_basic, &left);
Tomas Winklerc7c46672007-10-18 02:04:15 +02001500 active_rates &= ~ret_rates;
1501
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001502 ret_rates = iwl4965_supported_rate_to_ie(pos, active_rates,
mabbasbee488d2007-10-25 17:15:42 +08001503 active_rate_basic, &left);
Tomas Winklerc7c46672007-10-18 02:04:15 +02001504 active_rates &= ~ret_rates;
1505
Zhu Yib481de92007-09-25 17:54:57 -07001506 len += 2 + *pos;
1507 pos += (*pos) + 1;
Tomas Winklerc7c46672007-10-18 02:04:15 +02001508 if (active_rates == 0)
Zhu Yib481de92007-09-25 17:54:57 -07001509 goto fill_end;
1510
1511 /* fill in supported extended rate */
1512 /* ...next IE... */
1513 left -= 2;
1514 if (left < 0)
1515 return 0;
1516 /* ... fill it in... */
1517 *pos++ = WLAN_EID_EXT_SUPP_RATES;
1518 *pos = 0;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001519 iwl4965_supported_rate_to_ie(pos, active_rates,
mabbasbee488d2007-10-25 17:15:42 +08001520 active_rate_basic, &left);
Zhu Yib481de92007-09-25 17:54:57 -07001521 if (*pos > 0)
1522 len += 2 + *pos;
1523
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08001524#ifdef CONFIG_IWL4965_HT
Tomas Winkler78330fd2008-02-06 02:37:18 +02001525 if (sband && sband->ht_info.ht_supported) {
1526 struct ieee80211_ht_cap *ht_cap;
Zhu Yib481de92007-09-25 17:54:57 -07001527 pos += (*pos) + 1;
1528 *pos++ = WLAN_EID_HT_CAPABILITY;
Ron Rindjunsky8fb88032007-11-26 16:14:38 +02001529 *pos++ = sizeof(struct ieee80211_ht_cap);
Tomas Winkler78330fd2008-02-06 02:37:18 +02001530 ht_cap = (struct ieee80211_ht_cap *)pos;
1531 ht_cap->cap_info = cpu_to_le16(sband->ht_info.cap);
1532 memcpy(ht_cap->supp_mcs_set, sband->ht_info.supp_mcs_set, 16);
1533 ht_cap->ampdu_params_info =(sband->ht_info.ampdu_factor &
1534 IEEE80211_HT_CAP_AMPDU_FACTOR) |
1535 ((sband->ht_info.ampdu_density << 2) &
1536 IEEE80211_HT_CAP_AMPDU_DENSITY);
Ron Rindjunsky8fb88032007-11-26 16:14:38 +02001537 len += 2 + sizeof(struct ieee80211_ht_cap);
Zhu Yib481de92007-09-25 17:54:57 -07001538 }
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08001539#endif /*CONFIG_IWL4965_HT */
Zhu Yib481de92007-09-25 17:54:57 -07001540
1541 fill_end:
1542 return (u16)len;
1543}
1544
1545/*
1546 * QoS support
1547*/
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001548static int iwl4965_send_qos_params_command(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001549 struct iwl4965_qosparam_cmd *qos)
Zhu Yib481de92007-09-25 17:54:57 -07001550{
1551
Tomas Winkler857485c2008-03-21 13:53:44 -07001552 return iwl_send_cmd_pdu(priv, REPLY_QOS_PARAM,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001553 sizeof(struct iwl4965_qosparam_cmd), qos);
Zhu Yib481de92007-09-25 17:54:57 -07001554}
1555
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001556static void iwl4965_activate_qos(struct iwl_priv *priv, u8 force)
Zhu Yib481de92007-09-25 17:54:57 -07001557{
1558 unsigned long flags;
1559
Zhu Yib481de92007-09-25 17:54:57 -07001560 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
1561 return;
1562
1563 if (!priv->qos_data.qos_enable)
1564 return;
1565
1566 spin_lock_irqsave(&priv->lock, flags);
1567 priv->qos_data.def_qos_parm.qos_flags = 0;
1568
1569 if (priv->qos_data.qos_cap.q_AP.queue_request &&
1570 !priv->qos_data.qos_cap.q_AP.txop_request)
1571 priv->qos_data.def_qos_parm.qos_flags |=
1572 QOS_PARAM_FLG_TXOP_TYPE_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07001573 if (priv->qos_data.qos_active)
1574 priv->qos_data.def_qos_parm.qos_flags |=
1575 QOS_PARAM_FLG_UPDATE_EDCA_MSK;
1576
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08001577#ifdef CONFIG_IWL4965_HT
Ron Rindjunskyfd105e72007-11-26 16:14:39 +02001578 if (priv->current_ht_config.is_ht)
Tomas Winklerf1f1f5c2007-10-25 17:15:26 +08001579 priv->qos_data.def_qos_parm.qos_flags |= QOS_PARAM_FLG_TGN_MSK;
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08001580#endif /* CONFIG_IWL4965_HT */
Tomas Winklerf1f1f5c2007-10-25 17:15:26 +08001581
Zhu Yib481de92007-09-25 17:54:57 -07001582 spin_unlock_irqrestore(&priv->lock, flags);
1583
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001584 if (force || iwl4965_is_associated(priv)) {
Tomas Winklerf1f1f5c2007-10-25 17:15:26 +08001585 IWL_DEBUG_QOS("send QoS cmd with Qos active=%d FLAGS=0x%X\n",
1586 priv->qos_data.qos_active,
1587 priv->qos_data.def_qos_parm.qos_flags);
Zhu Yib481de92007-09-25 17:54:57 -07001588
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001589 iwl4965_send_qos_params_command(priv,
Zhu Yib481de92007-09-25 17:54:57 -07001590 &(priv->qos_data.def_qos_parm));
1591 }
1592}
1593
Zhu Yib481de92007-09-25 17:54:57 -07001594/*
1595 * Power management (not Tx power!) functions
1596 */
1597#define MSEC_TO_USEC 1024
1598
1599#define NOSLP __constant_cpu_to_le16(0), 0, 0
1600#define SLP IWL_POWER_DRIVER_ALLOW_SLEEP_MSK, 0, 0
1601#define SLP_TIMEOUT(T) __constant_cpu_to_le32((T) * MSEC_TO_USEC)
1602#define SLP_VEC(X0, X1, X2, X3, X4) {__constant_cpu_to_le32(X0), \
1603 __constant_cpu_to_le32(X1), \
1604 __constant_cpu_to_le32(X2), \
1605 __constant_cpu_to_le32(X3), \
1606 __constant_cpu_to_le32(X4)}
1607
1608
1609/* default power management (not Tx power) table values */
1610/* for tim 0-10 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001611static struct iwl4965_power_vec_entry range_0[IWL_POWER_AC] = {
Zhu Yib481de92007-09-25 17:54:57 -07001612 {{NOSLP, SLP_TIMEOUT(0), SLP_TIMEOUT(0), SLP_VEC(0, 0, 0, 0, 0)}, 0},
1613 {{SLP, SLP_TIMEOUT(200), SLP_TIMEOUT(500), SLP_VEC(1, 2, 3, 4, 4)}, 0},
1614 {{SLP, SLP_TIMEOUT(200), SLP_TIMEOUT(300), SLP_VEC(2, 4, 6, 7, 7)}, 0},
1615 {{SLP, SLP_TIMEOUT(50), SLP_TIMEOUT(100), SLP_VEC(2, 6, 9, 9, 10)}, 0},
1616 {{SLP, SLP_TIMEOUT(50), SLP_TIMEOUT(25), SLP_VEC(2, 7, 9, 9, 10)}, 1},
1617 {{SLP, SLP_TIMEOUT(25), SLP_TIMEOUT(25), SLP_VEC(4, 7, 10, 10, 10)}, 1}
1618};
1619
1620/* for tim > 10 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001621static struct iwl4965_power_vec_entry range_1[IWL_POWER_AC] = {
Zhu Yib481de92007-09-25 17:54:57 -07001622 {{NOSLP, SLP_TIMEOUT(0), SLP_TIMEOUT(0), SLP_VEC(0, 0, 0, 0, 0)}, 0},
1623 {{SLP, SLP_TIMEOUT(200), SLP_TIMEOUT(500),
1624 SLP_VEC(1, 2, 3, 4, 0xFF)}, 0},
1625 {{SLP, SLP_TIMEOUT(200), SLP_TIMEOUT(300),
1626 SLP_VEC(2, 4, 6, 7, 0xFF)}, 0},
1627 {{SLP, SLP_TIMEOUT(50), SLP_TIMEOUT(100),
1628 SLP_VEC(2, 6, 9, 9, 0xFF)}, 0},
1629 {{SLP, SLP_TIMEOUT(50), SLP_TIMEOUT(25), SLP_VEC(2, 7, 9, 9, 0xFF)}, 0},
1630 {{SLP, SLP_TIMEOUT(25), SLP_TIMEOUT(25),
1631 SLP_VEC(4, 7, 10, 10, 0xFF)}, 0}
1632};
1633
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001634int iwl4965_power_init_handle(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001635{
1636 int rc = 0, i;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001637 struct iwl4965_power_mgr *pow_data;
1638 int size = sizeof(struct iwl4965_power_vec_entry) * IWL_POWER_AC;
Zhu Yib481de92007-09-25 17:54:57 -07001639 u16 pci_pm;
1640
1641 IWL_DEBUG_POWER("Initialize power \n");
1642
1643 pow_data = &(priv->power_data);
1644
1645 memset(pow_data, 0, sizeof(*pow_data));
1646
1647 pow_data->active_index = IWL_POWER_RANGE_0;
1648 pow_data->dtim_val = 0xffff;
1649
1650 memcpy(&pow_data->pwr_range_0[0], &range_0[0], size);
1651 memcpy(&pow_data->pwr_range_1[0], &range_1[0], size);
1652
1653 rc = pci_read_config_word(priv->pci_dev, PCI_LINK_CTRL, &pci_pm);
1654 if (rc != 0)
1655 return 0;
1656 else {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001657 struct iwl4965_powertable_cmd *cmd;
Zhu Yib481de92007-09-25 17:54:57 -07001658
1659 IWL_DEBUG_POWER("adjust power command flags\n");
1660
1661 for (i = 0; i < IWL_POWER_AC; i++) {
1662 cmd = &pow_data->pwr_range_0[i].cmd;
1663
1664 if (pci_pm & 0x1)
1665 cmd->flags &= ~IWL_POWER_PCI_PM_MSK;
1666 else
1667 cmd->flags |= IWL_POWER_PCI_PM_MSK;
1668 }
1669 }
1670 return rc;
1671}
1672
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001673static int iwl4965_update_power_cmd(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001674 struct iwl4965_powertable_cmd *cmd, u32 mode)
Zhu Yib481de92007-09-25 17:54:57 -07001675{
1676 int rc = 0, i;
1677 u8 skip;
1678 u32 max_sleep = 0;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001679 struct iwl4965_power_vec_entry *range;
Zhu Yib481de92007-09-25 17:54:57 -07001680 u8 period = 0;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001681 struct iwl4965_power_mgr *pow_data;
Zhu Yib481de92007-09-25 17:54:57 -07001682
1683 if (mode > IWL_POWER_INDEX_5) {
1684 IWL_DEBUG_POWER("Error invalid power mode \n");
1685 return -1;
1686 }
1687 pow_data = &(priv->power_data);
1688
1689 if (pow_data->active_index == IWL_POWER_RANGE_0)
1690 range = &pow_data->pwr_range_0[0];
1691 else
1692 range = &pow_data->pwr_range_1[1];
1693
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001694 memcpy(cmd, &range[mode].cmd, sizeof(struct iwl4965_powertable_cmd));
Zhu Yib481de92007-09-25 17:54:57 -07001695
1696#ifdef IWL_MAC80211_DISABLE
1697 if (priv->assoc_network != NULL) {
1698 unsigned long flags;
1699
1700 period = priv->assoc_network->tim.tim_period;
1701 }
1702#endif /*IWL_MAC80211_DISABLE */
1703 skip = range[mode].no_dtim;
1704
1705 if (period == 0) {
1706 period = 1;
1707 skip = 0;
1708 }
1709
1710 if (skip == 0) {
1711 max_sleep = period;
1712 cmd->flags &= ~IWL_POWER_SLEEP_OVER_DTIM_MSK;
1713 } else {
1714 __le32 slp_itrvl = cmd->sleep_interval[IWL_POWER_VEC_SIZE - 1];
1715 max_sleep = (le32_to_cpu(slp_itrvl) / period) * period;
1716 cmd->flags |= IWL_POWER_SLEEP_OVER_DTIM_MSK;
1717 }
1718
1719 for (i = 0; i < IWL_POWER_VEC_SIZE; i++) {
1720 if (le32_to_cpu(cmd->sleep_interval[i]) > max_sleep)
1721 cmd->sleep_interval[i] = cpu_to_le32(max_sleep);
1722 }
1723
1724 IWL_DEBUG_POWER("Flags value = 0x%08X\n", cmd->flags);
1725 IWL_DEBUG_POWER("Tx timeout = %u\n", le32_to_cpu(cmd->tx_data_timeout));
1726 IWL_DEBUG_POWER("Rx timeout = %u\n", le32_to_cpu(cmd->rx_data_timeout));
1727 IWL_DEBUG_POWER("Sleep interval vector = { %d , %d , %d , %d , %d }\n",
1728 le32_to_cpu(cmd->sleep_interval[0]),
1729 le32_to_cpu(cmd->sleep_interval[1]),
1730 le32_to_cpu(cmd->sleep_interval[2]),
1731 le32_to_cpu(cmd->sleep_interval[3]),
1732 le32_to_cpu(cmd->sleep_interval[4]));
1733
1734 return rc;
1735}
1736
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001737static int iwl4965_send_power_mode(struct iwl_priv *priv, u32 mode)
Zhu Yib481de92007-09-25 17:54:57 -07001738{
John W. Linville9a62f732007-11-15 16:27:36 -05001739 u32 uninitialized_var(final_mode);
Zhu Yib481de92007-09-25 17:54:57 -07001740 int rc;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001741 struct iwl4965_powertable_cmd cmd;
Zhu Yib481de92007-09-25 17:54:57 -07001742
1743 /* If on battery, set to 3,
Ian Schram01ebd062007-10-25 17:15:22 +08001744 * if plugged into AC power, set to CAM ("continuously aware mode"),
Zhu Yib481de92007-09-25 17:54:57 -07001745 * else user level */
1746 switch (mode) {
1747 case IWL_POWER_BATTERY:
1748 final_mode = IWL_POWER_INDEX_3;
1749 break;
1750 case IWL_POWER_AC:
1751 final_mode = IWL_POWER_MODE_CAM;
1752 break;
1753 default:
1754 final_mode = mode;
1755 break;
1756 }
1757
1758 cmd.keep_alive_beacons = 0;
1759
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001760 iwl4965_update_power_cmd(priv, &cmd, final_mode);
Zhu Yib481de92007-09-25 17:54:57 -07001761
Tomas Winkler857485c2008-03-21 13:53:44 -07001762 rc = iwl_send_cmd_pdu(priv, POWER_TABLE_CMD, sizeof(cmd), &cmd);
Zhu Yib481de92007-09-25 17:54:57 -07001763
1764 if (final_mode == IWL_POWER_MODE_CAM)
1765 clear_bit(STATUS_POWER_PMI, &priv->status);
1766 else
1767 set_bit(STATUS_POWER_PMI, &priv->status);
1768
1769 return rc;
1770}
1771
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001772int iwl4965_is_network_packet(struct iwl_priv *priv, struct ieee80211_hdr *header)
Zhu Yib481de92007-09-25 17:54:57 -07001773{
1774 /* Filter incoming packets to determine if they are targeted toward
1775 * this network, discarding packets coming from ourselves */
1776 switch (priv->iw_mode) {
1777 case IEEE80211_IF_TYPE_IBSS: /* Header: Dest. | Source | BSSID */
1778 /* packets from our adapter are dropped (echo) */
1779 if (!compare_ether_addr(header->addr2, priv->mac_addr))
1780 return 0;
1781 /* {broad,multi}cast packets to our IBSS go through */
1782 if (is_multicast_ether_addr(header->addr1))
1783 return !compare_ether_addr(header->addr3, priv->bssid);
1784 /* packets to our adapter go through */
1785 return !compare_ether_addr(header->addr1, priv->mac_addr);
1786 case IEEE80211_IF_TYPE_STA: /* Header: Dest. | AP{BSSID} | Source */
1787 /* packets from our adapter are dropped (echo) */
1788 if (!compare_ether_addr(header->addr3, priv->mac_addr))
1789 return 0;
1790 /* {broad,multi}cast packets to our BSS go through */
1791 if (is_multicast_ether_addr(header->addr1))
1792 return !compare_ether_addr(header->addr2, priv->bssid);
1793 /* packets to our adapter go through */
1794 return !compare_ether_addr(header->addr1, priv->mac_addr);
1795 }
1796
1797 return 1;
1798}
1799
1800#define TX_STATUS_ENTRY(x) case TX_STATUS_FAIL_ ## x: return #x
1801
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001802static const char *iwl4965_get_tx_fail_reason(u32 status)
Zhu Yib481de92007-09-25 17:54:57 -07001803{
1804 switch (status & TX_STATUS_MSK) {
1805 case TX_STATUS_SUCCESS:
1806 return "SUCCESS";
1807 TX_STATUS_ENTRY(SHORT_LIMIT);
1808 TX_STATUS_ENTRY(LONG_LIMIT);
1809 TX_STATUS_ENTRY(FIFO_UNDERRUN);
1810 TX_STATUS_ENTRY(MGMNT_ABORT);
1811 TX_STATUS_ENTRY(NEXT_FRAG);
1812 TX_STATUS_ENTRY(LIFE_EXPIRE);
1813 TX_STATUS_ENTRY(DEST_PS);
1814 TX_STATUS_ENTRY(ABORTED);
1815 TX_STATUS_ENTRY(BT_RETRY);
1816 TX_STATUS_ENTRY(STA_INVALID);
1817 TX_STATUS_ENTRY(FRAG_DROPPED);
1818 TX_STATUS_ENTRY(TID_DISABLE);
1819 TX_STATUS_ENTRY(FRAME_FLUSHED);
1820 TX_STATUS_ENTRY(INSUFFICIENT_CF_POLL);
1821 TX_STATUS_ENTRY(TX_LOCKED);
1822 TX_STATUS_ENTRY(NO_BEACON_ON_RADAR);
1823 }
1824
1825 return "UNKNOWN";
1826}
1827
1828/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001829 * iwl4965_scan_cancel - Cancel any currently executing HW scan
Zhu Yib481de92007-09-25 17:54:57 -07001830 *
1831 * NOTE: priv->mutex is not required before calling this function
1832 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001833static int iwl4965_scan_cancel(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001834{
1835 if (!test_bit(STATUS_SCAN_HW, &priv->status)) {
1836 clear_bit(STATUS_SCANNING, &priv->status);
1837 return 0;
1838 }
1839
1840 if (test_bit(STATUS_SCANNING, &priv->status)) {
1841 if (!test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
1842 IWL_DEBUG_SCAN("Queuing scan abort.\n");
1843 set_bit(STATUS_SCAN_ABORTING, &priv->status);
1844 queue_work(priv->workqueue, &priv->abort_scan);
1845
1846 } else
1847 IWL_DEBUG_SCAN("Scan abort already in progress.\n");
1848
1849 return test_bit(STATUS_SCANNING, &priv->status);
1850 }
1851
1852 return 0;
1853}
1854
1855/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001856 * iwl4965_scan_cancel_timeout - Cancel any currently executing HW scan
Zhu Yib481de92007-09-25 17:54:57 -07001857 * @ms: amount of time to wait (in milliseconds) for scan to abort
1858 *
1859 * NOTE: priv->mutex must be held before calling this function
1860 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001861static int iwl4965_scan_cancel_timeout(struct iwl_priv *priv, unsigned long ms)
Zhu Yib481de92007-09-25 17:54:57 -07001862{
1863 unsigned long now = jiffies;
1864 int ret;
1865
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001866 ret = iwl4965_scan_cancel(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001867 if (ret && ms) {
1868 mutex_unlock(&priv->mutex);
1869 while (!time_after(jiffies, now + msecs_to_jiffies(ms)) &&
1870 test_bit(STATUS_SCANNING, &priv->status))
1871 msleep(1);
1872 mutex_lock(&priv->mutex);
1873
1874 return test_bit(STATUS_SCANNING, &priv->status);
1875 }
1876
1877 return ret;
1878}
1879
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001880static void iwl4965_sequence_reset(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001881{
1882 /* Reset ieee stats */
1883
1884 /* We don't reset the net_device_stats (ieee->stats) on
1885 * re-association */
1886
1887 priv->last_seq_num = -1;
1888 priv->last_frag_num = -1;
1889 priv->last_packet_time = 0;
1890
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001891 iwl4965_scan_cancel(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001892}
1893
1894#define MAX_UCODE_BEACON_INTERVAL 4096
1895#define INTEL_CONN_LISTEN_INTERVAL __constant_cpu_to_le16(0xA)
1896
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001897static __le16 iwl4965_adjust_beacon_interval(u16 beacon_val)
Zhu Yib481de92007-09-25 17:54:57 -07001898{
1899 u16 new_val = 0;
1900 u16 beacon_factor = 0;
1901
1902 beacon_factor =
1903 (beacon_val + MAX_UCODE_BEACON_INTERVAL)
1904 / MAX_UCODE_BEACON_INTERVAL;
1905 new_val = beacon_val / beacon_factor;
1906
1907 return cpu_to_le16(new_val);
1908}
1909
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001910static void iwl4965_setup_rxon_timing(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001911{
1912 u64 interval_tm_unit;
1913 u64 tsf, result;
1914 unsigned long flags;
1915 struct ieee80211_conf *conf = NULL;
1916 u16 beacon_int = 0;
1917
1918 conf = ieee80211_get_hw_conf(priv->hw);
1919
1920 spin_lock_irqsave(&priv->lock, flags);
1921 priv->rxon_timing.timestamp.dw[1] = cpu_to_le32(priv->timestamp1);
1922 priv->rxon_timing.timestamp.dw[0] = cpu_to_le32(priv->timestamp0);
1923
1924 priv->rxon_timing.listen_interval = INTEL_CONN_LISTEN_INTERVAL;
1925
1926 tsf = priv->timestamp1;
1927 tsf = ((tsf << 32) | priv->timestamp0);
1928
1929 beacon_int = priv->beacon_int;
1930 spin_unlock_irqrestore(&priv->lock, flags);
1931
1932 if (priv->iw_mode == IEEE80211_IF_TYPE_STA) {
1933 if (beacon_int == 0) {
1934 priv->rxon_timing.beacon_interval = cpu_to_le16(100);
1935 priv->rxon_timing.beacon_init_val = cpu_to_le32(102400);
1936 } else {
1937 priv->rxon_timing.beacon_interval =
1938 cpu_to_le16(beacon_int);
1939 priv->rxon_timing.beacon_interval =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001940 iwl4965_adjust_beacon_interval(
Zhu Yib481de92007-09-25 17:54:57 -07001941 le16_to_cpu(priv->rxon_timing.beacon_interval));
1942 }
1943
1944 priv->rxon_timing.atim_window = 0;
1945 } else {
1946 priv->rxon_timing.beacon_interval =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001947 iwl4965_adjust_beacon_interval(conf->beacon_int);
Zhu Yib481de92007-09-25 17:54:57 -07001948 /* TODO: we need to get atim_window from upper stack
1949 * for now we set to 0 */
1950 priv->rxon_timing.atim_window = 0;
1951 }
1952
1953 interval_tm_unit =
1954 (le16_to_cpu(priv->rxon_timing.beacon_interval) * 1024);
1955 result = do_div(tsf, interval_tm_unit);
1956 priv->rxon_timing.beacon_init_val =
1957 cpu_to_le32((u32) ((u64) interval_tm_unit - result));
1958
1959 IWL_DEBUG_ASSOC
1960 ("beacon interval %d beacon timer %d beacon tim %d\n",
1961 le16_to_cpu(priv->rxon_timing.beacon_interval),
1962 le32_to_cpu(priv->rxon_timing.beacon_init_val),
1963 le16_to_cpu(priv->rxon_timing.atim_window));
1964}
1965
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001966static int iwl4965_scan_initiate(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001967{
1968 if (priv->iw_mode == IEEE80211_IF_TYPE_AP) {
1969 IWL_ERROR("APs don't scan.\n");
1970 return 0;
1971 }
1972
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001973 if (!iwl4965_is_ready_rf(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07001974 IWL_DEBUG_SCAN("Aborting scan due to not ready.\n");
1975 return -EIO;
1976 }
1977
1978 if (test_bit(STATUS_SCANNING, &priv->status)) {
1979 IWL_DEBUG_SCAN("Scan already in progress.\n");
1980 return -EAGAIN;
1981 }
1982
1983 if (test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
1984 IWL_DEBUG_SCAN("Scan request while abort pending. "
1985 "Queuing.\n");
1986 return -EAGAIN;
1987 }
1988
1989 IWL_DEBUG_INFO("Starting scan...\n");
1990 priv->scan_bands = 2;
1991 set_bit(STATUS_SCANNING, &priv->status);
1992 priv->scan_start = jiffies;
1993 priv->scan_pass_start = priv->scan_start;
1994
1995 queue_work(priv->workqueue, &priv->request_scan);
1996
1997 return 0;
1998}
1999
Zhu Yib481de92007-09-25 17:54:57 -07002000
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002001static void iwl4965_set_flags_for_phymode(struct iwl_priv *priv,
Johannes Berg8318d782008-01-24 19:38:38 +01002002 enum ieee80211_band band)
Zhu Yib481de92007-09-25 17:54:57 -07002003{
Johannes Berg8318d782008-01-24 19:38:38 +01002004 if (band == IEEE80211_BAND_5GHZ) {
Zhu Yib481de92007-09-25 17:54:57 -07002005 priv->staging_rxon.flags &=
2006 ~(RXON_FLG_BAND_24G_MSK | RXON_FLG_AUTO_DETECT_MSK
2007 | RXON_FLG_CCK_MSK);
2008 priv->staging_rxon.flags |= RXON_FLG_SHORT_SLOT_MSK;
2009 } else {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002010 /* Copied from iwl4965_bg_post_associate() */
Zhu Yib481de92007-09-25 17:54:57 -07002011 if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_SLOT_TIME)
2012 priv->staging_rxon.flags |= RXON_FLG_SHORT_SLOT_MSK;
2013 else
2014 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
2015
2016 if (priv->iw_mode == IEEE80211_IF_TYPE_IBSS)
2017 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
2018
2019 priv->staging_rxon.flags |= RXON_FLG_BAND_24G_MSK;
2020 priv->staging_rxon.flags |= RXON_FLG_AUTO_DETECT_MSK;
2021 priv->staging_rxon.flags &= ~RXON_FLG_CCK_MSK;
2022 }
2023}
2024
2025/*
Ian Schram01ebd062007-10-25 17:15:22 +08002026 * initialize rxon structure with default values from eeprom
Zhu Yib481de92007-09-25 17:54:57 -07002027 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002028static void iwl4965_connection_init_rx_config(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002029{
Assaf Kraussbf85ea42008-03-14 10:38:49 -07002030 const struct iwl_channel_info *ch_info;
Zhu Yib481de92007-09-25 17:54:57 -07002031
2032 memset(&priv->staging_rxon, 0, sizeof(priv->staging_rxon));
2033
2034 switch (priv->iw_mode) {
2035 case IEEE80211_IF_TYPE_AP:
2036 priv->staging_rxon.dev_type = RXON_DEV_TYPE_AP;
2037 break;
2038
2039 case IEEE80211_IF_TYPE_STA:
2040 priv->staging_rxon.dev_type = RXON_DEV_TYPE_ESS;
2041 priv->staging_rxon.filter_flags = RXON_FILTER_ACCEPT_GRP_MSK;
2042 break;
2043
2044 case IEEE80211_IF_TYPE_IBSS:
2045 priv->staging_rxon.dev_type = RXON_DEV_TYPE_IBSS;
2046 priv->staging_rxon.flags = RXON_FLG_SHORT_PREAMBLE_MSK;
2047 priv->staging_rxon.filter_flags = RXON_FILTER_BCON_AWARE_MSK |
2048 RXON_FILTER_ACCEPT_GRP_MSK;
2049 break;
2050
2051 case IEEE80211_IF_TYPE_MNTR:
2052 priv->staging_rxon.dev_type = RXON_DEV_TYPE_SNIFFER;
2053 priv->staging_rxon.filter_flags = RXON_FILTER_PROMISC_MSK |
2054 RXON_FILTER_CTL2HOST_MSK | RXON_FILTER_ACCEPT_GRP_MSK;
2055 break;
2056 }
2057
2058#if 0
2059 /* TODO: Figure out when short_preamble would be set and cache from
2060 * that */
2061 if (!hw_to_local(priv->hw)->short_preamble)
2062 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_PREAMBLE_MSK;
2063 else
2064 priv->staging_rxon.flags |= RXON_FLG_SHORT_PREAMBLE_MSK;
2065#endif
2066
Assaf Krauss8622e702008-03-21 13:53:43 -07002067 ch_info = iwl_get_channel_info(priv, priv->band,
Zhu Yib481de92007-09-25 17:54:57 -07002068 le16_to_cpu(priv->staging_rxon.channel));
2069
2070 if (!ch_info)
2071 ch_info = &priv->channel_info[0];
2072
2073 /*
2074 * in some case A channels are all non IBSS
2075 * in this case force B/G channel
2076 */
2077 if ((priv->iw_mode == IEEE80211_IF_TYPE_IBSS) &&
2078 !(is_channel_ibss(ch_info)))
2079 ch_info = &priv->channel_info[0];
2080
2081 priv->staging_rxon.channel = cpu_to_le16(ch_info->channel);
Johannes Berg8318d782008-01-24 19:38:38 +01002082 priv->band = ch_info->band;
Zhu Yib481de92007-09-25 17:54:57 -07002083
Johannes Berg8318d782008-01-24 19:38:38 +01002084 iwl4965_set_flags_for_phymode(priv, priv->band);
Zhu Yib481de92007-09-25 17:54:57 -07002085
2086 priv->staging_rxon.ofdm_basic_rates =
2087 (IWL_OFDM_RATES_MASK >> IWL_FIRST_OFDM_RATE) & 0xFF;
2088 priv->staging_rxon.cck_basic_rates =
2089 (IWL_CCK_RATES_MASK >> IWL_FIRST_CCK_RATE) & 0xF;
2090
2091 priv->staging_rxon.flags &= ~(RXON_FLG_CHANNEL_MODE_MIXED_MSK |
2092 RXON_FLG_CHANNEL_MODE_PURE_40_MSK);
2093 memcpy(priv->staging_rxon.node_addr, priv->mac_addr, ETH_ALEN);
2094 memcpy(priv->staging_rxon.wlap_bssid_addr, priv->mac_addr, ETH_ALEN);
2095 priv->staging_rxon.ofdm_ht_single_stream_basic_rates = 0xff;
2096 priv->staging_rxon.ofdm_ht_dual_stream_basic_rates = 0xff;
2097 iwl4965_set_rxon_chain(priv);
2098}
2099
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002100static int iwl4965_set_mode(struct iwl_priv *priv, int mode)
Zhu Yib481de92007-09-25 17:54:57 -07002101{
Zhu Yib481de92007-09-25 17:54:57 -07002102 if (mode == IEEE80211_IF_TYPE_IBSS) {
Assaf Kraussbf85ea42008-03-14 10:38:49 -07002103 const struct iwl_channel_info *ch_info;
Zhu Yib481de92007-09-25 17:54:57 -07002104
Assaf Krauss8622e702008-03-21 13:53:43 -07002105 ch_info = iwl_get_channel_info(priv,
Johannes Berg8318d782008-01-24 19:38:38 +01002106 priv->band,
Zhu Yib481de92007-09-25 17:54:57 -07002107 le16_to_cpu(priv->staging_rxon.channel));
2108
2109 if (!ch_info || !is_channel_ibss(ch_info)) {
2110 IWL_ERROR("channel %d not IBSS channel\n",
2111 le16_to_cpu(priv->staging_rxon.channel));
2112 return -EINVAL;
2113 }
2114 }
2115
Zhu Yib481de92007-09-25 17:54:57 -07002116 priv->iw_mode = mode;
2117
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002118 iwl4965_connection_init_rx_config(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002119 memcpy(priv->staging_rxon.node_addr, priv->mac_addr, ETH_ALEN);
2120
Assaf Kraussbf85ea42008-03-14 10:38:49 -07002121 iwlcore_clear_stations_table(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002122
Mohamed Abbasfde35712007-11-29 11:10:15 +08002123 /* dont commit rxon if rf-kill is on*/
2124 if (!iwl4965_is_ready_rf(priv))
2125 return -EAGAIN;
2126
2127 cancel_delayed_work(&priv->scan_check);
2128 if (iwl4965_scan_cancel_timeout(priv, 100)) {
2129 IWL_WARNING("Aborted scan still in progress after 100ms\n");
2130 IWL_DEBUG_MAC80211("leaving - scan abort failed.\n");
2131 return -EAGAIN;
2132 }
2133
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002134 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002135
2136 return 0;
2137}
2138
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002139static void iwl4965_build_tx_cmd_hwcrypto(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07002140 struct ieee80211_tx_control *ctl,
Tomas Winkler857485c2008-03-21 13:53:44 -07002141 struct iwl_cmd *cmd,
Zhu Yib481de92007-09-25 17:54:57 -07002142 struct sk_buff *skb_frag,
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07002143 int sta_id)
Zhu Yib481de92007-09-25 17:54:57 -07002144{
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07002145 struct iwl4965_hw_key *keyinfo = &priv->stations[sta_id].keyinfo;
Zhu Yib481de92007-09-25 17:54:57 -07002146
2147 switch (keyinfo->alg) {
2148 case ALG_CCMP:
2149 cmd->cmd.tx.sec_ctl = TX_CMD_SEC_CCM;
2150 memcpy(cmd->cmd.tx.key, keyinfo->key, keyinfo->keylen);
Max Stepanov8236e182008-03-12 16:58:48 -07002151 if (ctl->flags & IEEE80211_TXCTL_AMPDU)
2152 cmd->cmd.tx.tx_flags |= TX_CMD_FLG_AGG_CCMP_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07002153 IWL_DEBUG_TX("tx_cmd with aes hwcrypto\n");
2154 break;
2155
2156 case ALG_TKIP:
Zhu Yib481de92007-09-25 17:54:57 -07002157 cmd->cmd.tx.sec_ctl = TX_CMD_SEC_TKIP;
Emmanuel Grumbach2bc75082008-03-19 16:41:45 -07002158 ieee80211_get_tkip_key(keyinfo->conf, skb_frag,
2159 IEEE80211_TKIP_P2_KEY, cmd->cmd.tx.key);
2160 IWL_DEBUG_TX("tx_cmd with tkip hwcrypto\n");
Zhu Yib481de92007-09-25 17:54:57 -07002161 break;
2162
2163 case ALG_WEP:
2164 cmd->cmd.tx.sec_ctl = TX_CMD_SEC_WEP |
2165 (ctl->key_idx & TX_CMD_SEC_MSK) << TX_CMD_SEC_SHIFT;
2166
2167 if (keyinfo->keylen == 13)
2168 cmd->cmd.tx.sec_ctl |= TX_CMD_SEC_KEY128;
2169
2170 memcpy(&cmd->cmd.tx.key[3], keyinfo->key, keyinfo->keylen);
2171
2172 IWL_DEBUG_TX("Configuring packet for WEP encryption "
2173 "with key %d\n", ctl->key_idx);
2174 break;
2175
Zhu Yib481de92007-09-25 17:54:57 -07002176 default:
2177 printk(KERN_ERR "Unknown encode alg %d\n", keyinfo->alg);
2178 break;
2179 }
2180}
2181
2182/*
2183 * handle build REPLY_TX command notification.
2184 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002185static void iwl4965_build_tx_cmd_basic(struct iwl_priv *priv,
Tomas Winkler857485c2008-03-21 13:53:44 -07002186 struct iwl_cmd *cmd,
Zhu Yib481de92007-09-25 17:54:57 -07002187 struct ieee80211_tx_control *ctrl,
2188 struct ieee80211_hdr *hdr,
2189 int is_unicast, u8 std_id)
2190{
2191 __le16 *qc;
2192 u16 fc = le16_to_cpu(hdr->frame_control);
2193 __le32 tx_flags = cmd->cmd.tx.tx_flags;
2194
2195 cmd->cmd.tx.stop_time.life_time = TX_CMD_LIFE_TIME_INFINITE;
2196 if (!(ctrl->flags & IEEE80211_TXCTL_NO_ACK)) {
2197 tx_flags |= TX_CMD_FLG_ACK_MSK;
2198 if ((fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_MGMT)
2199 tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
2200 if (ieee80211_is_probe_response(fc) &&
2201 !(le16_to_cpu(hdr->seq_ctrl) & 0xf))
2202 tx_flags |= TX_CMD_FLG_TSF_MSK;
2203 } else {
2204 tx_flags &= (~TX_CMD_FLG_ACK_MSK);
2205 tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
2206 }
2207
Tomas Winkler87e4f7d2008-01-14 17:46:16 -08002208 if (ieee80211_is_back_request(fc))
2209 tx_flags |= TX_CMD_FLG_ACK_MSK | TX_CMD_FLG_IMM_BA_RSP_MASK;
2210
2211
Zhu Yib481de92007-09-25 17:54:57 -07002212 cmd->cmd.tx.sta_id = std_id;
2213 if (ieee80211_get_morefrag(hdr))
2214 tx_flags |= TX_CMD_FLG_MORE_FRAG_MSK;
2215
2216 qc = ieee80211_get_qos_ctrl(hdr);
2217 if (qc) {
2218 cmd->cmd.tx.tid_tspec = (u8) (le16_to_cpu(*qc) & 0xf);
2219 tx_flags &= ~TX_CMD_FLG_SEQ_CTL_MSK;
2220 } else
2221 tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
2222
2223 if (ctrl->flags & IEEE80211_TXCTL_USE_RTS_CTS) {
2224 tx_flags |= TX_CMD_FLG_RTS_MSK;
2225 tx_flags &= ~TX_CMD_FLG_CTS_MSK;
2226 } else if (ctrl->flags & IEEE80211_TXCTL_USE_CTS_PROTECT) {
2227 tx_flags &= ~TX_CMD_FLG_RTS_MSK;
2228 tx_flags |= TX_CMD_FLG_CTS_MSK;
2229 }
2230
2231 if ((tx_flags & TX_CMD_FLG_RTS_MSK) || (tx_flags & TX_CMD_FLG_CTS_MSK))
2232 tx_flags |= TX_CMD_FLG_FULL_TXOP_PROT_MSK;
2233
2234 tx_flags &= ~(TX_CMD_FLG_ANT_SEL_MSK);
2235 if ((fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_MGMT) {
2236 if ((fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_ASSOC_REQ ||
2237 (fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_REASSOC_REQ)
Ian Schrambc434dd2007-10-25 17:15:29 +08002238 cmd->cmd.tx.timeout.pm_frame_timeout = cpu_to_le16(3);
Zhu Yib481de92007-09-25 17:54:57 -07002239 else
Ian Schrambc434dd2007-10-25 17:15:29 +08002240 cmd->cmd.tx.timeout.pm_frame_timeout = cpu_to_le16(2);
Mohamed Abbasab53d8a2008-03-25 16:33:36 -07002241 } else {
Zhu Yib481de92007-09-25 17:54:57 -07002242 cmd->cmd.tx.timeout.pm_frame_timeout = 0;
Mohamed Abbasab53d8a2008-03-25 16:33:36 -07002243 }
Zhu Yib481de92007-09-25 17:54:57 -07002244
2245 cmd->cmd.tx.driver_txop = 0;
2246 cmd->cmd.tx.tx_flags = tx_flags;
2247 cmd->cmd.tx.next_frame_len = 0;
2248}
Tomas Winkler19758be2008-03-12 16:58:51 -07002249static void iwl_update_tx_stats(struct iwl_priv *priv, u16 fc, u16 len)
2250{
2251 /* 0 - mgmt, 1 - cnt, 2 - data */
2252 int idx = (fc & IEEE80211_FCTL_FTYPE) >> 2;
2253 priv->tx_stats[idx].cnt++;
2254 priv->tx_stats[idx].bytes += len;
2255}
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002256/**
2257 * iwl4965_get_sta_id - Find station's index within station table
2258 *
2259 * If new IBSS station, create new entry in station table
2260 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002261static int iwl4965_get_sta_id(struct iwl_priv *priv,
Ben Cahill9fbab512007-11-29 11:09:47 +08002262 struct ieee80211_hdr *hdr)
Zhu Yib481de92007-09-25 17:54:57 -07002263{
2264 int sta_id;
2265 u16 fc = le16_to_cpu(hdr->frame_control);
Joe Perches0795af52007-10-03 17:59:30 -07002266 DECLARE_MAC_BUF(mac);
Zhu Yib481de92007-09-25 17:54:57 -07002267
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002268 /* If this frame is broadcast or management, use broadcast station id */
Zhu Yib481de92007-09-25 17:54:57 -07002269 if (((fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_DATA) ||
2270 is_multicast_ether_addr(hdr->addr1))
2271 return priv->hw_setting.bcast_sta_id;
2272
2273 switch (priv->iw_mode) {
2274
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002275 /* If we are a client station in a BSS network, use the special
2276 * AP station entry (that's the only station we communicate with) */
Zhu Yib481de92007-09-25 17:54:57 -07002277 case IEEE80211_IF_TYPE_STA:
2278 return IWL_AP_ID;
2279
2280 /* If we are an AP, then find the station, or use BCAST */
2281 case IEEE80211_IF_TYPE_AP:
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002282 sta_id = iwl4965_hw_find_station(priv, hdr->addr1);
Zhu Yib481de92007-09-25 17:54:57 -07002283 if (sta_id != IWL_INVALID_STATION)
2284 return sta_id;
2285 return priv->hw_setting.bcast_sta_id;
2286
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002287 /* If this frame is going out to an IBSS network, find the station,
2288 * or create a new station table entry */
Zhu Yib481de92007-09-25 17:54:57 -07002289 case IEEE80211_IF_TYPE_IBSS:
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002290 sta_id = iwl4965_hw_find_station(priv, hdr->addr1);
Zhu Yib481de92007-09-25 17:54:57 -07002291 if (sta_id != IWL_INVALID_STATION)
2292 return sta_id;
2293
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002294 /* Create new station table entry */
Ron Rindjunsky67d62032007-11-26 16:14:40 +02002295 sta_id = iwl4965_add_station_flags(priv, hdr->addr1,
2296 0, CMD_ASYNC, NULL);
Zhu Yib481de92007-09-25 17:54:57 -07002297
2298 if (sta_id != IWL_INVALID_STATION)
2299 return sta_id;
2300
Joe Perches0795af52007-10-03 17:59:30 -07002301 IWL_DEBUG_DROP("Station %s not in station map. "
Zhu Yib481de92007-09-25 17:54:57 -07002302 "Defaulting to broadcast...\n",
Joe Perches0795af52007-10-03 17:59:30 -07002303 print_mac(mac, hdr->addr1));
Tomas Winkler0a6857e2008-03-12 16:58:49 -07002304 iwl_print_hex_dump(IWL_DL_DROP, (u8 *) hdr, sizeof(*hdr));
Zhu Yib481de92007-09-25 17:54:57 -07002305 return priv->hw_setting.bcast_sta_id;
2306
2307 default:
Ian Schram01ebd062007-10-25 17:15:22 +08002308 IWL_WARNING("Unknown mode of operation: %d", priv->iw_mode);
Zhu Yib481de92007-09-25 17:54:57 -07002309 return priv->hw_setting.bcast_sta_id;
2310 }
2311}
2312
2313/*
2314 * start REPLY_TX command process
2315 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002316static int iwl4965_tx_skb(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07002317 struct sk_buff *skb, struct ieee80211_tx_control *ctl)
2318{
2319 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002320 struct iwl4965_tfd_frame *tfd;
Zhu Yib481de92007-09-25 17:54:57 -07002321 u32 *control_flags;
2322 int txq_id = ctl->queue;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002323 struct iwl4965_tx_queue *txq = NULL;
2324 struct iwl4965_queue *q = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07002325 dma_addr_t phys_addr;
2326 dma_addr_t txcmd_phys;
Tomas Winkler87e4f7d2008-01-14 17:46:16 -08002327 dma_addr_t scratch_phys;
Tomas Winkler857485c2008-03-21 13:53:44 -07002328 struct iwl_cmd *out_cmd = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07002329 u16 len, idx, len_org;
2330 u8 id, hdr_len, unicast;
2331 u8 sta_id;
2332 u16 seq_number = 0;
2333 u16 fc;
2334 __le16 *qc;
2335 u8 wait_write_ptr = 0;
2336 unsigned long flags;
2337 int rc;
2338
2339 spin_lock_irqsave(&priv->lock, flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002340 if (iwl4965_is_rfkill(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07002341 IWL_DEBUG_DROP("Dropping - RF KILL\n");
2342 goto drop_unlock;
2343 }
2344
Johannes Berg32bfd352007-12-19 01:31:26 +01002345 if (!priv->vif) {
2346 IWL_DEBUG_DROP("Dropping - !priv->vif\n");
Zhu Yib481de92007-09-25 17:54:57 -07002347 goto drop_unlock;
2348 }
2349
Johannes Berg8318d782008-01-24 19:38:38 +01002350 if ((ctl->tx_rate->hw_value & 0xFF) == IWL_INVALID_RATE) {
Zhu Yib481de92007-09-25 17:54:57 -07002351 IWL_ERROR("ERROR: No TX rate available.\n");
2352 goto drop_unlock;
2353 }
2354
2355 unicast = !is_multicast_ether_addr(hdr->addr1);
2356 id = 0;
2357
2358 fc = le16_to_cpu(hdr->frame_control);
2359
Tomas Winkler0a6857e2008-03-12 16:58:49 -07002360#ifdef CONFIG_IWLWIFI_DEBUG
Zhu Yib481de92007-09-25 17:54:57 -07002361 if (ieee80211_is_auth(fc))
2362 IWL_DEBUG_TX("Sending AUTH frame\n");
2363 else if (ieee80211_is_assoc_request(fc))
2364 IWL_DEBUG_TX("Sending ASSOC frame\n");
2365 else if (ieee80211_is_reassoc_request(fc))
2366 IWL_DEBUG_TX("Sending REASSOC frame\n");
2367#endif
2368
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08002369 /* drop all data frame if we are not associated */
Gregory Greenman76f39152008-01-23 10:15:21 -08002370 if (((fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_DATA) &&
2371 (!iwl4965_is_associated(priv) ||
Reinette Chatrea6477242008-02-14 10:40:28 -08002372 ((priv->iw_mode == IEEE80211_IF_TYPE_STA) && !priv->assoc_id) ||
Gregory Greenman76f39152008-01-23 10:15:21 -08002373 !priv->assoc_station_added)) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002374 IWL_DEBUG_DROP("Dropping - !iwl4965_is_associated\n");
Zhu Yib481de92007-09-25 17:54:57 -07002375 goto drop_unlock;
2376 }
2377
2378 spin_unlock_irqrestore(&priv->lock, flags);
2379
2380 hdr_len = ieee80211_get_hdrlen(fc);
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002381
2382 /* Find (or create) index into station table for destination station */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002383 sta_id = iwl4965_get_sta_id(priv, hdr);
Zhu Yib481de92007-09-25 17:54:57 -07002384 if (sta_id == IWL_INVALID_STATION) {
Joe Perches0795af52007-10-03 17:59:30 -07002385 DECLARE_MAC_BUF(mac);
2386
2387 IWL_DEBUG_DROP("Dropping - INVALID STATION: %s\n",
2388 print_mac(mac, hdr->addr1));
Zhu Yib481de92007-09-25 17:54:57 -07002389 goto drop;
2390 }
2391
2392 IWL_DEBUG_RATE("station Id %d\n", sta_id);
2393
2394 qc = ieee80211_get_qos_ctrl(hdr);
2395 if (qc) {
2396 u8 tid = (u8)(le16_to_cpu(*qc) & 0xf);
2397 seq_number = priv->stations[sta_id].tid[tid].seq_number &
2398 IEEE80211_SCTL_SEQ;
2399 hdr->seq_ctrl = cpu_to_le16(seq_number) |
2400 (hdr->seq_ctrl &
2401 __constant_cpu_to_le16(IEEE80211_SCTL_FRAG));
2402 seq_number += 0x10;
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08002403#ifdef CONFIG_IWL4965_HT
Zhu Yib481de92007-09-25 17:54:57 -07002404 /* aggregation is on for this <sta,tid> */
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02002405 if (ctl->flags & IEEE80211_TXCTL_AMPDU)
Zhu Yib481de92007-09-25 17:54:57 -07002406 txq_id = priv->stations[sta_id].tid[tid].agg.txq_id;
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02002407 priv->stations[sta_id].tid[tid].tfds_in_queue++;
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08002408#endif /* CONFIG_IWL4965_HT */
Zhu Yib481de92007-09-25 17:54:57 -07002409 }
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002410
2411 /* Descriptor for chosen Tx queue */
Zhu Yib481de92007-09-25 17:54:57 -07002412 txq = &priv->txq[txq_id];
2413 q = &txq->q;
2414
2415 spin_lock_irqsave(&priv->lock, flags);
2416
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002417 /* Set up first empty TFD within this queue's circular TFD buffer */
Tomas Winklerfc4b6852007-10-25 17:15:24 +08002418 tfd = &txq->bd[q->write_ptr];
Zhu Yib481de92007-09-25 17:54:57 -07002419 memset(tfd, 0, sizeof(*tfd));
2420 control_flags = (u32 *) tfd;
Tomas Winklerfc4b6852007-10-25 17:15:24 +08002421 idx = get_cmd_index(q, q->write_ptr, 0);
Zhu Yib481de92007-09-25 17:54:57 -07002422
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002423 /* Set up driver data for this TFD */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002424 memset(&(txq->txb[q->write_ptr]), 0, sizeof(struct iwl4965_tx_info));
Tomas Winklerfc4b6852007-10-25 17:15:24 +08002425 txq->txb[q->write_ptr].skb[0] = skb;
2426 memcpy(&(txq->txb[q->write_ptr].status.control),
Zhu Yib481de92007-09-25 17:54:57 -07002427 ctl, sizeof(struct ieee80211_tx_control));
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002428
2429 /* Set up first empty entry in queue's array of Tx/cmd buffers */
Zhu Yib481de92007-09-25 17:54:57 -07002430 out_cmd = &txq->cmd[idx];
2431 memset(&out_cmd->hdr, 0, sizeof(out_cmd->hdr));
2432 memset(&out_cmd->cmd.tx, 0, sizeof(out_cmd->cmd.tx));
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002433
2434 /*
2435 * Set up the Tx-command (not MAC!) header.
2436 * Store the chosen Tx queue and TFD index within the sequence field;
2437 * after Tx, uCode's Tx response will return this value so driver can
2438 * locate the frame within the tx queue and do post-tx processing.
2439 */
Zhu Yib481de92007-09-25 17:54:57 -07002440 out_cmd->hdr.cmd = REPLY_TX;
2441 out_cmd->hdr.sequence = cpu_to_le16((u16)(QUEUE_TO_SEQ(txq_id) |
Tomas Winklerfc4b6852007-10-25 17:15:24 +08002442 INDEX_TO_SEQ(q->write_ptr)));
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002443
2444 /* Copy MAC header from skb into command buffer */
Zhu Yib481de92007-09-25 17:54:57 -07002445 memcpy(out_cmd->cmd.tx.hdr, hdr, hdr_len);
2446
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002447 /*
2448 * Use the first empty entry in this queue's command buffer array
2449 * to contain the Tx command and MAC header concatenated together
2450 * (payload data will be in another buffer).
2451 * Size of this varies, due to varying MAC header length.
2452 * If end is not dword aligned, we'll have 2 extra bytes at the end
2453 * of the MAC header (device reads on dword boundaries).
2454 * We'll tell device about this padding later.
2455 */
Zhu Yib481de92007-09-25 17:54:57 -07002456 len = priv->hw_setting.tx_cmd_len +
Tomas Winkler857485c2008-03-21 13:53:44 -07002457 sizeof(struct iwl_cmd_header) + hdr_len;
Zhu Yib481de92007-09-25 17:54:57 -07002458
2459 len_org = len;
2460 len = (len + 3) & ~3;
2461
2462 if (len_org != len)
2463 len_org = 1;
2464 else
2465 len_org = 0;
2466
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002467 /* Physical address of this Tx command's header (not MAC header!),
2468 * within command buffer array. */
Tomas Winkler857485c2008-03-21 13:53:44 -07002469 txcmd_phys = txq->dma_addr_cmd + sizeof(struct iwl_cmd) * idx +
2470 offsetof(struct iwl_cmd, hdr);
Zhu Yib481de92007-09-25 17:54:57 -07002471
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002472 /* Add buffer containing Tx command and MAC(!) header to TFD's
2473 * first entry */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002474 iwl4965_hw_txq_attach_buf_to_tfd(priv, tfd, txcmd_phys, len);
Zhu Yib481de92007-09-25 17:54:57 -07002475
2476 if (!(ctl->flags & IEEE80211_TXCTL_DO_NOT_ENCRYPT))
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07002477 iwl4965_build_tx_cmd_hwcrypto(priv, ctl, out_cmd, skb, sta_id);
Zhu Yib481de92007-09-25 17:54:57 -07002478
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002479 /* Set up TFD's 2nd entry to point directly to remainder of skb,
2480 * if any (802.11 null frames have no payload). */
Zhu Yib481de92007-09-25 17:54:57 -07002481 len = skb->len - hdr_len;
2482 if (len) {
2483 phys_addr = pci_map_single(priv->pci_dev, skb->data + hdr_len,
2484 len, PCI_DMA_TODEVICE);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002485 iwl4965_hw_txq_attach_buf_to_tfd(priv, tfd, phys_addr, len);
Zhu Yib481de92007-09-25 17:54:57 -07002486 }
2487
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002488 /* Tell 4965 about any 2-byte padding after MAC header */
Zhu Yib481de92007-09-25 17:54:57 -07002489 if (len_org)
2490 out_cmd->cmd.tx.tx_flags |= TX_CMD_FLG_MH_PAD_MSK;
2491
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002492 /* Total # bytes to be transmitted */
Zhu Yib481de92007-09-25 17:54:57 -07002493 len = (u16)skb->len;
2494 out_cmd->cmd.tx.len = cpu_to_le16(len);
2495
2496 /* TODO need this for burst mode later on */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002497 iwl4965_build_tx_cmd_basic(priv, out_cmd, ctl, hdr, unicast, sta_id);
Zhu Yib481de92007-09-25 17:54:57 -07002498
2499 /* set is_hcca to 0; it probably will never be implemented */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002500 iwl4965_hw_build_tx_cmd_rate(priv, out_cmd, ctl, hdr, sta_id, 0);
Zhu Yib481de92007-09-25 17:54:57 -07002501
Tomas Winkler19758be2008-03-12 16:58:51 -07002502 iwl_update_tx_stats(priv, fc, len);
2503
Tomas Winkler857485c2008-03-21 13:53:44 -07002504 scratch_phys = txcmd_phys + sizeof(struct iwl_cmd_header) +
Tomas Winkler87e4f7d2008-01-14 17:46:16 -08002505 offsetof(struct iwl4965_tx_cmd, scratch);
2506 out_cmd->cmd.tx.dram_lsb_ptr = cpu_to_le32(scratch_phys);
2507 out_cmd->cmd.tx.dram_msb_ptr = iwl_get_dma_hi_address(scratch_phys);
2508
Zhu Yib481de92007-09-25 17:54:57 -07002509 if (!ieee80211_get_morefrag(hdr)) {
2510 txq->need_update = 1;
2511 if (qc) {
2512 u8 tid = (u8)(le16_to_cpu(*qc) & 0xf);
2513 priv->stations[sta_id].tid[tid].seq_number = seq_number;
2514 }
2515 } else {
2516 wait_write_ptr = 1;
2517 txq->need_update = 0;
2518 }
2519
Tomas Winkler0a6857e2008-03-12 16:58:49 -07002520 iwl_print_hex_dump(IWL_DL_TX, out_cmd->cmd.payload,
Zhu Yib481de92007-09-25 17:54:57 -07002521 sizeof(out_cmd->cmd.tx));
2522
Tomas Winkler0a6857e2008-03-12 16:58:49 -07002523 iwl_print_hex_dump(IWL_DL_TX, (u8 *)out_cmd->cmd.tx.hdr,
Zhu Yib481de92007-09-25 17:54:57 -07002524 ieee80211_get_hdrlen(fc));
2525
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002526 /* Set up entry for this TFD in Tx byte-count array */
Zhu Yib481de92007-09-25 17:54:57 -07002527 iwl4965_tx_queue_update_wr_ptr(priv, txq, len);
2528
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002529 /* Tell device the write index *just past* this latest filled TFD */
Tomas Winklerc54b6792008-03-06 17:36:53 -08002530 q->write_ptr = iwl_queue_inc_wrap(q->write_ptr, q->n_bd);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002531 rc = iwl4965_tx_queue_update_write_ptr(priv, txq);
Zhu Yib481de92007-09-25 17:54:57 -07002532 spin_unlock_irqrestore(&priv->lock, flags);
2533
2534 if (rc)
2535 return rc;
2536
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002537 if ((iwl4965_queue_space(q) < q->high_mark)
Zhu Yib481de92007-09-25 17:54:57 -07002538 && priv->mac80211_registered) {
2539 if (wait_write_ptr) {
2540 spin_lock_irqsave(&priv->lock, flags);
2541 txq->need_update = 1;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002542 iwl4965_tx_queue_update_write_ptr(priv, txq);
Zhu Yib481de92007-09-25 17:54:57 -07002543 spin_unlock_irqrestore(&priv->lock, flags);
2544 }
2545
2546 ieee80211_stop_queue(priv->hw, ctl->queue);
2547 }
2548
2549 return 0;
2550
2551drop_unlock:
2552 spin_unlock_irqrestore(&priv->lock, flags);
2553drop:
2554 return -1;
2555}
2556
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002557static void iwl4965_set_rate(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002558{
Johannes Berg8318d782008-01-24 19:38:38 +01002559 const struct ieee80211_supported_band *hw = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07002560 struct ieee80211_rate *rate;
2561 int i;
2562
Johannes Berg8318d782008-01-24 19:38:38 +01002563 hw = iwl4965_get_hw_mode(priv, priv->band);
Saleem Abdulrasoolc4ba9622007-11-18 23:59:08 -08002564 if (!hw) {
2565 IWL_ERROR("Failed to set rate: unable to get hw mode\n");
2566 return;
2567 }
Zhu Yib481de92007-09-25 17:54:57 -07002568
2569 priv->active_rate = 0;
2570 priv->active_rate_basic = 0;
2571
Johannes Berg8318d782008-01-24 19:38:38 +01002572 for (i = 0; i < hw->n_bitrates; i++) {
2573 rate = &(hw->bitrates[i]);
2574 if (rate->hw_value < IWL_RATE_COUNT)
2575 priv->active_rate |= (1 << rate->hw_value);
Zhu Yib481de92007-09-25 17:54:57 -07002576 }
2577
2578 IWL_DEBUG_RATE("Set active_rate = %0x, active_rate_basic = %0x\n",
2579 priv->active_rate, priv->active_rate_basic);
2580
2581 /*
2582 * If a basic rate is configured, then use it (adding IWL_RATE_1M_MASK)
2583 * otherwise set it to the default of all CCK rates and 6, 12, 24 for
2584 * OFDM
2585 */
2586 if (priv->active_rate_basic & IWL_CCK_BASIC_RATES_MASK)
2587 priv->staging_rxon.cck_basic_rates =
2588 ((priv->active_rate_basic &
2589 IWL_CCK_RATES_MASK) >> IWL_FIRST_CCK_RATE) & 0xF;
2590 else
2591 priv->staging_rxon.cck_basic_rates =
2592 (IWL_CCK_BASIC_RATES_MASK >> IWL_FIRST_CCK_RATE) & 0xF;
2593
2594 if (priv->active_rate_basic & IWL_OFDM_BASIC_RATES_MASK)
2595 priv->staging_rxon.ofdm_basic_rates =
2596 ((priv->active_rate_basic &
2597 (IWL_OFDM_BASIC_RATES_MASK | IWL_RATE_6M_MASK)) >>
2598 IWL_FIRST_OFDM_RATE) & 0xFF;
2599 else
2600 priv->staging_rxon.ofdm_basic_rates =
2601 (IWL_OFDM_BASIC_RATES_MASK >> IWL_FIRST_OFDM_RATE) & 0xFF;
2602}
2603
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002604static void iwl4965_radio_kill_sw(struct iwl_priv *priv, int disable_radio)
Zhu Yib481de92007-09-25 17:54:57 -07002605{
2606 unsigned long flags;
2607
2608 if (!!disable_radio == test_bit(STATUS_RF_KILL_SW, &priv->status))
2609 return;
2610
2611 IWL_DEBUG_RF_KILL("Manual SW RF KILL set to: RADIO %s\n",
2612 disable_radio ? "OFF" : "ON");
2613
2614 if (disable_radio) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002615 iwl4965_scan_cancel(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002616 /* FIXME: This is a workaround for AP */
2617 if (priv->iw_mode != IEEE80211_IF_TYPE_AP) {
2618 spin_lock_irqsave(&priv->lock, flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002619 iwl4965_write32(priv, CSR_UCODE_DRV_GP1_SET,
Zhu Yib481de92007-09-25 17:54:57 -07002620 CSR_UCODE_SW_BIT_RFKILL);
2621 spin_unlock_irqrestore(&priv->lock, flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002622 iwl4965_send_card_state(priv, CARD_STATE_CMD_DISABLE, 0);
Zhu Yib481de92007-09-25 17:54:57 -07002623 set_bit(STATUS_RF_KILL_SW, &priv->status);
2624 }
2625 return;
2626 }
2627
2628 spin_lock_irqsave(&priv->lock, flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002629 iwl4965_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
Zhu Yib481de92007-09-25 17:54:57 -07002630
2631 clear_bit(STATUS_RF_KILL_SW, &priv->status);
2632 spin_unlock_irqrestore(&priv->lock, flags);
2633
2634 /* wake up ucode */
2635 msleep(10);
2636
2637 spin_lock_irqsave(&priv->lock, flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002638 iwl4965_read32(priv, CSR_UCODE_DRV_GP1);
2639 if (!iwl4965_grab_nic_access(priv))
2640 iwl4965_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002641 spin_unlock_irqrestore(&priv->lock, flags);
2642
2643 if (test_bit(STATUS_RF_KILL_HW, &priv->status)) {
2644 IWL_DEBUG_RF_KILL("Can not turn radio back on - "
2645 "disabled by HW switch\n");
2646 return;
2647 }
2648
2649 queue_work(priv->workqueue, &priv->restart);
2650 return;
2651}
2652
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002653void iwl4965_set_decrypted_flag(struct iwl_priv *priv, struct sk_buff *skb,
Zhu Yib481de92007-09-25 17:54:57 -07002654 u32 decrypt_res, struct ieee80211_rx_status *stats)
2655{
2656 u16 fc =
2657 le16_to_cpu(((struct ieee80211_hdr *)skb->data)->frame_control);
2658
2659 if (priv->active_rxon.filter_flags & RXON_FILTER_DIS_DECRYPT_MSK)
2660 return;
2661
2662 if (!(fc & IEEE80211_FCTL_PROTECTED))
2663 return;
2664
2665 IWL_DEBUG_RX("decrypt_res:0x%x\n", decrypt_res);
2666 switch (decrypt_res & RX_RES_STATUS_SEC_TYPE_MSK) {
2667 case RX_RES_STATUS_SEC_TYPE_TKIP:
Emmanuel Grumbach17e476b2008-03-19 16:41:42 -07002668 /* The uCode has got a bad phase 1 Key, pushes the packet.
2669 * Decryption will be done in SW. */
2670 if ((decrypt_res & RX_RES_STATUS_DECRYPT_TYPE_MSK) ==
2671 RX_RES_STATUS_BAD_KEY_TTAK)
2672 break;
2673
Zhu Yib481de92007-09-25 17:54:57 -07002674 if ((decrypt_res & RX_RES_STATUS_DECRYPT_TYPE_MSK) ==
2675 RX_RES_STATUS_BAD_ICV_MIC)
2676 stats->flag |= RX_FLAG_MMIC_ERROR;
2677 case RX_RES_STATUS_SEC_TYPE_WEP:
2678 case RX_RES_STATUS_SEC_TYPE_CCMP:
2679 if ((decrypt_res & RX_RES_STATUS_DECRYPT_TYPE_MSK) ==
2680 RX_RES_STATUS_DECRYPT_OK) {
2681 IWL_DEBUG_RX("hw decrypt successfully!!!\n");
2682 stats->flag |= RX_FLAG_DECRYPTED;
2683 }
2684 break;
2685
2686 default:
2687 break;
2688 }
2689}
2690
Zhu Yib481de92007-09-25 17:54:57 -07002691
2692#define IWL_PACKET_RETRY_TIME HZ
2693
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002694int iwl4965_is_duplicate_packet(struct iwl_priv *priv, struct ieee80211_hdr *header)
Zhu Yib481de92007-09-25 17:54:57 -07002695{
2696 u16 sc = le16_to_cpu(header->seq_ctrl);
2697 u16 seq = (sc & IEEE80211_SCTL_SEQ) >> 4;
2698 u16 frag = sc & IEEE80211_SCTL_FRAG;
2699 u16 *last_seq, *last_frag;
2700 unsigned long *last_time;
2701
2702 switch (priv->iw_mode) {
2703 case IEEE80211_IF_TYPE_IBSS:{
2704 struct list_head *p;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002705 struct iwl4965_ibss_seq *entry = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07002706 u8 *mac = header->addr2;
2707 int index = mac[5] & (IWL_IBSS_MAC_HASH_SIZE - 1);
2708
2709 __list_for_each(p, &priv->ibss_mac_hash[index]) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002710 entry = list_entry(p, struct iwl4965_ibss_seq, list);
Zhu Yib481de92007-09-25 17:54:57 -07002711 if (!compare_ether_addr(entry->mac, mac))
2712 break;
2713 }
2714 if (p == &priv->ibss_mac_hash[index]) {
2715 entry = kzalloc(sizeof(*entry), GFP_ATOMIC);
2716 if (!entry) {
Ian Schrambc434dd2007-10-25 17:15:29 +08002717 IWL_ERROR("Cannot malloc new mac entry\n");
Zhu Yib481de92007-09-25 17:54:57 -07002718 return 0;
2719 }
2720 memcpy(entry->mac, mac, ETH_ALEN);
2721 entry->seq_num = seq;
2722 entry->frag_num = frag;
2723 entry->packet_time = jiffies;
Ian Schrambc434dd2007-10-25 17:15:29 +08002724 list_add(&entry->list, &priv->ibss_mac_hash[index]);
Zhu Yib481de92007-09-25 17:54:57 -07002725 return 0;
2726 }
2727 last_seq = &entry->seq_num;
2728 last_frag = &entry->frag_num;
2729 last_time = &entry->packet_time;
2730 break;
2731 }
2732 case IEEE80211_IF_TYPE_STA:
2733 last_seq = &priv->last_seq_num;
2734 last_frag = &priv->last_frag_num;
2735 last_time = &priv->last_packet_time;
2736 break;
2737 default:
2738 return 0;
2739 }
2740 if ((*last_seq == seq) &&
2741 time_after(*last_time + IWL_PACKET_RETRY_TIME, jiffies)) {
2742 if (*last_frag == frag)
2743 goto drop;
2744 if (*last_frag + 1 != frag)
2745 /* out-of-order fragment */
2746 goto drop;
2747 } else
2748 *last_seq = seq;
2749
2750 *last_frag = frag;
2751 *last_time = jiffies;
2752 return 0;
2753
2754 drop:
2755 return 1;
2756}
2757
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08002758#ifdef CONFIG_IWL4965_SPECTRUM_MEASUREMENT
Zhu Yib481de92007-09-25 17:54:57 -07002759
2760#include "iwl-spectrum.h"
2761
2762#define BEACON_TIME_MASK_LOW 0x00FFFFFF
2763#define BEACON_TIME_MASK_HIGH 0xFF000000
2764#define TIME_UNIT 1024
2765
2766/*
2767 * extended beacon time format
2768 * time in usec will be changed into a 32-bit value in 8:24 format
2769 * the high 1 byte is the beacon counts
2770 * the lower 3 bytes is the time in usec within one beacon interval
2771 */
2772
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002773static u32 iwl4965_usecs_to_beacons(u32 usec, u32 beacon_interval)
Zhu Yib481de92007-09-25 17:54:57 -07002774{
2775 u32 quot;
2776 u32 rem;
2777 u32 interval = beacon_interval * 1024;
2778
2779 if (!interval || !usec)
2780 return 0;
2781
2782 quot = (usec / interval) & (BEACON_TIME_MASK_HIGH >> 24);
2783 rem = (usec % interval) & BEACON_TIME_MASK_LOW;
2784
2785 return (quot << 24) + rem;
2786}
2787
2788/* base is usually what we get from ucode with each received frame,
2789 * the same as HW timer counter counting down
2790 */
2791
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002792static __le32 iwl4965_add_beacon_time(u32 base, u32 addon, u32 beacon_interval)
Zhu Yib481de92007-09-25 17:54:57 -07002793{
2794 u32 base_low = base & BEACON_TIME_MASK_LOW;
2795 u32 addon_low = addon & BEACON_TIME_MASK_LOW;
2796 u32 interval = beacon_interval * TIME_UNIT;
2797 u32 res = (base & BEACON_TIME_MASK_HIGH) +
2798 (addon & BEACON_TIME_MASK_HIGH);
2799
2800 if (base_low > addon_low)
2801 res += base_low - addon_low;
2802 else if (base_low < addon_low) {
2803 res += interval + base_low - addon_low;
2804 res += (1 << 24);
2805 } else
2806 res += (1 << 24);
2807
2808 return cpu_to_le32(res);
2809}
2810
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002811static int iwl4965_get_measurement(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07002812 struct ieee80211_measurement_params *params,
2813 u8 type)
2814{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002815 struct iwl4965_spectrum_cmd spectrum;
2816 struct iwl4965_rx_packet *res;
Tomas Winkler857485c2008-03-21 13:53:44 -07002817 struct iwl_host_cmd cmd = {
Zhu Yib481de92007-09-25 17:54:57 -07002818 .id = REPLY_SPECTRUM_MEASUREMENT_CMD,
2819 .data = (void *)&spectrum,
2820 .meta.flags = CMD_WANT_SKB,
2821 };
2822 u32 add_time = le64_to_cpu(params->start_time);
2823 int rc;
2824 int spectrum_resp_status;
2825 int duration = le16_to_cpu(params->duration);
2826
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002827 if (iwl4965_is_associated(priv))
Zhu Yib481de92007-09-25 17:54:57 -07002828 add_time =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002829 iwl4965_usecs_to_beacons(
Zhu Yib481de92007-09-25 17:54:57 -07002830 le64_to_cpu(params->start_time) - priv->last_tsf,
2831 le16_to_cpu(priv->rxon_timing.beacon_interval));
2832
2833 memset(&spectrum, 0, sizeof(spectrum));
2834
2835 spectrum.channel_count = cpu_to_le16(1);
2836 spectrum.flags =
2837 RXON_FLG_TSF2HOST_MSK | RXON_FLG_ANT_A_MSK | RXON_FLG_DIS_DIV_MSK;
2838 spectrum.filter_flags = MEASUREMENT_FILTER_FLAG;
2839 cmd.len = sizeof(spectrum);
2840 spectrum.len = cpu_to_le16(cmd.len - sizeof(spectrum.len));
2841
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002842 if (iwl4965_is_associated(priv))
Zhu Yib481de92007-09-25 17:54:57 -07002843 spectrum.start_time =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002844 iwl4965_add_beacon_time(priv->last_beacon_time,
Zhu Yib481de92007-09-25 17:54:57 -07002845 add_time,
2846 le16_to_cpu(priv->rxon_timing.beacon_interval));
2847 else
2848 spectrum.start_time = 0;
2849
2850 spectrum.channels[0].duration = cpu_to_le32(duration * TIME_UNIT);
2851 spectrum.channels[0].channel = params->channel;
2852 spectrum.channels[0].type = type;
2853 if (priv->active_rxon.flags & RXON_FLG_BAND_24G_MSK)
2854 spectrum.flags |= RXON_FLG_BAND_24G_MSK |
2855 RXON_FLG_AUTO_DETECT_MSK | RXON_FLG_TGG_PROTECT_MSK;
2856
Tomas Winkler857485c2008-03-21 13:53:44 -07002857 rc = iwl_send_cmd_sync(priv, &cmd);
Zhu Yib481de92007-09-25 17:54:57 -07002858 if (rc)
2859 return rc;
2860
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002861 res = (struct iwl4965_rx_packet *)cmd.meta.u.skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07002862 if (res->hdr.flags & IWL_CMD_FAILED_MSK) {
2863 IWL_ERROR("Bad return from REPLY_RX_ON_ASSOC command\n");
2864 rc = -EIO;
2865 }
2866
2867 spectrum_resp_status = le16_to_cpu(res->u.spectrum.status);
2868 switch (spectrum_resp_status) {
2869 case 0: /* Command will be handled */
2870 if (res->u.spectrum.id != 0xff) {
2871 IWL_DEBUG_INFO
2872 ("Replaced existing measurement: %d\n",
2873 res->u.spectrum.id);
2874 priv->measurement_status &= ~MEASUREMENT_READY;
2875 }
2876 priv->measurement_status |= MEASUREMENT_ACTIVE;
2877 rc = 0;
2878 break;
2879
2880 case 1: /* Command will not be handled */
2881 rc = -EAGAIN;
2882 break;
2883 }
2884
2885 dev_kfree_skb_any(cmd.meta.u.skb);
2886
2887 return rc;
2888}
2889#endif
2890
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002891static void iwl4965_txstatus_to_ieee(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002892 struct iwl4965_tx_info *tx_sta)
Zhu Yib481de92007-09-25 17:54:57 -07002893{
2894
2895 tx_sta->status.ack_signal = 0;
2896 tx_sta->status.excessive_retries = 0;
2897 tx_sta->status.queue_length = 0;
2898 tx_sta->status.queue_number = 0;
2899
2900 if (in_interrupt())
2901 ieee80211_tx_status_irqsafe(priv->hw,
2902 tx_sta->skb[0], &(tx_sta->status));
2903 else
2904 ieee80211_tx_status(priv->hw,
2905 tx_sta->skb[0], &(tx_sta->status));
2906
2907 tx_sta->skb[0] = NULL;
2908}
2909
2910/**
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002911 * iwl4965_tx_queue_reclaim - Reclaim Tx queue entries already Tx'd
Zhu Yib481de92007-09-25 17:54:57 -07002912 *
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002913 * When FW advances 'R' index, all entries between old and new 'R' index
2914 * need to be reclaimed. As result, some free space forms. If there is
2915 * enough free space (> low mark), wake the stack that feeds us.
Zhu Yib481de92007-09-25 17:54:57 -07002916 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002917int iwl4965_tx_queue_reclaim(struct iwl_priv *priv, int txq_id, int index)
Zhu Yib481de92007-09-25 17:54:57 -07002918{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002919 struct iwl4965_tx_queue *txq = &priv->txq[txq_id];
2920 struct iwl4965_queue *q = &txq->q;
Zhu Yib481de92007-09-25 17:54:57 -07002921 int nfreed = 0;
2922
2923 if ((index >= q->n_bd) || (x2_queue_used(q, index) == 0)) {
2924 IWL_ERROR("Read index for DMA queue txq id (%d), index %d, "
2925 "is out of range [0-%d] %d %d.\n", txq_id,
Tomas Winklerfc4b6852007-10-25 17:15:24 +08002926 index, q->n_bd, q->write_ptr, q->read_ptr);
Zhu Yib481de92007-09-25 17:54:57 -07002927 return 0;
2928 }
2929
Tomas Winklerc54b6792008-03-06 17:36:53 -08002930 for (index = iwl_queue_inc_wrap(index, q->n_bd);
Tomas Winklerfc4b6852007-10-25 17:15:24 +08002931 q->read_ptr != index;
Tomas Winklerc54b6792008-03-06 17:36:53 -08002932 q->read_ptr = iwl_queue_inc_wrap(q->read_ptr, q->n_bd)) {
Zhu Yib481de92007-09-25 17:54:57 -07002933 if (txq_id != IWL_CMD_QUEUE_NUM) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002934 iwl4965_txstatus_to_ieee(priv,
Tomas Winklerfc4b6852007-10-25 17:15:24 +08002935 &(txq->txb[txq->q.read_ptr]));
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002936 iwl4965_hw_txq_free_tfd(priv, txq);
Zhu Yib481de92007-09-25 17:54:57 -07002937 } else if (nfreed > 1) {
2938 IWL_ERROR("HCMD skipped: index (%d) %d %d\n", index,
Tomas Winklerfc4b6852007-10-25 17:15:24 +08002939 q->write_ptr, q->read_ptr);
Zhu Yib481de92007-09-25 17:54:57 -07002940 queue_work(priv->workqueue, &priv->restart);
2941 }
2942 nfreed++;
2943 }
2944
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02002945/* if (iwl4965_queue_space(q) > q->low_mark && (txq_id >= 0) &&
Zhu Yib481de92007-09-25 17:54:57 -07002946 (txq_id != IWL_CMD_QUEUE_NUM) &&
2947 priv->mac80211_registered)
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02002948 ieee80211_wake_queue(priv->hw, txq_id); */
Zhu Yib481de92007-09-25 17:54:57 -07002949
2950
2951 return nfreed;
2952}
2953
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002954static int iwl4965_is_tx_success(u32 status)
Zhu Yib481de92007-09-25 17:54:57 -07002955{
2956 status &= TX_STATUS_MSK;
2957 return (status == TX_STATUS_SUCCESS)
2958 || (status == TX_STATUS_DIRECT_DONE);
2959}
2960
2961/******************************************************************************
2962 *
2963 * Generic RX handler implementations
2964 *
2965 ******************************************************************************/
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08002966#ifdef CONFIG_IWL4965_HT
Zhu Yib481de92007-09-25 17:54:57 -07002967
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002968static inline int iwl4965_get_ra_sta_id(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07002969 struct ieee80211_hdr *hdr)
2970{
2971 if (priv->iw_mode == IEEE80211_IF_TYPE_STA)
2972 return IWL_AP_ID;
2973 else {
2974 u8 *da = ieee80211_get_DA(hdr);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002975 return iwl4965_hw_find_station(priv, da);
Zhu Yib481de92007-09-25 17:54:57 -07002976 }
2977}
2978
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002979static struct ieee80211_hdr *iwl4965_tx_queue_get_hdr(
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002980 struct iwl_priv *priv, int txq_id, int idx)
Zhu Yib481de92007-09-25 17:54:57 -07002981{
2982 if (priv->txq[txq_id].txb[idx].skb[0])
2983 return (struct ieee80211_hdr *)priv->txq[txq_id].
2984 txb[idx].skb[0]->data;
2985 return NULL;
2986}
2987
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002988static inline u32 iwl4965_get_scd_ssn(struct iwl4965_tx_resp *tx_resp)
Zhu Yib481de92007-09-25 17:54:57 -07002989{
2990 __le32 *scd_ssn = (__le32 *)((u32 *)&tx_resp->status +
2991 tx_resp->frame_count);
2992 return le32_to_cpu(*scd_ssn) & MAX_SN;
2993
2994}
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002995
2996/**
2997 * iwl4965_tx_status_reply_tx - Handle Tx rspnse for frames in aggregation queue
2998 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002999static int iwl4965_tx_status_reply_tx(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003000 struct iwl4965_ht_agg *agg,
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003001 struct iwl4965_tx_resp_agg *tx_resp,
Zhu Yib481de92007-09-25 17:54:57 -07003002 u16 start_idx)
3003{
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003004 u16 status;
3005 struct agg_tx_status *frame_status = &tx_resp->status;
Zhu Yib481de92007-09-25 17:54:57 -07003006 struct ieee80211_tx_status *tx_status = NULL;
3007 struct ieee80211_hdr *hdr = NULL;
3008 int i, sh;
3009 int txq_id, idx;
3010 u16 seq;
3011
3012 if (agg->wait_for_ba)
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003013 IWL_DEBUG_TX_REPLY("got tx response w/o block-ack\n");
Zhu Yib481de92007-09-25 17:54:57 -07003014
3015 agg->frame_count = tx_resp->frame_count;
3016 agg->start_idx = start_idx;
3017 agg->rate_n_flags = le32_to_cpu(tx_resp->rate_n_flags);
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003018 agg->bitmap = 0;
Zhu Yib481de92007-09-25 17:54:57 -07003019
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003020 /* # frames attempted by Tx command */
Zhu Yib481de92007-09-25 17:54:57 -07003021 if (agg->frame_count == 1) {
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003022 /* Only one frame was attempted; no block-ack will arrive */
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003023 status = le16_to_cpu(frame_status[0].status);
3024 seq = le16_to_cpu(frame_status[0].sequence);
3025 idx = SEQ_TO_INDEX(seq);
3026 txq_id = SEQ_TO_QUEUE(seq);
Zhu Yib481de92007-09-25 17:54:57 -07003027
Zhu Yib481de92007-09-25 17:54:57 -07003028 /* FIXME: code repetition */
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003029 IWL_DEBUG_TX_REPLY("FrameCnt = %d, StartIdx=%d idx=%d\n",
3030 agg->frame_count, agg->start_idx, idx);
Zhu Yib481de92007-09-25 17:54:57 -07003031
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003032 tx_status = &(priv->txq[txq_id].txb[idx].status);
Zhu Yib481de92007-09-25 17:54:57 -07003033 tx_status->retry_count = tx_resp->failure_frame;
3034 tx_status->queue_number = status & 0xff;
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003035 tx_status->queue_length = tx_resp->failure_rts;
3036 tx_status->control.flags &= ~IEEE80211_TXCTL_AMPDU;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003037 tx_status->flags = iwl4965_is_tx_success(status)?
Zhu Yib481de92007-09-25 17:54:57 -07003038 IEEE80211_TX_STATUS_ACK : 0;
Ron Rindjunsky4c424e42008-03-04 18:09:27 -08003039 iwl4965_hwrate_to_tx_control(priv,
3040 le32_to_cpu(tx_resp->rate_n_flags),
3041 &tx_status->control);
Zhu Yib481de92007-09-25 17:54:57 -07003042 /* FIXME: code repetition end */
3043
3044 IWL_DEBUG_TX_REPLY("1 Frame 0x%x failure :%d\n",
3045 status & 0xff, tx_resp->failure_frame);
3046 IWL_DEBUG_TX_REPLY("Rate Info rate_n_flags=%x\n",
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003047 iwl4965_hw_get_rate_n_flags(tx_resp->rate_n_flags));
Zhu Yib481de92007-09-25 17:54:57 -07003048
3049 agg->wait_for_ba = 0;
3050 } else {
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003051 /* Two or more frames were attempted; expect block-ack */
Zhu Yib481de92007-09-25 17:54:57 -07003052 u64 bitmap = 0;
3053 int start = agg->start_idx;
3054
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003055 /* Construct bit-map of pending frames within Tx window */
Zhu Yib481de92007-09-25 17:54:57 -07003056 for (i = 0; i < agg->frame_count; i++) {
3057 u16 sc;
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003058 status = le16_to_cpu(frame_status[i].status);
3059 seq = le16_to_cpu(frame_status[i].sequence);
Zhu Yib481de92007-09-25 17:54:57 -07003060 idx = SEQ_TO_INDEX(seq);
3061 txq_id = SEQ_TO_QUEUE(seq);
3062
3063 if (status & (AGG_TX_STATE_FEW_BYTES_MSK |
3064 AGG_TX_STATE_ABORT_MSK))
3065 continue;
3066
3067 IWL_DEBUG_TX_REPLY("FrameCnt = %d, txq_id=%d idx=%d\n",
3068 agg->frame_count, txq_id, idx);
3069
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003070 hdr = iwl4965_tx_queue_get_hdr(priv, txq_id, idx);
Zhu Yib481de92007-09-25 17:54:57 -07003071
3072 sc = le16_to_cpu(hdr->seq_ctrl);
3073 if (idx != (SEQ_TO_SN(sc) & 0xff)) {
3074 IWL_ERROR("BUG_ON idx doesn't match seq control"
3075 " idx=%d, seq_idx=%d, seq=%d\n",
3076 idx, SEQ_TO_SN(sc),
3077 hdr->seq_ctrl);
3078 return -1;
3079 }
3080
3081 IWL_DEBUG_TX_REPLY("AGG Frame i=%d idx %d seq=%d\n",
3082 i, idx, SEQ_TO_SN(sc));
3083
3084 sh = idx - start;
3085 if (sh > 64) {
3086 sh = (start - idx) + 0xff;
3087 bitmap = bitmap << sh;
3088 sh = 0;
3089 start = idx;
3090 } else if (sh < -64)
3091 sh = 0xff - (start - idx);
3092 else if (sh < 0) {
3093 sh = start - idx;
3094 start = idx;
3095 bitmap = bitmap << sh;
3096 sh = 0;
3097 }
3098 bitmap |= (1 << sh);
3099 IWL_DEBUG_TX_REPLY("start=%d bitmap=0x%x\n",
3100 start, (u32)(bitmap & 0xFFFFFFFF));
3101 }
3102
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003103 agg->bitmap = bitmap;
Zhu Yib481de92007-09-25 17:54:57 -07003104 agg->start_idx = start;
3105 agg->rate_n_flags = le32_to_cpu(tx_resp->rate_n_flags);
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003106 IWL_DEBUG_TX_REPLY("Frames %d start_idx=%d bitmap=0x%llx\n",
Zhu Yib481de92007-09-25 17:54:57 -07003107 agg->frame_count, agg->start_idx,
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003108 agg->bitmap);
Zhu Yib481de92007-09-25 17:54:57 -07003109
3110 if (bitmap)
3111 agg->wait_for_ba = 1;
3112 }
3113 return 0;
3114}
3115#endif
Zhu Yib481de92007-09-25 17:54:57 -07003116
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003117/**
3118 * iwl4965_rx_reply_tx - Handle standard (non-aggregation) Tx response
3119 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003120static void iwl4965_rx_reply_tx(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003121 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003122{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003123 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07003124 u16 sequence = le16_to_cpu(pkt->hdr.sequence);
3125 int txq_id = SEQ_TO_QUEUE(sequence);
3126 int index = SEQ_TO_INDEX(sequence);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003127 struct iwl4965_tx_queue *txq = &priv->txq[txq_id];
Zhu Yib481de92007-09-25 17:54:57 -07003128 struct ieee80211_tx_status *tx_status;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003129 struct iwl4965_tx_resp *tx_resp = (void *)&pkt->u.raw[0];
Zhu Yib481de92007-09-25 17:54:57 -07003130 u32 status = le32_to_cpu(tx_resp->status);
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08003131#ifdef CONFIG_IWL4965_HT
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003132 int tid = MAX_TID_COUNT, sta_id = IWL_INVALID_STATION;
3133 struct ieee80211_hdr *hdr;
3134 __le16 *qc;
Zhu Yib481de92007-09-25 17:54:57 -07003135#endif
3136
3137 if ((index >= txq->q.n_bd) || (x2_queue_used(&txq->q, index) == 0)) {
3138 IWL_ERROR("Read index for DMA queue txq_id (%d) index %d "
3139 "is out of range [0-%d] %d %d\n", txq_id,
Tomas Winklerfc4b6852007-10-25 17:15:24 +08003140 index, txq->q.n_bd, txq->q.write_ptr,
3141 txq->q.read_ptr);
Zhu Yib481de92007-09-25 17:54:57 -07003142 return;
3143 }
3144
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08003145#ifdef CONFIG_IWL4965_HT
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003146 hdr = iwl4965_tx_queue_get_hdr(priv, txq_id, index);
3147 qc = ieee80211_get_qos_ctrl(hdr);
Zhu Yib481de92007-09-25 17:54:57 -07003148
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003149 if (qc)
Zhu Yib481de92007-09-25 17:54:57 -07003150 tid = le16_to_cpu(*qc) & 0xf;
3151
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003152 sta_id = iwl4965_get_ra_sta_id(priv, hdr);
3153 if (txq->sched_retry && unlikely(sta_id == IWL_INVALID_STATION)) {
3154 IWL_ERROR("Station not known\n");
3155 return;
3156 }
3157
3158 if (txq->sched_retry) {
3159 const u32 scd_ssn = iwl4965_get_scd_ssn(tx_resp);
3160 struct iwl4965_ht_agg *agg = NULL;
3161
3162 if (!qc)
Zhu Yib481de92007-09-25 17:54:57 -07003163 return;
Zhu Yib481de92007-09-25 17:54:57 -07003164
3165 agg = &priv->stations[sta_id].tid[tid].agg;
3166
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003167 iwl4965_tx_status_reply_tx(priv, agg,
3168 (struct iwl4965_tx_resp_agg *)tx_resp, index);
Zhu Yib481de92007-09-25 17:54:57 -07003169
3170 if ((tx_resp->frame_count == 1) &&
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003171 !iwl4965_is_tx_success(status)) {
Zhu Yib481de92007-09-25 17:54:57 -07003172 /* TODO: send BAR */
3173 }
3174
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003175 if (txq->q.read_ptr != (scd_ssn & 0xff)) {
3176 int freed;
Tomas Winklerc54b6792008-03-06 17:36:53 -08003177 index = iwl_queue_dec_wrap(scd_ssn & 0xff, txq->q.n_bd);
Zhu Yib481de92007-09-25 17:54:57 -07003178 IWL_DEBUG_TX_REPLY("Retry scheduler reclaim scd_ssn "
3179 "%d index %d\n", scd_ssn , index);
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003180 freed = iwl4965_tx_queue_reclaim(priv, txq_id, index);
3181 priv->stations[sta_id].tid[tid].tfds_in_queue -= freed;
3182
3183 if (iwl4965_queue_space(&txq->q) > txq->q.low_mark &&
3184 txq_id >= 0 && priv->mac80211_registered &&
3185 agg->state != IWL_EMPTYING_HW_QUEUE_DELBA)
3186 ieee80211_wake_queue(priv->hw, txq_id);
3187
3188 iwl4965_check_empty_hw_queue(priv, sta_id, tid, txq_id);
Zhu Yib481de92007-09-25 17:54:57 -07003189 }
3190 } else {
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08003191#endif /* CONFIG_IWL4965_HT */
Tomas Winklerfc4b6852007-10-25 17:15:24 +08003192 tx_status = &(txq->txb[txq->q.read_ptr].status);
Zhu Yib481de92007-09-25 17:54:57 -07003193
3194 tx_status->retry_count = tx_resp->failure_frame;
3195 tx_status->queue_number = status;
3196 tx_status->queue_length = tx_resp->bt_kill_count;
3197 tx_status->queue_length |= tx_resp->failure_rts;
Zhu Yib481de92007-09-25 17:54:57 -07003198 tx_status->flags =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003199 iwl4965_is_tx_success(status) ? IEEE80211_TX_STATUS_ACK : 0;
Ron Rindjunsky4c424e42008-03-04 18:09:27 -08003200 iwl4965_hwrate_to_tx_control(priv, le32_to_cpu(tx_resp->rate_n_flags),
3201 &tx_status->control);
Zhu Yib481de92007-09-25 17:54:57 -07003202
Zhu Yib481de92007-09-25 17:54:57 -07003203 IWL_DEBUG_TX("Tx queue %d Status %s (0x%08x) rate_n_flags 0x%x "
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003204 "retries %d\n", txq_id, iwl4965_get_tx_fail_reason(status),
Zhu Yib481de92007-09-25 17:54:57 -07003205 status, le32_to_cpu(tx_resp->rate_n_flags),
3206 tx_resp->failure_frame);
3207
3208 IWL_DEBUG_TX_REPLY("Tx queue reclaim %d\n", index);
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003209 if (index != -1) {
3210 int freed = iwl4965_tx_queue_reclaim(priv, txq_id, index);
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08003211#ifdef CONFIG_IWL4965_HT
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003212 if (tid != MAX_TID_COUNT)
3213 priv->stations[sta_id].tid[tid].tfds_in_queue -= freed;
3214 if (iwl4965_queue_space(&txq->q) > txq->q.low_mark &&
3215 (txq_id >= 0) &&
3216 priv->mac80211_registered)
3217 ieee80211_wake_queue(priv->hw, txq_id);
3218 if (tid != MAX_TID_COUNT)
3219 iwl4965_check_empty_hw_queue(priv, sta_id, tid, txq_id);
3220#endif
Zhu Yib481de92007-09-25 17:54:57 -07003221 }
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003222#ifdef CONFIG_IWL4965_HT
3223 }
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08003224#endif /* CONFIG_IWL4965_HT */
Zhu Yib481de92007-09-25 17:54:57 -07003225
3226 if (iwl_check_bits(status, TX_ABORT_REQUIRED_MSK))
3227 IWL_ERROR("TODO: Implement Tx ABORT REQUIRED!!!\n");
3228}
3229
3230
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003231static void iwl4965_rx_reply_alive(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003232 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003233{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003234 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
3235 struct iwl4965_alive_resp *palive;
Zhu Yib481de92007-09-25 17:54:57 -07003236 struct delayed_work *pwork;
3237
3238 palive = &pkt->u.alive_frame;
3239
3240 IWL_DEBUG_INFO("Alive ucode status 0x%08X revision "
3241 "0x%01X 0x%01X\n",
3242 palive->is_valid, palive->ver_type,
3243 palive->ver_subtype);
3244
3245 if (palive->ver_subtype == INITIALIZE_SUBTYPE) {
3246 IWL_DEBUG_INFO("Initialization Alive received.\n");
3247 memcpy(&priv->card_alive_init,
3248 &pkt->u.alive_frame,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003249 sizeof(struct iwl4965_init_alive_resp));
Zhu Yib481de92007-09-25 17:54:57 -07003250 pwork = &priv->init_alive_start;
3251 } else {
3252 IWL_DEBUG_INFO("Runtime Alive received.\n");
3253 memcpy(&priv->card_alive, &pkt->u.alive_frame,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003254 sizeof(struct iwl4965_alive_resp));
Zhu Yib481de92007-09-25 17:54:57 -07003255 pwork = &priv->alive_start;
3256 }
3257
3258 /* We delay the ALIVE response by 5ms to
3259 * give the HW RF Kill time to activate... */
3260 if (palive->is_valid == UCODE_VALID_OK)
3261 queue_delayed_work(priv->workqueue, pwork,
3262 msecs_to_jiffies(5));
3263 else
3264 IWL_WARNING("uCode did not respond OK.\n");
3265}
3266
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003267static void iwl4965_rx_reply_add_sta(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003268 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003269{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003270 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07003271
3272 IWL_DEBUG_RX("Received REPLY_ADD_STA: 0x%02X\n", pkt->u.status);
3273 return;
3274}
3275
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003276static void iwl4965_rx_reply_error(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003277 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003278{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003279 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07003280
3281 IWL_ERROR("Error Reply type 0x%08X cmd %s (0x%02X) "
3282 "seq 0x%04X ser 0x%08X\n",
3283 le32_to_cpu(pkt->u.err_resp.error_type),
3284 get_cmd_string(pkt->u.err_resp.cmd_id),
3285 pkt->u.err_resp.cmd_id,
3286 le16_to_cpu(pkt->u.err_resp.bad_cmd_seq_num),
3287 le32_to_cpu(pkt->u.err_resp.error_info));
3288}
3289
3290#define TX_STATUS_ENTRY(x) case TX_STATUS_FAIL_ ## x: return #x
3291
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003292static void iwl4965_rx_csa(struct iwl_priv *priv, struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003293{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003294 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
3295 struct iwl4965_rxon_cmd *rxon = (void *)&priv->active_rxon;
3296 struct iwl4965_csa_notification *csa = &(pkt->u.csa_notif);
Zhu Yib481de92007-09-25 17:54:57 -07003297 IWL_DEBUG_11H("CSA notif: channel %d, status %d\n",
3298 le16_to_cpu(csa->channel), le32_to_cpu(csa->status));
3299 rxon->channel = csa->channel;
3300 priv->staging_rxon.channel = csa->channel;
3301}
3302
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003303static void iwl4965_rx_spectrum_measure_notif(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003304 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003305{
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08003306#ifdef CONFIG_IWL4965_SPECTRUM_MEASUREMENT
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003307 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
3308 struct iwl4965_spectrum_notification *report = &(pkt->u.spectrum_notif);
Zhu Yib481de92007-09-25 17:54:57 -07003309
3310 if (!report->state) {
3311 IWL_DEBUG(IWL_DL_11H | IWL_DL_INFO,
3312 "Spectrum Measure Notification: Start\n");
3313 return;
3314 }
3315
3316 memcpy(&priv->measure_report, report, sizeof(*report));
3317 priv->measurement_status |= MEASUREMENT_READY;
3318#endif
3319}
3320
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003321static void iwl4965_rx_pm_sleep_notif(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003322 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003323{
Tomas Winkler0a6857e2008-03-12 16:58:49 -07003324#ifdef CONFIG_IWLWIFI_DEBUG
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003325 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
3326 struct iwl4965_sleep_notification *sleep = &(pkt->u.sleep_notif);
Zhu Yib481de92007-09-25 17:54:57 -07003327 IWL_DEBUG_RX("sleep mode: %d, src: %d\n",
3328 sleep->pm_sleep_mode, sleep->pm_wakeup_src);
3329#endif
3330}
3331
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003332static void iwl4965_rx_pm_debug_statistics_notif(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003333 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003334{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003335 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07003336 IWL_DEBUG_RADIO("Dumping %d bytes of unhandled "
3337 "notification for %s:\n",
3338 le32_to_cpu(pkt->len), get_cmd_string(pkt->hdr.cmd));
Tomas Winkler0a6857e2008-03-12 16:58:49 -07003339 iwl_print_hex_dump(IWL_DL_RADIO, pkt->u.raw, le32_to_cpu(pkt->len));
Zhu Yib481de92007-09-25 17:54:57 -07003340}
3341
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003342static void iwl4965_bg_beacon_update(struct work_struct *work)
Zhu Yib481de92007-09-25 17:54:57 -07003343{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003344 struct iwl_priv *priv =
3345 container_of(work, struct iwl_priv, beacon_update);
Zhu Yib481de92007-09-25 17:54:57 -07003346 struct sk_buff *beacon;
3347
3348 /* Pull updated AP beacon from mac80211. will fail if not in AP mode */
Johannes Berg32bfd352007-12-19 01:31:26 +01003349 beacon = ieee80211_beacon_get(priv->hw, priv->vif, NULL);
Zhu Yib481de92007-09-25 17:54:57 -07003350
3351 if (!beacon) {
3352 IWL_ERROR("update beacon failed\n");
3353 return;
3354 }
3355
3356 mutex_lock(&priv->mutex);
3357 /* new beacon skb is allocated every time; dispose previous.*/
3358 if (priv->ibss_beacon)
3359 dev_kfree_skb(priv->ibss_beacon);
3360
3361 priv->ibss_beacon = beacon;
3362 mutex_unlock(&priv->mutex);
3363
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003364 iwl4965_send_beacon_cmd(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003365}
3366
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003367static void iwl4965_rx_beacon_notif(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003368 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003369{
Tomas Winkler0a6857e2008-03-12 16:58:49 -07003370#ifdef CONFIG_IWLWIFI_DEBUG
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003371 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
3372 struct iwl4965_beacon_notif *beacon = &(pkt->u.beacon_status);
3373 u8 rate = iwl4965_hw_get_rate(beacon->beacon_notify_hdr.rate_n_flags);
Zhu Yib481de92007-09-25 17:54:57 -07003374
3375 IWL_DEBUG_RX("beacon status %x retries %d iss %d "
3376 "tsf %d %d rate %d\n",
3377 le32_to_cpu(beacon->beacon_notify_hdr.status) & TX_STATUS_MSK,
3378 beacon->beacon_notify_hdr.failure_frame,
3379 le32_to_cpu(beacon->ibss_mgr_status),
3380 le32_to_cpu(beacon->high_tsf),
3381 le32_to_cpu(beacon->low_tsf), rate);
3382#endif
3383
3384 if ((priv->iw_mode == IEEE80211_IF_TYPE_AP) &&
3385 (!test_bit(STATUS_EXIT_PENDING, &priv->status)))
3386 queue_work(priv->workqueue, &priv->beacon_update);
3387}
3388
3389/* Service response to REPLY_SCAN_CMD (0x80) */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003390static void iwl4965_rx_reply_scan(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003391 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003392{
Tomas Winkler0a6857e2008-03-12 16:58:49 -07003393#ifdef CONFIG_IWLWIFI_DEBUG
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003394 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
3395 struct iwl4965_scanreq_notification *notif =
3396 (struct iwl4965_scanreq_notification *)pkt->u.raw;
Zhu Yib481de92007-09-25 17:54:57 -07003397
3398 IWL_DEBUG_RX("Scan request status = 0x%x\n", notif->status);
3399#endif
3400}
3401
3402/* Service SCAN_START_NOTIFICATION (0x82) */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003403static void iwl4965_rx_scan_start_notif(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003404 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003405{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003406 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
3407 struct iwl4965_scanstart_notification *notif =
3408 (struct iwl4965_scanstart_notification *)pkt->u.raw;
Zhu Yib481de92007-09-25 17:54:57 -07003409 priv->scan_start_tsf = le32_to_cpu(notif->tsf_low);
3410 IWL_DEBUG_SCAN("Scan start: "
3411 "%d [802.11%s] "
3412 "(TSF: 0x%08X:%08X) - %d (beacon timer %u)\n",
3413 notif->channel,
3414 notif->band ? "bg" : "a",
3415 notif->tsf_high,
3416 notif->tsf_low, notif->status, notif->beacon_timer);
3417}
3418
3419/* Service SCAN_RESULTS_NOTIFICATION (0x83) */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003420static void iwl4965_rx_scan_results_notif(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003421 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003422{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003423 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
3424 struct iwl4965_scanresults_notification *notif =
3425 (struct iwl4965_scanresults_notification *)pkt->u.raw;
Zhu Yib481de92007-09-25 17:54:57 -07003426
3427 IWL_DEBUG_SCAN("Scan ch.res: "
3428 "%d [802.11%s] "
3429 "(TSF: 0x%08X:%08X) - %d "
3430 "elapsed=%lu usec (%dms since last)\n",
3431 notif->channel,
3432 notif->band ? "bg" : "a",
3433 le32_to_cpu(notif->tsf_high),
3434 le32_to_cpu(notif->tsf_low),
3435 le32_to_cpu(notif->statistics[0]),
3436 le32_to_cpu(notif->tsf_low) - priv->scan_start_tsf,
3437 jiffies_to_msecs(elapsed_jiffies
3438 (priv->last_scan_jiffies, jiffies)));
3439
3440 priv->last_scan_jiffies = jiffies;
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08003441 priv->next_scan_jiffies = 0;
Zhu Yib481de92007-09-25 17:54:57 -07003442}
3443
3444/* Service SCAN_COMPLETE_NOTIFICATION (0x84) */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003445static void iwl4965_rx_scan_complete_notif(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003446 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003447{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003448 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
3449 struct iwl4965_scancomplete_notification *scan_notif = (void *)pkt->u.raw;
Zhu Yib481de92007-09-25 17:54:57 -07003450
3451 IWL_DEBUG_SCAN("Scan complete: %d channels (TSF 0x%08X:%08X) - %d\n",
3452 scan_notif->scanned_channels,
3453 scan_notif->tsf_low,
3454 scan_notif->tsf_high, scan_notif->status);
3455
3456 /* The HW is no longer scanning */
3457 clear_bit(STATUS_SCAN_HW, &priv->status);
3458
3459 /* The scan completion notification came in, so kill that timer... */
3460 cancel_delayed_work(&priv->scan_check);
3461
3462 IWL_DEBUG_INFO("Scan pass on %sGHz took %dms\n",
3463 (priv->scan_bands == 2) ? "2.4" : "5.2",
3464 jiffies_to_msecs(elapsed_jiffies
3465 (priv->scan_pass_start, jiffies)));
3466
3467 /* Remove this scanned band from the list
3468 * of pending bands to scan */
3469 priv->scan_bands--;
3470
3471 /* If a request to abort was given, or the scan did not succeed
3472 * then we reset the scan state machine and terminate,
3473 * re-queuing another scan if one has been requested */
3474 if (test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
3475 IWL_DEBUG_INFO("Aborted scan completed.\n");
3476 clear_bit(STATUS_SCAN_ABORTING, &priv->status);
3477 } else {
3478 /* If there are more bands on this scan pass reschedule */
3479 if (priv->scan_bands > 0)
3480 goto reschedule;
3481 }
3482
3483 priv->last_scan_jiffies = jiffies;
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08003484 priv->next_scan_jiffies = 0;
Zhu Yib481de92007-09-25 17:54:57 -07003485 IWL_DEBUG_INFO("Setting scan to off\n");
3486
3487 clear_bit(STATUS_SCANNING, &priv->status);
3488
3489 IWL_DEBUG_INFO("Scan took %dms\n",
3490 jiffies_to_msecs(elapsed_jiffies(priv->scan_start, jiffies)));
3491
3492 queue_work(priv->workqueue, &priv->scan_completed);
3493
3494 return;
3495
3496reschedule:
3497 priv->scan_pass_start = jiffies;
3498 queue_work(priv->workqueue, &priv->request_scan);
3499}
3500
3501/* Handle notification from uCode that card's power state is changing
3502 * due to software, hardware, or critical temperature RFKILL */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003503static void iwl4965_rx_card_state_notif(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003504 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003505{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003506 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07003507 u32 flags = le32_to_cpu(pkt->u.card_state_notif.flags);
3508 unsigned long status = priv->status;
3509
3510 IWL_DEBUG_RF_KILL("Card state received: HW:%s SW:%s\n",
3511 (flags & HW_CARD_DISABLED) ? "Kill" : "On",
3512 (flags & SW_CARD_DISABLED) ? "Kill" : "On");
3513
3514 if (flags & (SW_CARD_DISABLED | HW_CARD_DISABLED |
3515 RF_CARD_DISABLED)) {
3516
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003517 iwl4965_write32(priv, CSR_UCODE_DRV_GP1_SET,
Zhu Yib481de92007-09-25 17:54:57 -07003518 CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
3519
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003520 if (!iwl4965_grab_nic_access(priv)) {
3521 iwl4965_write_direct32(
Zhu Yib481de92007-09-25 17:54:57 -07003522 priv, HBUS_TARG_MBX_C,
3523 HBUS_TARG_MBX_C_REG_BIT_CMD_BLOCKED);
3524
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003525 iwl4965_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003526 }
3527
3528 if (!(flags & RXON_CARD_DISABLED)) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003529 iwl4965_write32(priv, CSR_UCODE_DRV_GP1_CLR,
Zhu Yib481de92007-09-25 17:54:57 -07003530 CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003531 if (!iwl4965_grab_nic_access(priv)) {
3532 iwl4965_write_direct32(
Zhu Yib481de92007-09-25 17:54:57 -07003533 priv, HBUS_TARG_MBX_C,
3534 HBUS_TARG_MBX_C_REG_BIT_CMD_BLOCKED);
3535
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003536 iwl4965_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003537 }
3538 }
3539
3540 if (flags & RF_CARD_DISABLED) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003541 iwl4965_write32(priv, CSR_UCODE_DRV_GP1_SET,
Zhu Yib481de92007-09-25 17:54:57 -07003542 CSR_UCODE_DRV_GP1_REG_BIT_CT_KILL_EXIT);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003543 iwl4965_read32(priv, CSR_UCODE_DRV_GP1);
3544 if (!iwl4965_grab_nic_access(priv))
3545 iwl4965_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003546 }
3547 }
3548
3549 if (flags & HW_CARD_DISABLED)
3550 set_bit(STATUS_RF_KILL_HW, &priv->status);
3551 else
3552 clear_bit(STATUS_RF_KILL_HW, &priv->status);
3553
3554
3555 if (flags & SW_CARD_DISABLED)
3556 set_bit(STATUS_RF_KILL_SW, &priv->status);
3557 else
3558 clear_bit(STATUS_RF_KILL_SW, &priv->status);
3559
3560 if (!(flags & RXON_CARD_DISABLED))
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003561 iwl4965_scan_cancel(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003562
3563 if ((test_bit(STATUS_RF_KILL_HW, &status) !=
3564 test_bit(STATUS_RF_KILL_HW, &priv->status)) ||
3565 (test_bit(STATUS_RF_KILL_SW, &status) !=
3566 test_bit(STATUS_RF_KILL_SW, &priv->status)))
3567 queue_work(priv->workqueue, &priv->rf_kill);
3568 else
3569 wake_up_interruptible(&priv->wait_command_queue);
3570}
3571
3572/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003573 * iwl4965_setup_rx_handlers - Initialize Rx handler callbacks
Zhu Yib481de92007-09-25 17:54:57 -07003574 *
3575 * Setup the RX handlers for each of the reply types sent from the uCode
3576 * to the host.
3577 *
3578 * This function chains into the hardware specific files for them to setup
3579 * any hardware specific handlers as well.
3580 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003581static void iwl4965_setup_rx_handlers(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003582{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003583 priv->rx_handlers[REPLY_ALIVE] = iwl4965_rx_reply_alive;
3584 priv->rx_handlers[REPLY_ADD_STA] = iwl4965_rx_reply_add_sta;
3585 priv->rx_handlers[REPLY_ERROR] = iwl4965_rx_reply_error;
3586 priv->rx_handlers[CHANNEL_SWITCH_NOTIFICATION] = iwl4965_rx_csa;
Zhu Yib481de92007-09-25 17:54:57 -07003587 priv->rx_handlers[SPECTRUM_MEASURE_NOTIFICATION] =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003588 iwl4965_rx_spectrum_measure_notif;
3589 priv->rx_handlers[PM_SLEEP_NOTIFICATION] = iwl4965_rx_pm_sleep_notif;
Zhu Yib481de92007-09-25 17:54:57 -07003590 priv->rx_handlers[PM_DEBUG_STATISTIC_NOTIFIC] =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003591 iwl4965_rx_pm_debug_statistics_notif;
3592 priv->rx_handlers[BEACON_NOTIFICATION] = iwl4965_rx_beacon_notif;
Zhu Yib481de92007-09-25 17:54:57 -07003593
Ben Cahill9fbab512007-11-29 11:09:47 +08003594 /*
3595 * The same handler is used for both the REPLY to a discrete
3596 * statistics request from the host as well as for the periodic
3597 * statistics notifications (after received beacons) from the uCode.
Zhu Yib481de92007-09-25 17:54:57 -07003598 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003599 priv->rx_handlers[REPLY_STATISTICS_CMD] = iwl4965_hw_rx_statistics;
3600 priv->rx_handlers[STATISTICS_NOTIFICATION] = iwl4965_hw_rx_statistics;
Zhu Yib481de92007-09-25 17:54:57 -07003601
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003602 priv->rx_handlers[REPLY_SCAN_CMD] = iwl4965_rx_reply_scan;
3603 priv->rx_handlers[SCAN_START_NOTIFICATION] = iwl4965_rx_scan_start_notif;
Zhu Yib481de92007-09-25 17:54:57 -07003604 priv->rx_handlers[SCAN_RESULTS_NOTIFICATION] =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003605 iwl4965_rx_scan_results_notif;
Zhu Yib481de92007-09-25 17:54:57 -07003606 priv->rx_handlers[SCAN_COMPLETE_NOTIFICATION] =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003607 iwl4965_rx_scan_complete_notif;
3608 priv->rx_handlers[CARD_STATE_NOTIFICATION] = iwl4965_rx_card_state_notif;
3609 priv->rx_handlers[REPLY_TX] = iwl4965_rx_reply_tx;
Zhu Yib481de92007-09-25 17:54:57 -07003610
Ben Cahill9fbab512007-11-29 11:09:47 +08003611 /* Set up hardware specific Rx handlers */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003612 iwl4965_hw_rx_handler_setup(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003613}
3614
3615/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003616 * iwl4965_tx_cmd_complete - Pull unused buffers off the queue and reclaim them
Zhu Yib481de92007-09-25 17:54:57 -07003617 * @rxb: Rx buffer to reclaim
3618 *
3619 * If an Rx buffer has an async callback associated with it the callback
3620 * will be executed. The attached skb (if present) will only be freed
3621 * if the callback returns 1
3622 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003623static void iwl4965_tx_cmd_complete(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003624 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003625{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003626 struct iwl4965_rx_packet *pkt = (struct iwl4965_rx_packet *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07003627 u16 sequence = le16_to_cpu(pkt->hdr.sequence);
3628 int txq_id = SEQ_TO_QUEUE(sequence);
3629 int index = SEQ_TO_INDEX(sequence);
3630 int huge = sequence & SEQ_HUGE_FRAME;
3631 int cmd_index;
Tomas Winkler857485c2008-03-21 13:53:44 -07003632 struct iwl_cmd *cmd;
Zhu Yib481de92007-09-25 17:54:57 -07003633
3634 /* If a Tx command is being handled and it isn't in the actual
3635 * command queue then there a command routing bug has been introduced
3636 * in the queue management code. */
3637 if (txq_id != IWL_CMD_QUEUE_NUM)
3638 IWL_ERROR("Error wrong command queue %d command id 0x%X\n",
3639 txq_id, pkt->hdr.cmd);
3640 BUG_ON(txq_id != IWL_CMD_QUEUE_NUM);
3641
3642 cmd_index = get_cmd_index(&priv->txq[IWL_CMD_QUEUE_NUM].q, index, huge);
3643 cmd = &priv->txq[IWL_CMD_QUEUE_NUM].cmd[cmd_index];
3644
3645 /* Input error checking is done when commands are added to queue. */
3646 if (cmd->meta.flags & CMD_WANT_SKB) {
3647 cmd->meta.source->u.skb = rxb->skb;
3648 rxb->skb = NULL;
3649 } else if (cmd->meta.u.callback &&
3650 !cmd->meta.u.callback(priv, cmd, rxb->skb))
3651 rxb->skb = NULL;
3652
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003653 iwl4965_tx_queue_reclaim(priv, txq_id, index);
Zhu Yib481de92007-09-25 17:54:57 -07003654
3655 if (!(cmd->meta.flags & CMD_ASYNC)) {
3656 clear_bit(STATUS_HCMD_ACTIVE, &priv->status);
3657 wake_up_interruptible(&priv->wait_command_queue);
3658 }
3659}
3660
3661/************************** RX-FUNCTIONS ****************************/
3662/*
3663 * Rx theory of operation
3664 *
Ben Cahill9fbab512007-11-29 11:09:47 +08003665 * Driver allocates a circular buffer of Receive Buffer Descriptors (RBDs),
3666 * each of which point to Receive Buffers to be filled by 4965. These get
3667 * used not only for Rx frames, but for any command response or notification
3668 * from the 4965. The driver and 4965 manage the Rx buffers by means
3669 * of indexes into the circular buffer.
Zhu Yib481de92007-09-25 17:54:57 -07003670 *
3671 * Rx Queue Indexes
3672 * The host/firmware share two index registers for managing the Rx buffers.
3673 *
3674 * The READ index maps to the first position that the firmware may be writing
3675 * to -- the driver can read up to (but not including) this position and get
3676 * good data.
3677 * The READ index is managed by the firmware once the card is enabled.
3678 *
3679 * The WRITE index maps to the last position the driver has read from -- the
3680 * position preceding WRITE is the last slot the firmware can place a packet.
3681 *
3682 * The queue is empty (no good data) if WRITE = READ - 1, and is full if
3683 * WRITE = READ.
3684 *
Ben Cahill9fbab512007-11-29 11:09:47 +08003685 * During initialization, the host sets up the READ queue position to the first
Zhu Yib481de92007-09-25 17:54:57 -07003686 * INDEX position, and WRITE to the last (READ - 1 wrapped)
3687 *
Ben Cahill9fbab512007-11-29 11:09:47 +08003688 * When the firmware places a packet in a buffer, it will advance the READ index
Zhu Yib481de92007-09-25 17:54:57 -07003689 * and fire the RX interrupt. The driver can then query the READ index and
3690 * process as many packets as possible, moving the WRITE index forward as it
3691 * resets the Rx queue buffers with new memory.
3692 *
3693 * The management in the driver is as follows:
3694 * + A list of pre-allocated SKBs is stored in iwl->rxq->rx_free. When
3695 * iwl->rxq->free_count drops to or below RX_LOW_WATERMARK, work is scheduled
Ian Schram01ebd062007-10-25 17:15:22 +08003696 * to replenish the iwl->rxq->rx_free.
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003697 * + In iwl4965_rx_replenish (scheduled) if 'processed' != 'read' then the
Zhu Yib481de92007-09-25 17:54:57 -07003698 * iwl->rxq is replenished and the READ INDEX is updated (updating the
3699 * 'processed' and 'read' driver indexes as well)
3700 * + A received packet is processed and handed to the kernel network stack,
3701 * detached from the iwl->rxq. The driver 'processed' index is updated.
3702 * + The Host/Firmware iwl->rxq is replenished at tasklet time from the rx_free
3703 * list. If there are no allocated buffers in iwl->rxq->rx_free, the READ
3704 * INDEX is not incremented and iwl->status(RX_STALLED) is set. If there
3705 * were enough free buffers and RX_STALLED is set it is cleared.
3706 *
3707 *
3708 * Driver sequence:
3709 *
Ben Cahill9fbab512007-11-29 11:09:47 +08003710 * iwl4965_rx_queue_alloc() Allocates rx_free
3711 * iwl4965_rx_replenish() Replenishes rx_free list from rx_used, and calls
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003712 * iwl4965_rx_queue_restock
Ben Cahill9fbab512007-11-29 11:09:47 +08003713 * iwl4965_rx_queue_restock() Moves available buffers from rx_free into Rx
Zhu Yib481de92007-09-25 17:54:57 -07003714 * queue, updates firmware pointers, and updates
3715 * the WRITE index. If insufficient rx_free buffers
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003716 * are available, schedules iwl4965_rx_replenish
Zhu Yib481de92007-09-25 17:54:57 -07003717 *
3718 * -- enable interrupts --
Ben Cahill9fbab512007-11-29 11:09:47 +08003719 * ISR - iwl4965_rx() Detach iwl4965_rx_mem_buffers from pool up to the
Zhu Yib481de92007-09-25 17:54:57 -07003720 * READ INDEX, detaching the SKB from the pool.
3721 * Moves the packet buffer from queue to rx_used.
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003722 * Calls iwl4965_rx_queue_restock to refill any empty
Zhu Yib481de92007-09-25 17:54:57 -07003723 * slots.
3724 * ...
3725 *
3726 */
3727
3728/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003729 * iwl4965_rx_queue_space - Return number of free slots available in queue.
Zhu Yib481de92007-09-25 17:54:57 -07003730 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003731static int iwl4965_rx_queue_space(const struct iwl4965_rx_queue *q)
Zhu Yib481de92007-09-25 17:54:57 -07003732{
3733 int s = q->read - q->write;
3734 if (s <= 0)
3735 s += RX_QUEUE_SIZE;
3736 /* keep some buffer to not confuse full and empty queue */
3737 s -= 2;
3738 if (s < 0)
3739 s = 0;
3740 return s;
3741}
3742
3743/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003744 * iwl4965_rx_queue_update_write_ptr - Update the write pointer for the RX queue
Zhu Yib481de92007-09-25 17:54:57 -07003745 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003746int iwl4965_rx_queue_update_write_ptr(struct iwl_priv *priv, struct iwl4965_rx_queue *q)
Zhu Yib481de92007-09-25 17:54:57 -07003747{
3748 u32 reg = 0;
3749 int rc = 0;
3750 unsigned long flags;
3751
3752 spin_lock_irqsave(&q->lock, flags);
3753
3754 if (q->need_update == 0)
3755 goto exit_unlock;
3756
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003757 /* If power-saving is in use, make sure device is awake */
Zhu Yib481de92007-09-25 17:54:57 -07003758 if (test_bit(STATUS_POWER_PMI, &priv->status)) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003759 reg = iwl4965_read32(priv, CSR_UCODE_DRV_GP1);
Zhu Yib481de92007-09-25 17:54:57 -07003760
3761 if (reg & CSR_UCODE_DRV_GP1_BIT_MAC_SLEEP) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003762 iwl4965_set_bit(priv, CSR_GP_CNTRL,
Zhu Yib481de92007-09-25 17:54:57 -07003763 CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
3764 goto exit_unlock;
3765 }
3766
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003767 rc = iwl4965_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003768 if (rc)
3769 goto exit_unlock;
3770
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003771 /* Device expects a multiple of 8 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003772 iwl4965_write_direct32(priv, FH_RSCSR_CHNL0_WPTR,
Zhu Yib481de92007-09-25 17:54:57 -07003773 q->write & ~0x7);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003774 iwl4965_release_nic_access(priv);
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003775
3776 /* Else device is assumed to be awake */
Zhu Yib481de92007-09-25 17:54:57 -07003777 } else
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003778 /* Device expects a multiple of 8 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003779 iwl4965_write32(priv, FH_RSCSR_CHNL0_WPTR, q->write & ~0x7);
Zhu Yib481de92007-09-25 17:54:57 -07003780
3781
3782 q->need_update = 0;
3783
3784 exit_unlock:
3785 spin_unlock_irqrestore(&q->lock, flags);
3786 return rc;
3787}
3788
3789/**
Ben Cahill9fbab512007-11-29 11:09:47 +08003790 * iwl4965_dma_addr2rbd_ptr - convert a DMA address to a uCode read buffer ptr
Zhu Yib481de92007-09-25 17:54:57 -07003791 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003792static inline __le32 iwl4965_dma_addr2rbd_ptr(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07003793 dma_addr_t dma_addr)
3794{
3795 return cpu_to_le32((u32)(dma_addr >> 8));
3796}
3797
3798
3799/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003800 * iwl4965_rx_queue_restock - refill RX queue from pre-allocated pool
Zhu Yib481de92007-09-25 17:54:57 -07003801 *
Ben Cahill9fbab512007-11-29 11:09:47 +08003802 * If there are slots in the RX queue that need to be restocked,
Zhu Yib481de92007-09-25 17:54:57 -07003803 * and we have free pre-allocated buffers, fill the ranks as much
Ben Cahill9fbab512007-11-29 11:09:47 +08003804 * as we can, pulling from rx_free.
Zhu Yib481de92007-09-25 17:54:57 -07003805 *
3806 * This moves the 'write' index forward to catch up with 'processed', and
3807 * also updates the memory address in the firmware to reference the new
3808 * target buffer.
3809 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003810static int iwl4965_rx_queue_restock(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003811{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003812 struct iwl4965_rx_queue *rxq = &priv->rxq;
Zhu Yib481de92007-09-25 17:54:57 -07003813 struct list_head *element;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003814 struct iwl4965_rx_mem_buffer *rxb;
Zhu Yib481de92007-09-25 17:54:57 -07003815 unsigned long flags;
3816 int write, rc;
3817
3818 spin_lock_irqsave(&rxq->lock, flags);
3819 write = rxq->write & ~0x7;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003820 while ((iwl4965_rx_queue_space(rxq) > 0) && (rxq->free_count)) {
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003821 /* Get next free Rx buffer, remove from free list */
Zhu Yib481de92007-09-25 17:54:57 -07003822 element = rxq->rx_free.next;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003823 rxb = list_entry(element, struct iwl4965_rx_mem_buffer, list);
Zhu Yib481de92007-09-25 17:54:57 -07003824 list_del(element);
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003825
3826 /* Point to Rx buffer via next RBD in circular buffer */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003827 rxq->bd[rxq->write] = iwl4965_dma_addr2rbd_ptr(priv, rxb->dma_addr);
Zhu Yib481de92007-09-25 17:54:57 -07003828 rxq->queue[rxq->write] = rxb;
3829 rxq->write = (rxq->write + 1) & RX_QUEUE_MASK;
3830 rxq->free_count--;
3831 }
3832 spin_unlock_irqrestore(&rxq->lock, flags);
3833 /* If the pre-allocated buffer pool is dropping low, schedule to
3834 * refill it */
3835 if (rxq->free_count <= RX_LOW_WATERMARK)
3836 queue_work(priv->workqueue, &priv->rx_replenish);
3837
3838
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003839 /* If we've added more space for the firmware to place data, tell it.
3840 * Increment device's write pointer in multiples of 8. */
Zhu Yib481de92007-09-25 17:54:57 -07003841 if ((write != (rxq->write & ~0x7))
3842 || (abs(rxq->write - rxq->read) > 7)) {
3843 spin_lock_irqsave(&rxq->lock, flags);
3844 rxq->need_update = 1;
3845 spin_unlock_irqrestore(&rxq->lock, flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003846 rc = iwl4965_rx_queue_update_write_ptr(priv, rxq);
Zhu Yib481de92007-09-25 17:54:57 -07003847 if (rc)
3848 return rc;
3849 }
3850
3851 return 0;
3852}
3853
3854/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003855 * iwl4965_rx_replenish - Move all used packet from rx_used to rx_free
Zhu Yib481de92007-09-25 17:54:57 -07003856 *
3857 * When moving to rx_free an SKB is allocated for the slot.
3858 *
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003859 * Also restock the Rx queue via iwl4965_rx_queue_restock.
Ian Schram01ebd062007-10-25 17:15:22 +08003860 * This is called as a scheduled work item (except for during initialization)
Zhu Yib481de92007-09-25 17:54:57 -07003861 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003862static void iwl4965_rx_allocate(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003863{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003864 struct iwl4965_rx_queue *rxq = &priv->rxq;
Zhu Yib481de92007-09-25 17:54:57 -07003865 struct list_head *element;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003866 struct iwl4965_rx_mem_buffer *rxb;
Zhu Yib481de92007-09-25 17:54:57 -07003867 unsigned long flags;
3868 spin_lock_irqsave(&rxq->lock, flags);
3869 while (!list_empty(&rxq->rx_used)) {
3870 element = rxq->rx_used.next;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003871 rxb = list_entry(element, struct iwl4965_rx_mem_buffer, list);
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003872
3873 /* Alloc a new receive buffer */
Zhu Yib481de92007-09-25 17:54:57 -07003874 rxb->skb =
Ron Rindjunsky9ee1ba42007-11-26 16:14:42 +02003875 alloc_skb(priv->hw_setting.rx_buf_size,
3876 __GFP_NOWARN | GFP_ATOMIC);
Zhu Yib481de92007-09-25 17:54:57 -07003877 if (!rxb->skb) {
3878 if (net_ratelimit())
3879 printk(KERN_CRIT DRV_NAME
3880 ": Can not allocate SKB buffers\n");
3881 /* We don't reschedule replenish work here -- we will
3882 * call the restock method and if it still needs
3883 * more buffers it will schedule replenish */
3884 break;
3885 }
3886 priv->alloc_rxb_skb++;
3887 list_del(element);
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003888
3889 /* Get physical address of RB/SKB */
Zhu Yib481de92007-09-25 17:54:57 -07003890 rxb->dma_addr =
3891 pci_map_single(priv->pci_dev, rxb->skb->data,
Ron Rindjunsky9ee1ba42007-11-26 16:14:42 +02003892 priv->hw_setting.rx_buf_size, PCI_DMA_FROMDEVICE);
Zhu Yib481de92007-09-25 17:54:57 -07003893 list_add_tail(&rxb->list, &rxq->rx_free);
3894 rxq->free_count++;
3895 }
3896 spin_unlock_irqrestore(&rxq->lock, flags);
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08003897}
3898
3899/*
3900 * this should be called while priv->lock is locked
3901*/
Tomas Winkler4fd1f842007-12-05 20:59:58 +02003902static void __iwl4965_rx_replenish(void *data)
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08003903{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003904 struct iwl_priv *priv = data;
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08003905
3906 iwl4965_rx_allocate(priv);
3907 iwl4965_rx_queue_restock(priv);
3908}
3909
3910
3911void iwl4965_rx_replenish(void *data)
3912{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003913 struct iwl_priv *priv = data;
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08003914 unsigned long flags;
3915
3916 iwl4965_rx_allocate(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003917
3918 spin_lock_irqsave(&priv->lock, flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003919 iwl4965_rx_queue_restock(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003920 spin_unlock_irqrestore(&priv->lock, flags);
3921}
3922
3923/* Assumes that the skb field of the buffers in 'pool' is kept accurate.
Ben Cahill9fbab512007-11-29 11:09:47 +08003924 * If an SKB has been detached, the POOL needs to have its SKB set to NULL
Zhu Yib481de92007-09-25 17:54:57 -07003925 * This free routine walks the list of POOL entries and if SKB is set to
3926 * non NULL it is unmapped and freed
3927 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003928static void iwl4965_rx_queue_free(struct iwl_priv *priv, struct iwl4965_rx_queue *rxq)
Zhu Yib481de92007-09-25 17:54:57 -07003929{
3930 int i;
3931 for (i = 0; i < RX_QUEUE_SIZE + RX_FREE_BUFFERS; i++) {
3932 if (rxq->pool[i].skb != NULL) {
3933 pci_unmap_single(priv->pci_dev,
3934 rxq->pool[i].dma_addr,
Ron Rindjunsky9ee1ba42007-11-26 16:14:42 +02003935 priv->hw_setting.rx_buf_size,
3936 PCI_DMA_FROMDEVICE);
Zhu Yib481de92007-09-25 17:54:57 -07003937 dev_kfree_skb(rxq->pool[i].skb);
3938 }
3939 }
3940
3941 pci_free_consistent(priv->pci_dev, 4 * RX_QUEUE_SIZE, rxq->bd,
3942 rxq->dma_addr);
3943 rxq->bd = NULL;
3944}
3945
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003946int iwl4965_rx_queue_alloc(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003947{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003948 struct iwl4965_rx_queue *rxq = &priv->rxq;
Zhu Yib481de92007-09-25 17:54:57 -07003949 struct pci_dev *dev = priv->pci_dev;
3950 int i;
3951
3952 spin_lock_init(&rxq->lock);
3953 INIT_LIST_HEAD(&rxq->rx_free);
3954 INIT_LIST_HEAD(&rxq->rx_used);
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003955
3956 /* Alloc the circular buffer of Read Buffer Descriptors (RBDs) */
Zhu Yib481de92007-09-25 17:54:57 -07003957 rxq->bd = pci_alloc_consistent(dev, 4 * RX_QUEUE_SIZE, &rxq->dma_addr);
3958 if (!rxq->bd)
3959 return -ENOMEM;
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003960
Zhu Yib481de92007-09-25 17:54:57 -07003961 /* Fill the rx_used queue with _all_ of the Rx buffers */
3962 for (i = 0; i < RX_FREE_BUFFERS + RX_QUEUE_SIZE; i++)
3963 list_add_tail(&rxq->pool[i].list, &rxq->rx_used);
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003964
Zhu Yib481de92007-09-25 17:54:57 -07003965 /* Set us so that we have processed and used all buffers, but have
3966 * not restocked the Rx queue with fresh buffers */
3967 rxq->read = rxq->write = 0;
3968 rxq->free_count = 0;
3969 rxq->need_update = 0;
3970 return 0;
3971}
3972
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003973void iwl4965_rx_queue_reset(struct iwl_priv *priv, struct iwl4965_rx_queue *rxq)
Zhu Yib481de92007-09-25 17:54:57 -07003974{
3975 unsigned long flags;
3976 int i;
3977 spin_lock_irqsave(&rxq->lock, flags);
3978 INIT_LIST_HEAD(&rxq->rx_free);
3979 INIT_LIST_HEAD(&rxq->rx_used);
3980 /* Fill the rx_used queue with _all_ of the Rx buffers */
3981 for (i = 0; i < RX_FREE_BUFFERS + RX_QUEUE_SIZE; i++) {
3982 /* In the reset function, these buffers may have been allocated
3983 * to an SKB, so we need to unmap and free potential storage */
3984 if (rxq->pool[i].skb != NULL) {
3985 pci_unmap_single(priv->pci_dev,
3986 rxq->pool[i].dma_addr,
Ron Rindjunsky9ee1ba42007-11-26 16:14:42 +02003987 priv->hw_setting.rx_buf_size,
3988 PCI_DMA_FROMDEVICE);
Zhu Yib481de92007-09-25 17:54:57 -07003989 priv->alloc_rxb_skb--;
3990 dev_kfree_skb(rxq->pool[i].skb);
3991 rxq->pool[i].skb = NULL;
3992 }
3993 list_add_tail(&rxq->pool[i].list, &rxq->rx_used);
3994 }
3995
3996 /* Set us so that we have processed and used all buffers, but have
3997 * not restocked the Rx queue with fresh buffers */
3998 rxq->read = rxq->write = 0;
3999 rxq->free_count = 0;
4000 spin_unlock_irqrestore(&rxq->lock, flags);
4001}
4002
4003/* Convert linear signal-to-noise ratio into dB */
4004static u8 ratio2dB[100] = {
4005/* 0 1 2 3 4 5 6 7 8 9 */
4006 0, 0, 6, 10, 12, 14, 16, 17, 18, 19, /* 00 - 09 */
4007 20, 21, 22, 22, 23, 23, 24, 25, 26, 26, /* 10 - 19 */
4008 26, 26, 26, 27, 27, 28, 28, 28, 29, 29, /* 20 - 29 */
4009 29, 30, 30, 30, 31, 31, 31, 31, 32, 32, /* 30 - 39 */
4010 32, 32, 32, 33, 33, 33, 33, 33, 34, 34, /* 40 - 49 */
4011 34, 34, 34, 34, 35, 35, 35, 35, 35, 35, /* 50 - 59 */
4012 36, 36, 36, 36, 36, 36, 36, 37, 37, 37, /* 60 - 69 */
4013 37, 37, 37, 37, 37, 38, 38, 38, 38, 38, /* 70 - 79 */
4014 38, 38, 38, 38, 38, 39, 39, 39, 39, 39, /* 80 - 89 */
4015 39, 39, 39, 39, 39, 40, 40, 40, 40, 40 /* 90 - 99 */
4016};
4017
4018/* Calculates a relative dB value from a ratio of linear
4019 * (i.e. not dB) signal levels.
4020 * Conversion assumes that levels are voltages (20*log), not powers (10*log). */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004021int iwl4965_calc_db_from_ratio(int sig_ratio)
Zhu Yib481de92007-09-25 17:54:57 -07004022{
Adrian Bunkc899a572007-10-14 19:51:15 +02004023 /* 1000:1 or higher just report as 60 dB */
4024 if (sig_ratio >= 1000)
Zhu Yib481de92007-09-25 17:54:57 -07004025 return 60;
4026
Adrian Bunkc899a572007-10-14 19:51:15 +02004027 /* 100:1 or higher, divide by 10 and use table,
Zhu Yib481de92007-09-25 17:54:57 -07004028 * add 20 dB to make up for divide by 10 */
Adrian Bunkc899a572007-10-14 19:51:15 +02004029 if (sig_ratio >= 100)
Zhu Yib481de92007-09-25 17:54:57 -07004030 return (20 + (int)ratio2dB[sig_ratio/10]);
4031
4032 /* We shouldn't see this */
4033 if (sig_ratio < 1)
4034 return 0;
4035
4036 /* Use table for ratios 1:1 - 99:1 */
4037 return (int)ratio2dB[sig_ratio];
4038}
4039
4040#define PERFECT_RSSI (-20) /* dBm */
4041#define WORST_RSSI (-95) /* dBm */
4042#define RSSI_RANGE (PERFECT_RSSI - WORST_RSSI)
4043
4044/* Calculate an indication of rx signal quality (a percentage, not dBm!).
4045 * See http://www.ces.clemson.edu/linux/signal_quality.shtml for info
4046 * about formulas used below. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004047int iwl4965_calc_sig_qual(int rssi_dbm, int noise_dbm)
Zhu Yib481de92007-09-25 17:54:57 -07004048{
4049 int sig_qual;
4050 int degradation = PERFECT_RSSI - rssi_dbm;
4051
4052 /* If we get a noise measurement, use signal-to-noise ratio (SNR)
4053 * as indicator; formula is (signal dbm - noise dbm).
4054 * SNR at or above 40 is a great signal (100%).
4055 * Below that, scale to fit SNR of 0 - 40 dB within 0 - 100% indicator.
4056 * Weakest usable signal is usually 10 - 15 dB SNR. */
4057 if (noise_dbm) {
4058 if (rssi_dbm - noise_dbm >= 40)
4059 return 100;
4060 else if (rssi_dbm < noise_dbm)
4061 return 0;
4062 sig_qual = ((rssi_dbm - noise_dbm) * 5) / 2;
4063
4064 /* Else use just the signal level.
4065 * This formula is a least squares fit of data points collected and
4066 * compared with a reference system that had a percentage (%) display
4067 * for signal quality. */
4068 } else
4069 sig_qual = (100 * (RSSI_RANGE * RSSI_RANGE) - degradation *
4070 (15 * RSSI_RANGE + 62 * degradation)) /
4071 (RSSI_RANGE * RSSI_RANGE);
4072
4073 if (sig_qual > 100)
4074 sig_qual = 100;
4075 else if (sig_qual < 1)
4076 sig_qual = 0;
4077
4078 return sig_qual;
4079}
4080
4081/**
Ben Cahill9fbab512007-11-29 11:09:47 +08004082 * iwl4965_rx_handle - Main entry function for receiving responses from uCode
Zhu Yib481de92007-09-25 17:54:57 -07004083 *
4084 * Uses the priv->rx_handlers callback function array to invoke
4085 * the appropriate handlers, including command responses,
4086 * frame-received notifications, and other notifications.
4087 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004088static void iwl4965_rx_handle(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004089{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004090 struct iwl4965_rx_mem_buffer *rxb;
4091 struct iwl4965_rx_packet *pkt;
4092 struct iwl4965_rx_queue *rxq = &priv->rxq;
Zhu Yib481de92007-09-25 17:54:57 -07004093 u32 r, i;
4094 int reclaim;
4095 unsigned long flags;
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08004096 u8 fill_rx = 0;
Mohamed Abbasd68ab682008-02-07 13:16:33 -08004097 u32 count = 8;
Zhu Yib481de92007-09-25 17:54:57 -07004098
Cahill, Ben M6440adb2007-11-29 11:09:55 +08004099 /* uCode's read index (stored in shared DRAM) indicates the last Rx
4100 * buffer that the driver may process (last buffer filled by ucode). */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004101 r = iwl4965_hw_get_rx_read(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004102 i = rxq->read;
4103
4104 /* Rx interrupt, but nothing sent from uCode */
4105 if (i == r)
4106 IWL_DEBUG(IWL_DL_RX | IWL_DL_ISR, "r = %d, i = %d\n", r, i);
4107
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08004108 if (iwl4965_rx_queue_space(rxq) > (RX_QUEUE_SIZE / 2))
4109 fill_rx = 1;
4110
Zhu Yib481de92007-09-25 17:54:57 -07004111 while (i != r) {
4112 rxb = rxq->queue[i];
4113
Ben Cahill9fbab512007-11-29 11:09:47 +08004114 /* If an RXB doesn't have a Rx queue slot associated with it,
Zhu Yib481de92007-09-25 17:54:57 -07004115 * then a bug has been introduced in the queue refilling
4116 * routines -- catch it here */
4117 BUG_ON(rxb == NULL);
4118
4119 rxq->queue[i] = NULL;
4120
4121 pci_dma_sync_single_for_cpu(priv->pci_dev, rxb->dma_addr,
Ron Rindjunsky9ee1ba42007-11-26 16:14:42 +02004122 priv->hw_setting.rx_buf_size,
Zhu Yib481de92007-09-25 17:54:57 -07004123 PCI_DMA_FROMDEVICE);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004124 pkt = (struct iwl4965_rx_packet *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07004125
4126 /* Reclaim a command buffer only if this packet is a response
4127 * to a (driver-originated) command.
4128 * If the packet (e.g. Rx frame) originated from uCode,
4129 * there is no command buffer to reclaim.
4130 * Ucode should set SEQ_RX_FRAME bit if ucode-originated,
4131 * but apparently a few don't get set; catch them here. */
4132 reclaim = !(pkt->hdr.sequence & SEQ_RX_FRAME) &&
4133 (pkt->hdr.cmd != REPLY_RX_PHY_CMD) &&
Tomas Winkler857485c2008-03-21 13:53:44 -07004134 (pkt->hdr.cmd != REPLY_RX) &&
Zhu Yicfe01702007-09-27 11:27:31 +08004135 (pkt->hdr.cmd != REPLY_COMPRESSED_BA) &&
Zhu Yib481de92007-09-25 17:54:57 -07004136 (pkt->hdr.cmd != STATISTICS_NOTIFICATION) &&
4137 (pkt->hdr.cmd != REPLY_TX);
4138
4139 /* Based on type of command response or notification,
4140 * handle those that need handling via function in
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004141 * rx_handlers table. See iwl4965_setup_rx_handlers() */
Zhu Yib481de92007-09-25 17:54:57 -07004142 if (priv->rx_handlers[pkt->hdr.cmd]) {
4143 IWL_DEBUG(IWL_DL_HOST_COMMAND | IWL_DL_RX | IWL_DL_ISR,
4144 "r = %d, i = %d, %s, 0x%02x\n", r, i,
4145 get_cmd_string(pkt->hdr.cmd), pkt->hdr.cmd);
4146 priv->rx_handlers[pkt->hdr.cmd] (priv, rxb);
4147 } else {
4148 /* No handling needed */
4149 IWL_DEBUG(IWL_DL_HOST_COMMAND | IWL_DL_RX | IWL_DL_ISR,
4150 "r %d i %d No handler needed for %s, 0x%02x\n",
4151 r, i, get_cmd_string(pkt->hdr.cmd),
4152 pkt->hdr.cmd);
4153 }
4154
4155 if (reclaim) {
Ben Cahill9fbab512007-11-29 11:09:47 +08004156 /* Invoke any callbacks, transfer the skb to caller, and
Tomas Winkler857485c2008-03-21 13:53:44 -07004157 * fire off the (possibly) blocking iwl_send_cmd()
Zhu Yib481de92007-09-25 17:54:57 -07004158 * as we reclaim the driver command queue */
4159 if (rxb && rxb->skb)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004160 iwl4965_tx_cmd_complete(priv, rxb);
Zhu Yib481de92007-09-25 17:54:57 -07004161 else
4162 IWL_WARNING("Claim null rxb?\n");
4163 }
4164
4165 /* For now we just don't re-use anything. We can tweak this
4166 * later to try and re-use notification packets and SKBs that
4167 * fail to Rx correctly */
4168 if (rxb->skb != NULL) {
4169 priv->alloc_rxb_skb--;
4170 dev_kfree_skb_any(rxb->skb);
4171 rxb->skb = NULL;
4172 }
4173
4174 pci_unmap_single(priv->pci_dev, rxb->dma_addr,
Ron Rindjunsky9ee1ba42007-11-26 16:14:42 +02004175 priv->hw_setting.rx_buf_size,
4176 PCI_DMA_FROMDEVICE);
Zhu Yib481de92007-09-25 17:54:57 -07004177 spin_lock_irqsave(&rxq->lock, flags);
4178 list_add_tail(&rxb->list, &priv->rxq.rx_used);
4179 spin_unlock_irqrestore(&rxq->lock, flags);
4180 i = (i + 1) & RX_QUEUE_MASK;
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08004181 /* If there are a lot of unused frames,
4182 * restock the Rx queue so ucode wont assert. */
4183 if (fill_rx) {
4184 count++;
4185 if (count >= 8) {
4186 priv->rxq.read = i;
4187 __iwl4965_rx_replenish(priv);
4188 count = 0;
4189 }
4190 }
Zhu Yib481de92007-09-25 17:54:57 -07004191 }
4192
4193 /* Backtrack one entry */
4194 priv->rxq.read = i;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004195 iwl4965_rx_queue_restock(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004196}
4197
Cahill, Ben M6440adb2007-11-29 11:09:55 +08004198/**
4199 * iwl4965_tx_queue_update_write_ptr - Send new write index to hardware
4200 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004201static int iwl4965_tx_queue_update_write_ptr(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004202 struct iwl4965_tx_queue *txq)
Zhu Yib481de92007-09-25 17:54:57 -07004203{
4204 u32 reg = 0;
4205 int rc = 0;
4206 int txq_id = txq->q.id;
4207
4208 if (txq->need_update == 0)
4209 return rc;
4210
4211 /* if we're trying to save power */
4212 if (test_bit(STATUS_POWER_PMI, &priv->status)) {
4213 /* wake up nic if it's powered down ...
4214 * uCode will wake up, and interrupt us again, so next
4215 * time we'll skip this part. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004216 reg = iwl4965_read32(priv, CSR_UCODE_DRV_GP1);
Zhu Yib481de92007-09-25 17:54:57 -07004217
4218 if (reg & CSR_UCODE_DRV_GP1_BIT_MAC_SLEEP) {
4219 IWL_DEBUG_INFO("Requesting wakeup, GP1 = 0x%x\n", reg);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004220 iwl4965_set_bit(priv, CSR_GP_CNTRL,
Zhu Yib481de92007-09-25 17:54:57 -07004221 CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
4222 return rc;
4223 }
4224
4225 /* restore this queue's parameters in nic hardware. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004226 rc = iwl4965_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004227 if (rc)
4228 return rc;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004229 iwl4965_write_direct32(priv, HBUS_TARG_WRPTR,
Tomas Winklerfc4b6852007-10-25 17:15:24 +08004230 txq->q.write_ptr | (txq_id << 8));
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004231 iwl4965_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004232
4233 /* else not in power-save mode, uCode will never sleep when we're
4234 * trying to tx (during RFKILL, we're not trying to tx). */
4235 } else
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004236 iwl4965_write32(priv, HBUS_TARG_WRPTR,
Tomas Winklerfc4b6852007-10-25 17:15:24 +08004237 txq->q.write_ptr | (txq_id << 8));
Zhu Yib481de92007-09-25 17:54:57 -07004238
4239 txq->need_update = 0;
4240
4241 return rc;
4242}
4243
Tomas Winkler0a6857e2008-03-12 16:58:49 -07004244#ifdef CONFIG_IWLWIFI_DEBUG
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004245static void iwl4965_print_rx_config_cmd(struct iwl4965_rxon_cmd *rxon)
Zhu Yib481de92007-09-25 17:54:57 -07004246{
Joe Perches0795af52007-10-03 17:59:30 -07004247 DECLARE_MAC_BUF(mac);
4248
Zhu Yib481de92007-09-25 17:54:57 -07004249 IWL_DEBUG_RADIO("RX CONFIG:\n");
Tomas Winkler0a6857e2008-03-12 16:58:49 -07004250 iwl_print_hex_dump(IWL_DL_RADIO, (u8 *) rxon, sizeof(*rxon));
Zhu Yib481de92007-09-25 17:54:57 -07004251 IWL_DEBUG_RADIO("u16 channel: 0x%x\n", le16_to_cpu(rxon->channel));
4252 IWL_DEBUG_RADIO("u32 flags: 0x%08X\n", le32_to_cpu(rxon->flags));
4253 IWL_DEBUG_RADIO("u32 filter_flags: 0x%08x\n",
4254 le32_to_cpu(rxon->filter_flags));
4255 IWL_DEBUG_RADIO("u8 dev_type: 0x%x\n", rxon->dev_type);
4256 IWL_DEBUG_RADIO("u8 ofdm_basic_rates: 0x%02x\n",
4257 rxon->ofdm_basic_rates);
4258 IWL_DEBUG_RADIO("u8 cck_basic_rates: 0x%02x\n", rxon->cck_basic_rates);
Joe Perches0795af52007-10-03 17:59:30 -07004259 IWL_DEBUG_RADIO("u8[6] node_addr: %s\n",
4260 print_mac(mac, rxon->node_addr));
4261 IWL_DEBUG_RADIO("u8[6] bssid_addr: %s\n",
4262 print_mac(mac, rxon->bssid_addr));
Zhu Yib481de92007-09-25 17:54:57 -07004263 IWL_DEBUG_RADIO("u16 assoc_id: 0x%x\n", le16_to_cpu(rxon->assoc_id));
4264}
4265#endif
4266
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004267static void iwl4965_enable_interrupts(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004268{
4269 IWL_DEBUG_ISR("Enabling interrupts\n");
4270 set_bit(STATUS_INT_ENABLED, &priv->status);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004271 iwl4965_write32(priv, CSR_INT_MASK, CSR_INI_SET_MASK);
Zhu Yib481de92007-09-25 17:54:57 -07004272}
4273
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004274static inline void iwl4965_disable_interrupts(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004275{
4276 clear_bit(STATUS_INT_ENABLED, &priv->status);
4277
4278 /* disable interrupts from uCode/NIC to host */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004279 iwl4965_write32(priv, CSR_INT_MASK, 0x00000000);
Zhu Yib481de92007-09-25 17:54:57 -07004280
4281 /* acknowledge/clear/reset any interrupts still pending
4282 * from uCode or flow handler (Rx/Tx DMA) */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004283 iwl4965_write32(priv, CSR_INT, 0xffffffff);
4284 iwl4965_write32(priv, CSR_FH_INT_STATUS, 0xffffffff);
Zhu Yib481de92007-09-25 17:54:57 -07004285 IWL_DEBUG_ISR("Disabled interrupts\n");
4286}
4287
4288static const char *desc_lookup(int i)
4289{
4290 switch (i) {
4291 case 1:
4292 return "FAIL";
4293 case 2:
4294 return "BAD_PARAM";
4295 case 3:
4296 return "BAD_CHECKSUM";
4297 case 4:
4298 return "NMI_INTERRUPT";
4299 case 5:
4300 return "SYSASSERT";
4301 case 6:
4302 return "FATAL_ERROR";
4303 }
4304
4305 return "UNKNOWN";
4306}
4307
4308#define ERROR_START_OFFSET (1 * sizeof(u32))
4309#define ERROR_ELEM_SIZE (7 * sizeof(u32))
4310
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004311static void iwl4965_dump_nic_error_log(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004312{
4313 u32 data2, line;
4314 u32 desc, time, count, base, data1;
4315 u32 blink1, blink2, ilink1, ilink2;
4316 int rc;
4317
4318 base = le32_to_cpu(priv->card_alive.error_event_table_ptr);
4319
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004320 if (!iwl4965_hw_valid_rtc_data_addr(base)) {
Zhu Yib481de92007-09-25 17:54:57 -07004321 IWL_ERROR("Not valid error log pointer 0x%08X\n", base);
4322 return;
4323 }
4324
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004325 rc = iwl4965_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004326 if (rc) {
4327 IWL_WARNING("Can not read from adapter at this time.\n");
4328 return;
4329 }
4330
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004331 count = iwl4965_read_targ_mem(priv, base);
Zhu Yib481de92007-09-25 17:54:57 -07004332
4333 if (ERROR_START_OFFSET <= count * ERROR_ELEM_SIZE) {
4334 IWL_ERROR("Start IWL Error Log Dump:\n");
Tomas Winkler2acae162008-03-02 01:25:59 +02004335 IWL_ERROR("Status: 0x%08lX, count: %d\n", priv->status, count);
Zhu Yib481de92007-09-25 17:54:57 -07004336 }
4337
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004338 desc = iwl4965_read_targ_mem(priv, base + 1 * sizeof(u32));
4339 blink1 = iwl4965_read_targ_mem(priv, base + 3 * sizeof(u32));
4340 blink2 = iwl4965_read_targ_mem(priv, base + 4 * sizeof(u32));
4341 ilink1 = iwl4965_read_targ_mem(priv, base + 5 * sizeof(u32));
4342 ilink2 = iwl4965_read_targ_mem(priv, base + 6 * sizeof(u32));
4343 data1 = iwl4965_read_targ_mem(priv, base + 7 * sizeof(u32));
4344 data2 = iwl4965_read_targ_mem(priv, base + 8 * sizeof(u32));
4345 line = iwl4965_read_targ_mem(priv, base + 9 * sizeof(u32));
4346 time = iwl4965_read_targ_mem(priv, base + 11 * sizeof(u32));
Zhu Yib481de92007-09-25 17:54:57 -07004347
4348 IWL_ERROR("Desc Time "
4349 "data1 data2 line\n");
4350 IWL_ERROR("%-13s (#%d) %010u 0x%08X 0x%08X %u\n",
4351 desc_lookup(desc), desc, time, data1, data2, line);
4352 IWL_ERROR("blink1 blink2 ilink1 ilink2\n");
4353 IWL_ERROR("0x%05X 0x%05X 0x%05X 0x%05X\n", blink1, blink2,
4354 ilink1, ilink2);
4355
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004356 iwl4965_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004357}
4358
4359#define EVENT_START_OFFSET (4 * sizeof(u32))
4360
4361/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004362 * iwl4965_print_event_log - Dump error event log to syslog
Zhu Yib481de92007-09-25 17:54:57 -07004363 *
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004364 * NOTE: Must be called with iwl4965_grab_nic_access() already obtained!
Zhu Yib481de92007-09-25 17:54:57 -07004365 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004366static void iwl4965_print_event_log(struct iwl_priv *priv, u32 start_idx,
Zhu Yib481de92007-09-25 17:54:57 -07004367 u32 num_events, u32 mode)
4368{
4369 u32 i;
4370 u32 base; /* SRAM byte address of event log header */
4371 u32 event_size; /* 2 u32s, or 3 u32s if timestamp recorded */
4372 u32 ptr; /* SRAM byte address of log data */
4373 u32 ev, time, data; /* event log data */
4374
4375 if (num_events == 0)
4376 return;
4377
4378 base = le32_to_cpu(priv->card_alive.log_event_table_ptr);
4379
4380 if (mode == 0)
4381 event_size = 2 * sizeof(u32);
4382 else
4383 event_size = 3 * sizeof(u32);
4384
4385 ptr = base + EVENT_START_OFFSET + (start_idx * event_size);
4386
4387 /* "time" is actually "data" for mode 0 (no timestamp).
4388 * place event id # at far right for easier visual parsing. */
4389 for (i = 0; i < num_events; i++) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004390 ev = iwl4965_read_targ_mem(priv, ptr);
Zhu Yib481de92007-09-25 17:54:57 -07004391 ptr += sizeof(u32);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004392 time = iwl4965_read_targ_mem(priv, ptr);
Zhu Yib481de92007-09-25 17:54:57 -07004393 ptr += sizeof(u32);
4394 if (mode == 0)
4395 IWL_ERROR("0x%08x\t%04u\n", time, ev); /* data, ev */
4396 else {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004397 data = iwl4965_read_targ_mem(priv, ptr);
Zhu Yib481de92007-09-25 17:54:57 -07004398 ptr += sizeof(u32);
4399 IWL_ERROR("%010u\t0x%08x\t%04u\n", time, data, ev);
4400 }
4401 }
4402}
4403
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004404static void iwl4965_dump_nic_event_log(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004405{
4406 int rc;
4407 u32 base; /* SRAM byte address of event log header */
4408 u32 capacity; /* event log capacity in # entries */
4409 u32 mode; /* 0 - no timestamp, 1 - timestamp recorded */
4410 u32 num_wraps; /* # times uCode wrapped to top of log */
4411 u32 next_entry; /* index of next entry to be written by uCode */
4412 u32 size; /* # entries that we'll print */
4413
4414 base = le32_to_cpu(priv->card_alive.log_event_table_ptr);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004415 if (!iwl4965_hw_valid_rtc_data_addr(base)) {
Zhu Yib481de92007-09-25 17:54:57 -07004416 IWL_ERROR("Invalid event log pointer 0x%08X\n", base);
4417 return;
4418 }
4419
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004420 rc = iwl4965_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004421 if (rc) {
4422 IWL_WARNING("Can not read from adapter at this time.\n");
4423 return;
4424 }
4425
4426 /* event log header */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004427 capacity = iwl4965_read_targ_mem(priv, base);
4428 mode = iwl4965_read_targ_mem(priv, base + (1 * sizeof(u32)));
4429 num_wraps = iwl4965_read_targ_mem(priv, base + (2 * sizeof(u32)));
4430 next_entry = iwl4965_read_targ_mem(priv, base + (3 * sizeof(u32)));
Zhu Yib481de92007-09-25 17:54:57 -07004431
4432 size = num_wraps ? capacity : next_entry;
4433
4434 /* bail out if nothing in log */
4435 if (size == 0) {
Zhu Yi583fab32007-09-27 11:27:30 +08004436 IWL_ERROR("Start IWL Event Log Dump: nothing in log\n");
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004437 iwl4965_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004438 return;
4439 }
4440
Zhu Yi583fab32007-09-27 11:27:30 +08004441 IWL_ERROR("Start IWL Event Log Dump: display count %d, wraps %d\n",
Zhu Yib481de92007-09-25 17:54:57 -07004442 size, num_wraps);
4443
4444 /* if uCode has wrapped back to top of log, start at the oldest entry,
4445 * i.e the next one that uCode would fill. */
4446 if (num_wraps)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004447 iwl4965_print_event_log(priv, next_entry,
Zhu Yib481de92007-09-25 17:54:57 -07004448 capacity - next_entry, mode);
4449
4450 /* (then/else) start at top of log */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004451 iwl4965_print_event_log(priv, 0, next_entry, mode);
Zhu Yib481de92007-09-25 17:54:57 -07004452
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004453 iwl4965_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004454}
4455
4456/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004457 * iwl4965_irq_handle_error - called for HW or SW error interrupt from card
Zhu Yib481de92007-09-25 17:54:57 -07004458 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004459static void iwl4965_irq_handle_error(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004460{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004461 /* Set the FW error flag -- cleared on iwl4965_down */
Zhu Yib481de92007-09-25 17:54:57 -07004462 set_bit(STATUS_FW_ERROR, &priv->status);
4463
4464 /* Cancel currently queued command. */
4465 clear_bit(STATUS_HCMD_ACTIVE, &priv->status);
4466
Tomas Winkler0a6857e2008-03-12 16:58:49 -07004467#ifdef CONFIG_IWLWIFI_DEBUG
4468 if (iwl_debug_level & IWL_DL_FW_ERRORS) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004469 iwl4965_dump_nic_error_log(priv);
4470 iwl4965_dump_nic_event_log(priv);
4471 iwl4965_print_rx_config_cmd(&priv->staging_rxon);
Zhu Yib481de92007-09-25 17:54:57 -07004472 }
4473#endif
4474
4475 wake_up_interruptible(&priv->wait_command_queue);
4476
4477 /* Keep the restart process from trying to send host
4478 * commands by clearing the INIT status bit */
4479 clear_bit(STATUS_READY, &priv->status);
4480
4481 if (!test_bit(STATUS_EXIT_PENDING, &priv->status)) {
4482 IWL_DEBUG(IWL_DL_INFO | IWL_DL_FW_ERRORS,
4483 "Restarting adapter due to uCode error.\n");
4484
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004485 if (iwl4965_is_associated(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07004486 memcpy(&priv->recovery_rxon, &priv->active_rxon,
4487 sizeof(priv->recovery_rxon));
4488 priv->error_recovering = 1;
4489 }
4490 queue_work(priv->workqueue, &priv->restart);
4491 }
4492}
4493
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004494static void iwl4965_error_recovery(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004495{
4496 unsigned long flags;
4497
4498 memcpy(&priv->staging_rxon, &priv->recovery_rxon,
4499 sizeof(priv->staging_rxon));
4500 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004501 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004502
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004503 iwl4965_rxon_add_station(priv, priv->bssid, 1);
Zhu Yib481de92007-09-25 17:54:57 -07004504
4505 spin_lock_irqsave(&priv->lock, flags);
4506 priv->assoc_id = le16_to_cpu(priv->staging_rxon.assoc_id);
4507 priv->error_recovering = 0;
4508 spin_unlock_irqrestore(&priv->lock, flags);
4509}
4510
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004511static void iwl4965_irq_tasklet(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004512{
4513 u32 inta, handled = 0;
4514 u32 inta_fh;
4515 unsigned long flags;
Tomas Winkler0a6857e2008-03-12 16:58:49 -07004516#ifdef CONFIG_IWLWIFI_DEBUG
Zhu Yib481de92007-09-25 17:54:57 -07004517 u32 inta_mask;
4518#endif
4519
4520 spin_lock_irqsave(&priv->lock, flags);
4521
4522 /* Ack/clear/reset pending uCode interrupts.
4523 * Note: Some bits in CSR_INT are "OR" of bits in CSR_FH_INT_STATUS,
4524 * and will clear only when CSR_FH_INT_STATUS gets cleared. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004525 inta = iwl4965_read32(priv, CSR_INT);
4526 iwl4965_write32(priv, CSR_INT, inta);
Zhu Yib481de92007-09-25 17:54:57 -07004527
4528 /* Ack/clear/reset pending flow-handler (DMA) interrupts.
4529 * Any new interrupts that happen after this, either while we're
4530 * in this tasklet, or later, will show up in next ISR/tasklet. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004531 inta_fh = iwl4965_read32(priv, CSR_FH_INT_STATUS);
4532 iwl4965_write32(priv, CSR_FH_INT_STATUS, inta_fh);
Zhu Yib481de92007-09-25 17:54:57 -07004533
Tomas Winkler0a6857e2008-03-12 16:58:49 -07004534#ifdef CONFIG_IWLWIFI_DEBUG
4535 if (iwl_debug_level & IWL_DL_ISR) {
Ben Cahill9fbab512007-11-29 11:09:47 +08004536 /* just for debug */
4537 inta_mask = iwl4965_read32(priv, CSR_INT_MASK);
Zhu Yib481de92007-09-25 17:54:57 -07004538 IWL_DEBUG_ISR("inta 0x%08x, enabled 0x%08x, fh 0x%08x\n",
4539 inta, inta_mask, inta_fh);
4540 }
4541#endif
4542
4543 /* Since CSR_INT and CSR_FH_INT_STATUS reads and clears are not
4544 * atomic, make sure that inta covers all the interrupts that
4545 * we've discovered, even if FH interrupt came in just after
4546 * reading CSR_INT. */
Tomas Winkler6f83eaa2008-03-04 18:09:28 -08004547 if (inta_fh & CSR49_FH_INT_RX_MASK)
Zhu Yib481de92007-09-25 17:54:57 -07004548 inta |= CSR_INT_BIT_FH_RX;
Tomas Winkler6f83eaa2008-03-04 18:09:28 -08004549 if (inta_fh & CSR49_FH_INT_TX_MASK)
Zhu Yib481de92007-09-25 17:54:57 -07004550 inta |= CSR_INT_BIT_FH_TX;
4551
4552 /* Now service all interrupt bits discovered above. */
4553 if (inta & CSR_INT_BIT_HW_ERR) {
4554 IWL_ERROR("Microcode HW error detected. Restarting.\n");
4555
4556 /* Tell the device to stop sending interrupts */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004557 iwl4965_disable_interrupts(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004558
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004559 iwl4965_irq_handle_error(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004560
4561 handled |= CSR_INT_BIT_HW_ERR;
4562
4563 spin_unlock_irqrestore(&priv->lock, flags);
4564
4565 return;
4566 }
4567
Tomas Winkler0a6857e2008-03-12 16:58:49 -07004568#ifdef CONFIG_IWLWIFI_DEBUG
4569 if (iwl_debug_level & (IWL_DL_ISR)) {
Zhu Yib481de92007-09-25 17:54:57 -07004570 /* NIC fires this, but we don't use it, redundant with WAKEUP */
Joonwoo Park25c03d82008-01-23 10:15:20 -08004571 if (inta & CSR_INT_BIT_SCD)
4572 IWL_DEBUG_ISR("Scheduler finished to transmit "
4573 "the frame/frames.\n");
Zhu Yib481de92007-09-25 17:54:57 -07004574
4575 /* Alive notification via Rx interrupt will do the real work */
4576 if (inta & CSR_INT_BIT_ALIVE)
4577 IWL_DEBUG_ISR("Alive interrupt\n");
4578 }
4579#endif
4580 /* Safely ignore these bits for debug checks below */
Joonwoo Park25c03d82008-01-23 10:15:20 -08004581 inta &= ~(CSR_INT_BIT_SCD | CSR_INT_BIT_ALIVE);
Zhu Yib481de92007-09-25 17:54:57 -07004582
Ben Cahill9fbab512007-11-29 11:09:47 +08004583 /* HW RF KILL switch toggled */
Zhu Yib481de92007-09-25 17:54:57 -07004584 if (inta & CSR_INT_BIT_RF_KILL) {
4585 int hw_rf_kill = 0;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004586 if (!(iwl4965_read32(priv, CSR_GP_CNTRL) &
Zhu Yib481de92007-09-25 17:54:57 -07004587 CSR_GP_CNTRL_REG_FLAG_HW_RF_KILL_SW))
4588 hw_rf_kill = 1;
4589
4590 IWL_DEBUG(IWL_DL_INFO | IWL_DL_RF_KILL | IWL_DL_ISR,
4591 "RF_KILL bit toggled to %s.\n",
4592 hw_rf_kill ? "disable radio":"enable radio");
4593
4594 /* Queue restart only if RF_KILL switch was set to "kill"
4595 * when we loaded driver, and is now set to "enable".
4596 * After we're Alive, RF_KILL gets handled by
Reinette Chatre32304552008-02-15 14:34:37 -08004597 * iwl4965_rx_card_state_notif() */
Zhu Yi53e49092007-12-06 16:08:44 +08004598 if (!hw_rf_kill && !test_bit(STATUS_ALIVE, &priv->status)) {
4599 clear_bit(STATUS_RF_KILL_HW, &priv->status);
Zhu Yib481de92007-09-25 17:54:57 -07004600 queue_work(priv->workqueue, &priv->restart);
Zhu Yi53e49092007-12-06 16:08:44 +08004601 }
Zhu Yib481de92007-09-25 17:54:57 -07004602
4603 handled |= CSR_INT_BIT_RF_KILL;
4604 }
4605
Ben Cahill9fbab512007-11-29 11:09:47 +08004606 /* Chip got too hot and stopped itself */
Zhu Yib481de92007-09-25 17:54:57 -07004607 if (inta & CSR_INT_BIT_CT_KILL) {
4608 IWL_ERROR("Microcode CT kill error detected.\n");
4609 handled |= CSR_INT_BIT_CT_KILL;
4610 }
4611
4612 /* Error detected by uCode */
4613 if (inta & CSR_INT_BIT_SW_ERR) {
4614 IWL_ERROR("Microcode SW error detected. Restarting 0x%X.\n",
4615 inta);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004616 iwl4965_irq_handle_error(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004617 handled |= CSR_INT_BIT_SW_ERR;
4618 }
4619
4620 /* uCode wakes up after power-down sleep */
4621 if (inta & CSR_INT_BIT_WAKEUP) {
4622 IWL_DEBUG_ISR("Wakeup interrupt\n");
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004623 iwl4965_rx_queue_update_write_ptr(priv, &priv->rxq);
4624 iwl4965_tx_queue_update_write_ptr(priv, &priv->txq[0]);
4625 iwl4965_tx_queue_update_write_ptr(priv, &priv->txq[1]);
4626 iwl4965_tx_queue_update_write_ptr(priv, &priv->txq[2]);
4627 iwl4965_tx_queue_update_write_ptr(priv, &priv->txq[3]);
4628 iwl4965_tx_queue_update_write_ptr(priv, &priv->txq[4]);
4629 iwl4965_tx_queue_update_write_ptr(priv, &priv->txq[5]);
Zhu Yib481de92007-09-25 17:54:57 -07004630
4631 handled |= CSR_INT_BIT_WAKEUP;
4632 }
4633
4634 /* All uCode command responses, including Tx command responses,
4635 * Rx "responses" (frame-received notification), and other
4636 * notifications from uCode come through here*/
4637 if (inta & (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX)) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004638 iwl4965_rx_handle(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004639 handled |= (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX);
4640 }
4641
4642 if (inta & CSR_INT_BIT_FH_TX) {
4643 IWL_DEBUG_ISR("Tx interrupt\n");
4644 handled |= CSR_INT_BIT_FH_TX;
4645 }
4646
4647 if (inta & ~handled)
4648 IWL_ERROR("Unhandled INTA bits 0x%08x\n", inta & ~handled);
4649
4650 if (inta & ~CSR_INI_SET_MASK) {
4651 IWL_WARNING("Disabled INTA bits 0x%08x were pending\n",
4652 inta & ~CSR_INI_SET_MASK);
4653 IWL_WARNING(" with FH_INT = 0x%08x\n", inta_fh);
4654 }
4655
4656 /* Re-enable all interrupts */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004657 iwl4965_enable_interrupts(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004658
Tomas Winkler0a6857e2008-03-12 16:58:49 -07004659#ifdef CONFIG_IWLWIFI_DEBUG
4660 if (iwl_debug_level & (IWL_DL_ISR)) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004661 inta = iwl4965_read32(priv, CSR_INT);
4662 inta_mask = iwl4965_read32(priv, CSR_INT_MASK);
4663 inta_fh = iwl4965_read32(priv, CSR_FH_INT_STATUS);
Zhu Yib481de92007-09-25 17:54:57 -07004664 IWL_DEBUG_ISR("End inta 0x%08x, enabled 0x%08x, fh 0x%08x, "
4665 "flags 0x%08lx\n", inta, inta_mask, inta_fh, flags);
4666 }
4667#endif
4668 spin_unlock_irqrestore(&priv->lock, flags);
4669}
4670
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004671static irqreturn_t iwl4965_isr(int irq, void *data)
Zhu Yib481de92007-09-25 17:54:57 -07004672{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004673 struct iwl_priv *priv = data;
Zhu Yib481de92007-09-25 17:54:57 -07004674 u32 inta, inta_mask;
4675 u32 inta_fh;
4676 if (!priv)
4677 return IRQ_NONE;
4678
4679 spin_lock(&priv->lock);
4680
4681 /* Disable (but don't clear!) interrupts here to avoid
4682 * back-to-back ISRs and sporadic interrupts from our NIC.
4683 * If we have something to service, the tasklet will re-enable ints.
4684 * If we *don't* have something, we'll re-enable before leaving here. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004685 inta_mask = iwl4965_read32(priv, CSR_INT_MASK); /* just for debug */
4686 iwl4965_write32(priv, CSR_INT_MASK, 0x00000000);
Zhu Yib481de92007-09-25 17:54:57 -07004687
4688 /* Discover which interrupts are active/pending */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004689 inta = iwl4965_read32(priv, CSR_INT);
4690 inta_fh = iwl4965_read32(priv, CSR_FH_INT_STATUS);
Zhu Yib481de92007-09-25 17:54:57 -07004691
4692 /* Ignore interrupt if there's nothing in NIC to service.
4693 * This may be due to IRQ shared with another device,
4694 * or due to sporadic interrupts thrown from our NIC. */
4695 if (!inta && !inta_fh) {
4696 IWL_DEBUG_ISR("Ignore interrupt, inta == 0, inta_fh == 0\n");
4697 goto none;
4698 }
4699
4700 if ((inta == 0xFFFFFFFF) || ((inta & 0xFFFFFFF0) == 0xa5a5a5a0)) {
Oliver Neukum66fbb542007-11-15 09:31:10 +08004701 /* Hardware disappeared. It might have already raised
4702 * an interrupt */
Zhu Yib481de92007-09-25 17:54:57 -07004703 IWL_WARNING("HARDWARE GONE?? INTA == 0x%080x\n", inta);
Oliver Neukum66fbb542007-11-15 09:31:10 +08004704 goto unplugged;
Zhu Yib481de92007-09-25 17:54:57 -07004705 }
4706
4707 IWL_DEBUG_ISR("ISR inta 0x%08x, enabled 0x%08x, fh 0x%08x\n",
4708 inta, inta_mask, inta_fh);
4709
Joonwoo Park25c03d82008-01-23 10:15:20 -08004710 inta &= ~CSR_INT_BIT_SCD;
4711
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004712 /* iwl4965_irq_tasklet() will service interrupts and re-enable them */
Joonwoo Park25c03d82008-01-23 10:15:20 -08004713 if (likely(inta || inta_fh))
4714 tasklet_schedule(&priv->irq_tasklet);
Zhu Yib481de92007-09-25 17:54:57 -07004715
Oliver Neukum66fbb542007-11-15 09:31:10 +08004716 unplugged:
4717 spin_unlock(&priv->lock);
Zhu Yib481de92007-09-25 17:54:57 -07004718 return IRQ_HANDLED;
4719
4720 none:
4721 /* re-enable interrupts here since we don't have anything to service. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004722 iwl4965_enable_interrupts(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004723 spin_unlock(&priv->lock);
4724 return IRQ_NONE;
4725}
4726
Zhu Yib481de92007-09-25 17:54:57 -07004727/* For active scan, listen ACTIVE_DWELL_TIME (msec) on each channel after
4728 * sending probe req. This should be set long enough to hear probe responses
4729 * from more than one AP. */
4730#define IWL_ACTIVE_DWELL_TIME_24 (20) /* all times in msec */
4731#define IWL_ACTIVE_DWELL_TIME_52 (10)
4732
4733/* For faster active scanning, scan will move to the next channel if fewer than
4734 * PLCP_QUIET_THRESH packets are heard on this channel within
4735 * ACTIVE_QUIET_TIME after sending probe request. This shortens the dwell
4736 * time if it's a quiet channel (nothing responded to our probe, and there's
4737 * no other traffic).
4738 * Disable "quiet" feature by setting PLCP_QUIET_THRESH to 0. */
4739#define IWL_PLCP_QUIET_THRESH __constant_cpu_to_le16(1) /* packets */
4740#define IWL_ACTIVE_QUIET_TIME __constant_cpu_to_le16(5) /* msec */
4741
4742/* For passive scan, listen PASSIVE_DWELL_TIME (msec) on each channel.
4743 * Must be set longer than active dwell time.
4744 * For the most reliable scan, set > AP beacon interval (typically 100msec). */
4745#define IWL_PASSIVE_DWELL_TIME_24 (20) /* all times in msec */
4746#define IWL_PASSIVE_DWELL_TIME_52 (10)
4747#define IWL_PASSIVE_DWELL_BASE (100)
4748#define IWL_CHANNEL_TUNE_TIME 5
4749
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004750static inline u16 iwl4965_get_active_dwell_time(struct iwl_priv *priv,
Johannes Berg8318d782008-01-24 19:38:38 +01004751 enum ieee80211_band band)
Zhu Yib481de92007-09-25 17:54:57 -07004752{
Johannes Berg8318d782008-01-24 19:38:38 +01004753 if (band == IEEE80211_BAND_5GHZ)
Zhu Yib481de92007-09-25 17:54:57 -07004754 return IWL_ACTIVE_DWELL_TIME_52;
4755 else
4756 return IWL_ACTIVE_DWELL_TIME_24;
4757}
4758
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004759static u16 iwl4965_get_passive_dwell_time(struct iwl_priv *priv,
Johannes Berg8318d782008-01-24 19:38:38 +01004760 enum ieee80211_band band)
Zhu Yib481de92007-09-25 17:54:57 -07004761{
Johannes Berg8318d782008-01-24 19:38:38 +01004762 u16 active = iwl4965_get_active_dwell_time(priv, band);
4763 u16 passive = (band != IEEE80211_BAND_5GHZ) ?
Zhu Yib481de92007-09-25 17:54:57 -07004764 IWL_PASSIVE_DWELL_BASE + IWL_PASSIVE_DWELL_TIME_24 :
4765 IWL_PASSIVE_DWELL_BASE + IWL_PASSIVE_DWELL_TIME_52;
4766
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004767 if (iwl4965_is_associated(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07004768 /* If we're associated, we clamp the maximum passive
4769 * dwell time to be 98% of the beacon interval (minus
4770 * 2 * channel tune time) */
4771 passive = priv->beacon_int;
4772 if ((passive > IWL_PASSIVE_DWELL_BASE) || !passive)
4773 passive = IWL_PASSIVE_DWELL_BASE;
4774 passive = (passive * 98) / 100 - IWL_CHANNEL_TUNE_TIME * 2;
4775 }
4776
4777 if (passive <= active)
4778 passive = active + 1;
4779
4780 return passive;
4781}
4782
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004783static int iwl4965_get_channels_for_scan(struct iwl_priv *priv,
Johannes Berg8318d782008-01-24 19:38:38 +01004784 enum ieee80211_band band,
Zhu Yib481de92007-09-25 17:54:57 -07004785 u8 is_active, u8 direct_mask,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004786 struct iwl4965_scan_channel *scan_ch)
Zhu Yib481de92007-09-25 17:54:57 -07004787{
4788 const struct ieee80211_channel *channels = NULL;
Johannes Berg8318d782008-01-24 19:38:38 +01004789 const struct ieee80211_supported_band *sband;
Assaf Kraussbf85ea42008-03-14 10:38:49 -07004790 const struct iwl_channel_info *ch_info;
Zhu Yib481de92007-09-25 17:54:57 -07004791 u16 passive_dwell = 0;
4792 u16 active_dwell = 0;
4793 int added, i;
4794
Johannes Berg8318d782008-01-24 19:38:38 +01004795 sband = iwl4965_get_hw_mode(priv, band);
4796 if (!sband)
Zhu Yib481de92007-09-25 17:54:57 -07004797 return 0;
4798
Johannes Berg8318d782008-01-24 19:38:38 +01004799 channels = sband->channels;
Zhu Yib481de92007-09-25 17:54:57 -07004800
Johannes Berg8318d782008-01-24 19:38:38 +01004801 active_dwell = iwl4965_get_active_dwell_time(priv, band);
4802 passive_dwell = iwl4965_get_passive_dwell_time(priv, band);
Zhu Yib481de92007-09-25 17:54:57 -07004803
Johannes Berg8318d782008-01-24 19:38:38 +01004804 for (i = 0, added = 0; i < sband->n_channels; i++) {
4805 if (ieee80211_frequency_to_channel(channels[i].center_freq) ==
Zhu Yib481de92007-09-25 17:54:57 -07004806 le16_to_cpu(priv->active_rxon.channel)) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004807 if (iwl4965_is_associated(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07004808 IWL_DEBUG_SCAN
4809 ("Skipping current channel %d\n",
4810 le16_to_cpu(priv->active_rxon.channel));
4811 continue;
4812 }
4813 } else if (priv->only_active_channel)
4814 continue;
4815
Johannes Berg8318d782008-01-24 19:38:38 +01004816 scan_ch->channel = ieee80211_frequency_to_channel(channels[i].center_freq);
Zhu Yib481de92007-09-25 17:54:57 -07004817
Assaf Krauss8622e702008-03-21 13:53:43 -07004818 ch_info = iwl_get_channel_info(priv, band,
Ben Cahill9fbab512007-11-29 11:09:47 +08004819 scan_ch->channel);
Zhu Yib481de92007-09-25 17:54:57 -07004820 if (!is_channel_valid(ch_info)) {
4821 IWL_DEBUG_SCAN("Channel %d is INVALID for this SKU.\n",
4822 scan_ch->channel);
4823 continue;
4824 }
4825
4826 if (!is_active || is_channel_passive(ch_info) ||
Johannes Berg8318d782008-01-24 19:38:38 +01004827 (channels[i].flags & IEEE80211_CHAN_PASSIVE_SCAN))
Zhu Yib481de92007-09-25 17:54:57 -07004828 scan_ch->type = 0; /* passive */
4829 else
4830 scan_ch->type = 1; /* active */
4831
4832 if (scan_ch->type & 1)
4833 scan_ch->type |= (direct_mask << 1);
4834
4835 if (is_channel_narrow(ch_info))
4836 scan_ch->type |= (1 << 7);
4837
4838 scan_ch->active_dwell = cpu_to_le16(active_dwell);
4839 scan_ch->passive_dwell = cpu_to_le16(passive_dwell);
4840
Ben Cahill9fbab512007-11-29 11:09:47 +08004841 /* Set txpower levels to defaults */
Zhu Yib481de92007-09-25 17:54:57 -07004842 scan_ch->tpc.dsp_atten = 110;
4843 /* scan_pwr_info->tpc.dsp_atten; */
4844
4845 /*scan_pwr_info->tpc.tx_gain; */
Johannes Berg8318d782008-01-24 19:38:38 +01004846 if (band == IEEE80211_BAND_5GHZ)
Zhu Yib481de92007-09-25 17:54:57 -07004847 scan_ch->tpc.tx_gain = ((1 << 5) | (3 << 3)) | 3;
4848 else {
4849 scan_ch->tpc.tx_gain = ((1 << 5) | (5 << 3));
4850 /* NOTE: if we were doing 6Mb OFDM for scans we'd use
Ben Cahill9fbab512007-11-29 11:09:47 +08004851 * power level:
Reinette Chatre8a1b0242008-01-14 17:46:25 -08004852 * scan_ch->tpc.tx_gain = ((1 << 5) | (2 << 3)) | 3;
Zhu Yib481de92007-09-25 17:54:57 -07004853 */
4854 }
4855
4856 IWL_DEBUG_SCAN("Scanning %d [%s %d]\n",
4857 scan_ch->channel,
4858 (scan_ch->type & 1) ? "ACTIVE" : "PASSIVE",
4859 (scan_ch->type & 1) ?
4860 active_dwell : passive_dwell);
4861
4862 scan_ch++;
4863 added++;
4864 }
4865
4866 IWL_DEBUG_SCAN("total channels to scan %d \n", added);
4867 return added;
4868}
4869
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004870static void iwl4965_init_hw_rates(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07004871 struct ieee80211_rate *rates)
4872{
4873 int i;
4874
4875 for (i = 0; i < IWL_RATE_COUNT; i++) {
Johannes Berg8318d782008-01-24 19:38:38 +01004876 rates[i].bitrate = iwl4965_rates[i].ieee * 5;
4877 rates[i].hw_value = i; /* Rate scaling will work on indexes */
4878 rates[i].hw_value_short = i;
4879 rates[i].flags = 0;
4880 if ((i > IWL_LAST_OFDM_RATE) || (i < IWL_FIRST_OFDM_RATE)) {
Zhu Yib481de92007-09-25 17:54:57 -07004881 /*
Johannes Berg8318d782008-01-24 19:38:38 +01004882 * If CCK != 1M then set short preamble rate flag.
Zhu Yib481de92007-09-25 17:54:57 -07004883 */
Tomas Winkler35cdeaf2008-03-11 16:17:20 -07004884 rates[i].flags |=
4885 (iwl4965_rates[i].plcp == IWL_RATE_1M_PLCP) ?
4886 0 : IEEE80211_RATE_SHORT_PREAMBLE;
Zhu Yib481de92007-09-25 17:54:57 -07004887 }
Zhu Yib481de92007-09-25 17:54:57 -07004888 }
Zhu Yib481de92007-09-25 17:54:57 -07004889}
4890
4891/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004892 * iwl4965_init_geos - Initialize mac80211's geo/channel info based from eeprom
Zhu Yib481de92007-09-25 17:54:57 -07004893 */
Assaf Kraussbf85ea42008-03-14 10:38:49 -07004894int iwl4965_init_geos(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004895{
Assaf Kraussbf85ea42008-03-14 10:38:49 -07004896 struct iwl_channel_info *ch;
Tomas Winkler8211ef72008-03-02 01:36:04 +02004897 struct ieee80211_supported_band *sband;
Zhu Yib481de92007-09-25 17:54:57 -07004898 struct ieee80211_channel *channels;
4899 struct ieee80211_channel *geo_ch;
4900 struct ieee80211_rate *rates;
4901 int i = 0;
Zhu Yib481de92007-09-25 17:54:57 -07004902
Johannes Berg8318d782008-01-24 19:38:38 +01004903 if (priv->bands[IEEE80211_BAND_2GHZ].n_bitrates ||
4904 priv->bands[IEEE80211_BAND_5GHZ].n_bitrates) {
Zhu Yib481de92007-09-25 17:54:57 -07004905 IWL_DEBUG_INFO("Geography modes already initialized.\n");
4906 set_bit(STATUS_GEO_CONFIGURED, &priv->status);
4907 return 0;
4908 }
4909
Zhu Yib481de92007-09-25 17:54:57 -07004910 channels = kzalloc(sizeof(struct ieee80211_channel) *
4911 priv->channel_count, GFP_KERNEL);
Johannes Berg8318d782008-01-24 19:38:38 +01004912 if (!channels)
Zhu Yib481de92007-09-25 17:54:57 -07004913 return -ENOMEM;
Zhu Yib481de92007-09-25 17:54:57 -07004914
Tomas Winkler8211ef72008-03-02 01:36:04 +02004915 rates = kzalloc((sizeof(struct ieee80211_rate) * (IWL_RATE_COUNT + 1)),
Zhu Yib481de92007-09-25 17:54:57 -07004916 GFP_KERNEL);
4917 if (!rates) {
Zhu Yib481de92007-09-25 17:54:57 -07004918 kfree(channels);
4919 return -ENOMEM;
4920 }
4921
Zhu Yib481de92007-09-25 17:54:57 -07004922 /* 5.2GHz channels start after the 2.4GHz channels */
Tomas Winkler8211ef72008-03-02 01:36:04 +02004923 sband = &priv->bands[IEEE80211_BAND_5GHZ];
Assaf Kraussbf85ea42008-03-14 10:38:49 -07004924 sband->channels = &channels[ARRAY_SIZE(iwl_eeprom_band_1)];
Tomas Winkler8211ef72008-03-02 01:36:04 +02004925 /* just OFDM */
4926 sband->bitrates = &rates[IWL_FIRST_OFDM_RATE];
4927 sband->n_bitrates = IWL_RATE_COUNT - IWL_FIRST_OFDM_RATE;
Johannes Berg8318d782008-01-24 19:38:38 +01004928
Assaf Krauss1ea87392008-03-18 14:57:50 -07004929 iwl4965_init_ht_hw_capab(priv, &sband->ht_info, IEEE80211_BAND_5GHZ);
Tomas Winkler78330fd2008-02-06 02:37:18 +02004930
Tomas Winkler8211ef72008-03-02 01:36:04 +02004931 sband = &priv->bands[IEEE80211_BAND_2GHZ];
4932 sband->channels = channels;
4933 /* OFDM & CCK */
4934 sband->bitrates = rates;
4935 sband->n_bitrates = IWL_RATE_COUNT;
Zhu Yib481de92007-09-25 17:54:57 -07004936
Assaf Krauss1ea87392008-03-18 14:57:50 -07004937 iwl4965_init_ht_hw_capab(priv, &sband->ht_info, IEEE80211_BAND_2GHZ);
Tomas Winkler78330fd2008-02-06 02:37:18 +02004938
Zhu Yib481de92007-09-25 17:54:57 -07004939 priv->ieee_channels = channels;
4940 priv->ieee_rates = rates;
4941
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004942 iwl4965_init_hw_rates(priv, rates);
Zhu Yib481de92007-09-25 17:54:57 -07004943
Tomas Winkler8211ef72008-03-02 01:36:04 +02004944 for (i = 0; i < priv->channel_count; i++) {
Zhu Yib481de92007-09-25 17:54:57 -07004945 ch = &priv->channel_info[i];
4946
Tomas Winkler8211ef72008-03-02 01:36:04 +02004947 /* FIXME: might be removed if scan is OK */
4948 if (!is_channel_valid(ch))
Zhu Yib481de92007-09-25 17:54:57 -07004949 continue;
Zhu Yib481de92007-09-25 17:54:57 -07004950
Tomas Winkler8211ef72008-03-02 01:36:04 +02004951 if (is_channel_a_band(ch))
4952 sband = &priv->bands[IEEE80211_BAND_5GHZ];
4953 else
4954 sband = &priv->bands[IEEE80211_BAND_2GHZ];
Zhu Yib481de92007-09-25 17:54:57 -07004955
Tomas Winkler8211ef72008-03-02 01:36:04 +02004956 geo_ch = &sband->channels[sband->n_channels++];
4957
4958 geo_ch->center_freq = ieee80211_channel_to_frequency(ch->channel);
Johannes Berg8318d782008-01-24 19:38:38 +01004959 geo_ch->max_power = ch->max_power_avg;
4960 geo_ch->max_antenna_gain = 0xff;
Mohamed Abbas7b723042008-01-31 21:46:40 -08004961 geo_ch->hw_value = ch->channel;
Zhu Yib481de92007-09-25 17:54:57 -07004962
4963 if (is_channel_valid(ch)) {
Johannes Berg8318d782008-01-24 19:38:38 +01004964 if (!(ch->flags & EEPROM_CHANNEL_IBSS))
4965 geo_ch->flags |= IEEE80211_CHAN_NO_IBSS;
Zhu Yib481de92007-09-25 17:54:57 -07004966
Johannes Berg8318d782008-01-24 19:38:38 +01004967 if (!(ch->flags & EEPROM_CHANNEL_ACTIVE))
4968 geo_ch->flags |= IEEE80211_CHAN_PASSIVE_SCAN;
Zhu Yib481de92007-09-25 17:54:57 -07004969
4970 if (ch->flags & EEPROM_CHANNEL_RADAR)
Johannes Berg8318d782008-01-24 19:38:38 +01004971 geo_ch->flags |= IEEE80211_CHAN_RADAR;
Zhu Yib481de92007-09-25 17:54:57 -07004972
4973 if (ch->max_power_avg > priv->max_channel_txpower_limit)
4974 priv->max_channel_txpower_limit =
4975 ch->max_power_avg;
Tomas Winkler8211ef72008-03-02 01:36:04 +02004976 } else {
Johannes Berg8318d782008-01-24 19:38:38 +01004977 geo_ch->flags |= IEEE80211_CHAN_DISABLED;
Tomas Winkler8211ef72008-03-02 01:36:04 +02004978 }
4979
4980 /* Save flags for reg domain usage */
4981 geo_ch->orig_flags = geo_ch->flags;
4982
4983 IWL_DEBUG_INFO("Channel %d Freq=%d[%sGHz] %s flag=0%X\n",
4984 ch->channel, geo_ch->center_freq,
4985 is_channel_a_band(ch) ? "5.2" : "2.4",
4986 geo_ch->flags & IEEE80211_CHAN_DISABLED ?
4987 "restricted" : "valid",
4988 geo_ch->flags);
Zhu Yib481de92007-09-25 17:54:57 -07004989 }
4990
Tomas Winkler82b9a122008-03-04 18:09:30 -08004991 if ((priv->bands[IEEE80211_BAND_5GHZ].n_channels == 0) &&
4992 priv->cfg->sku & IWL_SKU_A) {
Zhu Yib481de92007-09-25 17:54:57 -07004993 printk(KERN_INFO DRV_NAME
4994 ": Incorrectly detected BG card as ABG. Please send "
4995 "your PCI ID 0x%04X:0x%04X to maintainer.\n",
4996 priv->pci_dev->device, priv->pci_dev->subsystem_device);
Tomas Winkler82b9a122008-03-04 18:09:30 -08004997 priv->cfg->sku &= ~IWL_SKU_A;
Zhu Yib481de92007-09-25 17:54:57 -07004998 }
4999
5000 printk(KERN_INFO DRV_NAME
5001 ": Tunable channels: %d 802.11bg, %d 802.11a channels\n",
Johannes Berg8318d782008-01-24 19:38:38 +01005002 priv->bands[IEEE80211_BAND_2GHZ].n_channels,
5003 priv->bands[IEEE80211_BAND_5GHZ].n_channels);
Zhu Yib481de92007-09-25 17:54:57 -07005004
John W. Linvillee0e0a672008-03-25 15:58:40 -04005005 if (priv->bands[IEEE80211_BAND_2GHZ].n_channels)
5006 priv->hw->wiphy->bands[IEEE80211_BAND_2GHZ] =
5007 &priv->bands[IEEE80211_BAND_2GHZ];
5008 if (priv->bands[IEEE80211_BAND_5GHZ].n_channels)
5009 priv->hw->wiphy->bands[IEEE80211_BAND_5GHZ] =
5010 &priv->bands[IEEE80211_BAND_5GHZ];
Zhu Yib481de92007-09-25 17:54:57 -07005011
Zhu Yib481de92007-09-25 17:54:57 -07005012 set_bit(STATUS_GEO_CONFIGURED, &priv->status);
5013
5014 return 0;
5015}
5016
Reinette Chatre849e0dc2008-01-23 10:15:18 -08005017/*
5018 * iwl4965_free_geos - undo allocations in iwl4965_init_geos
5019 */
Assaf Kraussbf85ea42008-03-14 10:38:49 -07005020void iwl4965_free_geos(struct iwl_priv *priv)
Reinette Chatre849e0dc2008-01-23 10:15:18 -08005021{
Reinette Chatre849e0dc2008-01-23 10:15:18 -08005022 kfree(priv->ieee_channels);
5023 kfree(priv->ieee_rates);
5024 clear_bit(STATUS_GEO_CONFIGURED, &priv->status);
5025}
5026
Zhu Yib481de92007-09-25 17:54:57 -07005027/******************************************************************************
5028 *
5029 * uCode download functions
5030 *
5031 ******************************************************************************/
5032
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005033static void iwl4965_dealloc_ucode_pci(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005034{
Tomas Winkler98c92212008-01-14 17:46:20 -08005035 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_code);
5036 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_data);
5037 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_data_backup);
5038 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_init);
5039 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_init_data);
5040 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_boot);
Zhu Yib481de92007-09-25 17:54:57 -07005041}
5042
5043/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005044 * iwl4965_verify_inst_full - verify runtime uCode image in card vs. host,
Zhu Yib481de92007-09-25 17:54:57 -07005045 * looking at all data.
5046 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005047static int iwl4965_verify_inst_full(struct iwl_priv *priv, __le32 *image,
Ben Cahill9fbab512007-11-29 11:09:47 +08005048 u32 len)
Zhu Yib481de92007-09-25 17:54:57 -07005049{
5050 u32 val;
5051 u32 save_len = len;
5052 int rc = 0;
5053 u32 errcnt;
5054
5055 IWL_DEBUG_INFO("ucode inst image size is %u\n", len);
5056
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005057 rc = iwl4965_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005058 if (rc)
5059 return rc;
5060
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005061 iwl4965_write_direct32(priv, HBUS_TARG_MEM_RADDR, RTC_INST_LOWER_BOUND);
Zhu Yib481de92007-09-25 17:54:57 -07005062
5063 errcnt = 0;
5064 for (; len > 0; len -= sizeof(u32), image++) {
5065 /* read data comes through single port, auto-incr addr */
5066 /* NOTE: Use the debugless read so we don't flood kernel log
5067 * if IWL_DL_IO is set */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005068 val = _iwl4965_read_direct32(priv, HBUS_TARG_MEM_RDAT);
Zhu Yib481de92007-09-25 17:54:57 -07005069 if (val != le32_to_cpu(*image)) {
5070 IWL_ERROR("uCode INST section is invalid at "
5071 "offset 0x%x, is 0x%x, s/b 0x%x\n",
5072 save_len - len, val, le32_to_cpu(*image));
5073 rc = -EIO;
5074 errcnt++;
5075 if (errcnt >= 20)
5076 break;
5077 }
5078 }
5079
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005080 iwl4965_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005081
5082 if (!errcnt)
5083 IWL_DEBUG_INFO
5084 ("ucode image in INSTRUCTION memory is good\n");
5085
5086 return rc;
5087}
5088
5089
5090/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005091 * iwl4965_verify_inst_sparse - verify runtime uCode image in card vs. host,
Zhu Yib481de92007-09-25 17:54:57 -07005092 * using sample data 100 bytes apart. If these sample points are good,
5093 * it's a pretty good bet that everything between them is good, too.
5094 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005095static int iwl4965_verify_inst_sparse(struct iwl_priv *priv, __le32 *image, u32 len)
Zhu Yib481de92007-09-25 17:54:57 -07005096{
5097 u32 val;
5098 int rc = 0;
5099 u32 errcnt = 0;
5100 u32 i;
5101
5102 IWL_DEBUG_INFO("ucode inst image size is %u\n", len);
5103
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005104 rc = iwl4965_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005105 if (rc)
5106 return rc;
5107
5108 for (i = 0; i < len; i += 100, image += 100/sizeof(u32)) {
5109 /* read data comes through single port, auto-incr addr */
5110 /* NOTE: Use the debugless read so we don't flood kernel log
5111 * if IWL_DL_IO is set */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005112 iwl4965_write_direct32(priv, HBUS_TARG_MEM_RADDR,
Zhu Yib481de92007-09-25 17:54:57 -07005113 i + RTC_INST_LOWER_BOUND);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005114 val = _iwl4965_read_direct32(priv, HBUS_TARG_MEM_RDAT);
Zhu Yib481de92007-09-25 17:54:57 -07005115 if (val != le32_to_cpu(*image)) {
5116#if 0 /* Enable this if you want to see details */
5117 IWL_ERROR("uCode INST section is invalid at "
5118 "offset 0x%x, is 0x%x, s/b 0x%x\n",
5119 i, val, *image);
5120#endif
5121 rc = -EIO;
5122 errcnt++;
5123 if (errcnt >= 3)
5124 break;
5125 }
5126 }
5127
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005128 iwl4965_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005129
5130 return rc;
5131}
5132
5133
5134/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005135 * iwl4965_verify_ucode - determine which instruction image is in SRAM,
Zhu Yib481de92007-09-25 17:54:57 -07005136 * and verify its contents
5137 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005138static int iwl4965_verify_ucode(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005139{
5140 __le32 *image;
5141 u32 len;
5142 int rc = 0;
5143
5144 /* Try bootstrap */
5145 image = (__le32 *)priv->ucode_boot.v_addr;
5146 len = priv->ucode_boot.len;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005147 rc = iwl4965_verify_inst_sparse(priv, image, len);
Zhu Yib481de92007-09-25 17:54:57 -07005148 if (rc == 0) {
5149 IWL_DEBUG_INFO("Bootstrap uCode is good in inst SRAM\n");
5150 return 0;
5151 }
5152
5153 /* Try initialize */
5154 image = (__le32 *)priv->ucode_init.v_addr;
5155 len = priv->ucode_init.len;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005156 rc = iwl4965_verify_inst_sparse(priv, image, len);
Zhu Yib481de92007-09-25 17:54:57 -07005157 if (rc == 0) {
5158 IWL_DEBUG_INFO("Initialize uCode is good in inst SRAM\n");
5159 return 0;
5160 }
5161
5162 /* Try runtime/protocol */
5163 image = (__le32 *)priv->ucode_code.v_addr;
5164 len = priv->ucode_code.len;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005165 rc = iwl4965_verify_inst_sparse(priv, image, len);
Zhu Yib481de92007-09-25 17:54:57 -07005166 if (rc == 0) {
5167 IWL_DEBUG_INFO("Runtime uCode is good in inst SRAM\n");
5168 return 0;
5169 }
5170
5171 IWL_ERROR("NO VALID UCODE IMAGE IN INSTRUCTION SRAM!!\n");
5172
Ben Cahill9fbab512007-11-29 11:09:47 +08005173 /* Since nothing seems to match, show first several data entries in
5174 * instruction SRAM, so maybe visual inspection will give a clue.
5175 * Selection of bootstrap image (vs. other images) is arbitrary. */
Zhu Yib481de92007-09-25 17:54:57 -07005176 image = (__le32 *)priv->ucode_boot.v_addr;
5177 len = priv->ucode_boot.len;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005178 rc = iwl4965_verify_inst_full(priv, image, len);
Zhu Yib481de92007-09-25 17:54:57 -07005179
5180 return rc;
5181}
5182
5183
5184/* check contents of special bootstrap uCode SRAM */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005185static int iwl4965_verify_bsm(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005186{
5187 __le32 *image = priv->ucode_boot.v_addr;
5188 u32 len = priv->ucode_boot.len;
5189 u32 reg;
5190 u32 val;
5191
5192 IWL_DEBUG_INFO("Begin verify bsm\n");
5193
5194 /* verify BSM SRAM contents */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005195 val = iwl4965_read_prph(priv, BSM_WR_DWCOUNT_REG);
Zhu Yib481de92007-09-25 17:54:57 -07005196 for (reg = BSM_SRAM_LOWER_BOUND;
5197 reg < BSM_SRAM_LOWER_BOUND + len;
5198 reg += sizeof(u32), image ++) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005199 val = iwl4965_read_prph(priv, reg);
Zhu Yib481de92007-09-25 17:54:57 -07005200 if (val != le32_to_cpu(*image)) {
5201 IWL_ERROR("BSM uCode verification failed at "
5202 "addr 0x%08X+%u (of %u), is 0x%x, s/b 0x%x\n",
5203 BSM_SRAM_LOWER_BOUND,
5204 reg - BSM_SRAM_LOWER_BOUND, len,
5205 val, le32_to_cpu(*image));
5206 return -EIO;
5207 }
5208 }
5209
5210 IWL_DEBUG_INFO("BSM bootstrap uCode image OK\n");
5211
5212 return 0;
5213}
5214
5215/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005216 * iwl4965_load_bsm - Load bootstrap instructions
Zhu Yib481de92007-09-25 17:54:57 -07005217 *
5218 * BSM operation:
5219 *
5220 * The Bootstrap State Machine (BSM) stores a short bootstrap uCode program
5221 * in special SRAM that does not power down during RFKILL. When powering back
5222 * up after power-saving sleeps (or during initial uCode load), the BSM loads
5223 * the bootstrap program into the on-board processor, and starts it.
5224 *
5225 * The bootstrap program loads (via DMA) instructions and data for a new
5226 * program from host DRAM locations indicated by the host driver in the
5227 * BSM_DRAM_* registers. Once the new program is loaded, it starts
5228 * automatically.
5229 *
5230 * When initializing the NIC, the host driver points the BSM to the
5231 * "initialize" uCode image. This uCode sets up some internal data, then
5232 * notifies host via "initialize alive" that it is complete.
5233 *
5234 * The host then replaces the BSM_DRAM_* pointer values to point to the
5235 * normal runtime uCode instructions and a backup uCode data cache buffer
5236 * (filled initially with starting data values for the on-board processor),
5237 * then triggers the "initialize" uCode to load and launch the runtime uCode,
5238 * which begins normal operation.
5239 *
5240 * When doing a power-save shutdown, runtime uCode saves data SRAM into
5241 * the backup data cache in DRAM before SRAM is powered down.
5242 *
5243 * When powering back up, the BSM loads the bootstrap program. This reloads
5244 * the runtime uCode instructions and the backup data cache into SRAM,
5245 * and re-launches the runtime uCode from where it left off.
5246 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005247static int iwl4965_load_bsm(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005248{
5249 __le32 *image = priv->ucode_boot.v_addr;
5250 u32 len = priv->ucode_boot.len;
5251 dma_addr_t pinst;
5252 dma_addr_t pdata;
5253 u32 inst_len;
5254 u32 data_len;
5255 int rc;
5256 int i;
5257 u32 done;
5258 u32 reg_offset;
5259
5260 IWL_DEBUG_INFO("Begin load bsm\n");
5261
5262 /* make sure bootstrap program is no larger than BSM's SRAM size */
5263 if (len > IWL_MAX_BSM_SIZE)
5264 return -EINVAL;
5265
5266 /* Tell bootstrap uCode where to find the "Initialize" uCode
Ben Cahill9fbab512007-11-29 11:09:47 +08005267 * in host DRAM ... host DRAM physical address bits 35:4 for 4965.
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005268 * NOTE: iwl4965_initialize_alive_start() will replace these values,
Zhu Yib481de92007-09-25 17:54:57 -07005269 * after the "initialize" uCode has run, to point to
5270 * runtime/protocol instructions and backup data cache. */
5271 pinst = priv->ucode_init.p_addr >> 4;
5272 pdata = priv->ucode_init_data.p_addr >> 4;
5273 inst_len = priv->ucode_init.len;
5274 data_len = priv->ucode_init_data.len;
5275
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005276 rc = iwl4965_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005277 if (rc)
5278 return rc;
5279
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005280 iwl4965_write_prph(priv, BSM_DRAM_INST_PTR_REG, pinst);
5281 iwl4965_write_prph(priv, BSM_DRAM_DATA_PTR_REG, pdata);
5282 iwl4965_write_prph(priv, BSM_DRAM_INST_BYTECOUNT_REG, inst_len);
5283 iwl4965_write_prph(priv, BSM_DRAM_DATA_BYTECOUNT_REG, data_len);
Zhu Yib481de92007-09-25 17:54:57 -07005284
5285 /* Fill BSM memory with bootstrap instructions */
5286 for (reg_offset = BSM_SRAM_LOWER_BOUND;
5287 reg_offset < BSM_SRAM_LOWER_BOUND + len;
5288 reg_offset += sizeof(u32), image++)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005289 _iwl4965_write_prph(priv, reg_offset,
Zhu Yib481de92007-09-25 17:54:57 -07005290 le32_to_cpu(*image));
5291
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005292 rc = iwl4965_verify_bsm(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005293 if (rc) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005294 iwl4965_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005295 return rc;
5296 }
5297
5298 /* Tell BSM to copy from BSM SRAM into instruction SRAM, when asked */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005299 iwl4965_write_prph(priv, BSM_WR_MEM_SRC_REG, 0x0);
5300 iwl4965_write_prph(priv, BSM_WR_MEM_DST_REG,
Zhu Yib481de92007-09-25 17:54:57 -07005301 RTC_INST_LOWER_BOUND);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005302 iwl4965_write_prph(priv, BSM_WR_DWCOUNT_REG, len / sizeof(u32));
Zhu Yib481de92007-09-25 17:54:57 -07005303
5304 /* Load bootstrap code into instruction SRAM now,
5305 * to prepare to load "initialize" uCode */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005306 iwl4965_write_prph(priv, BSM_WR_CTRL_REG,
Zhu Yib481de92007-09-25 17:54:57 -07005307 BSM_WR_CTRL_REG_BIT_START);
5308
5309 /* Wait for load of bootstrap uCode to finish */
5310 for (i = 0; i < 100; i++) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005311 done = iwl4965_read_prph(priv, BSM_WR_CTRL_REG);
Zhu Yib481de92007-09-25 17:54:57 -07005312 if (!(done & BSM_WR_CTRL_REG_BIT_START))
5313 break;
5314 udelay(10);
5315 }
5316 if (i < 100)
5317 IWL_DEBUG_INFO("BSM write complete, poll %d iterations\n", i);
5318 else {
5319 IWL_ERROR("BSM write did not complete!\n");
5320 return -EIO;
5321 }
5322
5323 /* Enable future boot loads whenever power management unit triggers it
5324 * (e.g. when powering back up after power-save shutdown) */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005325 iwl4965_write_prph(priv, BSM_WR_CTRL_REG,
Zhu Yib481de92007-09-25 17:54:57 -07005326 BSM_WR_CTRL_REG_BIT_START_EN);
5327
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005328 iwl4965_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005329
5330 return 0;
5331}
5332
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005333static void iwl4965_nic_start(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005334{
5335 /* Remove all resets to allow NIC to operate */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005336 iwl4965_write32(priv, CSR_RESET, 0);
Zhu Yib481de92007-09-25 17:54:57 -07005337}
5338
Tomas Winkler90e759d2007-11-29 11:09:41 +08005339
Zhu Yib481de92007-09-25 17:54:57 -07005340/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005341 * iwl4965_read_ucode - Read uCode images from disk file.
Zhu Yib481de92007-09-25 17:54:57 -07005342 *
5343 * Copy into buffers for card to fetch via bus-mastering
5344 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005345static int iwl4965_read_ucode(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005346{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005347 struct iwl4965_ucode *ucode;
Tomas Winkler90e759d2007-11-29 11:09:41 +08005348 int ret;
Zhu Yib481de92007-09-25 17:54:57 -07005349 const struct firmware *ucode_raw;
Tomas Winkler4bf775c2008-03-04 18:09:31 -08005350 const char *name = priv->cfg->fw_name;
Zhu Yib481de92007-09-25 17:54:57 -07005351 u8 *src;
5352 size_t len;
5353 u32 ver, inst_size, data_size, init_size, init_data_size, boot_size;
5354
5355 /* Ask kernel firmware_class module to get the boot firmware off disk.
5356 * request_firmware() is synchronous, file is in memory on return. */
Tomas Winkler90e759d2007-11-29 11:09:41 +08005357 ret = request_firmware(&ucode_raw, name, &priv->pci_dev->dev);
5358 if (ret < 0) {
5359 IWL_ERROR("%s firmware file req failed: Reason %d\n",
5360 name, ret);
Zhu Yib481de92007-09-25 17:54:57 -07005361 goto error;
5362 }
5363
5364 IWL_DEBUG_INFO("Got firmware '%s' file (%zd bytes) from disk\n",
5365 name, ucode_raw->size);
5366
5367 /* Make sure that we got at least our header! */
5368 if (ucode_raw->size < sizeof(*ucode)) {
5369 IWL_ERROR("File size way too small!\n");
Tomas Winkler90e759d2007-11-29 11:09:41 +08005370 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07005371 goto err_release;
5372 }
5373
5374 /* Data from ucode file: header followed by uCode images */
5375 ucode = (void *)ucode_raw->data;
5376
5377 ver = le32_to_cpu(ucode->ver);
5378 inst_size = le32_to_cpu(ucode->inst_size);
5379 data_size = le32_to_cpu(ucode->data_size);
5380 init_size = le32_to_cpu(ucode->init_size);
5381 init_data_size = le32_to_cpu(ucode->init_data_size);
5382 boot_size = le32_to_cpu(ucode->boot_size);
5383
5384 IWL_DEBUG_INFO("f/w package hdr ucode version = 0x%x\n", ver);
5385 IWL_DEBUG_INFO("f/w package hdr runtime inst size = %u\n",
5386 inst_size);
5387 IWL_DEBUG_INFO("f/w package hdr runtime data size = %u\n",
5388 data_size);
5389 IWL_DEBUG_INFO("f/w package hdr init inst size = %u\n",
5390 init_size);
5391 IWL_DEBUG_INFO("f/w package hdr init data size = %u\n",
5392 init_data_size);
5393 IWL_DEBUG_INFO("f/w package hdr boot inst size = %u\n",
5394 boot_size);
5395
5396 /* Verify size of file vs. image size info in file's header */
5397 if (ucode_raw->size < sizeof(*ucode) +
5398 inst_size + data_size + init_size +
5399 init_data_size + boot_size) {
5400
5401 IWL_DEBUG_INFO("uCode file size %d too small\n",
5402 (int)ucode_raw->size);
Tomas Winkler90e759d2007-11-29 11:09:41 +08005403 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07005404 goto err_release;
5405 }
5406
5407 /* Verify that uCode images will fit in card's SRAM */
5408 if (inst_size > IWL_MAX_INST_SIZE) {
Tomas Winkler90e759d2007-11-29 11:09:41 +08005409 IWL_DEBUG_INFO("uCode instr len %d too large to fit in\n",
5410 inst_size);
5411 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07005412 goto err_release;
5413 }
5414
5415 if (data_size > IWL_MAX_DATA_SIZE) {
Tomas Winkler90e759d2007-11-29 11:09:41 +08005416 IWL_DEBUG_INFO("uCode data len %d too large to fit in\n",
5417 data_size);
5418 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07005419 goto err_release;
5420 }
5421 if (init_size > IWL_MAX_INST_SIZE) {
5422 IWL_DEBUG_INFO
Tomas Winkler90e759d2007-11-29 11:09:41 +08005423 ("uCode init instr len %d too large to fit in\n",
5424 init_size);
5425 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07005426 goto err_release;
5427 }
5428 if (init_data_size > IWL_MAX_DATA_SIZE) {
5429 IWL_DEBUG_INFO
Tomas Winkler90e759d2007-11-29 11:09:41 +08005430 ("uCode init data len %d too large to fit in\n",
5431 init_data_size);
5432 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07005433 goto err_release;
5434 }
5435 if (boot_size > IWL_MAX_BSM_SIZE) {
5436 IWL_DEBUG_INFO
Tomas Winkler90e759d2007-11-29 11:09:41 +08005437 ("uCode boot instr len %d too large to fit in\n",
5438 boot_size);
5439 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07005440 goto err_release;
5441 }
5442
5443 /* Allocate ucode buffers for card's bus-master loading ... */
5444
5445 /* Runtime instructions and 2 copies of data:
5446 * 1) unmodified from disk
5447 * 2) backup cache for save/restore during power-downs */
5448 priv->ucode_code.len = inst_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08005449 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_code);
Zhu Yib481de92007-09-25 17:54:57 -07005450
5451 priv->ucode_data.len = data_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08005452 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_data);
Zhu Yib481de92007-09-25 17:54:57 -07005453
5454 priv->ucode_data_backup.len = data_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08005455 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_data_backup);
Zhu Yib481de92007-09-25 17:54:57 -07005456
5457 /* Initialization instructions and data */
Tomas Winkler90e759d2007-11-29 11:09:41 +08005458 if (init_size && init_data_size) {
5459 priv->ucode_init.len = init_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08005460 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_init);
Zhu Yib481de92007-09-25 17:54:57 -07005461
Tomas Winkler90e759d2007-11-29 11:09:41 +08005462 priv->ucode_init_data.len = init_data_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08005463 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_init_data);
Tomas Winkler90e759d2007-11-29 11:09:41 +08005464
5465 if (!priv->ucode_init.v_addr || !priv->ucode_init_data.v_addr)
5466 goto err_pci_alloc;
5467 }
Zhu Yib481de92007-09-25 17:54:57 -07005468
5469 /* Bootstrap (instructions only, no data) */
Tomas Winkler90e759d2007-11-29 11:09:41 +08005470 if (boot_size) {
5471 priv->ucode_boot.len = boot_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08005472 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_boot);
Zhu Yib481de92007-09-25 17:54:57 -07005473
Tomas Winkler90e759d2007-11-29 11:09:41 +08005474 if (!priv->ucode_boot.v_addr)
5475 goto err_pci_alloc;
5476 }
Zhu Yib481de92007-09-25 17:54:57 -07005477
5478 /* Copy images into buffers for card's bus-master reads ... */
5479
5480 /* Runtime instructions (first block of data in file) */
5481 src = &ucode->data[0];
5482 len = priv->ucode_code.len;
Tomas Winkler90e759d2007-11-29 11:09:41 +08005483 IWL_DEBUG_INFO("Copying (but not loading) uCode instr len %Zd\n", len);
Zhu Yib481de92007-09-25 17:54:57 -07005484 memcpy(priv->ucode_code.v_addr, src, len);
5485 IWL_DEBUG_INFO("uCode instr buf vaddr = 0x%p, paddr = 0x%08x\n",
5486 priv->ucode_code.v_addr, (u32)priv->ucode_code.p_addr);
5487
5488 /* Runtime data (2nd block)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005489 * NOTE: Copy into backup buffer will be done in iwl4965_up() */
Zhu Yib481de92007-09-25 17:54:57 -07005490 src = &ucode->data[inst_size];
5491 len = priv->ucode_data.len;
Tomas Winkler90e759d2007-11-29 11:09:41 +08005492 IWL_DEBUG_INFO("Copying (but not loading) uCode data len %Zd\n", len);
Zhu Yib481de92007-09-25 17:54:57 -07005493 memcpy(priv->ucode_data.v_addr, src, len);
5494 memcpy(priv->ucode_data_backup.v_addr, src, len);
5495
5496 /* Initialization instructions (3rd block) */
5497 if (init_size) {
5498 src = &ucode->data[inst_size + data_size];
5499 len = priv->ucode_init.len;
Tomas Winkler90e759d2007-11-29 11:09:41 +08005500 IWL_DEBUG_INFO("Copying (but not loading) init instr len %Zd\n",
5501 len);
Zhu Yib481de92007-09-25 17:54:57 -07005502 memcpy(priv->ucode_init.v_addr, src, len);
5503 }
5504
5505 /* Initialization data (4th block) */
5506 if (init_data_size) {
5507 src = &ucode->data[inst_size + data_size + init_size];
5508 len = priv->ucode_init_data.len;
Tomas Winkler90e759d2007-11-29 11:09:41 +08005509 IWL_DEBUG_INFO("Copying (but not loading) init data len %Zd\n",
5510 len);
Zhu Yib481de92007-09-25 17:54:57 -07005511 memcpy(priv->ucode_init_data.v_addr, src, len);
5512 }
5513
5514 /* Bootstrap instructions (5th block) */
5515 src = &ucode->data[inst_size + data_size + init_size + init_data_size];
5516 len = priv->ucode_boot.len;
Tomas Winkler90e759d2007-11-29 11:09:41 +08005517 IWL_DEBUG_INFO("Copying (but not loading) boot instr len %Zd\n", len);
Zhu Yib481de92007-09-25 17:54:57 -07005518 memcpy(priv->ucode_boot.v_addr, src, len);
5519
5520 /* We have our copies now, allow OS release its copies */
5521 release_firmware(ucode_raw);
5522 return 0;
5523
5524 err_pci_alloc:
5525 IWL_ERROR("failed to allocate pci memory\n");
Tomas Winkler90e759d2007-11-29 11:09:41 +08005526 ret = -ENOMEM;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005527 iwl4965_dealloc_ucode_pci(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005528
5529 err_release:
5530 release_firmware(ucode_raw);
5531
5532 error:
Tomas Winkler90e759d2007-11-29 11:09:41 +08005533 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07005534}
5535
5536
5537/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005538 * iwl4965_set_ucode_ptrs - Set uCode address location
Zhu Yib481de92007-09-25 17:54:57 -07005539 *
5540 * Tell initialization uCode where to find runtime uCode.
5541 *
5542 * BSM registers initially contain pointers to initialization uCode.
5543 * We need to replace them to load runtime uCode inst and data,
5544 * and to save runtime data when powering down.
5545 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005546static int iwl4965_set_ucode_ptrs(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005547{
5548 dma_addr_t pinst;
5549 dma_addr_t pdata;
5550 int rc = 0;
5551 unsigned long flags;
5552
5553 /* bits 35:4 for 4965 */
5554 pinst = priv->ucode_code.p_addr >> 4;
5555 pdata = priv->ucode_data_backup.p_addr >> 4;
5556
5557 spin_lock_irqsave(&priv->lock, flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005558 rc = iwl4965_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005559 if (rc) {
5560 spin_unlock_irqrestore(&priv->lock, flags);
5561 return rc;
5562 }
5563
5564 /* Tell bootstrap uCode where to find image to load */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005565 iwl4965_write_prph(priv, BSM_DRAM_INST_PTR_REG, pinst);
5566 iwl4965_write_prph(priv, BSM_DRAM_DATA_PTR_REG, pdata);
5567 iwl4965_write_prph(priv, BSM_DRAM_DATA_BYTECOUNT_REG,
Zhu Yib481de92007-09-25 17:54:57 -07005568 priv->ucode_data.len);
5569
5570 /* Inst bytecount must be last to set up, bit 31 signals uCode
5571 * that all new ptr/size info is in place */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005572 iwl4965_write_prph(priv, BSM_DRAM_INST_BYTECOUNT_REG,
Zhu Yib481de92007-09-25 17:54:57 -07005573 priv->ucode_code.len | BSM_DRAM_INST_LOAD);
5574
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005575 iwl4965_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005576
5577 spin_unlock_irqrestore(&priv->lock, flags);
5578
5579 IWL_DEBUG_INFO("Runtime uCode pointers are set.\n");
5580
5581 return rc;
5582}
5583
5584/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005585 * iwl4965_init_alive_start - Called after REPLY_ALIVE notification received
Zhu Yib481de92007-09-25 17:54:57 -07005586 *
5587 * Called after REPLY_ALIVE notification received from "initialize" uCode.
5588 *
5589 * The 4965 "initialize" ALIVE reply contains calibration data for:
5590 * Voltage, temperature, and MIMO tx gain correction, now stored in priv
5591 * (3945 does not contain this data).
5592 *
5593 * Tell "initialize" uCode to go ahead and load the runtime uCode.
5594*/
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005595static void iwl4965_init_alive_start(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005596{
5597 /* Check alive response for "valid" sign from uCode */
5598 if (priv->card_alive_init.is_valid != UCODE_VALID_OK) {
5599 /* We had an error bringing up the hardware, so take it
5600 * all the way back down so we can try again */
5601 IWL_DEBUG_INFO("Initialize Alive failed.\n");
5602 goto restart;
5603 }
5604
5605 /* Bootstrap uCode has loaded initialize uCode ... verify inst image.
5606 * This is a paranoid check, because we would not have gotten the
5607 * "initialize" alive if code weren't properly loaded. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005608 if (iwl4965_verify_ucode(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07005609 /* Runtime instruction load was bad;
5610 * take it all the way back down so we can try again */
5611 IWL_DEBUG_INFO("Bad \"initialize\" uCode load.\n");
5612 goto restart;
5613 }
5614
5615 /* Calculate temperature */
5616 priv->temperature = iwl4965_get_temperature(priv);
5617
5618 /* Send pointers to protocol/runtime uCode image ... init code will
5619 * load and launch runtime uCode, which will send us another "Alive"
5620 * notification. */
5621 IWL_DEBUG_INFO("Initialization Alive received.\n");
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005622 if (iwl4965_set_ucode_ptrs(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07005623 /* Runtime instruction load won't happen;
5624 * take it all the way back down so we can try again */
5625 IWL_DEBUG_INFO("Couldn't set up uCode pointers.\n");
5626 goto restart;
5627 }
5628 return;
5629
5630 restart:
5631 queue_work(priv->workqueue, &priv->restart);
5632}
5633
5634
5635/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005636 * iwl4965_alive_start - called after REPLY_ALIVE notification received
Zhu Yib481de92007-09-25 17:54:57 -07005637 * from protocol/runtime uCode (initialization uCode's
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005638 * Alive gets handled by iwl4965_init_alive_start()).
Zhu Yib481de92007-09-25 17:54:57 -07005639 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005640static void iwl4965_alive_start(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005641{
5642 int rc = 0;
5643
5644 IWL_DEBUG_INFO("Runtime Alive received.\n");
5645
5646 if (priv->card_alive.is_valid != UCODE_VALID_OK) {
5647 /* We had an error bringing up the hardware, so take it
5648 * all the way back down so we can try again */
5649 IWL_DEBUG_INFO("Alive failed.\n");
5650 goto restart;
5651 }
5652
5653 /* Initialize uCode has loaded Runtime uCode ... verify inst image.
5654 * This is a paranoid check, because we would not have gotten the
5655 * "runtime" alive if code weren't properly loaded. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005656 if (iwl4965_verify_ucode(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07005657 /* Runtime instruction load was bad;
5658 * take it all the way back down so we can try again */
5659 IWL_DEBUG_INFO("Bad runtime uCode load.\n");
5660 goto restart;
5661 }
5662
Assaf Kraussbf85ea42008-03-14 10:38:49 -07005663 iwlcore_clear_stations_table(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005664
5665 rc = iwl4965_alive_notify(priv);
5666 if (rc) {
5667 IWL_WARNING("Could not complete ALIVE transition [ntf]: %d\n",
5668 rc);
5669 goto restart;
5670 }
5671
Ben Cahill9fbab512007-11-29 11:09:47 +08005672 /* After the ALIVE response, we can send host commands to 4965 uCode */
Zhu Yib481de92007-09-25 17:54:57 -07005673 set_bit(STATUS_ALIVE, &priv->status);
5674
5675 /* Clear out the uCode error bit if it is set */
5676 clear_bit(STATUS_FW_ERROR, &priv->status);
5677
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005678 if (iwl4965_is_rfkill(priv))
Zhu Yib481de92007-09-25 17:54:57 -07005679 return;
5680
Zhu Yi5a669262008-01-14 17:46:18 -08005681 ieee80211_start_queues(priv->hw);
Zhu Yib481de92007-09-25 17:54:57 -07005682
5683 priv->active_rate = priv->rates_mask;
5684 priv->active_rate_basic = priv->rates_mask & IWL_BASIC_RATES_MASK;
5685
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005686 iwl4965_send_power_mode(priv, IWL_POWER_LEVEL(priv->power_mode));
Zhu Yib481de92007-09-25 17:54:57 -07005687
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005688 if (iwl4965_is_associated(priv)) {
5689 struct iwl4965_rxon_cmd *active_rxon =
5690 (struct iwl4965_rxon_cmd *)(&priv->active_rxon);
Zhu Yib481de92007-09-25 17:54:57 -07005691
5692 memcpy(&priv->staging_rxon, &priv->active_rxon,
5693 sizeof(priv->staging_rxon));
5694 active_rxon->filter_flags &= ~RXON_FILTER_ASSOC_MSK;
5695 } else {
5696 /* Initialize our rx_config data */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005697 iwl4965_connection_init_rx_config(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005698 memcpy(priv->staging_rxon.node_addr, priv->mac_addr, ETH_ALEN);
5699 }
5700
Ben Cahill9fbab512007-11-29 11:09:47 +08005701 /* Configure Bluetooth device coexistence support */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005702 iwl4965_send_bt_config(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005703
5704 /* Configure the adapter for unassociated operation */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005705 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005706
5707 /* At this point, the NIC is initialized and operational */
5708 priv->notif_missed_beacons = 0;
5709 set_bit(STATUS_READY, &priv->status);
5710
5711 iwl4965_rf_kill_ct_config(priv);
Zhu Yi5a669262008-01-14 17:46:18 -08005712
Zhu Yib481de92007-09-25 17:54:57 -07005713 IWL_DEBUG_INFO("ALIVE processing complete.\n");
Zhu Yi5a669262008-01-14 17:46:18 -08005714 wake_up_interruptible(&priv->wait_command_queue);
Zhu Yib481de92007-09-25 17:54:57 -07005715
Mohamed Abbasab53d8a2008-03-25 16:33:36 -07005716 iwl_leds_register(priv);
5717
Zhu Yib481de92007-09-25 17:54:57 -07005718 if (priv->error_recovering)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005719 iwl4965_error_recovery(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005720
5721 return;
5722
5723 restart:
5724 queue_work(priv->workqueue, &priv->restart);
5725}
5726
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005727static void iwl4965_cancel_deferred_work(struct iwl_priv *priv);
Zhu Yib481de92007-09-25 17:54:57 -07005728
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005729static void __iwl4965_down(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005730{
5731 unsigned long flags;
5732 int exit_pending = test_bit(STATUS_EXIT_PENDING, &priv->status);
5733 struct ieee80211_conf *conf = NULL;
5734
5735 IWL_DEBUG_INFO(DRV_NAME " is going down\n");
5736
5737 conf = ieee80211_get_hw_conf(priv->hw);
5738
5739 if (!exit_pending)
5740 set_bit(STATUS_EXIT_PENDING, &priv->status);
5741
Mohamed Abbasab53d8a2008-03-25 16:33:36 -07005742 iwl_leds_unregister(priv);
5743
Assaf Kraussbf85ea42008-03-14 10:38:49 -07005744 iwlcore_clear_stations_table(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005745
5746 /* Unblock any waiting calls */
5747 wake_up_interruptible_all(&priv->wait_command_queue);
5748
Zhu Yib481de92007-09-25 17:54:57 -07005749 /* Wipe out the EXIT_PENDING status bit if we are not actually
5750 * exiting the module */
5751 if (!exit_pending)
5752 clear_bit(STATUS_EXIT_PENDING, &priv->status);
5753
5754 /* stop and reset the on-board processor */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005755 iwl4965_write32(priv, CSR_RESET, CSR_RESET_REG_FLAG_NEVO_RESET);
Zhu Yib481de92007-09-25 17:54:57 -07005756
5757 /* tell the device to stop sending interrupts */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005758 iwl4965_disable_interrupts(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005759
5760 if (priv->mac80211_registered)
5761 ieee80211_stop_queues(priv->hw);
5762
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005763 /* If we have not previously called iwl4965_init() then
Zhu Yib481de92007-09-25 17:54:57 -07005764 * clear all bits but the RF Kill and SUSPEND bits and return */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005765 if (!iwl4965_is_init(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07005766 priv->status = test_bit(STATUS_RF_KILL_HW, &priv->status) <<
5767 STATUS_RF_KILL_HW |
5768 test_bit(STATUS_RF_KILL_SW, &priv->status) <<
5769 STATUS_RF_KILL_SW |
Reinette Chatre97888642008-02-06 11:20:38 -08005770 test_bit(STATUS_GEO_CONFIGURED, &priv->status) <<
5771 STATUS_GEO_CONFIGURED |
Zhu Yib481de92007-09-25 17:54:57 -07005772 test_bit(STATUS_IN_SUSPEND, &priv->status) <<
5773 STATUS_IN_SUSPEND;
5774 goto exit;
5775 }
5776
5777 /* ...otherwise clear out all the status bits but the RF Kill and
5778 * SUSPEND bits and continue taking the NIC down. */
5779 priv->status &= test_bit(STATUS_RF_KILL_HW, &priv->status) <<
5780 STATUS_RF_KILL_HW |
5781 test_bit(STATUS_RF_KILL_SW, &priv->status) <<
5782 STATUS_RF_KILL_SW |
Reinette Chatre97888642008-02-06 11:20:38 -08005783 test_bit(STATUS_GEO_CONFIGURED, &priv->status) <<
5784 STATUS_GEO_CONFIGURED |
Zhu Yib481de92007-09-25 17:54:57 -07005785 test_bit(STATUS_IN_SUSPEND, &priv->status) <<
5786 STATUS_IN_SUSPEND |
5787 test_bit(STATUS_FW_ERROR, &priv->status) <<
5788 STATUS_FW_ERROR;
5789
5790 spin_lock_irqsave(&priv->lock, flags);
Ben Cahill9fbab512007-11-29 11:09:47 +08005791 iwl4965_clear_bit(priv, CSR_GP_CNTRL,
5792 CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
Zhu Yib481de92007-09-25 17:54:57 -07005793 spin_unlock_irqrestore(&priv->lock, flags);
5794
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005795 iwl4965_hw_txq_ctx_stop(priv);
5796 iwl4965_hw_rxq_stop(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005797
5798 spin_lock_irqsave(&priv->lock, flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005799 if (!iwl4965_grab_nic_access(priv)) {
5800 iwl4965_write_prph(priv, APMG_CLK_DIS_REG,
Zhu Yib481de92007-09-25 17:54:57 -07005801 APMG_CLK_VAL_DMA_CLK_RQT);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005802 iwl4965_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005803 }
5804 spin_unlock_irqrestore(&priv->lock, flags);
5805
5806 udelay(5);
5807
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005808 iwl4965_hw_nic_stop_master(priv);
5809 iwl4965_set_bit(priv, CSR_RESET, CSR_RESET_REG_FLAG_SW_RESET);
5810 iwl4965_hw_nic_reset(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005811
5812 exit:
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005813 memset(&priv->card_alive, 0, sizeof(struct iwl4965_alive_resp));
Zhu Yib481de92007-09-25 17:54:57 -07005814
5815 if (priv->ibss_beacon)
5816 dev_kfree_skb(priv->ibss_beacon);
5817 priv->ibss_beacon = NULL;
5818
5819 /* clear out any free frames */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005820 iwl4965_clear_free_frames(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005821}
5822
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005823static void iwl4965_down(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005824{
5825 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005826 __iwl4965_down(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005827 mutex_unlock(&priv->mutex);
Zhu Yib24d22b2007-12-19 13:59:52 +08005828
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005829 iwl4965_cancel_deferred_work(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005830}
5831
5832#define MAX_HW_RESTARTS 5
5833
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005834static int __iwl4965_up(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005835{
5836 int rc, i;
Zhu Yib481de92007-09-25 17:54:57 -07005837
5838 if (test_bit(STATUS_EXIT_PENDING, &priv->status)) {
5839 IWL_WARNING("Exit pending; will not bring the NIC up\n");
5840 return -EIO;
5841 }
5842
5843 if (test_bit(STATUS_RF_KILL_SW, &priv->status)) {
5844 IWL_WARNING("Radio disabled by SW RF kill (module "
5845 "parameter)\n");
Zhu Yie655b9f2008-01-24 02:19:38 -08005846 return -ENODEV;
5847 }
5848
Reinette Chatree903fbd2008-01-30 22:05:15 -08005849 if (!priv->ucode_data_backup.v_addr || !priv->ucode_data.v_addr) {
5850 IWL_ERROR("ucode not available for device bringup\n");
5851 return -EIO;
5852 }
5853
Zhu Yie655b9f2008-01-24 02:19:38 -08005854 /* If platform's RF_KILL switch is NOT set to KILL */
5855 if (iwl4965_read32(priv, CSR_GP_CNTRL) &
5856 CSR_GP_CNTRL_REG_FLAG_HW_RF_KILL_SW)
5857 clear_bit(STATUS_RF_KILL_HW, &priv->status);
5858 else {
5859 set_bit(STATUS_RF_KILL_HW, &priv->status);
5860 if (!test_bit(STATUS_IN_SUSPEND, &priv->status)) {
5861 IWL_WARNING("Radio disabled by HW RF Kill switch\n");
5862 return -ENODEV;
5863 }
Zhu Yib481de92007-09-25 17:54:57 -07005864 }
5865
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005866 iwl4965_write32(priv, CSR_INT, 0xFFFFFFFF);
Zhu Yib481de92007-09-25 17:54:57 -07005867
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005868 rc = iwl4965_hw_nic_init(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005869 if (rc) {
5870 IWL_ERROR("Unable to int nic\n");
5871 return rc;
5872 }
5873
5874 /* make sure rfkill handshake bits are cleared */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005875 iwl4965_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
5876 iwl4965_write32(priv, CSR_UCODE_DRV_GP1_CLR,
Zhu Yib481de92007-09-25 17:54:57 -07005877 CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
5878
5879 /* clear (again), then enable host interrupts */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005880 iwl4965_write32(priv, CSR_INT, 0xFFFFFFFF);
5881 iwl4965_enable_interrupts(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005882
5883 /* really make sure rfkill handshake bits are cleared */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005884 iwl4965_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
5885 iwl4965_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
Zhu Yib481de92007-09-25 17:54:57 -07005886
5887 /* Copy original ucode data image from disk into backup cache.
5888 * This will be used to initialize the on-board processor's
5889 * data SRAM for a clean start when the runtime program first loads. */
5890 memcpy(priv->ucode_data_backup.v_addr, priv->ucode_data.v_addr,
Zhu Yi5a669262008-01-14 17:46:18 -08005891 priv->ucode_data.len);
Zhu Yib481de92007-09-25 17:54:57 -07005892
Zhu Yie655b9f2008-01-24 02:19:38 -08005893 /* We return success when we resume from suspend and rf_kill is on. */
5894 if (test_bit(STATUS_RF_KILL_HW, &priv->status))
Zhu Yib481de92007-09-25 17:54:57 -07005895 return 0;
Zhu Yib481de92007-09-25 17:54:57 -07005896
5897 for (i = 0; i < MAX_HW_RESTARTS; i++) {
5898
Assaf Kraussbf85ea42008-03-14 10:38:49 -07005899 iwlcore_clear_stations_table(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005900
5901 /* load bootstrap state machine,
5902 * load bootstrap program into processor's memory,
5903 * prepare to load the "initialize" uCode */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005904 rc = iwl4965_load_bsm(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005905
5906 if (rc) {
5907 IWL_ERROR("Unable to set up bootstrap uCode: %d\n", rc);
5908 continue;
5909 }
5910
5911 /* start card; "initialize" will load runtime ucode */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005912 iwl4965_nic_start(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005913
Zhu Yib481de92007-09-25 17:54:57 -07005914 IWL_DEBUG_INFO(DRV_NAME " is coming up\n");
5915
5916 return 0;
5917 }
5918
5919 set_bit(STATUS_EXIT_PENDING, &priv->status);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005920 __iwl4965_down(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005921
5922 /* tried to restart and config the device for as long as our
5923 * patience could withstand */
5924 IWL_ERROR("Unable to initialize device after %d attempts.\n", i);
5925 return -EIO;
5926}
5927
5928
5929/*****************************************************************************
5930 *
5931 * Workqueue callbacks
5932 *
5933 *****************************************************************************/
5934
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005935static void iwl4965_bg_init_alive_start(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07005936{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005937 struct iwl_priv *priv =
5938 container_of(data, struct iwl_priv, init_alive_start.work);
Zhu Yib481de92007-09-25 17:54:57 -07005939
5940 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
5941 return;
5942
5943 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005944 iwl4965_init_alive_start(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005945 mutex_unlock(&priv->mutex);
5946}
5947
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005948static void iwl4965_bg_alive_start(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07005949{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005950 struct iwl_priv *priv =
5951 container_of(data, struct iwl_priv, alive_start.work);
Zhu Yib481de92007-09-25 17:54:57 -07005952
5953 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
5954 return;
5955
5956 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005957 iwl4965_alive_start(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005958 mutex_unlock(&priv->mutex);
5959}
5960
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005961static void iwl4965_bg_rf_kill(struct work_struct *work)
Zhu Yib481de92007-09-25 17:54:57 -07005962{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005963 struct iwl_priv *priv = container_of(work, struct iwl_priv, rf_kill);
Zhu Yib481de92007-09-25 17:54:57 -07005964
5965 wake_up_interruptible(&priv->wait_command_queue);
5966
5967 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
5968 return;
5969
5970 mutex_lock(&priv->mutex);
5971
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005972 if (!iwl4965_is_rfkill(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07005973 IWL_DEBUG(IWL_DL_INFO | IWL_DL_RF_KILL,
5974 "HW and/or SW RF Kill no longer active, restarting "
5975 "device\n");
5976 if (!test_bit(STATUS_EXIT_PENDING, &priv->status))
5977 queue_work(priv->workqueue, &priv->restart);
5978 } else {
5979
5980 if (!test_bit(STATUS_RF_KILL_HW, &priv->status))
5981 IWL_DEBUG_RF_KILL("Can not turn radio back on - "
5982 "disabled by SW switch\n");
5983 else
5984 IWL_WARNING("Radio Frequency Kill Switch is On:\n"
5985 "Kill switch must be turned off for "
5986 "wireless networking to work.\n");
5987 }
5988 mutex_unlock(&priv->mutex);
5989}
5990
5991#define IWL_SCAN_CHECK_WATCHDOG (7 * HZ)
5992
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005993static void iwl4965_bg_scan_check(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07005994{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005995 struct iwl_priv *priv =
5996 container_of(data, struct iwl_priv, scan_check.work);
Zhu Yib481de92007-09-25 17:54:57 -07005997
5998 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
5999 return;
6000
6001 mutex_lock(&priv->mutex);
6002 if (test_bit(STATUS_SCANNING, &priv->status) ||
6003 test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
6004 IWL_DEBUG(IWL_DL_INFO | IWL_DL_SCAN,
6005 "Scan completion watchdog resetting adapter (%dms)\n",
6006 jiffies_to_msecs(IWL_SCAN_CHECK_WATCHDOG));
mabbas052c4b92007-10-25 17:15:43 +08006007
Zhu Yib481de92007-09-25 17:54:57 -07006008 if (!test_bit(STATUS_EXIT_PENDING, &priv->status))
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006009 iwl4965_send_scan_abort(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006010 }
6011 mutex_unlock(&priv->mutex);
6012}
6013
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006014static void iwl4965_bg_request_scan(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07006015{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006016 struct iwl_priv *priv =
6017 container_of(data, struct iwl_priv, request_scan);
Tomas Winkler857485c2008-03-21 13:53:44 -07006018 struct iwl_host_cmd cmd = {
Zhu Yib481de92007-09-25 17:54:57 -07006019 .id = REPLY_SCAN_CMD,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006020 .len = sizeof(struct iwl4965_scan_cmd),
Zhu Yib481de92007-09-25 17:54:57 -07006021 .meta.flags = CMD_SIZE_HUGE,
6022 };
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006023 struct iwl4965_scan_cmd *scan;
Zhu Yib481de92007-09-25 17:54:57 -07006024 struct ieee80211_conf *conf = NULL;
Tomas Winkler78330fd2008-02-06 02:37:18 +02006025 u16 cmd_len;
Johannes Berg8318d782008-01-24 19:38:38 +01006026 enum ieee80211_band band;
Tomas Winkler78330fd2008-02-06 02:37:18 +02006027 u8 direct_mask;
Tomas Winkler857485c2008-03-21 13:53:44 -07006028 int ret = 0;
Zhu Yib481de92007-09-25 17:54:57 -07006029
6030 conf = ieee80211_get_hw_conf(priv->hw);
6031
6032 mutex_lock(&priv->mutex);
6033
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006034 if (!iwl4965_is_ready(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07006035 IWL_WARNING("request scan called when driver not ready.\n");
6036 goto done;
6037 }
6038
6039 /* Make sure the scan wasn't cancelled before this queued work
6040 * was given the chance to run... */
6041 if (!test_bit(STATUS_SCANNING, &priv->status))
6042 goto done;
6043
6044 /* This should never be called or scheduled if there is currently
6045 * a scan active in the hardware. */
6046 if (test_bit(STATUS_SCAN_HW, &priv->status)) {
6047 IWL_DEBUG_INFO("Multiple concurrent scan requests in parallel. "
6048 "Ignoring second request.\n");
Tomas Winkler857485c2008-03-21 13:53:44 -07006049 ret = -EIO;
Zhu Yib481de92007-09-25 17:54:57 -07006050 goto done;
6051 }
6052
6053 if (test_bit(STATUS_EXIT_PENDING, &priv->status)) {
6054 IWL_DEBUG_SCAN("Aborting scan due to device shutdown\n");
6055 goto done;
6056 }
6057
6058 if (test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
6059 IWL_DEBUG_HC("Scan request while abort pending. Queuing.\n");
6060 goto done;
6061 }
6062
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006063 if (iwl4965_is_rfkill(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07006064 IWL_DEBUG_HC("Aborting scan due to RF Kill activation\n");
6065 goto done;
6066 }
6067
6068 if (!test_bit(STATUS_READY, &priv->status)) {
6069 IWL_DEBUG_HC("Scan request while uninitialized. Queuing.\n");
6070 goto done;
6071 }
6072
6073 if (!priv->scan_bands) {
6074 IWL_DEBUG_HC("Aborting scan due to no requested bands\n");
6075 goto done;
6076 }
6077
6078 if (!priv->scan) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006079 priv->scan = kmalloc(sizeof(struct iwl4965_scan_cmd) +
Zhu Yib481de92007-09-25 17:54:57 -07006080 IWL_MAX_SCAN_SIZE, GFP_KERNEL);
6081 if (!priv->scan) {
Tomas Winkler857485c2008-03-21 13:53:44 -07006082 ret = -ENOMEM;
Zhu Yib481de92007-09-25 17:54:57 -07006083 goto done;
6084 }
6085 }
6086 scan = priv->scan;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006087 memset(scan, 0, sizeof(struct iwl4965_scan_cmd) + IWL_MAX_SCAN_SIZE);
Zhu Yib481de92007-09-25 17:54:57 -07006088
6089 scan->quiet_plcp_th = IWL_PLCP_QUIET_THRESH;
6090 scan->quiet_time = IWL_ACTIVE_QUIET_TIME;
6091
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006092 if (iwl4965_is_associated(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07006093 u16 interval = 0;
6094 u32 extra;
6095 u32 suspend_time = 100;
6096 u32 scan_suspend_time = 100;
6097 unsigned long flags;
6098
6099 IWL_DEBUG_INFO("Scanning while associated...\n");
6100
6101 spin_lock_irqsave(&priv->lock, flags);
6102 interval = priv->beacon_int;
6103 spin_unlock_irqrestore(&priv->lock, flags);
6104
6105 scan->suspend_time = 0;
mabbas052c4b92007-10-25 17:15:43 +08006106 scan->max_out_time = cpu_to_le32(200 * 1024);
Zhu Yib481de92007-09-25 17:54:57 -07006107 if (!interval)
6108 interval = suspend_time;
6109
6110 extra = (suspend_time / interval) << 22;
6111 scan_suspend_time = (extra |
6112 ((suspend_time % interval) * 1024));
6113 scan->suspend_time = cpu_to_le32(scan_suspend_time);
6114 IWL_DEBUG_SCAN("suspend_time 0x%X beacon interval %d\n",
6115 scan_suspend_time, interval);
6116 }
6117
6118 /* We should add the ability for user to lock to PASSIVE ONLY */
6119 if (priv->one_direct_scan) {
6120 IWL_DEBUG_SCAN
6121 ("Kicking off one direct scan for '%s'\n",
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006122 iwl4965_escape_essid(priv->direct_ssid,
Zhu Yib481de92007-09-25 17:54:57 -07006123 priv->direct_ssid_len));
6124 scan->direct_scan[0].id = WLAN_EID_SSID;
6125 scan->direct_scan[0].len = priv->direct_ssid_len;
6126 memcpy(scan->direct_scan[0].ssid,
6127 priv->direct_ssid, priv->direct_ssid_len);
6128 direct_mask = 1;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006129 } else if (!iwl4965_is_associated(priv) && priv->essid_len) {
Zhu Yib481de92007-09-25 17:54:57 -07006130 scan->direct_scan[0].id = WLAN_EID_SSID;
6131 scan->direct_scan[0].len = priv->essid_len;
6132 memcpy(scan->direct_scan[0].ssid, priv->essid, priv->essid_len);
6133 direct_mask = 1;
Tomas Winkler857485c2008-03-21 13:53:44 -07006134 } else {
Zhu Yib481de92007-09-25 17:54:57 -07006135 direct_mask = 0;
Tomas Winkler857485c2008-03-21 13:53:44 -07006136 }
Zhu Yib481de92007-09-25 17:54:57 -07006137
Zhu Yib481de92007-09-25 17:54:57 -07006138 scan->tx_cmd.tx_flags = TX_CMD_FLG_SEQ_CTL_MSK;
6139 scan->tx_cmd.sta_id = priv->hw_setting.bcast_sta_id;
6140 scan->tx_cmd.stop_time.life_time = TX_CMD_LIFE_TIME_INFINITE;
6141
Zhu Yib481de92007-09-25 17:54:57 -07006142
6143 switch (priv->scan_bands) {
6144 case 2:
6145 scan->flags = RXON_FLG_BAND_24G_MSK | RXON_FLG_AUTO_DETECT_MSK;
6146 scan->tx_cmd.rate_n_flags =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006147 iwl4965_hw_set_rate_n_flags(IWL_RATE_1M_PLCP,
Zhu Yib481de92007-09-25 17:54:57 -07006148 RATE_MCS_ANT_B_MSK|RATE_MCS_CCK_MSK);
6149
6150 scan->good_CRC_th = 0;
Johannes Berg8318d782008-01-24 19:38:38 +01006151 band = IEEE80211_BAND_2GHZ;
Zhu Yib481de92007-09-25 17:54:57 -07006152 break;
6153
6154 case 1:
6155 scan->tx_cmd.rate_n_flags =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006156 iwl4965_hw_set_rate_n_flags(IWL_RATE_6M_PLCP,
Zhu Yib481de92007-09-25 17:54:57 -07006157 RATE_MCS_ANT_B_MSK);
6158 scan->good_CRC_th = IWL_GOOD_CRC_TH;
Johannes Berg8318d782008-01-24 19:38:38 +01006159 band = IEEE80211_BAND_5GHZ;
Zhu Yib481de92007-09-25 17:54:57 -07006160 break;
6161
6162 default:
6163 IWL_WARNING("Invalid scan band count\n");
6164 goto done;
6165 }
6166
Tomas Winkler78330fd2008-02-06 02:37:18 +02006167 /* We don't build a direct scan probe request; the uCode will do
6168 * that based on the direct_mask added to each channel entry */
6169 cmd_len = iwl4965_fill_probe_req(priv, band,
6170 (struct ieee80211_mgmt *)scan->data,
6171 IWL_MAX_SCAN_SIZE - sizeof(*scan), 0);
6172
6173 scan->tx_cmd.len = cpu_to_le16(cmd_len);
Zhu Yib481de92007-09-25 17:54:57 -07006174 /* select Rx chains */
6175
6176 /* Force use of chains B and C (0x6) for scan Rx.
6177 * Avoid A (0x1) because of its off-channel reception on A-band.
6178 * MIMO is not used here, but value is required to make uCode happy. */
6179 scan->rx_chain = RXON_RX_CHAIN_DRIVER_FORCE_MSK |
6180 cpu_to_le16((0x7 << RXON_RX_CHAIN_VALID_POS) |
6181 (0x6 << RXON_RX_CHAIN_FORCE_SEL_POS) |
6182 (0x7 << RXON_RX_CHAIN_FORCE_MIMO_SEL_POS));
6183
6184 if (priv->iw_mode == IEEE80211_IF_TYPE_MNTR)
6185 scan->filter_flags = RXON_FILTER_PROMISC_MSK;
6186
Reinette Chatre26c0f032008-03-11 16:17:15 -07006187 if (direct_mask) {
Zhu Yib481de92007-09-25 17:54:57 -07006188 IWL_DEBUG_SCAN
6189 ("Initiating direct scan for %s.\n",
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006190 iwl4965_escape_essid(priv->essid, priv->essid_len));
Reinette Chatre26c0f032008-03-11 16:17:15 -07006191 scan->channel_count =
6192 iwl4965_get_channels_for_scan(
6193 priv, band, 1, /* active */
6194 direct_mask,
6195 (void *)&scan->data[le16_to_cpu(scan->tx_cmd.len)]);
6196 } else {
Zhu Yib481de92007-09-25 17:54:57 -07006197 IWL_DEBUG_SCAN("Initiating indirect scan.\n");
Reinette Chatre26c0f032008-03-11 16:17:15 -07006198 scan->channel_count =
6199 iwl4965_get_channels_for_scan(
6200 priv, band, 0, /* passive */
6201 direct_mask,
6202 (void *)&scan->data[le16_to_cpu(scan->tx_cmd.len)]);
6203 }
Zhu Yib481de92007-09-25 17:54:57 -07006204
6205 cmd.len += le16_to_cpu(scan->tx_cmd.len) +
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006206 scan->channel_count * sizeof(struct iwl4965_scan_channel);
Zhu Yib481de92007-09-25 17:54:57 -07006207 cmd.data = scan;
6208 scan->len = cpu_to_le16(cmd.len);
6209
6210 set_bit(STATUS_SCAN_HW, &priv->status);
Tomas Winkler857485c2008-03-21 13:53:44 -07006211 ret = iwl_send_cmd_sync(priv, &cmd);
6212 if (ret)
Zhu Yib481de92007-09-25 17:54:57 -07006213 goto done;
6214
6215 queue_delayed_work(priv->workqueue, &priv->scan_check,
6216 IWL_SCAN_CHECK_WATCHDOG);
6217
6218 mutex_unlock(&priv->mutex);
6219 return;
6220
6221 done:
Ian Schram01ebd062007-10-25 17:15:22 +08006222 /* inform mac80211 scan aborted */
Zhu Yib481de92007-09-25 17:54:57 -07006223 queue_work(priv->workqueue, &priv->scan_completed);
6224 mutex_unlock(&priv->mutex);
6225}
6226
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006227static void iwl4965_bg_up(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07006228{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006229 struct iwl_priv *priv = container_of(data, struct iwl_priv, up);
Zhu Yib481de92007-09-25 17:54:57 -07006230
6231 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
6232 return;
6233
6234 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006235 __iwl4965_up(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006236 mutex_unlock(&priv->mutex);
6237}
6238
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006239static void iwl4965_bg_restart(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07006240{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006241 struct iwl_priv *priv = container_of(data, struct iwl_priv, restart);
Zhu Yib481de92007-09-25 17:54:57 -07006242
6243 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
6244 return;
6245
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006246 iwl4965_down(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006247 queue_work(priv->workqueue, &priv->up);
6248}
6249
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006250static void iwl4965_bg_rx_replenish(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07006251{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006252 struct iwl_priv *priv =
6253 container_of(data, struct iwl_priv, rx_replenish);
Zhu Yib481de92007-09-25 17:54:57 -07006254
6255 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
6256 return;
6257
6258 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006259 iwl4965_rx_replenish(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006260 mutex_unlock(&priv->mutex);
6261}
6262
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08006263#define IWL_DELAY_NEXT_SCAN (HZ*2)
6264
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006265static void iwl4965_bg_post_associate(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07006266{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006267 struct iwl_priv *priv = container_of(data, struct iwl_priv,
Zhu Yib481de92007-09-25 17:54:57 -07006268 post_associate.work);
Zhu Yib481de92007-09-25 17:54:57 -07006269 struct ieee80211_conf *conf = NULL;
Tomas Winkler857485c2008-03-21 13:53:44 -07006270 int ret = 0;
Joe Perches0795af52007-10-03 17:59:30 -07006271 DECLARE_MAC_BUF(mac);
Zhu Yib481de92007-09-25 17:54:57 -07006272
6273 if (priv->iw_mode == IEEE80211_IF_TYPE_AP) {
6274 IWL_ERROR("%s Should not be called in AP mode\n", __FUNCTION__);
6275 return;
6276 }
6277
Joe Perches0795af52007-10-03 17:59:30 -07006278 IWL_DEBUG_ASSOC("Associated as %d to: %s\n",
6279 priv->assoc_id,
6280 print_mac(mac, priv->active_rxon.bssid_addr));
Zhu Yib481de92007-09-25 17:54:57 -07006281
6282
6283 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
6284 return;
6285
6286 mutex_lock(&priv->mutex);
6287
Johannes Berg32bfd352007-12-19 01:31:26 +01006288 if (!priv->vif || !priv->is_open) {
Mohamed Abbas948c1712007-10-25 17:15:45 +08006289 mutex_unlock(&priv->mutex);
6290 return;
6291 }
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006292 iwl4965_scan_cancel_timeout(priv, 200);
mabbas052c4b92007-10-25 17:15:43 +08006293
Zhu Yib481de92007-09-25 17:54:57 -07006294 conf = ieee80211_get_hw_conf(priv->hw);
6295
6296 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006297 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006298
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006299 memset(&priv->rxon_timing, 0, sizeof(struct iwl4965_rxon_time_cmd));
6300 iwl4965_setup_rxon_timing(priv);
Tomas Winkler857485c2008-03-21 13:53:44 -07006301 ret = iwl_send_cmd_pdu(priv, REPLY_RXON_TIMING,
Zhu Yib481de92007-09-25 17:54:57 -07006302 sizeof(priv->rxon_timing), &priv->rxon_timing);
Tomas Winkler857485c2008-03-21 13:53:44 -07006303 if (ret)
Zhu Yib481de92007-09-25 17:54:57 -07006304 IWL_WARNING("REPLY_RXON_TIMING failed - "
6305 "Attempting to continue.\n");
6306
6307 priv->staging_rxon.filter_flags |= RXON_FILTER_ASSOC_MSK;
6308
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08006309#ifdef CONFIG_IWL4965_HT
Ron Rindjunskyfd105e72007-11-26 16:14:39 +02006310 if (priv->current_ht_config.is_ht)
6311 iwl4965_set_rxon_ht(priv, &priv->current_ht_config);
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08006312#endif /* CONFIG_IWL4965_HT*/
Zhu Yib481de92007-09-25 17:54:57 -07006313 iwl4965_set_rxon_chain(priv);
6314 priv->staging_rxon.assoc_id = cpu_to_le16(priv->assoc_id);
6315
6316 IWL_DEBUG_ASSOC("assoc id %d beacon interval %d\n",
6317 priv->assoc_id, priv->beacon_int);
6318
6319 if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_PREAMBLE)
6320 priv->staging_rxon.flags |= RXON_FLG_SHORT_PREAMBLE_MSK;
6321 else
6322 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_PREAMBLE_MSK;
6323
6324 if (priv->staging_rxon.flags & RXON_FLG_BAND_24G_MSK) {
6325 if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_SLOT_TIME)
6326 priv->staging_rxon.flags |= RXON_FLG_SHORT_SLOT_MSK;
6327 else
6328 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
6329
6330 if (priv->iw_mode == IEEE80211_IF_TYPE_IBSS)
6331 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
6332
6333 }
6334
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006335 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006336
6337 switch (priv->iw_mode) {
6338 case IEEE80211_IF_TYPE_STA:
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006339 iwl4965_rate_scale_init(priv->hw, IWL_AP_ID);
Zhu Yib481de92007-09-25 17:54:57 -07006340 break;
6341
6342 case IEEE80211_IF_TYPE_IBSS:
6343
6344 /* clear out the station table */
Assaf Kraussbf85ea42008-03-14 10:38:49 -07006345 iwlcore_clear_stations_table(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006346
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006347 iwl4965_rxon_add_station(priv, iwl4965_broadcast_addr, 0);
6348 iwl4965_rxon_add_station(priv, priv->bssid, 0);
6349 iwl4965_rate_scale_init(priv->hw, IWL_STA_ID);
6350 iwl4965_send_beacon_cmd(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006351
6352 break;
6353
6354 default:
6355 IWL_ERROR("%s Should not be called in %d mode\n",
6356 __FUNCTION__, priv->iw_mode);
6357 break;
6358 }
6359
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006360 iwl4965_sequence_reset(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006361
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08006362#ifdef CONFIG_IWL4965_SENSITIVITY
Zhu Yib481de92007-09-25 17:54:57 -07006363 /* Enable Rx differential gain and sensitivity calibrations */
6364 iwl4965_chain_noise_reset(priv);
6365 priv->start_calib = 1;
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08006366#endif /* CONFIG_IWL4965_SENSITIVITY */
Zhu Yib481de92007-09-25 17:54:57 -07006367
6368 if (priv->iw_mode == IEEE80211_IF_TYPE_IBSS)
6369 priv->assoc_station_added = 1;
6370
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006371 iwl4965_activate_qos(priv, 0);
Ron Rindjunsky292ae172008-02-06 11:20:39 -08006372
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08006373 /* we have just associated, don't start scan too early */
6374 priv->next_scan_jiffies = jiffies + IWL_DELAY_NEXT_SCAN;
Zhu Yib481de92007-09-25 17:54:57 -07006375 mutex_unlock(&priv->mutex);
6376}
6377
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006378static void iwl4965_bg_abort_scan(struct work_struct *work)
Zhu Yib481de92007-09-25 17:54:57 -07006379{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006380 struct iwl_priv *priv = container_of(work, struct iwl_priv, abort_scan);
Zhu Yib481de92007-09-25 17:54:57 -07006381
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006382 if (!iwl4965_is_ready(priv))
Zhu Yib481de92007-09-25 17:54:57 -07006383 return;
6384
6385 mutex_lock(&priv->mutex);
6386
6387 set_bit(STATUS_SCAN_ABORTING, &priv->status);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006388 iwl4965_send_scan_abort(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006389
6390 mutex_unlock(&priv->mutex);
6391}
6392
Zhu Yi76bb77e2007-11-22 10:53:22 +08006393static int iwl4965_mac_config(struct ieee80211_hw *hw, struct ieee80211_conf *conf);
6394
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006395static void iwl4965_bg_scan_completed(struct work_struct *work)
Zhu Yib481de92007-09-25 17:54:57 -07006396{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006397 struct iwl_priv *priv =
6398 container_of(work, struct iwl_priv, scan_completed);
Zhu Yib481de92007-09-25 17:54:57 -07006399
6400 IWL_DEBUG(IWL_DL_INFO | IWL_DL_SCAN, "SCAN complete scan\n");
6401
6402 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
6403 return;
6404
Zhu Yia0646472007-12-20 14:10:01 +08006405 if (test_bit(STATUS_CONF_PENDING, &priv->status))
6406 iwl4965_mac_config(priv->hw, ieee80211_get_hw_conf(priv->hw));
Zhu Yi76bb77e2007-11-22 10:53:22 +08006407
Zhu Yib481de92007-09-25 17:54:57 -07006408 ieee80211_scan_completed(priv->hw);
6409
6410 /* Since setting the TXPOWER may have been deferred while
6411 * performing the scan, fire one off */
6412 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006413 iwl4965_hw_reg_send_txpower(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006414 mutex_unlock(&priv->mutex);
6415}
6416
6417/*****************************************************************************
6418 *
6419 * mac80211 entry point functions
6420 *
6421 *****************************************************************************/
6422
Zhu Yi5a669262008-01-14 17:46:18 -08006423#define UCODE_READY_TIMEOUT (2 * HZ)
6424
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006425static int iwl4965_mac_start(struct ieee80211_hw *hw)
Zhu Yib481de92007-09-25 17:54:57 -07006426{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006427 struct iwl_priv *priv = hw->priv;
Zhu Yi5a669262008-01-14 17:46:18 -08006428 int ret;
Zhu Yib481de92007-09-25 17:54:57 -07006429
6430 IWL_DEBUG_MAC80211("enter\n");
6431
Zhu Yi5a669262008-01-14 17:46:18 -08006432 if (pci_enable_device(priv->pci_dev)) {
6433 IWL_ERROR("Fail to pci_enable_device\n");
6434 return -ENODEV;
6435 }
6436 pci_restore_state(priv->pci_dev);
6437 pci_enable_msi(priv->pci_dev);
6438
6439 ret = request_irq(priv->pci_dev->irq, iwl4965_isr, IRQF_SHARED,
6440 DRV_NAME, priv);
6441 if (ret) {
6442 IWL_ERROR("Error allocating IRQ %d\n", priv->pci_dev->irq);
6443 goto out_disable_msi;
6444 }
6445
Zhu Yib481de92007-09-25 17:54:57 -07006446 /* we should be verifying the device is ready to be opened */
6447 mutex_lock(&priv->mutex);
6448
Zhu Yi5a669262008-01-14 17:46:18 -08006449 memset(&priv->staging_rxon, 0, sizeof(struct iwl4965_rxon_cmd));
6450 /* fetch ucode file from disk, alloc and copy to bus-master buffers ...
6451 * ucode filename and max sizes are card-specific. */
6452
6453 if (!priv->ucode_code.len) {
6454 ret = iwl4965_read_ucode(priv);
6455 if (ret) {
6456 IWL_ERROR("Could not read microcode: %d\n", ret);
6457 mutex_unlock(&priv->mutex);
6458 goto out_release_irq;
6459 }
6460 }
6461
Zhu Yie655b9f2008-01-24 02:19:38 -08006462 ret = __iwl4965_up(priv);
Zhu Yi5a669262008-01-14 17:46:18 -08006463
Zhu Yib481de92007-09-25 17:54:57 -07006464 mutex_unlock(&priv->mutex);
Zhu Yi5a669262008-01-14 17:46:18 -08006465
Zhu Yie655b9f2008-01-24 02:19:38 -08006466 if (ret)
6467 goto out_release_irq;
6468
6469 IWL_DEBUG_INFO("Start UP work done.\n");
6470
6471 if (test_bit(STATUS_IN_SUSPEND, &priv->status))
6472 return 0;
6473
Zhu Yi5a669262008-01-14 17:46:18 -08006474 /* Wait for START_ALIVE from ucode. Otherwise callbacks from
6475 * mac80211 will not be run successfully. */
6476 ret = wait_event_interruptible_timeout(priv->wait_command_queue,
6477 test_bit(STATUS_READY, &priv->status),
6478 UCODE_READY_TIMEOUT);
6479 if (!ret) {
6480 if (!test_bit(STATUS_READY, &priv->status)) {
6481 IWL_ERROR("Wait for START_ALIVE timeout after %dms.\n",
6482 jiffies_to_msecs(UCODE_READY_TIMEOUT));
6483 ret = -ETIMEDOUT;
6484 goto out_release_irq;
6485 }
6486 }
6487
Zhu Yie655b9f2008-01-24 02:19:38 -08006488 priv->is_open = 1;
Zhu Yib481de92007-09-25 17:54:57 -07006489 IWL_DEBUG_MAC80211("leave\n");
6490 return 0;
Zhu Yi5a669262008-01-14 17:46:18 -08006491
6492out_release_irq:
6493 free_irq(priv->pci_dev->irq, priv);
6494out_disable_msi:
6495 pci_disable_msi(priv->pci_dev);
Zhu Yie655b9f2008-01-24 02:19:38 -08006496 pci_disable_device(priv->pci_dev);
6497 priv->is_open = 0;
6498 IWL_DEBUG_MAC80211("leave - failed\n");
Zhu Yi5a669262008-01-14 17:46:18 -08006499 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07006500}
6501
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006502static void iwl4965_mac_stop(struct ieee80211_hw *hw)
Zhu Yib481de92007-09-25 17:54:57 -07006503{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006504 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07006505
6506 IWL_DEBUG_MAC80211("enter\n");
Mohamed Abbas948c1712007-10-25 17:15:45 +08006507
Zhu Yie655b9f2008-01-24 02:19:38 -08006508 if (!priv->is_open) {
6509 IWL_DEBUG_MAC80211("leave - skip\n");
6510 return;
6511 }
6512
Zhu Yib481de92007-09-25 17:54:57 -07006513 priv->is_open = 0;
Zhu Yi5a669262008-01-14 17:46:18 -08006514
6515 if (iwl4965_is_ready_rf(priv)) {
Zhu Yie655b9f2008-01-24 02:19:38 -08006516 /* stop mac, cancel any scan request and clear
6517 * RXON_FILTER_ASSOC_MSK BIT
6518 */
Zhu Yi5a669262008-01-14 17:46:18 -08006519 mutex_lock(&priv->mutex);
6520 iwl4965_scan_cancel_timeout(priv, 100);
6521 cancel_delayed_work(&priv->post_associate);
Mohamed Abbasfde35712007-11-29 11:10:15 +08006522 mutex_unlock(&priv->mutex);
Mohamed Abbasfde35712007-11-29 11:10:15 +08006523 }
6524
Zhu Yi5a669262008-01-14 17:46:18 -08006525 iwl4965_down(priv);
6526
6527 flush_workqueue(priv->workqueue);
6528 free_irq(priv->pci_dev->irq, priv);
6529 pci_disable_msi(priv->pci_dev);
6530 pci_save_state(priv->pci_dev);
6531 pci_disable_device(priv->pci_dev);
Mohamed Abbas948c1712007-10-25 17:15:45 +08006532
Zhu Yib481de92007-09-25 17:54:57 -07006533 IWL_DEBUG_MAC80211("leave\n");
Zhu Yib481de92007-09-25 17:54:57 -07006534}
6535
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006536static int iwl4965_mac_tx(struct ieee80211_hw *hw, struct sk_buff *skb,
Zhu Yib481de92007-09-25 17:54:57 -07006537 struct ieee80211_tx_control *ctl)
6538{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006539 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07006540
6541 IWL_DEBUG_MAC80211("enter\n");
6542
6543 if (priv->iw_mode == IEEE80211_IF_TYPE_MNTR) {
6544 IWL_DEBUG_MAC80211("leave - monitor\n");
6545 return -1;
6546 }
6547
6548 IWL_DEBUG_TX("dev->xmit(%d bytes) at rate 0x%02x\n", skb->len,
Johannes Berg8318d782008-01-24 19:38:38 +01006549 ctl->tx_rate->bitrate);
Zhu Yib481de92007-09-25 17:54:57 -07006550
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006551 if (iwl4965_tx_skb(priv, skb, ctl))
Zhu Yib481de92007-09-25 17:54:57 -07006552 dev_kfree_skb_any(skb);
6553
6554 IWL_DEBUG_MAC80211("leave\n");
6555 return 0;
6556}
6557
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006558static int iwl4965_mac_add_interface(struct ieee80211_hw *hw,
Zhu Yib481de92007-09-25 17:54:57 -07006559 struct ieee80211_if_init_conf *conf)
6560{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006561 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07006562 unsigned long flags;
Joe Perches0795af52007-10-03 17:59:30 -07006563 DECLARE_MAC_BUF(mac);
Zhu Yib481de92007-09-25 17:54:57 -07006564
Johannes Berg32bfd352007-12-19 01:31:26 +01006565 IWL_DEBUG_MAC80211("enter: type %d\n", conf->type);
Zhu Yib481de92007-09-25 17:54:57 -07006566
Johannes Berg32bfd352007-12-19 01:31:26 +01006567 if (priv->vif) {
6568 IWL_DEBUG_MAC80211("leave - vif != NULL\n");
Reinette Chatre75849d22008-01-23 10:15:17 -08006569 return -EOPNOTSUPP;
Zhu Yib481de92007-09-25 17:54:57 -07006570 }
6571
6572 spin_lock_irqsave(&priv->lock, flags);
Johannes Berg32bfd352007-12-19 01:31:26 +01006573 priv->vif = conf->vif;
Zhu Yib481de92007-09-25 17:54:57 -07006574
6575 spin_unlock_irqrestore(&priv->lock, flags);
6576
6577 mutex_lock(&priv->mutex);
Tomas Winkler864792e2007-11-27 21:00:52 +02006578
6579 if (conf->mac_addr) {
6580 IWL_DEBUG_MAC80211("Set %s\n", print_mac(mac, conf->mac_addr));
6581 memcpy(priv->mac_addr, conf->mac_addr, ETH_ALEN);
6582 }
Zhu Yib481de92007-09-25 17:54:57 -07006583
Zhu Yi5a669262008-01-14 17:46:18 -08006584 if (iwl4965_is_ready(priv))
6585 iwl4965_set_mode(priv, conf->type);
6586
Zhu Yib481de92007-09-25 17:54:57 -07006587 mutex_unlock(&priv->mutex);
6588
Zhu Yi5a669262008-01-14 17:46:18 -08006589 IWL_DEBUG_MAC80211("leave\n");
Zhu Yib481de92007-09-25 17:54:57 -07006590 return 0;
6591}
6592
6593/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006594 * iwl4965_mac_config - mac80211 config callback
Zhu Yib481de92007-09-25 17:54:57 -07006595 *
6596 * We ignore conf->flags & IEEE80211_CONF_SHORT_SLOT_TIME since it seems to
6597 * be set inappropriately and the driver currently sets the hardware up to
6598 * use it whenever needed.
6599 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006600static int iwl4965_mac_config(struct ieee80211_hw *hw, struct ieee80211_conf *conf)
Zhu Yib481de92007-09-25 17:54:57 -07006601{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006602 struct iwl_priv *priv = hw->priv;
Assaf Kraussbf85ea42008-03-14 10:38:49 -07006603 const struct iwl_channel_info *ch_info;
Zhu Yib481de92007-09-25 17:54:57 -07006604 unsigned long flags;
Zhu Yi76bb77e2007-11-22 10:53:22 +08006605 int ret = 0;
Zhu Yib481de92007-09-25 17:54:57 -07006606
6607 mutex_lock(&priv->mutex);
Johannes Berg8318d782008-01-24 19:38:38 +01006608 IWL_DEBUG_MAC80211("enter to channel %d\n", conf->channel->hw_value);
Zhu Yib481de92007-09-25 17:54:57 -07006609
Zhu Yi12342c42007-12-20 11:27:32 +08006610 priv->add_radiotap = !!(conf->flags & IEEE80211_CONF_RADIOTAP);
6611
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006612 if (!iwl4965_is_ready(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07006613 IWL_DEBUG_MAC80211("leave - not ready\n");
Zhu Yi76bb77e2007-11-22 10:53:22 +08006614 ret = -EIO;
6615 goto out;
Zhu Yib481de92007-09-25 17:54:57 -07006616 }
6617
Assaf Krauss1ea87392008-03-18 14:57:50 -07006618 if (unlikely(!priv->cfg->mod_params->disable_hw_scan &&
Zhu Yib481de92007-09-25 17:54:57 -07006619 test_bit(STATUS_SCANNING, &priv->status))) {
Zhu Yia0646472007-12-20 14:10:01 +08006620 IWL_DEBUG_MAC80211("leave - scanning\n");
6621 set_bit(STATUS_CONF_PENDING, &priv->status);
Zhu Yib481de92007-09-25 17:54:57 -07006622 mutex_unlock(&priv->mutex);
Zhu Yia0646472007-12-20 14:10:01 +08006623 return 0;
Zhu Yib481de92007-09-25 17:54:57 -07006624 }
6625
6626 spin_lock_irqsave(&priv->lock, flags);
6627
Assaf Krauss8622e702008-03-21 13:53:43 -07006628 ch_info = iwl_get_channel_info(priv, conf->channel->band,
Johannes Berg8318d782008-01-24 19:38:38 +01006629 ieee80211_frequency_to_channel(conf->channel->center_freq));
Zhu Yib481de92007-09-25 17:54:57 -07006630 if (!is_channel_valid(ch_info)) {
Zhu Yib481de92007-09-25 17:54:57 -07006631 IWL_DEBUG_MAC80211("leave - invalid channel\n");
6632 spin_unlock_irqrestore(&priv->lock, flags);
Zhu Yi76bb77e2007-11-22 10:53:22 +08006633 ret = -EINVAL;
6634 goto out;
Zhu Yib481de92007-09-25 17:54:57 -07006635 }
6636
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08006637#ifdef CONFIG_IWL4965_HT
Tomas Winkler78330fd2008-02-06 02:37:18 +02006638 /* if we are switching from ht to 2.4 clear flags
Zhu Yib481de92007-09-25 17:54:57 -07006639 * from any ht related info since 2.4 does not
6640 * support ht */
Tomas Winkler78330fd2008-02-06 02:37:18 +02006641 if ((le16_to_cpu(priv->staging_rxon.channel) != conf->channel->hw_value)
Zhu Yib481de92007-09-25 17:54:57 -07006642#ifdef IEEE80211_CONF_CHANNEL_SWITCH
6643 && !(conf->flags & IEEE80211_CONF_CHANNEL_SWITCH)
6644#endif
6645 )
6646 priv->staging_rxon.flags = 0;
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08006647#endif /* CONFIG_IWL4965_HT */
Zhu Yib481de92007-09-25 17:54:57 -07006648
Assaf Kraussbf85ea42008-03-14 10:38:49 -07006649 iwlcore_set_rxon_channel(priv, conf->channel->band,
Johannes Berg8318d782008-01-24 19:38:38 +01006650 ieee80211_frequency_to_channel(conf->channel->center_freq));
Zhu Yib481de92007-09-25 17:54:57 -07006651
Johannes Berg8318d782008-01-24 19:38:38 +01006652 iwl4965_set_flags_for_phymode(priv, conf->channel->band);
Zhu Yib481de92007-09-25 17:54:57 -07006653
6654 /* The list of supported rates and rate mask can be different
Johannes Berg8318d782008-01-24 19:38:38 +01006655 * for each band; since the band may have changed, reset
Zhu Yib481de92007-09-25 17:54:57 -07006656 * the rate mask to what mac80211 lists */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006657 iwl4965_set_rate(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006658
6659 spin_unlock_irqrestore(&priv->lock, flags);
6660
6661#ifdef IEEE80211_CONF_CHANNEL_SWITCH
6662 if (conf->flags & IEEE80211_CONF_CHANNEL_SWITCH) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006663 iwl4965_hw_channel_switch(priv, conf->channel);
Zhu Yi76bb77e2007-11-22 10:53:22 +08006664 goto out;
Zhu Yib481de92007-09-25 17:54:57 -07006665 }
6666#endif
6667
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006668 iwl4965_radio_kill_sw(priv, !conf->radio_enabled);
Zhu Yib481de92007-09-25 17:54:57 -07006669
6670 if (!conf->radio_enabled) {
6671 IWL_DEBUG_MAC80211("leave - radio disabled\n");
Zhu Yi76bb77e2007-11-22 10:53:22 +08006672 goto out;
Zhu Yib481de92007-09-25 17:54:57 -07006673 }
6674
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006675 if (iwl4965_is_rfkill(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07006676 IWL_DEBUG_MAC80211("leave - RF kill\n");
Zhu Yi76bb77e2007-11-22 10:53:22 +08006677 ret = -EIO;
6678 goto out;
Zhu Yib481de92007-09-25 17:54:57 -07006679 }
6680
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006681 iwl4965_set_rate(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006682
6683 if (memcmp(&priv->active_rxon,
6684 &priv->staging_rxon, sizeof(priv->staging_rxon)))
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006685 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006686 else
6687 IWL_DEBUG_INFO("No re-sending same RXON configuration.\n");
6688
6689 IWL_DEBUG_MAC80211("leave\n");
6690
Zhu Yia0646472007-12-20 14:10:01 +08006691out:
6692 clear_bit(STATUS_CONF_PENDING, &priv->status);
Zhu Yi5a669262008-01-14 17:46:18 -08006693 mutex_unlock(&priv->mutex);
Zhu Yi76bb77e2007-11-22 10:53:22 +08006694 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07006695}
6696
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006697static void iwl4965_config_ap(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07006698{
Tomas Winkler857485c2008-03-21 13:53:44 -07006699 int ret = 0;
Zhu Yib481de92007-09-25 17:54:57 -07006700
Maarten Lankhorstd986bcd2008-01-23 10:15:16 -08006701 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
Zhu Yib481de92007-09-25 17:54:57 -07006702 return;
6703
6704 /* The following should be done only at AP bring up */
6705 if ((priv->active_rxon.filter_flags & RXON_FILTER_ASSOC_MSK) == 0) {
6706
6707 /* RXON - unassoc (to set timing command) */
6708 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006709 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006710
6711 /* RXON Timing */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006712 memset(&priv->rxon_timing, 0, sizeof(struct iwl4965_rxon_time_cmd));
6713 iwl4965_setup_rxon_timing(priv);
Tomas Winkler857485c2008-03-21 13:53:44 -07006714 ret = iwl_send_cmd_pdu(priv, REPLY_RXON_TIMING,
Zhu Yib481de92007-09-25 17:54:57 -07006715 sizeof(priv->rxon_timing), &priv->rxon_timing);
Tomas Winkler857485c2008-03-21 13:53:44 -07006716 if (ret)
Zhu Yib481de92007-09-25 17:54:57 -07006717 IWL_WARNING("REPLY_RXON_TIMING failed - "
6718 "Attempting to continue.\n");
6719
6720 iwl4965_set_rxon_chain(priv);
6721
6722 /* FIXME: what should be the assoc_id for AP? */
6723 priv->staging_rxon.assoc_id = cpu_to_le16(priv->assoc_id);
6724 if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_PREAMBLE)
6725 priv->staging_rxon.flags |=
6726 RXON_FLG_SHORT_PREAMBLE_MSK;
6727 else
6728 priv->staging_rxon.flags &=
6729 ~RXON_FLG_SHORT_PREAMBLE_MSK;
6730
6731 if (priv->staging_rxon.flags & RXON_FLG_BAND_24G_MSK) {
6732 if (priv->assoc_capability &
6733 WLAN_CAPABILITY_SHORT_SLOT_TIME)
6734 priv->staging_rxon.flags |=
6735 RXON_FLG_SHORT_SLOT_MSK;
6736 else
6737 priv->staging_rxon.flags &=
6738 ~RXON_FLG_SHORT_SLOT_MSK;
6739
6740 if (priv->iw_mode == IEEE80211_IF_TYPE_IBSS)
6741 priv->staging_rxon.flags &=
6742 ~RXON_FLG_SHORT_SLOT_MSK;
6743 }
6744 /* restore RXON assoc */
6745 priv->staging_rxon.filter_flags |= RXON_FILTER_ASSOC_MSK;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006746 iwl4965_commit_rxon(priv);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006747 iwl4965_activate_qos(priv, 1);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006748 iwl4965_rxon_add_station(priv, iwl4965_broadcast_addr, 0);
Zhu Yie1493de2007-09-27 11:27:32 +08006749 }
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006750 iwl4965_send_beacon_cmd(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006751
6752 /* FIXME - we need to add code here to detect a totally new
6753 * configuration, reset the AP, unassoc, rxon timing, assoc,
6754 * clear sta table, add BCAST sta... */
6755}
6756
Johannes Berg32bfd352007-12-19 01:31:26 +01006757static int iwl4965_mac_config_interface(struct ieee80211_hw *hw,
6758 struct ieee80211_vif *vif,
Zhu Yib481de92007-09-25 17:54:57 -07006759 struct ieee80211_if_conf *conf)
6760{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006761 struct iwl_priv *priv = hw->priv;
Joe Perches0795af52007-10-03 17:59:30 -07006762 DECLARE_MAC_BUF(mac);
Zhu Yib481de92007-09-25 17:54:57 -07006763 unsigned long flags;
6764 int rc;
6765
6766 if (conf == NULL)
6767 return -EIO;
6768
Emmanuel Grumbachb716bb92008-03-04 18:09:32 -08006769 if (priv->vif != vif) {
6770 IWL_DEBUG_MAC80211("leave - priv->vif != vif\n");
6771 mutex_unlock(&priv->mutex);
6772 return 0;
6773 }
6774
Zhu Yib481de92007-09-25 17:54:57 -07006775 if ((priv->iw_mode == IEEE80211_IF_TYPE_AP) &&
6776 (!conf->beacon || !conf->ssid_len)) {
6777 IWL_DEBUG_MAC80211
6778 ("Leaving in AP mode because HostAPD is not ready.\n");
6779 return 0;
6780 }
6781
Zhu Yi5a669262008-01-14 17:46:18 -08006782 if (!iwl4965_is_alive(priv))
6783 return -EAGAIN;
6784
Zhu Yib481de92007-09-25 17:54:57 -07006785 mutex_lock(&priv->mutex);
6786
Zhu Yib481de92007-09-25 17:54:57 -07006787 if (conf->bssid)
Joe Perches0795af52007-10-03 17:59:30 -07006788 IWL_DEBUG_MAC80211("bssid: %s\n",
6789 print_mac(mac, conf->bssid));
Zhu Yib481de92007-09-25 17:54:57 -07006790
Johannes Berg4150c572007-09-17 01:29:23 -04006791/*
6792 * very dubious code was here; the probe filtering flag is never set:
6793 *
Zhu Yib481de92007-09-25 17:54:57 -07006794 if (unlikely(test_bit(STATUS_SCANNING, &priv->status)) &&
6795 !(priv->hw->flags & IEEE80211_HW_NO_PROBE_FILTERING)) {
Johannes Berg4150c572007-09-17 01:29:23 -04006796 */
Zhu Yib481de92007-09-25 17:54:57 -07006797
6798 if (priv->iw_mode == IEEE80211_IF_TYPE_AP) {
6799 if (!conf->bssid) {
6800 conf->bssid = priv->mac_addr;
6801 memcpy(priv->bssid, priv->mac_addr, ETH_ALEN);
Joe Perches0795af52007-10-03 17:59:30 -07006802 IWL_DEBUG_MAC80211("bssid was set to: %s\n",
6803 print_mac(mac, conf->bssid));
Zhu Yib481de92007-09-25 17:54:57 -07006804 }
6805 if (priv->ibss_beacon)
6806 dev_kfree_skb(priv->ibss_beacon);
6807
6808 priv->ibss_beacon = conf->beacon;
6809 }
6810
Mohamed Abbasfde35712007-11-29 11:10:15 +08006811 if (iwl4965_is_rfkill(priv))
6812 goto done;
6813
Zhu Yib481de92007-09-25 17:54:57 -07006814 if (conf->bssid && !is_zero_ether_addr(conf->bssid) &&
6815 !is_multicast_ether_addr(conf->bssid)) {
6816 /* If there is currently a HW scan going on in the background
6817 * then we need to cancel it else the RXON below will fail. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006818 if (iwl4965_scan_cancel_timeout(priv, 100)) {
Zhu Yib481de92007-09-25 17:54:57 -07006819 IWL_WARNING("Aborted scan still in progress "
6820 "after 100ms\n");
6821 IWL_DEBUG_MAC80211("leaving - scan abort failed.\n");
6822 mutex_unlock(&priv->mutex);
6823 return -EAGAIN;
6824 }
6825 memcpy(priv->staging_rxon.bssid_addr, conf->bssid, ETH_ALEN);
6826
6827 /* TODO: Audit driver for usage of these members and see
6828 * if mac80211 deprecates them (priv->bssid looks like it
6829 * shouldn't be there, but I haven't scanned the IBSS code
6830 * to verify) - jpk */
6831 memcpy(priv->bssid, conf->bssid, ETH_ALEN);
6832
6833 if (priv->iw_mode == IEEE80211_IF_TYPE_AP)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006834 iwl4965_config_ap(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006835 else {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006836 rc = iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006837 if ((priv->iw_mode == IEEE80211_IF_TYPE_STA) && rc)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006838 iwl4965_rxon_add_station(
Zhu Yib481de92007-09-25 17:54:57 -07006839 priv, priv->active_rxon.bssid_addr, 1);
6840 }
6841
6842 } else {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006843 iwl4965_scan_cancel_timeout(priv, 100);
Zhu Yib481de92007-09-25 17:54:57 -07006844 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006845 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006846 }
6847
Mohamed Abbasfde35712007-11-29 11:10:15 +08006848 done:
Zhu Yib481de92007-09-25 17:54:57 -07006849 spin_lock_irqsave(&priv->lock, flags);
6850 if (!conf->ssid_len)
6851 memset(priv->essid, 0, IW_ESSID_MAX_SIZE);
6852 else
6853 memcpy(priv->essid, conf->ssid, conf->ssid_len);
6854
6855 priv->essid_len = conf->ssid_len;
6856 spin_unlock_irqrestore(&priv->lock, flags);
6857
6858 IWL_DEBUG_MAC80211("leave\n");
6859 mutex_unlock(&priv->mutex);
6860
6861 return 0;
6862}
6863
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006864static void iwl4965_configure_filter(struct ieee80211_hw *hw,
Johannes Berg4150c572007-09-17 01:29:23 -04006865 unsigned int changed_flags,
6866 unsigned int *total_flags,
6867 int mc_count, struct dev_addr_list *mc_list)
6868{
6869 /*
6870 * XXX: dummy
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006871 * see also iwl4965_connection_init_rx_config
Johannes Berg4150c572007-09-17 01:29:23 -04006872 */
6873 *total_flags = 0;
6874}
6875
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006876static void iwl4965_mac_remove_interface(struct ieee80211_hw *hw,
Zhu Yib481de92007-09-25 17:54:57 -07006877 struct ieee80211_if_init_conf *conf)
6878{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006879 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07006880
6881 IWL_DEBUG_MAC80211("enter\n");
6882
6883 mutex_lock(&priv->mutex);
Mohamed Abbas948c1712007-10-25 17:15:45 +08006884
Mohamed Abbasfde35712007-11-29 11:10:15 +08006885 if (iwl4965_is_ready_rf(priv)) {
6886 iwl4965_scan_cancel_timeout(priv, 100);
6887 cancel_delayed_work(&priv->post_associate);
6888 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
6889 iwl4965_commit_rxon(priv);
6890 }
Johannes Berg32bfd352007-12-19 01:31:26 +01006891 if (priv->vif == conf->vif) {
6892 priv->vif = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07006893 memset(priv->bssid, 0, ETH_ALEN);
6894 memset(priv->essid, 0, IW_ESSID_MAX_SIZE);
6895 priv->essid_len = 0;
6896 }
6897 mutex_unlock(&priv->mutex);
6898
6899 IWL_DEBUG_MAC80211("leave\n");
6900
6901}
Johannes Berg471b3ef2007-12-28 14:32:58 +01006902
6903static void iwl4965_bss_info_changed(struct ieee80211_hw *hw,
6904 struct ieee80211_vif *vif,
6905 struct ieee80211_bss_conf *bss_conf,
6906 u32 changes)
Tomas Winkler220173b2007-10-16 00:50:25 +02006907{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006908 struct iwl_priv *priv = hw->priv;
Tomas Winkler220173b2007-10-16 00:50:25 +02006909
Johannes Berg471b3ef2007-12-28 14:32:58 +01006910 if (changes & BSS_CHANGED_ERP_PREAMBLE) {
6911 if (bss_conf->use_short_preamble)
Tomas Winkler220173b2007-10-16 00:50:25 +02006912 priv->staging_rxon.flags |= RXON_FLG_SHORT_PREAMBLE_MSK;
6913 else
6914 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_PREAMBLE_MSK;
6915 }
6916
Johannes Berg471b3ef2007-12-28 14:32:58 +01006917 if (changes & BSS_CHANGED_ERP_CTS_PROT) {
Johannes Berg8318d782008-01-24 19:38:38 +01006918 if (bss_conf->use_cts_prot && (priv->band != IEEE80211_BAND_5GHZ))
Tomas Winkler220173b2007-10-16 00:50:25 +02006919 priv->staging_rxon.flags |= RXON_FLG_TGG_PROTECT_MSK;
6920 else
6921 priv->staging_rxon.flags &= ~RXON_FLG_TGG_PROTECT_MSK;
6922 }
6923
Johannes Berg471b3ef2007-12-28 14:32:58 +01006924 if (changes & BSS_CHANGED_ASSOC) {
6925 /*
6926 * TODO:
6927 * do stuff instead of sniffing assoc resp
6928 */
6929 }
6930
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006931 if (iwl4965_is_associated(priv))
6932 iwl4965_send_rxon_assoc(priv);
Tomas Winkler220173b2007-10-16 00:50:25 +02006933}
Zhu Yib481de92007-09-25 17:54:57 -07006934
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006935static int iwl4965_mac_hw_scan(struct ieee80211_hw *hw, u8 *ssid, size_t len)
Zhu Yib481de92007-09-25 17:54:57 -07006936{
6937 int rc = 0;
6938 unsigned long flags;
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006939 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07006940
6941 IWL_DEBUG_MAC80211("enter\n");
6942
mabbas052c4b92007-10-25 17:15:43 +08006943 mutex_lock(&priv->mutex);
Zhu Yib481de92007-09-25 17:54:57 -07006944 spin_lock_irqsave(&priv->lock, flags);
6945
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006946 if (!iwl4965_is_ready_rf(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07006947 rc = -EIO;
6948 IWL_DEBUG_MAC80211("leave - not ready or exit pending\n");
6949 goto out_unlock;
6950 }
6951
6952 if (priv->iw_mode == IEEE80211_IF_TYPE_AP) { /* APs don't scan */
6953 rc = -EIO;
6954 IWL_ERROR("ERROR: APs don't scan\n");
6955 goto out_unlock;
6956 }
6957
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08006958 /* we don't schedule scan within next_scan_jiffies period */
6959 if (priv->next_scan_jiffies &&
6960 time_after(priv->next_scan_jiffies, jiffies)) {
6961 rc = -EAGAIN;
6962 goto out_unlock;
6963 }
Zhu Yib481de92007-09-25 17:54:57 -07006964 /* if we just finished scan ask for delay */
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08006965 if (priv->last_scan_jiffies && time_after(priv->last_scan_jiffies +
6966 IWL_DELAY_NEXT_SCAN, jiffies)) {
Zhu Yib481de92007-09-25 17:54:57 -07006967 rc = -EAGAIN;
6968 goto out_unlock;
6969 }
6970 if (len) {
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08006971 IWL_DEBUG_SCAN("direct scan for %s [%d]\n ",
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006972 iwl4965_escape_essid(ssid, len), (int)len);
Zhu Yib481de92007-09-25 17:54:57 -07006973
6974 priv->one_direct_scan = 1;
6975 priv->direct_ssid_len = (u8)
6976 min((u8) len, (u8) IW_ESSID_MAX_SIZE);
6977 memcpy(priv->direct_ssid, ssid, priv->direct_ssid_len);
Mohamed Abbas948c1712007-10-25 17:15:45 +08006978 } else
6979 priv->one_direct_scan = 0;
Zhu Yib481de92007-09-25 17:54:57 -07006980
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006981 rc = iwl4965_scan_initiate(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006982
6983 IWL_DEBUG_MAC80211("leave\n");
6984
6985out_unlock:
6986 spin_unlock_irqrestore(&priv->lock, flags);
mabbas052c4b92007-10-25 17:15:43 +08006987 mutex_unlock(&priv->mutex);
Zhu Yib481de92007-09-25 17:54:57 -07006988
6989 return rc;
6990}
6991
Emmanuel Grumbachab885f82008-03-20 15:06:43 +02006992static void iwl4965_mac_update_tkip_key(struct ieee80211_hw *hw,
6993 struct ieee80211_key_conf *keyconf, const u8 *addr,
6994 u32 iv32, u16 *phase1key)
6995{
6996 struct iwl_priv *priv = hw->priv;
6997 u8 sta_id = IWL_INVALID_STATION;
6998 unsigned long flags;
6999 __le16 key_flags = 0;
7000 int i;
7001 DECLARE_MAC_BUF(mac);
7002
7003 IWL_DEBUG_MAC80211("enter\n");
7004
7005 sta_id = iwl4965_hw_find_station(priv, addr);
7006 if (sta_id == IWL_INVALID_STATION) {
7007 IWL_DEBUG_MAC80211("leave - %s not in station map.\n",
7008 print_mac(mac, addr));
7009 return;
7010 }
7011
7012 iwl4965_scan_cancel_timeout(priv, 100);
7013
7014 key_flags |= (STA_KEY_FLG_TKIP | STA_KEY_FLG_MAP_KEY_MSK);
7015 key_flags |= cpu_to_le16(keyconf->keyidx << STA_KEY_FLG_KEYID_POS);
7016 key_flags &= ~STA_KEY_FLG_INVALID;
7017
7018 if (sta_id == priv->hw_setting.bcast_sta_id)
7019 key_flags |= STA_KEY_MULTICAST_MSK;
7020
7021 spin_lock_irqsave(&priv->sta_lock, flags);
7022
7023 priv->stations[sta_id].sta.key.key_offset =
7024 (sta_id % STA_KEY_MAX_NUM);/* FIXME */
7025 priv->stations[sta_id].sta.key.key_flags = key_flags;
7026 priv->stations[sta_id].sta.key.tkip_rx_tsc_byte2 = (u8) iv32;
7027
7028 for (i = 0; i < 5; i++)
7029 priv->stations[sta_id].sta.key.tkip_rx_ttak[i] =
7030 cpu_to_le16(phase1key[i]);
7031
7032 priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
7033 priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
7034
7035 iwl4965_send_add_station(priv, &priv->stations[sta_id].sta, CMD_ASYNC);
7036
7037 spin_unlock_irqrestore(&priv->sta_lock, flags);
7038
7039 IWL_DEBUG_MAC80211("leave\n");
7040}
7041
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007042static int iwl4965_mac_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
Zhu Yib481de92007-09-25 17:54:57 -07007043 const u8 *local_addr, const u8 *addr,
7044 struct ieee80211_key_conf *key)
7045{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007046 struct iwl_priv *priv = hw->priv;
Joe Perches0795af52007-10-03 17:59:30 -07007047 DECLARE_MAC_BUF(mac);
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07007048 int ret = 0;
7049 u8 sta_id = IWL_INVALID_STATION;
7050 u8 static_key;
Zhu Yib481de92007-09-25 17:54:57 -07007051
7052 IWL_DEBUG_MAC80211("enter\n");
7053
Assaf Krauss1ea87392008-03-18 14:57:50 -07007054 if (!priv->cfg->mod_params->hw_crypto) {
Zhu Yib481de92007-09-25 17:54:57 -07007055 IWL_DEBUG_MAC80211("leave - hwcrypto disabled\n");
7056 return -EOPNOTSUPP;
7057 }
7058
7059 if (is_zero_ether_addr(addr))
7060 /* only support pairwise keys */
7061 return -EOPNOTSUPP;
7062
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07007063 /* FIXME: need to differenciate between static and dynamic key
7064 * in the level of mac80211 */
7065 static_key = !iwl4965_is_associated(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007066
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07007067 if (!static_key) {
7068 sta_id = iwl4965_hw_find_station(priv, addr);
7069 if (sta_id == IWL_INVALID_STATION) {
7070 IWL_DEBUG_MAC80211("leave - %s not in station map.\n",
7071 print_mac(mac, addr));
7072 return -EINVAL;
7073 }
7074 }
Zhu Yib481de92007-09-25 17:54:57 -07007075
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007076 iwl4965_scan_cancel_timeout(priv, 100);
mabbas052c4b92007-10-25 17:15:43 +08007077
Zhu Yib481de92007-09-25 17:54:57 -07007078 switch (cmd) {
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07007079 case SET_KEY:
7080 if (static_key)
7081 ret = iwl4965_set_static_key(priv, key);
7082 else
7083 ret = iwl4965_set_dynamic_key(priv, key, sta_id);
7084
7085 IWL_DEBUG_MAC80211("enable hwcrypto key\n");
Zhu Yib481de92007-09-25 17:54:57 -07007086 break;
7087 case DISABLE_KEY:
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07007088 if (static_key)
7089 ret = iwl4965_remove_static_key(priv);
7090 else
7091 ret = iwl4965_clear_sta_key_info(priv, sta_id);
7092
7093 IWL_DEBUG_MAC80211("disable hwcrypto key\n");
Zhu Yib481de92007-09-25 17:54:57 -07007094 break;
7095 default:
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07007096 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07007097 }
7098
7099 IWL_DEBUG_MAC80211("leave\n");
Zhu Yib481de92007-09-25 17:54:57 -07007100
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07007101 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07007102}
7103
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007104static int iwl4965_mac_conf_tx(struct ieee80211_hw *hw, int queue,
Zhu Yib481de92007-09-25 17:54:57 -07007105 const struct ieee80211_tx_queue_params *params)
7106{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007107 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07007108 unsigned long flags;
7109 int q;
Zhu Yib481de92007-09-25 17:54:57 -07007110
7111 IWL_DEBUG_MAC80211("enter\n");
7112
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007113 if (!iwl4965_is_ready_rf(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07007114 IWL_DEBUG_MAC80211("leave - RF not ready\n");
7115 return -EIO;
7116 }
7117
7118 if (queue >= AC_NUM) {
7119 IWL_DEBUG_MAC80211("leave - queue >= AC_NUM %d\n", queue);
7120 return 0;
7121 }
7122
Zhu Yib481de92007-09-25 17:54:57 -07007123 if (!priv->qos_data.qos_enable) {
7124 priv->qos_data.qos_active = 0;
7125 IWL_DEBUG_MAC80211("leave - qos not enabled\n");
7126 return 0;
7127 }
7128 q = AC_NUM - 1 - queue;
7129
7130 spin_lock_irqsave(&priv->lock, flags);
7131
7132 priv->qos_data.def_qos_parm.ac[q].cw_min = cpu_to_le16(params->cw_min);
7133 priv->qos_data.def_qos_parm.ac[q].cw_max = cpu_to_le16(params->cw_max);
7134 priv->qos_data.def_qos_parm.ac[q].aifsn = params->aifs;
7135 priv->qos_data.def_qos_parm.ac[q].edca_txop =
Johannes Berg3330d7be2008-02-10 16:49:38 +01007136 cpu_to_le16((params->txop * 32));
Zhu Yib481de92007-09-25 17:54:57 -07007137
7138 priv->qos_data.def_qos_parm.ac[q].reserved1 = 0;
7139 priv->qos_data.qos_active = 1;
7140
7141 spin_unlock_irqrestore(&priv->lock, flags);
7142
7143 mutex_lock(&priv->mutex);
7144 if (priv->iw_mode == IEEE80211_IF_TYPE_AP)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007145 iwl4965_activate_qos(priv, 1);
7146 else if (priv->assoc_id && iwl4965_is_associated(priv))
7147 iwl4965_activate_qos(priv, 0);
Zhu Yib481de92007-09-25 17:54:57 -07007148
7149 mutex_unlock(&priv->mutex);
7150
Zhu Yib481de92007-09-25 17:54:57 -07007151 IWL_DEBUG_MAC80211("leave\n");
7152 return 0;
7153}
7154
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007155static int iwl4965_mac_get_tx_stats(struct ieee80211_hw *hw,
Zhu Yib481de92007-09-25 17:54:57 -07007156 struct ieee80211_tx_queue_stats *stats)
7157{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007158 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07007159 int i, avail;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007160 struct iwl4965_tx_queue *txq;
7161 struct iwl4965_queue *q;
Zhu Yib481de92007-09-25 17:54:57 -07007162 unsigned long flags;
7163
7164 IWL_DEBUG_MAC80211("enter\n");
7165
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007166 if (!iwl4965_is_ready_rf(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07007167 IWL_DEBUG_MAC80211("leave - RF not ready\n");
7168 return -EIO;
7169 }
7170
7171 spin_lock_irqsave(&priv->lock, flags);
7172
7173 for (i = 0; i < AC_NUM; i++) {
7174 txq = &priv->txq[i];
7175 q = &txq->q;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007176 avail = iwl4965_queue_space(q);
Zhu Yib481de92007-09-25 17:54:57 -07007177
7178 stats->data[i].len = q->n_window - avail;
7179 stats->data[i].limit = q->n_window - q->high_mark;
7180 stats->data[i].count = q->n_window;
7181
7182 }
7183 spin_unlock_irqrestore(&priv->lock, flags);
7184
7185 IWL_DEBUG_MAC80211("leave\n");
7186
7187 return 0;
7188}
7189
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007190static int iwl4965_mac_get_stats(struct ieee80211_hw *hw,
Zhu Yib481de92007-09-25 17:54:57 -07007191 struct ieee80211_low_level_stats *stats)
7192{
7193 IWL_DEBUG_MAC80211("enter\n");
7194 IWL_DEBUG_MAC80211("leave\n");
7195
7196 return 0;
7197}
7198
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007199static u64 iwl4965_mac_get_tsf(struct ieee80211_hw *hw)
Zhu Yib481de92007-09-25 17:54:57 -07007200{
7201 IWL_DEBUG_MAC80211("enter\n");
7202 IWL_DEBUG_MAC80211("leave\n");
7203
7204 return 0;
7205}
7206
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007207static void iwl4965_mac_reset_tsf(struct ieee80211_hw *hw)
Zhu Yib481de92007-09-25 17:54:57 -07007208{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007209 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07007210 unsigned long flags;
7211
7212 mutex_lock(&priv->mutex);
7213 IWL_DEBUG_MAC80211("enter\n");
7214
7215 priv->lq_mngr.lq_ready = 0;
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08007216#ifdef CONFIG_IWL4965_HT
Zhu Yib481de92007-09-25 17:54:57 -07007217 spin_lock_irqsave(&priv->lock, flags);
Ron Rindjunskyfd105e72007-11-26 16:14:39 +02007218 memset(&priv->current_ht_config, 0, sizeof(struct iwl_ht_info));
Zhu Yib481de92007-09-25 17:54:57 -07007219 spin_unlock_irqrestore(&priv->lock, flags);
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08007220#endif /* CONFIG_IWL4965_HT */
Zhu Yib481de92007-09-25 17:54:57 -07007221
Assaf Kraussbf85ea42008-03-14 10:38:49 -07007222 iwlcore_reset_qos(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007223
7224 cancel_delayed_work(&priv->post_associate);
7225
7226 spin_lock_irqsave(&priv->lock, flags);
7227 priv->assoc_id = 0;
7228 priv->assoc_capability = 0;
7229 priv->call_post_assoc_from_beacon = 0;
7230 priv->assoc_station_added = 0;
7231
7232 /* new association get rid of ibss beacon skb */
7233 if (priv->ibss_beacon)
7234 dev_kfree_skb(priv->ibss_beacon);
7235
7236 priv->ibss_beacon = NULL;
7237
7238 priv->beacon_int = priv->hw->conf.beacon_int;
7239 priv->timestamp1 = 0;
7240 priv->timestamp0 = 0;
7241 if ((priv->iw_mode == IEEE80211_IF_TYPE_STA))
7242 priv->beacon_int = 0;
7243
7244 spin_unlock_irqrestore(&priv->lock, flags);
7245
Mohamed Abbasfde35712007-11-29 11:10:15 +08007246 if (!iwl4965_is_ready_rf(priv)) {
7247 IWL_DEBUG_MAC80211("leave - not ready\n");
7248 mutex_unlock(&priv->mutex);
7249 return;
7250 }
7251
mabbas052c4b92007-10-25 17:15:43 +08007252 /* we are restarting association process
7253 * clear RXON_FILTER_ASSOC_MSK bit
7254 */
7255 if (priv->iw_mode != IEEE80211_IF_TYPE_AP) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007256 iwl4965_scan_cancel_timeout(priv, 100);
mabbas052c4b92007-10-25 17:15:43 +08007257 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007258 iwl4965_commit_rxon(priv);
mabbas052c4b92007-10-25 17:15:43 +08007259 }
7260
Zhu Yib481de92007-09-25 17:54:57 -07007261 /* Per mac80211.h: This is only used in IBSS mode... */
7262 if (priv->iw_mode != IEEE80211_IF_TYPE_IBSS) {
mabbas052c4b92007-10-25 17:15:43 +08007263
Zhu Yib481de92007-09-25 17:54:57 -07007264 IWL_DEBUG_MAC80211("leave - not in IBSS\n");
7265 mutex_unlock(&priv->mutex);
7266 return;
7267 }
7268
Zhu Yib481de92007-09-25 17:54:57 -07007269 priv->only_active_channel = 0;
7270
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007271 iwl4965_set_rate(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007272
7273 mutex_unlock(&priv->mutex);
7274
7275 IWL_DEBUG_MAC80211("leave\n");
Zhu Yib481de92007-09-25 17:54:57 -07007276}
7277
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007278static int iwl4965_mac_beacon_update(struct ieee80211_hw *hw, struct sk_buff *skb,
Zhu Yib481de92007-09-25 17:54:57 -07007279 struct ieee80211_tx_control *control)
7280{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007281 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07007282 unsigned long flags;
7283
7284 mutex_lock(&priv->mutex);
7285 IWL_DEBUG_MAC80211("enter\n");
7286
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007287 if (!iwl4965_is_ready_rf(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07007288 IWL_DEBUG_MAC80211("leave - RF not ready\n");
7289 mutex_unlock(&priv->mutex);
7290 return -EIO;
7291 }
7292
7293 if (priv->iw_mode != IEEE80211_IF_TYPE_IBSS) {
7294 IWL_DEBUG_MAC80211("leave - not IBSS\n");
7295 mutex_unlock(&priv->mutex);
7296 return -EIO;
7297 }
7298
7299 spin_lock_irqsave(&priv->lock, flags);
7300
7301 if (priv->ibss_beacon)
7302 dev_kfree_skb(priv->ibss_beacon);
7303
7304 priv->ibss_beacon = skb;
7305
7306 priv->assoc_id = 0;
7307
7308 IWL_DEBUG_MAC80211("leave\n");
7309 spin_unlock_irqrestore(&priv->lock, flags);
7310
Assaf Kraussbf85ea42008-03-14 10:38:49 -07007311 iwlcore_reset_qos(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007312
7313 queue_work(priv->workqueue, &priv->post_associate.work);
7314
7315 mutex_unlock(&priv->mutex);
7316
7317 return 0;
7318}
7319
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08007320#ifdef CONFIG_IWL4965_HT
Zhu Yib481de92007-09-25 17:54:57 -07007321
Ron Rindjunskyfd105e72007-11-26 16:14:39 +02007322static void iwl4965_ht_info_fill(struct ieee80211_conf *conf,
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007323 struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07007324{
Ron Rindjunskyfd105e72007-11-26 16:14:39 +02007325 struct iwl_ht_info *iwl_conf = &priv->current_ht_config;
7326 struct ieee80211_ht_info *ht_conf = &conf->ht_conf;
7327 struct ieee80211_ht_bss_info *ht_bss_conf = &conf->ht_bss_conf;
Zhu Yib481de92007-09-25 17:54:57 -07007328
7329 IWL_DEBUG_MAC80211("enter: \n");
7330
Ron Rindjunskyfd105e72007-11-26 16:14:39 +02007331 if (!(conf->flags & IEEE80211_CONF_SUPPORT_HT_MODE)) {
7332 iwl_conf->is_ht = 0;
7333 return;
Zhu Yib481de92007-09-25 17:54:57 -07007334 }
7335
Ron Rindjunskyfd105e72007-11-26 16:14:39 +02007336 iwl_conf->is_ht = 1;
7337 priv->ps_mode = (u8)((ht_conf->cap & IEEE80211_HT_CAP_MIMO_PS) >> 2);
Zhu Yib481de92007-09-25 17:54:57 -07007338
Ron Rindjunskyfd105e72007-11-26 16:14:39 +02007339 if (ht_conf->cap & IEEE80211_HT_CAP_SGI_20)
7340 iwl_conf->sgf |= 0x1;
7341 if (ht_conf->cap & IEEE80211_HT_CAP_SGI_40)
7342 iwl_conf->sgf |= 0x2;
Zhu Yib481de92007-09-25 17:54:57 -07007343
Ron Rindjunskyfd105e72007-11-26 16:14:39 +02007344 iwl_conf->is_green_field = !!(ht_conf->cap & IEEE80211_HT_CAP_GRN_FLD);
7345 iwl_conf->max_amsdu_size =
7346 !!(ht_conf->cap & IEEE80211_HT_CAP_MAX_AMSDU);
Guy Cohen134eb5d2008-03-04 18:09:25 -08007347
Ron Rindjunskyfd105e72007-11-26 16:14:39 +02007348 iwl_conf->supported_chan_width =
7349 !!(ht_conf->cap & IEEE80211_HT_CAP_SUP_WIDTH);
Guy Cohen134eb5d2008-03-04 18:09:25 -08007350 iwl_conf->extension_chan_offset =
7351 ht_bss_conf->bss_cap & IEEE80211_HT_IE_CHA_SEC_OFFSET;
7352 /* If no above or below channel supplied disable FAT channel */
7353 if (iwl_conf->extension_chan_offset != IWL_EXT_CHANNEL_OFFSET_ABOVE &&
7354 iwl_conf->extension_chan_offset != IWL_EXT_CHANNEL_OFFSET_BELOW)
7355 iwl_conf->supported_chan_width = 0;
7356
Ron Rindjunskyfd105e72007-11-26 16:14:39 +02007357 iwl_conf->tx_mimo_ps_mode =
7358 (u8)((ht_conf->cap & IEEE80211_HT_CAP_MIMO_PS) >> 2);
7359 memcpy(iwl_conf->supp_mcs_set, ht_conf->supp_mcs_set, 16);
Zhu Yib481de92007-09-25 17:54:57 -07007360
Ron Rindjunskyfd105e72007-11-26 16:14:39 +02007361 iwl_conf->control_channel = ht_bss_conf->primary_channel;
Ron Rindjunskyfd105e72007-11-26 16:14:39 +02007362 iwl_conf->tx_chan_width =
7363 !!(ht_bss_conf->bss_cap & IEEE80211_HT_IE_CHA_WIDTH);
7364 iwl_conf->ht_protection =
7365 ht_bss_conf->bss_op_mode & IEEE80211_HT_IE_HT_PROTECTION;
7366 iwl_conf->non_GF_STA_present =
7367 !!(ht_bss_conf->bss_op_mode & IEEE80211_HT_IE_NON_GF_STA_PRSNT);
Zhu Yib481de92007-09-25 17:54:57 -07007368
Ron Rindjunskyfd105e72007-11-26 16:14:39 +02007369 IWL_DEBUG_MAC80211("control channel %d\n",
7370 iwl_conf->control_channel);
Zhu Yib481de92007-09-25 17:54:57 -07007371 IWL_DEBUG_MAC80211("leave\n");
Zhu Yib481de92007-09-25 17:54:57 -07007372}
7373
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007374static int iwl4965_mac_conf_ht(struct ieee80211_hw *hw,
Ron Rindjunskyfd105e72007-11-26 16:14:39 +02007375 struct ieee80211_conf *conf)
Zhu Yib481de92007-09-25 17:54:57 -07007376{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007377 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07007378
7379 IWL_DEBUG_MAC80211("enter: \n");
7380
Ron Rindjunskyfd105e72007-11-26 16:14:39 +02007381 iwl4965_ht_info_fill(conf, priv);
Zhu Yib481de92007-09-25 17:54:57 -07007382 iwl4965_set_rxon_chain(priv);
7383
7384 if (priv && priv->assoc_id &&
7385 (priv->iw_mode == IEEE80211_IF_TYPE_STA)) {
7386 unsigned long flags;
7387
7388 spin_lock_irqsave(&priv->lock, flags);
7389 if (priv->beacon_int)
7390 queue_work(priv->workqueue, &priv->post_associate.work);
7391 else
7392 priv->call_post_assoc_from_beacon = 1;
7393 spin_unlock_irqrestore(&priv->lock, flags);
7394 }
7395
Ron Rindjunskyfd105e72007-11-26 16:14:39 +02007396 IWL_DEBUG_MAC80211("leave:\n");
7397 return 0;
Zhu Yib481de92007-09-25 17:54:57 -07007398}
7399
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08007400#endif /*CONFIG_IWL4965_HT*/
Zhu Yib481de92007-09-25 17:54:57 -07007401
7402/*****************************************************************************
7403 *
7404 * sysfs attributes
7405 *
7406 *****************************************************************************/
7407
Tomas Winkler0a6857e2008-03-12 16:58:49 -07007408#ifdef CONFIG_IWLWIFI_DEBUG
Zhu Yib481de92007-09-25 17:54:57 -07007409
7410/*
7411 * The following adds a new attribute to the sysfs representation
7412 * of this device driver (i.e. a new file in /sys/bus/pci/drivers/iwl/)
7413 * used for controlling the debug level.
7414 *
7415 * See the level definitions in iwl for details.
7416 */
7417
7418static ssize_t show_debug_level(struct device_driver *d, char *buf)
7419{
Tomas Winkler0a6857e2008-03-12 16:58:49 -07007420 return sprintf(buf, "0x%08X\n", iwl_debug_level);
Zhu Yib481de92007-09-25 17:54:57 -07007421}
7422static ssize_t store_debug_level(struct device_driver *d,
7423 const char *buf, size_t count)
7424{
7425 char *p = (char *)buf;
7426 u32 val;
7427
7428 val = simple_strtoul(p, &p, 0);
7429 if (p == buf)
7430 printk(KERN_INFO DRV_NAME
7431 ": %s is not in hex or decimal form.\n", buf);
7432 else
Tomas Winkler0a6857e2008-03-12 16:58:49 -07007433 iwl_debug_level = val;
Zhu Yib481de92007-09-25 17:54:57 -07007434
7435 return strnlen(buf, count);
7436}
7437
7438static DRIVER_ATTR(debug_level, S_IWUSR | S_IRUGO,
7439 show_debug_level, store_debug_level);
7440
Tomas Winkler0a6857e2008-03-12 16:58:49 -07007441#endif /* CONFIG_IWLWIFI_DEBUG */
Zhu Yib481de92007-09-25 17:54:57 -07007442
7443static ssize_t show_rf_kill(struct device *d,
7444 struct device_attribute *attr, char *buf)
7445{
7446 /*
7447 * 0 - RF kill not enabled
7448 * 1 - SW based RF kill active (sysfs)
7449 * 2 - HW based RF kill active
7450 * 3 - Both HW and SW based RF kill active
7451 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007452 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07007453 int val = (test_bit(STATUS_RF_KILL_SW, &priv->status) ? 0x1 : 0x0) |
7454 (test_bit(STATUS_RF_KILL_HW, &priv->status) ? 0x2 : 0x0);
7455
7456 return sprintf(buf, "%i\n", val);
7457}
7458
7459static ssize_t store_rf_kill(struct device *d,
7460 struct device_attribute *attr,
7461 const char *buf, size_t count)
7462{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007463 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07007464
7465 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007466 iwl4965_radio_kill_sw(priv, buf[0] == '1');
Zhu Yib481de92007-09-25 17:54:57 -07007467 mutex_unlock(&priv->mutex);
7468
7469 return count;
7470}
7471
7472static DEVICE_ATTR(rf_kill, S_IWUSR | S_IRUGO, show_rf_kill, store_rf_kill);
7473
7474static ssize_t show_temperature(struct device *d,
7475 struct device_attribute *attr, char *buf)
7476{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007477 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07007478
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007479 if (!iwl4965_is_alive(priv))
Zhu Yib481de92007-09-25 17:54:57 -07007480 return -EAGAIN;
7481
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007482 return sprintf(buf, "%d\n", iwl4965_hw_get_temperature(priv));
Zhu Yib481de92007-09-25 17:54:57 -07007483}
7484
7485static DEVICE_ATTR(temperature, S_IRUGO, show_temperature, NULL);
7486
7487static ssize_t show_rs_window(struct device *d,
7488 struct device_attribute *attr,
7489 char *buf)
7490{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007491 struct iwl_priv *priv = d->driver_data;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007492 return iwl4965_fill_rs_info(priv->hw, buf, IWL_AP_ID);
Zhu Yib481de92007-09-25 17:54:57 -07007493}
7494static DEVICE_ATTR(rs_window, S_IRUGO, show_rs_window, NULL);
7495
7496static ssize_t show_tx_power(struct device *d,
7497 struct device_attribute *attr, char *buf)
7498{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007499 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07007500 return sprintf(buf, "%d\n", priv->user_txpower_limit);
7501}
7502
7503static ssize_t store_tx_power(struct device *d,
7504 struct device_attribute *attr,
7505 const char *buf, size_t count)
7506{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007507 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07007508 char *p = (char *)buf;
7509 u32 val;
7510
7511 val = simple_strtoul(p, &p, 10);
7512 if (p == buf)
7513 printk(KERN_INFO DRV_NAME
7514 ": %s is not in decimal form.\n", buf);
7515 else
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007516 iwl4965_hw_reg_set_txpower(priv, val);
Zhu Yib481de92007-09-25 17:54:57 -07007517
7518 return count;
7519}
7520
7521static DEVICE_ATTR(tx_power, S_IWUSR | S_IRUGO, show_tx_power, store_tx_power);
7522
7523static ssize_t show_flags(struct device *d,
7524 struct device_attribute *attr, char *buf)
7525{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007526 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07007527
7528 return sprintf(buf, "0x%04X\n", priv->active_rxon.flags);
7529}
7530
7531static ssize_t store_flags(struct device *d,
7532 struct device_attribute *attr,
7533 const char *buf, size_t count)
7534{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007535 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07007536 u32 flags = simple_strtoul(buf, NULL, 0);
7537
7538 mutex_lock(&priv->mutex);
7539 if (le32_to_cpu(priv->staging_rxon.flags) != flags) {
7540 /* Cancel any currently running scans... */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007541 if (iwl4965_scan_cancel_timeout(priv, 100))
Zhu Yib481de92007-09-25 17:54:57 -07007542 IWL_WARNING("Could not cancel scan.\n");
7543 else {
7544 IWL_DEBUG_INFO("Committing rxon.flags = 0x%04X\n",
7545 flags);
7546 priv->staging_rxon.flags = cpu_to_le32(flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007547 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007548 }
7549 }
7550 mutex_unlock(&priv->mutex);
7551
7552 return count;
7553}
7554
7555static DEVICE_ATTR(flags, S_IWUSR | S_IRUGO, show_flags, store_flags);
7556
7557static ssize_t show_filter_flags(struct device *d,
7558 struct device_attribute *attr, char *buf)
7559{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007560 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07007561
7562 return sprintf(buf, "0x%04X\n",
7563 le32_to_cpu(priv->active_rxon.filter_flags));
7564}
7565
7566static ssize_t store_filter_flags(struct device *d,
7567 struct device_attribute *attr,
7568 const char *buf, size_t count)
7569{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007570 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07007571 u32 filter_flags = simple_strtoul(buf, NULL, 0);
7572
7573 mutex_lock(&priv->mutex);
7574 if (le32_to_cpu(priv->staging_rxon.filter_flags) != filter_flags) {
7575 /* Cancel any currently running scans... */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007576 if (iwl4965_scan_cancel_timeout(priv, 100))
Zhu Yib481de92007-09-25 17:54:57 -07007577 IWL_WARNING("Could not cancel scan.\n");
7578 else {
7579 IWL_DEBUG_INFO("Committing rxon.filter_flags = "
7580 "0x%04X\n", filter_flags);
7581 priv->staging_rxon.filter_flags =
7582 cpu_to_le32(filter_flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007583 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007584 }
7585 }
7586 mutex_unlock(&priv->mutex);
7587
7588 return count;
7589}
7590
7591static DEVICE_ATTR(filter_flags, S_IWUSR | S_IRUGO, show_filter_flags,
7592 store_filter_flags);
7593
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08007594#ifdef CONFIG_IWL4965_SPECTRUM_MEASUREMENT
Zhu Yib481de92007-09-25 17:54:57 -07007595
7596static ssize_t show_measurement(struct device *d,
7597 struct device_attribute *attr, char *buf)
7598{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007599 struct iwl_priv *priv = dev_get_drvdata(d);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007600 struct iwl4965_spectrum_notification measure_report;
Zhu Yib481de92007-09-25 17:54:57 -07007601 u32 size = sizeof(measure_report), len = 0, ofs = 0;
7602 u8 *data = (u8 *) & measure_report;
7603 unsigned long flags;
7604
7605 spin_lock_irqsave(&priv->lock, flags);
7606 if (!(priv->measurement_status & MEASUREMENT_READY)) {
7607 spin_unlock_irqrestore(&priv->lock, flags);
7608 return 0;
7609 }
7610 memcpy(&measure_report, &priv->measure_report, size);
7611 priv->measurement_status = 0;
7612 spin_unlock_irqrestore(&priv->lock, flags);
7613
7614 while (size && (PAGE_SIZE - len)) {
7615 hex_dump_to_buffer(data + ofs, size, 16, 1, buf + len,
7616 PAGE_SIZE - len, 1);
7617 len = strlen(buf);
7618 if (PAGE_SIZE - len)
7619 buf[len++] = '\n';
7620
7621 ofs += 16;
7622 size -= min(size, 16U);
7623 }
7624
7625 return len;
7626}
7627
7628static ssize_t store_measurement(struct device *d,
7629 struct device_attribute *attr,
7630 const char *buf, size_t count)
7631{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007632 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07007633 struct ieee80211_measurement_params params = {
7634 .channel = le16_to_cpu(priv->active_rxon.channel),
7635 .start_time = cpu_to_le64(priv->last_tsf),
7636 .duration = cpu_to_le16(1),
7637 };
7638 u8 type = IWL_MEASURE_BASIC;
7639 u8 buffer[32];
7640 u8 channel;
7641
7642 if (count) {
7643 char *p = buffer;
7644 strncpy(buffer, buf, min(sizeof(buffer), count));
7645 channel = simple_strtoul(p, NULL, 0);
7646 if (channel)
7647 params.channel = channel;
7648
7649 p = buffer;
7650 while (*p && *p != ' ')
7651 p++;
7652 if (*p)
7653 type = simple_strtoul(p + 1, NULL, 0);
7654 }
7655
7656 IWL_DEBUG_INFO("Invoking measurement of type %d on "
7657 "channel %d (for '%s')\n", type, params.channel, buf);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007658 iwl4965_get_measurement(priv, &params, type);
Zhu Yib481de92007-09-25 17:54:57 -07007659
7660 return count;
7661}
7662
7663static DEVICE_ATTR(measurement, S_IRUSR | S_IWUSR,
7664 show_measurement, store_measurement);
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08007665#endif /* CONFIG_IWL4965_SPECTRUM_MEASUREMENT */
Zhu Yib481de92007-09-25 17:54:57 -07007666
7667static ssize_t store_retry_rate(struct device *d,
7668 struct device_attribute *attr,
7669 const char *buf, size_t count)
7670{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007671 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07007672
7673 priv->retry_rate = simple_strtoul(buf, NULL, 0);
7674 if (priv->retry_rate <= 0)
7675 priv->retry_rate = 1;
7676
7677 return count;
7678}
7679
7680static ssize_t show_retry_rate(struct device *d,
7681 struct device_attribute *attr, char *buf)
7682{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007683 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07007684 return sprintf(buf, "%d", priv->retry_rate);
7685}
7686
7687static DEVICE_ATTR(retry_rate, S_IWUSR | S_IRUSR, show_retry_rate,
7688 store_retry_rate);
7689
7690static ssize_t store_power_level(struct device *d,
7691 struct device_attribute *attr,
7692 const char *buf, size_t count)
7693{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007694 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07007695 int rc;
7696 int mode;
7697
7698 mode = simple_strtoul(buf, NULL, 0);
7699 mutex_lock(&priv->mutex);
7700
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007701 if (!iwl4965_is_ready(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07007702 rc = -EAGAIN;
7703 goto out;
7704 }
7705
7706 if ((mode < 1) || (mode > IWL_POWER_LIMIT) || (mode == IWL_POWER_AC))
7707 mode = IWL_POWER_AC;
7708 else
7709 mode |= IWL_POWER_ENABLED;
7710
7711 if (mode != priv->power_mode) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007712 rc = iwl4965_send_power_mode(priv, IWL_POWER_LEVEL(mode));
Zhu Yib481de92007-09-25 17:54:57 -07007713 if (rc) {
7714 IWL_DEBUG_MAC80211("failed setting power mode.\n");
7715 goto out;
7716 }
7717 priv->power_mode = mode;
7718 }
7719
7720 rc = count;
7721
7722 out:
7723 mutex_unlock(&priv->mutex);
7724 return rc;
7725}
7726
7727#define MAX_WX_STRING 80
7728
7729/* Values are in microsecond */
7730static const s32 timeout_duration[] = {
7731 350000,
7732 250000,
7733 75000,
7734 37000,
7735 25000,
7736};
7737static const s32 period_duration[] = {
7738 400000,
7739 700000,
7740 1000000,
7741 1000000,
7742 1000000
7743};
7744
7745static ssize_t show_power_level(struct device *d,
7746 struct device_attribute *attr, char *buf)
7747{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007748 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07007749 int level = IWL_POWER_LEVEL(priv->power_mode);
7750 char *p = buf;
7751
7752 p += sprintf(p, "%d ", level);
7753 switch (level) {
7754 case IWL_POWER_MODE_CAM:
7755 case IWL_POWER_AC:
7756 p += sprintf(p, "(AC)");
7757 break;
7758 case IWL_POWER_BATTERY:
7759 p += sprintf(p, "(BATTERY)");
7760 break;
7761 default:
7762 p += sprintf(p,
7763 "(Timeout %dms, Period %dms)",
7764 timeout_duration[level - 1] / 1000,
7765 period_duration[level - 1] / 1000);
7766 }
7767
7768 if (!(priv->power_mode & IWL_POWER_ENABLED))
7769 p += sprintf(p, " OFF\n");
7770 else
7771 p += sprintf(p, " \n");
7772
7773 return (p - buf + 1);
7774
7775}
7776
7777static DEVICE_ATTR(power_level, S_IWUSR | S_IRUSR, show_power_level,
7778 store_power_level);
7779
7780static ssize_t show_channels(struct device *d,
7781 struct device_attribute *attr, char *buf)
7782{
Johannes Berg8318d782008-01-24 19:38:38 +01007783 /* all this shit doesn't belong into sysfs anyway */
7784 return 0;
Zhu Yib481de92007-09-25 17:54:57 -07007785}
7786
7787static DEVICE_ATTR(channels, S_IRUSR, show_channels, NULL);
7788
7789static ssize_t show_statistics(struct device *d,
7790 struct device_attribute *attr, char *buf)
7791{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007792 struct iwl_priv *priv = dev_get_drvdata(d);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007793 u32 size = sizeof(struct iwl4965_notif_statistics);
Zhu Yib481de92007-09-25 17:54:57 -07007794 u32 len = 0, ofs = 0;
7795 u8 *data = (u8 *) & priv->statistics;
7796 int rc = 0;
7797
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007798 if (!iwl4965_is_alive(priv))
Zhu Yib481de92007-09-25 17:54:57 -07007799 return -EAGAIN;
7800
7801 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007802 rc = iwl4965_send_statistics_request(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007803 mutex_unlock(&priv->mutex);
7804
7805 if (rc) {
7806 len = sprintf(buf,
7807 "Error sending statistics request: 0x%08X\n", rc);
7808 return len;
7809 }
7810
7811 while (size && (PAGE_SIZE - len)) {
7812 hex_dump_to_buffer(data + ofs, size, 16, 1, buf + len,
7813 PAGE_SIZE - len, 1);
7814 len = strlen(buf);
7815 if (PAGE_SIZE - len)
7816 buf[len++] = '\n';
7817
7818 ofs += 16;
7819 size -= min(size, 16U);
7820 }
7821
7822 return len;
7823}
7824
7825static DEVICE_ATTR(statistics, S_IRUGO, show_statistics, NULL);
7826
7827static ssize_t show_antenna(struct device *d,
7828 struct device_attribute *attr, char *buf)
7829{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007830 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07007831
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007832 if (!iwl4965_is_alive(priv))
Zhu Yib481de92007-09-25 17:54:57 -07007833 return -EAGAIN;
7834
7835 return sprintf(buf, "%d\n", priv->antenna);
7836}
7837
7838static ssize_t store_antenna(struct device *d,
7839 struct device_attribute *attr,
7840 const char *buf, size_t count)
7841{
7842 int ant;
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007843 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07007844
7845 if (count == 0)
7846 return 0;
7847
7848 if (sscanf(buf, "%1i", &ant) != 1) {
7849 IWL_DEBUG_INFO("not in hex or decimal form.\n");
7850 return count;
7851 }
7852
7853 if ((ant >= 0) && (ant <= 2)) {
7854 IWL_DEBUG_INFO("Setting antenna select to %d.\n", ant);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007855 priv->antenna = (enum iwl4965_antenna)ant;
Zhu Yib481de92007-09-25 17:54:57 -07007856 } else
7857 IWL_DEBUG_INFO("Bad antenna select value %d.\n", ant);
7858
7859
7860 return count;
7861}
7862
7863static DEVICE_ATTR(antenna, S_IWUSR | S_IRUGO, show_antenna, store_antenna);
7864
7865static ssize_t show_status(struct device *d,
7866 struct device_attribute *attr, char *buf)
7867{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007868 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007869 if (!iwl4965_is_alive(priv))
Zhu Yib481de92007-09-25 17:54:57 -07007870 return -EAGAIN;
7871 return sprintf(buf, "0x%08x\n", (int)priv->status);
7872}
7873
7874static DEVICE_ATTR(status, S_IRUGO, show_status, NULL);
7875
7876static ssize_t dump_error_log(struct device *d,
7877 struct device_attribute *attr,
7878 const char *buf, size_t count)
7879{
7880 char *p = (char *)buf;
7881
7882 if (p[0] == '1')
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007883 iwl4965_dump_nic_error_log((struct iwl_priv *)d->driver_data);
Zhu Yib481de92007-09-25 17:54:57 -07007884
7885 return strnlen(buf, count);
7886}
7887
7888static DEVICE_ATTR(dump_errors, S_IWUSR, NULL, dump_error_log);
7889
7890static ssize_t dump_event_log(struct device *d,
7891 struct device_attribute *attr,
7892 const char *buf, size_t count)
7893{
7894 char *p = (char *)buf;
7895
7896 if (p[0] == '1')
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007897 iwl4965_dump_nic_event_log((struct iwl_priv *)d->driver_data);
Zhu Yib481de92007-09-25 17:54:57 -07007898
7899 return strnlen(buf, count);
7900}
7901
7902static DEVICE_ATTR(dump_events, S_IWUSR, NULL, dump_event_log);
7903
7904/*****************************************************************************
7905 *
7906 * driver setup and teardown
7907 *
7908 *****************************************************************************/
7909
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007910static void iwl4965_setup_deferred_work(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07007911{
7912 priv->workqueue = create_workqueue(DRV_NAME);
7913
7914 init_waitqueue_head(&priv->wait_command_queue);
7915
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007916 INIT_WORK(&priv->up, iwl4965_bg_up);
7917 INIT_WORK(&priv->restart, iwl4965_bg_restart);
7918 INIT_WORK(&priv->rx_replenish, iwl4965_bg_rx_replenish);
7919 INIT_WORK(&priv->scan_completed, iwl4965_bg_scan_completed);
7920 INIT_WORK(&priv->request_scan, iwl4965_bg_request_scan);
7921 INIT_WORK(&priv->abort_scan, iwl4965_bg_abort_scan);
7922 INIT_WORK(&priv->rf_kill, iwl4965_bg_rf_kill);
7923 INIT_WORK(&priv->beacon_update, iwl4965_bg_beacon_update);
7924 INIT_DELAYED_WORK(&priv->post_associate, iwl4965_bg_post_associate);
7925 INIT_DELAYED_WORK(&priv->init_alive_start, iwl4965_bg_init_alive_start);
7926 INIT_DELAYED_WORK(&priv->alive_start, iwl4965_bg_alive_start);
7927 INIT_DELAYED_WORK(&priv->scan_check, iwl4965_bg_scan_check);
Zhu Yib481de92007-09-25 17:54:57 -07007928
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007929 iwl4965_hw_setup_deferred_work(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007930
7931 tasklet_init(&priv->irq_tasklet, (void (*)(unsigned long))
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007932 iwl4965_irq_tasklet, (unsigned long)priv);
Zhu Yib481de92007-09-25 17:54:57 -07007933}
7934
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007935static void iwl4965_cancel_deferred_work(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07007936{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007937 iwl4965_hw_cancel_deferred_work(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007938
Joonwoo Park3ae6a052007-11-29 10:43:16 +09007939 cancel_delayed_work_sync(&priv->init_alive_start);
Zhu Yib481de92007-09-25 17:54:57 -07007940 cancel_delayed_work(&priv->scan_check);
7941 cancel_delayed_work(&priv->alive_start);
7942 cancel_delayed_work(&priv->post_associate);
7943 cancel_work_sync(&priv->beacon_update);
7944}
7945
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007946static struct attribute *iwl4965_sysfs_entries[] = {
Zhu Yib481de92007-09-25 17:54:57 -07007947 &dev_attr_antenna.attr,
7948 &dev_attr_channels.attr,
7949 &dev_attr_dump_errors.attr,
7950 &dev_attr_dump_events.attr,
7951 &dev_attr_flags.attr,
7952 &dev_attr_filter_flags.attr,
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08007953#ifdef CONFIG_IWL4965_SPECTRUM_MEASUREMENT
Zhu Yib481de92007-09-25 17:54:57 -07007954 &dev_attr_measurement.attr,
7955#endif
7956 &dev_attr_power_level.attr,
7957 &dev_attr_retry_rate.attr,
7958 &dev_attr_rf_kill.attr,
7959 &dev_attr_rs_window.attr,
7960 &dev_attr_statistics.attr,
7961 &dev_attr_status.attr,
7962 &dev_attr_temperature.attr,
Zhu Yib481de92007-09-25 17:54:57 -07007963 &dev_attr_tx_power.attr,
7964
7965 NULL
7966};
7967
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007968static struct attribute_group iwl4965_attribute_group = {
Zhu Yib481de92007-09-25 17:54:57 -07007969 .name = NULL, /* put in device directory */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007970 .attrs = iwl4965_sysfs_entries,
Zhu Yib481de92007-09-25 17:54:57 -07007971};
7972
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007973static struct ieee80211_ops iwl4965_hw_ops = {
7974 .tx = iwl4965_mac_tx,
7975 .start = iwl4965_mac_start,
7976 .stop = iwl4965_mac_stop,
7977 .add_interface = iwl4965_mac_add_interface,
7978 .remove_interface = iwl4965_mac_remove_interface,
7979 .config = iwl4965_mac_config,
7980 .config_interface = iwl4965_mac_config_interface,
7981 .configure_filter = iwl4965_configure_filter,
7982 .set_key = iwl4965_mac_set_key,
Emmanuel Grumbachab885f82008-03-20 15:06:43 +02007983 .update_tkip_key = iwl4965_mac_update_tkip_key,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007984 .get_stats = iwl4965_mac_get_stats,
7985 .get_tx_stats = iwl4965_mac_get_tx_stats,
7986 .conf_tx = iwl4965_mac_conf_tx,
7987 .get_tsf = iwl4965_mac_get_tsf,
7988 .reset_tsf = iwl4965_mac_reset_tsf,
7989 .beacon_update = iwl4965_mac_beacon_update,
Johannes Berg471b3ef2007-12-28 14:32:58 +01007990 .bss_info_changed = iwl4965_bss_info_changed,
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08007991#ifdef CONFIG_IWL4965_HT
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007992 .conf_ht = iwl4965_mac_conf_ht,
Ron Rindjunsky9ab46172007-12-25 17:00:38 +02007993 .ampdu_action = iwl4965_mac_ampdu_action,
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08007994#endif /* CONFIG_IWL4965_HT */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007995 .hw_scan = iwl4965_mac_hw_scan
Zhu Yib481de92007-09-25 17:54:57 -07007996};
7997
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007998static int iwl4965_pci_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
Zhu Yib481de92007-09-25 17:54:57 -07007999{
8000 int err = 0;
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07008001 struct iwl_priv *priv;
Zhu Yib481de92007-09-25 17:54:57 -07008002 struct ieee80211_hw *hw;
Tomas Winkler82b9a122008-03-04 18:09:30 -08008003 struct iwl_cfg *cfg = (struct iwl_cfg *)(ent->driver_data);
Zhu Yi5a669262008-01-14 17:46:18 -08008004 DECLARE_MAC_BUF(mac);
Zhu Yib481de92007-09-25 17:54:57 -07008005
Assaf Krauss316c30d2008-03-14 10:38:46 -07008006 /************************
8007 * 1. Allocating HW data
8008 ************************/
8009
Cahill, Ben M6440adb2007-11-29 11:09:55 +08008010 /* Disabling hardware scan means that mac80211 will perform scans
8011 * "the hard way", rather than using device's scan. */
Assaf Krauss1ea87392008-03-18 14:57:50 -07008012 if (cfg->mod_params->disable_hw_scan) {
Zhu Yib481de92007-09-25 17:54:57 -07008013 IWL_DEBUG_INFO("Disabling hw_scan\n");
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008014 iwl4965_hw_ops.hw_scan = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07008015 }
8016
Assaf Krauss1d0a0822008-03-14 10:38:48 -07008017 hw = iwl_alloc_all(cfg, &iwl4965_hw_ops);
8018 if (!hw) {
Zhu Yib481de92007-09-25 17:54:57 -07008019 err = -ENOMEM;
8020 goto out;
8021 }
Assaf Krauss1d0a0822008-03-14 10:38:48 -07008022 priv = hw->priv;
8023 /* At this point both hw and priv are allocated. */
8024
Zhu Yib481de92007-09-25 17:54:57 -07008025 SET_IEEE80211_DEV(hw, &pdev->dev);
8026
8027 IWL_DEBUG_INFO("*** LOAD DRIVER ***\n");
Tomas Winkler82b9a122008-03-04 18:09:30 -08008028 priv->cfg = cfg;
Zhu Yib481de92007-09-25 17:54:57 -07008029 priv->pci_dev = pdev;
Assaf Krauss316c30d2008-03-14 10:38:46 -07008030
Tomas Winkler0a6857e2008-03-12 16:58:49 -07008031#ifdef CONFIG_IWLWIFI_DEBUG
Assaf Krauss1ea87392008-03-18 14:57:50 -07008032 iwl_debug_level = priv->cfg->mod_params->debug;
Zhu Yib481de92007-09-25 17:54:57 -07008033 atomic_set(&priv->restrict_refcnt, 0);
8034#endif
Zhu Yib481de92007-09-25 17:54:57 -07008035
Assaf Krauss316c30d2008-03-14 10:38:46 -07008036 /**************************
8037 * 2. Initializing PCI bus
8038 **************************/
8039 if (pci_enable_device(pdev)) {
8040 err = -ENODEV;
8041 goto out_ieee80211_free_hw;
8042 }
8043
8044 pci_set_master(pdev);
8045
8046 err = pci_set_dma_mask(pdev, DMA_32BIT_MASK);
8047 if (!err)
8048 err = pci_set_consistent_dma_mask(pdev, DMA_32BIT_MASK);
8049 if (err) {
8050 printk(KERN_WARNING DRV_NAME
8051 ": No suitable DMA available.\n");
8052 goto out_pci_disable_device;
8053 }
8054
8055 err = pci_request_regions(pdev, DRV_NAME);
8056 if (err)
8057 goto out_pci_disable_device;
8058
8059 pci_set_drvdata(pdev, priv);
8060
8061 /* We disable the RETRY_TIMEOUT register (0x41) to keep
8062 * PCI Tx retries from interfering with C3 CPU state */
8063 pci_write_config_byte(pdev, 0x41, 0x00);
8064
8065 /***********************
8066 * 3. Read REV register
8067 ***********************/
8068 priv->hw_base = pci_iomap(pdev, 0, 0);
8069 if (!priv->hw_base) {
8070 err = -ENODEV;
8071 goto out_pci_release_regions;
8072 }
8073
8074 IWL_DEBUG_INFO("pci_resource_len = 0x%08llx\n",
8075 (unsigned long long) pci_resource_len(pdev, 0));
8076 IWL_DEBUG_INFO("pci_resource_base = %p\n", priv->hw_base);
8077
8078 printk(KERN_INFO DRV_NAME
8079 ": Detected Intel Wireless WiFi Link %s\n", priv->cfg->name);
8080
8081 /*****************
8082 * 4. Read EEPROM
8083 *****************/
8084 /* nic init */
8085 iwl4965_set_bit(priv, CSR_GIO_CHICKEN_BITS,
8086 CSR_GIO_CHICKEN_BITS_REG_BIT_DIS_L0S_EXIT_TIMER);
8087
8088 iwl4965_set_bit(priv, CSR_GP_CNTRL, CSR_GP_CNTRL_REG_FLAG_INIT_DONE);
8089 err = iwl4965_poll_bit(priv, CSR_GP_CNTRL,
8090 CSR_GP_CNTRL_REG_FLAG_MAC_CLOCK_READY,
8091 CSR_GP_CNTRL_REG_FLAG_MAC_CLOCK_READY, 25000);
8092 if (err < 0) {
8093 IWL_DEBUG_INFO("Failed to init the card\n");
8094 goto out_iounmap;
8095 }
8096 /* Read the EEPROM */
8097 err = iwl_eeprom_init(priv);
8098 if (err) {
8099 IWL_ERROR("Unable to init EEPROM\n");
8100 goto out_iounmap;
8101 }
8102 /* MAC Address location in EEPROM same for 3945/4965 */
8103 iwl_eeprom_get_mac(priv, priv->mac_addr);
8104 IWL_DEBUG_INFO("MAC address: %s\n", print_mac(mac, priv->mac_addr));
8105 SET_IEEE80211_PERM_ADDR(priv->hw, priv->mac_addr);
8106
8107 /************************
8108 * 5. Setup HW constants
8109 ************************/
8110 /* Device-specific setup */
8111 if (iwl4965_hw_set_hw_setting(priv)) {
8112 IWL_ERROR("failed to set hw settings\n");
8113 goto out_iounmap;
8114 }
8115
8116 /*******************
8117 * 6. Setup hw/priv
8118 *******************/
Zhu Yib481de92007-09-25 17:54:57 -07008119
Assaf Kraussbf85ea42008-03-14 10:38:49 -07008120 err = iwl_setup(priv);
8121 if (err)
Assaf Krauss316c30d2008-03-14 10:38:46 -07008122 goto out_unset_hw_settings;
Assaf Kraussbf85ea42008-03-14 10:38:49 -07008123 /* At this point both hw and priv are initialized. */
Assaf Krauss316c30d2008-03-14 10:38:46 -07008124
8125 /**********************************
8126 * 7. Initialize module parameters
8127 **********************************/
8128
8129 /* Disable radio (SW RF KILL) via parameter when loading driver */
Assaf Krauss1ea87392008-03-18 14:57:50 -07008130 if (priv->cfg->mod_params->disable) {
Assaf Krauss316c30d2008-03-14 10:38:46 -07008131 set_bit(STATUS_RF_KILL_SW, &priv->status);
8132 IWL_DEBUG_INFO("Radio disabled.\n");
8133 }
8134
Assaf Krauss1ea87392008-03-18 14:57:50 -07008135 if (priv->cfg->mod_params->enable_qos)
Assaf Krauss316c30d2008-03-14 10:38:46 -07008136 priv->qos_data.qos_enable = 1;
8137
8138 /********************
8139 * 8. Setup services
8140 ********************/
8141 iwl4965_disable_interrupts(priv);
8142
8143 err = sysfs_create_group(&pdev->dev.kobj, &iwl4965_attribute_group);
8144 if (err) {
8145 IWL_ERROR("failed to create sysfs device attributes\n");
Assaf Kraussbf85ea42008-03-14 10:38:49 -07008146 goto out_unset_hw_settings;
Assaf Krauss316c30d2008-03-14 10:38:46 -07008147 }
8148
8149 err = iwl_dbgfs_register(priv, DRV_NAME);
8150 if (err) {
8151 IWL_ERROR("failed to create debugfs files\n");
8152 goto out_remove_sysfs;
8153 }
8154
8155 iwl4965_setup_deferred_work(priv);
8156 iwl4965_setup_rx_handlers(priv);
8157
8158 /********************
8159 * 9. Conclude
8160 ********************/
Zhu Yi5a669262008-01-14 17:46:18 -08008161 pci_save_state(pdev);
8162 pci_disable_device(pdev);
Zhu Yib481de92007-09-25 17:54:57 -07008163
8164 return 0;
8165
Assaf Krauss316c30d2008-03-14 10:38:46 -07008166 out_remove_sysfs:
8167 sysfs_remove_group(&pdev->dev.kobj, &iwl4965_attribute_group);
Assaf Krauss316c30d2008-03-14 10:38:46 -07008168 out_unset_hw_settings:
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008169 iwl4965_unset_hw_setting(priv);
Zhu Yib481de92007-09-25 17:54:57 -07008170 out_iounmap:
8171 pci_iounmap(pdev, priv->hw_base);
8172 out_pci_release_regions:
8173 pci_release_regions(pdev);
Assaf Krauss316c30d2008-03-14 10:38:46 -07008174 pci_set_drvdata(pdev, NULL);
Zhu Yib481de92007-09-25 17:54:57 -07008175 out_pci_disable_device:
8176 pci_disable_device(pdev);
Zhu Yib481de92007-09-25 17:54:57 -07008177 out_ieee80211_free_hw:
8178 ieee80211_free_hw(priv->hw);
8179 out:
8180 return err;
8181}
8182
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008183static void iwl4965_pci_remove(struct pci_dev *pdev)
Zhu Yib481de92007-09-25 17:54:57 -07008184{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07008185 struct iwl_priv *priv = pci_get_drvdata(pdev);
Zhu Yib481de92007-09-25 17:54:57 -07008186 struct list_head *p, *q;
8187 int i;
8188
8189 if (!priv)
8190 return;
8191
8192 IWL_DEBUG_INFO("*** UNLOAD DRIVER ***\n");
8193
Zhu Yib481de92007-09-25 17:54:57 -07008194 set_bit(STATUS_EXIT_PENDING, &priv->status);
Zhu Yib24d22b2007-12-19 13:59:52 +08008195
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008196 iwl4965_down(priv);
Zhu Yib481de92007-09-25 17:54:57 -07008197
8198 /* Free MAC hash list for ADHOC */
8199 for (i = 0; i < IWL_IBSS_MAC_HASH_SIZE; i++) {
8200 list_for_each_safe(p, q, &priv->ibss_mac_hash[i]) {
8201 list_del(p);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008202 kfree(list_entry(p, struct iwl4965_ibss_seq, list));
Zhu Yib481de92007-09-25 17:54:57 -07008203 }
8204 }
8205
Tomas Winkler712b6cf2008-03-12 16:58:52 -07008206 iwl_dbgfs_unregister(priv);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008207 sysfs_remove_group(&pdev->dev.kobj, &iwl4965_attribute_group);
Zhu Yib481de92007-09-25 17:54:57 -07008208
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008209 iwl4965_dealloc_ucode_pci(priv);
Zhu Yib481de92007-09-25 17:54:57 -07008210
8211 if (priv->rxq.bd)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008212 iwl4965_rx_queue_free(priv, &priv->rxq);
8213 iwl4965_hw_txq_ctx_free(priv);
Zhu Yib481de92007-09-25 17:54:57 -07008214
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008215 iwl4965_unset_hw_setting(priv);
Assaf Kraussbf85ea42008-03-14 10:38:49 -07008216 iwlcore_clear_stations_table(priv);
Zhu Yib481de92007-09-25 17:54:57 -07008217
8218 if (priv->mac80211_registered) {
8219 ieee80211_unregister_hw(priv->hw);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008220 iwl4965_rate_control_unregister(priv->hw);
Zhu Yib481de92007-09-25 17:54:57 -07008221 }
8222
Mohamed Abbas948c1712007-10-25 17:15:45 +08008223 /*netif_stop_queue(dev); */
8224 flush_workqueue(priv->workqueue);
8225
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008226 /* ieee80211_unregister_hw calls iwl4965_mac_stop, which flushes
Zhu Yib481de92007-09-25 17:54:57 -07008227 * priv->workqueue... so we can't take down the workqueue
8228 * until now... */
8229 destroy_workqueue(priv->workqueue);
8230 priv->workqueue = NULL;
8231
Zhu Yib481de92007-09-25 17:54:57 -07008232 pci_iounmap(pdev, priv->hw_base);
8233 pci_release_regions(pdev);
8234 pci_disable_device(pdev);
8235 pci_set_drvdata(pdev, NULL);
8236
Assaf Kraussbf85ea42008-03-14 10:38:49 -07008237 iwl_free_channel_map(priv);
Reinette Chatre849e0dc2008-01-23 10:15:18 -08008238 iwl4965_free_geos(priv);
Zhu Yib481de92007-09-25 17:54:57 -07008239
8240 if (priv->ibss_beacon)
8241 dev_kfree_skb(priv->ibss_beacon);
8242
8243 ieee80211_free_hw(priv->hw);
8244}
8245
8246#ifdef CONFIG_PM
8247
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008248static int iwl4965_pci_suspend(struct pci_dev *pdev, pm_message_t state)
Zhu Yib481de92007-09-25 17:54:57 -07008249{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07008250 struct iwl_priv *priv = pci_get_drvdata(pdev);
Zhu Yib481de92007-09-25 17:54:57 -07008251
Zhu Yie655b9f2008-01-24 02:19:38 -08008252 if (priv->is_open) {
8253 set_bit(STATUS_IN_SUSPEND, &priv->status);
8254 iwl4965_mac_stop(priv->hw);
8255 priv->is_open = 1;
8256 }
Zhu Yib481de92007-09-25 17:54:57 -07008257
Zhu Yib481de92007-09-25 17:54:57 -07008258 pci_set_power_state(pdev, PCI_D3hot);
8259
Zhu Yib481de92007-09-25 17:54:57 -07008260 return 0;
8261}
8262
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008263static int iwl4965_pci_resume(struct pci_dev *pdev)
Zhu Yib481de92007-09-25 17:54:57 -07008264{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07008265 struct iwl_priv *priv = pci_get_drvdata(pdev);
Zhu Yib481de92007-09-25 17:54:57 -07008266
Zhu Yib481de92007-09-25 17:54:57 -07008267 pci_set_power_state(pdev, PCI_D0);
Zhu Yib481de92007-09-25 17:54:57 -07008268
Zhu Yie655b9f2008-01-24 02:19:38 -08008269 if (priv->is_open)
8270 iwl4965_mac_start(priv->hw);
Zhu Yib481de92007-09-25 17:54:57 -07008271
Zhu Yie655b9f2008-01-24 02:19:38 -08008272 clear_bit(STATUS_IN_SUSPEND, &priv->status);
Zhu Yib481de92007-09-25 17:54:57 -07008273 return 0;
8274}
8275
8276#endif /* CONFIG_PM */
8277
8278/*****************************************************************************
8279 *
8280 * driver and module entry point
8281 *
8282 *****************************************************************************/
8283
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008284static struct pci_driver iwl4965_driver = {
Zhu Yib481de92007-09-25 17:54:57 -07008285 .name = DRV_NAME,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008286 .id_table = iwl4965_hw_card_ids,
8287 .probe = iwl4965_pci_probe,
8288 .remove = __devexit_p(iwl4965_pci_remove),
Zhu Yib481de92007-09-25 17:54:57 -07008289#ifdef CONFIG_PM
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008290 .suspend = iwl4965_pci_suspend,
8291 .resume = iwl4965_pci_resume,
Zhu Yib481de92007-09-25 17:54:57 -07008292#endif
8293};
8294
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008295static int __init iwl4965_init(void)
Zhu Yib481de92007-09-25 17:54:57 -07008296{
8297
8298 int ret;
8299 printk(KERN_INFO DRV_NAME ": " DRV_DESCRIPTION ", " DRV_VERSION "\n");
8300 printk(KERN_INFO DRV_NAME ": " DRV_COPYRIGHT "\n");
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008301 ret = pci_register_driver(&iwl4965_driver);
Zhu Yib481de92007-09-25 17:54:57 -07008302 if (ret) {
8303 IWL_ERROR("Unable to initialize PCI module\n");
8304 return ret;
8305 }
Tomas Winkler0a6857e2008-03-12 16:58:49 -07008306#ifdef CONFIG_IWLWIFI_DEBUG
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008307 ret = driver_create_file(&iwl4965_driver.driver, &driver_attr_debug_level);
Zhu Yib481de92007-09-25 17:54:57 -07008308 if (ret) {
8309 IWL_ERROR("Unable to create driver sysfs file\n");
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008310 pci_unregister_driver(&iwl4965_driver);
Zhu Yib481de92007-09-25 17:54:57 -07008311 return ret;
8312 }
8313#endif
8314
8315 return ret;
8316}
8317
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008318static void __exit iwl4965_exit(void)
Zhu Yib481de92007-09-25 17:54:57 -07008319{
Tomas Winkler0a6857e2008-03-12 16:58:49 -07008320#ifdef CONFIG_IWLWIFI_DEBUG
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008321 driver_remove_file(&iwl4965_driver.driver, &driver_attr_debug_level);
Zhu Yib481de92007-09-25 17:54:57 -07008322#endif
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008323 pci_unregister_driver(&iwl4965_driver);
Zhu Yib481de92007-09-25 17:54:57 -07008324}
8325
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008326module_exit(iwl4965_exit);
8327module_init(iwl4965_init);