blob: 421c944b1877022e79f86c065b73fdf71435b719 [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
Samuel Ortiza3139c52008-12-19 10:37:09 +080049#define DRV_NAME "iwl3945"
50
Tomas Winkler82b9a122008-03-04 18:09:30 -080051#include "iwl-3945-core.h"
Tomas Winkler600c0e12008-12-19 10:37:04 +080052#include "iwl-commands.h"
Zhu Yib481de92007-09-25 17:54:57 -070053#include "iwl-3945.h"
Tomas Winklerbddadf82008-12-19 10:37:01 +080054#include "iwl-3945-fh.h"
Zhu Yib481de92007-09-25 17:54:57 -070055#include "iwl-helpers.h"
Samuel Ortizd20b3c62008-12-19 10:37:15 +080056#include "iwl-dev.h"
Zhu Yib481de92007-09-25 17:54:57 -070057
Christoph Hellwigbb8c0932008-01-27 16:41:47 -080058static int iwl3945_tx_queue_update_write_ptr(struct iwl3945_priv *priv,
59 struct iwl3945_tx_queue *txq);
Christoph Hellwig416e1432007-10-25 17:15:49 +080060
Zhu Yib481de92007-09-25 17:54:57 -070061/******************************************************************************
62 *
63 * module boiler plate
64 *
65 ******************************************************************************/
66
67/* module parameters */
Cahill, Ben M6440adb2007-11-29 11:09:55 +080068static int iwl3945_param_disable_hw_scan; /* def: 0 = use 3945's h/w scan */
Wu, Fengguang95aa1942008-12-17 16:52:30 +080069static u32 iwl3945_param_debug; /* def: 0 = minimal debug log messages */
Cahill, Ben M6440adb2007-11-29 11:09:55 +080070static int iwl3945_param_disable; /* def: 0 = enable radio */
Ben Cahill9fbab512007-11-29 11:09:47 +080071static int iwl3945_param_antenna; /* def: 0 = both antennas (use diversity) */
Cahill, Ben M6440adb2007-11-29 11:09:55 +080072int iwl3945_param_hwcrypto; /* def: 0 = use software encryption */
Ron Rindjunskydfe7d452008-04-15 16:01:45 -070073int iwl3945_param_queues_num = IWL39_MAX_NUM_QUEUES; /* def: 8 Tx queues */
Zhu Yib481de92007-09-25 17:54:57 -070074
75/*
76 * module name, copyright, version, etc.
Zhu Yib481de92007-09-25 17:54:57 -070077 */
78
79#define DRV_DESCRIPTION \
80"Intel(R) PRO/Wireless 3945ABG/BG Network Connection driver for Linux"
81
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +080082#ifdef CONFIG_IWL3945_DEBUG
Zhu Yib481de92007-09-25 17:54:57 -070083#define VD "d"
84#else
85#define VD
86#endif
87
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +080088#ifdef CONFIG_IWL3945_SPECTRUM_MEASUREMENT
Zhu Yib481de92007-09-25 17:54:57 -070089#define VS "s"
90#else
91#define VS
92#endif
93
Reinette Chatreb9e0b442008-02-08 16:39:11 -080094#define IWLWIFI_VERSION "1.2.26k" VD VS
Reinette Chatreeb7ae892008-03-11 16:17:17 -070095#define DRV_COPYRIGHT "Copyright(c) 2003-2008 Intel Corporation"
Tomas Winklera7b75202008-12-11 10:33:41 -080096#define DRV_AUTHOR "<ilw@linux.intel.com>"
Zhu Yib481de92007-09-25 17:54:57 -070097#define DRV_VERSION IWLWIFI_VERSION
98
Zhu Yib481de92007-09-25 17:54:57 -070099
100MODULE_DESCRIPTION(DRV_DESCRIPTION);
101MODULE_VERSION(DRV_VERSION);
Tomas Winklera7b75202008-12-11 10:33:41 -0800102MODULE_AUTHOR(DRV_COPYRIGHT " " DRV_AUTHOR);
Zhu Yib481de92007-09-25 17:54:57 -0700103MODULE_LICENSE("GPL");
104
Johannes Berg8318d782008-01-24 19:38:38 +0100105static const struct ieee80211_supported_band *iwl3945_get_band(
106 struct iwl3945_priv *priv, enum ieee80211_band band)
Zhu Yib481de92007-09-25 17:54:57 -0700107{
Johannes Berg8318d782008-01-24 19:38:38 +0100108 return priv->hw->wiphy->bands[band];
Zhu Yib481de92007-09-25 17:54:57 -0700109}
110
Zhu Yib481de92007-09-25 17:54:57 -0700111/*************** DMA-QUEUE-GENERAL-FUNCTIONS *****
112 * DMA services
113 *
114 * Theory of operation
115 *
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800116 * A Tx or Rx queue resides in host DRAM, and is comprised of a circular buffer
117 * of buffer descriptors, each of which points to one or more data buffers for
118 * the device to read from or fill. Driver and device exchange status of each
119 * queue via "read" and "write" pointers. Driver keeps minimum of 2 empty
120 * entries in each circular buffer, to protect against confusing empty and full
121 * queue states.
122 *
123 * The device reads or writes the data in the queues via the device's several
124 * DMA/FIFO channels. Each queue is mapped to a single DMA channel.
Zhu Yib481de92007-09-25 17:54:57 -0700125 *
126 * For Tx queue, there are low mark and high mark limits. If, after queuing
127 * the packet for Tx, free space become < low mark, Tx queue stopped. When
128 * reclaiming packets (on 'tx done IRQ), if free space become > high mark,
129 * Tx queue resumed.
130 *
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800131 * The 3945 operates with six queues: One receive queue, one transmit queue
132 * (#4) for sending commands to the device firmware, and four transmit queues
133 * (#0-3) for data tx via EDCA. An additional 2 HCCA queues are unused.
Zhu Yib481de92007-09-25 17:54:57 -0700134 ***************************************************/
135
Samuel Ortizd20b3c62008-12-19 10:37:15 +0800136int iwl3945_x2_queue_used(const struct iwl_queue *q, int i)
Zhu Yib481de92007-09-25 17:54:57 -0700137{
Tomas Winklerfc4b6852007-10-25 17:15:24 +0800138 return q->write_ptr > q->read_ptr ?
139 (i >= q->read_ptr && i < q->write_ptr) :
140 !(i < q->read_ptr && i >= q->write_ptr);
Zhu Yib481de92007-09-25 17:54:57 -0700141}
142
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800143/**
144 * iwl3945_queue_init - Initialize queue's high/low-water and read/write indexes
145 */
Samuel Ortizd20b3c62008-12-19 10:37:15 +0800146static int iwl3945_queue_init(struct iwl3945_priv *priv, struct iwl_queue *q,
Zhu Yib481de92007-09-25 17:54:57 -0700147 int count, int slots_num, u32 id)
148{
149 q->n_bd = count;
150 q->n_window = slots_num;
151 q->id = id;
152
Tomas Winklerc54b6792008-03-06 17:36:53 -0800153 /* count must be power-of-two size, otherwise iwl_queue_inc_wrap
154 * and iwl_queue_dec_wrap are broken. */
Zhu Yib481de92007-09-25 17:54:57 -0700155 BUG_ON(!is_power_of_2(count));
156
157 /* slots_num must be power-of-two size, otherwise
158 * get_cmd_index is broken. */
159 BUG_ON(!is_power_of_2(slots_num));
160
161 q->low_mark = q->n_window / 4;
162 if (q->low_mark < 4)
163 q->low_mark = 4;
164
165 q->high_mark = q->n_window / 8;
166 if (q->high_mark < 2)
167 q->high_mark = 2;
168
Tomas Winklerfc4b6852007-10-25 17:15:24 +0800169 q->write_ptr = q->read_ptr = 0;
Zhu Yib481de92007-09-25 17:54:57 -0700170
171 return 0;
172}
173
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800174/**
175 * iwl3945_tx_queue_alloc - Alloc driver data and TFD CB for one Tx/cmd queue
176 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800177static int iwl3945_tx_queue_alloc(struct iwl3945_priv *priv,
178 struct iwl3945_tx_queue *txq, u32 id)
Zhu Yib481de92007-09-25 17:54:57 -0700179{
180 struct pci_dev *dev = priv->pci_dev;
181
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800182 /* Driver private data, only for Tx (not command) queues,
183 * not shared with device. */
Zhu Yib481de92007-09-25 17:54:57 -0700184 if (id != IWL_CMD_QUEUE_NUM) {
185 txq->txb = kmalloc(sizeof(txq->txb[0]) *
186 TFD_QUEUE_SIZE_MAX, GFP_KERNEL);
187 if (!txq->txb) {
Ian Schram01ebd062007-10-25 17:15:22 +0800188 IWL_ERROR("kmalloc for auxiliary BD "
Zhu Yib481de92007-09-25 17:54:57 -0700189 "structures failed\n");
190 goto error;
191 }
192 } else
193 txq->txb = NULL;
194
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800195 /* Circular buffer of transmit frame descriptors (TFDs),
196 * shared with device */
Zhu Yib481de92007-09-25 17:54:57 -0700197 txq->bd = pci_alloc_consistent(dev,
198 sizeof(txq->bd[0]) * TFD_QUEUE_SIZE_MAX,
199 &txq->q.dma_addr);
200
201 if (!txq->bd) {
202 IWL_ERROR("pci_alloc_consistent(%zd) failed\n",
203 sizeof(txq->bd[0]) * TFD_QUEUE_SIZE_MAX);
204 goto error;
205 }
206 txq->q.id = id;
207
208 return 0;
209
210 error:
Tomas Winkler3ac7f142008-07-21 02:40:14 +0300211 kfree(txq->txb);
212 txq->txb = NULL;
Zhu Yib481de92007-09-25 17:54:57 -0700213
214 return -ENOMEM;
215}
216
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800217/**
218 * iwl3945_tx_queue_init - Allocate and initialize one tx/cmd queue
219 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800220int iwl3945_tx_queue_init(struct iwl3945_priv *priv,
221 struct iwl3945_tx_queue *txq, int slots_num, u32 txq_id)
Zhu Yib481de92007-09-25 17:54:57 -0700222{
223 struct pci_dev *dev = priv->pci_dev;
224 int len;
225 int rc = 0;
226
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800227 /*
228 * Alloc buffer array for commands (Tx or other types of commands).
229 * For the command queue (#4), allocate command space + one big
230 * command for scan, since scan command is very huge; the system will
231 * not have two scans at the same time, so only one is needed.
232 * For data Tx queues (all other queues), no super-size command
233 * space is needed.
234 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800235 len = sizeof(struct iwl3945_cmd) * slots_num;
Zhu Yib481de92007-09-25 17:54:57 -0700236 if (txq_id == IWL_CMD_QUEUE_NUM)
237 len += IWL_MAX_SCAN_SIZE;
238 txq->cmd = pci_alloc_consistent(dev, len, &txq->dma_addr_cmd);
239 if (!txq->cmd)
240 return -ENOMEM;
241
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800242 /* Alloc driver data array and TFD circular buffer */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800243 rc = iwl3945_tx_queue_alloc(priv, txq, txq_id);
Zhu Yib481de92007-09-25 17:54:57 -0700244 if (rc) {
245 pci_free_consistent(dev, len, txq->cmd, txq->dma_addr_cmd);
246
247 return -ENOMEM;
248 }
249 txq->need_update = 0;
250
251 /* TFD_QUEUE_SIZE_MAX must be power-of-two size, otherwise
Tomas Winklerc54b6792008-03-06 17:36:53 -0800252 * iwl_queue_inc_wrap and iwl_queue_dec_wrap are broken. */
Zhu Yib481de92007-09-25 17:54:57 -0700253 BUILD_BUG_ON(TFD_QUEUE_SIZE_MAX & (TFD_QUEUE_SIZE_MAX - 1));
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800254
255 /* Initialize queue high/low-water, head/tail indexes */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800256 iwl3945_queue_init(priv, &txq->q, TFD_QUEUE_SIZE_MAX, slots_num, txq_id);
Zhu Yib481de92007-09-25 17:54:57 -0700257
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800258 /* Tell device where to find queue, enable DMA channel. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800259 iwl3945_hw_tx_queue_init(priv, txq);
Zhu Yib481de92007-09-25 17:54:57 -0700260
261 return 0;
262}
263
264/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800265 * iwl3945_tx_queue_free - Deallocate DMA queue.
Zhu Yib481de92007-09-25 17:54:57 -0700266 * @txq: Transmit queue to deallocate.
267 *
268 * Empty queue by removing and destroying all BD's.
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800269 * Free all buffers.
270 * 0-fill, but do not free "txq" descriptor structure.
Zhu Yib481de92007-09-25 17:54:57 -0700271 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800272void iwl3945_tx_queue_free(struct iwl3945_priv *priv, struct iwl3945_tx_queue *txq)
Zhu Yib481de92007-09-25 17:54:57 -0700273{
Samuel Ortizd20b3c62008-12-19 10:37:15 +0800274 struct iwl_queue *q = &txq->q;
Zhu Yib481de92007-09-25 17:54:57 -0700275 struct pci_dev *dev = priv->pci_dev;
276 int len;
277
278 if (q->n_bd == 0)
279 return;
280
281 /* first, empty all BD's */
Tomas Winklerfc4b6852007-10-25 17:15:24 +0800282 for (; q->write_ptr != q->read_ptr;
Tomas Winklerc54b6792008-03-06 17:36:53 -0800283 q->read_ptr = iwl_queue_inc_wrap(q->read_ptr, q->n_bd))
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800284 iwl3945_hw_txq_free_tfd(priv, txq);
Zhu Yib481de92007-09-25 17:54:57 -0700285
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800286 len = sizeof(struct iwl3945_cmd) * q->n_window;
Zhu Yib481de92007-09-25 17:54:57 -0700287 if (q->id == IWL_CMD_QUEUE_NUM)
288 len += IWL_MAX_SCAN_SIZE;
289
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800290 /* De-alloc array of command/tx buffers */
Zhu Yib481de92007-09-25 17:54:57 -0700291 pci_free_consistent(dev, len, txq->cmd, txq->dma_addr_cmd);
292
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800293 /* De-alloc circular buffer of TFDs */
Zhu Yib481de92007-09-25 17:54:57 -0700294 if (txq->q.n_bd)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800295 pci_free_consistent(dev, sizeof(struct iwl3945_tfd_frame) *
Zhu Yib481de92007-09-25 17:54:57 -0700296 txq->q.n_bd, txq->bd, txq->q.dma_addr);
297
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800298 /* De-alloc array of per-TFD driver data */
Tomas Winkler3ac7f142008-07-21 02:40:14 +0300299 kfree(txq->txb);
300 txq->txb = NULL;
Zhu Yib481de92007-09-25 17:54:57 -0700301
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800302 /* 0-fill queue descriptor structure */
Zhu Yib481de92007-09-25 17:54:57 -0700303 memset(txq, 0, sizeof(*txq));
304}
305
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800306const u8 iwl3945_broadcast_addr[ETH_ALEN] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
Zhu Yib481de92007-09-25 17:54:57 -0700307
308/*************** STATION TABLE MANAGEMENT ****
Ben Cahill9fbab512007-11-29 11:09:47 +0800309 * mac80211 should be examined to determine if sta_info is duplicating
Zhu Yib481de92007-09-25 17:54:57 -0700310 * the functionality provided here
311 */
312
313/**************************************************************/
Ian Schram01ebd062007-10-25 17:15:22 +0800314#if 0 /* temporary disable till we add real remove station */
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800315/**
316 * iwl3945_remove_station - Remove driver's knowledge of station.
317 *
318 * NOTE: This does not remove station from device's station table.
319 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800320static u8 iwl3945_remove_station(struct iwl3945_priv *priv, const u8 *addr, int is_ap)
Zhu Yib481de92007-09-25 17:54:57 -0700321{
322 int index = IWL_INVALID_STATION;
323 int i;
324 unsigned long flags;
325
326 spin_lock_irqsave(&priv->sta_lock, flags);
327
328 if (is_ap)
329 index = IWL_AP_ID;
330 else if (is_broadcast_ether_addr(addr))
331 index = priv->hw_setting.bcast_sta_id;
332 else
333 for (i = IWL_STA_ID; i < priv->hw_setting.max_stations; i++)
334 if (priv->stations[i].used &&
335 !compare_ether_addr(priv->stations[i].sta.sta.addr,
336 addr)) {
337 index = i;
338 break;
339 }
340
341 if (unlikely(index == IWL_INVALID_STATION))
342 goto out;
343
344 if (priv->stations[index].used) {
345 priv->stations[index].used = 0;
346 priv->num_stations--;
347 }
348
349 BUG_ON(priv->num_stations < 0);
350
351out:
352 spin_unlock_irqrestore(&priv->sta_lock, flags);
353 return 0;
354}
Zhu Yi556f8db2007-09-27 11:27:33 +0800355#endif
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800356
357/**
358 * iwl3945_clear_stations_table - Clear the driver's station table
359 *
360 * NOTE: This does not clear or otherwise alter the device's station table.
361 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800362static void iwl3945_clear_stations_table(struct iwl3945_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700363{
364 unsigned long flags;
365
366 spin_lock_irqsave(&priv->sta_lock, flags);
367
368 priv->num_stations = 0;
369 memset(priv->stations, 0, sizeof(priv->stations));
370
371 spin_unlock_irqrestore(&priv->sta_lock, flags);
372}
373
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800374/**
375 * iwl3945_add_station - Add station to station tables in driver and device
376 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800377u8 iwl3945_add_station(struct iwl3945_priv *priv, const u8 *addr, int is_ap, u8 flags)
Zhu Yib481de92007-09-25 17:54:57 -0700378{
379 int i;
380 int index = IWL_INVALID_STATION;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800381 struct iwl3945_station_entry *station;
Zhu Yib481de92007-09-25 17:54:57 -0700382 unsigned long flags_spin;
Zhu Yic14c5212007-09-27 11:27:35 +0800383 u8 rate;
Zhu Yib481de92007-09-25 17:54:57 -0700384
385 spin_lock_irqsave(&priv->sta_lock, flags_spin);
386 if (is_ap)
387 index = IWL_AP_ID;
388 else if (is_broadcast_ether_addr(addr))
389 index = priv->hw_setting.bcast_sta_id;
390 else
391 for (i = IWL_STA_ID; i < priv->hw_setting.max_stations; i++) {
392 if (!compare_ether_addr(priv->stations[i].sta.sta.addr,
393 addr)) {
394 index = i;
395 break;
396 }
397
398 if (!priv->stations[i].used &&
399 index == IWL_INVALID_STATION)
400 index = i;
401 }
402
Ian Schram01ebd062007-10-25 17:15:22 +0800403 /* These two conditions has the same outcome but keep them separate
Zhu Yib481de92007-09-25 17:54:57 -0700404 since they have different meaning */
405 if (unlikely(index == IWL_INVALID_STATION)) {
406 spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
407 return index;
408 }
409
410 if (priv->stations[index].used &&
411 !compare_ether_addr(priv->stations[index].sta.sta.addr, addr)) {
412 spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
413 return index;
414 }
415
Johannes Berge1749612008-10-27 15:59:26 -0700416 IWL_DEBUG_ASSOC("Add STA ID %d: %pM\n", index, addr);
Zhu Yib481de92007-09-25 17:54:57 -0700417 station = &priv->stations[index];
418 station->used = 1;
419 priv->num_stations++;
420
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800421 /* Set up the REPLY_ADD_STA command to send to device */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800422 memset(&station->sta, 0, sizeof(struct iwl3945_addsta_cmd));
Zhu Yib481de92007-09-25 17:54:57 -0700423 memcpy(station->sta.sta.addr, addr, ETH_ALEN);
424 station->sta.mode = 0;
425 station->sta.sta.sta_id = index;
426 station->sta.station_flags = 0;
427
Johannes Berg8318d782008-01-24 19:38:38 +0100428 if (priv->band == IEEE80211_BAND_5GHZ)
Tomas Winkler69946332007-10-25 17:15:27 +0800429 rate = IWL_RATE_6M_PLCP;
430 else
431 rate = IWL_RATE_1M_PLCP;
Zhu Yic14c5212007-09-27 11:27:35 +0800432
433 /* Turn on both antennas for the station... */
434 station->sta.rate_n_flags =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800435 iwl3945_hw_set_rate_n_flags(rate, RATE_MCS_ANT_AB_MSK);
Zhu Yic14c5212007-09-27 11:27:35 +0800436
Zhu Yib481de92007-09-25 17:54:57 -0700437 spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800438
439 /* Add station to device's station table */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800440 iwl3945_send_add_station(priv, &station->sta, flags);
Zhu Yib481de92007-09-25 17:54:57 -0700441 return index;
442
443}
444
445/*************** DRIVER STATUS FUNCTIONS *****/
446
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800447static inline int iwl3945_is_ready(struct iwl3945_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700448{
449 /* The adapter is 'ready' if READY and GEO_CONFIGURED bits are
450 * set but EXIT_PENDING is not */
451 return test_bit(STATUS_READY, &priv->status) &&
452 test_bit(STATUS_GEO_CONFIGURED, &priv->status) &&
453 !test_bit(STATUS_EXIT_PENDING, &priv->status);
454}
455
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800456static inline int iwl3945_is_alive(struct iwl3945_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700457{
458 return test_bit(STATUS_ALIVE, &priv->status);
459}
460
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800461static inline int iwl3945_is_init(struct iwl3945_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700462{
463 return test_bit(STATUS_INIT, &priv->status);
464}
465
Adel Gadllah80fcc9e2008-07-01 17:49:50 +0200466static inline int iwl3945_is_rfkill_sw(struct iwl3945_priv *priv)
467{
468 return test_bit(STATUS_RF_KILL_SW, &priv->status);
469}
470
471static inline int iwl3945_is_rfkill_hw(struct iwl3945_priv *priv)
472{
473 return test_bit(STATUS_RF_KILL_HW, &priv->status);
474}
475
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800476static inline int iwl3945_is_rfkill(struct iwl3945_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700477{
Adel Gadllah80fcc9e2008-07-01 17:49:50 +0200478 return iwl3945_is_rfkill_hw(priv) ||
479 iwl3945_is_rfkill_sw(priv);
Zhu Yib481de92007-09-25 17:54:57 -0700480}
481
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800482static inline int iwl3945_is_ready_rf(struct iwl3945_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700483{
484
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800485 if (iwl3945_is_rfkill(priv))
Zhu Yib481de92007-09-25 17:54:57 -0700486 return 0;
487
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800488 return iwl3945_is_ready(priv);
Zhu Yib481de92007-09-25 17:54:57 -0700489}
490
491/*************** HOST COMMAND QUEUE FUNCTIONS *****/
492
Abhijeet Kolekarc3056062008-11-12 13:14:08 -0800493#define IWL_CMD(x) case x: return #x
Zhu Yib481de92007-09-25 17:54:57 -0700494
495static const char *get_cmd_string(u8 cmd)
496{
497 switch (cmd) {
498 IWL_CMD(REPLY_ALIVE);
499 IWL_CMD(REPLY_ERROR);
500 IWL_CMD(REPLY_RXON);
501 IWL_CMD(REPLY_RXON_ASSOC);
502 IWL_CMD(REPLY_QOS_PARAM);
503 IWL_CMD(REPLY_RXON_TIMING);
504 IWL_CMD(REPLY_ADD_STA);
505 IWL_CMD(REPLY_REMOVE_STA);
506 IWL_CMD(REPLY_REMOVE_ALL_STA);
507 IWL_CMD(REPLY_3945_RX);
508 IWL_CMD(REPLY_TX);
509 IWL_CMD(REPLY_RATE_SCALE);
510 IWL_CMD(REPLY_LEDS_CMD);
511 IWL_CMD(REPLY_TX_LINK_QUALITY_CMD);
512 IWL_CMD(RADAR_NOTIFICATION);
513 IWL_CMD(REPLY_QUIET_CMD);
514 IWL_CMD(REPLY_CHANNEL_SWITCH);
515 IWL_CMD(CHANNEL_SWITCH_NOTIFICATION);
516 IWL_CMD(REPLY_SPECTRUM_MEASUREMENT_CMD);
517 IWL_CMD(SPECTRUM_MEASURE_NOTIFICATION);
518 IWL_CMD(POWER_TABLE_CMD);
519 IWL_CMD(PM_SLEEP_NOTIFICATION);
520 IWL_CMD(PM_DEBUG_STATISTIC_NOTIFIC);
521 IWL_CMD(REPLY_SCAN_CMD);
522 IWL_CMD(REPLY_SCAN_ABORT_CMD);
523 IWL_CMD(SCAN_START_NOTIFICATION);
524 IWL_CMD(SCAN_RESULTS_NOTIFICATION);
525 IWL_CMD(SCAN_COMPLETE_NOTIFICATION);
526 IWL_CMD(BEACON_NOTIFICATION);
527 IWL_CMD(REPLY_TX_BEACON);
528 IWL_CMD(WHO_IS_AWAKE_NOTIFICATION);
529 IWL_CMD(QUIET_NOTIFICATION);
530 IWL_CMD(REPLY_TX_PWR_TABLE_CMD);
531 IWL_CMD(MEASURE_ABORT_NOTIFICATION);
532 IWL_CMD(REPLY_BT_CONFIG);
533 IWL_CMD(REPLY_STATISTICS_CMD);
534 IWL_CMD(STATISTICS_NOTIFICATION);
535 IWL_CMD(REPLY_CARD_STATE_CMD);
536 IWL_CMD(CARD_STATE_NOTIFICATION);
537 IWL_CMD(MISSED_BEACONS_NOTIFICATION);
538 default:
539 return "UNKNOWN";
540
541 }
542}
543
544#define HOST_COMPLETE_TIMEOUT (HZ / 2)
545
546/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800547 * iwl3945_enqueue_hcmd - enqueue a uCode command
Zhu Yib481de92007-09-25 17:54:57 -0700548 * @priv: device private data point
549 * @cmd: a point to the ucode command structure
550 *
551 * The function returns < 0 values to indicate the operation is
552 * failed. On success, it turns the index (> 0) of command in the
553 * command queue.
554 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800555static int iwl3945_enqueue_hcmd(struct iwl3945_priv *priv, struct iwl3945_host_cmd *cmd)
Zhu Yib481de92007-09-25 17:54:57 -0700556{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800557 struct iwl3945_tx_queue *txq = &priv->txq[IWL_CMD_QUEUE_NUM];
Samuel Ortizd20b3c62008-12-19 10:37:15 +0800558 struct iwl_queue *q = &txq->q;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800559 struct iwl3945_tfd_frame *tfd;
Zhu Yib481de92007-09-25 17:54:57 -0700560 u32 *control_flags;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800561 struct iwl3945_cmd *out_cmd;
Zhu Yib481de92007-09-25 17:54:57 -0700562 u32 idx;
563 u16 fix_size = (u16)(cmd->len + sizeof(out_cmd->hdr));
564 dma_addr_t phys_addr;
565 int pad;
566 u16 count;
567 int ret;
568 unsigned long flags;
569
570 /* If any of the command structures end up being larger than
571 * the TFD_MAX_PAYLOAD_SIZE, and it sent as a 'small' command then
572 * we will need to increase the size of the TFD entries */
Samuel Ortizd20b3c62008-12-19 10:37:15 +0800573 BUG_ON((fix_size > TFD39_MAX_PAYLOAD_SIZE) &&
Zhu Yib481de92007-09-25 17:54:57 -0700574 !(cmd->meta.flags & CMD_SIZE_HUGE));
575
Gregory Greenmanc342a1b2008-02-06 11:20:40 -0800576
577 if (iwl3945_is_rfkill(priv)) {
578 IWL_DEBUG_INFO("Not sending command - RF KILL");
579 return -EIO;
580 }
581
Samuel Ortizd20b3c62008-12-19 10:37:15 +0800582 if (iwl_queue_space(q) < ((cmd->meta.flags & CMD_ASYNC) ? 2 : 1)) {
Zhu Yib481de92007-09-25 17:54:57 -0700583 IWL_ERROR("No space for Tx\n");
584 return -ENOSPC;
585 }
586
587 spin_lock_irqsave(&priv->hcmd_lock, flags);
588
Tomas Winklerfc4b6852007-10-25 17:15:24 +0800589 tfd = &txq->bd[q->write_ptr];
Zhu Yib481de92007-09-25 17:54:57 -0700590 memset(tfd, 0, sizeof(*tfd));
591
592 control_flags = (u32 *) tfd;
593
Tomas Winklerfc4b6852007-10-25 17:15:24 +0800594 idx = get_cmd_index(q, q->write_ptr, cmd->meta.flags & CMD_SIZE_HUGE);
Zhu Yib481de92007-09-25 17:54:57 -0700595 out_cmd = &txq->cmd[idx];
596
597 out_cmd->hdr.cmd = cmd->id;
598 memcpy(&out_cmd->meta, &cmd->meta, sizeof(cmd->meta));
599 memcpy(&out_cmd->cmd.payload, cmd->data, cmd->len);
600
601 /* At this point, the out_cmd now has all of the incoming cmd
602 * information */
603
604 out_cmd->hdr.flags = 0;
605 out_cmd->hdr.sequence = cpu_to_le16(QUEUE_TO_SEQ(IWL_CMD_QUEUE_NUM) |
Tomas Winklerfc4b6852007-10-25 17:15:24 +0800606 INDEX_TO_SEQ(q->write_ptr));
Zhu Yib481de92007-09-25 17:54:57 -0700607 if (out_cmd->meta.flags & CMD_SIZE_HUGE)
Tomas Winkler600c0e12008-12-19 10:37:04 +0800608 out_cmd->hdr.sequence |= SEQ_HUGE_FRAME;
Zhu Yib481de92007-09-25 17:54:57 -0700609
610 phys_addr = txq->dma_addr_cmd + sizeof(txq->cmd[0]) * idx +
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800611 offsetof(struct iwl3945_cmd, hdr);
612 iwl3945_hw_txq_attach_buf_to_tfd(priv, tfd, phys_addr, fix_size);
Zhu Yib481de92007-09-25 17:54:57 -0700613
614 pad = U32_PAD(cmd->len);
615 count = TFD_CTL_COUNT_GET(*control_flags);
616 *control_flags = TFD_CTL_COUNT_SET(count) | TFD_CTL_PAD_SET(pad);
617
618 IWL_DEBUG_HC("Sending command %s (#%x), seq: 0x%04X, "
619 "%d bytes at %d[%d]:%d\n",
620 get_cmd_string(out_cmd->hdr.cmd),
621 out_cmd->hdr.cmd, le16_to_cpu(out_cmd->hdr.sequence),
Tomas Winklerfc4b6852007-10-25 17:15:24 +0800622 fix_size, q->write_ptr, idx, IWL_CMD_QUEUE_NUM);
Zhu Yib481de92007-09-25 17:54:57 -0700623
624 txq->need_update = 1;
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800625
626 /* Increment and update queue's write index */
Tomas Winklerc54b6792008-03-06 17:36:53 -0800627 q->write_ptr = iwl_queue_inc_wrap(q->write_ptr, q->n_bd);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800628 ret = iwl3945_tx_queue_update_write_ptr(priv, txq);
Zhu Yib481de92007-09-25 17:54:57 -0700629
630 spin_unlock_irqrestore(&priv->hcmd_lock, flags);
631 return ret ? ret : idx;
632}
633
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800634static int iwl3945_send_cmd_async(struct iwl3945_priv *priv, struct iwl3945_host_cmd *cmd)
Zhu Yib481de92007-09-25 17:54:57 -0700635{
636 int ret;
637
638 BUG_ON(!(cmd->meta.flags & CMD_ASYNC));
639
640 /* An asynchronous command can not expect an SKB to be set. */
641 BUG_ON(cmd->meta.flags & CMD_WANT_SKB);
642
643 /* An asynchronous command MUST have a callback. */
644 BUG_ON(!cmd->meta.u.callback);
645
646 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
647 return -EBUSY;
648
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800649 ret = iwl3945_enqueue_hcmd(priv, cmd);
Zhu Yib481de92007-09-25 17:54:57 -0700650 if (ret < 0) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800651 IWL_ERROR("Error sending %s: iwl3945_enqueue_hcmd failed: %d\n",
Zhu Yib481de92007-09-25 17:54:57 -0700652 get_cmd_string(cmd->id), ret);
653 return ret;
654 }
655 return 0;
656}
657
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800658static int iwl3945_send_cmd_sync(struct iwl3945_priv *priv, struct iwl3945_host_cmd *cmd)
Zhu Yib481de92007-09-25 17:54:57 -0700659{
660 int cmd_idx;
661 int ret;
Zhu Yib481de92007-09-25 17:54:57 -0700662
663 BUG_ON(cmd->meta.flags & CMD_ASYNC);
664
665 /* A synchronous command can not have a callback set. */
666 BUG_ON(cmd->meta.u.callback != NULL);
667
Tomas Winklere5472972008-03-28 16:21:12 -0700668 if (test_and_set_bit(STATUS_HCMD_SYNC_ACTIVE, &priv->status)) {
Zhu Yib481de92007-09-25 17:54:57 -0700669 IWL_ERROR("Error sending %s: Already sending a host command\n",
670 get_cmd_string(cmd->id));
Tomas Winklere5472972008-03-28 16:21:12 -0700671 ret = -EBUSY;
672 goto out;
Zhu Yib481de92007-09-25 17:54:57 -0700673 }
674
675 set_bit(STATUS_HCMD_ACTIVE, &priv->status);
676
677 if (cmd->meta.flags & CMD_WANT_SKB)
678 cmd->meta.source = &cmd->meta;
679
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800680 cmd_idx = iwl3945_enqueue_hcmd(priv, cmd);
Zhu Yib481de92007-09-25 17:54:57 -0700681 if (cmd_idx < 0) {
682 ret = cmd_idx;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800683 IWL_ERROR("Error sending %s: iwl3945_enqueue_hcmd failed: %d\n",
Zhu Yib481de92007-09-25 17:54:57 -0700684 get_cmd_string(cmd->id), ret);
685 goto out;
686 }
687
688 ret = wait_event_interruptible_timeout(priv->wait_command_queue,
689 !test_bit(STATUS_HCMD_ACTIVE, &priv->status),
690 HOST_COMPLETE_TIMEOUT);
691 if (!ret) {
692 if (test_bit(STATUS_HCMD_ACTIVE, &priv->status)) {
693 IWL_ERROR("Error sending %s: time out after %dms.\n",
694 get_cmd_string(cmd->id),
695 jiffies_to_msecs(HOST_COMPLETE_TIMEOUT));
696
697 clear_bit(STATUS_HCMD_ACTIVE, &priv->status);
698 ret = -ETIMEDOUT;
699 goto cancel;
700 }
701 }
702
703 if (test_bit(STATUS_RF_KILL_HW, &priv->status)) {
704 IWL_DEBUG_INFO("Command %s aborted: RF KILL Switch\n",
705 get_cmd_string(cmd->id));
706 ret = -ECANCELED;
707 goto fail;
708 }
709 if (test_bit(STATUS_FW_ERROR, &priv->status)) {
710 IWL_DEBUG_INFO("Command %s failed: FW Error\n",
711 get_cmd_string(cmd->id));
712 ret = -EIO;
713 goto fail;
714 }
715 if ((cmd->meta.flags & CMD_WANT_SKB) && !cmd->meta.u.skb) {
716 IWL_ERROR("Error: Response NULL in '%s'\n",
717 get_cmd_string(cmd->id));
718 ret = -EIO;
Zhu Yi73e1a652009-01-08 10:19:58 -0800719 goto cancel;
Zhu Yib481de92007-09-25 17:54:57 -0700720 }
721
722 ret = 0;
723 goto out;
724
725cancel:
726 if (cmd->meta.flags & CMD_WANT_SKB) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800727 struct iwl3945_cmd *qcmd;
Zhu Yib481de92007-09-25 17:54:57 -0700728
729 /* Cancel the CMD_WANT_SKB flag for the cmd in the
730 * TX cmd queue. Otherwise in case the cmd comes
731 * in later, it will possibly set an invalid
732 * address (cmd->meta.source). */
733 qcmd = &priv->txq[IWL_CMD_QUEUE_NUM].cmd[cmd_idx];
734 qcmd->meta.flags &= ~CMD_WANT_SKB;
735 }
736fail:
737 if (cmd->meta.u.skb) {
738 dev_kfree_skb_any(cmd->meta.u.skb);
739 cmd->meta.u.skb = NULL;
740 }
741out:
Tomas Winklere5472972008-03-28 16:21:12 -0700742 clear_bit(STATUS_HCMD_SYNC_ACTIVE, &priv->status);
Zhu Yib481de92007-09-25 17:54:57 -0700743 return ret;
744}
745
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800746int iwl3945_send_cmd(struct iwl3945_priv *priv, struct iwl3945_host_cmd *cmd)
Zhu Yib481de92007-09-25 17:54:57 -0700747{
Zhu Yib481de92007-09-25 17:54:57 -0700748 if (cmd->meta.flags & CMD_ASYNC)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800749 return iwl3945_send_cmd_async(priv, cmd);
Zhu Yib481de92007-09-25 17:54:57 -0700750
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800751 return iwl3945_send_cmd_sync(priv, cmd);
Zhu Yib481de92007-09-25 17:54:57 -0700752}
753
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800754int iwl3945_send_cmd_pdu(struct iwl3945_priv *priv, u8 id, u16 len, const void *data)
Zhu Yib481de92007-09-25 17:54:57 -0700755{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800756 struct iwl3945_host_cmd cmd = {
Zhu Yib481de92007-09-25 17:54:57 -0700757 .id = id,
758 .len = len,
759 .data = data,
760 };
761
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800762 return iwl3945_send_cmd_sync(priv, &cmd);
Zhu Yib481de92007-09-25 17:54:57 -0700763}
764
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800765static int __must_check iwl3945_send_cmd_u32(struct iwl3945_priv *priv, u8 id, u32 val)
Zhu Yib481de92007-09-25 17:54:57 -0700766{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800767 struct iwl3945_host_cmd cmd = {
Zhu Yib481de92007-09-25 17:54:57 -0700768 .id = id,
769 .len = sizeof(val),
770 .data = &val,
771 };
772
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800773 return iwl3945_send_cmd_sync(priv, &cmd);
Zhu Yib481de92007-09-25 17:54:57 -0700774}
775
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800776int iwl3945_send_statistics_request(struct iwl3945_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700777{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800778 return iwl3945_send_cmd_u32(priv, REPLY_STATISTICS_CMD, 0);
Zhu Yib481de92007-09-25 17:54:57 -0700779}
780
781/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800782 * iwl3945_set_rxon_channel - Set the phymode and channel values in staging RXON
Johannes Berg8318d782008-01-24 19:38:38 +0100783 * @band: 2.4 or 5 GHz band
784 * @channel: Any channel valid for the requested band
Zhu Yib481de92007-09-25 17:54:57 -0700785
Johannes Berg8318d782008-01-24 19:38:38 +0100786 * In addition to setting the staging RXON, priv->band is also set.
Zhu Yib481de92007-09-25 17:54:57 -0700787 *
788 * NOTE: Does not commit to the hardware; it sets appropriate bit fields
Johannes Berg8318d782008-01-24 19:38:38 +0100789 * in the staging RXON flag structure based on the band
Zhu Yib481de92007-09-25 17:54:57 -0700790 */
Johannes Berg8318d782008-01-24 19:38:38 +0100791static int iwl3945_set_rxon_channel(struct iwl3945_priv *priv,
792 enum ieee80211_band band,
793 u16 channel)
Zhu Yib481de92007-09-25 17:54:57 -0700794{
Johannes Berg8318d782008-01-24 19:38:38 +0100795 if (!iwl3945_get_channel_info(priv, band, channel)) {
Zhu Yib481de92007-09-25 17:54:57 -0700796 IWL_DEBUG_INFO("Could not set channel to %d [%d]\n",
Johannes Berg8318d782008-01-24 19:38:38 +0100797 channel, band);
Zhu Yib481de92007-09-25 17:54:57 -0700798 return -EINVAL;
799 }
800
801 if ((le16_to_cpu(priv->staging_rxon.channel) == channel) &&
Johannes Berg8318d782008-01-24 19:38:38 +0100802 (priv->band == band))
Zhu Yib481de92007-09-25 17:54:57 -0700803 return 0;
804
805 priv->staging_rxon.channel = cpu_to_le16(channel);
Johannes Berg8318d782008-01-24 19:38:38 +0100806 if (band == IEEE80211_BAND_5GHZ)
Zhu Yib481de92007-09-25 17:54:57 -0700807 priv->staging_rxon.flags &= ~RXON_FLG_BAND_24G_MSK;
808 else
809 priv->staging_rxon.flags |= RXON_FLG_BAND_24G_MSK;
810
Johannes Berg8318d782008-01-24 19:38:38 +0100811 priv->band = band;
Zhu Yib481de92007-09-25 17:54:57 -0700812
Johannes Berg8318d782008-01-24 19:38:38 +0100813 IWL_DEBUG_INFO("Staging channel set to %d [%d]\n", channel, band);
Zhu Yib481de92007-09-25 17:54:57 -0700814
815 return 0;
816}
817
818/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800819 * iwl3945_check_rxon_cmd - validate RXON structure is valid
Zhu Yib481de92007-09-25 17:54:57 -0700820 *
821 * NOTE: This is really only useful during development and can eventually
822 * be #ifdef'd out once the driver is stable and folks aren't actively
823 * making changes
824 */
Samuel Ortiza3139c52008-12-19 10:37:09 +0800825static int iwl3945_check_rxon_cmd(struct iwl3945_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700826{
827 int error = 0;
828 int counter = 1;
Samuel Ortiza3139c52008-12-19 10:37:09 +0800829 struct iwl3945_rxon_cmd *rxon = &priv->staging_rxon;
Zhu Yib481de92007-09-25 17:54:57 -0700830
831 if (rxon->flags & RXON_FLG_BAND_24G_MSK) {
832 error |= le32_to_cpu(rxon->flags &
833 (RXON_FLG_TGJ_NARROW_BAND_MSK |
834 RXON_FLG_RADAR_DETECT_MSK));
835 if (error)
836 IWL_WARNING("check 24G fields %d | %d\n",
837 counter++, error);
838 } else {
839 error |= (rxon->flags & RXON_FLG_SHORT_SLOT_MSK) ?
840 0 : le32_to_cpu(RXON_FLG_SHORT_SLOT_MSK);
841 if (error)
842 IWL_WARNING("check 52 fields %d | %d\n",
843 counter++, error);
844 error |= le32_to_cpu(rxon->flags & RXON_FLG_CCK_MSK);
845 if (error)
846 IWL_WARNING("check 52 CCK %d | %d\n",
847 counter++, error);
848 }
849 error |= (rxon->node_addr[0] | rxon->bssid_addr[0]) & 0x1;
850 if (error)
851 IWL_WARNING("check mac addr %d | %d\n", counter++, error);
852
853 /* make sure basic rates 6Mbps and 1Mbps are supported */
854 error |= (((rxon->ofdm_basic_rates & IWL_RATE_6M_MASK) == 0) &&
855 ((rxon->cck_basic_rates & IWL_RATE_1M_MASK) == 0));
856 if (error)
857 IWL_WARNING("check basic rate %d | %d\n", counter++, error);
858
859 error |= (le16_to_cpu(rxon->assoc_id) > 2007);
860 if (error)
861 IWL_WARNING("check assoc id %d | %d\n", counter++, error);
862
863 error |= ((rxon->flags & (RXON_FLG_CCK_MSK | RXON_FLG_SHORT_SLOT_MSK))
864 == (RXON_FLG_CCK_MSK | RXON_FLG_SHORT_SLOT_MSK));
865 if (error)
866 IWL_WARNING("check CCK and short slot %d | %d\n",
867 counter++, error);
868
869 error |= ((rxon->flags & (RXON_FLG_CCK_MSK | RXON_FLG_AUTO_DETECT_MSK))
870 == (RXON_FLG_CCK_MSK | RXON_FLG_AUTO_DETECT_MSK));
871 if (error)
872 IWL_WARNING("check CCK & auto detect %d | %d\n",
873 counter++, error);
874
875 error |= ((rxon->flags & (RXON_FLG_AUTO_DETECT_MSK |
876 RXON_FLG_TGG_PROTECT_MSK)) == RXON_FLG_TGG_PROTECT_MSK);
877 if (error)
878 IWL_WARNING("check TGG and auto detect %d | %d\n",
879 counter++, error);
880
881 if ((rxon->flags & RXON_FLG_DIS_DIV_MSK))
882 error |= ((rxon->flags & (RXON_FLG_ANT_B_MSK |
883 RXON_FLG_ANT_A_MSK)) == 0);
884 if (error)
885 IWL_WARNING("check antenna %d %d\n", counter++, error);
886
887 if (error)
888 IWL_WARNING("Tuning to channel %d\n",
889 le16_to_cpu(rxon->channel));
890
891 if (error) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800892 IWL_ERROR("Not a valid iwl3945_rxon_assoc_cmd field values\n");
Zhu Yib481de92007-09-25 17:54:57 -0700893 return -1;
894 }
895 return 0;
896}
897
898/**
Ben Cahill9fbab512007-11-29 11:09:47 +0800899 * iwl3945_full_rxon_required - check if full RXON (vs RXON_ASSOC) cmd is needed
Ian Schram01ebd062007-10-25 17:15:22 +0800900 * @priv: staging_rxon is compared to active_rxon
Zhu Yib481de92007-09-25 17:54:57 -0700901 *
Ben Cahill9fbab512007-11-29 11:09:47 +0800902 * If the RXON structure is changing enough to require a new tune,
903 * or is clearing the RXON_FILTER_ASSOC_MSK, then return 1 to indicate that
904 * a new tune (full RXON command, rather than RXON_ASSOC cmd) is required.
Zhu Yib481de92007-09-25 17:54:57 -0700905 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800906static int iwl3945_full_rxon_required(struct iwl3945_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700907{
908
909 /* These items are only settable from the full RXON command */
Ron Rindjunsky5d1e2322008-06-30 17:23:04 +0800910 if (!(iwl3945_is_associated(priv)) ||
Zhu Yib481de92007-09-25 17:54:57 -0700911 compare_ether_addr(priv->staging_rxon.bssid_addr,
912 priv->active_rxon.bssid_addr) ||
913 compare_ether_addr(priv->staging_rxon.node_addr,
914 priv->active_rxon.node_addr) ||
915 compare_ether_addr(priv->staging_rxon.wlap_bssid_addr,
916 priv->active_rxon.wlap_bssid_addr) ||
917 (priv->staging_rxon.dev_type != priv->active_rxon.dev_type) ||
918 (priv->staging_rxon.channel != priv->active_rxon.channel) ||
919 (priv->staging_rxon.air_propagation !=
920 priv->active_rxon.air_propagation) ||
921 (priv->staging_rxon.assoc_id != priv->active_rxon.assoc_id))
922 return 1;
923
924 /* flags, filter_flags, ofdm_basic_rates, and cck_basic_rates can
925 * be updated with the RXON_ASSOC command -- however only some
926 * flag transitions are allowed using RXON_ASSOC */
927
928 /* Check if we are not switching bands */
929 if ((priv->staging_rxon.flags & RXON_FLG_BAND_24G_MSK) !=
930 (priv->active_rxon.flags & RXON_FLG_BAND_24G_MSK))
931 return 1;
932
933 /* Check if we are switching association toggle */
934 if ((priv->staging_rxon.filter_flags & RXON_FILTER_ASSOC_MSK) !=
935 (priv->active_rxon.filter_flags & RXON_FILTER_ASSOC_MSK))
936 return 1;
937
938 return 0;
939}
940
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800941static int iwl3945_send_rxon_assoc(struct iwl3945_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700942{
943 int rc = 0;
Tomas Winkler3d24a9f2008-12-19 10:37:07 +0800944 struct iwl_rx_packet *res = NULL;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800945 struct iwl3945_rxon_assoc_cmd rxon_assoc;
946 struct iwl3945_host_cmd cmd = {
Zhu Yib481de92007-09-25 17:54:57 -0700947 .id = REPLY_RXON_ASSOC,
948 .len = sizeof(rxon_assoc),
949 .meta.flags = CMD_WANT_SKB,
950 .data = &rxon_assoc,
951 };
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800952 const struct iwl3945_rxon_cmd *rxon1 = &priv->staging_rxon;
953 const struct iwl3945_rxon_cmd *rxon2 = &priv->active_rxon;
Zhu Yib481de92007-09-25 17:54:57 -0700954
955 if ((rxon1->flags == rxon2->flags) &&
956 (rxon1->filter_flags == rxon2->filter_flags) &&
957 (rxon1->cck_basic_rates == rxon2->cck_basic_rates) &&
958 (rxon1->ofdm_basic_rates == rxon2->ofdm_basic_rates)) {
959 IWL_DEBUG_INFO("Using current RXON_ASSOC. Not resending.\n");
960 return 0;
961 }
962
963 rxon_assoc.flags = priv->staging_rxon.flags;
964 rxon_assoc.filter_flags = priv->staging_rxon.filter_flags;
965 rxon_assoc.ofdm_basic_rates = priv->staging_rxon.ofdm_basic_rates;
966 rxon_assoc.cck_basic_rates = priv->staging_rxon.cck_basic_rates;
967 rxon_assoc.reserved = 0;
968
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800969 rc = iwl3945_send_cmd_sync(priv, &cmd);
Zhu Yib481de92007-09-25 17:54:57 -0700970 if (rc)
971 return rc;
972
Tomas Winkler3d24a9f2008-12-19 10:37:07 +0800973 res = (struct iwl_rx_packet *)cmd.meta.u.skb->data;
Zhu Yib481de92007-09-25 17:54:57 -0700974 if (res->hdr.flags & IWL_CMD_FAILED_MSK) {
975 IWL_ERROR("Bad return from REPLY_RXON_ASSOC command\n");
976 rc = -EIO;
977 }
978
979 priv->alloc_rxb_skb--;
980 dev_kfree_skb_any(cmd.meta.u.skb);
981
982 return rc;
983}
984
985/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800986 * iwl3945_commit_rxon - commit staging_rxon to hardware
Zhu Yib481de92007-09-25 17:54:57 -0700987 *
Ian Schram01ebd062007-10-25 17:15:22 +0800988 * The RXON command in staging_rxon is committed to the hardware and
Zhu Yib481de92007-09-25 17:54:57 -0700989 * the active_rxon structure is updated with the new data. This
990 * function correctly transitions out of the RXON_ASSOC_MSK state if
991 * a HW tune is required based on the RXON structure changes.
992 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800993static int iwl3945_commit_rxon(struct iwl3945_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700994{
995 /* cast away the const for active_rxon in this function */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800996 struct iwl3945_rxon_cmd *active_rxon = (void *)&priv->active_rxon;
Zhu Yib481de92007-09-25 17:54:57 -0700997 int rc = 0;
998
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800999 if (!iwl3945_is_alive(priv))
Zhu Yib481de92007-09-25 17:54:57 -07001000 return -1;
1001
1002 /* always get timestamp with Rx frame */
1003 priv->staging_rxon.flags |= RXON_FLG_TSF2HOST_MSK;
1004
1005 /* select antenna */
1006 priv->staging_rxon.flags &=
1007 ~(RXON_FLG_DIS_DIV_MSK | RXON_FLG_ANT_SEL_MSK);
1008 priv->staging_rxon.flags |= iwl3945_get_antenna_flags(priv);
1009
Samuel Ortiza3139c52008-12-19 10:37:09 +08001010 rc = iwl3945_check_rxon_cmd(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001011 if (rc) {
1012 IWL_ERROR("Invalid RXON configuration. Not committing.\n");
1013 return -EINVAL;
1014 }
1015
1016 /* If we don't need to send a full RXON, we can use
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001017 * iwl3945_rxon_assoc_cmd which is used to reconfigure filter
Zhu Yib481de92007-09-25 17:54:57 -07001018 * and other flags for the current radio configuration. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001019 if (!iwl3945_full_rxon_required(priv)) {
1020 rc = iwl3945_send_rxon_assoc(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001021 if (rc) {
1022 IWL_ERROR("Error setting RXON_ASSOC "
1023 "configuration (%d).\n", rc);
1024 return rc;
1025 }
1026
1027 memcpy(active_rxon, &priv->staging_rxon, sizeof(*active_rxon));
1028
1029 return 0;
1030 }
1031
1032 /* If we are currently associated and the new config requires
1033 * an RXON_ASSOC and the new config wants the associated mask enabled,
1034 * we must clear the associated from the active configuration
1035 * before we apply the new config */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001036 if (iwl3945_is_associated(priv) &&
Zhu Yib481de92007-09-25 17:54:57 -07001037 (priv->staging_rxon.filter_flags & RXON_FILTER_ASSOC_MSK)) {
1038 IWL_DEBUG_INFO("Toggling associated bit on current RXON\n");
1039 active_rxon->filter_flags &= ~RXON_FILTER_ASSOC_MSK;
1040
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001041 rc = iwl3945_send_cmd_pdu(priv, REPLY_RXON,
1042 sizeof(struct iwl3945_rxon_cmd),
Zhu Yib481de92007-09-25 17:54:57 -07001043 &priv->active_rxon);
1044
1045 /* If the mask clearing failed then we set
1046 * active_rxon back to what it was previously */
1047 if (rc) {
1048 active_rxon->filter_flags |= RXON_FILTER_ASSOC_MSK;
1049 IWL_ERROR("Error clearing ASSOC_MSK on current "
1050 "configuration (%d).\n", rc);
1051 return rc;
1052 }
Zhu Yib481de92007-09-25 17:54:57 -07001053 }
1054
1055 IWL_DEBUG_INFO("Sending RXON\n"
1056 "* with%s RXON_FILTER_ASSOC_MSK\n"
1057 "* channel = %d\n"
Johannes Berge1749612008-10-27 15:59:26 -07001058 "* bssid = %pM\n",
Zhu Yib481de92007-09-25 17:54:57 -07001059 ((priv->staging_rxon.filter_flags &
1060 RXON_FILTER_ASSOC_MSK) ? "" : "out"),
1061 le16_to_cpu(priv->staging_rxon.channel),
Johannes Berge1749612008-10-27 15:59:26 -07001062 priv->staging_rxon.bssid_addr);
Zhu Yib481de92007-09-25 17:54:57 -07001063
1064 /* Apply the new configuration */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001065 rc = iwl3945_send_cmd_pdu(priv, REPLY_RXON,
1066 sizeof(struct iwl3945_rxon_cmd), &priv->staging_rxon);
Zhu Yib481de92007-09-25 17:54:57 -07001067 if (rc) {
1068 IWL_ERROR("Error setting new configuration (%d).\n", rc);
1069 return rc;
1070 }
1071
1072 memcpy(active_rxon, &priv->staging_rxon, sizeof(*active_rxon));
1073
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001074 iwl3945_clear_stations_table(priv);
Zhu Yi556f8db2007-09-27 11:27:33 +08001075
Zhu Yib481de92007-09-25 17:54:57 -07001076 /* If we issue a new RXON command which required a tune then we must
1077 * send a new TXPOWER command or we won't be able to Tx any frames */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001078 rc = iwl3945_hw_reg_send_txpower(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001079 if (rc) {
1080 IWL_ERROR("Error setting Tx power (%d).\n", rc);
1081 return rc;
1082 }
1083
1084 /* Add the broadcast address so we can send broadcast frames */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001085 if (iwl3945_add_station(priv, iwl3945_broadcast_addr, 0, 0) ==
Zhu Yib481de92007-09-25 17:54:57 -07001086 IWL_INVALID_STATION) {
1087 IWL_ERROR("Error adding BROADCAST address for transmit.\n");
1088 return -EIO;
1089 }
1090
1091 /* If we have set the ASSOC_MSK and we are in BSS mode then
1092 * add the IWL_AP_ID to the station rate table */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001093 if (iwl3945_is_associated(priv) &&
Johannes Berg05c914f2008-09-11 00:01:58 +02001094 (priv->iw_mode == NL80211_IFTYPE_STATION))
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001095 if (iwl3945_add_station(priv, priv->active_rxon.bssid_addr, 1, 0)
Zhu Yib481de92007-09-25 17:54:57 -07001096 == IWL_INVALID_STATION) {
1097 IWL_ERROR("Error adding AP address for transmit.\n");
1098 return -EIO;
1099 }
1100
Johannes Berg8318d782008-01-24 19:38:38 +01001101 /* Init the hardware's rate fallback order based on the band */
Zhu Yib481de92007-09-25 17:54:57 -07001102 rc = iwl3945_init_hw_rate_table(priv);
1103 if (rc) {
1104 IWL_ERROR("Error setting HW rate table: %02X\n", rc);
1105 return -EIO;
1106 }
1107
1108 return 0;
1109}
1110
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001111static int iwl3945_send_bt_config(struct iwl3945_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001112{
Tomas Winkler4c897252008-12-19 10:37:05 +08001113 struct iwl_bt_cmd bt_cmd = {
Zhu Yib481de92007-09-25 17:54:57 -07001114 .flags = 3,
1115 .lead_time = 0xAA,
1116 .max_kill = 1,
1117 .kill_ack_mask = 0,
1118 .kill_cts_mask = 0,
1119 };
1120
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001121 return iwl3945_send_cmd_pdu(priv, REPLY_BT_CONFIG,
Tomas Winkler4c897252008-12-19 10:37:05 +08001122 sizeof(bt_cmd), &bt_cmd);
Zhu Yib481de92007-09-25 17:54:57 -07001123}
1124
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001125static int iwl3945_send_scan_abort(struct iwl3945_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001126{
1127 int rc = 0;
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08001128 struct iwl_rx_packet *res;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001129 struct iwl3945_host_cmd cmd = {
Zhu Yib481de92007-09-25 17:54:57 -07001130 .id = REPLY_SCAN_ABORT_CMD,
1131 .meta.flags = CMD_WANT_SKB,
1132 };
1133
1134 /* If there isn't a scan actively going on in the hardware
1135 * then we are in between scan bands and not actually
1136 * actively scanning, so don't send the abort command */
1137 if (!test_bit(STATUS_SCAN_HW, &priv->status)) {
1138 clear_bit(STATUS_SCAN_ABORTING, &priv->status);
1139 return 0;
1140 }
1141
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001142 rc = iwl3945_send_cmd_sync(priv, &cmd);
Zhu Yib481de92007-09-25 17:54:57 -07001143 if (rc) {
1144 clear_bit(STATUS_SCAN_ABORTING, &priv->status);
1145 return rc;
1146 }
1147
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08001148 res = (struct iwl_rx_packet *)cmd.meta.u.skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07001149 if (res->u.status != CAN_ABORT_STATUS) {
1150 /* The scan abort will return 1 for success or
1151 * 2 for "failure". A failure condition can be
1152 * due to simply not being in an active scan which
1153 * can occur if we send the scan abort before we
1154 * the microcode has notified us that a scan is
1155 * completed. */
1156 IWL_DEBUG_INFO("SCAN_ABORT returned %d.\n", res->u.status);
1157 clear_bit(STATUS_SCAN_ABORTING, &priv->status);
1158 clear_bit(STATUS_SCAN_HW, &priv->status);
1159 }
1160
1161 dev_kfree_skb_any(cmd.meta.u.skb);
1162
1163 return rc;
1164}
1165
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001166static int iwl3945_card_state_sync_callback(struct iwl3945_priv *priv,
1167 struct iwl3945_cmd *cmd,
Zhu Yib481de92007-09-25 17:54:57 -07001168 struct sk_buff *skb)
1169{
1170 return 1;
1171}
1172
1173/*
1174 * CARD_STATE_CMD
1175 *
Ben Cahill9fbab512007-11-29 11:09:47 +08001176 * Use: Sets the device's internal card state to enable, disable, or halt
Zhu Yib481de92007-09-25 17:54:57 -07001177 *
1178 * When in the 'enable' state the card operates as normal.
1179 * When in the 'disable' state, the card enters into a low power mode.
1180 * When in the 'halt' state, the card is shut down and must be fully
1181 * restarted to come back on.
1182 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001183static int iwl3945_send_card_state(struct iwl3945_priv *priv, u32 flags, u8 meta_flag)
Zhu Yib481de92007-09-25 17:54:57 -07001184{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001185 struct iwl3945_host_cmd cmd = {
Zhu Yib481de92007-09-25 17:54:57 -07001186 .id = REPLY_CARD_STATE_CMD,
1187 .len = sizeof(u32),
1188 .data = &flags,
1189 .meta.flags = meta_flag,
1190 };
1191
1192 if (meta_flag & CMD_ASYNC)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001193 cmd.meta.u.callback = iwl3945_card_state_sync_callback;
Zhu Yib481de92007-09-25 17:54:57 -07001194
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001195 return iwl3945_send_cmd(priv, &cmd);
Zhu Yib481de92007-09-25 17:54:57 -07001196}
1197
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001198static int iwl3945_add_sta_sync_callback(struct iwl3945_priv *priv,
1199 struct iwl3945_cmd *cmd, struct sk_buff *skb)
Zhu Yib481de92007-09-25 17:54:57 -07001200{
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08001201 struct iwl_rx_packet *res = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07001202
1203 if (!skb) {
1204 IWL_ERROR("Error: Response NULL in REPLY_ADD_STA.\n");
1205 return 1;
1206 }
1207
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08001208 res = (struct iwl_rx_packet *)skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07001209 if (res->hdr.flags & IWL_CMD_FAILED_MSK) {
1210 IWL_ERROR("Bad return from REPLY_ADD_STA (0x%08X)\n",
1211 res->hdr.flags);
1212 return 1;
1213 }
1214
1215 switch (res->u.add_sta.status) {
1216 case ADD_STA_SUCCESS_MSK:
1217 break;
1218 default:
1219 break;
1220 }
1221
1222 /* We didn't cache the SKB; let the caller free it */
1223 return 1;
1224}
1225
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001226int iwl3945_send_add_station(struct iwl3945_priv *priv,
1227 struct iwl3945_addsta_cmd *sta, u8 flags)
Zhu Yib481de92007-09-25 17:54:57 -07001228{
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08001229 struct iwl_rx_packet *res = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07001230 int rc = 0;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001231 struct iwl3945_host_cmd cmd = {
Zhu Yib481de92007-09-25 17:54:57 -07001232 .id = REPLY_ADD_STA,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001233 .len = sizeof(struct iwl3945_addsta_cmd),
Zhu Yib481de92007-09-25 17:54:57 -07001234 .meta.flags = flags,
1235 .data = sta,
1236 };
1237
1238 if (flags & CMD_ASYNC)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001239 cmd.meta.u.callback = iwl3945_add_sta_sync_callback;
Zhu Yib481de92007-09-25 17:54:57 -07001240 else
1241 cmd.meta.flags |= CMD_WANT_SKB;
1242
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001243 rc = iwl3945_send_cmd(priv, &cmd);
Zhu Yib481de92007-09-25 17:54:57 -07001244
1245 if (rc || (flags & CMD_ASYNC))
1246 return rc;
1247
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08001248 res = (struct iwl_rx_packet *)cmd.meta.u.skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07001249 if (res->hdr.flags & IWL_CMD_FAILED_MSK) {
1250 IWL_ERROR("Bad return from REPLY_ADD_STA (0x%08X)\n",
1251 res->hdr.flags);
1252 rc = -EIO;
1253 }
1254
1255 if (rc == 0) {
1256 switch (res->u.add_sta.status) {
1257 case ADD_STA_SUCCESS_MSK:
1258 IWL_DEBUG_INFO("REPLY_ADD_STA PASSED\n");
1259 break;
1260 default:
1261 rc = -EIO;
1262 IWL_WARNING("REPLY_ADD_STA failed\n");
1263 break;
1264 }
1265 }
1266
1267 priv->alloc_rxb_skb--;
1268 dev_kfree_skb_any(cmd.meta.u.skb);
1269
1270 return rc;
1271}
1272
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001273static int iwl3945_update_sta_key_info(struct iwl3945_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07001274 struct ieee80211_key_conf *keyconf,
1275 u8 sta_id)
1276{
1277 unsigned long flags;
1278 __le16 key_flags = 0;
1279
1280 switch (keyconf->alg) {
1281 case ALG_CCMP:
1282 key_flags |= STA_KEY_FLG_CCMP;
1283 key_flags |= cpu_to_le16(
1284 keyconf->keyidx << STA_KEY_FLG_KEYID_POS);
1285 key_flags &= ~STA_KEY_FLG_INVALID;
1286 break;
1287 case ALG_TKIP:
1288 case ALG_WEP:
Zhu Yib481de92007-09-25 17:54:57 -07001289 default:
1290 return -EINVAL;
1291 }
1292 spin_lock_irqsave(&priv->sta_lock, flags);
1293 priv->stations[sta_id].keyinfo.alg = keyconf->alg;
1294 priv->stations[sta_id].keyinfo.keylen = keyconf->keylen;
1295 memcpy(priv->stations[sta_id].keyinfo.key, keyconf->key,
1296 keyconf->keylen);
1297
1298 memcpy(priv->stations[sta_id].sta.key.key, keyconf->key,
1299 keyconf->keylen);
1300 priv->stations[sta_id].sta.key.key_flags = key_flags;
1301 priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
1302 priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
1303
1304 spin_unlock_irqrestore(&priv->sta_lock, flags);
1305
1306 IWL_DEBUG_INFO("hwcrypto: modify ucode station key info\n");
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001307 iwl3945_send_add_station(priv, &priv->stations[sta_id].sta, 0);
Zhu Yib481de92007-09-25 17:54:57 -07001308 return 0;
1309}
1310
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001311static int iwl3945_clear_sta_key_info(struct iwl3945_priv *priv, u8 sta_id)
Zhu Yib481de92007-09-25 17:54:57 -07001312{
1313 unsigned long flags;
1314
1315 spin_lock_irqsave(&priv->sta_lock, flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001316 memset(&priv->stations[sta_id].keyinfo, 0, sizeof(struct iwl3945_hw_key));
Tomas Winkler4c897252008-12-19 10:37:05 +08001317 memset(&priv->stations[sta_id].sta.key, 0,
1318 sizeof(struct iwl4965_keyinfo));
Zhu Yib481de92007-09-25 17:54:57 -07001319 priv->stations[sta_id].sta.key.key_flags = STA_KEY_FLG_NO_ENC;
1320 priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
1321 priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
1322 spin_unlock_irqrestore(&priv->sta_lock, flags);
1323
1324 IWL_DEBUG_INFO("hwcrypto: clear ucode station key info\n");
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001325 iwl3945_send_add_station(priv, &priv->stations[sta_id].sta, 0);
Zhu Yib481de92007-09-25 17:54:57 -07001326 return 0;
1327}
1328
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001329static void iwl3945_clear_free_frames(struct iwl3945_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001330{
1331 struct list_head *element;
1332
1333 IWL_DEBUG_INFO("%d frames on pre-allocated heap on clear.\n",
1334 priv->frames_count);
1335
1336 while (!list_empty(&priv->free_frames)) {
1337 element = priv->free_frames.next;
1338 list_del(element);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001339 kfree(list_entry(element, struct iwl3945_frame, list));
Zhu Yib481de92007-09-25 17:54:57 -07001340 priv->frames_count--;
1341 }
1342
1343 if (priv->frames_count) {
1344 IWL_WARNING("%d frames still in use. Did we lose one?\n",
1345 priv->frames_count);
1346 priv->frames_count = 0;
1347 }
1348}
1349
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001350static struct iwl3945_frame *iwl3945_get_free_frame(struct iwl3945_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001351{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001352 struct iwl3945_frame *frame;
Zhu Yib481de92007-09-25 17:54:57 -07001353 struct list_head *element;
1354 if (list_empty(&priv->free_frames)) {
1355 frame = kzalloc(sizeof(*frame), GFP_KERNEL);
1356 if (!frame) {
1357 IWL_ERROR("Could not allocate frame!\n");
1358 return NULL;
1359 }
1360
1361 priv->frames_count++;
1362 return frame;
1363 }
1364
1365 element = priv->free_frames.next;
1366 list_del(element);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001367 return list_entry(element, struct iwl3945_frame, list);
Zhu Yib481de92007-09-25 17:54:57 -07001368}
1369
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001370static void iwl3945_free_frame(struct iwl3945_priv *priv, struct iwl3945_frame *frame)
Zhu Yib481de92007-09-25 17:54:57 -07001371{
1372 memset(frame, 0, sizeof(*frame));
1373 list_add(&frame->list, &priv->free_frames);
1374}
1375
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001376unsigned int iwl3945_fill_beacon_frame(struct iwl3945_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07001377 struct ieee80211_hdr *hdr,
Rami Rosen73ec1cc2008-12-16 09:37:07 +02001378 int left)
Zhu Yib481de92007-09-25 17:54:57 -07001379{
1380
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001381 if (!iwl3945_is_associated(priv) || !priv->ibss_beacon ||
Johannes Berg05c914f2008-09-11 00:01:58 +02001382 ((priv->iw_mode != NL80211_IFTYPE_ADHOC) &&
1383 (priv->iw_mode != NL80211_IFTYPE_AP)))
Zhu Yib481de92007-09-25 17:54:57 -07001384 return 0;
1385
1386 if (priv->ibss_beacon->len > left)
1387 return 0;
1388
1389 memcpy(hdr, priv->ibss_beacon->data, priv->ibss_beacon->len);
1390
1391 return priv->ibss_beacon->len;
1392}
1393
Kolekar, Abhijeetc24f0812008-11-07 09:58:44 -08001394static u8 iwl3945_rate_get_lowest_plcp(struct iwl3945_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001395{
1396 u8 i;
Kolekar, Abhijeetc24f0812008-11-07 09:58:44 -08001397 int rate_mask;
1398
1399 /* Set rate mask*/
1400 if (priv->staging_rxon.flags & RXON_FLG_BAND_24G_MSK)
Chatre, Reinettedbce56a2008-11-12 13:14:07 -08001401 rate_mask = priv->active_rate_basic & IWL_CCK_RATES_MASK;
Kolekar, Abhijeetc24f0812008-11-07 09:58:44 -08001402 else
Chatre, Reinettedbce56a2008-11-12 13:14:07 -08001403 rate_mask = priv->active_rate_basic & IWL_OFDM_RATES_MASK;
Zhu Yib481de92007-09-25 17:54:57 -07001404
1405 for (i = IWL_RATE_1M_INDEX; i != IWL_RATE_INVALID;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001406 i = iwl3945_rates[i].next_ieee) {
Zhu Yib481de92007-09-25 17:54:57 -07001407 if (rate_mask & (1 << i))
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001408 return iwl3945_rates[i].plcp;
Zhu Yib481de92007-09-25 17:54:57 -07001409 }
1410
Kolekar, Abhijeetc24f0812008-11-07 09:58:44 -08001411 /* No valid rate was found. Assign the lowest one */
1412 if (priv->staging_rxon.flags & RXON_FLG_BAND_24G_MSK)
1413 return IWL_RATE_1M_PLCP;
1414 else
1415 return IWL_RATE_6M_PLCP;
Zhu Yib481de92007-09-25 17:54:57 -07001416}
1417
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001418static int iwl3945_send_beacon_cmd(struct iwl3945_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001419{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001420 struct iwl3945_frame *frame;
Zhu Yib481de92007-09-25 17:54:57 -07001421 unsigned int frame_size;
1422 int rc;
1423 u8 rate;
1424
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001425 frame = iwl3945_get_free_frame(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001426
1427 if (!frame) {
1428 IWL_ERROR("Could not obtain free frame buffer for beacon "
1429 "command.\n");
1430 return -ENOMEM;
1431 }
1432
Kolekar, Abhijeetc24f0812008-11-07 09:58:44 -08001433 rate = iwl3945_rate_get_lowest_plcp(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001434
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001435 frame_size = iwl3945_hw_get_beacon_cmd(priv, frame, rate);
Zhu Yib481de92007-09-25 17:54:57 -07001436
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001437 rc = iwl3945_send_cmd_pdu(priv, REPLY_TX_BEACON, frame_size,
Zhu Yib481de92007-09-25 17:54:57 -07001438 &frame->u.cmd[0]);
1439
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001440 iwl3945_free_frame(priv, frame);
Zhu Yib481de92007-09-25 17:54:57 -07001441
1442 return rc;
1443}
1444
1445/******************************************************************************
1446 *
1447 * EEPROM related functions
1448 *
1449 ******************************************************************************/
1450
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001451static void get_eeprom_mac(struct iwl3945_priv *priv, u8 *mac)
Zhu Yib481de92007-09-25 17:54:57 -07001452{
1453 memcpy(mac, priv->eeprom.mac_address, 6);
1454}
1455
Reinette Chatre74a3a252008-01-23 10:15:19 -08001456/*
1457 * Clear the OWNER_MSK, to establish driver (instead of uCode running on
1458 * embedded controller) as EEPROM reader; each read is a series of pulses
1459 * to/from the EEPROM chip, not a single event, so even reads could conflict
1460 * if they weren't arbitrated by some ownership mechanism. Here, the driver
1461 * simply claims ownership, which should be safe when this function is called
1462 * (i.e. before loading uCode!).
1463 */
1464static inline int iwl3945_eeprom_acquire_semaphore(struct iwl3945_priv *priv)
1465{
1466 _iwl3945_clear_bit(priv, CSR_EEPROM_GP, CSR_EEPROM_GP_IF_OWNER_MSK);
1467 return 0;
1468}
1469
Zhu Yib481de92007-09-25 17:54:57 -07001470/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001471 * iwl3945_eeprom_init - read EEPROM contents
Zhu Yib481de92007-09-25 17:54:57 -07001472 *
Cahill, Ben M6440adb2007-11-29 11:09:55 +08001473 * Load the EEPROM contents from adapter into priv->eeprom
Zhu Yib481de92007-09-25 17:54:57 -07001474 *
1475 * NOTE: This routine uses the non-debug IO access functions.
1476 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001477int iwl3945_eeprom_init(struct iwl3945_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001478{
Tomas Winkler58ff6d42008-02-13 02:47:54 +02001479 u16 *e = (u16 *)&priv->eeprom;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001480 u32 gp = iwl3945_read32(priv, CSR_EEPROM_GP);
Zhu Yib481de92007-09-25 17:54:57 -07001481 int sz = sizeof(priv->eeprom);
Zhu, Yi3d5717a2008-12-11 10:33:36 -08001482 int ret;
Zhu Yib481de92007-09-25 17:54:57 -07001483 u16 addr;
1484
1485 /* The EEPROM structure has several padding buffers within it
1486 * and when adding new EEPROM maps is subject to programmer errors
1487 * which may be very difficult to identify without explicitly
1488 * checking the resulting size of the eeprom map. */
1489 BUILD_BUG_ON(sizeof(priv->eeprom) != IWL_EEPROM_IMAGE_SIZE);
1490
1491 if ((gp & CSR_EEPROM_GP_VALID_MSK) == CSR_EEPROM_GP_BAD_SIGNATURE) {
Jiri Slaby6f147922008-08-11 23:49:41 +02001492 IWL_ERROR("EEPROM not found, EEPROM_GP=0x%08x\n", gp);
Zhu Yib481de92007-09-25 17:54:57 -07001493 return -ENOENT;
1494 }
1495
Cahill, Ben M6440adb2007-11-29 11:09:55 +08001496 /* Make sure driver (instead of uCode) is allowed to read EEPROM */
Zhu, Yi3d5717a2008-12-11 10:33:36 -08001497 ret = iwl3945_eeprom_acquire_semaphore(priv);
1498 if (ret < 0) {
Ian Schram91e17472007-10-25 17:15:23 +08001499 IWL_ERROR("Failed to acquire EEPROM semaphore.\n");
Zhu Yib481de92007-09-25 17:54:57 -07001500 return -ENOENT;
1501 }
1502
1503 /* eeprom is an array of 16bit values */
1504 for (addr = 0; addr < sz; addr += sizeof(u16)) {
Zhu, Yi3d5717a2008-12-11 10:33:36 -08001505 u32 r;
1506
1507 _iwl3945_write32(priv, CSR_EEPROM_REG,
1508 CSR_EEPROM_REG_MSK_ADDR & (addr << 1));
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001509 _iwl3945_clear_bit(priv, CSR_EEPROM_REG, CSR_EEPROM_REG_BIT_CMD);
Zhu, Yi3d5717a2008-12-11 10:33:36 -08001510 ret = iwl3945_poll_direct_bit(priv, CSR_EEPROM_REG,
1511 CSR_EEPROM_REG_READ_VALID_MSK,
1512 IWL_EEPROM_ACCESS_TIMEOUT);
1513 if (ret < 0) {
Jiri Slaby6f147922008-08-11 23:49:41 +02001514 IWL_ERROR("Time out reading EEPROM[%d]\n", addr);
Zhu, Yi3d5717a2008-12-11 10:33:36 -08001515 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07001516 }
Zhu, Yi3d5717a2008-12-11 10:33:36 -08001517
1518 r = _iwl3945_read_direct32(priv, CSR_EEPROM_REG);
Tomas Winkler58ff6d42008-02-13 02:47:54 +02001519 e[addr / 2] = le16_to_cpu((__force __le16)(r >> 16));
Zhu Yib481de92007-09-25 17:54:57 -07001520 }
1521
1522 return 0;
1523}
1524
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001525static void iwl3945_unset_hw_setting(struct iwl3945_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001526{
1527 if (priv->hw_setting.shared_virt)
1528 pci_free_consistent(priv->pci_dev,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001529 sizeof(struct iwl3945_shared),
Zhu Yib481de92007-09-25 17:54:57 -07001530 priv->hw_setting.shared_virt,
1531 priv->hw_setting.shared_phys);
1532}
1533
1534/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001535 * iwl3945_supported_rate_to_ie - fill in the supported rate in IE field
Zhu Yib481de92007-09-25 17:54:57 -07001536 *
1537 * return : set the bit for each supported rate insert in ie
1538 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001539static u16 iwl3945_supported_rate_to_ie(u8 *ie, u16 supported_rate,
Tomas Winklerc7c46672007-10-18 02:04:15 +02001540 u16 basic_rate, int *left)
Zhu Yib481de92007-09-25 17:54:57 -07001541{
1542 u16 ret_rates = 0, bit;
1543 int i;
Tomas Winklerc7c46672007-10-18 02:04:15 +02001544 u8 *cnt = ie;
1545 u8 *rates = ie + 1;
Zhu Yib481de92007-09-25 17:54:57 -07001546
1547 for (bit = 1, i = 0; i < IWL_RATE_COUNT; i++, bit <<= 1) {
1548 if (bit & supported_rate) {
1549 ret_rates |= bit;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001550 rates[*cnt] = iwl3945_rates[i].ieee |
Tomas Winklerc7c46672007-10-18 02:04:15 +02001551 ((bit & basic_rate) ? 0x80 : 0x00);
1552 (*cnt)++;
1553 (*left)--;
1554 if ((*left <= 0) ||
1555 (*cnt >= IWL_SUPPORTED_RATES_IE_LEN))
Zhu Yib481de92007-09-25 17:54:57 -07001556 break;
1557 }
1558 }
1559
1560 return ret_rates;
1561}
1562
1563/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001564 * iwl3945_fill_probe_req - fill in all required fields and IE for probe request
Zhu Yib481de92007-09-25 17:54:57 -07001565 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001566static u16 iwl3945_fill_probe_req(struct iwl3945_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07001567 struct ieee80211_mgmt *frame,
Johannes Berg430cfe92008-10-28 18:06:02 +01001568 int left)
Zhu Yib481de92007-09-25 17:54:57 -07001569{
1570 int len = 0;
1571 u8 *pos = NULL;
Tomas Winklerc7c46672007-10-18 02:04:15 +02001572 u16 active_rates, ret_rates, cck_rates;
Zhu Yib481de92007-09-25 17:54:57 -07001573
1574 /* Make sure there is enough space for the probe request,
1575 * two mandatory IEs and the data */
1576 left -= 24;
1577 if (left < 0)
1578 return 0;
1579 len += 24;
1580
1581 frame->frame_control = cpu_to_le16(IEEE80211_STYPE_PROBE_REQ);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001582 memcpy(frame->da, iwl3945_broadcast_addr, ETH_ALEN);
Zhu Yib481de92007-09-25 17:54:57 -07001583 memcpy(frame->sa, priv->mac_addr, ETH_ALEN);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001584 memcpy(frame->bssid, iwl3945_broadcast_addr, ETH_ALEN);
Zhu Yib481de92007-09-25 17:54:57 -07001585 frame->seq_ctrl = 0;
1586
1587 /* fill in our indirect SSID IE */
1588 /* ...next IE... */
1589
1590 left -= 2;
1591 if (left < 0)
1592 return 0;
1593 len += 2;
1594 pos = &(frame->u.probe_req.variable[0]);
1595 *pos++ = WLAN_EID_SSID;
1596 *pos++ = 0;
1597
Zhu Yib481de92007-09-25 17:54:57 -07001598 /* fill in supported rate */
1599 /* ...next IE... */
1600 left -= 2;
1601 if (left < 0)
1602 return 0;
Tomas Winklerc7c46672007-10-18 02:04:15 +02001603
Zhu Yib481de92007-09-25 17:54:57 -07001604 /* ... fill it in... */
1605 *pos++ = WLAN_EID_SUPP_RATES;
1606 *pos = 0;
Tomas Winklerc7c46672007-10-18 02:04:15 +02001607
1608 priv->active_rate = priv->rates_mask;
1609 active_rates = priv->active_rate;
Zhu Yib481de92007-09-25 17:54:57 -07001610 priv->active_rate_basic = priv->rates_mask & IWL_BASIC_RATES_MASK;
1611
Tomas Winklerc7c46672007-10-18 02:04:15 +02001612 cck_rates = IWL_CCK_RATES_MASK & active_rates;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001613 ret_rates = iwl3945_supported_rate_to_ie(pos, cck_rates,
Tomas Winklerc7c46672007-10-18 02:04:15 +02001614 priv->active_rate_basic, &left);
1615 active_rates &= ~ret_rates;
1616
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001617 ret_rates = iwl3945_supported_rate_to_ie(pos, active_rates,
Tomas Winklerc7c46672007-10-18 02:04:15 +02001618 priv->active_rate_basic, &left);
1619 active_rates &= ~ret_rates;
1620
Zhu Yib481de92007-09-25 17:54:57 -07001621 len += 2 + *pos;
1622 pos += (*pos) + 1;
Tomas Winklerc7c46672007-10-18 02:04:15 +02001623 if (active_rates == 0)
Zhu Yib481de92007-09-25 17:54:57 -07001624 goto fill_end;
1625
1626 /* fill in supported extended rate */
1627 /* ...next IE... */
1628 left -= 2;
1629 if (left < 0)
1630 return 0;
1631 /* ... fill it in... */
1632 *pos++ = WLAN_EID_EXT_SUPP_RATES;
1633 *pos = 0;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001634 iwl3945_supported_rate_to_ie(pos, active_rates,
Tomas Winklerc7c46672007-10-18 02:04:15 +02001635 priv->active_rate_basic, &left);
Zhu Yib481de92007-09-25 17:54:57 -07001636 if (*pos > 0)
1637 len += 2 + *pos;
1638
1639 fill_end:
1640 return (u16)len;
1641}
1642
1643/*
1644 * QoS support
1645*/
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001646static int iwl3945_send_qos_params_command(struct iwl3945_priv *priv,
Tomas Winkler4c897252008-12-19 10:37:05 +08001647 struct iwl_qosparam_cmd *qos)
Zhu Yib481de92007-09-25 17:54:57 -07001648{
1649
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001650 return iwl3945_send_cmd_pdu(priv, REPLY_QOS_PARAM,
Tomas Winkler4c897252008-12-19 10:37:05 +08001651 sizeof(struct iwl_qosparam_cmd), qos);
Zhu Yib481de92007-09-25 17:54:57 -07001652}
1653
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001654static void iwl3945_reset_qos(struct iwl3945_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001655{
1656 u16 cw_min = 15;
1657 u16 cw_max = 1023;
1658 u8 aifs = 2;
1659 u8 is_legacy = 0;
1660 unsigned long flags;
1661 int i;
1662
1663 spin_lock_irqsave(&priv->lock, flags);
1664 priv->qos_data.qos_active = 0;
1665
Winkler, Tomas6d1ef1a2008-12-09 11:29:00 -08001666 /* QoS always active in AP and ADHOC mode
1667 * In STA mode wait for association
1668 */
1669 if (priv->iw_mode == NL80211_IFTYPE_ADHOC ||
1670 priv->iw_mode == NL80211_IFTYPE_AP)
1671 priv->qos_data.qos_active = 1;
1672 else
1673 priv->qos_data.qos_active = 0;
1674
1675
1676 /* check for legacy mode */
1677 if ((priv->iw_mode == NL80211_IFTYPE_ADHOC &&
1678 (priv->active_rate & IWL_OFDM_RATES_MASK) == 0) ||
1679 (priv->iw_mode == NL80211_IFTYPE_STATION &&
1680 (priv->staging_rxon.flags & RXON_FLG_SHORT_SLOT_MSK) == 0)) {
Zhu Yib481de92007-09-25 17:54:57 -07001681 cw_min = 31;
1682 is_legacy = 1;
1683 }
1684
1685 if (priv->qos_data.qos_active)
1686 aifs = 3;
1687
1688 priv->qos_data.def_qos_parm.ac[0].cw_min = cpu_to_le16(cw_min);
1689 priv->qos_data.def_qos_parm.ac[0].cw_max = cpu_to_le16(cw_max);
1690 priv->qos_data.def_qos_parm.ac[0].aifsn = aifs;
1691 priv->qos_data.def_qos_parm.ac[0].edca_txop = 0;
1692 priv->qos_data.def_qos_parm.ac[0].reserved1 = 0;
1693
1694 if (priv->qos_data.qos_active) {
1695 i = 1;
1696 priv->qos_data.def_qos_parm.ac[i].cw_min = cpu_to_le16(cw_min);
1697 priv->qos_data.def_qos_parm.ac[i].cw_max = cpu_to_le16(cw_max);
1698 priv->qos_data.def_qos_parm.ac[i].aifsn = 7;
1699 priv->qos_data.def_qos_parm.ac[i].edca_txop = 0;
1700 priv->qos_data.def_qos_parm.ac[i].reserved1 = 0;
1701
1702 i = 2;
1703 priv->qos_data.def_qos_parm.ac[i].cw_min =
1704 cpu_to_le16((cw_min + 1) / 2 - 1);
1705 priv->qos_data.def_qos_parm.ac[i].cw_max =
1706 cpu_to_le16(cw_max);
1707 priv->qos_data.def_qos_parm.ac[i].aifsn = 2;
1708 if (is_legacy)
1709 priv->qos_data.def_qos_parm.ac[i].edca_txop =
1710 cpu_to_le16(6016);
1711 else
1712 priv->qos_data.def_qos_parm.ac[i].edca_txop =
1713 cpu_to_le16(3008);
1714 priv->qos_data.def_qos_parm.ac[i].reserved1 = 0;
1715
1716 i = 3;
1717 priv->qos_data.def_qos_parm.ac[i].cw_min =
1718 cpu_to_le16((cw_min + 1) / 4 - 1);
1719 priv->qos_data.def_qos_parm.ac[i].cw_max =
1720 cpu_to_le16((cw_max + 1) / 2 - 1);
1721 priv->qos_data.def_qos_parm.ac[i].aifsn = 2;
1722 priv->qos_data.def_qos_parm.ac[i].reserved1 = 0;
1723 if (is_legacy)
1724 priv->qos_data.def_qos_parm.ac[i].edca_txop =
1725 cpu_to_le16(3264);
1726 else
1727 priv->qos_data.def_qos_parm.ac[i].edca_txop =
1728 cpu_to_le16(1504);
1729 } else {
1730 for (i = 1; i < 4; i++) {
1731 priv->qos_data.def_qos_parm.ac[i].cw_min =
1732 cpu_to_le16(cw_min);
1733 priv->qos_data.def_qos_parm.ac[i].cw_max =
1734 cpu_to_le16(cw_max);
1735 priv->qos_data.def_qos_parm.ac[i].aifsn = aifs;
1736 priv->qos_data.def_qos_parm.ac[i].edca_txop = 0;
1737 priv->qos_data.def_qos_parm.ac[i].reserved1 = 0;
1738 }
1739 }
1740 IWL_DEBUG_QOS("set QoS to default \n");
1741
1742 spin_unlock_irqrestore(&priv->lock, flags);
1743}
1744
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001745static void iwl3945_activate_qos(struct iwl3945_priv *priv, u8 force)
Zhu Yib481de92007-09-25 17:54:57 -07001746{
1747 unsigned long flags;
1748
Zhu Yib481de92007-09-25 17:54:57 -07001749 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
1750 return;
1751
Zhu Yib481de92007-09-25 17:54:57 -07001752 spin_lock_irqsave(&priv->lock, flags);
1753 priv->qos_data.def_qos_parm.qos_flags = 0;
1754
1755 if (priv->qos_data.qos_cap.q_AP.queue_request &&
1756 !priv->qos_data.qos_cap.q_AP.txop_request)
1757 priv->qos_data.def_qos_parm.qos_flags |=
1758 QOS_PARAM_FLG_TXOP_TYPE_MSK;
1759
1760 if (priv->qos_data.qos_active)
1761 priv->qos_data.def_qos_parm.qos_flags |=
1762 QOS_PARAM_FLG_UPDATE_EDCA_MSK;
1763
1764 spin_unlock_irqrestore(&priv->lock, flags);
1765
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001766 if (force || iwl3945_is_associated(priv)) {
Tomas Winklera96a27f2008-10-23 23:48:56 -07001767 IWL_DEBUG_QOS("send QoS cmd with QoS active %d \n",
Zhu Yib481de92007-09-25 17:54:57 -07001768 priv->qos_data.qos_active);
1769
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001770 iwl3945_send_qos_params_command(priv,
Zhu Yib481de92007-09-25 17:54:57 -07001771 &(priv->qos_data.def_qos_parm));
1772 }
1773}
1774
Zhu Yib481de92007-09-25 17:54:57 -07001775/*
1776 * Power management (not Tx power!) functions
1777 */
1778#define MSEC_TO_USEC 1024
1779
Tomas Winkler600c0e12008-12-19 10:37:04 +08001780
1781#define NOSLP __constant_cpu_to_le16(0), 0, 0
1782#define SLP IWL_POWER_DRIVER_ALLOW_SLEEP_MSK, 0, 0
Zhu Yib481de92007-09-25 17:54:57 -07001783#define SLP_TIMEOUT(T) __constant_cpu_to_le32((T) * MSEC_TO_USEC)
1784#define SLP_VEC(X0, X1, X2, X3, X4) {__constant_cpu_to_le32(X0), \
1785 __constant_cpu_to_le32(X1), \
1786 __constant_cpu_to_le32(X2), \
1787 __constant_cpu_to_le32(X3), \
1788 __constant_cpu_to_le32(X4)}
1789
Zhu Yib481de92007-09-25 17:54:57 -07001790/* default power management (not Tx power) table values */
Tomas Winklera96a27f2008-10-23 23:48:56 -07001791/* for TIM 0-10 */
Samuel Ortiz1125eff2008-12-19 10:37:14 +08001792static struct iwl_power_vec_entry range_0[IWL39_POWER_AC] = {
Zhu Yib481de92007-09-25 17:54:57 -07001793 {{NOSLP, SLP_TIMEOUT(0), SLP_TIMEOUT(0), SLP_VEC(0, 0, 0, 0, 0)}, 0},
1794 {{SLP, SLP_TIMEOUT(200), SLP_TIMEOUT(500), SLP_VEC(1, 2, 3, 4, 4)}, 0},
1795 {{SLP, SLP_TIMEOUT(200), SLP_TIMEOUT(300), SLP_VEC(2, 4, 6, 7, 7)}, 0},
1796 {{SLP, SLP_TIMEOUT(50), SLP_TIMEOUT(100), SLP_VEC(2, 6, 9, 9, 10)}, 0},
1797 {{SLP, SLP_TIMEOUT(50), SLP_TIMEOUT(25), SLP_VEC(2, 7, 9, 9, 10)}, 1},
1798 {{SLP, SLP_TIMEOUT(25), SLP_TIMEOUT(25), SLP_VEC(4, 7, 10, 10, 10)}, 1}
1799};
1800
Tomas Winklera96a27f2008-10-23 23:48:56 -07001801/* for TIM > 10 */
Samuel Ortiz1125eff2008-12-19 10:37:14 +08001802static struct iwl_power_vec_entry range_1[IWL39_POWER_AC] = {
Zhu Yib481de92007-09-25 17:54:57 -07001803 {{NOSLP, SLP_TIMEOUT(0), SLP_TIMEOUT(0), SLP_VEC(0, 0, 0, 0, 0)}, 0},
1804 {{SLP, SLP_TIMEOUT(200), SLP_TIMEOUT(500),
1805 SLP_VEC(1, 2, 3, 4, 0xFF)}, 0},
1806 {{SLP, SLP_TIMEOUT(200), SLP_TIMEOUT(300),
1807 SLP_VEC(2, 4, 6, 7, 0xFF)}, 0},
1808 {{SLP, SLP_TIMEOUT(50), SLP_TIMEOUT(100),
1809 SLP_VEC(2, 6, 9, 9, 0xFF)}, 0},
1810 {{SLP, SLP_TIMEOUT(50), SLP_TIMEOUT(25), SLP_VEC(2, 7, 9, 9, 0xFF)}, 0},
1811 {{SLP, SLP_TIMEOUT(25), SLP_TIMEOUT(25),
1812 SLP_VEC(4, 7, 10, 10, 0xFF)}, 0}
1813};
1814
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001815int iwl3945_power_init_handle(struct iwl3945_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001816{
1817 int rc = 0, i;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001818 struct iwl3945_power_mgr *pow_data;
Samuel Ortiz1125eff2008-12-19 10:37:14 +08001819 int size = sizeof(struct iwl_power_vec_entry) * IWL39_POWER_AC;
Zhu Yib481de92007-09-25 17:54:57 -07001820 u16 pci_pm;
1821
1822 IWL_DEBUG_POWER("Initialize power \n");
1823
1824 pow_data = &(priv->power_data);
1825
1826 memset(pow_data, 0, sizeof(*pow_data));
1827
1828 pow_data->active_index = IWL_POWER_RANGE_0;
1829 pow_data->dtim_val = 0xffff;
1830
1831 memcpy(&pow_data->pwr_range_0[0], &range_0[0], size);
1832 memcpy(&pow_data->pwr_range_1[0], &range_1[0], size);
1833
1834 rc = pci_read_config_word(priv->pci_dev, PCI_LINK_CTRL, &pci_pm);
1835 if (rc != 0)
1836 return 0;
1837 else {
Tomas Winkler600c0e12008-12-19 10:37:04 +08001838 struct iwl_powertable_cmd *cmd;
Zhu Yib481de92007-09-25 17:54:57 -07001839
1840 IWL_DEBUG_POWER("adjust power command flags\n");
1841
Samuel Ortiz1125eff2008-12-19 10:37:14 +08001842 for (i = 0; i < IWL39_POWER_AC; i++) {
Zhu Yib481de92007-09-25 17:54:57 -07001843 cmd = &pow_data->pwr_range_0[i].cmd;
1844
1845 if (pci_pm & 0x1)
1846 cmd->flags &= ~IWL_POWER_PCI_PM_MSK;
1847 else
1848 cmd->flags |= IWL_POWER_PCI_PM_MSK;
1849 }
1850 }
1851 return rc;
1852}
1853
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001854static int iwl3945_update_power_cmd(struct iwl3945_priv *priv,
Tomas Winkler600c0e12008-12-19 10:37:04 +08001855 struct iwl_powertable_cmd *cmd, u32 mode)
Zhu Yib481de92007-09-25 17:54:57 -07001856{
1857 int rc = 0, i;
1858 u8 skip;
1859 u32 max_sleep = 0;
Samuel Ortiz1125eff2008-12-19 10:37:14 +08001860 struct iwl_power_vec_entry *range;
Zhu Yib481de92007-09-25 17:54:57 -07001861 u8 period = 0;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001862 struct iwl3945_power_mgr *pow_data;
Zhu Yib481de92007-09-25 17:54:57 -07001863
1864 if (mode > IWL_POWER_INDEX_5) {
1865 IWL_DEBUG_POWER("Error invalid power mode \n");
1866 return -1;
1867 }
1868 pow_data = &(priv->power_data);
1869
1870 if (pow_data->active_index == IWL_POWER_RANGE_0)
1871 range = &pow_data->pwr_range_0[0];
1872 else
1873 range = &pow_data->pwr_range_1[1];
1874
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001875 memcpy(cmd, &range[mode].cmd, sizeof(struct iwl3945_powertable_cmd));
Zhu Yib481de92007-09-25 17:54:57 -07001876
1877#ifdef IWL_MAC80211_DISABLE
1878 if (priv->assoc_network != NULL) {
1879 unsigned long flags;
1880
1881 period = priv->assoc_network->tim.tim_period;
1882 }
1883#endif /*IWL_MAC80211_DISABLE */
1884 skip = range[mode].no_dtim;
1885
1886 if (period == 0) {
1887 period = 1;
1888 skip = 0;
1889 }
1890
1891 if (skip == 0) {
1892 max_sleep = period;
1893 cmd->flags &= ~IWL_POWER_SLEEP_OVER_DTIM_MSK;
1894 } else {
1895 __le32 slp_itrvl = cmd->sleep_interval[IWL_POWER_VEC_SIZE - 1];
1896 max_sleep = (le32_to_cpu(slp_itrvl) / period) * period;
1897 cmd->flags |= IWL_POWER_SLEEP_OVER_DTIM_MSK;
1898 }
1899
1900 for (i = 0; i < IWL_POWER_VEC_SIZE; i++) {
1901 if (le32_to_cpu(cmd->sleep_interval[i]) > max_sleep)
1902 cmd->sleep_interval[i] = cpu_to_le32(max_sleep);
1903 }
1904
1905 IWL_DEBUG_POWER("Flags value = 0x%08X\n", cmd->flags);
1906 IWL_DEBUG_POWER("Tx timeout = %u\n", le32_to_cpu(cmd->tx_data_timeout));
1907 IWL_DEBUG_POWER("Rx timeout = %u\n", le32_to_cpu(cmd->rx_data_timeout));
1908 IWL_DEBUG_POWER("Sleep interval vector = { %d , %d , %d , %d , %d }\n",
1909 le32_to_cpu(cmd->sleep_interval[0]),
1910 le32_to_cpu(cmd->sleep_interval[1]),
1911 le32_to_cpu(cmd->sleep_interval[2]),
1912 le32_to_cpu(cmd->sleep_interval[3]),
1913 le32_to_cpu(cmd->sleep_interval[4]));
1914
1915 return rc;
1916}
1917
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001918static int iwl3945_send_power_mode(struct iwl3945_priv *priv, u32 mode)
Zhu Yib481de92007-09-25 17:54:57 -07001919{
John W. Linville9a62f732007-11-15 16:27:36 -05001920 u32 uninitialized_var(final_mode);
Zhu Yib481de92007-09-25 17:54:57 -07001921 int rc;
Tomas Winkler600c0e12008-12-19 10:37:04 +08001922 struct iwl_powertable_cmd cmd;
Zhu Yib481de92007-09-25 17:54:57 -07001923
1924 /* If on battery, set to 3,
Ian Schram01ebd062007-10-25 17:15:22 +08001925 * if plugged into AC power, set to CAM ("continuously aware mode"),
Zhu Yib481de92007-09-25 17:54:57 -07001926 * else user level */
1927 switch (mode) {
Samuel Ortiz1125eff2008-12-19 10:37:14 +08001928 case IWL39_POWER_BATTERY:
Zhu Yib481de92007-09-25 17:54:57 -07001929 final_mode = IWL_POWER_INDEX_3;
1930 break;
Samuel Ortiz1125eff2008-12-19 10:37:14 +08001931 case IWL39_POWER_AC:
Zhu Yib481de92007-09-25 17:54:57 -07001932 final_mode = IWL_POWER_MODE_CAM;
1933 break;
1934 default:
1935 final_mode = mode;
1936 break;
1937 }
1938
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001939 iwl3945_update_power_cmd(priv, &cmd, final_mode);
Zhu Yib481de92007-09-25 17:54:57 -07001940
Tomas Winkler600c0e12008-12-19 10:37:04 +08001941 /* FIXME use get_hcmd_size 3945 command is 4 bytes shorter */
1942 rc = iwl3945_send_cmd_pdu(priv, POWER_TABLE_CMD,
1943 sizeof(struct iwl3945_powertable_cmd), &cmd);
Zhu Yib481de92007-09-25 17:54:57 -07001944
1945 if (final_mode == IWL_POWER_MODE_CAM)
1946 clear_bit(STATUS_POWER_PMI, &priv->status);
1947 else
1948 set_bit(STATUS_POWER_PMI, &priv->status);
1949
1950 return rc;
1951}
1952
Zhu Yib481de92007-09-25 17:54:57 -07001953/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001954 * iwl3945_scan_cancel - Cancel any currently executing HW scan
Zhu Yib481de92007-09-25 17:54:57 -07001955 *
1956 * NOTE: priv->mutex is not required before calling this function
1957 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001958static int iwl3945_scan_cancel(struct iwl3945_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001959{
1960 if (!test_bit(STATUS_SCAN_HW, &priv->status)) {
1961 clear_bit(STATUS_SCANNING, &priv->status);
1962 return 0;
1963 }
1964
1965 if (test_bit(STATUS_SCANNING, &priv->status)) {
1966 if (!test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
1967 IWL_DEBUG_SCAN("Queuing scan abort.\n");
1968 set_bit(STATUS_SCAN_ABORTING, &priv->status);
1969 queue_work(priv->workqueue, &priv->abort_scan);
1970
1971 } else
1972 IWL_DEBUG_SCAN("Scan abort already in progress.\n");
1973
1974 return test_bit(STATUS_SCANNING, &priv->status);
1975 }
1976
1977 return 0;
1978}
1979
1980/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001981 * iwl3945_scan_cancel_timeout - Cancel any currently executing HW scan
Zhu Yib481de92007-09-25 17:54:57 -07001982 * @ms: amount of time to wait (in milliseconds) for scan to abort
1983 *
1984 * NOTE: priv->mutex must be held before calling this function
1985 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001986static int iwl3945_scan_cancel_timeout(struct iwl3945_priv *priv, unsigned long ms)
Zhu Yib481de92007-09-25 17:54:57 -07001987{
1988 unsigned long now = jiffies;
1989 int ret;
1990
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001991 ret = iwl3945_scan_cancel(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001992 if (ret && ms) {
1993 mutex_unlock(&priv->mutex);
1994 while (!time_after(jiffies, now + msecs_to_jiffies(ms)) &&
1995 test_bit(STATUS_SCANNING, &priv->status))
1996 msleep(1);
1997 mutex_lock(&priv->mutex);
1998
1999 return test_bit(STATUS_SCANNING, &priv->status);
2000 }
2001
2002 return ret;
2003}
2004
Zhu Yib481de92007-09-25 17:54:57 -07002005#define MAX_UCODE_BEACON_INTERVAL 1024
2006#define INTEL_CONN_LISTEN_INTERVAL __constant_cpu_to_le16(0xA)
2007
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002008static __le16 iwl3945_adjust_beacon_interval(u16 beacon_val)
Zhu Yib481de92007-09-25 17:54:57 -07002009{
2010 u16 new_val = 0;
2011 u16 beacon_factor = 0;
2012
2013 beacon_factor =
2014 (beacon_val + MAX_UCODE_BEACON_INTERVAL)
2015 / MAX_UCODE_BEACON_INTERVAL;
2016 new_val = beacon_val / beacon_factor;
2017
2018 return cpu_to_le16(new_val);
2019}
2020
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002021static void iwl3945_setup_rxon_timing(struct iwl3945_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002022{
2023 u64 interval_tm_unit;
2024 u64 tsf, result;
2025 unsigned long flags;
2026 struct ieee80211_conf *conf = NULL;
2027 u16 beacon_int = 0;
2028
2029 conf = ieee80211_get_hw_conf(priv->hw);
2030
2031 spin_lock_irqsave(&priv->lock, flags);
Tomas Winkler28afaf92008-12-19 10:37:06 +08002032 priv->rxon_timing.timestamp = cpu_to_le64(priv->timestamp);
Zhu Yib481de92007-09-25 17:54:57 -07002033 priv->rxon_timing.listen_interval = INTEL_CONN_LISTEN_INTERVAL;
2034
Tomas Winkler28afaf92008-12-19 10:37:06 +08002035 tsf = priv->timestamp;
Zhu Yib481de92007-09-25 17:54:57 -07002036
2037 beacon_int = priv->beacon_int;
2038 spin_unlock_irqrestore(&priv->lock, flags);
2039
Johannes Berg05c914f2008-09-11 00:01:58 +02002040 if (priv->iw_mode == NL80211_IFTYPE_STATION) {
Zhu Yib481de92007-09-25 17:54:57 -07002041 if (beacon_int == 0) {
2042 priv->rxon_timing.beacon_interval = cpu_to_le16(100);
2043 priv->rxon_timing.beacon_init_val = cpu_to_le32(102400);
2044 } else {
2045 priv->rxon_timing.beacon_interval =
2046 cpu_to_le16(beacon_int);
2047 priv->rxon_timing.beacon_interval =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002048 iwl3945_adjust_beacon_interval(
Zhu Yib481de92007-09-25 17:54:57 -07002049 le16_to_cpu(priv->rxon_timing.beacon_interval));
2050 }
2051
2052 priv->rxon_timing.atim_window = 0;
2053 } else {
2054 priv->rxon_timing.beacon_interval =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002055 iwl3945_adjust_beacon_interval(conf->beacon_int);
Zhu Yib481de92007-09-25 17:54:57 -07002056 /* TODO: we need to get atim_window from upper stack
2057 * for now we set to 0 */
2058 priv->rxon_timing.atim_window = 0;
2059 }
2060
2061 interval_tm_unit =
2062 (le16_to_cpu(priv->rxon_timing.beacon_interval) * 1024);
2063 result = do_div(tsf, interval_tm_unit);
2064 priv->rxon_timing.beacon_init_val =
2065 cpu_to_le32((u32) ((u64) interval_tm_unit - result));
2066
2067 IWL_DEBUG_ASSOC
2068 ("beacon interval %d beacon timer %d beacon tim %d\n",
2069 le16_to_cpu(priv->rxon_timing.beacon_interval),
2070 le32_to_cpu(priv->rxon_timing.beacon_init_val),
2071 le16_to_cpu(priv->rxon_timing.atim_window));
2072}
2073
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002074static int iwl3945_scan_initiate(struct iwl3945_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002075{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002076 if (!iwl3945_is_ready_rf(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07002077 IWL_DEBUG_SCAN("Aborting scan due to not ready.\n");
2078 return -EIO;
2079 }
2080
2081 if (test_bit(STATUS_SCANNING, &priv->status)) {
2082 IWL_DEBUG_SCAN("Scan already in progress.\n");
2083 return -EAGAIN;
2084 }
2085
2086 if (test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
2087 IWL_DEBUG_SCAN("Scan request while abort pending. "
2088 "Queuing.\n");
2089 return -EAGAIN;
2090 }
2091
2092 IWL_DEBUG_INFO("Starting scan...\n");
Ron Rindjunsky66b50042008-06-25 16:46:31 +08002093 if (priv->cfg->sku & IWL_SKU_G)
2094 priv->scan_bands |= BIT(IEEE80211_BAND_2GHZ);
2095 if (priv->cfg->sku & IWL_SKU_A)
2096 priv->scan_bands |= BIT(IEEE80211_BAND_5GHZ);
Zhu Yib481de92007-09-25 17:54:57 -07002097 set_bit(STATUS_SCANNING, &priv->status);
2098 priv->scan_start = jiffies;
2099 priv->scan_pass_start = priv->scan_start;
2100
2101 queue_work(priv->workqueue, &priv->request_scan);
2102
2103 return 0;
2104}
2105
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002106static int iwl3945_set_rxon_hwcrypto(struct iwl3945_priv *priv, int hw_decrypt)
Zhu Yib481de92007-09-25 17:54:57 -07002107{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002108 struct iwl3945_rxon_cmd *rxon = &priv->staging_rxon;
Zhu Yib481de92007-09-25 17:54:57 -07002109
2110 if (hw_decrypt)
2111 rxon->filter_flags &= ~RXON_FILTER_DIS_DECRYPT_MSK;
2112 else
2113 rxon->filter_flags |= RXON_FILTER_DIS_DECRYPT_MSK;
2114
2115 return 0;
2116}
2117
Johannes Berg8318d782008-01-24 19:38:38 +01002118static void iwl3945_set_flags_for_phymode(struct iwl3945_priv *priv,
2119 enum ieee80211_band band)
Zhu Yib481de92007-09-25 17:54:57 -07002120{
Johannes Berg8318d782008-01-24 19:38:38 +01002121 if (band == IEEE80211_BAND_5GHZ) {
Zhu Yib481de92007-09-25 17:54:57 -07002122 priv->staging_rxon.flags &=
2123 ~(RXON_FLG_BAND_24G_MSK | RXON_FLG_AUTO_DETECT_MSK
2124 | RXON_FLG_CCK_MSK);
2125 priv->staging_rxon.flags |= RXON_FLG_SHORT_SLOT_MSK;
2126 } else {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002127 /* Copied from iwl3945_bg_post_associate() */
Zhu Yib481de92007-09-25 17:54:57 -07002128 if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_SLOT_TIME)
2129 priv->staging_rxon.flags |= RXON_FLG_SHORT_SLOT_MSK;
2130 else
2131 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
2132
Johannes Berg05c914f2008-09-11 00:01:58 +02002133 if (priv->iw_mode == NL80211_IFTYPE_ADHOC)
Zhu Yib481de92007-09-25 17:54:57 -07002134 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
2135
2136 priv->staging_rxon.flags |= RXON_FLG_BAND_24G_MSK;
2137 priv->staging_rxon.flags |= RXON_FLG_AUTO_DETECT_MSK;
2138 priv->staging_rxon.flags &= ~RXON_FLG_CCK_MSK;
2139 }
2140}
2141
2142/*
Ian Schram01ebd062007-10-25 17:15:22 +08002143 * initialize rxon structure with default values from eeprom
Zhu Yib481de92007-09-25 17:54:57 -07002144 */
Zhu, Yi60294de2008-10-29 14:05:45 -07002145static void iwl3945_connection_init_rx_config(struct iwl3945_priv *priv,
2146 int mode)
Zhu Yib481de92007-09-25 17:54:57 -07002147{
Samuel Ortizd20b3c62008-12-19 10:37:15 +08002148 const struct iwl_channel_info *ch_info;
Zhu Yib481de92007-09-25 17:54:57 -07002149
2150 memset(&priv->staging_rxon, 0, sizeof(priv->staging_rxon));
2151
Zhu, Yi60294de2008-10-29 14:05:45 -07002152 switch (mode) {
Johannes Berg05c914f2008-09-11 00:01:58 +02002153 case NL80211_IFTYPE_AP:
Zhu Yib481de92007-09-25 17:54:57 -07002154 priv->staging_rxon.dev_type = RXON_DEV_TYPE_AP;
2155 break;
2156
Johannes Berg05c914f2008-09-11 00:01:58 +02002157 case NL80211_IFTYPE_STATION:
Zhu Yib481de92007-09-25 17:54:57 -07002158 priv->staging_rxon.dev_type = RXON_DEV_TYPE_ESS;
2159 priv->staging_rxon.filter_flags = RXON_FILTER_ACCEPT_GRP_MSK;
2160 break;
2161
Johannes Berg05c914f2008-09-11 00:01:58 +02002162 case NL80211_IFTYPE_ADHOC:
Zhu Yib481de92007-09-25 17:54:57 -07002163 priv->staging_rxon.dev_type = RXON_DEV_TYPE_IBSS;
2164 priv->staging_rxon.flags = RXON_FLG_SHORT_PREAMBLE_MSK;
2165 priv->staging_rxon.filter_flags = RXON_FILTER_BCON_AWARE_MSK |
2166 RXON_FILTER_ACCEPT_GRP_MSK;
2167 break;
2168
Johannes Berg05c914f2008-09-11 00:01:58 +02002169 case NL80211_IFTYPE_MONITOR:
Zhu Yib481de92007-09-25 17:54:57 -07002170 priv->staging_rxon.dev_type = RXON_DEV_TYPE_SNIFFER;
2171 priv->staging_rxon.filter_flags = RXON_FILTER_PROMISC_MSK |
2172 RXON_FILTER_CTL2HOST_MSK | RXON_FILTER_ACCEPT_GRP_MSK;
2173 break;
Tomas Winkler69dc5d92008-03-25 16:33:41 -07002174 default:
Zhu, Yi60294de2008-10-29 14:05:45 -07002175 IWL_ERROR("Unsupported interface type %d\n", mode);
Tomas Winkler69dc5d92008-03-25 16:33:41 -07002176 break;
Zhu Yib481de92007-09-25 17:54:57 -07002177 }
2178
2179#if 0
2180 /* TODO: Figure out when short_preamble would be set and cache from
2181 * that */
2182 if (!hw_to_local(priv->hw)->short_preamble)
2183 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_PREAMBLE_MSK;
2184 else
2185 priv->staging_rxon.flags |= RXON_FLG_SHORT_PREAMBLE_MSK;
2186#endif
2187
Johannes Berg8318d782008-01-24 19:38:38 +01002188 ch_info = iwl3945_get_channel_info(priv, priv->band,
Rick Farrington25b3f572008-06-30 17:23:28 +08002189 le16_to_cpu(priv->active_rxon.channel));
Zhu Yib481de92007-09-25 17:54:57 -07002190
2191 if (!ch_info)
2192 ch_info = &priv->channel_info[0];
2193
2194 /*
2195 * in some case A channels are all non IBSS
2196 * in this case force B/G channel
2197 */
Zhu, Yi60294de2008-10-29 14:05:45 -07002198 if ((mode == NL80211_IFTYPE_ADHOC) && !(is_channel_ibss(ch_info)))
Zhu Yib481de92007-09-25 17:54:57 -07002199 ch_info = &priv->channel_info[0];
2200
2201 priv->staging_rxon.channel = cpu_to_le16(ch_info->channel);
2202 if (is_channel_a_band(ch_info))
Johannes Berg8318d782008-01-24 19:38:38 +01002203 priv->band = IEEE80211_BAND_5GHZ;
Zhu Yib481de92007-09-25 17:54:57 -07002204 else
Johannes Berg8318d782008-01-24 19:38:38 +01002205 priv->band = IEEE80211_BAND_2GHZ;
Zhu Yib481de92007-09-25 17:54:57 -07002206
Johannes Berg8318d782008-01-24 19:38:38 +01002207 iwl3945_set_flags_for_phymode(priv, priv->band);
Zhu Yib481de92007-09-25 17:54:57 -07002208
2209 priv->staging_rxon.ofdm_basic_rates =
2210 (IWL_OFDM_RATES_MASK >> IWL_FIRST_OFDM_RATE) & 0xFF;
2211 priv->staging_rxon.cck_basic_rates =
2212 (IWL_CCK_RATES_MASK >> IWL_FIRST_CCK_RATE) & 0xF;
2213}
2214
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002215static int iwl3945_set_mode(struct iwl3945_priv *priv, int mode)
Zhu Yib481de92007-09-25 17:54:57 -07002216{
Johannes Berg05c914f2008-09-11 00:01:58 +02002217 if (mode == NL80211_IFTYPE_ADHOC) {
Samuel Ortizd20b3c62008-12-19 10:37:15 +08002218 const struct iwl_channel_info *ch_info;
Zhu Yib481de92007-09-25 17:54:57 -07002219
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002220 ch_info = iwl3945_get_channel_info(priv,
Johannes Berg8318d782008-01-24 19:38:38 +01002221 priv->band,
Zhu Yib481de92007-09-25 17:54:57 -07002222 le16_to_cpu(priv->staging_rxon.channel));
2223
2224 if (!ch_info || !is_channel_ibss(ch_info)) {
2225 IWL_ERROR("channel %d not IBSS channel\n",
2226 le16_to_cpu(priv->staging_rxon.channel));
2227 return -EINVAL;
2228 }
2229 }
2230
Zhu, Yi60294de2008-10-29 14:05:45 -07002231 iwl3945_connection_init_rx_config(priv, mode);
Zhu Yib481de92007-09-25 17:54:57 -07002232 memcpy(priv->staging_rxon.node_addr, priv->mac_addr, ETH_ALEN);
2233
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002234 iwl3945_clear_stations_table(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002235
Tomas Winklera96a27f2008-10-23 23:48:56 -07002236 /* don't commit rxon if rf-kill is on*/
Mohamed Abbasfde35712007-11-29 11:10:15 +08002237 if (!iwl3945_is_ready_rf(priv))
2238 return -EAGAIN;
2239
2240 cancel_delayed_work(&priv->scan_check);
2241 if (iwl3945_scan_cancel_timeout(priv, 100)) {
2242 IWL_WARNING("Aborted scan still in progress after 100ms\n");
2243 IWL_DEBUG_MAC80211("leaving - scan abort failed.\n");
2244 return -EAGAIN;
2245 }
2246
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002247 iwl3945_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002248
2249 return 0;
2250}
2251
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002252static void iwl3945_build_tx_cmd_hwcrypto(struct iwl3945_priv *priv,
Johannes Berge039fa42008-05-15 12:55:29 +02002253 struct ieee80211_tx_info *info,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002254 struct iwl3945_cmd *cmd,
Zhu Yib481de92007-09-25 17:54:57 -07002255 struct sk_buff *skb_frag,
2256 int last_frag)
2257{
Ivo van Doorn1c014422008-04-17 19:41:02 +02002258 struct iwl3945_hw_key *keyinfo =
Johannes Berge039fa42008-05-15 12:55:29 +02002259 &priv->stations[info->control.hw_key->hw_key_idx].keyinfo;
Zhu Yib481de92007-09-25 17:54:57 -07002260
2261 switch (keyinfo->alg) {
2262 case ALG_CCMP:
2263 cmd->cmd.tx.sec_ctl = TX_CMD_SEC_CCM;
2264 memcpy(cmd->cmd.tx.key, keyinfo->key, keyinfo->keylen);
Tomas Winklera96a27f2008-10-23 23:48:56 -07002265 IWL_DEBUG_TX("tx_cmd with AES hwcrypto\n");
Zhu Yib481de92007-09-25 17:54:57 -07002266 break;
2267
2268 case ALG_TKIP:
2269#if 0
2270 cmd->cmd.tx.sec_ctl = TX_CMD_SEC_TKIP;
2271
2272 if (last_frag)
2273 memcpy(cmd->cmd.tx.tkip_mic.byte, skb_frag->tail - 8,
2274 8);
2275 else
2276 memset(cmd->cmd.tx.tkip_mic.byte, 0, 8);
2277#endif
2278 break;
2279
2280 case ALG_WEP:
2281 cmd->cmd.tx.sec_ctl = TX_CMD_SEC_WEP |
Johannes Berge039fa42008-05-15 12:55:29 +02002282 (info->control.hw_key->hw_key_idx & TX_CMD_SEC_MSK) << TX_CMD_SEC_SHIFT;
Zhu Yib481de92007-09-25 17:54:57 -07002283
2284 if (keyinfo->keylen == 13)
2285 cmd->cmd.tx.sec_ctl |= TX_CMD_SEC_KEY128;
2286
2287 memcpy(&cmd->cmd.tx.key[3], keyinfo->key, keyinfo->keylen);
2288
2289 IWL_DEBUG_TX("Configuring packet for WEP encryption "
Johannes Berge039fa42008-05-15 12:55:29 +02002290 "with key %d\n", info->control.hw_key->hw_key_idx);
Zhu Yib481de92007-09-25 17:54:57 -07002291 break;
2292
Zhu Yib481de92007-09-25 17:54:57 -07002293 default:
2294 printk(KERN_ERR "Unknown encode alg %d\n", keyinfo->alg);
2295 break;
2296 }
2297}
2298
2299/*
2300 * handle build REPLY_TX command notification.
2301 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002302static void iwl3945_build_tx_cmd_basic(struct iwl3945_priv *priv,
2303 struct iwl3945_cmd *cmd,
Johannes Berge039fa42008-05-15 12:55:29 +02002304 struct ieee80211_tx_info *info,
Zhu Yib481de92007-09-25 17:54:57 -07002305 struct ieee80211_hdr *hdr,
2306 int is_unicast, u8 std_id)
2307{
Harvey Harrisonfd7c8a42008-06-11 14:21:56 -07002308 __le16 fc = hdr->frame_control;
Zhu Yib481de92007-09-25 17:54:57 -07002309 __le32 tx_flags = cmd->cmd.tx.tx_flags;
Johannes Berge6a98542008-10-21 12:40:02 +02002310 u8 rc_flags = info->control.rates[0].flags;
Zhu Yib481de92007-09-25 17:54:57 -07002311
2312 cmd->cmd.tx.stop_time.life_time = TX_CMD_LIFE_TIME_INFINITE;
Johannes Berge039fa42008-05-15 12:55:29 +02002313 if (!(info->flags & IEEE80211_TX_CTL_NO_ACK)) {
Zhu Yib481de92007-09-25 17:54:57 -07002314 tx_flags |= TX_CMD_FLG_ACK_MSK;
Harvey Harrisonfd7c8a42008-06-11 14:21:56 -07002315 if (ieee80211_is_mgmt(fc))
Zhu Yib481de92007-09-25 17:54:57 -07002316 tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
Harvey Harrisonfd7c8a42008-06-11 14:21:56 -07002317 if (ieee80211_is_probe_resp(fc) &&
Zhu Yib481de92007-09-25 17:54:57 -07002318 !(le16_to_cpu(hdr->seq_ctrl) & 0xf))
2319 tx_flags |= TX_CMD_FLG_TSF_MSK;
2320 } else {
2321 tx_flags &= (~TX_CMD_FLG_ACK_MSK);
2322 tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
2323 }
2324
2325 cmd->cmd.tx.sta_id = std_id;
Harvey Harrison8b7b1e02008-06-11 14:21:56 -07002326 if (ieee80211_has_morefrags(fc))
Zhu Yib481de92007-09-25 17:54:57 -07002327 tx_flags |= TX_CMD_FLG_MORE_FRAG_MSK;
2328
Harvey Harrisonfd7c8a42008-06-11 14:21:56 -07002329 if (ieee80211_is_data_qos(fc)) {
2330 u8 *qc = ieee80211_get_qos_ctl(hdr);
Tomas Winkler54dbb522008-05-15 13:54:06 +08002331 cmd->cmd.tx.tid_tspec = qc[0] & 0xf;
Zhu Yib481de92007-09-25 17:54:57 -07002332 tx_flags &= ~TX_CMD_FLG_SEQ_CTL_MSK;
Tomas Winkler54dbb522008-05-15 13:54:06 +08002333 } else {
Zhu Yib481de92007-09-25 17:54:57 -07002334 tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
Tomas Winkler54dbb522008-05-15 13:54:06 +08002335 }
Zhu Yib481de92007-09-25 17:54:57 -07002336
Johannes Berge6a98542008-10-21 12:40:02 +02002337 if (rc_flags & IEEE80211_TX_RC_USE_RTS_CTS) {
Zhu Yib481de92007-09-25 17:54:57 -07002338 tx_flags |= TX_CMD_FLG_RTS_MSK;
2339 tx_flags &= ~TX_CMD_FLG_CTS_MSK;
Johannes Berge6a98542008-10-21 12:40:02 +02002340 } else if (rc_flags & IEEE80211_TX_RC_USE_CTS_PROTECT) {
Zhu Yib481de92007-09-25 17:54:57 -07002341 tx_flags &= ~TX_CMD_FLG_RTS_MSK;
2342 tx_flags |= TX_CMD_FLG_CTS_MSK;
2343 }
2344
2345 if ((tx_flags & TX_CMD_FLG_RTS_MSK) || (tx_flags & TX_CMD_FLG_CTS_MSK))
2346 tx_flags |= TX_CMD_FLG_FULL_TXOP_PROT_MSK;
2347
2348 tx_flags &= ~(TX_CMD_FLG_ANT_SEL_MSK);
Harvey Harrisonfd7c8a42008-06-11 14:21:56 -07002349 if (ieee80211_is_mgmt(fc)) {
2350 if (ieee80211_is_assoc_req(fc) || ieee80211_is_reassoc_req(fc))
Ian Schrambc434dd2007-10-25 17:15:29 +08002351 cmd->cmd.tx.timeout.pm_frame_timeout = cpu_to_le16(3);
Zhu Yib481de92007-09-25 17:54:57 -07002352 else
Ian Schrambc434dd2007-10-25 17:15:29 +08002353 cmd->cmd.tx.timeout.pm_frame_timeout = cpu_to_le16(2);
Mohamed Abbasab53d8a2008-03-25 16:33:36 -07002354 } else {
Zhu Yib481de92007-09-25 17:54:57 -07002355 cmd->cmd.tx.timeout.pm_frame_timeout = 0;
Mohamed Abbasab53d8a2008-03-25 16:33:36 -07002356#ifdef CONFIG_IWL3945_LEDS
2357 priv->rxtxpackets += le16_to_cpu(cmd->cmd.tx.len);
2358#endif
2359 }
Zhu Yib481de92007-09-25 17:54:57 -07002360
2361 cmd->cmd.tx.driver_txop = 0;
2362 cmd->cmd.tx.tx_flags = tx_flags;
2363 cmd->cmd.tx.next_frame_len = 0;
2364}
2365
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002366/**
2367 * iwl3945_get_sta_id - Find station's index within station table
2368 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002369static int iwl3945_get_sta_id(struct iwl3945_priv *priv, struct ieee80211_hdr *hdr)
Zhu Yib481de92007-09-25 17:54:57 -07002370{
2371 int sta_id;
2372 u16 fc = le16_to_cpu(hdr->frame_control);
2373
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002374 /* If this frame is broadcast or management, use broadcast station id */
Zhu Yib481de92007-09-25 17:54:57 -07002375 if (((fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_DATA) ||
2376 is_multicast_ether_addr(hdr->addr1))
2377 return priv->hw_setting.bcast_sta_id;
2378
2379 switch (priv->iw_mode) {
2380
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002381 /* If we are a client station in a BSS network, use the special
2382 * AP station entry (that's the only station we communicate with) */
Johannes Berg05c914f2008-09-11 00:01:58 +02002383 case NL80211_IFTYPE_STATION:
Zhu Yib481de92007-09-25 17:54:57 -07002384 return IWL_AP_ID;
2385
2386 /* If we are an AP, then find the station, or use BCAST */
Johannes Berg05c914f2008-09-11 00:01:58 +02002387 case NL80211_IFTYPE_AP:
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002388 sta_id = iwl3945_hw_find_station(priv, hdr->addr1);
Zhu Yib481de92007-09-25 17:54:57 -07002389 if (sta_id != IWL_INVALID_STATION)
2390 return sta_id;
2391 return priv->hw_setting.bcast_sta_id;
2392
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002393 /* If this frame is going out to an IBSS network, find the station,
2394 * or create a new station table entry */
Johannes Berg05c914f2008-09-11 00:01:58 +02002395 case NL80211_IFTYPE_ADHOC: {
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002396 /* Create new station table entry */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002397 sta_id = iwl3945_hw_find_station(priv, hdr->addr1);
Zhu Yib481de92007-09-25 17:54:57 -07002398 if (sta_id != IWL_INVALID_STATION)
2399 return sta_id;
2400
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002401 sta_id = iwl3945_add_station(priv, hdr->addr1, 0, CMD_ASYNC);
Zhu Yib481de92007-09-25 17:54:57 -07002402
2403 if (sta_id != IWL_INVALID_STATION)
2404 return sta_id;
2405
Johannes Berge1749612008-10-27 15:59:26 -07002406 IWL_DEBUG_DROP("Station %pM not in station map. "
Zhu Yib481de92007-09-25 17:54:57 -07002407 "Defaulting to broadcast...\n",
Johannes Berge1749612008-10-27 15:59:26 -07002408 hdr->addr1);
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08002409 iwl_print_hex_dump(priv, IWL_DL_DROP, (u8 *) hdr, sizeof(*hdr));
Zhu Yib481de92007-09-25 17:54:57 -07002410 return priv->hw_setting.bcast_sta_id;
Joe Perches0795af52007-10-03 17:59:30 -07002411 }
Stefanik Gábor914233d2008-06-30 17:23:30 +08002412 /* If we are in monitor mode, use BCAST. This is required for
2413 * packet injection. */
Johannes Berg05c914f2008-09-11 00:01:58 +02002414 case NL80211_IFTYPE_MONITOR:
Stefanik Gábor914233d2008-06-30 17:23:30 +08002415 return priv->hw_setting.bcast_sta_id;
2416
Zhu Yib481de92007-09-25 17:54:57 -07002417 default:
Jiri Slaby6f147922008-08-11 23:49:41 +02002418 IWL_WARNING("Unknown mode of operation: %d\n", priv->iw_mode);
Zhu Yib481de92007-09-25 17:54:57 -07002419 return priv->hw_setting.bcast_sta_id;
2420 }
2421}
2422
2423/*
2424 * start REPLY_TX command process
2425 */
Johannes Berge039fa42008-05-15 12:55:29 +02002426static int iwl3945_tx_skb(struct iwl3945_priv *priv, struct sk_buff *skb)
Zhu Yib481de92007-09-25 17:54:57 -07002427{
2428 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
Johannes Berge039fa42008-05-15 12:55:29 +02002429 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002430 struct iwl3945_tfd_frame *tfd;
Zhu Yib481de92007-09-25 17:54:57 -07002431 u32 *control_flags;
Johannes Berge2530082008-05-17 00:57:14 +02002432 int txq_id = skb_get_queue_mapping(skb);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002433 struct iwl3945_tx_queue *txq = NULL;
Samuel Ortizd20b3c62008-12-19 10:37:15 +08002434 struct iwl_queue *q = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07002435 dma_addr_t phys_addr;
2436 dma_addr_t txcmd_phys;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002437 struct iwl3945_cmd *out_cmd = NULL;
Tomas Winkler54dbb522008-05-15 13:54:06 +08002438 u16 len, idx, len_org, hdr_len;
2439 u8 id;
2440 u8 unicast;
Zhu Yib481de92007-09-25 17:54:57 -07002441 u8 sta_id;
Tomas Winkler54dbb522008-05-15 13:54:06 +08002442 u8 tid = 0;
Zhu Yib481de92007-09-25 17:54:57 -07002443 u16 seq_number = 0;
Harvey Harrisonfd7c8a42008-06-11 14:21:56 -07002444 __le16 fc;
Zhu Yib481de92007-09-25 17:54:57 -07002445 u8 wait_write_ptr = 0;
Tomas Winkler54dbb522008-05-15 13:54:06 +08002446 u8 *qc = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07002447 unsigned long flags;
2448 int rc;
2449
2450 spin_lock_irqsave(&priv->lock, flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002451 if (iwl3945_is_rfkill(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07002452 IWL_DEBUG_DROP("Dropping - RF KILL\n");
2453 goto drop_unlock;
2454 }
2455
Johannes Berge039fa42008-05-15 12:55:29 +02002456 if ((ieee80211_get_tx_rate(priv->hw, info)->hw_value & 0xFF) == IWL_INVALID_RATE) {
Zhu Yib481de92007-09-25 17:54:57 -07002457 IWL_ERROR("ERROR: No TX rate available.\n");
2458 goto drop_unlock;
2459 }
2460
2461 unicast = !is_multicast_ether_addr(hdr->addr1);
2462 id = 0;
2463
Harvey Harrisonfd7c8a42008-06-11 14:21:56 -07002464 fc = hdr->frame_control;
Zhu Yib481de92007-09-25 17:54:57 -07002465
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08002466#ifdef CONFIG_IWL3945_DEBUG
Zhu Yib481de92007-09-25 17:54:57 -07002467 if (ieee80211_is_auth(fc))
2468 IWL_DEBUG_TX("Sending AUTH frame\n");
Harvey Harrisonfd7c8a42008-06-11 14:21:56 -07002469 else if (ieee80211_is_assoc_req(fc))
Zhu Yib481de92007-09-25 17:54:57 -07002470 IWL_DEBUG_TX("Sending ASSOC frame\n");
Harvey Harrisonfd7c8a42008-06-11 14:21:56 -07002471 else if (ieee80211_is_reassoc_req(fc))
Zhu Yib481de92007-09-25 17:54:57 -07002472 IWL_DEBUG_TX("Sending REASSOC frame\n");
2473#endif
2474
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08002475 /* drop all data frame if we are not associated */
Stefanik Gábor914233d2008-06-30 17:23:30 +08002476 if (ieee80211_is_data(fc) &&
Johannes Berg05c914f2008-09-11 00:01:58 +02002477 (priv->iw_mode != NL80211_IFTYPE_MONITOR) && /* packet injection */
Stefanik Gábor914233d2008-06-30 17:23:30 +08002478 (!iwl3945_is_associated(priv) ||
Johannes Berg05c914f2008-09-11 00:01:58 +02002479 ((priv->iw_mode == NL80211_IFTYPE_STATION) && !priv->assoc_id))) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002480 IWL_DEBUG_DROP("Dropping - !iwl3945_is_associated\n");
Zhu Yib481de92007-09-25 17:54:57 -07002481 goto drop_unlock;
2482 }
2483
2484 spin_unlock_irqrestore(&priv->lock, flags);
2485
Harvey Harrison7294ec92008-07-15 18:43:59 -07002486 hdr_len = ieee80211_hdrlen(fc);
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002487
2488 /* Find (or create) index into station table for destination station */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002489 sta_id = iwl3945_get_sta_id(priv, hdr);
Zhu Yib481de92007-09-25 17:54:57 -07002490 if (sta_id == IWL_INVALID_STATION) {
Johannes Berge1749612008-10-27 15:59:26 -07002491 IWL_DEBUG_DROP("Dropping - INVALID STATION: %pM\n",
2492 hdr->addr1);
Zhu Yib481de92007-09-25 17:54:57 -07002493 goto drop;
2494 }
2495
2496 IWL_DEBUG_RATE("station Id %d\n", sta_id);
2497
Harvey Harrisonfd7c8a42008-06-11 14:21:56 -07002498 if (ieee80211_is_data_qos(fc)) {
2499 qc = ieee80211_get_qos_ctl(hdr);
Harvey Harrison7294ec92008-07-15 18:43:59 -07002500 tid = qc[0] & IEEE80211_QOS_CTL_TID_MASK;
Zhu Yib481de92007-09-25 17:54:57 -07002501 seq_number = priv->stations[sta_id].tid[tid].seq_number &
2502 IEEE80211_SCTL_SEQ;
2503 hdr->seq_ctrl = cpu_to_le16(seq_number) |
2504 (hdr->seq_ctrl &
2505 __constant_cpu_to_le16(IEEE80211_SCTL_FRAG));
2506 seq_number += 0x10;
2507 }
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002508
2509 /* Descriptor for chosen Tx queue */
Zhu Yib481de92007-09-25 17:54:57 -07002510 txq = &priv->txq[txq_id];
2511 q = &txq->q;
2512
2513 spin_lock_irqsave(&priv->lock, flags);
2514
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002515 /* Set up first empty TFD within this queue's circular TFD buffer */
Tomas Winklerfc4b6852007-10-25 17:15:24 +08002516 tfd = &txq->bd[q->write_ptr];
Zhu Yib481de92007-09-25 17:54:57 -07002517 memset(tfd, 0, sizeof(*tfd));
2518 control_flags = (u32 *) tfd;
Tomas Winklerfc4b6852007-10-25 17:15:24 +08002519 idx = get_cmd_index(q, q->write_ptr, 0);
Zhu Yib481de92007-09-25 17:54:57 -07002520
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002521 /* Set up driver data for this TFD */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002522 memset(&(txq->txb[q->write_ptr]), 0, sizeof(struct iwl3945_tx_info));
Tomas Winklerfc4b6852007-10-25 17:15:24 +08002523 txq->txb[q->write_ptr].skb[0] = skb;
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002524
2525 /* Init first empty entry in queue's array of Tx/cmd buffers */
Zhu Yib481de92007-09-25 17:54:57 -07002526 out_cmd = &txq->cmd[idx];
2527 memset(&out_cmd->hdr, 0, sizeof(out_cmd->hdr));
2528 memset(&out_cmd->cmd.tx, 0, sizeof(out_cmd->cmd.tx));
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002529
2530 /*
2531 * Set up the Tx-command (not MAC!) header.
2532 * Store the chosen Tx queue and TFD index within the sequence field;
2533 * after Tx, uCode's Tx response will return this value so driver can
2534 * locate the frame within the tx queue and do post-tx processing.
2535 */
Zhu Yib481de92007-09-25 17:54:57 -07002536 out_cmd->hdr.cmd = REPLY_TX;
2537 out_cmd->hdr.sequence = cpu_to_le16((u16)(QUEUE_TO_SEQ(txq_id) |
Tomas Winklerfc4b6852007-10-25 17:15:24 +08002538 INDEX_TO_SEQ(q->write_ptr)));
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002539
2540 /* Copy MAC header from skb into command buffer */
Zhu Yib481de92007-09-25 17:54:57 -07002541 memcpy(out_cmd->cmd.tx.hdr, hdr, hdr_len);
2542
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002543 /*
2544 * Use the first empty entry in this queue's command buffer array
2545 * to contain the Tx command and MAC header concatenated together
2546 * (payload data will be in another buffer).
2547 * Size of this varies, due to varying MAC header length.
2548 * If end is not dword aligned, we'll have 2 extra bytes at the end
2549 * of the MAC header (device reads on dword boundaries).
2550 * We'll tell device about this padding later.
2551 */
Zhu Yib481de92007-09-25 17:54:57 -07002552 len = priv->hw_setting.tx_cmd_len +
Tomas Winkler4c897252008-12-19 10:37:05 +08002553 sizeof(struct iwl_cmd_header) + hdr_len;
Zhu Yib481de92007-09-25 17:54:57 -07002554
2555 len_org = len;
2556 len = (len + 3) & ~3;
2557
2558 if (len_org != len)
2559 len_org = 1;
2560 else
2561 len_org = 0;
2562
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002563 /* Physical address of this Tx command's header (not MAC header!),
2564 * within command buffer array. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002565 txcmd_phys = txq->dma_addr_cmd + sizeof(struct iwl3945_cmd) * idx +
2566 offsetof(struct iwl3945_cmd, hdr);
Zhu Yib481de92007-09-25 17:54:57 -07002567
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002568 /* Add buffer containing Tx command and MAC(!) header to TFD's
2569 * first entry */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002570 iwl3945_hw_txq_attach_buf_to_tfd(priv, tfd, txcmd_phys, len);
Zhu Yib481de92007-09-25 17:54:57 -07002571
Johannes Bergd0f09802008-07-29 11:32:07 +02002572 if (info->control.hw_key)
Johannes Berge039fa42008-05-15 12:55:29 +02002573 iwl3945_build_tx_cmd_hwcrypto(priv, info, out_cmd, skb, 0);
Zhu Yib481de92007-09-25 17:54:57 -07002574
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002575 /* Set up TFD's 2nd entry to point directly to remainder of skb,
2576 * if any (802.11 null frames have no payload). */
Zhu Yib481de92007-09-25 17:54:57 -07002577 len = skb->len - hdr_len;
2578 if (len) {
2579 phys_addr = pci_map_single(priv->pci_dev, skb->data + hdr_len,
2580 len, PCI_DMA_TODEVICE);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002581 iwl3945_hw_txq_attach_buf_to_tfd(priv, tfd, phys_addr, len);
Zhu Yib481de92007-09-25 17:54:57 -07002582 }
2583
Zhu Yib481de92007-09-25 17:54:57 -07002584 if (!len)
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002585 /* If there is no payload, then we use only one Tx buffer */
Zhu Yib481de92007-09-25 17:54:57 -07002586 *control_flags = TFD_CTL_COUNT_SET(1);
2587 else
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002588 /* Else use 2 buffers.
2589 * Tell 3945 about any padding after MAC header */
Zhu Yib481de92007-09-25 17:54:57 -07002590 *control_flags = TFD_CTL_COUNT_SET(2) |
2591 TFD_CTL_PAD_SET(U32_PAD(len));
2592
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002593 /* Total # bytes to be transmitted */
Zhu Yib481de92007-09-25 17:54:57 -07002594 len = (u16)skb->len;
2595 out_cmd->cmd.tx.len = cpu_to_le16(len);
2596
2597 /* TODO need this for burst mode later on */
Johannes Berge039fa42008-05-15 12:55:29 +02002598 iwl3945_build_tx_cmd_basic(priv, out_cmd, info, hdr, unicast, sta_id);
Zhu Yib481de92007-09-25 17:54:57 -07002599
2600 /* set is_hcca to 0; it probably will never be implemented */
Johannes Berge039fa42008-05-15 12:55:29 +02002601 iwl3945_hw_build_tx_cmd_rate(priv, out_cmd, info, hdr, sta_id, 0);
Zhu Yib481de92007-09-25 17:54:57 -07002602
2603 out_cmd->cmd.tx.tx_flags &= ~TX_CMD_FLG_ANT_A_MSK;
2604 out_cmd->cmd.tx.tx_flags &= ~TX_CMD_FLG_ANT_B_MSK;
2605
Harvey Harrison8b7b1e02008-06-11 14:21:56 -07002606 if (!ieee80211_has_morefrags(hdr->frame_control)) {
Zhu Yib481de92007-09-25 17:54:57 -07002607 txq->need_update = 1;
Tomas Winkler3ac7f142008-07-21 02:40:14 +03002608 if (qc)
Zhu Yib481de92007-09-25 17:54:57 -07002609 priv->stations[sta_id].tid[tid].seq_number = seq_number;
Zhu Yib481de92007-09-25 17:54:57 -07002610 } else {
2611 wait_write_ptr = 1;
2612 txq->need_update = 0;
2613 }
2614
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08002615 iwl_print_hex_dump(priv, IWL_DL_TX, out_cmd->cmd.payload,
Zhu Yib481de92007-09-25 17:54:57 -07002616 sizeof(out_cmd->cmd.tx));
2617
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08002618 iwl_print_hex_dump(priv, IWL_DL_TX, (u8 *)out_cmd->cmd.tx.hdr,
Harvey Harrison7294ec92008-07-15 18:43:59 -07002619 ieee80211_hdrlen(fc));
Zhu Yib481de92007-09-25 17:54:57 -07002620
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002621 /* Tell device the write index *just past* this latest filled TFD */
Tomas Winklerc54b6792008-03-06 17:36:53 -08002622 q->write_ptr = iwl_queue_inc_wrap(q->write_ptr, q->n_bd);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002623 rc = iwl3945_tx_queue_update_write_ptr(priv, txq);
Zhu Yib481de92007-09-25 17:54:57 -07002624 spin_unlock_irqrestore(&priv->lock, flags);
2625
2626 if (rc)
2627 return rc;
2628
Samuel Ortizd20b3c62008-12-19 10:37:15 +08002629 if ((iwl_queue_space(q) < q->high_mark)
Zhu Yib481de92007-09-25 17:54:57 -07002630 && priv->mac80211_registered) {
2631 if (wait_write_ptr) {
2632 spin_lock_irqsave(&priv->lock, flags);
2633 txq->need_update = 1;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002634 iwl3945_tx_queue_update_write_ptr(priv, txq);
Zhu Yib481de92007-09-25 17:54:57 -07002635 spin_unlock_irqrestore(&priv->lock, flags);
2636 }
2637
Johannes Berge2530082008-05-17 00:57:14 +02002638 ieee80211_stop_queue(priv->hw, skb_get_queue_mapping(skb));
Zhu Yib481de92007-09-25 17:54:57 -07002639 }
2640
2641 return 0;
2642
2643drop_unlock:
2644 spin_unlock_irqrestore(&priv->lock, flags);
2645drop:
2646 return -1;
2647}
2648
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002649static void iwl3945_set_rate(struct iwl3945_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002650{
Johannes Berg8318d782008-01-24 19:38:38 +01002651 const struct ieee80211_supported_band *sband = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07002652 struct ieee80211_rate *rate;
2653 int i;
2654
Johannes Berg8318d782008-01-24 19:38:38 +01002655 sband = iwl3945_get_band(priv, priv->band);
2656 if (!sband) {
Saleem Abdulrasoolc4ba9622007-11-18 23:59:08 -08002657 IWL_ERROR("Failed to set rate: unable to get hw mode\n");
2658 return;
2659 }
Zhu Yib481de92007-09-25 17:54:57 -07002660
2661 priv->active_rate = 0;
2662 priv->active_rate_basic = 0;
2663
Johannes Berg8318d782008-01-24 19:38:38 +01002664 IWL_DEBUG_RATE("Setting rates for %s GHz\n",
2665 sband->band == IEEE80211_BAND_2GHZ ? "2.4" : "5");
Zhu Yib481de92007-09-25 17:54:57 -07002666
Johannes Berg8318d782008-01-24 19:38:38 +01002667 for (i = 0; i < sband->n_bitrates; i++) {
2668 rate = &sband->bitrates[i];
2669 if ((rate->hw_value < IWL_RATE_COUNT) &&
2670 !(rate->flags & IEEE80211_CHAN_DISABLED)) {
2671 IWL_DEBUG_RATE("Adding rate index %d (plcp %d)\n",
2672 rate->hw_value, iwl3945_rates[rate->hw_value].plcp);
2673 priv->active_rate |= (1 << rate->hw_value);
2674 }
Zhu Yib481de92007-09-25 17:54:57 -07002675 }
2676
2677 IWL_DEBUG_RATE("Set active_rate = %0x, active_rate_basic = %0x\n",
2678 priv->active_rate, priv->active_rate_basic);
2679
2680 /*
2681 * If a basic rate is configured, then use it (adding IWL_RATE_1M_MASK)
2682 * otherwise set it to the default of all CCK rates and 6, 12, 24 for
2683 * OFDM
2684 */
2685 if (priv->active_rate_basic & IWL_CCK_BASIC_RATES_MASK)
2686 priv->staging_rxon.cck_basic_rates =
2687 ((priv->active_rate_basic &
2688 IWL_CCK_RATES_MASK) >> IWL_FIRST_CCK_RATE) & 0xF;
2689 else
2690 priv->staging_rxon.cck_basic_rates =
2691 (IWL_CCK_BASIC_RATES_MASK >> IWL_FIRST_CCK_RATE) & 0xF;
2692
2693 if (priv->active_rate_basic & IWL_OFDM_BASIC_RATES_MASK)
2694 priv->staging_rxon.ofdm_basic_rates =
2695 ((priv->active_rate_basic &
2696 (IWL_OFDM_BASIC_RATES_MASK | IWL_RATE_6M_MASK)) >>
2697 IWL_FIRST_OFDM_RATE) & 0xFF;
2698 else
2699 priv->staging_rxon.ofdm_basic_rates =
2700 (IWL_OFDM_BASIC_RATES_MASK >> IWL_FIRST_OFDM_RATE) & 0xFF;
2701}
2702
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002703static void iwl3945_radio_kill_sw(struct iwl3945_priv *priv, int disable_radio)
Zhu Yib481de92007-09-25 17:54:57 -07002704{
2705 unsigned long flags;
2706
2707 if (!!disable_radio == test_bit(STATUS_RF_KILL_SW, &priv->status))
2708 return;
2709
2710 IWL_DEBUG_RF_KILL("Manual SW RF KILL set to: RADIO %s\n",
2711 disable_radio ? "OFF" : "ON");
2712
2713 if (disable_radio) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002714 iwl3945_scan_cancel(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002715 /* FIXME: This is a workaround for AP */
Johannes Berg05c914f2008-09-11 00:01:58 +02002716 if (priv->iw_mode != NL80211_IFTYPE_AP) {
Zhu Yib481de92007-09-25 17:54:57 -07002717 spin_lock_irqsave(&priv->lock, flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002718 iwl3945_write32(priv, CSR_UCODE_DRV_GP1_SET,
Zhu Yib481de92007-09-25 17:54:57 -07002719 CSR_UCODE_SW_BIT_RFKILL);
2720 spin_unlock_irqrestore(&priv->lock, flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002721 iwl3945_send_card_state(priv, CARD_STATE_CMD_DISABLE, 0);
Zhu Yib481de92007-09-25 17:54:57 -07002722 set_bit(STATUS_RF_KILL_SW, &priv->status);
2723 }
2724 return;
2725 }
2726
2727 spin_lock_irqsave(&priv->lock, flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002728 iwl3945_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
Zhu Yib481de92007-09-25 17:54:57 -07002729
2730 clear_bit(STATUS_RF_KILL_SW, &priv->status);
2731 spin_unlock_irqrestore(&priv->lock, flags);
2732
2733 /* wake up ucode */
2734 msleep(10);
2735
2736 spin_lock_irqsave(&priv->lock, flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002737 iwl3945_read32(priv, CSR_UCODE_DRV_GP1);
2738 if (!iwl3945_grab_nic_access(priv))
2739 iwl3945_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002740 spin_unlock_irqrestore(&priv->lock, flags);
2741
2742 if (test_bit(STATUS_RF_KILL_HW, &priv->status)) {
2743 IWL_DEBUG_RF_KILL("Can not turn radio back on - "
2744 "disabled by HW switch\n");
2745 return;
2746 }
2747
Zhu Yi808e72a2008-06-12 09:47:17 +08002748 if (priv->is_open)
2749 queue_work(priv->workqueue, &priv->restart);
Zhu Yib481de92007-09-25 17:54:57 -07002750 return;
2751}
2752
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002753void iwl3945_set_decrypted_flag(struct iwl3945_priv *priv, struct sk_buff *skb,
Zhu Yib481de92007-09-25 17:54:57 -07002754 u32 decrypt_res, struct ieee80211_rx_status *stats)
2755{
2756 u16 fc =
2757 le16_to_cpu(((struct ieee80211_hdr *)skb->data)->frame_control);
2758
2759 if (priv->active_rxon.filter_flags & RXON_FILTER_DIS_DECRYPT_MSK)
2760 return;
2761
2762 if (!(fc & IEEE80211_FCTL_PROTECTED))
2763 return;
2764
2765 IWL_DEBUG_RX("decrypt_res:0x%x\n", decrypt_res);
2766 switch (decrypt_res & RX_RES_STATUS_SEC_TYPE_MSK) {
2767 case RX_RES_STATUS_SEC_TYPE_TKIP:
2768 if ((decrypt_res & RX_RES_STATUS_DECRYPT_TYPE_MSK) ==
2769 RX_RES_STATUS_BAD_ICV_MIC)
2770 stats->flag |= RX_FLAG_MMIC_ERROR;
2771 case RX_RES_STATUS_SEC_TYPE_WEP:
2772 case RX_RES_STATUS_SEC_TYPE_CCMP:
2773 if ((decrypt_res & RX_RES_STATUS_DECRYPT_TYPE_MSK) ==
2774 RX_RES_STATUS_DECRYPT_OK) {
2775 IWL_DEBUG_RX("hw decrypt successfully!!!\n");
2776 stats->flag |= RX_FLAG_DECRYPTED;
2777 }
2778 break;
2779
2780 default:
2781 break;
2782 }
2783}
2784
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08002785#ifdef CONFIG_IWL3945_SPECTRUM_MEASUREMENT
Zhu Yib481de92007-09-25 17:54:57 -07002786
2787#include "iwl-spectrum.h"
2788
2789#define BEACON_TIME_MASK_LOW 0x00FFFFFF
2790#define BEACON_TIME_MASK_HIGH 0xFF000000
2791#define TIME_UNIT 1024
2792
2793/*
2794 * extended beacon time format
2795 * time in usec will be changed into a 32-bit value in 8:24 format
2796 * the high 1 byte is the beacon counts
2797 * the lower 3 bytes is the time in usec within one beacon interval
2798 */
2799
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002800static u32 iwl3945_usecs_to_beacons(u32 usec, u32 beacon_interval)
Zhu Yib481de92007-09-25 17:54:57 -07002801{
2802 u32 quot;
2803 u32 rem;
2804 u32 interval = beacon_interval * 1024;
2805
2806 if (!interval || !usec)
2807 return 0;
2808
2809 quot = (usec / interval) & (BEACON_TIME_MASK_HIGH >> 24);
2810 rem = (usec % interval) & BEACON_TIME_MASK_LOW;
2811
2812 return (quot << 24) + rem;
2813}
2814
2815/* base is usually what we get from ucode with each received frame,
2816 * the same as HW timer counter counting down
2817 */
2818
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002819static __le32 iwl3945_add_beacon_time(u32 base, u32 addon, u32 beacon_interval)
Zhu Yib481de92007-09-25 17:54:57 -07002820{
2821 u32 base_low = base & BEACON_TIME_MASK_LOW;
2822 u32 addon_low = addon & BEACON_TIME_MASK_LOW;
2823 u32 interval = beacon_interval * TIME_UNIT;
2824 u32 res = (base & BEACON_TIME_MASK_HIGH) +
2825 (addon & BEACON_TIME_MASK_HIGH);
2826
2827 if (base_low > addon_low)
2828 res += base_low - addon_low;
2829 else if (base_low < addon_low) {
2830 res += interval + base_low - addon_low;
2831 res += (1 << 24);
2832 } else
2833 res += (1 << 24);
2834
2835 return cpu_to_le32(res);
2836}
2837
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002838static int iwl3945_get_measurement(struct iwl3945_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07002839 struct ieee80211_measurement_params *params,
2840 u8 type)
2841{
Tomas Winkler600c0e12008-12-19 10:37:04 +08002842 struct iwl_spectrum_cmd spectrum;
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08002843 struct iwl_rx_packet *res;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002844 struct iwl3945_host_cmd cmd = {
Zhu Yib481de92007-09-25 17:54:57 -07002845 .id = REPLY_SPECTRUM_MEASUREMENT_CMD,
2846 .data = (void *)&spectrum,
2847 .meta.flags = CMD_WANT_SKB,
2848 };
2849 u32 add_time = le64_to_cpu(params->start_time);
2850 int rc;
2851 int spectrum_resp_status;
2852 int duration = le16_to_cpu(params->duration);
2853
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002854 if (iwl3945_is_associated(priv))
Zhu Yib481de92007-09-25 17:54:57 -07002855 add_time =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002856 iwl3945_usecs_to_beacons(
Zhu Yib481de92007-09-25 17:54:57 -07002857 le64_to_cpu(params->start_time) - priv->last_tsf,
2858 le16_to_cpu(priv->rxon_timing.beacon_interval));
2859
2860 memset(&spectrum, 0, sizeof(spectrum));
2861
2862 spectrum.channel_count = cpu_to_le16(1);
2863 spectrum.flags =
2864 RXON_FLG_TSF2HOST_MSK | RXON_FLG_ANT_A_MSK | RXON_FLG_DIS_DIV_MSK;
2865 spectrum.filter_flags = MEASUREMENT_FILTER_FLAG;
2866 cmd.len = sizeof(spectrum);
2867 spectrum.len = cpu_to_le16(cmd.len - sizeof(spectrum.len));
2868
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002869 if (iwl3945_is_associated(priv))
Zhu Yib481de92007-09-25 17:54:57 -07002870 spectrum.start_time =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002871 iwl3945_add_beacon_time(priv->last_beacon_time,
Zhu Yib481de92007-09-25 17:54:57 -07002872 add_time,
2873 le16_to_cpu(priv->rxon_timing.beacon_interval));
2874 else
2875 spectrum.start_time = 0;
2876
2877 spectrum.channels[0].duration = cpu_to_le32(duration * TIME_UNIT);
2878 spectrum.channels[0].channel = params->channel;
2879 spectrum.channels[0].type = type;
2880 if (priv->active_rxon.flags & RXON_FLG_BAND_24G_MSK)
2881 spectrum.flags |= RXON_FLG_BAND_24G_MSK |
2882 RXON_FLG_AUTO_DETECT_MSK | RXON_FLG_TGG_PROTECT_MSK;
2883
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002884 rc = iwl3945_send_cmd_sync(priv, &cmd);
Zhu Yib481de92007-09-25 17:54:57 -07002885 if (rc)
2886 return rc;
2887
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08002888 res = (struct iwl_rx_packet *)cmd.meta.u.skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07002889 if (res->hdr.flags & IWL_CMD_FAILED_MSK) {
2890 IWL_ERROR("Bad return from REPLY_RX_ON_ASSOC command\n");
2891 rc = -EIO;
2892 }
2893
2894 spectrum_resp_status = le16_to_cpu(res->u.spectrum.status);
2895 switch (spectrum_resp_status) {
2896 case 0: /* Command will be handled */
2897 if (res->u.spectrum.id != 0xff) {
Ian Schrambc434dd2007-10-25 17:15:29 +08002898 IWL_DEBUG_INFO("Replaced existing measurement: %d\n",
2899 res->u.spectrum.id);
Zhu Yib481de92007-09-25 17:54:57 -07002900 priv->measurement_status &= ~MEASUREMENT_READY;
2901 }
2902 priv->measurement_status |= MEASUREMENT_ACTIVE;
2903 rc = 0;
2904 break;
2905
2906 case 1: /* Command will not be handled */
2907 rc = -EAGAIN;
2908 break;
2909 }
2910
2911 dev_kfree_skb_any(cmd.meta.u.skb);
2912
2913 return rc;
2914}
2915#endif
2916
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002917static void iwl3945_rx_reply_alive(struct iwl3945_priv *priv,
2918 struct iwl3945_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07002919{
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08002920 struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
2921 struct iwl_alive_resp *palive;
Zhu Yib481de92007-09-25 17:54:57 -07002922 struct delayed_work *pwork;
2923
2924 palive = &pkt->u.alive_frame;
2925
2926 IWL_DEBUG_INFO("Alive ucode status 0x%08X revision "
2927 "0x%01X 0x%01X\n",
2928 palive->is_valid, palive->ver_type,
2929 palive->ver_subtype);
2930
2931 if (palive->ver_subtype == INITIALIZE_SUBTYPE) {
2932 IWL_DEBUG_INFO("Initialization Alive received.\n");
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08002933 memcpy(&priv->card_alive_init, &pkt->u.alive_frame,
2934 sizeof(struct iwl_alive_resp));
Zhu Yib481de92007-09-25 17:54:57 -07002935 pwork = &priv->init_alive_start;
2936 } else {
2937 IWL_DEBUG_INFO("Runtime Alive received.\n");
2938 memcpy(&priv->card_alive, &pkt->u.alive_frame,
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08002939 sizeof(struct iwl_alive_resp));
Zhu Yib481de92007-09-25 17:54:57 -07002940 pwork = &priv->alive_start;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002941 iwl3945_disable_events(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002942 }
2943
2944 /* We delay the ALIVE response by 5ms to
2945 * give the HW RF Kill time to activate... */
2946 if (palive->is_valid == UCODE_VALID_OK)
2947 queue_delayed_work(priv->workqueue, pwork,
2948 msecs_to_jiffies(5));
2949 else
2950 IWL_WARNING("uCode did not respond OK.\n");
2951}
2952
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002953static void iwl3945_rx_reply_add_sta(struct iwl3945_priv *priv,
2954 struct iwl3945_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07002955{
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08002956 struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07002957
2958 IWL_DEBUG_RX("Received REPLY_ADD_STA: 0x%02X\n", pkt->u.status);
2959 return;
2960}
2961
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002962static void iwl3945_rx_reply_error(struct iwl3945_priv *priv,
2963 struct iwl3945_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07002964{
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08002965 struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07002966
2967 IWL_ERROR("Error Reply type 0x%08X cmd %s (0x%02X) "
2968 "seq 0x%04X ser 0x%08X\n",
2969 le32_to_cpu(pkt->u.err_resp.error_type),
2970 get_cmd_string(pkt->u.err_resp.cmd_id),
2971 pkt->u.err_resp.cmd_id,
2972 le16_to_cpu(pkt->u.err_resp.bad_cmd_seq_num),
2973 le32_to_cpu(pkt->u.err_resp.error_info));
2974}
2975
2976#define TX_STATUS_ENTRY(x) case TX_STATUS_FAIL_ ## x: return #x
2977
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002978static void iwl3945_rx_csa(struct iwl3945_priv *priv, struct iwl3945_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07002979{
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08002980 struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002981 struct iwl3945_rxon_cmd *rxon = (void *)&priv->active_rxon;
Tomas Winkler600c0e12008-12-19 10:37:04 +08002982 struct iwl_csa_notification *csa = &(pkt->u.csa_notif);
Zhu Yib481de92007-09-25 17:54:57 -07002983 IWL_DEBUG_11H("CSA notif: channel %d, status %d\n",
2984 le16_to_cpu(csa->channel), le32_to_cpu(csa->status));
2985 rxon->channel = csa->channel;
2986 priv->staging_rxon.channel = csa->channel;
2987}
2988
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002989static void iwl3945_rx_spectrum_measure_notif(struct iwl3945_priv *priv,
2990 struct iwl3945_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07002991{
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08002992#ifdef CONFIG_IWL3945_SPECTRUM_MEASUREMENT
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08002993 struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
Tomas Winkler600c0e12008-12-19 10:37:04 +08002994 struct iwl_spectrum_notification *report = &(pkt->u.spectrum_notif);
Zhu Yib481de92007-09-25 17:54:57 -07002995
2996 if (!report->state) {
2997 IWL_DEBUG(IWL_DL_11H | IWL_DL_INFO,
2998 "Spectrum Measure Notification: Start\n");
2999 return;
3000 }
3001
3002 memcpy(&priv->measure_report, report, sizeof(*report));
3003 priv->measurement_status |= MEASUREMENT_READY;
3004#endif
3005}
3006
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003007static void iwl3945_rx_pm_sleep_notif(struct iwl3945_priv *priv,
3008 struct iwl3945_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003009{
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08003010#ifdef CONFIG_IWL3945_DEBUG
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08003011 struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
Tomas Winkler600c0e12008-12-19 10:37:04 +08003012 struct iwl_sleep_notification *sleep = &(pkt->u.sleep_notif);
Zhu Yib481de92007-09-25 17:54:57 -07003013 IWL_DEBUG_RX("sleep mode: %d, src: %d\n",
3014 sleep->pm_sleep_mode, sleep->pm_wakeup_src);
3015#endif
3016}
3017
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003018static void iwl3945_rx_pm_debug_statistics_notif(struct iwl3945_priv *priv,
3019 struct iwl3945_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003020{
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08003021 struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07003022 IWL_DEBUG_RADIO("Dumping %d bytes of unhandled "
3023 "notification for %s:\n",
3024 le32_to_cpu(pkt->len), get_cmd_string(pkt->hdr.cmd));
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08003025 iwl_print_hex_dump(priv, IWL_DL_RADIO, pkt->u.raw,
3026 le32_to_cpu(pkt->len));
Zhu Yib481de92007-09-25 17:54:57 -07003027}
3028
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003029static void iwl3945_bg_beacon_update(struct work_struct *work)
Zhu Yib481de92007-09-25 17:54:57 -07003030{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003031 struct iwl3945_priv *priv =
3032 container_of(work, struct iwl3945_priv, beacon_update);
Zhu Yib481de92007-09-25 17:54:57 -07003033 struct sk_buff *beacon;
3034
3035 /* Pull updated AP beacon from mac80211. will fail if not in AP mode */
Johannes Berge039fa42008-05-15 12:55:29 +02003036 beacon = ieee80211_beacon_get(priv->hw, priv->vif);
Zhu Yib481de92007-09-25 17:54:57 -07003037
3038 if (!beacon) {
3039 IWL_ERROR("update beacon failed\n");
3040 return;
3041 }
3042
3043 mutex_lock(&priv->mutex);
3044 /* new beacon skb is allocated every time; dispose previous.*/
3045 if (priv->ibss_beacon)
3046 dev_kfree_skb(priv->ibss_beacon);
3047
3048 priv->ibss_beacon = beacon;
3049 mutex_unlock(&priv->mutex);
3050
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003051 iwl3945_send_beacon_cmd(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003052}
3053
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003054static void iwl3945_rx_beacon_notif(struct iwl3945_priv *priv,
3055 struct iwl3945_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003056{
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08003057#ifdef CONFIG_IWL3945_DEBUG
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08003058 struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003059 struct iwl3945_beacon_notif *beacon = &(pkt->u.beacon_status);
Zhu Yib481de92007-09-25 17:54:57 -07003060 u8 rate = beacon->beacon_notify_hdr.rate;
3061
3062 IWL_DEBUG_RX("beacon status %x retries %d iss %d "
3063 "tsf %d %d rate %d\n",
3064 le32_to_cpu(beacon->beacon_notify_hdr.status) & TX_STATUS_MSK,
3065 beacon->beacon_notify_hdr.failure_frame,
3066 le32_to_cpu(beacon->ibss_mgr_status),
3067 le32_to_cpu(beacon->high_tsf),
3068 le32_to_cpu(beacon->low_tsf), rate);
3069#endif
3070
Johannes Berg05c914f2008-09-11 00:01:58 +02003071 if ((priv->iw_mode == NL80211_IFTYPE_AP) &&
Zhu Yib481de92007-09-25 17:54:57 -07003072 (!test_bit(STATUS_EXIT_PENDING, &priv->status)))
3073 queue_work(priv->workqueue, &priv->beacon_update);
3074}
3075
3076/* Service response to REPLY_SCAN_CMD (0x80) */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003077static void iwl3945_rx_reply_scan(struct iwl3945_priv *priv,
3078 struct iwl3945_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003079{
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08003080#ifdef CONFIG_IWL3945_DEBUG
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08003081 struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
Tomas Winkler4c897252008-12-19 10:37:05 +08003082 struct iwl_scanreq_notification *notif =
3083 (struct iwl_scanreq_notification *)pkt->u.raw;
Zhu Yib481de92007-09-25 17:54:57 -07003084
3085 IWL_DEBUG_RX("Scan request status = 0x%x\n", notif->status);
3086#endif
3087}
3088
3089/* Service SCAN_START_NOTIFICATION (0x82) */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003090static void iwl3945_rx_scan_start_notif(struct iwl3945_priv *priv,
3091 struct iwl3945_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003092{
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08003093 struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
Tomas Winkler4c897252008-12-19 10:37:05 +08003094 struct iwl_scanstart_notification *notif =
3095 (struct iwl_scanstart_notification *)pkt->u.raw;
Zhu Yib481de92007-09-25 17:54:57 -07003096 priv->scan_start_tsf = le32_to_cpu(notif->tsf_low);
3097 IWL_DEBUG_SCAN("Scan start: "
3098 "%d [802.11%s] "
3099 "(TSF: 0x%08X:%08X) - %d (beacon timer %u)\n",
3100 notif->channel,
3101 notif->band ? "bg" : "a",
3102 notif->tsf_high,
3103 notif->tsf_low, notif->status, notif->beacon_timer);
3104}
3105
3106/* Service SCAN_RESULTS_NOTIFICATION (0x83) */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003107static void iwl3945_rx_scan_results_notif(struct iwl3945_priv *priv,
3108 struct iwl3945_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003109{
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08003110 struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
Tomas Winkler4c897252008-12-19 10:37:05 +08003111 struct iwl_scanresults_notification *notif =
3112 (struct iwl_scanresults_notification *)pkt->u.raw;
Zhu Yib481de92007-09-25 17:54:57 -07003113
3114 IWL_DEBUG_SCAN("Scan ch.res: "
3115 "%d [802.11%s] "
3116 "(TSF: 0x%08X:%08X) - %d "
3117 "elapsed=%lu usec (%dms since last)\n",
3118 notif->channel,
3119 notif->band ? "bg" : "a",
3120 le32_to_cpu(notif->tsf_high),
3121 le32_to_cpu(notif->tsf_low),
3122 le32_to_cpu(notif->statistics[0]),
3123 le32_to_cpu(notif->tsf_low) - priv->scan_start_tsf,
3124 jiffies_to_msecs(elapsed_jiffies
3125 (priv->last_scan_jiffies, jiffies)));
3126
3127 priv->last_scan_jiffies = jiffies;
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08003128 priv->next_scan_jiffies = 0;
Zhu Yib481de92007-09-25 17:54:57 -07003129}
3130
3131/* Service SCAN_COMPLETE_NOTIFICATION (0x84) */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003132static void iwl3945_rx_scan_complete_notif(struct iwl3945_priv *priv,
3133 struct iwl3945_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003134{
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08003135 struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
Tomas Winkler4c897252008-12-19 10:37:05 +08003136 struct iwl_scancomplete_notification *scan_notif = (void *)pkt->u.raw;
Zhu Yib481de92007-09-25 17:54:57 -07003137
3138 IWL_DEBUG_SCAN("Scan complete: %d channels (TSF 0x%08X:%08X) - %d\n",
3139 scan_notif->scanned_channels,
3140 scan_notif->tsf_low,
3141 scan_notif->tsf_high, scan_notif->status);
3142
3143 /* The HW is no longer scanning */
3144 clear_bit(STATUS_SCAN_HW, &priv->status);
3145
3146 /* The scan completion notification came in, so kill that timer... */
3147 cancel_delayed_work(&priv->scan_check);
3148
3149 IWL_DEBUG_INFO("Scan pass on %sGHz took %dms\n",
Ron Rindjunsky66b50042008-06-25 16:46:31 +08003150 (priv->scan_bands & BIT(IEEE80211_BAND_2GHZ)) ?
3151 "2.4" : "5.2",
Zhu Yib481de92007-09-25 17:54:57 -07003152 jiffies_to_msecs(elapsed_jiffies
3153 (priv->scan_pass_start, jiffies)));
3154
Ron Rindjunsky66b50042008-06-25 16:46:31 +08003155 /* Remove this scanned band from the list of pending
3156 * bands to scan, band G precedes A in order of scanning
3157 * as seen in iwl3945_bg_request_scan */
3158 if (priv->scan_bands & BIT(IEEE80211_BAND_2GHZ))
3159 priv->scan_bands &= ~BIT(IEEE80211_BAND_2GHZ);
3160 else if (priv->scan_bands & BIT(IEEE80211_BAND_5GHZ))
3161 priv->scan_bands &= ~BIT(IEEE80211_BAND_5GHZ);
Zhu Yib481de92007-09-25 17:54:57 -07003162
3163 /* If a request to abort was given, or the scan did not succeed
3164 * then we reset the scan state machine and terminate,
3165 * re-queuing another scan if one has been requested */
3166 if (test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
3167 IWL_DEBUG_INFO("Aborted scan completed.\n");
3168 clear_bit(STATUS_SCAN_ABORTING, &priv->status);
3169 } else {
3170 /* If there are more bands on this scan pass reschedule */
3171 if (priv->scan_bands > 0)
3172 goto reschedule;
3173 }
3174
3175 priv->last_scan_jiffies = jiffies;
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08003176 priv->next_scan_jiffies = 0;
Zhu Yib481de92007-09-25 17:54:57 -07003177 IWL_DEBUG_INFO("Setting scan to off\n");
3178
3179 clear_bit(STATUS_SCANNING, &priv->status);
3180
3181 IWL_DEBUG_INFO("Scan took %dms\n",
3182 jiffies_to_msecs(elapsed_jiffies(priv->scan_start, jiffies)));
3183
3184 queue_work(priv->workqueue, &priv->scan_completed);
3185
3186 return;
3187
3188reschedule:
3189 priv->scan_pass_start = jiffies;
3190 queue_work(priv->workqueue, &priv->request_scan);
3191}
3192
3193/* Handle notification from uCode that card's power state is changing
3194 * due to software, hardware, or critical temperature RFKILL */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003195static void iwl3945_rx_card_state_notif(struct iwl3945_priv *priv,
3196 struct iwl3945_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003197{
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08003198 struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07003199 u32 flags = le32_to_cpu(pkt->u.card_state_notif.flags);
3200 unsigned long status = priv->status;
3201
3202 IWL_DEBUG_RF_KILL("Card state received: HW:%s SW:%s\n",
3203 (flags & HW_CARD_DISABLED) ? "Kill" : "On",
3204 (flags & SW_CARD_DISABLED) ? "Kill" : "On");
3205
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003206 iwl3945_write32(priv, CSR_UCODE_DRV_GP1_SET,
Zhu Yib481de92007-09-25 17:54:57 -07003207 CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
3208
3209 if (flags & HW_CARD_DISABLED)
3210 set_bit(STATUS_RF_KILL_HW, &priv->status);
3211 else
3212 clear_bit(STATUS_RF_KILL_HW, &priv->status);
3213
3214
3215 if (flags & SW_CARD_DISABLED)
3216 set_bit(STATUS_RF_KILL_SW, &priv->status);
3217 else
3218 clear_bit(STATUS_RF_KILL_SW, &priv->status);
3219
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003220 iwl3945_scan_cancel(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003221
3222 if ((test_bit(STATUS_RF_KILL_HW, &status) !=
3223 test_bit(STATUS_RF_KILL_HW, &priv->status)) ||
3224 (test_bit(STATUS_RF_KILL_SW, &status) !=
3225 test_bit(STATUS_RF_KILL_SW, &priv->status)))
3226 queue_work(priv->workqueue, &priv->rf_kill);
3227 else
3228 wake_up_interruptible(&priv->wait_command_queue);
3229}
3230
3231/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003232 * iwl3945_setup_rx_handlers - Initialize Rx handler callbacks
Zhu Yib481de92007-09-25 17:54:57 -07003233 *
3234 * Setup the RX handlers for each of the reply types sent from the uCode
3235 * to the host.
3236 *
3237 * This function chains into the hardware specific files for them to setup
3238 * any hardware specific handlers as well.
3239 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003240static void iwl3945_setup_rx_handlers(struct iwl3945_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003241{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003242 priv->rx_handlers[REPLY_ALIVE] = iwl3945_rx_reply_alive;
3243 priv->rx_handlers[REPLY_ADD_STA] = iwl3945_rx_reply_add_sta;
3244 priv->rx_handlers[REPLY_ERROR] = iwl3945_rx_reply_error;
3245 priv->rx_handlers[CHANNEL_SWITCH_NOTIFICATION] = iwl3945_rx_csa;
Zhu Yib481de92007-09-25 17:54:57 -07003246 priv->rx_handlers[SPECTRUM_MEASURE_NOTIFICATION] =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003247 iwl3945_rx_spectrum_measure_notif;
3248 priv->rx_handlers[PM_SLEEP_NOTIFICATION] = iwl3945_rx_pm_sleep_notif;
Zhu Yib481de92007-09-25 17:54:57 -07003249 priv->rx_handlers[PM_DEBUG_STATISTIC_NOTIFIC] =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003250 iwl3945_rx_pm_debug_statistics_notif;
3251 priv->rx_handlers[BEACON_NOTIFICATION] = iwl3945_rx_beacon_notif;
Zhu Yib481de92007-09-25 17:54:57 -07003252
Ben Cahill9fbab512007-11-29 11:09:47 +08003253 /*
3254 * The same handler is used for both the REPLY to a discrete
3255 * statistics request from the host as well as for the periodic
3256 * statistics notifications (after received beacons) from the uCode.
Zhu Yib481de92007-09-25 17:54:57 -07003257 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003258 priv->rx_handlers[REPLY_STATISTICS_CMD] = iwl3945_hw_rx_statistics;
3259 priv->rx_handlers[STATISTICS_NOTIFICATION] = iwl3945_hw_rx_statistics;
Zhu Yib481de92007-09-25 17:54:57 -07003260
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003261 priv->rx_handlers[REPLY_SCAN_CMD] = iwl3945_rx_reply_scan;
3262 priv->rx_handlers[SCAN_START_NOTIFICATION] = iwl3945_rx_scan_start_notif;
Zhu Yib481de92007-09-25 17:54:57 -07003263 priv->rx_handlers[SCAN_RESULTS_NOTIFICATION] =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003264 iwl3945_rx_scan_results_notif;
Zhu Yib481de92007-09-25 17:54:57 -07003265 priv->rx_handlers[SCAN_COMPLETE_NOTIFICATION] =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003266 iwl3945_rx_scan_complete_notif;
3267 priv->rx_handlers[CARD_STATE_NOTIFICATION] = iwl3945_rx_card_state_notif;
Zhu Yib481de92007-09-25 17:54:57 -07003268
Ben Cahill9fbab512007-11-29 11:09:47 +08003269 /* Set up hardware specific Rx handlers */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003270 iwl3945_hw_rx_handler_setup(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003271}
3272
3273/**
Tomas Winkler91c066f2008-03-06 17:36:55 -08003274 * iwl3945_cmd_queue_reclaim - Reclaim CMD queue entries
3275 * When FW advances 'R' index, all entries between old and new 'R' index
3276 * need to be reclaimed.
3277 */
3278static void iwl3945_cmd_queue_reclaim(struct iwl3945_priv *priv,
3279 int txq_id, int index)
3280{
3281 struct iwl3945_tx_queue *txq = &priv->txq[txq_id];
Samuel Ortizd20b3c62008-12-19 10:37:15 +08003282 struct iwl_queue *q = &txq->q;
Tomas Winkler91c066f2008-03-06 17:36:55 -08003283 int nfreed = 0;
3284
3285 if ((index >= q->n_bd) || (iwl3945_x2_queue_used(q, index) == 0)) {
3286 IWL_ERROR("Read index for DMA queue txq id (%d), index %d, "
3287 "is out of range [0-%d] %d %d.\n", txq_id,
3288 index, q->n_bd, q->write_ptr, q->read_ptr);
3289 return;
3290 }
3291
3292 for (index = iwl_queue_inc_wrap(index, q->n_bd); q->read_ptr != index;
3293 q->read_ptr = iwl_queue_inc_wrap(q->read_ptr, q->n_bd)) {
3294 if (nfreed > 1) {
3295 IWL_ERROR("HCMD skipped: index (%d) %d %d\n", index,
3296 q->write_ptr, q->read_ptr);
3297 queue_work(priv->workqueue, &priv->restart);
3298 break;
3299 }
3300 nfreed++;
3301 }
3302}
3303
3304
3305/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003306 * iwl3945_tx_cmd_complete - Pull unused buffers off the queue and reclaim them
Zhu Yib481de92007-09-25 17:54:57 -07003307 * @rxb: Rx buffer to reclaim
3308 *
3309 * If an Rx buffer has an async callback associated with it the callback
3310 * will be executed. The attached skb (if present) will only be freed
3311 * if the callback returns 1
3312 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003313static void iwl3945_tx_cmd_complete(struct iwl3945_priv *priv,
3314 struct iwl3945_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003315{
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08003316 struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07003317 u16 sequence = le16_to_cpu(pkt->hdr.sequence);
3318 int txq_id = SEQ_TO_QUEUE(sequence);
3319 int index = SEQ_TO_INDEX(sequence);
Tomas Winkler600c0e12008-12-19 10:37:04 +08003320 int huge = !!(pkt->hdr.sequence & SEQ_HUGE_FRAME);
Zhu Yib481de92007-09-25 17:54:57 -07003321 int cmd_index;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003322 struct iwl3945_cmd *cmd;
Zhu Yib481de92007-09-25 17:54:57 -07003323
Zhu Yib481de92007-09-25 17:54:57 -07003324 BUG_ON(txq_id != IWL_CMD_QUEUE_NUM);
3325
3326 cmd_index = get_cmd_index(&priv->txq[IWL_CMD_QUEUE_NUM].q, index, huge);
3327 cmd = &priv->txq[IWL_CMD_QUEUE_NUM].cmd[cmd_index];
3328
3329 /* Input error checking is done when commands are added to queue. */
3330 if (cmd->meta.flags & CMD_WANT_SKB) {
3331 cmd->meta.source->u.skb = rxb->skb;
3332 rxb->skb = NULL;
3333 } else if (cmd->meta.u.callback &&
3334 !cmd->meta.u.callback(priv, cmd, rxb->skb))
3335 rxb->skb = NULL;
3336
Tomas Winkler91c066f2008-03-06 17:36:55 -08003337 iwl3945_cmd_queue_reclaim(priv, txq_id, index);
Zhu Yib481de92007-09-25 17:54:57 -07003338
3339 if (!(cmd->meta.flags & CMD_ASYNC)) {
3340 clear_bit(STATUS_HCMD_ACTIVE, &priv->status);
3341 wake_up_interruptible(&priv->wait_command_queue);
3342 }
3343}
3344
3345/************************** RX-FUNCTIONS ****************************/
3346/*
3347 * Rx theory of operation
3348 *
3349 * The host allocates 32 DMA target addresses and passes the host address
3350 * to the firmware at register IWL_RFDS_TABLE_LOWER + N * RFD_SIZE where N is
3351 * 0 to 31
3352 *
3353 * Rx Queue Indexes
3354 * The host/firmware share two index registers for managing the Rx buffers.
3355 *
3356 * The READ index maps to the first position that the firmware may be writing
3357 * to -- the driver can read up to (but not including) this position and get
3358 * good data.
3359 * The READ index is managed by the firmware once the card is enabled.
3360 *
3361 * The WRITE index maps to the last position the driver has read from -- the
3362 * position preceding WRITE is the last slot the firmware can place a packet.
3363 *
3364 * The queue is empty (no good data) if WRITE = READ - 1, and is full if
3365 * WRITE = READ.
3366 *
Ben Cahill9fbab512007-11-29 11:09:47 +08003367 * During initialization, the host sets up the READ queue position to the first
Zhu Yib481de92007-09-25 17:54:57 -07003368 * INDEX position, and WRITE to the last (READ - 1 wrapped)
3369 *
Ben Cahill9fbab512007-11-29 11:09:47 +08003370 * When the firmware places a packet in a buffer, it will advance the READ index
Zhu Yib481de92007-09-25 17:54:57 -07003371 * and fire the RX interrupt. The driver can then query the READ index and
3372 * process as many packets as possible, moving the WRITE index forward as it
3373 * resets the Rx queue buffers with new memory.
3374 *
3375 * The management in the driver is as follows:
3376 * + A list of pre-allocated SKBs is stored in iwl->rxq->rx_free. When
3377 * iwl->rxq->free_count drops to or below RX_LOW_WATERMARK, work is scheduled
Ian Schram01ebd062007-10-25 17:15:22 +08003378 * to replenish the iwl->rxq->rx_free.
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003379 * + In iwl3945_rx_replenish (scheduled) if 'processed' != 'read' then the
Zhu Yib481de92007-09-25 17:54:57 -07003380 * iwl->rxq is replenished and the READ INDEX is updated (updating the
3381 * 'processed' and 'read' driver indexes as well)
3382 * + A received packet is processed and handed to the kernel network stack,
3383 * detached from the iwl->rxq. The driver 'processed' index is updated.
3384 * + The Host/Firmware iwl->rxq is replenished at tasklet time from the rx_free
3385 * list. If there are no allocated buffers in iwl->rxq->rx_free, the READ
3386 * INDEX is not incremented and iwl->status(RX_STALLED) is set. If there
3387 * were enough free buffers and RX_STALLED is set it is cleared.
3388 *
3389 *
3390 * Driver sequence:
3391 *
Ben Cahill9fbab512007-11-29 11:09:47 +08003392 * iwl3945_rx_queue_alloc() Allocates rx_free
3393 * iwl3945_rx_replenish() Replenishes rx_free list from rx_used, and calls
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003394 * iwl3945_rx_queue_restock
Ben Cahill9fbab512007-11-29 11:09:47 +08003395 * iwl3945_rx_queue_restock() Moves available buffers from rx_free into Rx
Zhu Yib481de92007-09-25 17:54:57 -07003396 * queue, updates firmware pointers, and updates
3397 * the WRITE index. If insufficient rx_free buffers
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003398 * are available, schedules iwl3945_rx_replenish
Zhu Yib481de92007-09-25 17:54:57 -07003399 *
3400 * -- enable interrupts --
Ben Cahill9fbab512007-11-29 11:09:47 +08003401 * ISR - iwl3945_rx() Detach iwl3945_rx_mem_buffers from pool up to the
Zhu Yib481de92007-09-25 17:54:57 -07003402 * READ INDEX, detaching the SKB from the pool.
3403 * Moves the packet buffer from queue to rx_used.
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003404 * Calls iwl3945_rx_queue_restock to refill any empty
Zhu Yib481de92007-09-25 17:54:57 -07003405 * slots.
3406 * ...
3407 *
3408 */
3409
3410/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003411 * iwl3945_rx_queue_space - Return number of free slots available in queue.
Zhu Yib481de92007-09-25 17:54:57 -07003412 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003413static int iwl3945_rx_queue_space(const struct iwl3945_rx_queue *q)
Zhu Yib481de92007-09-25 17:54:57 -07003414{
3415 int s = q->read - q->write;
3416 if (s <= 0)
3417 s += RX_QUEUE_SIZE;
3418 /* keep some buffer to not confuse full and empty queue */
3419 s -= 2;
3420 if (s < 0)
3421 s = 0;
3422 return s;
3423}
3424
3425/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003426 * iwl3945_rx_queue_update_write_ptr - Update the write pointer for the RX queue
Zhu Yib481de92007-09-25 17:54:57 -07003427 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003428int iwl3945_rx_queue_update_write_ptr(struct iwl3945_priv *priv, struct iwl3945_rx_queue *q)
Zhu Yib481de92007-09-25 17:54:57 -07003429{
3430 u32 reg = 0;
3431 int rc = 0;
3432 unsigned long flags;
3433
3434 spin_lock_irqsave(&q->lock, flags);
3435
3436 if (q->need_update == 0)
3437 goto exit_unlock;
3438
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003439 /* If power-saving is in use, make sure device is awake */
Zhu Yib481de92007-09-25 17:54:57 -07003440 if (test_bit(STATUS_POWER_PMI, &priv->status)) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003441 reg = iwl3945_read32(priv, CSR_UCODE_DRV_GP1);
Zhu Yib481de92007-09-25 17:54:57 -07003442
3443 if (reg & CSR_UCODE_DRV_GP1_BIT_MAC_SLEEP) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003444 iwl3945_set_bit(priv, CSR_GP_CNTRL,
Zhu Yib481de92007-09-25 17:54:57 -07003445 CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
3446 goto exit_unlock;
3447 }
3448
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003449 rc = iwl3945_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003450 if (rc)
3451 goto exit_unlock;
3452
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003453 /* Device expects a multiple of 8 */
Tomas Winklerbddadf82008-12-19 10:37:01 +08003454 iwl3945_write_direct32(priv, FH39_RSCSR_CHNL0_WPTR,
Zhu Yib481de92007-09-25 17:54:57 -07003455 q->write & ~0x7);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003456 iwl3945_release_nic_access(priv);
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003457
3458 /* Else device is assumed to be awake */
Zhu Yib481de92007-09-25 17:54:57 -07003459 } else
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003460 /* Device expects a multiple of 8 */
Tomas Winklerbddadf82008-12-19 10:37:01 +08003461 iwl3945_write32(priv, FH39_RSCSR_CHNL0_WPTR, q->write & ~0x7);
Zhu Yib481de92007-09-25 17:54:57 -07003462
3463
3464 q->need_update = 0;
3465
3466 exit_unlock:
3467 spin_unlock_irqrestore(&q->lock, flags);
3468 return rc;
3469}
3470
3471/**
Ben Cahill9fbab512007-11-29 11:09:47 +08003472 * iwl3945_dma_addr2rbd_ptr - convert a DMA address to a uCode read buffer ptr
Zhu Yib481de92007-09-25 17:54:57 -07003473 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003474static inline __le32 iwl3945_dma_addr2rbd_ptr(struct iwl3945_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07003475 dma_addr_t dma_addr)
3476{
3477 return cpu_to_le32((u32)dma_addr);
3478}
3479
3480/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003481 * iwl3945_rx_queue_restock - refill RX queue from pre-allocated pool
Zhu Yib481de92007-09-25 17:54:57 -07003482 *
Ben Cahill9fbab512007-11-29 11:09:47 +08003483 * If there are slots in the RX queue that need to be restocked,
Zhu Yib481de92007-09-25 17:54:57 -07003484 * and we have free pre-allocated buffers, fill the ranks as much
Ben Cahill9fbab512007-11-29 11:09:47 +08003485 * as we can, pulling from rx_free.
Zhu Yib481de92007-09-25 17:54:57 -07003486 *
3487 * This moves the 'write' index forward to catch up with 'processed', and
3488 * also updates the memory address in the firmware to reference the new
3489 * target buffer.
3490 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003491static int iwl3945_rx_queue_restock(struct iwl3945_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003492{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003493 struct iwl3945_rx_queue *rxq = &priv->rxq;
Zhu Yib481de92007-09-25 17:54:57 -07003494 struct list_head *element;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003495 struct iwl3945_rx_mem_buffer *rxb;
Zhu Yib481de92007-09-25 17:54:57 -07003496 unsigned long flags;
3497 int write, rc;
3498
3499 spin_lock_irqsave(&rxq->lock, flags);
3500 write = rxq->write & ~0x7;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003501 while ((iwl3945_rx_queue_space(rxq) > 0) && (rxq->free_count)) {
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003502 /* Get next free Rx buffer, remove from free list */
Zhu Yib481de92007-09-25 17:54:57 -07003503 element = rxq->rx_free.next;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003504 rxb = list_entry(element, struct iwl3945_rx_mem_buffer, list);
Zhu Yib481de92007-09-25 17:54:57 -07003505 list_del(element);
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003506
3507 /* Point to Rx buffer via next RBD in circular buffer */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003508 rxq->bd[rxq->write] = iwl3945_dma_addr2rbd_ptr(priv, rxb->dma_addr);
Zhu Yib481de92007-09-25 17:54:57 -07003509 rxq->queue[rxq->write] = rxb;
3510 rxq->write = (rxq->write + 1) & RX_QUEUE_MASK;
3511 rxq->free_count--;
3512 }
3513 spin_unlock_irqrestore(&rxq->lock, flags);
3514 /* If the pre-allocated buffer pool is dropping low, schedule to
3515 * refill it */
3516 if (rxq->free_count <= RX_LOW_WATERMARK)
3517 queue_work(priv->workqueue, &priv->rx_replenish);
3518
3519
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003520 /* If we've added more space for the firmware to place data, tell it.
3521 * Increment device's write pointer in multiples of 8. */
Zhu Yib481de92007-09-25 17:54:57 -07003522 if ((write != (rxq->write & ~0x7))
3523 || (abs(rxq->write - rxq->read) > 7)) {
3524 spin_lock_irqsave(&rxq->lock, flags);
3525 rxq->need_update = 1;
3526 spin_unlock_irqrestore(&rxq->lock, flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003527 rc = iwl3945_rx_queue_update_write_ptr(priv, rxq);
Zhu Yib481de92007-09-25 17:54:57 -07003528 if (rc)
3529 return rc;
3530 }
3531
3532 return 0;
3533}
3534
3535/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003536 * iwl3945_rx_replenish - Move all used packet from rx_used to rx_free
Zhu Yib481de92007-09-25 17:54:57 -07003537 *
3538 * When moving to rx_free an SKB is allocated for the slot.
3539 *
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003540 * Also restock the Rx queue via iwl3945_rx_queue_restock.
Ian Schram01ebd062007-10-25 17:15:22 +08003541 * This is called as a scheduled work item (except for during initialization)
Zhu Yib481de92007-09-25 17:54:57 -07003542 */
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08003543static void iwl3945_rx_allocate(struct iwl3945_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003544{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003545 struct iwl3945_rx_queue *rxq = &priv->rxq;
Zhu Yib481de92007-09-25 17:54:57 -07003546 struct list_head *element;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003547 struct iwl3945_rx_mem_buffer *rxb;
Zhu Yib481de92007-09-25 17:54:57 -07003548 unsigned long flags;
3549 spin_lock_irqsave(&rxq->lock, flags);
3550 while (!list_empty(&rxq->rx_used)) {
3551 element = rxq->rx_used.next;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003552 rxb = list_entry(element, struct iwl3945_rx_mem_buffer, list);
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003553
3554 /* Alloc a new receive buffer */
Zhu Yib481de92007-09-25 17:54:57 -07003555 rxb->skb =
3556 alloc_skb(IWL_RX_BUF_SIZE, __GFP_NOWARN | GFP_ATOMIC);
3557 if (!rxb->skb) {
3558 if (net_ratelimit())
3559 printk(KERN_CRIT DRV_NAME
3560 ": Can not allocate SKB buffers\n");
3561 /* We don't reschedule replenish work here -- we will
3562 * call the restock method and if it still needs
3563 * more buffers it will schedule replenish */
3564 break;
3565 }
Zhu Yi12342c42007-12-20 11:27:32 +08003566
3567 /* If radiotap head is required, reserve some headroom here.
3568 * The physical head count is a variable rx_stats->phy_count.
3569 * We reserve 4 bytes here. Plus these extra bytes, the
3570 * headroom of the physical head should be enough for the
3571 * radiotap head that iwl3945 supported. See iwl3945_rt.
3572 */
3573 skb_reserve(rxb->skb, 4);
3574
Zhu Yib481de92007-09-25 17:54:57 -07003575 priv->alloc_rxb_skb++;
3576 list_del(element);
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003577
3578 /* Get physical address of RB/SKB */
Zhu Yib481de92007-09-25 17:54:57 -07003579 rxb->dma_addr =
3580 pci_map_single(priv->pci_dev, rxb->skb->data,
3581 IWL_RX_BUF_SIZE, PCI_DMA_FROMDEVICE);
3582 list_add_tail(&rxb->list, &rxq->rx_free);
3583 rxq->free_count++;
3584 }
3585 spin_unlock_irqrestore(&rxq->lock, flags);
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08003586}
3587
3588/*
3589 * this should be called while priv->lock is locked
3590 */
Tomas Winkler4fd1f842007-12-05 20:59:58 +02003591static void __iwl3945_rx_replenish(void *data)
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08003592{
3593 struct iwl3945_priv *priv = data;
3594
3595 iwl3945_rx_allocate(priv);
3596 iwl3945_rx_queue_restock(priv);
3597}
3598
3599
3600void iwl3945_rx_replenish(void *data)
3601{
3602 struct iwl3945_priv *priv = data;
3603 unsigned long flags;
3604
3605 iwl3945_rx_allocate(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003606
3607 spin_lock_irqsave(&priv->lock, flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003608 iwl3945_rx_queue_restock(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003609 spin_unlock_irqrestore(&priv->lock, flags);
3610}
3611
3612/* Assumes that the skb field of the buffers in 'pool' is kept accurate.
Ben Cahill9fbab512007-11-29 11:09:47 +08003613 * If an SKB has been detached, the POOL needs to have its SKB set to NULL
Zhu Yib481de92007-09-25 17:54:57 -07003614 * This free routine walks the list of POOL entries and if SKB is set to
3615 * non NULL it is unmapped and freed
3616 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003617static void iwl3945_rx_queue_free(struct iwl3945_priv *priv, struct iwl3945_rx_queue *rxq)
Zhu Yib481de92007-09-25 17:54:57 -07003618{
3619 int i;
3620 for (i = 0; i < RX_QUEUE_SIZE + RX_FREE_BUFFERS; i++) {
3621 if (rxq->pool[i].skb != NULL) {
3622 pci_unmap_single(priv->pci_dev,
3623 rxq->pool[i].dma_addr,
3624 IWL_RX_BUF_SIZE, PCI_DMA_FROMDEVICE);
3625 dev_kfree_skb(rxq->pool[i].skb);
3626 }
3627 }
3628
3629 pci_free_consistent(priv->pci_dev, 4 * RX_QUEUE_SIZE, rxq->bd,
3630 rxq->dma_addr);
3631 rxq->bd = NULL;
3632}
3633
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003634int iwl3945_rx_queue_alloc(struct iwl3945_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003635{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003636 struct iwl3945_rx_queue *rxq = &priv->rxq;
Zhu Yib481de92007-09-25 17:54:57 -07003637 struct pci_dev *dev = priv->pci_dev;
3638 int i;
3639
3640 spin_lock_init(&rxq->lock);
3641 INIT_LIST_HEAD(&rxq->rx_free);
3642 INIT_LIST_HEAD(&rxq->rx_used);
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003643
3644 /* Alloc the circular buffer of Read Buffer Descriptors (RBDs) */
Zhu Yib481de92007-09-25 17:54:57 -07003645 rxq->bd = pci_alloc_consistent(dev, 4 * RX_QUEUE_SIZE, &rxq->dma_addr);
3646 if (!rxq->bd)
3647 return -ENOMEM;
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003648
Zhu Yib481de92007-09-25 17:54:57 -07003649 /* Fill the rx_used queue with _all_ of the Rx buffers */
3650 for (i = 0; i < RX_FREE_BUFFERS + RX_QUEUE_SIZE; i++)
3651 list_add_tail(&rxq->pool[i].list, &rxq->rx_used);
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003652
Zhu Yib481de92007-09-25 17:54:57 -07003653 /* Set us so that we have processed and used all buffers, but have
3654 * not restocked the Rx queue with fresh buffers */
3655 rxq->read = rxq->write = 0;
3656 rxq->free_count = 0;
3657 rxq->need_update = 0;
3658 return 0;
3659}
3660
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003661void iwl3945_rx_queue_reset(struct iwl3945_priv *priv, struct iwl3945_rx_queue *rxq)
Zhu Yib481de92007-09-25 17:54:57 -07003662{
3663 unsigned long flags;
3664 int i;
3665 spin_lock_irqsave(&rxq->lock, flags);
3666 INIT_LIST_HEAD(&rxq->rx_free);
3667 INIT_LIST_HEAD(&rxq->rx_used);
3668 /* Fill the rx_used queue with _all_ of the Rx buffers */
3669 for (i = 0; i < RX_FREE_BUFFERS + RX_QUEUE_SIZE; i++) {
3670 /* In the reset function, these buffers may have been allocated
3671 * to an SKB, so we need to unmap and free potential storage */
3672 if (rxq->pool[i].skb != NULL) {
3673 pci_unmap_single(priv->pci_dev,
3674 rxq->pool[i].dma_addr,
3675 IWL_RX_BUF_SIZE, PCI_DMA_FROMDEVICE);
3676 priv->alloc_rxb_skb--;
3677 dev_kfree_skb(rxq->pool[i].skb);
3678 rxq->pool[i].skb = NULL;
3679 }
3680 list_add_tail(&rxq->pool[i].list, &rxq->rx_used);
3681 }
3682
3683 /* 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 spin_unlock_irqrestore(&rxq->lock, flags);
3688}
3689
3690/* Convert linear signal-to-noise ratio into dB */
3691static u8 ratio2dB[100] = {
3692/* 0 1 2 3 4 5 6 7 8 9 */
3693 0, 0, 6, 10, 12, 14, 16, 17, 18, 19, /* 00 - 09 */
3694 20, 21, 22, 22, 23, 23, 24, 25, 26, 26, /* 10 - 19 */
3695 26, 26, 26, 27, 27, 28, 28, 28, 29, 29, /* 20 - 29 */
3696 29, 30, 30, 30, 31, 31, 31, 31, 32, 32, /* 30 - 39 */
3697 32, 32, 32, 33, 33, 33, 33, 33, 34, 34, /* 40 - 49 */
3698 34, 34, 34, 34, 35, 35, 35, 35, 35, 35, /* 50 - 59 */
3699 36, 36, 36, 36, 36, 36, 36, 37, 37, 37, /* 60 - 69 */
3700 37, 37, 37, 37, 37, 38, 38, 38, 38, 38, /* 70 - 79 */
3701 38, 38, 38, 38, 38, 39, 39, 39, 39, 39, /* 80 - 89 */
3702 39, 39, 39, 39, 39, 40, 40, 40, 40, 40 /* 90 - 99 */
3703};
3704
3705/* Calculates a relative dB value from a ratio of linear
3706 * (i.e. not dB) signal levels.
3707 * Conversion assumes that levels are voltages (20*log), not powers (10*log). */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003708int iwl3945_calc_db_from_ratio(int sig_ratio)
Zhu Yib481de92007-09-25 17:54:57 -07003709{
Adrian Bunk221c80c2008-02-02 23:19:01 +02003710 /* 1000:1 or higher just report as 60 dB */
3711 if (sig_ratio >= 1000)
Zhu Yib481de92007-09-25 17:54:57 -07003712 return 60;
3713
Adrian Bunk221c80c2008-02-02 23:19:01 +02003714 /* 100:1 or higher, divide by 10 and use table,
Zhu Yib481de92007-09-25 17:54:57 -07003715 * add 20 dB to make up for divide by 10 */
Adrian Bunk221c80c2008-02-02 23:19:01 +02003716 if (sig_ratio >= 100)
Tomas Winkler3ac7f142008-07-21 02:40:14 +03003717 return 20 + (int)ratio2dB[sig_ratio/10];
Zhu Yib481de92007-09-25 17:54:57 -07003718
3719 /* We shouldn't see this */
3720 if (sig_ratio < 1)
3721 return 0;
3722
3723 /* Use table for ratios 1:1 - 99:1 */
3724 return (int)ratio2dB[sig_ratio];
3725}
3726
3727#define PERFECT_RSSI (-20) /* dBm */
3728#define WORST_RSSI (-95) /* dBm */
3729#define RSSI_RANGE (PERFECT_RSSI - WORST_RSSI)
3730
3731/* Calculate an indication of rx signal quality (a percentage, not dBm!).
3732 * See http://www.ces.clemson.edu/linux/signal_quality.shtml for info
3733 * about formulas used below. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003734int iwl3945_calc_sig_qual(int rssi_dbm, int noise_dbm)
Zhu Yib481de92007-09-25 17:54:57 -07003735{
3736 int sig_qual;
3737 int degradation = PERFECT_RSSI - rssi_dbm;
3738
3739 /* If we get a noise measurement, use signal-to-noise ratio (SNR)
3740 * as indicator; formula is (signal dbm - noise dbm).
3741 * SNR at or above 40 is a great signal (100%).
3742 * Below that, scale to fit SNR of 0 - 40 dB within 0 - 100% indicator.
3743 * Weakest usable signal is usually 10 - 15 dB SNR. */
3744 if (noise_dbm) {
3745 if (rssi_dbm - noise_dbm >= 40)
3746 return 100;
3747 else if (rssi_dbm < noise_dbm)
3748 return 0;
3749 sig_qual = ((rssi_dbm - noise_dbm) * 5) / 2;
3750
3751 /* Else use just the signal level.
3752 * This formula is a least squares fit of data points collected and
3753 * compared with a reference system that had a percentage (%) display
3754 * for signal quality. */
3755 } else
3756 sig_qual = (100 * (RSSI_RANGE * RSSI_RANGE) - degradation *
3757 (15 * RSSI_RANGE + 62 * degradation)) /
3758 (RSSI_RANGE * RSSI_RANGE);
3759
3760 if (sig_qual > 100)
3761 sig_qual = 100;
3762 else if (sig_qual < 1)
3763 sig_qual = 0;
3764
3765 return sig_qual;
3766}
3767
3768/**
Ben Cahill9fbab512007-11-29 11:09:47 +08003769 * iwl3945_rx_handle - Main entry function for receiving responses from uCode
Zhu Yib481de92007-09-25 17:54:57 -07003770 *
3771 * Uses the priv->rx_handlers callback function array to invoke
3772 * the appropriate handlers, including command responses,
3773 * frame-received notifications, and other notifications.
3774 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003775static void iwl3945_rx_handle(struct iwl3945_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003776{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003777 struct iwl3945_rx_mem_buffer *rxb;
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08003778 struct iwl_rx_packet *pkt;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003779 struct iwl3945_rx_queue *rxq = &priv->rxq;
Zhu Yib481de92007-09-25 17:54:57 -07003780 u32 r, i;
3781 int reclaim;
3782 unsigned long flags;
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08003783 u8 fill_rx = 0;
Mohamed Abbasd68ab682008-02-07 13:16:33 -08003784 u32 count = 8;
Zhu Yib481de92007-09-25 17:54:57 -07003785
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003786 /* uCode's read index (stored in shared DRAM) indicates the last Rx
3787 * buffer that the driver may process (last buffer filled by ucode). */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003788 r = iwl3945_hw_get_rx_read(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003789 i = rxq->read;
3790
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08003791 if (iwl3945_rx_queue_space(rxq) > (RX_QUEUE_SIZE / 2))
3792 fill_rx = 1;
Zhu Yib481de92007-09-25 17:54:57 -07003793 /* Rx interrupt, but nothing sent from uCode */
3794 if (i == r)
3795 IWL_DEBUG(IWL_DL_RX | IWL_DL_ISR, "r = %d, i = %d\n", r, i);
3796
3797 while (i != r) {
3798 rxb = rxq->queue[i];
3799
Ben Cahill9fbab512007-11-29 11:09:47 +08003800 /* If an RXB doesn't have a Rx queue slot associated with it,
Zhu Yib481de92007-09-25 17:54:57 -07003801 * then a bug has been introduced in the queue refilling
3802 * routines -- catch it here */
3803 BUG_ON(rxb == NULL);
3804
3805 rxq->queue[i] = NULL;
3806
3807 pci_dma_sync_single_for_cpu(priv->pci_dev, rxb->dma_addr,
3808 IWL_RX_BUF_SIZE,
3809 PCI_DMA_FROMDEVICE);
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08003810 pkt = (struct iwl_rx_packet *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07003811
3812 /* Reclaim a command buffer only if this packet is a response
3813 * to a (driver-originated) command.
3814 * If the packet (e.g. Rx frame) originated from uCode,
3815 * there is no command buffer to reclaim.
3816 * Ucode should set SEQ_RX_FRAME bit if ucode-originated,
3817 * but apparently a few don't get set; catch them here. */
3818 reclaim = !(pkt->hdr.sequence & SEQ_RX_FRAME) &&
3819 (pkt->hdr.cmd != STATISTICS_NOTIFICATION) &&
3820 (pkt->hdr.cmd != REPLY_TX);
3821
3822 /* Based on type of command response or notification,
3823 * handle those that need handling via function in
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003824 * rx_handlers table. See iwl3945_setup_rx_handlers() */
Zhu Yib481de92007-09-25 17:54:57 -07003825 if (priv->rx_handlers[pkt->hdr.cmd]) {
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08003826 IWL_DEBUG(IWL_DL_HCMD | IWL_DL_RX | IWL_DL_ISR,
Zhu Yib481de92007-09-25 17:54:57 -07003827 "r = %d, i = %d, %s, 0x%02x\n", r, i,
3828 get_cmd_string(pkt->hdr.cmd), pkt->hdr.cmd);
3829 priv->rx_handlers[pkt->hdr.cmd] (priv, rxb);
3830 } else {
3831 /* No handling needed */
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08003832 IWL_DEBUG(IWL_DL_HCMD | IWL_DL_RX | IWL_DL_ISR,
Zhu Yib481de92007-09-25 17:54:57 -07003833 "r %d i %d No handler needed for %s, 0x%02x\n",
3834 r, i, get_cmd_string(pkt->hdr.cmd),
3835 pkt->hdr.cmd);
3836 }
3837
3838 if (reclaim) {
Ben Cahill9fbab512007-11-29 11:09:47 +08003839 /* Invoke any callbacks, transfer the skb to caller, and
3840 * fire off the (possibly) blocking iwl3945_send_cmd()
Zhu Yib481de92007-09-25 17:54:57 -07003841 * as we reclaim the driver command queue */
3842 if (rxb && rxb->skb)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003843 iwl3945_tx_cmd_complete(priv, rxb);
Zhu Yib481de92007-09-25 17:54:57 -07003844 else
3845 IWL_WARNING("Claim null rxb?\n");
3846 }
3847
3848 /* For now we just don't re-use anything. We can tweak this
3849 * later to try and re-use notification packets and SKBs that
3850 * fail to Rx correctly */
3851 if (rxb->skb != NULL) {
3852 priv->alloc_rxb_skb--;
3853 dev_kfree_skb_any(rxb->skb);
3854 rxb->skb = NULL;
3855 }
3856
3857 pci_unmap_single(priv->pci_dev, rxb->dma_addr,
3858 IWL_RX_BUF_SIZE, PCI_DMA_FROMDEVICE);
3859 spin_lock_irqsave(&rxq->lock, flags);
3860 list_add_tail(&rxb->list, &priv->rxq.rx_used);
3861 spin_unlock_irqrestore(&rxq->lock, flags);
3862 i = (i + 1) & RX_QUEUE_MASK;
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08003863 /* If there are a lot of unused frames,
3864 * restock the Rx queue so ucode won't assert. */
3865 if (fill_rx) {
3866 count++;
3867 if (count >= 8) {
3868 priv->rxq.read = i;
3869 __iwl3945_rx_replenish(priv);
3870 count = 0;
3871 }
3872 }
Zhu Yib481de92007-09-25 17:54:57 -07003873 }
3874
3875 /* Backtrack one entry */
3876 priv->rxq.read = i;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003877 iwl3945_rx_queue_restock(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003878}
3879
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003880/**
3881 * iwl3945_tx_queue_update_write_ptr - Send new write index to hardware
3882 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003883static int iwl3945_tx_queue_update_write_ptr(struct iwl3945_priv *priv,
3884 struct iwl3945_tx_queue *txq)
Zhu Yib481de92007-09-25 17:54:57 -07003885{
3886 u32 reg = 0;
3887 int rc = 0;
3888 int txq_id = txq->q.id;
3889
3890 if (txq->need_update == 0)
3891 return rc;
3892
3893 /* if we're trying to save power */
3894 if (test_bit(STATUS_POWER_PMI, &priv->status)) {
3895 /* wake up nic if it's powered down ...
3896 * uCode will wake up, and interrupt us again, so next
3897 * time we'll skip this part. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003898 reg = iwl3945_read32(priv, CSR_UCODE_DRV_GP1);
Zhu Yib481de92007-09-25 17:54:57 -07003899
3900 if (reg & CSR_UCODE_DRV_GP1_BIT_MAC_SLEEP) {
3901 IWL_DEBUG_INFO("Requesting wakeup, GP1 = 0x%x\n", reg);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003902 iwl3945_set_bit(priv, CSR_GP_CNTRL,
Zhu Yib481de92007-09-25 17:54:57 -07003903 CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
3904 return rc;
3905 }
3906
3907 /* restore this queue's parameters in nic hardware. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003908 rc = iwl3945_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003909 if (rc)
3910 return rc;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003911 iwl3945_write_direct32(priv, HBUS_TARG_WRPTR,
Tomas Winklerfc4b6852007-10-25 17:15:24 +08003912 txq->q.write_ptr | (txq_id << 8));
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003913 iwl3945_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003914
3915 /* else not in power-save mode, uCode will never sleep when we're
3916 * trying to tx (during RFKILL, we're not trying to tx). */
3917 } else
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003918 iwl3945_write32(priv, HBUS_TARG_WRPTR,
Tomas Winklerfc4b6852007-10-25 17:15:24 +08003919 txq->q.write_ptr | (txq_id << 8));
Zhu Yib481de92007-09-25 17:54:57 -07003920
3921 txq->need_update = 0;
3922
3923 return rc;
3924}
3925
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08003926#ifdef CONFIG_IWL3945_DEBUG
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08003927static void iwl3945_print_rx_config_cmd(struct iwl3945_priv *priv,
3928 struct iwl3945_rxon_cmd *rxon)
Zhu Yib481de92007-09-25 17:54:57 -07003929{
3930 IWL_DEBUG_RADIO("RX CONFIG:\n");
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08003931 iwl_print_hex_dump(priv, IWL_DL_RADIO, (u8 *) rxon, sizeof(*rxon));
Zhu Yib481de92007-09-25 17:54:57 -07003932 IWL_DEBUG_RADIO("u16 channel: 0x%x\n", le16_to_cpu(rxon->channel));
3933 IWL_DEBUG_RADIO("u32 flags: 0x%08X\n", le32_to_cpu(rxon->flags));
3934 IWL_DEBUG_RADIO("u32 filter_flags: 0x%08x\n",
3935 le32_to_cpu(rxon->filter_flags));
3936 IWL_DEBUG_RADIO("u8 dev_type: 0x%x\n", rxon->dev_type);
3937 IWL_DEBUG_RADIO("u8 ofdm_basic_rates: 0x%02x\n",
3938 rxon->ofdm_basic_rates);
3939 IWL_DEBUG_RADIO("u8 cck_basic_rates: 0x%02x\n", rxon->cck_basic_rates);
Johannes Berge1749612008-10-27 15:59:26 -07003940 IWL_DEBUG_RADIO("u8[6] node_addr: %pM\n", rxon->node_addr);
3941 IWL_DEBUG_RADIO("u8[6] bssid_addr: %pM\n", rxon->bssid_addr);
Zhu Yib481de92007-09-25 17:54:57 -07003942 IWL_DEBUG_RADIO("u16 assoc_id: 0x%x\n", le16_to_cpu(rxon->assoc_id));
3943}
3944#endif
3945
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003946static void iwl3945_enable_interrupts(struct iwl3945_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003947{
3948 IWL_DEBUG_ISR("Enabling interrupts\n");
3949 set_bit(STATUS_INT_ENABLED, &priv->status);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003950 iwl3945_write32(priv, CSR_INT_MASK, CSR_INI_SET_MASK);
Zhu Yib481de92007-09-25 17:54:57 -07003951}
3952
Mohamed Abbas0359fac2008-03-28 16:21:08 -07003953
3954/* call this function to flush any scheduled tasklet */
3955static inline void iwl_synchronize_irq(struct iwl3945_priv *priv)
3956{
Tomas Winklera96a27f2008-10-23 23:48:56 -07003957 /* wait to make sure we flush pending tasklet*/
Mohamed Abbas0359fac2008-03-28 16:21:08 -07003958 synchronize_irq(priv->pci_dev->irq);
3959 tasklet_kill(&priv->irq_tasklet);
3960}
3961
3962
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003963static inline void iwl3945_disable_interrupts(struct iwl3945_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003964{
3965 clear_bit(STATUS_INT_ENABLED, &priv->status);
3966
3967 /* disable interrupts from uCode/NIC to host */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003968 iwl3945_write32(priv, CSR_INT_MASK, 0x00000000);
Zhu Yib481de92007-09-25 17:54:57 -07003969
3970 /* acknowledge/clear/reset any interrupts still pending
3971 * from uCode or flow handler (Rx/Tx DMA) */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003972 iwl3945_write32(priv, CSR_INT, 0xffffffff);
3973 iwl3945_write32(priv, CSR_FH_INT_STATUS, 0xffffffff);
Zhu Yib481de92007-09-25 17:54:57 -07003974 IWL_DEBUG_ISR("Disabled interrupts\n");
3975}
3976
3977static const char *desc_lookup(int i)
3978{
3979 switch (i) {
3980 case 1:
3981 return "FAIL";
3982 case 2:
3983 return "BAD_PARAM";
3984 case 3:
3985 return "BAD_CHECKSUM";
3986 case 4:
3987 return "NMI_INTERRUPT";
3988 case 5:
3989 return "SYSASSERT";
3990 case 6:
3991 return "FATAL_ERROR";
3992 }
3993
3994 return "UNKNOWN";
3995}
3996
3997#define ERROR_START_OFFSET (1 * sizeof(u32))
3998#define ERROR_ELEM_SIZE (7 * sizeof(u32))
3999
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004000static void iwl3945_dump_nic_error_log(struct iwl3945_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004001{
4002 u32 i;
4003 u32 desc, time, count, base, data1;
4004 u32 blink1, blink2, ilink1, ilink2;
4005 int rc;
4006
4007 base = le32_to_cpu(priv->card_alive.error_event_table_ptr);
4008
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004009 if (!iwl3945_hw_valid_rtc_data_addr(base)) {
Zhu Yib481de92007-09-25 17:54:57 -07004010 IWL_ERROR("Not valid error log pointer 0x%08X\n", base);
4011 return;
4012 }
4013
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004014 rc = iwl3945_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004015 if (rc) {
4016 IWL_WARNING("Can not read from adapter at this time.\n");
4017 return;
4018 }
4019
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004020 count = iwl3945_read_targ_mem(priv, base);
Zhu Yib481de92007-09-25 17:54:57 -07004021
4022 if (ERROR_START_OFFSET <= count * ERROR_ELEM_SIZE) {
4023 IWL_ERROR("Start IWL Error Log Dump:\n");
Tomas Winkler2acae162008-03-02 01:25:59 +02004024 IWL_ERROR("Status: 0x%08lX, count: %d\n", priv->status, count);
Zhu Yib481de92007-09-25 17:54:57 -07004025 }
4026
4027 IWL_ERROR("Desc Time asrtPC blink2 "
4028 "ilink1 nmiPC Line\n");
4029 for (i = ERROR_START_OFFSET;
4030 i < (count * ERROR_ELEM_SIZE) + ERROR_START_OFFSET;
4031 i += ERROR_ELEM_SIZE) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004032 desc = iwl3945_read_targ_mem(priv, base + i);
Zhu Yib481de92007-09-25 17:54:57 -07004033 time =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004034 iwl3945_read_targ_mem(priv, base + i + 1 * sizeof(u32));
Zhu Yib481de92007-09-25 17:54:57 -07004035 blink1 =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004036 iwl3945_read_targ_mem(priv, base + i + 2 * sizeof(u32));
Zhu Yib481de92007-09-25 17:54:57 -07004037 blink2 =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004038 iwl3945_read_targ_mem(priv, base + i + 3 * sizeof(u32));
Zhu Yib481de92007-09-25 17:54:57 -07004039 ilink1 =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004040 iwl3945_read_targ_mem(priv, base + i + 4 * sizeof(u32));
Zhu Yib481de92007-09-25 17:54:57 -07004041 ilink2 =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004042 iwl3945_read_targ_mem(priv, base + i + 5 * sizeof(u32));
Zhu Yib481de92007-09-25 17:54:57 -07004043 data1 =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004044 iwl3945_read_targ_mem(priv, base + i + 6 * sizeof(u32));
Zhu Yib481de92007-09-25 17:54:57 -07004045
4046 IWL_ERROR
4047 ("%-13s (#%d) %010u 0x%05X 0x%05X 0x%05X 0x%05X %u\n\n",
4048 desc_lookup(desc), desc, time, blink1, blink2,
4049 ilink1, ilink2, data1);
4050 }
4051
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004052 iwl3945_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004053
4054}
4055
Ben Cahillf58177b2007-11-29 11:09:43 +08004056#define EVENT_START_OFFSET (6 * sizeof(u32))
Zhu Yib481de92007-09-25 17:54:57 -07004057
4058/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004059 * iwl3945_print_event_log - Dump error event log to syslog
Zhu Yib481de92007-09-25 17:54:57 -07004060 *
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004061 * NOTE: Must be called with iwl3945_grab_nic_access() already obtained!
Zhu Yib481de92007-09-25 17:54:57 -07004062 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004063static void iwl3945_print_event_log(struct iwl3945_priv *priv, u32 start_idx,
Zhu Yib481de92007-09-25 17:54:57 -07004064 u32 num_events, u32 mode)
4065{
4066 u32 i;
4067 u32 base; /* SRAM byte address of event log header */
4068 u32 event_size; /* 2 u32s, or 3 u32s if timestamp recorded */
4069 u32 ptr; /* SRAM byte address of log data */
4070 u32 ev, time, data; /* event log data */
4071
4072 if (num_events == 0)
4073 return;
4074
4075 base = le32_to_cpu(priv->card_alive.log_event_table_ptr);
4076
4077 if (mode == 0)
4078 event_size = 2 * sizeof(u32);
4079 else
4080 event_size = 3 * sizeof(u32);
4081
4082 ptr = base + EVENT_START_OFFSET + (start_idx * event_size);
4083
4084 /* "time" is actually "data" for mode 0 (no timestamp).
4085 * place event id # at far right for easier visual parsing. */
4086 for (i = 0; i < num_events; i++) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004087 ev = iwl3945_read_targ_mem(priv, ptr);
Zhu Yib481de92007-09-25 17:54:57 -07004088 ptr += sizeof(u32);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004089 time = iwl3945_read_targ_mem(priv, ptr);
Zhu Yib481de92007-09-25 17:54:57 -07004090 ptr += sizeof(u32);
4091 if (mode == 0)
4092 IWL_ERROR("0x%08x\t%04u\n", time, ev); /* data, ev */
4093 else {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004094 data = iwl3945_read_targ_mem(priv, ptr);
Zhu Yib481de92007-09-25 17:54:57 -07004095 ptr += sizeof(u32);
4096 IWL_ERROR("%010u\t0x%08x\t%04u\n", time, data, ev);
4097 }
4098 }
4099}
4100
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004101static void iwl3945_dump_nic_event_log(struct iwl3945_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004102{
4103 int rc;
4104 u32 base; /* SRAM byte address of event log header */
4105 u32 capacity; /* event log capacity in # entries */
4106 u32 mode; /* 0 - no timestamp, 1 - timestamp recorded */
4107 u32 num_wraps; /* # times uCode wrapped to top of log */
4108 u32 next_entry; /* index of next entry to be written by uCode */
4109 u32 size; /* # entries that we'll print */
4110
4111 base = le32_to_cpu(priv->card_alive.log_event_table_ptr);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004112 if (!iwl3945_hw_valid_rtc_data_addr(base)) {
Zhu Yib481de92007-09-25 17:54:57 -07004113 IWL_ERROR("Invalid event log pointer 0x%08X\n", base);
4114 return;
4115 }
4116
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004117 rc = iwl3945_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004118 if (rc) {
4119 IWL_WARNING("Can not read from adapter at this time.\n");
4120 return;
4121 }
4122
4123 /* event log header */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004124 capacity = iwl3945_read_targ_mem(priv, base);
4125 mode = iwl3945_read_targ_mem(priv, base + (1 * sizeof(u32)));
4126 num_wraps = iwl3945_read_targ_mem(priv, base + (2 * sizeof(u32)));
4127 next_entry = iwl3945_read_targ_mem(priv, base + (3 * sizeof(u32)));
Zhu Yib481de92007-09-25 17:54:57 -07004128
4129 size = num_wraps ? capacity : next_entry;
4130
4131 /* bail out if nothing in log */
4132 if (size == 0) {
Zhu Yi583fab32007-09-27 11:27:30 +08004133 IWL_ERROR("Start IWL Event Log Dump: nothing in log\n");
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004134 iwl3945_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004135 return;
4136 }
4137
Zhu Yi583fab32007-09-27 11:27:30 +08004138 IWL_ERROR("Start IWL Event Log Dump: display count %d, wraps %d\n",
Zhu Yib481de92007-09-25 17:54:57 -07004139 size, num_wraps);
4140
4141 /* if uCode has wrapped back to top of log, start at the oldest entry,
4142 * i.e the next one that uCode would fill. */
4143 if (num_wraps)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004144 iwl3945_print_event_log(priv, next_entry,
Zhu Yib481de92007-09-25 17:54:57 -07004145 capacity - next_entry, mode);
4146
4147 /* (then/else) start at top of log */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004148 iwl3945_print_event_log(priv, 0, next_entry, mode);
Zhu Yib481de92007-09-25 17:54:57 -07004149
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004150 iwl3945_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004151}
4152
4153/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004154 * iwl3945_irq_handle_error - called for HW or SW error interrupt from card
Zhu Yib481de92007-09-25 17:54:57 -07004155 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004156static void iwl3945_irq_handle_error(struct iwl3945_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004157{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004158 /* Set the FW error flag -- cleared on iwl3945_down */
Zhu Yib481de92007-09-25 17:54:57 -07004159 set_bit(STATUS_FW_ERROR, &priv->status);
4160
4161 /* Cancel currently queued command. */
4162 clear_bit(STATUS_HCMD_ACTIVE, &priv->status);
4163
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08004164#ifdef CONFIG_IWL3945_DEBUG
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08004165 if (priv->debug_level & IWL_DL_FW_ERRORS) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004166 iwl3945_dump_nic_error_log(priv);
4167 iwl3945_dump_nic_event_log(priv);
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08004168 iwl3945_print_rx_config_cmd(priv, &priv->staging_rxon);
Zhu Yib481de92007-09-25 17:54:57 -07004169 }
4170#endif
4171
4172 wake_up_interruptible(&priv->wait_command_queue);
4173
4174 /* Keep the restart process from trying to send host
4175 * commands by clearing the INIT status bit */
4176 clear_bit(STATUS_READY, &priv->status);
4177
4178 if (!test_bit(STATUS_EXIT_PENDING, &priv->status)) {
4179 IWL_DEBUG(IWL_DL_INFO | IWL_DL_FW_ERRORS,
4180 "Restarting adapter due to uCode error.\n");
4181
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004182 if (iwl3945_is_associated(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07004183 memcpy(&priv->recovery_rxon, &priv->active_rxon,
4184 sizeof(priv->recovery_rxon));
4185 priv->error_recovering = 1;
4186 }
4187 queue_work(priv->workqueue, &priv->restart);
4188 }
4189}
4190
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004191static void iwl3945_error_recovery(struct iwl3945_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004192{
4193 unsigned long flags;
4194
4195 memcpy(&priv->staging_rxon, &priv->recovery_rxon,
4196 sizeof(priv->staging_rxon));
4197 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004198 iwl3945_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004199
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004200 iwl3945_add_station(priv, priv->bssid, 1, 0);
Zhu Yib481de92007-09-25 17:54:57 -07004201
4202 spin_lock_irqsave(&priv->lock, flags);
4203 priv->assoc_id = le16_to_cpu(priv->staging_rxon.assoc_id);
4204 priv->error_recovering = 0;
4205 spin_unlock_irqrestore(&priv->lock, flags);
4206}
4207
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004208static void iwl3945_irq_tasklet(struct iwl3945_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004209{
4210 u32 inta, handled = 0;
4211 u32 inta_fh;
4212 unsigned long flags;
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08004213#ifdef CONFIG_IWL3945_DEBUG
Zhu Yib481de92007-09-25 17:54:57 -07004214 u32 inta_mask;
4215#endif
4216
4217 spin_lock_irqsave(&priv->lock, flags);
4218
4219 /* Ack/clear/reset pending uCode interrupts.
4220 * Note: Some bits in CSR_INT are "OR" of bits in CSR_FH_INT_STATUS,
4221 * and will clear only when CSR_FH_INT_STATUS gets cleared. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004222 inta = iwl3945_read32(priv, CSR_INT);
4223 iwl3945_write32(priv, CSR_INT, inta);
Zhu Yib481de92007-09-25 17:54:57 -07004224
4225 /* Ack/clear/reset pending flow-handler (DMA) interrupts.
4226 * Any new interrupts that happen after this, either while we're
4227 * in this tasklet, or later, will show up in next ISR/tasklet. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004228 inta_fh = iwl3945_read32(priv, CSR_FH_INT_STATUS);
4229 iwl3945_write32(priv, CSR_FH_INT_STATUS, inta_fh);
Zhu Yib481de92007-09-25 17:54:57 -07004230
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08004231#ifdef CONFIG_IWL3945_DEBUG
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08004232 if (priv->debug_level & IWL_DL_ISR) {
Ben Cahill9fbab512007-11-29 11:09:47 +08004233 /* just for debug */
4234 inta_mask = iwl3945_read32(priv, CSR_INT_MASK);
Zhu Yib481de92007-09-25 17:54:57 -07004235 IWL_DEBUG_ISR("inta 0x%08x, enabled 0x%08x, fh 0x%08x\n",
4236 inta, inta_mask, inta_fh);
4237 }
4238#endif
4239
4240 /* Since CSR_INT and CSR_FH_INT_STATUS reads and clears are not
4241 * atomic, make sure that inta covers all the interrupts that
4242 * we've discovered, even if FH interrupt came in just after
4243 * reading CSR_INT. */
Tomas Winkler6f83eaa2008-03-04 18:09:28 -08004244 if (inta_fh & CSR39_FH_INT_RX_MASK)
Zhu Yib481de92007-09-25 17:54:57 -07004245 inta |= CSR_INT_BIT_FH_RX;
Tomas Winkler6f83eaa2008-03-04 18:09:28 -08004246 if (inta_fh & CSR39_FH_INT_TX_MASK)
Zhu Yib481de92007-09-25 17:54:57 -07004247 inta |= CSR_INT_BIT_FH_TX;
4248
4249 /* Now service all interrupt bits discovered above. */
4250 if (inta & CSR_INT_BIT_HW_ERR) {
4251 IWL_ERROR("Microcode HW error detected. Restarting.\n");
4252
4253 /* Tell the device to stop sending interrupts */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004254 iwl3945_disable_interrupts(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004255
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004256 iwl3945_irq_handle_error(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004257
4258 handled |= CSR_INT_BIT_HW_ERR;
4259
4260 spin_unlock_irqrestore(&priv->lock, flags);
4261
4262 return;
4263 }
4264
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08004265#ifdef CONFIG_IWL3945_DEBUG
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08004266 if (priv->debug_level & (IWL_DL_ISR)) {
Zhu Yib481de92007-09-25 17:54:57 -07004267 /* NIC fires this, but we don't use it, redundant with WAKEUP */
Joonwoo Park25c03d82008-01-23 10:15:20 -08004268 if (inta & CSR_INT_BIT_SCD)
4269 IWL_DEBUG_ISR("Scheduler finished to transmit "
4270 "the frame/frames.\n");
Zhu Yib481de92007-09-25 17:54:57 -07004271
4272 /* Alive notification via Rx interrupt will do the real work */
4273 if (inta & CSR_INT_BIT_ALIVE)
4274 IWL_DEBUG_ISR("Alive interrupt\n");
4275 }
4276#endif
4277 /* Safely ignore these bits for debug checks below */
Joonwoo Park25c03d82008-01-23 10:15:20 -08004278 inta &= ~(CSR_INT_BIT_SCD | CSR_INT_BIT_ALIVE);
Zhu Yib481de92007-09-25 17:54:57 -07004279
Zhu Yib481de92007-09-25 17:54:57 -07004280 /* Error detected by uCode */
4281 if (inta & CSR_INT_BIT_SW_ERR) {
4282 IWL_ERROR("Microcode SW error detected. Restarting 0x%X.\n",
4283 inta);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004284 iwl3945_irq_handle_error(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004285 handled |= CSR_INT_BIT_SW_ERR;
4286 }
4287
4288 /* uCode wakes up after power-down sleep */
4289 if (inta & CSR_INT_BIT_WAKEUP) {
4290 IWL_DEBUG_ISR("Wakeup interrupt\n");
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004291 iwl3945_rx_queue_update_write_ptr(priv, &priv->rxq);
4292 iwl3945_tx_queue_update_write_ptr(priv, &priv->txq[0]);
4293 iwl3945_tx_queue_update_write_ptr(priv, &priv->txq[1]);
4294 iwl3945_tx_queue_update_write_ptr(priv, &priv->txq[2]);
4295 iwl3945_tx_queue_update_write_ptr(priv, &priv->txq[3]);
4296 iwl3945_tx_queue_update_write_ptr(priv, &priv->txq[4]);
4297 iwl3945_tx_queue_update_write_ptr(priv, &priv->txq[5]);
Zhu Yib481de92007-09-25 17:54:57 -07004298
4299 handled |= CSR_INT_BIT_WAKEUP;
4300 }
4301
4302 /* All uCode command responses, including Tx command responses,
4303 * Rx "responses" (frame-received notification), and other
4304 * notifications from uCode come through here*/
4305 if (inta & (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX)) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004306 iwl3945_rx_handle(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004307 handled |= (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX);
4308 }
4309
4310 if (inta & CSR_INT_BIT_FH_TX) {
4311 IWL_DEBUG_ISR("Tx interrupt\n");
4312
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004313 iwl3945_write32(priv, CSR_FH_INT_STATUS, (1 << 6));
4314 if (!iwl3945_grab_nic_access(priv)) {
Tomas Winklerbddadf82008-12-19 10:37:01 +08004315 iwl3945_write_direct32(priv, FH39_TCSR_CREDIT
4316 (FH39_SRVC_CHNL), 0x0);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004317 iwl3945_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004318 }
4319 handled |= CSR_INT_BIT_FH_TX;
4320 }
4321
4322 if (inta & ~handled)
4323 IWL_ERROR("Unhandled INTA bits 0x%08x\n", inta & ~handled);
4324
4325 if (inta & ~CSR_INI_SET_MASK) {
4326 IWL_WARNING("Disabled INTA bits 0x%08x were pending\n",
4327 inta & ~CSR_INI_SET_MASK);
4328 IWL_WARNING(" with FH_INT = 0x%08x\n", inta_fh);
4329 }
4330
4331 /* Re-enable all interrupts */
Mohamed Abbas0359fac2008-03-28 16:21:08 -07004332 /* only Re-enable if disabled by irq */
4333 if (test_bit(STATUS_INT_ENABLED, &priv->status))
4334 iwl3945_enable_interrupts(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004335
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08004336#ifdef CONFIG_IWL3945_DEBUG
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08004337 if (priv->debug_level & (IWL_DL_ISR)) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004338 inta = iwl3945_read32(priv, CSR_INT);
4339 inta_mask = iwl3945_read32(priv, CSR_INT_MASK);
4340 inta_fh = iwl3945_read32(priv, CSR_FH_INT_STATUS);
Zhu Yib481de92007-09-25 17:54:57 -07004341 IWL_DEBUG_ISR("End inta 0x%08x, enabled 0x%08x, fh 0x%08x, "
4342 "flags 0x%08lx\n", inta, inta_mask, inta_fh, flags);
4343 }
4344#endif
4345 spin_unlock_irqrestore(&priv->lock, flags);
4346}
4347
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004348static irqreturn_t iwl3945_isr(int irq, void *data)
Zhu Yib481de92007-09-25 17:54:57 -07004349{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004350 struct iwl3945_priv *priv = data;
Zhu Yib481de92007-09-25 17:54:57 -07004351 u32 inta, inta_mask;
4352 u32 inta_fh;
4353 if (!priv)
4354 return IRQ_NONE;
4355
4356 spin_lock(&priv->lock);
4357
4358 /* Disable (but don't clear!) interrupts here to avoid
4359 * back-to-back ISRs and sporadic interrupts from our NIC.
4360 * If we have something to service, the tasklet will re-enable ints.
4361 * If we *don't* have something, we'll re-enable before leaving here. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004362 inta_mask = iwl3945_read32(priv, CSR_INT_MASK); /* just for debug */
4363 iwl3945_write32(priv, CSR_INT_MASK, 0x00000000);
Zhu Yib481de92007-09-25 17:54:57 -07004364
4365 /* Discover which interrupts are active/pending */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004366 inta = iwl3945_read32(priv, CSR_INT);
4367 inta_fh = iwl3945_read32(priv, CSR_FH_INT_STATUS);
Zhu Yib481de92007-09-25 17:54:57 -07004368
4369 /* Ignore interrupt if there's nothing in NIC to service.
4370 * This may be due to IRQ shared with another device,
4371 * or due to sporadic interrupts thrown from our NIC. */
4372 if (!inta && !inta_fh) {
4373 IWL_DEBUG_ISR("Ignore interrupt, inta == 0, inta_fh == 0\n");
4374 goto none;
4375 }
4376
4377 if ((inta == 0xFFFFFFFF) || ((inta & 0xFFFFFFF0) == 0xa5a5a5a0)) {
4378 /* Hardware disappeared */
Zhu Yi99df6302008-12-02 12:13:58 -08004379 IWL_WARNING("HARDWARE GONE?? INTA == 0x%08x\n", inta);
Oliver Neukumcb4da1a2007-11-13 21:10:32 -08004380 goto unplugged;
Zhu Yib481de92007-09-25 17:54:57 -07004381 }
4382
4383 IWL_DEBUG_ISR("ISR inta 0x%08x, enabled 0x%08x, fh 0x%08x\n",
4384 inta, inta_mask, inta_fh);
4385
Joonwoo Park25c03d82008-01-23 10:15:20 -08004386 inta &= ~CSR_INT_BIT_SCD;
4387
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004388 /* iwl3945_irq_tasklet() will service interrupts and re-enable them */
Joonwoo Park25c03d82008-01-23 10:15:20 -08004389 if (likely(inta || inta_fh))
4390 tasklet_schedule(&priv->irq_tasklet);
Oliver Neukumcb4da1a2007-11-13 21:10:32 -08004391unplugged:
Zhu Yib481de92007-09-25 17:54:57 -07004392 spin_unlock(&priv->lock);
4393
4394 return IRQ_HANDLED;
4395
4396 none:
4397 /* re-enable interrupts here since we don't have anything to service. */
Mohamed Abbas0359fac2008-03-28 16:21:08 -07004398 /* only Re-enable if disabled by irq */
4399 if (test_bit(STATUS_INT_ENABLED, &priv->status))
4400 iwl3945_enable_interrupts(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004401 spin_unlock(&priv->lock);
4402 return IRQ_NONE;
4403}
4404
4405/************************** EEPROM BANDS ****************************
4406 *
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004407 * The iwl3945_eeprom_band definitions below provide the mapping from the
Zhu Yib481de92007-09-25 17:54:57 -07004408 * EEPROM contents to the specific channel number supported for each
4409 * band.
4410 *
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004411 * For example, iwl3945_priv->eeprom.band_3_channels[4] from the band_3
Zhu Yib481de92007-09-25 17:54:57 -07004412 * definition below maps to physical channel 42 in the 5.2GHz spectrum.
4413 * The specific geography and calibration information for that channel
4414 * is contained in the eeprom map itself.
4415 *
4416 * During init, we copy the eeprom information and channel map
4417 * information into priv->channel_info_24/52 and priv->channel_map_24/52
4418 *
4419 * channel_map_24/52 provides the index in the channel_info array for a
4420 * given channel. We have to have two separate maps as there is channel
4421 * overlap with the 2.4GHz and 5.2GHz spectrum as seen in band_1 and
4422 * band_2
4423 *
4424 * A value of 0xff stored in the channel_map indicates that the channel
4425 * is not supported by the hardware at all.
4426 *
4427 * A value of 0xfe in the channel_map indicates that the channel is not
4428 * valid for Tx with the current hardware. This means that
4429 * while the system can tune and receive on a given channel, it may not
4430 * be able to associate or transmit any frames on that
4431 * channel. There is no corresponding channel information for that
4432 * entry.
4433 *
4434 *********************************************************************/
4435
4436/* 2.4 GHz */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004437static const u8 iwl3945_eeprom_band_1[14] = {
Zhu Yib481de92007-09-25 17:54:57 -07004438 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14
4439};
4440
4441/* 5.2 GHz bands */
Ben Cahill9fbab512007-11-29 11:09:47 +08004442static const u8 iwl3945_eeprom_band_2[] = { /* 4915-5080MHz */
Zhu Yib481de92007-09-25 17:54:57 -07004443 183, 184, 185, 187, 188, 189, 192, 196, 7, 8, 11, 12, 16
4444};
4445
Ben Cahill9fbab512007-11-29 11:09:47 +08004446static const u8 iwl3945_eeprom_band_3[] = { /* 5170-5320MHz */
Zhu Yib481de92007-09-25 17:54:57 -07004447 34, 36, 38, 40, 42, 44, 46, 48, 52, 56, 60, 64
4448};
4449
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004450static const u8 iwl3945_eeprom_band_4[] = { /* 5500-5700MHz */
Zhu Yib481de92007-09-25 17:54:57 -07004451 100, 104, 108, 112, 116, 120, 124, 128, 132, 136, 140
4452};
4453
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004454static const u8 iwl3945_eeprom_band_5[] = { /* 5725-5825MHz */
Zhu Yib481de92007-09-25 17:54:57 -07004455 145, 149, 153, 157, 161, 165
4456};
4457
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004458static void iwl3945_init_band_reference(const struct iwl3945_priv *priv, int band,
Zhu Yib481de92007-09-25 17:54:57 -07004459 int *eeprom_ch_count,
Samuel Ortiz0f741d92008-12-19 10:37:10 +08004460 const struct iwl_eeprom_channel
Zhu Yib481de92007-09-25 17:54:57 -07004461 **eeprom_ch_info,
4462 const u8 **eeprom_ch_index)
4463{
4464 switch (band) {
4465 case 1: /* 2.4GHz band */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004466 *eeprom_ch_count = ARRAY_SIZE(iwl3945_eeprom_band_1);
Zhu Yib481de92007-09-25 17:54:57 -07004467 *eeprom_ch_info = priv->eeprom.band_1_channels;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004468 *eeprom_ch_index = iwl3945_eeprom_band_1;
Zhu Yib481de92007-09-25 17:54:57 -07004469 break;
Ben Cahill9fbab512007-11-29 11:09:47 +08004470 case 2: /* 4.9GHz band */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004471 *eeprom_ch_count = ARRAY_SIZE(iwl3945_eeprom_band_2);
Zhu Yib481de92007-09-25 17:54:57 -07004472 *eeprom_ch_info = priv->eeprom.band_2_channels;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004473 *eeprom_ch_index = iwl3945_eeprom_band_2;
Zhu Yib481de92007-09-25 17:54:57 -07004474 break;
4475 case 3: /* 5.2GHz band */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004476 *eeprom_ch_count = ARRAY_SIZE(iwl3945_eeprom_band_3);
Zhu Yib481de92007-09-25 17:54:57 -07004477 *eeprom_ch_info = priv->eeprom.band_3_channels;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004478 *eeprom_ch_index = iwl3945_eeprom_band_3;
Zhu Yib481de92007-09-25 17:54:57 -07004479 break;
Ben Cahill9fbab512007-11-29 11:09:47 +08004480 case 4: /* 5.5GHz band */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004481 *eeprom_ch_count = ARRAY_SIZE(iwl3945_eeprom_band_4);
Zhu Yib481de92007-09-25 17:54:57 -07004482 *eeprom_ch_info = priv->eeprom.band_4_channels;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004483 *eeprom_ch_index = iwl3945_eeprom_band_4;
Zhu Yib481de92007-09-25 17:54:57 -07004484 break;
Ben Cahill9fbab512007-11-29 11:09:47 +08004485 case 5: /* 5.7GHz band */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004486 *eeprom_ch_count = ARRAY_SIZE(iwl3945_eeprom_band_5);
Zhu Yib481de92007-09-25 17:54:57 -07004487 *eeprom_ch_info = priv->eeprom.band_5_channels;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004488 *eeprom_ch_index = iwl3945_eeprom_band_5;
Zhu Yib481de92007-09-25 17:54:57 -07004489 break;
4490 default:
4491 BUG();
4492 return;
4493 }
4494}
4495
Cahill, Ben M6440adb2007-11-29 11:09:55 +08004496/**
4497 * iwl3945_get_channel_info - Find driver's private channel info
4498 *
4499 * Based on band and channel number.
4500 */
Samuel Ortizd20b3c62008-12-19 10:37:15 +08004501const struct iwl_channel_info *
4502iwl3945_get_channel_info(const struct iwl3945_priv *priv,
4503 enum ieee80211_band band, u16 channel)
Zhu Yib481de92007-09-25 17:54:57 -07004504{
4505 int i;
4506
Johannes Berg8318d782008-01-24 19:38:38 +01004507 switch (band) {
4508 case IEEE80211_BAND_5GHZ:
Zhu Yib481de92007-09-25 17:54:57 -07004509 for (i = 14; i < priv->channel_count; i++) {
4510 if (priv->channel_info[i].channel == channel)
4511 return &priv->channel_info[i];
4512 }
4513 break;
4514
Johannes Berg8318d782008-01-24 19:38:38 +01004515 case IEEE80211_BAND_2GHZ:
Zhu Yib481de92007-09-25 17:54:57 -07004516 if (channel >= 1 && channel <= 14)
4517 return &priv->channel_info[channel - 1];
4518 break;
Johannes Berg8318d782008-01-24 19:38:38 +01004519 case IEEE80211_NUM_BANDS:
4520 WARN_ON(1);
Zhu Yib481de92007-09-25 17:54:57 -07004521 }
4522
4523 return NULL;
4524}
4525
4526#define CHECK_AND_PRINT(x) ((eeprom_ch_info[ch].flags & EEPROM_CHANNEL_##x) \
4527 ? # x " " : "")
4528
Cahill, Ben M6440adb2007-11-29 11:09:55 +08004529/**
4530 * iwl3945_init_channel_map - Set up driver's info for all possible channels
4531 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004532static int iwl3945_init_channel_map(struct iwl3945_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004533{
4534 int eeprom_ch_count = 0;
4535 const u8 *eeprom_ch_index = NULL;
Samuel Ortiz0f741d92008-12-19 10:37:10 +08004536 const struct iwl_eeprom_channel *eeprom_ch_info = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07004537 int band, ch;
Samuel Ortizd20b3c62008-12-19 10:37:15 +08004538 struct iwl_channel_info *ch_info;
Zhu Yib481de92007-09-25 17:54:57 -07004539
4540 if (priv->channel_count) {
4541 IWL_DEBUG_INFO("Channel map already initialized.\n");
4542 return 0;
4543 }
4544
4545 if (priv->eeprom.version < 0x2f) {
4546 IWL_WARNING("Unsupported EEPROM version: 0x%04X\n",
4547 priv->eeprom.version);
4548 return -EINVAL;
4549 }
4550
4551 IWL_DEBUG_INFO("Initializing regulatory info from EEPROM\n");
4552
4553 priv->channel_count =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004554 ARRAY_SIZE(iwl3945_eeprom_band_1) +
4555 ARRAY_SIZE(iwl3945_eeprom_band_2) +
4556 ARRAY_SIZE(iwl3945_eeprom_band_3) +
4557 ARRAY_SIZE(iwl3945_eeprom_band_4) +
4558 ARRAY_SIZE(iwl3945_eeprom_band_5);
Zhu Yib481de92007-09-25 17:54:57 -07004559
4560 IWL_DEBUG_INFO("Parsing data for %d channels.\n", priv->channel_count);
4561
Samuel Ortizd20b3c62008-12-19 10:37:15 +08004562 priv->channel_info = kzalloc(sizeof(struct iwl_channel_info) *
Zhu Yib481de92007-09-25 17:54:57 -07004563 priv->channel_count, GFP_KERNEL);
4564 if (!priv->channel_info) {
4565 IWL_ERROR("Could not allocate channel_info\n");
4566 priv->channel_count = 0;
4567 return -ENOMEM;
4568 }
4569
4570 ch_info = priv->channel_info;
4571
4572 /* Loop through the 5 EEPROM bands adding them in order to the
4573 * channel map we maintain (that contains additional information than
4574 * what just in the EEPROM) */
4575 for (band = 1; band <= 5; band++) {
4576
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004577 iwl3945_init_band_reference(priv, band, &eeprom_ch_count,
Zhu Yib481de92007-09-25 17:54:57 -07004578 &eeprom_ch_info, &eeprom_ch_index);
4579
4580 /* Loop through each band adding each of the channels */
4581 for (ch = 0; ch < eeprom_ch_count; ch++) {
4582 ch_info->channel = eeprom_ch_index[ch];
Johannes Berg8318d782008-01-24 19:38:38 +01004583 ch_info->band = (band == 1) ? IEEE80211_BAND_2GHZ :
4584 IEEE80211_BAND_5GHZ;
Zhu Yib481de92007-09-25 17:54:57 -07004585
4586 /* permanently store EEPROM's channel regulatory flags
4587 * and max power in channel info database. */
4588 ch_info->eeprom = eeprom_ch_info[ch];
4589
4590 /* Copy the run-time flags so they are there even on
4591 * invalid channels */
4592 ch_info->flags = eeprom_ch_info[ch].flags;
4593
4594 if (!(is_channel_valid(ch_info))) {
4595 IWL_DEBUG_INFO("Ch. %d Flags %x [%sGHz] - "
4596 "No traffic\n",
4597 ch_info->channel,
4598 ch_info->flags,
4599 is_channel_a_band(ch_info) ?
4600 "5.2" : "2.4");
4601 ch_info++;
4602 continue;
4603 }
4604
4605 /* Initialize regulatory-based run-time data */
4606 ch_info->max_power_avg = ch_info->curr_txpow =
4607 eeprom_ch_info[ch].max_power_avg;
4608 ch_info->scan_power = eeprom_ch_info[ch].max_power_avg;
4609 ch_info->min_power = 0;
4610
Guy Cohenfe7c4042008-04-21 15:41:56 -07004611 IWL_DEBUG_INFO("Ch. %d [%sGHz] %s%s%s%s%s%s(0x%02x"
Zhu Yib481de92007-09-25 17:54:57 -07004612 " %ddBm): Ad-Hoc %ssupported\n",
4613 ch_info->channel,
4614 is_channel_a_band(ch_info) ?
4615 "5.2" : "2.4",
Tomas Winkler8211ef72008-03-02 01:36:04 +02004616 CHECK_AND_PRINT(VALID),
Zhu Yib481de92007-09-25 17:54:57 -07004617 CHECK_AND_PRINT(IBSS),
4618 CHECK_AND_PRINT(ACTIVE),
4619 CHECK_AND_PRINT(RADAR),
4620 CHECK_AND_PRINT(WIDE),
Zhu Yib481de92007-09-25 17:54:57 -07004621 CHECK_AND_PRINT(DFS),
4622 eeprom_ch_info[ch].flags,
4623 eeprom_ch_info[ch].max_power_avg,
4624 ((eeprom_ch_info[ch].
4625 flags & EEPROM_CHANNEL_IBSS)
4626 && !(eeprom_ch_info[ch].
4627 flags & EEPROM_CHANNEL_RADAR))
4628 ? "" : "not ");
4629
4630 /* Set the user_txpower_limit to the highest power
4631 * supported by any channel */
4632 if (eeprom_ch_info[ch].max_power_avg >
4633 priv->user_txpower_limit)
4634 priv->user_txpower_limit =
4635 eeprom_ch_info[ch].max_power_avg;
4636
4637 ch_info++;
4638 }
4639 }
4640
Cahill, Ben M6440adb2007-11-29 11:09:55 +08004641 /* Set up txpower settings in driver for all channels */
Zhu Yib481de92007-09-25 17:54:57 -07004642 if (iwl3945_txpower_set_from_eeprom(priv))
4643 return -EIO;
4644
4645 return 0;
4646}
4647
Reinette Chatre849e0dc2008-01-23 10:15:18 -08004648/*
4649 * iwl3945_free_channel_map - undo allocations in iwl3945_init_channel_map
4650 */
4651static void iwl3945_free_channel_map(struct iwl3945_priv *priv)
4652{
4653 kfree(priv->channel_info);
4654 priv->channel_count = 0;
4655}
4656
Zhu Yib481de92007-09-25 17:54:57 -07004657/* For active scan, listen ACTIVE_DWELL_TIME (msec) on each channel after
4658 * sending probe req. This should be set long enough to hear probe responses
4659 * from more than one AP. */
Abhijeet Kolekarf9340522008-09-03 11:26:58 +08004660#define IWL_ACTIVE_DWELL_TIME_24 (30) /* all times in msec */
4661#define IWL_ACTIVE_DWELL_TIME_52 (20)
4662
4663#define IWL_ACTIVE_DWELL_FACTOR_24GHZ (3)
4664#define IWL_ACTIVE_DWELL_FACTOR_52GHZ (2)
Zhu Yib481de92007-09-25 17:54:57 -07004665
4666/* For faster active scanning, scan will move to the next channel if fewer than
4667 * PLCP_QUIET_THRESH packets are heard on this channel within
4668 * ACTIVE_QUIET_TIME after sending probe request. This shortens the dwell
4669 * time if it's a quiet channel (nothing responded to our probe, and there's
4670 * no other traffic).
4671 * Disable "quiet" feature by setting PLCP_QUIET_THRESH to 0. */
4672#define IWL_PLCP_QUIET_THRESH __constant_cpu_to_le16(1) /* packets */
Abhijeet Kolekarf9340522008-09-03 11:26:58 +08004673#define IWL_ACTIVE_QUIET_TIME __constant_cpu_to_le16(10) /* msec */
Zhu Yib481de92007-09-25 17:54:57 -07004674
4675/* For passive scan, listen PASSIVE_DWELL_TIME (msec) on each channel.
4676 * Must be set longer than active dwell time.
4677 * For the most reliable scan, set > AP beacon interval (typically 100msec). */
4678#define IWL_PASSIVE_DWELL_TIME_24 (20) /* all times in msec */
4679#define IWL_PASSIVE_DWELL_TIME_52 (10)
4680#define IWL_PASSIVE_DWELL_BASE (100)
4681#define IWL_CHANNEL_TUNE_TIME 5
4682
Kolekar, Abhijeete720ce92008-11-07 09:58:42 -08004683#define IWL_SCAN_PROBE_MASK(n) (BIT(n) | (BIT(n) - BIT(1)))
Abhijeet Kolekarf9340522008-09-03 11:26:58 +08004684
Johannes Berg8318d782008-01-24 19:38:38 +01004685static inline u16 iwl3945_get_active_dwell_time(struct iwl3945_priv *priv,
Abhijeet Kolekarf9340522008-09-03 11:26:58 +08004686 enum ieee80211_band band,
4687 u8 n_probes)
Zhu Yib481de92007-09-25 17:54:57 -07004688{
Johannes Berg8318d782008-01-24 19:38:38 +01004689 if (band == IEEE80211_BAND_5GHZ)
Abhijeet Kolekarf9340522008-09-03 11:26:58 +08004690 return IWL_ACTIVE_DWELL_TIME_52 +
4691 IWL_ACTIVE_DWELL_FACTOR_52GHZ * (n_probes + 1);
Zhu Yib481de92007-09-25 17:54:57 -07004692 else
Abhijeet Kolekarf9340522008-09-03 11:26:58 +08004693 return IWL_ACTIVE_DWELL_TIME_24 +
4694 IWL_ACTIVE_DWELL_FACTOR_24GHZ * (n_probes + 1);
Zhu Yib481de92007-09-25 17:54:57 -07004695}
4696
Johannes Berg8318d782008-01-24 19:38:38 +01004697static u16 iwl3945_get_passive_dwell_time(struct iwl3945_priv *priv,
4698 enum ieee80211_band band)
Zhu Yib481de92007-09-25 17:54:57 -07004699{
Johannes Berg8318d782008-01-24 19:38:38 +01004700 u16 passive = (band == IEEE80211_BAND_2GHZ) ?
Zhu Yib481de92007-09-25 17:54:57 -07004701 IWL_PASSIVE_DWELL_BASE + IWL_PASSIVE_DWELL_TIME_24 :
4702 IWL_PASSIVE_DWELL_BASE + IWL_PASSIVE_DWELL_TIME_52;
4703
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004704 if (iwl3945_is_associated(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07004705 /* If we're associated, we clamp the maximum passive
4706 * dwell time to be 98% of the beacon interval (minus
4707 * 2 * channel tune time) */
4708 passive = priv->beacon_int;
4709 if ((passive > IWL_PASSIVE_DWELL_BASE) || !passive)
4710 passive = IWL_PASSIVE_DWELL_BASE;
4711 passive = (passive * 98) / 100 - IWL_CHANNEL_TUNE_TIME * 2;
4712 }
4713
Zhu Yib481de92007-09-25 17:54:57 -07004714 return passive;
4715}
4716
Johannes Berg8318d782008-01-24 19:38:38 +01004717static int iwl3945_get_channels_for_scan(struct iwl3945_priv *priv,
4718 enum ieee80211_band band,
Abhijeet Kolekarf9340522008-09-03 11:26:58 +08004719 u8 is_active, u8 n_probes,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004720 struct iwl3945_scan_channel *scan_ch)
Zhu Yib481de92007-09-25 17:54:57 -07004721{
4722 const struct ieee80211_channel *channels = NULL;
Johannes Berg8318d782008-01-24 19:38:38 +01004723 const struct ieee80211_supported_band *sband;
Samuel Ortizd20b3c62008-12-19 10:37:15 +08004724 const struct iwl_channel_info *ch_info;
Zhu Yib481de92007-09-25 17:54:57 -07004725 u16 passive_dwell = 0;
4726 u16 active_dwell = 0;
4727 int added, i;
4728
Johannes Berg8318d782008-01-24 19:38:38 +01004729 sband = iwl3945_get_band(priv, band);
4730 if (!sband)
Zhu Yib481de92007-09-25 17:54:57 -07004731 return 0;
4732
Johannes Berg8318d782008-01-24 19:38:38 +01004733 channels = sband->channels;
Zhu Yib481de92007-09-25 17:54:57 -07004734
Abhijeet Kolekarf9340522008-09-03 11:26:58 +08004735 active_dwell = iwl3945_get_active_dwell_time(priv, band, n_probes);
Johannes Berg8318d782008-01-24 19:38:38 +01004736 passive_dwell = iwl3945_get_passive_dwell_time(priv, band);
Zhu Yib481de92007-09-25 17:54:57 -07004737
Abhijeet Kolekar8f4807a2008-09-03 11:26:31 +08004738 if (passive_dwell <= active_dwell)
4739 passive_dwell = active_dwell + 1;
4740
Johannes Berg8318d782008-01-24 19:38:38 +01004741 for (i = 0, added = 0; i < sband->n_channels; i++) {
Johannes Berg182e2e62008-04-04 10:41:56 +02004742 if (channels[i].flags & IEEE80211_CHAN_DISABLED)
4743 continue;
4744
Johannes Berg8318d782008-01-24 19:38:38 +01004745 scan_ch->channel = channels[i].hw_value;
Zhu Yib481de92007-09-25 17:54:57 -07004746
Johannes Berg8318d782008-01-24 19:38:38 +01004747 ch_info = iwl3945_get_channel_info(priv, band, scan_ch->channel);
Zhu Yib481de92007-09-25 17:54:57 -07004748 if (!is_channel_valid(ch_info)) {
Ron Rindjunsky66b50042008-06-25 16:46:31 +08004749 IWL_DEBUG_SCAN("Channel %d is INVALID for this band.\n",
Zhu Yib481de92007-09-25 17:54:57 -07004750 scan_ch->channel);
4751 continue;
4752 }
4753
Zhu Yib481de92007-09-25 17:54:57 -07004754 scan_ch->active_dwell = cpu_to_le16(active_dwell);
4755 scan_ch->passive_dwell = cpu_to_le16(passive_dwell);
Abhijeet Kolekar011a0332008-12-02 12:14:07 -08004756 /* If passive , set up for auto-switch
4757 * and use long active_dwell time.
4758 */
4759 if (!is_active || is_channel_passive(ch_info) ||
4760 (channels[i].flags & IEEE80211_CHAN_PASSIVE_SCAN)) {
4761 scan_ch->type = 0; /* passive */
4762 if (IWL_UCODE_API(priv->ucode_ver) == 1)
4763 scan_ch->active_dwell = cpu_to_le16(passive_dwell - 1);
4764 } else {
4765 scan_ch->type = 1; /* active */
4766 }
4767
4768 /* Set direct probe bits. These may be used both for active
4769 * scan channels (probes gets sent right away),
4770 * or for passive channels (probes get se sent only after
4771 * hearing clear Rx packet).*/
4772 if (IWL_UCODE_API(priv->ucode_ver) >= 2) {
4773 if (n_probes)
4774 scan_ch->type |= IWL_SCAN_PROBE_MASK(n_probes);
4775 } else {
4776 /* uCode v1 does not allow setting direct probe bits on
4777 * passive channel. */
4778 if ((scan_ch->type & 1) && n_probes)
4779 scan_ch->type |= IWL_SCAN_PROBE_MASK(n_probes);
4780 }
Zhu Yib481de92007-09-25 17:54:57 -07004781
Ben Cahill9fbab512007-11-29 11:09:47 +08004782 /* Set txpower levels to defaults */
Zhu Yib481de92007-09-25 17:54:57 -07004783 scan_ch->tpc.dsp_atten = 110;
4784 /* scan_pwr_info->tpc.dsp_atten; */
4785
4786 /*scan_pwr_info->tpc.tx_gain; */
Johannes Berg8318d782008-01-24 19:38:38 +01004787 if (band == IEEE80211_BAND_5GHZ)
Zhu Yib481de92007-09-25 17:54:57 -07004788 scan_ch->tpc.tx_gain = ((1 << 5) | (3 << 3)) | 3;
4789 else {
4790 scan_ch->tpc.tx_gain = ((1 << 5) | (5 << 3));
4791 /* NOTE: if we were doing 6Mb OFDM for scans we'd use
Ben Cahill9fbab512007-11-29 11:09:47 +08004792 * power level:
Reinette Chatre8a1b0242008-01-14 17:46:25 -08004793 * scan_ch->tpc.tx_gain = ((1 << 5) | (2 << 3)) | 3;
Zhu Yib481de92007-09-25 17:54:57 -07004794 */
4795 }
4796
4797 IWL_DEBUG_SCAN("Scanning %d [%s %d]\n",
4798 scan_ch->channel,
4799 (scan_ch->type & 1) ? "ACTIVE" : "PASSIVE",
4800 (scan_ch->type & 1) ?
4801 active_dwell : passive_dwell);
4802
4803 scan_ch++;
4804 added++;
4805 }
4806
4807 IWL_DEBUG_SCAN("total channels to scan %d \n", added);
4808 return added;
4809}
4810
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004811static void iwl3945_init_hw_rates(struct iwl3945_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07004812 struct ieee80211_rate *rates)
4813{
4814 int i;
4815
4816 for (i = 0; i < IWL_RATE_COUNT; i++) {
Johannes Berg8318d782008-01-24 19:38:38 +01004817 rates[i].bitrate = iwl3945_rates[i].ieee * 5;
4818 rates[i].hw_value = i; /* Rate scaling will work on indexes */
4819 rates[i].hw_value_short = i;
4820 rates[i].flags = 0;
Samuel Ortizd9829a62008-12-19 10:37:12 +08004821 if ((i > IWL39_LAST_OFDM_RATE) || (i < IWL_FIRST_OFDM_RATE)) {
Zhu Yib481de92007-09-25 17:54:57 -07004822 /*
Johannes Berg8318d782008-01-24 19:38:38 +01004823 * If CCK != 1M then set short preamble rate flag.
Zhu Yib481de92007-09-25 17:54:57 -07004824 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004825 rates[i].flags |= (iwl3945_rates[i].plcp == 10) ?
Johannes Berg8318d782008-01-24 19:38:38 +01004826 0 : IEEE80211_RATE_SHORT_PREAMBLE;
Zhu Yib481de92007-09-25 17:54:57 -07004827 }
Zhu Yib481de92007-09-25 17:54:57 -07004828 }
4829}
4830
4831/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004832 * iwl3945_init_geos - Initialize mac80211's geo/channel info based from eeprom
Zhu Yib481de92007-09-25 17:54:57 -07004833 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004834static int iwl3945_init_geos(struct iwl3945_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004835{
Samuel Ortizd20b3c62008-12-19 10:37:15 +08004836 struct iwl_channel_info *ch;
Tomas Winkler8211ef72008-03-02 01:36:04 +02004837 struct ieee80211_supported_band *sband;
Zhu Yib481de92007-09-25 17:54:57 -07004838 struct ieee80211_channel *channels;
4839 struct ieee80211_channel *geo_ch;
4840 struct ieee80211_rate *rates;
4841 int i = 0;
Zhu Yib481de92007-09-25 17:54:57 -07004842
Johannes Berg8318d782008-01-24 19:38:38 +01004843 if (priv->bands[IEEE80211_BAND_2GHZ].n_bitrates ||
4844 priv->bands[IEEE80211_BAND_5GHZ].n_bitrates) {
Zhu Yib481de92007-09-25 17:54:57 -07004845 IWL_DEBUG_INFO("Geography modes already initialized.\n");
4846 set_bit(STATUS_GEO_CONFIGURED, &priv->status);
4847 return 0;
4848 }
4849
Zhu Yib481de92007-09-25 17:54:57 -07004850 channels = kzalloc(sizeof(struct ieee80211_channel) *
4851 priv->channel_count, GFP_KERNEL);
Johannes Berg8318d782008-01-24 19:38:38 +01004852 if (!channels)
Zhu Yib481de92007-09-25 17:54:57 -07004853 return -ENOMEM;
Zhu Yib481de92007-09-25 17:54:57 -07004854
Tomas Winkler8211ef72008-03-02 01:36:04 +02004855 rates = kzalloc((sizeof(struct ieee80211_rate) * (IWL_RATE_COUNT + 1)),
Zhu Yib481de92007-09-25 17:54:57 -07004856 GFP_KERNEL);
4857 if (!rates) {
Zhu Yib481de92007-09-25 17:54:57 -07004858 kfree(channels);
4859 return -ENOMEM;
4860 }
4861
Zhu Yib481de92007-09-25 17:54:57 -07004862 /* 5.2GHz channels start after the 2.4GHz channels */
Tomas Winkler8211ef72008-03-02 01:36:04 +02004863 sband = &priv->bands[IEEE80211_BAND_5GHZ];
4864 sband->channels = &channels[ARRAY_SIZE(iwl3945_eeprom_band_1)];
4865 /* just OFDM */
4866 sband->bitrates = &rates[IWL_FIRST_OFDM_RATE];
4867 sband->n_bitrates = IWL_RATE_COUNT - IWL_FIRST_OFDM_RATE;
Zhu Yib481de92007-09-25 17:54:57 -07004868
Tomas Winkler8211ef72008-03-02 01:36:04 +02004869 sband = &priv->bands[IEEE80211_BAND_2GHZ];
4870 sband->channels = channels;
4871 /* OFDM & CCK */
4872 sband->bitrates = rates;
4873 sband->n_bitrates = IWL_RATE_COUNT;
Zhu Yib481de92007-09-25 17:54:57 -07004874
4875 priv->ieee_channels = channels;
4876 priv->ieee_rates = rates;
4877
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004878 iwl3945_init_hw_rates(priv, rates);
Zhu Yib481de92007-09-25 17:54:57 -07004879
Tomas Winkler8211ef72008-03-02 01:36:04 +02004880 for (i = 0; i < priv->channel_count; i++) {
Zhu Yib481de92007-09-25 17:54:57 -07004881 ch = &priv->channel_info[i];
4882
Tomas Winkler8211ef72008-03-02 01:36:04 +02004883 /* FIXME: might be removed if scan is OK*/
4884 if (!is_channel_valid(ch))
Zhu Yib481de92007-09-25 17:54:57 -07004885 continue;
Zhu Yib481de92007-09-25 17:54:57 -07004886
4887 if (is_channel_a_band(ch))
Tomas Winkler8211ef72008-03-02 01:36:04 +02004888 sband = &priv->bands[IEEE80211_BAND_5GHZ];
Johannes Berg8318d782008-01-24 19:38:38 +01004889 else
Tomas Winkler8211ef72008-03-02 01:36:04 +02004890 sband = &priv->bands[IEEE80211_BAND_2GHZ];
Zhu Yib481de92007-09-25 17:54:57 -07004891
Tomas Winkler8211ef72008-03-02 01:36:04 +02004892 geo_ch = &sband->channels[sband->n_channels++];
4893
4894 geo_ch->center_freq = ieee80211_channel_to_frequency(ch->channel);
Johannes Berg8318d782008-01-24 19:38:38 +01004895 geo_ch->max_power = ch->max_power_avg;
4896 geo_ch->max_antenna_gain = 0xff;
Mohamed Abbas7b723042008-01-31 21:46:40 -08004897 geo_ch->hw_value = ch->channel;
Zhu Yib481de92007-09-25 17:54:57 -07004898
4899 if (is_channel_valid(ch)) {
Johannes Berg8318d782008-01-24 19:38:38 +01004900 if (!(ch->flags & EEPROM_CHANNEL_IBSS))
4901 geo_ch->flags |= IEEE80211_CHAN_NO_IBSS;
Zhu Yib481de92007-09-25 17:54:57 -07004902
Johannes Berg8318d782008-01-24 19:38:38 +01004903 if (!(ch->flags & EEPROM_CHANNEL_ACTIVE))
4904 geo_ch->flags |= IEEE80211_CHAN_PASSIVE_SCAN;
Zhu Yib481de92007-09-25 17:54:57 -07004905
4906 if (ch->flags & EEPROM_CHANNEL_RADAR)
Johannes Berg8318d782008-01-24 19:38:38 +01004907 geo_ch->flags |= IEEE80211_CHAN_RADAR;
Zhu Yib481de92007-09-25 17:54:57 -07004908
4909 if (ch->max_power_avg > priv->max_channel_txpower_limit)
4910 priv->max_channel_txpower_limit =
4911 ch->max_power_avg;
Tomas Winkler8211ef72008-03-02 01:36:04 +02004912 } else {
Johannes Berg8318d782008-01-24 19:38:38 +01004913 geo_ch->flags |= IEEE80211_CHAN_DISABLED;
Tomas Winkler8211ef72008-03-02 01:36:04 +02004914 }
4915
4916 /* Save flags for reg domain usage */
4917 geo_ch->orig_flags = geo_ch->flags;
4918
4919 IWL_DEBUG_INFO("Channel %d Freq=%d[%sGHz] %s flag=0%X\n",
4920 ch->channel, geo_ch->center_freq,
4921 is_channel_a_band(ch) ? "5.2" : "2.4",
4922 geo_ch->flags & IEEE80211_CHAN_DISABLED ?
4923 "restricted" : "valid",
4924 geo_ch->flags);
Zhu Yib481de92007-09-25 17:54:57 -07004925 }
4926
Tomas Winkler82b9a122008-03-04 18:09:30 -08004927 if ((priv->bands[IEEE80211_BAND_5GHZ].n_channels == 0) &&
4928 priv->cfg->sku & IWL_SKU_A) {
Zhu Yib481de92007-09-25 17:54:57 -07004929 printk(KERN_INFO DRV_NAME
4930 ": Incorrectly detected BG card as ABG. Please send "
4931 "your PCI ID 0x%04X:0x%04X to maintainer.\n",
4932 priv->pci_dev->device, priv->pci_dev->subsystem_device);
Tomas Winkler82b9a122008-03-04 18:09:30 -08004933 priv->cfg->sku &= ~IWL_SKU_A;
Zhu Yib481de92007-09-25 17:54:57 -07004934 }
4935
4936 printk(KERN_INFO DRV_NAME
4937 ": Tunable channels: %d 802.11bg, %d 802.11a channels\n",
Johannes Berg8318d782008-01-24 19:38:38 +01004938 priv->bands[IEEE80211_BAND_2GHZ].n_channels,
4939 priv->bands[IEEE80211_BAND_5GHZ].n_channels);
Zhu Yib481de92007-09-25 17:54:57 -07004940
John W. Linvillee0e0a672008-03-25 15:58:40 -04004941 if (priv->bands[IEEE80211_BAND_2GHZ].n_channels)
4942 priv->hw->wiphy->bands[IEEE80211_BAND_2GHZ] =
4943 &priv->bands[IEEE80211_BAND_2GHZ];
4944 if (priv->bands[IEEE80211_BAND_5GHZ].n_channels)
4945 priv->hw->wiphy->bands[IEEE80211_BAND_5GHZ] =
4946 &priv->bands[IEEE80211_BAND_5GHZ];
Zhu Yib481de92007-09-25 17:54:57 -07004947
Zhu Yib481de92007-09-25 17:54:57 -07004948 set_bit(STATUS_GEO_CONFIGURED, &priv->status);
4949
4950 return 0;
4951}
4952
Reinette Chatre849e0dc2008-01-23 10:15:18 -08004953/*
4954 * iwl3945_free_geos - undo allocations in iwl3945_init_geos
4955 */
4956static void iwl3945_free_geos(struct iwl3945_priv *priv)
4957{
Reinette Chatre849e0dc2008-01-23 10:15:18 -08004958 kfree(priv->ieee_channels);
4959 kfree(priv->ieee_rates);
4960 clear_bit(STATUS_GEO_CONFIGURED, &priv->status);
4961}
4962
Zhu Yib481de92007-09-25 17:54:57 -07004963/******************************************************************************
4964 *
4965 * uCode download functions
4966 *
4967 ******************************************************************************/
4968
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004969static void iwl3945_dealloc_ucode_pci(struct iwl3945_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004970{
Tomas Winkler98c92212008-01-14 17:46:20 -08004971 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_code);
4972 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_data);
4973 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_data_backup);
4974 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_init);
4975 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_init_data);
4976 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_boot);
Zhu Yib481de92007-09-25 17:54:57 -07004977}
4978
4979/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004980 * iwl3945_verify_inst_full - verify runtime uCode image in card vs. host,
Zhu Yib481de92007-09-25 17:54:57 -07004981 * looking at all data.
4982 */
Tomas Winkler3ac7f142008-07-21 02:40:14 +03004983static int iwl3945_verify_inst_full(struct iwl3945_priv *priv, __le32 *image, u32 len)
Zhu Yib481de92007-09-25 17:54:57 -07004984{
4985 u32 val;
4986 u32 save_len = len;
4987 int rc = 0;
4988 u32 errcnt;
4989
4990 IWL_DEBUG_INFO("ucode inst image size is %u\n", len);
4991
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004992 rc = iwl3945_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004993 if (rc)
4994 return rc;
4995
Samuel Ortiz250bdd22008-12-19 10:37:11 +08004996 iwl3945_write_direct32(priv, HBUS_TARG_MEM_RADDR,
4997 IWL39_RTC_INST_LOWER_BOUND);
Zhu Yib481de92007-09-25 17:54:57 -07004998
4999 errcnt = 0;
5000 for (; len > 0; len -= sizeof(u32), image++) {
5001 /* read data comes through single port, auto-incr addr */
5002 /* NOTE: Use the debugless read so we don't flood kernel log
5003 * if IWL_DL_IO is set */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005004 val = _iwl3945_read_direct32(priv, HBUS_TARG_MEM_RDAT);
Zhu Yib481de92007-09-25 17:54:57 -07005005 if (val != le32_to_cpu(*image)) {
5006 IWL_ERROR("uCode INST section is invalid at "
5007 "offset 0x%x, is 0x%x, s/b 0x%x\n",
5008 save_len - len, val, le32_to_cpu(*image));
5009 rc = -EIO;
5010 errcnt++;
5011 if (errcnt >= 20)
5012 break;
5013 }
5014 }
5015
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005016 iwl3945_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005017
5018 if (!errcnt)
Ian Schrambc434dd2007-10-25 17:15:29 +08005019 IWL_DEBUG_INFO("ucode image in INSTRUCTION memory is good\n");
Zhu Yib481de92007-09-25 17:54:57 -07005020
5021 return rc;
5022}
5023
5024
5025/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005026 * iwl3945_verify_inst_sparse - verify runtime uCode image in card vs. host,
Zhu Yib481de92007-09-25 17:54:57 -07005027 * using sample data 100 bytes apart. If these sample points are good,
5028 * it's a pretty good bet that everything between them is good, too.
5029 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005030static int iwl3945_verify_inst_sparse(struct iwl3945_priv *priv, __le32 *image, u32 len)
Zhu Yib481de92007-09-25 17:54:57 -07005031{
5032 u32 val;
5033 int rc = 0;
5034 u32 errcnt = 0;
5035 u32 i;
5036
5037 IWL_DEBUG_INFO("ucode inst image size is %u\n", len);
5038
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005039 rc = iwl3945_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005040 if (rc)
5041 return rc;
5042
5043 for (i = 0; i < len; i += 100, image += 100/sizeof(u32)) {
5044 /* read data comes through single port, auto-incr addr */
5045 /* NOTE: Use the debugless read so we don't flood kernel log
5046 * if IWL_DL_IO is set */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005047 iwl3945_write_direct32(priv, HBUS_TARG_MEM_RADDR,
Samuel Ortiz250bdd22008-12-19 10:37:11 +08005048 i + IWL39_RTC_INST_LOWER_BOUND);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005049 val = _iwl3945_read_direct32(priv, HBUS_TARG_MEM_RDAT);
Zhu Yib481de92007-09-25 17:54:57 -07005050 if (val != le32_to_cpu(*image)) {
5051#if 0 /* Enable this if you want to see details */
5052 IWL_ERROR("uCode INST section is invalid at "
5053 "offset 0x%x, is 0x%x, s/b 0x%x\n",
5054 i, val, *image);
5055#endif
5056 rc = -EIO;
5057 errcnt++;
5058 if (errcnt >= 3)
5059 break;
5060 }
5061 }
5062
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005063 iwl3945_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005064
5065 return rc;
5066}
5067
5068
5069/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005070 * iwl3945_verify_ucode - determine which instruction image is in SRAM,
Zhu Yib481de92007-09-25 17:54:57 -07005071 * and verify its contents
5072 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005073static int iwl3945_verify_ucode(struct iwl3945_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005074{
5075 __le32 *image;
5076 u32 len;
5077 int rc = 0;
5078
5079 /* Try bootstrap */
5080 image = (__le32 *)priv->ucode_boot.v_addr;
5081 len = priv->ucode_boot.len;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005082 rc = iwl3945_verify_inst_sparse(priv, image, len);
Zhu Yib481de92007-09-25 17:54:57 -07005083 if (rc == 0) {
5084 IWL_DEBUG_INFO("Bootstrap uCode is good in inst SRAM\n");
5085 return 0;
5086 }
5087
5088 /* Try initialize */
5089 image = (__le32 *)priv->ucode_init.v_addr;
5090 len = priv->ucode_init.len;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005091 rc = iwl3945_verify_inst_sparse(priv, image, len);
Zhu Yib481de92007-09-25 17:54:57 -07005092 if (rc == 0) {
5093 IWL_DEBUG_INFO("Initialize uCode is good in inst SRAM\n");
5094 return 0;
5095 }
5096
5097 /* Try runtime/protocol */
5098 image = (__le32 *)priv->ucode_code.v_addr;
5099 len = priv->ucode_code.len;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005100 rc = iwl3945_verify_inst_sparse(priv, image, len);
Zhu Yib481de92007-09-25 17:54:57 -07005101 if (rc == 0) {
5102 IWL_DEBUG_INFO("Runtime uCode is good in inst SRAM\n");
5103 return 0;
5104 }
5105
5106 IWL_ERROR("NO VALID UCODE IMAGE IN INSTRUCTION SRAM!!\n");
5107
Ben Cahill9fbab512007-11-29 11:09:47 +08005108 /* Since nothing seems to match, show first several data entries in
5109 * instruction SRAM, so maybe visual inspection will give a clue.
5110 * Selection of bootstrap image (vs. other images) is arbitrary. */
Zhu Yib481de92007-09-25 17:54:57 -07005111 image = (__le32 *)priv->ucode_boot.v_addr;
5112 len = priv->ucode_boot.len;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005113 rc = iwl3945_verify_inst_full(priv, image, len);
Zhu Yib481de92007-09-25 17:54:57 -07005114
5115 return rc;
5116}
5117
5118
5119/* check contents of special bootstrap uCode SRAM */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005120static int iwl3945_verify_bsm(struct iwl3945_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005121{
5122 __le32 *image = priv->ucode_boot.v_addr;
5123 u32 len = priv->ucode_boot.len;
5124 u32 reg;
5125 u32 val;
5126
5127 IWL_DEBUG_INFO("Begin verify bsm\n");
5128
5129 /* verify BSM SRAM contents */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005130 val = iwl3945_read_prph(priv, BSM_WR_DWCOUNT_REG);
Zhu Yib481de92007-09-25 17:54:57 -07005131 for (reg = BSM_SRAM_LOWER_BOUND;
5132 reg < BSM_SRAM_LOWER_BOUND + len;
Tomas Winkler3ac7f142008-07-21 02:40:14 +03005133 reg += sizeof(u32), image++) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005134 val = iwl3945_read_prph(priv, reg);
Zhu Yib481de92007-09-25 17:54:57 -07005135 if (val != le32_to_cpu(*image)) {
5136 IWL_ERROR("BSM uCode verification failed at "
5137 "addr 0x%08X+%u (of %u), is 0x%x, s/b 0x%x\n",
5138 BSM_SRAM_LOWER_BOUND,
5139 reg - BSM_SRAM_LOWER_BOUND, len,
5140 val, le32_to_cpu(*image));
5141 return -EIO;
5142 }
5143 }
5144
5145 IWL_DEBUG_INFO("BSM bootstrap uCode image OK\n");
5146
5147 return 0;
5148}
5149
5150/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005151 * iwl3945_load_bsm - Load bootstrap instructions
Zhu Yib481de92007-09-25 17:54:57 -07005152 *
5153 * BSM operation:
5154 *
5155 * The Bootstrap State Machine (BSM) stores a short bootstrap uCode program
5156 * in special SRAM that does not power down during RFKILL. When powering back
5157 * up after power-saving sleeps (or during initial uCode load), the BSM loads
5158 * the bootstrap program into the on-board processor, and starts it.
5159 *
5160 * The bootstrap program loads (via DMA) instructions and data for a new
5161 * program from host DRAM locations indicated by the host driver in the
5162 * BSM_DRAM_* registers. Once the new program is loaded, it starts
5163 * automatically.
5164 *
5165 * When initializing the NIC, the host driver points the BSM to the
5166 * "initialize" uCode image. This uCode sets up some internal data, then
5167 * notifies host via "initialize alive" that it is complete.
5168 *
5169 * The host then replaces the BSM_DRAM_* pointer values to point to the
5170 * normal runtime uCode instructions and a backup uCode data cache buffer
5171 * (filled initially with starting data values for the on-board processor),
5172 * then triggers the "initialize" uCode to load and launch the runtime uCode,
5173 * which begins normal operation.
5174 *
5175 * When doing a power-save shutdown, runtime uCode saves data SRAM into
5176 * the backup data cache in DRAM before SRAM is powered down.
5177 *
5178 * When powering back up, the BSM loads the bootstrap program. This reloads
5179 * the runtime uCode instructions and the backup data cache into SRAM,
5180 * and re-launches the runtime uCode from where it left off.
5181 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005182static int iwl3945_load_bsm(struct iwl3945_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005183{
5184 __le32 *image = priv->ucode_boot.v_addr;
5185 u32 len = priv->ucode_boot.len;
5186 dma_addr_t pinst;
5187 dma_addr_t pdata;
5188 u32 inst_len;
5189 u32 data_len;
5190 int rc;
5191 int i;
5192 u32 done;
5193 u32 reg_offset;
5194
5195 IWL_DEBUG_INFO("Begin load bsm\n");
5196
5197 /* make sure bootstrap program is no larger than BSM's SRAM size */
Samuel Ortiz250bdd22008-12-19 10:37:11 +08005198 if (len > IWL39_MAX_BSM_SIZE)
Zhu Yib481de92007-09-25 17:54:57 -07005199 return -EINVAL;
5200
5201 /* Tell bootstrap uCode where to find the "Initialize" uCode
Ben Cahill9fbab512007-11-29 11:09:47 +08005202 * in host DRAM ... host DRAM physical address bits 31:0 for 3945.
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005203 * NOTE: iwl3945_initialize_alive_start() will replace these values,
Zhu Yib481de92007-09-25 17:54:57 -07005204 * after the "initialize" uCode has run, to point to
5205 * runtime/protocol instructions and backup data cache. */
5206 pinst = priv->ucode_init.p_addr;
5207 pdata = priv->ucode_init_data.p_addr;
5208 inst_len = priv->ucode_init.len;
5209 data_len = priv->ucode_init_data.len;
5210
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005211 rc = iwl3945_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005212 if (rc)
5213 return rc;
5214
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005215 iwl3945_write_prph(priv, BSM_DRAM_INST_PTR_REG, pinst);
5216 iwl3945_write_prph(priv, BSM_DRAM_DATA_PTR_REG, pdata);
5217 iwl3945_write_prph(priv, BSM_DRAM_INST_BYTECOUNT_REG, inst_len);
5218 iwl3945_write_prph(priv, BSM_DRAM_DATA_BYTECOUNT_REG, data_len);
Zhu Yib481de92007-09-25 17:54:57 -07005219
5220 /* Fill BSM memory with bootstrap instructions */
5221 for (reg_offset = BSM_SRAM_LOWER_BOUND;
5222 reg_offset < BSM_SRAM_LOWER_BOUND + len;
5223 reg_offset += sizeof(u32), image++)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005224 _iwl3945_write_prph(priv, reg_offset,
Zhu Yib481de92007-09-25 17:54:57 -07005225 le32_to_cpu(*image));
5226
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005227 rc = iwl3945_verify_bsm(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005228 if (rc) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005229 iwl3945_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005230 return rc;
5231 }
5232
5233 /* Tell BSM to copy from BSM SRAM into instruction SRAM, when asked */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005234 iwl3945_write_prph(priv, BSM_WR_MEM_SRC_REG, 0x0);
5235 iwl3945_write_prph(priv, BSM_WR_MEM_DST_REG,
Samuel Ortiz250bdd22008-12-19 10:37:11 +08005236 IWL39_RTC_INST_LOWER_BOUND);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005237 iwl3945_write_prph(priv, BSM_WR_DWCOUNT_REG, len / sizeof(u32));
Zhu Yib481de92007-09-25 17:54:57 -07005238
5239 /* Load bootstrap code into instruction SRAM now,
5240 * to prepare to load "initialize" uCode */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005241 iwl3945_write_prph(priv, BSM_WR_CTRL_REG,
Zhu Yib481de92007-09-25 17:54:57 -07005242 BSM_WR_CTRL_REG_BIT_START);
5243
5244 /* Wait for load of bootstrap uCode to finish */
5245 for (i = 0; i < 100; i++) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005246 done = iwl3945_read_prph(priv, BSM_WR_CTRL_REG);
Zhu Yib481de92007-09-25 17:54:57 -07005247 if (!(done & BSM_WR_CTRL_REG_BIT_START))
5248 break;
5249 udelay(10);
5250 }
5251 if (i < 100)
5252 IWL_DEBUG_INFO("BSM write complete, poll %d iterations\n", i);
5253 else {
5254 IWL_ERROR("BSM write did not complete!\n");
5255 return -EIO;
5256 }
5257
5258 /* Enable future boot loads whenever power management unit triggers it
5259 * (e.g. when powering back up after power-save shutdown) */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005260 iwl3945_write_prph(priv, BSM_WR_CTRL_REG,
Zhu Yib481de92007-09-25 17:54:57 -07005261 BSM_WR_CTRL_REG_BIT_START_EN);
5262
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005263 iwl3945_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005264
5265 return 0;
5266}
5267
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005268static void iwl3945_nic_start(struct iwl3945_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005269{
5270 /* Remove all resets to allow NIC to operate */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005271 iwl3945_write32(priv, CSR_RESET, 0);
Zhu Yib481de92007-09-25 17:54:57 -07005272}
5273
5274/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005275 * iwl3945_read_ucode - Read uCode images from disk file.
Zhu Yib481de92007-09-25 17:54:57 -07005276 *
5277 * Copy into buffers for card to fetch via bus-mastering
5278 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005279static int iwl3945_read_ucode(struct iwl3945_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005280{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005281 struct iwl3945_ucode *ucode;
Reinette Chatrea0987a82008-12-02 12:14:06 -08005282 int ret = -EINVAL, index;
Zhu Yib481de92007-09-25 17:54:57 -07005283 const struct firmware *ucode_raw;
5284 /* firmware file name contains uCode/driver compatibility version */
Reinette Chatrea0987a82008-12-02 12:14:06 -08005285 const char *name_pre = priv->cfg->fw_name_pre;
5286 const unsigned int api_max = priv->cfg->ucode_api_max;
5287 const unsigned int api_min = priv->cfg->ucode_api_min;
5288 char buf[25];
Zhu Yib481de92007-09-25 17:54:57 -07005289 u8 *src;
5290 size_t len;
Reinette Chatrea0987a82008-12-02 12:14:06 -08005291 u32 api_ver, inst_size, data_size, init_size, init_data_size, boot_size;
Zhu Yib481de92007-09-25 17:54:57 -07005292
5293 /* Ask kernel firmware_class module to get the boot firmware off disk.
5294 * request_firmware() is synchronous, file is in memory on return. */
Reinette Chatrea0987a82008-12-02 12:14:06 -08005295 for (index = api_max; index >= api_min; index--) {
5296 sprintf(buf, "%s%u%s", name_pre, index, ".ucode");
5297 ret = request_firmware(&ucode_raw, buf, &priv->pci_dev->dev);
5298 if (ret < 0) {
5299 IWL_ERROR("%s firmware file req failed: Reason %d\n",
5300 buf, ret);
5301 if (ret == -ENOENT)
5302 continue;
5303 else
5304 goto error;
5305 } else {
5306 if (index < api_max)
5307 IWL_ERROR("Loaded firmware %s, which is deprecated. Please use API v%u instead.\n",
5308 buf, api_max);
5309 IWL_DEBUG_INFO("Got firmware '%s' file (%zd bytes) from disk\n",
5310 buf, ucode_raw->size);
5311 break;
5312 }
Zhu Yib481de92007-09-25 17:54:57 -07005313 }
5314
Reinette Chatrea0987a82008-12-02 12:14:06 -08005315 if (ret < 0)
5316 goto error;
Zhu Yib481de92007-09-25 17:54:57 -07005317
5318 /* Make sure that we got at least our header! */
5319 if (ucode_raw->size < sizeof(*ucode)) {
5320 IWL_ERROR("File size way too small!\n");
Tomas Winkler90e759d2007-11-29 11:09:41 +08005321 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07005322 goto err_release;
5323 }
5324
5325 /* Data from ucode file: header followed by uCode images */
5326 ucode = (void *)ucode_raw->data;
5327
Chatre, Reinettec02b3ac2008-12-02 12:14:05 -08005328 priv->ucode_ver = le32_to_cpu(ucode->ver);
Reinette Chatrea0987a82008-12-02 12:14:06 -08005329 api_ver = IWL_UCODE_API(priv->ucode_ver);
Zhu Yib481de92007-09-25 17:54:57 -07005330 inst_size = le32_to_cpu(ucode->inst_size);
5331 data_size = le32_to_cpu(ucode->data_size);
5332 init_size = le32_to_cpu(ucode->init_size);
5333 init_data_size = le32_to_cpu(ucode->init_data_size);
5334 boot_size = le32_to_cpu(ucode->boot_size);
5335
Reinette Chatrea0987a82008-12-02 12:14:06 -08005336 /* api_ver should match the api version forming part of the
5337 * firmware filename ... but we don't check for that and only rely
5338 * on the API version read from firware header from here on forward */
5339
5340 if (api_ver < api_min || api_ver > api_max) {
5341 IWL_ERROR("Driver unable to support your firmware API. "
5342 "Driver supports v%u, firmware is v%u.\n",
5343 api_max, api_ver);
5344 priv->ucode_ver = 0;
5345 ret = -EINVAL;
5346 goto err_release;
5347 }
5348 if (api_ver != api_max)
5349 IWL_ERROR("Firmware has old API version. Expected %u, "
5350 "got %u. New firmware can be obtained "
5351 "from http://www.intellinuxwireless.org.\n",
5352 api_max, api_ver);
5353
5354 printk(KERN_INFO DRV_NAME " loaded firmware version %u.%u.%u.%u\n",
Chatre, Reinettec02b3ac2008-12-02 12:14:05 -08005355 IWL_UCODE_MAJOR(priv->ucode_ver),
5356 IWL_UCODE_MINOR(priv->ucode_ver),
5357 IWL_UCODE_API(priv->ucode_ver),
5358 IWL_UCODE_SERIAL(priv->ucode_ver));
Reinette Chatrea0987a82008-12-02 12:14:06 -08005359 IWL_DEBUG_INFO("f/w package hdr ucode version raw = 0x%x\n",
5360 priv->ucode_ver);
Ian Schrambc434dd2007-10-25 17:15:29 +08005361 IWL_DEBUG_INFO("f/w package hdr runtime inst size = %u\n", inst_size);
5362 IWL_DEBUG_INFO("f/w package hdr runtime data size = %u\n", data_size);
5363 IWL_DEBUG_INFO("f/w package hdr init inst size = %u\n", init_size);
5364 IWL_DEBUG_INFO("f/w package hdr init data size = %u\n", init_data_size);
5365 IWL_DEBUG_INFO("f/w package hdr boot inst size = %u\n", boot_size);
Zhu Yib481de92007-09-25 17:54:57 -07005366
Reinette Chatrea0987a82008-12-02 12:14:06 -08005367
Zhu Yib481de92007-09-25 17:54:57 -07005368 /* Verify size of file vs. image size info in file's header */
5369 if (ucode_raw->size < sizeof(*ucode) +
5370 inst_size + data_size + init_size +
5371 init_data_size + boot_size) {
5372
5373 IWL_DEBUG_INFO("uCode file size %d too small\n",
5374 (int)ucode_raw->size);
Tomas Winkler90e759d2007-11-29 11:09:41 +08005375 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07005376 goto err_release;
5377 }
5378
5379 /* Verify that uCode images will fit in card's SRAM */
Samuel Ortiz250bdd22008-12-19 10:37:11 +08005380 if (inst_size > IWL39_MAX_INST_SIZE) {
Tomas Winkler90e759d2007-11-29 11:09:41 +08005381 IWL_DEBUG_INFO("uCode instr len %d too large to fit in\n",
5382 inst_size);
5383 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07005384 goto err_release;
5385 }
5386
Samuel Ortiz250bdd22008-12-19 10:37:11 +08005387 if (data_size > IWL39_MAX_DATA_SIZE) {
Tomas Winkler90e759d2007-11-29 11:09:41 +08005388 IWL_DEBUG_INFO("uCode data len %d too large to fit in\n",
5389 data_size);
5390 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07005391 goto err_release;
5392 }
Samuel Ortiz250bdd22008-12-19 10:37:11 +08005393 if (init_size > IWL39_MAX_INST_SIZE) {
Tomas Winkler90e759d2007-11-29 11:09:41 +08005394 IWL_DEBUG_INFO("uCode init instr len %d too large to fit in\n",
5395 init_size);
5396 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07005397 goto err_release;
5398 }
Samuel Ortiz250bdd22008-12-19 10:37:11 +08005399 if (init_data_size > IWL39_MAX_DATA_SIZE) {
Tomas Winkler90e759d2007-11-29 11:09:41 +08005400 IWL_DEBUG_INFO("uCode init data len %d too large to fit in\n",
5401 init_data_size);
5402 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07005403 goto err_release;
5404 }
Samuel Ortiz250bdd22008-12-19 10:37:11 +08005405 if (boot_size > IWL39_MAX_BSM_SIZE) {
Tomas Winkler90e759d2007-11-29 11:09:41 +08005406 IWL_DEBUG_INFO("uCode boot instr len %d too large to fit in\n",
5407 boot_size);
5408 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07005409 goto err_release;
5410 }
5411
5412 /* Allocate ucode buffers for card's bus-master loading ... */
5413
5414 /* Runtime instructions and 2 copies of data:
5415 * 1) unmodified from disk
5416 * 2) backup cache for save/restore during power-downs */
5417 priv->ucode_code.len = inst_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08005418 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_code);
Zhu Yib481de92007-09-25 17:54:57 -07005419
5420 priv->ucode_data.len = data_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08005421 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_data);
Zhu Yib481de92007-09-25 17:54:57 -07005422
5423 priv->ucode_data_backup.len = data_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08005424 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_data_backup);
Zhu Yib481de92007-09-25 17:54:57 -07005425
5426 if (!priv->ucode_code.v_addr || !priv->ucode_data.v_addr ||
Tomas Winkler90e759d2007-11-29 11:09:41 +08005427 !priv->ucode_data_backup.v_addr)
Zhu Yib481de92007-09-25 17:54:57 -07005428 goto err_pci_alloc;
5429
Tomas Winkler90e759d2007-11-29 11:09:41 +08005430 /* Initialization instructions and data */
5431 if (init_size && init_data_size) {
5432 priv->ucode_init.len = init_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08005433 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_init);
Tomas Winkler90e759d2007-11-29 11:09:41 +08005434
5435 priv->ucode_init_data.len = init_data_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08005436 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_init_data);
Tomas Winkler90e759d2007-11-29 11:09:41 +08005437
5438 if (!priv->ucode_init.v_addr || !priv->ucode_init_data.v_addr)
5439 goto err_pci_alloc;
5440 }
5441
5442 /* Bootstrap (instructions only, no data) */
5443 if (boot_size) {
5444 priv->ucode_boot.len = boot_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08005445 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_boot);
Tomas Winkler90e759d2007-11-29 11:09:41 +08005446
5447 if (!priv->ucode_boot.v_addr)
5448 goto err_pci_alloc;
5449 }
5450
Zhu Yib481de92007-09-25 17:54:57 -07005451 /* Copy images into buffers for card's bus-master reads ... */
5452
5453 /* Runtime instructions (first block of data in file) */
5454 src = &ucode->data[0];
5455 len = priv->ucode_code.len;
Tomas Winkler90e759d2007-11-29 11:09:41 +08005456 IWL_DEBUG_INFO("Copying (but not loading) uCode instr len %Zd\n", len);
Zhu Yib481de92007-09-25 17:54:57 -07005457 memcpy(priv->ucode_code.v_addr, src, len);
5458 IWL_DEBUG_INFO("uCode instr buf vaddr = 0x%p, paddr = 0x%08x\n",
5459 priv->ucode_code.v_addr, (u32)priv->ucode_code.p_addr);
5460
5461 /* Runtime data (2nd block)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005462 * NOTE: Copy into backup buffer will be done in iwl3945_up() */
Zhu Yib481de92007-09-25 17:54:57 -07005463 src = &ucode->data[inst_size];
5464 len = priv->ucode_data.len;
Tomas Winkler90e759d2007-11-29 11:09:41 +08005465 IWL_DEBUG_INFO("Copying (but not loading) uCode data len %Zd\n", len);
Zhu Yib481de92007-09-25 17:54:57 -07005466 memcpy(priv->ucode_data.v_addr, src, len);
5467 memcpy(priv->ucode_data_backup.v_addr, src, len);
5468
5469 /* Initialization instructions (3rd block) */
5470 if (init_size) {
5471 src = &ucode->data[inst_size + data_size];
5472 len = priv->ucode_init.len;
Tomas Winkler90e759d2007-11-29 11:09:41 +08005473 IWL_DEBUG_INFO("Copying (but not loading) init instr len %Zd\n",
5474 len);
Zhu Yib481de92007-09-25 17:54:57 -07005475 memcpy(priv->ucode_init.v_addr, src, len);
5476 }
5477
5478 /* Initialization data (4th block) */
5479 if (init_data_size) {
5480 src = &ucode->data[inst_size + data_size + init_size];
5481 len = priv->ucode_init_data.len;
5482 IWL_DEBUG_INFO("Copying (but not loading) init data len %d\n",
5483 (int)len);
5484 memcpy(priv->ucode_init_data.v_addr, src, len);
5485 }
5486
5487 /* Bootstrap instructions (5th block) */
5488 src = &ucode->data[inst_size + data_size + init_size + init_data_size];
5489 len = priv->ucode_boot.len;
5490 IWL_DEBUG_INFO("Copying (but not loading) boot instr len %d\n",
5491 (int)len);
5492 memcpy(priv->ucode_boot.v_addr, src, len);
5493
5494 /* We have our copies now, allow OS release its copies */
5495 release_firmware(ucode_raw);
5496 return 0;
5497
5498 err_pci_alloc:
5499 IWL_ERROR("failed to allocate pci memory\n");
Tomas Winkler90e759d2007-11-29 11:09:41 +08005500 ret = -ENOMEM;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005501 iwl3945_dealloc_ucode_pci(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005502
5503 err_release:
5504 release_firmware(ucode_raw);
5505
5506 error:
Tomas Winkler90e759d2007-11-29 11:09:41 +08005507 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07005508}
5509
5510
5511/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005512 * iwl3945_set_ucode_ptrs - Set uCode address location
Zhu Yib481de92007-09-25 17:54:57 -07005513 *
5514 * Tell initialization uCode where to find runtime uCode.
5515 *
5516 * BSM registers initially contain pointers to initialization uCode.
5517 * We need to replace them to load runtime uCode inst and data,
5518 * and to save runtime data when powering down.
5519 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005520static int iwl3945_set_ucode_ptrs(struct iwl3945_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005521{
5522 dma_addr_t pinst;
5523 dma_addr_t pdata;
5524 int rc = 0;
5525 unsigned long flags;
5526
5527 /* bits 31:0 for 3945 */
5528 pinst = priv->ucode_code.p_addr;
5529 pdata = priv->ucode_data_backup.p_addr;
5530
5531 spin_lock_irqsave(&priv->lock, flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005532 rc = iwl3945_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005533 if (rc) {
5534 spin_unlock_irqrestore(&priv->lock, flags);
5535 return rc;
5536 }
5537
5538 /* Tell bootstrap uCode where to find image to load */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005539 iwl3945_write_prph(priv, BSM_DRAM_INST_PTR_REG, pinst);
5540 iwl3945_write_prph(priv, BSM_DRAM_DATA_PTR_REG, pdata);
5541 iwl3945_write_prph(priv, BSM_DRAM_DATA_BYTECOUNT_REG,
Zhu Yib481de92007-09-25 17:54:57 -07005542 priv->ucode_data.len);
5543
Tomas Winklera96a27f2008-10-23 23:48:56 -07005544 /* Inst byte count must be last to set up, bit 31 signals uCode
Zhu Yib481de92007-09-25 17:54:57 -07005545 * that all new ptr/size info is in place */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005546 iwl3945_write_prph(priv, BSM_DRAM_INST_BYTECOUNT_REG,
Zhu Yib481de92007-09-25 17:54:57 -07005547 priv->ucode_code.len | BSM_DRAM_INST_LOAD);
5548
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005549 iwl3945_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005550
5551 spin_unlock_irqrestore(&priv->lock, flags);
5552
5553 IWL_DEBUG_INFO("Runtime uCode pointers are set.\n");
5554
5555 return rc;
5556}
5557
5558/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005559 * iwl3945_init_alive_start - Called after REPLY_ALIVE notification received
Zhu Yib481de92007-09-25 17:54:57 -07005560 *
5561 * Called after REPLY_ALIVE notification received from "initialize" uCode.
5562 *
Zhu Yib481de92007-09-25 17:54:57 -07005563 * Tell "initialize" uCode to go ahead and load the runtime uCode.
Ben Cahill9fbab512007-11-29 11:09:47 +08005564 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005565static void iwl3945_init_alive_start(struct iwl3945_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005566{
5567 /* Check alive response for "valid" sign from uCode */
5568 if (priv->card_alive_init.is_valid != UCODE_VALID_OK) {
5569 /* We had an error bringing up the hardware, so take it
5570 * all the way back down so we can try again */
5571 IWL_DEBUG_INFO("Initialize Alive failed.\n");
5572 goto restart;
5573 }
5574
5575 /* Bootstrap uCode has loaded initialize uCode ... verify inst image.
5576 * This is a paranoid check, because we would not have gotten the
5577 * "initialize" alive if code weren't properly loaded. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005578 if (iwl3945_verify_ucode(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07005579 /* Runtime instruction load was bad;
5580 * take it all the way back down so we can try again */
5581 IWL_DEBUG_INFO("Bad \"initialize\" uCode load.\n");
5582 goto restart;
5583 }
5584
5585 /* Send pointers to protocol/runtime uCode image ... init code will
5586 * load and launch runtime uCode, which will send us another "Alive"
5587 * notification. */
5588 IWL_DEBUG_INFO("Initialization Alive received.\n");
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005589 if (iwl3945_set_ucode_ptrs(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07005590 /* Runtime instruction load won't happen;
5591 * take it all the way back down so we can try again */
5592 IWL_DEBUG_INFO("Couldn't set up uCode pointers.\n");
5593 goto restart;
5594 }
5595 return;
5596
5597 restart:
5598 queue_work(priv->workqueue, &priv->restart);
5599}
5600
5601
Mohamed Abbas9bdf5ec2008-11-07 09:58:35 -08005602/* temporary */
5603static int iwl3945_mac_beacon_update(struct ieee80211_hw *hw,
5604 struct sk_buff *skb);
5605
Zhu Yib481de92007-09-25 17:54:57 -07005606/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005607 * iwl3945_alive_start - called after REPLY_ALIVE notification received
Zhu Yib481de92007-09-25 17:54:57 -07005608 * from protocol/runtime uCode (initialization uCode's
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005609 * Alive gets handled by iwl3945_init_alive_start()).
Zhu Yib481de92007-09-25 17:54:57 -07005610 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005611static void iwl3945_alive_start(struct iwl3945_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005612{
5613 int rc = 0;
5614 int thermal_spin = 0;
5615 u32 rfkill;
5616
5617 IWL_DEBUG_INFO("Runtime Alive received.\n");
5618
5619 if (priv->card_alive.is_valid != UCODE_VALID_OK) {
5620 /* We had an error bringing up the hardware, so take it
5621 * all the way back down so we can try again */
5622 IWL_DEBUG_INFO("Alive failed.\n");
5623 goto restart;
5624 }
5625
5626 /* Initialize uCode has loaded Runtime uCode ... verify inst image.
5627 * This is a paranoid check, because we would not have gotten the
5628 * "runtime" alive if code weren't properly loaded. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005629 if (iwl3945_verify_ucode(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07005630 /* Runtime instruction load was bad;
5631 * take it all the way back down so we can try again */
5632 IWL_DEBUG_INFO("Bad runtime uCode load.\n");
5633 goto restart;
5634 }
5635
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005636 iwl3945_clear_stations_table(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005637
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005638 rc = iwl3945_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005639 if (rc) {
Tomas Winklera96a27f2008-10-23 23:48:56 -07005640 IWL_WARNING("Can not read RFKILL status from adapter\n");
Zhu Yib481de92007-09-25 17:54:57 -07005641 return;
5642 }
5643
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005644 rfkill = iwl3945_read_prph(priv, APMG_RFKILL_REG);
Zhu Yib481de92007-09-25 17:54:57 -07005645 IWL_DEBUG_INFO("RFKILL status: 0x%x\n", rfkill);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005646 iwl3945_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005647
5648 if (rfkill & 0x1) {
5649 clear_bit(STATUS_RF_KILL_HW, &priv->status);
Tomas Winklera96a27f2008-10-23 23:48:56 -07005650 /* if RFKILL is not on, then wait for thermal
Zhu Yib481de92007-09-25 17:54:57 -07005651 * sensor in adapter to kick in */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005652 while (iwl3945_hw_get_temperature(priv) == 0) {
Zhu Yib481de92007-09-25 17:54:57 -07005653 thermal_spin++;
5654 udelay(10);
5655 }
5656
5657 if (thermal_spin)
5658 IWL_DEBUG_INFO("Thermal calibration took %dus\n",
5659 thermal_spin * 10);
5660 } else
5661 set_bit(STATUS_RF_KILL_HW, &priv->status);
5662
Ben Cahill9fbab512007-11-29 11:09:47 +08005663 /* After the ALIVE response, we can send commands to 3945 uCode */
Zhu Yib481de92007-09-25 17:54:57 -07005664 set_bit(STATUS_ALIVE, &priv->status);
5665
5666 /* Clear out the uCode error bit if it is set */
5667 clear_bit(STATUS_FW_ERROR, &priv->status);
5668
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005669 if (iwl3945_is_rfkill(priv))
Zhu Yib481de92007-09-25 17:54:57 -07005670 return;
5671
Johannes Berg36d68252008-05-15 12:55:26 +02005672 ieee80211_wake_queues(priv->hw);
Zhu Yib481de92007-09-25 17:54:57 -07005673
5674 priv->active_rate = priv->rates_mask;
5675 priv->active_rate_basic = priv->rates_mask & IWL_BASIC_RATES_MASK;
5676
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005677 iwl3945_send_power_mode(priv, IWL_POWER_LEVEL(priv->power_mode));
Zhu Yib481de92007-09-25 17:54:57 -07005678
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005679 if (iwl3945_is_associated(priv)) {
5680 struct iwl3945_rxon_cmd *active_rxon =
5681 (struct iwl3945_rxon_cmd *)(&priv->active_rxon);
Zhu Yib481de92007-09-25 17:54:57 -07005682
5683 memcpy(&priv->staging_rxon, &priv->active_rxon,
5684 sizeof(priv->staging_rxon));
5685 active_rxon->filter_flags &= ~RXON_FILTER_ASSOC_MSK;
5686 } else {
5687 /* Initialize our rx_config data */
Zhu, Yi60294de2008-10-29 14:05:45 -07005688 iwl3945_connection_init_rx_config(priv, priv->iw_mode);
Zhu Yib481de92007-09-25 17:54:57 -07005689 memcpy(priv->staging_rxon.node_addr, priv->mac_addr, ETH_ALEN);
5690 }
5691
Ben Cahill9fbab512007-11-29 11:09:47 +08005692 /* Configure Bluetooth device coexistence support */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005693 iwl3945_send_bt_config(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005694
5695 /* Configure the adapter for unassociated operation */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005696 iwl3945_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005697
Zhu Yib481de92007-09-25 17:54:57 -07005698 iwl3945_reg_txpower_periodic(priv);
5699
Reinette Chatrefe00b5a2008-04-03 16:05:23 -07005700 iwl3945_led_register(priv);
5701
Zhu Yib481de92007-09-25 17:54:57 -07005702 IWL_DEBUG_INFO("ALIVE processing complete.\n");
Rick Farringtona9f46782008-03-18 14:57:49 -07005703 set_bit(STATUS_READY, &priv->status);
Zhu Yi5a669262008-01-14 17:46:18 -08005704 wake_up_interruptible(&priv->wait_command_queue);
Zhu Yib481de92007-09-25 17:54:57 -07005705
5706 if (priv->error_recovering)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005707 iwl3945_error_recovery(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005708
Mohamed Abbas9bdf5ec2008-11-07 09:58:35 -08005709 /* reassociate for ADHOC mode */
5710 if (priv->vif && (priv->iw_mode == NL80211_IFTYPE_ADHOC)) {
5711 struct sk_buff *beacon = ieee80211_beacon_get(priv->hw,
5712 priv->vif);
5713 if (beacon)
5714 iwl3945_mac_beacon_update(priv->hw, beacon);
5715 }
5716
Zhu Yib481de92007-09-25 17:54:57 -07005717 return;
5718
5719 restart:
5720 queue_work(priv->workqueue, &priv->restart);
5721}
5722
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005723static void iwl3945_cancel_deferred_work(struct iwl3945_priv *priv);
Zhu Yib481de92007-09-25 17:54:57 -07005724
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005725static void __iwl3945_down(struct iwl3945_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005726{
5727 unsigned long flags;
5728 int exit_pending = test_bit(STATUS_EXIT_PENDING, &priv->status);
5729 struct ieee80211_conf *conf = NULL;
5730
5731 IWL_DEBUG_INFO(DRV_NAME " is going down\n");
5732
5733 conf = ieee80211_get_hw_conf(priv->hw);
5734
5735 if (!exit_pending)
5736 set_bit(STATUS_EXIT_PENDING, &priv->status);
5737
Mohamed Abbasab53d8a2008-03-25 16:33:36 -07005738 iwl3945_led_unregister(priv);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005739 iwl3945_clear_stations_table(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005740
5741 /* Unblock any waiting calls */
5742 wake_up_interruptible_all(&priv->wait_command_queue);
5743
Zhu Yib481de92007-09-25 17:54:57 -07005744 /* Wipe out the EXIT_PENDING status bit if we are not actually
5745 * exiting the module */
5746 if (!exit_pending)
5747 clear_bit(STATUS_EXIT_PENDING, &priv->status);
5748
5749 /* stop and reset the on-board processor */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005750 iwl3945_write32(priv, CSR_RESET, CSR_RESET_REG_FLAG_NEVO_RESET);
Zhu Yib481de92007-09-25 17:54:57 -07005751
5752 /* tell the device to stop sending interrupts */
Mohamed Abbas0359fac2008-03-28 16:21:08 -07005753 spin_lock_irqsave(&priv->lock, flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005754 iwl3945_disable_interrupts(priv);
Mohamed Abbas0359fac2008-03-28 16:21:08 -07005755 spin_unlock_irqrestore(&priv->lock, flags);
5756 iwl_synchronize_irq(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005757
5758 if (priv->mac80211_registered)
5759 ieee80211_stop_queues(priv->hw);
5760
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005761 /* If we have not previously called iwl3945_init() then
Zhu Yib481de92007-09-25 17:54:57 -07005762 * clear all bits but the RF Kill and SUSPEND bits and return */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005763 if (!iwl3945_is_init(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07005764 priv->status = test_bit(STATUS_RF_KILL_HW, &priv->status) <<
5765 STATUS_RF_KILL_HW |
5766 test_bit(STATUS_RF_KILL_SW, &priv->status) <<
5767 STATUS_RF_KILL_SW |
Reinette Chatre97888642008-02-06 11:20:38 -08005768 test_bit(STATUS_GEO_CONFIGURED, &priv->status) <<
5769 STATUS_GEO_CONFIGURED |
Zhu Yib481de92007-09-25 17:54:57 -07005770 test_bit(STATUS_IN_SUSPEND, &priv->status) <<
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08005771 STATUS_IN_SUSPEND |
5772 test_bit(STATUS_EXIT_PENDING, &priv->status) <<
5773 STATUS_EXIT_PENDING;
Zhu Yib481de92007-09-25 17:54:57 -07005774 goto exit;
5775 }
5776
5777 /* ...otherwise clear out all the status bits but the RF Kill and
5778 * SUSPEND bits and continue taking the NIC down. */
5779 priv->status &= test_bit(STATUS_RF_KILL_HW, &priv->status) <<
5780 STATUS_RF_KILL_HW |
5781 test_bit(STATUS_RF_KILL_SW, &priv->status) <<
5782 STATUS_RF_KILL_SW |
Reinette Chatre97888642008-02-06 11:20:38 -08005783 test_bit(STATUS_GEO_CONFIGURED, &priv->status) <<
5784 STATUS_GEO_CONFIGURED |
Zhu Yib481de92007-09-25 17:54:57 -07005785 test_bit(STATUS_IN_SUSPEND, &priv->status) <<
5786 STATUS_IN_SUSPEND |
5787 test_bit(STATUS_FW_ERROR, &priv->status) <<
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08005788 STATUS_FW_ERROR |
5789 test_bit(STATUS_EXIT_PENDING, &priv->status) <<
5790 STATUS_EXIT_PENDING;
Zhu Yib481de92007-09-25 17:54:57 -07005791
5792 spin_lock_irqsave(&priv->lock, flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005793 iwl3945_clear_bit(priv, CSR_GP_CNTRL, CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
Zhu Yib481de92007-09-25 17:54:57 -07005794 spin_unlock_irqrestore(&priv->lock, flags);
5795
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005796 iwl3945_hw_txq_ctx_stop(priv);
5797 iwl3945_hw_rxq_stop(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005798
5799 spin_lock_irqsave(&priv->lock, flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005800 if (!iwl3945_grab_nic_access(priv)) {
5801 iwl3945_write_prph(priv, APMG_CLK_DIS_REG,
Zhu Yib481de92007-09-25 17:54:57 -07005802 APMG_CLK_VAL_DMA_CLK_RQT);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005803 iwl3945_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005804 }
5805 spin_unlock_irqrestore(&priv->lock, flags);
5806
5807 udelay(5);
5808
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005809 iwl3945_hw_nic_stop_master(priv);
5810 iwl3945_set_bit(priv, CSR_RESET, CSR_RESET_REG_FLAG_SW_RESET);
5811 iwl3945_hw_nic_reset(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005812
5813 exit:
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08005814 memset(&priv->card_alive, 0, sizeof(struct iwl_alive_resp));
Zhu Yib481de92007-09-25 17:54:57 -07005815
5816 if (priv->ibss_beacon)
5817 dev_kfree_skb(priv->ibss_beacon);
5818 priv->ibss_beacon = NULL;
5819
5820 /* clear out any free frames */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005821 iwl3945_clear_free_frames(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005822}
5823
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005824static void iwl3945_down(struct iwl3945_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005825{
5826 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005827 __iwl3945_down(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005828 mutex_unlock(&priv->mutex);
Zhu Yib24d22b2007-12-19 13:59:52 +08005829
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005830 iwl3945_cancel_deferred_work(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005831}
5832
5833#define MAX_HW_RESTARTS 5
5834
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005835static int __iwl3945_up(struct iwl3945_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005836{
5837 int rc, i;
5838
5839 if (test_bit(STATUS_EXIT_PENDING, &priv->status)) {
5840 IWL_WARNING("Exit pending; will not bring the NIC up\n");
5841 return -EIO;
5842 }
5843
5844 if (test_bit(STATUS_RF_KILL_SW, &priv->status)) {
5845 IWL_WARNING("Radio disabled by SW RF kill (module "
5846 "parameter)\n");
Zhu Yie655b9f2008-01-24 02:19:38 -08005847 return -ENODEV;
5848 }
5849
Reinette Chatree903fbd2008-01-30 22:05:15 -08005850 if (!priv->ucode_data_backup.v_addr || !priv->ucode_data.v_addr) {
Tomas Winklera96a27f2008-10-23 23:48:56 -07005851 IWL_ERROR("ucode not available for device bring up\n");
Reinette Chatree903fbd2008-01-30 22:05:15 -08005852 return -EIO;
5853 }
5854
Zhu Yie655b9f2008-01-24 02:19:38 -08005855 /* If platform's RF_KILL switch is NOT set to KILL */
5856 if (iwl3945_read32(priv, CSR_GP_CNTRL) &
5857 CSR_GP_CNTRL_REG_FLAG_HW_RF_KILL_SW)
5858 clear_bit(STATUS_RF_KILL_HW, &priv->status);
5859 else {
5860 set_bit(STATUS_RF_KILL_HW, &priv->status);
5861 if (!test_bit(STATUS_IN_SUSPEND, &priv->status)) {
5862 IWL_WARNING("Radio disabled by HW RF Kill switch\n");
5863 return -ENODEV;
5864 }
Zhu Yib481de92007-09-25 17:54:57 -07005865 }
Adel Gadllah80fcc9e2008-07-01 17:49:50 +02005866
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005867 iwl3945_write32(priv, CSR_INT, 0xFFFFFFFF);
Zhu Yib481de92007-09-25 17:54:57 -07005868
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005869 rc = iwl3945_hw_nic_init(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005870 if (rc) {
5871 IWL_ERROR("Unable to int nic\n");
5872 return rc;
5873 }
5874
5875 /* make sure rfkill handshake bits are cleared */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005876 iwl3945_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
5877 iwl3945_write32(priv, CSR_UCODE_DRV_GP1_CLR,
Zhu Yib481de92007-09-25 17:54:57 -07005878 CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
5879
5880 /* clear (again), then enable host interrupts */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005881 iwl3945_write32(priv, CSR_INT, 0xFFFFFFFF);
5882 iwl3945_enable_interrupts(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005883
5884 /* really make sure rfkill handshake bits are cleared */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005885 iwl3945_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
5886 iwl3945_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
Zhu Yib481de92007-09-25 17:54:57 -07005887
5888 /* Copy original ucode data image from disk into backup cache.
5889 * This will be used to initialize the on-board processor's
5890 * data SRAM for a clean start when the runtime program first loads. */
5891 memcpy(priv->ucode_data_backup.v_addr, priv->ucode_data.v_addr,
Zhu Yi5a669262008-01-14 17:46:18 -08005892 priv->ucode_data.len);
Zhu Yib481de92007-09-25 17:54:57 -07005893
Zhu Yie655b9f2008-01-24 02:19:38 -08005894 /* We return success when we resume from suspend and rf_kill is on. */
5895 if (test_bit(STATUS_RF_KILL_HW, &priv->status))
5896 return 0;
5897
Zhu Yib481de92007-09-25 17:54:57 -07005898 for (i = 0; i < MAX_HW_RESTARTS; i++) {
5899
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005900 iwl3945_clear_stations_table(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005901
5902 /* load bootstrap state machine,
5903 * load bootstrap program into processor's memory,
5904 * prepare to load the "initialize" uCode */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005905 rc = iwl3945_load_bsm(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005906
5907 if (rc) {
5908 IWL_ERROR("Unable to set up bootstrap uCode: %d\n", rc);
5909 continue;
5910 }
5911
5912 /* start card; "initialize" will load runtime ucode */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005913 iwl3945_nic_start(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005914
Zhu Yib481de92007-09-25 17:54:57 -07005915 IWL_DEBUG_INFO(DRV_NAME " is coming up\n");
5916
5917 return 0;
5918 }
5919
5920 set_bit(STATUS_EXIT_PENDING, &priv->status);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005921 __iwl3945_down(priv);
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08005922 clear_bit(STATUS_EXIT_PENDING, &priv->status);
Zhu Yib481de92007-09-25 17:54:57 -07005923
5924 /* tried to restart and config the device for as long as our
5925 * patience could withstand */
5926 IWL_ERROR("Unable to initialize device after %d attempts.\n", i);
5927 return -EIO;
5928}
5929
5930
5931/*****************************************************************************
5932 *
5933 * Workqueue callbacks
5934 *
5935 *****************************************************************************/
5936
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005937static void iwl3945_bg_init_alive_start(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07005938{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005939 struct iwl3945_priv *priv =
5940 container_of(data, struct iwl3945_priv, init_alive_start.work);
Zhu Yib481de92007-09-25 17:54:57 -07005941
5942 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
5943 return;
5944
5945 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005946 iwl3945_init_alive_start(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005947 mutex_unlock(&priv->mutex);
5948}
5949
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005950static void iwl3945_bg_alive_start(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07005951{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005952 struct iwl3945_priv *priv =
5953 container_of(data, struct iwl3945_priv, alive_start.work);
Zhu Yib481de92007-09-25 17:54:57 -07005954
5955 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
5956 return;
5957
5958 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005959 iwl3945_alive_start(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005960 mutex_unlock(&priv->mutex);
5961}
5962
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005963static void iwl3945_bg_rf_kill(struct work_struct *work)
Zhu Yib481de92007-09-25 17:54:57 -07005964{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005965 struct iwl3945_priv *priv = container_of(work, struct iwl3945_priv, rf_kill);
Zhu Yib481de92007-09-25 17:54:57 -07005966
5967 wake_up_interruptible(&priv->wait_command_queue);
5968
5969 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
5970 return;
5971
5972 mutex_lock(&priv->mutex);
5973
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005974 if (!iwl3945_is_rfkill(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07005975 IWL_DEBUG(IWL_DL_INFO | IWL_DL_RF_KILL,
5976 "HW and/or SW RF Kill no longer active, restarting "
5977 "device\n");
5978 if (!test_bit(STATUS_EXIT_PENDING, &priv->status))
5979 queue_work(priv->workqueue, &priv->restart);
5980 } else {
5981
5982 if (!test_bit(STATUS_RF_KILL_HW, &priv->status))
5983 IWL_DEBUG_RF_KILL("Can not turn radio back on - "
5984 "disabled by SW switch\n");
5985 else
5986 IWL_WARNING("Radio Frequency Kill Switch is On:\n"
5987 "Kill switch must be turned off for "
5988 "wireless networking to work.\n");
5989 }
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08005990
Zhu Yib481de92007-09-25 17:54:57 -07005991 mutex_unlock(&priv->mutex);
Adel Gadllah80fcc9e2008-07-01 17:49:50 +02005992 iwl3945_rfkill_set_hw_state(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005993}
5994
5995#define IWL_SCAN_CHECK_WATCHDOG (7 * HZ)
5996
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005997static void iwl3945_bg_scan_check(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07005998{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005999 struct iwl3945_priv *priv =
6000 container_of(data, struct iwl3945_priv, scan_check.work);
Zhu Yib481de92007-09-25 17:54:57 -07006001
6002 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
6003 return;
6004
6005 mutex_lock(&priv->mutex);
6006 if (test_bit(STATUS_SCANNING, &priv->status) ||
6007 test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
6008 IWL_DEBUG(IWL_DL_INFO | IWL_DL_SCAN,
6009 "Scan completion watchdog resetting adapter (%dms)\n",
6010 jiffies_to_msecs(IWL_SCAN_CHECK_WATCHDOG));
Mohamed Abbas15e869d2007-10-25 17:15:46 +08006011
Zhu Yib481de92007-09-25 17:54:57 -07006012 if (!test_bit(STATUS_EXIT_PENDING, &priv->status))
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006013 iwl3945_send_scan_abort(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006014 }
6015 mutex_unlock(&priv->mutex);
6016}
6017
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006018static void iwl3945_bg_request_scan(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07006019{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006020 struct iwl3945_priv *priv =
6021 container_of(data, struct iwl3945_priv, request_scan);
6022 struct iwl3945_host_cmd cmd = {
Zhu Yib481de92007-09-25 17:54:57 -07006023 .id = REPLY_SCAN_CMD,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006024 .len = sizeof(struct iwl3945_scan_cmd),
Zhu Yib481de92007-09-25 17:54:57 -07006025 .meta.flags = CMD_SIZE_HUGE,
6026 };
6027 int rc = 0;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006028 struct iwl3945_scan_cmd *scan;
Zhu Yib481de92007-09-25 17:54:57 -07006029 struct ieee80211_conf *conf = NULL;
Abhijeet Kolekarf9340522008-09-03 11:26:58 +08006030 u8 n_probes = 2;
Johannes Berg8318d782008-01-24 19:38:38 +01006031 enum ieee80211_band band;
John W. Linville9387b7c2008-09-30 20:59:05 -04006032 DECLARE_SSID_BUF(ssid);
Zhu Yib481de92007-09-25 17:54:57 -07006033
6034 conf = ieee80211_get_hw_conf(priv->hw);
6035
6036 mutex_lock(&priv->mutex);
6037
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006038 if (!iwl3945_is_ready(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07006039 IWL_WARNING("request scan called when driver not ready.\n");
6040 goto done;
6041 }
6042
Tomas Winklera96a27f2008-10-23 23:48:56 -07006043 /* Make sure the scan wasn't canceled before this queued work
Zhu Yib481de92007-09-25 17:54:57 -07006044 * was given the chance to run... */
6045 if (!test_bit(STATUS_SCANNING, &priv->status))
6046 goto done;
6047
6048 /* This should never be called or scheduled if there is currently
6049 * a scan active in the hardware. */
6050 if (test_bit(STATUS_SCAN_HW, &priv->status)) {
6051 IWL_DEBUG_INFO("Multiple concurrent scan requests in parallel. "
6052 "Ignoring second request.\n");
6053 rc = -EIO;
6054 goto done;
6055 }
6056
6057 if (test_bit(STATUS_EXIT_PENDING, &priv->status)) {
6058 IWL_DEBUG_SCAN("Aborting scan due to device shutdown\n");
6059 goto done;
6060 }
6061
6062 if (test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
6063 IWL_DEBUG_HC("Scan request while abort pending. Queuing.\n");
6064 goto done;
6065 }
6066
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006067 if (iwl3945_is_rfkill(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07006068 IWL_DEBUG_HC("Aborting scan due to RF Kill activation\n");
6069 goto done;
6070 }
6071
6072 if (!test_bit(STATUS_READY, &priv->status)) {
6073 IWL_DEBUG_HC("Scan request while uninitialized. Queuing.\n");
6074 goto done;
6075 }
6076
6077 if (!priv->scan_bands) {
6078 IWL_DEBUG_HC("Aborting scan due to no requested bands\n");
6079 goto done;
6080 }
6081
6082 if (!priv->scan) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006083 priv->scan = kmalloc(sizeof(struct iwl3945_scan_cmd) +
Zhu Yib481de92007-09-25 17:54:57 -07006084 IWL_MAX_SCAN_SIZE, GFP_KERNEL);
6085 if (!priv->scan) {
6086 rc = -ENOMEM;
6087 goto done;
6088 }
6089 }
6090 scan = priv->scan;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006091 memset(scan, 0, sizeof(struct iwl3945_scan_cmd) + IWL_MAX_SCAN_SIZE);
Zhu Yib481de92007-09-25 17:54:57 -07006092
6093 scan->quiet_plcp_th = IWL_PLCP_QUIET_THRESH;
6094 scan->quiet_time = IWL_ACTIVE_QUIET_TIME;
6095
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006096 if (iwl3945_is_associated(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07006097 u16 interval = 0;
6098 u32 extra;
6099 u32 suspend_time = 100;
6100 u32 scan_suspend_time = 100;
6101 unsigned long flags;
6102
6103 IWL_DEBUG_INFO("Scanning while associated...\n");
6104
6105 spin_lock_irqsave(&priv->lock, flags);
6106 interval = priv->beacon_int;
6107 spin_unlock_irqrestore(&priv->lock, flags);
6108
6109 scan->suspend_time = 0;
Mohamed Abbas15e869d2007-10-25 17:15:46 +08006110 scan->max_out_time = cpu_to_le32(200 * 1024);
Zhu Yib481de92007-09-25 17:54:57 -07006111 if (!interval)
6112 interval = suspend_time;
6113 /*
6114 * suspend time format:
6115 * 0-19: beacon interval in usec (time before exec.)
6116 * 20-23: 0
6117 * 24-31: number of beacons (suspend between channels)
6118 */
6119
6120 extra = (suspend_time / interval) << 24;
6121 scan_suspend_time = 0xFF0FFFFF &
6122 (extra | ((suspend_time % interval) * 1024));
6123
6124 scan->suspend_time = cpu_to_le32(scan_suspend_time);
6125 IWL_DEBUG_SCAN("suspend_time 0x%X beacon interval %d\n",
6126 scan_suspend_time, interval);
6127 }
6128
6129 /* We should add the ability for user to lock to PASSIVE ONLY */
6130 if (priv->one_direct_scan) {
6131 IWL_DEBUG_SCAN
6132 ("Kicking off one direct scan for '%s'\n",
John W. Linville9387b7c2008-09-30 20:59:05 -04006133 print_ssid(ssid, priv->direct_ssid,
6134 priv->direct_ssid_len));
Zhu Yib481de92007-09-25 17:54:57 -07006135 scan->direct_scan[0].id = WLAN_EID_SSID;
6136 scan->direct_scan[0].len = priv->direct_ssid_len;
6137 memcpy(scan->direct_scan[0].ssid,
6138 priv->direct_ssid, priv->direct_ssid_len);
Abhijeet Kolekarf9340522008-09-03 11:26:58 +08006139 n_probes++;
Abhijeet Kolekarf9340522008-09-03 11:26:58 +08006140 } else
Bill Moss786b4552008-04-17 16:03:40 -07006141 IWL_DEBUG_SCAN("Kicking off one indirect scan.\n");
Zhu Yib481de92007-09-25 17:54:57 -07006142
6143 /* We don't build a direct scan probe request; the uCode will do
6144 * that based on the direct_mask added to each channel entry */
6145 scan->tx_cmd.len = cpu_to_le16(
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006146 iwl3945_fill_probe_req(priv, (struct ieee80211_mgmt *)scan->data,
Johannes Berg430cfe92008-10-28 18:06:02 +01006147 IWL_MAX_SCAN_SIZE - sizeof(*scan)));
Zhu Yib481de92007-09-25 17:54:57 -07006148 scan->tx_cmd.tx_flags = TX_CMD_FLG_SEQ_CTL_MSK;
6149 scan->tx_cmd.sta_id = priv->hw_setting.bcast_sta_id;
6150 scan->tx_cmd.stop_time.life_time = TX_CMD_LIFE_TIME_INFINITE;
6151
6152 /* flags + rate selection */
6153
Ron Rindjunsky66b50042008-06-25 16:46:31 +08006154 if (priv->scan_bands & BIT(IEEE80211_BAND_2GHZ)) {
Zhu Yib481de92007-09-25 17:54:57 -07006155 scan->flags = RXON_FLG_BAND_24G_MSK | RXON_FLG_AUTO_DETECT_MSK;
6156 scan->tx_cmd.rate = IWL_RATE_1M_PLCP;
6157 scan->good_CRC_th = 0;
Johannes Berg8318d782008-01-24 19:38:38 +01006158 band = IEEE80211_BAND_2GHZ;
Ron Rindjunsky66b50042008-06-25 16:46:31 +08006159 } else if (priv->scan_bands & BIT(IEEE80211_BAND_5GHZ)) {
Zhu Yib481de92007-09-25 17:54:57 -07006160 scan->tx_cmd.rate = IWL_RATE_6M_PLCP;
6161 scan->good_CRC_th = IWL_GOOD_CRC_TH;
Johannes Berg8318d782008-01-24 19:38:38 +01006162 band = IEEE80211_BAND_5GHZ;
Ron Rindjunsky66b50042008-06-25 16:46:31 +08006163 } else {
Zhu Yib481de92007-09-25 17:54:57 -07006164 IWL_WARNING("Invalid scan band count\n");
6165 goto done;
6166 }
6167
6168 /* select Rx antennas */
6169 scan->flags |= iwl3945_get_antenna_flags(priv);
6170
Johannes Berg05c914f2008-09-11 00:01:58 +02006171 if (priv->iw_mode == NL80211_IFTYPE_MONITOR)
Zhu Yib481de92007-09-25 17:54:57 -07006172 scan->filter_flags = RXON_FILTER_PROMISC_MSK;
6173
Abhijeet Kolekarf9340522008-09-03 11:26:58 +08006174 scan->channel_count =
6175 iwl3945_get_channels_for_scan(priv, band, 1, /* active */
6176 n_probes,
6177 (void *)&scan->data[le16_to_cpu(scan->tx_cmd.len)]);
Zhu Yib481de92007-09-25 17:54:57 -07006178
Reinette Chatre14b54332008-11-04 12:21:35 -08006179 if (scan->channel_count == 0) {
6180 IWL_DEBUG_SCAN("channel count %d\n", scan->channel_count);
6181 goto done;
6182 }
6183
Zhu Yib481de92007-09-25 17:54:57 -07006184 cmd.len += le16_to_cpu(scan->tx_cmd.len) +
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006185 scan->channel_count * sizeof(struct iwl3945_scan_channel);
Zhu Yib481de92007-09-25 17:54:57 -07006186 cmd.data = scan;
6187 scan->len = cpu_to_le16(cmd.len);
6188
6189 set_bit(STATUS_SCAN_HW, &priv->status);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006190 rc = iwl3945_send_cmd_sync(priv, &cmd);
Zhu Yib481de92007-09-25 17:54:57 -07006191 if (rc)
6192 goto done;
6193
6194 queue_delayed_work(priv->workqueue, &priv->scan_check,
6195 IWL_SCAN_CHECK_WATCHDOG);
6196
6197 mutex_unlock(&priv->mutex);
6198 return;
6199
6200 done:
Mohamed Abbas2420ebc2008-11-04 12:21:34 -08006201 /* can not perform scan make sure we clear scanning
6202 * bits from status so next scan request can be performed.
6203 * if we dont clear scanning status bit here all next scan
6204 * will fail
6205 */
6206 clear_bit(STATUS_SCAN_HW, &priv->status);
6207 clear_bit(STATUS_SCANNING, &priv->status);
6208
Ian Schram01ebd062007-10-25 17:15:22 +08006209 /* inform mac80211 scan aborted */
Zhu Yib481de92007-09-25 17:54:57 -07006210 queue_work(priv->workqueue, &priv->scan_completed);
6211 mutex_unlock(&priv->mutex);
6212}
6213
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006214static void iwl3945_bg_up(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07006215{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006216 struct iwl3945_priv *priv = container_of(data, struct iwl3945_priv, up);
Zhu Yib481de92007-09-25 17:54:57 -07006217
6218 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
6219 return;
6220
6221 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006222 __iwl3945_up(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006223 mutex_unlock(&priv->mutex);
Adel Gadllah80fcc9e2008-07-01 17:49:50 +02006224 iwl3945_rfkill_set_hw_state(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006225}
6226
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006227static void iwl3945_bg_restart(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07006228{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006229 struct iwl3945_priv *priv = container_of(data, struct iwl3945_priv, restart);
Zhu Yib481de92007-09-25 17:54:57 -07006230
6231 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
6232 return;
6233
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006234 iwl3945_down(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006235 queue_work(priv->workqueue, &priv->up);
6236}
6237
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006238static void iwl3945_bg_rx_replenish(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07006239{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006240 struct iwl3945_priv *priv =
6241 container_of(data, struct iwl3945_priv, rx_replenish);
Zhu Yib481de92007-09-25 17:54:57 -07006242
6243 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
6244 return;
6245
6246 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006247 iwl3945_rx_replenish(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006248 mutex_unlock(&priv->mutex);
6249}
6250
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08006251#define IWL_DELAY_NEXT_SCAN (HZ*2)
6252
Abhijeet Kolekarcd56d332008-09-03 11:26:21 +08006253static void iwl3945_post_associate(struct iwl3945_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07006254{
Zhu Yib481de92007-09-25 17:54:57 -07006255 int rc = 0;
6256 struct ieee80211_conf *conf = NULL;
6257
Johannes Berg05c914f2008-09-11 00:01:58 +02006258 if (priv->iw_mode == NL80211_IFTYPE_AP) {
Tomas Winkler3ac7f142008-07-21 02:40:14 +03006259 IWL_ERROR("%s Should not be called in AP mode\n", __func__);
Zhu Yib481de92007-09-25 17:54:57 -07006260 return;
6261 }
6262
6263
Johannes Berge1749612008-10-27 15:59:26 -07006264 IWL_DEBUG_ASSOC("Associated as %d to: %pM\n",
6265 priv->assoc_id, priv->active_rxon.bssid_addr);
Zhu Yib481de92007-09-25 17:54:57 -07006266
6267 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
6268 return;
6269
Abhijeet Kolekar322a9812008-09-03 11:26:27 +08006270 if (!priv->vif || !priv->is_open)
Mohamed Abbas6ef89d02007-10-25 17:15:47 +08006271 return;
Abhijeet Kolekar322a9812008-09-03 11:26:27 +08006272
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006273 iwl3945_scan_cancel_timeout(priv, 200);
Mohamed Abbas15e869d2007-10-25 17:15:46 +08006274
Zhu Yib481de92007-09-25 17:54:57 -07006275 conf = ieee80211_get_hw_conf(priv->hw);
6276
6277 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006278 iwl3945_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006279
Tomas Winkler28afaf92008-12-19 10:37:06 +08006280 memset(&priv->rxon_timing, 0, sizeof(struct iwl_rxon_time_cmd));
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006281 iwl3945_setup_rxon_timing(priv);
6282 rc = iwl3945_send_cmd_pdu(priv, REPLY_RXON_TIMING,
Zhu Yib481de92007-09-25 17:54:57 -07006283 sizeof(priv->rxon_timing), &priv->rxon_timing);
6284 if (rc)
6285 IWL_WARNING("REPLY_RXON_TIMING failed - "
6286 "Attempting to continue.\n");
6287
6288 priv->staging_rxon.filter_flags |= RXON_FILTER_ASSOC_MSK;
6289
6290 priv->staging_rxon.assoc_id = cpu_to_le16(priv->assoc_id);
6291
6292 IWL_DEBUG_ASSOC("assoc id %d beacon interval %d\n",
6293 priv->assoc_id, priv->beacon_int);
6294
6295 if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_PREAMBLE)
6296 priv->staging_rxon.flags |= RXON_FLG_SHORT_PREAMBLE_MSK;
6297 else
6298 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_PREAMBLE_MSK;
6299
6300 if (priv->staging_rxon.flags & RXON_FLG_BAND_24G_MSK) {
6301 if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_SLOT_TIME)
6302 priv->staging_rxon.flags |= RXON_FLG_SHORT_SLOT_MSK;
6303 else
6304 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
6305
Johannes Berg05c914f2008-09-11 00:01:58 +02006306 if (priv->iw_mode == NL80211_IFTYPE_ADHOC)
Zhu Yib481de92007-09-25 17:54:57 -07006307 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
6308
6309 }
6310
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006311 iwl3945_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006312
6313 switch (priv->iw_mode) {
Johannes Berg05c914f2008-09-11 00:01:58 +02006314 case NL80211_IFTYPE_STATION:
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006315 iwl3945_rate_scale_init(priv->hw, IWL_AP_ID);
Zhu Yib481de92007-09-25 17:54:57 -07006316 break;
6317
Johannes Berg05c914f2008-09-11 00:01:58 +02006318 case NL80211_IFTYPE_ADHOC:
Zhu Yib481de92007-09-25 17:54:57 -07006319
Abhijeet Kolekarce546fd2008-11-19 15:32:22 -08006320 priv->assoc_id = 1;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006321 iwl3945_add_station(priv, priv->bssid, 0, 0);
Zhu Yib481de92007-09-25 17:54:57 -07006322 iwl3945_sync_sta(priv, IWL_STA_ID,
Johannes Berg8318d782008-01-24 19:38:38 +01006323 (priv->band == IEEE80211_BAND_5GHZ) ?
Zhu Yib481de92007-09-25 17:54:57 -07006324 IWL_RATE_6M_PLCP : IWL_RATE_1M_PLCP,
6325 CMD_ASYNC);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006326 iwl3945_rate_scale_init(priv->hw, IWL_STA_ID);
6327 iwl3945_send_beacon_cmd(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006328
6329 break;
6330
6331 default:
6332 IWL_ERROR("%s Should not be called in %d mode\n",
Tomas Winkler3ac7f142008-07-21 02:40:14 +03006333 __func__, priv->iw_mode);
Zhu Yib481de92007-09-25 17:54:57 -07006334 break;
6335 }
6336
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006337 iwl3945_activate_qos(priv, 0);
Ron Rindjunsky292ae172008-02-06 11:20:39 -08006338
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08006339 /* we have just associated, don't start scan too early */
6340 priv->next_scan_jiffies = jiffies + IWL_DELAY_NEXT_SCAN;
Abhijeet Kolekarcd56d332008-09-03 11:26:21 +08006341}
6342
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006343static void iwl3945_bg_abort_scan(struct work_struct *work)
Zhu Yib481de92007-09-25 17:54:57 -07006344{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006345 struct iwl3945_priv *priv = container_of(work, struct iwl3945_priv, abort_scan);
Zhu Yib481de92007-09-25 17:54:57 -07006346
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006347 if (!iwl3945_is_ready(priv))
Zhu Yib481de92007-09-25 17:54:57 -07006348 return;
6349
6350 mutex_lock(&priv->mutex);
6351
6352 set_bit(STATUS_SCAN_ABORTING, &priv->status);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006353 iwl3945_send_scan_abort(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006354
6355 mutex_unlock(&priv->mutex);
6356}
6357
Johannes Berge8975582008-10-09 12:18:51 +02006358static int iwl3945_mac_config(struct ieee80211_hw *hw, u32 changed);
Zhu Yi76bb77e2007-11-22 10:53:22 +08006359
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006360static void iwl3945_bg_scan_completed(struct work_struct *work)
Zhu Yib481de92007-09-25 17:54:57 -07006361{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006362 struct iwl3945_priv *priv =
6363 container_of(work, struct iwl3945_priv, scan_completed);
Zhu Yib481de92007-09-25 17:54:57 -07006364
6365 IWL_DEBUG(IWL_DL_INFO | IWL_DL_SCAN, "SCAN complete scan\n");
6366
6367 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
6368 return;
6369
Zhu Yia0646472007-12-20 14:10:01 +08006370 if (test_bit(STATUS_CONF_PENDING, &priv->status))
Johannes Berge8975582008-10-09 12:18:51 +02006371 iwl3945_mac_config(priv->hw, 0);
Zhu Yi76bb77e2007-11-22 10:53:22 +08006372
Zhu Yib481de92007-09-25 17:54:57 -07006373 ieee80211_scan_completed(priv->hw);
6374
6375 /* Since setting the TXPOWER may have been deferred while
6376 * performing the scan, fire one off */
6377 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006378 iwl3945_hw_reg_send_txpower(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006379 mutex_unlock(&priv->mutex);
6380}
6381
6382/*****************************************************************************
6383 *
6384 * mac80211 entry point functions
6385 *
6386 *****************************************************************************/
6387
Zhu Yi5a669262008-01-14 17:46:18 -08006388#define UCODE_READY_TIMEOUT (2 * HZ)
6389
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006390static int iwl3945_mac_start(struct ieee80211_hw *hw)
Zhu Yib481de92007-09-25 17:54:57 -07006391{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006392 struct iwl3945_priv *priv = hw->priv;
Zhu Yi5a669262008-01-14 17:46:18 -08006393 int ret;
Zhu Yib481de92007-09-25 17:54:57 -07006394
6395 IWL_DEBUG_MAC80211("enter\n");
6396
Zhu Yi5a669262008-01-14 17:46:18 -08006397 if (pci_enable_device(priv->pci_dev)) {
6398 IWL_ERROR("Fail to pci_enable_device\n");
6399 return -ENODEV;
6400 }
6401 pci_restore_state(priv->pci_dev);
6402 pci_enable_msi(priv->pci_dev);
6403
6404 ret = request_irq(priv->pci_dev->irq, iwl3945_isr, IRQF_SHARED,
6405 DRV_NAME, priv);
6406 if (ret) {
6407 IWL_ERROR("Error allocating IRQ %d\n", priv->pci_dev->irq);
6408 goto out_disable_msi;
6409 }
6410
Zhu Yib481de92007-09-25 17:54:57 -07006411 /* we should be verifying the device is ready to be opened */
6412 mutex_lock(&priv->mutex);
6413
Zhu Yi5a669262008-01-14 17:46:18 -08006414 memset(&priv->staging_rxon, 0, sizeof(struct iwl3945_rxon_cmd));
6415 /* fetch ucode file from disk, alloc and copy to bus-master buffers ...
6416 * ucode filename and max sizes are card-specific. */
6417
6418 if (!priv->ucode_code.len) {
6419 ret = iwl3945_read_ucode(priv);
6420 if (ret) {
6421 IWL_ERROR("Could not read microcode: %d\n", ret);
6422 mutex_unlock(&priv->mutex);
6423 goto out_release_irq;
6424 }
6425 }
6426
Zhu Yie655b9f2008-01-24 02:19:38 -08006427 ret = __iwl3945_up(priv);
Zhu Yi5a669262008-01-14 17:46:18 -08006428
Zhu Yib481de92007-09-25 17:54:57 -07006429 mutex_unlock(&priv->mutex);
Zhu Yi5a669262008-01-14 17:46:18 -08006430
Adel Gadllah80fcc9e2008-07-01 17:49:50 +02006431 iwl3945_rfkill_set_hw_state(priv);
6432
Zhu Yie655b9f2008-01-24 02:19:38 -08006433 if (ret)
6434 goto out_release_irq;
6435
6436 IWL_DEBUG_INFO("Start UP work.\n");
6437
6438 if (test_bit(STATUS_IN_SUSPEND, &priv->status))
6439 return 0;
6440
Zhu Yi5a669262008-01-14 17:46:18 -08006441 /* Wait for START_ALIVE from ucode. Otherwise callbacks from
6442 * mac80211 will not be run successfully. */
6443 ret = wait_event_interruptible_timeout(priv->wait_command_queue,
6444 test_bit(STATUS_READY, &priv->status),
6445 UCODE_READY_TIMEOUT);
6446 if (!ret) {
6447 if (!test_bit(STATUS_READY, &priv->status)) {
6448 IWL_ERROR("Wait for START_ALIVE timeout after %dms.\n",
6449 jiffies_to_msecs(UCODE_READY_TIMEOUT));
6450 ret = -ETIMEDOUT;
6451 goto out_release_irq;
6452 }
6453 }
6454
Zhu Yie655b9f2008-01-24 02:19:38 -08006455 priv->is_open = 1;
Zhu Yib481de92007-09-25 17:54:57 -07006456 IWL_DEBUG_MAC80211("leave\n");
6457 return 0;
Zhu Yi5a669262008-01-14 17:46:18 -08006458
6459out_release_irq:
6460 free_irq(priv->pci_dev->irq, priv);
6461out_disable_msi:
6462 pci_disable_msi(priv->pci_dev);
Zhu Yie655b9f2008-01-24 02:19:38 -08006463 pci_disable_device(priv->pci_dev);
6464 priv->is_open = 0;
6465 IWL_DEBUG_MAC80211("leave - failed\n");
Zhu Yi5a669262008-01-14 17:46:18 -08006466 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07006467}
6468
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006469static void iwl3945_mac_stop(struct ieee80211_hw *hw)
Zhu Yib481de92007-09-25 17:54:57 -07006470{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006471 struct iwl3945_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07006472
6473 IWL_DEBUG_MAC80211("enter\n");
Mohamed Abbas6ef89d02007-10-25 17:15:47 +08006474
Zhu Yie655b9f2008-01-24 02:19:38 -08006475 if (!priv->is_open) {
6476 IWL_DEBUG_MAC80211("leave - skip\n");
6477 return;
6478 }
6479
Zhu Yib481de92007-09-25 17:54:57 -07006480 priv->is_open = 0;
Zhu Yi5a669262008-01-14 17:46:18 -08006481
6482 if (iwl3945_is_ready_rf(priv)) {
Zhu Yie655b9f2008-01-24 02:19:38 -08006483 /* stop mac, cancel any scan request and clear
6484 * RXON_FILTER_ASSOC_MSK BIT
6485 */
Zhu Yi5a669262008-01-14 17:46:18 -08006486 mutex_lock(&priv->mutex);
6487 iwl3945_scan_cancel_timeout(priv, 100);
Mohamed Abbasfde35712007-11-29 11:10:15 +08006488 mutex_unlock(&priv->mutex);
Mohamed Abbasfde35712007-11-29 11:10:15 +08006489 }
6490
Zhu Yi5a669262008-01-14 17:46:18 -08006491 iwl3945_down(priv);
6492
6493 flush_workqueue(priv->workqueue);
6494 free_irq(priv->pci_dev->irq, priv);
6495 pci_disable_msi(priv->pci_dev);
6496 pci_save_state(priv->pci_dev);
6497 pci_disable_device(priv->pci_dev);
Mohamed Abbas6ef89d02007-10-25 17:15:47 +08006498
Zhu Yib481de92007-09-25 17:54:57 -07006499 IWL_DEBUG_MAC80211("leave\n");
Zhu Yib481de92007-09-25 17:54:57 -07006500}
6501
Johannes Berge039fa42008-05-15 12:55:29 +02006502static int iwl3945_mac_tx(struct ieee80211_hw *hw, struct sk_buff *skb)
Zhu Yib481de92007-09-25 17:54:57 -07006503{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006504 struct iwl3945_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07006505
6506 IWL_DEBUG_MAC80211("enter\n");
6507
Zhu Yib481de92007-09-25 17:54:57 -07006508 IWL_DEBUG_TX("dev->xmit(%d bytes) at rate 0x%02x\n", skb->len,
Johannes Berge039fa42008-05-15 12:55:29 +02006509 ieee80211_get_tx_rate(hw, IEEE80211_SKB_CB(skb))->bitrate);
Zhu Yib481de92007-09-25 17:54:57 -07006510
Johannes Berge039fa42008-05-15 12:55:29 +02006511 if (iwl3945_tx_skb(priv, skb))
Zhu Yib481de92007-09-25 17:54:57 -07006512 dev_kfree_skb_any(skb);
6513
6514 IWL_DEBUG_MAC80211("leave\n");
Reinette Chatre637f8832009-01-19 15:30:32 -08006515 return NETDEV_TX_OK;
Zhu Yib481de92007-09-25 17:54:57 -07006516}
6517
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006518static int iwl3945_mac_add_interface(struct ieee80211_hw *hw,
Zhu Yib481de92007-09-25 17:54:57 -07006519 struct ieee80211_if_init_conf *conf)
6520{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006521 struct iwl3945_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07006522 unsigned long flags;
6523
Johannes Berg32bfd352007-12-19 01:31:26 +01006524 IWL_DEBUG_MAC80211("enter: type %d\n", conf->type);
Zhu Yib481de92007-09-25 17:54:57 -07006525
Johannes Berg32bfd352007-12-19 01:31:26 +01006526 if (priv->vif) {
6527 IWL_DEBUG_MAC80211("leave - vif != NULL\n");
Tomas Winkler864792e2007-11-27 21:00:52 +02006528 return -EOPNOTSUPP;
Zhu Yib481de92007-09-25 17:54:57 -07006529 }
6530
6531 spin_lock_irqsave(&priv->lock, flags);
Johannes Berg32bfd352007-12-19 01:31:26 +01006532 priv->vif = conf->vif;
Zhu, Yi60294de2008-10-29 14:05:45 -07006533 priv->iw_mode = conf->type;
Zhu Yib481de92007-09-25 17:54:57 -07006534
6535 spin_unlock_irqrestore(&priv->lock, flags);
6536
6537 mutex_lock(&priv->mutex);
Tomas Winkler864792e2007-11-27 21:00:52 +02006538
6539 if (conf->mac_addr) {
Johannes Berge1749612008-10-27 15:59:26 -07006540 IWL_DEBUG_MAC80211("Set: %pM\n", conf->mac_addr);
Tomas Winkler864792e2007-11-27 21:00:52 +02006541 memcpy(priv->mac_addr, conf->mac_addr, ETH_ALEN);
6542 }
6543
Zhu Yi5a669262008-01-14 17:46:18 -08006544 if (iwl3945_is_ready(priv))
6545 iwl3945_set_mode(priv, conf->type);
Zhu Yib481de92007-09-25 17:54:57 -07006546
Zhu Yib481de92007-09-25 17:54:57 -07006547 mutex_unlock(&priv->mutex);
6548
Zhu Yi5a669262008-01-14 17:46:18 -08006549 IWL_DEBUG_MAC80211("leave\n");
Zhu Yib481de92007-09-25 17:54:57 -07006550 return 0;
6551}
6552
6553/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006554 * iwl3945_mac_config - mac80211 config callback
Zhu Yib481de92007-09-25 17:54:57 -07006555 *
6556 * We ignore conf->flags & IEEE80211_CONF_SHORT_SLOT_TIME since it seems to
6557 * be set inappropriately and the driver currently sets the hardware up to
6558 * use it whenever needed.
6559 */
Johannes Berge8975582008-10-09 12:18:51 +02006560static int iwl3945_mac_config(struct ieee80211_hw *hw, u32 changed)
Zhu Yib481de92007-09-25 17:54:57 -07006561{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006562 struct iwl3945_priv *priv = hw->priv;
Samuel Ortizd20b3c62008-12-19 10:37:15 +08006563 const struct iwl_channel_info *ch_info;
Johannes Berge8975582008-10-09 12:18:51 +02006564 struct ieee80211_conf *conf = &hw->conf;
Zhu Yib481de92007-09-25 17:54:57 -07006565 unsigned long flags;
Zhu Yi76bb77e2007-11-22 10:53:22 +08006566 int ret = 0;
Zhu Yib481de92007-09-25 17:54:57 -07006567
6568 mutex_lock(&priv->mutex);
Johannes Berg8318d782008-01-24 19:38:38 +01006569 IWL_DEBUG_MAC80211("enter to channel %d\n", conf->channel->hw_value);
Zhu Yib481de92007-09-25 17:54:57 -07006570
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006571 if (!iwl3945_is_ready(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07006572 IWL_DEBUG_MAC80211("leave - not ready\n");
Zhu Yi76bb77e2007-11-22 10:53:22 +08006573 ret = -EIO;
6574 goto out;
Zhu Yib481de92007-09-25 17:54:57 -07006575 }
6576
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006577 if (unlikely(!iwl3945_param_disable_hw_scan &&
Zhu Yib481de92007-09-25 17:54:57 -07006578 test_bit(STATUS_SCANNING, &priv->status))) {
Zhu Yia0646472007-12-20 14:10:01 +08006579 IWL_DEBUG_MAC80211("leave - scanning\n");
6580 set_bit(STATUS_CONF_PENDING, &priv->status);
Zhu Yib481de92007-09-25 17:54:57 -07006581 mutex_unlock(&priv->mutex);
Zhu Yia0646472007-12-20 14:10:01 +08006582 return 0;
Zhu Yib481de92007-09-25 17:54:57 -07006583 }
6584
6585 spin_lock_irqsave(&priv->lock, flags);
6586
Johannes Berg8318d782008-01-24 19:38:38 +01006587 ch_info = iwl3945_get_channel_info(priv, conf->channel->band,
6588 conf->channel->hw_value);
Zhu Yib481de92007-09-25 17:54:57 -07006589 if (!is_channel_valid(ch_info)) {
Ron Rindjunsky66b50042008-06-25 16:46:31 +08006590 IWL_DEBUG_SCAN("Channel %d [%d] is INVALID for this band.\n",
Johannes Berg8318d782008-01-24 19:38:38 +01006591 conf->channel->hw_value, conf->channel->band);
Zhu Yib481de92007-09-25 17:54:57 -07006592 IWL_DEBUG_MAC80211("leave - invalid channel\n");
6593 spin_unlock_irqrestore(&priv->lock, flags);
Zhu Yi76bb77e2007-11-22 10:53:22 +08006594 ret = -EINVAL;
6595 goto out;
Zhu Yib481de92007-09-25 17:54:57 -07006596 }
6597
Johannes Berg8318d782008-01-24 19:38:38 +01006598 iwl3945_set_rxon_channel(priv, conf->channel->band, conf->channel->hw_value);
Zhu Yib481de92007-09-25 17:54:57 -07006599
Johannes Berg8318d782008-01-24 19:38:38 +01006600 iwl3945_set_flags_for_phymode(priv, conf->channel->band);
Zhu Yib481de92007-09-25 17:54:57 -07006601
6602 /* The list of supported rates and rate mask can be different
6603 * for each phymode; since the phymode may have changed, reset
6604 * the rate mask to what mac80211 lists */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006605 iwl3945_set_rate(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006606
6607 spin_unlock_irqrestore(&priv->lock, flags);
6608
6609#ifdef IEEE80211_CONF_CHANNEL_SWITCH
6610 if (conf->flags & IEEE80211_CONF_CHANNEL_SWITCH) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006611 iwl3945_hw_channel_switch(priv, conf->channel);
Zhu Yi76bb77e2007-11-22 10:53:22 +08006612 goto out;
Zhu Yib481de92007-09-25 17:54:57 -07006613 }
6614#endif
6615
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006616 iwl3945_radio_kill_sw(priv, !conf->radio_enabled);
Zhu Yib481de92007-09-25 17:54:57 -07006617
6618 if (!conf->radio_enabled) {
6619 IWL_DEBUG_MAC80211("leave - radio disabled\n");
Zhu Yi76bb77e2007-11-22 10:53:22 +08006620 goto out;
Zhu Yib481de92007-09-25 17:54:57 -07006621 }
6622
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006623 if (iwl3945_is_rfkill(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07006624 IWL_DEBUG_MAC80211("leave - RF kill\n");
Zhu Yi76bb77e2007-11-22 10:53:22 +08006625 ret = -EIO;
6626 goto out;
Zhu Yib481de92007-09-25 17:54:57 -07006627 }
6628
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006629 iwl3945_set_rate(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006630
6631 if (memcmp(&priv->active_rxon,
6632 &priv->staging_rxon, sizeof(priv->staging_rxon)))
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006633 iwl3945_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006634 else
6635 IWL_DEBUG_INFO("No re-sending same RXON configuration.\n");
6636
6637 IWL_DEBUG_MAC80211("leave\n");
6638
Zhu Yi76bb77e2007-11-22 10:53:22 +08006639out:
Zhu Yia0646472007-12-20 14:10:01 +08006640 clear_bit(STATUS_CONF_PENDING, &priv->status);
Zhu Yib481de92007-09-25 17:54:57 -07006641 mutex_unlock(&priv->mutex);
Zhu Yi76bb77e2007-11-22 10:53:22 +08006642 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07006643}
6644
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006645static void iwl3945_config_ap(struct iwl3945_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07006646{
6647 int rc = 0;
6648
Maarten Lankhorstd986bcd2008-01-23 10:15:16 -08006649 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
Zhu Yib481de92007-09-25 17:54:57 -07006650 return;
6651
6652 /* The following should be done only at AP bring up */
Ron Rindjunsky5d1e2322008-06-30 17:23:04 +08006653 if (!(iwl3945_is_associated(priv))) {
Zhu Yib481de92007-09-25 17:54:57 -07006654
6655 /* RXON - unassoc (to set timing command) */
6656 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006657 iwl3945_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006658
6659 /* RXON Timing */
Tomas Winkler28afaf92008-12-19 10:37:06 +08006660 memset(&priv->rxon_timing, 0, sizeof(struct iwl_rxon_time_cmd));
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006661 iwl3945_setup_rxon_timing(priv);
6662 rc = iwl3945_send_cmd_pdu(priv, REPLY_RXON_TIMING,
Zhu Yib481de92007-09-25 17:54:57 -07006663 sizeof(priv->rxon_timing), &priv->rxon_timing);
6664 if (rc)
6665 IWL_WARNING("REPLY_RXON_TIMING failed - "
6666 "Attempting to continue.\n");
6667
6668 /* FIXME: what should be the assoc_id for AP? */
6669 priv->staging_rxon.assoc_id = cpu_to_le16(priv->assoc_id);
6670 if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_PREAMBLE)
6671 priv->staging_rxon.flags |=
6672 RXON_FLG_SHORT_PREAMBLE_MSK;
6673 else
6674 priv->staging_rxon.flags &=
6675 ~RXON_FLG_SHORT_PREAMBLE_MSK;
6676
6677 if (priv->staging_rxon.flags & RXON_FLG_BAND_24G_MSK) {
6678 if (priv->assoc_capability &
6679 WLAN_CAPABILITY_SHORT_SLOT_TIME)
6680 priv->staging_rxon.flags |=
6681 RXON_FLG_SHORT_SLOT_MSK;
6682 else
6683 priv->staging_rxon.flags &=
6684 ~RXON_FLG_SHORT_SLOT_MSK;
6685
Johannes Berg05c914f2008-09-11 00:01:58 +02006686 if (priv->iw_mode == NL80211_IFTYPE_ADHOC)
Zhu Yib481de92007-09-25 17:54:57 -07006687 priv->staging_rxon.flags &=
6688 ~RXON_FLG_SHORT_SLOT_MSK;
6689 }
6690 /* restore RXON assoc */
6691 priv->staging_rxon.filter_flags |= RXON_FILTER_ASSOC_MSK;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006692 iwl3945_commit_rxon(priv);
6693 iwl3945_add_station(priv, iwl3945_broadcast_addr, 0, 0);
Zhu Yi556f8db2007-09-27 11:27:33 +08006694 }
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006695 iwl3945_send_beacon_cmd(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006696
6697 /* FIXME - we need to add code here to detect a totally new
6698 * configuration, reset the AP, unassoc, rxon timing, assoc,
6699 * clear sta table, add BCAST sta... */
6700}
6701
Johannes Berg32bfd352007-12-19 01:31:26 +01006702static int iwl3945_mac_config_interface(struct ieee80211_hw *hw,
6703 struct ieee80211_vif *vif,
Zhu Yib481de92007-09-25 17:54:57 -07006704 struct ieee80211_if_conf *conf)
6705{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006706 struct iwl3945_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07006707 int rc;
6708
6709 if (conf == NULL)
6710 return -EIO;
6711
Emmanuel Grumbachb716bb92008-03-04 18:09:32 -08006712 if (priv->vif != vif) {
6713 IWL_DEBUG_MAC80211("leave - priv->vif != vif\n");
Emmanuel Grumbachb716bb92008-03-04 18:09:32 -08006714 return 0;
6715 }
6716
Johannes Berg9d139c82008-07-09 14:40:37 +02006717 /* handle this temporarily here */
Johannes Berg05c914f2008-09-11 00:01:58 +02006718 if (priv->iw_mode == NL80211_IFTYPE_ADHOC &&
Johannes Berg9d139c82008-07-09 14:40:37 +02006719 conf->changed & IEEE80211_IFCC_BEACON) {
6720 struct sk_buff *beacon = ieee80211_beacon_get(hw, vif);
6721 if (!beacon)
6722 return -ENOMEM;
Mohamed Abbas9bdf5ec2008-11-07 09:58:35 -08006723 mutex_lock(&priv->mutex);
Johannes Berg9d139c82008-07-09 14:40:37 +02006724 rc = iwl3945_mac_beacon_update(hw, beacon);
Mohamed Abbas9bdf5ec2008-11-07 09:58:35 -08006725 mutex_unlock(&priv->mutex);
Johannes Berg9d139c82008-07-09 14:40:37 +02006726 if (rc)
6727 return rc;
6728 }
6729
Zhu Yi5a669262008-01-14 17:46:18 -08006730 if (!iwl3945_is_alive(priv))
6731 return -EAGAIN;
6732
Zhu Yib481de92007-09-25 17:54:57 -07006733 mutex_lock(&priv->mutex);
6734
Zhu Yib481de92007-09-25 17:54:57 -07006735 if (conf->bssid)
Johannes Berge1749612008-10-27 15:59:26 -07006736 IWL_DEBUG_MAC80211("bssid: %pM\n", conf->bssid);
Zhu Yib481de92007-09-25 17:54:57 -07006737
Johannes Berg4150c572007-09-17 01:29:23 -04006738/*
6739 * very dubious code was here; the probe filtering flag is never set:
6740 *
Zhu Yib481de92007-09-25 17:54:57 -07006741 if (unlikely(test_bit(STATUS_SCANNING, &priv->status)) &&
6742 !(priv->hw->flags & IEEE80211_HW_NO_PROBE_FILTERING)) {
Johannes Berg4150c572007-09-17 01:29:23 -04006743 */
Zhu Yib481de92007-09-25 17:54:57 -07006744
Johannes Berg05c914f2008-09-11 00:01:58 +02006745 if (priv->iw_mode == NL80211_IFTYPE_AP) {
Zhu Yib481de92007-09-25 17:54:57 -07006746 if (!conf->bssid) {
6747 conf->bssid = priv->mac_addr;
6748 memcpy(priv->bssid, priv->mac_addr, ETH_ALEN);
Johannes Berge1749612008-10-27 15:59:26 -07006749 IWL_DEBUG_MAC80211("bssid was set to: %pM\n",
6750 conf->bssid);
Zhu Yib481de92007-09-25 17:54:57 -07006751 }
6752 if (priv->ibss_beacon)
6753 dev_kfree_skb(priv->ibss_beacon);
6754
Johannes Berg9d139c82008-07-09 14:40:37 +02006755 priv->ibss_beacon = ieee80211_beacon_get(hw, vif);
Zhu Yib481de92007-09-25 17:54:57 -07006756 }
6757
Mohamed Abbasfde35712007-11-29 11:10:15 +08006758 if (iwl3945_is_rfkill(priv))
6759 goto done;
6760
Zhu Yib481de92007-09-25 17:54:57 -07006761 if (conf->bssid && !is_zero_ether_addr(conf->bssid) &&
6762 !is_multicast_ether_addr(conf->bssid)) {
6763 /* If there is currently a HW scan going on in the background
6764 * then we need to cancel it else the RXON below will fail. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006765 if (iwl3945_scan_cancel_timeout(priv, 100)) {
Zhu Yib481de92007-09-25 17:54:57 -07006766 IWL_WARNING("Aborted scan still in progress "
6767 "after 100ms\n");
6768 IWL_DEBUG_MAC80211("leaving - scan abort failed.\n");
6769 mutex_unlock(&priv->mutex);
6770 return -EAGAIN;
6771 }
6772 memcpy(priv->staging_rxon.bssid_addr, conf->bssid, ETH_ALEN);
6773
6774 /* TODO: Audit driver for usage of these members and see
6775 * if mac80211 deprecates them (priv->bssid looks like it
6776 * shouldn't be there, but I haven't scanned the IBSS code
6777 * to verify) - jpk */
6778 memcpy(priv->bssid, conf->bssid, ETH_ALEN);
6779
Johannes Berg05c914f2008-09-11 00:01:58 +02006780 if (priv->iw_mode == NL80211_IFTYPE_AP)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006781 iwl3945_config_ap(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006782 else {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006783 rc = iwl3945_commit_rxon(priv);
Johannes Berg05c914f2008-09-11 00:01:58 +02006784 if ((priv->iw_mode == NL80211_IFTYPE_STATION) && rc)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006785 iwl3945_add_station(priv,
Zhu Yi556f8db2007-09-27 11:27:33 +08006786 priv->active_rxon.bssid_addr, 1, 0);
Zhu Yib481de92007-09-25 17:54:57 -07006787 }
6788
6789 } else {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006790 iwl3945_scan_cancel_timeout(priv, 100);
Zhu Yib481de92007-09-25 17:54:57 -07006791 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006792 iwl3945_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006793 }
6794
Mohamed Abbasfde35712007-11-29 11:10:15 +08006795 done:
Zhu Yib481de92007-09-25 17:54:57 -07006796 IWL_DEBUG_MAC80211("leave\n");
6797 mutex_unlock(&priv->mutex);
6798
6799 return 0;
6800}
6801
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006802static void iwl3945_configure_filter(struct ieee80211_hw *hw,
Johannes Berg4150c572007-09-17 01:29:23 -04006803 unsigned int changed_flags,
6804 unsigned int *total_flags,
6805 int mc_count, struct dev_addr_list *mc_list)
6806{
Abhijeet Kolekar5ec03972008-05-05 10:22:48 +08006807 struct iwl3945_priv *priv = hw->priv;
Zhu, Yi352bc8d2008-11-12 13:14:09 -08006808 __le32 *filter_flags = &priv->staging_rxon.filter_flags;
Rick Farrington25b3f572008-06-30 17:23:28 +08006809
Zhu, Yi352bc8d2008-11-12 13:14:09 -08006810 IWL_DEBUG_MAC80211("Enter: changed: 0x%x, total: 0x%x\n",
6811 changed_flags, *total_flags);
6812
6813 if (changed_flags & (FIF_OTHER_BSS | FIF_PROMISC_IN_BSS)) {
6814 if (*total_flags & (FIF_OTHER_BSS | FIF_PROMISC_IN_BSS))
6815 *filter_flags |= RXON_FILTER_PROMISC_MSK;
6816 else
6817 *filter_flags &= ~RXON_FILTER_PROMISC_MSK;
Abhijeet Kolekar5ec03972008-05-05 10:22:48 +08006818 }
Zhu, Yi352bc8d2008-11-12 13:14:09 -08006819 if (changed_flags & FIF_ALLMULTI) {
6820 if (*total_flags & FIF_ALLMULTI)
6821 *filter_flags |= RXON_FILTER_ACCEPT_GRP_MSK;
6822 else
6823 *filter_flags &= ~RXON_FILTER_ACCEPT_GRP_MSK;
6824 }
6825 if (changed_flags & FIF_CONTROL) {
6826 if (*total_flags & FIF_CONTROL)
6827 *filter_flags |= RXON_FILTER_CTL2HOST_MSK;
6828 else
6829 *filter_flags &= ~RXON_FILTER_CTL2HOST_MSK;
6830 }
6831 if (changed_flags & FIF_BCN_PRBRESP_PROMISC) {
6832 if (*total_flags & FIF_BCN_PRBRESP_PROMISC)
6833 *filter_flags |= RXON_FILTER_BCON_AWARE_MSK;
6834 else
6835 *filter_flags &= ~RXON_FILTER_BCON_AWARE_MSK;
6836 }
6837
6838 /* We avoid iwl_commit_rxon here to commit the new filter flags
6839 * since mac80211 will call ieee80211_hw_config immediately.
6840 * (mc_list is not supported at this time). Otherwise, we need to
6841 * queue a background iwl_commit_rxon work.
6842 */
6843
6844 *total_flags &= FIF_OTHER_BSS | FIF_ALLMULTI | FIF_PROMISC_IN_BSS |
Rick Farrington25b3f572008-06-30 17:23:28 +08006845 FIF_BCN_PRBRESP_PROMISC | FIF_CONTROL;
Johannes Berg4150c572007-09-17 01:29:23 -04006846}
6847
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006848static void iwl3945_mac_remove_interface(struct ieee80211_hw *hw,
Zhu Yib481de92007-09-25 17:54:57 -07006849 struct ieee80211_if_init_conf *conf)
6850{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006851 struct iwl3945_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07006852
6853 IWL_DEBUG_MAC80211("enter\n");
6854
6855 mutex_lock(&priv->mutex);
Mohamed Abbas6ef89d02007-10-25 17:15:47 +08006856
Mohamed Abbasfde35712007-11-29 11:10:15 +08006857 if (iwl3945_is_ready_rf(priv)) {
6858 iwl3945_scan_cancel_timeout(priv, 100);
Mohamed Abbasfde35712007-11-29 11:10:15 +08006859 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
6860 iwl3945_commit_rxon(priv);
6861 }
Johannes Berg32bfd352007-12-19 01:31:26 +01006862 if (priv->vif == conf->vif) {
6863 priv->vif = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07006864 memset(priv->bssid, 0, ETH_ALEN);
Zhu Yib481de92007-09-25 17:54:57 -07006865 }
6866 mutex_unlock(&priv->mutex);
6867
6868 IWL_DEBUG_MAC80211("leave\n");
Zhu Yib481de92007-09-25 17:54:57 -07006869}
6870
Abhijeet Kolekarcd56d332008-09-03 11:26:21 +08006871#define IWL_DELAY_NEXT_SCAN_AFTER_ASSOC (HZ*6)
6872
6873static void iwl3945_bss_info_changed(struct ieee80211_hw *hw,
6874 struct ieee80211_vif *vif,
6875 struct ieee80211_bss_conf *bss_conf,
6876 u32 changes)
6877{
6878 struct iwl3945_priv *priv = hw->priv;
6879
6880 IWL_DEBUG_MAC80211("changes = 0x%X\n", changes);
6881
6882 if (changes & BSS_CHANGED_ERP_PREAMBLE) {
6883 IWL_DEBUG_MAC80211("ERP_PREAMBLE %d\n",
6884 bss_conf->use_short_preamble);
6885 if (bss_conf->use_short_preamble)
6886 priv->staging_rxon.flags |= RXON_FLG_SHORT_PREAMBLE_MSK;
6887 else
6888 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_PREAMBLE_MSK;
6889 }
6890
6891 if (changes & BSS_CHANGED_ERP_CTS_PROT) {
6892 IWL_DEBUG_MAC80211("ERP_CTS %d\n", bss_conf->use_cts_prot);
6893 if (bss_conf->use_cts_prot && (priv->band != IEEE80211_BAND_5GHZ))
6894 priv->staging_rxon.flags |= RXON_FLG_TGG_PROTECT_MSK;
6895 else
6896 priv->staging_rxon.flags &= ~RXON_FLG_TGG_PROTECT_MSK;
6897 }
6898
6899 if (changes & BSS_CHANGED_ASSOC) {
6900 IWL_DEBUG_MAC80211("ASSOC %d\n", bss_conf->assoc);
6901 /* This should never happen as this function should
6902 * never be called from interrupt context. */
6903 if (WARN_ON_ONCE(in_interrupt()))
6904 return;
6905 if (bss_conf->assoc) {
6906 priv->assoc_id = bss_conf->aid;
6907 priv->beacon_int = bss_conf->beacon_int;
Tomas Winkler28afaf92008-12-19 10:37:06 +08006908 priv->timestamp = bss_conf->timestamp;
Abhijeet Kolekarcd56d332008-09-03 11:26:21 +08006909 priv->assoc_capability = bss_conf->assoc_capability;
6910 priv->next_scan_jiffies = jiffies +
6911 IWL_DELAY_NEXT_SCAN_AFTER_ASSOC;
6912 mutex_lock(&priv->mutex);
6913 iwl3945_post_associate(priv);
6914 mutex_unlock(&priv->mutex);
6915 } else {
6916 priv->assoc_id = 0;
6917 IWL_DEBUG_MAC80211("DISASSOC %d\n", bss_conf->assoc);
6918 }
6919 } else if (changes && iwl3945_is_associated(priv) && priv->assoc_id) {
6920 IWL_DEBUG_MAC80211("Associated Changes %d\n", changes);
6921 iwl3945_send_rxon_assoc(priv);
6922 }
6923
6924}
6925
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006926static int iwl3945_mac_hw_scan(struct ieee80211_hw *hw, u8 *ssid, size_t len)
Zhu Yib481de92007-09-25 17:54:57 -07006927{
6928 int rc = 0;
6929 unsigned long flags;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006930 struct iwl3945_priv *priv = hw->priv;
John W. Linville9387b7c2008-09-30 20:59:05 -04006931 DECLARE_SSID_BUF(ssid_buf);
Zhu Yib481de92007-09-25 17:54:57 -07006932
6933 IWL_DEBUG_MAC80211("enter\n");
6934
Mohamed Abbas15e869d2007-10-25 17:15:46 +08006935 mutex_lock(&priv->mutex);
Zhu Yib481de92007-09-25 17:54:57 -07006936 spin_lock_irqsave(&priv->lock, flags);
6937
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006938 if (!iwl3945_is_ready_rf(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07006939 rc = -EIO;
6940 IWL_DEBUG_MAC80211("leave - not ready or exit pending\n");
6941 goto out_unlock;
6942 }
6943
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08006944 /* we don't schedule scan within next_scan_jiffies period */
6945 if (priv->next_scan_jiffies &&
6946 time_after(priv->next_scan_jiffies, jiffies)) {
6947 rc = -EAGAIN;
6948 goto out_unlock;
6949 }
Bill Moss15dbf1b2008-05-06 11:05:15 +08006950 /* if we just finished scan ask for delay for a broadcast scan */
6951 if ((len == 0) && priv->last_scan_jiffies &&
6952 time_after(priv->last_scan_jiffies + IWL_DELAY_NEXT_SCAN,
6953 jiffies)) {
Zhu Yib481de92007-09-25 17:54:57 -07006954 rc = -EAGAIN;
6955 goto out_unlock;
6956 }
6957 if (len) {
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08006958 IWL_DEBUG_SCAN("direct scan for %s [%d]\n ",
John W. Linville9387b7c2008-09-30 20:59:05 -04006959 print_ssid(ssid_buf, ssid, len), (int)len);
Zhu Yib481de92007-09-25 17:54:57 -07006960
6961 priv->one_direct_scan = 1;
6962 priv->direct_ssid_len = (u8)
6963 min((u8) len, (u8) IW_ESSID_MAX_SIZE);
6964 memcpy(priv->direct_ssid, ssid, priv->direct_ssid_len);
Mohamed Abbas6ef89d02007-10-25 17:15:47 +08006965 } else
6966 priv->one_direct_scan = 0;
Zhu Yib481de92007-09-25 17:54:57 -07006967
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006968 rc = iwl3945_scan_initiate(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006969
6970 IWL_DEBUG_MAC80211("leave\n");
6971
6972out_unlock:
6973 spin_unlock_irqrestore(&priv->lock, flags);
Mohamed Abbas15e869d2007-10-25 17:15:46 +08006974 mutex_unlock(&priv->mutex);
Zhu Yib481de92007-09-25 17:54:57 -07006975
6976 return rc;
6977}
6978
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006979static int iwl3945_mac_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
Zhu Yib481de92007-09-25 17:54:57 -07006980 const u8 *local_addr, const u8 *addr,
6981 struct ieee80211_key_conf *key)
6982{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006983 struct iwl3945_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07006984 int rc = 0;
6985 u8 sta_id;
6986
6987 IWL_DEBUG_MAC80211("enter\n");
6988
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006989 if (!iwl3945_param_hwcrypto) {
Zhu Yib481de92007-09-25 17:54:57 -07006990 IWL_DEBUG_MAC80211("leave - hwcrypto disabled\n");
6991 return -EOPNOTSUPP;
6992 }
6993
6994 if (is_zero_ether_addr(addr))
6995 /* only support pairwise keys */
6996 return -EOPNOTSUPP;
6997
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006998 sta_id = iwl3945_hw_find_station(priv, addr);
Zhu Yib481de92007-09-25 17:54:57 -07006999 if (sta_id == IWL_INVALID_STATION) {
Johannes Berge1749612008-10-27 15:59:26 -07007000 IWL_DEBUG_MAC80211("leave - %pM not in station map.\n",
7001 addr);
Zhu Yib481de92007-09-25 17:54:57 -07007002 return -EINVAL;
7003 }
7004
7005 mutex_lock(&priv->mutex);
7006
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007007 iwl3945_scan_cancel_timeout(priv, 100);
Mohamed Abbas15e869d2007-10-25 17:15:46 +08007008
Zhu Yib481de92007-09-25 17:54:57 -07007009 switch (cmd) {
7010 case SET_KEY:
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007011 rc = iwl3945_update_sta_key_info(priv, key, sta_id);
Zhu Yib481de92007-09-25 17:54:57 -07007012 if (!rc) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007013 iwl3945_set_rxon_hwcrypto(priv, 1);
7014 iwl3945_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007015 key->hw_key_idx = sta_id;
7016 IWL_DEBUG_MAC80211("set_key success, using hwcrypto\n");
7017 key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
7018 }
7019 break;
7020 case DISABLE_KEY:
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007021 rc = iwl3945_clear_sta_key_info(priv, sta_id);
Zhu Yib481de92007-09-25 17:54:57 -07007022 if (!rc) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007023 iwl3945_set_rxon_hwcrypto(priv, 0);
7024 iwl3945_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007025 IWL_DEBUG_MAC80211("disable hwcrypto key\n");
7026 }
7027 break;
7028 default:
7029 rc = -EINVAL;
7030 }
7031
7032 IWL_DEBUG_MAC80211("leave\n");
7033 mutex_unlock(&priv->mutex);
7034
7035 return rc;
7036}
7037
Johannes Berge100bb62008-04-30 18:51:21 +02007038static int iwl3945_mac_conf_tx(struct ieee80211_hw *hw, u16 queue,
Zhu Yib481de92007-09-25 17:54:57 -07007039 const struct ieee80211_tx_queue_params *params)
7040{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007041 struct iwl3945_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07007042 unsigned long flags;
7043 int q;
Zhu Yib481de92007-09-25 17:54:57 -07007044
7045 IWL_DEBUG_MAC80211("enter\n");
7046
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007047 if (!iwl3945_is_ready_rf(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07007048 IWL_DEBUG_MAC80211("leave - RF not ready\n");
7049 return -EIO;
7050 }
7051
7052 if (queue >= AC_NUM) {
7053 IWL_DEBUG_MAC80211("leave - queue >= AC_NUM %d\n", queue);
7054 return 0;
7055 }
7056
Zhu Yib481de92007-09-25 17:54:57 -07007057 q = AC_NUM - 1 - queue;
7058
7059 spin_lock_irqsave(&priv->lock, flags);
7060
7061 priv->qos_data.def_qos_parm.ac[q].cw_min = cpu_to_le16(params->cw_min);
7062 priv->qos_data.def_qos_parm.ac[q].cw_max = cpu_to_le16(params->cw_max);
7063 priv->qos_data.def_qos_parm.ac[q].aifsn = params->aifs;
7064 priv->qos_data.def_qos_parm.ac[q].edca_txop =
Johannes Berg3330d7b2008-02-10 16:49:38 +01007065 cpu_to_le16((params->txop * 32));
Zhu Yib481de92007-09-25 17:54:57 -07007066
7067 priv->qos_data.def_qos_parm.ac[q].reserved1 = 0;
7068 priv->qos_data.qos_active = 1;
7069
7070 spin_unlock_irqrestore(&priv->lock, flags);
7071
7072 mutex_lock(&priv->mutex);
Johannes Berg05c914f2008-09-11 00:01:58 +02007073 if (priv->iw_mode == NL80211_IFTYPE_AP)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007074 iwl3945_activate_qos(priv, 1);
7075 else if (priv->assoc_id && iwl3945_is_associated(priv))
7076 iwl3945_activate_qos(priv, 0);
Zhu Yib481de92007-09-25 17:54:57 -07007077
7078 mutex_unlock(&priv->mutex);
7079
Zhu Yib481de92007-09-25 17:54:57 -07007080 IWL_DEBUG_MAC80211("leave\n");
7081 return 0;
7082}
7083
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007084static int iwl3945_mac_get_tx_stats(struct ieee80211_hw *hw,
Zhu Yib481de92007-09-25 17:54:57 -07007085 struct ieee80211_tx_queue_stats *stats)
7086{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007087 struct iwl3945_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07007088 int i, avail;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007089 struct iwl3945_tx_queue *txq;
Samuel Ortizd20b3c62008-12-19 10:37:15 +08007090 struct iwl_queue *q;
Zhu Yib481de92007-09-25 17:54:57 -07007091 unsigned long flags;
7092
7093 IWL_DEBUG_MAC80211("enter\n");
7094
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007095 if (!iwl3945_is_ready_rf(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07007096 IWL_DEBUG_MAC80211("leave - RF not ready\n");
7097 return -EIO;
7098 }
7099
7100 spin_lock_irqsave(&priv->lock, flags);
7101
7102 for (i = 0; i < AC_NUM; i++) {
7103 txq = &priv->txq[i];
7104 q = &txq->q;
Samuel Ortizd20b3c62008-12-19 10:37:15 +08007105 avail = iwl_queue_space(q);
Zhu Yib481de92007-09-25 17:54:57 -07007106
Johannes Berg57ffc582008-04-29 17:18:59 +02007107 stats[i].len = q->n_window - avail;
7108 stats[i].limit = q->n_window - q->high_mark;
7109 stats[i].count = q->n_window;
Zhu Yib481de92007-09-25 17:54:57 -07007110
7111 }
7112 spin_unlock_irqrestore(&priv->lock, flags);
7113
7114 IWL_DEBUG_MAC80211("leave\n");
7115
7116 return 0;
7117}
7118
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007119static int iwl3945_mac_get_stats(struct ieee80211_hw *hw,
Zhu Yib481de92007-09-25 17:54:57 -07007120 struct ieee80211_low_level_stats *stats)
7121{
Zhu Yib481de92007-09-25 17:54:57 -07007122 return 0;
7123}
7124
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007125static void iwl3945_mac_reset_tsf(struct ieee80211_hw *hw)
Zhu Yib481de92007-09-25 17:54:57 -07007126{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007127 struct iwl3945_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07007128 unsigned long flags;
7129
7130 mutex_lock(&priv->mutex);
7131 IWL_DEBUG_MAC80211("enter\n");
7132
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007133 iwl3945_reset_qos(priv);
Ron Rindjunsky292ae172008-02-06 11:20:39 -08007134
Zhu Yib481de92007-09-25 17:54:57 -07007135 spin_lock_irqsave(&priv->lock, flags);
7136 priv->assoc_id = 0;
7137 priv->assoc_capability = 0;
7138 priv->call_post_assoc_from_beacon = 0;
7139
7140 /* new association get rid of ibss beacon skb */
7141 if (priv->ibss_beacon)
7142 dev_kfree_skb(priv->ibss_beacon);
7143
7144 priv->ibss_beacon = NULL;
7145
7146 priv->beacon_int = priv->hw->conf.beacon_int;
Tomas Winkler28afaf92008-12-19 10:37:06 +08007147 priv->timestamp = 0;
Johannes Berg05c914f2008-09-11 00:01:58 +02007148 if ((priv->iw_mode == NL80211_IFTYPE_STATION))
Zhu Yib481de92007-09-25 17:54:57 -07007149 priv->beacon_int = 0;
7150
7151 spin_unlock_irqrestore(&priv->lock, flags);
7152
Mohamed Abbasfde35712007-11-29 11:10:15 +08007153 if (!iwl3945_is_ready_rf(priv)) {
7154 IWL_DEBUG_MAC80211("leave - not ready\n");
7155 mutex_unlock(&priv->mutex);
7156 return;
7157 }
7158
Mohamed Abbas15e869d2007-10-25 17:15:46 +08007159 /* we are restarting association process
7160 * clear RXON_FILTER_ASSOC_MSK bit
7161 */
Johannes Berg05c914f2008-09-11 00:01:58 +02007162 if (priv->iw_mode != NL80211_IFTYPE_AP) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007163 iwl3945_scan_cancel_timeout(priv, 100);
Mohamed Abbas15e869d2007-10-25 17:15:46 +08007164 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007165 iwl3945_commit_rxon(priv);
Mohamed Abbas15e869d2007-10-25 17:15:46 +08007166 }
7167
Zhu Yib481de92007-09-25 17:54:57 -07007168 /* Per mac80211.h: This is only used in IBSS mode... */
Johannes Berg05c914f2008-09-11 00:01:58 +02007169 if (priv->iw_mode != NL80211_IFTYPE_ADHOC) {
Mohamed Abbas15e869d2007-10-25 17:15:46 +08007170
Zhu Yib481de92007-09-25 17:54:57 -07007171 IWL_DEBUG_MAC80211("leave - not in IBSS\n");
7172 mutex_unlock(&priv->mutex);
7173 return;
7174 }
7175
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007176 iwl3945_set_rate(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007177
7178 mutex_unlock(&priv->mutex);
7179
7180 IWL_DEBUG_MAC80211("leave\n");
7181
7182}
7183
Johannes Berge039fa42008-05-15 12:55:29 +02007184static int iwl3945_mac_beacon_update(struct ieee80211_hw *hw, struct sk_buff *skb)
Zhu Yib481de92007-09-25 17:54:57 -07007185{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007186 struct iwl3945_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07007187 unsigned long flags;
7188
Zhu Yib481de92007-09-25 17:54:57 -07007189 IWL_DEBUG_MAC80211("enter\n");
7190
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007191 if (!iwl3945_is_ready_rf(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07007192 IWL_DEBUG_MAC80211("leave - RF not ready\n");
Zhu Yib481de92007-09-25 17:54:57 -07007193 return -EIO;
7194 }
7195
Johannes Berg05c914f2008-09-11 00:01:58 +02007196 if (priv->iw_mode != NL80211_IFTYPE_ADHOC) {
Zhu Yib481de92007-09-25 17:54:57 -07007197 IWL_DEBUG_MAC80211("leave - not IBSS\n");
Zhu Yib481de92007-09-25 17:54:57 -07007198 return -EIO;
7199 }
7200
7201 spin_lock_irqsave(&priv->lock, flags);
7202
7203 if (priv->ibss_beacon)
7204 dev_kfree_skb(priv->ibss_beacon);
7205
7206 priv->ibss_beacon = skb;
7207
7208 priv->assoc_id = 0;
7209
7210 IWL_DEBUG_MAC80211("leave\n");
7211 spin_unlock_irqrestore(&priv->lock, flags);
7212
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007213 iwl3945_reset_qos(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007214
Abhijeet Kolekardc4b1e72008-09-03 11:26:30 +08007215 iwl3945_post_associate(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007216
Zhu Yib481de92007-09-25 17:54:57 -07007217
7218 return 0;
7219}
7220
7221/*****************************************************************************
7222 *
7223 * sysfs attributes
7224 *
7225 *****************************************************************************/
7226
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08007227#ifdef CONFIG_IWL3945_DEBUG
Zhu Yib481de92007-09-25 17:54:57 -07007228
7229/*
7230 * The following adds a new attribute to the sysfs representation
7231 * of this device driver (i.e. a new file in /sys/bus/pci/drivers/iwl/)
7232 * used for controlling the debug level.
7233 *
7234 * See the level definitions in iwl for details.
7235 */
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08007236static ssize_t show_debug_level(struct device *d,
7237 struct device_attribute *attr, char *buf)
Zhu Yib481de92007-09-25 17:54:57 -07007238{
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08007239 struct iwl3945_priv *priv = d->driver_data;
7240
7241 return sprintf(buf, "0x%08X\n", priv->debug_level);
Zhu Yib481de92007-09-25 17:54:57 -07007242}
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08007243static ssize_t store_debug_level(struct device *d,
7244 struct device_attribute *attr,
Zhu Yib481de92007-09-25 17:54:57 -07007245 const char *buf, size_t count)
7246{
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08007247 struct iwl3945_priv *priv = d->driver_data;
7248 unsigned long val;
7249 int ret;
Zhu Yib481de92007-09-25 17:54:57 -07007250
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08007251 ret = strict_strtoul(buf, 0, &val);
7252 if (ret)
Zhu Yib481de92007-09-25 17:54:57 -07007253 printk(KERN_INFO DRV_NAME
7254 ": %s is not in hex or decimal form.\n", buf);
7255 else
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08007256 priv->debug_level = val;
Zhu Yib481de92007-09-25 17:54:57 -07007257
7258 return strnlen(buf, count);
7259}
7260
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08007261static DEVICE_ATTR(debug_level, S_IWUSR | S_IRUGO,
7262 show_debug_level, store_debug_level);
Zhu Yib481de92007-09-25 17:54:57 -07007263
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08007264#endif /* CONFIG_IWL3945_DEBUG */
Zhu Yib481de92007-09-25 17:54:57 -07007265
Zhu Yib481de92007-09-25 17:54:57 -07007266static ssize_t show_temperature(struct device *d,
7267 struct device_attribute *attr, char *buf)
7268{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007269 struct iwl3945_priv *priv = (struct iwl3945_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07007270
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007271 if (!iwl3945_is_alive(priv))
Zhu Yib481de92007-09-25 17:54:57 -07007272 return -EAGAIN;
7273
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007274 return sprintf(buf, "%d\n", iwl3945_hw_get_temperature(priv));
Zhu Yib481de92007-09-25 17:54:57 -07007275}
7276
7277static DEVICE_ATTR(temperature, S_IRUGO, show_temperature, NULL);
7278
Zhu Yib481de92007-09-25 17:54:57 -07007279static ssize_t show_tx_power(struct device *d,
7280 struct device_attribute *attr, char *buf)
7281{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007282 struct iwl3945_priv *priv = (struct iwl3945_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07007283 return sprintf(buf, "%d\n", priv->user_txpower_limit);
7284}
7285
7286static ssize_t store_tx_power(struct device *d,
7287 struct device_attribute *attr,
7288 const char *buf, size_t count)
7289{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007290 struct iwl3945_priv *priv = (struct iwl3945_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07007291 char *p = (char *)buf;
7292 u32 val;
7293
7294 val = simple_strtoul(p, &p, 10);
7295 if (p == buf)
7296 printk(KERN_INFO DRV_NAME
7297 ": %s is not in decimal form.\n", buf);
7298 else
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007299 iwl3945_hw_reg_set_txpower(priv, val);
Zhu Yib481de92007-09-25 17:54:57 -07007300
7301 return count;
7302}
7303
7304static DEVICE_ATTR(tx_power, S_IWUSR | S_IRUGO, show_tx_power, store_tx_power);
7305
7306static ssize_t show_flags(struct device *d,
7307 struct device_attribute *attr, char *buf)
7308{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007309 struct iwl3945_priv *priv = (struct iwl3945_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07007310
7311 return sprintf(buf, "0x%04X\n", priv->active_rxon.flags);
7312}
7313
7314static ssize_t store_flags(struct device *d,
7315 struct device_attribute *attr,
7316 const char *buf, size_t count)
7317{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007318 struct iwl3945_priv *priv = (struct iwl3945_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07007319 u32 flags = simple_strtoul(buf, NULL, 0);
7320
7321 mutex_lock(&priv->mutex);
7322 if (le32_to_cpu(priv->staging_rxon.flags) != flags) {
7323 /* Cancel any currently running scans... */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007324 if (iwl3945_scan_cancel_timeout(priv, 100))
Zhu Yib481de92007-09-25 17:54:57 -07007325 IWL_WARNING("Could not cancel scan.\n");
7326 else {
7327 IWL_DEBUG_INFO("Committing rxon.flags = 0x%04X\n",
7328 flags);
7329 priv->staging_rxon.flags = cpu_to_le32(flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007330 iwl3945_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007331 }
7332 }
7333 mutex_unlock(&priv->mutex);
7334
7335 return count;
7336}
7337
7338static DEVICE_ATTR(flags, S_IWUSR | S_IRUGO, show_flags, store_flags);
7339
7340static ssize_t show_filter_flags(struct device *d,
7341 struct device_attribute *attr, char *buf)
7342{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007343 struct iwl3945_priv *priv = (struct iwl3945_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07007344
7345 return sprintf(buf, "0x%04X\n",
7346 le32_to_cpu(priv->active_rxon.filter_flags));
7347}
7348
7349static ssize_t store_filter_flags(struct device *d,
7350 struct device_attribute *attr,
7351 const char *buf, size_t count)
7352{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007353 struct iwl3945_priv *priv = (struct iwl3945_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07007354 u32 filter_flags = simple_strtoul(buf, NULL, 0);
7355
7356 mutex_lock(&priv->mutex);
7357 if (le32_to_cpu(priv->staging_rxon.filter_flags) != filter_flags) {
7358 /* Cancel any currently running scans... */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007359 if (iwl3945_scan_cancel_timeout(priv, 100))
Zhu Yib481de92007-09-25 17:54:57 -07007360 IWL_WARNING("Could not cancel scan.\n");
7361 else {
7362 IWL_DEBUG_INFO("Committing rxon.filter_flags = "
7363 "0x%04X\n", filter_flags);
7364 priv->staging_rxon.filter_flags =
7365 cpu_to_le32(filter_flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007366 iwl3945_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007367 }
7368 }
7369 mutex_unlock(&priv->mutex);
7370
7371 return count;
7372}
7373
7374static DEVICE_ATTR(filter_flags, S_IWUSR | S_IRUGO, show_filter_flags,
7375 store_filter_flags);
7376
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08007377#ifdef CONFIG_IWL3945_SPECTRUM_MEASUREMENT
Zhu Yib481de92007-09-25 17:54:57 -07007378
7379static ssize_t show_measurement(struct device *d,
7380 struct device_attribute *attr, char *buf)
7381{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007382 struct iwl3945_priv *priv = dev_get_drvdata(d);
Tomas Winkler600c0e12008-12-19 10:37:04 +08007383 struct iwl_spectrum_notification measure_report;
Zhu Yib481de92007-09-25 17:54:57 -07007384 u32 size = sizeof(measure_report), len = 0, ofs = 0;
Tomas Winkler3ac7f142008-07-21 02:40:14 +03007385 u8 *data = (u8 *)&measure_report;
Zhu Yib481de92007-09-25 17:54:57 -07007386 unsigned long flags;
7387
7388 spin_lock_irqsave(&priv->lock, flags);
7389 if (!(priv->measurement_status & MEASUREMENT_READY)) {
7390 spin_unlock_irqrestore(&priv->lock, flags);
7391 return 0;
7392 }
7393 memcpy(&measure_report, &priv->measure_report, size);
7394 priv->measurement_status = 0;
7395 spin_unlock_irqrestore(&priv->lock, flags);
7396
7397 while (size && (PAGE_SIZE - len)) {
7398 hex_dump_to_buffer(data + ofs, size, 16, 1, buf + len,
7399 PAGE_SIZE - len, 1);
7400 len = strlen(buf);
7401 if (PAGE_SIZE - len)
7402 buf[len++] = '\n';
7403
7404 ofs += 16;
7405 size -= min(size, 16U);
7406 }
7407
7408 return len;
7409}
7410
7411static ssize_t store_measurement(struct device *d,
7412 struct device_attribute *attr,
7413 const char *buf, size_t count)
7414{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007415 struct iwl3945_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07007416 struct ieee80211_measurement_params params = {
7417 .channel = le16_to_cpu(priv->active_rxon.channel),
7418 .start_time = cpu_to_le64(priv->last_tsf),
7419 .duration = cpu_to_le16(1),
7420 };
7421 u8 type = IWL_MEASURE_BASIC;
7422 u8 buffer[32];
7423 u8 channel;
7424
7425 if (count) {
7426 char *p = buffer;
7427 strncpy(buffer, buf, min(sizeof(buffer), count));
7428 channel = simple_strtoul(p, NULL, 0);
7429 if (channel)
7430 params.channel = channel;
7431
7432 p = buffer;
7433 while (*p && *p != ' ')
7434 p++;
7435 if (*p)
7436 type = simple_strtoul(p + 1, NULL, 0);
7437 }
7438
7439 IWL_DEBUG_INFO("Invoking measurement of type %d on "
7440 "channel %d (for '%s')\n", type, params.channel, buf);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007441 iwl3945_get_measurement(priv, &params, type);
Zhu Yib481de92007-09-25 17:54:57 -07007442
7443 return count;
7444}
7445
7446static DEVICE_ATTR(measurement, S_IRUSR | S_IWUSR,
7447 show_measurement, store_measurement);
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08007448#endif /* CONFIG_IWL3945_SPECTRUM_MEASUREMENT */
Zhu Yib481de92007-09-25 17:54:57 -07007449
Zhu Yib481de92007-09-25 17:54:57 -07007450static ssize_t store_retry_rate(struct device *d,
7451 struct device_attribute *attr,
7452 const char *buf, size_t count)
7453{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007454 struct iwl3945_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07007455
7456 priv->retry_rate = simple_strtoul(buf, NULL, 0);
7457 if (priv->retry_rate <= 0)
7458 priv->retry_rate = 1;
7459
7460 return count;
7461}
7462
7463static ssize_t show_retry_rate(struct device *d,
7464 struct device_attribute *attr, char *buf)
7465{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007466 struct iwl3945_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07007467 return sprintf(buf, "%d", priv->retry_rate);
7468}
7469
7470static DEVICE_ATTR(retry_rate, S_IWUSR | S_IRUSR, show_retry_rate,
7471 store_retry_rate);
7472
7473static ssize_t store_power_level(struct device *d,
7474 struct device_attribute *attr,
7475 const char *buf, size_t count)
7476{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007477 struct iwl3945_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07007478 int rc;
7479 int mode;
7480
7481 mode = simple_strtoul(buf, NULL, 0);
7482 mutex_lock(&priv->mutex);
7483
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007484 if (!iwl3945_is_ready(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07007485 rc = -EAGAIN;
7486 goto out;
7487 }
7488
Samuel Ortiz1125eff2008-12-19 10:37:14 +08007489 if ((mode < 1) || (mode > IWL39_POWER_LIMIT) ||
7490 (mode == IWL39_POWER_AC))
7491 mode = IWL39_POWER_AC;
Zhu Yib481de92007-09-25 17:54:57 -07007492 else
7493 mode |= IWL_POWER_ENABLED;
7494
7495 if (mode != priv->power_mode) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007496 rc = iwl3945_send_power_mode(priv, IWL_POWER_LEVEL(mode));
Zhu Yib481de92007-09-25 17:54:57 -07007497 if (rc) {
7498 IWL_DEBUG_MAC80211("failed setting power mode.\n");
7499 goto out;
7500 }
7501 priv->power_mode = mode;
7502 }
7503
7504 rc = count;
7505
7506 out:
7507 mutex_unlock(&priv->mutex);
7508 return rc;
7509}
7510
7511#define MAX_WX_STRING 80
7512
7513/* Values are in microsecond */
7514static const s32 timeout_duration[] = {
7515 350000,
7516 250000,
7517 75000,
7518 37000,
7519 25000,
7520};
7521static const s32 period_duration[] = {
7522 400000,
7523 700000,
7524 1000000,
7525 1000000,
7526 1000000
7527};
7528
7529static ssize_t show_power_level(struct device *d,
7530 struct device_attribute *attr, char *buf)
7531{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007532 struct iwl3945_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07007533 int level = IWL_POWER_LEVEL(priv->power_mode);
7534 char *p = buf;
7535
7536 p += sprintf(p, "%d ", level);
7537 switch (level) {
7538 case IWL_POWER_MODE_CAM:
Samuel Ortiz1125eff2008-12-19 10:37:14 +08007539 case IWL39_POWER_AC:
Zhu Yib481de92007-09-25 17:54:57 -07007540 p += sprintf(p, "(AC)");
7541 break;
Samuel Ortiz1125eff2008-12-19 10:37:14 +08007542 case IWL39_POWER_BATTERY:
Zhu Yib481de92007-09-25 17:54:57 -07007543 p += sprintf(p, "(BATTERY)");
7544 break;
7545 default:
7546 p += sprintf(p,
7547 "(Timeout %dms, Period %dms)",
7548 timeout_duration[level - 1] / 1000,
7549 period_duration[level - 1] / 1000);
7550 }
7551
7552 if (!(priv->power_mode & IWL_POWER_ENABLED))
7553 p += sprintf(p, " OFF\n");
7554 else
7555 p += sprintf(p, " \n");
7556
Tomas Winkler3ac7f142008-07-21 02:40:14 +03007557 return p - buf + 1;
Zhu Yib481de92007-09-25 17:54:57 -07007558
7559}
7560
7561static DEVICE_ATTR(power_level, S_IWUSR | S_IRUSR, show_power_level,
7562 store_power_level);
7563
7564static ssize_t show_channels(struct device *d,
7565 struct device_attribute *attr, char *buf)
7566{
Johannes Berg8318d782008-01-24 19:38:38 +01007567 /* all this shit doesn't belong into sysfs anyway */
7568 return 0;
Zhu Yib481de92007-09-25 17:54:57 -07007569}
7570
7571static DEVICE_ATTR(channels, S_IRUSR, show_channels, NULL);
7572
7573static ssize_t show_statistics(struct device *d,
7574 struct device_attribute *attr, char *buf)
7575{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007576 struct iwl3945_priv *priv = dev_get_drvdata(d);
7577 u32 size = sizeof(struct iwl3945_notif_statistics);
Zhu Yib481de92007-09-25 17:54:57 -07007578 u32 len = 0, ofs = 0;
Tomas Winkler3ac7f142008-07-21 02:40:14 +03007579 u8 *data = (u8 *)&priv->statistics;
Zhu Yib481de92007-09-25 17:54:57 -07007580 int rc = 0;
7581
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007582 if (!iwl3945_is_alive(priv))
Zhu Yib481de92007-09-25 17:54:57 -07007583 return -EAGAIN;
7584
7585 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007586 rc = iwl3945_send_statistics_request(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007587 mutex_unlock(&priv->mutex);
7588
7589 if (rc) {
7590 len = sprintf(buf,
7591 "Error sending statistics request: 0x%08X\n", rc);
7592 return len;
7593 }
7594
7595 while (size && (PAGE_SIZE - len)) {
7596 hex_dump_to_buffer(data + ofs, size, 16, 1, buf + len,
7597 PAGE_SIZE - len, 1);
7598 len = strlen(buf);
7599 if (PAGE_SIZE - len)
7600 buf[len++] = '\n';
7601
7602 ofs += 16;
7603 size -= min(size, 16U);
7604 }
7605
7606 return len;
7607}
7608
7609static DEVICE_ATTR(statistics, S_IRUGO, show_statistics, NULL);
7610
7611static ssize_t show_antenna(struct device *d,
7612 struct device_attribute *attr, char *buf)
7613{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007614 struct iwl3945_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07007615
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007616 if (!iwl3945_is_alive(priv))
Zhu Yib481de92007-09-25 17:54:57 -07007617 return -EAGAIN;
7618
7619 return sprintf(buf, "%d\n", priv->antenna);
7620}
7621
7622static ssize_t store_antenna(struct device *d,
7623 struct device_attribute *attr,
7624 const char *buf, size_t count)
7625{
7626 int ant;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007627 struct iwl3945_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07007628
7629 if (count == 0)
7630 return 0;
7631
7632 if (sscanf(buf, "%1i", &ant) != 1) {
7633 IWL_DEBUG_INFO("not in hex or decimal form.\n");
7634 return count;
7635 }
7636
7637 if ((ant >= 0) && (ant <= 2)) {
7638 IWL_DEBUG_INFO("Setting antenna select to %d.\n", ant);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007639 priv->antenna = (enum iwl3945_antenna)ant;
Zhu Yib481de92007-09-25 17:54:57 -07007640 } else
7641 IWL_DEBUG_INFO("Bad antenna select value %d.\n", ant);
7642
7643
7644 return count;
7645}
7646
7647static DEVICE_ATTR(antenna, S_IWUSR | S_IRUGO, show_antenna, store_antenna);
7648
7649static ssize_t show_status(struct device *d,
7650 struct device_attribute *attr, char *buf)
7651{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007652 struct iwl3945_priv *priv = (struct iwl3945_priv *)d->driver_data;
7653 if (!iwl3945_is_alive(priv))
Zhu Yib481de92007-09-25 17:54:57 -07007654 return -EAGAIN;
7655 return sprintf(buf, "0x%08x\n", (int)priv->status);
7656}
7657
7658static DEVICE_ATTR(status, S_IRUGO, show_status, NULL);
7659
7660static ssize_t dump_error_log(struct device *d,
7661 struct device_attribute *attr,
7662 const char *buf, size_t count)
7663{
7664 char *p = (char *)buf;
7665
7666 if (p[0] == '1')
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007667 iwl3945_dump_nic_error_log((struct iwl3945_priv *)d->driver_data);
Zhu Yib481de92007-09-25 17:54:57 -07007668
7669 return strnlen(buf, count);
7670}
7671
7672static DEVICE_ATTR(dump_errors, S_IWUSR, NULL, dump_error_log);
7673
7674static ssize_t dump_event_log(struct device *d,
7675 struct device_attribute *attr,
7676 const char *buf, size_t count)
7677{
7678 char *p = (char *)buf;
7679
7680 if (p[0] == '1')
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007681 iwl3945_dump_nic_event_log((struct iwl3945_priv *)d->driver_data);
Zhu Yib481de92007-09-25 17:54:57 -07007682
7683 return strnlen(buf, count);
7684}
7685
7686static DEVICE_ATTR(dump_events, S_IWUSR, NULL, dump_event_log);
7687
7688/*****************************************************************************
7689 *
Tomas Winklera96a27f2008-10-23 23:48:56 -07007690 * driver setup and tear down
Zhu Yib481de92007-09-25 17:54:57 -07007691 *
7692 *****************************************************************************/
7693
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007694static void iwl3945_setup_deferred_work(struct iwl3945_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07007695{
7696 priv->workqueue = create_workqueue(DRV_NAME);
7697
7698 init_waitqueue_head(&priv->wait_command_queue);
7699
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007700 INIT_WORK(&priv->up, iwl3945_bg_up);
7701 INIT_WORK(&priv->restart, iwl3945_bg_restart);
7702 INIT_WORK(&priv->rx_replenish, iwl3945_bg_rx_replenish);
7703 INIT_WORK(&priv->scan_completed, iwl3945_bg_scan_completed);
7704 INIT_WORK(&priv->request_scan, iwl3945_bg_request_scan);
7705 INIT_WORK(&priv->abort_scan, iwl3945_bg_abort_scan);
7706 INIT_WORK(&priv->rf_kill, iwl3945_bg_rf_kill);
7707 INIT_WORK(&priv->beacon_update, iwl3945_bg_beacon_update);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007708 INIT_DELAYED_WORK(&priv->init_alive_start, iwl3945_bg_init_alive_start);
7709 INIT_DELAYED_WORK(&priv->alive_start, iwl3945_bg_alive_start);
7710 INIT_DELAYED_WORK(&priv->scan_check, iwl3945_bg_scan_check);
Zhu Yib481de92007-09-25 17:54:57 -07007711
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007712 iwl3945_hw_setup_deferred_work(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007713
7714 tasklet_init(&priv->irq_tasklet, (void (*)(unsigned long))
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007715 iwl3945_irq_tasklet, (unsigned long)priv);
Zhu Yib481de92007-09-25 17:54:57 -07007716}
7717
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007718static void iwl3945_cancel_deferred_work(struct iwl3945_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07007719{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007720 iwl3945_hw_cancel_deferred_work(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007721
Joonwoo Parke47eb6a2007-11-29 10:42:49 +09007722 cancel_delayed_work_sync(&priv->init_alive_start);
Zhu Yib481de92007-09-25 17:54:57 -07007723 cancel_delayed_work(&priv->scan_check);
7724 cancel_delayed_work(&priv->alive_start);
Zhu Yib481de92007-09-25 17:54:57 -07007725 cancel_work_sync(&priv->beacon_update);
7726}
7727
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007728static struct attribute *iwl3945_sysfs_entries[] = {
Zhu Yib481de92007-09-25 17:54:57 -07007729 &dev_attr_antenna.attr,
7730 &dev_attr_channels.attr,
7731 &dev_attr_dump_errors.attr,
7732 &dev_attr_dump_events.attr,
7733 &dev_attr_flags.attr,
7734 &dev_attr_filter_flags.attr,
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08007735#ifdef CONFIG_IWL3945_SPECTRUM_MEASUREMENT
Zhu Yib481de92007-09-25 17:54:57 -07007736 &dev_attr_measurement.attr,
7737#endif
7738 &dev_attr_power_level.attr,
Zhu Yib481de92007-09-25 17:54:57 -07007739 &dev_attr_retry_rate.attr,
Zhu Yib481de92007-09-25 17:54:57 -07007740 &dev_attr_statistics.attr,
7741 &dev_attr_status.attr,
7742 &dev_attr_temperature.attr,
Zhu Yib481de92007-09-25 17:54:57 -07007743 &dev_attr_tx_power.attr,
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08007744#ifdef CONFIG_IWL3945_DEBUG
7745 &dev_attr_debug_level.attr,
7746#endif
Zhu Yib481de92007-09-25 17:54:57 -07007747 NULL
7748};
7749
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007750static struct attribute_group iwl3945_attribute_group = {
Zhu Yib481de92007-09-25 17:54:57 -07007751 .name = NULL, /* put in device directory */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007752 .attrs = iwl3945_sysfs_entries,
Zhu Yib481de92007-09-25 17:54:57 -07007753};
7754
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007755static struct ieee80211_ops iwl3945_hw_ops = {
7756 .tx = iwl3945_mac_tx,
7757 .start = iwl3945_mac_start,
7758 .stop = iwl3945_mac_stop,
7759 .add_interface = iwl3945_mac_add_interface,
7760 .remove_interface = iwl3945_mac_remove_interface,
7761 .config = iwl3945_mac_config,
7762 .config_interface = iwl3945_mac_config_interface,
7763 .configure_filter = iwl3945_configure_filter,
7764 .set_key = iwl3945_mac_set_key,
7765 .get_stats = iwl3945_mac_get_stats,
7766 .get_tx_stats = iwl3945_mac_get_tx_stats,
7767 .conf_tx = iwl3945_mac_conf_tx,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007768 .reset_tsf = iwl3945_mac_reset_tsf,
Abhijeet Kolekarcd56d332008-09-03 11:26:21 +08007769 .bss_info_changed = iwl3945_bss_info_changed,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007770 .hw_scan = iwl3945_mac_hw_scan
Zhu Yib481de92007-09-25 17:54:57 -07007771};
7772
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007773static int iwl3945_pci_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
Zhu Yib481de92007-09-25 17:54:57 -07007774{
7775 int err = 0;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007776 struct iwl3945_priv *priv;
Zhu Yib481de92007-09-25 17:54:57 -07007777 struct ieee80211_hw *hw;
Tomas Winkler82b9a122008-03-04 18:09:30 -08007778 struct iwl_3945_cfg *cfg = (struct iwl_3945_cfg *)(ent->driver_data);
Mohamed Abbas0359fac2008-03-28 16:21:08 -07007779 unsigned long flags;
Zhu Yib481de92007-09-25 17:54:57 -07007780
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08007781 /***********************
7782 * 1. Allocating HW data
7783 * ********************/
7784
Zhu Yib481de92007-09-25 17:54:57 -07007785 /* mac80211 allocates memory for this device instance, including
7786 * space for this driver's private structure */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007787 hw = ieee80211_alloc_hw(sizeof(struct iwl3945_priv), &iwl3945_hw_ops);
Zhu Yib481de92007-09-25 17:54:57 -07007788 if (hw == NULL) {
Samuel Ortiza3139c52008-12-19 10:37:09 +08007789 printk(KERN_ERR DRV_NAME "Can not allocate network device\n");
Zhu Yib481de92007-09-25 17:54:57 -07007790 err = -ENOMEM;
7791 goto out;
7792 }
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08007793
Zhu Yib481de92007-09-25 17:54:57 -07007794 SET_IEEE80211_DEV(hw, &pdev->dev);
7795
Zhu Yib481de92007-09-25 17:54:57 -07007796 priv = hw->priv;
7797 priv->hw = hw;
Zhu Yib481de92007-09-25 17:54:57 -07007798 priv->pci_dev = pdev;
Tomas Winkler82b9a122008-03-04 18:09:30 -08007799 priv->cfg = cfg;
Cahill, Ben M6440adb2007-11-29 11:09:55 +08007800
Samuel Ortiza3139c52008-12-19 10:37:09 +08007801 if ((iwl3945_param_queues_num > IWL39_MAX_NUM_QUEUES) ||
7802 (iwl3945_param_queues_num < IWL_MIN_NUM_QUEUES)) {
7803 IWL_ERROR("invalid queues_num, should be between %d and %d\n",
7804 IWL_MIN_NUM_QUEUES, IWL39_MAX_NUM_QUEUES);
7805 err = -EINVAL;
7806 goto out;
7807 }
7808
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08007809 /* Disabling hardware scan means that mac80211 will perform scans
7810 * "the hard way", rather than using device's scan. */
7811 if (iwl3945_param_disable_hw_scan) {
7812 IWL_DEBUG_INFO("Disabling hw_scan\n");
7813 iwl3945_hw_ops.hw_scan = NULL;
7814 }
7815
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08007816 IWL_DEBUG_INFO("*** LOAD DRIVER ***\n");
7817 hw->rate_control_algorithm = "iwl-3945-rs";
7818 hw->sta_data_size = sizeof(struct iwl3945_sta_priv);
7819
Cahill, Ben M6440adb2007-11-29 11:09:55 +08007820 /* Select antenna (may be helpful if only one antenna is connected) */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007821 priv->antenna = (enum iwl3945_antenna)iwl3945_param_antenna;
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08007822#ifdef CONFIG_IWL3945_DEBUG
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08007823 priv->debug_level = iwl3945_param_debug;
Zhu Yib481de92007-09-25 17:54:57 -07007824 atomic_set(&priv->restrict_refcnt, 0);
7825#endif
Zhu Yib481de92007-09-25 17:54:57 -07007826
Bruno Randolf566bfe52008-05-08 19:15:40 +02007827 /* Tell mac80211 our characteristics */
Johannes Berg605a0bd2008-07-15 10:10:01 +02007828 hw->flags = IEEE80211_HW_SIGNAL_DBM |
Bruno Randolf566bfe52008-05-08 19:15:40 +02007829 IEEE80211_HW_NOISE_DBM;
Zhu Yib481de92007-09-25 17:54:57 -07007830
Luis R. Rodriguezf59ac042008-08-29 16:26:43 -07007831 hw->wiphy->interface_modes =
Luis R. Rodriguezf59ac042008-08-29 16:26:43 -07007832 BIT(NL80211_IFTYPE_STATION) |
7833 BIT(NL80211_IFTYPE_ADHOC);
7834
Luis R. Rodriguezea4a82dc2008-11-12 14:22:04 -08007835 hw->wiphy->fw_handles_regulatory = true;
7836
Cahill, Ben M6440adb2007-11-29 11:09:55 +08007837 /* 4 EDCA QOS priorities */
Zhu Yib481de92007-09-25 17:54:57 -07007838 hw->queues = 4;
7839
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08007840 /***************************
7841 * 2. Initializing PCI bus
7842 * *************************/
Zhu Yib481de92007-09-25 17:54:57 -07007843 if (pci_enable_device(pdev)) {
7844 err = -ENODEV;
7845 goto out_ieee80211_free_hw;
7846 }
7847
7848 pci_set_master(pdev);
7849
Zhu Yib481de92007-09-25 17:54:57 -07007850 err = pci_set_dma_mask(pdev, DMA_32BIT_MASK);
7851 if (!err)
7852 err = pci_set_consistent_dma_mask(pdev, DMA_32BIT_MASK);
7853 if (err) {
7854 printk(KERN_WARNING DRV_NAME ": No suitable DMA available.\n");
7855 goto out_pci_disable_device;
7856 }
7857
7858 pci_set_drvdata(pdev, priv);
7859 err = pci_request_regions(pdev, DRV_NAME);
7860 if (err)
7861 goto out_pci_disable_device;
Cahill, Ben M6440adb2007-11-29 11:09:55 +08007862
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08007863 /***********************
7864 * 3. Read REV Register
7865 * ********************/
Zhu Yib481de92007-09-25 17:54:57 -07007866 priv->hw_base = pci_iomap(pdev, 0, 0);
7867 if (!priv->hw_base) {
7868 err = -ENODEV;
7869 goto out_pci_release_regions;
7870 }
7871
7872 IWL_DEBUG_INFO("pci_resource_len = 0x%08llx\n",
7873 (unsigned long long) pci_resource_len(pdev, 0));
7874 IWL_DEBUG_INFO("pci_resource_base = %p\n", priv->hw_base);
7875
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08007876 /* We disable the RETRY_TIMEOUT register (0x41) to keep
7877 * PCI Tx retries from interfering with C3 CPU state */
7878 pci_write_config_byte(pdev, 0x41, 0x00);
Zhu Yib481de92007-09-25 17:54:57 -07007879
Zhu Yi5a669262008-01-14 17:46:18 -08007880 /* nic init */
7881 iwl3945_set_bit(priv, CSR_GIO_CHICKEN_BITS,
Tomas Winkler3ac7f142008-07-21 02:40:14 +03007882 CSR_GIO_CHICKEN_BITS_REG_BIT_DIS_L0S_EXIT_TIMER);
Zhu Yi5a669262008-01-14 17:46:18 -08007883
Tomas Winkler3ac7f142008-07-21 02:40:14 +03007884 iwl3945_set_bit(priv, CSR_GP_CNTRL, CSR_GP_CNTRL_REG_FLAG_INIT_DONE);
Zhu, Yi73d7b5a2008-12-05 07:58:40 -08007885 err = iwl3945_poll_direct_bit(priv, CSR_GP_CNTRL,
7886 CSR_GP_CNTRL_REG_FLAG_MAC_CLOCK_READY, 25000);
Tomas Winkler3ac7f142008-07-21 02:40:14 +03007887 if (err < 0) {
7888 IWL_DEBUG_INFO("Failed to init the card\n");
Zhu Yi5a669262008-01-14 17:46:18 -08007889 goto out_remove_sysfs;
Tomas Winkler3ac7f142008-07-21 02:40:14 +03007890 }
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08007891
7892 /***********************
7893 * 4. Read EEPROM
7894 * ********************/
Zhu Yi5a669262008-01-14 17:46:18 -08007895 /* Read the EEPROM */
7896 err = iwl3945_eeprom_init(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007897 if (err) {
Zhu Yi5a669262008-01-14 17:46:18 -08007898 IWL_ERROR("Unable to init EEPROM\n");
7899 goto out_remove_sysfs;
7900 }
7901 /* MAC Address location in EEPROM same for 3945/4965 */
7902 get_eeprom_mac(priv, priv->mac_addr);
Johannes Berge1749612008-10-27 15:59:26 -07007903 IWL_DEBUG_INFO("MAC address: %pM\n", priv->mac_addr);
Zhu Yi5a669262008-01-14 17:46:18 -08007904 SET_IEEE80211_PERM_ADDR(priv->hw, priv->mac_addr);
7905
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08007906 /***********************
7907 * 5. Setup HW Constants
7908 * ********************/
7909 /* Device-specific setup */
7910 if (iwl3945_hw_set_hw_setting(priv)) {
7911 IWL_ERROR("failed to set hw settings\n");
7912 goto out_iounmap;
7913 }
7914
7915 /***********************
7916 * 6. Setup priv
7917 * ********************/
7918 priv->retry_rate = 1;
7919 priv->ibss_beacon = NULL;
7920
7921 spin_lock_init(&priv->lock);
7922 spin_lock_init(&priv->power_data.lock);
7923 spin_lock_init(&priv->sta_lock);
7924 spin_lock_init(&priv->hcmd_lock);
7925
7926 INIT_LIST_HEAD(&priv->free_frames);
7927 mutex_init(&priv->mutex);
7928
7929 /* Clear the driver's (not device's) station table */
7930 iwl3945_clear_stations_table(priv);
7931
7932 priv->data_retry_limit = -1;
7933 priv->ieee_channels = NULL;
7934 priv->ieee_rates = NULL;
7935 priv->band = IEEE80211_BAND_2GHZ;
7936
7937 priv->iw_mode = NL80211_IFTYPE_STATION;
7938
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08007939 iwl3945_reset_qos(priv);
7940
7941 priv->qos_data.qos_active = 0;
7942 priv->qos_data.qos_cap.val = 0;
7943
7944
7945 priv->rates_mask = IWL_RATES_MASK;
7946 /* If power management is turned on, default to AC mode */
Samuel Ortiz1125eff2008-12-19 10:37:14 +08007947 priv->power_mode = IWL39_POWER_AC;
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08007948 priv->user_txpower_limit = IWL_DEFAULT_TX_POWER;
7949
Reinette Chatre849e0dc2008-01-23 10:15:18 -08007950 err = iwl3945_init_channel_map(priv);
7951 if (err) {
7952 IWL_ERROR("initializing regulatory failed: %d\n", err);
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08007953 goto out_release_irq;
Reinette Chatre849e0dc2008-01-23 10:15:18 -08007954 }
7955
7956 err = iwl3945_init_geos(priv);
7957 if (err) {
7958 IWL_ERROR("initializing geos failed: %d\n", err);
7959 goto out_free_channel_map;
7960 }
Reinette Chatre849e0dc2008-01-23 10:15:18 -08007961
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08007962 printk(KERN_INFO DRV_NAME
7963 ": Detected Intel Wireless WiFi Link %s\n", priv->cfg->name);
7964
7965 /***********************************
7966 * 7. Initialize Module Parameters
7967 * **********************************/
7968
7969 /* Initialize module parameter values here */
7970 /* Disable radio (SW RF KILL) via parameter when loading driver */
7971 if (iwl3945_param_disable) {
7972 set_bit(STATUS_RF_KILL_SW, &priv->status);
7973 IWL_DEBUG_INFO("Radio disabled.\n");
7974 }
7975
7976
7977 /***********************
7978 * 8. Setup Services
7979 * ********************/
7980
7981 spin_lock_irqsave(&priv->lock, flags);
7982 iwl3945_disable_interrupts(priv);
7983 spin_unlock_irqrestore(&priv->lock, flags);
7984
7985 err = sysfs_create_group(&pdev->dev.kobj, &iwl3945_attribute_group);
7986 if (err) {
7987 IWL_ERROR("failed to create sysfs device attributes\n");
7988 goto out_free_geos;
7989 }
7990
7991 iwl3945_set_rxon_channel(priv, IEEE80211_BAND_2GHZ, 6);
7992 iwl3945_setup_deferred_work(priv);
7993 iwl3945_setup_rx_handlers(priv);
7994
7995 /***********************
7996 * 9. Conclude
7997 * ********************/
7998 pci_save_state(pdev);
7999 pci_disable_device(pdev);
8000
8001 /*********************************
8002 * 10. Setup and Register mac80211
8003 * *******************************/
8004
Zhu Yi5a669262008-01-14 17:46:18 -08008005 err = ieee80211_register_hw(priv->hw);
8006 if (err) {
8007 IWL_ERROR("Failed to register network device (error %d)\n", err);
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08008008 goto out_remove_sysfs;
Zhu Yib481de92007-09-25 17:54:57 -07008009 }
8010
Zhu Yi5a669262008-01-14 17:46:18 -08008011 priv->hw->conf.beacon_int = 100;
8012 priv->mac80211_registered = 1;
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08008013
Zhu Yib481de92007-09-25 17:54:57 -07008014
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08008015 err = iwl3945_rfkill_init(priv);
8016 if (err)
8017 IWL_ERROR("Unable to initialize RFKILL system. "
8018 "Ignoring error: %d\n", err);
8019
Zhu Yib481de92007-09-25 17:54:57 -07008020 return 0;
8021
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08008022 out_remove_sysfs:
8023 sysfs_remove_group(&pdev->dev.kobj, &iwl3945_attribute_group);
Reinette Chatre849e0dc2008-01-23 10:15:18 -08008024 out_free_geos:
8025 iwl3945_free_geos(priv);
8026 out_free_channel_map:
8027 iwl3945_free_channel_map(priv);
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08008028
Zhu Yib481de92007-09-25 17:54:57 -07008029
8030 out_release_irq:
Zhu Yib481de92007-09-25 17:54:57 -07008031 destroy_workqueue(priv->workqueue);
8032 priv->workqueue = NULL;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008033 iwl3945_unset_hw_setting(priv);
Zhu Yib481de92007-09-25 17:54:57 -07008034
8035 out_iounmap:
8036 pci_iounmap(pdev, priv->hw_base);
8037 out_pci_release_regions:
8038 pci_release_regions(pdev);
8039 out_pci_disable_device:
8040 pci_disable_device(pdev);
8041 pci_set_drvdata(pdev, NULL);
8042 out_ieee80211_free_hw:
8043 ieee80211_free_hw(priv->hw);
8044 out:
8045 return err;
8046}
8047
Reinette Chatrec83dbf62008-03-21 13:53:41 -07008048static void __devexit iwl3945_pci_remove(struct pci_dev *pdev)
Zhu Yib481de92007-09-25 17:54:57 -07008049{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008050 struct iwl3945_priv *priv = pci_get_drvdata(pdev);
Mohamed Abbas0359fac2008-03-28 16:21:08 -07008051 unsigned long flags;
Zhu Yib481de92007-09-25 17:54:57 -07008052
8053 if (!priv)
8054 return;
8055
8056 IWL_DEBUG_INFO("*** UNLOAD DRIVER ***\n");
8057
Zhu Yib481de92007-09-25 17:54:57 -07008058 set_bit(STATUS_EXIT_PENDING, &priv->status);
Zhu Yib24d22b2007-12-19 13:59:52 +08008059
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008060 iwl3945_down(priv);
Zhu Yib481de92007-09-25 17:54:57 -07008061
Mohamed Abbas0359fac2008-03-28 16:21:08 -07008062 /* make sure we flush any pending irq or
8063 * tasklet for the driver
8064 */
8065 spin_lock_irqsave(&priv->lock, flags);
8066 iwl3945_disable_interrupts(priv);
8067 spin_unlock_irqrestore(&priv->lock, flags);
8068
8069 iwl_synchronize_irq(priv);
8070
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008071 sysfs_remove_group(&pdev->dev.kobj, &iwl3945_attribute_group);
Zhu Yib481de92007-09-25 17:54:57 -07008072
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08008073 iwl3945_rfkill_unregister(priv);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008074 iwl3945_dealloc_ucode_pci(priv);
Zhu Yib481de92007-09-25 17:54:57 -07008075
8076 if (priv->rxq.bd)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008077 iwl3945_rx_queue_free(priv, &priv->rxq);
8078 iwl3945_hw_txq_ctx_free(priv);
Zhu Yib481de92007-09-25 17:54:57 -07008079
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008080 iwl3945_unset_hw_setting(priv);
8081 iwl3945_clear_stations_table(priv);
Zhu Yib481de92007-09-25 17:54:57 -07008082
Tomas Winkler3ac7f142008-07-21 02:40:14 +03008083 if (priv->mac80211_registered)
Zhu Yib481de92007-09-25 17:54:57 -07008084 ieee80211_unregister_hw(priv->hw);
Zhu Yib481de92007-09-25 17:54:57 -07008085
Mohamed Abbas6ef89d02007-10-25 17:15:47 +08008086 /*netif_stop_queue(dev); */
8087 flush_workqueue(priv->workqueue);
8088
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008089 /* ieee80211_unregister_hw calls iwl3945_mac_stop, which flushes
Zhu Yib481de92007-09-25 17:54:57 -07008090 * priv->workqueue... so we can't take down the workqueue
8091 * until now... */
8092 destroy_workqueue(priv->workqueue);
8093 priv->workqueue = NULL;
8094
Zhu Yib481de92007-09-25 17:54:57 -07008095 pci_iounmap(pdev, priv->hw_base);
8096 pci_release_regions(pdev);
8097 pci_disable_device(pdev);
8098 pci_set_drvdata(pdev, NULL);
8099
Reinette Chatre849e0dc2008-01-23 10:15:18 -08008100 iwl3945_free_channel_map(priv);
8101 iwl3945_free_geos(priv);
Emmanuel Grumbach261415f2008-05-29 16:35:25 +08008102 kfree(priv->scan);
Zhu Yib481de92007-09-25 17:54:57 -07008103 if (priv->ibss_beacon)
8104 dev_kfree_skb(priv->ibss_beacon);
8105
8106 ieee80211_free_hw(priv->hw);
8107}
8108
8109#ifdef CONFIG_PM
8110
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008111static int iwl3945_pci_suspend(struct pci_dev *pdev, pm_message_t state)
Zhu Yib481de92007-09-25 17:54:57 -07008112{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008113 struct iwl3945_priv *priv = pci_get_drvdata(pdev);
Zhu Yib481de92007-09-25 17:54:57 -07008114
Zhu Yie655b9f2008-01-24 02:19:38 -08008115 if (priv->is_open) {
8116 set_bit(STATUS_IN_SUSPEND, &priv->status);
8117 iwl3945_mac_stop(priv->hw);
8118 priv->is_open = 1;
8119 }
Zhu Yib481de92007-09-25 17:54:57 -07008120
Zhu Yib481de92007-09-25 17:54:57 -07008121 pci_set_power_state(pdev, PCI_D3hot);
8122
Zhu Yib481de92007-09-25 17:54:57 -07008123 return 0;
8124}
8125
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008126static int iwl3945_pci_resume(struct pci_dev *pdev)
Zhu Yib481de92007-09-25 17:54:57 -07008127{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008128 struct iwl3945_priv *priv = pci_get_drvdata(pdev);
Zhu Yib481de92007-09-25 17:54:57 -07008129
Zhu Yib481de92007-09-25 17:54:57 -07008130 pci_set_power_state(pdev, PCI_D0);
Zhu Yib481de92007-09-25 17:54:57 -07008131
Zhu Yie655b9f2008-01-24 02:19:38 -08008132 if (priv->is_open)
8133 iwl3945_mac_start(priv->hw);
Zhu Yib481de92007-09-25 17:54:57 -07008134
Zhu Yie655b9f2008-01-24 02:19:38 -08008135 clear_bit(STATUS_IN_SUSPEND, &priv->status);
Zhu Yib481de92007-09-25 17:54:57 -07008136 return 0;
8137}
8138
8139#endif /* CONFIG_PM */
8140
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08008141/*************** RFKILL FUNCTIONS **********/
Adel Gadllah80fcc9e2008-07-01 17:49:50 +02008142#ifdef CONFIG_IWL3945_RFKILL
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08008143/* software rf-kill from user */
8144static int iwl3945_rfkill_soft_rf_kill(void *data, enum rfkill_state state)
8145{
8146 struct iwl3945_priv *priv = data;
8147 int err = 0;
8148
Adel Gadllah80fcc9e2008-07-01 17:49:50 +02008149 if (!priv->rfkill)
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08008150 return 0;
8151
8152 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
8153 return 0;
8154
Tomas Winklera96a27f2008-10-23 23:48:56 -07008155 IWL_DEBUG_RF_KILL("we received soft RFKILL set to state %d\n", state);
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08008156 mutex_lock(&priv->mutex);
8157
8158 switch (state) {
Zhu Yiacdfe9b2008-06-30 17:23:32 +08008159 case RFKILL_STATE_UNBLOCKED:
Adel Gadllah80fcc9e2008-07-01 17:49:50 +02008160 if (iwl3945_is_rfkill_hw(priv)) {
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08008161 err = -EBUSY;
Adel Gadllah80fcc9e2008-07-01 17:49:50 +02008162 goto out_unlock;
8163 }
8164 iwl3945_radio_kill_sw(priv, 0);
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08008165 break;
Zhu Yiacdfe9b2008-06-30 17:23:32 +08008166 case RFKILL_STATE_SOFT_BLOCKED:
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08008167 iwl3945_radio_kill_sw(priv, 1);
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08008168 break;
Zhu Yiacdfe9b2008-06-30 17:23:32 +08008169 default:
Tomas Winklera96a27f2008-10-23 23:48:56 -07008170 IWL_WARNING("we received unexpected RFKILL state %d\n", state);
Zhu Yiacdfe9b2008-06-30 17:23:32 +08008171 break;
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08008172 }
Adel Gadllah80fcc9e2008-07-01 17:49:50 +02008173out_unlock:
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08008174 mutex_unlock(&priv->mutex);
8175
8176 return err;
8177}
8178
8179int iwl3945_rfkill_init(struct iwl3945_priv *priv)
8180{
8181 struct device *device = wiphy_dev(priv->hw->wiphy);
8182 int ret = 0;
8183
8184 BUG_ON(device == NULL);
8185
8186 IWL_DEBUG_RF_KILL("Initializing RFKILL.\n");
Adel Gadllah80fcc9e2008-07-01 17:49:50 +02008187 priv->rfkill = rfkill_allocate(device, RFKILL_TYPE_WLAN);
8188 if (!priv->rfkill) {
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08008189 IWL_ERROR("Unable to allocate rfkill device.\n");
8190 ret = -ENOMEM;
8191 goto error;
8192 }
8193
Adel Gadllah80fcc9e2008-07-01 17:49:50 +02008194 priv->rfkill->name = priv->cfg->name;
8195 priv->rfkill->data = priv;
8196 priv->rfkill->state = RFKILL_STATE_UNBLOCKED;
8197 priv->rfkill->toggle_radio = iwl3945_rfkill_soft_rf_kill;
8198 priv->rfkill->user_claim_unsupported = 1;
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08008199
Adel Gadllah80fcc9e2008-07-01 17:49:50 +02008200 priv->rfkill->dev.class->suspend = NULL;
8201 priv->rfkill->dev.class->resume = NULL;
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08008202
Adel Gadllah80fcc9e2008-07-01 17:49:50 +02008203 ret = rfkill_register(priv->rfkill);
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08008204 if (ret) {
8205 IWL_ERROR("Unable to register rfkill: %d\n", ret);
Adel Gadllah80fcc9e2008-07-01 17:49:50 +02008206 goto freed_rfkill;
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08008207 }
8208
8209 IWL_DEBUG_RF_KILL("RFKILL initialization complete.\n");
8210 return ret;
8211
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08008212freed_rfkill:
Adel Gadllah80fcc9e2008-07-01 17:49:50 +02008213 if (priv->rfkill != NULL)
8214 rfkill_free(priv->rfkill);
8215 priv->rfkill = NULL;
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08008216
8217error:
8218 IWL_DEBUG_RF_KILL("RFKILL initialization complete.\n");
8219 return ret;
8220}
8221
8222void iwl3945_rfkill_unregister(struct iwl3945_priv *priv)
8223{
Adel Gadllah80fcc9e2008-07-01 17:49:50 +02008224 if (priv->rfkill)
8225 rfkill_unregister(priv->rfkill);
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08008226
Adel Gadllah80fcc9e2008-07-01 17:49:50 +02008227 priv->rfkill = NULL;
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08008228}
8229
8230/* set rf-kill to the right state. */
8231void iwl3945_rfkill_set_hw_state(struct iwl3945_priv *priv)
8232{
8233
Adel Gadllah80fcc9e2008-07-01 17:49:50 +02008234 if (!priv->rfkill)
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08008235 return;
8236
Adel Gadllah80fcc9e2008-07-01 17:49:50 +02008237 if (iwl3945_is_rfkill_hw(priv)) {
8238 rfkill_force_state(priv->rfkill, RFKILL_STATE_HARD_BLOCKED);
8239 return;
8240 }
8241
8242 if (!iwl3945_is_rfkill_sw(priv))
8243 rfkill_force_state(priv->rfkill, RFKILL_STATE_UNBLOCKED);
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08008244 else
Adel Gadllah80fcc9e2008-07-01 17:49:50 +02008245 rfkill_force_state(priv->rfkill, RFKILL_STATE_SOFT_BLOCKED);
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08008246}
8247#endif
8248
Zhu Yib481de92007-09-25 17:54:57 -07008249/*****************************************************************************
8250 *
8251 * driver and module entry point
8252 *
8253 *****************************************************************************/
8254
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008255static struct pci_driver iwl3945_driver = {
Zhu Yib481de92007-09-25 17:54:57 -07008256 .name = DRV_NAME,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008257 .id_table = iwl3945_hw_card_ids,
8258 .probe = iwl3945_pci_probe,
8259 .remove = __devexit_p(iwl3945_pci_remove),
Zhu Yib481de92007-09-25 17:54:57 -07008260#ifdef CONFIG_PM
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008261 .suspend = iwl3945_pci_suspend,
8262 .resume = iwl3945_pci_resume,
Zhu Yib481de92007-09-25 17:54:57 -07008263#endif
8264};
8265
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008266static int __init iwl3945_init(void)
Zhu Yib481de92007-09-25 17:54:57 -07008267{
8268
8269 int ret;
8270 printk(KERN_INFO DRV_NAME ": " DRV_DESCRIPTION ", " DRV_VERSION "\n");
8271 printk(KERN_INFO DRV_NAME ": " DRV_COPYRIGHT "\n");
Reinette Chatre897e1cf2008-03-28 16:21:09 -07008272
8273 ret = iwl3945_rate_control_register();
8274 if (ret) {
Samuel Ortiza3139c52008-12-19 10:37:09 +08008275 printk(KERN_ERR DRV_NAME
8276 "Unable to register rate control algorithm: %d\n", ret);
Reinette Chatre897e1cf2008-03-28 16:21:09 -07008277 return ret;
8278 }
8279
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008280 ret = pci_register_driver(&iwl3945_driver);
Zhu Yib481de92007-09-25 17:54:57 -07008281 if (ret) {
Samuel Ortiza3139c52008-12-19 10:37:09 +08008282 printk(KERN_ERR DRV_NAME "Unable to initialize PCI module\n");
Reinette Chatre897e1cf2008-03-28 16:21:09 -07008283 goto error_register;
Zhu Yib481de92007-09-25 17:54:57 -07008284 }
Zhu Yib481de92007-09-25 17:54:57 -07008285
8286 return ret;
Reinette Chatre897e1cf2008-03-28 16:21:09 -07008287
Reinette Chatre897e1cf2008-03-28 16:21:09 -07008288error_register:
8289 iwl3945_rate_control_unregister();
8290 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07008291}
8292
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008293static void __exit iwl3945_exit(void)
Zhu Yib481de92007-09-25 17:54:57 -07008294{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008295 pci_unregister_driver(&iwl3945_driver);
Reinette Chatre897e1cf2008-03-28 16:21:09 -07008296 iwl3945_rate_control_unregister();
Zhu Yib481de92007-09-25 17:54:57 -07008297}
8298
Reinette Chatrea0987a82008-12-02 12:14:06 -08008299MODULE_FIRMWARE(IWL3945_MODULE_FIRMWARE(IWL3945_UCODE_API_MAX));
Zhu Yi25cb6ca2008-09-11 11:45:22 +08008300
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008301module_param_named(antenna, iwl3945_param_antenna, int, 0444);
Zhu Yib481de92007-09-25 17:54:57 -07008302MODULE_PARM_DESC(antenna, "select antenna (1=Main, 2=Aux, default 0 [both])");
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008303module_param_named(disable, iwl3945_param_disable, int, 0444);
Zhu Yib481de92007-09-25 17:54:57 -07008304MODULE_PARM_DESC(disable, "manually disable the radio (default 0 [radio on])");
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008305module_param_named(hwcrypto, iwl3945_param_hwcrypto, int, 0444);
Zhu Yib481de92007-09-25 17:54:57 -07008306MODULE_PARM_DESC(hwcrypto,
8307 "using hardware crypto engine (default 0 [software])\n");
Wu, Fengguang95aa1942008-12-17 16:52:30 +08008308module_param_named(debug, iwl3945_param_debug, uint, 0444);
Zhu Yib481de92007-09-25 17:54:57 -07008309MODULE_PARM_DESC(debug, "debug output mask");
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008310module_param_named(disable_hw_scan, iwl3945_param_disable_hw_scan, int, 0444);
Zhu Yib481de92007-09-25 17:54:57 -07008311MODULE_PARM_DESC(disable_hw_scan, "disable hardware scanning (default 0)");
8312
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008313module_param_named(queues_num, iwl3945_param_queues_num, int, 0444);
Zhu Yib481de92007-09-25 17:54:57 -07008314MODULE_PARM_DESC(queues_num, "number of hw queues.");
8315
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008316module_exit(iwl3945_exit);
8317module_init(iwl3945_init);