blob: d84a7f296d90c090cba4e38c3fe8079a2e226128 [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
Christoph Hellwigbb8c0932008-01-27 16:41:47 -080053static int iwl4965_tx_queue_update_write_ptr(struct iwl4965_priv *priv,
54 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
62/* module parameters */
Cahill, Ben M6440adb2007-11-29 11:09:55 +080063static int iwl4965_param_disable_hw_scan; /* def: 0 = use 4965's h/w scan */
64static int iwl4965_param_debug; /* def: 0 = minimal debug log messages */
Ben Cahill9fbab512007-11-29 11:09:47 +080065static int iwl4965_param_disable; /* def: enable radio */
66static int iwl4965_param_antenna; /* def: 0 = both antennas (use diversity) */
67int iwl4965_param_hwcrypto; /* def: using software encryption */
Cahill, Ben M6440adb2007-11-29 11:09:55 +080068static int iwl4965_param_qos_enable = 1; /* def: 1 = use quality of service */
69int iwl4965_param_queues_num = IWL_MAX_NUM_QUEUES; /* def: 16 Tx queues */
Ron Rindjunsky9ee1ba42007-11-26 16:14:42 +020070int iwl4965_param_amsdu_size_8K; /* def: enable 8K amsdu size */
Zhu Yib481de92007-09-25 17:54:57 -070071
72/*
73 * module name, copyright, version, etc.
74 * NOTE: DRV_NAME is defined in iwlwifi.h for use by iwl-debug.h and printk
75 */
76
77#define DRV_DESCRIPTION "Intel(R) Wireless WiFi Link 4965AGN driver for Linux"
78
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +080079#ifdef CONFIG_IWL4965_DEBUG
Zhu Yib481de92007-09-25 17:54:57 -070080#define VD "d"
81#else
82#define VD
83#endif
84
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +080085#ifdef CONFIG_IWL4965_SPECTRUM_MEASUREMENT
Zhu Yib481de92007-09-25 17:54:57 -070086#define VS "s"
87#else
88#define VS
89#endif
90
Tomas Winklerdf48c322008-03-06 10:40:19 -080091#define DRV_VERSION IWLWIFI_VERSION VD VS
Zhu Yib481de92007-09-25 17:54:57 -070092
Zhu Yib481de92007-09-25 17:54:57 -070093
94MODULE_DESCRIPTION(DRV_DESCRIPTION);
95MODULE_VERSION(DRV_VERSION);
96MODULE_AUTHOR(DRV_COPYRIGHT);
97MODULE_LICENSE("GPL");
98
99__le16 *ieee80211_get_qos_ctrl(struct ieee80211_hdr *hdr)
100{
101 u16 fc = le16_to_cpu(hdr->frame_control);
102 int hdr_len = ieee80211_get_hdrlen(fc);
103
104 if ((fc & 0x00cc) == (IEEE80211_STYPE_QOS_DATA | IEEE80211_FTYPE_DATA))
105 return (__le16 *) ((u8 *) hdr + hdr_len - QOS_CONTROL_LEN);
106 return NULL;
107}
108
Johannes Berg8318d782008-01-24 19:38:38 +0100109static const struct ieee80211_supported_band *iwl4965_get_hw_mode(
110 struct iwl4965_priv *priv, enum ieee80211_band band)
Zhu Yib481de92007-09-25 17:54:57 -0700111{
Johannes Berg8318d782008-01-24 19:38:38 +0100112 return priv->hw->wiphy->bands[band];
Zhu Yib481de92007-09-25 17:54:57 -0700113}
114
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800115static int iwl4965_is_empty_essid(const char *essid, int essid_len)
Zhu Yib481de92007-09-25 17:54:57 -0700116{
117 /* Single white space is for Linksys APs */
118 if (essid_len == 1 && essid[0] == ' ')
119 return 1;
120
121 /* Otherwise, if the entire essid is 0, we assume it is hidden */
122 while (essid_len) {
123 essid_len--;
124 if (essid[essid_len] != '\0')
125 return 0;
126 }
127
128 return 1;
129}
130
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800131static const char *iwl4965_escape_essid(const char *essid, u8 essid_len)
Zhu Yib481de92007-09-25 17:54:57 -0700132{
133 static char escaped[IW_ESSID_MAX_SIZE * 2 + 1];
134 const char *s = essid;
135 char *d = escaped;
136
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800137 if (iwl4965_is_empty_essid(essid, essid_len)) {
Zhu Yib481de92007-09-25 17:54:57 -0700138 memcpy(escaped, "<hidden>", sizeof("<hidden>"));
139 return escaped;
140 }
141
142 essid_len = min(essid_len, (u8) IW_ESSID_MAX_SIZE);
143 while (essid_len--) {
144 if (*s == '\0') {
145 *d++ = '\\';
146 *d++ = '0';
147 s++;
148 } else
149 *d++ = *s++;
150 }
151 *d = '\0';
152 return escaped;
153}
154
Zhu Yib481de92007-09-25 17:54:57 -0700155/*************** DMA-QUEUE-GENERAL-FUNCTIONS *****
156 * DMA services
157 *
158 * Theory of operation
159 *
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800160 * A Tx or Rx queue resides in host DRAM, and is comprised of a circular buffer
161 * of buffer descriptors, each of which points to one or more data buffers for
162 * the device to read from or fill. Driver and device exchange status of each
163 * queue via "read" and "write" pointers. Driver keeps minimum of 2 empty
164 * entries in each circular buffer, to protect against confusing empty and full
165 * queue states.
166 *
167 * The device reads or writes the data in the queues via the device's several
168 * DMA/FIFO channels. Each queue is mapped to a single DMA channel.
Zhu Yib481de92007-09-25 17:54:57 -0700169 *
170 * For Tx queue, there are low mark and high mark limits. If, after queuing
171 * the packet for Tx, free space become < low mark, Tx queue stopped. When
172 * reclaiming packets (on 'tx done IRQ), if free space become > high mark,
173 * Tx queue resumed.
174 *
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800175 * The 4965 operates with up to 17 queues: One receive queue, one transmit
176 * queue (#4) for sending commands to the device firmware, and 15 other
177 * Tx queues that may be mapped to prioritized Tx DMA/FIFO channels.
Ben Cahille3851442007-11-29 11:10:07 +0800178 *
179 * See more detailed info in iwl-4965-hw.h.
Zhu Yib481de92007-09-25 17:54:57 -0700180 ***************************************************/
181
Ron Rindjunskyfe01b472008-01-28 14:07:24 +0200182int iwl4965_queue_space(const struct iwl4965_queue *q)
Zhu Yib481de92007-09-25 17:54:57 -0700183{
Tomas Winklerfc4b6852007-10-25 17:15:24 +0800184 int s = q->read_ptr - q->write_ptr;
Zhu Yib481de92007-09-25 17:54:57 -0700185
Tomas Winklerfc4b6852007-10-25 17:15:24 +0800186 if (q->read_ptr > q->write_ptr)
Zhu Yib481de92007-09-25 17:54:57 -0700187 s -= q->n_bd;
188
189 if (s <= 0)
190 s += q->n_window;
191 /* keep some reserve to not confuse empty and full situations */
192 s -= 2;
193 if (s < 0)
194 s = 0;
195 return s;
196}
197
Zhu Yib481de92007-09-25 17:54:57 -0700198
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800199static inline int x2_queue_used(const struct iwl4965_queue *q, int i)
Zhu Yib481de92007-09-25 17:54:57 -0700200{
Tomas Winklerfc4b6852007-10-25 17:15:24 +0800201 return q->write_ptr > q->read_ptr ?
202 (i >= q->read_ptr && i < q->write_ptr) :
203 !(i < q->read_ptr && i >= q->write_ptr);
Zhu Yib481de92007-09-25 17:54:57 -0700204}
205
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800206static inline u8 get_cmd_index(struct iwl4965_queue *q, u32 index, int is_huge)
Zhu Yib481de92007-09-25 17:54:57 -0700207{
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800208 /* This is for scan command, the big buffer at end of command array */
Zhu Yib481de92007-09-25 17:54:57 -0700209 if (is_huge)
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800210 return q->n_window; /* must be power of 2 */
Zhu Yib481de92007-09-25 17:54:57 -0700211
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800212 /* Otherwise, use normal size buffers */
Zhu Yib481de92007-09-25 17:54:57 -0700213 return index & (q->n_window - 1);
214}
215
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800216/**
217 * iwl4965_queue_init - Initialize queue's high/low-water and read/write indexes
218 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800219static int iwl4965_queue_init(struct iwl4965_priv *priv, struct iwl4965_queue *q,
Zhu Yib481de92007-09-25 17:54:57 -0700220 int count, int slots_num, u32 id)
221{
222 q->n_bd = count;
223 q->n_window = slots_num;
224 q->id = id;
225
Tomas Winklerc54b6792008-03-06 17:36:53 -0800226 /* count must be power-of-two size, otherwise iwl_queue_inc_wrap
227 * and iwl_queue_dec_wrap are broken. */
Zhu Yib481de92007-09-25 17:54:57 -0700228 BUG_ON(!is_power_of_2(count));
229
230 /* slots_num must be power-of-two size, otherwise
231 * get_cmd_index is broken. */
232 BUG_ON(!is_power_of_2(slots_num));
233
234 q->low_mark = q->n_window / 4;
235 if (q->low_mark < 4)
236 q->low_mark = 4;
237
238 q->high_mark = q->n_window / 8;
239 if (q->high_mark < 2)
240 q->high_mark = 2;
241
Tomas Winklerfc4b6852007-10-25 17:15:24 +0800242 q->write_ptr = q->read_ptr = 0;
Zhu Yib481de92007-09-25 17:54:57 -0700243
244 return 0;
245}
246
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800247/**
248 * iwl4965_tx_queue_alloc - Alloc driver data and TFD CB for one Tx/cmd queue
249 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800250static int iwl4965_tx_queue_alloc(struct iwl4965_priv *priv,
251 struct iwl4965_tx_queue *txq, u32 id)
Zhu Yib481de92007-09-25 17:54:57 -0700252{
253 struct pci_dev *dev = priv->pci_dev;
254
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800255 /* Driver private data, only for Tx (not command) queues,
256 * not shared with device. */
Zhu Yib481de92007-09-25 17:54:57 -0700257 if (id != IWL_CMD_QUEUE_NUM) {
258 txq->txb = kmalloc(sizeof(txq->txb[0]) *
259 TFD_QUEUE_SIZE_MAX, GFP_KERNEL);
260 if (!txq->txb) {
Ian Schram01ebd062007-10-25 17:15:22 +0800261 IWL_ERROR("kmalloc for auxiliary BD "
Zhu Yib481de92007-09-25 17:54:57 -0700262 "structures failed\n");
263 goto error;
264 }
265 } else
266 txq->txb = NULL;
267
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800268 /* Circular buffer of transmit frame descriptors (TFDs),
269 * shared with device */
Zhu Yib481de92007-09-25 17:54:57 -0700270 txq->bd = pci_alloc_consistent(dev,
271 sizeof(txq->bd[0]) * TFD_QUEUE_SIZE_MAX,
272 &txq->q.dma_addr);
273
274 if (!txq->bd) {
275 IWL_ERROR("pci_alloc_consistent(%zd) failed\n",
276 sizeof(txq->bd[0]) * TFD_QUEUE_SIZE_MAX);
277 goto error;
278 }
279 txq->q.id = id;
280
281 return 0;
282
283 error:
284 if (txq->txb) {
285 kfree(txq->txb);
286 txq->txb = NULL;
287 }
288
289 return -ENOMEM;
290}
291
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +0800292/**
293 * iwl4965_tx_queue_init - Allocate and initialize one tx/cmd queue
294 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800295int iwl4965_tx_queue_init(struct iwl4965_priv *priv,
296 struct iwl4965_tx_queue *txq, int slots_num, u32 txq_id)
Zhu Yib481de92007-09-25 17:54:57 -0700297{
298 struct pci_dev *dev = priv->pci_dev;
299 int len;
300 int rc = 0;
301
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +0800302 /*
303 * Alloc buffer array for commands (Tx or other types of commands).
304 * For the command queue (#4), allocate command space + one big
305 * command for scan, since scan command is very huge; the system will
306 * not have two scans at the same time, so only one is needed.
Tomas Winklerbb542442007-12-05 20:59:59 +0200307 * For normal Tx queues (all other queues), no super-size command
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +0800308 * space is needed.
309 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800310 len = sizeof(struct iwl4965_cmd) * slots_num;
Zhu Yib481de92007-09-25 17:54:57 -0700311 if (txq_id == IWL_CMD_QUEUE_NUM)
312 len += IWL_MAX_SCAN_SIZE;
313 txq->cmd = pci_alloc_consistent(dev, len, &txq->dma_addr_cmd);
314 if (!txq->cmd)
315 return -ENOMEM;
316
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +0800317 /* Alloc driver data array and TFD circular buffer */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800318 rc = iwl4965_tx_queue_alloc(priv, txq, txq_id);
Zhu Yib481de92007-09-25 17:54:57 -0700319 if (rc) {
320 pci_free_consistent(dev, len, txq->cmd, txq->dma_addr_cmd);
321
322 return -ENOMEM;
323 }
324 txq->need_update = 0;
325
326 /* TFD_QUEUE_SIZE_MAX must be power-of-two size, otherwise
Tomas Winklerc54b6792008-03-06 17:36:53 -0800327 * iwl_queue_inc_wrap and iwl_queue_dec_wrap are broken. */
Zhu Yib481de92007-09-25 17:54:57 -0700328 BUILD_BUG_ON(TFD_QUEUE_SIZE_MAX & (TFD_QUEUE_SIZE_MAX - 1));
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +0800329
330 /* Initialize queue's high/low-water marks, and head/tail indexes */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800331 iwl4965_queue_init(priv, &txq->q, TFD_QUEUE_SIZE_MAX, slots_num, txq_id);
Zhu Yib481de92007-09-25 17:54:57 -0700332
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +0800333 /* Tell device where to find queue */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800334 iwl4965_hw_tx_queue_init(priv, txq);
Zhu Yib481de92007-09-25 17:54:57 -0700335
336 return 0;
337}
338
339/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800340 * iwl4965_tx_queue_free - Deallocate DMA queue.
Zhu Yib481de92007-09-25 17:54:57 -0700341 * @txq: Transmit queue to deallocate.
342 *
343 * Empty queue by removing and destroying all BD's.
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800344 * Free all buffers.
345 * 0-fill, but do not free "txq" descriptor structure.
Zhu Yib481de92007-09-25 17:54:57 -0700346 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800347void iwl4965_tx_queue_free(struct iwl4965_priv *priv, struct iwl4965_tx_queue *txq)
Zhu Yib481de92007-09-25 17:54:57 -0700348{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800349 struct iwl4965_queue *q = &txq->q;
Zhu Yib481de92007-09-25 17:54:57 -0700350 struct pci_dev *dev = priv->pci_dev;
351 int len;
352
353 if (q->n_bd == 0)
354 return;
355
356 /* first, empty all BD's */
Tomas Winklerfc4b6852007-10-25 17:15:24 +0800357 for (; q->write_ptr != q->read_ptr;
Tomas Winklerc54b6792008-03-06 17:36:53 -0800358 q->read_ptr = iwl_queue_inc_wrap(q->read_ptr, q->n_bd))
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800359 iwl4965_hw_txq_free_tfd(priv, txq);
Zhu Yib481de92007-09-25 17:54:57 -0700360
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800361 len = sizeof(struct iwl4965_cmd) * q->n_window;
Zhu Yib481de92007-09-25 17:54:57 -0700362 if (q->id == IWL_CMD_QUEUE_NUM)
363 len += IWL_MAX_SCAN_SIZE;
364
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800365 /* De-alloc array of command/tx buffers */
Zhu Yib481de92007-09-25 17:54:57 -0700366 pci_free_consistent(dev, len, txq->cmd, txq->dma_addr_cmd);
367
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800368 /* De-alloc circular buffer of TFDs */
Zhu Yib481de92007-09-25 17:54:57 -0700369 if (txq->q.n_bd)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800370 pci_free_consistent(dev, sizeof(struct iwl4965_tfd_frame) *
Zhu Yib481de92007-09-25 17:54:57 -0700371 txq->q.n_bd, txq->bd, txq->q.dma_addr);
372
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800373 /* De-alloc array of per-TFD driver data */
Zhu Yib481de92007-09-25 17:54:57 -0700374 if (txq->txb) {
375 kfree(txq->txb);
376 txq->txb = NULL;
377 }
378
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800379 /* 0-fill queue descriptor structure */
Zhu Yib481de92007-09-25 17:54:57 -0700380 memset(txq, 0, sizeof(*txq));
381}
382
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800383const u8 iwl4965_broadcast_addr[ETH_ALEN] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
Zhu Yib481de92007-09-25 17:54:57 -0700384
385/*************** STATION TABLE MANAGEMENT ****
Ben Cahill9fbab512007-11-29 11:09:47 +0800386 * mac80211 should be examined to determine if sta_info is duplicating
Zhu Yib481de92007-09-25 17:54:57 -0700387 * the functionality provided here
388 */
389
390/**************************************************************/
391
Ian Schram01ebd062007-10-25 17:15:22 +0800392#if 0 /* temporary disable till we add real remove station */
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800393/**
394 * iwl4965_remove_station - Remove driver's knowledge of station.
395 *
396 * NOTE: This does not remove station from device's station table.
397 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800398static u8 iwl4965_remove_station(struct iwl4965_priv *priv, const u8 *addr, int is_ap)
Zhu Yib481de92007-09-25 17:54:57 -0700399{
400 int index = IWL_INVALID_STATION;
401 int i;
402 unsigned long flags;
403
404 spin_lock_irqsave(&priv->sta_lock, flags);
405
406 if (is_ap)
407 index = IWL_AP_ID;
408 else if (is_broadcast_ether_addr(addr))
409 index = priv->hw_setting.bcast_sta_id;
410 else
411 for (i = IWL_STA_ID; i < priv->hw_setting.max_stations; i++)
412 if (priv->stations[i].used &&
413 !compare_ether_addr(priv->stations[i].sta.sta.addr,
414 addr)) {
415 index = i;
416 break;
417 }
418
419 if (unlikely(index == IWL_INVALID_STATION))
420 goto out;
421
422 if (priv->stations[index].used) {
423 priv->stations[index].used = 0;
424 priv->num_stations--;
425 }
426
427 BUG_ON(priv->num_stations < 0);
428
429out:
430 spin_unlock_irqrestore(&priv->sta_lock, flags);
431 return 0;
432}
Zhu Yi556f8db2007-09-27 11:27:33 +0800433#endif
Zhu Yib481de92007-09-25 17:54:57 -0700434
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800435/**
436 * iwl4965_clear_stations_table - Clear the driver's station table
437 *
438 * NOTE: This does not clear or otherwise alter the device's station table.
439 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800440static void iwl4965_clear_stations_table(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700441{
442 unsigned long flags;
443
444 spin_lock_irqsave(&priv->sta_lock, flags);
445
446 priv->num_stations = 0;
447 memset(priv->stations, 0, sizeof(priv->stations));
448
449 spin_unlock_irqrestore(&priv->sta_lock, flags);
450}
451
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800452/**
453 * iwl4965_add_station_flags - Add station to tables in driver and device
454 */
Ron Rindjunsky67d62032007-11-26 16:14:40 +0200455u8 iwl4965_add_station_flags(struct iwl4965_priv *priv, const u8 *addr,
456 int is_ap, u8 flags, void *ht_data)
Zhu Yib481de92007-09-25 17:54:57 -0700457{
458 int i;
459 int index = IWL_INVALID_STATION;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800460 struct iwl4965_station_entry *station;
Zhu Yib481de92007-09-25 17:54:57 -0700461 unsigned long flags_spin;
Joe Perches0795af52007-10-03 17:59:30 -0700462 DECLARE_MAC_BUF(mac);
Zhu Yib481de92007-09-25 17:54:57 -0700463
464 spin_lock_irqsave(&priv->sta_lock, flags_spin);
465 if (is_ap)
466 index = IWL_AP_ID;
467 else if (is_broadcast_ether_addr(addr))
468 index = priv->hw_setting.bcast_sta_id;
469 else
470 for (i = IWL_STA_ID; i < priv->hw_setting.max_stations; i++) {
471 if (!compare_ether_addr(priv->stations[i].sta.sta.addr,
472 addr)) {
473 index = i;
474 break;
475 }
476
477 if (!priv->stations[i].used &&
478 index == IWL_INVALID_STATION)
479 index = i;
480 }
481
482
Ben Cahill9fbab512007-11-29 11:09:47 +0800483 /* These two conditions have the same outcome, but keep them separate
484 since they have different meanings */
Zhu Yib481de92007-09-25 17:54:57 -0700485 if (unlikely(index == IWL_INVALID_STATION)) {
486 spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
487 return index;
488 }
489
490 if (priv->stations[index].used &&
491 !compare_ether_addr(priv->stations[index].sta.sta.addr, addr)) {
492 spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
493 return index;
494 }
495
496
Joe Perches0795af52007-10-03 17:59:30 -0700497 IWL_DEBUG_ASSOC("Add STA ID %d: %s\n", index, print_mac(mac, addr));
Zhu Yib481de92007-09-25 17:54:57 -0700498 station = &priv->stations[index];
499 station->used = 1;
500 priv->num_stations++;
501
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800502 /* Set up the REPLY_ADD_STA command to send to device */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800503 memset(&station->sta, 0, sizeof(struct iwl4965_addsta_cmd));
Zhu Yib481de92007-09-25 17:54:57 -0700504 memcpy(station->sta.sta.addr, addr, ETH_ALEN);
505 station->sta.mode = 0;
506 station->sta.sta.sta_id = index;
507 station->sta.station_flags = 0;
508
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +0800509#ifdef CONFIG_IWL4965_HT
Zhu Yib481de92007-09-25 17:54:57 -0700510 /* BCAST station and IBSS stations do not work in HT mode */
511 if (index != priv->hw_setting.bcast_sta_id &&
512 priv->iw_mode != IEEE80211_IF_TYPE_IBSS)
Ron Rindjunsky67d62032007-11-26 16:14:40 +0200513 iwl4965_set_ht_add_station(priv, index,
514 (struct ieee80211_ht_info *) ht_data);
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +0800515#endif /*CONFIG_IWL4965_HT*/
Zhu Yib481de92007-09-25 17:54:57 -0700516
517 spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800518
519 /* Add station to device's station table */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800520 iwl4965_send_add_station(priv, &station->sta, flags);
Zhu Yib481de92007-09-25 17:54:57 -0700521 return index;
522
523}
524
525/*************** DRIVER STATUS FUNCTIONS *****/
526
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800527static inline int iwl4965_is_ready(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700528{
529 /* The adapter is 'ready' if READY and GEO_CONFIGURED bits are
530 * set but EXIT_PENDING is not */
531 return test_bit(STATUS_READY, &priv->status) &&
532 test_bit(STATUS_GEO_CONFIGURED, &priv->status) &&
533 !test_bit(STATUS_EXIT_PENDING, &priv->status);
534}
535
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800536static inline int iwl4965_is_alive(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700537{
538 return test_bit(STATUS_ALIVE, &priv->status);
539}
540
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800541static inline int iwl4965_is_init(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700542{
543 return test_bit(STATUS_INIT, &priv->status);
544}
545
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800546static inline int iwl4965_is_rfkill(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700547{
548 return test_bit(STATUS_RF_KILL_HW, &priv->status) ||
549 test_bit(STATUS_RF_KILL_SW, &priv->status);
550}
551
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800552static inline int iwl4965_is_ready_rf(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700553{
554
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800555 if (iwl4965_is_rfkill(priv))
Zhu Yib481de92007-09-25 17:54:57 -0700556 return 0;
557
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800558 return iwl4965_is_ready(priv);
Zhu Yib481de92007-09-25 17:54:57 -0700559}
560
561/*************** HOST COMMAND QUEUE FUNCTIONS *****/
562
563#define IWL_CMD(x) case x : return #x
564
565static const char *get_cmd_string(u8 cmd)
566{
567 switch (cmd) {
568 IWL_CMD(REPLY_ALIVE);
569 IWL_CMD(REPLY_ERROR);
570 IWL_CMD(REPLY_RXON);
571 IWL_CMD(REPLY_RXON_ASSOC);
572 IWL_CMD(REPLY_QOS_PARAM);
573 IWL_CMD(REPLY_RXON_TIMING);
574 IWL_CMD(REPLY_ADD_STA);
575 IWL_CMD(REPLY_REMOVE_STA);
576 IWL_CMD(REPLY_REMOVE_ALL_STA);
577 IWL_CMD(REPLY_TX);
578 IWL_CMD(REPLY_RATE_SCALE);
579 IWL_CMD(REPLY_LEDS_CMD);
580 IWL_CMD(REPLY_TX_LINK_QUALITY_CMD);
581 IWL_CMD(RADAR_NOTIFICATION);
582 IWL_CMD(REPLY_QUIET_CMD);
583 IWL_CMD(REPLY_CHANNEL_SWITCH);
584 IWL_CMD(CHANNEL_SWITCH_NOTIFICATION);
585 IWL_CMD(REPLY_SPECTRUM_MEASUREMENT_CMD);
586 IWL_CMD(SPECTRUM_MEASURE_NOTIFICATION);
587 IWL_CMD(POWER_TABLE_CMD);
588 IWL_CMD(PM_SLEEP_NOTIFICATION);
589 IWL_CMD(PM_DEBUG_STATISTIC_NOTIFIC);
590 IWL_CMD(REPLY_SCAN_CMD);
591 IWL_CMD(REPLY_SCAN_ABORT_CMD);
592 IWL_CMD(SCAN_START_NOTIFICATION);
593 IWL_CMD(SCAN_RESULTS_NOTIFICATION);
594 IWL_CMD(SCAN_COMPLETE_NOTIFICATION);
595 IWL_CMD(BEACON_NOTIFICATION);
596 IWL_CMD(REPLY_TX_BEACON);
597 IWL_CMD(WHO_IS_AWAKE_NOTIFICATION);
598 IWL_CMD(QUIET_NOTIFICATION);
599 IWL_CMD(REPLY_TX_PWR_TABLE_CMD);
600 IWL_CMD(MEASURE_ABORT_NOTIFICATION);
601 IWL_CMD(REPLY_BT_CONFIG);
602 IWL_CMD(REPLY_STATISTICS_CMD);
603 IWL_CMD(STATISTICS_NOTIFICATION);
604 IWL_CMD(REPLY_CARD_STATE_CMD);
605 IWL_CMD(CARD_STATE_NOTIFICATION);
606 IWL_CMD(MISSED_BEACONS_NOTIFICATION);
607 IWL_CMD(REPLY_CT_KILL_CONFIG_CMD);
608 IWL_CMD(SENSITIVITY_CMD);
609 IWL_CMD(REPLY_PHY_CALIBRATION_CMD);
610 IWL_CMD(REPLY_RX_PHY_CMD);
611 IWL_CMD(REPLY_RX_MPDU_CMD);
612 IWL_CMD(REPLY_4965_RX);
613 IWL_CMD(REPLY_COMPRESSED_BA);
614 default:
615 return "UNKNOWN";
616
617 }
618}
619
620#define HOST_COMPLETE_TIMEOUT (HZ / 2)
621
622/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800623 * iwl4965_enqueue_hcmd - enqueue a uCode command
Zhu Yib481de92007-09-25 17:54:57 -0700624 * @priv: device private data point
625 * @cmd: a point to the ucode command structure
626 *
627 * The function returns < 0 values to indicate the operation is
628 * failed. On success, it turns the index (> 0) of command in the
629 * command queue.
630 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800631static int iwl4965_enqueue_hcmd(struct iwl4965_priv *priv, struct iwl4965_host_cmd *cmd)
Zhu Yib481de92007-09-25 17:54:57 -0700632{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800633 struct iwl4965_tx_queue *txq = &priv->txq[IWL_CMD_QUEUE_NUM];
634 struct iwl4965_queue *q = &txq->q;
635 struct iwl4965_tfd_frame *tfd;
Zhu Yib481de92007-09-25 17:54:57 -0700636 u32 *control_flags;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800637 struct iwl4965_cmd *out_cmd;
Zhu Yib481de92007-09-25 17:54:57 -0700638 u32 idx;
639 u16 fix_size = (u16)(cmd->len + sizeof(out_cmd->hdr));
640 dma_addr_t phys_addr;
641 int ret;
642 unsigned long flags;
643
644 /* If any of the command structures end up being larger than
645 * the TFD_MAX_PAYLOAD_SIZE, and it sent as a 'small' command then
646 * we will need to increase the size of the TFD entries */
647 BUG_ON((fix_size > TFD_MAX_PAYLOAD_SIZE) &&
648 !(cmd->meta.flags & CMD_SIZE_HUGE));
649
Gregory Greenmanc342a1b2008-02-06 11:20:40 -0800650 if (iwl4965_is_rfkill(priv)) {
651 IWL_DEBUG_INFO("Not sending command - RF KILL");
652 return -EIO;
653 }
654
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800655 if (iwl4965_queue_space(q) < ((cmd->meta.flags & CMD_ASYNC) ? 2 : 1)) {
Zhu Yib481de92007-09-25 17:54:57 -0700656 IWL_ERROR("No space for Tx\n");
657 return -ENOSPC;
658 }
659
660 spin_lock_irqsave(&priv->hcmd_lock, flags);
661
Tomas Winklerfc4b6852007-10-25 17:15:24 +0800662 tfd = &txq->bd[q->write_ptr];
Zhu Yib481de92007-09-25 17:54:57 -0700663 memset(tfd, 0, sizeof(*tfd));
664
665 control_flags = (u32 *) tfd;
666
Tomas Winklerfc4b6852007-10-25 17:15:24 +0800667 idx = get_cmd_index(q, q->write_ptr, cmd->meta.flags & CMD_SIZE_HUGE);
Zhu Yib481de92007-09-25 17:54:57 -0700668 out_cmd = &txq->cmd[idx];
669
670 out_cmd->hdr.cmd = cmd->id;
671 memcpy(&out_cmd->meta, &cmd->meta, sizeof(cmd->meta));
672 memcpy(&out_cmd->cmd.payload, cmd->data, cmd->len);
673
674 /* At this point, the out_cmd now has all of the incoming cmd
675 * information */
676
677 out_cmd->hdr.flags = 0;
678 out_cmd->hdr.sequence = cpu_to_le16(QUEUE_TO_SEQ(IWL_CMD_QUEUE_NUM) |
Tomas Winklerfc4b6852007-10-25 17:15:24 +0800679 INDEX_TO_SEQ(q->write_ptr));
Zhu Yib481de92007-09-25 17:54:57 -0700680 if (out_cmd->meta.flags & CMD_SIZE_HUGE)
681 out_cmd->hdr.sequence |= cpu_to_le16(SEQ_HUGE_FRAME);
682
683 phys_addr = txq->dma_addr_cmd + sizeof(txq->cmd[0]) * idx +
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800684 offsetof(struct iwl4965_cmd, hdr);
685 iwl4965_hw_txq_attach_buf_to_tfd(priv, tfd, phys_addr, fix_size);
Zhu Yib481de92007-09-25 17:54:57 -0700686
687 IWL_DEBUG_HC("Sending command %s (#%x), seq: 0x%04X, "
688 "%d bytes at %d[%d]:%d\n",
689 get_cmd_string(out_cmd->hdr.cmd),
690 out_cmd->hdr.cmd, le16_to_cpu(out_cmd->hdr.sequence),
Tomas Winklerfc4b6852007-10-25 17:15:24 +0800691 fix_size, q->write_ptr, idx, IWL_CMD_QUEUE_NUM);
Zhu Yib481de92007-09-25 17:54:57 -0700692
693 txq->need_update = 1;
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800694
695 /* Set up entry in queue's byte count circular buffer */
Zhu Yib481de92007-09-25 17:54:57 -0700696 ret = iwl4965_tx_queue_update_wr_ptr(priv, txq, 0);
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800697
698 /* Increment and update queue's write index */
Tomas Winklerc54b6792008-03-06 17:36:53 -0800699 q->write_ptr = iwl_queue_inc_wrap(q->write_ptr, q->n_bd);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800700 iwl4965_tx_queue_update_write_ptr(priv, txq);
Zhu Yib481de92007-09-25 17:54:57 -0700701
702 spin_unlock_irqrestore(&priv->hcmd_lock, flags);
703 return ret ? ret : idx;
704}
705
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800706static int iwl4965_send_cmd_async(struct iwl4965_priv *priv, struct iwl4965_host_cmd *cmd)
Zhu Yib481de92007-09-25 17:54:57 -0700707{
708 int ret;
709
710 BUG_ON(!(cmd->meta.flags & CMD_ASYNC));
711
712 /* An asynchronous command can not expect an SKB to be set. */
713 BUG_ON(cmd->meta.flags & CMD_WANT_SKB);
714
715 /* An asynchronous command MUST have a callback. */
716 BUG_ON(!cmd->meta.u.callback);
717
718 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
719 return -EBUSY;
720
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800721 ret = iwl4965_enqueue_hcmd(priv, cmd);
Zhu Yib481de92007-09-25 17:54:57 -0700722 if (ret < 0) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800723 IWL_ERROR("Error sending %s: iwl4965_enqueue_hcmd failed: %d\n",
Zhu Yib481de92007-09-25 17:54:57 -0700724 get_cmd_string(cmd->id), ret);
725 return ret;
726 }
727 return 0;
728}
729
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800730static int iwl4965_send_cmd_sync(struct iwl4965_priv *priv, struct iwl4965_host_cmd *cmd)
Zhu Yib481de92007-09-25 17:54:57 -0700731{
732 int cmd_idx;
733 int ret;
734 static atomic_t entry = ATOMIC_INIT(0); /* reentrance protection */
735
736 BUG_ON(cmd->meta.flags & CMD_ASYNC);
737
738 /* A synchronous command can not have a callback set. */
739 BUG_ON(cmd->meta.u.callback != NULL);
740
741 if (atomic_xchg(&entry, 1)) {
742 IWL_ERROR("Error sending %s: Already sending a host command\n",
743 get_cmd_string(cmd->id));
744 return -EBUSY;
745 }
746
747 set_bit(STATUS_HCMD_ACTIVE, &priv->status);
748
749 if (cmd->meta.flags & CMD_WANT_SKB)
750 cmd->meta.source = &cmd->meta;
751
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800752 cmd_idx = iwl4965_enqueue_hcmd(priv, cmd);
Zhu Yib481de92007-09-25 17:54:57 -0700753 if (cmd_idx < 0) {
754 ret = cmd_idx;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800755 IWL_ERROR("Error sending %s: iwl4965_enqueue_hcmd failed: %d\n",
Zhu Yib481de92007-09-25 17:54:57 -0700756 get_cmd_string(cmd->id), ret);
757 goto out;
758 }
759
760 ret = wait_event_interruptible_timeout(priv->wait_command_queue,
761 !test_bit(STATUS_HCMD_ACTIVE, &priv->status),
762 HOST_COMPLETE_TIMEOUT);
763 if (!ret) {
764 if (test_bit(STATUS_HCMD_ACTIVE, &priv->status)) {
765 IWL_ERROR("Error sending %s: time out after %dms.\n",
766 get_cmd_string(cmd->id),
767 jiffies_to_msecs(HOST_COMPLETE_TIMEOUT));
768
769 clear_bit(STATUS_HCMD_ACTIVE, &priv->status);
770 ret = -ETIMEDOUT;
771 goto cancel;
772 }
773 }
774
775 if (test_bit(STATUS_RF_KILL_HW, &priv->status)) {
776 IWL_DEBUG_INFO("Command %s aborted: RF KILL Switch\n",
777 get_cmd_string(cmd->id));
778 ret = -ECANCELED;
779 goto fail;
780 }
781 if (test_bit(STATUS_FW_ERROR, &priv->status)) {
782 IWL_DEBUG_INFO("Command %s failed: FW Error\n",
783 get_cmd_string(cmd->id));
784 ret = -EIO;
785 goto fail;
786 }
787 if ((cmd->meta.flags & CMD_WANT_SKB) && !cmd->meta.u.skb) {
788 IWL_ERROR("Error: Response NULL in '%s'\n",
789 get_cmd_string(cmd->id));
790 ret = -EIO;
791 goto out;
792 }
793
794 ret = 0;
795 goto out;
796
797cancel:
798 if (cmd->meta.flags & CMD_WANT_SKB) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800799 struct iwl4965_cmd *qcmd;
Zhu Yib481de92007-09-25 17:54:57 -0700800
801 /* Cancel the CMD_WANT_SKB flag for the cmd in the
802 * TX cmd queue. Otherwise in case the cmd comes
803 * in later, it will possibly set an invalid
804 * address (cmd->meta.source). */
805 qcmd = &priv->txq[IWL_CMD_QUEUE_NUM].cmd[cmd_idx];
806 qcmd->meta.flags &= ~CMD_WANT_SKB;
807 }
808fail:
809 if (cmd->meta.u.skb) {
810 dev_kfree_skb_any(cmd->meta.u.skb);
811 cmd->meta.u.skb = NULL;
812 }
813out:
814 atomic_set(&entry, 0);
815 return ret;
816}
817
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800818int iwl4965_send_cmd(struct iwl4965_priv *priv, struct iwl4965_host_cmd *cmd)
Zhu Yib481de92007-09-25 17:54:57 -0700819{
Zhu Yib481de92007-09-25 17:54:57 -0700820 if (cmd->meta.flags & CMD_ASYNC)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800821 return iwl4965_send_cmd_async(priv, cmd);
Zhu Yib481de92007-09-25 17:54:57 -0700822
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800823 return iwl4965_send_cmd_sync(priv, cmd);
Zhu Yib481de92007-09-25 17:54:57 -0700824}
825
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800826int iwl4965_send_cmd_pdu(struct iwl4965_priv *priv, u8 id, u16 len, const void *data)
Zhu Yib481de92007-09-25 17:54:57 -0700827{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800828 struct iwl4965_host_cmd cmd = {
Zhu Yib481de92007-09-25 17:54:57 -0700829 .id = id,
830 .len = len,
831 .data = data,
832 };
833
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800834 return iwl4965_send_cmd_sync(priv, &cmd);
Zhu Yib481de92007-09-25 17:54:57 -0700835}
836
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800837static int __must_check iwl4965_send_cmd_u32(struct iwl4965_priv *priv, u8 id, u32 val)
Zhu Yib481de92007-09-25 17:54:57 -0700838{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800839 struct iwl4965_host_cmd cmd = {
Zhu Yib481de92007-09-25 17:54:57 -0700840 .id = id,
841 .len = sizeof(val),
842 .data = &val,
843 };
844
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800845 return iwl4965_send_cmd_sync(priv, &cmd);
Zhu Yib481de92007-09-25 17:54:57 -0700846}
847
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800848int iwl4965_send_statistics_request(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700849{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800850 return iwl4965_send_cmd_u32(priv, REPLY_STATISTICS_CMD, 0);
Zhu Yib481de92007-09-25 17:54:57 -0700851}
852
853/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800854 * iwl4965_rxon_add_station - add station into station table.
Zhu Yib481de92007-09-25 17:54:57 -0700855 *
856 * there is only one AP station with id= IWL_AP_ID
Ben Cahill9fbab512007-11-29 11:09:47 +0800857 * NOTE: mutex must be held before calling this fnction
858 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800859static int iwl4965_rxon_add_station(struct iwl4965_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -0700860 const u8 *addr, int is_ap)
861{
Zhu Yi556f8db2007-09-27 11:27:33 +0800862 u8 sta_id;
Zhu Yib481de92007-09-25 17:54:57 -0700863
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800864 /* Add station to device's station table */
Ron Rindjunsky67d62032007-11-26 16:14:40 +0200865#ifdef CONFIG_IWL4965_HT
866 struct ieee80211_conf *conf = &priv->hw->conf;
867 struct ieee80211_ht_info *cur_ht_config = &conf->ht_conf;
868
869 if ((is_ap) &&
870 (conf->flags & IEEE80211_CONF_SUPPORT_HT_MODE) &&
871 (priv->iw_mode == IEEE80211_IF_TYPE_STA))
872 sta_id = iwl4965_add_station_flags(priv, addr, is_ap,
873 0, cur_ht_config);
874 else
875#endif /* CONFIG_IWL4965_HT */
876 sta_id = iwl4965_add_station_flags(priv, addr, is_ap,
877 0, NULL);
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800878
879 /* Set up default rate scaling table in device's station table */
Zhu Yib481de92007-09-25 17:54:57 -0700880 iwl4965_add_station(priv, addr, is_ap);
881
Zhu Yi556f8db2007-09-27 11:27:33 +0800882 return sta_id;
Zhu Yib481de92007-09-25 17:54:57 -0700883}
884
885/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800886 * iwl4965_set_rxon_channel - Set the phymode and channel values in staging RXON
Zhu Yib481de92007-09-25 17:54:57 -0700887 * @phymode: MODE_IEEE80211A sets to 5.2GHz; all else set to 2.4GHz
888 * @channel: Any channel valid for the requested phymode
889
890 * In addition to setting the staging RXON, priv->phymode is also set.
891 *
892 * NOTE: Does not commit to the hardware; it sets appropriate bit fields
893 * in the staging RXON flag structure based on the phymode
894 */
Johannes Berg8318d782008-01-24 19:38:38 +0100895static int iwl4965_set_rxon_channel(struct iwl4965_priv *priv,
896 enum ieee80211_band band,
Ben Cahill9fbab512007-11-29 11:09:47 +0800897 u16 channel)
Zhu Yib481de92007-09-25 17:54:57 -0700898{
Johannes Berg8318d782008-01-24 19:38:38 +0100899 if (!iwl4965_get_channel_info(priv, band, channel)) {
Zhu Yib481de92007-09-25 17:54:57 -0700900 IWL_DEBUG_INFO("Could not set channel to %d [%d]\n",
Johannes Berg8318d782008-01-24 19:38:38 +0100901 channel, band);
Zhu Yib481de92007-09-25 17:54:57 -0700902 return -EINVAL;
903 }
904
905 if ((le16_to_cpu(priv->staging_rxon.channel) == channel) &&
Johannes Berg8318d782008-01-24 19:38:38 +0100906 (priv->band == band))
Zhu Yib481de92007-09-25 17:54:57 -0700907 return 0;
908
909 priv->staging_rxon.channel = cpu_to_le16(channel);
Johannes Berg8318d782008-01-24 19:38:38 +0100910 if (band == IEEE80211_BAND_5GHZ)
Zhu Yib481de92007-09-25 17:54:57 -0700911 priv->staging_rxon.flags &= ~RXON_FLG_BAND_24G_MSK;
912 else
913 priv->staging_rxon.flags |= RXON_FLG_BAND_24G_MSK;
914
Johannes Berg8318d782008-01-24 19:38:38 +0100915 priv->band = band;
Zhu Yib481de92007-09-25 17:54:57 -0700916
Johannes Berg8318d782008-01-24 19:38:38 +0100917 IWL_DEBUG_INFO("Staging channel set to %d [%d]\n", channel, band);
Zhu Yib481de92007-09-25 17:54:57 -0700918
919 return 0;
920}
921
922/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800923 * iwl4965_check_rxon_cmd - validate RXON structure is valid
Zhu Yib481de92007-09-25 17:54:57 -0700924 *
925 * NOTE: This is really only useful during development and can eventually
926 * be #ifdef'd out once the driver is stable and folks aren't actively
927 * making changes
928 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800929static int iwl4965_check_rxon_cmd(struct iwl4965_rxon_cmd *rxon)
Zhu Yib481de92007-09-25 17:54:57 -0700930{
931 int error = 0;
932 int counter = 1;
933
934 if (rxon->flags & RXON_FLG_BAND_24G_MSK) {
935 error |= le32_to_cpu(rxon->flags &
936 (RXON_FLG_TGJ_NARROW_BAND_MSK |
937 RXON_FLG_RADAR_DETECT_MSK));
938 if (error)
939 IWL_WARNING("check 24G fields %d | %d\n",
940 counter++, error);
941 } else {
942 error |= (rxon->flags & RXON_FLG_SHORT_SLOT_MSK) ?
943 0 : le32_to_cpu(RXON_FLG_SHORT_SLOT_MSK);
944 if (error)
945 IWL_WARNING("check 52 fields %d | %d\n",
946 counter++, error);
947 error |= le32_to_cpu(rxon->flags & RXON_FLG_CCK_MSK);
948 if (error)
949 IWL_WARNING("check 52 CCK %d | %d\n",
950 counter++, error);
951 }
952 error |= (rxon->node_addr[0] | rxon->bssid_addr[0]) & 0x1;
953 if (error)
954 IWL_WARNING("check mac addr %d | %d\n", counter++, error);
955
956 /* make sure basic rates 6Mbps and 1Mbps are supported */
957 error |= (((rxon->ofdm_basic_rates & IWL_RATE_6M_MASK) == 0) &&
958 ((rxon->cck_basic_rates & IWL_RATE_1M_MASK) == 0));
959 if (error)
960 IWL_WARNING("check basic rate %d | %d\n", counter++, error);
961
962 error |= (le16_to_cpu(rxon->assoc_id) > 2007);
963 if (error)
964 IWL_WARNING("check assoc id %d | %d\n", counter++, error);
965
966 error |= ((rxon->flags & (RXON_FLG_CCK_MSK | RXON_FLG_SHORT_SLOT_MSK))
967 == (RXON_FLG_CCK_MSK | RXON_FLG_SHORT_SLOT_MSK));
968 if (error)
969 IWL_WARNING("check CCK and short slot %d | %d\n",
970 counter++, error);
971
972 error |= ((rxon->flags & (RXON_FLG_CCK_MSK | RXON_FLG_AUTO_DETECT_MSK))
973 == (RXON_FLG_CCK_MSK | RXON_FLG_AUTO_DETECT_MSK));
974 if (error)
975 IWL_WARNING("check CCK & auto detect %d | %d\n",
976 counter++, error);
977
978 error |= ((rxon->flags & (RXON_FLG_AUTO_DETECT_MSK |
979 RXON_FLG_TGG_PROTECT_MSK)) == RXON_FLG_TGG_PROTECT_MSK);
980 if (error)
981 IWL_WARNING("check TGG and auto detect %d | %d\n",
982 counter++, error);
983
984 if (error)
985 IWL_WARNING("Tuning to channel %d\n",
986 le16_to_cpu(rxon->channel));
987
988 if (error) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800989 IWL_ERROR("Not a valid iwl4965_rxon_assoc_cmd field values\n");
Zhu Yib481de92007-09-25 17:54:57 -0700990 return -1;
991 }
992 return 0;
993}
994
995/**
Ben Cahill9fbab512007-11-29 11:09:47 +0800996 * iwl4965_full_rxon_required - check if full RXON (vs RXON_ASSOC) cmd is needed
Ian Schram01ebd062007-10-25 17:15:22 +0800997 * @priv: staging_rxon is compared to active_rxon
Zhu Yib481de92007-09-25 17:54:57 -0700998 *
Ben Cahill9fbab512007-11-29 11:09:47 +0800999 * If the RXON structure is changing enough to require a new tune,
1000 * or is clearing the RXON_FILTER_ASSOC_MSK, then return 1 to indicate that
1001 * a new tune (full RXON command, rather than RXON_ASSOC cmd) is required.
Zhu Yib481de92007-09-25 17:54:57 -07001002 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001003static int iwl4965_full_rxon_required(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001004{
1005
1006 /* These items are only settable from the full RXON command */
1007 if (!(priv->active_rxon.filter_flags & RXON_FILTER_ASSOC_MSK) ||
1008 compare_ether_addr(priv->staging_rxon.bssid_addr,
1009 priv->active_rxon.bssid_addr) ||
1010 compare_ether_addr(priv->staging_rxon.node_addr,
1011 priv->active_rxon.node_addr) ||
1012 compare_ether_addr(priv->staging_rxon.wlap_bssid_addr,
1013 priv->active_rxon.wlap_bssid_addr) ||
1014 (priv->staging_rxon.dev_type != priv->active_rxon.dev_type) ||
1015 (priv->staging_rxon.channel != priv->active_rxon.channel) ||
1016 (priv->staging_rxon.air_propagation !=
1017 priv->active_rxon.air_propagation) ||
1018 (priv->staging_rxon.ofdm_ht_single_stream_basic_rates !=
1019 priv->active_rxon.ofdm_ht_single_stream_basic_rates) ||
1020 (priv->staging_rxon.ofdm_ht_dual_stream_basic_rates !=
1021 priv->active_rxon.ofdm_ht_dual_stream_basic_rates) ||
1022 (priv->staging_rxon.rx_chain != priv->active_rxon.rx_chain) ||
1023 (priv->staging_rxon.assoc_id != priv->active_rxon.assoc_id))
1024 return 1;
1025
1026 /* flags, filter_flags, ofdm_basic_rates, and cck_basic_rates can
1027 * be updated with the RXON_ASSOC command -- however only some
1028 * flag transitions are allowed using RXON_ASSOC */
1029
1030 /* Check if we are not switching bands */
1031 if ((priv->staging_rxon.flags & RXON_FLG_BAND_24G_MSK) !=
1032 (priv->active_rxon.flags & RXON_FLG_BAND_24G_MSK))
1033 return 1;
1034
1035 /* Check if we are switching association toggle */
1036 if ((priv->staging_rxon.filter_flags & RXON_FILTER_ASSOC_MSK) !=
1037 (priv->active_rxon.filter_flags & RXON_FILTER_ASSOC_MSK))
1038 return 1;
1039
1040 return 0;
1041}
1042
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001043static int iwl4965_send_rxon_assoc(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001044{
1045 int rc = 0;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001046 struct iwl4965_rx_packet *res = NULL;
1047 struct iwl4965_rxon_assoc_cmd rxon_assoc;
1048 struct iwl4965_host_cmd cmd = {
Zhu Yib481de92007-09-25 17:54:57 -07001049 .id = REPLY_RXON_ASSOC,
1050 .len = sizeof(rxon_assoc),
1051 .meta.flags = CMD_WANT_SKB,
1052 .data = &rxon_assoc,
1053 };
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001054 const struct iwl4965_rxon_cmd *rxon1 = &priv->staging_rxon;
1055 const struct iwl4965_rxon_cmd *rxon2 = &priv->active_rxon;
Zhu Yib481de92007-09-25 17:54:57 -07001056
1057 if ((rxon1->flags == rxon2->flags) &&
1058 (rxon1->filter_flags == rxon2->filter_flags) &&
1059 (rxon1->cck_basic_rates == rxon2->cck_basic_rates) &&
1060 (rxon1->ofdm_ht_single_stream_basic_rates ==
1061 rxon2->ofdm_ht_single_stream_basic_rates) &&
1062 (rxon1->ofdm_ht_dual_stream_basic_rates ==
1063 rxon2->ofdm_ht_dual_stream_basic_rates) &&
1064 (rxon1->rx_chain == rxon2->rx_chain) &&
1065 (rxon1->ofdm_basic_rates == rxon2->ofdm_basic_rates)) {
1066 IWL_DEBUG_INFO("Using current RXON_ASSOC. Not resending.\n");
1067 return 0;
1068 }
1069
1070 rxon_assoc.flags = priv->staging_rxon.flags;
1071 rxon_assoc.filter_flags = priv->staging_rxon.filter_flags;
1072 rxon_assoc.ofdm_basic_rates = priv->staging_rxon.ofdm_basic_rates;
1073 rxon_assoc.cck_basic_rates = priv->staging_rxon.cck_basic_rates;
1074 rxon_assoc.reserved = 0;
1075 rxon_assoc.ofdm_ht_single_stream_basic_rates =
1076 priv->staging_rxon.ofdm_ht_single_stream_basic_rates;
1077 rxon_assoc.ofdm_ht_dual_stream_basic_rates =
1078 priv->staging_rxon.ofdm_ht_dual_stream_basic_rates;
1079 rxon_assoc.rx_chain_select_flags = priv->staging_rxon.rx_chain;
1080
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001081 rc = iwl4965_send_cmd_sync(priv, &cmd);
Zhu Yib481de92007-09-25 17:54:57 -07001082 if (rc)
1083 return rc;
1084
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001085 res = (struct iwl4965_rx_packet *)cmd.meta.u.skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07001086 if (res->hdr.flags & IWL_CMD_FAILED_MSK) {
1087 IWL_ERROR("Bad return from REPLY_RXON_ASSOC command\n");
1088 rc = -EIO;
1089 }
1090
1091 priv->alloc_rxb_skb--;
1092 dev_kfree_skb_any(cmd.meta.u.skb);
1093
1094 return rc;
1095}
1096
1097/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001098 * iwl4965_commit_rxon - commit staging_rxon to hardware
Zhu Yib481de92007-09-25 17:54:57 -07001099 *
Ian Schram01ebd062007-10-25 17:15:22 +08001100 * The RXON command in staging_rxon is committed to the hardware and
Zhu Yib481de92007-09-25 17:54:57 -07001101 * the active_rxon structure is updated with the new data. This
1102 * function correctly transitions out of the RXON_ASSOC_MSK state if
1103 * a HW tune is required based on the RXON structure changes.
1104 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001105static int iwl4965_commit_rxon(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001106{
1107 /* cast away the const for active_rxon in this function */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001108 struct iwl4965_rxon_cmd *active_rxon = (void *)&priv->active_rxon;
Joe Perches0795af52007-10-03 17:59:30 -07001109 DECLARE_MAC_BUF(mac);
Zhu Yib481de92007-09-25 17:54:57 -07001110 int rc = 0;
1111
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001112 if (!iwl4965_is_alive(priv))
Zhu Yib481de92007-09-25 17:54:57 -07001113 return -1;
1114
1115 /* always get timestamp with Rx frame */
1116 priv->staging_rxon.flags |= RXON_FLG_TSF2HOST_MSK;
1117
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001118 rc = iwl4965_check_rxon_cmd(&priv->staging_rxon);
Zhu Yib481de92007-09-25 17:54:57 -07001119 if (rc) {
1120 IWL_ERROR("Invalid RXON configuration. Not committing.\n");
1121 return -EINVAL;
1122 }
1123
1124 /* If we don't need to send a full RXON, we can use
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001125 * iwl4965_rxon_assoc_cmd which is used to reconfigure filter
Zhu Yib481de92007-09-25 17:54:57 -07001126 * and other flags for the current radio configuration. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001127 if (!iwl4965_full_rxon_required(priv)) {
1128 rc = iwl4965_send_rxon_assoc(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001129 if (rc) {
1130 IWL_ERROR("Error setting RXON_ASSOC "
1131 "configuration (%d).\n", rc);
1132 return rc;
1133 }
1134
1135 memcpy(active_rxon, &priv->staging_rxon, sizeof(*active_rxon));
1136
1137 return 0;
1138 }
1139
1140 /* station table will be cleared */
1141 priv->assoc_station_added = 0;
1142
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08001143#ifdef CONFIG_IWL4965_SENSITIVITY
Zhu Yib481de92007-09-25 17:54:57 -07001144 priv->sensitivity_data.state = IWL_SENS_CALIB_NEED_REINIT;
1145 if (!priv->error_recovering)
1146 priv->start_calib = 0;
1147
1148 iwl4965_init_sensitivity(priv, CMD_ASYNC, 1);
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08001149#endif /* CONFIG_IWL4965_SENSITIVITY */
Zhu Yib481de92007-09-25 17:54:57 -07001150
1151 /* If we are currently associated and the new config requires
1152 * an RXON_ASSOC and the new config wants the associated mask enabled,
1153 * we must clear the associated from the active configuration
1154 * before we apply the new config */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001155 if (iwl4965_is_associated(priv) &&
Zhu Yib481de92007-09-25 17:54:57 -07001156 (priv->staging_rxon.filter_flags & RXON_FILTER_ASSOC_MSK)) {
1157 IWL_DEBUG_INFO("Toggling associated bit on current RXON\n");
1158 active_rxon->filter_flags &= ~RXON_FILTER_ASSOC_MSK;
1159
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001160 rc = iwl4965_send_cmd_pdu(priv, REPLY_RXON,
1161 sizeof(struct iwl4965_rxon_cmd),
Zhu Yib481de92007-09-25 17:54:57 -07001162 &priv->active_rxon);
1163
1164 /* If the mask clearing failed then we set
1165 * active_rxon back to what it was previously */
1166 if (rc) {
1167 active_rxon->filter_flags |= RXON_FILTER_ASSOC_MSK;
1168 IWL_ERROR("Error clearing ASSOC_MSK on current "
1169 "configuration (%d).\n", rc);
1170 return rc;
1171 }
Zhu Yib481de92007-09-25 17:54:57 -07001172 }
1173
1174 IWL_DEBUG_INFO("Sending RXON\n"
1175 "* with%s RXON_FILTER_ASSOC_MSK\n"
1176 "* channel = %d\n"
Joe Perches0795af52007-10-03 17:59:30 -07001177 "* bssid = %s\n",
Zhu Yib481de92007-09-25 17:54:57 -07001178 ((priv->staging_rxon.filter_flags &
1179 RXON_FILTER_ASSOC_MSK) ? "" : "out"),
1180 le16_to_cpu(priv->staging_rxon.channel),
Joe Perches0795af52007-10-03 17:59:30 -07001181 print_mac(mac, priv->staging_rxon.bssid_addr));
Zhu Yib481de92007-09-25 17:54:57 -07001182
1183 /* Apply the new configuration */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001184 rc = iwl4965_send_cmd_pdu(priv, REPLY_RXON,
1185 sizeof(struct iwl4965_rxon_cmd), &priv->staging_rxon);
Zhu Yib481de92007-09-25 17:54:57 -07001186 if (rc) {
1187 IWL_ERROR("Error setting new configuration (%d).\n", rc);
1188 return rc;
1189 }
1190
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001191 iwl4965_clear_stations_table(priv);
Zhu Yi556f8db2007-09-27 11:27:33 +08001192
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08001193#ifdef CONFIG_IWL4965_SENSITIVITY
Zhu Yib481de92007-09-25 17:54:57 -07001194 if (!priv->error_recovering)
1195 priv->start_calib = 0;
1196
1197 priv->sensitivity_data.state = IWL_SENS_CALIB_NEED_REINIT;
1198 iwl4965_init_sensitivity(priv, CMD_ASYNC, 1);
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08001199#endif /* CONFIG_IWL4965_SENSITIVITY */
Zhu Yib481de92007-09-25 17:54:57 -07001200
1201 memcpy(active_rxon, &priv->staging_rxon, sizeof(*active_rxon));
1202
1203 /* If we issue a new RXON command which required a tune then we must
1204 * send a new TXPOWER command or we won't be able to Tx any frames */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001205 rc = iwl4965_hw_reg_send_txpower(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001206 if (rc) {
1207 IWL_ERROR("Error setting Tx power (%d).\n", rc);
1208 return rc;
1209 }
1210
1211 /* Add the broadcast address so we can send broadcast frames */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001212 if (iwl4965_rxon_add_station(priv, iwl4965_broadcast_addr, 0) ==
Zhu Yib481de92007-09-25 17:54:57 -07001213 IWL_INVALID_STATION) {
1214 IWL_ERROR("Error adding BROADCAST address for transmit.\n");
1215 return -EIO;
1216 }
1217
1218 /* If we have set the ASSOC_MSK and we are in BSS mode then
1219 * add the IWL_AP_ID to the station rate table */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001220 if (iwl4965_is_associated(priv) &&
Zhu Yib481de92007-09-25 17:54:57 -07001221 (priv->iw_mode == IEEE80211_IF_TYPE_STA)) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001222 if (iwl4965_rxon_add_station(priv, priv->active_rxon.bssid_addr, 1)
Zhu Yib481de92007-09-25 17:54:57 -07001223 == IWL_INVALID_STATION) {
1224 IWL_ERROR("Error adding AP address for transmit.\n");
1225 return -EIO;
1226 }
1227 priv->assoc_station_added = 1;
1228 }
1229
1230 return 0;
1231}
1232
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001233static int iwl4965_send_bt_config(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001234{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001235 struct iwl4965_bt_cmd bt_cmd = {
Zhu Yib481de92007-09-25 17:54:57 -07001236 .flags = 3,
1237 .lead_time = 0xAA,
1238 .max_kill = 1,
1239 .kill_ack_mask = 0,
1240 .kill_cts_mask = 0,
1241 };
1242
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001243 return iwl4965_send_cmd_pdu(priv, REPLY_BT_CONFIG,
1244 sizeof(struct iwl4965_bt_cmd), &bt_cmd);
Zhu Yib481de92007-09-25 17:54:57 -07001245}
1246
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001247static int iwl4965_send_scan_abort(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001248{
1249 int rc = 0;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001250 struct iwl4965_rx_packet *res;
1251 struct iwl4965_host_cmd cmd = {
Zhu Yib481de92007-09-25 17:54:57 -07001252 .id = REPLY_SCAN_ABORT_CMD,
1253 .meta.flags = CMD_WANT_SKB,
1254 };
1255
1256 /* If there isn't a scan actively going on in the hardware
1257 * then we are in between scan bands and not actually
1258 * actively scanning, so don't send the abort command */
1259 if (!test_bit(STATUS_SCAN_HW, &priv->status)) {
1260 clear_bit(STATUS_SCAN_ABORTING, &priv->status);
1261 return 0;
1262 }
1263
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001264 rc = iwl4965_send_cmd_sync(priv, &cmd);
Zhu Yib481de92007-09-25 17:54:57 -07001265 if (rc) {
1266 clear_bit(STATUS_SCAN_ABORTING, &priv->status);
1267 return rc;
1268 }
1269
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001270 res = (struct iwl4965_rx_packet *)cmd.meta.u.skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07001271 if (res->u.status != CAN_ABORT_STATUS) {
1272 /* The scan abort will return 1 for success or
1273 * 2 for "failure". A failure condition can be
1274 * due to simply not being in an active scan which
1275 * can occur if we send the scan abort before we
1276 * the microcode has notified us that a scan is
1277 * completed. */
1278 IWL_DEBUG_INFO("SCAN_ABORT returned %d.\n", res->u.status);
1279 clear_bit(STATUS_SCAN_ABORTING, &priv->status);
1280 clear_bit(STATUS_SCAN_HW, &priv->status);
1281 }
1282
1283 dev_kfree_skb_any(cmd.meta.u.skb);
1284
1285 return rc;
1286}
1287
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001288static int iwl4965_card_state_sync_callback(struct iwl4965_priv *priv,
1289 struct iwl4965_cmd *cmd,
Zhu Yib481de92007-09-25 17:54:57 -07001290 struct sk_buff *skb)
1291{
1292 return 1;
1293}
1294
1295/*
1296 * CARD_STATE_CMD
1297 *
Ben Cahill9fbab512007-11-29 11:09:47 +08001298 * Use: Sets the device's internal card state to enable, disable, or halt
Zhu Yib481de92007-09-25 17:54:57 -07001299 *
1300 * When in the 'enable' state the card operates as normal.
1301 * When in the 'disable' state, the card enters into a low power mode.
1302 * When in the 'halt' state, the card is shut down and must be fully
1303 * restarted to come back on.
1304 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001305static int iwl4965_send_card_state(struct iwl4965_priv *priv, u32 flags, u8 meta_flag)
Zhu Yib481de92007-09-25 17:54:57 -07001306{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001307 struct iwl4965_host_cmd cmd = {
Zhu Yib481de92007-09-25 17:54:57 -07001308 .id = REPLY_CARD_STATE_CMD,
1309 .len = sizeof(u32),
1310 .data = &flags,
1311 .meta.flags = meta_flag,
1312 };
1313
1314 if (meta_flag & CMD_ASYNC)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001315 cmd.meta.u.callback = iwl4965_card_state_sync_callback;
Zhu Yib481de92007-09-25 17:54:57 -07001316
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001317 return iwl4965_send_cmd(priv, &cmd);
Zhu Yib481de92007-09-25 17:54:57 -07001318}
1319
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001320static int iwl4965_add_sta_sync_callback(struct iwl4965_priv *priv,
1321 struct iwl4965_cmd *cmd, struct sk_buff *skb)
Zhu Yib481de92007-09-25 17:54:57 -07001322{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001323 struct iwl4965_rx_packet *res = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07001324
1325 if (!skb) {
1326 IWL_ERROR("Error: Response NULL in REPLY_ADD_STA.\n");
1327 return 1;
1328 }
1329
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001330 res = (struct iwl4965_rx_packet *)skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07001331 if (res->hdr.flags & IWL_CMD_FAILED_MSK) {
1332 IWL_ERROR("Bad return from REPLY_ADD_STA (0x%08X)\n",
1333 res->hdr.flags);
1334 return 1;
1335 }
1336
1337 switch (res->u.add_sta.status) {
1338 case ADD_STA_SUCCESS_MSK:
1339 break;
1340 default:
1341 break;
1342 }
1343
1344 /* We didn't cache the SKB; let the caller free it */
1345 return 1;
1346}
1347
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001348int iwl4965_send_add_station(struct iwl4965_priv *priv,
1349 struct iwl4965_addsta_cmd *sta, u8 flags)
Zhu Yib481de92007-09-25 17:54:57 -07001350{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001351 struct iwl4965_rx_packet *res = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07001352 int rc = 0;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001353 struct iwl4965_host_cmd cmd = {
Zhu Yib481de92007-09-25 17:54:57 -07001354 .id = REPLY_ADD_STA,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001355 .len = sizeof(struct iwl4965_addsta_cmd),
Zhu Yib481de92007-09-25 17:54:57 -07001356 .meta.flags = flags,
1357 .data = sta,
1358 };
1359
1360 if (flags & CMD_ASYNC)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001361 cmd.meta.u.callback = iwl4965_add_sta_sync_callback;
Zhu Yib481de92007-09-25 17:54:57 -07001362 else
1363 cmd.meta.flags |= CMD_WANT_SKB;
1364
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001365 rc = iwl4965_send_cmd(priv, &cmd);
Zhu Yib481de92007-09-25 17:54:57 -07001366
1367 if (rc || (flags & CMD_ASYNC))
1368 return rc;
1369
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001370 res = (struct iwl4965_rx_packet *)cmd.meta.u.skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07001371 if (res->hdr.flags & IWL_CMD_FAILED_MSK) {
1372 IWL_ERROR("Bad return from REPLY_ADD_STA (0x%08X)\n",
1373 res->hdr.flags);
1374 rc = -EIO;
1375 }
1376
1377 if (rc == 0) {
1378 switch (res->u.add_sta.status) {
1379 case ADD_STA_SUCCESS_MSK:
1380 IWL_DEBUG_INFO("REPLY_ADD_STA PASSED\n");
1381 break;
1382 default:
1383 rc = -EIO;
1384 IWL_WARNING("REPLY_ADD_STA failed\n");
1385 break;
1386 }
1387 }
1388
1389 priv->alloc_rxb_skb--;
1390 dev_kfree_skb_any(cmd.meta.u.skb);
1391
1392 return rc;
1393}
1394
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001395static int iwl4965_update_sta_key_info(struct iwl4965_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07001396 struct ieee80211_key_conf *keyconf,
1397 u8 sta_id)
1398{
1399 unsigned long flags;
1400 __le16 key_flags = 0;
1401
1402 switch (keyconf->alg) {
1403 case ALG_CCMP:
1404 key_flags |= STA_KEY_FLG_CCMP;
1405 key_flags |= cpu_to_le16(
1406 keyconf->keyidx << STA_KEY_FLG_KEYID_POS);
1407 key_flags &= ~STA_KEY_FLG_INVALID;
1408 break;
1409 case ALG_TKIP:
1410 case ALG_WEP:
Zhu Yib481de92007-09-25 17:54:57 -07001411 default:
1412 return -EINVAL;
1413 }
1414 spin_lock_irqsave(&priv->sta_lock, flags);
1415 priv->stations[sta_id].keyinfo.alg = keyconf->alg;
1416 priv->stations[sta_id].keyinfo.keylen = keyconf->keylen;
1417 memcpy(priv->stations[sta_id].keyinfo.key, keyconf->key,
1418 keyconf->keylen);
1419
1420 memcpy(priv->stations[sta_id].sta.key.key, keyconf->key,
1421 keyconf->keylen);
1422 priv->stations[sta_id].sta.key.key_flags = key_flags;
1423 priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
1424 priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
1425
1426 spin_unlock_irqrestore(&priv->sta_lock, flags);
1427
1428 IWL_DEBUG_INFO("hwcrypto: modify ucode station key info\n");
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001429 iwl4965_send_add_station(priv, &priv->stations[sta_id].sta, 0);
Zhu Yib481de92007-09-25 17:54:57 -07001430 return 0;
1431}
1432
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001433static int iwl4965_clear_sta_key_info(struct iwl4965_priv *priv, u8 sta_id)
Zhu Yib481de92007-09-25 17:54:57 -07001434{
1435 unsigned long flags;
1436
1437 spin_lock_irqsave(&priv->sta_lock, flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001438 memset(&priv->stations[sta_id].keyinfo, 0, sizeof(struct iwl4965_hw_key));
1439 memset(&priv->stations[sta_id].sta.key, 0, sizeof(struct iwl4965_keyinfo));
Zhu Yib481de92007-09-25 17:54:57 -07001440 priv->stations[sta_id].sta.key.key_flags = STA_KEY_FLG_NO_ENC;
1441 priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
1442 priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
1443 spin_unlock_irqrestore(&priv->sta_lock, flags);
1444
1445 IWL_DEBUG_INFO("hwcrypto: clear ucode station key info\n");
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001446 iwl4965_send_add_station(priv, &priv->stations[sta_id].sta, 0);
Zhu Yib481de92007-09-25 17:54:57 -07001447 return 0;
1448}
1449
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001450static void iwl4965_clear_free_frames(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001451{
1452 struct list_head *element;
1453
1454 IWL_DEBUG_INFO("%d frames on pre-allocated heap on clear.\n",
1455 priv->frames_count);
1456
1457 while (!list_empty(&priv->free_frames)) {
1458 element = priv->free_frames.next;
1459 list_del(element);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001460 kfree(list_entry(element, struct iwl4965_frame, list));
Zhu Yib481de92007-09-25 17:54:57 -07001461 priv->frames_count--;
1462 }
1463
1464 if (priv->frames_count) {
1465 IWL_WARNING("%d frames still in use. Did we lose one?\n",
1466 priv->frames_count);
1467 priv->frames_count = 0;
1468 }
1469}
1470
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001471static struct iwl4965_frame *iwl4965_get_free_frame(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001472{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001473 struct iwl4965_frame *frame;
Zhu Yib481de92007-09-25 17:54:57 -07001474 struct list_head *element;
1475 if (list_empty(&priv->free_frames)) {
1476 frame = kzalloc(sizeof(*frame), GFP_KERNEL);
1477 if (!frame) {
1478 IWL_ERROR("Could not allocate frame!\n");
1479 return NULL;
1480 }
1481
1482 priv->frames_count++;
1483 return frame;
1484 }
1485
1486 element = priv->free_frames.next;
1487 list_del(element);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001488 return list_entry(element, struct iwl4965_frame, list);
Zhu Yib481de92007-09-25 17:54:57 -07001489}
1490
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001491static void iwl4965_free_frame(struct iwl4965_priv *priv, struct iwl4965_frame *frame)
Zhu Yib481de92007-09-25 17:54:57 -07001492{
1493 memset(frame, 0, sizeof(*frame));
1494 list_add(&frame->list, &priv->free_frames);
1495}
1496
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001497unsigned int iwl4965_fill_beacon_frame(struct iwl4965_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07001498 struct ieee80211_hdr *hdr,
1499 const u8 *dest, int left)
1500{
1501
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001502 if (!iwl4965_is_associated(priv) || !priv->ibss_beacon ||
Zhu Yib481de92007-09-25 17:54:57 -07001503 ((priv->iw_mode != IEEE80211_IF_TYPE_IBSS) &&
1504 (priv->iw_mode != IEEE80211_IF_TYPE_AP)))
1505 return 0;
1506
1507 if (priv->ibss_beacon->len > left)
1508 return 0;
1509
1510 memcpy(hdr, priv->ibss_beacon->data, priv->ibss_beacon->len);
1511
1512 return priv->ibss_beacon->len;
1513}
1514
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001515static u8 iwl4965_rate_get_lowest_plcp(int rate_mask)
Zhu Yib481de92007-09-25 17:54:57 -07001516{
1517 u8 i;
1518
1519 for (i = IWL_RATE_1M_INDEX; i != IWL_RATE_INVALID;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001520 i = iwl4965_rates[i].next_ieee) {
Zhu Yib481de92007-09-25 17:54:57 -07001521 if (rate_mask & (1 << i))
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001522 return iwl4965_rates[i].plcp;
Zhu Yib481de92007-09-25 17:54:57 -07001523 }
1524
1525 return IWL_RATE_INVALID;
1526}
1527
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001528static int iwl4965_send_beacon_cmd(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001529{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001530 struct iwl4965_frame *frame;
Zhu Yib481de92007-09-25 17:54:57 -07001531 unsigned int frame_size;
1532 int rc;
1533 u8 rate;
1534
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001535 frame = iwl4965_get_free_frame(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001536
1537 if (!frame) {
1538 IWL_ERROR("Could not obtain free frame buffer for beacon "
1539 "command.\n");
1540 return -ENOMEM;
1541 }
1542
1543 if (!(priv->staging_rxon.flags & RXON_FLG_BAND_24G_MSK)) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001544 rate = iwl4965_rate_get_lowest_plcp(priv->active_rate_basic &
Zhu Yib481de92007-09-25 17:54:57 -07001545 0xFF0);
1546 if (rate == IWL_INVALID_RATE)
1547 rate = IWL_RATE_6M_PLCP;
1548 } else {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001549 rate = iwl4965_rate_get_lowest_plcp(priv->active_rate_basic & 0xF);
Zhu Yib481de92007-09-25 17:54:57 -07001550 if (rate == IWL_INVALID_RATE)
1551 rate = IWL_RATE_1M_PLCP;
1552 }
1553
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001554 frame_size = iwl4965_hw_get_beacon_cmd(priv, frame, rate);
Zhu Yib481de92007-09-25 17:54:57 -07001555
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001556 rc = iwl4965_send_cmd_pdu(priv, REPLY_TX_BEACON, frame_size,
Zhu Yib481de92007-09-25 17:54:57 -07001557 &frame->u.cmd[0]);
1558
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001559 iwl4965_free_frame(priv, frame);
Zhu Yib481de92007-09-25 17:54:57 -07001560
1561 return rc;
1562}
1563
1564/******************************************************************************
1565 *
Zhu Yib481de92007-09-25 17:54:57 -07001566 * Misc. internal state and helper functions
1567 *
1568 ******************************************************************************/
Zhu Yib481de92007-09-25 17:54:57 -07001569
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001570static void iwl4965_unset_hw_setting(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001571{
1572 if (priv->hw_setting.shared_virt)
1573 pci_free_consistent(priv->pci_dev,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001574 sizeof(struct iwl4965_shared),
Zhu Yib481de92007-09-25 17:54:57 -07001575 priv->hw_setting.shared_virt,
1576 priv->hw_setting.shared_phys);
1577}
1578
1579/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001580 * iwl4965_supported_rate_to_ie - fill in the supported rate in IE field
Zhu Yib481de92007-09-25 17:54:57 -07001581 *
1582 * return : set the bit for each supported rate insert in ie
1583 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001584static u16 iwl4965_supported_rate_to_ie(u8 *ie, u16 supported_rate,
Tomas Winklerc7c46672007-10-18 02:04:15 +02001585 u16 basic_rate, int *left)
Zhu Yib481de92007-09-25 17:54:57 -07001586{
1587 u16 ret_rates = 0, bit;
1588 int i;
Tomas Winklerc7c46672007-10-18 02:04:15 +02001589 u8 *cnt = ie;
1590 u8 *rates = ie + 1;
Zhu Yib481de92007-09-25 17:54:57 -07001591
1592 for (bit = 1, i = 0; i < IWL_RATE_COUNT; i++, bit <<= 1) {
1593 if (bit & supported_rate) {
1594 ret_rates |= bit;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001595 rates[*cnt] = iwl4965_rates[i].ieee |
Tomas Winklerc7c46672007-10-18 02:04:15 +02001596 ((bit & basic_rate) ? 0x80 : 0x00);
1597 (*cnt)++;
1598 (*left)--;
1599 if ((*left <= 0) ||
1600 (*cnt >= IWL_SUPPORTED_RATES_IE_LEN))
Zhu Yib481de92007-09-25 17:54:57 -07001601 break;
1602 }
1603 }
1604
1605 return ret_rates;
1606}
1607
Zhu Yib481de92007-09-25 17:54:57 -07001608/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001609 * iwl4965_fill_probe_req - fill in all required fields and IE for probe request
Zhu Yib481de92007-09-25 17:54:57 -07001610 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001611static u16 iwl4965_fill_probe_req(struct iwl4965_priv *priv,
Tomas Winkler78330fd2008-02-06 02:37:18 +02001612 enum ieee80211_band band,
1613 struct ieee80211_mgmt *frame,
1614 int left, int is_direct)
Zhu Yib481de92007-09-25 17:54:57 -07001615{
1616 int len = 0;
1617 u8 *pos = NULL;
mabbasbee488d2007-10-25 17:15:42 +08001618 u16 active_rates, ret_rates, cck_rates, active_rate_basic;
Ron Rindjunsky8fb880322007-11-26 16:14:38 +02001619#ifdef CONFIG_IWL4965_HT
Tomas Winkler78330fd2008-02-06 02:37:18 +02001620 const struct ieee80211_supported_band *sband =
1621 iwl4965_get_hw_mode(priv, band);
Ron Rindjunsky8fb880322007-11-26 16:14:38 +02001622#endif /* CONFIG_IWL4965_HT */
Zhu Yib481de92007-09-25 17:54:57 -07001623
1624 /* Make sure there is enough space for the probe request,
1625 * two mandatory IEs and the data */
1626 left -= 24;
1627 if (left < 0)
1628 return 0;
1629 len += 24;
1630
1631 frame->frame_control = cpu_to_le16(IEEE80211_STYPE_PROBE_REQ);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001632 memcpy(frame->da, iwl4965_broadcast_addr, ETH_ALEN);
Zhu Yib481de92007-09-25 17:54:57 -07001633 memcpy(frame->sa, priv->mac_addr, ETH_ALEN);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001634 memcpy(frame->bssid, iwl4965_broadcast_addr, ETH_ALEN);
Zhu Yib481de92007-09-25 17:54:57 -07001635 frame->seq_ctrl = 0;
1636
1637 /* fill in our indirect SSID IE */
1638 /* ...next IE... */
1639
1640 left -= 2;
1641 if (left < 0)
1642 return 0;
1643 len += 2;
1644 pos = &(frame->u.probe_req.variable[0]);
1645 *pos++ = WLAN_EID_SSID;
1646 *pos++ = 0;
1647
1648 /* fill in our direct SSID IE... */
1649 if (is_direct) {
1650 /* ...next IE... */
1651 left -= 2 + priv->essid_len;
1652 if (left < 0)
1653 return 0;
1654 /* ... fill it in... */
1655 *pos++ = WLAN_EID_SSID;
1656 *pos++ = priv->essid_len;
1657 memcpy(pos, priv->essid, priv->essid_len);
1658 pos += priv->essid_len;
1659 len += 2 + priv->essid_len;
1660 }
1661
1662 /* fill in supported rate */
1663 /* ...next IE... */
1664 left -= 2;
1665 if (left < 0)
1666 return 0;
Tomas Winklerc7c46672007-10-18 02:04:15 +02001667
Zhu Yib481de92007-09-25 17:54:57 -07001668 /* ... fill it in... */
1669 *pos++ = WLAN_EID_SUPP_RATES;
1670 *pos = 0;
Tomas Winklerc7c46672007-10-18 02:04:15 +02001671
mabbasbee488d2007-10-25 17:15:42 +08001672 /* exclude 60M rate */
1673 active_rates = priv->rates_mask;
1674 active_rates &= ~IWL_RATE_60M_MASK;
1675
1676 active_rate_basic = active_rates & IWL_BASIC_RATES_MASK;
Zhu Yib481de92007-09-25 17:54:57 -07001677
Tomas Winklerc7c46672007-10-18 02:04:15 +02001678 cck_rates = IWL_CCK_RATES_MASK & active_rates;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001679 ret_rates = iwl4965_supported_rate_to_ie(pos, cck_rates,
mabbasbee488d2007-10-25 17:15:42 +08001680 active_rate_basic, &left);
Tomas Winklerc7c46672007-10-18 02:04:15 +02001681 active_rates &= ~ret_rates;
1682
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001683 ret_rates = iwl4965_supported_rate_to_ie(pos, active_rates,
mabbasbee488d2007-10-25 17:15:42 +08001684 active_rate_basic, &left);
Tomas Winklerc7c46672007-10-18 02:04:15 +02001685 active_rates &= ~ret_rates;
1686
Zhu Yib481de92007-09-25 17:54:57 -07001687 len += 2 + *pos;
1688 pos += (*pos) + 1;
Tomas Winklerc7c46672007-10-18 02:04:15 +02001689 if (active_rates == 0)
Zhu Yib481de92007-09-25 17:54:57 -07001690 goto fill_end;
1691
1692 /* fill in supported extended rate */
1693 /* ...next IE... */
1694 left -= 2;
1695 if (left < 0)
1696 return 0;
1697 /* ... fill it in... */
1698 *pos++ = WLAN_EID_EXT_SUPP_RATES;
1699 *pos = 0;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001700 iwl4965_supported_rate_to_ie(pos, active_rates,
mabbasbee488d2007-10-25 17:15:42 +08001701 active_rate_basic, &left);
Zhu Yib481de92007-09-25 17:54:57 -07001702 if (*pos > 0)
1703 len += 2 + *pos;
1704
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08001705#ifdef CONFIG_IWL4965_HT
Tomas Winkler78330fd2008-02-06 02:37:18 +02001706 if (sband && sband->ht_info.ht_supported) {
1707 struct ieee80211_ht_cap *ht_cap;
Zhu Yib481de92007-09-25 17:54:57 -07001708 pos += (*pos) + 1;
1709 *pos++ = WLAN_EID_HT_CAPABILITY;
Ron Rindjunsky8fb880322007-11-26 16:14:38 +02001710 *pos++ = sizeof(struct ieee80211_ht_cap);
Tomas Winkler78330fd2008-02-06 02:37:18 +02001711 ht_cap = (struct ieee80211_ht_cap *)pos;
1712 ht_cap->cap_info = cpu_to_le16(sband->ht_info.cap);
1713 memcpy(ht_cap->supp_mcs_set, sband->ht_info.supp_mcs_set, 16);
1714 ht_cap->ampdu_params_info =(sband->ht_info.ampdu_factor &
1715 IEEE80211_HT_CAP_AMPDU_FACTOR) |
1716 ((sband->ht_info.ampdu_density << 2) &
1717 IEEE80211_HT_CAP_AMPDU_DENSITY);
Ron Rindjunsky8fb880322007-11-26 16:14:38 +02001718 len += 2 + sizeof(struct ieee80211_ht_cap);
Zhu Yib481de92007-09-25 17:54:57 -07001719 }
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08001720#endif /*CONFIG_IWL4965_HT */
Zhu Yib481de92007-09-25 17:54:57 -07001721
1722 fill_end:
1723 return (u16)len;
1724}
1725
1726/*
1727 * QoS support
1728*/
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001729static int iwl4965_send_qos_params_command(struct iwl4965_priv *priv,
1730 struct iwl4965_qosparam_cmd *qos)
Zhu Yib481de92007-09-25 17:54:57 -07001731{
1732
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001733 return iwl4965_send_cmd_pdu(priv, REPLY_QOS_PARAM,
1734 sizeof(struct iwl4965_qosparam_cmd), qos);
Zhu Yib481de92007-09-25 17:54:57 -07001735}
1736
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001737static void iwl4965_reset_qos(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001738{
1739 u16 cw_min = 15;
1740 u16 cw_max = 1023;
1741 u8 aifs = 2;
1742 u8 is_legacy = 0;
1743 unsigned long flags;
1744 int i;
1745
1746 spin_lock_irqsave(&priv->lock, flags);
1747 priv->qos_data.qos_active = 0;
1748
1749 if (priv->iw_mode == IEEE80211_IF_TYPE_IBSS) {
1750 if (priv->qos_data.qos_enable)
1751 priv->qos_data.qos_active = 1;
1752 if (!(priv->active_rate & 0xfff0)) {
1753 cw_min = 31;
1754 is_legacy = 1;
1755 }
1756 } else if (priv->iw_mode == IEEE80211_IF_TYPE_AP) {
1757 if (priv->qos_data.qos_enable)
1758 priv->qos_data.qos_active = 1;
1759 } else if (!(priv->staging_rxon.flags & RXON_FLG_SHORT_SLOT_MSK)) {
1760 cw_min = 31;
1761 is_legacy = 1;
1762 }
1763
1764 if (priv->qos_data.qos_active)
1765 aifs = 3;
1766
1767 priv->qos_data.def_qos_parm.ac[0].cw_min = cpu_to_le16(cw_min);
1768 priv->qos_data.def_qos_parm.ac[0].cw_max = cpu_to_le16(cw_max);
1769 priv->qos_data.def_qos_parm.ac[0].aifsn = aifs;
1770 priv->qos_data.def_qos_parm.ac[0].edca_txop = 0;
1771 priv->qos_data.def_qos_parm.ac[0].reserved1 = 0;
1772
1773 if (priv->qos_data.qos_active) {
1774 i = 1;
1775 priv->qos_data.def_qos_parm.ac[i].cw_min = cpu_to_le16(cw_min);
1776 priv->qos_data.def_qos_parm.ac[i].cw_max = cpu_to_le16(cw_max);
1777 priv->qos_data.def_qos_parm.ac[i].aifsn = 7;
1778 priv->qos_data.def_qos_parm.ac[i].edca_txop = 0;
1779 priv->qos_data.def_qos_parm.ac[i].reserved1 = 0;
1780
1781 i = 2;
1782 priv->qos_data.def_qos_parm.ac[i].cw_min =
1783 cpu_to_le16((cw_min + 1) / 2 - 1);
1784 priv->qos_data.def_qos_parm.ac[i].cw_max =
1785 cpu_to_le16(cw_max);
1786 priv->qos_data.def_qos_parm.ac[i].aifsn = 2;
1787 if (is_legacy)
1788 priv->qos_data.def_qos_parm.ac[i].edca_txop =
1789 cpu_to_le16(6016);
1790 else
1791 priv->qos_data.def_qos_parm.ac[i].edca_txop =
1792 cpu_to_le16(3008);
1793 priv->qos_data.def_qos_parm.ac[i].reserved1 = 0;
1794
1795 i = 3;
1796 priv->qos_data.def_qos_parm.ac[i].cw_min =
1797 cpu_to_le16((cw_min + 1) / 4 - 1);
1798 priv->qos_data.def_qos_parm.ac[i].cw_max =
1799 cpu_to_le16((cw_max + 1) / 2 - 1);
1800 priv->qos_data.def_qos_parm.ac[i].aifsn = 2;
1801 priv->qos_data.def_qos_parm.ac[i].reserved1 = 0;
1802 if (is_legacy)
1803 priv->qos_data.def_qos_parm.ac[i].edca_txop =
1804 cpu_to_le16(3264);
1805 else
1806 priv->qos_data.def_qos_parm.ac[i].edca_txop =
1807 cpu_to_le16(1504);
1808 } else {
1809 for (i = 1; i < 4; i++) {
1810 priv->qos_data.def_qos_parm.ac[i].cw_min =
1811 cpu_to_le16(cw_min);
1812 priv->qos_data.def_qos_parm.ac[i].cw_max =
1813 cpu_to_le16(cw_max);
1814 priv->qos_data.def_qos_parm.ac[i].aifsn = aifs;
1815 priv->qos_data.def_qos_parm.ac[i].edca_txop = 0;
1816 priv->qos_data.def_qos_parm.ac[i].reserved1 = 0;
1817 }
1818 }
1819 IWL_DEBUG_QOS("set QoS to default \n");
1820
1821 spin_unlock_irqrestore(&priv->lock, flags);
1822}
1823
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001824static void iwl4965_activate_qos(struct iwl4965_priv *priv, u8 force)
Zhu Yib481de92007-09-25 17:54:57 -07001825{
1826 unsigned long flags;
1827
Zhu Yib481de92007-09-25 17:54:57 -07001828 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
1829 return;
1830
1831 if (!priv->qos_data.qos_enable)
1832 return;
1833
1834 spin_lock_irqsave(&priv->lock, flags);
1835 priv->qos_data.def_qos_parm.qos_flags = 0;
1836
1837 if (priv->qos_data.qos_cap.q_AP.queue_request &&
1838 !priv->qos_data.qos_cap.q_AP.txop_request)
1839 priv->qos_data.def_qos_parm.qos_flags |=
1840 QOS_PARAM_FLG_TXOP_TYPE_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07001841 if (priv->qos_data.qos_active)
1842 priv->qos_data.def_qos_parm.qos_flags |=
1843 QOS_PARAM_FLG_UPDATE_EDCA_MSK;
1844
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08001845#ifdef CONFIG_IWL4965_HT
Ron Rindjunskyfd105e72007-11-26 16:14:39 +02001846 if (priv->current_ht_config.is_ht)
Tomas Winklerf1f1f5c2007-10-25 17:15:26 +08001847 priv->qos_data.def_qos_parm.qos_flags |= QOS_PARAM_FLG_TGN_MSK;
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08001848#endif /* CONFIG_IWL4965_HT */
Tomas Winklerf1f1f5c2007-10-25 17:15:26 +08001849
Zhu Yib481de92007-09-25 17:54:57 -07001850 spin_unlock_irqrestore(&priv->lock, flags);
1851
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001852 if (force || iwl4965_is_associated(priv)) {
Tomas Winklerf1f1f5c2007-10-25 17:15:26 +08001853 IWL_DEBUG_QOS("send QoS cmd with Qos active=%d FLAGS=0x%X\n",
1854 priv->qos_data.qos_active,
1855 priv->qos_data.def_qos_parm.qos_flags);
Zhu Yib481de92007-09-25 17:54:57 -07001856
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001857 iwl4965_send_qos_params_command(priv,
Zhu Yib481de92007-09-25 17:54:57 -07001858 &(priv->qos_data.def_qos_parm));
1859 }
1860}
1861
Zhu Yib481de92007-09-25 17:54:57 -07001862/*
1863 * Power management (not Tx power!) functions
1864 */
1865#define MSEC_TO_USEC 1024
1866
1867#define NOSLP __constant_cpu_to_le16(0), 0, 0
1868#define SLP IWL_POWER_DRIVER_ALLOW_SLEEP_MSK, 0, 0
1869#define SLP_TIMEOUT(T) __constant_cpu_to_le32((T) * MSEC_TO_USEC)
1870#define SLP_VEC(X0, X1, X2, X3, X4) {__constant_cpu_to_le32(X0), \
1871 __constant_cpu_to_le32(X1), \
1872 __constant_cpu_to_le32(X2), \
1873 __constant_cpu_to_le32(X3), \
1874 __constant_cpu_to_le32(X4)}
1875
1876
1877/* default power management (not Tx power) table values */
1878/* for tim 0-10 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001879static struct iwl4965_power_vec_entry range_0[IWL_POWER_AC] = {
Zhu Yib481de92007-09-25 17:54:57 -07001880 {{NOSLP, SLP_TIMEOUT(0), SLP_TIMEOUT(0), SLP_VEC(0, 0, 0, 0, 0)}, 0},
1881 {{SLP, SLP_TIMEOUT(200), SLP_TIMEOUT(500), SLP_VEC(1, 2, 3, 4, 4)}, 0},
1882 {{SLP, SLP_TIMEOUT(200), SLP_TIMEOUT(300), SLP_VEC(2, 4, 6, 7, 7)}, 0},
1883 {{SLP, SLP_TIMEOUT(50), SLP_TIMEOUT(100), SLP_VEC(2, 6, 9, 9, 10)}, 0},
1884 {{SLP, SLP_TIMEOUT(50), SLP_TIMEOUT(25), SLP_VEC(2, 7, 9, 9, 10)}, 1},
1885 {{SLP, SLP_TIMEOUT(25), SLP_TIMEOUT(25), SLP_VEC(4, 7, 10, 10, 10)}, 1}
1886};
1887
1888/* for tim > 10 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001889static struct iwl4965_power_vec_entry range_1[IWL_POWER_AC] = {
Zhu Yib481de92007-09-25 17:54:57 -07001890 {{NOSLP, SLP_TIMEOUT(0), SLP_TIMEOUT(0), SLP_VEC(0, 0, 0, 0, 0)}, 0},
1891 {{SLP, SLP_TIMEOUT(200), SLP_TIMEOUT(500),
1892 SLP_VEC(1, 2, 3, 4, 0xFF)}, 0},
1893 {{SLP, SLP_TIMEOUT(200), SLP_TIMEOUT(300),
1894 SLP_VEC(2, 4, 6, 7, 0xFF)}, 0},
1895 {{SLP, SLP_TIMEOUT(50), SLP_TIMEOUT(100),
1896 SLP_VEC(2, 6, 9, 9, 0xFF)}, 0},
1897 {{SLP, SLP_TIMEOUT(50), SLP_TIMEOUT(25), SLP_VEC(2, 7, 9, 9, 0xFF)}, 0},
1898 {{SLP, SLP_TIMEOUT(25), SLP_TIMEOUT(25),
1899 SLP_VEC(4, 7, 10, 10, 0xFF)}, 0}
1900};
1901
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001902int iwl4965_power_init_handle(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001903{
1904 int rc = 0, i;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001905 struct iwl4965_power_mgr *pow_data;
1906 int size = sizeof(struct iwl4965_power_vec_entry) * IWL_POWER_AC;
Zhu Yib481de92007-09-25 17:54:57 -07001907 u16 pci_pm;
1908
1909 IWL_DEBUG_POWER("Initialize power \n");
1910
1911 pow_data = &(priv->power_data);
1912
1913 memset(pow_data, 0, sizeof(*pow_data));
1914
1915 pow_data->active_index = IWL_POWER_RANGE_0;
1916 pow_data->dtim_val = 0xffff;
1917
1918 memcpy(&pow_data->pwr_range_0[0], &range_0[0], size);
1919 memcpy(&pow_data->pwr_range_1[0], &range_1[0], size);
1920
1921 rc = pci_read_config_word(priv->pci_dev, PCI_LINK_CTRL, &pci_pm);
1922 if (rc != 0)
1923 return 0;
1924 else {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001925 struct iwl4965_powertable_cmd *cmd;
Zhu Yib481de92007-09-25 17:54:57 -07001926
1927 IWL_DEBUG_POWER("adjust power command flags\n");
1928
1929 for (i = 0; i < IWL_POWER_AC; i++) {
1930 cmd = &pow_data->pwr_range_0[i].cmd;
1931
1932 if (pci_pm & 0x1)
1933 cmd->flags &= ~IWL_POWER_PCI_PM_MSK;
1934 else
1935 cmd->flags |= IWL_POWER_PCI_PM_MSK;
1936 }
1937 }
1938 return rc;
1939}
1940
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001941static int iwl4965_update_power_cmd(struct iwl4965_priv *priv,
1942 struct iwl4965_powertable_cmd *cmd, u32 mode)
Zhu Yib481de92007-09-25 17:54:57 -07001943{
1944 int rc = 0, i;
1945 u8 skip;
1946 u32 max_sleep = 0;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001947 struct iwl4965_power_vec_entry *range;
Zhu Yib481de92007-09-25 17:54:57 -07001948 u8 period = 0;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001949 struct iwl4965_power_mgr *pow_data;
Zhu Yib481de92007-09-25 17:54:57 -07001950
1951 if (mode > IWL_POWER_INDEX_5) {
1952 IWL_DEBUG_POWER("Error invalid power mode \n");
1953 return -1;
1954 }
1955 pow_data = &(priv->power_data);
1956
1957 if (pow_data->active_index == IWL_POWER_RANGE_0)
1958 range = &pow_data->pwr_range_0[0];
1959 else
1960 range = &pow_data->pwr_range_1[1];
1961
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001962 memcpy(cmd, &range[mode].cmd, sizeof(struct iwl4965_powertable_cmd));
Zhu Yib481de92007-09-25 17:54:57 -07001963
1964#ifdef IWL_MAC80211_DISABLE
1965 if (priv->assoc_network != NULL) {
1966 unsigned long flags;
1967
1968 period = priv->assoc_network->tim.tim_period;
1969 }
1970#endif /*IWL_MAC80211_DISABLE */
1971 skip = range[mode].no_dtim;
1972
1973 if (period == 0) {
1974 period = 1;
1975 skip = 0;
1976 }
1977
1978 if (skip == 0) {
1979 max_sleep = period;
1980 cmd->flags &= ~IWL_POWER_SLEEP_OVER_DTIM_MSK;
1981 } else {
1982 __le32 slp_itrvl = cmd->sleep_interval[IWL_POWER_VEC_SIZE - 1];
1983 max_sleep = (le32_to_cpu(slp_itrvl) / period) * period;
1984 cmd->flags |= IWL_POWER_SLEEP_OVER_DTIM_MSK;
1985 }
1986
1987 for (i = 0; i < IWL_POWER_VEC_SIZE; i++) {
1988 if (le32_to_cpu(cmd->sleep_interval[i]) > max_sleep)
1989 cmd->sleep_interval[i] = cpu_to_le32(max_sleep);
1990 }
1991
1992 IWL_DEBUG_POWER("Flags value = 0x%08X\n", cmd->flags);
1993 IWL_DEBUG_POWER("Tx timeout = %u\n", le32_to_cpu(cmd->tx_data_timeout));
1994 IWL_DEBUG_POWER("Rx timeout = %u\n", le32_to_cpu(cmd->rx_data_timeout));
1995 IWL_DEBUG_POWER("Sleep interval vector = { %d , %d , %d , %d , %d }\n",
1996 le32_to_cpu(cmd->sleep_interval[0]),
1997 le32_to_cpu(cmd->sleep_interval[1]),
1998 le32_to_cpu(cmd->sleep_interval[2]),
1999 le32_to_cpu(cmd->sleep_interval[3]),
2000 le32_to_cpu(cmd->sleep_interval[4]));
2001
2002 return rc;
2003}
2004
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002005static int iwl4965_send_power_mode(struct iwl4965_priv *priv, u32 mode)
Zhu Yib481de92007-09-25 17:54:57 -07002006{
John W. Linville9a62f732007-11-15 16:27:36 -05002007 u32 uninitialized_var(final_mode);
Zhu Yib481de92007-09-25 17:54:57 -07002008 int rc;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002009 struct iwl4965_powertable_cmd cmd;
Zhu Yib481de92007-09-25 17:54:57 -07002010
2011 /* If on battery, set to 3,
Ian Schram01ebd062007-10-25 17:15:22 +08002012 * if plugged into AC power, set to CAM ("continuously aware mode"),
Zhu Yib481de92007-09-25 17:54:57 -07002013 * else user level */
2014 switch (mode) {
2015 case IWL_POWER_BATTERY:
2016 final_mode = IWL_POWER_INDEX_3;
2017 break;
2018 case IWL_POWER_AC:
2019 final_mode = IWL_POWER_MODE_CAM;
2020 break;
2021 default:
2022 final_mode = mode;
2023 break;
2024 }
2025
2026 cmd.keep_alive_beacons = 0;
2027
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002028 iwl4965_update_power_cmd(priv, &cmd, final_mode);
Zhu Yib481de92007-09-25 17:54:57 -07002029
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002030 rc = iwl4965_send_cmd_pdu(priv, POWER_TABLE_CMD, sizeof(cmd), &cmd);
Zhu Yib481de92007-09-25 17:54:57 -07002031
2032 if (final_mode == IWL_POWER_MODE_CAM)
2033 clear_bit(STATUS_POWER_PMI, &priv->status);
2034 else
2035 set_bit(STATUS_POWER_PMI, &priv->status);
2036
2037 return rc;
2038}
2039
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002040int iwl4965_is_network_packet(struct iwl4965_priv *priv, struct ieee80211_hdr *header)
Zhu Yib481de92007-09-25 17:54:57 -07002041{
2042 /* Filter incoming packets to determine if they are targeted toward
2043 * this network, discarding packets coming from ourselves */
2044 switch (priv->iw_mode) {
2045 case IEEE80211_IF_TYPE_IBSS: /* Header: Dest. | Source | BSSID */
2046 /* packets from our adapter are dropped (echo) */
2047 if (!compare_ether_addr(header->addr2, priv->mac_addr))
2048 return 0;
2049 /* {broad,multi}cast packets to our IBSS go through */
2050 if (is_multicast_ether_addr(header->addr1))
2051 return !compare_ether_addr(header->addr3, priv->bssid);
2052 /* packets to our adapter go through */
2053 return !compare_ether_addr(header->addr1, priv->mac_addr);
2054 case IEEE80211_IF_TYPE_STA: /* Header: Dest. | AP{BSSID} | Source */
2055 /* packets from our adapter are dropped (echo) */
2056 if (!compare_ether_addr(header->addr3, priv->mac_addr))
2057 return 0;
2058 /* {broad,multi}cast packets to our BSS go through */
2059 if (is_multicast_ether_addr(header->addr1))
2060 return !compare_ether_addr(header->addr2, priv->bssid);
2061 /* packets to our adapter go through */
2062 return !compare_ether_addr(header->addr1, priv->mac_addr);
2063 }
2064
2065 return 1;
2066}
2067
2068#define TX_STATUS_ENTRY(x) case TX_STATUS_FAIL_ ## x: return #x
2069
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002070static const char *iwl4965_get_tx_fail_reason(u32 status)
Zhu Yib481de92007-09-25 17:54:57 -07002071{
2072 switch (status & TX_STATUS_MSK) {
2073 case TX_STATUS_SUCCESS:
2074 return "SUCCESS";
2075 TX_STATUS_ENTRY(SHORT_LIMIT);
2076 TX_STATUS_ENTRY(LONG_LIMIT);
2077 TX_STATUS_ENTRY(FIFO_UNDERRUN);
2078 TX_STATUS_ENTRY(MGMNT_ABORT);
2079 TX_STATUS_ENTRY(NEXT_FRAG);
2080 TX_STATUS_ENTRY(LIFE_EXPIRE);
2081 TX_STATUS_ENTRY(DEST_PS);
2082 TX_STATUS_ENTRY(ABORTED);
2083 TX_STATUS_ENTRY(BT_RETRY);
2084 TX_STATUS_ENTRY(STA_INVALID);
2085 TX_STATUS_ENTRY(FRAG_DROPPED);
2086 TX_STATUS_ENTRY(TID_DISABLE);
2087 TX_STATUS_ENTRY(FRAME_FLUSHED);
2088 TX_STATUS_ENTRY(INSUFFICIENT_CF_POLL);
2089 TX_STATUS_ENTRY(TX_LOCKED);
2090 TX_STATUS_ENTRY(NO_BEACON_ON_RADAR);
2091 }
2092
2093 return "UNKNOWN";
2094}
2095
2096/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002097 * iwl4965_scan_cancel - Cancel any currently executing HW scan
Zhu Yib481de92007-09-25 17:54:57 -07002098 *
2099 * NOTE: priv->mutex is not required before calling this function
2100 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002101static int iwl4965_scan_cancel(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002102{
2103 if (!test_bit(STATUS_SCAN_HW, &priv->status)) {
2104 clear_bit(STATUS_SCANNING, &priv->status);
2105 return 0;
2106 }
2107
2108 if (test_bit(STATUS_SCANNING, &priv->status)) {
2109 if (!test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
2110 IWL_DEBUG_SCAN("Queuing scan abort.\n");
2111 set_bit(STATUS_SCAN_ABORTING, &priv->status);
2112 queue_work(priv->workqueue, &priv->abort_scan);
2113
2114 } else
2115 IWL_DEBUG_SCAN("Scan abort already in progress.\n");
2116
2117 return test_bit(STATUS_SCANNING, &priv->status);
2118 }
2119
2120 return 0;
2121}
2122
2123/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002124 * iwl4965_scan_cancel_timeout - Cancel any currently executing HW scan
Zhu Yib481de92007-09-25 17:54:57 -07002125 * @ms: amount of time to wait (in milliseconds) for scan to abort
2126 *
2127 * NOTE: priv->mutex must be held before calling this function
2128 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002129static int iwl4965_scan_cancel_timeout(struct iwl4965_priv *priv, unsigned long ms)
Zhu Yib481de92007-09-25 17:54:57 -07002130{
2131 unsigned long now = jiffies;
2132 int ret;
2133
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002134 ret = iwl4965_scan_cancel(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002135 if (ret && ms) {
2136 mutex_unlock(&priv->mutex);
2137 while (!time_after(jiffies, now + msecs_to_jiffies(ms)) &&
2138 test_bit(STATUS_SCANNING, &priv->status))
2139 msleep(1);
2140 mutex_lock(&priv->mutex);
2141
2142 return test_bit(STATUS_SCANNING, &priv->status);
2143 }
2144
2145 return ret;
2146}
2147
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002148static void iwl4965_sequence_reset(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002149{
2150 /* Reset ieee stats */
2151
2152 /* We don't reset the net_device_stats (ieee->stats) on
2153 * re-association */
2154
2155 priv->last_seq_num = -1;
2156 priv->last_frag_num = -1;
2157 priv->last_packet_time = 0;
2158
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002159 iwl4965_scan_cancel(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002160}
2161
2162#define MAX_UCODE_BEACON_INTERVAL 4096
2163#define INTEL_CONN_LISTEN_INTERVAL __constant_cpu_to_le16(0xA)
2164
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002165static __le16 iwl4965_adjust_beacon_interval(u16 beacon_val)
Zhu Yib481de92007-09-25 17:54:57 -07002166{
2167 u16 new_val = 0;
2168 u16 beacon_factor = 0;
2169
2170 beacon_factor =
2171 (beacon_val + MAX_UCODE_BEACON_INTERVAL)
2172 / MAX_UCODE_BEACON_INTERVAL;
2173 new_val = beacon_val / beacon_factor;
2174
2175 return cpu_to_le16(new_val);
2176}
2177
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002178static void iwl4965_setup_rxon_timing(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002179{
2180 u64 interval_tm_unit;
2181 u64 tsf, result;
2182 unsigned long flags;
2183 struct ieee80211_conf *conf = NULL;
2184 u16 beacon_int = 0;
2185
2186 conf = ieee80211_get_hw_conf(priv->hw);
2187
2188 spin_lock_irqsave(&priv->lock, flags);
2189 priv->rxon_timing.timestamp.dw[1] = cpu_to_le32(priv->timestamp1);
2190 priv->rxon_timing.timestamp.dw[0] = cpu_to_le32(priv->timestamp0);
2191
2192 priv->rxon_timing.listen_interval = INTEL_CONN_LISTEN_INTERVAL;
2193
2194 tsf = priv->timestamp1;
2195 tsf = ((tsf << 32) | priv->timestamp0);
2196
2197 beacon_int = priv->beacon_int;
2198 spin_unlock_irqrestore(&priv->lock, flags);
2199
2200 if (priv->iw_mode == IEEE80211_IF_TYPE_STA) {
2201 if (beacon_int == 0) {
2202 priv->rxon_timing.beacon_interval = cpu_to_le16(100);
2203 priv->rxon_timing.beacon_init_val = cpu_to_le32(102400);
2204 } else {
2205 priv->rxon_timing.beacon_interval =
2206 cpu_to_le16(beacon_int);
2207 priv->rxon_timing.beacon_interval =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002208 iwl4965_adjust_beacon_interval(
Zhu Yib481de92007-09-25 17:54:57 -07002209 le16_to_cpu(priv->rxon_timing.beacon_interval));
2210 }
2211
2212 priv->rxon_timing.atim_window = 0;
2213 } else {
2214 priv->rxon_timing.beacon_interval =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002215 iwl4965_adjust_beacon_interval(conf->beacon_int);
Zhu Yib481de92007-09-25 17:54:57 -07002216 /* TODO: we need to get atim_window from upper stack
2217 * for now we set to 0 */
2218 priv->rxon_timing.atim_window = 0;
2219 }
2220
2221 interval_tm_unit =
2222 (le16_to_cpu(priv->rxon_timing.beacon_interval) * 1024);
2223 result = do_div(tsf, interval_tm_unit);
2224 priv->rxon_timing.beacon_init_val =
2225 cpu_to_le32((u32) ((u64) interval_tm_unit - result));
2226
2227 IWL_DEBUG_ASSOC
2228 ("beacon interval %d beacon timer %d beacon tim %d\n",
2229 le16_to_cpu(priv->rxon_timing.beacon_interval),
2230 le32_to_cpu(priv->rxon_timing.beacon_init_val),
2231 le16_to_cpu(priv->rxon_timing.atim_window));
2232}
2233
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002234static int iwl4965_scan_initiate(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002235{
2236 if (priv->iw_mode == IEEE80211_IF_TYPE_AP) {
2237 IWL_ERROR("APs don't scan.\n");
2238 return 0;
2239 }
2240
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002241 if (!iwl4965_is_ready_rf(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07002242 IWL_DEBUG_SCAN("Aborting scan due to not ready.\n");
2243 return -EIO;
2244 }
2245
2246 if (test_bit(STATUS_SCANNING, &priv->status)) {
2247 IWL_DEBUG_SCAN("Scan already in progress.\n");
2248 return -EAGAIN;
2249 }
2250
2251 if (test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
2252 IWL_DEBUG_SCAN("Scan request while abort pending. "
2253 "Queuing.\n");
2254 return -EAGAIN;
2255 }
2256
2257 IWL_DEBUG_INFO("Starting scan...\n");
2258 priv->scan_bands = 2;
2259 set_bit(STATUS_SCANNING, &priv->status);
2260 priv->scan_start = jiffies;
2261 priv->scan_pass_start = priv->scan_start;
2262
2263 queue_work(priv->workqueue, &priv->request_scan);
2264
2265 return 0;
2266}
2267
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002268static int iwl4965_set_rxon_hwcrypto(struct iwl4965_priv *priv, int hw_decrypt)
Zhu Yib481de92007-09-25 17:54:57 -07002269{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002270 struct iwl4965_rxon_cmd *rxon = &priv->staging_rxon;
Zhu Yib481de92007-09-25 17:54:57 -07002271
2272 if (hw_decrypt)
2273 rxon->filter_flags &= ~RXON_FILTER_DIS_DECRYPT_MSK;
2274 else
2275 rxon->filter_flags |= RXON_FILTER_DIS_DECRYPT_MSK;
2276
2277 return 0;
2278}
2279
Johannes Berg8318d782008-01-24 19:38:38 +01002280static void iwl4965_set_flags_for_phymode(struct iwl4965_priv *priv,
2281 enum ieee80211_band band)
Zhu Yib481de92007-09-25 17:54:57 -07002282{
Johannes Berg8318d782008-01-24 19:38:38 +01002283 if (band == IEEE80211_BAND_5GHZ) {
Zhu Yib481de92007-09-25 17:54:57 -07002284 priv->staging_rxon.flags &=
2285 ~(RXON_FLG_BAND_24G_MSK | RXON_FLG_AUTO_DETECT_MSK
2286 | RXON_FLG_CCK_MSK);
2287 priv->staging_rxon.flags |= RXON_FLG_SHORT_SLOT_MSK;
2288 } else {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002289 /* Copied from iwl4965_bg_post_associate() */
Zhu Yib481de92007-09-25 17:54:57 -07002290 if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_SLOT_TIME)
2291 priv->staging_rxon.flags |= RXON_FLG_SHORT_SLOT_MSK;
2292 else
2293 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
2294
2295 if (priv->iw_mode == IEEE80211_IF_TYPE_IBSS)
2296 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
2297
2298 priv->staging_rxon.flags |= RXON_FLG_BAND_24G_MSK;
2299 priv->staging_rxon.flags |= RXON_FLG_AUTO_DETECT_MSK;
2300 priv->staging_rxon.flags &= ~RXON_FLG_CCK_MSK;
2301 }
2302}
2303
2304/*
Ian Schram01ebd062007-10-25 17:15:22 +08002305 * initialize rxon structure with default values from eeprom
Zhu Yib481de92007-09-25 17:54:57 -07002306 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002307static void iwl4965_connection_init_rx_config(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002308{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002309 const struct iwl4965_channel_info *ch_info;
Zhu Yib481de92007-09-25 17:54:57 -07002310
2311 memset(&priv->staging_rxon, 0, sizeof(priv->staging_rxon));
2312
2313 switch (priv->iw_mode) {
2314 case IEEE80211_IF_TYPE_AP:
2315 priv->staging_rxon.dev_type = RXON_DEV_TYPE_AP;
2316 break;
2317
2318 case IEEE80211_IF_TYPE_STA:
2319 priv->staging_rxon.dev_type = RXON_DEV_TYPE_ESS;
2320 priv->staging_rxon.filter_flags = RXON_FILTER_ACCEPT_GRP_MSK;
2321 break;
2322
2323 case IEEE80211_IF_TYPE_IBSS:
2324 priv->staging_rxon.dev_type = RXON_DEV_TYPE_IBSS;
2325 priv->staging_rxon.flags = RXON_FLG_SHORT_PREAMBLE_MSK;
2326 priv->staging_rxon.filter_flags = RXON_FILTER_BCON_AWARE_MSK |
2327 RXON_FILTER_ACCEPT_GRP_MSK;
2328 break;
2329
2330 case IEEE80211_IF_TYPE_MNTR:
2331 priv->staging_rxon.dev_type = RXON_DEV_TYPE_SNIFFER;
2332 priv->staging_rxon.filter_flags = RXON_FILTER_PROMISC_MSK |
2333 RXON_FILTER_CTL2HOST_MSK | RXON_FILTER_ACCEPT_GRP_MSK;
2334 break;
2335 }
2336
2337#if 0
2338 /* TODO: Figure out when short_preamble would be set and cache from
2339 * that */
2340 if (!hw_to_local(priv->hw)->short_preamble)
2341 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_PREAMBLE_MSK;
2342 else
2343 priv->staging_rxon.flags |= RXON_FLG_SHORT_PREAMBLE_MSK;
2344#endif
2345
Johannes Berg8318d782008-01-24 19:38:38 +01002346 ch_info = iwl4965_get_channel_info(priv, priv->band,
Zhu Yib481de92007-09-25 17:54:57 -07002347 le16_to_cpu(priv->staging_rxon.channel));
2348
2349 if (!ch_info)
2350 ch_info = &priv->channel_info[0];
2351
2352 /*
2353 * in some case A channels are all non IBSS
2354 * in this case force B/G channel
2355 */
2356 if ((priv->iw_mode == IEEE80211_IF_TYPE_IBSS) &&
2357 !(is_channel_ibss(ch_info)))
2358 ch_info = &priv->channel_info[0];
2359
2360 priv->staging_rxon.channel = cpu_to_le16(ch_info->channel);
Johannes Berg8318d782008-01-24 19:38:38 +01002361 priv->band = ch_info->band;
Zhu Yib481de92007-09-25 17:54:57 -07002362
Johannes Berg8318d782008-01-24 19:38:38 +01002363 iwl4965_set_flags_for_phymode(priv, priv->band);
Zhu Yib481de92007-09-25 17:54:57 -07002364
2365 priv->staging_rxon.ofdm_basic_rates =
2366 (IWL_OFDM_RATES_MASK >> IWL_FIRST_OFDM_RATE) & 0xFF;
2367 priv->staging_rxon.cck_basic_rates =
2368 (IWL_CCK_RATES_MASK >> IWL_FIRST_CCK_RATE) & 0xF;
2369
2370 priv->staging_rxon.flags &= ~(RXON_FLG_CHANNEL_MODE_MIXED_MSK |
2371 RXON_FLG_CHANNEL_MODE_PURE_40_MSK);
2372 memcpy(priv->staging_rxon.node_addr, priv->mac_addr, ETH_ALEN);
2373 memcpy(priv->staging_rxon.wlap_bssid_addr, priv->mac_addr, ETH_ALEN);
2374 priv->staging_rxon.ofdm_ht_single_stream_basic_rates = 0xff;
2375 priv->staging_rxon.ofdm_ht_dual_stream_basic_rates = 0xff;
2376 iwl4965_set_rxon_chain(priv);
2377}
2378
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002379static int iwl4965_set_mode(struct iwl4965_priv *priv, int mode)
Zhu Yib481de92007-09-25 17:54:57 -07002380{
Zhu Yib481de92007-09-25 17:54:57 -07002381 if (mode == IEEE80211_IF_TYPE_IBSS) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002382 const struct iwl4965_channel_info *ch_info;
Zhu Yib481de92007-09-25 17:54:57 -07002383
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002384 ch_info = iwl4965_get_channel_info(priv,
Johannes Berg8318d782008-01-24 19:38:38 +01002385 priv->band,
Zhu Yib481de92007-09-25 17:54:57 -07002386 le16_to_cpu(priv->staging_rxon.channel));
2387
2388 if (!ch_info || !is_channel_ibss(ch_info)) {
2389 IWL_ERROR("channel %d not IBSS channel\n",
2390 le16_to_cpu(priv->staging_rxon.channel));
2391 return -EINVAL;
2392 }
2393 }
2394
Zhu Yib481de92007-09-25 17:54:57 -07002395 priv->iw_mode = mode;
2396
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002397 iwl4965_connection_init_rx_config(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002398 memcpy(priv->staging_rxon.node_addr, priv->mac_addr, ETH_ALEN);
2399
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002400 iwl4965_clear_stations_table(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002401
Mohamed Abbasfde35712007-11-29 11:10:15 +08002402 /* dont commit rxon if rf-kill is on*/
2403 if (!iwl4965_is_ready_rf(priv))
2404 return -EAGAIN;
2405
2406 cancel_delayed_work(&priv->scan_check);
2407 if (iwl4965_scan_cancel_timeout(priv, 100)) {
2408 IWL_WARNING("Aborted scan still in progress after 100ms\n");
2409 IWL_DEBUG_MAC80211("leaving - scan abort failed.\n");
2410 return -EAGAIN;
2411 }
2412
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002413 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002414
2415 return 0;
2416}
2417
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002418static void iwl4965_build_tx_cmd_hwcrypto(struct iwl4965_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07002419 struct ieee80211_tx_control *ctl,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002420 struct iwl4965_cmd *cmd,
Zhu Yib481de92007-09-25 17:54:57 -07002421 struct sk_buff *skb_frag,
2422 int last_frag)
2423{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002424 struct iwl4965_hw_key *keyinfo = &priv->stations[ctl->key_idx].keyinfo;
Zhu Yib481de92007-09-25 17:54:57 -07002425
2426 switch (keyinfo->alg) {
2427 case ALG_CCMP:
2428 cmd->cmd.tx.sec_ctl = TX_CMD_SEC_CCM;
2429 memcpy(cmd->cmd.tx.key, keyinfo->key, keyinfo->keylen);
2430 IWL_DEBUG_TX("tx_cmd with aes hwcrypto\n");
2431 break;
2432
2433 case ALG_TKIP:
2434#if 0
2435 cmd->cmd.tx.sec_ctl = TX_CMD_SEC_TKIP;
2436
2437 if (last_frag)
2438 memcpy(cmd->cmd.tx.tkip_mic.byte, skb_frag->tail - 8,
2439 8);
2440 else
2441 memset(cmd->cmd.tx.tkip_mic.byte, 0, 8);
2442#endif
2443 break;
2444
2445 case ALG_WEP:
2446 cmd->cmd.tx.sec_ctl = TX_CMD_SEC_WEP |
2447 (ctl->key_idx & TX_CMD_SEC_MSK) << TX_CMD_SEC_SHIFT;
2448
2449 if (keyinfo->keylen == 13)
2450 cmd->cmd.tx.sec_ctl |= TX_CMD_SEC_KEY128;
2451
2452 memcpy(&cmd->cmd.tx.key[3], keyinfo->key, keyinfo->keylen);
2453
2454 IWL_DEBUG_TX("Configuring packet for WEP encryption "
2455 "with key %d\n", ctl->key_idx);
2456 break;
2457
Zhu Yib481de92007-09-25 17:54:57 -07002458 default:
2459 printk(KERN_ERR "Unknown encode alg %d\n", keyinfo->alg);
2460 break;
2461 }
2462}
2463
2464/*
2465 * handle build REPLY_TX command notification.
2466 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002467static void iwl4965_build_tx_cmd_basic(struct iwl4965_priv *priv,
2468 struct iwl4965_cmd *cmd,
Zhu Yib481de92007-09-25 17:54:57 -07002469 struct ieee80211_tx_control *ctrl,
2470 struct ieee80211_hdr *hdr,
2471 int is_unicast, u8 std_id)
2472{
2473 __le16 *qc;
2474 u16 fc = le16_to_cpu(hdr->frame_control);
2475 __le32 tx_flags = cmd->cmd.tx.tx_flags;
2476
2477 cmd->cmd.tx.stop_time.life_time = TX_CMD_LIFE_TIME_INFINITE;
2478 if (!(ctrl->flags & IEEE80211_TXCTL_NO_ACK)) {
2479 tx_flags |= TX_CMD_FLG_ACK_MSK;
2480 if ((fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_MGMT)
2481 tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
2482 if (ieee80211_is_probe_response(fc) &&
2483 !(le16_to_cpu(hdr->seq_ctrl) & 0xf))
2484 tx_flags |= TX_CMD_FLG_TSF_MSK;
2485 } else {
2486 tx_flags &= (~TX_CMD_FLG_ACK_MSK);
2487 tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
2488 }
2489
Tomas Winkler87e4f7d2008-01-14 17:46:16 -08002490 if (ieee80211_is_back_request(fc))
2491 tx_flags |= TX_CMD_FLG_ACK_MSK | TX_CMD_FLG_IMM_BA_RSP_MASK;
2492
2493
Zhu Yib481de92007-09-25 17:54:57 -07002494 cmd->cmd.tx.sta_id = std_id;
2495 if (ieee80211_get_morefrag(hdr))
2496 tx_flags |= TX_CMD_FLG_MORE_FRAG_MSK;
2497
2498 qc = ieee80211_get_qos_ctrl(hdr);
2499 if (qc) {
2500 cmd->cmd.tx.tid_tspec = (u8) (le16_to_cpu(*qc) & 0xf);
2501 tx_flags &= ~TX_CMD_FLG_SEQ_CTL_MSK;
2502 } else
2503 tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
2504
2505 if (ctrl->flags & IEEE80211_TXCTL_USE_RTS_CTS) {
2506 tx_flags |= TX_CMD_FLG_RTS_MSK;
2507 tx_flags &= ~TX_CMD_FLG_CTS_MSK;
2508 } else if (ctrl->flags & IEEE80211_TXCTL_USE_CTS_PROTECT) {
2509 tx_flags &= ~TX_CMD_FLG_RTS_MSK;
2510 tx_flags |= TX_CMD_FLG_CTS_MSK;
2511 }
2512
2513 if ((tx_flags & TX_CMD_FLG_RTS_MSK) || (tx_flags & TX_CMD_FLG_CTS_MSK))
2514 tx_flags |= TX_CMD_FLG_FULL_TXOP_PROT_MSK;
2515
2516 tx_flags &= ~(TX_CMD_FLG_ANT_SEL_MSK);
2517 if ((fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_MGMT) {
2518 if ((fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_ASSOC_REQ ||
2519 (fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_REASSOC_REQ)
Ian Schrambc434dd2007-10-25 17:15:29 +08002520 cmd->cmd.tx.timeout.pm_frame_timeout = cpu_to_le16(3);
Zhu Yib481de92007-09-25 17:54:57 -07002521 else
Ian Schrambc434dd2007-10-25 17:15:29 +08002522 cmd->cmd.tx.timeout.pm_frame_timeout = cpu_to_le16(2);
Zhu Yib481de92007-09-25 17:54:57 -07002523 } else
2524 cmd->cmd.tx.timeout.pm_frame_timeout = 0;
2525
2526 cmd->cmd.tx.driver_txop = 0;
2527 cmd->cmd.tx.tx_flags = tx_flags;
2528 cmd->cmd.tx.next_frame_len = 0;
2529}
2530
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002531/**
2532 * iwl4965_get_sta_id - Find station's index within station table
2533 *
2534 * If new IBSS station, create new entry in station table
2535 */
Ben Cahill9fbab512007-11-29 11:09:47 +08002536static int iwl4965_get_sta_id(struct iwl4965_priv *priv,
2537 struct ieee80211_hdr *hdr)
Zhu Yib481de92007-09-25 17:54:57 -07002538{
2539 int sta_id;
2540 u16 fc = le16_to_cpu(hdr->frame_control);
Joe Perches0795af52007-10-03 17:59:30 -07002541 DECLARE_MAC_BUF(mac);
Zhu Yib481de92007-09-25 17:54:57 -07002542
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002543 /* If this frame is broadcast or management, use broadcast station id */
Zhu Yib481de92007-09-25 17:54:57 -07002544 if (((fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_DATA) ||
2545 is_multicast_ether_addr(hdr->addr1))
2546 return priv->hw_setting.bcast_sta_id;
2547
2548 switch (priv->iw_mode) {
2549
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002550 /* If we are a client station in a BSS network, use the special
2551 * AP station entry (that's the only station we communicate with) */
Zhu Yib481de92007-09-25 17:54:57 -07002552 case IEEE80211_IF_TYPE_STA:
2553 return IWL_AP_ID;
2554
2555 /* If we are an AP, then find the station, or use BCAST */
2556 case IEEE80211_IF_TYPE_AP:
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002557 sta_id = iwl4965_hw_find_station(priv, hdr->addr1);
Zhu Yib481de92007-09-25 17:54:57 -07002558 if (sta_id != IWL_INVALID_STATION)
2559 return sta_id;
2560 return priv->hw_setting.bcast_sta_id;
2561
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002562 /* If this frame is going out to an IBSS network, find the station,
2563 * or create a new station table entry */
Zhu Yib481de92007-09-25 17:54:57 -07002564 case IEEE80211_IF_TYPE_IBSS:
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002565 sta_id = iwl4965_hw_find_station(priv, hdr->addr1);
Zhu Yib481de92007-09-25 17:54:57 -07002566 if (sta_id != IWL_INVALID_STATION)
2567 return sta_id;
2568
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002569 /* Create new station table entry */
Ron Rindjunsky67d62032007-11-26 16:14:40 +02002570 sta_id = iwl4965_add_station_flags(priv, hdr->addr1,
2571 0, CMD_ASYNC, NULL);
Zhu Yib481de92007-09-25 17:54:57 -07002572
2573 if (sta_id != IWL_INVALID_STATION)
2574 return sta_id;
2575
Joe Perches0795af52007-10-03 17:59:30 -07002576 IWL_DEBUG_DROP("Station %s not in station map. "
Zhu Yib481de92007-09-25 17:54:57 -07002577 "Defaulting to broadcast...\n",
Joe Perches0795af52007-10-03 17:59:30 -07002578 print_mac(mac, hdr->addr1));
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002579 iwl4965_print_hex_dump(IWL_DL_DROP, (u8 *) hdr, sizeof(*hdr));
Zhu Yib481de92007-09-25 17:54:57 -07002580 return priv->hw_setting.bcast_sta_id;
2581
2582 default:
Ian Schram01ebd062007-10-25 17:15:22 +08002583 IWL_WARNING("Unknown mode of operation: %d", priv->iw_mode);
Zhu Yib481de92007-09-25 17:54:57 -07002584 return priv->hw_setting.bcast_sta_id;
2585 }
2586}
2587
2588/*
2589 * start REPLY_TX command process
2590 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002591static int iwl4965_tx_skb(struct iwl4965_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07002592 struct sk_buff *skb, struct ieee80211_tx_control *ctl)
2593{
2594 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002595 struct iwl4965_tfd_frame *tfd;
Zhu Yib481de92007-09-25 17:54:57 -07002596 u32 *control_flags;
2597 int txq_id = ctl->queue;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002598 struct iwl4965_tx_queue *txq = NULL;
2599 struct iwl4965_queue *q = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07002600 dma_addr_t phys_addr;
2601 dma_addr_t txcmd_phys;
Tomas Winkler87e4f7d2008-01-14 17:46:16 -08002602 dma_addr_t scratch_phys;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002603 struct iwl4965_cmd *out_cmd = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07002604 u16 len, idx, len_org;
2605 u8 id, hdr_len, unicast;
2606 u8 sta_id;
2607 u16 seq_number = 0;
2608 u16 fc;
2609 __le16 *qc;
2610 u8 wait_write_ptr = 0;
2611 unsigned long flags;
2612 int rc;
2613
2614 spin_lock_irqsave(&priv->lock, flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002615 if (iwl4965_is_rfkill(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07002616 IWL_DEBUG_DROP("Dropping - RF KILL\n");
2617 goto drop_unlock;
2618 }
2619
Johannes Berg32bfd352007-12-19 01:31:26 +01002620 if (!priv->vif) {
2621 IWL_DEBUG_DROP("Dropping - !priv->vif\n");
Zhu Yib481de92007-09-25 17:54:57 -07002622 goto drop_unlock;
2623 }
2624
Johannes Berg8318d782008-01-24 19:38:38 +01002625 if ((ctl->tx_rate->hw_value & 0xFF) == IWL_INVALID_RATE) {
Zhu Yib481de92007-09-25 17:54:57 -07002626 IWL_ERROR("ERROR: No TX rate available.\n");
2627 goto drop_unlock;
2628 }
2629
2630 unicast = !is_multicast_ether_addr(hdr->addr1);
2631 id = 0;
2632
2633 fc = le16_to_cpu(hdr->frame_control);
2634
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08002635#ifdef CONFIG_IWL4965_DEBUG
Zhu Yib481de92007-09-25 17:54:57 -07002636 if (ieee80211_is_auth(fc))
2637 IWL_DEBUG_TX("Sending AUTH frame\n");
2638 else if (ieee80211_is_assoc_request(fc))
2639 IWL_DEBUG_TX("Sending ASSOC frame\n");
2640 else if (ieee80211_is_reassoc_request(fc))
2641 IWL_DEBUG_TX("Sending REASSOC frame\n");
2642#endif
2643
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08002644 /* drop all data frame if we are not associated */
Gregory Greenman76f39152008-01-23 10:15:21 -08002645 if (((fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_DATA) &&
2646 (!iwl4965_is_associated(priv) ||
Reinette Chatrea6477242008-02-14 10:40:28 -08002647 ((priv->iw_mode == IEEE80211_IF_TYPE_STA) && !priv->assoc_id) ||
Gregory Greenman76f39152008-01-23 10:15:21 -08002648 !priv->assoc_station_added)) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002649 IWL_DEBUG_DROP("Dropping - !iwl4965_is_associated\n");
Zhu Yib481de92007-09-25 17:54:57 -07002650 goto drop_unlock;
2651 }
2652
2653 spin_unlock_irqrestore(&priv->lock, flags);
2654
2655 hdr_len = ieee80211_get_hdrlen(fc);
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002656
2657 /* Find (or create) index into station table for destination station */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002658 sta_id = iwl4965_get_sta_id(priv, hdr);
Zhu Yib481de92007-09-25 17:54:57 -07002659 if (sta_id == IWL_INVALID_STATION) {
Joe Perches0795af52007-10-03 17:59:30 -07002660 DECLARE_MAC_BUF(mac);
2661
2662 IWL_DEBUG_DROP("Dropping - INVALID STATION: %s\n",
2663 print_mac(mac, hdr->addr1));
Zhu Yib481de92007-09-25 17:54:57 -07002664 goto drop;
2665 }
2666
2667 IWL_DEBUG_RATE("station Id %d\n", sta_id);
2668
2669 qc = ieee80211_get_qos_ctrl(hdr);
2670 if (qc) {
2671 u8 tid = (u8)(le16_to_cpu(*qc) & 0xf);
2672 seq_number = priv->stations[sta_id].tid[tid].seq_number &
2673 IEEE80211_SCTL_SEQ;
2674 hdr->seq_ctrl = cpu_to_le16(seq_number) |
2675 (hdr->seq_ctrl &
2676 __constant_cpu_to_le16(IEEE80211_SCTL_FRAG));
2677 seq_number += 0x10;
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08002678#ifdef CONFIG_IWL4965_HT
Zhu Yib481de92007-09-25 17:54:57 -07002679 /* aggregation is on for this <sta,tid> */
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02002680 if (ctl->flags & IEEE80211_TXCTL_AMPDU)
Zhu Yib481de92007-09-25 17:54:57 -07002681 txq_id = priv->stations[sta_id].tid[tid].agg.txq_id;
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02002682 priv->stations[sta_id].tid[tid].tfds_in_queue++;
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08002683#endif /* CONFIG_IWL4965_HT */
Zhu Yib481de92007-09-25 17:54:57 -07002684 }
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002685
2686 /* Descriptor for chosen Tx queue */
Zhu Yib481de92007-09-25 17:54:57 -07002687 txq = &priv->txq[txq_id];
2688 q = &txq->q;
2689
2690 spin_lock_irqsave(&priv->lock, flags);
2691
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002692 /* Set up first empty TFD within this queue's circular TFD buffer */
Tomas Winklerfc4b6852007-10-25 17:15:24 +08002693 tfd = &txq->bd[q->write_ptr];
Zhu Yib481de92007-09-25 17:54:57 -07002694 memset(tfd, 0, sizeof(*tfd));
2695 control_flags = (u32 *) tfd;
Tomas Winklerfc4b6852007-10-25 17:15:24 +08002696 idx = get_cmd_index(q, q->write_ptr, 0);
Zhu Yib481de92007-09-25 17:54:57 -07002697
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002698 /* Set up driver data for this TFD */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002699 memset(&(txq->txb[q->write_ptr]), 0, sizeof(struct iwl4965_tx_info));
Tomas Winklerfc4b6852007-10-25 17:15:24 +08002700 txq->txb[q->write_ptr].skb[0] = skb;
2701 memcpy(&(txq->txb[q->write_ptr].status.control),
Zhu Yib481de92007-09-25 17:54:57 -07002702 ctl, sizeof(struct ieee80211_tx_control));
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002703
2704 /* Set up first empty entry in queue's array of Tx/cmd buffers */
Zhu Yib481de92007-09-25 17:54:57 -07002705 out_cmd = &txq->cmd[idx];
2706 memset(&out_cmd->hdr, 0, sizeof(out_cmd->hdr));
2707 memset(&out_cmd->cmd.tx, 0, sizeof(out_cmd->cmd.tx));
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002708
2709 /*
2710 * Set up the Tx-command (not MAC!) header.
2711 * Store the chosen Tx queue and TFD index within the sequence field;
2712 * after Tx, uCode's Tx response will return this value so driver can
2713 * locate the frame within the tx queue and do post-tx processing.
2714 */
Zhu Yib481de92007-09-25 17:54:57 -07002715 out_cmd->hdr.cmd = REPLY_TX;
2716 out_cmd->hdr.sequence = cpu_to_le16((u16)(QUEUE_TO_SEQ(txq_id) |
Tomas Winklerfc4b6852007-10-25 17:15:24 +08002717 INDEX_TO_SEQ(q->write_ptr)));
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002718
2719 /* Copy MAC header from skb into command buffer */
Zhu Yib481de92007-09-25 17:54:57 -07002720 memcpy(out_cmd->cmd.tx.hdr, hdr, hdr_len);
2721
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002722 /*
2723 * Use the first empty entry in this queue's command buffer array
2724 * to contain the Tx command and MAC header concatenated together
2725 * (payload data will be in another buffer).
2726 * Size of this varies, due to varying MAC header length.
2727 * If end is not dword aligned, we'll have 2 extra bytes at the end
2728 * of the MAC header (device reads on dword boundaries).
2729 * We'll tell device about this padding later.
2730 */
Zhu Yib481de92007-09-25 17:54:57 -07002731 len = priv->hw_setting.tx_cmd_len +
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002732 sizeof(struct iwl4965_cmd_header) + hdr_len;
Zhu Yib481de92007-09-25 17:54:57 -07002733
2734 len_org = len;
2735 len = (len + 3) & ~3;
2736
2737 if (len_org != len)
2738 len_org = 1;
2739 else
2740 len_org = 0;
2741
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002742 /* Physical address of this Tx command's header (not MAC header!),
2743 * within command buffer array. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002744 txcmd_phys = txq->dma_addr_cmd + sizeof(struct iwl4965_cmd) * idx +
2745 offsetof(struct iwl4965_cmd, hdr);
Zhu Yib481de92007-09-25 17:54:57 -07002746
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002747 /* Add buffer containing Tx command and MAC(!) header to TFD's
2748 * first entry */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002749 iwl4965_hw_txq_attach_buf_to_tfd(priv, tfd, txcmd_phys, len);
Zhu Yib481de92007-09-25 17:54:57 -07002750
2751 if (!(ctl->flags & IEEE80211_TXCTL_DO_NOT_ENCRYPT))
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002752 iwl4965_build_tx_cmd_hwcrypto(priv, ctl, out_cmd, skb, 0);
Zhu Yib481de92007-09-25 17:54:57 -07002753
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002754 /* Set up TFD's 2nd entry to point directly to remainder of skb,
2755 * if any (802.11 null frames have no payload). */
Zhu Yib481de92007-09-25 17:54:57 -07002756 len = skb->len - hdr_len;
2757 if (len) {
2758 phys_addr = pci_map_single(priv->pci_dev, skb->data + hdr_len,
2759 len, PCI_DMA_TODEVICE);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002760 iwl4965_hw_txq_attach_buf_to_tfd(priv, tfd, phys_addr, len);
Zhu Yib481de92007-09-25 17:54:57 -07002761 }
2762
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002763 /* Tell 4965 about any 2-byte padding after MAC header */
Zhu Yib481de92007-09-25 17:54:57 -07002764 if (len_org)
2765 out_cmd->cmd.tx.tx_flags |= TX_CMD_FLG_MH_PAD_MSK;
2766
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002767 /* Total # bytes to be transmitted */
Zhu Yib481de92007-09-25 17:54:57 -07002768 len = (u16)skb->len;
2769 out_cmd->cmd.tx.len = cpu_to_le16(len);
2770
2771 /* TODO need this for burst mode later on */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002772 iwl4965_build_tx_cmd_basic(priv, out_cmd, ctl, hdr, unicast, sta_id);
Zhu Yib481de92007-09-25 17:54:57 -07002773
2774 /* set is_hcca to 0; it probably will never be implemented */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002775 iwl4965_hw_build_tx_cmd_rate(priv, out_cmd, ctl, hdr, sta_id, 0);
Zhu Yib481de92007-09-25 17:54:57 -07002776
Tomas Winkler87e4f7d2008-01-14 17:46:16 -08002777 scratch_phys = txcmd_phys + sizeof(struct iwl4965_cmd_header) +
2778 offsetof(struct iwl4965_tx_cmd, scratch);
2779 out_cmd->cmd.tx.dram_lsb_ptr = cpu_to_le32(scratch_phys);
2780 out_cmd->cmd.tx.dram_msb_ptr = iwl_get_dma_hi_address(scratch_phys);
2781
Zhu Yib481de92007-09-25 17:54:57 -07002782 if (!ieee80211_get_morefrag(hdr)) {
2783 txq->need_update = 1;
2784 if (qc) {
2785 u8 tid = (u8)(le16_to_cpu(*qc) & 0xf);
2786 priv->stations[sta_id].tid[tid].seq_number = seq_number;
2787 }
2788 } else {
2789 wait_write_ptr = 1;
2790 txq->need_update = 0;
2791 }
2792
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002793 iwl4965_print_hex_dump(IWL_DL_TX, out_cmd->cmd.payload,
Zhu Yib481de92007-09-25 17:54:57 -07002794 sizeof(out_cmd->cmd.tx));
2795
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002796 iwl4965_print_hex_dump(IWL_DL_TX, (u8 *)out_cmd->cmd.tx.hdr,
Zhu Yib481de92007-09-25 17:54:57 -07002797 ieee80211_get_hdrlen(fc));
2798
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002799 /* Set up entry for this TFD in Tx byte-count array */
Zhu Yib481de92007-09-25 17:54:57 -07002800 iwl4965_tx_queue_update_wr_ptr(priv, txq, len);
2801
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002802 /* Tell device the write index *just past* this latest filled TFD */
Tomas Winklerc54b6792008-03-06 17:36:53 -08002803 q->write_ptr = iwl_queue_inc_wrap(q->write_ptr, q->n_bd);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002804 rc = iwl4965_tx_queue_update_write_ptr(priv, txq);
Zhu Yib481de92007-09-25 17:54:57 -07002805 spin_unlock_irqrestore(&priv->lock, flags);
2806
2807 if (rc)
2808 return rc;
2809
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002810 if ((iwl4965_queue_space(q) < q->high_mark)
Zhu Yib481de92007-09-25 17:54:57 -07002811 && priv->mac80211_registered) {
2812 if (wait_write_ptr) {
2813 spin_lock_irqsave(&priv->lock, flags);
2814 txq->need_update = 1;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002815 iwl4965_tx_queue_update_write_ptr(priv, txq);
Zhu Yib481de92007-09-25 17:54:57 -07002816 spin_unlock_irqrestore(&priv->lock, flags);
2817 }
2818
2819 ieee80211_stop_queue(priv->hw, ctl->queue);
2820 }
2821
2822 return 0;
2823
2824drop_unlock:
2825 spin_unlock_irqrestore(&priv->lock, flags);
2826drop:
2827 return -1;
2828}
2829
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002830static void iwl4965_set_rate(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002831{
Johannes Berg8318d782008-01-24 19:38:38 +01002832 const struct ieee80211_supported_band *hw = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07002833 struct ieee80211_rate *rate;
2834 int i;
2835
Johannes Berg8318d782008-01-24 19:38:38 +01002836 hw = iwl4965_get_hw_mode(priv, priv->band);
Saleem Abdulrasoolc4ba9622007-11-18 23:59:08 -08002837 if (!hw) {
2838 IWL_ERROR("Failed to set rate: unable to get hw mode\n");
2839 return;
2840 }
Zhu Yib481de92007-09-25 17:54:57 -07002841
2842 priv->active_rate = 0;
2843 priv->active_rate_basic = 0;
2844
Johannes Berg8318d782008-01-24 19:38:38 +01002845 for (i = 0; i < hw->n_bitrates; i++) {
2846 rate = &(hw->bitrates[i]);
2847 if (rate->hw_value < IWL_RATE_COUNT)
2848 priv->active_rate |= (1 << rate->hw_value);
Zhu Yib481de92007-09-25 17:54:57 -07002849 }
2850
2851 IWL_DEBUG_RATE("Set active_rate = %0x, active_rate_basic = %0x\n",
2852 priv->active_rate, priv->active_rate_basic);
2853
2854 /*
2855 * If a basic rate is configured, then use it (adding IWL_RATE_1M_MASK)
2856 * otherwise set it to the default of all CCK rates and 6, 12, 24 for
2857 * OFDM
2858 */
2859 if (priv->active_rate_basic & IWL_CCK_BASIC_RATES_MASK)
2860 priv->staging_rxon.cck_basic_rates =
2861 ((priv->active_rate_basic &
2862 IWL_CCK_RATES_MASK) >> IWL_FIRST_CCK_RATE) & 0xF;
2863 else
2864 priv->staging_rxon.cck_basic_rates =
2865 (IWL_CCK_BASIC_RATES_MASK >> IWL_FIRST_CCK_RATE) & 0xF;
2866
2867 if (priv->active_rate_basic & IWL_OFDM_BASIC_RATES_MASK)
2868 priv->staging_rxon.ofdm_basic_rates =
2869 ((priv->active_rate_basic &
2870 (IWL_OFDM_BASIC_RATES_MASK | IWL_RATE_6M_MASK)) >>
2871 IWL_FIRST_OFDM_RATE) & 0xFF;
2872 else
2873 priv->staging_rxon.ofdm_basic_rates =
2874 (IWL_OFDM_BASIC_RATES_MASK >> IWL_FIRST_OFDM_RATE) & 0xFF;
2875}
2876
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002877static void iwl4965_radio_kill_sw(struct iwl4965_priv *priv, int disable_radio)
Zhu Yib481de92007-09-25 17:54:57 -07002878{
2879 unsigned long flags;
2880
2881 if (!!disable_radio == test_bit(STATUS_RF_KILL_SW, &priv->status))
2882 return;
2883
2884 IWL_DEBUG_RF_KILL("Manual SW RF KILL set to: RADIO %s\n",
2885 disable_radio ? "OFF" : "ON");
2886
2887 if (disable_radio) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002888 iwl4965_scan_cancel(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002889 /* FIXME: This is a workaround for AP */
2890 if (priv->iw_mode != IEEE80211_IF_TYPE_AP) {
2891 spin_lock_irqsave(&priv->lock, flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002892 iwl4965_write32(priv, CSR_UCODE_DRV_GP1_SET,
Zhu Yib481de92007-09-25 17:54:57 -07002893 CSR_UCODE_SW_BIT_RFKILL);
2894 spin_unlock_irqrestore(&priv->lock, flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002895 iwl4965_send_card_state(priv, CARD_STATE_CMD_DISABLE, 0);
Zhu Yib481de92007-09-25 17:54:57 -07002896 set_bit(STATUS_RF_KILL_SW, &priv->status);
2897 }
2898 return;
2899 }
2900
2901 spin_lock_irqsave(&priv->lock, flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002902 iwl4965_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
Zhu Yib481de92007-09-25 17:54:57 -07002903
2904 clear_bit(STATUS_RF_KILL_SW, &priv->status);
2905 spin_unlock_irqrestore(&priv->lock, flags);
2906
2907 /* wake up ucode */
2908 msleep(10);
2909
2910 spin_lock_irqsave(&priv->lock, flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002911 iwl4965_read32(priv, CSR_UCODE_DRV_GP1);
2912 if (!iwl4965_grab_nic_access(priv))
2913 iwl4965_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002914 spin_unlock_irqrestore(&priv->lock, flags);
2915
2916 if (test_bit(STATUS_RF_KILL_HW, &priv->status)) {
2917 IWL_DEBUG_RF_KILL("Can not turn radio back on - "
2918 "disabled by HW switch\n");
2919 return;
2920 }
2921
2922 queue_work(priv->workqueue, &priv->restart);
2923 return;
2924}
2925
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002926void iwl4965_set_decrypted_flag(struct iwl4965_priv *priv, struct sk_buff *skb,
Zhu Yib481de92007-09-25 17:54:57 -07002927 u32 decrypt_res, struct ieee80211_rx_status *stats)
2928{
2929 u16 fc =
2930 le16_to_cpu(((struct ieee80211_hdr *)skb->data)->frame_control);
2931
2932 if (priv->active_rxon.filter_flags & RXON_FILTER_DIS_DECRYPT_MSK)
2933 return;
2934
2935 if (!(fc & IEEE80211_FCTL_PROTECTED))
2936 return;
2937
2938 IWL_DEBUG_RX("decrypt_res:0x%x\n", decrypt_res);
2939 switch (decrypt_res & RX_RES_STATUS_SEC_TYPE_MSK) {
2940 case RX_RES_STATUS_SEC_TYPE_TKIP:
2941 if ((decrypt_res & RX_RES_STATUS_DECRYPT_TYPE_MSK) ==
2942 RX_RES_STATUS_BAD_ICV_MIC)
2943 stats->flag |= RX_FLAG_MMIC_ERROR;
2944 case RX_RES_STATUS_SEC_TYPE_WEP:
2945 case RX_RES_STATUS_SEC_TYPE_CCMP:
2946 if ((decrypt_res & RX_RES_STATUS_DECRYPT_TYPE_MSK) ==
2947 RX_RES_STATUS_DECRYPT_OK) {
2948 IWL_DEBUG_RX("hw decrypt successfully!!!\n");
2949 stats->flag |= RX_FLAG_DECRYPTED;
2950 }
2951 break;
2952
2953 default:
2954 break;
2955 }
2956}
2957
Zhu Yib481de92007-09-25 17:54:57 -07002958
2959#define IWL_PACKET_RETRY_TIME HZ
2960
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002961int iwl4965_is_duplicate_packet(struct iwl4965_priv *priv, struct ieee80211_hdr *header)
Zhu Yib481de92007-09-25 17:54:57 -07002962{
2963 u16 sc = le16_to_cpu(header->seq_ctrl);
2964 u16 seq = (sc & IEEE80211_SCTL_SEQ) >> 4;
2965 u16 frag = sc & IEEE80211_SCTL_FRAG;
2966 u16 *last_seq, *last_frag;
2967 unsigned long *last_time;
2968
2969 switch (priv->iw_mode) {
2970 case IEEE80211_IF_TYPE_IBSS:{
2971 struct list_head *p;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002972 struct iwl4965_ibss_seq *entry = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07002973 u8 *mac = header->addr2;
2974 int index = mac[5] & (IWL_IBSS_MAC_HASH_SIZE - 1);
2975
2976 __list_for_each(p, &priv->ibss_mac_hash[index]) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002977 entry = list_entry(p, struct iwl4965_ibss_seq, list);
Zhu Yib481de92007-09-25 17:54:57 -07002978 if (!compare_ether_addr(entry->mac, mac))
2979 break;
2980 }
2981 if (p == &priv->ibss_mac_hash[index]) {
2982 entry = kzalloc(sizeof(*entry), GFP_ATOMIC);
2983 if (!entry) {
Ian Schrambc434dd2007-10-25 17:15:29 +08002984 IWL_ERROR("Cannot malloc new mac entry\n");
Zhu Yib481de92007-09-25 17:54:57 -07002985 return 0;
2986 }
2987 memcpy(entry->mac, mac, ETH_ALEN);
2988 entry->seq_num = seq;
2989 entry->frag_num = frag;
2990 entry->packet_time = jiffies;
Ian Schrambc434dd2007-10-25 17:15:29 +08002991 list_add(&entry->list, &priv->ibss_mac_hash[index]);
Zhu Yib481de92007-09-25 17:54:57 -07002992 return 0;
2993 }
2994 last_seq = &entry->seq_num;
2995 last_frag = &entry->frag_num;
2996 last_time = &entry->packet_time;
2997 break;
2998 }
2999 case IEEE80211_IF_TYPE_STA:
3000 last_seq = &priv->last_seq_num;
3001 last_frag = &priv->last_frag_num;
3002 last_time = &priv->last_packet_time;
3003 break;
3004 default:
3005 return 0;
3006 }
3007 if ((*last_seq == seq) &&
3008 time_after(*last_time + IWL_PACKET_RETRY_TIME, jiffies)) {
3009 if (*last_frag == frag)
3010 goto drop;
3011 if (*last_frag + 1 != frag)
3012 /* out-of-order fragment */
3013 goto drop;
3014 } else
3015 *last_seq = seq;
3016
3017 *last_frag = frag;
3018 *last_time = jiffies;
3019 return 0;
3020
3021 drop:
3022 return 1;
3023}
3024
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08003025#ifdef CONFIG_IWL4965_SPECTRUM_MEASUREMENT
Zhu Yib481de92007-09-25 17:54:57 -07003026
3027#include "iwl-spectrum.h"
3028
3029#define BEACON_TIME_MASK_LOW 0x00FFFFFF
3030#define BEACON_TIME_MASK_HIGH 0xFF000000
3031#define TIME_UNIT 1024
3032
3033/*
3034 * extended beacon time format
3035 * time in usec will be changed into a 32-bit value in 8:24 format
3036 * the high 1 byte is the beacon counts
3037 * the lower 3 bytes is the time in usec within one beacon interval
3038 */
3039
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003040static u32 iwl4965_usecs_to_beacons(u32 usec, u32 beacon_interval)
Zhu Yib481de92007-09-25 17:54:57 -07003041{
3042 u32 quot;
3043 u32 rem;
3044 u32 interval = beacon_interval * 1024;
3045
3046 if (!interval || !usec)
3047 return 0;
3048
3049 quot = (usec / interval) & (BEACON_TIME_MASK_HIGH >> 24);
3050 rem = (usec % interval) & BEACON_TIME_MASK_LOW;
3051
3052 return (quot << 24) + rem;
3053}
3054
3055/* base is usually what we get from ucode with each received frame,
3056 * the same as HW timer counter counting down
3057 */
3058
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003059static __le32 iwl4965_add_beacon_time(u32 base, u32 addon, u32 beacon_interval)
Zhu Yib481de92007-09-25 17:54:57 -07003060{
3061 u32 base_low = base & BEACON_TIME_MASK_LOW;
3062 u32 addon_low = addon & BEACON_TIME_MASK_LOW;
3063 u32 interval = beacon_interval * TIME_UNIT;
3064 u32 res = (base & BEACON_TIME_MASK_HIGH) +
3065 (addon & BEACON_TIME_MASK_HIGH);
3066
3067 if (base_low > addon_low)
3068 res += base_low - addon_low;
3069 else if (base_low < addon_low) {
3070 res += interval + base_low - addon_low;
3071 res += (1 << 24);
3072 } else
3073 res += (1 << 24);
3074
3075 return cpu_to_le32(res);
3076}
3077
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003078static int iwl4965_get_measurement(struct iwl4965_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07003079 struct ieee80211_measurement_params *params,
3080 u8 type)
3081{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003082 struct iwl4965_spectrum_cmd spectrum;
3083 struct iwl4965_rx_packet *res;
3084 struct iwl4965_host_cmd cmd = {
Zhu Yib481de92007-09-25 17:54:57 -07003085 .id = REPLY_SPECTRUM_MEASUREMENT_CMD,
3086 .data = (void *)&spectrum,
3087 .meta.flags = CMD_WANT_SKB,
3088 };
3089 u32 add_time = le64_to_cpu(params->start_time);
3090 int rc;
3091 int spectrum_resp_status;
3092 int duration = le16_to_cpu(params->duration);
3093
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003094 if (iwl4965_is_associated(priv))
Zhu Yib481de92007-09-25 17:54:57 -07003095 add_time =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003096 iwl4965_usecs_to_beacons(
Zhu Yib481de92007-09-25 17:54:57 -07003097 le64_to_cpu(params->start_time) - priv->last_tsf,
3098 le16_to_cpu(priv->rxon_timing.beacon_interval));
3099
3100 memset(&spectrum, 0, sizeof(spectrum));
3101
3102 spectrum.channel_count = cpu_to_le16(1);
3103 spectrum.flags =
3104 RXON_FLG_TSF2HOST_MSK | RXON_FLG_ANT_A_MSK | RXON_FLG_DIS_DIV_MSK;
3105 spectrum.filter_flags = MEASUREMENT_FILTER_FLAG;
3106 cmd.len = sizeof(spectrum);
3107 spectrum.len = cpu_to_le16(cmd.len - sizeof(spectrum.len));
3108
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003109 if (iwl4965_is_associated(priv))
Zhu Yib481de92007-09-25 17:54:57 -07003110 spectrum.start_time =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003111 iwl4965_add_beacon_time(priv->last_beacon_time,
Zhu Yib481de92007-09-25 17:54:57 -07003112 add_time,
3113 le16_to_cpu(priv->rxon_timing.beacon_interval));
3114 else
3115 spectrum.start_time = 0;
3116
3117 spectrum.channels[0].duration = cpu_to_le32(duration * TIME_UNIT);
3118 spectrum.channels[0].channel = params->channel;
3119 spectrum.channels[0].type = type;
3120 if (priv->active_rxon.flags & RXON_FLG_BAND_24G_MSK)
3121 spectrum.flags |= RXON_FLG_BAND_24G_MSK |
3122 RXON_FLG_AUTO_DETECT_MSK | RXON_FLG_TGG_PROTECT_MSK;
3123
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003124 rc = iwl4965_send_cmd_sync(priv, &cmd);
Zhu Yib481de92007-09-25 17:54:57 -07003125 if (rc)
3126 return rc;
3127
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003128 res = (struct iwl4965_rx_packet *)cmd.meta.u.skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07003129 if (res->hdr.flags & IWL_CMD_FAILED_MSK) {
3130 IWL_ERROR("Bad return from REPLY_RX_ON_ASSOC command\n");
3131 rc = -EIO;
3132 }
3133
3134 spectrum_resp_status = le16_to_cpu(res->u.spectrum.status);
3135 switch (spectrum_resp_status) {
3136 case 0: /* Command will be handled */
3137 if (res->u.spectrum.id != 0xff) {
3138 IWL_DEBUG_INFO
3139 ("Replaced existing measurement: %d\n",
3140 res->u.spectrum.id);
3141 priv->measurement_status &= ~MEASUREMENT_READY;
3142 }
3143 priv->measurement_status |= MEASUREMENT_ACTIVE;
3144 rc = 0;
3145 break;
3146
3147 case 1: /* Command will not be handled */
3148 rc = -EAGAIN;
3149 break;
3150 }
3151
3152 dev_kfree_skb_any(cmd.meta.u.skb);
3153
3154 return rc;
3155}
3156#endif
3157
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003158static void iwl4965_txstatus_to_ieee(struct iwl4965_priv *priv,
3159 struct iwl4965_tx_info *tx_sta)
Zhu Yib481de92007-09-25 17:54:57 -07003160{
3161
3162 tx_sta->status.ack_signal = 0;
3163 tx_sta->status.excessive_retries = 0;
3164 tx_sta->status.queue_length = 0;
3165 tx_sta->status.queue_number = 0;
3166
3167 if (in_interrupt())
3168 ieee80211_tx_status_irqsafe(priv->hw,
3169 tx_sta->skb[0], &(tx_sta->status));
3170 else
3171 ieee80211_tx_status(priv->hw,
3172 tx_sta->skb[0], &(tx_sta->status));
3173
3174 tx_sta->skb[0] = NULL;
3175}
3176
3177/**
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003178 * iwl4965_tx_queue_reclaim - Reclaim Tx queue entries already Tx'd
Zhu Yib481de92007-09-25 17:54:57 -07003179 *
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003180 * When FW advances 'R' index, all entries between old and new 'R' index
3181 * need to be reclaimed. As result, some free space forms. If there is
3182 * enough free space (> low mark), wake the stack that feeds us.
Zhu Yib481de92007-09-25 17:54:57 -07003183 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003184int iwl4965_tx_queue_reclaim(struct iwl4965_priv *priv, int txq_id, int index)
Zhu Yib481de92007-09-25 17:54:57 -07003185{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003186 struct iwl4965_tx_queue *txq = &priv->txq[txq_id];
3187 struct iwl4965_queue *q = &txq->q;
Zhu Yib481de92007-09-25 17:54:57 -07003188 int nfreed = 0;
3189
3190 if ((index >= q->n_bd) || (x2_queue_used(q, index) == 0)) {
3191 IWL_ERROR("Read index for DMA queue txq id (%d), index %d, "
3192 "is out of range [0-%d] %d %d.\n", txq_id,
Tomas Winklerfc4b6852007-10-25 17:15:24 +08003193 index, q->n_bd, q->write_ptr, q->read_ptr);
Zhu Yib481de92007-09-25 17:54:57 -07003194 return 0;
3195 }
3196
Tomas Winklerc54b6792008-03-06 17:36:53 -08003197 for (index = iwl_queue_inc_wrap(index, q->n_bd);
Tomas Winklerfc4b6852007-10-25 17:15:24 +08003198 q->read_ptr != index;
Tomas Winklerc54b6792008-03-06 17:36:53 -08003199 q->read_ptr = iwl_queue_inc_wrap(q->read_ptr, q->n_bd)) {
Zhu Yib481de92007-09-25 17:54:57 -07003200 if (txq_id != IWL_CMD_QUEUE_NUM) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003201 iwl4965_txstatus_to_ieee(priv,
Tomas Winklerfc4b6852007-10-25 17:15:24 +08003202 &(txq->txb[txq->q.read_ptr]));
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003203 iwl4965_hw_txq_free_tfd(priv, txq);
Zhu Yib481de92007-09-25 17:54:57 -07003204 } else if (nfreed > 1) {
3205 IWL_ERROR("HCMD skipped: index (%d) %d %d\n", index,
Tomas Winklerfc4b6852007-10-25 17:15:24 +08003206 q->write_ptr, q->read_ptr);
Zhu Yib481de92007-09-25 17:54:57 -07003207 queue_work(priv->workqueue, &priv->restart);
3208 }
3209 nfreed++;
3210 }
3211
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003212/* if (iwl4965_queue_space(q) > q->low_mark && (txq_id >= 0) &&
Zhu Yib481de92007-09-25 17:54:57 -07003213 (txq_id != IWL_CMD_QUEUE_NUM) &&
3214 priv->mac80211_registered)
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003215 ieee80211_wake_queue(priv->hw, txq_id); */
Zhu Yib481de92007-09-25 17:54:57 -07003216
3217
3218 return nfreed;
3219}
3220
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003221static int iwl4965_is_tx_success(u32 status)
Zhu Yib481de92007-09-25 17:54:57 -07003222{
3223 status &= TX_STATUS_MSK;
3224 return (status == TX_STATUS_SUCCESS)
3225 || (status == TX_STATUS_DIRECT_DONE);
3226}
3227
3228/******************************************************************************
3229 *
3230 * Generic RX handler implementations
3231 *
3232 ******************************************************************************/
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08003233#ifdef CONFIG_IWL4965_HT
Zhu Yib481de92007-09-25 17:54:57 -07003234
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003235static inline int iwl4965_get_ra_sta_id(struct iwl4965_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07003236 struct ieee80211_hdr *hdr)
3237{
3238 if (priv->iw_mode == IEEE80211_IF_TYPE_STA)
3239 return IWL_AP_ID;
3240 else {
3241 u8 *da = ieee80211_get_DA(hdr);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003242 return iwl4965_hw_find_station(priv, da);
Zhu Yib481de92007-09-25 17:54:57 -07003243 }
3244}
3245
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003246static struct ieee80211_hdr *iwl4965_tx_queue_get_hdr(
3247 struct iwl4965_priv *priv, int txq_id, int idx)
Zhu Yib481de92007-09-25 17:54:57 -07003248{
3249 if (priv->txq[txq_id].txb[idx].skb[0])
3250 return (struct ieee80211_hdr *)priv->txq[txq_id].
3251 txb[idx].skb[0]->data;
3252 return NULL;
3253}
3254
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003255static inline u32 iwl4965_get_scd_ssn(struct iwl4965_tx_resp *tx_resp)
Zhu Yib481de92007-09-25 17:54:57 -07003256{
3257 __le32 *scd_ssn = (__le32 *)((u32 *)&tx_resp->status +
3258 tx_resp->frame_count);
3259 return le32_to_cpu(*scd_ssn) & MAX_SN;
3260
3261}
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003262
3263/**
3264 * iwl4965_tx_status_reply_tx - Handle Tx rspnse for frames in aggregation queue
3265 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003266static int iwl4965_tx_status_reply_tx(struct iwl4965_priv *priv,
3267 struct iwl4965_ht_agg *agg,
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003268 struct iwl4965_tx_resp_agg *tx_resp,
Zhu Yib481de92007-09-25 17:54:57 -07003269 u16 start_idx)
3270{
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003271 u16 status;
3272 struct agg_tx_status *frame_status = &tx_resp->status;
Zhu Yib481de92007-09-25 17:54:57 -07003273 struct ieee80211_tx_status *tx_status = NULL;
3274 struct ieee80211_hdr *hdr = NULL;
3275 int i, sh;
3276 int txq_id, idx;
3277 u16 seq;
3278
3279 if (agg->wait_for_ba)
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003280 IWL_DEBUG_TX_REPLY("got tx response w/o block-ack\n");
Zhu Yib481de92007-09-25 17:54:57 -07003281
3282 agg->frame_count = tx_resp->frame_count;
3283 agg->start_idx = start_idx;
3284 agg->rate_n_flags = le32_to_cpu(tx_resp->rate_n_flags);
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003285 agg->bitmap = 0;
Zhu Yib481de92007-09-25 17:54:57 -07003286
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003287 /* # frames attempted by Tx command */
Zhu Yib481de92007-09-25 17:54:57 -07003288 if (agg->frame_count == 1) {
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003289 /* Only one frame was attempted; no block-ack will arrive */
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003290 status = le16_to_cpu(frame_status[0].status);
3291 seq = le16_to_cpu(frame_status[0].sequence);
3292 idx = SEQ_TO_INDEX(seq);
3293 txq_id = SEQ_TO_QUEUE(seq);
Zhu Yib481de92007-09-25 17:54:57 -07003294
Zhu Yib481de92007-09-25 17:54:57 -07003295 /* FIXME: code repetition */
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003296 IWL_DEBUG_TX_REPLY("FrameCnt = %d, StartIdx=%d idx=%d\n",
3297 agg->frame_count, agg->start_idx, idx);
Zhu Yib481de92007-09-25 17:54:57 -07003298
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003299 tx_status = &(priv->txq[txq_id].txb[idx].status);
Zhu Yib481de92007-09-25 17:54:57 -07003300 tx_status->retry_count = tx_resp->failure_frame;
3301 tx_status->queue_number = status & 0xff;
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003302 tx_status->queue_length = tx_resp->failure_rts;
3303 tx_status->control.flags &= ~IEEE80211_TXCTL_AMPDU;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003304 tx_status->flags = iwl4965_is_tx_success(status)?
Zhu Yib481de92007-09-25 17:54:57 -07003305 IEEE80211_TX_STATUS_ACK : 0;
Ron Rindjunsky4c424e42008-03-04 18:09:27 -08003306 iwl4965_hwrate_to_tx_control(priv,
3307 le32_to_cpu(tx_resp->rate_n_flags),
3308 &tx_status->control);
Zhu Yib481de92007-09-25 17:54:57 -07003309 /* FIXME: code repetition end */
3310
3311 IWL_DEBUG_TX_REPLY("1 Frame 0x%x failure :%d\n",
3312 status & 0xff, tx_resp->failure_frame);
3313 IWL_DEBUG_TX_REPLY("Rate Info rate_n_flags=%x\n",
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003314 iwl4965_hw_get_rate_n_flags(tx_resp->rate_n_flags));
Zhu Yib481de92007-09-25 17:54:57 -07003315
3316 agg->wait_for_ba = 0;
3317 } else {
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003318 /* Two or more frames were attempted; expect block-ack */
Zhu Yib481de92007-09-25 17:54:57 -07003319 u64 bitmap = 0;
3320 int start = agg->start_idx;
3321
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003322 /* Construct bit-map of pending frames within Tx window */
Zhu Yib481de92007-09-25 17:54:57 -07003323 for (i = 0; i < agg->frame_count; i++) {
3324 u16 sc;
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003325 status = le16_to_cpu(frame_status[i].status);
3326 seq = le16_to_cpu(frame_status[i].sequence);
Zhu Yib481de92007-09-25 17:54:57 -07003327 idx = SEQ_TO_INDEX(seq);
3328 txq_id = SEQ_TO_QUEUE(seq);
3329
3330 if (status & (AGG_TX_STATE_FEW_BYTES_MSK |
3331 AGG_TX_STATE_ABORT_MSK))
3332 continue;
3333
3334 IWL_DEBUG_TX_REPLY("FrameCnt = %d, txq_id=%d idx=%d\n",
3335 agg->frame_count, txq_id, idx);
3336
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003337 hdr = iwl4965_tx_queue_get_hdr(priv, txq_id, idx);
Zhu Yib481de92007-09-25 17:54:57 -07003338
3339 sc = le16_to_cpu(hdr->seq_ctrl);
3340 if (idx != (SEQ_TO_SN(sc) & 0xff)) {
3341 IWL_ERROR("BUG_ON idx doesn't match seq control"
3342 " idx=%d, seq_idx=%d, seq=%d\n",
3343 idx, SEQ_TO_SN(sc),
3344 hdr->seq_ctrl);
3345 return -1;
3346 }
3347
3348 IWL_DEBUG_TX_REPLY("AGG Frame i=%d idx %d seq=%d\n",
3349 i, idx, SEQ_TO_SN(sc));
3350
3351 sh = idx - start;
3352 if (sh > 64) {
3353 sh = (start - idx) + 0xff;
3354 bitmap = bitmap << sh;
3355 sh = 0;
3356 start = idx;
3357 } else if (sh < -64)
3358 sh = 0xff - (start - idx);
3359 else if (sh < 0) {
3360 sh = start - idx;
3361 start = idx;
3362 bitmap = bitmap << sh;
3363 sh = 0;
3364 }
3365 bitmap |= (1 << sh);
3366 IWL_DEBUG_TX_REPLY("start=%d bitmap=0x%x\n",
3367 start, (u32)(bitmap & 0xFFFFFFFF));
3368 }
3369
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003370 agg->bitmap = bitmap;
Zhu Yib481de92007-09-25 17:54:57 -07003371 agg->start_idx = start;
3372 agg->rate_n_flags = le32_to_cpu(tx_resp->rate_n_flags);
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003373 IWL_DEBUG_TX_REPLY("Frames %d start_idx=%d bitmap=0x%llx\n",
Zhu Yib481de92007-09-25 17:54:57 -07003374 agg->frame_count, agg->start_idx,
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003375 agg->bitmap);
Zhu Yib481de92007-09-25 17:54:57 -07003376
3377 if (bitmap)
3378 agg->wait_for_ba = 1;
3379 }
3380 return 0;
3381}
3382#endif
Zhu Yib481de92007-09-25 17:54:57 -07003383
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003384/**
3385 * iwl4965_rx_reply_tx - Handle standard (non-aggregation) Tx response
3386 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003387static void iwl4965_rx_reply_tx(struct iwl4965_priv *priv,
3388 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003389{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003390 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07003391 u16 sequence = le16_to_cpu(pkt->hdr.sequence);
3392 int txq_id = SEQ_TO_QUEUE(sequence);
3393 int index = SEQ_TO_INDEX(sequence);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003394 struct iwl4965_tx_queue *txq = &priv->txq[txq_id];
Zhu Yib481de92007-09-25 17:54:57 -07003395 struct ieee80211_tx_status *tx_status;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003396 struct iwl4965_tx_resp *tx_resp = (void *)&pkt->u.raw[0];
Zhu Yib481de92007-09-25 17:54:57 -07003397 u32 status = le32_to_cpu(tx_resp->status);
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08003398#ifdef CONFIG_IWL4965_HT
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003399 int tid = MAX_TID_COUNT, sta_id = IWL_INVALID_STATION;
3400 struct ieee80211_hdr *hdr;
3401 __le16 *qc;
Zhu Yib481de92007-09-25 17:54:57 -07003402#endif
3403
3404 if ((index >= txq->q.n_bd) || (x2_queue_used(&txq->q, index) == 0)) {
3405 IWL_ERROR("Read index for DMA queue txq_id (%d) index %d "
3406 "is out of range [0-%d] %d %d\n", txq_id,
Tomas Winklerfc4b6852007-10-25 17:15:24 +08003407 index, txq->q.n_bd, txq->q.write_ptr,
3408 txq->q.read_ptr);
Zhu Yib481de92007-09-25 17:54:57 -07003409 return;
3410 }
3411
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08003412#ifdef CONFIG_IWL4965_HT
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003413 hdr = iwl4965_tx_queue_get_hdr(priv, txq_id, index);
3414 qc = ieee80211_get_qos_ctrl(hdr);
Zhu Yib481de92007-09-25 17:54:57 -07003415
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003416 if (qc)
Zhu Yib481de92007-09-25 17:54:57 -07003417 tid = le16_to_cpu(*qc) & 0xf;
3418
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003419 sta_id = iwl4965_get_ra_sta_id(priv, hdr);
3420 if (txq->sched_retry && unlikely(sta_id == IWL_INVALID_STATION)) {
3421 IWL_ERROR("Station not known\n");
3422 return;
3423 }
3424
3425 if (txq->sched_retry) {
3426 const u32 scd_ssn = iwl4965_get_scd_ssn(tx_resp);
3427 struct iwl4965_ht_agg *agg = NULL;
3428
3429 if (!qc)
Zhu Yib481de92007-09-25 17:54:57 -07003430 return;
Zhu Yib481de92007-09-25 17:54:57 -07003431
3432 agg = &priv->stations[sta_id].tid[tid].agg;
3433
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003434 iwl4965_tx_status_reply_tx(priv, agg,
3435 (struct iwl4965_tx_resp_agg *)tx_resp, index);
Zhu Yib481de92007-09-25 17:54:57 -07003436
3437 if ((tx_resp->frame_count == 1) &&
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003438 !iwl4965_is_tx_success(status)) {
Zhu Yib481de92007-09-25 17:54:57 -07003439 /* TODO: send BAR */
3440 }
3441
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003442 if (txq->q.read_ptr != (scd_ssn & 0xff)) {
3443 int freed;
Tomas Winklerc54b6792008-03-06 17:36:53 -08003444 index = iwl_queue_dec_wrap(scd_ssn & 0xff, txq->q.n_bd);
Zhu Yib481de92007-09-25 17:54:57 -07003445 IWL_DEBUG_TX_REPLY("Retry scheduler reclaim scd_ssn "
3446 "%d index %d\n", scd_ssn , index);
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003447 freed = iwl4965_tx_queue_reclaim(priv, txq_id, index);
3448 priv->stations[sta_id].tid[tid].tfds_in_queue -= freed;
3449
3450 if (iwl4965_queue_space(&txq->q) > txq->q.low_mark &&
3451 txq_id >= 0 && priv->mac80211_registered &&
3452 agg->state != IWL_EMPTYING_HW_QUEUE_DELBA)
3453 ieee80211_wake_queue(priv->hw, txq_id);
3454
3455 iwl4965_check_empty_hw_queue(priv, sta_id, tid, txq_id);
Zhu Yib481de92007-09-25 17:54:57 -07003456 }
3457 } else {
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08003458#endif /* CONFIG_IWL4965_HT */
Tomas Winklerfc4b6852007-10-25 17:15:24 +08003459 tx_status = &(txq->txb[txq->q.read_ptr].status);
Zhu Yib481de92007-09-25 17:54:57 -07003460
3461 tx_status->retry_count = tx_resp->failure_frame;
3462 tx_status->queue_number = status;
3463 tx_status->queue_length = tx_resp->bt_kill_count;
3464 tx_status->queue_length |= tx_resp->failure_rts;
Zhu Yib481de92007-09-25 17:54:57 -07003465 tx_status->flags =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003466 iwl4965_is_tx_success(status) ? IEEE80211_TX_STATUS_ACK : 0;
Ron Rindjunsky4c424e42008-03-04 18:09:27 -08003467 iwl4965_hwrate_to_tx_control(priv, le32_to_cpu(tx_resp->rate_n_flags),
3468 &tx_status->control);
Zhu Yib481de92007-09-25 17:54:57 -07003469
Zhu Yib481de92007-09-25 17:54:57 -07003470 IWL_DEBUG_TX("Tx queue %d Status %s (0x%08x) rate_n_flags 0x%x "
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003471 "retries %d\n", txq_id, iwl4965_get_tx_fail_reason(status),
Zhu Yib481de92007-09-25 17:54:57 -07003472 status, le32_to_cpu(tx_resp->rate_n_flags),
3473 tx_resp->failure_frame);
3474
3475 IWL_DEBUG_TX_REPLY("Tx queue reclaim %d\n", index);
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003476 if (index != -1) {
3477 int freed = iwl4965_tx_queue_reclaim(priv, txq_id, index);
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08003478#ifdef CONFIG_IWL4965_HT
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003479 if (tid != MAX_TID_COUNT)
3480 priv->stations[sta_id].tid[tid].tfds_in_queue -= freed;
3481 if (iwl4965_queue_space(&txq->q) > txq->q.low_mark &&
3482 (txq_id >= 0) &&
3483 priv->mac80211_registered)
3484 ieee80211_wake_queue(priv->hw, txq_id);
3485 if (tid != MAX_TID_COUNT)
3486 iwl4965_check_empty_hw_queue(priv, sta_id, tid, txq_id);
3487#endif
Zhu Yib481de92007-09-25 17:54:57 -07003488 }
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003489#ifdef CONFIG_IWL4965_HT
3490 }
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08003491#endif /* CONFIG_IWL4965_HT */
Zhu Yib481de92007-09-25 17:54:57 -07003492
3493 if (iwl_check_bits(status, TX_ABORT_REQUIRED_MSK))
3494 IWL_ERROR("TODO: Implement Tx ABORT REQUIRED!!!\n");
3495}
3496
3497
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003498static void iwl4965_rx_reply_alive(struct iwl4965_priv *priv,
3499 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003500{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003501 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
3502 struct iwl4965_alive_resp *palive;
Zhu Yib481de92007-09-25 17:54:57 -07003503 struct delayed_work *pwork;
3504
3505 palive = &pkt->u.alive_frame;
3506
3507 IWL_DEBUG_INFO("Alive ucode status 0x%08X revision "
3508 "0x%01X 0x%01X\n",
3509 palive->is_valid, palive->ver_type,
3510 palive->ver_subtype);
3511
3512 if (palive->ver_subtype == INITIALIZE_SUBTYPE) {
3513 IWL_DEBUG_INFO("Initialization Alive received.\n");
3514 memcpy(&priv->card_alive_init,
3515 &pkt->u.alive_frame,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003516 sizeof(struct iwl4965_init_alive_resp));
Zhu Yib481de92007-09-25 17:54:57 -07003517 pwork = &priv->init_alive_start;
3518 } else {
3519 IWL_DEBUG_INFO("Runtime Alive received.\n");
3520 memcpy(&priv->card_alive, &pkt->u.alive_frame,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003521 sizeof(struct iwl4965_alive_resp));
Zhu Yib481de92007-09-25 17:54:57 -07003522 pwork = &priv->alive_start;
3523 }
3524
3525 /* We delay the ALIVE response by 5ms to
3526 * give the HW RF Kill time to activate... */
3527 if (palive->is_valid == UCODE_VALID_OK)
3528 queue_delayed_work(priv->workqueue, pwork,
3529 msecs_to_jiffies(5));
3530 else
3531 IWL_WARNING("uCode did not respond OK.\n");
3532}
3533
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003534static void iwl4965_rx_reply_add_sta(struct iwl4965_priv *priv,
3535 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003536{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003537 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07003538
3539 IWL_DEBUG_RX("Received REPLY_ADD_STA: 0x%02X\n", pkt->u.status);
3540 return;
3541}
3542
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003543static void iwl4965_rx_reply_error(struct iwl4965_priv *priv,
3544 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003545{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003546 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07003547
3548 IWL_ERROR("Error Reply type 0x%08X cmd %s (0x%02X) "
3549 "seq 0x%04X ser 0x%08X\n",
3550 le32_to_cpu(pkt->u.err_resp.error_type),
3551 get_cmd_string(pkt->u.err_resp.cmd_id),
3552 pkt->u.err_resp.cmd_id,
3553 le16_to_cpu(pkt->u.err_resp.bad_cmd_seq_num),
3554 le32_to_cpu(pkt->u.err_resp.error_info));
3555}
3556
3557#define TX_STATUS_ENTRY(x) case TX_STATUS_FAIL_ ## x: return #x
3558
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003559static void iwl4965_rx_csa(struct iwl4965_priv *priv, struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003560{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003561 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
3562 struct iwl4965_rxon_cmd *rxon = (void *)&priv->active_rxon;
3563 struct iwl4965_csa_notification *csa = &(pkt->u.csa_notif);
Zhu Yib481de92007-09-25 17:54:57 -07003564 IWL_DEBUG_11H("CSA notif: channel %d, status %d\n",
3565 le16_to_cpu(csa->channel), le32_to_cpu(csa->status));
3566 rxon->channel = csa->channel;
3567 priv->staging_rxon.channel = csa->channel;
3568}
3569
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003570static void iwl4965_rx_spectrum_measure_notif(struct iwl4965_priv *priv,
3571 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003572{
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08003573#ifdef CONFIG_IWL4965_SPECTRUM_MEASUREMENT
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003574 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
3575 struct iwl4965_spectrum_notification *report = &(pkt->u.spectrum_notif);
Zhu Yib481de92007-09-25 17:54:57 -07003576
3577 if (!report->state) {
3578 IWL_DEBUG(IWL_DL_11H | IWL_DL_INFO,
3579 "Spectrum Measure Notification: Start\n");
3580 return;
3581 }
3582
3583 memcpy(&priv->measure_report, report, sizeof(*report));
3584 priv->measurement_status |= MEASUREMENT_READY;
3585#endif
3586}
3587
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003588static void iwl4965_rx_pm_sleep_notif(struct iwl4965_priv *priv,
3589 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003590{
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08003591#ifdef CONFIG_IWL4965_DEBUG
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003592 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
3593 struct iwl4965_sleep_notification *sleep = &(pkt->u.sleep_notif);
Zhu Yib481de92007-09-25 17:54:57 -07003594 IWL_DEBUG_RX("sleep mode: %d, src: %d\n",
3595 sleep->pm_sleep_mode, sleep->pm_wakeup_src);
3596#endif
3597}
3598
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003599static void iwl4965_rx_pm_debug_statistics_notif(struct iwl4965_priv *priv,
3600 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003601{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003602 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07003603 IWL_DEBUG_RADIO("Dumping %d bytes of unhandled "
3604 "notification for %s:\n",
3605 le32_to_cpu(pkt->len), get_cmd_string(pkt->hdr.cmd));
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003606 iwl4965_print_hex_dump(IWL_DL_RADIO, pkt->u.raw, le32_to_cpu(pkt->len));
Zhu Yib481de92007-09-25 17:54:57 -07003607}
3608
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003609static void iwl4965_bg_beacon_update(struct work_struct *work)
Zhu Yib481de92007-09-25 17:54:57 -07003610{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003611 struct iwl4965_priv *priv =
3612 container_of(work, struct iwl4965_priv, beacon_update);
Zhu Yib481de92007-09-25 17:54:57 -07003613 struct sk_buff *beacon;
3614
3615 /* Pull updated AP beacon from mac80211. will fail if not in AP mode */
Johannes Berg32bfd352007-12-19 01:31:26 +01003616 beacon = ieee80211_beacon_get(priv->hw, priv->vif, NULL);
Zhu Yib481de92007-09-25 17:54:57 -07003617
3618 if (!beacon) {
3619 IWL_ERROR("update beacon failed\n");
3620 return;
3621 }
3622
3623 mutex_lock(&priv->mutex);
3624 /* new beacon skb is allocated every time; dispose previous.*/
3625 if (priv->ibss_beacon)
3626 dev_kfree_skb(priv->ibss_beacon);
3627
3628 priv->ibss_beacon = beacon;
3629 mutex_unlock(&priv->mutex);
3630
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003631 iwl4965_send_beacon_cmd(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003632}
3633
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003634static void iwl4965_rx_beacon_notif(struct iwl4965_priv *priv,
3635 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003636{
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08003637#ifdef CONFIG_IWL4965_DEBUG
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003638 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
3639 struct iwl4965_beacon_notif *beacon = &(pkt->u.beacon_status);
3640 u8 rate = iwl4965_hw_get_rate(beacon->beacon_notify_hdr.rate_n_flags);
Zhu Yib481de92007-09-25 17:54:57 -07003641
3642 IWL_DEBUG_RX("beacon status %x retries %d iss %d "
3643 "tsf %d %d rate %d\n",
3644 le32_to_cpu(beacon->beacon_notify_hdr.status) & TX_STATUS_MSK,
3645 beacon->beacon_notify_hdr.failure_frame,
3646 le32_to_cpu(beacon->ibss_mgr_status),
3647 le32_to_cpu(beacon->high_tsf),
3648 le32_to_cpu(beacon->low_tsf), rate);
3649#endif
3650
3651 if ((priv->iw_mode == IEEE80211_IF_TYPE_AP) &&
3652 (!test_bit(STATUS_EXIT_PENDING, &priv->status)))
3653 queue_work(priv->workqueue, &priv->beacon_update);
3654}
3655
3656/* Service response to REPLY_SCAN_CMD (0x80) */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003657static void iwl4965_rx_reply_scan(struct iwl4965_priv *priv,
3658 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003659{
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08003660#ifdef CONFIG_IWL4965_DEBUG
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003661 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
3662 struct iwl4965_scanreq_notification *notif =
3663 (struct iwl4965_scanreq_notification *)pkt->u.raw;
Zhu Yib481de92007-09-25 17:54:57 -07003664
3665 IWL_DEBUG_RX("Scan request status = 0x%x\n", notif->status);
3666#endif
3667}
3668
3669/* Service SCAN_START_NOTIFICATION (0x82) */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003670static void iwl4965_rx_scan_start_notif(struct iwl4965_priv *priv,
3671 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003672{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003673 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
3674 struct iwl4965_scanstart_notification *notif =
3675 (struct iwl4965_scanstart_notification *)pkt->u.raw;
Zhu Yib481de92007-09-25 17:54:57 -07003676 priv->scan_start_tsf = le32_to_cpu(notif->tsf_low);
3677 IWL_DEBUG_SCAN("Scan start: "
3678 "%d [802.11%s] "
3679 "(TSF: 0x%08X:%08X) - %d (beacon timer %u)\n",
3680 notif->channel,
3681 notif->band ? "bg" : "a",
3682 notif->tsf_high,
3683 notif->tsf_low, notif->status, notif->beacon_timer);
3684}
3685
3686/* Service SCAN_RESULTS_NOTIFICATION (0x83) */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003687static void iwl4965_rx_scan_results_notif(struct iwl4965_priv *priv,
3688 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003689{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003690 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
3691 struct iwl4965_scanresults_notification *notif =
3692 (struct iwl4965_scanresults_notification *)pkt->u.raw;
Zhu Yib481de92007-09-25 17:54:57 -07003693
3694 IWL_DEBUG_SCAN("Scan ch.res: "
3695 "%d [802.11%s] "
3696 "(TSF: 0x%08X:%08X) - %d "
3697 "elapsed=%lu usec (%dms since last)\n",
3698 notif->channel,
3699 notif->band ? "bg" : "a",
3700 le32_to_cpu(notif->tsf_high),
3701 le32_to_cpu(notif->tsf_low),
3702 le32_to_cpu(notif->statistics[0]),
3703 le32_to_cpu(notif->tsf_low) - priv->scan_start_tsf,
3704 jiffies_to_msecs(elapsed_jiffies
3705 (priv->last_scan_jiffies, jiffies)));
3706
3707 priv->last_scan_jiffies = jiffies;
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08003708 priv->next_scan_jiffies = 0;
Zhu Yib481de92007-09-25 17:54:57 -07003709}
3710
3711/* Service SCAN_COMPLETE_NOTIFICATION (0x84) */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003712static void iwl4965_rx_scan_complete_notif(struct iwl4965_priv *priv,
3713 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003714{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003715 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
3716 struct iwl4965_scancomplete_notification *scan_notif = (void *)pkt->u.raw;
Zhu Yib481de92007-09-25 17:54:57 -07003717
3718 IWL_DEBUG_SCAN("Scan complete: %d channels (TSF 0x%08X:%08X) - %d\n",
3719 scan_notif->scanned_channels,
3720 scan_notif->tsf_low,
3721 scan_notif->tsf_high, scan_notif->status);
3722
3723 /* The HW is no longer scanning */
3724 clear_bit(STATUS_SCAN_HW, &priv->status);
3725
3726 /* The scan completion notification came in, so kill that timer... */
3727 cancel_delayed_work(&priv->scan_check);
3728
3729 IWL_DEBUG_INFO("Scan pass on %sGHz took %dms\n",
3730 (priv->scan_bands == 2) ? "2.4" : "5.2",
3731 jiffies_to_msecs(elapsed_jiffies
3732 (priv->scan_pass_start, jiffies)));
3733
3734 /* Remove this scanned band from the list
3735 * of pending bands to scan */
3736 priv->scan_bands--;
3737
3738 /* If a request to abort was given, or the scan did not succeed
3739 * then we reset the scan state machine and terminate,
3740 * re-queuing another scan if one has been requested */
3741 if (test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
3742 IWL_DEBUG_INFO("Aborted scan completed.\n");
3743 clear_bit(STATUS_SCAN_ABORTING, &priv->status);
3744 } else {
3745 /* If there are more bands on this scan pass reschedule */
3746 if (priv->scan_bands > 0)
3747 goto reschedule;
3748 }
3749
3750 priv->last_scan_jiffies = jiffies;
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08003751 priv->next_scan_jiffies = 0;
Zhu Yib481de92007-09-25 17:54:57 -07003752 IWL_DEBUG_INFO("Setting scan to off\n");
3753
3754 clear_bit(STATUS_SCANNING, &priv->status);
3755
3756 IWL_DEBUG_INFO("Scan took %dms\n",
3757 jiffies_to_msecs(elapsed_jiffies(priv->scan_start, jiffies)));
3758
3759 queue_work(priv->workqueue, &priv->scan_completed);
3760
3761 return;
3762
3763reschedule:
3764 priv->scan_pass_start = jiffies;
3765 queue_work(priv->workqueue, &priv->request_scan);
3766}
3767
3768/* Handle notification from uCode that card's power state is changing
3769 * due to software, hardware, or critical temperature RFKILL */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003770static void iwl4965_rx_card_state_notif(struct iwl4965_priv *priv,
3771 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003772{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003773 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07003774 u32 flags = le32_to_cpu(pkt->u.card_state_notif.flags);
3775 unsigned long status = priv->status;
3776
3777 IWL_DEBUG_RF_KILL("Card state received: HW:%s SW:%s\n",
3778 (flags & HW_CARD_DISABLED) ? "Kill" : "On",
3779 (flags & SW_CARD_DISABLED) ? "Kill" : "On");
3780
3781 if (flags & (SW_CARD_DISABLED | HW_CARD_DISABLED |
3782 RF_CARD_DISABLED)) {
3783
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003784 iwl4965_write32(priv, CSR_UCODE_DRV_GP1_SET,
Zhu Yib481de92007-09-25 17:54:57 -07003785 CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
3786
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003787 if (!iwl4965_grab_nic_access(priv)) {
3788 iwl4965_write_direct32(
Zhu Yib481de92007-09-25 17:54:57 -07003789 priv, HBUS_TARG_MBX_C,
3790 HBUS_TARG_MBX_C_REG_BIT_CMD_BLOCKED);
3791
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003792 iwl4965_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003793 }
3794
3795 if (!(flags & RXON_CARD_DISABLED)) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003796 iwl4965_write32(priv, CSR_UCODE_DRV_GP1_CLR,
Zhu Yib481de92007-09-25 17:54:57 -07003797 CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003798 if (!iwl4965_grab_nic_access(priv)) {
3799 iwl4965_write_direct32(
Zhu Yib481de92007-09-25 17:54:57 -07003800 priv, HBUS_TARG_MBX_C,
3801 HBUS_TARG_MBX_C_REG_BIT_CMD_BLOCKED);
3802
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003803 iwl4965_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003804 }
3805 }
3806
3807 if (flags & RF_CARD_DISABLED) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003808 iwl4965_write32(priv, CSR_UCODE_DRV_GP1_SET,
Zhu Yib481de92007-09-25 17:54:57 -07003809 CSR_UCODE_DRV_GP1_REG_BIT_CT_KILL_EXIT);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003810 iwl4965_read32(priv, CSR_UCODE_DRV_GP1);
3811 if (!iwl4965_grab_nic_access(priv))
3812 iwl4965_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003813 }
3814 }
3815
3816 if (flags & HW_CARD_DISABLED)
3817 set_bit(STATUS_RF_KILL_HW, &priv->status);
3818 else
3819 clear_bit(STATUS_RF_KILL_HW, &priv->status);
3820
3821
3822 if (flags & SW_CARD_DISABLED)
3823 set_bit(STATUS_RF_KILL_SW, &priv->status);
3824 else
3825 clear_bit(STATUS_RF_KILL_SW, &priv->status);
3826
3827 if (!(flags & RXON_CARD_DISABLED))
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003828 iwl4965_scan_cancel(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003829
3830 if ((test_bit(STATUS_RF_KILL_HW, &status) !=
3831 test_bit(STATUS_RF_KILL_HW, &priv->status)) ||
3832 (test_bit(STATUS_RF_KILL_SW, &status) !=
3833 test_bit(STATUS_RF_KILL_SW, &priv->status)))
3834 queue_work(priv->workqueue, &priv->rf_kill);
3835 else
3836 wake_up_interruptible(&priv->wait_command_queue);
3837}
3838
3839/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003840 * iwl4965_setup_rx_handlers - Initialize Rx handler callbacks
Zhu Yib481de92007-09-25 17:54:57 -07003841 *
3842 * Setup the RX handlers for each of the reply types sent from the uCode
3843 * to the host.
3844 *
3845 * This function chains into the hardware specific files for them to setup
3846 * any hardware specific handlers as well.
3847 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003848static void iwl4965_setup_rx_handlers(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003849{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003850 priv->rx_handlers[REPLY_ALIVE] = iwl4965_rx_reply_alive;
3851 priv->rx_handlers[REPLY_ADD_STA] = iwl4965_rx_reply_add_sta;
3852 priv->rx_handlers[REPLY_ERROR] = iwl4965_rx_reply_error;
3853 priv->rx_handlers[CHANNEL_SWITCH_NOTIFICATION] = iwl4965_rx_csa;
Zhu Yib481de92007-09-25 17:54:57 -07003854 priv->rx_handlers[SPECTRUM_MEASURE_NOTIFICATION] =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003855 iwl4965_rx_spectrum_measure_notif;
3856 priv->rx_handlers[PM_SLEEP_NOTIFICATION] = iwl4965_rx_pm_sleep_notif;
Zhu Yib481de92007-09-25 17:54:57 -07003857 priv->rx_handlers[PM_DEBUG_STATISTIC_NOTIFIC] =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003858 iwl4965_rx_pm_debug_statistics_notif;
3859 priv->rx_handlers[BEACON_NOTIFICATION] = iwl4965_rx_beacon_notif;
Zhu Yib481de92007-09-25 17:54:57 -07003860
Ben Cahill9fbab512007-11-29 11:09:47 +08003861 /*
3862 * The same handler is used for both the REPLY to a discrete
3863 * statistics request from the host as well as for the periodic
3864 * statistics notifications (after received beacons) from the uCode.
Zhu Yib481de92007-09-25 17:54:57 -07003865 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003866 priv->rx_handlers[REPLY_STATISTICS_CMD] = iwl4965_hw_rx_statistics;
3867 priv->rx_handlers[STATISTICS_NOTIFICATION] = iwl4965_hw_rx_statistics;
Zhu Yib481de92007-09-25 17:54:57 -07003868
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003869 priv->rx_handlers[REPLY_SCAN_CMD] = iwl4965_rx_reply_scan;
3870 priv->rx_handlers[SCAN_START_NOTIFICATION] = iwl4965_rx_scan_start_notif;
Zhu Yib481de92007-09-25 17:54:57 -07003871 priv->rx_handlers[SCAN_RESULTS_NOTIFICATION] =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003872 iwl4965_rx_scan_results_notif;
Zhu Yib481de92007-09-25 17:54:57 -07003873 priv->rx_handlers[SCAN_COMPLETE_NOTIFICATION] =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003874 iwl4965_rx_scan_complete_notif;
3875 priv->rx_handlers[CARD_STATE_NOTIFICATION] = iwl4965_rx_card_state_notif;
3876 priv->rx_handlers[REPLY_TX] = iwl4965_rx_reply_tx;
Zhu Yib481de92007-09-25 17:54:57 -07003877
Ben Cahill9fbab512007-11-29 11:09:47 +08003878 /* Set up hardware specific Rx handlers */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003879 iwl4965_hw_rx_handler_setup(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003880}
3881
3882/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003883 * iwl4965_tx_cmd_complete - Pull unused buffers off the queue and reclaim them
Zhu Yib481de92007-09-25 17:54:57 -07003884 * @rxb: Rx buffer to reclaim
3885 *
3886 * If an Rx buffer has an async callback associated with it the callback
3887 * will be executed. The attached skb (if present) will only be freed
3888 * if the callback returns 1
3889 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003890static void iwl4965_tx_cmd_complete(struct iwl4965_priv *priv,
3891 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003892{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003893 struct iwl4965_rx_packet *pkt = (struct iwl4965_rx_packet *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07003894 u16 sequence = le16_to_cpu(pkt->hdr.sequence);
3895 int txq_id = SEQ_TO_QUEUE(sequence);
3896 int index = SEQ_TO_INDEX(sequence);
3897 int huge = sequence & SEQ_HUGE_FRAME;
3898 int cmd_index;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003899 struct iwl4965_cmd *cmd;
Zhu Yib481de92007-09-25 17:54:57 -07003900
3901 /* If a Tx command is being handled and it isn't in the actual
3902 * command queue then there a command routing bug has been introduced
3903 * in the queue management code. */
3904 if (txq_id != IWL_CMD_QUEUE_NUM)
3905 IWL_ERROR("Error wrong command queue %d command id 0x%X\n",
3906 txq_id, pkt->hdr.cmd);
3907 BUG_ON(txq_id != IWL_CMD_QUEUE_NUM);
3908
3909 cmd_index = get_cmd_index(&priv->txq[IWL_CMD_QUEUE_NUM].q, index, huge);
3910 cmd = &priv->txq[IWL_CMD_QUEUE_NUM].cmd[cmd_index];
3911
3912 /* Input error checking is done when commands are added to queue. */
3913 if (cmd->meta.flags & CMD_WANT_SKB) {
3914 cmd->meta.source->u.skb = rxb->skb;
3915 rxb->skb = NULL;
3916 } else if (cmd->meta.u.callback &&
3917 !cmd->meta.u.callback(priv, cmd, rxb->skb))
3918 rxb->skb = NULL;
3919
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003920 iwl4965_tx_queue_reclaim(priv, txq_id, index);
Zhu Yib481de92007-09-25 17:54:57 -07003921
3922 if (!(cmd->meta.flags & CMD_ASYNC)) {
3923 clear_bit(STATUS_HCMD_ACTIVE, &priv->status);
3924 wake_up_interruptible(&priv->wait_command_queue);
3925 }
3926}
3927
3928/************************** RX-FUNCTIONS ****************************/
3929/*
3930 * Rx theory of operation
3931 *
Ben Cahill9fbab512007-11-29 11:09:47 +08003932 * Driver allocates a circular buffer of Receive Buffer Descriptors (RBDs),
3933 * each of which point to Receive Buffers to be filled by 4965. These get
3934 * used not only for Rx frames, but for any command response or notification
3935 * from the 4965. The driver and 4965 manage the Rx buffers by means
3936 * of indexes into the circular buffer.
Zhu Yib481de92007-09-25 17:54:57 -07003937 *
3938 * Rx Queue Indexes
3939 * The host/firmware share two index registers for managing the Rx buffers.
3940 *
3941 * The READ index maps to the first position that the firmware may be writing
3942 * to -- the driver can read up to (but not including) this position and get
3943 * good data.
3944 * The READ index is managed by the firmware once the card is enabled.
3945 *
3946 * The WRITE index maps to the last position the driver has read from -- the
3947 * position preceding WRITE is the last slot the firmware can place a packet.
3948 *
3949 * The queue is empty (no good data) if WRITE = READ - 1, and is full if
3950 * WRITE = READ.
3951 *
Ben Cahill9fbab512007-11-29 11:09:47 +08003952 * During initialization, the host sets up the READ queue position to the first
Zhu Yib481de92007-09-25 17:54:57 -07003953 * INDEX position, and WRITE to the last (READ - 1 wrapped)
3954 *
Ben Cahill9fbab512007-11-29 11:09:47 +08003955 * When the firmware places a packet in a buffer, it will advance the READ index
Zhu Yib481de92007-09-25 17:54:57 -07003956 * and fire the RX interrupt. The driver can then query the READ index and
3957 * process as many packets as possible, moving the WRITE index forward as it
3958 * resets the Rx queue buffers with new memory.
3959 *
3960 * The management in the driver is as follows:
3961 * + A list of pre-allocated SKBs is stored in iwl->rxq->rx_free. When
3962 * iwl->rxq->free_count drops to or below RX_LOW_WATERMARK, work is scheduled
Ian Schram01ebd062007-10-25 17:15:22 +08003963 * to replenish the iwl->rxq->rx_free.
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003964 * + In iwl4965_rx_replenish (scheduled) if 'processed' != 'read' then the
Zhu Yib481de92007-09-25 17:54:57 -07003965 * iwl->rxq is replenished and the READ INDEX is updated (updating the
3966 * 'processed' and 'read' driver indexes as well)
3967 * + A received packet is processed and handed to the kernel network stack,
3968 * detached from the iwl->rxq. The driver 'processed' index is updated.
3969 * + The Host/Firmware iwl->rxq is replenished at tasklet time from the rx_free
3970 * list. If there are no allocated buffers in iwl->rxq->rx_free, the READ
3971 * INDEX is not incremented and iwl->status(RX_STALLED) is set. If there
3972 * were enough free buffers and RX_STALLED is set it is cleared.
3973 *
3974 *
3975 * Driver sequence:
3976 *
Ben Cahill9fbab512007-11-29 11:09:47 +08003977 * iwl4965_rx_queue_alloc() Allocates rx_free
3978 * iwl4965_rx_replenish() Replenishes rx_free list from rx_used, and calls
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003979 * iwl4965_rx_queue_restock
Ben Cahill9fbab512007-11-29 11:09:47 +08003980 * iwl4965_rx_queue_restock() Moves available buffers from rx_free into Rx
Zhu Yib481de92007-09-25 17:54:57 -07003981 * queue, updates firmware pointers, and updates
3982 * the WRITE index. If insufficient rx_free buffers
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003983 * are available, schedules iwl4965_rx_replenish
Zhu Yib481de92007-09-25 17:54:57 -07003984 *
3985 * -- enable interrupts --
Ben Cahill9fbab512007-11-29 11:09:47 +08003986 * ISR - iwl4965_rx() Detach iwl4965_rx_mem_buffers from pool up to the
Zhu Yib481de92007-09-25 17:54:57 -07003987 * READ INDEX, detaching the SKB from the pool.
3988 * Moves the packet buffer from queue to rx_used.
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003989 * Calls iwl4965_rx_queue_restock to refill any empty
Zhu Yib481de92007-09-25 17:54:57 -07003990 * slots.
3991 * ...
3992 *
3993 */
3994
3995/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003996 * iwl4965_rx_queue_space - Return number of free slots available in queue.
Zhu Yib481de92007-09-25 17:54:57 -07003997 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003998static int iwl4965_rx_queue_space(const struct iwl4965_rx_queue *q)
Zhu Yib481de92007-09-25 17:54:57 -07003999{
4000 int s = q->read - q->write;
4001 if (s <= 0)
4002 s += RX_QUEUE_SIZE;
4003 /* keep some buffer to not confuse full and empty queue */
4004 s -= 2;
4005 if (s < 0)
4006 s = 0;
4007 return s;
4008}
4009
4010/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004011 * iwl4965_rx_queue_update_write_ptr - Update the write pointer for the RX queue
Zhu Yib481de92007-09-25 17:54:57 -07004012 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004013int iwl4965_rx_queue_update_write_ptr(struct iwl4965_priv *priv, struct iwl4965_rx_queue *q)
Zhu Yib481de92007-09-25 17:54:57 -07004014{
4015 u32 reg = 0;
4016 int rc = 0;
4017 unsigned long flags;
4018
4019 spin_lock_irqsave(&q->lock, flags);
4020
4021 if (q->need_update == 0)
4022 goto exit_unlock;
4023
Cahill, Ben M6440adb2007-11-29 11:09:55 +08004024 /* If power-saving is in use, make sure device is awake */
Zhu Yib481de92007-09-25 17:54:57 -07004025 if (test_bit(STATUS_POWER_PMI, &priv->status)) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004026 reg = iwl4965_read32(priv, CSR_UCODE_DRV_GP1);
Zhu Yib481de92007-09-25 17:54:57 -07004027
4028 if (reg & CSR_UCODE_DRV_GP1_BIT_MAC_SLEEP) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004029 iwl4965_set_bit(priv, CSR_GP_CNTRL,
Zhu Yib481de92007-09-25 17:54:57 -07004030 CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
4031 goto exit_unlock;
4032 }
4033
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004034 rc = iwl4965_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004035 if (rc)
4036 goto exit_unlock;
4037
Cahill, Ben M6440adb2007-11-29 11:09:55 +08004038 /* Device expects a multiple of 8 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004039 iwl4965_write_direct32(priv, FH_RSCSR_CHNL0_WPTR,
Zhu Yib481de92007-09-25 17:54:57 -07004040 q->write & ~0x7);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004041 iwl4965_release_nic_access(priv);
Cahill, Ben M6440adb2007-11-29 11:09:55 +08004042
4043 /* Else device is assumed to be awake */
Zhu Yib481de92007-09-25 17:54:57 -07004044 } else
Cahill, Ben M6440adb2007-11-29 11:09:55 +08004045 /* Device expects a multiple of 8 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004046 iwl4965_write32(priv, FH_RSCSR_CHNL0_WPTR, q->write & ~0x7);
Zhu Yib481de92007-09-25 17:54:57 -07004047
4048
4049 q->need_update = 0;
4050
4051 exit_unlock:
4052 spin_unlock_irqrestore(&q->lock, flags);
4053 return rc;
4054}
4055
4056/**
Ben Cahill9fbab512007-11-29 11:09:47 +08004057 * iwl4965_dma_addr2rbd_ptr - convert a DMA address to a uCode read buffer ptr
Zhu Yib481de92007-09-25 17:54:57 -07004058 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004059static inline __le32 iwl4965_dma_addr2rbd_ptr(struct iwl4965_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07004060 dma_addr_t dma_addr)
4061{
4062 return cpu_to_le32((u32)(dma_addr >> 8));
4063}
4064
4065
4066/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004067 * iwl4965_rx_queue_restock - refill RX queue from pre-allocated pool
Zhu Yib481de92007-09-25 17:54:57 -07004068 *
Ben Cahill9fbab512007-11-29 11:09:47 +08004069 * If there are slots in the RX queue that need to be restocked,
Zhu Yib481de92007-09-25 17:54:57 -07004070 * and we have free pre-allocated buffers, fill the ranks as much
Ben Cahill9fbab512007-11-29 11:09:47 +08004071 * as we can, pulling from rx_free.
Zhu Yib481de92007-09-25 17:54:57 -07004072 *
4073 * This moves the 'write' index forward to catch up with 'processed', and
4074 * also updates the memory address in the firmware to reference the new
4075 * target buffer.
4076 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004077static int iwl4965_rx_queue_restock(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004078{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004079 struct iwl4965_rx_queue *rxq = &priv->rxq;
Zhu Yib481de92007-09-25 17:54:57 -07004080 struct list_head *element;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004081 struct iwl4965_rx_mem_buffer *rxb;
Zhu Yib481de92007-09-25 17:54:57 -07004082 unsigned long flags;
4083 int write, rc;
4084
4085 spin_lock_irqsave(&rxq->lock, flags);
4086 write = rxq->write & ~0x7;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004087 while ((iwl4965_rx_queue_space(rxq) > 0) && (rxq->free_count)) {
Cahill, Ben M6440adb2007-11-29 11:09:55 +08004088 /* Get next free Rx buffer, remove from free list */
Zhu Yib481de92007-09-25 17:54:57 -07004089 element = rxq->rx_free.next;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004090 rxb = list_entry(element, struct iwl4965_rx_mem_buffer, list);
Zhu Yib481de92007-09-25 17:54:57 -07004091 list_del(element);
Cahill, Ben M6440adb2007-11-29 11:09:55 +08004092
4093 /* Point to Rx buffer via next RBD in circular buffer */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004094 rxq->bd[rxq->write] = iwl4965_dma_addr2rbd_ptr(priv, rxb->dma_addr);
Zhu Yib481de92007-09-25 17:54:57 -07004095 rxq->queue[rxq->write] = rxb;
4096 rxq->write = (rxq->write + 1) & RX_QUEUE_MASK;
4097 rxq->free_count--;
4098 }
4099 spin_unlock_irqrestore(&rxq->lock, flags);
4100 /* If the pre-allocated buffer pool is dropping low, schedule to
4101 * refill it */
4102 if (rxq->free_count <= RX_LOW_WATERMARK)
4103 queue_work(priv->workqueue, &priv->rx_replenish);
4104
4105
Cahill, Ben M6440adb2007-11-29 11:09:55 +08004106 /* If we've added more space for the firmware to place data, tell it.
4107 * Increment device's write pointer in multiples of 8. */
Zhu Yib481de92007-09-25 17:54:57 -07004108 if ((write != (rxq->write & ~0x7))
4109 || (abs(rxq->write - rxq->read) > 7)) {
4110 spin_lock_irqsave(&rxq->lock, flags);
4111 rxq->need_update = 1;
4112 spin_unlock_irqrestore(&rxq->lock, flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004113 rc = iwl4965_rx_queue_update_write_ptr(priv, rxq);
Zhu Yib481de92007-09-25 17:54:57 -07004114 if (rc)
4115 return rc;
4116 }
4117
4118 return 0;
4119}
4120
4121/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004122 * iwl4965_rx_replenish - Move all used packet from rx_used to rx_free
Zhu Yib481de92007-09-25 17:54:57 -07004123 *
4124 * When moving to rx_free an SKB is allocated for the slot.
4125 *
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004126 * Also restock the Rx queue via iwl4965_rx_queue_restock.
Ian Schram01ebd062007-10-25 17:15:22 +08004127 * This is called as a scheduled work item (except for during initialization)
Zhu Yib481de92007-09-25 17:54:57 -07004128 */
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08004129static void iwl4965_rx_allocate(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004130{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004131 struct iwl4965_rx_queue *rxq = &priv->rxq;
Zhu Yib481de92007-09-25 17:54:57 -07004132 struct list_head *element;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004133 struct iwl4965_rx_mem_buffer *rxb;
Zhu Yib481de92007-09-25 17:54:57 -07004134 unsigned long flags;
4135 spin_lock_irqsave(&rxq->lock, flags);
4136 while (!list_empty(&rxq->rx_used)) {
4137 element = rxq->rx_used.next;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004138 rxb = list_entry(element, struct iwl4965_rx_mem_buffer, list);
Cahill, Ben M6440adb2007-11-29 11:09:55 +08004139
4140 /* Alloc a new receive buffer */
Zhu Yib481de92007-09-25 17:54:57 -07004141 rxb->skb =
Ron Rindjunsky9ee1ba42007-11-26 16:14:42 +02004142 alloc_skb(priv->hw_setting.rx_buf_size,
4143 __GFP_NOWARN | GFP_ATOMIC);
Zhu Yib481de92007-09-25 17:54:57 -07004144 if (!rxb->skb) {
4145 if (net_ratelimit())
4146 printk(KERN_CRIT DRV_NAME
4147 ": Can not allocate SKB buffers\n");
4148 /* We don't reschedule replenish work here -- we will
4149 * call the restock method and if it still needs
4150 * more buffers it will schedule replenish */
4151 break;
4152 }
4153 priv->alloc_rxb_skb++;
4154 list_del(element);
Cahill, Ben M6440adb2007-11-29 11:09:55 +08004155
4156 /* Get physical address of RB/SKB */
Zhu Yib481de92007-09-25 17:54:57 -07004157 rxb->dma_addr =
4158 pci_map_single(priv->pci_dev, rxb->skb->data,
Ron Rindjunsky9ee1ba42007-11-26 16:14:42 +02004159 priv->hw_setting.rx_buf_size, PCI_DMA_FROMDEVICE);
Zhu Yib481de92007-09-25 17:54:57 -07004160 list_add_tail(&rxb->list, &rxq->rx_free);
4161 rxq->free_count++;
4162 }
4163 spin_unlock_irqrestore(&rxq->lock, flags);
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08004164}
4165
4166/*
4167 * this should be called while priv->lock is locked
4168*/
Tomas Winkler4fd1f842007-12-05 20:59:58 +02004169static void __iwl4965_rx_replenish(void *data)
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08004170{
4171 struct iwl4965_priv *priv = data;
4172
4173 iwl4965_rx_allocate(priv);
4174 iwl4965_rx_queue_restock(priv);
4175}
4176
4177
4178void iwl4965_rx_replenish(void *data)
4179{
4180 struct iwl4965_priv *priv = data;
4181 unsigned long flags;
4182
4183 iwl4965_rx_allocate(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004184
4185 spin_lock_irqsave(&priv->lock, flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004186 iwl4965_rx_queue_restock(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004187 spin_unlock_irqrestore(&priv->lock, flags);
4188}
4189
4190/* Assumes that the skb field of the buffers in 'pool' is kept accurate.
Ben Cahill9fbab512007-11-29 11:09:47 +08004191 * If an SKB has been detached, the POOL needs to have its SKB set to NULL
Zhu Yib481de92007-09-25 17:54:57 -07004192 * This free routine walks the list of POOL entries and if SKB is set to
4193 * non NULL it is unmapped and freed
4194 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004195static void iwl4965_rx_queue_free(struct iwl4965_priv *priv, struct iwl4965_rx_queue *rxq)
Zhu Yib481de92007-09-25 17:54:57 -07004196{
4197 int i;
4198 for (i = 0; i < RX_QUEUE_SIZE + RX_FREE_BUFFERS; i++) {
4199 if (rxq->pool[i].skb != NULL) {
4200 pci_unmap_single(priv->pci_dev,
4201 rxq->pool[i].dma_addr,
Ron Rindjunsky9ee1ba42007-11-26 16:14:42 +02004202 priv->hw_setting.rx_buf_size,
4203 PCI_DMA_FROMDEVICE);
Zhu Yib481de92007-09-25 17:54:57 -07004204 dev_kfree_skb(rxq->pool[i].skb);
4205 }
4206 }
4207
4208 pci_free_consistent(priv->pci_dev, 4 * RX_QUEUE_SIZE, rxq->bd,
4209 rxq->dma_addr);
4210 rxq->bd = NULL;
4211}
4212
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004213int iwl4965_rx_queue_alloc(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004214{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004215 struct iwl4965_rx_queue *rxq = &priv->rxq;
Zhu Yib481de92007-09-25 17:54:57 -07004216 struct pci_dev *dev = priv->pci_dev;
4217 int i;
4218
4219 spin_lock_init(&rxq->lock);
4220 INIT_LIST_HEAD(&rxq->rx_free);
4221 INIT_LIST_HEAD(&rxq->rx_used);
Cahill, Ben M6440adb2007-11-29 11:09:55 +08004222
4223 /* Alloc the circular buffer of Read Buffer Descriptors (RBDs) */
Zhu Yib481de92007-09-25 17:54:57 -07004224 rxq->bd = pci_alloc_consistent(dev, 4 * RX_QUEUE_SIZE, &rxq->dma_addr);
4225 if (!rxq->bd)
4226 return -ENOMEM;
Cahill, Ben M6440adb2007-11-29 11:09:55 +08004227
Zhu Yib481de92007-09-25 17:54:57 -07004228 /* Fill the rx_used queue with _all_ of the Rx buffers */
4229 for (i = 0; i < RX_FREE_BUFFERS + RX_QUEUE_SIZE; i++)
4230 list_add_tail(&rxq->pool[i].list, &rxq->rx_used);
Cahill, Ben M6440adb2007-11-29 11:09:55 +08004231
Zhu Yib481de92007-09-25 17:54:57 -07004232 /* Set us so that we have processed and used all buffers, but have
4233 * not restocked the Rx queue with fresh buffers */
4234 rxq->read = rxq->write = 0;
4235 rxq->free_count = 0;
4236 rxq->need_update = 0;
4237 return 0;
4238}
4239
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004240void iwl4965_rx_queue_reset(struct iwl4965_priv *priv, struct iwl4965_rx_queue *rxq)
Zhu Yib481de92007-09-25 17:54:57 -07004241{
4242 unsigned long flags;
4243 int i;
4244 spin_lock_irqsave(&rxq->lock, flags);
4245 INIT_LIST_HEAD(&rxq->rx_free);
4246 INIT_LIST_HEAD(&rxq->rx_used);
4247 /* Fill the rx_used queue with _all_ of the Rx buffers */
4248 for (i = 0; i < RX_FREE_BUFFERS + RX_QUEUE_SIZE; i++) {
4249 /* In the reset function, these buffers may have been allocated
4250 * to an SKB, so we need to unmap and free potential storage */
4251 if (rxq->pool[i].skb != NULL) {
4252 pci_unmap_single(priv->pci_dev,
4253 rxq->pool[i].dma_addr,
Ron Rindjunsky9ee1ba42007-11-26 16:14:42 +02004254 priv->hw_setting.rx_buf_size,
4255 PCI_DMA_FROMDEVICE);
Zhu Yib481de92007-09-25 17:54:57 -07004256 priv->alloc_rxb_skb--;
4257 dev_kfree_skb(rxq->pool[i].skb);
4258 rxq->pool[i].skb = NULL;
4259 }
4260 list_add_tail(&rxq->pool[i].list, &rxq->rx_used);
4261 }
4262
4263 /* Set us so that we have processed and used all buffers, but have
4264 * not restocked the Rx queue with fresh buffers */
4265 rxq->read = rxq->write = 0;
4266 rxq->free_count = 0;
4267 spin_unlock_irqrestore(&rxq->lock, flags);
4268}
4269
4270/* Convert linear signal-to-noise ratio into dB */
4271static u8 ratio2dB[100] = {
4272/* 0 1 2 3 4 5 6 7 8 9 */
4273 0, 0, 6, 10, 12, 14, 16, 17, 18, 19, /* 00 - 09 */
4274 20, 21, 22, 22, 23, 23, 24, 25, 26, 26, /* 10 - 19 */
4275 26, 26, 26, 27, 27, 28, 28, 28, 29, 29, /* 20 - 29 */
4276 29, 30, 30, 30, 31, 31, 31, 31, 32, 32, /* 30 - 39 */
4277 32, 32, 32, 33, 33, 33, 33, 33, 34, 34, /* 40 - 49 */
4278 34, 34, 34, 34, 35, 35, 35, 35, 35, 35, /* 50 - 59 */
4279 36, 36, 36, 36, 36, 36, 36, 37, 37, 37, /* 60 - 69 */
4280 37, 37, 37, 37, 37, 38, 38, 38, 38, 38, /* 70 - 79 */
4281 38, 38, 38, 38, 38, 39, 39, 39, 39, 39, /* 80 - 89 */
4282 39, 39, 39, 39, 39, 40, 40, 40, 40, 40 /* 90 - 99 */
4283};
4284
4285/* Calculates a relative dB value from a ratio of linear
4286 * (i.e. not dB) signal levels.
4287 * Conversion assumes that levels are voltages (20*log), not powers (10*log). */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004288int iwl4965_calc_db_from_ratio(int sig_ratio)
Zhu Yib481de92007-09-25 17:54:57 -07004289{
Adrian Bunkc899a572007-10-14 19:51:15 +02004290 /* 1000:1 or higher just report as 60 dB */
4291 if (sig_ratio >= 1000)
Zhu Yib481de92007-09-25 17:54:57 -07004292 return 60;
4293
Adrian Bunkc899a572007-10-14 19:51:15 +02004294 /* 100:1 or higher, divide by 10 and use table,
Zhu Yib481de92007-09-25 17:54:57 -07004295 * add 20 dB to make up for divide by 10 */
Adrian Bunkc899a572007-10-14 19:51:15 +02004296 if (sig_ratio >= 100)
Zhu Yib481de92007-09-25 17:54:57 -07004297 return (20 + (int)ratio2dB[sig_ratio/10]);
4298
4299 /* We shouldn't see this */
4300 if (sig_ratio < 1)
4301 return 0;
4302
4303 /* Use table for ratios 1:1 - 99:1 */
4304 return (int)ratio2dB[sig_ratio];
4305}
4306
4307#define PERFECT_RSSI (-20) /* dBm */
4308#define WORST_RSSI (-95) /* dBm */
4309#define RSSI_RANGE (PERFECT_RSSI - WORST_RSSI)
4310
4311/* Calculate an indication of rx signal quality (a percentage, not dBm!).
4312 * See http://www.ces.clemson.edu/linux/signal_quality.shtml for info
4313 * about formulas used below. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004314int iwl4965_calc_sig_qual(int rssi_dbm, int noise_dbm)
Zhu Yib481de92007-09-25 17:54:57 -07004315{
4316 int sig_qual;
4317 int degradation = PERFECT_RSSI - rssi_dbm;
4318
4319 /* If we get a noise measurement, use signal-to-noise ratio (SNR)
4320 * as indicator; formula is (signal dbm - noise dbm).
4321 * SNR at or above 40 is a great signal (100%).
4322 * Below that, scale to fit SNR of 0 - 40 dB within 0 - 100% indicator.
4323 * Weakest usable signal is usually 10 - 15 dB SNR. */
4324 if (noise_dbm) {
4325 if (rssi_dbm - noise_dbm >= 40)
4326 return 100;
4327 else if (rssi_dbm < noise_dbm)
4328 return 0;
4329 sig_qual = ((rssi_dbm - noise_dbm) * 5) / 2;
4330
4331 /* Else use just the signal level.
4332 * This formula is a least squares fit of data points collected and
4333 * compared with a reference system that had a percentage (%) display
4334 * for signal quality. */
4335 } else
4336 sig_qual = (100 * (RSSI_RANGE * RSSI_RANGE) - degradation *
4337 (15 * RSSI_RANGE + 62 * degradation)) /
4338 (RSSI_RANGE * RSSI_RANGE);
4339
4340 if (sig_qual > 100)
4341 sig_qual = 100;
4342 else if (sig_qual < 1)
4343 sig_qual = 0;
4344
4345 return sig_qual;
4346}
4347
4348/**
Ben Cahill9fbab512007-11-29 11:09:47 +08004349 * iwl4965_rx_handle - Main entry function for receiving responses from uCode
Zhu Yib481de92007-09-25 17:54:57 -07004350 *
4351 * Uses the priv->rx_handlers callback function array to invoke
4352 * the appropriate handlers, including command responses,
4353 * frame-received notifications, and other notifications.
4354 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004355static void iwl4965_rx_handle(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004356{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004357 struct iwl4965_rx_mem_buffer *rxb;
4358 struct iwl4965_rx_packet *pkt;
4359 struct iwl4965_rx_queue *rxq = &priv->rxq;
Zhu Yib481de92007-09-25 17:54:57 -07004360 u32 r, i;
4361 int reclaim;
4362 unsigned long flags;
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08004363 u8 fill_rx = 0;
Mohamed Abbasd68ab682008-02-07 13:16:33 -08004364 u32 count = 8;
Zhu Yib481de92007-09-25 17:54:57 -07004365
Cahill, Ben M6440adb2007-11-29 11:09:55 +08004366 /* uCode's read index (stored in shared DRAM) indicates the last Rx
4367 * buffer that the driver may process (last buffer filled by ucode). */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004368 r = iwl4965_hw_get_rx_read(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004369 i = rxq->read;
4370
4371 /* Rx interrupt, but nothing sent from uCode */
4372 if (i == r)
4373 IWL_DEBUG(IWL_DL_RX | IWL_DL_ISR, "r = %d, i = %d\n", r, i);
4374
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08004375 if (iwl4965_rx_queue_space(rxq) > (RX_QUEUE_SIZE / 2))
4376 fill_rx = 1;
4377
Zhu Yib481de92007-09-25 17:54:57 -07004378 while (i != r) {
4379 rxb = rxq->queue[i];
4380
Ben Cahill9fbab512007-11-29 11:09:47 +08004381 /* If an RXB doesn't have a Rx queue slot associated with it,
Zhu Yib481de92007-09-25 17:54:57 -07004382 * then a bug has been introduced in the queue refilling
4383 * routines -- catch it here */
4384 BUG_ON(rxb == NULL);
4385
4386 rxq->queue[i] = NULL;
4387
4388 pci_dma_sync_single_for_cpu(priv->pci_dev, rxb->dma_addr,
Ron Rindjunsky9ee1ba42007-11-26 16:14:42 +02004389 priv->hw_setting.rx_buf_size,
Zhu Yib481de92007-09-25 17:54:57 -07004390 PCI_DMA_FROMDEVICE);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004391 pkt = (struct iwl4965_rx_packet *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07004392
4393 /* Reclaim a command buffer only if this packet is a response
4394 * to a (driver-originated) command.
4395 * If the packet (e.g. Rx frame) originated from uCode,
4396 * there is no command buffer to reclaim.
4397 * Ucode should set SEQ_RX_FRAME bit if ucode-originated,
4398 * but apparently a few don't get set; catch them here. */
4399 reclaim = !(pkt->hdr.sequence & SEQ_RX_FRAME) &&
4400 (pkt->hdr.cmd != REPLY_RX_PHY_CMD) &&
4401 (pkt->hdr.cmd != REPLY_4965_RX) &&
Zhu Yicfe01702007-09-27 11:27:31 +08004402 (pkt->hdr.cmd != REPLY_COMPRESSED_BA) &&
Zhu Yib481de92007-09-25 17:54:57 -07004403 (pkt->hdr.cmd != STATISTICS_NOTIFICATION) &&
4404 (pkt->hdr.cmd != REPLY_TX);
4405
4406 /* Based on type of command response or notification,
4407 * handle those that need handling via function in
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004408 * rx_handlers table. See iwl4965_setup_rx_handlers() */
Zhu Yib481de92007-09-25 17:54:57 -07004409 if (priv->rx_handlers[pkt->hdr.cmd]) {
4410 IWL_DEBUG(IWL_DL_HOST_COMMAND | IWL_DL_RX | IWL_DL_ISR,
4411 "r = %d, i = %d, %s, 0x%02x\n", r, i,
4412 get_cmd_string(pkt->hdr.cmd), pkt->hdr.cmd);
4413 priv->rx_handlers[pkt->hdr.cmd] (priv, rxb);
4414 } else {
4415 /* No handling needed */
4416 IWL_DEBUG(IWL_DL_HOST_COMMAND | IWL_DL_RX | IWL_DL_ISR,
4417 "r %d i %d No handler needed for %s, 0x%02x\n",
4418 r, i, get_cmd_string(pkt->hdr.cmd),
4419 pkt->hdr.cmd);
4420 }
4421
4422 if (reclaim) {
Ben Cahill9fbab512007-11-29 11:09:47 +08004423 /* Invoke any callbacks, transfer the skb to caller, and
4424 * fire off the (possibly) blocking iwl4965_send_cmd()
Zhu Yib481de92007-09-25 17:54:57 -07004425 * as we reclaim the driver command queue */
4426 if (rxb && rxb->skb)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004427 iwl4965_tx_cmd_complete(priv, rxb);
Zhu Yib481de92007-09-25 17:54:57 -07004428 else
4429 IWL_WARNING("Claim null rxb?\n");
4430 }
4431
4432 /* For now we just don't re-use anything. We can tweak this
4433 * later to try and re-use notification packets and SKBs that
4434 * fail to Rx correctly */
4435 if (rxb->skb != NULL) {
4436 priv->alloc_rxb_skb--;
4437 dev_kfree_skb_any(rxb->skb);
4438 rxb->skb = NULL;
4439 }
4440
4441 pci_unmap_single(priv->pci_dev, rxb->dma_addr,
Ron Rindjunsky9ee1ba42007-11-26 16:14:42 +02004442 priv->hw_setting.rx_buf_size,
4443 PCI_DMA_FROMDEVICE);
Zhu Yib481de92007-09-25 17:54:57 -07004444 spin_lock_irqsave(&rxq->lock, flags);
4445 list_add_tail(&rxb->list, &priv->rxq.rx_used);
4446 spin_unlock_irqrestore(&rxq->lock, flags);
4447 i = (i + 1) & RX_QUEUE_MASK;
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08004448 /* If there are a lot of unused frames,
4449 * restock the Rx queue so ucode wont assert. */
4450 if (fill_rx) {
4451 count++;
4452 if (count >= 8) {
4453 priv->rxq.read = i;
4454 __iwl4965_rx_replenish(priv);
4455 count = 0;
4456 }
4457 }
Zhu Yib481de92007-09-25 17:54:57 -07004458 }
4459
4460 /* Backtrack one entry */
4461 priv->rxq.read = i;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004462 iwl4965_rx_queue_restock(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004463}
4464
Cahill, Ben M6440adb2007-11-29 11:09:55 +08004465/**
4466 * iwl4965_tx_queue_update_write_ptr - Send new write index to hardware
4467 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004468static int iwl4965_tx_queue_update_write_ptr(struct iwl4965_priv *priv,
4469 struct iwl4965_tx_queue *txq)
Zhu Yib481de92007-09-25 17:54:57 -07004470{
4471 u32 reg = 0;
4472 int rc = 0;
4473 int txq_id = txq->q.id;
4474
4475 if (txq->need_update == 0)
4476 return rc;
4477
4478 /* if we're trying to save power */
4479 if (test_bit(STATUS_POWER_PMI, &priv->status)) {
4480 /* wake up nic if it's powered down ...
4481 * uCode will wake up, and interrupt us again, so next
4482 * time we'll skip this part. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004483 reg = iwl4965_read32(priv, CSR_UCODE_DRV_GP1);
Zhu Yib481de92007-09-25 17:54:57 -07004484
4485 if (reg & CSR_UCODE_DRV_GP1_BIT_MAC_SLEEP) {
4486 IWL_DEBUG_INFO("Requesting wakeup, GP1 = 0x%x\n", reg);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004487 iwl4965_set_bit(priv, CSR_GP_CNTRL,
Zhu Yib481de92007-09-25 17:54:57 -07004488 CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
4489 return rc;
4490 }
4491
4492 /* restore this queue's parameters in nic hardware. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004493 rc = iwl4965_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004494 if (rc)
4495 return rc;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004496 iwl4965_write_direct32(priv, HBUS_TARG_WRPTR,
Tomas Winklerfc4b6852007-10-25 17:15:24 +08004497 txq->q.write_ptr | (txq_id << 8));
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004498 iwl4965_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004499
4500 /* else not in power-save mode, uCode will never sleep when we're
4501 * trying to tx (during RFKILL, we're not trying to tx). */
4502 } else
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004503 iwl4965_write32(priv, HBUS_TARG_WRPTR,
Tomas Winklerfc4b6852007-10-25 17:15:24 +08004504 txq->q.write_ptr | (txq_id << 8));
Zhu Yib481de92007-09-25 17:54:57 -07004505
4506 txq->need_update = 0;
4507
4508 return rc;
4509}
4510
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08004511#ifdef CONFIG_IWL4965_DEBUG
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004512static void iwl4965_print_rx_config_cmd(struct iwl4965_rxon_cmd *rxon)
Zhu Yib481de92007-09-25 17:54:57 -07004513{
Joe Perches0795af52007-10-03 17:59:30 -07004514 DECLARE_MAC_BUF(mac);
4515
Zhu Yib481de92007-09-25 17:54:57 -07004516 IWL_DEBUG_RADIO("RX CONFIG:\n");
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004517 iwl4965_print_hex_dump(IWL_DL_RADIO, (u8 *) rxon, sizeof(*rxon));
Zhu Yib481de92007-09-25 17:54:57 -07004518 IWL_DEBUG_RADIO("u16 channel: 0x%x\n", le16_to_cpu(rxon->channel));
4519 IWL_DEBUG_RADIO("u32 flags: 0x%08X\n", le32_to_cpu(rxon->flags));
4520 IWL_DEBUG_RADIO("u32 filter_flags: 0x%08x\n",
4521 le32_to_cpu(rxon->filter_flags));
4522 IWL_DEBUG_RADIO("u8 dev_type: 0x%x\n", rxon->dev_type);
4523 IWL_DEBUG_RADIO("u8 ofdm_basic_rates: 0x%02x\n",
4524 rxon->ofdm_basic_rates);
4525 IWL_DEBUG_RADIO("u8 cck_basic_rates: 0x%02x\n", rxon->cck_basic_rates);
Joe Perches0795af52007-10-03 17:59:30 -07004526 IWL_DEBUG_RADIO("u8[6] node_addr: %s\n",
4527 print_mac(mac, rxon->node_addr));
4528 IWL_DEBUG_RADIO("u8[6] bssid_addr: %s\n",
4529 print_mac(mac, rxon->bssid_addr));
Zhu Yib481de92007-09-25 17:54:57 -07004530 IWL_DEBUG_RADIO("u16 assoc_id: 0x%x\n", le16_to_cpu(rxon->assoc_id));
4531}
4532#endif
4533
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004534static void iwl4965_enable_interrupts(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004535{
4536 IWL_DEBUG_ISR("Enabling interrupts\n");
4537 set_bit(STATUS_INT_ENABLED, &priv->status);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004538 iwl4965_write32(priv, CSR_INT_MASK, CSR_INI_SET_MASK);
Zhu Yib481de92007-09-25 17:54:57 -07004539}
4540
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004541static inline void iwl4965_disable_interrupts(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004542{
4543 clear_bit(STATUS_INT_ENABLED, &priv->status);
4544
4545 /* disable interrupts from uCode/NIC to host */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004546 iwl4965_write32(priv, CSR_INT_MASK, 0x00000000);
Zhu Yib481de92007-09-25 17:54:57 -07004547
4548 /* acknowledge/clear/reset any interrupts still pending
4549 * from uCode or flow handler (Rx/Tx DMA) */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004550 iwl4965_write32(priv, CSR_INT, 0xffffffff);
4551 iwl4965_write32(priv, CSR_FH_INT_STATUS, 0xffffffff);
Zhu Yib481de92007-09-25 17:54:57 -07004552 IWL_DEBUG_ISR("Disabled interrupts\n");
4553}
4554
4555static const char *desc_lookup(int i)
4556{
4557 switch (i) {
4558 case 1:
4559 return "FAIL";
4560 case 2:
4561 return "BAD_PARAM";
4562 case 3:
4563 return "BAD_CHECKSUM";
4564 case 4:
4565 return "NMI_INTERRUPT";
4566 case 5:
4567 return "SYSASSERT";
4568 case 6:
4569 return "FATAL_ERROR";
4570 }
4571
4572 return "UNKNOWN";
4573}
4574
4575#define ERROR_START_OFFSET (1 * sizeof(u32))
4576#define ERROR_ELEM_SIZE (7 * sizeof(u32))
4577
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004578static void iwl4965_dump_nic_error_log(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004579{
4580 u32 data2, line;
4581 u32 desc, time, count, base, data1;
4582 u32 blink1, blink2, ilink1, ilink2;
4583 int rc;
4584
4585 base = le32_to_cpu(priv->card_alive.error_event_table_ptr);
4586
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004587 if (!iwl4965_hw_valid_rtc_data_addr(base)) {
Zhu Yib481de92007-09-25 17:54:57 -07004588 IWL_ERROR("Not valid error log pointer 0x%08X\n", base);
4589 return;
4590 }
4591
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004592 rc = iwl4965_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004593 if (rc) {
4594 IWL_WARNING("Can not read from adapter at this time.\n");
4595 return;
4596 }
4597
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004598 count = iwl4965_read_targ_mem(priv, base);
Zhu Yib481de92007-09-25 17:54:57 -07004599
4600 if (ERROR_START_OFFSET <= count * ERROR_ELEM_SIZE) {
4601 IWL_ERROR("Start IWL Error Log Dump:\n");
Tomas Winkler2acae162008-03-02 01:25:59 +02004602 IWL_ERROR("Status: 0x%08lX, count: %d\n", priv->status, count);
Zhu Yib481de92007-09-25 17:54:57 -07004603 }
4604
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004605 desc = iwl4965_read_targ_mem(priv, base + 1 * sizeof(u32));
4606 blink1 = iwl4965_read_targ_mem(priv, base + 3 * sizeof(u32));
4607 blink2 = iwl4965_read_targ_mem(priv, base + 4 * sizeof(u32));
4608 ilink1 = iwl4965_read_targ_mem(priv, base + 5 * sizeof(u32));
4609 ilink2 = iwl4965_read_targ_mem(priv, base + 6 * sizeof(u32));
4610 data1 = iwl4965_read_targ_mem(priv, base + 7 * sizeof(u32));
4611 data2 = iwl4965_read_targ_mem(priv, base + 8 * sizeof(u32));
4612 line = iwl4965_read_targ_mem(priv, base + 9 * sizeof(u32));
4613 time = iwl4965_read_targ_mem(priv, base + 11 * sizeof(u32));
Zhu Yib481de92007-09-25 17:54:57 -07004614
4615 IWL_ERROR("Desc Time "
4616 "data1 data2 line\n");
4617 IWL_ERROR("%-13s (#%d) %010u 0x%08X 0x%08X %u\n",
4618 desc_lookup(desc), desc, time, data1, data2, line);
4619 IWL_ERROR("blink1 blink2 ilink1 ilink2\n");
4620 IWL_ERROR("0x%05X 0x%05X 0x%05X 0x%05X\n", blink1, blink2,
4621 ilink1, ilink2);
4622
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004623 iwl4965_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004624}
4625
4626#define EVENT_START_OFFSET (4 * sizeof(u32))
4627
4628/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004629 * iwl4965_print_event_log - Dump error event log to syslog
Zhu Yib481de92007-09-25 17:54:57 -07004630 *
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004631 * NOTE: Must be called with iwl4965_grab_nic_access() already obtained!
Zhu Yib481de92007-09-25 17:54:57 -07004632 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004633static void iwl4965_print_event_log(struct iwl4965_priv *priv, u32 start_idx,
Zhu Yib481de92007-09-25 17:54:57 -07004634 u32 num_events, u32 mode)
4635{
4636 u32 i;
4637 u32 base; /* SRAM byte address of event log header */
4638 u32 event_size; /* 2 u32s, or 3 u32s if timestamp recorded */
4639 u32 ptr; /* SRAM byte address of log data */
4640 u32 ev, time, data; /* event log data */
4641
4642 if (num_events == 0)
4643 return;
4644
4645 base = le32_to_cpu(priv->card_alive.log_event_table_ptr);
4646
4647 if (mode == 0)
4648 event_size = 2 * sizeof(u32);
4649 else
4650 event_size = 3 * sizeof(u32);
4651
4652 ptr = base + EVENT_START_OFFSET + (start_idx * event_size);
4653
4654 /* "time" is actually "data" for mode 0 (no timestamp).
4655 * place event id # at far right for easier visual parsing. */
4656 for (i = 0; i < num_events; i++) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004657 ev = iwl4965_read_targ_mem(priv, ptr);
Zhu Yib481de92007-09-25 17:54:57 -07004658 ptr += sizeof(u32);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004659 time = iwl4965_read_targ_mem(priv, ptr);
Zhu Yib481de92007-09-25 17:54:57 -07004660 ptr += sizeof(u32);
4661 if (mode == 0)
4662 IWL_ERROR("0x%08x\t%04u\n", time, ev); /* data, ev */
4663 else {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004664 data = iwl4965_read_targ_mem(priv, ptr);
Zhu Yib481de92007-09-25 17:54:57 -07004665 ptr += sizeof(u32);
4666 IWL_ERROR("%010u\t0x%08x\t%04u\n", time, data, ev);
4667 }
4668 }
4669}
4670
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004671static void iwl4965_dump_nic_event_log(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004672{
4673 int rc;
4674 u32 base; /* SRAM byte address of event log header */
4675 u32 capacity; /* event log capacity in # entries */
4676 u32 mode; /* 0 - no timestamp, 1 - timestamp recorded */
4677 u32 num_wraps; /* # times uCode wrapped to top of log */
4678 u32 next_entry; /* index of next entry to be written by uCode */
4679 u32 size; /* # entries that we'll print */
4680
4681 base = le32_to_cpu(priv->card_alive.log_event_table_ptr);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004682 if (!iwl4965_hw_valid_rtc_data_addr(base)) {
Zhu Yib481de92007-09-25 17:54:57 -07004683 IWL_ERROR("Invalid event log pointer 0x%08X\n", base);
4684 return;
4685 }
4686
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004687 rc = iwl4965_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004688 if (rc) {
4689 IWL_WARNING("Can not read from adapter at this time.\n");
4690 return;
4691 }
4692
4693 /* event log header */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004694 capacity = iwl4965_read_targ_mem(priv, base);
4695 mode = iwl4965_read_targ_mem(priv, base + (1 * sizeof(u32)));
4696 num_wraps = iwl4965_read_targ_mem(priv, base + (2 * sizeof(u32)));
4697 next_entry = iwl4965_read_targ_mem(priv, base + (3 * sizeof(u32)));
Zhu Yib481de92007-09-25 17:54:57 -07004698
4699 size = num_wraps ? capacity : next_entry;
4700
4701 /* bail out if nothing in log */
4702 if (size == 0) {
Zhu Yi583fab32007-09-27 11:27:30 +08004703 IWL_ERROR("Start IWL Event Log Dump: nothing in log\n");
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004704 iwl4965_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004705 return;
4706 }
4707
Zhu Yi583fab32007-09-27 11:27:30 +08004708 IWL_ERROR("Start IWL Event Log Dump: display count %d, wraps %d\n",
Zhu Yib481de92007-09-25 17:54:57 -07004709 size, num_wraps);
4710
4711 /* if uCode has wrapped back to top of log, start at the oldest entry,
4712 * i.e the next one that uCode would fill. */
4713 if (num_wraps)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004714 iwl4965_print_event_log(priv, next_entry,
Zhu Yib481de92007-09-25 17:54:57 -07004715 capacity - next_entry, mode);
4716
4717 /* (then/else) start at top of log */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004718 iwl4965_print_event_log(priv, 0, next_entry, mode);
Zhu Yib481de92007-09-25 17:54:57 -07004719
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004720 iwl4965_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004721}
4722
4723/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004724 * iwl4965_irq_handle_error - called for HW or SW error interrupt from card
Zhu Yib481de92007-09-25 17:54:57 -07004725 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004726static void iwl4965_irq_handle_error(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004727{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004728 /* Set the FW error flag -- cleared on iwl4965_down */
Zhu Yib481de92007-09-25 17:54:57 -07004729 set_bit(STATUS_FW_ERROR, &priv->status);
4730
4731 /* Cancel currently queued command. */
4732 clear_bit(STATUS_HCMD_ACTIVE, &priv->status);
4733
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08004734#ifdef CONFIG_IWL4965_DEBUG
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004735 if (iwl4965_debug_level & IWL_DL_FW_ERRORS) {
4736 iwl4965_dump_nic_error_log(priv);
4737 iwl4965_dump_nic_event_log(priv);
4738 iwl4965_print_rx_config_cmd(&priv->staging_rxon);
Zhu Yib481de92007-09-25 17:54:57 -07004739 }
4740#endif
4741
4742 wake_up_interruptible(&priv->wait_command_queue);
4743
4744 /* Keep the restart process from trying to send host
4745 * commands by clearing the INIT status bit */
4746 clear_bit(STATUS_READY, &priv->status);
4747
4748 if (!test_bit(STATUS_EXIT_PENDING, &priv->status)) {
4749 IWL_DEBUG(IWL_DL_INFO | IWL_DL_FW_ERRORS,
4750 "Restarting adapter due to uCode error.\n");
4751
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004752 if (iwl4965_is_associated(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07004753 memcpy(&priv->recovery_rxon, &priv->active_rxon,
4754 sizeof(priv->recovery_rxon));
4755 priv->error_recovering = 1;
4756 }
4757 queue_work(priv->workqueue, &priv->restart);
4758 }
4759}
4760
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004761static void iwl4965_error_recovery(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004762{
4763 unsigned long flags;
4764
4765 memcpy(&priv->staging_rxon, &priv->recovery_rxon,
4766 sizeof(priv->staging_rxon));
4767 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004768 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004769
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004770 iwl4965_rxon_add_station(priv, priv->bssid, 1);
Zhu Yib481de92007-09-25 17:54:57 -07004771
4772 spin_lock_irqsave(&priv->lock, flags);
4773 priv->assoc_id = le16_to_cpu(priv->staging_rxon.assoc_id);
4774 priv->error_recovering = 0;
4775 spin_unlock_irqrestore(&priv->lock, flags);
4776}
4777
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004778static void iwl4965_irq_tasklet(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004779{
4780 u32 inta, handled = 0;
4781 u32 inta_fh;
4782 unsigned long flags;
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08004783#ifdef CONFIG_IWL4965_DEBUG
Zhu Yib481de92007-09-25 17:54:57 -07004784 u32 inta_mask;
4785#endif
4786
4787 spin_lock_irqsave(&priv->lock, flags);
4788
4789 /* Ack/clear/reset pending uCode interrupts.
4790 * Note: Some bits in CSR_INT are "OR" of bits in CSR_FH_INT_STATUS,
4791 * and will clear only when CSR_FH_INT_STATUS gets cleared. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004792 inta = iwl4965_read32(priv, CSR_INT);
4793 iwl4965_write32(priv, CSR_INT, inta);
Zhu Yib481de92007-09-25 17:54:57 -07004794
4795 /* Ack/clear/reset pending flow-handler (DMA) interrupts.
4796 * Any new interrupts that happen after this, either while we're
4797 * in this tasklet, or later, will show up in next ISR/tasklet. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004798 inta_fh = iwl4965_read32(priv, CSR_FH_INT_STATUS);
4799 iwl4965_write32(priv, CSR_FH_INT_STATUS, inta_fh);
Zhu Yib481de92007-09-25 17:54:57 -07004800
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08004801#ifdef CONFIG_IWL4965_DEBUG
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004802 if (iwl4965_debug_level & IWL_DL_ISR) {
Ben Cahill9fbab512007-11-29 11:09:47 +08004803 /* just for debug */
4804 inta_mask = iwl4965_read32(priv, CSR_INT_MASK);
Zhu Yib481de92007-09-25 17:54:57 -07004805 IWL_DEBUG_ISR("inta 0x%08x, enabled 0x%08x, fh 0x%08x\n",
4806 inta, inta_mask, inta_fh);
4807 }
4808#endif
4809
4810 /* Since CSR_INT and CSR_FH_INT_STATUS reads and clears are not
4811 * atomic, make sure that inta covers all the interrupts that
4812 * we've discovered, even if FH interrupt came in just after
4813 * reading CSR_INT. */
Tomas Winkler6f83eaa2008-03-04 18:09:28 -08004814 if (inta_fh & CSR49_FH_INT_RX_MASK)
Zhu Yib481de92007-09-25 17:54:57 -07004815 inta |= CSR_INT_BIT_FH_RX;
Tomas Winkler6f83eaa2008-03-04 18:09:28 -08004816 if (inta_fh & CSR49_FH_INT_TX_MASK)
Zhu Yib481de92007-09-25 17:54:57 -07004817 inta |= CSR_INT_BIT_FH_TX;
4818
4819 /* Now service all interrupt bits discovered above. */
4820 if (inta & CSR_INT_BIT_HW_ERR) {
4821 IWL_ERROR("Microcode HW error detected. Restarting.\n");
4822
4823 /* Tell the device to stop sending interrupts */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004824 iwl4965_disable_interrupts(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004825
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004826 iwl4965_irq_handle_error(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004827
4828 handled |= CSR_INT_BIT_HW_ERR;
4829
4830 spin_unlock_irqrestore(&priv->lock, flags);
4831
4832 return;
4833 }
4834
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08004835#ifdef CONFIG_IWL4965_DEBUG
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004836 if (iwl4965_debug_level & (IWL_DL_ISR)) {
Zhu Yib481de92007-09-25 17:54:57 -07004837 /* NIC fires this, but we don't use it, redundant with WAKEUP */
Joonwoo Park25c03d82008-01-23 10:15:20 -08004838 if (inta & CSR_INT_BIT_SCD)
4839 IWL_DEBUG_ISR("Scheduler finished to transmit "
4840 "the frame/frames.\n");
Zhu Yib481de92007-09-25 17:54:57 -07004841
4842 /* Alive notification via Rx interrupt will do the real work */
4843 if (inta & CSR_INT_BIT_ALIVE)
4844 IWL_DEBUG_ISR("Alive interrupt\n");
4845 }
4846#endif
4847 /* Safely ignore these bits for debug checks below */
Joonwoo Park25c03d82008-01-23 10:15:20 -08004848 inta &= ~(CSR_INT_BIT_SCD | CSR_INT_BIT_ALIVE);
Zhu Yib481de92007-09-25 17:54:57 -07004849
Ben Cahill9fbab512007-11-29 11:09:47 +08004850 /* HW RF KILL switch toggled */
Zhu Yib481de92007-09-25 17:54:57 -07004851 if (inta & CSR_INT_BIT_RF_KILL) {
4852 int hw_rf_kill = 0;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004853 if (!(iwl4965_read32(priv, CSR_GP_CNTRL) &
Zhu Yib481de92007-09-25 17:54:57 -07004854 CSR_GP_CNTRL_REG_FLAG_HW_RF_KILL_SW))
4855 hw_rf_kill = 1;
4856
4857 IWL_DEBUG(IWL_DL_INFO | IWL_DL_RF_KILL | IWL_DL_ISR,
4858 "RF_KILL bit toggled to %s.\n",
4859 hw_rf_kill ? "disable radio":"enable radio");
4860
4861 /* Queue restart only if RF_KILL switch was set to "kill"
4862 * when we loaded driver, and is now set to "enable".
4863 * After we're Alive, RF_KILL gets handled by
Reinette Chatre32304552008-02-15 14:34:37 -08004864 * iwl4965_rx_card_state_notif() */
Zhu Yi53e49092007-12-06 16:08:44 +08004865 if (!hw_rf_kill && !test_bit(STATUS_ALIVE, &priv->status)) {
4866 clear_bit(STATUS_RF_KILL_HW, &priv->status);
Zhu Yib481de92007-09-25 17:54:57 -07004867 queue_work(priv->workqueue, &priv->restart);
Zhu Yi53e49092007-12-06 16:08:44 +08004868 }
Zhu Yib481de92007-09-25 17:54:57 -07004869
4870 handled |= CSR_INT_BIT_RF_KILL;
4871 }
4872
Ben Cahill9fbab512007-11-29 11:09:47 +08004873 /* Chip got too hot and stopped itself */
Zhu Yib481de92007-09-25 17:54:57 -07004874 if (inta & CSR_INT_BIT_CT_KILL) {
4875 IWL_ERROR("Microcode CT kill error detected.\n");
4876 handled |= CSR_INT_BIT_CT_KILL;
4877 }
4878
4879 /* Error detected by uCode */
4880 if (inta & CSR_INT_BIT_SW_ERR) {
4881 IWL_ERROR("Microcode SW error detected. Restarting 0x%X.\n",
4882 inta);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004883 iwl4965_irq_handle_error(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004884 handled |= CSR_INT_BIT_SW_ERR;
4885 }
4886
4887 /* uCode wakes up after power-down sleep */
4888 if (inta & CSR_INT_BIT_WAKEUP) {
4889 IWL_DEBUG_ISR("Wakeup interrupt\n");
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004890 iwl4965_rx_queue_update_write_ptr(priv, &priv->rxq);
4891 iwl4965_tx_queue_update_write_ptr(priv, &priv->txq[0]);
4892 iwl4965_tx_queue_update_write_ptr(priv, &priv->txq[1]);
4893 iwl4965_tx_queue_update_write_ptr(priv, &priv->txq[2]);
4894 iwl4965_tx_queue_update_write_ptr(priv, &priv->txq[3]);
4895 iwl4965_tx_queue_update_write_ptr(priv, &priv->txq[4]);
4896 iwl4965_tx_queue_update_write_ptr(priv, &priv->txq[5]);
Zhu Yib481de92007-09-25 17:54:57 -07004897
4898 handled |= CSR_INT_BIT_WAKEUP;
4899 }
4900
4901 /* All uCode command responses, including Tx command responses,
4902 * Rx "responses" (frame-received notification), and other
4903 * notifications from uCode come through here*/
4904 if (inta & (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX)) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004905 iwl4965_rx_handle(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004906 handled |= (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX);
4907 }
4908
4909 if (inta & CSR_INT_BIT_FH_TX) {
4910 IWL_DEBUG_ISR("Tx interrupt\n");
4911 handled |= CSR_INT_BIT_FH_TX;
4912 }
4913
4914 if (inta & ~handled)
4915 IWL_ERROR("Unhandled INTA bits 0x%08x\n", inta & ~handled);
4916
4917 if (inta & ~CSR_INI_SET_MASK) {
4918 IWL_WARNING("Disabled INTA bits 0x%08x were pending\n",
4919 inta & ~CSR_INI_SET_MASK);
4920 IWL_WARNING(" with FH_INT = 0x%08x\n", inta_fh);
4921 }
4922
4923 /* Re-enable all interrupts */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004924 iwl4965_enable_interrupts(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004925
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08004926#ifdef CONFIG_IWL4965_DEBUG
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004927 if (iwl4965_debug_level & (IWL_DL_ISR)) {
4928 inta = iwl4965_read32(priv, CSR_INT);
4929 inta_mask = iwl4965_read32(priv, CSR_INT_MASK);
4930 inta_fh = iwl4965_read32(priv, CSR_FH_INT_STATUS);
Zhu Yib481de92007-09-25 17:54:57 -07004931 IWL_DEBUG_ISR("End inta 0x%08x, enabled 0x%08x, fh 0x%08x, "
4932 "flags 0x%08lx\n", inta, inta_mask, inta_fh, flags);
4933 }
4934#endif
4935 spin_unlock_irqrestore(&priv->lock, flags);
4936}
4937
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004938static irqreturn_t iwl4965_isr(int irq, void *data)
Zhu Yib481de92007-09-25 17:54:57 -07004939{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004940 struct iwl4965_priv *priv = data;
Zhu Yib481de92007-09-25 17:54:57 -07004941 u32 inta, inta_mask;
4942 u32 inta_fh;
4943 if (!priv)
4944 return IRQ_NONE;
4945
4946 spin_lock(&priv->lock);
4947
4948 /* Disable (but don't clear!) interrupts here to avoid
4949 * back-to-back ISRs and sporadic interrupts from our NIC.
4950 * If we have something to service, the tasklet will re-enable ints.
4951 * If we *don't* have something, we'll re-enable before leaving here. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004952 inta_mask = iwl4965_read32(priv, CSR_INT_MASK); /* just for debug */
4953 iwl4965_write32(priv, CSR_INT_MASK, 0x00000000);
Zhu Yib481de92007-09-25 17:54:57 -07004954
4955 /* Discover which interrupts are active/pending */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004956 inta = iwl4965_read32(priv, CSR_INT);
4957 inta_fh = iwl4965_read32(priv, CSR_FH_INT_STATUS);
Zhu Yib481de92007-09-25 17:54:57 -07004958
4959 /* Ignore interrupt if there's nothing in NIC to service.
4960 * This may be due to IRQ shared with another device,
4961 * or due to sporadic interrupts thrown from our NIC. */
4962 if (!inta && !inta_fh) {
4963 IWL_DEBUG_ISR("Ignore interrupt, inta == 0, inta_fh == 0\n");
4964 goto none;
4965 }
4966
4967 if ((inta == 0xFFFFFFFF) || ((inta & 0xFFFFFFF0) == 0xa5a5a5a0)) {
Oliver Neukum66fbb542007-11-15 09:31:10 +08004968 /* Hardware disappeared. It might have already raised
4969 * an interrupt */
Zhu Yib481de92007-09-25 17:54:57 -07004970 IWL_WARNING("HARDWARE GONE?? INTA == 0x%080x\n", inta);
Oliver Neukum66fbb542007-11-15 09:31:10 +08004971 goto unplugged;
Zhu Yib481de92007-09-25 17:54:57 -07004972 }
4973
4974 IWL_DEBUG_ISR("ISR inta 0x%08x, enabled 0x%08x, fh 0x%08x\n",
4975 inta, inta_mask, inta_fh);
4976
Joonwoo Park25c03d82008-01-23 10:15:20 -08004977 inta &= ~CSR_INT_BIT_SCD;
4978
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004979 /* iwl4965_irq_tasklet() will service interrupts and re-enable them */
Joonwoo Park25c03d82008-01-23 10:15:20 -08004980 if (likely(inta || inta_fh))
4981 tasklet_schedule(&priv->irq_tasklet);
Zhu Yib481de92007-09-25 17:54:57 -07004982
Oliver Neukum66fbb542007-11-15 09:31:10 +08004983 unplugged:
4984 spin_unlock(&priv->lock);
Zhu Yib481de92007-09-25 17:54:57 -07004985 return IRQ_HANDLED;
4986
4987 none:
4988 /* re-enable interrupts here since we don't have anything to service. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004989 iwl4965_enable_interrupts(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004990 spin_unlock(&priv->lock);
4991 return IRQ_NONE;
4992}
4993
4994/************************** EEPROM BANDS ****************************
4995 *
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004996 * The iwl4965_eeprom_band definitions below provide the mapping from the
Zhu Yib481de92007-09-25 17:54:57 -07004997 * EEPROM contents to the specific channel number supported for each
4998 * band.
4999 *
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005000 * For example, iwl4965_priv->eeprom.band_3_channels[4] from the band_3
Zhu Yib481de92007-09-25 17:54:57 -07005001 * definition below maps to physical channel 42 in the 5.2GHz spectrum.
5002 * The specific geography and calibration information for that channel
5003 * is contained in the eeprom map itself.
5004 *
5005 * During init, we copy the eeprom information and channel map
5006 * information into priv->channel_info_24/52 and priv->channel_map_24/52
5007 *
5008 * channel_map_24/52 provides the index in the channel_info array for a
5009 * given channel. We have to have two separate maps as there is channel
5010 * overlap with the 2.4GHz and 5.2GHz spectrum as seen in band_1 and
5011 * band_2
5012 *
5013 * A value of 0xff stored in the channel_map indicates that the channel
5014 * is not supported by the hardware at all.
5015 *
5016 * A value of 0xfe in the channel_map indicates that the channel is not
5017 * valid for Tx with the current hardware. This means that
5018 * while the system can tune and receive on a given channel, it may not
5019 * be able to associate or transmit any frames on that
5020 * channel. There is no corresponding channel information for that
5021 * entry.
5022 *
5023 *********************************************************************/
5024
5025/* 2.4 GHz */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005026static const u8 iwl4965_eeprom_band_1[14] = {
Zhu Yib481de92007-09-25 17:54:57 -07005027 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14
5028};
5029
5030/* 5.2 GHz bands */
Ben Cahill9fbab512007-11-29 11:09:47 +08005031static const u8 iwl4965_eeprom_band_2[] = { /* 4915-5080MHz */
Zhu Yib481de92007-09-25 17:54:57 -07005032 183, 184, 185, 187, 188, 189, 192, 196, 7, 8, 11, 12, 16
5033};
5034
Ben Cahill9fbab512007-11-29 11:09:47 +08005035static const u8 iwl4965_eeprom_band_3[] = { /* 5170-5320MHz */
Zhu Yib481de92007-09-25 17:54:57 -07005036 34, 36, 38, 40, 42, 44, 46, 48, 52, 56, 60, 64
5037};
5038
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005039static const u8 iwl4965_eeprom_band_4[] = { /* 5500-5700MHz */
Zhu Yib481de92007-09-25 17:54:57 -07005040 100, 104, 108, 112, 116, 120, 124, 128, 132, 136, 140
5041};
5042
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005043static const u8 iwl4965_eeprom_band_5[] = { /* 5725-5825MHz */
Zhu Yib481de92007-09-25 17:54:57 -07005044 145, 149, 153, 157, 161, 165
5045};
5046
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005047static u8 iwl4965_eeprom_band_6[] = { /* 2.4 FAT channel */
Zhu Yib481de92007-09-25 17:54:57 -07005048 1, 2, 3, 4, 5, 6, 7
5049};
5050
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005051static u8 iwl4965_eeprom_band_7[] = { /* 5.2 FAT channel */
Zhu Yib481de92007-09-25 17:54:57 -07005052 36, 44, 52, 60, 100, 108, 116, 124, 132, 149, 157
5053};
5054
Ben Cahill9fbab512007-11-29 11:09:47 +08005055static void iwl4965_init_band_reference(const struct iwl4965_priv *priv,
5056 int band,
Zhu Yib481de92007-09-25 17:54:57 -07005057 int *eeprom_ch_count,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005058 const struct iwl4965_eeprom_channel
Zhu Yib481de92007-09-25 17:54:57 -07005059 **eeprom_ch_info,
5060 const u8 **eeprom_ch_index)
5061{
5062 switch (band) {
5063 case 1: /* 2.4GHz band */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005064 *eeprom_ch_count = ARRAY_SIZE(iwl4965_eeprom_band_1);
Zhu Yib481de92007-09-25 17:54:57 -07005065 *eeprom_ch_info = priv->eeprom.band_1_channels;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005066 *eeprom_ch_index = iwl4965_eeprom_band_1;
Zhu Yib481de92007-09-25 17:54:57 -07005067 break;
Ben Cahill9fbab512007-11-29 11:09:47 +08005068 case 2: /* 4.9GHz band */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005069 *eeprom_ch_count = ARRAY_SIZE(iwl4965_eeprom_band_2);
Zhu Yib481de92007-09-25 17:54:57 -07005070 *eeprom_ch_info = priv->eeprom.band_2_channels;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005071 *eeprom_ch_index = iwl4965_eeprom_band_2;
Zhu Yib481de92007-09-25 17:54:57 -07005072 break;
5073 case 3: /* 5.2GHz band */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005074 *eeprom_ch_count = ARRAY_SIZE(iwl4965_eeprom_band_3);
Zhu Yib481de92007-09-25 17:54:57 -07005075 *eeprom_ch_info = priv->eeprom.band_3_channels;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005076 *eeprom_ch_index = iwl4965_eeprom_band_3;
Zhu Yib481de92007-09-25 17:54:57 -07005077 break;
Ben Cahill9fbab512007-11-29 11:09:47 +08005078 case 4: /* 5.5GHz band */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005079 *eeprom_ch_count = ARRAY_SIZE(iwl4965_eeprom_band_4);
Zhu Yib481de92007-09-25 17:54:57 -07005080 *eeprom_ch_info = priv->eeprom.band_4_channels;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005081 *eeprom_ch_index = iwl4965_eeprom_band_4;
Zhu Yib481de92007-09-25 17:54:57 -07005082 break;
Ben Cahill9fbab512007-11-29 11:09:47 +08005083 case 5: /* 5.7GHz band */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005084 *eeprom_ch_count = ARRAY_SIZE(iwl4965_eeprom_band_5);
Zhu Yib481de92007-09-25 17:54:57 -07005085 *eeprom_ch_info = priv->eeprom.band_5_channels;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005086 *eeprom_ch_index = iwl4965_eeprom_band_5;
Zhu Yib481de92007-09-25 17:54:57 -07005087 break;
Ben Cahill9fbab512007-11-29 11:09:47 +08005088 case 6: /* 2.4GHz FAT channels */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005089 *eeprom_ch_count = ARRAY_SIZE(iwl4965_eeprom_band_6);
Zhu Yib481de92007-09-25 17:54:57 -07005090 *eeprom_ch_info = priv->eeprom.band_24_channels;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005091 *eeprom_ch_index = iwl4965_eeprom_band_6;
Zhu Yib481de92007-09-25 17:54:57 -07005092 break;
Ben Cahill9fbab512007-11-29 11:09:47 +08005093 case 7: /* 5 GHz FAT channels */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005094 *eeprom_ch_count = ARRAY_SIZE(iwl4965_eeprom_band_7);
Zhu Yib481de92007-09-25 17:54:57 -07005095 *eeprom_ch_info = priv->eeprom.band_52_channels;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005096 *eeprom_ch_index = iwl4965_eeprom_band_7;
Zhu Yib481de92007-09-25 17:54:57 -07005097 break;
5098 default:
5099 BUG();
5100 return;
5101 }
5102}
5103
Cahill, Ben M6440adb2007-11-29 11:09:55 +08005104/**
5105 * iwl4965_get_channel_info - Find driver's private channel info
5106 *
5107 * Based on band and channel number.
5108 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005109const struct iwl4965_channel_info *iwl4965_get_channel_info(const struct iwl4965_priv *priv,
Johannes Berg8318d782008-01-24 19:38:38 +01005110 enum ieee80211_band band, u16 channel)
Zhu Yib481de92007-09-25 17:54:57 -07005111{
5112 int i;
5113
Johannes Berg8318d782008-01-24 19:38:38 +01005114 switch (band) {
5115 case IEEE80211_BAND_5GHZ:
Zhu Yib481de92007-09-25 17:54:57 -07005116 for (i = 14; i < priv->channel_count; i++) {
5117 if (priv->channel_info[i].channel == channel)
5118 return &priv->channel_info[i];
5119 }
5120 break;
Johannes Berg8318d782008-01-24 19:38:38 +01005121 case IEEE80211_BAND_2GHZ:
Zhu Yib481de92007-09-25 17:54:57 -07005122 if (channel >= 1 && channel <= 14)
5123 return &priv->channel_info[channel - 1];
5124 break;
Johannes Berg8318d782008-01-24 19:38:38 +01005125 default:
5126 BUG();
Zhu Yib481de92007-09-25 17:54:57 -07005127 }
5128
5129 return NULL;
5130}
5131
5132#define CHECK_AND_PRINT(x) ((eeprom_ch_info[ch].flags & EEPROM_CHANNEL_##x) \
5133 ? # x " " : "")
5134
Cahill, Ben M6440adb2007-11-29 11:09:55 +08005135/**
5136 * iwl4965_init_channel_map - Set up driver's info for all possible channels
5137 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005138static int iwl4965_init_channel_map(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005139{
5140 int eeprom_ch_count = 0;
5141 const u8 *eeprom_ch_index = NULL;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005142 const struct iwl4965_eeprom_channel *eeprom_ch_info = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07005143 int band, ch;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005144 struct iwl4965_channel_info *ch_info;
Zhu Yib481de92007-09-25 17:54:57 -07005145
5146 if (priv->channel_count) {
5147 IWL_DEBUG_INFO("Channel map already initialized.\n");
5148 return 0;
5149 }
5150
5151 if (priv->eeprom.version < 0x2f) {
5152 IWL_WARNING("Unsupported EEPROM version: 0x%04X\n",
5153 priv->eeprom.version);
5154 return -EINVAL;
5155 }
5156
5157 IWL_DEBUG_INFO("Initializing regulatory info from EEPROM\n");
5158
5159 priv->channel_count =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005160 ARRAY_SIZE(iwl4965_eeprom_band_1) +
5161 ARRAY_SIZE(iwl4965_eeprom_band_2) +
5162 ARRAY_SIZE(iwl4965_eeprom_band_3) +
5163 ARRAY_SIZE(iwl4965_eeprom_band_4) +
5164 ARRAY_SIZE(iwl4965_eeprom_band_5);
Zhu Yib481de92007-09-25 17:54:57 -07005165
5166 IWL_DEBUG_INFO("Parsing data for %d channels.\n", priv->channel_count);
5167
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005168 priv->channel_info = kzalloc(sizeof(struct iwl4965_channel_info) *
Zhu Yib481de92007-09-25 17:54:57 -07005169 priv->channel_count, GFP_KERNEL);
5170 if (!priv->channel_info) {
5171 IWL_ERROR("Could not allocate channel_info\n");
5172 priv->channel_count = 0;
5173 return -ENOMEM;
5174 }
5175
5176 ch_info = priv->channel_info;
5177
5178 /* Loop through the 5 EEPROM bands adding them in order to the
5179 * channel map we maintain (that contains additional information than
5180 * what just in the EEPROM) */
5181 for (band = 1; band <= 5; band++) {
5182
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005183 iwl4965_init_band_reference(priv, band, &eeprom_ch_count,
Zhu Yib481de92007-09-25 17:54:57 -07005184 &eeprom_ch_info, &eeprom_ch_index);
5185
5186 /* Loop through each band adding each of the channels */
5187 for (ch = 0; ch < eeprom_ch_count; ch++) {
5188 ch_info->channel = eeprom_ch_index[ch];
Johannes Berg8318d782008-01-24 19:38:38 +01005189 ch_info->band = (band == 1) ? IEEE80211_BAND_2GHZ :
5190 IEEE80211_BAND_5GHZ;
Zhu Yib481de92007-09-25 17:54:57 -07005191
5192 /* permanently store EEPROM's channel regulatory flags
5193 * and max power in channel info database. */
5194 ch_info->eeprom = eeprom_ch_info[ch];
5195
5196 /* Copy the run-time flags so they are there even on
5197 * invalid channels */
5198 ch_info->flags = eeprom_ch_info[ch].flags;
5199
5200 if (!(is_channel_valid(ch_info))) {
5201 IWL_DEBUG_INFO("Ch. %d Flags %x [%sGHz] - "
5202 "No traffic\n",
5203 ch_info->channel,
5204 ch_info->flags,
5205 is_channel_a_band(ch_info) ?
5206 "5.2" : "2.4");
5207 ch_info++;
5208 continue;
5209 }
5210
5211 /* Initialize regulatory-based run-time data */
5212 ch_info->max_power_avg = ch_info->curr_txpow =
5213 eeprom_ch_info[ch].max_power_avg;
5214 ch_info->scan_power = eeprom_ch_info[ch].max_power_avg;
5215 ch_info->min_power = 0;
5216
Tomas Winkler8211ef72008-03-02 01:36:04 +02005217 IWL_DEBUG_INFO("Ch. %d [%sGHz] %s%s%s%s%s%s%s(0x%02x"
Zhu Yib481de92007-09-25 17:54:57 -07005218 " %ddBm): Ad-Hoc %ssupported\n",
5219 ch_info->channel,
5220 is_channel_a_band(ch_info) ?
5221 "5.2" : "2.4",
Tomas Winkler8211ef72008-03-02 01:36:04 +02005222 CHECK_AND_PRINT(VALID),
Zhu Yib481de92007-09-25 17:54:57 -07005223 CHECK_AND_PRINT(IBSS),
5224 CHECK_AND_PRINT(ACTIVE),
5225 CHECK_AND_PRINT(RADAR),
5226 CHECK_AND_PRINT(WIDE),
5227 CHECK_AND_PRINT(NARROW),
5228 CHECK_AND_PRINT(DFS),
5229 eeprom_ch_info[ch].flags,
5230 eeprom_ch_info[ch].max_power_avg,
5231 ((eeprom_ch_info[ch].
5232 flags & EEPROM_CHANNEL_IBSS)
5233 && !(eeprom_ch_info[ch].
5234 flags & EEPROM_CHANNEL_RADAR))
5235 ? "" : "not ");
5236
5237 /* Set the user_txpower_limit to the highest power
5238 * supported by any channel */
5239 if (eeprom_ch_info[ch].max_power_avg >
5240 priv->user_txpower_limit)
5241 priv->user_txpower_limit =
5242 eeprom_ch_info[ch].max_power_avg;
5243
5244 ch_info++;
5245 }
5246 }
5247
Cahill, Ben M6440adb2007-11-29 11:09:55 +08005248 /* Two additional EEPROM bands for 2.4 and 5 GHz FAT channels */
Zhu Yib481de92007-09-25 17:54:57 -07005249 for (band = 6; band <= 7; band++) {
Johannes Berg8318d782008-01-24 19:38:38 +01005250 enum ieee80211_band ieeeband;
Zhu Yib481de92007-09-25 17:54:57 -07005251 u8 fat_extension_chan;
5252
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005253 iwl4965_init_band_reference(priv, band, &eeprom_ch_count,
Zhu Yib481de92007-09-25 17:54:57 -07005254 &eeprom_ch_info, &eeprom_ch_index);
5255
Cahill, Ben M6440adb2007-11-29 11:09:55 +08005256 /* EEPROM band 6 is 2.4, band 7 is 5 GHz */
Johannes Berg8318d782008-01-24 19:38:38 +01005257 ieeeband = (band == 6) ? IEEE80211_BAND_2GHZ : IEEE80211_BAND_5GHZ;
Cahill, Ben M6440adb2007-11-29 11:09:55 +08005258
Zhu Yib481de92007-09-25 17:54:57 -07005259 /* Loop through each band adding each of the channels */
5260 for (ch = 0; ch < eeprom_ch_count; ch++) {
5261
5262 if ((band == 6) &&
5263 ((eeprom_ch_index[ch] == 5) ||
5264 (eeprom_ch_index[ch] == 6) ||
5265 (eeprom_ch_index[ch] == 7)))
5266 fat_extension_chan = HT_IE_EXT_CHANNEL_MAX;
5267 else
5268 fat_extension_chan = HT_IE_EXT_CHANNEL_ABOVE;
5269
Cahill, Ben M6440adb2007-11-29 11:09:55 +08005270 /* Set up driver's info for lower half */
Johannes Berg8318d782008-01-24 19:38:38 +01005271 iwl4965_set_fat_chan_info(priv, ieeeband,
Zhu Yib481de92007-09-25 17:54:57 -07005272 eeprom_ch_index[ch],
5273 &(eeprom_ch_info[ch]),
5274 fat_extension_chan);
5275
Cahill, Ben M6440adb2007-11-29 11:09:55 +08005276 /* Set up driver's info for upper half */
Johannes Berg8318d782008-01-24 19:38:38 +01005277 iwl4965_set_fat_chan_info(priv, ieeeband,
Zhu Yib481de92007-09-25 17:54:57 -07005278 (eeprom_ch_index[ch] + 4),
5279 &(eeprom_ch_info[ch]),
5280 HT_IE_EXT_CHANNEL_BELOW);
5281 }
5282 }
5283
5284 return 0;
5285}
5286
Reinette Chatre849e0dc2008-01-23 10:15:18 -08005287/*
5288 * iwl4965_free_channel_map - undo allocations in iwl4965_init_channel_map
5289 */
5290static void iwl4965_free_channel_map(struct iwl4965_priv *priv)
5291{
5292 kfree(priv->channel_info);
5293 priv->channel_count = 0;
5294}
5295
Zhu Yib481de92007-09-25 17:54:57 -07005296/* For active scan, listen ACTIVE_DWELL_TIME (msec) on each channel after
5297 * sending probe req. This should be set long enough to hear probe responses
5298 * from more than one AP. */
5299#define IWL_ACTIVE_DWELL_TIME_24 (20) /* all times in msec */
5300#define IWL_ACTIVE_DWELL_TIME_52 (10)
5301
5302/* For faster active scanning, scan will move to the next channel if fewer than
5303 * PLCP_QUIET_THRESH packets are heard on this channel within
5304 * ACTIVE_QUIET_TIME after sending probe request. This shortens the dwell
5305 * time if it's a quiet channel (nothing responded to our probe, and there's
5306 * no other traffic).
5307 * Disable "quiet" feature by setting PLCP_QUIET_THRESH to 0. */
5308#define IWL_PLCP_QUIET_THRESH __constant_cpu_to_le16(1) /* packets */
5309#define IWL_ACTIVE_QUIET_TIME __constant_cpu_to_le16(5) /* msec */
5310
5311/* For passive scan, listen PASSIVE_DWELL_TIME (msec) on each channel.
5312 * Must be set longer than active dwell time.
5313 * For the most reliable scan, set > AP beacon interval (typically 100msec). */
5314#define IWL_PASSIVE_DWELL_TIME_24 (20) /* all times in msec */
5315#define IWL_PASSIVE_DWELL_TIME_52 (10)
5316#define IWL_PASSIVE_DWELL_BASE (100)
5317#define IWL_CHANNEL_TUNE_TIME 5
5318
Johannes Berg8318d782008-01-24 19:38:38 +01005319static inline u16 iwl4965_get_active_dwell_time(struct iwl4965_priv *priv,
5320 enum ieee80211_band band)
Zhu Yib481de92007-09-25 17:54:57 -07005321{
Johannes Berg8318d782008-01-24 19:38:38 +01005322 if (band == IEEE80211_BAND_5GHZ)
Zhu Yib481de92007-09-25 17:54:57 -07005323 return IWL_ACTIVE_DWELL_TIME_52;
5324 else
5325 return IWL_ACTIVE_DWELL_TIME_24;
5326}
5327
Johannes Berg8318d782008-01-24 19:38:38 +01005328static u16 iwl4965_get_passive_dwell_time(struct iwl4965_priv *priv,
5329 enum ieee80211_band band)
Zhu Yib481de92007-09-25 17:54:57 -07005330{
Johannes Berg8318d782008-01-24 19:38:38 +01005331 u16 active = iwl4965_get_active_dwell_time(priv, band);
5332 u16 passive = (band != IEEE80211_BAND_5GHZ) ?
Zhu Yib481de92007-09-25 17:54:57 -07005333 IWL_PASSIVE_DWELL_BASE + IWL_PASSIVE_DWELL_TIME_24 :
5334 IWL_PASSIVE_DWELL_BASE + IWL_PASSIVE_DWELL_TIME_52;
5335
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005336 if (iwl4965_is_associated(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07005337 /* If we're associated, we clamp the maximum passive
5338 * dwell time to be 98% of the beacon interval (minus
5339 * 2 * channel tune time) */
5340 passive = priv->beacon_int;
5341 if ((passive > IWL_PASSIVE_DWELL_BASE) || !passive)
5342 passive = IWL_PASSIVE_DWELL_BASE;
5343 passive = (passive * 98) / 100 - IWL_CHANNEL_TUNE_TIME * 2;
5344 }
5345
5346 if (passive <= active)
5347 passive = active + 1;
5348
5349 return passive;
5350}
5351
Johannes Berg8318d782008-01-24 19:38:38 +01005352static int iwl4965_get_channels_for_scan(struct iwl4965_priv *priv,
5353 enum ieee80211_band band,
Zhu Yib481de92007-09-25 17:54:57 -07005354 u8 is_active, u8 direct_mask,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005355 struct iwl4965_scan_channel *scan_ch)
Zhu Yib481de92007-09-25 17:54:57 -07005356{
5357 const struct ieee80211_channel *channels = NULL;
Johannes Berg8318d782008-01-24 19:38:38 +01005358 const struct ieee80211_supported_band *sband;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005359 const struct iwl4965_channel_info *ch_info;
Zhu Yib481de92007-09-25 17:54:57 -07005360 u16 passive_dwell = 0;
5361 u16 active_dwell = 0;
5362 int added, i;
5363
Johannes Berg8318d782008-01-24 19:38:38 +01005364 sband = iwl4965_get_hw_mode(priv, band);
5365 if (!sband)
Zhu Yib481de92007-09-25 17:54:57 -07005366 return 0;
5367
Johannes Berg8318d782008-01-24 19:38:38 +01005368 channels = sband->channels;
Zhu Yib481de92007-09-25 17:54:57 -07005369
Johannes Berg8318d782008-01-24 19:38:38 +01005370 active_dwell = iwl4965_get_active_dwell_time(priv, band);
5371 passive_dwell = iwl4965_get_passive_dwell_time(priv, band);
Zhu Yib481de92007-09-25 17:54:57 -07005372
Johannes Berg8318d782008-01-24 19:38:38 +01005373 for (i = 0, added = 0; i < sband->n_channels; i++) {
5374 if (ieee80211_frequency_to_channel(channels[i].center_freq) ==
Zhu Yib481de92007-09-25 17:54:57 -07005375 le16_to_cpu(priv->active_rxon.channel)) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005376 if (iwl4965_is_associated(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07005377 IWL_DEBUG_SCAN
5378 ("Skipping current channel %d\n",
5379 le16_to_cpu(priv->active_rxon.channel));
5380 continue;
5381 }
5382 } else if (priv->only_active_channel)
5383 continue;
5384
Johannes Berg8318d782008-01-24 19:38:38 +01005385 scan_ch->channel = ieee80211_frequency_to_channel(channels[i].center_freq);
Zhu Yib481de92007-09-25 17:54:57 -07005386
Johannes Berg8318d782008-01-24 19:38:38 +01005387 ch_info = iwl4965_get_channel_info(priv, band,
Ben Cahill9fbab512007-11-29 11:09:47 +08005388 scan_ch->channel);
Zhu Yib481de92007-09-25 17:54:57 -07005389 if (!is_channel_valid(ch_info)) {
5390 IWL_DEBUG_SCAN("Channel %d is INVALID for this SKU.\n",
5391 scan_ch->channel);
5392 continue;
5393 }
5394
5395 if (!is_active || is_channel_passive(ch_info) ||
Johannes Berg8318d782008-01-24 19:38:38 +01005396 (channels[i].flags & IEEE80211_CHAN_PASSIVE_SCAN))
Zhu Yib481de92007-09-25 17:54:57 -07005397 scan_ch->type = 0; /* passive */
5398 else
5399 scan_ch->type = 1; /* active */
5400
5401 if (scan_ch->type & 1)
5402 scan_ch->type |= (direct_mask << 1);
5403
5404 if (is_channel_narrow(ch_info))
5405 scan_ch->type |= (1 << 7);
5406
5407 scan_ch->active_dwell = cpu_to_le16(active_dwell);
5408 scan_ch->passive_dwell = cpu_to_le16(passive_dwell);
5409
Ben Cahill9fbab512007-11-29 11:09:47 +08005410 /* Set txpower levels to defaults */
Zhu Yib481de92007-09-25 17:54:57 -07005411 scan_ch->tpc.dsp_atten = 110;
5412 /* scan_pwr_info->tpc.dsp_atten; */
5413
5414 /*scan_pwr_info->tpc.tx_gain; */
Johannes Berg8318d782008-01-24 19:38:38 +01005415 if (band == IEEE80211_BAND_5GHZ)
Zhu Yib481de92007-09-25 17:54:57 -07005416 scan_ch->tpc.tx_gain = ((1 << 5) | (3 << 3)) | 3;
5417 else {
5418 scan_ch->tpc.tx_gain = ((1 << 5) | (5 << 3));
5419 /* NOTE: if we were doing 6Mb OFDM for scans we'd use
Ben Cahill9fbab512007-11-29 11:09:47 +08005420 * power level:
Reinette Chatre8a1b0242008-01-14 17:46:25 -08005421 * scan_ch->tpc.tx_gain = ((1 << 5) | (2 << 3)) | 3;
Zhu Yib481de92007-09-25 17:54:57 -07005422 */
5423 }
5424
5425 IWL_DEBUG_SCAN("Scanning %d [%s %d]\n",
5426 scan_ch->channel,
5427 (scan_ch->type & 1) ? "ACTIVE" : "PASSIVE",
5428 (scan_ch->type & 1) ?
5429 active_dwell : passive_dwell);
5430
5431 scan_ch++;
5432 added++;
5433 }
5434
5435 IWL_DEBUG_SCAN("total channels to scan %d \n", added);
5436 return added;
5437}
5438
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005439static void iwl4965_init_hw_rates(struct iwl4965_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07005440 struct ieee80211_rate *rates)
5441{
5442 int i;
5443
5444 for (i = 0; i < IWL_RATE_COUNT; i++) {
Johannes Berg8318d782008-01-24 19:38:38 +01005445 rates[i].bitrate = iwl4965_rates[i].ieee * 5;
5446 rates[i].hw_value = i; /* Rate scaling will work on indexes */
5447 rates[i].hw_value_short = i;
5448 rates[i].flags = 0;
5449 if ((i > IWL_LAST_OFDM_RATE) || (i < IWL_FIRST_OFDM_RATE)) {
Zhu Yib481de92007-09-25 17:54:57 -07005450 /*
Johannes Berg8318d782008-01-24 19:38:38 +01005451 * If CCK != 1M then set short preamble rate flag.
Zhu Yib481de92007-09-25 17:54:57 -07005452 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005453 rates[i].flags |= (iwl4965_rates[i].plcp == 10) ?
Johannes Berg8318d782008-01-24 19:38:38 +01005454 0 : IEEE80211_RATE_SHORT_PREAMBLE;
Zhu Yib481de92007-09-25 17:54:57 -07005455 }
Zhu Yib481de92007-09-25 17:54:57 -07005456 }
Zhu Yib481de92007-09-25 17:54:57 -07005457}
5458
5459/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005460 * iwl4965_init_geos - Initialize mac80211's geo/channel info based from eeprom
Zhu Yib481de92007-09-25 17:54:57 -07005461 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005462static int iwl4965_init_geos(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005463{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005464 struct iwl4965_channel_info *ch;
Tomas Winkler8211ef72008-03-02 01:36:04 +02005465 struct ieee80211_supported_band *sband;
Zhu Yib481de92007-09-25 17:54:57 -07005466 struct ieee80211_channel *channels;
5467 struct ieee80211_channel *geo_ch;
5468 struct ieee80211_rate *rates;
5469 int i = 0;
Zhu Yib481de92007-09-25 17:54:57 -07005470
Johannes Berg8318d782008-01-24 19:38:38 +01005471 if (priv->bands[IEEE80211_BAND_2GHZ].n_bitrates ||
5472 priv->bands[IEEE80211_BAND_5GHZ].n_bitrates) {
Zhu Yib481de92007-09-25 17:54:57 -07005473 IWL_DEBUG_INFO("Geography modes already initialized.\n");
5474 set_bit(STATUS_GEO_CONFIGURED, &priv->status);
5475 return 0;
5476 }
5477
Zhu Yib481de92007-09-25 17:54:57 -07005478 channels = kzalloc(sizeof(struct ieee80211_channel) *
5479 priv->channel_count, GFP_KERNEL);
Johannes Berg8318d782008-01-24 19:38:38 +01005480 if (!channels)
Zhu Yib481de92007-09-25 17:54:57 -07005481 return -ENOMEM;
Zhu Yib481de92007-09-25 17:54:57 -07005482
Tomas Winkler8211ef72008-03-02 01:36:04 +02005483 rates = kzalloc((sizeof(struct ieee80211_rate) * (IWL_RATE_COUNT + 1)),
Zhu Yib481de92007-09-25 17:54:57 -07005484 GFP_KERNEL);
5485 if (!rates) {
Zhu Yib481de92007-09-25 17:54:57 -07005486 kfree(channels);
5487 return -ENOMEM;
5488 }
5489
Zhu Yib481de92007-09-25 17:54:57 -07005490 /* 5.2GHz channels start after the 2.4GHz channels */
Tomas Winkler8211ef72008-03-02 01:36:04 +02005491 sband = &priv->bands[IEEE80211_BAND_5GHZ];
5492 sband->channels = &channels[ARRAY_SIZE(iwl4965_eeprom_band_1)];
5493 /* just OFDM */
5494 sband->bitrates = &rates[IWL_FIRST_OFDM_RATE];
5495 sband->n_bitrates = IWL_RATE_COUNT - IWL_FIRST_OFDM_RATE;
Johannes Berg8318d782008-01-24 19:38:38 +01005496
Tomas Winkler8211ef72008-03-02 01:36:04 +02005497 iwl4965_init_ht_hw_capab(&sband->ht_info, IEEE80211_BAND_5GHZ);
Tomas Winkler78330fd2008-02-06 02:37:18 +02005498
Tomas Winkler8211ef72008-03-02 01:36:04 +02005499 sband = &priv->bands[IEEE80211_BAND_2GHZ];
5500 sband->channels = channels;
5501 /* OFDM & CCK */
5502 sband->bitrates = rates;
5503 sband->n_bitrates = IWL_RATE_COUNT;
Zhu Yib481de92007-09-25 17:54:57 -07005504
Tomas Winkler8211ef72008-03-02 01:36:04 +02005505 iwl4965_init_ht_hw_capab(&sband->ht_info, IEEE80211_BAND_2GHZ);
Tomas Winkler78330fd2008-02-06 02:37:18 +02005506
Zhu Yib481de92007-09-25 17:54:57 -07005507 priv->ieee_channels = channels;
5508 priv->ieee_rates = rates;
5509
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005510 iwl4965_init_hw_rates(priv, rates);
Zhu Yib481de92007-09-25 17:54:57 -07005511
Tomas Winkler8211ef72008-03-02 01:36:04 +02005512 for (i = 0; i < priv->channel_count; i++) {
Zhu Yib481de92007-09-25 17:54:57 -07005513 ch = &priv->channel_info[i];
5514
Tomas Winkler8211ef72008-03-02 01:36:04 +02005515 /* FIXME: might be removed if scan is OK */
5516 if (!is_channel_valid(ch))
Zhu Yib481de92007-09-25 17:54:57 -07005517 continue;
Zhu Yib481de92007-09-25 17:54:57 -07005518
Tomas Winkler8211ef72008-03-02 01:36:04 +02005519 if (is_channel_a_band(ch))
5520 sband = &priv->bands[IEEE80211_BAND_5GHZ];
5521 else
5522 sband = &priv->bands[IEEE80211_BAND_2GHZ];
Zhu Yib481de92007-09-25 17:54:57 -07005523
Tomas Winkler8211ef72008-03-02 01:36:04 +02005524 geo_ch = &sband->channels[sband->n_channels++];
5525
5526 geo_ch->center_freq = ieee80211_channel_to_frequency(ch->channel);
Johannes Berg8318d782008-01-24 19:38:38 +01005527 geo_ch->max_power = ch->max_power_avg;
5528 geo_ch->max_antenna_gain = 0xff;
Mohamed Abbas7b723042008-01-31 21:46:40 -08005529 geo_ch->hw_value = ch->channel;
Zhu Yib481de92007-09-25 17:54:57 -07005530
5531 if (is_channel_valid(ch)) {
Johannes Berg8318d782008-01-24 19:38:38 +01005532 if (!(ch->flags & EEPROM_CHANNEL_IBSS))
5533 geo_ch->flags |= IEEE80211_CHAN_NO_IBSS;
Zhu Yib481de92007-09-25 17:54:57 -07005534
Johannes Berg8318d782008-01-24 19:38:38 +01005535 if (!(ch->flags & EEPROM_CHANNEL_ACTIVE))
5536 geo_ch->flags |= IEEE80211_CHAN_PASSIVE_SCAN;
Zhu Yib481de92007-09-25 17:54:57 -07005537
5538 if (ch->flags & EEPROM_CHANNEL_RADAR)
Johannes Berg8318d782008-01-24 19:38:38 +01005539 geo_ch->flags |= IEEE80211_CHAN_RADAR;
Zhu Yib481de92007-09-25 17:54:57 -07005540
5541 if (ch->max_power_avg > priv->max_channel_txpower_limit)
5542 priv->max_channel_txpower_limit =
5543 ch->max_power_avg;
Tomas Winkler8211ef72008-03-02 01:36:04 +02005544 } else {
Johannes Berg8318d782008-01-24 19:38:38 +01005545 geo_ch->flags |= IEEE80211_CHAN_DISABLED;
Tomas Winkler8211ef72008-03-02 01:36:04 +02005546 }
5547
5548 /* Save flags for reg domain usage */
5549 geo_ch->orig_flags = geo_ch->flags;
5550
5551 IWL_DEBUG_INFO("Channel %d Freq=%d[%sGHz] %s flag=0%X\n",
5552 ch->channel, geo_ch->center_freq,
5553 is_channel_a_band(ch) ? "5.2" : "2.4",
5554 geo_ch->flags & IEEE80211_CHAN_DISABLED ?
5555 "restricted" : "valid",
5556 geo_ch->flags);
Zhu Yib481de92007-09-25 17:54:57 -07005557 }
5558
Tomas Winkler82b9a122008-03-04 18:09:30 -08005559 if ((priv->bands[IEEE80211_BAND_5GHZ].n_channels == 0) &&
5560 priv->cfg->sku & IWL_SKU_A) {
Zhu Yib481de92007-09-25 17:54:57 -07005561 printk(KERN_INFO DRV_NAME
5562 ": Incorrectly detected BG card as ABG. Please send "
5563 "your PCI ID 0x%04X:0x%04X to maintainer.\n",
5564 priv->pci_dev->device, priv->pci_dev->subsystem_device);
Tomas Winkler82b9a122008-03-04 18:09:30 -08005565 priv->cfg->sku &= ~IWL_SKU_A;
Zhu Yib481de92007-09-25 17:54:57 -07005566 }
5567
5568 printk(KERN_INFO DRV_NAME
5569 ": Tunable channels: %d 802.11bg, %d 802.11a channels\n",
Johannes Berg8318d782008-01-24 19:38:38 +01005570 priv->bands[IEEE80211_BAND_2GHZ].n_channels,
5571 priv->bands[IEEE80211_BAND_5GHZ].n_channels);
Zhu Yib481de92007-09-25 17:54:57 -07005572
Johannes Berg8318d782008-01-24 19:38:38 +01005573 priv->hw->wiphy->bands[IEEE80211_BAND_2GHZ] = &priv->bands[IEEE80211_BAND_2GHZ];
5574 priv->hw->wiphy->bands[IEEE80211_BAND_5GHZ] = &priv->bands[IEEE80211_BAND_5GHZ];
Zhu Yib481de92007-09-25 17:54:57 -07005575
Zhu Yib481de92007-09-25 17:54:57 -07005576 set_bit(STATUS_GEO_CONFIGURED, &priv->status);
5577
5578 return 0;
5579}
5580
Reinette Chatre849e0dc2008-01-23 10:15:18 -08005581/*
5582 * iwl4965_free_geos - undo allocations in iwl4965_init_geos
5583 */
5584static void iwl4965_free_geos(struct iwl4965_priv *priv)
5585{
Reinette Chatre849e0dc2008-01-23 10:15:18 -08005586 kfree(priv->ieee_channels);
5587 kfree(priv->ieee_rates);
5588 clear_bit(STATUS_GEO_CONFIGURED, &priv->status);
5589}
5590
Zhu Yib481de92007-09-25 17:54:57 -07005591/******************************************************************************
5592 *
5593 * uCode download functions
5594 *
5595 ******************************************************************************/
5596
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005597static void iwl4965_dealloc_ucode_pci(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005598{
Tomas Winkler98c92212008-01-14 17:46:20 -08005599 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_code);
5600 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_data);
5601 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_data_backup);
5602 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_init);
5603 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_init_data);
5604 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_boot);
Zhu Yib481de92007-09-25 17:54:57 -07005605}
5606
5607/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005608 * iwl4965_verify_inst_full - verify runtime uCode image in card vs. host,
Zhu Yib481de92007-09-25 17:54:57 -07005609 * looking at all data.
5610 */
Tomas Winkler4fd1f842007-12-05 20:59:58 +02005611static int iwl4965_verify_inst_full(struct iwl4965_priv *priv, __le32 *image,
Ben Cahill9fbab512007-11-29 11:09:47 +08005612 u32 len)
Zhu Yib481de92007-09-25 17:54:57 -07005613{
5614 u32 val;
5615 u32 save_len = len;
5616 int rc = 0;
5617 u32 errcnt;
5618
5619 IWL_DEBUG_INFO("ucode inst image size is %u\n", len);
5620
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005621 rc = iwl4965_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005622 if (rc)
5623 return rc;
5624
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005625 iwl4965_write_direct32(priv, HBUS_TARG_MEM_RADDR, RTC_INST_LOWER_BOUND);
Zhu Yib481de92007-09-25 17:54:57 -07005626
5627 errcnt = 0;
5628 for (; len > 0; len -= sizeof(u32), image++) {
5629 /* read data comes through single port, auto-incr addr */
5630 /* NOTE: Use the debugless read so we don't flood kernel log
5631 * if IWL_DL_IO is set */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005632 val = _iwl4965_read_direct32(priv, HBUS_TARG_MEM_RDAT);
Zhu Yib481de92007-09-25 17:54:57 -07005633 if (val != le32_to_cpu(*image)) {
5634 IWL_ERROR("uCode INST section is invalid at "
5635 "offset 0x%x, is 0x%x, s/b 0x%x\n",
5636 save_len - len, val, le32_to_cpu(*image));
5637 rc = -EIO;
5638 errcnt++;
5639 if (errcnt >= 20)
5640 break;
5641 }
5642 }
5643
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005644 iwl4965_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005645
5646 if (!errcnt)
5647 IWL_DEBUG_INFO
5648 ("ucode image in INSTRUCTION memory is good\n");
5649
5650 return rc;
5651}
5652
5653
5654/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005655 * iwl4965_verify_inst_sparse - verify runtime uCode image in card vs. host,
Zhu Yib481de92007-09-25 17:54:57 -07005656 * using sample data 100 bytes apart. If these sample points are good,
5657 * it's a pretty good bet that everything between them is good, too.
5658 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005659static int iwl4965_verify_inst_sparse(struct iwl4965_priv *priv, __le32 *image, u32 len)
Zhu Yib481de92007-09-25 17:54:57 -07005660{
5661 u32 val;
5662 int rc = 0;
5663 u32 errcnt = 0;
5664 u32 i;
5665
5666 IWL_DEBUG_INFO("ucode inst image size is %u\n", len);
5667
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005668 rc = iwl4965_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005669 if (rc)
5670 return rc;
5671
5672 for (i = 0; i < len; i += 100, image += 100/sizeof(u32)) {
5673 /* read data comes through single port, auto-incr addr */
5674 /* NOTE: Use the debugless read so we don't flood kernel log
5675 * if IWL_DL_IO is set */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005676 iwl4965_write_direct32(priv, HBUS_TARG_MEM_RADDR,
Zhu Yib481de92007-09-25 17:54:57 -07005677 i + RTC_INST_LOWER_BOUND);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005678 val = _iwl4965_read_direct32(priv, HBUS_TARG_MEM_RDAT);
Zhu Yib481de92007-09-25 17:54:57 -07005679 if (val != le32_to_cpu(*image)) {
5680#if 0 /* Enable this if you want to see details */
5681 IWL_ERROR("uCode INST section is invalid at "
5682 "offset 0x%x, is 0x%x, s/b 0x%x\n",
5683 i, val, *image);
5684#endif
5685 rc = -EIO;
5686 errcnt++;
5687 if (errcnt >= 3)
5688 break;
5689 }
5690 }
5691
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005692 iwl4965_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005693
5694 return rc;
5695}
5696
5697
5698/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005699 * iwl4965_verify_ucode - determine which instruction image is in SRAM,
Zhu Yib481de92007-09-25 17:54:57 -07005700 * and verify its contents
5701 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005702static int iwl4965_verify_ucode(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005703{
5704 __le32 *image;
5705 u32 len;
5706 int rc = 0;
5707
5708 /* Try bootstrap */
5709 image = (__le32 *)priv->ucode_boot.v_addr;
5710 len = priv->ucode_boot.len;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005711 rc = iwl4965_verify_inst_sparse(priv, image, len);
Zhu Yib481de92007-09-25 17:54:57 -07005712 if (rc == 0) {
5713 IWL_DEBUG_INFO("Bootstrap uCode is good in inst SRAM\n");
5714 return 0;
5715 }
5716
5717 /* Try initialize */
5718 image = (__le32 *)priv->ucode_init.v_addr;
5719 len = priv->ucode_init.len;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005720 rc = iwl4965_verify_inst_sparse(priv, image, len);
Zhu Yib481de92007-09-25 17:54:57 -07005721 if (rc == 0) {
5722 IWL_DEBUG_INFO("Initialize uCode is good in inst SRAM\n");
5723 return 0;
5724 }
5725
5726 /* Try runtime/protocol */
5727 image = (__le32 *)priv->ucode_code.v_addr;
5728 len = priv->ucode_code.len;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005729 rc = iwl4965_verify_inst_sparse(priv, image, len);
Zhu Yib481de92007-09-25 17:54:57 -07005730 if (rc == 0) {
5731 IWL_DEBUG_INFO("Runtime uCode is good in inst SRAM\n");
5732 return 0;
5733 }
5734
5735 IWL_ERROR("NO VALID UCODE IMAGE IN INSTRUCTION SRAM!!\n");
5736
Ben Cahill9fbab512007-11-29 11:09:47 +08005737 /* Since nothing seems to match, show first several data entries in
5738 * instruction SRAM, so maybe visual inspection will give a clue.
5739 * Selection of bootstrap image (vs. other images) is arbitrary. */
Zhu Yib481de92007-09-25 17:54:57 -07005740 image = (__le32 *)priv->ucode_boot.v_addr;
5741 len = priv->ucode_boot.len;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005742 rc = iwl4965_verify_inst_full(priv, image, len);
Zhu Yib481de92007-09-25 17:54:57 -07005743
5744 return rc;
5745}
5746
5747
5748/* check contents of special bootstrap uCode SRAM */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005749static int iwl4965_verify_bsm(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005750{
5751 __le32 *image = priv->ucode_boot.v_addr;
5752 u32 len = priv->ucode_boot.len;
5753 u32 reg;
5754 u32 val;
5755
5756 IWL_DEBUG_INFO("Begin verify bsm\n");
5757
5758 /* verify BSM SRAM contents */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005759 val = iwl4965_read_prph(priv, BSM_WR_DWCOUNT_REG);
Zhu Yib481de92007-09-25 17:54:57 -07005760 for (reg = BSM_SRAM_LOWER_BOUND;
5761 reg < BSM_SRAM_LOWER_BOUND + len;
5762 reg += sizeof(u32), image ++) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005763 val = iwl4965_read_prph(priv, reg);
Zhu Yib481de92007-09-25 17:54:57 -07005764 if (val != le32_to_cpu(*image)) {
5765 IWL_ERROR("BSM uCode verification failed at "
5766 "addr 0x%08X+%u (of %u), is 0x%x, s/b 0x%x\n",
5767 BSM_SRAM_LOWER_BOUND,
5768 reg - BSM_SRAM_LOWER_BOUND, len,
5769 val, le32_to_cpu(*image));
5770 return -EIO;
5771 }
5772 }
5773
5774 IWL_DEBUG_INFO("BSM bootstrap uCode image OK\n");
5775
5776 return 0;
5777}
5778
5779/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005780 * iwl4965_load_bsm - Load bootstrap instructions
Zhu Yib481de92007-09-25 17:54:57 -07005781 *
5782 * BSM operation:
5783 *
5784 * The Bootstrap State Machine (BSM) stores a short bootstrap uCode program
5785 * in special SRAM that does not power down during RFKILL. When powering back
5786 * up after power-saving sleeps (or during initial uCode load), the BSM loads
5787 * the bootstrap program into the on-board processor, and starts it.
5788 *
5789 * The bootstrap program loads (via DMA) instructions and data for a new
5790 * program from host DRAM locations indicated by the host driver in the
5791 * BSM_DRAM_* registers. Once the new program is loaded, it starts
5792 * automatically.
5793 *
5794 * When initializing the NIC, the host driver points the BSM to the
5795 * "initialize" uCode image. This uCode sets up some internal data, then
5796 * notifies host via "initialize alive" that it is complete.
5797 *
5798 * The host then replaces the BSM_DRAM_* pointer values to point to the
5799 * normal runtime uCode instructions and a backup uCode data cache buffer
5800 * (filled initially with starting data values for the on-board processor),
5801 * then triggers the "initialize" uCode to load and launch the runtime uCode,
5802 * which begins normal operation.
5803 *
5804 * When doing a power-save shutdown, runtime uCode saves data SRAM into
5805 * the backup data cache in DRAM before SRAM is powered down.
5806 *
5807 * When powering back up, the BSM loads the bootstrap program. This reloads
5808 * the runtime uCode instructions and the backup data cache into SRAM,
5809 * and re-launches the runtime uCode from where it left off.
5810 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005811static int iwl4965_load_bsm(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005812{
5813 __le32 *image = priv->ucode_boot.v_addr;
5814 u32 len = priv->ucode_boot.len;
5815 dma_addr_t pinst;
5816 dma_addr_t pdata;
5817 u32 inst_len;
5818 u32 data_len;
5819 int rc;
5820 int i;
5821 u32 done;
5822 u32 reg_offset;
5823
5824 IWL_DEBUG_INFO("Begin load bsm\n");
5825
5826 /* make sure bootstrap program is no larger than BSM's SRAM size */
5827 if (len > IWL_MAX_BSM_SIZE)
5828 return -EINVAL;
5829
5830 /* Tell bootstrap uCode where to find the "Initialize" uCode
Ben Cahill9fbab512007-11-29 11:09:47 +08005831 * in host DRAM ... host DRAM physical address bits 35:4 for 4965.
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005832 * NOTE: iwl4965_initialize_alive_start() will replace these values,
Zhu Yib481de92007-09-25 17:54:57 -07005833 * after the "initialize" uCode has run, to point to
5834 * runtime/protocol instructions and backup data cache. */
5835 pinst = priv->ucode_init.p_addr >> 4;
5836 pdata = priv->ucode_init_data.p_addr >> 4;
5837 inst_len = priv->ucode_init.len;
5838 data_len = priv->ucode_init_data.len;
5839
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005840 rc = iwl4965_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005841 if (rc)
5842 return rc;
5843
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005844 iwl4965_write_prph(priv, BSM_DRAM_INST_PTR_REG, pinst);
5845 iwl4965_write_prph(priv, BSM_DRAM_DATA_PTR_REG, pdata);
5846 iwl4965_write_prph(priv, BSM_DRAM_INST_BYTECOUNT_REG, inst_len);
5847 iwl4965_write_prph(priv, BSM_DRAM_DATA_BYTECOUNT_REG, data_len);
Zhu Yib481de92007-09-25 17:54:57 -07005848
5849 /* Fill BSM memory with bootstrap instructions */
5850 for (reg_offset = BSM_SRAM_LOWER_BOUND;
5851 reg_offset < BSM_SRAM_LOWER_BOUND + len;
5852 reg_offset += sizeof(u32), image++)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005853 _iwl4965_write_prph(priv, reg_offset,
Zhu Yib481de92007-09-25 17:54:57 -07005854 le32_to_cpu(*image));
5855
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005856 rc = iwl4965_verify_bsm(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005857 if (rc) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005858 iwl4965_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005859 return rc;
5860 }
5861
5862 /* Tell BSM to copy from BSM SRAM into instruction SRAM, when asked */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005863 iwl4965_write_prph(priv, BSM_WR_MEM_SRC_REG, 0x0);
5864 iwl4965_write_prph(priv, BSM_WR_MEM_DST_REG,
Zhu Yib481de92007-09-25 17:54:57 -07005865 RTC_INST_LOWER_BOUND);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005866 iwl4965_write_prph(priv, BSM_WR_DWCOUNT_REG, len / sizeof(u32));
Zhu Yib481de92007-09-25 17:54:57 -07005867
5868 /* Load bootstrap code into instruction SRAM now,
5869 * to prepare to load "initialize" uCode */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005870 iwl4965_write_prph(priv, BSM_WR_CTRL_REG,
Zhu Yib481de92007-09-25 17:54:57 -07005871 BSM_WR_CTRL_REG_BIT_START);
5872
5873 /* Wait for load of bootstrap uCode to finish */
5874 for (i = 0; i < 100; i++) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005875 done = iwl4965_read_prph(priv, BSM_WR_CTRL_REG);
Zhu Yib481de92007-09-25 17:54:57 -07005876 if (!(done & BSM_WR_CTRL_REG_BIT_START))
5877 break;
5878 udelay(10);
5879 }
5880 if (i < 100)
5881 IWL_DEBUG_INFO("BSM write complete, poll %d iterations\n", i);
5882 else {
5883 IWL_ERROR("BSM write did not complete!\n");
5884 return -EIO;
5885 }
5886
5887 /* Enable future boot loads whenever power management unit triggers it
5888 * (e.g. when powering back up after power-save shutdown) */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005889 iwl4965_write_prph(priv, BSM_WR_CTRL_REG,
Zhu Yib481de92007-09-25 17:54:57 -07005890 BSM_WR_CTRL_REG_BIT_START_EN);
5891
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005892 iwl4965_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005893
5894 return 0;
5895}
5896
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005897static void iwl4965_nic_start(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005898{
5899 /* Remove all resets to allow NIC to operate */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005900 iwl4965_write32(priv, CSR_RESET, 0);
Zhu Yib481de92007-09-25 17:54:57 -07005901}
5902
Tomas Winkler90e759d2007-11-29 11:09:41 +08005903
Zhu Yib481de92007-09-25 17:54:57 -07005904/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005905 * iwl4965_read_ucode - Read uCode images from disk file.
Zhu Yib481de92007-09-25 17:54:57 -07005906 *
5907 * Copy into buffers for card to fetch via bus-mastering
5908 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005909static int iwl4965_read_ucode(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005910{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005911 struct iwl4965_ucode *ucode;
Tomas Winkler90e759d2007-11-29 11:09:41 +08005912 int ret;
Zhu Yib481de92007-09-25 17:54:57 -07005913 const struct firmware *ucode_raw;
Tomas Winkler4bf775c2008-03-04 18:09:31 -08005914 const char *name = priv->cfg->fw_name;
Zhu Yib481de92007-09-25 17:54:57 -07005915 u8 *src;
5916 size_t len;
5917 u32 ver, inst_size, data_size, init_size, init_data_size, boot_size;
5918
5919 /* Ask kernel firmware_class module to get the boot firmware off disk.
5920 * request_firmware() is synchronous, file is in memory on return. */
Tomas Winkler90e759d2007-11-29 11:09:41 +08005921 ret = request_firmware(&ucode_raw, name, &priv->pci_dev->dev);
5922 if (ret < 0) {
5923 IWL_ERROR("%s firmware file req failed: Reason %d\n",
5924 name, ret);
Zhu Yib481de92007-09-25 17:54:57 -07005925 goto error;
5926 }
5927
5928 IWL_DEBUG_INFO("Got firmware '%s' file (%zd bytes) from disk\n",
5929 name, ucode_raw->size);
5930
5931 /* Make sure that we got at least our header! */
5932 if (ucode_raw->size < sizeof(*ucode)) {
5933 IWL_ERROR("File size way too small!\n");
Tomas Winkler90e759d2007-11-29 11:09:41 +08005934 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07005935 goto err_release;
5936 }
5937
5938 /* Data from ucode file: header followed by uCode images */
5939 ucode = (void *)ucode_raw->data;
5940
5941 ver = le32_to_cpu(ucode->ver);
5942 inst_size = le32_to_cpu(ucode->inst_size);
5943 data_size = le32_to_cpu(ucode->data_size);
5944 init_size = le32_to_cpu(ucode->init_size);
5945 init_data_size = le32_to_cpu(ucode->init_data_size);
5946 boot_size = le32_to_cpu(ucode->boot_size);
5947
5948 IWL_DEBUG_INFO("f/w package hdr ucode version = 0x%x\n", ver);
5949 IWL_DEBUG_INFO("f/w package hdr runtime inst size = %u\n",
5950 inst_size);
5951 IWL_DEBUG_INFO("f/w package hdr runtime data size = %u\n",
5952 data_size);
5953 IWL_DEBUG_INFO("f/w package hdr init inst size = %u\n",
5954 init_size);
5955 IWL_DEBUG_INFO("f/w package hdr init data size = %u\n",
5956 init_data_size);
5957 IWL_DEBUG_INFO("f/w package hdr boot inst size = %u\n",
5958 boot_size);
5959
5960 /* Verify size of file vs. image size info in file's header */
5961 if (ucode_raw->size < sizeof(*ucode) +
5962 inst_size + data_size + init_size +
5963 init_data_size + boot_size) {
5964
5965 IWL_DEBUG_INFO("uCode file size %d too small\n",
5966 (int)ucode_raw->size);
Tomas Winkler90e759d2007-11-29 11:09:41 +08005967 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07005968 goto err_release;
5969 }
5970
5971 /* Verify that uCode images will fit in card's SRAM */
5972 if (inst_size > IWL_MAX_INST_SIZE) {
Tomas Winkler90e759d2007-11-29 11:09:41 +08005973 IWL_DEBUG_INFO("uCode instr len %d too large to fit in\n",
5974 inst_size);
5975 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07005976 goto err_release;
5977 }
5978
5979 if (data_size > IWL_MAX_DATA_SIZE) {
Tomas Winkler90e759d2007-11-29 11:09:41 +08005980 IWL_DEBUG_INFO("uCode data len %d too large to fit in\n",
5981 data_size);
5982 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07005983 goto err_release;
5984 }
5985 if (init_size > IWL_MAX_INST_SIZE) {
5986 IWL_DEBUG_INFO
Tomas Winkler90e759d2007-11-29 11:09:41 +08005987 ("uCode init instr len %d too large to fit in\n",
5988 init_size);
5989 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07005990 goto err_release;
5991 }
5992 if (init_data_size > IWL_MAX_DATA_SIZE) {
5993 IWL_DEBUG_INFO
Tomas Winkler90e759d2007-11-29 11:09:41 +08005994 ("uCode init data len %d too large to fit in\n",
5995 init_data_size);
5996 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07005997 goto err_release;
5998 }
5999 if (boot_size > IWL_MAX_BSM_SIZE) {
6000 IWL_DEBUG_INFO
Tomas Winkler90e759d2007-11-29 11:09:41 +08006001 ("uCode boot instr len %d too large to fit in\n",
6002 boot_size);
6003 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07006004 goto err_release;
6005 }
6006
6007 /* Allocate ucode buffers for card's bus-master loading ... */
6008
6009 /* Runtime instructions and 2 copies of data:
6010 * 1) unmodified from disk
6011 * 2) backup cache for save/restore during power-downs */
6012 priv->ucode_code.len = inst_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08006013 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_code);
Zhu Yib481de92007-09-25 17:54:57 -07006014
6015 priv->ucode_data.len = data_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08006016 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_data);
Zhu Yib481de92007-09-25 17:54:57 -07006017
6018 priv->ucode_data_backup.len = data_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08006019 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_data_backup);
Zhu Yib481de92007-09-25 17:54:57 -07006020
6021 /* Initialization instructions and data */
Tomas Winkler90e759d2007-11-29 11:09:41 +08006022 if (init_size && init_data_size) {
6023 priv->ucode_init.len = init_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08006024 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_init);
Zhu Yib481de92007-09-25 17:54:57 -07006025
Tomas Winkler90e759d2007-11-29 11:09:41 +08006026 priv->ucode_init_data.len = init_data_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08006027 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_init_data);
Tomas Winkler90e759d2007-11-29 11:09:41 +08006028
6029 if (!priv->ucode_init.v_addr || !priv->ucode_init_data.v_addr)
6030 goto err_pci_alloc;
6031 }
Zhu Yib481de92007-09-25 17:54:57 -07006032
6033 /* Bootstrap (instructions only, no data) */
Tomas Winkler90e759d2007-11-29 11:09:41 +08006034 if (boot_size) {
6035 priv->ucode_boot.len = boot_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08006036 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_boot);
Zhu Yib481de92007-09-25 17:54:57 -07006037
Tomas Winkler90e759d2007-11-29 11:09:41 +08006038 if (!priv->ucode_boot.v_addr)
6039 goto err_pci_alloc;
6040 }
Zhu Yib481de92007-09-25 17:54:57 -07006041
6042 /* Copy images into buffers for card's bus-master reads ... */
6043
6044 /* Runtime instructions (first block of data in file) */
6045 src = &ucode->data[0];
6046 len = priv->ucode_code.len;
Tomas Winkler90e759d2007-11-29 11:09:41 +08006047 IWL_DEBUG_INFO("Copying (but not loading) uCode instr len %Zd\n", len);
Zhu Yib481de92007-09-25 17:54:57 -07006048 memcpy(priv->ucode_code.v_addr, src, len);
6049 IWL_DEBUG_INFO("uCode instr buf vaddr = 0x%p, paddr = 0x%08x\n",
6050 priv->ucode_code.v_addr, (u32)priv->ucode_code.p_addr);
6051
6052 /* Runtime data (2nd block)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006053 * NOTE: Copy into backup buffer will be done in iwl4965_up() */
Zhu Yib481de92007-09-25 17:54:57 -07006054 src = &ucode->data[inst_size];
6055 len = priv->ucode_data.len;
Tomas Winkler90e759d2007-11-29 11:09:41 +08006056 IWL_DEBUG_INFO("Copying (but not loading) uCode data len %Zd\n", len);
Zhu Yib481de92007-09-25 17:54:57 -07006057 memcpy(priv->ucode_data.v_addr, src, len);
6058 memcpy(priv->ucode_data_backup.v_addr, src, len);
6059
6060 /* Initialization instructions (3rd block) */
6061 if (init_size) {
6062 src = &ucode->data[inst_size + data_size];
6063 len = priv->ucode_init.len;
Tomas Winkler90e759d2007-11-29 11:09:41 +08006064 IWL_DEBUG_INFO("Copying (but not loading) init instr len %Zd\n",
6065 len);
Zhu Yib481de92007-09-25 17:54:57 -07006066 memcpy(priv->ucode_init.v_addr, src, len);
6067 }
6068
6069 /* Initialization data (4th block) */
6070 if (init_data_size) {
6071 src = &ucode->data[inst_size + data_size + init_size];
6072 len = priv->ucode_init_data.len;
Tomas Winkler90e759d2007-11-29 11:09:41 +08006073 IWL_DEBUG_INFO("Copying (but not loading) init data len %Zd\n",
6074 len);
Zhu Yib481de92007-09-25 17:54:57 -07006075 memcpy(priv->ucode_init_data.v_addr, src, len);
6076 }
6077
6078 /* Bootstrap instructions (5th block) */
6079 src = &ucode->data[inst_size + data_size + init_size + init_data_size];
6080 len = priv->ucode_boot.len;
Tomas Winkler90e759d2007-11-29 11:09:41 +08006081 IWL_DEBUG_INFO("Copying (but not loading) boot instr len %Zd\n", len);
Zhu Yib481de92007-09-25 17:54:57 -07006082 memcpy(priv->ucode_boot.v_addr, src, len);
6083
6084 /* We have our copies now, allow OS release its copies */
6085 release_firmware(ucode_raw);
6086 return 0;
6087
6088 err_pci_alloc:
6089 IWL_ERROR("failed to allocate pci memory\n");
Tomas Winkler90e759d2007-11-29 11:09:41 +08006090 ret = -ENOMEM;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006091 iwl4965_dealloc_ucode_pci(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006092
6093 err_release:
6094 release_firmware(ucode_raw);
6095
6096 error:
Tomas Winkler90e759d2007-11-29 11:09:41 +08006097 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07006098}
6099
6100
6101/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006102 * iwl4965_set_ucode_ptrs - Set uCode address location
Zhu Yib481de92007-09-25 17:54:57 -07006103 *
6104 * Tell initialization uCode where to find runtime uCode.
6105 *
6106 * BSM registers initially contain pointers to initialization uCode.
6107 * We need to replace them to load runtime uCode inst and data,
6108 * and to save runtime data when powering down.
6109 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006110static int iwl4965_set_ucode_ptrs(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07006111{
6112 dma_addr_t pinst;
6113 dma_addr_t pdata;
6114 int rc = 0;
6115 unsigned long flags;
6116
6117 /* bits 35:4 for 4965 */
6118 pinst = priv->ucode_code.p_addr >> 4;
6119 pdata = priv->ucode_data_backup.p_addr >> 4;
6120
6121 spin_lock_irqsave(&priv->lock, flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006122 rc = iwl4965_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006123 if (rc) {
6124 spin_unlock_irqrestore(&priv->lock, flags);
6125 return rc;
6126 }
6127
6128 /* Tell bootstrap uCode where to find image to load */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006129 iwl4965_write_prph(priv, BSM_DRAM_INST_PTR_REG, pinst);
6130 iwl4965_write_prph(priv, BSM_DRAM_DATA_PTR_REG, pdata);
6131 iwl4965_write_prph(priv, BSM_DRAM_DATA_BYTECOUNT_REG,
Zhu Yib481de92007-09-25 17:54:57 -07006132 priv->ucode_data.len);
6133
6134 /* Inst bytecount must be last to set up, bit 31 signals uCode
6135 * that all new ptr/size info is in place */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006136 iwl4965_write_prph(priv, BSM_DRAM_INST_BYTECOUNT_REG,
Zhu Yib481de92007-09-25 17:54:57 -07006137 priv->ucode_code.len | BSM_DRAM_INST_LOAD);
6138
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006139 iwl4965_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006140
6141 spin_unlock_irqrestore(&priv->lock, flags);
6142
6143 IWL_DEBUG_INFO("Runtime uCode pointers are set.\n");
6144
6145 return rc;
6146}
6147
6148/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006149 * iwl4965_init_alive_start - Called after REPLY_ALIVE notification received
Zhu Yib481de92007-09-25 17:54:57 -07006150 *
6151 * Called after REPLY_ALIVE notification received from "initialize" uCode.
6152 *
6153 * The 4965 "initialize" ALIVE reply contains calibration data for:
6154 * Voltage, temperature, and MIMO tx gain correction, now stored in priv
6155 * (3945 does not contain this data).
6156 *
6157 * Tell "initialize" uCode to go ahead and load the runtime uCode.
6158*/
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006159static void iwl4965_init_alive_start(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07006160{
6161 /* Check alive response for "valid" sign from uCode */
6162 if (priv->card_alive_init.is_valid != UCODE_VALID_OK) {
6163 /* We had an error bringing up the hardware, so take it
6164 * all the way back down so we can try again */
6165 IWL_DEBUG_INFO("Initialize Alive failed.\n");
6166 goto restart;
6167 }
6168
6169 /* Bootstrap uCode has loaded initialize uCode ... verify inst image.
6170 * This is a paranoid check, because we would not have gotten the
6171 * "initialize" alive if code weren't properly loaded. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006172 if (iwl4965_verify_ucode(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07006173 /* Runtime instruction load was bad;
6174 * take it all the way back down so we can try again */
6175 IWL_DEBUG_INFO("Bad \"initialize\" uCode load.\n");
6176 goto restart;
6177 }
6178
6179 /* Calculate temperature */
6180 priv->temperature = iwl4965_get_temperature(priv);
6181
6182 /* Send pointers to protocol/runtime uCode image ... init code will
6183 * load and launch runtime uCode, which will send us another "Alive"
6184 * notification. */
6185 IWL_DEBUG_INFO("Initialization Alive received.\n");
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006186 if (iwl4965_set_ucode_ptrs(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07006187 /* Runtime instruction load won't happen;
6188 * take it all the way back down so we can try again */
6189 IWL_DEBUG_INFO("Couldn't set up uCode pointers.\n");
6190 goto restart;
6191 }
6192 return;
6193
6194 restart:
6195 queue_work(priv->workqueue, &priv->restart);
6196}
6197
6198
6199/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006200 * iwl4965_alive_start - called after REPLY_ALIVE notification received
Zhu Yib481de92007-09-25 17:54:57 -07006201 * from protocol/runtime uCode (initialization uCode's
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006202 * Alive gets handled by iwl4965_init_alive_start()).
Zhu Yib481de92007-09-25 17:54:57 -07006203 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006204static void iwl4965_alive_start(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07006205{
6206 int rc = 0;
6207
6208 IWL_DEBUG_INFO("Runtime Alive received.\n");
6209
6210 if (priv->card_alive.is_valid != UCODE_VALID_OK) {
6211 /* We had an error bringing up the hardware, so take it
6212 * all the way back down so we can try again */
6213 IWL_DEBUG_INFO("Alive failed.\n");
6214 goto restart;
6215 }
6216
6217 /* Initialize uCode has loaded Runtime uCode ... verify inst image.
6218 * This is a paranoid check, because we would not have gotten the
6219 * "runtime" alive if code weren't properly loaded. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006220 if (iwl4965_verify_ucode(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07006221 /* Runtime instruction load was bad;
6222 * take it all the way back down so we can try again */
6223 IWL_DEBUG_INFO("Bad runtime uCode load.\n");
6224 goto restart;
6225 }
6226
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006227 iwl4965_clear_stations_table(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006228
6229 rc = iwl4965_alive_notify(priv);
6230 if (rc) {
6231 IWL_WARNING("Could not complete ALIVE transition [ntf]: %d\n",
6232 rc);
6233 goto restart;
6234 }
6235
Ben Cahill9fbab512007-11-29 11:09:47 +08006236 /* After the ALIVE response, we can send host commands to 4965 uCode */
Zhu Yib481de92007-09-25 17:54:57 -07006237 set_bit(STATUS_ALIVE, &priv->status);
6238
6239 /* Clear out the uCode error bit if it is set */
6240 clear_bit(STATUS_FW_ERROR, &priv->status);
6241
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006242 if (iwl4965_is_rfkill(priv))
Zhu Yib481de92007-09-25 17:54:57 -07006243 return;
6244
Zhu Yi5a66926a2008-01-14 17:46:18 -08006245 ieee80211_start_queues(priv->hw);
Zhu Yib481de92007-09-25 17:54:57 -07006246
6247 priv->active_rate = priv->rates_mask;
6248 priv->active_rate_basic = priv->rates_mask & IWL_BASIC_RATES_MASK;
6249
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006250 iwl4965_send_power_mode(priv, IWL_POWER_LEVEL(priv->power_mode));
Zhu Yib481de92007-09-25 17:54:57 -07006251
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006252 if (iwl4965_is_associated(priv)) {
6253 struct iwl4965_rxon_cmd *active_rxon =
6254 (struct iwl4965_rxon_cmd *)(&priv->active_rxon);
Zhu Yib481de92007-09-25 17:54:57 -07006255
6256 memcpy(&priv->staging_rxon, &priv->active_rxon,
6257 sizeof(priv->staging_rxon));
6258 active_rxon->filter_flags &= ~RXON_FILTER_ASSOC_MSK;
6259 } else {
6260 /* Initialize our rx_config data */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006261 iwl4965_connection_init_rx_config(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006262 memcpy(priv->staging_rxon.node_addr, priv->mac_addr, ETH_ALEN);
6263 }
6264
Ben Cahill9fbab512007-11-29 11:09:47 +08006265 /* Configure Bluetooth device coexistence support */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006266 iwl4965_send_bt_config(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006267
6268 /* Configure the adapter for unassociated operation */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006269 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006270
6271 /* At this point, the NIC is initialized and operational */
6272 priv->notif_missed_beacons = 0;
6273 set_bit(STATUS_READY, &priv->status);
6274
6275 iwl4965_rf_kill_ct_config(priv);
Zhu Yi5a66926a2008-01-14 17:46:18 -08006276
Zhu Yib481de92007-09-25 17:54:57 -07006277 IWL_DEBUG_INFO("ALIVE processing complete.\n");
Zhu Yi5a66926a2008-01-14 17:46:18 -08006278 wake_up_interruptible(&priv->wait_command_queue);
Zhu Yib481de92007-09-25 17:54:57 -07006279
6280 if (priv->error_recovering)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006281 iwl4965_error_recovery(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006282
6283 return;
6284
6285 restart:
6286 queue_work(priv->workqueue, &priv->restart);
6287}
6288
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006289static void iwl4965_cancel_deferred_work(struct iwl4965_priv *priv);
Zhu Yib481de92007-09-25 17:54:57 -07006290
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006291static void __iwl4965_down(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07006292{
6293 unsigned long flags;
6294 int exit_pending = test_bit(STATUS_EXIT_PENDING, &priv->status);
6295 struct ieee80211_conf *conf = NULL;
6296
6297 IWL_DEBUG_INFO(DRV_NAME " is going down\n");
6298
6299 conf = ieee80211_get_hw_conf(priv->hw);
6300
6301 if (!exit_pending)
6302 set_bit(STATUS_EXIT_PENDING, &priv->status);
6303
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006304 iwl4965_clear_stations_table(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006305
6306 /* Unblock any waiting calls */
6307 wake_up_interruptible_all(&priv->wait_command_queue);
6308
Zhu Yib481de92007-09-25 17:54:57 -07006309 /* Wipe out the EXIT_PENDING status bit if we are not actually
6310 * exiting the module */
6311 if (!exit_pending)
6312 clear_bit(STATUS_EXIT_PENDING, &priv->status);
6313
6314 /* stop and reset the on-board processor */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006315 iwl4965_write32(priv, CSR_RESET, CSR_RESET_REG_FLAG_NEVO_RESET);
Zhu Yib481de92007-09-25 17:54:57 -07006316
6317 /* tell the device to stop sending interrupts */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006318 iwl4965_disable_interrupts(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006319
6320 if (priv->mac80211_registered)
6321 ieee80211_stop_queues(priv->hw);
6322
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006323 /* If we have not previously called iwl4965_init() then
Zhu Yib481de92007-09-25 17:54:57 -07006324 * clear all bits but the RF Kill and SUSPEND bits and return */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006325 if (!iwl4965_is_init(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07006326 priv->status = test_bit(STATUS_RF_KILL_HW, &priv->status) <<
6327 STATUS_RF_KILL_HW |
6328 test_bit(STATUS_RF_KILL_SW, &priv->status) <<
6329 STATUS_RF_KILL_SW |
Reinette Chatre97888642008-02-06 11:20:38 -08006330 test_bit(STATUS_GEO_CONFIGURED, &priv->status) <<
6331 STATUS_GEO_CONFIGURED |
Zhu Yib481de92007-09-25 17:54:57 -07006332 test_bit(STATUS_IN_SUSPEND, &priv->status) <<
6333 STATUS_IN_SUSPEND;
6334 goto exit;
6335 }
6336
6337 /* ...otherwise clear out all the status bits but the RF Kill and
6338 * SUSPEND bits and continue taking the NIC down. */
6339 priv->status &= test_bit(STATUS_RF_KILL_HW, &priv->status) <<
6340 STATUS_RF_KILL_HW |
6341 test_bit(STATUS_RF_KILL_SW, &priv->status) <<
6342 STATUS_RF_KILL_SW |
Reinette Chatre97888642008-02-06 11:20:38 -08006343 test_bit(STATUS_GEO_CONFIGURED, &priv->status) <<
6344 STATUS_GEO_CONFIGURED |
Zhu Yib481de92007-09-25 17:54:57 -07006345 test_bit(STATUS_IN_SUSPEND, &priv->status) <<
6346 STATUS_IN_SUSPEND |
6347 test_bit(STATUS_FW_ERROR, &priv->status) <<
6348 STATUS_FW_ERROR;
6349
6350 spin_lock_irqsave(&priv->lock, flags);
Ben Cahill9fbab512007-11-29 11:09:47 +08006351 iwl4965_clear_bit(priv, CSR_GP_CNTRL,
6352 CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
Zhu Yib481de92007-09-25 17:54:57 -07006353 spin_unlock_irqrestore(&priv->lock, flags);
6354
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006355 iwl4965_hw_txq_ctx_stop(priv);
6356 iwl4965_hw_rxq_stop(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006357
6358 spin_lock_irqsave(&priv->lock, flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006359 if (!iwl4965_grab_nic_access(priv)) {
6360 iwl4965_write_prph(priv, APMG_CLK_DIS_REG,
Zhu Yib481de92007-09-25 17:54:57 -07006361 APMG_CLK_VAL_DMA_CLK_RQT);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006362 iwl4965_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006363 }
6364 spin_unlock_irqrestore(&priv->lock, flags);
6365
6366 udelay(5);
6367
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006368 iwl4965_hw_nic_stop_master(priv);
6369 iwl4965_set_bit(priv, CSR_RESET, CSR_RESET_REG_FLAG_SW_RESET);
6370 iwl4965_hw_nic_reset(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006371
6372 exit:
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006373 memset(&priv->card_alive, 0, sizeof(struct iwl4965_alive_resp));
Zhu Yib481de92007-09-25 17:54:57 -07006374
6375 if (priv->ibss_beacon)
6376 dev_kfree_skb(priv->ibss_beacon);
6377 priv->ibss_beacon = NULL;
6378
6379 /* clear out any free frames */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006380 iwl4965_clear_free_frames(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006381}
6382
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006383static void iwl4965_down(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07006384{
6385 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006386 __iwl4965_down(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006387 mutex_unlock(&priv->mutex);
Zhu Yib24d22b2007-12-19 13:59:52 +08006388
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006389 iwl4965_cancel_deferred_work(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006390}
6391
6392#define MAX_HW_RESTARTS 5
6393
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006394static int __iwl4965_up(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07006395{
6396 int rc, i;
Zhu Yib481de92007-09-25 17:54:57 -07006397
6398 if (test_bit(STATUS_EXIT_PENDING, &priv->status)) {
6399 IWL_WARNING("Exit pending; will not bring the NIC up\n");
6400 return -EIO;
6401 }
6402
6403 if (test_bit(STATUS_RF_KILL_SW, &priv->status)) {
6404 IWL_WARNING("Radio disabled by SW RF kill (module "
6405 "parameter)\n");
Zhu Yie655b9f2008-01-24 02:19:38 -08006406 return -ENODEV;
6407 }
6408
Reinette Chatree903fbd2008-01-30 22:05:15 -08006409 if (!priv->ucode_data_backup.v_addr || !priv->ucode_data.v_addr) {
6410 IWL_ERROR("ucode not available for device bringup\n");
6411 return -EIO;
6412 }
6413
Zhu Yie655b9f2008-01-24 02:19:38 -08006414 /* If platform's RF_KILL switch is NOT set to KILL */
6415 if (iwl4965_read32(priv, CSR_GP_CNTRL) &
6416 CSR_GP_CNTRL_REG_FLAG_HW_RF_KILL_SW)
6417 clear_bit(STATUS_RF_KILL_HW, &priv->status);
6418 else {
6419 set_bit(STATUS_RF_KILL_HW, &priv->status);
6420 if (!test_bit(STATUS_IN_SUSPEND, &priv->status)) {
6421 IWL_WARNING("Radio disabled by HW RF Kill switch\n");
6422 return -ENODEV;
6423 }
Zhu Yib481de92007-09-25 17:54:57 -07006424 }
6425
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006426 iwl4965_write32(priv, CSR_INT, 0xFFFFFFFF);
Zhu Yib481de92007-09-25 17:54:57 -07006427
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006428 rc = iwl4965_hw_nic_init(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006429 if (rc) {
6430 IWL_ERROR("Unable to int nic\n");
6431 return rc;
6432 }
6433
6434 /* make sure rfkill handshake bits are cleared */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006435 iwl4965_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
6436 iwl4965_write32(priv, CSR_UCODE_DRV_GP1_CLR,
Zhu Yib481de92007-09-25 17:54:57 -07006437 CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
6438
6439 /* clear (again), then enable host interrupts */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006440 iwl4965_write32(priv, CSR_INT, 0xFFFFFFFF);
6441 iwl4965_enable_interrupts(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006442
6443 /* really make sure rfkill handshake bits are cleared */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006444 iwl4965_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
6445 iwl4965_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
Zhu Yib481de92007-09-25 17:54:57 -07006446
6447 /* Copy original ucode data image from disk into backup cache.
6448 * This will be used to initialize the on-board processor's
6449 * data SRAM for a clean start when the runtime program first loads. */
6450 memcpy(priv->ucode_data_backup.v_addr, priv->ucode_data.v_addr,
Zhu Yi5a66926a2008-01-14 17:46:18 -08006451 priv->ucode_data.len);
Zhu Yib481de92007-09-25 17:54:57 -07006452
Zhu Yie655b9f2008-01-24 02:19:38 -08006453 /* We return success when we resume from suspend and rf_kill is on. */
6454 if (test_bit(STATUS_RF_KILL_HW, &priv->status))
Zhu Yib481de92007-09-25 17:54:57 -07006455 return 0;
Zhu Yib481de92007-09-25 17:54:57 -07006456
6457 for (i = 0; i < MAX_HW_RESTARTS; i++) {
6458
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006459 iwl4965_clear_stations_table(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006460
6461 /* load bootstrap state machine,
6462 * load bootstrap program into processor's memory,
6463 * prepare to load the "initialize" uCode */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006464 rc = iwl4965_load_bsm(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006465
6466 if (rc) {
6467 IWL_ERROR("Unable to set up bootstrap uCode: %d\n", rc);
6468 continue;
6469 }
6470
6471 /* start card; "initialize" will load runtime ucode */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006472 iwl4965_nic_start(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006473
Zhu Yib481de92007-09-25 17:54:57 -07006474 IWL_DEBUG_INFO(DRV_NAME " is coming up\n");
6475
6476 return 0;
6477 }
6478
6479 set_bit(STATUS_EXIT_PENDING, &priv->status);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006480 __iwl4965_down(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006481
6482 /* tried to restart and config the device for as long as our
6483 * patience could withstand */
6484 IWL_ERROR("Unable to initialize device after %d attempts.\n", i);
6485 return -EIO;
6486}
6487
6488
6489/*****************************************************************************
6490 *
6491 * Workqueue callbacks
6492 *
6493 *****************************************************************************/
6494
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006495static void iwl4965_bg_init_alive_start(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07006496{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006497 struct iwl4965_priv *priv =
6498 container_of(data, struct iwl4965_priv, init_alive_start.work);
Zhu Yib481de92007-09-25 17:54:57 -07006499
6500 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
6501 return;
6502
6503 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006504 iwl4965_init_alive_start(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006505 mutex_unlock(&priv->mutex);
6506}
6507
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006508static void iwl4965_bg_alive_start(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07006509{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006510 struct iwl4965_priv *priv =
6511 container_of(data, struct iwl4965_priv, alive_start.work);
Zhu Yib481de92007-09-25 17:54:57 -07006512
6513 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
6514 return;
6515
6516 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006517 iwl4965_alive_start(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006518 mutex_unlock(&priv->mutex);
6519}
6520
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006521static void iwl4965_bg_rf_kill(struct work_struct *work)
Zhu Yib481de92007-09-25 17:54:57 -07006522{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006523 struct iwl4965_priv *priv = container_of(work, struct iwl4965_priv, rf_kill);
Zhu Yib481de92007-09-25 17:54:57 -07006524
6525 wake_up_interruptible(&priv->wait_command_queue);
6526
6527 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
6528 return;
6529
6530 mutex_lock(&priv->mutex);
6531
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006532 if (!iwl4965_is_rfkill(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07006533 IWL_DEBUG(IWL_DL_INFO | IWL_DL_RF_KILL,
6534 "HW and/or SW RF Kill no longer active, restarting "
6535 "device\n");
6536 if (!test_bit(STATUS_EXIT_PENDING, &priv->status))
6537 queue_work(priv->workqueue, &priv->restart);
6538 } else {
6539
6540 if (!test_bit(STATUS_RF_KILL_HW, &priv->status))
6541 IWL_DEBUG_RF_KILL("Can not turn radio back on - "
6542 "disabled by SW switch\n");
6543 else
6544 IWL_WARNING("Radio Frequency Kill Switch is On:\n"
6545 "Kill switch must be turned off for "
6546 "wireless networking to work.\n");
6547 }
6548 mutex_unlock(&priv->mutex);
6549}
6550
6551#define IWL_SCAN_CHECK_WATCHDOG (7 * HZ)
6552
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006553static void iwl4965_bg_scan_check(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07006554{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006555 struct iwl4965_priv *priv =
6556 container_of(data, struct iwl4965_priv, scan_check.work);
Zhu Yib481de92007-09-25 17:54:57 -07006557
6558 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
6559 return;
6560
6561 mutex_lock(&priv->mutex);
6562 if (test_bit(STATUS_SCANNING, &priv->status) ||
6563 test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
6564 IWL_DEBUG(IWL_DL_INFO | IWL_DL_SCAN,
6565 "Scan completion watchdog resetting adapter (%dms)\n",
6566 jiffies_to_msecs(IWL_SCAN_CHECK_WATCHDOG));
mabbas052c4b92007-10-25 17:15:43 +08006567
Zhu Yib481de92007-09-25 17:54:57 -07006568 if (!test_bit(STATUS_EXIT_PENDING, &priv->status))
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006569 iwl4965_send_scan_abort(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006570 }
6571 mutex_unlock(&priv->mutex);
6572}
6573
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006574static void iwl4965_bg_request_scan(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07006575{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006576 struct iwl4965_priv *priv =
6577 container_of(data, struct iwl4965_priv, request_scan);
6578 struct iwl4965_host_cmd cmd = {
Zhu Yib481de92007-09-25 17:54:57 -07006579 .id = REPLY_SCAN_CMD,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006580 .len = sizeof(struct iwl4965_scan_cmd),
Zhu Yib481de92007-09-25 17:54:57 -07006581 .meta.flags = CMD_SIZE_HUGE,
6582 };
6583 int rc = 0;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006584 struct iwl4965_scan_cmd *scan;
Zhu Yib481de92007-09-25 17:54:57 -07006585 struct ieee80211_conf *conf = NULL;
Tomas Winkler78330fd2008-02-06 02:37:18 +02006586 u16 cmd_len;
Johannes Berg8318d782008-01-24 19:38:38 +01006587 enum ieee80211_band band;
Tomas Winkler78330fd2008-02-06 02:37:18 +02006588 u8 direct_mask;
Zhu Yib481de92007-09-25 17:54:57 -07006589
6590 conf = ieee80211_get_hw_conf(priv->hw);
6591
6592 mutex_lock(&priv->mutex);
6593
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006594 if (!iwl4965_is_ready(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07006595 IWL_WARNING("request scan called when driver not ready.\n");
6596 goto done;
6597 }
6598
6599 /* Make sure the scan wasn't cancelled before this queued work
6600 * was given the chance to run... */
6601 if (!test_bit(STATUS_SCANNING, &priv->status))
6602 goto done;
6603
6604 /* This should never be called or scheduled if there is currently
6605 * a scan active in the hardware. */
6606 if (test_bit(STATUS_SCAN_HW, &priv->status)) {
6607 IWL_DEBUG_INFO("Multiple concurrent scan requests in parallel. "
6608 "Ignoring second request.\n");
6609 rc = -EIO;
6610 goto done;
6611 }
6612
6613 if (test_bit(STATUS_EXIT_PENDING, &priv->status)) {
6614 IWL_DEBUG_SCAN("Aborting scan due to device shutdown\n");
6615 goto done;
6616 }
6617
6618 if (test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
6619 IWL_DEBUG_HC("Scan request while abort pending. Queuing.\n");
6620 goto done;
6621 }
6622
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006623 if (iwl4965_is_rfkill(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07006624 IWL_DEBUG_HC("Aborting scan due to RF Kill activation\n");
6625 goto done;
6626 }
6627
6628 if (!test_bit(STATUS_READY, &priv->status)) {
6629 IWL_DEBUG_HC("Scan request while uninitialized. Queuing.\n");
6630 goto done;
6631 }
6632
6633 if (!priv->scan_bands) {
6634 IWL_DEBUG_HC("Aborting scan due to no requested bands\n");
6635 goto done;
6636 }
6637
6638 if (!priv->scan) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006639 priv->scan = kmalloc(sizeof(struct iwl4965_scan_cmd) +
Zhu Yib481de92007-09-25 17:54:57 -07006640 IWL_MAX_SCAN_SIZE, GFP_KERNEL);
6641 if (!priv->scan) {
6642 rc = -ENOMEM;
6643 goto done;
6644 }
6645 }
6646 scan = priv->scan;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006647 memset(scan, 0, sizeof(struct iwl4965_scan_cmd) + IWL_MAX_SCAN_SIZE);
Zhu Yib481de92007-09-25 17:54:57 -07006648
6649 scan->quiet_plcp_th = IWL_PLCP_QUIET_THRESH;
6650 scan->quiet_time = IWL_ACTIVE_QUIET_TIME;
6651
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006652 if (iwl4965_is_associated(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07006653 u16 interval = 0;
6654 u32 extra;
6655 u32 suspend_time = 100;
6656 u32 scan_suspend_time = 100;
6657 unsigned long flags;
6658
6659 IWL_DEBUG_INFO("Scanning while associated...\n");
6660
6661 spin_lock_irqsave(&priv->lock, flags);
6662 interval = priv->beacon_int;
6663 spin_unlock_irqrestore(&priv->lock, flags);
6664
6665 scan->suspend_time = 0;
mabbas052c4b92007-10-25 17:15:43 +08006666 scan->max_out_time = cpu_to_le32(200 * 1024);
Zhu Yib481de92007-09-25 17:54:57 -07006667 if (!interval)
6668 interval = suspend_time;
6669
6670 extra = (suspend_time / interval) << 22;
6671 scan_suspend_time = (extra |
6672 ((suspend_time % interval) * 1024));
6673 scan->suspend_time = cpu_to_le32(scan_suspend_time);
6674 IWL_DEBUG_SCAN("suspend_time 0x%X beacon interval %d\n",
6675 scan_suspend_time, interval);
6676 }
6677
6678 /* We should add the ability for user to lock to PASSIVE ONLY */
6679 if (priv->one_direct_scan) {
6680 IWL_DEBUG_SCAN
6681 ("Kicking off one direct scan for '%s'\n",
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006682 iwl4965_escape_essid(priv->direct_ssid,
Zhu Yib481de92007-09-25 17:54:57 -07006683 priv->direct_ssid_len));
6684 scan->direct_scan[0].id = WLAN_EID_SSID;
6685 scan->direct_scan[0].len = priv->direct_ssid_len;
6686 memcpy(scan->direct_scan[0].ssid,
6687 priv->direct_ssid, priv->direct_ssid_len);
6688 direct_mask = 1;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006689 } else if (!iwl4965_is_associated(priv) && priv->essid_len) {
Zhu Yib481de92007-09-25 17:54:57 -07006690 scan->direct_scan[0].id = WLAN_EID_SSID;
6691 scan->direct_scan[0].len = priv->essid_len;
6692 memcpy(scan->direct_scan[0].ssid, priv->essid, priv->essid_len);
6693 direct_mask = 1;
6694 } else
6695 direct_mask = 0;
6696
Zhu Yib481de92007-09-25 17:54:57 -07006697 scan->tx_cmd.tx_flags = TX_CMD_FLG_SEQ_CTL_MSK;
6698 scan->tx_cmd.sta_id = priv->hw_setting.bcast_sta_id;
6699 scan->tx_cmd.stop_time.life_time = TX_CMD_LIFE_TIME_INFINITE;
6700
Zhu Yib481de92007-09-25 17:54:57 -07006701
6702 switch (priv->scan_bands) {
6703 case 2:
6704 scan->flags = RXON_FLG_BAND_24G_MSK | RXON_FLG_AUTO_DETECT_MSK;
6705 scan->tx_cmd.rate_n_flags =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006706 iwl4965_hw_set_rate_n_flags(IWL_RATE_1M_PLCP,
Zhu Yib481de92007-09-25 17:54:57 -07006707 RATE_MCS_ANT_B_MSK|RATE_MCS_CCK_MSK);
6708
6709 scan->good_CRC_th = 0;
Johannes Berg8318d782008-01-24 19:38:38 +01006710 band = IEEE80211_BAND_2GHZ;
Zhu Yib481de92007-09-25 17:54:57 -07006711 break;
6712
6713 case 1:
6714 scan->tx_cmd.rate_n_flags =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006715 iwl4965_hw_set_rate_n_flags(IWL_RATE_6M_PLCP,
Zhu Yib481de92007-09-25 17:54:57 -07006716 RATE_MCS_ANT_B_MSK);
6717 scan->good_CRC_th = IWL_GOOD_CRC_TH;
Johannes Berg8318d782008-01-24 19:38:38 +01006718 band = IEEE80211_BAND_5GHZ;
Zhu Yib481de92007-09-25 17:54:57 -07006719 break;
6720
6721 default:
6722 IWL_WARNING("Invalid scan band count\n");
6723 goto done;
6724 }
6725
Tomas Winkler78330fd2008-02-06 02:37:18 +02006726 /* We don't build a direct scan probe request; the uCode will do
6727 * that based on the direct_mask added to each channel entry */
6728 cmd_len = iwl4965_fill_probe_req(priv, band,
6729 (struct ieee80211_mgmt *)scan->data,
6730 IWL_MAX_SCAN_SIZE - sizeof(*scan), 0);
6731
6732 scan->tx_cmd.len = cpu_to_le16(cmd_len);
Zhu Yib481de92007-09-25 17:54:57 -07006733 /* select Rx chains */
6734
6735 /* Force use of chains B and C (0x6) for scan Rx.
6736 * Avoid A (0x1) because of its off-channel reception on A-band.
6737 * MIMO is not used here, but value is required to make uCode happy. */
6738 scan->rx_chain = RXON_RX_CHAIN_DRIVER_FORCE_MSK |
6739 cpu_to_le16((0x7 << RXON_RX_CHAIN_VALID_POS) |
6740 (0x6 << RXON_RX_CHAIN_FORCE_SEL_POS) |
6741 (0x7 << RXON_RX_CHAIN_FORCE_MIMO_SEL_POS));
6742
6743 if (priv->iw_mode == IEEE80211_IF_TYPE_MNTR)
6744 scan->filter_flags = RXON_FILTER_PROMISC_MSK;
6745
6746 if (direct_mask)
6747 IWL_DEBUG_SCAN
6748 ("Initiating direct scan for %s.\n",
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006749 iwl4965_escape_essid(priv->essid, priv->essid_len));
Zhu Yib481de92007-09-25 17:54:57 -07006750 else
6751 IWL_DEBUG_SCAN("Initiating indirect scan.\n");
6752
6753 scan->channel_count =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006754 iwl4965_get_channels_for_scan(
Johannes Berg8318d782008-01-24 19:38:38 +01006755 priv, band, 1, /* active */
Zhu Yib481de92007-09-25 17:54:57 -07006756 direct_mask,
6757 (void *)&scan->data[le16_to_cpu(scan->tx_cmd.len)]);
6758
6759 cmd.len += le16_to_cpu(scan->tx_cmd.len) +
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006760 scan->channel_count * sizeof(struct iwl4965_scan_channel);
Zhu Yib481de92007-09-25 17:54:57 -07006761 cmd.data = scan;
6762 scan->len = cpu_to_le16(cmd.len);
6763
6764 set_bit(STATUS_SCAN_HW, &priv->status);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006765 rc = iwl4965_send_cmd_sync(priv, &cmd);
Zhu Yib481de92007-09-25 17:54:57 -07006766 if (rc)
6767 goto done;
6768
6769 queue_delayed_work(priv->workqueue, &priv->scan_check,
6770 IWL_SCAN_CHECK_WATCHDOG);
6771
6772 mutex_unlock(&priv->mutex);
6773 return;
6774
6775 done:
Ian Schram01ebd062007-10-25 17:15:22 +08006776 /* inform mac80211 scan aborted */
Zhu Yib481de92007-09-25 17:54:57 -07006777 queue_work(priv->workqueue, &priv->scan_completed);
6778 mutex_unlock(&priv->mutex);
6779}
6780
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006781static void iwl4965_bg_up(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07006782{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006783 struct iwl4965_priv *priv = container_of(data, struct iwl4965_priv, up);
Zhu Yib481de92007-09-25 17:54:57 -07006784
6785 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
6786 return;
6787
6788 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006789 __iwl4965_up(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006790 mutex_unlock(&priv->mutex);
6791}
6792
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006793static void iwl4965_bg_restart(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07006794{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006795 struct iwl4965_priv *priv = container_of(data, struct iwl4965_priv, restart);
Zhu Yib481de92007-09-25 17:54:57 -07006796
6797 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
6798 return;
6799
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006800 iwl4965_down(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006801 queue_work(priv->workqueue, &priv->up);
6802}
6803
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006804static void iwl4965_bg_rx_replenish(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07006805{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006806 struct iwl4965_priv *priv =
6807 container_of(data, struct iwl4965_priv, rx_replenish);
Zhu Yib481de92007-09-25 17:54:57 -07006808
6809 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
6810 return;
6811
6812 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006813 iwl4965_rx_replenish(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006814 mutex_unlock(&priv->mutex);
6815}
6816
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08006817#define IWL_DELAY_NEXT_SCAN (HZ*2)
6818
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006819static void iwl4965_bg_post_associate(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07006820{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006821 struct iwl4965_priv *priv = container_of(data, struct iwl4965_priv,
Zhu Yib481de92007-09-25 17:54:57 -07006822 post_associate.work);
6823
6824 int rc = 0;
6825 struct ieee80211_conf *conf = NULL;
Joe Perches0795af52007-10-03 17:59:30 -07006826 DECLARE_MAC_BUF(mac);
Zhu Yib481de92007-09-25 17:54:57 -07006827
6828 if (priv->iw_mode == IEEE80211_IF_TYPE_AP) {
6829 IWL_ERROR("%s Should not be called in AP mode\n", __FUNCTION__);
6830 return;
6831 }
6832
Joe Perches0795af52007-10-03 17:59:30 -07006833 IWL_DEBUG_ASSOC("Associated as %d to: %s\n",
6834 priv->assoc_id,
6835 print_mac(mac, priv->active_rxon.bssid_addr));
Zhu Yib481de92007-09-25 17:54:57 -07006836
6837
6838 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
6839 return;
6840
6841 mutex_lock(&priv->mutex);
6842
Johannes Berg32bfd352007-12-19 01:31:26 +01006843 if (!priv->vif || !priv->is_open) {
Mohamed Abbas948c1712007-10-25 17:15:45 +08006844 mutex_unlock(&priv->mutex);
6845 return;
6846 }
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006847 iwl4965_scan_cancel_timeout(priv, 200);
mabbas052c4b92007-10-25 17:15:43 +08006848
Zhu Yib481de92007-09-25 17:54:57 -07006849 conf = ieee80211_get_hw_conf(priv->hw);
6850
6851 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006852 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006853
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006854 memset(&priv->rxon_timing, 0, sizeof(struct iwl4965_rxon_time_cmd));
6855 iwl4965_setup_rxon_timing(priv);
6856 rc = iwl4965_send_cmd_pdu(priv, REPLY_RXON_TIMING,
Zhu Yib481de92007-09-25 17:54:57 -07006857 sizeof(priv->rxon_timing), &priv->rxon_timing);
6858 if (rc)
6859 IWL_WARNING("REPLY_RXON_TIMING failed - "
6860 "Attempting to continue.\n");
6861
6862 priv->staging_rxon.filter_flags |= RXON_FILTER_ASSOC_MSK;
6863
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08006864#ifdef CONFIG_IWL4965_HT
Ron Rindjunskyfd105e72007-11-26 16:14:39 +02006865 if (priv->current_ht_config.is_ht)
6866 iwl4965_set_rxon_ht(priv, &priv->current_ht_config);
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08006867#endif /* CONFIG_IWL4965_HT*/
Zhu Yib481de92007-09-25 17:54:57 -07006868 iwl4965_set_rxon_chain(priv);
6869 priv->staging_rxon.assoc_id = cpu_to_le16(priv->assoc_id);
6870
6871 IWL_DEBUG_ASSOC("assoc id %d beacon interval %d\n",
6872 priv->assoc_id, priv->beacon_int);
6873
6874 if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_PREAMBLE)
6875 priv->staging_rxon.flags |= RXON_FLG_SHORT_PREAMBLE_MSK;
6876 else
6877 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_PREAMBLE_MSK;
6878
6879 if (priv->staging_rxon.flags & RXON_FLG_BAND_24G_MSK) {
6880 if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_SLOT_TIME)
6881 priv->staging_rxon.flags |= RXON_FLG_SHORT_SLOT_MSK;
6882 else
6883 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
6884
6885 if (priv->iw_mode == IEEE80211_IF_TYPE_IBSS)
6886 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
6887
6888 }
6889
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006890 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006891
6892 switch (priv->iw_mode) {
6893 case IEEE80211_IF_TYPE_STA:
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006894 iwl4965_rate_scale_init(priv->hw, IWL_AP_ID);
Zhu Yib481de92007-09-25 17:54:57 -07006895 break;
6896
6897 case IEEE80211_IF_TYPE_IBSS:
6898
6899 /* clear out the station table */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006900 iwl4965_clear_stations_table(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006901
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006902 iwl4965_rxon_add_station(priv, iwl4965_broadcast_addr, 0);
6903 iwl4965_rxon_add_station(priv, priv->bssid, 0);
6904 iwl4965_rate_scale_init(priv->hw, IWL_STA_ID);
6905 iwl4965_send_beacon_cmd(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006906
6907 break;
6908
6909 default:
6910 IWL_ERROR("%s Should not be called in %d mode\n",
6911 __FUNCTION__, priv->iw_mode);
6912 break;
6913 }
6914
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006915 iwl4965_sequence_reset(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006916
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08006917#ifdef CONFIG_IWL4965_SENSITIVITY
Zhu Yib481de92007-09-25 17:54:57 -07006918 /* Enable Rx differential gain and sensitivity calibrations */
6919 iwl4965_chain_noise_reset(priv);
6920 priv->start_calib = 1;
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08006921#endif /* CONFIG_IWL4965_SENSITIVITY */
Zhu Yib481de92007-09-25 17:54:57 -07006922
6923 if (priv->iw_mode == IEEE80211_IF_TYPE_IBSS)
6924 priv->assoc_station_added = 1;
6925
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006926 iwl4965_activate_qos(priv, 0);
Ron Rindjunsky292ae172008-02-06 11:20:39 -08006927
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08006928 /* we have just associated, don't start scan too early */
6929 priv->next_scan_jiffies = jiffies + IWL_DELAY_NEXT_SCAN;
Zhu Yib481de92007-09-25 17:54:57 -07006930 mutex_unlock(&priv->mutex);
6931}
6932
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006933static void iwl4965_bg_abort_scan(struct work_struct *work)
Zhu Yib481de92007-09-25 17:54:57 -07006934{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006935 struct iwl4965_priv *priv = container_of(work, struct iwl4965_priv, abort_scan);
Zhu Yib481de92007-09-25 17:54:57 -07006936
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006937 if (!iwl4965_is_ready(priv))
Zhu Yib481de92007-09-25 17:54:57 -07006938 return;
6939
6940 mutex_lock(&priv->mutex);
6941
6942 set_bit(STATUS_SCAN_ABORTING, &priv->status);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006943 iwl4965_send_scan_abort(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006944
6945 mutex_unlock(&priv->mutex);
6946}
6947
Zhu Yi76bb77e2007-11-22 10:53:22 +08006948static int iwl4965_mac_config(struct ieee80211_hw *hw, struct ieee80211_conf *conf);
6949
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006950static void iwl4965_bg_scan_completed(struct work_struct *work)
Zhu Yib481de92007-09-25 17:54:57 -07006951{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006952 struct iwl4965_priv *priv =
6953 container_of(work, struct iwl4965_priv, scan_completed);
Zhu Yib481de92007-09-25 17:54:57 -07006954
6955 IWL_DEBUG(IWL_DL_INFO | IWL_DL_SCAN, "SCAN complete scan\n");
6956
6957 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
6958 return;
6959
Zhu Yia0646472007-12-20 14:10:01 +08006960 if (test_bit(STATUS_CONF_PENDING, &priv->status))
6961 iwl4965_mac_config(priv->hw, ieee80211_get_hw_conf(priv->hw));
Zhu Yi76bb77e2007-11-22 10:53:22 +08006962
Zhu Yib481de92007-09-25 17:54:57 -07006963 ieee80211_scan_completed(priv->hw);
6964
6965 /* Since setting the TXPOWER may have been deferred while
6966 * performing the scan, fire one off */
6967 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006968 iwl4965_hw_reg_send_txpower(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006969 mutex_unlock(&priv->mutex);
6970}
6971
6972/*****************************************************************************
6973 *
6974 * mac80211 entry point functions
6975 *
6976 *****************************************************************************/
6977
Zhu Yi5a66926a2008-01-14 17:46:18 -08006978#define UCODE_READY_TIMEOUT (2 * HZ)
6979
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006980static int iwl4965_mac_start(struct ieee80211_hw *hw)
Zhu Yib481de92007-09-25 17:54:57 -07006981{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006982 struct iwl4965_priv *priv = hw->priv;
Zhu Yi5a66926a2008-01-14 17:46:18 -08006983 int ret;
Zhu Yib481de92007-09-25 17:54:57 -07006984
6985 IWL_DEBUG_MAC80211("enter\n");
6986
Zhu Yi5a66926a2008-01-14 17:46:18 -08006987 if (pci_enable_device(priv->pci_dev)) {
6988 IWL_ERROR("Fail to pci_enable_device\n");
6989 return -ENODEV;
6990 }
6991 pci_restore_state(priv->pci_dev);
6992 pci_enable_msi(priv->pci_dev);
6993
6994 ret = request_irq(priv->pci_dev->irq, iwl4965_isr, IRQF_SHARED,
6995 DRV_NAME, priv);
6996 if (ret) {
6997 IWL_ERROR("Error allocating IRQ %d\n", priv->pci_dev->irq);
6998 goto out_disable_msi;
6999 }
7000
Zhu Yib481de92007-09-25 17:54:57 -07007001 /* we should be verifying the device is ready to be opened */
7002 mutex_lock(&priv->mutex);
7003
Zhu Yi5a66926a2008-01-14 17:46:18 -08007004 memset(&priv->staging_rxon, 0, sizeof(struct iwl4965_rxon_cmd));
7005 /* fetch ucode file from disk, alloc and copy to bus-master buffers ...
7006 * ucode filename and max sizes are card-specific. */
7007
7008 if (!priv->ucode_code.len) {
7009 ret = iwl4965_read_ucode(priv);
7010 if (ret) {
7011 IWL_ERROR("Could not read microcode: %d\n", ret);
7012 mutex_unlock(&priv->mutex);
7013 goto out_release_irq;
7014 }
7015 }
7016
Zhu Yie655b9f2008-01-24 02:19:38 -08007017 ret = __iwl4965_up(priv);
Zhu Yi5a66926a2008-01-14 17:46:18 -08007018
Zhu Yib481de92007-09-25 17:54:57 -07007019 mutex_unlock(&priv->mutex);
Zhu Yi5a66926a2008-01-14 17:46:18 -08007020
Zhu Yie655b9f2008-01-24 02:19:38 -08007021 if (ret)
7022 goto out_release_irq;
7023
7024 IWL_DEBUG_INFO("Start UP work done.\n");
7025
7026 if (test_bit(STATUS_IN_SUSPEND, &priv->status))
7027 return 0;
7028
Zhu Yi5a66926a2008-01-14 17:46:18 -08007029 /* Wait for START_ALIVE from ucode. Otherwise callbacks from
7030 * mac80211 will not be run successfully. */
7031 ret = wait_event_interruptible_timeout(priv->wait_command_queue,
7032 test_bit(STATUS_READY, &priv->status),
7033 UCODE_READY_TIMEOUT);
7034 if (!ret) {
7035 if (!test_bit(STATUS_READY, &priv->status)) {
7036 IWL_ERROR("Wait for START_ALIVE timeout after %dms.\n",
7037 jiffies_to_msecs(UCODE_READY_TIMEOUT));
7038 ret = -ETIMEDOUT;
7039 goto out_release_irq;
7040 }
7041 }
7042
Zhu Yie655b9f2008-01-24 02:19:38 -08007043 priv->is_open = 1;
Zhu Yib481de92007-09-25 17:54:57 -07007044 IWL_DEBUG_MAC80211("leave\n");
7045 return 0;
Zhu Yi5a66926a2008-01-14 17:46:18 -08007046
7047out_release_irq:
7048 free_irq(priv->pci_dev->irq, priv);
7049out_disable_msi:
7050 pci_disable_msi(priv->pci_dev);
Zhu Yie655b9f2008-01-24 02:19:38 -08007051 pci_disable_device(priv->pci_dev);
7052 priv->is_open = 0;
7053 IWL_DEBUG_MAC80211("leave - failed\n");
Zhu Yi5a66926a2008-01-14 17:46:18 -08007054 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07007055}
7056
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007057static void iwl4965_mac_stop(struct ieee80211_hw *hw)
Zhu Yib481de92007-09-25 17:54:57 -07007058{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007059 struct iwl4965_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07007060
7061 IWL_DEBUG_MAC80211("enter\n");
Mohamed Abbas948c1712007-10-25 17:15:45 +08007062
Zhu Yie655b9f2008-01-24 02:19:38 -08007063 if (!priv->is_open) {
7064 IWL_DEBUG_MAC80211("leave - skip\n");
7065 return;
7066 }
7067
Zhu Yib481de92007-09-25 17:54:57 -07007068 priv->is_open = 0;
Zhu Yi5a66926a2008-01-14 17:46:18 -08007069
7070 if (iwl4965_is_ready_rf(priv)) {
Zhu Yie655b9f2008-01-24 02:19:38 -08007071 /* stop mac, cancel any scan request and clear
7072 * RXON_FILTER_ASSOC_MSK BIT
7073 */
Zhu Yi5a66926a2008-01-14 17:46:18 -08007074 mutex_lock(&priv->mutex);
7075 iwl4965_scan_cancel_timeout(priv, 100);
7076 cancel_delayed_work(&priv->post_associate);
Mohamed Abbasfde35712007-11-29 11:10:15 +08007077 mutex_unlock(&priv->mutex);
Mohamed Abbasfde35712007-11-29 11:10:15 +08007078 }
7079
Zhu Yi5a66926a2008-01-14 17:46:18 -08007080 iwl4965_down(priv);
7081
7082 flush_workqueue(priv->workqueue);
7083 free_irq(priv->pci_dev->irq, priv);
7084 pci_disable_msi(priv->pci_dev);
7085 pci_save_state(priv->pci_dev);
7086 pci_disable_device(priv->pci_dev);
Mohamed Abbas948c1712007-10-25 17:15:45 +08007087
Zhu Yib481de92007-09-25 17:54:57 -07007088 IWL_DEBUG_MAC80211("leave\n");
Zhu Yib481de92007-09-25 17:54:57 -07007089}
7090
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007091static int iwl4965_mac_tx(struct ieee80211_hw *hw, struct sk_buff *skb,
Zhu Yib481de92007-09-25 17:54:57 -07007092 struct ieee80211_tx_control *ctl)
7093{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007094 struct iwl4965_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07007095
7096 IWL_DEBUG_MAC80211("enter\n");
7097
7098 if (priv->iw_mode == IEEE80211_IF_TYPE_MNTR) {
7099 IWL_DEBUG_MAC80211("leave - monitor\n");
7100 return -1;
7101 }
7102
7103 IWL_DEBUG_TX("dev->xmit(%d bytes) at rate 0x%02x\n", skb->len,
Johannes Berg8318d782008-01-24 19:38:38 +01007104 ctl->tx_rate->bitrate);
Zhu Yib481de92007-09-25 17:54:57 -07007105
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007106 if (iwl4965_tx_skb(priv, skb, ctl))
Zhu Yib481de92007-09-25 17:54:57 -07007107 dev_kfree_skb_any(skb);
7108
7109 IWL_DEBUG_MAC80211("leave\n");
7110 return 0;
7111}
7112
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007113static int iwl4965_mac_add_interface(struct ieee80211_hw *hw,
Zhu Yib481de92007-09-25 17:54:57 -07007114 struct ieee80211_if_init_conf *conf)
7115{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007116 struct iwl4965_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07007117 unsigned long flags;
Joe Perches0795af52007-10-03 17:59:30 -07007118 DECLARE_MAC_BUF(mac);
Zhu Yib481de92007-09-25 17:54:57 -07007119
Johannes Berg32bfd352007-12-19 01:31:26 +01007120 IWL_DEBUG_MAC80211("enter: type %d\n", conf->type);
Zhu Yib481de92007-09-25 17:54:57 -07007121
Johannes Berg32bfd352007-12-19 01:31:26 +01007122 if (priv->vif) {
7123 IWL_DEBUG_MAC80211("leave - vif != NULL\n");
Reinette Chatre75849d22008-01-23 10:15:17 -08007124 return -EOPNOTSUPP;
Zhu Yib481de92007-09-25 17:54:57 -07007125 }
7126
7127 spin_lock_irqsave(&priv->lock, flags);
Johannes Berg32bfd352007-12-19 01:31:26 +01007128 priv->vif = conf->vif;
Zhu Yib481de92007-09-25 17:54:57 -07007129
7130 spin_unlock_irqrestore(&priv->lock, flags);
7131
7132 mutex_lock(&priv->mutex);
Tomas Winkler864792e2007-11-27 21:00:52 +02007133
7134 if (conf->mac_addr) {
7135 IWL_DEBUG_MAC80211("Set %s\n", print_mac(mac, conf->mac_addr));
7136 memcpy(priv->mac_addr, conf->mac_addr, ETH_ALEN);
7137 }
Zhu Yib481de92007-09-25 17:54:57 -07007138
Zhu Yi5a66926a2008-01-14 17:46:18 -08007139 if (iwl4965_is_ready(priv))
7140 iwl4965_set_mode(priv, conf->type);
7141
Zhu Yib481de92007-09-25 17:54:57 -07007142 mutex_unlock(&priv->mutex);
7143
Zhu Yi5a66926a2008-01-14 17:46:18 -08007144 IWL_DEBUG_MAC80211("leave\n");
Zhu Yib481de92007-09-25 17:54:57 -07007145 return 0;
7146}
7147
7148/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007149 * iwl4965_mac_config - mac80211 config callback
Zhu Yib481de92007-09-25 17:54:57 -07007150 *
7151 * We ignore conf->flags & IEEE80211_CONF_SHORT_SLOT_TIME since it seems to
7152 * be set inappropriately and the driver currently sets the hardware up to
7153 * use it whenever needed.
7154 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007155static int iwl4965_mac_config(struct ieee80211_hw *hw, struct ieee80211_conf *conf)
Zhu Yib481de92007-09-25 17:54:57 -07007156{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007157 struct iwl4965_priv *priv = hw->priv;
7158 const struct iwl4965_channel_info *ch_info;
Zhu Yib481de92007-09-25 17:54:57 -07007159 unsigned long flags;
Zhu Yi76bb77e2007-11-22 10:53:22 +08007160 int ret = 0;
Zhu Yib481de92007-09-25 17:54:57 -07007161
7162 mutex_lock(&priv->mutex);
Johannes Berg8318d782008-01-24 19:38:38 +01007163 IWL_DEBUG_MAC80211("enter to channel %d\n", conf->channel->hw_value);
Zhu Yib481de92007-09-25 17:54:57 -07007164
Zhu Yi12342c42007-12-20 11:27:32 +08007165 priv->add_radiotap = !!(conf->flags & IEEE80211_CONF_RADIOTAP);
7166
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007167 if (!iwl4965_is_ready(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07007168 IWL_DEBUG_MAC80211("leave - not ready\n");
Zhu Yi76bb77e2007-11-22 10:53:22 +08007169 ret = -EIO;
7170 goto out;
Zhu Yib481de92007-09-25 17:54:57 -07007171 }
7172
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007173 if (unlikely(!iwl4965_param_disable_hw_scan &&
Zhu Yib481de92007-09-25 17:54:57 -07007174 test_bit(STATUS_SCANNING, &priv->status))) {
Zhu Yia0646472007-12-20 14:10:01 +08007175 IWL_DEBUG_MAC80211("leave - scanning\n");
7176 set_bit(STATUS_CONF_PENDING, &priv->status);
Zhu Yib481de92007-09-25 17:54:57 -07007177 mutex_unlock(&priv->mutex);
Zhu Yia0646472007-12-20 14:10:01 +08007178 return 0;
Zhu Yib481de92007-09-25 17:54:57 -07007179 }
7180
7181 spin_lock_irqsave(&priv->lock, flags);
7182
Johannes Berg8318d782008-01-24 19:38:38 +01007183 ch_info = iwl4965_get_channel_info(priv, conf->channel->band,
7184 ieee80211_frequency_to_channel(conf->channel->center_freq));
Zhu Yib481de92007-09-25 17:54:57 -07007185 if (!is_channel_valid(ch_info)) {
Zhu Yib481de92007-09-25 17:54:57 -07007186 IWL_DEBUG_MAC80211("leave - invalid channel\n");
7187 spin_unlock_irqrestore(&priv->lock, flags);
Zhu Yi76bb77e2007-11-22 10:53:22 +08007188 ret = -EINVAL;
7189 goto out;
Zhu Yib481de92007-09-25 17:54:57 -07007190 }
7191
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08007192#ifdef CONFIG_IWL4965_HT
Tomas Winkler78330fd2008-02-06 02:37:18 +02007193 /* if we are switching from ht to 2.4 clear flags
Zhu Yib481de92007-09-25 17:54:57 -07007194 * from any ht related info since 2.4 does not
7195 * support ht */
Tomas Winkler78330fd2008-02-06 02:37:18 +02007196 if ((le16_to_cpu(priv->staging_rxon.channel) != conf->channel->hw_value)
Zhu Yib481de92007-09-25 17:54:57 -07007197#ifdef IEEE80211_CONF_CHANNEL_SWITCH
7198 && !(conf->flags & IEEE80211_CONF_CHANNEL_SWITCH)
7199#endif
7200 )
7201 priv->staging_rxon.flags = 0;
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08007202#endif /* CONFIG_IWL4965_HT */
Zhu Yib481de92007-09-25 17:54:57 -07007203
Johannes Berg8318d782008-01-24 19:38:38 +01007204 iwl4965_set_rxon_channel(priv, conf->channel->band,
7205 ieee80211_frequency_to_channel(conf->channel->center_freq));
Zhu Yib481de92007-09-25 17:54:57 -07007206
Johannes Berg8318d782008-01-24 19:38:38 +01007207 iwl4965_set_flags_for_phymode(priv, conf->channel->band);
Zhu Yib481de92007-09-25 17:54:57 -07007208
7209 /* The list of supported rates and rate mask can be different
Johannes Berg8318d782008-01-24 19:38:38 +01007210 * for each band; since the band may have changed, reset
Zhu Yib481de92007-09-25 17:54:57 -07007211 * the rate mask to what mac80211 lists */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007212 iwl4965_set_rate(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007213
7214 spin_unlock_irqrestore(&priv->lock, flags);
7215
7216#ifdef IEEE80211_CONF_CHANNEL_SWITCH
7217 if (conf->flags & IEEE80211_CONF_CHANNEL_SWITCH) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007218 iwl4965_hw_channel_switch(priv, conf->channel);
Zhu Yi76bb77e2007-11-22 10:53:22 +08007219 goto out;
Zhu Yib481de92007-09-25 17:54:57 -07007220 }
7221#endif
7222
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007223 iwl4965_radio_kill_sw(priv, !conf->radio_enabled);
Zhu Yib481de92007-09-25 17:54:57 -07007224
7225 if (!conf->radio_enabled) {
7226 IWL_DEBUG_MAC80211("leave - radio disabled\n");
Zhu Yi76bb77e2007-11-22 10:53:22 +08007227 goto out;
Zhu Yib481de92007-09-25 17:54:57 -07007228 }
7229
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007230 if (iwl4965_is_rfkill(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07007231 IWL_DEBUG_MAC80211("leave - RF kill\n");
Zhu Yi76bb77e2007-11-22 10:53:22 +08007232 ret = -EIO;
7233 goto out;
Zhu Yib481de92007-09-25 17:54:57 -07007234 }
7235
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007236 iwl4965_set_rate(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007237
7238 if (memcmp(&priv->active_rxon,
7239 &priv->staging_rxon, sizeof(priv->staging_rxon)))
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007240 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007241 else
7242 IWL_DEBUG_INFO("No re-sending same RXON configuration.\n");
7243
7244 IWL_DEBUG_MAC80211("leave\n");
7245
Zhu Yia0646472007-12-20 14:10:01 +08007246out:
7247 clear_bit(STATUS_CONF_PENDING, &priv->status);
Zhu Yi5a66926a2008-01-14 17:46:18 -08007248 mutex_unlock(&priv->mutex);
Zhu Yi76bb77e2007-11-22 10:53:22 +08007249 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07007250}
7251
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007252static void iwl4965_config_ap(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07007253{
7254 int rc = 0;
7255
Maarten Lankhorstd986bcd2008-01-23 10:15:16 -08007256 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
Zhu Yib481de92007-09-25 17:54:57 -07007257 return;
7258
7259 /* The following should be done only at AP bring up */
7260 if ((priv->active_rxon.filter_flags & RXON_FILTER_ASSOC_MSK) == 0) {
7261
7262 /* RXON - unassoc (to set timing command) */
7263 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007264 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007265
7266 /* RXON Timing */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007267 memset(&priv->rxon_timing, 0, sizeof(struct iwl4965_rxon_time_cmd));
7268 iwl4965_setup_rxon_timing(priv);
7269 rc = iwl4965_send_cmd_pdu(priv, REPLY_RXON_TIMING,
Zhu Yib481de92007-09-25 17:54:57 -07007270 sizeof(priv->rxon_timing), &priv->rxon_timing);
7271 if (rc)
7272 IWL_WARNING("REPLY_RXON_TIMING failed - "
7273 "Attempting to continue.\n");
7274
7275 iwl4965_set_rxon_chain(priv);
7276
7277 /* FIXME: what should be the assoc_id for AP? */
7278 priv->staging_rxon.assoc_id = cpu_to_le16(priv->assoc_id);
7279 if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_PREAMBLE)
7280 priv->staging_rxon.flags |=
7281 RXON_FLG_SHORT_PREAMBLE_MSK;
7282 else
7283 priv->staging_rxon.flags &=
7284 ~RXON_FLG_SHORT_PREAMBLE_MSK;
7285
7286 if (priv->staging_rxon.flags & RXON_FLG_BAND_24G_MSK) {
7287 if (priv->assoc_capability &
7288 WLAN_CAPABILITY_SHORT_SLOT_TIME)
7289 priv->staging_rxon.flags |=
7290 RXON_FLG_SHORT_SLOT_MSK;
7291 else
7292 priv->staging_rxon.flags &=
7293 ~RXON_FLG_SHORT_SLOT_MSK;
7294
7295 if (priv->iw_mode == IEEE80211_IF_TYPE_IBSS)
7296 priv->staging_rxon.flags &=
7297 ~RXON_FLG_SHORT_SLOT_MSK;
7298 }
7299 /* restore RXON assoc */
7300 priv->staging_rxon.filter_flags |= RXON_FILTER_ASSOC_MSK;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007301 iwl4965_commit_rxon(priv);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007302 iwl4965_activate_qos(priv, 1);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007303 iwl4965_rxon_add_station(priv, iwl4965_broadcast_addr, 0);
Zhu Yie1493de2007-09-27 11:27:32 +08007304 }
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007305 iwl4965_send_beacon_cmd(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007306
7307 /* FIXME - we need to add code here to detect a totally new
7308 * configuration, reset the AP, unassoc, rxon timing, assoc,
7309 * clear sta table, add BCAST sta... */
7310}
7311
Johannes Berg32bfd352007-12-19 01:31:26 +01007312static int iwl4965_mac_config_interface(struct ieee80211_hw *hw,
7313 struct ieee80211_vif *vif,
Zhu Yib481de92007-09-25 17:54:57 -07007314 struct ieee80211_if_conf *conf)
7315{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007316 struct iwl4965_priv *priv = hw->priv;
Joe Perches0795af52007-10-03 17:59:30 -07007317 DECLARE_MAC_BUF(mac);
Zhu Yib481de92007-09-25 17:54:57 -07007318 unsigned long flags;
7319 int rc;
7320
7321 if (conf == NULL)
7322 return -EIO;
7323
Emmanuel Grumbachb716bb92008-03-04 18:09:32 -08007324 if (priv->vif != vif) {
7325 IWL_DEBUG_MAC80211("leave - priv->vif != vif\n");
7326 mutex_unlock(&priv->mutex);
7327 return 0;
7328 }
7329
Zhu Yib481de92007-09-25 17:54:57 -07007330 if ((priv->iw_mode == IEEE80211_IF_TYPE_AP) &&
7331 (!conf->beacon || !conf->ssid_len)) {
7332 IWL_DEBUG_MAC80211
7333 ("Leaving in AP mode because HostAPD is not ready.\n");
7334 return 0;
7335 }
7336
Zhu Yi5a66926a2008-01-14 17:46:18 -08007337 if (!iwl4965_is_alive(priv))
7338 return -EAGAIN;
7339
Zhu Yib481de92007-09-25 17:54:57 -07007340 mutex_lock(&priv->mutex);
7341
Zhu Yib481de92007-09-25 17:54:57 -07007342 if (conf->bssid)
Joe Perches0795af52007-10-03 17:59:30 -07007343 IWL_DEBUG_MAC80211("bssid: %s\n",
7344 print_mac(mac, conf->bssid));
Zhu Yib481de92007-09-25 17:54:57 -07007345
Johannes Berg4150c572007-09-17 01:29:23 -04007346/*
7347 * very dubious code was here; the probe filtering flag is never set:
7348 *
Zhu Yib481de92007-09-25 17:54:57 -07007349 if (unlikely(test_bit(STATUS_SCANNING, &priv->status)) &&
7350 !(priv->hw->flags & IEEE80211_HW_NO_PROBE_FILTERING)) {
Johannes Berg4150c572007-09-17 01:29:23 -04007351 */
Zhu Yib481de92007-09-25 17:54:57 -07007352
7353 if (priv->iw_mode == IEEE80211_IF_TYPE_AP) {
7354 if (!conf->bssid) {
7355 conf->bssid = priv->mac_addr;
7356 memcpy(priv->bssid, priv->mac_addr, ETH_ALEN);
Joe Perches0795af52007-10-03 17:59:30 -07007357 IWL_DEBUG_MAC80211("bssid was set to: %s\n",
7358 print_mac(mac, conf->bssid));
Zhu Yib481de92007-09-25 17:54:57 -07007359 }
7360 if (priv->ibss_beacon)
7361 dev_kfree_skb(priv->ibss_beacon);
7362
7363 priv->ibss_beacon = conf->beacon;
7364 }
7365
Mohamed Abbasfde35712007-11-29 11:10:15 +08007366 if (iwl4965_is_rfkill(priv))
7367 goto done;
7368
Zhu Yib481de92007-09-25 17:54:57 -07007369 if (conf->bssid && !is_zero_ether_addr(conf->bssid) &&
7370 !is_multicast_ether_addr(conf->bssid)) {
7371 /* If there is currently a HW scan going on in the background
7372 * then we need to cancel it else the RXON below will fail. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007373 if (iwl4965_scan_cancel_timeout(priv, 100)) {
Zhu Yib481de92007-09-25 17:54:57 -07007374 IWL_WARNING("Aborted scan still in progress "
7375 "after 100ms\n");
7376 IWL_DEBUG_MAC80211("leaving - scan abort failed.\n");
7377 mutex_unlock(&priv->mutex);
7378 return -EAGAIN;
7379 }
7380 memcpy(priv->staging_rxon.bssid_addr, conf->bssid, ETH_ALEN);
7381
7382 /* TODO: Audit driver for usage of these members and see
7383 * if mac80211 deprecates them (priv->bssid looks like it
7384 * shouldn't be there, but I haven't scanned the IBSS code
7385 * to verify) - jpk */
7386 memcpy(priv->bssid, conf->bssid, ETH_ALEN);
7387
7388 if (priv->iw_mode == IEEE80211_IF_TYPE_AP)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007389 iwl4965_config_ap(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007390 else {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007391 rc = iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007392 if ((priv->iw_mode == IEEE80211_IF_TYPE_STA) && rc)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007393 iwl4965_rxon_add_station(
Zhu Yib481de92007-09-25 17:54:57 -07007394 priv, priv->active_rxon.bssid_addr, 1);
7395 }
7396
7397 } else {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007398 iwl4965_scan_cancel_timeout(priv, 100);
Zhu Yib481de92007-09-25 17:54:57 -07007399 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007400 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007401 }
7402
Mohamed Abbasfde35712007-11-29 11:10:15 +08007403 done:
Zhu Yib481de92007-09-25 17:54:57 -07007404 spin_lock_irqsave(&priv->lock, flags);
7405 if (!conf->ssid_len)
7406 memset(priv->essid, 0, IW_ESSID_MAX_SIZE);
7407 else
7408 memcpy(priv->essid, conf->ssid, conf->ssid_len);
7409
7410 priv->essid_len = conf->ssid_len;
7411 spin_unlock_irqrestore(&priv->lock, flags);
7412
7413 IWL_DEBUG_MAC80211("leave\n");
7414 mutex_unlock(&priv->mutex);
7415
7416 return 0;
7417}
7418
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007419static void iwl4965_configure_filter(struct ieee80211_hw *hw,
Johannes Berg4150c572007-09-17 01:29:23 -04007420 unsigned int changed_flags,
7421 unsigned int *total_flags,
7422 int mc_count, struct dev_addr_list *mc_list)
7423{
7424 /*
7425 * XXX: dummy
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007426 * see also iwl4965_connection_init_rx_config
Johannes Berg4150c572007-09-17 01:29:23 -04007427 */
7428 *total_flags = 0;
7429}
7430
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007431static void iwl4965_mac_remove_interface(struct ieee80211_hw *hw,
Zhu Yib481de92007-09-25 17:54:57 -07007432 struct ieee80211_if_init_conf *conf)
7433{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007434 struct iwl4965_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07007435
7436 IWL_DEBUG_MAC80211("enter\n");
7437
7438 mutex_lock(&priv->mutex);
Mohamed Abbas948c1712007-10-25 17:15:45 +08007439
Mohamed Abbasfde35712007-11-29 11:10:15 +08007440 if (iwl4965_is_ready_rf(priv)) {
7441 iwl4965_scan_cancel_timeout(priv, 100);
7442 cancel_delayed_work(&priv->post_associate);
7443 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
7444 iwl4965_commit_rxon(priv);
7445 }
Johannes Berg32bfd352007-12-19 01:31:26 +01007446 if (priv->vif == conf->vif) {
7447 priv->vif = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07007448 memset(priv->bssid, 0, ETH_ALEN);
7449 memset(priv->essid, 0, IW_ESSID_MAX_SIZE);
7450 priv->essid_len = 0;
7451 }
7452 mutex_unlock(&priv->mutex);
7453
7454 IWL_DEBUG_MAC80211("leave\n");
7455
7456}
Johannes Berg471b3ef2007-12-28 14:32:58 +01007457
7458static void iwl4965_bss_info_changed(struct ieee80211_hw *hw,
7459 struct ieee80211_vif *vif,
7460 struct ieee80211_bss_conf *bss_conf,
7461 u32 changes)
Tomas Winkler220173b2007-10-16 00:50:25 +02007462{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007463 struct iwl4965_priv *priv = hw->priv;
Tomas Winkler220173b2007-10-16 00:50:25 +02007464
Johannes Berg471b3ef2007-12-28 14:32:58 +01007465 if (changes & BSS_CHANGED_ERP_PREAMBLE) {
7466 if (bss_conf->use_short_preamble)
Tomas Winkler220173b2007-10-16 00:50:25 +02007467 priv->staging_rxon.flags |= RXON_FLG_SHORT_PREAMBLE_MSK;
7468 else
7469 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_PREAMBLE_MSK;
7470 }
7471
Johannes Berg471b3ef2007-12-28 14:32:58 +01007472 if (changes & BSS_CHANGED_ERP_CTS_PROT) {
Johannes Berg8318d782008-01-24 19:38:38 +01007473 if (bss_conf->use_cts_prot && (priv->band != IEEE80211_BAND_5GHZ))
Tomas Winkler220173b2007-10-16 00:50:25 +02007474 priv->staging_rxon.flags |= RXON_FLG_TGG_PROTECT_MSK;
7475 else
7476 priv->staging_rxon.flags &= ~RXON_FLG_TGG_PROTECT_MSK;
7477 }
7478
Johannes Berg471b3ef2007-12-28 14:32:58 +01007479 if (changes & BSS_CHANGED_ASSOC) {
7480 /*
7481 * TODO:
7482 * do stuff instead of sniffing assoc resp
7483 */
7484 }
7485
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007486 if (iwl4965_is_associated(priv))
7487 iwl4965_send_rxon_assoc(priv);
Tomas Winkler220173b2007-10-16 00:50:25 +02007488}
Zhu Yib481de92007-09-25 17:54:57 -07007489
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007490static int iwl4965_mac_hw_scan(struct ieee80211_hw *hw, u8 *ssid, size_t len)
Zhu Yib481de92007-09-25 17:54:57 -07007491{
7492 int rc = 0;
7493 unsigned long flags;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007494 struct iwl4965_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07007495
7496 IWL_DEBUG_MAC80211("enter\n");
7497
mabbas052c4b92007-10-25 17:15:43 +08007498 mutex_lock(&priv->mutex);
Zhu Yib481de92007-09-25 17:54:57 -07007499 spin_lock_irqsave(&priv->lock, flags);
7500
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007501 if (!iwl4965_is_ready_rf(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07007502 rc = -EIO;
7503 IWL_DEBUG_MAC80211("leave - not ready or exit pending\n");
7504 goto out_unlock;
7505 }
7506
7507 if (priv->iw_mode == IEEE80211_IF_TYPE_AP) { /* APs don't scan */
7508 rc = -EIO;
7509 IWL_ERROR("ERROR: APs don't scan\n");
7510 goto out_unlock;
7511 }
7512
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08007513 /* we don't schedule scan within next_scan_jiffies period */
7514 if (priv->next_scan_jiffies &&
7515 time_after(priv->next_scan_jiffies, jiffies)) {
7516 rc = -EAGAIN;
7517 goto out_unlock;
7518 }
Zhu Yib481de92007-09-25 17:54:57 -07007519 /* if we just finished scan ask for delay */
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08007520 if (priv->last_scan_jiffies && time_after(priv->last_scan_jiffies +
7521 IWL_DELAY_NEXT_SCAN, jiffies)) {
Zhu Yib481de92007-09-25 17:54:57 -07007522 rc = -EAGAIN;
7523 goto out_unlock;
7524 }
7525 if (len) {
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08007526 IWL_DEBUG_SCAN("direct scan for %s [%d]\n ",
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007527 iwl4965_escape_essid(ssid, len), (int)len);
Zhu Yib481de92007-09-25 17:54:57 -07007528
7529 priv->one_direct_scan = 1;
7530 priv->direct_ssid_len = (u8)
7531 min((u8) len, (u8) IW_ESSID_MAX_SIZE);
7532 memcpy(priv->direct_ssid, ssid, priv->direct_ssid_len);
Mohamed Abbas948c1712007-10-25 17:15:45 +08007533 } else
7534 priv->one_direct_scan = 0;
Zhu Yib481de92007-09-25 17:54:57 -07007535
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007536 rc = iwl4965_scan_initiate(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007537
7538 IWL_DEBUG_MAC80211("leave\n");
7539
7540out_unlock:
7541 spin_unlock_irqrestore(&priv->lock, flags);
mabbas052c4b92007-10-25 17:15:43 +08007542 mutex_unlock(&priv->mutex);
Zhu Yib481de92007-09-25 17:54:57 -07007543
7544 return rc;
7545}
7546
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007547static int iwl4965_mac_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
Zhu Yib481de92007-09-25 17:54:57 -07007548 const u8 *local_addr, const u8 *addr,
7549 struct ieee80211_key_conf *key)
7550{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007551 struct iwl4965_priv *priv = hw->priv;
Joe Perches0795af52007-10-03 17:59:30 -07007552 DECLARE_MAC_BUF(mac);
Zhu Yib481de92007-09-25 17:54:57 -07007553 int rc = 0;
7554 u8 sta_id;
7555
7556 IWL_DEBUG_MAC80211("enter\n");
7557
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007558 if (!iwl4965_param_hwcrypto) {
Zhu Yib481de92007-09-25 17:54:57 -07007559 IWL_DEBUG_MAC80211("leave - hwcrypto disabled\n");
7560 return -EOPNOTSUPP;
7561 }
7562
7563 if (is_zero_ether_addr(addr))
7564 /* only support pairwise keys */
7565 return -EOPNOTSUPP;
7566
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007567 sta_id = iwl4965_hw_find_station(priv, addr);
Zhu Yib481de92007-09-25 17:54:57 -07007568 if (sta_id == IWL_INVALID_STATION) {
Joe Perches0795af52007-10-03 17:59:30 -07007569 IWL_DEBUG_MAC80211("leave - %s not in station map.\n",
7570 print_mac(mac, addr));
Zhu Yib481de92007-09-25 17:54:57 -07007571 return -EINVAL;
7572 }
7573
7574 mutex_lock(&priv->mutex);
7575
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007576 iwl4965_scan_cancel_timeout(priv, 100);
mabbas052c4b92007-10-25 17:15:43 +08007577
Zhu Yib481de92007-09-25 17:54:57 -07007578 switch (cmd) {
7579 case SET_KEY:
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007580 rc = iwl4965_update_sta_key_info(priv, key, sta_id);
Zhu Yib481de92007-09-25 17:54:57 -07007581 if (!rc) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007582 iwl4965_set_rxon_hwcrypto(priv, 1);
7583 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007584 key->hw_key_idx = sta_id;
7585 IWL_DEBUG_MAC80211("set_key success, using hwcrypto\n");
7586 key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
7587 }
7588 break;
7589 case DISABLE_KEY:
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007590 rc = iwl4965_clear_sta_key_info(priv, sta_id);
Zhu Yib481de92007-09-25 17:54:57 -07007591 if (!rc) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007592 iwl4965_set_rxon_hwcrypto(priv, 0);
7593 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007594 IWL_DEBUG_MAC80211("disable hwcrypto key\n");
7595 }
7596 break;
7597 default:
7598 rc = -EINVAL;
7599 }
7600
7601 IWL_DEBUG_MAC80211("leave\n");
7602 mutex_unlock(&priv->mutex);
7603
7604 return rc;
7605}
7606
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007607static int iwl4965_mac_conf_tx(struct ieee80211_hw *hw, int queue,
Zhu Yib481de92007-09-25 17:54:57 -07007608 const struct ieee80211_tx_queue_params *params)
7609{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007610 struct iwl4965_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07007611 unsigned long flags;
7612 int q;
Zhu Yib481de92007-09-25 17:54:57 -07007613
7614 IWL_DEBUG_MAC80211("enter\n");
7615
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007616 if (!iwl4965_is_ready_rf(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07007617 IWL_DEBUG_MAC80211("leave - RF not ready\n");
7618 return -EIO;
7619 }
7620
7621 if (queue >= AC_NUM) {
7622 IWL_DEBUG_MAC80211("leave - queue >= AC_NUM %d\n", queue);
7623 return 0;
7624 }
7625
Zhu Yib481de92007-09-25 17:54:57 -07007626 if (!priv->qos_data.qos_enable) {
7627 priv->qos_data.qos_active = 0;
7628 IWL_DEBUG_MAC80211("leave - qos not enabled\n");
7629 return 0;
7630 }
7631 q = AC_NUM - 1 - queue;
7632
7633 spin_lock_irqsave(&priv->lock, flags);
7634
7635 priv->qos_data.def_qos_parm.ac[q].cw_min = cpu_to_le16(params->cw_min);
7636 priv->qos_data.def_qos_parm.ac[q].cw_max = cpu_to_le16(params->cw_max);
7637 priv->qos_data.def_qos_parm.ac[q].aifsn = params->aifs;
7638 priv->qos_data.def_qos_parm.ac[q].edca_txop =
Johannes Berg3330d7b2008-02-10 16:49:38 +01007639 cpu_to_le16((params->txop * 32));
Zhu Yib481de92007-09-25 17:54:57 -07007640
7641 priv->qos_data.def_qos_parm.ac[q].reserved1 = 0;
7642 priv->qos_data.qos_active = 1;
7643
7644 spin_unlock_irqrestore(&priv->lock, flags);
7645
7646 mutex_lock(&priv->mutex);
7647 if (priv->iw_mode == IEEE80211_IF_TYPE_AP)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007648 iwl4965_activate_qos(priv, 1);
7649 else if (priv->assoc_id && iwl4965_is_associated(priv))
7650 iwl4965_activate_qos(priv, 0);
Zhu Yib481de92007-09-25 17:54:57 -07007651
7652 mutex_unlock(&priv->mutex);
7653
Zhu Yib481de92007-09-25 17:54:57 -07007654 IWL_DEBUG_MAC80211("leave\n");
7655 return 0;
7656}
7657
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007658static int iwl4965_mac_get_tx_stats(struct ieee80211_hw *hw,
Zhu Yib481de92007-09-25 17:54:57 -07007659 struct ieee80211_tx_queue_stats *stats)
7660{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007661 struct iwl4965_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07007662 int i, avail;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007663 struct iwl4965_tx_queue *txq;
7664 struct iwl4965_queue *q;
Zhu Yib481de92007-09-25 17:54:57 -07007665 unsigned long flags;
7666
7667 IWL_DEBUG_MAC80211("enter\n");
7668
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007669 if (!iwl4965_is_ready_rf(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07007670 IWL_DEBUG_MAC80211("leave - RF not ready\n");
7671 return -EIO;
7672 }
7673
7674 spin_lock_irqsave(&priv->lock, flags);
7675
7676 for (i = 0; i < AC_NUM; i++) {
7677 txq = &priv->txq[i];
7678 q = &txq->q;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007679 avail = iwl4965_queue_space(q);
Zhu Yib481de92007-09-25 17:54:57 -07007680
7681 stats->data[i].len = q->n_window - avail;
7682 stats->data[i].limit = q->n_window - q->high_mark;
7683 stats->data[i].count = q->n_window;
7684
7685 }
7686 spin_unlock_irqrestore(&priv->lock, flags);
7687
7688 IWL_DEBUG_MAC80211("leave\n");
7689
7690 return 0;
7691}
7692
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007693static int iwl4965_mac_get_stats(struct ieee80211_hw *hw,
Zhu Yib481de92007-09-25 17:54:57 -07007694 struct ieee80211_low_level_stats *stats)
7695{
7696 IWL_DEBUG_MAC80211("enter\n");
7697 IWL_DEBUG_MAC80211("leave\n");
7698
7699 return 0;
7700}
7701
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007702static u64 iwl4965_mac_get_tsf(struct ieee80211_hw *hw)
Zhu Yib481de92007-09-25 17:54:57 -07007703{
7704 IWL_DEBUG_MAC80211("enter\n");
7705 IWL_DEBUG_MAC80211("leave\n");
7706
7707 return 0;
7708}
7709
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007710static void iwl4965_mac_reset_tsf(struct ieee80211_hw *hw)
Zhu Yib481de92007-09-25 17:54:57 -07007711{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007712 struct iwl4965_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07007713 unsigned long flags;
7714
7715 mutex_lock(&priv->mutex);
7716 IWL_DEBUG_MAC80211("enter\n");
7717
7718 priv->lq_mngr.lq_ready = 0;
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08007719#ifdef CONFIG_IWL4965_HT
Zhu Yib481de92007-09-25 17:54:57 -07007720 spin_lock_irqsave(&priv->lock, flags);
Ron Rindjunskyfd105e72007-11-26 16:14:39 +02007721 memset(&priv->current_ht_config, 0, sizeof(struct iwl_ht_info));
Zhu Yib481de92007-09-25 17:54:57 -07007722 spin_unlock_irqrestore(&priv->lock, flags);
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08007723#endif /* CONFIG_IWL4965_HT */
Zhu Yib481de92007-09-25 17:54:57 -07007724
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007725 iwl4965_reset_qos(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007726
7727 cancel_delayed_work(&priv->post_associate);
7728
7729 spin_lock_irqsave(&priv->lock, flags);
7730 priv->assoc_id = 0;
7731 priv->assoc_capability = 0;
7732 priv->call_post_assoc_from_beacon = 0;
7733 priv->assoc_station_added = 0;
7734
7735 /* new association get rid of ibss beacon skb */
7736 if (priv->ibss_beacon)
7737 dev_kfree_skb(priv->ibss_beacon);
7738
7739 priv->ibss_beacon = NULL;
7740
7741 priv->beacon_int = priv->hw->conf.beacon_int;
7742 priv->timestamp1 = 0;
7743 priv->timestamp0 = 0;
7744 if ((priv->iw_mode == IEEE80211_IF_TYPE_STA))
7745 priv->beacon_int = 0;
7746
7747 spin_unlock_irqrestore(&priv->lock, flags);
7748
Mohamed Abbasfde35712007-11-29 11:10:15 +08007749 if (!iwl4965_is_ready_rf(priv)) {
7750 IWL_DEBUG_MAC80211("leave - not ready\n");
7751 mutex_unlock(&priv->mutex);
7752 return;
7753 }
7754
mabbas052c4b92007-10-25 17:15:43 +08007755 /* we are restarting association process
7756 * clear RXON_FILTER_ASSOC_MSK bit
7757 */
7758 if (priv->iw_mode != IEEE80211_IF_TYPE_AP) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007759 iwl4965_scan_cancel_timeout(priv, 100);
mabbas052c4b92007-10-25 17:15:43 +08007760 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007761 iwl4965_commit_rxon(priv);
mabbas052c4b92007-10-25 17:15:43 +08007762 }
7763
Zhu Yib481de92007-09-25 17:54:57 -07007764 /* Per mac80211.h: This is only used in IBSS mode... */
7765 if (priv->iw_mode != IEEE80211_IF_TYPE_IBSS) {
mabbas052c4b92007-10-25 17:15:43 +08007766
Zhu Yib481de92007-09-25 17:54:57 -07007767 IWL_DEBUG_MAC80211("leave - not in IBSS\n");
7768 mutex_unlock(&priv->mutex);
7769 return;
7770 }
7771
Zhu Yib481de92007-09-25 17:54:57 -07007772 priv->only_active_channel = 0;
7773
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007774 iwl4965_set_rate(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007775
7776 mutex_unlock(&priv->mutex);
7777
7778 IWL_DEBUG_MAC80211("leave\n");
Zhu Yib481de92007-09-25 17:54:57 -07007779}
7780
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007781static int iwl4965_mac_beacon_update(struct ieee80211_hw *hw, struct sk_buff *skb,
Zhu Yib481de92007-09-25 17:54:57 -07007782 struct ieee80211_tx_control *control)
7783{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007784 struct iwl4965_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07007785 unsigned long flags;
7786
7787 mutex_lock(&priv->mutex);
7788 IWL_DEBUG_MAC80211("enter\n");
7789
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007790 if (!iwl4965_is_ready_rf(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07007791 IWL_DEBUG_MAC80211("leave - RF not ready\n");
7792 mutex_unlock(&priv->mutex);
7793 return -EIO;
7794 }
7795
7796 if (priv->iw_mode != IEEE80211_IF_TYPE_IBSS) {
7797 IWL_DEBUG_MAC80211("leave - not IBSS\n");
7798 mutex_unlock(&priv->mutex);
7799 return -EIO;
7800 }
7801
7802 spin_lock_irqsave(&priv->lock, flags);
7803
7804 if (priv->ibss_beacon)
7805 dev_kfree_skb(priv->ibss_beacon);
7806
7807 priv->ibss_beacon = skb;
7808
7809 priv->assoc_id = 0;
7810
7811 IWL_DEBUG_MAC80211("leave\n");
7812 spin_unlock_irqrestore(&priv->lock, flags);
7813
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007814 iwl4965_reset_qos(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007815
7816 queue_work(priv->workqueue, &priv->post_associate.work);
7817
7818 mutex_unlock(&priv->mutex);
7819
7820 return 0;
7821}
7822
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08007823#ifdef CONFIG_IWL4965_HT
Zhu Yib481de92007-09-25 17:54:57 -07007824
Ron Rindjunskyfd105e72007-11-26 16:14:39 +02007825static void iwl4965_ht_info_fill(struct ieee80211_conf *conf,
7826 struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07007827{
Ron Rindjunskyfd105e72007-11-26 16:14:39 +02007828 struct iwl_ht_info *iwl_conf = &priv->current_ht_config;
7829 struct ieee80211_ht_info *ht_conf = &conf->ht_conf;
7830 struct ieee80211_ht_bss_info *ht_bss_conf = &conf->ht_bss_conf;
Zhu Yib481de92007-09-25 17:54:57 -07007831
7832 IWL_DEBUG_MAC80211("enter: \n");
7833
Ron Rindjunskyfd105e72007-11-26 16:14:39 +02007834 if (!(conf->flags & IEEE80211_CONF_SUPPORT_HT_MODE)) {
7835 iwl_conf->is_ht = 0;
7836 return;
Zhu Yib481de92007-09-25 17:54:57 -07007837 }
7838
Ron Rindjunskyfd105e72007-11-26 16:14:39 +02007839 iwl_conf->is_ht = 1;
7840 priv->ps_mode = (u8)((ht_conf->cap & IEEE80211_HT_CAP_MIMO_PS) >> 2);
Zhu Yib481de92007-09-25 17:54:57 -07007841
Ron Rindjunskyfd105e72007-11-26 16:14:39 +02007842 if (ht_conf->cap & IEEE80211_HT_CAP_SGI_20)
7843 iwl_conf->sgf |= 0x1;
7844 if (ht_conf->cap & IEEE80211_HT_CAP_SGI_40)
7845 iwl_conf->sgf |= 0x2;
Zhu Yib481de92007-09-25 17:54:57 -07007846
Ron Rindjunskyfd105e72007-11-26 16:14:39 +02007847 iwl_conf->is_green_field = !!(ht_conf->cap & IEEE80211_HT_CAP_GRN_FLD);
7848 iwl_conf->max_amsdu_size =
7849 !!(ht_conf->cap & IEEE80211_HT_CAP_MAX_AMSDU);
Guy Cohen134eb5d2008-03-04 18:09:25 -08007850
Ron Rindjunskyfd105e72007-11-26 16:14:39 +02007851 iwl_conf->supported_chan_width =
7852 !!(ht_conf->cap & IEEE80211_HT_CAP_SUP_WIDTH);
Guy Cohen134eb5d2008-03-04 18:09:25 -08007853 iwl_conf->extension_chan_offset =
7854 ht_bss_conf->bss_cap & IEEE80211_HT_IE_CHA_SEC_OFFSET;
7855 /* If no above or below channel supplied disable FAT channel */
7856 if (iwl_conf->extension_chan_offset != IWL_EXT_CHANNEL_OFFSET_ABOVE &&
7857 iwl_conf->extension_chan_offset != IWL_EXT_CHANNEL_OFFSET_BELOW)
7858 iwl_conf->supported_chan_width = 0;
7859
Ron Rindjunskyfd105e72007-11-26 16:14:39 +02007860 iwl_conf->tx_mimo_ps_mode =
7861 (u8)((ht_conf->cap & IEEE80211_HT_CAP_MIMO_PS) >> 2);
7862 memcpy(iwl_conf->supp_mcs_set, ht_conf->supp_mcs_set, 16);
Zhu Yib481de92007-09-25 17:54:57 -07007863
Ron Rindjunskyfd105e72007-11-26 16:14:39 +02007864 iwl_conf->control_channel = ht_bss_conf->primary_channel;
Ron Rindjunskyfd105e72007-11-26 16:14:39 +02007865 iwl_conf->tx_chan_width =
7866 !!(ht_bss_conf->bss_cap & IEEE80211_HT_IE_CHA_WIDTH);
7867 iwl_conf->ht_protection =
7868 ht_bss_conf->bss_op_mode & IEEE80211_HT_IE_HT_PROTECTION;
7869 iwl_conf->non_GF_STA_present =
7870 !!(ht_bss_conf->bss_op_mode & IEEE80211_HT_IE_NON_GF_STA_PRSNT);
Zhu Yib481de92007-09-25 17:54:57 -07007871
Ron Rindjunskyfd105e72007-11-26 16:14:39 +02007872 IWL_DEBUG_MAC80211("control channel %d\n",
7873 iwl_conf->control_channel);
Zhu Yib481de92007-09-25 17:54:57 -07007874 IWL_DEBUG_MAC80211("leave\n");
Zhu Yib481de92007-09-25 17:54:57 -07007875}
7876
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007877static int iwl4965_mac_conf_ht(struct ieee80211_hw *hw,
Ron Rindjunskyfd105e72007-11-26 16:14:39 +02007878 struct ieee80211_conf *conf)
Zhu Yib481de92007-09-25 17:54:57 -07007879{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007880 struct iwl4965_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07007881
7882 IWL_DEBUG_MAC80211("enter: \n");
7883
Ron Rindjunskyfd105e72007-11-26 16:14:39 +02007884 iwl4965_ht_info_fill(conf, priv);
Zhu Yib481de92007-09-25 17:54:57 -07007885 iwl4965_set_rxon_chain(priv);
7886
7887 if (priv && priv->assoc_id &&
7888 (priv->iw_mode == IEEE80211_IF_TYPE_STA)) {
7889 unsigned long flags;
7890
7891 spin_lock_irqsave(&priv->lock, flags);
7892 if (priv->beacon_int)
7893 queue_work(priv->workqueue, &priv->post_associate.work);
7894 else
7895 priv->call_post_assoc_from_beacon = 1;
7896 spin_unlock_irqrestore(&priv->lock, flags);
7897 }
7898
Ron Rindjunskyfd105e72007-11-26 16:14:39 +02007899 IWL_DEBUG_MAC80211("leave:\n");
7900 return 0;
Zhu Yib481de92007-09-25 17:54:57 -07007901}
7902
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08007903#endif /*CONFIG_IWL4965_HT*/
Zhu Yib481de92007-09-25 17:54:57 -07007904
7905/*****************************************************************************
7906 *
7907 * sysfs attributes
7908 *
7909 *****************************************************************************/
7910
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08007911#ifdef CONFIG_IWL4965_DEBUG
Zhu Yib481de92007-09-25 17:54:57 -07007912
7913/*
7914 * The following adds a new attribute to the sysfs representation
7915 * of this device driver (i.e. a new file in /sys/bus/pci/drivers/iwl/)
7916 * used for controlling the debug level.
7917 *
7918 * See the level definitions in iwl for details.
7919 */
7920
7921static ssize_t show_debug_level(struct device_driver *d, char *buf)
7922{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007923 return sprintf(buf, "0x%08X\n", iwl4965_debug_level);
Zhu Yib481de92007-09-25 17:54:57 -07007924}
7925static ssize_t store_debug_level(struct device_driver *d,
7926 const char *buf, size_t count)
7927{
7928 char *p = (char *)buf;
7929 u32 val;
7930
7931 val = simple_strtoul(p, &p, 0);
7932 if (p == buf)
7933 printk(KERN_INFO DRV_NAME
7934 ": %s is not in hex or decimal form.\n", buf);
7935 else
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007936 iwl4965_debug_level = val;
Zhu Yib481de92007-09-25 17:54:57 -07007937
7938 return strnlen(buf, count);
7939}
7940
7941static DRIVER_ATTR(debug_level, S_IWUSR | S_IRUGO,
7942 show_debug_level, store_debug_level);
7943
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08007944#endif /* CONFIG_IWL4965_DEBUG */
Zhu Yib481de92007-09-25 17:54:57 -07007945
7946static ssize_t show_rf_kill(struct device *d,
7947 struct device_attribute *attr, char *buf)
7948{
7949 /*
7950 * 0 - RF kill not enabled
7951 * 1 - SW based RF kill active (sysfs)
7952 * 2 - HW based RF kill active
7953 * 3 - Both HW and SW based RF kill active
7954 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007955 struct iwl4965_priv *priv = (struct iwl4965_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07007956 int val = (test_bit(STATUS_RF_KILL_SW, &priv->status) ? 0x1 : 0x0) |
7957 (test_bit(STATUS_RF_KILL_HW, &priv->status) ? 0x2 : 0x0);
7958
7959 return sprintf(buf, "%i\n", val);
7960}
7961
7962static ssize_t store_rf_kill(struct device *d,
7963 struct device_attribute *attr,
7964 const char *buf, size_t count)
7965{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007966 struct iwl4965_priv *priv = (struct iwl4965_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07007967
7968 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007969 iwl4965_radio_kill_sw(priv, buf[0] == '1');
Zhu Yib481de92007-09-25 17:54:57 -07007970 mutex_unlock(&priv->mutex);
7971
7972 return count;
7973}
7974
7975static DEVICE_ATTR(rf_kill, S_IWUSR | S_IRUGO, show_rf_kill, store_rf_kill);
7976
7977static ssize_t show_temperature(struct device *d,
7978 struct device_attribute *attr, char *buf)
7979{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007980 struct iwl4965_priv *priv = (struct iwl4965_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07007981
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007982 if (!iwl4965_is_alive(priv))
Zhu Yib481de92007-09-25 17:54:57 -07007983 return -EAGAIN;
7984
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007985 return sprintf(buf, "%d\n", iwl4965_hw_get_temperature(priv));
Zhu Yib481de92007-09-25 17:54:57 -07007986}
7987
7988static DEVICE_ATTR(temperature, S_IRUGO, show_temperature, NULL);
7989
7990static ssize_t show_rs_window(struct device *d,
7991 struct device_attribute *attr,
7992 char *buf)
7993{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007994 struct iwl4965_priv *priv = d->driver_data;
7995 return iwl4965_fill_rs_info(priv->hw, buf, IWL_AP_ID);
Zhu Yib481de92007-09-25 17:54:57 -07007996}
7997static DEVICE_ATTR(rs_window, S_IRUGO, show_rs_window, NULL);
7998
7999static ssize_t show_tx_power(struct device *d,
8000 struct device_attribute *attr, char *buf)
8001{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008002 struct iwl4965_priv *priv = (struct iwl4965_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07008003 return sprintf(buf, "%d\n", priv->user_txpower_limit);
8004}
8005
8006static ssize_t store_tx_power(struct device *d,
8007 struct device_attribute *attr,
8008 const char *buf, size_t count)
8009{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008010 struct iwl4965_priv *priv = (struct iwl4965_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07008011 char *p = (char *)buf;
8012 u32 val;
8013
8014 val = simple_strtoul(p, &p, 10);
8015 if (p == buf)
8016 printk(KERN_INFO DRV_NAME
8017 ": %s is not in decimal form.\n", buf);
8018 else
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008019 iwl4965_hw_reg_set_txpower(priv, val);
Zhu Yib481de92007-09-25 17:54:57 -07008020
8021 return count;
8022}
8023
8024static DEVICE_ATTR(tx_power, S_IWUSR | S_IRUGO, show_tx_power, store_tx_power);
8025
8026static ssize_t show_flags(struct device *d,
8027 struct device_attribute *attr, char *buf)
8028{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008029 struct iwl4965_priv *priv = (struct iwl4965_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07008030
8031 return sprintf(buf, "0x%04X\n", priv->active_rxon.flags);
8032}
8033
8034static ssize_t store_flags(struct device *d,
8035 struct device_attribute *attr,
8036 const char *buf, size_t count)
8037{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008038 struct iwl4965_priv *priv = (struct iwl4965_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07008039 u32 flags = simple_strtoul(buf, NULL, 0);
8040
8041 mutex_lock(&priv->mutex);
8042 if (le32_to_cpu(priv->staging_rxon.flags) != flags) {
8043 /* Cancel any currently running scans... */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008044 if (iwl4965_scan_cancel_timeout(priv, 100))
Zhu Yib481de92007-09-25 17:54:57 -07008045 IWL_WARNING("Could not cancel scan.\n");
8046 else {
8047 IWL_DEBUG_INFO("Committing rxon.flags = 0x%04X\n",
8048 flags);
8049 priv->staging_rxon.flags = cpu_to_le32(flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008050 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07008051 }
8052 }
8053 mutex_unlock(&priv->mutex);
8054
8055 return count;
8056}
8057
8058static DEVICE_ATTR(flags, S_IWUSR | S_IRUGO, show_flags, store_flags);
8059
8060static ssize_t show_filter_flags(struct device *d,
8061 struct device_attribute *attr, char *buf)
8062{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008063 struct iwl4965_priv *priv = (struct iwl4965_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07008064
8065 return sprintf(buf, "0x%04X\n",
8066 le32_to_cpu(priv->active_rxon.filter_flags));
8067}
8068
8069static ssize_t store_filter_flags(struct device *d,
8070 struct device_attribute *attr,
8071 const char *buf, size_t count)
8072{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008073 struct iwl4965_priv *priv = (struct iwl4965_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07008074 u32 filter_flags = simple_strtoul(buf, NULL, 0);
8075
8076 mutex_lock(&priv->mutex);
8077 if (le32_to_cpu(priv->staging_rxon.filter_flags) != filter_flags) {
8078 /* Cancel any currently running scans... */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008079 if (iwl4965_scan_cancel_timeout(priv, 100))
Zhu Yib481de92007-09-25 17:54:57 -07008080 IWL_WARNING("Could not cancel scan.\n");
8081 else {
8082 IWL_DEBUG_INFO("Committing rxon.filter_flags = "
8083 "0x%04X\n", filter_flags);
8084 priv->staging_rxon.filter_flags =
8085 cpu_to_le32(filter_flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008086 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07008087 }
8088 }
8089 mutex_unlock(&priv->mutex);
8090
8091 return count;
8092}
8093
8094static DEVICE_ATTR(filter_flags, S_IWUSR | S_IRUGO, show_filter_flags,
8095 store_filter_flags);
8096
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08008097#ifdef CONFIG_IWL4965_SPECTRUM_MEASUREMENT
Zhu Yib481de92007-09-25 17:54:57 -07008098
8099static ssize_t show_measurement(struct device *d,
8100 struct device_attribute *attr, char *buf)
8101{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008102 struct iwl4965_priv *priv = dev_get_drvdata(d);
8103 struct iwl4965_spectrum_notification measure_report;
Zhu Yib481de92007-09-25 17:54:57 -07008104 u32 size = sizeof(measure_report), len = 0, ofs = 0;
8105 u8 *data = (u8 *) & measure_report;
8106 unsigned long flags;
8107
8108 spin_lock_irqsave(&priv->lock, flags);
8109 if (!(priv->measurement_status & MEASUREMENT_READY)) {
8110 spin_unlock_irqrestore(&priv->lock, flags);
8111 return 0;
8112 }
8113 memcpy(&measure_report, &priv->measure_report, size);
8114 priv->measurement_status = 0;
8115 spin_unlock_irqrestore(&priv->lock, flags);
8116
8117 while (size && (PAGE_SIZE - len)) {
8118 hex_dump_to_buffer(data + ofs, size, 16, 1, buf + len,
8119 PAGE_SIZE - len, 1);
8120 len = strlen(buf);
8121 if (PAGE_SIZE - len)
8122 buf[len++] = '\n';
8123
8124 ofs += 16;
8125 size -= min(size, 16U);
8126 }
8127
8128 return len;
8129}
8130
8131static ssize_t store_measurement(struct device *d,
8132 struct device_attribute *attr,
8133 const char *buf, size_t count)
8134{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008135 struct iwl4965_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07008136 struct ieee80211_measurement_params params = {
8137 .channel = le16_to_cpu(priv->active_rxon.channel),
8138 .start_time = cpu_to_le64(priv->last_tsf),
8139 .duration = cpu_to_le16(1),
8140 };
8141 u8 type = IWL_MEASURE_BASIC;
8142 u8 buffer[32];
8143 u8 channel;
8144
8145 if (count) {
8146 char *p = buffer;
8147 strncpy(buffer, buf, min(sizeof(buffer), count));
8148 channel = simple_strtoul(p, NULL, 0);
8149 if (channel)
8150 params.channel = channel;
8151
8152 p = buffer;
8153 while (*p && *p != ' ')
8154 p++;
8155 if (*p)
8156 type = simple_strtoul(p + 1, NULL, 0);
8157 }
8158
8159 IWL_DEBUG_INFO("Invoking measurement of type %d on "
8160 "channel %d (for '%s')\n", type, params.channel, buf);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008161 iwl4965_get_measurement(priv, &params, type);
Zhu Yib481de92007-09-25 17:54:57 -07008162
8163 return count;
8164}
8165
8166static DEVICE_ATTR(measurement, S_IRUSR | S_IWUSR,
8167 show_measurement, store_measurement);
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08008168#endif /* CONFIG_IWL4965_SPECTRUM_MEASUREMENT */
Zhu Yib481de92007-09-25 17:54:57 -07008169
8170static ssize_t store_retry_rate(struct device *d,
8171 struct device_attribute *attr,
8172 const char *buf, size_t count)
8173{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008174 struct iwl4965_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07008175
8176 priv->retry_rate = simple_strtoul(buf, NULL, 0);
8177 if (priv->retry_rate <= 0)
8178 priv->retry_rate = 1;
8179
8180 return count;
8181}
8182
8183static ssize_t show_retry_rate(struct device *d,
8184 struct device_attribute *attr, char *buf)
8185{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008186 struct iwl4965_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07008187 return sprintf(buf, "%d", priv->retry_rate);
8188}
8189
8190static DEVICE_ATTR(retry_rate, S_IWUSR | S_IRUSR, show_retry_rate,
8191 store_retry_rate);
8192
8193static ssize_t store_power_level(struct device *d,
8194 struct device_attribute *attr,
8195 const char *buf, size_t count)
8196{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008197 struct iwl4965_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07008198 int rc;
8199 int mode;
8200
8201 mode = simple_strtoul(buf, NULL, 0);
8202 mutex_lock(&priv->mutex);
8203
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008204 if (!iwl4965_is_ready(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07008205 rc = -EAGAIN;
8206 goto out;
8207 }
8208
8209 if ((mode < 1) || (mode > IWL_POWER_LIMIT) || (mode == IWL_POWER_AC))
8210 mode = IWL_POWER_AC;
8211 else
8212 mode |= IWL_POWER_ENABLED;
8213
8214 if (mode != priv->power_mode) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008215 rc = iwl4965_send_power_mode(priv, IWL_POWER_LEVEL(mode));
Zhu Yib481de92007-09-25 17:54:57 -07008216 if (rc) {
8217 IWL_DEBUG_MAC80211("failed setting power mode.\n");
8218 goto out;
8219 }
8220 priv->power_mode = mode;
8221 }
8222
8223 rc = count;
8224
8225 out:
8226 mutex_unlock(&priv->mutex);
8227 return rc;
8228}
8229
8230#define MAX_WX_STRING 80
8231
8232/* Values are in microsecond */
8233static const s32 timeout_duration[] = {
8234 350000,
8235 250000,
8236 75000,
8237 37000,
8238 25000,
8239};
8240static const s32 period_duration[] = {
8241 400000,
8242 700000,
8243 1000000,
8244 1000000,
8245 1000000
8246};
8247
8248static ssize_t show_power_level(struct device *d,
8249 struct device_attribute *attr, char *buf)
8250{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008251 struct iwl4965_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07008252 int level = IWL_POWER_LEVEL(priv->power_mode);
8253 char *p = buf;
8254
8255 p += sprintf(p, "%d ", level);
8256 switch (level) {
8257 case IWL_POWER_MODE_CAM:
8258 case IWL_POWER_AC:
8259 p += sprintf(p, "(AC)");
8260 break;
8261 case IWL_POWER_BATTERY:
8262 p += sprintf(p, "(BATTERY)");
8263 break;
8264 default:
8265 p += sprintf(p,
8266 "(Timeout %dms, Period %dms)",
8267 timeout_duration[level - 1] / 1000,
8268 period_duration[level - 1] / 1000);
8269 }
8270
8271 if (!(priv->power_mode & IWL_POWER_ENABLED))
8272 p += sprintf(p, " OFF\n");
8273 else
8274 p += sprintf(p, " \n");
8275
8276 return (p - buf + 1);
8277
8278}
8279
8280static DEVICE_ATTR(power_level, S_IWUSR | S_IRUSR, show_power_level,
8281 store_power_level);
8282
8283static ssize_t show_channels(struct device *d,
8284 struct device_attribute *attr, char *buf)
8285{
Johannes Berg8318d782008-01-24 19:38:38 +01008286 /* all this shit doesn't belong into sysfs anyway */
8287 return 0;
Zhu Yib481de92007-09-25 17:54:57 -07008288}
8289
8290static DEVICE_ATTR(channels, S_IRUSR, show_channels, NULL);
8291
8292static ssize_t show_statistics(struct device *d,
8293 struct device_attribute *attr, char *buf)
8294{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008295 struct iwl4965_priv *priv = dev_get_drvdata(d);
8296 u32 size = sizeof(struct iwl4965_notif_statistics);
Zhu Yib481de92007-09-25 17:54:57 -07008297 u32 len = 0, ofs = 0;
8298 u8 *data = (u8 *) & priv->statistics;
8299 int rc = 0;
8300
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008301 if (!iwl4965_is_alive(priv))
Zhu Yib481de92007-09-25 17:54:57 -07008302 return -EAGAIN;
8303
8304 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008305 rc = iwl4965_send_statistics_request(priv);
Zhu Yib481de92007-09-25 17:54:57 -07008306 mutex_unlock(&priv->mutex);
8307
8308 if (rc) {
8309 len = sprintf(buf,
8310 "Error sending statistics request: 0x%08X\n", rc);
8311 return len;
8312 }
8313
8314 while (size && (PAGE_SIZE - len)) {
8315 hex_dump_to_buffer(data + ofs, size, 16, 1, buf + len,
8316 PAGE_SIZE - len, 1);
8317 len = strlen(buf);
8318 if (PAGE_SIZE - len)
8319 buf[len++] = '\n';
8320
8321 ofs += 16;
8322 size -= min(size, 16U);
8323 }
8324
8325 return len;
8326}
8327
8328static DEVICE_ATTR(statistics, S_IRUGO, show_statistics, NULL);
8329
8330static ssize_t show_antenna(struct device *d,
8331 struct device_attribute *attr, char *buf)
8332{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008333 struct iwl4965_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07008334
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008335 if (!iwl4965_is_alive(priv))
Zhu Yib481de92007-09-25 17:54:57 -07008336 return -EAGAIN;
8337
8338 return sprintf(buf, "%d\n", priv->antenna);
8339}
8340
8341static ssize_t store_antenna(struct device *d,
8342 struct device_attribute *attr,
8343 const char *buf, size_t count)
8344{
8345 int ant;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008346 struct iwl4965_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07008347
8348 if (count == 0)
8349 return 0;
8350
8351 if (sscanf(buf, "%1i", &ant) != 1) {
8352 IWL_DEBUG_INFO("not in hex or decimal form.\n");
8353 return count;
8354 }
8355
8356 if ((ant >= 0) && (ant <= 2)) {
8357 IWL_DEBUG_INFO("Setting antenna select to %d.\n", ant);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008358 priv->antenna = (enum iwl4965_antenna)ant;
Zhu Yib481de92007-09-25 17:54:57 -07008359 } else
8360 IWL_DEBUG_INFO("Bad antenna select value %d.\n", ant);
8361
8362
8363 return count;
8364}
8365
8366static DEVICE_ATTR(antenna, S_IWUSR | S_IRUGO, show_antenna, store_antenna);
8367
8368static ssize_t show_status(struct device *d,
8369 struct device_attribute *attr, char *buf)
8370{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008371 struct iwl4965_priv *priv = (struct iwl4965_priv *)d->driver_data;
8372 if (!iwl4965_is_alive(priv))
Zhu Yib481de92007-09-25 17:54:57 -07008373 return -EAGAIN;
8374 return sprintf(buf, "0x%08x\n", (int)priv->status);
8375}
8376
8377static DEVICE_ATTR(status, S_IRUGO, show_status, NULL);
8378
8379static ssize_t dump_error_log(struct device *d,
8380 struct device_attribute *attr,
8381 const char *buf, size_t count)
8382{
8383 char *p = (char *)buf;
8384
8385 if (p[0] == '1')
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008386 iwl4965_dump_nic_error_log((struct iwl4965_priv *)d->driver_data);
Zhu Yib481de92007-09-25 17:54:57 -07008387
8388 return strnlen(buf, count);
8389}
8390
8391static DEVICE_ATTR(dump_errors, S_IWUSR, NULL, dump_error_log);
8392
8393static ssize_t dump_event_log(struct device *d,
8394 struct device_attribute *attr,
8395 const char *buf, size_t count)
8396{
8397 char *p = (char *)buf;
8398
8399 if (p[0] == '1')
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008400 iwl4965_dump_nic_event_log((struct iwl4965_priv *)d->driver_data);
Zhu Yib481de92007-09-25 17:54:57 -07008401
8402 return strnlen(buf, count);
8403}
8404
8405static DEVICE_ATTR(dump_events, S_IWUSR, NULL, dump_event_log);
8406
8407/*****************************************************************************
8408 *
8409 * driver setup and teardown
8410 *
8411 *****************************************************************************/
8412
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008413static void iwl4965_setup_deferred_work(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07008414{
8415 priv->workqueue = create_workqueue(DRV_NAME);
8416
8417 init_waitqueue_head(&priv->wait_command_queue);
8418
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008419 INIT_WORK(&priv->up, iwl4965_bg_up);
8420 INIT_WORK(&priv->restart, iwl4965_bg_restart);
8421 INIT_WORK(&priv->rx_replenish, iwl4965_bg_rx_replenish);
8422 INIT_WORK(&priv->scan_completed, iwl4965_bg_scan_completed);
8423 INIT_WORK(&priv->request_scan, iwl4965_bg_request_scan);
8424 INIT_WORK(&priv->abort_scan, iwl4965_bg_abort_scan);
8425 INIT_WORK(&priv->rf_kill, iwl4965_bg_rf_kill);
8426 INIT_WORK(&priv->beacon_update, iwl4965_bg_beacon_update);
8427 INIT_DELAYED_WORK(&priv->post_associate, iwl4965_bg_post_associate);
8428 INIT_DELAYED_WORK(&priv->init_alive_start, iwl4965_bg_init_alive_start);
8429 INIT_DELAYED_WORK(&priv->alive_start, iwl4965_bg_alive_start);
8430 INIT_DELAYED_WORK(&priv->scan_check, iwl4965_bg_scan_check);
Zhu Yib481de92007-09-25 17:54:57 -07008431
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008432 iwl4965_hw_setup_deferred_work(priv);
Zhu Yib481de92007-09-25 17:54:57 -07008433
8434 tasklet_init(&priv->irq_tasklet, (void (*)(unsigned long))
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008435 iwl4965_irq_tasklet, (unsigned long)priv);
Zhu Yib481de92007-09-25 17:54:57 -07008436}
8437
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008438static void iwl4965_cancel_deferred_work(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07008439{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008440 iwl4965_hw_cancel_deferred_work(priv);
Zhu Yib481de92007-09-25 17:54:57 -07008441
Joonwoo Park3ae6a052007-11-29 10:43:16 +09008442 cancel_delayed_work_sync(&priv->init_alive_start);
Zhu Yib481de92007-09-25 17:54:57 -07008443 cancel_delayed_work(&priv->scan_check);
8444 cancel_delayed_work(&priv->alive_start);
8445 cancel_delayed_work(&priv->post_associate);
8446 cancel_work_sync(&priv->beacon_update);
8447}
8448
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008449static struct attribute *iwl4965_sysfs_entries[] = {
Zhu Yib481de92007-09-25 17:54:57 -07008450 &dev_attr_antenna.attr,
8451 &dev_attr_channels.attr,
8452 &dev_attr_dump_errors.attr,
8453 &dev_attr_dump_events.attr,
8454 &dev_attr_flags.attr,
8455 &dev_attr_filter_flags.attr,
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08008456#ifdef CONFIG_IWL4965_SPECTRUM_MEASUREMENT
Zhu Yib481de92007-09-25 17:54:57 -07008457 &dev_attr_measurement.attr,
8458#endif
8459 &dev_attr_power_level.attr,
8460 &dev_attr_retry_rate.attr,
8461 &dev_attr_rf_kill.attr,
8462 &dev_attr_rs_window.attr,
8463 &dev_attr_statistics.attr,
8464 &dev_attr_status.attr,
8465 &dev_attr_temperature.attr,
Zhu Yib481de92007-09-25 17:54:57 -07008466 &dev_attr_tx_power.attr,
8467
8468 NULL
8469};
8470
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008471static struct attribute_group iwl4965_attribute_group = {
Zhu Yib481de92007-09-25 17:54:57 -07008472 .name = NULL, /* put in device directory */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008473 .attrs = iwl4965_sysfs_entries,
Zhu Yib481de92007-09-25 17:54:57 -07008474};
8475
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008476static struct ieee80211_ops iwl4965_hw_ops = {
8477 .tx = iwl4965_mac_tx,
8478 .start = iwl4965_mac_start,
8479 .stop = iwl4965_mac_stop,
8480 .add_interface = iwl4965_mac_add_interface,
8481 .remove_interface = iwl4965_mac_remove_interface,
8482 .config = iwl4965_mac_config,
8483 .config_interface = iwl4965_mac_config_interface,
8484 .configure_filter = iwl4965_configure_filter,
8485 .set_key = iwl4965_mac_set_key,
8486 .get_stats = iwl4965_mac_get_stats,
8487 .get_tx_stats = iwl4965_mac_get_tx_stats,
8488 .conf_tx = iwl4965_mac_conf_tx,
8489 .get_tsf = iwl4965_mac_get_tsf,
8490 .reset_tsf = iwl4965_mac_reset_tsf,
8491 .beacon_update = iwl4965_mac_beacon_update,
Johannes Berg471b3ef2007-12-28 14:32:58 +01008492 .bss_info_changed = iwl4965_bss_info_changed,
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08008493#ifdef CONFIG_IWL4965_HT
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008494 .conf_ht = iwl4965_mac_conf_ht,
Ron Rindjunsky9ab46172007-12-25 17:00:38 +02008495 .ampdu_action = iwl4965_mac_ampdu_action,
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08008496#endif /* CONFIG_IWL4965_HT */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008497 .hw_scan = iwl4965_mac_hw_scan
Zhu Yib481de92007-09-25 17:54:57 -07008498};
8499
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008500static int iwl4965_pci_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
Zhu Yib481de92007-09-25 17:54:57 -07008501{
8502 int err = 0;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008503 struct iwl4965_priv *priv;
Zhu Yib481de92007-09-25 17:54:57 -07008504 struct ieee80211_hw *hw;
Tomas Winkler82b9a122008-03-04 18:09:30 -08008505 struct iwl_cfg *cfg = (struct iwl_cfg *)(ent->driver_data);
Zhu Yib481de92007-09-25 17:54:57 -07008506 int i;
Zhu Yi5a66926a2008-01-14 17:46:18 -08008507 DECLARE_MAC_BUF(mac);
Zhu Yib481de92007-09-25 17:54:57 -07008508
Cahill, Ben M6440adb2007-11-29 11:09:55 +08008509 /* Disabling hardware scan means that mac80211 will perform scans
8510 * "the hard way", rather than using device's scan. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008511 if (iwl4965_param_disable_hw_scan) {
Zhu Yib481de92007-09-25 17:54:57 -07008512 IWL_DEBUG_INFO("Disabling hw_scan\n");
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008513 iwl4965_hw_ops.hw_scan = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07008514 }
8515
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008516 if ((iwl4965_param_queues_num > IWL_MAX_NUM_QUEUES) ||
8517 (iwl4965_param_queues_num < IWL_MIN_NUM_QUEUES)) {
Zhu Yib481de92007-09-25 17:54:57 -07008518 IWL_ERROR("invalid queues_num, should be between %d and %d\n",
8519 IWL_MIN_NUM_QUEUES, IWL_MAX_NUM_QUEUES);
8520 err = -EINVAL;
8521 goto out;
8522 }
8523
8524 /* mac80211 allocates memory for this device instance, including
8525 * space for this driver's private structure */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008526 hw = ieee80211_alloc_hw(sizeof(struct iwl4965_priv), &iwl4965_hw_ops);
Zhu Yib481de92007-09-25 17:54:57 -07008527 if (hw == NULL) {
8528 IWL_ERROR("Can not allocate network device\n");
8529 err = -ENOMEM;
8530 goto out;
8531 }
8532 SET_IEEE80211_DEV(hw, &pdev->dev);
8533
Johannes Bergf51359a2007-10-28 14:53:36 +01008534 hw->rate_control_algorithm = "iwl-4965-rs";
8535
Zhu Yib481de92007-09-25 17:54:57 -07008536 IWL_DEBUG_INFO("*** LOAD DRIVER ***\n");
8537 priv = hw->priv;
8538 priv->hw = hw;
Tomas Winkler82b9a122008-03-04 18:09:30 -08008539 priv->cfg = cfg;
Zhu Yib481de92007-09-25 17:54:57 -07008540
8541 priv->pci_dev = pdev;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008542 priv->antenna = (enum iwl4965_antenna)iwl4965_param_antenna;
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08008543#ifdef CONFIG_IWL4965_DEBUG
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008544 iwl4965_debug_level = iwl4965_param_debug;
Zhu Yib481de92007-09-25 17:54:57 -07008545 atomic_set(&priv->restrict_refcnt, 0);
8546#endif
8547 priv->retry_rate = 1;
8548
8549 priv->ibss_beacon = NULL;
8550
8551 /* Tell mac80211 and its clients (e.g. Wireless Extensions)
8552 * the range of signal quality values that we'll provide.
8553 * Negative values for level/noise indicate that we'll provide dBm.
8554 * For WE, at least, non-0 values here *enable* display of values
8555 * in app (iwconfig). */
8556 hw->max_rssi = -20; /* signal level, negative indicates dBm */
8557 hw->max_noise = -20; /* noise level, negative indicates dBm */
8558 hw->max_signal = 100; /* link quality indication (%) */
8559
8560 /* Tell mac80211 our Tx characteristics */
8561 hw->flags = IEEE80211_HW_HOST_GEN_BEACON_TEMPLATE;
8562
Cahill, Ben M6440adb2007-11-29 11:09:55 +08008563 /* Default value; 4 EDCA QOS priorities */
Zhu Yib481de92007-09-25 17:54:57 -07008564 hw->queues = 4;
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08008565#ifdef CONFIG_IWL4965_HT
Cahill, Ben M6440adb2007-11-29 11:09:55 +08008566 /* Enhanced value; more queues, to support 11n aggregation */
Zhu Yib481de92007-09-25 17:54:57 -07008567 hw->queues = 16;
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08008568#endif /* CONFIG_IWL4965_HT */
Zhu Yib481de92007-09-25 17:54:57 -07008569
8570 spin_lock_init(&priv->lock);
8571 spin_lock_init(&priv->power_data.lock);
8572 spin_lock_init(&priv->sta_lock);
8573 spin_lock_init(&priv->hcmd_lock);
8574 spin_lock_init(&priv->lq_mngr.lock);
8575
8576 for (i = 0; i < IWL_IBSS_MAC_HASH_SIZE; i++)
8577 INIT_LIST_HEAD(&priv->ibss_mac_hash[i]);
8578
8579 INIT_LIST_HEAD(&priv->free_frames);
8580
8581 mutex_init(&priv->mutex);
8582 if (pci_enable_device(pdev)) {
8583 err = -ENODEV;
8584 goto out_ieee80211_free_hw;
8585 }
8586
8587 pci_set_master(pdev);
8588
Cahill, Ben M6440adb2007-11-29 11:09:55 +08008589 /* Clear the driver's (not device's) station table */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008590 iwl4965_clear_stations_table(priv);
Zhu Yib481de92007-09-25 17:54:57 -07008591
8592 priv->data_retry_limit = -1;
8593 priv->ieee_channels = NULL;
8594 priv->ieee_rates = NULL;
Johannes Berg8318d782008-01-24 19:38:38 +01008595 priv->band = IEEE80211_BAND_2GHZ;
Zhu Yib481de92007-09-25 17:54:57 -07008596
8597 err = pci_set_dma_mask(pdev, DMA_32BIT_MASK);
8598 if (!err)
8599 err = pci_set_consistent_dma_mask(pdev, DMA_32BIT_MASK);
8600 if (err) {
8601 printk(KERN_WARNING DRV_NAME ": No suitable DMA available.\n");
8602 goto out_pci_disable_device;
8603 }
8604
8605 pci_set_drvdata(pdev, priv);
8606 err = pci_request_regions(pdev, DRV_NAME);
8607 if (err)
8608 goto out_pci_disable_device;
Cahill, Ben M6440adb2007-11-29 11:09:55 +08008609
Zhu Yib481de92007-09-25 17:54:57 -07008610 /* We disable the RETRY_TIMEOUT register (0x41) to keep
8611 * PCI Tx retries from interfering with C3 CPU state */
8612 pci_write_config_byte(pdev, 0x41, 0x00);
Cahill, Ben M6440adb2007-11-29 11:09:55 +08008613
Zhu Yib481de92007-09-25 17:54:57 -07008614 priv->hw_base = pci_iomap(pdev, 0, 0);
8615 if (!priv->hw_base) {
8616 err = -ENODEV;
8617 goto out_pci_release_regions;
8618 }
8619
8620 IWL_DEBUG_INFO("pci_resource_len = 0x%08llx\n",
8621 (unsigned long long) pci_resource_len(pdev, 0));
8622 IWL_DEBUG_INFO("pci_resource_base = %p\n", priv->hw_base);
8623
8624 /* Initialize module parameter values here */
8625
Cahill, Ben M6440adb2007-11-29 11:09:55 +08008626 /* Disable radio (SW RF KILL) via parameter when loading driver */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008627 if (iwl4965_param_disable) {
Zhu Yib481de92007-09-25 17:54:57 -07008628 set_bit(STATUS_RF_KILL_SW, &priv->status);
8629 IWL_DEBUG_INFO("Radio disabled.\n");
8630 }
8631
8632 priv->iw_mode = IEEE80211_IF_TYPE_STA;
8633
8634 priv->ps_mode = 0;
8635 priv->use_ant_b_for_management_frame = 1; /* start with ant B */
Zhu Yib481de92007-09-25 17:54:57 -07008636 priv->valid_antenna = 0x7; /* assume all 3 connected */
8637 priv->ps_mode = IWL_MIMO_PS_NONE;
Zhu Yib481de92007-09-25 17:54:57 -07008638
Cahill, Ben M6440adb2007-11-29 11:09:55 +08008639 /* Choose which receivers/antennas to use */
Zhu Yib481de92007-09-25 17:54:57 -07008640 iwl4965_set_rxon_chain(priv);
8641
Tomas Winkler82b9a122008-03-04 18:09:30 -08008642
Zhu Yib481de92007-09-25 17:54:57 -07008643 printk(KERN_INFO DRV_NAME
Tomas Winkler82b9a122008-03-04 18:09:30 -08008644 ": Detected Intel Wireless WiFi Link %s\n", priv->cfg->name);
Zhu Yib481de92007-09-25 17:54:57 -07008645
8646 /* Device-specific setup */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008647 if (iwl4965_hw_set_hw_setting(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07008648 IWL_ERROR("failed to set hw settings\n");
Zhu Yib481de92007-09-25 17:54:57 -07008649 goto out_iounmap;
8650 }
8651
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008652 if (iwl4965_param_qos_enable)
Zhu Yib481de92007-09-25 17:54:57 -07008653 priv->qos_data.qos_enable = 1;
8654
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008655 iwl4965_reset_qos(priv);
Zhu Yib481de92007-09-25 17:54:57 -07008656
8657 priv->qos_data.qos_active = 0;
8658 priv->qos_data.qos_cap.val = 0;
Zhu Yib481de92007-09-25 17:54:57 -07008659
Johannes Berg8318d782008-01-24 19:38:38 +01008660 iwl4965_set_rxon_channel(priv, IEEE80211_BAND_2GHZ, 6);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008661 iwl4965_setup_deferred_work(priv);
8662 iwl4965_setup_rx_handlers(priv);
Zhu Yib481de92007-09-25 17:54:57 -07008663
8664 priv->rates_mask = IWL_RATES_MASK;
8665 /* If power management is turned on, default to AC mode */
8666 priv->power_mode = IWL_POWER_AC;
8667 priv->user_txpower_limit = IWL_DEFAULT_TX_POWER;
8668
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008669 iwl4965_disable_interrupts(priv);
Jes Sorensen49df2b32007-10-26 16:10:39 +02008670
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008671 err = sysfs_create_group(&pdev->dev.kobj, &iwl4965_attribute_group);
Zhu Yib481de92007-09-25 17:54:57 -07008672 if (err) {
8673 IWL_ERROR("failed to create sysfs device attributes\n");
Zhu Yib481de92007-09-25 17:54:57 -07008674 goto out_release_irq;
8675 }
8676
Zhu Yi5a66926a2008-01-14 17:46:18 -08008677 /* nic init */
8678 iwl4965_set_bit(priv, CSR_GIO_CHICKEN_BITS,
8679 CSR_GIO_CHICKEN_BITS_REG_BIT_DIS_L0S_EXIT_TIMER);
8680
8681 iwl4965_set_bit(priv, CSR_GP_CNTRL, CSR_GP_CNTRL_REG_FLAG_INIT_DONE);
8682 err = iwl4965_poll_bit(priv, CSR_GP_CNTRL,
8683 CSR_GP_CNTRL_REG_FLAG_MAC_CLOCK_READY,
8684 CSR_GP_CNTRL_REG_FLAG_MAC_CLOCK_READY, 25000);
8685 if (err < 0) {
8686 IWL_DEBUG_INFO("Failed to init the card\n");
8687 goto out_remove_sysfs;
8688 }
8689 /* Read the EEPROM */
Assaf Krauss6bc913b2008-03-11 16:17:18 -07008690 err = iwl_eeprom_init(priv);
Zhu Yib481de92007-09-25 17:54:57 -07008691 if (err) {
Zhu Yi5a66926a2008-01-14 17:46:18 -08008692 IWL_ERROR("Unable to init EEPROM\n");
8693 goto out_remove_sysfs;
8694 }
8695 /* MAC Address location in EEPROM same for 3945/4965 */
Assaf Krauss6bc913b2008-03-11 16:17:18 -07008696 iwl_eeprom_get_mac(priv, priv->mac_addr);
Zhu Yi5a66926a2008-01-14 17:46:18 -08008697 IWL_DEBUG_INFO("MAC address: %s\n", print_mac(mac, priv->mac_addr));
8698 SET_IEEE80211_PERM_ADDR(priv->hw, priv->mac_addr);
8699
Reinette Chatre849e0dc2008-01-23 10:15:18 -08008700 err = iwl4965_init_channel_map(priv);
8701 if (err) {
8702 IWL_ERROR("initializing regulatory failed: %d\n", err);
8703 goto out_remove_sysfs;
8704 }
8705
8706 err = iwl4965_init_geos(priv);
8707 if (err) {
8708 IWL_ERROR("initializing geos failed: %d\n", err);
8709 goto out_free_channel_map;
8710 }
Reinette Chatre849e0dc2008-01-23 10:15:18 -08008711
Zhu Yi5a66926a2008-01-14 17:46:18 -08008712 iwl4965_rate_control_register(priv->hw);
8713 err = ieee80211_register_hw(priv->hw);
8714 if (err) {
8715 IWL_ERROR("Failed to register network device (error %d)\n", err);
Reinette Chatre849e0dc2008-01-23 10:15:18 -08008716 goto out_free_geos;
Zhu Yib481de92007-09-25 17:54:57 -07008717 }
8718
Zhu Yi5a66926a2008-01-14 17:46:18 -08008719 priv->hw->conf.beacon_int = 100;
8720 priv->mac80211_registered = 1;
8721 pci_save_state(pdev);
8722 pci_disable_device(pdev);
Zhu Yib481de92007-09-25 17:54:57 -07008723
8724 return 0;
8725
Reinette Chatre849e0dc2008-01-23 10:15:18 -08008726 out_free_geos:
8727 iwl4965_free_geos(priv);
8728 out_free_channel_map:
8729 iwl4965_free_channel_map(priv);
Zhu Yi5a66926a2008-01-14 17:46:18 -08008730 out_remove_sysfs:
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008731 sysfs_remove_group(&pdev->dev.kobj, &iwl4965_attribute_group);
Zhu Yib481de92007-09-25 17:54:57 -07008732
8733 out_release_irq:
Zhu Yib481de92007-09-25 17:54:57 -07008734 destroy_workqueue(priv->workqueue);
8735 priv->workqueue = NULL;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008736 iwl4965_unset_hw_setting(priv);
Zhu Yib481de92007-09-25 17:54:57 -07008737
8738 out_iounmap:
8739 pci_iounmap(pdev, priv->hw_base);
8740 out_pci_release_regions:
8741 pci_release_regions(pdev);
8742 out_pci_disable_device:
8743 pci_disable_device(pdev);
8744 pci_set_drvdata(pdev, NULL);
8745 out_ieee80211_free_hw:
8746 ieee80211_free_hw(priv->hw);
8747 out:
8748 return err;
8749}
8750
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008751static void iwl4965_pci_remove(struct pci_dev *pdev)
Zhu Yib481de92007-09-25 17:54:57 -07008752{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008753 struct iwl4965_priv *priv = pci_get_drvdata(pdev);
Zhu Yib481de92007-09-25 17:54:57 -07008754 struct list_head *p, *q;
8755 int i;
8756
8757 if (!priv)
8758 return;
8759
8760 IWL_DEBUG_INFO("*** UNLOAD DRIVER ***\n");
8761
Zhu Yib481de92007-09-25 17:54:57 -07008762 set_bit(STATUS_EXIT_PENDING, &priv->status);
Zhu Yib24d22b2007-12-19 13:59:52 +08008763
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008764 iwl4965_down(priv);
Zhu Yib481de92007-09-25 17:54:57 -07008765
8766 /* Free MAC hash list for ADHOC */
8767 for (i = 0; i < IWL_IBSS_MAC_HASH_SIZE; i++) {
8768 list_for_each_safe(p, q, &priv->ibss_mac_hash[i]) {
8769 list_del(p);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008770 kfree(list_entry(p, struct iwl4965_ibss_seq, list));
Zhu Yib481de92007-09-25 17:54:57 -07008771 }
8772 }
8773
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008774 sysfs_remove_group(&pdev->dev.kobj, &iwl4965_attribute_group);
Zhu Yib481de92007-09-25 17:54:57 -07008775
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008776 iwl4965_dealloc_ucode_pci(priv);
Zhu Yib481de92007-09-25 17:54:57 -07008777
8778 if (priv->rxq.bd)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008779 iwl4965_rx_queue_free(priv, &priv->rxq);
8780 iwl4965_hw_txq_ctx_free(priv);
Zhu Yib481de92007-09-25 17:54:57 -07008781
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008782 iwl4965_unset_hw_setting(priv);
8783 iwl4965_clear_stations_table(priv);
Zhu Yib481de92007-09-25 17:54:57 -07008784
8785 if (priv->mac80211_registered) {
8786 ieee80211_unregister_hw(priv->hw);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008787 iwl4965_rate_control_unregister(priv->hw);
Zhu Yib481de92007-09-25 17:54:57 -07008788 }
8789
Mohamed Abbas948c1712007-10-25 17:15:45 +08008790 /*netif_stop_queue(dev); */
8791 flush_workqueue(priv->workqueue);
8792
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008793 /* ieee80211_unregister_hw calls iwl4965_mac_stop, which flushes
Zhu Yib481de92007-09-25 17:54:57 -07008794 * priv->workqueue... so we can't take down the workqueue
8795 * until now... */
8796 destroy_workqueue(priv->workqueue);
8797 priv->workqueue = NULL;
8798
Zhu Yib481de92007-09-25 17:54:57 -07008799 pci_iounmap(pdev, priv->hw_base);
8800 pci_release_regions(pdev);
8801 pci_disable_device(pdev);
8802 pci_set_drvdata(pdev, NULL);
8803
Reinette Chatre849e0dc2008-01-23 10:15:18 -08008804 iwl4965_free_channel_map(priv);
8805 iwl4965_free_geos(priv);
Zhu Yib481de92007-09-25 17:54:57 -07008806
8807 if (priv->ibss_beacon)
8808 dev_kfree_skb(priv->ibss_beacon);
8809
8810 ieee80211_free_hw(priv->hw);
8811}
8812
8813#ifdef CONFIG_PM
8814
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008815static int iwl4965_pci_suspend(struct pci_dev *pdev, pm_message_t state)
Zhu Yib481de92007-09-25 17:54:57 -07008816{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008817 struct iwl4965_priv *priv = pci_get_drvdata(pdev);
Zhu Yib481de92007-09-25 17:54:57 -07008818
Zhu Yie655b9f2008-01-24 02:19:38 -08008819 if (priv->is_open) {
8820 set_bit(STATUS_IN_SUSPEND, &priv->status);
8821 iwl4965_mac_stop(priv->hw);
8822 priv->is_open = 1;
8823 }
Zhu Yib481de92007-09-25 17:54:57 -07008824
Zhu Yib481de92007-09-25 17:54:57 -07008825 pci_set_power_state(pdev, PCI_D3hot);
8826
Zhu Yib481de92007-09-25 17:54:57 -07008827 return 0;
8828}
8829
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008830static int iwl4965_pci_resume(struct pci_dev *pdev)
Zhu Yib481de92007-09-25 17:54:57 -07008831{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008832 struct iwl4965_priv *priv = pci_get_drvdata(pdev);
Zhu Yib481de92007-09-25 17:54:57 -07008833
Zhu Yib481de92007-09-25 17:54:57 -07008834 pci_set_power_state(pdev, PCI_D0);
Zhu Yib481de92007-09-25 17:54:57 -07008835
Zhu Yie655b9f2008-01-24 02:19:38 -08008836 if (priv->is_open)
8837 iwl4965_mac_start(priv->hw);
Zhu Yib481de92007-09-25 17:54:57 -07008838
Zhu Yie655b9f2008-01-24 02:19:38 -08008839 clear_bit(STATUS_IN_SUSPEND, &priv->status);
Zhu Yib481de92007-09-25 17:54:57 -07008840 return 0;
8841}
8842
8843#endif /* CONFIG_PM */
8844
8845/*****************************************************************************
8846 *
8847 * driver and module entry point
8848 *
8849 *****************************************************************************/
8850
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008851static struct pci_driver iwl4965_driver = {
Zhu Yib481de92007-09-25 17:54:57 -07008852 .name = DRV_NAME,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008853 .id_table = iwl4965_hw_card_ids,
8854 .probe = iwl4965_pci_probe,
8855 .remove = __devexit_p(iwl4965_pci_remove),
Zhu Yib481de92007-09-25 17:54:57 -07008856#ifdef CONFIG_PM
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008857 .suspend = iwl4965_pci_suspend,
8858 .resume = iwl4965_pci_resume,
Zhu Yib481de92007-09-25 17:54:57 -07008859#endif
8860};
8861
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008862static int __init iwl4965_init(void)
Zhu Yib481de92007-09-25 17:54:57 -07008863{
8864
8865 int ret;
8866 printk(KERN_INFO DRV_NAME ": " DRV_DESCRIPTION ", " DRV_VERSION "\n");
8867 printk(KERN_INFO DRV_NAME ": " DRV_COPYRIGHT "\n");
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008868 ret = pci_register_driver(&iwl4965_driver);
Zhu Yib481de92007-09-25 17:54:57 -07008869 if (ret) {
8870 IWL_ERROR("Unable to initialize PCI module\n");
8871 return ret;
8872 }
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08008873#ifdef CONFIG_IWL4965_DEBUG
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008874 ret = driver_create_file(&iwl4965_driver.driver, &driver_attr_debug_level);
Zhu Yib481de92007-09-25 17:54:57 -07008875 if (ret) {
8876 IWL_ERROR("Unable to create driver sysfs file\n");
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008877 pci_unregister_driver(&iwl4965_driver);
Zhu Yib481de92007-09-25 17:54:57 -07008878 return ret;
8879 }
8880#endif
8881
8882 return ret;
8883}
8884
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008885static void __exit iwl4965_exit(void)
Zhu Yib481de92007-09-25 17:54:57 -07008886{
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08008887#ifdef CONFIG_IWL4965_DEBUG
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008888 driver_remove_file(&iwl4965_driver.driver, &driver_attr_debug_level);
Zhu Yib481de92007-09-25 17:54:57 -07008889#endif
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008890 pci_unregister_driver(&iwl4965_driver);
Zhu Yib481de92007-09-25 17:54:57 -07008891}
8892
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008893module_param_named(antenna, iwl4965_param_antenna, int, 0444);
Zhu Yib481de92007-09-25 17:54:57 -07008894MODULE_PARM_DESC(antenna, "select antenna (1=Main, 2=Aux, default 0 [both])");
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008895module_param_named(disable, iwl4965_param_disable, int, 0444);
Zhu Yib481de92007-09-25 17:54:57 -07008896MODULE_PARM_DESC(disable, "manually disable the radio (default 0 [radio on])");
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008897module_param_named(hwcrypto, iwl4965_param_hwcrypto, int, 0444);
Zhu Yib481de92007-09-25 17:54:57 -07008898MODULE_PARM_DESC(hwcrypto,
8899 "using hardware crypto engine (default 0 [software])\n");
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008900module_param_named(debug, iwl4965_param_debug, int, 0444);
Zhu Yib481de92007-09-25 17:54:57 -07008901MODULE_PARM_DESC(debug, "debug output mask");
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008902module_param_named(disable_hw_scan, iwl4965_param_disable_hw_scan, int, 0444);
Zhu Yib481de92007-09-25 17:54:57 -07008903MODULE_PARM_DESC(disable_hw_scan, "disable hardware scanning (default 0)");
8904
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008905module_param_named(queues_num, iwl4965_param_queues_num, int, 0444);
Zhu Yib481de92007-09-25 17:54:57 -07008906MODULE_PARM_DESC(queues_num, "number of hw queues.");
8907
8908/* QoS */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008909module_param_named(qos_enable, iwl4965_param_qos_enable, int, 0444);
Zhu Yib481de92007-09-25 17:54:57 -07008910MODULE_PARM_DESC(qos_enable, "enable all QoS functionality");
Ron Rindjunsky9ee1ba42007-11-26 16:14:42 +02008911module_param_named(amsdu_size_8K, iwl4965_param_amsdu_size_8K, int, 0444);
8912MODULE_PARM_DESC(amsdu_size_8K, "enable 8K amsdu size");
Zhu Yib481de92007-09-25 17:54:57 -07008913
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008914module_exit(iwl4965_exit);
8915module_init(iwl4965_init);