blob: 541cdbe8172f7d3ad4ec25c292ae6993e3561ff3 [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:
Winkler, Tomas759ef892008-12-09 11:28:58 -080025 * Intel Linux Wireless <ilw@linux.intel.com>
Zhu Yib481de92007-09-25 17:54:57 -070026 * 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>
Zhu Yib481de92007-09-25 17:54:57 -070032#include <linux/init.h>
33#include <linux/pci.h>
34#include <linux/dma-mapping.h>
35#include <linux/delay.h>
36#include <linux/skbuff.h>
37#include <linux/netdevice.h>
38#include <linux/wireless.h>
39#include <linux/firmware.h>
Zhu Yib481de92007-09-25 17:54:57 -070040#include <linux/etherdevice.h>
41#include <linux/if_arp.h>
42
43#include <net/ieee80211_radiotap.h>
John W. Linville7e272fc2008-09-24 18:13:14 -040044#include <net/lib80211.h>
Zhu Yib481de92007-09-25 17:54:57 -070045#include <net/mac80211.h>
46
47#include <asm/div64.h>
48
Tomas Winkler82b9a122008-03-04 18:09:30 -080049#include "iwl-3945-core.h"
Tomas Winkler69d00d22008-12-19 10:37:02 +080050#include "iwl-3945-commands.h"
Zhu Yib481de92007-09-25 17:54:57 -070051#include "iwl-3945.h"
Tomas Winklerbddadf82008-12-19 10:37:01 +080052#include "iwl-3945-fh.h"
Zhu Yib481de92007-09-25 17:54:57 -070053#include "iwl-helpers.h"
54
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +080055#ifdef CONFIG_IWL3945_DEBUG
Christoph Hellwigbb8c0932008-01-27 16:41:47 -080056u32 iwl3945_debug_level;
Zhu Yib481de92007-09-25 17:54:57 -070057#endif
58
Christoph Hellwigbb8c0932008-01-27 16:41:47 -080059static int iwl3945_tx_queue_update_write_ptr(struct iwl3945_priv *priv,
60 struct iwl3945_tx_queue *txq);
Christoph Hellwig416e1432007-10-25 17:15:49 +080061
Zhu Yib481de92007-09-25 17:54:57 -070062/******************************************************************************
63 *
64 * module boiler plate
65 *
66 ******************************************************************************/
67
68/* module parameters */
Cahill, Ben M6440adb2007-11-29 11:09:55 +080069static int iwl3945_param_disable_hw_scan; /* def: 0 = use 3945's h/w scan */
Wu, Fengguang95aa1942008-12-17 16:52:30 +080070static u32 iwl3945_param_debug; /* def: 0 = minimal debug log messages */
Cahill, Ben M6440adb2007-11-29 11:09:55 +080071static int iwl3945_param_disable; /* def: 0 = enable radio */
Ben Cahill9fbab512007-11-29 11:09:47 +080072static int iwl3945_param_antenna; /* def: 0 = both antennas (use diversity) */
Cahill, Ben M6440adb2007-11-29 11:09:55 +080073int iwl3945_param_hwcrypto; /* def: 0 = use software encryption */
Ron Rindjunskydfe7d452008-04-15 16:01:45 -070074int iwl3945_param_queues_num = IWL39_MAX_NUM_QUEUES; /* def: 8 Tx queues */
Zhu Yib481de92007-09-25 17:54:57 -070075
76/*
77 * module name, copyright, version, etc.
78 * NOTE: DRV_NAME is defined in iwlwifi.h for use by iwl-debug.h and printk
79 */
80
81#define DRV_DESCRIPTION \
82"Intel(R) PRO/Wireless 3945ABG/BG Network Connection driver for Linux"
83
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +080084#ifdef CONFIG_IWL3945_DEBUG
Zhu Yib481de92007-09-25 17:54:57 -070085#define VD "d"
86#else
87#define VD
88#endif
89
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +080090#ifdef CONFIG_IWL3945_SPECTRUM_MEASUREMENT
Zhu Yib481de92007-09-25 17:54:57 -070091#define VS "s"
92#else
93#define VS
94#endif
95
Reinette Chatreb9e0b442008-02-08 16:39:11 -080096#define IWLWIFI_VERSION "1.2.26k" VD VS
Reinette Chatreeb7ae892008-03-11 16:17:17 -070097#define DRV_COPYRIGHT "Copyright(c) 2003-2008 Intel Corporation"
Tomas Winklera7b75202008-12-11 10:33:41 -080098#define DRV_AUTHOR "<ilw@linux.intel.com>"
Zhu Yib481de92007-09-25 17:54:57 -070099#define DRV_VERSION IWLWIFI_VERSION
100
Zhu Yib481de92007-09-25 17:54:57 -0700101
102MODULE_DESCRIPTION(DRV_DESCRIPTION);
103MODULE_VERSION(DRV_VERSION);
Tomas Winklera7b75202008-12-11 10:33:41 -0800104MODULE_AUTHOR(DRV_COPYRIGHT " " DRV_AUTHOR);
Zhu Yib481de92007-09-25 17:54:57 -0700105MODULE_LICENSE("GPL");
106
Johannes Berg8318d782008-01-24 19:38:38 +0100107static const struct ieee80211_supported_band *iwl3945_get_band(
108 struct iwl3945_priv *priv, enum ieee80211_band band)
Zhu Yib481de92007-09-25 17:54:57 -0700109{
Johannes Berg8318d782008-01-24 19:38:38 +0100110 return priv->hw->wiphy->bands[band];
Zhu Yib481de92007-09-25 17:54:57 -0700111}
112
Zhu Yib481de92007-09-25 17:54:57 -0700113/*************** DMA-QUEUE-GENERAL-FUNCTIONS *****
114 * DMA services
115 *
116 * Theory of operation
117 *
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800118 * A Tx or Rx queue resides in host DRAM, and is comprised of a circular buffer
119 * of buffer descriptors, each of which points to one or more data buffers for
120 * the device to read from or fill. Driver and device exchange status of each
121 * queue via "read" and "write" pointers. Driver keeps minimum of 2 empty
122 * entries in each circular buffer, to protect against confusing empty and full
123 * queue states.
124 *
125 * The device reads or writes the data in the queues via the device's several
126 * DMA/FIFO channels. Each queue is mapped to a single DMA channel.
Zhu Yib481de92007-09-25 17:54:57 -0700127 *
128 * For Tx queue, there are low mark and high mark limits. If, after queuing
129 * the packet for Tx, free space become < low mark, Tx queue stopped. When
130 * reclaiming packets (on 'tx done IRQ), if free space become > high mark,
131 * Tx queue resumed.
132 *
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800133 * The 3945 operates with six queues: One receive queue, one transmit queue
134 * (#4) for sending commands to the device firmware, and four transmit queues
135 * (#0-3) for data tx via EDCA. An additional 2 HCCA queues are unused.
Zhu Yib481de92007-09-25 17:54:57 -0700136 ***************************************************/
137
Tomas Winklerc54b6792008-03-06 17:36:53 -0800138int iwl3945_queue_space(const struct iwl3945_queue *q)
Zhu Yib481de92007-09-25 17:54:57 -0700139{
Tomas Winklerfc4b6852007-10-25 17:15:24 +0800140 int s = q->read_ptr - q->write_ptr;
Zhu Yib481de92007-09-25 17:54:57 -0700141
Tomas Winklerfc4b6852007-10-25 17:15:24 +0800142 if (q->read_ptr > q->write_ptr)
Zhu Yib481de92007-09-25 17:54:57 -0700143 s -= q->n_bd;
144
145 if (s <= 0)
146 s += q->n_window;
147 /* keep some reserve to not confuse empty and full situations */
148 s -= 2;
149 if (s < 0)
150 s = 0;
151 return s;
152}
153
Tomas Winklerc54b6792008-03-06 17:36:53 -0800154int iwl3945_x2_queue_used(const struct iwl3945_queue *q, int i)
Zhu Yib481de92007-09-25 17:54:57 -0700155{
Tomas Winklerfc4b6852007-10-25 17:15:24 +0800156 return q->write_ptr > q->read_ptr ?
157 (i >= q->read_ptr && i < q->write_ptr) :
158 !(i < q->read_ptr && i >= q->write_ptr);
Zhu Yib481de92007-09-25 17:54:57 -0700159}
160
Tomas Winklerc54b6792008-03-06 17:36:53 -0800161
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800162static inline u8 get_cmd_index(struct iwl3945_queue *q, u32 index, int is_huge)
Zhu Yib481de92007-09-25 17:54:57 -0700163{
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800164 /* This is for scan command, the big buffer at end of command array */
Zhu Yib481de92007-09-25 17:54:57 -0700165 if (is_huge)
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800166 return q->n_window; /* must be power of 2 */
Zhu Yib481de92007-09-25 17:54:57 -0700167
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800168 /* Otherwise, use normal size buffers */
Zhu Yib481de92007-09-25 17:54:57 -0700169 return index & (q->n_window - 1);
170}
171
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800172/**
173 * iwl3945_queue_init - Initialize queue's high/low-water and read/write indexes
174 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800175static int iwl3945_queue_init(struct iwl3945_priv *priv, struct iwl3945_queue *q,
Zhu Yib481de92007-09-25 17:54:57 -0700176 int count, int slots_num, u32 id)
177{
178 q->n_bd = count;
179 q->n_window = slots_num;
180 q->id = id;
181
Tomas Winklerc54b6792008-03-06 17:36:53 -0800182 /* count must be power-of-two size, otherwise iwl_queue_inc_wrap
183 * and iwl_queue_dec_wrap are broken. */
Zhu Yib481de92007-09-25 17:54:57 -0700184 BUG_ON(!is_power_of_2(count));
185
186 /* slots_num must be power-of-two size, otherwise
187 * get_cmd_index is broken. */
188 BUG_ON(!is_power_of_2(slots_num));
189
190 q->low_mark = q->n_window / 4;
191 if (q->low_mark < 4)
192 q->low_mark = 4;
193
194 q->high_mark = q->n_window / 8;
195 if (q->high_mark < 2)
196 q->high_mark = 2;
197
Tomas Winklerfc4b6852007-10-25 17:15:24 +0800198 q->write_ptr = q->read_ptr = 0;
Zhu Yib481de92007-09-25 17:54:57 -0700199
200 return 0;
201}
202
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800203/**
204 * iwl3945_tx_queue_alloc - Alloc driver data and TFD CB for one Tx/cmd queue
205 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800206static int iwl3945_tx_queue_alloc(struct iwl3945_priv *priv,
207 struct iwl3945_tx_queue *txq, u32 id)
Zhu Yib481de92007-09-25 17:54:57 -0700208{
209 struct pci_dev *dev = priv->pci_dev;
210
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800211 /* Driver private data, only for Tx (not command) queues,
212 * not shared with device. */
Zhu Yib481de92007-09-25 17:54:57 -0700213 if (id != IWL_CMD_QUEUE_NUM) {
214 txq->txb = kmalloc(sizeof(txq->txb[0]) *
215 TFD_QUEUE_SIZE_MAX, GFP_KERNEL);
216 if (!txq->txb) {
Ian Schram01ebd062007-10-25 17:15:22 +0800217 IWL_ERROR("kmalloc for auxiliary BD "
Zhu Yib481de92007-09-25 17:54:57 -0700218 "structures failed\n");
219 goto error;
220 }
221 } else
222 txq->txb = NULL;
223
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800224 /* Circular buffer of transmit frame descriptors (TFDs),
225 * shared with device */
Zhu Yib481de92007-09-25 17:54:57 -0700226 txq->bd = pci_alloc_consistent(dev,
227 sizeof(txq->bd[0]) * TFD_QUEUE_SIZE_MAX,
228 &txq->q.dma_addr);
229
230 if (!txq->bd) {
231 IWL_ERROR("pci_alloc_consistent(%zd) failed\n",
232 sizeof(txq->bd[0]) * TFD_QUEUE_SIZE_MAX);
233 goto error;
234 }
235 txq->q.id = id;
236
237 return 0;
238
239 error:
Tomas Winkler3ac7f142008-07-21 02:40:14 +0300240 kfree(txq->txb);
241 txq->txb = NULL;
Zhu Yib481de92007-09-25 17:54:57 -0700242
243 return -ENOMEM;
244}
245
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800246/**
247 * iwl3945_tx_queue_init - Allocate and initialize one tx/cmd queue
248 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800249int iwl3945_tx_queue_init(struct iwl3945_priv *priv,
250 struct iwl3945_tx_queue *txq, int slots_num, u32 txq_id)
Zhu Yib481de92007-09-25 17:54:57 -0700251{
252 struct pci_dev *dev = priv->pci_dev;
253 int len;
254 int rc = 0;
255
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800256 /*
257 * Alloc buffer array for commands (Tx or other types of commands).
258 * For the command queue (#4), allocate command space + one big
259 * command for scan, since scan command is very huge; the system will
260 * not have two scans at the same time, so only one is needed.
261 * For data Tx queues (all other queues), no super-size command
262 * space is needed.
263 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800264 len = sizeof(struct iwl3945_cmd) * slots_num;
Zhu Yib481de92007-09-25 17:54:57 -0700265 if (txq_id == IWL_CMD_QUEUE_NUM)
266 len += IWL_MAX_SCAN_SIZE;
267 txq->cmd = pci_alloc_consistent(dev, len, &txq->dma_addr_cmd);
268 if (!txq->cmd)
269 return -ENOMEM;
270
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800271 /* Alloc driver data array and TFD circular buffer */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800272 rc = iwl3945_tx_queue_alloc(priv, txq, txq_id);
Zhu Yib481de92007-09-25 17:54:57 -0700273 if (rc) {
274 pci_free_consistent(dev, len, txq->cmd, txq->dma_addr_cmd);
275
276 return -ENOMEM;
277 }
278 txq->need_update = 0;
279
280 /* TFD_QUEUE_SIZE_MAX must be power-of-two size, otherwise
Tomas Winklerc54b6792008-03-06 17:36:53 -0800281 * iwl_queue_inc_wrap and iwl_queue_dec_wrap are broken. */
Zhu Yib481de92007-09-25 17:54:57 -0700282 BUILD_BUG_ON(TFD_QUEUE_SIZE_MAX & (TFD_QUEUE_SIZE_MAX - 1));
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800283
284 /* Initialize queue high/low-water, head/tail indexes */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800285 iwl3945_queue_init(priv, &txq->q, TFD_QUEUE_SIZE_MAX, slots_num, txq_id);
Zhu Yib481de92007-09-25 17:54:57 -0700286
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800287 /* Tell device where to find queue, enable DMA channel. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800288 iwl3945_hw_tx_queue_init(priv, txq);
Zhu Yib481de92007-09-25 17:54:57 -0700289
290 return 0;
291}
292
293/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800294 * iwl3945_tx_queue_free - Deallocate DMA queue.
Zhu Yib481de92007-09-25 17:54:57 -0700295 * @txq: Transmit queue to deallocate.
296 *
297 * Empty queue by removing and destroying all BD's.
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800298 * Free all buffers.
299 * 0-fill, but do not free "txq" descriptor structure.
Zhu Yib481de92007-09-25 17:54:57 -0700300 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800301void iwl3945_tx_queue_free(struct iwl3945_priv *priv, struct iwl3945_tx_queue *txq)
Zhu Yib481de92007-09-25 17:54:57 -0700302{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800303 struct iwl3945_queue *q = &txq->q;
Zhu Yib481de92007-09-25 17:54:57 -0700304 struct pci_dev *dev = priv->pci_dev;
305 int len;
306
307 if (q->n_bd == 0)
308 return;
309
310 /* first, empty all BD's */
Tomas Winklerfc4b6852007-10-25 17:15:24 +0800311 for (; q->write_ptr != q->read_ptr;
Tomas Winklerc54b6792008-03-06 17:36:53 -0800312 q->read_ptr = iwl_queue_inc_wrap(q->read_ptr, q->n_bd))
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800313 iwl3945_hw_txq_free_tfd(priv, txq);
Zhu Yib481de92007-09-25 17:54:57 -0700314
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800315 len = sizeof(struct iwl3945_cmd) * q->n_window;
Zhu Yib481de92007-09-25 17:54:57 -0700316 if (q->id == IWL_CMD_QUEUE_NUM)
317 len += IWL_MAX_SCAN_SIZE;
318
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800319 /* De-alloc array of command/tx buffers */
Zhu Yib481de92007-09-25 17:54:57 -0700320 pci_free_consistent(dev, len, txq->cmd, txq->dma_addr_cmd);
321
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800322 /* De-alloc circular buffer of TFDs */
Zhu Yib481de92007-09-25 17:54:57 -0700323 if (txq->q.n_bd)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800324 pci_free_consistent(dev, sizeof(struct iwl3945_tfd_frame) *
Zhu Yib481de92007-09-25 17:54:57 -0700325 txq->q.n_bd, txq->bd, txq->q.dma_addr);
326
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800327 /* De-alloc array of per-TFD driver data */
Tomas Winkler3ac7f142008-07-21 02:40:14 +0300328 kfree(txq->txb);
329 txq->txb = NULL;
Zhu Yib481de92007-09-25 17:54:57 -0700330
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800331 /* 0-fill queue descriptor structure */
Zhu Yib481de92007-09-25 17:54:57 -0700332 memset(txq, 0, sizeof(*txq));
333}
334
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800335const u8 iwl3945_broadcast_addr[ETH_ALEN] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
Zhu Yib481de92007-09-25 17:54:57 -0700336
337/*************** STATION TABLE MANAGEMENT ****
Ben Cahill9fbab512007-11-29 11:09:47 +0800338 * mac80211 should be examined to determine if sta_info is duplicating
Zhu Yib481de92007-09-25 17:54:57 -0700339 * the functionality provided here
340 */
341
342/**************************************************************/
Ian Schram01ebd062007-10-25 17:15:22 +0800343#if 0 /* temporary disable till we add real remove station */
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800344/**
345 * iwl3945_remove_station - Remove driver's knowledge of station.
346 *
347 * NOTE: This does not remove station from device's station table.
348 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800349static u8 iwl3945_remove_station(struct iwl3945_priv *priv, const u8 *addr, int is_ap)
Zhu Yib481de92007-09-25 17:54:57 -0700350{
351 int index = IWL_INVALID_STATION;
352 int i;
353 unsigned long flags;
354
355 spin_lock_irqsave(&priv->sta_lock, flags);
356
357 if (is_ap)
358 index = IWL_AP_ID;
359 else if (is_broadcast_ether_addr(addr))
360 index = priv->hw_setting.bcast_sta_id;
361 else
362 for (i = IWL_STA_ID; i < priv->hw_setting.max_stations; i++)
363 if (priv->stations[i].used &&
364 !compare_ether_addr(priv->stations[i].sta.sta.addr,
365 addr)) {
366 index = i;
367 break;
368 }
369
370 if (unlikely(index == IWL_INVALID_STATION))
371 goto out;
372
373 if (priv->stations[index].used) {
374 priv->stations[index].used = 0;
375 priv->num_stations--;
376 }
377
378 BUG_ON(priv->num_stations < 0);
379
380out:
381 spin_unlock_irqrestore(&priv->sta_lock, flags);
382 return 0;
383}
Zhu Yi556f8db2007-09-27 11:27:33 +0800384#endif
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800385
386/**
387 * iwl3945_clear_stations_table - Clear the driver's station table
388 *
389 * NOTE: This does not clear or otherwise alter the device's station table.
390 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800391static void iwl3945_clear_stations_table(struct iwl3945_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700392{
393 unsigned long flags;
394
395 spin_lock_irqsave(&priv->sta_lock, flags);
396
397 priv->num_stations = 0;
398 memset(priv->stations, 0, sizeof(priv->stations));
399
400 spin_unlock_irqrestore(&priv->sta_lock, flags);
401}
402
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800403/**
404 * iwl3945_add_station - Add station to station tables in driver and device
405 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800406u8 iwl3945_add_station(struct iwl3945_priv *priv, const u8 *addr, int is_ap, u8 flags)
Zhu Yib481de92007-09-25 17:54:57 -0700407{
408 int i;
409 int index = IWL_INVALID_STATION;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800410 struct iwl3945_station_entry *station;
Zhu Yib481de92007-09-25 17:54:57 -0700411 unsigned long flags_spin;
Zhu Yic14c5212007-09-27 11:27:35 +0800412 u8 rate;
Zhu Yib481de92007-09-25 17:54:57 -0700413
414 spin_lock_irqsave(&priv->sta_lock, flags_spin);
415 if (is_ap)
416 index = IWL_AP_ID;
417 else if (is_broadcast_ether_addr(addr))
418 index = priv->hw_setting.bcast_sta_id;
419 else
420 for (i = IWL_STA_ID; i < priv->hw_setting.max_stations; i++) {
421 if (!compare_ether_addr(priv->stations[i].sta.sta.addr,
422 addr)) {
423 index = i;
424 break;
425 }
426
427 if (!priv->stations[i].used &&
428 index == IWL_INVALID_STATION)
429 index = i;
430 }
431
Ian Schram01ebd062007-10-25 17:15:22 +0800432 /* These two conditions has the same outcome but keep them separate
Zhu Yib481de92007-09-25 17:54:57 -0700433 since they have different meaning */
434 if (unlikely(index == IWL_INVALID_STATION)) {
435 spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
436 return index;
437 }
438
439 if (priv->stations[index].used &&
440 !compare_ether_addr(priv->stations[index].sta.sta.addr, addr)) {
441 spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
442 return index;
443 }
444
Johannes Berge1749612008-10-27 15:59:26 -0700445 IWL_DEBUG_ASSOC("Add STA ID %d: %pM\n", index, addr);
Zhu Yib481de92007-09-25 17:54:57 -0700446 station = &priv->stations[index];
447 station->used = 1;
448 priv->num_stations++;
449
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800450 /* Set up the REPLY_ADD_STA command to send to device */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800451 memset(&station->sta, 0, sizeof(struct iwl3945_addsta_cmd));
Zhu Yib481de92007-09-25 17:54:57 -0700452 memcpy(station->sta.sta.addr, addr, ETH_ALEN);
453 station->sta.mode = 0;
454 station->sta.sta.sta_id = index;
455 station->sta.station_flags = 0;
456
Johannes Berg8318d782008-01-24 19:38:38 +0100457 if (priv->band == IEEE80211_BAND_5GHZ)
Tomas Winkler69946332007-10-25 17:15:27 +0800458 rate = IWL_RATE_6M_PLCP;
459 else
460 rate = IWL_RATE_1M_PLCP;
Zhu Yic14c5212007-09-27 11:27:35 +0800461
462 /* Turn on both antennas for the station... */
463 station->sta.rate_n_flags =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800464 iwl3945_hw_set_rate_n_flags(rate, RATE_MCS_ANT_AB_MSK);
Zhu Yic14c5212007-09-27 11:27:35 +0800465 station->current_rate.rate_n_flags =
466 le16_to_cpu(station->sta.rate_n_flags);
467
Zhu Yib481de92007-09-25 17:54:57 -0700468 spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800469
470 /* Add station to device's station table */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800471 iwl3945_send_add_station(priv, &station->sta, flags);
Zhu Yib481de92007-09-25 17:54:57 -0700472 return index;
473
474}
475
476/*************** DRIVER STATUS FUNCTIONS *****/
477
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800478static inline int iwl3945_is_ready(struct iwl3945_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700479{
480 /* The adapter is 'ready' if READY and GEO_CONFIGURED bits are
481 * set but EXIT_PENDING is not */
482 return test_bit(STATUS_READY, &priv->status) &&
483 test_bit(STATUS_GEO_CONFIGURED, &priv->status) &&
484 !test_bit(STATUS_EXIT_PENDING, &priv->status);
485}
486
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800487static inline int iwl3945_is_alive(struct iwl3945_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700488{
489 return test_bit(STATUS_ALIVE, &priv->status);
490}
491
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800492static inline int iwl3945_is_init(struct iwl3945_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700493{
494 return test_bit(STATUS_INIT, &priv->status);
495}
496
Adel Gadllah80fcc9e2008-07-01 17:49:50 +0200497static inline int iwl3945_is_rfkill_sw(struct iwl3945_priv *priv)
498{
499 return test_bit(STATUS_RF_KILL_SW, &priv->status);
500}
501
502static inline int iwl3945_is_rfkill_hw(struct iwl3945_priv *priv)
503{
504 return test_bit(STATUS_RF_KILL_HW, &priv->status);
505}
506
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800507static inline int iwl3945_is_rfkill(struct iwl3945_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700508{
Adel Gadllah80fcc9e2008-07-01 17:49:50 +0200509 return iwl3945_is_rfkill_hw(priv) ||
510 iwl3945_is_rfkill_sw(priv);
Zhu Yib481de92007-09-25 17:54:57 -0700511}
512
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800513static inline int iwl3945_is_ready_rf(struct iwl3945_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700514{
515
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800516 if (iwl3945_is_rfkill(priv))
Zhu Yib481de92007-09-25 17:54:57 -0700517 return 0;
518
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800519 return iwl3945_is_ready(priv);
Zhu Yib481de92007-09-25 17:54:57 -0700520}
521
522/*************** HOST COMMAND QUEUE FUNCTIONS *****/
523
Abhijeet Kolekarc3056062008-11-12 13:14:08 -0800524#define IWL_CMD(x) case x: return #x
Zhu Yib481de92007-09-25 17:54:57 -0700525
526static const char *get_cmd_string(u8 cmd)
527{
528 switch (cmd) {
529 IWL_CMD(REPLY_ALIVE);
530 IWL_CMD(REPLY_ERROR);
531 IWL_CMD(REPLY_RXON);
532 IWL_CMD(REPLY_RXON_ASSOC);
533 IWL_CMD(REPLY_QOS_PARAM);
534 IWL_CMD(REPLY_RXON_TIMING);
535 IWL_CMD(REPLY_ADD_STA);
536 IWL_CMD(REPLY_REMOVE_STA);
537 IWL_CMD(REPLY_REMOVE_ALL_STA);
538 IWL_CMD(REPLY_3945_RX);
539 IWL_CMD(REPLY_TX);
540 IWL_CMD(REPLY_RATE_SCALE);
541 IWL_CMD(REPLY_LEDS_CMD);
542 IWL_CMD(REPLY_TX_LINK_QUALITY_CMD);
543 IWL_CMD(RADAR_NOTIFICATION);
544 IWL_CMD(REPLY_QUIET_CMD);
545 IWL_CMD(REPLY_CHANNEL_SWITCH);
546 IWL_CMD(CHANNEL_SWITCH_NOTIFICATION);
547 IWL_CMD(REPLY_SPECTRUM_MEASUREMENT_CMD);
548 IWL_CMD(SPECTRUM_MEASURE_NOTIFICATION);
549 IWL_CMD(POWER_TABLE_CMD);
550 IWL_CMD(PM_SLEEP_NOTIFICATION);
551 IWL_CMD(PM_DEBUG_STATISTIC_NOTIFIC);
552 IWL_CMD(REPLY_SCAN_CMD);
553 IWL_CMD(REPLY_SCAN_ABORT_CMD);
554 IWL_CMD(SCAN_START_NOTIFICATION);
555 IWL_CMD(SCAN_RESULTS_NOTIFICATION);
556 IWL_CMD(SCAN_COMPLETE_NOTIFICATION);
557 IWL_CMD(BEACON_NOTIFICATION);
558 IWL_CMD(REPLY_TX_BEACON);
559 IWL_CMD(WHO_IS_AWAKE_NOTIFICATION);
560 IWL_CMD(QUIET_NOTIFICATION);
561 IWL_CMD(REPLY_TX_PWR_TABLE_CMD);
562 IWL_CMD(MEASURE_ABORT_NOTIFICATION);
563 IWL_CMD(REPLY_BT_CONFIG);
564 IWL_CMD(REPLY_STATISTICS_CMD);
565 IWL_CMD(STATISTICS_NOTIFICATION);
566 IWL_CMD(REPLY_CARD_STATE_CMD);
567 IWL_CMD(CARD_STATE_NOTIFICATION);
568 IWL_CMD(MISSED_BEACONS_NOTIFICATION);
569 default:
570 return "UNKNOWN";
571
572 }
573}
574
575#define HOST_COMPLETE_TIMEOUT (HZ / 2)
576
577/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800578 * iwl3945_enqueue_hcmd - enqueue a uCode command
Zhu Yib481de92007-09-25 17:54:57 -0700579 * @priv: device private data point
580 * @cmd: a point to the ucode command structure
581 *
582 * The function returns < 0 values to indicate the operation is
583 * failed. On success, it turns the index (> 0) of command in the
584 * command queue.
585 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800586static int iwl3945_enqueue_hcmd(struct iwl3945_priv *priv, struct iwl3945_host_cmd *cmd)
Zhu Yib481de92007-09-25 17:54:57 -0700587{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800588 struct iwl3945_tx_queue *txq = &priv->txq[IWL_CMD_QUEUE_NUM];
589 struct iwl3945_queue *q = &txq->q;
590 struct iwl3945_tfd_frame *tfd;
Zhu Yib481de92007-09-25 17:54:57 -0700591 u32 *control_flags;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800592 struct iwl3945_cmd *out_cmd;
Zhu Yib481de92007-09-25 17:54:57 -0700593 u32 idx;
594 u16 fix_size = (u16)(cmd->len + sizeof(out_cmd->hdr));
595 dma_addr_t phys_addr;
596 int pad;
597 u16 count;
598 int ret;
599 unsigned long flags;
600
601 /* If any of the command structures end up being larger than
602 * the TFD_MAX_PAYLOAD_SIZE, and it sent as a 'small' command then
603 * we will need to increase the size of the TFD entries */
604 BUG_ON((fix_size > TFD_MAX_PAYLOAD_SIZE) &&
605 !(cmd->meta.flags & CMD_SIZE_HUGE));
606
Gregory Greenmanc342a1b2008-02-06 11:20:40 -0800607
608 if (iwl3945_is_rfkill(priv)) {
609 IWL_DEBUG_INFO("Not sending command - RF KILL");
610 return -EIO;
611 }
612
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800613 if (iwl3945_queue_space(q) < ((cmd->meta.flags & CMD_ASYNC) ? 2 : 1)) {
Zhu Yib481de92007-09-25 17:54:57 -0700614 IWL_ERROR("No space for Tx\n");
615 return -ENOSPC;
616 }
617
618 spin_lock_irqsave(&priv->hcmd_lock, flags);
619
Tomas Winklerfc4b6852007-10-25 17:15:24 +0800620 tfd = &txq->bd[q->write_ptr];
Zhu Yib481de92007-09-25 17:54:57 -0700621 memset(tfd, 0, sizeof(*tfd));
622
623 control_flags = (u32 *) tfd;
624
Tomas Winklerfc4b6852007-10-25 17:15:24 +0800625 idx = get_cmd_index(q, q->write_ptr, cmd->meta.flags & CMD_SIZE_HUGE);
Zhu Yib481de92007-09-25 17:54:57 -0700626 out_cmd = &txq->cmd[idx];
627
628 out_cmd->hdr.cmd = cmd->id;
629 memcpy(&out_cmd->meta, &cmd->meta, sizeof(cmd->meta));
630 memcpy(&out_cmd->cmd.payload, cmd->data, cmd->len);
631
632 /* At this point, the out_cmd now has all of the incoming cmd
633 * information */
634
635 out_cmd->hdr.flags = 0;
636 out_cmd->hdr.sequence = cpu_to_le16(QUEUE_TO_SEQ(IWL_CMD_QUEUE_NUM) |
Tomas Winklerfc4b6852007-10-25 17:15:24 +0800637 INDEX_TO_SEQ(q->write_ptr));
Zhu Yib481de92007-09-25 17:54:57 -0700638 if (out_cmd->meta.flags & CMD_SIZE_HUGE)
639 out_cmd->hdr.sequence |= cpu_to_le16(SEQ_HUGE_FRAME);
640
641 phys_addr = txq->dma_addr_cmd + sizeof(txq->cmd[0]) * idx +
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800642 offsetof(struct iwl3945_cmd, hdr);
643 iwl3945_hw_txq_attach_buf_to_tfd(priv, tfd, phys_addr, fix_size);
Zhu Yib481de92007-09-25 17:54:57 -0700644
645 pad = U32_PAD(cmd->len);
646 count = TFD_CTL_COUNT_GET(*control_flags);
647 *control_flags = TFD_CTL_COUNT_SET(count) | TFD_CTL_PAD_SET(pad);
648
649 IWL_DEBUG_HC("Sending command %s (#%x), seq: 0x%04X, "
650 "%d bytes at %d[%d]:%d\n",
651 get_cmd_string(out_cmd->hdr.cmd),
652 out_cmd->hdr.cmd, le16_to_cpu(out_cmd->hdr.sequence),
Tomas Winklerfc4b6852007-10-25 17:15:24 +0800653 fix_size, q->write_ptr, idx, IWL_CMD_QUEUE_NUM);
Zhu Yib481de92007-09-25 17:54:57 -0700654
655 txq->need_update = 1;
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800656
657 /* Increment and update queue's write index */
Tomas Winklerc54b6792008-03-06 17:36:53 -0800658 q->write_ptr = iwl_queue_inc_wrap(q->write_ptr, q->n_bd);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800659 ret = iwl3945_tx_queue_update_write_ptr(priv, txq);
Zhu Yib481de92007-09-25 17:54:57 -0700660
661 spin_unlock_irqrestore(&priv->hcmd_lock, flags);
662 return ret ? ret : idx;
663}
664
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800665static int iwl3945_send_cmd_async(struct iwl3945_priv *priv, struct iwl3945_host_cmd *cmd)
Zhu Yib481de92007-09-25 17:54:57 -0700666{
667 int ret;
668
669 BUG_ON(!(cmd->meta.flags & CMD_ASYNC));
670
671 /* An asynchronous command can not expect an SKB to be set. */
672 BUG_ON(cmd->meta.flags & CMD_WANT_SKB);
673
674 /* An asynchronous command MUST have a callback. */
675 BUG_ON(!cmd->meta.u.callback);
676
677 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
678 return -EBUSY;
679
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800680 ret = iwl3945_enqueue_hcmd(priv, cmd);
Zhu Yib481de92007-09-25 17:54:57 -0700681 if (ret < 0) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800682 IWL_ERROR("Error sending %s: iwl3945_enqueue_hcmd failed: %d\n",
Zhu Yib481de92007-09-25 17:54:57 -0700683 get_cmd_string(cmd->id), ret);
684 return ret;
685 }
686 return 0;
687}
688
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800689static int iwl3945_send_cmd_sync(struct iwl3945_priv *priv, struct iwl3945_host_cmd *cmd)
Zhu Yib481de92007-09-25 17:54:57 -0700690{
691 int cmd_idx;
692 int ret;
Zhu Yib481de92007-09-25 17:54:57 -0700693
694 BUG_ON(cmd->meta.flags & CMD_ASYNC);
695
696 /* A synchronous command can not have a callback set. */
697 BUG_ON(cmd->meta.u.callback != NULL);
698
Tomas Winklere5472972008-03-28 16:21:12 -0700699 if (test_and_set_bit(STATUS_HCMD_SYNC_ACTIVE, &priv->status)) {
Zhu Yib481de92007-09-25 17:54:57 -0700700 IWL_ERROR("Error sending %s: Already sending a host command\n",
701 get_cmd_string(cmd->id));
Tomas Winklere5472972008-03-28 16:21:12 -0700702 ret = -EBUSY;
703 goto out;
Zhu Yib481de92007-09-25 17:54:57 -0700704 }
705
706 set_bit(STATUS_HCMD_ACTIVE, &priv->status);
707
708 if (cmd->meta.flags & CMD_WANT_SKB)
709 cmd->meta.source = &cmd->meta;
710
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800711 cmd_idx = iwl3945_enqueue_hcmd(priv, cmd);
Zhu Yib481de92007-09-25 17:54:57 -0700712 if (cmd_idx < 0) {
713 ret = cmd_idx;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800714 IWL_ERROR("Error sending %s: iwl3945_enqueue_hcmd failed: %d\n",
Zhu Yib481de92007-09-25 17:54:57 -0700715 get_cmd_string(cmd->id), ret);
716 goto out;
717 }
718
719 ret = wait_event_interruptible_timeout(priv->wait_command_queue,
720 !test_bit(STATUS_HCMD_ACTIVE, &priv->status),
721 HOST_COMPLETE_TIMEOUT);
722 if (!ret) {
723 if (test_bit(STATUS_HCMD_ACTIVE, &priv->status)) {
724 IWL_ERROR("Error sending %s: time out after %dms.\n",
725 get_cmd_string(cmd->id),
726 jiffies_to_msecs(HOST_COMPLETE_TIMEOUT));
727
728 clear_bit(STATUS_HCMD_ACTIVE, &priv->status);
729 ret = -ETIMEDOUT;
730 goto cancel;
731 }
732 }
733
734 if (test_bit(STATUS_RF_KILL_HW, &priv->status)) {
735 IWL_DEBUG_INFO("Command %s aborted: RF KILL Switch\n",
736 get_cmd_string(cmd->id));
737 ret = -ECANCELED;
738 goto fail;
739 }
740 if (test_bit(STATUS_FW_ERROR, &priv->status)) {
741 IWL_DEBUG_INFO("Command %s failed: FW Error\n",
742 get_cmd_string(cmd->id));
743 ret = -EIO;
744 goto fail;
745 }
746 if ((cmd->meta.flags & CMD_WANT_SKB) && !cmd->meta.u.skb) {
747 IWL_ERROR("Error: Response NULL in '%s'\n",
748 get_cmd_string(cmd->id));
749 ret = -EIO;
Zhu Yi73e1a652009-01-08 10:19:58 -0800750 goto cancel;
Zhu Yib481de92007-09-25 17:54:57 -0700751 }
752
753 ret = 0;
754 goto out;
755
756cancel:
757 if (cmd->meta.flags & CMD_WANT_SKB) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800758 struct iwl3945_cmd *qcmd;
Zhu Yib481de92007-09-25 17:54:57 -0700759
760 /* Cancel the CMD_WANT_SKB flag for the cmd in the
761 * TX cmd queue. Otherwise in case the cmd comes
762 * in later, it will possibly set an invalid
763 * address (cmd->meta.source). */
764 qcmd = &priv->txq[IWL_CMD_QUEUE_NUM].cmd[cmd_idx];
765 qcmd->meta.flags &= ~CMD_WANT_SKB;
766 }
767fail:
768 if (cmd->meta.u.skb) {
769 dev_kfree_skb_any(cmd->meta.u.skb);
770 cmd->meta.u.skb = NULL;
771 }
772out:
Tomas Winklere5472972008-03-28 16:21:12 -0700773 clear_bit(STATUS_HCMD_SYNC_ACTIVE, &priv->status);
Zhu Yib481de92007-09-25 17:54:57 -0700774 return ret;
775}
776
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800777int iwl3945_send_cmd(struct iwl3945_priv *priv, struct iwl3945_host_cmd *cmd)
Zhu Yib481de92007-09-25 17:54:57 -0700778{
Zhu Yib481de92007-09-25 17:54:57 -0700779 if (cmd->meta.flags & CMD_ASYNC)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800780 return iwl3945_send_cmd_async(priv, cmd);
Zhu Yib481de92007-09-25 17:54:57 -0700781
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800782 return iwl3945_send_cmd_sync(priv, cmd);
Zhu Yib481de92007-09-25 17:54:57 -0700783}
784
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800785int iwl3945_send_cmd_pdu(struct iwl3945_priv *priv, u8 id, u16 len, const void *data)
Zhu Yib481de92007-09-25 17:54:57 -0700786{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800787 struct iwl3945_host_cmd cmd = {
Zhu Yib481de92007-09-25 17:54:57 -0700788 .id = id,
789 .len = len,
790 .data = data,
791 };
792
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800793 return iwl3945_send_cmd_sync(priv, &cmd);
Zhu Yib481de92007-09-25 17:54:57 -0700794}
795
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800796static int __must_check iwl3945_send_cmd_u32(struct iwl3945_priv *priv, u8 id, u32 val)
Zhu Yib481de92007-09-25 17:54:57 -0700797{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800798 struct iwl3945_host_cmd cmd = {
Zhu Yib481de92007-09-25 17:54:57 -0700799 .id = id,
800 .len = sizeof(val),
801 .data = &val,
802 };
803
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800804 return iwl3945_send_cmd_sync(priv, &cmd);
Zhu Yib481de92007-09-25 17:54:57 -0700805}
806
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800807int iwl3945_send_statistics_request(struct iwl3945_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700808{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800809 return iwl3945_send_cmd_u32(priv, REPLY_STATISTICS_CMD, 0);
Zhu Yib481de92007-09-25 17:54:57 -0700810}
811
812/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800813 * iwl3945_set_rxon_channel - Set the phymode and channel values in staging RXON
Johannes Berg8318d782008-01-24 19:38:38 +0100814 * @band: 2.4 or 5 GHz band
815 * @channel: Any channel valid for the requested band
Zhu Yib481de92007-09-25 17:54:57 -0700816
Johannes Berg8318d782008-01-24 19:38:38 +0100817 * In addition to setting the staging RXON, priv->band is also set.
Zhu Yib481de92007-09-25 17:54:57 -0700818 *
819 * NOTE: Does not commit to the hardware; it sets appropriate bit fields
Johannes Berg8318d782008-01-24 19:38:38 +0100820 * in the staging RXON flag structure based on the band
Zhu Yib481de92007-09-25 17:54:57 -0700821 */
Johannes Berg8318d782008-01-24 19:38:38 +0100822static int iwl3945_set_rxon_channel(struct iwl3945_priv *priv,
823 enum ieee80211_band band,
824 u16 channel)
Zhu Yib481de92007-09-25 17:54:57 -0700825{
Johannes Berg8318d782008-01-24 19:38:38 +0100826 if (!iwl3945_get_channel_info(priv, band, channel)) {
Zhu Yib481de92007-09-25 17:54:57 -0700827 IWL_DEBUG_INFO("Could not set channel to %d [%d]\n",
Johannes Berg8318d782008-01-24 19:38:38 +0100828 channel, band);
Zhu Yib481de92007-09-25 17:54:57 -0700829 return -EINVAL;
830 }
831
832 if ((le16_to_cpu(priv->staging_rxon.channel) == channel) &&
Johannes Berg8318d782008-01-24 19:38:38 +0100833 (priv->band == band))
Zhu Yib481de92007-09-25 17:54:57 -0700834 return 0;
835
836 priv->staging_rxon.channel = cpu_to_le16(channel);
Johannes Berg8318d782008-01-24 19:38:38 +0100837 if (band == IEEE80211_BAND_5GHZ)
Zhu Yib481de92007-09-25 17:54:57 -0700838 priv->staging_rxon.flags &= ~RXON_FLG_BAND_24G_MSK;
839 else
840 priv->staging_rxon.flags |= RXON_FLG_BAND_24G_MSK;
841
Johannes Berg8318d782008-01-24 19:38:38 +0100842 priv->band = band;
Zhu Yib481de92007-09-25 17:54:57 -0700843
Johannes Berg8318d782008-01-24 19:38:38 +0100844 IWL_DEBUG_INFO("Staging channel set to %d [%d]\n", channel, band);
Zhu Yib481de92007-09-25 17:54:57 -0700845
846 return 0;
847}
848
849/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800850 * iwl3945_check_rxon_cmd - validate RXON structure is valid
Zhu Yib481de92007-09-25 17:54:57 -0700851 *
852 * NOTE: This is really only useful during development and can eventually
853 * be #ifdef'd out once the driver is stable and folks aren't actively
854 * making changes
855 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800856static int iwl3945_check_rxon_cmd(struct iwl3945_rxon_cmd *rxon)
Zhu Yib481de92007-09-25 17:54:57 -0700857{
858 int error = 0;
859 int counter = 1;
860
861 if (rxon->flags & RXON_FLG_BAND_24G_MSK) {
862 error |= le32_to_cpu(rxon->flags &
863 (RXON_FLG_TGJ_NARROW_BAND_MSK |
864 RXON_FLG_RADAR_DETECT_MSK));
865 if (error)
866 IWL_WARNING("check 24G fields %d | %d\n",
867 counter++, error);
868 } else {
869 error |= (rxon->flags & RXON_FLG_SHORT_SLOT_MSK) ?
870 0 : le32_to_cpu(RXON_FLG_SHORT_SLOT_MSK);
871 if (error)
872 IWL_WARNING("check 52 fields %d | %d\n",
873 counter++, error);
874 error |= le32_to_cpu(rxon->flags & RXON_FLG_CCK_MSK);
875 if (error)
876 IWL_WARNING("check 52 CCK %d | %d\n",
877 counter++, error);
878 }
879 error |= (rxon->node_addr[0] | rxon->bssid_addr[0]) & 0x1;
880 if (error)
881 IWL_WARNING("check mac addr %d | %d\n", counter++, error);
882
883 /* make sure basic rates 6Mbps and 1Mbps are supported */
884 error |= (((rxon->ofdm_basic_rates & IWL_RATE_6M_MASK) == 0) &&
885 ((rxon->cck_basic_rates & IWL_RATE_1M_MASK) == 0));
886 if (error)
887 IWL_WARNING("check basic rate %d | %d\n", counter++, error);
888
889 error |= (le16_to_cpu(rxon->assoc_id) > 2007);
890 if (error)
891 IWL_WARNING("check assoc id %d | %d\n", counter++, error);
892
893 error |= ((rxon->flags & (RXON_FLG_CCK_MSK | RXON_FLG_SHORT_SLOT_MSK))
894 == (RXON_FLG_CCK_MSK | RXON_FLG_SHORT_SLOT_MSK));
895 if (error)
896 IWL_WARNING("check CCK and short slot %d | %d\n",
897 counter++, error);
898
899 error |= ((rxon->flags & (RXON_FLG_CCK_MSK | RXON_FLG_AUTO_DETECT_MSK))
900 == (RXON_FLG_CCK_MSK | RXON_FLG_AUTO_DETECT_MSK));
901 if (error)
902 IWL_WARNING("check CCK & auto detect %d | %d\n",
903 counter++, error);
904
905 error |= ((rxon->flags & (RXON_FLG_AUTO_DETECT_MSK |
906 RXON_FLG_TGG_PROTECT_MSK)) == RXON_FLG_TGG_PROTECT_MSK);
907 if (error)
908 IWL_WARNING("check TGG and auto detect %d | %d\n",
909 counter++, error);
910
911 if ((rxon->flags & RXON_FLG_DIS_DIV_MSK))
912 error |= ((rxon->flags & (RXON_FLG_ANT_B_MSK |
913 RXON_FLG_ANT_A_MSK)) == 0);
914 if (error)
915 IWL_WARNING("check antenna %d %d\n", counter++, error);
916
917 if (error)
918 IWL_WARNING("Tuning to channel %d\n",
919 le16_to_cpu(rxon->channel));
920
921 if (error) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800922 IWL_ERROR("Not a valid iwl3945_rxon_assoc_cmd field values\n");
Zhu Yib481de92007-09-25 17:54:57 -0700923 return -1;
924 }
925 return 0;
926}
927
928/**
Ben Cahill9fbab512007-11-29 11:09:47 +0800929 * iwl3945_full_rxon_required - check if full RXON (vs RXON_ASSOC) cmd is needed
Ian Schram01ebd062007-10-25 17:15:22 +0800930 * @priv: staging_rxon is compared to active_rxon
Zhu Yib481de92007-09-25 17:54:57 -0700931 *
Ben Cahill9fbab512007-11-29 11:09:47 +0800932 * If the RXON structure is changing enough to require a new tune,
933 * or is clearing the RXON_FILTER_ASSOC_MSK, then return 1 to indicate that
934 * a new tune (full RXON command, rather than RXON_ASSOC cmd) is required.
Zhu Yib481de92007-09-25 17:54:57 -0700935 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800936static int iwl3945_full_rxon_required(struct iwl3945_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700937{
938
939 /* These items are only settable from the full RXON command */
Ron Rindjunsky5d1e2322008-06-30 17:23:04 +0800940 if (!(iwl3945_is_associated(priv)) ||
Zhu Yib481de92007-09-25 17:54:57 -0700941 compare_ether_addr(priv->staging_rxon.bssid_addr,
942 priv->active_rxon.bssid_addr) ||
943 compare_ether_addr(priv->staging_rxon.node_addr,
944 priv->active_rxon.node_addr) ||
945 compare_ether_addr(priv->staging_rxon.wlap_bssid_addr,
946 priv->active_rxon.wlap_bssid_addr) ||
947 (priv->staging_rxon.dev_type != priv->active_rxon.dev_type) ||
948 (priv->staging_rxon.channel != priv->active_rxon.channel) ||
949 (priv->staging_rxon.air_propagation !=
950 priv->active_rxon.air_propagation) ||
951 (priv->staging_rxon.assoc_id != priv->active_rxon.assoc_id))
952 return 1;
953
954 /* flags, filter_flags, ofdm_basic_rates, and cck_basic_rates can
955 * be updated with the RXON_ASSOC command -- however only some
956 * flag transitions are allowed using RXON_ASSOC */
957
958 /* Check if we are not switching bands */
959 if ((priv->staging_rxon.flags & RXON_FLG_BAND_24G_MSK) !=
960 (priv->active_rxon.flags & RXON_FLG_BAND_24G_MSK))
961 return 1;
962
963 /* Check if we are switching association toggle */
964 if ((priv->staging_rxon.filter_flags & RXON_FILTER_ASSOC_MSK) !=
965 (priv->active_rxon.filter_flags & RXON_FILTER_ASSOC_MSK))
966 return 1;
967
968 return 0;
969}
970
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800971static int iwl3945_send_rxon_assoc(struct iwl3945_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700972{
973 int rc = 0;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800974 struct iwl3945_rx_packet *res = NULL;
975 struct iwl3945_rxon_assoc_cmd rxon_assoc;
976 struct iwl3945_host_cmd cmd = {
Zhu Yib481de92007-09-25 17:54:57 -0700977 .id = REPLY_RXON_ASSOC,
978 .len = sizeof(rxon_assoc),
979 .meta.flags = CMD_WANT_SKB,
980 .data = &rxon_assoc,
981 };
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800982 const struct iwl3945_rxon_cmd *rxon1 = &priv->staging_rxon;
983 const struct iwl3945_rxon_cmd *rxon2 = &priv->active_rxon;
Zhu Yib481de92007-09-25 17:54:57 -0700984
985 if ((rxon1->flags == rxon2->flags) &&
986 (rxon1->filter_flags == rxon2->filter_flags) &&
987 (rxon1->cck_basic_rates == rxon2->cck_basic_rates) &&
988 (rxon1->ofdm_basic_rates == rxon2->ofdm_basic_rates)) {
989 IWL_DEBUG_INFO("Using current RXON_ASSOC. Not resending.\n");
990 return 0;
991 }
992
993 rxon_assoc.flags = priv->staging_rxon.flags;
994 rxon_assoc.filter_flags = priv->staging_rxon.filter_flags;
995 rxon_assoc.ofdm_basic_rates = priv->staging_rxon.ofdm_basic_rates;
996 rxon_assoc.cck_basic_rates = priv->staging_rxon.cck_basic_rates;
997 rxon_assoc.reserved = 0;
998
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800999 rc = iwl3945_send_cmd_sync(priv, &cmd);
Zhu Yib481de92007-09-25 17:54:57 -07001000 if (rc)
1001 return rc;
1002
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001003 res = (struct iwl3945_rx_packet *)cmd.meta.u.skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07001004 if (res->hdr.flags & IWL_CMD_FAILED_MSK) {
1005 IWL_ERROR("Bad return from REPLY_RXON_ASSOC command\n");
1006 rc = -EIO;
1007 }
1008
1009 priv->alloc_rxb_skb--;
1010 dev_kfree_skb_any(cmd.meta.u.skb);
1011
1012 return rc;
1013}
1014
1015/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001016 * iwl3945_commit_rxon - commit staging_rxon to hardware
Zhu Yib481de92007-09-25 17:54:57 -07001017 *
Ian Schram01ebd062007-10-25 17:15:22 +08001018 * The RXON command in staging_rxon is committed to the hardware and
Zhu Yib481de92007-09-25 17:54:57 -07001019 * the active_rxon structure is updated with the new data. This
1020 * function correctly transitions out of the RXON_ASSOC_MSK state if
1021 * a HW tune is required based on the RXON structure changes.
1022 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001023static int iwl3945_commit_rxon(struct iwl3945_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001024{
1025 /* cast away the const for active_rxon in this function */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001026 struct iwl3945_rxon_cmd *active_rxon = (void *)&priv->active_rxon;
Zhu Yib481de92007-09-25 17:54:57 -07001027 int rc = 0;
1028
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001029 if (!iwl3945_is_alive(priv))
Zhu Yib481de92007-09-25 17:54:57 -07001030 return -1;
1031
1032 /* always get timestamp with Rx frame */
1033 priv->staging_rxon.flags |= RXON_FLG_TSF2HOST_MSK;
1034
1035 /* select antenna */
1036 priv->staging_rxon.flags &=
1037 ~(RXON_FLG_DIS_DIV_MSK | RXON_FLG_ANT_SEL_MSK);
1038 priv->staging_rxon.flags |= iwl3945_get_antenna_flags(priv);
1039
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001040 rc = iwl3945_check_rxon_cmd(&priv->staging_rxon);
Zhu Yib481de92007-09-25 17:54:57 -07001041 if (rc) {
1042 IWL_ERROR("Invalid RXON configuration. Not committing.\n");
1043 return -EINVAL;
1044 }
1045
1046 /* If we don't need to send a full RXON, we can use
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001047 * iwl3945_rxon_assoc_cmd which is used to reconfigure filter
Zhu Yib481de92007-09-25 17:54:57 -07001048 * and other flags for the current radio configuration. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001049 if (!iwl3945_full_rxon_required(priv)) {
1050 rc = iwl3945_send_rxon_assoc(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001051 if (rc) {
1052 IWL_ERROR("Error setting RXON_ASSOC "
1053 "configuration (%d).\n", rc);
1054 return rc;
1055 }
1056
1057 memcpy(active_rxon, &priv->staging_rxon, sizeof(*active_rxon));
1058
1059 return 0;
1060 }
1061
1062 /* If we are currently associated and the new config requires
1063 * an RXON_ASSOC and the new config wants the associated mask enabled,
1064 * we must clear the associated from the active configuration
1065 * before we apply the new config */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001066 if (iwl3945_is_associated(priv) &&
Zhu Yib481de92007-09-25 17:54:57 -07001067 (priv->staging_rxon.filter_flags & RXON_FILTER_ASSOC_MSK)) {
1068 IWL_DEBUG_INFO("Toggling associated bit on current RXON\n");
1069 active_rxon->filter_flags &= ~RXON_FILTER_ASSOC_MSK;
1070
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001071 rc = iwl3945_send_cmd_pdu(priv, REPLY_RXON,
1072 sizeof(struct iwl3945_rxon_cmd),
Zhu Yib481de92007-09-25 17:54:57 -07001073 &priv->active_rxon);
1074
1075 /* If the mask clearing failed then we set
1076 * active_rxon back to what it was previously */
1077 if (rc) {
1078 active_rxon->filter_flags |= RXON_FILTER_ASSOC_MSK;
1079 IWL_ERROR("Error clearing ASSOC_MSK on current "
1080 "configuration (%d).\n", rc);
1081 return rc;
1082 }
Zhu Yib481de92007-09-25 17:54:57 -07001083 }
1084
1085 IWL_DEBUG_INFO("Sending RXON\n"
1086 "* with%s RXON_FILTER_ASSOC_MSK\n"
1087 "* channel = %d\n"
Johannes Berge1749612008-10-27 15:59:26 -07001088 "* bssid = %pM\n",
Zhu Yib481de92007-09-25 17:54:57 -07001089 ((priv->staging_rxon.filter_flags &
1090 RXON_FILTER_ASSOC_MSK) ? "" : "out"),
1091 le16_to_cpu(priv->staging_rxon.channel),
Johannes Berge1749612008-10-27 15:59:26 -07001092 priv->staging_rxon.bssid_addr);
Zhu Yib481de92007-09-25 17:54:57 -07001093
1094 /* Apply the new configuration */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001095 rc = iwl3945_send_cmd_pdu(priv, REPLY_RXON,
1096 sizeof(struct iwl3945_rxon_cmd), &priv->staging_rxon);
Zhu Yib481de92007-09-25 17:54:57 -07001097 if (rc) {
1098 IWL_ERROR("Error setting new configuration (%d).\n", rc);
1099 return rc;
1100 }
1101
1102 memcpy(active_rxon, &priv->staging_rxon, sizeof(*active_rxon));
1103
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001104 iwl3945_clear_stations_table(priv);
Zhu Yi556f8db2007-09-27 11:27:33 +08001105
Zhu Yib481de92007-09-25 17:54:57 -07001106 /* If we issue a new RXON command which required a tune then we must
1107 * send a new TXPOWER command or we won't be able to Tx any frames */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001108 rc = iwl3945_hw_reg_send_txpower(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001109 if (rc) {
1110 IWL_ERROR("Error setting Tx power (%d).\n", rc);
1111 return rc;
1112 }
1113
1114 /* Add the broadcast address so we can send broadcast frames */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001115 if (iwl3945_add_station(priv, iwl3945_broadcast_addr, 0, 0) ==
Zhu Yib481de92007-09-25 17:54:57 -07001116 IWL_INVALID_STATION) {
1117 IWL_ERROR("Error adding BROADCAST address for transmit.\n");
1118 return -EIO;
1119 }
1120
1121 /* If we have set the ASSOC_MSK and we are in BSS mode then
1122 * add the IWL_AP_ID to the station rate table */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001123 if (iwl3945_is_associated(priv) &&
Johannes Berg05c914f2008-09-11 00:01:58 +02001124 (priv->iw_mode == NL80211_IFTYPE_STATION))
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001125 if (iwl3945_add_station(priv, priv->active_rxon.bssid_addr, 1, 0)
Zhu Yib481de92007-09-25 17:54:57 -07001126 == IWL_INVALID_STATION) {
1127 IWL_ERROR("Error adding AP address for transmit.\n");
1128 return -EIO;
1129 }
1130
Johannes Berg8318d782008-01-24 19:38:38 +01001131 /* Init the hardware's rate fallback order based on the band */
Zhu Yib481de92007-09-25 17:54:57 -07001132 rc = iwl3945_init_hw_rate_table(priv);
1133 if (rc) {
1134 IWL_ERROR("Error setting HW rate table: %02X\n", rc);
1135 return -EIO;
1136 }
1137
1138 return 0;
1139}
1140
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001141static int iwl3945_send_bt_config(struct iwl3945_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001142{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001143 struct iwl3945_bt_cmd bt_cmd = {
Zhu Yib481de92007-09-25 17:54:57 -07001144 .flags = 3,
1145 .lead_time = 0xAA,
1146 .max_kill = 1,
1147 .kill_ack_mask = 0,
1148 .kill_cts_mask = 0,
1149 };
1150
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001151 return iwl3945_send_cmd_pdu(priv, REPLY_BT_CONFIG,
1152 sizeof(struct iwl3945_bt_cmd), &bt_cmd);
Zhu Yib481de92007-09-25 17:54:57 -07001153}
1154
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001155static int iwl3945_send_scan_abort(struct iwl3945_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001156{
1157 int rc = 0;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001158 struct iwl3945_rx_packet *res;
1159 struct iwl3945_host_cmd cmd = {
Zhu Yib481de92007-09-25 17:54:57 -07001160 .id = REPLY_SCAN_ABORT_CMD,
1161 .meta.flags = CMD_WANT_SKB,
1162 };
1163
1164 /* If there isn't a scan actively going on in the hardware
1165 * then we are in between scan bands and not actually
1166 * actively scanning, so don't send the abort command */
1167 if (!test_bit(STATUS_SCAN_HW, &priv->status)) {
1168 clear_bit(STATUS_SCAN_ABORTING, &priv->status);
1169 return 0;
1170 }
1171
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001172 rc = iwl3945_send_cmd_sync(priv, &cmd);
Zhu Yib481de92007-09-25 17:54:57 -07001173 if (rc) {
1174 clear_bit(STATUS_SCAN_ABORTING, &priv->status);
1175 return rc;
1176 }
1177
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001178 res = (struct iwl3945_rx_packet *)cmd.meta.u.skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07001179 if (res->u.status != CAN_ABORT_STATUS) {
1180 /* The scan abort will return 1 for success or
1181 * 2 for "failure". A failure condition can be
1182 * due to simply not being in an active scan which
1183 * can occur if we send the scan abort before we
1184 * the microcode has notified us that a scan is
1185 * completed. */
1186 IWL_DEBUG_INFO("SCAN_ABORT returned %d.\n", res->u.status);
1187 clear_bit(STATUS_SCAN_ABORTING, &priv->status);
1188 clear_bit(STATUS_SCAN_HW, &priv->status);
1189 }
1190
1191 dev_kfree_skb_any(cmd.meta.u.skb);
1192
1193 return rc;
1194}
1195
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001196static int iwl3945_card_state_sync_callback(struct iwl3945_priv *priv,
1197 struct iwl3945_cmd *cmd,
Zhu Yib481de92007-09-25 17:54:57 -07001198 struct sk_buff *skb)
1199{
1200 return 1;
1201}
1202
1203/*
1204 * CARD_STATE_CMD
1205 *
Ben Cahill9fbab512007-11-29 11:09:47 +08001206 * Use: Sets the device's internal card state to enable, disable, or halt
Zhu Yib481de92007-09-25 17:54:57 -07001207 *
1208 * When in the 'enable' state the card operates as normal.
1209 * When in the 'disable' state, the card enters into a low power mode.
1210 * When in the 'halt' state, the card is shut down and must be fully
1211 * restarted to come back on.
1212 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001213static int iwl3945_send_card_state(struct iwl3945_priv *priv, u32 flags, u8 meta_flag)
Zhu Yib481de92007-09-25 17:54:57 -07001214{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001215 struct iwl3945_host_cmd cmd = {
Zhu Yib481de92007-09-25 17:54:57 -07001216 .id = REPLY_CARD_STATE_CMD,
1217 .len = sizeof(u32),
1218 .data = &flags,
1219 .meta.flags = meta_flag,
1220 };
1221
1222 if (meta_flag & CMD_ASYNC)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001223 cmd.meta.u.callback = iwl3945_card_state_sync_callback;
Zhu Yib481de92007-09-25 17:54:57 -07001224
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001225 return iwl3945_send_cmd(priv, &cmd);
Zhu Yib481de92007-09-25 17:54:57 -07001226}
1227
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001228static int iwl3945_add_sta_sync_callback(struct iwl3945_priv *priv,
1229 struct iwl3945_cmd *cmd, struct sk_buff *skb)
Zhu Yib481de92007-09-25 17:54:57 -07001230{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001231 struct iwl3945_rx_packet *res = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07001232
1233 if (!skb) {
1234 IWL_ERROR("Error: Response NULL in REPLY_ADD_STA.\n");
1235 return 1;
1236 }
1237
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001238 res = (struct iwl3945_rx_packet *)skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07001239 if (res->hdr.flags & IWL_CMD_FAILED_MSK) {
1240 IWL_ERROR("Bad return from REPLY_ADD_STA (0x%08X)\n",
1241 res->hdr.flags);
1242 return 1;
1243 }
1244
1245 switch (res->u.add_sta.status) {
1246 case ADD_STA_SUCCESS_MSK:
1247 break;
1248 default:
1249 break;
1250 }
1251
1252 /* We didn't cache the SKB; let the caller free it */
1253 return 1;
1254}
1255
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001256int iwl3945_send_add_station(struct iwl3945_priv *priv,
1257 struct iwl3945_addsta_cmd *sta, u8 flags)
Zhu Yib481de92007-09-25 17:54:57 -07001258{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001259 struct iwl3945_rx_packet *res = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07001260 int rc = 0;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001261 struct iwl3945_host_cmd cmd = {
Zhu Yib481de92007-09-25 17:54:57 -07001262 .id = REPLY_ADD_STA,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001263 .len = sizeof(struct iwl3945_addsta_cmd),
Zhu Yib481de92007-09-25 17:54:57 -07001264 .meta.flags = flags,
1265 .data = sta,
1266 };
1267
1268 if (flags & CMD_ASYNC)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001269 cmd.meta.u.callback = iwl3945_add_sta_sync_callback;
Zhu Yib481de92007-09-25 17:54:57 -07001270 else
1271 cmd.meta.flags |= CMD_WANT_SKB;
1272
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001273 rc = iwl3945_send_cmd(priv, &cmd);
Zhu Yib481de92007-09-25 17:54:57 -07001274
1275 if (rc || (flags & CMD_ASYNC))
1276 return rc;
1277
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001278 res = (struct iwl3945_rx_packet *)cmd.meta.u.skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07001279 if (res->hdr.flags & IWL_CMD_FAILED_MSK) {
1280 IWL_ERROR("Bad return from REPLY_ADD_STA (0x%08X)\n",
1281 res->hdr.flags);
1282 rc = -EIO;
1283 }
1284
1285 if (rc == 0) {
1286 switch (res->u.add_sta.status) {
1287 case ADD_STA_SUCCESS_MSK:
1288 IWL_DEBUG_INFO("REPLY_ADD_STA PASSED\n");
1289 break;
1290 default:
1291 rc = -EIO;
1292 IWL_WARNING("REPLY_ADD_STA failed\n");
1293 break;
1294 }
1295 }
1296
1297 priv->alloc_rxb_skb--;
1298 dev_kfree_skb_any(cmd.meta.u.skb);
1299
1300 return rc;
1301}
1302
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001303static int iwl3945_update_sta_key_info(struct iwl3945_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07001304 struct ieee80211_key_conf *keyconf,
1305 u8 sta_id)
1306{
1307 unsigned long flags;
1308 __le16 key_flags = 0;
1309
1310 switch (keyconf->alg) {
1311 case ALG_CCMP:
1312 key_flags |= STA_KEY_FLG_CCMP;
1313 key_flags |= cpu_to_le16(
1314 keyconf->keyidx << STA_KEY_FLG_KEYID_POS);
1315 key_flags &= ~STA_KEY_FLG_INVALID;
1316 break;
1317 case ALG_TKIP:
1318 case ALG_WEP:
Zhu Yib481de92007-09-25 17:54:57 -07001319 default:
1320 return -EINVAL;
1321 }
1322 spin_lock_irqsave(&priv->sta_lock, flags);
1323 priv->stations[sta_id].keyinfo.alg = keyconf->alg;
1324 priv->stations[sta_id].keyinfo.keylen = keyconf->keylen;
1325 memcpy(priv->stations[sta_id].keyinfo.key, keyconf->key,
1326 keyconf->keylen);
1327
1328 memcpy(priv->stations[sta_id].sta.key.key, keyconf->key,
1329 keyconf->keylen);
1330 priv->stations[sta_id].sta.key.key_flags = key_flags;
1331 priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
1332 priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
1333
1334 spin_unlock_irqrestore(&priv->sta_lock, flags);
1335
1336 IWL_DEBUG_INFO("hwcrypto: modify ucode station key info\n");
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001337 iwl3945_send_add_station(priv, &priv->stations[sta_id].sta, 0);
Zhu Yib481de92007-09-25 17:54:57 -07001338 return 0;
1339}
1340
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001341static int iwl3945_clear_sta_key_info(struct iwl3945_priv *priv, u8 sta_id)
Zhu Yib481de92007-09-25 17:54:57 -07001342{
1343 unsigned long flags;
1344
1345 spin_lock_irqsave(&priv->sta_lock, flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001346 memset(&priv->stations[sta_id].keyinfo, 0, sizeof(struct iwl3945_hw_key));
1347 memset(&priv->stations[sta_id].sta.key, 0, sizeof(struct iwl3945_keyinfo));
Zhu Yib481de92007-09-25 17:54:57 -07001348 priv->stations[sta_id].sta.key.key_flags = STA_KEY_FLG_NO_ENC;
1349 priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
1350 priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
1351 spin_unlock_irqrestore(&priv->sta_lock, flags);
1352
1353 IWL_DEBUG_INFO("hwcrypto: clear ucode station key info\n");
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001354 iwl3945_send_add_station(priv, &priv->stations[sta_id].sta, 0);
Zhu Yib481de92007-09-25 17:54:57 -07001355 return 0;
1356}
1357
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001358static void iwl3945_clear_free_frames(struct iwl3945_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001359{
1360 struct list_head *element;
1361
1362 IWL_DEBUG_INFO("%d frames on pre-allocated heap on clear.\n",
1363 priv->frames_count);
1364
1365 while (!list_empty(&priv->free_frames)) {
1366 element = priv->free_frames.next;
1367 list_del(element);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001368 kfree(list_entry(element, struct iwl3945_frame, list));
Zhu Yib481de92007-09-25 17:54:57 -07001369 priv->frames_count--;
1370 }
1371
1372 if (priv->frames_count) {
1373 IWL_WARNING("%d frames still in use. Did we lose one?\n",
1374 priv->frames_count);
1375 priv->frames_count = 0;
1376 }
1377}
1378
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001379static struct iwl3945_frame *iwl3945_get_free_frame(struct iwl3945_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001380{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001381 struct iwl3945_frame *frame;
Zhu Yib481de92007-09-25 17:54:57 -07001382 struct list_head *element;
1383 if (list_empty(&priv->free_frames)) {
1384 frame = kzalloc(sizeof(*frame), GFP_KERNEL);
1385 if (!frame) {
1386 IWL_ERROR("Could not allocate frame!\n");
1387 return NULL;
1388 }
1389
1390 priv->frames_count++;
1391 return frame;
1392 }
1393
1394 element = priv->free_frames.next;
1395 list_del(element);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001396 return list_entry(element, struct iwl3945_frame, list);
Zhu Yib481de92007-09-25 17:54:57 -07001397}
1398
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001399static void iwl3945_free_frame(struct iwl3945_priv *priv, struct iwl3945_frame *frame)
Zhu Yib481de92007-09-25 17:54:57 -07001400{
1401 memset(frame, 0, sizeof(*frame));
1402 list_add(&frame->list, &priv->free_frames);
1403}
1404
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001405unsigned int iwl3945_fill_beacon_frame(struct iwl3945_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07001406 struct ieee80211_hdr *hdr,
Rami Rosen73ec1cc2008-12-16 09:37:07 +02001407 int left)
Zhu Yib481de92007-09-25 17:54:57 -07001408{
1409
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001410 if (!iwl3945_is_associated(priv) || !priv->ibss_beacon ||
Johannes Berg05c914f2008-09-11 00:01:58 +02001411 ((priv->iw_mode != NL80211_IFTYPE_ADHOC) &&
1412 (priv->iw_mode != NL80211_IFTYPE_AP)))
Zhu Yib481de92007-09-25 17:54:57 -07001413 return 0;
1414
1415 if (priv->ibss_beacon->len > left)
1416 return 0;
1417
1418 memcpy(hdr, priv->ibss_beacon->data, priv->ibss_beacon->len);
1419
1420 return priv->ibss_beacon->len;
1421}
1422
Kolekar, Abhijeetc24f0812008-11-07 09:58:44 -08001423static u8 iwl3945_rate_get_lowest_plcp(struct iwl3945_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001424{
1425 u8 i;
Kolekar, Abhijeetc24f0812008-11-07 09:58:44 -08001426 int rate_mask;
1427
1428 /* Set rate mask*/
1429 if (priv->staging_rxon.flags & RXON_FLG_BAND_24G_MSK)
Chatre, Reinettedbce56a2008-11-12 13:14:07 -08001430 rate_mask = priv->active_rate_basic & IWL_CCK_RATES_MASK;
Kolekar, Abhijeetc24f0812008-11-07 09:58:44 -08001431 else
Chatre, Reinettedbce56a2008-11-12 13:14:07 -08001432 rate_mask = priv->active_rate_basic & IWL_OFDM_RATES_MASK;
Zhu Yib481de92007-09-25 17:54:57 -07001433
1434 for (i = IWL_RATE_1M_INDEX; i != IWL_RATE_INVALID;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001435 i = iwl3945_rates[i].next_ieee) {
Zhu Yib481de92007-09-25 17:54:57 -07001436 if (rate_mask & (1 << i))
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001437 return iwl3945_rates[i].plcp;
Zhu Yib481de92007-09-25 17:54:57 -07001438 }
1439
Kolekar, Abhijeetc24f0812008-11-07 09:58:44 -08001440 /* No valid rate was found. Assign the lowest one */
1441 if (priv->staging_rxon.flags & RXON_FLG_BAND_24G_MSK)
1442 return IWL_RATE_1M_PLCP;
1443 else
1444 return IWL_RATE_6M_PLCP;
Zhu Yib481de92007-09-25 17:54:57 -07001445}
1446
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001447static int iwl3945_send_beacon_cmd(struct iwl3945_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001448{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001449 struct iwl3945_frame *frame;
Zhu Yib481de92007-09-25 17:54:57 -07001450 unsigned int frame_size;
1451 int rc;
1452 u8 rate;
1453
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001454 frame = iwl3945_get_free_frame(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001455
1456 if (!frame) {
1457 IWL_ERROR("Could not obtain free frame buffer for beacon "
1458 "command.\n");
1459 return -ENOMEM;
1460 }
1461
Kolekar, Abhijeetc24f0812008-11-07 09:58:44 -08001462 rate = iwl3945_rate_get_lowest_plcp(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001463
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001464 frame_size = iwl3945_hw_get_beacon_cmd(priv, frame, rate);
Zhu Yib481de92007-09-25 17:54:57 -07001465
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001466 rc = iwl3945_send_cmd_pdu(priv, REPLY_TX_BEACON, frame_size,
Zhu Yib481de92007-09-25 17:54:57 -07001467 &frame->u.cmd[0]);
1468
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001469 iwl3945_free_frame(priv, frame);
Zhu Yib481de92007-09-25 17:54:57 -07001470
1471 return rc;
1472}
1473
1474/******************************************************************************
1475 *
1476 * EEPROM related functions
1477 *
1478 ******************************************************************************/
1479
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001480static void get_eeprom_mac(struct iwl3945_priv *priv, u8 *mac)
Zhu Yib481de92007-09-25 17:54:57 -07001481{
1482 memcpy(mac, priv->eeprom.mac_address, 6);
1483}
1484
Reinette Chatre74a3a252008-01-23 10:15:19 -08001485/*
1486 * Clear the OWNER_MSK, to establish driver (instead of uCode running on
1487 * embedded controller) as EEPROM reader; each read is a series of pulses
1488 * to/from the EEPROM chip, not a single event, so even reads could conflict
1489 * if they weren't arbitrated by some ownership mechanism. Here, the driver
1490 * simply claims ownership, which should be safe when this function is called
1491 * (i.e. before loading uCode!).
1492 */
1493static inline int iwl3945_eeprom_acquire_semaphore(struct iwl3945_priv *priv)
1494{
1495 _iwl3945_clear_bit(priv, CSR_EEPROM_GP, CSR_EEPROM_GP_IF_OWNER_MSK);
1496 return 0;
1497}
1498
Zhu Yib481de92007-09-25 17:54:57 -07001499/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001500 * iwl3945_eeprom_init - read EEPROM contents
Zhu Yib481de92007-09-25 17:54:57 -07001501 *
Cahill, Ben M6440adb2007-11-29 11:09:55 +08001502 * Load the EEPROM contents from adapter into priv->eeprom
Zhu Yib481de92007-09-25 17:54:57 -07001503 *
1504 * NOTE: This routine uses the non-debug IO access functions.
1505 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001506int iwl3945_eeprom_init(struct iwl3945_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001507{
Tomas Winkler58ff6d42008-02-13 02:47:54 +02001508 u16 *e = (u16 *)&priv->eeprom;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001509 u32 gp = iwl3945_read32(priv, CSR_EEPROM_GP);
Zhu Yib481de92007-09-25 17:54:57 -07001510 int sz = sizeof(priv->eeprom);
Zhu, Yi3d5717a2008-12-11 10:33:36 -08001511 int ret;
Zhu Yib481de92007-09-25 17:54:57 -07001512 u16 addr;
1513
1514 /* The EEPROM structure has several padding buffers within it
1515 * and when adding new EEPROM maps is subject to programmer errors
1516 * which may be very difficult to identify without explicitly
1517 * checking the resulting size of the eeprom map. */
1518 BUILD_BUG_ON(sizeof(priv->eeprom) != IWL_EEPROM_IMAGE_SIZE);
1519
1520 if ((gp & CSR_EEPROM_GP_VALID_MSK) == CSR_EEPROM_GP_BAD_SIGNATURE) {
Jiri Slaby6f147922008-08-11 23:49:41 +02001521 IWL_ERROR("EEPROM not found, EEPROM_GP=0x%08x\n", gp);
Zhu Yib481de92007-09-25 17:54:57 -07001522 return -ENOENT;
1523 }
1524
Cahill, Ben M6440adb2007-11-29 11:09:55 +08001525 /* Make sure driver (instead of uCode) is allowed to read EEPROM */
Zhu, Yi3d5717a2008-12-11 10:33:36 -08001526 ret = iwl3945_eeprom_acquire_semaphore(priv);
1527 if (ret < 0) {
Ian Schram91e17472007-10-25 17:15:23 +08001528 IWL_ERROR("Failed to acquire EEPROM semaphore.\n");
Zhu Yib481de92007-09-25 17:54:57 -07001529 return -ENOENT;
1530 }
1531
1532 /* eeprom is an array of 16bit values */
1533 for (addr = 0; addr < sz; addr += sizeof(u16)) {
Zhu, Yi3d5717a2008-12-11 10:33:36 -08001534 u32 r;
1535
1536 _iwl3945_write32(priv, CSR_EEPROM_REG,
1537 CSR_EEPROM_REG_MSK_ADDR & (addr << 1));
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001538 _iwl3945_clear_bit(priv, CSR_EEPROM_REG, CSR_EEPROM_REG_BIT_CMD);
Zhu, Yi3d5717a2008-12-11 10:33:36 -08001539 ret = iwl3945_poll_direct_bit(priv, CSR_EEPROM_REG,
1540 CSR_EEPROM_REG_READ_VALID_MSK,
1541 IWL_EEPROM_ACCESS_TIMEOUT);
1542 if (ret < 0) {
Jiri Slaby6f147922008-08-11 23:49:41 +02001543 IWL_ERROR("Time out reading EEPROM[%d]\n", addr);
Zhu, Yi3d5717a2008-12-11 10:33:36 -08001544 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07001545 }
Zhu, Yi3d5717a2008-12-11 10:33:36 -08001546
1547 r = _iwl3945_read_direct32(priv, CSR_EEPROM_REG);
Tomas Winkler58ff6d42008-02-13 02:47:54 +02001548 e[addr / 2] = le16_to_cpu((__force __le16)(r >> 16));
Zhu Yib481de92007-09-25 17:54:57 -07001549 }
1550
1551 return 0;
1552}
1553
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001554static void iwl3945_unset_hw_setting(struct iwl3945_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001555{
1556 if (priv->hw_setting.shared_virt)
1557 pci_free_consistent(priv->pci_dev,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001558 sizeof(struct iwl3945_shared),
Zhu Yib481de92007-09-25 17:54:57 -07001559 priv->hw_setting.shared_virt,
1560 priv->hw_setting.shared_phys);
1561}
1562
1563/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001564 * iwl3945_supported_rate_to_ie - fill in the supported rate in IE field
Zhu Yib481de92007-09-25 17:54:57 -07001565 *
1566 * return : set the bit for each supported rate insert in ie
1567 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001568static u16 iwl3945_supported_rate_to_ie(u8 *ie, u16 supported_rate,
Tomas Winklerc7c46672007-10-18 02:04:15 +02001569 u16 basic_rate, int *left)
Zhu Yib481de92007-09-25 17:54:57 -07001570{
1571 u16 ret_rates = 0, bit;
1572 int i;
Tomas Winklerc7c46672007-10-18 02:04:15 +02001573 u8 *cnt = ie;
1574 u8 *rates = ie + 1;
Zhu Yib481de92007-09-25 17:54:57 -07001575
1576 for (bit = 1, i = 0; i < IWL_RATE_COUNT; i++, bit <<= 1) {
1577 if (bit & supported_rate) {
1578 ret_rates |= bit;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001579 rates[*cnt] = iwl3945_rates[i].ieee |
Tomas Winklerc7c46672007-10-18 02:04:15 +02001580 ((bit & basic_rate) ? 0x80 : 0x00);
1581 (*cnt)++;
1582 (*left)--;
1583 if ((*left <= 0) ||
1584 (*cnt >= IWL_SUPPORTED_RATES_IE_LEN))
Zhu Yib481de92007-09-25 17:54:57 -07001585 break;
1586 }
1587 }
1588
1589 return ret_rates;
1590}
1591
1592/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001593 * iwl3945_fill_probe_req - fill in all required fields and IE for probe request
Zhu Yib481de92007-09-25 17:54:57 -07001594 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001595static u16 iwl3945_fill_probe_req(struct iwl3945_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07001596 struct ieee80211_mgmt *frame,
Johannes Berg430cfe92008-10-28 18:06:02 +01001597 int left)
Zhu Yib481de92007-09-25 17:54:57 -07001598{
1599 int len = 0;
1600 u8 *pos = NULL;
Tomas Winklerc7c46672007-10-18 02:04:15 +02001601 u16 active_rates, ret_rates, cck_rates;
Zhu Yib481de92007-09-25 17:54:57 -07001602
1603 /* Make sure there is enough space for the probe request,
1604 * two mandatory IEs and the data */
1605 left -= 24;
1606 if (left < 0)
1607 return 0;
1608 len += 24;
1609
1610 frame->frame_control = cpu_to_le16(IEEE80211_STYPE_PROBE_REQ);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001611 memcpy(frame->da, iwl3945_broadcast_addr, ETH_ALEN);
Zhu Yib481de92007-09-25 17:54:57 -07001612 memcpy(frame->sa, priv->mac_addr, ETH_ALEN);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001613 memcpy(frame->bssid, iwl3945_broadcast_addr, ETH_ALEN);
Zhu Yib481de92007-09-25 17:54:57 -07001614 frame->seq_ctrl = 0;
1615
1616 /* fill in our indirect SSID IE */
1617 /* ...next IE... */
1618
1619 left -= 2;
1620 if (left < 0)
1621 return 0;
1622 len += 2;
1623 pos = &(frame->u.probe_req.variable[0]);
1624 *pos++ = WLAN_EID_SSID;
1625 *pos++ = 0;
1626
Zhu Yib481de92007-09-25 17:54:57 -07001627 /* fill in supported rate */
1628 /* ...next IE... */
1629 left -= 2;
1630 if (left < 0)
1631 return 0;
Tomas Winklerc7c46672007-10-18 02:04:15 +02001632
Zhu Yib481de92007-09-25 17:54:57 -07001633 /* ... fill it in... */
1634 *pos++ = WLAN_EID_SUPP_RATES;
1635 *pos = 0;
Tomas Winklerc7c46672007-10-18 02:04:15 +02001636
1637 priv->active_rate = priv->rates_mask;
1638 active_rates = priv->active_rate;
Zhu Yib481de92007-09-25 17:54:57 -07001639 priv->active_rate_basic = priv->rates_mask & IWL_BASIC_RATES_MASK;
1640
Tomas Winklerc7c46672007-10-18 02:04:15 +02001641 cck_rates = IWL_CCK_RATES_MASK & active_rates;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001642 ret_rates = iwl3945_supported_rate_to_ie(pos, cck_rates,
Tomas Winklerc7c46672007-10-18 02:04:15 +02001643 priv->active_rate_basic, &left);
1644 active_rates &= ~ret_rates;
1645
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001646 ret_rates = iwl3945_supported_rate_to_ie(pos, active_rates,
Tomas Winklerc7c46672007-10-18 02:04:15 +02001647 priv->active_rate_basic, &left);
1648 active_rates &= ~ret_rates;
1649
Zhu Yib481de92007-09-25 17:54:57 -07001650 len += 2 + *pos;
1651 pos += (*pos) + 1;
Tomas Winklerc7c46672007-10-18 02:04:15 +02001652 if (active_rates == 0)
Zhu Yib481de92007-09-25 17:54:57 -07001653 goto fill_end;
1654
1655 /* fill in supported extended rate */
1656 /* ...next IE... */
1657 left -= 2;
1658 if (left < 0)
1659 return 0;
1660 /* ... fill it in... */
1661 *pos++ = WLAN_EID_EXT_SUPP_RATES;
1662 *pos = 0;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001663 iwl3945_supported_rate_to_ie(pos, active_rates,
Tomas Winklerc7c46672007-10-18 02:04:15 +02001664 priv->active_rate_basic, &left);
Zhu Yib481de92007-09-25 17:54:57 -07001665 if (*pos > 0)
1666 len += 2 + *pos;
1667
1668 fill_end:
1669 return (u16)len;
1670}
1671
1672/*
1673 * QoS support
1674*/
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001675static int iwl3945_send_qos_params_command(struct iwl3945_priv *priv,
1676 struct iwl3945_qosparam_cmd *qos)
Zhu Yib481de92007-09-25 17:54:57 -07001677{
1678
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001679 return iwl3945_send_cmd_pdu(priv, REPLY_QOS_PARAM,
1680 sizeof(struct iwl3945_qosparam_cmd), qos);
Zhu Yib481de92007-09-25 17:54:57 -07001681}
1682
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001683static void iwl3945_reset_qos(struct iwl3945_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001684{
1685 u16 cw_min = 15;
1686 u16 cw_max = 1023;
1687 u8 aifs = 2;
1688 u8 is_legacy = 0;
1689 unsigned long flags;
1690 int i;
1691
1692 spin_lock_irqsave(&priv->lock, flags);
1693 priv->qos_data.qos_active = 0;
1694
Winkler, Tomas6d1ef1a2008-12-09 11:29:00 -08001695 /* QoS always active in AP and ADHOC mode
1696 * In STA mode wait for association
1697 */
1698 if (priv->iw_mode == NL80211_IFTYPE_ADHOC ||
1699 priv->iw_mode == NL80211_IFTYPE_AP)
1700 priv->qos_data.qos_active = 1;
1701 else
1702 priv->qos_data.qos_active = 0;
1703
1704
1705 /* check for legacy mode */
1706 if ((priv->iw_mode == NL80211_IFTYPE_ADHOC &&
1707 (priv->active_rate & IWL_OFDM_RATES_MASK) == 0) ||
1708 (priv->iw_mode == NL80211_IFTYPE_STATION &&
1709 (priv->staging_rxon.flags & RXON_FLG_SHORT_SLOT_MSK) == 0)) {
Zhu Yib481de92007-09-25 17:54:57 -07001710 cw_min = 31;
1711 is_legacy = 1;
1712 }
1713
1714 if (priv->qos_data.qos_active)
1715 aifs = 3;
1716
1717 priv->qos_data.def_qos_parm.ac[0].cw_min = cpu_to_le16(cw_min);
1718 priv->qos_data.def_qos_parm.ac[0].cw_max = cpu_to_le16(cw_max);
1719 priv->qos_data.def_qos_parm.ac[0].aifsn = aifs;
1720 priv->qos_data.def_qos_parm.ac[0].edca_txop = 0;
1721 priv->qos_data.def_qos_parm.ac[0].reserved1 = 0;
1722
1723 if (priv->qos_data.qos_active) {
1724 i = 1;
1725 priv->qos_data.def_qos_parm.ac[i].cw_min = cpu_to_le16(cw_min);
1726 priv->qos_data.def_qos_parm.ac[i].cw_max = cpu_to_le16(cw_max);
1727 priv->qos_data.def_qos_parm.ac[i].aifsn = 7;
1728 priv->qos_data.def_qos_parm.ac[i].edca_txop = 0;
1729 priv->qos_data.def_qos_parm.ac[i].reserved1 = 0;
1730
1731 i = 2;
1732 priv->qos_data.def_qos_parm.ac[i].cw_min =
1733 cpu_to_le16((cw_min + 1) / 2 - 1);
1734 priv->qos_data.def_qos_parm.ac[i].cw_max =
1735 cpu_to_le16(cw_max);
1736 priv->qos_data.def_qos_parm.ac[i].aifsn = 2;
1737 if (is_legacy)
1738 priv->qos_data.def_qos_parm.ac[i].edca_txop =
1739 cpu_to_le16(6016);
1740 else
1741 priv->qos_data.def_qos_parm.ac[i].edca_txop =
1742 cpu_to_le16(3008);
1743 priv->qos_data.def_qos_parm.ac[i].reserved1 = 0;
1744
1745 i = 3;
1746 priv->qos_data.def_qos_parm.ac[i].cw_min =
1747 cpu_to_le16((cw_min + 1) / 4 - 1);
1748 priv->qos_data.def_qos_parm.ac[i].cw_max =
1749 cpu_to_le16((cw_max + 1) / 2 - 1);
1750 priv->qos_data.def_qos_parm.ac[i].aifsn = 2;
1751 priv->qos_data.def_qos_parm.ac[i].reserved1 = 0;
1752 if (is_legacy)
1753 priv->qos_data.def_qos_parm.ac[i].edca_txop =
1754 cpu_to_le16(3264);
1755 else
1756 priv->qos_data.def_qos_parm.ac[i].edca_txop =
1757 cpu_to_le16(1504);
1758 } else {
1759 for (i = 1; i < 4; i++) {
1760 priv->qos_data.def_qos_parm.ac[i].cw_min =
1761 cpu_to_le16(cw_min);
1762 priv->qos_data.def_qos_parm.ac[i].cw_max =
1763 cpu_to_le16(cw_max);
1764 priv->qos_data.def_qos_parm.ac[i].aifsn = aifs;
1765 priv->qos_data.def_qos_parm.ac[i].edca_txop = 0;
1766 priv->qos_data.def_qos_parm.ac[i].reserved1 = 0;
1767 }
1768 }
1769 IWL_DEBUG_QOS("set QoS to default \n");
1770
1771 spin_unlock_irqrestore(&priv->lock, flags);
1772}
1773
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001774static void iwl3945_activate_qos(struct iwl3945_priv *priv, u8 force)
Zhu Yib481de92007-09-25 17:54:57 -07001775{
1776 unsigned long flags;
1777
Zhu Yib481de92007-09-25 17:54:57 -07001778 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
1779 return;
1780
Zhu Yib481de92007-09-25 17:54:57 -07001781 spin_lock_irqsave(&priv->lock, flags);
1782 priv->qos_data.def_qos_parm.qos_flags = 0;
1783
1784 if (priv->qos_data.qos_cap.q_AP.queue_request &&
1785 !priv->qos_data.qos_cap.q_AP.txop_request)
1786 priv->qos_data.def_qos_parm.qos_flags |=
1787 QOS_PARAM_FLG_TXOP_TYPE_MSK;
1788
1789 if (priv->qos_data.qos_active)
1790 priv->qos_data.def_qos_parm.qos_flags |=
1791 QOS_PARAM_FLG_UPDATE_EDCA_MSK;
1792
1793 spin_unlock_irqrestore(&priv->lock, flags);
1794
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001795 if (force || iwl3945_is_associated(priv)) {
Tomas Winklera96a27f2008-10-23 23:48:56 -07001796 IWL_DEBUG_QOS("send QoS cmd with QoS active %d \n",
Zhu Yib481de92007-09-25 17:54:57 -07001797 priv->qos_data.qos_active);
1798
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001799 iwl3945_send_qos_params_command(priv,
Zhu Yib481de92007-09-25 17:54:57 -07001800 &(priv->qos_data.def_qos_parm));
1801 }
1802}
1803
Zhu Yib481de92007-09-25 17:54:57 -07001804/*
1805 * Power management (not Tx power!) functions
1806 */
1807#define MSEC_TO_USEC 1024
1808
1809#define NOSLP __constant_cpu_to_le32(0)
1810#define SLP IWL_POWER_DRIVER_ALLOW_SLEEP_MSK
1811#define SLP_TIMEOUT(T) __constant_cpu_to_le32((T) * MSEC_TO_USEC)
1812#define SLP_VEC(X0, X1, X2, X3, X4) {__constant_cpu_to_le32(X0), \
1813 __constant_cpu_to_le32(X1), \
1814 __constant_cpu_to_le32(X2), \
1815 __constant_cpu_to_le32(X3), \
1816 __constant_cpu_to_le32(X4)}
1817
1818
1819/* default power management (not Tx power) table values */
Tomas Winklera96a27f2008-10-23 23:48:56 -07001820/* for TIM 0-10 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001821static struct iwl3945_power_vec_entry range_0[IWL_POWER_AC] = {
Zhu Yib481de92007-09-25 17:54:57 -07001822 {{NOSLP, SLP_TIMEOUT(0), SLP_TIMEOUT(0), SLP_VEC(0, 0, 0, 0, 0)}, 0},
1823 {{SLP, SLP_TIMEOUT(200), SLP_TIMEOUT(500), SLP_VEC(1, 2, 3, 4, 4)}, 0},
1824 {{SLP, SLP_TIMEOUT(200), SLP_TIMEOUT(300), SLP_VEC(2, 4, 6, 7, 7)}, 0},
1825 {{SLP, SLP_TIMEOUT(50), SLP_TIMEOUT(100), SLP_VEC(2, 6, 9, 9, 10)}, 0},
1826 {{SLP, SLP_TIMEOUT(50), SLP_TIMEOUT(25), SLP_VEC(2, 7, 9, 9, 10)}, 1},
1827 {{SLP, SLP_TIMEOUT(25), SLP_TIMEOUT(25), SLP_VEC(4, 7, 10, 10, 10)}, 1}
1828};
1829
Tomas Winklera96a27f2008-10-23 23:48:56 -07001830/* for TIM > 10 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001831static struct iwl3945_power_vec_entry range_1[IWL_POWER_AC] = {
Zhu Yib481de92007-09-25 17:54:57 -07001832 {{NOSLP, SLP_TIMEOUT(0), SLP_TIMEOUT(0), SLP_VEC(0, 0, 0, 0, 0)}, 0},
1833 {{SLP, SLP_TIMEOUT(200), SLP_TIMEOUT(500),
1834 SLP_VEC(1, 2, 3, 4, 0xFF)}, 0},
1835 {{SLP, SLP_TIMEOUT(200), SLP_TIMEOUT(300),
1836 SLP_VEC(2, 4, 6, 7, 0xFF)}, 0},
1837 {{SLP, SLP_TIMEOUT(50), SLP_TIMEOUT(100),
1838 SLP_VEC(2, 6, 9, 9, 0xFF)}, 0},
1839 {{SLP, SLP_TIMEOUT(50), SLP_TIMEOUT(25), SLP_VEC(2, 7, 9, 9, 0xFF)}, 0},
1840 {{SLP, SLP_TIMEOUT(25), SLP_TIMEOUT(25),
1841 SLP_VEC(4, 7, 10, 10, 0xFF)}, 0}
1842};
1843
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001844int iwl3945_power_init_handle(struct iwl3945_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001845{
1846 int rc = 0, i;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001847 struct iwl3945_power_mgr *pow_data;
1848 int size = sizeof(struct iwl3945_power_vec_entry) * IWL_POWER_AC;
Zhu Yib481de92007-09-25 17:54:57 -07001849 u16 pci_pm;
1850
1851 IWL_DEBUG_POWER("Initialize power \n");
1852
1853 pow_data = &(priv->power_data);
1854
1855 memset(pow_data, 0, sizeof(*pow_data));
1856
1857 pow_data->active_index = IWL_POWER_RANGE_0;
1858 pow_data->dtim_val = 0xffff;
1859
1860 memcpy(&pow_data->pwr_range_0[0], &range_0[0], size);
1861 memcpy(&pow_data->pwr_range_1[0], &range_1[0], size);
1862
1863 rc = pci_read_config_word(priv->pci_dev, PCI_LINK_CTRL, &pci_pm);
1864 if (rc != 0)
1865 return 0;
1866 else {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001867 struct iwl3945_powertable_cmd *cmd;
Zhu Yib481de92007-09-25 17:54:57 -07001868
1869 IWL_DEBUG_POWER("adjust power command flags\n");
1870
1871 for (i = 0; i < IWL_POWER_AC; i++) {
1872 cmd = &pow_data->pwr_range_0[i].cmd;
1873
1874 if (pci_pm & 0x1)
1875 cmd->flags &= ~IWL_POWER_PCI_PM_MSK;
1876 else
1877 cmd->flags |= IWL_POWER_PCI_PM_MSK;
1878 }
1879 }
1880 return rc;
1881}
1882
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001883static int iwl3945_update_power_cmd(struct iwl3945_priv *priv,
1884 struct iwl3945_powertable_cmd *cmd, u32 mode)
Zhu Yib481de92007-09-25 17:54:57 -07001885{
1886 int rc = 0, i;
1887 u8 skip;
1888 u32 max_sleep = 0;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001889 struct iwl3945_power_vec_entry *range;
Zhu Yib481de92007-09-25 17:54:57 -07001890 u8 period = 0;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001891 struct iwl3945_power_mgr *pow_data;
Zhu Yib481de92007-09-25 17:54:57 -07001892
1893 if (mode > IWL_POWER_INDEX_5) {
1894 IWL_DEBUG_POWER("Error invalid power mode \n");
1895 return -1;
1896 }
1897 pow_data = &(priv->power_data);
1898
1899 if (pow_data->active_index == IWL_POWER_RANGE_0)
1900 range = &pow_data->pwr_range_0[0];
1901 else
1902 range = &pow_data->pwr_range_1[1];
1903
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001904 memcpy(cmd, &range[mode].cmd, sizeof(struct iwl3945_powertable_cmd));
Zhu Yib481de92007-09-25 17:54:57 -07001905
1906#ifdef IWL_MAC80211_DISABLE
1907 if (priv->assoc_network != NULL) {
1908 unsigned long flags;
1909
1910 period = priv->assoc_network->tim.tim_period;
1911 }
1912#endif /*IWL_MAC80211_DISABLE */
1913 skip = range[mode].no_dtim;
1914
1915 if (period == 0) {
1916 period = 1;
1917 skip = 0;
1918 }
1919
1920 if (skip == 0) {
1921 max_sleep = period;
1922 cmd->flags &= ~IWL_POWER_SLEEP_OVER_DTIM_MSK;
1923 } else {
1924 __le32 slp_itrvl = cmd->sleep_interval[IWL_POWER_VEC_SIZE - 1];
1925 max_sleep = (le32_to_cpu(slp_itrvl) / period) * period;
1926 cmd->flags |= IWL_POWER_SLEEP_OVER_DTIM_MSK;
1927 }
1928
1929 for (i = 0; i < IWL_POWER_VEC_SIZE; i++) {
1930 if (le32_to_cpu(cmd->sleep_interval[i]) > max_sleep)
1931 cmd->sleep_interval[i] = cpu_to_le32(max_sleep);
1932 }
1933
1934 IWL_DEBUG_POWER("Flags value = 0x%08X\n", cmd->flags);
1935 IWL_DEBUG_POWER("Tx timeout = %u\n", le32_to_cpu(cmd->tx_data_timeout));
1936 IWL_DEBUG_POWER("Rx timeout = %u\n", le32_to_cpu(cmd->rx_data_timeout));
1937 IWL_DEBUG_POWER("Sleep interval vector = { %d , %d , %d , %d , %d }\n",
1938 le32_to_cpu(cmd->sleep_interval[0]),
1939 le32_to_cpu(cmd->sleep_interval[1]),
1940 le32_to_cpu(cmd->sleep_interval[2]),
1941 le32_to_cpu(cmd->sleep_interval[3]),
1942 le32_to_cpu(cmd->sleep_interval[4]));
1943
1944 return rc;
1945}
1946
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001947static int iwl3945_send_power_mode(struct iwl3945_priv *priv, u32 mode)
Zhu Yib481de92007-09-25 17:54:57 -07001948{
John W. Linville9a62f732007-11-15 16:27:36 -05001949 u32 uninitialized_var(final_mode);
Zhu Yib481de92007-09-25 17:54:57 -07001950 int rc;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001951 struct iwl3945_powertable_cmd cmd;
Zhu Yib481de92007-09-25 17:54:57 -07001952
1953 /* If on battery, set to 3,
Ian Schram01ebd062007-10-25 17:15:22 +08001954 * if plugged into AC power, set to CAM ("continuously aware mode"),
Zhu Yib481de92007-09-25 17:54:57 -07001955 * else user level */
1956 switch (mode) {
1957 case IWL_POWER_BATTERY:
1958 final_mode = IWL_POWER_INDEX_3;
1959 break;
1960 case IWL_POWER_AC:
1961 final_mode = IWL_POWER_MODE_CAM;
1962 break;
1963 default:
1964 final_mode = mode;
1965 break;
1966 }
1967
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001968 iwl3945_update_power_cmd(priv, &cmd, final_mode);
Zhu Yib481de92007-09-25 17:54:57 -07001969
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001970 rc = iwl3945_send_cmd_pdu(priv, POWER_TABLE_CMD, sizeof(cmd), &cmd);
Zhu Yib481de92007-09-25 17:54:57 -07001971
1972 if (final_mode == IWL_POWER_MODE_CAM)
1973 clear_bit(STATUS_POWER_PMI, &priv->status);
1974 else
1975 set_bit(STATUS_POWER_PMI, &priv->status);
1976
1977 return rc;
1978}
1979
Zhu Yib481de92007-09-25 17:54:57 -07001980/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001981 * iwl3945_scan_cancel - Cancel any currently executing HW scan
Zhu Yib481de92007-09-25 17:54:57 -07001982 *
1983 * NOTE: priv->mutex is not required before calling this function
1984 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001985static int iwl3945_scan_cancel(struct iwl3945_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001986{
1987 if (!test_bit(STATUS_SCAN_HW, &priv->status)) {
1988 clear_bit(STATUS_SCANNING, &priv->status);
1989 return 0;
1990 }
1991
1992 if (test_bit(STATUS_SCANNING, &priv->status)) {
1993 if (!test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
1994 IWL_DEBUG_SCAN("Queuing scan abort.\n");
1995 set_bit(STATUS_SCAN_ABORTING, &priv->status);
1996 queue_work(priv->workqueue, &priv->abort_scan);
1997
1998 } else
1999 IWL_DEBUG_SCAN("Scan abort already in progress.\n");
2000
2001 return test_bit(STATUS_SCANNING, &priv->status);
2002 }
2003
2004 return 0;
2005}
2006
2007/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002008 * iwl3945_scan_cancel_timeout - Cancel any currently executing HW scan
Zhu Yib481de92007-09-25 17:54:57 -07002009 * @ms: amount of time to wait (in milliseconds) for scan to abort
2010 *
2011 * NOTE: priv->mutex must be held before calling this function
2012 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002013static int iwl3945_scan_cancel_timeout(struct iwl3945_priv *priv, unsigned long ms)
Zhu Yib481de92007-09-25 17:54:57 -07002014{
2015 unsigned long now = jiffies;
2016 int ret;
2017
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002018 ret = iwl3945_scan_cancel(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002019 if (ret && ms) {
2020 mutex_unlock(&priv->mutex);
2021 while (!time_after(jiffies, now + msecs_to_jiffies(ms)) &&
2022 test_bit(STATUS_SCANNING, &priv->status))
2023 msleep(1);
2024 mutex_lock(&priv->mutex);
2025
2026 return test_bit(STATUS_SCANNING, &priv->status);
2027 }
2028
2029 return ret;
2030}
2031
Zhu Yib481de92007-09-25 17:54:57 -07002032#define MAX_UCODE_BEACON_INTERVAL 1024
2033#define INTEL_CONN_LISTEN_INTERVAL __constant_cpu_to_le16(0xA)
2034
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002035static __le16 iwl3945_adjust_beacon_interval(u16 beacon_val)
Zhu Yib481de92007-09-25 17:54:57 -07002036{
2037 u16 new_val = 0;
2038 u16 beacon_factor = 0;
2039
2040 beacon_factor =
2041 (beacon_val + MAX_UCODE_BEACON_INTERVAL)
2042 / MAX_UCODE_BEACON_INTERVAL;
2043 new_val = beacon_val / beacon_factor;
2044
2045 return cpu_to_le16(new_val);
2046}
2047
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002048static void iwl3945_setup_rxon_timing(struct iwl3945_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002049{
2050 u64 interval_tm_unit;
2051 u64 tsf, result;
2052 unsigned long flags;
2053 struct ieee80211_conf *conf = NULL;
2054 u16 beacon_int = 0;
2055
2056 conf = ieee80211_get_hw_conf(priv->hw);
2057
2058 spin_lock_irqsave(&priv->lock, flags);
2059 priv->rxon_timing.timestamp.dw[1] = cpu_to_le32(priv->timestamp1);
2060 priv->rxon_timing.timestamp.dw[0] = cpu_to_le32(priv->timestamp0);
2061
2062 priv->rxon_timing.listen_interval = INTEL_CONN_LISTEN_INTERVAL;
2063
2064 tsf = priv->timestamp1;
2065 tsf = ((tsf << 32) | priv->timestamp0);
2066
2067 beacon_int = priv->beacon_int;
2068 spin_unlock_irqrestore(&priv->lock, flags);
2069
Johannes Berg05c914f2008-09-11 00:01:58 +02002070 if (priv->iw_mode == NL80211_IFTYPE_STATION) {
Zhu Yib481de92007-09-25 17:54:57 -07002071 if (beacon_int == 0) {
2072 priv->rxon_timing.beacon_interval = cpu_to_le16(100);
2073 priv->rxon_timing.beacon_init_val = cpu_to_le32(102400);
2074 } else {
2075 priv->rxon_timing.beacon_interval =
2076 cpu_to_le16(beacon_int);
2077 priv->rxon_timing.beacon_interval =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002078 iwl3945_adjust_beacon_interval(
Zhu Yib481de92007-09-25 17:54:57 -07002079 le16_to_cpu(priv->rxon_timing.beacon_interval));
2080 }
2081
2082 priv->rxon_timing.atim_window = 0;
2083 } else {
2084 priv->rxon_timing.beacon_interval =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002085 iwl3945_adjust_beacon_interval(conf->beacon_int);
Zhu Yib481de92007-09-25 17:54:57 -07002086 /* TODO: we need to get atim_window from upper stack
2087 * for now we set to 0 */
2088 priv->rxon_timing.atim_window = 0;
2089 }
2090
2091 interval_tm_unit =
2092 (le16_to_cpu(priv->rxon_timing.beacon_interval) * 1024);
2093 result = do_div(tsf, interval_tm_unit);
2094 priv->rxon_timing.beacon_init_val =
2095 cpu_to_le32((u32) ((u64) interval_tm_unit - result));
2096
2097 IWL_DEBUG_ASSOC
2098 ("beacon interval %d beacon timer %d beacon tim %d\n",
2099 le16_to_cpu(priv->rxon_timing.beacon_interval),
2100 le32_to_cpu(priv->rxon_timing.beacon_init_val),
2101 le16_to_cpu(priv->rxon_timing.atim_window));
2102}
2103
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002104static int iwl3945_scan_initiate(struct iwl3945_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002105{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002106 if (!iwl3945_is_ready_rf(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07002107 IWL_DEBUG_SCAN("Aborting scan due to not ready.\n");
2108 return -EIO;
2109 }
2110
2111 if (test_bit(STATUS_SCANNING, &priv->status)) {
2112 IWL_DEBUG_SCAN("Scan already in progress.\n");
2113 return -EAGAIN;
2114 }
2115
2116 if (test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
2117 IWL_DEBUG_SCAN("Scan request while abort pending. "
2118 "Queuing.\n");
2119 return -EAGAIN;
2120 }
2121
2122 IWL_DEBUG_INFO("Starting scan...\n");
Ron Rindjunsky66b50042008-06-25 16:46:31 +08002123 if (priv->cfg->sku & IWL_SKU_G)
2124 priv->scan_bands |= BIT(IEEE80211_BAND_2GHZ);
2125 if (priv->cfg->sku & IWL_SKU_A)
2126 priv->scan_bands |= BIT(IEEE80211_BAND_5GHZ);
Zhu Yib481de92007-09-25 17:54:57 -07002127 set_bit(STATUS_SCANNING, &priv->status);
2128 priv->scan_start = jiffies;
2129 priv->scan_pass_start = priv->scan_start;
2130
2131 queue_work(priv->workqueue, &priv->request_scan);
2132
2133 return 0;
2134}
2135
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002136static int iwl3945_set_rxon_hwcrypto(struct iwl3945_priv *priv, int hw_decrypt)
Zhu Yib481de92007-09-25 17:54:57 -07002137{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002138 struct iwl3945_rxon_cmd *rxon = &priv->staging_rxon;
Zhu Yib481de92007-09-25 17:54:57 -07002139
2140 if (hw_decrypt)
2141 rxon->filter_flags &= ~RXON_FILTER_DIS_DECRYPT_MSK;
2142 else
2143 rxon->filter_flags |= RXON_FILTER_DIS_DECRYPT_MSK;
2144
2145 return 0;
2146}
2147
Johannes Berg8318d782008-01-24 19:38:38 +01002148static void iwl3945_set_flags_for_phymode(struct iwl3945_priv *priv,
2149 enum ieee80211_band band)
Zhu Yib481de92007-09-25 17:54:57 -07002150{
Johannes Berg8318d782008-01-24 19:38:38 +01002151 if (band == IEEE80211_BAND_5GHZ) {
Zhu Yib481de92007-09-25 17:54:57 -07002152 priv->staging_rxon.flags &=
2153 ~(RXON_FLG_BAND_24G_MSK | RXON_FLG_AUTO_DETECT_MSK
2154 | RXON_FLG_CCK_MSK);
2155 priv->staging_rxon.flags |= RXON_FLG_SHORT_SLOT_MSK;
2156 } else {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002157 /* Copied from iwl3945_bg_post_associate() */
Zhu Yib481de92007-09-25 17:54:57 -07002158 if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_SLOT_TIME)
2159 priv->staging_rxon.flags |= RXON_FLG_SHORT_SLOT_MSK;
2160 else
2161 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
2162
Johannes Berg05c914f2008-09-11 00:01:58 +02002163 if (priv->iw_mode == NL80211_IFTYPE_ADHOC)
Zhu Yib481de92007-09-25 17:54:57 -07002164 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
2165
2166 priv->staging_rxon.flags |= RXON_FLG_BAND_24G_MSK;
2167 priv->staging_rxon.flags |= RXON_FLG_AUTO_DETECT_MSK;
2168 priv->staging_rxon.flags &= ~RXON_FLG_CCK_MSK;
2169 }
2170}
2171
2172/*
Ian Schram01ebd062007-10-25 17:15:22 +08002173 * initialize rxon structure with default values from eeprom
Zhu Yib481de92007-09-25 17:54:57 -07002174 */
Zhu, Yi60294de2008-10-29 14:05:45 -07002175static void iwl3945_connection_init_rx_config(struct iwl3945_priv *priv,
2176 int mode)
Zhu Yib481de92007-09-25 17:54:57 -07002177{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002178 const struct iwl3945_channel_info *ch_info;
Zhu Yib481de92007-09-25 17:54:57 -07002179
2180 memset(&priv->staging_rxon, 0, sizeof(priv->staging_rxon));
2181
Zhu, Yi60294de2008-10-29 14:05:45 -07002182 switch (mode) {
Johannes Berg05c914f2008-09-11 00:01:58 +02002183 case NL80211_IFTYPE_AP:
Zhu Yib481de92007-09-25 17:54:57 -07002184 priv->staging_rxon.dev_type = RXON_DEV_TYPE_AP;
2185 break;
2186
Johannes Berg05c914f2008-09-11 00:01:58 +02002187 case NL80211_IFTYPE_STATION:
Zhu Yib481de92007-09-25 17:54:57 -07002188 priv->staging_rxon.dev_type = RXON_DEV_TYPE_ESS;
2189 priv->staging_rxon.filter_flags = RXON_FILTER_ACCEPT_GRP_MSK;
2190 break;
2191
Johannes Berg05c914f2008-09-11 00:01:58 +02002192 case NL80211_IFTYPE_ADHOC:
Zhu Yib481de92007-09-25 17:54:57 -07002193 priv->staging_rxon.dev_type = RXON_DEV_TYPE_IBSS;
2194 priv->staging_rxon.flags = RXON_FLG_SHORT_PREAMBLE_MSK;
2195 priv->staging_rxon.filter_flags = RXON_FILTER_BCON_AWARE_MSK |
2196 RXON_FILTER_ACCEPT_GRP_MSK;
2197 break;
2198
Johannes Berg05c914f2008-09-11 00:01:58 +02002199 case NL80211_IFTYPE_MONITOR:
Zhu Yib481de92007-09-25 17:54:57 -07002200 priv->staging_rxon.dev_type = RXON_DEV_TYPE_SNIFFER;
2201 priv->staging_rxon.filter_flags = RXON_FILTER_PROMISC_MSK |
2202 RXON_FILTER_CTL2HOST_MSK | RXON_FILTER_ACCEPT_GRP_MSK;
2203 break;
Tomas Winkler69dc5d92008-03-25 16:33:41 -07002204 default:
Zhu, Yi60294de2008-10-29 14:05:45 -07002205 IWL_ERROR("Unsupported interface type %d\n", mode);
Tomas Winkler69dc5d92008-03-25 16:33:41 -07002206 break;
Zhu Yib481de92007-09-25 17:54:57 -07002207 }
2208
2209#if 0
2210 /* TODO: Figure out when short_preamble would be set and cache from
2211 * that */
2212 if (!hw_to_local(priv->hw)->short_preamble)
2213 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_PREAMBLE_MSK;
2214 else
2215 priv->staging_rxon.flags |= RXON_FLG_SHORT_PREAMBLE_MSK;
2216#endif
2217
Johannes Berg8318d782008-01-24 19:38:38 +01002218 ch_info = iwl3945_get_channel_info(priv, priv->band,
Rick Farrington25b3f572008-06-30 17:23:28 +08002219 le16_to_cpu(priv->active_rxon.channel));
Zhu Yib481de92007-09-25 17:54:57 -07002220
2221 if (!ch_info)
2222 ch_info = &priv->channel_info[0];
2223
2224 /*
2225 * in some case A channels are all non IBSS
2226 * in this case force B/G channel
2227 */
Zhu, Yi60294de2008-10-29 14:05:45 -07002228 if ((mode == NL80211_IFTYPE_ADHOC) && !(is_channel_ibss(ch_info)))
Zhu Yib481de92007-09-25 17:54:57 -07002229 ch_info = &priv->channel_info[0];
2230
2231 priv->staging_rxon.channel = cpu_to_le16(ch_info->channel);
2232 if (is_channel_a_band(ch_info))
Johannes Berg8318d782008-01-24 19:38:38 +01002233 priv->band = IEEE80211_BAND_5GHZ;
Zhu Yib481de92007-09-25 17:54:57 -07002234 else
Johannes Berg8318d782008-01-24 19:38:38 +01002235 priv->band = IEEE80211_BAND_2GHZ;
Zhu Yib481de92007-09-25 17:54:57 -07002236
Johannes Berg8318d782008-01-24 19:38:38 +01002237 iwl3945_set_flags_for_phymode(priv, priv->band);
Zhu Yib481de92007-09-25 17:54:57 -07002238
2239 priv->staging_rxon.ofdm_basic_rates =
2240 (IWL_OFDM_RATES_MASK >> IWL_FIRST_OFDM_RATE) & 0xFF;
2241 priv->staging_rxon.cck_basic_rates =
2242 (IWL_CCK_RATES_MASK >> IWL_FIRST_CCK_RATE) & 0xF;
2243}
2244
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002245static int iwl3945_set_mode(struct iwl3945_priv *priv, int mode)
Zhu Yib481de92007-09-25 17:54:57 -07002246{
Johannes Berg05c914f2008-09-11 00:01:58 +02002247 if (mode == NL80211_IFTYPE_ADHOC) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002248 const struct iwl3945_channel_info *ch_info;
Zhu Yib481de92007-09-25 17:54:57 -07002249
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002250 ch_info = iwl3945_get_channel_info(priv,
Johannes Berg8318d782008-01-24 19:38:38 +01002251 priv->band,
Zhu Yib481de92007-09-25 17:54:57 -07002252 le16_to_cpu(priv->staging_rxon.channel));
2253
2254 if (!ch_info || !is_channel_ibss(ch_info)) {
2255 IWL_ERROR("channel %d not IBSS channel\n",
2256 le16_to_cpu(priv->staging_rxon.channel));
2257 return -EINVAL;
2258 }
2259 }
2260
Zhu, Yi60294de2008-10-29 14:05:45 -07002261 iwl3945_connection_init_rx_config(priv, mode);
Zhu Yib481de92007-09-25 17:54:57 -07002262 memcpy(priv->staging_rxon.node_addr, priv->mac_addr, ETH_ALEN);
2263
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002264 iwl3945_clear_stations_table(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002265
Tomas Winklera96a27f2008-10-23 23:48:56 -07002266 /* don't commit rxon if rf-kill is on*/
Mohamed Abbasfde35712007-11-29 11:10:15 +08002267 if (!iwl3945_is_ready_rf(priv))
2268 return -EAGAIN;
2269
2270 cancel_delayed_work(&priv->scan_check);
2271 if (iwl3945_scan_cancel_timeout(priv, 100)) {
2272 IWL_WARNING("Aborted scan still in progress after 100ms\n");
2273 IWL_DEBUG_MAC80211("leaving - scan abort failed.\n");
2274 return -EAGAIN;
2275 }
2276
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002277 iwl3945_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002278
2279 return 0;
2280}
2281
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002282static void iwl3945_build_tx_cmd_hwcrypto(struct iwl3945_priv *priv,
Johannes Berge039fa42008-05-15 12:55:29 +02002283 struct ieee80211_tx_info *info,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002284 struct iwl3945_cmd *cmd,
Zhu Yib481de92007-09-25 17:54:57 -07002285 struct sk_buff *skb_frag,
2286 int last_frag)
2287{
Ivo van Doorn1c014422008-04-17 19:41:02 +02002288 struct iwl3945_hw_key *keyinfo =
Johannes Berge039fa42008-05-15 12:55:29 +02002289 &priv->stations[info->control.hw_key->hw_key_idx].keyinfo;
Zhu Yib481de92007-09-25 17:54:57 -07002290
2291 switch (keyinfo->alg) {
2292 case ALG_CCMP:
2293 cmd->cmd.tx.sec_ctl = TX_CMD_SEC_CCM;
2294 memcpy(cmd->cmd.tx.key, keyinfo->key, keyinfo->keylen);
Tomas Winklera96a27f2008-10-23 23:48:56 -07002295 IWL_DEBUG_TX("tx_cmd with AES hwcrypto\n");
Zhu Yib481de92007-09-25 17:54:57 -07002296 break;
2297
2298 case ALG_TKIP:
2299#if 0
2300 cmd->cmd.tx.sec_ctl = TX_CMD_SEC_TKIP;
2301
2302 if (last_frag)
2303 memcpy(cmd->cmd.tx.tkip_mic.byte, skb_frag->tail - 8,
2304 8);
2305 else
2306 memset(cmd->cmd.tx.tkip_mic.byte, 0, 8);
2307#endif
2308 break;
2309
2310 case ALG_WEP:
2311 cmd->cmd.tx.sec_ctl = TX_CMD_SEC_WEP |
Johannes Berge039fa42008-05-15 12:55:29 +02002312 (info->control.hw_key->hw_key_idx & TX_CMD_SEC_MSK) << TX_CMD_SEC_SHIFT;
Zhu Yib481de92007-09-25 17:54:57 -07002313
2314 if (keyinfo->keylen == 13)
2315 cmd->cmd.tx.sec_ctl |= TX_CMD_SEC_KEY128;
2316
2317 memcpy(&cmd->cmd.tx.key[3], keyinfo->key, keyinfo->keylen);
2318
2319 IWL_DEBUG_TX("Configuring packet for WEP encryption "
Johannes Berge039fa42008-05-15 12:55:29 +02002320 "with key %d\n", info->control.hw_key->hw_key_idx);
Zhu Yib481de92007-09-25 17:54:57 -07002321 break;
2322
Zhu Yib481de92007-09-25 17:54:57 -07002323 default:
2324 printk(KERN_ERR "Unknown encode alg %d\n", keyinfo->alg);
2325 break;
2326 }
2327}
2328
2329/*
2330 * handle build REPLY_TX command notification.
2331 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002332static void iwl3945_build_tx_cmd_basic(struct iwl3945_priv *priv,
2333 struct iwl3945_cmd *cmd,
Johannes Berge039fa42008-05-15 12:55:29 +02002334 struct ieee80211_tx_info *info,
Zhu Yib481de92007-09-25 17:54:57 -07002335 struct ieee80211_hdr *hdr,
2336 int is_unicast, u8 std_id)
2337{
Harvey Harrisonfd7c8a42008-06-11 14:21:56 -07002338 __le16 fc = hdr->frame_control;
Zhu Yib481de92007-09-25 17:54:57 -07002339 __le32 tx_flags = cmd->cmd.tx.tx_flags;
Johannes Berge6a98542008-10-21 12:40:02 +02002340 u8 rc_flags = info->control.rates[0].flags;
Zhu Yib481de92007-09-25 17:54:57 -07002341
2342 cmd->cmd.tx.stop_time.life_time = TX_CMD_LIFE_TIME_INFINITE;
Johannes Berge039fa42008-05-15 12:55:29 +02002343 if (!(info->flags & IEEE80211_TX_CTL_NO_ACK)) {
Zhu Yib481de92007-09-25 17:54:57 -07002344 tx_flags |= TX_CMD_FLG_ACK_MSK;
Harvey Harrisonfd7c8a42008-06-11 14:21:56 -07002345 if (ieee80211_is_mgmt(fc))
Zhu Yib481de92007-09-25 17:54:57 -07002346 tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
Harvey Harrisonfd7c8a42008-06-11 14:21:56 -07002347 if (ieee80211_is_probe_resp(fc) &&
Zhu Yib481de92007-09-25 17:54:57 -07002348 !(le16_to_cpu(hdr->seq_ctrl) & 0xf))
2349 tx_flags |= TX_CMD_FLG_TSF_MSK;
2350 } else {
2351 tx_flags &= (~TX_CMD_FLG_ACK_MSK);
2352 tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
2353 }
2354
2355 cmd->cmd.tx.sta_id = std_id;
Harvey Harrison8b7b1e02008-06-11 14:21:56 -07002356 if (ieee80211_has_morefrags(fc))
Zhu Yib481de92007-09-25 17:54:57 -07002357 tx_flags |= TX_CMD_FLG_MORE_FRAG_MSK;
2358
Harvey Harrisonfd7c8a42008-06-11 14:21:56 -07002359 if (ieee80211_is_data_qos(fc)) {
2360 u8 *qc = ieee80211_get_qos_ctl(hdr);
Tomas Winkler54dbb522008-05-15 13:54:06 +08002361 cmd->cmd.tx.tid_tspec = qc[0] & 0xf;
Zhu Yib481de92007-09-25 17:54:57 -07002362 tx_flags &= ~TX_CMD_FLG_SEQ_CTL_MSK;
Tomas Winkler54dbb522008-05-15 13:54:06 +08002363 } else {
Zhu Yib481de92007-09-25 17:54:57 -07002364 tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
Tomas Winkler54dbb522008-05-15 13:54:06 +08002365 }
Zhu Yib481de92007-09-25 17:54:57 -07002366
Johannes Berge6a98542008-10-21 12:40:02 +02002367 if (rc_flags & IEEE80211_TX_RC_USE_RTS_CTS) {
Zhu Yib481de92007-09-25 17:54:57 -07002368 tx_flags |= TX_CMD_FLG_RTS_MSK;
2369 tx_flags &= ~TX_CMD_FLG_CTS_MSK;
Johannes Berge6a98542008-10-21 12:40:02 +02002370 } else if (rc_flags & IEEE80211_TX_RC_USE_CTS_PROTECT) {
Zhu Yib481de92007-09-25 17:54:57 -07002371 tx_flags &= ~TX_CMD_FLG_RTS_MSK;
2372 tx_flags |= TX_CMD_FLG_CTS_MSK;
2373 }
2374
2375 if ((tx_flags & TX_CMD_FLG_RTS_MSK) || (tx_flags & TX_CMD_FLG_CTS_MSK))
2376 tx_flags |= TX_CMD_FLG_FULL_TXOP_PROT_MSK;
2377
2378 tx_flags &= ~(TX_CMD_FLG_ANT_SEL_MSK);
Harvey Harrisonfd7c8a42008-06-11 14:21:56 -07002379 if (ieee80211_is_mgmt(fc)) {
2380 if (ieee80211_is_assoc_req(fc) || ieee80211_is_reassoc_req(fc))
Ian Schrambc434dd2007-10-25 17:15:29 +08002381 cmd->cmd.tx.timeout.pm_frame_timeout = cpu_to_le16(3);
Zhu Yib481de92007-09-25 17:54:57 -07002382 else
Ian Schrambc434dd2007-10-25 17:15:29 +08002383 cmd->cmd.tx.timeout.pm_frame_timeout = cpu_to_le16(2);
Mohamed Abbasab53d8a2008-03-25 16:33:36 -07002384 } else {
Zhu Yib481de92007-09-25 17:54:57 -07002385 cmd->cmd.tx.timeout.pm_frame_timeout = 0;
Mohamed Abbasab53d8a2008-03-25 16:33:36 -07002386#ifdef CONFIG_IWL3945_LEDS
2387 priv->rxtxpackets += le16_to_cpu(cmd->cmd.tx.len);
2388#endif
2389 }
Zhu Yib481de92007-09-25 17:54:57 -07002390
2391 cmd->cmd.tx.driver_txop = 0;
2392 cmd->cmd.tx.tx_flags = tx_flags;
2393 cmd->cmd.tx.next_frame_len = 0;
2394}
2395
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002396/**
2397 * iwl3945_get_sta_id - Find station's index within station table
2398 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002399static int iwl3945_get_sta_id(struct iwl3945_priv *priv, struct ieee80211_hdr *hdr)
Zhu Yib481de92007-09-25 17:54:57 -07002400{
2401 int sta_id;
2402 u16 fc = le16_to_cpu(hdr->frame_control);
2403
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002404 /* If this frame is broadcast or management, use broadcast station id */
Zhu Yib481de92007-09-25 17:54:57 -07002405 if (((fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_DATA) ||
2406 is_multicast_ether_addr(hdr->addr1))
2407 return priv->hw_setting.bcast_sta_id;
2408
2409 switch (priv->iw_mode) {
2410
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002411 /* If we are a client station in a BSS network, use the special
2412 * AP station entry (that's the only station we communicate with) */
Johannes Berg05c914f2008-09-11 00:01:58 +02002413 case NL80211_IFTYPE_STATION:
Zhu Yib481de92007-09-25 17:54:57 -07002414 return IWL_AP_ID;
2415
2416 /* If we are an AP, then find the station, or use BCAST */
Johannes Berg05c914f2008-09-11 00:01:58 +02002417 case NL80211_IFTYPE_AP:
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002418 sta_id = iwl3945_hw_find_station(priv, hdr->addr1);
Zhu Yib481de92007-09-25 17:54:57 -07002419 if (sta_id != IWL_INVALID_STATION)
2420 return sta_id;
2421 return priv->hw_setting.bcast_sta_id;
2422
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002423 /* If this frame is going out to an IBSS network, find the station,
2424 * or create a new station table entry */
Johannes Berg05c914f2008-09-11 00:01:58 +02002425 case NL80211_IFTYPE_ADHOC: {
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002426 /* Create new station table entry */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002427 sta_id = iwl3945_hw_find_station(priv, hdr->addr1);
Zhu Yib481de92007-09-25 17:54:57 -07002428 if (sta_id != IWL_INVALID_STATION)
2429 return sta_id;
2430
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002431 sta_id = iwl3945_add_station(priv, hdr->addr1, 0, CMD_ASYNC);
Zhu Yib481de92007-09-25 17:54:57 -07002432
2433 if (sta_id != IWL_INVALID_STATION)
2434 return sta_id;
2435
Johannes Berge1749612008-10-27 15:59:26 -07002436 IWL_DEBUG_DROP("Station %pM not in station map. "
Zhu Yib481de92007-09-25 17:54:57 -07002437 "Defaulting to broadcast...\n",
Johannes Berge1749612008-10-27 15:59:26 -07002438 hdr->addr1);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002439 iwl3945_print_hex_dump(IWL_DL_DROP, (u8 *) hdr, sizeof(*hdr));
Zhu Yib481de92007-09-25 17:54:57 -07002440 return priv->hw_setting.bcast_sta_id;
Joe Perches0795af52007-10-03 17:59:30 -07002441 }
Stefanik Gábor914233d2008-06-30 17:23:30 +08002442 /* If we are in monitor mode, use BCAST. This is required for
2443 * packet injection. */
Johannes Berg05c914f2008-09-11 00:01:58 +02002444 case NL80211_IFTYPE_MONITOR:
Stefanik Gábor914233d2008-06-30 17:23:30 +08002445 return priv->hw_setting.bcast_sta_id;
2446
Zhu Yib481de92007-09-25 17:54:57 -07002447 default:
Jiri Slaby6f147922008-08-11 23:49:41 +02002448 IWL_WARNING("Unknown mode of operation: %d\n", priv->iw_mode);
Zhu Yib481de92007-09-25 17:54:57 -07002449 return priv->hw_setting.bcast_sta_id;
2450 }
2451}
2452
2453/*
2454 * start REPLY_TX command process
2455 */
Johannes Berge039fa42008-05-15 12:55:29 +02002456static int iwl3945_tx_skb(struct iwl3945_priv *priv, struct sk_buff *skb)
Zhu Yib481de92007-09-25 17:54:57 -07002457{
2458 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
Johannes Berge039fa42008-05-15 12:55:29 +02002459 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002460 struct iwl3945_tfd_frame *tfd;
Zhu Yib481de92007-09-25 17:54:57 -07002461 u32 *control_flags;
Johannes Berge2530082008-05-17 00:57:14 +02002462 int txq_id = skb_get_queue_mapping(skb);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002463 struct iwl3945_tx_queue *txq = NULL;
2464 struct iwl3945_queue *q = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07002465 dma_addr_t phys_addr;
2466 dma_addr_t txcmd_phys;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002467 struct iwl3945_cmd *out_cmd = NULL;
Tomas Winkler54dbb522008-05-15 13:54:06 +08002468 u16 len, idx, len_org, hdr_len;
2469 u8 id;
2470 u8 unicast;
Zhu Yib481de92007-09-25 17:54:57 -07002471 u8 sta_id;
Tomas Winkler54dbb522008-05-15 13:54:06 +08002472 u8 tid = 0;
Zhu Yib481de92007-09-25 17:54:57 -07002473 u16 seq_number = 0;
Harvey Harrisonfd7c8a42008-06-11 14:21:56 -07002474 __le16 fc;
Zhu Yib481de92007-09-25 17:54:57 -07002475 u8 wait_write_ptr = 0;
Tomas Winkler54dbb522008-05-15 13:54:06 +08002476 u8 *qc = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07002477 unsigned long flags;
2478 int rc;
2479
2480 spin_lock_irqsave(&priv->lock, flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002481 if (iwl3945_is_rfkill(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07002482 IWL_DEBUG_DROP("Dropping - RF KILL\n");
2483 goto drop_unlock;
2484 }
2485
Johannes Berge039fa42008-05-15 12:55:29 +02002486 if ((ieee80211_get_tx_rate(priv->hw, info)->hw_value & 0xFF) == IWL_INVALID_RATE) {
Zhu Yib481de92007-09-25 17:54:57 -07002487 IWL_ERROR("ERROR: No TX rate available.\n");
2488 goto drop_unlock;
2489 }
2490
2491 unicast = !is_multicast_ether_addr(hdr->addr1);
2492 id = 0;
2493
Harvey Harrisonfd7c8a42008-06-11 14:21:56 -07002494 fc = hdr->frame_control;
Zhu Yib481de92007-09-25 17:54:57 -07002495
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08002496#ifdef CONFIG_IWL3945_DEBUG
Zhu Yib481de92007-09-25 17:54:57 -07002497 if (ieee80211_is_auth(fc))
2498 IWL_DEBUG_TX("Sending AUTH frame\n");
Harvey Harrisonfd7c8a42008-06-11 14:21:56 -07002499 else if (ieee80211_is_assoc_req(fc))
Zhu Yib481de92007-09-25 17:54:57 -07002500 IWL_DEBUG_TX("Sending ASSOC frame\n");
Harvey Harrisonfd7c8a42008-06-11 14:21:56 -07002501 else if (ieee80211_is_reassoc_req(fc))
Zhu Yib481de92007-09-25 17:54:57 -07002502 IWL_DEBUG_TX("Sending REASSOC frame\n");
2503#endif
2504
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08002505 /* drop all data frame if we are not associated */
Stefanik Gábor914233d2008-06-30 17:23:30 +08002506 if (ieee80211_is_data(fc) &&
Johannes Berg05c914f2008-09-11 00:01:58 +02002507 (priv->iw_mode != NL80211_IFTYPE_MONITOR) && /* packet injection */
Stefanik Gábor914233d2008-06-30 17:23:30 +08002508 (!iwl3945_is_associated(priv) ||
Johannes Berg05c914f2008-09-11 00:01:58 +02002509 ((priv->iw_mode == NL80211_IFTYPE_STATION) && !priv->assoc_id))) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002510 IWL_DEBUG_DROP("Dropping - !iwl3945_is_associated\n");
Zhu Yib481de92007-09-25 17:54:57 -07002511 goto drop_unlock;
2512 }
2513
2514 spin_unlock_irqrestore(&priv->lock, flags);
2515
Harvey Harrison7294ec92008-07-15 18:43:59 -07002516 hdr_len = ieee80211_hdrlen(fc);
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002517
2518 /* Find (or create) index into station table for destination station */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002519 sta_id = iwl3945_get_sta_id(priv, hdr);
Zhu Yib481de92007-09-25 17:54:57 -07002520 if (sta_id == IWL_INVALID_STATION) {
Johannes Berge1749612008-10-27 15:59:26 -07002521 IWL_DEBUG_DROP("Dropping - INVALID STATION: %pM\n",
2522 hdr->addr1);
Zhu Yib481de92007-09-25 17:54:57 -07002523 goto drop;
2524 }
2525
2526 IWL_DEBUG_RATE("station Id %d\n", sta_id);
2527
Harvey Harrisonfd7c8a42008-06-11 14:21:56 -07002528 if (ieee80211_is_data_qos(fc)) {
2529 qc = ieee80211_get_qos_ctl(hdr);
Harvey Harrison7294ec92008-07-15 18:43:59 -07002530 tid = qc[0] & IEEE80211_QOS_CTL_TID_MASK;
Zhu Yib481de92007-09-25 17:54:57 -07002531 seq_number = priv->stations[sta_id].tid[tid].seq_number &
2532 IEEE80211_SCTL_SEQ;
2533 hdr->seq_ctrl = cpu_to_le16(seq_number) |
2534 (hdr->seq_ctrl &
2535 __constant_cpu_to_le16(IEEE80211_SCTL_FRAG));
2536 seq_number += 0x10;
2537 }
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002538
2539 /* Descriptor for chosen Tx queue */
Zhu Yib481de92007-09-25 17:54:57 -07002540 txq = &priv->txq[txq_id];
2541 q = &txq->q;
2542
2543 spin_lock_irqsave(&priv->lock, flags);
2544
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002545 /* Set up first empty TFD within this queue's circular TFD buffer */
Tomas Winklerfc4b6852007-10-25 17:15:24 +08002546 tfd = &txq->bd[q->write_ptr];
Zhu Yib481de92007-09-25 17:54:57 -07002547 memset(tfd, 0, sizeof(*tfd));
2548 control_flags = (u32 *) tfd;
Tomas Winklerfc4b6852007-10-25 17:15:24 +08002549 idx = get_cmd_index(q, q->write_ptr, 0);
Zhu Yib481de92007-09-25 17:54:57 -07002550
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002551 /* Set up driver data for this TFD */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002552 memset(&(txq->txb[q->write_ptr]), 0, sizeof(struct iwl3945_tx_info));
Tomas Winklerfc4b6852007-10-25 17:15:24 +08002553 txq->txb[q->write_ptr].skb[0] = skb;
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002554
2555 /* Init first empty entry in queue's array of Tx/cmd buffers */
Zhu Yib481de92007-09-25 17:54:57 -07002556 out_cmd = &txq->cmd[idx];
2557 memset(&out_cmd->hdr, 0, sizeof(out_cmd->hdr));
2558 memset(&out_cmd->cmd.tx, 0, sizeof(out_cmd->cmd.tx));
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002559
2560 /*
2561 * Set up the Tx-command (not MAC!) header.
2562 * Store the chosen Tx queue and TFD index within the sequence field;
2563 * after Tx, uCode's Tx response will return this value so driver can
2564 * locate the frame within the tx queue and do post-tx processing.
2565 */
Zhu Yib481de92007-09-25 17:54:57 -07002566 out_cmd->hdr.cmd = REPLY_TX;
2567 out_cmd->hdr.sequence = cpu_to_le16((u16)(QUEUE_TO_SEQ(txq_id) |
Tomas Winklerfc4b6852007-10-25 17:15:24 +08002568 INDEX_TO_SEQ(q->write_ptr)));
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002569
2570 /* Copy MAC header from skb into command buffer */
Zhu Yib481de92007-09-25 17:54:57 -07002571 memcpy(out_cmd->cmd.tx.hdr, hdr, hdr_len);
2572
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002573 /*
2574 * Use the first empty entry in this queue's command buffer array
2575 * to contain the Tx command and MAC header concatenated together
2576 * (payload data will be in another buffer).
2577 * Size of this varies, due to varying MAC header length.
2578 * If end is not dword aligned, we'll have 2 extra bytes at the end
2579 * of the MAC header (device reads on dword boundaries).
2580 * We'll tell device about this padding later.
2581 */
Zhu Yib481de92007-09-25 17:54:57 -07002582 len = priv->hw_setting.tx_cmd_len +
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002583 sizeof(struct iwl3945_cmd_header) + hdr_len;
Zhu Yib481de92007-09-25 17:54:57 -07002584
2585 len_org = len;
2586 len = (len + 3) & ~3;
2587
2588 if (len_org != len)
2589 len_org = 1;
2590 else
2591 len_org = 0;
2592
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002593 /* Physical address of this Tx command's header (not MAC header!),
2594 * within command buffer array. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002595 txcmd_phys = txq->dma_addr_cmd + sizeof(struct iwl3945_cmd) * idx +
2596 offsetof(struct iwl3945_cmd, hdr);
Zhu Yib481de92007-09-25 17:54:57 -07002597
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002598 /* Add buffer containing Tx command and MAC(!) header to TFD's
2599 * first entry */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002600 iwl3945_hw_txq_attach_buf_to_tfd(priv, tfd, txcmd_phys, len);
Zhu Yib481de92007-09-25 17:54:57 -07002601
Johannes Bergd0f09802008-07-29 11:32:07 +02002602 if (info->control.hw_key)
Johannes Berge039fa42008-05-15 12:55:29 +02002603 iwl3945_build_tx_cmd_hwcrypto(priv, info, out_cmd, skb, 0);
Zhu Yib481de92007-09-25 17:54:57 -07002604
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002605 /* Set up TFD's 2nd entry to point directly to remainder of skb,
2606 * if any (802.11 null frames have no payload). */
Zhu Yib481de92007-09-25 17:54:57 -07002607 len = skb->len - hdr_len;
2608 if (len) {
2609 phys_addr = pci_map_single(priv->pci_dev, skb->data + hdr_len,
2610 len, PCI_DMA_TODEVICE);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002611 iwl3945_hw_txq_attach_buf_to_tfd(priv, tfd, phys_addr, len);
Zhu Yib481de92007-09-25 17:54:57 -07002612 }
2613
Zhu Yib481de92007-09-25 17:54:57 -07002614 if (!len)
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002615 /* If there is no payload, then we use only one Tx buffer */
Zhu Yib481de92007-09-25 17:54:57 -07002616 *control_flags = TFD_CTL_COUNT_SET(1);
2617 else
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002618 /* Else use 2 buffers.
2619 * Tell 3945 about any padding after MAC header */
Zhu Yib481de92007-09-25 17:54:57 -07002620 *control_flags = TFD_CTL_COUNT_SET(2) |
2621 TFD_CTL_PAD_SET(U32_PAD(len));
2622
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002623 /* Total # bytes to be transmitted */
Zhu Yib481de92007-09-25 17:54:57 -07002624 len = (u16)skb->len;
2625 out_cmd->cmd.tx.len = cpu_to_le16(len);
2626
2627 /* TODO need this for burst mode later on */
Johannes Berge039fa42008-05-15 12:55:29 +02002628 iwl3945_build_tx_cmd_basic(priv, out_cmd, info, hdr, unicast, sta_id);
Zhu Yib481de92007-09-25 17:54:57 -07002629
2630 /* set is_hcca to 0; it probably will never be implemented */
Johannes Berge039fa42008-05-15 12:55:29 +02002631 iwl3945_hw_build_tx_cmd_rate(priv, out_cmd, info, hdr, sta_id, 0);
Zhu Yib481de92007-09-25 17:54:57 -07002632
2633 out_cmd->cmd.tx.tx_flags &= ~TX_CMD_FLG_ANT_A_MSK;
2634 out_cmd->cmd.tx.tx_flags &= ~TX_CMD_FLG_ANT_B_MSK;
2635
Harvey Harrison8b7b1e02008-06-11 14:21:56 -07002636 if (!ieee80211_has_morefrags(hdr->frame_control)) {
Zhu Yib481de92007-09-25 17:54:57 -07002637 txq->need_update = 1;
Tomas Winkler3ac7f142008-07-21 02:40:14 +03002638 if (qc)
Zhu Yib481de92007-09-25 17:54:57 -07002639 priv->stations[sta_id].tid[tid].seq_number = seq_number;
Zhu Yib481de92007-09-25 17:54:57 -07002640 } else {
2641 wait_write_ptr = 1;
2642 txq->need_update = 0;
2643 }
2644
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002645 iwl3945_print_hex_dump(IWL_DL_TX, out_cmd->cmd.payload,
Zhu Yib481de92007-09-25 17:54:57 -07002646 sizeof(out_cmd->cmd.tx));
2647
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002648 iwl3945_print_hex_dump(IWL_DL_TX, (u8 *)out_cmd->cmd.tx.hdr,
Harvey Harrison7294ec92008-07-15 18:43:59 -07002649 ieee80211_hdrlen(fc));
Zhu Yib481de92007-09-25 17:54:57 -07002650
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002651 /* Tell device the write index *just past* this latest filled TFD */
Tomas Winklerc54b6792008-03-06 17:36:53 -08002652 q->write_ptr = iwl_queue_inc_wrap(q->write_ptr, q->n_bd);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002653 rc = iwl3945_tx_queue_update_write_ptr(priv, txq);
Zhu Yib481de92007-09-25 17:54:57 -07002654 spin_unlock_irqrestore(&priv->lock, flags);
2655
2656 if (rc)
2657 return rc;
2658
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002659 if ((iwl3945_queue_space(q) < q->high_mark)
Zhu Yib481de92007-09-25 17:54:57 -07002660 && priv->mac80211_registered) {
2661 if (wait_write_ptr) {
2662 spin_lock_irqsave(&priv->lock, flags);
2663 txq->need_update = 1;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002664 iwl3945_tx_queue_update_write_ptr(priv, txq);
Zhu Yib481de92007-09-25 17:54:57 -07002665 spin_unlock_irqrestore(&priv->lock, flags);
2666 }
2667
Johannes Berge2530082008-05-17 00:57:14 +02002668 ieee80211_stop_queue(priv->hw, skb_get_queue_mapping(skb));
Zhu Yib481de92007-09-25 17:54:57 -07002669 }
2670
2671 return 0;
2672
2673drop_unlock:
2674 spin_unlock_irqrestore(&priv->lock, flags);
2675drop:
2676 return -1;
2677}
2678
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002679static void iwl3945_set_rate(struct iwl3945_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002680{
Johannes Berg8318d782008-01-24 19:38:38 +01002681 const struct ieee80211_supported_band *sband = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07002682 struct ieee80211_rate *rate;
2683 int i;
2684
Johannes Berg8318d782008-01-24 19:38:38 +01002685 sband = iwl3945_get_band(priv, priv->band);
2686 if (!sband) {
Saleem Abdulrasoolc4ba9622007-11-18 23:59:08 -08002687 IWL_ERROR("Failed to set rate: unable to get hw mode\n");
2688 return;
2689 }
Zhu Yib481de92007-09-25 17:54:57 -07002690
2691 priv->active_rate = 0;
2692 priv->active_rate_basic = 0;
2693
Johannes Berg8318d782008-01-24 19:38:38 +01002694 IWL_DEBUG_RATE("Setting rates for %s GHz\n",
2695 sband->band == IEEE80211_BAND_2GHZ ? "2.4" : "5");
Zhu Yib481de92007-09-25 17:54:57 -07002696
Johannes Berg8318d782008-01-24 19:38:38 +01002697 for (i = 0; i < sband->n_bitrates; i++) {
2698 rate = &sband->bitrates[i];
2699 if ((rate->hw_value < IWL_RATE_COUNT) &&
2700 !(rate->flags & IEEE80211_CHAN_DISABLED)) {
2701 IWL_DEBUG_RATE("Adding rate index %d (plcp %d)\n",
2702 rate->hw_value, iwl3945_rates[rate->hw_value].plcp);
2703 priv->active_rate |= (1 << rate->hw_value);
2704 }
Zhu Yib481de92007-09-25 17:54:57 -07002705 }
2706
2707 IWL_DEBUG_RATE("Set active_rate = %0x, active_rate_basic = %0x\n",
2708 priv->active_rate, priv->active_rate_basic);
2709
2710 /*
2711 * If a basic rate is configured, then use it (adding IWL_RATE_1M_MASK)
2712 * otherwise set it to the default of all CCK rates and 6, 12, 24 for
2713 * OFDM
2714 */
2715 if (priv->active_rate_basic & IWL_CCK_BASIC_RATES_MASK)
2716 priv->staging_rxon.cck_basic_rates =
2717 ((priv->active_rate_basic &
2718 IWL_CCK_RATES_MASK) >> IWL_FIRST_CCK_RATE) & 0xF;
2719 else
2720 priv->staging_rxon.cck_basic_rates =
2721 (IWL_CCK_BASIC_RATES_MASK >> IWL_FIRST_CCK_RATE) & 0xF;
2722
2723 if (priv->active_rate_basic & IWL_OFDM_BASIC_RATES_MASK)
2724 priv->staging_rxon.ofdm_basic_rates =
2725 ((priv->active_rate_basic &
2726 (IWL_OFDM_BASIC_RATES_MASK | IWL_RATE_6M_MASK)) >>
2727 IWL_FIRST_OFDM_RATE) & 0xFF;
2728 else
2729 priv->staging_rxon.ofdm_basic_rates =
2730 (IWL_OFDM_BASIC_RATES_MASK >> IWL_FIRST_OFDM_RATE) & 0xFF;
2731}
2732
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002733static void iwl3945_radio_kill_sw(struct iwl3945_priv *priv, int disable_radio)
Zhu Yib481de92007-09-25 17:54:57 -07002734{
2735 unsigned long flags;
2736
2737 if (!!disable_radio == test_bit(STATUS_RF_KILL_SW, &priv->status))
2738 return;
2739
2740 IWL_DEBUG_RF_KILL("Manual SW RF KILL set to: RADIO %s\n",
2741 disable_radio ? "OFF" : "ON");
2742
2743 if (disable_radio) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002744 iwl3945_scan_cancel(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002745 /* FIXME: This is a workaround for AP */
Johannes Berg05c914f2008-09-11 00:01:58 +02002746 if (priv->iw_mode != NL80211_IFTYPE_AP) {
Zhu Yib481de92007-09-25 17:54:57 -07002747 spin_lock_irqsave(&priv->lock, flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002748 iwl3945_write32(priv, CSR_UCODE_DRV_GP1_SET,
Zhu Yib481de92007-09-25 17:54:57 -07002749 CSR_UCODE_SW_BIT_RFKILL);
2750 spin_unlock_irqrestore(&priv->lock, flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002751 iwl3945_send_card_state(priv, CARD_STATE_CMD_DISABLE, 0);
Zhu Yib481de92007-09-25 17:54:57 -07002752 set_bit(STATUS_RF_KILL_SW, &priv->status);
2753 }
2754 return;
2755 }
2756
2757 spin_lock_irqsave(&priv->lock, flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002758 iwl3945_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
Zhu Yib481de92007-09-25 17:54:57 -07002759
2760 clear_bit(STATUS_RF_KILL_SW, &priv->status);
2761 spin_unlock_irqrestore(&priv->lock, flags);
2762
2763 /* wake up ucode */
2764 msleep(10);
2765
2766 spin_lock_irqsave(&priv->lock, flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002767 iwl3945_read32(priv, CSR_UCODE_DRV_GP1);
2768 if (!iwl3945_grab_nic_access(priv))
2769 iwl3945_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002770 spin_unlock_irqrestore(&priv->lock, flags);
2771
2772 if (test_bit(STATUS_RF_KILL_HW, &priv->status)) {
2773 IWL_DEBUG_RF_KILL("Can not turn radio back on - "
2774 "disabled by HW switch\n");
2775 return;
2776 }
2777
Zhu Yi808e72a2008-06-12 09:47:17 +08002778 if (priv->is_open)
2779 queue_work(priv->workqueue, &priv->restart);
Zhu Yib481de92007-09-25 17:54:57 -07002780 return;
2781}
2782
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002783void iwl3945_set_decrypted_flag(struct iwl3945_priv *priv, struct sk_buff *skb,
Zhu Yib481de92007-09-25 17:54:57 -07002784 u32 decrypt_res, struct ieee80211_rx_status *stats)
2785{
2786 u16 fc =
2787 le16_to_cpu(((struct ieee80211_hdr *)skb->data)->frame_control);
2788
2789 if (priv->active_rxon.filter_flags & RXON_FILTER_DIS_DECRYPT_MSK)
2790 return;
2791
2792 if (!(fc & IEEE80211_FCTL_PROTECTED))
2793 return;
2794
2795 IWL_DEBUG_RX("decrypt_res:0x%x\n", decrypt_res);
2796 switch (decrypt_res & RX_RES_STATUS_SEC_TYPE_MSK) {
2797 case RX_RES_STATUS_SEC_TYPE_TKIP:
2798 if ((decrypt_res & RX_RES_STATUS_DECRYPT_TYPE_MSK) ==
2799 RX_RES_STATUS_BAD_ICV_MIC)
2800 stats->flag |= RX_FLAG_MMIC_ERROR;
2801 case RX_RES_STATUS_SEC_TYPE_WEP:
2802 case RX_RES_STATUS_SEC_TYPE_CCMP:
2803 if ((decrypt_res & RX_RES_STATUS_DECRYPT_TYPE_MSK) ==
2804 RX_RES_STATUS_DECRYPT_OK) {
2805 IWL_DEBUG_RX("hw decrypt successfully!!!\n");
2806 stats->flag |= RX_FLAG_DECRYPTED;
2807 }
2808 break;
2809
2810 default:
2811 break;
2812 }
2813}
2814
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08002815#ifdef CONFIG_IWL3945_SPECTRUM_MEASUREMENT
Zhu Yib481de92007-09-25 17:54:57 -07002816
2817#include "iwl-spectrum.h"
2818
2819#define BEACON_TIME_MASK_LOW 0x00FFFFFF
2820#define BEACON_TIME_MASK_HIGH 0xFF000000
2821#define TIME_UNIT 1024
2822
2823/*
2824 * extended beacon time format
2825 * time in usec will be changed into a 32-bit value in 8:24 format
2826 * the high 1 byte is the beacon counts
2827 * the lower 3 bytes is the time in usec within one beacon interval
2828 */
2829
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002830static u32 iwl3945_usecs_to_beacons(u32 usec, u32 beacon_interval)
Zhu Yib481de92007-09-25 17:54:57 -07002831{
2832 u32 quot;
2833 u32 rem;
2834 u32 interval = beacon_interval * 1024;
2835
2836 if (!interval || !usec)
2837 return 0;
2838
2839 quot = (usec / interval) & (BEACON_TIME_MASK_HIGH >> 24);
2840 rem = (usec % interval) & BEACON_TIME_MASK_LOW;
2841
2842 return (quot << 24) + rem;
2843}
2844
2845/* base is usually what we get from ucode with each received frame,
2846 * the same as HW timer counter counting down
2847 */
2848
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002849static __le32 iwl3945_add_beacon_time(u32 base, u32 addon, u32 beacon_interval)
Zhu Yib481de92007-09-25 17:54:57 -07002850{
2851 u32 base_low = base & BEACON_TIME_MASK_LOW;
2852 u32 addon_low = addon & BEACON_TIME_MASK_LOW;
2853 u32 interval = beacon_interval * TIME_UNIT;
2854 u32 res = (base & BEACON_TIME_MASK_HIGH) +
2855 (addon & BEACON_TIME_MASK_HIGH);
2856
2857 if (base_low > addon_low)
2858 res += base_low - addon_low;
2859 else if (base_low < addon_low) {
2860 res += interval + base_low - addon_low;
2861 res += (1 << 24);
2862 } else
2863 res += (1 << 24);
2864
2865 return cpu_to_le32(res);
2866}
2867
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002868static int iwl3945_get_measurement(struct iwl3945_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07002869 struct ieee80211_measurement_params *params,
2870 u8 type)
2871{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002872 struct iwl3945_spectrum_cmd spectrum;
2873 struct iwl3945_rx_packet *res;
2874 struct iwl3945_host_cmd cmd = {
Zhu Yib481de92007-09-25 17:54:57 -07002875 .id = REPLY_SPECTRUM_MEASUREMENT_CMD,
2876 .data = (void *)&spectrum,
2877 .meta.flags = CMD_WANT_SKB,
2878 };
2879 u32 add_time = le64_to_cpu(params->start_time);
2880 int rc;
2881 int spectrum_resp_status;
2882 int duration = le16_to_cpu(params->duration);
2883
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002884 if (iwl3945_is_associated(priv))
Zhu Yib481de92007-09-25 17:54:57 -07002885 add_time =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002886 iwl3945_usecs_to_beacons(
Zhu Yib481de92007-09-25 17:54:57 -07002887 le64_to_cpu(params->start_time) - priv->last_tsf,
2888 le16_to_cpu(priv->rxon_timing.beacon_interval));
2889
2890 memset(&spectrum, 0, sizeof(spectrum));
2891
2892 spectrum.channel_count = cpu_to_le16(1);
2893 spectrum.flags =
2894 RXON_FLG_TSF2HOST_MSK | RXON_FLG_ANT_A_MSK | RXON_FLG_DIS_DIV_MSK;
2895 spectrum.filter_flags = MEASUREMENT_FILTER_FLAG;
2896 cmd.len = sizeof(spectrum);
2897 spectrum.len = cpu_to_le16(cmd.len - sizeof(spectrum.len));
2898
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002899 if (iwl3945_is_associated(priv))
Zhu Yib481de92007-09-25 17:54:57 -07002900 spectrum.start_time =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002901 iwl3945_add_beacon_time(priv->last_beacon_time,
Zhu Yib481de92007-09-25 17:54:57 -07002902 add_time,
2903 le16_to_cpu(priv->rxon_timing.beacon_interval));
2904 else
2905 spectrum.start_time = 0;
2906
2907 spectrum.channels[0].duration = cpu_to_le32(duration * TIME_UNIT);
2908 spectrum.channels[0].channel = params->channel;
2909 spectrum.channels[0].type = type;
2910 if (priv->active_rxon.flags & RXON_FLG_BAND_24G_MSK)
2911 spectrum.flags |= RXON_FLG_BAND_24G_MSK |
2912 RXON_FLG_AUTO_DETECT_MSK | RXON_FLG_TGG_PROTECT_MSK;
2913
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002914 rc = iwl3945_send_cmd_sync(priv, &cmd);
Zhu Yib481de92007-09-25 17:54:57 -07002915 if (rc)
2916 return rc;
2917
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002918 res = (struct iwl3945_rx_packet *)cmd.meta.u.skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07002919 if (res->hdr.flags & IWL_CMD_FAILED_MSK) {
2920 IWL_ERROR("Bad return from REPLY_RX_ON_ASSOC command\n");
2921 rc = -EIO;
2922 }
2923
2924 spectrum_resp_status = le16_to_cpu(res->u.spectrum.status);
2925 switch (spectrum_resp_status) {
2926 case 0: /* Command will be handled */
2927 if (res->u.spectrum.id != 0xff) {
Ian Schrambc434dd2007-10-25 17:15:29 +08002928 IWL_DEBUG_INFO("Replaced existing measurement: %d\n",
2929 res->u.spectrum.id);
Zhu Yib481de92007-09-25 17:54:57 -07002930 priv->measurement_status &= ~MEASUREMENT_READY;
2931 }
2932 priv->measurement_status |= MEASUREMENT_ACTIVE;
2933 rc = 0;
2934 break;
2935
2936 case 1: /* Command will not be handled */
2937 rc = -EAGAIN;
2938 break;
2939 }
2940
2941 dev_kfree_skb_any(cmd.meta.u.skb);
2942
2943 return rc;
2944}
2945#endif
2946
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002947static void iwl3945_rx_reply_alive(struct iwl3945_priv *priv,
2948 struct iwl3945_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07002949{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002950 struct iwl3945_rx_packet *pkt = (void *)rxb->skb->data;
2951 struct iwl3945_alive_resp *palive;
Zhu Yib481de92007-09-25 17:54:57 -07002952 struct delayed_work *pwork;
2953
2954 palive = &pkt->u.alive_frame;
2955
2956 IWL_DEBUG_INFO("Alive ucode status 0x%08X revision "
2957 "0x%01X 0x%01X\n",
2958 palive->is_valid, palive->ver_type,
2959 palive->ver_subtype);
2960
2961 if (palive->ver_subtype == INITIALIZE_SUBTYPE) {
2962 IWL_DEBUG_INFO("Initialization Alive received.\n");
2963 memcpy(&priv->card_alive_init,
2964 &pkt->u.alive_frame,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002965 sizeof(struct iwl3945_init_alive_resp));
Zhu Yib481de92007-09-25 17:54:57 -07002966 pwork = &priv->init_alive_start;
2967 } else {
2968 IWL_DEBUG_INFO("Runtime Alive received.\n");
2969 memcpy(&priv->card_alive, &pkt->u.alive_frame,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002970 sizeof(struct iwl3945_alive_resp));
Zhu Yib481de92007-09-25 17:54:57 -07002971 pwork = &priv->alive_start;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002972 iwl3945_disable_events(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002973 }
2974
2975 /* We delay the ALIVE response by 5ms to
2976 * give the HW RF Kill time to activate... */
2977 if (palive->is_valid == UCODE_VALID_OK)
2978 queue_delayed_work(priv->workqueue, pwork,
2979 msecs_to_jiffies(5));
2980 else
2981 IWL_WARNING("uCode did not respond OK.\n");
2982}
2983
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002984static void iwl3945_rx_reply_add_sta(struct iwl3945_priv *priv,
2985 struct iwl3945_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07002986{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002987 struct iwl3945_rx_packet *pkt = (void *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07002988
2989 IWL_DEBUG_RX("Received REPLY_ADD_STA: 0x%02X\n", pkt->u.status);
2990 return;
2991}
2992
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002993static void iwl3945_rx_reply_error(struct iwl3945_priv *priv,
2994 struct iwl3945_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07002995{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002996 struct iwl3945_rx_packet *pkt = (void *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07002997
2998 IWL_ERROR("Error Reply type 0x%08X cmd %s (0x%02X) "
2999 "seq 0x%04X ser 0x%08X\n",
3000 le32_to_cpu(pkt->u.err_resp.error_type),
3001 get_cmd_string(pkt->u.err_resp.cmd_id),
3002 pkt->u.err_resp.cmd_id,
3003 le16_to_cpu(pkt->u.err_resp.bad_cmd_seq_num),
3004 le32_to_cpu(pkt->u.err_resp.error_info));
3005}
3006
3007#define TX_STATUS_ENTRY(x) case TX_STATUS_FAIL_ ## x: return #x
3008
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003009static void iwl3945_rx_csa(struct iwl3945_priv *priv, struct iwl3945_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003010{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003011 struct iwl3945_rx_packet *pkt = (void *)rxb->skb->data;
3012 struct iwl3945_rxon_cmd *rxon = (void *)&priv->active_rxon;
3013 struct iwl3945_csa_notification *csa = &(pkt->u.csa_notif);
Zhu Yib481de92007-09-25 17:54:57 -07003014 IWL_DEBUG_11H("CSA notif: channel %d, status %d\n",
3015 le16_to_cpu(csa->channel), le32_to_cpu(csa->status));
3016 rxon->channel = csa->channel;
3017 priv->staging_rxon.channel = csa->channel;
3018}
3019
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003020static void iwl3945_rx_spectrum_measure_notif(struct iwl3945_priv *priv,
3021 struct iwl3945_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003022{
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08003023#ifdef CONFIG_IWL3945_SPECTRUM_MEASUREMENT
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003024 struct iwl3945_rx_packet *pkt = (void *)rxb->skb->data;
3025 struct iwl3945_spectrum_notification *report = &(pkt->u.spectrum_notif);
Zhu Yib481de92007-09-25 17:54:57 -07003026
3027 if (!report->state) {
3028 IWL_DEBUG(IWL_DL_11H | IWL_DL_INFO,
3029 "Spectrum Measure Notification: Start\n");
3030 return;
3031 }
3032
3033 memcpy(&priv->measure_report, report, sizeof(*report));
3034 priv->measurement_status |= MEASUREMENT_READY;
3035#endif
3036}
3037
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003038static void iwl3945_rx_pm_sleep_notif(struct iwl3945_priv *priv,
3039 struct iwl3945_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003040{
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08003041#ifdef CONFIG_IWL3945_DEBUG
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003042 struct iwl3945_rx_packet *pkt = (void *)rxb->skb->data;
3043 struct iwl3945_sleep_notification *sleep = &(pkt->u.sleep_notif);
Zhu Yib481de92007-09-25 17:54:57 -07003044 IWL_DEBUG_RX("sleep mode: %d, src: %d\n",
3045 sleep->pm_sleep_mode, sleep->pm_wakeup_src);
3046#endif
3047}
3048
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003049static void iwl3945_rx_pm_debug_statistics_notif(struct iwl3945_priv *priv,
3050 struct iwl3945_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003051{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003052 struct iwl3945_rx_packet *pkt = (void *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07003053 IWL_DEBUG_RADIO("Dumping %d bytes of unhandled "
3054 "notification for %s:\n",
3055 le32_to_cpu(pkt->len), get_cmd_string(pkt->hdr.cmd));
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003056 iwl3945_print_hex_dump(IWL_DL_RADIO, pkt->u.raw, le32_to_cpu(pkt->len));
Zhu Yib481de92007-09-25 17:54:57 -07003057}
3058
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003059static void iwl3945_bg_beacon_update(struct work_struct *work)
Zhu Yib481de92007-09-25 17:54:57 -07003060{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003061 struct iwl3945_priv *priv =
3062 container_of(work, struct iwl3945_priv, beacon_update);
Zhu Yib481de92007-09-25 17:54:57 -07003063 struct sk_buff *beacon;
3064
3065 /* Pull updated AP beacon from mac80211. will fail if not in AP mode */
Johannes Berge039fa42008-05-15 12:55:29 +02003066 beacon = ieee80211_beacon_get(priv->hw, priv->vif);
Zhu Yib481de92007-09-25 17:54:57 -07003067
3068 if (!beacon) {
3069 IWL_ERROR("update beacon failed\n");
3070 return;
3071 }
3072
3073 mutex_lock(&priv->mutex);
3074 /* new beacon skb is allocated every time; dispose previous.*/
3075 if (priv->ibss_beacon)
3076 dev_kfree_skb(priv->ibss_beacon);
3077
3078 priv->ibss_beacon = beacon;
3079 mutex_unlock(&priv->mutex);
3080
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003081 iwl3945_send_beacon_cmd(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003082}
3083
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003084static void iwl3945_rx_beacon_notif(struct iwl3945_priv *priv,
3085 struct iwl3945_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003086{
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08003087#ifdef CONFIG_IWL3945_DEBUG
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003088 struct iwl3945_rx_packet *pkt = (void *)rxb->skb->data;
3089 struct iwl3945_beacon_notif *beacon = &(pkt->u.beacon_status);
Zhu Yib481de92007-09-25 17:54:57 -07003090 u8 rate = beacon->beacon_notify_hdr.rate;
3091
3092 IWL_DEBUG_RX("beacon status %x retries %d iss %d "
3093 "tsf %d %d rate %d\n",
3094 le32_to_cpu(beacon->beacon_notify_hdr.status) & TX_STATUS_MSK,
3095 beacon->beacon_notify_hdr.failure_frame,
3096 le32_to_cpu(beacon->ibss_mgr_status),
3097 le32_to_cpu(beacon->high_tsf),
3098 le32_to_cpu(beacon->low_tsf), rate);
3099#endif
3100
Johannes Berg05c914f2008-09-11 00:01:58 +02003101 if ((priv->iw_mode == NL80211_IFTYPE_AP) &&
Zhu Yib481de92007-09-25 17:54:57 -07003102 (!test_bit(STATUS_EXIT_PENDING, &priv->status)))
3103 queue_work(priv->workqueue, &priv->beacon_update);
3104}
3105
3106/* Service response to REPLY_SCAN_CMD (0x80) */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003107static void iwl3945_rx_reply_scan(struct iwl3945_priv *priv,
3108 struct iwl3945_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003109{
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08003110#ifdef CONFIG_IWL3945_DEBUG
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003111 struct iwl3945_rx_packet *pkt = (void *)rxb->skb->data;
3112 struct iwl3945_scanreq_notification *notif =
3113 (struct iwl3945_scanreq_notification *)pkt->u.raw;
Zhu Yib481de92007-09-25 17:54:57 -07003114
3115 IWL_DEBUG_RX("Scan request status = 0x%x\n", notif->status);
3116#endif
3117}
3118
3119/* Service SCAN_START_NOTIFICATION (0x82) */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003120static void iwl3945_rx_scan_start_notif(struct iwl3945_priv *priv,
3121 struct iwl3945_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003122{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003123 struct iwl3945_rx_packet *pkt = (void *)rxb->skb->data;
3124 struct iwl3945_scanstart_notification *notif =
3125 (struct iwl3945_scanstart_notification *)pkt->u.raw;
Zhu Yib481de92007-09-25 17:54:57 -07003126 priv->scan_start_tsf = le32_to_cpu(notif->tsf_low);
3127 IWL_DEBUG_SCAN("Scan start: "
3128 "%d [802.11%s] "
3129 "(TSF: 0x%08X:%08X) - %d (beacon timer %u)\n",
3130 notif->channel,
3131 notif->band ? "bg" : "a",
3132 notif->tsf_high,
3133 notif->tsf_low, notif->status, notif->beacon_timer);
3134}
3135
3136/* Service SCAN_RESULTS_NOTIFICATION (0x83) */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003137static void iwl3945_rx_scan_results_notif(struct iwl3945_priv *priv,
3138 struct iwl3945_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003139{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003140 struct iwl3945_rx_packet *pkt = (void *)rxb->skb->data;
3141 struct iwl3945_scanresults_notification *notif =
3142 (struct iwl3945_scanresults_notification *)pkt->u.raw;
Zhu Yib481de92007-09-25 17:54:57 -07003143
3144 IWL_DEBUG_SCAN("Scan ch.res: "
3145 "%d [802.11%s] "
3146 "(TSF: 0x%08X:%08X) - %d "
3147 "elapsed=%lu usec (%dms since last)\n",
3148 notif->channel,
3149 notif->band ? "bg" : "a",
3150 le32_to_cpu(notif->tsf_high),
3151 le32_to_cpu(notif->tsf_low),
3152 le32_to_cpu(notif->statistics[0]),
3153 le32_to_cpu(notif->tsf_low) - priv->scan_start_tsf,
3154 jiffies_to_msecs(elapsed_jiffies
3155 (priv->last_scan_jiffies, jiffies)));
3156
3157 priv->last_scan_jiffies = jiffies;
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08003158 priv->next_scan_jiffies = 0;
Zhu Yib481de92007-09-25 17:54:57 -07003159}
3160
3161/* Service SCAN_COMPLETE_NOTIFICATION (0x84) */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003162static void iwl3945_rx_scan_complete_notif(struct iwl3945_priv *priv,
3163 struct iwl3945_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003164{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003165 struct iwl3945_rx_packet *pkt = (void *)rxb->skb->data;
3166 struct iwl3945_scancomplete_notification *scan_notif = (void *)pkt->u.raw;
Zhu Yib481de92007-09-25 17:54:57 -07003167
3168 IWL_DEBUG_SCAN("Scan complete: %d channels (TSF 0x%08X:%08X) - %d\n",
3169 scan_notif->scanned_channels,
3170 scan_notif->tsf_low,
3171 scan_notif->tsf_high, scan_notif->status);
3172
3173 /* The HW is no longer scanning */
3174 clear_bit(STATUS_SCAN_HW, &priv->status);
3175
3176 /* The scan completion notification came in, so kill that timer... */
3177 cancel_delayed_work(&priv->scan_check);
3178
3179 IWL_DEBUG_INFO("Scan pass on %sGHz took %dms\n",
Ron Rindjunsky66b50042008-06-25 16:46:31 +08003180 (priv->scan_bands & BIT(IEEE80211_BAND_2GHZ)) ?
3181 "2.4" : "5.2",
Zhu Yib481de92007-09-25 17:54:57 -07003182 jiffies_to_msecs(elapsed_jiffies
3183 (priv->scan_pass_start, jiffies)));
3184
Ron Rindjunsky66b50042008-06-25 16:46:31 +08003185 /* Remove this scanned band from the list of pending
3186 * bands to scan, band G precedes A in order of scanning
3187 * as seen in iwl3945_bg_request_scan */
3188 if (priv->scan_bands & BIT(IEEE80211_BAND_2GHZ))
3189 priv->scan_bands &= ~BIT(IEEE80211_BAND_2GHZ);
3190 else if (priv->scan_bands & BIT(IEEE80211_BAND_5GHZ))
3191 priv->scan_bands &= ~BIT(IEEE80211_BAND_5GHZ);
Zhu Yib481de92007-09-25 17:54:57 -07003192
3193 /* If a request to abort was given, or the scan did not succeed
3194 * then we reset the scan state machine and terminate,
3195 * re-queuing another scan if one has been requested */
3196 if (test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
3197 IWL_DEBUG_INFO("Aborted scan completed.\n");
3198 clear_bit(STATUS_SCAN_ABORTING, &priv->status);
3199 } else {
3200 /* If there are more bands on this scan pass reschedule */
3201 if (priv->scan_bands > 0)
3202 goto reschedule;
3203 }
3204
3205 priv->last_scan_jiffies = jiffies;
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08003206 priv->next_scan_jiffies = 0;
Zhu Yib481de92007-09-25 17:54:57 -07003207 IWL_DEBUG_INFO("Setting scan to off\n");
3208
3209 clear_bit(STATUS_SCANNING, &priv->status);
3210
3211 IWL_DEBUG_INFO("Scan took %dms\n",
3212 jiffies_to_msecs(elapsed_jiffies(priv->scan_start, jiffies)));
3213
3214 queue_work(priv->workqueue, &priv->scan_completed);
3215
3216 return;
3217
3218reschedule:
3219 priv->scan_pass_start = jiffies;
3220 queue_work(priv->workqueue, &priv->request_scan);
3221}
3222
3223/* Handle notification from uCode that card's power state is changing
3224 * due to software, hardware, or critical temperature RFKILL */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003225static void iwl3945_rx_card_state_notif(struct iwl3945_priv *priv,
3226 struct iwl3945_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003227{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003228 struct iwl3945_rx_packet *pkt = (void *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07003229 u32 flags = le32_to_cpu(pkt->u.card_state_notif.flags);
3230 unsigned long status = priv->status;
3231
3232 IWL_DEBUG_RF_KILL("Card state received: HW:%s SW:%s\n",
3233 (flags & HW_CARD_DISABLED) ? "Kill" : "On",
3234 (flags & SW_CARD_DISABLED) ? "Kill" : "On");
3235
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003236 iwl3945_write32(priv, CSR_UCODE_DRV_GP1_SET,
Zhu Yib481de92007-09-25 17:54:57 -07003237 CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
3238
3239 if (flags & HW_CARD_DISABLED)
3240 set_bit(STATUS_RF_KILL_HW, &priv->status);
3241 else
3242 clear_bit(STATUS_RF_KILL_HW, &priv->status);
3243
3244
3245 if (flags & SW_CARD_DISABLED)
3246 set_bit(STATUS_RF_KILL_SW, &priv->status);
3247 else
3248 clear_bit(STATUS_RF_KILL_SW, &priv->status);
3249
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003250 iwl3945_scan_cancel(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003251
3252 if ((test_bit(STATUS_RF_KILL_HW, &status) !=
3253 test_bit(STATUS_RF_KILL_HW, &priv->status)) ||
3254 (test_bit(STATUS_RF_KILL_SW, &status) !=
3255 test_bit(STATUS_RF_KILL_SW, &priv->status)))
3256 queue_work(priv->workqueue, &priv->rf_kill);
3257 else
3258 wake_up_interruptible(&priv->wait_command_queue);
3259}
3260
3261/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003262 * iwl3945_setup_rx_handlers - Initialize Rx handler callbacks
Zhu Yib481de92007-09-25 17:54:57 -07003263 *
3264 * Setup the RX handlers for each of the reply types sent from the uCode
3265 * to the host.
3266 *
3267 * This function chains into the hardware specific files for them to setup
3268 * any hardware specific handlers as well.
3269 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003270static void iwl3945_setup_rx_handlers(struct iwl3945_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003271{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003272 priv->rx_handlers[REPLY_ALIVE] = iwl3945_rx_reply_alive;
3273 priv->rx_handlers[REPLY_ADD_STA] = iwl3945_rx_reply_add_sta;
3274 priv->rx_handlers[REPLY_ERROR] = iwl3945_rx_reply_error;
3275 priv->rx_handlers[CHANNEL_SWITCH_NOTIFICATION] = iwl3945_rx_csa;
Zhu Yib481de92007-09-25 17:54:57 -07003276 priv->rx_handlers[SPECTRUM_MEASURE_NOTIFICATION] =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003277 iwl3945_rx_spectrum_measure_notif;
3278 priv->rx_handlers[PM_SLEEP_NOTIFICATION] = iwl3945_rx_pm_sleep_notif;
Zhu Yib481de92007-09-25 17:54:57 -07003279 priv->rx_handlers[PM_DEBUG_STATISTIC_NOTIFIC] =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003280 iwl3945_rx_pm_debug_statistics_notif;
3281 priv->rx_handlers[BEACON_NOTIFICATION] = iwl3945_rx_beacon_notif;
Zhu Yib481de92007-09-25 17:54:57 -07003282
Ben Cahill9fbab512007-11-29 11:09:47 +08003283 /*
3284 * The same handler is used for both the REPLY to a discrete
3285 * statistics request from the host as well as for the periodic
3286 * statistics notifications (after received beacons) from the uCode.
Zhu Yib481de92007-09-25 17:54:57 -07003287 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003288 priv->rx_handlers[REPLY_STATISTICS_CMD] = iwl3945_hw_rx_statistics;
3289 priv->rx_handlers[STATISTICS_NOTIFICATION] = iwl3945_hw_rx_statistics;
Zhu Yib481de92007-09-25 17:54:57 -07003290
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003291 priv->rx_handlers[REPLY_SCAN_CMD] = iwl3945_rx_reply_scan;
3292 priv->rx_handlers[SCAN_START_NOTIFICATION] = iwl3945_rx_scan_start_notif;
Zhu Yib481de92007-09-25 17:54:57 -07003293 priv->rx_handlers[SCAN_RESULTS_NOTIFICATION] =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003294 iwl3945_rx_scan_results_notif;
Zhu Yib481de92007-09-25 17:54:57 -07003295 priv->rx_handlers[SCAN_COMPLETE_NOTIFICATION] =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003296 iwl3945_rx_scan_complete_notif;
3297 priv->rx_handlers[CARD_STATE_NOTIFICATION] = iwl3945_rx_card_state_notif;
Zhu Yib481de92007-09-25 17:54:57 -07003298
Ben Cahill9fbab512007-11-29 11:09:47 +08003299 /* Set up hardware specific Rx handlers */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003300 iwl3945_hw_rx_handler_setup(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003301}
3302
3303/**
Tomas Winkler91c066f2008-03-06 17:36:55 -08003304 * iwl3945_cmd_queue_reclaim - Reclaim CMD queue entries
3305 * When FW advances 'R' index, all entries between old and new 'R' index
3306 * need to be reclaimed.
3307 */
3308static void iwl3945_cmd_queue_reclaim(struct iwl3945_priv *priv,
3309 int txq_id, int index)
3310{
3311 struct iwl3945_tx_queue *txq = &priv->txq[txq_id];
3312 struct iwl3945_queue *q = &txq->q;
3313 int nfreed = 0;
3314
3315 if ((index >= q->n_bd) || (iwl3945_x2_queue_used(q, index) == 0)) {
3316 IWL_ERROR("Read index for DMA queue txq id (%d), index %d, "
3317 "is out of range [0-%d] %d %d.\n", txq_id,
3318 index, q->n_bd, q->write_ptr, q->read_ptr);
3319 return;
3320 }
3321
3322 for (index = iwl_queue_inc_wrap(index, q->n_bd); q->read_ptr != index;
3323 q->read_ptr = iwl_queue_inc_wrap(q->read_ptr, q->n_bd)) {
3324 if (nfreed > 1) {
3325 IWL_ERROR("HCMD skipped: index (%d) %d %d\n", index,
3326 q->write_ptr, q->read_ptr);
3327 queue_work(priv->workqueue, &priv->restart);
3328 break;
3329 }
3330 nfreed++;
3331 }
3332}
3333
3334
3335/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003336 * iwl3945_tx_cmd_complete - Pull unused buffers off the queue and reclaim them
Zhu Yib481de92007-09-25 17:54:57 -07003337 * @rxb: Rx buffer to reclaim
3338 *
3339 * If an Rx buffer has an async callback associated with it the callback
3340 * will be executed. The attached skb (if present) will only be freed
3341 * if the callback returns 1
3342 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003343static void iwl3945_tx_cmd_complete(struct iwl3945_priv *priv,
3344 struct iwl3945_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003345{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003346 struct iwl3945_rx_packet *pkt = (struct iwl3945_rx_packet *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07003347 u16 sequence = le16_to_cpu(pkt->hdr.sequence);
3348 int txq_id = SEQ_TO_QUEUE(sequence);
3349 int index = SEQ_TO_INDEX(sequence);
3350 int huge = sequence & SEQ_HUGE_FRAME;
3351 int cmd_index;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003352 struct iwl3945_cmd *cmd;
Zhu Yib481de92007-09-25 17:54:57 -07003353
Zhu Yib481de92007-09-25 17:54:57 -07003354 BUG_ON(txq_id != IWL_CMD_QUEUE_NUM);
3355
3356 cmd_index = get_cmd_index(&priv->txq[IWL_CMD_QUEUE_NUM].q, index, huge);
3357 cmd = &priv->txq[IWL_CMD_QUEUE_NUM].cmd[cmd_index];
3358
3359 /* Input error checking is done when commands are added to queue. */
3360 if (cmd->meta.flags & CMD_WANT_SKB) {
3361 cmd->meta.source->u.skb = rxb->skb;
3362 rxb->skb = NULL;
3363 } else if (cmd->meta.u.callback &&
3364 !cmd->meta.u.callback(priv, cmd, rxb->skb))
3365 rxb->skb = NULL;
3366
Tomas Winkler91c066f2008-03-06 17:36:55 -08003367 iwl3945_cmd_queue_reclaim(priv, txq_id, index);
Zhu Yib481de92007-09-25 17:54:57 -07003368
3369 if (!(cmd->meta.flags & CMD_ASYNC)) {
3370 clear_bit(STATUS_HCMD_ACTIVE, &priv->status);
3371 wake_up_interruptible(&priv->wait_command_queue);
3372 }
3373}
3374
3375/************************** RX-FUNCTIONS ****************************/
3376/*
3377 * Rx theory of operation
3378 *
3379 * The host allocates 32 DMA target addresses and passes the host address
3380 * to the firmware at register IWL_RFDS_TABLE_LOWER + N * RFD_SIZE where N is
3381 * 0 to 31
3382 *
3383 * Rx Queue Indexes
3384 * The host/firmware share two index registers for managing the Rx buffers.
3385 *
3386 * The READ index maps to the first position that the firmware may be writing
3387 * to -- the driver can read up to (but not including) this position and get
3388 * good data.
3389 * The READ index is managed by the firmware once the card is enabled.
3390 *
3391 * The WRITE index maps to the last position the driver has read from -- the
3392 * position preceding WRITE is the last slot the firmware can place a packet.
3393 *
3394 * The queue is empty (no good data) if WRITE = READ - 1, and is full if
3395 * WRITE = READ.
3396 *
Ben Cahill9fbab512007-11-29 11:09:47 +08003397 * During initialization, the host sets up the READ queue position to the first
Zhu Yib481de92007-09-25 17:54:57 -07003398 * INDEX position, and WRITE to the last (READ - 1 wrapped)
3399 *
Ben Cahill9fbab512007-11-29 11:09:47 +08003400 * When the firmware places a packet in a buffer, it will advance the READ index
Zhu Yib481de92007-09-25 17:54:57 -07003401 * and fire the RX interrupt. The driver can then query the READ index and
3402 * process as many packets as possible, moving the WRITE index forward as it
3403 * resets the Rx queue buffers with new memory.
3404 *
3405 * The management in the driver is as follows:
3406 * + A list of pre-allocated SKBs is stored in iwl->rxq->rx_free. When
3407 * iwl->rxq->free_count drops to or below RX_LOW_WATERMARK, work is scheduled
Ian Schram01ebd062007-10-25 17:15:22 +08003408 * to replenish the iwl->rxq->rx_free.
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003409 * + In iwl3945_rx_replenish (scheduled) if 'processed' != 'read' then the
Zhu Yib481de92007-09-25 17:54:57 -07003410 * iwl->rxq is replenished and the READ INDEX is updated (updating the
3411 * 'processed' and 'read' driver indexes as well)
3412 * + A received packet is processed and handed to the kernel network stack,
3413 * detached from the iwl->rxq. The driver 'processed' index is updated.
3414 * + The Host/Firmware iwl->rxq is replenished at tasklet time from the rx_free
3415 * list. If there are no allocated buffers in iwl->rxq->rx_free, the READ
3416 * INDEX is not incremented and iwl->status(RX_STALLED) is set. If there
3417 * were enough free buffers and RX_STALLED is set it is cleared.
3418 *
3419 *
3420 * Driver sequence:
3421 *
Ben Cahill9fbab512007-11-29 11:09:47 +08003422 * iwl3945_rx_queue_alloc() Allocates rx_free
3423 * iwl3945_rx_replenish() Replenishes rx_free list from rx_used, and calls
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003424 * iwl3945_rx_queue_restock
Ben Cahill9fbab512007-11-29 11:09:47 +08003425 * iwl3945_rx_queue_restock() Moves available buffers from rx_free into Rx
Zhu Yib481de92007-09-25 17:54:57 -07003426 * queue, updates firmware pointers, and updates
3427 * the WRITE index. If insufficient rx_free buffers
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003428 * are available, schedules iwl3945_rx_replenish
Zhu Yib481de92007-09-25 17:54:57 -07003429 *
3430 * -- enable interrupts --
Ben Cahill9fbab512007-11-29 11:09:47 +08003431 * ISR - iwl3945_rx() Detach iwl3945_rx_mem_buffers from pool up to the
Zhu Yib481de92007-09-25 17:54:57 -07003432 * READ INDEX, detaching the SKB from the pool.
3433 * Moves the packet buffer from queue to rx_used.
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003434 * Calls iwl3945_rx_queue_restock to refill any empty
Zhu Yib481de92007-09-25 17:54:57 -07003435 * slots.
3436 * ...
3437 *
3438 */
3439
3440/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003441 * iwl3945_rx_queue_space - Return number of free slots available in queue.
Zhu Yib481de92007-09-25 17:54:57 -07003442 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003443static int iwl3945_rx_queue_space(const struct iwl3945_rx_queue *q)
Zhu Yib481de92007-09-25 17:54:57 -07003444{
3445 int s = q->read - q->write;
3446 if (s <= 0)
3447 s += RX_QUEUE_SIZE;
3448 /* keep some buffer to not confuse full and empty queue */
3449 s -= 2;
3450 if (s < 0)
3451 s = 0;
3452 return s;
3453}
3454
3455/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003456 * iwl3945_rx_queue_update_write_ptr - Update the write pointer for the RX queue
Zhu Yib481de92007-09-25 17:54:57 -07003457 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003458int iwl3945_rx_queue_update_write_ptr(struct iwl3945_priv *priv, struct iwl3945_rx_queue *q)
Zhu Yib481de92007-09-25 17:54:57 -07003459{
3460 u32 reg = 0;
3461 int rc = 0;
3462 unsigned long flags;
3463
3464 spin_lock_irqsave(&q->lock, flags);
3465
3466 if (q->need_update == 0)
3467 goto exit_unlock;
3468
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003469 /* If power-saving is in use, make sure device is awake */
Zhu Yib481de92007-09-25 17:54:57 -07003470 if (test_bit(STATUS_POWER_PMI, &priv->status)) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003471 reg = iwl3945_read32(priv, CSR_UCODE_DRV_GP1);
Zhu Yib481de92007-09-25 17:54:57 -07003472
3473 if (reg & CSR_UCODE_DRV_GP1_BIT_MAC_SLEEP) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003474 iwl3945_set_bit(priv, CSR_GP_CNTRL,
Zhu Yib481de92007-09-25 17:54:57 -07003475 CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
3476 goto exit_unlock;
3477 }
3478
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003479 rc = iwl3945_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003480 if (rc)
3481 goto exit_unlock;
3482
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003483 /* Device expects a multiple of 8 */
Tomas Winklerbddadf82008-12-19 10:37:01 +08003484 iwl3945_write_direct32(priv, FH39_RSCSR_CHNL0_WPTR,
Zhu Yib481de92007-09-25 17:54:57 -07003485 q->write & ~0x7);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003486 iwl3945_release_nic_access(priv);
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003487
3488 /* Else device is assumed to be awake */
Zhu Yib481de92007-09-25 17:54:57 -07003489 } else
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003490 /* Device expects a multiple of 8 */
Tomas Winklerbddadf82008-12-19 10:37:01 +08003491 iwl3945_write32(priv, FH39_RSCSR_CHNL0_WPTR, q->write & ~0x7);
Zhu Yib481de92007-09-25 17:54:57 -07003492
3493
3494 q->need_update = 0;
3495
3496 exit_unlock:
3497 spin_unlock_irqrestore(&q->lock, flags);
3498 return rc;
3499}
3500
3501/**
Ben Cahill9fbab512007-11-29 11:09:47 +08003502 * iwl3945_dma_addr2rbd_ptr - convert a DMA address to a uCode read buffer ptr
Zhu Yib481de92007-09-25 17:54:57 -07003503 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003504static inline __le32 iwl3945_dma_addr2rbd_ptr(struct iwl3945_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07003505 dma_addr_t dma_addr)
3506{
3507 return cpu_to_le32((u32)dma_addr);
3508}
3509
3510/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003511 * iwl3945_rx_queue_restock - refill RX queue from pre-allocated pool
Zhu Yib481de92007-09-25 17:54:57 -07003512 *
Ben Cahill9fbab512007-11-29 11:09:47 +08003513 * If there are slots in the RX queue that need to be restocked,
Zhu Yib481de92007-09-25 17:54:57 -07003514 * and we have free pre-allocated buffers, fill the ranks as much
Ben Cahill9fbab512007-11-29 11:09:47 +08003515 * as we can, pulling from rx_free.
Zhu Yib481de92007-09-25 17:54:57 -07003516 *
3517 * This moves the 'write' index forward to catch up with 'processed', and
3518 * also updates the memory address in the firmware to reference the new
3519 * target buffer.
3520 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003521static int iwl3945_rx_queue_restock(struct iwl3945_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003522{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003523 struct iwl3945_rx_queue *rxq = &priv->rxq;
Zhu Yib481de92007-09-25 17:54:57 -07003524 struct list_head *element;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003525 struct iwl3945_rx_mem_buffer *rxb;
Zhu Yib481de92007-09-25 17:54:57 -07003526 unsigned long flags;
3527 int write, rc;
3528
3529 spin_lock_irqsave(&rxq->lock, flags);
3530 write = rxq->write & ~0x7;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003531 while ((iwl3945_rx_queue_space(rxq) > 0) && (rxq->free_count)) {
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003532 /* Get next free Rx buffer, remove from free list */
Zhu Yib481de92007-09-25 17:54:57 -07003533 element = rxq->rx_free.next;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003534 rxb = list_entry(element, struct iwl3945_rx_mem_buffer, list);
Zhu Yib481de92007-09-25 17:54:57 -07003535 list_del(element);
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003536
3537 /* Point to Rx buffer via next RBD in circular buffer */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003538 rxq->bd[rxq->write] = iwl3945_dma_addr2rbd_ptr(priv, rxb->dma_addr);
Zhu Yib481de92007-09-25 17:54:57 -07003539 rxq->queue[rxq->write] = rxb;
3540 rxq->write = (rxq->write + 1) & RX_QUEUE_MASK;
3541 rxq->free_count--;
3542 }
3543 spin_unlock_irqrestore(&rxq->lock, flags);
3544 /* If the pre-allocated buffer pool is dropping low, schedule to
3545 * refill it */
3546 if (rxq->free_count <= RX_LOW_WATERMARK)
3547 queue_work(priv->workqueue, &priv->rx_replenish);
3548
3549
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003550 /* If we've added more space for the firmware to place data, tell it.
3551 * Increment device's write pointer in multiples of 8. */
Zhu Yib481de92007-09-25 17:54:57 -07003552 if ((write != (rxq->write & ~0x7))
3553 || (abs(rxq->write - rxq->read) > 7)) {
3554 spin_lock_irqsave(&rxq->lock, flags);
3555 rxq->need_update = 1;
3556 spin_unlock_irqrestore(&rxq->lock, flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003557 rc = iwl3945_rx_queue_update_write_ptr(priv, rxq);
Zhu Yib481de92007-09-25 17:54:57 -07003558 if (rc)
3559 return rc;
3560 }
3561
3562 return 0;
3563}
3564
3565/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003566 * iwl3945_rx_replenish - Move all used packet from rx_used to rx_free
Zhu Yib481de92007-09-25 17:54:57 -07003567 *
3568 * When moving to rx_free an SKB is allocated for the slot.
3569 *
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003570 * Also restock the Rx queue via iwl3945_rx_queue_restock.
Ian Schram01ebd062007-10-25 17:15:22 +08003571 * This is called as a scheduled work item (except for during initialization)
Zhu Yib481de92007-09-25 17:54:57 -07003572 */
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08003573static void iwl3945_rx_allocate(struct iwl3945_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003574{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003575 struct iwl3945_rx_queue *rxq = &priv->rxq;
Zhu Yib481de92007-09-25 17:54:57 -07003576 struct list_head *element;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003577 struct iwl3945_rx_mem_buffer *rxb;
Zhu Yib481de92007-09-25 17:54:57 -07003578 unsigned long flags;
3579 spin_lock_irqsave(&rxq->lock, flags);
3580 while (!list_empty(&rxq->rx_used)) {
3581 element = rxq->rx_used.next;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003582 rxb = list_entry(element, struct iwl3945_rx_mem_buffer, list);
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003583
3584 /* Alloc a new receive buffer */
Zhu Yib481de92007-09-25 17:54:57 -07003585 rxb->skb =
3586 alloc_skb(IWL_RX_BUF_SIZE, __GFP_NOWARN | GFP_ATOMIC);
3587 if (!rxb->skb) {
3588 if (net_ratelimit())
3589 printk(KERN_CRIT DRV_NAME
3590 ": Can not allocate SKB buffers\n");
3591 /* We don't reschedule replenish work here -- we will
3592 * call the restock method and if it still needs
3593 * more buffers it will schedule replenish */
3594 break;
3595 }
Zhu Yi12342c42007-12-20 11:27:32 +08003596
3597 /* If radiotap head is required, reserve some headroom here.
3598 * The physical head count is a variable rx_stats->phy_count.
3599 * We reserve 4 bytes here. Plus these extra bytes, the
3600 * headroom of the physical head should be enough for the
3601 * radiotap head that iwl3945 supported. See iwl3945_rt.
3602 */
3603 skb_reserve(rxb->skb, 4);
3604
Zhu Yib481de92007-09-25 17:54:57 -07003605 priv->alloc_rxb_skb++;
3606 list_del(element);
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003607
3608 /* Get physical address of RB/SKB */
Zhu Yib481de92007-09-25 17:54:57 -07003609 rxb->dma_addr =
3610 pci_map_single(priv->pci_dev, rxb->skb->data,
3611 IWL_RX_BUF_SIZE, PCI_DMA_FROMDEVICE);
3612 list_add_tail(&rxb->list, &rxq->rx_free);
3613 rxq->free_count++;
3614 }
3615 spin_unlock_irqrestore(&rxq->lock, flags);
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08003616}
3617
3618/*
3619 * this should be called while priv->lock is locked
3620 */
Tomas Winkler4fd1f842007-12-05 20:59:58 +02003621static void __iwl3945_rx_replenish(void *data)
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08003622{
3623 struct iwl3945_priv *priv = data;
3624
3625 iwl3945_rx_allocate(priv);
3626 iwl3945_rx_queue_restock(priv);
3627}
3628
3629
3630void iwl3945_rx_replenish(void *data)
3631{
3632 struct iwl3945_priv *priv = data;
3633 unsigned long flags;
3634
3635 iwl3945_rx_allocate(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003636
3637 spin_lock_irqsave(&priv->lock, flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003638 iwl3945_rx_queue_restock(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003639 spin_unlock_irqrestore(&priv->lock, flags);
3640}
3641
3642/* Assumes that the skb field of the buffers in 'pool' is kept accurate.
Ben Cahill9fbab512007-11-29 11:09:47 +08003643 * If an SKB has been detached, the POOL needs to have its SKB set to NULL
Zhu Yib481de92007-09-25 17:54:57 -07003644 * This free routine walks the list of POOL entries and if SKB is set to
3645 * non NULL it is unmapped and freed
3646 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003647static void iwl3945_rx_queue_free(struct iwl3945_priv *priv, struct iwl3945_rx_queue *rxq)
Zhu Yib481de92007-09-25 17:54:57 -07003648{
3649 int i;
3650 for (i = 0; i < RX_QUEUE_SIZE + RX_FREE_BUFFERS; i++) {
3651 if (rxq->pool[i].skb != NULL) {
3652 pci_unmap_single(priv->pci_dev,
3653 rxq->pool[i].dma_addr,
3654 IWL_RX_BUF_SIZE, PCI_DMA_FROMDEVICE);
3655 dev_kfree_skb(rxq->pool[i].skb);
3656 }
3657 }
3658
3659 pci_free_consistent(priv->pci_dev, 4 * RX_QUEUE_SIZE, rxq->bd,
3660 rxq->dma_addr);
3661 rxq->bd = NULL;
3662}
3663
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003664int iwl3945_rx_queue_alloc(struct iwl3945_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003665{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003666 struct iwl3945_rx_queue *rxq = &priv->rxq;
Zhu Yib481de92007-09-25 17:54:57 -07003667 struct pci_dev *dev = priv->pci_dev;
3668 int i;
3669
3670 spin_lock_init(&rxq->lock);
3671 INIT_LIST_HEAD(&rxq->rx_free);
3672 INIT_LIST_HEAD(&rxq->rx_used);
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003673
3674 /* Alloc the circular buffer of Read Buffer Descriptors (RBDs) */
Zhu Yib481de92007-09-25 17:54:57 -07003675 rxq->bd = pci_alloc_consistent(dev, 4 * RX_QUEUE_SIZE, &rxq->dma_addr);
3676 if (!rxq->bd)
3677 return -ENOMEM;
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003678
Zhu Yib481de92007-09-25 17:54:57 -07003679 /* Fill the rx_used queue with _all_ of the Rx buffers */
3680 for (i = 0; i < RX_FREE_BUFFERS + RX_QUEUE_SIZE; i++)
3681 list_add_tail(&rxq->pool[i].list, &rxq->rx_used);
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003682
Zhu Yib481de92007-09-25 17:54:57 -07003683 /* Set us so that we have processed and used all buffers, but have
3684 * not restocked the Rx queue with fresh buffers */
3685 rxq->read = rxq->write = 0;
3686 rxq->free_count = 0;
3687 rxq->need_update = 0;
3688 return 0;
3689}
3690
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003691void iwl3945_rx_queue_reset(struct iwl3945_priv *priv, struct iwl3945_rx_queue *rxq)
Zhu Yib481de92007-09-25 17:54:57 -07003692{
3693 unsigned long flags;
3694 int i;
3695 spin_lock_irqsave(&rxq->lock, flags);
3696 INIT_LIST_HEAD(&rxq->rx_free);
3697 INIT_LIST_HEAD(&rxq->rx_used);
3698 /* Fill the rx_used queue with _all_ of the Rx buffers */
3699 for (i = 0; i < RX_FREE_BUFFERS + RX_QUEUE_SIZE; i++) {
3700 /* In the reset function, these buffers may have been allocated
3701 * to an SKB, so we need to unmap and free potential storage */
3702 if (rxq->pool[i].skb != NULL) {
3703 pci_unmap_single(priv->pci_dev,
3704 rxq->pool[i].dma_addr,
3705 IWL_RX_BUF_SIZE, PCI_DMA_FROMDEVICE);
3706 priv->alloc_rxb_skb--;
3707 dev_kfree_skb(rxq->pool[i].skb);
3708 rxq->pool[i].skb = NULL;
3709 }
3710 list_add_tail(&rxq->pool[i].list, &rxq->rx_used);
3711 }
3712
3713 /* Set us so that we have processed and used all buffers, but have
3714 * not restocked the Rx queue with fresh buffers */
3715 rxq->read = rxq->write = 0;
3716 rxq->free_count = 0;
3717 spin_unlock_irqrestore(&rxq->lock, flags);
3718}
3719
3720/* Convert linear signal-to-noise ratio into dB */
3721static u8 ratio2dB[100] = {
3722/* 0 1 2 3 4 5 6 7 8 9 */
3723 0, 0, 6, 10, 12, 14, 16, 17, 18, 19, /* 00 - 09 */
3724 20, 21, 22, 22, 23, 23, 24, 25, 26, 26, /* 10 - 19 */
3725 26, 26, 26, 27, 27, 28, 28, 28, 29, 29, /* 20 - 29 */
3726 29, 30, 30, 30, 31, 31, 31, 31, 32, 32, /* 30 - 39 */
3727 32, 32, 32, 33, 33, 33, 33, 33, 34, 34, /* 40 - 49 */
3728 34, 34, 34, 34, 35, 35, 35, 35, 35, 35, /* 50 - 59 */
3729 36, 36, 36, 36, 36, 36, 36, 37, 37, 37, /* 60 - 69 */
3730 37, 37, 37, 37, 37, 38, 38, 38, 38, 38, /* 70 - 79 */
3731 38, 38, 38, 38, 38, 39, 39, 39, 39, 39, /* 80 - 89 */
3732 39, 39, 39, 39, 39, 40, 40, 40, 40, 40 /* 90 - 99 */
3733};
3734
3735/* Calculates a relative dB value from a ratio of linear
3736 * (i.e. not dB) signal levels.
3737 * Conversion assumes that levels are voltages (20*log), not powers (10*log). */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003738int iwl3945_calc_db_from_ratio(int sig_ratio)
Zhu Yib481de92007-09-25 17:54:57 -07003739{
Adrian Bunk221c80c2008-02-02 23:19:01 +02003740 /* 1000:1 or higher just report as 60 dB */
3741 if (sig_ratio >= 1000)
Zhu Yib481de92007-09-25 17:54:57 -07003742 return 60;
3743
Adrian Bunk221c80c2008-02-02 23:19:01 +02003744 /* 100:1 or higher, divide by 10 and use table,
Zhu Yib481de92007-09-25 17:54:57 -07003745 * add 20 dB to make up for divide by 10 */
Adrian Bunk221c80c2008-02-02 23:19:01 +02003746 if (sig_ratio >= 100)
Tomas Winkler3ac7f142008-07-21 02:40:14 +03003747 return 20 + (int)ratio2dB[sig_ratio/10];
Zhu Yib481de92007-09-25 17:54:57 -07003748
3749 /* We shouldn't see this */
3750 if (sig_ratio < 1)
3751 return 0;
3752
3753 /* Use table for ratios 1:1 - 99:1 */
3754 return (int)ratio2dB[sig_ratio];
3755}
3756
3757#define PERFECT_RSSI (-20) /* dBm */
3758#define WORST_RSSI (-95) /* dBm */
3759#define RSSI_RANGE (PERFECT_RSSI - WORST_RSSI)
3760
3761/* Calculate an indication of rx signal quality (a percentage, not dBm!).
3762 * See http://www.ces.clemson.edu/linux/signal_quality.shtml for info
3763 * about formulas used below. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003764int iwl3945_calc_sig_qual(int rssi_dbm, int noise_dbm)
Zhu Yib481de92007-09-25 17:54:57 -07003765{
3766 int sig_qual;
3767 int degradation = PERFECT_RSSI - rssi_dbm;
3768
3769 /* If we get a noise measurement, use signal-to-noise ratio (SNR)
3770 * as indicator; formula is (signal dbm - noise dbm).
3771 * SNR at or above 40 is a great signal (100%).
3772 * Below that, scale to fit SNR of 0 - 40 dB within 0 - 100% indicator.
3773 * Weakest usable signal is usually 10 - 15 dB SNR. */
3774 if (noise_dbm) {
3775 if (rssi_dbm - noise_dbm >= 40)
3776 return 100;
3777 else if (rssi_dbm < noise_dbm)
3778 return 0;
3779 sig_qual = ((rssi_dbm - noise_dbm) * 5) / 2;
3780
3781 /* Else use just the signal level.
3782 * This formula is a least squares fit of data points collected and
3783 * compared with a reference system that had a percentage (%) display
3784 * for signal quality. */
3785 } else
3786 sig_qual = (100 * (RSSI_RANGE * RSSI_RANGE) - degradation *
3787 (15 * RSSI_RANGE + 62 * degradation)) /
3788 (RSSI_RANGE * RSSI_RANGE);
3789
3790 if (sig_qual > 100)
3791 sig_qual = 100;
3792 else if (sig_qual < 1)
3793 sig_qual = 0;
3794
3795 return sig_qual;
3796}
3797
3798/**
Ben Cahill9fbab512007-11-29 11:09:47 +08003799 * iwl3945_rx_handle - Main entry function for receiving responses from uCode
Zhu Yib481de92007-09-25 17:54:57 -07003800 *
3801 * Uses the priv->rx_handlers callback function array to invoke
3802 * the appropriate handlers, including command responses,
3803 * frame-received notifications, and other notifications.
3804 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003805static void iwl3945_rx_handle(struct iwl3945_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003806{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003807 struct iwl3945_rx_mem_buffer *rxb;
3808 struct iwl3945_rx_packet *pkt;
3809 struct iwl3945_rx_queue *rxq = &priv->rxq;
Zhu Yib481de92007-09-25 17:54:57 -07003810 u32 r, i;
3811 int reclaim;
3812 unsigned long flags;
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08003813 u8 fill_rx = 0;
Mohamed Abbasd68ab682008-02-07 13:16:33 -08003814 u32 count = 8;
Zhu Yib481de92007-09-25 17:54:57 -07003815
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003816 /* uCode's read index (stored in shared DRAM) indicates the last Rx
3817 * buffer that the driver may process (last buffer filled by ucode). */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003818 r = iwl3945_hw_get_rx_read(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003819 i = rxq->read;
3820
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08003821 if (iwl3945_rx_queue_space(rxq) > (RX_QUEUE_SIZE / 2))
3822 fill_rx = 1;
Zhu Yib481de92007-09-25 17:54:57 -07003823 /* Rx interrupt, but nothing sent from uCode */
3824 if (i == r)
3825 IWL_DEBUG(IWL_DL_RX | IWL_DL_ISR, "r = %d, i = %d\n", r, i);
3826
3827 while (i != r) {
3828 rxb = rxq->queue[i];
3829
Ben Cahill9fbab512007-11-29 11:09:47 +08003830 /* If an RXB doesn't have a Rx queue slot associated with it,
Zhu Yib481de92007-09-25 17:54:57 -07003831 * then a bug has been introduced in the queue refilling
3832 * routines -- catch it here */
3833 BUG_ON(rxb == NULL);
3834
3835 rxq->queue[i] = NULL;
3836
3837 pci_dma_sync_single_for_cpu(priv->pci_dev, rxb->dma_addr,
3838 IWL_RX_BUF_SIZE,
3839 PCI_DMA_FROMDEVICE);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003840 pkt = (struct iwl3945_rx_packet *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07003841
3842 /* Reclaim a command buffer only if this packet is a response
3843 * to a (driver-originated) command.
3844 * If the packet (e.g. Rx frame) originated from uCode,
3845 * there is no command buffer to reclaim.
3846 * Ucode should set SEQ_RX_FRAME bit if ucode-originated,
3847 * but apparently a few don't get set; catch them here. */
3848 reclaim = !(pkt->hdr.sequence & SEQ_RX_FRAME) &&
3849 (pkt->hdr.cmd != STATISTICS_NOTIFICATION) &&
3850 (pkt->hdr.cmd != REPLY_TX);
3851
3852 /* Based on type of command response or notification,
3853 * handle those that need handling via function in
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003854 * rx_handlers table. See iwl3945_setup_rx_handlers() */
Zhu Yib481de92007-09-25 17:54:57 -07003855 if (priv->rx_handlers[pkt->hdr.cmd]) {
3856 IWL_DEBUG(IWL_DL_HOST_COMMAND | IWL_DL_RX | IWL_DL_ISR,
3857 "r = %d, i = %d, %s, 0x%02x\n", r, i,
3858 get_cmd_string(pkt->hdr.cmd), pkt->hdr.cmd);
3859 priv->rx_handlers[pkt->hdr.cmd] (priv, rxb);
3860 } else {
3861 /* No handling needed */
3862 IWL_DEBUG(IWL_DL_HOST_COMMAND | IWL_DL_RX | IWL_DL_ISR,
3863 "r %d i %d No handler needed for %s, 0x%02x\n",
3864 r, i, get_cmd_string(pkt->hdr.cmd),
3865 pkt->hdr.cmd);
3866 }
3867
3868 if (reclaim) {
Ben Cahill9fbab512007-11-29 11:09:47 +08003869 /* Invoke any callbacks, transfer the skb to caller, and
3870 * fire off the (possibly) blocking iwl3945_send_cmd()
Zhu Yib481de92007-09-25 17:54:57 -07003871 * as we reclaim the driver command queue */
3872 if (rxb && rxb->skb)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003873 iwl3945_tx_cmd_complete(priv, rxb);
Zhu Yib481de92007-09-25 17:54:57 -07003874 else
3875 IWL_WARNING("Claim null rxb?\n");
3876 }
3877
3878 /* For now we just don't re-use anything. We can tweak this
3879 * later to try and re-use notification packets and SKBs that
3880 * fail to Rx correctly */
3881 if (rxb->skb != NULL) {
3882 priv->alloc_rxb_skb--;
3883 dev_kfree_skb_any(rxb->skb);
3884 rxb->skb = NULL;
3885 }
3886
3887 pci_unmap_single(priv->pci_dev, rxb->dma_addr,
3888 IWL_RX_BUF_SIZE, PCI_DMA_FROMDEVICE);
3889 spin_lock_irqsave(&rxq->lock, flags);
3890 list_add_tail(&rxb->list, &priv->rxq.rx_used);
3891 spin_unlock_irqrestore(&rxq->lock, flags);
3892 i = (i + 1) & RX_QUEUE_MASK;
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08003893 /* If there are a lot of unused frames,
3894 * restock the Rx queue so ucode won't assert. */
3895 if (fill_rx) {
3896 count++;
3897 if (count >= 8) {
3898 priv->rxq.read = i;
3899 __iwl3945_rx_replenish(priv);
3900 count = 0;
3901 }
3902 }
Zhu Yib481de92007-09-25 17:54:57 -07003903 }
3904
3905 /* Backtrack one entry */
3906 priv->rxq.read = i;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003907 iwl3945_rx_queue_restock(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003908}
3909
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003910/**
3911 * iwl3945_tx_queue_update_write_ptr - Send new write index to hardware
3912 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003913static int iwl3945_tx_queue_update_write_ptr(struct iwl3945_priv *priv,
3914 struct iwl3945_tx_queue *txq)
Zhu Yib481de92007-09-25 17:54:57 -07003915{
3916 u32 reg = 0;
3917 int rc = 0;
3918 int txq_id = txq->q.id;
3919
3920 if (txq->need_update == 0)
3921 return rc;
3922
3923 /* if we're trying to save power */
3924 if (test_bit(STATUS_POWER_PMI, &priv->status)) {
3925 /* wake up nic if it's powered down ...
3926 * uCode will wake up, and interrupt us again, so next
3927 * time we'll skip this part. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003928 reg = iwl3945_read32(priv, CSR_UCODE_DRV_GP1);
Zhu Yib481de92007-09-25 17:54:57 -07003929
3930 if (reg & CSR_UCODE_DRV_GP1_BIT_MAC_SLEEP) {
3931 IWL_DEBUG_INFO("Requesting wakeup, GP1 = 0x%x\n", reg);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003932 iwl3945_set_bit(priv, CSR_GP_CNTRL,
Zhu Yib481de92007-09-25 17:54:57 -07003933 CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
3934 return rc;
3935 }
3936
3937 /* restore this queue's parameters in nic hardware. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003938 rc = iwl3945_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003939 if (rc)
3940 return rc;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003941 iwl3945_write_direct32(priv, HBUS_TARG_WRPTR,
Tomas Winklerfc4b6852007-10-25 17:15:24 +08003942 txq->q.write_ptr | (txq_id << 8));
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003943 iwl3945_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003944
3945 /* else not in power-save mode, uCode will never sleep when we're
3946 * trying to tx (during RFKILL, we're not trying to tx). */
3947 } else
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003948 iwl3945_write32(priv, HBUS_TARG_WRPTR,
Tomas Winklerfc4b6852007-10-25 17:15:24 +08003949 txq->q.write_ptr | (txq_id << 8));
Zhu Yib481de92007-09-25 17:54:57 -07003950
3951 txq->need_update = 0;
3952
3953 return rc;
3954}
3955
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08003956#ifdef CONFIG_IWL3945_DEBUG
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003957static void iwl3945_print_rx_config_cmd(struct iwl3945_rxon_cmd *rxon)
Zhu Yib481de92007-09-25 17:54:57 -07003958{
3959 IWL_DEBUG_RADIO("RX CONFIG:\n");
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003960 iwl3945_print_hex_dump(IWL_DL_RADIO, (u8 *) rxon, sizeof(*rxon));
Zhu Yib481de92007-09-25 17:54:57 -07003961 IWL_DEBUG_RADIO("u16 channel: 0x%x\n", le16_to_cpu(rxon->channel));
3962 IWL_DEBUG_RADIO("u32 flags: 0x%08X\n", le32_to_cpu(rxon->flags));
3963 IWL_DEBUG_RADIO("u32 filter_flags: 0x%08x\n",
3964 le32_to_cpu(rxon->filter_flags));
3965 IWL_DEBUG_RADIO("u8 dev_type: 0x%x\n", rxon->dev_type);
3966 IWL_DEBUG_RADIO("u8 ofdm_basic_rates: 0x%02x\n",
3967 rxon->ofdm_basic_rates);
3968 IWL_DEBUG_RADIO("u8 cck_basic_rates: 0x%02x\n", rxon->cck_basic_rates);
Johannes Berge1749612008-10-27 15:59:26 -07003969 IWL_DEBUG_RADIO("u8[6] node_addr: %pM\n", rxon->node_addr);
3970 IWL_DEBUG_RADIO("u8[6] bssid_addr: %pM\n", rxon->bssid_addr);
Zhu Yib481de92007-09-25 17:54:57 -07003971 IWL_DEBUG_RADIO("u16 assoc_id: 0x%x\n", le16_to_cpu(rxon->assoc_id));
3972}
3973#endif
3974
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003975static void iwl3945_enable_interrupts(struct iwl3945_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003976{
3977 IWL_DEBUG_ISR("Enabling interrupts\n");
3978 set_bit(STATUS_INT_ENABLED, &priv->status);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003979 iwl3945_write32(priv, CSR_INT_MASK, CSR_INI_SET_MASK);
Zhu Yib481de92007-09-25 17:54:57 -07003980}
3981
Mohamed Abbas0359fac2008-03-28 16:21:08 -07003982
3983/* call this function to flush any scheduled tasklet */
3984static inline void iwl_synchronize_irq(struct iwl3945_priv *priv)
3985{
Tomas Winklera96a27f2008-10-23 23:48:56 -07003986 /* wait to make sure we flush pending tasklet*/
Mohamed Abbas0359fac2008-03-28 16:21:08 -07003987 synchronize_irq(priv->pci_dev->irq);
3988 tasklet_kill(&priv->irq_tasklet);
3989}
3990
3991
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003992static inline void iwl3945_disable_interrupts(struct iwl3945_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003993{
3994 clear_bit(STATUS_INT_ENABLED, &priv->status);
3995
3996 /* disable interrupts from uCode/NIC to host */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003997 iwl3945_write32(priv, CSR_INT_MASK, 0x00000000);
Zhu Yib481de92007-09-25 17:54:57 -07003998
3999 /* acknowledge/clear/reset any interrupts still pending
4000 * from uCode or flow handler (Rx/Tx DMA) */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004001 iwl3945_write32(priv, CSR_INT, 0xffffffff);
4002 iwl3945_write32(priv, CSR_FH_INT_STATUS, 0xffffffff);
Zhu Yib481de92007-09-25 17:54:57 -07004003 IWL_DEBUG_ISR("Disabled interrupts\n");
4004}
4005
4006static const char *desc_lookup(int i)
4007{
4008 switch (i) {
4009 case 1:
4010 return "FAIL";
4011 case 2:
4012 return "BAD_PARAM";
4013 case 3:
4014 return "BAD_CHECKSUM";
4015 case 4:
4016 return "NMI_INTERRUPT";
4017 case 5:
4018 return "SYSASSERT";
4019 case 6:
4020 return "FATAL_ERROR";
4021 }
4022
4023 return "UNKNOWN";
4024}
4025
4026#define ERROR_START_OFFSET (1 * sizeof(u32))
4027#define ERROR_ELEM_SIZE (7 * sizeof(u32))
4028
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004029static void iwl3945_dump_nic_error_log(struct iwl3945_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004030{
4031 u32 i;
4032 u32 desc, time, count, base, data1;
4033 u32 blink1, blink2, ilink1, ilink2;
4034 int rc;
4035
4036 base = le32_to_cpu(priv->card_alive.error_event_table_ptr);
4037
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004038 if (!iwl3945_hw_valid_rtc_data_addr(base)) {
Zhu Yib481de92007-09-25 17:54:57 -07004039 IWL_ERROR("Not valid error log pointer 0x%08X\n", base);
4040 return;
4041 }
4042
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004043 rc = iwl3945_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004044 if (rc) {
4045 IWL_WARNING("Can not read from adapter at this time.\n");
4046 return;
4047 }
4048
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004049 count = iwl3945_read_targ_mem(priv, base);
Zhu Yib481de92007-09-25 17:54:57 -07004050
4051 if (ERROR_START_OFFSET <= count * ERROR_ELEM_SIZE) {
4052 IWL_ERROR("Start IWL Error Log Dump:\n");
Tomas Winkler2acae162008-03-02 01:25:59 +02004053 IWL_ERROR("Status: 0x%08lX, count: %d\n", priv->status, count);
Zhu Yib481de92007-09-25 17:54:57 -07004054 }
4055
4056 IWL_ERROR("Desc Time asrtPC blink2 "
4057 "ilink1 nmiPC Line\n");
4058 for (i = ERROR_START_OFFSET;
4059 i < (count * ERROR_ELEM_SIZE) + ERROR_START_OFFSET;
4060 i += ERROR_ELEM_SIZE) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004061 desc = iwl3945_read_targ_mem(priv, base + i);
Zhu Yib481de92007-09-25 17:54:57 -07004062 time =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004063 iwl3945_read_targ_mem(priv, base + i + 1 * sizeof(u32));
Zhu Yib481de92007-09-25 17:54:57 -07004064 blink1 =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004065 iwl3945_read_targ_mem(priv, base + i + 2 * sizeof(u32));
Zhu Yib481de92007-09-25 17:54:57 -07004066 blink2 =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004067 iwl3945_read_targ_mem(priv, base + i + 3 * sizeof(u32));
Zhu Yib481de92007-09-25 17:54:57 -07004068 ilink1 =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004069 iwl3945_read_targ_mem(priv, base + i + 4 * sizeof(u32));
Zhu Yib481de92007-09-25 17:54:57 -07004070 ilink2 =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004071 iwl3945_read_targ_mem(priv, base + i + 5 * sizeof(u32));
Zhu Yib481de92007-09-25 17:54:57 -07004072 data1 =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004073 iwl3945_read_targ_mem(priv, base + i + 6 * sizeof(u32));
Zhu Yib481de92007-09-25 17:54:57 -07004074
4075 IWL_ERROR
4076 ("%-13s (#%d) %010u 0x%05X 0x%05X 0x%05X 0x%05X %u\n\n",
4077 desc_lookup(desc), desc, time, blink1, blink2,
4078 ilink1, ilink2, data1);
4079 }
4080
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004081 iwl3945_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004082
4083}
4084
Ben Cahillf58177b2007-11-29 11:09:43 +08004085#define EVENT_START_OFFSET (6 * sizeof(u32))
Zhu Yib481de92007-09-25 17:54:57 -07004086
4087/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004088 * iwl3945_print_event_log - Dump error event log to syslog
Zhu Yib481de92007-09-25 17:54:57 -07004089 *
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004090 * NOTE: Must be called with iwl3945_grab_nic_access() already obtained!
Zhu Yib481de92007-09-25 17:54:57 -07004091 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004092static void iwl3945_print_event_log(struct iwl3945_priv *priv, u32 start_idx,
Zhu Yib481de92007-09-25 17:54:57 -07004093 u32 num_events, u32 mode)
4094{
4095 u32 i;
4096 u32 base; /* SRAM byte address of event log header */
4097 u32 event_size; /* 2 u32s, or 3 u32s if timestamp recorded */
4098 u32 ptr; /* SRAM byte address of log data */
4099 u32 ev, time, data; /* event log data */
4100
4101 if (num_events == 0)
4102 return;
4103
4104 base = le32_to_cpu(priv->card_alive.log_event_table_ptr);
4105
4106 if (mode == 0)
4107 event_size = 2 * sizeof(u32);
4108 else
4109 event_size = 3 * sizeof(u32);
4110
4111 ptr = base + EVENT_START_OFFSET + (start_idx * event_size);
4112
4113 /* "time" is actually "data" for mode 0 (no timestamp).
4114 * place event id # at far right for easier visual parsing. */
4115 for (i = 0; i < num_events; i++) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004116 ev = iwl3945_read_targ_mem(priv, ptr);
Zhu Yib481de92007-09-25 17:54:57 -07004117 ptr += sizeof(u32);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004118 time = iwl3945_read_targ_mem(priv, ptr);
Zhu Yib481de92007-09-25 17:54:57 -07004119 ptr += sizeof(u32);
4120 if (mode == 0)
4121 IWL_ERROR("0x%08x\t%04u\n", time, ev); /* data, ev */
4122 else {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004123 data = iwl3945_read_targ_mem(priv, ptr);
Zhu Yib481de92007-09-25 17:54:57 -07004124 ptr += sizeof(u32);
4125 IWL_ERROR("%010u\t0x%08x\t%04u\n", time, data, ev);
4126 }
4127 }
4128}
4129
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004130static void iwl3945_dump_nic_event_log(struct iwl3945_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004131{
4132 int rc;
4133 u32 base; /* SRAM byte address of event log header */
4134 u32 capacity; /* event log capacity in # entries */
4135 u32 mode; /* 0 - no timestamp, 1 - timestamp recorded */
4136 u32 num_wraps; /* # times uCode wrapped to top of log */
4137 u32 next_entry; /* index of next entry to be written by uCode */
4138 u32 size; /* # entries that we'll print */
4139
4140 base = le32_to_cpu(priv->card_alive.log_event_table_ptr);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004141 if (!iwl3945_hw_valid_rtc_data_addr(base)) {
Zhu Yib481de92007-09-25 17:54:57 -07004142 IWL_ERROR("Invalid event log pointer 0x%08X\n", base);
4143 return;
4144 }
4145
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004146 rc = iwl3945_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004147 if (rc) {
4148 IWL_WARNING("Can not read from adapter at this time.\n");
4149 return;
4150 }
4151
4152 /* event log header */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004153 capacity = iwl3945_read_targ_mem(priv, base);
4154 mode = iwl3945_read_targ_mem(priv, base + (1 * sizeof(u32)));
4155 num_wraps = iwl3945_read_targ_mem(priv, base + (2 * sizeof(u32)));
4156 next_entry = iwl3945_read_targ_mem(priv, base + (3 * sizeof(u32)));
Zhu Yib481de92007-09-25 17:54:57 -07004157
4158 size = num_wraps ? capacity : next_entry;
4159
4160 /* bail out if nothing in log */
4161 if (size == 0) {
Zhu Yi583fab32007-09-27 11:27:30 +08004162 IWL_ERROR("Start IWL Event Log Dump: nothing in log\n");
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004163 iwl3945_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004164 return;
4165 }
4166
Zhu Yi583fab32007-09-27 11:27:30 +08004167 IWL_ERROR("Start IWL Event Log Dump: display count %d, wraps %d\n",
Zhu Yib481de92007-09-25 17:54:57 -07004168 size, num_wraps);
4169
4170 /* if uCode has wrapped back to top of log, start at the oldest entry,
4171 * i.e the next one that uCode would fill. */
4172 if (num_wraps)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004173 iwl3945_print_event_log(priv, next_entry,
Zhu Yib481de92007-09-25 17:54:57 -07004174 capacity - next_entry, mode);
4175
4176 /* (then/else) start at top of log */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004177 iwl3945_print_event_log(priv, 0, next_entry, mode);
Zhu Yib481de92007-09-25 17:54:57 -07004178
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004179 iwl3945_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004180}
4181
4182/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004183 * iwl3945_irq_handle_error - called for HW or SW error interrupt from card
Zhu Yib481de92007-09-25 17:54:57 -07004184 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004185static void iwl3945_irq_handle_error(struct iwl3945_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004186{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004187 /* Set the FW error flag -- cleared on iwl3945_down */
Zhu Yib481de92007-09-25 17:54:57 -07004188 set_bit(STATUS_FW_ERROR, &priv->status);
4189
4190 /* Cancel currently queued command. */
4191 clear_bit(STATUS_HCMD_ACTIVE, &priv->status);
4192
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08004193#ifdef CONFIG_IWL3945_DEBUG
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004194 if (iwl3945_debug_level & IWL_DL_FW_ERRORS) {
4195 iwl3945_dump_nic_error_log(priv);
4196 iwl3945_dump_nic_event_log(priv);
4197 iwl3945_print_rx_config_cmd(&priv->staging_rxon);
Zhu Yib481de92007-09-25 17:54:57 -07004198 }
4199#endif
4200
4201 wake_up_interruptible(&priv->wait_command_queue);
4202
4203 /* Keep the restart process from trying to send host
4204 * commands by clearing the INIT status bit */
4205 clear_bit(STATUS_READY, &priv->status);
4206
4207 if (!test_bit(STATUS_EXIT_PENDING, &priv->status)) {
4208 IWL_DEBUG(IWL_DL_INFO | IWL_DL_FW_ERRORS,
4209 "Restarting adapter due to uCode error.\n");
4210
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004211 if (iwl3945_is_associated(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07004212 memcpy(&priv->recovery_rxon, &priv->active_rxon,
4213 sizeof(priv->recovery_rxon));
4214 priv->error_recovering = 1;
4215 }
4216 queue_work(priv->workqueue, &priv->restart);
4217 }
4218}
4219
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004220static void iwl3945_error_recovery(struct iwl3945_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004221{
4222 unsigned long flags;
4223
4224 memcpy(&priv->staging_rxon, &priv->recovery_rxon,
4225 sizeof(priv->staging_rxon));
4226 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004227 iwl3945_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004228
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004229 iwl3945_add_station(priv, priv->bssid, 1, 0);
Zhu Yib481de92007-09-25 17:54:57 -07004230
4231 spin_lock_irqsave(&priv->lock, flags);
4232 priv->assoc_id = le16_to_cpu(priv->staging_rxon.assoc_id);
4233 priv->error_recovering = 0;
4234 spin_unlock_irqrestore(&priv->lock, flags);
4235}
4236
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004237static void iwl3945_irq_tasklet(struct iwl3945_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004238{
4239 u32 inta, handled = 0;
4240 u32 inta_fh;
4241 unsigned long flags;
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08004242#ifdef CONFIG_IWL3945_DEBUG
Zhu Yib481de92007-09-25 17:54:57 -07004243 u32 inta_mask;
4244#endif
4245
4246 spin_lock_irqsave(&priv->lock, flags);
4247
4248 /* Ack/clear/reset pending uCode interrupts.
4249 * Note: Some bits in CSR_INT are "OR" of bits in CSR_FH_INT_STATUS,
4250 * and will clear only when CSR_FH_INT_STATUS gets cleared. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004251 inta = iwl3945_read32(priv, CSR_INT);
4252 iwl3945_write32(priv, CSR_INT, inta);
Zhu Yib481de92007-09-25 17:54:57 -07004253
4254 /* Ack/clear/reset pending flow-handler (DMA) interrupts.
4255 * Any new interrupts that happen after this, either while we're
4256 * in this tasklet, or later, will show up in next ISR/tasklet. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004257 inta_fh = iwl3945_read32(priv, CSR_FH_INT_STATUS);
4258 iwl3945_write32(priv, CSR_FH_INT_STATUS, inta_fh);
Zhu Yib481de92007-09-25 17:54:57 -07004259
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08004260#ifdef CONFIG_IWL3945_DEBUG
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004261 if (iwl3945_debug_level & IWL_DL_ISR) {
Ben Cahill9fbab512007-11-29 11:09:47 +08004262 /* just for debug */
4263 inta_mask = iwl3945_read32(priv, CSR_INT_MASK);
Zhu Yib481de92007-09-25 17:54:57 -07004264 IWL_DEBUG_ISR("inta 0x%08x, enabled 0x%08x, fh 0x%08x\n",
4265 inta, inta_mask, inta_fh);
4266 }
4267#endif
4268
4269 /* Since CSR_INT and CSR_FH_INT_STATUS reads and clears are not
4270 * atomic, make sure that inta covers all the interrupts that
4271 * we've discovered, even if FH interrupt came in just after
4272 * reading CSR_INT. */
Tomas Winkler6f83eaa2008-03-04 18:09:28 -08004273 if (inta_fh & CSR39_FH_INT_RX_MASK)
Zhu Yib481de92007-09-25 17:54:57 -07004274 inta |= CSR_INT_BIT_FH_RX;
Tomas Winkler6f83eaa2008-03-04 18:09:28 -08004275 if (inta_fh & CSR39_FH_INT_TX_MASK)
Zhu Yib481de92007-09-25 17:54:57 -07004276 inta |= CSR_INT_BIT_FH_TX;
4277
4278 /* Now service all interrupt bits discovered above. */
4279 if (inta & CSR_INT_BIT_HW_ERR) {
4280 IWL_ERROR("Microcode HW error detected. Restarting.\n");
4281
4282 /* Tell the device to stop sending interrupts */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004283 iwl3945_disable_interrupts(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004284
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004285 iwl3945_irq_handle_error(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004286
4287 handled |= CSR_INT_BIT_HW_ERR;
4288
4289 spin_unlock_irqrestore(&priv->lock, flags);
4290
4291 return;
4292 }
4293
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08004294#ifdef CONFIG_IWL3945_DEBUG
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004295 if (iwl3945_debug_level & (IWL_DL_ISR)) {
Zhu Yib481de92007-09-25 17:54:57 -07004296 /* NIC fires this, but we don't use it, redundant with WAKEUP */
Joonwoo Park25c03d82008-01-23 10:15:20 -08004297 if (inta & CSR_INT_BIT_SCD)
4298 IWL_DEBUG_ISR("Scheduler finished to transmit "
4299 "the frame/frames.\n");
Zhu Yib481de92007-09-25 17:54:57 -07004300
4301 /* Alive notification via Rx interrupt will do the real work */
4302 if (inta & CSR_INT_BIT_ALIVE)
4303 IWL_DEBUG_ISR("Alive interrupt\n");
4304 }
4305#endif
4306 /* Safely ignore these bits for debug checks below */
Joonwoo Park25c03d82008-01-23 10:15:20 -08004307 inta &= ~(CSR_INT_BIT_SCD | CSR_INT_BIT_ALIVE);
Zhu Yib481de92007-09-25 17:54:57 -07004308
Zhu Yib481de92007-09-25 17:54:57 -07004309 /* Error detected by uCode */
4310 if (inta & CSR_INT_BIT_SW_ERR) {
4311 IWL_ERROR("Microcode SW error detected. Restarting 0x%X.\n",
4312 inta);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004313 iwl3945_irq_handle_error(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004314 handled |= CSR_INT_BIT_SW_ERR;
4315 }
4316
4317 /* uCode wakes up after power-down sleep */
4318 if (inta & CSR_INT_BIT_WAKEUP) {
4319 IWL_DEBUG_ISR("Wakeup interrupt\n");
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004320 iwl3945_rx_queue_update_write_ptr(priv, &priv->rxq);
4321 iwl3945_tx_queue_update_write_ptr(priv, &priv->txq[0]);
4322 iwl3945_tx_queue_update_write_ptr(priv, &priv->txq[1]);
4323 iwl3945_tx_queue_update_write_ptr(priv, &priv->txq[2]);
4324 iwl3945_tx_queue_update_write_ptr(priv, &priv->txq[3]);
4325 iwl3945_tx_queue_update_write_ptr(priv, &priv->txq[4]);
4326 iwl3945_tx_queue_update_write_ptr(priv, &priv->txq[5]);
Zhu Yib481de92007-09-25 17:54:57 -07004327
4328 handled |= CSR_INT_BIT_WAKEUP;
4329 }
4330
4331 /* All uCode command responses, including Tx command responses,
4332 * Rx "responses" (frame-received notification), and other
4333 * notifications from uCode come through here*/
4334 if (inta & (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX)) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004335 iwl3945_rx_handle(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004336 handled |= (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX);
4337 }
4338
4339 if (inta & CSR_INT_BIT_FH_TX) {
4340 IWL_DEBUG_ISR("Tx interrupt\n");
4341
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004342 iwl3945_write32(priv, CSR_FH_INT_STATUS, (1 << 6));
4343 if (!iwl3945_grab_nic_access(priv)) {
Tomas Winklerbddadf82008-12-19 10:37:01 +08004344 iwl3945_write_direct32(priv, FH39_TCSR_CREDIT
4345 (FH39_SRVC_CHNL), 0x0);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004346 iwl3945_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004347 }
4348 handled |= CSR_INT_BIT_FH_TX;
4349 }
4350
4351 if (inta & ~handled)
4352 IWL_ERROR("Unhandled INTA bits 0x%08x\n", inta & ~handled);
4353
4354 if (inta & ~CSR_INI_SET_MASK) {
4355 IWL_WARNING("Disabled INTA bits 0x%08x were pending\n",
4356 inta & ~CSR_INI_SET_MASK);
4357 IWL_WARNING(" with FH_INT = 0x%08x\n", inta_fh);
4358 }
4359
4360 /* Re-enable all interrupts */
Mohamed Abbas0359fac2008-03-28 16:21:08 -07004361 /* only Re-enable if disabled by irq */
4362 if (test_bit(STATUS_INT_ENABLED, &priv->status))
4363 iwl3945_enable_interrupts(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004364
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08004365#ifdef CONFIG_IWL3945_DEBUG
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004366 if (iwl3945_debug_level & (IWL_DL_ISR)) {
4367 inta = iwl3945_read32(priv, CSR_INT);
4368 inta_mask = iwl3945_read32(priv, CSR_INT_MASK);
4369 inta_fh = iwl3945_read32(priv, CSR_FH_INT_STATUS);
Zhu Yib481de92007-09-25 17:54:57 -07004370 IWL_DEBUG_ISR("End inta 0x%08x, enabled 0x%08x, fh 0x%08x, "
4371 "flags 0x%08lx\n", inta, inta_mask, inta_fh, flags);
4372 }
4373#endif
4374 spin_unlock_irqrestore(&priv->lock, flags);
4375}
4376
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004377static irqreturn_t iwl3945_isr(int irq, void *data)
Zhu Yib481de92007-09-25 17:54:57 -07004378{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004379 struct iwl3945_priv *priv = data;
Zhu Yib481de92007-09-25 17:54:57 -07004380 u32 inta, inta_mask;
4381 u32 inta_fh;
4382 if (!priv)
4383 return IRQ_NONE;
4384
4385 spin_lock(&priv->lock);
4386
4387 /* Disable (but don't clear!) interrupts here to avoid
4388 * back-to-back ISRs and sporadic interrupts from our NIC.
4389 * If we have something to service, the tasklet will re-enable ints.
4390 * If we *don't* have something, we'll re-enable before leaving here. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004391 inta_mask = iwl3945_read32(priv, CSR_INT_MASK); /* just for debug */
4392 iwl3945_write32(priv, CSR_INT_MASK, 0x00000000);
Zhu Yib481de92007-09-25 17:54:57 -07004393
4394 /* Discover which interrupts are active/pending */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004395 inta = iwl3945_read32(priv, CSR_INT);
4396 inta_fh = iwl3945_read32(priv, CSR_FH_INT_STATUS);
Zhu Yib481de92007-09-25 17:54:57 -07004397
4398 /* Ignore interrupt if there's nothing in NIC to service.
4399 * This may be due to IRQ shared with another device,
4400 * or due to sporadic interrupts thrown from our NIC. */
4401 if (!inta && !inta_fh) {
4402 IWL_DEBUG_ISR("Ignore interrupt, inta == 0, inta_fh == 0\n");
4403 goto none;
4404 }
4405
4406 if ((inta == 0xFFFFFFFF) || ((inta & 0xFFFFFFF0) == 0xa5a5a5a0)) {
4407 /* Hardware disappeared */
Zhu Yi99df6302008-12-02 12:13:58 -08004408 IWL_WARNING("HARDWARE GONE?? INTA == 0x%08x\n", inta);
Oliver Neukumcb4da1a2007-11-13 21:10:32 -08004409 goto unplugged;
Zhu Yib481de92007-09-25 17:54:57 -07004410 }
4411
4412 IWL_DEBUG_ISR("ISR inta 0x%08x, enabled 0x%08x, fh 0x%08x\n",
4413 inta, inta_mask, inta_fh);
4414
Joonwoo Park25c03d82008-01-23 10:15:20 -08004415 inta &= ~CSR_INT_BIT_SCD;
4416
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004417 /* iwl3945_irq_tasklet() will service interrupts and re-enable them */
Joonwoo Park25c03d82008-01-23 10:15:20 -08004418 if (likely(inta || inta_fh))
4419 tasklet_schedule(&priv->irq_tasklet);
Oliver Neukumcb4da1a2007-11-13 21:10:32 -08004420unplugged:
Zhu Yib481de92007-09-25 17:54:57 -07004421 spin_unlock(&priv->lock);
4422
4423 return IRQ_HANDLED;
4424
4425 none:
4426 /* re-enable interrupts here since we don't have anything to service. */
Mohamed Abbas0359fac2008-03-28 16:21:08 -07004427 /* only Re-enable if disabled by irq */
4428 if (test_bit(STATUS_INT_ENABLED, &priv->status))
4429 iwl3945_enable_interrupts(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004430 spin_unlock(&priv->lock);
4431 return IRQ_NONE;
4432}
4433
4434/************************** EEPROM BANDS ****************************
4435 *
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004436 * The iwl3945_eeprom_band definitions below provide the mapping from the
Zhu Yib481de92007-09-25 17:54:57 -07004437 * EEPROM contents to the specific channel number supported for each
4438 * band.
4439 *
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004440 * For example, iwl3945_priv->eeprom.band_3_channels[4] from the band_3
Zhu Yib481de92007-09-25 17:54:57 -07004441 * definition below maps to physical channel 42 in the 5.2GHz spectrum.
4442 * The specific geography and calibration information for that channel
4443 * is contained in the eeprom map itself.
4444 *
4445 * During init, we copy the eeprom information and channel map
4446 * information into priv->channel_info_24/52 and priv->channel_map_24/52
4447 *
4448 * channel_map_24/52 provides the index in the channel_info array for a
4449 * given channel. We have to have two separate maps as there is channel
4450 * overlap with the 2.4GHz and 5.2GHz spectrum as seen in band_1 and
4451 * band_2
4452 *
4453 * A value of 0xff stored in the channel_map indicates that the channel
4454 * is not supported by the hardware at all.
4455 *
4456 * A value of 0xfe in the channel_map indicates that the channel is not
4457 * valid for Tx with the current hardware. This means that
4458 * while the system can tune and receive on a given channel, it may not
4459 * be able to associate or transmit any frames on that
4460 * channel. There is no corresponding channel information for that
4461 * entry.
4462 *
4463 *********************************************************************/
4464
4465/* 2.4 GHz */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004466static const u8 iwl3945_eeprom_band_1[14] = {
Zhu Yib481de92007-09-25 17:54:57 -07004467 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14
4468};
4469
4470/* 5.2 GHz bands */
Ben Cahill9fbab512007-11-29 11:09:47 +08004471static const u8 iwl3945_eeprom_band_2[] = { /* 4915-5080MHz */
Zhu Yib481de92007-09-25 17:54:57 -07004472 183, 184, 185, 187, 188, 189, 192, 196, 7, 8, 11, 12, 16
4473};
4474
Ben Cahill9fbab512007-11-29 11:09:47 +08004475static const u8 iwl3945_eeprom_band_3[] = { /* 5170-5320MHz */
Zhu Yib481de92007-09-25 17:54:57 -07004476 34, 36, 38, 40, 42, 44, 46, 48, 52, 56, 60, 64
4477};
4478
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004479static const u8 iwl3945_eeprom_band_4[] = { /* 5500-5700MHz */
Zhu Yib481de92007-09-25 17:54:57 -07004480 100, 104, 108, 112, 116, 120, 124, 128, 132, 136, 140
4481};
4482
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004483static const u8 iwl3945_eeprom_band_5[] = { /* 5725-5825MHz */
Zhu Yib481de92007-09-25 17:54:57 -07004484 145, 149, 153, 157, 161, 165
4485};
4486
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004487static void iwl3945_init_band_reference(const struct iwl3945_priv *priv, int band,
Zhu Yib481de92007-09-25 17:54:57 -07004488 int *eeprom_ch_count,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004489 const struct iwl3945_eeprom_channel
Zhu Yib481de92007-09-25 17:54:57 -07004490 **eeprom_ch_info,
4491 const u8 **eeprom_ch_index)
4492{
4493 switch (band) {
4494 case 1: /* 2.4GHz band */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004495 *eeprom_ch_count = ARRAY_SIZE(iwl3945_eeprom_band_1);
Zhu Yib481de92007-09-25 17:54:57 -07004496 *eeprom_ch_info = priv->eeprom.band_1_channels;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004497 *eeprom_ch_index = iwl3945_eeprom_band_1;
Zhu Yib481de92007-09-25 17:54:57 -07004498 break;
Ben Cahill9fbab512007-11-29 11:09:47 +08004499 case 2: /* 4.9GHz band */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004500 *eeprom_ch_count = ARRAY_SIZE(iwl3945_eeprom_band_2);
Zhu Yib481de92007-09-25 17:54:57 -07004501 *eeprom_ch_info = priv->eeprom.band_2_channels;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004502 *eeprom_ch_index = iwl3945_eeprom_band_2;
Zhu Yib481de92007-09-25 17:54:57 -07004503 break;
4504 case 3: /* 5.2GHz band */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004505 *eeprom_ch_count = ARRAY_SIZE(iwl3945_eeprom_band_3);
Zhu Yib481de92007-09-25 17:54:57 -07004506 *eeprom_ch_info = priv->eeprom.band_3_channels;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004507 *eeprom_ch_index = iwl3945_eeprom_band_3;
Zhu Yib481de92007-09-25 17:54:57 -07004508 break;
Ben Cahill9fbab512007-11-29 11:09:47 +08004509 case 4: /* 5.5GHz band */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004510 *eeprom_ch_count = ARRAY_SIZE(iwl3945_eeprom_band_4);
Zhu Yib481de92007-09-25 17:54:57 -07004511 *eeprom_ch_info = priv->eeprom.band_4_channels;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004512 *eeprom_ch_index = iwl3945_eeprom_band_4;
Zhu Yib481de92007-09-25 17:54:57 -07004513 break;
Ben Cahill9fbab512007-11-29 11:09:47 +08004514 case 5: /* 5.7GHz band */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004515 *eeprom_ch_count = ARRAY_SIZE(iwl3945_eeprom_band_5);
Zhu Yib481de92007-09-25 17:54:57 -07004516 *eeprom_ch_info = priv->eeprom.band_5_channels;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004517 *eeprom_ch_index = iwl3945_eeprom_band_5;
Zhu Yib481de92007-09-25 17:54:57 -07004518 break;
4519 default:
4520 BUG();
4521 return;
4522 }
4523}
4524
Cahill, Ben M6440adb2007-11-29 11:09:55 +08004525/**
4526 * iwl3945_get_channel_info - Find driver's private channel info
4527 *
4528 * Based on band and channel number.
4529 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004530const struct iwl3945_channel_info *iwl3945_get_channel_info(const struct iwl3945_priv *priv,
Johannes Berg8318d782008-01-24 19:38:38 +01004531 enum ieee80211_band band, u16 channel)
Zhu Yib481de92007-09-25 17:54:57 -07004532{
4533 int i;
4534
Johannes Berg8318d782008-01-24 19:38:38 +01004535 switch (band) {
4536 case IEEE80211_BAND_5GHZ:
Zhu Yib481de92007-09-25 17:54:57 -07004537 for (i = 14; i < priv->channel_count; i++) {
4538 if (priv->channel_info[i].channel == channel)
4539 return &priv->channel_info[i];
4540 }
4541 break;
4542
Johannes Berg8318d782008-01-24 19:38:38 +01004543 case IEEE80211_BAND_2GHZ:
Zhu Yib481de92007-09-25 17:54:57 -07004544 if (channel >= 1 && channel <= 14)
4545 return &priv->channel_info[channel - 1];
4546 break;
Johannes Berg8318d782008-01-24 19:38:38 +01004547 case IEEE80211_NUM_BANDS:
4548 WARN_ON(1);
Zhu Yib481de92007-09-25 17:54:57 -07004549 }
4550
4551 return NULL;
4552}
4553
4554#define CHECK_AND_PRINT(x) ((eeprom_ch_info[ch].flags & EEPROM_CHANNEL_##x) \
4555 ? # x " " : "")
4556
Cahill, Ben M6440adb2007-11-29 11:09:55 +08004557/**
4558 * iwl3945_init_channel_map - Set up driver's info for all possible channels
4559 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004560static int iwl3945_init_channel_map(struct iwl3945_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004561{
4562 int eeprom_ch_count = 0;
4563 const u8 *eeprom_ch_index = NULL;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004564 const struct iwl3945_eeprom_channel *eeprom_ch_info = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07004565 int band, ch;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004566 struct iwl3945_channel_info *ch_info;
Zhu Yib481de92007-09-25 17:54:57 -07004567
4568 if (priv->channel_count) {
4569 IWL_DEBUG_INFO("Channel map already initialized.\n");
4570 return 0;
4571 }
4572
4573 if (priv->eeprom.version < 0x2f) {
4574 IWL_WARNING("Unsupported EEPROM version: 0x%04X\n",
4575 priv->eeprom.version);
4576 return -EINVAL;
4577 }
4578
4579 IWL_DEBUG_INFO("Initializing regulatory info from EEPROM\n");
4580
4581 priv->channel_count =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004582 ARRAY_SIZE(iwl3945_eeprom_band_1) +
4583 ARRAY_SIZE(iwl3945_eeprom_band_2) +
4584 ARRAY_SIZE(iwl3945_eeprom_band_3) +
4585 ARRAY_SIZE(iwl3945_eeprom_band_4) +
4586 ARRAY_SIZE(iwl3945_eeprom_band_5);
Zhu Yib481de92007-09-25 17:54:57 -07004587
4588 IWL_DEBUG_INFO("Parsing data for %d channels.\n", priv->channel_count);
4589
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004590 priv->channel_info = kzalloc(sizeof(struct iwl3945_channel_info) *
Zhu Yib481de92007-09-25 17:54:57 -07004591 priv->channel_count, GFP_KERNEL);
4592 if (!priv->channel_info) {
4593 IWL_ERROR("Could not allocate channel_info\n");
4594 priv->channel_count = 0;
4595 return -ENOMEM;
4596 }
4597
4598 ch_info = priv->channel_info;
4599
4600 /* Loop through the 5 EEPROM bands adding them in order to the
4601 * channel map we maintain (that contains additional information than
4602 * what just in the EEPROM) */
4603 for (band = 1; band <= 5; band++) {
4604
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004605 iwl3945_init_band_reference(priv, band, &eeprom_ch_count,
Zhu Yib481de92007-09-25 17:54:57 -07004606 &eeprom_ch_info, &eeprom_ch_index);
4607
4608 /* Loop through each band adding each of the channels */
4609 for (ch = 0; ch < eeprom_ch_count; ch++) {
4610 ch_info->channel = eeprom_ch_index[ch];
Johannes Berg8318d782008-01-24 19:38:38 +01004611 ch_info->band = (band == 1) ? IEEE80211_BAND_2GHZ :
4612 IEEE80211_BAND_5GHZ;
Zhu Yib481de92007-09-25 17:54:57 -07004613
4614 /* permanently store EEPROM's channel regulatory flags
4615 * and max power in channel info database. */
4616 ch_info->eeprom = eeprom_ch_info[ch];
4617
4618 /* Copy the run-time flags so they are there even on
4619 * invalid channels */
4620 ch_info->flags = eeprom_ch_info[ch].flags;
4621
4622 if (!(is_channel_valid(ch_info))) {
4623 IWL_DEBUG_INFO("Ch. %d Flags %x [%sGHz] - "
4624 "No traffic\n",
4625 ch_info->channel,
4626 ch_info->flags,
4627 is_channel_a_band(ch_info) ?
4628 "5.2" : "2.4");
4629 ch_info++;
4630 continue;
4631 }
4632
4633 /* Initialize regulatory-based run-time data */
4634 ch_info->max_power_avg = ch_info->curr_txpow =
4635 eeprom_ch_info[ch].max_power_avg;
4636 ch_info->scan_power = eeprom_ch_info[ch].max_power_avg;
4637 ch_info->min_power = 0;
4638
Guy Cohenfe7c4042008-04-21 15:41:56 -07004639 IWL_DEBUG_INFO("Ch. %d [%sGHz] %s%s%s%s%s%s(0x%02x"
Zhu Yib481de92007-09-25 17:54:57 -07004640 " %ddBm): Ad-Hoc %ssupported\n",
4641 ch_info->channel,
4642 is_channel_a_band(ch_info) ?
4643 "5.2" : "2.4",
Tomas Winkler8211ef72008-03-02 01:36:04 +02004644 CHECK_AND_PRINT(VALID),
Zhu Yib481de92007-09-25 17:54:57 -07004645 CHECK_AND_PRINT(IBSS),
4646 CHECK_AND_PRINT(ACTIVE),
4647 CHECK_AND_PRINT(RADAR),
4648 CHECK_AND_PRINT(WIDE),
Zhu Yib481de92007-09-25 17:54:57 -07004649 CHECK_AND_PRINT(DFS),
4650 eeprom_ch_info[ch].flags,
4651 eeprom_ch_info[ch].max_power_avg,
4652 ((eeprom_ch_info[ch].
4653 flags & EEPROM_CHANNEL_IBSS)
4654 && !(eeprom_ch_info[ch].
4655 flags & EEPROM_CHANNEL_RADAR))
4656 ? "" : "not ");
4657
4658 /* Set the user_txpower_limit to the highest power
4659 * supported by any channel */
4660 if (eeprom_ch_info[ch].max_power_avg >
4661 priv->user_txpower_limit)
4662 priv->user_txpower_limit =
4663 eeprom_ch_info[ch].max_power_avg;
4664
4665 ch_info++;
4666 }
4667 }
4668
Cahill, Ben M6440adb2007-11-29 11:09:55 +08004669 /* Set up txpower settings in driver for all channels */
Zhu Yib481de92007-09-25 17:54:57 -07004670 if (iwl3945_txpower_set_from_eeprom(priv))
4671 return -EIO;
4672
4673 return 0;
4674}
4675
Reinette Chatre849e0dc2008-01-23 10:15:18 -08004676/*
4677 * iwl3945_free_channel_map - undo allocations in iwl3945_init_channel_map
4678 */
4679static void iwl3945_free_channel_map(struct iwl3945_priv *priv)
4680{
4681 kfree(priv->channel_info);
4682 priv->channel_count = 0;
4683}
4684
Zhu Yib481de92007-09-25 17:54:57 -07004685/* For active scan, listen ACTIVE_DWELL_TIME (msec) on each channel after
4686 * sending probe req. This should be set long enough to hear probe responses
4687 * from more than one AP. */
Abhijeet Kolekarf9340522008-09-03 11:26:58 +08004688#define IWL_ACTIVE_DWELL_TIME_24 (30) /* all times in msec */
4689#define IWL_ACTIVE_DWELL_TIME_52 (20)
4690
4691#define IWL_ACTIVE_DWELL_FACTOR_24GHZ (3)
4692#define IWL_ACTIVE_DWELL_FACTOR_52GHZ (2)
Zhu Yib481de92007-09-25 17:54:57 -07004693
4694/* For faster active scanning, scan will move to the next channel if fewer than
4695 * PLCP_QUIET_THRESH packets are heard on this channel within
4696 * ACTIVE_QUIET_TIME after sending probe request. This shortens the dwell
4697 * time if it's a quiet channel (nothing responded to our probe, and there's
4698 * no other traffic).
4699 * Disable "quiet" feature by setting PLCP_QUIET_THRESH to 0. */
4700#define IWL_PLCP_QUIET_THRESH __constant_cpu_to_le16(1) /* packets */
Abhijeet Kolekarf9340522008-09-03 11:26:58 +08004701#define IWL_ACTIVE_QUIET_TIME __constant_cpu_to_le16(10) /* msec */
Zhu Yib481de92007-09-25 17:54:57 -07004702
4703/* For passive scan, listen PASSIVE_DWELL_TIME (msec) on each channel.
4704 * Must be set longer than active dwell time.
4705 * For the most reliable scan, set > AP beacon interval (typically 100msec). */
4706#define IWL_PASSIVE_DWELL_TIME_24 (20) /* all times in msec */
4707#define IWL_PASSIVE_DWELL_TIME_52 (10)
4708#define IWL_PASSIVE_DWELL_BASE (100)
4709#define IWL_CHANNEL_TUNE_TIME 5
4710
Kolekar, Abhijeete720ce92008-11-07 09:58:42 -08004711#define IWL_SCAN_PROBE_MASK(n) (BIT(n) | (BIT(n) - BIT(1)))
Abhijeet Kolekarf9340522008-09-03 11:26:58 +08004712
Johannes Berg8318d782008-01-24 19:38:38 +01004713static inline u16 iwl3945_get_active_dwell_time(struct iwl3945_priv *priv,
Abhijeet Kolekarf9340522008-09-03 11:26:58 +08004714 enum ieee80211_band band,
4715 u8 n_probes)
Zhu Yib481de92007-09-25 17:54:57 -07004716{
Johannes Berg8318d782008-01-24 19:38:38 +01004717 if (band == IEEE80211_BAND_5GHZ)
Abhijeet Kolekarf9340522008-09-03 11:26:58 +08004718 return IWL_ACTIVE_DWELL_TIME_52 +
4719 IWL_ACTIVE_DWELL_FACTOR_52GHZ * (n_probes + 1);
Zhu Yib481de92007-09-25 17:54:57 -07004720 else
Abhijeet Kolekarf9340522008-09-03 11:26:58 +08004721 return IWL_ACTIVE_DWELL_TIME_24 +
4722 IWL_ACTIVE_DWELL_FACTOR_24GHZ * (n_probes + 1);
Zhu Yib481de92007-09-25 17:54:57 -07004723}
4724
Johannes Berg8318d782008-01-24 19:38:38 +01004725static u16 iwl3945_get_passive_dwell_time(struct iwl3945_priv *priv,
4726 enum ieee80211_band band)
Zhu Yib481de92007-09-25 17:54:57 -07004727{
Johannes Berg8318d782008-01-24 19:38:38 +01004728 u16 passive = (band == IEEE80211_BAND_2GHZ) ?
Zhu Yib481de92007-09-25 17:54:57 -07004729 IWL_PASSIVE_DWELL_BASE + IWL_PASSIVE_DWELL_TIME_24 :
4730 IWL_PASSIVE_DWELL_BASE + IWL_PASSIVE_DWELL_TIME_52;
4731
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004732 if (iwl3945_is_associated(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07004733 /* If we're associated, we clamp the maximum passive
4734 * dwell time to be 98% of the beacon interval (minus
4735 * 2 * channel tune time) */
4736 passive = priv->beacon_int;
4737 if ((passive > IWL_PASSIVE_DWELL_BASE) || !passive)
4738 passive = IWL_PASSIVE_DWELL_BASE;
4739 passive = (passive * 98) / 100 - IWL_CHANNEL_TUNE_TIME * 2;
4740 }
4741
Zhu Yib481de92007-09-25 17:54:57 -07004742 return passive;
4743}
4744
Johannes Berg8318d782008-01-24 19:38:38 +01004745static int iwl3945_get_channels_for_scan(struct iwl3945_priv *priv,
4746 enum ieee80211_band band,
Abhijeet Kolekarf9340522008-09-03 11:26:58 +08004747 u8 is_active, u8 n_probes,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004748 struct iwl3945_scan_channel *scan_ch)
Zhu Yib481de92007-09-25 17:54:57 -07004749{
4750 const struct ieee80211_channel *channels = NULL;
Johannes Berg8318d782008-01-24 19:38:38 +01004751 const struct ieee80211_supported_band *sband;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004752 const struct iwl3945_channel_info *ch_info;
Zhu Yib481de92007-09-25 17:54:57 -07004753 u16 passive_dwell = 0;
4754 u16 active_dwell = 0;
4755 int added, i;
4756
Johannes Berg8318d782008-01-24 19:38:38 +01004757 sband = iwl3945_get_band(priv, band);
4758 if (!sband)
Zhu Yib481de92007-09-25 17:54:57 -07004759 return 0;
4760
Johannes Berg8318d782008-01-24 19:38:38 +01004761 channels = sband->channels;
Zhu Yib481de92007-09-25 17:54:57 -07004762
Abhijeet Kolekarf9340522008-09-03 11:26:58 +08004763 active_dwell = iwl3945_get_active_dwell_time(priv, band, n_probes);
Johannes Berg8318d782008-01-24 19:38:38 +01004764 passive_dwell = iwl3945_get_passive_dwell_time(priv, band);
Zhu Yib481de92007-09-25 17:54:57 -07004765
Abhijeet Kolekar8f4807a2008-09-03 11:26:31 +08004766 if (passive_dwell <= active_dwell)
4767 passive_dwell = active_dwell + 1;
4768
Johannes Berg8318d782008-01-24 19:38:38 +01004769 for (i = 0, added = 0; i < sband->n_channels; i++) {
Johannes Berg182e2e62008-04-04 10:41:56 +02004770 if (channels[i].flags & IEEE80211_CHAN_DISABLED)
4771 continue;
4772
Johannes Berg8318d782008-01-24 19:38:38 +01004773 scan_ch->channel = channels[i].hw_value;
Zhu Yib481de92007-09-25 17:54:57 -07004774
Johannes Berg8318d782008-01-24 19:38:38 +01004775 ch_info = iwl3945_get_channel_info(priv, band, scan_ch->channel);
Zhu Yib481de92007-09-25 17:54:57 -07004776 if (!is_channel_valid(ch_info)) {
Ron Rindjunsky66b50042008-06-25 16:46:31 +08004777 IWL_DEBUG_SCAN("Channel %d is INVALID for this band.\n",
Zhu Yib481de92007-09-25 17:54:57 -07004778 scan_ch->channel);
4779 continue;
4780 }
4781
Zhu Yib481de92007-09-25 17:54:57 -07004782 scan_ch->active_dwell = cpu_to_le16(active_dwell);
4783 scan_ch->passive_dwell = cpu_to_le16(passive_dwell);
Abhijeet Kolekar011a0332008-12-02 12:14:07 -08004784 /* If passive , set up for auto-switch
4785 * and use long active_dwell time.
4786 */
4787 if (!is_active || is_channel_passive(ch_info) ||
4788 (channels[i].flags & IEEE80211_CHAN_PASSIVE_SCAN)) {
4789 scan_ch->type = 0; /* passive */
4790 if (IWL_UCODE_API(priv->ucode_ver) == 1)
4791 scan_ch->active_dwell = cpu_to_le16(passive_dwell - 1);
4792 } else {
4793 scan_ch->type = 1; /* active */
4794 }
4795
4796 /* Set direct probe bits. These may be used both for active
4797 * scan channels (probes gets sent right away),
4798 * or for passive channels (probes get se sent only after
4799 * hearing clear Rx packet).*/
4800 if (IWL_UCODE_API(priv->ucode_ver) >= 2) {
4801 if (n_probes)
4802 scan_ch->type |= IWL_SCAN_PROBE_MASK(n_probes);
4803 } else {
4804 /* uCode v1 does not allow setting direct probe bits on
4805 * passive channel. */
4806 if ((scan_ch->type & 1) && n_probes)
4807 scan_ch->type |= IWL_SCAN_PROBE_MASK(n_probes);
4808 }
Zhu Yib481de92007-09-25 17:54:57 -07004809
Ben Cahill9fbab512007-11-29 11:09:47 +08004810 /* Set txpower levels to defaults */
Zhu Yib481de92007-09-25 17:54:57 -07004811 scan_ch->tpc.dsp_atten = 110;
4812 /* scan_pwr_info->tpc.dsp_atten; */
4813
4814 /*scan_pwr_info->tpc.tx_gain; */
Johannes Berg8318d782008-01-24 19:38:38 +01004815 if (band == IEEE80211_BAND_5GHZ)
Zhu Yib481de92007-09-25 17:54:57 -07004816 scan_ch->tpc.tx_gain = ((1 << 5) | (3 << 3)) | 3;
4817 else {
4818 scan_ch->tpc.tx_gain = ((1 << 5) | (5 << 3));
4819 /* NOTE: if we were doing 6Mb OFDM for scans we'd use
Ben Cahill9fbab512007-11-29 11:09:47 +08004820 * power level:
Reinette Chatre8a1b0242008-01-14 17:46:25 -08004821 * scan_ch->tpc.tx_gain = ((1 << 5) | (2 << 3)) | 3;
Zhu Yib481de92007-09-25 17:54:57 -07004822 */
4823 }
4824
4825 IWL_DEBUG_SCAN("Scanning %d [%s %d]\n",
4826 scan_ch->channel,
4827 (scan_ch->type & 1) ? "ACTIVE" : "PASSIVE",
4828 (scan_ch->type & 1) ?
4829 active_dwell : passive_dwell);
4830
4831 scan_ch++;
4832 added++;
4833 }
4834
4835 IWL_DEBUG_SCAN("total channels to scan %d \n", added);
4836 return added;
4837}
4838
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004839static void iwl3945_init_hw_rates(struct iwl3945_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07004840 struct ieee80211_rate *rates)
4841{
4842 int i;
4843
4844 for (i = 0; i < IWL_RATE_COUNT; i++) {
Johannes Berg8318d782008-01-24 19:38:38 +01004845 rates[i].bitrate = iwl3945_rates[i].ieee * 5;
4846 rates[i].hw_value = i; /* Rate scaling will work on indexes */
4847 rates[i].hw_value_short = i;
4848 rates[i].flags = 0;
4849 if ((i > IWL_LAST_OFDM_RATE) || (i < IWL_FIRST_OFDM_RATE)) {
Zhu Yib481de92007-09-25 17:54:57 -07004850 /*
Johannes Berg8318d782008-01-24 19:38:38 +01004851 * If CCK != 1M then set short preamble rate flag.
Zhu Yib481de92007-09-25 17:54:57 -07004852 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004853 rates[i].flags |= (iwl3945_rates[i].plcp == 10) ?
Johannes Berg8318d782008-01-24 19:38:38 +01004854 0 : IEEE80211_RATE_SHORT_PREAMBLE;
Zhu Yib481de92007-09-25 17:54:57 -07004855 }
Zhu Yib481de92007-09-25 17:54:57 -07004856 }
4857}
4858
4859/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004860 * iwl3945_init_geos - Initialize mac80211's geo/channel info based from eeprom
Zhu Yib481de92007-09-25 17:54:57 -07004861 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004862static int iwl3945_init_geos(struct iwl3945_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004863{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004864 struct iwl3945_channel_info *ch;
Tomas Winkler8211ef72008-03-02 01:36:04 +02004865 struct ieee80211_supported_band *sband;
Zhu Yib481de92007-09-25 17:54:57 -07004866 struct ieee80211_channel *channels;
4867 struct ieee80211_channel *geo_ch;
4868 struct ieee80211_rate *rates;
4869 int i = 0;
Zhu Yib481de92007-09-25 17:54:57 -07004870
Johannes Berg8318d782008-01-24 19:38:38 +01004871 if (priv->bands[IEEE80211_BAND_2GHZ].n_bitrates ||
4872 priv->bands[IEEE80211_BAND_5GHZ].n_bitrates) {
Zhu Yib481de92007-09-25 17:54:57 -07004873 IWL_DEBUG_INFO("Geography modes already initialized.\n");
4874 set_bit(STATUS_GEO_CONFIGURED, &priv->status);
4875 return 0;
4876 }
4877
Zhu Yib481de92007-09-25 17:54:57 -07004878 channels = kzalloc(sizeof(struct ieee80211_channel) *
4879 priv->channel_count, GFP_KERNEL);
Johannes Berg8318d782008-01-24 19:38:38 +01004880 if (!channels)
Zhu Yib481de92007-09-25 17:54:57 -07004881 return -ENOMEM;
Zhu Yib481de92007-09-25 17:54:57 -07004882
Tomas Winkler8211ef72008-03-02 01:36:04 +02004883 rates = kzalloc((sizeof(struct ieee80211_rate) * (IWL_RATE_COUNT + 1)),
Zhu Yib481de92007-09-25 17:54:57 -07004884 GFP_KERNEL);
4885 if (!rates) {
Zhu Yib481de92007-09-25 17:54:57 -07004886 kfree(channels);
4887 return -ENOMEM;
4888 }
4889
Zhu Yib481de92007-09-25 17:54:57 -07004890 /* 5.2GHz channels start after the 2.4GHz channels */
Tomas Winkler8211ef72008-03-02 01:36:04 +02004891 sband = &priv->bands[IEEE80211_BAND_5GHZ];
4892 sband->channels = &channels[ARRAY_SIZE(iwl3945_eeprom_band_1)];
4893 /* just OFDM */
4894 sband->bitrates = &rates[IWL_FIRST_OFDM_RATE];
4895 sband->n_bitrates = IWL_RATE_COUNT - IWL_FIRST_OFDM_RATE;
Zhu Yib481de92007-09-25 17:54:57 -07004896
Tomas Winkler8211ef72008-03-02 01:36:04 +02004897 sband = &priv->bands[IEEE80211_BAND_2GHZ];
4898 sband->channels = channels;
4899 /* OFDM & CCK */
4900 sband->bitrates = rates;
4901 sband->n_bitrates = IWL_RATE_COUNT;
Zhu Yib481de92007-09-25 17:54:57 -07004902
4903 priv->ieee_channels = channels;
4904 priv->ieee_rates = rates;
4905
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004906 iwl3945_init_hw_rates(priv, rates);
Zhu Yib481de92007-09-25 17:54:57 -07004907
Tomas Winkler8211ef72008-03-02 01:36:04 +02004908 for (i = 0; i < priv->channel_count; i++) {
Zhu Yib481de92007-09-25 17:54:57 -07004909 ch = &priv->channel_info[i];
4910
Tomas Winkler8211ef72008-03-02 01:36:04 +02004911 /* FIXME: might be removed if scan is OK*/
4912 if (!is_channel_valid(ch))
Zhu Yib481de92007-09-25 17:54:57 -07004913 continue;
Zhu Yib481de92007-09-25 17:54:57 -07004914
4915 if (is_channel_a_band(ch))
Tomas Winkler8211ef72008-03-02 01:36:04 +02004916 sband = &priv->bands[IEEE80211_BAND_5GHZ];
Johannes Berg8318d782008-01-24 19:38:38 +01004917 else
Tomas Winkler8211ef72008-03-02 01:36:04 +02004918 sband = &priv->bands[IEEE80211_BAND_2GHZ];
Zhu Yib481de92007-09-25 17:54:57 -07004919
Tomas Winkler8211ef72008-03-02 01:36:04 +02004920 geo_ch = &sband->channels[sband->n_channels++];
4921
4922 geo_ch->center_freq = ieee80211_channel_to_frequency(ch->channel);
Johannes Berg8318d782008-01-24 19:38:38 +01004923 geo_ch->max_power = ch->max_power_avg;
4924 geo_ch->max_antenna_gain = 0xff;
Mohamed Abbas7b723042008-01-31 21:46:40 -08004925 geo_ch->hw_value = ch->channel;
Zhu Yib481de92007-09-25 17:54:57 -07004926
4927 if (is_channel_valid(ch)) {
Johannes Berg8318d782008-01-24 19:38:38 +01004928 if (!(ch->flags & EEPROM_CHANNEL_IBSS))
4929 geo_ch->flags |= IEEE80211_CHAN_NO_IBSS;
Zhu Yib481de92007-09-25 17:54:57 -07004930
Johannes Berg8318d782008-01-24 19:38:38 +01004931 if (!(ch->flags & EEPROM_CHANNEL_ACTIVE))
4932 geo_ch->flags |= IEEE80211_CHAN_PASSIVE_SCAN;
Zhu Yib481de92007-09-25 17:54:57 -07004933
4934 if (ch->flags & EEPROM_CHANNEL_RADAR)
Johannes Berg8318d782008-01-24 19:38:38 +01004935 geo_ch->flags |= IEEE80211_CHAN_RADAR;
Zhu Yib481de92007-09-25 17:54:57 -07004936
4937 if (ch->max_power_avg > priv->max_channel_txpower_limit)
4938 priv->max_channel_txpower_limit =
4939 ch->max_power_avg;
Tomas Winkler8211ef72008-03-02 01:36:04 +02004940 } else {
Johannes Berg8318d782008-01-24 19:38:38 +01004941 geo_ch->flags |= IEEE80211_CHAN_DISABLED;
Tomas Winkler8211ef72008-03-02 01:36:04 +02004942 }
4943
4944 /* Save flags for reg domain usage */
4945 geo_ch->orig_flags = geo_ch->flags;
4946
4947 IWL_DEBUG_INFO("Channel %d Freq=%d[%sGHz] %s flag=0%X\n",
4948 ch->channel, geo_ch->center_freq,
4949 is_channel_a_band(ch) ? "5.2" : "2.4",
4950 geo_ch->flags & IEEE80211_CHAN_DISABLED ?
4951 "restricted" : "valid",
4952 geo_ch->flags);
Zhu Yib481de92007-09-25 17:54:57 -07004953 }
4954
Tomas Winkler82b9a122008-03-04 18:09:30 -08004955 if ((priv->bands[IEEE80211_BAND_5GHZ].n_channels == 0) &&
4956 priv->cfg->sku & IWL_SKU_A) {
Zhu Yib481de92007-09-25 17:54:57 -07004957 printk(KERN_INFO DRV_NAME
4958 ": Incorrectly detected BG card as ABG. Please send "
4959 "your PCI ID 0x%04X:0x%04X to maintainer.\n",
4960 priv->pci_dev->device, priv->pci_dev->subsystem_device);
Tomas Winkler82b9a122008-03-04 18:09:30 -08004961 priv->cfg->sku &= ~IWL_SKU_A;
Zhu Yib481de92007-09-25 17:54:57 -07004962 }
4963
4964 printk(KERN_INFO DRV_NAME
4965 ": Tunable channels: %d 802.11bg, %d 802.11a channels\n",
Johannes Berg8318d782008-01-24 19:38:38 +01004966 priv->bands[IEEE80211_BAND_2GHZ].n_channels,
4967 priv->bands[IEEE80211_BAND_5GHZ].n_channels);
Zhu Yib481de92007-09-25 17:54:57 -07004968
John W. Linvillee0e0a672008-03-25 15:58:40 -04004969 if (priv->bands[IEEE80211_BAND_2GHZ].n_channels)
4970 priv->hw->wiphy->bands[IEEE80211_BAND_2GHZ] =
4971 &priv->bands[IEEE80211_BAND_2GHZ];
4972 if (priv->bands[IEEE80211_BAND_5GHZ].n_channels)
4973 priv->hw->wiphy->bands[IEEE80211_BAND_5GHZ] =
4974 &priv->bands[IEEE80211_BAND_5GHZ];
Zhu Yib481de92007-09-25 17:54:57 -07004975
Zhu Yib481de92007-09-25 17:54:57 -07004976 set_bit(STATUS_GEO_CONFIGURED, &priv->status);
4977
4978 return 0;
4979}
4980
Reinette Chatre849e0dc2008-01-23 10:15:18 -08004981/*
4982 * iwl3945_free_geos - undo allocations in iwl3945_init_geos
4983 */
4984static void iwl3945_free_geos(struct iwl3945_priv *priv)
4985{
Reinette Chatre849e0dc2008-01-23 10:15:18 -08004986 kfree(priv->ieee_channels);
4987 kfree(priv->ieee_rates);
4988 clear_bit(STATUS_GEO_CONFIGURED, &priv->status);
4989}
4990
Zhu Yib481de92007-09-25 17:54:57 -07004991/******************************************************************************
4992 *
4993 * uCode download functions
4994 *
4995 ******************************************************************************/
4996
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004997static void iwl3945_dealloc_ucode_pci(struct iwl3945_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004998{
Tomas Winkler98c92212008-01-14 17:46:20 -08004999 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_code);
5000 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_data);
5001 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_data_backup);
5002 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_init);
5003 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_init_data);
5004 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_boot);
Zhu Yib481de92007-09-25 17:54:57 -07005005}
5006
5007/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005008 * iwl3945_verify_inst_full - verify runtime uCode image in card vs. host,
Zhu Yib481de92007-09-25 17:54:57 -07005009 * looking at all data.
5010 */
Tomas Winkler3ac7f142008-07-21 02:40:14 +03005011static int iwl3945_verify_inst_full(struct iwl3945_priv *priv, __le32 *image, u32 len)
Zhu Yib481de92007-09-25 17:54:57 -07005012{
5013 u32 val;
5014 u32 save_len = len;
5015 int rc = 0;
5016 u32 errcnt;
5017
5018 IWL_DEBUG_INFO("ucode inst image size is %u\n", len);
5019
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005020 rc = iwl3945_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005021 if (rc)
5022 return rc;
5023
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005024 iwl3945_write_direct32(priv, HBUS_TARG_MEM_RADDR, RTC_INST_LOWER_BOUND);
Zhu Yib481de92007-09-25 17:54:57 -07005025
5026 errcnt = 0;
5027 for (; len > 0; len -= sizeof(u32), image++) {
5028 /* read data comes through single port, auto-incr addr */
5029 /* NOTE: Use the debugless read so we don't flood kernel log
5030 * if IWL_DL_IO is set */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005031 val = _iwl3945_read_direct32(priv, HBUS_TARG_MEM_RDAT);
Zhu Yib481de92007-09-25 17:54:57 -07005032 if (val != le32_to_cpu(*image)) {
5033 IWL_ERROR("uCode INST section is invalid at "
5034 "offset 0x%x, is 0x%x, s/b 0x%x\n",
5035 save_len - len, val, le32_to_cpu(*image));
5036 rc = -EIO;
5037 errcnt++;
5038 if (errcnt >= 20)
5039 break;
5040 }
5041 }
5042
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005043 iwl3945_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005044
5045 if (!errcnt)
Ian Schrambc434dd2007-10-25 17:15:29 +08005046 IWL_DEBUG_INFO("ucode image in INSTRUCTION memory is good\n");
Zhu Yib481de92007-09-25 17:54:57 -07005047
5048 return rc;
5049}
5050
5051
5052/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005053 * iwl3945_verify_inst_sparse - verify runtime uCode image in card vs. host,
Zhu Yib481de92007-09-25 17:54:57 -07005054 * using sample data 100 bytes apart. If these sample points are good,
5055 * it's a pretty good bet that everything between them is good, too.
5056 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005057static int iwl3945_verify_inst_sparse(struct iwl3945_priv *priv, __le32 *image, u32 len)
Zhu Yib481de92007-09-25 17:54:57 -07005058{
5059 u32 val;
5060 int rc = 0;
5061 u32 errcnt = 0;
5062 u32 i;
5063
5064 IWL_DEBUG_INFO("ucode inst image size is %u\n", len);
5065
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005066 rc = iwl3945_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005067 if (rc)
5068 return rc;
5069
5070 for (i = 0; i < len; i += 100, image += 100/sizeof(u32)) {
5071 /* read data comes through single port, auto-incr addr */
5072 /* NOTE: Use the debugless read so we don't flood kernel log
5073 * if IWL_DL_IO is set */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005074 iwl3945_write_direct32(priv, HBUS_TARG_MEM_RADDR,
Zhu Yib481de92007-09-25 17:54:57 -07005075 i + RTC_INST_LOWER_BOUND);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005076 val = _iwl3945_read_direct32(priv, HBUS_TARG_MEM_RDAT);
Zhu Yib481de92007-09-25 17:54:57 -07005077 if (val != le32_to_cpu(*image)) {
5078#if 0 /* Enable this if you want to see details */
5079 IWL_ERROR("uCode INST section is invalid at "
5080 "offset 0x%x, is 0x%x, s/b 0x%x\n",
5081 i, val, *image);
5082#endif
5083 rc = -EIO;
5084 errcnt++;
5085 if (errcnt >= 3)
5086 break;
5087 }
5088 }
5089
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005090 iwl3945_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005091
5092 return rc;
5093}
5094
5095
5096/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005097 * iwl3945_verify_ucode - determine which instruction image is in SRAM,
Zhu Yib481de92007-09-25 17:54:57 -07005098 * and verify its contents
5099 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005100static int iwl3945_verify_ucode(struct iwl3945_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005101{
5102 __le32 *image;
5103 u32 len;
5104 int rc = 0;
5105
5106 /* Try bootstrap */
5107 image = (__le32 *)priv->ucode_boot.v_addr;
5108 len = priv->ucode_boot.len;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005109 rc = iwl3945_verify_inst_sparse(priv, image, len);
Zhu Yib481de92007-09-25 17:54:57 -07005110 if (rc == 0) {
5111 IWL_DEBUG_INFO("Bootstrap uCode is good in inst SRAM\n");
5112 return 0;
5113 }
5114
5115 /* Try initialize */
5116 image = (__le32 *)priv->ucode_init.v_addr;
5117 len = priv->ucode_init.len;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005118 rc = iwl3945_verify_inst_sparse(priv, image, len);
Zhu Yib481de92007-09-25 17:54:57 -07005119 if (rc == 0) {
5120 IWL_DEBUG_INFO("Initialize uCode is good in inst SRAM\n");
5121 return 0;
5122 }
5123
5124 /* Try runtime/protocol */
5125 image = (__le32 *)priv->ucode_code.v_addr;
5126 len = priv->ucode_code.len;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005127 rc = iwl3945_verify_inst_sparse(priv, image, len);
Zhu Yib481de92007-09-25 17:54:57 -07005128 if (rc == 0) {
5129 IWL_DEBUG_INFO("Runtime uCode is good in inst SRAM\n");
5130 return 0;
5131 }
5132
5133 IWL_ERROR("NO VALID UCODE IMAGE IN INSTRUCTION SRAM!!\n");
5134
Ben Cahill9fbab512007-11-29 11:09:47 +08005135 /* Since nothing seems to match, show first several data entries in
5136 * instruction SRAM, so maybe visual inspection will give a clue.
5137 * Selection of bootstrap image (vs. other images) is arbitrary. */
Zhu Yib481de92007-09-25 17:54:57 -07005138 image = (__le32 *)priv->ucode_boot.v_addr;
5139 len = priv->ucode_boot.len;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005140 rc = iwl3945_verify_inst_full(priv, image, len);
Zhu Yib481de92007-09-25 17:54:57 -07005141
5142 return rc;
5143}
5144
5145
5146/* check contents of special bootstrap uCode SRAM */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005147static int iwl3945_verify_bsm(struct iwl3945_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005148{
5149 __le32 *image = priv->ucode_boot.v_addr;
5150 u32 len = priv->ucode_boot.len;
5151 u32 reg;
5152 u32 val;
5153
5154 IWL_DEBUG_INFO("Begin verify bsm\n");
5155
5156 /* verify BSM SRAM contents */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005157 val = iwl3945_read_prph(priv, BSM_WR_DWCOUNT_REG);
Zhu Yib481de92007-09-25 17:54:57 -07005158 for (reg = BSM_SRAM_LOWER_BOUND;
5159 reg < BSM_SRAM_LOWER_BOUND + len;
Tomas Winkler3ac7f142008-07-21 02:40:14 +03005160 reg += sizeof(u32), image++) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005161 val = iwl3945_read_prph(priv, reg);
Zhu Yib481de92007-09-25 17:54:57 -07005162 if (val != le32_to_cpu(*image)) {
5163 IWL_ERROR("BSM uCode verification failed at "
5164 "addr 0x%08X+%u (of %u), is 0x%x, s/b 0x%x\n",
5165 BSM_SRAM_LOWER_BOUND,
5166 reg - BSM_SRAM_LOWER_BOUND, len,
5167 val, le32_to_cpu(*image));
5168 return -EIO;
5169 }
5170 }
5171
5172 IWL_DEBUG_INFO("BSM bootstrap uCode image OK\n");
5173
5174 return 0;
5175}
5176
5177/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005178 * iwl3945_load_bsm - Load bootstrap instructions
Zhu Yib481de92007-09-25 17:54:57 -07005179 *
5180 * BSM operation:
5181 *
5182 * The Bootstrap State Machine (BSM) stores a short bootstrap uCode program
5183 * in special SRAM that does not power down during RFKILL. When powering back
5184 * up after power-saving sleeps (or during initial uCode load), the BSM loads
5185 * the bootstrap program into the on-board processor, and starts it.
5186 *
5187 * The bootstrap program loads (via DMA) instructions and data for a new
5188 * program from host DRAM locations indicated by the host driver in the
5189 * BSM_DRAM_* registers. Once the new program is loaded, it starts
5190 * automatically.
5191 *
5192 * When initializing the NIC, the host driver points the BSM to the
5193 * "initialize" uCode image. This uCode sets up some internal data, then
5194 * notifies host via "initialize alive" that it is complete.
5195 *
5196 * The host then replaces the BSM_DRAM_* pointer values to point to the
5197 * normal runtime uCode instructions and a backup uCode data cache buffer
5198 * (filled initially with starting data values for the on-board processor),
5199 * then triggers the "initialize" uCode to load and launch the runtime uCode,
5200 * which begins normal operation.
5201 *
5202 * When doing a power-save shutdown, runtime uCode saves data SRAM into
5203 * the backup data cache in DRAM before SRAM is powered down.
5204 *
5205 * When powering back up, the BSM loads the bootstrap program. This reloads
5206 * the runtime uCode instructions and the backup data cache into SRAM,
5207 * and re-launches the runtime uCode from where it left off.
5208 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005209static int iwl3945_load_bsm(struct iwl3945_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005210{
5211 __le32 *image = priv->ucode_boot.v_addr;
5212 u32 len = priv->ucode_boot.len;
5213 dma_addr_t pinst;
5214 dma_addr_t pdata;
5215 u32 inst_len;
5216 u32 data_len;
5217 int rc;
5218 int i;
5219 u32 done;
5220 u32 reg_offset;
5221
5222 IWL_DEBUG_INFO("Begin load bsm\n");
5223
5224 /* make sure bootstrap program is no larger than BSM's SRAM size */
5225 if (len > IWL_MAX_BSM_SIZE)
5226 return -EINVAL;
5227
5228 /* Tell bootstrap uCode where to find the "Initialize" uCode
Ben Cahill9fbab512007-11-29 11:09:47 +08005229 * in host DRAM ... host DRAM physical address bits 31:0 for 3945.
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005230 * NOTE: iwl3945_initialize_alive_start() will replace these values,
Zhu Yib481de92007-09-25 17:54:57 -07005231 * after the "initialize" uCode has run, to point to
5232 * runtime/protocol instructions and backup data cache. */
5233 pinst = priv->ucode_init.p_addr;
5234 pdata = priv->ucode_init_data.p_addr;
5235 inst_len = priv->ucode_init.len;
5236 data_len = priv->ucode_init_data.len;
5237
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005238 rc = iwl3945_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005239 if (rc)
5240 return rc;
5241
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005242 iwl3945_write_prph(priv, BSM_DRAM_INST_PTR_REG, pinst);
5243 iwl3945_write_prph(priv, BSM_DRAM_DATA_PTR_REG, pdata);
5244 iwl3945_write_prph(priv, BSM_DRAM_INST_BYTECOUNT_REG, inst_len);
5245 iwl3945_write_prph(priv, BSM_DRAM_DATA_BYTECOUNT_REG, data_len);
Zhu Yib481de92007-09-25 17:54:57 -07005246
5247 /* Fill BSM memory with bootstrap instructions */
5248 for (reg_offset = BSM_SRAM_LOWER_BOUND;
5249 reg_offset < BSM_SRAM_LOWER_BOUND + len;
5250 reg_offset += sizeof(u32), image++)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005251 _iwl3945_write_prph(priv, reg_offset,
Zhu Yib481de92007-09-25 17:54:57 -07005252 le32_to_cpu(*image));
5253
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005254 rc = iwl3945_verify_bsm(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005255 if (rc) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005256 iwl3945_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005257 return rc;
5258 }
5259
5260 /* Tell BSM to copy from BSM SRAM into instruction SRAM, when asked */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005261 iwl3945_write_prph(priv, BSM_WR_MEM_SRC_REG, 0x0);
5262 iwl3945_write_prph(priv, BSM_WR_MEM_DST_REG,
Zhu Yib481de92007-09-25 17:54:57 -07005263 RTC_INST_LOWER_BOUND);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005264 iwl3945_write_prph(priv, BSM_WR_DWCOUNT_REG, len / sizeof(u32));
Zhu Yib481de92007-09-25 17:54:57 -07005265
5266 /* Load bootstrap code into instruction SRAM now,
5267 * to prepare to load "initialize" uCode */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005268 iwl3945_write_prph(priv, BSM_WR_CTRL_REG,
Zhu Yib481de92007-09-25 17:54:57 -07005269 BSM_WR_CTRL_REG_BIT_START);
5270
5271 /* Wait for load of bootstrap uCode to finish */
5272 for (i = 0; i < 100; i++) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005273 done = iwl3945_read_prph(priv, BSM_WR_CTRL_REG);
Zhu Yib481de92007-09-25 17:54:57 -07005274 if (!(done & BSM_WR_CTRL_REG_BIT_START))
5275 break;
5276 udelay(10);
5277 }
5278 if (i < 100)
5279 IWL_DEBUG_INFO("BSM write complete, poll %d iterations\n", i);
5280 else {
5281 IWL_ERROR("BSM write did not complete!\n");
5282 return -EIO;
5283 }
5284
5285 /* Enable future boot loads whenever power management unit triggers it
5286 * (e.g. when powering back up after power-save shutdown) */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005287 iwl3945_write_prph(priv, BSM_WR_CTRL_REG,
Zhu Yib481de92007-09-25 17:54:57 -07005288 BSM_WR_CTRL_REG_BIT_START_EN);
5289
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005290 iwl3945_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005291
5292 return 0;
5293}
5294
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005295static void iwl3945_nic_start(struct iwl3945_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005296{
5297 /* Remove all resets to allow NIC to operate */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005298 iwl3945_write32(priv, CSR_RESET, 0);
Zhu Yib481de92007-09-25 17:54:57 -07005299}
5300
5301/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005302 * iwl3945_read_ucode - Read uCode images from disk file.
Zhu Yib481de92007-09-25 17:54:57 -07005303 *
5304 * Copy into buffers for card to fetch via bus-mastering
5305 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005306static int iwl3945_read_ucode(struct iwl3945_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005307{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005308 struct iwl3945_ucode *ucode;
Reinette Chatrea0987a82008-12-02 12:14:06 -08005309 int ret = -EINVAL, index;
Zhu Yib481de92007-09-25 17:54:57 -07005310 const struct firmware *ucode_raw;
5311 /* firmware file name contains uCode/driver compatibility version */
Reinette Chatrea0987a82008-12-02 12:14:06 -08005312 const char *name_pre = priv->cfg->fw_name_pre;
5313 const unsigned int api_max = priv->cfg->ucode_api_max;
5314 const unsigned int api_min = priv->cfg->ucode_api_min;
5315 char buf[25];
Zhu Yib481de92007-09-25 17:54:57 -07005316 u8 *src;
5317 size_t len;
Reinette Chatrea0987a82008-12-02 12:14:06 -08005318 u32 api_ver, inst_size, data_size, init_size, init_data_size, boot_size;
Zhu Yib481de92007-09-25 17:54:57 -07005319
5320 /* Ask kernel firmware_class module to get the boot firmware off disk.
5321 * request_firmware() is synchronous, file is in memory on return. */
Reinette Chatrea0987a82008-12-02 12:14:06 -08005322 for (index = api_max; index >= api_min; index--) {
5323 sprintf(buf, "%s%u%s", name_pre, index, ".ucode");
5324 ret = request_firmware(&ucode_raw, buf, &priv->pci_dev->dev);
5325 if (ret < 0) {
5326 IWL_ERROR("%s firmware file req failed: Reason %d\n",
5327 buf, ret);
5328 if (ret == -ENOENT)
5329 continue;
5330 else
5331 goto error;
5332 } else {
5333 if (index < api_max)
5334 IWL_ERROR("Loaded firmware %s, which is deprecated. Please use API v%u instead.\n",
5335 buf, api_max);
5336 IWL_DEBUG_INFO("Got firmware '%s' file (%zd bytes) from disk\n",
5337 buf, ucode_raw->size);
5338 break;
5339 }
Zhu Yib481de92007-09-25 17:54:57 -07005340 }
5341
Reinette Chatrea0987a82008-12-02 12:14:06 -08005342 if (ret < 0)
5343 goto error;
Zhu Yib481de92007-09-25 17:54:57 -07005344
5345 /* Make sure that we got at least our header! */
5346 if (ucode_raw->size < sizeof(*ucode)) {
5347 IWL_ERROR("File size way too small!\n");
Tomas Winkler90e759d2007-11-29 11:09:41 +08005348 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07005349 goto err_release;
5350 }
5351
5352 /* Data from ucode file: header followed by uCode images */
5353 ucode = (void *)ucode_raw->data;
5354
Chatre, Reinettec02b3ac2008-12-02 12:14:05 -08005355 priv->ucode_ver = le32_to_cpu(ucode->ver);
Reinette Chatrea0987a82008-12-02 12:14:06 -08005356 api_ver = IWL_UCODE_API(priv->ucode_ver);
Zhu Yib481de92007-09-25 17:54:57 -07005357 inst_size = le32_to_cpu(ucode->inst_size);
5358 data_size = le32_to_cpu(ucode->data_size);
5359 init_size = le32_to_cpu(ucode->init_size);
5360 init_data_size = le32_to_cpu(ucode->init_data_size);
5361 boot_size = le32_to_cpu(ucode->boot_size);
5362
Reinette Chatrea0987a82008-12-02 12:14:06 -08005363 /* api_ver should match the api version forming part of the
5364 * firmware filename ... but we don't check for that and only rely
5365 * on the API version read from firware header from here on forward */
5366
5367 if (api_ver < api_min || api_ver > api_max) {
5368 IWL_ERROR("Driver unable to support your firmware API. "
5369 "Driver supports v%u, firmware is v%u.\n",
5370 api_max, api_ver);
5371 priv->ucode_ver = 0;
5372 ret = -EINVAL;
5373 goto err_release;
5374 }
5375 if (api_ver != api_max)
5376 IWL_ERROR("Firmware has old API version. Expected %u, "
5377 "got %u. New firmware can be obtained "
5378 "from http://www.intellinuxwireless.org.\n",
5379 api_max, api_ver);
5380
5381 printk(KERN_INFO DRV_NAME " loaded firmware version %u.%u.%u.%u\n",
Chatre, Reinettec02b3ac2008-12-02 12:14:05 -08005382 IWL_UCODE_MAJOR(priv->ucode_ver),
5383 IWL_UCODE_MINOR(priv->ucode_ver),
5384 IWL_UCODE_API(priv->ucode_ver),
5385 IWL_UCODE_SERIAL(priv->ucode_ver));
Reinette Chatrea0987a82008-12-02 12:14:06 -08005386 IWL_DEBUG_INFO("f/w package hdr ucode version raw = 0x%x\n",
5387 priv->ucode_ver);
Ian Schrambc434dd2007-10-25 17:15:29 +08005388 IWL_DEBUG_INFO("f/w package hdr runtime inst size = %u\n", inst_size);
5389 IWL_DEBUG_INFO("f/w package hdr runtime data size = %u\n", data_size);
5390 IWL_DEBUG_INFO("f/w package hdr init inst size = %u\n", init_size);
5391 IWL_DEBUG_INFO("f/w package hdr init data size = %u\n", init_data_size);
5392 IWL_DEBUG_INFO("f/w package hdr boot inst size = %u\n", boot_size);
Zhu Yib481de92007-09-25 17:54:57 -07005393
Reinette Chatrea0987a82008-12-02 12:14:06 -08005394
Zhu Yib481de92007-09-25 17:54:57 -07005395 /* Verify size of file vs. image size info in file's header */
5396 if (ucode_raw->size < sizeof(*ucode) +
5397 inst_size + data_size + init_size +
5398 init_data_size + boot_size) {
5399
5400 IWL_DEBUG_INFO("uCode file size %d too small\n",
5401 (int)ucode_raw->size);
Tomas Winkler90e759d2007-11-29 11:09:41 +08005402 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07005403 goto err_release;
5404 }
5405
5406 /* Verify that uCode images will fit in card's SRAM */
5407 if (inst_size > IWL_MAX_INST_SIZE) {
Tomas Winkler90e759d2007-11-29 11:09:41 +08005408 IWL_DEBUG_INFO("uCode instr len %d too large to fit in\n",
5409 inst_size);
5410 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07005411 goto err_release;
5412 }
5413
5414 if (data_size > IWL_MAX_DATA_SIZE) {
Tomas Winkler90e759d2007-11-29 11:09:41 +08005415 IWL_DEBUG_INFO("uCode data len %d too large to fit in\n",
5416 data_size);
5417 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07005418 goto err_release;
5419 }
5420 if (init_size > IWL_MAX_INST_SIZE) {
Tomas Winkler90e759d2007-11-29 11:09:41 +08005421 IWL_DEBUG_INFO("uCode init instr len %d too large to fit in\n",
5422 init_size);
5423 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07005424 goto err_release;
5425 }
5426 if (init_data_size > IWL_MAX_DATA_SIZE) {
Tomas Winkler90e759d2007-11-29 11:09:41 +08005427 IWL_DEBUG_INFO("uCode init data len %d too large to fit in\n",
5428 init_data_size);
5429 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07005430 goto err_release;
5431 }
5432 if (boot_size > IWL_MAX_BSM_SIZE) {
Tomas Winkler90e759d2007-11-29 11:09:41 +08005433 IWL_DEBUG_INFO("uCode boot instr len %d too large to fit in\n",
5434 boot_size);
5435 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07005436 goto err_release;
5437 }
5438
5439 /* Allocate ucode buffers for card's bus-master loading ... */
5440
5441 /* Runtime instructions and 2 copies of data:
5442 * 1) unmodified from disk
5443 * 2) backup cache for save/restore during power-downs */
5444 priv->ucode_code.len = inst_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08005445 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_code);
Zhu Yib481de92007-09-25 17:54:57 -07005446
5447 priv->ucode_data.len = data_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08005448 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_data);
Zhu Yib481de92007-09-25 17:54:57 -07005449
5450 priv->ucode_data_backup.len = data_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08005451 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_data_backup);
Zhu Yib481de92007-09-25 17:54:57 -07005452
5453 if (!priv->ucode_code.v_addr || !priv->ucode_data.v_addr ||
Tomas Winkler90e759d2007-11-29 11:09:41 +08005454 !priv->ucode_data_backup.v_addr)
Zhu Yib481de92007-09-25 17:54:57 -07005455 goto err_pci_alloc;
5456
Tomas Winkler90e759d2007-11-29 11:09:41 +08005457 /* Initialization instructions and data */
5458 if (init_size && init_data_size) {
5459 priv->ucode_init.len = init_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08005460 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_init);
Tomas Winkler90e759d2007-11-29 11:09:41 +08005461
5462 priv->ucode_init_data.len = init_data_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08005463 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_init_data);
Tomas Winkler90e759d2007-11-29 11:09:41 +08005464
5465 if (!priv->ucode_init.v_addr || !priv->ucode_init_data.v_addr)
5466 goto err_pci_alloc;
5467 }
5468
5469 /* Bootstrap (instructions only, no data) */
5470 if (boot_size) {
5471 priv->ucode_boot.len = boot_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08005472 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_boot);
Tomas Winkler90e759d2007-11-29 11:09:41 +08005473
5474 if (!priv->ucode_boot.v_addr)
5475 goto err_pci_alloc;
5476 }
5477
Zhu Yib481de92007-09-25 17:54:57 -07005478 /* Copy images into buffers for card's bus-master reads ... */
5479
5480 /* Runtime instructions (first block of data in file) */
5481 src = &ucode->data[0];
5482 len = priv->ucode_code.len;
Tomas Winkler90e759d2007-11-29 11:09:41 +08005483 IWL_DEBUG_INFO("Copying (but not loading) uCode instr len %Zd\n", len);
Zhu Yib481de92007-09-25 17:54:57 -07005484 memcpy(priv->ucode_code.v_addr, src, len);
5485 IWL_DEBUG_INFO("uCode instr buf vaddr = 0x%p, paddr = 0x%08x\n",
5486 priv->ucode_code.v_addr, (u32)priv->ucode_code.p_addr);
5487
5488 /* Runtime data (2nd block)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005489 * NOTE: Copy into backup buffer will be done in iwl3945_up() */
Zhu Yib481de92007-09-25 17:54:57 -07005490 src = &ucode->data[inst_size];
5491 len = priv->ucode_data.len;
Tomas Winkler90e759d2007-11-29 11:09:41 +08005492 IWL_DEBUG_INFO("Copying (but not loading) uCode data len %Zd\n", len);
Zhu Yib481de92007-09-25 17:54:57 -07005493 memcpy(priv->ucode_data.v_addr, src, len);
5494 memcpy(priv->ucode_data_backup.v_addr, src, len);
5495
5496 /* Initialization instructions (3rd block) */
5497 if (init_size) {
5498 src = &ucode->data[inst_size + data_size];
5499 len = priv->ucode_init.len;
Tomas Winkler90e759d2007-11-29 11:09:41 +08005500 IWL_DEBUG_INFO("Copying (but not loading) init instr len %Zd\n",
5501 len);
Zhu Yib481de92007-09-25 17:54:57 -07005502 memcpy(priv->ucode_init.v_addr, src, len);
5503 }
5504
5505 /* Initialization data (4th block) */
5506 if (init_data_size) {
5507 src = &ucode->data[inst_size + data_size + init_size];
5508 len = priv->ucode_init_data.len;
5509 IWL_DEBUG_INFO("Copying (but not loading) init data len %d\n",
5510 (int)len);
5511 memcpy(priv->ucode_init_data.v_addr, src, len);
5512 }
5513
5514 /* Bootstrap instructions (5th block) */
5515 src = &ucode->data[inst_size + data_size + init_size + init_data_size];
5516 len = priv->ucode_boot.len;
5517 IWL_DEBUG_INFO("Copying (but not loading) boot instr len %d\n",
5518 (int)len);
5519 memcpy(priv->ucode_boot.v_addr, src, len);
5520
5521 /* We have our copies now, allow OS release its copies */
5522 release_firmware(ucode_raw);
5523 return 0;
5524
5525 err_pci_alloc:
5526 IWL_ERROR("failed to allocate pci memory\n");
Tomas Winkler90e759d2007-11-29 11:09:41 +08005527 ret = -ENOMEM;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005528 iwl3945_dealloc_ucode_pci(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005529
5530 err_release:
5531 release_firmware(ucode_raw);
5532
5533 error:
Tomas Winkler90e759d2007-11-29 11:09:41 +08005534 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07005535}
5536
5537
5538/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005539 * iwl3945_set_ucode_ptrs - Set uCode address location
Zhu Yib481de92007-09-25 17:54:57 -07005540 *
5541 * Tell initialization uCode where to find runtime uCode.
5542 *
5543 * BSM registers initially contain pointers to initialization uCode.
5544 * We need to replace them to load runtime uCode inst and data,
5545 * and to save runtime data when powering down.
5546 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005547static int iwl3945_set_ucode_ptrs(struct iwl3945_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005548{
5549 dma_addr_t pinst;
5550 dma_addr_t pdata;
5551 int rc = 0;
5552 unsigned long flags;
5553
5554 /* bits 31:0 for 3945 */
5555 pinst = priv->ucode_code.p_addr;
5556 pdata = priv->ucode_data_backup.p_addr;
5557
5558 spin_lock_irqsave(&priv->lock, flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005559 rc = iwl3945_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005560 if (rc) {
5561 spin_unlock_irqrestore(&priv->lock, flags);
5562 return rc;
5563 }
5564
5565 /* Tell bootstrap uCode where to find image to load */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005566 iwl3945_write_prph(priv, BSM_DRAM_INST_PTR_REG, pinst);
5567 iwl3945_write_prph(priv, BSM_DRAM_DATA_PTR_REG, pdata);
5568 iwl3945_write_prph(priv, BSM_DRAM_DATA_BYTECOUNT_REG,
Zhu Yib481de92007-09-25 17:54:57 -07005569 priv->ucode_data.len);
5570
Tomas Winklera96a27f2008-10-23 23:48:56 -07005571 /* Inst byte count must be last to set up, bit 31 signals uCode
Zhu Yib481de92007-09-25 17:54:57 -07005572 * that all new ptr/size info is in place */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005573 iwl3945_write_prph(priv, BSM_DRAM_INST_BYTECOUNT_REG,
Zhu Yib481de92007-09-25 17:54:57 -07005574 priv->ucode_code.len | BSM_DRAM_INST_LOAD);
5575
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005576 iwl3945_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005577
5578 spin_unlock_irqrestore(&priv->lock, flags);
5579
5580 IWL_DEBUG_INFO("Runtime uCode pointers are set.\n");
5581
5582 return rc;
5583}
5584
5585/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005586 * iwl3945_init_alive_start - Called after REPLY_ALIVE notification received
Zhu Yib481de92007-09-25 17:54:57 -07005587 *
5588 * Called after REPLY_ALIVE notification received from "initialize" uCode.
5589 *
Zhu Yib481de92007-09-25 17:54:57 -07005590 * Tell "initialize" uCode to go ahead and load the runtime uCode.
Ben Cahill9fbab512007-11-29 11:09:47 +08005591 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005592static void iwl3945_init_alive_start(struct iwl3945_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005593{
5594 /* Check alive response for "valid" sign from uCode */
5595 if (priv->card_alive_init.is_valid != UCODE_VALID_OK) {
5596 /* We had an error bringing up the hardware, so take it
5597 * all the way back down so we can try again */
5598 IWL_DEBUG_INFO("Initialize Alive failed.\n");
5599 goto restart;
5600 }
5601
5602 /* Bootstrap uCode has loaded initialize uCode ... verify inst image.
5603 * This is a paranoid check, because we would not have gotten the
5604 * "initialize" alive if code weren't properly loaded. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005605 if (iwl3945_verify_ucode(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07005606 /* Runtime instruction load was bad;
5607 * take it all the way back down so we can try again */
5608 IWL_DEBUG_INFO("Bad \"initialize\" uCode load.\n");
5609 goto restart;
5610 }
5611
5612 /* Send pointers to protocol/runtime uCode image ... init code will
5613 * load and launch runtime uCode, which will send us another "Alive"
5614 * notification. */
5615 IWL_DEBUG_INFO("Initialization Alive received.\n");
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005616 if (iwl3945_set_ucode_ptrs(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07005617 /* Runtime instruction load won't happen;
5618 * take it all the way back down so we can try again */
5619 IWL_DEBUG_INFO("Couldn't set up uCode pointers.\n");
5620 goto restart;
5621 }
5622 return;
5623
5624 restart:
5625 queue_work(priv->workqueue, &priv->restart);
5626}
5627
5628
Mohamed Abbas9bdf5ec2008-11-07 09:58:35 -08005629/* temporary */
5630static int iwl3945_mac_beacon_update(struct ieee80211_hw *hw,
5631 struct sk_buff *skb);
5632
Zhu Yib481de92007-09-25 17:54:57 -07005633/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005634 * iwl3945_alive_start - called after REPLY_ALIVE notification received
Zhu Yib481de92007-09-25 17:54:57 -07005635 * from protocol/runtime uCode (initialization uCode's
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005636 * Alive gets handled by iwl3945_init_alive_start()).
Zhu Yib481de92007-09-25 17:54:57 -07005637 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005638static void iwl3945_alive_start(struct iwl3945_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005639{
5640 int rc = 0;
5641 int thermal_spin = 0;
5642 u32 rfkill;
5643
5644 IWL_DEBUG_INFO("Runtime Alive received.\n");
5645
5646 if (priv->card_alive.is_valid != UCODE_VALID_OK) {
5647 /* We had an error bringing up the hardware, so take it
5648 * all the way back down so we can try again */
5649 IWL_DEBUG_INFO("Alive failed.\n");
5650 goto restart;
5651 }
5652
5653 /* Initialize uCode has loaded Runtime uCode ... verify inst image.
5654 * This is a paranoid check, because we would not have gotten the
5655 * "runtime" alive if code weren't properly loaded. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005656 if (iwl3945_verify_ucode(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07005657 /* Runtime instruction load was bad;
5658 * take it all the way back down so we can try again */
5659 IWL_DEBUG_INFO("Bad runtime uCode load.\n");
5660 goto restart;
5661 }
5662
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005663 iwl3945_clear_stations_table(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005664
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005665 rc = iwl3945_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005666 if (rc) {
Tomas Winklera96a27f2008-10-23 23:48:56 -07005667 IWL_WARNING("Can not read RFKILL status from adapter\n");
Zhu Yib481de92007-09-25 17:54:57 -07005668 return;
5669 }
5670
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005671 rfkill = iwl3945_read_prph(priv, APMG_RFKILL_REG);
Zhu Yib481de92007-09-25 17:54:57 -07005672 IWL_DEBUG_INFO("RFKILL status: 0x%x\n", rfkill);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005673 iwl3945_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005674
5675 if (rfkill & 0x1) {
5676 clear_bit(STATUS_RF_KILL_HW, &priv->status);
Tomas Winklera96a27f2008-10-23 23:48:56 -07005677 /* if RFKILL is not on, then wait for thermal
Zhu Yib481de92007-09-25 17:54:57 -07005678 * sensor in adapter to kick in */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005679 while (iwl3945_hw_get_temperature(priv) == 0) {
Zhu Yib481de92007-09-25 17:54:57 -07005680 thermal_spin++;
5681 udelay(10);
5682 }
5683
5684 if (thermal_spin)
5685 IWL_DEBUG_INFO("Thermal calibration took %dus\n",
5686 thermal_spin * 10);
5687 } else
5688 set_bit(STATUS_RF_KILL_HW, &priv->status);
5689
Ben Cahill9fbab512007-11-29 11:09:47 +08005690 /* After the ALIVE response, we can send commands to 3945 uCode */
Zhu Yib481de92007-09-25 17:54:57 -07005691 set_bit(STATUS_ALIVE, &priv->status);
5692
5693 /* Clear out the uCode error bit if it is set */
5694 clear_bit(STATUS_FW_ERROR, &priv->status);
5695
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005696 if (iwl3945_is_rfkill(priv))
Zhu Yib481de92007-09-25 17:54:57 -07005697 return;
5698
Johannes Berg36d68252008-05-15 12:55:26 +02005699 ieee80211_wake_queues(priv->hw);
Zhu Yib481de92007-09-25 17:54:57 -07005700
5701 priv->active_rate = priv->rates_mask;
5702 priv->active_rate_basic = priv->rates_mask & IWL_BASIC_RATES_MASK;
5703
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005704 iwl3945_send_power_mode(priv, IWL_POWER_LEVEL(priv->power_mode));
Zhu Yib481de92007-09-25 17:54:57 -07005705
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005706 if (iwl3945_is_associated(priv)) {
5707 struct iwl3945_rxon_cmd *active_rxon =
5708 (struct iwl3945_rxon_cmd *)(&priv->active_rxon);
Zhu Yib481de92007-09-25 17:54:57 -07005709
5710 memcpy(&priv->staging_rxon, &priv->active_rxon,
5711 sizeof(priv->staging_rxon));
5712 active_rxon->filter_flags &= ~RXON_FILTER_ASSOC_MSK;
5713 } else {
5714 /* Initialize our rx_config data */
Zhu, Yi60294de2008-10-29 14:05:45 -07005715 iwl3945_connection_init_rx_config(priv, priv->iw_mode);
Zhu Yib481de92007-09-25 17:54:57 -07005716 memcpy(priv->staging_rxon.node_addr, priv->mac_addr, ETH_ALEN);
5717 }
5718
Ben Cahill9fbab512007-11-29 11:09:47 +08005719 /* Configure Bluetooth device coexistence support */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005720 iwl3945_send_bt_config(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005721
5722 /* Configure the adapter for unassociated operation */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005723 iwl3945_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005724
Zhu Yib481de92007-09-25 17:54:57 -07005725 iwl3945_reg_txpower_periodic(priv);
5726
Reinette Chatrefe00b5a2008-04-03 16:05:23 -07005727 iwl3945_led_register(priv);
5728
Zhu Yib481de92007-09-25 17:54:57 -07005729 IWL_DEBUG_INFO("ALIVE processing complete.\n");
Rick Farringtona9f46782008-03-18 14:57:49 -07005730 set_bit(STATUS_READY, &priv->status);
Zhu Yi5a669262008-01-14 17:46:18 -08005731 wake_up_interruptible(&priv->wait_command_queue);
Zhu Yib481de92007-09-25 17:54:57 -07005732
5733 if (priv->error_recovering)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005734 iwl3945_error_recovery(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005735
Mohamed Abbas9bdf5ec2008-11-07 09:58:35 -08005736 /* reassociate for ADHOC mode */
5737 if (priv->vif && (priv->iw_mode == NL80211_IFTYPE_ADHOC)) {
5738 struct sk_buff *beacon = ieee80211_beacon_get(priv->hw,
5739 priv->vif);
5740 if (beacon)
5741 iwl3945_mac_beacon_update(priv->hw, beacon);
5742 }
5743
Zhu Yib481de92007-09-25 17:54:57 -07005744 return;
5745
5746 restart:
5747 queue_work(priv->workqueue, &priv->restart);
5748}
5749
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005750static void iwl3945_cancel_deferred_work(struct iwl3945_priv *priv);
Zhu Yib481de92007-09-25 17:54:57 -07005751
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005752static void __iwl3945_down(struct iwl3945_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005753{
5754 unsigned long flags;
5755 int exit_pending = test_bit(STATUS_EXIT_PENDING, &priv->status);
5756 struct ieee80211_conf *conf = NULL;
5757
5758 IWL_DEBUG_INFO(DRV_NAME " is going down\n");
5759
5760 conf = ieee80211_get_hw_conf(priv->hw);
5761
5762 if (!exit_pending)
5763 set_bit(STATUS_EXIT_PENDING, &priv->status);
5764
Mohamed Abbasab53d8a2008-03-25 16:33:36 -07005765 iwl3945_led_unregister(priv);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005766 iwl3945_clear_stations_table(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005767
5768 /* Unblock any waiting calls */
5769 wake_up_interruptible_all(&priv->wait_command_queue);
5770
Zhu Yib481de92007-09-25 17:54:57 -07005771 /* Wipe out the EXIT_PENDING status bit if we are not actually
5772 * exiting the module */
5773 if (!exit_pending)
5774 clear_bit(STATUS_EXIT_PENDING, &priv->status);
5775
5776 /* stop and reset the on-board processor */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005777 iwl3945_write32(priv, CSR_RESET, CSR_RESET_REG_FLAG_NEVO_RESET);
Zhu Yib481de92007-09-25 17:54:57 -07005778
5779 /* tell the device to stop sending interrupts */
Mohamed Abbas0359fac2008-03-28 16:21:08 -07005780 spin_lock_irqsave(&priv->lock, flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005781 iwl3945_disable_interrupts(priv);
Mohamed Abbas0359fac2008-03-28 16:21:08 -07005782 spin_unlock_irqrestore(&priv->lock, flags);
5783 iwl_synchronize_irq(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005784
5785 if (priv->mac80211_registered)
5786 ieee80211_stop_queues(priv->hw);
5787
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005788 /* If we have not previously called iwl3945_init() then
Zhu Yib481de92007-09-25 17:54:57 -07005789 * clear all bits but the RF Kill and SUSPEND bits and return */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005790 if (!iwl3945_is_init(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07005791 priv->status = test_bit(STATUS_RF_KILL_HW, &priv->status) <<
5792 STATUS_RF_KILL_HW |
5793 test_bit(STATUS_RF_KILL_SW, &priv->status) <<
5794 STATUS_RF_KILL_SW |
Reinette Chatre97888642008-02-06 11:20:38 -08005795 test_bit(STATUS_GEO_CONFIGURED, &priv->status) <<
5796 STATUS_GEO_CONFIGURED |
Zhu Yib481de92007-09-25 17:54:57 -07005797 test_bit(STATUS_IN_SUSPEND, &priv->status) <<
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08005798 STATUS_IN_SUSPEND |
5799 test_bit(STATUS_EXIT_PENDING, &priv->status) <<
5800 STATUS_EXIT_PENDING;
Zhu Yib481de92007-09-25 17:54:57 -07005801 goto exit;
5802 }
5803
5804 /* ...otherwise clear out all the status bits but the RF Kill and
5805 * SUSPEND bits and continue taking the NIC down. */
5806 priv->status &= test_bit(STATUS_RF_KILL_HW, &priv->status) <<
5807 STATUS_RF_KILL_HW |
5808 test_bit(STATUS_RF_KILL_SW, &priv->status) <<
5809 STATUS_RF_KILL_SW |
Reinette Chatre97888642008-02-06 11:20:38 -08005810 test_bit(STATUS_GEO_CONFIGURED, &priv->status) <<
5811 STATUS_GEO_CONFIGURED |
Zhu Yib481de92007-09-25 17:54:57 -07005812 test_bit(STATUS_IN_SUSPEND, &priv->status) <<
5813 STATUS_IN_SUSPEND |
5814 test_bit(STATUS_FW_ERROR, &priv->status) <<
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08005815 STATUS_FW_ERROR |
5816 test_bit(STATUS_EXIT_PENDING, &priv->status) <<
5817 STATUS_EXIT_PENDING;
Zhu Yib481de92007-09-25 17:54:57 -07005818
5819 spin_lock_irqsave(&priv->lock, flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005820 iwl3945_clear_bit(priv, CSR_GP_CNTRL, CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
Zhu Yib481de92007-09-25 17:54:57 -07005821 spin_unlock_irqrestore(&priv->lock, flags);
5822
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005823 iwl3945_hw_txq_ctx_stop(priv);
5824 iwl3945_hw_rxq_stop(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005825
5826 spin_lock_irqsave(&priv->lock, flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005827 if (!iwl3945_grab_nic_access(priv)) {
5828 iwl3945_write_prph(priv, APMG_CLK_DIS_REG,
Zhu Yib481de92007-09-25 17:54:57 -07005829 APMG_CLK_VAL_DMA_CLK_RQT);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005830 iwl3945_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005831 }
5832 spin_unlock_irqrestore(&priv->lock, flags);
5833
5834 udelay(5);
5835
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005836 iwl3945_hw_nic_stop_master(priv);
5837 iwl3945_set_bit(priv, CSR_RESET, CSR_RESET_REG_FLAG_SW_RESET);
5838 iwl3945_hw_nic_reset(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005839
5840 exit:
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005841 memset(&priv->card_alive, 0, sizeof(struct iwl3945_alive_resp));
Zhu Yib481de92007-09-25 17:54:57 -07005842
5843 if (priv->ibss_beacon)
5844 dev_kfree_skb(priv->ibss_beacon);
5845 priv->ibss_beacon = NULL;
5846
5847 /* clear out any free frames */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005848 iwl3945_clear_free_frames(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005849}
5850
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005851static void iwl3945_down(struct iwl3945_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005852{
5853 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005854 __iwl3945_down(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005855 mutex_unlock(&priv->mutex);
Zhu Yib24d22b2007-12-19 13:59:52 +08005856
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005857 iwl3945_cancel_deferred_work(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005858}
5859
5860#define MAX_HW_RESTARTS 5
5861
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005862static int __iwl3945_up(struct iwl3945_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005863{
5864 int rc, i;
5865
5866 if (test_bit(STATUS_EXIT_PENDING, &priv->status)) {
5867 IWL_WARNING("Exit pending; will not bring the NIC up\n");
5868 return -EIO;
5869 }
5870
5871 if (test_bit(STATUS_RF_KILL_SW, &priv->status)) {
5872 IWL_WARNING("Radio disabled by SW RF kill (module "
5873 "parameter)\n");
Zhu Yie655b9f2008-01-24 02:19:38 -08005874 return -ENODEV;
5875 }
5876
Reinette Chatree903fbd2008-01-30 22:05:15 -08005877 if (!priv->ucode_data_backup.v_addr || !priv->ucode_data.v_addr) {
Tomas Winklera96a27f2008-10-23 23:48:56 -07005878 IWL_ERROR("ucode not available for device bring up\n");
Reinette Chatree903fbd2008-01-30 22:05:15 -08005879 return -EIO;
5880 }
5881
Zhu Yie655b9f2008-01-24 02:19:38 -08005882 /* If platform's RF_KILL switch is NOT set to KILL */
5883 if (iwl3945_read32(priv, CSR_GP_CNTRL) &
5884 CSR_GP_CNTRL_REG_FLAG_HW_RF_KILL_SW)
5885 clear_bit(STATUS_RF_KILL_HW, &priv->status);
5886 else {
5887 set_bit(STATUS_RF_KILL_HW, &priv->status);
5888 if (!test_bit(STATUS_IN_SUSPEND, &priv->status)) {
5889 IWL_WARNING("Radio disabled by HW RF Kill switch\n");
5890 return -ENODEV;
5891 }
Zhu Yib481de92007-09-25 17:54:57 -07005892 }
Adel Gadllah80fcc9e2008-07-01 17:49:50 +02005893
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005894 iwl3945_write32(priv, CSR_INT, 0xFFFFFFFF);
Zhu Yib481de92007-09-25 17:54:57 -07005895
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005896 rc = iwl3945_hw_nic_init(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005897 if (rc) {
5898 IWL_ERROR("Unable to int nic\n");
5899 return rc;
5900 }
5901
5902 /* make sure rfkill handshake bits are cleared */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005903 iwl3945_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
5904 iwl3945_write32(priv, CSR_UCODE_DRV_GP1_CLR,
Zhu Yib481de92007-09-25 17:54:57 -07005905 CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
5906
5907 /* clear (again), then enable host interrupts */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005908 iwl3945_write32(priv, CSR_INT, 0xFFFFFFFF);
5909 iwl3945_enable_interrupts(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005910
5911 /* really make sure rfkill handshake bits are cleared */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005912 iwl3945_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
5913 iwl3945_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
Zhu Yib481de92007-09-25 17:54:57 -07005914
5915 /* Copy original ucode data image from disk into backup cache.
5916 * This will be used to initialize the on-board processor's
5917 * data SRAM for a clean start when the runtime program first loads. */
5918 memcpy(priv->ucode_data_backup.v_addr, priv->ucode_data.v_addr,
Zhu Yi5a669262008-01-14 17:46:18 -08005919 priv->ucode_data.len);
Zhu Yib481de92007-09-25 17:54:57 -07005920
Zhu Yie655b9f2008-01-24 02:19:38 -08005921 /* We return success when we resume from suspend and rf_kill is on. */
5922 if (test_bit(STATUS_RF_KILL_HW, &priv->status))
5923 return 0;
5924
Zhu Yib481de92007-09-25 17:54:57 -07005925 for (i = 0; i < MAX_HW_RESTARTS; i++) {
5926
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005927 iwl3945_clear_stations_table(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005928
5929 /* load bootstrap state machine,
5930 * load bootstrap program into processor's memory,
5931 * prepare to load the "initialize" uCode */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005932 rc = iwl3945_load_bsm(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005933
5934 if (rc) {
5935 IWL_ERROR("Unable to set up bootstrap uCode: %d\n", rc);
5936 continue;
5937 }
5938
5939 /* start card; "initialize" will load runtime ucode */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005940 iwl3945_nic_start(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005941
Zhu Yib481de92007-09-25 17:54:57 -07005942 IWL_DEBUG_INFO(DRV_NAME " is coming up\n");
5943
5944 return 0;
5945 }
5946
5947 set_bit(STATUS_EXIT_PENDING, &priv->status);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005948 __iwl3945_down(priv);
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08005949 clear_bit(STATUS_EXIT_PENDING, &priv->status);
Zhu Yib481de92007-09-25 17:54:57 -07005950
5951 /* tried to restart and config the device for as long as our
5952 * patience could withstand */
5953 IWL_ERROR("Unable to initialize device after %d attempts.\n", i);
5954 return -EIO;
5955}
5956
5957
5958/*****************************************************************************
5959 *
5960 * Workqueue callbacks
5961 *
5962 *****************************************************************************/
5963
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005964static void iwl3945_bg_init_alive_start(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07005965{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005966 struct iwl3945_priv *priv =
5967 container_of(data, struct iwl3945_priv, init_alive_start.work);
Zhu Yib481de92007-09-25 17:54:57 -07005968
5969 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
5970 return;
5971
5972 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005973 iwl3945_init_alive_start(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005974 mutex_unlock(&priv->mutex);
5975}
5976
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005977static void iwl3945_bg_alive_start(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07005978{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005979 struct iwl3945_priv *priv =
5980 container_of(data, struct iwl3945_priv, alive_start.work);
Zhu Yib481de92007-09-25 17:54:57 -07005981
5982 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
5983 return;
5984
5985 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005986 iwl3945_alive_start(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005987 mutex_unlock(&priv->mutex);
5988}
5989
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005990static void iwl3945_bg_rf_kill(struct work_struct *work)
Zhu Yib481de92007-09-25 17:54:57 -07005991{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005992 struct iwl3945_priv *priv = container_of(work, struct iwl3945_priv, rf_kill);
Zhu Yib481de92007-09-25 17:54:57 -07005993
5994 wake_up_interruptible(&priv->wait_command_queue);
5995
5996 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
5997 return;
5998
5999 mutex_lock(&priv->mutex);
6000
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006001 if (!iwl3945_is_rfkill(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07006002 IWL_DEBUG(IWL_DL_INFO | IWL_DL_RF_KILL,
6003 "HW and/or SW RF Kill no longer active, restarting "
6004 "device\n");
6005 if (!test_bit(STATUS_EXIT_PENDING, &priv->status))
6006 queue_work(priv->workqueue, &priv->restart);
6007 } else {
6008
6009 if (!test_bit(STATUS_RF_KILL_HW, &priv->status))
6010 IWL_DEBUG_RF_KILL("Can not turn radio back on - "
6011 "disabled by SW switch\n");
6012 else
6013 IWL_WARNING("Radio Frequency Kill Switch is On:\n"
6014 "Kill switch must be turned off for "
6015 "wireless networking to work.\n");
6016 }
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08006017
Zhu Yib481de92007-09-25 17:54:57 -07006018 mutex_unlock(&priv->mutex);
Adel Gadllah80fcc9e2008-07-01 17:49:50 +02006019 iwl3945_rfkill_set_hw_state(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006020}
6021
6022#define IWL_SCAN_CHECK_WATCHDOG (7 * HZ)
6023
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006024static void iwl3945_bg_scan_check(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07006025{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006026 struct iwl3945_priv *priv =
6027 container_of(data, struct iwl3945_priv, scan_check.work);
Zhu Yib481de92007-09-25 17:54:57 -07006028
6029 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
6030 return;
6031
6032 mutex_lock(&priv->mutex);
6033 if (test_bit(STATUS_SCANNING, &priv->status) ||
6034 test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
6035 IWL_DEBUG(IWL_DL_INFO | IWL_DL_SCAN,
6036 "Scan completion watchdog resetting adapter (%dms)\n",
6037 jiffies_to_msecs(IWL_SCAN_CHECK_WATCHDOG));
Mohamed Abbas15e869d2007-10-25 17:15:46 +08006038
Zhu Yib481de92007-09-25 17:54:57 -07006039 if (!test_bit(STATUS_EXIT_PENDING, &priv->status))
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006040 iwl3945_send_scan_abort(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006041 }
6042 mutex_unlock(&priv->mutex);
6043}
6044
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006045static void iwl3945_bg_request_scan(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07006046{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006047 struct iwl3945_priv *priv =
6048 container_of(data, struct iwl3945_priv, request_scan);
6049 struct iwl3945_host_cmd cmd = {
Zhu Yib481de92007-09-25 17:54:57 -07006050 .id = REPLY_SCAN_CMD,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006051 .len = sizeof(struct iwl3945_scan_cmd),
Zhu Yib481de92007-09-25 17:54:57 -07006052 .meta.flags = CMD_SIZE_HUGE,
6053 };
6054 int rc = 0;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006055 struct iwl3945_scan_cmd *scan;
Zhu Yib481de92007-09-25 17:54:57 -07006056 struct ieee80211_conf *conf = NULL;
Abhijeet Kolekarf9340522008-09-03 11:26:58 +08006057 u8 n_probes = 2;
Johannes Berg8318d782008-01-24 19:38:38 +01006058 enum ieee80211_band band;
John W. Linville9387b7c2008-09-30 20:59:05 -04006059 DECLARE_SSID_BUF(ssid);
Zhu Yib481de92007-09-25 17:54:57 -07006060
6061 conf = ieee80211_get_hw_conf(priv->hw);
6062
6063 mutex_lock(&priv->mutex);
6064
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006065 if (!iwl3945_is_ready(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07006066 IWL_WARNING("request scan called when driver not ready.\n");
6067 goto done;
6068 }
6069
Tomas Winklera96a27f2008-10-23 23:48:56 -07006070 /* Make sure the scan wasn't canceled before this queued work
Zhu Yib481de92007-09-25 17:54:57 -07006071 * was given the chance to run... */
6072 if (!test_bit(STATUS_SCANNING, &priv->status))
6073 goto done;
6074
6075 /* This should never be called or scheduled if there is currently
6076 * a scan active in the hardware. */
6077 if (test_bit(STATUS_SCAN_HW, &priv->status)) {
6078 IWL_DEBUG_INFO("Multiple concurrent scan requests in parallel. "
6079 "Ignoring second request.\n");
6080 rc = -EIO;
6081 goto done;
6082 }
6083
6084 if (test_bit(STATUS_EXIT_PENDING, &priv->status)) {
6085 IWL_DEBUG_SCAN("Aborting scan due to device shutdown\n");
6086 goto done;
6087 }
6088
6089 if (test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
6090 IWL_DEBUG_HC("Scan request while abort pending. Queuing.\n");
6091 goto done;
6092 }
6093
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006094 if (iwl3945_is_rfkill(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07006095 IWL_DEBUG_HC("Aborting scan due to RF Kill activation\n");
6096 goto done;
6097 }
6098
6099 if (!test_bit(STATUS_READY, &priv->status)) {
6100 IWL_DEBUG_HC("Scan request while uninitialized. Queuing.\n");
6101 goto done;
6102 }
6103
6104 if (!priv->scan_bands) {
6105 IWL_DEBUG_HC("Aborting scan due to no requested bands\n");
6106 goto done;
6107 }
6108
6109 if (!priv->scan) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006110 priv->scan = kmalloc(sizeof(struct iwl3945_scan_cmd) +
Zhu Yib481de92007-09-25 17:54:57 -07006111 IWL_MAX_SCAN_SIZE, GFP_KERNEL);
6112 if (!priv->scan) {
6113 rc = -ENOMEM;
6114 goto done;
6115 }
6116 }
6117 scan = priv->scan;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006118 memset(scan, 0, sizeof(struct iwl3945_scan_cmd) + IWL_MAX_SCAN_SIZE);
Zhu Yib481de92007-09-25 17:54:57 -07006119
6120 scan->quiet_plcp_th = IWL_PLCP_QUIET_THRESH;
6121 scan->quiet_time = IWL_ACTIVE_QUIET_TIME;
6122
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006123 if (iwl3945_is_associated(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07006124 u16 interval = 0;
6125 u32 extra;
6126 u32 suspend_time = 100;
6127 u32 scan_suspend_time = 100;
6128 unsigned long flags;
6129
6130 IWL_DEBUG_INFO("Scanning while associated...\n");
6131
6132 spin_lock_irqsave(&priv->lock, flags);
6133 interval = priv->beacon_int;
6134 spin_unlock_irqrestore(&priv->lock, flags);
6135
6136 scan->suspend_time = 0;
Mohamed Abbas15e869d2007-10-25 17:15:46 +08006137 scan->max_out_time = cpu_to_le32(200 * 1024);
Zhu Yib481de92007-09-25 17:54:57 -07006138 if (!interval)
6139 interval = suspend_time;
6140 /*
6141 * suspend time format:
6142 * 0-19: beacon interval in usec (time before exec.)
6143 * 20-23: 0
6144 * 24-31: number of beacons (suspend between channels)
6145 */
6146
6147 extra = (suspend_time / interval) << 24;
6148 scan_suspend_time = 0xFF0FFFFF &
6149 (extra | ((suspend_time % interval) * 1024));
6150
6151 scan->suspend_time = cpu_to_le32(scan_suspend_time);
6152 IWL_DEBUG_SCAN("suspend_time 0x%X beacon interval %d\n",
6153 scan_suspend_time, interval);
6154 }
6155
6156 /* We should add the ability for user to lock to PASSIVE ONLY */
6157 if (priv->one_direct_scan) {
6158 IWL_DEBUG_SCAN
6159 ("Kicking off one direct scan for '%s'\n",
John W. Linville9387b7c2008-09-30 20:59:05 -04006160 print_ssid(ssid, priv->direct_ssid,
6161 priv->direct_ssid_len));
Zhu Yib481de92007-09-25 17:54:57 -07006162 scan->direct_scan[0].id = WLAN_EID_SSID;
6163 scan->direct_scan[0].len = priv->direct_ssid_len;
6164 memcpy(scan->direct_scan[0].ssid,
6165 priv->direct_ssid, priv->direct_ssid_len);
Abhijeet Kolekarf9340522008-09-03 11:26:58 +08006166 n_probes++;
Abhijeet Kolekarf9340522008-09-03 11:26:58 +08006167 } else
Bill Moss786b4552008-04-17 16:03:40 -07006168 IWL_DEBUG_SCAN("Kicking off one indirect scan.\n");
Zhu Yib481de92007-09-25 17:54:57 -07006169
6170 /* We don't build a direct scan probe request; the uCode will do
6171 * that based on the direct_mask added to each channel entry */
6172 scan->tx_cmd.len = cpu_to_le16(
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006173 iwl3945_fill_probe_req(priv, (struct ieee80211_mgmt *)scan->data,
Johannes Berg430cfe92008-10-28 18:06:02 +01006174 IWL_MAX_SCAN_SIZE - sizeof(*scan)));
Zhu Yib481de92007-09-25 17:54:57 -07006175 scan->tx_cmd.tx_flags = TX_CMD_FLG_SEQ_CTL_MSK;
6176 scan->tx_cmd.sta_id = priv->hw_setting.bcast_sta_id;
6177 scan->tx_cmd.stop_time.life_time = TX_CMD_LIFE_TIME_INFINITE;
6178
6179 /* flags + rate selection */
6180
Ron Rindjunsky66b50042008-06-25 16:46:31 +08006181 if (priv->scan_bands & BIT(IEEE80211_BAND_2GHZ)) {
Zhu Yib481de92007-09-25 17:54:57 -07006182 scan->flags = RXON_FLG_BAND_24G_MSK | RXON_FLG_AUTO_DETECT_MSK;
6183 scan->tx_cmd.rate = IWL_RATE_1M_PLCP;
6184 scan->good_CRC_th = 0;
Johannes Berg8318d782008-01-24 19:38:38 +01006185 band = IEEE80211_BAND_2GHZ;
Ron Rindjunsky66b50042008-06-25 16:46:31 +08006186 } else if (priv->scan_bands & BIT(IEEE80211_BAND_5GHZ)) {
Zhu Yib481de92007-09-25 17:54:57 -07006187 scan->tx_cmd.rate = IWL_RATE_6M_PLCP;
6188 scan->good_CRC_th = IWL_GOOD_CRC_TH;
Johannes Berg8318d782008-01-24 19:38:38 +01006189 band = IEEE80211_BAND_5GHZ;
Ron Rindjunsky66b50042008-06-25 16:46:31 +08006190 } else {
Zhu Yib481de92007-09-25 17:54:57 -07006191 IWL_WARNING("Invalid scan band count\n");
6192 goto done;
6193 }
6194
6195 /* select Rx antennas */
6196 scan->flags |= iwl3945_get_antenna_flags(priv);
6197
Johannes Berg05c914f2008-09-11 00:01:58 +02006198 if (priv->iw_mode == NL80211_IFTYPE_MONITOR)
Zhu Yib481de92007-09-25 17:54:57 -07006199 scan->filter_flags = RXON_FILTER_PROMISC_MSK;
6200
Abhijeet Kolekarf9340522008-09-03 11:26:58 +08006201 scan->channel_count =
6202 iwl3945_get_channels_for_scan(priv, band, 1, /* active */
6203 n_probes,
6204 (void *)&scan->data[le16_to_cpu(scan->tx_cmd.len)]);
Zhu Yib481de92007-09-25 17:54:57 -07006205
Reinette Chatre14b54332008-11-04 12:21:35 -08006206 if (scan->channel_count == 0) {
6207 IWL_DEBUG_SCAN("channel count %d\n", scan->channel_count);
6208 goto done;
6209 }
6210
Zhu Yib481de92007-09-25 17:54:57 -07006211 cmd.len += le16_to_cpu(scan->tx_cmd.len) +
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006212 scan->channel_count * sizeof(struct iwl3945_scan_channel);
Zhu Yib481de92007-09-25 17:54:57 -07006213 cmd.data = scan;
6214 scan->len = cpu_to_le16(cmd.len);
6215
6216 set_bit(STATUS_SCAN_HW, &priv->status);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006217 rc = iwl3945_send_cmd_sync(priv, &cmd);
Zhu Yib481de92007-09-25 17:54:57 -07006218 if (rc)
6219 goto done;
6220
6221 queue_delayed_work(priv->workqueue, &priv->scan_check,
6222 IWL_SCAN_CHECK_WATCHDOG);
6223
6224 mutex_unlock(&priv->mutex);
6225 return;
6226
6227 done:
Mohamed Abbas2420ebc2008-11-04 12:21:34 -08006228 /* can not perform scan make sure we clear scanning
6229 * bits from status so next scan request can be performed.
6230 * if we dont clear scanning status bit here all next scan
6231 * will fail
6232 */
6233 clear_bit(STATUS_SCAN_HW, &priv->status);
6234 clear_bit(STATUS_SCANNING, &priv->status);
6235
Ian Schram01ebd062007-10-25 17:15:22 +08006236 /* inform mac80211 scan aborted */
Zhu Yib481de92007-09-25 17:54:57 -07006237 queue_work(priv->workqueue, &priv->scan_completed);
6238 mutex_unlock(&priv->mutex);
6239}
6240
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006241static void iwl3945_bg_up(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07006242{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006243 struct iwl3945_priv *priv = container_of(data, struct iwl3945_priv, up);
Zhu Yib481de92007-09-25 17:54:57 -07006244
6245 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
6246 return;
6247
6248 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006249 __iwl3945_up(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006250 mutex_unlock(&priv->mutex);
Adel Gadllah80fcc9e2008-07-01 17:49:50 +02006251 iwl3945_rfkill_set_hw_state(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006252}
6253
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006254static void iwl3945_bg_restart(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07006255{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006256 struct iwl3945_priv *priv = container_of(data, struct iwl3945_priv, restart);
Zhu Yib481de92007-09-25 17:54:57 -07006257
6258 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
6259 return;
6260
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006261 iwl3945_down(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006262 queue_work(priv->workqueue, &priv->up);
6263}
6264
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006265static void iwl3945_bg_rx_replenish(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07006266{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006267 struct iwl3945_priv *priv =
6268 container_of(data, struct iwl3945_priv, rx_replenish);
Zhu Yib481de92007-09-25 17:54:57 -07006269
6270 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
6271 return;
6272
6273 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006274 iwl3945_rx_replenish(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006275 mutex_unlock(&priv->mutex);
6276}
6277
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08006278#define IWL_DELAY_NEXT_SCAN (HZ*2)
6279
Abhijeet Kolekarcd56d332008-09-03 11:26:21 +08006280static void iwl3945_post_associate(struct iwl3945_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07006281{
Zhu Yib481de92007-09-25 17:54:57 -07006282 int rc = 0;
6283 struct ieee80211_conf *conf = NULL;
6284
Johannes Berg05c914f2008-09-11 00:01:58 +02006285 if (priv->iw_mode == NL80211_IFTYPE_AP) {
Tomas Winkler3ac7f142008-07-21 02:40:14 +03006286 IWL_ERROR("%s Should not be called in AP mode\n", __func__);
Zhu Yib481de92007-09-25 17:54:57 -07006287 return;
6288 }
6289
6290
Johannes Berge1749612008-10-27 15:59:26 -07006291 IWL_DEBUG_ASSOC("Associated as %d to: %pM\n",
6292 priv->assoc_id, priv->active_rxon.bssid_addr);
Zhu Yib481de92007-09-25 17:54:57 -07006293
6294 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
6295 return;
6296
Abhijeet Kolekar322a9812008-09-03 11:26:27 +08006297 if (!priv->vif || !priv->is_open)
Mohamed Abbas6ef89d02007-10-25 17:15:47 +08006298 return;
Abhijeet Kolekar322a9812008-09-03 11:26:27 +08006299
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006300 iwl3945_scan_cancel_timeout(priv, 200);
Mohamed Abbas15e869d2007-10-25 17:15:46 +08006301
Zhu Yib481de92007-09-25 17:54:57 -07006302 conf = ieee80211_get_hw_conf(priv->hw);
6303
6304 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006305 iwl3945_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006306
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006307 memset(&priv->rxon_timing, 0, sizeof(struct iwl3945_rxon_time_cmd));
6308 iwl3945_setup_rxon_timing(priv);
6309 rc = iwl3945_send_cmd_pdu(priv, REPLY_RXON_TIMING,
Zhu Yib481de92007-09-25 17:54:57 -07006310 sizeof(priv->rxon_timing), &priv->rxon_timing);
6311 if (rc)
6312 IWL_WARNING("REPLY_RXON_TIMING failed - "
6313 "Attempting to continue.\n");
6314
6315 priv->staging_rxon.filter_flags |= RXON_FILTER_ASSOC_MSK;
6316
6317 priv->staging_rxon.assoc_id = cpu_to_le16(priv->assoc_id);
6318
6319 IWL_DEBUG_ASSOC("assoc id %d beacon interval %d\n",
6320 priv->assoc_id, priv->beacon_int);
6321
6322 if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_PREAMBLE)
6323 priv->staging_rxon.flags |= RXON_FLG_SHORT_PREAMBLE_MSK;
6324 else
6325 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_PREAMBLE_MSK;
6326
6327 if (priv->staging_rxon.flags & RXON_FLG_BAND_24G_MSK) {
6328 if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_SLOT_TIME)
6329 priv->staging_rxon.flags |= RXON_FLG_SHORT_SLOT_MSK;
6330 else
6331 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
6332
Johannes Berg05c914f2008-09-11 00:01:58 +02006333 if (priv->iw_mode == NL80211_IFTYPE_ADHOC)
Zhu Yib481de92007-09-25 17:54:57 -07006334 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
6335
6336 }
6337
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006338 iwl3945_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006339
6340 switch (priv->iw_mode) {
Johannes Berg05c914f2008-09-11 00:01:58 +02006341 case NL80211_IFTYPE_STATION:
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006342 iwl3945_rate_scale_init(priv->hw, IWL_AP_ID);
Zhu Yib481de92007-09-25 17:54:57 -07006343 break;
6344
Johannes Berg05c914f2008-09-11 00:01:58 +02006345 case NL80211_IFTYPE_ADHOC:
Zhu Yib481de92007-09-25 17:54:57 -07006346
Abhijeet Kolekarce546fd2008-11-19 15:32:22 -08006347 priv->assoc_id = 1;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006348 iwl3945_add_station(priv, priv->bssid, 0, 0);
Zhu Yib481de92007-09-25 17:54:57 -07006349 iwl3945_sync_sta(priv, IWL_STA_ID,
Johannes Berg8318d782008-01-24 19:38:38 +01006350 (priv->band == IEEE80211_BAND_5GHZ) ?
Zhu Yib481de92007-09-25 17:54:57 -07006351 IWL_RATE_6M_PLCP : IWL_RATE_1M_PLCP,
6352 CMD_ASYNC);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006353 iwl3945_rate_scale_init(priv->hw, IWL_STA_ID);
6354 iwl3945_send_beacon_cmd(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006355
6356 break;
6357
6358 default:
6359 IWL_ERROR("%s Should not be called in %d mode\n",
Tomas Winkler3ac7f142008-07-21 02:40:14 +03006360 __func__, priv->iw_mode);
Zhu Yib481de92007-09-25 17:54:57 -07006361 break;
6362 }
6363
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006364 iwl3945_activate_qos(priv, 0);
Ron Rindjunsky292ae172008-02-06 11:20:39 -08006365
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08006366 /* we have just associated, don't start scan too early */
6367 priv->next_scan_jiffies = jiffies + IWL_DELAY_NEXT_SCAN;
Abhijeet Kolekarcd56d332008-09-03 11:26:21 +08006368}
6369
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006370static void iwl3945_bg_abort_scan(struct work_struct *work)
Zhu Yib481de92007-09-25 17:54:57 -07006371{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006372 struct iwl3945_priv *priv = container_of(work, struct iwl3945_priv, abort_scan);
Zhu Yib481de92007-09-25 17:54:57 -07006373
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006374 if (!iwl3945_is_ready(priv))
Zhu Yib481de92007-09-25 17:54:57 -07006375 return;
6376
6377 mutex_lock(&priv->mutex);
6378
6379 set_bit(STATUS_SCAN_ABORTING, &priv->status);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006380 iwl3945_send_scan_abort(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006381
6382 mutex_unlock(&priv->mutex);
6383}
6384
Johannes Berge8975582008-10-09 12:18:51 +02006385static int iwl3945_mac_config(struct ieee80211_hw *hw, u32 changed);
Zhu Yi76bb77e2007-11-22 10:53:22 +08006386
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006387static void iwl3945_bg_scan_completed(struct work_struct *work)
Zhu Yib481de92007-09-25 17:54:57 -07006388{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006389 struct iwl3945_priv *priv =
6390 container_of(work, struct iwl3945_priv, scan_completed);
Zhu Yib481de92007-09-25 17:54:57 -07006391
6392 IWL_DEBUG(IWL_DL_INFO | IWL_DL_SCAN, "SCAN complete scan\n");
6393
6394 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
6395 return;
6396
Zhu Yia0646472007-12-20 14:10:01 +08006397 if (test_bit(STATUS_CONF_PENDING, &priv->status))
Johannes Berge8975582008-10-09 12:18:51 +02006398 iwl3945_mac_config(priv->hw, 0);
Zhu Yi76bb77e2007-11-22 10:53:22 +08006399
Zhu Yib481de92007-09-25 17:54:57 -07006400 ieee80211_scan_completed(priv->hw);
6401
6402 /* Since setting the TXPOWER may have been deferred while
6403 * performing the scan, fire one off */
6404 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006405 iwl3945_hw_reg_send_txpower(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006406 mutex_unlock(&priv->mutex);
6407}
6408
6409/*****************************************************************************
6410 *
6411 * mac80211 entry point functions
6412 *
6413 *****************************************************************************/
6414
Zhu Yi5a669262008-01-14 17:46:18 -08006415#define UCODE_READY_TIMEOUT (2 * HZ)
6416
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006417static int iwl3945_mac_start(struct ieee80211_hw *hw)
Zhu Yib481de92007-09-25 17:54:57 -07006418{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006419 struct iwl3945_priv *priv = hw->priv;
Zhu Yi5a669262008-01-14 17:46:18 -08006420 int ret;
Zhu Yib481de92007-09-25 17:54:57 -07006421
6422 IWL_DEBUG_MAC80211("enter\n");
6423
Zhu Yi5a669262008-01-14 17:46:18 -08006424 if (pci_enable_device(priv->pci_dev)) {
6425 IWL_ERROR("Fail to pci_enable_device\n");
6426 return -ENODEV;
6427 }
6428 pci_restore_state(priv->pci_dev);
6429 pci_enable_msi(priv->pci_dev);
6430
6431 ret = request_irq(priv->pci_dev->irq, iwl3945_isr, IRQF_SHARED,
6432 DRV_NAME, priv);
6433 if (ret) {
6434 IWL_ERROR("Error allocating IRQ %d\n", priv->pci_dev->irq);
6435 goto out_disable_msi;
6436 }
6437
Zhu Yib481de92007-09-25 17:54:57 -07006438 /* we should be verifying the device is ready to be opened */
6439 mutex_lock(&priv->mutex);
6440
Zhu Yi5a669262008-01-14 17:46:18 -08006441 memset(&priv->staging_rxon, 0, sizeof(struct iwl3945_rxon_cmd));
6442 /* fetch ucode file from disk, alloc and copy to bus-master buffers ...
6443 * ucode filename and max sizes are card-specific. */
6444
6445 if (!priv->ucode_code.len) {
6446 ret = iwl3945_read_ucode(priv);
6447 if (ret) {
6448 IWL_ERROR("Could not read microcode: %d\n", ret);
6449 mutex_unlock(&priv->mutex);
6450 goto out_release_irq;
6451 }
6452 }
6453
Zhu Yie655b9f2008-01-24 02:19:38 -08006454 ret = __iwl3945_up(priv);
Zhu Yi5a669262008-01-14 17:46:18 -08006455
Zhu Yib481de92007-09-25 17:54:57 -07006456 mutex_unlock(&priv->mutex);
Zhu Yi5a669262008-01-14 17:46:18 -08006457
Adel Gadllah80fcc9e2008-07-01 17:49:50 +02006458 iwl3945_rfkill_set_hw_state(priv);
6459
Zhu Yie655b9f2008-01-24 02:19:38 -08006460 if (ret)
6461 goto out_release_irq;
6462
6463 IWL_DEBUG_INFO("Start UP work.\n");
6464
6465 if (test_bit(STATUS_IN_SUSPEND, &priv->status))
6466 return 0;
6467
Zhu Yi5a669262008-01-14 17:46:18 -08006468 /* Wait for START_ALIVE from ucode. Otherwise callbacks from
6469 * mac80211 will not be run successfully. */
6470 ret = wait_event_interruptible_timeout(priv->wait_command_queue,
6471 test_bit(STATUS_READY, &priv->status),
6472 UCODE_READY_TIMEOUT);
6473 if (!ret) {
6474 if (!test_bit(STATUS_READY, &priv->status)) {
6475 IWL_ERROR("Wait for START_ALIVE timeout after %dms.\n",
6476 jiffies_to_msecs(UCODE_READY_TIMEOUT));
6477 ret = -ETIMEDOUT;
6478 goto out_release_irq;
6479 }
6480 }
6481
Zhu Yie655b9f2008-01-24 02:19:38 -08006482 priv->is_open = 1;
Zhu Yib481de92007-09-25 17:54:57 -07006483 IWL_DEBUG_MAC80211("leave\n");
6484 return 0;
Zhu Yi5a669262008-01-14 17:46:18 -08006485
6486out_release_irq:
6487 free_irq(priv->pci_dev->irq, priv);
6488out_disable_msi:
6489 pci_disable_msi(priv->pci_dev);
Zhu Yie655b9f2008-01-24 02:19:38 -08006490 pci_disable_device(priv->pci_dev);
6491 priv->is_open = 0;
6492 IWL_DEBUG_MAC80211("leave - failed\n");
Zhu Yi5a669262008-01-14 17:46:18 -08006493 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07006494}
6495
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006496static void iwl3945_mac_stop(struct ieee80211_hw *hw)
Zhu Yib481de92007-09-25 17:54:57 -07006497{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006498 struct iwl3945_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07006499
6500 IWL_DEBUG_MAC80211("enter\n");
Mohamed Abbas6ef89d02007-10-25 17:15:47 +08006501
Zhu Yie655b9f2008-01-24 02:19:38 -08006502 if (!priv->is_open) {
6503 IWL_DEBUG_MAC80211("leave - skip\n");
6504 return;
6505 }
6506
Zhu Yib481de92007-09-25 17:54:57 -07006507 priv->is_open = 0;
Zhu Yi5a669262008-01-14 17:46:18 -08006508
6509 if (iwl3945_is_ready_rf(priv)) {
Zhu Yie655b9f2008-01-24 02:19:38 -08006510 /* stop mac, cancel any scan request and clear
6511 * RXON_FILTER_ASSOC_MSK BIT
6512 */
Zhu Yi5a669262008-01-14 17:46:18 -08006513 mutex_lock(&priv->mutex);
6514 iwl3945_scan_cancel_timeout(priv, 100);
Mohamed Abbasfde35712007-11-29 11:10:15 +08006515 mutex_unlock(&priv->mutex);
Mohamed Abbasfde35712007-11-29 11:10:15 +08006516 }
6517
Zhu Yi5a669262008-01-14 17:46:18 -08006518 iwl3945_down(priv);
6519
6520 flush_workqueue(priv->workqueue);
6521 free_irq(priv->pci_dev->irq, priv);
6522 pci_disable_msi(priv->pci_dev);
6523 pci_save_state(priv->pci_dev);
6524 pci_disable_device(priv->pci_dev);
Mohamed Abbas6ef89d02007-10-25 17:15:47 +08006525
Zhu Yib481de92007-09-25 17:54:57 -07006526 IWL_DEBUG_MAC80211("leave\n");
Zhu Yib481de92007-09-25 17:54:57 -07006527}
6528
Johannes Berge039fa42008-05-15 12:55:29 +02006529static int iwl3945_mac_tx(struct ieee80211_hw *hw, struct sk_buff *skb)
Zhu Yib481de92007-09-25 17:54:57 -07006530{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006531 struct iwl3945_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07006532
6533 IWL_DEBUG_MAC80211("enter\n");
6534
Zhu Yib481de92007-09-25 17:54:57 -07006535 IWL_DEBUG_TX("dev->xmit(%d bytes) at rate 0x%02x\n", skb->len,
Johannes Berge039fa42008-05-15 12:55:29 +02006536 ieee80211_get_tx_rate(hw, IEEE80211_SKB_CB(skb))->bitrate);
Zhu Yib481de92007-09-25 17:54:57 -07006537
Johannes Berge039fa42008-05-15 12:55:29 +02006538 if (iwl3945_tx_skb(priv, skb))
Zhu Yib481de92007-09-25 17:54:57 -07006539 dev_kfree_skb_any(skb);
6540
6541 IWL_DEBUG_MAC80211("leave\n");
Reinette Chatre637f8832009-01-19 15:30:32 -08006542 return NETDEV_TX_OK;
Zhu Yib481de92007-09-25 17:54:57 -07006543}
6544
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006545static int iwl3945_mac_add_interface(struct ieee80211_hw *hw,
Zhu Yib481de92007-09-25 17:54:57 -07006546 struct ieee80211_if_init_conf *conf)
6547{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006548 struct iwl3945_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07006549 unsigned long flags;
6550
Johannes Berg32bfd352007-12-19 01:31:26 +01006551 IWL_DEBUG_MAC80211("enter: type %d\n", conf->type);
Zhu Yib481de92007-09-25 17:54:57 -07006552
Johannes Berg32bfd352007-12-19 01:31:26 +01006553 if (priv->vif) {
6554 IWL_DEBUG_MAC80211("leave - vif != NULL\n");
Tomas Winkler864792e2007-11-27 21:00:52 +02006555 return -EOPNOTSUPP;
Zhu Yib481de92007-09-25 17:54:57 -07006556 }
6557
6558 spin_lock_irqsave(&priv->lock, flags);
Johannes Berg32bfd352007-12-19 01:31:26 +01006559 priv->vif = conf->vif;
Zhu, Yi60294de2008-10-29 14:05:45 -07006560 priv->iw_mode = conf->type;
Zhu Yib481de92007-09-25 17:54:57 -07006561
6562 spin_unlock_irqrestore(&priv->lock, flags);
6563
6564 mutex_lock(&priv->mutex);
Tomas Winkler864792e2007-11-27 21:00:52 +02006565
6566 if (conf->mac_addr) {
Johannes Berge1749612008-10-27 15:59:26 -07006567 IWL_DEBUG_MAC80211("Set: %pM\n", conf->mac_addr);
Tomas Winkler864792e2007-11-27 21:00:52 +02006568 memcpy(priv->mac_addr, conf->mac_addr, ETH_ALEN);
6569 }
6570
Zhu Yi5a669262008-01-14 17:46:18 -08006571 if (iwl3945_is_ready(priv))
6572 iwl3945_set_mode(priv, conf->type);
Zhu Yib481de92007-09-25 17:54:57 -07006573
Zhu Yib481de92007-09-25 17:54:57 -07006574 mutex_unlock(&priv->mutex);
6575
Zhu Yi5a669262008-01-14 17:46:18 -08006576 IWL_DEBUG_MAC80211("leave\n");
Zhu Yib481de92007-09-25 17:54:57 -07006577 return 0;
6578}
6579
6580/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006581 * iwl3945_mac_config - mac80211 config callback
Zhu Yib481de92007-09-25 17:54:57 -07006582 *
6583 * We ignore conf->flags & IEEE80211_CONF_SHORT_SLOT_TIME since it seems to
6584 * be set inappropriately and the driver currently sets the hardware up to
6585 * use it whenever needed.
6586 */
Johannes Berge8975582008-10-09 12:18:51 +02006587static int iwl3945_mac_config(struct ieee80211_hw *hw, u32 changed)
Zhu Yib481de92007-09-25 17:54:57 -07006588{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006589 struct iwl3945_priv *priv = hw->priv;
6590 const struct iwl3945_channel_info *ch_info;
Johannes Berge8975582008-10-09 12:18:51 +02006591 struct ieee80211_conf *conf = &hw->conf;
Zhu Yib481de92007-09-25 17:54:57 -07006592 unsigned long flags;
Zhu Yi76bb77e2007-11-22 10:53:22 +08006593 int ret = 0;
Zhu Yib481de92007-09-25 17:54:57 -07006594
6595 mutex_lock(&priv->mutex);
Johannes Berg8318d782008-01-24 19:38:38 +01006596 IWL_DEBUG_MAC80211("enter to channel %d\n", conf->channel->hw_value);
Zhu Yib481de92007-09-25 17:54:57 -07006597
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006598 if (!iwl3945_is_ready(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07006599 IWL_DEBUG_MAC80211("leave - not ready\n");
Zhu Yi76bb77e2007-11-22 10:53:22 +08006600 ret = -EIO;
6601 goto out;
Zhu Yib481de92007-09-25 17:54:57 -07006602 }
6603
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006604 if (unlikely(!iwl3945_param_disable_hw_scan &&
Zhu Yib481de92007-09-25 17:54:57 -07006605 test_bit(STATUS_SCANNING, &priv->status))) {
Zhu Yia0646472007-12-20 14:10:01 +08006606 IWL_DEBUG_MAC80211("leave - scanning\n");
6607 set_bit(STATUS_CONF_PENDING, &priv->status);
Zhu Yib481de92007-09-25 17:54:57 -07006608 mutex_unlock(&priv->mutex);
Zhu Yia0646472007-12-20 14:10:01 +08006609 return 0;
Zhu Yib481de92007-09-25 17:54:57 -07006610 }
6611
6612 spin_lock_irqsave(&priv->lock, flags);
6613
Johannes Berg8318d782008-01-24 19:38:38 +01006614 ch_info = iwl3945_get_channel_info(priv, conf->channel->band,
6615 conf->channel->hw_value);
Zhu Yib481de92007-09-25 17:54:57 -07006616 if (!is_channel_valid(ch_info)) {
Ron Rindjunsky66b50042008-06-25 16:46:31 +08006617 IWL_DEBUG_SCAN("Channel %d [%d] is INVALID for this band.\n",
Johannes Berg8318d782008-01-24 19:38:38 +01006618 conf->channel->hw_value, conf->channel->band);
Zhu Yib481de92007-09-25 17:54:57 -07006619 IWL_DEBUG_MAC80211("leave - invalid channel\n");
6620 spin_unlock_irqrestore(&priv->lock, flags);
Zhu Yi76bb77e2007-11-22 10:53:22 +08006621 ret = -EINVAL;
6622 goto out;
Zhu Yib481de92007-09-25 17:54:57 -07006623 }
6624
Johannes Berg8318d782008-01-24 19:38:38 +01006625 iwl3945_set_rxon_channel(priv, conf->channel->band, conf->channel->hw_value);
Zhu Yib481de92007-09-25 17:54:57 -07006626
Johannes Berg8318d782008-01-24 19:38:38 +01006627 iwl3945_set_flags_for_phymode(priv, conf->channel->band);
Zhu Yib481de92007-09-25 17:54:57 -07006628
6629 /* The list of supported rates and rate mask can be different
6630 * for each phymode; since the phymode may have changed, reset
6631 * the rate mask to what mac80211 lists */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006632 iwl3945_set_rate(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006633
6634 spin_unlock_irqrestore(&priv->lock, flags);
6635
6636#ifdef IEEE80211_CONF_CHANNEL_SWITCH
6637 if (conf->flags & IEEE80211_CONF_CHANNEL_SWITCH) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006638 iwl3945_hw_channel_switch(priv, conf->channel);
Zhu Yi76bb77e2007-11-22 10:53:22 +08006639 goto out;
Zhu Yib481de92007-09-25 17:54:57 -07006640 }
6641#endif
6642
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006643 iwl3945_radio_kill_sw(priv, !conf->radio_enabled);
Zhu Yib481de92007-09-25 17:54:57 -07006644
6645 if (!conf->radio_enabled) {
6646 IWL_DEBUG_MAC80211("leave - radio disabled\n");
Zhu Yi76bb77e2007-11-22 10:53:22 +08006647 goto out;
Zhu Yib481de92007-09-25 17:54:57 -07006648 }
6649
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006650 if (iwl3945_is_rfkill(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07006651 IWL_DEBUG_MAC80211("leave - RF kill\n");
Zhu Yi76bb77e2007-11-22 10:53:22 +08006652 ret = -EIO;
6653 goto out;
Zhu Yib481de92007-09-25 17:54:57 -07006654 }
6655
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006656 iwl3945_set_rate(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006657
6658 if (memcmp(&priv->active_rxon,
6659 &priv->staging_rxon, sizeof(priv->staging_rxon)))
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006660 iwl3945_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006661 else
6662 IWL_DEBUG_INFO("No re-sending same RXON configuration.\n");
6663
6664 IWL_DEBUG_MAC80211("leave\n");
6665
Zhu Yi76bb77e2007-11-22 10:53:22 +08006666out:
Zhu Yia0646472007-12-20 14:10:01 +08006667 clear_bit(STATUS_CONF_PENDING, &priv->status);
Zhu Yib481de92007-09-25 17:54:57 -07006668 mutex_unlock(&priv->mutex);
Zhu Yi76bb77e2007-11-22 10:53:22 +08006669 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07006670}
6671
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006672static void iwl3945_config_ap(struct iwl3945_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07006673{
6674 int rc = 0;
6675
Maarten Lankhorstd986bcd2008-01-23 10:15:16 -08006676 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
Zhu Yib481de92007-09-25 17:54:57 -07006677 return;
6678
6679 /* The following should be done only at AP bring up */
Ron Rindjunsky5d1e2322008-06-30 17:23:04 +08006680 if (!(iwl3945_is_associated(priv))) {
Zhu Yib481de92007-09-25 17:54:57 -07006681
6682 /* RXON - unassoc (to set timing command) */
6683 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006684 iwl3945_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006685
6686 /* RXON Timing */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006687 memset(&priv->rxon_timing, 0, sizeof(struct iwl3945_rxon_time_cmd));
6688 iwl3945_setup_rxon_timing(priv);
6689 rc = iwl3945_send_cmd_pdu(priv, REPLY_RXON_TIMING,
Zhu Yib481de92007-09-25 17:54:57 -07006690 sizeof(priv->rxon_timing), &priv->rxon_timing);
6691 if (rc)
6692 IWL_WARNING("REPLY_RXON_TIMING failed - "
6693 "Attempting to continue.\n");
6694
6695 /* FIXME: what should be the assoc_id for AP? */
6696 priv->staging_rxon.assoc_id = cpu_to_le16(priv->assoc_id);
6697 if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_PREAMBLE)
6698 priv->staging_rxon.flags |=
6699 RXON_FLG_SHORT_PREAMBLE_MSK;
6700 else
6701 priv->staging_rxon.flags &=
6702 ~RXON_FLG_SHORT_PREAMBLE_MSK;
6703
6704 if (priv->staging_rxon.flags & RXON_FLG_BAND_24G_MSK) {
6705 if (priv->assoc_capability &
6706 WLAN_CAPABILITY_SHORT_SLOT_TIME)
6707 priv->staging_rxon.flags |=
6708 RXON_FLG_SHORT_SLOT_MSK;
6709 else
6710 priv->staging_rxon.flags &=
6711 ~RXON_FLG_SHORT_SLOT_MSK;
6712
Johannes Berg05c914f2008-09-11 00:01:58 +02006713 if (priv->iw_mode == NL80211_IFTYPE_ADHOC)
Zhu Yib481de92007-09-25 17:54:57 -07006714 priv->staging_rxon.flags &=
6715 ~RXON_FLG_SHORT_SLOT_MSK;
6716 }
6717 /* restore RXON assoc */
6718 priv->staging_rxon.filter_flags |= RXON_FILTER_ASSOC_MSK;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006719 iwl3945_commit_rxon(priv);
6720 iwl3945_add_station(priv, iwl3945_broadcast_addr, 0, 0);
Zhu Yi556f8db2007-09-27 11:27:33 +08006721 }
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006722 iwl3945_send_beacon_cmd(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006723
6724 /* FIXME - we need to add code here to detect a totally new
6725 * configuration, reset the AP, unassoc, rxon timing, assoc,
6726 * clear sta table, add BCAST sta... */
6727}
6728
Johannes Berg32bfd352007-12-19 01:31:26 +01006729static int iwl3945_mac_config_interface(struct ieee80211_hw *hw,
6730 struct ieee80211_vif *vif,
Zhu Yib481de92007-09-25 17:54:57 -07006731 struct ieee80211_if_conf *conf)
6732{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006733 struct iwl3945_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07006734 int rc;
6735
6736 if (conf == NULL)
6737 return -EIO;
6738
Emmanuel Grumbachb716bb92008-03-04 18:09:32 -08006739 if (priv->vif != vif) {
6740 IWL_DEBUG_MAC80211("leave - priv->vif != vif\n");
Emmanuel Grumbachb716bb92008-03-04 18:09:32 -08006741 return 0;
6742 }
6743
Johannes Berg9d139c82008-07-09 14:40:37 +02006744 /* handle this temporarily here */
Johannes Berg05c914f2008-09-11 00:01:58 +02006745 if (priv->iw_mode == NL80211_IFTYPE_ADHOC &&
Johannes Berg9d139c82008-07-09 14:40:37 +02006746 conf->changed & IEEE80211_IFCC_BEACON) {
6747 struct sk_buff *beacon = ieee80211_beacon_get(hw, vif);
6748 if (!beacon)
6749 return -ENOMEM;
Mohamed Abbas9bdf5ec2008-11-07 09:58:35 -08006750 mutex_lock(&priv->mutex);
Johannes Berg9d139c82008-07-09 14:40:37 +02006751 rc = iwl3945_mac_beacon_update(hw, beacon);
Mohamed Abbas9bdf5ec2008-11-07 09:58:35 -08006752 mutex_unlock(&priv->mutex);
Johannes Berg9d139c82008-07-09 14:40:37 +02006753 if (rc)
6754 return rc;
6755 }
6756
Zhu Yi5a669262008-01-14 17:46:18 -08006757 if (!iwl3945_is_alive(priv))
6758 return -EAGAIN;
6759
Zhu Yib481de92007-09-25 17:54:57 -07006760 mutex_lock(&priv->mutex);
6761
Zhu Yib481de92007-09-25 17:54:57 -07006762 if (conf->bssid)
Johannes Berge1749612008-10-27 15:59:26 -07006763 IWL_DEBUG_MAC80211("bssid: %pM\n", conf->bssid);
Zhu Yib481de92007-09-25 17:54:57 -07006764
Johannes Berg4150c572007-09-17 01:29:23 -04006765/*
6766 * very dubious code was here; the probe filtering flag is never set:
6767 *
Zhu Yib481de92007-09-25 17:54:57 -07006768 if (unlikely(test_bit(STATUS_SCANNING, &priv->status)) &&
6769 !(priv->hw->flags & IEEE80211_HW_NO_PROBE_FILTERING)) {
Johannes Berg4150c572007-09-17 01:29:23 -04006770 */
Zhu Yib481de92007-09-25 17:54:57 -07006771
Johannes Berg05c914f2008-09-11 00:01:58 +02006772 if (priv->iw_mode == NL80211_IFTYPE_AP) {
Zhu Yib481de92007-09-25 17:54:57 -07006773 if (!conf->bssid) {
6774 conf->bssid = priv->mac_addr;
6775 memcpy(priv->bssid, priv->mac_addr, ETH_ALEN);
Johannes Berge1749612008-10-27 15:59:26 -07006776 IWL_DEBUG_MAC80211("bssid was set to: %pM\n",
6777 conf->bssid);
Zhu Yib481de92007-09-25 17:54:57 -07006778 }
6779 if (priv->ibss_beacon)
6780 dev_kfree_skb(priv->ibss_beacon);
6781
Johannes Berg9d139c82008-07-09 14:40:37 +02006782 priv->ibss_beacon = ieee80211_beacon_get(hw, vif);
Zhu Yib481de92007-09-25 17:54:57 -07006783 }
6784
Mohamed Abbasfde35712007-11-29 11:10:15 +08006785 if (iwl3945_is_rfkill(priv))
6786 goto done;
6787
Zhu Yib481de92007-09-25 17:54:57 -07006788 if (conf->bssid && !is_zero_ether_addr(conf->bssid) &&
6789 !is_multicast_ether_addr(conf->bssid)) {
6790 /* If there is currently a HW scan going on in the background
6791 * then we need to cancel it else the RXON below will fail. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006792 if (iwl3945_scan_cancel_timeout(priv, 100)) {
Zhu Yib481de92007-09-25 17:54:57 -07006793 IWL_WARNING("Aborted scan still in progress "
6794 "after 100ms\n");
6795 IWL_DEBUG_MAC80211("leaving - scan abort failed.\n");
6796 mutex_unlock(&priv->mutex);
6797 return -EAGAIN;
6798 }
6799 memcpy(priv->staging_rxon.bssid_addr, conf->bssid, ETH_ALEN);
6800
6801 /* TODO: Audit driver for usage of these members and see
6802 * if mac80211 deprecates them (priv->bssid looks like it
6803 * shouldn't be there, but I haven't scanned the IBSS code
6804 * to verify) - jpk */
6805 memcpy(priv->bssid, conf->bssid, ETH_ALEN);
6806
Johannes Berg05c914f2008-09-11 00:01:58 +02006807 if (priv->iw_mode == NL80211_IFTYPE_AP)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006808 iwl3945_config_ap(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006809 else {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006810 rc = iwl3945_commit_rxon(priv);
Johannes Berg05c914f2008-09-11 00:01:58 +02006811 if ((priv->iw_mode == NL80211_IFTYPE_STATION) && rc)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006812 iwl3945_add_station(priv,
Zhu Yi556f8db2007-09-27 11:27:33 +08006813 priv->active_rxon.bssid_addr, 1, 0);
Zhu Yib481de92007-09-25 17:54:57 -07006814 }
6815
6816 } else {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006817 iwl3945_scan_cancel_timeout(priv, 100);
Zhu Yib481de92007-09-25 17:54:57 -07006818 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006819 iwl3945_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006820 }
6821
Mohamed Abbasfde35712007-11-29 11:10:15 +08006822 done:
Zhu Yib481de92007-09-25 17:54:57 -07006823 IWL_DEBUG_MAC80211("leave\n");
6824 mutex_unlock(&priv->mutex);
6825
6826 return 0;
6827}
6828
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006829static void iwl3945_configure_filter(struct ieee80211_hw *hw,
Johannes Berg4150c572007-09-17 01:29:23 -04006830 unsigned int changed_flags,
6831 unsigned int *total_flags,
6832 int mc_count, struct dev_addr_list *mc_list)
6833{
Abhijeet Kolekar5ec03972008-05-05 10:22:48 +08006834 struct iwl3945_priv *priv = hw->priv;
Zhu, Yi352bc8d2008-11-12 13:14:09 -08006835 __le32 *filter_flags = &priv->staging_rxon.filter_flags;
Rick Farrington25b3f572008-06-30 17:23:28 +08006836
Zhu, Yi352bc8d2008-11-12 13:14:09 -08006837 IWL_DEBUG_MAC80211("Enter: changed: 0x%x, total: 0x%x\n",
6838 changed_flags, *total_flags);
6839
6840 if (changed_flags & (FIF_OTHER_BSS | FIF_PROMISC_IN_BSS)) {
6841 if (*total_flags & (FIF_OTHER_BSS | FIF_PROMISC_IN_BSS))
6842 *filter_flags |= RXON_FILTER_PROMISC_MSK;
6843 else
6844 *filter_flags &= ~RXON_FILTER_PROMISC_MSK;
Abhijeet Kolekar5ec03972008-05-05 10:22:48 +08006845 }
Zhu, Yi352bc8d2008-11-12 13:14:09 -08006846 if (changed_flags & FIF_ALLMULTI) {
6847 if (*total_flags & FIF_ALLMULTI)
6848 *filter_flags |= RXON_FILTER_ACCEPT_GRP_MSK;
6849 else
6850 *filter_flags &= ~RXON_FILTER_ACCEPT_GRP_MSK;
6851 }
6852 if (changed_flags & FIF_CONTROL) {
6853 if (*total_flags & FIF_CONTROL)
6854 *filter_flags |= RXON_FILTER_CTL2HOST_MSK;
6855 else
6856 *filter_flags &= ~RXON_FILTER_CTL2HOST_MSK;
6857 }
6858 if (changed_flags & FIF_BCN_PRBRESP_PROMISC) {
6859 if (*total_flags & FIF_BCN_PRBRESP_PROMISC)
6860 *filter_flags |= RXON_FILTER_BCON_AWARE_MSK;
6861 else
6862 *filter_flags &= ~RXON_FILTER_BCON_AWARE_MSK;
6863 }
6864
6865 /* We avoid iwl_commit_rxon here to commit the new filter flags
6866 * since mac80211 will call ieee80211_hw_config immediately.
6867 * (mc_list is not supported at this time). Otherwise, we need to
6868 * queue a background iwl_commit_rxon work.
6869 */
6870
6871 *total_flags &= FIF_OTHER_BSS | FIF_ALLMULTI | FIF_PROMISC_IN_BSS |
Rick Farrington25b3f572008-06-30 17:23:28 +08006872 FIF_BCN_PRBRESP_PROMISC | FIF_CONTROL;
Johannes Berg4150c572007-09-17 01:29:23 -04006873}
6874
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006875static void iwl3945_mac_remove_interface(struct ieee80211_hw *hw,
Zhu Yib481de92007-09-25 17:54:57 -07006876 struct ieee80211_if_init_conf *conf)
6877{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006878 struct iwl3945_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07006879
6880 IWL_DEBUG_MAC80211("enter\n");
6881
6882 mutex_lock(&priv->mutex);
Mohamed Abbas6ef89d02007-10-25 17:15:47 +08006883
Mohamed Abbasfde35712007-11-29 11:10:15 +08006884 if (iwl3945_is_ready_rf(priv)) {
6885 iwl3945_scan_cancel_timeout(priv, 100);
Mohamed Abbasfde35712007-11-29 11:10:15 +08006886 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
6887 iwl3945_commit_rxon(priv);
6888 }
Johannes Berg32bfd352007-12-19 01:31:26 +01006889 if (priv->vif == conf->vif) {
6890 priv->vif = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07006891 memset(priv->bssid, 0, ETH_ALEN);
Zhu Yib481de92007-09-25 17:54:57 -07006892 }
6893 mutex_unlock(&priv->mutex);
6894
6895 IWL_DEBUG_MAC80211("leave\n");
Zhu Yib481de92007-09-25 17:54:57 -07006896}
6897
Abhijeet Kolekarcd56d332008-09-03 11:26:21 +08006898#define IWL_DELAY_NEXT_SCAN_AFTER_ASSOC (HZ*6)
6899
6900static void iwl3945_bss_info_changed(struct ieee80211_hw *hw,
6901 struct ieee80211_vif *vif,
6902 struct ieee80211_bss_conf *bss_conf,
6903 u32 changes)
6904{
6905 struct iwl3945_priv *priv = hw->priv;
6906
6907 IWL_DEBUG_MAC80211("changes = 0x%X\n", changes);
6908
6909 if (changes & BSS_CHANGED_ERP_PREAMBLE) {
6910 IWL_DEBUG_MAC80211("ERP_PREAMBLE %d\n",
6911 bss_conf->use_short_preamble);
6912 if (bss_conf->use_short_preamble)
6913 priv->staging_rxon.flags |= RXON_FLG_SHORT_PREAMBLE_MSK;
6914 else
6915 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_PREAMBLE_MSK;
6916 }
6917
6918 if (changes & BSS_CHANGED_ERP_CTS_PROT) {
6919 IWL_DEBUG_MAC80211("ERP_CTS %d\n", bss_conf->use_cts_prot);
6920 if (bss_conf->use_cts_prot && (priv->band != IEEE80211_BAND_5GHZ))
6921 priv->staging_rxon.flags |= RXON_FLG_TGG_PROTECT_MSK;
6922 else
6923 priv->staging_rxon.flags &= ~RXON_FLG_TGG_PROTECT_MSK;
6924 }
6925
6926 if (changes & BSS_CHANGED_ASSOC) {
6927 IWL_DEBUG_MAC80211("ASSOC %d\n", bss_conf->assoc);
6928 /* This should never happen as this function should
6929 * never be called from interrupt context. */
6930 if (WARN_ON_ONCE(in_interrupt()))
6931 return;
6932 if (bss_conf->assoc) {
6933 priv->assoc_id = bss_conf->aid;
6934 priv->beacon_int = bss_conf->beacon_int;
6935 priv->timestamp0 = bss_conf->timestamp & 0xFFFFFFFF;
6936 priv->timestamp1 = (bss_conf->timestamp >> 32) &
6937 0xFFFFFFFF;
6938 priv->assoc_capability = bss_conf->assoc_capability;
6939 priv->next_scan_jiffies = jiffies +
6940 IWL_DELAY_NEXT_SCAN_AFTER_ASSOC;
6941 mutex_lock(&priv->mutex);
6942 iwl3945_post_associate(priv);
6943 mutex_unlock(&priv->mutex);
6944 } else {
6945 priv->assoc_id = 0;
6946 IWL_DEBUG_MAC80211("DISASSOC %d\n", bss_conf->assoc);
6947 }
6948 } else if (changes && iwl3945_is_associated(priv) && priv->assoc_id) {
6949 IWL_DEBUG_MAC80211("Associated Changes %d\n", changes);
6950 iwl3945_send_rxon_assoc(priv);
6951 }
6952
6953}
6954
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006955static int iwl3945_mac_hw_scan(struct ieee80211_hw *hw, u8 *ssid, size_t len)
Zhu Yib481de92007-09-25 17:54:57 -07006956{
6957 int rc = 0;
6958 unsigned long flags;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006959 struct iwl3945_priv *priv = hw->priv;
John W. Linville9387b7c2008-09-30 20:59:05 -04006960 DECLARE_SSID_BUF(ssid_buf);
Zhu Yib481de92007-09-25 17:54:57 -07006961
6962 IWL_DEBUG_MAC80211("enter\n");
6963
Mohamed Abbas15e869d2007-10-25 17:15:46 +08006964 mutex_lock(&priv->mutex);
Zhu Yib481de92007-09-25 17:54:57 -07006965 spin_lock_irqsave(&priv->lock, flags);
6966
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006967 if (!iwl3945_is_ready_rf(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07006968 rc = -EIO;
6969 IWL_DEBUG_MAC80211("leave - not ready or exit pending\n");
6970 goto out_unlock;
6971 }
6972
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08006973 /* we don't schedule scan within next_scan_jiffies period */
6974 if (priv->next_scan_jiffies &&
6975 time_after(priv->next_scan_jiffies, jiffies)) {
6976 rc = -EAGAIN;
6977 goto out_unlock;
6978 }
Bill Moss15dbf1b2008-05-06 11:05:15 +08006979 /* if we just finished scan ask for delay for a broadcast scan */
6980 if ((len == 0) && priv->last_scan_jiffies &&
6981 time_after(priv->last_scan_jiffies + IWL_DELAY_NEXT_SCAN,
6982 jiffies)) {
Zhu Yib481de92007-09-25 17:54:57 -07006983 rc = -EAGAIN;
6984 goto out_unlock;
6985 }
6986 if (len) {
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08006987 IWL_DEBUG_SCAN("direct scan for %s [%d]\n ",
John W. Linville9387b7c2008-09-30 20:59:05 -04006988 print_ssid(ssid_buf, ssid, len), (int)len);
Zhu Yib481de92007-09-25 17:54:57 -07006989
6990 priv->one_direct_scan = 1;
6991 priv->direct_ssid_len = (u8)
6992 min((u8) len, (u8) IW_ESSID_MAX_SIZE);
6993 memcpy(priv->direct_ssid, ssid, priv->direct_ssid_len);
Mohamed Abbas6ef89d02007-10-25 17:15:47 +08006994 } else
6995 priv->one_direct_scan = 0;
Zhu Yib481de92007-09-25 17:54:57 -07006996
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006997 rc = iwl3945_scan_initiate(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006998
6999 IWL_DEBUG_MAC80211("leave\n");
7000
7001out_unlock:
7002 spin_unlock_irqrestore(&priv->lock, flags);
Mohamed Abbas15e869d2007-10-25 17:15:46 +08007003 mutex_unlock(&priv->mutex);
Zhu Yib481de92007-09-25 17:54:57 -07007004
7005 return rc;
7006}
7007
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007008static int iwl3945_mac_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
Zhu Yib481de92007-09-25 17:54:57 -07007009 const u8 *local_addr, const u8 *addr,
7010 struct ieee80211_key_conf *key)
7011{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007012 struct iwl3945_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07007013 int rc = 0;
7014 u8 sta_id;
7015
7016 IWL_DEBUG_MAC80211("enter\n");
7017
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007018 if (!iwl3945_param_hwcrypto) {
Zhu Yib481de92007-09-25 17:54:57 -07007019 IWL_DEBUG_MAC80211("leave - hwcrypto disabled\n");
7020 return -EOPNOTSUPP;
7021 }
7022
7023 if (is_zero_ether_addr(addr))
7024 /* only support pairwise keys */
7025 return -EOPNOTSUPP;
7026
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007027 sta_id = iwl3945_hw_find_station(priv, addr);
Zhu Yib481de92007-09-25 17:54:57 -07007028 if (sta_id == IWL_INVALID_STATION) {
Johannes Berge1749612008-10-27 15:59:26 -07007029 IWL_DEBUG_MAC80211("leave - %pM not in station map.\n",
7030 addr);
Zhu Yib481de92007-09-25 17:54:57 -07007031 return -EINVAL;
7032 }
7033
7034 mutex_lock(&priv->mutex);
7035
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007036 iwl3945_scan_cancel_timeout(priv, 100);
Mohamed Abbas15e869d2007-10-25 17:15:46 +08007037
Zhu Yib481de92007-09-25 17:54:57 -07007038 switch (cmd) {
7039 case SET_KEY:
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007040 rc = iwl3945_update_sta_key_info(priv, key, sta_id);
Zhu Yib481de92007-09-25 17:54:57 -07007041 if (!rc) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007042 iwl3945_set_rxon_hwcrypto(priv, 1);
7043 iwl3945_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007044 key->hw_key_idx = sta_id;
7045 IWL_DEBUG_MAC80211("set_key success, using hwcrypto\n");
7046 key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
7047 }
7048 break;
7049 case DISABLE_KEY:
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007050 rc = iwl3945_clear_sta_key_info(priv, sta_id);
Zhu Yib481de92007-09-25 17:54:57 -07007051 if (!rc) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007052 iwl3945_set_rxon_hwcrypto(priv, 0);
7053 iwl3945_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007054 IWL_DEBUG_MAC80211("disable hwcrypto key\n");
7055 }
7056 break;
7057 default:
7058 rc = -EINVAL;
7059 }
7060
7061 IWL_DEBUG_MAC80211("leave\n");
7062 mutex_unlock(&priv->mutex);
7063
7064 return rc;
7065}
7066
Johannes Berge100bb62008-04-30 18:51:21 +02007067static int iwl3945_mac_conf_tx(struct ieee80211_hw *hw, u16 queue,
Zhu Yib481de92007-09-25 17:54:57 -07007068 const struct ieee80211_tx_queue_params *params)
7069{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007070 struct iwl3945_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07007071 unsigned long flags;
7072 int q;
Zhu Yib481de92007-09-25 17:54:57 -07007073
7074 IWL_DEBUG_MAC80211("enter\n");
7075
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007076 if (!iwl3945_is_ready_rf(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07007077 IWL_DEBUG_MAC80211("leave - RF not ready\n");
7078 return -EIO;
7079 }
7080
7081 if (queue >= AC_NUM) {
7082 IWL_DEBUG_MAC80211("leave - queue >= AC_NUM %d\n", queue);
7083 return 0;
7084 }
7085
Zhu Yib481de92007-09-25 17:54:57 -07007086 q = AC_NUM - 1 - queue;
7087
7088 spin_lock_irqsave(&priv->lock, flags);
7089
7090 priv->qos_data.def_qos_parm.ac[q].cw_min = cpu_to_le16(params->cw_min);
7091 priv->qos_data.def_qos_parm.ac[q].cw_max = cpu_to_le16(params->cw_max);
7092 priv->qos_data.def_qos_parm.ac[q].aifsn = params->aifs;
7093 priv->qos_data.def_qos_parm.ac[q].edca_txop =
Johannes Berg3330d7b2008-02-10 16:49:38 +01007094 cpu_to_le16((params->txop * 32));
Zhu Yib481de92007-09-25 17:54:57 -07007095
7096 priv->qos_data.def_qos_parm.ac[q].reserved1 = 0;
7097 priv->qos_data.qos_active = 1;
7098
7099 spin_unlock_irqrestore(&priv->lock, flags);
7100
7101 mutex_lock(&priv->mutex);
Johannes Berg05c914f2008-09-11 00:01:58 +02007102 if (priv->iw_mode == NL80211_IFTYPE_AP)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007103 iwl3945_activate_qos(priv, 1);
7104 else if (priv->assoc_id && iwl3945_is_associated(priv))
7105 iwl3945_activate_qos(priv, 0);
Zhu Yib481de92007-09-25 17:54:57 -07007106
7107 mutex_unlock(&priv->mutex);
7108
Zhu Yib481de92007-09-25 17:54:57 -07007109 IWL_DEBUG_MAC80211("leave\n");
7110 return 0;
7111}
7112
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007113static int iwl3945_mac_get_tx_stats(struct ieee80211_hw *hw,
Zhu Yib481de92007-09-25 17:54:57 -07007114 struct ieee80211_tx_queue_stats *stats)
7115{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007116 struct iwl3945_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07007117 int i, avail;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007118 struct iwl3945_tx_queue *txq;
7119 struct iwl3945_queue *q;
Zhu Yib481de92007-09-25 17:54:57 -07007120 unsigned long flags;
7121
7122 IWL_DEBUG_MAC80211("enter\n");
7123
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007124 if (!iwl3945_is_ready_rf(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07007125 IWL_DEBUG_MAC80211("leave - RF not ready\n");
7126 return -EIO;
7127 }
7128
7129 spin_lock_irqsave(&priv->lock, flags);
7130
7131 for (i = 0; i < AC_NUM; i++) {
7132 txq = &priv->txq[i];
7133 q = &txq->q;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007134 avail = iwl3945_queue_space(q);
Zhu Yib481de92007-09-25 17:54:57 -07007135
Johannes Berg57ffc582008-04-29 17:18:59 +02007136 stats[i].len = q->n_window - avail;
7137 stats[i].limit = q->n_window - q->high_mark;
7138 stats[i].count = q->n_window;
Zhu Yib481de92007-09-25 17:54:57 -07007139
7140 }
7141 spin_unlock_irqrestore(&priv->lock, flags);
7142
7143 IWL_DEBUG_MAC80211("leave\n");
7144
7145 return 0;
7146}
7147
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007148static int iwl3945_mac_get_stats(struct ieee80211_hw *hw,
Zhu Yib481de92007-09-25 17:54:57 -07007149 struct ieee80211_low_level_stats *stats)
7150{
7151 IWL_DEBUG_MAC80211("enter\n");
7152 IWL_DEBUG_MAC80211("leave\n");
7153
7154 return 0;
7155}
7156
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007157static void iwl3945_mac_reset_tsf(struct ieee80211_hw *hw)
Zhu Yib481de92007-09-25 17:54:57 -07007158{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007159 struct iwl3945_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07007160 unsigned long flags;
7161
7162 mutex_lock(&priv->mutex);
7163 IWL_DEBUG_MAC80211("enter\n");
7164
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007165 iwl3945_reset_qos(priv);
Ron Rindjunsky292ae172008-02-06 11:20:39 -08007166
Zhu Yib481de92007-09-25 17:54:57 -07007167 spin_lock_irqsave(&priv->lock, flags);
7168 priv->assoc_id = 0;
7169 priv->assoc_capability = 0;
7170 priv->call_post_assoc_from_beacon = 0;
7171
7172 /* new association get rid of ibss beacon skb */
7173 if (priv->ibss_beacon)
7174 dev_kfree_skb(priv->ibss_beacon);
7175
7176 priv->ibss_beacon = NULL;
7177
7178 priv->beacon_int = priv->hw->conf.beacon_int;
7179 priv->timestamp1 = 0;
7180 priv->timestamp0 = 0;
Johannes Berg05c914f2008-09-11 00:01:58 +02007181 if ((priv->iw_mode == NL80211_IFTYPE_STATION))
Zhu Yib481de92007-09-25 17:54:57 -07007182 priv->beacon_int = 0;
7183
7184 spin_unlock_irqrestore(&priv->lock, flags);
7185
Mohamed Abbasfde35712007-11-29 11:10:15 +08007186 if (!iwl3945_is_ready_rf(priv)) {
7187 IWL_DEBUG_MAC80211("leave - not ready\n");
7188 mutex_unlock(&priv->mutex);
7189 return;
7190 }
7191
Mohamed Abbas15e869d2007-10-25 17:15:46 +08007192 /* we are restarting association process
7193 * clear RXON_FILTER_ASSOC_MSK bit
7194 */
Johannes Berg05c914f2008-09-11 00:01:58 +02007195 if (priv->iw_mode != NL80211_IFTYPE_AP) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007196 iwl3945_scan_cancel_timeout(priv, 100);
Mohamed Abbas15e869d2007-10-25 17:15:46 +08007197 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007198 iwl3945_commit_rxon(priv);
Mohamed Abbas15e869d2007-10-25 17:15:46 +08007199 }
7200
Zhu Yib481de92007-09-25 17:54:57 -07007201 /* Per mac80211.h: This is only used in IBSS mode... */
Johannes Berg05c914f2008-09-11 00:01:58 +02007202 if (priv->iw_mode != NL80211_IFTYPE_ADHOC) {
Mohamed Abbas15e869d2007-10-25 17:15:46 +08007203
Zhu Yib481de92007-09-25 17:54:57 -07007204 IWL_DEBUG_MAC80211("leave - not in IBSS\n");
7205 mutex_unlock(&priv->mutex);
7206 return;
7207 }
7208
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007209 iwl3945_set_rate(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007210
7211 mutex_unlock(&priv->mutex);
7212
7213 IWL_DEBUG_MAC80211("leave\n");
7214
7215}
7216
Johannes Berge039fa42008-05-15 12:55:29 +02007217static int iwl3945_mac_beacon_update(struct ieee80211_hw *hw, struct sk_buff *skb)
Zhu Yib481de92007-09-25 17:54:57 -07007218{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007219 struct iwl3945_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07007220 unsigned long flags;
7221
Zhu Yib481de92007-09-25 17:54:57 -07007222 IWL_DEBUG_MAC80211("enter\n");
7223
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007224 if (!iwl3945_is_ready_rf(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07007225 IWL_DEBUG_MAC80211("leave - RF not ready\n");
Zhu Yib481de92007-09-25 17:54:57 -07007226 return -EIO;
7227 }
7228
Johannes Berg05c914f2008-09-11 00:01:58 +02007229 if (priv->iw_mode != NL80211_IFTYPE_ADHOC) {
Zhu Yib481de92007-09-25 17:54:57 -07007230 IWL_DEBUG_MAC80211("leave - not IBSS\n");
Zhu Yib481de92007-09-25 17:54:57 -07007231 return -EIO;
7232 }
7233
7234 spin_lock_irqsave(&priv->lock, flags);
7235
7236 if (priv->ibss_beacon)
7237 dev_kfree_skb(priv->ibss_beacon);
7238
7239 priv->ibss_beacon = skb;
7240
7241 priv->assoc_id = 0;
7242
7243 IWL_DEBUG_MAC80211("leave\n");
7244 spin_unlock_irqrestore(&priv->lock, flags);
7245
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007246 iwl3945_reset_qos(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007247
Abhijeet Kolekardc4b1e72008-09-03 11:26:30 +08007248 iwl3945_post_associate(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007249
Zhu Yib481de92007-09-25 17:54:57 -07007250
7251 return 0;
7252}
7253
7254/*****************************************************************************
7255 *
7256 * sysfs attributes
7257 *
7258 *****************************************************************************/
7259
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08007260#ifdef CONFIG_IWL3945_DEBUG
Zhu Yib481de92007-09-25 17:54:57 -07007261
7262/*
7263 * The following adds a new attribute to the sysfs representation
7264 * of this device driver (i.e. a new file in /sys/bus/pci/drivers/iwl/)
7265 * used for controlling the debug level.
7266 *
7267 * See the level definitions in iwl for details.
7268 */
7269
7270static ssize_t show_debug_level(struct device_driver *d, char *buf)
7271{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007272 return sprintf(buf, "0x%08X\n", iwl3945_debug_level);
Zhu Yib481de92007-09-25 17:54:57 -07007273}
7274static ssize_t store_debug_level(struct device_driver *d,
7275 const char *buf, size_t count)
7276{
7277 char *p = (char *)buf;
7278 u32 val;
7279
7280 val = simple_strtoul(p, &p, 0);
7281 if (p == buf)
7282 printk(KERN_INFO DRV_NAME
7283 ": %s is not in hex or decimal form.\n", buf);
7284 else
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007285 iwl3945_debug_level = val;
Zhu Yib481de92007-09-25 17:54:57 -07007286
7287 return strnlen(buf, count);
7288}
7289
7290static DRIVER_ATTR(debug_level, S_IWUSR | S_IRUGO,
7291 show_debug_level, store_debug_level);
7292
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08007293#endif /* CONFIG_IWL3945_DEBUG */
Zhu Yib481de92007-09-25 17:54:57 -07007294
Zhu Yib481de92007-09-25 17:54:57 -07007295static ssize_t show_temperature(struct device *d,
7296 struct device_attribute *attr, char *buf)
7297{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007298 struct iwl3945_priv *priv = (struct iwl3945_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07007299
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007300 if (!iwl3945_is_alive(priv))
Zhu Yib481de92007-09-25 17:54:57 -07007301 return -EAGAIN;
7302
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007303 return sprintf(buf, "%d\n", iwl3945_hw_get_temperature(priv));
Zhu Yib481de92007-09-25 17:54:57 -07007304}
7305
7306static DEVICE_ATTR(temperature, S_IRUGO, show_temperature, NULL);
7307
Zhu Yib481de92007-09-25 17:54:57 -07007308static ssize_t show_tx_power(struct device *d,
7309 struct device_attribute *attr, char *buf)
7310{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007311 struct iwl3945_priv *priv = (struct iwl3945_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07007312 return sprintf(buf, "%d\n", priv->user_txpower_limit);
7313}
7314
7315static ssize_t store_tx_power(struct device *d,
7316 struct device_attribute *attr,
7317 const char *buf, size_t count)
7318{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007319 struct iwl3945_priv *priv = (struct iwl3945_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07007320 char *p = (char *)buf;
7321 u32 val;
7322
7323 val = simple_strtoul(p, &p, 10);
7324 if (p == buf)
7325 printk(KERN_INFO DRV_NAME
7326 ": %s is not in decimal form.\n", buf);
7327 else
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007328 iwl3945_hw_reg_set_txpower(priv, val);
Zhu Yib481de92007-09-25 17:54:57 -07007329
7330 return count;
7331}
7332
7333static DEVICE_ATTR(tx_power, S_IWUSR | S_IRUGO, show_tx_power, store_tx_power);
7334
7335static ssize_t show_flags(struct device *d,
7336 struct device_attribute *attr, char *buf)
7337{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007338 struct iwl3945_priv *priv = (struct iwl3945_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07007339
7340 return sprintf(buf, "0x%04X\n", priv->active_rxon.flags);
7341}
7342
7343static ssize_t store_flags(struct device *d,
7344 struct device_attribute *attr,
7345 const char *buf, size_t count)
7346{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007347 struct iwl3945_priv *priv = (struct iwl3945_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07007348 u32 flags = simple_strtoul(buf, NULL, 0);
7349
7350 mutex_lock(&priv->mutex);
7351 if (le32_to_cpu(priv->staging_rxon.flags) != flags) {
7352 /* Cancel any currently running scans... */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007353 if (iwl3945_scan_cancel_timeout(priv, 100))
Zhu Yib481de92007-09-25 17:54:57 -07007354 IWL_WARNING("Could not cancel scan.\n");
7355 else {
7356 IWL_DEBUG_INFO("Committing rxon.flags = 0x%04X\n",
7357 flags);
7358 priv->staging_rxon.flags = cpu_to_le32(flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007359 iwl3945_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007360 }
7361 }
7362 mutex_unlock(&priv->mutex);
7363
7364 return count;
7365}
7366
7367static DEVICE_ATTR(flags, S_IWUSR | S_IRUGO, show_flags, store_flags);
7368
7369static ssize_t show_filter_flags(struct device *d,
7370 struct device_attribute *attr, char *buf)
7371{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007372 struct iwl3945_priv *priv = (struct iwl3945_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07007373
7374 return sprintf(buf, "0x%04X\n",
7375 le32_to_cpu(priv->active_rxon.filter_flags));
7376}
7377
7378static ssize_t store_filter_flags(struct device *d,
7379 struct device_attribute *attr,
7380 const char *buf, size_t count)
7381{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007382 struct iwl3945_priv *priv = (struct iwl3945_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07007383 u32 filter_flags = simple_strtoul(buf, NULL, 0);
7384
7385 mutex_lock(&priv->mutex);
7386 if (le32_to_cpu(priv->staging_rxon.filter_flags) != filter_flags) {
7387 /* Cancel any currently running scans... */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007388 if (iwl3945_scan_cancel_timeout(priv, 100))
Zhu Yib481de92007-09-25 17:54:57 -07007389 IWL_WARNING("Could not cancel scan.\n");
7390 else {
7391 IWL_DEBUG_INFO("Committing rxon.filter_flags = "
7392 "0x%04X\n", filter_flags);
7393 priv->staging_rxon.filter_flags =
7394 cpu_to_le32(filter_flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007395 iwl3945_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007396 }
7397 }
7398 mutex_unlock(&priv->mutex);
7399
7400 return count;
7401}
7402
7403static DEVICE_ATTR(filter_flags, S_IWUSR | S_IRUGO, show_filter_flags,
7404 store_filter_flags);
7405
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08007406#ifdef CONFIG_IWL3945_SPECTRUM_MEASUREMENT
Zhu Yib481de92007-09-25 17:54:57 -07007407
7408static ssize_t show_measurement(struct device *d,
7409 struct device_attribute *attr, char *buf)
7410{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007411 struct iwl3945_priv *priv = dev_get_drvdata(d);
7412 struct iwl3945_spectrum_notification measure_report;
Zhu Yib481de92007-09-25 17:54:57 -07007413 u32 size = sizeof(measure_report), len = 0, ofs = 0;
Tomas Winkler3ac7f142008-07-21 02:40:14 +03007414 u8 *data = (u8 *)&measure_report;
Zhu Yib481de92007-09-25 17:54:57 -07007415 unsigned long flags;
7416
7417 spin_lock_irqsave(&priv->lock, flags);
7418 if (!(priv->measurement_status & MEASUREMENT_READY)) {
7419 spin_unlock_irqrestore(&priv->lock, flags);
7420 return 0;
7421 }
7422 memcpy(&measure_report, &priv->measure_report, size);
7423 priv->measurement_status = 0;
7424 spin_unlock_irqrestore(&priv->lock, flags);
7425
7426 while (size && (PAGE_SIZE - len)) {
7427 hex_dump_to_buffer(data + ofs, size, 16, 1, buf + len,
7428 PAGE_SIZE - len, 1);
7429 len = strlen(buf);
7430 if (PAGE_SIZE - len)
7431 buf[len++] = '\n';
7432
7433 ofs += 16;
7434 size -= min(size, 16U);
7435 }
7436
7437 return len;
7438}
7439
7440static ssize_t store_measurement(struct device *d,
7441 struct device_attribute *attr,
7442 const char *buf, size_t count)
7443{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007444 struct iwl3945_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07007445 struct ieee80211_measurement_params params = {
7446 .channel = le16_to_cpu(priv->active_rxon.channel),
7447 .start_time = cpu_to_le64(priv->last_tsf),
7448 .duration = cpu_to_le16(1),
7449 };
7450 u8 type = IWL_MEASURE_BASIC;
7451 u8 buffer[32];
7452 u8 channel;
7453
7454 if (count) {
7455 char *p = buffer;
7456 strncpy(buffer, buf, min(sizeof(buffer), count));
7457 channel = simple_strtoul(p, NULL, 0);
7458 if (channel)
7459 params.channel = channel;
7460
7461 p = buffer;
7462 while (*p && *p != ' ')
7463 p++;
7464 if (*p)
7465 type = simple_strtoul(p + 1, NULL, 0);
7466 }
7467
7468 IWL_DEBUG_INFO("Invoking measurement of type %d on "
7469 "channel %d (for '%s')\n", type, params.channel, buf);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007470 iwl3945_get_measurement(priv, &params, type);
Zhu Yib481de92007-09-25 17:54:57 -07007471
7472 return count;
7473}
7474
7475static DEVICE_ATTR(measurement, S_IRUSR | S_IWUSR,
7476 show_measurement, store_measurement);
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08007477#endif /* CONFIG_IWL3945_SPECTRUM_MEASUREMENT */
Zhu Yib481de92007-09-25 17:54:57 -07007478
Zhu Yib481de92007-09-25 17:54:57 -07007479static ssize_t store_retry_rate(struct device *d,
7480 struct device_attribute *attr,
7481 const char *buf, size_t count)
7482{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007483 struct iwl3945_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07007484
7485 priv->retry_rate = simple_strtoul(buf, NULL, 0);
7486 if (priv->retry_rate <= 0)
7487 priv->retry_rate = 1;
7488
7489 return count;
7490}
7491
7492static ssize_t show_retry_rate(struct device *d,
7493 struct device_attribute *attr, char *buf)
7494{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007495 struct iwl3945_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07007496 return sprintf(buf, "%d", priv->retry_rate);
7497}
7498
7499static DEVICE_ATTR(retry_rate, S_IWUSR | S_IRUSR, show_retry_rate,
7500 store_retry_rate);
7501
7502static ssize_t store_power_level(struct device *d,
7503 struct device_attribute *attr,
7504 const char *buf, size_t count)
7505{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007506 struct iwl3945_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07007507 int rc;
7508 int mode;
7509
7510 mode = simple_strtoul(buf, NULL, 0);
7511 mutex_lock(&priv->mutex);
7512
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007513 if (!iwl3945_is_ready(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07007514 rc = -EAGAIN;
7515 goto out;
7516 }
7517
7518 if ((mode < 1) || (mode > IWL_POWER_LIMIT) || (mode == IWL_POWER_AC))
7519 mode = IWL_POWER_AC;
7520 else
7521 mode |= IWL_POWER_ENABLED;
7522
7523 if (mode != priv->power_mode) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007524 rc = iwl3945_send_power_mode(priv, IWL_POWER_LEVEL(mode));
Zhu Yib481de92007-09-25 17:54:57 -07007525 if (rc) {
7526 IWL_DEBUG_MAC80211("failed setting power mode.\n");
7527 goto out;
7528 }
7529 priv->power_mode = mode;
7530 }
7531
7532 rc = count;
7533
7534 out:
7535 mutex_unlock(&priv->mutex);
7536 return rc;
7537}
7538
7539#define MAX_WX_STRING 80
7540
7541/* Values are in microsecond */
7542static const s32 timeout_duration[] = {
7543 350000,
7544 250000,
7545 75000,
7546 37000,
7547 25000,
7548};
7549static const s32 period_duration[] = {
7550 400000,
7551 700000,
7552 1000000,
7553 1000000,
7554 1000000
7555};
7556
7557static ssize_t show_power_level(struct device *d,
7558 struct device_attribute *attr, char *buf)
7559{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007560 struct iwl3945_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07007561 int level = IWL_POWER_LEVEL(priv->power_mode);
7562 char *p = buf;
7563
7564 p += sprintf(p, "%d ", level);
7565 switch (level) {
7566 case IWL_POWER_MODE_CAM:
7567 case IWL_POWER_AC:
7568 p += sprintf(p, "(AC)");
7569 break;
7570 case IWL_POWER_BATTERY:
7571 p += sprintf(p, "(BATTERY)");
7572 break;
7573 default:
7574 p += sprintf(p,
7575 "(Timeout %dms, Period %dms)",
7576 timeout_duration[level - 1] / 1000,
7577 period_duration[level - 1] / 1000);
7578 }
7579
7580 if (!(priv->power_mode & IWL_POWER_ENABLED))
7581 p += sprintf(p, " OFF\n");
7582 else
7583 p += sprintf(p, " \n");
7584
Tomas Winkler3ac7f142008-07-21 02:40:14 +03007585 return p - buf + 1;
Zhu Yib481de92007-09-25 17:54:57 -07007586
7587}
7588
7589static DEVICE_ATTR(power_level, S_IWUSR | S_IRUSR, show_power_level,
7590 store_power_level);
7591
7592static ssize_t show_channels(struct device *d,
7593 struct device_attribute *attr, char *buf)
7594{
Johannes Berg8318d782008-01-24 19:38:38 +01007595 /* all this shit doesn't belong into sysfs anyway */
7596 return 0;
Zhu Yib481de92007-09-25 17:54:57 -07007597}
7598
7599static DEVICE_ATTR(channels, S_IRUSR, show_channels, NULL);
7600
7601static ssize_t show_statistics(struct device *d,
7602 struct device_attribute *attr, char *buf)
7603{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007604 struct iwl3945_priv *priv = dev_get_drvdata(d);
7605 u32 size = sizeof(struct iwl3945_notif_statistics);
Zhu Yib481de92007-09-25 17:54:57 -07007606 u32 len = 0, ofs = 0;
Tomas Winkler3ac7f142008-07-21 02:40:14 +03007607 u8 *data = (u8 *)&priv->statistics;
Zhu Yib481de92007-09-25 17:54:57 -07007608 int rc = 0;
7609
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007610 if (!iwl3945_is_alive(priv))
Zhu Yib481de92007-09-25 17:54:57 -07007611 return -EAGAIN;
7612
7613 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007614 rc = iwl3945_send_statistics_request(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007615 mutex_unlock(&priv->mutex);
7616
7617 if (rc) {
7618 len = sprintf(buf,
7619 "Error sending statistics request: 0x%08X\n", rc);
7620 return len;
7621 }
7622
7623 while (size && (PAGE_SIZE - len)) {
7624 hex_dump_to_buffer(data + ofs, size, 16, 1, buf + len,
7625 PAGE_SIZE - len, 1);
7626 len = strlen(buf);
7627 if (PAGE_SIZE - len)
7628 buf[len++] = '\n';
7629
7630 ofs += 16;
7631 size -= min(size, 16U);
7632 }
7633
7634 return len;
7635}
7636
7637static DEVICE_ATTR(statistics, S_IRUGO, show_statistics, NULL);
7638
7639static ssize_t show_antenna(struct device *d,
7640 struct device_attribute *attr, char *buf)
7641{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007642 struct iwl3945_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07007643
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007644 if (!iwl3945_is_alive(priv))
Zhu Yib481de92007-09-25 17:54:57 -07007645 return -EAGAIN;
7646
7647 return sprintf(buf, "%d\n", priv->antenna);
7648}
7649
7650static ssize_t store_antenna(struct device *d,
7651 struct device_attribute *attr,
7652 const char *buf, size_t count)
7653{
7654 int ant;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007655 struct iwl3945_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07007656
7657 if (count == 0)
7658 return 0;
7659
7660 if (sscanf(buf, "%1i", &ant) != 1) {
7661 IWL_DEBUG_INFO("not in hex or decimal form.\n");
7662 return count;
7663 }
7664
7665 if ((ant >= 0) && (ant <= 2)) {
7666 IWL_DEBUG_INFO("Setting antenna select to %d.\n", ant);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007667 priv->antenna = (enum iwl3945_antenna)ant;
Zhu Yib481de92007-09-25 17:54:57 -07007668 } else
7669 IWL_DEBUG_INFO("Bad antenna select value %d.\n", ant);
7670
7671
7672 return count;
7673}
7674
7675static DEVICE_ATTR(antenna, S_IWUSR | S_IRUGO, show_antenna, store_antenna);
7676
7677static ssize_t show_status(struct device *d,
7678 struct device_attribute *attr, char *buf)
7679{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007680 struct iwl3945_priv *priv = (struct iwl3945_priv *)d->driver_data;
7681 if (!iwl3945_is_alive(priv))
Zhu Yib481de92007-09-25 17:54:57 -07007682 return -EAGAIN;
7683 return sprintf(buf, "0x%08x\n", (int)priv->status);
7684}
7685
7686static DEVICE_ATTR(status, S_IRUGO, show_status, NULL);
7687
7688static ssize_t dump_error_log(struct device *d,
7689 struct device_attribute *attr,
7690 const char *buf, size_t count)
7691{
7692 char *p = (char *)buf;
7693
7694 if (p[0] == '1')
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007695 iwl3945_dump_nic_error_log((struct iwl3945_priv *)d->driver_data);
Zhu Yib481de92007-09-25 17:54:57 -07007696
7697 return strnlen(buf, count);
7698}
7699
7700static DEVICE_ATTR(dump_errors, S_IWUSR, NULL, dump_error_log);
7701
7702static ssize_t dump_event_log(struct device *d,
7703 struct device_attribute *attr,
7704 const char *buf, size_t count)
7705{
7706 char *p = (char *)buf;
7707
7708 if (p[0] == '1')
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007709 iwl3945_dump_nic_event_log((struct iwl3945_priv *)d->driver_data);
Zhu Yib481de92007-09-25 17:54:57 -07007710
7711 return strnlen(buf, count);
7712}
7713
7714static DEVICE_ATTR(dump_events, S_IWUSR, NULL, dump_event_log);
7715
7716/*****************************************************************************
7717 *
Tomas Winklera96a27f2008-10-23 23:48:56 -07007718 * driver setup and tear down
Zhu Yib481de92007-09-25 17:54:57 -07007719 *
7720 *****************************************************************************/
7721
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007722static void iwl3945_setup_deferred_work(struct iwl3945_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07007723{
7724 priv->workqueue = create_workqueue(DRV_NAME);
7725
7726 init_waitqueue_head(&priv->wait_command_queue);
7727
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007728 INIT_WORK(&priv->up, iwl3945_bg_up);
7729 INIT_WORK(&priv->restart, iwl3945_bg_restart);
7730 INIT_WORK(&priv->rx_replenish, iwl3945_bg_rx_replenish);
7731 INIT_WORK(&priv->scan_completed, iwl3945_bg_scan_completed);
7732 INIT_WORK(&priv->request_scan, iwl3945_bg_request_scan);
7733 INIT_WORK(&priv->abort_scan, iwl3945_bg_abort_scan);
7734 INIT_WORK(&priv->rf_kill, iwl3945_bg_rf_kill);
7735 INIT_WORK(&priv->beacon_update, iwl3945_bg_beacon_update);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007736 INIT_DELAYED_WORK(&priv->init_alive_start, iwl3945_bg_init_alive_start);
7737 INIT_DELAYED_WORK(&priv->alive_start, iwl3945_bg_alive_start);
7738 INIT_DELAYED_WORK(&priv->scan_check, iwl3945_bg_scan_check);
Zhu Yib481de92007-09-25 17:54:57 -07007739
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007740 iwl3945_hw_setup_deferred_work(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007741
7742 tasklet_init(&priv->irq_tasklet, (void (*)(unsigned long))
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007743 iwl3945_irq_tasklet, (unsigned long)priv);
Zhu Yib481de92007-09-25 17:54:57 -07007744}
7745
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007746static void iwl3945_cancel_deferred_work(struct iwl3945_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07007747{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007748 iwl3945_hw_cancel_deferred_work(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007749
Joonwoo Parke47eb6a2007-11-29 10:42:49 +09007750 cancel_delayed_work_sync(&priv->init_alive_start);
Zhu Yib481de92007-09-25 17:54:57 -07007751 cancel_delayed_work(&priv->scan_check);
7752 cancel_delayed_work(&priv->alive_start);
Zhu Yib481de92007-09-25 17:54:57 -07007753 cancel_work_sync(&priv->beacon_update);
7754}
7755
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007756static struct attribute *iwl3945_sysfs_entries[] = {
Zhu Yib481de92007-09-25 17:54:57 -07007757 &dev_attr_antenna.attr,
7758 &dev_attr_channels.attr,
7759 &dev_attr_dump_errors.attr,
7760 &dev_attr_dump_events.attr,
7761 &dev_attr_flags.attr,
7762 &dev_attr_filter_flags.attr,
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08007763#ifdef CONFIG_IWL3945_SPECTRUM_MEASUREMENT
Zhu Yib481de92007-09-25 17:54:57 -07007764 &dev_attr_measurement.attr,
7765#endif
7766 &dev_attr_power_level.attr,
Zhu Yib481de92007-09-25 17:54:57 -07007767 &dev_attr_retry_rate.attr,
Zhu Yib481de92007-09-25 17:54:57 -07007768 &dev_attr_statistics.attr,
7769 &dev_attr_status.attr,
7770 &dev_attr_temperature.attr,
Zhu Yib481de92007-09-25 17:54:57 -07007771 &dev_attr_tx_power.attr,
7772
7773 NULL
7774};
7775
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007776static struct attribute_group iwl3945_attribute_group = {
Zhu Yib481de92007-09-25 17:54:57 -07007777 .name = NULL, /* put in device directory */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007778 .attrs = iwl3945_sysfs_entries,
Zhu Yib481de92007-09-25 17:54:57 -07007779};
7780
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007781static struct ieee80211_ops iwl3945_hw_ops = {
7782 .tx = iwl3945_mac_tx,
7783 .start = iwl3945_mac_start,
7784 .stop = iwl3945_mac_stop,
7785 .add_interface = iwl3945_mac_add_interface,
7786 .remove_interface = iwl3945_mac_remove_interface,
7787 .config = iwl3945_mac_config,
7788 .config_interface = iwl3945_mac_config_interface,
7789 .configure_filter = iwl3945_configure_filter,
7790 .set_key = iwl3945_mac_set_key,
7791 .get_stats = iwl3945_mac_get_stats,
7792 .get_tx_stats = iwl3945_mac_get_tx_stats,
7793 .conf_tx = iwl3945_mac_conf_tx,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007794 .reset_tsf = iwl3945_mac_reset_tsf,
Abhijeet Kolekarcd56d332008-09-03 11:26:21 +08007795 .bss_info_changed = iwl3945_bss_info_changed,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007796 .hw_scan = iwl3945_mac_hw_scan
Zhu Yib481de92007-09-25 17:54:57 -07007797};
7798
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007799static int iwl3945_pci_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
Zhu Yib481de92007-09-25 17:54:57 -07007800{
7801 int err = 0;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007802 struct iwl3945_priv *priv;
Zhu Yib481de92007-09-25 17:54:57 -07007803 struct ieee80211_hw *hw;
Tomas Winkler82b9a122008-03-04 18:09:30 -08007804 struct iwl_3945_cfg *cfg = (struct iwl_3945_cfg *)(ent->driver_data);
Mohamed Abbas0359fac2008-03-28 16:21:08 -07007805 unsigned long flags;
Zhu Yib481de92007-09-25 17:54:57 -07007806
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08007807 /***********************
7808 * 1. Allocating HW data
7809 * ********************/
7810
Cahill, Ben M6440adb2007-11-29 11:09:55 +08007811 /* Disabling hardware scan means that mac80211 will perform scans
7812 * "the hard way", rather than using device's scan. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007813 if (iwl3945_param_disable_hw_scan) {
Zhu Yib481de92007-09-25 17:54:57 -07007814 IWL_DEBUG_INFO("Disabling hw_scan\n");
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007815 iwl3945_hw_ops.hw_scan = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07007816 }
7817
Ron Rindjunskydfe7d452008-04-15 16:01:45 -07007818 if ((iwl3945_param_queues_num > IWL39_MAX_NUM_QUEUES) ||
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007819 (iwl3945_param_queues_num < IWL_MIN_NUM_QUEUES)) {
Zhu Yib481de92007-09-25 17:54:57 -07007820 IWL_ERROR("invalid queues_num, should be between %d and %d\n",
Ron Rindjunskydfe7d452008-04-15 16:01:45 -07007821 IWL_MIN_NUM_QUEUES, IWL39_MAX_NUM_QUEUES);
Zhu Yib481de92007-09-25 17:54:57 -07007822 err = -EINVAL;
7823 goto out;
7824 }
7825
7826 /* mac80211 allocates memory for this device instance, including
7827 * space for this driver's private structure */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007828 hw = ieee80211_alloc_hw(sizeof(struct iwl3945_priv), &iwl3945_hw_ops);
Zhu Yib481de92007-09-25 17:54:57 -07007829 if (hw == NULL) {
7830 IWL_ERROR("Can not allocate network device\n");
7831 err = -ENOMEM;
7832 goto out;
7833 }
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08007834
Zhu Yib481de92007-09-25 17:54:57 -07007835 SET_IEEE80211_DEV(hw, &pdev->dev);
7836
Zhu Yib481de92007-09-25 17:54:57 -07007837 priv = hw->priv;
7838 priv->hw = hw;
Zhu Yib481de92007-09-25 17:54:57 -07007839 priv->pci_dev = pdev;
Tomas Winkler82b9a122008-03-04 18:09:30 -08007840 priv->cfg = cfg;
Cahill, Ben M6440adb2007-11-29 11:09:55 +08007841
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08007842 IWL_DEBUG_INFO("*** LOAD DRIVER ***\n");
7843 hw->rate_control_algorithm = "iwl-3945-rs";
7844 hw->sta_data_size = sizeof(struct iwl3945_sta_priv);
7845
Cahill, Ben M6440adb2007-11-29 11:09:55 +08007846 /* Select antenna (may be helpful if only one antenna is connected) */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007847 priv->antenna = (enum iwl3945_antenna)iwl3945_param_antenna;
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08007848#ifdef CONFIG_IWL3945_DEBUG
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007849 iwl3945_debug_level = iwl3945_param_debug;
Zhu Yib481de92007-09-25 17:54:57 -07007850 atomic_set(&priv->restrict_refcnt, 0);
7851#endif
Zhu Yib481de92007-09-25 17:54:57 -07007852
Bruno Randolf566bfe52008-05-08 19:15:40 +02007853 /* Tell mac80211 our characteristics */
Johannes Berg605a0bd2008-07-15 10:10:01 +02007854 hw->flags = IEEE80211_HW_SIGNAL_DBM |
Bruno Randolf566bfe52008-05-08 19:15:40 +02007855 IEEE80211_HW_NOISE_DBM;
Zhu Yib481de92007-09-25 17:54:57 -07007856
Luis R. Rodriguezf59ac042008-08-29 16:26:43 -07007857 hw->wiphy->interface_modes =
Luis R. Rodriguezf59ac042008-08-29 16:26:43 -07007858 BIT(NL80211_IFTYPE_STATION) |
7859 BIT(NL80211_IFTYPE_ADHOC);
7860
Luis R. Rodriguezea4a82dc2008-11-12 14:22:04 -08007861 hw->wiphy->fw_handles_regulatory = true;
7862
Cahill, Ben M6440adb2007-11-29 11:09:55 +08007863 /* 4 EDCA QOS priorities */
Zhu Yib481de92007-09-25 17:54:57 -07007864 hw->queues = 4;
7865
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08007866 /***************************
7867 * 2. Initializing PCI bus
7868 * *************************/
Zhu Yib481de92007-09-25 17:54:57 -07007869 if (pci_enable_device(pdev)) {
7870 err = -ENODEV;
7871 goto out_ieee80211_free_hw;
7872 }
7873
7874 pci_set_master(pdev);
7875
Zhu Yib481de92007-09-25 17:54:57 -07007876 err = pci_set_dma_mask(pdev, DMA_32BIT_MASK);
7877 if (!err)
7878 err = pci_set_consistent_dma_mask(pdev, DMA_32BIT_MASK);
7879 if (err) {
7880 printk(KERN_WARNING DRV_NAME ": No suitable DMA available.\n");
7881 goto out_pci_disable_device;
7882 }
7883
7884 pci_set_drvdata(pdev, priv);
7885 err = pci_request_regions(pdev, DRV_NAME);
7886 if (err)
7887 goto out_pci_disable_device;
Cahill, Ben M6440adb2007-11-29 11:09:55 +08007888
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08007889 /***********************
7890 * 3. Read REV Register
7891 * ********************/
Zhu Yib481de92007-09-25 17:54:57 -07007892 priv->hw_base = pci_iomap(pdev, 0, 0);
7893 if (!priv->hw_base) {
7894 err = -ENODEV;
7895 goto out_pci_release_regions;
7896 }
7897
7898 IWL_DEBUG_INFO("pci_resource_len = 0x%08llx\n",
7899 (unsigned long long) pci_resource_len(pdev, 0));
7900 IWL_DEBUG_INFO("pci_resource_base = %p\n", priv->hw_base);
7901
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08007902 /* We disable the RETRY_TIMEOUT register (0x41) to keep
7903 * PCI Tx retries from interfering with C3 CPU state */
7904 pci_write_config_byte(pdev, 0x41, 0x00);
Zhu Yib481de92007-09-25 17:54:57 -07007905
Zhu Yi5a669262008-01-14 17:46:18 -08007906 /* nic init */
7907 iwl3945_set_bit(priv, CSR_GIO_CHICKEN_BITS,
Tomas Winkler3ac7f142008-07-21 02:40:14 +03007908 CSR_GIO_CHICKEN_BITS_REG_BIT_DIS_L0S_EXIT_TIMER);
Zhu Yi5a669262008-01-14 17:46:18 -08007909
Tomas Winkler3ac7f142008-07-21 02:40:14 +03007910 iwl3945_set_bit(priv, CSR_GP_CNTRL, CSR_GP_CNTRL_REG_FLAG_INIT_DONE);
Zhu, Yi73d7b5a2008-12-05 07:58:40 -08007911 err = iwl3945_poll_direct_bit(priv, CSR_GP_CNTRL,
7912 CSR_GP_CNTRL_REG_FLAG_MAC_CLOCK_READY, 25000);
Tomas Winkler3ac7f142008-07-21 02:40:14 +03007913 if (err < 0) {
7914 IWL_DEBUG_INFO("Failed to init the card\n");
Zhu Yi5a669262008-01-14 17:46:18 -08007915 goto out_remove_sysfs;
Tomas Winkler3ac7f142008-07-21 02:40:14 +03007916 }
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08007917
7918 /***********************
7919 * 4. Read EEPROM
7920 * ********************/
Zhu Yi5a669262008-01-14 17:46:18 -08007921 /* Read the EEPROM */
7922 err = iwl3945_eeprom_init(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007923 if (err) {
Zhu Yi5a669262008-01-14 17:46:18 -08007924 IWL_ERROR("Unable to init EEPROM\n");
7925 goto out_remove_sysfs;
7926 }
7927 /* MAC Address location in EEPROM same for 3945/4965 */
7928 get_eeprom_mac(priv, priv->mac_addr);
Johannes Berge1749612008-10-27 15:59:26 -07007929 IWL_DEBUG_INFO("MAC address: %pM\n", priv->mac_addr);
Zhu Yi5a669262008-01-14 17:46:18 -08007930 SET_IEEE80211_PERM_ADDR(priv->hw, priv->mac_addr);
7931
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08007932 /***********************
7933 * 5. Setup HW Constants
7934 * ********************/
7935 /* Device-specific setup */
7936 if (iwl3945_hw_set_hw_setting(priv)) {
7937 IWL_ERROR("failed to set hw settings\n");
7938 goto out_iounmap;
7939 }
7940
7941 /***********************
7942 * 6. Setup priv
7943 * ********************/
7944 priv->retry_rate = 1;
7945 priv->ibss_beacon = NULL;
7946
7947 spin_lock_init(&priv->lock);
7948 spin_lock_init(&priv->power_data.lock);
7949 spin_lock_init(&priv->sta_lock);
7950 spin_lock_init(&priv->hcmd_lock);
7951
7952 INIT_LIST_HEAD(&priv->free_frames);
7953 mutex_init(&priv->mutex);
7954
7955 /* Clear the driver's (not device's) station table */
7956 iwl3945_clear_stations_table(priv);
7957
7958 priv->data_retry_limit = -1;
7959 priv->ieee_channels = NULL;
7960 priv->ieee_rates = NULL;
7961 priv->band = IEEE80211_BAND_2GHZ;
7962
7963 priv->iw_mode = NL80211_IFTYPE_STATION;
7964
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08007965 iwl3945_reset_qos(priv);
7966
7967 priv->qos_data.qos_active = 0;
7968 priv->qos_data.qos_cap.val = 0;
7969
7970
7971 priv->rates_mask = IWL_RATES_MASK;
7972 /* If power management is turned on, default to AC mode */
7973 priv->power_mode = IWL_POWER_AC;
7974 priv->user_txpower_limit = IWL_DEFAULT_TX_POWER;
7975
Reinette Chatre849e0dc2008-01-23 10:15:18 -08007976 err = iwl3945_init_channel_map(priv);
7977 if (err) {
7978 IWL_ERROR("initializing regulatory failed: %d\n", err);
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08007979 goto out_release_irq;
Reinette Chatre849e0dc2008-01-23 10:15:18 -08007980 }
7981
7982 err = iwl3945_init_geos(priv);
7983 if (err) {
7984 IWL_ERROR("initializing geos failed: %d\n", err);
7985 goto out_free_channel_map;
7986 }
Reinette Chatre849e0dc2008-01-23 10:15:18 -08007987
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08007988 printk(KERN_INFO DRV_NAME
7989 ": Detected Intel Wireless WiFi Link %s\n", priv->cfg->name);
7990
7991 /***********************************
7992 * 7. Initialize Module Parameters
7993 * **********************************/
7994
7995 /* Initialize module parameter values here */
7996 /* Disable radio (SW RF KILL) via parameter when loading driver */
7997 if (iwl3945_param_disable) {
7998 set_bit(STATUS_RF_KILL_SW, &priv->status);
7999 IWL_DEBUG_INFO("Radio disabled.\n");
8000 }
8001
8002
8003 /***********************
8004 * 8. Setup Services
8005 * ********************/
8006
8007 spin_lock_irqsave(&priv->lock, flags);
8008 iwl3945_disable_interrupts(priv);
8009 spin_unlock_irqrestore(&priv->lock, flags);
8010
8011 err = sysfs_create_group(&pdev->dev.kobj, &iwl3945_attribute_group);
8012 if (err) {
8013 IWL_ERROR("failed to create sysfs device attributes\n");
8014 goto out_free_geos;
8015 }
8016
8017 iwl3945_set_rxon_channel(priv, IEEE80211_BAND_2GHZ, 6);
8018 iwl3945_setup_deferred_work(priv);
8019 iwl3945_setup_rx_handlers(priv);
8020
8021 /***********************
8022 * 9. Conclude
8023 * ********************/
8024 pci_save_state(pdev);
8025 pci_disable_device(pdev);
8026
8027 /*********************************
8028 * 10. Setup and Register mac80211
8029 * *******************************/
8030
Zhu Yi5a669262008-01-14 17:46:18 -08008031 err = ieee80211_register_hw(priv->hw);
8032 if (err) {
8033 IWL_ERROR("Failed to register network device (error %d)\n", err);
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08008034 goto out_remove_sysfs;
Zhu Yib481de92007-09-25 17:54:57 -07008035 }
8036
Zhu Yi5a669262008-01-14 17:46:18 -08008037 priv->hw->conf.beacon_int = 100;
8038 priv->mac80211_registered = 1;
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08008039
Zhu Yib481de92007-09-25 17:54:57 -07008040
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08008041 err = iwl3945_rfkill_init(priv);
8042 if (err)
8043 IWL_ERROR("Unable to initialize RFKILL system. "
8044 "Ignoring error: %d\n", err);
8045
Zhu Yib481de92007-09-25 17:54:57 -07008046 return 0;
8047
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08008048 out_remove_sysfs:
8049 sysfs_remove_group(&pdev->dev.kobj, &iwl3945_attribute_group);
Reinette Chatre849e0dc2008-01-23 10:15:18 -08008050 out_free_geos:
8051 iwl3945_free_geos(priv);
8052 out_free_channel_map:
8053 iwl3945_free_channel_map(priv);
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08008054
Zhu Yib481de92007-09-25 17:54:57 -07008055
8056 out_release_irq:
Zhu Yib481de92007-09-25 17:54:57 -07008057 destroy_workqueue(priv->workqueue);
8058 priv->workqueue = NULL;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008059 iwl3945_unset_hw_setting(priv);
Zhu Yib481de92007-09-25 17:54:57 -07008060
8061 out_iounmap:
8062 pci_iounmap(pdev, priv->hw_base);
8063 out_pci_release_regions:
8064 pci_release_regions(pdev);
8065 out_pci_disable_device:
8066 pci_disable_device(pdev);
8067 pci_set_drvdata(pdev, NULL);
8068 out_ieee80211_free_hw:
8069 ieee80211_free_hw(priv->hw);
8070 out:
8071 return err;
8072}
8073
Reinette Chatrec83dbf62008-03-21 13:53:41 -07008074static void __devexit iwl3945_pci_remove(struct pci_dev *pdev)
Zhu Yib481de92007-09-25 17:54:57 -07008075{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008076 struct iwl3945_priv *priv = pci_get_drvdata(pdev);
Mohamed Abbas0359fac2008-03-28 16:21:08 -07008077 unsigned long flags;
Zhu Yib481de92007-09-25 17:54:57 -07008078
8079 if (!priv)
8080 return;
8081
8082 IWL_DEBUG_INFO("*** UNLOAD DRIVER ***\n");
8083
Zhu Yib481de92007-09-25 17:54:57 -07008084 set_bit(STATUS_EXIT_PENDING, &priv->status);
Zhu Yib24d22b2007-12-19 13:59:52 +08008085
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008086 iwl3945_down(priv);
Zhu Yib481de92007-09-25 17:54:57 -07008087
Mohamed Abbas0359fac2008-03-28 16:21:08 -07008088 /* make sure we flush any pending irq or
8089 * tasklet for the driver
8090 */
8091 spin_lock_irqsave(&priv->lock, flags);
8092 iwl3945_disable_interrupts(priv);
8093 spin_unlock_irqrestore(&priv->lock, flags);
8094
8095 iwl_synchronize_irq(priv);
8096
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008097 sysfs_remove_group(&pdev->dev.kobj, &iwl3945_attribute_group);
Zhu Yib481de92007-09-25 17:54:57 -07008098
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08008099 iwl3945_rfkill_unregister(priv);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008100 iwl3945_dealloc_ucode_pci(priv);
Zhu Yib481de92007-09-25 17:54:57 -07008101
8102 if (priv->rxq.bd)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008103 iwl3945_rx_queue_free(priv, &priv->rxq);
8104 iwl3945_hw_txq_ctx_free(priv);
Zhu Yib481de92007-09-25 17:54:57 -07008105
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008106 iwl3945_unset_hw_setting(priv);
8107 iwl3945_clear_stations_table(priv);
Zhu Yib481de92007-09-25 17:54:57 -07008108
Tomas Winkler3ac7f142008-07-21 02:40:14 +03008109 if (priv->mac80211_registered)
Zhu Yib481de92007-09-25 17:54:57 -07008110 ieee80211_unregister_hw(priv->hw);
Zhu Yib481de92007-09-25 17:54:57 -07008111
Mohamed Abbas6ef89d02007-10-25 17:15:47 +08008112 /*netif_stop_queue(dev); */
8113 flush_workqueue(priv->workqueue);
8114
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008115 /* ieee80211_unregister_hw calls iwl3945_mac_stop, which flushes
Zhu Yib481de92007-09-25 17:54:57 -07008116 * priv->workqueue... so we can't take down the workqueue
8117 * until now... */
8118 destroy_workqueue(priv->workqueue);
8119 priv->workqueue = NULL;
8120
Zhu Yib481de92007-09-25 17:54:57 -07008121 pci_iounmap(pdev, priv->hw_base);
8122 pci_release_regions(pdev);
8123 pci_disable_device(pdev);
8124 pci_set_drvdata(pdev, NULL);
8125
Reinette Chatre849e0dc2008-01-23 10:15:18 -08008126 iwl3945_free_channel_map(priv);
8127 iwl3945_free_geos(priv);
Emmanuel Grumbach261415f2008-05-29 16:35:25 +08008128 kfree(priv->scan);
Zhu Yib481de92007-09-25 17:54:57 -07008129 if (priv->ibss_beacon)
8130 dev_kfree_skb(priv->ibss_beacon);
8131
8132 ieee80211_free_hw(priv->hw);
8133}
8134
8135#ifdef CONFIG_PM
8136
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008137static int iwl3945_pci_suspend(struct pci_dev *pdev, pm_message_t state)
Zhu Yib481de92007-09-25 17:54:57 -07008138{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008139 struct iwl3945_priv *priv = pci_get_drvdata(pdev);
Zhu Yib481de92007-09-25 17:54:57 -07008140
Zhu Yie655b9f2008-01-24 02:19:38 -08008141 if (priv->is_open) {
8142 set_bit(STATUS_IN_SUSPEND, &priv->status);
8143 iwl3945_mac_stop(priv->hw);
8144 priv->is_open = 1;
8145 }
Zhu Yib481de92007-09-25 17:54:57 -07008146
Zhu Yib481de92007-09-25 17:54:57 -07008147 pci_set_power_state(pdev, PCI_D3hot);
8148
Zhu Yib481de92007-09-25 17:54:57 -07008149 return 0;
8150}
8151
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008152static int iwl3945_pci_resume(struct pci_dev *pdev)
Zhu Yib481de92007-09-25 17:54:57 -07008153{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008154 struct iwl3945_priv *priv = pci_get_drvdata(pdev);
Zhu Yib481de92007-09-25 17:54:57 -07008155
Zhu Yib481de92007-09-25 17:54:57 -07008156 pci_set_power_state(pdev, PCI_D0);
Zhu Yib481de92007-09-25 17:54:57 -07008157
Zhu Yie655b9f2008-01-24 02:19:38 -08008158 if (priv->is_open)
8159 iwl3945_mac_start(priv->hw);
Zhu Yib481de92007-09-25 17:54:57 -07008160
Zhu Yie655b9f2008-01-24 02:19:38 -08008161 clear_bit(STATUS_IN_SUSPEND, &priv->status);
Zhu Yib481de92007-09-25 17:54:57 -07008162 return 0;
8163}
8164
8165#endif /* CONFIG_PM */
8166
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08008167/*************** RFKILL FUNCTIONS **********/
Adel Gadllah80fcc9e2008-07-01 17:49:50 +02008168#ifdef CONFIG_IWL3945_RFKILL
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08008169/* software rf-kill from user */
8170static int iwl3945_rfkill_soft_rf_kill(void *data, enum rfkill_state state)
8171{
8172 struct iwl3945_priv *priv = data;
8173 int err = 0;
8174
Adel Gadllah80fcc9e2008-07-01 17:49:50 +02008175 if (!priv->rfkill)
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08008176 return 0;
8177
8178 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
8179 return 0;
8180
Tomas Winklera96a27f2008-10-23 23:48:56 -07008181 IWL_DEBUG_RF_KILL("we received soft RFKILL set to state %d\n", state);
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08008182 mutex_lock(&priv->mutex);
8183
8184 switch (state) {
Zhu Yiacdfe9b2008-06-30 17:23:32 +08008185 case RFKILL_STATE_UNBLOCKED:
Adel Gadllah80fcc9e2008-07-01 17:49:50 +02008186 if (iwl3945_is_rfkill_hw(priv)) {
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08008187 err = -EBUSY;
Adel Gadllah80fcc9e2008-07-01 17:49:50 +02008188 goto out_unlock;
8189 }
8190 iwl3945_radio_kill_sw(priv, 0);
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08008191 break;
Zhu Yiacdfe9b2008-06-30 17:23:32 +08008192 case RFKILL_STATE_SOFT_BLOCKED:
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08008193 iwl3945_radio_kill_sw(priv, 1);
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08008194 break;
Zhu Yiacdfe9b2008-06-30 17:23:32 +08008195 default:
Tomas Winklera96a27f2008-10-23 23:48:56 -07008196 IWL_WARNING("we received unexpected RFKILL state %d\n", state);
Zhu Yiacdfe9b2008-06-30 17:23:32 +08008197 break;
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08008198 }
Adel Gadllah80fcc9e2008-07-01 17:49:50 +02008199out_unlock:
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08008200 mutex_unlock(&priv->mutex);
8201
8202 return err;
8203}
8204
8205int iwl3945_rfkill_init(struct iwl3945_priv *priv)
8206{
8207 struct device *device = wiphy_dev(priv->hw->wiphy);
8208 int ret = 0;
8209
8210 BUG_ON(device == NULL);
8211
8212 IWL_DEBUG_RF_KILL("Initializing RFKILL.\n");
Adel Gadllah80fcc9e2008-07-01 17:49:50 +02008213 priv->rfkill = rfkill_allocate(device, RFKILL_TYPE_WLAN);
8214 if (!priv->rfkill) {
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08008215 IWL_ERROR("Unable to allocate rfkill device.\n");
8216 ret = -ENOMEM;
8217 goto error;
8218 }
8219
Adel Gadllah80fcc9e2008-07-01 17:49:50 +02008220 priv->rfkill->name = priv->cfg->name;
8221 priv->rfkill->data = priv;
8222 priv->rfkill->state = RFKILL_STATE_UNBLOCKED;
8223 priv->rfkill->toggle_radio = iwl3945_rfkill_soft_rf_kill;
8224 priv->rfkill->user_claim_unsupported = 1;
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08008225
Adel Gadllah80fcc9e2008-07-01 17:49:50 +02008226 priv->rfkill->dev.class->suspend = NULL;
8227 priv->rfkill->dev.class->resume = NULL;
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08008228
Adel Gadllah80fcc9e2008-07-01 17:49:50 +02008229 ret = rfkill_register(priv->rfkill);
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08008230 if (ret) {
8231 IWL_ERROR("Unable to register rfkill: %d\n", ret);
Adel Gadllah80fcc9e2008-07-01 17:49:50 +02008232 goto freed_rfkill;
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08008233 }
8234
8235 IWL_DEBUG_RF_KILL("RFKILL initialization complete.\n");
8236 return ret;
8237
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08008238freed_rfkill:
Adel Gadllah80fcc9e2008-07-01 17:49:50 +02008239 if (priv->rfkill != NULL)
8240 rfkill_free(priv->rfkill);
8241 priv->rfkill = NULL;
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08008242
8243error:
8244 IWL_DEBUG_RF_KILL("RFKILL initialization complete.\n");
8245 return ret;
8246}
8247
8248void iwl3945_rfkill_unregister(struct iwl3945_priv *priv)
8249{
Adel Gadllah80fcc9e2008-07-01 17:49:50 +02008250 if (priv->rfkill)
8251 rfkill_unregister(priv->rfkill);
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08008252
Adel Gadllah80fcc9e2008-07-01 17:49:50 +02008253 priv->rfkill = NULL;
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08008254}
8255
8256/* set rf-kill to the right state. */
8257void iwl3945_rfkill_set_hw_state(struct iwl3945_priv *priv)
8258{
8259
Adel Gadllah80fcc9e2008-07-01 17:49:50 +02008260 if (!priv->rfkill)
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08008261 return;
8262
Adel Gadllah80fcc9e2008-07-01 17:49:50 +02008263 if (iwl3945_is_rfkill_hw(priv)) {
8264 rfkill_force_state(priv->rfkill, RFKILL_STATE_HARD_BLOCKED);
8265 return;
8266 }
8267
8268 if (!iwl3945_is_rfkill_sw(priv))
8269 rfkill_force_state(priv->rfkill, RFKILL_STATE_UNBLOCKED);
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08008270 else
Adel Gadllah80fcc9e2008-07-01 17:49:50 +02008271 rfkill_force_state(priv->rfkill, RFKILL_STATE_SOFT_BLOCKED);
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08008272}
8273#endif
8274
Zhu Yib481de92007-09-25 17:54:57 -07008275/*****************************************************************************
8276 *
8277 * driver and module entry point
8278 *
8279 *****************************************************************************/
8280
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008281static struct pci_driver iwl3945_driver = {
Zhu Yib481de92007-09-25 17:54:57 -07008282 .name = DRV_NAME,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008283 .id_table = iwl3945_hw_card_ids,
8284 .probe = iwl3945_pci_probe,
8285 .remove = __devexit_p(iwl3945_pci_remove),
Zhu Yib481de92007-09-25 17:54:57 -07008286#ifdef CONFIG_PM
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008287 .suspend = iwl3945_pci_suspend,
8288 .resume = iwl3945_pci_resume,
Zhu Yib481de92007-09-25 17:54:57 -07008289#endif
8290};
8291
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008292static int __init iwl3945_init(void)
Zhu Yib481de92007-09-25 17:54:57 -07008293{
8294
8295 int ret;
8296 printk(KERN_INFO DRV_NAME ": " DRV_DESCRIPTION ", " DRV_VERSION "\n");
8297 printk(KERN_INFO DRV_NAME ": " DRV_COPYRIGHT "\n");
Reinette Chatre897e1cf2008-03-28 16:21:09 -07008298
8299 ret = iwl3945_rate_control_register();
8300 if (ret) {
8301 IWL_ERROR("Unable to register rate control algorithm: %d\n", ret);
8302 return ret;
8303 }
8304
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008305 ret = pci_register_driver(&iwl3945_driver);
Zhu Yib481de92007-09-25 17:54:57 -07008306 if (ret) {
8307 IWL_ERROR("Unable to initialize PCI module\n");
Reinette Chatre897e1cf2008-03-28 16:21:09 -07008308 goto error_register;
Zhu Yib481de92007-09-25 17:54:57 -07008309 }
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08008310#ifdef CONFIG_IWL3945_DEBUG
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008311 ret = driver_create_file(&iwl3945_driver.driver, &driver_attr_debug_level);
Zhu Yib481de92007-09-25 17:54:57 -07008312 if (ret) {
8313 IWL_ERROR("Unable to create driver sysfs file\n");
Reinette Chatre897e1cf2008-03-28 16:21:09 -07008314 goto error_debug;
Zhu Yib481de92007-09-25 17:54:57 -07008315 }
8316#endif
8317
8318 return ret;
Reinette Chatre897e1cf2008-03-28 16:21:09 -07008319
8320#ifdef CONFIG_IWL3945_DEBUG
8321error_debug:
8322 pci_unregister_driver(&iwl3945_driver);
8323#endif
8324error_register:
8325 iwl3945_rate_control_unregister();
8326 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07008327}
8328
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008329static void __exit iwl3945_exit(void)
Zhu Yib481de92007-09-25 17:54:57 -07008330{
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08008331#ifdef CONFIG_IWL3945_DEBUG
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008332 driver_remove_file(&iwl3945_driver.driver, &driver_attr_debug_level);
Zhu Yib481de92007-09-25 17:54:57 -07008333#endif
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008334 pci_unregister_driver(&iwl3945_driver);
Reinette Chatre897e1cf2008-03-28 16:21:09 -07008335 iwl3945_rate_control_unregister();
Zhu Yib481de92007-09-25 17:54:57 -07008336}
8337
Reinette Chatrea0987a82008-12-02 12:14:06 -08008338MODULE_FIRMWARE(IWL3945_MODULE_FIRMWARE(IWL3945_UCODE_API_MAX));
Zhu Yi25cb6ca2008-09-11 11:45:22 +08008339
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008340module_param_named(antenna, iwl3945_param_antenna, int, 0444);
Zhu Yib481de92007-09-25 17:54:57 -07008341MODULE_PARM_DESC(antenna, "select antenna (1=Main, 2=Aux, default 0 [both])");
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008342module_param_named(disable, iwl3945_param_disable, int, 0444);
Zhu Yib481de92007-09-25 17:54:57 -07008343MODULE_PARM_DESC(disable, "manually disable the radio (default 0 [radio on])");
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008344module_param_named(hwcrypto, iwl3945_param_hwcrypto, int, 0444);
Zhu Yib481de92007-09-25 17:54:57 -07008345MODULE_PARM_DESC(hwcrypto,
8346 "using hardware crypto engine (default 0 [software])\n");
Wu, Fengguang95aa1942008-12-17 16:52:30 +08008347module_param_named(debug, iwl3945_param_debug, uint, 0444);
Zhu Yib481de92007-09-25 17:54:57 -07008348MODULE_PARM_DESC(debug, "debug output mask");
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008349module_param_named(disable_hw_scan, iwl3945_param_disable_hw_scan, int, 0444);
Zhu Yib481de92007-09-25 17:54:57 -07008350MODULE_PARM_DESC(disable_hw_scan, "disable hardware scanning (default 0)");
8351
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008352module_param_named(queues_num, iwl3945_param_queues_num, int, 0444);
Zhu Yib481de92007-09-25 17:54:57 -07008353MODULE_PARM_DESC(queues_num, "number of hw queues.");
8354
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008355module_exit(iwl3945_exit);
8356module_init(iwl3945_init);