blob: 26f53647bf36031ab8ac178f65fba48d931647f2 [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 Winkler600c0e12008-12-19 10:37:04 +080051#include "iwl-commands.h"
Zhu Yib481de92007-09-25 17:54:57 -070052#include "iwl-3945.h"
Tomas Winklerbddadf82008-12-19 10:37:01 +080053#include "iwl-3945-fh.h"
Zhu Yib481de92007-09-25 17:54:57 -070054#include "iwl-helpers.h"
Kolekar, Abhijeet5747d472008-12-19 10:37:18 +080055#include "iwl-core.h"
Samuel Ortizd20b3c62008-12-19 10:37:15 +080056#include "iwl-dev.h"
Zhu Yib481de92007-09-25 17:54:57 -070057
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +080058static int iwl3945_tx_queue_update_write_ptr(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -080059 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
Kolekar, Abhijeeteaa686c2008-12-19 10:37:17 +080094#define IWL39_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>"
Kolekar, Abhijeeteaa686c2008-12-19 10:37:17 +080097#define DRV_VERSION IWL39_VERSION
Zhu Yib481de92007-09-25 17:54:57 -070098
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(
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800106 struct iwl_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 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800146static int iwl3945_queue_init(struct iwl_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 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800177static int iwl3945_tx_queue_alloc(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800178 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 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800220int iwl3945_tx_queue_init(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800221 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 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800272void iwl3945_tx_queue_free(struct iwl_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
Zhu Yib481de92007-09-25 17:54:57 -0700306/*************** STATION TABLE MANAGEMENT ****
Ben Cahill9fbab512007-11-29 11:09:47 +0800307 * mac80211 should be examined to determine if sta_info is duplicating
Zhu Yib481de92007-09-25 17:54:57 -0700308 * the functionality provided here
309 */
310
311/**************************************************************/
Ian Schram01ebd062007-10-25 17:15:22 +0800312#if 0 /* temporary disable till we add real remove station */
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800313/**
314 * iwl3945_remove_station - Remove driver's knowledge of station.
315 *
316 * NOTE: This does not remove station from device's station table.
317 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800318static u8 iwl3945_remove_station(struct iwl_priv *priv, const u8 *addr, int is_ap)
Zhu Yib481de92007-09-25 17:54:57 -0700319{
320 int index = IWL_INVALID_STATION;
321 int i;
322 unsigned long flags;
323
324 spin_lock_irqsave(&priv->sta_lock, flags);
325
326 if (is_ap)
327 index = IWL_AP_ID;
328 else if (is_broadcast_ether_addr(addr))
Abhijeet Kolekar3832ec92008-12-19 10:37:26 +0800329 index = priv->hw_params.bcast_sta_id;
Zhu Yib481de92007-09-25 17:54:57 -0700330 else
Abhijeet Kolekar3832ec92008-12-19 10:37:26 +0800331 for (i = IWL_STA_ID; i < priv->hw_params.max_stations; i++)
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800332 if (priv->stations_39[i].used &&
333 !compare_ether_addr(priv->stations_39[i].sta.sta.addr,
Zhu Yib481de92007-09-25 17:54:57 -0700334 addr)) {
335 index = i;
336 break;
337 }
338
339 if (unlikely(index == IWL_INVALID_STATION))
340 goto out;
341
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800342 if (priv->stations_39[index].used) {
343 priv->stations_39[index].used = 0;
Zhu Yib481de92007-09-25 17:54:57 -0700344 priv->num_stations--;
345 }
346
347 BUG_ON(priv->num_stations < 0);
348
349out:
350 spin_unlock_irqrestore(&priv->sta_lock, flags);
351 return 0;
352}
Zhu Yi556f8db2007-09-27 11:27:33 +0800353#endif
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800354
355/**
356 * iwl3945_clear_stations_table - Clear the driver's station table
357 *
358 * NOTE: This does not clear or otherwise alter the device's station table.
359 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800360static void iwl3945_clear_stations_table(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700361{
362 unsigned long flags;
363
364 spin_lock_irqsave(&priv->sta_lock, flags);
365
366 priv->num_stations = 0;
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800367 memset(priv->stations_39, 0, sizeof(priv->stations_39));
Zhu Yib481de92007-09-25 17:54:57 -0700368
369 spin_unlock_irqrestore(&priv->sta_lock, flags);
370}
371
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800372/**
373 * iwl3945_add_station - Add station to station tables in driver and device
374 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800375u8 iwl3945_add_station(struct iwl_priv *priv, const u8 *addr, int is_ap, u8 flags)
Zhu Yib481de92007-09-25 17:54:57 -0700376{
377 int i;
378 int index = IWL_INVALID_STATION;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800379 struct iwl3945_station_entry *station;
Zhu Yib481de92007-09-25 17:54:57 -0700380 unsigned long flags_spin;
Zhu Yic14c5212007-09-27 11:27:35 +0800381 u8 rate;
Zhu Yib481de92007-09-25 17:54:57 -0700382
383 spin_lock_irqsave(&priv->sta_lock, flags_spin);
384 if (is_ap)
385 index = IWL_AP_ID;
386 else if (is_broadcast_ether_addr(addr))
Abhijeet Kolekar3832ec92008-12-19 10:37:26 +0800387 index = priv->hw_params.bcast_sta_id;
Zhu Yib481de92007-09-25 17:54:57 -0700388 else
Abhijeet Kolekar3832ec92008-12-19 10:37:26 +0800389 for (i = IWL_STA_ID; i < priv->hw_params.max_stations; i++) {
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800390 if (!compare_ether_addr(priv->stations_39[i].sta.sta.addr,
Zhu Yib481de92007-09-25 17:54:57 -0700391 addr)) {
392 index = i;
393 break;
394 }
395
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800396 if (!priv->stations_39[i].used &&
Zhu Yib481de92007-09-25 17:54:57 -0700397 index == IWL_INVALID_STATION)
398 index = i;
399 }
400
Ian Schram01ebd062007-10-25 17:15:22 +0800401 /* These two conditions has the same outcome but keep them separate
Zhu Yib481de92007-09-25 17:54:57 -0700402 since they have different meaning */
403 if (unlikely(index == IWL_INVALID_STATION)) {
404 spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
405 return index;
406 }
407
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800408 if (priv->stations_39[index].used &&
409 !compare_ether_addr(priv->stations_39[index].sta.sta.addr, addr)) {
Zhu Yib481de92007-09-25 17:54:57 -0700410 spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
411 return index;
412 }
413
Johannes Berge1749612008-10-27 15:59:26 -0700414 IWL_DEBUG_ASSOC("Add STA ID %d: %pM\n", index, addr);
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800415 station = &priv->stations_39[index];
Zhu Yib481de92007-09-25 17:54:57 -0700416 station->used = 1;
417 priv->num_stations++;
418
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800419 /* Set up the REPLY_ADD_STA command to send to device */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800420 memset(&station->sta, 0, sizeof(struct iwl3945_addsta_cmd));
Zhu Yib481de92007-09-25 17:54:57 -0700421 memcpy(station->sta.sta.addr, addr, ETH_ALEN);
422 station->sta.mode = 0;
423 station->sta.sta.sta_id = index;
424 station->sta.station_flags = 0;
425
Johannes Berg8318d782008-01-24 19:38:38 +0100426 if (priv->band == IEEE80211_BAND_5GHZ)
Tomas Winkler69946332007-10-25 17:15:27 +0800427 rate = IWL_RATE_6M_PLCP;
428 else
429 rate = IWL_RATE_1M_PLCP;
Zhu Yic14c5212007-09-27 11:27:35 +0800430
431 /* Turn on both antennas for the station... */
432 station->sta.rate_n_flags =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800433 iwl3945_hw_set_rate_n_flags(rate, RATE_MCS_ANT_AB_MSK);
Zhu Yic14c5212007-09-27 11:27:35 +0800434
Zhu Yib481de92007-09-25 17:54:57 -0700435 spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800436
437 /* Add station to device's station table */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800438 iwl3945_send_add_station(priv, &station->sta, flags);
Zhu Yib481de92007-09-25 17:54:57 -0700439 return index;
440
441}
442
443/*************** DRIVER STATUS FUNCTIONS *****/
444
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800445static inline int iwl3945_is_ready(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700446{
447 /* The adapter is 'ready' if READY and GEO_CONFIGURED bits are
448 * set but EXIT_PENDING is not */
449 return test_bit(STATUS_READY, &priv->status) &&
450 test_bit(STATUS_GEO_CONFIGURED, &priv->status) &&
451 !test_bit(STATUS_EXIT_PENDING, &priv->status);
452}
453
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800454static inline int iwl3945_is_alive(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700455{
456 return test_bit(STATUS_ALIVE, &priv->status);
457}
458
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800459static inline int iwl3945_is_init(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700460{
461 return test_bit(STATUS_INIT, &priv->status);
462}
463
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800464static inline int iwl3945_is_rfkill_sw(struct iwl_priv *priv)
Adel Gadllah80fcc9e2008-07-01 17:49:50 +0200465{
466 return test_bit(STATUS_RF_KILL_SW, &priv->status);
467}
468
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800469static inline int iwl3945_is_rfkill_hw(struct iwl_priv *priv)
Adel Gadllah80fcc9e2008-07-01 17:49:50 +0200470{
471 return test_bit(STATUS_RF_KILL_HW, &priv->status);
472}
473
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800474static inline int iwl3945_is_rfkill(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700475{
Adel Gadllah80fcc9e2008-07-01 17:49:50 +0200476 return iwl3945_is_rfkill_hw(priv) ||
477 iwl3945_is_rfkill_sw(priv);
Zhu Yib481de92007-09-25 17:54:57 -0700478}
479
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800480static inline int iwl3945_is_ready_rf(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700481{
482
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800483 if (iwl3945_is_rfkill(priv))
Zhu Yib481de92007-09-25 17:54:57 -0700484 return 0;
485
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800486 return iwl3945_is_ready(priv);
Zhu Yib481de92007-09-25 17:54:57 -0700487}
488
489/*************** HOST COMMAND QUEUE FUNCTIONS *****/
490
Abhijeet Kolekarc3056062008-11-12 13:14:08 -0800491#define IWL_CMD(x) case x: return #x
Zhu Yib481de92007-09-25 17:54:57 -0700492#define HOST_COMPLETE_TIMEOUT (HZ / 2)
493
494/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800495 * iwl3945_enqueue_hcmd - enqueue a uCode command
Zhu Yib481de92007-09-25 17:54:57 -0700496 * @priv: device private data point
497 * @cmd: a point to the ucode command structure
498 *
499 * The function returns < 0 values to indicate the operation is
500 * failed. On success, it turns the index (> 0) of command in the
501 * command queue.
502 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800503static int iwl3945_enqueue_hcmd(struct iwl_priv *priv, struct iwl3945_host_cmd *cmd)
Zhu Yib481de92007-09-25 17:54:57 -0700504{
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800505 struct iwl3945_tx_queue *txq = &priv->txq39[IWL_CMD_QUEUE_NUM];
Samuel Ortizd20b3c62008-12-19 10:37:15 +0800506 struct iwl_queue *q = &txq->q;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800507 struct iwl3945_tfd_frame *tfd;
Zhu Yib481de92007-09-25 17:54:57 -0700508 u32 *control_flags;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800509 struct iwl3945_cmd *out_cmd;
Zhu Yib481de92007-09-25 17:54:57 -0700510 u32 idx;
511 u16 fix_size = (u16)(cmd->len + sizeof(out_cmd->hdr));
512 dma_addr_t phys_addr;
513 int pad;
514 u16 count;
515 int ret;
516 unsigned long flags;
517
518 /* If any of the command structures end up being larger than
519 * the TFD_MAX_PAYLOAD_SIZE, and it sent as a 'small' command then
520 * we will need to increase the size of the TFD entries */
Samuel Ortizd20b3c62008-12-19 10:37:15 +0800521 BUG_ON((fix_size > TFD39_MAX_PAYLOAD_SIZE) &&
Zhu Yib481de92007-09-25 17:54:57 -0700522 !(cmd->meta.flags & CMD_SIZE_HUGE));
523
Gregory Greenmanc342a1b2008-02-06 11:20:40 -0800524
525 if (iwl3945_is_rfkill(priv)) {
526 IWL_DEBUG_INFO("Not sending command - RF KILL");
527 return -EIO;
528 }
529
Samuel Ortizd20b3c62008-12-19 10:37:15 +0800530 if (iwl_queue_space(q) < ((cmd->meta.flags & CMD_ASYNC) ? 2 : 1)) {
Zhu Yib481de92007-09-25 17:54:57 -0700531 IWL_ERROR("No space for Tx\n");
532 return -ENOSPC;
533 }
534
535 spin_lock_irqsave(&priv->hcmd_lock, flags);
536
Tomas Winklerfc4b6852007-10-25 17:15:24 +0800537 tfd = &txq->bd[q->write_ptr];
Zhu Yib481de92007-09-25 17:54:57 -0700538 memset(tfd, 0, sizeof(*tfd));
539
540 control_flags = (u32 *) tfd;
541
Tomas Winklerfc4b6852007-10-25 17:15:24 +0800542 idx = get_cmd_index(q, q->write_ptr, cmd->meta.flags & CMD_SIZE_HUGE);
Zhu Yib481de92007-09-25 17:54:57 -0700543 out_cmd = &txq->cmd[idx];
544
545 out_cmd->hdr.cmd = cmd->id;
546 memcpy(&out_cmd->meta, &cmd->meta, sizeof(cmd->meta));
547 memcpy(&out_cmd->cmd.payload, cmd->data, cmd->len);
548
549 /* At this point, the out_cmd now has all of the incoming cmd
550 * information */
551
552 out_cmd->hdr.flags = 0;
553 out_cmd->hdr.sequence = cpu_to_le16(QUEUE_TO_SEQ(IWL_CMD_QUEUE_NUM) |
Tomas Winklerfc4b6852007-10-25 17:15:24 +0800554 INDEX_TO_SEQ(q->write_ptr));
Zhu Yib481de92007-09-25 17:54:57 -0700555 if (out_cmd->meta.flags & CMD_SIZE_HUGE)
Tomas Winkler600c0e12008-12-19 10:37:04 +0800556 out_cmd->hdr.sequence |= SEQ_HUGE_FRAME;
Zhu Yib481de92007-09-25 17:54:57 -0700557
558 phys_addr = txq->dma_addr_cmd + sizeof(txq->cmd[0]) * idx +
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800559 offsetof(struct iwl3945_cmd, hdr);
560 iwl3945_hw_txq_attach_buf_to_tfd(priv, tfd, phys_addr, fix_size);
Zhu Yib481de92007-09-25 17:54:57 -0700561
562 pad = U32_PAD(cmd->len);
563 count = TFD_CTL_COUNT_GET(*control_flags);
564 *control_flags = TFD_CTL_COUNT_SET(count) | TFD_CTL_PAD_SET(pad);
565
566 IWL_DEBUG_HC("Sending command %s (#%x), seq: 0x%04X, "
567 "%d bytes at %d[%d]:%d\n",
568 get_cmd_string(out_cmd->hdr.cmd),
569 out_cmd->hdr.cmd, le16_to_cpu(out_cmd->hdr.sequence),
Tomas Winklerfc4b6852007-10-25 17:15:24 +0800570 fix_size, q->write_ptr, idx, IWL_CMD_QUEUE_NUM);
Zhu Yib481de92007-09-25 17:54:57 -0700571
572 txq->need_update = 1;
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800573
574 /* Increment and update queue's write index */
Tomas Winklerc54b6792008-03-06 17:36:53 -0800575 q->write_ptr = iwl_queue_inc_wrap(q->write_ptr, q->n_bd);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800576 ret = iwl3945_tx_queue_update_write_ptr(priv, txq);
Zhu Yib481de92007-09-25 17:54:57 -0700577
578 spin_unlock_irqrestore(&priv->hcmd_lock, flags);
579 return ret ? ret : idx;
580}
581
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800582static int iwl3945_send_cmd_async(struct iwl_priv *priv, struct iwl3945_host_cmd *cmd)
Zhu Yib481de92007-09-25 17:54:57 -0700583{
584 int ret;
585
586 BUG_ON(!(cmd->meta.flags & CMD_ASYNC));
587
588 /* An asynchronous command can not expect an SKB to be set. */
589 BUG_ON(cmd->meta.flags & CMD_WANT_SKB);
590
591 /* An asynchronous command MUST have a callback. */
592 BUG_ON(!cmd->meta.u.callback);
593
594 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
595 return -EBUSY;
596
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800597 ret = iwl3945_enqueue_hcmd(priv, cmd);
Zhu Yib481de92007-09-25 17:54:57 -0700598 if (ret < 0) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800599 IWL_ERROR("Error sending %s: iwl3945_enqueue_hcmd failed: %d\n",
Zhu Yib481de92007-09-25 17:54:57 -0700600 get_cmd_string(cmd->id), ret);
601 return ret;
602 }
603 return 0;
604}
605
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800606static int iwl3945_send_cmd_sync(struct iwl_priv *priv, struct iwl3945_host_cmd *cmd)
Zhu Yib481de92007-09-25 17:54:57 -0700607{
608 int cmd_idx;
609 int ret;
Zhu Yib481de92007-09-25 17:54:57 -0700610
611 BUG_ON(cmd->meta.flags & CMD_ASYNC);
612
613 /* A synchronous command can not have a callback set. */
614 BUG_ON(cmd->meta.u.callback != NULL);
615
Tomas Winklere5472972008-03-28 16:21:12 -0700616 if (test_and_set_bit(STATUS_HCMD_SYNC_ACTIVE, &priv->status)) {
Zhu Yib481de92007-09-25 17:54:57 -0700617 IWL_ERROR("Error sending %s: Already sending a host command\n",
618 get_cmd_string(cmd->id));
Tomas Winklere5472972008-03-28 16:21:12 -0700619 ret = -EBUSY;
620 goto out;
Zhu Yib481de92007-09-25 17:54:57 -0700621 }
622
623 set_bit(STATUS_HCMD_ACTIVE, &priv->status);
624
625 if (cmd->meta.flags & CMD_WANT_SKB)
626 cmd->meta.source = &cmd->meta;
627
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800628 cmd_idx = iwl3945_enqueue_hcmd(priv, cmd);
Zhu Yib481de92007-09-25 17:54:57 -0700629 if (cmd_idx < 0) {
630 ret = cmd_idx;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800631 IWL_ERROR("Error sending %s: iwl3945_enqueue_hcmd failed: %d\n",
Zhu Yib481de92007-09-25 17:54:57 -0700632 get_cmd_string(cmd->id), ret);
633 goto out;
634 }
635
636 ret = wait_event_interruptible_timeout(priv->wait_command_queue,
637 !test_bit(STATUS_HCMD_ACTIVE, &priv->status),
638 HOST_COMPLETE_TIMEOUT);
639 if (!ret) {
640 if (test_bit(STATUS_HCMD_ACTIVE, &priv->status)) {
641 IWL_ERROR("Error sending %s: time out after %dms.\n",
642 get_cmd_string(cmd->id),
643 jiffies_to_msecs(HOST_COMPLETE_TIMEOUT));
644
645 clear_bit(STATUS_HCMD_ACTIVE, &priv->status);
646 ret = -ETIMEDOUT;
647 goto cancel;
648 }
649 }
650
651 if (test_bit(STATUS_RF_KILL_HW, &priv->status)) {
652 IWL_DEBUG_INFO("Command %s aborted: RF KILL Switch\n",
653 get_cmd_string(cmd->id));
654 ret = -ECANCELED;
655 goto fail;
656 }
657 if (test_bit(STATUS_FW_ERROR, &priv->status)) {
658 IWL_DEBUG_INFO("Command %s failed: FW Error\n",
659 get_cmd_string(cmd->id));
660 ret = -EIO;
661 goto fail;
662 }
663 if ((cmd->meta.flags & CMD_WANT_SKB) && !cmd->meta.u.skb) {
664 IWL_ERROR("Error: Response NULL in '%s'\n",
665 get_cmd_string(cmd->id));
666 ret = -EIO;
Zhu Yi73e1a652009-01-08 10:19:58 -0800667 goto cancel;
Zhu Yib481de92007-09-25 17:54:57 -0700668 }
669
670 ret = 0;
671 goto out;
672
673cancel:
674 if (cmd->meta.flags & CMD_WANT_SKB) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800675 struct iwl3945_cmd *qcmd;
Zhu Yib481de92007-09-25 17:54:57 -0700676
677 /* Cancel the CMD_WANT_SKB flag for the cmd in the
678 * TX cmd queue. Otherwise in case the cmd comes
679 * in later, it will possibly set an invalid
680 * address (cmd->meta.source). */
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800681 qcmd = &priv->txq39[IWL_CMD_QUEUE_NUM].cmd[cmd_idx];
Zhu Yib481de92007-09-25 17:54:57 -0700682 qcmd->meta.flags &= ~CMD_WANT_SKB;
683 }
684fail:
685 if (cmd->meta.u.skb) {
686 dev_kfree_skb_any(cmd->meta.u.skb);
687 cmd->meta.u.skb = NULL;
688 }
689out:
Tomas Winklere5472972008-03-28 16:21:12 -0700690 clear_bit(STATUS_HCMD_SYNC_ACTIVE, &priv->status);
Zhu Yib481de92007-09-25 17:54:57 -0700691 return ret;
692}
693
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800694int iwl3945_send_cmd(struct iwl_priv *priv, struct iwl3945_host_cmd *cmd)
Zhu Yib481de92007-09-25 17:54:57 -0700695{
Zhu Yib481de92007-09-25 17:54:57 -0700696 if (cmd->meta.flags & CMD_ASYNC)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800697 return iwl3945_send_cmd_async(priv, cmd);
Zhu Yib481de92007-09-25 17:54:57 -0700698
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800699 return iwl3945_send_cmd_sync(priv, cmd);
Zhu Yib481de92007-09-25 17:54:57 -0700700}
701
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800702int iwl3945_send_cmd_pdu(struct iwl_priv *priv, u8 id, u16 len, const void *data)
Zhu Yib481de92007-09-25 17:54:57 -0700703{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800704 struct iwl3945_host_cmd cmd = {
Zhu Yib481de92007-09-25 17:54:57 -0700705 .id = id,
706 .len = len,
707 .data = data,
708 };
709
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800710 return iwl3945_send_cmd_sync(priv, &cmd);
Zhu Yib481de92007-09-25 17:54:57 -0700711}
712
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800713static int __must_check iwl3945_send_cmd_u32(struct iwl_priv *priv, u8 id, u32 val)
Zhu Yib481de92007-09-25 17:54:57 -0700714{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800715 struct iwl3945_host_cmd cmd = {
Zhu Yib481de92007-09-25 17:54:57 -0700716 .id = id,
717 .len = sizeof(val),
718 .data = &val,
719 };
720
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800721 return iwl3945_send_cmd_sync(priv, &cmd);
Zhu Yib481de92007-09-25 17:54:57 -0700722}
723
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800724int iwl3945_send_statistics_request(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700725{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800726 return iwl3945_send_cmd_u32(priv, REPLY_STATISTICS_CMD, 0);
Zhu Yib481de92007-09-25 17:54:57 -0700727}
728
729/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800730 * iwl3945_set_rxon_channel - Set the phymode and channel values in staging RXON
Johannes Berg8318d782008-01-24 19:38:38 +0100731 * @band: 2.4 or 5 GHz band
732 * @channel: Any channel valid for the requested band
Zhu Yib481de92007-09-25 17:54:57 -0700733
Johannes Berg8318d782008-01-24 19:38:38 +0100734 * In addition to setting the staging RXON, priv->band is also set.
Zhu Yib481de92007-09-25 17:54:57 -0700735 *
736 * NOTE: Does not commit to the hardware; it sets appropriate bit fields
Johannes Berg8318d782008-01-24 19:38:38 +0100737 * in the staging RXON flag structure based on the band
Zhu Yib481de92007-09-25 17:54:57 -0700738 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800739static int iwl3945_set_rxon_channel(struct iwl_priv *priv,
Johannes Berg8318d782008-01-24 19:38:38 +0100740 enum ieee80211_band band,
741 u16 channel)
Zhu Yib481de92007-09-25 17:54:57 -0700742{
Johannes Berg8318d782008-01-24 19:38:38 +0100743 if (!iwl3945_get_channel_info(priv, band, channel)) {
Zhu Yib481de92007-09-25 17:54:57 -0700744 IWL_DEBUG_INFO("Could not set channel to %d [%d]\n",
Johannes Berg8318d782008-01-24 19:38:38 +0100745 channel, band);
Zhu Yib481de92007-09-25 17:54:57 -0700746 return -EINVAL;
747 }
748
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800749 if ((le16_to_cpu(priv->staging39_rxon.channel) == channel) &&
Johannes Berg8318d782008-01-24 19:38:38 +0100750 (priv->band == band))
Zhu Yib481de92007-09-25 17:54:57 -0700751 return 0;
752
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800753 priv->staging39_rxon.channel = cpu_to_le16(channel);
Johannes Berg8318d782008-01-24 19:38:38 +0100754 if (band == IEEE80211_BAND_5GHZ)
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800755 priv->staging39_rxon.flags &= ~RXON_FLG_BAND_24G_MSK;
Zhu Yib481de92007-09-25 17:54:57 -0700756 else
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800757 priv->staging39_rxon.flags |= RXON_FLG_BAND_24G_MSK;
Zhu Yib481de92007-09-25 17:54:57 -0700758
Johannes Berg8318d782008-01-24 19:38:38 +0100759 priv->band = band;
Zhu Yib481de92007-09-25 17:54:57 -0700760
Johannes Berg8318d782008-01-24 19:38:38 +0100761 IWL_DEBUG_INFO("Staging channel set to %d [%d]\n", channel, band);
Zhu Yib481de92007-09-25 17:54:57 -0700762
763 return 0;
764}
765
766/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800767 * iwl3945_check_rxon_cmd - validate RXON structure is valid
Zhu Yib481de92007-09-25 17:54:57 -0700768 *
769 * NOTE: This is really only useful during development and can eventually
770 * be #ifdef'd out once the driver is stable and folks aren't actively
771 * making changes
772 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800773static int iwl3945_check_rxon_cmd(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700774{
775 int error = 0;
776 int counter = 1;
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800777 struct iwl3945_rxon_cmd *rxon = &priv->staging39_rxon;
Zhu Yib481de92007-09-25 17:54:57 -0700778
779 if (rxon->flags & RXON_FLG_BAND_24G_MSK) {
780 error |= le32_to_cpu(rxon->flags &
781 (RXON_FLG_TGJ_NARROW_BAND_MSK |
782 RXON_FLG_RADAR_DETECT_MSK));
783 if (error)
Winkler, Tomas39aadf82008-12-19 10:37:32 +0800784 IWL_WARN(priv, "check 24G fields %d | %d\n",
Zhu Yib481de92007-09-25 17:54:57 -0700785 counter++, error);
786 } else {
787 error |= (rxon->flags & RXON_FLG_SHORT_SLOT_MSK) ?
788 0 : le32_to_cpu(RXON_FLG_SHORT_SLOT_MSK);
789 if (error)
Winkler, Tomas39aadf82008-12-19 10:37:32 +0800790 IWL_WARN(priv, "check 52 fields %d | %d\n",
Zhu Yib481de92007-09-25 17:54:57 -0700791 counter++, error);
792 error |= le32_to_cpu(rxon->flags & RXON_FLG_CCK_MSK);
793 if (error)
Winkler, Tomas39aadf82008-12-19 10:37:32 +0800794 IWL_WARN(priv, "check 52 CCK %d | %d\n",
Zhu Yib481de92007-09-25 17:54:57 -0700795 counter++, error);
796 }
797 error |= (rxon->node_addr[0] | rxon->bssid_addr[0]) & 0x1;
798 if (error)
Winkler, Tomas39aadf82008-12-19 10:37:32 +0800799 IWL_WARN(priv, "check mac addr %d | %d\n", counter++, error);
Zhu Yib481de92007-09-25 17:54:57 -0700800
801 /* make sure basic rates 6Mbps and 1Mbps are supported */
802 error |= (((rxon->ofdm_basic_rates & IWL_RATE_6M_MASK) == 0) &&
803 ((rxon->cck_basic_rates & IWL_RATE_1M_MASK) == 0));
804 if (error)
Winkler, Tomas39aadf82008-12-19 10:37:32 +0800805 IWL_WARN(priv, "check basic rate %d | %d\n", counter++, error);
Zhu Yib481de92007-09-25 17:54:57 -0700806
807 error |= (le16_to_cpu(rxon->assoc_id) > 2007);
808 if (error)
Winkler, Tomas39aadf82008-12-19 10:37:32 +0800809 IWL_WARN(priv, "check assoc id %d | %d\n", counter++, error);
Zhu Yib481de92007-09-25 17:54:57 -0700810
811 error |= ((rxon->flags & (RXON_FLG_CCK_MSK | RXON_FLG_SHORT_SLOT_MSK))
812 == (RXON_FLG_CCK_MSK | RXON_FLG_SHORT_SLOT_MSK));
813 if (error)
Winkler, Tomas39aadf82008-12-19 10:37:32 +0800814 IWL_WARN(priv, "check CCK and short slot %d | %d\n",
Zhu Yib481de92007-09-25 17:54:57 -0700815 counter++, error);
816
817 error |= ((rxon->flags & (RXON_FLG_CCK_MSK | RXON_FLG_AUTO_DETECT_MSK))
818 == (RXON_FLG_CCK_MSK | RXON_FLG_AUTO_DETECT_MSK));
819 if (error)
Winkler, Tomas39aadf82008-12-19 10:37:32 +0800820 IWL_WARN(priv, "check CCK & auto detect %d | %d\n",
Zhu Yib481de92007-09-25 17:54:57 -0700821 counter++, error);
822
823 error |= ((rxon->flags & (RXON_FLG_AUTO_DETECT_MSK |
824 RXON_FLG_TGG_PROTECT_MSK)) == RXON_FLG_TGG_PROTECT_MSK);
825 if (error)
Winkler, Tomas39aadf82008-12-19 10:37:32 +0800826 IWL_WARN(priv, "check TGG and auto detect %d | %d\n",
Zhu Yib481de92007-09-25 17:54:57 -0700827 counter++, error);
828
829 if ((rxon->flags & RXON_FLG_DIS_DIV_MSK))
830 error |= ((rxon->flags & (RXON_FLG_ANT_B_MSK |
831 RXON_FLG_ANT_A_MSK)) == 0);
832 if (error)
Winkler, Tomas39aadf82008-12-19 10:37:32 +0800833 IWL_WARN(priv, "check antenna %d %d\n", counter++, error);
Zhu Yib481de92007-09-25 17:54:57 -0700834
835 if (error)
Winkler, Tomas39aadf82008-12-19 10:37:32 +0800836 IWL_WARN(priv, "Tuning to channel %d\n",
Zhu Yib481de92007-09-25 17:54:57 -0700837 le16_to_cpu(rxon->channel));
838
839 if (error) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800840 IWL_ERROR("Not a valid iwl3945_rxon_assoc_cmd field values\n");
Zhu Yib481de92007-09-25 17:54:57 -0700841 return -1;
842 }
843 return 0;
844}
845
846/**
Ben Cahill9fbab512007-11-29 11:09:47 +0800847 * iwl3945_full_rxon_required - check if full RXON (vs RXON_ASSOC) cmd is needed
Ian Schram01ebd062007-10-25 17:15:22 +0800848 * @priv: staging_rxon is compared to active_rxon
Zhu Yib481de92007-09-25 17:54:57 -0700849 *
Ben Cahill9fbab512007-11-29 11:09:47 +0800850 * If the RXON structure is changing enough to require a new tune,
851 * or is clearing the RXON_FILTER_ASSOC_MSK, then return 1 to indicate that
852 * a new tune (full RXON command, rather than RXON_ASSOC cmd) is required.
Zhu Yib481de92007-09-25 17:54:57 -0700853 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800854static int iwl3945_full_rxon_required(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700855{
856
857 /* These items are only settable from the full RXON command */
Ron Rindjunsky5d1e2322008-06-30 17:23:04 +0800858 if (!(iwl3945_is_associated(priv)) ||
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800859 compare_ether_addr(priv->staging39_rxon.bssid_addr,
860 priv->active39_rxon.bssid_addr) ||
861 compare_ether_addr(priv->staging39_rxon.node_addr,
862 priv->active39_rxon.node_addr) ||
863 compare_ether_addr(priv->staging39_rxon.wlap_bssid_addr,
864 priv->active39_rxon.wlap_bssid_addr) ||
865 (priv->staging39_rxon.dev_type != priv->active39_rxon.dev_type) ||
866 (priv->staging39_rxon.channel != priv->active39_rxon.channel) ||
867 (priv->staging39_rxon.air_propagation !=
868 priv->active39_rxon.air_propagation) ||
869 (priv->staging39_rxon.assoc_id != priv->active39_rxon.assoc_id))
Zhu Yib481de92007-09-25 17:54:57 -0700870 return 1;
871
872 /* flags, filter_flags, ofdm_basic_rates, and cck_basic_rates can
873 * be updated with the RXON_ASSOC command -- however only some
874 * flag transitions are allowed using RXON_ASSOC */
875
876 /* Check if we are not switching bands */
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800877 if ((priv->staging39_rxon.flags & RXON_FLG_BAND_24G_MSK) !=
878 (priv->active39_rxon.flags & RXON_FLG_BAND_24G_MSK))
Zhu Yib481de92007-09-25 17:54:57 -0700879 return 1;
880
881 /* Check if we are switching association toggle */
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800882 if ((priv->staging39_rxon.filter_flags & RXON_FILTER_ASSOC_MSK) !=
883 (priv->active39_rxon.filter_flags & RXON_FILTER_ASSOC_MSK))
Zhu Yib481de92007-09-25 17:54:57 -0700884 return 1;
885
886 return 0;
887}
888
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800889static int iwl3945_send_rxon_assoc(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700890{
891 int rc = 0;
Tomas Winkler3d24a9f2008-12-19 10:37:07 +0800892 struct iwl_rx_packet *res = NULL;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800893 struct iwl3945_rxon_assoc_cmd rxon_assoc;
894 struct iwl3945_host_cmd cmd = {
Zhu Yib481de92007-09-25 17:54:57 -0700895 .id = REPLY_RXON_ASSOC,
896 .len = sizeof(rxon_assoc),
897 .meta.flags = CMD_WANT_SKB,
898 .data = &rxon_assoc,
899 };
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800900 const struct iwl3945_rxon_cmd *rxon1 = &priv->staging39_rxon;
901 const struct iwl3945_rxon_cmd *rxon2 = &priv->active39_rxon;
Zhu Yib481de92007-09-25 17:54:57 -0700902
903 if ((rxon1->flags == rxon2->flags) &&
904 (rxon1->filter_flags == rxon2->filter_flags) &&
905 (rxon1->cck_basic_rates == rxon2->cck_basic_rates) &&
906 (rxon1->ofdm_basic_rates == rxon2->ofdm_basic_rates)) {
907 IWL_DEBUG_INFO("Using current RXON_ASSOC. Not resending.\n");
908 return 0;
909 }
910
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800911 rxon_assoc.flags = priv->staging39_rxon.flags;
912 rxon_assoc.filter_flags = priv->staging39_rxon.filter_flags;
913 rxon_assoc.ofdm_basic_rates = priv->staging39_rxon.ofdm_basic_rates;
914 rxon_assoc.cck_basic_rates = priv->staging39_rxon.cck_basic_rates;
Zhu Yib481de92007-09-25 17:54:57 -0700915 rxon_assoc.reserved = 0;
916
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800917 rc = iwl3945_send_cmd_sync(priv, &cmd);
Zhu Yib481de92007-09-25 17:54:57 -0700918 if (rc)
919 return rc;
920
Tomas Winkler3d24a9f2008-12-19 10:37:07 +0800921 res = (struct iwl_rx_packet *)cmd.meta.u.skb->data;
Zhu Yib481de92007-09-25 17:54:57 -0700922 if (res->hdr.flags & IWL_CMD_FAILED_MSK) {
923 IWL_ERROR("Bad return from REPLY_RXON_ASSOC command\n");
924 rc = -EIO;
925 }
926
927 priv->alloc_rxb_skb--;
928 dev_kfree_skb_any(cmd.meta.u.skb);
929
930 return rc;
931}
932
933/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800934 * iwl3945_commit_rxon - commit staging_rxon to hardware
Zhu Yib481de92007-09-25 17:54:57 -0700935 *
Ian Schram01ebd062007-10-25 17:15:22 +0800936 * The RXON command in staging_rxon is committed to the hardware and
Zhu Yib481de92007-09-25 17:54:57 -0700937 * the active_rxon structure is updated with the new data. This
938 * function correctly transitions out of the RXON_ASSOC_MSK state if
939 * a HW tune is required based on the RXON structure changes.
940 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800941static int iwl3945_commit_rxon(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700942{
943 /* cast away the const for active_rxon in this function */
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800944 struct iwl3945_rxon_cmd *active_rxon = (void *)&priv->active39_rxon;
Zhu Yib481de92007-09-25 17:54:57 -0700945 int rc = 0;
946
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800947 if (!iwl3945_is_alive(priv))
Zhu Yib481de92007-09-25 17:54:57 -0700948 return -1;
949
950 /* always get timestamp with Rx frame */
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800951 priv->staging39_rxon.flags |= RXON_FLG_TSF2HOST_MSK;
Zhu Yib481de92007-09-25 17:54:57 -0700952
953 /* select antenna */
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800954 priv->staging39_rxon.flags &=
Zhu Yib481de92007-09-25 17:54:57 -0700955 ~(RXON_FLG_DIS_DIV_MSK | RXON_FLG_ANT_SEL_MSK);
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800956 priv->staging39_rxon.flags |= iwl3945_get_antenna_flags(priv);
Zhu Yib481de92007-09-25 17:54:57 -0700957
Samuel Ortiza3139c52008-12-19 10:37:09 +0800958 rc = iwl3945_check_rxon_cmd(priv);
Zhu Yib481de92007-09-25 17:54:57 -0700959 if (rc) {
960 IWL_ERROR("Invalid RXON configuration. Not committing.\n");
961 return -EINVAL;
962 }
963
964 /* If we don't need to send a full RXON, we can use
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800965 * iwl3945_rxon_assoc_cmd which is used to reconfigure filter
Zhu Yib481de92007-09-25 17:54:57 -0700966 * and other flags for the current radio configuration. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800967 if (!iwl3945_full_rxon_required(priv)) {
968 rc = iwl3945_send_rxon_assoc(priv);
Zhu Yib481de92007-09-25 17:54:57 -0700969 if (rc) {
970 IWL_ERROR("Error setting RXON_ASSOC "
971 "configuration (%d).\n", rc);
972 return rc;
973 }
974
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800975 memcpy(active_rxon, &priv->staging39_rxon, sizeof(*active_rxon));
Zhu Yib481de92007-09-25 17:54:57 -0700976
977 return 0;
978 }
979
980 /* If we are currently associated and the new config requires
981 * an RXON_ASSOC and the new config wants the associated mask enabled,
982 * we must clear the associated from the active configuration
983 * before we apply the new config */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800984 if (iwl3945_is_associated(priv) &&
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800985 (priv->staging39_rxon.filter_flags & RXON_FILTER_ASSOC_MSK)) {
Zhu Yib481de92007-09-25 17:54:57 -0700986 IWL_DEBUG_INFO("Toggling associated bit on current RXON\n");
987 active_rxon->filter_flags &= ~RXON_FILTER_ASSOC_MSK;
988
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800989 rc = iwl3945_send_cmd_pdu(priv, REPLY_RXON,
990 sizeof(struct iwl3945_rxon_cmd),
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800991 &priv->active39_rxon);
Zhu Yib481de92007-09-25 17:54:57 -0700992
993 /* If the mask clearing failed then we set
994 * active_rxon back to what it was previously */
995 if (rc) {
996 active_rxon->filter_flags |= RXON_FILTER_ASSOC_MSK;
997 IWL_ERROR("Error clearing ASSOC_MSK on current "
998 "configuration (%d).\n", rc);
999 return rc;
1000 }
Zhu Yib481de92007-09-25 17:54:57 -07001001 }
1002
1003 IWL_DEBUG_INFO("Sending RXON\n"
1004 "* with%s RXON_FILTER_ASSOC_MSK\n"
1005 "* channel = %d\n"
Johannes Berge1749612008-10-27 15:59:26 -07001006 "* bssid = %pM\n",
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001007 ((priv->staging39_rxon.filter_flags &
Zhu Yib481de92007-09-25 17:54:57 -07001008 RXON_FILTER_ASSOC_MSK) ? "" : "out"),
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001009 le16_to_cpu(priv->staging39_rxon.channel),
Johannes Berge1749612008-10-27 15:59:26 -07001010 priv->staging_rxon.bssid_addr);
Zhu Yib481de92007-09-25 17:54:57 -07001011
1012 /* Apply the new configuration */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001013 rc = iwl3945_send_cmd_pdu(priv, REPLY_RXON,
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001014 sizeof(struct iwl3945_rxon_cmd), &priv->staging39_rxon);
Zhu Yib481de92007-09-25 17:54:57 -07001015 if (rc) {
1016 IWL_ERROR("Error setting new configuration (%d).\n", rc);
1017 return rc;
1018 }
1019
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001020 memcpy(active_rxon, &priv->staging39_rxon, sizeof(*active_rxon));
Zhu Yib481de92007-09-25 17:54:57 -07001021
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001022 iwl3945_clear_stations_table(priv);
Zhu Yi556f8db2007-09-27 11:27:33 +08001023
Zhu Yib481de92007-09-25 17:54:57 -07001024 /* If we issue a new RXON command which required a tune then we must
1025 * send a new TXPOWER command or we won't be able to Tx any frames */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001026 rc = iwl3945_hw_reg_send_txpower(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001027 if (rc) {
1028 IWL_ERROR("Error setting Tx power (%d).\n", rc);
1029 return rc;
1030 }
1031
1032 /* Add the broadcast address so we can send broadcast frames */
Kolekar, Abhijeetb5323d32008-12-19 10:37:22 +08001033 if (iwl3945_add_station(priv, iwl_bcast_addr, 0, 0) ==
Zhu Yib481de92007-09-25 17:54:57 -07001034 IWL_INVALID_STATION) {
1035 IWL_ERROR("Error adding BROADCAST address for transmit.\n");
1036 return -EIO;
1037 }
1038
1039 /* If we have set the ASSOC_MSK and we are in BSS mode then
1040 * add the IWL_AP_ID to the station rate table */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001041 if (iwl3945_is_associated(priv) &&
Johannes Berg05c914f2008-09-11 00:01:58 +02001042 (priv->iw_mode == NL80211_IFTYPE_STATION))
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001043 if (iwl3945_add_station(priv, priv->active39_rxon.bssid_addr, 1, 0)
Zhu Yib481de92007-09-25 17:54:57 -07001044 == IWL_INVALID_STATION) {
1045 IWL_ERROR("Error adding AP address for transmit.\n");
1046 return -EIO;
1047 }
1048
Johannes Berg8318d782008-01-24 19:38:38 +01001049 /* Init the hardware's rate fallback order based on the band */
Zhu Yib481de92007-09-25 17:54:57 -07001050 rc = iwl3945_init_hw_rate_table(priv);
1051 if (rc) {
1052 IWL_ERROR("Error setting HW rate table: %02X\n", rc);
1053 return -EIO;
1054 }
1055
1056 return 0;
1057}
1058
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001059static int iwl3945_send_bt_config(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001060{
Tomas Winkler4c897252008-12-19 10:37:05 +08001061 struct iwl_bt_cmd bt_cmd = {
Zhu Yib481de92007-09-25 17:54:57 -07001062 .flags = 3,
1063 .lead_time = 0xAA,
1064 .max_kill = 1,
1065 .kill_ack_mask = 0,
1066 .kill_cts_mask = 0,
1067 };
1068
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001069 return iwl3945_send_cmd_pdu(priv, REPLY_BT_CONFIG,
Tomas Winkler4c897252008-12-19 10:37:05 +08001070 sizeof(bt_cmd), &bt_cmd);
Zhu Yib481de92007-09-25 17:54:57 -07001071}
1072
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001073static int iwl3945_send_scan_abort(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001074{
1075 int rc = 0;
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08001076 struct iwl_rx_packet *res;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001077 struct iwl3945_host_cmd cmd = {
Zhu Yib481de92007-09-25 17:54:57 -07001078 .id = REPLY_SCAN_ABORT_CMD,
1079 .meta.flags = CMD_WANT_SKB,
1080 };
1081
1082 /* If there isn't a scan actively going on in the hardware
1083 * then we are in between scan bands and not actually
1084 * actively scanning, so don't send the abort command */
1085 if (!test_bit(STATUS_SCAN_HW, &priv->status)) {
1086 clear_bit(STATUS_SCAN_ABORTING, &priv->status);
1087 return 0;
1088 }
1089
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001090 rc = iwl3945_send_cmd_sync(priv, &cmd);
Zhu Yib481de92007-09-25 17:54:57 -07001091 if (rc) {
1092 clear_bit(STATUS_SCAN_ABORTING, &priv->status);
1093 return rc;
1094 }
1095
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08001096 res = (struct iwl_rx_packet *)cmd.meta.u.skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07001097 if (res->u.status != CAN_ABORT_STATUS) {
1098 /* The scan abort will return 1 for success or
1099 * 2 for "failure". A failure condition can be
1100 * due to simply not being in an active scan which
1101 * can occur if we send the scan abort before we
1102 * the microcode has notified us that a scan is
1103 * completed. */
1104 IWL_DEBUG_INFO("SCAN_ABORT returned %d.\n", res->u.status);
1105 clear_bit(STATUS_SCAN_ABORTING, &priv->status);
1106 clear_bit(STATUS_SCAN_HW, &priv->status);
1107 }
1108
1109 dev_kfree_skb_any(cmd.meta.u.skb);
1110
1111 return rc;
1112}
1113
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001114static int iwl3945_card_state_sync_callback(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001115 struct iwl3945_cmd *cmd,
Zhu Yib481de92007-09-25 17:54:57 -07001116 struct sk_buff *skb)
1117{
1118 return 1;
1119}
1120
1121/*
1122 * CARD_STATE_CMD
1123 *
Ben Cahill9fbab512007-11-29 11:09:47 +08001124 * Use: Sets the device's internal card state to enable, disable, or halt
Zhu Yib481de92007-09-25 17:54:57 -07001125 *
1126 * When in the 'enable' state the card operates as normal.
1127 * When in the 'disable' state, the card enters into a low power mode.
1128 * When in the 'halt' state, the card is shut down and must be fully
1129 * restarted to come back on.
1130 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001131static int iwl3945_send_card_state(struct iwl_priv *priv, u32 flags, u8 meta_flag)
Zhu Yib481de92007-09-25 17:54:57 -07001132{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001133 struct iwl3945_host_cmd cmd = {
Zhu Yib481de92007-09-25 17:54:57 -07001134 .id = REPLY_CARD_STATE_CMD,
1135 .len = sizeof(u32),
1136 .data = &flags,
1137 .meta.flags = meta_flag,
1138 };
1139
1140 if (meta_flag & CMD_ASYNC)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001141 cmd.meta.u.callback = iwl3945_card_state_sync_callback;
Zhu Yib481de92007-09-25 17:54:57 -07001142
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001143 return iwl3945_send_cmd(priv, &cmd);
Zhu Yib481de92007-09-25 17:54:57 -07001144}
1145
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001146static int iwl3945_add_sta_sync_callback(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001147 struct iwl3945_cmd *cmd, struct sk_buff *skb)
Zhu Yib481de92007-09-25 17:54:57 -07001148{
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08001149 struct iwl_rx_packet *res = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07001150
1151 if (!skb) {
1152 IWL_ERROR("Error: Response NULL in REPLY_ADD_STA.\n");
1153 return 1;
1154 }
1155
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08001156 res = (struct iwl_rx_packet *)skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07001157 if (res->hdr.flags & IWL_CMD_FAILED_MSK) {
1158 IWL_ERROR("Bad return from REPLY_ADD_STA (0x%08X)\n",
1159 res->hdr.flags);
1160 return 1;
1161 }
1162
1163 switch (res->u.add_sta.status) {
1164 case ADD_STA_SUCCESS_MSK:
1165 break;
1166 default:
1167 break;
1168 }
1169
1170 /* We didn't cache the SKB; let the caller free it */
1171 return 1;
1172}
1173
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001174int iwl3945_send_add_station(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001175 struct iwl3945_addsta_cmd *sta, u8 flags)
Zhu Yib481de92007-09-25 17:54:57 -07001176{
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08001177 struct iwl_rx_packet *res = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07001178 int rc = 0;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001179 struct iwl3945_host_cmd cmd = {
Zhu Yib481de92007-09-25 17:54:57 -07001180 .id = REPLY_ADD_STA,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001181 .len = sizeof(struct iwl3945_addsta_cmd),
Zhu Yib481de92007-09-25 17:54:57 -07001182 .meta.flags = flags,
1183 .data = sta,
1184 };
1185
1186 if (flags & CMD_ASYNC)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001187 cmd.meta.u.callback = iwl3945_add_sta_sync_callback;
Zhu Yib481de92007-09-25 17:54:57 -07001188 else
1189 cmd.meta.flags |= CMD_WANT_SKB;
1190
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001191 rc = iwl3945_send_cmd(priv, &cmd);
Zhu Yib481de92007-09-25 17:54:57 -07001192
1193 if (rc || (flags & CMD_ASYNC))
1194 return rc;
1195
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08001196 res = (struct iwl_rx_packet *)cmd.meta.u.skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07001197 if (res->hdr.flags & IWL_CMD_FAILED_MSK) {
1198 IWL_ERROR("Bad return from REPLY_ADD_STA (0x%08X)\n",
1199 res->hdr.flags);
1200 rc = -EIO;
1201 }
1202
1203 if (rc == 0) {
1204 switch (res->u.add_sta.status) {
1205 case ADD_STA_SUCCESS_MSK:
1206 IWL_DEBUG_INFO("REPLY_ADD_STA PASSED\n");
1207 break;
1208 default:
1209 rc = -EIO;
Winkler, Tomas39aadf82008-12-19 10:37:32 +08001210 IWL_WARN(priv, "REPLY_ADD_STA failed\n");
Zhu Yib481de92007-09-25 17:54:57 -07001211 break;
1212 }
1213 }
1214
1215 priv->alloc_rxb_skb--;
1216 dev_kfree_skb_any(cmd.meta.u.skb);
1217
1218 return rc;
1219}
1220
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001221static int iwl3945_update_sta_key_info(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07001222 struct ieee80211_key_conf *keyconf,
1223 u8 sta_id)
1224{
1225 unsigned long flags;
1226 __le16 key_flags = 0;
1227
1228 switch (keyconf->alg) {
1229 case ALG_CCMP:
1230 key_flags |= STA_KEY_FLG_CCMP;
1231 key_flags |= cpu_to_le16(
1232 keyconf->keyidx << STA_KEY_FLG_KEYID_POS);
1233 key_flags &= ~STA_KEY_FLG_INVALID;
1234 break;
1235 case ALG_TKIP:
1236 case ALG_WEP:
Zhu Yib481de92007-09-25 17:54:57 -07001237 default:
1238 return -EINVAL;
1239 }
1240 spin_lock_irqsave(&priv->sta_lock, flags);
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001241 priv->stations_39[sta_id].keyinfo.alg = keyconf->alg;
1242 priv->stations_39[sta_id].keyinfo.keylen = keyconf->keylen;
1243 memcpy(priv->stations_39[sta_id].keyinfo.key, keyconf->key,
Zhu Yib481de92007-09-25 17:54:57 -07001244 keyconf->keylen);
1245
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001246 memcpy(priv->stations_39[sta_id].sta.key.key, keyconf->key,
Zhu Yib481de92007-09-25 17:54:57 -07001247 keyconf->keylen);
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001248 priv->stations_39[sta_id].sta.key.key_flags = key_flags;
1249 priv->stations_39[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
1250 priv->stations_39[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07001251
1252 spin_unlock_irqrestore(&priv->sta_lock, flags);
1253
1254 IWL_DEBUG_INFO("hwcrypto: modify ucode station key info\n");
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001255 iwl3945_send_add_station(priv, &priv->stations_39[sta_id].sta, 0);
Zhu Yib481de92007-09-25 17:54:57 -07001256 return 0;
1257}
1258
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001259static int iwl3945_clear_sta_key_info(struct iwl_priv *priv, u8 sta_id)
Zhu Yib481de92007-09-25 17:54:57 -07001260{
1261 unsigned long flags;
1262
1263 spin_lock_irqsave(&priv->sta_lock, flags);
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001264 memset(&priv->stations_39[sta_id].keyinfo, 0, sizeof(struct iwl3945_hw_key));
1265 memset(&priv->stations_39[sta_id].sta.key, 0,
Tomas Winkler4c897252008-12-19 10:37:05 +08001266 sizeof(struct iwl4965_keyinfo));
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001267 priv->stations_39[sta_id].sta.key.key_flags = STA_KEY_FLG_NO_ENC;
1268 priv->stations_39[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
1269 priv->stations_39[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07001270 spin_unlock_irqrestore(&priv->sta_lock, flags);
1271
1272 IWL_DEBUG_INFO("hwcrypto: clear ucode station key info\n");
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001273 iwl3945_send_add_station(priv, &priv->stations_39[sta_id].sta, 0);
Zhu Yib481de92007-09-25 17:54:57 -07001274 return 0;
1275}
1276
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001277static void iwl3945_clear_free_frames(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001278{
1279 struct list_head *element;
1280
1281 IWL_DEBUG_INFO("%d frames on pre-allocated heap on clear.\n",
1282 priv->frames_count);
1283
1284 while (!list_empty(&priv->free_frames)) {
1285 element = priv->free_frames.next;
1286 list_del(element);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001287 kfree(list_entry(element, struct iwl3945_frame, list));
Zhu Yib481de92007-09-25 17:54:57 -07001288 priv->frames_count--;
1289 }
1290
1291 if (priv->frames_count) {
Winkler, Tomas39aadf82008-12-19 10:37:32 +08001292 IWL_WARN(priv, "%d frames still in use. Did we lose one?\n",
Zhu Yib481de92007-09-25 17:54:57 -07001293 priv->frames_count);
1294 priv->frames_count = 0;
1295 }
1296}
1297
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001298static struct iwl3945_frame *iwl3945_get_free_frame(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001299{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001300 struct iwl3945_frame *frame;
Zhu Yib481de92007-09-25 17:54:57 -07001301 struct list_head *element;
1302 if (list_empty(&priv->free_frames)) {
1303 frame = kzalloc(sizeof(*frame), GFP_KERNEL);
1304 if (!frame) {
1305 IWL_ERROR("Could not allocate frame!\n");
1306 return NULL;
1307 }
1308
1309 priv->frames_count++;
1310 return frame;
1311 }
1312
1313 element = priv->free_frames.next;
1314 list_del(element);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001315 return list_entry(element, struct iwl3945_frame, list);
Zhu Yib481de92007-09-25 17:54:57 -07001316}
1317
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001318static void iwl3945_free_frame(struct iwl_priv *priv, struct iwl3945_frame *frame)
Zhu Yib481de92007-09-25 17:54:57 -07001319{
1320 memset(frame, 0, sizeof(*frame));
1321 list_add(&frame->list, &priv->free_frames);
1322}
1323
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001324unsigned int iwl3945_fill_beacon_frame(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07001325 struct ieee80211_hdr *hdr,
Rami Rosen73ec1cc2008-12-16 09:37:07 +02001326 int left)
Zhu Yib481de92007-09-25 17:54:57 -07001327{
1328
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001329 if (!iwl3945_is_associated(priv) || !priv->ibss_beacon ||
Johannes Berg05c914f2008-09-11 00:01:58 +02001330 ((priv->iw_mode != NL80211_IFTYPE_ADHOC) &&
1331 (priv->iw_mode != NL80211_IFTYPE_AP)))
Zhu Yib481de92007-09-25 17:54:57 -07001332 return 0;
1333
1334 if (priv->ibss_beacon->len > left)
1335 return 0;
1336
1337 memcpy(hdr, priv->ibss_beacon->data, priv->ibss_beacon->len);
1338
1339 return priv->ibss_beacon->len;
1340}
1341
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001342static u8 iwl3945_rate_get_lowest_plcp(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001343{
1344 u8 i;
Kolekar, Abhijeetc24f0812008-11-07 09:58:44 -08001345 int rate_mask;
1346
1347 /* Set rate mask*/
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001348 if (priv->staging39_rxon.flags & RXON_FLG_BAND_24G_MSK)
Chatre, Reinettedbce56a2008-11-12 13:14:07 -08001349 rate_mask = priv->active_rate_basic & IWL_CCK_RATES_MASK;
Kolekar, Abhijeetc24f0812008-11-07 09:58:44 -08001350 else
Chatre, Reinettedbce56a2008-11-12 13:14:07 -08001351 rate_mask = priv->active_rate_basic & IWL_OFDM_RATES_MASK;
Zhu Yib481de92007-09-25 17:54:57 -07001352
1353 for (i = IWL_RATE_1M_INDEX; i != IWL_RATE_INVALID;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001354 i = iwl3945_rates[i].next_ieee) {
Zhu Yib481de92007-09-25 17:54:57 -07001355 if (rate_mask & (1 << i))
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001356 return iwl3945_rates[i].plcp;
Zhu Yib481de92007-09-25 17:54:57 -07001357 }
1358
Kolekar, Abhijeetc24f0812008-11-07 09:58:44 -08001359 /* No valid rate was found. Assign the lowest one */
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001360 if (priv->staging39_rxon.flags & RXON_FLG_BAND_24G_MSK)
Kolekar, Abhijeetc24f0812008-11-07 09:58:44 -08001361 return IWL_RATE_1M_PLCP;
1362 else
1363 return IWL_RATE_6M_PLCP;
Zhu Yib481de92007-09-25 17:54:57 -07001364}
1365
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001366static int iwl3945_send_beacon_cmd(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001367{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001368 struct iwl3945_frame *frame;
Zhu Yib481de92007-09-25 17:54:57 -07001369 unsigned int frame_size;
1370 int rc;
1371 u8 rate;
1372
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001373 frame = iwl3945_get_free_frame(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001374
1375 if (!frame) {
1376 IWL_ERROR("Could not obtain free frame buffer for beacon "
1377 "command.\n");
1378 return -ENOMEM;
1379 }
1380
Kolekar, Abhijeetc24f0812008-11-07 09:58:44 -08001381 rate = iwl3945_rate_get_lowest_plcp(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001382
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001383 frame_size = iwl3945_hw_get_beacon_cmd(priv, frame, rate);
Zhu Yib481de92007-09-25 17:54:57 -07001384
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001385 rc = iwl3945_send_cmd_pdu(priv, REPLY_TX_BEACON, frame_size,
Zhu Yib481de92007-09-25 17:54:57 -07001386 &frame->u.cmd[0]);
1387
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001388 iwl3945_free_frame(priv, frame);
Zhu Yib481de92007-09-25 17:54:57 -07001389
1390 return rc;
1391}
1392
1393/******************************************************************************
1394 *
1395 * EEPROM related functions
1396 *
1397 ******************************************************************************/
1398
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001399static void get_eeprom_mac(struct iwl_priv *priv, u8 *mac)
Zhu Yib481de92007-09-25 17:54:57 -07001400{
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001401 memcpy(mac, priv->eeprom39.mac_address, 6);
Zhu Yib481de92007-09-25 17:54:57 -07001402}
1403
Reinette Chatre74a3a252008-01-23 10:15:19 -08001404/*
1405 * Clear the OWNER_MSK, to establish driver (instead of uCode running on
1406 * embedded controller) as EEPROM reader; each read is a series of pulses
1407 * to/from the EEPROM chip, not a single event, so even reads could conflict
1408 * if they weren't arbitrated by some ownership mechanism. Here, the driver
1409 * simply claims ownership, which should be safe when this function is called
1410 * (i.e. before loading uCode!).
1411 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001412static inline int iwl3945_eeprom_acquire_semaphore(struct iwl_priv *priv)
Reinette Chatre74a3a252008-01-23 10:15:19 -08001413{
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08001414 _iwl_clear_bit(priv, CSR_EEPROM_GP, CSR_EEPROM_GP_IF_OWNER_MSK);
Reinette Chatre74a3a252008-01-23 10:15:19 -08001415 return 0;
1416}
1417
Zhu Yib481de92007-09-25 17:54:57 -07001418/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001419 * iwl3945_eeprom_init - read EEPROM contents
Zhu Yib481de92007-09-25 17:54:57 -07001420 *
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001421 * Load the EEPROM contents from adapter into priv->eeprom39
Zhu Yib481de92007-09-25 17:54:57 -07001422 *
1423 * NOTE: This routine uses the non-debug IO access functions.
1424 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001425int iwl3945_eeprom_init(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001426{
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001427 u16 *e = (u16 *)&priv->eeprom39;
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08001428 u32 gp = iwl_read32(priv, CSR_EEPROM_GP);
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001429 int sz = sizeof(priv->eeprom39);
Zhu, Yi3d5717a2008-12-11 10:33:36 -08001430 int ret;
Zhu Yib481de92007-09-25 17:54:57 -07001431 u16 addr;
1432
1433 /* The EEPROM structure has several padding buffers within it
1434 * and when adding new EEPROM maps is subject to programmer errors
1435 * which may be very difficult to identify without explicitly
1436 * checking the resulting size of the eeprom map. */
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001437 BUILD_BUG_ON(sizeof(priv->eeprom39) != IWL_EEPROM_IMAGE_SIZE);
Zhu Yib481de92007-09-25 17:54:57 -07001438
1439 if ((gp & CSR_EEPROM_GP_VALID_MSK) == CSR_EEPROM_GP_BAD_SIGNATURE) {
Jiri Slaby6f147922008-08-11 23:49:41 +02001440 IWL_ERROR("EEPROM not found, EEPROM_GP=0x%08x\n", gp);
Zhu Yib481de92007-09-25 17:54:57 -07001441 return -ENOENT;
1442 }
1443
Cahill, Ben M6440adb2007-11-29 11:09:55 +08001444 /* Make sure driver (instead of uCode) is allowed to read EEPROM */
Zhu, Yi3d5717a2008-12-11 10:33:36 -08001445 ret = iwl3945_eeprom_acquire_semaphore(priv);
1446 if (ret < 0) {
Ian Schram91e17472007-10-25 17:15:23 +08001447 IWL_ERROR("Failed to acquire EEPROM semaphore.\n");
Zhu Yib481de92007-09-25 17:54:57 -07001448 return -ENOENT;
1449 }
1450
1451 /* eeprom is an array of 16bit values */
1452 for (addr = 0; addr < sz; addr += sizeof(u16)) {
Zhu, Yi3d5717a2008-12-11 10:33:36 -08001453 u32 r;
1454
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08001455 _iwl_write32(priv, CSR_EEPROM_REG,
Zhu, Yi3d5717a2008-12-11 10:33:36 -08001456 CSR_EEPROM_REG_MSK_ADDR & (addr << 1));
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08001457 _iwl_clear_bit(priv, CSR_EEPROM_REG, CSR_EEPROM_REG_BIT_CMD);
1458 ret = iwl_poll_direct_bit(priv, CSR_EEPROM_REG,
Zhu, Yi3d5717a2008-12-11 10:33:36 -08001459 CSR_EEPROM_REG_READ_VALID_MSK,
1460 IWL_EEPROM_ACCESS_TIMEOUT);
1461 if (ret < 0) {
Jiri Slaby6f147922008-08-11 23:49:41 +02001462 IWL_ERROR("Time out reading EEPROM[%d]\n", addr);
Zhu, Yi3d5717a2008-12-11 10:33:36 -08001463 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07001464 }
Zhu, Yi3d5717a2008-12-11 10:33:36 -08001465
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08001466 r = _iwl_read_direct32(priv, CSR_EEPROM_REG);
Tomas Winkler58ff6d42008-02-13 02:47:54 +02001467 e[addr / 2] = le16_to_cpu((__force __le16)(r >> 16));
Zhu Yib481de92007-09-25 17:54:57 -07001468 }
1469
1470 return 0;
1471}
1472
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001473static void iwl3945_unset_hw_params(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001474{
Abhijeet Kolekar3832ec92008-12-19 10:37:26 +08001475 if (priv->shared_virt)
Zhu Yib481de92007-09-25 17:54:57 -07001476 pci_free_consistent(priv->pci_dev,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001477 sizeof(struct iwl3945_shared),
Abhijeet Kolekar3832ec92008-12-19 10:37:26 +08001478 priv->shared_virt,
1479 priv->shared_phys);
Zhu Yib481de92007-09-25 17:54:57 -07001480}
1481
1482/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001483 * iwl3945_supported_rate_to_ie - fill in the supported rate in IE field
Zhu Yib481de92007-09-25 17:54:57 -07001484 *
1485 * return : set the bit for each supported rate insert in ie
1486 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001487static u16 iwl3945_supported_rate_to_ie(u8 *ie, u16 supported_rate,
Tomas Winklerc7c46672007-10-18 02:04:15 +02001488 u16 basic_rate, int *left)
Zhu Yib481de92007-09-25 17:54:57 -07001489{
1490 u16 ret_rates = 0, bit;
1491 int i;
Tomas Winklerc7c46672007-10-18 02:04:15 +02001492 u8 *cnt = ie;
1493 u8 *rates = ie + 1;
Zhu Yib481de92007-09-25 17:54:57 -07001494
1495 for (bit = 1, i = 0; i < IWL_RATE_COUNT; i++, bit <<= 1) {
1496 if (bit & supported_rate) {
1497 ret_rates |= bit;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001498 rates[*cnt] = iwl3945_rates[i].ieee |
Tomas Winklerc7c46672007-10-18 02:04:15 +02001499 ((bit & basic_rate) ? 0x80 : 0x00);
1500 (*cnt)++;
1501 (*left)--;
1502 if ((*left <= 0) ||
1503 (*cnt >= IWL_SUPPORTED_RATES_IE_LEN))
Zhu Yib481de92007-09-25 17:54:57 -07001504 break;
1505 }
1506 }
1507
1508 return ret_rates;
1509}
1510
1511/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001512 * iwl3945_fill_probe_req - fill in all required fields and IE for probe request
Zhu Yib481de92007-09-25 17:54:57 -07001513 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001514static u16 iwl3945_fill_probe_req(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07001515 struct ieee80211_mgmt *frame,
Johannes Berg430cfe92008-10-28 18:06:02 +01001516 int left)
Zhu Yib481de92007-09-25 17:54:57 -07001517{
1518 int len = 0;
1519 u8 *pos = NULL;
Tomas Winklerc7c46672007-10-18 02:04:15 +02001520 u16 active_rates, ret_rates, cck_rates;
Zhu Yib481de92007-09-25 17:54:57 -07001521
1522 /* Make sure there is enough space for the probe request,
1523 * two mandatory IEs and the data */
1524 left -= 24;
1525 if (left < 0)
1526 return 0;
1527 len += 24;
1528
1529 frame->frame_control = cpu_to_le16(IEEE80211_STYPE_PROBE_REQ);
Kolekar, Abhijeetb5323d32008-12-19 10:37:22 +08001530 memcpy(frame->da, iwl_bcast_addr, ETH_ALEN);
Zhu Yib481de92007-09-25 17:54:57 -07001531 memcpy(frame->sa, priv->mac_addr, ETH_ALEN);
Kolekar, Abhijeetb5323d32008-12-19 10:37:22 +08001532 memcpy(frame->bssid, iwl_bcast_addr, ETH_ALEN);
Zhu Yib481de92007-09-25 17:54:57 -07001533 frame->seq_ctrl = 0;
1534
1535 /* fill in our indirect SSID IE */
1536 /* ...next IE... */
1537
1538 left -= 2;
1539 if (left < 0)
1540 return 0;
1541 len += 2;
1542 pos = &(frame->u.probe_req.variable[0]);
1543 *pos++ = WLAN_EID_SSID;
1544 *pos++ = 0;
1545
Zhu Yib481de92007-09-25 17:54:57 -07001546 /* fill in supported rate */
1547 /* ...next IE... */
1548 left -= 2;
1549 if (left < 0)
1550 return 0;
Tomas Winklerc7c46672007-10-18 02:04:15 +02001551
Zhu Yib481de92007-09-25 17:54:57 -07001552 /* ... fill it in... */
1553 *pos++ = WLAN_EID_SUPP_RATES;
1554 *pos = 0;
Tomas Winklerc7c46672007-10-18 02:04:15 +02001555
1556 priv->active_rate = priv->rates_mask;
1557 active_rates = priv->active_rate;
Zhu Yib481de92007-09-25 17:54:57 -07001558 priv->active_rate_basic = priv->rates_mask & IWL_BASIC_RATES_MASK;
1559
Tomas Winklerc7c46672007-10-18 02:04:15 +02001560 cck_rates = IWL_CCK_RATES_MASK & active_rates;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001561 ret_rates = iwl3945_supported_rate_to_ie(pos, cck_rates,
Tomas Winklerc7c46672007-10-18 02:04:15 +02001562 priv->active_rate_basic, &left);
1563 active_rates &= ~ret_rates;
1564
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001565 ret_rates = iwl3945_supported_rate_to_ie(pos, active_rates,
Tomas Winklerc7c46672007-10-18 02:04:15 +02001566 priv->active_rate_basic, &left);
1567 active_rates &= ~ret_rates;
1568
Zhu Yib481de92007-09-25 17:54:57 -07001569 len += 2 + *pos;
1570 pos += (*pos) + 1;
Tomas Winklerc7c46672007-10-18 02:04:15 +02001571 if (active_rates == 0)
Zhu Yib481de92007-09-25 17:54:57 -07001572 goto fill_end;
1573
1574 /* fill in supported extended rate */
1575 /* ...next IE... */
1576 left -= 2;
1577 if (left < 0)
1578 return 0;
1579 /* ... fill it in... */
1580 *pos++ = WLAN_EID_EXT_SUPP_RATES;
1581 *pos = 0;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001582 iwl3945_supported_rate_to_ie(pos, active_rates,
Tomas Winklerc7c46672007-10-18 02:04:15 +02001583 priv->active_rate_basic, &left);
Zhu Yib481de92007-09-25 17:54:57 -07001584 if (*pos > 0)
1585 len += 2 + *pos;
1586
1587 fill_end:
1588 return (u16)len;
1589}
1590
1591/*
1592 * QoS support
1593*/
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001594static int iwl3945_send_qos_params_command(struct iwl_priv *priv,
Tomas Winkler4c897252008-12-19 10:37:05 +08001595 struct iwl_qosparam_cmd *qos)
Zhu Yib481de92007-09-25 17:54:57 -07001596{
1597
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001598 return iwl3945_send_cmd_pdu(priv, REPLY_QOS_PARAM,
Tomas Winkler4c897252008-12-19 10:37:05 +08001599 sizeof(struct iwl_qosparam_cmd), qos);
Zhu Yib481de92007-09-25 17:54:57 -07001600}
1601
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001602static void iwl3945_reset_qos(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001603{
1604 u16 cw_min = 15;
1605 u16 cw_max = 1023;
1606 u8 aifs = 2;
1607 u8 is_legacy = 0;
1608 unsigned long flags;
1609 int i;
1610
1611 spin_lock_irqsave(&priv->lock, flags);
1612 priv->qos_data.qos_active = 0;
1613
Winkler, Tomas6d1ef1a2008-12-09 11:29:00 -08001614 /* QoS always active in AP and ADHOC mode
1615 * In STA mode wait for association
1616 */
1617 if (priv->iw_mode == NL80211_IFTYPE_ADHOC ||
1618 priv->iw_mode == NL80211_IFTYPE_AP)
1619 priv->qos_data.qos_active = 1;
1620 else
1621 priv->qos_data.qos_active = 0;
1622
1623
1624 /* check for legacy mode */
1625 if ((priv->iw_mode == NL80211_IFTYPE_ADHOC &&
1626 (priv->active_rate & IWL_OFDM_RATES_MASK) == 0) ||
1627 (priv->iw_mode == NL80211_IFTYPE_STATION &&
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001628 (priv->staging39_rxon.flags & RXON_FLG_SHORT_SLOT_MSK) == 0)) {
Zhu Yib481de92007-09-25 17:54:57 -07001629 cw_min = 31;
1630 is_legacy = 1;
1631 }
1632
1633 if (priv->qos_data.qos_active)
1634 aifs = 3;
1635
1636 priv->qos_data.def_qos_parm.ac[0].cw_min = cpu_to_le16(cw_min);
1637 priv->qos_data.def_qos_parm.ac[0].cw_max = cpu_to_le16(cw_max);
1638 priv->qos_data.def_qos_parm.ac[0].aifsn = aifs;
1639 priv->qos_data.def_qos_parm.ac[0].edca_txop = 0;
1640 priv->qos_data.def_qos_parm.ac[0].reserved1 = 0;
1641
1642 if (priv->qos_data.qos_active) {
1643 i = 1;
1644 priv->qos_data.def_qos_parm.ac[i].cw_min = cpu_to_le16(cw_min);
1645 priv->qos_data.def_qos_parm.ac[i].cw_max = cpu_to_le16(cw_max);
1646 priv->qos_data.def_qos_parm.ac[i].aifsn = 7;
1647 priv->qos_data.def_qos_parm.ac[i].edca_txop = 0;
1648 priv->qos_data.def_qos_parm.ac[i].reserved1 = 0;
1649
1650 i = 2;
1651 priv->qos_data.def_qos_parm.ac[i].cw_min =
1652 cpu_to_le16((cw_min + 1) / 2 - 1);
1653 priv->qos_data.def_qos_parm.ac[i].cw_max =
1654 cpu_to_le16(cw_max);
1655 priv->qos_data.def_qos_parm.ac[i].aifsn = 2;
1656 if (is_legacy)
1657 priv->qos_data.def_qos_parm.ac[i].edca_txop =
1658 cpu_to_le16(6016);
1659 else
1660 priv->qos_data.def_qos_parm.ac[i].edca_txop =
1661 cpu_to_le16(3008);
1662 priv->qos_data.def_qos_parm.ac[i].reserved1 = 0;
1663
1664 i = 3;
1665 priv->qos_data.def_qos_parm.ac[i].cw_min =
1666 cpu_to_le16((cw_min + 1) / 4 - 1);
1667 priv->qos_data.def_qos_parm.ac[i].cw_max =
1668 cpu_to_le16((cw_max + 1) / 2 - 1);
1669 priv->qos_data.def_qos_parm.ac[i].aifsn = 2;
1670 priv->qos_data.def_qos_parm.ac[i].reserved1 = 0;
1671 if (is_legacy)
1672 priv->qos_data.def_qos_parm.ac[i].edca_txop =
1673 cpu_to_le16(3264);
1674 else
1675 priv->qos_data.def_qos_parm.ac[i].edca_txop =
1676 cpu_to_le16(1504);
1677 } else {
1678 for (i = 1; i < 4; i++) {
1679 priv->qos_data.def_qos_parm.ac[i].cw_min =
1680 cpu_to_le16(cw_min);
1681 priv->qos_data.def_qos_parm.ac[i].cw_max =
1682 cpu_to_le16(cw_max);
1683 priv->qos_data.def_qos_parm.ac[i].aifsn = aifs;
1684 priv->qos_data.def_qos_parm.ac[i].edca_txop = 0;
1685 priv->qos_data.def_qos_parm.ac[i].reserved1 = 0;
1686 }
1687 }
1688 IWL_DEBUG_QOS("set QoS to default \n");
1689
1690 spin_unlock_irqrestore(&priv->lock, flags);
1691}
1692
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001693static void iwl3945_activate_qos(struct iwl_priv *priv, u8 force)
Zhu Yib481de92007-09-25 17:54:57 -07001694{
1695 unsigned long flags;
1696
Zhu Yib481de92007-09-25 17:54:57 -07001697 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
1698 return;
1699
Zhu Yib481de92007-09-25 17:54:57 -07001700 spin_lock_irqsave(&priv->lock, flags);
1701 priv->qos_data.def_qos_parm.qos_flags = 0;
1702
1703 if (priv->qos_data.qos_cap.q_AP.queue_request &&
1704 !priv->qos_data.qos_cap.q_AP.txop_request)
1705 priv->qos_data.def_qos_parm.qos_flags |=
1706 QOS_PARAM_FLG_TXOP_TYPE_MSK;
1707
1708 if (priv->qos_data.qos_active)
1709 priv->qos_data.def_qos_parm.qos_flags |=
1710 QOS_PARAM_FLG_UPDATE_EDCA_MSK;
1711
1712 spin_unlock_irqrestore(&priv->lock, flags);
1713
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001714 if (force || iwl3945_is_associated(priv)) {
Tomas Winklera96a27f2008-10-23 23:48:56 -07001715 IWL_DEBUG_QOS("send QoS cmd with QoS active %d \n",
Zhu Yib481de92007-09-25 17:54:57 -07001716 priv->qos_data.qos_active);
1717
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001718 iwl3945_send_qos_params_command(priv,
Zhu Yib481de92007-09-25 17:54:57 -07001719 &(priv->qos_data.def_qos_parm));
1720 }
1721}
1722
Zhu Yib481de92007-09-25 17:54:57 -07001723/*
1724 * Power management (not Tx power!) functions
1725 */
1726#define MSEC_TO_USEC 1024
1727
Tomas Winkler600c0e12008-12-19 10:37:04 +08001728
1729#define NOSLP __constant_cpu_to_le16(0), 0, 0
1730#define SLP IWL_POWER_DRIVER_ALLOW_SLEEP_MSK, 0, 0
Zhu Yib481de92007-09-25 17:54:57 -07001731#define SLP_TIMEOUT(T) __constant_cpu_to_le32((T) * MSEC_TO_USEC)
1732#define SLP_VEC(X0, X1, X2, X3, X4) {__constant_cpu_to_le32(X0), \
1733 __constant_cpu_to_le32(X1), \
1734 __constant_cpu_to_le32(X2), \
1735 __constant_cpu_to_le32(X3), \
1736 __constant_cpu_to_le32(X4)}
1737
Zhu Yib481de92007-09-25 17:54:57 -07001738/* default power management (not Tx power) table values */
Tomas Winklera96a27f2008-10-23 23:48:56 -07001739/* for TIM 0-10 */
Samuel Ortiz1125eff2008-12-19 10:37:14 +08001740static struct iwl_power_vec_entry range_0[IWL39_POWER_AC] = {
Zhu Yib481de92007-09-25 17:54:57 -07001741 {{NOSLP, SLP_TIMEOUT(0), SLP_TIMEOUT(0), SLP_VEC(0, 0, 0, 0, 0)}, 0},
1742 {{SLP, SLP_TIMEOUT(200), SLP_TIMEOUT(500), SLP_VEC(1, 2, 3, 4, 4)}, 0},
1743 {{SLP, SLP_TIMEOUT(200), SLP_TIMEOUT(300), SLP_VEC(2, 4, 6, 7, 7)}, 0},
1744 {{SLP, SLP_TIMEOUT(50), SLP_TIMEOUT(100), SLP_VEC(2, 6, 9, 9, 10)}, 0},
1745 {{SLP, SLP_TIMEOUT(50), SLP_TIMEOUT(25), SLP_VEC(2, 7, 9, 9, 10)}, 1},
1746 {{SLP, SLP_TIMEOUT(25), SLP_TIMEOUT(25), SLP_VEC(4, 7, 10, 10, 10)}, 1}
1747};
1748
Tomas Winklera96a27f2008-10-23 23:48:56 -07001749/* for TIM > 10 */
Samuel Ortiz1125eff2008-12-19 10:37:14 +08001750static struct iwl_power_vec_entry range_1[IWL39_POWER_AC] = {
Zhu Yib481de92007-09-25 17:54:57 -07001751 {{NOSLP, SLP_TIMEOUT(0), SLP_TIMEOUT(0), SLP_VEC(0, 0, 0, 0, 0)}, 0},
1752 {{SLP, SLP_TIMEOUT(200), SLP_TIMEOUT(500),
1753 SLP_VEC(1, 2, 3, 4, 0xFF)}, 0},
1754 {{SLP, SLP_TIMEOUT(200), SLP_TIMEOUT(300),
1755 SLP_VEC(2, 4, 6, 7, 0xFF)}, 0},
1756 {{SLP, SLP_TIMEOUT(50), SLP_TIMEOUT(100),
1757 SLP_VEC(2, 6, 9, 9, 0xFF)}, 0},
1758 {{SLP, SLP_TIMEOUT(50), SLP_TIMEOUT(25), SLP_VEC(2, 7, 9, 9, 0xFF)}, 0},
1759 {{SLP, SLP_TIMEOUT(25), SLP_TIMEOUT(25),
1760 SLP_VEC(4, 7, 10, 10, 0xFF)}, 0}
1761};
1762
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001763int iwl3945_power_init_handle(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001764{
1765 int rc = 0, i;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001766 struct iwl3945_power_mgr *pow_data;
Samuel Ortiz1125eff2008-12-19 10:37:14 +08001767 int size = sizeof(struct iwl_power_vec_entry) * IWL39_POWER_AC;
Zhu Yib481de92007-09-25 17:54:57 -07001768 u16 pci_pm;
1769
1770 IWL_DEBUG_POWER("Initialize power \n");
1771
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001772 pow_data = &(priv->power_data_39);
Zhu Yib481de92007-09-25 17:54:57 -07001773
1774 memset(pow_data, 0, sizeof(*pow_data));
1775
1776 pow_data->active_index = IWL_POWER_RANGE_0;
1777 pow_data->dtim_val = 0xffff;
1778
1779 memcpy(&pow_data->pwr_range_0[0], &range_0[0], size);
1780 memcpy(&pow_data->pwr_range_1[0], &range_1[0], size);
1781
1782 rc = pci_read_config_word(priv->pci_dev, PCI_LINK_CTRL, &pci_pm);
1783 if (rc != 0)
1784 return 0;
1785 else {
Tomas Winkler600c0e12008-12-19 10:37:04 +08001786 struct iwl_powertable_cmd *cmd;
Zhu Yib481de92007-09-25 17:54:57 -07001787
1788 IWL_DEBUG_POWER("adjust power command flags\n");
1789
Samuel Ortiz1125eff2008-12-19 10:37:14 +08001790 for (i = 0; i < IWL39_POWER_AC; i++) {
Zhu Yib481de92007-09-25 17:54:57 -07001791 cmd = &pow_data->pwr_range_0[i].cmd;
1792
1793 if (pci_pm & 0x1)
1794 cmd->flags &= ~IWL_POWER_PCI_PM_MSK;
1795 else
1796 cmd->flags |= IWL_POWER_PCI_PM_MSK;
1797 }
1798 }
1799 return rc;
1800}
1801
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001802static int iwl3945_update_power_cmd(struct iwl_priv *priv,
Tomas Winkler600c0e12008-12-19 10:37:04 +08001803 struct iwl_powertable_cmd *cmd, u32 mode)
Zhu Yib481de92007-09-25 17:54:57 -07001804{
1805 int rc = 0, i;
1806 u8 skip;
1807 u32 max_sleep = 0;
Samuel Ortiz1125eff2008-12-19 10:37:14 +08001808 struct iwl_power_vec_entry *range;
Zhu Yib481de92007-09-25 17:54:57 -07001809 u8 period = 0;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001810 struct iwl3945_power_mgr *pow_data;
Zhu Yib481de92007-09-25 17:54:57 -07001811
1812 if (mode > IWL_POWER_INDEX_5) {
1813 IWL_DEBUG_POWER("Error invalid power mode \n");
1814 return -1;
1815 }
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001816 pow_data = &(priv->power_data_39);
Zhu Yib481de92007-09-25 17:54:57 -07001817
1818 if (pow_data->active_index == IWL_POWER_RANGE_0)
1819 range = &pow_data->pwr_range_0[0];
1820 else
1821 range = &pow_data->pwr_range_1[1];
1822
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001823 memcpy(cmd, &range[mode].cmd, sizeof(struct iwl3945_powertable_cmd));
Zhu Yib481de92007-09-25 17:54:57 -07001824
1825#ifdef IWL_MAC80211_DISABLE
1826 if (priv->assoc_network != NULL) {
1827 unsigned long flags;
1828
1829 period = priv->assoc_network->tim.tim_period;
1830 }
1831#endif /*IWL_MAC80211_DISABLE */
1832 skip = range[mode].no_dtim;
1833
1834 if (period == 0) {
1835 period = 1;
1836 skip = 0;
1837 }
1838
1839 if (skip == 0) {
1840 max_sleep = period;
1841 cmd->flags &= ~IWL_POWER_SLEEP_OVER_DTIM_MSK;
1842 } else {
1843 __le32 slp_itrvl = cmd->sleep_interval[IWL_POWER_VEC_SIZE - 1];
1844 max_sleep = (le32_to_cpu(slp_itrvl) / period) * period;
1845 cmd->flags |= IWL_POWER_SLEEP_OVER_DTIM_MSK;
1846 }
1847
1848 for (i = 0; i < IWL_POWER_VEC_SIZE; i++) {
1849 if (le32_to_cpu(cmd->sleep_interval[i]) > max_sleep)
1850 cmd->sleep_interval[i] = cpu_to_le32(max_sleep);
1851 }
1852
1853 IWL_DEBUG_POWER("Flags value = 0x%08X\n", cmd->flags);
1854 IWL_DEBUG_POWER("Tx timeout = %u\n", le32_to_cpu(cmd->tx_data_timeout));
1855 IWL_DEBUG_POWER("Rx timeout = %u\n", le32_to_cpu(cmd->rx_data_timeout));
1856 IWL_DEBUG_POWER("Sleep interval vector = { %d , %d , %d , %d , %d }\n",
1857 le32_to_cpu(cmd->sleep_interval[0]),
1858 le32_to_cpu(cmd->sleep_interval[1]),
1859 le32_to_cpu(cmd->sleep_interval[2]),
1860 le32_to_cpu(cmd->sleep_interval[3]),
1861 le32_to_cpu(cmd->sleep_interval[4]));
1862
1863 return rc;
1864}
1865
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001866static int iwl3945_send_power_mode(struct iwl_priv *priv, u32 mode)
Zhu Yib481de92007-09-25 17:54:57 -07001867{
John W. Linville9a62f732007-11-15 16:27:36 -05001868 u32 uninitialized_var(final_mode);
Zhu Yib481de92007-09-25 17:54:57 -07001869 int rc;
Tomas Winkler600c0e12008-12-19 10:37:04 +08001870 struct iwl_powertable_cmd cmd;
Zhu Yib481de92007-09-25 17:54:57 -07001871
1872 /* If on battery, set to 3,
Ian Schram01ebd062007-10-25 17:15:22 +08001873 * if plugged into AC power, set to CAM ("continuously aware mode"),
Zhu Yib481de92007-09-25 17:54:57 -07001874 * else user level */
1875 switch (mode) {
Samuel Ortiz1125eff2008-12-19 10:37:14 +08001876 case IWL39_POWER_BATTERY:
Zhu Yib481de92007-09-25 17:54:57 -07001877 final_mode = IWL_POWER_INDEX_3;
1878 break;
Samuel Ortiz1125eff2008-12-19 10:37:14 +08001879 case IWL39_POWER_AC:
Zhu Yib481de92007-09-25 17:54:57 -07001880 final_mode = IWL_POWER_MODE_CAM;
1881 break;
1882 default:
1883 final_mode = mode;
1884 break;
1885 }
1886
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001887 iwl3945_update_power_cmd(priv, &cmd, final_mode);
Zhu Yib481de92007-09-25 17:54:57 -07001888
Tomas Winkler600c0e12008-12-19 10:37:04 +08001889 /* FIXME use get_hcmd_size 3945 command is 4 bytes shorter */
1890 rc = iwl3945_send_cmd_pdu(priv, POWER_TABLE_CMD,
1891 sizeof(struct iwl3945_powertable_cmd), &cmd);
Zhu Yib481de92007-09-25 17:54:57 -07001892
1893 if (final_mode == IWL_POWER_MODE_CAM)
1894 clear_bit(STATUS_POWER_PMI, &priv->status);
1895 else
1896 set_bit(STATUS_POWER_PMI, &priv->status);
1897
1898 return rc;
1899}
1900
Zhu Yib481de92007-09-25 17:54:57 -07001901/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001902 * iwl3945_scan_cancel - Cancel any currently executing HW scan
Zhu Yib481de92007-09-25 17:54:57 -07001903 *
1904 * NOTE: priv->mutex is not required before calling this function
1905 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001906static int iwl3945_scan_cancel(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001907{
1908 if (!test_bit(STATUS_SCAN_HW, &priv->status)) {
1909 clear_bit(STATUS_SCANNING, &priv->status);
1910 return 0;
1911 }
1912
1913 if (test_bit(STATUS_SCANNING, &priv->status)) {
1914 if (!test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
1915 IWL_DEBUG_SCAN("Queuing scan abort.\n");
1916 set_bit(STATUS_SCAN_ABORTING, &priv->status);
1917 queue_work(priv->workqueue, &priv->abort_scan);
1918
1919 } else
1920 IWL_DEBUG_SCAN("Scan abort already in progress.\n");
1921
1922 return test_bit(STATUS_SCANNING, &priv->status);
1923 }
1924
1925 return 0;
1926}
1927
1928/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001929 * iwl3945_scan_cancel_timeout - Cancel any currently executing HW scan
Zhu Yib481de92007-09-25 17:54:57 -07001930 * @ms: amount of time to wait (in milliseconds) for scan to abort
1931 *
1932 * NOTE: priv->mutex must be held before calling this function
1933 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001934static int iwl3945_scan_cancel_timeout(struct iwl_priv *priv, unsigned long ms)
Zhu Yib481de92007-09-25 17:54:57 -07001935{
1936 unsigned long now = jiffies;
1937 int ret;
1938
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001939 ret = iwl3945_scan_cancel(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001940 if (ret && ms) {
1941 mutex_unlock(&priv->mutex);
1942 while (!time_after(jiffies, now + msecs_to_jiffies(ms)) &&
1943 test_bit(STATUS_SCANNING, &priv->status))
1944 msleep(1);
1945 mutex_lock(&priv->mutex);
1946
1947 return test_bit(STATUS_SCANNING, &priv->status);
1948 }
1949
1950 return ret;
1951}
1952
Zhu Yib481de92007-09-25 17:54:57 -07001953#define MAX_UCODE_BEACON_INTERVAL 1024
1954#define INTEL_CONN_LISTEN_INTERVAL __constant_cpu_to_le16(0xA)
1955
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001956static __le16 iwl3945_adjust_beacon_interval(u16 beacon_val)
Zhu Yib481de92007-09-25 17:54:57 -07001957{
1958 u16 new_val = 0;
1959 u16 beacon_factor = 0;
1960
1961 beacon_factor =
1962 (beacon_val + MAX_UCODE_BEACON_INTERVAL)
1963 / MAX_UCODE_BEACON_INTERVAL;
1964 new_val = beacon_val / beacon_factor;
1965
1966 return cpu_to_le16(new_val);
1967}
1968
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001969static void iwl3945_setup_rxon_timing(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001970{
1971 u64 interval_tm_unit;
1972 u64 tsf, result;
1973 unsigned long flags;
1974 struct ieee80211_conf *conf = NULL;
1975 u16 beacon_int = 0;
1976
1977 conf = ieee80211_get_hw_conf(priv->hw);
1978
1979 spin_lock_irqsave(&priv->lock, flags);
Tomas Winkler28afaf92008-12-19 10:37:06 +08001980 priv->rxon_timing.timestamp = cpu_to_le64(priv->timestamp);
Zhu Yib481de92007-09-25 17:54:57 -07001981 priv->rxon_timing.listen_interval = INTEL_CONN_LISTEN_INTERVAL;
1982
Tomas Winkler28afaf92008-12-19 10:37:06 +08001983 tsf = priv->timestamp;
Zhu Yib481de92007-09-25 17:54:57 -07001984
1985 beacon_int = priv->beacon_int;
1986 spin_unlock_irqrestore(&priv->lock, flags);
1987
Johannes Berg05c914f2008-09-11 00:01:58 +02001988 if (priv->iw_mode == NL80211_IFTYPE_STATION) {
Zhu Yib481de92007-09-25 17:54:57 -07001989 if (beacon_int == 0) {
1990 priv->rxon_timing.beacon_interval = cpu_to_le16(100);
1991 priv->rxon_timing.beacon_init_val = cpu_to_le32(102400);
1992 } else {
1993 priv->rxon_timing.beacon_interval =
1994 cpu_to_le16(beacon_int);
1995 priv->rxon_timing.beacon_interval =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001996 iwl3945_adjust_beacon_interval(
Zhu Yib481de92007-09-25 17:54:57 -07001997 le16_to_cpu(priv->rxon_timing.beacon_interval));
1998 }
1999
2000 priv->rxon_timing.atim_window = 0;
2001 } else {
2002 priv->rxon_timing.beacon_interval =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002003 iwl3945_adjust_beacon_interval(conf->beacon_int);
Zhu Yib481de92007-09-25 17:54:57 -07002004 /* TODO: we need to get atim_window from upper stack
2005 * for now we set to 0 */
2006 priv->rxon_timing.atim_window = 0;
2007 }
2008
2009 interval_tm_unit =
2010 (le16_to_cpu(priv->rxon_timing.beacon_interval) * 1024);
2011 result = do_div(tsf, interval_tm_unit);
2012 priv->rxon_timing.beacon_init_val =
2013 cpu_to_le32((u32) ((u64) interval_tm_unit - result));
2014
2015 IWL_DEBUG_ASSOC
2016 ("beacon interval %d beacon timer %d beacon tim %d\n",
2017 le16_to_cpu(priv->rxon_timing.beacon_interval),
2018 le32_to_cpu(priv->rxon_timing.beacon_init_val),
2019 le16_to_cpu(priv->rxon_timing.atim_window));
2020}
2021
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002022static int iwl3945_scan_initiate(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002023{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002024 if (!iwl3945_is_ready_rf(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07002025 IWL_DEBUG_SCAN("Aborting scan due to not ready.\n");
2026 return -EIO;
2027 }
2028
2029 if (test_bit(STATUS_SCANNING, &priv->status)) {
2030 IWL_DEBUG_SCAN("Scan already in progress.\n");
2031 return -EAGAIN;
2032 }
2033
2034 if (test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
2035 IWL_DEBUG_SCAN("Scan request while abort pending. "
2036 "Queuing.\n");
2037 return -EAGAIN;
2038 }
2039
2040 IWL_DEBUG_INFO("Starting scan...\n");
Ron Rindjunsky66b50042008-06-25 16:46:31 +08002041 if (priv->cfg->sku & IWL_SKU_G)
2042 priv->scan_bands |= BIT(IEEE80211_BAND_2GHZ);
2043 if (priv->cfg->sku & IWL_SKU_A)
2044 priv->scan_bands |= BIT(IEEE80211_BAND_5GHZ);
Zhu Yib481de92007-09-25 17:54:57 -07002045 set_bit(STATUS_SCANNING, &priv->status);
2046 priv->scan_start = jiffies;
2047 priv->scan_pass_start = priv->scan_start;
2048
2049 queue_work(priv->workqueue, &priv->request_scan);
2050
2051 return 0;
2052}
2053
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002054static int iwl3945_set_rxon_hwcrypto(struct iwl_priv *priv, int hw_decrypt)
Zhu Yib481de92007-09-25 17:54:57 -07002055{
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08002056 struct iwl3945_rxon_cmd *rxon = &priv->staging39_rxon;
Zhu Yib481de92007-09-25 17:54:57 -07002057
2058 if (hw_decrypt)
2059 rxon->filter_flags &= ~RXON_FILTER_DIS_DECRYPT_MSK;
2060 else
2061 rxon->filter_flags |= RXON_FILTER_DIS_DECRYPT_MSK;
2062
2063 return 0;
2064}
2065
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002066static void iwl3945_set_flags_for_phymode(struct iwl_priv *priv,
Johannes Berg8318d782008-01-24 19:38:38 +01002067 enum ieee80211_band band)
Zhu Yib481de92007-09-25 17:54:57 -07002068{
Johannes Berg8318d782008-01-24 19:38:38 +01002069 if (band == IEEE80211_BAND_5GHZ) {
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08002070 priv->staging39_rxon.flags &=
Zhu Yib481de92007-09-25 17:54:57 -07002071 ~(RXON_FLG_BAND_24G_MSK | RXON_FLG_AUTO_DETECT_MSK
2072 | RXON_FLG_CCK_MSK);
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08002073 priv->staging39_rxon.flags |= RXON_FLG_SHORT_SLOT_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07002074 } else {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002075 /* Copied from iwl3945_bg_post_associate() */
Zhu Yib481de92007-09-25 17:54:57 -07002076 if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_SLOT_TIME)
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08002077 priv->staging39_rxon.flags |= RXON_FLG_SHORT_SLOT_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07002078 else
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08002079 priv->staging39_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07002080
Johannes Berg05c914f2008-09-11 00:01:58 +02002081 if (priv->iw_mode == NL80211_IFTYPE_ADHOC)
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08002082 priv->staging39_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07002083
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08002084 priv->staging39_rxon.flags |= RXON_FLG_BAND_24G_MSK;
2085 priv->staging39_rxon.flags |= RXON_FLG_AUTO_DETECT_MSK;
2086 priv->staging39_rxon.flags &= ~RXON_FLG_CCK_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07002087 }
2088}
2089
2090/*
Ian Schram01ebd062007-10-25 17:15:22 +08002091 * initialize rxon structure with default values from eeprom
Zhu Yib481de92007-09-25 17:54:57 -07002092 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002093static void iwl3945_connection_init_rx_config(struct iwl_priv *priv,
Zhu, Yi60294de2008-10-29 14:05:45 -07002094 int mode)
Zhu Yib481de92007-09-25 17:54:57 -07002095{
Samuel Ortizd20b3c62008-12-19 10:37:15 +08002096 const struct iwl_channel_info *ch_info;
Zhu Yib481de92007-09-25 17:54:57 -07002097
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08002098 memset(&priv->staging39_rxon, 0, sizeof(priv->staging39_rxon));
Zhu Yib481de92007-09-25 17:54:57 -07002099
Zhu, Yi60294de2008-10-29 14:05:45 -07002100 switch (mode) {
Johannes Berg05c914f2008-09-11 00:01:58 +02002101 case NL80211_IFTYPE_AP:
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08002102 priv->staging39_rxon.dev_type = RXON_DEV_TYPE_AP;
Zhu Yib481de92007-09-25 17:54:57 -07002103 break;
2104
Johannes Berg05c914f2008-09-11 00:01:58 +02002105 case NL80211_IFTYPE_STATION:
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08002106 priv->staging39_rxon.dev_type = RXON_DEV_TYPE_ESS;
2107 priv->staging39_rxon.filter_flags = RXON_FILTER_ACCEPT_GRP_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07002108 break;
2109
Johannes Berg05c914f2008-09-11 00:01:58 +02002110 case NL80211_IFTYPE_ADHOC:
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08002111 priv->staging39_rxon.dev_type = RXON_DEV_TYPE_IBSS;
2112 priv->staging39_rxon.flags = RXON_FLG_SHORT_PREAMBLE_MSK;
2113 priv->staging39_rxon.filter_flags = RXON_FILTER_BCON_AWARE_MSK |
Zhu Yib481de92007-09-25 17:54:57 -07002114 RXON_FILTER_ACCEPT_GRP_MSK;
2115 break;
2116
Johannes Berg05c914f2008-09-11 00:01:58 +02002117 case NL80211_IFTYPE_MONITOR:
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08002118 priv->staging39_rxon.dev_type = RXON_DEV_TYPE_SNIFFER;
2119 priv->staging39_rxon.filter_flags = RXON_FILTER_PROMISC_MSK |
Zhu Yib481de92007-09-25 17:54:57 -07002120 RXON_FILTER_CTL2HOST_MSK | RXON_FILTER_ACCEPT_GRP_MSK;
2121 break;
Tomas Winkler69dc5d92008-03-25 16:33:41 -07002122 default:
Zhu, Yi60294de2008-10-29 14:05:45 -07002123 IWL_ERROR("Unsupported interface type %d\n", mode);
Tomas Winkler69dc5d92008-03-25 16:33:41 -07002124 break;
Zhu Yib481de92007-09-25 17:54:57 -07002125 }
2126
2127#if 0
2128 /* TODO: Figure out when short_preamble would be set and cache from
2129 * that */
2130 if (!hw_to_local(priv->hw)->short_preamble)
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08002131 priv->staging39_rxon.flags &= ~RXON_FLG_SHORT_PREAMBLE_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07002132 else
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08002133 priv->staging39_rxon.flags |= RXON_FLG_SHORT_PREAMBLE_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07002134#endif
2135
Johannes Berg8318d782008-01-24 19:38:38 +01002136 ch_info = iwl3945_get_channel_info(priv, priv->band,
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08002137 le16_to_cpu(priv->active39_rxon.channel));
Zhu Yib481de92007-09-25 17:54:57 -07002138
2139 if (!ch_info)
2140 ch_info = &priv->channel_info[0];
2141
2142 /*
2143 * in some case A channels are all non IBSS
2144 * in this case force B/G channel
2145 */
Zhu, Yi60294de2008-10-29 14:05:45 -07002146 if ((mode == NL80211_IFTYPE_ADHOC) && !(is_channel_ibss(ch_info)))
Zhu Yib481de92007-09-25 17:54:57 -07002147 ch_info = &priv->channel_info[0];
2148
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08002149 priv->staging39_rxon.channel = cpu_to_le16(ch_info->channel);
Zhu Yib481de92007-09-25 17:54:57 -07002150 if (is_channel_a_band(ch_info))
Johannes Berg8318d782008-01-24 19:38:38 +01002151 priv->band = IEEE80211_BAND_5GHZ;
Zhu Yib481de92007-09-25 17:54:57 -07002152 else
Johannes Berg8318d782008-01-24 19:38:38 +01002153 priv->band = IEEE80211_BAND_2GHZ;
Zhu Yib481de92007-09-25 17:54:57 -07002154
Johannes Berg8318d782008-01-24 19:38:38 +01002155 iwl3945_set_flags_for_phymode(priv, priv->band);
Zhu Yib481de92007-09-25 17:54:57 -07002156
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08002157 priv->staging39_rxon.ofdm_basic_rates =
Zhu Yib481de92007-09-25 17:54:57 -07002158 (IWL_OFDM_RATES_MASK >> IWL_FIRST_OFDM_RATE) & 0xFF;
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08002159 priv->staging39_rxon.cck_basic_rates =
Zhu Yib481de92007-09-25 17:54:57 -07002160 (IWL_CCK_RATES_MASK >> IWL_FIRST_CCK_RATE) & 0xF;
2161}
2162
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002163static int iwl3945_set_mode(struct iwl_priv *priv, int mode)
Zhu Yib481de92007-09-25 17:54:57 -07002164{
Johannes Berg05c914f2008-09-11 00:01:58 +02002165 if (mode == NL80211_IFTYPE_ADHOC) {
Samuel Ortizd20b3c62008-12-19 10:37:15 +08002166 const struct iwl_channel_info *ch_info;
Zhu Yib481de92007-09-25 17:54:57 -07002167
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002168 ch_info = iwl3945_get_channel_info(priv,
Johannes Berg8318d782008-01-24 19:38:38 +01002169 priv->band,
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08002170 le16_to_cpu(priv->staging39_rxon.channel));
Zhu Yib481de92007-09-25 17:54:57 -07002171
2172 if (!ch_info || !is_channel_ibss(ch_info)) {
2173 IWL_ERROR("channel %d not IBSS channel\n",
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08002174 le16_to_cpu(priv->staging39_rxon.channel));
Zhu Yib481de92007-09-25 17:54:57 -07002175 return -EINVAL;
2176 }
2177 }
2178
Zhu, Yi60294de2008-10-29 14:05:45 -07002179 iwl3945_connection_init_rx_config(priv, mode);
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08002180 memcpy(priv->staging39_rxon.node_addr, priv->mac_addr, ETH_ALEN);
Zhu Yib481de92007-09-25 17:54:57 -07002181
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002182 iwl3945_clear_stations_table(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002183
Tomas Winklera96a27f2008-10-23 23:48:56 -07002184 /* don't commit rxon if rf-kill is on*/
Mohamed Abbasfde35712007-11-29 11:10:15 +08002185 if (!iwl3945_is_ready_rf(priv))
2186 return -EAGAIN;
2187
2188 cancel_delayed_work(&priv->scan_check);
2189 if (iwl3945_scan_cancel_timeout(priv, 100)) {
Winkler, Tomas39aadf82008-12-19 10:37:32 +08002190 IWL_WARN(priv, "Aborted scan still in progress after 100ms\n");
Mohamed Abbasfde35712007-11-29 11:10:15 +08002191 IWL_DEBUG_MAC80211("leaving - scan abort failed.\n");
2192 return -EAGAIN;
2193 }
2194
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002195 iwl3945_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002196
2197 return 0;
2198}
2199
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002200static void iwl3945_build_tx_cmd_hwcrypto(struct iwl_priv *priv,
Johannes Berge039fa42008-05-15 12:55:29 +02002201 struct ieee80211_tx_info *info,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002202 struct iwl3945_cmd *cmd,
Zhu Yib481de92007-09-25 17:54:57 -07002203 struct sk_buff *skb_frag,
2204 int last_frag)
2205{
Ivo van Doorn1c014422008-04-17 19:41:02 +02002206 struct iwl3945_hw_key *keyinfo =
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08002207 &priv->stations_39[info->control.hw_key->hw_key_idx].keyinfo;
Zhu Yib481de92007-09-25 17:54:57 -07002208
2209 switch (keyinfo->alg) {
2210 case ALG_CCMP:
2211 cmd->cmd.tx.sec_ctl = TX_CMD_SEC_CCM;
2212 memcpy(cmd->cmd.tx.key, keyinfo->key, keyinfo->keylen);
Tomas Winklera96a27f2008-10-23 23:48:56 -07002213 IWL_DEBUG_TX("tx_cmd with AES hwcrypto\n");
Zhu Yib481de92007-09-25 17:54:57 -07002214 break;
2215
2216 case ALG_TKIP:
2217#if 0
2218 cmd->cmd.tx.sec_ctl = TX_CMD_SEC_TKIP;
2219
2220 if (last_frag)
2221 memcpy(cmd->cmd.tx.tkip_mic.byte, skb_frag->tail - 8,
2222 8);
2223 else
2224 memset(cmd->cmd.tx.tkip_mic.byte, 0, 8);
2225#endif
2226 break;
2227
2228 case ALG_WEP:
2229 cmd->cmd.tx.sec_ctl = TX_CMD_SEC_WEP |
Johannes Berge039fa42008-05-15 12:55:29 +02002230 (info->control.hw_key->hw_key_idx & TX_CMD_SEC_MSK) << TX_CMD_SEC_SHIFT;
Zhu Yib481de92007-09-25 17:54:57 -07002231
2232 if (keyinfo->keylen == 13)
2233 cmd->cmd.tx.sec_ctl |= TX_CMD_SEC_KEY128;
2234
2235 memcpy(&cmd->cmd.tx.key[3], keyinfo->key, keyinfo->keylen);
2236
2237 IWL_DEBUG_TX("Configuring packet for WEP encryption "
Johannes Berge039fa42008-05-15 12:55:29 +02002238 "with key %d\n", info->control.hw_key->hw_key_idx);
Zhu Yib481de92007-09-25 17:54:57 -07002239 break;
2240
Zhu Yib481de92007-09-25 17:54:57 -07002241 default:
Tomas Winkler978785a2008-12-19 10:37:31 +08002242 IWL_ERR(priv, "Unknown encode alg %d\n", keyinfo->alg);
Zhu Yib481de92007-09-25 17:54:57 -07002243 break;
2244 }
2245}
2246
2247/*
2248 * handle build REPLY_TX command notification.
2249 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002250static void iwl3945_build_tx_cmd_basic(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002251 struct iwl3945_cmd *cmd,
Johannes Berge039fa42008-05-15 12:55:29 +02002252 struct ieee80211_tx_info *info,
Zhu Yib481de92007-09-25 17:54:57 -07002253 struct ieee80211_hdr *hdr,
2254 int is_unicast, u8 std_id)
2255{
Harvey Harrisonfd7c8a42008-06-11 14:21:56 -07002256 __le16 fc = hdr->frame_control;
Zhu Yib481de92007-09-25 17:54:57 -07002257 __le32 tx_flags = cmd->cmd.tx.tx_flags;
Johannes Berge6a98542008-10-21 12:40:02 +02002258 u8 rc_flags = info->control.rates[0].flags;
Zhu Yib481de92007-09-25 17:54:57 -07002259
2260 cmd->cmd.tx.stop_time.life_time = TX_CMD_LIFE_TIME_INFINITE;
Johannes Berge039fa42008-05-15 12:55:29 +02002261 if (!(info->flags & IEEE80211_TX_CTL_NO_ACK)) {
Zhu Yib481de92007-09-25 17:54:57 -07002262 tx_flags |= TX_CMD_FLG_ACK_MSK;
Harvey Harrisonfd7c8a42008-06-11 14:21:56 -07002263 if (ieee80211_is_mgmt(fc))
Zhu Yib481de92007-09-25 17:54:57 -07002264 tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
Harvey Harrisonfd7c8a42008-06-11 14:21:56 -07002265 if (ieee80211_is_probe_resp(fc) &&
Zhu Yib481de92007-09-25 17:54:57 -07002266 !(le16_to_cpu(hdr->seq_ctrl) & 0xf))
2267 tx_flags |= TX_CMD_FLG_TSF_MSK;
2268 } else {
2269 tx_flags &= (~TX_CMD_FLG_ACK_MSK);
2270 tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
2271 }
2272
2273 cmd->cmd.tx.sta_id = std_id;
Harvey Harrison8b7b1e02008-06-11 14:21:56 -07002274 if (ieee80211_has_morefrags(fc))
Zhu Yib481de92007-09-25 17:54:57 -07002275 tx_flags |= TX_CMD_FLG_MORE_FRAG_MSK;
2276
Harvey Harrisonfd7c8a42008-06-11 14:21:56 -07002277 if (ieee80211_is_data_qos(fc)) {
2278 u8 *qc = ieee80211_get_qos_ctl(hdr);
Tomas Winkler54dbb522008-05-15 13:54:06 +08002279 cmd->cmd.tx.tid_tspec = qc[0] & 0xf;
Zhu Yib481de92007-09-25 17:54:57 -07002280 tx_flags &= ~TX_CMD_FLG_SEQ_CTL_MSK;
Tomas Winkler54dbb522008-05-15 13:54:06 +08002281 } else {
Zhu Yib481de92007-09-25 17:54:57 -07002282 tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
Tomas Winkler54dbb522008-05-15 13:54:06 +08002283 }
Zhu Yib481de92007-09-25 17:54:57 -07002284
Johannes Berge6a98542008-10-21 12:40:02 +02002285 if (rc_flags & IEEE80211_TX_RC_USE_RTS_CTS) {
Zhu Yib481de92007-09-25 17:54:57 -07002286 tx_flags |= TX_CMD_FLG_RTS_MSK;
2287 tx_flags &= ~TX_CMD_FLG_CTS_MSK;
Johannes Berge6a98542008-10-21 12:40:02 +02002288 } else if (rc_flags & IEEE80211_TX_RC_USE_CTS_PROTECT) {
Zhu Yib481de92007-09-25 17:54:57 -07002289 tx_flags &= ~TX_CMD_FLG_RTS_MSK;
2290 tx_flags |= TX_CMD_FLG_CTS_MSK;
2291 }
2292
2293 if ((tx_flags & TX_CMD_FLG_RTS_MSK) || (tx_flags & TX_CMD_FLG_CTS_MSK))
2294 tx_flags |= TX_CMD_FLG_FULL_TXOP_PROT_MSK;
2295
2296 tx_flags &= ~(TX_CMD_FLG_ANT_SEL_MSK);
Harvey Harrisonfd7c8a42008-06-11 14:21:56 -07002297 if (ieee80211_is_mgmt(fc)) {
2298 if (ieee80211_is_assoc_req(fc) || ieee80211_is_reassoc_req(fc))
Ian Schrambc434dd2007-10-25 17:15:29 +08002299 cmd->cmd.tx.timeout.pm_frame_timeout = cpu_to_le16(3);
Zhu Yib481de92007-09-25 17:54:57 -07002300 else
Ian Schrambc434dd2007-10-25 17:15:29 +08002301 cmd->cmd.tx.timeout.pm_frame_timeout = cpu_to_le16(2);
Mohamed Abbasab53d8a2008-03-25 16:33:36 -07002302 } else {
Zhu Yib481de92007-09-25 17:54:57 -07002303 cmd->cmd.tx.timeout.pm_frame_timeout = 0;
Mohamed Abbasab53d8a2008-03-25 16:33:36 -07002304#ifdef CONFIG_IWL3945_LEDS
2305 priv->rxtxpackets += le16_to_cpu(cmd->cmd.tx.len);
2306#endif
2307 }
Zhu Yib481de92007-09-25 17:54:57 -07002308
2309 cmd->cmd.tx.driver_txop = 0;
2310 cmd->cmd.tx.tx_flags = tx_flags;
2311 cmd->cmd.tx.next_frame_len = 0;
2312}
2313
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002314/**
2315 * iwl3945_get_sta_id - Find station's index within station table
2316 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002317static int iwl3945_get_sta_id(struct iwl_priv *priv, struct ieee80211_hdr *hdr)
Zhu Yib481de92007-09-25 17:54:57 -07002318{
2319 int sta_id;
2320 u16 fc = le16_to_cpu(hdr->frame_control);
2321
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002322 /* If this frame is broadcast or management, use broadcast station id */
Zhu Yib481de92007-09-25 17:54:57 -07002323 if (((fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_DATA) ||
2324 is_multicast_ether_addr(hdr->addr1))
Abhijeet Kolekar3832ec92008-12-19 10:37:26 +08002325 return priv->hw_params.bcast_sta_id;
Zhu Yib481de92007-09-25 17:54:57 -07002326
2327 switch (priv->iw_mode) {
2328
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002329 /* If we are a client station in a BSS network, use the special
2330 * AP station entry (that's the only station we communicate with) */
Johannes Berg05c914f2008-09-11 00:01:58 +02002331 case NL80211_IFTYPE_STATION:
Zhu Yib481de92007-09-25 17:54:57 -07002332 return IWL_AP_ID;
2333
2334 /* If we are an AP, then find the station, or use BCAST */
Johannes Berg05c914f2008-09-11 00:01:58 +02002335 case NL80211_IFTYPE_AP:
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002336 sta_id = iwl3945_hw_find_station(priv, hdr->addr1);
Zhu Yib481de92007-09-25 17:54:57 -07002337 if (sta_id != IWL_INVALID_STATION)
2338 return sta_id;
Abhijeet Kolekar3832ec92008-12-19 10:37:26 +08002339 return priv->hw_params.bcast_sta_id;
Zhu Yib481de92007-09-25 17:54:57 -07002340
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002341 /* If this frame is going out to an IBSS network, find the station,
2342 * or create a new station table entry */
Johannes Berg05c914f2008-09-11 00:01:58 +02002343 case NL80211_IFTYPE_ADHOC: {
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002344 /* Create new station table entry */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002345 sta_id = iwl3945_hw_find_station(priv, hdr->addr1);
Zhu Yib481de92007-09-25 17:54:57 -07002346 if (sta_id != IWL_INVALID_STATION)
2347 return sta_id;
2348
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002349 sta_id = iwl3945_add_station(priv, hdr->addr1, 0, CMD_ASYNC);
Zhu Yib481de92007-09-25 17:54:57 -07002350
2351 if (sta_id != IWL_INVALID_STATION)
2352 return sta_id;
2353
Johannes Berge1749612008-10-27 15:59:26 -07002354 IWL_DEBUG_DROP("Station %pM not in station map. "
Zhu Yib481de92007-09-25 17:54:57 -07002355 "Defaulting to broadcast...\n",
Johannes Berge1749612008-10-27 15:59:26 -07002356 hdr->addr1);
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08002357 iwl_print_hex_dump(priv, IWL_DL_DROP, (u8 *) hdr, sizeof(*hdr));
Abhijeet Kolekar3832ec92008-12-19 10:37:26 +08002358 return priv->hw_params.bcast_sta_id;
Joe Perches0795af52007-10-03 17:59:30 -07002359 }
Stefanik Gábor914233d2008-06-30 17:23:30 +08002360 /* If we are in monitor mode, use BCAST. This is required for
2361 * packet injection. */
Johannes Berg05c914f2008-09-11 00:01:58 +02002362 case NL80211_IFTYPE_MONITOR:
Abhijeet Kolekar3832ec92008-12-19 10:37:26 +08002363 return priv->hw_params.bcast_sta_id;
Stefanik Gábor914233d2008-06-30 17:23:30 +08002364
Zhu Yib481de92007-09-25 17:54:57 -07002365 default:
Winkler, Tomas39aadf82008-12-19 10:37:32 +08002366 IWL_WARN(priv, "Unknown mode of operation: %d\n",
2367 priv->iw_mode);
Abhijeet Kolekar3832ec92008-12-19 10:37:26 +08002368 return priv->hw_params.bcast_sta_id;
Zhu Yib481de92007-09-25 17:54:57 -07002369 }
2370}
2371
2372/*
2373 * start REPLY_TX command process
2374 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002375static int iwl3945_tx_skb(struct iwl_priv *priv, struct sk_buff *skb)
Zhu Yib481de92007-09-25 17:54:57 -07002376{
2377 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
Johannes Berge039fa42008-05-15 12:55:29 +02002378 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002379 struct iwl3945_tfd_frame *tfd;
Zhu Yib481de92007-09-25 17:54:57 -07002380 u32 *control_flags;
Johannes Berge2530082008-05-17 00:57:14 +02002381 int txq_id = skb_get_queue_mapping(skb);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002382 struct iwl3945_tx_queue *txq = NULL;
Samuel Ortizd20b3c62008-12-19 10:37:15 +08002383 struct iwl_queue *q = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07002384 dma_addr_t phys_addr;
2385 dma_addr_t txcmd_phys;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002386 struct iwl3945_cmd *out_cmd = NULL;
Tomas Winkler54dbb522008-05-15 13:54:06 +08002387 u16 len, idx, len_org, hdr_len;
2388 u8 id;
2389 u8 unicast;
Zhu Yib481de92007-09-25 17:54:57 -07002390 u8 sta_id;
Tomas Winkler54dbb522008-05-15 13:54:06 +08002391 u8 tid = 0;
Zhu Yib481de92007-09-25 17:54:57 -07002392 u16 seq_number = 0;
Harvey Harrisonfd7c8a42008-06-11 14:21:56 -07002393 __le16 fc;
Zhu Yib481de92007-09-25 17:54:57 -07002394 u8 wait_write_ptr = 0;
Tomas Winkler54dbb522008-05-15 13:54:06 +08002395 u8 *qc = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07002396 unsigned long flags;
2397 int rc;
2398
2399 spin_lock_irqsave(&priv->lock, flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002400 if (iwl3945_is_rfkill(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07002401 IWL_DEBUG_DROP("Dropping - RF KILL\n");
2402 goto drop_unlock;
2403 }
2404
Johannes Berge039fa42008-05-15 12:55:29 +02002405 if ((ieee80211_get_tx_rate(priv->hw, info)->hw_value & 0xFF) == IWL_INVALID_RATE) {
Zhu Yib481de92007-09-25 17:54:57 -07002406 IWL_ERROR("ERROR: No TX rate available.\n");
2407 goto drop_unlock;
2408 }
2409
2410 unicast = !is_multicast_ether_addr(hdr->addr1);
2411 id = 0;
2412
Harvey Harrisonfd7c8a42008-06-11 14:21:56 -07002413 fc = hdr->frame_control;
Zhu Yib481de92007-09-25 17:54:57 -07002414
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08002415#ifdef CONFIG_IWL3945_DEBUG
Zhu Yib481de92007-09-25 17:54:57 -07002416 if (ieee80211_is_auth(fc))
2417 IWL_DEBUG_TX("Sending AUTH frame\n");
Harvey Harrisonfd7c8a42008-06-11 14:21:56 -07002418 else if (ieee80211_is_assoc_req(fc))
Zhu Yib481de92007-09-25 17:54:57 -07002419 IWL_DEBUG_TX("Sending ASSOC frame\n");
Harvey Harrisonfd7c8a42008-06-11 14:21:56 -07002420 else if (ieee80211_is_reassoc_req(fc))
Zhu Yib481de92007-09-25 17:54:57 -07002421 IWL_DEBUG_TX("Sending REASSOC frame\n");
2422#endif
2423
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08002424 /* drop all data frame if we are not associated */
Stefanik Gábor914233d2008-06-30 17:23:30 +08002425 if (ieee80211_is_data(fc) &&
Johannes Berg05c914f2008-09-11 00:01:58 +02002426 (priv->iw_mode != NL80211_IFTYPE_MONITOR) && /* packet injection */
Stefanik Gábor914233d2008-06-30 17:23:30 +08002427 (!iwl3945_is_associated(priv) ||
Johannes Berg05c914f2008-09-11 00:01:58 +02002428 ((priv->iw_mode == NL80211_IFTYPE_STATION) && !priv->assoc_id))) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002429 IWL_DEBUG_DROP("Dropping - !iwl3945_is_associated\n");
Zhu Yib481de92007-09-25 17:54:57 -07002430 goto drop_unlock;
2431 }
2432
2433 spin_unlock_irqrestore(&priv->lock, flags);
2434
Harvey Harrison7294ec92008-07-15 18:43:59 -07002435 hdr_len = ieee80211_hdrlen(fc);
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002436
2437 /* Find (or create) index into station table for destination station */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002438 sta_id = iwl3945_get_sta_id(priv, hdr);
Zhu Yib481de92007-09-25 17:54:57 -07002439 if (sta_id == IWL_INVALID_STATION) {
Johannes Berge1749612008-10-27 15:59:26 -07002440 IWL_DEBUG_DROP("Dropping - INVALID STATION: %pM\n",
2441 hdr->addr1);
Zhu Yib481de92007-09-25 17:54:57 -07002442 goto drop;
2443 }
2444
2445 IWL_DEBUG_RATE("station Id %d\n", sta_id);
2446
Harvey Harrisonfd7c8a42008-06-11 14:21:56 -07002447 if (ieee80211_is_data_qos(fc)) {
2448 qc = ieee80211_get_qos_ctl(hdr);
Harvey Harrison7294ec92008-07-15 18:43:59 -07002449 tid = qc[0] & IEEE80211_QOS_CTL_TID_MASK;
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08002450 seq_number = priv->stations_39[sta_id].tid[tid].seq_number &
Zhu Yib481de92007-09-25 17:54:57 -07002451 IEEE80211_SCTL_SEQ;
2452 hdr->seq_ctrl = cpu_to_le16(seq_number) |
2453 (hdr->seq_ctrl &
2454 __constant_cpu_to_le16(IEEE80211_SCTL_FRAG));
2455 seq_number += 0x10;
2456 }
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002457
2458 /* Descriptor for chosen Tx queue */
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08002459 txq = &priv->txq39[txq_id];
Zhu Yib481de92007-09-25 17:54:57 -07002460 q = &txq->q;
2461
2462 spin_lock_irqsave(&priv->lock, flags);
2463
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002464 /* Set up first empty TFD within this queue's circular TFD buffer */
Tomas Winklerfc4b6852007-10-25 17:15:24 +08002465 tfd = &txq->bd[q->write_ptr];
Zhu Yib481de92007-09-25 17:54:57 -07002466 memset(tfd, 0, sizeof(*tfd));
2467 control_flags = (u32 *) tfd;
Tomas Winklerfc4b6852007-10-25 17:15:24 +08002468 idx = get_cmd_index(q, q->write_ptr, 0);
Zhu Yib481de92007-09-25 17:54:57 -07002469
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002470 /* Set up driver data for this TFD */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002471 memset(&(txq->txb[q->write_ptr]), 0, sizeof(struct iwl3945_tx_info));
Tomas Winklerfc4b6852007-10-25 17:15:24 +08002472 txq->txb[q->write_ptr].skb[0] = skb;
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002473
2474 /* Init first empty entry in queue's array of Tx/cmd buffers */
Zhu Yib481de92007-09-25 17:54:57 -07002475 out_cmd = &txq->cmd[idx];
2476 memset(&out_cmd->hdr, 0, sizeof(out_cmd->hdr));
2477 memset(&out_cmd->cmd.tx, 0, sizeof(out_cmd->cmd.tx));
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002478
2479 /*
2480 * Set up the Tx-command (not MAC!) header.
2481 * Store the chosen Tx queue and TFD index within the sequence field;
2482 * after Tx, uCode's Tx response will return this value so driver can
2483 * locate the frame within the tx queue and do post-tx processing.
2484 */
Zhu Yib481de92007-09-25 17:54:57 -07002485 out_cmd->hdr.cmd = REPLY_TX;
2486 out_cmd->hdr.sequence = cpu_to_le16((u16)(QUEUE_TO_SEQ(txq_id) |
Tomas Winklerfc4b6852007-10-25 17:15:24 +08002487 INDEX_TO_SEQ(q->write_ptr)));
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002488
2489 /* Copy MAC header from skb into command buffer */
Zhu Yib481de92007-09-25 17:54:57 -07002490 memcpy(out_cmd->cmd.tx.hdr, hdr, hdr_len);
2491
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002492 /*
2493 * Use the first empty entry in this queue's command buffer array
2494 * to contain the Tx command and MAC header concatenated together
2495 * (payload data will be in another buffer).
2496 * Size of this varies, due to varying MAC header length.
2497 * If end is not dword aligned, we'll have 2 extra bytes at the end
2498 * of the MAC header (device reads on dword boundaries).
2499 * We'll tell device about this padding later.
2500 */
Abhijeet Kolekar3832ec92008-12-19 10:37:26 +08002501 len = sizeof(struct iwl3945_tx_cmd) +
Tomas Winkler4c897252008-12-19 10:37:05 +08002502 sizeof(struct iwl_cmd_header) + hdr_len;
Zhu Yib481de92007-09-25 17:54:57 -07002503
2504 len_org = len;
2505 len = (len + 3) & ~3;
2506
2507 if (len_org != len)
2508 len_org = 1;
2509 else
2510 len_org = 0;
2511
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002512 /* Physical address of this Tx command's header (not MAC header!),
2513 * within command buffer array. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002514 txcmd_phys = txq->dma_addr_cmd + sizeof(struct iwl3945_cmd) * idx +
2515 offsetof(struct iwl3945_cmd, hdr);
Zhu Yib481de92007-09-25 17:54:57 -07002516
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002517 /* Add buffer containing Tx command and MAC(!) header to TFD's
2518 * first entry */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002519 iwl3945_hw_txq_attach_buf_to_tfd(priv, tfd, txcmd_phys, len);
Zhu Yib481de92007-09-25 17:54:57 -07002520
Johannes Bergd0f09802008-07-29 11:32:07 +02002521 if (info->control.hw_key)
Johannes Berge039fa42008-05-15 12:55:29 +02002522 iwl3945_build_tx_cmd_hwcrypto(priv, info, out_cmd, skb, 0);
Zhu Yib481de92007-09-25 17:54:57 -07002523
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002524 /* Set up TFD's 2nd entry to point directly to remainder of skb,
2525 * if any (802.11 null frames have no payload). */
Zhu Yib481de92007-09-25 17:54:57 -07002526 len = skb->len - hdr_len;
2527 if (len) {
2528 phys_addr = pci_map_single(priv->pci_dev, skb->data + hdr_len,
2529 len, PCI_DMA_TODEVICE);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002530 iwl3945_hw_txq_attach_buf_to_tfd(priv, tfd, phys_addr, len);
Zhu Yib481de92007-09-25 17:54:57 -07002531 }
2532
Zhu Yib481de92007-09-25 17:54:57 -07002533 if (!len)
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002534 /* If there is no payload, then we use only one Tx buffer */
Zhu Yib481de92007-09-25 17:54:57 -07002535 *control_flags = TFD_CTL_COUNT_SET(1);
2536 else
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002537 /* Else use 2 buffers.
2538 * Tell 3945 about any padding after MAC header */
Zhu Yib481de92007-09-25 17:54:57 -07002539 *control_flags = TFD_CTL_COUNT_SET(2) |
2540 TFD_CTL_PAD_SET(U32_PAD(len));
2541
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002542 /* Total # bytes to be transmitted */
Zhu Yib481de92007-09-25 17:54:57 -07002543 len = (u16)skb->len;
2544 out_cmd->cmd.tx.len = cpu_to_le16(len);
2545
2546 /* TODO need this for burst mode later on */
Johannes Berge039fa42008-05-15 12:55:29 +02002547 iwl3945_build_tx_cmd_basic(priv, out_cmd, info, hdr, unicast, sta_id);
Zhu Yib481de92007-09-25 17:54:57 -07002548
2549 /* set is_hcca to 0; it probably will never be implemented */
Johannes Berge039fa42008-05-15 12:55:29 +02002550 iwl3945_hw_build_tx_cmd_rate(priv, out_cmd, info, hdr, sta_id, 0);
Zhu Yib481de92007-09-25 17:54:57 -07002551
2552 out_cmd->cmd.tx.tx_flags &= ~TX_CMD_FLG_ANT_A_MSK;
2553 out_cmd->cmd.tx.tx_flags &= ~TX_CMD_FLG_ANT_B_MSK;
2554
Harvey Harrison8b7b1e02008-06-11 14:21:56 -07002555 if (!ieee80211_has_morefrags(hdr->frame_control)) {
Zhu Yib481de92007-09-25 17:54:57 -07002556 txq->need_update = 1;
Tomas Winkler3ac7f142008-07-21 02:40:14 +03002557 if (qc)
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08002558 priv->stations_39[sta_id].tid[tid].seq_number = seq_number;
Zhu Yib481de92007-09-25 17:54:57 -07002559 } else {
2560 wait_write_ptr = 1;
2561 txq->need_update = 0;
2562 }
2563
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08002564 iwl_print_hex_dump(priv, IWL_DL_TX, out_cmd->cmd.payload,
Zhu Yib481de92007-09-25 17:54:57 -07002565 sizeof(out_cmd->cmd.tx));
2566
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08002567 iwl_print_hex_dump(priv, IWL_DL_TX, (u8 *)out_cmd->cmd.tx.hdr,
Harvey Harrison7294ec92008-07-15 18:43:59 -07002568 ieee80211_hdrlen(fc));
Zhu Yib481de92007-09-25 17:54:57 -07002569
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002570 /* Tell device the write index *just past* this latest filled TFD */
Tomas Winklerc54b6792008-03-06 17:36:53 -08002571 q->write_ptr = iwl_queue_inc_wrap(q->write_ptr, q->n_bd);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002572 rc = iwl3945_tx_queue_update_write_ptr(priv, txq);
Zhu Yib481de92007-09-25 17:54:57 -07002573 spin_unlock_irqrestore(&priv->lock, flags);
2574
2575 if (rc)
2576 return rc;
2577
Samuel Ortizd20b3c62008-12-19 10:37:15 +08002578 if ((iwl_queue_space(q) < q->high_mark)
Zhu Yib481de92007-09-25 17:54:57 -07002579 && priv->mac80211_registered) {
2580 if (wait_write_ptr) {
2581 spin_lock_irqsave(&priv->lock, flags);
2582 txq->need_update = 1;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002583 iwl3945_tx_queue_update_write_ptr(priv, txq);
Zhu Yib481de92007-09-25 17:54:57 -07002584 spin_unlock_irqrestore(&priv->lock, flags);
2585 }
2586
Johannes Berge2530082008-05-17 00:57:14 +02002587 ieee80211_stop_queue(priv->hw, skb_get_queue_mapping(skb));
Zhu Yib481de92007-09-25 17:54:57 -07002588 }
2589
2590 return 0;
2591
2592drop_unlock:
2593 spin_unlock_irqrestore(&priv->lock, flags);
2594drop:
2595 return -1;
2596}
2597
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002598static void iwl3945_set_rate(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002599{
Johannes Berg8318d782008-01-24 19:38:38 +01002600 const struct ieee80211_supported_band *sband = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07002601 struct ieee80211_rate *rate;
2602 int i;
2603
Johannes Berg8318d782008-01-24 19:38:38 +01002604 sband = iwl3945_get_band(priv, priv->band);
2605 if (!sband) {
Saleem Abdulrasoolc4ba9622007-11-18 23:59:08 -08002606 IWL_ERROR("Failed to set rate: unable to get hw mode\n");
2607 return;
2608 }
Zhu Yib481de92007-09-25 17:54:57 -07002609
2610 priv->active_rate = 0;
2611 priv->active_rate_basic = 0;
2612
Johannes Berg8318d782008-01-24 19:38:38 +01002613 IWL_DEBUG_RATE("Setting rates for %s GHz\n",
2614 sband->band == IEEE80211_BAND_2GHZ ? "2.4" : "5");
Zhu Yib481de92007-09-25 17:54:57 -07002615
Johannes Berg8318d782008-01-24 19:38:38 +01002616 for (i = 0; i < sband->n_bitrates; i++) {
2617 rate = &sband->bitrates[i];
2618 if ((rate->hw_value < IWL_RATE_COUNT) &&
2619 !(rate->flags & IEEE80211_CHAN_DISABLED)) {
2620 IWL_DEBUG_RATE("Adding rate index %d (plcp %d)\n",
2621 rate->hw_value, iwl3945_rates[rate->hw_value].plcp);
2622 priv->active_rate |= (1 << rate->hw_value);
2623 }
Zhu Yib481de92007-09-25 17:54:57 -07002624 }
2625
2626 IWL_DEBUG_RATE("Set active_rate = %0x, active_rate_basic = %0x\n",
2627 priv->active_rate, priv->active_rate_basic);
2628
2629 /*
2630 * If a basic rate is configured, then use it (adding IWL_RATE_1M_MASK)
2631 * otherwise set it to the default of all CCK rates and 6, 12, 24 for
2632 * OFDM
2633 */
2634 if (priv->active_rate_basic & IWL_CCK_BASIC_RATES_MASK)
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08002635 priv->staging39_rxon.cck_basic_rates =
Zhu Yib481de92007-09-25 17:54:57 -07002636 ((priv->active_rate_basic &
2637 IWL_CCK_RATES_MASK) >> IWL_FIRST_CCK_RATE) & 0xF;
2638 else
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08002639 priv->staging39_rxon.cck_basic_rates =
Zhu Yib481de92007-09-25 17:54:57 -07002640 (IWL_CCK_BASIC_RATES_MASK >> IWL_FIRST_CCK_RATE) & 0xF;
2641
2642 if (priv->active_rate_basic & IWL_OFDM_BASIC_RATES_MASK)
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08002643 priv->staging39_rxon.ofdm_basic_rates =
Zhu Yib481de92007-09-25 17:54:57 -07002644 ((priv->active_rate_basic &
2645 (IWL_OFDM_BASIC_RATES_MASK | IWL_RATE_6M_MASK)) >>
2646 IWL_FIRST_OFDM_RATE) & 0xFF;
2647 else
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08002648 priv->staging39_rxon.ofdm_basic_rates =
Zhu Yib481de92007-09-25 17:54:57 -07002649 (IWL_OFDM_BASIC_RATES_MASK >> IWL_FIRST_OFDM_RATE) & 0xFF;
2650}
2651
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002652static void iwl3945_radio_kill_sw(struct iwl_priv *priv, int disable_radio)
Zhu Yib481de92007-09-25 17:54:57 -07002653{
2654 unsigned long flags;
2655
2656 if (!!disable_radio == test_bit(STATUS_RF_KILL_SW, &priv->status))
2657 return;
2658
2659 IWL_DEBUG_RF_KILL("Manual SW RF KILL set to: RADIO %s\n",
2660 disable_radio ? "OFF" : "ON");
2661
2662 if (disable_radio) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002663 iwl3945_scan_cancel(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002664 /* FIXME: This is a workaround for AP */
Johannes Berg05c914f2008-09-11 00:01:58 +02002665 if (priv->iw_mode != NL80211_IFTYPE_AP) {
Zhu Yib481de92007-09-25 17:54:57 -07002666 spin_lock_irqsave(&priv->lock, flags);
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08002667 iwl_write32(priv, CSR_UCODE_DRV_GP1_SET,
Zhu Yib481de92007-09-25 17:54:57 -07002668 CSR_UCODE_SW_BIT_RFKILL);
2669 spin_unlock_irqrestore(&priv->lock, flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002670 iwl3945_send_card_state(priv, CARD_STATE_CMD_DISABLE, 0);
Zhu Yib481de92007-09-25 17:54:57 -07002671 set_bit(STATUS_RF_KILL_SW, &priv->status);
2672 }
2673 return;
2674 }
2675
2676 spin_lock_irqsave(&priv->lock, flags);
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08002677 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
Zhu Yib481de92007-09-25 17:54:57 -07002678
2679 clear_bit(STATUS_RF_KILL_SW, &priv->status);
2680 spin_unlock_irqrestore(&priv->lock, flags);
2681
2682 /* wake up ucode */
2683 msleep(10);
2684
2685 spin_lock_irqsave(&priv->lock, flags);
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08002686 iwl_read32(priv, CSR_UCODE_DRV_GP1);
2687 if (!iwl_grab_nic_access(priv))
2688 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002689 spin_unlock_irqrestore(&priv->lock, flags);
2690
2691 if (test_bit(STATUS_RF_KILL_HW, &priv->status)) {
2692 IWL_DEBUG_RF_KILL("Can not turn radio back on - "
2693 "disabled by HW switch\n");
2694 return;
2695 }
2696
Zhu Yi808e72a2008-06-12 09:47:17 +08002697 if (priv->is_open)
2698 queue_work(priv->workqueue, &priv->restart);
Zhu Yib481de92007-09-25 17:54:57 -07002699 return;
2700}
2701
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002702void iwl3945_set_decrypted_flag(struct iwl_priv *priv, struct sk_buff *skb,
Zhu Yib481de92007-09-25 17:54:57 -07002703 u32 decrypt_res, struct ieee80211_rx_status *stats)
2704{
2705 u16 fc =
2706 le16_to_cpu(((struct ieee80211_hdr *)skb->data)->frame_control);
2707
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08002708 if (priv->active39_rxon.filter_flags & RXON_FILTER_DIS_DECRYPT_MSK)
Zhu Yib481de92007-09-25 17:54:57 -07002709 return;
2710
2711 if (!(fc & IEEE80211_FCTL_PROTECTED))
2712 return;
2713
2714 IWL_DEBUG_RX("decrypt_res:0x%x\n", decrypt_res);
2715 switch (decrypt_res & RX_RES_STATUS_SEC_TYPE_MSK) {
2716 case RX_RES_STATUS_SEC_TYPE_TKIP:
2717 if ((decrypt_res & RX_RES_STATUS_DECRYPT_TYPE_MSK) ==
2718 RX_RES_STATUS_BAD_ICV_MIC)
2719 stats->flag |= RX_FLAG_MMIC_ERROR;
2720 case RX_RES_STATUS_SEC_TYPE_WEP:
2721 case RX_RES_STATUS_SEC_TYPE_CCMP:
2722 if ((decrypt_res & RX_RES_STATUS_DECRYPT_TYPE_MSK) ==
2723 RX_RES_STATUS_DECRYPT_OK) {
2724 IWL_DEBUG_RX("hw decrypt successfully!!!\n");
2725 stats->flag |= RX_FLAG_DECRYPTED;
2726 }
2727 break;
2728
2729 default:
2730 break;
2731 }
2732}
2733
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08002734#ifdef CONFIG_IWL3945_SPECTRUM_MEASUREMENT
Zhu Yib481de92007-09-25 17:54:57 -07002735
2736#include "iwl-spectrum.h"
2737
2738#define BEACON_TIME_MASK_LOW 0x00FFFFFF
2739#define BEACON_TIME_MASK_HIGH 0xFF000000
2740#define TIME_UNIT 1024
2741
2742/*
2743 * extended beacon time format
2744 * time in usec will be changed into a 32-bit value in 8:24 format
2745 * the high 1 byte is the beacon counts
2746 * the lower 3 bytes is the time in usec within one beacon interval
2747 */
2748
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002749static u32 iwl3945_usecs_to_beacons(u32 usec, u32 beacon_interval)
Zhu Yib481de92007-09-25 17:54:57 -07002750{
2751 u32 quot;
2752 u32 rem;
2753 u32 interval = beacon_interval * 1024;
2754
2755 if (!interval || !usec)
2756 return 0;
2757
2758 quot = (usec / interval) & (BEACON_TIME_MASK_HIGH >> 24);
2759 rem = (usec % interval) & BEACON_TIME_MASK_LOW;
2760
2761 return (quot << 24) + rem;
2762}
2763
2764/* base is usually what we get from ucode with each received frame,
2765 * the same as HW timer counter counting down
2766 */
2767
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002768static __le32 iwl3945_add_beacon_time(u32 base, u32 addon, u32 beacon_interval)
Zhu Yib481de92007-09-25 17:54:57 -07002769{
2770 u32 base_low = base & BEACON_TIME_MASK_LOW;
2771 u32 addon_low = addon & BEACON_TIME_MASK_LOW;
2772 u32 interval = beacon_interval * TIME_UNIT;
2773 u32 res = (base & BEACON_TIME_MASK_HIGH) +
2774 (addon & BEACON_TIME_MASK_HIGH);
2775
2776 if (base_low > addon_low)
2777 res += base_low - addon_low;
2778 else if (base_low < addon_low) {
2779 res += interval + base_low - addon_low;
2780 res += (1 << 24);
2781 } else
2782 res += (1 << 24);
2783
2784 return cpu_to_le32(res);
2785}
2786
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002787static int iwl3945_get_measurement(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07002788 struct ieee80211_measurement_params *params,
2789 u8 type)
2790{
Tomas Winkler600c0e12008-12-19 10:37:04 +08002791 struct iwl_spectrum_cmd spectrum;
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08002792 struct iwl_rx_packet *res;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002793 struct iwl3945_host_cmd cmd = {
Zhu Yib481de92007-09-25 17:54:57 -07002794 .id = REPLY_SPECTRUM_MEASUREMENT_CMD,
2795 .data = (void *)&spectrum,
2796 .meta.flags = CMD_WANT_SKB,
2797 };
2798 u32 add_time = le64_to_cpu(params->start_time);
2799 int rc;
2800 int spectrum_resp_status;
2801 int duration = le16_to_cpu(params->duration);
2802
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002803 if (iwl3945_is_associated(priv))
Zhu Yib481de92007-09-25 17:54:57 -07002804 add_time =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002805 iwl3945_usecs_to_beacons(
Zhu Yib481de92007-09-25 17:54:57 -07002806 le64_to_cpu(params->start_time) - priv->last_tsf,
2807 le16_to_cpu(priv->rxon_timing.beacon_interval));
2808
2809 memset(&spectrum, 0, sizeof(spectrum));
2810
2811 spectrum.channel_count = cpu_to_le16(1);
2812 spectrum.flags =
2813 RXON_FLG_TSF2HOST_MSK | RXON_FLG_ANT_A_MSK | RXON_FLG_DIS_DIV_MSK;
2814 spectrum.filter_flags = MEASUREMENT_FILTER_FLAG;
2815 cmd.len = sizeof(spectrum);
2816 spectrum.len = cpu_to_le16(cmd.len - sizeof(spectrum.len));
2817
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002818 if (iwl3945_is_associated(priv))
Zhu Yib481de92007-09-25 17:54:57 -07002819 spectrum.start_time =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002820 iwl3945_add_beacon_time(priv->last_beacon_time,
Zhu Yib481de92007-09-25 17:54:57 -07002821 add_time,
2822 le16_to_cpu(priv->rxon_timing.beacon_interval));
2823 else
2824 spectrum.start_time = 0;
2825
2826 spectrum.channels[0].duration = cpu_to_le32(duration * TIME_UNIT);
2827 spectrum.channels[0].channel = params->channel;
2828 spectrum.channels[0].type = type;
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08002829 if (priv->active39_rxon.flags & RXON_FLG_BAND_24G_MSK)
Zhu Yib481de92007-09-25 17:54:57 -07002830 spectrum.flags |= RXON_FLG_BAND_24G_MSK |
2831 RXON_FLG_AUTO_DETECT_MSK | RXON_FLG_TGG_PROTECT_MSK;
2832
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002833 rc = iwl3945_send_cmd_sync(priv, &cmd);
Zhu Yib481de92007-09-25 17:54:57 -07002834 if (rc)
2835 return rc;
2836
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08002837 res = (struct iwl_rx_packet *)cmd.meta.u.skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07002838 if (res->hdr.flags & IWL_CMD_FAILED_MSK) {
2839 IWL_ERROR("Bad return from REPLY_RX_ON_ASSOC command\n");
2840 rc = -EIO;
2841 }
2842
2843 spectrum_resp_status = le16_to_cpu(res->u.spectrum.status);
2844 switch (spectrum_resp_status) {
2845 case 0: /* Command will be handled */
2846 if (res->u.spectrum.id != 0xff) {
Ian Schrambc434dd2007-10-25 17:15:29 +08002847 IWL_DEBUG_INFO("Replaced existing measurement: %d\n",
2848 res->u.spectrum.id);
Zhu Yib481de92007-09-25 17:54:57 -07002849 priv->measurement_status &= ~MEASUREMENT_READY;
2850 }
2851 priv->measurement_status |= MEASUREMENT_ACTIVE;
2852 rc = 0;
2853 break;
2854
2855 case 1: /* Command will not be handled */
2856 rc = -EAGAIN;
2857 break;
2858 }
2859
2860 dev_kfree_skb_any(cmd.meta.u.skb);
2861
2862 return rc;
2863}
2864#endif
2865
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002866static void iwl3945_rx_reply_alive(struct iwl_priv *priv,
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08002867 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07002868{
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08002869 struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
2870 struct iwl_alive_resp *palive;
Zhu Yib481de92007-09-25 17:54:57 -07002871 struct delayed_work *pwork;
2872
2873 palive = &pkt->u.alive_frame;
2874
2875 IWL_DEBUG_INFO("Alive ucode status 0x%08X revision "
2876 "0x%01X 0x%01X\n",
2877 palive->is_valid, palive->ver_type,
2878 palive->ver_subtype);
2879
2880 if (palive->ver_subtype == INITIALIZE_SUBTYPE) {
2881 IWL_DEBUG_INFO("Initialization Alive received.\n");
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08002882 memcpy(&priv->card_alive_init, &pkt->u.alive_frame,
2883 sizeof(struct iwl_alive_resp));
Zhu Yib481de92007-09-25 17:54:57 -07002884 pwork = &priv->init_alive_start;
2885 } else {
2886 IWL_DEBUG_INFO("Runtime Alive received.\n");
2887 memcpy(&priv->card_alive, &pkt->u.alive_frame,
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08002888 sizeof(struct iwl_alive_resp));
Zhu Yib481de92007-09-25 17:54:57 -07002889 pwork = &priv->alive_start;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002890 iwl3945_disable_events(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002891 }
2892
2893 /* We delay the ALIVE response by 5ms to
2894 * give the HW RF Kill time to activate... */
2895 if (palive->is_valid == UCODE_VALID_OK)
2896 queue_delayed_work(priv->workqueue, pwork,
2897 msecs_to_jiffies(5));
2898 else
Winkler, Tomas39aadf82008-12-19 10:37:32 +08002899 IWL_WARN(priv, "uCode did not respond OK.\n");
Zhu Yib481de92007-09-25 17:54:57 -07002900}
2901
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002902static void iwl3945_rx_reply_add_sta(struct iwl_priv *priv,
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08002903 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07002904{
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08002905 struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07002906
2907 IWL_DEBUG_RX("Received REPLY_ADD_STA: 0x%02X\n", pkt->u.status);
2908 return;
2909}
2910
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002911static void iwl3945_rx_reply_error(struct iwl_priv *priv,
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08002912 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07002913{
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08002914 struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07002915
2916 IWL_ERROR("Error Reply type 0x%08X cmd %s (0x%02X) "
2917 "seq 0x%04X ser 0x%08X\n",
2918 le32_to_cpu(pkt->u.err_resp.error_type),
2919 get_cmd_string(pkt->u.err_resp.cmd_id),
2920 pkt->u.err_resp.cmd_id,
2921 le16_to_cpu(pkt->u.err_resp.bad_cmd_seq_num),
2922 le32_to_cpu(pkt->u.err_resp.error_info));
2923}
2924
2925#define TX_STATUS_ENTRY(x) case TX_STATUS_FAIL_ ## x: return #x
2926
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002927static void iwl3945_rx_csa(struct iwl_priv *priv, struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07002928{
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08002929 struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08002930 struct iwl3945_rxon_cmd *rxon = (void *)&priv->active39_rxon;
Tomas Winkler600c0e12008-12-19 10:37:04 +08002931 struct iwl_csa_notification *csa = &(pkt->u.csa_notif);
Zhu Yib481de92007-09-25 17:54:57 -07002932 IWL_DEBUG_11H("CSA notif: channel %d, status %d\n",
2933 le16_to_cpu(csa->channel), le32_to_cpu(csa->status));
2934 rxon->channel = csa->channel;
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08002935 priv->staging39_rxon.channel = csa->channel;
Zhu Yib481de92007-09-25 17:54:57 -07002936}
2937
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002938static void iwl3945_rx_spectrum_measure_notif(struct iwl_priv *priv,
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08002939 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07002940{
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08002941#ifdef CONFIG_IWL3945_SPECTRUM_MEASUREMENT
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08002942 struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
Tomas Winkler600c0e12008-12-19 10:37:04 +08002943 struct iwl_spectrum_notification *report = &(pkt->u.spectrum_notif);
Zhu Yib481de92007-09-25 17:54:57 -07002944
2945 if (!report->state) {
2946 IWL_DEBUG(IWL_DL_11H | IWL_DL_INFO,
2947 "Spectrum Measure Notification: Start\n");
2948 return;
2949 }
2950
2951 memcpy(&priv->measure_report, report, sizeof(*report));
2952 priv->measurement_status |= MEASUREMENT_READY;
2953#endif
2954}
2955
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002956static void iwl3945_rx_pm_sleep_notif(struct iwl_priv *priv,
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08002957 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07002958{
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08002959#ifdef CONFIG_IWL3945_DEBUG
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08002960 struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
Tomas Winkler600c0e12008-12-19 10:37:04 +08002961 struct iwl_sleep_notification *sleep = &(pkt->u.sleep_notif);
Zhu Yib481de92007-09-25 17:54:57 -07002962 IWL_DEBUG_RX("sleep mode: %d, src: %d\n",
2963 sleep->pm_sleep_mode, sleep->pm_wakeup_src);
2964#endif
2965}
2966
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002967static void iwl3945_rx_pm_debug_statistics_notif(struct iwl_priv *priv,
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08002968 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07002969{
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08002970 struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07002971 IWL_DEBUG_RADIO("Dumping %d bytes of unhandled "
2972 "notification for %s:\n",
2973 le32_to_cpu(pkt->len), get_cmd_string(pkt->hdr.cmd));
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08002974 iwl_print_hex_dump(priv, IWL_DL_RADIO, pkt->u.raw,
2975 le32_to_cpu(pkt->len));
Zhu Yib481de92007-09-25 17:54:57 -07002976}
2977
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002978static void iwl3945_bg_beacon_update(struct work_struct *work)
Zhu Yib481de92007-09-25 17:54:57 -07002979{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002980 struct iwl_priv *priv =
2981 container_of(work, struct iwl_priv, beacon_update);
Zhu Yib481de92007-09-25 17:54:57 -07002982 struct sk_buff *beacon;
2983
2984 /* Pull updated AP beacon from mac80211. will fail if not in AP mode */
Johannes Berge039fa42008-05-15 12:55:29 +02002985 beacon = ieee80211_beacon_get(priv->hw, priv->vif);
Zhu Yib481de92007-09-25 17:54:57 -07002986
2987 if (!beacon) {
2988 IWL_ERROR("update beacon failed\n");
2989 return;
2990 }
2991
2992 mutex_lock(&priv->mutex);
2993 /* new beacon skb is allocated every time; dispose previous.*/
2994 if (priv->ibss_beacon)
2995 dev_kfree_skb(priv->ibss_beacon);
2996
2997 priv->ibss_beacon = beacon;
2998 mutex_unlock(&priv->mutex);
2999
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003000 iwl3945_send_beacon_cmd(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003001}
3002
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003003static void iwl3945_rx_beacon_notif(struct iwl_priv *priv,
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08003004 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003005{
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08003006#ifdef CONFIG_IWL3945_DEBUG
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08003007 struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003008 struct iwl3945_beacon_notif *beacon = &(pkt->u.beacon_status);
Zhu Yib481de92007-09-25 17:54:57 -07003009 u8 rate = beacon->beacon_notify_hdr.rate;
3010
3011 IWL_DEBUG_RX("beacon status %x retries %d iss %d "
3012 "tsf %d %d rate %d\n",
3013 le32_to_cpu(beacon->beacon_notify_hdr.status) & TX_STATUS_MSK,
3014 beacon->beacon_notify_hdr.failure_frame,
3015 le32_to_cpu(beacon->ibss_mgr_status),
3016 le32_to_cpu(beacon->high_tsf),
3017 le32_to_cpu(beacon->low_tsf), rate);
3018#endif
3019
Johannes Berg05c914f2008-09-11 00:01:58 +02003020 if ((priv->iw_mode == NL80211_IFTYPE_AP) &&
Zhu Yib481de92007-09-25 17:54:57 -07003021 (!test_bit(STATUS_EXIT_PENDING, &priv->status)))
3022 queue_work(priv->workqueue, &priv->beacon_update);
3023}
3024
3025/* Service response to REPLY_SCAN_CMD (0x80) */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003026static void iwl3945_rx_reply_scan(struct iwl_priv *priv,
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08003027 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003028{
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08003029#ifdef CONFIG_IWL3945_DEBUG
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08003030 struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
Tomas Winkler4c897252008-12-19 10:37:05 +08003031 struct iwl_scanreq_notification *notif =
3032 (struct iwl_scanreq_notification *)pkt->u.raw;
Zhu Yib481de92007-09-25 17:54:57 -07003033
3034 IWL_DEBUG_RX("Scan request status = 0x%x\n", notif->status);
3035#endif
3036}
3037
3038/* Service SCAN_START_NOTIFICATION (0x82) */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003039static void iwl3945_rx_scan_start_notif(struct iwl_priv *priv,
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08003040 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003041{
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08003042 struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
Tomas Winkler4c897252008-12-19 10:37:05 +08003043 struct iwl_scanstart_notification *notif =
3044 (struct iwl_scanstart_notification *)pkt->u.raw;
Zhu Yib481de92007-09-25 17:54:57 -07003045 priv->scan_start_tsf = le32_to_cpu(notif->tsf_low);
3046 IWL_DEBUG_SCAN("Scan start: "
3047 "%d [802.11%s] "
3048 "(TSF: 0x%08X:%08X) - %d (beacon timer %u)\n",
3049 notif->channel,
3050 notif->band ? "bg" : "a",
3051 notif->tsf_high,
3052 notif->tsf_low, notif->status, notif->beacon_timer);
3053}
3054
3055/* Service SCAN_RESULTS_NOTIFICATION (0x83) */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003056static void iwl3945_rx_scan_results_notif(struct iwl_priv *priv,
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08003057 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003058{
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08003059 struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
Tomas Winkler4c897252008-12-19 10:37:05 +08003060 struct iwl_scanresults_notification *notif =
3061 (struct iwl_scanresults_notification *)pkt->u.raw;
Zhu Yib481de92007-09-25 17:54:57 -07003062
3063 IWL_DEBUG_SCAN("Scan ch.res: "
3064 "%d [802.11%s] "
3065 "(TSF: 0x%08X:%08X) - %d "
3066 "elapsed=%lu usec (%dms since last)\n",
3067 notif->channel,
3068 notif->band ? "bg" : "a",
3069 le32_to_cpu(notif->tsf_high),
3070 le32_to_cpu(notif->tsf_low),
3071 le32_to_cpu(notif->statistics[0]),
3072 le32_to_cpu(notif->tsf_low) - priv->scan_start_tsf,
3073 jiffies_to_msecs(elapsed_jiffies
3074 (priv->last_scan_jiffies, jiffies)));
3075
3076 priv->last_scan_jiffies = jiffies;
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08003077 priv->next_scan_jiffies = 0;
Zhu Yib481de92007-09-25 17:54:57 -07003078}
3079
3080/* Service SCAN_COMPLETE_NOTIFICATION (0x84) */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003081static void iwl3945_rx_scan_complete_notif(struct iwl_priv *priv,
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08003082 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003083{
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08003084 struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
Tomas Winkler4c897252008-12-19 10:37:05 +08003085 struct iwl_scancomplete_notification *scan_notif = (void *)pkt->u.raw;
Zhu Yib481de92007-09-25 17:54:57 -07003086
3087 IWL_DEBUG_SCAN("Scan complete: %d channels (TSF 0x%08X:%08X) - %d\n",
3088 scan_notif->scanned_channels,
3089 scan_notif->tsf_low,
3090 scan_notif->tsf_high, scan_notif->status);
3091
3092 /* The HW is no longer scanning */
3093 clear_bit(STATUS_SCAN_HW, &priv->status);
3094
3095 /* The scan completion notification came in, so kill that timer... */
3096 cancel_delayed_work(&priv->scan_check);
3097
3098 IWL_DEBUG_INFO("Scan pass on %sGHz took %dms\n",
Ron Rindjunsky66b50042008-06-25 16:46:31 +08003099 (priv->scan_bands & BIT(IEEE80211_BAND_2GHZ)) ?
3100 "2.4" : "5.2",
Zhu Yib481de92007-09-25 17:54:57 -07003101 jiffies_to_msecs(elapsed_jiffies
3102 (priv->scan_pass_start, jiffies)));
3103
Ron Rindjunsky66b50042008-06-25 16:46:31 +08003104 /* Remove this scanned band from the list of pending
3105 * bands to scan, band G precedes A in order of scanning
3106 * as seen in iwl3945_bg_request_scan */
3107 if (priv->scan_bands & BIT(IEEE80211_BAND_2GHZ))
3108 priv->scan_bands &= ~BIT(IEEE80211_BAND_2GHZ);
3109 else if (priv->scan_bands & BIT(IEEE80211_BAND_5GHZ))
3110 priv->scan_bands &= ~BIT(IEEE80211_BAND_5GHZ);
Zhu Yib481de92007-09-25 17:54:57 -07003111
3112 /* If a request to abort was given, or the scan did not succeed
3113 * then we reset the scan state machine and terminate,
3114 * re-queuing another scan if one has been requested */
3115 if (test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
3116 IWL_DEBUG_INFO("Aborted scan completed.\n");
3117 clear_bit(STATUS_SCAN_ABORTING, &priv->status);
3118 } else {
3119 /* If there are more bands on this scan pass reschedule */
3120 if (priv->scan_bands > 0)
3121 goto reschedule;
3122 }
3123
3124 priv->last_scan_jiffies = jiffies;
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08003125 priv->next_scan_jiffies = 0;
Zhu Yib481de92007-09-25 17:54:57 -07003126 IWL_DEBUG_INFO("Setting scan to off\n");
3127
3128 clear_bit(STATUS_SCANNING, &priv->status);
3129
3130 IWL_DEBUG_INFO("Scan took %dms\n",
3131 jiffies_to_msecs(elapsed_jiffies(priv->scan_start, jiffies)));
3132
3133 queue_work(priv->workqueue, &priv->scan_completed);
3134
3135 return;
3136
3137reschedule:
3138 priv->scan_pass_start = jiffies;
3139 queue_work(priv->workqueue, &priv->request_scan);
3140}
3141
3142/* Handle notification from uCode that card's power state is changing
3143 * due to software, hardware, or critical temperature RFKILL */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003144static void iwl3945_rx_card_state_notif(struct iwl_priv *priv,
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08003145 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003146{
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08003147 struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07003148 u32 flags = le32_to_cpu(pkt->u.card_state_notif.flags);
3149 unsigned long status = priv->status;
3150
3151 IWL_DEBUG_RF_KILL("Card state received: HW:%s SW:%s\n",
3152 (flags & HW_CARD_DISABLED) ? "Kill" : "On",
3153 (flags & SW_CARD_DISABLED) ? "Kill" : "On");
3154
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003155 iwl_write32(priv, CSR_UCODE_DRV_GP1_SET,
Zhu Yib481de92007-09-25 17:54:57 -07003156 CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
3157
3158 if (flags & HW_CARD_DISABLED)
3159 set_bit(STATUS_RF_KILL_HW, &priv->status);
3160 else
3161 clear_bit(STATUS_RF_KILL_HW, &priv->status);
3162
3163
3164 if (flags & SW_CARD_DISABLED)
3165 set_bit(STATUS_RF_KILL_SW, &priv->status);
3166 else
3167 clear_bit(STATUS_RF_KILL_SW, &priv->status);
3168
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003169 iwl3945_scan_cancel(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003170
3171 if ((test_bit(STATUS_RF_KILL_HW, &status) !=
3172 test_bit(STATUS_RF_KILL_HW, &priv->status)) ||
3173 (test_bit(STATUS_RF_KILL_SW, &status) !=
3174 test_bit(STATUS_RF_KILL_SW, &priv->status)))
3175 queue_work(priv->workqueue, &priv->rf_kill);
3176 else
3177 wake_up_interruptible(&priv->wait_command_queue);
3178}
3179
3180/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003181 * iwl3945_setup_rx_handlers - Initialize Rx handler callbacks
Zhu Yib481de92007-09-25 17:54:57 -07003182 *
3183 * Setup the RX handlers for each of the reply types sent from the uCode
3184 * to the host.
3185 *
3186 * This function chains into the hardware specific files for them to setup
3187 * any hardware specific handlers as well.
3188 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003189static void iwl3945_setup_rx_handlers(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003190{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003191 priv->rx_handlers[REPLY_ALIVE] = iwl3945_rx_reply_alive;
3192 priv->rx_handlers[REPLY_ADD_STA] = iwl3945_rx_reply_add_sta;
3193 priv->rx_handlers[REPLY_ERROR] = iwl3945_rx_reply_error;
3194 priv->rx_handlers[CHANNEL_SWITCH_NOTIFICATION] = iwl3945_rx_csa;
Zhu Yib481de92007-09-25 17:54:57 -07003195 priv->rx_handlers[SPECTRUM_MEASURE_NOTIFICATION] =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003196 iwl3945_rx_spectrum_measure_notif;
3197 priv->rx_handlers[PM_SLEEP_NOTIFICATION] = iwl3945_rx_pm_sleep_notif;
Zhu Yib481de92007-09-25 17:54:57 -07003198 priv->rx_handlers[PM_DEBUG_STATISTIC_NOTIFIC] =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003199 iwl3945_rx_pm_debug_statistics_notif;
3200 priv->rx_handlers[BEACON_NOTIFICATION] = iwl3945_rx_beacon_notif;
Zhu Yib481de92007-09-25 17:54:57 -07003201
Ben Cahill9fbab512007-11-29 11:09:47 +08003202 /*
3203 * The same handler is used for both the REPLY to a discrete
3204 * statistics request from the host as well as for the periodic
3205 * statistics notifications (after received beacons) from the uCode.
Zhu Yib481de92007-09-25 17:54:57 -07003206 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003207 priv->rx_handlers[REPLY_STATISTICS_CMD] = iwl3945_hw_rx_statistics;
3208 priv->rx_handlers[STATISTICS_NOTIFICATION] = iwl3945_hw_rx_statistics;
Zhu Yib481de92007-09-25 17:54:57 -07003209
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003210 priv->rx_handlers[REPLY_SCAN_CMD] = iwl3945_rx_reply_scan;
3211 priv->rx_handlers[SCAN_START_NOTIFICATION] = iwl3945_rx_scan_start_notif;
Zhu Yib481de92007-09-25 17:54:57 -07003212 priv->rx_handlers[SCAN_RESULTS_NOTIFICATION] =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003213 iwl3945_rx_scan_results_notif;
Zhu Yib481de92007-09-25 17:54:57 -07003214 priv->rx_handlers[SCAN_COMPLETE_NOTIFICATION] =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003215 iwl3945_rx_scan_complete_notif;
3216 priv->rx_handlers[CARD_STATE_NOTIFICATION] = iwl3945_rx_card_state_notif;
Zhu Yib481de92007-09-25 17:54:57 -07003217
Ben Cahill9fbab512007-11-29 11:09:47 +08003218 /* Set up hardware specific Rx handlers */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003219 iwl3945_hw_rx_handler_setup(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003220}
3221
3222/**
Tomas Winkler91c066f2008-03-06 17:36:55 -08003223 * iwl3945_cmd_queue_reclaim - Reclaim CMD queue entries
3224 * When FW advances 'R' index, all entries between old and new 'R' index
3225 * need to be reclaimed.
3226 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003227static void iwl3945_cmd_queue_reclaim(struct iwl_priv *priv,
Tomas Winkler91c066f2008-03-06 17:36:55 -08003228 int txq_id, int index)
3229{
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08003230 struct iwl3945_tx_queue *txq = &priv->txq39[txq_id];
Samuel Ortizd20b3c62008-12-19 10:37:15 +08003231 struct iwl_queue *q = &txq->q;
Tomas Winkler91c066f2008-03-06 17:36:55 -08003232 int nfreed = 0;
3233
3234 if ((index >= q->n_bd) || (iwl3945_x2_queue_used(q, index) == 0)) {
3235 IWL_ERROR("Read index for DMA queue txq id (%d), index %d, "
3236 "is out of range [0-%d] %d %d.\n", txq_id,
3237 index, q->n_bd, q->write_ptr, q->read_ptr);
3238 return;
3239 }
3240
3241 for (index = iwl_queue_inc_wrap(index, q->n_bd); q->read_ptr != index;
3242 q->read_ptr = iwl_queue_inc_wrap(q->read_ptr, q->n_bd)) {
3243 if (nfreed > 1) {
3244 IWL_ERROR("HCMD skipped: index (%d) %d %d\n", index,
3245 q->write_ptr, q->read_ptr);
3246 queue_work(priv->workqueue, &priv->restart);
3247 break;
3248 }
3249 nfreed++;
3250 }
3251}
3252
3253
3254/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003255 * iwl3945_tx_cmd_complete - Pull unused buffers off the queue and reclaim them
Zhu Yib481de92007-09-25 17:54:57 -07003256 * @rxb: Rx buffer to reclaim
3257 *
3258 * If an Rx buffer has an async callback associated with it the callback
3259 * will be executed. The attached skb (if present) will only be freed
3260 * if the callback returns 1
3261 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003262static void iwl3945_tx_cmd_complete(struct iwl_priv *priv,
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08003263 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003264{
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08003265 struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07003266 u16 sequence = le16_to_cpu(pkt->hdr.sequence);
3267 int txq_id = SEQ_TO_QUEUE(sequence);
3268 int index = SEQ_TO_INDEX(sequence);
Tomas Winkler600c0e12008-12-19 10:37:04 +08003269 int huge = !!(pkt->hdr.sequence & SEQ_HUGE_FRAME);
Zhu Yib481de92007-09-25 17:54:57 -07003270 int cmd_index;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003271 struct iwl3945_cmd *cmd;
Zhu Yib481de92007-09-25 17:54:57 -07003272
Zhu Yib481de92007-09-25 17:54:57 -07003273 BUG_ON(txq_id != IWL_CMD_QUEUE_NUM);
3274
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08003275 cmd_index = get_cmd_index(&priv->txq39[IWL_CMD_QUEUE_NUM].q, index, huge);
3276 cmd = &priv->txq39[IWL_CMD_QUEUE_NUM].cmd[cmd_index];
Zhu Yib481de92007-09-25 17:54:57 -07003277
3278 /* Input error checking is done when commands are added to queue. */
3279 if (cmd->meta.flags & CMD_WANT_SKB) {
3280 cmd->meta.source->u.skb = rxb->skb;
3281 rxb->skb = NULL;
3282 } else if (cmd->meta.u.callback &&
3283 !cmd->meta.u.callback(priv, cmd, rxb->skb))
3284 rxb->skb = NULL;
3285
Tomas Winkler91c066f2008-03-06 17:36:55 -08003286 iwl3945_cmd_queue_reclaim(priv, txq_id, index);
Zhu Yib481de92007-09-25 17:54:57 -07003287
3288 if (!(cmd->meta.flags & CMD_ASYNC)) {
3289 clear_bit(STATUS_HCMD_ACTIVE, &priv->status);
3290 wake_up_interruptible(&priv->wait_command_queue);
3291 }
3292}
3293
3294/************************** RX-FUNCTIONS ****************************/
3295/*
3296 * Rx theory of operation
3297 *
3298 * The host allocates 32 DMA target addresses and passes the host address
3299 * to the firmware at register IWL_RFDS_TABLE_LOWER + N * RFD_SIZE where N is
3300 * 0 to 31
3301 *
3302 * Rx Queue Indexes
3303 * The host/firmware share two index registers for managing the Rx buffers.
3304 *
3305 * The READ index maps to the first position that the firmware may be writing
3306 * to -- the driver can read up to (but not including) this position and get
3307 * good data.
3308 * The READ index is managed by the firmware once the card is enabled.
3309 *
3310 * The WRITE index maps to the last position the driver has read from -- the
3311 * position preceding WRITE is the last slot the firmware can place a packet.
3312 *
3313 * The queue is empty (no good data) if WRITE = READ - 1, and is full if
3314 * WRITE = READ.
3315 *
Ben Cahill9fbab512007-11-29 11:09:47 +08003316 * During initialization, the host sets up the READ queue position to the first
Zhu Yib481de92007-09-25 17:54:57 -07003317 * INDEX position, and WRITE to the last (READ - 1 wrapped)
3318 *
Ben Cahill9fbab512007-11-29 11:09:47 +08003319 * When the firmware places a packet in a buffer, it will advance the READ index
Zhu Yib481de92007-09-25 17:54:57 -07003320 * and fire the RX interrupt. The driver can then query the READ index and
3321 * process as many packets as possible, moving the WRITE index forward as it
3322 * resets the Rx queue buffers with new memory.
3323 *
3324 * The management in the driver is as follows:
3325 * + A list of pre-allocated SKBs is stored in iwl->rxq->rx_free. When
3326 * iwl->rxq->free_count drops to or below RX_LOW_WATERMARK, work is scheduled
Ian Schram01ebd062007-10-25 17:15:22 +08003327 * to replenish the iwl->rxq->rx_free.
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003328 * + In iwl3945_rx_replenish (scheduled) if 'processed' != 'read' then the
Zhu Yib481de92007-09-25 17:54:57 -07003329 * iwl->rxq is replenished and the READ INDEX is updated (updating the
3330 * 'processed' and 'read' driver indexes as well)
3331 * + A received packet is processed and handed to the kernel network stack,
3332 * detached from the iwl->rxq. The driver 'processed' index is updated.
3333 * + The Host/Firmware iwl->rxq is replenished at tasklet time from the rx_free
3334 * list. If there are no allocated buffers in iwl->rxq->rx_free, the READ
3335 * INDEX is not incremented and iwl->status(RX_STALLED) is set. If there
3336 * were enough free buffers and RX_STALLED is set it is cleared.
3337 *
3338 *
3339 * Driver sequence:
3340 *
Ben Cahill9fbab512007-11-29 11:09:47 +08003341 * iwl3945_rx_queue_alloc() Allocates rx_free
3342 * iwl3945_rx_replenish() Replenishes rx_free list from rx_used, and calls
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003343 * iwl3945_rx_queue_restock
Ben Cahill9fbab512007-11-29 11:09:47 +08003344 * iwl3945_rx_queue_restock() Moves available buffers from rx_free into Rx
Zhu Yib481de92007-09-25 17:54:57 -07003345 * queue, updates firmware pointers, and updates
3346 * the WRITE index. If insufficient rx_free buffers
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003347 * are available, schedules iwl3945_rx_replenish
Zhu Yib481de92007-09-25 17:54:57 -07003348 *
3349 * -- enable interrupts --
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08003350 * ISR - iwl3945_rx() Detach iwl_rx_mem_buffers from pool up to the
Zhu Yib481de92007-09-25 17:54:57 -07003351 * READ INDEX, detaching the SKB from the pool.
3352 * Moves the packet buffer from queue to rx_used.
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003353 * Calls iwl3945_rx_queue_restock to refill any empty
Zhu Yib481de92007-09-25 17:54:57 -07003354 * slots.
3355 * ...
3356 *
3357 */
3358
3359/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003360 * iwl3945_rx_queue_space - Return number of free slots available in queue.
Zhu Yib481de92007-09-25 17:54:57 -07003361 */
Abhijeet Kolekarcc2f3622008-12-19 10:37:25 +08003362static int iwl3945_rx_queue_space(const struct iwl_rx_queue *q)
Zhu Yib481de92007-09-25 17:54:57 -07003363{
3364 int s = q->read - q->write;
3365 if (s <= 0)
3366 s += RX_QUEUE_SIZE;
3367 /* keep some buffer to not confuse full and empty queue */
3368 s -= 2;
3369 if (s < 0)
3370 s = 0;
3371 return s;
3372}
3373
3374/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003375 * iwl3945_rx_queue_update_write_ptr - Update the write pointer for the RX queue
Zhu Yib481de92007-09-25 17:54:57 -07003376 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003377int iwl3945_rx_queue_update_write_ptr(struct iwl_priv *priv, struct iwl_rx_queue *q)
Zhu Yib481de92007-09-25 17:54:57 -07003378{
3379 u32 reg = 0;
3380 int rc = 0;
3381 unsigned long flags;
3382
3383 spin_lock_irqsave(&q->lock, flags);
3384
3385 if (q->need_update == 0)
3386 goto exit_unlock;
3387
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003388 /* If power-saving is in use, make sure device is awake */
Zhu Yib481de92007-09-25 17:54:57 -07003389 if (test_bit(STATUS_POWER_PMI, &priv->status)) {
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003390 reg = iwl_read32(priv, CSR_UCODE_DRV_GP1);
Zhu Yib481de92007-09-25 17:54:57 -07003391
3392 if (reg & CSR_UCODE_DRV_GP1_BIT_MAC_SLEEP) {
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003393 iwl_set_bit(priv, CSR_GP_CNTRL,
Zhu Yib481de92007-09-25 17:54:57 -07003394 CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
3395 goto exit_unlock;
3396 }
3397
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003398 rc = iwl_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003399 if (rc)
3400 goto exit_unlock;
3401
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003402 /* Device expects a multiple of 8 */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003403 iwl_write_direct32(priv, FH39_RSCSR_CHNL0_WPTR,
Zhu Yib481de92007-09-25 17:54:57 -07003404 q->write & ~0x7);
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003405 iwl_release_nic_access(priv);
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003406
3407 /* Else device is assumed to be awake */
Zhu Yib481de92007-09-25 17:54:57 -07003408 } else
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003409 /* Device expects a multiple of 8 */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003410 iwl_write32(priv, FH39_RSCSR_CHNL0_WPTR, q->write & ~0x7);
Zhu Yib481de92007-09-25 17:54:57 -07003411
3412
3413 q->need_update = 0;
3414
3415 exit_unlock:
3416 spin_unlock_irqrestore(&q->lock, flags);
3417 return rc;
3418}
3419
3420/**
Ben Cahill9fbab512007-11-29 11:09:47 +08003421 * iwl3945_dma_addr2rbd_ptr - convert a DMA address to a uCode read buffer ptr
Zhu Yib481de92007-09-25 17:54:57 -07003422 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003423static inline __le32 iwl3945_dma_addr2rbd_ptr(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07003424 dma_addr_t dma_addr)
3425{
3426 return cpu_to_le32((u32)dma_addr);
3427}
3428
3429/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003430 * iwl3945_rx_queue_restock - refill RX queue from pre-allocated pool
Zhu Yib481de92007-09-25 17:54:57 -07003431 *
Ben Cahill9fbab512007-11-29 11:09:47 +08003432 * If there are slots in the RX queue that need to be restocked,
Zhu Yib481de92007-09-25 17:54:57 -07003433 * and we have free pre-allocated buffers, fill the ranks as much
Ben Cahill9fbab512007-11-29 11:09:47 +08003434 * as we can, pulling from rx_free.
Zhu Yib481de92007-09-25 17:54:57 -07003435 *
3436 * This moves the 'write' index forward to catch up with 'processed', and
3437 * also updates the memory address in the firmware to reference the new
3438 * target buffer.
3439 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003440static int iwl3945_rx_queue_restock(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003441{
Abhijeet Kolekarcc2f3622008-12-19 10:37:25 +08003442 struct iwl_rx_queue *rxq = &priv->rxq;
Zhu Yib481de92007-09-25 17:54:57 -07003443 struct list_head *element;
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08003444 struct iwl_rx_mem_buffer *rxb;
Zhu Yib481de92007-09-25 17:54:57 -07003445 unsigned long flags;
3446 int write, rc;
3447
3448 spin_lock_irqsave(&rxq->lock, flags);
3449 write = rxq->write & ~0x7;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003450 while ((iwl3945_rx_queue_space(rxq) > 0) && (rxq->free_count)) {
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003451 /* Get next free Rx buffer, remove from free list */
Zhu Yib481de92007-09-25 17:54:57 -07003452 element = rxq->rx_free.next;
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08003453 rxb = list_entry(element, struct iwl_rx_mem_buffer, list);
Zhu Yib481de92007-09-25 17:54:57 -07003454 list_del(element);
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003455
3456 /* Point to Rx buffer via next RBD in circular buffer */
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08003457 rxq->bd[rxq->write] = iwl3945_dma_addr2rbd_ptr(priv, rxb->real_dma_addr);
Zhu Yib481de92007-09-25 17:54:57 -07003458 rxq->queue[rxq->write] = rxb;
3459 rxq->write = (rxq->write + 1) & RX_QUEUE_MASK;
3460 rxq->free_count--;
3461 }
3462 spin_unlock_irqrestore(&rxq->lock, flags);
3463 /* If the pre-allocated buffer pool is dropping low, schedule to
3464 * refill it */
3465 if (rxq->free_count <= RX_LOW_WATERMARK)
3466 queue_work(priv->workqueue, &priv->rx_replenish);
3467
3468
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003469 /* If we've added more space for the firmware to place data, tell it.
3470 * Increment device's write pointer in multiples of 8. */
Zhu Yib481de92007-09-25 17:54:57 -07003471 if ((write != (rxq->write & ~0x7))
3472 || (abs(rxq->write - rxq->read) > 7)) {
3473 spin_lock_irqsave(&rxq->lock, flags);
3474 rxq->need_update = 1;
3475 spin_unlock_irqrestore(&rxq->lock, flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003476 rc = iwl3945_rx_queue_update_write_ptr(priv, rxq);
Zhu Yib481de92007-09-25 17:54:57 -07003477 if (rc)
3478 return rc;
3479 }
3480
3481 return 0;
3482}
3483
3484/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003485 * iwl3945_rx_replenish - Move all used packet from rx_used to rx_free
Zhu Yib481de92007-09-25 17:54:57 -07003486 *
3487 * When moving to rx_free an SKB is allocated for the slot.
3488 *
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003489 * Also restock the Rx queue via iwl3945_rx_queue_restock.
Ian Schram01ebd062007-10-25 17:15:22 +08003490 * This is called as a scheduled work item (except for during initialization)
Zhu Yib481de92007-09-25 17:54:57 -07003491 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003492static void iwl3945_rx_allocate(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003493{
Abhijeet Kolekarcc2f3622008-12-19 10:37:25 +08003494 struct iwl_rx_queue *rxq = &priv->rxq;
Zhu Yib481de92007-09-25 17:54:57 -07003495 struct list_head *element;
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08003496 struct iwl_rx_mem_buffer *rxb;
Zhu Yib481de92007-09-25 17:54:57 -07003497 unsigned long flags;
3498 spin_lock_irqsave(&rxq->lock, flags);
3499 while (!list_empty(&rxq->rx_used)) {
3500 element = rxq->rx_used.next;
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08003501 rxb = list_entry(element, struct iwl_rx_mem_buffer, list);
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003502
3503 /* Alloc a new receive buffer */
Zhu Yib481de92007-09-25 17:54:57 -07003504 rxb->skb =
3505 alloc_skb(IWL_RX_BUF_SIZE, __GFP_NOWARN | GFP_ATOMIC);
3506 if (!rxb->skb) {
3507 if (net_ratelimit())
Tomas Winkler978785a2008-12-19 10:37:31 +08003508 IWL_CRIT(priv, ": Can not allocate SKB buffers\n");
Zhu Yib481de92007-09-25 17:54:57 -07003509 /* We don't reschedule replenish work here -- we will
3510 * call the restock method and if it still needs
3511 * more buffers it will schedule replenish */
3512 break;
3513 }
Zhu Yi12342c42007-12-20 11:27:32 +08003514
3515 /* If radiotap head is required, reserve some headroom here.
3516 * The physical head count is a variable rx_stats->phy_count.
3517 * We reserve 4 bytes here. Plus these extra bytes, the
3518 * headroom of the physical head should be enough for the
3519 * radiotap head that iwl3945 supported. See iwl3945_rt.
3520 */
3521 skb_reserve(rxb->skb, 4);
3522
Zhu Yib481de92007-09-25 17:54:57 -07003523 priv->alloc_rxb_skb++;
3524 list_del(element);
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003525
3526 /* Get physical address of RB/SKB */
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08003527 rxb->real_dma_addr =
Zhu Yib481de92007-09-25 17:54:57 -07003528 pci_map_single(priv->pci_dev, rxb->skb->data,
3529 IWL_RX_BUF_SIZE, PCI_DMA_FROMDEVICE);
3530 list_add_tail(&rxb->list, &rxq->rx_free);
3531 rxq->free_count++;
3532 }
3533 spin_unlock_irqrestore(&rxq->lock, flags);
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08003534}
3535
3536/*
3537 * this should be called while priv->lock is locked
3538 */
Tomas Winkler4fd1f842007-12-05 20:59:58 +02003539static void __iwl3945_rx_replenish(void *data)
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08003540{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003541 struct iwl_priv *priv = data;
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08003542
3543 iwl3945_rx_allocate(priv);
3544 iwl3945_rx_queue_restock(priv);
3545}
3546
3547
3548void iwl3945_rx_replenish(void *data)
3549{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003550 struct iwl_priv *priv = data;
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08003551 unsigned long flags;
3552
3553 iwl3945_rx_allocate(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003554
3555 spin_lock_irqsave(&priv->lock, flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003556 iwl3945_rx_queue_restock(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003557 spin_unlock_irqrestore(&priv->lock, flags);
3558}
3559
3560/* Assumes that the skb field of the buffers in 'pool' is kept accurate.
Ben Cahill9fbab512007-11-29 11:09:47 +08003561 * If an SKB has been detached, the POOL needs to have its SKB set to NULL
Zhu Yib481de92007-09-25 17:54:57 -07003562 * This free routine walks the list of POOL entries and if SKB is set to
3563 * non NULL it is unmapped and freed
3564 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003565static void iwl3945_rx_queue_free(struct iwl_priv *priv, struct iwl_rx_queue *rxq)
Zhu Yib481de92007-09-25 17:54:57 -07003566{
3567 int i;
3568 for (i = 0; i < RX_QUEUE_SIZE + RX_FREE_BUFFERS; i++) {
3569 if (rxq->pool[i].skb != NULL) {
3570 pci_unmap_single(priv->pci_dev,
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08003571 rxq->pool[i].real_dma_addr,
Zhu Yib481de92007-09-25 17:54:57 -07003572 IWL_RX_BUF_SIZE, PCI_DMA_FROMDEVICE);
3573 dev_kfree_skb(rxq->pool[i].skb);
3574 }
3575 }
3576
3577 pci_free_consistent(priv->pci_dev, 4 * RX_QUEUE_SIZE, rxq->bd,
3578 rxq->dma_addr);
3579 rxq->bd = NULL;
3580}
3581
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003582int iwl3945_rx_queue_alloc(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003583{
Abhijeet Kolekarcc2f3622008-12-19 10:37:25 +08003584 struct iwl_rx_queue *rxq = &priv->rxq;
Zhu Yib481de92007-09-25 17:54:57 -07003585 struct pci_dev *dev = priv->pci_dev;
3586 int i;
3587
3588 spin_lock_init(&rxq->lock);
3589 INIT_LIST_HEAD(&rxq->rx_free);
3590 INIT_LIST_HEAD(&rxq->rx_used);
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003591
3592 /* Alloc the circular buffer of Read Buffer Descriptors (RBDs) */
Zhu Yib481de92007-09-25 17:54:57 -07003593 rxq->bd = pci_alloc_consistent(dev, 4 * RX_QUEUE_SIZE, &rxq->dma_addr);
3594 if (!rxq->bd)
3595 return -ENOMEM;
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003596
Zhu Yib481de92007-09-25 17:54:57 -07003597 /* Fill the rx_used queue with _all_ of the Rx buffers */
3598 for (i = 0; i < RX_FREE_BUFFERS + RX_QUEUE_SIZE; i++)
3599 list_add_tail(&rxq->pool[i].list, &rxq->rx_used);
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003600
Zhu Yib481de92007-09-25 17:54:57 -07003601 /* Set us so that we have processed and used all buffers, but have
3602 * not restocked the Rx queue with fresh buffers */
3603 rxq->read = rxq->write = 0;
3604 rxq->free_count = 0;
3605 rxq->need_update = 0;
3606 return 0;
3607}
3608
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003609void iwl3945_rx_queue_reset(struct iwl_priv *priv, struct iwl_rx_queue *rxq)
Zhu Yib481de92007-09-25 17:54:57 -07003610{
3611 unsigned long flags;
3612 int i;
3613 spin_lock_irqsave(&rxq->lock, flags);
3614 INIT_LIST_HEAD(&rxq->rx_free);
3615 INIT_LIST_HEAD(&rxq->rx_used);
3616 /* Fill the rx_used queue with _all_ of the Rx buffers */
3617 for (i = 0; i < RX_FREE_BUFFERS + RX_QUEUE_SIZE; i++) {
3618 /* In the reset function, these buffers may have been allocated
3619 * to an SKB, so we need to unmap and free potential storage */
3620 if (rxq->pool[i].skb != NULL) {
3621 pci_unmap_single(priv->pci_dev,
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08003622 rxq->pool[i].real_dma_addr,
Zhu Yib481de92007-09-25 17:54:57 -07003623 IWL_RX_BUF_SIZE, PCI_DMA_FROMDEVICE);
3624 priv->alloc_rxb_skb--;
3625 dev_kfree_skb(rxq->pool[i].skb);
3626 rxq->pool[i].skb = NULL;
3627 }
3628 list_add_tail(&rxq->pool[i].list, &rxq->rx_used);
3629 }
3630
3631 /* Set us so that we have processed and used all buffers, but have
3632 * not restocked the Rx queue with fresh buffers */
3633 rxq->read = rxq->write = 0;
3634 rxq->free_count = 0;
3635 spin_unlock_irqrestore(&rxq->lock, flags);
3636}
3637
3638/* Convert linear signal-to-noise ratio into dB */
3639static u8 ratio2dB[100] = {
3640/* 0 1 2 3 4 5 6 7 8 9 */
3641 0, 0, 6, 10, 12, 14, 16, 17, 18, 19, /* 00 - 09 */
3642 20, 21, 22, 22, 23, 23, 24, 25, 26, 26, /* 10 - 19 */
3643 26, 26, 26, 27, 27, 28, 28, 28, 29, 29, /* 20 - 29 */
3644 29, 30, 30, 30, 31, 31, 31, 31, 32, 32, /* 30 - 39 */
3645 32, 32, 32, 33, 33, 33, 33, 33, 34, 34, /* 40 - 49 */
3646 34, 34, 34, 34, 35, 35, 35, 35, 35, 35, /* 50 - 59 */
3647 36, 36, 36, 36, 36, 36, 36, 37, 37, 37, /* 60 - 69 */
3648 37, 37, 37, 37, 37, 38, 38, 38, 38, 38, /* 70 - 79 */
3649 38, 38, 38, 38, 38, 39, 39, 39, 39, 39, /* 80 - 89 */
3650 39, 39, 39, 39, 39, 40, 40, 40, 40, 40 /* 90 - 99 */
3651};
3652
3653/* Calculates a relative dB value from a ratio of linear
3654 * (i.e. not dB) signal levels.
3655 * Conversion assumes that levels are voltages (20*log), not powers (10*log). */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003656int iwl3945_calc_db_from_ratio(int sig_ratio)
Zhu Yib481de92007-09-25 17:54:57 -07003657{
Adrian Bunk221c80c2008-02-02 23:19:01 +02003658 /* 1000:1 or higher just report as 60 dB */
3659 if (sig_ratio >= 1000)
Zhu Yib481de92007-09-25 17:54:57 -07003660 return 60;
3661
Adrian Bunk221c80c2008-02-02 23:19:01 +02003662 /* 100:1 or higher, divide by 10 and use table,
Zhu Yib481de92007-09-25 17:54:57 -07003663 * add 20 dB to make up for divide by 10 */
Adrian Bunk221c80c2008-02-02 23:19:01 +02003664 if (sig_ratio >= 100)
Tomas Winkler3ac7f142008-07-21 02:40:14 +03003665 return 20 + (int)ratio2dB[sig_ratio/10];
Zhu Yib481de92007-09-25 17:54:57 -07003666
3667 /* We shouldn't see this */
3668 if (sig_ratio < 1)
3669 return 0;
3670
3671 /* Use table for ratios 1:1 - 99:1 */
3672 return (int)ratio2dB[sig_ratio];
3673}
3674
3675#define PERFECT_RSSI (-20) /* dBm */
3676#define WORST_RSSI (-95) /* dBm */
3677#define RSSI_RANGE (PERFECT_RSSI - WORST_RSSI)
3678
3679/* Calculate an indication of rx signal quality (a percentage, not dBm!).
3680 * See http://www.ces.clemson.edu/linux/signal_quality.shtml for info
3681 * about formulas used below. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003682int iwl3945_calc_sig_qual(int rssi_dbm, int noise_dbm)
Zhu Yib481de92007-09-25 17:54:57 -07003683{
3684 int sig_qual;
3685 int degradation = PERFECT_RSSI - rssi_dbm;
3686
3687 /* If we get a noise measurement, use signal-to-noise ratio (SNR)
3688 * as indicator; formula is (signal dbm - noise dbm).
3689 * SNR at or above 40 is a great signal (100%).
3690 * Below that, scale to fit SNR of 0 - 40 dB within 0 - 100% indicator.
3691 * Weakest usable signal is usually 10 - 15 dB SNR. */
3692 if (noise_dbm) {
3693 if (rssi_dbm - noise_dbm >= 40)
3694 return 100;
3695 else if (rssi_dbm < noise_dbm)
3696 return 0;
3697 sig_qual = ((rssi_dbm - noise_dbm) * 5) / 2;
3698
3699 /* Else use just the signal level.
3700 * This formula is a least squares fit of data points collected and
3701 * compared with a reference system that had a percentage (%) display
3702 * for signal quality. */
3703 } else
3704 sig_qual = (100 * (RSSI_RANGE * RSSI_RANGE) - degradation *
3705 (15 * RSSI_RANGE + 62 * degradation)) /
3706 (RSSI_RANGE * RSSI_RANGE);
3707
3708 if (sig_qual > 100)
3709 sig_qual = 100;
3710 else if (sig_qual < 1)
3711 sig_qual = 0;
3712
3713 return sig_qual;
3714}
3715
3716/**
Ben Cahill9fbab512007-11-29 11:09:47 +08003717 * iwl3945_rx_handle - Main entry function for receiving responses from uCode
Zhu Yib481de92007-09-25 17:54:57 -07003718 *
3719 * Uses the priv->rx_handlers callback function array to invoke
3720 * the appropriate handlers, including command responses,
3721 * frame-received notifications, and other notifications.
3722 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003723static void iwl3945_rx_handle(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003724{
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08003725 struct iwl_rx_mem_buffer *rxb;
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08003726 struct iwl_rx_packet *pkt;
Abhijeet Kolekarcc2f3622008-12-19 10:37:25 +08003727 struct iwl_rx_queue *rxq = &priv->rxq;
Zhu Yib481de92007-09-25 17:54:57 -07003728 u32 r, i;
3729 int reclaim;
3730 unsigned long flags;
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08003731 u8 fill_rx = 0;
Mohamed Abbasd68ab682008-02-07 13:16:33 -08003732 u32 count = 8;
Zhu Yib481de92007-09-25 17:54:57 -07003733
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003734 /* uCode's read index (stored in shared DRAM) indicates the last Rx
3735 * buffer that the driver may process (last buffer filled by ucode). */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003736 r = iwl3945_hw_get_rx_read(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003737 i = rxq->read;
3738
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08003739 if (iwl3945_rx_queue_space(rxq) > (RX_QUEUE_SIZE / 2))
3740 fill_rx = 1;
Zhu Yib481de92007-09-25 17:54:57 -07003741 /* Rx interrupt, but nothing sent from uCode */
3742 if (i == r)
3743 IWL_DEBUG(IWL_DL_RX | IWL_DL_ISR, "r = %d, i = %d\n", r, i);
3744
3745 while (i != r) {
3746 rxb = rxq->queue[i];
3747
Ben Cahill9fbab512007-11-29 11:09:47 +08003748 /* If an RXB doesn't have a Rx queue slot associated with it,
Zhu Yib481de92007-09-25 17:54:57 -07003749 * then a bug has been introduced in the queue refilling
3750 * routines -- catch it here */
3751 BUG_ON(rxb == NULL);
3752
3753 rxq->queue[i] = NULL;
3754
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08003755 pci_dma_sync_single_for_cpu(priv->pci_dev, rxb->real_dma_addr,
Zhu Yib481de92007-09-25 17:54:57 -07003756 IWL_RX_BUF_SIZE,
3757 PCI_DMA_FROMDEVICE);
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08003758 pkt = (struct iwl_rx_packet *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07003759
3760 /* Reclaim a command buffer only if this packet is a response
3761 * to a (driver-originated) command.
3762 * If the packet (e.g. Rx frame) originated from uCode,
3763 * there is no command buffer to reclaim.
3764 * Ucode should set SEQ_RX_FRAME bit if ucode-originated,
3765 * but apparently a few don't get set; catch them here. */
3766 reclaim = !(pkt->hdr.sequence & SEQ_RX_FRAME) &&
3767 (pkt->hdr.cmd != STATISTICS_NOTIFICATION) &&
3768 (pkt->hdr.cmd != REPLY_TX);
3769
3770 /* Based on type of command response or notification,
3771 * handle those that need handling via function in
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003772 * rx_handlers table. See iwl3945_setup_rx_handlers() */
Zhu Yib481de92007-09-25 17:54:57 -07003773 if (priv->rx_handlers[pkt->hdr.cmd]) {
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08003774 IWL_DEBUG(IWL_DL_HCMD | IWL_DL_RX | IWL_DL_ISR,
Zhu Yib481de92007-09-25 17:54:57 -07003775 "r = %d, i = %d, %s, 0x%02x\n", r, i,
3776 get_cmd_string(pkt->hdr.cmd), pkt->hdr.cmd);
3777 priv->rx_handlers[pkt->hdr.cmd] (priv, rxb);
3778 } else {
3779 /* No handling needed */
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08003780 IWL_DEBUG(IWL_DL_HCMD | IWL_DL_RX | IWL_DL_ISR,
Zhu Yib481de92007-09-25 17:54:57 -07003781 "r %d i %d No handler needed for %s, 0x%02x\n",
3782 r, i, get_cmd_string(pkt->hdr.cmd),
3783 pkt->hdr.cmd);
3784 }
3785
3786 if (reclaim) {
Ben Cahill9fbab512007-11-29 11:09:47 +08003787 /* Invoke any callbacks, transfer the skb to caller, and
3788 * fire off the (possibly) blocking iwl3945_send_cmd()
Zhu Yib481de92007-09-25 17:54:57 -07003789 * as we reclaim the driver command queue */
3790 if (rxb && rxb->skb)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003791 iwl3945_tx_cmd_complete(priv, rxb);
Zhu Yib481de92007-09-25 17:54:57 -07003792 else
Winkler, Tomas39aadf82008-12-19 10:37:32 +08003793 IWL_WARN(priv, "Claim null rxb?\n");
Zhu Yib481de92007-09-25 17:54:57 -07003794 }
3795
3796 /* For now we just don't re-use anything. We can tweak this
3797 * later to try and re-use notification packets and SKBs that
3798 * fail to Rx correctly */
3799 if (rxb->skb != NULL) {
3800 priv->alloc_rxb_skb--;
3801 dev_kfree_skb_any(rxb->skb);
3802 rxb->skb = NULL;
3803 }
3804
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08003805 pci_unmap_single(priv->pci_dev, rxb->real_dma_addr,
Zhu Yib481de92007-09-25 17:54:57 -07003806 IWL_RX_BUF_SIZE, PCI_DMA_FROMDEVICE);
3807 spin_lock_irqsave(&rxq->lock, flags);
3808 list_add_tail(&rxb->list, &priv->rxq.rx_used);
3809 spin_unlock_irqrestore(&rxq->lock, flags);
3810 i = (i + 1) & RX_QUEUE_MASK;
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08003811 /* If there are a lot of unused frames,
3812 * restock the Rx queue so ucode won't assert. */
3813 if (fill_rx) {
3814 count++;
3815 if (count >= 8) {
3816 priv->rxq.read = i;
3817 __iwl3945_rx_replenish(priv);
3818 count = 0;
3819 }
3820 }
Zhu Yib481de92007-09-25 17:54:57 -07003821 }
3822
3823 /* Backtrack one entry */
3824 priv->rxq.read = i;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003825 iwl3945_rx_queue_restock(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003826}
3827
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003828/**
3829 * iwl3945_tx_queue_update_write_ptr - Send new write index to hardware
3830 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003831static int iwl3945_tx_queue_update_write_ptr(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003832 struct iwl3945_tx_queue *txq)
Zhu Yib481de92007-09-25 17:54:57 -07003833{
3834 u32 reg = 0;
3835 int rc = 0;
3836 int txq_id = txq->q.id;
3837
3838 if (txq->need_update == 0)
3839 return rc;
3840
3841 /* if we're trying to save power */
3842 if (test_bit(STATUS_POWER_PMI, &priv->status)) {
3843 /* wake up nic if it's powered down ...
3844 * uCode will wake up, and interrupt us again, so next
3845 * time we'll skip this part. */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003846 reg = iwl_read32(priv, CSR_UCODE_DRV_GP1);
Zhu Yib481de92007-09-25 17:54:57 -07003847
3848 if (reg & CSR_UCODE_DRV_GP1_BIT_MAC_SLEEP) {
3849 IWL_DEBUG_INFO("Requesting wakeup, GP1 = 0x%x\n", reg);
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003850 iwl_set_bit(priv, CSR_GP_CNTRL,
Zhu Yib481de92007-09-25 17:54:57 -07003851 CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
3852 return rc;
3853 }
3854
3855 /* restore this queue's parameters in nic hardware. */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003856 rc = iwl_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003857 if (rc)
3858 return rc;
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003859 iwl_write_direct32(priv, HBUS_TARG_WRPTR,
Tomas Winklerfc4b6852007-10-25 17:15:24 +08003860 txq->q.write_ptr | (txq_id << 8));
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003861 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003862
3863 /* else not in power-save mode, uCode will never sleep when we're
3864 * trying to tx (during RFKILL, we're not trying to tx). */
3865 } else
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003866 iwl_write32(priv, HBUS_TARG_WRPTR,
Tomas Winklerfc4b6852007-10-25 17:15:24 +08003867 txq->q.write_ptr | (txq_id << 8));
Zhu Yib481de92007-09-25 17:54:57 -07003868
3869 txq->need_update = 0;
3870
3871 return rc;
3872}
3873
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08003874#ifdef CONFIG_IWL3945_DEBUG
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003875static void iwl3945_print_rx_config_cmd(struct iwl_priv *priv,
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08003876 struct iwl3945_rxon_cmd *rxon)
Zhu Yib481de92007-09-25 17:54:57 -07003877{
3878 IWL_DEBUG_RADIO("RX CONFIG:\n");
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08003879 iwl_print_hex_dump(priv, IWL_DL_RADIO, (u8 *) rxon, sizeof(*rxon));
Zhu Yib481de92007-09-25 17:54:57 -07003880 IWL_DEBUG_RADIO("u16 channel: 0x%x\n", le16_to_cpu(rxon->channel));
3881 IWL_DEBUG_RADIO("u32 flags: 0x%08X\n", le32_to_cpu(rxon->flags));
3882 IWL_DEBUG_RADIO("u32 filter_flags: 0x%08x\n",
3883 le32_to_cpu(rxon->filter_flags));
3884 IWL_DEBUG_RADIO("u8 dev_type: 0x%x\n", rxon->dev_type);
3885 IWL_DEBUG_RADIO("u8 ofdm_basic_rates: 0x%02x\n",
3886 rxon->ofdm_basic_rates);
3887 IWL_DEBUG_RADIO("u8 cck_basic_rates: 0x%02x\n", rxon->cck_basic_rates);
Johannes Berge1749612008-10-27 15:59:26 -07003888 IWL_DEBUG_RADIO("u8[6] node_addr: %pM\n", rxon->node_addr);
3889 IWL_DEBUG_RADIO("u8[6] bssid_addr: %pM\n", rxon->bssid_addr);
Zhu Yib481de92007-09-25 17:54:57 -07003890 IWL_DEBUG_RADIO("u16 assoc_id: 0x%x\n", le16_to_cpu(rxon->assoc_id));
3891}
3892#endif
3893
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003894static void iwl3945_enable_interrupts(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003895{
3896 IWL_DEBUG_ISR("Enabling interrupts\n");
3897 set_bit(STATUS_INT_ENABLED, &priv->status);
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003898 iwl_write32(priv, CSR_INT_MASK, CSR_INI_SET_MASK);
Zhu Yib481de92007-09-25 17:54:57 -07003899}
3900
Mohamed Abbas0359fac2008-03-28 16:21:08 -07003901
3902/* call this function to flush any scheduled tasklet */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003903static inline void iwl_synchronize_irq(struct iwl_priv *priv)
Mohamed Abbas0359fac2008-03-28 16:21:08 -07003904{
Tomas Winklera96a27f2008-10-23 23:48:56 -07003905 /* wait to make sure we flush pending tasklet*/
Mohamed Abbas0359fac2008-03-28 16:21:08 -07003906 synchronize_irq(priv->pci_dev->irq);
3907 tasklet_kill(&priv->irq_tasklet);
3908}
3909
3910
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003911static inline void iwl3945_disable_interrupts(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003912{
3913 clear_bit(STATUS_INT_ENABLED, &priv->status);
3914
3915 /* disable interrupts from uCode/NIC to host */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003916 iwl_write32(priv, CSR_INT_MASK, 0x00000000);
Zhu Yib481de92007-09-25 17:54:57 -07003917
3918 /* acknowledge/clear/reset any interrupts still pending
3919 * from uCode or flow handler (Rx/Tx DMA) */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003920 iwl_write32(priv, CSR_INT, 0xffffffff);
3921 iwl_write32(priv, CSR_FH_INT_STATUS, 0xffffffff);
Zhu Yib481de92007-09-25 17:54:57 -07003922 IWL_DEBUG_ISR("Disabled interrupts\n");
3923}
3924
3925static const char *desc_lookup(int i)
3926{
3927 switch (i) {
3928 case 1:
3929 return "FAIL";
3930 case 2:
3931 return "BAD_PARAM";
3932 case 3:
3933 return "BAD_CHECKSUM";
3934 case 4:
3935 return "NMI_INTERRUPT";
3936 case 5:
3937 return "SYSASSERT";
3938 case 6:
3939 return "FATAL_ERROR";
3940 }
3941
3942 return "UNKNOWN";
3943}
3944
3945#define ERROR_START_OFFSET (1 * sizeof(u32))
3946#define ERROR_ELEM_SIZE (7 * sizeof(u32))
3947
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003948static void iwl3945_dump_nic_error_log(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003949{
3950 u32 i;
3951 u32 desc, time, count, base, data1;
3952 u32 blink1, blink2, ilink1, ilink2;
3953 int rc;
3954
3955 base = le32_to_cpu(priv->card_alive.error_event_table_ptr);
3956
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003957 if (!iwl3945_hw_valid_rtc_data_addr(base)) {
Zhu Yib481de92007-09-25 17:54:57 -07003958 IWL_ERROR("Not valid error log pointer 0x%08X\n", base);
3959 return;
3960 }
3961
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003962 rc = iwl_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003963 if (rc) {
Winkler, Tomas39aadf82008-12-19 10:37:32 +08003964 IWL_WARN(priv, "Can not read from adapter at this time.\n");
Zhu Yib481de92007-09-25 17:54:57 -07003965 return;
3966 }
3967
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003968 count = iwl_read_targ_mem(priv, base);
Zhu Yib481de92007-09-25 17:54:57 -07003969
3970 if (ERROR_START_OFFSET <= count * ERROR_ELEM_SIZE) {
3971 IWL_ERROR("Start IWL Error Log Dump:\n");
Tomas Winkler2acae162008-03-02 01:25:59 +02003972 IWL_ERROR("Status: 0x%08lX, count: %d\n", priv->status, count);
Zhu Yib481de92007-09-25 17:54:57 -07003973 }
3974
3975 IWL_ERROR("Desc Time asrtPC blink2 "
3976 "ilink1 nmiPC Line\n");
3977 for (i = ERROR_START_OFFSET;
3978 i < (count * ERROR_ELEM_SIZE) + ERROR_START_OFFSET;
3979 i += ERROR_ELEM_SIZE) {
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003980 desc = iwl_read_targ_mem(priv, base + i);
Zhu Yib481de92007-09-25 17:54:57 -07003981 time =
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003982 iwl_read_targ_mem(priv, base + i + 1 * sizeof(u32));
Zhu Yib481de92007-09-25 17:54:57 -07003983 blink1 =
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003984 iwl_read_targ_mem(priv, base + i + 2 * sizeof(u32));
Zhu Yib481de92007-09-25 17:54:57 -07003985 blink2 =
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003986 iwl_read_targ_mem(priv, base + i + 3 * sizeof(u32));
Zhu Yib481de92007-09-25 17:54:57 -07003987 ilink1 =
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003988 iwl_read_targ_mem(priv, base + i + 4 * sizeof(u32));
Zhu Yib481de92007-09-25 17:54:57 -07003989 ilink2 =
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003990 iwl_read_targ_mem(priv, base + i + 5 * sizeof(u32));
Zhu Yib481de92007-09-25 17:54:57 -07003991 data1 =
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003992 iwl_read_targ_mem(priv, base + i + 6 * sizeof(u32));
Zhu Yib481de92007-09-25 17:54:57 -07003993
3994 IWL_ERROR
3995 ("%-13s (#%d) %010u 0x%05X 0x%05X 0x%05X 0x%05X %u\n\n",
3996 desc_lookup(desc), desc, time, blink1, blink2,
3997 ilink1, ilink2, data1);
3998 }
3999
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08004000 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004001
4002}
4003
Ben Cahillf58177b2007-11-29 11:09:43 +08004004#define EVENT_START_OFFSET (6 * sizeof(u32))
Zhu Yib481de92007-09-25 17:54:57 -07004005
4006/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004007 * iwl3945_print_event_log - Dump error event log to syslog
Zhu Yib481de92007-09-25 17:54:57 -07004008 *
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08004009 * NOTE: Must be called with iwl_grab_nic_access() already obtained!
Zhu Yib481de92007-09-25 17:54:57 -07004010 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08004011static void iwl3945_print_event_log(struct iwl_priv *priv, u32 start_idx,
Zhu Yib481de92007-09-25 17:54:57 -07004012 u32 num_events, u32 mode)
4013{
4014 u32 i;
4015 u32 base; /* SRAM byte address of event log header */
4016 u32 event_size; /* 2 u32s, or 3 u32s if timestamp recorded */
4017 u32 ptr; /* SRAM byte address of log data */
4018 u32 ev, time, data; /* event log data */
4019
4020 if (num_events == 0)
4021 return;
4022
4023 base = le32_to_cpu(priv->card_alive.log_event_table_ptr);
4024
4025 if (mode == 0)
4026 event_size = 2 * sizeof(u32);
4027 else
4028 event_size = 3 * sizeof(u32);
4029
4030 ptr = base + EVENT_START_OFFSET + (start_idx * event_size);
4031
4032 /* "time" is actually "data" for mode 0 (no timestamp).
4033 * place event id # at far right for easier visual parsing. */
4034 for (i = 0; i < num_events; i++) {
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08004035 ev = iwl_read_targ_mem(priv, ptr);
Zhu Yib481de92007-09-25 17:54:57 -07004036 ptr += sizeof(u32);
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08004037 time = iwl_read_targ_mem(priv, ptr);
Zhu Yib481de92007-09-25 17:54:57 -07004038 ptr += sizeof(u32);
4039 if (mode == 0)
4040 IWL_ERROR("0x%08x\t%04u\n", time, ev); /* data, ev */
4041 else {
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08004042 data = iwl_read_targ_mem(priv, ptr);
Zhu Yib481de92007-09-25 17:54:57 -07004043 ptr += sizeof(u32);
4044 IWL_ERROR("%010u\t0x%08x\t%04u\n", time, data, ev);
4045 }
4046 }
4047}
4048
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08004049static void iwl3945_dump_nic_event_log(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004050{
4051 int rc;
4052 u32 base; /* SRAM byte address of event log header */
4053 u32 capacity; /* event log capacity in # entries */
4054 u32 mode; /* 0 - no timestamp, 1 - timestamp recorded */
4055 u32 num_wraps; /* # times uCode wrapped to top of log */
4056 u32 next_entry; /* index of next entry to be written by uCode */
4057 u32 size; /* # entries that we'll print */
4058
4059 base = le32_to_cpu(priv->card_alive.log_event_table_ptr);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004060 if (!iwl3945_hw_valid_rtc_data_addr(base)) {
Zhu Yib481de92007-09-25 17:54:57 -07004061 IWL_ERROR("Invalid event log pointer 0x%08X\n", base);
4062 return;
4063 }
4064
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08004065 rc = iwl_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004066 if (rc) {
Winkler, Tomas39aadf82008-12-19 10:37:32 +08004067 IWL_WARN(priv, "Can not read from adapter at this time.\n");
Zhu Yib481de92007-09-25 17:54:57 -07004068 return;
4069 }
4070
4071 /* event log header */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08004072 capacity = iwl_read_targ_mem(priv, base);
4073 mode = iwl_read_targ_mem(priv, base + (1 * sizeof(u32)));
4074 num_wraps = iwl_read_targ_mem(priv, base + (2 * sizeof(u32)));
4075 next_entry = iwl_read_targ_mem(priv, base + (3 * sizeof(u32)));
Zhu Yib481de92007-09-25 17:54:57 -07004076
4077 size = num_wraps ? capacity : next_entry;
4078
4079 /* bail out if nothing in log */
4080 if (size == 0) {
Zhu Yi583fab32007-09-27 11:27:30 +08004081 IWL_ERROR("Start IWL Event Log Dump: nothing in log\n");
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08004082 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004083 return;
4084 }
4085
Zhu Yi583fab32007-09-27 11:27:30 +08004086 IWL_ERROR("Start IWL Event Log Dump: display count %d, wraps %d\n",
Zhu Yib481de92007-09-25 17:54:57 -07004087 size, num_wraps);
4088
4089 /* if uCode has wrapped back to top of log, start at the oldest entry,
4090 * i.e the next one that uCode would fill. */
4091 if (num_wraps)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004092 iwl3945_print_event_log(priv, next_entry,
Zhu Yib481de92007-09-25 17:54:57 -07004093 capacity - next_entry, mode);
4094
4095 /* (then/else) start at top of log */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004096 iwl3945_print_event_log(priv, 0, next_entry, mode);
Zhu Yib481de92007-09-25 17:54:57 -07004097
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08004098 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004099}
4100
4101/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004102 * iwl3945_irq_handle_error - called for HW or SW error interrupt from card
Zhu Yib481de92007-09-25 17:54:57 -07004103 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08004104static void iwl3945_irq_handle_error(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004105{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004106 /* Set the FW error flag -- cleared on iwl3945_down */
Zhu Yib481de92007-09-25 17:54:57 -07004107 set_bit(STATUS_FW_ERROR, &priv->status);
4108
4109 /* Cancel currently queued command. */
4110 clear_bit(STATUS_HCMD_ACTIVE, &priv->status);
4111
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08004112#ifdef CONFIG_IWL3945_DEBUG
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08004113 if (priv->debug_level & IWL_DL_FW_ERRORS) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004114 iwl3945_dump_nic_error_log(priv);
4115 iwl3945_dump_nic_event_log(priv);
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08004116 iwl3945_print_rx_config_cmd(priv, &priv->staging39_rxon);
Zhu Yib481de92007-09-25 17:54:57 -07004117 }
4118#endif
4119
4120 wake_up_interruptible(&priv->wait_command_queue);
4121
4122 /* Keep the restart process from trying to send host
4123 * commands by clearing the INIT status bit */
4124 clear_bit(STATUS_READY, &priv->status);
4125
4126 if (!test_bit(STATUS_EXIT_PENDING, &priv->status)) {
4127 IWL_DEBUG(IWL_DL_INFO | IWL_DL_FW_ERRORS,
4128 "Restarting adapter due to uCode error.\n");
4129
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004130 if (iwl3945_is_associated(priv)) {
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08004131 memcpy(&priv->recovery39_rxon, &priv->active39_rxon,
4132 sizeof(priv->recovery39_rxon));
Zhu Yib481de92007-09-25 17:54:57 -07004133 priv->error_recovering = 1;
4134 }
4135 queue_work(priv->workqueue, &priv->restart);
4136 }
4137}
4138
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08004139static void iwl3945_error_recovery(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004140{
4141 unsigned long flags;
4142
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08004143 memcpy(&priv->staging39_rxon, &priv->recovery39_rxon,
4144 sizeof(priv->staging39_rxon));
4145 priv->staging39_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004146 iwl3945_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004147
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004148 iwl3945_add_station(priv, priv->bssid, 1, 0);
Zhu Yib481de92007-09-25 17:54:57 -07004149
4150 spin_lock_irqsave(&priv->lock, flags);
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08004151 priv->assoc_id = le16_to_cpu(priv->staging39_rxon.assoc_id);
Zhu Yib481de92007-09-25 17:54:57 -07004152 priv->error_recovering = 0;
4153 spin_unlock_irqrestore(&priv->lock, flags);
4154}
4155
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08004156static void iwl3945_irq_tasklet(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004157{
4158 u32 inta, handled = 0;
4159 u32 inta_fh;
4160 unsigned long flags;
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08004161#ifdef CONFIG_IWL3945_DEBUG
Zhu Yib481de92007-09-25 17:54:57 -07004162 u32 inta_mask;
4163#endif
4164
4165 spin_lock_irqsave(&priv->lock, flags);
4166
4167 /* Ack/clear/reset pending uCode interrupts.
4168 * Note: Some bits in CSR_INT are "OR" of bits in CSR_FH_INT_STATUS,
4169 * and will clear only when CSR_FH_INT_STATUS gets cleared. */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08004170 inta = iwl_read32(priv, CSR_INT);
4171 iwl_write32(priv, CSR_INT, inta);
Zhu Yib481de92007-09-25 17:54:57 -07004172
4173 /* Ack/clear/reset pending flow-handler (DMA) interrupts.
4174 * Any new interrupts that happen after this, either while we're
4175 * in this tasklet, or later, will show up in next ISR/tasklet. */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08004176 inta_fh = iwl_read32(priv, CSR_FH_INT_STATUS);
4177 iwl_write32(priv, CSR_FH_INT_STATUS, inta_fh);
Zhu Yib481de92007-09-25 17:54:57 -07004178
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08004179#ifdef CONFIG_IWL3945_DEBUG
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08004180 if (priv->debug_level & IWL_DL_ISR) {
Ben Cahill9fbab512007-11-29 11:09:47 +08004181 /* just for debug */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08004182 inta_mask = iwl_read32(priv, CSR_INT_MASK);
Zhu Yib481de92007-09-25 17:54:57 -07004183 IWL_DEBUG_ISR("inta 0x%08x, enabled 0x%08x, fh 0x%08x\n",
4184 inta, inta_mask, inta_fh);
4185 }
4186#endif
4187
4188 /* Since CSR_INT and CSR_FH_INT_STATUS reads and clears are not
4189 * atomic, make sure that inta covers all the interrupts that
4190 * we've discovered, even if FH interrupt came in just after
4191 * reading CSR_INT. */
Tomas Winkler6f83eaa2008-03-04 18:09:28 -08004192 if (inta_fh & CSR39_FH_INT_RX_MASK)
Zhu Yib481de92007-09-25 17:54:57 -07004193 inta |= CSR_INT_BIT_FH_RX;
Tomas Winkler6f83eaa2008-03-04 18:09:28 -08004194 if (inta_fh & CSR39_FH_INT_TX_MASK)
Zhu Yib481de92007-09-25 17:54:57 -07004195 inta |= CSR_INT_BIT_FH_TX;
4196
4197 /* Now service all interrupt bits discovered above. */
4198 if (inta & CSR_INT_BIT_HW_ERR) {
4199 IWL_ERROR("Microcode HW error detected. Restarting.\n");
4200
4201 /* Tell the device to stop sending interrupts */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004202 iwl3945_disable_interrupts(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004203
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004204 iwl3945_irq_handle_error(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004205
4206 handled |= CSR_INT_BIT_HW_ERR;
4207
4208 spin_unlock_irqrestore(&priv->lock, flags);
4209
4210 return;
4211 }
4212
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08004213#ifdef CONFIG_IWL3945_DEBUG
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08004214 if (priv->debug_level & (IWL_DL_ISR)) {
Zhu Yib481de92007-09-25 17:54:57 -07004215 /* NIC fires this, but we don't use it, redundant with WAKEUP */
Joonwoo Park25c03d82008-01-23 10:15:20 -08004216 if (inta & CSR_INT_BIT_SCD)
4217 IWL_DEBUG_ISR("Scheduler finished to transmit "
4218 "the frame/frames.\n");
Zhu Yib481de92007-09-25 17:54:57 -07004219
4220 /* Alive notification via Rx interrupt will do the real work */
4221 if (inta & CSR_INT_BIT_ALIVE)
4222 IWL_DEBUG_ISR("Alive interrupt\n");
4223 }
4224#endif
4225 /* Safely ignore these bits for debug checks below */
Joonwoo Park25c03d82008-01-23 10:15:20 -08004226 inta &= ~(CSR_INT_BIT_SCD | CSR_INT_BIT_ALIVE);
Zhu Yib481de92007-09-25 17:54:57 -07004227
Zhu Yib481de92007-09-25 17:54:57 -07004228 /* Error detected by uCode */
4229 if (inta & CSR_INT_BIT_SW_ERR) {
4230 IWL_ERROR("Microcode SW error detected. Restarting 0x%X.\n",
4231 inta);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004232 iwl3945_irq_handle_error(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004233 handled |= CSR_INT_BIT_SW_ERR;
4234 }
4235
4236 /* uCode wakes up after power-down sleep */
4237 if (inta & CSR_INT_BIT_WAKEUP) {
4238 IWL_DEBUG_ISR("Wakeup interrupt\n");
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004239 iwl3945_rx_queue_update_write_ptr(priv, &priv->rxq);
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08004240 iwl3945_tx_queue_update_write_ptr(priv, &priv->txq39[0]);
4241 iwl3945_tx_queue_update_write_ptr(priv, &priv->txq39[1]);
4242 iwl3945_tx_queue_update_write_ptr(priv, &priv->txq39[2]);
4243 iwl3945_tx_queue_update_write_ptr(priv, &priv->txq39[3]);
4244 iwl3945_tx_queue_update_write_ptr(priv, &priv->txq39[4]);
4245 iwl3945_tx_queue_update_write_ptr(priv, &priv->txq39[5]);
Zhu Yib481de92007-09-25 17:54:57 -07004246
4247 handled |= CSR_INT_BIT_WAKEUP;
4248 }
4249
4250 /* All uCode command responses, including Tx command responses,
4251 * Rx "responses" (frame-received notification), and other
4252 * notifications from uCode come through here*/
4253 if (inta & (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX)) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004254 iwl3945_rx_handle(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004255 handled |= (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX);
4256 }
4257
4258 if (inta & CSR_INT_BIT_FH_TX) {
4259 IWL_DEBUG_ISR("Tx interrupt\n");
4260
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08004261 iwl_write32(priv, CSR_FH_INT_STATUS, (1 << 6));
4262 if (!iwl_grab_nic_access(priv)) {
4263 iwl_write_direct32(priv, FH39_TCSR_CREDIT
Tomas Winklerbddadf82008-12-19 10:37:01 +08004264 (FH39_SRVC_CHNL), 0x0);
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08004265 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004266 }
4267 handled |= CSR_INT_BIT_FH_TX;
4268 }
4269
4270 if (inta & ~handled)
4271 IWL_ERROR("Unhandled INTA bits 0x%08x\n", inta & ~handled);
4272
4273 if (inta & ~CSR_INI_SET_MASK) {
Winkler, Tomas39aadf82008-12-19 10:37:32 +08004274 IWL_WARN(priv, "Disabled INTA bits 0x%08x were pending\n",
Zhu Yib481de92007-09-25 17:54:57 -07004275 inta & ~CSR_INI_SET_MASK);
Winkler, Tomas39aadf82008-12-19 10:37:32 +08004276 IWL_WARN(priv, " with FH_INT = 0x%08x\n", inta_fh);
Zhu Yib481de92007-09-25 17:54:57 -07004277 }
4278
4279 /* Re-enable all interrupts */
Mohamed Abbas0359fac2008-03-28 16:21:08 -07004280 /* only Re-enable if disabled by irq */
4281 if (test_bit(STATUS_INT_ENABLED, &priv->status))
4282 iwl3945_enable_interrupts(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004283
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08004284#ifdef CONFIG_IWL3945_DEBUG
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08004285 if (priv->debug_level & (IWL_DL_ISR)) {
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08004286 inta = iwl_read32(priv, CSR_INT);
4287 inta_mask = iwl_read32(priv, CSR_INT_MASK);
4288 inta_fh = iwl_read32(priv, CSR_FH_INT_STATUS);
Zhu Yib481de92007-09-25 17:54:57 -07004289 IWL_DEBUG_ISR("End inta 0x%08x, enabled 0x%08x, fh 0x%08x, "
4290 "flags 0x%08lx\n", inta, inta_mask, inta_fh, flags);
4291 }
4292#endif
4293 spin_unlock_irqrestore(&priv->lock, flags);
4294}
4295
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004296static irqreturn_t iwl3945_isr(int irq, void *data)
Zhu Yib481de92007-09-25 17:54:57 -07004297{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08004298 struct iwl_priv *priv = data;
Zhu Yib481de92007-09-25 17:54:57 -07004299 u32 inta, inta_mask;
4300 u32 inta_fh;
4301 if (!priv)
4302 return IRQ_NONE;
4303
4304 spin_lock(&priv->lock);
4305
4306 /* Disable (but don't clear!) interrupts here to avoid
4307 * back-to-back ISRs and sporadic interrupts from our NIC.
4308 * If we have something to service, the tasklet will re-enable ints.
4309 * If we *don't* have something, we'll re-enable before leaving here. */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08004310 inta_mask = iwl_read32(priv, CSR_INT_MASK); /* just for debug */
4311 iwl_write32(priv, CSR_INT_MASK, 0x00000000);
Zhu Yib481de92007-09-25 17:54:57 -07004312
4313 /* Discover which interrupts are active/pending */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08004314 inta = iwl_read32(priv, CSR_INT);
4315 inta_fh = iwl_read32(priv, CSR_FH_INT_STATUS);
Zhu Yib481de92007-09-25 17:54:57 -07004316
4317 /* Ignore interrupt if there's nothing in NIC to service.
4318 * This may be due to IRQ shared with another device,
4319 * or due to sporadic interrupts thrown from our NIC. */
4320 if (!inta && !inta_fh) {
4321 IWL_DEBUG_ISR("Ignore interrupt, inta == 0, inta_fh == 0\n");
4322 goto none;
4323 }
4324
4325 if ((inta == 0xFFFFFFFF) || ((inta & 0xFFFFFFF0) == 0xa5a5a5a0)) {
4326 /* Hardware disappeared */
Winkler, Tomas39aadf82008-12-19 10:37:32 +08004327 IWL_WARN(priv, "HARDWARE GONE?? INTA == 0x%08x\n", inta);
Oliver Neukumcb4da1a2007-11-13 21:10:32 -08004328 goto unplugged;
Zhu Yib481de92007-09-25 17:54:57 -07004329 }
4330
4331 IWL_DEBUG_ISR("ISR inta 0x%08x, enabled 0x%08x, fh 0x%08x\n",
4332 inta, inta_mask, inta_fh);
4333
Joonwoo Park25c03d82008-01-23 10:15:20 -08004334 inta &= ~CSR_INT_BIT_SCD;
4335
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004336 /* iwl3945_irq_tasklet() will service interrupts and re-enable them */
Joonwoo Park25c03d82008-01-23 10:15:20 -08004337 if (likely(inta || inta_fh))
4338 tasklet_schedule(&priv->irq_tasklet);
Oliver Neukumcb4da1a2007-11-13 21:10:32 -08004339unplugged:
Zhu Yib481de92007-09-25 17:54:57 -07004340 spin_unlock(&priv->lock);
4341
4342 return IRQ_HANDLED;
4343
4344 none:
4345 /* re-enable interrupts here since we don't have anything to service. */
Mohamed Abbas0359fac2008-03-28 16:21:08 -07004346 /* only Re-enable if disabled by irq */
4347 if (test_bit(STATUS_INT_ENABLED, &priv->status))
4348 iwl3945_enable_interrupts(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004349 spin_unlock(&priv->lock);
4350 return IRQ_NONE;
4351}
4352
4353/************************** EEPROM BANDS ****************************
4354 *
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004355 * The iwl3945_eeprom_band definitions below provide the mapping from the
Zhu Yib481de92007-09-25 17:54:57 -07004356 * EEPROM contents to the specific channel number supported for each
4357 * band.
4358 *
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08004359 * For example, iwl3945_priv->eeprom39.band_3_channels[4] from the band_3
Zhu Yib481de92007-09-25 17:54:57 -07004360 * definition below maps to physical channel 42 in the 5.2GHz spectrum.
4361 * The specific geography and calibration information for that channel
4362 * is contained in the eeprom map itself.
4363 *
4364 * During init, we copy the eeprom information and channel map
4365 * information into priv->channel_info_24/52 and priv->channel_map_24/52
4366 *
4367 * channel_map_24/52 provides the index in the channel_info array for a
4368 * given channel. We have to have two separate maps as there is channel
4369 * overlap with the 2.4GHz and 5.2GHz spectrum as seen in band_1 and
4370 * band_2
4371 *
4372 * A value of 0xff stored in the channel_map indicates that the channel
4373 * is not supported by the hardware at all.
4374 *
4375 * A value of 0xfe in the channel_map indicates that the channel is not
4376 * valid for Tx with the current hardware. This means that
4377 * while the system can tune and receive on a given channel, it may not
4378 * be able to associate or transmit any frames on that
4379 * channel. There is no corresponding channel information for that
4380 * entry.
4381 *
4382 *********************************************************************/
4383
4384/* 2.4 GHz */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004385static const u8 iwl3945_eeprom_band_1[14] = {
Zhu Yib481de92007-09-25 17:54:57 -07004386 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14
4387};
4388
4389/* 5.2 GHz bands */
Ben Cahill9fbab512007-11-29 11:09:47 +08004390static const u8 iwl3945_eeprom_band_2[] = { /* 4915-5080MHz */
Zhu Yib481de92007-09-25 17:54:57 -07004391 183, 184, 185, 187, 188, 189, 192, 196, 7, 8, 11, 12, 16
4392};
4393
Ben Cahill9fbab512007-11-29 11:09:47 +08004394static const u8 iwl3945_eeprom_band_3[] = { /* 5170-5320MHz */
Zhu Yib481de92007-09-25 17:54:57 -07004395 34, 36, 38, 40, 42, 44, 46, 48, 52, 56, 60, 64
4396};
4397
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004398static const u8 iwl3945_eeprom_band_4[] = { /* 5500-5700MHz */
Zhu Yib481de92007-09-25 17:54:57 -07004399 100, 104, 108, 112, 116, 120, 124, 128, 132, 136, 140
4400};
4401
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004402static const u8 iwl3945_eeprom_band_5[] = { /* 5725-5825MHz */
Zhu Yib481de92007-09-25 17:54:57 -07004403 145, 149, 153, 157, 161, 165
4404};
4405
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08004406static void iwl3945_init_band_reference(const struct iwl_priv *priv, int band,
Zhu Yib481de92007-09-25 17:54:57 -07004407 int *eeprom_ch_count,
Samuel Ortiz0f741d92008-12-19 10:37:10 +08004408 const struct iwl_eeprom_channel
Zhu Yib481de92007-09-25 17:54:57 -07004409 **eeprom_ch_info,
4410 const u8 **eeprom_ch_index)
4411{
4412 switch (band) {
4413 case 1: /* 2.4GHz band */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004414 *eeprom_ch_count = ARRAY_SIZE(iwl3945_eeprom_band_1);
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08004415 *eeprom_ch_info = priv->eeprom39.band_1_channels;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004416 *eeprom_ch_index = iwl3945_eeprom_band_1;
Zhu Yib481de92007-09-25 17:54:57 -07004417 break;
Ben Cahill9fbab512007-11-29 11:09:47 +08004418 case 2: /* 4.9GHz band */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004419 *eeprom_ch_count = ARRAY_SIZE(iwl3945_eeprom_band_2);
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08004420 *eeprom_ch_info = priv->eeprom39.band_2_channels;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004421 *eeprom_ch_index = iwl3945_eeprom_band_2;
Zhu Yib481de92007-09-25 17:54:57 -07004422 break;
4423 case 3: /* 5.2GHz band */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004424 *eeprom_ch_count = ARRAY_SIZE(iwl3945_eeprom_band_3);
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08004425 *eeprom_ch_info = priv->eeprom39.band_3_channels;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004426 *eeprom_ch_index = iwl3945_eeprom_band_3;
Zhu Yib481de92007-09-25 17:54:57 -07004427 break;
Ben Cahill9fbab512007-11-29 11:09:47 +08004428 case 4: /* 5.5GHz band */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004429 *eeprom_ch_count = ARRAY_SIZE(iwl3945_eeprom_band_4);
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08004430 *eeprom_ch_info = priv->eeprom39.band_4_channels;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004431 *eeprom_ch_index = iwl3945_eeprom_band_4;
Zhu Yib481de92007-09-25 17:54:57 -07004432 break;
Ben Cahill9fbab512007-11-29 11:09:47 +08004433 case 5: /* 5.7GHz band */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004434 *eeprom_ch_count = ARRAY_SIZE(iwl3945_eeprom_band_5);
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08004435 *eeprom_ch_info = priv->eeprom39.band_5_channels;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004436 *eeprom_ch_index = iwl3945_eeprom_band_5;
Zhu Yib481de92007-09-25 17:54:57 -07004437 break;
4438 default:
4439 BUG();
4440 return;
4441 }
4442}
4443
Cahill, Ben M6440adb2007-11-29 11:09:55 +08004444/**
4445 * iwl3945_get_channel_info - Find driver's private channel info
4446 *
4447 * Based on band and channel number.
4448 */
Samuel Ortizd20b3c62008-12-19 10:37:15 +08004449const struct iwl_channel_info *
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08004450iwl3945_get_channel_info(const struct iwl_priv *priv,
Samuel Ortizd20b3c62008-12-19 10:37:15 +08004451 enum ieee80211_band band, u16 channel)
Zhu Yib481de92007-09-25 17:54:57 -07004452{
4453 int i;
4454
Johannes Berg8318d782008-01-24 19:38:38 +01004455 switch (band) {
4456 case IEEE80211_BAND_5GHZ:
Zhu Yib481de92007-09-25 17:54:57 -07004457 for (i = 14; i < priv->channel_count; i++) {
4458 if (priv->channel_info[i].channel == channel)
4459 return &priv->channel_info[i];
4460 }
4461 break;
4462
Johannes Berg8318d782008-01-24 19:38:38 +01004463 case IEEE80211_BAND_2GHZ:
Zhu Yib481de92007-09-25 17:54:57 -07004464 if (channel >= 1 && channel <= 14)
4465 return &priv->channel_info[channel - 1];
4466 break;
Johannes Berg8318d782008-01-24 19:38:38 +01004467 case IEEE80211_NUM_BANDS:
4468 WARN_ON(1);
Zhu Yib481de92007-09-25 17:54:57 -07004469 }
4470
4471 return NULL;
4472}
4473
4474#define CHECK_AND_PRINT(x) ((eeprom_ch_info[ch].flags & EEPROM_CHANNEL_##x) \
4475 ? # x " " : "")
4476
Cahill, Ben M6440adb2007-11-29 11:09:55 +08004477/**
4478 * iwl3945_init_channel_map - Set up driver's info for all possible channels
4479 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08004480static int iwl3945_init_channel_map(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004481{
4482 int eeprom_ch_count = 0;
4483 const u8 *eeprom_ch_index = NULL;
Samuel Ortiz0f741d92008-12-19 10:37:10 +08004484 const struct iwl_eeprom_channel *eeprom_ch_info = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07004485 int band, ch;
Samuel Ortizd20b3c62008-12-19 10:37:15 +08004486 struct iwl_channel_info *ch_info;
Zhu Yib481de92007-09-25 17:54:57 -07004487
4488 if (priv->channel_count) {
4489 IWL_DEBUG_INFO("Channel map already initialized.\n");
4490 return 0;
4491 }
4492
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08004493 if (priv->eeprom39.version < 0x2f) {
Winkler, Tomas39aadf82008-12-19 10:37:32 +08004494 IWL_WARN(priv, "Unsupported EEPROM version: 0x%04X\n",
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08004495 priv->eeprom39.version);
Zhu Yib481de92007-09-25 17:54:57 -07004496 return -EINVAL;
4497 }
4498
4499 IWL_DEBUG_INFO("Initializing regulatory info from EEPROM\n");
4500
4501 priv->channel_count =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004502 ARRAY_SIZE(iwl3945_eeprom_band_1) +
4503 ARRAY_SIZE(iwl3945_eeprom_band_2) +
4504 ARRAY_SIZE(iwl3945_eeprom_band_3) +
4505 ARRAY_SIZE(iwl3945_eeprom_band_4) +
4506 ARRAY_SIZE(iwl3945_eeprom_band_5);
Zhu Yib481de92007-09-25 17:54:57 -07004507
4508 IWL_DEBUG_INFO("Parsing data for %d channels.\n", priv->channel_count);
4509
Samuel Ortizd20b3c62008-12-19 10:37:15 +08004510 priv->channel_info = kzalloc(sizeof(struct iwl_channel_info) *
Zhu Yib481de92007-09-25 17:54:57 -07004511 priv->channel_count, GFP_KERNEL);
4512 if (!priv->channel_info) {
4513 IWL_ERROR("Could not allocate channel_info\n");
4514 priv->channel_count = 0;
4515 return -ENOMEM;
4516 }
4517
4518 ch_info = priv->channel_info;
4519
4520 /* Loop through the 5 EEPROM bands adding them in order to the
4521 * channel map we maintain (that contains additional information than
4522 * what just in the EEPROM) */
4523 for (band = 1; band <= 5; band++) {
4524
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004525 iwl3945_init_band_reference(priv, band, &eeprom_ch_count,
Zhu Yib481de92007-09-25 17:54:57 -07004526 &eeprom_ch_info, &eeprom_ch_index);
4527
4528 /* Loop through each band adding each of the channels */
4529 for (ch = 0; ch < eeprom_ch_count; ch++) {
4530 ch_info->channel = eeprom_ch_index[ch];
Johannes Berg8318d782008-01-24 19:38:38 +01004531 ch_info->band = (band == 1) ? IEEE80211_BAND_2GHZ :
4532 IEEE80211_BAND_5GHZ;
Zhu Yib481de92007-09-25 17:54:57 -07004533
4534 /* permanently store EEPROM's channel regulatory flags
4535 * and max power in channel info database. */
4536 ch_info->eeprom = eeprom_ch_info[ch];
4537
4538 /* Copy the run-time flags so they are there even on
4539 * invalid channels */
4540 ch_info->flags = eeprom_ch_info[ch].flags;
4541
4542 if (!(is_channel_valid(ch_info))) {
4543 IWL_DEBUG_INFO("Ch. %d Flags %x [%sGHz] - "
4544 "No traffic\n",
4545 ch_info->channel,
4546 ch_info->flags,
4547 is_channel_a_band(ch_info) ?
4548 "5.2" : "2.4");
4549 ch_info++;
4550 continue;
4551 }
4552
4553 /* Initialize regulatory-based run-time data */
4554 ch_info->max_power_avg = ch_info->curr_txpow =
4555 eeprom_ch_info[ch].max_power_avg;
4556 ch_info->scan_power = eeprom_ch_info[ch].max_power_avg;
4557 ch_info->min_power = 0;
4558
Guy Cohenfe7c4042008-04-21 15:41:56 -07004559 IWL_DEBUG_INFO("Ch. %d [%sGHz] %s%s%s%s%s%s(0x%02x"
Zhu Yib481de92007-09-25 17:54:57 -07004560 " %ddBm): Ad-Hoc %ssupported\n",
4561 ch_info->channel,
4562 is_channel_a_band(ch_info) ?
4563 "5.2" : "2.4",
Tomas Winkler8211ef72008-03-02 01:36:04 +02004564 CHECK_AND_PRINT(VALID),
Zhu Yib481de92007-09-25 17:54:57 -07004565 CHECK_AND_PRINT(IBSS),
4566 CHECK_AND_PRINT(ACTIVE),
4567 CHECK_AND_PRINT(RADAR),
4568 CHECK_AND_PRINT(WIDE),
Zhu Yib481de92007-09-25 17:54:57 -07004569 CHECK_AND_PRINT(DFS),
4570 eeprom_ch_info[ch].flags,
4571 eeprom_ch_info[ch].max_power_avg,
4572 ((eeprom_ch_info[ch].
4573 flags & EEPROM_CHANNEL_IBSS)
4574 && !(eeprom_ch_info[ch].
4575 flags & EEPROM_CHANNEL_RADAR))
4576 ? "" : "not ");
4577
4578 /* Set the user_txpower_limit to the highest power
4579 * supported by any channel */
4580 if (eeprom_ch_info[ch].max_power_avg >
4581 priv->user_txpower_limit)
4582 priv->user_txpower_limit =
4583 eeprom_ch_info[ch].max_power_avg;
4584
4585 ch_info++;
4586 }
4587 }
4588
Cahill, Ben M6440adb2007-11-29 11:09:55 +08004589 /* Set up txpower settings in driver for all channels */
Zhu Yib481de92007-09-25 17:54:57 -07004590 if (iwl3945_txpower_set_from_eeprom(priv))
4591 return -EIO;
4592
4593 return 0;
4594}
4595
Reinette Chatre849e0dc2008-01-23 10:15:18 -08004596/*
4597 * iwl3945_free_channel_map - undo allocations in iwl3945_init_channel_map
4598 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08004599static void iwl3945_free_channel_map(struct iwl_priv *priv)
Reinette Chatre849e0dc2008-01-23 10:15:18 -08004600{
4601 kfree(priv->channel_info);
4602 priv->channel_count = 0;
4603}
4604
Zhu Yib481de92007-09-25 17:54:57 -07004605/* For active scan, listen ACTIVE_DWELL_TIME (msec) on each channel after
4606 * sending probe req. This should be set long enough to hear probe responses
4607 * from more than one AP. */
Abhijeet Kolekarf9340522008-09-03 11:26:58 +08004608#define IWL_ACTIVE_DWELL_TIME_24 (30) /* all times in msec */
4609#define IWL_ACTIVE_DWELL_TIME_52 (20)
4610
4611#define IWL_ACTIVE_DWELL_FACTOR_24GHZ (3)
4612#define IWL_ACTIVE_DWELL_FACTOR_52GHZ (2)
Zhu Yib481de92007-09-25 17:54:57 -07004613
4614/* For faster active scanning, scan will move to the next channel if fewer than
4615 * PLCP_QUIET_THRESH packets are heard on this channel within
4616 * ACTIVE_QUIET_TIME after sending probe request. This shortens the dwell
4617 * time if it's a quiet channel (nothing responded to our probe, and there's
4618 * no other traffic).
4619 * Disable "quiet" feature by setting PLCP_QUIET_THRESH to 0. */
4620#define IWL_PLCP_QUIET_THRESH __constant_cpu_to_le16(1) /* packets */
Abhijeet Kolekarf9340522008-09-03 11:26:58 +08004621#define IWL_ACTIVE_QUIET_TIME __constant_cpu_to_le16(10) /* msec */
Zhu Yib481de92007-09-25 17:54:57 -07004622
4623/* For passive scan, listen PASSIVE_DWELL_TIME (msec) on each channel.
4624 * Must be set longer than active dwell time.
4625 * For the most reliable scan, set > AP beacon interval (typically 100msec). */
4626#define IWL_PASSIVE_DWELL_TIME_24 (20) /* all times in msec */
4627#define IWL_PASSIVE_DWELL_TIME_52 (10)
4628#define IWL_PASSIVE_DWELL_BASE (100)
4629#define IWL_CHANNEL_TUNE_TIME 5
4630
Kolekar, Abhijeete720ce92008-11-07 09:58:42 -08004631#define IWL_SCAN_PROBE_MASK(n) (BIT(n) | (BIT(n) - BIT(1)))
Abhijeet Kolekarf9340522008-09-03 11:26:58 +08004632
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08004633static inline u16 iwl3945_get_active_dwell_time(struct iwl_priv *priv,
Abhijeet Kolekarf9340522008-09-03 11:26:58 +08004634 enum ieee80211_band band,
4635 u8 n_probes)
Zhu Yib481de92007-09-25 17:54:57 -07004636{
Johannes Berg8318d782008-01-24 19:38:38 +01004637 if (band == IEEE80211_BAND_5GHZ)
Abhijeet Kolekarf9340522008-09-03 11:26:58 +08004638 return IWL_ACTIVE_DWELL_TIME_52 +
4639 IWL_ACTIVE_DWELL_FACTOR_52GHZ * (n_probes + 1);
Zhu Yib481de92007-09-25 17:54:57 -07004640 else
Abhijeet Kolekarf9340522008-09-03 11:26:58 +08004641 return IWL_ACTIVE_DWELL_TIME_24 +
4642 IWL_ACTIVE_DWELL_FACTOR_24GHZ * (n_probes + 1);
Zhu Yib481de92007-09-25 17:54:57 -07004643}
4644
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08004645static u16 iwl3945_get_passive_dwell_time(struct iwl_priv *priv,
Johannes Berg8318d782008-01-24 19:38:38 +01004646 enum ieee80211_band band)
Zhu Yib481de92007-09-25 17:54:57 -07004647{
Johannes Berg8318d782008-01-24 19:38:38 +01004648 u16 passive = (band == IEEE80211_BAND_2GHZ) ?
Zhu Yib481de92007-09-25 17:54:57 -07004649 IWL_PASSIVE_DWELL_BASE + IWL_PASSIVE_DWELL_TIME_24 :
4650 IWL_PASSIVE_DWELL_BASE + IWL_PASSIVE_DWELL_TIME_52;
4651
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004652 if (iwl3945_is_associated(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07004653 /* If we're associated, we clamp the maximum passive
4654 * dwell time to be 98% of the beacon interval (minus
4655 * 2 * channel tune time) */
4656 passive = priv->beacon_int;
4657 if ((passive > IWL_PASSIVE_DWELL_BASE) || !passive)
4658 passive = IWL_PASSIVE_DWELL_BASE;
4659 passive = (passive * 98) / 100 - IWL_CHANNEL_TUNE_TIME * 2;
4660 }
4661
Zhu Yib481de92007-09-25 17:54:57 -07004662 return passive;
4663}
4664
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08004665static int iwl3945_get_channels_for_scan(struct iwl_priv *priv,
Johannes Berg8318d782008-01-24 19:38:38 +01004666 enum ieee80211_band band,
Abhijeet Kolekarf9340522008-09-03 11:26:58 +08004667 u8 is_active, u8 n_probes,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004668 struct iwl3945_scan_channel *scan_ch)
Zhu Yib481de92007-09-25 17:54:57 -07004669{
4670 const struct ieee80211_channel *channels = NULL;
Johannes Berg8318d782008-01-24 19:38:38 +01004671 const struct ieee80211_supported_band *sband;
Samuel Ortizd20b3c62008-12-19 10:37:15 +08004672 const struct iwl_channel_info *ch_info;
Zhu Yib481de92007-09-25 17:54:57 -07004673 u16 passive_dwell = 0;
4674 u16 active_dwell = 0;
4675 int added, i;
4676
Johannes Berg8318d782008-01-24 19:38:38 +01004677 sband = iwl3945_get_band(priv, band);
4678 if (!sband)
Zhu Yib481de92007-09-25 17:54:57 -07004679 return 0;
4680
Johannes Berg8318d782008-01-24 19:38:38 +01004681 channels = sband->channels;
Zhu Yib481de92007-09-25 17:54:57 -07004682
Abhijeet Kolekarf9340522008-09-03 11:26:58 +08004683 active_dwell = iwl3945_get_active_dwell_time(priv, band, n_probes);
Johannes Berg8318d782008-01-24 19:38:38 +01004684 passive_dwell = iwl3945_get_passive_dwell_time(priv, band);
Zhu Yib481de92007-09-25 17:54:57 -07004685
Abhijeet Kolekar8f4807a2008-09-03 11:26:31 +08004686 if (passive_dwell <= active_dwell)
4687 passive_dwell = active_dwell + 1;
4688
Johannes Berg8318d782008-01-24 19:38:38 +01004689 for (i = 0, added = 0; i < sband->n_channels; i++) {
Johannes Berg182e2e62008-04-04 10:41:56 +02004690 if (channels[i].flags & IEEE80211_CHAN_DISABLED)
4691 continue;
4692
Johannes Berg8318d782008-01-24 19:38:38 +01004693 scan_ch->channel = channels[i].hw_value;
Zhu Yib481de92007-09-25 17:54:57 -07004694
Johannes Berg8318d782008-01-24 19:38:38 +01004695 ch_info = iwl3945_get_channel_info(priv, band, scan_ch->channel);
Zhu Yib481de92007-09-25 17:54:57 -07004696 if (!is_channel_valid(ch_info)) {
Ron Rindjunsky66b50042008-06-25 16:46:31 +08004697 IWL_DEBUG_SCAN("Channel %d is INVALID for this band.\n",
Zhu Yib481de92007-09-25 17:54:57 -07004698 scan_ch->channel);
4699 continue;
4700 }
4701
Zhu Yib481de92007-09-25 17:54:57 -07004702 scan_ch->active_dwell = cpu_to_le16(active_dwell);
4703 scan_ch->passive_dwell = cpu_to_le16(passive_dwell);
Abhijeet Kolekar011a0332008-12-02 12:14:07 -08004704 /* If passive , set up for auto-switch
4705 * and use long active_dwell time.
4706 */
4707 if (!is_active || is_channel_passive(ch_info) ||
4708 (channels[i].flags & IEEE80211_CHAN_PASSIVE_SCAN)) {
4709 scan_ch->type = 0; /* passive */
4710 if (IWL_UCODE_API(priv->ucode_ver) == 1)
4711 scan_ch->active_dwell = cpu_to_le16(passive_dwell - 1);
4712 } else {
4713 scan_ch->type = 1; /* active */
4714 }
4715
4716 /* Set direct probe bits. These may be used both for active
4717 * scan channels (probes gets sent right away),
4718 * or for passive channels (probes get se sent only after
4719 * hearing clear Rx packet).*/
4720 if (IWL_UCODE_API(priv->ucode_ver) >= 2) {
4721 if (n_probes)
4722 scan_ch->type |= IWL_SCAN_PROBE_MASK(n_probes);
4723 } else {
4724 /* uCode v1 does not allow setting direct probe bits on
4725 * passive channel. */
4726 if ((scan_ch->type & 1) && n_probes)
4727 scan_ch->type |= IWL_SCAN_PROBE_MASK(n_probes);
4728 }
Zhu Yib481de92007-09-25 17:54:57 -07004729
Ben Cahill9fbab512007-11-29 11:09:47 +08004730 /* Set txpower levels to defaults */
Zhu Yib481de92007-09-25 17:54:57 -07004731 scan_ch->tpc.dsp_atten = 110;
4732 /* scan_pwr_info->tpc.dsp_atten; */
4733
4734 /*scan_pwr_info->tpc.tx_gain; */
Johannes Berg8318d782008-01-24 19:38:38 +01004735 if (band == IEEE80211_BAND_5GHZ)
Zhu Yib481de92007-09-25 17:54:57 -07004736 scan_ch->tpc.tx_gain = ((1 << 5) | (3 << 3)) | 3;
4737 else {
4738 scan_ch->tpc.tx_gain = ((1 << 5) | (5 << 3));
4739 /* NOTE: if we were doing 6Mb OFDM for scans we'd use
Ben Cahill9fbab512007-11-29 11:09:47 +08004740 * power level:
Reinette Chatre8a1b0242008-01-14 17:46:25 -08004741 * scan_ch->tpc.tx_gain = ((1 << 5) | (2 << 3)) | 3;
Zhu Yib481de92007-09-25 17:54:57 -07004742 */
4743 }
4744
4745 IWL_DEBUG_SCAN("Scanning %d [%s %d]\n",
4746 scan_ch->channel,
4747 (scan_ch->type & 1) ? "ACTIVE" : "PASSIVE",
4748 (scan_ch->type & 1) ?
4749 active_dwell : passive_dwell);
4750
4751 scan_ch++;
4752 added++;
4753 }
4754
4755 IWL_DEBUG_SCAN("total channels to scan %d \n", added);
4756 return added;
4757}
4758
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08004759static void iwl3945_init_hw_rates(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07004760 struct ieee80211_rate *rates)
4761{
4762 int i;
4763
4764 for (i = 0; i < IWL_RATE_COUNT; i++) {
Johannes Berg8318d782008-01-24 19:38:38 +01004765 rates[i].bitrate = iwl3945_rates[i].ieee * 5;
4766 rates[i].hw_value = i; /* Rate scaling will work on indexes */
4767 rates[i].hw_value_short = i;
4768 rates[i].flags = 0;
Samuel Ortizd9829a62008-12-19 10:37:12 +08004769 if ((i > IWL39_LAST_OFDM_RATE) || (i < IWL_FIRST_OFDM_RATE)) {
Zhu Yib481de92007-09-25 17:54:57 -07004770 /*
Johannes Berg8318d782008-01-24 19:38:38 +01004771 * If CCK != 1M then set short preamble rate flag.
Zhu Yib481de92007-09-25 17:54:57 -07004772 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004773 rates[i].flags |= (iwl3945_rates[i].plcp == 10) ?
Johannes Berg8318d782008-01-24 19:38:38 +01004774 0 : IEEE80211_RATE_SHORT_PREAMBLE;
Zhu Yib481de92007-09-25 17:54:57 -07004775 }
Zhu Yib481de92007-09-25 17:54:57 -07004776 }
4777}
4778
4779/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004780 * iwl3945_init_geos - Initialize mac80211's geo/channel info based from eeprom
Zhu Yib481de92007-09-25 17:54:57 -07004781 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08004782static int iwl3945_init_geos(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004783{
Samuel Ortizd20b3c62008-12-19 10:37:15 +08004784 struct iwl_channel_info *ch;
Tomas Winkler8211ef72008-03-02 01:36:04 +02004785 struct ieee80211_supported_band *sband;
Zhu Yib481de92007-09-25 17:54:57 -07004786 struct ieee80211_channel *channels;
4787 struct ieee80211_channel *geo_ch;
4788 struct ieee80211_rate *rates;
4789 int i = 0;
Zhu Yib481de92007-09-25 17:54:57 -07004790
Johannes Berg8318d782008-01-24 19:38:38 +01004791 if (priv->bands[IEEE80211_BAND_2GHZ].n_bitrates ||
4792 priv->bands[IEEE80211_BAND_5GHZ].n_bitrates) {
Zhu Yib481de92007-09-25 17:54:57 -07004793 IWL_DEBUG_INFO("Geography modes already initialized.\n");
4794 set_bit(STATUS_GEO_CONFIGURED, &priv->status);
4795 return 0;
4796 }
4797
Zhu Yib481de92007-09-25 17:54:57 -07004798 channels = kzalloc(sizeof(struct ieee80211_channel) *
4799 priv->channel_count, GFP_KERNEL);
Johannes Berg8318d782008-01-24 19:38:38 +01004800 if (!channels)
Zhu Yib481de92007-09-25 17:54:57 -07004801 return -ENOMEM;
Zhu Yib481de92007-09-25 17:54:57 -07004802
Tomas Winkler8211ef72008-03-02 01:36:04 +02004803 rates = kzalloc((sizeof(struct ieee80211_rate) * (IWL_RATE_COUNT + 1)),
Zhu Yib481de92007-09-25 17:54:57 -07004804 GFP_KERNEL);
4805 if (!rates) {
Zhu Yib481de92007-09-25 17:54:57 -07004806 kfree(channels);
4807 return -ENOMEM;
4808 }
4809
Zhu Yib481de92007-09-25 17:54:57 -07004810 /* 5.2GHz channels start after the 2.4GHz channels */
Tomas Winkler8211ef72008-03-02 01:36:04 +02004811 sband = &priv->bands[IEEE80211_BAND_5GHZ];
4812 sband->channels = &channels[ARRAY_SIZE(iwl3945_eeprom_band_1)];
4813 /* just OFDM */
4814 sband->bitrates = &rates[IWL_FIRST_OFDM_RATE];
4815 sband->n_bitrates = IWL_RATE_COUNT - IWL_FIRST_OFDM_RATE;
Zhu Yib481de92007-09-25 17:54:57 -07004816
Tomas Winkler8211ef72008-03-02 01:36:04 +02004817 sband = &priv->bands[IEEE80211_BAND_2GHZ];
4818 sband->channels = channels;
4819 /* OFDM & CCK */
4820 sband->bitrates = rates;
4821 sband->n_bitrates = IWL_RATE_COUNT;
Zhu Yib481de92007-09-25 17:54:57 -07004822
4823 priv->ieee_channels = channels;
4824 priv->ieee_rates = rates;
4825
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004826 iwl3945_init_hw_rates(priv, rates);
Zhu Yib481de92007-09-25 17:54:57 -07004827
Tomas Winkler8211ef72008-03-02 01:36:04 +02004828 for (i = 0; i < priv->channel_count; i++) {
Zhu Yib481de92007-09-25 17:54:57 -07004829 ch = &priv->channel_info[i];
4830
Tomas Winkler8211ef72008-03-02 01:36:04 +02004831 /* FIXME: might be removed if scan is OK*/
4832 if (!is_channel_valid(ch))
Zhu Yib481de92007-09-25 17:54:57 -07004833 continue;
Zhu Yib481de92007-09-25 17:54:57 -07004834
4835 if (is_channel_a_band(ch))
Tomas Winkler8211ef72008-03-02 01:36:04 +02004836 sband = &priv->bands[IEEE80211_BAND_5GHZ];
Johannes Berg8318d782008-01-24 19:38:38 +01004837 else
Tomas Winkler8211ef72008-03-02 01:36:04 +02004838 sband = &priv->bands[IEEE80211_BAND_2GHZ];
Zhu Yib481de92007-09-25 17:54:57 -07004839
Tomas Winkler8211ef72008-03-02 01:36:04 +02004840 geo_ch = &sband->channels[sband->n_channels++];
4841
4842 geo_ch->center_freq = ieee80211_channel_to_frequency(ch->channel);
Johannes Berg8318d782008-01-24 19:38:38 +01004843 geo_ch->max_power = ch->max_power_avg;
4844 geo_ch->max_antenna_gain = 0xff;
Mohamed Abbas7b723042008-01-31 21:46:40 -08004845 geo_ch->hw_value = ch->channel;
Zhu Yib481de92007-09-25 17:54:57 -07004846
4847 if (is_channel_valid(ch)) {
Johannes Berg8318d782008-01-24 19:38:38 +01004848 if (!(ch->flags & EEPROM_CHANNEL_IBSS))
4849 geo_ch->flags |= IEEE80211_CHAN_NO_IBSS;
Zhu Yib481de92007-09-25 17:54:57 -07004850
Johannes Berg8318d782008-01-24 19:38:38 +01004851 if (!(ch->flags & EEPROM_CHANNEL_ACTIVE))
4852 geo_ch->flags |= IEEE80211_CHAN_PASSIVE_SCAN;
Zhu Yib481de92007-09-25 17:54:57 -07004853
4854 if (ch->flags & EEPROM_CHANNEL_RADAR)
Johannes Berg8318d782008-01-24 19:38:38 +01004855 geo_ch->flags |= IEEE80211_CHAN_RADAR;
Zhu Yib481de92007-09-25 17:54:57 -07004856
4857 if (ch->max_power_avg > priv->max_channel_txpower_limit)
4858 priv->max_channel_txpower_limit =
4859 ch->max_power_avg;
Tomas Winkler8211ef72008-03-02 01:36:04 +02004860 } else {
Johannes Berg8318d782008-01-24 19:38:38 +01004861 geo_ch->flags |= IEEE80211_CHAN_DISABLED;
Tomas Winkler8211ef72008-03-02 01:36:04 +02004862 }
4863
4864 /* Save flags for reg domain usage */
4865 geo_ch->orig_flags = geo_ch->flags;
4866
4867 IWL_DEBUG_INFO("Channel %d Freq=%d[%sGHz] %s flag=0%X\n",
4868 ch->channel, geo_ch->center_freq,
4869 is_channel_a_band(ch) ? "5.2" : "2.4",
4870 geo_ch->flags & IEEE80211_CHAN_DISABLED ?
4871 "restricted" : "valid",
4872 geo_ch->flags);
Zhu Yib481de92007-09-25 17:54:57 -07004873 }
4874
Tomas Winkler82b9a122008-03-04 18:09:30 -08004875 if ((priv->bands[IEEE80211_BAND_5GHZ].n_channels == 0) &&
4876 priv->cfg->sku & IWL_SKU_A) {
Tomas Winkler978785a2008-12-19 10:37:31 +08004877 IWL_INFO(priv, "Incorrectly detected BG card as ABG. "
4878 "Please send your PCI ID 0x%04X:0x%04X to maintainer.\n",
4879 priv->pci_dev->device, priv->pci_dev->subsystem_device);
Tomas Winkler82b9a122008-03-04 18:09:30 -08004880 priv->cfg->sku &= ~IWL_SKU_A;
Zhu Yib481de92007-09-25 17:54:57 -07004881 }
4882
Tomas Winkler978785a2008-12-19 10:37:31 +08004883 IWL_INFO(priv, "Tunable channels: %d 802.11bg, %d 802.11a channels\n",
Johannes Berg8318d782008-01-24 19:38:38 +01004884 priv->bands[IEEE80211_BAND_2GHZ].n_channels,
4885 priv->bands[IEEE80211_BAND_5GHZ].n_channels);
Zhu Yib481de92007-09-25 17:54:57 -07004886
John W. Linvillee0e0a672008-03-25 15:58:40 -04004887 if (priv->bands[IEEE80211_BAND_2GHZ].n_channels)
4888 priv->hw->wiphy->bands[IEEE80211_BAND_2GHZ] =
4889 &priv->bands[IEEE80211_BAND_2GHZ];
4890 if (priv->bands[IEEE80211_BAND_5GHZ].n_channels)
4891 priv->hw->wiphy->bands[IEEE80211_BAND_5GHZ] =
4892 &priv->bands[IEEE80211_BAND_5GHZ];
Zhu Yib481de92007-09-25 17:54:57 -07004893
Zhu Yib481de92007-09-25 17:54:57 -07004894 set_bit(STATUS_GEO_CONFIGURED, &priv->status);
4895
4896 return 0;
4897}
4898
Reinette Chatre849e0dc2008-01-23 10:15:18 -08004899/*
4900 * iwl3945_free_geos - undo allocations in iwl3945_init_geos
4901 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08004902static void iwl3945_free_geos(struct iwl_priv *priv)
Reinette Chatre849e0dc2008-01-23 10:15:18 -08004903{
Reinette Chatre849e0dc2008-01-23 10:15:18 -08004904 kfree(priv->ieee_channels);
4905 kfree(priv->ieee_rates);
4906 clear_bit(STATUS_GEO_CONFIGURED, &priv->status);
4907}
4908
Zhu Yib481de92007-09-25 17:54:57 -07004909/******************************************************************************
4910 *
4911 * uCode download functions
4912 *
4913 ******************************************************************************/
4914
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08004915static void iwl3945_dealloc_ucode_pci(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004916{
Tomas Winkler98c92212008-01-14 17:46:20 -08004917 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_code);
4918 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_data);
4919 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_data_backup);
4920 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_init);
4921 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_init_data);
4922 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_boot);
Zhu Yib481de92007-09-25 17:54:57 -07004923}
4924
4925/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004926 * iwl3945_verify_inst_full - verify runtime uCode image in card vs. host,
Zhu Yib481de92007-09-25 17:54:57 -07004927 * looking at all data.
4928 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08004929static int iwl3945_verify_inst_full(struct iwl_priv *priv, __le32 *image, u32 len)
Zhu Yib481de92007-09-25 17:54:57 -07004930{
4931 u32 val;
4932 u32 save_len = len;
4933 int rc = 0;
4934 u32 errcnt;
4935
4936 IWL_DEBUG_INFO("ucode inst image size is %u\n", len);
4937
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08004938 rc = iwl_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004939 if (rc)
4940 return rc;
4941
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08004942 iwl_write_direct32(priv, HBUS_TARG_MEM_RADDR,
Samuel Ortiz250bdd22008-12-19 10:37:11 +08004943 IWL39_RTC_INST_LOWER_BOUND);
Zhu Yib481de92007-09-25 17:54:57 -07004944
4945 errcnt = 0;
4946 for (; len > 0; len -= sizeof(u32), image++) {
4947 /* read data comes through single port, auto-incr addr */
4948 /* NOTE: Use the debugless read so we don't flood kernel log
4949 * if IWL_DL_IO is set */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08004950 val = _iwl_read_direct32(priv, HBUS_TARG_MEM_RDAT);
Zhu Yib481de92007-09-25 17:54:57 -07004951 if (val != le32_to_cpu(*image)) {
4952 IWL_ERROR("uCode INST section is invalid at "
4953 "offset 0x%x, is 0x%x, s/b 0x%x\n",
4954 save_len - len, val, le32_to_cpu(*image));
4955 rc = -EIO;
4956 errcnt++;
4957 if (errcnt >= 20)
4958 break;
4959 }
4960 }
4961
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08004962 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004963
4964 if (!errcnt)
Ian Schrambc434dd2007-10-25 17:15:29 +08004965 IWL_DEBUG_INFO("ucode image in INSTRUCTION memory is good\n");
Zhu Yib481de92007-09-25 17:54:57 -07004966
4967 return rc;
4968}
4969
4970
4971/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004972 * iwl3945_verify_inst_sparse - verify runtime uCode image in card vs. host,
Zhu Yib481de92007-09-25 17:54:57 -07004973 * using sample data 100 bytes apart. If these sample points are good,
4974 * it's a pretty good bet that everything between them is good, too.
4975 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08004976static int iwl3945_verify_inst_sparse(struct iwl_priv *priv, __le32 *image, u32 len)
Zhu Yib481de92007-09-25 17:54:57 -07004977{
4978 u32 val;
4979 int rc = 0;
4980 u32 errcnt = 0;
4981 u32 i;
4982
4983 IWL_DEBUG_INFO("ucode inst image size is %u\n", len);
4984
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08004985 rc = iwl_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004986 if (rc)
4987 return rc;
4988
4989 for (i = 0; i < len; i += 100, image += 100/sizeof(u32)) {
4990 /* read data comes through single port, auto-incr addr */
4991 /* NOTE: Use the debugless read so we don't flood kernel log
4992 * if IWL_DL_IO is set */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08004993 iwl_write_direct32(priv, HBUS_TARG_MEM_RADDR,
Samuel Ortiz250bdd22008-12-19 10:37:11 +08004994 i + IWL39_RTC_INST_LOWER_BOUND);
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08004995 val = _iwl_read_direct32(priv, HBUS_TARG_MEM_RDAT);
Zhu Yib481de92007-09-25 17:54:57 -07004996 if (val != le32_to_cpu(*image)) {
4997#if 0 /* Enable this if you want to see details */
4998 IWL_ERROR("uCode INST section is invalid at "
4999 "offset 0x%x, is 0x%x, s/b 0x%x\n",
5000 i, val, *image);
5001#endif
5002 rc = -EIO;
5003 errcnt++;
5004 if (errcnt >= 3)
5005 break;
5006 }
5007 }
5008
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08005009 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005010
5011 return rc;
5012}
5013
5014
5015/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005016 * iwl3945_verify_ucode - determine which instruction image is in SRAM,
Zhu Yib481de92007-09-25 17:54:57 -07005017 * and verify its contents
5018 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08005019static int iwl3945_verify_ucode(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005020{
5021 __le32 *image;
5022 u32 len;
5023 int rc = 0;
5024
5025 /* Try bootstrap */
5026 image = (__le32 *)priv->ucode_boot.v_addr;
5027 len = priv->ucode_boot.len;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005028 rc = iwl3945_verify_inst_sparse(priv, image, len);
Zhu Yib481de92007-09-25 17:54:57 -07005029 if (rc == 0) {
5030 IWL_DEBUG_INFO("Bootstrap uCode is good in inst SRAM\n");
5031 return 0;
5032 }
5033
5034 /* Try initialize */
5035 image = (__le32 *)priv->ucode_init.v_addr;
5036 len = priv->ucode_init.len;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005037 rc = iwl3945_verify_inst_sparse(priv, image, len);
Zhu Yib481de92007-09-25 17:54:57 -07005038 if (rc == 0) {
5039 IWL_DEBUG_INFO("Initialize uCode is good in inst SRAM\n");
5040 return 0;
5041 }
5042
5043 /* Try runtime/protocol */
5044 image = (__le32 *)priv->ucode_code.v_addr;
5045 len = priv->ucode_code.len;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005046 rc = iwl3945_verify_inst_sparse(priv, image, len);
Zhu Yib481de92007-09-25 17:54:57 -07005047 if (rc == 0) {
5048 IWL_DEBUG_INFO("Runtime uCode is good in inst SRAM\n");
5049 return 0;
5050 }
5051
5052 IWL_ERROR("NO VALID UCODE IMAGE IN INSTRUCTION SRAM!!\n");
5053
Ben Cahill9fbab512007-11-29 11:09:47 +08005054 /* Since nothing seems to match, show first several data entries in
5055 * instruction SRAM, so maybe visual inspection will give a clue.
5056 * Selection of bootstrap image (vs. other images) is arbitrary. */
Zhu Yib481de92007-09-25 17:54:57 -07005057 image = (__le32 *)priv->ucode_boot.v_addr;
5058 len = priv->ucode_boot.len;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005059 rc = iwl3945_verify_inst_full(priv, image, len);
Zhu Yib481de92007-09-25 17:54:57 -07005060
5061 return rc;
5062}
5063
5064
5065/* check contents of special bootstrap uCode SRAM */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08005066static int iwl3945_verify_bsm(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005067{
5068 __le32 *image = priv->ucode_boot.v_addr;
5069 u32 len = priv->ucode_boot.len;
5070 u32 reg;
5071 u32 val;
5072
5073 IWL_DEBUG_INFO("Begin verify bsm\n");
5074
5075 /* verify BSM SRAM contents */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08005076 val = iwl_read_prph(priv, BSM_WR_DWCOUNT_REG);
Zhu Yib481de92007-09-25 17:54:57 -07005077 for (reg = BSM_SRAM_LOWER_BOUND;
5078 reg < BSM_SRAM_LOWER_BOUND + len;
Tomas Winkler3ac7f142008-07-21 02:40:14 +03005079 reg += sizeof(u32), image++) {
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08005080 val = iwl_read_prph(priv, reg);
Zhu Yib481de92007-09-25 17:54:57 -07005081 if (val != le32_to_cpu(*image)) {
5082 IWL_ERROR("BSM uCode verification failed at "
5083 "addr 0x%08X+%u (of %u), is 0x%x, s/b 0x%x\n",
5084 BSM_SRAM_LOWER_BOUND,
5085 reg - BSM_SRAM_LOWER_BOUND, len,
5086 val, le32_to_cpu(*image));
5087 return -EIO;
5088 }
5089 }
5090
5091 IWL_DEBUG_INFO("BSM bootstrap uCode image OK\n");
5092
5093 return 0;
5094}
5095
5096/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005097 * iwl3945_load_bsm - Load bootstrap instructions
Zhu Yib481de92007-09-25 17:54:57 -07005098 *
5099 * BSM operation:
5100 *
5101 * The Bootstrap State Machine (BSM) stores a short bootstrap uCode program
5102 * in special SRAM that does not power down during RFKILL. When powering back
5103 * up after power-saving sleeps (or during initial uCode load), the BSM loads
5104 * the bootstrap program into the on-board processor, and starts it.
5105 *
5106 * The bootstrap program loads (via DMA) instructions and data for a new
5107 * program from host DRAM locations indicated by the host driver in the
5108 * BSM_DRAM_* registers. Once the new program is loaded, it starts
5109 * automatically.
5110 *
5111 * When initializing the NIC, the host driver points the BSM to the
5112 * "initialize" uCode image. This uCode sets up some internal data, then
5113 * notifies host via "initialize alive" that it is complete.
5114 *
5115 * The host then replaces the BSM_DRAM_* pointer values to point to the
5116 * normal runtime uCode instructions and a backup uCode data cache buffer
5117 * (filled initially with starting data values for the on-board processor),
5118 * then triggers the "initialize" uCode to load and launch the runtime uCode,
5119 * which begins normal operation.
5120 *
5121 * When doing a power-save shutdown, runtime uCode saves data SRAM into
5122 * the backup data cache in DRAM before SRAM is powered down.
5123 *
5124 * When powering back up, the BSM loads the bootstrap program. This reloads
5125 * the runtime uCode instructions and the backup data cache into SRAM,
5126 * and re-launches the runtime uCode from where it left off.
5127 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08005128static int iwl3945_load_bsm(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005129{
5130 __le32 *image = priv->ucode_boot.v_addr;
5131 u32 len = priv->ucode_boot.len;
5132 dma_addr_t pinst;
5133 dma_addr_t pdata;
5134 u32 inst_len;
5135 u32 data_len;
5136 int rc;
5137 int i;
5138 u32 done;
5139 u32 reg_offset;
5140
5141 IWL_DEBUG_INFO("Begin load bsm\n");
5142
5143 /* make sure bootstrap program is no larger than BSM's SRAM size */
Samuel Ortiz250bdd22008-12-19 10:37:11 +08005144 if (len > IWL39_MAX_BSM_SIZE)
Zhu Yib481de92007-09-25 17:54:57 -07005145 return -EINVAL;
5146
5147 /* Tell bootstrap uCode where to find the "Initialize" uCode
Ben Cahill9fbab512007-11-29 11:09:47 +08005148 * in host DRAM ... host DRAM physical address bits 31:0 for 3945.
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005149 * NOTE: iwl3945_initialize_alive_start() will replace these values,
Zhu Yib481de92007-09-25 17:54:57 -07005150 * after the "initialize" uCode has run, to point to
5151 * runtime/protocol instructions and backup data cache. */
5152 pinst = priv->ucode_init.p_addr;
5153 pdata = priv->ucode_init_data.p_addr;
5154 inst_len = priv->ucode_init.len;
5155 data_len = priv->ucode_init_data.len;
5156
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08005157 rc = iwl_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005158 if (rc)
5159 return rc;
5160
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08005161 iwl_write_prph(priv, BSM_DRAM_INST_PTR_REG, pinst);
5162 iwl_write_prph(priv, BSM_DRAM_DATA_PTR_REG, pdata);
5163 iwl_write_prph(priv, BSM_DRAM_INST_BYTECOUNT_REG, inst_len);
5164 iwl_write_prph(priv, BSM_DRAM_DATA_BYTECOUNT_REG, data_len);
Zhu Yib481de92007-09-25 17:54:57 -07005165
5166 /* Fill BSM memory with bootstrap instructions */
5167 for (reg_offset = BSM_SRAM_LOWER_BOUND;
5168 reg_offset < BSM_SRAM_LOWER_BOUND + len;
5169 reg_offset += sizeof(u32), image++)
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08005170 _iwl_write_prph(priv, reg_offset,
Zhu Yib481de92007-09-25 17:54:57 -07005171 le32_to_cpu(*image));
5172
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005173 rc = iwl3945_verify_bsm(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005174 if (rc) {
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08005175 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005176 return rc;
5177 }
5178
5179 /* Tell BSM to copy from BSM SRAM into instruction SRAM, when asked */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08005180 iwl_write_prph(priv, BSM_WR_MEM_SRC_REG, 0x0);
5181 iwl_write_prph(priv, BSM_WR_MEM_DST_REG,
Samuel Ortiz250bdd22008-12-19 10:37:11 +08005182 IWL39_RTC_INST_LOWER_BOUND);
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08005183 iwl_write_prph(priv, BSM_WR_DWCOUNT_REG, len / sizeof(u32));
Zhu Yib481de92007-09-25 17:54:57 -07005184
5185 /* Load bootstrap code into instruction SRAM now,
5186 * to prepare to load "initialize" uCode */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08005187 iwl_write_prph(priv, BSM_WR_CTRL_REG,
Zhu Yib481de92007-09-25 17:54:57 -07005188 BSM_WR_CTRL_REG_BIT_START);
5189
5190 /* Wait for load of bootstrap uCode to finish */
5191 for (i = 0; i < 100; i++) {
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08005192 done = iwl_read_prph(priv, BSM_WR_CTRL_REG);
Zhu Yib481de92007-09-25 17:54:57 -07005193 if (!(done & BSM_WR_CTRL_REG_BIT_START))
5194 break;
5195 udelay(10);
5196 }
5197 if (i < 100)
5198 IWL_DEBUG_INFO("BSM write complete, poll %d iterations\n", i);
5199 else {
5200 IWL_ERROR("BSM write did not complete!\n");
5201 return -EIO;
5202 }
5203
5204 /* Enable future boot loads whenever power management unit triggers it
5205 * (e.g. when powering back up after power-save shutdown) */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08005206 iwl_write_prph(priv, BSM_WR_CTRL_REG,
Zhu Yib481de92007-09-25 17:54:57 -07005207 BSM_WR_CTRL_REG_BIT_START_EN);
5208
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08005209 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005210
5211 return 0;
5212}
5213
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08005214static void iwl3945_nic_start(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005215{
5216 /* Remove all resets to allow NIC to operate */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08005217 iwl_write32(priv, CSR_RESET, 0);
Zhu Yib481de92007-09-25 17:54:57 -07005218}
5219
5220/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005221 * iwl3945_read_ucode - Read uCode images from disk file.
Zhu Yib481de92007-09-25 17:54:57 -07005222 *
5223 * Copy into buffers for card to fetch via bus-mastering
5224 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08005225static int iwl3945_read_ucode(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005226{
Kolekar, Abhijeeta78fe752008-12-19 10:37:21 +08005227 struct iwl_ucode *ucode;
Reinette Chatrea0987a82008-12-02 12:14:06 -08005228 int ret = -EINVAL, index;
Zhu Yib481de92007-09-25 17:54:57 -07005229 const struct firmware *ucode_raw;
5230 /* firmware file name contains uCode/driver compatibility version */
Reinette Chatrea0987a82008-12-02 12:14:06 -08005231 const char *name_pre = priv->cfg->fw_name_pre;
5232 const unsigned int api_max = priv->cfg->ucode_api_max;
5233 const unsigned int api_min = priv->cfg->ucode_api_min;
5234 char buf[25];
Zhu Yib481de92007-09-25 17:54:57 -07005235 u8 *src;
5236 size_t len;
Reinette Chatrea0987a82008-12-02 12:14:06 -08005237 u32 api_ver, inst_size, data_size, init_size, init_data_size, boot_size;
Zhu Yib481de92007-09-25 17:54:57 -07005238
5239 /* Ask kernel firmware_class module to get the boot firmware off disk.
5240 * request_firmware() is synchronous, file is in memory on return. */
Reinette Chatrea0987a82008-12-02 12:14:06 -08005241 for (index = api_max; index >= api_min; index--) {
5242 sprintf(buf, "%s%u%s", name_pre, index, ".ucode");
5243 ret = request_firmware(&ucode_raw, buf, &priv->pci_dev->dev);
5244 if (ret < 0) {
5245 IWL_ERROR("%s firmware file req failed: Reason %d\n",
5246 buf, ret);
5247 if (ret == -ENOENT)
5248 continue;
5249 else
5250 goto error;
5251 } else {
5252 if (index < api_max)
5253 IWL_ERROR("Loaded firmware %s, which is deprecated. Please use API v%u instead.\n",
5254 buf, api_max);
5255 IWL_DEBUG_INFO("Got firmware '%s' file (%zd bytes) from disk\n",
5256 buf, ucode_raw->size);
5257 break;
5258 }
Zhu Yib481de92007-09-25 17:54:57 -07005259 }
5260
Reinette Chatrea0987a82008-12-02 12:14:06 -08005261 if (ret < 0)
5262 goto error;
Zhu Yib481de92007-09-25 17:54:57 -07005263
5264 /* Make sure that we got at least our header! */
5265 if (ucode_raw->size < sizeof(*ucode)) {
5266 IWL_ERROR("File size way too small!\n");
Tomas Winkler90e759d2007-11-29 11:09:41 +08005267 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07005268 goto err_release;
5269 }
5270
5271 /* Data from ucode file: header followed by uCode images */
5272 ucode = (void *)ucode_raw->data;
5273
Chatre, Reinettec02b3ac2008-12-02 12:14:05 -08005274 priv->ucode_ver = le32_to_cpu(ucode->ver);
Reinette Chatrea0987a82008-12-02 12:14:06 -08005275 api_ver = IWL_UCODE_API(priv->ucode_ver);
Zhu Yib481de92007-09-25 17:54:57 -07005276 inst_size = le32_to_cpu(ucode->inst_size);
5277 data_size = le32_to_cpu(ucode->data_size);
5278 init_size = le32_to_cpu(ucode->init_size);
5279 init_data_size = le32_to_cpu(ucode->init_data_size);
5280 boot_size = le32_to_cpu(ucode->boot_size);
5281
Reinette Chatrea0987a82008-12-02 12:14:06 -08005282 /* api_ver should match the api version forming part of the
5283 * firmware filename ... but we don't check for that and only rely
5284 * on the API version read from firware header from here on forward */
5285
5286 if (api_ver < api_min || api_ver > api_max) {
5287 IWL_ERROR("Driver unable to support your firmware API. "
5288 "Driver supports v%u, firmware is v%u.\n",
5289 api_max, api_ver);
5290 priv->ucode_ver = 0;
5291 ret = -EINVAL;
5292 goto err_release;
5293 }
5294 if (api_ver != api_max)
5295 IWL_ERROR("Firmware has old API version. Expected %u, "
5296 "got %u. New firmware can be obtained "
5297 "from http://www.intellinuxwireless.org.\n",
5298 api_max, api_ver);
5299
Tomas Winkler978785a2008-12-19 10:37:31 +08005300 IWL_INFO(priv, "loaded firmware version %u.%u.%u.%u\n",
5301 IWL_UCODE_MAJOR(priv->ucode_ver),
5302 IWL_UCODE_MINOR(priv->ucode_ver),
5303 IWL_UCODE_API(priv->ucode_ver),
5304 IWL_UCODE_SERIAL(priv->ucode_ver));
5305
Reinette Chatrea0987a82008-12-02 12:14:06 -08005306 IWL_DEBUG_INFO("f/w package hdr ucode version raw = 0x%x\n",
5307 priv->ucode_ver);
Ian Schrambc434dd2007-10-25 17:15:29 +08005308 IWL_DEBUG_INFO("f/w package hdr runtime inst size = %u\n", inst_size);
5309 IWL_DEBUG_INFO("f/w package hdr runtime data size = %u\n", data_size);
5310 IWL_DEBUG_INFO("f/w package hdr init inst size = %u\n", init_size);
5311 IWL_DEBUG_INFO("f/w package hdr init data size = %u\n", init_data_size);
5312 IWL_DEBUG_INFO("f/w package hdr boot inst size = %u\n", boot_size);
Zhu Yib481de92007-09-25 17:54:57 -07005313
Reinette Chatrea0987a82008-12-02 12:14:06 -08005314
Zhu Yib481de92007-09-25 17:54:57 -07005315 /* Verify size of file vs. image size info in file's header */
5316 if (ucode_raw->size < sizeof(*ucode) +
5317 inst_size + data_size + init_size +
5318 init_data_size + boot_size) {
5319
5320 IWL_DEBUG_INFO("uCode file size %d too small\n",
5321 (int)ucode_raw->size);
Tomas Winkler90e759d2007-11-29 11:09:41 +08005322 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07005323 goto err_release;
5324 }
5325
5326 /* Verify that uCode images will fit in card's SRAM */
Samuel Ortiz250bdd22008-12-19 10:37:11 +08005327 if (inst_size > IWL39_MAX_INST_SIZE) {
Tomas Winkler90e759d2007-11-29 11:09:41 +08005328 IWL_DEBUG_INFO("uCode instr len %d too large to fit in\n",
5329 inst_size);
5330 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07005331 goto err_release;
5332 }
5333
Samuel Ortiz250bdd22008-12-19 10:37:11 +08005334 if (data_size > IWL39_MAX_DATA_SIZE) {
Tomas Winkler90e759d2007-11-29 11:09:41 +08005335 IWL_DEBUG_INFO("uCode data len %d too large to fit in\n",
5336 data_size);
5337 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07005338 goto err_release;
5339 }
Samuel Ortiz250bdd22008-12-19 10:37:11 +08005340 if (init_size > IWL39_MAX_INST_SIZE) {
Tomas Winkler90e759d2007-11-29 11:09:41 +08005341 IWL_DEBUG_INFO("uCode init instr len %d too large to fit in\n",
5342 init_size);
5343 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07005344 goto err_release;
5345 }
Samuel Ortiz250bdd22008-12-19 10:37:11 +08005346 if (init_data_size > IWL39_MAX_DATA_SIZE) {
Tomas Winkler90e759d2007-11-29 11:09:41 +08005347 IWL_DEBUG_INFO("uCode init data len %d too large to fit in\n",
5348 init_data_size);
5349 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07005350 goto err_release;
5351 }
Samuel Ortiz250bdd22008-12-19 10:37:11 +08005352 if (boot_size > IWL39_MAX_BSM_SIZE) {
Tomas Winkler90e759d2007-11-29 11:09:41 +08005353 IWL_DEBUG_INFO("uCode boot instr len %d too large to fit in\n",
5354 boot_size);
5355 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07005356 goto err_release;
5357 }
5358
5359 /* Allocate ucode buffers for card's bus-master loading ... */
5360
5361 /* Runtime instructions and 2 copies of data:
5362 * 1) unmodified from disk
5363 * 2) backup cache for save/restore during power-downs */
5364 priv->ucode_code.len = inst_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08005365 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_code);
Zhu Yib481de92007-09-25 17:54:57 -07005366
5367 priv->ucode_data.len = data_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08005368 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_data);
Zhu Yib481de92007-09-25 17:54:57 -07005369
5370 priv->ucode_data_backup.len = data_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08005371 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_data_backup);
Zhu Yib481de92007-09-25 17:54:57 -07005372
5373 if (!priv->ucode_code.v_addr || !priv->ucode_data.v_addr ||
Tomas Winkler90e759d2007-11-29 11:09:41 +08005374 !priv->ucode_data_backup.v_addr)
Zhu Yib481de92007-09-25 17:54:57 -07005375 goto err_pci_alloc;
5376
Tomas Winkler90e759d2007-11-29 11:09:41 +08005377 /* Initialization instructions and data */
5378 if (init_size && init_data_size) {
5379 priv->ucode_init.len = init_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08005380 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_init);
Tomas Winkler90e759d2007-11-29 11:09:41 +08005381
5382 priv->ucode_init_data.len = init_data_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08005383 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_init_data);
Tomas Winkler90e759d2007-11-29 11:09:41 +08005384
5385 if (!priv->ucode_init.v_addr || !priv->ucode_init_data.v_addr)
5386 goto err_pci_alloc;
5387 }
5388
5389 /* Bootstrap (instructions only, no data) */
5390 if (boot_size) {
5391 priv->ucode_boot.len = boot_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08005392 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_boot);
Tomas Winkler90e759d2007-11-29 11:09:41 +08005393
5394 if (!priv->ucode_boot.v_addr)
5395 goto err_pci_alloc;
5396 }
5397
Zhu Yib481de92007-09-25 17:54:57 -07005398 /* Copy images into buffers for card's bus-master reads ... */
5399
5400 /* Runtime instructions (first block of data in file) */
5401 src = &ucode->data[0];
5402 len = priv->ucode_code.len;
Tomas Winkler90e759d2007-11-29 11:09:41 +08005403 IWL_DEBUG_INFO("Copying (but not loading) uCode instr len %Zd\n", len);
Zhu Yib481de92007-09-25 17:54:57 -07005404 memcpy(priv->ucode_code.v_addr, src, len);
5405 IWL_DEBUG_INFO("uCode instr buf vaddr = 0x%p, paddr = 0x%08x\n",
5406 priv->ucode_code.v_addr, (u32)priv->ucode_code.p_addr);
5407
5408 /* Runtime data (2nd block)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005409 * NOTE: Copy into backup buffer will be done in iwl3945_up() */
Zhu Yib481de92007-09-25 17:54:57 -07005410 src = &ucode->data[inst_size];
5411 len = priv->ucode_data.len;
Tomas Winkler90e759d2007-11-29 11:09:41 +08005412 IWL_DEBUG_INFO("Copying (but not loading) uCode data len %Zd\n", len);
Zhu Yib481de92007-09-25 17:54:57 -07005413 memcpy(priv->ucode_data.v_addr, src, len);
5414 memcpy(priv->ucode_data_backup.v_addr, src, len);
5415
5416 /* Initialization instructions (3rd block) */
5417 if (init_size) {
5418 src = &ucode->data[inst_size + data_size];
5419 len = priv->ucode_init.len;
Tomas Winkler90e759d2007-11-29 11:09:41 +08005420 IWL_DEBUG_INFO("Copying (but not loading) init instr len %Zd\n",
5421 len);
Zhu Yib481de92007-09-25 17:54:57 -07005422 memcpy(priv->ucode_init.v_addr, src, len);
5423 }
5424
5425 /* Initialization data (4th block) */
5426 if (init_data_size) {
5427 src = &ucode->data[inst_size + data_size + init_size];
5428 len = priv->ucode_init_data.len;
5429 IWL_DEBUG_INFO("Copying (but not loading) init data len %d\n",
5430 (int)len);
5431 memcpy(priv->ucode_init_data.v_addr, src, len);
5432 }
5433
5434 /* Bootstrap instructions (5th block) */
5435 src = &ucode->data[inst_size + data_size + init_size + init_data_size];
5436 len = priv->ucode_boot.len;
5437 IWL_DEBUG_INFO("Copying (but not loading) boot instr len %d\n",
5438 (int)len);
5439 memcpy(priv->ucode_boot.v_addr, src, len);
5440
5441 /* We have our copies now, allow OS release its copies */
5442 release_firmware(ucode_raw);
5443 return 0;
5444
5445 err_pci_alloc:
5446 IWL_ERROR("failed to allocate pci memory\n");
Tomas Winkler90e759d2007-11-29 11:09:41 +08005447 ret = -ENOMEM;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005448 iwl3945_dealloc_ucode_pci(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005449
5450 err_release:
5451 release_firmware(ucode_raw);
5452
5453 error:
Tomas Winkler90e759d2007-11-29 11:09:41 +08005454 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07005455}
5456
5457
5458/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005459 * iwl3945_set_ucode_ptrs - Set uCode address location
Zhu Yib481de92007-09-25 17:54:57 -07005460 *
5461 * Tell initialization uCode where to find runtime uCode.
5462 *
5463 * BSM registers initially contain pointers to initialization uCode.
5464 * We need to replace them to load runtime uCode inst and data,
5465 * and to save runtime data when powering down.
5466 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08005467static int iwl3945_set_ucode_ptrs(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005468{
5469 dma_addr_t pinst;
5470 dma_addr_t pdata;
5471 int rc = 0;
5472 unsigned long flags;
5473
5474 /* bits 31:0 for 3945 */
5475 pinst = priv->ucode_code.p_addr;
5476 pdata = priv->ucode_data_backup.p_addr;
5477
5478 spin_lock_irqsave(&priv->lock, flags);
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08005479 rc = iwl_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005480 if (rc) {
5481 spin_unlock_irqrestore(&priv->lock, flags);
5482 return rc;
5483 }
5484
5485 /* Tell bootstrap uCode where to find image to load */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08005486 iwl_write_prph(priv, BSM_DRAM_INST_PTR_REG, pinst);
5487 iwl_write_prph(priv, BSM_DRAM_DATA_PTR_REG, pdata);
5488 iwl_write_prph(priv, BSM_DRAM_DATA_BYTECOUNT_REG,
Zhu Yib481de92007-09-25 17:54:57 -07005489 priv->ucode_data.len);
5490
Tomas Winklera96a27f2008-10-23 23:48:56 -07005491 /* Inst byte count must be last to set up, bit 31 signals uCode
Zhu Yib481de92007-09-25 17:54:57 -07005492 * that all new ptr/size info is in place */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08005493 iwl_write_prph(priv, BSM_DRAM_INST_BYTECOUNT_REG,
Zhu Yib481de92007-09-25 17:54:57 -07005494 priv->ucode_code.len | BSM_DRAM_INST_LOAD);
5495
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08005496 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005497
5498 spin_unlock_irqrestore(&priv->lock, flags);
5499
5500 IWL_DEBUG_INFO("Runtime uCode pointers are set.\n");
5501
5502 return rc;
5503}
5504
5505/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005506 * iwl3945_init_alive_start - Called after REPLY_ALIVE notification received
Zhu Yib481de92007-09-25 17:54:57 -07005507 *
5508 * Called after REPLY_ALIVE notification received from "initialize" uCode.
5509 *
Zhu Yib481de92007-09-25 17:54:57 -07005510 * Tell "initialize" uCode to go ahead and load the runtime uCode.
Ben Cahill9fbab512007-11-29 11:09:47 +08005511 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08005512static void iwl3945_init_alive_start(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005513{
5514 /* Check alive response for "valid" sign from uCode */
5515 if (priv->card_alive_init.is_valid != UCODE_VALID_OK) {
5516 /* We had an error bringing up the hardware, so take it
5517 * all the way back down so we can try again */
5518 IWL_DEBUG_INFO("Initialize Alive failed.\n");
5519 goto restart;
5520 }
5521
5522 /* Bootstrap uCode has loaded initialize uCode ... verify inst image.
5523 * This is a paranoid check, because we would not have gotten the
5524 * "initialize" alive if code weren't properly loaded. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005525 if (iwl3945_verify_ucode(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07005526 /* Runtime instruction load was bad;
5527 * take it all the way back down so we can try again */
5528 IWL_DEBUG_INFO("Bad \"initialize\" uCode load.\n");
5529 goto restart;
5530 }
5531
5532 /* Send pointers to protocol/runtime uCode image ... init code will
5533 * load and launch runtime uCode, which will send us another "Alive"
5534 * notification. */
5535 IWL_DEBUG_INFO("Initialization Alive received.\n");
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005536 if (iwl3945_set_ucode_ptrs(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07005537 /* Runtime instruction load won't happen;
5538 * take it all the way back down so we can try again */
5539 IWL_DEBUG_INFO("Couldn't set up uCode pointers.\n");
5540 goto restart;
5541 }
5542 return;
5543
5544 restart:
5545 queue_work(priv->workqueue, &priv->restart);
5546}
5547
5548
Mohamed Abbas9bdf5ec2008-11-07 09:58:35 -08005549/* temporary */
5550static int iwl3945_mac_beacon_update(struct ieee80211_hw *hw,
5551 struct sk_buff *skb);
5552
Zhu Yib481de92007-09-25 17:54:57 -07005553/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005554 * iwl3945_alive_start - called after REPLY_ALIVE notification received
Zhu Yib481de92007-09-25 17:54:57 -07005555 * from protocol/runtime uCode (initialization uCode's
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005556 * Alive gets handled by iwl3945_init_alive_start()).
Zhu Yib481de92007-09-25 17:54:57 -07005557 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08005558static void iwl3945_alive_start(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005559{
5560 int rc = 0;
5561 int thermal_spin = 0;
5562 u32 rfkill;
5563
5564 IWL_DEBUG_INFO("Runtime Alive received.\n");
5565
5566 if (priv->card_alive.is_valid != UCODE_VALID_OK) {
5567 /* We had an error bringing up the hardware, so take it
5568 * all the way back down so we can try again */
5569 IWL_DEBUG_INFO("Alive failed.\n");
5570 goto restart;
5571 }
5572
5573 /* Initialize uCode has loaded Runtime uCode ... verify inst image.
5574 * This is a paranoid check, because we would not have gotten the
5575 * "runtime" alive if code weren't properly loaded. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005576 if (iwl3945_verify_ucode(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07005577 /* Runtime instruction load was bad;
5578 * take it all the way back down so we can try again */
5579 IWL_DEBUG_INFO("Bad runtime uCode load.\n");
5580 goto restart;
5581 }
5582
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005583 iwl3945_clear_stations_table(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005584
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08005585 rc = iwl_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005586 if (rc) {
Winkler, Tomas39aadf82008-12-19 10:37:32 +08005587 IWL_WARN(priv, "Can not read RFKILL status from adapter\n");
Zhu Yib481de92007-09-25 17:54:57 -07005588 return;
5589 }
5590
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08005591 rfkill = iwl_read_prph(priv, APMG_RFKILL_REG);
Zhu Yib481de92007-09-25 17:54:57 -07005592 IWL_DEBUG_INFO("RFKILL status: 0x%x\n", rfkill);
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08005593 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005594
5595 if (rfkill & 0x1) {
5596 clear_bit(STATUS_RF_KILL_HW, &priv->status);
Tomas Winklera96a27f2008-10-23 23:48:56 -07005597 /* if RFKILL is not on, then wait for thermal
Zhu Yib481de92007-09-25 17:54:57 -07005598 * sensor in adapter to kick in */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005599 while (iwl3945_hw_get_temperature(priv) == 0) {
Zhu Yib481de92007-09-25 17:54:57 -07005600 thermal_spin++;
5601 udelay(10);
5602 }
5603
5604 if (thermal_spin)
5605 IWL_DEBUG_INFO("Thermal calibration took %dus\n",
5606 thermal_spin * 10);
5607 } else
5608 set_bit(STATUS_RF_KILL_HW, &priv->status);
5609
Ben Cahill9fbab512007-11-29 11:09:47 +08005610 /* After the ALIVE response, we can send commands to 3945 uCode */
Zhu Yib481de92007-09-25 17:54:57 -07005611 set_bit(STATUS_ALIVE, &priv->status);
5612
5613 /* Clear out the uCode error bit if it is set */
5614 clear_bit(STATUS_FW_ERROR, &priv->status);
5615
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005616 if (iwl3945_is_rfkill(priv))
Zhu Yib481de92007-09-25 17:54:57 -07005617 return;
5618
Johannes Berg36d68252008-05-15 12:55:26 +02005619 ieee80211_wake_queues(priv->hw);
Zhu Yib481de92007-09-25 17:54:57 -07005620
5621 priv->active_rate = priv->rates_mask;
5622 priv->active_rate_basic = priv->rates_mask & IWL_BASIC_RATES_MASK;
5623
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005624 iwl3945_send_power_mode(priv, IWL_POWER_LEVEL(priv->power_mode));
Zhu Yib481de92007-09-25 17:54:57 -07005625
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005626 if (iwl3945_is_associated(priv)) {
5627 struct iwl3945_rxon_cmd *active_rxon =
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08005628 (struct iwl3945_rxon_cmd *)(&priv->active39_rxon);
Zhu Yib481de92007-09-25 17:54:57 -07005629
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08005630 memcpy(&priv->staging39_rxon, &priv->active39_rxon,
5631 sizeof(priv->staging39_rxon));
Zhu Yib481de92007-09-25 17:54:57 -07005632 active_rxon->filter_flags &= ~RXON_FILTER_ASSOC_MSK;
5633 } else {
5634 /* Initialize our rx_config data */
Zhu, Yi60294de2008-10-29 14:05:45 -07005635 iwl3945_connection_init_rx_config(priv, priv->iw_mode);
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08005636 memcpy(priv->staging39_rxon.node_addr, priv->mac_addr, ETH_ALEN);
Zhu Yib481de92007-09-25 17:54:57 -07005637 }
5638
Ben Cahill9fbab512007-11-29 11:09:47 +08005639 /* Configure Bluetooth device coexistence support */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005640 iwl3945_send_bt_config(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005641
5642 /* Configure the adapter for unassociated operation */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005643 iwl3945_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005644
Zhu Yib481de92007-09-25 17:54:57 -07005645 iwl3945_reg_txpower_periodic(priv);
5646
Reinette Chatrefe00b5a2008-04-03 16:05:23 -07005647 iwl3945_led_register(priv);
5648
Zhu Yib481de92007-09-25 17:54:57 -07005649 IWL_DEBUG_INFO("ALIVE processing complete.\n");
Rick Farringtona9f46782008-03-18 14:57:49 -07005650 set_bit(STATUS_READY, &priv->status);
Zhu Yi5a669262008-01-14 17:46:18 -08005651 wake_up_interruptible(&priv->wait_command_queue);
Zhu Yib481de92007-09-25 17:54:57 -07005652
5653 if (priv->error_recovering)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005654 iwl3945_error_recovery(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005655
Mohamed Abbas9bdf5ec2008-11-07 09:58:35 -08005656 /* reassociate for ADHOC mode */
5657 if (priv->vif && (priv->iw_mode == NL80211_IFTYPE_ADHOC)) {
5658 struct sk_buff *beacon = ieee80211_beacon_get(priv->hw,
5659 priv->vif);
5660 if (beacon)
5661 iwl3945_mac_beacon_update(priv->hw, beacon);
5662 }
5663
Zhu Yib481de92007-09-25 17:54:57 -07005664 return;
5665
5666 restart:
5667 queue_work(priv->workqueue, &priv->restart);
5668}
5669
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08005670static void iwl3945_cancel_deferred_work(struct iwl_priv *priv);
Zhu Yib481de92007-09-25 17:54:57 -07005671
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08005672static void __iwl3945_down(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005673{
5674 unsigned long flags;
5675 int exit_pending = test_bit(STATUS_EXIT_PENDING, &priv->status);
5676 struct ieee80211_conf *conf = NULL;
5677
5678 IWL_DEBUG_INFO(DRV_NAME " is going down\n");
5679
5680 conf = ieee80211_get_hw_conf(priv->hw);
5681
5682 if (!exit_pending)
5683 set_bit(STATUS_EXIT_PENDING, &priv->status);
5684
Mohamed Abbasab53d8a2008-03-25 16:33:36 -07005685 iwl3945_led_unregister(priv);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005686 iwl3945_clear_stations_table(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005687
5688 /* Unblock any waiting calls */
5689 wake_up_interruptible_all(&priv->wait_command_queue);
5690
Zhu Yib481de92007-09-25 17:54:57 -07005691 /* Wipe out the EXIT_PENDING status bit if we are not actually
5692 * exiting the module */
5693 if (!exit_pending)
5694 clear_bit(STATUS_EXIT_PENDING, &priv->status);
5695
5696 /* stop and reset the on-board processor */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08005697 iwl_write32(priv, CSR_RESET, CSR_RESET_REG_FLAG_NEVO_RESET);
Zhu Yib481de92007-09-25 17:54:57 -07005698
5699 /* tell the device to stop sending interrupts */
Mohamed Abbas0359fac2008-03-28 16:21:08 -07005700 spin_lock_irqsave(&priv->lock, flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005701 iwl3945_disable_interrupts(priv);
Mohamed Abbas0359fac2008-03-28 16:21:08 -07005702 spin_unlock_irqrestore(&priv->lock, flags);
5703 iwl_synchronize_irq(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005704
5705 if (priv->mac80211_registered)
5706 ieee80211_stop_queues(priv->hw);
5707
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005708 /* If we have not previously called iwl3945_init() then
Zhu Yib481de92007-09-25 17:54:57 -07005709 * clear all bits but the RF Kill and SUSPEND bits and return */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005710 if (!iwl3945_is_init(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07005711 priv->status = test_bit(STATUS_RF_KILL_HW, &priv->status) <<
5712 STATUS_RF_KILL_HW |
5713 test_bit(STATUS_RF_KILL_SW, &priv->status) <<
5714 STATUS_RF_KILL_SW |
Reinette Chatre97888642008-02-06 11:20:38 -08005715 test_bit(STATUS_GEO_CONFIGURED, &priv->status) <<
5716 STATUS_GEO_CONFIGURED |
Zhu Yib481de92007-09-25 17:54:57 -07005717 test_bit(STATUS_IN_SUSPEND, &priv->status) <<
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08005718 STATUS_IN_SUSPEND |
5719 test_bit(STATUS_EXIT_PENDING, &priv->status) <<
5720 STATUS_EXIT_PENDING;
Zhu Yib481de92007-09-25 17:54:57 -07005721 goto exit;
5722 }
5723
5724 /* ...otherwise clear out all the status bits but the RF Kill and
5725 * SUSPEND bits and continue taking the NIC down. */
5726 priv->status &= test_bit(STATUS_RF_KILL_HW, &priv->status) <<
5727 STATUS_RF_KILL_HW |
5728 test_bit(STATUS_RF_KILL_SW, &priv->status) <<
5729 STATUS_RF_KILL_SW |
Reinette Chatre97888642008-02-06 11:20:38 -08005730 test_bit(STATUS_GEO_CONFIGURED, &priv->status) <<
5731 STATUS_GEO_CONFIGURED |
Zhu Yib481de92007-09-25 17:54:57 -07005732 test_bit(STATUS_IN_SUSPEND, &priv->status) <<
5733 STATUS_IN_SUSPEND |
5734 test_bit(STATUS_FW_ERROR, &priv->status) <<
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08005735 STATUS_FW_ERROR |
5736 test_bit(STATUS_EXIT_PENDING, &priv->status) <<
5737 STATUS_EXIT_PENDING;
Zhu Yib481de92007-09-25 17:54:57 -07005738
5739 spin_lock_irqsave(&priv->lock, flags);
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08005740 iwl_clear_bit(priv, CSR_GP_CNTRL, CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
Zhu Yib481de92007-09-25 17:54:57 -07005741 spin_unlock_irqrestore(&priv->lock, flags);
5742
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005743 iwl3945_hw_txq_ctx_stop(priv);
5744 iwl3945_hw_rxq_stop(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005745
5746 spin_lock_irqsave(&priv->lock, flags);
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08005747 if (!iwl_grab_nic_access(priv)) {
5748 iwl_write_prph(priv, APMG_CLK_DIS_REG,
Zhu Yib481de92007-09-25 17:54:57 -07005749 APMG_CLK_VAL_DMA_CLK_RQT);
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08005750 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005751 }
5752 spin_unlock_irqrestore(&priv->lock, flags);
5753
5754 udelay(5);
5755
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005756 iwl3945_hw_nic_stop_master(priv);
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08005757 iwl_set_bit(priv, CSR_RESET, CSR_RESET_REG_FLAG_SW_RESET);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005758 iwl3945_hw_nic_reset(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005759
5760 exit:
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08005761 memset(&priv->card_alive, 0, sizeof(struct iwl_alive_resp));
Zhu Yib481de92007-09-25 17:54:57 -07005762
5763 if (priv->ibss_beacon)
5764 dev_kfree_skb(priv->ibss_beacon);
5765 priv->ibss_beacon = NULL;
5766
5767 /* clear out any free frames */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005768 iwl3945_clear_free_frames(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005769}
5770
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08005771static void iwl3945_down(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005772{
5773 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005774 __iwl3945_down(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005775 mutex_unlock(&priv->mutex);
Zhu Yib24d22b2007-12-19 13:59:52 +08005776
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005777 iwl3945_cancel_deferred_work(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005778}
5779
5780#define MAX_HW_RESTARTS 5
5781
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08005782static int __iwl3945_up(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005783{
5784 int rc, i;
5785
5786 if (test_bit(STATUS_EXIT_PENDING, &priv->status)) {
Winkler, Tomas39aadf82008-12-19 10:37:32 +08005787 IWL_WARN(priv, "Exit pending; will not bring the NIC up\n");
Zhu Yib481de92007-09-25 17:54:57 -07005788 return -EIO;
5789 }
5790
5791 if (test_bit(STATUS_RF_KILL_SW, &priv->status)) {
Winkler, Tomas39aadf82008-12-19 10:37:32 +08005792 IWL_WARN(priv, "Radio disabled by SW RF kill (module "
Zhu Yib481de92007-09-25 17:54:57 -07005793 "parameter)\n");
Zhu Yie655b9f2008-01-24 02:19:38 -08005794 return -ENODEV;
5795 }
5796
Reinette Chatree903fbd2008-01-30 22:05:15 -08005797 if (!priv->ucode_data_backup.v_addr || !priv->ucode_data.v_addr) {
Tomas Winklera96a27f2008-10-23 23:48:56 -07005798 IWL_ERROR("ucode not available for device bring up\n");
Reinette Chatree903fbd2008-01-30 22:05:15 -08005799 return -EIO;
5800 }
5801
Zhu Yie655b9f2008-01-24 02:19:38 -08005802 /* If platform's RF_KILL switch is NOT set to KILL */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08005803 if (iwl_read32(priv, CSR_GP_CNTRL) &
Zhu Yie655b9f2008-01-24 02:19:38 -08005804 CSR_GP_CNTRL_REG_FLAG_HW_RF_KILL_SW)
5805 clear_bit(STATUS_RF_KILL_HW, &priv->status);
5806 else {
5807 set_bit(STATUS_RF_KILL_HW, &priv->status);
5808 if (!test_bit(STATUS_IN_SUSPEND, &priv->status)) {
Winkler, Tomas39aadf82008-12-19 10:37:32 +08005809 IWL_WARN(priv, "Radio disabled by HW RF Kill switch\n");
Zhu Yie655b9f2008-01-24 02:19:38 -08005810 return -ENODEV;
5811 }
Zhu Yib481de92007-09-25 17:54:57 -07005812 }
Adel Gadllah80fcc9e2008-07-01 17:49:50 +02005813
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08005814 iwl_write32(priv, CSR_INT, 0xFFFFFFFF);
Zhu Yib481de92007-09-25 17:54:57 -07005815
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005816 rc = iwl3945_hw_nic_init(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005817 if (rc) {
5818 IWL_ERROR("Unable to int nic\n");
5819 return rc;
5820 }
5821
5822 /* make sure rfkill handshake bits are cleared */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08005823 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
5824 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR,
Zhu Yib481de92007-09-25 17:54:57 -07005825 CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
5826
5827 /* clear (again), then enable host interrupts */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08005828 iwl_write32(priv, CSR_INT, 0xFFFFFFFF);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005829 iwl3945_enable_interrupts(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005830
5831 /* really make sure rfkill handshake bits are cleared */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08005832 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
5833 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
Zhu Yib481de92007-09-25 17:54:57 -07005834
5835 /* Copy original ucode data image from disk into backup cache.
5836 * This will be used to initialize the on-board processor's
5837 * data SRAM for a clean start when the runtime program first loads. */
5838 memcpy(priv->ucode_data_backup.v_addr, priv->ucode_data.v_addr,
Zhu Yi5a669262008-01-14 17:46:18 -08005839 priv->ucode_data.len);
Zhu Yib481de92007-09-25 17:54:57 -07005840
Zhu Yie655b9f2008-01-24 02:19:38 -08005841 /* We return success when we resume from suspend and rf_kill is on. */
5842 if (test_bit(STATUS_RF_KILL_HW, &priv->status))
5843 return 0;
5844
Zhu Yib481de92007-09-25 17:54:57 -07005845 for (i = 0; i < MAX_HW_RESTARTS; i++) {
5846
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005847 iwl3945_clear_stations_table(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005848
5849 /* load bootstrap state machine,
5850 * load bootstrap program into processor's memory,
5851 * prepare to load the "initialize" uCode */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005852 rc = iwl3945_load_bsm(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005853
5854 if (rc) {
5855 IWL_ERROR("Unable to set up bootstrap uCode: %d\n", rc);
5856 continue;
5857 }
5858
5859 /* start card; "initialize" will load runtime ucode */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005860 iwl3945_nic_start(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005861
Zhu Yib481de92007-09-25 17:54:57 -07005862 IWL_DEBUG_INFO(DRV_NAME " is coming up\n");
5863
5864 return 0;
5865 }
5866
5867 set_bit(STATUS_EXIT_PENDING, &priv->status);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005868 __iwl3945_down(priv);
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08005869 clear_bit(STATUS_EXIT_PENDING, &priv->status);
Zhu Yib481de92007-09-25 17:54:57 -07005870
5871 /* tried to restart and config the device for as long as our
5872 * patience could withstand */
5873 IWL_ERROR("Unable to initialize device after %d attempts.\n", i);
5874 return -EIO;
5875}
5876
5877
5878/*****************************************************************************
5879 *
5880 * Workqueue callbacks
5881 *
5882 *****************************************************************************/
5883
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005884static void iwl3945_bg_init_alive_start(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07005885{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08005886 struct iwl_priv *priv =
5887 container_of(data, struct iwl_priv, init_alive_start.work);
Zhu Yib481de92007-09-25 17:54:57 -07005888
5889 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
5890 return;
5891
5892 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005893 iwl3945_init_alive_start(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005894 mutex_unlock(&priv->mutex);
5895}
5896
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005897static void iwl3945_bg_alive_start(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07005898{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08005899 struct iwl_priv *priv =
5900 container_of(data, struct iwl_priv, alive_start.work);
Zhu Yib481de92007-09-25 17:54:57 -07005901
5902 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
5903 return;
5904
5905 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005906 iwl3945_alive_start(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005907 mutex_unlock(&priv->mutex);
5908}
5909
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005910static void iwl3945_bg_rf_kill(struct work_struct *work)
Zhu Yib481de92007-09-25 17:54:57 -07005911{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08005912 struct iwl_priv *priv = container_of(work, struct iwl_priv, rf_kill);
Zhu Yib481de92007-09-25 17:54:57 -07005913
5914 wake_up_interruptible(&priv->wait_command_queue);
5915
5916 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
5917 return;
5918
5919 mutex_lock(&priv->mutex);
5920
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005921 if (!iwl3945_is_rfkill(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07005922 IWL_DEBUG(IWL_DL_INFO | IWL_DL_RF_KILL,
5923 "HW and/or SW RF Kill no longer active, restarting "
5924 "device\n");
5925 if (!test_bit(STATUS_EXIT_PENDING, &priv->status))
5926 queue_work(priv->workqueue, &priv->restart);
5927 } else {
5928
5929 if (!test_bit(STATUS_RF_KILL_HW, &priv->status))
5930 IWL_DEBUG_RF_KILL("Can not turn radio back on - "
5931 "disabled by SW switch\n");
5932 else
Winkler, Tomas39aadf82008-12-19 10:37:32 +08005933 IWL_WARN(priv, "Radio Frequency Kill Switch is On:\n"
Zhu Yib481de92007-09-25 17:54:57 -07005934 "Kill switch must be turned off for "
5935 "wireless networking to work.\n");
5936 }
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08005937
Zhu Yib481de92007-09-25 17:54:57 -07005938 mutex_unlock(&priv->mutex);
Adel Gadllah80fcc9e2008-07-01 17:49:50 +02005939 iwl3945_rfkill_set_hw_state(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005940}
5941
5942#define IWL_SCAN_CHECK_WATCHDOG (7 * HZ)
5943
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005944static void iwl3945_bg_scan_check(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07005945{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08005946 struct iwl_priv *priv =
5947 container_of(data, struct iwl_priv, scan_check.work);
Zhu Yib481de92007-09-25 17:54:57 -07005948
5949 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
5950 return;
5951
5952 mutex_lock(&priv->mutex);
5953 if (test_bit(STATUS_SCANNING, &priv->status) ||
5954 test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
5955 IWL_DEBUG(IWL_DL_INFO | IWL_DL_SCAN,
5956 "Scan completion watchdog resetting adapter (%dms)\n",
5957 jiffies_to_msecs(IWL_SCAN_CHECK_WATCHDOG));
Mohamed Abbas15e869d2007-10-25 17:15:46 +08005958
Zhu Yib481de92007-09-25 17:54:57 -07005959 if (!test_bit(STATUS_EXIT_PENDING, &priv->status))
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005960 iwl3945_send_scan_abort(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005961 }
5962 mutex_unlock(&priv->mutex);
5963}
5964
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005965static void iwl3945_bg_request_scan(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07005966{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08005967 struct iwl_priv *priv =
5968 container_of(data, struct iwl_priv, request_scan);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005969 struct iwl3945_host_cmd cmd = {
Zhu Yib481de92007-09-25 17:54:57 -07005970 .id = REPLY_SCAN_CMD,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005971 .len = sizeof(struct iwl3945_scan_cmd),
Zhu Yib481de92007-09-25 17:54:57 -07005972 .meta.flags = CMD_SIZE_HUGE,
5973 };
5974 int rc = 0;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005975 struct iwl3945_scan_cmd *scan;
Zhu Yib481de92007-09-25 17:54:57 -07005976 struct ieee80211_conf *conf = NULL;
Abhijeet Kolekarf9340522008-09-03 11:26:58 +08005977 u8 n_probes = 2;
Johannes Berg8318d782008-01-24 19:38:38 +01005978 enum ieee80211_band band;
John W. Linville9387b7c2008-09-30 20:59:05 -04005979 DECLARE_SSID_BUF(ssid);
Zhu Yib481de92007-09-25 17:54:57 -07005980
5981 conf = ieee80211_get_hw_conf(priv->hw);
5982
5983 mutex_lock(&priv->mutex);
5984
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005985 if (!iwl3945_is_ready(priv)) {
Winkler, Tomas39aadf82008-12-19 10:37:32 +08005986 IWL_WARN(priv, "request scan called when driver not ready.\n");
Zhu Yib481de92007-09-25 17:54:57 -07005987 goto done;
5988 }
5989
Tomas Winklera96a27f2008-10-23 23:48:56 -07005990 /* Make sure the scan wasn't canceled before this queued work
Zhu Yib481de92007-09-25 17:54:57 -07005991 * was given the chance to run... */
5992 if (!test_bit(STATUS_SCANNING, &priv->status))
5993 goto done;
5994
5995 /* This should never be called or scheduled if there is currently
5996 * a scan active in the hardware. */
5997 if (test_bit(STATUS_SCAN_HW, &priv->status)) {
5998 IWL_DEBUG_INFO("Multiple concurrent scan requests in parallel. "
5999 "Ignoring second request.\n");
6000 rc = -EIO;
6001 goto done;
6002 }
6003
6004 if (test_bit(STATUS_EXIT_PENDING, &priv->status)) {
6005 IWL_DEBUG_SCAN("Aborting scan due to device shutdown\n");
6006 goto done;
6007 }
6008
6009 if (test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
6010 IWL_DEBUG_HC("Scan request while abort pending. Queuing.\n");
6011 goto done;
6012 }
6013
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006014 if (iwl3945_is_rfkill(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07006015 IWL_DEBUG_HC("Aborting scan due to RF Kill activation\n");
6016 goto done;
6017 }
6018
6019 if (!test_bit(STATUS_READY, &priv->status)) {
6020 IWL_DEBUG_HC("Scan request while uninitialized. Queuing.\n");
6021 goto done;
6022 }
6023
6024 if (!priv->scan_bands) {
6025 IWL_DEBUG_HC("Aborting scan due to no requested bands\n");
6026 goto done;
6027 }
6028
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08006029 if (!priv->scan39) {
6030 priv->scan39 = kmalloc(sizeof(struct iwl3945_scan_cmd) +
Zhu Yib481de92007-09-25 17:54:57 -07006031 IWL_MAX_SCAN_SIZE, GFP_KERNEL);
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08006032 if (!priv->scan39) {
Zhu Yib481de92007-09-25 17:54:57 -07006033 rc = -ENOMEM;
6034 goto done;
6035 }
6036 }
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08006037 scan = priv->scan39;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006038 memset(scan, 0, sizeof(struct iwl3945_scan_cmd) + IWL_MAX_SCAN_SIZE);
Zhu Yib481de92007-09-25 17:54:57 -07006039
6040 scan->quiet_plcp_th = IWL_PLCP_QUIET_THRESH;
6041 scan->quiet_time = IWL_ACTIVE_QUIET_TIME;
6042
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006043 if (iwl3945_is_associated(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07006044 u16 interval = 0;
6045 u32 extra;
6046 u32 suspend_time = 100;
6047 u32 scan_suspend_time = 100;
6048 unsigned long flags;
6049
6050 IWL_DEBUG_INFO("Scanning while associated...\n");
6051
6052 spin_lock_irqsave(&priv->lock, flags);
6053 interval = priv->beacon_int;
6054 spin_unlock_irqrestore(&priv->lock, flags);
6055
6056 scan->suspend_time = 0;
Mohamed Abbas15e869d2007-10-25 17:15:46 +08006057 scan->max_out_time = cpu_to_le32(200 * 1024);
Zhu Yib481de92007-09-25 17:54:57 -07006058 if (!interval)
6059 interval = suspend_time;
6060 /*
6061 * suspend time format:
6062 * 0-19: beacon interval in usec (time before exec.)
6063 * 20-23: 0
6064 * 24-31: number of beacons (suspend between channels)
6065 */
6066
6067 extra = (suspend_time / interval) << 24;
6068 scan_suspend_time = 0xFF0FFFFF &
6069 (extra | ((suspend_time % interval) * 1024));
6070
6071 scan->suspend_time = cpu_to_le32(scan_suspend_time);
6072 IWL_DEBUG_SCAN("suspend_time 0x%X beacon interval %d\n",
6073 scan_suspend_time, interval);
6074 }
6075
6076 /* We should add the ability for user to lock to PASSIVE ONLY */
6077 if (priv->one_direct_scan) {
6078 IWL_DEBUG_SCAN
6079 ("Kicking off one direct scan for '%s'\n",
John W. Linville9387b7c2008-09-30 20:59:05 -04006080 print_ssid(ssid, priv->direct_ssid,
6081 priv->direct_ssid_len));
Zhu Yib481de92007-09-25 17:54:57 -07006082 scan->direct_scan[0].id = WLAN_EID_SSID;
6083 scan->direct_scan[0].len = priv->direct_ssid_len;
6084 memcpy(scan->direct_scan[0].ssid,
6085 priv->direct_ssid, priv->direct_ssid_len);
Abhijeet Kolekarf9340522008-09-03 11:26:58 +08006086 n_probes++;
Abhijeet Kolekarf9340522008-09-03 11:26:58 +08006087 } else
Bill Moss786b4552008-04-17 16:03:40 -07006088 IWL_DEBUG_SCAN("Kicking off one indirect scan.\n");
Zhu Yib481de92007-09-25 17:54:57 -07006089
6090 /* We don't build a direct scan probe request; the uCode will do
6091 * that based on the direct_mask added to each channel entry */
6092 scan->tx_cmd.len = cpu_to_le16(
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006093 iwl3945_fill_probe_req(priv, (struct ieee80211_mgmt *)scan->data,
Johannes Berg430cfe92008-10-28 18:06:02 +01006094 IWL_MAX_SCAN_SIZE - sizeof(*scan)));
Zhu Yib481de92007-09-25 17:54:57 -07006095 scan->tx_cmd.tx_flags = TX_CMD_FLG_SEQ_CTL_MSK;
Abhijeet Kolekar3832ec92008-12-19 10:37:26 +08006096 scan->tx_cmd.sta_id = priv->hw_params.bcast_sta_id;
Zhu Yib481de92007-09-25 17:54:57 -07006097 scan->tx_cmd.stop_time.life_time = TX_CMD_LIFE_TIME_INFINITE;
6098
6099 /* flags + rate selection */
6100
Ron Rindjunsky66b50042008-06-25 16:46:31 +08006101 if (priv->scan_bands & BIT(IEEE80211_BAND_2GHZ)) {
Zhu Yib481de92007-09-25 17:54:57 -07006102 scan->flags = RXON_FLG_BAND_24G_MSK | RXON_FLG_AUTO_DETECT_MSK;
6103 scan->tx_cmd.rate = IWL_RATE_1M_PLCP;
6104 scan->good_CRC_th = 0;
Johannes Berg8318d782008-01-24 19:38:38 +01006105 band = IEEE80211_BAND_2GHZ;
Ron Rindjunsky66b50042008-06-25 16:46:31 +08006106 } else if (priv->scan_bands & BIT(IEEE80211_BAND_5GHZ)) {
Zhu Yib481de92007-09-25 17:54:57 -07006107 scan->tx_cmd.rate = IWL_RATE_6M_PLCP;
6108 scan->good_CRC_th = IWL_GOOD_CRC_TH;
Johannes Berg8318d782008-01-24 19:38:38 +01006109 band = IEEE80211_BAND_5GHZ;
Ron Rindjunsky66b50042008-06-25 16:46:31 +08006110 } else {
Winkler, Tomas39aadf82008-12-19 10:37:32 +08006111 IWL_WARN(priv, "Invalid scan band count\n");
Zhu Yib481de92007-09-25 17:54:57 -07006112 goto done;
6113 }
6114
6115 /* select Rx antennas */
6116 scan->flags |= iwl3945_get_antenna_flags(priv);
6117
Johannes Berg05c914f2008-09-11 00:01:58 +02006118 if (priv->iw_mode == NL80211_IFTYPE_MONITOR)
Zhu Yib481de92007-09-25 17:54:57 -07006119 scan->filter_flags = RXON_FILTER_PROMISC_MSK;
6120
Abhijeet Kolekarf9340522008-09-03 11:26:58 +08006121 scan->channel_count =
6122 iwl3945_get_channels_for_scan(priv, band, 1, /* active */
6123 n_probes,
6124 (void *)&scan->data[le16_to_cpu(scan->tx_cmd.len)]);
Zhu Yib481de92007-09-25 17:54:57 -07006125
Reinette Chatre14b54332008-11-04 12:21:35 -08006126 if (scan->channel_count == 0) {
6127 IWL_DEBUG_SCAN("channel count %d\n", scan->channel_count);
6128 goto done;
6129 }
6130
Zhu Yib481de92007-09-25 17:54:57 -07006131 cmd.len += le16_to_cpu(scan->tx_cmd.len) +
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006132 scan->channel_count * sizeof(struct iwl3945_scan_channel);
Zhu Yib481de92007-09-25 17:54:57 -07006133 cmd.data = scan;
6134 scan->len = cpu_to_le16(cmd.len);
6135
6136 set_bit(STATUS_SCAN_HW, &priv->status);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006137 rc = iwl3945_send_cmd_sync(priv, &cmd);
Zhu Yib481de92007-09-25 17:54:57 -07006138 if (rc)
6139 goto done;
6140
6141 queue_delayed_work(priv->workqueue, &priv->scan_check,
6142 IWL_SCAN_CHECK_WATCHDOG);
6143
6144 mutex_unlock(&priv->mutex);
6145 return;
6146
6147 done:
Mohamed Abbas2420ebc2008-11-04 12:21:34 -08006148 /* can not perform scan make sure we clear scanning
6149 * bits from status so next scan request can be performed.
6150 * if we dont clear scanning status bit here all next scan
6151 * will fail
6152 */
6153 clear_bit(STATUS_SCAN_HW, &priv->status);
6154 clear_bit(STATUS_SCANNING, &priv->status);
6155
Ian Schram01ebd062007-10-25 17:15:22 +08006156 /* inform mac80211 scan aborted */
Zhu Yib481de92007-09-25 17:54:57 -07006157 queue_work(priv->workqueue, &priv->scan_completed);
6158 mutex_unlock(&priv->mutex);
6159}
6160
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006161static void iwl3945_bg_up(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07006162{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08006163 struct iwl_priv *priv = container_of(data, struct iwl_priv, up);
Zhu Yib481de92007-09-25 17:54:57 -07006164
6165 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
6166 return;
6167
6168 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006169 __iwl3945_up(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006170 mutex_unlock(&priv->mutex);
Adel Gadllah80fcc9e2008-07-01 17:49:50 +02006171 iwl3945_rfkill_set_hw_state(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006172}
6173
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006174static void iwl3945_bg_restart(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07006175{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08006176 struct iwl_priv *priv = container_of(data, struct iwl_priv, restart);
Zhu Yib481de92007-09-25 17:54:57 -07006177
6178 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
6179 return;
6180
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006181 iwl3945_down(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006182 queue_work(priv->workqueue, &priv->up);
6183}
6184
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006185static void iwl3945_bg_rx_replenish(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07006186{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08006187 struct iwl_priv *priv =
6188 container_of(data, struct iwl_priv, rx_replenish);
Zhu Yib481de92007-09-25 17:54:57 -07006189
6190 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
6191 return;
6192
6193 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006194 iwl3945_rx_replenish(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006195 mutex_unlock(&priv->mutex);
6196}
6197
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08006198#define IWL_DELAY_NEXT_SCAN (HZ*2)
6199
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08006200static void iwl3945_post_associate(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07006201{
Zhu Yib481de92007-09-25 17:54:57 -07006202 int rc = 0;
6203 struct ieee80211_conf *conf = NULL;
6204
Johannes Berg05c914f2008-09-11 00:01:58 +02006205 if (priv->iw_mode == NL80211_IFTYPE_AP) {
Tomas Winkler3ac7f142008-07-21 02:40:14 +03006206 IWL_ERROR("%s Should not be called in AP mode\n", __func__);
Zhu Yib481de92007-09-25 17:54:57 -07006207 return;
6208 }
6209
6210
Johannes Berge1749612008-10-27 15:59:26 -07006211 IWL_DEBUG_ASSOC("Associated as %d to: %pM\n",
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08006212 priv->assoc_id, priv->active39_rxon.bssid_addr);
Zhu Yib481de92007-09-25 17:54:57 -07006213
6214 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
6215 return;
6216
Abhijeet Kolekar322a9812008-09-03 11:26:27 +08006217 if (!priv->vif || !priv->is_open)
Mohamed Abbas6ef89d02007-10-25 17:15:47 +08006218 return;
Abhijeet Kolekar322a9812008-09-03 11:26:27 +08006219
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006220 iwl3945_scan_cancel_timeout(priv, 200);
Mohamed Abbas15e869d2007-10-25 17:15:46 +08006221
Zhu Yib481de92007-09-25 17:54:57 -07006222 conf = ieee80211_get_hw_conf(priv->hw);
6223
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08006224 priv->staging39_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006225 iwl3945_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006226
Tomas Winkler28afaf92008-12-19 10:37:06 +08006227 memset(&priv->rxon_timing, 0, sizeof(struct iwl_rxon_time_cmd));
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006228 iwl3945_setup_rxon_timing(priv);
6229 rc = iwl3945_send_cmd_pdu(priv, REPLY_RXON_TIMING,
Zhu Yib481de92007-09-25 17:54:57 -07006230 sizeof(priv->rxon_timing), &priv->rxon_timing);
6231 if (rc)
Winkler, Tomas39aadf82008-12-19 10:37:32 +08006232 IWL_WARN(priv, "REPLY_RXON_TIMING failed - "
Zhu Yib481de92007-09-25 17:54:57 -07006233 "Attempting to continue.\n");
6234
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08006235 priv->staging39_rxon.filter_flags |= RXON_FILTER_ASSOC_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07006236
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08006237 priv->staging39_rxon.assoc_id = cpu_to_le16(priv->assoc_id);
Zhu Yib481de92007-09-25 17:54:57 -07006238
6239 IWL_DEBUG_ASSOC("assoc id %d beacon interval %d\n",
6240 priv->assoc_id, priv->beacon_int);
6241
6242 if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_PREAMBLE)
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08006243 priv->staging39_rxon.flags |= RXON_FLG_SHORT_PREAMBLE_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07006244 else
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08006245 priv->staging39_rxon.flags &= ~RXON_FLG_SHORT_PREAMBLE_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07006246
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08006247 if (priv->staging39_rxon.flags & RXON_FLG_BAND_24G_MSK) {
Zhu Yib481de92007-09-25 17:54:57 -07006248 if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_SLOT_TIME)
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08006249 priv->staging39_rxon.flags |= RXON_FLG_SHORT_SLOT_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07006250 else
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08006251 priv->staging39_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07006252
Johannes Berg05c914f2008-09-11 00:01:58 +02006253 if (priv->iw_mode == NL80211_IFTYPE_ADHOC)
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08006254 priv->staging39_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07006255
6256 }
6257
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006258 iwl3945_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006259
6260 switch (priv->iw_mode) {
Johannes Berg05c914f2008-09-11 00:01:58 +02006261 case NL80211_IFTYPE_STATION:
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006262 iwl3945_rate_scale_init(priv->hw, IWL_AP_ID);
Zhu Yib481de92007-09-25 17:54:57 -07006263 break;
6264
Johannes Berg05c914f2008-09-11 00:01:58 +02006265 case NL80211_IFTYPE_ADHOC:
Zhu Yib481de92007-09-25 17:54:57 -07006266
Abhijeet Kolekarce546fd2008-11-19 15:32:22 -08006267 priv->assoc_id = 1;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006268 iwl3945_add_station(priv, priv->bssid, 0, 0);
Zhu Yib481de92007-09-25 17:54:57 -07006269 iwl3945_sync_sta(priv, IWL_STA_ID,
Johannes Berg8318d782008-01-24 19:38:38 +01006270 (priv->band == IEEE80211_BAND_5GHZ) ?
Zhu Yib481de92007-09-25 17:54:57 -07006271 IWL_RATE_6M_PLCP : IWL_RATE_1M_PLCP,
6272 CMD_ASYNC);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006273 iwl3945_rate_scale_init(priv->hw, IWL_STA_ID);
6274 iwl3945_send_beacon_cmd(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006275
6276 break;
6277
6278 default:
6279 IWL_ERROR("%s Should not be called in %d mode\n",
Tomas Winkler3ac7f142008-07-21 02:40:14 +03006280 __func__, priv->iw_mode);
Zhu Yib481de92007-09-25 17:54:57 -07006281 break;
6282 }
6283
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006284 iwl3945_activate_qos(priv, 0);
Ron Rindjunsky292ae172008-02-06 11:20:39 -08006285
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08006286 /* we have just associated, don't start scan too early */
6287 priv->next_scan_jiffies = jiffies + IWL_DELAY_NEXT_SCAN;
Abhijeet Kolekarcd56d332008-09-03 11:26:21 +08006288}
6289
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006290static void iwl3945_bg_abort_scan(struct work_struct *work)
Zhu Yib481de92007-09-25 17:54:57 -07006291{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08006292 struct iwl_priv *priv = container_of(work, struct iwl_priv, abort_scan);
Zhu Yib481de92007-09-25 17:54:57 -07006293
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006294 if (!iwl3945_is_ready(priv))
Zhu Yib481de92007-09-25 17:54:57 -07006295 return;
6296
6297 mutex_lock(&priv->mutex);
6298
6299 set_bit(STATUS_SCAN_ABORTING, &priv->status);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006300 iwl3945_send_scan_abort(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006301
6302 mutex_unlock(&priv->mutex);
6303}
6304
Johannes Berge8975582008-10-09 12:18:51 +02006305static int iwl3945_mac_config(struct ieee80211_hw *hw, u32 changed);
Zhu Yi76bb77e2007-11-22 10:53:22 +08006306
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006307static void iwl3945_bg_scan_completed(struct work_struct *work)
Zhu Yib481de92007-09-25 17:54:57 -07006308{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08006309 struct iwl_priv *priv =
6310 container_of(work, struct iwl_priv, scan_completed);
Zhu Yib481de92007-09-25 17:54:57 -07006311
6312 IWL_DEBUG(IWL_DL_INFO | IWL_DL_SCAN, "SCAN complete scan\n");
6313
6314 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
6315 return;
6316
Zhu Yia0646472007-12-20 14:10:01 +08006317 if (test_bit(STATUS_CONF_PENDING, &priv->status))
Johannes Berge8975582008-10-09 12:18:51 +02006318 iwl3945_mac_config(priv->hw, 0);
Zhu Yi76bb77e2007-11-22 10:53:22 +08006319
Zhu Yib481de92007-09-25 17:54:57 -07006320 ieee80211_scan_completed(priv->hw);
6321
6322 /* Since setting the TXPOWER may have been deferred while
6323 * performing the scan, fire one off */
6324 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006325 iwl3945_hw_reg_send_txpower(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006326 mutex_unlock(&priv->mutex);
6327}
6328
6329/*****************************************************************************
6330 *
6331 * mac80211 entry point functions
6332 *
6333 *****************************************************************************/
6334
Zhu Yi5a669262008-01-14 17:46:18 -08006335#define UCODE_READY_TIMEOUT (2 * HZ)
6336
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006337static int iwl3945_mac_start(struct ieee80211_hw *hw)
Zhu Yib481de92007-09-25 17:54:57 -07006338{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08006339 struct iwl_priv *priv = hw->priv;
Zhu Yi5a669262008-01-14 17:46:18 -08006340 int ret;
Zhu Yib481de92007-09-25 17:54:57 -07006341
6342 IWL_DEBUG_MAC80211("enter\n");
6343
Zhu Yi5a669262008-01-14 17:46:18 -08006344 if (pci_enable_device(priv->pci_dev)) {
6345 IWL_ERROR("Fail to pci_enable_device\n");
6346 return -ENODEV;
6347 }
6348 pci_restore_state(priv->pci_dev);
6349 pci_enable_msi(priv->pci_dev);
6350
6351 ret = request_irq(priv->pci_dev->irq, iwl3945_isr, IRQF_SHARED,
6352 DRV_NAME, priv);
6353 if (ret) {
6354 IWL_ERROR("Error allocating IRQ %d\n", priv->pci_dev->irq);
6355 goto out_disable_msi;
6356 }
6357
Zhu Yib481de92007-09-25 17:54:57 -07006358 /* we should be verifying the device is ready to be opened */
6359 mutex_lock(&priv->mutex);
6360
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08006361 memset(&priv->staging39_rxon, 0, sizeof(struct iwl3945_rxon_cmd));
Zhu Yi5a669262008-01-14 17:46:18 -08006362 /* fetch ucode file from disk, alloc and copy to bus-master buffers ...
6363 * ucode filename and max sizes are card-specific. */
6364
6365 if (!priv->ucode_code.len) {
6366 ret = iwl3945_read_ucode(priv);
6367 if (ret) {
6368 IWL_ERROR("Could not read microcode: %d\n", ret);
6369 mutex_unlock(&priv->mutex);
6370 goto out_release_irq;
6371 }
6372 }
6373
Zhu Yie655b9f2008-01-24 02:19:38 -08006374 ret = __iwl3945_up(priv);
Zhu Yi5a669262008-01-14 17:46:18 -08006375
Zhu Yib481de92007-09-25 17:54:57 -07006376 mutex_unlock(&priv->mutex);
Zhu Yi5a669262008-01-14 17:46:18 -08006377
Adel Gadllah80fcc9e2008-07-01 17:49:50 +02006378 iwl3945_rfkill_set_hw_state(priv);
6379
Zhu Yie655b9f2008-01-24 02:19:38 -08006380 if (ret)
6381 goto out_release_irq;
6382
6383 IWL_DEBUG_INFO("Start UP work.\n");
6384
6385 if (test_bit(STATUS_IN_SUSPEND, &priv->status))
6386 return 0;
6387
Zhu Yi5a669262008-01-14 17:46:18 -08006388 /* Wait for START_ALIVE from ucode. Otherwise callbacks from
6389 * mac80211 will not be run successfully. */
6390 ret = wait_event_interruptible_timeout(priv->wait_command_queue,
6391 test_bit(STATUS_READY, &priv->status),
6392 UCODE_READY_TIMEOUT);
6393 if (!ret) {
6394 if (!test_bit(STATUS_READY, &priv->status)) {
6395 IWL_ERROR("Wait for START_ALIVE timeout after %dms.\n",
6396 jiffies_to_msecs(UCODE_READY_TIMEOUT));
6397 ret = -ETIMEDOUT;
6398 goto out_release_irq;
6399 }
6400 }
6401
Zhu Yie655b9f2008-01-24 02:19:38 -08006402 priv->is_open = 1;
Zhu Yib481de92007-09-25 17:54:57 -07006403 IWL_DEBUG_MAC80211("leave\n");
6404 return 0;
Zhu Yi5a669262008-01-14 17:46:18 -08006405
6406out_release_irq:
6407 free_irq(priv->pci_dev->irq, priv);
6408out_disable_msi:
6409 pci_disable_msi(priv->pci_dev);
Zhu Yie655b9f2008-01-24 02:19:38 -08006410 pci_disable_device(priv->pci_dev);
6411 priv->is_open = 0;
6412 IWL_DEBUG_MAC80211("leave - failed\n");
Zhu Yi5a669262008-01-14 17:46:18 -08006413 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07006414}
6415
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006416static void iwl3945_mac_stop(struct ieee80211_hw *hw)
Zhu Yib481de92007-09-25 17:54:57 -07006417{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08006418 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07006419
6420 IWL_DEBUG_MAC80211("enter\n");
Mohamed Abbas6ef89d02007-10-25 17:15:47 +08006421
Zhu Yie655b9f2008-01-24 02:19:38 -08006422 if (!priv->is_open) {
6423 IWL_DEBUG_MAC80211("leave - skip\n");
6424 return;
6425 }
6426
Zhu Yib481de92007-09-25 17:54:57 -07006427 priv->is_open = 0;
Zhu Yi5a669262008-01-14 17:46:18 -08006428
6429 if (iwl3945_is_ready_rf(priv)) {
Zhu Yie655b9f2008-01-24 02:19:38 -08006430 /* stop mac, cancel any scan request and clear
6431 * RXON_FILTER_ASSOC_MSK BIT
6432 */
Zhu Yi5a669262008-01-14 17:46:18 -08006433 mutex_lock(&priv->mutex);
6434 iwl3945_scan_cancel_timeout(priv, 100);
Mohamed Abbasfde35712007-11-29 11:10:15 +08006435 mutex_unlock(&priv->mutex);
Mohamed Abbasfde35712007-11-29 11:10:15 +08006436 }
6437
Zhu Yi5a669262008-01-14 17:46:18 -08006438 iwl3945_down(priv);
6439
6440 flush_workqueue(priv->workqueue);
6441 free_irq(priv->pci_dev->irq, priv);
6442 pci_disable_msi(priv->pci_dev);
6443 pci_save_state(priv->pci_dev);
6444 pci_disable_device(priv->pci_dev);
Mohamed Abbas6ef89d02007-10-25 17:15:47 +08006445
Zhu Yib481de92007-09-25 17:54:57 -07006446 IWL_DEBUG_MAC80211("leave\n");
Zhu Yib481de92007-09-25 17:54:57 -07006447}
6448
Johannes Berge039fa42008-05-15 12:55:29 +02006449static int iwl3945_mac_tx(struct ieee80211_hw *hw, struct sk_buff *skb)
Zhu Yib481de92007-09-25 17:54:57 -07006450{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08006451 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07006452
6453 IWL_DEBUG_MAC80211("enter\n");
6454
Zhu Yib481de92007-09-25 17:54:57 -07006455 IWL_DEBUG_TX("dev->xmit(%d bytes) at rate 0x%02x\n", skb->len,
Johannes Berge039fa42008-05-15 12:55:29 +02006456 ieee80211_get_tx_rate(hw, IEEE80211_SKB_CB(skb))->bitrate);
Zhu Yib481de92007-09-25 17:54:57 -07006457
Johannes Berge039fa42008-05-15 12:55:29 +02006458 if (iwl3945_tx_skb(priv, skb))
Zhu Yib481de92007-09-25 17:54:57 -07006459 dev_kfree_skb_any(skb);
6460
6461 IWL_DEBUG_MAC80211("leave\n");
Reinette Chatre637f8832009-01-19 15:30:32 -08006462 return NETDEV_TX_OK;
Zhu Yib481de92007-09-25 17:54:57 -07006463}
6464
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006465static int iwl3945_mac_add_interface(struct ieee80211_hw *hw,
Zhu Yib481de92007-09-25 17:54:57 -07006466 struct ieee80211_if_init_conf *conf)
6467{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08006468 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07006469 unsigned long flags;
6470
Johannes Berg32bfd352007-12-19 01:31:26 +01006471 IWL_DEBUG_MAC80211("enter: type %d\n", conf->type);
Zhu Yib481de92007-09-25 17:54:57 -07006472
Johannes Berg32bfd352007-12-19 01:31:26 +01006473 if (priv->vif) {
6474 IWL_DEBUG_MAC80211("leave - vif != NULL\n");
Tomas Winkler864792e2007-11-27 21:00:52 +02006475 return -EOPNOTSUPP;
Zhu Yib481de92007-09-25 17:54:57 -07006476 }
6477
6478 spin_lock_irqsave(&priv->lock, flags);
Johannes Berg32bfd352007-12-19 01:31:26 +01006479 priv->vif = conf->vif;
Zhu, Yi60294de2008-10-29 14:05:45 -07006480 priv->iw_mode = conf->type;
Zhu Yib481de92007-09-25 17:54:57 -07006481
6482 spin_unlock_irqrestore(&priv->lock, flags);
6483
6484 mutex_lock(&priv->mutex);
Tomas Winkler864792e2007-11-27 21:00:52 +02006485
6486 if (conf->mac_addr) {
Johannes Berge1749612008-10-27 15:59:26 -07006487 IWL_DEBUG_MAC80211("Set: %pM\n", conf->mac_addr);
Tomas Winkler864792e2007-11-27 21:00:52 +02006488 memcpy(priv->mac_addr, conf->mac_addr, ETH_ALEN);
6489 }
6490
Zhu Yi5a669262008-01-14 17:46:18 -08006491 if (iwl3945_is_ready(priv))
6492 iwl3945_set_mode(priv, conf->type);
Zhu Yib481de92007-09-25 17:54:57 -07006493
Zhu Yib481de92007-09-25 17:54:57 -07006494 mutex_unlock(&priv->mutex);
6495
Zhu Yi5a669262008-01-14 17:46:18 -08006496 IWL_DEBUG_MAC80211("leave\n");
Zhu Yib481de92007-09-25 17:54:57 -07006497 return 0;
6498}
6499
6500/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006501 * iwl3945_mac_config - mac80211 config callback
Zhu Yib481de92007-09-25 17:54:57 -07006502 *
6503 * We ignore conf->flags & IEEE80211_CONF_SHORT_SLOT_TIME since it seems to
6504 * be set inappropriately and the driver currently sets the hardware up to
6505 * use it whenever needed.
6506 */
Johannes Berge8975582008-10-09 12:18:51 +02006507static int iwl3945_mac_config(struct ieee80211_hw *hw, u32 changed)
Zhu Yib481de92007-09-25 17:54:57 -07006508{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08006509 struct iwl_priv *priv = hw->priv;
Samuel Ortizd20b3c62008-12-19 10:37:15 +08006510 const struct iwl_channel_info *ch_info;
Johannes Berge8975582008-10-09 12:18:51 +02006511 struct ieee80211_conf *conf = &hw->conf;
Zhu Yib481de92007-09-25 17:54:57 -07006512 unsigned long flags;
Zhu Yi76bb77e2007-11-22 10:53:22 +08006513 int ret = 0;
Zhu Yib481de92007-09-25 17:54:57 -07006514
6515 mutex_lock(&priv->mutex);
Johannes Berg8318d782008-01-24 19:38:38 +01006516 IWL_DEBUG_MAC80211("enter to channel %d\n", conf->channel->hw_value);
Zhu Yib481de92007-09-25 17:54:57 -07006517
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006518 if (!iwl3945_is_ready(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07006519 IWL_DEBUG_MAC80211("leave - not ready\n");
Zhu Yi76bb77e2007-11-22 10:53:22 +08006520 ret = -EIO;
6521 goto out;
Zhu Yib481de92007-09-25 17:54:57 -07006522 }
6523
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006524 if (unlikely(!iwl3945_param_disable_hw_scan &&
Zhu Yib481de92007-09-25 17:54:57 -07006525 test_bit(STATUS_SCANNING, &priv->status))) {
Zhu Yia0646472007-12-20 14:10:01 +08006526 IWL_DEBUG_MAC80211("leave - scanning\n");
6527 set_bit(STATUS_CONF_PENDING, &priv->status);
Zhu Yib481de92007-09-25 17:54:57 -07006528 mutex_unlock(&priv->mutex);
Zhu Yia0646472007-12-20 14:10:01 +08006529 return 0;
Zhu Yib481de92007-09-25 17:54:57 -07006530 }
6531
6532 spin_lock_irqsave(&priv->lock, flags);
6533
Johannes Berg8318d782008-01-24 19:38:38 +01006534 ch_info = iwl3945_get_channel_info(priv, conf->channel->band,
6535 conf->channel->hw_value);
Zhu Yib481de92007-09-25 17:54:57 -07006536 if (!is_channel_valid(ch_info)) {
Ron Rindjunsky66b50042008-06-25 16:46:31 +08006537 IWL_DEBUG_SCAN("Channel %d [%d] is INVALID for this band.\n",
Johannes Berg8318d782008-01-24 19:38:38 +01006538 conf->channel->hw_value, conf->channel->band);
Zhu Yib481de92007-09-25 17:54:57 -07006539 IWL_DEBUG_MAC80211("leave - invalid channel\n");
6540 spin_unlock_irqrestore(&priv->lock, flags);
Zhu Yi76bb77e2007-11-22 10:53:22 +08006541 ret = -EINVAL;
6542 goto out;
Zhu Yib481de92007-09-25 17:54:57 -07006543 }
6544
Johannes Berg8318d782008-01-24 19:38:38 +01006545 iwl3945_set_rxon_channel(priv, conf->channel->band, conf->channel->hw_value);
Zhu Yib481de92007-09-25 17:54:57 -07006546
Johannes Berg8318d782008-01-24 19:38:38 +01006547 iwl3945_set_flags_for_phymode(priv, conf->channel->band);
Zhu Yib481de92007-09-25 17:54:57 -07006548
6549 /* The list of supported rates and rate mask can be different
6550 * for each phymode; since the phymode may have changed, reset
6551 * the rate mask to what mac80211 lists */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006552 iwl3945_set_rate(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006553
6554 spin_unlock_irqrestore(&priv->lock, flags);
6555
6556#ifdef IEEE80211_CONF_CHANNEL_SWITCH
6557 if (conf->flags & IEEE80211_CONF_CHANNEL_SWITCH) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006558 iwl3945_hw_channel_switch(priv, conf->channel);
Zhu Yi76bb77e2007-11-22 10:53:22 +08006559 goto out;
Zhu Yib481de92007-09-25 17:54:57 -07006560 }
6561#endif
6562
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006563 iwl3945_radio_kill_sw(priv, !conf->radio_enabled);
Zhu Yib481de92007-09-25 17:54:57 -07006564
6565 if (!conf->radio_enabled) {
6566 IWL_DEBUG_MAC80211("leave - radio disabled\n");
Zhu Yi76bb77e2007-11-22 10:53:22 +08006567 goto out;
Zhu Yib481de92007-09-25 17:54:57 -07006568 }
6569
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006570 if (iwl3945_is_rfkill(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07006571 IWL_DEBUG_MAC80211("leave - RF kill\n");
Zhu Yi76bb77e2007-11-22 10:53:22 +08006572 ret = -EIO;
6573 goto out;
Zhu Yib481de92007-09-25 17:54:57 -07006574 }
6575
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006576 iwl3945_set_rate(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006577
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08006578 if (memcmp(&priv->active39_rxon,
6579 &priv->staging39_rxon, sizeof(priv->staging39_rxon)))
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006580 iwl3945_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006581 else
6582 IWL_DEBUG_INFO("No re-sending same RXON configuration.\n");
6583
6584 IWL_DEBUG_MAC80211("leave\n");
6585
Zhu Yi76bb77e2007-11-22 10:53:22 +08006586out:
Zhu Yia0646472007-12-20 14:10:01 +08006587 clear_bit(STATUS_CONF_PENDING, &priv->status);
Zhu Yib481de92007-09-25 17:54:57 -07006588 mutex_unlock(&priv->mutex);
Zhu Yi76bb77e2007-11-22 10:53:22 +08006589 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07006590}
6591
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08006592static void iwl3945_config_ap(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07006593{
6594 int rc = 0;
6595
Maarten Lankhorstd986bcd2008-01-23 10:15:16 -08006596 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
Zhu Yib481de92007-09-25 17:54:57 -07006597 return;
6598
6599 /* The following should be done only at AP bring up */
Ron Rindjunsky5d1e2322008-06-30 17:23:04 +08006600 if (!(iwl3945_is_associated(priv))) {
Zhu Yib481de92007-09-25 17:54:57 -07006601
6602 /* RXON - unassoc (to set timing command) */
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08006603 priv->staging39_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006604 iwl3945_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006605
6606 /* RXON Timing */
Tomas Winkler28afaf92008-12-19 10:37:06 +08006607 memset(&priv->rxon_timing, 0, sizeof(struct iwl_rxon_time_cmd));
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006608 iwl3945_setup_rxon_timing(priv);
6609 rc = iwl3945_send_cmd_pdu(priv, REPLY_RXON_TIMING,
Zhu Yib481de92007-09-25 17:54:57 -07006610 sizeof(priv->rxon_timing), &priv->rxon_timing);
6611 if (rc)
Winkler, Tomas39aadf82008-12-19 10:37:32 +08006612 IWL_WARN(priv, "REPLY_RXON_TIMING failed - "
Zhu Yib481de92007-09-25 17:54:57 -07006613 "Attempting to continue.\n");
6614
6615 /* FIXME: what should be the assoc_id for AP? */
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08006616 priv->staging39_rxon.assoc_id = cpu_to_le16(priv->assoc_id);
Zhu Yib481de92007-09-25 17:54:57 -07006617 if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_PREAMBLE)
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08006618 priv->staging39_rxon.flags |=
Zhu Yib481de92007-09-25 17:54:57 -07006619 RXON_FLG_SHORT_PREAMBLE_MSK;
6620 else
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08006621 priv->staging39_rxon.flags &=
Zhu Yib481de92007-09-25 17:54:57 -07006622 ~RXON_FLG_SHORT_PREAMBLE_MSK;
6623
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08006624 if (priv->staging39_rxon.flags & RXON_FLG_BAND_24G_MSK) {
Zhu Yib481de92007-09-25 17:54:57 -07006625 if (priv->assoc_capability &
6626 WLAN_CAPABILITY_SHORT_SLOT_TIME)
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08006627 priv->staging39_rxon.flags |=
Zhu Yib481de92007-09-25 17:54:57 -07006628 RXON_FLG_SHORT_SLOT_MSK;
6629 else
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08006630 priv->staging39_rxon.flags &=
Zhu Yib481de92007-09-25 17:54:57 -07006631 ~RXON_FLG_SHORT_SLOT_MSK;
6632
Johannes Berg05c914f2008-09-11 00:01:58 +02006633 if (priv->iw_mode == NL80211_IFTYPE_ADHOC)
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08006634 priv->staging39_rxon.flags &=
Zhu Yib481de92007-09-25 17:54:57 -07006635 ~RXON_FLG_SHORT_SLOT_MSK;
6636 }
6637 /* restore RXON assoc */
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08006638 priv->staging39_rxon.filter_flags |= RXON_FILTER_ASSOC_MSK;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006639 iwl3945_commit_rxon(priv);
Kolekar, Abhijeetb5323d32008-12-19 10:37:22 +08006640 iwl3945_add_station(priv, iwl_bcast_addr, 0, 0);
Zhu Yi556f8db2007-09-27 11:27:33 +08006641 }
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006642 iwl3945_send_beacon_cmd(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006643
6644 /* FIXME - we need to add code here to detect a totally new
6645 * configuration, reset the AP, unassoc, rxon timing, assoc,
6646 * clear sta table, add BCAST sta... */
6647}
6648
Johannes Berg32bfd352007-12-19 01:31:26 +01006649static int iwl3945_mac_config_interface(struct ieee80211_hw *hw,
6650 struct ieee80211_vif *vif,
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08006651 struct ieee80211_if_conf *conf)
Zhu Yib481de92007-09-25 17:54:57 -07006652{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08006653 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07006654 int rc;
6655
6656 if (conf == NULL)
6657 return -EIO;
6658
Emmanuel Grumbachb716bb92008-03-04 18:09:32 -08006659 if (priv->vif != vif) {
6660 IWL_DEBUG_MAC80211("leave - priv->vif != vif\n");
Emmanuel Grumbachb716bb92008-03-04 18:09:32 -08006661 return 0;
6662 }
6663
Johannes Berg9d139c82008-07-09 14:40:37 +02006664 /* handle this temporarily here */
Johannes Berg05c914f2008-09-11 00:01:58 +02006665 if (priv->iw_mode == NL80211_IFTYPE_ADHOC &&
Johannes Berg9d139c82008-07-09 14:40:37 +02006666 conf->changed & IEEE80211_IFCC_BEACON) {
6667 struct sk_buff *beacon = ieee80211_beacon_get(hw, vif);
6668 if (!beacon)
6669 return -ENOMEM;
Mohamed Abbas9bdf5ec2008-11-07 09:58:35 -08006670 mutex_lock(&priv->mutex);
Johannes Berg9d139c82008-07-09 14:40:37 +02006671 rc = iwl3945_mac_beacon_update(hw, beacon);
Mohamed Abbas9bdf5ec2008-11-07 09:58:35 -08006672 mutex_unlock(&priv->mutex);
Johannes Berg9d139c82008-07-09 14:40:37 +02006673 if (rc)
6674 return rc;
6675 }
6676
Zhu Yi5a669262008-01-14 17:46:18 -08006677 if (!iwl3945_is_alive(priv))
6678 return -EAGAIN;
6679
Zhu Yib481de92007-09-25 17:54:57 -07006680 mutex_lock(&priv->mutex);
6681
Zhu Yib481de92007-09-25 17:54:57 -07006682 if (conf->bssid)
Johannes Berge1749612008-10-27 15:59:26 -07006683 IWL_DEBUG_MAC80211("bssid: %pM\n", conf->bssid);
Zhu Yib481de92007-09-25 17:54:57 -07006684
Johannes Berg4150c572007-09-17 01:29:23 -04006685/*
6686 * very dubious code was here; the probe filtering flag is never set:
6687 *
Zhu Yib481de92007-09-25 17:54:57 -07006688 if (unlikely(test_bit(STATUS_SCANNING, &priv->status)) &&
6689 !(priv->hw->flags & IEEE80211_HW_NO_PROBE_FILTERING)) {
Johannes Berg4150c572007-09-17 01:29:23 -04006690 */
Zhu Yib481de92007-09-25 17:54:57 -07006691
Johannes Berg05c914f2008-09-11 00:01:58 +02006692 if (priv->iw_mode == NL80211_IFTYPE_AP) {
Zhu Yib481de92007-09-25 17:54:57 -07006693 if (!conf->bssid) {
6694 conf->bssid = priv->mac_addr;
6695 memcpy(priv->bssid, priv->mac_addr, ETH_ALEN);
Johannes Berge1749612008-10-27 15:59:26 -07006696 IWL_DEBUG_MAC80211("bssid was set to: %pM\n",
6697 conf->bssid);
Zhu Yib481de92007-09-25 17:54:57 -07006698 }
6699 if (priv->ibss_beacon)
6700 dev_kfree_skb(priv->ibss_beacon);
6701
Johannes Berg9d139c82008-07-09 14:40:37 +02006702 priv->ibss_beacon = ieee80211_beacon_get(hw, vif);
Zhu Yib481de92007-09-25 17:54:57 -07006703 }
6704
Mohamed Abbasfde35712007-11-29 11:10:15 +08006705 if (iwl3945_is_rfkill(priv))
6706 goto done;
6707
Zhu Yib481de92007-09-25 17:54:57 -07006708 if (conf->bssid && !is_zero_ether_addr(conf->bssid) &&
6709 !is_multicast_ether_addr(conf->bssid)) {
6710 /* If there is currently a HW scan going on in the background
6711 * then we need to cancel it else the RXON below will fail. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006712 if (iwl3945_scan_cancel_timeout(priv, 100)) {
Winkler, Tomas39aadf82008-12-19 10:37:32 +08006713 IWL_WARN(priv, "Aborted scan still in progress "
Zhu Yib481de92007-09-25 17:54:57 -07006714 "after 100ms\n");
6715 IWL_DEBUG_MAC80211("leaving - scan abort failed.\n");
6716 mutex_unlock(&priv->mutex);
6717 return -EAGAIN;
6718 }
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08006719 memcpy(priv->staging39_rxon.bssid_addr, conf->bssid, ETH_ALEN);
Zhu Yib481de92007-09-25 17:54:57 -07006720
6721 /* TODO: Audit driver for usage of these members and see
6722 * if mac80211 deprecates them (priv->bssid looks like it
6723 * shouldn't be there, but I haven't scanned the IBSS code
6724 * to verify) - jpk */
6725 memcpy(priv->bssid, conf->bssid, ETH_ALEN);
6726
Johannes Berg05c914f2008-09-11 00:01:58 +02006727 if (priv->iw_mode == NL80211_IFTYPE_AP)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006728 iwl3945_config_ap(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006729 else {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006730 rc = iwl3945_commit_rxon(priv);
Johannes Berg05c914f2008-09-11 00:01:58 +02006731 if ((priv->iw_mode == NL80211_IFTYPE_STATION) && rc)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006732 iwl3945_add_station(priv,
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08006733 priv->active39_rxon.bssid_addr, 1, 0);
Zhu Yib481de92007-09-25 17:54:57 -07006734 }
6735
6736 } else {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006737 iwl3945_scan_cancel_timeout(priv, 100);
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08006738 priv->staging39_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006739 iwl3945_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006740 }
6741
Mohamed Abbasfde35712007-11-29 11:10:15 +08006742 done:
Zhu Yib481de92007-09-25 17:54:57 -07006743 IWL_DEBUG_MAC80211("leave\n");
6744 mutex_unlock(&priv->mutex);
6745
6746 return 0;
6747}
6748
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006749static void iwl3945_configure_filter(struct ieee80211_hw *hw,
Johannes Berg4150c572007-09-17 01:29:23 -04006750 unsigned int changed_flags,
6751 unsigned int *total_flags,
6752 int mc_count, struct dev_addr_list *mc_list)
6753{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08006754 struct iwl_priv *priv = hw->priv;
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08006755 __le32 *filter_flags = &priv->staging39_rxon.filter_flags;
Rick Farrington25b3f572008-06-30 17:23:28 +08006756
Zhu, Yi352bc8d2008-11-12 13:14:09 -08006757 IWL_DEBUG_MAC80211("Enter: changed: 0x%x, total: 0x%x\n",
6758 changed_flags, *total_flags);
6759
6760 if (changed_flags & (FIF_OTHER_BSS | FIF_PROMISC_IN_BSS)) {
6761 if (*total_flags & (FIF_OTHER_BSS | FIF_PROMISC_IN_BSS))
6762 *filter_flags |= RXON_FILTER_PROMISC_MSK;
6763 else
6764 *filter_flags &= ~RXON_FILTER_PROMISC_MSK;
Abhijeet Kolekar5ec03972008-05-05 10:22:48 +08006765 }
Zhu, Yi352bc8d2008-11-12 13:14:09 -08006766 if (changed_flags & FIF_ALLMULTI) {
6767 if (*total_flags & FIF_ALLMULTI)
6768 *filter_flags |= RXON_FILTER_ACCEPT_GRP_MSK;
6769 else
6770 *filter_flags &= ~RXON_FILTER_ACCEPT_GRP_MSK;
6771 }
6772 if (changed_flags & FIF_CONTROL) {
6773 if (*total_flags & FIF_CONTROL)
6774 *filter_flags |= RXON_FILTER_CTL2HOST_MSK;
6775 else
6776 *filter_flags &= ~RXON_FILTER_CTL2HOST_MSK;
6777 }
6778 if (changed_flags & FIF_BCN_PRBRESP_PROMISC) {
6779 if (*total_flags & FIF_BCN_PRBRESP_PROMISC)
6780 *filter_flags |= RXON_FILTER_BCON_AWARE_MSK;
6781 else
6782 *filter_flags &= ~RXON_FILTER_BCON_AWARE_MSK;
6783 }
6784
6785 /* We avoid iwl_commit_rxon here to commit the new filter flags
6786 * since mac80211 will call ieee80211_hw_config immediately.
6787 * (mc_list is not supported at this time). Otherwise, we need to
6788 * queue a background iwl_commit_rxon work.
6789 */
6790
6791 *total_flags &= FIF_OTHER_BSS | FIF_ALLMULTI | FIF_PROMISC_IN_BSS |
Rick Farrington25b3f572008-06-30 17:23:28 +08006792 FIF_BCN_PRBRESP_PROMISC | FIF_CONTROL;
Johannes Berg4150c572007-09-17 01:29:23 -04006793}
6794
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006795static void iwl3945_mac_remove_interface(struct ieee80211_hw *hw,
Zhu Yib481de92007-09-25 17:54:57 -07006796 struct ieee80211_if_init_conf *conf)
6797{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08006798 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07006799
6800 IWL_DEBUG_MAC80211("enter\n");
6801
6802 mutex_lock(&priv->mutex);
Mohamed Abbas6ef89d02007-10-25 17:15:47 +08006803
Mohamed Abbasfde35712007-11-29 11:10:15 +08006804 if (iwl3945_is_ready_rf(priv)) {
6805 iwl3945_scan_cancel_timeout(priv, 100);
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08006806 priv->staging39_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
Mohamed Abbasfde35712007-11-29 11:10:15 +08006807 iwl3945_commit_rxon(priv);
6808 }
Johannes Berg32bfd352007-12-19 01:31:26 +01006809 if (priv->vif == conf->vif) {
6810 priv->vif = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07006811 memset(priv->bssid, 0, ETH_ALEN);
Zhu Yib481de92007-09-25 17:54:57 -07006812 }
6813 mutex_unlock(&priv->mutex);
6814
6815 IWL_DEBUG_MAC80211("leave\n");
Zhu Yib481de92007-09-25 17:54:57 -07006816}
6817
Abhijeet Kolekarcd56d332008-09-03 11:26:21 +08006818#define IWL_DELAY_NEXT_SCAN_AFTER_ASSOC (HZ*6)
6819
6820static void iwl3945_bss_info_changed(struct ieee80211_hw *hw,
6821 struct ieee80211_vif *vif,
6822 struct ieee80211_bss_conf *bss_conf,
6823 u32 changes)
6824{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08006825 struct iwl_priv *priv = hw->priv;
Abhijeet Kolekarcd56d332008-09-03 11:26:21 +08006826
6827 IWL_DEBUG_MAC80211("changes = 0x%X\n", changes);
6828
6829 if (changes & BSS_CHANGED_ERP_PREAMBLE) {
6830 IWL_DEBUG_MAC80211("ERP_PREAMBLE %d\n",
6831 bss_conf->use_short_preamble);
6832 if (bss_conf->use_short_preamble)
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08006833 priv->staging39_rxon.flags |= RXON_FLG_SHORT_PREAMBLE_MSK;
Abhijeet Kolekarcd56d332008-09-03 11:26:21 +08006834 else
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08006835 priv->staging39_rxon.flags &= ~RXON_FLG_SHORT_PREAMBLE_MSK;
Abhijeet Kolekarcd56d332008-09-03 11:26:21 +08006836 }
6837
6838 if (changes & BSS_CHANGED_ERP_CTS_PROT) {
6839 IWL_DEBUG_MAC80211("ERP_CTS %d\n", bss_conf->use_cts_prot);
6840 if (bss_conf->use_cts_prot && (priv->band != IEEE80211_BAND_5GHZ))
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08006841 priv->staging39_rxon.flags |= RXON_FLG_TGG_PROTECT_MSK;
Abhijeet Kolekarcd56d332008-09-03 11:26:21 +08006842 else
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08006843 priv->staging39_rxon.flags &= ~RXON_FLG_TGG_PROTECT_MSK;
Abhijeet Kolekarcd56d332008-09-03 11:26:21 +08006844 }
6845
6846 if (changes & BSS_CHANGED_ASSOC) {
6847 IWL_DEBUG_MAC80211("ASSOC %d\n", bss_conf->assoc);
6848 /* This should never happen as this function should
6849 * never be called from interrupt context. */
6850 if (WARN_ON_ONCE(in_interrupt()))
6851 return;
6852 if (bss_conf->assoc) {
6853 priv->assoc_id = bss_conf->aid;
6854 priv->beacon_int = bss_conf->beacon_int;
Tomas Winkler28afaf92008-12-19 10:37:06 +08006855 priv->timestamp = bss_conf->timestamp;
Abhijeet Kolekarcd56d332008-09-03 11:26:21 +08006856 priv->assoc_capability = bss_conf->assoc_capability;
6857 priv->next_scan_jiffies = jiffies +
6858 IWL_DELAY_NEXT_SCAN_AFTER_ASSOC;
6859 mutex_lock(&priv->mutex);
6860 iwl3945_post_associate(priv);
6861 mutex_unlock(&priv->mutex);
6862 } else {
6863 priv->assoc_id = 0;
6864 IWL_DEBUG_MAC80211("DISASSOC %d\n", bss_conf->assoc);
6865 }
6866 } else if (changes && iwl3945_is_associated(priv) && priv->assoc_id) {
6867 IWL_DEBUG_MAC80211("Associated Changes %d\n", changes);
6868 iwl3945_send_rxon_assoc(priv);
6869 }
6870
6871}
6872
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006873static int iwl3945_mac_hw_scan(struct ieee80211_hw *hw, u8 *ssid, size_t len)
Zhu Yib481de92007-09-25 17:54:57 -07006874{
6875 int rc = 0;
6876 unsigned long flags;
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08006877 struct iwl_priv *priv = hw->priv;
John W. Linville9387b7c2008-09-30 20:59:05 -04006878 DECLARE_SSID_BUF(ssid_buf);
Zhu Yib481de92007-09-25 17:54:57 -07006879
6880 IWL_DEBUG_MAC80211("enter\n");
6881
Mohamed Abbas15e869d2007-10-25 17:15:46 +08006882 mutex_lock(&priv->mutex);
Zhu Yib481de92007-09-25 17:54:57 -07006883 spin_lock_irqsave(&priv->lock, flags);
6884
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006885 if (!iwl3945_is_ready_rf(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07006886 rc = -EIO;
6887 IWL_DEBUG_MAC80211("leave - not ready or exit pending\n");
6888 goto out_unlock;
6889 }
6890
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08006891 /* we don't schedule scan within next_scan_jiffies period */
6892 if (priv->next_scan_jiffies &&
6893 time_after(priv->next_scan_jiffies, jiffies)) {
6894 rc = -EAGAIN;
6895 goto out_unlock;
6896 }
Bill Moss15dbf1b2008-05-06 11:05:15 +08006897 /* if we just finished scan ask for delay for a broadcast scan */
6898 if ((len == 0) && priv->last_scan_jiffies &&
6899 time_after(priv->last_scan_jiffies + IWL_DELAY_NEXT_SCAN,
6900 jiffies)) {
Zhu Yib481de92007-09-25 17:54:57 -07006901 rc = -EAGAIN;
6902 goto out_unlock;
6903 }
6904 if (len) {
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08006905 IWL_DEBUG_SCAN("direct scan for %s [%d]\n ",
John W. Linville9387b7c2008-09-30 20:59:05 -04006906 print_ssid(ssid_buf, ssid, len), (int)len);
Zhu Yib481de92007-09-25 17:54:57 -07006907
6908 priv->one_direct_scan = 1;
6909 priv->direct_ssid_len = (u8)
6910 min((u8) len, (u8) IW_ESSID_MAX_SIZE);
6911 memcpy(priv->direct_ssid, ssid, priv->direct_ssid_len);
Mohamed Abbas6ef89d02007-10-25 17:15:47 +08006912 } else
6913 priv->one_direct_scan = 0;
Zhu Yib481de92007-09-25 17:54:57 -07006914
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006915 rc = iwl3945_scan_initiate(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006916
6917 IWL_DEBUG_MAC80211("leave\n");
6918
6919out_unlock:
6920 spin_unlock_irqrestore(&priv->lock, flags);
Mohamed Abbas15e869d2007-10-25 17:15:46 +08006921 mutex_unlock(&priv->mutex);
Zhu Yib481de92007-09-25 17:54:57 -07006922
6923 return rc;
6924}
6925
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006926static int iwl3945_mac_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
Zhu Yib481de92007-09-25 17:54:57 -07006927 const u8 *local_addr, const u8 *addr,
6928 struct ieee80211_key_conf *key)
6929{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08006930 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07006931 int rc = 0;
6932 u8 sta_id;
6933
6934 IWL_DEBUG_MAC80211("enter\n");
6935
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006936 if (!iwl3945_param_hwcrypto) {
Zhu Yib481de92007-09-25 17:54:57 -07006937 IWL_DEBUG_MAC80211("leave - hwcrypto disabled\n");
6938 return -EOPNOTSUPP;
6939 }
6940
6941 if (is_zero_ether_addr(addr))
6942 /* only support pairwise keys */
6943 return -EOPNOTSUPP;
6944
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006945 sta_id = iwl3945_hw_find_station(priv, addr);
Zhu Yib481de92007-09-25 17:54:57 -07006946 if (sta_id == IWL_INVALID_STATION) {
Johannes Berge1749612008-10-27 15:59:26 -07006947 IWL_DEBUG_MAC80211("leave - %pM not in station map.\n",
6948 addr);
Zhu Yib481de92007-09-25 17:54:57 -07006949 return -EINVAL;
6950 }
6951
6952 mutex_lock(&priv->mutex);
6953
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006954 iwl3945_scan_cancel_timeout(priv, 100);
Mohamed Abbas15e869d2007-10-25 17:15:46 +08006955
Zhu Yib481de92007-09-25 17:54:57 -07006956 switch (cmd) {
6957 case SET_KEY:
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006958 rc = iwl3945_update_sta_key_info(priv, key, sta_id);
Zhu Yib481de92007-09-25 17:54:57 -07006959 if (!rc) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006960 iwl3945_set_rxon_hwcrypto(priv, 1);
6961 iwl3945_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006962 key->hw_key_idx = sta_id;
6963 IWL_DEBUG_MAC80211("set_key success, using hwcrypto\n");
6964 key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
6965 }
6966 break;
6967 case DISABLE_KEY:
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006968 rc = iwl3945_clear_sta_key_info(priv, sta_id);
Zhu Yib481de92007-09-25 17:54:57 -07006969 if (!rc) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006970 iwl3945_set_rxon_hwcrypto(priv, 0);
6971 iwl3945_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006972 IWL_DEBUG_MAC80211("disable hwcrypto key\n");
6973 }
6974 break;
6975 default:
6976 rc = -EINVAL;
6977 }
6978
6979 IWL_DEBUG_MAC80211("leave\n");
6980 mutex_unlock(&priv->mutex);
6981
6982 return rc;
6983}
6984
Johannes Berge100bb62008-04-30 18:51:21 +02006985static int iwl3945_mac_conf_tx(struct ieee80211_hw *hw, u16 queue,
Zhu Yib481de92007-09-25 17:54:57 -07006986 const struct ieee80211_tx_queue_params *params)
6987{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08006988 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07006989 unsigned long flags;
6990 int q;
Zhu Yib481de92007-09-25 17:54:57 -07006991
6992 IWL_DEBUG_MAC80211("enter\n");
6993
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006994 if (!iwl3945_is_ready_rf(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07006995 IWL_DEBUG_MAC80211("leave - RF not ready\n");
6996 return -EIO;
6997 }
6998
6999 if (queue >= AC_NUM) {
7000 IWL_DEBUG_MAC80211("leave - queue >= AC_NUM %d\n", queue);
7001 return 0;
7002 }
7003
Zhu Yib481de92007-09-25 17:54:57 -07007004 q = AC_NUM - 1 - queue;
7005
7006 spin_lock_irqsave(&priv->lock, flags);
7007
7008 priv->qos_data.def_qos_parm.ac[q].cw_min = cpu_to_le16(params->cw_min);
7009 priv->qos_data.def_qos_parm.ac[q].cw_max = cpu_to_le16(params->cw_max);
7010 priv->qos_data.def_qos_parm.ac[q].aifsn = params->aifs;
7011 priv->qos_data.def_qos_parm.ac[q].edca_txop =
Johannes Berg3330d7b2008-02-10 16:49:38 +01007012 cpu_to_le16((params->txop * 32));
Zhu Yib481de92007-09-25 17:54:57 -07007013
7014 priv->qos_data.def_qos_parm.ac[q].reserved1 = 0;
7015 priv->qos_data.qos_active = 1;
7016
7017 spin_unlock_irqrestore(&priv->lock, flags);
7018
7019 mutex_lock(&priv->mutex);
Johannes Berg05c914f2008-09-11 00:01:58 +02007020 if (priv->iw_mode == NL80211_IFTYPE_AP)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007021 iwl3945_activate_qos(priv, 1);
7022 else if (priv->assoc_id && iwl3945_is_associated(priv))
7023 iwl3945_activate_qos(priv, 0);
Zhu Yib481de92007-09-25 17:54:57 -07007024
7025 mutex_unlock(&priv->mutex);
7026
Zhu Yib481de92007-09-25 17:54:57 -07007027 IWL_DEBUG_MAC80211("leave\n");
7028 return 0;
7029}
7030
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007031static int iwl3945_mac_get_tx_stats(struct ieee80211_hw *hw,
Zhu Yib481de92007-09-25 17:54:57 -07007032 struct ieee80211_tx_queue_stats *stats)
7033{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08007034 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07007035 int i, avail;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007036 struct iwl3945_tx_queue *txq;
Samuel Ortizd20b3c62008-12-19 10:37:15 +08007037 struct iwl_queue *q;
Zhu Yib481de92007-09-25 17:54:57 -07007038 unsigned long flags;
7039
7040 IWL_DEBUG_MAC80211("enter\n");
7041
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007042 if (!iwl3945_is_ready_rf(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07007043 IWL_DEBUG_MAC80211("leave - RF not ready\n");
7044 return -EIO;
7045 }
7046
7047 spin_lock_irqsave(&priv->lock, flags);
7048
7049 for (i = 0; i < AC_NUM; i++) {
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08007050 txq = &priv->txq39[i];
Zhu Yib481de92007-09-25 17:54:57 -07007051 q = &txq->q;
Samuel Ortizd20b3c62008-12-19 10:37:15 +08007052 avail = iwl_queue_space(q);
Zhu Yib481de92007-09-25 17:54:57 -07007053
Johannes Berg57ffc582008-04-29 17:18:59 +02007054 stats[i].len = q->n_window - avail;
7055 stats[i].limit = q->n_window - q->high_mark;
7056 stats[i].count = q->n_window;
Zhu Yib481de92007-09-25 17:54:57 -07007057
7058 }
7059 spin_unlock_irqrestore(&priv->lock, flags);
7060
7061 IWL_DEBUG_MAC80211("leave\n");
7062
7063 return 0;
7064}
7065
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007066static void iwl3945_mac_reset_tsf(struct ieee80211_hw *hw)
Zhu Yib481de92007-09-25 17:54:57 -07007067{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08007068 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07007069 unsigned long flags;
7070
7071 mutex_lock(&priv->mutex);
7072 IWL_DEBUG_MAC80211("enter\n");
7073
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007074 iwl3945_reset_qos(priv);
Ron Rindjunsky292ae172008-02-06 11:20:39 -08007075
Zhu Yib481de92007-09-25 17:54:57 -07007076 spin_lock_irqsave(&priv->lock, flags);
7077 priv->assoc_id = 0;
7078 priv->assoc_capability = 0;
7079 priv->call_post_assoc_from_beacon = 0;
7080
7081 /* new association get rid of ibss beacon skb */
7082 if (priv->ibss_beacon)
7083 dev_kfree_skb(priv->ibss_beacon);
7084
7085 priv->ibss_beacon = NULL;
7086
7087 priv->beacon_int = priv->hw->conf.beacon_int;
Tomas Winkler28afaf92008-12-19 10:37:06 +08007088 priv->timestamp = 0;
Johannes Berg05c914f2008-09-11 00:01:58 +02007089 if ((priv->iw_mode == NL80211_IFTYPE_STATION))
Zhu Yib481de92007-09-25 17:54:57 -07007090 priv->beacon_int = 0;
7091
7092 spin_unlock_irqrestore(&priv->lock, flags);
7093
Mohamed Abbasfde35712007-11-29 11:10:15 +08007094 if (!iwl3945_is_ready_rf(priv)) {
7095 IWL_DEBUG_MAC80211("leave - not ready\n");
7096 mutex_unlock(&priv->mutex);
7097 return;
7098 }
7099
Mohamed Abbas15e869d2007-10-25 17:15:46 +08007100 /* we are restarting association process
7101 * clear RXON_FILTER_ASSOC_MSK bit
7102 */
Johannes Berg05c914f2008-09-11 00:01:58 +02007103 if (priv->iw_mode != NL80211_IFTYPE_AP) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007104 iwl3945_scan_cancel_timeout(priv, 100);
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08007105 priv->staging39_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007106 iwl3945_commit_rxon(priv);
Mohamed Abbas15e869d2007-10-25 17:15:46 +08007107 }
7108
Zhu Yib481de92007-09-25 17:54:57 -07007109 /* Per mac80211.h: This is only used in IBSS mode... */
Johannes Berg05c914f2008-09-11 00:01:58 +02007110 if (priv->iw_mode != NL80211_IFTYPE_ADHOC) {
Mohamed Abbas15e869d2007-10-25 17:15:46 +08007111
Zhu Yib481de92007-09-25 17:54:57 -07007112 IWL_DEBUG_MAC80211("leave - not in IBSS\n");
7113 mutex_unlock(&priv->mutex);
7114 return;
7115 }
7116
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007117 iwl3945_set_rate(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007118
7119 mutex_unlock(&priv->mutex);
7120
7121 IWL_DEBUG_MAC80211("leave\n");
7122
7123}
7124
Johannes Berge039fa42008-05-15 12:55:29 +02007125static int iwl3945_mac_beacon_update(struct ieee80211_hw *hw, struct sk_buff *skb)
Zhu Yib481de92007-09-25 17:54:57 -07007126{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08007127 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07007128 unsigned long flags;
7129
Zhu Yib481de92007-09-25 17:54:57 -07007130 IWL_DEBUG_MAC80211("enter\n");
7131
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007132 if (!iwl3945_is_ready_rf(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07007133 IWL_DEBUG_MAC80211("leave - RF not ready\n");
Zhu Yib481de92007-09-25 17:54:57 -07007134 return -EIO;
7135 }
7136
Johannes Berg05c914f2008-09-11 00:01:58 +02007137 if (priv->iw_mode != NL80211_IFTYPE_ADHOC) {
Zhu Yib481de92007-09-25 17:54:57 -07007138 IWL_DEBUG_MAC80211("leave - not IBSS\n");
Zhu Yib481de92007-09-25 17:54:57 -07007139 return -EIO;
7140 }
7141
7142 spin_lock_irqsave(&priv->lock, flags);
7143
7144 if (priv->ibss_beacon)
7145 dev_kfree_skb(priv->ibss_beacon);
7146
7147 priv->ibss_beacon = skb;
7148
7149 priv->assoc_id = 0;
7150
7151 IWL_DEBUG_MAC80211("leave\n");
7152 spin_unlock_irqrestore(&priv->lock, flags);
7153
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007154 iwl3945_reset_qos(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007155
Abhijeet Kolekardc4b1e72008-09-03 11:26:30 +08007156 iwl3945_post_associate(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007157
Zhu Yib481de92007-09-25 17:54:57 -07007158
7159 return 0;
7160}
7161
7162/*****************************************************************************
7163 *
7164 * sysfs attributes
7165 *
7166 *****************************************************************************/
7167
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08007168#ifdef CONFIG_IWL3945_DEBUG
Zhu Yib481de92007-09-25 17:54:57 -07007169
7170/*
7171 * The following adds a new attribute to the sysfs representation
7172 * of this device driver (i.e. a new file in /sys/bus/pci/drivers/iwl/)
7173 * used for controlling the debug level.
7174 *
7175 * See the level definitions in iwl for details.
7176 */
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08007177static ssize_t show_debug_level(struct device *d,
7178 struct device_attribute *attr, char *buf)
Zhu Yib481de92007-09-25 17:54:57 -07007179{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08007180 struct iwl_priv *priv = d->driver_data;
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08007181
7182 return sprintf(buf, "0x%08X\n", priv->debug_level);
Zhu Yib481de92007-09-25 17:54:57 -07007183}
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08007184static ssize_t store_debug_level(struct device *d,
7185 struct device_attribute *attr,
Zhu Yib481de92007-09-25 17:54:57 -07007186 const char *buf, size_t count)
7187{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08007188 struct iwl_priv *priv = d->driver_data;
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08007189 unsigned long val;
7190 int ret;
Zhu Yib481de92007-09-25 17:54:57 -07007191
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08007192 ret = strict_strtoul(buf, 0, &val);
7193 if (ret)
Tomas Winkler978785a2008-12-19 10:37:31 +08007194 IWL_INFO(priv, "%s is not in hex or decimal form.\n", buf);
Zhu Yib481de92007-09-25 17:54:57 -07007195 else
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08007196 priv->debug_level = val;
Zhu Yib481de92007-09-25 17:54:57 -07007197
7198 return strnlen(buf, count);
7199}
7200
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08007201static DEVICE_ATTR(debug_level, S_IWUSR | S_IRUGO,
7202 show_debug_level, store_debug_level);
Zhu Yib481de92007-09-25 17:54:57 -07007203
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08007204#endif /* CONFIG_IWL3945_DEBUG */
Zhu Yib481de92007-09-25 17:54:57 -07007205
Zhu Yib481de92007-09-25 17:54:57 -07007206static ssize_t show_temperature(struct device *d,
7207 struct device_attribute *attr, char *buf)
7208{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08007209 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07007210
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007211 if (!iwl3945_is_alive(priv))
Zhu Yib481de92007-09-25 17:54:57 -07007212 return -EAGAIN;
7213
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007214 return sprintf(buf, "%d\n", iwl3945_hw_get_temperature(priv));
Zhu Yib481de92007-09-25 17:54:57 -07007215}
7216
7217static DEVICE_ATTR(temperature, S_IRUGO, show_temperature, NULL);
7218
Zhu Yib481de92007-09-25 17:54:57 -07007219static ssize_t show_tx_power(struct device *d,
7220 struct device_attribute *attr, char *buf)
7221{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08007222 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07007223 return sprintf(buf, "%d\n", priv->user_txpower_limit);
7224}
7225
7226static ssize_t store_tx_power(struct device *d,
7227 struct device_attribute *attr,
7228 const char *buf, size_t count)
7229{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08007230 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07007231 char *p = (char *)buf;
7232 u32 val;
7233
7234 val = simple_strtoul(p, &p, 10);
7235 if (p == buf)
Tomas Winkler978785a2008-12-19 10:37:31 +08007236 IWL_INFO(priv, ": %s is not in decimal form.\n", buf);
Zhu Yib481de92007-09-25 17:54:57 -07007237 else
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007238 iwl3945_hw_reg_set_txpower(priv, val);
Zhu Yib481de92007-09-25 17:54:57 -07007239
7240 return count;
7241}
7242
7243static DEVICE_ATTR(tx_power, S_IWUSR | S_IRUGO, show_tx_power, store_tx_power);
7244
7245static ssize_t show_flags(struct device *d,
7246 struct device_attribute *attr, char *buf)
7247{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08007248 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07007249
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08007250 return sprintf(buf, "0x%04X\n", priv->active39_rxon.flags);
Zhu Yib481de92007-09-25 17:54:57 -07007251}
7252
7253static ssize_t store_flags(struct device *d,
7254 struct device_attribute *attr,
7255 const char *buf, size_t count)
7256{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08007257 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07007258 u32 flags = simple_strtoul(buf, NULL, 0);
7259
7260 mutex_lock(&priv->mutex);
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08007261 if (le32_to_cpu(priv->staging39_rxon.flags) != flags) {
Zhu Yib481de92007-09-25 17:54:57 -07007262 /* Cancel any currently running scans... */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007263 if (iwl3945_scan_cancel_timeout(priv, 100))
Winkler, Tomas39aadf82008-12-19 10:37:32 +08007264 IWL_WARN(priv, "Could not cancel scan.\n");
Zhu Yib481de92007-09-25 17:54:57 -07007265 else {
7266 IWL_DEBUG_INFO("Committing rxon.flags = 0x%04X\n",
7267 flags);
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08007268 priv->staging39_rxon.flags = cpu_to_le32(flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007269 iwl3945_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007270 }
7271 }
7272 mutex_unlock(&priv->mutex);
7273
7274 return count;
7275}
7276
7277static DEVICE_ATTR(flags, S_IWUSR | S_IRUGO, show_flags, store_flags);
7278
7279static ssize_t show_filter_flags(struct device *d,
7280 struct device_attribute *attr, char *buf)
7281{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08007282 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07007283
7284 return sprintf(buf, "0x%04X\n",
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08007285 le32_to_cpu(priv->active39_rxon.filter_flags));
Zhu Yib481de92007-09-25 17:54:57 -07007286}
7287
7288static ssize_t store_filter_flags(struct device *d,
7289 struct device_attribute *attr,
7290 const char *buf, size_t count)
7291{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08007292 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07007293 u32 filter_flags = simple_strtoul(buf, NULL, 0);
7294
7295 mutex_lock(&priv->mutex);
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08007296 if (le32_to_cpu(priv->staging39_rxon.filter_flags) != filter_flags) {
Zhu Yib481de92007-09-25 17:54:57 -07007297 /* Cancel any currently running scans... */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007298 if (iwl3945_scan_cancel_timeout(priv, 100))
Winkler, Tomas39aadf82008-12-19 10:37:32 +08007299 IWL_WARN(priv, "Could not cancel scan.\n");
Zhu Yib481de92007-09-25 17:54:57 -07007300 else {
7301 IWL_DEBUG_INFO("Committing rxon.filter_flags = "
7302 "0x%04X\n", filter_flags);
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08007303 priv->staging39_rxon.filter_flags =
Zhu Yib481de92007-09-25 17:54:57 -07007304 cpu_to_le32(filter_flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007305 iwl3945_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007306 }
7307 }
7308 mutex_unlock(&priv->mutex);
7309
7310 return count;
7311}
7312
7313static DEVICE_ATTR(filter_flags, S_IWUSR | S_IRUGO, show_filter_flags,
7314 store_filter_flags);
7315
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08007316#ifdef CONFIG_IWL3945_SPECTRUM_MEASUREMENT
Zhu Yib481de92007-09-25 17:54:57 -07007317
7318static ssize_t show_measurement(struct device *d,
7319 struct device_attribute *attr, char *buf)
7320{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08007321 struct iwl_priv *priv = dev_get_drvdata(d);
Tomas Winkler600c0e12008-12-19 10:37:04 +08007322 struct iwl_spectrum_notification measure_report;
Zhu Yib481de92007-09-25 17:54:57 -07007323 u32 size = sizeof(measure_report), len = 0, ofs = 0;
Tomas Winkler3ac7f142008-07-21 02:40:14 +03007324 u8 *data = (u8 *)&measure_report;
Zhu Yib481de92007-09-25 17:54:57 -07007325 unsigned long flags;
7326
7327 spin_lock_irqsave(&priv->lock, flags);
7328 if (!(priv->measurement_status & MEASUREMENT_READY)) {
7329 spin_unlock_irqrestore(&priv->lock, flags);
7330 return 0;
7331 }
7332 memcpy(&measure_report, &priv->measure_report, size);
7333 priv->measurement_status = 0;
7334 spin_unlock_irqrestore(&priv->lock, flags);
7335
7336 while (size && (PAGE_SIZE - len)) {
7337 hex_dump_to_buffer(data + ofs, size, 16, 1, buf + len,
7338 PAGE_SIZE - len, 1);
7339 len = strlen(buf);
7340 if (PAGE_SIZE - len)
7341 buf[len++] = '\n';
7342
7343 ofs += 16;
7344 size -= min(size, 16U);
7345 }
7346
7347 return len;
7348}
7349
7350static ssize_t store_measurement(struct device *d,
7351 struct device_attribute *attr,
7352 const char *buf, size_t count)
7353{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08007354 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07007355 struct ieee80211_measurement_params params = {
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08007356 .channel = le16_to_cpu(priv->active39_rxon.channel),
Zhu Yib481de92007-09-25 17:54:57 -07007357 .start_time = cpu_to_le64(priv->last_tsf),
7358 .duration = cpu_to_le16(1),
7359 };
7360 u8 type = IWL_MEASURE_BASIC;
7361 u8 buffer[32];
7362 u8 channel;
7363
7364 if (count) {
7365 char *p = buffer;
7366 strncpy(buffer, buf, min(sizeof(buffer), count));
7367 channel = simple_strtoul(p, NULL, 0);
7368 if (channel)
7369 params.channel = channel;
7370
7371 p = buffer;
7372 while (*p && *p != ' ')
7373 p++;
7374 if (*p)
7375 type = simple_strtoul(p + 1, NULL, 0);
7376 }
7377
7378 IWL_DEBUG_INFO("Invoking measurement of type %d on "
7379 "channel %d (for '%s')\n", type, params.channel, buf);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007380 iwl3945_get_measurement(priv, &params, type);
Zhu Yib481de92007-09-25 17:54:57 -07007381
7382 return count;
7383}
7384
7385static DEVICE_ATTR(measurement, S_IRUSR | S_IWUSR,
7386 show_measurement, store_measurement);
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08007387#endif /* CONFIG_IWL3945_SPECTRUM_MEASUREMENT */
Zhu Yib481de92007-09-25 17:54:57 -07007388
Zhu Yib481de92007-09-25 17:54:57 -07007389static ssize_t store_retry_rate(struct device *d,
7390 struct device_attribute *attr,
7391 const char *buf, size_t count)
7392{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08007393 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07007394
7395 priv->retry_rate = simple_strtoul(buf, NULL, 0);
7396 if (priv->retry_rate <= 0)
7397 priv->retry_rate = 1;
7398
7399 return count;
7400}
7401
7402static ssize_t show_retry_rate(struct device *d,
7403 struct device_attribute *attr, char *buf)
7404{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08007405 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07007406 return sprintf(buf, "%d", priv->retry_rate);
7407}
7408
7409static DEVICE_ATTR(retry_rate, S_IWUSR | S_IRUSR, show_retry_rate,
7410 store_retry_rate);
7411
7412static ssize_t store_power_level(struct device *d,
7413 struct device_attribute *attr,
7414 const char *buf, size_t count)
7415{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08007416 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07007417 int rc;
7418 int mode;
7419
7420 mode = simple_strtoul(buf, NULL, 0);
7421 mutex_lock(&priv->mutex);
7422
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007423 if (!iwl3945_is_ready(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07007424 rc = -EAGAIN;
7425 goto out;
7426 }
7427
Samuel Ortiz1125eff2008-12-19 10:37:14 +08007428 if ((mode < 1) || (mode > IWL39_POWER_LIMIT) ||
7429 (mode == IWL39_POWER_AC))
7430 mode = IWL39_POWER_AC;
Zhu Yib481de92007-09-25 17:54:57 -07007431 else
7432 mode |= IWL_POWER_ENABLED;
7433
7434 if (mode != priv->power_mode) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007435 rc = iwl3945_send_power_mode(priv, IWL_POWER_LEVEL(mode));
Zhu Yib481de92007-09-25 17:54:57 -07007436 if (rc) {
7437 IWL_DEBUG_MAC80211("failed setting power mode.\n");
7438 goto out;
7439 }
7440 priv->power_mode = mode;
7441 }
7442
7443 rc = count;
7444
7445 out:
7446 mutex_unlock(&priv->mutex);
7447 return rc;
7448}
7449
7450#define MAX_WX_STRING 80
7451
7452/* Values are in microsecond */
7453static const s32 timeout_duration[] = {
7454 350000,
7455 250000,
7456 75000,
7457 37000,
7458 25000,
7459};
7460static const s32 period_duration[] = {
7461 400000,
7462 700000,
7463 1000000,
7464 1000000,
7465 1000000
7466};
7467
7468static ssize_t show_power_level(struct device *d,
7469 struct device_attribute *attr, char *buf)
7470{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08007471 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07007472 int level = IWL_POWER_LEVEL(priv->power_mode);
7473 char *p = buf;
7474
7475 p += sprintf(p, "%d ", level);
7476 switch (level) {
7477 case IWL_POWER_MODE_CAM:
Samuel Ortiz1125eff2008-12-19 10:37:14 +08007478 case IWL39_POWER_AC:
Zhu Yib481de92007-09-25 17:54:57 -07007479 p += sprintf(p, "(AC)");
7480 break;
Samuel Ortiz1125eff2008-12-19 10:37:14 +08007481 case IWL39_POWER_BATTERY:
Zhu Yib481de92007-09-25 17:54:57 -07007482 p += sprintf(p, "(BATTERY)");
7483 break;
7484 default:
7485 p += sprintf(p,
7486 "(Timeout %dms, Period %dms)",
7487 timeout_duration[level - 1] / 1000,
7488 period_duration[level - 1] / 1000);
7489 }
7490
7491 if (!(priv->power_mode & IWL_POWER_ENABLED))
7492 p += sprintf(p, " OFF\n");
7493 else
7494 p += sprintf(p, " \n");
7495
Tomas Winkler3ac7f142008-07-21 02:40:14 +03007496 return p - buf + 1;
Zhu Yib481de92007-09-25 17:54:57 -07007497
7498}
7499
7500static DEVICE_ATTR(power_level, S_IWUSR | S_IRUSR, show_power_level,
7501 store_power_level);
7502
7503static ssize_t show_channels(struct device *d,
7504 struct device_attribute *attr, char *buf)
7505{
Johannes Berg8318d782008-01-24 19:38:38 +01007506 /* all this shit doesn't belong into sysfs anyway */
7507 return 0;
Zhu Yib481de92007-09-25 17:54:57 -07007508}
7509
7510static DEVICE_ATTR(channels, S_IRUSR, show_channels, NULL);
7511
7512static ssize_t show_statistics(struct device *d,
7513 struct device_attribute *attr, char *buf)
7514{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08007515 struct iwl_priv *priv = dev_get_drvdata(d);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007516 u32 size = sizeof(struct iwl3945_notif_statistics);
Zhu Yib481de92007-09-25 17:54:57 -07007517 u32 len = 0, ofs = 0;
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08007518 u8 *data = (u8 *)&priv->statistics_39;
Zhu Yib481de92007-09-25 17:54:57 -07007519 int rc = 0;
7520
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007521 if (!iwl3945_is_alive(priv))
Zhu Yib481de92007-09-25 17:54:57 -07007522 return -EAGAIN;
7523
7524 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007525 rc = iwl3945_send_statistics_request(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007526 mutex_unlock(&priv->mutex);
7527
7528 if (rc) {
7529 len = sprintf(buf,
7530 "Error sending statistics request: 0x%08X\n", rc);
7531 return len;
7532 }
7533
7534 while (size && (PAGE_SIZE - len)) {
7535 hex_dump_to_buffer(data + ofs, size, 16, 1, buf + len,
7536 PAGE_SIZE - len, 1);
7537 len = strlen(buf);
7538 if (PAGE_SIZE - len)
7539 buf[len++] = '\n';
7540
7541 ofs += 16;
7542 size -= min(size, 16U);
7543 }
7544
7545 return len;
7546}
7547
7548static DEVICE_ATTR(statistics, S_IRUGO, show_statistics, NULL);
7549
7550static ssize_t show_antenna(struct device *d,
7551 struct device_attribute *attr, char *buf)
7552{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08007553 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07007554
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007555 if (!iwl3945_is_alive(priv))
Zhu Yib481de92007-09-25 17:54:57 -07007556 return -EAGAIN;
7557
7558 return sprintf(buf, "%d\n", priv->antenna);
7559}
7560
7561static ssize_t store_antenna(struct device *d,
7562 struct device_attribute *attr,
7563 const char *buf, size_t count)
7564{
7565 int ant;
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08007566 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07007567
7568 if (count == 0)
7569 return 0;
7570
7571 if (sscanf(buf, "%1i", &ant) != 1) {
7572 IWL_DEBUG_INFO("not in hex or decimal form.\n");
7573 return count;
7574 }
7575
7576 if ((ant >= 0) && (ant <= 2)) {
7577 IWL_DEBUG_INFO("Setting antenna select to %d.\n", ant);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007578 priv->antenna = (enum iwl3945_antenna)ant;
Zhu Yib481de92007-09-25 17:54:57 -07007579 } else
7580 IWL_DEBUG_INFO("Bad antenna select value %d.\n", ant);
7581
7582
7583 return count;
7584}
7585
7586static DEVICE_ATTR(antenna, S_IWUSR | S_IRUGO, show_antenna, store_antenna);
7587
7588static ssize_t show_status(struct device *d,
7589 struct device_attribute *attr, char *buf)
7590{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08007591 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007592 if (!iwl3945_is_alive(priv))
Zhu Yib481de92007-09-25 17:54:57 -07007593 return -EAGAIN;
7594 return sprintf(buf, "0x%08x\n", (int)priv->status);
7595}
7596
7597static DEVICE_ATTR(status, S_IRUGO, show_status, NULL);
7598
7599static ssize_t dump_error_log(struct device *d,
7600 struct device_attribute *attr,
7601 const char *buf, size_t count)
7602{
7603 char *p = (char *)buf;
7604
7605 if (p[0] == '1')
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08007606 iwl3945_dump_nic_error_log((struct iwl_priv *)d->driver_data);
Zhu Yib481de92007-09-25 17:54:57 -07007607
7608 return strnlen(buf, count);
7609}
7610
7611static DEVICE_ATTR(dump_errors, S_IWUSR, NULL, dump_error_log);
7612
7613static ssize_t dump_event_log(struct device *d,
7614 struct device_attribute *attr,
7615 const char *buf, size_t count)
7616{
7617 char *p = (char *)buf;
7618
7619 if (p[0] == '1')
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08007620 iwl3945_dump_nic_event_log((struct iwl_priv *)d->driver_data);
Zhu Yib481de92007-09-25 17:54:57 -07007621
7622 return strnlen(buf, count);
7623}
7624
7625static DEVICE_ATTR(dump_events, S_IWUSR, NULL, dump_event_log);
7626
7627/*****************************************************************************
7628 *
Tomas Winklera96a27f2008-10-23 23:48:56 -07007629 * driver setup and tear down
Zhu Yib481de92007-09-25 17:54:57 -07007630 *
7631 *****************************************************************************/
7632
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08007633static void iwl3945_setup_deferred_work(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07007634{
7635 priv->workqueue = create_workqueue(DRV_NAME);
7636
7637 init_waitqueue_head(&priv->wait_command_queue);
7638
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007639 INIT_WORK(&priv->up, iwl3945_bg_up);
7640 INIT_WORK(&priv->restart, iwl3945_bg_restart);
7641 INIT_WORK(&priv->rx_replenish, iwl3945_bg_rx_replenish);
7642 INIT_WORK(&priv->scan_completed, iwl3945_bg_scan_completed);
7643 INIT_WORK(&priv->request_scan, iwl3945_bg_request_scan);
7644 INIT_WORK(&priv->abort_scan, iwl3945_bg_abort_scan);
7645 INIT_WORK(&priv->rf_kill, iwl3945_bg_rf_kill);
7646 INIT_WORK(&priv->beacon_update, iwl3945_bg_beacon_update);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007647 INIT_DELAYED_WORK(&priv->init_alive_start, iwl3945_bg_init_alive_start);
7648 INIT_DELAYED_WORK(&priv->alive_start, iwl3945_bg_alive_start);
7649 INIT_DELAYED_WORK(&priv->scan_check, iwl3945_bg_scan_check);
Zhu Yib481de92007-09-25 17:54:57 -07007650
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007651 iwl3945_hw_setup_deferred_work(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007652
7653 tasklet_init(&priv->irq_tasklet, (void (*)(unsigned long))
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007654 iwl3945_irq_tasklet, (unsigned long)priv);
Zhu Yib481de92007-09-25 17:54:57 -07007655}
7656
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08007657static void iwl3945_cancel_deferred_work(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07007658{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007659 iwl3945_hw_cancel_deferred_work(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007660
Joonwoo Parke47eb6a2007-11-29 10:42:49 +09007661 cancel_delayed_work_sync(&priv->init_alive_start);
Zhu Yib481de92007-09-25 17:54:57 -07007662 cancel_delayed_work(&priv->scan_check);
7663 cancel_delayed_work(&priv->alive_start);
Zhu Yib481de92007-09-25 17:54:57 -07007664 cancel_work_sync(&priv->beacon_update);
7665}
7666
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007667static struct attribute *iwl3945_sysfs_entries[] = {
Zhu Yib481de92007-09-25 17:54:57 -07007668 &dev_attr_antenna.attr,
7669 &dev_attr_channels.attr,
7670 &dev_attr_dump_errors.attr,
7671 &dev_attr_dump_events.attr,
7672 &dev_attr_flags.attr,
7673 &dev_attr_filter_flags.attr,
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08007674#ifdef CONFIG_IWL3945_SPECTRUM_MEASUREMENT
Zhu Yib481de92007-09-25 17:54:57 -07007675 &dev_attr_measurement.attr,
7676#endif
7677 &dev_attr_power_level.attr,
Zhu Yib481de92007-09-25 17:54:57 -07007678 &dev_attr_retry_rate.attr,
Zhu Yib481de92007-09-25 17:54:57 -07007679 &dev_attr_statistics.attr,
7680 &dev_attr_status.attr,
7681 &dev_attr_temperature.attr,
Zhu Yib481de92007-09-25 17:54:57 -07007682 &dev_attr_tx_power.attr,
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08007683#ifdef CONFIG_IWL3945_DEBUG
7684 &dev_attr_debug_level.attr,
7685#endif
Zhu Yib481de92007-09-25 17:54:57 -07007686 NULL
7687};
7688
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007689static struct attribute_group iwl3945_attribute_group = {
Zhu Yib481de92007-09-25 17:54:57 -07007690 .name = NULL, /* put in device directory */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007691 .attrs = iwl3945_sysfs_entries,
Zhu Yib481de92007-09-25 17:54:57 -07007692};
7693
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007694static struct ieee80211_ops iwl3945_hw_ops = {
7695 .tx = iwl3945_mac_tx,
7696 .start = iwl3945_mac_start,
7697 .stop = iwl3945_mac_stop,
7698 .add_interface = iwl3945_mac_add_interface,
7699 .remove_interface = iwl3945_mac_remove_interface,
7700 .config = iwl3945_mac_config,
7701 .config_interface = iwl3945_mac_config_interface,
7702 .configure_filter = iwl3945_configure_filter,
7703 .set_key = iwl3945_mac_set_key,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007704 .get_tx_stats = iwl3945_mac_get_tx_stats,
7705 .conf_tx = iwl3945_mac_conf_tx,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007706 .reset_tsf = iwl3945_mac_reset_tsf,
Abhijeet Kolekarcd56d332008-09-03 11:26:21 +08007707 .bss_info_changed = iwl3945_bss_info_changed,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007708 .hw_scan = iwl3945_mac_hw_scan
Zhu Yib481de92007-09-25 17:54:57 -07007709};
7710
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007711static int iwl3945_pci_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
Zhu Yib481de92007-09-25 17:54:57 -07007712{
7713 int err = 0;
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08007714 struct iwl_priv *priv;
Zhu Yib481de92007-09-25 17:54:57 -07007715 struct ieee80211_hw *hw;
Kolekar, Abhijeetc0f20d92008-12-19 10:37:19 +08007716 struct iwl_cfg *cfg = (struct iwl_cfg *)(ent->driver_data);
Mohamed Abbas0359fac2008-03-28 16:21:08 -07007717 unsigned long flags;
Zhu Yib481de92007-09-25 17:54:57 -07007718
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08007719 /***********************
7720 * 1. Allocating HW data
7721 * ********************/
7722
Zhu Yib481de92007-09-25 17:54:57 -07007723 /* mac80211 allocates memory for this device instance, including
7724 * space for this driver's private structure */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08007725 hw = ieee80211_alloc_hw(sizeof(struct iwl_priv), &iwl3945_hw_ops);
Zhu Yib481de92007-09-25 17:54:57 -07007726 if (hw == NULL) {
Samuel Ortiza3139c52008-12-19 10:37:09 +08007727 printk(KERN_ERR DRV_NAME "Can not allocate network device\n");
Zhu Yib481de92007-09-25 17:54:57 -07007728 err = -ENOMEM;
7729 goto out;
7730 }
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08007731
Zhu Yib481de92007-09-25 17:54:57 -07007732 SET_IEEE80211_DEV(hw, &pdev->dev);
7733
Zhu Yib481de92007-09-25 17:54:57 -07007734 priv = hw->priv;
7735 priv->hw = hw;
Zhu Yib481de92007-09-25 17:54:57 -07007736 priv->pci_dev = pdev;
Tomas Winkler82b9a122008-03-04 18:09:30 -08007737 priv->cfg = cfg;
Cahill, Ben M6440adb2007-11-29 11:09:55 +08007738
Samuel Ortiza3139c52008-12-19 10:37:09 +08007739 if ((iwl3945_param_queues_num > IWL39_MAX_NUM_QUEUES) ||
7740 (iwl3945_param_queues_num < IWL_MIN_NUM_QUEUES)) {
7741 IWL_ERROR("invalid queues_num, should be between %d and %d\n",
7742 IWL_MIN_NUM_QUEUES, IWL39_MAX_NUM_QUEUES);
7743 err = -EINVAL;
7744 goto out;
7745 }
7746
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08007747 /* Disabling hardware scan means that mac80211 will perform scans
7748 * "the hard way", rather than using device's scan. */
7749 if (iwl3945_param_disable_hw_scan) {
7750 IWL_DEBUG_INFO("Disabling hw_scan\n");
7751 iwl3945_hw_ops.hw_scan = NULL;
7752 }
7753
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08007754 IWL_DEBUG_INFO("*** LOAD DRIVER ***\n");
7755 hw->rate_control_algorithm = "iwl-3945-rs";
7756 hw->sta_data_size = sizeof(struct iwl3945_sta_priv);
7757
Cahill, Ben M6440adb2007-11-29 11:09:55 +08007758 /* Select antenna (may be helpful if only one antenna is connected) */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007759 priv->antenna = (enum iwl3945_antenna)iwl3945_param_antenna;
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08007760#ifdef CONFIG_IWL3945_DEBUG
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08007761 priv->debug_level = iwl3945_param_debug;
Zhu Yib481de92007-09-25 17:54:57 -07007762 atomic_set(&priv->restrict_refcnt, 0);
7763#endif
Zhu Yib481de92007-09-25 17:54:57 -07007764
Bruno Randolf566bfe52008-05-08 19:15:40 +02007765 /* Tell mac80211 our characteristics */
Johannes Berg605a0bd2008-07-15 10:10:01 +02007766 hw->flags = IEEE80211_HW_SIGNAL_DBM |
Bruno Randolf566bfe52008-05-08 19:15:40 +02007767 IEEE80211_HW_NOISE_DBM;
Zhu Yib481de92007-09-25 17:54:57 -07007768
Luis R. Rodriguezf59ac042008-08-29 16:26:43 -07007769 hw->wiphy->interface_modes =
Luis R. Rodriguezf59ac042008-08-29 16:26:43 -07007770 BIT(NL80211_IFTYPE_STATION) |
7771 BIT(NL80211_IFTYPE_ADHOC);
7772
Luis R. Rodriguezea4a82dc2008-11-12 14:22:04 -08007773 hw->wiphy->fw_handles_regulatory = true;
7774
Cahill, Ben M6440adb2007-11-29 11:09:55 +08007775 /* 4 EDCA QOS priorities */
Zhu Yib481de92007-09-25 17:54:57 -07007776 hw->queues = 4;
7777
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08007778 /***************************
7779 * 2. Initializing PCI bus
7780 * *************************/
Zhu Yib481de92007-09-25 17:54:57 -07007781 if (pci_enable_device(pdev)) {
7782 err = -ENODEV;
7783 goto out_ieee80211_free_hw;
7784 }
7785
7786 pci_set_master(pdev);
7787
Zhu Yib481de92007-09-25 17:54:57 -07007788 err = pci_set_dma_mask(pdev, DMA_32BIT_MASK);
7789 if (!err)
7790 err = pci_set_consistent_dma_mask(pdev, DMA_32BIT_MASK);
7791 if (err) {
Tomas Winkler978785a2008-12-19 10:37:31 +08007792 IWL_WARN(priv, "No suitable DMA available.\n");
Zhu Yib481de92007-09-25 17:54:57 -07007793 goto out_pci_disable_device;
7794 }
7795
7796 pci_set_drvdata(pdev, priv);
7797 err = pci_request_regions(pdev, DRV_NAME);
7798 if (err)
7799 goto out_pci_disable_device;
Cahill, Ben M6440adb2007-11-29 11:09:55 +08007800
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08007801 /***********************
7802 * 3. Read REV Register
7803 * ********************/
Zhu Yib481de92007-09-25 17:54:57 -07007804 priv->hw_base = pci_iomap(pdev, 0, 0);
7805 if (!priv->hw_base) {
7806 err = -ENODEV;
7807 goto out_pci_release_regions;
7808 }
7809
7810 IWL_DEBUG_INFO("pci_resource_len = 0x%08llx\n",
7811 (unsigned long long) pci_resource_len(pdev, 0));
7812 IWL_DEBUG_INFO("pci_resource_base = %p\n", priv->hw_base);
7813
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08007814 /* We disable the RETRY_TIMEOUT register (0x41) to keep
7815 * PCI Tx retries from interfering with C3 CPU state */
7816 pci_write_config_byte(pdev, 0x41, 0x00);
Zhu Yib481de92007-09-25 17:54:57 -07007817
Zhu Yi5a669262008-01-14 17:46:18 -08007818 /* nic init */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08007819 iwl_set_bit(priv, CSR_GIO_CHICKEN_BITS,
Tomas Winkler3ac7f142008-07-21 02:40:14 +03007820 CSR_GIO_CHICKEN_BITS_REG_BIT_DIS_L0S_EXIT_TIMER);
Zhu Yi5a669262008-01-14 17:46:18 -08007821
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08007822 iwl_set_bit(priv, CSR_GP_CNTRL, CSR_GP_CNTRL_REG_FLAG_INIT_DONE);
7823 err = iwl_poll_direct_bit(priv, CSR_GP_CNTRL,
Zhu, Yi73d7b5a2008-12-05 07:58:40 -08007824 CSR_GP_CNTRL_REG_FLAG_MAC_CLOCK_READY, 25000);
Tomas Winkler3ac7f142008-07-21 02:40:14 +03007825 if (err < 0) {
7826 IWL_DEBUG_INFO("Failed to init the card\n");
Zhu Yi5a669262008-01-14 17:46:18 -08007827 goto out_remove_sysfs;
Tomas Winkler3ac7f142008-07-21 02:40:14 +03007828 }
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08007829
7830 /***********************
7831 * 4. Read EEPROM
7832 * ********************/
Zhu Yi5a669262008-01-14 17:46:18 -08007833 /* Read the EEPROM */
7834 err = iwl3945_eeprom_init(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007835 if (err) {
Zhu Yi5a669262008-01-14 17:46:18 -08007836 IWL_ERROR("Unable to init EEPROM\n");
7837 goto out_remove_sysfs;
7838 }
7839 /* MAC Address location in EEPROM same for 3945/4965 */
7840 get_eeprom_mac(priv, priv->mac_addr);
Johannes Berge1749612008-10-27 15:59:26 -07007841 IWL_DEBUG_INFO("MAC address: %pM\n", priv->mac_addr);
Zhu Yi5a669262008-01-14 17:46:18 -08007842 SET_IEEE80211_PERM_ADDR(priv->hw, priv->mac_addr);
7843
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08007844 /***********************
7845 * 5. Setup HW Constants
7846 * ********************/
7847 /* Device-specific setup */
Abhijeet Kolekar3832ec92008-12-19 10:37:26 +08007848 if (iwl3945_hw_set_hw_params(priv)) {
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08007849 IWL_ERROR("failed to set hw settings\n");
7850 goto out_iounmap;
7851 }
7852
7853 /***********************
7854 * 6. Setup priv
7855 * ********************/
7856 priv->retry_rate = 1;
7857 priv->ibss_beacon = NULL;
7858
7859 spin_lock_init(&priv->lock);
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08007860 spin_lock_init(&priv->power_data_39.lock);
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08007861 spin_lock_init(&priv->sta_lock);
7862 spin_lock_init(&priv->hcmd_lock);
7863
7864 INIT_LIST_HEAD(&priv->free_frames);
7865 mutex_init(&priv->mutex);
7866
7867 /* Clear the driver's (not device's) station table */
7868 iwl3945_clear_stations_table(priv);
7869
7870 priv->data_retry_limit = -1;
7871 priv->ieee_channels = NULL;
7872 priv->ieee_rates = NULL;
7873 priv->band = IEEE80211_BAND_2GHZ;
7874
7875 priv->iw_mode = NL80211_IFTYPE_STATION;
7876
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08007877 iwl3945_reset_qos(priv);
7878
7879 priv->qos_data.qos_active = 0;
7880 priv->qos_data.qos_cap.val = 0;
7881
7882
7883 priv->rates_mask = IWL_RATES_MASK;
7884 /* If power management is turned on, default to AC mode */
Samuel Ortiz1125eff2008-12-19 10:37:14 +08007885 priv->power_mode = IWL39_POWER_AC;
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08007886 priv->user_txpower_limit = IWL_DEFAULT_TX_POWER;
7887
Reinette Chatre849e0dc2008-01-23 10:15:18 -08007888 err = iwl3945_init_channel_map(priv);
7889 if (err) {
7890 IWL_ERROR("initializing regulatory failed: %d\n", err);
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08007891 goto out_release_irq;
Reinette Chatre849e0dc2008-01-23 10:15:18 -08007892 }
7893
7894 err = iwl3945_init_geos(priv);
7895 if (err) {
7896 IWL_ERROR("initializing geos failed: %d\n", err);
7897 goto out_free_channel_map;
7898 }
Reinette Chatre849e0dc2008-01-23 10:15:18 -08007899
Tomas Winkler978785a2008-12-19 10:37:31 +08007900 IWL_INFO(priv, "Detected Intel Wireless WiFi Link %s\n",
7901 priv->cfg->name);
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08007902
7903 /***********************************
7904 * 7. Initialize Module Parameters
7905 * **********************************/
7906
7907 /* Initialize module parameter values here */
7908 /* Disable radio (SW RF KILL) via parameter when loading driver */
7909 if (iwl3945_param_disable) {
7910 set_bit(STATUS_RF_KILL_SW, &priv->status);
7911 IWL_DEBUG_INFO("Radio disabled.\n");
7912 }
7913
7914
7915 /***********************
7916 * 8. Setup Services
7917 * ********************/
7918
7919 spin_lock_irqsave(&priv->lock, flags);
7920 iwl3945_disable_interrupts(priv);
7921 spin_unlock_irqrestore(&priv->lock, flags);
7922
7923 err = sysfs_create_group(&pdev->dev.kobj, &iwl3945_attribute_group);
7924 if (err) {
7925 IWL_ERROR("failed to create sysfs device attributes\n");
7926 goto out_free_geos;
7927 }
7928
7929 iwl3945_set_rxon_channel(priv, IEEE80211_BAND_2GHZ, 6);
7930 iwl3945_setup_deferred_work(priv);
7931 iwl3945_setup_rx_handlers(priv);
7932
7933 /***********************
7934 * 9. Conclude
7935 * ********************/
7936 pci_save_state(pdev);
7937 pci_disable_device(pdev);
7938
7939 /*********************************
7940 * 10. Setup and Register mac80211
7941 * *******************************/
7942
Zhu Yi5a669262008-01-14 17:46:18 -08007943 err = ieee80211_register_hw(priv->hw);
7944 if (err) {
7945 IWL_ERROR("Failed to register network device (error %d)\n", err);
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08007946 goto out_remove_sysfs;
Zhu Yib481de92007-09-25 17:54:57 -07007947 }
7948
Zhu Yi5a669262008-01-14 17:46:18 -08007949 priv->hw->conf.beacon_int = 100;
7950 priv->mac80211_registered = 1;
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08007951
Zhu Yib481de92007-09-25 17:54:57 -07007952
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08007953 err = iwl3945_rfkill_init(priv);
7954 if (err)
7955 IWL_ERROR("Unable to initialize RFKILL system. "
7956 "Ignoring error: %d\n", err);
7957
Zhu Yib481de92007-09-25 17:54:57 -07007958 return 0;
7959
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08007960 out_remove_sysfs:
7961 sysfs_remove_group(&pdev->dev.kobj, &iwl3945_attribute_group);
Reinette Chatre849e0dc2008-01-23 10:15:18 -08007962 out_free_geos:
7963 iwl3945_free_geos(priv);
7964 out_free_channel_map:
7965 iwl3945_free_channel_map(priv);
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08007966
Zhu Yib481de92007-09-25 17:54:57 -07007967
7968 out_release_irq:
Zhu Yib481de92007-09-25 17:54:57 -07007969 destroy_workqueue(priv->workqueue);
7970 priv->workqueue = NULL;
Abhijeet Kolekar3832ec92008-12-19 10:37:26 +08007971 iwl3945_unset_hw_params(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007972
7973 out_iounmap:
7974 pci_iounmap(pdev, priv->hw_base);
7975 out_pci_release_regions:
7976 pci_release_regions(pdev);
7977 out_pci_disable_device:
7978 pci_disable_device(pdev);
7979 pci_set_drvdata(pdev, NULL);
7980 out_ieee80211_free_hw:
7981 ieee80211_free_hw(priv->hw);
7982 out:
7983 return err;
7984}
7985
Reinette Chatrec83dbf62008-03-21 13:53:41 -07007986static void __devexit iwl3945_pci_remove(struct pci_dev *pdev)
Zhu Yib481de92007-09-25 17:54:57 -07007987{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08007988 struct iwl_priv *priv = pci_get_drvdata(pdev);
Mohamed Abbas0359fac2008-03-28 16:21:08 -07007989 unsigned long flags;
Zhu Yib481de92007-09-25 17:54:57 -07007990
7991 if (!priv)
7992 return;
7993
7994 IWL_DEBUG_INFO("*** UNLOAD DRIVER ***\n");
7995
Zhu Yib481de92007-09-25 17:54:57 -07007996 set_bit(STATUS_EXIT_PENDING, &priv->status);
Zhu Yib24d22b2007-12-19 13:59:52 +08007997
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007998 iwl3945_down(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007999
Mohamed Abbas0359fac2008-03-28 16:21:08 -07008000 /* make sure we flush any pending irq or
8001 * tasklet for the driver
8002 */
8003 spin_lock_irqsave(&priv->lock, flags);
8004 iwl3945_disable_interrupts(priv);
8005 spin_unlock_irqrestore(&priv->lock, flags);
8006
8007 iwl_synchronize_irq(priv);
8008
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008009 sysfs_remove_group(&pdev->dev.kobj, &iwl3945_attribute_group);
Zhu Yib481de92007-09-25 17:54:57 -07008010
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08008011 iwl3945_rfkill_unregister(priv);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008012 iwl3945_dealloc_ucode_pci(priv);
Zhu Yib481de92007-09-25 17:54:57 -07008013
8014 if (priv->rxq.bd)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008015 iwl3945_rx_queue_free(priv, &priv->rxq);
8016 iwl3945_hw_txq_ctx_free(priv);
Zhu Yib481de92007-09-25 17:54:57 -07008017
Abhijeet Kolekar3832ec92008-12-19 10:37:26 +08008018 iwl3945_unset_hw_params(priv);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008019 iwl3945_clear_stations_table(priv);
Zhu Yib481de92007-09-25 17:54:57 -07008020
Tomas Winkler3ac7f142008-07-21 02:40:14 +03008021 if (priv->mac80211_registered)
Zhu Yib481de92007-09-25 17:54:57 -07008022 ieee80211_unregister_hw(priv->hw);
Zhu Yib481de92007-09-25 17:54:57 -07008023
Mohamed Abbas6ef89d02007-10-25 17:15:47 +08008024 /*netif_stop_queue(dev); */
8025 flush_workqueue(priv->workqueue);
8026
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008027 /* ieee80211_unregister_hw calls iwl3945_mac_stop, which flushes
Zhu Yib481de92007-09-25 17:54:57 -07008028 * priv->workqueue... so we can't take down the workqueue
8029 * until now... */
8030 destroy_workqueue(priv->workqueue);
8031 priv->workqueue = NULL;
8032
Zhu Yib481de92007-09-25 17:54:57 -07008033 pci_iounmap(pdev, priv->hw_base);
8034 pci_release_regions(pdev);
8035 pci_disable_device(pdev);
8036 pci_set_drvdata(pdev, NULL);
8037
Reinette Chatre849e0dc2008-01-23 10:15:18 -08008038 iwl3945_free_channel_map(priv);
8039 iwl3945_free_geos(priv);
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08008040 kfree(priv->scan39);
Zhu Yib481de92007-09-25 17:54:57 -07008041 if (priv->ibss_beacon)
8042 dev_kfree_skb(priv->ibss_beacon);
8043
8044 ieee80211_free_hw(priv->hw);
8045}
8046
8047#ifdef CONFIG_PM
8048
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008049static int iwl3945_pci_suspend(struct pci_dev *pdev, pm_message_t state)
Zhu Yib481de92007-09-25 17:54:57 -07008050{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08008051 struct iwl_priv *priv = pci_get_drvdata(pdev);
Zhu Yib481de92007-09-25 17:54:57 -07008052
Zhu Yie655b9f2008-01-24 02:19:38 -08008053 if (priv->is_open) {
8054 set_bit(STATUS_IN_SUSPEND, &priv->status);
8055 iwl3945_mac_stop(priv->hw);
8056 priv->is_open = 1;
8057 }
Zhu Yib481de92007-09-25 17:54:57 -07008058
Zhu Yib481de92007-09-25 17:54:57 -07008059 pci_set_power_state(pdev, PCI_D3hot);
8060
Zhu Yib481de92007-09-25 17:54:57 -07008061 return 0;
8062}
8063
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008064static int iwl3945_pci_resume(struct pci_dev *pdev)
Zhu Yib481de92007-09-25 17:54:57 -07008065{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08008066 struct iwl_priv *priv = pci_get_drvdata(pdev);
Zhu Yib481de92007-09-25 17:54:57 -07008067
Zhu Yib481de92007-09-25 17:54:57 -07008068 pci_set_power_state(pdev, PCI_D0);
Zhu Yib481de92007-09-25 17:54:57 -07008069
Zhu Yie655b9f2008-01-24 02:19:38 -08008070 if (priv->is_open)
8071 iwl3945_mac_start(priv->hw);
Zhu Yib481de92007-09-25 17:54:57 -07008072
Zhu Yie655b9f2008-01-24 02:19:38 -08008073 clear_bit(STATUS_IN_SUSPEND, &priv->status);
Zhu Yib481de92007-09-25 17:54:57 -07008074 return 0;
8075}
8076
8077#endif /* CONFIG_PM */
8078
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08008079/*************** RFKILL FUNCTIONS **********/
Adel Gadllah80fcc9e2008-07-01 17:49:50 +02008080#ifdef CONFIG_IWL3945_RFKILL
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08008081/* software rf-kill from user */
8082static int iwl3945_rfkill_soft_rf_kill(void *data, enum rfkill_state state)
8083{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08008084 struct iwl_priv *priv = data;
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08008085 int err = 0;
8086
Adel Gadllah80fcc9e2008-07-01 17:49:50 +02008087 if (!priv->rfkill)
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08008088 return 0;
8089
8090 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
8091 return 0;
8092
Tomas Winklera96a27f2008-10-23 23:48:56 -07008093 IWL_DEBUG_RF_KILL("we received soft RFKILL set to state %d\n", state);
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08008094 mutex_lock(&priv->mutex);
8095
8096 switch (state) {
Zhu Yiacdfe9b2008-06-30 17:23:32 +08008097 case RFKILL_STATE_UNBLOCKED:
Adel Gadllah80fcc9e2008-07-01 17:49:50 +02008098 if (iwl3945_is_rfkill_hw(priv)) {
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08008099 err = -EBUSY;
Adel Gadllah80fcc9e2008-07-01 17:49:50 +02008100 goto out_unlock;
8101 }
8102 iwl3945_radio_kill_sw(priv, 0);
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08008103 break;
Zhu Yiacdfe9b2008-06-30 17:23:32 +08008104 case RFKILL_STATE_SOFT_BLOCKED:
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08008105 iwl3945_radio_kill_sw(priv, 1);
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08008106 break;
Zhu Yiacdfe9b2008-06-30 17:23:32 +08008107 default:
Winkler, Tomas39aadf82008-12-19 10:37:32 +08008108 IWL_WARN(priv, "received unexpected RFKILL state %d\n", state);
Zhu Yiacdfe9b2008-06-30 17:23:32 +08008109 break;
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08008110 }
Adel Gadllah80fcc9e2008-07-01 17:49:50 +02008111out_unlock:
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08008112 mutex_unlock(&priv->mutex);
8113
8114 return err;
8115}
8116
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08008117int iwl3945_rfkill_init(struct iwl_priv *priv)
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08008118{
8119 struct device *device = wiphy_dev(priv->hw->wiphy);
8120 int ret = 0;
8121
8122 BUG_ON(device == NULL);
8123
8124 IWL_DEBUG_RF_KILL("Initializing RFKILL.\n");
Adel Gadllah80fcc9e2008-07-01 17:49:50 +02008125 priv->rfkill = rfkill_allocate(device, RFKILL_TYPE_WLAN);
8126 if (!priv->rfkill) {
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08008127 IWL_ERROR("Unable to allocate rfkill device.\n");
8128 ret = -ENOMEM;
8129 goto error;
8130 }
8131
Adel Gadllah80fcc9e2008-07-01 17:49:50 +02008132 priv->rfkill->name = priv->cfg->name;
8133 priv->rfkill->data = priv;
8134 priv->rfkill->state = RFKILL_STATE_UNBLOCKED;
8135 priv->rfkill->toggle_radio = iwl3945_rfkill_soft_rf_kill;
8136 priv->rfkill->user_claim_unsupported = 1;
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08008137
Adel Gadllah80fcc9e2008-07-01 17:49:50 +02008138 priv->rfkill->dev.class->suspend = NULL;
8139 priv->rfkill->dev.class->resume = NULL;
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08008140
Adel Gadllah80fcc9e2008-07-01 17:49:50 +02008141 ret = rfkill_register(priv->rfkill);
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08008142 if (ret) {
8143 IWL_ERROR("Unable to register rfkill: %d\n", ret);
Adel Gadllah80fcc9e2008-07-01 17:49:50 +02008144 goto freed_rfkill;
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08008145 }
8146
8147 IWL_DEBUG_RF_KILL("RFKILL initialization complete.\n");
8148 return ret;
8149
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08008150freed_rfkill:
Adel Gadllah80fcc9e2008-07-01 17:49:50 +02008151 if (priv->rfkill != NULL)
8152 rfkill_free(priv->rfkill);
8153 priv->rfkill = NULL;
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08008154
8155error:
8156 IWL_DEBUG_RF_KILL("RFKILL initialization complete.\n");
8157 return ret;
8158}
8159
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08008160void iwl3945_rfkill_unregister(struct iwl_priv *priv)
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08008161{
Adel Gadllah80fcc9e2008-07-01 17:49:50 +02008162 if (priv->rfkill)
8163 rfkill_unregister(priv->rfkill);
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08008164
Adel Gadllah80fcc9e2008-07-01 17:49:50 +02008165 priv->rfkill = NULL;
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08008166}
8167
8168/* set rf-kill to the right state. */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08008169void iwl3945_rfkill_set_hw_state(struct iwl_priv *priv)
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08008170{
8171
Adel Gadllah80fcc9e2008-07-01 17:49:50 +02008172 if (!priv->rfkill)
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08008173 return;
8174
Adel Gadllah80fcc9e2008-07-01 17:49:50 +02008175 if (iwl3945_is_rfkill_hw(priv)) {
8176 rfkill_force_state(priv->rfkill, RFKILL_STATE_HARD_BLOCKED);
8177 return;
8178 }
8179
8180 if (!iwl3945_is_rfkill_sw(priv))
8181 rfkill_force_state(priv->rfkill, RFKILL_STATE_UNBLOCKED);
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08008182 else
Adel Gadllah80fcc9e2008-07-01 17:49:50 +02008183 rfkill_force_state(priv->rfkill, RFKILL_STATE_SOFT_BLOCKED);
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08008184}
8185#endif
8186
Zhu Yib481de92007-09-25 17:54:57 -07008187/*****************************************************************************
8188 *
8189 * driver and module entry point
8190 *
8191 *****************************************************************************/
8192
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008193static struct pci_driver iwl3945_driver = {
Zhu Yib481de92007-09-25 17:54:57 -07008194 .name = DRV_NAME,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008195 .id_table = iwl3945_hw_card_ids,
8196 .probe = iwl3945_pci_probe,
8197 .remove = __devexit_p(iwl3945_pci_remove),
Zhu Yib481de92007-09-25 17:54:57 -07008198#ifdef CONFIG_PM
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008199 .suspend = iwl3945_pci_suspend,
8200 .resume = iwl3945_pci_resume,
Zhu Yib481de92007-09-25 17:54:57 -07008201#endif
8202};
8203
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008204static int __init iwl3945_init(void)
Zhu Yib481de92007-09-25 17:54:57 -07008205{
8206
8207 int ret;
8208 printk(KERN_INFO DRV_NAME ": " DRV_DESCRIPTION ", " DRV_VERSION "\n");
8209 printk(KERN_INFO DRV_NAME ": " DRV_COPYRIGHT "\n");
Reinette Chatre897e1cf2008-03-28 16:21:09 -07008210
8211 ret = iwl3945_rate_control_register();
8212 if (ret) {
Samuel Ortiza3139c52008-12-19 10:37:09 +08008213 printk(KERN_ERR DRV_NAME
8214 "Unable to register rate control algorithm: %d\n", ret);
Reinette Chatre897e1cf2008-03-28 16:21:09 -07008215 return ret;
8216 }
8217
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008218 ret = pci_register_driver(&iwl3945_driver);
Zhu Yib481de92007-09-25 17:54:57 -07008219 if (ret) {
Samuel Ortiza3139c52008-12-19 10:37:09 +08008220 printk(KERN_ERR DRV_NAME "Unable to initialize PCI module\n");
Reinette Chatre897e1cf2008-03-28 16:21:09 -07008221 goto error_register;
Zhu Yib481de92007-09-25 17:54:57 -07008222 }
Zhu Yib481de92007-09-25 17:54:57 -07008223
8224 return ret;
Reinette Chatre897e1cf2008-03-28 16:21:09 -07008225
Reinette Chatre897e1cf2008-03-28 16:21:09 -07008226error_register:
8227 iwl3945_rate_control_unregister();
8228 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07008229}
8230
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008231static void __exit iwl3945_exit(void)
Zhu Yib481de92007-09-25 17:54:57 -07008232{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008233 pci_unregister_driver(&iwl3945_driver);
Reinette Chatre897e1cf2008-03-28 16:21:09 -07008234 iwl3945_rate_control_unregister();
Zhu Yib481de92007-09-25 17:54:57 -07008235}
8236
Reinette Chatrea0987a82008-12-02 12:14:06 -08008237MODULE_FIRMWARE(IWL3945_MODULE_FIRMWARE(IWL3945_UCODE_API_MAX));
Zhu Yi25cb6ca2008-09-11 11:45:22 +08008238
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008239module_param_named(antenna, iwl3945_param_antenna, int, 0444);
Zhu Yib481de92007-09-25 17:54:57 -07008240MODULE_PARM_DESC(antenna, "select antenna (1=Main, 2=Aux, default 0 [both])");
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008241module_param_named(disable, iwl3945_param_disable, int, 0444);
Zhu Yib481de92007-09-25 17:54:57 -07008242MODULE_PARM_DESC(disable, "manually disable the radio (default 0 [radio on])");
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008243module_param_named(hwcrypto, iwl3945_param_hwcrypto, int, 0444);
Zhu Yib481de92007-09-25 17:54:57 -07008244MODULE_PARM_DESC(hwcrypto,
8245 "using hardware crypto engine (default 0 [software])\n");
Wu, Fengguang95aa1942008-12-17 16:52:30 +08008246module_param_named(debug, iwl3945_param_debug, uint, 0444);
Zhu Yib481de92007-09-25 17:54:57 -07008247MODULE_PARM_DESC(debug, "debug output mask");
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008248module_param_named(disable_hw_scan, iwl3945_param_disable_hw_scan, int, 0444);
Zhu Yib481de92007-09-25 17:54:57 -07008249MODULE_PARM_DESC(disable_hw_scan, "disable hardware scanning (default 0)");
8250
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008251module_param_named(queues_num, iwl3945_param_queues_num, int, 0444);
Zhu Yib481de92007-09-25 17:54:57 -07008252MODULE_PARM_DESC(queues_num, "number of hw queues.");
8253
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008254module_exit(iwl3945_exit);
8255module_init(iwl3945_init);