blob: 52c9075b1889e2d2539e798053617352def15412 [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) {
Winkler, Tomas15b16872008-12-19 10:37:33 +0800188 IWL_ERR(priv, "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) {
Winkler, Tomas15b16872008-12-19 10:37:33 +0800202 IWL_ERR(priv, "pci_alloc_consistent(%zd) failed\n",
Zhu Yib481de92007-09-25 17:54:57 -0700203 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)) {
Winkler, Tomas15b16872008-12-19 10:37:33 +0800531 IWL_ERR(priv, "No space for Tx\n");
Zhu Yib481de92007-09-25 17:54:57 -0700532 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) {
Winkler, Tomas15b16872008-12-19 10:37:33 +0800599 IWL_ERR(priv,
600 "Error sending %s: iwl3945_enqueue_hcmd failed: %d\n",
601 get_cmd_string(cmd->id), ret);
Zhu Yib481de92007-09-25 17:54:57 -0700602 return ret;
603 }
604 return 0;
605}
606
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800607static int iwl3945_send_cmd_sync(struct iwl_priv *priv, struct iwl3945_host_cmd *cmd)
Zhu Yib481de92007-09-25 17:54:57 -0700608{
609 int cmd_idx;
610 int ret;
Zhu Yib481de92007-09-25 17:54:57 -0700611
612 BUG_ON(cmd->meta.flags & CMD_ASYNC);
613
614 /* A synchronous command can not have a callback set. */
615 BUG_ON(cmd->meta.u.callback != NULL);
616
Tomas Winklere5472972008-03-28 16:21:12 -0700617 if (test_and_set_bit(STATUS_HCMD_SYNC_ACTIVE, &priv->status)) {
Winkler, Tomas15b16872008-12-19 10:37:33 +0800618 IWL_ERR(priv,
619 "Error sending %s: Already sending a host command\n",
620 get_cmd_string(cmd->id));
Tomas Winklere5472972008-03-28 16:21:12 -0700621 ret = -EBUSY;
622 goto out;
Zhu Yib481de92007-09-25 17:54:57 -0700623 }
624
625 set_bit(STATUS_HCMD_ACTIVE, &priv->status);
626
627 if (cmd->meta.flags & CMD_WANT_SKB)
628 cmd->meta.source = &cmd->meta;
629
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800630 cmd_idx = iwl3945_enqueue_hcmd(priv, cmd);
Zhu Yib481de92007-09-25 17:54:57 -0700631 if (cmd_idx < 0) {
632 ret = cmd_idx;
Winkler, Tomas15b16872008-12-19 10:37:33 +0800633 IWL_ERR(priv,
634 "Error sending %s: iwl3945_enqueue_hcmd failed: %d\n",
635 get_cmd_string(cmd->id), ret);
Zhu Yib481de92007-09-25 17:54:57 -0700636 goto out;
637 }
638
639 ret = wait_event_interruptible_timeout(priv->wait_command_queue,
640 !test_bit(STATUS_HCMD_ACTIVE, &priv->status),
641 HOST_COMPLETE_TIMEOUT);
642 if (!ret) {
643 if (test_bit(STATUS_HCMD_ACTIVE, &priv->status)) {
Winkler, Tomas15b16872008-12-19 10:37:33 +0800644 IWL_ERR(priv, "Error sending %s: time out after %dms\n",
Zhu Yib481de92007-09-25 17:54:57 -0700645 get_cmd_string(cmd->id),
646 jiffies_to_msecs(HOST_COMPLETE_TIMEOUT));
647
648 clear_bit(STATUS_HCMD_ACTIVE, &priv->status);
649 ret = -ETIMEDOUT;
650 goto cancel;
651 }
652 }
653
654 if (test_bit(STATUS_RF_KILL_HW, &priv->status)) {
655 IWL_DEBUG_INFO("Command %s aborted: RF KILL Switch\n",
656 get_cmd_string(cmd->id));
657 ret = -ECANCELED;
658 goto fail;
659 }
660 if (test_bit(STATUS_FW_ERROR, &priv->status)) {
661 IWL_DEBUG_INFO("Command %s failed: FW Error\n",
662 get_cmd_string(cmd->id));
663 ret = -EIO;
664 goto fail;
665 }
666 if ((cmd->meta.flags & CMD_WANT_SKB) && !cmd->meta.u.skb) {
Winkler, Tomas15b16872008-12-19 10:37:33 +0800667 IWL_ERR(priv, "Error: Response NULL in '%s'\n",
Zhu Yib481de92007-09-25 17:54:57 -0700668 get_cmd_string(cmd->id));
669 ret = -EIO;
Zhu Yi73e1a652009-01-08 10:19:58 -0800670 goto cancel;
Zhu Yib481de92007-09-25 17:54:57 -0700671 }
672
673 ret = 0;
674 goto out;
675
676cancel:
677 if (cmd->meta.flags & CMD_WANT_SKB) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800678 struct iwl3945_cmd *qcmd;
Zhu Yib481de92007-09-25 17:54:57 -0700679
680 /* Cancel the CMD_WANT_SKB flag for the cmd in the
681 * TX cmd queue. Otherwise in case the cmd comes
682 * in later, it will possibly set an invalid
683 * address (cmd->meta.source). */
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800684 qcmd = &priv->txq39[IWL_CMD_QUEUE_NUM].cmd[cmd_idx];
Zhu Yib481de92007-09-25 17:54:57 -0700685 qcmd->meta.flags &= ~CMD_WANT_SKB;
686 }
687fail:
688 if (cmd->meta.u.skb) {
689 dev_kfree_skb_any(cmd->meta.u.skb);
690 cmd->meta.u.skb = NULL;
691 }
692out:
Tomas Winklere5472972008-03-28 16:21:12 -0700693 clear_bit(STATUS_HCMD_SYNC_ACTIVE, &priv->status);
Zhu Yib481de92007-09-25 17:54:57 -0700694 return ret;
695}
696
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800697int iwl3945_send_cmd(struct iwl_priv *priv, struct iwl3945_host_cmd *cmd)
Zhu Yib481de92007-09-25 17:54:57 -0700698{
Zhu Yib481de92007-09-25 17:54:57 -0700699 if (cmd->meta.flags & CMD_ASYNC)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800700 return iwl3945_send_cmd_async(priv, cmd);
Zhu Yib481de92007-09-25 17:54:57 -0700701
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800702 return iwl3945_send_cmd_sync(priv, cmd);
Zhu Yib481de92007-09-25 17:54:57 -0700703}
704
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800705int iwl3945_send_cmd_pdu(struct iwl_priv *priv, u8 id, u16 len, const void *data)
Zhu Yib481de92007-09-25 17:54:57 -0700706{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800707 struct iwl3945_host_cmd cmd = {
Zhu Yib481de92007-09-25 17:54:57 -0700708 .id = id,
709 .len = len,
710 .data = data,
711 };
712
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800713 return iwl3945_send_cmd_sync(priv, &cmd);
Zhu Yib481de92007-09-25 17:54:57 -0700714}
715
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800716static int __must_check iwl3945_send_cmd_u32(struct iwl_priv *priv, u8 id, u32 val)
Zhu Yib481de92007-09-25 17:54:57 -0700717{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800718 struct iwl3945_host_cmd cmd = {
Zhu Yib481de92007-09-25 17:54:57 -0700719 .id = id,
720 .len = sizeof(val),
721 .data = &val,
722 };
723
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800724 return iwl3945_send_cmd_sync(priv, &cmd);
Zhu Yib481de92007-09-25 17:54:57 -0700725}
726
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800727int iwl3945_send_statistics_request(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700728{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800729 return iwl3945_send_cmd_u32(priv, REPLY_STATISTICS_CMD, 0);
Zhu Yib481de92007-09-25 17:54:57 -0700730}
731
732/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800733 * iwl3945_set_rxon_channel - Set the phymode and channel values in staging RXON
Johannes Berg8318d782008-01-24 19:38:38 +0100734 * @band: 2.4 or 5 GHz band
735 * @channel: Any channel valid for the requested band
Zhu Yib481de92007-09-25 17:54:57 -0700736
Johannes Berg8318d782008-01-24 19:38:38 +0100737 * In addition to setting the staging RXON, priv->band is also set.
Zhu Yib481de92007-09-25 17:54:57 -0700738 *
739 * NOTE: Does not commit to the hardware; it sets appropriate bit fields
Johannes Berg8318d782008-01-24 19:38:38 +0100740 * in the staging RXON flag structure based on the band
Zhu Yib481de92007-09-25 17:54:57 -0700741 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800742static int iwl3945_set_rxon_channel(struct iwl_priv *priv,
Johannes Berg8318d782008-01-24 19:38:38 +0100743 enum ieee80211_band band,
744 u16 channel)
Zhu Yib481de92007-09-25 17:54:57 -0700745{
Johannes Berg8318d782008-01-24 19:38:38 +0100746 if (!iwl3945_get_channel_info(priv, band, channel)) {
Zhu Yib481de92007-09-25 17:54:57 -0700747 IWL_DEBUG_INFO("Could not set channel to %d [%d]\n",
Johannes Berg8318d782008-01-24 19:38:38 +0100748 channel, band);
Zhu Yib481de92007-09-25 17:54:57 -0700749 return -EINVAL;
750 }
751
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800752 if ((le16_to_cpu(priv->staging39_rxon.channel) == channel) &&
Johannes Berg8318d782008-01-24 19:38:38 +0100753 (priv->band == band))
Zhu Yib481de92007-09-25 17:54:57 -0700754 return 0;
755
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800756 priv->staging39_rxon.channel = cpu_to_le16(channel);
Johannes Berg8318d782008-01-24 19:38:38 +0100757 if (band == IEEE80211_BAND_5GHZ)
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800758 priv->staging39_rxon.flags &= ~RXON_FLG_BAND_24G_MSK;
Zhu Yib481de92007-09-25 17:54:57 -0700759 else
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800760 priv->staging39_rxon.flags |= RXON_FLG_BAND_24G_MSK;
Zhu Yib481de92007-09-25 17:54:57 -0700761
Johannes Berg8318d782008-01-24 19:38:38 +0100762 priv->band = band;
Zhu Yib481de92007-09-25 17:54:57 -0700763
Johannes Berg8318d782008-01-24 19:38:38 +0100764 IWL_DEBUG_INFO("Staging channel set to %d [%d]\n", channel, band);
Zhu Yib481de92007-09-25 17:54:57 -0700765
766 return 0;
767}
768
769/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800770 * iwl3945_check_rxon_cmd - validate RXON structure is valid
Zhu Yib481de92007-09-25 17:54:57 -0700771 *
772 * NOTE: This is really only useful during development and can eventually
773 * be #ifdef'd out once the driver is stable and folks aren't actively
774 * making changes
775 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800776static int iwl3945_check_rxon_cmd(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700777{
778 int error = 0;
779 int counter = 1;
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800780 struct iwl3945_rxon_cmd *rxon = &priv->staging39_rxon;
Zhu Yib481de92007-09-25 17:54:57 -0700781
782 if (rxon->flags & RXON_FLG_BAND_24G_MSK) {
783 error |= le32_to_cpu(rxon->flags &
784 (RXON_FLG_TGJ_NARROW_BAND_MSK |
785 RXON_FLG_RADAR_DETECT_MSK));
786 if (error)
Winkler, Tomas39aadf82008-12-19 10:37:32 +0800787 IWL_WARN(priv, "check 24G fields %d | %d\n",
Zhu Yib481de92007-09-25 17:54:57 -0700788 counter++, error);
789 } else {
790 error |= (rxon->flags & RXON_FLG_SHORT_SLOT_MSK) ?
791 0 : le32_to_cpu(RXON_FLG_SHORT_SLOT_MSK);
792 if (error)
Winkler, Tomas39aadf82008-12-19 10:37:32 +0800793 IWL_WARN(priv, "check 52 fields %d | %d\n",
Zhu Yib481de92007-09-25 17:54:57 -0700794 counter++, error);
795 error |= le32_to_cpu(rxon->flags & RXON_FLG_CCK_MSK);
796 if (error)
Winkler, Tomas39aadf82008-12-19 10:37:32 +0800797 IWL_WARN(priv, "check 52 CCK %d | %d\n",
Zhu Yib481de92007-09-25 17:54:57 -0700798 counter++, error);
799 }
800 error |= (rxon->node_addr[0] | rxon->bssid_addr[0]) & 0x1;
801 if (error)
Winkler, Tomas39aadf82008-12-19 10:37:32 +0800802 IWL_WARN(priv, "check mac addr %d | %d\n", counter++, error);
Zhu Yib481de92007-09-25 17:54:57 -0700803
804 /* make sure basic rates 6Mbps and 1Mbps are supported */
805 error |= (((rxon->ofdm_basic_rates & IWL_RATE_6M_MASK) == 0) &&
806 ((rxon->cck_basic_rates & IWL_RATE_1M_MASK) == 0));
807 if (error)
Winkler, Tomas39aadf82008-12-19 10:37:32 +0800808 IWL_WARN(priv, "check basic rate %d | %d\n", counter++, error);
Zhu Yib481de92007-09-25 17:54:57 -0700809
810 error |= (le16_to_cpu(rxon->assoc_id) > 2007);
811 if (error)
Winkler, Tomas39aadf82008-12-19 10:37:32 +0800812 IWL_WARN(priv, "check assoc id %d | %d\n", counter++, error);
Zhu Yib481de92007-09-25 17:54:57 -0700813
814 error |= ((rxon->flags & (RXON_FLG_CCK_MSK | RXON_FLG_SHORT_SLOT_MSK))
815 == (RXON_FLG_CCK_MSK | RXON_FLG_SHORT_SLOT_MSK));
816 if (error)
Winkler, Tomas39aadf82008-12-19 10:37:32 +0800817 IWL_WARN(priv, "check CCK and short slot %d | %d\n",
Zhu Yib481de92007-09-25 17:54:57 -0700818 counter++, error);
819
820 error |= ((rxon->flags & (RXON_FLG_CCK_MSK | RXON_FLG_AUTO_DETECT_MSK))
821 == (RXON_FLG_CCK_MSK | RXON_FLG_AUTO_DETECT_MSK));
822 if (error)
Winkler, Tomas39aadf82008-12-19 10:37:32 +0800823 IWL_WARN(priv, "check CCK & auto detect %d | %d\n",
Zhu Yib481de92007-09-25 17:54:57 -0700824 counter++, error);
825
826 error |= ((rxon->flags & (RXON_FLG_AUTO_DETECT_MSK |
827 RXON_FLG_TGG_PROTECT_MSK)) == RXON_FLG_TGG_PROTECT_MSK);
828 if (error)
Winkler, Tomas39aadf82008-12-19 10:37:32 +0800829 IWL_WARN(priv, "check TGG and auto detect %d | %d\n",
Zhu Yib481de92007-09-25 17:54:57 -0700830 counter++, error);
831
832 if ((rxon->flags & RXON_FLG_DIS_DIV_MSK))
833 error |= ((rxon->flags & (RXON_FLG_ANT_B_MSK |
834 RXON_FLG_ANT_A_MSK)) == 0);
835 if (error)
Winkler, Tomas39aadf82008-12-19 10:37:32 +0800836 IWL_WARN(priv, "check antenna %d %d\n", counter++, error);
Zhu Yib481de92007-09-25 17:54:57 -0700837
838 if (error)
Winkler, Tomas39aadf82008-12-19 10:37:32 +0800839 IWL_WARN(priv, "Tuning to channel %d\n",
Zhu Yib481de92007-09-25 17:54:57 -0700840 le16_to_cpu(rxon->channel));
841
842 if (error) {
Winkler, Tomas15b16872008-12-19 10:37:33 +0800843 IWL_ERR(priv, "Not a valid rxon_assoc_cmd field values\n");
Zhu Yib481de92007-09-25 17:54:57 -0700844 return -1;
845 }
846 return 0;
847}
848
849/**
Ben Cahill9fbab512007-11-29 11:09:47 +0800850 * iwl3945_full_rxon_required - check if full RXON (vs RXON_ASSOC) cmd is needed
Ian Schram01ebd062007-10-25 17:15:22 +0800851 * @priv: staging_rxon is compared to active_rxon
Zhu Yib481de92007-09-25 17:54:57 -0700852 *
Ben Cahill9fbab512007-11-29 11:09:47 +0800853 * If the RXON structure is changing enough to require a new tune,
854 * or is clearing the RXON_FILTER_ASSOC_MSK, then return 1 to indicate that
855 * a new tune (full RXON command, rather than RXON_ASSOC cmd) is required.
Zhu Yib481de92007-09-25 17:54:57 -0700856 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800857static int iwl3945_full_rxon_required(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700858{
859
860 /* These items are only settable from the full RXON command */
Ron Rindjunsky5d1e2322008-06-30 17:23:04 +0800861 if (!(iwl3945_is_associated(priv)) ||
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800862 compare_ether_addr(priv->staging39_rxon.bssid_addr,
863 priv->active39_rxon.bssid_addr) ||
864 compare_ether_addr(priv->staging39_rxon.node_addr,
865 priv->active39_rxon.node_addr) ||
866 compare_ether_addr(priv->staging39_rxon.wlap_bssid_addr,
867 priv->active39_rxon.wlap_bssid_addr) ||
868 (priv->staging39_rxon.dev_type != priv->active39_rxon.dev_type) ||
869 (priv->staging39_rxon.channel != priv->active39_rxon.channel) ||
870 (priv->staging39_rxon.air_propagation !=
871 priv->active39_rxon.air_propagation) ||
872 (priv->staging39_rxon.assoc_id != priv->active39_rxon.assoc_id))
Zhu Yib481de92007-09-25 17:54:57 -0700873 return 1;
874
875 /* flags, filter_flags, ofdm_basic_rates, and cck_basic_rates can
876 * be updated with the RXON_ASSOC command -- however only some
877 * flag transitions are allowed using RXON_ASSOC */
878
879 /* Check if we are not switching bands */
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800880 if ((priv->staging39_rxon.flags & RXON_FLG_BAND_24G_MSK) !=
881 (priv->active39_rxon.flags & RXON_FLG_BAND_24G_MSK))
Zhu Yib481de92007-09-25 17:54:57 -0700882 return 1;
883
884 /* Check if we are switching association toggle */
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800885 if ((priv->staging39_rxon.filter_flags & RXON_FILTER_ASSOC_MSK) !=
886 (priv->active39_rxon.filter_flags & RXON_FILTER_ASSOC_MSK))
Zhu Yib481de92007-09-25 17:54:57 -0700887 return 1;
888
889 return 0;
890}
891
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800892static int iwl3945_send_rxon_assoc(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700893{
894 int rc = 0;
Tomas Winkler3d24a9f2008-12-19 10:37:07 +0800895 struct iwl_rx_packet *res = NULL;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800896 struct iwl3945_rxon_assoc_cmd rxon_assoc;
897 struct iwl3945_host_cmd cmd = {
Zhu Yib481de92007-09-25 17:54:57 -0700898 .id = REPLY_RXON_ASSOC,
899 .len = sizeof(rxon_assoc),
900 .meta.flags = CMD_WANT_SKB,
901 .data = &rxon_assoc,
902 };
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800903 const struct iwl3945_rxon_cmd *rxon1 = &priv->staging39_rxon;
904 const struct iwl3945_rxon_cmd *rxon2 = &priv->active39_rxon;
Zhu Yib481de92007-09-25 17:54:57 -0700905
906 if ((rxon1->flags == rxon2->flags) &&
907 (rxon1->filter_flags == rxon2->filter_flags) &&
908 (rxon1->cck_basic_rates == rxon2->cck_basic_rates) &&
909 (rxon1->ofdm_basic_rates == rxon2->ofdm_basic_rates)) {
910 IWL_DEBUG_INFO("Using current RXON_ASSOC. Not resending.\n");
911 return 0;
912 }
913
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800914 rxon_assoc.flags = priv->staging39_rxon.flags;
915 rxon_assoc.filter_flags = priv->staging39_rxon.filter_flags;
916 rxon_assoc.ofdm_basic_rates = priv->staging39_rxon.ofdm_basic_rates;
917 rxon_assoc.cck_basic_rates = priv->staging39_rxon.cck_basic_rates;
Zhu Yib481de92007-09-25 17:54:57 -0700918 rxon_assoc.reserved = 0;
919
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800920 rc = iwl3945_send_cmd_sync(priv, &cmd);
Zhu Yib481de92007-09-25 17:54:57 -0700921 if (rc)
922 return rc;
923
Tomas Winkler3d24a9f2008-12-19 10:37:07 +0800924 res = (struct iwl_rx_packet *)cmd.meta.u.skb->data;
Zhu Yib481de92007-09-25 17:54:57 -0700925 if (res->hdr.flags & IWL_CMD_FAILED_MSK) {
Winkler, Tomas15b16872008-12-19 10:37:33 +0800926 IWL_ERR(priv, "Bad return from REPLY_RXON_ASSOC command\n");
Zhu Yib481de92007-09-25 17:54:57 -0700927 rc = -EIO;
928 }
929
930 priv->alloc_rxb_skb--;
931 dev_kfree_skb_any(cmd.meta.u.skb);
932
933 return rc;
934}
935
936/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800937 * iwl3945_commit_rxon - commit staging_rxon to hardware
Zhu Yib481de92007-09-25 17:54:57 -0700938 *
Ian Schram01ebd062007-10-25 17:15:22 +0800939 * The RXON command in staging_rxon is committed to the hardware and
Zhu Yib481de92007-09-25 17:54:57 -0700940 * the active_rxon structure is updated with the new data. This
941 * function correctly transitions out of the RXON_ASSOC_MSK state if
942 * a HW tune is required based on the RXON structure changes.
943 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800944static int iwl3945_commit_rxon(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700945{
946 /* cast away the const for active_rxon in this function */
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800947 struct iwl3945_rxon_cmd *active_rxon = (void *)&priv->active39_rxon;
Zhu Yib481de92007-09-25 17:54:57 -0700948 int rc = 0;
949
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800950 if (!iwl3945_is_alive(priv))
Zhu Yib481de92007-09-25 17:54:57 -0700951 return -1;
952
953 /* always get timestamp with Rx frame */
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800954 priv->staging39_rxon.flags |= RXON_FLG_TSF2HOST_MSK;
Zhu Yib481de92007-09-25 17:54:57 -0700955
956 /* select antenna */
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800957 priv->staging39_rxon.flags &=
Zhu Yib481de92007-09-25 17:54:57 -0700958 ~(RXON_FLG_DIS_DIV_MSK | RXON_FLG_ANT_SEL_MSK);
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800959 priv->staging39_rxon.flags |= iwl3945_get_antenna_flags(priv);
Zhu Yib481de92007-09-25 17:54:57 -0700960
Samuel Ortiza3139c52008-12-19 10:37:09 +0800961 rc = iwl3945_check_rxon_cmd(priv);
Zhu Yib481de92007-09-25 17:54:57 -0700962 if (rc) {
Winkler, Tomas15b16872008-12-19 10:37:33 +0800963 IWL_ERR(priv, "Invalid RXON configuration. Not committing.\n");
Zhu Yib481de92007-09-25 17:54:57 -0700964 return -EINVAL;
965 }
966
967 /* If we don't need to send a full RXON, we can use
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800968 * iwl3945_rxon_assoc_cmd which is used to reconfigure filter
Zhu Yib481de92007-09-25 17:54:57 -0700969 * and other flags for the current radio configuration. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800970 if (!iwl3945_full_rxon_required(priv)) {
971 rc = iwl3945_send_rxon_assoc(priv);
Zhu Yib481de92007-09-25 17:54:57 -0700972 if (rc) {
Winkler, Tomas15b16872008-12-19 10:37:33 +0800973 IWL_ERR(priv, "Error setting RXON_ASSOC "
Zhu Yib481de92007-09-25 17:54:57 -0700974 "configuration (%d).\n", rc);
975 return rc;
976 }
977
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800978 memcpy(active_rxon, &priv->staging39_rxon, sizeof(*active_rxon));
Zhu Yib481de92007-09-25 17:54:57 -0700979
980 return 0;
981 }
982
983 /* If we are currently associated and the new config requires
984 * an RXON_ASSOC and the new config wants the associated mask enabled,
985 * we must clear the associated from the active configuration
986 * before we apply the new config */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800987 if (iwl3945_is_associated(priv) &&
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800988 (priv->staging39_rxon.filter_flags & RXON_FILTER_ASSOC_MSK)) {
Zhu Yib481de92007-09-25 17:54:57 -0700989 IWL_DEBUG_INFO("Toggling associated bit on current RXON\n");
990 active_rxon->filter_flags &= ~RXON_FILTER_ASSOC_MSK;
991
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800992 rc = iwl3945_send_cmd_pdu(priv, REPLY_RXON,
993 sizeof(struct iwl3945_rxon_cmd),
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800994 &priv->active39_rxon);
Zhu Yib481de92007-09-25 17:54:57 -0700995
996 /* If the mask clearing failed then we set
997 * active_rxon back to what it was previously */
998 if (rc) {
999 active_rxon->filter_flags |= RXON_FILTER_ASSOC_MSK;
Winkler, Tomas15b16872008-12-19 10:37:33 +08001000 IWL_ERR(priv, "Error clearing ASSOC_MSK on current "
Zhu Yib481de92007-09-25 17:54:57 -07001001 "configuration (%d).\n", rc);
1002 return rc;
1003 }
Zhu Yib481de92007-09-25 17:54:57 -07001004 }
1005
1006 IWL_DEBUG_INFO("Sending RXON\n"
1007 "* with%s RXON_FILTER_ASSOC_MSK\n"
1008 "* channel = %d\n"
Johannes Berge1749612008-10-27 15:59:26 -07001009 "* bssid = %pM\n",
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001010 ((priv->staging39_rxon.filter_flags &
Zhu Yib481de92007-09-25 17:54:57 -07001011 RXON_FILTER_ASSOC_MSK) ? "" : "out"),
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001012 le16_to_cpu(priv->staging39_rxon.channel),
Johannes Berge1749612008-10-27 15:59:26 -07001013 priv->staging_rxon.bssid_addr);
Zhu Yib481de92007-09-25 17:54:57 -07001014
1015 /* Apply the new configuration */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001016 rc = iwl3945_send_cmd_pdu(priv, REPLY_RXON,
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001017 sizeof(struct iwl3945_rxon_cmd), &priv->staging39_rxon);
Zhu Yib481de92007-09-25 17:54:57 -07001018 if (rc) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08001019 IWL_ERR(priv, "Error setting new configuration (%d).\n", rc);
Zhu Yib481de92007-09-25 17:54:57 -07001020 return rc;
1021 }
1022
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001023 memcpy(active_rxon, &priv->staging39_rxon, sizeof(*active_rxon));
Zhu Yib481de92007-09-25 17:54:57 -07001024
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001025 iwl3945_clear_stations_table(priv);
Zhu Yi556f8db2007-09-27 11:27:33 +08001026
Zhu Yib481de92007-09-25 17:54:57 -07001027 /* If we issue a new RXON command which required a tune then we must
1028 * send a new TXPOWER command or we won't be able to Tx any frames */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001029 rc = iwl3945_hw_reg_send_txpower(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001030 if (rc) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08001031 IWL_ERR(priv, "Error setting Tx power (%d).\n", rc);
Zhu Yib481de92007-09-25 17:54:57 -07001032 return rc;
1033 }
1034
1035 /* Add the broadcast address so we can send broadcast frames */
Kolekar, Abhijeetb5323d32008-12-19 10:37:22 +08001036 if (iwl3945_add_station(priv, iwl_bcast_addr, 0, 0) ==
Zhu Yib481de92007-09-25 17:54:57 -07001037 IWL_INVALID_STATION) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08001038 IWL_ERR(priv, "Error adding BROADCAST address for transmit.\n");
Zhu Yib481de92007-09-25 17:54:57 -07001039 return -EIO;
1040 }
1041
1042 /* If we have set the ASSOC_MSK and we are in BSS mode then
1043 * add the IWL_AP_ID to the station rate table */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001044 if (iwl3945_is_associated(priv) &&
Johannes Berg05c914f2008-09-11 00:01:58 +02001045 (priv->iw_mode == NL80211_IFTYPE_STATION))
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001046 if (iwl3945_add_station(priv, priv->active39_rxon.bssid_addr, 1, 0)
Zhu Yib481de92007-09-25 17:54:57 -07001047 == IWL_INVALID_STATION) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08001048 IWL_ERR(priv, "Error adding AP address for transmit\n");
Zhu Yib481de92007-09-25 17:54:57 -07001049 return -EIO;
1050 }
1051
Johannes Berg8318d782008-01-24 19:38:38 +01001052 /* Init the hardware's rate fallback order based on the band */
Zhu Yib481de92007-09-25 17:54:57 -07001053 rc = iwl3945_init_hw_rate_table(priv);
1054 if (rc) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08001055 IWL_ERR(priv, "Error setting HW rate table: %02X\n", rc);
Zhu Yib481de92007-09-25 17:54:57 -07001056 return -EIO;
1057 }
1058
1059 return 0;
1060}
1061
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001062static int iwl3945_send_bt_config(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001063{
Tomas Winkler4c897252008-12-19 10:37:05 +08001064 struct iwl_bt_cmd bt_cmd = {
Zhu Yib481de92007-09-25 17:54:57 -07001065 .flags = 3,
1066 .lead_time = 0xAA,
1067 .max_kill = 1,
1068 .kill_ack_mask = 0,
1069 .kill_cts_mask = 0,
1070 };
1071
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001072 return iwl3945_send_cmd_pdu(priv, REPLY_BT_CONFIG,
Tomas Winkler4c897252008-12-19 10:37:05 +08001073 sizeof(bt_cmd), &bt_cmd);
Zhu Yib481de92007-09-25 17:54:57 -07001074}
1075
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001076static int iwl3945_send_scan_abort(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001077{
1078 int rc = 0;
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08001079 struct iwl_rx_packet *res;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001080 struct iwl3945_host_cmd cmd = {
Zhu Yib481de92007-09-25 17:54:57 -07001081 .id = REPLY_SCAN_ABORT_CMD,
1082 .meta.flags = CMD_WANT_SKB,
1083 };
1084
1085 /* If there isn't a scan actively going on in the hardware
1086 * then we are in between scan bands and not actually
1087 * actively scanning, so don't send the abort command */
1088 if (!test_bit(STATUS_SCAN_HW, &priv->status)) {
1089 clear_bit(STATUS_SCAN_ABORTING, &priv->status);
1090 return 0;
1091 }
1092
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001093 rc = iwl3945_send_cmd_sync(priv, &cmd);
Zhu Yib481de92007-09-25 17:54:57 -07001094 if (rc) {
1095 clear_bit(STATUS_SCAN_ABORTING, &priv->status);
1096 return rc;
1097 }
1098
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08001099 res = (struct iwl_rx_packet *)cmd.meta.u.skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07001100 if (res->u.status != CAN_ABORT_STATUS) {
1101 /* The scan abort will return 1 for success or
1102 * 2 for "failure". A failure condition can be
1103 * due to simply not being in an active scan which
1104 * can occur if we send the scan abort before we
1105 * the microcode has notified us that a scan is
1106 * completed. */
1107 IWL_DEBUG_INFO("SCAN_ABORT returned %d.\n", res->u.status);
1108 clear_bit(STATUS_SCAN_ABORTING, &priv->status);
1109 clear_bit(STATUS_SCAN_HW, &priv->status);
1110 }
1111
1112 dev_kfree_skb_any(cmd.meta.u.skb);
1113
1114 return rc;
1115}
1116
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001117static int iwl3945_card_state_sync_callback(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001118 struct iwl3945_cmd *cmd,
Zhu Yib481de92007-09-25 17:54:57 -07001119 struct sk_buff *skb)
1120{
1121 return 1;
1122}
1123
1124/*
1125 * CARD_STATE_CMD
1126 *
Ben Cahill9fbab512007-11-29 11:09:47 +08001127 * Use: Sets the device's internal card state to enable, disable, or halt
Zhu Yib481de92007-09-25 17:54:57 -07001128 *
1129 * When in the 'enable' state the card operates as normal.
1130 * When in the 'disable' state, the card enters into a low power mode.
1131 * When in the 'halt' state, the card is shut down and must be fully
1132 * restarted to come back on.
1133 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001134static int iwl3945_send_card_state(struct iwl_priv *priv, u32 flags, u8 meta_flag)
Zhu Yib481de92007-09-25 17:54:57 -07001135{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001136 struct iwl3945_host_cmd cmd = {
Zhu Yib481de92007-09-25 17:54:57 -07001137 .id = REPLY_CARD_STATE_CMD,
1138 .len = sizeof(u32),
1139 .data = &flags,
1140 .meta.flags = meta_flag,
1141 };
1142
1143 if (meta_flag & CMD_ASYNC)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001144 cmd.meta.u.callback = iwl3945_card_state_sync_callback;
Zhu Yib481de92007-09-25 17:54:57 -07001145
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001146 return iwl3945_send_cmd(priv, &cmd);
Zhu Yib481de92007-09-25 17:54:57 -07001147}
1148
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001149static int iwl3945_add_sta_sync_callback(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001150 struct iwl3945_cmd *cmd, struct sk_buff *skb)
Zhu Yib481de92007-09-25 17:54:57 -07001151{
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08001152 struct iwl_rx_packet *res = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07001153
1154 if (!skb) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08001155 IWL_ERR(priv, "Error: Response NULL in REPLY_ADD_STA.\n");
Zhu Yib481de92007-09-25 17:54:57 -07001156 return 1;
1157 }
1158
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08001159 res = (struct iwl_rx_packet *)skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07001160 if (res->hdr.flags & IWL_CMD_FAILED_MSK) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08001161 IWL_ERR(priv, "Bad return from REPLY_ADD_STA (0x%08X)\n",
Zhu Yib481de92007-09-25 17:54:57 -07001162 res->hdr.flags);
1163 return 1;
1164 }
1165
1166 switch (res->u.add_sta.status) {
1167 case ADD_STA_SUCCESS_MSK:
1168 break;
1169 default:
1170 break;
1171 }
1172
1173 /* We didn't cache the SKB; let the caller free it */
1174 return 1;
1175}
1176
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001177int iwl3945_send_add_station(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001178 struct iwl3945_addsta_cmd *sta, u8 flags)
Zhu Yib481de92007-09-25 17:54:57 -07001179{
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08001180 struct iwl_rx_packet *res = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07001181 int rc = 0;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001182 struct iwl3945_host_cmd cmd = {
Zhu Yib481de92007-09-25 17:54:57 -07001183 .id = REPLY_ADD_STA,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001184 .len = sizeof(struct iwl3945_addsta_cmd),
Zhu Yib481de92007-09-25 17:54:57 -07001185 .meta.flags = flags,
1186 .data = sta,
1187 };
1188
1189 if (flags & CMD_ASYNC)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001190 cmd.meta.u.callback = iwl3945_add_sta_sync_callback;
Zhu Yib481de92007-09-25 17:54:57 -07001191 else
1192 cmd.meta.flags |= CMD_WANT_SKB;
1193
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001194 rc = iwl3945_send_cmd(priv, &cmd);
Zhu Yib481de92007-09-25 17:54:57 -07001195
1196 if (rc || (flags & CMD_ASYNC))
1197 return rc;
1198
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08001199 res = (struct iwl_rx_packet *)cmd.meta.u.skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07001200 if (res->hdr.flags & IWL_CMD_FAILED_MSK) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08001201 IWL_ERR(priv, "Bad return from REPLY_ADD_STA (0x%08X)\n",
Zhu Yib481de92007-09-25 17:54:57 -07001202 res->hdr.flags);
1203 rc = -EIO;
1204 }
1205
1206 if (rc == 0) {
1207 switch (res->u.add_sta.status) {
1208 case ADD_STA_SUCCESS_MSK:
1209 IWL_DEBUG_INFO("REPLY_ADD_STA PASSED\n");
1210 break;
1211 default:
1212 rc = -EIO;
Winkler, Tomas39aadf82008-12-19 10:37:32 +08001213 IWL_WARN(priv, "REPLY_ADD_STA failed\n");
Zhu Yib481de92007-09-25 17:54:57 -07001214 break;
1215 }
1216 }
1217
1218 priv->alloc_rxb_skb--;
1219 dev_kfree_skb_any(cmd.meta.u.skb);
1220
1221 return rc;
1222}
1223
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001224static int iwl3945_update_sta_key_info(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07001225 struct ieee80211_key_conf *keyconf,
1226 u8 sta_id)
1227{
1228 unsigned long flags;
1229 __le16 key_flags = 0;
1230
1231 switch (keyconf->alg) {
1232 case ALG_CCMP:
1233 key_flags |= STA_KEY_FLG_CCMP;
1234 key_flags |= cpu_to_le16(
1235 keyconf->keyidx << STA_KEY_FLG_KEYID_POS);
1236 key_flags &= ~STA_KEY_FLG_INVALID;
1237 break;
1238 case ALG_TKIP:
1239 case ALG_WEP:
Zhu Yib481de92007-09-25 17:54:57 -07001240 default:
1241 return -EINVAL;
1242 }
1243 spin_lock_irqsave(&priv->sta_lock, flags);
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001244 priv->stations_39[sta_id].keyinfo.alg = keyconf->alg;
1245 priv->stations_39[sta_id].keyinfo.keylen = keyconf->keylen;
1246 memcpy(priv->stations_39[sta_id].keyinfo.key, keyconf->key,
Zhu Yib481de92007-09-25 17:54:57 -07001247 keyconf->keylen);
1248
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001249 memcpy(priv->stations_39[sta_id].sta.key.key, keyconf->key,
Zhu Yib481de92007-09-25 17:54:57 -07001250 keyconf->keylen);
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001251 priv->stations_39[sta_id].sta.key.key_flags = key_flags;
1252 priv->stations_39[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
1253 priv->stations_39[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07001254
1255 spin_unlock_irqrestore(&priv->sta_lock, flags);
1256
1257 IWL_DEBUG_INFO("hwcrypto: modify ucode station key info\n");
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001258 iwl3945_send_add_station(priv, &priv->stations_39[sta_id].sta, 0);
Zhu Yib481de92007-09-25 17:54:57 -07001259 return 0;
1260}
1261
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001262static int iwl3945_clear_sta_key_info(struct iwl_priv *priv, u8 sta_id)
Zhu Yib481de92007-09-25 17:54:57 -07001263{
1264 unsigned long flags;
1265
1266 spin_lock_irqsave(&priv->sta_lock, flags);
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001267 memset(&priv->stations_39[sta_id].keyinfo, 0, sizeof(struct iwl3945_hw_key));
1268 memset(&priv->stations_39[sta_id].sta.key, 0,
Tomas Winkler4c897252008-12-19 10:37:05 +08001269 sizeof(struct iwl4965_keyinfo));
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001270 priv->stations_39[sta_id].sta.key.key_flags = STA_KEY_FLG_NO_ENC;
1271 priv->stations_39[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
1272 priv->stations_39[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07001273 spin_unlock_irqrestore(&priv->sta_lock, flags);
1274
1275 IWL_DEBUG_INFO("hwcrypto: clear ucode station key info\n");
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001276 iwl3945_send_add_station(priv, &priv->stations_39[sta_id].sta, 0);
Zhu Yib481de92007-09-25 17:54:57 -07001277 return 0;
1278}
1279
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001280static void iwl3945_clear_free_frames(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001281{
1282 struct list_head *element;
1283
1284 IWL_DEBUG_INFO("%d frames on pre-allocated heap on clear.\n",
1285 priv->frames_count);
1286
1287 while (!list_empty(&priv->free_frames)) {
1288 element = priv->free_frames.next;
1289 list_del(element);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001290 kfree(list_entry(element, struct iwl3945_frame, list));
Zhu Yib481de92007-09-25 17:54:57 -07001291 priv->frames_count--;
1292 }
1293
1294 if (priv->frames_count) {
Winkler, Tomas39aadf82008-12-19 10:37:32 +08001295 IWL_WARN(priv, "%d frames still in use. Did we lose one?\n",
Zhu Yib481de92007-09-25 17:54:57 -07001296 priv->frames_count);
1297 priv->frames_count = 0;
1298 }
1299}
1300
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001301static struct iwl3945_frame *iwl3945_get_free_frame(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001302{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001303 struct iwl3945_frame *frame;
Zhu Yib481de92007-09-25 17:54:57 -07001304 struct list_head *element;
1305 if (list_empty(&priv->free_frames)) {
1306 frame = kzalloc(sizeof(*frame), GFP_KERNEL);
1307 if (!frame) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08001308 IWL_ERR(priv, "Could not allocate frame!\n");
Zhu Yib481de92007-09-25 17:54:57 -07001309 return NULL;
1310 }
1311
1312 priv->frames_count++;
1313 return frame;
1314 }
1315
1316 element = priv->free_frames.next;
1317 list_del(element);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001318 return list_entry(element, struct iwl3945_frame, list);
Zhu Yib481de92007-09-25 17:54:57 -07001319}
1320
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001321static void iwl3945_free_frame(struct iwl_priv *priv, struct iwl3945_frame *frame)
Zhu Yib481de92007-09-25 17:54:57 -07001322{
1323 memset(frame, 0, sizeof(*frame));
1324 list_add(&frame->list, &priv->free_frames);
1325}
1326
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001327unsigned int iwl3945_fill_beacon_frame(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07001328 struct ieee80211_hdr *hdr,
Rami Rosen73ec1cc2008-12-16 09:37:07 +02001329 int left)
Zhu Yib481de92007-09-25 17:54:57 -07001330{
1331
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001332 if (!iwl3945_is_associated(priv) || !priv->ibss_beacon ||
Johannes Berg05c914f2008-09-11 00:01:58 +02001333 ((priv->iw_mode != NL80211_IFTYPE_ADHOC) &&
1334 (priv->iw_mode != NL80211_IFTYPE_AP)))
Zhu Yib481de92007-09-25 17:54:57 -07001335 return 0;
1336
1337 if (priv->ibss_beacon->len > left)
1338 return 0;
1339
1340 memcpy(hdr, priv->ibss_beacon->data, priv->ibss_beacon->len);
1341
1342 return priv->ibss_beacon->len;
1343}
1344
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001345static u8 iwl3945_rate_get_lowest_plcp(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001346{
1347 u8 i;
Kolekar, Abhijeetc24f0812008-11-07 09:58:44 -08001348 int rate_mask;
1349
1350 /* Set rate mask*/
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001351 if (priv->staging39_rxon.flags & RXON_FLG_BAND_24G_MSK)
Chatre, Reinettedbce56a2008-11-12 13:14:07 -08001352 rate_mask = priv->active_rate_basic & IWL_CCK_RATES_MASK;
Kolekar, Abhijeetc24f0812008-11-07 09:58:44 -08001353 else
Chatre, Reinettedbce56a2008-11-12 13:14:07 -08001354 rate_mask = priv->active_rate_basic & IWL_OFDM_RATES_MASK;
Zhu Yib481de92007-09-25 17:54:57 -07001355
1356 for (i = IWL_RATE_1M_INDEX; i != IWL_RATE_INVALID;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001357 i = iwl3945_rates[i].next_ieee) {
Zhu Yib481de92007-09-25 17:54:57 -07001358 if (rate_mask & (1 << i))
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001359 return iwl3945_rates[i].plcp;
Zhu Yib481de92007-09-25 17:54:57 -07001360 }
1361
Kolekar, Abhijeetc24f0812008-11-07 09:58:44 -08001362 /* No valid rate was found. Assign the lowest one */
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001363 if (priv->staging39_rxon.flags & RXON_FLG_BAND_24G_MSK)
Kolekar, Abhijeetc24f0812008-11-07 09:58:44 -08001364 return IWL_RATE_1M_PLCP;
1365 else
1366 return IWL_RATE_6M_PLCP;
Zhu Yib481de92007-09-25 17:54:57 -07001367}
1368
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001369static int iwl3945_send_beacon_cmd(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001370{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001371 struct iwl3945_frame *frame;
Zhu Yib481de92007-09-25 17:54:57 -07001372 unsigned int frame_size;
1373 int rc;
1374 u8 rate;
1375
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001376 frame = iwl3945_get_free_frame(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001377
1378 if (!frame) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08001379 IWL_ERR(priv, "Could not obtain free frame buffer for beacon "
Zhu Yib481de92007-09-25 17:54:57 -07001380 "command.\n");
1381 return -ENOMEM;
1382 }
1383
Kolekar, Abhijeetc24f0812008-11-07 09:58:44 -08001384 rate = iwl3945_rate_get_lowest_plcp(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001385
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001386 frame_size = iwl3945_hw_get_beacon_cmd(priv, frame, rate);
Zhu Yib481de92007-09-25 17:54:57 -07001387
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001388 rc = iwl3945_send_cmd_pdu(priv, REPLY_TX_BEACON, frame_size,
Zhu Yib481de92007-09-25 17:54:57 -07001389 &frame->u.cmd[0]);
1390
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001391 iwl3945_free_frame(priv, frame);
Zhu Yib481de92007-09-25 17:54:57 -07001392
1393 return rc;
1394}
1395
1396/******************************************************************************
1397 *
1398 * EEPROM related functions
1399 *
1400 ******************************************************************************/
1401
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001402static void get_eeprom_mac(struct iwl_priv *priv, u8 *mac)
Zhu Yib481de92007-09-25 17:54:57 -07001403{
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001404 memcpy(mac, priv->eeprom39.mac_address, 6);
Zhu Yib481de92007-09-25 17:54:57 -07001405}
1406
Reinette Chatre74a3a252008-01-23 10:15:19 -08001407/*
1408 * Clear the OWNER_MSK, to establish driver (instead of uCode running on
1409 * embedded controller) as EEPROM reader; each read is a series of pulses
1410 * to/from the EEPROM chip, not a single event, so even reads could conflict
1411 * if they weren't arbitrated by some ownership mechanism. Here, the driver
1412 * simply claims ownership, which should be safe when this function is called
1413 * (i.e. before loading uCode!).
1414 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001415static inline int iwl3945_eeprom_acquire_semaphore(struct iwl_priv *priv)
Reinette Chatre74a3a252008-01-23 10:15:19 -08001416{
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08001417 _iwl_clear_bit(priv, CSR_EEPROM_GP, CSR_EEPROM_GP_IF_OWNER_MSK);
Reinette Chatre74a3a252008-01-23 10:15:19 -08001418 return 0;
1419}
1420
Zhu Yib481de92007-09-25 17:54:57 -07001421/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001422 * iwl3945_eeprom_init - read EEPROM contents
Zhu Yib481de92007-09-25 17:54:57 -07001423 *
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001424 * Load the EEPROM contents from adapter into priv->eeprom39
Zhu Yib481de92007-09-25 17:54:57 -07001425 *
1426 * NOTE: This routine uses the non-debug IO access functions.
1427 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001428int iwl3945_eeprom_init(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001429{
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001430 u16 *e = (u16 *)&priv->eeprom39;
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08001431 u32 gp = iwl_read32(priv, CSR_EEPROM_GP);
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001432 int sz = sizeof(priv->eeprom39);
Zhu, Yi3d5717a2008-12-11 10:33:36 -08001433 int ret;
Zhu Yib481de92007-09-25 17:54:57 -07001434 u16 addr;
1435
1436 /* The EEPROM structure has several padding buffers within it
1437 * and when adding new EEPROM maps is subject to programmer errors
1438 * which may be very difficult to identify without explicitly
1439 * checking the resulting size of the eeprom map. */
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001440 BUILD_BUG_ON(sizeof(priv->eeprom39) != IWL_EEPROM_IMAGE_SIZE);
Zhu Yib481de92007-09-25 17:54:57 -07001441
1442 if ((gp & CSR_EEPROM_GP_VALID_MSK) == CSR_EEPROM_GP_BAD_SIGNATURE) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08001443 IWL_ERR(priv, "EEPROM not found, EEPROM_GP=0x%08x\n", gp);
Zhu Yib481de92007-09-25 17:54:57 -07001444 return -ENOENT;
1445 }
1446
Cahill, Ben M6440adb2007-11-29 11:09:55 +08001447 /* Make sure driver (instead of uCode) is allowed to read EEPROM */
Zhu, Yi3d5717a2008-12-11 10:33:36 -08001448 ret = iwl3945_eeprom_acquire_semaphore(priv);
1449 if (ret < 0) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08001450 IWL_ERR(priv, "Failed to acquire EEPROM semaphore.\n");
Zhu Yib481de92007-09-25 17:54:57 -07001451 return -ENOENT;
1452 }
1453
1454 /* eeprom is an array of 16bit values */
1455 for (addr = 0; addr < sz; addr += sizeof(u16)) {
Zhu, Yi3d5717a2008-12-11 10:33:36 -08001456 u32 r;
1457
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08001458 _iwl_write32(priv, CSR_EEPROM_REG,
Zhu, Yi3d5717a2008-12-11 10:33:36 -08001459 CSR_EEPROM_REG_MSK_ADDR & (addr << 1));
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08001460 _iwl_clear_bit(priv, CSR_EEPROM_REG, CSR_EEPROM_REG_BIT_CMD);
1461 ret = iwl_poll_direct_bit(priv, CSR_EEPROM_REG,
Zhu, Yi3d5717a2008-12-11 10:33:36 -08001462 CSR_EEPROM_REG_READ_VALID_MSK,
1463 IWL_EEPROM_ACCESS_TIMEOUT);
1464 if (ret < 0) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08001465 IWL_ERR(priv, "Time out reading EEPROM[%d]\n", addr);
Zhu, Yi3d5717a2008-12-11 10:33:36 -08001466 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07001467 }
Zhu, Yi3d5717a2008-12-11 10:33:36 -08001468
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08001469 r = _iwl_read_direct32(priv, CSR_EEPROM_REG);
Tomas Winkler58ff6d42008-02-13 02:47:54 +02001470 e[addr / 2] = le16_to_cpu((__force __le16)(r >> 16));
Zhu Yib481de92007-09-25 17:54:57 -07001471 }
1472
1473 return 0;
1474}
1475
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001476static void iwl3945_unset_hw_params(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001477{
Abhijeet Kolekar3832ec92008-12-19 10:37:26 +08001478 if (priv->shared_virt)
Zhu Yib481de92007-09-25 17:54:57 -07001479 pci_free_consistent(priv->pci_dev,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001480 sizeof(struct iwl3945_shared),
Abhijeet Kolekar3832ec92008-12-19 10:37:26 +08001481 priv->shared_virt,
1482 priv->shared_phys);
Zhu Yib481de92007-09-25 17:54:57 -07001483}
1484
1485/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001486 * iwl3945_supported_rate_to_ie - fill in the supported rate in IE field
Zhu Yib481de92007-09-25 17:54:57 -07001487 *
1488 * return : set the bit for each supported rate insert in ie
1489 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001490static u16 iwl3945_supported_rate_to_ie(u8 *ie, u16 supported_rate,
Tomas Winklerc7c46672007-10-18 02:04:15 +02001491 u16 basic_rate, int *left)
Zhu Yib481de92007-09-25 17:54:57 -07001492{
1493 u16 ret_rates = 0, bit;
1494 int i;
Tomas Winklerc7c46672007-10-18 02:04:15 +02001495 u8 *cnt = ie;
1496 u8 *rates = ie + 1;
Zhu Yib481de92007-09-25 17:54:57 -07001497
1498 for (bit = 1, i = 0; i < IWL_RATE_COUNT; i++, bit <<= 1) {
1499 if (bit & supported_rate) {
1500 ret_rates |= bit;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001501 rates[*cnt] = iwl3945_rates[i].ieee |
Tomas Winklerc7c46672007-10-18 02:04:15 +02001502 ((bit & basic_rate) ? 0x80 : 0x00);
1503 (*cnt)++;
1504 (*left)--;
1505 if ((*left <= 0) ||
1506 (*cnt >= IWL_SUPPORTED_RATES_IE_LEN))
Zhu Yib481de92007-09-25 17:54:57 -07001507 break;
1508 }
1509 }
1510
1511 return ret_rates;
1512}
1513
1514/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001515 * iwl3945_fill_probe_req - fill in all required fields and IE for probe request
Zhu Yib481de92007-09-25 17:54:57 -07001516 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001517static u16 iwl3945_fill_probe_req(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07001518 struct ieee80211_mgmt *frame,
Johannes Berg430cfe92008-10-28 18:06:02 +01001519 int left)
Zhu Yib481de92007-09-25 17:54:57 -07001520{
1521 int len = 0;
1522 u8 *pos = NULL;
Tomas Winklerc7c46672007-10-18 02:04:15 +02001523 u16 active_rates, ret_rates, cck_rates;
Zhu Yib481de92007-09-25 17:54:57 -07001524
1525 /* Make sure there is enough space for the probe request,
1526 * two mandatory IEs and the data */
1527 left -= 24;
1528 if (left < 0)
1529 return 0;
1530 len += 24;
1531
1532 frame->frame_control = cpu_to_le16(IEEE80211_STYPE_PROBE_REQ);
Kolekar, Abhijeetb5323d32008-12-19 10:37:22 +08001533 memcpy(frame->da, iwl_bcast_addr, ETH_ALEN);
Zhu Yib481de92007-09-25 17:54:57 -07001534 memcpy(frame->sa, priv->mac_addr, ETH_ALEN);
Kolekar, Abhijeetb5323d32008-12-19 10:37:22 +08001535 memcpy(frame->bssid, iwl_bcast_addr, ETH_ALEN);
Zhu Yib481de92007-09-25 17:54:57 -07001536 frame->seq_ctrl = 0;
1537
1538 /* fill in our indirect SSID IE */
1539 /* ...next IE... */
1540
1541 left -= 2;
1542 if (left < 0)
1543 return 0;
1544 len += 2;
1545 pos = &(frame->u.probe_req.variable[0]);
1546 *pos++ = WLAN_EID_SSID;
1547 *pos++ = 0;
1548
Zhu Yib481de92007-09-25 17:54:57 -07001549 /* fill in supported rate */
1550 /* ...next IE... */
1551 left -= 2;
1552 if (left < 0)
1553 return 0;
Tomas Winklerc7c46672007-10-18 02:04:15 +02001554
Zhu Yib481de92007-09-25 17:54:57 -07001555 /* ... fill it in... */
1556 *pos++ = WLAN_EID_SUPP_RATES;
1557 *pos = 0;
Tomas Winklerc7c46672007-10-18 02:04:15 +02001558
1559 priv->active_rate = priv->rates_mask;
1560 active_rates = priv->active_rate;
Zhu Yib481de92007-09-25 17:54:57 -07001561 priv->active_rate_basic = priv->rates_mask & IWL_BASIC_RATES_MASK;
1562
Tomas Winklerc7c46672007-10-18 02:04:15 +02001563 cck_rates = IWL_CCK_RATES_MASK & active_rates;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001564 ret_rates = iwl3945_supported_rate_to_ie(pos, cck_rates,
Tomas Winklerc7c46672007-10-18 02:04:15 +02001565 priv->active_rate_basic, &left);
1566 active_rates &= ~ret_rates;
1567
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001568 ret_rates = iwl3945_supported_rate_to_ie(pos, active_rates,
Tomas Winklerc7c46672007-10-18 02:04:15 +02001569 priv->active_rate_basic, &left);
1570 active_rates &= ~ret_rates;
1571
Zhu Yib481de92007-09-25 17:54:57 -07001572 len += 2 + *pos;
1573 pos += (*pos) + 1;
Tomas Winklerc7c46672007-10-18 02:04:15 +02001574 if (active_rates == 0)
Zhu Yib481de92007-09-25 17:54:57 -07001575 goto fill_end;
1576
1577 /* fill in supported extended rate */
1578 /* ...next IE... */
1579 left -= 2;
1580 if (left < 0)
1581 return 0;
1582 /* ... fill it in... */
1583 *pos++ = WLAN_EID_EXT_SUPP_RATES;
1584 *pos = 0;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001585 iwl3945_supported_rate_to_ie(pos, active_rates,
Tomas Winklerc7c46672007-10-18 02:04:15 +02001586 priv->active_rate_basic, &left);
Zhu Yib481de92007-09-25 17:54:57 -07001587 if (*pos > 0)
1588 len += 2 + *pos;
1589
1590 fill_end:
1591 return (u16)len;
1592}
1593
1594/*
1595 * QoS support
1596*/
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001597static int iwl3945_send_qos_params_command(struct iwl_priv *priv,
Tomas Winkler4c897252008-12-19 10:37:05 +08001598 struct iwl_qosparam_cmd *qos)
Zhu Yib481de92007-09-25 17:54:57 -07001599{
1600
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001601 return iwl3945_send_cmd_pdu(priv, REPLY_QOS_PARAM,
Tomas Winkler4c897252008-12-19 10:37:05 +08001602 sizeof(struct iwl_qosparam_cmd), qos);
Zhu Yib481de92007-09-25 17:54:57 -07001603}
1604
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001605static void iwl3945_reset_qos(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001606{
1607 u16 cw_min = 15;
1608 u16 cw_max = 1023;
1609 u8 aifs = 2;
1610 u8 is_legacy = 0;
1611 unsigned long flags;
1612 int i;
1613
1614 spin_lock_irqsave(&priv->lock, flags);
1615 priv->qos_data.qos_active = 0;
1616
Winkler, Tomas6d1ef1a2008-12-09 11:29:00 -08001617 /* QoS always active in AP and ADHOC mode
1618 * In STA mode wait for association
1619 */
1620 if (priv->iw_mode == NL80211_IFTYPE_ADHOC ||
1621 priv->iw_mode == NL80211_IFTYPE_AP)
1622 priv->qos_data.qos_active = 1;
1623 else
1624 priv->qos_data.qos_active = 0;
1625
1626
1627 /* check for legacy mode */
1628 if ((priv->iw_mode == NL80211_IFTYPE_ADHOC &&
1629 (priv->active_rate & IWL_OFDM_RATES_MASK) == 0) ||
1630 (priv->iw_mode == NL80211_IFTYPE_STATION &&
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001631 (priv->staging39_rxon.flags & RXON_FLG_SHORT_SLOT_MSK) == 0)) {
Zhu Yib481de92007-09-25 17:54:57 -07001632 cw_min = 31;
1633 is_legacy = 1;
1634 }
1635
1636 if (priv->qos_data.qos_active)
1637 aifs = 3;
1638
1639 priv->qos_data.def_qos_parm.ac[0].cw_min = cpu_to_le16(cw_min);
1640 priv->qos_data.def_qos_parm.ac[0].cw_max = cpu_to_le16(cw_max);
1641 priv->qos_data.def_qos_parm.ac[0].aifsn = aifs;
1642 priv->qos_data.def_qos_parm.ac[0].edca_txop = 0;
1643 priv->qos_data.def_qos_parm.ac[0].reserved1 = 0;
1644
1645 if (priv->qos_data.qos_active) {
1646 i = 1;
1647 priv->qos_data.def_qos_parm.ac[i].cw_min = cpu_to_le16(cw_min);
1648 priv->qos_data.def_qos_parm.ac[i].cw_max = cpu_to_le16(cw_max);
1649 priv->qos_data.def_qos_parm.ac[i].aifsn = 7;
1650 priv->qos_data.def_qos_parm.ac[i].edca_txop = 0;
1651 priv->qos_data.def_qos_parm.ac[i].reserved1 = 0;
1652
1653 i = 2;
1654 priv->qos_data.def_qos_parm.ac[i].cw_min =
1655 cpu_to_le16((cw_min + 1) / 2 - 1);
1656 priv->qos_data.def_qos_parm.ac[i].cw_max =
1657 cpu_to_le16(cw_max);
1658 priv->qos_data.def_qos_parm.ac[i].aifsn = 2;
1659 if (is_legacy)
1660 priv->qos_data.def_qos_parm.ac[i].edca_txop =
1661 cpu_to_le16(6016);
1662 else
1663 priv->qos_data.def_qos_parm.ac[i].edca_txop =
1664 cpu_to_le16(3008);
1665 priv->qos_data.def_qos_parm.ac[i].reserved1 = 0;
1666
1667 i = 3;
1668 priv->qos_data.def_qos_parm.ac[i].cw_min =
1669 cpu_to_le16((cw_min + 1) / 4 - 1);
1670 priv->qos_data.def_qos_parm.ac[i].cw_max =
1671 cpu_to_le16((cw_max + 1) / 2 - 1);
1672 priv->qos_data.def_qos_parm.ac[i].aifsn = 2;
1673 priv->qos_data.def_qos_parm.ac[i].reserved1 = 0;
1674 if (is_legacy)
1675 priv->qos_data.def_qos_parm.ac[i].edca_txop =
1676 cpu_to_le16(3264);
1677 else
1678 priv->qos_data.def_qos_parm.ac[i].edca_txop =
1679 cpu_to_le16(1504);
1680 } else {
1681 for (i = 1; i < 4; i++) {
1682 priv->qos_data.def_qos_parm.ac[i].cw_min =
1683 cpu_to_le16(cw_min);
1684 priv->qos_data.def_qos_parm.ac[i].cw_max =
1685 cpu_to_le16(cw_max);
1686 priv->qos_data.def_qos_parm.ac[i].aifsn = aifs;
1687 priv->qos_data.def_qos_parm.ac[i].edca_txop = 0;
1688 priv->qos_data.def_qos_parm.ac[i].reserved1 = 0;
1689 }
1690 }
1691 IWL_DEBUG_QOS("set QoS to default \n");
1692
1693 spin_unlock_irqrestore(&priv->lock, flags);
1694}
1695
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001696static void iwl3945_activate_qos(struct iwl_priv *priv, u8 force)
Zhu Yib481de92007-09-25 17:54:57 -07001697{
1698 unsigned long flags;
1699
Zhu Yib481de92007-09-25 17:54:57 -07001700 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
1701 return;
1702
Zhu Yib481de92007-09-25 17:54:57 -07001703 spin_lock_irqsave(&priv->lock, flags);
1704 priv->qos_data.def_qos_parm.qos_flags = 0;
1705
1706 if (priv->qos_data.qos_cap.q_AP.queue_request &&
1707 !priv->qos_data.qos_cap.q_AP.txop_request)
1708 priv->qos_data.def_qos_parm.qos_flags |=
1709 QOS_PARAM_FLG_TXOP_TYPE_MSK;
1710
1711 if (priv->qos_data.qos_active)
1712 priv->qos_data.def_qos_parm.qos_flags |=
1713 QOS_PARAM_FLG_UPDATE_EDCA_MSK;
1714
1715 spin_unlock_irqrestore(&priv->lock, flags);
1716
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001717 if (force || iwl3945_is_associated(priv)) {
Tomas Winklera96a27f2008-10-23 23:48:56 -07001718 IWL_DEBUG_QOS("send QoS cmd with QoS active %d \n",
Zhu Yib481de92007-09-25 17:54:57 -07001719 priv->qos_data.qos_active);
1720
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001721 iwl3945_send_qos_params_command(priv,
Zhu Yib481de92007-09-25 17:54:57 -07001722 &(priv->qos_data.def_qos_parm));
1723 }
1724}
1725
Zhu Yib481de92007-09-25 17:54:57 -07001726/*
1727 * Power management (not Tx power!) functions
1728 */
1729#define MSEC_TO_USEC 1024
1730
Tomas Winkler600c0e12008-12-19 10:37:04 +08001731
1732#define NOSLP __constant_cpu_to_le16(0), 0, 0
1733#define SLP IWL_POWER_DRIVER_ALLOW_SLEEP_MSK, 0, 0
Zhu Yib481de92007-09-25 17:54:57 -07001734#define SLP_TIMEOUT(T) __constant_cpu_to_le32((T) * MSEC_TO_USEC)
1735#define SLP_VEC(X0, X1, X2, X3, X4) {__constant_cpu_to_le32(X0), \
1736 __constant_cpu_to_le32(X1), \
1737 __constant_cpu_to_le32(X2), \
1738 __constant_cpu_to_le32(X3), \
1739 __constant_cpu_to_le32(X4)}
1740
Zhu Yib481de92007-09-25 17:54:57 -07001741/* default power management (not Tx power) table values */
Tomas Winklera96a27f2008-10-23 23:48:56 -07001742/* for TIM 0-10 */
Samuel Ortiz1125eff2008-12-19 10:37:14 +08001743static struct iwl_power_vec_entry range_0[IWL39_POWER_AC] = {
Zhu Yib481de92007-09-25 17:54:57 -07001744 {{NOSLP, SLP_TIMEOUT(0), SLP_TIMEOUT(0), SLP_VEC(0, 0, 0, 0, 0)}, 0},
1745 {{SLP, SLP_TIMEOUT(200), SLP_TIMEOUT(500), SLP_VEC(1, 2, 3, 4, 4)}, 0},
1746 {{SLP, SLP_TIMEOUT(200), SLP_TIMEOUT(300), SLP_VEC(2, 4, 6, 7, 7)}, 0},
1747 {{SLP, SLP_TIMEOUT(50), SLP_TIMEOUT(100), SLP_VEC(2, 6, 9, 9, 10)}, 0},
1748 {{SLP, SLP_TIMEOUT(50), SLP_TIMEOUT(25), SLP_VEC(2, 7, 9, 9, 10)}, 1},
1749 {{SLP, SLP_TIMEOUT(25), SLP_TIMEOUT(25), SLP_VEC(4, 7, 10, 10, 10)}, 1}
1750};
1751
Tomas Winklera96a27f2008-10-23 23:48:56 -07001752/* for TIM > 10 */
Samuel Ortiz1125eff2008-12-19 10:37:14 +08001753static struct iwl_power_vec_entry range_1[IWL39_POWER_AC] = {
Zhu Yib481de92007-09-25 17:54:57 -07001754 {{NOSLP, SLP_TIMEOUT(0), SLP_TIMEOUT(0), SLP_VEC(0, 0, 0, 0, 0)}, 0},
1755 {{SLP, SLP_TIMEOUT(200), SLP_TIMEOUT(500),
1756 SLP_VEC(1, 2, 3, 4, 0xFF)}, 0},
1757 {{SLP, SLP_TIMEOUT(200), SLP_TIMEOUT(300),
1758 SLP_VEC(2, 4, 6, 7, 0xFF)}, 0},
1759 {{SLP, SLP_TIMEOUT(50), SLP_TIMEOUT(100),
1760 SLP_VEC(2, 6, 9, 9, 0xFF)}, 0},
1761 {{SLP, SLP_TIMEOUT(50), SLP_TIMEOUT(25), SLP_VEC(2, 7, 9, 9, 0xFF)}, 0},
1762 {{SLP, SLP_TIMEOUT(25), SLP_TIMEOUT(25),
1763 SLP_VEC(4, 7, 10, 10, 0xFF)}, 0}
1764};
1765
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001766int iwl3945_power_init_handle(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001767{
1768 int rc = 0, i;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001769 struct iwl3945_power_mgr *pow_data;
Samuel Ortiz1125eff2008-12-19 10:37:14 +08001770 int size = sizeof(struct iwl_power_vec_entry) * IWL39_POWER_AC;
Zhu Yib481de92007-09-25 17:54:57 -07001771 u16 pci_pm;
1772
1773 IWL_DEBUG_POWER("Initialize power \n");
1774
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001775 pow_data = &(priv->power_data_39);
Zhu Yib481de92007-09-25 17:54:57 -07001776
1777 memset(pow_data, 0, sizeof(*pow_data));
1778
1779 pow_data->active_index = IWL_POWER_RANGE_0;
1780 pow_data->dtim_val = 0xffff;
1781
1782 memcpy(&pow_data->pwr_range_0[0], &range_0[0], size);
1783 memcpy(&pow_data->pwr_range_1[0], &range_1[0], size);
1784
1785 rc = pci_read_config_word(priv->pci_dev, PCI_LINK_CTRL, &pci_pm);
1786 if (rc != 0)
1787 return 0;
1788 else {
Tomas Winkler600c0e12008-12-19 10:37:04 +08001789 struct iwl_powertable_cmd *cmd;
Zhu Yib481de92007-09-25 17:54:57 -07001790
1791 IWL_DEBUG_POWER("adjust power command flags\n");
1792
Samuel Ortiz1125eff2008-12-19 10:37:14 +08001793 for (i = 0; i < IWL39_POWER_AC; i++) {
Zhu Yib481de92007-09-25 17:54:57 -07001794 cmd = &pow_data->pwr_range_0[i].cmd;
1795
1796 if (pci_pm & 0x1)
1797 cmd->flags &= ~IWL_POWER_PCI_PM_MSK;
1798 else
1799 cmd->flags |= IWL_POWER_PCI_PM_MSK;
1800 }
1801 }
1802 return rc;
1803}
1804
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001805static int iwl3945_update_power_cmd(struct iwl_priv *priv,
Tomas Winkler600c0e12008-12-19 10:37:04 +08001806 struct iwl_powertable_cmd *cmd, u32 mode)
Zhu Yib481de92007-09-25 17:54:57 -07001807{
1808 int rc = 0, i;
1809 u8 skip;
1810 u32 max_sleep = 0;
Samuel Ortiz1125eff2008-12-19 10:37:14 +08001811 struct iwl_power_vec_entry *range;
Zhu Yib481de92007-09-25 17:54:57 -07001812 u8 period = 0;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001813 struct iwl3945_power_mgr *pow_data;
Zhu Yib481de92007-09-25 17:54:57 -07001814
1815 if (mode > IWL_POWER_INDEX_5) {
1816 IWL_DEBUG_POWER("Error invalid power mode \n");
1817 return -1;
1818 }
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001819 pow_data = &(priv->power_data_39);
Zhu Yib481de92007-09-25 17:54:57 -07001820
1821 if (pow_data->active_index == IWL_POWER_RANGE_0)
1822 range = &pow_data->pwr_range_0[0];
1823 else
1824 range = &pow_data->pwr_range_1[1];
1825
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001826 memcpy(cmd, &range[mode].cmd, sizeof(struct iwl3945_powertable_cmd));
Zhu Yib481de92007-09-25 17:54:57 -07001827
1828#ifdef IWL_MAC80211_DISABLE
1829 if (priv->assoc_network != NULL) {
1830 unsigned long flags;
1831
1832 period = priv->assoc_network->tim.tim_period;
1833 }
1834#endif /*IWL_MAC80211_DISABLE */
1835 skip = range[mode].no_dtim;
1836
1837 if (period == 0) {
1838 period = 1;
1839 skip = 0;
1840 }
1841
1842 if (skip == 0) {
1843 max_sleep = period;
1844 cmd->flags &= ~IWL_POWER_SLEEP_OVER_DTIM_MSK;
1845 } else {
1846 __le32 slp_itrvl = cmd->sleep_interval[IWL_POWER_VEC_SIZE - 1];
1847 max_sleep = (le32_to_cpu(slp_itrvl) / period) * period;
1848 cmd->flags |= IWL_POWER_SLEEP_OVER_DTIM_MSK;
1849 }
1850
1851 for (i = 0; i < IWL_POWER_VEC_SIZE; i++) {
1852 if (le32_to_cpu(cmd->sleep_interval[i]) > max_sleep)
1853 cmd->sleep_interval[i] = cpu_to_le32(max_sleep);
1854 }
1855
1856 IWL_DEBUG_POWER("Flags value = 0x%08X\n", cmd->flags);
1857 IWL_DEBUG_POWER("Tx timeout = %u\n", le32_to_cpu(cmd->tx_data_timeout));
1858 IWL_DEBUG_POWER("Rx timeout = %u\n", le32_to_cpu(cmd->rx_data_timeout));
1859 IWL_DEBUG_POWER("Sleep interval vector = { %d , %d , %d , %d , %d }\n",
1860 le32_to_cpu(cmd->sleep_interval[0]),
1861 le32_to_cpu(cmd->sleep_interval[1]),
1862 le32_to_cpu(cmd->sleep_interval[2]),
1863 le32_to_cpu(cmd->sleep_interval[3]),
1864 le32_to_cpu(cmd->sleep_interval[4]));
1865
1866 return rc;
1867}
1868
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001869static int iwl3945_send_power_mode(struct iwl_priv *priv, u32 mode)
Zhu Yib481de92007-09-25 17:54:57 -07001870{
John W. Linville9a62f732007-11-15 16:27:36 -05001871 u32 uninitialized_var(final_mode);
Zhu Yib481de92007-09-25 17:54:57 -07001872 int rc;
Tomas Winkler600c0e12008-12-19 10:37:04 +08001873 struct iwl_powertable_cmd cmd;
Zhu Yib481de92007-09-25 17:54:57 -07001874
1875 /* If on battery, set to 3,
Ian Schram01ebd062007-10-25 17:15:22 +08001876 * if plugged into AC power, set to CAM ("continuously aware mode"),
Zhu Yib481de92007-09-25 17:54:57 -07001877 * else user level */
1878 switch (mode) {
Samuel Ortiz1125eff2008-12-19 10:37:14 +08001879 case IWL39_POWER_BATTERY:
Zhu Yib481de92007-09-25 17:54:57 -07001880 final_mode = IWL_POWER_INDEX_3;
1881 break;
Samuel Ortiz1125eff2008-12-19 10:37:14 +08001882 case IWL39_POWER_AC:
Zhu Yib481de92007-09-25 17:54:57 -07001883 final_mode = IWL_POWER_MODE_CAM;
1884 break;
1885 default:
1886 final_mode = mode;
1887 break;
1888 }
1889
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001890 iwl3945_update_power_cmd(priv, &cmd, final_mode);
Zhu Yib481de92007-09-25 17:54:57 -07001891
Tomas Winkler600c0e12008-12-19 10:37:04 +08001892 /* FIXME use get_hcmd_size 3945 command is 4 bytes shorter */
1893 rc = iwl3945_send_cmd_pdu(priv, POWER_TABLE_CMD,
1894 sizeof(struct iwl3945_powertable_cmd), &cmd);
Zhu Yib481de92007-09-25 17:54:57 -07001895
1896 if (final_mode == IWL_POWER_MODE_CAM)
1897 clear_bit(STATUS_POWER_PMI, &priv->status);
1898 else
1899 set_bit(STATUS_POWER_PMI, &priv->status);
1900
1901 return rc;
1902}
1903
Zhu Yib481de92007-09-25 17:54:57 -07001904/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001905 * iwl3945_scan_cancel - Cancel any currently executing HW scan
Zhu Yib481de92007-09-25 17:54:57 -07001906 *
1907 * NOTE: priv->mutex is not required before calling this function
1908 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001909static int iwl3945_scan_cancel(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001910{
1911 if (!test_bit(STATUS_SCAN_HW, &priv->status)) {
1912 clear_bit(STATUS_SCANNING, &priv->status);
1913 return 0;
1914 }
1915
1916 if (test_bit(STATUS_SCANNING, &priv->status)) {
1917 if (!test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
1918 IWL_DEBUG_SCAN("Queuing scan abort.\n");
1919 set_bit(STATUS_SCAN_ABORTING, &priv->status);
1920 queue_work(priv->workqueue, &priv->abort_scan);
1921
1922 } else
1923 IWL_DEBUG_SCAN("Scan abort already in progress.\n");
1924
1925 return test_bit(STATUS_SCANNING, &priv->status);
1926 }
1927
1928 return 0;
1929}
1930
1931/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001932 * iwl3945_scan_cancel_timeout - Cancel any currently executing HW scan
Zhu Yib481de92007-09-25 17:54:57 -07001933 * @ms: amount of time to wait (in milliseconds) for scan to abort
1934 *
1935 * NOTE: priv->mutex must be held before calling this function
1936 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001937static int iwl3945_scan_cancel_timeout(struct iwl_priv *priv, unsigned long ms)
Zhu Yib481de92007-09-25 17:54:57 -07001938{
1939 unsigned long now = jiffies;
1940 int ret;
1941
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001942 ret = iwl3945_scan_cancel(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001943 if (ret && ms) {
1944 mutex_unlock(&priv->mutex);
1945 while (!time_after(jiffies, now + msecs_to_jiffies(ms)) &&
1946 test_bit(STATUS_SCANNING, &priv->status))
1947 msleep(1);
1948 mutex_lock(&priv->mutex);
1949
1950 return test_bit(STATUS_SCANNING, &priv->status);
1951 }
1952
1953 return ret;
1954}
1955
Zhu Yib481de92007-09-25 17:54:57 -07001956#define MAX_UCODE_BEACON_INTERVAL 1024
1957#define INTEL_CONN_LISTEN_INTERVAL __constant_cpu_to_le16(0xA)
1958
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001959static __le16 iwl3945_adjust_beacon_interval(u16 beacon_val)
Zhu Yib481de92007-09-25 17:54:57 -07001960{
1961 u16 new_val = 0;
1962 u16 beacon_factor = 0;
1963
1964 beacon_factor =
1965 (beacon_val + MAX_UCODE_BEACON_INTERVAL)
1966 / MAX_UCODE_BEACON_INTERVAL;
1967 new_val = beacon_val / beacon_factor;
1968
1969 return cpu_to_le16(new_val);
1970}
1971
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001972static void iwl3945_setup_rxon_timing(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001973{
1974 u64 interval_tm_unit;
1975 u64 tsf, result;
1976 unsigned long flags;
1977 struct ieee80211_conf *conf = NULL;
1978 u16 beacon_int = 0;
1979
1980 conf = ieee80211_get_hw_conf(priv->hw);
1981
1982 spin_lock_irqsave(&priv->lock, flags);
Tomas Winkler28afaf92008-12-19 10:37:06 +08001983 priv->rxon_timing.timestamp = cpu_to_le64(priv->timestamp);
Zhu Yib481de92007-09-25 17:54:57 -07001984 priv->rxon_timing.listen_interval = INTEL_CONN_LISTEN_INTERVAL;
1985
Tomas Winkler28afaf92008-12-19 10:37:06 +08001986 tsf = priv->timestamp;
Zhu Yib481de92007-09-25 17:54:57 -07001987
1988 beacon_int = priv->beacon_int;
1989 spin_unlock_irqrestore(&priv->lock, flags);
1990
Johannes Berg05c914f2008-09-11 00:01:58 +02001991 if (priv->iw_mode == NL80211_IFTYPE_STATION) {
Zhu Yib481de92007-09-25 17:54:57 -07001992 if (beacon_int == 0) {
1993 priv->rxon_timing.beacon_interval = cpu_to_le16(100);
1994 priv->rxon_timing.beacon_init_val = cpu_to_le32(102400);
1995 } else {
1996 priv->rxon_timing.beacon_interval =
1997 cpu_to_le16(beacon_int);
1998 priv->rxon_timing.beacon_interval =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001999 iwl3945_adjust_beacon_interval(
Zhu Yib481de92007-09-25 17:54:57 -07002000 le16_to_cpu(priv->rxon_timing.beacon_interval));
2001 }
2002
2003 priv->rxon_timing.atim_window = 0;
2004 } else {
2005 priv->rxon_timing.beacon_interval =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002006 iwl3945_adjust_beacon_interval(conf->beacon_int);
Zhu Yib481de92007-09-25 17:54:57 -07002007 /* TODO: we need to get atim_window from upper stack
2008 * for now we set to 0 */
2009 priv->rxon_timing.atim_window = 0;
2010 }
2011
2012 interval_tm_unit =
2013 (le16_to_cpu(priv->rxon_timing.beacon_interval) * 1024);
2014 result = do_div(tsf, interval_tm_unit);
2015 priv->rxon_timing.beacon_init_val =
2016 cpu_to_le32((u32) ((u64) interval_tm_unit - result));
2017
2018 IWL_DEBUG_ASSOC
2019 ("beacon interval %d beacon timer %d beacon tim %d\n",
2020 le16_to_cpu(priv->rxon_timing.beacon_interval),
2021 le32_to_cpu(priv->rxon_timing.beacon_init_val),
2022 le16_to_cpu(priv->rxon_timing.atim_window));
2023}
2024
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002025static int iwl3945_scan_initiate(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002026{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002027 if (!iwl3945_is_ready_rf(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07002028 IWL_DEBUG_SCAN("Aborting scan due to not ready.\n");
2029 return -EIO;
2030 }
2031
2032 if (test_bit(STATUS_SCANNING, &priv->status)) {
2033 IWL_DEBUG_SCAN("Scan already in progress.\n");
2034 return -EAGAIN;
2035 }
2036
2037 if (test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
2038 IWL_DEBUG_SCAN("Scan request while abort pending. "
2039 "Queuing.\n");
2040 return -EAGAIN;
2041 }
2042
2043 IWL_DEBUG_INFO("Starting scan...\n");
Ron Rindjunsky66b50042008-06-25 16:46:31 +08002044 if (priv->cfg->sku & IWL_SKU_G)
2045 priv->scan_bands |= BIT(IEEE80211_BAND_2GHZ);
2046 if (priv->cfg->sku & IWL_SKU_A)
2047 priv->scan_bands |= BIT(IEEE80211_BAND_5GHZ);
Zhu Yib481de92007-09-25 17:54:57 -07002048 set_bit(STATUS_SCANNING, &priv->status);
2049 priv->scan_start = jiffies;
2050 priv->scan_pass_start = priv->scan_start;
2051
2052 queue_work(priv->workqueue, &priv->request_scan);
2053
2054 return 0;
2055}
2056
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002057static int iwl3945_set_rxon_hwcrypto(struct iwl_priv *priv, int hw_decrypt)
Zhu Yib481de92007-09-25 17:54:57 -07002058{
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08002059 struct iwl3945_rxon_cmd *rxon = &priv->staging39_rxon;
Zhu Yib481de92007-09-25 17:54:57 -07002060
2061 if (hw_decrypt)
2062 rxon->filter_flags &= ~RXON_FILTER_DIS_DECRYPT_MSK;
2063 else
2064 rxon->filter_flags |= RXON_FILTER_DIS_DECRYPT_MSK;
2065
2066 return 0;
2067}
2068
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002069static void iwl3945_set_flags_for_phymode(struct iwl_priv *priv,
Johannes Berg8318d782008-01-24 19:38:38 +01002070 enum ieee80211_band band)
Zhu Yib481de92007-09-25 17:54:57 -07002071{
Johannes Berg8318d782008-01-24 19:38:38 +01002072 if (band == IEEE80211_BAND_5GHZ) {
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08002073 priv->staging39_rxon.flags &=
Zhu Yib481de92007-09-25 17:54:57 -07002074 ~(RXON_FLG_BAND_24G_MSK | RXON_FLG_AUTO_DETECT_MSK
2075 | RXON_FLG_CCK_MSK);
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08002076 priv->staging39_rxon.flags |= RXON_FLG_SHORT_SLOT_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07002077 } else {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002078 /* Copied from iwl3945_bg_post_associate() */
Zhu Yib481de92007-09-25 17:54:57 -07002079 if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_SLOT_TIME)
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08002080 priv->staging39_rxon.flags |= RXON_FLG_SHORT_SLOT_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07002081 else
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
Johannes Berg05c914f2008-09-11 00:01:58 +02002084 if (priv->iw_mode == NL80211_IFTYPE_ADHOC)
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08002085 priv->staging39_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07002086
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08002087 priv->staging39_rxon.flags |= RXON_FLG_BAND_24G_MSK;
2088 priv->staging39_rxon.flags |= RXON_FLG_AUTO_DETECT_MSK;
2089 priv->staging39_rxon.flags &= ~RXON_FLG_CCK_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07002090 }
2091}
2092
2093/*
Ian Schram01ebd062007-10-25 17:15:22 +08002094 * initialize rxon structure with default values from eeprom
Zhu Yib481de92007-09-25 17:54:57 -07002095 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002096static void iwl3945_connection_init_rx_config(struct iwl_priv *priv,
Zhu, Yi60294de2008-10-29 14:05:45 -07002097 int mode)
Zhu Yib481de92007-09-25 17:54:57 -07002098{
Samuel Ortizd20b3c62008-12-19 10:37:15 +08002099 const struct iwl_channel_info *ch_info;
Zhu Yib481de92007-09-25 17:54:57 -07002100
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08002101 memset(&priv->staging39_rxon, 0, sizeof(priv->staging39_rxon));
Zhu Yib481de92007-09-25 17:54:57 -07002102
Zhu, Yi60294de2008-10-29 14:05:45 -07002103 switch (mode) {
Johannes Berg05c914f2008-09-11 00:01:58 +02002104 case NL80211_IFTYPE_AP:
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08002105 priv->staging39_rxon.dev_type = RXON_DEV_TYPE_AP;
Zhu Yib481de92007-09-25 17:54:57 -07002106 break;
2107
Johannes Berg05c914f2008-09-11 00:01:58 +02002108 case NL80211_IFTYPE_STATION:
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08002109 priv->staging39_rxon.dev_type = RXON_DEV_TYPE_ESS;
2110 priv->staging39_rxon.filter_flags = RXON_FILTER_ACCEPT_GRP_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07002111 break;
2112
Johannes Berg05c914f2008-09-11 00:01:58 +02002113 case NL80211_IFTYPE_ADHOC:
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08002114 priv->staging39_rxon.dev_type = RXON_DEV_TYPE_IBSS;
2115 priv->staging39_rxon.flags = RXON_FLG_SHORT_PREAMBLE_MSK;
2116 priv->staging39_rxon.filter_flags = RXON_FILTER_BCON_AWARE_MSK |
Zhu Yib481de92007-09-25 17:54:57 -07002117 RXON_FILTER_ACCEPT_GRP_MSK;
2118 break;
2119
Johannes Berg05c914f2008-09-11 00:01:58 +02002120 case NL80211_IFTYPE_MONITOR:
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08002121 priv->staging39_rxon.dev_type = RXON_DEV_TYPE_SNIFFER;
2122 priv->staging39_rxon.filter_flags = RXON_FILTER_PROMISC_MSK |
Zhu Yib481de92007-09-25 17:54:57 -07002123 RXON_FILTER_CTL2HOST_MSK | RXON_FILTER_ACCEPT_GRP_MSK;
2124 break;
Tomas Winkler69dc5d92008-03-25 16:33:41 -07002125 default:
Winkler, Tomas15b16872008-12-19 10:37:33 +08002126 IWL_ERR(priv, "Unsupported interface type %d\n", mode);
Tomas Winkler69dc5d92008-03-25 16:33:41 -07002127 break;
Zhu Yib481de92007-09-25 17:54:57 -07002128 }
2129
2130#if 0
2131 /* TODO: Figure out when short_preamble would be set and cache from
2132 * that */
2133 if (!hw_to_local(priv->hw)->short_preamble)
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08002134 priv->staging39_rxon.flags &= ~RXON_FLG_SHORT_PREAMBLE_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07002135 else
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08002136 priv->staging39_rxon.flags |= RXON_FLG_SHORT_PREAMBLE_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07002137#endif
2138
Johannes Berg8318d782008-01-24 19:38:38 +01002139 ch_info = iwl3945_get_channel_info(priv, priv->band,
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08002140 le16_to_cpu(priv->active39_rxon.channel));
Zhu Yib481de92007-09-25 17:54:57 -07002141
2142 if (!ch_info)
2143 ch_info = &priv->channel_info[0];
2144
2145 /*
2146 * in some case A channels are all non IBSS
2147 * in this case force B/G channel
2148 */
Zhu, Yi60294de2008-10-29 14:05:45 -07002149 if ((mode == NL80211_IFTYPE_ADHOC) && !(is_channel_ibss(ch_info)))
Zhu Yib481de92007-09-25 17:54:57 -07002150 ch_info = &priv->channel_info[0];
2151
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08002152 priv->staging39_rxon.channel = cpu_to_le16(ch_info->channel);
Zhu Yib481de92007-09-25 17:54:57 -07002153 if (is_channel_a_band(ch_info))
Johannes Berg8318d782008-01-24 19:38:38 +01002154 priv->band = IEEE80211_BAND_5GHZ;
Zhu Yib481de92007-09-25 17:54:57 -07002155 else
Johannes Berg8318d782008-01-24 19:38:38 +01002156 priv->band = IEEE80211_BAND_2GHZ;
Zhu Yib481de92007-09-25 17:54:57 -07002157
Johannes Berg8318d782008-01-24 19:38:38 +01002158 iwl3945_set_flags_for_phymode(priv, priv->band);
Zhu Yib481de92007-09-25 17:54:57 -07002159
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08002160 priv->staging39_rxon.ofdm_basic_rates =
Zhu Yib481de92007-09-25 17:54:57 -07002161 (IWL_OFDM_RATES_MASK >> IWL_FIRST_OFDM_RATE) & 0xFF;
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08002162 priv->staging39_rxon.cck_basic_rates =
Zhu Yib481de92007-09-25 17:54:57 -07002163 (IWL_CCK_RATES_MASK >> IWL_FIRST_CCK_RATE) & 0xF;
2164}
2165
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002166static int iwl3945_set_mode(struct iwl_priv *priv, int mode)
Zhu Yib481de92007-09-25 17:54:57 -07002167{
Johannes Berg05c914f2008-09-11 00:01:58 +02002168 if (mode == NL80211_IFTYPE_ADHOC) {
Samuel Ortizd20b3c62008-12-19 10:37:15 +08002169 const struct iwl_channel_info *ch_info;
Zhu Yib481de92007-09-25 17:54:57 -07002170
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002171 ch_info = iwl3945_get_channel_info(priv,
Johannes Berg8318d782008-01-24 19:38:38 +01002172 priv->band,
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08002173 le16_to_cpu(priv->staging39_rxon.channel));
Zhu Yib481de92007-09-25 17:54:57 -07002174
2175 if (!ch_info || !is_channel_ibss(ch_info)) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08002176 IWL_ERR(priv, "channel %d not IBSS channel\n",
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08002177 le16_to_cpu(priv->staging39_rxon.channel));
Zhu Yib481de92007-09-25 17:54:57 -07002178 return -EINVAL;
2179 }
2180 }
2181
Zhu, Yi60294de2008-10-29 14:05:45 -07002182 iwl3945_connection_init_rx_config(priv, mode);
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08002183 memcpy(priv->staging39_rxon.node_addr, priv->mac_addr, ETH_ALEN);
Zhu Yib481de92007-09-25 17:54:57 -07002184
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002185 iwl3945_clear_stations_table(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002186
Tomas Winklera96a27f2008-10-23 23:48:56 -07002187 /* don't commit rxon if rf-kill is on*/
Mohamed Abbasfde35712007-11-29 11:10:15 +08002188 if (!iwl3945_is_ready_rf(priv))
2189 return -EAGAIN;
2190
2191 cancel_delayed_work(&priv->scan_check);
2192 if (iwl3945_scan_cancel_timeout(priv, 100)) {
Winkler, Tomas39aadf82008-12-19 10:37:32 +08002193 IWL_WARN(priv, "Aborted scan still in progress after 100ms\n");
Mohamed Abbasfde35712007-11-29 11:10:15 +08002194 IWL_DEBUG_MAC80211("leaving - scan abort failed.\n");
2195 return -EAGAIN;
2196 }
2197
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002198 iwl3945_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002199
2200 return 0;
2201}
2202
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002203static void iwl3945_build_tx_cmd_hwcrypto(struct iwl_priv *priv,
Johannes Berge039fa42008-05-15 12:55:29 +02002204 struct ieee80211_tx_info *info,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002205 struct iwl3945_cmd *cmd,
Zhu Yib481de92007-09-25 17:54:57 -07002206 struct sk_buff *skb_frag,
2207 int last_frag)
2208{
Ivo van Doorn1c014422008-04-17 19:41:02 +02002209 struct iwl3945_hw_key *keyinfo =
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08002210 &priv->stations_39[info->control.hw_key->hw_key_idx].keyinfo;
Zhu Yib481de92007-09-25 17:54:57 -07002211
2212 switch (keyinfo->alg) {
2213 case ALG_CCMP:
2214 cmd->cmd.tx.sec_ctl = TX_CMD_SEC_CCM;
2215 memcpy(cmd->cmd.tx.key, keyinfo->key, keyinfo->keylen);
Tomas Winklera96a27f2008-10-23 23:48:56 -07002216 IWL_DEBUG_TX("tx_cmd with AES hwcrypto\n");
Zhu Yib481de92007-09-25 17:54:57 -07002217 break;
2218
2219 case ALG_TKIP:
2220#if 0
2221 cmd->cmd.tx.sec_ctl = TX_CMD_SEC_TKIP;
2222
2223 if (last_frag)
2224 memcpy(cmd->cmd.tx.tkip_mic.byte, skb_frag->tail - 8,
2225 8);
2226 else
2227 memset(cmd->cmd.tx.tkip_mic.byte, 0, 8);
2228#endif
2229 break;
2230
2231 case ALG_WEP:
2232 cmd->cmd.tx.sec_ctl = TX_CMD_SEC_WEP |
Johannes Berge039fa42008-05-15 12:55:29 +02002233 (info->control.hw_key->hw_key_idx & TX_CMD_SEC_MSK) << TX_CMD_SEC_SHIFT;
Zhu Yib481de92007-09-25 17:54:57 -07002234
2235 if (keyinfo->keylen == 13)
2236 cmd->cmd.tx.sec_ctl |= TX_CMD_SEC_KEY128;
2237
2238 memcpy(&cmd->cmd.tx.key[3], keyinfo->key, keyinfo->keylen);
2239
2240 IWL_DEBUG_TX("Configuring packet for WEP encryption "
Johannes Berge039fa42008-05-15 12:55:29 +02002241 "with key %d\n", info->control.hw_key->hw_key_idx);
Zhu Yib481de92007-09-25 17:54:57 -07002242 break;
2243
Zhu Yib481de92007-09-25 17:54:57 -07002244 default:
Tomas Winkler978785a2008-12-19 10:37:31 +08002245 IWL_ERR(priv, "Unknown encode alg %d\n", keyinfo->alg);
Zhu Yib481de92007-09-25 17:54:57 -07002246 break;
2247 }
2248}
2249
2250/*
2251 * handle build REPLY_TX command notification.
2252 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002253static void iwl3945_build_tx_cmd_basic(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002254 struct iwl3945_cmd *cmd,
Johannes Berge039fa42008-05-15 12:55:29 +02002255 struct ieee80211_tx_info *info,
Zhu Yib481de92007-09-25 17:54:57 -07002256 struct ieee80211_hdr *hdr,
2257 int is_unicast, u8 std_id)
2258{
Harvey Harrisonfd7c8a42008-06-11 14:21:56 -07002259 __le16 fc = hdr->frame_control;
Zhu Yib481de92007-09-25 17:54:57 -07002260 __le32 tx_flags = cmd->cmd.tx.tx_flags;
Johannes Berge6a98542008-10-21 12:40:02 +02002261 u8 rc_flags = info->control.rates[0].flags;
Zhu Yib481de92007-09-25 17:54:57 -07002262
2263 cmd->cmd.tx.stop_time.life_time = TX_CMD_LIFE_TIME_INFINITE;
Johannes Berge039fa42008-05-15 12:55:29 +02002264 if (!(info->flags & IEEE80211_TX_CTL_NO_ACK)) {
Zhu Yib481de92007-09-25 17:54:57 -07002265 tx_flags |= TX_CMD_FLG_ACK_MSK;
Harvey Harrisonfd7c8a42008-06-11 14:21:56 -07002266 if (ieee80211_is_mgmt(fc))
Zhu Yib481de92007-09-25 17:54:57 -07002267 tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
Harvey Harrisonfd7c8a42008-06-11 14:21:56 -07002268 if (ieee80211_is_probe_resp(fc) &&
Zhu Yib481de92007-09-25 17:54:57 -07002269 !(le16_to_cpu(hdr->seq_ctrl) & 0xf))
2270 tx_flags |= TX_CMD_FLG_TSF_MSK;
2271 } else {
2272 tx_flags &= (~TX_CMD_FLG_ACK_MSK);
2273 tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
2274 }
2275
2276 cmd->cmd.tx.sta_id = std_id;
Harvey Harrison8b7b1e02008-06-11 14:21:56 -07002277 if (ieee80211_has_morefrags(fc))
Zhu Yib481de92007-09-25 17:54:57 -07002278 tx_flags |= TX_CMD_FLG_MORE_FRAG_MSK;
2279
Harvey Harrisonfd7c8a42008-06-11 14:21:56 -07002280 if (ieee80211_is_data_qos(fc)) {
2281 u8 *qc = ieee80211_get_qos_ctl(hdr);
Tomas Winkler54dbb522008-05-15 13:54:06 +08002282 cmd->cmd.tx.tid_tspec = qc[0] & 0xf;
Zhu Yib481de92007-09-25 17:54:57 -07002283 tx_flags &= ~TX_CMD_FLG_SEQ_CTL_MSK;
Tomas Winkler54dbb522008-05-15 13:54:06 +08002284 } else {
Zhu Yib481de92007-09-25 17:54:57 -07002285 tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
Tomas Winkler54dbb522008-05-15 13:54:06 +08002286 }
Zhu Yib481de92007-09-25 17:54:57 -07002287
Johannes Berge6a98542008-10-21 12:40:02 +02002288 if (rc_flags & IEEE80211_TX_RC_USE_RTS_CTS) {
Zhu Yib481de92007-09-25 17:54:57 -07002289 tx_flags |= TX_CMD_FLG_RTS_MSK;
2290 tx_flags &= ~TX_CMD_FLG_CTS_MSK;
Johannes Berge6a98542008-10-21 12:40:02 +02002291 } else if (rc_flags & IEEE80211_TX_RC_USE_CTS_PROTECT) {
Zhu Yib481de92007-09-25 17:54:57 -07002292 tx_flags &= ~TX_CMD_FLG_RTS_MSK;
2293 tx_flags |= TX_CMD_FLG_CTS_MSK;
2294 }
2295
2296 if ((tx_flags & TX_CMD_FLG_RTS_MSK) || (tx_flags & TX_CMD_FLG_CTS_MSK))
2297 tx_flags |= TX_CMD_FLG_FULL_TXOP_PROT_MSK;
2298
2299 tx_flags &= ~(TX_CMD_FLG_ANT_SEL_MSK);
Harvey Harrisonfd7c8a42008-06-11 14:21:56 -07002300 if (ieee80211_is_mgmt(fc)) {
2301 if (ieee80211_is_assoc_req(fc) || ieee80211_is_reassoc_req(fc))
Ian Schrambc434dd2007-10-25 17:15:29 +08002302 cmd->cmd.tx.timeout.pm_frame_timeout = cpu_to_le16(3);
Zhu Yib481de92007-09-25 17:54:57 -07002303 else
Ian Schrambc434dd2007-10-25 17:15:29 +08002304 cmd->cmd.tx.timeout.pm_frame_timeout = cpu_to_le16(2);
Mohamed Abbasab53d8a2008-03-25 16:33:36 -07002305 } else {
Zhu Yib481de92007-09-25 17:54:57 -07002306 cmd->cmd.tx.timeout.pm_frame_timeout = 0;
Mohamed Abbasab53d8a2008-03-25 16:33:36 -07002307#ifdef CONFIG_IWL3945_LEDS
2308 priv->rxtxpackets += le16_to_cpu(cmd->cmd.tx.len);
2309#endif
2310 }
Zhu Yib481de92007-09-25 17:54:57 -07002311
2312 cmd->cmd.tx.driver_txop = 0;
2313 cmd->cmd.tx.tx_flags = tx_flags;
2314 cmd->cmd.tx.next_frame_len = 0;
2315}
2316
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002317/**
2318 * iwl3945_get_sta_id - Find station's index within station table
2319 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002320static int iwl3945_get_sta_id(struct iwl_priv *priv, struct ieee80211_hdr *hdr)
Zhu Yib481de92007-09-25 17:54:57 -07002321{
2322 int sta_id;
2323 u16 fc = le16_to_cpu(hdr->frame_control);
2324
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002325 /* If this frame is broadcast or management, use broadcast station id */
Zhu Yib481de92007-09-25 17:54:57 -07002326 if (((fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_DATA) ||
2327 is_multicast_ether_addr(hdr->addr1))
Abhijeet Kolekar3832ec92008-12-19 10:37:26 +08002328 return priv->hw_params.bcast_sta_id;
Zhu Yib481de92007-09-25 17:54:57 -07002329
2330 switch (priv->iw_mode) {
2331
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002332 /* If we are a client station in a BSS network, use the special
2333 * AP station entry (that's the only station we communicate with) */
Johannes Berg05c914f2008-09-11 00:01:58 +02002334 case NL80211_IFTYPE_STATION:
Zhu Yib481de92007-09-25 17:54:57 -07002335 return IWL_AP_ID;
2336
2337 /* If we are an AP, then find the station, or use BCAST */
Johannes Berg05c914f2008-09-11 00:01:58 +02002338 case NL80211_IFTYPE_AP:
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002339 sta_id = iwl3945_hw_find_station(priv, hdr->addr1);
Zhu Yib481de92007-09-25 17:54:57 -07002340 if (sta_id != IWL_INVALID_STATION)
2341 return sta_id;
Abhijeet Kolekar3832ec92008-12-19 10:37:26 +08002342 return priv->hw_params.bcast_sta_id;
Zhu Yib481de92007-09-25 17:54:57 -07002343
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002344 /* If this frame is going out to an IBSS network, find the station,
2345 * or create a new station table entry */
Johannes Berg05c914f2008-09-11 00:01:58 +02002346 case NL80211_IFTYPE_ADHOC: {
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002347 /* Create new station table entry */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002348 sta_id = iwl3945_hw_find_station(priv, hdr->addr1);
Zhu Yib481de92007-09-25 17:54:57 -07002349 if (sta_id != IWL_INVALID_STATION)
2350 return sta_id;
2351
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002352 sta_id = iwl3945_add_station(priv, hdr->addr1, 0, CMD_ASYNC);
Zhu Yib481de92007-09-25 17:54:57 -07002353
2354 if (sta_id != IWL_INVALID_STATION)
2355 return sta_id;
2356
Johannes Berge1749612008-10-27 15:59:26 -07002357 IWL_DEBUG_DROP("Station %pM not in station map. "
Zhu Yib481de92007-09-25 17:54:57 -07002358 "Defaulting to broadcast...\n",
Johannes Berge1749612008-10-27 15:59:26 -07002359 hdr->addr1);
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08002360 iwl_print_hex_dump(priv, IWL_DL_DROP, (u8 *) hdr, sizeof(*hdr));
Abhijeet Kolekar3832ec92008-12-19 10:37:26 +08002361 return priv->hw_params.bcast_sta_id;
Joe Perches0795af52007-10-03 17:59:30 -07002362 }
Stefanik Gábor914233d2008-06-30 17:23:30 +08002363 /* If we are in monitor mode, use BCAST. This is required for
2364 * packet injection. */
Johannes Berg05c914f2008-09-11 00:01:58 +02002365 case NL80211_IFTYPE_MONITOR:
Abhijeet Kolekar3832ec92008-12-19 10:37:26 +08002366 return priv->hw_params.bcast_sta_id;
Stefanik Gábor914233d2008-06-30 17:23:30 +08002367
Zhu Yib481de92007-09-25 17:54:57 -07002368 default:
Winkler, Tomas39aadf82008-12-19 10:37:32 +08002369 IWL_WARN(priv, "Unknown mode of operation: %d\n",
2370 priv->iw_mode);
Abhijeet Kolekar3832ec92008-12-19 10:37:26 +08002371 return priv->hw_params.bcast_sta_id;
Zhu Yib481de92007-09-25 17:54:57 -07002372 }
2373}
2374
2375/*
2376 * start REPLY_TX command process
2377 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002378static int iwl3945_tx_skb(struct iwl_priv *priv, struct sk_buff *skb)
Zhu Yib481de92007-09-25 17:54:57 -07002379{
2380 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
Johannes Berge039fa42008-05-15 12:55:29 +02002381 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002382 struct iwl3945_tfd_frame *tfd;
Zhu Yib481de92007-09-25 17:54:57 -07002383 u32 *control_flags;
Johannes Berge2530082008-05-17 00:57:14 +02002384 int txq_id = skb_get_queue_mapping(skb);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002385 struct iwl3945_tx_queue *txq = NULL;
Samuel Ortizd20b3c62008-12-19 10:37:15 +08002386 struct iwl_queue *q = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07002387 dma_addr_t phys_addr;
2388 dma_addr_t txcmd_phys;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002389 struct iwl3945_cmd *out_cmd = NULL;
Tomas Winkler54dbb522008-05-15 13:54:06 +08002390 u16 len, idx, len_org, hdr_len;
2391 u8 id;
2392 u8 unicast;
Zhu Yib481de92007-09-25 17:54:57 -07002393 u8 sta_id;
Tomas Winkler54dbb522008-05-15 13:54:06 +08002394 u8 tid = 0;
Zhu Yib481de92007-09-25 17:54:57 -07002395 u16 seq_number = 0;
Harvey Harrisonfd7c8a42008-06-11 14:21:56 -07002396 __le16 fc;
Zhu Yib481de92007-09-25 17:54:57 -07002397 u8 wait_write_ptr = 0;
Tomas Winkler54dbb522008-05-15 13:54:06 +08002398 u8 *qc = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07002399 unsigned long flags;
2400 int rc;
2401
2402 spin_lock_irqsave(&priv->lock, flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002403 if (iwl3945_is_rfkill(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07002404 IWL_DEBUG_DROP("Dropping - RF KILL\n");
2405 goto drop_unlock;
2406 }
2407
Johannes Berge039fa42008-05-15 12:55:29 +02002408 if ((ieee80211_get_tx_rate(priv->hw, info)->hw_value & 0xFF) == IWL_INVALID_RATE) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08002409 IWL_ERR(priv, "ERROR: No TX rate available.\n");
Zhu Yib481de92007-09-25 17:54:57 -07002410 goto drop_unlock;
2411 }
2412
2413 unicast = !is_multicast_ether_addr(hdr->addr1);
2414 id = 0;
2415
Harvey Harrisonfd7c8a42008-06-11 14:21:56 -07002416 fc = hdr->frame_control;
Zhu Yib481de92007-09-25 17:54:57 -07002417
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08002418#ifdef CONFIG_IWL3945_DEBUG
Zhu Yib481de92007-09-25 17:54:57 -07002419 if (ieee80211_is_auth(fc))
2420 IWL_DEBUG_TX("Sending AUTH frame\n");
Harvey Harrisonfd7c8a42008-06-11 14:21:56 -07002421 else if (ieee80211_is_assoc_req(fc))
Zhu Yib481de92007-09-25 17:54:57 -07002422 IWL_DEBUG_TX("Sending ASSOC frame\n");
Harvey Harrisonfd7c8a42008-06-11 14:21:56 -07002423 else if (ieee80211_is_reassoc_req(fc))
Zhu Yib481de92007-09-25 17:54:57 -07002424 IWL_DEBUG_TX("Sending REASSOC frame\n");
2425#endif
2426
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08002427 /* drop all data frame if we are not associated */
Stefanik Gábor914233d2008-06-30 17:23:30 +08002428 if (ieee80211_is_data(fc) &&
Johannes Berg05c914f2008-09-11 00:01:58 +02002429 (priv->iw_mode != NL80211_IFTYPE_MONITOR) && /* packet injection */
Stefanik Gábor914233d2008-06-30 17:23:30 +08002430 (!iwl3945_is_associated(priv) ||
Johannes Berg05c914f2008-09-11 00:01:58 +02002431 ((priv->iw_mode == NL80211_IFTYPE_STATION) && !priv->assoc_id))) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002432 IWL_DEBUG_DROP("Dropping - !iwl3945_is_associated\n");
Zhu Yib481de92007-09-25 17:54:57 -07002433 goto drop_unlock;
2434 }
2435
2436 spin_unlock_irqrestore(&priv->lock, flags);
2437
Harvey Harrison7294ec92008-07-15 18:43:59 -07002438 hdr_len = ieee80211_hdrlen(fc);
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002439
2440 /* Find (or create) index into station table for destination station */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002441 sta_id = iwl3945_get_sta_id(priv, hdr);
Zhu Yib481de92007-09-25 17:54:57 -07002442 if (sta_id == IWL_INVALID_STATION) {
Johannes Berge1749612008-10-27 15:59:26 -07002443 IWL_DEBUG_DROP("Dropping - INVALID STATION: %pM\n",
2444 hdr->addr1);
Zhu Yib481de92007-09-25 17:54:57 -07002445 goto drop;
2446 }
2447
2448 IWL_DEBUG_RATE("station Id %d\n", sta_id);
2449
Harvey Harrisonfd7c8a42008-06-11 14:21:56 -07002450 if (ieee80211_is_data_qos(fc)) {
2451 qc = ieee80211_get_qos_ctl(hdr);
Harvey Harrison7294ec92008-07-15 18:43:59 -07002452 tid = qc[0] & IEEE80211_QOS_CTL_TID_MASK;
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08002453 seq_number = priv->stations_39[sta_id].tid[tid].seq_number &
Zhu Yib481de92007-09-25 17:54:57 -07002454 IEEE80211_SCTL_SEQ;
2455 hdr->seq_ctrl = cpu_to_le16(seq_number) |
2456 (hdr->seq_ctrl &
2457 __constant_cpu_to_le16(IEEE80211_SCTL_FRAG));
2458 seq_number += 0x10;
2459 }
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002460
2461 /* Descriptor for chosen Tx queue */
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08002462 txq = &priv->txq39[txq_id];
Zhu Yib481de92007-09-25 17:54:57 -07002463 q = &txq->q;
2464
2465 spin_lock_irqsave(&priv->lock, flags);
2466
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002467 /* Set up first empty TFD within this queue's circular TFD buffer */
Tomas Winklerfc4b6852007-10-25 17:15:24 +08002468 tfd = &txq->bd[q->write_ptr];
Zhu Yib481de92007-09-25 17:54:57 -07002469 memset(tfd, 0, sizeof(*tfd));
2470 control_flags = (u32 *) tfd;
Tomas Winklerfc4b6852007-10-25 17:15:24 +08002471 idx = get_cmd_index(q, q->write_ptr, 0);
Zhu Yib481de92007-09-25 17:54:57 -07002472
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002473 /* Set up driver data for this TFD */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002474 memset(&(txq->txb[q->write_ptr]), 0, sizeof(struct iwl3945_tx_info));
Tomas Winklerfc4b6852007-10-25 17:15:24 +08002475 txq->txb[q->write_ptr].skb[0] = skb;
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002476
2477 /* Init first empty entry in queue's array of Tx/cmd buffers */
Zhu Yib481de92007-09-25 17:54:57 -07002478 out_cmd = &txq->cmd[idx];
2479 memset(&out_cmd->hdr, 0, sizeof(out_cmd->hdr));
2480 memset(&out_cmd->cmd.tx, 0, sizeof(out_cmd->cmd.tx));
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002481
2482 /*
2483 * Set up the Tx-command (not MAC!) header.
2484 * Store the chosen Tx queue and TFD index within the sequence field;
2485 * after Tx, uCode's Tx response will return this value so driver can
2486 * locate the frame within the tx queue and do post-tx processing.
2487 */
Zhu Yib481de92007-09-25 17:54:57 -07002488 out_cmd->hdr.cmd = REPLY_TX;
2489 out_cmd->hdr.sequence = cpu_to_le16((u16)(QUEUE_TO_SEQ(txq_id) |
Tomas Winklerfc4b6852007-10-25 17:15:24 +08002490 INDEX_TO_SEQ(q->write_ptr)));
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002491
2492 /* Copy MAC header from skb into command buffer */
Zhu Yib481de92007-09-25 17:54:57 -07002493 memcpy(out_cmd->cmd.tx.hdr, hdr, hdr_len);
2494
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002495 /*
2496 * Use the first empty entry in this queue's command buffer array
2497 * to contain the Tx command and MAC header concatenated together
2498 * (payload data will be in another buffer).
2499 * Size of this varies, due to varying MAC header length.
2500 * If end is not dword aligned, we'll have 2 extra bytes at the end
2501 * of the MAC header (device reads on dword boundaries).
2502 * We'll tell device about this padding later.
2503 */
Abhijeet Kolekar3832ec92008-12-19 10:37:26 +08002504 len = sizeof(struct iwl3945_tx_cmd) +
Tomas Winkler4c897252008-12-19 10:37:05 +08002505 sizeof(struct iwl_cmd_header) + hdr_len;
Zhu Yib481de92007-09-25 17:54:57 -07002506
2507 len_org = len;
2508 len = (len + 3) & ~3;
2509
2510 if (len_org != len)
2511 len_org = 1;
2512 else
2513 len_org = 0;
2514
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002515 /* Physical address of this Tx command's header (not MAC header!),
2516 * within command buffer array. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002517 txcmd_phys = txq->dma_addr_cmd + sizeof(struct iwl3945_cmd) * idx +
2518 offsetof(struct iwl3945_cmd, hdr);
Zhu Yib481de92007-09-25 17:54:57 -07002519
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002520 /* Add buffer containing Tx command and MAC(!) header to TFD's
2521 * first entry */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002522 iwl3945_hw_txq_attach_buf_to_tfd(priv, tfd, txcmd_phys, len);
Zhu Yib481de92007-09-25 17:54:57 -07002523
Johannes Bergd0f09802008-07-29 11:32:07 +02002524 if (info->control.hw_key)
Johannes Berge039fa42008-05-15 12:55:29 +02002525 iwl3945_build_tx_cmd_hwcrypto(priv, info, out_cmd, skb, 0);
Zhu Yib481de92007-09-25 17:54:57 -07002526
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002527 /* Set up TFD's 2nd entry to point directly to remainder of skb,
2528 * if any (802.11 null frames have no payload). */
Zhu Yib481de92007-09-25 17:54:57 -07002529 len = skb->len - hdr_len;
2530 if (len) {
2531 phys_addr = pci_map_single(priv->pci_dev, skb->data + hdr_len,
2532 len, PCI_DMA_TODEVICE);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002533 iwl3945_hw_txq_attach_buf_to_tfd(priv, tfd, phys_addr, len);
Zhu Yib481de92007-09-25 17:54:57 -07002534 }
2535
Zhu Yib481de92007-09-25 17:54:57 -07002536 if (!len)
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002537 /* If there is no payload, then we use only one Tx buffer */
Zhu Yib481de92007-09-25 17:54:57 -07002538 *control_flags = TFD_CTL_COUNT_SET(1);
2539 else
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002540 /* Else use 2 buffers.
2541 * Tell 3945 about any padding after MAC header */
Zhu Yib481de92007-09-25 17:54:57 -07002542 *control_flags = TFD_CTL_COUNT_SET(2) |
2543 TFD_CTL_PAD_SET(U32_PAD(len));
2544
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002545 /* Total # bytes to be transmitted */
Zhu Yib481de92007-09-25 17:54:57 -07002546 len = (u16)skb->len;
2547 out_cmd->cmd.tx.len = cpu_to_le16(len);
2548
2549 /* TODO need this for burst mode later on */
Johannes Berge039fa42008-05-15 12:55:29 +02002550 iwl3945_build_tx_cmd_basic(priv, out_cmd, info, hdr, unicast, sta_id);
Zhu Yib481de92007-09-25 17:54:57 -07002551
2552 /* set is_hcca to 0; it probably will never be implemented */
Johannes Berge039fa42008-05-15 12:55:29 +02002553 iwl3945_hw_build_tx_cmd_rate(priv, out_cmd, info, hdr, sta_id, 0);
Zhu Yib481de92007-09-25 17:54:57 -07002554
2555 out_cmd->cmd.tx.tx_flags &= ~TX_CMD_FLG_ANT_A_MSK;
2556 out_cmd->cmd.tx.tx_flags &= ~TX_CMD_FLG_ANT_B_MSK;
2557
Harvey Harrison8b7b1e02008-06-11 14:21:56 -07002558 if (!ieee80211_has_morefrags(hdr->frame_control)) {
Zhu Yib481de92007-09-25 17:54:57 -07002559 txq->need_update = 1;
Tomas Winkler3ac7f142008-07-21 02:40:14 +03002560 if (qc)
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08002561 priv->stations_39[sta_id].tid[tid].seq_number = seq_number;
Zhu Yib481de92007-09-25 17:54:57 -07002562 } else {
2563 wait_write_ptr = 1;
2564 txq->need_update = 0;
2565 }
2566
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08002567 iwl_print_hex_dump(priv, IWL_DL_TX, out_cmd->cmd.payload,
Zhu Yib481de92007-09-25 17:54:57 -07002568 sizeof(out_cmd->cmd.tx));
2569
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08002570 iwl_print_hex_dump(priv, IWL_DL_TX, (u8 *)out_cmd->cmd.tx.hdr,
Harvey Harrison7294ec92008-07-15 18:43:59 -07002571 ieee80211_hdrlen(fc));
Zhu Yib481de92007-09-25 17:54:57 -07002572
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002573 /* Tell device the write index *just past* this latest filled TFD */
Tomas Winklerc54b6792008-03-06 17:36:53 -08002574 q->write_ptr = iwl_queue_inc_wrap(q->write_ptr, q->n_bd);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002575 rc = iwl3945_tx_queue_update_write_ptr(priv, txq);
Zhu Yib481de92007-09-25 17:54:57 -07002576 spin_unlock_irqrestore(&priv->lock, flags);
2577
2578 if (rc)
2579 return rc;
2580
Samuel Ortizd20b3c62008-12-19 10:37:15 +08002581 if ((iwl_queue_space(q) < q->high_mark)
Zhu Yib481de92007-09-25 17:54:57 -07002582 && priv->mac80211_registered) {
2583 if (wait_write_ptr) {
2584 spin_lock_irqsave(&priv->lock, flags);
2585 txq->need_update = 1;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002586 iwl3945_tx_queue_update_write_ptr(priv, txq);
Zhu Yib481de92007-09-25 17:54:57 -07002587 spin_unlock_irqrestore(&priv->lock, flags);
2588 }
2589
Johannes Berge2530082008-05-17 00:57:14 +02002590 ieee80211_stop_queue(priv->hw, skb_get_queue_mapping(skb));
Zhu Yib481de92007-09-25 17:54:57 -07002591 }
2592
2593 return 0;
2594
2595drop_unlock:
2596 spin_unlock_irqrestore(&priv->lock, flags);
2597drop:
2598 return -1;
2599}
2600
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002601static void iwl3945_set_rate(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002602{
Johannes Berg8318d782008-01-24 19:38:38 +01002603 const struct ieee80211_supported_band *sband = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07002604 struct ieee80211_rate *rate;
2605 int i;
2606
Johannes Berg8318d782008-01-24 19:38:38 +01002607 sband = iwl3945_get_band(priv, priv->band);
2608 if (!sband) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08002609 IWL_ERR(priv, "Failed to set rate: unable to get hw mode\n");
Saleem Abdulrasoolc4ba9622007-11-18 23:59:08 -08002610 return;
2611 }
Zhu Yib481de92007-09-25 17:54:57 -07002612
2613 priv->active_rate = 0;
2614 priv->active_rate_basic = 0;
2615
Johannes Berg8318d782008-01-24 19:38:38 +01002616 IWL_DEBUG_RATE("Setting rates for %s GHz\n",
2617 sband->band == IEEE80211_BAND_2GHZ ? "2.4" : "5");
Zhu Yib481de92007-09-25 17:54:57 -07002618
Johannes Berg8318d782008-01-24 19:38:38 +01002619 for (i = 0; i < sband->n_bitrates; i++) {
2620 rate = &sband->bitrates[i];
2621 if ((rate->hw_value < IWL_RATE_COUNT) &&
2622 !(rate->flags & IEEE80211_CHAN_DISABLED)) {
2623 IWL_DEBUG_RATE("Adding rate index %d (plcp %d)\n",
2624 rate->hw_value, iwl3945_rates[rate->hw_value].plcp);
2625 priv->active_rate |= (1 << rate->hw_value);
2626 }
Zhu Yib481de92007-09-25 17:54:57 -07002627 }
2628
2629 IWL_DEBUG_RATE("Set active_rate = %0x, active_rate_basic = %0x\n",
2630 priv->active_rate, priv->active_rate_basic);
2631
2632 /*
2633 * If a basic rate is configured, then use it (adding IWL_RATE_1M_MASK)
2634 * otherwise set it to the default of all CCK rates and 6, 12, 24 for
2635 * OFDM
2636 */
2637 if (priv->active_rate_basic & IWL_CCK_BASIC_RATES_MASK)
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08002638 priv->staging39_rxon.cck_basic_rates =
Zhu Yib481de92007-09-25 17:54:57 -07002639 ((priv->active_rate_basic &
2640 IWL_CCK_RATES_MASK) >> IWL_FIRST_CCK_RATE) & 0xF;
2641 else
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08002642 priv->staging39_rxon.cck_basic_rates =
Zhu Yib481de92007-09-25 17:54:57 -07002643 (IWL_CCK_BASIC_RATES_MASK >> IWL_FIRST_CCK_RATE) & 0xF;
2644
2645 if (priv->active_rate_basic & IWL_OFDM_BASIC_RATES_MASK)
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08002646 priv->staging39_rxon.ofdm_basic_rates =
Zhu Yib481de92007-09-25 17:54:57 -07002647 ((priv->active_rate_basic &
2648 (IWL_OFDM_BASIC_RATES_MASK | IWL_RATE_6M_MASK)) >>
2649 IWL_FIRST_OFDM_RATE) & 0xFF;
2650 else
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08002651 priv->staging39_rxon.ofdm_basic_rates =
Zhu Yib481de92007-09-25 17:54:57 -07002652 (IWL_OFDM_BASIC_RATES_MASK >> IWL_FIRST_OFDM_RATE) & 0xFF;
2653}
2654
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002655static void iwl3945_radio_kill_sw(struct iwl_priv *priv, int disable_radio)
Zhu Yib481de92007-09-25 17:54:57 -07002656{
2657 unsigned long flags;
2658
2659 if (!!disable_radio == test_bit(STATUS_RF_KILL_SW, &priv->status))
2660 return;
2661
2662 IWL_DEBUG_RF_KILL("Manual SW RF KILL set to: RADIO %s\n",
2663 disable_radio ? "OFF" : "ON");
2664
2665 if (disable_radio) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002666 iwl3945_scan_cancel(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002667 /* FIXME: This is a workaround for AP */
Johannes Berg05c914f2008-09-11 00:01:58 +02002668 if (priv->iw_mode != NL80211_IFTYPE_AP) {
Zhu Yib481de92007-09-25 17:54:57 -07002669 spin_lock_irqsave(&priv->lock, flags);
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08002670 iwl_write32(priv, CSR_UCODE_DRV_GP1_SET,
Zhu Yib481de92007-09-25 17:54:57 -07002671 CSR_UCODE_SW_BIT_RFKILL);
2672 spin_unlock_irqrestore(&priv->lock, flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002673 iwl3945_send_card_state(priv, CARD_STATE_CMD_DISABLE, 0);
Zhu Yib481de92007-09-25 17:54:57 -07002674 set_bit(STATUS_RF_KILL_SW, &priv->status);
2675 }
2676 return;
2677 }
2678
2679 spin_lock_irqsave(&priv->lock, flags);
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08002680 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
Zhu Yib481de92007-09-25 17:54:57 -07002681
2682 clear_bit(STATUS_RF_KILL_SW, &priv->status);
2683 spin_unlock_irqrestore(&priv->lock, flags);
2684
2685 /* wake up ucode */
2686 msleep(10);
2687
2688 spin_lock_irqsave(&priv->lock, flags);
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08002689 iwl_read32(priv, CSR_UCODE_DRV_GP1);
2690 if (!iwl_grab_nic_access(priv))
2691 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002692 spin_unlock_irqrestore(&priv->lock, flags);
2693
2694 if (test_bit(STATUS_RF_KILL_HW, &priv->status)) {
2695 IWL_DEBUG_RF_KILL("Can not turn radio back on - "
2696 "disabled by HW switch\n");
2697 return;
2698 }
2699
Zhu Yi808e72a2008-06-12 09:47:17 +08002700 if (priv->is_open)
2701 queue_work(priv->workqueue, &priv->restart);
Zhu Yib481de92007-09-25 17:54:57 -07002702 return;
2703}
2704
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002705void iwl3945_set_decrypted_flag(struct iwl_priv *priv, struct sk_buff *skb,
Zhu Yib481de92007-09-25 17:54:57 -07002706 u32 decrypt_res, struct ieee80211_rx_status *stats)
2707{
2708 u16 fc =
2709 le16_to_cpu(((struct ieee80211_hdr *)skb->data)->frame_control);
2710
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08002711 if (priv->active39_rxon.filter_flags & RXON_FILTER_DIS_DECRYPT_MSK)
Zhu Yib481de92007-09-25 17:54:57 -07002712 return;
2713
2714 if (!(fc & IEEE80211_FCTL_PROTECTED))
2715 return;
2716
2717 IWL_DEBUG_RX("decrypt_res:0x%x\n", decrypt_res);
2718 switch (decrypt_res & RX_RES_STATUS_SEC_TYPE_MSK) {
2719 case RX_RES_STATUS_SEC_TYPE_TKIP:
2720 if ((decrypt_res & RX_RES_STATUS_DECRYPT_TYPE_MSK) ==
2721 RX_RES_STATUS_BAD_ICV_MIC)
2722 stats->flag |= RX_FLAG_MMIC_ERROR;
2723 case RX_RES_STATUS_SEC_TYPE_WEP:
2724 case RX_RES_STATUS_SEC_TYPE_CCMP:
2725 if ((decrypt_res & RX_RES_STATUS_DECRYPT_TYPE_MSK) ==
2726 RX_RES_STATUS_DECRYPT_OK) {
2727 IWL_DEBUG_RX("hw decrypt successfully!!!\n");
2728 stats->flag |= RX_FLAG_DECRYPTED;
2729 }
2730 break;
2731
2732 default:
2733 break;
2734 }
2735}
2736
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08002737#ifdef CONFIG_IWL3945_SPECTRUM_MEASUREMENT
Zhu Yib481de92007-09-25 17:54:57 -07002738
2739#include "iwl-spectrum.h"
2740
2741#define BEACON_TIME_MASK_LOW 0x00FFFFFF
2742#define BEACON_TIME_MASK_HIGH 0xFF000000
2743#define TIME_UNIT 1024
2744
2745/*
2746 * extended beacon time format
2747 * time in usec will be changed into a 32-bit value in 8:24 format
2748 * the high 1 byte is the beacon counts
2749 * the lower 3 bytes is the time in usec within one beacon interval
2750 */
2751
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002752static u32 iwl3945_usecs_to_beacons(u32 usec, u32 beacon_interval)
Zhu Yib481de92007-09-25 17:54:57 -07002753{
2754 u32 quot;
2755 u32 rem;
2756 u32 interval = beacon_interval * 1024;
2757
2758 if (!interval || !usec)
2759 return 0;
2760
2761 quot = (usec / interval) & (BEACON_TIME_MASK_HIGH >> 24);
2762 rem = (usec % interval) & BEACON_TIME_MASK_LOW;
2763
2764 return (quot << 24) + rem;
2765}
2766
2767/* base is usually what we get from ucode with each received frame,
2768 * the same as HW timer counter counting down
2769 */
2770
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002771static __le32 iwl3945_add_beacon_time(u32 base, u32 addon, u32 beacon_interval)
Zhu Yib481de92007-09-25 17:54:57 -07002772{
2773 u32 base_low = base & BEACON_TIME_MASK_LOW;
2774 u32 addon_low = addon & BEACON_TIME_MASK_LOW;
2775 u32 interval = beacon_interval * TIME_UNIT;
2776 u32 res = (base & BEACON_TIME_MASK_HIGH) +
2777 (addon & BEACON_TIME_MASK_HIGH);
2778
2779 if (base_low > addon_low)
2780 res += base_low - addon_low;
2781 else if (base_low < addon_low) {
2782 res += interval + base_low - addon_low;
2783 res += (1 << 24);
2784 } else
2785 res += (1 << 24);
2786
2787 return cpu_to_le32(res);
2788}
2789
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002790static int iwl3945_get_measurement(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07002791 struct ieee80211_measurement_params *params,
2792 u8 type)
2793{
Tomas Winkler600c0e12008-12-19 10:37:04 +08002794 struct iwl_spectrum_cmd spectrum;
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08002795 struct iwl_rx_packet *res;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002796 struct iwl3945_host_cmd cmd = {
Zhu Yib481de92007-09-25 17:54:57 -07002797 .id = REPLY_SPECTRUM_MEASUREMENT_CMD,
2798 .data = (void *)&spectrum,
2799 .meta.flags = CMD_WANT_SKB,
2800 };
2801 u32 add_time = le64_to_cpu(params->start_time);
2802 int rc;
2803 int spectrum_resp_status;
2804 int duration = le16_to_cpu(params->duration);
2805
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002806 if (iwl3945_is_associated(priv))
Zhu Yib481de92007-09-25 17:54:57 -07002807 add_time =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002808 iwl3945_usecs_to_beacons(
Zhu Yib481de92007-09-25 17:54:57 -07002809 le64_to_cpu(params->start_time) - priv->last_tsf,
2810 le16_to_cpu(priv->rxon_timing.beacon_interval));
2811
2812 memset(&spectrum, 0, sizeof(spectrum));
2813
2814 spectrum.channel_count = cpu_to_le16(1);
2815 spectrum.flags =
2816 RXON_FLG_TSF2HOST_MSK | RXON_FLG_ANT_A_MSK | RXON_FLG_DIS_DIV_MSK;
2817 spectrum.filter_flags = MEASUREMENT_FILTER_FLAG;
2818 cmd.len = sizeof(spectrum);
2819 spectrum.len = cpu_to_le16(cmd.len - sizeof(spectrum.len));
2820
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002821 if (iwl3945_is_associated(priv))
Zhu Yib481de92007-09-25 17:54:57 -07002822 spectrum.start_time =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002823 iwl3945_add_beacon_time(priv->last_beacon_time,
Zhu Yib481de92007-09-25 17:54:57 -07002824 add_time,
2825 le16_to_cpu(priv->rxon_timing.beacon_interval));
2826 else
2827 spectrum.start_time = 0;
2828
2829 spectrum.channels[0].duration = cpu_to_le32(duration * TIME_UNIT);
2830 spectrum.channels[0].channel = params->channel;
2831 spectrum.channels[0].type = type;
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08002832 if (priv->active39_rxon.flags & RXON_FLG_BAND_24G_MSK)
Zhu Yib481de92007-09-25 17:54:57 -07002833 spectrum.flags |= RXON_FLG_BAND_24G_MSK |
2834 RXON_FLG_AUTO_DETECT_MSK | RXON_FLG_TGG_PROTECT_MSK;
2835
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002836 rc = iwl3945_send_cmd_sync(priv, &cmd);
Zhu Yib481de92007-09-25 17:54:57 -07002837 if (rc)
2838 return rc;
2839
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08002840 res = (struct iwl_rx_packet *)cmd.meta.u.skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07002841 if (res->hdr.flags & IWL_CMD_FAILED_MSK) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08002842 IWL_ERR(priv, "Bad return from REPLY_RX_ON_ASSOC command\n");
Zhu Yib481de92007-09-25 17:54:57 -07002843 rc = -EIO;
2844 }
2845
2846 spectrum_resp_status = le16_to_cpu(res->u.spectrum.status);
2847 switch (spectrum_resp_status) {
2848 case 0: /* Command will be handled */
2849 if (res->u.spectrum.id != 0xff) {
Ian Schrambc434dd2007-10-25 17:15:29 +08002850 IWL_DEBUG_INFO("Replaced existing measurement: %d\n",
2851 res->u.spectrum.id);
Zhu Yib481de92007-09-25 17:54:57 -07002852 priv->measurement_status &= ~MEASUREMENT_READY;
2853 }
2854 priv->measurement_status |= MEASUREMENT_ACTIVE;
2855 rc = 0;
2856 break;
2857
2858 case 1: /* Command will not be handled */
2859 rc = -EAGAIN;
2860 break;
2861 }
2862
2863 dev_kfree_skb_any(cmd.meta.u.skb);
2864
2865 return rc;
2866}
2867#endif
2868
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002869static void iwl3945_rx_reply_alive(struct iwl_priv *priv,
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08002870 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07002871{
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08002872 struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
2873 struct iwl_alive_resp *palive;
Zhu Yib481de92007-09-25 17:54:57 -07002874 struct delayed_work *pwork;
2875
2876 palive = &pkt->u.alive_frame;
2877
2878 IWL_DEBUG_INFO("Alive ucode status 0x%08X revision "
2879 "0x%01X 0x%01X\n",
2880 palive->is_valid, palive->ver_type,
2881 palive->ver_subtype);
2882
2883 if (palive->ver_subtype == INITIALIZE_SUBTYPE) {
2884 IWL_DEBUG_INFO("Initialization Alive received.\n");
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08002885 memcpy(&priv->card_alive_init, &pkt->u.alive_frame,
2886 sizeof(struct iwl_alive_resp));
Zhu Yib481de92007-09-25 17:54:57 -07002887 pwork = &priv->init_alive_start;
2888 } else {
2889 IWL_DEBUG_INFO("Runtime Alive received.\n");
2890 memcpy(&priv->card_alive, &pkt->u.alive_frame,
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08002891 sizeof(struct iwl_alive_resp));
Zhu Yib481de92007-09-25 17:54:57 -07002892 pwork = &priv->alive_start;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002893 iwl3945_disable_events(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002894 }
2895
2896 /* We delay the ALIVE response by 5ms to
2897 * give the HW RF Kill time to activate... */
2898 if (palive->is_valid == UCODE_VALID_OK)
2899 queue_delayed_work(priv->workqueue, pwork,
2900 msecs_to_jiffies(5));
2901 else
Winkler, Tomas39aadf82008-12-19 10:37:32 +08002902 IWL_WARN(priv, "uCode did not respond OK.\n");
Zhu Yib481de92007-09-25 17:54:57 -07002903}
2904
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002905static void iwl3945_rx_reply_add_sta(struct iwl_priv *priv,
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08002906 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07002907{
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08002908 struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07002909
2910 IWL_DEBUG_RX("Received REPLY_ADD_STA: 0x%02X\n", pkt->u.status);
2911 return;
2912}
2913
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002914static void iwl3945_rx_reply_error(struct iwl_priv *priv,
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08002915 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07002916{
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08002917 struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07002918
Winkler, Tomas15b16872008-12-19 10:37:33 +08002919 IWL_ERR(priv, "Error Reply type 0x%08X cmd %s (0x%02X) "
Zhu Yib481de92007-09-25 17:54:57 -07002920 "seq 0x%04X ser 0x%08X\n",
2921 le32_to_cpu(pkt->u.err_resp.error_type),
2922 get_cmd_string(pkt->u.err_resp.cmd_id),
2923 pkt->u.err_resp.cmd_id,
2924 le16_to_cpu(pkt->u.err_resp.bad_cmd_seq_num),
2925 le32_to_cpu(pkt->u.err_resp.error_info));
2926}
2927
2928#define TX_STATUS_ENTRY(x) case TX_STATUS_FAIL_ ## x: return #x
2929
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002930static void iwl3945_rx_csa(struct iwl_priv *priv, struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07002931{
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08002932 struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08002933 struct iwl3945_rxon_cmd *rxon = (void *)&priv->active39_rxon;
Tomas Winkler600c0e12008-12-19 10:37:04 +08002934 struct iwl_csa_notification *csa = &(pkt->u.csa_notif);
Zhu Yib481de92007-09-25 17:54:57 -07002935 IWL_DEBUG_11H("CSA notif: channel %d, status %d\n",
2936 le16_to_cpu(csa->channel), le32_to_cpu(csa->status));
2937 rxon->channel = csa->channel;
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08002938 priv->staging39_rxon.channel = csa->channel;
Zhu Yib481de92007-09-25 17:54:57 -07002939}
2940
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002941static void iwl3945_rx_spectrum_measure_notif(struct iwl_priv *priv,
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08002942 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07002943{
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08002944#ifdef CONFIG_IWL3945_SPECTRUM_MEASUREMENT
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08002945 struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
Tomas Winkler600c0e12008-12-19 10:37:04 +08002946 struct iwl_spectrum_notification *report = &(pkt->u.spectrum_notif);
Zhu Yib481de92007-09-25 17:54:57 -07002947
2948 if (!report->state) {
2949 IWL_DEBUG(IWL_DL_11H | IWL_DL_INFO,
2950 "Spectrum Measure Notification: Start\n");
2951 return;
2952 }
2953
2954 memcpy(&priv->measure_report, report, sizeof(*report));
2955 priv->measurement_status |= MEASUREMENT_READY;
2956#endif
2957}
2958
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002959static void iwl3945_rx_pm_sleep_notif(struct iwl_priv *priv,
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08002960 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07002961{
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08002962#ifdef CONFIG_IWL3945_DEBUG
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08002963 struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
Tomas Winkler600c0e12008-12-19 10:37:04 +08002964 struct iwl_sleep_notification *sleep = &(pkt->u.sleep_notif);
Zhu Yib481de92007-09-25 17:54:57 -07002965 IWL_DEBUG_RX("sleep mode: %d, src: %d\n",
2966 sleep->pm_sleep_mode, sleep->pm_wakeup_src);
2967#endif
2968}
2969
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002970static void iwl3945_rx_pm_debug_statistics_notif(struct iwl_priv *priv,
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08002971 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07002972{
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08002973 struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07002974 IWL_DEBUG_RADIO("Dumping %d bytes of unhandled "
2975 "notification for %s:\n",
2976 le32_to_cpu(pkt->len), get_cmd_string(pkt->hdr.cmd));
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08002977 iwl_print_hex_dump(priv, IWL_DL_RADIO, pkt->u.raw,
2978 le32_to_cpu(pkt->len));
Zhu Yib481de92007-09-25 17:54:57 -07002979}
2980
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002981static void iwl3945_bg_beacon_update(struct work_struct *work)
Zhu Yib481de92007-09-25 17:54:57 -07002982{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002983 struct iwl_priv *priv =
2984 container_of(work, struct iwl_priv, beacon_update);
Zhu Yib481de92007-09-25 17:54:57 -07002985 struct sk_buff *beacon;
2986
2987 /* Pull updated AP beacon from mac80211. will fail if not in AP mode */
Johannes Berge039fa42008-05-15 12:55:29 +02002988 beacon = ieee80211_beacon_get(priv->hw, priv->vif);
Zhu Yib481de92007-09-25 17:54:57 -07002989
2990 if (!beacon) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08002991 IWL_ERR(priv, "update beacon failed\n");
Zhu Yib481de92007-09-25 17:54:57 -07002992 return;
2993 }
2994
2995 mutex_lock(&priv->mutex);
2996 /* new beacon skb is allocated every time; dispose previous.*/
2997 if (priv->ibss_beacon)
2998 dev_kfree_skb(priv->ibss_beacon);
2999
3000 priv->ibss_beacon = beacon;
3001 mutex_unlock(&priv->mutex);
3002
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003003 iwl3945_send_beacon_cmd(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003004}
3005
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003006static void iwl3945_rx_beacon_notif(struct iwl_priv *priv,
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08003007 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003008{
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08003009#ifdef CONFIG_IWL3945_DEBUG
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08003010 struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003011 struct iwl3945_beacon_notif *beacon = &(pkt->u.beacon_status);
Zhu Yib481de92007-09-25 17:54:57 -07003012 u8 rate = beacon->beacon_notify_hdr.rate;
3013
3014 IWL_DEBUG_RX("beacon status %x retries %d iss %d "
3015 "tsf %d %d rate %d\n",
3016 le32_to_cpu(beacon->beacon_notify_hdr.status) & TX_STATUS_MSK,
3017 beacon->beacon_notify_hdr.failure_frame,
3018 le32_to_cpu(beacon->ibss_mgr_status),
3019 le32_to_cpu(beacon->high_tsf),
3020 le32_to_cpu(beacon->low_tsf), rate);
3021#endif
3022
Johannes Berg05c914f2008-09-11 00:01:58 +02003023 if ((priv->iw_mode == NL80211_IFTYPE_AP) &&
Zhu Yib481de92007-09-25 17:54:57 -07003024 (!test_bit(STATUS_EXIT_PENDING, &priv->status)))
3025 queue_work(priv->workqueue, &priv->beacon_update);
3026}
3027
3028/* Service response to REPLY_SCAN_CMD (0x80) */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003029static void iwl3945_rx_reply_scan(struct iwl_priv *priv,
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08003030 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003031{
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08003032#ifdef CONFIG_IWL3945_DEBUG
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08003033 struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
Tomas Winkler4c897252008-12-19 10:37:05 +08003034 struct iwl_scanreq_notification *notif =
3035 (struct iwl_scanreq_notification *)pkt->u.raw;
Zhu Yib481de92007-09-25 17:54:57 -07003036
3037 IWL_DEBUG_RX("Scan request status = 0x%x\n", notif->status);
3038#endif
3039}
3040
3041/* Service SCAN_START_NOTIFICATION (0x82) */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003042static void iwl3945_rx_scan_start_notif(struct iwl_priv *priv,
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08003043 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003044{
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08003045 struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
Tomas Winkler4c897252008-12-19 10:37:05 +08003046 struct iwl_scanstart_notification *notif =
3047 (struct iwl_scanstart_notification *)pkt->u.raw;
Zhu Yib481de92007-09-25 17:54:57 -07003048 priv->scan_start_tsf = le32_to_cpu(notif->tsf_low);
3049 IWL_DEBUG_SCAN("Scan start: "
3050 "%d [802.11%s] "
3051 "(TSF: 0x%08X:%08X) - %d (beacon timer %u)\n",
3052 notif->channel,
3053 notif->band ? "bg" : "a",
3054 notif->tsf_high,
3055 notif->tsf_low, notif->status, notif->beacon_timer);
3056}
3057
3058/* Service SCAN_RESULTS_NOTIFICATION (0x83) */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003059static void iwl3945_rx_scan_results_notif(struct iwl_priv *priv,
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08003060 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003061{
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08003062 struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
Tomas Winkler4c897252008-12-19 10:37:05 +08003063 struct iwl_scanresults_notification *notif =
3064 (struct iwl_scanresults_notification *)pkt->u.raw;
Zhu Yib481de92007-09-25 17:54:57 -07003065
3066 IWL_DEBUG_SCAN("Scan ch.res: "
3067 "%d [802.11%s] "
3068 "(TSF: 0x%08X:%08X) - %d "
3069 "elapsed=%lu usec (%dms since last)\n",
3070 notif->channel,
3071 notif->band ? "bg" : "a",
3072 le32_to_cpu(notif->tsf_high),
3073 le32_to_cpu(notif->tsf_low),
3074 le32_to_cpu(notif->statistics[0]),
3075 le32_to_cpu(notif->tsf_low) - priv->scan_start_tsf,
3076 jiffies_to_msecs(elapsed_jiffies
3077 (priv->last_scan_jiffies, jiffies)));
3078
3079 priv->last_scan_jiffies = jiffies;
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08003080 priv->next_scan_jiffies = 0;
Zhu Yib481de92007-09-25 17:54:57 -07003081}
3082
3083/* Service SCAN_COMPLETE_NOTIFICATION (0x84) */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003084static void iwl3945_rx_scan_complete_notif(struct iwl_priv *priv,
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08003085 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003086{
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08003087 struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
Tomas Winkler4c897252008-12-19 10:37:05 +08003088 struct iwl_scancomplete_notification *scan_notif = (void *)pkt->u.raw;
Zhu Yib481de92007-09-25 17:54:57 -07003089
3090 IWL_DEBUG_SCAN("Scan complete: %d channels (TSF 0x%08X:%08X) - %d\n",
3091 scan_notif->scanned_channels,
3092 scan_notif->tsf_low,
3093 scan_notif->tsf_high, scan_notif->status);
3094
3095 /* The HW is no longer scanning */
3096 clear_bit(STATUS_SCAN_HW, &priv->status);
3097
3098 /* The scan completion notification came in, so kill that timer... */
3099 cancel_delayed_work(&priv->scan_check);
3100
3101 IWL_DEBUG_INFO("Scan pass on %sGHz took %dms\n",
Ron Rindjunsky66b50042008-06-25 16:46:31 +08003102 (priv->scan_bands & BIT(IEEE80211_BAND_2GHZ)) ?
3103 "2.4" : "5.2",
Zhu Yib481de92007-09-25 17:54:57 -07003104 jiffies_to_msecs(elapsed_jiffies
3105 (priv->scan_pass_start, jiffies)));
3106
Ron Rindjunsky66b50042008-06-25 16:46:31 +08003107 /* Remove this scanned band from the list of pending
3108 * bands to scan, band G precedes A in order of scanning
3109 * as seen in iwl3945_bg_request_scan */
3110 if (priv->scan_bands & BIT(IEEE80211_BAND_2GHZ))
3111 priv->scan_bands &= ~BIT(IEEE80211_BAND_2GHZ);
3112 else if (priv->scan_bands & BIT(IEEE80211_BAND_5GHZ))
3113 priv->scan_bands &= ~BIT(IEEE80211_BAND_5GHZ);
Zhu Yib481de92007-09-25 17:54:57 -07003114
3115 /* If a request to abort was given, or the scan did not succeed
3116 * then we reset the scan state machine and terminate,
3117 * re-queuing another scan if one has been requested */
3118 if (test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
3119 IWL_DEBUG_INFO("Aborted scan completed.\n");
3120 clear_bit(STATUS_SCAN_ABORTING, &priv->status);
3121 } else {
3122 /* If there are more bands on this scan pass reschedule */
3123 if (priv->scan_bands > 0)
3124 goto reschedule;
3125 }
3126
3127 priv->last_scan_jiffies = jiffies;
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08003128 priv->next_scan_jiffies = 0;
Zhu Yib481de92007-09-25 17:54:57 -07003129 IWL_DEBUG_INFO("Setting scan to off\n");
3130
3131 clear_bit(STATUS_SCANNING, &priv->status);
3132
3133 IWL_DEBUG_INFO("Scan took %dms\n",
3134 jiffies_to_msecs(elapsed_jiffies(priv->scan_start, jiffies)));
3135
3136 queue_work(priv->workqueue, &priv->scan_completed);
3137
3138 return;
3139
3140reschedule:
3141 priv->scan_pass_start = jiffies;
3142 queue_work(priv->workqueue, &priv->request_scan);
3143}
3144
3145/* Handle notification from uCode that card's power state is changing
3146 * due to software, hardware, or critical temperature RFKILL */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003147static void iwl3945_rx_card_state_notif(struct iwl_priv *priv,
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08003148 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003149{
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08003150 struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07003151 u32 flags = le32_to_cpu(pkt->u.card_state_notif.flags);
3152 unsigned long status = priv->status;
3153
3154 IWL_DEBUG_RF_KILL("Card state received: HW:%s SW:%s\n",
3155 (flags & HW_CARD_DISABLED) ? "Kill" : "On",
3156 (flags & SW_CARD_DISABLED) ? "Kill" : "On");
3157
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003158 iwl_write32(priv, CSR_UCODE_DRV_GP1_SET,
Zhu Yib481de92007-09-25 17:54:57 -07003159 CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
3160
3161 if (flags & HW_CARD_DISABLED)
3162 set_bit(STATUS_RF_KILL_HW, &priv->status);
3163 else
3164 clear_bit(STATUS_RF_KILL_HW, &priv->status);
3165
3166
3167 if (flags & SW_CARD_DISABLED)
3168 set_bit(STATUS_RF_KILL_SW, &priv->status);
3169 else
3170 clear_bit(STATUS_RF_KILL_SW, &priv->status);
3171
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003172 iwl3945_scan_cancel(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003173
3174 if ((test_bit(STATUS_RF_KILL_HW, &status) !=
3175 test_bit(STATUS_RF_KILL_HW, &priv->status)) ||
3176 (test_bit(STATUS_RF_KILL_SW, &status) !=
3177 test_bit(STATUS_RF_KILL_SW, &priv->status)))
3178 queue_work(priv->workqueue, &priv->rf_kill);
3179 else
3180 wake_up_interruptible(&priv->wait_command_queue);
3181}
3182
3183/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003184 * iwl3945_setup_rx_handlers - Initialize Rx handler callbacks
Zhu Yib481de92007-09-25 17:54:57 -07003185 *
3186 * Setup the RX handlers for each of the reply types sent from the uCode
3187 * to the host.
3188 *
3189 * This function chains into the hardware specific files for them to setup
3190 * any hardware specific handlers as well.
3191 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003192static void iwl3945_setup_rx_handlers(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003193{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003194 priv->rx_handlers[REPLY_ALIVE] = iwl3945_rx_reply_alive;
3195 priv->rx_handlers[REPLY_ADD_STA] = iwl3945_rx_reply_add_sta;
3196 priv->rx_handlers[REPLY_ERROR] = iwl3945_rx_reply_error;
3197 priv->rx_handlers[CHANNEL_SWITCH_NOTIFICATION] = iwl3945_rx_csa;
Zhu Yib481de92007-09-25 17:54:57 -07003198 priv->rx_handlers[SPECTRUM_MEASURE_NOTIFICATION] =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003199 iwl3945_rx_spectrum_measure_notif;
3200 priv->rx_handlers[PM_SLEEP_NOTIFICATION] = iwl3945_rx_pm_sleep_notif;
Zhu Yib481de92007-09-25 17:54:57 -07003201 priv->rx_handlers[PM_DEBUG_STATISTIC_NOTIFIC] =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003202 iwl3945_rx_pm_debug_statistics_notif;
3203 priv->rx_handlers[BEACON_NOTIFICATION] = iwl3945_rx_beacon_notif;
Zhu Yib481de92007-09-25 17:54:57 -07003204
Ben Cahill9fbab512007-11-29 11:09:47 +08003205 /*
3206 * The same handler is used for both the REPLY to a discrete
3207 * statistics request from the host as well as for the periodic
3208 * statistics notifications (after received beacons) from the uCode.
Zhu Yib481de92007-09-25 17:54:57 -07003209 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003210 priv->rx_handlers[REPLY_STATISTICS_CMD] = iwl3945_hw_rx_statistics;
3211 priv->rx_handlers[STATISTICS_NOTIFICATION] = iwl3945_hw_rx_statistics;
Zhu Yib481de92007-09-25 17:54:57 -07003212
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003213 priv->rx_handlers[REPLY_SCAN_CMD] = iwl3945_rx_reply_scan;
3214 priv->rx_handlers[SCAN_START_NOTIFICATION] = iwl3945_rx_scan_start_notif;
Zhu Yib481de92007-09-25 17:54:57 -07003215 priv->rx_handlers[SCAN_RESULTS_NOTIFICATION] =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003216 iwl3945_rx_scan_results_notif;
Zhu Yib481de92007-09-25 17:54:57 -07003217 priv->rx_handlers[SCAN_COMPLETE_NOTIFICATION] =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003218 iwl3945_rx_scan_complete_notif;
3219 priv->rx_handlers[CARD_STATE_NOTIFICATION] = iwl3945_rx_card_state_notif;
Zhu Yib481de92007-09-25 17:54:57 -07003220
Ben Cahill9fbab512007-11-29 11:09:47 +08003221 /* Set up hardware specific Rx handlers */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003222 iwl3945_hw_rx_handler_setup(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003223}
3224
3225/**
Tomas Winkler91c066f2008-03-06 17:36:55 -08003226 * iwl3945_cmd_queue_reclaim - Reclaim CMD queue entries
3227 * When FW advances 'R' index, all entries between old and new 'R' index
3228 * need to be reclaimed.
3229 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003230static void iwl3945_cmd_queue_reclaim(struct iwl_priv *priv,
Tomas Winkler91c066f2008-03-06 17:36:55 -08003231 int txq_id, int index)
3232{
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08003233 struct iwl3945_tx_queue *txq = &priv->txq39[txq_id];
Samuel Ortizd20b3c62008-12-19 10:37:15 +08003234 struct iwl_queue *q = &txq->q;
Tomas Winkler91c066f2008-03-06 17:36:55 -08003235 int nfreed = 0;
3236
3237 if ((index >= q->n_bd) || (iwl3945_x2_queue_used(q, index) == 0)) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08003238 IWL_ERR(priv, "Read index for DMA queue txq id (%d), index %d, "
Tomas Winkler91c066f2008-03-06 17:36:55 -08003239 "is out of range [0-%d] %d %d.\n", txq_id,
3240 index, q->n_bd, q->write_ptr, q->read_ptr);
3241 return;
3242 }
3243
3244 for (index = iwl_queue_inc_wrap(index, q->n_bd); q->read_ptr != index;
3245 q->read_ptr = iwl_queue_inc_wrap(q->read_ptr, q->n_bd)) {
3246 if (nfreed > 1) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08003247 IWL_ERR(priv, "HCMD skipped: index (%d) %d %d\n", index,
Tomas Winkler91c066f2008-03-06 17:36:55 -08003248 q->write_ptr, q->read_ptr);
3249 queue_work(priv->workqueue, &priv->restart);
3250 break;
3251 }
3252 nfreed++;
3253 }
3254}
3255
3256
3257/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003258 * iwl3945_tx_cmd_complete - Pull unused buffers off the queue and reclaim them
Zhu Yib481de92007-09-25 17:54:57 -07003259 * @rxb: Rx buffer to reclaim
3260 *
3261 * If an Rx buffer has an async callback associated with it the callback
3262 * will be executed. The attached skb (if present) will only be freed
3263 * if the callback returns 1
3264 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003265static void iwl3945_tx_cmd_complete(struct iwl_priv *priv,
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08003266 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003267{
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08003268 struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07003269 u16 sequence = le16_to_cpu(pkt->hdr.sequence);
3270 int txq_id = SEQ_TO_QUEUE(sequence);
3271 int index = SEQ_TO_INDEX(sequence);
Tomas Winkler600c0e12008-12-19 10:37:04 +08003272 int huge = !!(pkt->hdr.sequence & SEQ_HUGE_FRAME);
Zhu Yib481de92007-09-25 17:54:57 -07003273 int cmd_index;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003274 struct iwl3945_cmd *cmd;
Zhu Yib481de92007-09-25 17:54:57 -07003275
Zhu Yib481de92007-09-25 17:54:57 -07003276 BUG_ON(txq_id != IWL_CMD_QUEUE_NUM);
3277
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08003278 cmd_index = get_cmd_index(&priv->txq39[IWL_CMD_QUEUE_NUM].q, index, huge);
3279 cmd = &priv->txq39[IWL_CMD_QUEUE_NUM].cmd[cmd_index];
Zhu Yib481de92007-09-25 17:54:57 -07003280
3281 /* Input error checking is done when commands are added to queue. */
3282 if (cmd->meta.flags & CMD_WANT_SKB) {
3283 cmd->meta.source->u.skb = rxb->skb;
3284 rxb->skb = NULL;
3285 } else if (cmd->meta.u.callback &&
3286 !cmd->meta.u.callback(priv, cmd, rxb->skb))
3287 rxb->skb = NULL;
3288
Tomas Winkler91c066f2008-03-06 17:36:55 -08003289 iwl3945_cmd_queue_reclaim(priv, txq_id, index);
Zhu Yib481de92007-09-25 17:54:57 -07003290
3291 if (!(cmd->meta.flags & CMD_ASYNC)) {
3292 clear_bit(STATUS_HCMD_ACTIVE, &priv->status);
3293 wake_up_interruptible(&priv->wait_command_queue);
3294 }
3295}
3296
3297/************************** RX-FUNCTIONS ****************************/
3298/*
3299 * Rx theory of operation
3300 *
3301 * The host allocates 32 DMA target addresses and passes the host address
3302 * to the firmware at register IWL_RFDS_TABLE_LOWER + N * RFD_SIZE where N is
3303 * 0 to 31
3304 *
3305 * Rx Queue Indexes
3306 * The host/firmware share two index registers for managing the Rx buffers.
3307 *
3308 * The READ index maps to the first position that the firmware may be writing
3309 * to -- the driver can read up to (but not including) this position and get
3310 * good data.
3311 * The READ index is managed by the firmware once the card is enabled.
3312 *
3313 * The WRITE index maps to the last position the driver has read from -- the
3314 * position preceding WRITE is the last slot the firmware can place a packet.
3315 *
3316 * The queue is empty (no good data) if WRITE = READ - 1, and is full if
3317 * WRITE = READ.
3318 *
Ben Cahill9fbab512007-11-29 11:09:47 +08003319 * During initialization, the host sets up the READ queue position to the first
Zhu Yib481de92007-09-25 17:54:57 -07003320 * INDEX position, and WRITE to the last (READ - 1 wrapped)
3321 *
Ben Cahill9fbab512007-11-29 11:09:47 +08003322 * When the firmware places a packet in a buffer, it will advance the READ index
Zhu Yib481de92007-09-25 17:54:57 -07003323 * and fire the RX interrupt. The driver can then query the READ index and
3324 * process as many packets as possible, moving the WRITE index forward as it
3325 * resets the Rx queue buffers with new memory.
3326 *
3327 * The management in the driver is as follows:
3328 * + A list of pre-allocated SKBs is stored in iwl->rxq->rx_free. When
3329 * iwl->rxq->free_count drops to or below RX_LOW_WATERMARK, work is scheduled
Ian Schram01ebd062007-10-25 17:15:22 +08003330 * to replenish the iwl->rxq->rx_free.
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003331 * + In iwl3945_rx_replenish (scheduled) if 'processed' != 'read' then the
Zhu Yib481de92007-09-25 17:54:57 -07003332 * iwl->rxq is replenished and the READ INDEX is updated (updating the
3333 * 'processed' and 'read' driver indexes as well)
3334 * + A received packet is processed and handed to the kernel network stack,
3335 * detached from the iwl->rxq. The driver 'processed' index is updated.
3336 * + The Host/Firmware iwl->rxq is replenished at tasklet time from the rx_free
3337 * list. If there are no allocated buffers in iwl->rxq->rx_free, the READ
3338 * INDEX is not incremented and iwl->status(RX_STALLED) is set. If there
3339 * were enough free buffers and RX_STALLED is set it is cleared.
3340 *
3341 *
3342 * Driver sequence:
3343 *
Ben Cahill9fbab512007-11-29 11:09:47 +08003344 * iwl3945_rx_queue_alloc() Allocates rx_free
3345 * iwl3945_rx_replenish() Replenishes rx_free list from rx_used, and calls
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003346 * iwl3945_rx_queue_restock
Ben Cahill9fbab512007-11-29 11:09:47 +08003347 * iwl3945_rx_queue_restock() Moves available buffers from rx_free into Rx
Zhu Yib481de92007-09-25 17:54:57 -07003348 * queue, updates firmware pointers, and updates
3349 * the WRITE index. If insufficient rx_free buffers
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003350 * are available, schedules iwl3945_rx_replenish
Zhu Yib481de92007-09-25 17:54:57 -07003351 *
3352 * -- enable interrupts --
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08003353 * ISR - iwl3945_rx() Detach iwl_rx_mem_buffers from pool up to the
Zhu Yib481de92007-09-25 17:54:57 -07003354 * READ INDEX, detaching the SKB from the pool.
3355 * Moves the packet buffer from queue to rx_used.
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003356 * Calls iwl3945_rx_queue_restock to refill any empty
Zhu Yib481de92007-09-25 17:54:57 -07003357 * slots.
3358 * ...
3359 *
3360 */
3361
3362/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003363 * iwl3945_rx_queue_space - Return number of free slots available in queue.
Zhu Yib481de92007-09-25 17:54:57 -07003364 */
Abhijeet Kolekarcc2f3622008-12-19 10:37:25 +08003365static int iwl3945_rx_queue_space(const struct iwl_rx_queue *q)
Zhu Yib481de92007-09-25 17:54:57 -07003366{
3367 int s = q->read - q->write;
3368 if (s <= 0)
3369 s += RX_QUEUE_SIZE;
3370 /* keep some buffer to not confuse full and empty queue */
3371 s -= 2;
3372 if (s < 0)
3373 s = 0;
3374 return s;
3375}
3376
3377/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003378 * iwl3945_rx_queue_update_write_ptr - Update the write pointer for the RX queue
Zhu Yib481de92007-09-25 17:54:57 -07003379 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003380int iwl3945_rx_queue_update_write_ptr(struct iwl_priv *priv, struct iwl_rx_queue *q)
Zhu Yib481de92007-09-25 17:54:57 -07003381{
3382 u32 reg = 0;
3383 int rc = 0;
3384 unsigned long flags;
3385
3386 spin_lock_irqsave(&q->lock, flags);
3387
3388 if (q->need_update == 0)
3389 goto exit_unlock;
3390
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003391 /* If power-saving is in use, make sure device is awake */
Zhu Yib481de92007-09-25 17:54:57 -07003392 if (test_bit(STATUS_POWER_PMI, &priv->status)) {
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003393 reg = iwl_read32(priv, CSR_UCODE_DRV_GP1);
Zhu Yib481de92007-09-25 17:54:57 -07003394
3395 if (reg & CSR_UCODE_DRV_GP1_BIT_MAC_SLEEP) {
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003396 iwl_set_bit(priv, CSR_GP_CNTRL,
Zhu Yib481de92007-09-25 17:54:57 -07003397 CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
3398 goto exit_unlock;
3399 }
3400
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003401 rc = iwl_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003402 if (rc)
3403 goto exit_unlock;
3404
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003405 /* Device expects a multiple of 8 */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003406 iwl_write_direct32(priv, FH39_RSCSR_CHNL0_WPTR,
Zhu Yib481de92007-09-25 17:54:57 -07003407 q->write & ~0x7);
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003408 iwl_release_nic_access(priv);
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003409
3410 /* Else device is assumed to be awake */
Zhu Yib481de92007-09-25 17:54:57 -07003411 } else
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003412 /* Device expects a multiple of 8 */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003413 iwl_write32(priv, FH39_RSCSR_CHNL0_WPTR, q->write & ~0x7);
Zhu Yib481de92007-09-25 17:54:57 -07003414
3415
3416 q->need_update = 0;
3417
3418 exit_unlock:
3419 spin_unlock_irqrestore(&q->lock, flags);
3420 return rc;
3421}
3422
3423/**
Ben Cahill9fbab512007-11-29 11:09:47 +08003424 * iwl3945_dma_addr2rbd_ptr - convert a DMA address to a uCode read buffer ptr
Zhu Yib481de92007-09-25 17:54:57 -07003425 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003426static inline __le32 iwl3945_dma_addr2rbd_ptr(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07003427 dma_addr_t dma_addr)
3428{
3429 return cpu_to_le32((u32)dma_addr);
3430}
3431
3432/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003433 * iwl3945_rx_queue_restock - refill RX queue from pre-allocated pool
Zhu Yib481de92007-09-25 17:54:57 -07003434 *
Ben Cahill9fbab512007-11-29 11:09:47 +08003435 * If there are slots in the RX queue that need to be restocked,
Zhu Yib481de92007-09-25 17:54:57 -07003436 * and we have free pre-allocated buffers, fill the ranks as much
Ben Cahill9fbab512007-11-29 11:09:47 +08003437 * as we can, pulling from rx_free.
Zhu Yib481de92007-09-25 17:54:57 -07003438 *
3439 * This moves the 'write' index forward to catch up with 'processed', and
3440 * also updates the memory address in the firmware to reference the new
3441 * target buffer.
3442 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003443static int iwl3945_rx_queue_restock(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003444{
Abhijeet Kolekarcc2f3622008-12-19 10:37:25 +08003445 struct iwl_rx_queue *rxq = &priv->rxq;
Zhu Yib481de92007-09-25 17:54:57 -07003446 struct list_head *element;
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08003447 struct iwl_rx_mem_buffer *rxb;
Zhu Yib481de92007-09-25 17:54:57 -07003448 unsigned long flags;
3449 int write, rc;
3450
3451 spin_lock_irqsave(&rxq->lock, flags);
3452 write = rxq->write & ~0x7;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003453 while ((iwl3945_rx_queue_space(rxq) > 0) && (rxq->free_count)) {
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003454 /* Get next free Rx buffer, remove from free list */
Zhu Yib481de92007-09-25 17:54:57 -07003455 element = rxq->rx_free.next;
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08003456 rxb = list_entry(element, struct iwl_rx_mem_buffer, list);
Zhu Yib481de92007-09-25 17:54:57 -07003457 list_del(element);
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003458
3459 /* Point to Rx buffer via next RBD in circular buffer */
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08003460 rxq->bd[rxq->write] = iwl3945_dma_addr2rbd_ptr(priv, rxb->real_dma_addr);
Zhu Yib481de92007-09-25 17:54:57 -07003461 rxq->queue[rxq->write] = rxb;
3462 rxq->write = (rxq->write + 1) & RX_QUEUE_MASK;
3463 rxq->free_count--;
3464 }
3465 spin_unlock_irqrestore(&rxq->lock, flags);
3466 /* If the pre-allocated buffer pool is dropping low, schedule to
3467 * refill it */
3468 if (rxq->free_count <= RX_LOW_WATERMARK)
3469 queue_work(priv->workqueue, &priv->rx_replenish);
3470
3471
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003472 /* If we've added more space for the firmware to place data, tell it.
3473 * Increment device's write pointer in multiples of 8. */
Zhu Yib481de92007-09-25 17:54:57 -07003474 if ((write != (rxq->write & ~0x7))
3475 || (abs(rxq->write - rxq->read) > 7)) {
3476 spin_lock_irqsave(&rxq->lock, flags);
3477 rxq->need_update = 1;
3478 spin_unlock_irqrestore(&rxq->lock, flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003479 rc = iwl3945_rx_queue_update_write_ptr(priv, rxq);
Zhu Yib481de92007-09-25 17:54:57 -07003480 if (rc)
3481 return rc;
3482 }
3483
3484 return 0;
3485}
3486
3487/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003488 * iwl3945_rx_replenish - Move all used packet from rx_used to rx_free
Zhu Yib481de92007-09-25 17:54:57 -07003489 *
3490 * When moving to rx_free an SKB is allocated for the slot.
3491 *
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003492 * Also restock the Rx queue via iwl3945_rx_queue_restock.
Ian Schram01ebd062007-10-25 17:15:22 +08003493 * This is called as a scheduled work item (except for during initialization)
Zhu Yib481de92007-09-25 17:54:57 -07003494 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003495static void iwl3945_rx_allocate(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003496{
Abhijeet Kolekarcc2f3622008-12-19 10:37:25 +08003497 struct iwl_rx_queue *rxq = &priv->rxq;
Zhu Yib481de92007-09-25 17:54:57 -07003498 struct list_head *element;
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08003499 struct iwl_rx_mem_buffer *rxb;
Zhu Yib481de92007-09-25 17:54:57 -07003500 unsigned long flags;
3501 spin_lock_irqsave(&rxq->lock, flags);
3502 while (!list_empty(&rxq->rx_used)) {
3503 element = rxq->rx_used.next;
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08003504 rxb = list_entry(element, struct iwl_rx_mem_buffer, list);
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003505
3506 /* Alloc a new receive buffer */
Zhu Yib481de92007-09-25 17:54:57 -07003507 rxb->skb =
3508 alloc_skb(IWL_RX_BUF_SIZE, __GFP_NOWARN | GFP_ATOMIC);
3509 if (!rxb->skb) {
3510 if (net_ratelimit())
Tomas Winkler978785a2008-12-19 10:37:31 +08003511 IWL_CRIT(priv, ": Can not allocate SKB buffers\n");
Zhu Yib481de92007-09-25 17:54:57 -07003512 /* We don't reschedule replenish work here -- we will
3513 * call the restock method and if it still needs
3514 * more buffers it will schedule replenish */
3515 break;
3516 }
Zhu Yi12342c42007-12-20 11:27:32 +08003517
3518 /* If radiotap head is required, reserve some headroom here.
3519 * The physical head count is a variable rx_stats->phy_count.
3520 * We reserve 4 bytes here. Plus these extra bytes, the
3521 * headroom of the physical head should be enough for the
3522 * radiotap head that iwl3945 supported. See iwl3945_rt.
3523 */
3524 skb_reserve(rxb->skb, 4);
3525
Zhu Yib481de92007-09-25 17:54:57 -07003526 priv->alloc_rxb_skb++;
3527 list_del(element);
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003528
3529 /* Get physical address of RB/SKB */
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08003530 rxb->real_dma_addr =
Zhu Yib481de92007-09-25 17:54:57 -07003531 pci_map_single(priv->pci_dev, rxb->skb->data,
3532 IWL_RX_BUF_SIZE, PCI_DMA_FROMDEVICE);
3533 list_add_tail(&rxb->list, &rxq->rx_free);
3534 rxq->free_count++;
3535 }
3536 spin_unlock_irqrestore(&rxq->lock, flags);
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08003537}
3538
3539/*
3540 * this should be called while priv->lock is locked
3541 */
Tomas Winkler4fd1f842007-12-05 20:59:58 +02003542static void __iwl3945_rx_replenish(void *data)
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08003543{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003544 struct iwl_priv *priv = data;
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08003545
3546 iwl3945_rx_allocate(priv);
3547 iwl3945_rx_queue_restock(priv);
3548}
3549
3550
3551void iwl3945_rx_replenish(void *data)
3552{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003553 struct iwl_priv *priv = data;
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08003554 unsigned long flags;
3555
3556 iwl3945_rx_allocate(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003557
3558 spin_lock_irqsave(&priv->lock, flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003559 iwl3945_rx_queue_restock(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003560 spin_unlock_irqrestore(&priv->lock, flags);
3561}
3562
3563/* Assumes that the skb field of the buffers in 'pool' is kept accurate.
Ben Cahill9fbab512007-11-29 11:09:47 +08003564 * If an SKB has been detached, the POOL needs to have its SKB set to NULL
Zhu Yib481de92007-09-25 17:54:57 -07003565 * This free routine walks the list of POOL entries and if SKB is set to
3566 * non NULL it is unmapped and freed
3567 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003568static void iwl3945_rx_queue_free(struct iwl_priv *priv, struct iwl_rx_queue *rxq)
Zhu Yib481de92007-09-25 17:54:57 -07003569{
3570 int i;
3571 for (i = 0; i < RX_QUEUE_SIZE + RX_FREE_BUFFERS; i++) {
3572 if (rxq->pool[i].skb != NULL) {
3573 pci_unmap_single(priv->pci_dev,
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08003574 rxq->pool[i].real_dma_addr,
Zhu Yib481de92007-09-25 17:54:57 -07003575 IWL_RX_BUF_SIZE, PCI_DMA_FROMDEVICE);
3576 dev_kfree_skb(rxq->pool[i].skb);
3577 }
3578 }
3579
3580 pci_free_consistent(priv->pci_dev, 4 * RX_QUEUE_SIZE, rxq->bd,
3581 rxq->dma_addr);
3582 rxq->bd = NULL;
3583}
3584
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003585int iwl3945_rx_queue_alloc(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003586{
Abhijeet Kolekarcc2f3622008-12-19 10:37:25 +08003587 struct iwl_rx_queue *rxq = &priv->rxq;
Zhu Yib481de92007-09-25 17:54:57 -07003588 struct pci_dev *dev = priv->pci_dev;
3589 int i;
3590
3591 spin_lock_init(&rxq->lock);
3592 INIT_LIST_HEAD(&rxq->rx_free);
3593 INIT_LIST_HEAD(&rxq->rx_used);
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003594
3595 /* Alloc the circular buffer of Read Buffer Descriptors (RBDs) */
Zhu Yib481de92007-09-25 17:54:57 -07003596 rxq->bd = pci_alloc_consistent(dev, 4 * RX_QUEUE_SIZE, &rxq->dma_addr);
3597 if (!rxq->bd)
3598 return -ENOMEM;
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003599
Zhu Yib481de92007-09-25 17:54:57 -07003600 /* Fill the rx_used queue with _all_ of the Rx buffers */
3601 for (i = 0; i < RX_FREE_BUFFERS + RX_QUEUE_SIZE; i++)
3602 list_add_tail(&rxq->pool[i].list, &rxq->rx_used);
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003603
Zhu Yib481de92007-09-25 17:54:57 -07003604 /* Set us so that we have processed and used all buffers, but have
3605 * not restocked the Rx queue with fresh buffers */
3606 rxq->read = rxq->write = 0;
3607 rxq->free_count = 0;
3608 rxq->need_update = 0;
3609 return 0;
3610}
3611
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003612void iwl3945_rx_queue_reset(struct iwl_priv *priv, struct iwl_rx_queue *rxq)
Zhu Yib481de92007-09-25 17:54:57 -07003613{
3614 unsigned long flags;
3615 int i;
3616 spin_lock_irqsave(&rxq->lock, flags);
3617 INIT_LIST_HEAD(&rxq->rx_free);
3618 INIT_LIST_HEAD(&rxq->rx_used);
3619 /* Fill the rx_used queue with _all_ of the Rx buffers */
3620 for (i = 0; i < RX_FREE_BUFFERS + RX_QUEUE_SIZE; i++) {
3621 /* In the reset function, these buffers may have been allocated
3622 * to an SKB, so we need to unmap and free potential storage */
3623 if (rxq->pool[i].skb != NULL) {
3624 pci_unmap_single(priv->pci_dev,
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08003625 rxq->pool[i].real_dma_addr,
Zhu Yib481de92007-09-25 17:54:57 -07003626 IWL_RX_BUF_SIZE, PCI_DMA_FROMDEVICE);
3627 priv->alloc_rxb_skb--;
3628 dev_kfree_skb(rxq->pool[i].skb);
3629 rxq->pool[i].skb = NULL;
3630 }
3631 list_add_tail(&rxq->pool[i].list, &rxq->rx_used);
3632 }
3633
3634 /* Set us so that we have processed and used all buffers, but have
3635 * not restocked the Rx queue with fresh buffers */
3636 rxq->read = rxq->write = 0;
3637 rxq->free_count = 0;
3638 spin_unlock_irqrestore(&rxq->lock, flags);
3639}
3640
3641/* Convert linear signal-to-noise ratio into dB */
3642static u8 ratio2dB[100] = {
3643/* 0 1 2 3 4 5 6 7 8 9 */
3644 0, 0, 6, 10, 12, 14, 16, 17, 18, 19, /* 00 - 09 */
3645 20, 21, 22, 22, 23, 23, 24, 25, 26, 26, /* 10 - 19 */
3646 26, 26, 26, 27, 27, 28, 28, 28, 29, 29, /* 20 - 29 */
3647 29, 30, 30, 30, 31, 31, 31, 31, 32, 32, /* 30 - 39 */
3648 32, 32, 32, 33, 33, 33, 33, 33, 34, 34, /* 40 - 49 */
3649 34, 34, 34, 34, 35, 35, 35, 35, 35, 35, /* 50 - 59 */
3650 36, 36, 36, 36, 36, 36, 36, 37, 37, 37, /* 60 - 69 */
3651 37, 37, 37, 37, 37, 38, 38, 38, 38, 38, /* 70 - 79 */
3652 38, 38, 38, 38, 38, 39, 39, 39, 39, 39, /* 80 - 89 */
3653 39, 39, 39, 39, 39, 40, 40, 40, 40, 40 /* 90 - 99 */
3654};
3655
3656/* Calculates a relative dB value from a ratio of linear
3657 * (i.e. not dB) signal levels.
3658 * Conversion assumes that levels are voltages (20*log), not powers (10*log). */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003659int iwl3945_calc_db_from_ratio(int sig_ratio)
Zhu Yib481de92007-09-25 17:54:57 -07003660{
Adrian Bunk221c80c2008-02-02 23:19:01 +02003661 /* 1000:1 or higher just report as 60 dB */
3662 if (sig_ratio >= 1000)
Zhu Yib481de92007-09-25 17:54:57 -07003663 return 60;
3664
Adrian Bunk221c80c2008-02-02 23:19:01 +02003665 /* 100:1 or higher, divide by 10 and use table,
Zhu Yib481de92007-09-25 17:54:57 -07003666 * add 20 dB to make up for divide by 10 */
Adrian Bunk221c80c2008-02-02 23:19:01 +02003667 if (sig_ratio >= 100)
Tomas Winkler3ac7f142008-07-21 02:40:14 +03003668 return 20 + (int)ratio2dB[sig_ratio/10];
Zhu Yib481de92007-09-25 17:54:57 -07003669
3670 /* We shouldn't see this */
3671 if (sig_ratio < 1)
3672 return 0;
3673
3674 /* Use table for ratios 1:1 - 99:1 */
3675 return (int)ratio2dB[sig_ratio];
3676}
3677
3678#define PERFECT_RSSI (-20) /* dBm */
3679#define WORST_RSSI (-95) /* dBm */
3680#define RSSI_RANGE (PERFECT_RSSI - WORST_RSSI)
3681
3682/* Calculate an indication of rx signal quality (a percentage, not dBm!).
3683 * See http://www.ces.clemson.edu/linux/signal_quality.shtml for info
3684 * about formulas used below. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003685int iwl3945_calc_sig_qual(int rssi_dbm, int noise_dbm)
Zhu Yib481de92007-09-25 17:54:57 -07003686{
3687 int sig_qual;
3688 int degradation = PERFECT_RSSI - rssi_dbm;
3689
3690 /* If we get a noise measurement, use signal-to-noise ratio (SNR)
3691 * as indicator; formula is (signal dbm - noise dbm).
3692 * SNR at or above 40 is a great signal (100%).
3693 * Below that, scale to fit SNR of 0 - 40 dB within 0 - 100% indicator.
3694 * Weakest usable signal is usually 10 - 15 dB SNR. */
3695 if (noise_dbm) {
3696 if (rssi_dbm - noise_dbm >= 40)
3697 return 100;
3698 else if (rssi_dbm < noise_dbm)
3699 return 0;
3700 sig_qual = ((rssi_dbm - noise_dbm) * 5) / 2;
3701
3702 /* Else use just the signal level.
3703 * This formula is a least squares fit of data points collected and
3704 * compared with a reference system that had a percentage (%) display
3705 * for signal quality. */
3706 } else
3707 sig_qual = (100 * (RSSI_RANGE * RSSI_RANGE) - degradation *
3708 (15 * RSSI_RANGE + 62 * degradation)) /
3709 (RSSI_RANGE * RSSI_RANGE);
3710
3711 if (sig_qual > 100)
3712 sig_qual = 100;
3713 else if (sig_qual < 1)
3714 sig_qual = 0;
3715
3716 return sig_qual;
3717}
3718
3719/**
Ben Cahill9fbab512007-11-29 11:09:47 +08003720 * iwl3945_rx_handle - Main entry function for receiving responses from uCode
Zhu Yib481de92007-09-25 17:54:57 -07003721 *
3722 * Uses the priv->rx_handlers callback function array to invoke
3723 * the appropriate handlers, including command responses,
3724 * frame-received notifications, and other notifications.
3725 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003726static void iwl3945_rx_handle(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003727{
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08003728 struct iwl_rx_mem_buffer *rxb;
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08003729 struct iwl_rx_packet *pkt;
Abhijeet Kolekarcc2f3622008-12-19 10:37:25 +08003730 struct iwl_rx_queue *rxq = &priv->rxq;
Zhu Yib481de92007-09-25 17:54:57 -07003731 u32 r, i;
3732 int reclaim;
3733 unsigned long flags;
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08003734 u8 fill_rx = 0;
Mohamed Abbasd68ab682008-02-07 13:16:33 -08003735 u32 count = 8;
Zhu Yib481de92007-09-25 17:54:57 -07003736
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003737 /* uCode's read index (stored in shared DRAM) indicates the last Rx
3738 * buffer that the driver may process (last buffer filled by ucode). */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003739 r = iwl3945_hw_get_rx_read(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003740 i = rxq->read;
3741
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08003742 if (iwl3945_rx_queue_space(rxq) > (RX_QUEUE_SIZE / 2))
3743 fill_rx = 1;
Zhu Yib481de92007-09-25 17:54:57 -07003744 /* Rx interrupt, but nothing sent from uCode */
3745 if (i == r)
3746 IWL_DEBUG(IWL_DL_RX | IWL_DL_ISR, "r = %d, i = %d\n", r, i);
3747
3748 while (i != r) {
3749 rxb = rxq->queue[i];
3750
Ben Cahill9fbab512007-11-29 11:09:47 +08003751 /* If an RXB doesn't have a Rx queue slot associated with it,
Zhu Yib481de92007-09-25 17:54:57 -07003752 * then a bug has been introduced in the queue refilling
3753 * routines -- catch it here */
3754 BUG_ON(rxb == NULL);
3755
3756 rxq->queue[i] = NULL;
3757
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08003758 pci_dma_sync_single_for_cpu(priv->pci_dev, rxb->real_dma_addr,
Zhu Yib481de92007-09-25 17:54:57 -07003759 IWL_RX_BUF_SIZE,
3760 PCI_DMA_FROMDEVICE);
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08003761 pkt = (struct iwl_rx_packet *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07003762
3763 /* Reclaim a command buffer only if this packet is a response
3764 * to a (driver-originated) command.
3765 * If the packet (e.g. Rx frame) originated from uCode,
3766 * there is no command buffer to reclaim.
3767 * Ucode should set SEQ_RX_FRAME bit if ucode-originated,
3768 * but apparently a few don't get set; catch them here. */
3769 reclaim = !(pkt->hdr.sequence & SEQ_RX_FRAME) &&
3770 (pkt->hdr.cmd != STATISTICS_NOTIFICATION) &&
3771 (pkt->hdr.cmd != REPLY_TX);
3772
3773 /* Based on type of command response or notification,
3774 * handle those that need handling via function in
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003775 * rx_handlers table. See iwl3945_setup_rx_handlers() */
Zhu Yib481de92007-09-25 17:54:57 -07003776 if (priv->rx_handlers[pkt->hdr.cmd]) {
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08003777 IWL_DEBUG(IWL_DL_HCMD | IWL_DL_RX | IWL_DL_ISR,
Zhu Yib481de92007-09-25 17:54:57 -07003778 "r = %d, i = %d, %s, 0x%02x\n", r, i,
3779 get_cmd_string(pkt->hdr.cmd), pkt->hdr.cmd);
3780 priv->rx_handlers[pkt->hdr.cmd] (priv, rxb);
3781 } else {
3782 /* No handling needed */
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08003783 IWL_DEBUG(IWL_DL_HCMD | IWL_DL_RX | IWL_DL_ISR,
Zhu Yib481de92007-09-25 17:54:57 -07003784 "r %d i %d No handler needed for %s, 0x%02x\n",
3785 r, i, get_cmd_string(pkt->hdr.cmd),
3786 pkt->hdr.cmd);
3787 }
3788
3789 if (reclaim) {
Ben Cahill9fbab512007-11-29 11:09:47 +08003790 /* Invoke any callbacks, transfer the skb to caller, and
3791 * fire off the (possibly) blocking iwl3945_send_cmd()
Zhu Yib481de92007-09-25 17:54:57 -07003792 * as we reclaim the driver command queue */
3793 if (rxb && rxb->skb)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003794 iwl3945_tx_cmd_complete(priv, rxb);
Zhu Yib481de92007-09-25 17:54:57 -07003795 else
Winkler, Tomas39aadf82008-12-19 10:37:32 +08003796 IWL_WARN(priv, "Claim null rxb?\n");
Zhu Yib481de92007-09-25 17:54:57 -07003797 }
3798
3799 /* For now we just don't re-use anything. We can tweak this
3800 * later to try and re-use notification packets and SKBs that
3801 * fail to Rx correctly */
3802 if (rxb->skb != NULL) {
3803 priv->alloc_rxb_skb--;
3804 dev_kfree_skb_any(rxb->skb);
3805 rxb->skb = NULL;
3806 }
3807
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08003808 pci_unmap_single(priv->pci_dev, rxb->real_dma_addr,
Zhu Yib481de92007-09-25 17:54:57 -07003809 IWL_RX_BUF_SIZE, PCI_DMA_FROMDEVICE);
3810 spin_lock_irqsave(&rxq->lock, flags);
3811 list_add_tail(&rxb->list, &priv->rxq.rx_used);
3812 spin_unlock_irqrestore(&rxq->lock, flags);
3813 i = (i + 1) & RX_QUEUE_MASK;
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08003814 /* If there are a lot of unused frames,
3815 * restock the Rx queue so ucode won't assert. */
3816 if (fill_rx) {
3817 count++;
3818 if (count >= 8) {
3819 priv->rxq.read = i;
3820 __iwl3945_rx_replenish(priv);
3821 count = 0;
3822 }
3823 }
Zhu Yib481de92007-09-25 17:54:57 -07003824 }
3825
3826 /* Backtrack one entry */
3827 priv->rxq.read = i;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003828 iwl3945_rx_queue_restock(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003829}
3830
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003831/**
3832 * iwl3945_tx_queue_update_write_ptr - Send new write index to hardware
3833 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003834static int iwl3945_tx_queue_update_write_ptr(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003835 struct iwl3945_tx_queue *txq)
Zhu Yib481de92007-09-25 17:54:57 -07003836{
3837 u32 reg = 0;
3838 int rc = 0;
3839 int txq_id = txq->q.id;
3840
3841 if (txq->need_update == 0)
3842 return rc;
3843
3844 /* if we're trying to save power */
3845 if (test_bit(STATUS_POWER_PMI, &priv->status)) {
3846 /* wake up nic if it's powered down ...
3847 * uCode will wake up, and interrupt us again, so next
3848 * time we'll skip this part. */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003849 reg = iwl_read32(priv, CSR_UCODE_DRV_GP1);
Zhu Yib481de92007-09-25 17:54:57 -07003850
3851 if (reg & CSR_UCODE_DRV_GP1_BIT_MAC_SLEEP) {
3852 IWL_DEBUG_INFO("Requesting wakeup, GP1 = 0x%x\n", reg);
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003853 iwl_set_bit(priv, CSR_GP_CNTRL,
Zhu Yib481de92007-09-25 17:54:57 -07003854 CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
3855 return rc;
3856 }
3857
3858 /* restore this queue's parameters in nic hardware. */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003859 rc = iwl_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003860 if (rc)
3861 return rc;
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003862 iwl_write_direct32(priv, HBUS_TARG_WRPTR,
Tomas Winklerfc4b6852007-10-25 17:15:24 +08003863 txq->q.write_ptr | (txq_id << 8));
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003864 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003865
3866 /* else not in power-save mode, uCode will never sleep when we're
3867 * trying to tx (during RFKILL, we're not trying to tx). */
3868 } else
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003869 iwl_write32(priv, HBUS_TARG_WRPTR,
Tomas Winklerfc4b6852007-10-25 17:15:24 +08003870 txq->q.write_ptr | (txq_id << 8));
Zhu Yib481de92007-09-25 17:54:57 -07003871
3872 txq->need_update = 0;
3873
3874 return rc;
3875}
3876
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08003877#ifdef CONFIG_IWL3945_DEBUG
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003878static void iwl3945_print_rx_config_cmd(struct iwl_priv *priv,
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08003879 struct iwl3945_rxon_cmd *rxon)
Zhu Yib481de92007-09-25 17:54:57 -07003880{
3881 IWL_DEBUG_RADIO("RX CONFIG:\n");
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08003882 iwl_print_hex_dump(priv, IWL_DL_RADIO, (u8 *) rxon, sizeof(*rxon));
Zhu Yib481de92007-09-25 17:54:57 -07003883 IWL_DEBUG_RADIO("u16 channel: 0x%x\n", le16_to_cpu(rxon->channel));
3884 IWL_DEBUG_RADIO("u32 flags: 0x%08X\n", le32_to_cpu(rxon->flags));
3885 IWL_DEBUG_RADIO("u32 filter_flags: 0x%08x\n",
3886 le32_to_cpu(rxon->filter_flags));
3887 IWL_DEBUG_RADIO("u8 dev_type: 0x%x\n", rxon->dev_type);
3888 IWL_DEBUG_RADIO("u8 ofdm_basic_rates: 0x%02x\n",
3889 rxon->ofdm_basic_rates);
3890 IWL_DEBUG_RADIO("u8 cck_basic_rates: 0x%02x\n", rxon->cck_basic_rates);
Johannes Berge1749612008-10-27 15:59:26 -07003891 IWL_DEBUG_RADIO("u8[6] node_addr: %pM\n", rxon->node_addr);
3892 IWL_DEBUG_RADIO("u8[6] bssid_addr: %pM\n", rxon->bssid_addr);
Zhu Yib481de92007-09-25 17:54:57 -07003893 IWL_DEBUG_RADIO("u16 assoc_id: 0x%x\n", le16_to_cpu(rxon->assoc_id));
3894}
3895#endif
3896
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003897static void iwl3945_enable_interrupts(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003898{
3899 IWL_DEBUG_ISR("Enabling interrupts\n");
3900 set_bit(STATUS_INT_ENABLED, &priv->status);
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003901 iwl_write32(priv, CSR_INT_MASK, CSR_INI_SET_MASK);
Zhu Yib481de92007-09-25 17:54:57 -07003902}
3903
Mohamed Abbas0359fac2008-03-28 16:21:08 -07003904
3905/* call this function to flush any scheduled tasklet */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003906static inline void iwl_synchronize_irq(struct iwl_priv *priv)
Mohamed Abbas0359fac2008-03-28 16:21:08 -07003907{
Tomas Winklera96a27f2008-10-23 23:48:56 -07003908 /* wait to make sure we flush pending tasklet*/
Mohamed Abbas0359fac2008-03-28 16:21:08 -07003909 synchronize_irq(priv->pci_dev->irq);
3910 tasklet_kill(&priv->irq_tasklet);
3911}
3912
3913
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003914static inline void iwl3945_disable_interrupts(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003915{
3916 clear_bit(STATUS_INT_ENABLED, &priv->status);
3917
3918 /* disable interrupts from uCode/NIC to host */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003919 iwl_write32(priv, CSR_INT_MASK, 0x00000000);
Zhu Yib481de92007-09-25 17:54:57 -07003920
3921 /* acknowledge/clear/reset any interrupts still pending
3922 * from uCode or flow handler (Rx/Tx DMA) */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003923 iwl_write32(priv, CSR_INT, 0xffffffff);
3924 iwl_write32(priv, CSR_FH_INT_STATUS, 0xffffffff);
Zhu Yib481de92007-09-25 17:54:57 -07003925 IWL_DEBUG_ISR("Disabled interrupts\n");
3926}
3927
3928static const char *desc_lookup(int i)
3929{
3930 switch (i) {
3931 case 1:
3932 return "FAIL";
3933 case 2:
3934 return "BAD_PARAM";
3935 case 3:
3936 return "BAD_CHECKSUM";
3937 case 4:
3938 return "NMI_INTERRUPT";
3939 case 5:
3940 return "SYSASSERT";
3941 case 6:
3942 return "FATAL_ERROR";
3943 }
3944
3945 return "UNKNOWN";
3946}
3947
3948#define ERROR_START_OFFSET (1 * sizeof(u32))
3949#define ERROR_ELEM_SIZE (7 * sizeof(u32))
3950
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003951static void iwl3945_dump_nic_error_log(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003952{
3953 u32 i;
3954 u32 desc, time, count, base, data1;
3955 u32 blink1, blink2, ilink1, ilink2;
3956 int rc;
3957
3958 base = le32_to_cpu(priv->card_alive.error_event_table_ptr);
3959
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003960 if (!iwl3945_hw_valid_rtc_data_addr(base)) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08003961 IWL_ERR(priv, "Not valid error log pointer 0x%08X\n", base);
Zhu Yib481de92007-09-25 17:54:57 -07003962 return;
3963 }
3964
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003965 rc = iwl_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003966 if (rc) {
Winkler, Tomas39aadf82008-12-19 10:37:32 +08003967 IWL_WARN(priv, "Can not read from adapter at this time.\n");
Zhu Yib481de92007-09-25 17:54:57 -07003968 return;
3969 }
3970
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003971 count = iwl_read_targ_mem(priv, base);
Zhu Yib481de92007-09-25 17:54:57 -07003972
3973 if (ERROR_START_OFFSET <= count * ERROR_ELEM_SIZE) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08003974 IWL_ERR(priv, "Start IWL Error Log Dump:\n");
3975 IWL_ERR(priv, "Status: 0x%08lX, count: %d\n",
3976 priv->status, count);
Zhu Yib481de92007-09-25 17:54:57 -07003977 }
3978
Winkler, Tomas15b16872008-12-19 10:37:33 +08003979 IWL_ERR(priv, "Desc Time asrtPC blink2 "
Zhu Yib481de92007-09-25 17:54:57 -07003980 "ilink1 nmiPC Line\n");
3981 for (i = ERROR_START_OFFSET;
3982 i < (count * ERROR_ELEM_SIZE) + ERROR_START_OFFSET;
3983 i += ERROR_ELEM_SIZE) {
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003984 desc = iwl_read_targ_mem(priv, base + i);
Zhu Yib481de92007-09-25 17:54:57 -07003985 time =
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003986 iwl_read_targ_mem(priv, base + i + 1 * sizeof(u32));
Zhu Yib481de92007-09-25 17:54:57 -07003987 blink1 =
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003988 iwl_read_targ_mem(priv, base + i + 2 * sizeof(u32));
Zhu Yib481de92007-09-25 17:54:57 -07003989 blink2 =
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003990 iwl_read_targ_mem(priv, base + i + 3 * sizeof(u32));
Zhu Yib481de92007-09-25 17:54:57 -07003991 ilink1 =
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003992 iwl_read_targ_mem(priv, base + i + 4 * sizeof(u32));
Zhu Yib481de92007-09-25 17:54:57 -07003993 ilink2 =
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003994 iwl_read_targ_mem(priv, base + i + 5 * sizeof(u32));
Zhu Yib481de92007-09-25 17:54:57 -07003995 data1 =
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003996 iwl_read_targ_mem(priv, base + i + 6 * sizeof(u32));
Zhu Yib481de92007-09-25 17:54:57 -07003997
Winkler, Tomas15b16872008-12-19 10:37:33 +08003998 IWL_ERR(priv,
3999 "%-13s (#%d) %010u 0x%05X 0x%05X 0x%05X 0x%05X %u\n\n",
4000 desc_lookup(desc), desc, time, blink1, blink2,
4001 ilink1, ilink2, data1);
Zhu Yib481de92007-09-25 17:54:57 -07004002 }
4003
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08004004 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004005
4006}
4007
Ben Cahillf58177b2007-11-29 11:09:43 +08004008#define EVENT_START_OFFSET (6 * sizeof(u32))
Zhu Yib481de92007-09-25 17:54:57 -07004009
4010/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004011 * iwl3945_print_event_log - Dump error event log to syslog
Zhu Yib481de92007-09-25 17:54:57 -07004012 *
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08004013 * NOTE: Must be called with iwl_grab_nic_access() already obtained!
Zhu Yib481de92007-09-25 17:54:57 -07004014 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08004015static void iwl3945_print_event_log(struct iwl_priv *priv, u32 start_idx,
Zhu Yib481de92007-09-25 17:54:57 -07004016 u32 num_events, u32 mode)
4017{
4018 u32 i;
4019 u32 base; /* SRAM byte address of event log header */
4020 u32 event_size; /* 2 u32s, or 3 u32s if timestamp recorded */
4021 u32 ptr; /* SRAM byte address of log data */
4022 u32 ev, time, data; /* event log data */
4023
4024 if (num_events == 0)
4025 return;
4026
4027 base = le32_to_cpu(priv->card_alive.log_event_table_ptr);
4028
4029 if (mode == 0)
4030 event_size = 2 * sizeof(u32);
4031 else
4032 event_size = 3 * sizeof(u32);
4033
4034 ptr = base + EVENT_START_OFFSET + (start_idx * event_size);
4035
4036 /* "time" is actually "data" for mode 0 (no timestamp).
4037 * place event id # at far right for easier visual parsing. */
4038 for (i = 0; i < num_events; i++) {
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08004039 ev = iwl_read_targ_mem(priv, ptr);
Zhu Yib481de92007-09-25 17:54:57 -07004040 ptr += sizeof(u32);
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08004041 time = iwl_read_targ_mem(priv, ptr);
Zhu Yib481de92007-09-25 17:54:57 -07004042 ptr += sizeof(u32);
Winkler, Tomas15b16872008-12-19 10:37:33 +08004043 if (mode == 0) {
4044 /* data, ev */
4045 IWL_ERR(priv, "0x%08x\t%04u\n", time, ev);
4046 } else {
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08004047 data = iwl_read_targ_mem(priv, ptr);
Zhu Yib481de92007-09-25 17:54:57 -07004048 ptr += sizeof(u32);
Winkler, Tomas15b16872008-12-19 10:37:33 +08004049 IWL_ERR(priv, "%010u\t0x%08x\t%04u\n", time, data, ev);
Zhu Yib481de92007-09-25 17:54:57 -07004050 }
4051 }
4052}
4053
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08004054static void iwl3945_dump_nic_event_log(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004055{
4056 int rc;
4057 u32 base; /* SRAM byte address of event log header */
4058 u32 capacity; /* event log capacity in # entries */
4059 u32 mode; /* 0 - no timestamp, 1 - timestamp recorded */
4060 u32 num_wraps; /* # times uCode wrapped to top of log */
4061 u32 next_entry; /* index of next entry to be written by uCode */
4062 u32 size; /* # entries that we'll print */
4063
4064 base = le32_to_cpu(priv->card_alive.log_event_table_ptr);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004065 if (!iwl3945_hw_valid_rtc_data_addr(base)) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08004066 IWL_ERR(priv, "Invalid event log pointer 0x%08X\n", base);
Zhu Yib481de92007-09-25 17:54:57 -07004067 return;
4068 }
4069
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08004070 rc = iwl_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004071 if (rc) {
Winkler, Tomas39aadf82008-12-19 10:37:32 +08004072 IWL_WARN(priv, "Can not read from adapter at this time.\n");
Zhu Yib481de92007-09-25 17:54:57 -07004073 return;
4074 }
4075
4076 /* event log header */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08004077 capacity = iwl_read_targ_mem(priv, base);
4078 mode = iwl_read_targ_mem(priv, base + (1 * sizeof(u32)));
4079 num_wraps = iwl_read_targ_mem(priv, base + (2 * sizeof(u32)));
4080 next_entry = iwl_read_targ_mem(priv, base + (3 * sizeof(u32)));
Zhu Yib481de92007-09-25 17:54:57 -07004081
4082 size = num_wraps ? capacity : next_entry;
4083
4084 /* bail out if nothing in log */
4085 if (size == 0) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08004086 IWL_ERR(priv, "Start IWL Event Log Dump: nothing in log\n");
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08004087 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004088 return;
4089 }
4090
Winkler, Tomas15b16872008-12-19 10:37:33 +08004091 IWL_ERR(priv, "Start IWL Event Log Dump: display count %d, wraps %d\n",
Zhu Yib481de92007-09-25 17:54:57 -07004092 size, num_wraps);
4093
4094 /* if uCode has wrapped back to top of log, start at the oldest entry,
4095 * i.e the next one that uCode would fill. */
4096 if (num_wraps)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004097 iwl3945_print_event_log(priv, next_entry,
Zhu Yib481de92007-09-25 17:54:57 -07004098 capacity - next_entry, mode);
4099
4100 /* (then/else) start at top of log */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004101 iwl3945_print_event_log(priv, 0, next_entry, mode);
Zhu Yib481de92007-09-25 17:54:57 -07004102
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08004103 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004104}
4105
4106/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004107 * iwl3945_irq_handle_error - called for HW or SW error interrupt from card
Zhu Yib481de92007-09-25 17:54:57 -07004108 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08004109static void iwl3945_irq_handle_error(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004110{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004111 /* Set the FW error flag -- cleared on iwl3945_down */
Zhu Yib481de92007-09-25 17:54:57 -07004112 set_bit(STATUS_FW_ERROR, &priv->status);
4113
4114 /* Cancel currently queued command. */
4115 clear_bit(STATUS_HCMD_ACTIVE, &priv->status);
4116
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08004117#ifdef CONFIG_IWL3945_DEBUG
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08004118 if (priv->debug_level & IWL_DL_FW_ERRORS) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004119 iwl3945_dump_nic_error_log(priv);
4120 iwl3945_dump_nic_event_log(priv);
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08004121 iwl3945_print_rx_config_cmd(priv, &priv->staging39_rxon);
Zhu Yib481de92007-09-25 17:54:57 -07004122 }
4123#endif
4124
4125 wake_up_interruptible(&priv->wait_command_queue);
4126
4127 /* Keep the restart process from trying to send host
4128 * commands by clearing the INIT status bit */
4129 clear_bit(STATUS_READY, &priv->status);
4130
4131 if (!test_bit(STATUS_EXIT_PENDING, &priv->status)) {
4132 IWL_DEBUG(IWL_DL_INFO | IWL_DL_FW_ERRORS,
4133 "Restarting adapter due to uCode error.\n");
4134
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004135 if (iwl3945_is_associated(priv)) {
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08004136 memcpy(&priv->recovery39_rxon, &priv->active39_rxon,
4137 sizeof(priv->recovery39_rxon));
Zhu Yib481de92007-09-25 17:54:57 -07004138 priv->error_recovering = 1;
4139 }
4140 queue_work(priv->workqueue, &priv->restart);
4141 }
4142}
4143
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08004144static void iwl3945_error_recovery(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004145{
4146 unsigned long flags;
4147
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08004148 memcpy(&priv->staging39_rxon, &priv->recovery39_rxon,
4149 sizeof(priv->staging39_rxon));
4150 priv->staging39_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004151 iwl3945_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004152
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004153 iwl3945_add_station(priv, priv->bssid, 1, 0);
Zhu Yib481de92007-09-25 17:54:57 -07004154
4155 spin_lock_irqsave(&priv->lock, flags);
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08004156 priv->assoc_id = le16_to_cpu(priv->staging39_rxon.assoc_id);
Zhu Yib481de92007-09-25 17:54:57 -07004157 priv->error_recovering = 0;
4158 spin_unlock_irqrestore(&priv->lock, flags);
4159}
4160
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08004161static void iwl3945_irq_tasklet(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004162{
4163 u32 inta, handled = 0;
4164 u32 inta_fh;
4165 unsigned long flags;
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08004166#ifdef CONFIG_IWL3945_DEBUG
Zhu Yib481de92007-09-25 17:54:57 -07004167 u32 inta_mask;
4168#endif
4169
4170 spin_lock_irqsave(&priv->lock, flags);
4171
4172 /* Ack/clear/reset pending uCode interrupts.
4173 * Note: Some bits in CSR_INT are "OR" of bits in CSR_FH_INT_STATUS,
4174 * and will clear only when CSR_FH_INT_STATUS gets cleared. */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08004175 inta = iwl_read32(priv, CSR_INT);
4176 iwl_write32(priv, CSR_INT, inta);
Zhu Yib481de92007-09-25 17:54:57 -07004177
4178 /* Ack/clear/reset pending flow-handler (DMA) interrupts.
4179 * Any new interrupts that happen after this, either while we're
4180 * in this tasklet, or later, will show up in next ISR/tasklet. */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08004181 inta_fh = iwl_read32(priv, CSR_FH_INT_STATUS);
4182 iwl_write32(priv, CSR_FH_INT_STATUS, inta_fh);
Zhu Yib481de92007-09-25 17:54:57 -07004183
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08004184#ifdef CONFIG_IWL3945_DEBUG
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08004185 if (priv->debug_level & IWL_DL_ISR) {
Ben Cahill9fbab512007-11-29 11:09:47 +08004186 /* just for debug */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08004187 inta_mask = iwl_read32(priv, CSR_INT_MASK);
Zhu Yib481de92007-09-25 17:54:57 -07004188 IWL_DEBUG_ISR("inta 0x%08x, enabled 0x%08x, fh 0x%08x\n",
4189 inta, inta_mask, inta_fh);
4190 }
4191#endif
4192
4193 /* Since CSR_INT and CSR_FH_INT_STATUS reads and clears are not
4194 * atomic, make sure that inta covers all the interrupts that
4195 * we've discovered, even if FH interrupt came in just after
4196 * reading CSR_INT. */
Tomas Winkler6f83eaa2008-03-04 18:09:28 -08004197 if (inta_fh & CSR39_FH_INT_RX_MASK)
Zhu Yib481de92007-09-25 17:54:57 -07004198 inta |= CSR_INT_BIT_FH_RX;
Tomas Winkler6f83eaa2008-03-04 18:09:28 -08004199 if (inta_fh & CSR39_FH_INT_TX_MASK)
Zhu Yib481de92007-09-25 17:54:57 -07004200 inta |= CSR_INT_BIT_FH_TX;
4201
4202 /* Now service all interrupt bits discovered above. */
4203 if (inta & CSR_INT_BIT_HW_ERR) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08004204 IWL_ERR(priv, "Microcode HW error detected. Restarting.\n");
Zhu Yib481de92007-09-25 17:54:57 -07004205
4206 /* Tell the device to stop sending interrupts */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004207 iwl3945_disable_interrupts(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004208
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004209 iwl3945_irq_handle_error(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004210
4211 handled |= CSR_INT_BIT_HW_ERR;
4212
4213 spin_unlock_irqrestore(&priv->lock, flags);
4214
4215 return;
4216 }
4217
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08004218#ifdef CONFIG_IWL3945_DEBUG
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08004219 if (priv->debug_level & (IWL_DL_ISR)) {
Zhu Yib481de92007-09-25 17:54:57 -07004220 /* NIC fires this, but we don't use it, redundant with WAKEUP */
Joonwoo Park25c03d82008-01-23 10:15:20 -08004221 if (inta & CSR_INT_BIT_SCD)
4222 IWL_DEBUG_ISR("Scheduler finished to transmit "
4223 "the frame/frames.\n");
Zhu Yib481de92007-09-25 17:54:57 -07004224
4225 /* Alive notification via Rx interrupt will do the real work */
4226 if (inta & CSR_INT_BIT_ALIVE)
4227 IWL_DEBUG_ISR("Alive interrupt\n");
4228 }
4229#endif
4230 /* Safely ignore these bits for debug checks below */
Joonwoo Park25c03d82008-01-23 10:15:20 -08004231 inta &= ~(CSR_INT_BIT_SCD | CSR_INT_BIT_ALIVE);
Zhu Yib481de92007-09-25 17:54:57 -07004232
Zhu Yib481de92007-09-25 17:54:57 -07004233 /* Error detected by uCode */
4234 if (inta & CSR_INT_BIT_SW_ERR) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08004235 IWL_ERR(priv, "Microcode SW error detected. "
4236 "Restarting 0x%X.\n", inta);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004237 iwl3945_irq_handle_error(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004238 handled |= CSR_INT_BIT_SW_ERR;
4239 }
4240
4241 /* uCode wakes up after power-down sleep */
4242 if (inta & CSR_INT_BIT_WAKEUP) {
4243 IWL_DEBUG_ISR("Wakeup interrupt\n");
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004244 iwl3945_rx_queue_update_write_ptr(priv, &priv->rxq);
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08004245 iwl3945_tx_queue_update_write_ptr(priv, &priv->txq39[0]);
4246 iwl3945_tx_queue_update_write_ptr(priv, &priv->txq39[1]);
4247 iwl3945_tx_queue_update_write_ptr(priv, &priv->txq39[2]);
4248 iwl3945_tx_queue_update_write_ptr(priv, &priv->txq39[3]);
4249 iwl3945_tx_queue_update_write_ptr(priv, &priv->txq39[4]);
4250 iwl3945_tx_queue_update_write_ptr(priv, &priv->txq39[5]);
Zhu Yib481de92007-09-25 17:54:57 -07004251
4252 handled |= CSR_INT_BIT_WAKEUP;
4253 }
4254
4255 /* All uCode command responses, including Tx command responses,
4256 * Rx "responses" (frame-received notification), and other
4257 * notifications from uCode come through here*/
4258 if (inta & (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX)) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004259 iwl3945_rx_handle(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004260 handled |= (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX);
4261 }
4262
4263 if (inta & CSR_INT_BIT_FH_TX) {
4264 IWL_DEBUG_ISR("Tx interrupt\n");
4265
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08004266 iwl_write32(priv, CSR_FH_INT_STATUS, (1 << 6));
4267 if (!iwl_grab_nic_access(priv)) {
4268 iwl_write_direct32(priv, FH39_TCSR_CREDIT
Tomas Winklerbddadf82008-12-19 10:37:01 +08004269 (FH39_SRVC_CHNL), 0x0);
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08004270 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004271 }
4272 handled |= CSR_INT_BIT_FH_TX;
4273 }
4274
4275 if (inta & ~handled)
Winkler, Tomas15b16872008-12-19 10:37:33 +08004276 IWL_ERR(priv, "Unhandled INTA bits 0x%08x\n", inta & ~handled);
Zhu Yib481de92007-09-25 17:54:57 -07004277
4278 if (inta & ~CSR_INI_SET_MASK) {
Winkler, Tomas39aadf82008-12-19 10:37:32 +08004279 IWL_WARN(priv, "Disabled INTA bits 0x%08x were pending\n",
Zhu Yib481de92007-09-25 17:54:57 -07004280 inta & ~CSR_INI_SET_MASK);
Winkler, Tomas39aadf82008-12-19 10:37:32 +08004281 IWL_WARN(priv, " with FH_INT = 0x%08x\n", inta_fh);
Zhu Yib481de92007-09-25 17:54:57 -07004282 }
4283
4284 /* Re-enable all interrupts */
Mohamed Abbas0359fac2008-03-28 16:21:08 -07004285 /* only Re-enable if disabled by irq */
4286 if (test_bit(STATUS_INT_ENABLED, &priv->status))
4287 iwl3945_enable_interrupts(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004288
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08004289#ifdef CONFIG_IWL3945_DEBUG
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08004290 if (priv->debug_level & (IWL_DL_ISR)) {
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08004291 inta = iwl_read32(priv, CSR_INT);
4292 inta_mask = iwl_read32(priv, CSR_INT_MASK);
4293 inta_fh = iwl_read32(priv, CSR_FH_INT_STATUS);
Zhu Yib481de92007-09-25 17:54:57 -07004294 IWL_DEBUG_ISR("End inta 0x%08x, enabled 0x%08x, fh 0x%08x, "
4295 "flags 0x%08lx\n", inta, inta_mask, inta_fh, flags);
4296 }
4297#endif
4298 spin_unlock_irqrestore(&priv->lock, flags);
4299}
4300
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004301static irqreturn_t iwl3945_isr(int irq, void *data)
Zhu Yib481de92007-09-25 17:54:57 -07004302{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08004303 struct iwl_priv *priv = data;
Zhu Yib481de92007-09-25 17:54:57 -07004304 u32 inta, inta_mask;
4305 u32 inta_fh;
4306 if (!priv)
4307 return IRQ_NONE;
4308
4309 spin_lock(&priv->lock);
4310
4311 /* Disable (but don't clear!) interrupts here to avoid
4312 * back-to-back ISRs and sporadic interrupts from our NIC.
4313 * If we have something to service, the tasklet will re-enable ints.
4314 * If we *don't* have something, we'll re-enable before leaving here. */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08004315 inta_mask = iwl_read32(priv, CSR_INT_MASK); /* just for debug */
4316 iwl_write32(priv, CSR_INT_MASK, 0x00000000);
Zhu Yib481de92007-09-25 17:54:57 -07004317
4318 /* Discover which interrupts are active/pending */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08004319 inta = iwl_read32(priv, CSR_INT);
4320 inta_fh = iwl_read32(priv, CSR_FH_INT_STATUS);
Zhu Yib481de92007-09-25 17:54:57 -07004321
4322 /* Ignore interrupt if there's nothing in NIC to service.
4323 * This may be due to IRQ shared with another device,
4324 * or due to sporadic interrupts thrown from our NIC. */
4325 if (!inta && !inta_fh) {
4326 IWL_DEBUG_ISR("Ignore interrupt, inta == 0, inta_fh == 0\n");
4327 goto none;
4328 }
4329
4330 if ((inta == 0xFFFFFFFF) || ((inta & 0xFFFFFFF0) == 0xa5a5a5a0)) {
4331 /* Hardware disappeared */
Winkler, Tomas39aadf82008-12-19 10:37:32 +08004332 IWL_WARN(priv, "HARDWARE GONE?? INTA == 0x%08x\n", inta);
Oliver Neukumcb4da1a2007-11-13 21:10:32 -08004333 goto unplugged;
Zhu Yib481de92007-09-25 17:54:57 -07004334 }
4335
4336 IWL_DEBUG_ISR("ISR inta 0x%08x, enabled 0x%08x, fh 0x%08x\n",
4337 inta, inta_mask, inta_fh);
4338
Joonwoo Park25c03d82008-01-23 10:15:20 -08004339 inta &= ~CSR_INT_BIT_SCD;
4340
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004341 /* iwl3945_irq_tasklet() will service interrupts and re-enable them */
Joonwoo Park25c03d82008-01-23 10:15:20 -08004342 if (likely(inta || inta_fh))
4343 tasklet_schedule(&priv->irq_tasklet);
Oliver Neukumcb4da1a2007-11-13 21:10:32 -08004344unplugged:
Zhu Yib481de92007-09-25 17:54:57 -07004345 spin_unlock(&priv->lock);
4346
4347 return IRQ_HANDLED;
4348
4349 none:
4350 /* re-enable interrupts here since we don't have anything to service. */
Mohamed Abbas0359fac2008-03-28 16:21:08 -07004351 /* only Re-enable if disabled by irq */
4352 if (test_bit(STATUS_INT_ENABLED, &priv->status))
4353 iwl3945_enable_interrupts(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004354 spin_unlock(&priv->lock);
4355 return IRQ_NONE;
4356}
4357
4358/************************** EEPROM BANDS ****************************
4359 *
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004360 * The iwl3945_eeprom_band definitions below provide the mapping from the
Zhu Yib481de92007-09-25 17:54:57 -07004361 * EEPROM contents to the specific channel number supported for each
4362 * band.
4363 *
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08004364 * For example, iwl3945_priv->eeprom39.band_3_channels[4] from the band_3
Zhu Yib481de92007-09-25 17:54:57 -07004365 * definition below maps to physical channel 42 in the 5.2GHz spectrum.
4366 * The specific geography and calibration information for that channel
4367 * is contained in the eeprom map itself.
4368 *
4369 * During init, we copy the eeprom information and channel map
4370 * information into priv->channel_info_24/52 and priv->channel_map_24/52
4371 *
4372 * channel_map_24/52 provides the index in the channel_info array for a
4373 * given channel. We have to have two separate maps as there is channel
4374 * overlap with the 2.4GHz and 5.2GHz spectrum as seen in band_1 and
4375 * band_2
4376 *
4377 * A value of 0xff stored in the channel_map indicates that the channel
4378 * is not supported by the hardware at all.
4379 *
4380 * A value of 0xfe in the channel_map indicates that the channel is not
4381 * valid for Tx with the current hardware. This means that
4382 * while the system can tune and receive on a given channel, it may not
4383 * be able to associate or transmit any frames on that
4384 * channel. There is no corresponding channel information for that
4385 * entry.
4386 *
4387 *********************************************************************/
4388
4389/* 2.4 GHz */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004390static const u8 iwl3945_eeprom_band_1[14] = {
Zhu Yib481de92007-09-25 17:54:57 -07004391 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14
4392};
4393
4394/* 5.2 GHz bands */
Ben Cahill9fbab512007-11-29 11:09:47 +08004395static const u8 iwl3945_eeprom_band_2[] = { /* 4915-5080MHz */
Zhu Yib481de92007-09-25 17:54:57 -07004396 183, 184, 185, 187, 188, 189, 192, 196, 7, 8, 11, 12, 16
4397};
4398
Ben Cahill9fbab512007-11-29 11:09:47 +08004399static const u8 iwl3945_eeprom_band_3[] = { /* 5170-5320MHz */
Zhu Yib481de92007-09-25 17:54:57 -07004400 34, 36, 38, 40, 42, 44, 46, 48, 52, 56, 60, 64
4401};
4402
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004403static const u8 iwl3945_eeprom_band_4[] = { /* 5500-5700MHz */
Zhu Yib481de92007-09-25 17:54:57 -07004404 100, 104, 108, 112, 116, 120, 124, 128, 132, 136, 140
4405};
4406
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004407static const u8 iwl3945_eeprom_band_5[] = { /* 5725-5825MHz */
Zhu Yib481de92007-09-25 17:54:57 -07004408 145, 149, 153, 157, 161, 165
4409};
4410
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08004411static void iwl3945_init_band_reference(const struct iwl_priv *priv, int band,
Zhu Yib481de92007-09-25 17:54:57 -07004412 int *eeprom_ch_count,
Samuel Ortiz0f741d92008-12-19 10:37:10 +08004413 const struct iwl_eeprom_channel
Zhu Yib481de92007-09-25 17:54:57 -07004414 **eeprom_ch_info,
4415 const u8 **eeprom_ch_index)
4416{
4417 switch (band) {
4418 case 1: /* 2.4GHz band */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004419 *eeprom_ch_count = ARRAY_SIZE(iwl3945_eeprom_band_1);
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08004420 *eeprom_ch_info = priv->eeprom39.band_1_channels;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004421 *eeprom_ch_index = iwl3945_eeprom_band_1;
Zhu Yib481de92007-09-25 17:54:57 -07004422 break;
Ben Cahill9fbab512007-11-29 11:09:47 +08004423 case 2: /* 4.9GHz band */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004424 *eeprom_ch_count = ARRAY_SIZE(iwl3945_eeprom_band_2);
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08004425 *eeprom_ch_info = priv->eeprom39.band_2_channels;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004426 *eeprom_ch_index = iwl3945_eeprom_band_2;
Zhu Yib481de92007-09-25 17:54:57 -07004427 break;
4428 case 3: /* 5.2GHz band */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004429 *eeprom_ch_count = ARRAY_SIZE(iwl3945_eeprom_band_3);
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08004430 *eeprom_ch_info = priv->eeprom39.band_3_channels;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004431 *eeprom_ch_index = iwl3945_eeprom_band_3;
Zhu Yib481de92007-09-25 17:54:57 -07004432 break;
Ben Cahill9fbab512007-11-29 11:09:47 +08004433 case 4: /* 5.5GHz band */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004434 *eeprom_ch_count = ARRAY_SIZE(iwl3945_eeprom_band_4);
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08004435 *eeprom_ch_info = priv->eeprom39.band_4_channels;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004436 *eeprom_ch_index = iwl3945_eeprom_band_4;
Zhu Yib481de92007-09-25 17:54:57 -07004437 break;
Ben Cahill9fbab512007-11-29 11:09:47 +08004438 case 5: /* 5.7GHz band */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004439 *eeprom_ch_count = ARRAY_SIZE(iwl3945_eeprom_band_5);
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08004440 *eeprom_ch_info = priv->eeprom39.band_5_channels;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004441 *eeprom_ch_index = iwl3945_eeprom_band_5;
Zhu Yib481de92007-09-25 17:54:57 -07004442 break;
4443 default:
4444 BUG();
4445 return;
4446 }
4447}
4448
Cahill, Ben M6440adb2007-11-29 11:09:55 +08004449/**
4450 * iwl3945_get_channel_info - Find driver's private channel info
4451 *
4452 * Based on band and channel number.
4453 */
Samuel Ortizd20b3c62008-12-19 10:37:15 +08004454const struct iwl_channel_info *
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08004455iwl3945_get_channel_info(const struct iwl_priv *priv,
Samuel Ortizd20b3c62008-12-19 10:37:15 +08004456 enum ieee80211_band band, u16 channel)
Zhu Yib481de92007-09-25 17:54:57 -07004457{
4458 int i;
4459
Johannes Berg8318d782008-01-24 19:38:38 +01004460 switch (band) {
4461 case IEEE80211_BAND_5GHZ:
Zhu Yib481de92007-09-25 17:54:57 -07004462 for (i = 14; i < priv->channel_count; i++) {
4463 if (priv->channel_info[i].channel == channel)
4464 return &priv->channel_info[i];
4465 }
4466 break;
4467
Johannes Berg8318d782008-01-24 19:38:38 +01004468 case IEEE80211_BAND_2GHZ:
Zhu Yib481de92007-09-25 17:54:57 -07004469 if (channel >= 1 && channel <= 14)
4470 return &priv->channel_info[channel - 1];
4471 break;
Johannes Berg8318d782008-01-24 19:38:38 +01004472 case IEEE80211_NUM_BANDS:
4473 WARN_ON(1);
Zhu Yib481de92007-09-25 17:54:57 -07004474 }
4475
4476 return NULL;
4477}
4478
4479#define CHECK_AND_PRINT(x) ((eeprom_ch_info[ch].flags & EEPROM_CHANNEL_##x) \
4480 ? # x " " : "")
4481
Cahill, Ben M6440adb2007-11-29 11:09:55 +08004482/**
4483 * iwl3945_init_channel_map - Set up driver's info for all possible channels
4484 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08004485static int iwl3945_init_channel_map(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004486{
4487 int eeprom_ch_count = 0;
4488 const u8 *eeprom_ch_index = NULL;
Samuel Ortiz0f741d92008-12-19 10:37:10 +08004489 const struct iwl_eeprom_channel *eeprom_ch_info = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07004490 int band, ch;
Samuel Ortizd20b3c62008-12-19 10:37:15 +08004491 struct iwl_channel_info *ch_info;
Zhu Yib481de92007-09-25 17:54:57 -07004492
4493 if (priv->channel_count) {
4494 IWL_DEBUG_INFO("Channel map already initialized.\n");
4495 return 0;
4496 }
4497
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08004498 if (priv->eeprom39.version < 0x2f) {
Winkler, Tomas39aadf82008-12-19 10:37:32 +08004499 IWL_WARN(priv, "Unsupported EEPROM version: 0x%04X\n",
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08004500 priv->eeprom39.version);
Zhu Yib481de92007-09-25 17:54:57 -07004501 return -EINVAL;
4502 }
4503
4504 IWL_DEBUG_INFO("Initializing regulatory info from EEPROM\n");
4505
4506 priv->channel_count =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004507 ARRAY_SIZE(iwl3945_eeprom_band_1) +
4508 ARRAY_SIZE(iwl3945_eeprom_band_2) +
4509 ARRAY_SIZE(iwl3945_eeprom_band_3) +
4510 ARRAY_SIZE(iwl3945_eeprom_band_4) +
4511 ARRAY_SIZE(iwl3945_eeprom_band_5);
Zhu Yib481de92007-09-25 17:54:57 -07004512
4513 IWL_DEBUG_INFO("Parsing data for %d channels.\n", priv->channel_count);
4514
Samuel Ortizd20b3c62008-12-19 10:37:15 +08004515 priv->channel_info = kzalloc(sizeof(struct iwl_channel_info) *
Zhu Yib481de92007-09-25 17:54:57 -07004516 priv->channel_count, GFP_KERNEL);
4517 if (!priv->channel_info) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08004518 IWL_ERR(priv, "Could not allocate channel_info\n");
Zhu Yib481de92007-09-25 17:54:57 -07004519 priv->channel_count = 0;
4520 return -ENOMEM;
4521 }
4522
4523 ch_info = priv->channel_info;
4524
4525 /* Loop through the 5 EEPROM bands adding them in order to the
4526 * channel map we maintain (that contains additional information than
4527 * what just in the EEPROM) */
4528 for (band = 1; band <= 5; band++) {
4529
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004530 iwl3945_init_band_reference(priv, band, &eeprom_ch_count,
Zhu Yib481de92007-09-25 17:54:57 -07004531 &eeprom_ch_info, &eeprom_ch_index);
4532
4533 /* Loop through each band adding each of the channels */
4534 for (ch = 0; ch < eeprom_ch_count; ch++) {
4535 ch_info->channel = eeprom_ch_index[ch];
Johannes Berg8318d782008-01-24 19:38:38 +01004536 ch_info->band = (band == 1) ? IEEE80211_BAND_2GHZ :
4537 IEEE80211_BAND_5GHZ;
Zhu Yib481de92007-09-25 17:54:57 -07004538
4539 /* permanently store EEPROM's channel regulatory flags
4540 * and max power in channel info database. */
4541 ch_info->eeprom = eeprom_ch_info[ch];
4542
4543 /* Copy the run-time flags so they are there even on
4544 * invalid channels */
4545 ch_info->flags = eeprom_ch_info[ch].flags;
4546
4547 if (!(is_channel_valid(ch_info))) {
4548 IWL_DEBUG_INFO("Ch. %d Flags %x [%sGHz] - "
4549 "No traffic\n",
4550 ch_info->channel,
4551 ch_info->flags,
4552 is_channel_a_band(ch_info) ?
4553 "5.2" : "2.4");
4554 ch_info++;
4555 continue;
4556 }
4557
4558 /* Initialize regulatory-based run-time data */
4559 ch_info->max_power_avg = ch_info->curr_txpow =
4560 eeprom_ch_info[ch].max_power_avg;
4561 ch_info->scan_power = eeprom_ch_info[ch].max_power_avg;
4562 ch_info->min_power = 0;
4563
Guy Cohenfe7c4042008-04-21 15:41:56 -07004564 IWL_DEBUG_INFO("Ch. %d [%sGHz] %s%s%s%s%s%s(0x%02x"
Zhu Yib481de92007-09-25 17:54:57 -07004565 " %ddBm): Ad-Hoc %ssupported\n",
4566 ch_info->channel,
4567 is_channel_a_band(ch_info) ?
4568 "5.2" : "2.4",
Tomas Winkler8211ef72008-03-02 01:36:04 +02004569 CHECK_AND_PRINT(VALID),
Zhu Yib481de92007-09-25 17:54:57 -07004570 CHECK_AND_PRINT(IBSS),
4571 CHECK_AND_PRINT(ACTIVE),
4572 CHECK_AND_PRINT(RADAR),
4573 CHECK_AND_PRINT(WIDE),
Zhu Yib481de92007-09-25 17:54:57 -07004574 CHECK_AND_PRINT(DFS),
4575 eeprom_ch_info[ch].flags,
4576 eeprom_ch_info[ch].max_power_avg,
4577 ((eeprom_ch_info[ch].
4578 flags & EEPROM_CHANNEL_IBSS)
4579 && !(eeprom_ch_info[ch].
4580 flags & EEPROM_CHANNEL_RADAR))
4581 ? "" : "not ");
4582
4583 /* Set the user_txpower_limit to the highest power
4584 * supported by any channel */
4585 if (eeprom_ch_info[ch].max_power_avg >
4586 priv->user_txpower_limit)
4587 priv->user_txpower_limit =
4588 eeprom_ch_info[ch].max_power_avg;
4589
4590 ch_info++;
4591 }
4592 }
4593
Cahill, Ben M6440adb2007-11-29 11:09:55 +08004594 /* Set up txpower settings in driver for all channels */
Zhu Yib481de92007-09-25 17:54:57 -07004595 if (iwl3945_txpower_set_from_eeprom(priv))
4596 return -EIO;
4597
4598 return 0;
4599}
4600
Reinette Chatre849e0dc2008-01-23 10:15:18 -08004601/*
4602 * iwl3945_free_channel_map - undo allocations in iwl3945_init_channel_map
4603 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08004604static void iwl3945_free_channel_map(struct iwl_priv *priv)
Reinette Chatre849e0dc2008-01-23 10:15:18 -08004605{
4606 kfree(priv->channel_info);
4607 priv->channel_count = 0;
4608}
4609
Zhu Yib481de92007-09-25 17:54:57 -07004610/* For active scan, listen ACTIVE_DWELL_TIME (msec) on each channel after
4611 * sending probe req. This should be set long enough to hear probe responses
4612 * from more than one AP. */
Abhijeet Kolekarf9340522008-09-03 11:26:58 +08004613#define IWL_ACTIVE_DWELL_TIME_24 (30) /* all times in msec */
4614#define IWL_ACTIVE_DWELL_TIME_52 (20)
4615
4616#define IWL_ACTIVE_DWELL_FACTOR_24GHZ (3)
4617#define IWL_ACTIVE_DWELL_FACTOR_52GHZ (2)
Zhu Yib481de92007-09-25 17:54:57 -07004618
4619/* For faster active scanning, scan will move to the next channel if fewer than
4620 * PLCP_QUIET_THRESH packets are heard on this channel within
4621 * ACTIVE_QUIET_TIME after sending probe request. This shortens the dwell
4622 * time if it's a quiet channel (nothing responded to our probe, and there's
4623 * no other traffic).
4624 * Disable "quiet" feature by setting PLCP_QUIET_THRESH to 0. */
4625#define IWL_PLCP_QUIET_THRESH __constant_cpu_to_le16(1) /* packets */
Abhijeet Kolekarf9340522008-09-03 11:26:58 +08004626#define IWL_ACTIVE_QUIET_TIME __constant_cpu_to_le16(10) /* msec */
Zhu Yib481de92007-09-25 17:54:57 -07004627
4628/* For passive scan, listen PASSIVE_DWELL_TIME (msec) on each channel.
4629 * Must be set longer than active dwell time.
4630 * For the most reliable scan, set > AP beacon interval (typically 100msec). */
4631#define IWL_PASSIVE_DWELL_TIME_24 (20) /* all times in msec */
4632#define IWL_PASSIVE_DWELL_TIME_52 (10)
4633#define IWL_PASSIVE_DWELL_BASE (100)
4634#define IWL_CHANNEL_TUNE_TIME 5
4635
Kolekar, Abhijeete720ce92008-11-07 09:58:42 -08004636#define IWL_SCAN_PROBE_MASK(n) (BIT(n) | (BIT(n) - BIT(1)))
Abhijeet Kolekarf9340522008-09-03 11:26:58 +08004637
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08004638static inline u16 iwl3945_get_active_dwell_time(struct iwl_priv *priv,
Abhijeet Kolekarf9340522008-09-03 11:26:58 +08004639 enum ieee80211_band band,
4640 u8 n_probes)
Zhu Yib481de92007-09-25 17:54:57 -07004641{
Johannes Berg8318d782008-01-24 19:38:38 +01004642 if (band == IEEE80211_BAND_5GHZ)
Abhijeet Kolekarf9340522008-09-03 11:26:58 +08004643 return IWL_ACTIVE_DWELL_TIME_52 +
4644 IWL_ACTIVE_DWELL_FACTOR_52GHZ * (n_probes + 1);
Zhu Yib481de92007-09-25 17:54:57 -07004645 else
Abhijeet Kolekarf9340522008-09-03 11:26:58 +08004646 return IWL_ACTIVE_DWELL_TIME_24 +
4647 IWL_ACTIVE_DWELL_FACTOR_24GHZ * (n_probes + 1);
Zhu Yib481de92007-09-25 17:54:57 -07004648}
4649
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08004650static u16 iwl3945_get_passive_dwell_time(struct iwl_priv *priv,
Johannes Berg8318d782008-01-24 19:38:38 +01004651 enum ieee80211_band band)
Zhu Yib481de92007-09-25 17:54:57 -07004652{
Johannes Berg8318d782008-01-24 19:38:38 +01004653 u16 passive = (band == IEEE80211_BAND_2GHZ) ?
Zhu Yib481de92007-09-25 17:54:57 -07004654 IWL_PASSIVE_DWELL_BASE + IWL_PASSIVE_DWELL_TIME_24 :
4655 IWL_PASSIVE_DWELL_BASE + IWL_PASSIVE_DWELL_TIME_52;
4656
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004657 if (iwl3945_is_associated(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07004658 /* If we're associated, we clamp the maximum passive
4659 * dwell time to be 98% of the beacon interval (minus
4660 * 2 * channel tune time) */
4661 passive = priv->beacon_int;
4662 if ((passive > IWL_PASSIVE_DWELL_BASE) || !passive)
4663 passive = IWL_PASSIVE_DWELL_BASE;
4664 passive = (passive * 98) / 100 - IWL_CHANNEL_TUNE_TIME * 2;
4665 }
4666
Zhu Yib481de92007-09-25 17:54:57 -07004667 return passive;
4668}
4669
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08004670static int iwl3945_get_channels_for_scan(struct iwl_priv *priv,
Johannes Berg8318d782008-01-24 19:38:38 +01004671 enum ieee80211_band band,
Abhijeet Kolekarf9340522008-09-03 11:26:58 +08004672 u8 is_active, u8 n_probes,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004673 struct iwl3945_scan_channel *scan_ch)
Zhu Yib481de92007-09-25 17:54:57 -07004674{
4675 const struct ieee80211_channel *channels = NULL;
Johannes Berg8318d782008-01-24 19:38:38 +01004676 const struct ieee80211_supported_band *sband;
Samuel Ortizd20b3c62008-12-19 10:37:15 +08004677 const struct iwl_channel_info *ch_info;
Zhu Yib481de92007-09-25 17:54:57 -07004678 u16 passive_dwell = 0;
4679 u16 active_dwell = 0;
4680 int added, i;
4681
Johannes Berg8318d782008-01-24 19:38:38 +01004682 sband = iwl3945_get_band(priv, band);
4683 if (!sband)
Zhu Yib481de92007-09-25 17:54:57 -07004684 return 0;
4685
Johannes Berg8318d782008-01-24 19:38:38 +01004686 channels = sband->channels;
Zhu Yib481de92007-09-25 17:54:57 -07004687
Abhijeet Kolekarf9340522008-09-03 11:26:58 +08004688 active_dwell = iwl3945_get_active_dwell_time(priv, band, n_probes);
Johannes Berg8318d782008-01-24 19:38:38 +01004689 passive_dwell = iwl3945_get_passive_dwell_time(priv, band);
Zhu Yib481de92007-09-25 17:54:57 -07004690
Abhijeet Kolekar8f4807a2008-09-03 11:26:31 +08004691 if (passive_dwell <= active_dwell)
4692 passive_dwell = active_dwell + 1;
4693
Johannes Berg8318d782008-01-24 19:38:38 +01004694 for (i = 0, added = 0; i < sband->n_channels; i++) {
Johannes Berg182e2e62008-04-04 10:41:56 +02004695 if (channels[i].flags & IEEE80211_CHAN_DISABLED)
4696 continue;
4697
Johannes Berg8318d782008-01-24 19:38:38 +01004698 scan_ch->channel = channels[i].hw_value;
Zhu Yib481de92007-09-25 17:54:57 -07004699
Johannes Berg8318d782008-01-24 19:38:38 +01004700 ch_info = iwl3945_get_channel_info(priv, band, scan_ch->channel);
Zhu Yib481de92007-09-25 17:54:57 -07004701 if (!is_channel_valid(ch_info)) {
Ron Rindjunsky66b50042008-06-25 16:46:31 +08004702 IWL_DEBUG_SCAN("Channel %d is INVALID for this band.\n",
Zhu Yib481de92007-09-25 17:54:57 -07004703 scan_ch->channel);
4704 continue;
4705 }
4706
Zhu Yib481de92007-09-25 17:54:57 -07004707 scan_ch->active_dwell = cpu_to_le16(active_dwell);
4708 scan_ch->passive_dwell = cpu_to_le16(passive_dwell);
Abhijeet Kolekar011a0332008-12-02 12:14:07 -08004709 /* If passive , set up for auto-switch
4710 * and use long active_dwell time.
4711 */
4712 if (!is_active || is_channel_passive(ch_info) ||
4713 (channels[i].flags & IEEE80211_CHAN_PASSIVE_SCAN)) {
4714 scan_ch->type = 0; /* passive */
4715 if (IWL_UCODE_API(priv->ucode_ver) == 1)
4716 scan_ch->active_dwell = cpu_to_le16(passive_dwell - 1);
4717 } else {
4718 scan_ch->type = 1; /* active */
4719 }
4720
4721 /* Set direct probe bits. These may be used both for active
4722 * scan channels (probes gets sent right away),
4723 * or for passive channels (probes get se sent only after
4724 * hearing clear Rx packet).*/
4725 if (IWL_UCODE_API(priv->ucode_ver) >= 2) {
4726 if (n_probes)
4727 scan_ch->type |= IWL_SCAN_PROBE_MASK(n_probes);
4728 } else {
4729 /* uCode v1 does not allow setting direct probe bits on
4730 * passive channel. */
4731 if ((scan_ch->type & 1) && n_probes)
4732 scan_ch->type |= IWL_SCAN_PROBE_MASK(n_probes);
4733 }
Zhu Yib481de92007-09-25 17:54:57 -07004734
Ben Cahill9fbab512007-11-29 11:09:47 +08004735 /* Set txpower levels to defaults */
Zhu Yib481de92007-09-25 17:54:57 -07004736 scan_ch->tpc.dsp_atten = 110;
4737 /* scan_pwr_info->tpc.dsp_atten; */
4738
4739 /*scan_pwr_info->tpc.tx_gain; */
Johannes Berg8318d782008-01-24 19:38:38 +01004740 if (band == IEEE80211_BAND_5GHZ)
Zhu Yib481de92007-09-25 17:54:57 -07004741 scan_ch->tpc.tx_gain = ((1 << 5) | (3 << 3)) | 3;
4742 else {
4743 scan_ch->tpc.tx_gain = ((1 << 5) | (5 << 3));
4744 /* NOTE: if we were doing 6Mb OFDM for scans we'd use
Ben Cahill9fbab512007-11-29 11:09:47 +08004745 * power level:
Reinette Chatre8a1b0242008-01-14 17:46:25 -08004746 * scan_ch->tpc.tx_gain = ((1 << 5) | (2 << 3)) | 3;
Zhu Yib481de92007-09-25 17:54:57 -07004747 */
4748 }
4749
4750 IWL_DEBUG_SCAN("Scanning %d [%s %d]\n",
4751 scan_ch->channel,
4752 (scan_ch->type & 1) ? "ACTIVE" : "PASSIVE",
4753 (scan_ch->type & 1) ?
4754 active_dwell : passive_dwell);
4755
4756 scan_ch++;
4757 added++;
4758 }
4759
4760 IWL_DEBUG_SCAN("total channels to scan %d \n", added);
4761 return added;
4762}
4763
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08004764static void iwl3945_init_hw_rates(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07004765 struct ieee80211_rate *rates)
4766{
4767 int i;
4768
4769 for (i = 0; i < IWL_RATE_COUNT; i++) {
Johannes Berg8318d782008-01-24 19:38:38 +01004770 rates[i].bitrate = iwl3945_rates[i].ieee * 5;
4771 rates[i].hw_value = i; /* Rate scaling will work on indexes */
4772 rates[i].hw_value_short = i;
4773 rates[i].flags = 0;
Samuel Ortizd9829a62008-12-19 10:37:12 +08004774 if ((i > IWL39_LAST_OFDM_RATE) || (i < IWL_FIRST_OFDM_RATE)) {
Zhu Yib481de92007-09-25 17:54:57 -07004775 /*
Johannes Berg8318d782008-01-24 19:38:38 +01004776 * If CCK != 1M then set short preamble rate flag.
Zhu Yib481de92007-09-25 17:54:57 -07004777 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004778 rates[i].flags |= (iwl3945_rates[i].plcp == 10) ?
Johannes Berg8318d782008-01-24 19:38:38 +01004779 0 : IEEE80211_RATE_SHORT_PREAMBLE;
Zhu Yib481de92007-09-25 17:54:57 -07004780 }
Zhu Yib481de92007-09-25 17:54:57 -07004781 }
4782}
4783
4784/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004785 * iwl3945_init_geos - Initialize mac80211's geo/channel info based from eeprom
Zhu Yib481de92007-09-25 17:54:57 -07004786 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08004787static int iwl3945_init_geos(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004788{
Samuel Ortizd20b3c62008-12-19 10:37:15 +08004789 struct iwl_channel_info *ch;
Tomas Winkler8211ef72008-03-02 01:36:04 +02004790 struct ieee80211_supported_band *sband;
Zhu Yib481de92007-09-25 17:54:57 -07004791 struct ieee80211_channel *channels;
4792 struct ieee80211_channel *geo_ch;
4793 struct ieee80211_rate *rates;
4794 int i = 0;
Zhu Yib481de92007-09-25 17:54:57 -07004795
Johannes Berg8318d782008-01-24 19:38:38 +01004796 if (priv->bands[IEEE80211_BAND_2GHZ].n_bitrates ||
4797 priv->bands[IEEE80211_BAND_5GHZ].n_bitrates) {
Zhu Yib481de92007-09-25 17:54:57 -07004798 IWL_DEBUG_INFO("Geography modes already initialized.\n");
4799 set_bit(STATUS_GEO_CONFIGURED, &priv->status);
4800 return 0;
4801 }
4802
Zhu Yib481de92007-09-25 17:54:57 -07004803 channels = kzalloc(sizeof(struct ieee80211_channel) *
4804 priv->channel_count, GFP_KERNEL);
Johannes Berg8318d782008-01-24 19:38:38 +01004805 if (!channels)
Zhu Yib481de92007-09-25 17:54:57 -07004806 return -ENOMEM;
Zhu Yib481de92007-09-25 17:54:57 -07004807
Tomas Winkler8211ef72008-03-02 01:36:04 +02004808 rates = kzalloc((sizeof(struct ieee80211_rate) * (IWL_RATE_COUNT + 1)),
Zhu Yib481de92007-09-25 17:54:57 -07004809 GFP_KERNEL);
4810 if (!rates) {
Zhu Yib481de92007-09-25 17:54:57 -07004811 kfree(channels);
4812 return -ENOMEM;
4813 }
4814
Zhu Yib481de92007-09-25 17:54:57 -07004815 /* 5.2GHz channels start after the 2.4GHz channels */
Tomas Winkler8211ef72008-03-02 01:36:04 +02004816 sband = &priv->bands[IEEE80211_BAND_5GHZ];
4817 sband->channels = &channels[ARRAY_SIZE(iwl3945_eeprom_band_1)];
4818 /* just OFDM */
4819 sband->bitrates = &rates[IWL_FIRST_OFDM_RATE];
4820 sband->n_bitrates = IWL_RATE_COUNT - IWL_FIRST_OFDM_RATE;
Zhu Yib481de92007-09-25 17:54:57 -07004821
Tomas Winkler8211ef72008-03-02 01:36:04 +02004822 sband = &priv->bands[IEEE80211_BAND_2GHZ];
4823 sband->channels = channels;
4824 /* OFDM & CCK */
4825 sband->bitrates = rates;
4826 sband->n_bitrates = IWL_RATE_COUNT;
Zhu Yib481de92007-09-25 17:54:57 -07004827
4828 priv->ieee_channels = channels;
4829 priv->ieee_rates = rates;
4830
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004831 iwl3945_init_hw_rates(priv, rates);
Zhu Yib481de92007-09-25 17:54:57 -07004832
Tomas Winkler8211ef72008-03-02 01:36:04 +02004833 for (i = 0; i < priv->channel_count; i++) {
Zhu Yib481de92007-09-25 17:54:57 -07004834 ch = &priv->channel_info[i];
4835
Tomas Winkler8211ef72008-03-02 01:36:04 +02004836 /* FIXME: might be removed if scan is OK*/
4837 if (!is_channel_valid(ch))
Zhu Yib481de92007-09-25 17:54:57 -07004838 continue;
Zhu Yib481de92007-09-25 17:54:57 -07004839
4840 if (is_channel_a_band(ch))
Tomas Winkler8211ef72008-03-02 01:36:04 +02004841 sband = &priv->bands[IEEE80211_BAND_5GHZ];
Johannes Berg8318d782008-01-24 19:38:38 +01004842 else
Tomas Winkler8211ef72008-03-02 01:36:04 +02004843 sband = &priv->bands[IEEE80211_BAND_2GHZ];
Zhu Yib481de92007-09-25 17:54:57 -07004844
Tomas Winkler8211ef72008-03-02 01:36:04 +02004845 geo_ch = &sband->channels[sband->n_channels++];
4846
4847 geo_ch->center_freq = ieee80211_channel_to_frequency(ch->channel);
Johannes Berg8318d782008-01-24 19:38:38 +01004848 geo_ch->max_power = ch->max_power_avg;
4849 geo_ch->max_antenna_gain = 0xff;
Mohamed Abbas7b723042008-01-31 21:46:40 -08004850 geo_ch->hw_value = ch->channel;
Zhu Yib481de92007-09-25 17:54:57 -07004851
4852 if (is_channel_valid(ch)) {
Johannes Berg8318d782008-01-24 19:38:38 +01004853 if (!(ch->flags & EEPROM_CHANNEL_IBSS))
4854 geo_ch->flags |= IEEE80211_CHAN_NO_IBSS;
Zhu Yib481de92007-09-25 17:54:57 -07004855
Johannes Berg8318d782008-01-24 19:38:38 +01004856 if (!(ch->flags & EEPROM_CHANNEL_ACTIVE))
4857 geo_ch->flags |= IEEE80211_CHAN_PASSIVE_SCAN;
Zhu Yib481de92007-09-25 17:54:57 -07004858
4859 if (ch->flags & EEPROM_CHANNEL_RADAR)
Johannes Berg8318d782008-01-24 19:38:38 +01004860 geo_ch->flags |= IEEE80211_CHAN_RADAR;
Zhu Yib481de92007-09-25 17:54:57 -07004861
4862 if (ch->max_power_avg > priv->max_channel_txpower_limit)
4863 priv->max_channel_txpower_limit =
4864 ch->max_power_avg;
Tomas Winkler8211ef72008-03-02 01:36:04 +02004865 } else {
Johannes Berg8318d782008-01-24 19:38:38 +01004866 geo_ch->flags |= IEEE80211_CHAN_DISABLED;
Tomas Winkler8211ef72008-03-02 01:36:04 +02004867 }
4868
4869 /* Save flags for reg domain usage */
4870 geo_ch->orig_flags = geo_ch->flags;
4871
4872 IWL_DEBUG_INFO("Channel %d Freq=%d[%sGHz] %s flag=0%X\n",
4873 ch->channel, geo_ch->center_freq,
4874 is_channel_a_band(ch) ? "5.2" : "2.4",
4875 geo_ch->flags & IEEE80211_CHAN_DISABLED ?
4876 "restricted" : "valid",
4877 geo_ch->flags);
Zhu Yib481de92007-09-25 17:54:57 -07004878 }
4879
Tomas Winkler82b9a122008-03-04 18:09:30 -08004880 if ((priv->bands[IEEE80211_BAND_5GHZ].n_channels == 0) &&
4881 priv->cfg->sku & IWL_SKU_A) {
Tomas Winkler978785a2008-12-19 10:37:31 +08004882 IWL_INFO(priv, "Incorrectly detected BG card as ABG. "
4883 "Please send your PCI ID 0x%04X:0x%04X to maintainer.\n",
4884 priv->pci_dev->device, priv->pci_dev->subsystem_device);
Tomas Winkler82b9a122008-03-04 18:09:30 -08004885 priv->cfg->sku &= ~IWL_SKU_A;
Zhu Yib481de92007-09-25 17:54:57 -07004886 }
4887
Tomas Winkler978785a2008-12-19 10:37:31 +08004888 IWL_INFO(priv, "Tunable channels: %d 802.11bg, %d 802.11a channels\n",
Johannes Berg8318d782008-01-24 19:38:38 +01004889 priv->bands[IEEE80211_BAND_2GHZ].n_channels,
4890 priv->bands[IEEE80211_BAND_5GHZ].n_channels);
Zhu Yib481de92007-09-25 17:54:57 -07004891
John W. Linvillee0e0a672008-03-25 15:58:40 -04004892 if (priv->bands[IEEE80211_BAND_2GHZ].n_channels)
4893 priv->hw->wiphy->bands[IEEE80211_BAND_2GHZ] =
4894 &priv->bands[IEEE80211_BAND_2GHZ];
4895 if (priv->bands[IEEE80211_BAND_5GHZ].n_channels)
4896 priv->hw->wiphy->bands[IEEE80211_BAND_5GHZ] =
4897 &priv->bands[IEEE80211_BAND_5GHZ];
Zhu Yib481de92007-09-25 17:54:57 -07004898
Zhu Yib481de92007-09-25 17:54:57 -07004899 set_bit(STATUS_GEO_CONFIGURED, &priv->status);
4900
4901 return 0;
4902}
4903
Reinette Chatre849e0dc2008-01-23 10:15:18 -08004904/*
4905 * iwl3945_free_geos - undo allocations in iwl3945_init_geos
4906 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08004907static void iwl3945_free_geos(struct iwl_priv *priv)
Reinette Chatre849e0dc2008-01-23 10:15:18 -08004908{
Reinette Chatre849e0dc2008-01-23 10:15:18 -08004909 kfree(priv->ieee_channels);
4910 kfree(priv->ieee_rates);
4911 clear_bit(STATUS_GEO_CONFIGURED, &priv->status);
4912}
4913
Zhu Yib481de92007-09-25 17:54:57 -07004914/******************************************************************************
4915 *
4916 * uCode download functions
4917 *
4918 ******************************************************************************/
4919
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08004920static void iwl3945_dealloc_ucode_pci(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004921{
Tomas Winkler98c92212008-01-14 17:46:20 -08004922 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_code);
4923 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_data);
4924 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_data_backup);
4925 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_init);
4926 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_init_data);
4927 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_boot);
Zhu Yib481de92007-09-25 17:54:57 -07004928}
4929
4930/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004931 * iwl3945_verify_inst_full - verify runtime uCode image in card vs. host,
Zhu Yib481de92007-09-25 17:54:57 -07004932 * looking at all data.
4933 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08004934static int iwl3945_verify_inst_full(struct iwl_priv *priv, __le32 *image, u32 len)
Zhu Yib481de92007-09-25 17:54:57 -07004935{
4936 u32 val;
4937 u32 save_len = len;
4938 int rc = 0;
4939 u32 errcnt;
4940
4941 IWL_DEBUG_INFO("ucode inst image size is %u\n", len);
4942
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08004943 rc = iwl_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004944 if (rc)
4945 return rc;
4946
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08004947 iwl_write_direct32(priv, HBUS_TARG_MEM_RADDR,
Samuel Ortiz250bdd22008-12-19 10:37:11 +08004948 IWL39_RTC_INST_LOWER_BOUND);
Zhu Yib481de92007-09-25 17:54:57 -07004949
4950 errcnt = 0;
4951 for (; len > 0; len -= sizeof(u32), image++) {
4952 /* read data comes through single port, auto-incr addr */
4953 /* NOTE: Use the debugless read so we don't flood kernel log
4954 * if IWL_DL_IO is set */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08004955 val = _iwl_read_direct32(priv, HBUS_TARG_MEM_RDAT);
Zhu Yib481de92007-09-25 17:54:57 -07004956 if (val != le32_to_cpu(*image)) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08004957 IWL_ERR(priv, "uCode INST section is invalid at "
Zhu Yib481de92007-09-25 17:54:57 -07004958 "offset 0x%x, is 0x%x, s/b 0x%x\n",
4959 save_len - len, val, le32_to_cpu(*image));
4960 rc = -EIO;
4961 errcnt++;
4962 if (errcnt >= 20)
4963 break;
4964 }
4965 }
4966
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08004967 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004968
4969 if (!errcnt)
Ian Schrambc434dd2007-10-25 17:15:29 +08004970 IWL_DEBUG_INFO("ucode image in INSTRUCTION memory is good\n");
Zhu Yib481de92007-09-25 17:54:57 -07004971
4972 return rc;
4973}
4974
4975
4976/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004977 * iwl3945_verify_inst_sparse - verify runtime uCode image in card vs. host,
Zhu Yib481de92007-09-25 17:54:57 -07004978 * using sample data 100 bytes apart. If these sample points are good,
4979 * it's a pretty good bet that everything between them is good, too.
4980 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08004981static int iwl3945_verify_inst_sparse(struct iwl_priv *priv, __le32 *image, u32 len)
Zhu Yib481de92007-09-25 17:54:57 -07004982{
4983 u32 val;
4984 int rc = 0;
4985 u32 errcnt = 0;
4986 u32 i;
4987
4988 IWL_DEBUG_INFO("ucode inst image size is %u\n", len);
4989
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08004990 rc = iwl_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004991 if (rc)
4992 return rc;
4993
4994 for (i = 0; i < len; i += 100, image += 100/sizeof(u32)) {
4995 /* read data comes through single port, auto-incr addr */
4996 /* NOTE: Use the debugless read so we don't flood kernel log
4997 * if IWL_DL_IO is set */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08004998 iwl_write_direct32(priv, HBUS_TARG_MEM_RADDR,
Samuel Ortiz250bdd22008-12-19 10:37:11 +08004999 i + IWL39_RTC_INST_LOWER_BOUND);
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08005000 val = _iwl_read_direct32(priv, HBUS_TARG_MEM_RDAT);
Zhu Yib481de92007-09-25 17:54:57 -07005001 if (val != le32_to_cpu(*image)) {
5002#if 0 /* Enable this if you want to see details */
Winkler, Tomas15b16872008-12-19 10:37:33 +08005003 IWL_ERR(priv, "uCode INST section is invalid at "
Zhu Yib481de92007-09-25 17:54:57 -07005004 "offset 0x%x, is 0x%x, s/b 0x%x\n",
5005 i, val, *image);
5006#endif
5007 rc = -EIO;
5008 errcnt++;
5009 if (errcnt >= 3)
5010 break;
5011 }
5012 }
5013
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08005014 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005015
5016 return rc;
5017}
5018
5019
5020/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005021 * iwl3945_verify_ucode - determine which instruction image is in SRAM,
Zhu Yib481de92007-09-25 17:54:57 -07005022 * and verify its contents
5023 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08005024static int iwl3945_verify_ucode(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005025{
5026 __le32 *image;
5027 u32 len;
5028 int rc = 0;
5029
5030 /* Try bootstrap */
5031 image = (__le32 *)priv->ucode_boot.v_addr;
5032 len = priv->ucode_boot.len;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005033 rc = iwl3945_verify_inst_sparse(priv, image, len);
Zhu Yib481de92007-09-25 17:54:57 -07005034 if (rc == 0) {
5035 IWL_DEBUG_INFO("Bootstrap uCode is good in inst SRAM\n");
5036 return 0;
5037 }
5038
5039 /* Try initialize */
5040 image = (__le32 *)priv->ucode_init.v_addr;
5041 len = priv->ucode_init.len;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005042 rc = iwl3945_verify_inst_sparse(priv, image, len);
Zhu Yib481de92007-09-25 17:54:57 -07005043 if (rc == 0) {
5044 IWL_DEBUG_INFO("Initialize uCode is good in inst SRAM\n");
5045 return 0;
5046 }
5047
5048 /* Try runtime/protocol */
5049 image = (__le32 *)priv->ucode_code.v_addr;
5050 len = priv->ucode_code.len;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005051 rc = iwl3945_verify_inst_sparse(priv, image, len);
Zhu Yib481de92007-09-25 17:54:57 -07005052 if (rc == 0) {
5053 IWL_DEBUG_INFO("Runtime uCode is good in inst SRAM\n");
5054 return 0;
5055 }
5056
Winkler, Tomas15b16872008-12-19 10:37:33 +08005057 IWL_ERR(priv, "NO VALID UCODE IMAGE IN INSTRUCTION SRAM!!\n");
Zhu Yib481de92007-09-25 17:54:57 -07005058
Ben Cahill9fbab512007-11-29 11:09:47 +08005059 /* Since nothing seems to match, show first several data entries in
5060 * instruction SRAM, so maybe visual inspection will give a clue.
5061 * Selection of bootstrap image (vs. other images) is arbitrary. */
Zhu Yib481de92007-09-25 17:54:57 -07005062 image = (__le32 *)priv->ucode_boot.v_addr;
5063 len = priv->ucode_boot.len;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005064 rc = iwl3945_verify_inst_full(priv, image, len);
Zhu Yib481de92007-09-25 17:54:57 -07005065
5066 return rc;
5067}
5068
5069
5070/* check contents of special bootstrap uCode SRAM */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08005071static int iwl3945_verify_bsm(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005072{
5073 __le32 *image = priv->ucode_boot.v_addr;
5074 u32 len = priv->ucode_boot.len;
5075 u32 reg;
5076 u32 val;
5077
5078 IWL_DEBUG_INFO("Begin verify bsm\n");
5079
5080 /* verify BSM SRAM contents */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08005081 val = iwl_read_prph(priv, BSM_WR_DWCOUNT_REG);
Zhu Yib481de92007-09-25 17:54:57 -07005082 for (reg = BSM_SRAM_LOWER_BOUND;
5083 reg < BSM_SRAM_LOWER_BOUND + len;
Tomas Winkler3ac7f142008-07-21 02:40:14 +03005084 reg += sizeof(u32), image++) {
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08005085 val = iwl_read_prph(priv, reg);
Zhu Yib481de92007-09-25 17:54:57 -07005086 if (val != le32_to_cpu(*image)) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08005087 IWL_ERR(priv, "BSM uCode verification failed at "
Zhu Yib481de92007-09-25 17:54:57 -07005088 "addr 0x%08X+%u (of %u), is 0x%x, s/b 0x%x\n",
5089 BSM_SRAM_LOWER_BOUND,
5090 reg - BSM_SRAM_LOWER_BOUND, len,
5091 val, le32_to_cpu(*image));
5092 return -EIO;
5093 }
5094 }
5095
5096 IWL_DEBUG_INFO("BSM bootstrap uCode image OK\n");
5097
5098 return 0;
5099}
5100
5101/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005102 * iwl3945_load_bsm - Load bootstrap instructions
Zhu Yib481de92007-09-25 17:54:57 -07005103 *
5104 * BSM operation:
5105 *
5106 * The Bootstrap State Machine (BSM) stores a short bootstrap uCode program
5107 * in special SRAM that does not power down during RFKILL. When powering back
5108 * up after power-saving sleeps (or during initial uCode load), the BSM loads
5109 * the bootstrap program into the on-board processor, and starts it.
5110 *
5111 * The bootstrap program loads (via DMA) instructions and data for a new
5112 * program from host DRAM locations indicated by the host driver in the
5113 * BSM_DRAM_* registers. Once the new program is loaded, it starts
5114 * automatically.
5115 *
5116 * When initializing the NIC, the host driver points the BSM to the
5117 * "initialize" uCode image. This uCode sets up some internal data, then
5118 * notifies host via "initialize alive" that it is complete.
5119 *
5120 * The host then replaces the BSM_DRAM_* pointer values to point to the
5121 * normal runtime uCode instructions and a backup uCode data cache buffer
5122 * (filled initially with starting data values for the on-board processor),
5123 * then triggers the "initialize" uCode to load and launch the runtime uCode,
5124 * which begins normal operation.
5125 *
5126 * When doing a power-save shutdown, runtime uCode saves data SRAM into
5127 * the backup data cache in DRAM before SRAM is powered down.
5128 *
5129 * When powering back up, the BSM loads the bootstrap program. This reloads
5130 * the runtime uCode instructions and the backup data cache into SRAM,
5131 * and re-launches the runtime uCode from where it left off.
5132 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08005133static int iwl3945_load_bsm(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005134{
5135 __le32 *image = priv->ucode_boot.v_addr;
5136 u32 len = priv->ucode_boot.len;
5137 dma_addr_t pinst;
5138 dma_addr_t pdata;
5139 u32 inst_len;
5140 u32 data_len;
5141 int rc;
5142 int i;
5143 u32 done;
5144 u32 reg_offset;
5145
5146 IWL_DEBUG_INFO("Begin load bsm\n");
5147
5148 /* make sure bootstrap program is no larger than BSM's SRAM size */
Samuel Ortiz250bdd22008-12-19 10:37:11 +08005149 if (len > IWL39_MAX_BSM_SIZE)
Zhu Yib481de92007-09-25 17:54:57 -07005150 return -EINVAL;
5151
5152 /* Tell bootstrap uCode where to find the "Initialize" uCode
Ben Cahill9fbab512007-11-29 11:09:47 +08005153 * in host DRAM ... host DRAM physical address bits 31:0 for 3945.
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005154 * NOTE: iwl3945_initialize_alive_start() will replace these values,
Zhu Yib481de92007-09-25 17:54:57 -07005155 * after the "initialize" uCode has run, to point to
5156 * runtime/protocol instructions and backup data cache. */
5157 pinst = priv->ucode_init.p_addr;
5158 pdata = priv->ucode_init_data.p_addr;
5159 inst_len = priv->ucode_init.len;
5160 data_len = priv->ucode_init_data.len;
5161
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08005162 rc = iwl_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005163 if (rc)
5164 return rc;
5165
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08005166 iwl_write_prph(priv, BSM_DRAM_INST_PTR_REG, pinst);
5167 iwl_write_prph(priv, BSM_DRAM_DATA_PTR_REG, pdata);
5168 iwl_write_prph(priv, BSM_DRAM_INST_BYTECOUNT_REG, inst_len);
5169 iwl_write_prph(priv, BSM_DRAM_DATA_BYTECOUNT_REG, data_len);
Zhu Yib481de92007-09-25 17:54:57 -07005170
5171 /* Fill BSM memory with bootstrap instructions */
5172 for (reg_offset = BSM_SRAM_LOWER_BOUND;
5173 reg_offset < BSM_SRAM_LOWER_BOUND + len;
5174 reg_offset += sizeof(u32), image++)
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08005175 _iwl_write_prph(priv, reg_offset,
Zhu Yib481de92007-09-25 17:54:57 -07005176 le32_to_cpu(*image));
5177
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005178 rc = iwl3945_verify_bsm(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005179 if (rc) {
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08005180 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005181 return rc;
5182 }
5183
5184 /* Tell BSM to copy from BSM SRAM into instruction SRAM, when asked */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08005185 iwl_write_prph(priv, BSM_WR_MEM_SRC_REG, 0x0);
5186 iwl_write_prph(priv, BSM_WR_MEM_DST_REG,
Samuel Ortiz250bdd22008-12-19 10:37:11 +08005187 IWL39_RTC_INST_LOWER_BOUND);
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08005188 iwl_write_prph(priv, BSM_WR_DWCOUNT_REG, len / sizeof(u32));
Zhu Yib481de92007-09-25 17:54:57 -07005189
5190 /* Load bootstrap code into instruction SRAM now,
5191 * to prepare to load "initialize" uCode */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08005192 iwl_write_prph(priv, BSM_WR_CTRL_REG,
Zhu Yib481de92007-09-25 17:54:57 -07005193 BSM_WR_CTRL_REG_BIT_START);
5194
5195 /* Wait for load of bootstrap uCode to finish */
5196 for (i = 0; i < 100; i++) {
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08005197 done = iwl_read_prph(priv, BSM_WR_CTRL_REG);
Zhu Yib481de92007-09-25 17:54:57 -07005198 if (!(done & BSM_WR_CTRL_REG_BIT_START))
5199 break;
5200 udelay(10);
5201 }
5202 if (i < 100)
5203 IWL_DEBUG_INFO("BSM write complete, poll %d iterations\n", i);
5204 else {
Winkler, Tomas15b16872008-12-19 10:37:33 +08005205 IWL_ERR(priv, "BSM write did not complete!\n");
Zhu Yib481de92007-09-25 17:54:57 -07005206 return -EIO;
5207 }
5208
5209 /* Enable future boot loads whenever power management unit triggers it
5210 * (e.g. when powering back up after power-save shutdown) */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08005211 iwl_write_prph(priv, BSM_WR_CTRL_REG,
Zhu Yib481de92007-09-25 17:54:57 -07005212 BSM_WR_CTRL_REG_BIT_START_EN);
5213
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08005214 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005215
5216 return 0;
5217}
5218
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08005219static void iwl3945_nic_start(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005220{
5221 /* Remove all resets to allow NIC to operate */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08005222 iwl_write32(priv, CSR_RESET, 0);
Zhu Yib481de92007-09-25 17:54:57 -07005223}
5224
5225/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005226 * iwl3945_read_ucode - Read uCode images from disk file.
Zhu Yib481de92007-09-25 17:54:57 -07005227 *
5228 * Copy into buffers for card to fetch via bus-mastering
5229 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08005230static int iwl3945_read_ucode(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005231{
Kolekar, Abhijeeta78fe752008-12-19 10:37:21 +08005232 struct iwl_ucode *ucode;
Reinette Chatrea0987a82008-12-02 12:14:06 -08005233 int ret = -EINVAL, index;
Zhu Yib481de92007-09-25 17:54:57 -07005234 const struct firmware *ucode_raw;
5235 /* firmware file name contains uCode/driver compatibility version */
Reinette Chatrea0987a82008-12-02 12:14:06 -08005236 const char *name_pre = priv->cfg->fw_name_pre;
5237 const unsigned int api_max = priv->cfg->ucode_api_max;
5238 const unsigned int api_min = priv->cfg->ucode_api_min;
5239 char buf[25];
Zhu Yib481de92007-09-25 17:54:57 -07005240 u8 *src;
5241 size_t len;
Reinette Chatrea0987a82008-12-02 12:14:06 -08005242 u32 api_ver, inst_size, data_size, init_size, init_data_size, boot_size;
Zhu Yib481de92007-09-25 17:54:57 -07005243
5244 /* Ask kernel firmware_class module to get the boot firmware off disk.
5245 * request_firmware() is synchronous, file is in memory on return. */
Reinette Chatrea0987a82008-12-02 12:14:06 -08005246 for (index = api_max; index >= api_min; index--) {
5247 sprintf(buf, "%s%u%s", name_pre, index, ".ucode");
5248 ret = request_firmware(&ucode_raw, buf, &priv->pci_dev->dev);
5249 if (ret < 0) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08005250 IWL_ERR(priv, "%s firmware file req failed: %d\n",
Reinette Chatrea0987a82008-12-02 12:14:06 -08005251 buf, ret);
5252 if (ret == -ENOENT)
5253 continue;
5254 else
5255 goto error;
5256 } else {
5257 if (index < api_max)
Winkler, Tomas15b16872008-12-19 10:37:33 +08005258 IWL_ERR(priv, "Loaded firmware %s, "
5259 "which is deprecated. "
5260 " Please use API v%u instead.\n",
Reinette Chatrea0987a82008-12-02 12:14:06 -08005261 buf, api_max);
5262 IWL_DEBUG_INFO("Got firmware '%s' file (%zd bytes) from disk\n",
5263 buf, ucode_raw->size);
5264 break;
5265 }
Zhu Yib481de92007-09-25 17:54:57 -07005266 }
5267
Reinette Chatrea0987a82008-12-02 12:14:06 -08005268 if (ret < 0)
5269 goto error;
Zhu Yib481de92007-09-25 17:54:57 -07005270
5271 /* Make sure that we got at least our header! */
5272 if (ucode_raw->size < sizeof(*ucode)) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08005273 IWL_ERR(priv, "File size way too small!\n");
Tomas Winkler90e759d2007-11-29 11:09:41 +08005274 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07005275 goto err_release;
5276 }
5277
5278 /* Data from ucode file: header followed by uCode images */
5279 ucode = (void *)ucode_raw->data;
5280
Chatre, Reinettec02b3ac2008-12-02 12:14:05 -08005281 priv->ucode_ver = le32_to_cpu(ucode->ver);
Reinette Chatrea0987a82008-12-02 12:14:06 -08005282 api_ver = IWL_UCODE_API(priv->ucode_ver);
Zhu Yib481de92007-09-25 17:54:57 -07005283 inst_size = le32_to_cpu(ucode->inst_size);
5284 data_size = le32_to_cpu(ucode->data_size);
5285 init_size = le32_to_cpu(ucode->init_size);
5286 init_data_size = le32_to_cpu(ucode->init_data_size);
5287 boot_size = le32_to_cpu(ucode->boot_size);
5288
Reinette Chatrea0987a82008-12-02 12:14:06 -08005289 /* api_ver should match the api version forming part of the
5290 * firmware filename ... but we don't check for that and only rely
5291 * on the API version read from firware header from here on forward */
5292
5293 if (api_ver < api_min || api_ver > api_max) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08005294 IWL_ERR(priv, "Driver unable to support your firmware API. "
Reinette Chatrea0987a82008-12-02 12:14:06 -08005295 "Driver supports v%u, firmware is v%u.\n",
5296 api_max, api_ver);
5297 priv->ucode_ver = 0;
5298 ret = -EINVAL;
5299 goto err_release;
5300 }
5301 if (api_ver != api_max)
Winkler, Tomas15b16872008-12-19 10:37:33 +08005302 IWL_ERR(priv, "Firmware has old API version. Expected %u, "
Reinette Chatrea0987a82008-12-02 12:14:06 -08005303 "got %u. New firmware can be obtained "
5304 "from http://www.intellinuxwireless.org.\n",
5305 api_max, api_ver);
5306
Tomas Winkler978785a2008-12-19 10:37:31 +08005307 IWL_INFO(priv, "loaded firmware version %u.%u.%u.%u\n",
5308 IWL_UCODE_MAJOR(priv->ucode_ver),
5309 IWL_UCODE_MINOR(priv->ucode_ver),
5310 IWL_UCODE_API(priv->ucode_ver),
5311 IWL_UCODE_SERIAL(priv->ucode_ver));
5312
Reinette Chatrea0987a82008-12-02 12:14:06 -08005313 IWL_DEBUG_INFO("f/w package hdr ucode version raw = 0x%x\n",
5314 priv->ucode_ver);
Ian Schrambc434dd2007-10-25 17:15:29 +08005315 IWL_DEBUG_INFO("f/w package hdr runtime inst size = %u\n", inst_size);
5316 IWL_DEBUG_INFO("f/w package hdr runtime data size = %u\n", data_size);
5317 IWL_DEBUG_INFO("f/w package hdr init inst size = %u\n", init_size);
5318 IWL_DEBUG_INFO("f/w package hdr init data size = %u\n", init_data_size);
5319 IWL_DEBUG_INFO("f/w package hdr boot inst size = %u\n", boot_size);
Zhu Yib481de92007-09-25 17:54:57 -07005320
Reinette Chatrea0987a82008-12-02 12:14:06 -08005321
Zhu Yib481de92007-09-25 17:54:57 -07005322 /* Verify size of file vs. image size info in file's header */
5323 if (ucode_raw->size < sizeof(*ucode) +
5324 inst_size + data_size + init_size +
5325 init_data_size + boot_size) {
5326
5327 IWL_DEBUG_INFO("uCode file size %d too small\n",
5328 (int)ucode_raw->size);
Tomas Winkler90e759d2007-11-29 11:09:41 +08005329 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07005330 goto err_release;
5331 }
5332
5333 /* Verify that uCode images will fit in card's SRAM */
Samuel Ortiz250bdd22008-12-19 10:37:11 +08005334 if (inst_size > IWL39_MAX_INST_SIZE) {
Tomas Winkler90e759d2007-11-29 11:09:41 +08005335 IWL_DEBUG_INFO("uCode instr len %d too large to fit in\n",
5336 inst_size);
5337 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07005338 goto err_release;
5339 }
5340
Samuel Ortiz250bdd22008-12-19 10:37:11 +08005341 if (data_size > IWL39_MAX_DATA_SIZE) {
Tomas Winkler90e759d2007-11-29 11:09:41 +08005342 IWL_DEBUG_INFO("uCode data len %d too large to fit in\n",
5343 data_size);
5344 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07005345 goto err_release;
5346 }
Samuel Ortiz250bdd22008-12-19 10:37:11 +08005347 if (init_size > IWL39_MAX_INST_SIZE) {
Tomas Winkler90e759d2007-11-29 11:09:41 +08005348 IWL_DEBUG_INFO("uCode init instr len %d too large to fit in\n",
5349 init_size);
5350 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07005351 goto err_release;
5352 }
Samuel Ortiz250bdd22008-12-19 10:37:11 +08005353 if (init_data_size > IWL39_MAX_DATA_SIZE) {
Tomas Winkler90e759d2007-11-29 11:09:41 +08005354 IWL_DEBUG_INFO("uCode init data len %d too large to fit in\n",
5355 init_data_size);
5356 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07005357 goto err_release;
5358 }
Samuel Ortiz250bdd22008-12-19 10:37:11 +08005359 if (boot_size > IWL39_MAX_BSM_SIZE) {
Tomas Winkler90e759d2007-11-29 11:09:41 +08005360 IWL_DEBUG_INFO("uCode boot instr len %d too large to fit in\n",
5361 boot_size);
5362 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07005363 goto err_release;
5364 }
5365
5366 /* Allocate ucode buffers for card's bus-master loading ... */
5367
5368 /* Runtime instructions and 2 copies of data:
5369 * 1) unmodified from disk
5370 * 2) backup cache for save/restore during power-downs */
5371 priv->ucode_code.len = inst_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08005372 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_code);
Zhu Yib481de92007-09-25 17:54:57 -07005373
5374 priv->ucode_data.len = data_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08005375 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_data);
Zhu Yib481de92007-09-25 17:54:57 -07005376
5377 priv->ucode_data_backup.len = data_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08005378 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_data_backup);
Zhu Yib481de92007-09-25 17:54:57 -07005379
5380 if (!priv->ucode_code.v_addr || !priv->ucode_data.v_addr ||
Tomas Winkler90e759d2007-11-29 11:09:41 +08005381 !priv->ucode_data_backup.v_addr)
Zhu Yib481de92007-09-25 17:54:57 -07005382 goto err_pci_alloc;
5383
Tomas Winkler90e759d2007-11-29 11:09:41 +08005384 /* Initialization instructions and data */
5385 if (init_size && init_data_size) {
5386 priv->ucode_init.len = init_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08005387 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_init);
Tomas Winkler90e759d2007-11-29 11:09:41 +08005388
5389 priv->ucode_init_data.len = init_data_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08005390 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_init_data);
Tomas Winkler90e759d2007-11-29 11:09:41 +08005391
5392 if (!priv->ucode_init.v_addr || !priv->ucode_init_data.v_addr)
5393 goto err_pci_alloc;
5394 }
5395
5396 /* Bootstrap (instructions only, no data) */
5397 if (boot_size) {
5398 priv->ucode_boot.len = boot_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08005399 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_boot);
Tomas Winkler90e759d2007-11-29 11:09:41 +08005400
5401 if (!priv->ucode_boot.v_addr)
5402 goto err_pci_alloc;
5403 }
5404
Zhu Yib481de92007-09-25 17:54:57 -07005405 /* Copy images into buffers for card's bus-master reads ... */
5406
5407 /* Runtime instructions (first block of data in file) */
5408 src = &ucode->data[0];
5409 len = priv->ucode_code.len;
Tomas Winkler90e759d2007-11-29 11:09:41 +08005410 IWL_DEBUG_INFO("Copying (but not loading) uCode instr len %Zd\n", len);
Zhu Yib481de92007-09-25 17:54:57 -07005411 memcpy(priv->ucode_code.v_addr, src, len);
5412 IWL_DEBUG_INFO("uCode instr buf vaddr = 0x%p, paddr = 0x%08x\n",
5413 priv->ucode_code.v_addr, (u32)priv->ucode_code.p_addr);
5414
5415 /* Runtime data (2nd block)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005416 * NOTE: Copy into backup buffer will be done in iwl3945_up() */
Zhu Yib481de92007-09-25 17:54:57 -07005417 src = &ucode->data[inst_size];
5418 len = priv->ucode_data.len;
Tomas Winkler90e759d2007-11-29 11:09:41 +08005419 IWL_DEBUG_INFO("Copying (but not loading) uCode data len %Zd\n", len);
Zhu Yib481de92007-09-25 17:54:57 -07005420 memcpy(priv->ucode_data.v_addr, src, len);
5421 memcpy(priv->ucode_data_backup.v_addr, src, len);
5422
5423 /* Initialization instructions (3rd block) */
5424 if (init_size) {
5425 src = &ucode->data[inst_size + data_size];
5426 len = priv->ucode_init.len;
Tomas Winkler90e759d2007-11-29 11:09:41 +08005427 IWL_DEBUG_INFO("Copying (but not loading) init instr len %Zd\n",
5428 len);
Zhu Yib481de92007-09-25 17:54:57 -07005429 memcpy(priv->ucode_init.v_addr, src, len);
5430 }
5431
5432 /* Initialization data (4th block) */
5433 if (init_data_size) {
5434 src = &ucode->data[inst_size + data_size + init_size];
5435 len = priv->ucode_init_data.len;
5436 IWL_DEBUG_INFO("Copying (but not loading) init data len %d\n",
5437 (int)len);
5438 memcpy(priv->ucode_init_data.v_addr, src, len);
5439 }
5440
5441 /* Bootstrap instructions (5th block) */
5442 src = &ucode->data[inst_size + data_size + init_size + init_data_size];
5443 len = priv->ucode_boot.len;
5444 IWL_DEBUG_INFO("Copying (but not loading) boot instr len %d\n",
5445 (int)len);
5446 memcpy(priv->ucode_boot.v_addr, src, len);
5447
5448 /* We have our copies now, allow OS release its copies */
5449 release_firmware(ucode_raw);
5450 return 0;
5451
5452 err_pci_alloc:
Winkler, Tomas15b16872008-12-19 10:37:33 +08005453 IWL_ERR(priv, "failed to allocate pci memory\n");
Tomas Winkler90e759d2007-11-29 11:09:41 +08005454 ret = -ENOMEM;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005455 iwl3945_dealloc_ucode_pci(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005456
5457 err_release:
5458 release_firmware(ucode_raw);
5459
5460 error:
Tomas Winkler90e759d2007-11-29 11:09:41 +08005461 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07005462}
5463
5464
5465/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005466 * iwl3945_set_ucode_ptrs - Set uCode address location
Zhu Yib481de92007-09-25 17:54:57 -07005467 *
5468 * Tell initialization uCode where to find runtime uCode.
5469 *
5470 * BSM registers initially contain pointers to initialization uCode.
5471 * We need to replace them to load runtime uCode inst and data,
5472 * and to save runtime data when powering down.
5473 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08005474static int iwl3945_set_ucode_ptrs(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005475{
5476 dma_addr_t pinst;
5477 dma_addr_t pdata;
5478 int rc = 0;
5479 unsigned long flags;
5480
5481 /* bits 31:0 for 3945 */
5482 pinst = priv->ucode_code.p_addr;
5483 pdata = priv->ucode_data_backup.p_addr;
5484
5485 spin_lock_irqsave(&priv->lock, flags);
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08005486 rc = iwl_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005487 if (rc) {
5488 spin_unlock_irqrestore(&priv->lock, flags);
5489 return rc;
5490 }
5491
5492 /* Tell bootstrap uCode where to find image to load */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08005493 iwl_write_prph(priv, BSM_DRAM_INST_PTR_REG, pinst);
5494 iwl_write_prph(priv, BSM_DRAM_DATA_PTR_REG, pdata);
5495 iwl_write_prph(priv, BSM_DRAM_DATA_BYTECOUNT_REG,
Zhu Yib481de92007-09-25 17:54:57 -07005496 priv->ucode_data.len);
5497
Tomas Winklera96a27f2008-10-23 23:48:56 -07005498 /* Inst byte count must be last to set up, bit 31 signals uCode
Zhu Yib481de92007-09-25 17:54:57 -07005499 * that all new ptr/size info is in place */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08005500 iwl_write_prph(priv, BSM_DRAM_INST_BYTECOUNT_REG,
Zhu Yib481de92007-09-25 17:54:57 -07005501 priv->ucode_code.len | BSM_DRAM_INST_LOAD);
5502
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08005503 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005504
5505 spin_unlock_irqrestore(&priv->lock, flags);
5506
5507 IWL_DEBUG_INFO("Runtime uCode pointers are set.\n");
5508
5509 return rc;
5510}
5511
5512/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005513 * iwl3945_init_alive_start - Called after REPLY_ALIVE notification received
Zhu Yib481de92007-09-25 17:54:57 -07005514 *
5515 * Called after REPLY_ALIVE notification received from "initialize" uCode.
5516 *
Zhu Yib481de92007-09-25 17:54:57 -07005517 * Tell "initialize" uCode to go ahead and load the runtime uCode.
Ben Cahill9fbab512007-11-29 11:09:47 +08005518 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08005519static void iwl3945_init_alive_start(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005520{
5521 /* Check alive response for "valid" sign from uCode */
5522 if (priv->card_alive_init.is_valid != UCODE_VALID_OK) {
5523 /* We had an error bringing up the hardware, so take it
5524 * all the way back down so we can try again */
5525 IWL_DEBUG_INFO("Initialize Alive failed.\n");
5526 goto restart;
5527 }
5528
5529 /* Bootstrap uCode has loaded initialize uCode ... verify inst image.
5530 * This is a paranoid check, because we would not have gotten the
5531 * "initialize" alive if code weren't properly loaded. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005532 if (iwl3945_verify_ucode(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07005533 /* Runtime instruction load was bad;
5534 * take it all the way back down so we can try again */
5535 IWL_DEBUG_INFO("Bad \"initialize\" uCode load.\n");
5536 goto restart;
5537 }
5538
5539 /* Send pointers to protocol/runtime uCode image ... init code will
5540 * load and launch runtime uCode, which will send us another "Alive"
5541 * notification. */
5542 IWL_DEBUG_INFO("Initialization Alive received.\n");
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005543 if (iwl3945_set_ucode_ptrs(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07005544 /* Runtime instruction load won't happen;
5545 * take it all the way back down so we can try again */
5546 IWL_DEBUG_INFO("Couldn't set up uCode pointers.\n");
5547 goto restart;
5548 }
5549 return;
5550
5551 restart:
5552 queue_work(priv->workqueue, &priv->restart);
5553}
5554
5555
Mohamed Abbas9bdf5ec2008-11-07 09:58:35 -08005556/* temporary */
5557static int iwl3945_mac_beacon_update(struct ieee80211_hw *hw,
5558 struct sk_buff *skb);
5559
Zhu Yib481de92007-09-25 17:54:57 -07005560/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005561 * iwl3945_alive_start - called after REPLY_ALIVE notification received
Zhu Yib481de92007-09-25 17:54:57 -07005562 * from protocol/runtime uCode (initialization uCode's
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005563 * Alive gets handled by iwl3945_init_alive_start()).
Zhu Yib481de92007-09-25 17:54:57 -07005564 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08005565static void iwl3945_alive_start(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005566{
5567 int rc = 0;
5568 int thermal_spin = 0;
5569 u32 rfkill;
5570
5571 IWL_DEBUG_INFO("Runtime Alive received.\n");
5572
5573 if (priv->card_alive.is_valid != UCODE_VALID_OK) {
5574 /* We had an error bringing up the hardware, so take it
5575 * all the way back down so we can try again */
5576 IWL_DEBUG_INFO("Alive failed.\n");
5577 goto restart;
5578 }
5579
5580 /* Initialize uCode has loaded Runtime uCode ... verify inst image.
5581 * This is a paranoid check, because we would not have gotten the
5582 * "runtime" alive if code weren't properly loaded. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005583 if (iwl3945_verify_ucode(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07005584 /* Runtime instruction load was bad;
5585 * take it all the way back down so we can try again */
5586 IWL_DEBUG_INFO("Bad runtime uCode load.\n");
5587 goto restart;
5588 }
5589
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005590 iwl3945_clear_stations_table(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005591
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08005592 rc = iwl_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005593 if (rc) {
Winkler, Tomas39aadf82008-12-19 10:37:32 +08005594 IWL_WARN(priv, "Can not read RFKILL status from adapter\n");
Zhu Yib481de92007-09-25 17:54:57 -07005595 return;
5596 }
5597
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08005598 rfkill = iwl_read_prph(priv, APMG_RFKILL_REG);
Zhu Yib481de92007-09-25 17:54:57 -07005599 IWL_DEBUG_INFO("RFKILL status: 0x%x\n", rfkill);
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08005600 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005601
5602 if (rfkill & 0x1) {
5603 clear_bit(STATUS_RF_KILL_HW, &priv->status);
Tomas Winklera96a27f2008-10-23 23:48:56 -07005604 /* if RFKILL is not on, then wait for thermal
Zhu Yib481de92007-09-25 17:54:57 -07005605 * sensor in adapter to kick in */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005606 while (iwl3945_hw_get_temperature(priv) == 0) {
Zhu Yib481de92007-09-25 17:54:57 -07005607 thermal_spin++;
5608 udelay(10);
5609 }
5610
5611 if (thermal_spin)
5612 IWL_DEBUG_INFO("Thermal calibration took %dus\n",
5613 thermal_spin * 10);
5614 } else
5615 set_bit(STATUS_RF_KILL_HW, &priv->status);
5616
Ben Cahill9fbab512007-11-29 11:09:47 +08005617 /* After the ALIVE response, we can send commands to 3945 uCode */
Zhu Yib481de92007-09-25 17:54:57 -07005618 set_bit(STATUS_ALIVE, &priv->status);
5619
5620 /* Clear out the uCode error bit if it is set */
5621 clear_bit(STATUS_FW_ERROR, &priv->status);
5622
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005623 if (iwl3945_is_rfkill(priv))
Zhu Yib481de92007-09-25 17:54:57 -07005624 return;
5625
Johannes Berg36d68252008-05-15 12:55:26 +02005626 ieee80211_wake_queues(priv->hw);
Zhu Yib481de92007-09-25 17:54:57 -07005627
5628 priv->active_rate = priv->rates_mask;
5629 priv->active_rate_basic = priv->rates_mask & IWL_BASIC_RATES_MASK;
5630
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005631 iwl3945_send_power_mode(priv, IWL_POWER_LEVEL(priv->power_mode));
Zhu Yib481de92007-09-25 17:54:57 -07005632
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005633 if (iwl3945_is_associated(priv)) {
5634 struct iwl3945_rxon_cmd *active_rxon =
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08005635 (struct iwl3945_rxon_cmd *)(&priv->active39_rxon);
Zhu Yib481de92007-09-25 17:54:57 -07005636
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08005637 memcpy(&priv->staging39_rxon, &priv->active39_rxon,
5638 sizeof(priv->staging39_rxon));
Zhu Yib481de92007-09-25 17:54:57 -07005639 active_rxon->filter_flags &= ~RXON_FILTER_ASSOC_MSK;
5640 } else {
5641 /* Initialize our rx_config data */
Zhu, Yi60294de2008-10-29 14:05:45 -07005642 iwl3945_connection_init_rx_config(priv, priv->iw_mode);
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08005643 memcpy(priv->staging39_rxon.node_addr, priv->mac_addr, ETH_ALEN);
Zhu Yib481de92007-09-25 17:54:57 -07005644 }
5645
Ben Cahill9fbab512007-11-29 11:09:47 +08005646 /* Configure Bluetooth device coexistence support */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005647 iwl3945_send_bt_config(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005648
5649 /* Configure the adapter for unassociated operation */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005650 iwl3945_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005651
Zhu Yib481de92007-09-25 17:54:57 -07005652 iwl3945_reg_txpower_periodic(priv);
5653
Reinette Chatrefe00b5a2008-04-03 16:05:23 -07005654 iwl3945_led_register(priv);
5655
Zhu Yib481de92007-09-25 17:54:57 -07005656 IWL_DEBUG_INFO("ALIVE processing complete.\n");
Rick Farringtona9f46782008-03-18 14:57:49 -07005657 set_bit(STATUS_READY, &priv->status);
Zhu Yi5a669262008-01-14 17:46:18 -08005658 wake_up_interruptible(&priv->wait_command_queue);
Zhu Yib481de92007-09-25 17:54:57 -07005659
5660 if (priv->error_recovering)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005661 iwl3945_error_recovery(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005662
Mohamed Abbas9bdf5ec2008-11-07 09:58:35 -08005663 /* reassociate for ADHOC mode */
5664 if (priv->vif && (priv->iw_mode == NL80211_IFTYPE_ADHOC)) {
5665 struct sk_buff *beacon = ieee80211_beacon_get(priv->hw,
5666 priv->vif);
5667 if (beacon)
5668 iwl3945_mac_beacon_update(priv->hw, beacon);
5669 }
5670
Zhu Yib481de92007-09-25 17:54:57 -07005671 return;
5672
5673 restart:
5674 queue_work(priv->workqueue, &priv->restart);
5675}
5676
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08005677static void iwl3945_cancel_deferred_work(struct iwl_priv *priv);
Zhu Yib481de92007-09-25 17:54:57 -07005678
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08005679static void __iwl3945_down(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005680{
5681 unsigned long flags;
5682 int exit_pending = test_bit(STATUS_EXIT_PENDING, &priv->status);
5683 struct ieee80211_conf *conf = NULL;
5684
5685 IWL_DEBUG_INFO(DRV_NAME " is going down\n");
5686
5687 conf = ieee80211_get_hw_conf(priv->hw);
5688
5689 if (!exit_pending)
5690 set_bit(STATUS_EXIT_PENDING, &priv->status);
5691
Mohamed Abbasab53d8a2008-03-25 16:33:36 -07005692 iwl3945_led_unregister(priv);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005693 iwl3945_clear_stations_table(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005694
5695 /* Unblock any waiting calls */
5696 wake_up_interruptible_all(&priv->wait_command_queue);
5697
Zhu Yib481de92007-09-25 17:54:57 -07005698 /* Wipe out the EXIT_PENDING status bit if we are not actually
5699 * exiting the module */
5700 if (!exit_pending)
5701 clear_bit(STATUS_EXIT_PENDING, &priv->status);
5702
5703 /* stop and reset the on-board processor */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08005704 iwl_write32(priv, CSR_RESET, CSR_RESET_REG_FLAG_NEVO_RESET);
Zhu Yib481de92007-09-25 17:54:57 -07005705
5706 /* tell the device to stop sending interrupts */
Mohamed Abbas0359fac2008-03-28 16:21:08 -07005707 spin_lock_irqsave(&priv->lock, flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005708 iwl3945_disable_interrupts(priv);
Mohamed Abbas0359fac2008-03-28 16:21:08 -07005709 spin_unlock_irqrestore(&priv->lock, flags);
5710 iwl_synchronize_irq(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005711
5712 if (priv->mac80211_registered)
5713 ieee80211_stop_queues(priv->hw);
5714
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005715 /* If we have not previously called iwl3945_init() then
Zhu Yib481de92007-09-25 17:54:57 -07005716 * clear all bits but the RF Kill and SUSPEND bits and return */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005717 if (!iwl3945_is_init(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07005718 priv->status = test_bit(STATUS_RF_KILL_HW, &priv->status) <<
5719 STATUS_RF_KILL_HW |
5720 test_bit(STATUS_RF_KILL_SW, &priv->status) <<
5721 STATUS_RF_KILL_SW |
Reinette Chatre97888642008-02-06 11:20:38 -08005722 test_bit(STATUS_GEO_CONFIGURED, &priv->status) <<
5723 STATUS_GEO_CONFIGURED |
Zhu Yib481de92007-09-25 17:54:57 -07005724 test_bit(STATUS_IN_SUSPEND, &priv->status) <<
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08005725 STATUS_IN_SUSPEND |
5726 test_bit(STATUS_EXIT_PENDING, &priv->status) <<
5727 STATUS_EXIT_PENDING;
Zhu Yib481de92007-09-25 17:54:57 -07005728 goto exit;
5729 }
5730
5731 /* ...otherwise clear out all the status bits but the RF Kill and
5732 * SUSPEND bits and continue taking the NIC down. */
5733 priv->status &= test_bit(STATUS_RF_KILL_HW, &priv->status) <<
5734 STATUS_RF_KILL_HW |
5735 test_bit(STATUS_RF_KILL_SW, &priv->status) <<
5736 STATUS_RF_KILL_SW |
Reinette Chatre97888642008-02-06 11:20:38 -08005737 test_bit(STATUS_GEO_CONFIGURED, &priv->status) <<
5738 STATUS_GEO_CONFIGURED |
Zhu Yib481de92007-09-25 17:54:57 -07005739 test_bit(STATUS_IN_SUSPEND, &priv->status) <<
5740 STATUS_IN_SUSPEND |
5741 test_bit(STATUS_FW_ERROR, &priv->status) <<
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08005742 STATUS_FW_ERROR |
5743 test_bit(STATUS_EXIT_PENDING, &priv->status) <<
5744 STATUS_EXIT_PENDING;
Zhu Yib481de92007-09-25 17:54:57 -07005745
5746 spin_lock_irqsave(&priv->lock, flags);
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08005747 iwl_clear_bit(priv, CSR_GP_CNTRL, CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
Zhu Yib481de92007-09-25 17:54:57 -07005748 spin_unlock_irqrestore(&priv->lock, flags);
5749
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005750 iwl3945_hw_txq_ctx_stop(priv);
5751 iwl3945_hw_rxq_stop(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005752
5753 spin_lock_irqsave(&priv->lock, flags);
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08005754 if (!iwl_grab_nic_access(priv)) {
5755 iwl_write_prph(priv, APMG_CLK_DIS_REG,
Zhu Yib481de92007-09-25 17:54:57 -07005756 APMG_CLK_VAL_DMA_CLK_RQT);
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08005757 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005758 }
5759 spin_unlock_irqrestore(&priv->lock, flags);
5760
5761 udelay(5);
5762
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005763 iwl3945_hw_nic_stop_master(priv);
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08005764 iwl_set_bit(priv, CSR_RESET, CSR_RESET_REG_FLAG_SW_RESET);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005765 iwl3945_hw_nic_reset(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005766
5767 exit:
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08005768 memset(&priv->card_alive, 0, sizeof(struct iwl_alive_resp));
Zhu Yib481de92007-09-25 17:54:57 -07005769
5770 if (priv->ibss_beacon)
5771 dev_kfree_skb(priv->ibss_beacon);
5772 priv->ibss_beacon = NULL;
5773
5774 /* clear out any free frames */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005775 iwl3945_clear_free_frames(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005776}
5777
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08005778static void iwl3945_down(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005779{
5780 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005781 __iwl3945_down(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005782 mutex_unlock(&priv->mutex);
Zhu Yib24d22b2007-12-19 13:59:52 +08005783
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005784 iwl3945_cancel_deferred_work(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005785}
5786
5787#define MAX_HW_RESTARTS 5
5788
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08005789static int __iwl3945_up(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005790{
5791 int rc, i;
5792
5793 if (test_bit(STATUS_EXIT_PENDING, &priv->status)) {
Winkler, Tomas39aadf82008-12-19 10:37:32 +08005794 IWL_WARN(priv, "Exit pending; will not bring the NIC up\n");
Zhu Yib481de92007-09-25 17:54:57 -07005795 return -EIO;
5796 }
5797
5798 if (test_bit(STATUS_RF_KILL_SW, &priv->status)) {
Winkler, Tomas39aadf82008-12-19 10:37:32 +08005799 IWL_WARN(priv, "Radio disabled by SW RF kill (module "
Zhu Yib481de92007-09-25 17:54:57 -07005800 "parameter)\n");
Zhu Yie655b9f2008-01-24 02:19:38 -08005801 return -ENODEV;
5802 }
5803
Reinette Chatree903fbd2008-01-30 22:05:15 -08005804 if (!priv->ucode_data_backup.v_addr || !priv->ucode_data.v_addr) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08005805 IWL_ERR(priv, "ucode not available for device bring up\n");
Reinette Chatree903fbd2008-01-30 22:05:15 -08005806 return -EIO;
5807 }
5808
Zhu Yie655b9f2008-01-24 02:19:38 -08005809 /* If platform's RF_KILL switch is NOT set to KILL */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08005810 if (iwl_read32(priv, CSR_GP_CNTRL) &
Zhu Yie655b9f2008-01-24 02:19:38 -08005811 CSR_GP_CNTRL_REG_FLAG_HW_RF_KILL_SW)
5812 clear_bit(STATUS_RF_KILL_HW, &priv->status);
5813 else {
5814 set_bit(STATUS_RF_KILL_HW, &priv->status);
5815 if (!test_bit(STATUS_IN_SUSPEND, &priv->status)) {
Winkler, Tomas39aadf82008-12-19 10:37:32 +08005816 IWL_WARN(priv, "Radio disabled by HW RF Kill switch\n");
Zhu Yie655b9f2008-01-24 02:19:38 -08005817 return -ENODEV;
5818 }
Zhu Yib481de92007-09-25 17:54:57 -07005819 }
Adel Gadllah80fcc9e2008-07-01 17:49:50 +02005820
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08005821 iwl_write32(priv, CSR_INT, 0xFFFFFFFF);
Zhu Yib481de92007-09-25 17:54:57 -07005822
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005823 rc = iwl3945_hw_nic_init(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005824 if (rc) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08005825 IWL_ERR(priv, "Unable to int nic\n");
Zhu Yib481de92007-09-25 17:54:57 -07005826 return rc;
5827 }
5828
5829 /* make sure rfkill handshake bits are cleared */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08005830 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
5831 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR,
Zhu Yib481de92007-09-25 17:54:57 -07005832 CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
5833
5834 /* clear (again), then enable host interrupts */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08005835 iwl_write32(priv, CSR_INT, 0xFFFFFFFF);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005836 iwl3945_enable_interrupts(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005837
5838 /* really make sure rfkill handshake bits are cleared */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08005839 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
5840 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
Zhu Yib481de92007-09-25 17:54:57 -07005841
5842 /* Copy original ucode data image from disk into backup cache.
5843 * This will be used to initialize the on-board processor's
5844 * data SRAM for a clean start when the runtime program first loads. */
5845 memcpy(priv->ucode_data_backup.v_addr, priv->ucode_data.v_addr,
Zhu Yi5a669262008-01-14 17:46:18 -08005846 priv->ucode_data.len);
Zhu Yib481de92007-09-25 17:54:57 -07005847
Zhu Yie655b9f2008-01-24 02:19:38 -08005848 /* We return success when we resume from suspend and rf_kill is on. */
5849 if (test_bit(STATUS_RF_KILL_HW, &priv->status))
5850 return 0;
5851
Zhu Yib481de92007-09-25 17:54:57 -07005852 for (i = 0; i < MAX_HW_RESTARTS; i++) {
5853
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005854 iwl3945_clear_stations_table(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005855
5856 /* load bootstrap state machine,
5857 * load bootstrap program into processor's memory,
5858 * prepare to load the "initialize" uCode */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005859 rc = iwl3945_load_bsm(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005860
5861 if (rc) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08005862 IWL_ERR(priv,
5863 "Unable to set up bootstrap uCode: %d\n", rc);
Zhu Yib481de92007-09-25 17:54:57 -07005864 continue;
5865 }
5866
5867 /* start card; "initialize" will load runtime ucode */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005868 iwl3945_nic_start(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005869
Zhu Yib481de92007-09-25 17:54:57 -07005870 IWL_DEBUG_INFO(DRV_NAME " is coming up\n");
5871
5872 return 0;
5873 }
5874
5875 set_bit(STATUS_EXIT_PENDING, &priv->status);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005876 __iwl3945_down(priv);
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08005877 clear_bit(STATUS_EXIT_PENDING, &priv->status);
Zhu Yib481de92007-09-25 17:54:57 -07005878
5879 /* tried to restart and config the device for as long as our
5880 * patience could withstand */
Winkler, Tomas15b16872008-12-19 10:37:33 +08005881 IWL_ERR(priv, "Unable to initialize device after %d attempts.\n", i);
Zhu Yib481de92007-09-25 17:54:57 -07005882 return -EIO;
5883}
5884
5885
5886/*****************************************************************************
5887 *
5888 * Workqueue callbacks
5889 *
5890 *****************************************************************************/
5891
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005892static void iwl3945_bg_init_alive_start(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07005893{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08005894 struct iwl_priv *priv =
5895 container_of(data, struct iwl_priv, init_alive_start.work);
Zhu Yib481de92007-09-25 17:54:57 -07005896
5897 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
5898 return;
5899
5900 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005901 iwl3945_init_alive_start(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005902 mutex_unlock(&priv->mutex);
5903}
5904
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005905static void iwl3945_bg_alive_start(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07005906{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08005907 struct iwl_priv *priv =
5908 container_of(data, struct iwl_priv, alive_start.work);
Zhu Yib481de92007-09-25 17:54:57 -07005909
5910 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
5911 return;
5912
5913 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005914 iwl3945_alive_start(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005915 mutex_unlock(&priv->mutex);
5916}
5917
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005918static void iwl3945_bg_rf_kill(struct work_struct *work)
Zhu Yib481de92007-09-25 17:54:57 -07005919{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08005920 struct iwl_priv *priv = container_of(work, struct iwl_priv, rf_kill);
Zhu Yib481de92007-09-25 17:54:57 -07005921
5922 wake_up_interruptible(&priv->wait_command_queue);
5923
5924 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
5925 return;
5926
5927 mutex_lock(&priv->mutex);
5928
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005929 if (!iwl3945_is_rfkill(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07005930 IWL_DEBUG(IWL_DL_INFO | IWL_DL_RF_KILL,
5931 "HW and/or SW RF Kill no longer active, restarting "
5932 "device\n");
5933 if (!test_bit(STATUS_EXIT_PENDING, &priv->status))
5934 queue_work(priv->workqueue, &priv->restart);
5935 } else {
5936
5937 if (!test_bit(STATUS_RF_KILL_HW, &priv->status))
5938 IWL_DEBUG_RF_KILL("Can not turn radio back on - "
5939 "disabled by SW switch\n");
5940 else
Winkler, Tomas39aadf82008-12-19 10:37:32 +08005941 IWL_WARN(priv, "Radio Frequency Kill Switch is On:\n"
Zhu Yib481de92007-09-25 17:54:57 -07005942 "Kill switch must be turned off for "
5943 "wireless networking to work.\n");
5944 }
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08005945
Zhu Yib481de92007-09-25 17:54:57 -07005946 mutex_unlock(&priv->mutex);
Adel Gadllah80fcc9e2008-07-01 17:49:50 +02005947 iwl3945_rfkill_set_hw_state(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005948}
5949
5950#define IWL_SCAN_CHECK_WATCHDOG (7 * HZ)
5951
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005952static void iwl3945_bg_scan_check(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07005953{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08005954 struct iwl_priv *priv =
5955 container_of(data, struct iwl_priv, scan_check.work);
Zhu Yib481de92007-09-25 17:54:57 -07005956
5957 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
5958 return;
5959
5960 mutex_lock(&priv->mutex);
5961 if (test_bit(STATUS_SCANNING, &priv->status) ||
5962 test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
5963 IWL_DEBUG(IWL_DL_INFO | IWL_DL_SCAN,
5964 "Scan completion watchdog resetting adapter (%dms)\n",
5965 jiffies_to_msecs(IWL_SCAN_CHECK_WATCHDOG));
Mohamed Abbas15e869d2007-10-25 17:15:46 +08005966
Zhu Yib481de92007-09-25 17:54:57 -07005967 if (!test_bit(STATUS_EXIT_PENDING, &priv->status))
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005968 iwl3945_send_scan_abort(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005969 }
5970 mutex_unlock(&priv->mutex);
5971}
5972
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005973static void iwl3945_bg_request_scan(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07005974{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08005975 struct iwl_priv *priv =
5976 container_of(data, struct iwl_priv, request_scan);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005977 struct iwl3945_host_cmd cmd = {
Zhu Yib481de92007-09-25 17:54:57 -07005978 .id = REPLY_SCAN_CMD,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005979 .len = sizeof(struct iwl3945_scan_cmd),
Zhu Yib481de92007-09-25 17:54:57 -07005980 .meta.flags = CMD_SIZE_HUGE,
5981 };
5982 int rc = 0;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005983 struct iwl3945_scan_cmd *scan;
Zhu Yib481de92007-09-25 17:54:57 -07005984 struct ieee80211_conf *conf = NULL;
Abhijeet Kolekarf9340522008-09-03 11:26:58 +08005985 u8 n_probes = 2;
Johannes Berg8318d782008-01-24 19:38:38 +01005986 enum ieee80211_band band;
John W. Linville9387b7c2008-09-30 20:59:05 -04005987 DECLARE_SSID_BUF(ssid);
Zhu Yib481de92007-09-25 17:54:57 -07005988
5989 conf = ieee80211_get_hw_conf(priv->hw);
5990
5991 mutex_lock(&priv->mutex);
5992
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005993 if (!iwl3945_is_ready(priv)) {
Winkler, Tomas39aadf82008-12-19 10:37:32 +08005994 IWL_WARN(priv, "request scan called when driver not ready.\n");
Zhu Yib481de92007-09-25 17:54:57 -07005995 goto done;
5996 }
5997
Tomas Winklera96a27f2008-10-23 23:48:56 -07005998 /* Make sure the scan wasn't canceled before this queued work
Zhu Yib481de92007-09-25 17:54:57 -07005999 * was given the chance to run... */
6000 if (!test_bit(STATUS_SCANNING, &priv->status))
6001 goto done;
6002
6003 /* This should never be called or scheduled if there is currently
6004 * a scan active in the hardware. */
6005 if (test_bit(STATUS_SCAN_HW, &priv->status)) {
6006 IWL_DEBUG_INFO("Multiple concurrent scan requests in parallel. "
6007 "Ignoring second request.\n");
6008 rc = -EIO;
6009 goto done;
6010 }
6011
6012 if (test_bit(STATUS_EXIT_PENDING, &priv->status)) {
6013 IWL_DEBUG_SCAN("Aborting scan due to device shutdown\n");
6014 goto done;
6015 }
6016
6017 if (test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
6018 IWL_DEBUG_HC("Scan request while abort pending. Queuing.\n");
6019 goto done;
6020 }
6021
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006022 if (iwl3945_is_rfkill(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07006023 IWL_DEBUG_HC("Aborting scan due to RF Kill activation\n");
6024 goto done;
6025 }
6026
6027 if (!test_bit(STATUS_READY, &priv->status)) {
6028 IWL_DEBUG_HC("Scan request while uninitialized. Queuing.\n");
6029 goto done;
6030 }
6031
6032 if (!priv->scan_bands) {
6033 IWL_DEBUG_HC("Aborting scan due to no requested bands\n");
6034 goto done;
6035 }
6036
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08006037 if (!priv->scan39) {
6038 priv->scan39 = kmalloc(sizeof(struct iwl3945_scan_cmd) +
Zhu Yib481de92007-09-25 17:54:57 -07006039 IWL_MAX_SCAN_SIZE, GFP_KERNEL);
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08006040 if (!priv->scan39) {
Zhu Yib481de92007-09-25 17:54:57 -07006041 rc = -ENOMEM;
6042 goto done;
6043 }
6044 }
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08006045 scan = priv->scan39;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006046 memset(scan, 0, sizeof(struct iwl3945_scan_cmd) + IWL_MAX_SCAN_SIZE);
Zhu Yib481de92007-09-25 17:54:57 -07006047
6048 scan->quiet_plcp_th = IWL_PLCP_QUIET_THRESH;
6049 scan->quiet_time = IWL_ACTIVE_QUIET_TIME;
6050
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006051 if (iwl3945_is_associated(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07006052 u16 interval = 0;
6053 u32 extra;
6054 u32 suspend_time = 100;
6055 u32 scan_suspend_time = 100;
6056 unsigned long flags;
6057
6058 IWL_DEBUG_INFO("Scanning while associated...\n");
6059
6060 spin_lock_irqsave(&priv->lock, flags);
6061 interval = priv->beacon_int;
6062 spin_unlock_irqrestore(&priv->lock, flags);
6063
6064 scan->suspend_time = 0;
Mohamed Abbas15e869d2007-10-25 17:15:46 +08006065 scan->max_out_time = cpu_to_le32(200 * 1024);
Zhu Yib481de92007-09-25 17:54:57 -07006066 if (!interval)
6067 interval = suspend_time;
6068 /*
6069 * suspend time format:
6070 * 0-19: beacon interval in usec (time before exec.)
6071 * 20-23: 0
6072 * 24-31: number of beacons (suspend between channels)
6073 */
6074
6075 extra = (suspend_time / interval) << 24;
6076 scan_suspend_time = 0xFF0FFFFF &
6077 (extra | ((suspend_time % interval) * 1024));
6078
6079 scan->suspend_time = cpu_to_le32(scan_suspend_time);
6080 IWL_DEBUG_SCAN("suspend_time 0x%X beacon interval %d\n",
6081 scan_suspend_time, interval);
6082 }
6083
6084 /* We should add the ability for user to lock to PASSIVE ONLY */
6085 if (priv->one_direct_scan) {
6086 IWL_DEBUG_SCAN
6087 ("Kicking off one direct scan for '%s'\n",
John W. Linville9387b7c2008-09-30 20:59:05 -04006088 print_ssid(ssid, priv->direct_ssid,
6089 priv->direct_ssid_len));
Zhu Yib481de92007-09-25 17:54:57 -07006090 scan->direct_scan[0].id = WLAN_EID_SSID;
6091 scan->direct_scan[0].len = priv->direct_ssid_len;
6092 memcpy(scan->direct_scan[0].ssid,
6093 priv->direct_ssid, priv->direct_ssid_len);
Abhijeet Kolekarf9340522008-09-03 11:26:58 +08006094 n_probes++;
Abhijeet Kolekarf9340522008-09-03 11:26:58 +08006095 } else
Bill Moss786b4552008-04-17 16:03:40 -07006096 IWL_DEBUG_SCAN("Kicking off one indirect scan.\n");
Zhu Yib481de92007-09-25 17:54:57 -07006097
6098 /* We don't build a direct scan probe request; the uCode will do
6099 * that based on the direct_mask added to each channel entry */
6100 scan->tx_cmd.len = cpu_to_le16(
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006101 iwl3945_fill_probe_req(priv, (struct ieee80211_mgmt *)scan->data,
Johannes Berg430cfe92008-10-28 18:06:02 +01006102 IWL_MAX_SCAN_SIZE - sizeof(*scan)));
Zhu Yib481de92007-09-25 17:54:57 -07006103 scan->tx_cmd.tx_flags = TX_CMD_FLG_SEQ_CTL_MSK;
Abhijeet Kolekar3832ec92008-12-19 10:37:26 +08006104 scan->tx_cmd.sta_id = priv->hw_params.bcast_sta_id;
Zhu Yib481de92007-09-25 17:54:57 -07006105 scan->tx_cmd.stop_time.life_time = TX_CMD_LIFE_TIME_INFINITE;
6106
6107 /* flags + rate selection */
6108
Ron Rindjunsky66b50042008-06-25 16:46:31 +08006109 if (priv->scan_bands & BIT(IEEE80211_BAND_2GHZ)) {
Zhu Yib481de92007-09-25 17:54:57 -07006110 scan->flags = RXON_FLG_BAND_24G_MSK | RXON_FLG_AUTO_DETECT_MSK;
6111 scan->tx_cmd.rate = IWL_RATE_1M_PLCP;
6112 scan->good_CRC_th = 0;
Johannes Berg8318d782008-01-24 19:38:38 +01006113 band = IEEE80211_BAND_2GHZ;
Ron Rindjunsky66b50042008-06-25 16:46:31 +08006114 } else if (priv->scan_bands & BIT(IEEE80211_BAND_5GHZ)) {
Zhu Yib481de92007-09-25 17:54:57 -07006115 scan->tx_cmd.rate = IWL_RATE_6M_PLCP;
6116 scan->good_CRC_th = IWL_GOOD_CRC_TH;
Johannes Berg8318d782008-01-24 19:38:38 +01006117 band = IEEE80211_BAND_5GHZ;
Ron Rindjunsky66b50042008-06-25 16:46:31 +08006118 } else {
Winkler, Tomas39aadf82008-12-19 10:37:32 +08006119 IWL_WARN(priv, "Invalid scan band count\n");
Zhu Yib481de92007-09-25 17:54:57 -07006120 goto done;
6121 }
6122
6123 /* select Rx antennas */
6124 scan->flags |= iwl3945_get_antenna_flags(priv);
6125
Johannes Berg05c914f2008-09-11 00:01:58 +02006126 if (priv->iw_mode == NL80211_IFTYPE_MONITOR)
Zhu Yib481de92007-09-25 17:54:57 -07006127 scan->filter_flags = RXON_FILTER_PROMISC_MSK;
6128
Abhijeet Kolekarf9340522008-09-03 11:26:58 +08006129 scan->channel_count =
6130 iwl3945_get_channels_for_scan(priv, band, 1, /* active */
6131 n_probes,
6132 (void *)&scan->data[le16_to_cpu(scan->tx_cmd.len)]);
Zhu Yib481de92007-09-25 17:54:57 -07006133
Reinette Chatre14b54332008-11-04 12:21:35 -08006134 if (scan->channel_count == 0) {
6135 IWL_DEBUG_SCAN("channel count %d\n", scan->channel_count);
6136 goto done;
6137 }
6138
Zhu Yib481de92007-09-25 17:54:57 -07006139 cmd.len += le16_to_cpu(scan->tx_cmd.len) +
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006140 scan->channel_count * sizeof(struct iwl3945_scan_channel);
Zhu Yib481de92007-09-25 17:54:57 -07006141 cmd.data = scan;
6142 scan->len = cpu_to_le16(cmd.len);
6143
6144 set_bit(STATUS_SCAN_HW, &priv->status);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006145 rc = iwl3945_send_cmd_sync(priv, &cmd);
Zhu Yib481de92007-09-25 17:54:57 -07006146 if (rc)
6147 goto done;
6148
6149 queue_delayed_work(priv->workqueue, &priv->scan_check,
6150 IWL_SCAN_CHECK_WATCHDOG);
6151
6152 mutex_unlock(&priv->mutex);
6153 return;
6154
6155 done:
Mohamed Abbas2420ebc2008-11-04 12:21:34 -08006156 /* can not perform scan make sure we clear scanning
6157 * bits from status so next scan request can be performed.
6158 * if we dont clear scanning status bit here all next scan
6159 * will fail
6160 */
6161 clear_bit(STATUS_SCAN_HW, &priv->status);
6162 clear_bit(STATUS_SCANNING, &priv->status);
6163
Ian Schram01ebd062007-10-25 17:15:22 +08006164 /* inform mac80211 scan aborted */
Zhu Yib481de92007-09-25 17:54:57 -07006165 queue_work(priv->workqueue, &priv->scan_completed);
6166 mutex_unlock(&priv->mutex);
6167}
6168
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006169static void iwl3945_bg_up(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07006170{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08006171 struct iwl_priv *priv = container_of(data, struct iwl_priv, up);
Zhu Yib481de92007-09-25 17:54:57 -07006172
6173 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
6174 return;
6175
6176 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006177 __iwl3945_up(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006178 mutex_unlock(&priv->mutex);
Adel Gadllah80fcc9e2008-07-01 17:49:50 +02006179 iwl3945_rfkill_set_hw_state(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006180}
6181
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006182static void iwl3945_bg_restart(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07006183{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08006184 struct iwl_priv *priv = container_of(data, struct iwl_priv, restart);
Zhu Yib481de92007-09-25 17:54:57 -07006185
6186 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
6187 return;
6188
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006189 iwl3945_down(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006190 queue_work(priv->workqueue, &priv->up);
6191}
6192
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006193static void iwl3945_bg_rx_replenish(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07006194{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08006195 struct iwl_priv *priv =
6196 container_of(data, struct iwl_priv, rx_replenish);
Zhu Yib481de92007-09-25 17:54:57 -07006197
6198 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
6199 return;
6200
6201 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006202 iwl3945_rx_replenish(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006203 mutex_unlock(&priv->mutex);
6204}
6205
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08006206#define IWL_DELAY_NEXT_SCAN (HZ*2)
6207
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08006208static void iwl3945_post_associate(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07006209{
Zhu Yib481de92007-09-25 17:54:57 -07006210 int rc = 0;
6211 struct ieee80211_conf *conf = NULL;
6212
Johannes Berg05c914f2008-09-11 00:01:58 +02006213 if (priv->iw_mode == NL80211_IFTYPE_AP) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08006214 IWL_ERR(priv, "%s Should not be called in AP mode\n", __func__);
Zhu Yib481de92007-09-25 17:54:57 -07006215 return;
6216 }
6217
6218
Johannes Berge1749612008-10-27 15:59:26 -07006219 IWL_DEBUG_ASSOC("Associated as %d to: %pM\n",
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08006220 priv->assoc_id, priv->active39_rxon.bssid_addr);
Zhu Yib481de92007-09-25 17:54:57 -07006221
6222 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
6223 return;
6224
Abhijeet Kolekar322a9812008-09-03 11:26:27 +08006225 if (!priv->vif || !priv->is_open)
Mohamed Abbas6ef89d02007-10-25 17:15:47 +08006226 return;
Abhijeet Kolekar322a9812008-09-03 11:26:27 +08006227
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006228 iwl3945_scan_cancel_timeout(priv, 200);
Mohamed Abbas15e869d2007-10-25 17:15:46 +08006229
Zhu Yib481de92007-09-25 17:54:57 -07006230 conf = ieee80211_get_hw_conf(priv->hw);
6231
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08006232 priv->staging39_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006233 iwl3945_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006234
Tomas Winkler28afaf92008-12-19 10:37:06 +08006235 memset(&priv->rxon_timing, 0, sizeof(struct iwl_rxon_time_cmd));
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006236 iwl3945_setup_rxon_timing(priv);
6237 rc = iwl3945_send_cmd_pdu(priv, REPLY_RXON_TIMING,
Zhu Yib481de92007-09-25 17:54:57 -07006238 sizeof(priv->rxon_timing), &priv->rxon_timing);
6239 if (rc)
Winkler, Tomas39aadf82008-12-19 10:37:32 +08006240 IWL_WARN(priv, "REPLY_RXON_TIMING failed - "
Zhu Yib481de92007-09-25 17:54:57 -07006241 "Attempting to continue.\n");
6242
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08006243 priv->staging39_rxon.filter_flags |= RXON_FILTER_ASSOC_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07006244
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08006245 priv->staging39_rxon.assoc_id = cpu_to_le16(priv->assoc_id);
Zhu Yib481de92007-09-25 17:54:57 -07006246
6247 IWL_DEBUG_ASSOC("assoc id %d beacon interval %d\n",
6248 priv->assoc_id, priv->beacon_int);
6249
6250 if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_PREAMBLE)
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08006251 priv->staging39_rxon.flags |= RXON_FLG_SHORT_PREAMBLE_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07006252 else
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08006253 priv->staging39_rxon.flags &= ~RXON_FLG_SHORT_PREAMBLE_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07006254
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08006255 if (priv->staging39_rxon.flags & RXON_FLG_BAND_24G_MSK) {
Zhu Yib481de92007-09-25 17:54:57 -07006256 if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_SLOT_TIME)
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08006257 priv->staging39_rxon.flags |= RXON_FLG_SHORT_SLOT_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07006258 else
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08006259 priv->staging39_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07006260
Johannes Berg05c914f2008-09-11 00:01:58 +02006261 if (priv->iw_mode == NL80211_IFTYPE_ADHOC)
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08006262 priv->staging39_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07006263
6264 }
6265
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006266 iwl3945_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006267
6268 switch (priv->iw_mode) {
Johannes Berg05c914f2008-09-11 00:01:58 +02006269 case NL80211_IFTYPE_STATION:
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006270 iwl3945_rate_scale_init(priv->hw, IWL_AP_ID);
Zhu Yib481de92007-09-25 17:54:57 -07006271 break;
6272
Johannes Berg05c914f2008-09-11 00:01:58 +02006273 case NL80211_IFTYPE_ADHOC:
Zhu Yib481de92007-09-25 17:54:57 -07006274
Abhijeet Kolekarce546fd2008-11-19 15:32:22 -08006275 priv->assoc_id = 1;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006276 iwl3945_add_station(priv, priv->bssid, 0, 0);
Zhu Yib481de92007-09-25 17:54:57 -07006277 iwl3945_sync_sta(priv, IWL_STA_ID,
Johannes Berg8318d782008-01-24 19:38:38 +01006278 (priv->band == IEEE80211_BAND_5GHZ) ?
Zhu Yib481de92007-09-25 17:54:57 -07006279 IWL_RATE_6M_PLCP : IWL_RATE_1M_PLCP,
6280 CMD_ASYNC);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006281 iwl3945_rate_scale_init(priv->hw, IWL_STA_ID);
6282 iwl3945_send_beacon_cmd(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006283
6284 break;
6285
6286 default:
Winkler, Tomas15b16872008-12-19 10:37:33 +08006287 IWL_ERR(priv, "%s Should not be called in %d mode\n",
Tomas Winkler3ac7f142008-07-21 02:40:14 +03006288 __func__, priv->iw_mode);
Zhu Yib481de92007-09-25 17:54:57 -07006289 break;
6290 }
6291
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006292 iwl3945_activate_qos(priv, 0);
Ron Rindjunsky292ae172008-02-06 11:20:39 -08006293
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08006294 /* we have just associated, don't start scan too early */
6295 priv->next_scan_jiffies = jiffies + IWL_DELAY_NEXT_SCAN;
Abhijeet Kolekarcd56d332008-09-03 11:26:21 +08006296}
6297
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006298static void iwl3945_bg_abort_scan(struct work_struct *work)
Zhu Yib481de92007-09-25 17:54:57 -07006299{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08006300 struct iwl_priv *priv = container_of(work, struct iwl_priv, abort_scan);
Zhu Yib481de92007-09-25 17:54:57 -07006301
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006302 if (!iwl3945_is_ready(priv))
Zhu Yib481de92007-09-25 17:54:57 -07006303 return;
6304
6305 mutex_lock(&priv->mutex);
6306
6307 set_bit(STATUS_SCAN_ABORTING, &priv->status);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006308 iwl3945_send_scan_abort(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006309
6310 mutex_unlock(&priv->mutex);
6311}
6312
Johannes Berge8975582008-10-09 12:18:51 +02006313static int iwl3945_mac_config(struct ieee80211_hw *hw, u32 changed);
Zhu Yi76bb77e2007-11-22 10:53:22 +08006314
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006315static void iwl3945_bg_scan_completed(struct work_struct *work)
Zhu Yib481de92007-09-25 17:54:57 -07006316{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08006317 struct iwl_priv *priv =
6318 container_of(work, struct iwl_priv, scan_completed);
Zhu Yib481de92007-09-25 17:54:57 -07006319
6320 IWL_DEBUG(IWL_DL_INFO | IWL_DL_SCAN, "SCAN complete scan\n");
6321
6322 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
6323 return;
6324
Zhu Yia0646472007-12-20 14:10:01 +08006325 if (test_bit(STATUS_CONF_PENDING, &priv->status))
Johannes Berge8975582008-10-09 12:18:51 +02006326 iwl3945_mac_config(priv->hw, 0);
Zhu Yi76bb77e2007-11-22 10:53:22 +08006327
Zhu Yib481de92007-09-25 17:54:57 -07006328 ieee80211_scan_completed(priv->hw);
6329
6330 /* Since setting the TXPOWER may have been deferred while
6331 * performing the scan, fire one off */
6332 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006333 iwl3945_hw_reg_send_txpower(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006334 mutex_unlock(&priv->mutex);
6335}
6336
6337/*****************************************************************************
6338 *
6339 * mac80211 entry point functions
6340 *
6341 *****************************************************************************/
6342
Zhu Yi5a669262008-01-14 17:46:18 -08006343#define UCODE_READY_TIMEOUT (2 * HZ)
6344
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006345static int iwl3945_mac_start(struct ieee80211_hw *hw)
Zhu Yib481de92007-09-25 17:54:57 -07006346{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08006347 struct iwl_priv *priv = hw->priv;
Zhu Yi5a669262008-01-14 17:46:18 -08006348 int ret;
Zhu Yib481de92007-09-25 17:54:57 -07006349
6350 IWL_DEBUG_MAC80211("enter\n");
6351
Zhu Yi5a669262008-01-14 17:46:18 -08006352 if (pci_enable_device(priv->pci_dev)) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08006353 IWL_ERR(priv, "Fail to pci_enable_device\n");
Zhu Yi5a669262008-01-14 17:46:18 -08006354 return -ENODEV;
6355 }
6356 pci_restore_state(priv->pci_dev);
6357 pci_enable_msi(priv->pci_dev);
6358
6359 ret = request_irq(priv->pci_dev->irq, iwl3945_isr, IRQF_SHARED,
6360 DRV_NAME, priv);
6361 if (ret) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08006362 IWL_ERR(priv, "Error allocating IRQ %d\n", priv->pci_dev->irq);
Zhu Yi5a669262008-01-14 17:46:18 -08006363 goto out_disable_msi;
6364 }
6365
Zhu Yib481de92007-09-25 17:54:57 -07006366 /* we should be verifying the device is ready to be opened */
6367 mutex_lock(&priv->mutex);
6368
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08006369 memset(&priv->staging39_rxon, 0, sizeof(struct iwl3945_rxon_cmd));
Zhu Yi5a669262008-01-14 17:46:18 -08006370 /* fetch ucode file from disk, alloc and copy to bus-master buffers ...
6371 * ucode filename and max sizes are card-specific. */
6372
6373 if (!priv->ucode_code.len) {
6374 ret = iwl3945_read_ucode(priv);
6375 if (ret) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08006376 IWL_ERR(priv, "Could not read microcode: %d\n", ret);
Zhu Yi5a669262008-01-14 17:46:18 -08006377 mutex_unlock(&priv->mutex);
6378 goto out_release_irq;
6379 }
6380 }
6381
Zhu Yie655b9f2008-01-24 02:19:38 -08006382 ret = __iwl3945_up(priv);
Zhu Yi5a669262008-01-14 17:46:18 -08006383
Zhu Yib481de92007-09-25 17:54:57 -07006384 mutex_unlock(&priv->mutex);
Zhu Yi5a669262008-01-14 17:46:18 -08006385
Adel Gadllah80fcc9e2008-07-01 17:49:50 +02006386 iwl3945_rfkill_set_hw_state(priv);
6387
Zhu Yie655b9f2008-01-24 02:19:38 -08006388 if (ret)
6389 goto out_release_irq;
6390
6391 IWL_DEBUG_INFO("Start UP work.\n");
6392
6393 if (test_bit(STATUS_IN_SUSPEND, &priv->status))
6394 return 0;
6395
Zhu Yi5a669262008-01-14 17:46:18 -08006396 /* Wait for START_ALIVE from ucode. Otherwise callbacks from
6397 * mac80211 will not be run successfully. */
6398 ret = wait_event_interruptible_timeout(priv->wait_command_queue,
6399 test_bit(STATUS_READY, &priv->status),
6400 UCODE_READY_TIMEOUT);
6401 if (!ret) {
6402 if (!test_bit(STATUS_READY, &priv->status)) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08006403 IWL_ERR(priv,
6404 "Wait for START_ALIVE timeout after %dms.\n",
6405 jiffies_to_msecs(UCODE_READY_TIMEOUT));
Zhu Yi5a669262008-01-14 17:46:18 -08006406 ret = -ETIMEDOUT;
6407 goto out_release_irq;
6408 }
6409 }
6410
Zhu Yie655b9f2008-01-24 02:19:38 -08006411 priv->is_open = 1;
Zhu Yib481de92007-09-25 17:54:57 -07006412 IWL_DEBUG_MAC80211("leave\n");
6413 return 0;
Zhu Yi5a669262008-01-14 17:46:18 -08006414
6415out_release_irq:
6416 free_irq(priv->pci_dev->irq, priv);
6417out_disable_msi:
6418 pci_disable_msi(priv->pci_dev);
Zhu Yie655b9f2008-01-24 02:19:38 -08006419 pci_disable_device(priv->pci_dev);
6420 priv->is_open = 0;
6421 IWL_DEBUG_MAC80211("leave - failed\n");
Zhu Yi5a669262008-01-14 17:46:18 -08006422 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07006423}
6424
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006425static void iwl3945_mac_stop(struct ieee80211_hw *hw)
Zhu Yib481de92007-09-25 17:54:57 -07006426{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08006427 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07006428
6429 IWL_DEBUG_MAC80211("enter\n");
Mohamed Abbas6ef89d02007-10-25 17:15:47 +08006430
Zhu Yie655b9f2008-01-24 02:19:38 -08006431 if (!priv->is_open) {
6432 IWL_DEBUG_MAC80211("leave - skip\n");
6433 return;
6434 }
6435
Zhu Yib481de92007-09-25 17:54:57 -07006436 priv->is_open = 0;
Zhu Yi5a669262008-01-14 17:46:18 -08006437
6438 if (iwl3945_is_ready_rf(priv)) {
Zhu Yie655b9f2008-01-24 02:19:38 -08006439 /* stop mac, cancel any scan request and clear
6440 * RXON_FILTER_ASSOC_MSK BIT
6441 */
Zhu Yi5a669262008-01-14 17:46:18 -08006442 mutex_lock(&priv->mutex);
6443 iwl3945_scan_cancel_timeout(priv, 100);
Mohamed Abbasfde35712007-11-29 11:10:15 +08006444 mutex_unlock(&priv->mutex);
Mohamed Abbasfde35712007-11-29 11:10:15 +08006445 }
6446
Zhu Yi5a669262008-01-14 17:46:18 -08006447 iwl3945_down(priv);
6448
6449 flush_workqueue(priv->workqueue);
6450 free_irq(priv->pci_dev->irq, priv);
6451 pci_disable_msi(priv->pci_dev);
6452 pci_save_state(priv->pci_dev);
6453 pci_disable_device(priv->pci_dev);
Mohamed Abbas6ef89d02007-10-25 17:15:47 +08006454
Zhu Yib481de92007-09-25 17:54:57 -07006455 IWL_DEBUG_MAC80211("leave\n");
Zhu Yib481de92007-09-25 17:54:57 -07006456}
6457
Johannes Berge039fa42008-05-15 12:55:29 +02006458static int iwl3945_mac_tx(struct ieee80211_hw *hw, struct sk_buff *skb)
Zhu Yib481de92007-09-25 17:54:57 -07006459{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08006460 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07006461
6462 IWL_DEBUG_MAC80211("enter\n");
6463
Zhu Yib481de92007-09-25 17:54:57 -07006464 IWL_DEBUG_TX("dev->xmit(%d bytes) at rate 0x%02x\n", skb->len,
Johannes Berge039fa42008-05-15 12:55:29 +02006465 ieee80211_get_tx_rate(hw, IEEE80211_SKB_CB(skb))->bitrate);
Zhu Yib481de92007-09-25 17:54:57 -07006466
Johannes Berge039fa42008-05-15 12:55:29 +02006467 if (iwl3945_tx_skb(priv, skb))
Zhu Yib481de92007-09-25 17:54:57 -07006468 dev_kfree_skb_any(skb);
6469
6470 IWL_DEBUG_MAC80211("leave\n");
Reinette Chatre637f8832009-01-19 15:30:32 -08006471 return NETDEV_TX_OK;
Zhu Yib481de92007-09-25 17:54:57 -07006472}
6473
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006474static int iwl3945_mac_add_interface(struct ieee80211_hw *hw,
Zhu Yib481de92007-09-25 17:54:57 -07006475 struct ieee80211_if_init_conf *conf)
6476{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08006477 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07006478 unsigned long flags;
6479
Johannes Berg32bfd352007-12-19 01:31:26 +01006480 IWL_DEBUG_MAC80211("enter: type %d\n", conf->type);
Zhu Yib481de92007-09-25 17:54:57 -07006481
Johannes Berg32bfd352007-12-19 01:31:26 +01006482 if (priv->vif) {
6483 IWL_DEBUG_MAC80211("leave - vif != NULL\n");
Tomas Winkler864792e2007-11-27 21:00:52 +02006484 return -EOPNOTSUPP;
Zhu Yib481de92007-09-25 17:54:57 -07006485 }
6486
6487 spin_lock_irqsave(&priv->lock, flags);
Johannes Berg32bfd352007-12-19 01:31:26 +01006488 priv->vif = conf->vif;
Zhu, Yi60294de2008-10-29 14:05:45 -07006489 priv->iw_mode = conf->type;
Zhu Yib481de92007-09-25 17:54:57 -07006490
6491 spin_unlock_irqrestore(&priv->lock, flags);
6492
6493 mutex_lock(&priv->mutex);
Tomas Winkler864792e2007-11-27 21:00:52 +02006494
6495 if (conf->mac_addr) {
Johannes Berge1749612008-10-27 15:59:26 -07006496 IWL_DEBUG_MAC80211("Set: %pM\n", conf->mac_addr);
Tomas Winkler864792e2007-11-27 21:00:52 +02006497 memcpy(priv->mac_addr, conf->mac_addr, ETH_ALEN);
6498 }
6499
Zhu Yi5a669262008-01-14 17:46:18 -08006500 if (iwl3945_is_ready(priv))
6501 iwl3945_set_mode(priv, conf->type);
Zhu Yib481de92007-09-25 17:54:57 -07006502
Zhu Yib481de92007-09-25 17:54:57 -07006503 mutex_unlock(&priv->mutex);
6504
Zhu Yi5a669262008-01-14 17:46:18 -08006505 IWL_DEBUG_MAC80211("leave\n");
Zhu Yib481de92007-09-25 17:54:57 -07006506 return 0;
6507}
6508
6509/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006510 * iwl3945_mac_config - mac80211 config callback
Zhu Yib481de92007-09-25 17:54:57 -07006511 *
6512 * We ignore conf->flags & IEEE80211_CONF_SHORT_SLOT_TIME since it seems to
6513 * be set inappropriately and the driver currently sets the hardware up to
6514 * use it whenever needed.
6515 */
Johannes Berge8975582008-10-09 12:18:51 +02006516static int iwl3945_mac_config(struct ieee80211_hw *hw, u32 changed)
Zhu Yib481de92007-09-25 17:54:57 -07006517{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08006518 struct iwl_priv *priv = hw->priv;
Samuel Ortizd20b3c62008-12-19 10:37:15 +08006519 const struct iwl_channel_info *ch_info;
Johannes Berge8975582008-10-09 12:18:51 +02006520 struct ieee80211_conf *conf = &hw->conf;
Zhu Yib481de92007-09-25 17:54:57 -07006521 unsigned long flags;
Zhu Yi76bb77e2007-11-22 10:53:22 +08006522 int ret = 0;
Zhu Yib481de92007-09-25 17:54:57 -07006523
6524 mutex_lock(&priv->mutex);
Johannes Berg8318d782008-01-24 19:38:38 +01006525 IWL_DEBUG_MAC80211("enter to channel %d\n", conf->channel->hw_value);
Zhu Yib481de92007-09-25 17:54:57 -07006526
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006527 if (!iwl3945_is_ready(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07006528 IWL_DEBUG_MAC80211("leave - not ready\n");
Zhu Yi76bb77e2007-11-22 10:53:22 +08006529 ret = -EIO;
6530 goto out;
Zhu Yib481de92007-09-25 17:54:57 -07006531 }
6532
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006533 if (unlikely(!iwl3945_param_disable_hw_scan &&
Zhu Yib481de92007-09-25 17:54:57 -07006534 test_bit(STATUS_SCANNING, &priv->status))) {
Zhu Yia0646472007-12-20 14:10:01 +08006535 IWL_DEBUG_MAC80211("leave - scanning\n");
6536 set_bit(STATUS_CONF_PENDING, &priv->status);
Zhu Yib481de92007-09-25 17:54:57 -07006537 mutex_unlock(&priv->mutex);
Zhu Yia0646472007-12-20 14:10:01 +08006538 return 0;
Zhu Yib481de92007-09-25 17:54:57 -07006539 }
6540
6541 spin_lock_irqsave(&priv->lock, flags);
6542
Johannes Berg8318d782008-01-24 19:38:38 +01006543 ch_info = iwl3945_get_channel_info(priv, conf->channel->band,
6544 conf->channel->hw_value);
Zhu Yib481de92007-09-25 17:54:57 -07006545 if (!is_channel_valid(ch_info)) {
Ron Rindjunsky66b50042008-06-25 16:46:31 +08006546 IWL_DEBUG_SCAN("Channel %d [%d] is INVALID for this band.\n",
Johannes Berg8318d782008-01-24 19:38:38 +01006547 conf->channel->hw_value, conf->channel->band);
Zhu Yib481de92007-09-25 17:54:57 -07006548 IWL_DEBUG_MAC80211("leave - invalid channel\n");
6549 spin_unlock_irqrestore(&priv->lock, flags);
Zhu Yi76bb77e2007-11-22 10:53:22 +08006550 ret = -EINVAL;
6551 goto out;
Zhu Yib481de92007-09-25 17:54:57 -07006552 }
6553
Johannes Berg8318d782008-01-24 19:38:38 +01006554 iwl3945_set_rxon_channel(priv, conf->channel->band, conf->channel->hw_value);
Zhu Yib481de92007-09-25 17:54:57 -07006555
Johannes Berg8318d782008-01-24 19:38:38 +01006556 iwl3945_set_flags_for_phymode(priv, conf->channel->band);
Zhu Yib481de92007-09-25 17:54:57 -07006557
6558 /* The list of supported rates and rate mask can be different
6559 * for each phymode; since the phymode may have changed, reset
6560 * the rate mask to what mac80211 lists */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006561 iwl3945_set_rate(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006562
6563 spin_unlock_irqrestore(&priv->lock, flags);
6564
6565#ifdef IEEE80211_CONF_CHANNEL_SWITCH
6566 if (conf->flags & IEEE80211_CONF_CHANNEL_SWITCH) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006567 iwl3945_hw_channel_switch(priv, conf->channel);
Zhu Yi76bb77e2007-11-22 10:53:22 +08006568 goto out;
Zhu Yib481de92007-09-25 17:54:57 -07006569 }
6570#endif
6571
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006572 iwl3945_radio_kill_sw(priv, !conf->radio_enabled);
Zhu Yib481de92007-09-25 17:54:57 -07006573
6574 if (!conf->radio_enabled) {
6575 IWL_DEBUG_MAC80211("leave - radio disabled\n");
Zhu Yi76bb77e2007-11-22 10:53:22 +08006576 goto out;
Zhu Yib481de92007-09-25 17:54:57 -07006577 }
6578
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006579 if (iwl3945_is_rfkill(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07006580 IWL_DEBUG_MAC80211("leave - RF kill\n");
Zhu Yi76bb77e2007-11-22 10:53:22 +08006581 ret = -EIO;
6582 goto out;
Zhu Yib481de92007-09-25 17:54:57 -07006583 }
6584
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006585 iwl3945_set_rate(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006586
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08006587 if (memcmp(&priv->active39_rxon,
6588 &priv->staging39_rxon, sizeof(priv->staging39_rxon)))
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006589 iwl3945_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006590 else
6591 IWL_DEBUG_INFO("No re-sending same RXON configuration.\n");
6592
6593 IWL_DEBUG_MAC80211("leave\n");
6594
Zhu Yi76bb77e2007-11-22 10:53:22 +08006595out:
Zhu Yia0646472007-12-20 14:10:01 +08006596 clear_bit(STATUS_CONF_PENDING, &priv->status);
Zhu Yib481de92007-09-25 17:54:57 -07006597 mutex_unlock(&priv->mutex);
Zhu Yi76bb77e2007-11-22 10:53:22 +08006598 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07006599}
6600
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08006601static void iwl3945_config_ap(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07006602{
6603 int rc = 0;
6604
Maarten Lankhorstd986bcd2008-01-23 10:15:16 -08006605 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
Zhu Yib481de92007-09-25 17:54:57 -07006606 return;
6607
6608 /* The following should be done only at AP bring up */
Ron Rindjunsky5d1e2322008-06-30 17:23:04 +08006609 if (!(iwl3945_is_associated(priv))) {
Zhu Yib481de92007-09-25 17:54:57 -07006610
6611 /* RXON - unassoc (to set timing command) */
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08006612 priv->staging39_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006613 iwl3945_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006614
6615 /* RXON Timing */
Tomas Winkler28afaf92008-12-19 10:37:06 +08006616 memset(&priv->rxon_timing, 0, sizeof(struct iwl_rxon_time_cmd));
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006617 iwl3945_setup_rxon_timing(priv);
6618 rc = iwl3945_send_cmd_pdu(priv, REPLY_RXON_TIMING,
Zhu Yib481de92007-09-25 17:54:57 -07006619 sizeof(priv->rxon_timing), &priv->rxon_timing);
6620 if (rc)
Winkler, Tomas39aadf82008-12-19 10:37:32 +08006621 IWL_WARN(priv, "REPLY_RXON_TIMING failed - "
Zhu Yib481de92007-09-25 17:54:57 -07006622 "Attempting to continue.\n");
6623
6624 /* FIXME: what should be the assoc_id for AP? */
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08006625 priv->staging39_rxon.assoc_id = cpu_to_le16(priv->assoc_id);
Zhu Yib481de92007-09-25 17:54:57 -07006626 if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_PREAMBLE)
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08006627 priv->staging39_rxon.flags |=
Zhu Yib481de92007-09-25 17:54:57 -07006628 RXON_FLG_SHORT_PREAMBLE_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_PREAMBLE_MSK;
6632
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08006633 if (priv->staging39_rxon.flags & RXON_FLG_BAND_24G_MSK) {
Zhu Yib481de92007-09-25 17:54:57 -07006634 if (priv->assoc_capability &
6635 WLAN_CAPABILITY_SHORT_SLOT_TIME)
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08006636 priv->staging39_rxon.flags |=
Zhu Yib481de92007-09-25 17:54:57 -07006637 RXON_FLG_SHORT_SLOT_MSK;
6638 else
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08006639 priv->staging39_rxon.flags &=
Zhu Yib481de92007-09-25 17:54:57 -07006640 ~RXON_FLG_SHORT_SLOT_MSK;
6641
Johannes Berg05c914f2008-09-11 00:01:58 +02006642 if (priv->iw_mode == NL80211_IFTYPE_ADHOC)
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08006643 priv->staging39_rxon.flags &=
Zhu Yib481de92007-09-25 17:54:57 -07006644 ~RXON_FLG_SHORT_SLOT_MSK;
6645 }
6646 /* restore RXON assoc */
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08006647 priv->staging39_rxon.filter_flags |= RXON_FILTER_ASSOC_MSK;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006648 iwl3945_commit_rxon(priv);
Kolekar, Abhijeetb5323d32008-12-19 10:37:22 +08006649 iwl3945_add_station(priv, iwl_bcast_addr, 0, 0);
Zhu Yi556f8db2007-09-27 11:27:33 +08006650 }
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006651 iwl3945_send_beacon_cmd(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006652
6653 /* FIXME - we need to add code here to detect a totally new
6654 * configuration, reset the AP, unassoc, rxon timing, assoc,
6655 * clear sta table, add BCAST sta... */
6656}
6657
Johannes Berg32bfd352007-12-19 01:31:26 +01006658static int iwl3945_mac_config_interface(struct ieee80211_hw *hw,
6659 struct ieee80211_vif *vif,
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08006660 struct ieee80211_if_conf *conf)
Zhu Yib481de92007-09-25 17:54:57 -07006661{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08006662 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07006663 int rc;
6664
6665 if (conf == NULL)
6666 return -EIO;
6667
Emmanuel Grumbachb716bb92008-03-04 18:09:32 -08006668 if (priv->vif != vif) {
6669 IWL_DEBUG_MAC80211("leave - priv->vif != vif\n");
Emmanuel Grumbachb716bb92008-03-04 18:09:32 -08006670 return 0;
6671 }
6672
Johannes Berg9d139c82008-07-09 14:40:37 +02006673 /* handle this temporarily here */
Johannes Berg05c914f2008-09-11 00:01:58 +02006674 if (priv->iw_mode == NL80211_IFTYPE_ADHOC &&
Johannes Berg9d139c82008-07-09 14:40:37 +02006675 conf->changed & IEEE80211_IFCC_BEACON) {
6676 struct sk_buff *beacon = ieee80211_beacon_get(hw, vif);
6677 if (!beacon)
6678 return -ENOMEM;
Mohamed Abbas9bdf5ec2008-11-07 09:58:35 -08006679 mutex_lock(&priv->mutex);
Johannes Berg9d139c82008-07-09 14:40:37 +02006680 rc = iwl3945_mac_beacon_update(hw, beacon);
Mohamed Abbas9bdf5ec2008-11-07 09:58:35 -08006681 mutex_unlock(&priv->mutex);
Johannes Berg9d139c82008-07-09 14:40:37 +02006682 if (rc)
6683 return rc;
6684 }
6685
Zhu Yi5a669262008-01-14 17:46:18 -08006686 if (!iwl3945_is_alive(priv))
6687 return -EAGAIN;
6688
Zhu Yib481de92007-09-25 17:54:57 -07006689 mutex_lock(&priv->mutex);
6690
Zhu Yib481de92007-09-25 17:54:57 -07006691 if (conf->bssid)
Johannes Berge1749612008-10-27 15:59:26 -07006692 IWL_DEBUG_MAC80211("bssid: %pM\n", conf->bssid);
Zhu Yib481de92007-09-25 17:54:57 -07006693
Johannes Berg4150c572007-09-17 01:29:23 -04006694/*
6695 * very dubious code was here; the probe filtering flag is never set:
6696 *
Zhu Yib481de92007-09-25 17:54:57 -07006697 if (unlikely(test_bit(STATUS_SCANNING, &priv->status)) &&
6698 !(priv->hw->flags & IEEE80211_HW_NO_PROBE_FILTERING)) {
Johannes Berg4150c572007-09-17 01:29:23 -04006699 */
Zhu Yib481de92007-09-25 17:54:57 -07006700
Johannes Berg05c914f2008-09-11 00:01:58 +02006701 if (priv->iw_mode == NL80211_IFTYPE_AP) {
Zhu Yib481de92007-09-25 17:54:57 -07006702 if (!conf->bssid) {
6703 conf->bssid = priv->mac_addr;
6704 memcpy(priv->bssid, priv->mac_addr, ETH_ALEN);
Johannes Berge1749612008-10-27 15:59:26 -07006705 IWL_DEBUG_MAC80211("bssid was set to: %pM\n",
6706 conf->bssid);
Zhu Yib481de92007-09-25 17:54:57 -07006707 }
6708 if (priv->ibss_beacon)
6709 dev_kfree_skb(priv->ibss_beacon);
6710
Johannes Berg9d139c82008-07-09 14:40:37 +02006711 priv->ibss_beacon = ieee80211_beacon_get(hw, vif);
Zhu Yib481de92007-09-25 17:54:57 -07006712 }
6713
Mohamed Abbasfde35712007-11-29 11:10:15 +08006714 if (iwl3945_is_rfkill(priv))
6715 goto done;
6716
Zhu Yib481de92007-09-25 17:54:57 -07006717 if (conf->bssid && !is_zero_ether_addr(conf->bssid) &&
6718 !is_multicast_ether_addr(conf->bssid)) {
6719 /* If there is currently a HW scan going on in the background
6720 * then we need to cancel it else the RXON below will fail. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006721 if (iwl3945_scan_cancel_timeout(priv, 100)) {
Winkler, Tomas39aadf82008-12-19 10:37:32 +08006722 IWL_WARN(priv, "Aborted scan still in progress "
Zhu Yib481de92007-09-25 17:54:57 -07006723 "after 100ms\n");
6724 IWL_DEBUG_MAC80211("leaving - scan abort failed.\n");
6725 mutex_unlock(&priv->mutex);
6726 return -EAGAIN;
6727 }
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08006728 memcpy(priv->staging39_rxon.bssid_addr, conf->bssid, ETH_ALEN);
Zhu Yib481de92007-09-25 17:54:57 -07006729
6730 /* TODO: Audit driver for usage of these members and see
6731 * if mac80211 deprecates them (priv->bssid looks like it
6732 * shouldn't be there, but I haven't scanned the IBSS code
6733 * to verify) - jpk */
6734 memcpy(priv->bssid, conf->bssid, ETH_ALEN);
6735
Johannes Berg05c914f2008-09-11 00:01:58 +02006736 if (priv->iw_mode == NL80211_IFTYPE_AP)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006737 iwl3945_config_ap(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006738 else {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006739 rc = iwl3945_commit_rxon(priv);
Johannes Berg05c914f2008-09-11 00:01:58 +02006740 if ((priv->iw_mode == NL80211_IFTYPE_STATION) && rc)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006741 iwl3945_add_station(priv,
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08006742 priv->active39_rxon.bssid_addr, 1, 0);
Zhu Yib481de92007-09-25 17:54:57 -07006743 }
6744
6745 } else {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006746 iwl3945_scan_cancel_timeout(priv, 100);
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08006747 priv->staging39_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006748 iwl3945_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006749 }
6750
Mohamed Abbasfde35712007-11-29 11:10:15 +08006751 done:
Zhu Yib481de92007-09-25 17:54:57 -07006752 IWL_DEBUG_MAC80211("leave\n");
6753 mutex_unlock(&priv->mutex);
6754
6755 return 0;
6756}
6757
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006758static void iwl3945_configure_filter(struct ieee80211_hw *hw,
Johannes Berg4150c572007-09-17 01:29:23 -04006759 unsigned int changed_flags,
6760 unsigned int *total_flags,
6761 int mc_count, struct dev_addr_list *mc_list)
6762{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08006763 struct iwl_priv *priv = hw->priv;
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08006764 __le32 *filter_flags = &priv->staging39_rxon.filter_flags;
Rick Farrington25b3f572008-06-30 17:23:28 +08006765
Zhu, Yi352bc8d2008-11-12 13:14:09 -08006766 IWL_DEBUG_MAC80211("Enter: changed: 0x%x, total: 0x%x\n",
6767 changed_flags, *total_flags);
6768
6769 if (changed_flags & (FIF_OTHER_BSS | FIF_PROMISC_IN_BSS)) {
6770 if (*total_flags & (FIF_OTHER_BSS | FIF_PROMISC_IN_BSS))
6771 *filter_flags |= RXON_FILTER_PROMISC_MSK;
6772 else
6773 *filter_flags &= ~RXON_FILTER_PROMISC_MSK;
Abhijeet Kolekar5ec03972008-05-05 10:22:48 +08006774 }
Zhu, Yi352bc8d2008-11-12 13:14:09 -08006775 if (changed_flags & FIF_ALLMULTI) {
6776 if (*total_flags & FIF_ALLMULTI)
6777 *filter_flags |= RXON_FILTER_ACCEPT_GRP_MSK;
6778 else
6779 *filter_flags &= ~RXON_FILTER_ACCEPT_GRP_MSK;
6780 }
6781 if (changed_flags & FIF_CONTROL) {
6782 if (*total_flags & FIF_CONTROL)
6783 *filter_flags |= RXON_FILTER_CTL2HOST_MSK;
6784 else
6785 *filter_flags &= ~RXON_FILTER_CTL2HOST_MSK;
6786 }
6787 if (changed_flags & FIF_BCN_PRBRESP_PROMISC) {
6788 if (*total_flags & FIF_BCN_PRBRESP_PROMISC)
6789 *filter_flags |= RXON_FILTER_BCON_AWARE_MSK;
6790 else
6791 *filter_flags &= ~RXON_FILTER_BCON_AWARE_MSK;
6792 }
6793
6794 /* We avoid iwl_commit_rxon here to commit the new filter flags
6795 * since mac80211 will call ieee80211_hw_config immediately.
6796 * (mc_list is not supported at this time). Otherwise, we need to
6797 * queue a background iwl_commit_rxon work.
6798 */
6799
6800 *total_flags &= FIF_OTHER_BSS | FIF_ALLMULTI | FIF_PROMISC_IN_BSS |
Rick Farrington25b3f572008-06-30 17:23:28 +08006801 FIF_BCN_PRBRESP_PROMISC | FIF_CONTROL;
Johannes Berg4150c572007-09-17 01:29:23 -04006802}
6803
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006804static void iwl3945_mac_remove_interface(struct ieee80211_hw *hw,
Zhu Yib481de92007-09-25 17:54:57 -07006805 struct ieee80211_if_init_conf *conf)
6806{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08006807 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07006808
6809 IWL_DEBUG_MAC80211("enter\n");
6810
6811 mutex_lock(&priv->mutex);
Mohamed Abbas6ef89d02007-10-25 17:15:47 +08006812
Mohamed Abbasfde35712007-11-29 11:10:15 +08006813 if (iwl3945_is_ready_rf(priv)) {
6814 iwl3945_scan_cancel_timeout(priv, 100);
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08006815 priv->staging39_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
Mohamed Abbasfde35712007-11-29 11:10:15 +08006816 iwl3945_commit_rxon(priv);
6817 }
Johannes Berg32bfd352007-12-19 01:31:26 +01006818 if (priv->vif == conf->vif) {
6819 priv->vif = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07006820 memset(priv->bssid, 0, ETH_ALEN);
Zhu Yib481de92007-09-25 17:54:57 -07006821 }
6822 mutex_unlock(&priv->mutex);
6823
6824 IWL_DEBUG_MAC80211("leave\n");
Zhu Yib481de92007-09-25 17:54:57 -07006825}
6826
Abhijeet Kolekarcd56d332008-09-03 11:26:21 +08006827#define IWL_DELAY_NEXT_SCAN_AFTER_ASSOC (HZ*6)
6828
6829static void iwl3945_bss_info_changed(struct ieee80211_hw *hw,
6830 struct ieee80211_vif *vif,
6831 struct ieee80211_bss_conf *bss_conf,
6832 u32 changes)
6833{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08006834 struct iwl_priv *priv = hw->priv;
Abhijeet Kolekarcd56d332008-09-03 11:26:21 +08006835
6836 IWL_DEBUG_MAC80211("changes = 0x%X\n", changes);
6837
6838 if (changes & BSS_CHANGED_ERP_PREAMBLE) {
6839 IWL_DEBUG_MAC80211("ERP_PREAMBLE %d\n",
6840 bss_conf->use_short_preamble);
6841 if (bss_conf->use_short_preamble)
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08006842 priv->staging39_rxon.flags |= RXON_FLG_SHORT_PREAMBLE_MSK;
Abhijeet Kolekarcd56d332008-09-03 11:26:21 +08006843 else
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08006844 priv->staging39_rxon.flags &= ~RXON_FLG_SHORT_PREAMBLE_MSK;
Abhijeet Kolekarcd56d332008-09-03 11:26:21 +08006845 }
6846
6847 if (changes & BSS_CHANGED_ERP_CTS_PROT) {
6848 IWL_DEBUG_MAC80211("ERP_CTS %d\n", bss_conf->use_cts_prot);
6849 if (bss_conf->use_cts_prot && (priv->band != IEEE80211_BAND_5GHZ))
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08006850 priv->staging39_rxon.flags |= RXON_FLG_TGG_PROTECT_MSK;
Abhijeet Kolekarcd56d332008-09-03 11:26:21 +08006851 else
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08006852 priv->staging39_rxon.flags &= ~RXON_FLG_TGG_PROTECT_MSK;
Abhijeet Kolekarcd56d332008-09-03 11:26:21 +08006853 }
6854
6855 if (changes & BSS_CHANGED_ASSOC) {
6856 IWL_DEBUG_MAC80211("ASSOC %d\n", bss_conf->assoc);
6857 /* This should never happen as this function should
6858 * never be called from interrupt context. */
6859 if (WARN_ON_ONCE(in_interrupt()))
6860 return;
6861 if (bss_conf->assoc) {
6862 priv->assoc_id = bss_conf->aid;
6863 priv->beacon_int = bss_conf->beacon_int;
Tomas Winkler28afaf92008-12-19 10:37:06 +08006864 priv->timestamp = bss_conf->timestamp;
Abhijeet Kolekarcd56d332008-09-03 11:26:21 +08006865 priv->assoc_capability = bss_conf->assoc_capability;
6866 priv->next_scan_jiffies = jiffies +
6867 IWL_DELAY_NEXT_SCAN_AFTER_ASSOC;
6868 mutex_lock(&priv->mutex);
6869 iwl3945_post_associate(priv);
6870 mutex_unlock(&priv->mutex);
6871 } else {
6872 priv->assoc_id = 0;
6873 IWL_DEBUG_MAC80211("DISASSOC %d\n", bss_conf->assoc);
6874 }
6875 } else if (changes && iwl3945_is_associated(priv) && priv->assoc_id) {
6876 IWL_DEBUG_MAC80211("Associated Changes %d\n", changes);
6877 iwl3945_send_rxon_assoc(priv);
6878 }
6879
6880}
6881
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006882static int iwl3945_mac_hw_scan(struct ieee80211_hw *hw, u8 *ssid, size_t len)
Zhu Yib481de92007-09-25 17:54:57 -07006883{
6884 int rc = 0;
6885 unsigned long flags;
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08006886 struct iwl_priv *priv = hw->priv;
John W. Linville9387b7c2008-09-30 20:59:05 -04006887 DECLARE_SSID_BUF(ssid_buf);
Zhu Yib481de92007-09-25 17:54:57 -07006888
6889 IWL_DEBUG_MAC80211("enter\n");
6890
Mohamed Abbas15e869d2007-10-25 17:15:46 +08006891 mutex_lock(&priv->mutex);
Zhu Yib481de92007-09-25 17:54:57 -07006892 spin_lock_irqsave(&priv->lock, flags);
6893
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006894 if (!iwl3945_is_ready_rf(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07006895 rc = -EIO;
6896 IWL_DEBUG_MAC80211("leave - not ready or exit pending\n");
6897 goto out_unlock;
6898 }
6899
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08006900 /* we don't schedule scan within next_scan_jiffies period */
6901 if (priv->next_scan_jiffies &&
6902 time_after(priv->next_scan_jiffies, jiffies)) {
6903 rc = -EAGAIN;
6904 goto out_unlock;
6905 }
Bill Moss15dbf1b2008-05-06 11:05:15 +08006906 /* if we just finished scan ask for delay for a broadcast scan */
6907 if ((len == 0) && priv->last_scan_jiffies &&
6908 time_after(priv->last_scan_jiffies + IWL_DELAY_NEXT_SCAN,
6909 jiffies)) {
Zhu Yib481de92007-09-25 17:54:57 -07006910 rc = -EAGAIN;
6911 goto out_unlock;
6912 }
6913 if (len) {
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08006914 IWL_DEBUG_SCAN("direct scan for %s [%d]\n ",
John W. Linville9387b7c2008-09-30 20:59:05 -04006915 print_ssid(ssid_buf, ssid, len), (int)len);
Zhu Yib481de92007-09-25 17:54:57 -07006916
6917 priv->one_direct_scan = 1;
6918 priv->direct_ssid_len = (u8)
6919 min((u8) len, (u8) IW_ESSID_MAX_SIZE);
6920 memcpy(priv->direct_ssid, ssid, priv->direct_ssid_len);
Mohamed Abbas6ef89d02007-10-25 17:15:47 +08006921 } else
6922 priv->one_direct_scan = 0;
Zhu Yib481de92007-09-25 17:54:57 -07006923
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006924 rc = iwl3945_scan_initiate(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006925
6926 IWL_DEBUG_MAC80211("leave\n");
6927
6928out_unlock:
6929 spin_unlock_irqrestore(&priv->lock, flags);
Mohamed Abbas15e869d2007-10-25 17:15:46 +08006930 mutex_unlock(&priv->mutex);
Zhu Yib481de92007-09-25 17:54:57 -07006931
6932 return rc;
6933}
6934
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006935static int iwl3945_mac_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
Zhu Yib481de92007-09-25 17:54:57 -07006936 const u8 *local_addr, const u8 *addr,
6937 struct ieee80211_key_conf *key)
6938{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08006939 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07006940 int rc = 0;
6941 u8 sta_id;
6942
6943 IWL_DEBUG_MAC80211("enter\n");
6944
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006945 if (!iwl3945_param_hwcrypto) {
Zhu Yib481de92007-09-25 17:54:57 -07006946 IWL_DEBUG_MAC80211("leave - hwcrypto disabled\n");
6947 return -EOPNOTSUPP;
6948 }
6949
6950 if (is_zero_ether_addr(addr))
6951 /* only support pairwise keys */
6952 return -EOPNOTSUPP;
6953
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006954 sta_id = iwl3945_hw_find_station(priv, addr);
Zhu Yib481de92007-09-25 17:54:57 -07006955 if (sta_id == IWL_INVALID_STATION) {
Johannes Berge1749612008-10-27 15:59:26 -07006956 IWL_DEBUG_MAC80211("leave - %pM not in station map.\n",
6957 addr);
Zhu Yib481de92007-09-25 17:54:57 -07006958 return -EINVAL;
6959 }
6960
6961 mutex_lock(&priv->mutex);
6962
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006963 iwl3945_scan_cancel_timeout(priv, 100);
Mohamed Abbas15e869d2007-10-25 17:15:46 +08006964
Zhu Yib481de92007-09-25 17:54:57 -07006965 switch (cmd) {
6966 case SET_KEY:
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006967 rc = iwl3945_update_sta_key_info(priv, key, sta_id);
Zhu Yib481de92007-09-25 17:54:57 -07006968 if (!rc) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006969 iwl3945_set_rxon_hwcrypto(priv, 1);
6970 iwl3945_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006971 key->hw_key_idx = sta_id;
6972 IWL_DEBUG_MAC80211("set_key success, using hwcrypto\n");
6973 key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
6974 }
6975 break;
6976 case DISABLE_KEY:
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006977 rc = iwl3945_clear_sta_key_info(priv, sta_id);
Zhu Yib481de92007-09-25 17:54:57 -07006978 if (!rc) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006979 iwl3945_set_rxon_hwcrypto(priv, 0);
6980 iwl3945_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006981 IWL_DEBUG_MAC80211("disable hwcrypto key\n");
6982 }
6983 break;
6984 default:
6985 rc = -EINVAL;
6986 }
6987
6988 IWL_DEBUG_MAC80211("leave\n");
6989 mutex_unlock(&priv->mutex);
6990
6991 return rc;
6992}
6993
Johannes Berge100bb62008-04-30 18:51:21 +02006994static int iwl3945_mac_conf_tx(struct ieee80211_hw *hw, u16 queue,
Zhu Yib481de92007-09-25 17:54:57 -07006995 const struct ieee80211_tx_queue_params *params)
6996{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08006997 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07006998 unsigned long flags;
6999 int q;
Zhu Yib481de92007-09-25 17:54:57 -07007000
7001 IWL_DEBUG_MAC80211("enter\n");
7002
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007003 if (!iwl3945_is_ready_rf(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07007004 IWL_DEBUG_MAC80211("leave - RF not ready\n");
7005 return -EIO;
7006 }
7007
7008 if (queue >= AC_NUM) {
7009 IWL_DEBUG_MAC80211("leave - queue >= AC_NUM %d\n", queue);
7010 return 0;
7011 }
7012
Zhu Yib481de92007-09-25 17:54:57 -07007013 q = AC_NUM - 1 - queue;
7014
7015 spin_lock_irqsave(&priv->lock, flags);
7016
7017 priv->qos_data.def_qos_parm.ac[q].cw_min = cpu_to_le16(params->cw_min);
7018 priv->qos_data.def_qos_parm.ac[q].cw_max = cpu_to_le16(params->cw_max);
7019 priv->qos_data.def_qos_parm.ac[q].aifsn = params->aifs;
7020 priv->qos_data.def_qos_parm.ac[q].edca_txop =
Johannes Berg3330d7b2008-02-10 16:49:38 +01007021 cpu_to_le16((params->txop * 32));
Zhu Yib481de92007-09-25 17:54:57 -07007022
7023 priv->qos_data.def_qos_parm.ac[q].reserved1 = 0;
7024 priv->qos_data.qos_active = 1;
7025
7026 spin_unlock_irqrestore(&priv->lock, flags);
7027
7028 mutex_lock(&priv->mutex);
Johannes Berg05c914f2008-09-11 00:01:58 +02007029 if (priv->iw_mode == NL80211_IFTYPE_AP)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007030 iwl3945_activate_qos(priv, 1);
7031 else if (priv->assoc_id && iwl3945_is_associated(priv))
7032 iwl3945_activate_qos(priv, 0);
Zhu Yib481de92007-09-25 17:54:57 -07007033
7034 mutex_unlock(&priv->mutex);
7035
Zhu Yib481de92007-09-25 17:54:57 -07007036 IWL_DEBUG_MAC80211("leave\n");
7037 return 0;
7038}
7039
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007040static int iwl3945_mac_get_tx_stats(struct ieee80211_hw *hw,
Zhu Yib481de92007-09-25 17:54:57 -07007041 struct ieee80211_tx_queue_stats *stats)
7042{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08007043 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07007044 int i, avail;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007045 struct iwl3945_tx_queue *txq;
Samuel Ortizd20b3c62008-12-19 10:37:15 +08007046 struct iwl_queue *q;
Zhu Yib481de92007-09-25 17:54:57 -07007047 unsigned long flags;
7048
7049 IWL_DEBUG_MAC80211("enter\n");
7050
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007051 if (!iwl3945_is_ready_rf(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07007052 IWL_DEBUG_MAC80211("leave - RF not ready\n");
7053 return -EIO;
7054 }
7055
7056 spin_lock_irqsave(&priv->lock, flags);
7057
7058 for (i = 0; i < AC_NUM; i++) {
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08007059 txq = &priv->txq39[i];
Zhu Yib481de92007-09-25 17:54:57 -07007060 q = &txq->q;
Samuel Ortizd20b3c62008-12-19 10:37:15 +08007061 avail = iwl_queue_space(q);
Zhu Yib481de92007-09-25 17:54:57 -07007062
Johannes Berg57ffc582008-04-29 17:18:59 +02007063 stats[i].len = q->n_window - avail;
7064 stats[i].limit = q->n_window - q->high_mark;
7065 stats[i].count = q->n_window;
Zhu Yib481de92007-09-25 17:54:57 -07007066
7067 }
7068 spin_unlock_irqrestore(&priv->lock, flags);
7069
7070 IWL_DEBUG_MAC80211("leave\n");
7071
7072 return 0;
7073}
7074
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007075static void iwl3945_mac_reset_tsf(struct ieee80211_hw *hw)
Zhu Yib481de92007-09-25 17:54:57 -07007076{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08007077 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07007078 unsigned long flags;
7079
7080 mutex_lock(&priv->mutex);
7081 IWL_DEBUG_MAC80211("enter\n");
7082
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007083 iwl3945_reset_qos(priv);
Ron Rindjunsky292ae172008-02-06 11:20:39 -08007084
Zhu Yib481de92007-09-25 17:54:57 -07007085 spin_lock_irqsave(&priv->lock, flags);
7086 priv->assoc_id = 0;
7087 priv->assoc_capability = 0;
7088 priv->call_post_assoc_from_beacon = 0;
7089
7090 /* new association get rid of ibss beacon skb */
7091 if (priv->ibss_beacon)
7092 dev_kfree_skb(priv->ibss_beacon);
7093
7094 priv->ibss_beacon = NULL;
7095
7096 priv->beacon_int = priv->hw->conf.beacon_int;
Tomas Winkler28afaf92008-12-19 10:37:06 +08007097 priv->timestamp = 0;
Johannes Berg05c914f2008-09-11 00:01:58 +02007098 if ((priv->iw_mode == NL80211_IFTYPE_STATION))
Zhu Yib481de92007-09-25 17:54:57 -07007099 priv->beacon_int = 0;
7100
7101 spin_unlock_irqrestore(&priv->lock, flags);
7102
Mohamed Abbasfde35712007-11-29 11:10:15 +08007103 if (!iwl3945_is_ready_rf(priv)) {
7104 IWL_DEBUG_MAC80211("leave - not ready\n");
7105 mutex_unlock(&priv->mutex);
7106 return;
7107 }
7108
Mohamed Abbas15e869d2007-10-25 17:15:46 +08007109 /* we are restarting association process
7110 * clear RXON_FILTER_ASSOC_MSK bit
7111 */
Johannes Berg05c914f2008-09-11 00:01:58 +02007112 if (priv->iw_mode != NL80211_IFTYPE_AP) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007113 iwl3945_scan_cancel_timeout(priv, 100);
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08007114 priv->staging39_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007115 iwl3945_commit_rxon(priv);
Mohamed Abbas15e869d2007-10-25 17:15:46 +08007116 }
7117
Zhu Yib481de92007-09-25 17:54:57 -07007118 /* Per mac80211.h: This is only used in IBSS mode... */
Johannes Berg05c914f2008-09-11 00:01:58 +02007119 if (priv->iw_mode != NL80211_IFTYPE_ADHOC) {
Mohamed Abbas15e869d2007-10-25 17:15:46 +08007120
Zhu Yib481de92007-09-25 17:54:57 -07007121 IWL_DEBUG_MAC80211("leave - not in IBSS\n");
7122 mutex_unlock(&priv->mutex);
7123 return;
7124 }
7125
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007126 iwl3945_set_rate(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007127
7128 mutex_unlock(&priv->mutex);
7129
7130 IWL_DEBUG_MAC80211("leave\n");
7131
7132}
7133
Johannes Berge039fa42008-05-15 12:55:29 +02007134static int iwl3945_mac_beacon_update(struct ieee80211_hw *hw, struct sk_buff *skb)
Zhu Yib481de92007-09-25 17:54:57 -07007135{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08007136 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07007137 unsigned long flags;
7138
Zhu Yib481de92007-09-25 17:54:57 -07007139 IWL_DEBUG_MAC80211("enter\n");
7140
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007141 if (!iwl3945_is_ready_rf(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07007142 IWL_DEBUG_MAC80211("leave - RF not ready\n");
Zhu Yib481de92007-09-25 17:54:57 -07007143 return -EIO;
7144 }
7145
Johannes Berg05c914f2008-09-11 00:01:58 +02007146 if (priv->iw_mode != NL80211_IFTYPE_ADHOC) {
Zhu Yib481de92007-09-25 17:54:57 -07007147 IWL_DEBUG_MAC80211("leave - not IBSS\n");
Zhu Yib481de92007-09-25 17:54:57 -07007148 return -EIO;
7149 }
7150
7151 spin_lock_irqsave(&priv->lock, flags);
7152
7153 if (priv->ibss_beacon)
7154 dev_kfree_skb(priv->ibss_beacon);
7155
7156 priv->ibss_beacon = skb;
7157
7158 priv->assoc_id = 0;
7159
7160 IWL_DEBUG_MAC80211("leave\n");
7161 spin_unlock_irqrestore(&priv->lock, flags);
7162
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007163 iwl3945_reset_qos(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007164
Abhijeet Kolekardc4b1e72008-09-03 11:26:30 +08007165 iwl3945_post_associate(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007166
Zhu Yib481de92007-09-25 17:54:57 -07007167
7168 return 0;
7169}
7170
7171/*****************************************************************************
7172 *
7173 * sysfs attributes
7174 *
7175 *****************************************************************************/
7176
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08007177#ifdef CONFIG_IWL3945_DEBUG
Zhu Yib481de92007-09-25 17:54:57 -07007178
7179/*
7180 * The following adds a new attribute to the sysfs representation
7181 * of this device driver (i.e. a new file in /sys/bus/pci/drivers/iwl/)
7182 * used for controlling the debug level.
7183 *
7184 * See the level definitions in iwl for details.
7185 */
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08007186static ssize_t show_debug_level(struct device *d,
7187 struct device_attribute *attr, char *buf)
Zhu Yib481de92007-09-25 17:54:57 -07007188{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08007189 struct iwl_priv *priv = d->driver_data;
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08007190
7191 return sprintf(buf, "0x%08X\n", priv->debug_level);
Zhu Yib481de92007-09-25 17:54:57 -07007192}
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08007193static ssize_t store_debug_level(struct device *d,
7194 struct device_attribute *attr,
Zhu Yib481de92007-09-25 17:54:57 -07007195 const char *buf, size_t count)
7196{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08007197 struct iwl_priv *priv = d->driver_data;
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08007198 unsigned long val;
7199 int ret;
Zhu Yib481de92007-09-25 17:54:57 -07007200
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08007201 ret = strict_strtoul(buf, 0, &val);
7202 if (ret)
Tomas Winkler978785a2008-12-19 10:37:31 +08007203 IWL_INFO(priv, "%s is not in hex or decimal form.\n", buf);
Zhu Yib481de92007-09-25 17:54:57 -07007204 else
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08007205 priv->debug_level = val;
Zhu Yib481de92007-09-25 17:54:57 -07007206
7207 return strnlen(buf, count);
7208}
7209
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08007210static DEVICE_ATTR(debug_level, S_IWUSR | S_IRUGO,
7211 show_debug_level, store_debug_level);
Zhu Yib481de92007-09-25 17:54:57 -07007212
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08007213#endif /* CONFIG_IWL3945_DEBUG */
Zhu Yib481de92007-09-25 17:54:57 -07007214
Zhu Yib481de92007-09-25 17:54:57 -07007215static ssize_t show_temperature(struct device *d,
7216 struct device_attribute *attr, char *buf)
7217{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08007218 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07007219
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007220 if (!iwl3945_is_alive(priv))
Zhu Yib481de92007-09-25 17:54:57 -07007221 return -EAGAIN;
7222
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007223 return sprintf(buf, "%d\n", iwl3945_hw_get_temperature(priv));
Zhu Yib481de92007-09-25 17:54:57 -07007224}
7225
7226static DEVICE_ATTR(temperature, S_IRUGO, show_temperature, NULL);
7227
Zhu Yib481de92007-09-25 17:54:57 -07007228static ssize_t show_tx_power(struct device *d,
7229 struct device_attribute *attr, char *buf)
7230{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08007231 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07007232 return sprintf(buf, "%d\n", priv->user_txpower_limit);
7233}
7234
7235static ssize_t store_tx_power(struct device *d,
7236 struct device_attribute *attr,
7237 const char *buf, size_t count)
7238{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08007239 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07007240 char *p = (char *)buf;
7241 u32 val;
7242
7243 val = simple_strtoul(p, &p, 10);
7244 if (p == buf)
Tomas Winkler978785a2008-12-19 10:37:31 +08007245 IWL_INFO(priv, ": %s is not in decimal form.\n", buf);
Zhu Yib481de92007-09-25 17:54:57 -07007246 else
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007247 iwl3945_hw_reg_set_txpower(priv, val);
Zhu Yib481de92007-09-25 17:54:57 -07007248
7249 return count;
7250}
7251
7252static DEVICE_ATTR(tx_power, S_IWUSR | S_IRUGO, show_tx_power, store_tx_power);
7253
7254static ssize_t show_flags(struct device *d,
7255 struct device_attribute *attr, char *buf)
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
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08007259 return sprintf(buf, "0x%04X\n", priv->active39_rxon.flags);
Zhu Yib481de92007-09-25 17:54:57 -07007260}
7261
7262static ssize_t store_flags(struct device *d,
7263 struct device_attribute *attr,
7264 const char *buf, size_t count)
7265{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08007266 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07007267 u32 flags = simple_strtoul(buf, NULL, 0);
7268
7269 mutex_lock(&priv->mutex);
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08007270 if (le32_to_cpu(priv->staging39_rxon.flags) != flags) {
Zhu Yib481de92007-09-25 17:54:57 -07007271 /* Cancel any currently running scans... */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007272 if (iwl3945_scan_cancel_timeout(priv, 100))
Winkler, Tomas39aadf82008-12-19 10:37:32 +08007273 IWL_WARN(priv, "Could not cancel scan.\n");
Zhu Yib481de92007-09-25 17:54:57 -07007274 else {
7275 IWL_DEBUG_INFO("Committing rxon.flags = 0x%04X\n",
7276 flags);
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08007277 priv->staging39_rxon.flags = cpu_to_le32(flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007278 iwl3945_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007279 }
7280 }
7281 mutex_unlock(&priv->mutex);
7282
7283 return count;
7284}
7285
7286static DEVICE_ATTR(flags, S_IWUSR | S_IRUGO, show_flags, store_flags);
7287
7288static ssize_t show_filter_flags(struct device *d,
7289 struct device_attribute *attr, char *buf)
7290{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08007291 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07007292
7293 return sprintf(buf, "0x%04X\n",
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08007294 le32_to_cpu(priv->active39_rxon.filter_flags));
Zhu Yib481de92007-09-25 17:54:57 -07007295}
7296
7297static ssize_t store_filter_flags(struct device *d,
7298 struct device_attribute *attr,
7299 const char *buf, size_t count)
7300{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08007301 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07007302 u32 filter_flags = simple_strtoul(buf, NULL, 0);
7303
7304 mutex_lock(&priv->mutex);
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08007305 if (le32_to_cpu(priv->staging39_rxon.filter_flags) != filter_flags) {
Zhu Yib481de92007-09-25 17:54:57 -07007306 /* Cancel any currently running scans... */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007307 if (iwl3945_scan_cancel_timeout(priv, 100))
Winkler, Tomas39aadf82008-12-19 10:37:32 +08007308 IWL_WARN(priv, "Could not cancel scan.\n");
Zhu Yib481de92007-09-25 17:54:57 -07007309 else {
7310 IWL_DEBUG_INFO("Committing rxon.filter_flags = "
7311 "0x%04X\n", filter_flags);
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08007312 priv->staging39_rxon.filter_flags =
Zhu Yib481de92007-09-25 17:54:57 -07007313 cpu_to_le32(filter_flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007314 iwl3945_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007315 }
7316 }
7317 mutex_unlock(&priv->mutex);
7318
7319 return count;
7320}
7321
7322static DEVICE_ATTR(filter_flags, S_IWUSR | S_IRUGO, show_filter_flags,
7323 store_filter_flags);
7324
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08007325#ifdef CONFIG_IWL3945_SPECTRUM_MEASUREMENT
Zhu Yib481de92007-09-25 17:54:57 -07007326
7327static ssize_t show_measurement(struct device *d,
7328 struct device_attribute *attr, char *buf)
7329{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08007330 struct iwl_priv *priv = dev_get_drvdata(d);
Tomas Winkler600c0e12008-12-19 10:37:04 +08007331 struct iwl_spectrum_notification measure_report;
Zhu Yib481de92007-09-25 17:54:57 -07007332 u32 size = sizeof(measure_report), len = 0, ofs = 0;
Tomas Winkler3ac7f142008-07-21 02:40:14 +03007333 u8 *data = (u8 *)&measure_report;
Zhu Yib481de92007-09-25 17:54:57 -07007334 unsigned long flags;
7335
7336 spin_lock_irqsave(&priv->lock, flags);
7337 if (!(priv->measurement_status & MEASUREMENT_READY)) {
7338 spin_unlock_irqrestore(&priv->lock, flags);
7339 return 0;
7340 }
7341 memcpy(&measure_report, &priv->measure_report, size);
7342 priv->measurement_status = 0;
7343 spin_unlock_irqrestore(&priv->lock, flags);
7344
7345 while (size && (PAGE_SIZE - len)) {
7346 hex_dump_to_buffer(data + ofs, size, 16, 1, buf + len,
7347 PAGE_SIZE - len, 1);
7348 len = strlen(buf);
7349 if (PAGE_SIZE - len)
7350 buf[len++] = '\n';
7351
7352 ofs += 16;
7353 size -= min(size, 16U);
7354 }
7355
7356 return len;
7357}
7358
7359static ssize_t store_measurement(struct device *d,
7360 struct device_attribute *attr,
7361 const char *buf, size_t count)
7362{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08007363 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07007364 struct ieee80211_measurement_params params = {
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08007365 .channel = le16_to_cpu(priv->active39_rxon.channel),
Zhu Yib481de92007-09-25 17:54:57 -07007366 .start_time = cpu_to_le64(priv->last_tsf),
7367 .duration = cpu_to_le16(1),
7368 };
7369 u8 type = IWL_MEASURE_BASIC;
7370 u8 buffer[32];
7371 u8 channel;
7372
7373 if (count) {
7374 char *p = buffer;
7375 strncpy(buffer, buf, min(sizeof(buffer), count));
7376 channel = simple_strtoul(p, NULL, 0);
7377 if (channel)
7378 params.channel = channel;
7379
7380 p = buffer;
7381 while (*p && *p != ' ')
7382 p++;
7383 if (*p)
7384 type = simple_strtoul(p + 1, NULL, 0);
7385 }
7386
7387 IWL_DEBUG_INFO("Invoking measurement of type %d on "
7388 "channel %d (for '%s')\n", type, params.channel, buf);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007389 iwl3945_get_measurement(priv, &params, type);
Zhu Yib481de92007-09-25 17:54:57 -07007390
7391 return count;
7392}
7393
7394static DEVICE_ATTR(measurement, S_IRUSR | S_IWUSR,
7395 show_measurement, store_measurement);
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08007396#endif /* CONFIG_IWL3945_SPECTRUM_MEASUREMENT */
Zhu Yib481de92007-09-25 17:54:57 -07007397
Zhu Yib481de92007-09-25 17:54:57 -07007398static ssize_t store_retry_rate(struct device *d,
7399 struct device_attribute *attr,
7400 const char *buf, size_t count)
7401{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08007402 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07007403
7404 priv->retry_rate = simple_strtoul(buf, NULL, 0);
7405 if (priv->retry_rate <= 0)
7406 priv->retry_rate = 1;
7407
7408 return count;
7409}
7410
7411static ssize_t show_retry_rate(struct device *d,
7412 struct device_attribute *attr, char *buf)
7413{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08007414 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07007415 return sprintf(buf, "%d", priv->retry_rate);
7416}
7417
7418static DEVICE_ATTR(retry_rate, S_IWUSR | S_IRUSR, show_retry_rate,
7419 store_retry_rate);
7420
7421static ssize_t store_power_level(struct device *d,
7422 struct device_attribute *attr,
7423 const char *buf, size_t count)
7424{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08007425 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07007426 int rc;
7427 int mode;
7428
7429 mode = simple_strtoul(buf, NULL, 0);
7430 mutex_lock(&priv->mutex);
7431
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007432 if (!iwl3945_is_ready(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07007433 rc = -EAGAIN;
7434 goto out;
7435 }
7436
Samuel Ortiz1125eff2008-12-19 10:37:14 +08007437 if ((mode < 1) || (mode > IWL39_POWER_LIMIT) ||
7438 (mode == IWL39_POWER_AC))
7439 mode = IWL39_POWER_AC;
Zhu Yib481de92007-09-25 17:54:57 -07007440 else
7441 mode |= IWL_POWER_ENABLED;
7442
7443 if (mode != priv->power_mode) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007444 rc = iwl3945_send_power_mode(priv, IWL_POWER_LEVEL(mode));
Zhu Yib481de92007-09-25 17:54:57 -07007445 if (rc) {
7446 IWL_DEBUG_MAC80211("failed setting power mode.\n");
7447 goto out;
7448 }
7449 priv->power_mode = mode;
7450 }
7451
7452 rc = count;
7453
7454 out:
7455 mutex_unlock(&priv->mutex);
7456 return rc;
7457}
7458
7459#define MAX_WX_STRING 80
7460
7461/* Values are in microsecond */
7462static const s32 timeout_duration[] = {
7463 350000,
7464 250000,
7465 75000,
7466 37000,
7467 25000,
7468};
7469static const s32 period_duration[] = {
7470 400000,
7471 700000,
7472 1000000,
7473 1000000,
7474 1000000
7475};
7476
7477static ssize_t show_power_level(struct device *d,
7478 struct device_attribute *attr, char *buf)
7479{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08007480 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07007481 int level = IWL_POWER_LEVEL(priv->power_mode);
7482 char *p = buf;
7483
7484 p += sprintf(p, "%d ", level);
7485 switch (level) {
7486 case IWL_POWER_MODE_CAM:
Samuel Ortiz1125eff2008-12-19 10:37:14 +08007487 case IWL39_POWER_AC:
Zhu Yib481de92007-09-25 17:54:57 -07007488 p += sprintf(p, "(AC)");
7489 break;
Samuel Ortiz1125eff2008-12-19 10:37:14 +08007490 case IWL39_POWER_BATTERY:
Zhu Yib481de92007-09-25 17:54:57 -07007491 p += sprintf(p, "(BATTERY)");
7492 break;
7493 default:
7494 p += sprintf(p,
7495 "(Timeout %dms, Period %dms)",
7496 timeout_duration[level - 1] / 1000,
7497 period_duration[level - 1] / 1000);
7498 }
7499
7500 if (!(priv->power_mode & IWL_POWER_ENABLED))
7501 p += sprintf(p, " OFF\n");
7502 else
7503 p += sprintf(p, " \n");
7504
Tomas Winkler3ac7f142008-07-21 02:40:14 +03007505 return p - buf + 1;
Zhu Yib481de92007-09-25 17:54:57 -07007506
7507}
7508
7509static DEVICE_ATTR(power_level, S_IWUSR | S_IRUSR, show_power_level,
7510 store_power_level);
7511
7512static ssize_t show_channels(struct device *d,
7513 struct device_attribute *attr, char *buf)
7514{
Johannes Berg8318d782008-01-24 19:38:38 +01007515 /* all this shit doesn't belong into sysfs anyway */
7516 return 0;
Zhu Yib481de92007-09-25 17:54:57 -07007517}
7518
7519static DEVICE_ATTR(channels, S_IRUSR, show_channels, NULL);
7520
7521static ssize_t show_statistics(struct device *d,
7522 struct device_attribute *attr, char *buf)
7523{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08007524 struct iwl_priv *priv = dev_get_drvdata(d);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007525 u32 size = sizeof(struct iwl3945_notif_statistics);
Zhu Yib481de92007-09-25 17:54:57 -07007526 u32 len = 0, ofs = 0;
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08007527 u8 *data = (u8 *)&priv->statistics_39;
Zhu Yib481de92007-09-25 17:54:57 -07007528 int rc = 0;
7529
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007530 if (!iwl3945_is_alive(priv))
Zhu Yib481de92007-09-25 17:54:57 -07007531 return -EAGAIN;
7532
7533 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007534 rc = iwl3945_send_statistics_request(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007535 mutex_unlock(&priv->mutex);
7536
7537 if (rc) {
7538 len = sprintf(buf,
7539 "Error sending statistics request: 0x%08X\n", rc);
7540 return len;
7541 }
7542
7543 while (size && (PAGE_SIZE - len)) {
7544 hex_dump_to_buffer(data + ofs, size, 16, 1, buf + len,
7545 PAGE_SIZE - len, 1);
7546 len = strlen(buf);
7547 if (PAGE_SIZE - len)
7548 buf[len++] = '\n';
7549
7550 ofs += 16;
7551 size -= min(size, 16U);
7552 }
7553
7554 return len;
7555}
7556
7557static DEVICE_ATTR(statistics, S_IRUGO, show_statistics, NULL);
7558
7559static ssize_t show_antenna(struct device *d,
7560 struct device_attribute *attr, char *buf)
7561{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08007562 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07007563
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007564 if (!iwl3945_is_alive(priv))
Zhu Yib481de92007-09-25 17:54:57 -07007565 return -EAGAIN;
7566
7567 return sprintf(buf, "%d\n", priv->antenna);
7568}
7569
7570static ssize_t store_antenna(struct device *d,
7571 struct device_attribute *attr,
7572 const char *buf, size_t count)
7573{
7574 int ant;
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08007575 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07007576
7577 if (count == 0)
7578 return 0;
7579
7580 if (sscanf(buf, "%1i", &ant) != 1) {
7581 IWL_DEBUG_INFO("not in hex or decimal form.\n");
7582 return count;
7583 }
7584
7585 if ((ant >= 0) && (ant <= 2)) {
7586 IWL_DEBUG_INFO("Setting antenna select to %d.\n", ant);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007587 priv->antenna = (enum iwl3945_antenna)ant;
Zhu Yib481de92007-09-25 17:54:57 -07007588 } else
7589 IWL_DEBUG_INFO("Bad antenna select value %d.\n", ant);
7590
7591
7592 return count;
7593}
7594
7595static DEVICE_ATTR(antenna, S_IWUSR | S_IRUGO, show_antenna, store_antenna);
7596
7597static ssize_t show_status(struct device *d,
7598 struct device_attribute *attr, char *buf)
7599{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08007600 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007601 if (!iwl3945_is_alive(priv))
Zhu Yib481de92007-09-25 17:54:57 -07007602 return -EAGAIN;
7603 return sprintf(buf, "0x%08x\n", (int)priv->status);
7604}
7605
7606static DEVICE_ATTR(status, S_IRUGO, show_status, NULL);
7607
7608static ssize_t dump_error_log(struct device *d,
7609 struct device_attribute *attr,
7610 const char *buf, size_t count)
7611{
7612 char *p = (char *)buf;
7613
7614 if (p[0] == '1')
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08007615 iwl3945_dump_nic_error_log((struct iwl_priv *)d->driver_data);
Zhu Yib481de92007-09-25 17:54:57 -07007616
7617 return strnlen(buf, count);
7618}
7619
7620static DEVICE_ATTR(dump_errors, S_IWUSR, NULL, dump_error_log);
7621
7622static ssize_t dump_event_log(struct device *d,
7623 struct device_attribute *attr,
7624 const char *buf, size_t count)
7625{
7626 char *p = (char *)buf;
7627
7628 if (p[0] == '1')
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08007629 iwl3945_dump_nic_event_log((struct iwl_priv *)d->driver_data);
Zhu Yib481de92007-09-25 17:54:57 -07007630
7631 return strnlen(buf, count);
7632}
7633
7634static DEVICE_ATTR(dump_events, S_IWUSR, NULL, dump_event_log);
7635
7636/*****************************************************************************
7637 *
Tomas Winklera96a27f2008-10-23 23:48:56 -07007638 * driver setup and tear down
Zhu Yib481de92007-09-25 17:54:57 -07007639 *
7640 *****************************************************************************/
7641
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08007642static void iwl3945_setup_deferred_work(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07007643{
7644 priv->workqueue = create_workqueue(DRV_NAME);
7645
7646 init_waitqueue_head(&priv->wait_command_queue);
7647
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007648 INIT_WORK(&priv->up, iwl3945_bg_up);
7649 INIT_WORK(&priv->restart, iwl3945_bg_restart);
7650 INIT_WORK(&priv->rx_replenish, iwl3945_bg_rx_replenish);
7651 INIT_WORK(&priv->scan_completed, iwl3945_bg_scan_completed);
7652 INIT_WORK(&priv->request_scan, iwl3945_bg_request_scan);
7653 INIT_WORK(&priv->abort_scan, iwl3945_bg_abort_scan);
7654 INIT_WORK(&priv->rf_kill, iwl3945_bg_rf_kill);
7655 INIT_WORK(&priv->beacon_update, iwl3945_bg_beacon_update);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007656 INIT_DELAYED_WORK(&priv->init_alive_start, iwl3945_bg_init_alive_start);
7657 INIT_DELAYED_WORK(&priv->alive_start, iwl3945_bg_alive_start);
7658 INIT_DELAYED_WORK(&priv->scan_check, iwl3945_bg_scan_check);
Zhu Yib481de92007-09-25 17:54:57 -07007659
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007660 iwl3945_hw_setup_deferred_work(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007661
7662 tasklet_init(&priv->irq_tasklet, (void (*)(unsigned long))
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007663 iwl3945_irq_tasklet, (unsigned long)priv);
Zhu Yib481de92007-09-25 17:54:57 -07007664}
7665
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08007666static void iwl3945_cancel_deferred_work(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07007667{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007668 iwl3945_hw_cancel_deferred_work(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007669
Joonwoo Parke47eb6a2007-11-29 10:42:49 +09007670 cancel_delayed_work_sync(&priv->init_alive_start);
Zhu Yib481de92007-09-25 17:54:57 -07007671 cancel_delayed_work(&priv->scan_check);
7672 cancel_delayed_work(&priv->alive_start);
Zhu Yib481de92007-09-25 17:54:57 -07007673 cancel_work_sync(&priv->beacon_update);
7674}
7675
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007676static struct attribute *iwl3945_sysfs_entries[] = {
Zhu Yib481de92007-09-25 17:54:57 -07007677 &dev_attr_antenna.attr,
7678 &dev_attr_channels.attr,
7679 &dev_attr_dump_errors.attr,
7680 &dev_attr_dump_events.attr,
7681 &dev_attr_flags.attr,
7682 &dev_attr_filter_flags.attr,
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08007683#ifdef CONFIG_IWL3945_SPECTRUM_MEASUREMENT
Zhu Yib481de92007-09-25 17:54:57 -07007684 &dev_attr_measurement.attr,
7685#endif
7686 &dev_attr_power_level.attr,
Zhu Yib481de92007-09-25 17:54:57 -07007687 &dev_attr_retry_rate.attr,
Zhu Yib481de92007-09-25 17:54:57 -07007688 &dev_attr_statistics.attr,
7689 &dev_attr_status.attr,
7690 &dev_attr_temperature.attr,
Zhu Yib481de92007-09-25 17:54:57 -07007691 &dev_attr_tx_power.attr,
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08007692#ifdef CONFIG_IWL3945_DEBUG
7693 &dev_attr_debug_level.attr,
7694#endif
Zhu Yib481de92007-09-25 17:54:57 -07007695 NULL
7696};
7697
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007698static struct attribute_group iwl3945_attribute_group = {
Zhu Yib481de92007-09-25 17:54:57 -07007699 .name = NULL, /* put in device directory */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007700 .attrs = iwl3945_sysfs_entries,
Zhu Yib481de92007-09-25 17:54:57 -07007701};
7702
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007703static struct ieee80211_ops iwl3945_hw_ops = {
7704 .tx = iwl3945_mac_tx,
7705 .start = iwl3945_mac_start,
7706 .stop = iwl3945_mac_stop,
7707 .add_interface = iwl3945_mac_add_interface,
7708 .remove_interface = iwl3945_mac_remove_interface,
7709 .config = iwl3945_mac_config,
7710 .config_interface = iwl3945_mac_config_interface,
7711 .configure_filter = iwl3945_configure_filter,
7712 .set_key = iwl3945_mac_set_key,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007713 .get_tx_stats = iwl3945_mac_get_tx_stats,
7714 .conf_tx = iwl3945_mac_conf_tx,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007715 .reset_tsf = iwl3945_mac_reset_tsf,
Abhijeet Kolekarcd56d332008-09-03 11:26:21 +08007716 .bss_info_changed = iwl3945_bss_info_changed,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007717 .hw_scan = iwl3945_mac_hw_scan
Zhu Yib481de92007-09-25 17:54:57 -07007718};
7719
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007720static int iwl3945_pci_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
Zhu Yib481de92007-09-25 17:54:57 -07007721{
7722 int err = 0;
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08007723 struct iwl_priv *priv;
Zhu Yib481de92007-09-25 17:54:57 -07007724 struct ieee80211_hw *hw;
Kolekar, Abhijeetc0f20d92008-12-19 10:37:19 +08007725 struct iwl_cfg *cfg = (struct iwl_cfg *)(ent->driver_data);
Mohamed Abbas0359fac2008-03-28 16:21:08 -07007726 unsigned long flags;
Zhu Yib481de92007-09-25 17:54:57 -07007727
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08007728 /***********************
7729 * 1. Allocating HW data
7730 * ********************/
7731
Zhu Yib481de92007-09-25 17:54:57 -07007732 /* mac80211 allocates memory for this device instance, including
7733 * space for this driver's private structure */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08007734 hw = ieee80211_alloc_hw(sizeof(struct iwl_priv), &iwl3945_hw_ops);
Zhu Yib481de92007-09-25 17:54:57 -07007735 if (hw == NULL) {
Samuel Ortiza3139c52008-12-19 10:37:09 +08007736 printk(KERN_ERR DRV_NAME "Can not allocate network device\n");
Zhu Yib481de92007-09-25 17:54:57 -07007737 err = -ENOMEM;
7738 goto out;
7739 }
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08007740
Zhu Yib481de92007-09-25 17:54:57 -07007741 SET_IEEE80211_DEV(hw, &pdev->dev);
7742
Zhu Yib481de92007-09-25 17:54:57 -07007743 priv = hw->priv;
7744 priv->hw = hw;
Zhu Yib481de92007-09-25 17:54:57 -07007745 priv->pci_dev = pdev;
Tomas Winkler82b9a122008-03-04 18:09:30 -08007746 priv->cfg = cfg;
Cahill, Ben M6440adb2007-11-29 11:09:55 +08007747
Samuel Ortiza3139c52008-12-19 10:37:09 +08007748 if ((iwl3945_param_queues_num > IWL39_MAX_NUM_QUEUES) ||
7749 (iwl3945_param_queues_num < IWL_MIN_NUM_QUEUES)) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08007750 IWL_ERR(priv,
7751 "invalid queues_num, should be between %d and %d\n",
7752 IWL_MIN_NUM_QUEUES, IWL39_MAX_NUM_QUEUES);
Samuel Ortiza3139c52008-12-19 10:37:09 +08007753 err = -EINVAL;
7754 goto out;
7755 }
7756
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08007757 /* Disabling hardware scan means that mac80211 will perform scans
7758 * "the hard way", rather than using device's scan. */
7759 if (iwl3945_param_disable_hw_scan) {
7760 IWL_DEBUG_INFO("Disabling hw_scan\n");
7761 iwl3945_hw_ops.hw_scan = NULL;
7762 }
7763
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08007764 IWL_DEBUG_INFO("*** LOAD DRIVER ***\n");
7765 hw->rate_control_algorithm = "iwl-3945-rs";
7766 hw->sta_data_size = sizeof(struct iwl3945_sta_priv);
7767
Cahill, Ben M6440adb2007-11-29 11:09:55 +08007768 /* Select antenna (may be helpful if only one antenna is connected) */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007769 priv->antenna = (enum iwl3945_antenna)iwl3945_param_antenna;
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08007770#ifdef CONFIG_IWL3945_DEBUG
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08007771 priv->debug_level = iwl3945_param_debug;
Zhu Yib481de92007-09-25 17:54:57 -07007772 atomic_set(&priv->restrict_refcnt, 0);
7773#endif
Zhu Yib481de92007-09-25 17:54:57 -07007774
Bruno Randolf566bfe52008-05-08 19:15:40 +02007775 /* Tell mac80211 our characteristics */
Johannes Berg605a0bd2008-07-15 10:10:01 +02007776 hw->flags = IEEE80211_HW_SIGNAL_DBM |
Bruno Randolf566bfe52008-05-08 19:15:40 +02007777 IEEE80211_HW_NOISE_DBM;
Zhu Yib481de92007-09-25 17:54:57 -07007778
Luis R. Rodriguezf59ac042008-08-29 16:26:43 -07007779 hw->wiphy->interface_modes =
Luis R. Rodriguezf59ac042008-08-29 16:26:43 -07007780 BIT(NL80211_IFTYPE_STATION) |
7781 BIT(NL80211_IFTYPE_ADHOC);
7782
Luis R. Rodriguezea4a82dc2008-11-12 14:22:04 -08007783 hw->wiphy->fw_handles_regulatory = true;
7784
Cahill, Ben M6440adb2007-11-29 11:09:55 +08007785 /* 4 EDCA QOS priorities */
Zhu Yib481de92007-09-25 17:54:57 -07007786 hw->queues = 4;
7787
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08007788 /***************************
7789 * 2. Initializing PCI bus
7790 * *************************/
Zhu Yib481de92007-09-25 17:54:57 -07007791 if (pci_enable_device(pdev)) {
7792 err = -ENODEV;
7793 goto out_ieee80211_free_hw;
7794 }
7795
7796 pci_set_master(pdev);
7797
Zhu Yib481de92007-09-25 17:54:57 -07007798 err = pci_set_dma_mask(pdev, DMA_32BIT_MASK);
7799 if (!err)
7800 err = pci_set_consistent_dma_mask(pdev, DMA_32BIT_MASK);
7801 if (err) {
Tomas Winkler978785a2008-12-19 10:37:31 +08007802 IWL_WARN(priv, "No suitable DMA available.\n");
Zhu Yib481de92007-09-25 17:54:57 -07007803 goto out_pci_disable_device;
7804 }
7805
7806 pci_set_drvdata(pdev, priv);
7807 err = pci_request_regions(pdev, DRV_NAME);
7808 if (err)
7809 goto out_pci_disable_device;
Cahill, Ben M6440adb2007-11-29 11:09:55 +08007810
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08007811 /***********************
7812 * 3. Read REV Register
7813 * ********************/
Zhu Yib481de92007-09-25 17:54:57 -07007814 priv->hw_base = pci_iomap(pdev, 0, 0);
7815 if (!priv->hw_base) {
7816 err = -ENODEV;
7817 goto out_pci_release_regions;
7818 }
7819
7820 IWL_DEBUG_INFO("pci_resource_len = 0x%08llx\n",
7821 (unsigned long long) pci_resource_len(pdev, 0));
7822 IWL_DEBUG_INFO("pci_resource_base = %p\n", priv->hw_base);
7823
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08007824 /* We disable the RETRY_TIMEOUT register (0x41) to keep
7825 * PCI Tx retries from interfering with C3 CPU state */
7826 pci_write_config_byte(pdev, 0x41, 0x00);
Zhu Yib481de92007-09-25 17:54:57 -07007827
Zhu Yi5a669262008-01-14 17:46:18 -08007828 /* nic init */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08007829 iwl_set_bit(priv, CSR_GIO_CHICKEN_BITS,
Tomas Winkler3ac7f142008-07-21 02:40:14 +03007830 CSR_GIO_CHICKEN_BITS_REG_BIT_DIS_L0S_EXIT_TIMER);
Zhu Yi5a669262008-01-14 17:46:18 -08007831
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08007832 iwl_set_bit(priv, CSR_GP_CNTRL, CSR_GP_CNTRL_REG_FLAG_INIT_DONE);
7833 err = iwl_poll_direct_bit(priv, CSR_GP_CNTRL,
Zhu, Yi73d7b5a2008-12-05 07:58:40 -08007834 CSR_GP_CNTRL_REG_FLAG_MAC_CLOCK_READY, 25000);
Tomas Winkler3ac7f142008-07-21 02:40:14 +03007835 if (err < 0) {
7836 IWL_DEBUG_INFO("Failed to init the card\n");
Zhu Yi5a669262008-01-14 17:46:18 -08007837 goto out_remove_sysfs;
Tomas Winkler3ac7f142008-07-21 02:40:14 +03007838 }
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08007839
7840 /***********************
7841 * 4. Read EEPROM
7842 * ********************/
Zhu Yi5a669262008-01-14 17:46:18 -08007843 /* Read the EEPROM */
7844 err = iwl3945_eeprom_init(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007845 if (err) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08007846 IWL_ERR(priv, "Unable to init EEPROM\n");
Zhu Yi5a669262008-01-14 17:46:18 -08007847 goto out_remove_sysfs;
7848 }
7849 /* MAC Address location in EEPROM same for 3945/4965 */
7850 get_eeprom_mac(priv, priv->mac_addr);
Johannes Berge1749612008-10-27 15:59:26 -07007851 IWL_DEBUG_INFO("MAC address: %pM\n", priv->mac_addr);
Zhu Yi5a669262008-01-14 17:46:18 -08007852 SET_IEEE80211_PERM_ADDR(priv->hw, priv->mac_addr);
7853
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08007854 /***********************
7855 * 5. Setup HW Constants
7856 * ********************/
7857 /* Device-specific setup */
Abhijeet Kolekar3832ec92008-12-19 10:37:26 +08007858 if (iwl3945_hw_set_hw_params(priv)) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08007859 IWL_ERR(priv, "failed to set hw settings\n");
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08007860 goto out_iounmap;
7861 }
7862
7863 /***********************
7864 * 6. Setup priv
7865 * ********************/
7866 priv->retry_rate = 1;
7867 priv->ibss_beacon = NULL;
7868
7869 spin_lock_init(&priv->lock);
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08007870 spin_lock_init(&priv->power_data_39.lock);
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08007871 spin_lock_init(&priv->sta_lock);
7872 spin_lock_init(&priv->hcmd_lock);
7873
7874 INIT_LIST_HEAD(&priv->free_frames);
7875 mutex_init(&priv->mutex);
7876
7877 /* Clear the driver's (not device's) station table */
7878 iwl3945_clear_stations_table(priv);
7879
7880 priv->data_retry_limit = -1;
7881 priv->ieee_channels = NULL;
7882 priv->ieee_rates = NULL;
7883 priv->band = IEEE80211_BAND_2GHZ;
7884
7885 priv->iw_mode = NL80211_IFTYPE_STATION;
7886
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08007887 iwl3945_reset_qos(priv);
7888
7889 priv->qos_data.qos_active = 0;
7890 priv->qos_data.qos_cap.val = 0;
7891
7892
7893 priv->rates_mask = IWL_RATES_MASK;
7894 /* If power management is turned on, default to AC mode */
Samuel Ortiz1125eff2008-12-19 10:37:14 +08007895 priv->power_mode = IWL39_POWER_AC;
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08007896 priv->user_txpower_limit = IWL_DEFAULT_TX_POWER;
7897
Reinette Chatre849e0dc2008-01-23 10:15:18 -08007898 err = iwl3945_init_channel_map(priv);
7899 if (err) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08007900 IWL_ERR(priv, "initializing regulatory failed: %d\n", err);
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08007901 goto out_release_irq;
Reinette Chatre849e0dc2008-01-23 10:15:18 -08007902 }
7903
7904 err = iwl3945_init_geos(priv);
7905 if (err) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08007906 IWL_ERR(priv, "initializing geos failed: %d\n", err);
Reinette Chatre849e0dc2008-01-23 10:15:18 -08007907 goto out_free_channel_map;
7908 }
Reinette Chatre849e0dc2008-01-23 10:15:18 -08007909
Tomas Winkler978785a2008-12-19 10:37:31 +08007910 IWL_INFO(priv, "Detected Intel Wireless WiFi Link %s\n",
7911 priv->cfg->name);
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08007912
7913 /***********************************
7914 * 7. Initialize Module Parameters
7915 * **********************************/
7916
7917 /* Initialize module parameter values here */
7918 /* Disable radio (SW RF KILL) via parameter when loading driver */
7919 if (iwl3945_param_disable) {
7920 set_bit(STATUS_RF_KILL_SW, &priv->status);
7921 IWL_DEBUG_INFO("Radio disabled.\n");
7922 }
7923
7924
7925 /***********************
7926 * 8. Setup Services
7927 * ********************/
7928
7929 spin_lock_irqsave(&priv->lock, flags);
7930 iwl3945_disable_interrupts(priv);
7931 spin_unlock_irqrestore(&priv->lock, flags);
7932
7933 err = sysfs_create_group(&pdev->dev.kobj, &iwl3945_attribute_group);
7934 if (err) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08007935 IWL_ERR(priv, "failed to create sysfs device attributes\n");
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08007936 goto out_free_geos;
7937 }
7938
7939 iwl3945_set_rxon_channel(priv, IEEE80211_BAND_2GHZ, 6);
7940 iwl3945_setup_deferred_work(priv);
7941 iwl3945_setup_rx_handlers(priv);
7942
7943 /***********************
7944 * 9. Conclude
7945 * ********************/
7946 pci_save_state(pdev);
7947 pci_disable_device(pdev);
7948
7949 /*********************************
7950 * 10. Setup and Register mac80211
7951 * *******************************/
7952
Zhu Yi5a669262008-01-14 17:46:18 -08007953 err = ieee80211_register_hw(priv->hw);
7954 if (err) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08007955 IWL_ERR(priv, "Failed to register network device: %d\n", err);
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08007956 goto out_remove_sysfs;
Zhu Yib481de92007-09-25 17:54:57 -07007957 }
7958
Zhu Yi5a669262008-01-14 17:46:18 -08007959 priv->hw->conf.beacon_int = 100;
7960 priv->mac80211_registered = 1;
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08007961
Zhu Yib481de92007-09-25 17:54:57 -07007962
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08007963 err = iwl3945_rfkill_init(priv);
7964 if (err)
Winkler, Tomas15b16872008-12-19 10:37:33 +08007965 IWL_ERR(priv, "Unable to initialize RFKILL system. "
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08007966 "Ignoring error: %d\n", err);
7967
Zhu Yib481de92007-09-25 17:54:57 -07007968 return 0;
7969
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08007970 out_remove_sysfs:
7971 sysfs_remove_group(&pdev->dev.kobj, &iwl3945_attribute_group);
Reinette Chatre849e0dc2008-01-23 10:15:18 -08007972 out_free_geos:
7973 iwl3945_free_geos(priv);
7974 out_free_channel_map:
7975 iwl3945_free_channel_map(priv);
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08007976
Zhu Yib481de92007-09-25 17:54:57 -07007977
7978 out_release_irq:
Zhu Yib481de92007-09-25 17:54:57 -07007979 destroy_workqueue(priv->workqueue);
7980 priv->workqueue = NULL;
Abhijeet Kolekar3832ec92008-12-19 10:37:26 +08007981 iwl3945_unset_hw_params(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007982
7983 out_iounmap:
7984 pci_iounmap(pdev, priv->hw_base);
7985 out_pci_release_regions:
7986 pci_release_regions(pdev);
7987 out_pci_disable_device:
7988 pci_disable_device(pdev);
7989 pci_set_drvdata(pdev, NULL);
7990 out_ieee80211_free_hw:
7991 ieee80211_free_hw(priv->hw);
7992 out:
7993 return err;
7994}
7995
Reinette Chatrec83dbf62008-03-21 13:53:41 -07007996static void __devexit iwl3945_pci_remove(struct pci_dev *pdev)
Zhu Yib481de92007-09-25 17:54:57 -07007997{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08007998 struct iwl_priv *priv = pci_get_drvdata(pdev);
Mohamed Abbas0359fac2008-03-28 16:21:08 -07007999 unsigned long flags;
Zhu Yib481de92007-09-25 17:54:57 -07008000
8001 if (!priv)
8002 return;
8003
8004 IWL_DEBUG_INFO("*** UNLOAD DRIVER ***\n");
8005
Zhu Yib481de92007-09-25 17:54:57 -07008006 set_bit(STATUS_EXIT_PENDING, &priv->status);
Zhu Yib24d22b2007-12-19 13:59:52 +08008007
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008008 iwl3945_down(priv);
Zhu Yib481de92007-09-25 17:54:57 -07008009
Mohamed Abbas0359fac2008-03-28 16:21:08 -07008010 /* make sure we flush any pending irq or
8011 * tasklet for the driver
8012 */
8013 spin_lock_irqsave(&priv->lock, flags);
8014 iwl3945_disable_interrupts(priv);
8015 spin_unlock_irqrestore(&priv->lock, flags);
8016
8017 iwl_synchronize_irq(priv);
8018
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008019 sysfs_remove_group(&pdev->dev.kobj, &iwl3945_attribute_group);
Zhu Yib481de92007-09-25 17:54:57 -07008020
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08008021 iwl3945_rfkill_unregister(priv);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008022 iwl3945_dealloc_ucode_pci(priv);
Zhu Yib481de92007-09-25 17:54:57 -07008023
8024 if (priv->rxq.bd)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008025 iwl3945_rx_queue_free(priv, &priv->rxq);
8026 iwl3945_hw_txq_ctx_free(priv);
Zhu Yib481de92007-09-25 17:54:57 -07008027
Abhijeet Kolekar3832ec92008-12-19 10:37:26 +08008028 iwl3945_unset_hw_params(priv);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008029 iwl3945_clear_stations_table(priv);
Zhu Yib481de92007-09-25 17:54:57 -07008030
Tomas Winkler3ac7f142008-07-21 02:40:14 +03008031 if (priv->mac80211_registered)
Zhu Yib481de92007-09-25 17:54:57 -07008032 ieee80211_unregister_hw(priv->hw);
Zhu Yib481de92007-09-25 17:54:57 -07008033
Mohamed Abbas6ef89d02007-10-25 17:15:47 +08008034 /*netif_stop_queue(dev); */
8035 flush_workqueue(priv->workqueue);
8036
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008037 /* ieee80211_unregister_hw calls iwl3945_mac_stop, which flushes
Zhu Yib481de92007-09-25 17:54:57 -07008038 * priv->workqueue... so we can't take down the workqueue
8039 * until now... */
8040 destroy_workqueue(priv->workqueue);
8041 priv->workqueue = NULL;
8042
Zhu Yib481de92007-09-25 17:54:57 -07008043 pci_iounmap(pdev, priv->hw_base);
8044 pci_release_regions(pdev);
8045 pci_disable_device(pdev);
8046 pci_set_drvdata(pdev, NULL);
8047
Reinette Chatre849e0dc2008-01-23 10:15:18 -08008048 iwl3945_free_channel_map(priv);
8049 iwl3945_free_geos(priv);
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08008050 kfree(priv->scan39);
Zhu Yib481de92007-09-25 17:54:57 -07008051 if (priv->ibss_beacon)
8052 dev_kfree_skb(priv->ibss_beacon);
8053
8054 ieee80211_free_hw(priv->hw);
8055}
8056
8057#ifdef CONFIG_PM
8058
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008059static int iwl3945_pci_suspend(struct pci_dev *pdev, pm_message_t state)
Zhu Yib481de92007-09-25 17:54:57 -07008060{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08008061 struct iwl_priv *priv = pci_get_drvdata(pdev);
Zhu Yib481de92007-09-25 17:54:57 -07008062
Zhu Yie655b9f2008-01-24 02:19:38 -08008063 if (priv->is_open) {
8064 set_bit(STATUS_IN_SUSPEND, &priv->status);
8065 iwl3945_mac_stop(priv->hw);
8066 priv->is_open = 1;
8067 }
Zhu Yib481de92007-09-25 17:54:57 -07008068
Zhu Yib481de92007-09-25 17:54:57 -07008069 pci_set_power_state(pdev, PCI_D3hot);
8070
Zhu Yib481de92007-09-25 17:54:57 -07008071 return 0;
8072}
8073
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008074static int iwl3945_pci_resume(struct pci_dev *pdev)
Zhu Yib481de92007-09-25 17:54:57 -07008075{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08008076 struct iwl_priv *priv = pci_get_drvdata(pdev);
Zhu Yib481de92007-09-25 17:54:57 -07008077
Zhu Yib481de92007-09-25 17:54:57 -07008078 pci_set_power_state(pdev, PCI_D0);
Zhu Yib481de92007-09-25 17:54:57 -07008079
Zhu Yie655b9f2008-01-24 02:19:38 -08008080 if (priv->is_open)
8081 iwl3945_mac_start(priv->hw);
Zhu Yib481de92007-09-25 17:54:57 -07008082
Zhu Yie655b9f2008-01-24 02:19:38 -08008083 clear_bit(STATUS_IN_SUSPEND, &priv->status);
Zhu Yib481de92007-09-25 17:54:57 -07008084 return 0;
8085}
8086
8087#endif /* CONFIG_PM */
8088
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08008089/*************** RFKILL FUNCTIONS **********/
Adel Gadllah80fcc9e2008-07-01 17:49:50 +02008090#ifdef CONFIG_IWL3945_RFKILL
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08008091/* software rf-kill from user */
8092static int iwl3945_rfkill_soft_rf_kill(void *data, enum rfkill_state state)
8093{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08008094 struct iwl_priv *priv = data;
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08008095 int err = 0;
8096
Adel Gadllah80fcc9e2008-07-01 17:49:50 +02008097 if (!priv->rfkill)
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08008098 return 0;
8099
8100 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
8101 return 0;
8102
Tomas Winklera96a27f2008-10-23 23:48:56 -07008103 IWL_DEBUG_RF_KILL("we received soft RFKILL set to state %d\n", state);
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08008104 mutex_lock(&priv->mutex);
8105
8106 switch (state) {
Zhu Yiacdfe9b2008-06-30 17:23:32 +08008107 case RFKILL_STATE_UNBLOCKED:
Adel Gadllah80fcc9e2008-07-01 17:49:50 +02008108 if (iwl3945_is_rfkill_hw(priv)) {
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08008109 err = -EBUSY;
Adel Gadllah80fcc9e2008-07-01 17:49:50 +02008110 goto out_unlock;
8111 }
8112 iwl3945_radio_kill_sw(priv, 0);
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08008113 break;
Zhu Yiacdfe9b2008-06-30 17:23:32 +08008114 case RFKILL_STATE_SOFT_BLOCKED:
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08008115 iwl3945_radio_kill_sw(priv, 1);
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08008116 break;
Zhu Yiacdfe9b2008-06-30 17:23:32 +08008117 default:
Winkler, Tomas39aadf82008-12-19 10:37:32 +08008118 IWL_WARN(priv, "received unexpected RFKILL state %d\n", state);
Zhu Yiacdfe9b2008-06-30 17:23:32 +08008119 break;
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08008120 }
Adel Gadllah80fcc9e2008-07-01 17:49:50 +02008121out_unlock:
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08008122 mutex_unlock(&priv->mutex);
8123
8124 return err;
8125}
8126
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08008127int iwl3945_rfkill_init(struct iwl_priv *priv)
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08008128{
8129 struct device *device = wiphy_dev(priv->hw->wiphy);
8130 int ret = 0;
8131
8132 BUG_ON(device == NULL);
8133
8134 IWL_DEBUG_RF_KILL("Initializing RFKILL.\n");
Adel Gadllah80fcc9e2008-07-01 17:49:50 +02008135 priv->rfkill = rfkill_allocate(device, RFKILL_TYPE_WLAN);
8136 if (!priv->rfkill) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08008137 IWL_ERR(priv, "Unable to allocate rfkill device.\n");
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08008138 ret = -ENOMEM;
8139 goto error;
8140 }
8141
Adel Gadllah80fcc9e2008-07-01 17:49:50 +02008142 priv->rfkill->name = priv->cfg->name;
8143 priv->rfkill->data = priv;
8144 priv->rfkill->state = RFKILL_STATE_UNBLOCKED;
8145 priv->rfkill->toggle_radio = iwl3945_rfkill_soft_rf_kill;
8146 priv->rfkill->user_claim_unsupported = 1;
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08008147
Adel Gadllah80fcc9e2008-07-01 17:49:50 +02008148 priv->rfkill->dev.class->suspend = NULL;
8149 priv->rfkill->dev.class->resume = NULL;
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08008150
Adel Gadllah80fcc9e2008-07-01 17:49:50 +02008151 ret = rfkill_register(priv->rfkill);
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08008152 if (ret) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08008153 IWL_ERR(priv, "Unable to register rfkill: %d\n", ret);
Adel Gadllah80fcc9e2008-07-01 17:49:50 +02008154 goto freed_rfkill;
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08008155 }
8156
8157 IWL_DEBUG_RF_KILL("RFKILL initialization complete.\n");
8158 return ret;
8159
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08008160freed_rfkill:
Adel Gadllah80fcc9e2008-07-01 17:49:50 +02008161 if (priv->rfkill != NULL)
8162 rfkill_free(priv->rfkill);
8163 priv->rfkill = NULL;
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08008164
8165error:
8166 IWL_DEBUG_RF_KILL("RFKILL initialization complete.\n");
8167 return ret;
8168}
8169
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08008170void iwl3945_rfkill_unregister(struct iwl_priv *priv)
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08008171{
Adel Gadllah80fcc9e2008-07-01 17:49:50 +02008172 if (priv->rfkill)
8173 rfkill_unregister(priv->rfkill);
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08008174
Adel Gadllah80fcc9e2008-07-01 17:49:50 +02008175 priv->rfkill = NULL;
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08008176}
8177
8178/* set rf-kill to the right state. */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08008179void iwl3945_rfkill_set_hw_state(struct iwl_priv *priv)
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08008180{
8181
Adel Gadllah80fcc9e2008-07-01 17:49:50 +02008182 if (!priv->rfkill)
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08008183 return;
8184
Adel Gadllah80fcc9e2008-07-01 17:49:50 +02008185 if (iwl3945_is_rfkill_hw(priv)) {
8186 rfkill_force_state(priv->rfkill, RFKILL_STATE_HARD_BLOCKED);
8187 return;
8188 }
8189
8190 if (!iwl3945_is_rfkill_sw(priv))
8191 rfkill_force_state(priv->rfkill, RFKILL_STATE_UNBLOCKED);
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08008192 else
Adel Gadllah80fcc9e2008-07-01 17:49:50 +02008193 rfkill_force_state(priv->rfkill, RFKILL_STATE_SOFT_BLOCKED);
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08008194}
8195#endif
8196
Zhu Yib481de92007-09-25 17:54:57 -07008197/*****************************************************************************
8198 *
8199 * driver and module entry point
8200 *
8201 *****************************************************************************/
8202
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008203static struct pci_driver iwl3945_driver = {
Zhu Yib481de92007-09-25 17:54:57 -07008204 .name = DRV_NAME,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008205 .id_table = iwl3945_hw_card_ids,
8206 .probe = iwl3945_pci_probe,
8207 .remove = __devexit_p(iwl3945_pci_remove),
Zhu Yib481de92007-09-25 17:54:57 -07008208#ifdef CONFIG_PM
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008209 .suspend = iwl3945_pci_suspend,
8210 .resume = iwl3945_pci_resume,
Zhu Yib481de92007-09-25 17:54:57 -07008211#endif
8212};
8213
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008214static int __init iwl3945_init(void)
Zhu Yib481de92007-09-25 17:54:57 -07008215{
8216
8217 int ret;
8218 printk(KERN_INFO DRV_NAME ": " DRV_DESCRIPTION ", " DRV_VERSION "\n");
8219 printk(KERN_INFO DRV_NAME ": " DRV_COPYRIGHT "\n");
Reinette Chatre897e1cf2008-03-28 16:21:09 -07008220
8221 ret = iwl3945_rate_control_register();
8222 if (ret) {
Samuel Ortiza3139c52008-12-19 10:37:09 +08008223 printk(KERN_ERR DRV_NAME
8224 "Unable to register rate control algorithm: %d\n", ret);
Reinette Chatre897e1cf2008-03-28 16:21:09 -07008225 return ret;
8226 }
8227
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008228 ret = pci_register_driver(&iwl3945_driver);
Zhu Yib481de92007-09-25 17:54:57 -07008229 if (ret) {
Samuel Ortiza3139c52008-12-19 10:37:09 +08008230 printk(KERN_ERR DRV_NAME "Unable to initialize PCI module\n");
Reinette Chatre897e1cf2008-03-28 16:21:09 -07008231 goto error_register;
Zhu Yib481de92007-09-25 17:54:57 -07008232 }
Zhu Yib481de92007-09-25 17:54:57 -07008233
8234 return ret;
Reinette Chatre897e1cf2008-03-28 16:21:09 -07008235
Reinette Chatre897e1cf2008-03-28 16:21:09 -07008236error_register:
8237 iwl3945_rate_control_unregister();
8238 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07008239}
8240
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008241static void __exit iwl3945_exit(void)
Zhu Yib481de92007-09-25 17:54:57 -07008242{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008243 pci_unregister_driver(&iwl3945_driver);
Reinette Chatre897e1cf2008-03-28 16:21:09 -07008244 iwl3945_rate_control_unregister();
Zhu Yib481de92007-09-25 17:54:57 -07008245}
8246
Reinette Chatrea0987a82008-12-02 12:14:06 -08008247MODULE_FIRMWARE(IWL3945_MODULE_FIRMWARE(IWL3945_UCODE_API_MAX));
Zhu Yi25cb6ca2008-09-11 11:45:22 +08008248
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008249module_param_named(antenna, iwl3945_param_antenna, int, 0444);
Zhu Yib481de92007-09-25 17:54:57 -07008250MODULE_PARM_DESC(antenna, "select antenna (1=Main, 2=Aux, default 0 [both])");
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008251module_param_named(disable, iwl3945_param_disable, int, 0444);
Zhu Yib481de92007-09-25 17:54:57 -07008252MODULE_PARM_DESC(disable, "manually disable the radio (default 0 [radio on])");
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008253module_param_named(hwcrypto, iwl3945_param_hwcrypto, int, 0444);
Zhu Yib481de92007-09-25 17:54:57 -07008254MODULE_PARM_DESC(hwcrypto,
8255 "using hardware crypto engine (default 0 [software])\n");
Wu, Fengguang95aa1942008-12-17 16:52:30 +08008256module_param_named(debug, iwl3945_param_debug, uint, 0444);
Zhu Yib481de92007-09-25 17:54:57 -07008257MODULE_PARM_DESC(debug, "debug output mask");
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008258module_param_named(disable_hw_scan, iwl3945_param_disable_hw_scan, int, 0444);
Zhu Yib481de92007-09-25 17:54:57 -07008259MODULE_PARM_DESC(disable_hw_scan, "disable hardware scanning (default 0)");
8260
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008261module_param_named(queues_num, iwl3945_param_queues_num, int, 0444);
Zhu Yib481de92007-09-25 17:54:57 -07008262MODULE_PARM_DESC(queues_num, "number of hw queues.");
8263
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008264module_exit(iwl3945_exit);
8265module_init(iwl3945_init);