blob: 0af710b8fb78b9dd3496037ae879729ecbac736c [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 */
Winkler, Tomasc2d79b482008-12-19 10:37:34 +0800235 len = sizeof(struct iwl_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
Winkler, Tomasc2d79b482008-12-19 10:37:34 +0800286 len = sizeof(struct iwl_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 */
Winkler, Tomasc2d79b482008-12-19 10:37:34 +0800503static int iwl3945_enqueue_hcmd(struct iwl_priv *priv, struct iwl_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;
Winkler, Tomasc2d79b482008-12-19 10:37:34 +0800509 struct iwl_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 */
Winkler, Tomasc2d79b482008-12-19 10:37:34 +0800521 BUG_ON((fix_size > TFD_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 +
Winkler, Tomasc2d79b482008-12-19 10:37:34 +0800559 offsetof(struct iwl_cmd, hdr);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800560 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
Winkler, Tomasc2d79b482008-12-19 10:37:34 +0800582static int iwl3945_send_cmd_async(struct iwl_priv *priv,
583 struct iwl_host_cmd *cmd)
Zhu Yib481de92007-09-25 17:54:57 -0700584{
585 int ret;
586
587 BUG_ON(!(cmd->meta.flags & CMD_ASYNC));
588
589 /* An asynchronous command can not expect an SKB to be set. */
590 BUG_ON(cmd->meta.flags & CMD_WANT_SKB);
591
592 /* An asynchronous command MUST have a callback. */
593 BUG_ON(!cmd->meta.u.callback);
594
595 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
596 return -EBUSY;
597
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800598 ret = iwl3945_enqueue_hcmd(priv, cmd);
Zhu Yib481de92007-09-25 17:54:57 -0700599 if (ret < 0) {
Winkler, Tomas15b16872008-12-19 10:37:33 +0800600 IWL_ERR(priv,
601 "Error sending %s: iwl3945_enqueue_hcmd failed: %d\n",
602 get_cmd_string(cmd->id), ret);
Zhu Yib481de92007-09-25 17:54:57 -0700603 return ret;
604 }
605 return 0;
606}
607
Winkler, Tomasc2d79b482008-12-19 10:37:34 +0800608static int iwl3945_send_cmd_sync(struct iwl_priv *priv,
609 struct iwl_host_cmd *cmd)
Zhu Yib481de92007-09-25 17:54:57 -0700610{
611 int cmd_idx;
612 int ret;
Zhu Yib481de92007-09-25 17:54:57 -0700613
614 BUG_ON(cmd->meta.flags & CMD_ASYNC);
615
616 /* A synchronous command can not have a callback set. */
617 BUG_ON(cmd->meta.u.callback != NULL);
618
Tomas Winklere5472972008-03-28 16:21:12 -0700619 if (test_and_set_bit(STATUS_HCMD_SYNC_ACTIVE, &priv->status)) {
Winkler, Tomas15b16872008-12-19 10:37:33 +0800620 IWL_ERR(priv,
621 "Error sending %s: Already sending a host command\n",
622 get_cmd_string(cmd->id));
Tomas Winklere5472972008-03-28 16:21:12 -0700623 ret = -EBUSY;
624 goto out;
Zhu Yib481de92007-09-25 17:54:57 -0700625 }
626
627 set_bit(STATUS_HCMD_ACTIVE, &priv->status);
628
629 if (cmd->meta.flags & CMD_WANT_SKB)
630 cmd->meta.source = &cmd->meta;
631
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800632 cmd_idx = iwl3945_enqueue_hcmd(priv, cmd);
Zhu Yib481de92007-09-25 17:54:57 -0700633 if (cmd_idx < 0) {
634 ret = cmd_idx;
Winkler, Tomas15b16872008-12-19 10:37:33 +0800635 IWL_ERR(priv,
636 "Error sending %s: iwl3945_enqueue_hcmd failed: %d\n",
637 get_cmd_string(cmd->id), ret);
Zhu Yib481de92007-09-25 17:54:57 -0700638 goto out;
639 }
640
641 ret = wait_event_interruptible_timeout(priv->wait_command_queue,
642 !test_bit(STATUS_HCMD_ACTIVE, &priv->status),
643 HOST_COMPLETE_TIMEOUT);
644 if (!ret) {
645 if (test_bit(STATUS_HCMD_ACTIVE, &priv->status)) {
Winkler, Tomas15b16872008-12-19 10:37:33 +0800646 IWL_ERR(priv, "Error sending %s: time out after %dms\n",
Zhu Yib481de92007-09-25 17:54:57 -0700647 get_cmd_string(cmd->id),
648 jiffies_to_msecs(HOST_COMPLETE_TIMEOUT));
649
650 clear_bit(STATUS_HCMD_ACTIVE, &priv->status);
651 ret = -ETIMEDOUT;
652 goto cancel;
653 }
654 }
655
656 if (test_bit(STATUS_RF_KILL_HW, &priv->status)) {
657 IWL_DEBUG_INFO("Command %s aborted: RF KILL Switch\n",
658 get_cmd_string(cmd->id));
659 ret = -ECANCELED;
660 goto fail;
661 }
662 if (test_bit(STATUS_FW_ERROR, &priv->status)) {
663 IWL_DEBUG_INFO("Command %s failed: FW Error\n",
664 get_cmd_string(cmd->id));
665 ret = -EIO;
666 goto fail;
667 }
668 if ((cmd->meta.flags & CMD_WANT_SKB) && !cmd->meta.u.skb) {
Winkler, Tomas15b16872008-12-19 10:37:33 +0800669 IWL_ERR(priv, "Error: Response NULL in '%s'\n",
Zhu Yib481de92007-09-25 17:54:57 -0700670 get_cmd_string(cmd->id));
671 ret = -EIO;
Zhu Yi73e1a652009-01-08 10:19:58 -0800672 goto cancel;
Zhu Yib481de92007-09-25 17:54:57 -0700673 }
674
675 ret = 0;
676 goto out;
677
678cancel:
679 if (cmd->meta.flags & CMD_WANT_SKB) {
Winkler, Tomasc2d79b482008-12-19 10:37:34 +0800680 struct iwl_cmd *qcmd;
Zhu Yib481de92007-09-25 17:54:57 -0700681
682 /* Cancel the CMD_WANT_SKB flag for the cmd in the
683 * TX cmd queue. Otherwise in case the cmd comes
684 * in later, it will possibly set an invalid
685 * address (cmd->meta.source). */
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800686 qcmd = &priv->txq39[IWL_CMD_QUEUE_NUM].cmd[cmd_idx];
Zhu Yib481de92007-09-25 17:54:57 -0700687 qcmd->meta.flags &= ~CMD_WANT_SKB;
688 }
689fail:
690 if (cmd->meta.u.skb) {
691 dev_kfree_skb_any(cmd->meta.u.skb);
692 cmd->meta.u.skb = NULL;
693 }
694out:
Tomas Winklere5472972008-03-28 16:21:12 -0700695 clear_bit(STATUS_HCMD_SYNC_ACTIVE, &priv->status);
Zhu Yib481de92007-09-25 17:54:57 -0700696 return ret;
697}
698
Winkler, Tomasc2d79b482008-12-19 10:37:34 +0800699int iwl3945_send_cmd(struct iwl_priv *priv, struct iwl_host_cmd *cmd)
Zhu Yib481de92007-09-25 17:54:57 -0700700{
Zhu Yib481de92007-09-25 17:54:57 -0700701 if (cmd->meta.flags & CMD_ASYNC)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800702 return iwl3945_send_cmd_async(priv, cmd);
Zhu Yib481de92007-09-25 17:54:57 -0700703
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800704 return iwl3945_send_cmd_sync(priv, cmd);
Zhu Yib481de92007-09-25 17:54:57 -0700705}
706
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800707int iwl3945_send_cmd_pdu(struct iwl_priv *priv, u8 id, u16 len, const void *data)
Zhu Yib481de92007-09-25 17:54:57 -0700708{
Winkler, Tomasc2d79b482008-12-19 10:37:34 +0800709 struct iwl_host_cmd cmd = {
Zhu Yib481de92007-09-25 17:54:57 -0700710 .id = id,
711 .len = len,
712 .data = data,
713 };
714
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800715 return iwl3945_send_cmd_sync(priv, &cmd);
Zhu Yib481de92007-09-25 17:54:57 -0700716}
717
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800718static int __must_check iwl3945_send_cmd_u32(struct iwl_priv *priv, u8 id, u32 val)
Zhu Yib481de92007-09-25 17:54:57 -0700719{
Winkler, Tomasc2d79b482008-12-19 10:37:34 +0800720 struct iwl_host_cmd cmd = {
Zhu Yib481de92007-09-25 17:54:57 -0700721 .id = id,
722 .len = sizeof(val),
723 .data = &val,
724 };
725
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800726 return iwl3945_send_cmd_sync(priv, &cmd);
Zhu Yib481de92007-09-25 17:54:57 -0700727}
728
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800729int iwl3945_send_statistics_request(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700730{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800731 return iwl3945_send_cmd_u32(priv, REPLY_STATISTICS_CMD, 0);
Zhu Yib481de92007-09-25 17:54:57 -0700732}
733
734/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800735 * iwl3945_set_rxon_channel - Set the phymode and channel values in staging RXON
Johannes Berg8318d782008-01-24 19:38:38 +0100736 * @band: 2.4 or 5 GHz band
737 * @channel: Any channel valid for the requested band
Zhu Yib481de92007-09-25 17:54:57 -0700738
Johannes Berg8318d782008-01-24 19:38:38 +0100739 * In addition to setting the staging RXON, priv->band is also set.
Zhu Yib481de92007-09-25 17:54:57 -0700740 *
741 * NOTE: Does not commit to the hardware; it sets appropriate bit fields
Johannes Berg8318d782008-01-24 19:38:38 +0100742 * in the staging RXON flag structure based on the band
Zhu Yib481de92007-09-25 17:54:57 -0700743 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800744static int iwl3945_set_rxon_channel(struct iwl_priv *priv,
Johannes Berg8318d782008-01-24 19:38:38 +0100745 enum ieee80211_band band,
746 u16 channel)
Zhu Yib481de92007-09-25 17:54:57 -0700747{
Johannes Berg8318d782008-01-24 19:38:38 +0100748 if (!iwl3945_get_channel_info(priv, band, channel)) {
Zhu Yib481de92007-09-25 17:54:57 -0700749 IWL_DEBUG_INFO("Could not set channel to %d [%d]\n",
Johannes Berg8318d782008-01-24 19:38:38 +0100750 channel, band);
Zhu Yib481de92007-09-25 17:54:57 -0700751 return -EINVAL;
752 }
753
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800754 if ((le16_to_cpu(priv->staging39_rxon.channel) == channel) &&
Johannes Berg8318d782008-01-24 19:38:38 +0100755 (priv->band == band))
Zhu Yib481de92007-09-25 17:54:57 -0700756 return 0;
757
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800758 priv->staging39_rxon.channel = cpu_to_le16(channel);
Johannes Berg8318d782008-01-24 19:38:38 +0100759 if (band == IEEE80211_BAND_5GHZ)
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 else
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800762 priv->staging39_rxon.flags |= RXON_FLG_BAND_24G_MSK;
Zhu Yib481de92007-09-25 17:54:57 -0700763
Johannes Berg8318d782008-01-24 19:38:38 +0100764 priv->band = band;
Zhu Yib481de92007-09-25 17:54:57 -0700765
Johannes Berg8318d782008-01-24 19:38:38 +0100766 IWL_DEBUG_INFO("Staging channel set to %d [%d]\n", channel, band);
Zhu Yib481de92007-09-25 17:54:57 -0700767
768 return 0;
769}
770
771/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800772 * iwl3945_check_rxon_cmd - validate RXON structure is valid
Zhu Yib481de92007-09-25 17:54:57 -0700773 *
774 * NOTE: This is really only useful during development and can eventually
775 * be #ifdef'd out once the driver is stable and folks aren't actively
776 * making changes
777 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800778static int iwl3945_check_rxon_cmd(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700779{
780 int error = 0;
781 int counter = 1;
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800782 struct iwl3945_rxon_cmd *rxon = &priv->staging39_rxon;
Zhu Yib481de92007-09-25 17:54:57 -0700783
784 if (rxon->flags & RXON_FLG_BAND_24G_MSK) {
785 error |= le32_to_cpu(rxon->flags &
786 (RXON_FLG_TGJ_NARROW_BAND_MSK |
787 RXON_FLG_RADAR_DETECT_MSK));
788 if (error)
Winkler, Tomas39aadf82008-12-19 10:37:32 +0800789 IWL_WARN(priv, "check 24G fields %d | %d\n",
Zhu Yib481de92007-09-25 17:54:57 -0700790 counter++, error);
791 } else {
792 error |= (rxon->flags & RXON_FLG_SHORT_SLOT_MSK) ?
793 0 : le32_to_cpu(RXON_FLG_SHORT_SLOT_MSK);
794 if (error)
Winkler, Tomas39aadf82008-12-19 10:37:32 +0800795 IWL_WARN(priv, "check 52 fields %d | %d\n",
Zhu Yib481de92007-09-25 17:54:57 -0700796 counter++, error);
797 error |= le32_to_cpu(rxon->flags & RXON_FLG_CCK_MSK);
798 if (error)
Winkler, Tomas39aadf82008-12-19 10:37:32 +0800799 IWL_WARN(priv, "check 52 CCK %d | %d\n",
Zhu Yib481de92007-09-25 17:54:57 -0700800 counter++, error);
801 }
802 error |= (rxon->node_addr[0] | rxon->bssid_addr[0]) & 0x1;
803 if (error)
Winkler, Tomas39aadf82008-12-19 10:37:32 +0800804 IWL_WARN(priv, "check mac addr %d | %d\n", counter++, error);
Zhu Yib481de92007-09-25 17:54:57 -0700805
806 /* make sure basic rates 6Mbps and 1Mbps are supported */
807 error |= (((rxon->ofdm_basic_rates & IWL_RATE_6M_MASK) == 0) &&
808 ((rxon->cck_basic_rates & IWL_RATE_1M_MASK) == 0));
809 if (error)
Winkler, Tomas39aadf82008-12-19 10:37:32 +0800810 IWL_WARN(priv, "check basic rate %d | %d\n", counter++, error);
Zhu Yib481de92007-09-25 17:54:57 -0700811
812 error |= (le16_to_cpu(rxon->assoc_id) > 2007);
813 if (error)
Winkler, Tomas39aadf82008-12-19 10:37:32 +0800814 IWL_WARN(priv, "check assoc id %d | %d\n", counter++, error);
Zhu Yib481de92007-09-25 17:54:57 -0700815
816 error |= ((rxon->flags & (RXON_FLG_CCK_MSK | RXON_FLG_SHORT_SLOT_MSK))
817 == (RXON_FLG_CCK_MSK | RXON_FLG_SHORT_SLOT_MSK));
818 if (error)
Winkler, Tomas39aadf82008-12-19 10:37:32 +0800819 IWL_WARN(priv, "check CCK and short slot %d | %d\n",
Zhu Yib481de92007-09-25 17:54:57 -0700820 counter++, error);
821
822 error |= ((rxon->flags & (RXON_FLG_CCK_MSK | RXON_FLG_AUTO_DETECT_MSK))
823 == (RXON_FLG_CCK_MSK | RXON_FLG_AUTO_DETECT_MSK));
824 if (error)
Winkler, Tomas39aadf82008-12-19 10:37:32 +0800825 IWL_WARN(priv, "check CCK & auto detect %d | %d\n",
Zhu Yib481de92007-09-25 17:54:57 -0700826 counter++, error);
827
828 error |= ((rxon->flags & (RXON_FLG_AUTO_DETECT_MSK |
829 RXON_FLG_TGG_PROTECT_MSK)) == RXON_FLG_TGG_PROTECT_MSK);
830 if (error)
Winkler, Tomas39aadf82008-12-19 10:37:32 +0800831 IWL_WARN(priv, "check TGG and auto detect %d | %d\n",
Zhu Yib481de92007-09-25 17:54:57 -0700832 counter++, error);
833
834 if ((rxon->flags & RXON_FLG_DIS_DIV_MSK))
835 error |= ((rxon->flags & (RXON_FLG_ANT_B_MSK |
836 RXON_FLG_ANT_A_MSK)) == 0);
837 if (error)
Winkler, Tomas39aadf82008-12-19 10:37:32 +0800838 IWL_WARN(priv, "check antenna %d %d\n", counter++, error);
Zhu Yib481de92007-09-25 17:54:57 -0700839
840 if (error)
Winkler, Tomas39aadf82008-12-19 10:37:32 +0800841 IWL_WARN(priv, "Tuning to channel %d\n",
Zhu Yib481de92007-09-25 17:54:57 -0700842 le16_to_cpu(rxon->channel));
843
844 if (error) {
Winkler, Tomas15b16872008-12-19 10:37:33 +0800845 IWL_ERR(priv, "Not a valid rxon_assoc_cmd field values\n");
Zhu Yib481de92007-09-25 17:54:57 -0700846 return -1;
847 }
848 return 0;
849}
850
851/**
Ben Cahill9fbab512007-11-29 11:09:47 +0800852 * iwl3945_full_rxon_required - check if full RXON (vs RXON_ASSOC) cmd is needed
Ian Schram01ebd062007-10-25 17:15:22 +0800853 * @priv: staging_rxon is compared to active_rxon
Zhu Yib481de92007-09-25 17:54:57 -0700854 *
Ben Cahill9fbab512007-11-29 11:09:47 +0800855 * If the RXON structure is changing enough to require a new tune,
856 * or is clearing the RXON_FILTER_ASSOC_MSK, then return 1 to indicate that
857 * a new tune (full RXON command, rather than RXON_ASSOC cmd) is required.
Zhu Yib481de92007-09-25 17:54:57 -0700858 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800859static int iwl3945_full_rxon_required(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700860{
861
862 /* These items are only settable from the full RXON command */
Ron Rindjunsky5d1e2322008-06-30 17:23:04 +0800863 if (!(iwl3945_is_associated(priv)) ||
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800864 compare_ether_addr(priv->staging39_rxon.bssid_addr,
865 priv->active39_rxon.bssid_addr) ||
866 compare_ether_addr(priv->staging39_rxon.node_addr,
867 priv->active39_rxon.node_addr) ||
868 compare_ether_addr(priv->staging39_rxon.wlap_bssid_addr,
869 priv->active39_rxon.wlap_bssid_addr) ||
870 (priv->staging39_rxon.dev_type != priv->active39_rxon.dev_type) ||
871 (priv->staging39_rxon.channel != priv->active39_rxon.channel) ||
872 (priv->staging39_rxon.air_propagation !=
873 priv->active39_rxon.air_propagation) ||
874 (priv->staging39_rxon.assoc_id != priv->active39_rxon.assoc_id))
Zhu Yib481de92007-09-25 17:54:57 -0700875 return 1;
876
877 /* flags, filter_flags, ofdm_basic_rates, and cck_basic_rates can
878 * be updated with the RXON_ASSOC command -- however only some
879 * flag transitions are allowed using RXON_ASSOC */
880
881 /* Check if we are not switching bands */
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800882 if ((priv->staging39_rxon.flags & RXON_FLG_BAND_24G_MSK) !=
883 (priv->active39_rxon.flags & RXON_FLG_BAND_24G_MSK))
Zhu Yib481de92007-09-25 17:54:57 -0700884 return 1;
885
886 /* Check if we are switching association toggle */
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800887 if ((priv->staging39_rxon.filter_flags & RXON_FILTER_ASSOC_MSK) !=
888 (priv->active39_rxon.filter_flags & RXON_FILTER_ASSOC_MSK))
Zhu Yib481de92007-09-25 17:54:57 -0700889 return 1;
890
891 return 0;
892}
893
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800894static int iwl3945_send_rxon_assoc(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700895{
896 int rc = 0;
Tomas Winkler3d24a9f2008-12-19 10:37:07 +0800897 struct iwl_rx_packet *res = NULL;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800898 struct iwl3945_rxon_assoc_cmd rxon_assoc;
Winkler, Tomasc2d79b482008-12-19 10:37:34 +0800899 struct iwl_host_cmd cmd = {
Zhu Yib481de92007-09-25 17:54:57 -0700900 .id = REPLY_RXON_ASSOC,
901 .len = sizeof(rxon_assoc),
902 .meta.flags = CMD_WANT_SKB,
903 .data = &rxon_assoc,
904 };
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800905 const struct iwl3945_rxon_cmd *rxon1 = &priv->staging39_rxon;
906 const struct iwl3945_rxon_cmd *rxon2 = &priv->active39_rxon;
Zhu Yib481de92007-09-25 17:54:57 -0700907
908 if ((rxon1->flags == rxon2->flags) &&
909 (rxon1->filter_flags == rxon2->filter_flags) &&
910 (rxon1->cck_basic_rates == rxon2->cck_basic_rates) &&
911 (rxon1->ofdm_basic_rates == rxon2->ofdm_basic_rates)) {
912 IWL_DEBUG_INFO("Using current RXON_ASSOC. Not resending.\n");
913 return 0;
914 }
915
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800916 rxon_assoc.flags = priv->staging39_rxon.flags;
917 rxon_assoc.filter_flags = priv->staging39_rxon.filter_flags;
918 rxon_assoc.ofdm_basic_rates = priv->staging39_rxon.ofdm_basic_rates;
919 rxon_assoc.cck_basic_rates = priv->staging39_rxon.cck_basic_rates;
Zhu Yib481de92007-09-25 17:54:57 -0700920 rxon_assoc.reserved = 0;
921
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800922 rc = iwl3945_send_cmd_sync(priv, &cmd);
Zhu Yib481de92007-09-25 17:54:57 -0700923 if (rc)
924 return rc;
925
Tomas Winkler3d24a9f2008-12-19 10:37:07 +0800926 res = (struct iwl_rx_packet *)cmd.meta.u.skb->data;
Zhu Yib481de92007-09-25 17:54:57 -0700927 if (res->hdr.flags & IWL_CMD_FAILED_MSK) {
Winkler, Tomas15b16872008-12-19 10:37:33 +0800928 IWL_ERR(priv, "Bad return from REPLY_RXON_ASSOC command\n");
Zhu Yib481de92007-09-25 17:54:57 -0700929 rc = -EIO;
930 }
931
932 priv->alloc_rxb_skb--;
933 dev_kfree_skb_any(cmd.meta.u.skb);
934
935 return rc;
936}
937
938/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800939 * iwl3945_commit_rxon - commit staging_rxon to hardware
Zhu Yib481de92007-09-25 17:54:57 -0700940 *
Ian Schram01ebd062007-10-25 17:15:22 +0800941 * The RXON command in staging_rxon is committed to the hardware and
Zhu Yib481de92007-09-25 17:54:57 -0700942 * the active_rxon structure is updated with the new data. This
943 * function correctly transitions out of the RXON_ASSOC_MSK state if
944 * a HW tune is required based on the RXON structure changes.
945 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800946static int iwl3945_commit_rxon(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700947{
948 /* cast away the const for active_rxon in this function */
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800949 struct iwl3945_rxon_cmd *active_rxon = (void *)&priv->active39_rxon;
Zhu Yib481de92007-09-25 17:54:57 -0700950 int rc = 0;
951
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800952 if (!iwl3945_is_alive(priv))
Zhu Yib481de92007-09-25 17:54:57 -0700953 return -1;
954
955 /* always get timestamp with Rx frame */
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800956 priv->staging39_rxon.flags |= RXON_FLG_TSF2HOST_MSK;
Zhu Yib481de92007-09-25 17:54:57 -0700957
958 /* select antenna */
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800959 priv->staging39_rxon.flags &=
Zhu Yib481de92007-09-25 17:54:57 -0700960 ~(RXON_FLG_DIS_DIV_MSK | RXON_FLG_ANT_SEL_MSK);
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800961 priv->staging39_rxon.flags |= iwl3945_get_antenna_flags(priv);
Zhu Yib481de92007-09-25 17:54:57 -0700962
Samuel Ortiza3139c52008-12-19 10:37:09 +0800963 rc = iwl3945_check_rxon_cmd(priv);
Zhu Yib481de92007-09-25 17:54:57 -0700964 if (rc) {
Winkler, Tomas15b16872008-12-19 10:37:33 +0800965 IWL_ERR(priv, "Invalid RXON configuration. Not committing.\n");
Zhu Yib481de92007-09-25 17:54:57 -0700966 return -EINVAL;
967 }
968
969 /* If we don't need to send a full RXON, we can use
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800970 * iwl3945_rxon_assoc_cmd which is used to reconfigure filter
Zhu Yib481de92007-09-25 17:54:57 -0700971 * and other flags for the current radio configuration. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800972 if (!iwl3945_full_rxon_required(priv)) {
973 rc = iwl3945_send_rxon_assoc(priv);
Zhu Yib481de92007-09-25 17:54:57 -0700974 if (rc) {
Winkler, Tomas15b16872008-12-19 10:37:33 +0800975 IWL_ERR(priv, "Error setting RXON_ASSOC "
Zhu Yib481de92007-09-25 17:54:57 -0700976 "configuration (%d).\n", rc);
977 return rc;
978 }
979
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800980 memcpy(active_rxon, &priv->staging39_rxon, sizeof(*active_rxon));
Zhu Yib481de92007-09-25 17:54:57 -0700981
982 return 0;
983 }
984
985 /* If we are currently associated and the new config requires
986 * an RXON_ASSOC and the new config wants the associated mask enabled,
987 * we must clear the associated from the active configuration
988 * before we apply the new config */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800989 if (iwl3945_is_associated(priv) &&
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800990 (priv->staging39_rxon.filter_flags & RXON_FILTER_ASSOC_MSK)) {
Zhu Yib481de92007-09-25 17:54:57 -0700991 IWL_DEBUG_INFO("Toggling associated bit on current RXON\n");
992 active_rxon->filter_flags &= ~RXON_FILTER_ASSOC_MSK;
993
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800994 rc = iwl3945_send_cmd_pdu(priv, REPLY_RXON,
995 sizeof(struct iwl3945_rxon_cmd),
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800996 &priv->active39_rxon);
Zhu Yib481de92007-09-25 17:54:57 -0700997
998 /* If the mask clearing failed then we set
999 * active_rxon back to what it was previously */
1000 if (rc) {
1001 active_rxon->filter_flags |= RXON_FILTER_ASSOC_MSK;
Winkler, Tomas15b16872008-12-19 10:37:33 +08001002 IWL_ERR(priv, "Error clearing ASSOC_MSK on current "
Zhu Yib481de92007-09-25 17:54:57 -07001003 "configuration (%d).\n", rc);
1004 return rc;
1005 }
Zhu Yib481de92007-09-25 17:54:57 -07001006 }
1007
1008 IWL_DEBUG_INFO("Sending RXON\n"
1009 "* with%s RXON_FILTER_ASSOC_MSK\n"
1010 "* channel = %d\n"
Johannes Berge1749612008-10-27 15:59:26 -07001011 "* bssid = %pM\n",
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001012 ((priv->staging39_rxon.filter_flags &
Zhu Yib481de92007-09-25 17:54:57 -07001013 RXON_FILTER_ASSOC_MSK) ? "" : "out"),
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001014 le16_to_cpu(priv->staging39_rxon.channel),
Johannes Berge1749612008-10-27 15:59:26 -07001015 priv->staging_rxon.bssid_addr);
Zhu Yib481de92007-09-25 17:54:57 -07001016
1017 /* Apply the new configuration */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001018 rc = iwl3945_send_cmd_pdu(priv, REPLY_RXON,
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001019 sizeof(struct iwl3945_rxon_cmd), &priv->staging39_rxon);
Zhu Yib481de92007-09-25 17:54:57 -07001020 if (rc) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08001021 IWL_ERR(priv, "Error setting new configuration (%d).\n", rc);
Zhu Yib481de92007-09-25 17:54:57 -07001022 return rc;
1023 }
1024
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001025 memcpy(active_rxon, &priv->staging39_rxon, sizeof(*active_rxon));
Zhu Yib481de92007-09-25 17:54:57 -07001026
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001027 iwl3945_clear_stations_table(priv);
Zhu Yi556f8db2007-09-27 11:27:33 +08001028
Zhu Yib481de92007-09-25 17:54:57 -07001029 /* If we issue a new RXON command which required a tune then we must
1030 * send a new TXPOWER command or we won't be able to Tx any frames */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001031 rc = iwl3945_hw_reg_send_txpower(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001032 if (rc) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08001033 IWL_ERR(priv, "Error setting Tx power (%d).\n", rc);
Zhu Yib481de92007-09-25 17:54:57 -07001034 return rc;
1035 }
1036
1037 /* Add the broadcast address so we can send broadcast frames */
Kolekar, Abhijeetb5323d32008-12-19 10:37:22 +08001038 if (iwl3945_add_station(priv, iwl_bcast_addr, 0, 0) ==
Zhu Yib481de92007-09-25 17:54:57 -07001039 IWL_INVALID_STATION) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08001040 IWL_ERR(priv, "Error adding BROADCAST address for transmit.\n");
Zhu Yib481de92007-09-25 17:54:57 -07001041 return -EIO;
1042 }
1043
1044 /* If we have set the ASSOC_MSK and we are in BSS mode then
1045 * add the IWL_AP_ID to the station rate table */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001046 if (iwl3945_is_associated(priv) &&
Johannes Berg05c914f2008-09-11 00:01:58 +02001047 (priv->iw_mode == NL80211_IFTYPE_STATION))
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001048 if (iwl3945_add_station(priv, priv->active39_rxon.bssid_addr, 1, 0)
Zhu Yib481de92007-09-25 17:54:57 -07001049 == IWL_INVALID_STATION) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08001050 IWL_ERR(priv, "Error adding AP address for transmit\n");
Zhu Yib481de92007-09-25 17:54:57 -07001051 return -EIO;
1052 }
1053
Johannes Berg8318d782008-01-24 19:38:38 +01001054 /* Init the hardware's rate fallback order based on the band */
Zhu Yib481de92007-09-25 17:54:57 -07001055 rc = iwl3945_init_hw_rate_table(priv);
1056 if (rc) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08001057 IWL_ERR(priv, "Error setting HW rate table: %02X\n", rc);
Zhu Yib481de92007-09-25 17:54:57 -07001058 return -EIO;
1059 }
1060
1061 return 0;
1062}
1063
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001064static int iwl3945_send_bt_config(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001065{
Tomas Winkler4c897252008-12-19 10:37:05 +08001066 struct iwl_bt_cmd bt_cmd = {
Zhu Yib481de92007-09-25 17:54:57 -07001067 .flags = 3,
1068 .lead_time = 0xAA,
1069 .max_kill = 1,
1070 .kill_ack_mask = 0,
1071 .kill_cts_mask = 0,
1072 };
1073
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001074 return iwl3945_send_cmd_pdu(priv, REPLY_BT_CONFIG,
Tomas Winkler4c897252008-12-19 10:37:05 +08001075 sizeof(bt_cmd), &bt_cmd);
Zhu Yib481de92007-09-25 17:54:57 -07001076}
1077
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001078static int iwl3945_send_scan_abort(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001079{
1080 int rc = 0;
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08001081 struct iwl_rx_packet *res;
Winkler, Tomasc2d79b482008-12-19 10:37:34 +08001082 struct iwl_host_cmd cmd = {
Zhu Yib481de92007-09-25 17:54:57 -07001083 .id = REPLY_SCAN_ABORT_CMD,
1084 .meta.flags = CMD_WANT_SKB,
1085 };
1086
1087 /* If there isn't a scan actively going on in the hardware
1088 * then we are in between scan bands and not actually
1089 * actively scanning, so don't send the abort command */
1090 if (!test_bit(STATUS_SCAN_HW, &priv->status)) {
1091 clear_bit(STATUS_SCAN_ABORTING, &priv->status);
1092 return 0;
1093 }
1094
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001095 rc = iwl3945_send_cmd_sync(priv, &cmd);
Zhu Yib481de92007-09-25 17:54:57 -07001096 if (rc) {
1097 clear_bit(STATUS_SCAN_ABORTING, &priv->status);
1098 return rc;
1099 }
1100
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08001101 res = (struct iwl_rx_packet *)cmd.meta.u.skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07001102 if (res->u.status != CAN_ABORT_STATUS) {
1103 /* The scan abort will return 1 for success or
1104 * 2 for "failure". A failure condition can be
1105 * due to simply not being in an active scan which
1106 * can occur if we send the scan abort before we
1107 * the microcode has notified us that a scan is
1108 * completed. */
1109 IWL_DEBUG_INFO("SCAN_ABORT returned %d.\n", res->u.status);
1110 clear_bit(STATUS_SCAN_ABORTING, &priv->status);
1111 clear_bit(STATUS_SCAN_HW, &priv->status);
1112 }
1113
1114 dev_kfree_skb_any(cmd.meta.u.skb);
1115
1116 return rc;
1117}
1118
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001119static int iwl3945_card_state_sync_callback(struct iwl_priv *priv,
Winkler, Tomasc2d79b482008-12-19 10:37:34 +08001120 struct iwl_cmd *cmd,
Zhu Yib481de92007-09-25 17:54:57 -07001121 struct sk_buff *skb)
1122{
1123 return 1;
1124}
1125
1126/*
1127 * CARD_STATE_CMD
1128 *
Ben Cahill9fbab512007-11-29 11:09:47 +08001129 * Use: Sets the device's internal card state to enable, disable, or halt
Zhu Yib481de92007-09-25 17:54:57 -07001130 *
1131 * When in the 'enable' state the card operates as normal.
1132 * When in the 'disable' state, the card enters into a low power mode.
1133 * When in the 'halt' state, the card is shut down and must be fully
1134 * restarted to come back on.
1135 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001136static int iwl3945_send_card_state(struct iwl_priv *priv, u32 flags, u8 meta_flag)
Zhu Yib481de92007-09-25 17:54:57 -07001137{
Winkler, Tomasc2d79b482008-12-19 10:37:34 +08001138 struct iwl_host_cmd cmd = {
Zhu Yib481de92007-09-25 17:54:57 -07001139 .id = REPLY_CARD_STATE_CMD,
1140 .len = sizeof(u32),
1141 .data = &flags,
1142 .meta.flags = meta_flag,
1143 };
1144
1145 if (meta_flag & CMD_ASYNC)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001146 cmd.meta.u.callback = iwl3945_card_state_sync_callback;
Zhu Yib481de92007-09-25 17:54:57 -07001147
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001148 return iwl3945_send_cmd(priv, &cmd);
Zhu Yib481de92007-09-25 17:54:57 -07001149}
1150
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001151static int iwl3945_add_sta_sync_callback(struct iwl_priv *priv,
Winkler, Tomasc2d79b482008-12-19 10:37:34 +08001152 struct iwl_cmd *cmd, struct sk_buff *skb)
Zhu Yib481de92007-09-25 17:54:57 -07001153{
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08001154 struct iwl_rx_packet *res = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07001155
1156 if (!skb) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08001157 IWL_ERR(priv, "Error: Response NULL in REPLY_ADD_STA.\n");
Zhu Yib481de92007-09-25 17:54:57 -07001158 return 1;
1159 }
1160
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08001161 res = (struct iwl_rx_packet *)skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07001162 if (res->hdr.flags & IWL_CMD_FAILED_MSK) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08001163 IWL_ERR(priv, "Bad return from REPLY_ADD_STA (0x%08X)\n",
Zhu Yib481de92007-09-25 17:54:57 -07001164 res->hdr.flags);
1165 return 1;
1166 }
1167
1168 switch (res->u.add_sta.status) {
1169 case ADD_STA_SUCCESS_MSK:
1170 break;
1171 default:
1172 break;
1173 }
1174
1175 /* We didn't cache the SKB; let the caller free it */
1176 return 1;
1177}
1178
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001179int iwl3945_send_add_station(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001180 struct iwl3945_addsta_cmd *sta, u8 flags)
Zhu Yib481de92007-09-25 17:54:57 -07001181{
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08001182 struct iwl_rx_packet *res = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07001183 int rc = 0;
Winkler, Tomasc2d79b482008-12-19 10:37:34 +08001184 struct iwl_host_cmd cmd = {
Zhu Yib481de92007-09-25 17:54:57 -07001185 .id = REPLY_ADD_STA,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001186 .len = sizeof(struct iwl3945_addsta_cmd),
Zhu Yib481de92007-09-25 17:54:57 -07001187 .meta.flags = flags,
1188 .data = sta,
1189 };
1190
1191 if (flags & CMD_ASYNC)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001192 cmd.meta.u.callback = iwl3945_add_sta_sync_callback;
Zhu Yib481de92007-09-25 17:54:57 -07001193 else
1194 cmd.meta.flags |= CMD_WANT_SKB;
1195
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001196 rc = iwl3945_send_cmd(priv, &cmd);
Zhu Yib481de92007-09-25 17:54:57 -07001197
1198 if (rc || (flags & CMD_ASYNC))
1199 return rc;
1200
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08001201 res = (struct iwl_rx_packet *)cmd.meta.u.skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07001202 if (res->hdr.flags & IWL_CMD_FAILED_MSK) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08001203 IWL_ERR(priv, "Bad return from REPLY_ADD_STA (0x%08X)\n",
Zhu Yib481de92007-09-25 17:54:57 -07001204 res->hdr.flags);
1205 rc = -EIO;
1206 }
1207
1208 if (rc == 0) {
1209 switch (res->u.add_sta.status) {
1210 case ADD_STA_SUCCESS_MSK:
1211 IWL_DEBUG_INFO("REPLY_ADD_STA PASSED\n");
1212 break;
1213 default:
1214 rc = -EIO;
Winkler, Tomas39aadf82008-12-19 10:37:32 +08001215 IWL_WARN(priv, "REPLY_ADD_STA failed\n");
Zhu Yib481de92007-09-25 17:54:57 -07001216 break;
1217 }
1218 }
1219
1220 priv->alloc_rxb_skb--;
1221 dev_kfree_skb_any(cmd.meta.u.skb);
1222
1223 return rc;
1224}
1225
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001226static int iwl3945_update_sta_key_info(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07001227 struct ieee80211_key_conf *keyconf,
1228 u8 sta_id)
1229{
1230 unsigned long flags;
1231 __le16 key_flags = 0;
1232
1233 switch (keyconf->alg) {
1234 case ALG_CCMP:
1235 key_flags |= STA_KEY_FLG_CCMP;
1236 key_flags |= cpu_to_le16(
1237 keyconf->keyidx << STA_KEY_FLG_KEYID_POS);
1238 key_flags &= ~STA_KEY_FLG_INVALID;
1239 break;
1240 case ALG_TKIP:
1241 case ALG_WEP:
Zhu Yib481de92007-09-25 17:54:57 -07001242 default:
1243 return -EINVAL;
1244 }
1245 spin_lock_irqsave(&priv->sta_lock, flags);
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001246 priv->stations_39[sta_id].keyinfo.alg = keyconf->alg;
1247 priv->stations_39[sta_id].keyinfo.keylen = keyconf->keylen;
1248 memcpy(priv->stations_39[sta_id].keyinfo.key, keyconf->key,
Zhu Yib481de92007-09-25 17:54:57 -07001249 keyconf->keylen);
1250
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001251 memcpy(priv->stations_39[sta_id].sta.key.key, keyconf->key,
Zhu Yib481de92007-09-25 17:54:57 -07001252 keyconf->keylen);
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001253 priv->stations_39[sta_id].sta.key.key_flags = key_flags;
1254 priv->stations_39[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
1255 priv->stations_39[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07001256
1257 spin_unlock_irqrestore(&priv->sta_lock, flags);
1258
1259 IWL_DEBUG_INFO("hwcrypto: modify ucode station key info\n");
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001260 iwl3945_send_add_station(priv, &priv->stations_39[sta_id].sta, 0);
Zhu Yib481de92007-09-25 17:54:57 -07001261 return 0;
1262}
1263
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001264static int iwl3945_clear_sta_key_info(struct iwl_priv *priv, u8 sta_id)
Zhu Yib481de92007-09-25 17:54:57 -07001265{
1266 unsigned long flags;
1267
1268 spin_lock_irqsave(&priv->sta_lock, flags);
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001269 memset(&priv->stations_39[sta_id].keyinfo, 0, sizeof(struct iwl3945_hw_key));
1270 memset(&priv->stations_39[sta_id].sta.key, 0,
Tomas Winkler4c897252008-12-19 10:37:05 +08001271 sizeof(struct iwl4965_keyinfo));
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001272 priv->stations_39[sta_id].sta.key.key_flags = STA_KEY_FLG_NO_ENC;
1273 priv->stations_39[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
1274 priv->stations_39[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07001275 spin_unlock_irqrestore(&priv->sta_lock, flags);
1276
1277 IWL_DEBUG_INFO("hwcrypto: clear ucode station key info\n");
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001278 iwl3945_send_add_station(priv, &priv->stations_39[sta_id].sta, 0);
Zhu Yib481de92007-09-25 17:54:57 -07001279 return 0;
1280}
1281
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001282static void iwl3945_clear_free_frames(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001283{
1284 struct list_head *element;
1285
1286 IWL_DEBUG_INFO("%d frames on pre-allocated heap on clear.\n",
1287 priv->frames_count);
1288
1289 while (!list_empty(&priv->free_frames)) {
1290 element = priv->free_frames.next;
1291 list_del(element);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001292 kfree(list_entry(element, struct iwl3945_frame, list));
Zhu Yib481de92007-09-25 17:54:57 -07001293 priv->frames_count--;
1294 }
1295
1296 if (priv->frames_count) {
Winkler, Tomas39aadf82008-12-19 10:37:32 +08001297 IWL_WARN(priv, "%d frames still in use. Did we lose one?\n",
Zhu Yib481de92007-09-25 17:54:57 -07001298 priv->frames_count);
1299 priv->frames_count = 0;
1300 }
1301}
1302
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001303static struct iwl3945_frame *iwl3945_get_free_frame(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001304{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001305 struct iwl3945_frame *frame;
Zhu Yib481de92007-09-25 17:54:57 -07001306 struct list_head *element;
1307 if (list_empty(&priv->free_frames)) {
1308 frame = kzalloc(sizeof(*frame), GFP_KERNEL);
1309 if (!frame) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08001310 IWL_ERR(priv, "Could not allocate frame!\n");
Zhu Yib481de92007-09-25 17:54:57 -07001311 return NULL;
1312 }
1313
1314 priv->frames_count++;
1315 return frame;
1316 }
1317
1318 element = priv->free_frames.next;
1319 list_del(element);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001320 return list_entry(element, struct iwl3945_frame, list);
Zhu Yib481de92007-09-25 17:54:57 -07001321}
1322
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001323static void iwl3945_free_frame(struct iwl_priv *priv, struct iwl3945_frame *frame)
Zhu Yib481de92007-09-25 17:54:57 -07001324{
1325 memset(frame, 0, sizeof(*frame));
1326 list_add(&frame->list, &priv->free_frames);
1327}
1328
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001329unsigned int iwl3945_fill_beacon_frame(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07001330 struct ieee80211_hdr *hdr,
Rami Rosen73ec1cc2008-12-16 09:37:07 +02001331 int left)
Zhu Yib481de92007-09-25 17:54:57 -07001332{
1333
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001334 if (!iwl3945_is_associated(priv) || !priv->ibss_beacon ||
Johannes Berg05c914f2008-09-11 00:01:58 +02001335 ((priv->iw_mode != NL80211_IFTYPE_ADHOC) &&
1336 (priv->iw_mode != NL80211_IFTYPE_AP)))
Zhu Yib481de92007-09-25 17:54:57 -07001337 return 0;
1338
1339 if (priv->ibss_beacon->len > left)
1340 return 0;
1341
1342 memcpy(hdr, priv->ibss_beacon->data, priv->ibss_beacon->len);
1343
1344 return priv->ibss_beacon->len;
1345}
1346
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001347static u8 iwl3945_rate_get_lowest_plcp(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001348{
1349 u8 i;
Kolekar, Abhijeetc24f0812008-11-07 09:58:44 -08001350 int rate_mask;
1351
1352 /* Set rate mask*/
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001353 if (priv->staging39_rxon.flags & RXON_FLG_BAND_24G_MSK)
Chatre, Reinettedbce56a2008-11-12 13:14:07 -08001354 rate_mask = priv->active_rate_basic & IWL_CCK_RATES_MASK;
Kolekar, Abhijeetc24f0812008-11-07 09:58:44 -08001355 else
Chatre, Reinettedbce56a2008-11-12 13:14:07 -08001356 rate_mask = priv->active_rate_basic & IWL_OFDM_RATES_MASK;
Zhu Yib481de92007-09-25 17:54:57 -07001357
1358 for (i = IWL_RATE_1M_INDEX; i != IWL_RATE_INVALID;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001359 i = iwl3945_rates[i].next_ieee) {
Zhu Yib481de92007-09-25 17:54:57 -07001360 if (rate_mask & (1 << i))
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001361 return iwl3945_rates[i].plcp;
Zhu Yib481de92007-09-25 17:54:57 -07001362 }
1363
Kolekar, Abhijeetc24f0812008-11-07 09:58:44 -08001364 /* No valid rate was found. Assign the lowest one */
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001365 if (priv->staging39_rxon.flags & RXON_FLG_BAND_24G_MSK)
Kolekar, Abhijeetc24f0812008-11-07 09:58:44 -08001366 return IWL_RATE_1M_PLCP;
1367 else
1368 return IWL_RATE_6M_PLCP;
Zhu Yib481de92007-09-25 17:54:57 -07001369}
1370
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001371static int iwl3945_send_beacon_cmd(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001372{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001373 struct iwl3945_frame *frame;
Zhu Yib481de92007-09-25 17:54:57 -07001374 unsigned int frame_size;
1375 int rc;
1376 u8 rate;
1377
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001378 frame = iwl3945_get_free_frame(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001379
1380 if (!frame) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08001381 IWL_ERR(priv, "Could not obtain free frame buffer for beacon "
Zhu Yib481de92007-09-25 17:54:57 -07001382 "command.\n");
1383 return -ENOMEM;
1384 }
1385
Kolekar, Abhijeetc24f0812008-11-07 09:58:44 -08001386 rate = iwl3945_rate_get_lowest_plcp(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001387
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001388 frame_size = iwl3945_hw_get_beacon_cmd(priv, frame, rate);
Zhu Yib481de92007-09-25 17:54:57 -07001389
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001390 rc = iwl3945_send_cmd_pdu(priv, REPLY_TX_BEACON, frame_size,
Zhu Yib481de92007-09-25 17:54:57 -07001391 &frame->u.cmd[0]);
1392
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001393 iwl3945_free_frame(priv, frame);
Zhu Yib481de92007-09-25 17:54:57 -07001394
1395 return rc;
1396}
1397
1398/******************************************************************************
1399 *
1400 * EEPROM related functions
1401 *
1402 ******************************************************************************/
1403
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001404static void get_eeprom_mac(struct iwl_priv *priv, u8 *mac)
Zhu Yib481de92007-09-25 17:54:57 -07001405{
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001406 memcpy(mac, priv->eeprom39.mac_address, 6);
Zhu Yib481de92007-09-25 17:54:57 -07001407}
1408
Reinette Chatre74a3a252008-01-23 10:15:19 -08001409/*
1410 * Clear the OWNER_MSK, to establish driver (instead of uCode running on
1411 * embedded controller) as EEPROM reader; each read is a series of pulses
1412 * to/from the EEPROM chip, not a single event, so even reads could conflict
1413 * if they weren't arbitrated by some ownership mechanism. Here, the driver
1414 * simply claims ownership, which should be safe when this function is called
1415 * (i.e. before loading uCode!).
1416 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001417static inline int iwl3945_eeprom_acquire_semaphore(struct iwl_priv *priv)
Reinette Chatre74a3a252008-01-23 10:15:19 -08001418{
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08001419 _iwl_clear_bit(priv, CSR_EEPROM_GP, CSR_EEPROM_GP_IF_OWNER_MSK);
Reinette Chatre74a3a252008-01-23 10:15:19 -08001420 return 0;
1421}
1422
Zhu Yib481de92007-09-25 17:54:57 -07001423/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001424 * iwl3945_eeprom_init - read EEPROM contents
Zhu Yib481de92007-09-25 17:54:57 -07001425 *
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001426 * Load the EEPROM contents from adapter into priv->eeprom39
Zhu Yib481de92007-09-25 17:54:57 -07001427 *
1428 * NOTE: This routine uses the non-debug IO access functions.
1429 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001430int iwl3945_eeprom_init(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001431{
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001432 u16 *e = (u16 *)&priv->eeprom39;
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08001433 u32 gp = iwl_read32(priv, CSR_EEPROM_GP);
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001434 int sz = sizeof(priv->eeprom39);
Zhu, Yi3d5717a2008-12-11 10:33:36 -08001435 int ret;
Zhu Yib481de92007-09-25 17:54:57 -07001436 u16 addr;
1437
1438 /* The EEPROM structure has several padding buffers within it
1439 * and when adding new EEPROM maps is subject to programmer errors
1440 * which may be very difficult to identify without explicitly
1441 * checking the resulting size of the eeprom map. */
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001442 BUILD_BUG_ON(sizeof(priv->eeprom39) != IWL_EEPROM_IMAGE_SIZE);
Zhu Yib481de92007-09-25 17:54:57 -07001443
1444 if ((gp & CSR_EEPROM_GP_VALID_MSK) == CSR_EEPROM_GP_BAD_SIGNATURE) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08001445 IWL_ERR(priv, "EEPROM not found, EEPROM_GP=0x%08x\n", gp);
Zhu Yib481de92007-09-25 17:54:57 -07001446 return -ENOENT;
1447 }
1448
Cahill, Ben M6440adb2007-11-29 11:09:55 +08001449 /* Make sure driver (instead of uCode) is allowed to read EEPROM */
Zhu, Yi3d5717a2008-12-11 10:33:36 -08001450 ret = iwl3945_eeprom_acquire_semaphore(priv);
1451 if (ret < 0) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08001452 IWL_ERR(priv, "Failed to acquire EEPROM semaphore.\n");
Zhu Yib481de92007-09-25 17:54:57 -07001453 return -ENOENT;
1454 }
1455
1456 /* eeprom is an array of 16bit values */
1457 for (addr = 0; addr < sz; addr += sizeof(u16)) {
Zhu, Yi3d5717a2008-12-11 10:33:36 -08001458 u32 r;
1459
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08001460 _iwl_write32(priv, CSR_EEPROM_REG,
Zhu, Yi3d5717a2008-12-11 10:33:36 -08001461 CSR_EEPROM_REG_MSK_ADDR & (addr << 1));
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08001462 _iwl_clear_bit(priv, CSR_EEPROM_REG, CSR_EEPROM_REG_BIT_CMD);
1463 ret = iwl_poll_direct_bit(priv, CSR_EEPROM_REG,
Zhu, Yi3d5717a2008-12-11 10:33:36 -08001464 CSR_EEPROM_REG_READ_VALID_MSK,
1465 IWL_EEPROM_ACCESS_TIMEOUT);
1466 if (ret < 0) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08001467 IWL_ERR(priv, "Time out reading EEPROM[%d]\n", addr);
Zhu, Yi3d5717a2008-12-11 10:33:36 -08001468 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07001469 }
Zhu, Yi3d5717a2008-12-11 10:33:36 -08001470
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08001471 r = _iwl_read_direct32(priv, CSR_EEPROM_REG);
Tomas Winkler58ff6d42008-02-13 02:47:54 +02001472 e[addr / 2] = le16_to_cpu((__force __le16)(r >> 16));
Zhu Yib481de92007-09-25 17:54:57 -07001473 }
1474
1475 return 0;
1476}
1477
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001478static void iwl3945_unset_hw_params(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001479{
Abhijeet Kolekar3832ec92008-12-19 10:37:26 +08001480 if (priv->shared_virt)
Zhu Yib481de92007-09-25 17:54:57 -07001481 pci_free_consistent(priv->pci_dev,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001482 sizeof(struct iwl3945_shared),
Abhijeet Kolekar3832ec92008-12-19 10:37:26 +08001483 priv->shared_virt,
1484 priv->shared_phys);
Zhu Yib481de92007-09-25 17:54:57 -07001485}
1486
1487/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001488 * iwl3945_supported_rate_to_ie - fill in the supported rate in IE field
Zhu Yib481de92007-09-25 17:54:57 -07001489 *
1490 * return : set the bit for each supported rate insert in ie
1491 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001492static u16 iwl3945_supported_rate_to_ie(u8 *ie, u16 supported_rate,
Tomas Winklerc7c46672007-10-18 02:04:15 +02001493 u16 basic_rate, int *left)
Zhu Yib481de92007-09-25 17:54:57 -07001494{
1495 u16 ret_rates = 0, bit;
1496 int i;
Tomas Winklerc7c46672007-10-18 02:04:15 +02001497 u8 *cnt = ie;
1498 u8 *rates = ie + 1;
Zhu Yib481de92007-09-25 17:54:57 -07001499
1500 for (bit = 1, i = 0; i < IWL_RATE_COUNT; i++, bit <<= 1) {
1501 if (bit & supported_rate) {
1502 ret_rates |= bit;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001503 rates[*cnt] = iwl3945_rates[i].ieee |
Tomas Winklerc7c46672007-10-18 02:04:15 +02001504 ((bit & basic_rate) ? 0x80 : 0x00);
1505 (*cnt)++;
1506 (*left)--;
1507 if ((*left <= 0) ||
1508 (*cnt >= IWL_SUPPORTED_RATES_IE_LEN))
Zhu Yib481de92007-09-25 17:54:57 -07001509 break;
1510 }
1511 }
1512
1513 return ret_rates;
1514}
1515
1516/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001517 * iwl3945_fill_probe_req - fill in all required fields and IE for probe request
Zhu Yib481de92007-09-25 17:54:57 -07001518 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001519static u16 iwl3945_fill_probe_req(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07001520 struct ieee80211_mgmt *frame,
Johannes Berg430cfe92008-10-28 18:06:02 +01001521 int left)
Zhu Yib481de92007-09-25 17:54:57 -07001522{
1523 int len = 0;
1524 u8 *pos = NULL;
Tomas Winklerc7c46672007-10-18 02:04:15 +02001525 u16 active_rates, ret_rates, cck_rates;
Zhu Yib481de92007-09-25 17:54:57 -07001526
1527 /* Make sure there is enough space for the probe request,
1528 * two mandatory IEs and the data */
1529 left -= 24;
1530 if (left < 0)
1531 return 0;
1532 len += 24;
1533
1534 frame->frame_control = cpu_to_le16(IEEE80211_STYPE_PROBE_REQ);
Kolekar, Abhijeetb5323d32008-12-19 10:37:22 +08001535 memcpy(frame->da, iwl_bcast_addr, ETH_ALEN);
Zhu Yib481de92007-09-25 17:54:57 -07001536 memcpy(frame->sa, priv->mac_addr, ETH_ALEN);
Kolekar, Abhijeetb5323d32008-12-19 10:37:22 +08001537 memcpy(frame->bssid, iwl_bcast_addr, ETH_ALEN);
Zhu Yib481de92007-09-25 17:54:57 -07001538 frame->seq_ctrl = 0;
1539
1540 /* fill in our indirect SSID IE */
1541 /* ...next IE... */
1542
1543 left -= 2;
1544 if (left < 0)
1545 return 0;
1546 len += 2;
1547 pos = &(frame->u.probe_req.variable[0]);
1548 *pos++ = WLAN_EID_SSID;
1549 *pos++ = 0;
1550
Zhu Yib481de92007-09-25 17:54:57 -07001551 /* fill in supported rate */
1552 /* ...next IE... */
1553 left -= 2;
1554 if (left < 0)
1555 return 0;
Tomas Winklerc7c46672007-10-18 02:04:15 +02001556
Zhu Yib481de92007-09-25 17:54:57 -07001557 /* ... fill it in... */
1558 *pos++ = WLAN_EID_SUPP_RATES;
1559 *pos = 0;
Tomas Winklerc7c46672007-10-18 02:04:15 +02001560
1561 priv->active_rate = priv->rates_mask;
1562 active_rates = priv->active_rate;
Zhu Yib481de92007-09-25 17:54:57 -07001563 priv->active_rate_basic = priv->rates_mask & IWL_BASIC_RATES_MASK;
1564
Tomas Winklerc7c46672007-10-18 02:04:15 +02001565 cck_rates = IWL_CCK_RATES_MASK & active_rates;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001566 ret_rates = iwl3945_supported_rate_to_ie(pos, cck_rates,
Tomas Winklerc7c46672007-10-18 02:04:15 +02001567 priv->active_rate_basic, &left);
1568 active_rates &= ~ret_rates;
1569
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001570 ret_rates = iwl3945_supported_rate_to_ie(pos, active_rates,
Tomas Winklerc7c46672007-10-18 02:04:15 +02001571 priv->active_rate_basic, &left);
1572 active_rates &= ~ret_rates;
1573
Zhu Yib481de92007-09-25 17:54:57 -07001574 len += 2 + *pos;
1575 pos += (*pos) + 1;
Tomas Winklerc7c46672007-10-18 02:04:15 +02001576 if (active_rates == 0)
Zhu Yib481de92007-09-25 17:54:57 -07001577 goto fill_end;
1578
1579 /* fill in supported extended rate */
1580 /* ...next IE... */
1581 left -= 2;
1582 if (left < 0)
1583 return 0;
1584 /* ... fill it in... */
1585 *pos++ = WLAN_EID_EXT_SUPP_RATES;
1586 *pos = 0;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001587 iwl3945_supported_rate_to_ie(pos, active_rates,
Tomas Winklerc7c46672007-10-18 02:04:15 +02001588 priv->active_rate_basic, &left);
Zhu Yib481de92007-09-25 17:54:57 -07001589 if (*pos > 0)
1590 len += 2 + *pos;
1591
1592 fill_end:
1593 return (u16)len;
1594}
1595
1596/*
1597 * QoS support
1598*/
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001599static int iwl3945_send_qos_params_command(struct iwl_priv *priv,
Tomas Winkler4c897252008-12-19 10:37:05 +08001600 struct iwl_qosparam_cmd *qos)
Zhu Yib481de92007-09-25 17:54:57 -07001601{
1602
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001603 return iwl3945_send_cmd_pdu(priv, REPLY_QOS_PARAM,
Tomas Winkler4c897252008-12-19 10:37:05 +08001604 sizeof(struct iwl_qosparam_cmd), qos);
Zhu Yib481de92007-09-25 17:54:57 -07001605}
1606
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001607static void iwl3945_reset_qos(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001608{
1609 u16 cw_min = 15;
1610 u16 cw_max = 1023;
1611 u8 aifs = 2;
1612 u8 is_legacy = 0;
1613 unsigned long flags;
1614 int i;
1615
1616 spin_lock_irqsave(&priv->lock, flags);
1617 priv->qos_data.qos_active = 0;
1618
Winkler, Tomas6d1ef1a2008-12-09 11:29:00 -08001619 /* QoS always active in AP and ADHOC mode
1620 * In STA mode wait for association
1621 */
1622 if (priv->iw_mode == NL80211_IFTYPE_ADHOC ||
1623 priv->iw_mode == NL80211_IFTYPE_AP)
1624 priv->qos_data.qos_active = 1;
1625 else
1626 priv->qos_data.qos_active = 0;
1627
1628
1629 /* check for legacy mode */
1630 if ((priv->iw_mode == NL80211_IFTYPE_ADHOC &&
1631 (priv->active_rate & IWL_OFDM_RATES_MASK) == 0) ||
1632 (priv->iw_mode == NL80211_IFTYPE_STATION &&
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001633 (priv->staging39_rxon.flags & RXON_FLG_SHORT_SLOT_MSK) == 0)) {
Zhu Yib481de92007-09-25 17:54:57 -07001634 cw_min = 31;
1635 is_legacy = 1;
1636 }
1637
1638 if (priv->qos_data.qos_active)
1639 aifs = 3;
1640
1641 priv->qos_data.def_qos_parm.ac[0].cw_min = cpu_to_le16(cw_min);
1642 priv->qos_data.def_qos_parm.ac[0].cw_max = cpu_to_le16(cw_max);
1643 priv->qos_data.def_qos_parm.ac[0].aifsn = aifs;
1644 priv->qos_data.def_qos_parm.ac[0].edca_txop = 0;
1645 priv->qos_data.def_qos_parm.ac[0].reserved1 = 0;
1646
1647 if (priv->qos_data.qos_active) {
1648 i = 1;
1649 priv->qos_data.def_qos_parm.ac[i].cw_min = cpu_to_le16(cw_min);
1650 priv->qos_data.def_qos_parm.ac[i].cw_max = cpu_to_le16(cw_max);
1651 priv->qos_data.def_qos_parm.ac[i].aifsn = 7;
1652 priv->qos_data.def_qos_parm.ac[i].edca_txop = 0;
1653 priv->qos_data.def_qos_parm.ac[i].reserved1 = 0;
1654
1655 i = 2;
1656 priv->qos_data.def_qos_parm.ac[i].cw_min =
1657 cpu_to_le16((cw_min + 1) / 2 - 1);
1658 priv->qos_data.def_qos_parm.ac[i].cw_max =
1659 cpu_to_le16(cw_max);
1660 priv->qos_data.def_qos_parm.ac[i].aifsn = 2;
1661 if (is_legacy)
1662 priv->qos_data.def_qos_parm.ac[i].edca_txop =
1663 cpu_to_le16(6016);
1664 else
1665 priv->qos_data.def_qos_parm.ac[i].edca_txop =
1666 cpu_to_le16(3008);
1667 priv->qos_data.def_qos_parm.ac[i].reserved1 = 0;
1668
1669 i = 3;
1670 priv->qos_data.def_qos_parm.ac[i].cw_min =
1671 cpu_to_le16((cw_min + 1) / 4 - 1);
1672 priv->qos_data.def_qos_parm.ac[i].cw_max =
1673 cpu_to_le16((cw_max + 1) / 2 - 1);
1674 priv->qos_data.def_qos_parm.ac[i].aifsn = 2;
1675 priv->qos_data.def_qos_parm.ac[i].reserved1 = 0;
1676 if (is_legacy)
1677 priv->qos_data.def_qos_parm.ac[i].edca_txop =
1678 cpu_to_le16(3264);
1679 else
1680 priv->qos_data.def_qos_parm.ac[i].edca_txop =
1681 cpu_to_le16(1504);
1682 } else {
1683 for (i = 1; i < 4; i++) {
1684 priv->qos_data.def_qos_parm.ac[i].cw_min =
1685 cpu_to_le16(cw_min);
1686 priv->qos_data.def_qos_parm.ac[i].cw_max =
1687 cpu_to_le16(cw_max);
1688 priv->qos_data.def_qos_parm.ac[i].aifsn = aifs;
1689 priv->qos_data.def_qos_parm.ac[i].edca_txop = 0;
1690 priv->qos_data.def_qos_parm.ac[i].reserved1 = 0;
1691 }
1692 }
1693 IWL_DEBUG_QOS("set QoS to default \n");
1694
1695 spin_unlock_irqrestore(&priv->lock, flags);
1696}
1697
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001698static void iwl3945_activate_qos(struct iwl_priv *priv, u8 force)
Zhu Yib481de92007-09-25 17:54:57 -07001699{
1700 unsigned long flags;
1701
Zhu Yib481de92007-09-25 17:54:57 -07001702 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
1703 return;
1704
Zhu Yib481de92007-09-25 17:54:57 -07001705 spin_lock_irqsave(&priv->lock, flags);
1706 priv->qos_data.def_qos_parm.qos_flags = 0;
1707
1708 if (priv->qos_data.qos_cap.q_AP.queue_request &&
1709 !priv->qos_data.qos_cap.q_AP.txop_request)
1710 priv->qos_data.def_qos_parm.qos_flags |=
1711 QOS_PARAM_FLG_TXOP_TYPE_MSK;
1712
1713 if (priv->qos_data.qos_active)
1714 priv->qos_data.def_qos_parm.qos_flags |=
1715 QOS_PARAM_FLG_UPDATE_EDCA_MSK;
1716
1717 spin_unlock_irqrestore(&priv->lock, flags);
1718
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001719 if (force || iwl3945_is_associated(priv)) {
Tomas Winklera96a27f2008-10-23 23:48:56 -07001720 IWL_DEBUG_QOS("send QoS cmd with QoS active %d \n",
Zhu Yib481de92007-09-25 17:54:57 -07001721 priv->qos_data.qos_active);
1722
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001723 iwl3945_send_qos_params_command(priv,
Zhu Yib481de92007-09-25 17:54:57 -07001724 &(priv->qos_data.def_qos_parm));
1725 }
1726}
1727
Zhu Yib481de92007-09-25 17:54:57 -07001728/*
1729 * Power management (not Tx power!) functions
1730 */
1731#define MSEC_TO_USEC 1024
1732
Tomas Winkler600c0e12008-12-19 10:37:04 +08001733
1734#define NOSLP __constant_cpu_to_le16(0), 0, 0
1735#define SLP IWL_POWER_DRIVER_ALLOW_SLEEP_MSK, 0, 0
Zhu Yib481de92007-09-25 17:54:57 -07001736#define SLP_TIMEOUT(T) __constant_cpu_to_le32((T) * MSEC_TO_USEC)
1737#define SLP_VEC(X0, X1, X2, X3, X4) {__constant_cpu_to_le32(X0), \
1738 __constant_cpu_to_le32(X1), \
1739 __constant_cpu_to_le32(X2), \
1740 __constant_cpu_to_le32(X3), \
1741 __constant_cpu_to_le32(X4)}
1742
Zhu Yib481de92007-09-25 17:54:57 -07001743/* default power management (not Tx power) table values */
Tomas Winklera96a27f2008-10-23 23:48:56 -07001744/* for TIM 0-10 */
Samuel Ortiz1125eff2008-12-19 10:37:14 +08001745static struct iwl_power_vec_entry range_0[IWL39_POWER_AC] = {
Zhu Yib481de92007-09-25 17:54:57 -07001746 {{NOSLP, SLP_TIMEOUT(0), SLP_TIMEOUT(0), SLP_VEC(0, 0, 0, 0, 0)}, 0},
1747 {{SLP, SLP_TIMEOUT(200), SLP_TIMEOUT(500), SLP_VEC(1, 2, 3, 4, 4)}, 0},
1748 {{SLP, SLP_TIMEOUT(200), SLP_TIMEOUT(300), SLP_VEC(2, 4, 6, 7, 7)}, 0},
1749 {{SLP, SLP_TIMEOUT(50), SLP_TIMEOUT(100), SLP_VEC(2, 6, 9, 9, 10)}, 0},
1750 {{SLP, SLP_TIMEOUT(50), SLP_TIMEOUT(25), SLP_VEC(2, 7, 9, 9, 10)}, 1},
1751 {{SLP, SLP_TIMEOUT(25), SLP_TIMEOUT(25), SLP_VEC(4, 7, 10, 10, 10)}, 1}
1752};
1753
Tomas Winklera96a27f2008-10-23 23:48:56 -07001754/* for TIM > 10 */
Samuel Ortiz1125eff2008-12-19 10:37:14 +08001755static struct iwl_power_vec_entry range_1[IWL39_POWER_AC] = {
Zhu Yib481de92007-09-25 17:54:57 -07001756 {{NOSLP, SLP_TIMEOUT(0), SLP_TIMEOUT(0), SLP_VEC(0, 0, 0, 0, 0)}, 0},
1757 {{SLP, SLP_TIMEOUT(200), SLP_TIMEOUT(500),
1758 SLP_VEC(1, 2, 3, 4, 0xFF)}, 0},
1759 {{SLP, SLP_TIMEOUT(200), SLP_TIMEOUT(300),
1760 SLP_VEC(2, 4, 6, 7, 0xFF)}, 0},
1761 {{SLP, SLP_TIMEOUT(50), SLP_TIMEOUT(100),
1762 SLP_VEC(2, 6, 9, 9, 0xFF)}, 0},
1763 {{SLP, SLP_TIMEOUT(50), SLP_TIMEOUT(25), SLP_VEC(2, 7, 9, 9, 0xFF)}, 0},
1764 {{SLP, SLP_TIMEOUT(25), SLP_TIMEOUT(25),
1765 SLP_VEC(4, 7, 10, 10, 0xFF)}, 0}
1766};
1767
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001768int iwl3945_power_init_handle(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001769{
1770 int rc = 0, i;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001771 struct iwl3945_power_mgr *pow_data;
Samuel Ortiz1125eff2008-12-19 10:37:14 +08001772 int size = sizeof(struct iwl_power_vec_entry) * IWL39_POWER_AC;
Zhu Yib481de92007-09-25 17:54:57 -07001773 u16 pci_pm;
1774
1775 IWL_DEBUG_POWER("Initialize power \n");
1776
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001777 pow_data = &(priv->power_data_39);
Zhu Yib481de92007-09-25 17:54:57 -07001778
1779 memset(pow_data, 0, sizeof(*pow_data));
1780
1781 pow_data->active_index = IWL_POWER_RANGE_0;
1782 pow_data->dtim_val = 0xffff;
1783
1784 memcpy(&pow_data->pwr_range_0[0], &range_0[0], size);
1785 memcpy(&pow_data->pwr_range_1[0], &range_1[0], size);
1786
1787 rc = pci_read_config_word(priv->pci_dev, PCI_LINK_CTRL, &pci_pm);
1788 if (rc != 0)
1789 return 0;
1790 else {
Tomas Winkler600c0e12008-12-19 10:37:04 +08001791 struct iwl_powertable_cmd *cmd;
Zhu Yib481de92007-09-25 17:54:57 -07001792
1793 IWL_DEBUG_POWER("adjust power command flags\n");
1794
Samuel Ortiz1125eff2008-12-19 10:37:14 +08001795 for (i = 0; i < IWL39_POWER_AC; i++) {
Zhu Yib481de92007-09-25 17:54:57 -07001796 cmd = &pow_data->pwr_range_0[i].cmd;
1797
1798 if (pci_pm & 0x1)
1799 cmd->flags &= ~IWL_POWER_PCI_PM_MSK;
1800 else
1801 cmd->flags |= IWL_POWER_PCI_PM_MSK;
1802 }
1803 }
1804 return rc;
1805}
1806
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001807static int iwl3945_update_power_cmd(struct iwl_priv *priv,
Tomas Winkler600c0e12008-12-19 10:37:04 +08001808 struct iwl_powertable_cmd *cmd, u32 mode)
Zhu Yib481de92007-09-25 17:54:57 -07001809{
1810 int rc = 0, i;
1811 u8 skip;
1812 u32 max_sleep = 0;
Samuel Ortiz1125eff2008-12-19 10:37:14 +08001813 struct iwl_power_vec_entry *range;
Zhu Yib481de92007-09-25 17:54:57 -07001814 u8 period = 0;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001815 struct iwl3945_power_mgr *pow_data;
Zhu Yib481de92007-09-25 17:54:57 -07001816
1817 if (mode > IWL_POWER_INDEX_5) {
1818 IWL_DEBUG_POWER("Error invalid power mode \n");
1819 return -1;
1820 }
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001821 pow_data = &(priv->power_data_39);
Zhu Yib481de92007-09-25 17:54:57 -07001822
1823 if (pow_data->active_index == IWL_POWER_RANGE_0)
1824 range = &pow_data->pwr_range_0[0];
1825 else
1826 range = &pow_data->pwr_range_1[1];
1827
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001828 memcpy(cmd, &range[mode].cmd, sizeof(struct iwl3945_powertable_cmd));
Zhu Yib481de92007-09-25 17:54:57 -07001829
1830#ifdef IWL_MAC80211_DISABLE
1831 if (priv->assoc_network != NULL) {
1832 unsigned long flags;
1833
1834 period = priv->assoc_network->tim.tim_period;
1835 }
1836#endif /*IWL_MAC80211_DISABLE */
1837 skip = range[mode].no_dtim;
1838
1839 if (period == 0) {
1840 period = 1;
1841 skip = 0;
1842 }
1843
1844 if (skip == 0) {
1845 max_sleep = period;
1846 cmd->flags &= ~IWL_POWER_SLEEP_OVER_DTIM_MSK;
1847 } else {
1848 __le32 slp_itrvl = cmd->sleep_interval[IWL_POWER_VEC_SIZE - 1];
1849 max_sleep = (le32_to_cpu(slp_itrvl) / period) * period;
1850 cmd->flags |= IWL_POWER_SLEEP_OVER_DTIM_MSK;
1851 }
1852
1853 for (i = 0; i < IWL_POWER_VEC_SIZE; i++) {
1854 if (le32_to_cpu(cmd->sleep_interval[i]) > max_sleep)
1855 cmd->sleep_interval[i] = cpu_to_le32(max_sleep);
1856 }
1857
1858 IWL_DEBUG_POWER("Flags value = 0x%08X\n", cmd->flags);
1859 IWL_DEBUG_POWER("Tx timeout = %u\n", le32_to_cpu(cmd->tx_data_timeout));
1860 IWL_DEBUG_POWER("Rx timeout = %u\n", le32_to_cpu(cmd->rx_data_timeout));
1861 IWL_DEBUG_POWER("Sleep interval vector = { %d , %d , %d , %d , %d }\n",
1862 le32_to_cpu(cmd->sleep_interval[0]),
1863 le32_to_cpu(cmd->sleep_interval[1]),
1864 le32_to_cpu(cmd->sleep_interval[2]),
1865 le32_to_cpu(cmd->sleep_interval[3]),
1866 le32_to_cpu(cmd->sleep_interval[4]));
1867
1868 return rc;
1869}
1870
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001871static int iwl3945_send_power_mode(struct iwl_priv *priv, u32 mode)
Zhu Yib481de92007-09-25 17:54:57 -07001872{
John W. Linville9a62f732007-11-15 16:27:36 -05001873 u32 uninitialized_var(final_mode);
Zhu Yib481de92007-09-25 17:54:57 -07001874 int rc;
Tomas Winkler600c0e12008-12-19 10:37:04 +08001875 struct iwl_powertable_cmd cmd;
Zhu Yib481de92007-09-25 17:54:57 -07001876
1877 /* If on battery, set to 3,
Ian Schram01ebd062007-10-25 17:15:22 +08001878 * if plugged into AC power, set to CAM ("continuously aware mode"),
Zhu Yib481de92007-09-25 17:54:57 -07001879 * else user level */
1880 switch (mode) {
Samuel Ortiz1125eff2008-12-19 10:37:14 +08001881 case IWL39_POWER_BATTERY:
Zhu Yib481de92007-09-25 17:54:57 -07001882 final_mode = IWL_POWER_INDEX_3;
1883 break;
Samuel Ortiz1125eff2008-12-19 10:37:14 +08001884 case IWL39_POWER_AC:
Zhu Yib481de92007-09-25 17:54:57 -07001885 final_mode = IWL_POWER_MODE_CAM;
1886 break;
1887 default:
1888 final_mode = mode;
1889 break;
1890 }
1891
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001892 iwl3945_update_power_cmd(priv, &cmd, final_mode);
Zhu Yib481de92007-09-25 17:54:57 -07001893
Tomas Winkler600c0e12008-12-19 10:37:04 +08001894 /* FIXME use get_hcmd_size 3945 command is 4 bytes shorter */
1895 rc = iwl3945_send_cmd_pdu(priv, POWER_TABLE_CMD,
1896 sizeof(struct iwl3945_powertable_cmd), &cmd);
Zhu Yib481de92007-09-25 17:54:57 -07001897
1898 if (final_mode == IWL_POWER_MODE_CAM)
1899 clear_bit(STATUS_POWER_PMI, &priv->status);
1900 else
1901 set_bit(STATUS_POWER_PMI, &priv->status);
1902
1903 return rc;
1904}
1905
Zhu Yib481de92007-09-25 17:54:57 -07001906/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001907 * iwl3945_scan_cancel - Cancel any currently executing HW scan
Zhu Yib481de92007-09-25 17:54:57 -07001908 *
1909 * NOTE: priv->mutex is not required before calling this function
1910 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001911static int iwl3945_scan_cancel(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001912{
1913 if (!test_bit(STATUS_SCAN_HW, &priv->status)) {
1914 clear_bit(STATUS_SCANNING, &priv->status);
1915 return 0;
1916 }
1917
1918 if (test_bit(STATUS_SCANNING, &priv->status)) {
1919 if (!test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
1920 IWL_DEBUG_SCAN("Queuing scan abort.\n");
1921 set_bit(STATUS_SCAN_ABORTING, &priv->status);
1922 queue_work(priv->workqueue, &priv->abort_scan);
1923
1924 } else
1925 IWL_DEBUG_SCAN("Scan abort already in progress.\n");
1926
1927 return test_bit(STATUS_SCANNING, &priv->status);
1928 }
1929
1930 return 0;
1931}
1932
1933/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001934 * iwl3945_scan_cancel_timeout - Cancel any currently executing HW scan
Zhu Yib481de92007-09-25 17:54:57 -07001935 * @ms: amount of time to wait (in milliseconds) for scan to abort
1936 *
1937 * NOTE: priv->mutex must be held before calling this function
1938 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001939static int iwl3945_scan_cancel_timeout(struct iwl_priv *priv, unsigned long ms)
Zhu Yib481de92007-09-25 17:54:57 -07001940{
1941 unsigned long now = jiffies;
1942 int ret;
1943
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001944 ret = iwl3945_scan_cancel(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001945 if (ret && ms) {
1946 mutex_unlock(&priv->mutex);
1947 while (!time_after(jiffies, now + msecs_to_jiffies(ms)) &&
1948 test_bit(STATUS_SCANNING, &priv->status))
1949 msleep(1);
1950 mutex_lock(&priv->mutex);
1951
1952 return test_bit(STATUS_SCANNING, &priv->status);
1953 }
1954
1955 return ret;
1956}
1957
Zhu Yib481de92007-09-25 17:54:57 -07001958#define MAX_UCODE_BEACON_INTERVAL 1024
1959#define INTEL_CONN_LISTEN_INTERVAL __constant_cpu_to_le16(0xA)
1960
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001961static __le16 iwl3945_adjust_beacon_interval(u16 beacon_val)
Zhu Yib481de92007-09-25 17:54:57 -07001962{
1963 u16 new_val = 0;
1964 u16 beacon_factor = 0;
1965
1966 beacon_factor =
1967 (beacon_val + MAX_UCODE_BEACON_INTERVAL)
1968 / MAX_UCODE_BEACON_INTERVAL;
1969 new_val = beacon_val / beacon_factor;
1970
1971 return cpu_to_le16(new_val);
1972}
1973
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001974static void iwl3945_setup_rxon_timing(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001975{
1976 u64 interval_tm_unit;
1977 u64 tsf, result;
1978 unsigned long flags;
1979 struct ieee80211_conf *conf = NULL;
1980 u16 beacon_int = 0;
1981
1982 conf = ieee80211_get_hw_conf(priv->hw);
1983
1984 spin_lock_irqsave(&priv->lock, flags);
Tomas Winkler28afaf92008-12-19 10:37:06 +08001985 priv->rxon_timing.timestamp = cpu_to_le64(priv->timestamp);
Zhu Yib481de92007-09-25 17:54:57 -07001986 priv->rxon_timing.listen_interval = INTEL_CONN_LISTEN_INTERVAL;
1987
Tomas Winkler28afaf92008-12-19 10:37:06 +08001988 tsf = priv->timestamp;
Zhu Yib481de92007-09-25 17:54:57 -07001989
1990 beacon_int = priv->beacon_int;
1991 spin_unlock_irqrestore(&priv->lock, flags);
1992
Johannes Berg05c914f2008-09-11 00:01:58 +02001993 if (priv->iw_mode == NL80211_IFTYPE_STATION) {
Zhu Yib481de92007-09-25 17:54:57 -07001994 if (beacon_int == 0) {
1995 priv->rxon_timing.beacon_interval = cpu_to_le16(100);
1996 priv->rxon_timing.beacon_init_val = cpu_to_le32(102400);
1997 } else {
1998 priv->rxon_timing.beacon_interval =
1999 cpu_to_le16(beacon_int);
2000 priv->rxon_timing.beacon_interval =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002001 iwl3945_adjust_beacon_interval(
Zhu Yib481de92007-09-25 17:54:57 -07002002 le16_to_cpu(priv->rxon_timing.beacon_interval));
2003 }
2004
2005 priv->rxon_timing.atim_window = 0;
2006 } else {
2007 priv->rxon_timing.beacon_interval =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002008 iwl3945_adjust_beacon_interval(conf->beacon_int);
Zhu Yib481de92007-09-25 17:54:57 -07002009 /* TODO: we need to get atim_window from upper stack
2010 * for now we set to 0 */
2011 priv->rxon_timing.atim_window = 0;
2012 }
2013
2014 interval_tm_unit =
2015 (le16_to_cpu(priv->rxon_timing.beacon_interval) * 1024);
2016 result = do_div(tsf, interval_tm_unit);
2017 priv->rxon_timing.beacon_init_val =
2018 cpu_to_le32((u32) ((u64) interval_tm_unit - result));
2019
2020 IWL_DEBUG_ASSOC
2021 ("beacon interval %d beacon timer %d beacon tim %d\n",
2022 le16_to_cpu(priv->rxon_timing.beacon_interval),
2023 le32_to_cpu(priv->rxon_timing.beacon_init_val),
2024 le16_to_cpu(priv->rxon_timing.atim_window));
2025}
2026
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002027static int iwl3945_scan_initiate(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002028{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002029 if (!iwl3945_is_ready_rf(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07002030 IWL_DEBUG_SCAN("Aborting scan due to not ready.\n");
2031 return -EIO;
2032 }
2033
2034 if (test_bit(STATUS_SCANNING, &priv->status)) {
2035 IWL_DEBUG_SCAN("Scan already in progress.\n");
2036 return -EAGAIN;
2037 }
2038
2039 if (test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
2040 IWL_DEBUG_SCAN("Scan request while abort pending. "
2041 "Queuing.\n");
2042 return -EAGAIN;
2043 }
2044
2045 IWL_DEBUG_INFO("Starting scan...\n");
Ron Rindjunsky66b50042008-06-25 16:46:31 +08002046 if (priv->cfg->sku & IWL_SKU_G)
2047 priv->scan_bands |= BIT(IEEE80211_BAND_2GHZ);
2048 if (priv->cfg->sku & IWL_SKU_A)
2049 priv->scan_bands |= BIT(IEEE80211_BAND_5GHZ);
Zhu Yib481de92007-09-25 17:54:57 -07002050 set_bit(STATUS_SCANNING, &priv->status);
2051 priv->scan_start = jiffies;
2052 priv->scan_pass_start = priv->scan_start;
2053
2054 queue_work(priv->workqueue, &priv->request_scan);
2055
2056 return 0;
2057}
2058
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002059static int iwl3945_set_rxon_hwcrypto(struct iwl_priv *priv, int hw_decrypt)
Zhu Yib481de92007-09-25 17:54:57 -07002060{
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08002061 struct iwl3945_rxon_cmd *rxon = &priv->staging39_rxon;
Zhu Yib481de92007-09-25 17:54:57 -07002062
2063 if (hw_decrypt)
2064 rxon->filter_flags &= ~RXON_FILTER_DIS_DECRYPT_MSK;
2065 else
2066 rxon->filter_flags |= RXON_FILTER_DIS_DECRYPT_MSK;
2067
2068 return 0;
2069}
2070
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002071static void iwl3945_set_flags_for_phymode(struct iwl_priv *priv,
Johannes Berg8318d782008-01-24 19:38:38 +01002072 enum ieee80211_band band)
Zhu Yib481de92007-09-25 17:54:57 -07002073{
Johannes Berg8318d782008-01-24 19:38:38 +01002074 if (band == IEEE80211_BAND_5GHZ) {
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08002075 priv->staging39_rxon.flags &=
Zhu Yib481de92007-09-25 17:54:57 -07002076 ~(RXON_FLG_BAND_24G_MSK | RXON_FLG_AUTO_DETECT_MSK
2077 | RXON_FLG_CCK_MSK);
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08002078 priv->staging39_rxon.flags |= RXON_FLG_SHORT_SLOT_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07002079 } else {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002080 /* Copied from iwl3945_bg_post_associate() */
Zhu Yib481de92007-09-25 17:54:57 -07002081 if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_SLOT_TIME)
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 else
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08002084 priv->staging39_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07002085
Johannes Berg05c914f2008-09-11 00:01:58 +02002086 if (priv->iw_mode == NL80211_IFTYPE_ADHOC)
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08002087 priv->staging39_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07002088
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08002089 priv->staging39_rxon.flags |= RXON_FLG_BAND_24G_MSK;
2090 priv->staging39_rxon.flags |= RXON_FLG_AUTO_DETECT_MSK;
2091 priv->staging39_rxon.flags &= ~RXON_FLG_CCK_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07002092 }
2093}
2094
2095/*
Ian Schram01ebd062007-10-25 17:15:22 +08002096 * initialize rxon structure with default values from eeprom
Zhu Yib481de92007-09-25 17:54:57 -07002097 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002098static void iwl3945_connection_init_rx_config(struct iwl_priv *priv,
Zhu, Yi60294de2008-10-29 14:05:45 -07002099 int mode)
Zhu Yib481de92007-09-25 17:54:57 -07002100{
Samuel Ortizd20b3c62008-12-19 10:37:15 +08002101 const struct iwl_channel_info *ch_info;
Zhu Yib481de92007-09-25 17:54:57 -07002102
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08002103 memset(&priv->staging39_rxon, 0, sizeof(priv->staging39_rxon));
Zhu Yib481de92007-09-25 17:54:57 -07002104
Zhu, Yi60294de2008-10-29 14:05:45 -07002105 switch (mode) {
Johannes Berg05c914f2008-09-11 00:01:58 +02002106 case NL80211_IFTYPE_AP:
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08002107 priv->staging39_rxon.dev_type = RXON_DEV_TYPE_AP;
Zhu Yib481de92007-09-25 17:54:57 -07002108 break;
2109
Johannes Berg05c914f2008-09-11 00:01:58 +02002110 case NL80211_IFTYPE_STATION:
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08002111 priv->staging39_rxon.dev_type = RXON_DEV_TYPE_ESS;
2112 priv->staging39_rxon.filter_flags = RXON_FILTER_ACCEPT_GRP_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07002113 break;
2114
Johannes Berg05c914f2008-09-11 00:01:58 +02002115 case NL80211_IFTYPE_ADHOC:
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08002116 priv->staging39_rxon.dev_type = RXON_DEV_TYPE_IBSS;
2117 priv->staging39_rxon.flags = RXON_FLG_SHORT_PREAMBLE_MSK;
2118 priv->staging39_rxon.filter_flags = RXON_FILTER_BCON_AWARE_MSK |
Zhu Yib481de92007-09-25 17:54:57 -07002119 RXON_FILTER_ACCEPT_GRP_MSK;
2120 break;
2121
Johannes Berg05c914f2008-09-11 00:01:58 +02002122 case NL80211_IFTYPE_MONITOR:
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08002123 priv->staging39_rxon.dev_type = RXON_DEV_TYPE_SNIFFER;
2124 priv->staging39_rxon.filter_flags = RXON_FILTER_PROMISC_MSK |
Zhu Yib481de92007-09-25 17:54:57 -07002125 RXON_FILTER_CTL2HOST_MSK | RXON_FILTER_ACCEPT_GRP_MSK;
2126 break;
Tomas Winkler69dc5d92008-03-25 16:33:41 -07002127 default:
Winkler, Tomas15b16872008-12-19 10:37:33 +08002128 IWL_ERR(priv, "Unsupported interface type %d\n", mode);
Tomas Winkler69dc5d92008-03-25 16:33:41 -07002129 break;
Zhu Yib481de92007-09-25 17:54:57 -07002130 }
2131
2132#if 0
2133 /* TODO: Figure out when short_preamble would be set and cache from
2134 * that */
2135 if (!hw_to_local(priv->hw)->short_preamble)
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 else
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08002138 priv->staging39_rxon.flags |= RXON_FLG_SHORT_PREAMBLE_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07002139#endif
2140
Johannes Berg8318d782008-01-24 19:38:38 +01002141 ch_info = iwl3945_get_channel_info(priv, priv->band,
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08002142 le16_to_cpu(priv->active39_rxon.channel));
Zhu Yib481de92007-09-25 17:54:57 -07002143
2144 if (!ch_info)
2145 ch_info = &priv->channel_info[0];
2146
2147 /*
2148 * in some case A channels are all non IBSS
2149 * in this case force B/G channel
2150 */
Zhu, Yi60294de2008-10-29 14:05:45 -07002151 if ((mode == NL80211_IFTYPE_ADHOC) && !(is_channel_ibss(ch_info)))
Zhu Yib481de92007-09-25 17:54:57 -07002152 ch_info = &priv->channel_info[0];
2153
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08002154 priv->staging39_rxon.channel = cpu_to_le16(ch_info->channel);
Zhu Yib481de92007-09-25 17:54:57 -07002155 if (is_channel_a_band(ch_info))
Johannes Berg8318d782008-01-24 19:38:38 +01002156 priv->band = IEEE80211_BAND_5GHZ;
Zhu Yib481de92007-09-25 17:54:57 -07002157 else
Johannes Berg8318d782008-01-24 19:38:38 +01002158 priv->band = IEEE80211_BAND_2GHZ;
Zhu Yib481de92007-09-25 17:54:57 -07002159
Johannes Berg8318d782008-01-24 19:38:38 +01002160 iwl3945_set_flags_for_phymode(priv, priv->band);
Zhu Yib481de92007-09-25 17:54:57 -07002161
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08002162 priv->staging39_rxon.ofdm_basic_rates =
Zhu Yib481de92007-09-25 17:54:57 -07002163 (IWL_OFDM_RATES_MASK >> IWL_FIRST_OFDM_RATE) & 0xFF;
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08002164 priv->staging39_rxon.cck_basic_rates =
Zhu Yib481de92007-09-25 17:54:57 -07002165 (IWL_CCK_RATES_MASK >> IWL_FIRST_CCK_RATE) & 0xF;
2166}
2167
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002168static int iwl3945_set_mode(struct iwl_priv *priv, int mode)
Zhu Yib481de92007-09-25 17:54:57 -07002169{
Johannes Berg05c914f2008-09-11 00:01:58 +02002170 if (mode == NL80211_IFTYPE_ADHOC) {
Samuel Ortizd20b3c62008-12-19 10:37:15 +08002171 const struct iwl_channel_info *ch_info;
Zhu Yib481de92007-09-25 17:54:57 -07002172
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002173 ch_info = iwl3945_get_channel_info(priv,
Johannes Berg8318d782008-01-24 19:38:38 +01002174 priv->band,
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08002175 le16_to_cpu(priv->staging39_rxon.channel));
Zhu Yib481de92007-09-25 17:54:57 -07002176
2177 if (!ch_info || !is_channel_ibss(ch_info)) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08002178 IWL_ERR(priv, "channel %d not IBSS channel\n",
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08002179 le16_to_cpu(priv->staging39_rxon.channel));
Zhu Yib481de92007-09-25 17:54:57 -07002180 return -EINVAL;
2181 }
2182 }
2183
Zhu, Yi60294de2008-10-29 14:05:45 -07002184 iwl3945_connection_init_rx_config(priv, mode);
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08002185 memcpy(priv->staging39_rxon.node_addr, priv->mac_addr, ETH_ALEN);
Zhu Yib481de92007-09-25 17:54:57 -07002186
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002187 iwl3945_clear_stations_table(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002188
Tomas Winklera96a27f2008-10-23 23:48:56 -07002189 /* don't commit rxon if rf-kill is on*/
Mohamed Abbasfde35712007-11-29 11:10:15 +08002190 if (!iwl3945_is_ready_rf(priv))
2191 return -EAGAIN;
2192
2193 cancel_delayed_work(&priv->scan_check);
2194 if (iwl3945_scan_cancel_timeout(priv, 100)) {
Winkler, Tomas39aadf82008-12-19 10:37:32 +08002195 IWL_WARN(priv, "Aborted scan still in progress after 100ms\n");
Mohamed Abbasfde35712007-11-29 11:10:15 +08002196 IWL_DEBUG_MAC80211("leaving - scan abort failed.\n");
2197 return -EAGAIN;
2198 }
2199
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002200 iwl3945_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002201
2202 return 0;
2203}
2204
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002205static void iwl3945_build_tx_cmd_hwcrypto(struct iwl_priv *priv,
Johannes Berge039fa42008-05-15 12:55:29 +02002206 struct ieee80211_tx_info *info,
Winkler, Tomasc2d79b482008-12-19 10:37:34 +08002207 struct iwl_cmd *cmd,
Zhu Yib481de92007-09-25 17:54:57 -07002208 struct sk_buff *skb_frag,
2209 int last_frag)
2210{
Ivo van Doorn1c014422008-04-17 19:41:02 +02002211 struct iwl3945_hw_key *keyinfo =
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08002212 &priv->stations_39[info->control.hw_key->hw_key_idx].keyinfo;
Zhu Yib481de92007-09-25 17:54:57 -07002213
2214 switch (keyinfo->alg) {
2215 case ALG_CCMP:
2216 cmd->cmd.tx.sec_ctl = TX_CMD_SEC_CCM;
2217 memcpy(cmd->cmd.tx.key, keyinfo->key, keyinfo->keylen);
Tomas Winklera96a27f2008-10-23 23:48:56 -07002218 IWL_DEBUG_TX("tx_cmd with AES hwcrypto\n");
Zhu Yib481de92007-09-25 17:54:57 -07002219 break;
2220
2221 case ALG_TKIP:
2222#if 0
2223 cmd->cmd.tx.sec_ctl = TX_CMD_SEC_TKIP;
2224
2225 if (last_frag)
2226 memcpy(cmd->cmd.tx.tkip_mic.byte, skb_frag->tail - 8,
2227 8);
2228 else
2229 memset(cmd->cmd.tx.tkip_mic.byte, 0, 8);
2230#endif
2231 break;
2232
2233 case ALG_WEP:
2234 cmd->cmd.tx.sec_ctl = TX_CMD_SEC_WEP |
Johannes Berge039fa42008-05-15 12:55:29 +02002235 (info->control.hw_key->hw_key_idx & TX_CMD_SEC_MSK) << TX_CMD_SEC_SHIFT;
Zhu Yib481de92007-09-25 17:54:57 -07002236
2237 if (keyinfo->keylen == 13)
2238 cmd->cmd.tx.sec_ctl |= TX_CMD_SEC_KEY128;
2239
2240 memcpy(&cmd->cmd.tx.key[3], keyinfo->key, keyinfo->keylen);
2241
2242 IWL_DEBUG_TX("Configuring packet for WEP encryption "
Johannes Berge039fa42008-05-15 12:55:29 +02002243 "with key %d\n", info->control.hw_key->hw_key_idx);
Zhu Yib481de92007-09-25 17:54:57 -07002244 break;
2245
Zhu Yib481de92007-09-25 17:54:57 -07002246 default:
Tomas Winkler978785a2008-12-19 10:37:31 +08002247 IWL_ERR(priv, "Unknown encode alg %d\n", keyinfo->alg);
Zhu Yib481de92007-09-25 17:54:57 -07002248 break;
2249 }
2250}
2251
2252/*
2253 * handle build REPLY_TX command notification.
2254 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002255static void iwl3945_build_tx_cmd_basic(struct iwl_priv *priv,
Winkler, Tomasc2d79b482008-12-19 10:37:34 +08002256 struct iwl_cmd *cmd,
Johannes Berge039fa42008-05-15 12:55:29 +02002257 struct ieee80211_tx_info *info,
Zhu Yib481de92007-09-25 17:54:57 -07002258 struct ieee80211_hdr *hdr,
2259 int is_unicast, u8 std_id)
2260{
Harvey Harrisonfd7c8a42008-06-11 14:21:56 -07002261 __le16 fc = hdr->frame_control;
Zhu Yib481de92007-09-25 17:54:57 -07002262 __le32 tx_flags = cmd->cmd.tx.tx_flags;
Johannes Berge6a98542008-10-21 12:40:02 +02002263 u8 rc_flags = info->control.rates[0].flags;
Zhu Yib481de92007-09-25 17:54:57 -07002264
2265 cmd->cmd.tx.stop_time.life_time = TX_CMD_LIFE_TIME_INFINITE;
Johannes Berge039fa42008-05-15 12:55:29 +02002266 if (!(info->flags & IEEE80211_TX_CTL_NO_ACK)) {
Zhu Yib481de92007-09-25 17:54:57 -07002267 tx_flags |= TX_CMD_FLG_ACK_MSK;
Harvey Harrisonfd7c8a42008-06-11 14:21:56 -07002268 if (ieee80211_is_mgmt(fc))
Zhu Yib481de92007-09-25 17:54:57 -07002269 tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
Harvey Harrisonfd7c8a42008-06-11 14:21:56 -07002270 if (ieee80211_is_probe_resp(fc) &&
Zhu Yib481de92007-09-25 17:54:57 -07002271 !(le16_to_cpu(hdr->seq_ctrl) & 0xf))
2272 tx_flags |= TX_CMD_FLG_TSF_MSK;
2273 } else {
2274 tx_flags &= (~TX_CMD_FLG_ACK_MSK);
2275 tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
2276 }
2277
2278 cmd->cmd.tx.sta_id = std_id;
Harvey Harrison8b7b1e02008-06-11 14:21:56 -07002279 if (ieee80211_has_morefrags(fc))
Zhu Yib481de92007-09-25 17:54:57 -07002280 tx_flags |= TX_CMD_FLG_MORE_FRAG_MSK;
2281
Harvey Harrisonfd7c8a42008-06-11 14:21:56 -07002282 if (ieee80211_is_data_qos(fc)) {
2283 u8 *qc = ieee80211_get_qos_ctl(hdr);
Tomas Winkler54dbb522008-05-15 13:54:06 +08002284 cmd->cmd.tx.tid_tspec = qc[0] & 0xf;
Zhu Yib481de92007-09-25 17:54:57 -07002285 tx_flags &= ~TX_CMD_FLG_SEQ_CTL_MSK;
Tomas Winkler54dbb522008-05-15 13:54:06 +08002286 } else {
Zhu Yib481de92007-09-25 17:54:57 -07002287 tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
Tomas Winkler54dbb522008-05-15 13:54:06 +08002288 }
Zhu Yib481de92007-09-25 17:54:57 -07002289
Johannes Berge6a98542008-10-21 12:40:02 +02002290 if (rc_flags & IEEE80211_TX_RC_USE_RTS_CTS) {
Zhu Yib481de92007-09-25 17:54:57 -07002291 tx_flags |= TX_CMD_FLG_RTS_MSK;
2292 tx_flags &= ~TX_CMD_FLG_CTS_MSK;
Johannes Berge6a98542008-10-21 12:40:02 +02002293 } else if (rc_flags & IEEE80211_TX_RC_USE_CTS_PROTECT) {
Zhu Yib481de92007-09-25 17:54:57 -07002294 tx_flags &= ~TX_CMD_FLG_RTS_MSK;
2295 tx_flags |= TX_CMD_FLG_CTS_MSK;
2296 }
2297
2298 if ((tx_flags & TX_CMD_FLG_RTS_MSK) || (tx_flags & TX_CMD_FLG_CTS_MSK))
2299 tx_flags |= TX_CMD_FLG_FULL_TXOP_PROT_MSK;
2300
2301 tx_flags &= ~(TX_CMD_FLG_ANT_SEL_MSK);
Harvey Harrisonfd7c8a42008-06-11 14:21:56 -07002302 if (ieee80211_is_mgmt(fc)) {
2303 if (ieee80211_is_assoc_req(fc) || ieee80211_is_reassoc_req(fc))
Ian Schrambc434dd2007-10-25 17:15:29 +08002304 cmd->cmd.tx.timeout.pm_frame_timeout = cpu_to_le16(3);
Zhu Yib481de92007-09-25 17:54:57 -07002305 else
Ian Schrambc434dd2007-10-25 17:15:29 +08002306 cmd->cmd.tx.timeout.pm_frame_timeout = cpu_to_le16(2);
Mohamed Abbasab53d8a2008-03-25 16:33:36 -07002307 } else {
Zhu Yib481de92007-09-25 17:54:57 -07002308 cmd->cmd.tx.timeout.pm_frame_timeout = 0;
Mohamed Abbasab53d8a2008-03-25 16:33:36 -07002309#ifdef CONFIG_IWL3945_LEDS
2310 priv->rxtxpackets += le16_to_cpu(cmd->cmd.tx.len);
2311#endif
2312 }
Zhu Yib481de92007-09-25 17:54:57 -07002313
2314 cmd->cmd.tx.driver_txop = 0;
2315 cmd->cmd.tx.tx_flags = tx_flags;
2316 cmd->cmd.tx.next_frame_len = 0;
2317}
2318
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002319/**
2320 * iwl3945_get_sta_id - Find station's index within station table
2321 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002322static int iwl3945_get_sta_id(struct iwl_priv *priv, struct ieee80211_hdr *hdr)
Zhu Yib481de92007-09-25 17:54:57 -07002323{
2324 int sta_id;
2325 u16 fc = le16_to_cpu(hdr->frame_control);
2326
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002327 /* If this frame is broadcast or management, use broadcast station id */
Zhu Yib481de92007-09-25 17:54:57 -07002328 if (((fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_DATA) ||
2329 is_multicast_ether_addr(hdr->addr1))
Abhijeet Kolekar3832ec92008-12-19 10:37:26 +08002330 return priv->hw_params.bcast_sta_id;
Zhu Yib481de92007-09-25 17:54:57 -07002331
2332 switch (priv->iw_mode) {
2333
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002334 /* If we are a client station in a BSS network, use the special
2335 * AP station entry (that's the only station we communicate with) */
Johannes Berg05c914f2008-09-11 00:01:58 +02002336 case NL80211_IFTYPE_STATION:
Zhu Yib481de92007-09-25 17:54:57 -07002337 return IWL_AP_ID;
2338
2339 /* If we are an AP, then find the station, or use BCAST */
Johannes Berg05c914f2008-09-11 00:01:58 +02002340 case NL80211_IFTYPE_AP:
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002341 sta_id = iwl3945_hw_find_station(priv, hdr->addr1);
Zhu Yib481de92007-09-25 17:54:57 -07002342 if (sta_id != IWL_INVALID_STATION)
2343 return sta_id;
Abhijeet Kolekar3832ec92008-12-19 10:37:26 +08002344 return priv->hw_params.bcast_sta_id;
Zhu Yib481de92007-09-25 17:54:57 -07002345
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002346 /* If this frame is going out to an IBSS network, find the station,
2347 * or create a new station table entry */
Johannes Berg05c914f2008-09-11 00:01:58 +02002348 case NL80211_IFTYPE_ADHOC: {
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002349 /* Create new station table entry */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002350 sta_id = iwl3945_hw_find_station(priv, hdr->addr1);
Zhu Yib481de92007-09-25 17:54:57 -07002351 if (sta_id != IWL_INVALID_STATION)
2352 return sta_id;
2353
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002354 sta_id = iwl3945_add_station(priv, hdr->addr1, 0, CMD_ASYNC);
Zhu Yib481de92007-09-25 17:54:57 -07002355
2356 if (sta_id != IWL_INVALID_STATION)
2357 return sta_id;
2358
Johannes Berge1749612008-10-27 15:59:26 -07002359 IWL_DEBUG_DROP("Station %pM not in station map. "
Zhu Yib481de92007-09-25 17:54:57 -07002360 "Defaulting to broadcast...\n",
Johannes Berge1749612008-10-27 15:59:26 -07002361 hdr->addr1);
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08002362 iwl_print_hex_dump(priv, IWL_DL_DROP, (u8 *) hdr, sizeof(*hdr));
Abhijeet Kolekar3832ec92008-12-19 10:37:26 +08002363 return priv->hw_params.bcast_sta_id;
Joe Perches0795af52007-10-03 17:59:30 -07002364 }
Stefanik Gábor914233d2008-06-30 17:23:30 +08002365 /* If we are in monitor mode, use BCAST. This is required for
2366 * packet injection. */
Johannes Berg05c914f2008-09-11 00:01:58 +02002367 case NL80211_IFTYPE_MONITOR:
Abhijeet Kolekar3832ec92008-12-19 10:37:26 +08002368 return priv->hw_params.bcast_sta_id;
Stefanik Gábor914233d2008-06-30 17:23:30 +08002369
Zhu Yib481de92007-09-25 17:54:57 -07002370 default:
Winkler, Tomas39aadf82008-12-19 10:37:32 +08002371 IWL_WARN(priv, "Unknown mode of operation: %d\n",
2372 priv->iw_mode);
Abhijeet Kolekar3832ec92008-12-19 10:37:26 +08002373 return priv->hw_params.bcast_sta_id;
Zhu Yib481de92007-09-25 17:54:57 -07002374 }
2375}
2376
2377/*
2378 * start REPLY_TX command process
2379 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002380static int iwl3945_tx_skb(struct iwl_priv *priv, struct sk_buff *skb)
Zhu Yib481de92007-09-25 17:54:57 -07002381{
2382 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
Johannes Berge039fa42008-05-15 12:55:29 +02002383 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002384 struct iwl3945_tfd_frame *tfd;
Zhu Yib481de92007-09-25 17:54:57 -07002385 u32 *control_flags;
Johannes Berge2530082008-05-17 00:57:14 +02002386 int txq_id = skb_get_queue_mapping(skb);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002387 struct iwl3945_tx_queue *txq = NULL;
Samuel Ortizd20b3c62008-12-19 10:37:15 +08002388 struct iwl_queue *q = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07002389 dma_addr_t phys_addr;
2390 dma_addr_t txcmd_phys;
Winkler, Tomasc2d79b482008-12-19 10:37:34 +08002391 struct iwl_cmd *out_cmd = NULL;
Tomas Winkler54dbb522008-05-15 13:54:06 +08002392 u16 len, idx, len_org, hdr_len;
2393 u8 id;
2394 u8 unicast;
Zhu Yib481de92007-09-25 17:54:57 -07002395 u8 sta_id;
Tomas Winkler54dbb522008-05-15 13:54:06 +08002396 u8 tid = 0;
Zhu Yib481de92007-09-25 17:54:57 -07002397 u16 seq_number = 0;
Harvey Harrisonfd7c8a42008-06-11 14:21:56 -07002398 __le16 fc;
Zhu Yib481de92007-09-25 17:54:57 -07002399 u8 wait_write_ptr = 0;
Tomas Winkler54dbb522008-05-15 13:54:06 +08002400 u8 *qc = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07002401 unsigned long flags;
2402 int rc;
2403
2404 spin_lock_irqsave(&priv->lock, flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002405 if (iwl3945_is_rfkill(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07002406 IWL_DEBUG_DROP("Dropping - RF KILL\n");
2407 goto drop_unlock;
2408 }
2409
Johannes Berge039fa42008-05-15 12:55:29 +02002410 if ((ieee80211_get_tx_rate(priv->hw, info)->hw_value & 0xFF) == IWL_INVALID_RATE) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08002411 IWL_ERR(priv, "ERROR: No TX rate available.\n");
Zhu Yib481de92007-09-25 17:54:57 -07002412 goto drop_unlock;
2413 }
2414
2415 unicast = !is_multicast_ether_addr(hdr->addr1);
2416 id = 0;
2417
Harvey Harrisonfd7c8a42008-06-11 14:21:56 -07002418 fc = hdr->frame_control;
Zhu Yib481de92007-09-25 17:54:57 -07002419
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08002420#ifdef CONFIG_IWL3945_DEBUG
Zhu Yib481de92007-09-25 17:54:57 -07002421 if (ieee80211_is_auth(fc))
2422 IWL_DEBUG_TX("Sending AUTH frame\n");
Harvey Harrisonfd7c8a42008-06-11 14:21:56 -07002423 else if (ieee80211_is_assoc_req(fc))
Zhu Yib481de92007-09-25 17:54:57 -07002424 IWL_DEBUG_TX("Sending ASSOC frame\n");
Harvey Harrisonfd7c8a42008-06-11 14:21:56 -07002425 else if (ieee80211_is_reassoc_req(fc))
Zhu Yib481de92007-09-25 17:54:57 -07002426 IWL_DEBUG_TX("Sending REASSOC frame\n");
2427#endif
2428
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08002429 /* drop all data frame if we are not associated */
Stefanik Gábor914233d2008-06-30 17:23:30 +08002430 if (ieee80211_is_data(fc) &&
Johannes Berg05c914f2008-09-11 00:01:58 +02002431 (priv->iw_mode != NL80211_IFTYPE_MONITOR) && /* packet injection */
Stefanik Gábor914233d2008-06-30 17:23:30 +08002432 (!iwl3945_is_associated(priv) ||
Johannes Berg05c914f2008-09-11 00:01:58 +02002433 ((priv->iw_mode == NL80211_IFTYPE_STATION) && !priv->assoc_id))) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002434 IWL_DEBUG_DROP("Dropping - !iwl3945_is_associated\n");
Zhu Yib481de92007-09-25 17:54:57 -07002435 goto drop_unlock;
2436 }
2437
2438 spin_unlock_irqrestore(&priv->lock, flags);
2439
Harvey Harrison7294ec92008-07-15 18:43:59 -07002440 hdr_len = ieee80211_hdrlen(fc);
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002441
2442 /* Find (or create) index into station table for destination station */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002443 sta_id = iwl3945_get_sta_id(priv, hdr);
Zhu Yib481de92007-09-25 17:54:57 -07002444 if (sta_id == IWL_INVALID_STATION) {
Johannes Berge1749612008-10-27 15:59:26 -07002445 IWL_DEBUG_DROP("Dropping - INVALID STATION: %pM\n",
2446 hdr->addr1);
Zhu Yib481de92007-09-25 17:54:57 -07002447 goto drop;
2448 }
2449
2450 IWL_DEBUG_RATE("station Id %d\n", sta_id);
2451
Harvey Harrisonfd7c8a42008-06-11 14:21:56 -07002452 if (ieee80211_is_data_qos(fc)) {
2453 qc = ieee80211_get_qos_ctl(hdr);
Harvey Harrison7294ec92008-07-15 18:43:59 -07002454 tid = qc[0] & IEEE80211_QOS_CTL_TID_MASK;
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08002455 seq_number = priv->stations_39[sta_id].tid[tid].seq_number &
Zhu Yib481de92007-09-25 17:54:57 -07002456 IEEE80211_SCTL_SEQ;
2457 hdr->seq_ctrl = cpu_to_le16(seq_number) |
2458 (hdr->seq_ctrl &
2459 __constant_cpu_to_le16(IEEE80211_SCTL_FRAG));
2460 seq_number += 0x10;
2461 }
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002462
2463 /* Descriptor for chosen Tx queue */
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08002464 txq = &priv->txq39[txq_id];
Zhu Yib481de92007-09-25 17:54:57 -07002465 q = &txq->q;
2466
2467 spin_lock_irqsave(&priv->lock, flags);
2468
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002469 /* Set up first empty TFD within this queue's circular TFD buffer */
Tomas Winklerfc4b6852007-10-25 17:15:24 +08002470 tfd = &txq->bd[q->write_ptr];
Zhu Yib481de92007-09-25 17:54:57 -07002471 memset(tfd, 0, sizeof(*tfd));
2472 control_flags = (u32 *) tfd;
Tomas Winklerfc4b6852007-10-25 17:15:24 +08002473 idx = get_cmd_index(q, q->write_ptr, 0);
Zhu Yib481de92007-09-25 17:54:57 -07002474
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002475 /* Set up driver data for this TFD */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002476 memset(&(txq->txb[q->write_ptr]), 0, sizeof(struct iwl3945_tx_info));
Tomas Winklerfc4b6852007-10-25 17:15:24 +08002477 txq->txb[q->write_ptr].skb[0] = skb;
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002478
2479 /* Init first empty entry in queue's array of Tx/cmd buffers */
Zhu Yib481de92007-09-25 17:54:57 -07002480 out_cmd = &txq->cmd[idx];
2481 memset(&out_cmd->hdr, 0, sizeof(out_cmd->hdr));
2482 memset(&out_cmd->cmd.tx, 0, sizeof(out_cmd->cmd.tx));
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002483
2484 /*
2485 * Set up the Tx-command (not MAC!) header.
2486 * Store the chosen Tx queue and TFD index within the sequence field;
2487 * after Tx, uCode's Tx response will return this value so driver can
2488 * locate the frame within the tx queue and do post-tx processing.
2489 */
Zhu Yib481de92007-09-25 17:54:57 -07002490 out_cmd->hdr.cmd = REPLY_TX;
2491 out_cmd->hdr.sequence = cpu_to_le16((u16)(QUEUE_TO_SEQ(txq_id) |
Tomas Winklerfc4b6852007-10-25 17:15:24 +08002492 INDEX_TO_SEQ(q->write_ptr)));
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002493
2494 /* Copy MAC header from skb into command buffer */
Zhu Yib481de92007-09-25 17:54:57 -07002495 memcpy(out_cmd->cmd.tx.hdr, hdr, hdr_len);
2496
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002497 /*
2498 * Use the first empty entry in this queue's command buffer array
2499 * to contain the Tx command and MAC header concatenated together
2500 * (payload data will be in another buffer).
2501 * Size of this varies, due to varying MAC header length.
2502 * If end is not dword aligned, we'll have 2 extra bytes at the end
2503 * of the MAC header (device reads on dword boundaries).
2504 * We'll tell device about this padding later.
2505 */
Abhijeet Kolekar3832ec92008-12-19 10:37:26 +08002506 len = sizeof(struct iwl3945_tx_cmd) +
Tomas Winkler4c897252008-12-19 10:37:05 +08002507 sizeof(struct iwl_cmd_header) + hdr_len;
Zhu Yib481de92007-09-25 17:54:57 -07002508
2509 len_org = len;
2510 len = (len + 3) & ~3;
2511
2512 if (len_org != len)
2513 len_org = 1;
2514 else
2515 len_org = 0;
2516
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002517 /* Physical address of this Tx command's header (not MAC header!),
2518 * within command buffer array. */
Winkler, Tomasc2d79b482008-12-19 10:37:34 +08002519 txcmd_phys = txq->dma_addr_cmd + sizeof(struct iwl_cmd) * idx +
2520 offsetof(struct iwl_cmd, hdr);
Zhu Yib481de92007-09-25 17:54:57 -07002521
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002522 /* Add buffer containing Tx command and MAC(!) header to TFD's
2523 * first entry */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002524 iwl3945_hw_txq_attach_buf_to_tfd(priv, tfd, txcmd_phys, len);
Zhu Yib481de92007-09-25 17:54:57 -07002525
Johannes Bergd0f09802008-07-29 11:32:07 +02002526 if (info->control.hw_key)
Johannes Berge039fa42008-05-15 12:55:29 +02002527 iwl3945_build_tx_cmd_hwcrypto(priv, info, out_cmd, skb, 0);
Zhu Yib481de92007-09-25 17:54:57 -07002528
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002529 /* Set up TFD's 2nd entry to point directly to remainder of skb,
2530 * if any (802.11 null frames have no payload). */
Zhu Yib481de92007-09-25 17:54:57 -07002531 len = skb->len - hdr_len;
2532 if (len) {
2533 phys_addr = pci_map_single(priv->pci_dev, skb->data + hdr_len,
2534 len, PCI_DMA_TODEVICE);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002535 iwl3945_hw_txq_attach_buf_to_tfd(priv, tfd, phys_addr, len);
Zhu Yib481de92007-09-25 17:54:57 -07002536 }
2537
Zhu Yib481de92007-09-25 17:54:57 -07002538 if (!len)
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002539 /* If there is no payload, then we use only one Tx buffer */
Zhu Yib481de92007-09-25 17:54:57 -07002540 *control_flags = TFD_CTL_COUNT_SET(1);
2541 else
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002542 /* Else use 2 buffers.
2543 * Tell 3945 about any padding after MAC header */
Zhu Yib481de92007-09-25 17:54:57 -07002544 *control_flags = TFD_CTL_COUNT_SET(2) |
2545 TFD_CTL_PAD_SET(U32_PAD(len));
2546
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002547 /* Total # bytes to be transmitted */
Zhu Yib481de92007-09-25 17:54:57 -07002548 len = (u16)skb->len;
2549 out_cmd->cmd.tx.len = cpu_to_le16(len);
2550
2551 /* TODO need this for burst mode later on */
Johannes Berge039fa42008-05-15 12:55:29 +02002552 iwl3945_build_tx_cmd_basic(priv, out_cmd, info, hdr, unicast, sta_id);
Zhu Yib481de92007-09-25 17:54:57 -07002553
2554 /* set is_hcca to 0; it probably will never be implemented */
Johannes Berge039fa42008-05-15 12:55:29 +02002555 iwl3945_hw_build_tx_cmd_rate(priv, out_cmd, info, hdr, sta_id, 0);
Zhu Yib481de92007-09-25 17:54:57 -07002556
2557 out_cmd->cmd.tx.tx_flags &= ~TX_CMD_FLG_ANT_A_MSK;
2558 out_cmd->cmd.tx.tx_flags &= ~TX_CMD_FLG_ANT_B_MSK;
2559
Harvey Harrison8b7b1e02008-06-11 14:21:56 -07002560 if (!ieee80211_has_morefrags(hdr->frame_control)) {
Zhu Yib481de92007-09-25 17:54:57 -07002561 txq->need_update = 1;
Tomas Winkler3ac7f142008-07-21 02:40:14 +03002562 if (qc)
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08002563 priv->stations_39[sta_id].tid[tid].seq_number = seq_number;
Zhu Yib481de92007-09-25 17:54:57 -07002564 } else {
2565 wait_write_ptr = 1;
2566 txq->need_update = 0;
2567 }
2568
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08002569 iwl_print_hex_dump(priv, IWL_DL_TX, out_cmd->cmd.payload,
Zhu Yib481de92007-09-25 17:54:57 -07002570 sizeof(out_cmd->cmd.tx));
2571
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08002572 iwl_print_hex_dump(priv, IWL_DL_TX, (u8 *)out_cmd->cmd.tx.hdr,
Harvey Harrison7294ec92008-07-15 18:43:59 -07002573 ieee80211_hdrlen(fc));
Zhu Yib481de92007-09-25 17:54:57 -07002574
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002575 /* Tell device the write index *just past* this latest filled TFD */
Tomas Winklerc54b6792008-03-06 17:36:53 -08002576 q->write_ptr = iwl_queue_inc_wrap(q->write_ptr, q->n_bd);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002577 rc = iwl3945_tx_queue_update_write_ptr(priv, txq);
Zhu Yib481de92007-09-25 17:54:57 -07002578 spin_unlock_irqrestore(&priv->lock, flags);
2579
2580 if (rc)
2581 return rc;
2582
Samuel Ortizd20b3c62008-12-19 10:37:15 +08002583 if ((iwl_queue_space(q) < q->high_mark)
Zhu Yib481de92007-09-25 17:54:57 -07002584 && priv->mac80211_registered) {
2585 if (wait_write_ptr) {
2586 spin_lock_irqsave(&priv->lock, flags);
2587 txq->need_update = 1;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002588 iwl3945_tx_queue_update_write_ptr(priv, txq);
Zhu Yib481de92007-09-25 17:54:57 -07002589 spin_unlock_irqrestore(&priv->lock, flags);
2590 }
2591
Johannes Berge2530082008-05-17 00:57:14 +02002592 ieee80211_stop_queue(priv->hw, skb_get_queue_mapping(skb));
Zhu Yib481de92007-09-25 17:54:57 -07002593 }
2594
2595 return 0;
2596
2597drop_unlock:
2598 spin_unlock_irqrestore(&priv->lock, flags);
2599drop:
2600 return -1;
2601}
2602
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002603static void iwl3945_set_rate(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002604{
Johannes Berg8318d782008-01-24 19:38:38 +01002605 const struct ieee80211_supported_band *sband = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07002606 struct ieee80211_rate *rate;
2607 int i;
2608
Johannes Berg8318d782008-01-24 19:38:38 +01002609 sband = iwl3945_get_band(priv, priv->band);
2610 if (!sband) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08002611 IWL_ERR(priv, "Failed to set rate: unable to get hw mode\n");
Saleem Abdulrasoolc4ba9622007-11-18 23:59:08 -08002612 return;
2613 }
Zhu Yib481de92007-09-25 17:54:57 -07002614
2615 priv->active_rate = 0;
2616 priv->active_rate_basic = 0;
2617
Johannes Berg8318d782008-01-24 19:38:38 +01002618 IWL_DEBUG_RATE("Setting rates for %s GHz\n",
2619 sband->band == IEEE80211_BAND_2GHZ ? "2.4" : "5");
Zhu Yib481de92007-09-25 17:54:57 -07002620
Johannes Berg8318d782008-01-24 19:38:38 +01002621 for (i = 0; i < sband->n_bitrates; i++) {
2622 rate = &sband->bitrates[i];
2623 if ((rate->hw_value < IWL_RATE_COUNT) &&
2624 !(rate->flags & IEEE80211_CHAN_DISABLED)) {
2625 IWL_DEBUG_RATE("Adding rate index %d (plcp %d)\n",
2626 rate->hw_value, iwl3945_rates[rate->hw_value].plcp);
2627 priv->active_rate |= (1 << rate->hw_value);
2628 }
Zhu Yib481de92007-09-25 17:54:57 -07002629 }
2630
2631 IWL_DEBUG_RATE("Set active_rate = %0x, active_rate_basic = %0x\n",
2632 priv->active_rate, priv->active_rate_basic);
2633
2634 /*
2635 * If a basic rate is configured, then use it (adding IWL_RATE_1M_MASK)
2636 * otherwise set it to the default of all CCK rates and 6, 12, 24 for
2637 * OFDM
2638 */
2639 if (priv->active_rate_basic & IWL_CCK_BASIC_RATES_MASK)
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08002640 priv->staging39_rxon.cck_basic_rates =
Zhu Yib481de92007-09-25 17:54:57 -07002641 ((priv->active_rate_basic &
2642 IWL_CCK_RATES_MASK) >> IWL_FIRST_CCK_RATE) & 0xF;
2643 else
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08002644 priv->staging39_rxon.cck_basic_rates =
Zhu Yib481de92007-09-25 17:54:57 -07002645 (IWL_CCK_BASIC_RATES_MASK >> IWL_FIRST_CCK_RATE) & 0xF;
2646
2647 if (priv->active_rate_basic & IWL_OFDM_BASIC_RATES_MASK)
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08002648 priv->staging39_rxon.ofdm_basic_rates =
Zhu Yib481de92007-09-25 17:54:57 -07002649 ((priv->active_rate_basic &
2650 (IWL_OFDM_BASIC_RATES_MASK | IWL_RATE_6M_MASK)) >>
2651 IWL_FIRST_OFDM_RATE) & 0xFF;
2652 else
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08002653 priv->staging39_rxon.ofdm_basic_rates =
Zhu Yib481de92007-09-25 17:54:57 -07002654 (IWL_OFDM_BASIC_RATES_MASK >> IWL_FIRST_OFDM_RATE) & 0xFF;
2655}
2656
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002657static void iwl3945_radio_kill_sw(struct iwl_priv *priv, int disable_radio)
Zhu Yib481de92007-09-25 17:54:57 -07002658{
2659 unsigned long flags;
2660
2661 if (!!disable_radio == test_bit(STATUS_RF_KILL_SW, &priv->status))
2662 return;
2663
2664 IWL_DEBUG_RF_KILL("Manual SW RF KILL set to: RADIO %s\n",
2665 disable_radio ? "OFF" : "ON");
2666
2667 if (disable_radio) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002668 iwl3945_scan_cancel(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002669 /* FIXME: This is a workaround for AP */
Johannes Berg05c914f2008-09-11 00:01:58 +02002670 if (priv->iw_mode != NL80211_IFTYPE_AP) {
Zhu Yib481de92007-09-25 17:54:57 -07002671 spin_lock_irqsave(&priv->lock, flags);
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08002672 iwl_write32(priv, CSR_UCODE_DRV_GP1_SET,
Zhu Yib481de92007-09-25 17:54:57 -07002673 CSR_UCODE_SW_BIT_RFKILL);
2674 spin_unlock_irqrestore(&priv->lock, flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002675 iwl3945_send_card_state(priv, CARD_STATE_CMD_DISABLE, 0);
Zhu Yib481de92007-09-25 17:54:57 -07002676 set_bit(STATUS_RF_KILL_SW, &priv->status);
2677 }
2678 return;
2679 }
2680
2681 spin_lock_irqsave(&priv->lock, flags);
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08002682 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
Zhu Yib481de92007-09-25 17:54:57 -07002683
2684 clear_bit(STATUS_RF_KILL_SW, &priv->status);
2685 spin_unlock_irqrestore(&priv->lock, flags);
2686
2687 /* wake up ucode */
2688 msleep(10);
2689
2690 spin_lock_irqsave(&priv->lock, flags);
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08002691 iwl_read32(priv, CSR_UCODE_DRV_GP1);
2692 if (!iwl_grab_nic_access(priv))
2693 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002694 spin_unlock_irqrestore(&priv->lock, flags);
2695
2696 if (test_bit(STATUS_RF_KILL_HW, &priv->status)) {
2697 IWL_DEBUG_RF_KILL("Can not turn radio back on - "
2698 "disabled by HW switch\n");
2699 return;
2700 }
2701
Zhu Yi808e72a2008-06-12 09:47:17 +08002702 if (priv->is_open)
2703 queue_work(priv->workqueue, &priv->restart);
Zhu Yib481de92007-09-25 17:54:57 -07002704 return;
2705}
2706
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002707void iwl3945_set_decrypted_flag(struct iwl_priv *priv, struct sk_buff *skb,
Zhu Yib481de92007-09-25 17:54:57 -07002708 u32 decrypt_res, struct ieee80211_rx_status *stats)
2709{
2710 u16 fc =
2711 le16_to_cpu(((struct ieee80211_hdr *)skb->data)->frame_control);
2712
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08002713 if (priv->active39_rxon.filter_flags & RXON_FILTER_DIS_DECRYPT_MSK)
Zhu Yib481de92007-09-25 17:54:57 -07002714 return;
2715
2716 if (!(fc & IEEE80211_FCTL_PROTECTED))
2717 return;
2718
2719 IWL_DEBUG_RX("decrypt_res:0x%x\n", decrypt_res);
2720 switch (decrypt_res & RX_RES_STATUS_SEC_TYPE_MSK) {
2721 case RX_RES_STATUS_SEC_TYPE_TKIP:
2722 if ((decrypt_res & RX_RES_STATUS_DECRYPT_TYPE_MSK) ==
2723 RX_RES_STATUS_BAD_ICV_MIC)
2724 stats->flag |= RX_FLAG_MMIC_ERROR;
2725 case RX_RES_STATUS_SEC_TYPE_WEP:
2726 case RX_RES_STATUS_SEC_TYPE_CCMP:
2727 if ((decrypt_res & RX_RES_STATUS_DECRYPT_TYPE_MSK) ==
2728 RX_RES_STATUS_DECRYPT_OK) {
2729 IWL_DEBUG_RX("hw decrypt successfully!!!\n");
2730 stats->flag |= RX_FLAG_DECRYPTED;
2731 }
2732 break;
2733
2734 default:
2735 break;
2736 }
2737}
2738
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08002739#ifdef CONFIG_IWL3945_SPECTRUM_MEASUREMENT
Zhu Yib481de92007-09-25 17:54:57 -07002740
2741#include "iwl-spectrum.h"
2742
2743#define BEACON_TIME_MASK_LOW 0x00FFFFFF
2744#define BEACON_TIME_MASK_HIGH 0xFF000000
2745#define TIME_UNIT 1024
2746
2747/*
2748 * extended beacon time format
2749 * time in usec will be changed into a 32-bit value in 8:24 format
2750 * the high 1 byte is the beacon counts
2751 * the lower 3 bytes is the time in usec within one beacon interval
2752 */
2753
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002754static u32 iwl3945_usecs_to_beacons(u32 usec, u32 beacon_interval)
Zhu Yib481de92007-09-25 17:54:57 -07002755{
2756 u32 quot;
2757 u32 rem;
2758 u32 interval = beacon_interval * 1024;
2759
2760 if (!interval || !usec)
2761 return 0;
2762
2763 quot = (usec / interval) & (BEACON_TIME_MASK_HIGH >> 24);
2764 rem = (usec % interval) & BEACON_TIME_MASK_LOW;
2765
2766 return (quot << 24) + rem;
2767}
2768
2769/* base is usually what we get from ucode with each received frame,
2770 * the same as HW timer counter counting down
2771 */
2772
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002773static __le32 iwl3945_add_beacon_time(u32 base, u32 addon, u32 beacon_interval)
Zhu Yib481de92007-09-25 17:54:57 -07002774{
2775 u32 base_low = base & BEACON_TIME_MASK_LOW;
2776 u32 addon_low = addon & BEACON_TIME_MASK_LOW;
2777 u32 interval = beacon_interval * TIME_UNIT;
2778 u32 res = (base & BEACON_TIME_MASK_HIGH) +
2779 (addon & BEACON_TIME_MASK_HIGH);
2780
2781 if (base_low > addon_low)
2782 res += base_low - addon_low;
2783 else if (base_low < addon_low) {
2784 res += interval + base_low - addon_low;
2785 res += (1 << 24);
2786 } else
2787 res += (1 << 24);
2788
2789 return cpu_to_le32(res);
2790}
2791
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002792static int iwl3945_get_measurement(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07002793 struct ieee80211_measurement_params *params,
2794 u8 type)
2795{
Tomas Winkler600c0e12008-12-19 10:37:04 +08002796 struct iwl_spectrum_cmd spectrum;
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08002797 struct iwl_rx_packet *res;
Winkler, Tomasc2d79b482008-12-19 10:37:34 +08002798 struct iwl_host_cmd cmd = {
Zhu Yib481de92007-09-25 17:54:57 -07002799 .id = REPLY_SPECTRUM_MEASUREMENT_CMD,
2800 .data = (void *)&spectrum,
2801 .meta.flags = CMD_WANT_SKB,
2802 };
2803 u32 add_time = le64_to_cpu(params->start_time);
2804 int rc;
2805 int spectrum_resp_status;
2806 int duration = le16_to_cpu(params->duration);
2807
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002808 if (iwl3945_is_associated(priv))
Zhu Yib481de92007-09-25 17:54:57 -07002809 add_time =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002810 iwl3945_usecs_to_beacons(
Zhu Yib481de92007-09-25 17:54:57 -07002811 le64_to_cpu(params->start_time) - priv->last_tsf,
2812 le16_to_cpu(priv->rxon_timing.beacon_interval));
2813
2814 memset(&spectrum, 0, sizeof(spectrum));
2815
2816 spectrum.channel_count = cpu_to_le16(1);
2817 spectrum.flags =
2818 RXON_FLG_TSF2HOST_MSK | RXON_FLG_ANT_A_MSK | RXON_FLG_DIS_DIV_MSK;
2819 spectrum.filter_flags = MEASUREMENT_FILTER_FLAG;
2820 cmd.len = sizeof(spectrum);
2821 spectrum.len = cpu_to_le16(cmd.len - sizeof(spectrum.len));
2822
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002823 if (iwl3945_is_associated(priv))
Zhu Yib481de92007-09-25 17:54:57 -07002824 spectrum.start_time =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002825 iwl3945_add_beacon_time(priv->last_beacon_time,
Zhu Yib481de92007-09-25 17:54:57 -07002826 add_time,
2827 le16_to_cpu(priv->rxon_timing.beacon_interval));
2828 else
2829 spectrum.start_time = 0;
2830
2831 spectrum.channels[0].duration = cpu_to_le32(duration * TIME_UNIT);
2832 spectrum.channels[0].channel = params->channel;
2833 spectrum.channels[0].type = type;
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08002834 if (priv->active39_rxon.flags & RXON_FLG_BAND_24G_MSK)
Zhu Yib481de92007-09-25 17:54:57 -07002835 spectrum.flags |= RXON_FLG_BAND_24G_MSK |
2836 RXON_FLG_AUTO_DETECT_MSK | RXON_FLG_TGG_PROTECT_MSK;
2837
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002838 rc = iwl3945_send_cmd_sync(priv, &cmd);
Zhu Yib481de92007-09-25 17:54:57 -07002839 if (rc)
2840 return rc;
2841
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08002842 res = (struct iwl_rx_packet *)cmd.meta.u.skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07002843 if (res->hdr.flags & IWL_CMD_FAILED_MSK) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08002844 IWL_ERR(priv, "Bad return from REPLY_RX_ON_ASSOC command\n");
Zhu Yib481de92007-09-25 17:54:57 -07002845 rc = -EIO;
2846 }
2847
2848 spectrum_resp_status = le16_to_cpu(res->u.spectrum.status);
2849 switch (spectrum_resp_status) {
2850 case 0: /* Command will be handled */
2851 if (res->u.spectrum.id != 0xff) {
Ian Schrambc434dd2007-10-25 17:15:29 +08002852 IWL_DEBUG_INFO("Replaced existing measurement: %d\n",
2853 res->u.spectrum.id);
Zhu Yib481de92007-09-25 17:54:57 -07002854 priv->measurement_status &= ~MEASUREMENT_READY;
2855 }
2856 priv->measurement_status |= MEASUREMENT_ACTIVE;
2857 rc = 0;
2858 break;
2859
2860 case 1: /* Command will not be handled */
2861 rc = -EAGAIN;
2862 break;
2863 }
2864
2865 dev_kfree_skb_any(cmd.meta.u.skb);
2866
2867 return rc;
2868}
2869#endif
2870
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002871static void iwl3945_rx_reply_alive(struct iwl_priv *priv,
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08002872 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07002873{
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08002874 struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
2875 struct iwl_alive_resp *palive;
Zhu Yib481de92007-09-25 17:54:57 -07002876 struct delayed_work *pwork;
2877
2878 palive = &pkt->u.alive_frame;
2879
2880 IWL_DEBUG_INFO("Alive ucode status 0x%08X revision "
2881 "0x%01X 0x%01X\n",
2882 palive->is_valid, palive->ver_type,
2883 palive->ver_subtype);
2884
2885 if (palive->ver_subtype == INITIALIZE_SUBTYPE) {
2886 IWL_DEBUG_INFO("Initialization Alive received.\n");
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08002887 memcpy(&priv->card_alive_init, &pkt->u.alive_frame,
2888 sizeof(struct iwl_alive_resp));
Zhu Yib481de92007-09-25 17:54:57 -07002889 pwork = &priv->init_alive_start;
2890 } else {
2891 IWL_DEBUG_INFO("Runtime Alive received.\n");
2892 memcpy(&priv->card_alive, &pkt->u.alive_frame,
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08002893 sizeof(struct iwl_alive_resp));
Zhu Yib481de92007-09-25 17:54:57 -07002894 pwork = &priv->alive_start;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002895 iwl3945_disable_events(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002896 }
2897
2898 /* We delay the ALIVE response by 5ms to
2899 * give the HW RF Kill time to activate... */
2900 if (palive->is_valid == UCODE_VALID_OK)
2901 queue_delayed_work(priv->workqueue, pwork,
2902 msecs_to_jiffies(5));
2903 else
Winkler, Tomas39aadf82008-12-19 10:37:32 +08002904 IWL_WARN(priv, "uCode did not respond OK.\n");
Zhu Yib481de92007-09-25 17:54:57 -07002905}
2906
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002907static void iwl3945_rx_reply_add_sta(struct iwl_priv *priv,
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08002908 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07002909{
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08002910 struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07002911
2912 IWL_DEBUG_RX("Received REPLY_ADD_STA: 0x%02X\n", pkt->u.status);
2913 return;
2914}
2915
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002916static void iwl3945_rx_reply_error(struct iwl_priv *priv,
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08002917 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07002918{
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08002919 struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07002920
Winkler, Tomas15b16872008-12-19 10:37:33 +08002921 IWL_ERR(priv, "Error Reply type 0x%08X cmd %s (0x%02X) "
Zhu Yib481de92007-09-25 17:54:57 -07002922 "seq 0x%04X ser 0x%08X\n",
2923 le32_to_cpu(pkt->u.err_resp.error_type),
2924 get_cmd_string(pkt->u.err_resp.cmd_id),
2925 pkt->u.err_resp.cmd_id,
2926 le16_to_cpu(pkt->u.err_resp.bad_cmd_seq_num),
2927 le32_to_cpu(pkt->u.err_resp.error_info));
2928}
2929
2930#define TX_STATUS_ENTRY(x) case TX_STATUS_FAIL_ ## x: return #x
2931
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002932static void iwl3945_rx_csa(struct iwl_priv *priv, struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07002933{
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08002934 struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08002935 struct iwl3945_rxon_cmd *rxon = (void *)&priv->active39_rxon;
Tomas Winkler600c0e12008-12-19 10:37:04 +08002936 struct iwl_csa_notification *csa = &(pkt->u.csa_notif);
Zhu Yib481de92007-09-25 17:54:57 -07002937 IWL_DEBUG_11H("CSA notif: channel %d, status %d\n",
2938 le16_to_cpu(csa->channel), le32_to_cpu(csa->status));
2939 rxon->channel = csa->channel;
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08002940 priv->staging39_rxon.channel = csa->channel;
Zhu Yib481de92007-09-25 17:54:57 -07002941}
2942
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002943static void iwl3945_rx_spectrum_measure_notif(struct iwl_priv *priv,
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08002944 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07002945{
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08002946#ifdef CONFIG_IWL3945_SPECTRUM_MEASUREMENT
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08002947 struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
Tomas Winkler600c0e12008-12-19 10:37:04 +08002948 struct iwl_spectrum_notification *report = &(pkt->u.spectrum_notif);
Zhu Yib481de92007-09-25 17:54:57 -07002949
2950 if (!report->state) {
2951 IWL_DEBUG(IWL_DL_11H | IWL_DL_INFO,
2952 "Spectrum Measure Notification: Start\n");
2953 return;
2954 }
2955
2956 memcpy(&priv->measure_report, report, sizeof(*report));
2957 priv->measurement_status |= MEASUREMENT_READY;
2958#endif
2959}
2960
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002961static void iwl3945_rx_pm_sleep_notif(struct iwl_priv *priv,
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08002962 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07002963{
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08002964#ifdef CONFIG_IWL3945_DEBUG
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08002965 struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
Tomas Winkler600c0e12008-12-19 10:37:04 +08002966 struct iwl_sleep_notification *sleep = &(pkt->u.sleep_notif);
Zhu Yib481de92007-09-25 17:54:57 -07002967 IWL_DEBUG_RX("sleep mode: %d, src: %d\n",
2968 sleep->pm_sleep_mode, sleep->pm_wakeup_src);
2969#endif
2970}
2971
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002972static void iwl3945_rx_pm_debug_statistics_notif(struct iwl_priv *priv,
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08002973 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07002974{
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08002975 struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07002976 IWL_DEBUG_RADIO("Dumping %d bytes of unhandled "
2977 "notification for %s:\n",
2978 le32_to_cpu(pkt->len), get_cmd_string(pkt->hdr.cmd));
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08002979 iwl_print_hex_dump(priv, IWL_DL_RADIO, pkt->u.raw,
2980 le32_to_cpu(pkt->len));
Zhu Yib481de92007-09-25 17:54:57 -07002981}
2982
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002983static void iwl3945_bg_beacon_update(struct work_struct *work)
Zhu Yib481de92007-09-25 17:54:57 -07002984{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002985 struct iwl_priv *priv =
2986 container_of(work, struct iwl_priv, beacon_update);
Zhu Yib481de92007-09-25 17:54:57 -07002987 struct sk_buff *beacon;
2988
2989 /* Pull updated AP beacon from mac80211. will fail if not in AP mode */
Johannes Berge039fa42008-05-15 12:55:29 +02002990 beacon = ieee80211_beacon_get(priv->hw, priv->vif);
Zhu Yib481de92007-09-25 17:54:57 -07002991
2992 if (!beacon) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08002993 IWL_ERR(priv, "update beacon failed\n");
Zhu Yib481de92007-09-25 17:54:57 -07002994 return;
2995 }
2996
2997 mutex_lock(&priv->mutex);
2998 /* new beacon skb is allocated every time; dispose previous.*/
2999 if (priv->ibss_beacon)
3000 dev_kfree_skb(priv->ibss_beacon);
3001
3002 priv->ibss_beacon = beacon;
3003 mutex_unlock(&priv->mutex);
3004
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003005 iwl3945_send_beacon_cmd(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003006}
3007
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003008static void iwl3945_rx_beacon_notif(struct iwl_priv *priv,
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08003009 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003010{
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08003011#ifdef CONFIG_IWL3945_DEBUG
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08003012 struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003013 struct iwl3945_beacon_notif *beacon = &(pkt->u.beacon_status);
Zhu Yib481de92007-09-25 17:54:57 -07003014 u8 rate = beacon->beacon_notify_hdr.rate;
3015
3016 IWL_DEBUG_RX("beacon status %x retries %d iss %d "
3017 "tsf %d %d rate %d\n",
3018 le32_to_cpu(beacon->beacon_notify_hdr.status) & TX_STATUS_MSK,
3019 beacon->beacon_notify_hdr.failure_frame,
3020 le32_to_cpu(beacon->ibss_mgr_status),
3021 le32_to_cpu(beacon->high_tsf),
3022 le32_to_cpu(beacon->low_tsf), rate);
3023#endif
3024
Johannes Berg05c914f2008-09-11 00:01:58 +02003025 if ((priv->iw_mode == NL80211_IFTYPE_AP) &&
Zhu Yib481de92007-09-25 17:54:57 -07003026 (!test_bit(STATUS_EXIT_PENDING, &priv->status)))
3027 queue_work(priv->workqueue, &priv->beacon_update);
3028}
3029
3030/* Service response to REPLY_SCAN_CMD (0x80) */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003031static void iwl3945_rx_reply_scan(struct iwl_priv *priv,
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08003032 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003033{
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08003034#ifdef CONFIG_IWL3945_DEBUG
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08003035 struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
Tomas Winkler4c897252008-12-19 10:37:05 +08003036 struct iwl_scanreq_notification *notif =
3037 (struct iwl_scanreq_notification *)pkt->u.raw;
Zhu Yib481de92007-09-25 17:54:57 -07003038
3039 IWL_DEBUG_RX("Scan request status = 0x%x\n", notif->status);
3040#endif
3041}
3042
3043/* Service SCAN_START_NOTIFICATION (0x82) */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003044static void iwl3945_rx_scan_start_notif(struct iwl_priv *priv,
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08003045 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003046{
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08003047 struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
Tomas Winkler4c897252008-12-19 10:37:05 +08003048 struct iwl_scanstart_notification *notif =
3049 (struct iwl_scanstart_notification *)pkt->u.raw;
Zhu Yib481de92007-09-25 17:54:57 -07003050 priv->scan_start_tsf = le32_to_cpu(notif->tsf_low);
3051 IWL_DEBUG_SCAN("Scan start: "
3052 "%d [802.11%s] "
3053 "(TSF: 0x%08X:%08X) - %d (beacon timer %u)\n",
3054 notif->channel,
3055 notif->band ? "bg" : "a",
3056 notif->tsf_high,
3057 notif->tsf_low, notif->status, notif->beacon_timer);
3058}
3059
3060/* Service SCAN_RESULTS_NOTIFICATION (0x83) */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003061static void iwl3945_rx_scan_results_notif(struct iwl_priv *priv,
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08003062 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003063{
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08003064 struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
Tomas Winkler4c897252008-12-19 10:37:05 +08003065 struct iwl_scanresults_notification *notif =
3066 (struct iwl_scanresults_notification *)pkt->u.raw;
Zhu Yib481de92007-09-25 17:54:57 -07003067
3068 IWL_DEBUG_SCAN("Scan ch.res: "
3069 "%d [802.11%s] "
3070 "(TSF: 0x%08X:%08X) - %d "
3071 "elapsed=%lu usec (%dms since last)\n",
3072 notif->channel,
3073 notif->band ? "bg" : "a",
3074 le32_to_cpu(notif->tsf_high),
3075 le32_to_cpu(notif->tsf_low),
3076 le32_to_cpu(notif->statistics[0]),
3077 le32_to_cpu(notif->tsf_low) - priv->scan_start_tsf,
3078 jiffies_to_msecs(elapsed_jiffies
3079 (priv->last_scan_jiffies, jiffies)));
3080
3081 priv->last_scan_jiffies = jiffies;
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08003082 priv->next_scan_jiffies = 0;
Zhu Yib481de92007-09-25 17:54:57 -07003083}
3084
3085/* Service SCAN_COMPLETE_NOTIFICATION (0x84) */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003086static void iwl3945_rx_scan_complete_notif(struct iwl_priv *priv,
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08003087 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003088{
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08003089 struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
Tomas Winkler4c897252008-12-19 10:37:05 +08003090 struct iwl_scancomplete_notification *scan_notif = (void *)pkt->u.raw;
Zhu Yib481de92007-09-25 17:54:57 -07003091
3092 IWL_DEBUG_SCAN("Scan complete: %d channels (TSF 0x%08X:%08X) - %d\n",
3093 scan_notif->scanned_channels,
3094 scan_notif->tsf_low,
3095 scan_notif->tsf_high, scan_notif->status);
3096
3097 /* The HW is no longer scanning */
3098 clear_bit(STATUS_SCAN_HW, &priv->status);
3099
3100 /* The scan completion notification came in, so kill that timer... */
3101 cancel_delayed_work(&priv->scan_check);
3102
3103 IWL_DEBUG_INFO("Scan pass on %sGHz took %dms\n",
Ron Rindjunsky66b50042008-06-25 16:46:31 +08003104 (priv->scan_bands & BIT(IEEE80211_BAND_2GHZ)) ?
3105 "2.4" : "5.2",
Zhu Yib481de92007-09-25 17:54:57 -07003106 jiffies_to_msecs(elapsed_jiffies
3107 (priv->scan_pass_start, jiffies)));
3108
Ron Rindjunsky66b50042008-06-25 16:46:31 +08003109 /* Remove this scanned band from the list of pending
3110 * bands to scan, band G precedes A in order of scanning
3111 * as seen in iwl3945_bg_request_scan */
3112 if (priv->scan_bands & BIT(IEEE80211_BAND_2GHZ))
3113 priv->scan_bands &= ~BIT(IEEE80211_BAND_2GHZ);
3114 else if (priv->scan_bands & BIT(IEEE80211_BAND_5GHZ))
3115 priv->scan_bands &= ~BIT(IEEE80211_BAND_5GHZ);
Zhu Yib481de92007-09-25 17:54:57 -07003116
3117 /* If a request to abort was given, or the scan did not succeed
3118 * then we reset the scan state machine and terminate,
3119 * re-queuing another scan if one has been requested */
3120 if (test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
3121 IWL_DEBUG_INFO("Aborted scan completed.\n");
3122 clear_bit(STATUS_SCAN_ABORTING, &priv->status);
3123 } else {
3124 /* If there are more bands on this scan pass reschedule */
3125 if (priv->scan_bands > 0)
3126 goto reschedule;
3127 }
3128
3129 priv->last_scan_jiffies = jiffies;
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08003130 priv->next_scan_jiffies = 0;
Zhu Yib481de92007-09-25 17:54:57 -07003131 IWL_DEBUG_INFO("Setting scan to off\n");
3132
3133 clear_bit(STATUS_SCANNING, &priv->status);
3134
3135 IWL_DEBUG_INFO("Scan took %dms\n",
3136 jiffies_to_msecs(elapsed_jiffies(priv->scan_start, jiffies)));
3137
3138 queue_work(priv->workqueue, &priv->scan_completed);
3139
3140 return;
3141
3142reschedule:
3143 priv->scan_pass_start = jiffies;
3144 queue_work(priv->workqueue, &priv->request_scan);
3145}
3146
3147/* Handle notification from uCode that card's power state is changing
3148 * due to software, hardware, or critical temperature RFKILL */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003149static void iwl3945_rx_card_state_notif(struct iwl_priv *priv,
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08003150 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003151{
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08003152 struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07003153 u32 flags = le32_to_cpu(pkt->u.card_state_notif.flags);
3154 unsigned long status = priv->status;
3155
3156 IWL_DEBUG_RF_KILL("Card state received: HW:%s SW:%s\n",
3157 (flags & HW_CARD_DISABLED) ? "Kill" : "On",
3158 (flags & SW_CARD_DISABLED) ? "Kill" : "On");
3159
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003160 iwl_write32(priv, CSR_UCODE_DRV_GP1_SET,
Zhu Yib481de92007-09-25 17:54:57 -07003161 CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
3162
3163 if (flags & HW_CARD_DISABLED)
3164 set_bit(STATUS_RF_KILL_HW, &priv->status);
3165 else
3166 clear_bit(STATUS_RF_KILL_HW, &priv->status);
3167
3168
3169 if (flags & SW_CARD_DISABLED)
3170 set_bit(STATUS_RF_KILL_SW, &priv->status);
3171 else
3172 clear_bit(STATUS_RF_KILL_SW, &priv->status);
3173
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003174 iwl3945_scan_cancel(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003175
3176 if ((test_bit(STATUS_RF_KILL_HW, &status) !=
3177 test_bit(STATUS_RF_KILL_HW, &priv->status)) ||
3178 (test_bit(STATUS_RF_KILL_SW, &status) !=
3179 test_bit(STATUS_RF_KILL_SW, &priv->status)))
3180 queue_work(priv->workqueue, &priv->rf_kill);
3181 else
3182 wake_up_interruptible(&priv->wait_command_queue);
3183}
3184
3185/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003186 * iwl3945_setup_rx_handlers - Initialize Rx handler callbacks
Zhu Yib481de92007-09-25 17:54:57 -07003187 *
3188 * Setup the RX handlers for each of the reply types sent from the uCode
3189 * to the host.
3190 *
3191 * This function chains into the hardware specific files for them to setup
3192 * any hardware specific handlers as well.
3193 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003194static void iwl3945_setup_rx_handlers(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003195{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003196 priv->rx_handlers[REPLY_ALIVE] = iwl3945_rx_reply_alive;
3197 priv->rx_handlers[REPLY_ADD_STA] = iwl3945_rx_reply_add_sta;
3198 priv->rx_handlers[REPLY_ERROR] = iwl3945_rx_reply_error;
3199 priv->rx_handlers[CHANNEL_SWITCH_NOTIFICATION] = iwl3945_rx_csa;
Zhu Yib481de92007-09-25 17:54:57 -07003200 priv->rx_handlers[SPECTRUM_MEASURE_NOTIFICATION] =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003201 iwl3945_rx_spectrum_measure_notif;
3202 priv->rx_handlers[PM_SLEEP_NOTIFICATION] = iwl3945_rx_pm_sleep_notif;
Zhu Yib481de92007-09-25 17:54:57 -07003203 priv->rx_handlers[PM_DEBUG_STATISTIC_NOTIFIC] =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003204 iwl3945_rx_pm_debug_statistics_notif;
3205 priv->rx_handlers[BEACON_NOTIFICATION] = iwl3945_rx_beacon_notif;
Zhu Yib481de92007-09-25 17:54:57 -07003206
Ben Cahill9fbab512007-11-29 11:09:47 +08003207 /*
3208 * The same handler is used for both the REPLY to a discrete
3209 * statistics request from the host as well as for the periodic
3210 * statistics notifications (after received beacons) from the uCode.
Zhu Yib481de92007-09-25 17:54:57 -07003211 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003212 priv->rx_handlers[REPLY_STATISTICS_CMD] = iwl3945_hw_rx_statistics;
3213 priv->rx_handlers[STATISTICS_NOTIFICATION] = iwl3945_hw_rx_statistics;
Zhu Yib481de92007-09-25 17:54:57 -07003214
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003215 priv->rx_handlers[REPLY_SCAN_CMD] = iwl3945_rx_reply_scan;
3216 priv->rx_handlers[SCAN_START_NOTIFICATION] = iwl3945_rx_scan_start_notif;
Zhu Yib481de92007-09-25 17:54:57 -07003217 priv->rx_handlers[SCAN_RESULTS_NOTIFICATION] =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003218 iwl3945_rx_scan_results_notif;
Zhu Yib481de92007-09-25 17:54:57 -07003219 priv->rx_handlers[SCAN_COMPLETE_NOTIFICATION] =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003220 iwl3945_rx_scan_complete_notif;
3221 priv->rx_handlers[CARD_STATE_NOTIFICATION] = iwl3945_rx_card_state_notif;
Zhu Yib481de92007-09-25 17:54:57 -07003222
Ben Cahill9fbab512007-11-29 11:09:47 +08003223 /* Set up hardware specific Rx handlers */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003224 iwl3945_hw_rx_handler_setup(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003225}
3226
3227/**
Tomas Winkler91c066f2008-03-06 17:36:55 -08003228 * iwl3945_cmd_queue_reclaim - Reclaim CMD queue entries
3229 * When FW advances 'R' index, all entries between old and new 'R' index
3230 * need to be reclaimed.
3231 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003232static void iwl3945_cmd_queue_reclaim(struct iwl_priv *priv,
Tomas Winkler91c066f2008-03-06 17:36:55 -08003233 int txq_id, int index)
3234{
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08003235 struct iwl3945_tx_queue *txq = &priv->txq39[txq_id];
Samuel Ortizd20b3c62008-12-19 10:37:15 +08003236 struct iwl_queue *q = &txq->q;
Tomas Winkler91c066f2008-03-06 17:36:55 -08003237 int nfreed = 0;
3238
3239 if ((index >= q->n_bd) || (iwl3945_x2_queue_used(q, index) == 0)) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08003240 IWL_ERR(priv, "Read index for DMA queue txq id (%d), index %d, "
Tomas Winkler91c066f2008-03-06 17:36:55 -08003241 "is out of range [0-%d] %d %d.\n", txq_id,
3242 index, q->n_bd, q->write_ptr, q->read_ptr);
3243 return;
3244 }
3245
3246 for (index = iwl_queue_inc_wrap(index, q->n_bd); q->read_ptr != index;
3247 q->read_ptr = iwl_queue_inc_wrap(q->read_ptr, q->n_bd)) {
3248 if (nfreed > 1) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08003249 IWL_ERR(priv, "HCMD skipped: index (%d) %d %d\n", index,
Tomas Winkler91c066f2008-03-06 17:36:55 -08003250 q->write_ptr, q->read_ptr);
3251 queue_work(priv->workqueue, &priv->restart);
3252 break;
3253 }
3254 nfreed++;
3255 }
3256}
3257
3258
3259/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003260 * iwl3945_tx_cmd_complete - Pull unused buffers off the queue and reclaim them
Zhu Yib481de92007-09-25 17:54:57 -07003261 * @rxb: Rx buffer to reclaim
3262 *
3263 * If an Rx buffer has an async callback associated with it the callback
3264 * will be executed. The attached skb (if present) will only be freed
3265 * if the callback returns 1
3266 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003267static void iwl3945_tx_cmd_complete(struct iwl_priv *priv,
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08003268 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003269{
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08003270 struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07003271 u16 sequence = le16_to_cpu(pkt->hdr.sequence);
3272 int txq_id = SEQ_TO_QUEUE(sequence);
3273 int index = SEQ_TO_INDEX(sequence);
Tomas Winkler600c0e12008-12-19 10:37:04 +08003274 int huge = !!(pkt->hdr.sequence & SEQ_HUGE_FRAME);
Zhu Yib481de92007-09-25 17:54:57 -07003275 int cmd_index;
Winkler, Tomasc2d79b482008-12-19 10:37:34 +08003276 struct iwl_cmd *cmd;
Zhu Yib481de92007-09-25 17:54:57 -07003277
Zhu Yib481de92007-09-25 17:54:57 -07003278 BUG_ON(txq_id != IWL_CMD_QUEUE_NUM);
3279
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08003280 cmd_index = get_cmd_index(&priv->txq39[IWL_CMD_QUEUE_NUM].q, index, huge);
3281 cmd = &priv->txq39[IWL_CMD_QUEUE_NUM].cmd[cmd_index];
Zhu Yib481de92007-09-25 17:54:57 -07003282
3283 /* Input error checking is done when commands are added to queue. */
3284 if (cmd->meta.flags & CMD_WANT_SKB) {
3285 cmd->meta.source->u.skb = rxb->skb;
3286 rxb->skb = NULL;
3287 } else if (cmd->meta.u.callback &&
3288 !cmd->meta.u.callback(priv, cmd, rxb->skb))
3289 rxb->skb = NULL;
3290
Tomas Winkler91c066f2008-03-06 17:36:55 -08003291 iwl3945_cmd_queue_reclaim(priv, txq_id, index);
Zhu Yib481de92007-09-25 17:54:57 -07003292
3293 if (!(cmd->meta.flags & CMD_ASYNC)) {
3294 clear_bit(STATUS_HCMD_ACTIVE, &priv->status);
3295 wake_up_interruptible(&priv->wait_command_queue);
3296 }
3297}
3298
3299/************************** RX-FUNCTIONS ****************************/
3300/*
3301 * Rx theory of operation
3302 *
3303 * The host allocates 32 DMA target addresses and passes the host address
3304 * to the firmware at register IWL_RFDS_TABLE_LOWER + N * RFD_SIZE where N is
3305 * 0 to 31
3306 *
3307 * Rx Queue Indexes
3308 * The host/firmware share two index registers for managing the Rx buffers.
3309 *
3310 * The READ index maps to the first position that the firmware may be writing
3311 * to -- the driver can read up to (but not including) this position and get
3312 * good data.
3313 * The READ index is managed by the firmware once the card is enabled.
3314 *
3315 * The WRITE index maps to the last position the driver has read from -- the
3316 * position preceding WRITE is the last slot the firmware can place a packet.
3317 *
3318 * The queue is empty (no good data) if WRITE = READ - 1, and is full if
3319 * WRITE = READ.
3320 *
Ben Cahill9fbab512007-11-29 11:09:47 +08003321 * During initialization, the host sets up the READ queue position to the first
Zhu Yib481de92007-09-25 17:54:57 -07003322 * INDEX position, and WRITE to the last (READ - 1 wrapped)
3323 *
Ben Cahill9fbab512007-11-29 11:09:47 +08003324 * When the firmware places a packet in a buffer, it will advance the READ index
Zhu Yib481de92007-09-25 17:54:57 -07003325 * and fire the RX interrupt. The driver can then query the READ index and
3326 * process as many packets as possible, moving the WRITE index forward as it
3327 * resets the Rx queue buffers with new memory.
3328 *
3329 * The management in the driver is as follows:
3330 * + A list of pre-allocated SKBs is stored in iwl->rxq->rx_free. When
3331 * iwl->rxq->free_count drops to or below RX_LOW_WATERMARK, work is scheduled
Ian Schram01ebd062007-10-25 17:15:22 +08003332 * to replenish the iwl->rxq->rx_free.
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003333 * + In iwl3945_rx_replenish (scheduled) if 'processed' != 'read' then the
Zhu Yib481de92007-09-25 17:54:57 -07003334 * iwl->rxq is replenished and the READ INDEX is updated (updating the
3335 * 'processed' and 'read' driver indexes as well)
3336 * + A received packet is processed and handed to the kernel network stack,
3337 * detached from the iwl->rxq. The driver 'processed' index is updated.
3338 * + The Host/Firmware iwl->rxq is replenished at tasklet time from the rx_free
3339 * list. If there are no allocated buffers in iwl->rxq->rx_free, the READ
3340 * INDEX is not incremented and iwl->status(RX_STALLED) is set. If there
3341 * were enough free buffers and RX_STALLED is set it is cleared.
3342 *
3343 *
3344 * Driver sequence:
3345 *
Ben Cahill9fbab512007-11-29 11:09:47 +08003346 * iwl3945_rx_queue_alloc() Allocates rx_free
3347 * iwl3945_rx_replenish() Replenishes rx_free list from rx_used, and calls
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003348 * iwl3945_rx_queue_restock
Ben Cahill9fbab512007-11-29 11:09:47 +08003349 * iwl3945_rx_queue_restock() Moves available buffers from rx_free into Rx
Zhu Yib481de92007-09-25 17:54:57 -07003350 * queue, updates firmware pointers, and updates
3351 * the WRITE index. If insufficient rx_free buffers
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003352 * are available, schedules iwl3945_rx_replenish
Zhu Yib481de92007-09-25 17:54:57 -07003353 *
3354 * -- enable interrupts --
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08003355 * ISR - iwl3945_rx() Detach iwl_rx_mem_buffers from pool up to the
Zhu Yib481de92007-09-25 17:54:57 -07003356 * READ INDEX, detaching the SKB from the pool.
3357 * Moves the packet buffer from queue to rx_used.
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003358 * Calls iwl3945_rx_queue_restock to refill any empty
Zhu Yib481de92007-09-25 17:54:57 -07003359 * slots.
3360 * ...
3361 *
3362 */
3363
3364/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003365 * iwl3945_rx_queue_space - Return number of free slots available in queue.
Zhu Yib481de92007-09-25 17:54:57 -07003366 */
Abhijeet Kolekarcc2f3622008-12-19 10:37:25 +08003367static int iwl3945_rx_queue_space(const struct iwl_rx_queue *q)
Zhu Yib481de92007-09-25 17:54:57 -07003368{
3369 int s = q->read - q->write;
3370 if (s <= 0)
3371 s += RX_QUEUE_SIZE;
3372 /* keep some buffer to not confuse full and empty queue */
3373 s -= 2;
3374 if (s < 0)
3375 s = 0;
3376 return s;
3377}
3378
3379/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003380 * iwl3945_rx_queue_update_write_ptr - Update the write pointer for the RX queue
Zhu Yib481de92007-09-25 17:54:57 -07003381 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003382int iwl3945_rx_queue_update_write_ptr(struct iwl_priv *priv, struct iwl_rx_queue *q)
Zhu Yib481de92007-09-25 17:54:57 -07003383{
3384 u32 reg = 0;
3385 int rc = 0;
3386 unsigned long flags;
3387
3388 spin_lock_irqsave(&q->lock, flags);
3389
3390 if (q->need_update == 0)
3391 goto exit_unlock;
3392
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003393 /* If power-saving is in use, make sure device is awake */
Zhu Yib481de92007-09-25 17:54:57 -07003394 if (test_bit(STATUS_POWER_PMI, &priv->status)) {
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003395 reg = iwl_read32(priv, CSR_UCODE_DRV_GP1);
Zhu Yib481de92007-09-25 17:54:57 -07003396
3397 if (reg & CSR_UCODE_DRV_GP1_BIT_MAC_SLEEP) {
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003398 iwl_set_bit(priv, CSR_GP_CNTRL,
Zhu Yib481de92007-09-25 17:54:57 -07003399 CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
3400 goto exit_unlock;
3401 }
3402
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003403 rc = iwl_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003404 if (rc)
3405 goto exit_unlock;
3406
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003407 /* Device expects a multiple of 8 */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003408 iwl_write_direct32(priv, FH39_RSCSR_CHNL0_WPTR,
Zhu Yib481de92007-09-25 17:54:57 -07003409 q->write & ~0x7);
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003410 iwl_release_nic_access(priv);
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003411
3412 /* Else device is assumed to be awake */
Zhu Yib481de92007-09-25 17:54:57 -07003413 } else
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003414 /* Device expects a multiple of 8 */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003415 iwl_write32(priv, FH39_RSCSR_CHNL0_WPTR, q->write & ~0x7);
Zhu Yib481de92007-09-25 17:54:57 -07003416
3417
3418 q->need_update = 0;
3419
3420 exit_unlock:
3421 spin_unlock_irqrestore(&q->lock, flags);
3422 return rc;
3423}
3424
3425/**
Ben Cahill9fbab512007-11-29 11:09:47 +08003426 * iwl3945_dma_addr2rbd_ptr - convert a DMA address to a uCode read buffer ptr
Zhu Yib481de92007-09-25 17:54:57 -07003427 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003428static inline __le32 iwl3945_dma_addr2rbd_ptr(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07003429 dma_addr_t dma_addr)
3430{
3431 return cpu_to_le32((u32)dma_addr);
3432}
3433
3434/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003435 * iwl3945_rx_queue_restock - refill RX queue from pre-allocated pool
Zhu Yib481de92007-09-25 17:54:57 -07003436 *
Ben Cahill9fbab512007-11-29 11:09:47 +08003437 * If there are slots in the RX queue that need to be restocked,
Zhu Yib481de92007-09-25 17:54:57 -07003438 * and we have free pre-allocated buffers, fill the ranks as much
Ben Cahill9fbab512007-11-29 11:09:47 +08003439 * as we can, pulling from rx_free.
Zhu Yib481de92007-09-25 17:54:57 -07003440 *
3441 * This moves the 'write' index forward to catch up with 'processed', and
3442 * also updates the memory address in the firmware to reference the new
3443 * target buffer.
3444 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003445static int iwl3945_rx_queue_restock(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003446{
Abhijeet Kolekarcc2f3622008-12-19 10:37:25 +08003447 struct iwl_rx_queue *rxq = &priv->rxq;
Zhu Yib481de92007-09-25 17:54:57 -07003448 struct list_head *element;
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08003449 struct iwl_rx_mem_buffer *rxb;
Zhu Yib481de92007-09-25 17:54:57 -07003450 unsigned long flags;
3451 int write, rc;
3452
3453 spin_lock_irqsave(&rxq->lock, flags);
3454 write = rxq->write & ~0x7;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003455 while ((iwl3945_rx_queue_space(rxq) > 0) && (rxq->free_count)) {
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003456 /* Get next free Rx buffer, remove from free list */
Zhu Yib481de92007-09-25 17:54:57 -07003457 element = rxq->rx_free.next;
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08003458 rxb = list_entry(element, struct iwl_rx_mem_buffer, list);
Zhu Yib481de92007-09-25 17:54:57 -07003459 list_del(element);
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003460
3461 /* Point to Rx buffer via next RBD in circular buffer */
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08003462 rxq->bd[rxq->write] = iwl3945_dma_addr2rbd_ptr(priv, rxb->real_dma_addr);
Zhu Yib481de92007-09-25 17:54:57 -07003463 rxq->queue[rxq->write] = rxb;
3464 rxq->write = (rxq->write + 1) & RX_QUEUE_MASK;
3465 rxq->free_count--;
3466 }
3467 spin_unlock_irqrestore(&rxq->lock, flags);
3468 /* If the pre-allocated buffer pool is dropping low, schedule to
3469 * refill it */
3470 if (rxq->free_count <= RX_LOW_WATERMARK)
3471 queue_work(priv->workqueue, &priv->rx_replenish);
3472
3473
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003474 /* If we've added more space for the firmware to place data, tell it.
3475 * Increment device's write pointer in multiples of 8. */
Zhu Yib481de92007-09-25 17:54:57 -07003476 if ((write != (rxq->write & ~0x7))
3477 || (abs(rxq->write - rxq->read) > 7)) {
3478 spin_lock_irqsave(&rxq->lock, flags);
3479 rxq->need_update = 1;
3480 spin_unlock_irqrestore(&rxq->lock, flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003481 rc = iwl3945_rx_queue_update_write_ptr(priv, rxq);
Zhu Yib481de92007-09-25 17:54:57 -07003482 if (rc)
3483 return rc;
3484 }
3485
3486 return 0;
3487}
3488
3489/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003490 * iwl3945_rx_replenish - Move all used packet from rx_used to rx_free
Zhu Yib481de92007-09-25 17:54:57 -07003491 *
3492 * When moving to rx_free an SKB is allocated for the slot.
3493 *
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003494 * Also restock the Rx queue via iwl3945_rx_queue_restock.
Ian Schram01ebd062007-10-25 17:15:22 +08003495 * This is called as a scheduled work item (except for during initialization)
Zhu Yib481de92007-09-25 17:54:57 -07003496 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003497static void iwl3945_rx_allocate(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003498{
Abhijeet Kolekarcc2f3622008-12-19 10:37:25 +08003499 struct iwl_rx_queue *rxq = &priv->rxq;
Zhu Yib481de92007-09-25 17:54:57 -07003500 struct list_head *element;
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08003501 struct iwl_rx_mem_buffer *rxb;
Zhu Yib481de92007-09-25 17:54:57 -07003502 unsigned long flags;
3503 spin_lock_irqsave(&rxq->lock, flags);
3504 while (!list_empty(&rxq->rx_used)) {
3505 element = rxq->rx_used.next;
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08003506 rxb = list_entry(element, struct iwl_rx_mem_buffer, list);
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003507
3508 /* Alloc a new receive buffer */
Zhu Yib481de92007-09-25 17:54:57 -07003509 rxb->skb =
3510 alloc_skb(IWL_RX_BUF_SIZE, __GFP_NOWARN | GFP_ATOMIC);
3511 if (!rxb->skb) {
3512 if (net_ratelimit())
Tomas Winkler978785a2008-12-19 10:37:31 +08003513 IWL_CRIT(priv, ": Can not allocate SKB buffers\n");
Zhu Yib481de92007-09-25 17:54:57 -07003514 /* We don't reschedule replenish work here -- we will
3515 * call the restock method and if it still needs
3516 * more buffers it will schedule replenish */
3517 break;
3518 }
Zhu Yi12342c42007-12-20 11:27:32 +08003519
3520 /* If radiotap head is required, reserve some headroom here.
3521 * The physical head count is a variable rx_stats->phy_count.
3522 * We reserve 4 bytes here. Plus these extra bytes, the
3523 * headroom of the physical head should be enough for the
3524 * radiotap head that iwl3945 supported. See iwl3945_rt.
3525 */
3526 skb_reserve(rxb->skb, 4);
3527
Zhu Yib481de92007-09-25 17:54:57 -07003528 priv->alloc_rxb_skb++;
3529 list_del(element);
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003530
3531 /* Get physical address of RB/SKB */
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08003532 rxb->real_dma_addr =
Zhu Yib481de92007-09-25 17:54:57 -07003533 pci_map_single(priv->pci_dev, rxb->skb->data,
3534 IWL_RX_BUF_SIZE, PCI_DMA_FROMDEVICE);
3535 list_add_tail(&rxb->list, &rxq->rx_free);
3536 rxq->free_count++;
3537 }
3538 spin_unlock_irqrestore(&rxq->lock, flags);
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08003539}
3540
3541/*
3542 * this should be called while priv->lock is locked
3543 */
Tomas Winkler4fd1f842007-12-05 20:59:58 +02003544static void __iwl3945_rx_replenish(void *data)
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08003545{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003546 struct iwl_priv *priv = data;
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08003547
3548 iwl3945_rx_allocate(priv);
3549 iwl3945_rx_queue_restock(priv);
3550}
3551
3552
3553void iwl3945_rx_replenish(void *data)
3554{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003555 struct iwl_priv *priv = data;
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08003556 unsigned long flags;
3557
3558 iwl3945_rx_allocate(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003559
3560 spin_lock_irqsave(&priv->lock, flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003561 iwl3945_rx_queue_restock(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003562 spin_unlock_irqrestore(&priv->lock, flags);
3563}
3564
3565/* Assumes that the skb field of the buffers in 'pool' is kept accurate.
Ben Cahill9fbab512007-11-29 11:09:47 +08003566 * If an SKB has been detached, the POOL needs to have its SKB set to NULL
Zhu Yib481de92007-09-25 17:54:57 -07003567 * This free routine walks the list of POOL entries and if SKB is set to
3568 * non NULL it is unmapped and freed
3569 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003570static void iwl3945_rx_queue_free(struct iwl_priv *priv, struct iwl_rx_queue *rxq)
Zhu Yib481de92007-09-25 17:54:57 -07003571{
3572 int i;
3573 for (i = 0; i < RX_QUEUE_SIZE + RX_FREE_BUFFERS; i++) {
3574 if (rxq->pool[i].skb != NULL) {
3575 pci_unmap_single(priv->pci_dev,
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08003576 rxq->pool[i].real_dma_addr,
Zhu Yib481de92007-09-25 17:54:57 -07003577 IWL_RX_BUF_SIZE, PCI_DMA_FROMDEVICE);
3578 dev_kfree_skb(rxq->pool[i].skb);
3579 }
3580 }
3581
3582 pci_free_consistent(priv->pci_dev, 4 * RX_QUEUE_SIZE, rxq->bd,
3583 rxq->dma_addr);
3584 rxq->bd = NULL;
3585}
3586
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003587int iwl3945_rx_queue_alloc(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003588{
Abhijeet Kolekarcc2f3622008-12-19 10:37:25 +08003589 struct iwl_rx_queue *rxq = &priv->rxq;
Zhu Yib481de92007-09-25 17:54:57 -07003590 struct pci_dev *dev = priv->pci_dev;
3591 int i;
3592
3593 spin_lock_init(&rxq->lock);
3594 INIT_LIST_HEAD(&rxq->rx_free);
3595 INIT_LIST_HEAD(&rxq->rx_used);
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003596
3597 /* Alloc the circular buffer of Read Buffer Descriptors (RBDs) */
Zhu Yib481de92007-09-25 17:54:57 -07003598 rxq->bd = pci_alloc_consistent(dev, 4 * RX_QUEUE_SIZE, &rxq->dma_addr);
3599 if (!rxq->bd)
3600 return -ENOMEM;
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003601
Zhu Yib481de92007-09-25 17:54:57 -07003602 /* Fill the rx_used queue with _all_ of the Rx buffers */
3603 for (i = 0; i < RX_FREE_BUFFERS + RX_QUEUE_SIZE; i++)
3604 list_add_tail(&rxq->pool[i].list, &rxq->rx_used);
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003605
Zhu Yib481de92007-09-25 17:54:57 -07003606 /* Set us so that we have processed and used all buffers, but have
3607 * not restocked the Rx queue with fresh buffers */
3608 rxq->read = rxq->write = 0;
3609 rxq->free_count = 0;
3610 rxq->need_update = 0;
3611 return 0;
3612}
3613
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003614void iwl3945_rx_queue_reset(struct iwl_priv *priv, struct iwl_rx_queue *rxq)
Zhu Yib481de92007-09-25 17:54:57 -07003615{
3616 unsigned long flags;
3617 int i;
3618 spin_lock_irqsave(&rxq->lock, flags);
3619 INIT_LIST_HEAD(&rxq->rx_free);
3620 INIT_LIST_HEAD(&rxq->rx_used);
3621 /* Fill the rx_used queue with _all_ of the Rx buffers */
3622 for (i = 0; i < RX_FREE_BUFFERS + RX_QUEUE_SIZE; i++) {
3623 /* In the reset function, these buffers may have been allocated
3624 * to an SKB, so we need to unmap and free potential storage */
3625 if (rxq->pool[i].skb != NULL) {
3626 pci_unmap_single(priv->pci_dev,
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08003627 rxq->pool[i].real_dma_addr,
Zhu Yib481de92007-09-25 17:54:57 -07003628 IWL_RX_BUF_SIZE, PCI_DMA_FROMDEVICE);
3629 priv->alloc_rxb_skb--;
3630 dev_kfree_skb(rxq->pool[i].skb);
3631 rxq->pool[i].skb = NULL;
3632 }
3633 list_add_tail(&rxq->pool[i].list, &rxq->rx_used);
3634 }
3635
3636 /* Set us so that we have processed and used all buffers, but have
3637 * not restocked the Rx queue with fresh buffers */
3638 rxq->read = rxq->write = 0;
3639 rxq->free_count = 0;
3640 spin_unlock_irqrestore(&rxq->lock, flags);
3641}
3642
3643/* Convert linear signal-to-noise ratio into dB */
3644static u8 ratio2dB[100] = {
3645/* 0 1 2 3 4 5 6 7 8 9 */
3646 0, 0, 6, 10, 12, 14, 16, 17, 18, 19, /* 00 - 09 */
3647 20, 21, 22, 22, 23, 23, 24, 25, 26, 26, /* 10 - 19 */
3648 26, 26, 26, 27, 27, 28, 28, 28, 29, 29, /* 20 - 29 */
3649 29, 30, 30, 30, 31, 31, 31, 31, 32, 32, /* 30 - 39 */
3650 32, 32, 32, 33, 33, 33, 33, 33, 34, 34, /* 40 - 49 */
3651 34, 34, 34, 34, 35, 35, 35, 35, 35, 35, /* 50 - 59 */
3652 36, 36, 36, 36, 36, 36, 36, 37, 37, 37, /* 60 - 69 */
3653 37, 37, 37, 37, 37, 38, 38, 38, 38, 38, /* 70 - 79 */
3654 38, 38, 38, 38, 38, 39, 39, 39, 39, 39, /* 80 - 89 */
3655 39, 39, 39, 39, 39, 40, 40, 40, 40, 40 /* 90 - 99 */
3656};
3657
3658/* Calculates a relative dB value from a ratio of linear
3659 * (i.e. not dB) signal levels.
3660 * Conversion assumes that levels are voltages (20*log), not powers (10*log). */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003661int iwl3945_calc_db_from_ratio(int sig_ratio)
Zhu Yib481de92007-09-25 17:54:57 -07003662{
Adrian Bunk221c80c2008-02-02 23:19:01 +02003663 /* 1000:1 or higher just report as 60 dB */
3664 if (sig_ratio >= 1000)
Zhu Yib481de92007-09-25 17:54:57 -07003665 return 60;
3666
Adrian Bunk221c80c2008-02-02 23:19:01 +02003667 /* 100:1 or higher, divide by 10 and use table,
Zhu Yib481de92007-09-25 17:54:57 -07003668 * add 20 dB to make up for divide by 10 */
Adrian Bunk221c80c2008-02-02 23:19:01 +02003669 if (sig_ratio >= 100)
Tomas Winkler3ac7f142008-07-21 02:40:14 +03003670 return 20 + (int)ratio2dB[sig_ratio/10];
Zhu Yib481de92007-09-25 17:54:57 -07003671
3672 /* We shouldn't see this */
3673 if (sig_ratio < 1)
3674 return 0;
3675
3676 /* Use table for ratios 1:1 - 99:1 */
3677 return (int)ratio2dB[sig_ratio];
3678}
3679
3680#define PERFECT_RSSI (-20) /* dBm */
3681#define WORST_RSSI (-95) /* dBm */
3682#define RSSI_RANGE (PERFECT_RSSI - WORST_RSSI)
3683
3684/* Calculate an indication of rx signal quality (a percentage, not dBm!).
3685 * See http://www.ces.clemson.edu/linux/signal_quality.shtml for info
3686 * about formulas used below. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003687int iwl3945_calc_sig_qual(int rssi_dbm, int noise_dbm)
Zhu Yib481de92007-09-25 17:54:57 -07003688{
3689 int sig_qual;
3690 int degradation = PERFECT_RSSI - rssi_dbm;
3691
3692 /* If we get a noise measurement, use signal-to-noise ratio (SNR)
3693 * as indicator; formula is (signal dbm - noise dbm).
3694 * SNR at or above 40 is a great signal (100%).
3695 * Below that, scale to fit SNR of 0 - 40 dB within 0 - 100% indicator.
3696 * Weakest usable signal is usually 10 - 15 dB SNR. */
3697 if (noise_dbm) {
3698 if (rssi_dbm - noise_dbm >= 40)
3699 return 100;
3700 else if (rssi_dbm < noise_dbm)
3701 return 0;
3702 sig_qual = ((rssi_dbm - noise_dbm) * 5) / 2;
3703
3704 /* Else use just the signal level.
3705 * This formula is a least squares fit of data points collected and
3706 * compared with a reference system that had a percentage (%) display
3707 * for signal quality. */
3708 } else
3709 sig_qual = (100 * (RSSI_RANGE * RSSI_RANGE) - degradation *
3710 (15 * RSSI_RANGE + 62 * degradation)) /
3711 (RSSI_RANGE * RSSI_RANGE);
3712
3713 if (sig_qual > 100)
3714 sig_qual = 100;
3715 else if (sig_qual < 1)
3716 sig_qual = 0;
3717
3718 return sig_qual;
3719}
3720
3721/**
Ben Cahill9fbab512007-11-29 11:09:47 +08003722 * iwl3945_rx_handle - Main entry function for receiving responses from uCode
Zhu Yib481de92007-09-25 17:54:57 -07003723 *
3724 * Uses the priv->rx_handlers callback function array to invoke
3725 * the appropriate handlers, including command responses,
3726 * frame-received notifications, and other notifications.
3727 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003728static void iwl3945_rx_handle(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003729{
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08003730 struct iwl_rx_mem_buffer *rxb;
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08003731 struct iwl_rx_packet *pkt;
Abhijeet Kolekarcc2f3622008-12-19 10:37:25 +08003732 struct iwl_rx_queue *rxq = &priv->rxq;
Zhu Yib481de92007-09-25 17:54:57 -07003733 u32 r, i;
3734 int reclaim;
3735 unsigned long flags;
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08003736 u8 fill_rx = 0;
Mohamed Abbasd68ab682008-02-07 13:16:33 -08003737 u32 count = 8;
Zhu Yib481de92007-09-25 17:54:57 -07003738
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003739 /* uCode's read index (stored in shared DRAM) indicates the last Rx
3740 * buffer that the driver may process (last buffer filled by ucode). */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003741 r = iwl3945_hw_get_rx_read(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003742 i = rxq->read;
3743
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08003744 if (iwl3945_rx_queue_space(rxq) > (RX_QUEUE_SIZE / 2))
3745 fill_rx = 1;
Zhu Yib481de92007-09-25 17:54:57 -07003746 /* Rx interrupt, but nothing sent from uCode */
3747 if (i == r)
3748 IWL_DEBUG(IWL_DL_RX | IWL_DL_ISR, "r = %d, i = %d\n", r, i);
3749
3750 while (i != r) {
3751 rxb = rxq->queue[i];
3752
Ben Cahill9fbab512007-11-29 11:09:47 +08003753 /* If an RXB doesn't have a Rx queue slot associated with it,
Zhu Yib481de92007-09-25 17:54:57 -07003754 * then a bug has been introduced in the queue refilling
3755 * routines -- catch it here */
3756 BUG_ON(rxb == NULL);
3757
3758 rxq->queue[i] = NULL;
3759
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08003760 pci_dma_sync_single_for_cpu(priv->pci_dev, rxb->real_dma_addr,
Zhu Yib481de92007-09-25 17:54:57 -07003761 IWL_RX_BUF_SIZE,
3762 PCI_DMA_FROMDEVICE);
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08003763 pkt = (struct iwl_rx_packet *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07003764
3765 /* Reclaim a command buffer only if this packet is a response
3766 * to a (driver-originated) command.
3767 * If the packet (e.g. Rx frame) originated from uCode,
3768 * there is no command buffer to reclaim.
3769 * Ucode should set SEQ_RX_FRAME bit if ucode-originated,
3770 * but apparently a few don't get set; catch them here. */
3771 reclaim = !(pkt->hdr.sequence & SEQ_RX_FRAME) &&
3772 (pkt->hdr.cmd != STATISTICS_NOTIFICATION) &&
3773 (pkt->hdr.cmd != REPLY_TX);
3774
3775 /* Based on type of command response or notification,
3776 * handle those that need handling via function in
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003777 * rx_handlers table. See iwl3945_setup_rx_handlers() */
Zhu Yib481de92007-09-25 17:54:57 -07003778 if (priv->rx_handlers[pkt->hdr.cmd]) {
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08003779 IWL_DEBUG(IWL_DL_HCMD | IWL_DL_RX | IWL_DL_ISR,
Zhu Yib481de92007-09-25 17:54:57 -07003780 "r = %d, i = %d, %s, 0x%02x\n", r, i,
3781 get_cmd_string(pkt->hdr.cmd), pkt->hdr.cmd);
3782 priv->rx_handlers[pkt->hdr.cmd] (priv, rxb);
3783 } else {
3784 /* No handling needed */
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08003785 IWL_DEBUG(IWL_DL_HCMD | IWL_DL_RX | IWL_DL_ISR,
Zhu Yib481de92007-09-25 17:54:57 -07003786 "r %d i %d No handler needed for %s, 0x%02x\n",
3787 r, i, get_cmd_string(pkt->hdr.cmd),
3788 pkt->hdr.cmd);
3789 }
3790
3791 if (reclaim) {
Ben Cahill9fbab512007-11-29 11:09:47 +08003792 /* Invoke any callbacks, transfer the skb to caller, and
3793 * fire off the (possibly) blocking iwl3945_send_cmd()
Zhu Yib481de92007-09-25 17:54:57 -07003794 * as we reclaim the driver command queue */
3795 if (rxb && rxb->skb)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003796 iwl3945_tx_cmd_complete(priv, rxb);
Zhu Yib481de92007-09-25 17:54:57 -07003797 else
Winkler, Tomas39aadf82008-12-19 10:37:32 +08003798 IWL_WARN(priv, "Claim null rxb?\n");
Zhu Yib481de92007-09-25 17:54:57 -07003799 }
3800
3801 /* For now we just don't re-use anything. We can tweak this
3802 * later to try and re-use notification packets and SKBs that
3803 * fail to Rx correctly */
3804 if (rxb->skb != NULL) {
3805 priv->alloc_rxb_skb--;
3806 dev_kfree_skb_any(rxb->skb);
3807 rxb->skb = NULL;
3808 }
3809
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08003810 pci_unmap_single(priv->pci_dev, rxb->real_dma_addr,
Zhu Yib481de92007-09-25 17:54:57 -07003811 IWL_RX_BUF_SIZE, PCI_DMA_FROMDEVICE);
3812 spin_lock_irqsave(&rxq->lock, flags);
3813 list_add_tail(&rxb->list, &priv->rxq.rx_used);
3814 spin_unlock_irqrestore(&rxq->lock, flags);
3815 i = (i + 1) & RX_QUEUE_MASK;
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08003816 /* If there are a lot of unused frames,
3817 * restock the Rx queue so ucode won't assert. */
3818 if (fill_rx) {
3819 count++;
3820 if (count >= 8) {
3821 priv->rxq.read = i;
3822 __iwl3945_rx_replenish(priv);
3823 count = 0;
3824 }
3825 }
Zhu Yib481de92007-09-25 17:54:57 -07003826 }
3827
3828 /* Backtrack one entry */
3829 priv->rxq.read = i;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003830 iwl3945_rx_queue_restock(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003831}
3832
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003833/**
3834 * iwl3945_tx_queue_update_write_ptr - Send new write index to hardware
3835 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003836static int iwl3945_tx_queue_update_write_ptr(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003837 struct iwl3945_tx_queue *txq)
Zhu Yib481de92007-09-25 17:54:57 -07003838{
3839 u32 reg = 0;
3840 int rc = 0;
3841 int txq_id = txq->q.id;
3842
3843 if (txq->need_update == 0)
3844 return rc;
3845
3846 /* if we're trying to save power */
3847 if (test_bit(STATUS_POWER_PMI, &priv->status)) {
3848 /* wake up nic if it's powered down ...
3849 * uCode will wake up, and interrupt us again, so next
3850 * time we'll skip this part. */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003851 reg = iwl_read32(priv, CSR_UCODE_DRV_GP1);
Zhu Yib481de92007-09-25 17:54:57 -07003852
3853 if (reg & CSR_UCODE_DRV_GP1_BIT_MAC_SLEEP) {
3854 IWL_DEBUG_INFO("Requesting wakeup, GP1 = 0x%x\n", reg);
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003855 iwl_set_bit(priv, CSR_GP_CNTRL,
Zhu Yib481de92007-09-25 17:54:57 -07003856 CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
3857 return rc;
3858 }
3859
3860 /* restore this queue's parameters in nic hardware. */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003861 rc = iwl_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003862 if (rc)
3863 return rc;
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003864 iwl_write_direct32(priv, HBUS_TARG_WRPTR,
Tomas Winklerfc4b6852007-10-25 17:15:24 +08003865 txq->q.write_ptr | (txq_id << 8));
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003866 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003867
3868 /* else not in power-save mode, uCode will never sleep when we're
3869 * trying to tx (during RFKILL, we're not trying to tx). */
3870 } else
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003871 iwl_write32(priv, HBUS_TARG_WRPTR,
Tomas Winklerfc4b6852007-10-25 17:15:24 +08003872 txq->q.write_ptr | (txq_id << 8));
Zhu Yib481de92007-09-25 17:54:57 -07003873
3874 txq->need_update = 0;
3875
3876 return rc;
3877}
3878
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08003879#ifdef CONFIG_IWL3945_DEBUG
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003880static void iwl3945_print_rx_config_cmd(struct iwl_priv *priv,
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08003881 struct iwl3945_rxon_cmd *rxon)
Zhu Yib481de92007-09-25 17:54:57 -07003882{
3883 IWL_DEBUG_RADIO("RX CONFIG:\n");
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08003884 iwl_print_hex_dump(priv, IWL_DL_RADIO, (u8 *) rxon, sizeof(*rxon));
Zhu Yib481de92007-09-25 17:54:57 -07003885 IWL_DEBUG_RADIO("u16 channel: 0x%x\n", le16_to_cpu(rxon->channel));
3886 IWL_DEBUG_RADIO("u32 flags: 0x%08X\n", le32_to_cpu(rxon->flags));
3887 IWL_DEBUG_RADIO("u32 filter_flags: 0x%08x\n",
3888 le32_to_cpu(rxon->filter_flags));
3889 IWL_DEBUG_RADIO("u8 dev_type: 0x%x\n", rxon->dev_type);
3890 IWL_DEBUG_RADIO("u8 ofdm_basic_rates: 0x%02x\n",
3891 rxon->ofdm_basic_rates);
3892 IWL_DEBUG_RADIO("u8 cck_basic_rates: 0x%02x\n", rxon->cck_basic_rates);
Johannes Berge1749612008-10-27 15:59:26 -07003893 IWL_DEBUG_RADIO("u8[6] node_addr: %pM\n", rxon->node_addr);
3894 IWL_DEBUG_RADIO("u8[6] bssid_addr: %pM\n", rxon->bssid_addr);
Zhu Yib481de92007-09-25 17:54:57 -07003895 IWL_DEBUG_RADIO("u16 assoc_id: 0x%x\n", le16_to_cpu(rxon->assoc_id));
3896}
3897#endif
3898
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003899static void iwl3945_enable_interrupts(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003900{
3901 IWL_DEBUG_ISR("Enabling interrupts\n");
3902 set_bit(STATUS_INT_ENABLED, &priv->status);
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003903 iwl_write32(priv, CSR_INT_MASK, CSR_INI_SET_MASK);
Zhu Yib481de92007-09-25 17:54:57 -07003904}
3905
Mohamed Abbas0359fac2008-03-28 16:21:08 -07003906
3907/* call this function to flush any scheduled tasklet */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003908static inline void iwl_synchronize_irq(struct iwl_priv *priv)
Mohamed Abbas0359fac2008-03-28 16:21:08 -07003909{
Tomas Winklera96a27f2008-10-23 23:48:56 -07003910 /* wait to make sure we flush pending tasklet*/
Mohamed Abbas0359fac2008-03-28 16:21:08 -07003911 synchronize_irq(priv->pci_dev->irq);
3912 tasklet_kill(&priv->irq_tasklet);
3913}
3914
3915
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003916static inline void iwl3945_disable_interrupts(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003917{
3918 clear_bit(STATUS_INT_ENABLED, &priv->status);
3919
3920 /* disable interrupts from uCode/NIC to host */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003921 iwl_write32(priv, CSR_INT_MASK, 0x00000000);
Zhu Yib481de92007-09-25 17:54:57 -07003922
3923 /* acknowledge/clear/reset any interrupts still pending
3924 * from uCode or flow handler (Rx/Tx DMA) */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003925 iwl_write32(priv, CSR_INT, 0xffffffff);
3926 iwl_write32(priv, CSR_FH_INT_STATUS, 0xffffffff);
Zhu Yib481de92007-09-25 17:54:57 -07003927 IWL_DEBUG_ISR("Disabled interrupts\n");
3928}
3929
3930static const char *desc_lookup(int i)
3931{
3932 switch (i) {
3933 case 1:
3934 return "FAIL";
3935 case 2:
3936 return "BAD_PARAM";
3937 case 3:
3938 return "BAD_CHECKSUM";
3939 case 4:
3940 return "NMI_INTERRUPT";
3941 case 5:
3942 return "SYSASSERT";
3943 case 6:
3944 return "FATAL_ERROR";
3945 }
3946
3947 return "UNKNOWN";
3948}
3949
3950#define ERROR_START_OFFSET (1 * sizeof(u32))
3951#define ERROR_ELEM_SIZE (7 * sizeof(u32))
3952
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003953static void iwl3945_dump_nic_error_log(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003954{
3955 u32 i;
3956 u32 desc, time, count, base, data1;
3957 u32 blink1, blink2, ilink1, ilink2;
3958 int rc;
3959
3960 base = le32_to_cpu(priv->card_alive.error_event_table_ptr);
3961
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003962 if (!iwl3945_hw_valid_rtc_data_addr(base)) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08003963 IWL_ERR(priv, "Not valid error log pointer 0x%08X\n", base);
Zhu Yib481de92007-09-25 17:54:57 -07003964 return;
3965 }
3966
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003967 rc = iwl_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003968 if (rc) {
Winkler, Tomas39aadf82008-12-19 10:37:32 +08003969 IWL_WARN(priv, "Can not read from adapter at this time.\n");
Zhu Yib481de92007-09-25 17:54:57 -07003970 return;
3971 }
3972
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003973 count = iwl_read_targ_mem(priv, base);
Zhu Yib481de92007-09-25 17:54:57 -07003974
3975 if (ERROR_START_OFFSET <= count * ERROR_ELEM_SIZE) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08003976 IWL_ERR(priv, "Start IWL Error Log Dump:\n");
3977 IWL_ERR(priv, "Status: 0x%08lX, count: %d\n",
3978 priv->status, count);
Zhu Yib481de92007-09-25 17:54:57 -07003979 }
3980
Winkler, Tomas15b16872008-12-19 10:37:33 +08003981 IWL_ERR(priv, "Desc Time asrtPC blink2 "
Zhu Yib481de92007-09-25 17:54:57 -07003982 "ilink1 nmiPC Line\n");
3983 for (i = ERROR_START_OFFSET;
3984 i < (count * ERROR_ELEM_SIZE) + ERROR_START_OFFSET;
3985 i += ERROR_ELEM_SIZE) {
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003986 desc = iwl_read_targ_mem(priv, base + i);
Zhu Yib481de92007-09-25 17:54:57 -07003987 time =
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003988 iwl_read_targ_mem(priv, base + i + 1 * sizeof(u32));
Zhu Yib481de92007-09-25 17:54:57 -07003989 blink1 =
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003990 iwl_read_targ_mem(priv, base + i + 2 * sizeof(u32));
Zhu Yib481de92007-09-25 17:54:57 -07003991 blink2 =
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003992 iwl_read_targ_mem(priv, base + i + 3 * sizeof(u32));
Zhu Yib481de92007-09-25 17:54:57 -07003993 ilink1 =
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003994 iwl_read_targ_mem(priv, base + i + 4 * sizeof(u32));
Zhu Yib481de92007-09-25 17:54:57 -07003995 ilink2 =
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003996 iwl_read_targ_mem(priv, base + i + 5 * sizeof(u32));
Zhu Yib481de92007-09-25 17:54:57 -07003997 data1 =
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003998 iwl_read_targ_mem(priv, base + i + 6 * sizeof(u32));
Zhu Yib481de92007-09-25 17:54:57 -07003999
Winkler, Tomas15b16872008-12-19 10:37:33 +08004000 IWL_ERR(priv,
4001 "%-13s (#%d) %010u 0x%05X 0x%05X 0x%05X 0x%05X %u\n\n",
4002 desc_lookup(desc), desc, time, blink1, blink2,
4003 ilink1, ilink2, data1);
Zhu Yib481de92007-09-25 17:54:57 -07004004 }
4005
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08004006 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004007
4008}
4009
Ben Cahillf58177b2007-11-29 11:09:43 +08004010#define EVENT_START_OFFSET (6 * sizeof(u32))
Zhu Yib481de92007-09-25 17:54:57 -07004011
4012/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004013 * iwl3945_print_event_log - Dump error event log to syslog
Zhu Yib481de92007-09-25 17:54:57 -07004014 *
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08004015 * NOTE: Must be called with iwl_grab_nic_access() already obtained!
Zhu Yib481de92007-09-25 17:54:57 -07004016 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08004017static void iwl3945_print_event_log(struct iwl_priv *priv, u32 start_idx,
Zhu Yib481de92007-09-25 17:54:57 -07004018 u32 num_events, u32 mode)
4019{
4020 u32 i;
4021 u32 base; /* SRAM byte address of event log header */
4022 u32 event_size; /* 2 u32s, or 3 u32s if timestamp recorded */
4023 u32 ptr; /* SRAM byte address of log data */
4024 u32 ev, time, data; /* event log data */
4025
4026 if (num_events == 0)
4027 return;
4028
4029 base = le32_to_cpu(priv->card_alive.log_event_table_ptr);
4030
4031 if (mode == 0)
4032 event_size = 2 * sizeof(u32);
4033 else
4034 event_size = 3 * sizeof(u32);
4035
4036 ptr = base + EVENT_START_OFFSET + (start_idx * event_size);
4037
4038 /* "time" is actually "data" for mode 0 (no timestamp).
4039 * place event id # at far right for easier visual parsing. */
4040 for (i = 0; i < num_events; i++) {
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08004041 ev = iwl_read_targ_mem(priv, ptr);
Zhu Yib481de92007-09-25 17:54:57 -07004042 ptr += sizeof(u32);
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08004043 time = iwl_read_targ_mem(priv, ptr);
Zhu Yib481de92007-09-25 17:54:57 -07004044 ptr += sizeof(u32);
Winkler, Tomas15b16872008-12-19 10:37:33 +08004045 if (mode == 0) {
4046 /* data, ev */
4047 IWL_ERR(priv, "0x%08x\t%04u\n", time, ev);
4048 } else {
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08004049 data = iwl_read_targ_mem(priv, ptr);
Zhu Yib481de92007-09-25 17:54:57 -07004050 ptr += sizeof(u32);
Winkler, Tomas15b16872008-12-19 10:37:33 +08004051 IWL_ERR(priv, "%010u\t0x%08x\t%04u\n", time, data, ev);
Zhu Yib481de92007-09-25 17:54:57 -07004052 }
4053 }
4054}
4055
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08004056static void iwl3945_dump_nic_event_log(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004057{
4058 int rc;
4059 u32 base; /* SRAM byte address of event log header */
4060 u32 capacity; /* event log capacity in # entries */
4061 u32 mode; /* 0 - no timestamp, 1 - timestamp recorded */
4062 u32 num_wraps; /* # times uCode wrapped to top of log */
4063 u32 next_entry; /* index of next entry to be written by uCode */
4064 u32 size; /* # entries that we'll print */
4065
4066 base = le32_to_cpu(priv->card_alive.log_event_table_ptr);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004067 if (!iwl3945_hw_valid_rtc_data_addr(base)) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08004068 IWL_ERR(priv, "Invalid event log pointer 0x%08X\n", base);
Zhu Yib481de92007-09-25 17:54:57 -07004069 return;
4070 }
4071
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08004072 rc = iwl_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004073 if (rc) {
Winkler, Tomas39aadf82008-12-19 10:37:32 +08004074 IWL_WARN(priv, "Can not read from adapter at this time.\n");
Zhu Yib481de92007-09-25 17:54:57 -07004075 return;
4076 }
4077
4078 /* event log header */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08004079 capacity = iwl_read_targ_mem(priv, base);
4080 mode = iwl_read_targ_mem(priv, base + (1 * sizeof(u32)));
4081 num_wraps = iwl_read_targ_mem(priv, base + (2 * sizeof(u32)));
4082 next_entry = iwl_read_targ_mem(priv, base + (3 * sizeof(u32)));
Zhu Yib481de92007-09-25 17:54:57 -07004083
4084 size = num_wraps ? capacity : next_entry;
4085
4086 /* bail out if nothing in log */
4087 if (size == 0) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08004088 IWL_ERR(priv, "Start IWL Event Log Dump: nothing in log\n");
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08004089 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004090 return;
4091 }
4092
Winkler, Tomas15b16872008-12-19 10:37:33 +08004093 IWL_ERR(priv, "Start IWL Event Log Dump: display count %d, wraps %d\n",
Zhu Yib481de92007-09-25 17:54:57 -07004094 size, num_wraps);
4095
4096 /* if uCode has wrapped back to top of log, start at the oldest entry,
4097 * i.e the next one that uCode would fill. */
4098 if (num_wraps)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004099 iwl3945_print_event_log(priv, next_entry,
Zhu Yib481de92007-09-25 17:54:57 -07004100 capacity - next_entry, mode);
4101
4102 /* (then/else) start at top of log */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004103 iwl3945_print_event_log(priv, 0, next_entry, mode);
Zhu Yib481de92007-09-25 17:54:57 -07004104
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08004105 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004106}
4107
4108/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004109 * iwl3945_irq_handle_error - called for HW or SW error interrupt from card
Zhu Yib481de92007-09-25 17:54:57 -07004110 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08004111static void iwl3945_irq_handle_error(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004112{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004113 /* Set the FW error flag -- cleared on iwl3945_down */
Zhu Yib481de92007-09-25 17:54:57 -07004114 set_bit(STATUS_FW_ERROR, &priv->status);
4115
4116 /* Cancel currently queued command. */
4117 clear_bit(STATUS_HCMD_ACTIVE, &priv->status);
4118
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08004119#ifdef CONFIG_IWL3945_DEBUG
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08004120 if (priv->debug_level & IWL_DL_FW_ERRORS) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004121 iwl3945_dump_nic_error_log(priv);
4122 iwl3945_dump_nic_event_log(priv);
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08004123 iwl3945_print_rx_config_cmd(priv, &priv->staging39_rxon);
Zhu Yib481de92007-09-25 17:54:57 -07004124 }
4125#endif
4126
4127 wake_up_interruptible(&priv->wait_command_queue);
4128
4129 /* Keep the restart process from trying to send host
4130 * commands by clearing the INIT status bit */
4131 clear_bit(STATUS_READY, &priv->status);
4132
4133 if (!test_bit(STATUS_EXIT_PENDING, &priv->status)) {
4134 IWL_DEBUG(IWL_DL_INFO | IWL_DL_FW_ERRORS,
4135 "Restarting adapter due to uCode error.\n");
4136
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004137 if (iwl3945_is_associated(priv)) {
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08004138 memcpy(&priv->recovery39_rxon, &priv->active39_rxon,
4139 sizeof(priv->recovery39_rxon));
Zhu Yib481de92007-09-25 17:54:57 -07004140 priv->error_recovering = 1;
4141 }
4142 queue_work(priv->workqueue, &priv->restart);
4143 }
4144}
4145
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08004146static void iwl3945_error_recovery(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004147{
4148 unsigned long flags;
4149
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08004150 memcpy(&priv->staging39_rxon, &priv->recovery39_rxon,
4151 sizeof(priv->staging39_rxon));
4152 priv->staging39_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004153 iwl3945_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004154
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004155 iwl3945_add_station(priv, priv->bssid, 1, 0);
Zhu Yib481de92007-09-25 17:54:57 -07004156
4157 spin_lock_irqsave(&priv->lock, flags);
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08004158 priv->assoc_id = le16_to_cpu(priv->staging39_rxon.assoc_id);
Zhu Yib481de92007-09-25 17:54:57 -07004159 priv->error_recovering = 0;
4160 spin_unlock_irqrestore(&priv->lock, flags);
4161}
4162
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08004163static void iwl3945_irq_tasklet(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004164{
4165 u32 inta, handled = 0;
4166 u32 inta_fh;
4167 unsigned long flags;
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08004168#ifdef CONFIG_IWL3945_DEBUG
Zhu Yib481de92007-09-25 17:54:57 -07004169 u32 inta_mask;
4170#endif
4171
4172 spin_lock_irqsave(&priv->lock, flags);
4173
4174 /* Ack/clear/reset pending uCode interrupts.
4175 * Note: Some bits in CSR_INT are "OR" of bits in CSR_FH_INT_STATUS,
4176 * and will clear only when CSR_FH_INT_STATUS gets cleared. */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08004177 inta = iwl_read32(priv, CSR_INT);
4178 iwl_write32(priv, CSR_INT, inta);
Zhu Yib481de92007-09-25 17:54:57 -07004179
4180 /* Ack/clear/reset pending flow-handler (DMA) interrupts.
4181 * Any new interrupts that happen after this, either while we're
4182 * in this tasklet, or later, will show up in next ISR/tasklet. */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08004183 inta_fh = iwl_read32(priv, CSR_FH_INT_STATUS);
4184 iwl_write32(priv, CSR_FH_INT_STATUS, inta_fh);
Zhu Yib481de92007-09-25 17:54:57 -07004185
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08004186#ifdef CONFIG_IWL3945_DEBUG
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08004187 if (priv->debug_level & IWL_DL_ISR) {
Ben Cahill9fbab512007-11-29 11:09:47 +08004188 /* just for debug */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08004189 inta_mask = iwl_read32(priv, CSR_INT_MASK);
Zhu Yib481de92007-09-25 17:54:57 -07004190 IWL_DEBUG_ISR("inta 0x%08x, enabled 0x%08x, fh 0x%08x\n",
4191 inta, inta_mask, inta_fh);
4192 }
4193#endif
4194
4195 /* Since CSR_INT and CSR_FH_INT_STATUS reads and clears are not
4196 * atomic, make sure that inta covers all the interrupts that
4197 * we've discovered, even if FH interrupt came in just after
4198 * reading CSR_INT. */
Tomas Winkler6f83eaa2008-03-04 18:09:28 -08004199 if (inta_fh & CSR39_FH_INT_RX_MASK)
Zhu Yib481de92007-09-25 17:54:57 -07004200 inta |= CSR_INT_BIT_FH_RX;
Tomas Winkler6f83eaa2008-03-04 18:09:28 -08004201 if (inta_fh & CSR39_FH_INT_TX_MASK)
Zhu Yib481de92007-09-25 17:54:57 -07004202 inta |= CSR_INT_BIT_FH_TX;
4203
4204 /* Now service all interrupt bits discovered above. */
4205 if (inta & CSR_INT_BIT_HW_ERR) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08004206 IWL_ERR(priv, "Microcode HW error detected. Restarting.\n");
Zhu Yib481de92007-09-25 17:54:57 -07004207
4208 /* Tell the device to stop sending interrupts */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004209 iwl3945_disable_interrupts(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004210
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004211 iwl3945_irq_handle_error(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004212
4213 handled |= CSR_INT_BIT_HW_ERR;
4214
4215 spin_unlock_irqrestore(&priv->lock, flags);
4216
4217 return;
4218 }
4219
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08004220#ifdef CONFIG_IWL3945_DEBUG
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08004221 if (priv->debug_level & (IWL_DL_ISR)) {
Zhu Yib481de92007-09-25 17:54:57 -07004222 /* NIC fires this, but we don't use it, redundant with WAKEUP */
Joonwoo Park25c03d82008-01-23 10:15:20 -08004223 if (inta & CSR_INT_BIT_SCD)
4224 IWL_DEBUG_ISR("Scheduler finished to transmit "
4225 "the frame/frames.\n");
Zhu Yib481de92007-09-25 17:54:57 -07004226
4227 /* Alive notification via Rx interrupt will do the real work */
4228 if (inta & CSR_INT_BIT_ALIVE)
4229 IWL_DEBUG_ISR("Alive interrupt\n");
4230 }
4231#endif
4232 /* Safely ignore these bits for debug checks below */
Joonwoo Park25c03d82008-01-23 10:15:20 -08004233 inta &= ~(CSR_INT_BIT_SCD | CSR_INT_BIT_ALIVE);
Zhu Yib481de92007-09-25 17:54:57 -07004234
Zhu Yib481de92007-09-25 17:54:57 -07004235 /* Error detected by uCode */
4236 if (inta & CSR_INT_BIT_SW_ERR) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08004237 IWL_ERR(priv, "Microcode SW error detected. "
4238 "Restarting 0x%X.\n", inta);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004239 iwl3945_irq_handle_error(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004240 handled |= CSR_INT_BIT_SW_ERR;
4241 }
4242
4243 /* uCode wakes up after power-down sleep */
4244 if (inta & CSR_INT_BIT_WAKEUP) {
4245 IWL_DEBUG_ISR("Wakeup interrupt\n");
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004246 iwl3945_rx_queue_update_write_ptr(priv, &priv->rxq);
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08004247 iwl3945_tx_queue_update_write_ptr(priv, &priv->txq39[0]);
4248 iwl3945_tx_queue_update_write_ptr(priv, &priv->txq39[1]);
4249 iwl3945_tx_queue_update_write_ptr(priv, &priv->txq39[2]);
4250 iwl3945_tx_queue_update_write_ptr(priv, &priv->txq39[3]);
4251 iwl3945_tx_queue_update_write_ptr(priv, &priv->txq39[4]);
4252 iwl3945_tx_queue_update_write_ptr(priv, &priv->txq39[5]);
Zhu Yib481de92007-09-25 17:54:57 -07004253
4254 handled |= CSR_INT_BIT_WAKEUP;
4255 }
4256
4257 /* All uCode command responses, including Tx command responses,
4258 * Rx "responses" (frame-received notification), and other
4259 * notifications from uCode come through here*/
4260 if (inta & (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX)) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004261 iwl3945_rx_handle(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004262 handled |= (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX);
4263 }
4264
4265 if (inta & CSR_INT_BIT_FH_TX) {
4266 IWL_DEBUG_ISR("Tx interrupt\n");
4267
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08004268 iwl_write32(priv, CSR_FH_INT_STATUS, (1 << 6));
4269 if (!iwl_grab_nic_access(priv)) {
4270 iwl_write_direct32(priv, FH39_TCSR_CREDIT
Tomas Winklerbddadf82008-12-19 10:37:01 +08004271 (FH39_SRVC_CHNL), 0x0);
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08004272 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004273 }
4274 handled |= CSR_INT_BIT_FH_TX;
4275 }
4276
4277 if (inta & ~handled)
Winkler, Tomas15b16872008-12-19 10:37:33 +08004278 IWL_ERR(priv, "Unhandled INTA bits 0x%08x\n", inta & ~handled);
Zhu Yib481de92007-09-25 17:54:57 -07004279
4280 if (inta & ~CSR_INI_SET_MASK) {
Winkler, Tomas39aadf82008-12-19 10:37:32 +08004281 IWL_WARN(priv, "Disabled INTA bits 0x%08x were pending\n",
Zhu Yib481de92007-09-25 17:54:57 -07004282 inta & ~CSR_INI_SET_MASK);
Winkler, Tomas39aadf82008-12-19 10:37:32 +08004283 IWL_WARN(priv, " with FH_INT = 0x%08x\n", inta_fh);
Zhu Yib481de92007-09-25 17:54:57 -07004284 }
4285
4286 /* Re-enable all interrupts */
Mohamed Abbas0359fac2008-03-28 16:21:08 -07004287 /* only Re-enable if disabled by irq */
4288 if (test_bit(STATUS_INT_ENABLED, &priv->status))
4289 iwl3945_enable_interrupts(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004290
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08004291#ifdef CONFIG_IWL3945_DEBUG
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08004292 if (priv->debug_level & (IWL_DL_ISR)) {
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08004293 inta = iwl_read32(priv, CSR_INT);
4294 inta_mask = iwl_read32(priv, CSR_INT_MASK);
4295 inta_fh = iwl_read32(priv, CSR_FH_INT_STATUS);
Zhu Yib481de92007-09-25 17:54:57 -07004296 IWL_DEBUG_ISR("End inta 0x%08x, enabled 0x%08x, fh 0x%08x, "
4297 "flags 0x%08lx\n", inta, inta_mask, inta_fh, flags);
4298 }
4299#endif
4300 spin_unlock_irqrestore(&priv->lock, flags);
4301}
4302
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004303static irqreturn_t iwl3945_isr(int irq, void *data)
Zhu Yib481de92007-09-25 17:54:57 -07004304{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08004305 struct iwl_priv *priv = data;
Zhu Yib481de92007-09-25 17:54:57 -07004306 u32 inta, inta_mask;
4307 u32 inta_fh;
4308 if (!priv)
4309 return IRQ_NONE;
4310
4311 spin_lock(&priv->lock);
4312
4313 /* Disable (but don't clear!) interrupts here to avoid
4314 * back-to-back ISRs and sporadic interrupts from our NIC.
4315 * If we have something to service, the tasklet will re-enable ints.
4316 * If we *don't* have something, we'll re-enable before leaving here. */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08004317 inta_mask = iwl_read32(priv, CSR_INT_MASK); /* just for debug */
4318 iwl_write32(priv, CSR_INT_MASK, 0x00000000);
Zhu Yib481de92007-09-25 17:54:57 -07004319
4320 /* Discover which interrupts are active/pending */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08004321 inta = iwl_read32(priv, CSR_INT);
4322 inta_fh = iwl_read32(priv, CSR_FH_INT_STATUS);
Zhu Yib481de92007-09-25 17:54:57 -07004323
4324 /* Ignore interrupt if there's nothing in NIC to service.
4325 * This may be due to IRQ shared with another device,
4326 * or due to sporadic interrupts thrown from our NIC. */
4327 if (!inta && !inta_fh) {
4328 IWL_DEBUG_ISR("Ignore interrupt, inta == 0, inta_fh == 0\n");
4329 goto none;
4330 }
4331
4332 if ((inta == 0xFFFFFFFF) || ((inta & 0xFFFFFFF0) == 0xa5a5a5a0)) {
4333 /* Hardware disappeared */
Winkler, Tomas39aadf82008-12-19 10:37:32 +08004334 IWL_WARN(priv, "HARDWARE GONE?? INTA == 0x%08x\n", inta);
Oliver Neukumcb4da1a2007-11-13 21:10:32 -08004335 goto unplugged;
Zhu Yib481de92007-09-25 17:54:57 -07004336 }
4337
4338 IWL_DEBUG_ISR("ISR inta 0x%08x, enabled 0x%08x, fh 0x%08x\n",
4339 inta, inta_mask, inta_fh);
4340
Joonwoo Park25c03d82008-01-23 10:15:20 -08004341 inta &= ~CSR_INT_BIT_SCD;
4342
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004343 /* iwl3945_irq_tasklet() will service interrupts and re-enable them */
Joonwoo Park25c03d82008-01-23 10:15:20 -08004344 if (likely(inta || inta_fh))
4345 tasklet_schedule(&priv->irq_tasklet);
Oliver Neukumcb4da1a2007-11-13 21:10:32 -08004346unplugged:
Zhu Yib481de92007-09-25 17:54:57 -07004347 spin_unlock(&priv->lock);
4348
4349 return IRQ_HANDLED;
4350
4351 none:
4352 /* re-enable interrupts here since we don't have anything to service. */
Mohamed Abbas0359fac2008-03-28 16:21:08 -07004353 /* only Re-enable if disabled by irq */
4354 if (test_bit(STATUS_INT_ENABLED, &priv->status))
4355 iwl3945_enable_interrupts(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004356 spin_unlock(&priv->lock);
4357 return IRQ_NONE;
4358}
4359
4360/************************** EEPROM BANDS ****************************
4361 *
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004362 * The iwl3945_eeprom_band definitions below provide the mapping from the
Zhu Yib481de92007-09-25 17:54:57 -07004363 * EEPROM contents to the specific channel number supported for each
4364 * band.
4365 *
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08004366 * For example, iwl3945_priv->eeprom39.band_3_channels[4] from the band_3
Zhu Yib481de92007-09-25 17:54:57 -07004367 * definition below maps to physical channel 42 in the 5.2GHz spectrum.
4368 * The specific geography and calibration information for that channel
4369 * is contained in the eeprom map itself.
4370 *
4371 * During init, we copy the eeprom information and channel map
4372 * information into priv->channel_info_24/52 and priv->channel_map_24/52
4373 *
4374 * channel_map_24/52 provides the index in the channel_info array for a
4375 * given channel. We have to have two separate maps as there is channel
4376 * overlap with the 2.4GHz and 5.2GHz spectrum as seen in band_1 and
4377 * band_2
4378 *
4379 * A value of 0xff stored in the channel_map indicates that the channel
4380 * is not supported by the hardware at all.
4381 *
4382 * A value of 0xfe in the channel_map indicates that the channel is not
4383 * valid for Tx with the current hardware. This means that
4384 * while the system can tune and receive on a given channel, it may not
4385 * be able to associate or transmit any frames on that
4386 * channel. There is no corresponding channel information for that
4387 * entry.
4388 *
4389 *********************************************************************/
4390
4391/* 2.4 GHz */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004392static const u8 iwl3945_eeprom_band_1[14] = {
Zhu Yib481de92007-09-25 17:54:57 -07004393 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14
4394};
4395
4396/* 5.2 GHz bands */
Ben Cahill9fbab512007-11-29 11:09:47 +08004397static const u8 iwl3945_eeprom_band_2[] = { /* 4915-5080MHz */
Zhu Yib481de92007-09-25 17:54:57 -07004398 183, 184, 185, 187, 188, 189, 192, 196, 7, 8, 11, 12, 16
4399};
4400
Ben Cahill9fbab512007-11-29 11:09:47 +08004401static const u8 iwl3945_eeprom_band_3[] = { /* 5170-5320MHz */
Zhu Yib481de92007-09-25 17:54:57 -07004402 34, 36, 38, 40, 42, 44, 46, 48, 52, 56, 60, 64
4403};
4404
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004405static const u8 iwl3945_eeprom_band_4[] = { /* 5500-5700MHz */
Zhu Yib481de92007-09-25 17:54:57 -07004406 100, 104, 108, 112, 116, 120, 124, 128, 132, 136, 140
4407};
4408
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004409static const u8 iwl3945_eeprom_band_5[] = { /* 5725-5825MHz */
Zhu Yib481de92007-09-25 17:54:57 -07004410 145, 149, 153, 157, 161, 165
4411};
4412
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08004413static void iwl3945_init_band_reference(const struct iwl_priv *priv, int band,
Zhu Yib481de92007-09-25 17:54:57 -07004414 int *eeprom_ch_count,
Samuel Ortiz0f741d92008-12-19 10:37:10 +08004415 const struct iwl_eeprom_channel
Zhu Yib481de92007-09-25 17:54:57 -07004416 **eeprom_ch_info,
4417 const u8 **eeprom_ch_index)
4418{
4419 switch (band) {
4420 case 1: /* 2.4GHz band */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004421 *eeprom_ch_count = ARRAY_SIZE(iwl3945_eeprom_band_1);
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08004422 *eeprom_ch_info = priv->eeprom39.band_1_channels;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004423 *eeprom_ch_index = iwl3945_eeprom_band_1;
Zhu Yib481de92007-09-25 17:54:57 -07004424 break;
Ben Cahill9fbab512007-11-29 11:09:47 +08004425 case 2: /* 4.9GHz band */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004426 *eeprom_ch_count = ARRAY_SIZE(iwl3945_eeprom_band_2);
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08004427 *eeprom_ch_info = priv->eeprom39.band_2_channels;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004428 *eeprom_ch_index = iwl3945_eeprom_band_2;
Zhu Yib481de92007-09-25 17:54:57 -07004429 break;
4430 case 3: /* 5.2GHz band */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004431 *eeprom_ch_count = ARRAY_SIZE(iwl3945_eeprom_band_3);
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08004432 *eeprom_ch_info = priv->eeprom39.band_3_channels;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004433 *eeprom_ch_index = iwl3945_eeprom_band_3;
Zhu Yib481de92007-09-25 17:54:57 -07004434 break;
Ben Cahill9fbab512007-11-29 11:09:47 +08004435 case 4: /* 5.5GHz band */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004436 *eeprom_ch_count = ARRAY_SIZE(iwl3945_eeprom_band_4);
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08004437 *eeprom_ch_info = priv->eeprom39.band_4_channels;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004438 *eeprom_ch_index = iwl3945_eeprom_band_4;
Zhu Yib481de92007-09-25 17:54:57 -07004439 break;
Ben Cahill9fbab512007-11-29 11:09:47 +08004440 case 5: /* 5.7GHz band */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004441 *eeprom_ch_count = ARRAY_SIZE(iwl3945_eeprom_band_5);
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08004442 *eeprom_ch_info = priv->eeprom39.band_5_channels;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004443 *eeprom_ch_index = iwl3945_eeprom_band_5;
Zhu Yib481de92007-09-25 17:54:57 -07004444 break;
4445 default:
4446 BUG();
4447 return;
4448 }
4449}
4450
Cahill, Ben M6440adb2007-11-29 11:09:55 +08004451/**
4452 * iwl3945_get_channel_info - Find driver's private channel info
4453 *
4454 * Based on band and channel number.
4455 */
Samuel Ortizd20b3c62008-12-19 10:37:15 +08004456const struct iwl_channel_info *
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08004457iwl3945_get_channel_info(const struct iwl_priv *priv,
Samuel Ortizd20b3c62008-12-19 10:37:15 +08004458 enum ieee80211_band band, u16 channel)
Zhu Yib481de92007-09-25 17:54:57 -07004459{
4460 int i;
4461
Johannes Berg8318d782008-01-24 19:38:38 +01004462 switch (band) {
4463 case IEEE80211_BAND_5GHZ:
Zhu Yib481de92007-09-25 17:54:57 -07004464 for (i = 14; i < priv->channel_count; i++) {
4465 if (priv->channel_info[i].channel == channel)
4466 return &priv->channel_info[i];
4467 }
4468 break;
4469
Johannes Berg8318d782008-01-24 19:38:38 +01004470 case IEEE80211_BAND_2GHZ:
Zhu Yib481de92007-09-25 17:54:57 -07004471 if (channel >= 1 && channel <= 14)
4472 return &priv->channel_info[channel - 1];
4473 break;
Johannes Berg8318d782008-01-24 19:38:38 +01004474 case IEEE80211_NUM_BANDS:
4475 WARN_ON(1);
Zhu Yib481de92007-09-25 17:54:57 -07004476 }
4477
4478 return NULL;
4479}
4480
4481#define CHECK_AND_PRINT(x) ((eeprom_ch_info[ch].flags & EEPROM_CHANNEL_##x) \
4482 ? # x " " : "")
4483
Cahill, Ben M6440adb2007-11-29 11:09:55 +08004484/**
4485 * iwl3945_init_channel_map - Set up driver's info for all possible channels
4486 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08004487static int iwl3945_init_channel_map(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004488{
4489 int eeprom_ch_count = 0;
4490 const u8 *eeprom_ch_index = NULL;
Samuel Ortiz0f741d92008-12-19 10:37:10 +08004491 const struct iwl_eeprom_channel *eeprom_ch_info = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07004492 int band, ch;
Samuel Ortizd20b3c62008-12-19 10:37:15 +08004493 struct iwl_channel_info *ch_info;
Zhu Yib481de92007-09-25 17:54:57 -07004494
4495 if (priv->channel_count) {
4496 IWL_DEBUG_INFO("Channel map already initialized.\n");
4497 return 0;
4498 }
4499
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08004500 if (priv->eeprom39.version < 0x2f) {
Winkler, Tomas39aadf82008-12-19 10:37:32 +08004501 IWL_WARN(priv, "Unsupported EEPROM version: 0x%04X\n",
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08004502 priv->eeprom39.version);
Zhu Yib481de92007-09-25 17:54:57 -07004503 return -EINVAL;
4504 }
4505
4506 IWL_DEBUG_INFO("Initializing regulatory info from EEPROM\n");
4507
4508 priv->channel_count =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004509 ARRAY_SIZE(iwl3945_eeprom_band_1) +
4510 ARRAY_SIZE(iwl3945_eeprom_band_2) +
4511 ARRAY_SIZE(iwl3945_eeprom_band_3) +
4512 ARRAY_SIZE(iwl3945_eeprom_band_4) +
4513 ARRAY_SIZE(iwl3945_eeprom_band_5);
Zhu Yib481de92007-09-25 17:54:57 -07004514
4515 IWL_DEBUG_INFO("Parsing data for %d channels.\n", priv->channel_count);
4516
Samuel Ortizd20b3c62008-12-19 10:37:15 +08004517 priv->channel_info = kzalloc(sizeof(struct iwl_channel_info) *
Zhu Yib481de92007-09-25 17:54:57 -07004518 priv->channel_count, GFP_KERNEL);
4519 if (!priv->channel_info) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08004520 IWL_ERR(priv, "Could not allocate channel_info\n");
Zhu Yib481de92007-09-25 17:54:57 -07004521 priv->channel_count = 0;
4522 return -ENOMEM;
4523 }
4524
4525 ch_info = priv->channel_info;
4526
4527 /* Loop through the 5 EEPROM bands adding them in order to the
4528 * channel map we maintain (that contains additional information than
4529 * what just in the EEPROM) */
4530 for (band = 1; band <= 5; band++) {
4531
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004532 iwl3945_init_band_reference(priv, band, &eeprom_ch_count,
Zhu Yib481de92007-09-25 17:54:57 -07004533 &eeprom_ch_info, &eeprom_ch_index);
4534
4535 /* Loop through each band adding each of the channels */
4536 for (ch = 0; ch < eeprom_ch_count; ch++) {
4537 ch_info->channel = eeprom_ch_index[ch];
Johannes Berg8318d782008-01-24 19:38:38 +01004538 ch_info->band = (band == 1) ? IEEE80211_BAND_2GHZ :
4539 IEEE80211_BAND_5GHZ;
Zhu Yib481de92007-09-25 17:54:57 -07004540
4541 /* permanently store EEPROM's channel regulatory flags
4542 * and max power in channel info database. */
4543 ch_info->eeprom = eeprom_ch_info[ch];
4544
4545 /* Copy the run-time flags so they are there even on
4546 * invalid channels */
4547 ch_info->flags = eeprom_ch_info[ch].flags;
4548
4549 if (!(is_channel_valid(ch_info))) {
4550 IWL_DEBUG_INFO("Ch. %d Flags %x [%sGHz] - "
4551 "No traffic\n",
4552 ch_info->channel,
4553 ch_info->flags,
4554 is_channel_a_band(ch_info) ?
4555 "5.2" : "2.4");
4556 ch_info++;
4557 continue;
4558 }
4559
4560 /* Initialize regulatory-based run-time data */
4561 ch_info->max_power_avg = ch_info->curr_txpow =
4562 eeprom_ch_info[ch].max_power_avg;
4563 ch_info->scan_power = eeprom_ch_info[ch].max_power_avg;
4564 ch_info->min_power = 0;
4565
Guy Cohenfe7c4042008-04-21 15:41:56 -07004566 IWL_DEBUG_INFO("Ch. %d [%sGHz] %s%s%s%s%s%s(0x%02x"
Zhu Yib481de92007-09-25 17:54:57 -07004567 " %ddBm): Ad-Hoc %ssupported\n",
4568 ch_info->channel,
4569 is_channel_a_band(ch_info) ?
4570 "5.2" : "2.4",
Tomas Winkler8211ef72008-03-02 01:36:04 +02004571 CHECK_AND_PRINT(VALID),
Zhu Yib481de92007-09-25 17:54:57 -07004572 CHECK_AND_PRINT(IBSS),
4573 CHECK_AND_PRINT(ACTIVE),
4574 CHECK_AND_PRINT(RADAR),
4575 CHECK_AND_PRINT(WIDE),
Zhu Yib481de92007-09-25 17:54:57 -07004576 CHECK_AND_PRINT(DFS),
4577 eeprom_ch_info[ch].flags,
4578 eeprom_ch_info[ch].max_power_avg,
4579 ((eeprom_ch_info[ch].
4580 flags & EEPROM_CHANNEL_IBSS)
4581 && !(eeprom_ch_info[ch].
4582 flags & EEPROM_CHANNEL_RADAR))
4583 ? "" : "not ");
4584
4585 /* Set the user_txpower_limit to the highest power
4586 * supported by any channel */
4587 if (eeprom_ch_info[ch].max_power_avg >
4588 priv->user_txpower_limit)
4589 priv->user_txpower_limit =
4590 eeprom_ch_info[ch].max_power_avg;
4591
4592 ch_info++;
4593 }
4594 }
4595
Cahill, Ben M6440adb2007-11-29 11:09:55 +08004596 /* Set up txpower settings in driver for all channels */
Zhu Yib481de92007-09-25 17:54:57 -07004597 if (iwl3945_txpower_set_from_eeprom(priv))
4598 return -EIO;
4599
4600 return 0;
4601}
4602
Reinette Chatre849e0dc2008-01-23 10:15:18 -08004603/*
4604 * iwl3945_free_channel_map - undo allocations in iwl3945_init_channel_map
4605 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08004606static void iwl3945_free_channel_map(struct iwl_priv *priv)
Reinette Chatre849e0dc2008-01-23 10:15:18 -08004607{
4608 kfree(priv->channel_info);
4609 priv->channel_count = 0;
4610}
4611
Zhu Yib481de92007-09-25 17:54:57 -07004612/* For active scan, listen ACTIVE_DWELL_TIME (msec) on each channel after
4613 * sending probe req. This should be set long enough to hear probe responses
4614 * from more than one AP. */
Abhijeet Kolekarf9340522008-09-03 11:26:58 +08004615#define IWL_ACTIVE_DWELL_TIME_24 (30) /* all times in msec */
4616#define IWL_ACTIVE_DWELL_TIME_52 (20)
4617
4618#define IWL_ACTIVE_DWELL_FACTOR_24GHZ (3)
4619#define IWL_ACTIVE_DWELL_FACTOR_52GHZ (2)
Zhu Yib481de92007-09-25 17:54:57 -07004620
4621/* For faster active scanning, scan will move to the next channel if fewer than
4622 * PLCP_QUIET_THRESH packets are heard on this channel within
4623 * ACTIVE_QUIET_TIME after sending probe request. This shortens the dwell
4624 * time if it's a quiet channel (nothing responded to our probe, and there's
4625 * no other traffic).
4626 * Disable "quiet" feature by setting PLCP_QUIET_THRESH to 0. */
4627#define IWL_PLCP_QUIET_THRESH __constant_cpu_to_le16(1) /* packets */
Abhijeet Kolekarf9340522008-09-03 11:26:58 +08004628#define IWL_ACTIVE_QUIET_TIME __constant_cpu_to_le16(10) /* msec */
Zhu Yib481de92007-09-25 17:54:57 -07004629
4630/* For passive scan, listen PASSIVE_DWELL_TIME (msec) on each channel.
4631 * Must be set longer than active dwell time.
4632 * For the most reliable scan, set > AP beacon interval (typically 100msec). */
4633#define IWL_PASSIVE_DWELL_TIME_24 (20) /* all times in msec */
4634#define IWL_PASSIVE_DWELL_TIME_52 (10)
4635#define IWL_PASSIVE_DWELL_BASE (100)
4636#define IWL_CHANNEL_TUNE_TIME 5
4637
Kolekar, Abhijeete720ce92008-11-07 09:58:42 -08004638#define IWL_SCAN_PROBE_MASK(n) (BIT(n) | (BIT(n) - BIT(1)))
Abhijeet Kolekarf9340522008-09-03 11:26:58 +08004639
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08004640static inline u16 iwl3945_get_active_dwell_time(struct iwl_priv *priv,
Abhijeet Kolekarf9340522008-09-03 11:26:58 +08004641 enum ieee80211_band band,
4642 u8 n_probes)
Zhu Yib481de92007-09-25 17:54:57 -07004643{
Johannes Berg8318d782008-01-24 19:38:38 +01004644 if (band == IEEE80211_BAND_5GHZ)
Abhijeet Kolekarf9340522008-09-03 11:26:58 +08004645 return IWL_ACTIVE_DWELL_TIME_52 +
4646 IWL_ACTIVE_DWELL_FACTOR_52GHZ * (n_probes + 1);
Zhu Yib481de92007-09-25 17:54:57 -07004647 else
Abhijeet Kolekarf9340522008-09-03 11:26:58 +08004648 return IWL_ACTIVE_DWELL_TIME_24 +
4649 IWL_ACTIVE_DWELL_FACTOR_24GHZ * (n_probes + 1);
Zhu Yib481de92007-09-25 17:54:57 -07004650}
4651
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08004652static u16 iwl3945_get_passive_dwell_time(struct iwl_priv *priv,
Johannes Berg8318d782008-01-24 19:38:38 +01004653 enum ieee80211_band band)
Zhu Yib481de92007-09-25 17:54:57 -07004654{
Johannes Berg8318d782008-01-24 19:38:38 +01004655 u16 passive = (band == IEEE80211_BAND_2GHZ) ?
Zhu Yib481de92007-09-25 17:54:57 -07004656 IWL_PASSIVE_DWELL_BASE + IWL_PASSIVE_DWELL_TIME_24 :
4657 IWL_PASSIVE_DWELL_BASE + IWL_PASSIVE_DWELL_TIME_52;
4658
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004659 if (iwl3945_is_associated(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07004660 /* If we're associated, we clamp the maximum passive
4661 * dwell time to be 98% of the beacon interval (minus
4662 * 2 * channel tune time) */
4663 passive = priv->beacon_int;
4664 if ((passive > IWL_PASSIVE_DWELL_BASE) || !passive)
4665 passive = IWL_PASSIVE_DWELL_BASE;
4666 passive = (passive * 98) / 100 - IWL_CHANNEL_TUNE_TIME * 2;
4667 }
4668
Zhu Yib481de92007-09-25 17:54:57 -07004669 return passive;
4670}
4671
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08004672static int iwl3945_get_channels_for_scan(struct iwl_priv *priv,
Johannes Berg8318d782008-01-24 19:38:38 +01004673 enum ieee80211_band band,
Abhijeet Kolekarf9340522008-09-03 11:26:58 +08004674 u8 is_active, u8 n_probes,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004675 struct iwl3945_scan_channel *scan_ch)
Zhu Yib481de92007-09-25 17:54:57 -07004676{
4677 const struct ieee80211_channel *channels = NULL;
Johannes Berg8318d782008-01-24 19:38:38 +01004678 const struct ieee80211_supported_band *sband;
Samuel Ortizd20b3c62008-12-19 10:37:15 +08004679 const struct iwl_channel_info *ch_info;
Zhu Yib481de92007-09-25 17:54:57 -07004680 u16 passive_dwell = 0;
4681 u16 active_dwell = 0;
4682 int added, i;
4683
Johannes Berg8318d782008-01-24 19:38:38 +01004684 sband = iwl3945_get_band(priv, band);
4685 if (!sband)
Zhu Yib481de92007-09-25 17:54:57 -07004686 return 0;
4687
Johannes Berg8318d782008-01-24 19:38:38 +01004688 channels = sband->channels;
Zhu Yib481de92007-09-25 17:54:57 -07004689
Abhijeet Kolekarf9340522008-09-03 11:26:58 +08004690 active_dwell = iwl3945_get_active_dwell_time(priv, band, n_probes);
Johannes Berg8318d782008-01-24 19:38:38 +01004691 passive_dwell = iwl3945_get_passive_dwell_time(priv, band);
Zhu Yib481de92007-09-25 17:54:57 -07004692
Abhijeet Kolekar8f4807a2008-09-03 11:26:31 +08004693 if (passive_dwell <= active_dwell)
4694 passive_dwell = active_dwell + 1;
4695
Johannes Berg8318d782008-01-24 19:38:38 +01004696 for (i = 0, added = 0; i < sband->n_channels; i++) {
Johannes Berg182e2e62008-04-04 10:41:56 +02004697 if (channels[i].flags & IEEE80211_CHAN_DISABLED)
4698 continue;
4699
Johannes Berg8318d782008-01-24 19:38:38 +01004700 scan_ch->channel = channels[i].hw_value;
Zhu Yib481de92007-09-25 17:54:57 -07004701
Johannes Berg8318d782008-01-24 19:38:38 +01004702 ch_info = iwl3945_get_channel_info(priv, band, scan_ch->channel);
Zhu Yib481de92007-09-25 17:54:57 -07004703 if (!is_channel_valid(ch_info)) {
Ron Rindjunsky66b50042008-06-25 16:46:31 +08004704 IWL_DEBUG_SCAN("Channel %d is INVALID for this band.\n",
Zhu Yib481de92007-09-25 17:54:57 -07004705 scan_ch->channel);
4706 continue;
4707 }
4708
Zhu Yib481de92007-09-25 17:54:57 -07004709 scan_ch->active_dwell = cpu_to_le16(active_dwell);
4710 scan_ch->passive_dwell = cpu_to_le16(passive_dwell);
Abhijeet Kolekar011a0332008-12-02 12:14:07 -08004711 /* If passive , set up for auto-switch
4712 * and use long active_dwell time.
4713 */
4714 if (!is_active || is_channel_passive(ch_info) ||
4715 (channels[i].flags & IEEE80211_CHAN_PASSIVE_SCAN)) {
4716 scan_ch->type = 0; /* passive */
4717 if (IWL_UCODE_API(priv->ucode_ver) == 1)
4718 scan_ch->active_dwell = cpu_to_le16(passive_dwell - 1);
4719 } else {
4720 scan_ch->type = 1; /* active */
4721 }
4722
4723 /* Set direct probe bits. These may be used both for active
4724 * scan channels (probes gets sent right away),
4725 * or for passive channels (probes get se sent only after
4726 * hearing clear Rx packet).*/
4727 if (IWL_UCODE_API(priv->ucode_ver) >= 2) {
4728 if (n_probes)
4729 scan_ch->type |= IWL_SCAN_PROBE_MASK(n_probes);
4730 } else {
4731 /* uCode v1 does not allow setting direct probe bits on
4732 * passive channel. */
4733 if ((scan_ch->type & 1) && n_probes)
4734 scan_ch->type |= IWL_SCAN_PROBE_MASK(n_probes);
4735 }
Zhu Yib481de92007-09-25 17:54:57 -07004736
Ben Cahill9fbab512007-11-29 11:09:47 +08004737 /* Set txpower levels to defaults */
Zhu Yib481de92007-09-25 17:54:57 -07004738 scan_ch->tpc.dsp_atten = 110;
4739 /* scan_pwr_info->tpc.dsp_atten; */
4740
4741 /*scan_pwr_info->tpc.tx_gain; */
Johannes Berg8318d782008-01-24 19:38:38 +01004742 if (band == IEEE80211_BAND_5GHZ)
Zhu Yib481de92007-09-25 17:54:57 -07004743 scan_ch->tpc.tx_gain = ((1 << 5) | (3 << 3)) | 3;
4744 else {
4745 scan_ch->tpc.tx_gain = ((1 << 5) | (5 << 3));
4746 /* NOTE: if we were doing 6Mb OFDM for scans we'd use
Ben Cahill9fbab512007-11-29 11:09:47 +08004747 * power level:
Reinette Chatre8a1b0242008-01-14 17:46:25 -08004748 * scan_ch->tpc.tx_gain = ((1 << 5) | (2 << 3)) | 3;
Zhu Yib481de92007-09-25 17:54:57 -07004749 */
4750 }
4751
4752 IWL_DEBUG_SCAN("Scanning %d [%s %d]\n",
4753 scan_ch->channel,
4754 (scan_ch->type & 1) ? "ACTIVE" : "PASSIVE",
4755 (scan_ch->type & 1) ?
4756 active_dwell : passive_dwell);
4757
4758 scan_ch++;
4759 added++;
4760 }
4761
4762 IWL_DEBUG_SCAN("total channels to scan %d \n", added);
4763 return added;
4764}
4765
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08004766static void iwl3945_init_hw_rates(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07004767 struct ieee80211_rate *rates)
4768{
4769 int i;
4770
4771 for (i = 0; i < IWL_RATE_COUNT; i++) {
Johannes Berg8318d782008-01-24 19:38:38 +01004772 rates[i].bitrate = iwl3945_rates[i].ieee * 5;
4773 rates[i].hw_value = i; /* Rate scaling will work on indexes */
4774 rates[i].hw_value_short = i;
4775 rates[i].flags = 0;
Samuel Ortizd9829a62008-12-19 10:37:12 +08004776 if ((i > IWL39_LAST_OFDM_RATE) || (i < IWL_FIRST_OFDM_RATE)) {
Zhu Yib481de92007-09-25 17:54:57 -07004777 /*
Johannes Berg8318d782008-01-24 19:38:38 +01004778 * If CCK != 1M then set short preamble rate flag.
Zhu Yib481de92007-09-25 17:54:57 -07004779 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004780 rates[i].flags |= (iwl3945_rates[i].plcp == 10) ?
Johannes Berg8318d782008-01-24 19:38:38 +01004781 0 : IEEE80211_RATE_SHORT_PREAMBLE;
Zhu Yib481de92007-09-25 17:54:57 -07004782 }
Zhu Yib481de92007-09-25 17:54:57 -07004783 }
4784}
4785
4786/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004787 * iwl3945_init_geos - Initialize mac80211's geo/channel info based from eeprom
Zhu Yib481de92007-09-25 17:54:57 -07004788 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08004789static int iwl3945_init_geos(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004790{
Samuel Ortizd20b3c62008-12-19 10:37:15 +08004791 struct iwl_channel_info *ch;
Tomas Winkler8211ef72008-03-02 01:36:04 +02004792 struct ieee80211_supported_band *sband;
Zhu Yib481de92007-09-25 17:54:57 -07004793 struct ieee80211_channel *channels;
4794 struct ieee80211_channel *geo_ch;
4795 struct ieee80211_rate *rates;
4796 int i = 0;
Zhu Yib481de92007-09-25 17:54:57 -07004797
Johannes Berg8318d782008-01-24 19:38:38 +01004798 if (priv->bands[IEEE80211_BAND_2GHZ].n_bitrates ||
4799 priv->bands[IEEE80211_BAND_5GHZ].n_bitrates) {
Zhu Yib481de92007-09-25 17:54:57 -07004800 IWL_DEBUG_INFO("Geography modes already initialized.\n");
4801 set_bit(STATUS_GEO_CONFIGURED, &priv->status);
4802 return 0;
4803 }
4804
Zhu Yib481de92007-09-25 17:54:57 -07004805 channels = kzalloc(sizeof(struct ieee80211_channel) *
4806 priv->channel_count, GFP_KERNEL);
Johannes Berg8318d782008-01-24 19:38:38 +01004807 if (!channels)
Zhu Yib481de92007-09-25 17:54:57 -07004808 return -ENOMEM;
Zhu Yib481de92007-09-25 17:54:57 -07004809
Tomas Winkler8211ef72008-03-02 01:36:04 +02004810 rates = kzalloc((sizeof(struct ieee80211_rate) * (IWL_RATE_COUNT + 1)),
Zhu Yib481de92007-09-25 17:54:57 -07004811 GFP_KERNEL);
4812 if (!rates) {
Zhu Yib481de92007-09-25 17:54:57 -07004813 kfree(channels);
4814 return -ENOMEM;
4815 }
4816
Zhu Yib481de92007-09-25 17:54:57 -07004817 /* 5.2GHz channels start after the 2.4GHz channels */
Tomas Winkler8211ef72008-03-02 01:36:04 +02004818 sband = &priv->bands[IEEE80211_BAND_5GHZ];
4819 sband->channels = &channels[ARRAY_SIZE(iwl3945_eeprom_band_1)];
4820 /* just OFDM */
4821 sband->bitrates = &rates[IWL_FIRST_OFDM_RATE];
4822 sband->n_bitrates = IWL_RATE_COUNT - IWL_FIRST_OFDM_RATE;
Zhu Yib481de92007-09-25 17:54:57 -07004823
Tomas Winkler8211ef72008-03-02 01:36:04 +02004824 sband = &priv->bands[IEEE80211_BAND_2GHZ];
4825 sband->channels = channels;
4826 /* OFDM & CCK */
4827 sband->bitrates = rates;
4828 sband->n_bitrates = IWL_RATE_COUNT;
Zhu Yib481de92007-09-25 17:54:57 -07004829
4830 priv->ieee_channels = channels;
4831 priv->ieee_rates = rates;
4832
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004833 iwl3945_init_hw_rates(priv, rates);
Zhu Yib481de92007-09-25 17:54:57 -07004834
Tomas Winkler8211ef72008-03-02 01:36:04 +02004835 for (i = 0; i < priv->channel_count; i++) {
Zhu Yib481de92007-09-25 17:54:57 -07004836 ch = &priv->channel_info[i];
4837
Tomas Winkler8211ef72008-03-02 01:36:04 +02004838 /* FIXME: might be removed if scan is OK*/
4839 if (!is_channel_valid(ch))
Zhu Yib481de92007-09-25 17:54:57 -07004840 continue;
Zhu Yib481de92007-09-25 17:54:57 -07004841
4842 if (is_channel_a_band(ch))
Tomas Winkler8211ef72008-03-02 01:36:04 +02004843 sband = &priv->bands[IEEE80211_BAND_5GHZ];
Johannes Berg8318d782008-01-24 19:38:38 +01004844 else
Tomas Winkler8211ef72008-03-02 01:36:04 +02004845 sband = &priv->bands[IEEE80211_BAND_2GHZ];
Zhu Yib481de92007-09-25 17:54:57 -07004846
Tomas Winkler8211ef72008-03-02 01:36:04 +02004847 geo_ch = &sband->channels[sband->n_channels++];
4848
4849 geo_ch->center_freq = ieee80211_channel_to_frequency(ch->channel);
Johannes Berg8318d782008-01-24 19:38:38 +01004850 geo_ch->max_power = ch->max_power_avg;
4851 geo_ch->max_antenna_gain = 0xff;
Mohamed Abbas7b723042008-01-31 21:46:40 -08004852 geo_ch->hw_value = ch->channel;
Zhu Yib481de92007-09-25 17:54:57 -07004853
4854 if (is_channel_valid(ch)) {
Johannes Berg8318d782008-01-24 19:38:38 +01004855 if (!(ch->flags & EEPROM_CHANNEL_IBSS))
4856 geo_ch->flags |= IEEE80211_CHAN_NO_IBSS;
Zhu Yib481de92007-09-25 17:54:57 -07004857
Johannes Berg8318d782008-01-24 19:38:38 +01004858 if (!(ch->flags & EEPROM_CHANNEL_ACTIVE))
4859 geo_ch->flags |= IEEE80211_CHAN_PASSIVE_SCAN;
Zhu Yib481de92007-09-25 17:54:57 -07004860
4861 if (ch->flags & EEPROM_CHANNEL_RADAR)
Johannes Berg8318d782008-01-24 19:38:38 +01004862 geo_ch->flags |= IEEE80211_CHAN_RADAR;
Zhu Yib481de92007-09-25 17:54:57 -07004863
4864 if (ch->max_power_avg > priv->max_channel_txpower_limit)
4865 priv->max_channel_txpower_limit =
4866 ch->max_power_avg;
Tomas Winkler8211ef72008-03-02 01:36:04 +02004867 } else {
Johannes Berg8318d782008-01-24 19:38:38 +01004868 geo_ch->flags |= IEEE80211_CHAN_DISABLED;
Tomas Winkler8211ef72008-03-02 01:36:04 +02004869 }
4870
4871 /* Save flags for reg domain usage */
4872 geo_ch->orig_flags = geo_ch->flags;
4873
4874 IWL_DEBUG_INFO("Channel %d Freq=%d[%sGHz] %s flag=0%X\n",
4875 ch->channel, geo_ch->center_freq,
4876 is_channel_a_band(ch) ? "5.2" : "2.4",
4877 geo_ch->flags & IEEE80211_CHAN_DISABLED ?
4878 "restricted" : "valid",
4879 geo_ch->flags);
Zhu Yib481de92007-09-25 17:54:57 -07004880 }
4881
Tomas Winkler82b9a122008-03-04 18:09:30 -08004882 if ((priv->bands[IEEE80211_BAND_5GHZ].n_channels == 0) &&
4883 priv->cfg->sku & IWL_SKU_A) {
Tomas Winkler978785a2008-12-19 10:37:31 +08004884 IWL_INFO(priv, "Incorrectly detected BG card as ABG. "
4885 "Please send your PCI ID 0x%04X:0x%04X to maintainer.\n",
4886 priv->pci_dev->device, priv->pci_dev->subsystem_device);
Tomas Winkler82b9a122008-03-04 18:09:30 -08004887 priv->cfg->sku &= ~IWL_SKU_A;
Zhu Yib481de92007-09-25 17:54:57 -07004888 }
4889
Tomas Winkler978785a2008-12-19 10:37:31 +08004890 IWL_INFO(priv, "Tunable channels: %d 802.11bg, %d 802.11a channels\n",
Johannes Berg8318d782008-01-24 19:38:38 +01004891 priv->bands[IEEE80211_BAND_2GHZ].n_channels,
4892 priv->bands[IEEE80211_BAND_5GHZ].n_channels);
Zhu Yib481de92007-09-25 17:54:57 -07004893
John W. Linvillee0e0a672008-03-25 15:58:40 -04004894 if (priv->bands[IEEE80211_BAND_2GHZ].n_channels)
4895 priv->hw->wiphy->bands[IEEE80211_BAND_2GHZ] =
4896 &priv->bands[IEEE80211_BAND_2GHZ];
4897 if (priv->bands[IEEE80211_BAND_5GHZ].n_channels)
4898 priv->hw->wiphy->bands[IEEE80211_BAND_5GHZ] =
4899 &priv->bands[IEEE80211_BAND_5GHZ];
Zhu Yib481de92007-09-25 17:54:57 -07004900
Zhu Yib481de92007-09-25 17:54:57 -07004901 set_bit(STATUS_GEO_CONFIGURED, &priv->status);
4902
4903 return 0;
4904}
4905
Reinette Chatre849e0dc2008-01-23 10:15:18 -08004906/*
4907 * iwl3945_free_geos - undo allocations in iwl3945_init_geos
4908 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08004909static void iwl3945_free_geos(struct iwl_priv *priv)
Reinette Chatre849e0dc2008-01-23 10:15:18 -08004910{
Reinette Chatre849e0dc2008-01-23 10:15:18 -08004911 kfree(priv->ieee_channels);
4912 kfree(priv->ieee_rates);
4913 clear_bit(STATUS_GEO_CONFIGURED, &priv->status);
4914}
4915
Zhu Yib481de92007-09-25 17:54:57 -07004916/******************************************************************************
4917 *
4918 * uCode download functions
4919 *
4920 ******************************************************************************/
4921
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08004922static void iwl3945_dealloc_ucode_pci(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004923{
Tomas Winkler98c92212008-01-14 17:46:20 -08004924 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_code);
4925 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_data);
4926 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_data_backup);
4927 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_init);
4928 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_init_data);
4929 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_boot);
Zhu Yib481de92007-09-25 17:54:57 -07004930}
4931
4932/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004933 * iwl3945_verify_inst_full - verify runtime uCode image in card vs. host,
Zhu Yib481de92007-09-25 17:54:57 -07004934 * looking at all data.
4935 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08004936static int iwl3945_verify_inst_full(struct iwl_priv *priv, __le32 *image, u32 len)
Zhu Yib481de92007-09-25 17:54:57 -07004937{
4938 u32 val;
4939 u32 save_len = len;
4940 int rc = 0;
4941 u32 errcnt;
4942
4943 IWL_DEBUG_INFO("ucode inst image size is %u\n", len);
4944
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08004945 rc = iwl_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004946 if (rc)
4947 return rc;
4948
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08004949 iwl_write_direct32(priv, HBUS_TARG_MEM_RADDR,
Samuel Ortiz250bdd22008-12-19 10:37:11 +08004950 IWL39_RTC_INST_LOWER_BOUND);
Zhu Yib481de92007-09-25 17:54:57 -07004951
4952 errcnt = 0;
4953 for (; len > 0; len -= sizeof(u32), image++) {
4954 /* read data comes through single port, auto-incr addr */
4955 /* NOTE: Use the debugless read so we don't flood kernel log
4956 * if IWL_DL_IO is set */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08004957 val = _iwl_read_direct32(priv, HBUS_TARG_MEM_RDAT);
Zhu Yib481de92007-09-25 17:54:57 -07004958 if (val != le32_to_cpu(*image)) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08004959 IWL_ERR(priv, "uCode INST section is invalid at "
Zhu Yib481de92007-09-25 17:54:57 -07004960 "offset 0x%x, is 0x%x, s/b 0x%x\n",
4961 save_len - len, val, le32_to_cpu(*image));
4962 rc = -EIO;
4963 errcnt++;
4964 if (errcnt >= 20)
4965 break;
4966 }
4967 }
4968
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08004969 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004970
4971 if (!errcnt)
Ian Schrambc434dd2007-10-25 17:15:29 +08004972 IWL_DEBUG_INFO("ucode image in INSTRUCTION memory is good\n");
Zhu Yib481de92007-09-25 17:54:57 -07004973
4974 return rc;
4975}
4976
4977
4978/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004979 * iwl3945_verify_inst_sparse - verify runtime uCode image in card vs. host,
Zhu Yib481de92007-09-25 17:54:57 -07004980 * using sample data 100 bytes apart. If these sample points are good,
4981 * it's a pretty good bet that everything between them is good, too.
4982 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08004983static int iwl3945_verify_inst_sparse(struct iwl_priv *priv, __le32 *image, u32 len)
Zhu Yib481de92007-09-25 17:54:57 -07004984{
4985 u32 val;
4986 int rc = 0;
4987 u32 errcnt = 0;
4988 u32 i;
4989
4990 IWL_DEBUG_INFO("ucode inst image size is %u\n", len);
4991
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08004992 rc = iwl_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004993 if (rc)
4994 return rc;
4995
4996 for (i = 0; i < len; i += 100, image += 100/sizeof(u32)) {
4997 /* read data comes through single port, auto-incr addr */
4998 /* NOTE: Use the debugless read so we don't flood kernel log
4999 * if IWL_DL_IO is set */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08005000 iwl_write_direct32(priv, HBUS_TARG_MEM_RADDR,
Samuel Ortiz250bdd22008-12-19 10:37:11 +08005001 i + IWL39_RTC_INST_LOWER_BOUND);
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08005002 val = _iwl_read_direct32(priv, HBUS_TARG_MEM_RDAT);
Zhu Yib481de92007-09-25 17:54:57 -07005003 if (val != le32_to_cpu(*image)) {
5004#if 0 /* Enable this if you want to see details */
Winkler, Tomas15b16872008-12-19 10:37:33 +08005005 IWL_ERR(priv, "uCode INST section is invalid at "
Zhu Yib481de92007-09-25 17:54:57 -07005006 "offset 0x%x, is 0x%x, s/b 0x%x\n",
5007 i, val, *image);
5008#endif
5009 rc = -EIO;
5010 errcnt++;
5011 if (errcnt >= 3)
5012 break;
5013 }
5014 }
5015
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08005016 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005017
5018 return rc;
5019}
5020
5021
5022/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005023 * iwl3945_verify_ucode - determine which instruction image is in SRAM,
Zhu Yib481de92007-09-25 17:54:57 -07005024 * and verify its contents
5025 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08005026static int iwl3945_verify_ucode(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005027{
5028 __le32 *image;
5029 u32 len;
5030 int rc = 0;
5031
5032 /* Try bootstrap */
5033 image = (__le32 *)priv->ucode_boot.v_addr;
5034 len = priv->ucode_boot.len;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005035 rc = iwl3945_verify_inst_sparse(priv, image, len);
Zhu Yib481de92007-09-25 17:54:57 -07005036 if (rc == 0) {
5037 IWL_DEBUG_INFO("Bootstrap uCode is good in inst SRAM\n");
5038 return 0;
5039 }
5040
5041 /* Try initialize */
5042 image = (__le32 *)priv->ucode_init.v_addr;
5043 len = priv->ucode_init.len;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005044 rc = iwl3945_verify_inst_sparse(priv, image, len);
Zhu Yib481de92007-09-25 17:54:57 -07005045 if (rc == 0) {
5046 IWL_DEBUG_INFO("Initialize uCode is good in inst SRAM\n");
5047 return 0;
5048 }
5049
5050 /* Try runtime/protocol */
5051 image = (__le32 *)priv->ucode_code.v_addr;
5052 len = priv->ucode_code.len;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005053 rc = iwl3945_verify_inst_sparse(priv, image, len);
Zhu Yib481de92007-09-25 17:54:57 -07005054 if (rc == 0) {
5055 IWL_DEBUG_INFO("Runtime uCode is good in inst SRAM\n");
5056 return 0;
5057 }
5058
Winkler, Tomas15b16872008-12-19 10:37:33 +08005059 IWL_ERR(priv, "NO VALID UCODE IMAGE IN INSTRUCTION SRAM!!\n");
Zhu Yib481de92007-09-25 17:54:57 -07005060
Ben Cahill9fbab512007-11-29 11:09:47 +08005061 /* Since nothing seems to match, show first several data entries in
5062 * instruction SRAM, so maybe visual inspection will give a clue.
5063 * Selection of bootstrap image (vs. other images) is arbitrary. */
Zhu Yib481de92007-09-25 17:54:57 -07005064 image = (__le32 *)priv->ucode_boot.v_addr;
5065 len = priv->ucode_boot.len;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005066 rc = iwl3945_verify_inst_full(priv, image, len);
Zhu Yib481de92007-09-25 17:54:57 -07005067
5068 return rc;
5069}
5070
5071
5072/* check contents of special bootstrap uCode SRAM */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08005073static int iwl3945_verify_bsm(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005074{
5075 __le32 *image = priv->ucode_boot.v_addr;
5076 u32 len = priv->ucode_boot.len;
5077 u32 reg;
5078 u32 val;
5079
5080 IWL_DEBUG_INFO("Begin verify bsm\n");
5081
5082 /* verify BSM SRAM contents */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08005083 val = iwl_read_prph(priv, BSM_WR_DWCOUNT_REG);
Zhu Yib481de92007-09-25 17:54:57 -07005084 for (reg = BSM_SRAM_LOWER_BOUND;
5085 reg < BSM_SRAM_LOWER_BOUND + len;
Tomas Winkler3ac7f142008-07-21 02:40:14 +03005086 reg += sizeof(u32), image++) {
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08005087 val = iwl_read_prph(priv, reg);
Zhu Yib481de92007-09-25 17:54:57 -07005088 if (val != le32_to_cpu(*image)) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08005089 IWL_ERR(priv, "BSM uCode verification failed at "
Zhu Yib481de92007-09-25 17:54:57 -07005090 "addr 0x%08X+%u (of %u), is 0x%x, s/b 0x%x\n",
5091 BSM_SRAM_LOWER_BOUND,
5092 reg - BSM_SRAM_LOWER_BOUND, len,
5093 val, le32_to_cpu(*image));
5094 return -EIO;
5095 }
5096 }
5097
5098 IWL_DEBUG_INFO("BSM bootstrap uCode image OK\n");
5099
5100 return 0;
5101}
5102
5103/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005104 * iwl3945_load_bsm - Load bootstrap instructions
Zhu Yib481de92007-09-25 17:54:57 -07005105 *
5106 * BSM operation:
5107 *
5108 * The Bootstrap State Machine (BSM) stores a short bootstrap uCode program
5109 * in special SRAM that does not power down during RFKILL. When powering back
5110 * up after power-saving sleeps (or during initial uCode load), the BSM loads
5111 * the bootstrap program into the on-board processor, and starts it.
5112 *
5113 * The bootstrap program loads (via DMA) instructions and data for a new
5114 * program from host DRAM locations indicated by the host driver in the
5115 * BSM_DRAM_* registers. Once the new program is loaded, it starts
5116 * automatically.
5117 *
5118 * When initializing the NIC, the host driver points the BSM to the
5119 * "initialize" uCode image. This uCode sets up some internal data, then
5120 * notifies host via "initialize alive" that it is complete.
5121 *
5122 * The host then replaces the BSM_DRAM_* pointer values to point to the
5123 * normal runtime uCode instructions and a backup uCode data cache buffer
5124 * (filled initially with starting data values for the on-board processor),
5125 * then triggers the "initialize" uCode to load and launch the runtime uCode,
5126 * which begins normal operation.
5127 *
5128 * When doing a power-save shutdown, runtime uCode saves data SRAM into
5129 * the backup data cache in DRAM before SRAM is powered down.
5130 *
5131 * When powering back up, the BSM loads the bootstrap program. This reloads
5132 * the runtime uCode instructions and the backup data cache into SRAM,
5133 * and re-launches the runtime uCode from where it left off.
5134 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08005135static int iwl3945_load_bsm(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005136{
5137 __le32 *image = priv->ucode_boot.v_addr;
5138 u32 len = priv->ucode_boot.len;
5139 dma_addr_t pinst;
5140 dma_addr_t pdata;
5141 u32 inst_len;
5142 u32 data_len;
5143 int rc;
5144 int i;
5145 u32 done;
5146 u32 reg_offset;
5147
5148 IWL_DEBUG_INFO("Begin load bsm\n");
5149
5150 /* make sure bootstrap program is no larger than BSM's SRAM size */
Samuel Ortiz250bdd22008-12-19 10:37:11 +08005151 if (len > IWL39_MAX_BSM_SIZE)
Zhu Yib481de92007-09-25 17:54:57 -07005152 return -EINVAL;
5153
5154 /* Tell bootstrap uCode where to find the "Initialize" uCode
Ben Cahill9fbab512007-11-29 11:09:47 +08005155 * in host DRAM ... host DRAM physical address bits 31:0 for 3945.
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005156 * NOTE: iwl3945_initialize_alive_start() will replace these values,
Zhu Yib481de92007-09-25 17:54:57 -07005157 * after the "initialize" uCode has run, to point to
5158 * runtime/protocol instructions and backup data cache. */
5159 pinst = priv->ucode_init.p_addr;
5160 pdata = priv->ucode_init_data.p_addr;
5161 inst_len = priv->ucode_init.len;
5162 data_len = priv->ucode_init_data.len;
5163
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08005164 rc = iwl_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005165 if (rc)
5166 return rc;
5167
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08005168 iwl_write_prph(priv, BSM_DRAM_INST_PTR_REG, pinst);
5169 iwl_write_prph(priv, BSM_DRAM_DATA_PTR_REG, pdata);
5170 iwl_write_prph(priv, BSM_DRAM_INST_BYTECOUNT_REG, inst_len);
5171 iwl_write_prph(priv, BSM_DRAM_DATA_BYTECOUNT_REG, data_len);
Zhu Yib481de92007-09-25 17:54:57 -07005172
5173 /* Fill BSM memory with bootstrap instructions */
5174 for (reg_offset = BSM_SRAM_LOWER_BOUND;
5175 reg_offset < BSM_SRAM_LOWER_BOUND + len;
5176 reg_offset += sizeof(u32), image++)
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08005177 _iwl_write_prph(priv, reg_offset,
Zhu Yib481de92007-09-25 17:54:57 -07005178 le32_to_cpu(*image));
5179
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005180 rc = iwl3945_verify_bsm(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005181 if (rc) {
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08005182 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005183 return rc;
5184 }
5185
5186 /* Tell BSM to copy from BSM SRAM into instruction SRAM, when asked */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08005187 iwl_write_prph(priv, BSM_WR_MEM_SRC_REG, 0x0);
5188 iwl_write_prph(priv, BSM_WR_MEM_DST_REG,
Samuel Ortiz250bdd22008-12-19 10:37:11 +08005189 IWL39_RTC_INST_LOWER_BOUND);
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08005190 iwl_write_prph(priv, BSM_WR_DWCOUNT_REG, len / sizeof(u32));
Zhu Yib481de92007-09-25 17:54:57 -07005191
5192 /* Load bootstrap code into instruction SRAM now,
5193 * to prepare to load "initialize" uCode */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08005194 iwl_write_prph(priv, BSM_WR_CTRL_REG,
Zhu Yib481de92007-09-25 17:54:57 -07005195 BSM_WR_CTRL_REG_BIT_START);
5196
5197 /* Wait for load of bootstrap uCode to finish */
5198 for (i = 0; i < 100; i++) {
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08005199 done = iwl_read_prph(priv, BSM_WR_CTRL_REG);
Zhu Yib481de92007-09-25 17:54:57 -07005200 if (!(done & BSM_WR_CTRL_REG_BIT_START))
5201 break;
5202 udelay(10);
5203 }
5204 if (i < 100)
5205 IWL_DEBUG_INFO("BSM write complete, poll %d iterations\n", i);
5206 else {
Winkler, Tomas15b16872008-12-19 10:37:33 +08005207 IWL_ERR(priv, "BSM write did not complete!\n");
Zhu Yib481de92007-09-25 17:54:57 -07005208 return -EIO;
5209 }
5210
5211 /* Enable future boot loads whenever power management unit triggers it
5212 * (e.g. when powering back up after power-save shutdown) */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08005213 iwl_write_prph(priv, BSM_WR_CTRL_REG,
Zhu Yib481de92007-09-25 17:54:57 -07005214 BSM_WR_CTRL_REG_BIT_START_EN);
5215
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08005216 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005217
5218 return 0;
5219}
5220
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08005221static void iwl3945_nic_start(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005222{
5223 /* Remove all resets to allow NIC to operate */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08005224 iwl_write32(priv, CSR_RESET, 0);
Zhu Yib481de92007-09-25 17:54:57 -07005225}
5226
5227/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005228 * iwl3945_read_ucode - Read uCode images from disk file.
Zhu Yib481de92007-09-25 17:54:57 -07005229 *
5230 * Copy into buffers for card to fetch via bus-mastering
5231 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08005232static int iwl3945_read_ucode(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005233{
Kolekar, Abhijeeta78fe752008-12-19 10:37:21 +08005234 struct iwl_ucode *ucode;
Reinette Chatrea0987a82008-12-02 12:14:06 -08005235 int ret = -EINVAL, index;
Zhu Yib481de92007-09-25 17:54:57 -07005236 const struct firmware *ucode_raw;
5237 /* firmware file name contains uCode/driver compatibility version */
Reinette Chatrea0987a82008-12-02 12:14:06 -08005238 const char *name_pre = priv->cfg->fw_name_pre;
5239 const unsigned int api_max = priv->cfg->ucode_api_max;
5240 const unsigned int api_min = priv->cfg->ucode_api_min;
5241 char buf[25];
Zhu Yib481de92007-09-25 17:54:57 -07005242 u8 *src;
5243 size_t len;
Reinette Chatrea0987a82008-12-02 12:14:06 -08005244 u32 api_ver, inst_size, data_size, init_size, init_data_size, boot_size;
Zhu Yib481de92007-09-25 17:54:57 -07005245
5246 /* Ask kernel firmware_class module to get the boot firmware off disk.
5247 * request_firmware() is synchronous, file is in memory on return. */
Reinette Chatrea0987a82008-12-02 12:14:06 -08005248 for (index = api_max; index >= api_min; index--) {
5249 sprintf(buf, "%s%u%s", name_pre, index, ".ucode");
5250 ret = request_firmware(&ucode_raw, buf, &priv->pci_dev->dev);
5251 if (ret < 0) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08005252 IWL_ERR(priv, "%s firmware file req failed: %d\n",
Reinette Chatrea0987a82008-12-02 12:14:06 -08005253 buf, ret);
5254 if (ret == -ENOENT)
5255 continue;
5256 else
5257 goto error;
5258 } else {
5259 if (index < api_max)
Winkler, Tomas15b16872008-12-19 10:37:33 +08005260 IWL_ERR(priv, "Loaded firmware %s, "
5261 "which is deprecated. "
5262 " Please use API v%u instead.\n",
Reinette Chatrea0987a82008-12-02 12:14:06 -08005263 buf, api_max);
5264 IWL_DEBUG_INFO("Got firmware '%s' file (%zd bytes) from disk\n",
5265 buf, ucode_raw->size);
5266 break;
5267 }
Zhu Yib481de92007-09-25 17:54:57 -07005268 }
5269
Reinette Chatrea0987a82008-12-02 12:14:06 -08005270 if (ret < 0)
5271 goto error;
Zhu Yib481de92007-09-25 17:54:57 -07005272
5273 /* Make sure that we got at least our header! */
5274 if (ucode_raw->size < sizeof(*ucode)) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08005275 IWL_ERR(priv, "File size way too small!\n");
Tomas Winkler90e759d2007-11-29 11:09:41 +08005276 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07005277 goto err_release;
5278 }
5279
5280 /* Data from ucode file: header followed by uCode images */
5281 ucode = (void *)ucode_raw->data;
5282
Chatre, Reinettec02b3ac2008-12-02 12:14:05 -08005283 priv->ucode_ver = le32_to_cpu(ucode->ver);
Reinette Chatrea0987a82008-12-02 12:14:06 -08005284 api_ver = IWL_UCODE_API(priv->ucode_ver);
Zhu Yib481de92007-09-25 17:54:57 -07005285 inst_size = le32_to_cpu(ucode->inst_size);
5286 data_size = le32_to_cpu(ucode->data_size);
5287 init_size = le32_to_cpu(ucode->init_size);
5288 init_data_size = le32_to_cpu(ucode->init_data_size);
5289 boot_size = le32_to_cpu(ucode->boot_size);
5290
Reinette Chatrea0987a82008-12-02 12:14:06 -08005291 /* api_ver should match the api version forming part of the
5292 * firmware filename ... but we don't check for that and only rely
5293 * on the API version read from firware header from here on forward */
5294
5295 if (api_ver < api_min || api_ver > api_max) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08005296 IWL_ERR(priv, "Driver unable to support your firmware API. "
Reinette Chatrea0987a82008-12-02 12:14:06 -08005297 "Driver supports v%u, firmware is v%u.\n",
5298 api_max, api_ver);
5299 priv->ucode_ver = 0;
5300 ret = -EINVAL;
5301 goto err_release;
5302 }
5303 if (api_ver != api_max)
Winkler, Tomas15b16872008-12-19 10:37:33 +08005304 IWL_ERR(priv, "Firmware has old API version. Expected %u, "
Reinette Chatrea0987a82008-12-02 12:14:06 -08005305 "got %u. New firmware can be obtained "
5306 "from http://www.intellinuxwireless.org.\n",
5307 api_max, api_ver);
5308
Tomas Winkler978785a2008-12-19 10:37:31 +08005309 IWL_INFO(priv, "loaded firmware version %u.%u.%u.%u\n",
5310 IWL_UCODE_MAJOR(priv->ucode_ver),
5311 IWL_UCODE_MINOR(priv->ucode_ver),
5312 IWL_UCODE_API(priv->ucode_ver),
5313 IWL_UCODE_SERIAL(priv->ucode_ver));
5314
Reinette Chatrea0987a82008-12-02 12:14:06 -08005315 IWL_DEBUG_INFO("f/w package hdr ucode version raw = 0x%x\n",
5316 priv->ucode_ver);
Ian Schrambc434dd2007-10-25 17:15:29 +08005317 IWL_DEBUG_INFO("f/w package hdr runtime inst size = %u\n", inst_size);
5318 IWL_DEBUG_INFO("f/w package hdr runtime data size = %u\n", data_size);
5319 IWL_DEBUG_INFO("f/w package hdr init inst size = %u\n", init_size);
5320 IWL_DEBUG_INFO("f/w package hdr init data size = %u\n", init_data_size);
5321 IWL_DEBUG_INFO("f/w package hdr boot inst size = %u\n", boot_size);
Zhu Yib481de92007-09-25 17:54:57 -07005322
Reinette Chatrea0987a82008-12-02 12:14:06 -08005323
Zhu Yib481de92007-09-25 17:54:57 -07005324 /* Verify size of file vs. image size info in file's header */
5325 if (ucode_raw->size < sizeof(*ucode) +
5326 inst_size + data_size + init_size +
5327 init_data_size + boot_size) {
5328
5329 IWL_DEBUG_INFO("uCode file size %d too small\n",
5330 (int)ucode_raw->size);
Tomas Winkler90e759d2007-11-29 11:09:41 +08005331 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07005332 goto err_release;
5333 }
5334
5335 /* Verify that uCode images will fit in card's SRAM */
Samuel Ortiz250bdd22008-12-19 10:37:11 +08005336 if (inst_size > IWL39_MAX_INST_SIZE) {
Tomas Winkler90e759d2007-11-29 11:09:41 +08005337 IWL_DEBUG_INFO("uCode instr len %d too large to fit in\n",
5338 inst_size);
5339 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07005340 goto err_release;
5341 }
5342
Samuel Ortiz250bdd22008-12-19 10:37:11 +08005343 if (data_size > IWL39_MAX_DATA_SIZE) {
Tomas Winkler90e759d2007-11-29 11:09:41 +08005344 IWL_DEBUG_INFO("uCode data len %d too large to fit in\n",
5345 data_size);
5346 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07005347 goto err_release;
5348 }
Samuel Ortiz250bdd22008-12-19 10:37:11 +08005349 if (init_size > IWL39_MAX_INST_SIZE) {
Tomas Winkler90e759d2007-11-29 11:09:41 +08005350 IWL_DEBUG_INFO("uCode init instr len %d too large to fit in\n",
5351 init_size);
5352 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07005353 goto err_release;
5354 }
Samuel Ortiz250bdd22008-12-19 10:37:11 +08005355 if (init_data_size > IWL39_MAX_DATA_SIZE) {
Tomas Winkler90e759d2007-11-29 11:09:41 +08005356 IWL_DEBUG_INFO("uCode init data len %d too large to fit in\n",
5357 init_data_size);
5358 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07005359 goto err_release;
5360 }
Samuel Ortiz250bdd22008-12-19 10:37:11 +08005361 if (boot_size > IWL39_MAX_BSM_SIZE) {
Tomas Winkler90e759d2007-11-29 11:09:41 +08005362 IWL_DEBUG_INFO("uCode boot instr len %d too large to fit in\n",
5363 boot_size);
5364 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07005365 goto err_release;
5366 }
5367
5368 /* Allocate ucode buffers for card's bus-master loading ... */
5369
5370 /* Runtime instructions and 2 copies of data:
5371 * 1) unmodified from disk
5372 * 2) backup cache for save/restore during power-downs */
5373 priv->ucode_code.len = inst_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08005374 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_code);
Zhu Yib481de92007-09-25 17:54:57 -07005375
5376 priv->ucode_data.len = data_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08005377 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_data);
Zhu Yib481de92007-09-25 17:54:57 -07005378
5379 priv->ucode_data_backup.len = data_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08005380 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_data_backup);
Zhu Yib481de92007-09-25 17:54:57 -07005381
5382 if (!priv->ucode_code.v_addr || !priv->ucode_data.v_addr ||
Tomas Winkler90e759d2007-11-29 11:09:41 +08005383 !priv->ucode_data_backup.v_addr)
Zhu Yib481de92007-09-25 17:54:57 -07005384 goto err_pci_alloc;
5385
Tomas Winkler90e759d2007-11-29 11:09:41 +08005386 /* Initialization instructions and data */
5387 if (init_size && init_data_size) {
5388 priv->ucode_init.len = init_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08005389 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_init);
Tomas Winkler90e759d2007-11-29 11:09:41 +08005390
5391 priv->ucode_init_data.len = init_data_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08005392 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_init_data);
Tomas Winkler90e759d2007-11-29 11:09:41 +08005393
5394 if (!priv->ucode_init.v_addr || !priv->ucode_init_data.v_addr)
5395 goto err_pci_alloc;
5396 }
5397
5398 /* Bootstrap (instructions only, no data) */
5399 if (boot_size) {
5400 priv->ucode_boot.len = boot_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08005401 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_boot);
Tomas Winkler90e759d2007-11-29 11:09:41 +08005402
5403 if (!priv->ucode_boot.v_addr)
5404 goto err_pci_alloc;
5405 }
5406
Zhu Yib481de92007-09-25 17:54:57 -07005407 /* Copy images into buffers for card's bus-master reads ... */
5408
5409 /* Runtime instructions (first block of data in file) */
5410 src = &ucode->data[0];
5411 len = priv->ucode_code.len;
Tomas Winkler90e759d2007-11-29 11:09:41 +08005412 IWL_DEBUG_INFO("Copying (but not loading) uCode instr len %Zd\n", len);
Zhu Yib481de92007-09-25 17:54:57 -07005413 memcpy(priv->ucode_code.v_addr, src, len);
5414 IWL_DEBUG_INFO("uCode instr buf vaddr = 0x%p, paddr = 0x%08x\n",
5415 priv->ucode_code.v_addr, (u32)priv->ucode_code.p_addr);
5416
5417 /* Runtime data (2nd block)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005418 * NOTE: Copy into backup buffer will be done in iwl3945_up() */
Zhu Yib481de92007-09-25 17:54:57 -07005419 src = &ucode->data[inst_size];
5420 len = priv->ucode_data.len;
Tomas Winkler90e759d2007-11-29 11:09:41 +08005421 IWL_DEBUG_INFO("Copying (but not loading) uCode data len %Zd\n", len);
Zhu Yib481de92007-09-25 17:54:57 -07005422 memcpy(priv->ucode_data.v_addr, src, len);
5423 memcpy(priv->ucode_data_backup.v_addr, src, len);
5424
5425 /* Initialization instructions (3rd block) */
5426 if (init_size) {
5427 src = &ucode->data[inst_size + data_size];
5428 len = priv->ucode_init.len;
Tomas Winkler90e759d2007-11-29 11:09:41 +08005429 IWL_DEBUG_INFO("Copying (but not loading) init instr len %Zd\n",
5430 len);
Zhu Yib481de92007-09-25 17:54:57 -07005431 memcpy(priv->ucode_init.v_addr, src, len);
5432 }
5433
5434 /* Initialization data (4th block) */
5435 if (init_data_size) {
5436 src = &ucode->data[inst_size + data_size + init_size];
5437 len = priv->ucode_init_data.len;
5438 IWL_DEBUG_INFO("Copying (but not loading) init data len %d\n",
5439 (int)len);
5440 memcpy(priv->ucode_init_data.v_addr, src, len);
5441 }
5442
5443 /* Bootstrap instructions (5th block) */
5444 src = &ucode->data[inst_size + data_size + init_size + init_data_size];
5445 len = priv->ucode_boot.len;
5446 IWL_DEBUG_INFO("Copying (but not loading) boot instr len %d\n",
5447 (int)len);
5448 memcpy(priv->ucode_boot.v_addr, src, len);
5449
5450 /* We have our copies now, allow OS release its copies */
5451 release_firmware(ucode_raw);
5452 return 0;
5453
5454 err_pci_alloc:
Winkler, Tomas15b16872008-12-19 10:37:33 +08005455 IWL_ERR(priv, "failed to allocate pci memory\n");
Tomas Winkler90e759d2007-11-29 11:09:41 +08005456 ret = -ENOMEM;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005457 iwl3945_dealloc_ucode_pci(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005458
5459 err_release:
5460 release_firmware(ucode_raw);
5461
5462 error:
Tomas Winkler90e759d2007-11-29 11:09:41 +08005463 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07005464}
5465
5466
5467/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005468 * iwl3945_set_ucode_ptrs - Set uCode address location
Zhu Yib481de92007-09-25 17:54:57 -07005469 *
5470 * Tell initialization uCode where to find runtime uCode.
5471 *
5472 * BSM registers initially contain pointers to initialization uCode.
5473 * We need to replace them to load runtime uCode inst and data,
5474 * and to save runtime data when powering down.
5475 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08005476static int iwl3945_set_ucode_ptrs(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005477{
5478 dma_addr_t pinst;
5479 dma_addr_t pdata;
5480 int rc = 0;
5481 unsigned long flags;
5482
5483 /* bits 31:0 for 3945 */
5484 pinst = priv->ucode_code.p_addr;
5485 pdata = priv->ucode_data_backup.p_addr;
5486
5487 spin_lock_irqsave(&priv->lock, flags);
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08005488 rc = iwl_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005489 if (rc) {
5490 spin_unlock_irqrestore(&priv->lock, flags);
5491 return rc;
5492 }
5493
5494 /* Tell bootstrap uCode where to find image to load */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08005495 iwl_write_prph(priv, BSM_DRAM_INST_PTR_REG, pinst);
5496 iwl_write_prph(priv, BSM_DRAM_DATA_PTR_REG, pdata);
5497 iwl_write_prph(priv, BSM_DRAM_DATA_BYTECOUNT_REG,
Zhu Yib481de92007-09-25 17:54:57 -07005498 priv->ucode_data.len);
5499
Tomas Winklera96a27f2008-10-23 23:48:56 -07005500 /* Inst byte count must be last to set up, bit 31 signals uCode
Zhu Yib481de92007-09-25 17:54:57 -07005501 * that all new ptr/size info is in place */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08005502 iwl_write_prph(priv, BSM_DRAM_INST_BYTECOUNT_REG,
Zhu Yib481de92007-09-25 17:54:57 -07005503 priv->ucode_code.len | BSM_DRAM_INST_LOAD);
5504
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08005505 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005506
5507 spin_unlock_irqrestore(&priv->lock, flags);
5508
5509 IWL_DEBUG_INFO("Runtime uCode pointers are set.\n");
5510
5511 return rc;
5512}
5513
5514/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005515 * iwl3945_init_alive_start - Called after REPLY_ALIVE notification received
Zhu Yib481de92007-09-25 17:54:57 -07005516 *
5517 * Called after REPLY_ALIVE notification received from "initialize" uCode.
5518 *
Zhu Yib481de92007-09-25 17:54:57 -07005519 * Tell "initialize" uCode to go ahead and load the runtime uCode.
Ben Cahill9fbab512007-11-29 11:09:47 +08005520 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08005521static void iwl3945_init_alive_start(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005522{
5523 /* Check alive response for "valid" sign from uCode */
5524 if (priv->card_alive_init.is_valid != UCODE_VALID_OK) {
5525 /* We had an error bringing up the hardware, so take it
5526 * all the way back down so we can try again */
5527 IWL_DEBUG_INFO("Initialize Alive failed.\n");
5528 goto restart;
5529 }
5530
5531 /* Bootstrap uCode has loaded initialize uCode ... verify inst image.
5532 * This is a paranoid check, because we would not have gotten the
5533 * "initialize" alive if code weren't properly loaded. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005534 if (iwl3945_verify_ucode(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07005535 /* Runtime instruction load was bad;
5536 * take it all the way back down so we can try again */
5537 IWL_DEBUG_INFO("Bad \"initialize\" uCode load.\n");
5538 goto restart;
5539 }
5540
5541 /* Send pointers to protocol/runtime uCode image ... init code will
5542 * load and launch runtime uCode, which will send us another "Alive"
5543 * notification. */
5544 IWL_DEBUG_INFO("Initialization Alive received.\n");
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005545 if (iwl3945_set_ucode_ptrs(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07005546 /* Runtime instruction load won't happen;
5547 * take it all the way back down so we can try again */
5548 IWL_DEBUG_INFO("Couldn't set up uCode pointers.\n");
5549 goto restart;
5550 }
5551 return;
5552
5553 restart:
5554 queue_work(priv->workqueue, &priv->restart);
5555}
5556
5557
Mohamed Abbas9bdf5ec2008-11-07 09:58:35 -08005558/* temporary */
5559static int iwl3945_mac_beacon_update(struct ieee80211_hw *hw,
5560 struct sk_buff *skb);
5561
Zhu Yib481de92007-09-25 17:54:57 -07005562/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005563 * iwl3945_alive_start - called after REPLY_ALIVE notification received
Zhu Yib481de92007-09-25 17:54:57 -07005564 * from protocol/runtime uCode (initialization uCode's
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005565 * Alive gets handled by iwl3945_init_alive_start()).
Zhu Yib481de92007-09-25 17:54:57 -07005566 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08005567static void iwl3945_alive_start(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005568{
5569 int rc = 0;
5570 int thermal_spin = 0;
5571 u32 rfkill;
5572
5573 IWL_DEBUG_INFO("Runtime Alive received.\n");
5574
5575 if (priv->card_alive.is_valid != UCODE_VALID_OK) {
5576 /* We had an error bringing up the hardware, so take it
5577 * all the way back down so we can try again */
5578 IWL_DEBUG_INFO("Alive failed.\n");
5579 goto restart;
5580 }
5581
5582 /* Initialize uCode has loaded Runtime uCode ... verify inst image.
5583 * This is a paranoid check, because we would not have gotten the
5584 * "runtime" alive if code weren't properly loaded. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005585 if (iwl3945_verify_ucode(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07005586 /* Runtime instruction load was bad;
5587 * take it all the way back down so we can try again */
5588 IWL_DEBUG_INFO("Bad runtime uCode load.\n");
5589 goto restart;
5590 }
5591
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005592 iwl3945_clear_stations_table(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005593
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08005594 rc = iwl_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005595 if (rc) {
Winkler, Tomas39aadf82008-12-19 10:37:32 +08005596 IWL_WARN(priv, "Can not read RFKILL status from adapter\n");
Zhu Yib481de92007-09-25 17:54:57 -07005597 return;
5598 }
5599
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08005600 rfkill = iwl_read_prph(priv, APMG_RFKILL_REG);
Zhu Yib481de92007-09-25 17:54:57 -07005601 IWL_DEBUG_INFO("RFKILL status: 0x%x\n", rfkill);
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08005602 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005603
5604 if (rfkill & 0x1) {
5605 clear_bit(STATUS_RF_KILL_HW, &priv->status);
Tomas Winklera96a27f2008-10-23 23:48:56 -07005606 /* if RFKILL is not on, then wait for thermal
Zhu Yib481de92007-09-25 17:54:57 -07005607 * sensor in adapter to kick in */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005608 while (iwl3945_hw_get_temperature(priv) == 0) {
Zhu Yib481de92007-09-25 17:54:57 -07005609 thermal_spin++;
5610 udelay(10);
5611 }
5612
5613 if (thermal_spin)
5614 IWL_DEBUG_INFO("Thermal calibration took %dus\n",
5615 thermal_spin * 10);
5616 } else
5617 set_bit(STATUS_RF_KILL_HW, &priv->status);
5618
Ben Cahill9fbab512007-11-29 11:09:47 +08005619 /* After the ALIVE response, we can send commands to 3945 uCode */
Zhu Yib481de92007-09-25 17:54:57 -07005620 set_bit(STATUS_ALIVE, &priv->status);
5621
5622 /* Clear out the uCode error bit if it is set */
5623 clear_bit(STATUS_FW_ERROR, &priv->status);
5624
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005625 if (iwl3945_is_rfkill(priv))
Zhu Yib481de92007-09-25 17:54:57 -07005626 return;
5627
Johannes Berg36d68252008-05-15 12:55:26 +02005628 ieee80211_wake_queues(priv->hw);
Zhu Yib481de92007-09-25 17:54:57 -07005629
5630 priv->active_rate = priv->rates_mask;
5631 priv->active_rate_basic = priv->rates_mask & IWL_BASIC_RATES_MASK;
5632
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005633 iwl3945_send_power_mode(priv, IWL_POWER_LEVEL(priv->power_mode));
Zhu Yib481de92007-09-25 17:54:57 -07005634
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005635 if (iwl3945_is_associated(priv)) {
5636 struct iwl3945_rxon_cmd *active_rxon =
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08005637 (struct iwl3945_rxon_cmd *)(&priv->active39_rxon);
Zhu Yib481de92007-09-25 17:54:57 -07005638
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08005639 memcpy(&priv->staging39_rxon, &priv->active39_rxon,
5640 sizeof(priv->staging39_rxon));
Zhu Yib481de92007-09-25 17:54:57 -07005641 active_rxon->filter_flags &= ~RXON_FILTER_ASSOC_MSK;
5642 } else {
5643 /* Initialize our rx_config data */
Zhu, Yi60294de2008-10-29 14:05:45 -07005644 iwl3945_connection_init_rx_config(priv, priv->iw_mode);
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08005645 memcpy(priv->staging39_rxon.node_addr, priv->mac_addr, ETH_ALEN);
Zhu Yib481de92007-09-25 17:54:57 -07005646 }
5647
Ben Cahill9fbab512007-11-29 11:09:47 +08005648 /* Configure Bluetooth device coexistence support */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005649 iwl3945_send_bt_config(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005650
5651 /* Configure the adapter for unassociated operation */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005652 iwl3945_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005653
Zhu Yib481de92007-09-25 17:54:57 -07005654 iwl3945_reg_txpower_periodic(priv);
5655
Reinette Chatrefe00b5a2008-04-03 16:05:23 -07005656 iwl3945_led_register(priv);
5657
Zhu Yib481de92007-09-25 17:54:57 -07005658 IWL_DEBUG_INFO("ALIVE processing complete.\n");
Rick Farringtona9f46782008-03-18 14:57:49 -07005659 set_bit(STATUS_READY, &priv->status);
Zhu Yi5a66926a2008-01-14 17:46:18 -08005660 wake_up_interruptible(&priv->wait_command_queue);
Zhu Yib481de92007-09-25 17:54:57 -07005661
5662 if (priv->error_recovering)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005663 iwl3945_error_recovery(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005664
Mohamed Abbas9bdf5ec2008-11-07 09:58:35 -08005665 /* reassociate for ADHOC mode */
5666 if (priv->vif && (priv->iw_mode == NL80211_IFTYPE_ADHOC)) {
5667 struct sk_buff *beacon = ieee80211_beacon_get(priv->hw,
5668 priv->vif);
5669 if (beacon)
5670 iwl3945_mac_beacon_update(priv->hw, beacon);
5671 }
5672
Zhu Yib481de92007-09-25 17:54:57 -07005673 return;
5674
5675 restart:
5676 queue_work(priv->workqueue, &priv->restart);
5677}
5678
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08005679static void iwl3945_cancel_deferred_work(struct iwl_priv *priv);
Zhu Yib481de92007-09-25 17:54:57 -07005680
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08005681static void __iwl3945_down(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005682{
5683 unsigned long flags;
5684 int exit_pending = test_bit(STATUS_EXIT_PENDING, &priv->status);
5685 struct ieee80211_conf *conf = NULL;
5686
5687 IWL_DEBUG_INFO(DRV_NAME " is going down\n");
5688
5689 conf = ieee80211_get_hw_conf(priv->hw);
5690
5691 if (!exit_pending)
5692 set_bit(STATUS_EXIT_PENDING, &priv->status);
5693
Mohamed Abbasab53d8a2008-03-25 16:33:36 -07005694 iwl3945_led_unregister(priv);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005695 iwl3945_clear_stations_table(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005696
5697 /* Unblock any waiting calls */
5698 wake_up_interruptible_all(&priv->wait_command_queue);
5699
Zhu Yib481de92007-09-25 17:54:57 -07005700 /* Wipe out the EXIT_PENDING status bit if we are not actually
5701 * exiting the module */
5702 if (!exit_pending)
5703 clear_bit(STATUS_EXIT_PENDING, &priv->status);
5704
5705 /* stop and reset the on-board processor */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08005706 iwl_write32(priv, CSR_RESET, CSR_RESET_REG_FLAG_NEVO_RESET);
Zhu Yib481de92007-09-25 17:54:57 -07005707
5708 /* tell the device to stop sending interrupts */
Mohamed Abbas0359fac2008-03-28 16:21:08 -07005709 spin_lock_irqsave(&priv->lock, flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005710 iwl3945_disable_interrupts(priv);
Mohamed Abbas0359fac2008-03-28 16:21:08 -07005711 spin_unlock_irqrestore(&priv->lock, flags);
5712 iwl_synchronize_irq(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005713
5714 if (priv->mac80211_registered)
5715 ieee80211_stop_queues(priv->hw);
5716
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005717 /* If we have not previously called iwl3945_init() then
Zhu Yib481de92007-09-25 17:54:57 -07005718 * clear all bits but the RF Kill and SUSPEND bits and return */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005719 if (!iwl3945_is_init(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07005720 priv->status = test_bit(STATUS_RF_KILL_HW, &priv->status) <<
5721 STATUS_RF_KILL_HW |
5722 test_bit(STATUS_RF_KILL_SW, &priv->status) <<
5723 STATUS_RF_KILL_SW |
Reinette Chatre97888642008-02-06 11:20:38 -08005724 test_bit(STATUS_GEO_CONFIGURED, &priv->status) <<
5725 STATUS_GEO_CONFIGURED |
Zhu Yib481de92007-09-25 17:54:57 -07005726 test_bit(STATUS_IN_SUSPEND, &priv->status) <<
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08005727 STATUS_IN_SUSPEND |
5728 test_bit(STATUS_EXIT_PENDING, &priv->status) <<
5729 STATUS_EXIT_PENDING;
Zhu Yib481de92007-09-25 17:54:57 -07005730 goto exit;
5731 }
5732
5733 /* ...otherwise clear out all the status bits but the RF Kill and
5734 * SUSPEND bits and continue taking the NIC down. */
5735 priv->status &= test_bit(STATUS_RF_KILL_HW, &priv->status) <<
5736 STATUS_RF_KILL_HW |
5737 test_bit(STATUS_RF_KILL_SW, &priv->status) <<
5738 STATUS_RF_KILL_SW |
Reinette Chatre97888642008-02-06 11:20:38 -08005739 test_bit(STATUS_GEO_CONFIGURED, &priv->status) <<
5740 STATUS_GEO_CONFIGURED |
Zhu Yib481de92007-09-25 17:54:57 -07005741 test_bit(STATUS_IN_SUSPEND, &priv->status) <<
5742 STATUS_IN_SUSPEND |
5743 test_bit(STATUS_FW_ERROR, &priv->status) <<
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08005744 STATUS_FW_ERROR |
5745 test_bit(STATUS_EXIT_PENDING, &priv->status) <<
5746 STATUS_EXIT_PENDING;
Zhu Yib481de92007-09-25 17:54:57 -07005747
5748 spin_lock_irqsave(&priv->lock, flags);
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08005749 iwl_clear_bit(priv, CSR_GP_CNTRL, CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
Zhu Yib481de92007-09-25 17:54:57 -07005750 spin_unlock_irqrestore(&priv->lock, flags);
5751
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005752 iwl3945_hw_txq_ctx_stop(priv);
5753 iwl3945_hw_rxq_stop(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005754
5755 spin_lock_irqsave(&priv->lock, flags);
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08005756 if (!iwl_grab_nic_access(priv)) {
5757 iwl_write_prph(priv, APMG_CLK_DIS_REG,
Zhu Yib481de92007-09-25 17:54:57 -07005758 APMG_CLK_VAL_DMA_CLK_RQT);
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08005759 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005760 }
5761 spin_unlock_irqrestore(&priv->lock, flags);
5762
5763 udelay(5);
5764
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005765 iwl3945_hw_nic_stop_master(priv);
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08005766 iwl_set_bit(priv, CSR_RESET, CSR_RESET_REG_FLAG_SW_RESET);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005767 iwl3945_hw_nic_reset(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005768
5769 exit:
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08005770 memset(&priv->card_alive, 0, sizeof(struct iwl_alive_resp));
Zhu Yib481de92007-09-25 17:54:57 -07005771
5772 if (priv->ibss_beacon)
5773 dev_kfree_skb(priv->ibss_beacon);
5774 priv->ibss_beacon = NULL;
5775
5776 /* clear out any free frames */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005777 iwl3945_clear_free_frames(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005778}
5779
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08005780static void iwl3945_down(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005781{
5782 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005783 __iwl3945_down(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005784 mutex_unlock(&priv->mutex);
Zhu Yib24d22b2007-12-19 13:59:52 +08005785
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005786 iwl3945_cancel_deferred_work(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005787}
5788
5789#define MAX_HW_RESTARTS 5
5790
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08005791static int __iwl3945_up(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005792{
5793 int rc, i;
5794
5795 if (test_bit(STATUS_EXIT_PENDING, &priv->status)) {
Winkler, Tomas39aadf82008-12-19 10:37:32 +08005796 IWL_WARN(priv, "Exit pending; will not bring the NIC up\n");
Zhu Yib481de92007-09-25 17:54:57 -07005797 return -EIO;
5798 }
5799
5800 if (test_bit(STATUS_RF_KILL_SW, &priv->status)) {
Winkler, Tomas39aadf82008-12-19 10:37:32 +08005801 IWL_WARN(priv, "Radio disabled by SW RF kill (module "
Zhu Yib481de92007-09-25 17:54:57 -07005802 "parameter)\n");
Zhu Yie655b9f2008-01-24 02:19:38 -08005803 return -ENODEV;
5804 }
5805
Reinette Chatree903fbd2008-01-30 22:05:15 -08005806 if (!priv->ucode_data_backup.v_addr || !priv->ucode_data.v_addr) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08005807 IWL_ERR(priv, "ucode not available for device bring up\n");
Reinette Chatree903fbd2008-01-30 22:05:15 -08005808 return -EIO;
5809 }
5810
Zhu Yie655b9f2008-01-24 02:19:38 -08005811 /* If platform's RF_KILL switch is NOT set to KILL */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08005812 if (iwl_read32(priv, CSR_GP_CNTRL) &
Zhu Yie655b9f2008-01-24 02:19:38 -08005813 CSR_GP_CNTRL_REG_FLAG_HW_RF_KILL_SW)
5814 clear_bit(STATUS_RF_KILL_HW, &priv->status);
5815 else {
5816 set_bit(STATUS_RF_KILL_HW, &priv->status);
5817 if (!test_bit(STATUS_IN_SUSPEND, &priv->status)) {
Winkler, Tomas39aadf82008-12-19 10:37:32 +08005818 IWL_WARN(priv, "Radio disabled by HW RF Kill switch\n");
Zhu Yie655b9f2008-01-24 02:19:38 -08005819 return -ENODEV;
5820 }
Zhu Yib481de92007-09-25 17:54:57 -07005821 }
Adel Gadllah80fcc9e2008-07-01 17:49:50 +02005822
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08005823 iwl_write32(priv, CSR_INT, 0xFFFFFFFF);
Zhu Yib481de92007-09-25 17:54:57 -07005824
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005825 rc = iwl3945_hw_nic_init(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005826 if (rc) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08005827 IWL_ERR(priv, "Unable to int nic\n");
Zhu Yib481de92007-09-25 17:54:57 -07005828 return rc;
5829 }
5830
5831 /* make sure rfkill handshake bits are cleared */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08005832 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
5833 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR,
Zhu Yib481de92007-09-25 17:54:57 -07005834 CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
5835
5836 /* clear (again), then enable host interrupts */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08005837 iwl_write32(priv, CSR_INT, 0xFFFFFFFF);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005838 iwl3945_enable_interrupts(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005839
5840 /* really make sure rfkill handshake bits are cleared */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08005841 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
5842 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
Zhu Yib481de92007-09-25 17:54:57 -07005843
5844 /* Copy original ucode data image from disk into backup cache.
5845 * This will be used to initialize the on-board processor's
5846 * data SRAM for a clean start when the runtime program first loads. */
5847 memcpy(priv->ucode_data_backup.v_addr, priv->ucode_data.v_addr,
Zhu Yi5a66926a2008-01-14 17:46:18 -08005848 priv->ucode_data.len);
Zhu Yib481de92007-09-25 17:54:57 -07005849
Zhu Yie655b9f2008-01-24 02:19:38 -08005850 /* We return success when we resume from suspend and rf_kill is on. */
5851 if (test_bit(STATUS_RF_KILL_HW, &priv->status))
5852 return 0;
5853
Zhu Yib481de92007-09-25 17:54:57 -07005854 for (i = 0; i < MAX_HW_RESTARTS; i++) {
5855
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005856 iwl3945_clear_stations_table(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005857
5858 /* load bootstrap state machine,
5859 * load bootstrap program into processor's memory,
5860 * prepare to load the "initialize" uCode */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005861 rc = iwl3945_load_bsm(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005862
5863 if (rc) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08005864 IWL_ERR(priv,
5865 "Unable to set up bootstrap uCode: %d\n", rc);
Zhu Yib481de92007-09-25 17:54:57 -07005866 continue;
5867 }
5868
5869 /* start card; "initialize" will load runtime ucode */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005870 iwl3945_nic_start(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005871
Zhu Yib481de92007-09-25 17:54:57 -07005872 IWL_DEBUG_INFO(DRV_NAME " is coming up\n");
5873
5874 return 0;
5875 }
5876
5877 set_bit(STATUS_EXIT_PENDING, &priv->status);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005878 __iwl3945_down(priv);
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08005879 clear_bit(STATUS_EXIT_PENDING, &priv->status);
Zhu Yib481de92007-09-25 17:54:57 -07005880
5881 /* tried to restart and config the device for as long as our
5882 * patience could withstand */
Winkler, Tomas15b16872008-12-19 10:37:33 +08005883 IWL_ERR(priv, "Unable to initialize device after %d attempts.\n", i);
Zhu Yib481de92007-09-25 17:54:57 -07005884 return -EIO;
5885}
5886
5887
5888/*****************************************************************************
5889 *
5890 * Workqueue callbacks
5891 *
5892 *****************************************************************************/
5893
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005894static void iwl3945_bg_init_alive_start(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07005895{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08005896 struct iwl_priv *priv =
5897 container_of(data, struct iwl_priv, init_alive_start.work);
Zhu Yib481de92007-09-25 17:54:57 -07005898
5899 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
5900 return;
5901
5902 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005903 iwl3945_init_alive_start(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005904 mutex_unlock(&priv->mutex);
5905}
5906
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005907static void iwl3945_bg_alive_start(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07005908{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08005909 struct iwl_priv *priv =
5910 container_of(data, struct iwl_priv, alive_start.work);
Zhu Yib481de92007-09-25 17:54:57 -07005911
5912 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
5913 return;
5914
5915 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005916 iwl3945_alive_start(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005917 mutex_unlock(&priv->mutex);
5918}
5919
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005920static void iwl3945_bg_rf_kill(struct work_struct *work)
Zhu Yib481de92007-09-25 17:54:57 -07005921{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08005922 struct iwl_priv *priv = container_of(work, struct iwl_priv, rf_kill);
Zhu Yib481de92007-09-25 17:54:57 -07005923
5924 wake_up_interruptible(&priv->wait_command_queue);
5925
5926 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
5927 return;
5928
5929 mutex_lock(&priv->mutex);
5930
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005931 if (!iwl3945_is_rfkill(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07005932 IWL_DEBUG(IWL_DL_INFO | IWL_DL_RF_KILL,
5933 "HW and/or SW RF Kill no longer active, restarting "
5934 "device\n");
5935 if (!test_bit(STATUS_EXIT_PENDING, &priv->status))
5936 queue_work(priv->workqueue, &priv->restart);
5937 } else {
5938
5939 if (!test_bit(STATUS_RF_KILL_HW, &priv->status))
5940 IWL_DEBUG_RF_KILL("Can not turn radio back on - "
5941 "disabled by SW switch\n");
5942 else
Winkler, Tomas39aadf82008-12-19 10:37:32 +08005943 IWL_WARN(priv, "Radio Frequency Kill Switch is On:\n"
Zhu Yib481de92007-09-25 17:54:57 -07005944 "Kill switch must be turned off for "
5945 "wireless networking to work.\n");
5946 }
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08005947
Zhu Yib481de92007-09-25 17:54:57 -07005948 mutex_unlock(&priv->mutex);
Adel Gadllah80fcc9e2008-07-01 17:49:50 +02005949 iwl3945_rfkill_set_hw_state(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005950}
5951
5952#define IWL_SCAN_CHECK_WATCHDOG (7 * HZ)
5953
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005954static void iwl3945_bg_scan_check(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07005955{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08005956 struct iwl_priv *priv =
5957 container_of(data, struct iwl_priv, scan_check.work);
Zhu Yib481de92007-09-25 17:54:57 -07005958
5959 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
5960 return;
5961
5962 mutex_lock(&priv->mutex);
5963 if (test_bit(STATUS_SCANNING, &priv->status) ||
5964 test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
5965 IWL_DEBUG(IWL_DL_INFO | IWL_DL_SCAN,
5966 "Scan completion watchdog resetting adapter (%dms)\n",
5967 jiffies_to_msecs(IWL_SCAN_CHECK_WATCHDOG));
Mohamed Abbas15e869d2007-10-25 17:15:46 +08005968
Zhu Yib481de92007-09-25 17:54:57 -07005969 if (!test_bit(STATUS_EXIT_PENDING, &priv->status))
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005970 iwl3945_send_scan_abort(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005971 }
5972 mutex_unlock(&priv->mutex);
5973}
5974
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005975static void iwl3945_bg_request_scan(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07005976{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08005977 struct iwl_priv *priv =
5978 container_of(data, struct iwl_priv, request_scan);
Winkler, Tomasc2d79b482008-12-19 10:37:34 +08005979 struct iwl_host_cmd cmd = {
Zhu Yib481de92007-09-25 17:54:57 -07005980 .id = REPLY_SCAN_CMD,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005981 .len = sizeof(struct iwl3945_scan_cmd),
Zhu Yib481de92007-09-25 17:54:57 -07005982 .meta.flags = CMD_SIZE_HUGE,
5983 };
5984 int rc = 0;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005985 struct iwl3945_scan_cmd *scan;
Zhu Yib481de92007-09-25 17:54:57 -07005986 struct ieee80211_conf *conf = NULL;
Abhijeet Kolekarf9340522008-09-03 11:26:58 +08005987 u8 n_probes = 2;
Johannes Berg8318d782008-01-24 19:38:38 +01005988 enum ieee80211_band band;
John W. Linville9387b7c2008-09-30 20:59:05 -04005989 DECLARE_SSID_BUF(ssid);
Zhu Yib481de92007-09-25 17:54:57 -07005990
5991 conf = ieee80211_get_hw_conf(priv->hw);
5992
5993 mutex_lock(&priv->mutex);
5994
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005995 if (!iwl3945_is_ready(priv)) {
Winkler, Tomas39aadf82008-12-19 10:37:32 +08005996 IWL_WARN(priv, "request scan called when driver not ready.\n");
Zhu Yib481de92007-09-25 17:54:57 -07005997 goto done;
5998 }
5999
Tomas Winklera96a27f2008-10-23 23:48:56 -07006000 /* Make sure the scan wasn't canceled before this queued work
Zhu Yib481de92007-09-25 17:54:57 -07006001 * was given the chance to run... */
6002 if (!test_bit(STATUS_SCANNING, &priv->status))
6003 goto done;
6004
6005 /* This should never be called or scheduled if there is currently
6006 * a scan active in the hardware. */
6007 if (test_bit(STATUS_SCAN_HW, &priv->status)) {
6008 IWL_DEBUG_INFO("Multiple concurrent scan requests in parallel. "
6009 "Ignoring second request.\n");
6010 rc = -EIO;
6011 goto done;
6012 }
6013
6014 if (test_bit(STATUS_EXIT_PENDING, &priv->status)) {
6015 IWL_DEBUG_SCAN("Aborting scan due to device shutdown\n");
6016 goto done;
6017 }
6018
6019 if (test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
6020 IWL_DEBUG_HC("Scan request while abort pending. Queuing.\n");
6021 goto done;
6022 }
6023
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006024 if (iwl3945_is_rfkill(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07006025 IWL_DEBUG_HC("Aborting scan due to RF Kill activation\n");
6026 goto done;
6027 }
6028
6029 if (!test_bit(STATUS_READY, &priv->status)) {
6030 IWL_DEBUG_HC("Scan request while uninitialized. Queuing.\n");
6031 goto done;
6032 }
6033
6034 if (!priv->scan_bands) {
6035 IWL_DEBUG_HC("Aborting scan due to no requested bands\n");
6036 goto done;
6037 }
6038
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08006039 if (!priv->scan39) {
6040 priv->scan39 = kmalloc(sizeof(struct iwl3945_scan_cmd) +
Zhu Yib481de92007-09-25 17:54:57 -07006041 IWL_MAX_SCAN_SIZE, GFP_KERNEL);
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08006042 if (!priv->scan39) {
Zhu Yib481de92007-09-25 17:54:57 -07006043 rc = -ENOMEM;
6044 goto done;
6045 }
6046 }
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08006047 scan = priv->scan39;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006048 memset(scan, 0, sizeof(struct iwl3945_scan_cmd) + IWL_MAX_SCAN_SIZE);
Zhu Yib481de92007-09-25 17:54:57 -07006049
6050 scan->quiet_plcp_th = IWL_PLCP_QUIET_THRESH;
6051 scan->quiet_time = IWL_ACTIVE_QUIET_TIME;
6052
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006053 if (iwl3945_is_associated(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07006054 u16 interval = 0;
6055 u32 extra;
6056 u32 suspend_time = 100;
6057 u32 scan_suspend_time = 100;
6058 unsigned long flags;
6059
6060 IWL_DEBUG_INFO("Scanning while associated...\n");
6061
6062 spin_lock_irqsave(&priv->lock, flags);
6063 interval = priv->beacon_int;
6064 spin_unlock_irqrestore(&priv->lock, flags);
6065
6066 scan->suspend_time = 0;
Mohamed Abbas15e869d2007-10-25 17:15:46 +08006067 scan->max_out_time = cpu_to_le32(200 * 1024);
Zhu Yib481de92007-09-25 17:54:57 -07006068 if (!interval)
6069 interval = suspend_time;
6070 /*
6071 * suspend time format:
6072 * 0-19: beacon interval in usec (time before exec.)
6073 * 20-23: 0
6074 * 24-31: number of beacons (suspend between channels)
6075 */
6076
6077 extra = (suspend_time / interval) << 24;
6078 scan_suspend_time = 0xFF0FFFFF &
6079 (extra | ((suspend_time % interval) * 1024));
6080
6081 scan->suspend_time = cpu_to_le32(scan_suspend_time);
6082 IWL_DEBUG_SCAN("suspend_time 0x%X beacon interval %d\n",
6083 scan_suspend_time, interval);
6084 }
6085
6086 /* We should add the ability for user to lock to PASSIVE ONLY */
6087 if (priv->one_direct_scan) {
6088 IWL_DEBUG_SCAN
6089 ("Kicking off one direct scan for '%s'\n",
John W. Linville9387b7c2008-09-30 20:59:05 -04006090 print_ssid(ssid, priv->direct_ssid,
6091 priv->direct_ssid_len));
Zhu Yib481de92007-09-25 17:54:57 -07006092 scan->direct_scan[0].id = WLAN_EID_SSID;
6093 scan->direct_scan[0].len = priv->direct_ssid_len;
6094 memcpy(scan->direct_scan[0].ssid,
6095 priv->direct_ssid, priv->direct_ssid_len);
Abhijeet Kolekarf9340522008-09-03 11:26:58 +08006096 n_probes++;
Abhijeet Kolekarf9340522008-09-03 11:26:58 +08006097 } else
Bill Moss786b4552008-04-17 16:03:40 -07006098 IWL_DEBUG_SCAN("Kicking off one indirect scan.\n");
Zhu Yib481de92007-09-25 17:54:57 -07006099
6100 /* We don't build a direct scan probe request; the uCode will do
6101 * that based on the direct_mask added to each channel entry */
6102 scan->tx_cmd.len = cpu_to_le16(
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006103 iwl3945_fill_probe_req(priv, (struct ieee80211_mgmt *)scan->data,
Johannes Berg430cfe92008-10-28 18:06:02 +01006104 IWL_MAX_SCAN_SIZE - sizeof(*scan)));
Zhu Yib481de92007-09-25 17:54:57 -07006105 scan->tx_cmd.tx_flags = TX_CMD_FLG_SEQ_CTL_MSK;
Abhijeet Kolekar3832ec92008-12-19 10:37:26 +08006106 scan->tx_cmd.sta_id = priv->hw_params.bcast_sta_id;
Zhu Yib481de92007-09-25 17:54:57 -07006107 scan->tx_cmd.stop_time.life_time = TX_CMD_LIFE_TIME_INFINITE;
6108
6109 /* flags + rate selection */
6110
Ron Rindjunsky66b50042008-06-25 16:46:31 +08006111 if (priv->scan_bands & BIT(IEEE80211_BAND_2GHZ)) {
Zhu Yib481de92007-09-25 17:54:57 -07006112 scan->flags = RXON_FLG_BAND_24G_MSK | RXON_FLG_AUTO_DETECT_MSK;
6113 scan->tx_cmd.rate = IWL_RATE_1M_PLCP;
6114 scan->good_CRC_th = 0;
Johannes Berg8318d782008-01-24 19:38:38 +01006115 band = IEEE80211_BAND_2GHZ;
Ron Rindjunsky66b50042008-06-25 16:46:31 +08006116 } else if (priv->scan_bands & BIT(IEEE80211_BAND_5GHZ)) {
Zhu Yib481de92007-09-25 17:54:57 -07006117 scan->tx_cmd.rate = IWL_RATE_6M_PLCP;
6118 scan->good_CRC_th = IWL_GOOD_CRC_TH;
Johannes Berg8318d782008-01-24 19:38:38 +01006119 band = IEEE80211_BAND_5GHZ;
Ron Rindjunsky66b50042008-06-25 16:46:31 +08006120 } else {
Winkler, Tomas39aadf82008-12-19 10:37:32 +08006121 IWL_WARN(priv, "Invalid scan band count\n");
Zhu Yib481de92007-09-25 17:54:57 -07006122 goto done;
6123 }
6124
6125 /* select Rx antennas */
6126 scan->flags |= iwl3945_get_antenna_flags(priv);
6127
Johannes Berg05c914f2008-09-11 00:01:58 +02006128 if (priv->iw_mode == NL80211_IFTYPE_MONITOR)
Zhu Yib481de92007-09-25 17:54:57 -07006129 scan->filter_flags = RXON_FILTER_PROMISC_MSK;
6130
Abhijeet Kolekarf9340522008-09-03 11:26:58 +08006131 scan->channel_count =
6132 iwl3945_get_channels_for_scan(priv, band, 1, /* active */
6133 n_probes,
6134 (void *)&scan->data[le16_to_cpu(scan->tx_cmd.len)]);
Zhu Yib481de92007-09-25 17:54:57 -07006135
Reinette Chatre14b54332008-11-04 12:21:35 -08006136 if (scan->channel_count == 0) {
6137 IWL_DEBUG_SCAN("channel count %d\n", scan->channel_count);
6138 goto done;
6139 }
6140
Zhu Yib481de92007-09-25 17:54:57 -07006141 cmd.len += le16_to_cpu(scan->tx_cmd.len) +
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006142 scan->channel_count * sizeof(struct iwl3945_scan_channel);
Zhu Yib481de92007-09-25 17:54:57 -07006143 cmd.data = scan;
6144 scan->len = cpu_to_le16(cmd.len);
6145
6146 set_bit(STATUS_SCAN_HW, &priv->status);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006147 rc = iwl3945_send_cmd_sync(priv, &cmd);
Zhu Yib481de92007-09-25 17:54:57 -07006148 if (rc)
6149 goto done;
6150
6151 queue_delayed_work(priv->workqueue, &priv->scan_check,
6152 IWL_SCAN_CHECK_WATCHDOG);
6153
6154 mutex_unlock(&priv->mutex);
6155 return;
6156
6157 done:
Mohamed Abbas2420ebc2008-11-04 12:21:34 -08006158 /* can not perform scan make sure we clear scanning
6159 * bits from status so next scan request can be performed.
6160 * if we dont clear scanning status bit here all next scan
6161 * will fail
6162 */
6163 clear_bit(STATUS_SCAN_HW, &priv->status);
6164 clear_bit(STATUS_SCANNING, &priv->status);
6165
Ian Schram01ebd062007-10-25 17:15:22 +08006166 /* inform mac80211 scan aborted */
Zhu Yib481de92007-09-25 17:54:57 -07006167 queue_work(priv->workqueue, &priv->scan_completed);
6168 mutex_unlock(&priv->mutex);
6169}
6170
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006171static void iwl3945_bg_up(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07006172{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08006173 struct iwl_priv *priv = container_of(data, struct iwl_priv, up);
Zhu Yib481de92007-09-25 17:54:57 -07006174
6175 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
6176 return;
6177
6178 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006179 __iwl3945_up(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006180 mutex_unlock(&priv->mutex);
Adel Gadllah80fcc9e2008-07-01 17:49:50 +02006181 iwl3945_rfkill_set_hw_state(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006182}
6183
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006184static void iwl3945_bg_restart(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07006185{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08006186 struct iwl_priv *priv = container_of(data, struct iwl_priv, restart);
Zhu Yib481de92007-09-25 17:54:57 -07006187
6188 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
6189 return;
6190
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006191 iwl3945_down(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006192 queue_work(priv->workqueue, &priv->up);
6193}
6194
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006195static void iwl3945_bg_rx_replenish(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07006196{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08006197 struct iwl_priv *priv =
6198 container_of(data, struct iwl_priv, rx_replenish);
Zhu Yib481de92007-09-25 17:54:57 -07006199
6200 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
6201 return;
6202
6203 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006204 iwl3945_rx_replenish(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006205 mutex_unlock(&priv->mutex);
6206}
6207
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08006208#define IWL_DELAY_NEXT_SCAN (HZ*2)
6209
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08006210static void iwl3945_post_associate(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07006211{
Zhu Yib481de92007-09-25 17:54:57 -07006212 int rc = 0;
6213 struct ieee80211_conf *conf = NULL;
6214
Johannes Berg05c914f2008-09-11 00:01:58 +02006215 if (priv->iw_mode == NL80211_IFTYPE_AP) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08006216 IWL_ERR(priv, "%s Should not be called in AP mode\n", __func__);
Zhu Yib481de92007-09-25 17:54:57 -07006217 return;
6218 }
6219
6220
Johannes Berge1749612008-10-27 15:59:26 -07006221 IWL_DEBUG_ASSOC("Associated as %d to: %pM\n",
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08006222 priv->assoc_id, priv->active39_rxon.bssid_addr);
Zhu Yib481de92007-09-25 17:54:57 -07006223
6224 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
6225 return;
6226
Abhijeet Kolekar322a9812008-09-03 11:26:27 +08006227 if (!priv->vif || !priv->is_open)
Mohamed Abbas6ef89d02007-10-25 17:15:47 +08006228 return;
Abhijeet Kolekar322a9812008-09-03 11:26:27 +08006229
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006230 iwl3945_scan_cancel_timeout(priv, 200);
Mohamed Abbas15e869d2007-10-25 17:15:46 +08006231
Zhu Yib481de92007-09-25 17:54:57 -07006232 conf = ieee80211_get_hw_conf(priv->hw);
6233
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08006234 priv->staging39_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006235 iwl3945_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006236
Tomas Winkler28afaf92008-12-19 10:37:06 +08006237 memset(&priv->rxon_timing, 0, sizeof(struct iwl_rxon_time_cmd));
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006238 iwl3945_setup_rxon_timing(priv);
6239 rc = iwl3945_send_cmd_pdu(priv, REPLY_RXON_TIMING,
Zhu Yib481de92007-09-25 17:54:57 -07006240 sizeof(priv->rxon_timing), &priv->rxon_timing);
6241 if (rc)
Winkler, Tomas39aadf82008-12-19 10:37:32 +08006242 IWL_WARN(priv, "REPLY_RXON_TIMING failed - "
Zhu Yib481de92007-09-25 17:54:57 -07006243 "Attempting to continue.\n");
6244
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08006245 priv->staging39_rxon.filter_flags |= RXON_FILTER_ASSOC_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07006246
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08006247 priv->staging39_rxon.assoc_id = cpu_to_le16(priv->assoc_id);
Zhu Yib481de92007-09-25 17:54:57 -07006248
6249 IWL_DEBUG_ASSOC("assoc id %d beacon interval %d\n",
6250 priv->assoc_id, priv->beacon_int);
6251
6252 if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_PREAMBLE)
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 else
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08006255 priv->staging39_rxon.flags &= ~RXON_FLG_SHORT_PREAMBLE_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07006256
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08006257 if (priv->staging39_rxon.flags & RXON_FLG_BAND_24G_MSK) {
Zhu Yib481de92007-09-25 17:54:57 -07006258 if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_SLOT_TIME)
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 else
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08006261 priv->staging39_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07006262
Johannes Berg05c914f2008-09-11 00:01:58 +02006263 if (priv->iw_mode == NL80211_IFTYPE_ADHOC)
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08006264 priv->staging39_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07006265
6266 }
6267
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006268 iwl3945_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006269
6270 switch (priv->iw_mode) {
Johannes Berg05c914f2008-09-11 00:01:58 +02006271 case NL80211_IFTYPE_STATION:
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006272 iwl3945_rate_scale_init(priv->hw, IWL_AP_ID);
Zhu Yib481de92007-09-25 17:54:57 -07006273 break;
6274
Johannes Berg05c914f2008-09-11 00:01:58 +02006275 case NL80211_IFTYPE_ADHOC:
Zhu Yib481de92007-09-25 17:54:57 -07006276
Abhijeet Kolekarce546fd2008-11-19 15:32:22 -08006277 priv->assoc_id = 1;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006278 iwl3945_add_station(priv, priv->bssid, 0, 0);
Zhu Yib481de92007-09-25 17:54:57 -07006279 iwl3945_sync_sta(priv, IWL_STA_ID,
Johannes Berg8318d782008-01-24 19:38:38 +01006280 (priv->band == IEEE80211_BAND_5GHZ) ?
Zhu Yib481de92007-09-25 17:54:57 -07006281 IWL_RATE_6M_PLCP : IWL_RATE_1M_PLCP,
6282 CMD_ASYNC);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006283 iwl3945_rate_scale_init(priv->hw, IWL_STA_ID);
6284 iwl3945_send_beacon_cmd(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006285
6286 break;
6287
6288 default:
Winkler, Tomas15b16872008-12-19 10:37:33 +08006289 IWL_ERR(priv, "%s Should not be called in %d mode\n",
Tomas Winkler3ac7f142008-07-21 02:40:14 +03006290 __func__, priv->iw_mode);
Zhu Yib481de92007-09-25 17:54:57 -07006291 break;
6292 }
6293
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006294 iwl3945_activate_qos(priv, 0);
Ron Rindjunsky292ae172008-02-06 11:20:39 -08006295
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08006296 /* we have just associated, don't start scan too early */
6297 priv->next_scan_jiffies = jiffies + IWL_DELAY_NEXT_SCAN;
Abhijeet Kolekarcd56d332008-09-03 11:26:21 +08006298}
6299
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006300static void iwl3945_bg_abort_scan(struct work_struct *work)
Zhu Yib481de92007-09-25 17:54:57 -07006301{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08006302 struct iwl_priv *priv = container_of(work, struct iwl_priv, abort_scan);
Zhu Yib481de92007-09-25 17:54:57 -07006303
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006304 if (!iwl3945_is_ready(priv))
Zhu Yib481de92007-09-25 17:54:57 -07006305 return;
6306
6307 mutex_lock(&priv->mutex);
6308
6309 set_bit(STATUS_SCAN_ABORTING, &priv->status);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006310 iwl3945_send_scan_abort(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006311
6312 mutex_unlock(&priv->mutex);
6313}
6314
Johannes Berge8975582008-10-09 12:18:51 +02006315static int iwl3945_mac_config(struct ieee80211_hw *hw, u32 changed);
Zhu Yi76bb77e2007-11-22 10:53:22 +08006316
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006317static void iwl3945_bg_scan_completed(struct work_struct *work)
Zhu Yib481de92007-09-25 17:54:57 -07006318{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08006319 struct iwl_priv *priv =
6320 container_of(work, struct iwl_priv, scan_completed);
Zhu Yib481de92007-09-25 17:54:57 -07006321
6322 IWL_DEBUG(IWL_DL_INFO | IWL_DL_SCAN, "SCAN complete scan\n");
6323
6324 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
6325 return;
6326
Zhu Yia0646472007-12-20 14:10:01 +08006327 if (test_bit(STATUS_CONF_PENDING, &priv->status))
Johannes Berge8975582008-10-09 12:18:51 +02006328 iwl3945_mac_config(priv->hw, 0);
Zhu Yi76bb77e2007-11-22 10:53:22 +08006329
Zhu Yib481de92007-09-25 17:54:57 -07006330 ieee80211_scan_completed(priv->hw);
6331
6332 /* Since setting the TXPOWER may have been deferred while
6333 * performing the scan, fire one off */
6334 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006335 iwl3945_hw_reg_send_txpower(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006336 mutex_unlock(&priv->mutex);
6337}
6338
6339/*****************************************************************************
6340 *
6341 * mac80211 entry point functions
6342 *
6343 *****************************************************************************/
6344
Zhu Yi5a66926a2008-01-14 17:46:18 -08006345#define UCODE_READY_TIMEOUT (2 * HZ)
6346
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006347static int iwl3945_mac_start(struct ieee80211_hw *hw)
Zhu Yib481de92007-09-25 17:54:57 -07006348{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08006349 struct iwl_priv *priv = hw->priv;
Zhu Yi5a66926a2008-01-14 17:46:18 -08006350 int ret;
Zhu Yib481de92007-09-25 17:54:57 -07006351
6352 IWL_DEBUG_MAC80211("enter\n");
6353
Zhu Yi5a66926a2008-01-14 17:46:18 -08006354 if (pci_enable_device(priv->pci_dev)) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08006355 IWL_ERR(priv, "Fail to pci_enable_device\n");
Zhu Yi5a66926a2008-01-14 17:46:18 -08006356 return -ENODEV;
6357 }
6358 pci_restore_state(priv->pci_dev);
6359 pci_enable_msi(priv->pci_dev);
6360
6361 ret = request_irq(priv->pci_dev->irq, iwl3945_isr, IRQF_SHARED,
6362 DRV_NAME, priv);
6363 if (ret) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08006364 IWL_ERR(priv, "Error allocating IRQ %d\n", priv->pci_dev->irq);
Zhu Yi5a66926a2008-01-14 17:46:18 -08006365 goto out_disable_msi;
6366 }
6367
Zhu Yib481de92007-09-25 17:54:57 -07006368 /* we should be verifying the device is ready to be opened */
6369 mutex_lock(&priv->mutex);
6370
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08006371 memset(&priv->staging39_rxon, 0, sizeof(struct iwl3945_rxon_cmd));
Zhu Yi5a66926a2008-01-14 17:46:18 -08006372 /* fetch ucode file from disk, alloc and copy to bus-master buffers ...
6373 * ucode filename and max sizes are card-specific. */
6374
6375 if (!priv->ucode_code.len) {
6376 ret = iwl3945_read_ucode(priv);
6377 if (ret) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08006378 IWL_ERR(priv, "Could not read microcode: %d\n", ret);
Zhu Yi5a66926a2008-01-14 17:46:18 -08006379 mutex_unlock(&priv->mutex);
6380 goto out_release_irq;
6381 }
6382 }
6383
Zhu Yie655b9f2008-01-24 02:19:38 -08006384 ret = __iwl3945_up(priv);
Zhu Yi5a66926a2008-01-14 17:46:18 -08006385
Zhu Yib481de92007-09-25 17:54:57 -07006386 mutex_unlock(&priv->mutex);
Zhu Yi5a66926a2008-01-14 17:46:18 -08006387
Adel Gadllah80fcc9e2008-07-01 17:49:50 +02006388 iwl3945_rfkill_set_hw_state(priv);
6389
Zhu Yie655b9f2008-01-24 02:19:38 -08006390 if (ret)
6391 goto out_release_irq;
6392
6393 IWL_DEBUG_INFO("Start UP work.\n");
6394
6395 if (test_bit(STATUS_IN_SUSPEND, &priv->status))
6396 return 0;
6397
Zhu Yi5a66926a2008-01-14 17:46:18 -08006398 /* Wait for START_ALIVE from ucode. Otherwise callbacks from
6399 * mac80211 will not be run successfully. */
6400 ret = wait_event_interruptible_timeout(priv->wait_command_queue,
6401 test_bit(STATUS_READY, &priv->status),
6402 UCODE_READY_TIMEOUT);
6403 if (!ret) {
6404 if (!test_bit(STATUS_READY, &priv->status)) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08006405 IWL_ERR(priv,
6406 "Wait for START_ALIVE timeout after %dms.\n",
6407 jiffies_to_msecs(UCODE_READY_TIMEOUT));
Zhu Yi5a66926a2008-01-14 17:46:18 -08006408 ret = -ETIMEDOUT;
6409 goto out_release_irq;
6410 }
6411 }
6412
Zhu Yie655b9f2008-01-24 02:19:38 -08006413 priv->is_open = 1;
Zhu Yib481de92007-09-25 17:54:57 -07006414 IWL_DEBUG_MAC80211("leave\n");
6415 return 0;
Zhu Yi5a66926a2008-01-14 17:46:18 -08006416
6417out_release_irq:
6418 free_irq(priv->pci_dev->irq, priv);
6419out_disable_msi:
6420 pci_disable_msi(priv->pci_dev);
Zhu Yie655b9f2008-01-24 02:19:38 -08006421 pci_disable_device(priv->pci_dev);
6422 priv->is_open = 0;
6423 IWL_DEBUG_MAC80211("leave - failed\n");
Zhu Yi5a66926a2008-01-14 17:46:18 -08006424 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07006425}
6426
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006427static void iwl3945_mac_stop(struct ieee80211_hw *hw)
Zhu Yib481de92007-09-25 17:54:57 -07006428{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08006429 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07006430
6431 IWL_DEBUG_MAC80211("enter\n");
Mohamed Abbas6ef89d02007-10-25 17:15:47 +08006432
Zhu Yie655b9f2008-01-24 02:19:38 -08006433 if (!priv->is_open) {
6434 IWL_DEBUG_MAC80211("leave - skip\n");
6435 return;
6436 }
6437
Zhu Yib481de92007-09-25 17:54:57 -07006438 priv->is_open = 0;
Zhu Yi5a66926a2008-01-14 17:46:18 -08006439
6440 if (iwl3945_is_ready_rf(priv)) {
Zhu Yie655b9f2008-01-24 02:19:38 -08006441 /* stop mac, cancel any scan request and clear
6442 * RXON_FILTER_ASSOC_MSK BIT
6443 */
Zhu Yi5a66926a2008-01-14 17:46:18 -08006444 mutex_lock(&priv->mutex);
6445 iwl3945_scan_cancel_timeout(priv, 100);
Mohamed Abbasfde35712007-11-29 11:10:15 +08006446 mutex_unlock(&priv->mutex);
Mohamed Abbasfde35712007-11-29 11:10:15 +08006447 }
6448
Zhu Yi5a66926a2008-01-14 17:46:18 -08006449 iwl3945_down(priv);
6450
6451 flush_workqueue(priv->workqueue);
6452 free_irq(priv->pci_dev->irq, priv);
6453 pci_disable_msi(priv->pci_dev);
6454 pci_save_state(priv->pci_dev);
6455 pci_disable_device(priv->pci_dev);
Mohamed Abbas6ef89d02007-10-25 17:15:47 +08006456
Zhu Yib481de92007-09-25 17:54:57 -07006457 IWL_DEBUG_MAC80211("leave\n");
Zhu Yib481de92007-09-25 17:54:57 -07006458}
6459
Johannes Berge039fa42008-05-15 12:55:29 +02006460static int iwl3945_mac_tx(struct ieee80211_hw *hw, struct sk_buff *skb)
Zhu Yib481de92007-09-25 17:54:57 -07006461{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08006462 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07006463
6464 IWL_DEBUG_MAC80211("enter\n");
6465
Zhu Yib481de92007-09-25 17:54:57 -07006466 IWL_DEBUG_TX("dev->xmit(%d bytes) at rate 0x%02x\n", skb->len,
Johannes Berge039fa42008-05-15 12:55:29 +02006467 ieee80211_get_tx_rate(hw, IEEE80211_SKB_CB(skb))->bitrate);
Zhu Yib481de92007-09-25 17:54:57 -07006468
Johannes Berge039fa42008-05-15 12:55:29 +02006469 if (iwl3945_tx_skb(priv, skb))
Zhu Yib481de92007-09-25 17:54:57 -07006470 dev_kfree_skb_any(skb);
6471
6472 IWL_DEBUG_MAC80211("leave\n");
Reinette Chatre637f8832009-01-19 15:30:32 -08006473 return NETDEV_TX_OK;
Zhu Yib481de92007-09-25 17:54:57 -07006474}
6475
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006476static int iwl3945_mac_add_interface(struct ieee80211_hw *hw,
Zhu Yib481de92007-09-25 17:54:57 -07006477 struct ieee80211_if_init_conf *conf)
6478{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08006479 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07006480 unsigned long flags;
6481
Johannes Berg32bfd352007-12-19 01:31:26 +01006482 IWL_DEBUG_MAC80211("enter: type %d\n", conf->type);
Zhu Yib481de92007-09-25 17:54:57 -07006483
Johannes Berg32bfd352007-12-19 01:31:26 +01006484 if (priv->vif) {
6485 IWL_DEBUG_MAC80211("leave - vif != NULL\n");
Tomas Winkler864792e2007-11-27 21:00:52 +02006486 return -EOPNOTSUPP;
Zhu Yib481de92007-09-25 17:54:57 -07006487 }
6488
6489 spin_lock_irqsave(&priv->lock, flags);
Johannes Berg32bfd352007-12-19 01:31:26 +01006490 priv->vif = conf->vif;
Zhu, Yi60294de2008-10-29 14:05:45 -07006491 priv->iw_mode = conf->type;
Zhu Yib481de92007-09-25 17:54:57 -07006492
6493 spin_unlock_irqrestore(&priv->lock, flags);
6494
6495 mutex_lock(&priv->mutex);
Tomas Winkler864792e2007-11-27 21:00:52 +02006496
6497 if (conf->mac_addr) {
Johannes Berge1749612008-10-27 15:59:26 -07006498 IWL_DEBUG_MAC80211("Set: %pM\n", conf->mac_addr);
Tomas Winkler864792e2007-11-27 21:00:52 +02006499 memcpy(priv->mac_addr, conf->mac_addr, ETH_ALEN);
6500 }
6501
Zhu Yi5a66926a2008-01-14 17:46:18 -08006502 if (iwl3945_is_ready(priv))
6503 iwl3945_set_mode(priv, conf->type);
Zhu Yib481de92007-09-25 17:54:57 -07006504
Zhu Yib481de92007-09-25 17:54:57 -07006505 mutex_unlock(&priv->mutex);
6506
Zhu Yi5a66926a2008-01-14 17:46:18 -08006507 IWL_DEBUG_MAC80211("leave\n");
Zhu Yib481de92007-09-25 17:54:57 -07006508 return 0;
6509}
6510
6511/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006512 * iwl3945_mac_config - mac80211 config callback
Zhu Yib481de92007-09-25 17:54:57 -07006513 *
6514 * We ignore conf->flags & IEEE80211_CONF_SHORT_SLOT_TIME since it seems to
6515 * be set inappropriately and the driver currently sets the hardware up to
6516 * use it whenever needed.
6517 */
Johannes Berge8975582008-10-09 12:18:51 +02006518static int iwl3945_mac_config(struct ieee80211_hw *hw, u32 changed)
Zhu Yib481de92007-09-25 17:54:57 -07006519{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08006520 struct iwl_priv *priv = hw->priv;
Samuel Ortizd20b3c62008-12-19 10:37:15 +08006521 const struct iwl_channel_info *ch_info;
Johannes Berge8975582008-10-09 12:18:51 +02006522 struct ieee80211_conf *conf = &hw->conf;
Zhu Yib481de92007-09-25 17:54:57 -07006523 unsigned long flags;
Zhu Yi76bb77e2007-11-22 10:53:22 +08006524 int ret = 0;
Zhu Yib481de92007-09-25 17:54:57 -07006525
6526 mutex_lock(&priv->mutex);
Johannes Berg8318d782008-01-24 19:38:38 +01006527 IWL_DEBUG_MAC80211("enter to channel %d\n", conf->channel->hw_value);
Zhu Yib481de92007-09-25 17:54:57 -07006528
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006529 if (!iwl3945_is_ready(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07006530 IWL_DEBUG_MAC80211("leave - not ready\n");
Zhu Yi76bb77e2007-11-22 10:53:22 +08006531 ret = -EIO;
6532 goto out;
Zhu Yib481de92007-09-25 17:54:57 -07006533 }
6534
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006535 if (unlikely(!iwl3945_param_disable_hw_scan &&
Zhu Yib481de92007-09-25 17:54:57 -07006536 test_bit(STATUS_SCANNING, &priv->status))) {
Zhu Yia0646472007-12-20 14:10:01 +08006537 IWL_DEBUG_MAC80211("leave - scanning\n");
6538 set_bit(STATUS_CONF_PENDING, &priv->status);
Zhu Yib481de92007-09-25 17:54:57 -07006539 mutex_unlock(&priv->mutex);
Zhu Yia0646472007-12-20 14:10:01 +08006540 return 0;
Zhu Yib481de92007-09-25 17:54:57 -07006541 }
6542
6543 spin_lock_irqsave(&priv->lock, flags);
6544
Johannes Berg8318d782008-01-24 19:38:38 +01006545 ch_info = iwl3945_get_channel_info(priv, conf->channel->band,
6546 conf->channel->hw_value);
Zhu Yib481de92007-09-25 17:54:57 -07006547 if (!is_channel_valid(ch_info)) {
Ron Rindjunsky66b50042008-06-25 16:46:31 +08006548 IWL_DEBUG_SCAN("Channel %d [%d] is INVALID for this band.\n",
Johannes Berg8318d782008-01-24 19:38:38 +01006549 conf->channel->hw_value, conf->channel->band);
Zhu Yib481de92007-09-25 17:54:57 -07006550 IWL_DEBUG_MAC80211("leave - invalid channel\n");
6551 spin_unlock_irqrestore(&priv->lock, flags);
Zhu Yi76bb77e2007-11-22 10:53:22 +08006552 ret = -EINVAL;
6553 goto out;
Zhu Yib481de92007-09-25 17:54:57 -07006554 }
6555
Johannes Berg8318d782008-01-24 19:38:38 +01006556 iwl3945_set_rxon_channel(priv, conf->channel->band, conf->channel->hw_value);
Zhu Yib481de92007-09-25 17:54:57 -07006557
Johannes Berg8318d782008-01-24 19:38:38 +01006558 iwl3945_set_flags_for_phymode(priv, conf->channel->band);
Zhu Yib481de92007-09-25 17:54:57 -07006559
6560 /* The list of supported rates and rate mask can be different
6561 * for each phymode; since the phymode may have changed, reset
6562 * the rate mask to what mac80211 lists */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006563 iwl3945_set_rate(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006564
6565 spin_unlock_irqrestore(&priv->lock, flags);
6566
6567#ifdef IEEE80211_CONF_CHANNEL_SWITCH
6568 if (conf->flags & IEEE80211_CONF_CHANNEL_SWITCH) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006569 iwl3945_hw_channel_switch(priv, conf->channel);
Zhu Yi76bb77e2007-11-22 10:53:22 +08006570 goto out;
Zhu Yib481de92007-09-25 17:54:57 -07006571 }
6572#endif
6573
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006574 iwl3945_radio_kill_sw(priv, !conf->radio_enabled);
Zhu Yib481de92007-09-25 17:54:57 -07006575
6576 if (!conf->radio_enabled) {
6577 IWL_DEBUG_MAC80211("leave - radio disabled\n");
Zhu Yi76bb77e2007-11-22 10:53:22 +08006578 goto out;
Zhu Yib481de92007-09-25 17:54:57 -07006579 }
6580
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006581 if (iwl3945_is_rfkill(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07006582 IWL_DEBUG_MAC80211("leave - RF kill\n");
Zhu Yi76bb77e2007-11-22 10:53:22 +08006583 ret = -EIO;
6584 goto out;
Zhu Yib481de92007-09-25 17:54:57 -07006585 }
6586
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006587 iwl3945_set_rate(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006588
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08006589 if (memcmp(&priv->active39_rxon,
6590 &priv->staging39_rxon, sizeof(priv->staging39_rxon)))
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006591 iwl3945_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006592 else
6593 IWL_DEBUG_INFO("No re-sending same RXON configuration.\n");
6594
6595 IWL_DEBUG_MAC80211("leave\n");
6596
Zhu Yi76bb77e2007-11-22 10:53:22 +08006597out:
Zhu Yia0646472007-12-20 14:10:01 +08006598 clear_bit(STATUS_CONF_PENDING, &priv->status);
Zhu Yib481de92007-09-25 17:54:57 -07006599 mutex_unlock(&priv->mutex);
Zhu Yi76bb77e2007-11-22 10:53:22 +08006600 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07006601}
6602
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08006603static void iwl3945_config_ap(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07006604{
6605 int rc = 0;
6606
Maarten Lankhorstd986bcd2008-01-23 10:15:16 -08006607 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
Zhu Yib481de92007-09-25 17:54:57 -07006608 return;
6609
6610 /* The following should be done only at AP bring up */
Ron Rindjunsky5d1e2322008-06-30 17:23:04 +08006611 if (!(iwl3945_is_associated(priv))) {
Zhu Yib481de92007-09-25 17:54:57 -07006612
6613 /* RXON - unassoc (to set timing command) */
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08006614 priv->staging39_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006615 iwl3945_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006616
6617 /* RXON Timing */
Tomas Winkler28afaf92008-12-19 10:37:06 +08006618 memset(&priv->rxon_timing, 0, sizeof(struct iwl_rxon_time_cmd));
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006619 iwl3945_setup_rxon_timing(priv);
6620 rc = iwl3945_send_cmd_pdu(priv, REPLY_RXON_TIMING,
Zhu Yib481de92007-09-25 17:54:57 -07006621 sizeof(priv->rxon_timing), &priv->rxon_timing);
6622 if (rc)
Winkler, Tomas39aadf82008-12-19 10:37:32 +08006623 IWL_WARN(priv, "REPLY_RXON_TIMING failed - "
Zhu Yib481de92007-09-25 17:54:57 -07006624 "Attempting to continue.\n");
6625
6626 /* FIXME: what should be the assoc_id for AP? */
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08006627 priv->staging39_rxon.assoc_id = cpu_to_le16(priv->assoc_id);
Zhu Yib481de92007-09-25 17:54:57 -07006628 if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_PREAMBLE)
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08006629 priv->staging39_rxon.flags |=
Zhu Yib481de92007-09-25 17:54:57 -07006630 RXON_FLG_SHORT_PREAMBLE_MSK;
6631 else
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08006632 priv->staging39_rxon.flags &=
Zhu Yib481de92007-09-25 17:54:57 -07006633 ~RXON_FLG_SHORT_PREAMBLE_MSK;
6634
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08006635 if (priv->staging39_rxon.flags & RXON_FLG_BAND_24G_MSK) {
Zhu Yib481de92007-09-25 17:54:57 -07006636 if (priv->assoc_capability &
6637 WLAN_CAPABILITY_SHORT_SLOT_TIME)
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08006638 priv->staging39_rxon.flags |=
Zhu Yib481de92007-09-25 17:54:57 -07006639 RXON_FLG_SHORT_SLOT_MSK;
6640 else
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08006641 priv->staging39_rxon.flags &=
Zhu Yib481de92007-09-25 17:54:57 -07006642 ~RXON_FLG_SHORT_SLOT_MSK;
6643
Johannes Berg05c914f2008-09-11 00:01:58 +02006644 if (priv->iw_mode == NL80211_IFTYPE_ADHOC)
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08006645 priv->staging39_rxon.flags &=
Zhu Yib481de92007-09-25 17:54:57 -07006646 ~RXON_FLG_SHORT_SLOT_MSK;
6647 }
6648 /* restore RXON assoc */
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08006649 priv->staging39_rxon.filter_flags |= RXON_FILTER_ASSOC_MSK;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006650 iwl3945_commit_rxon(priv);
Kolekar, Abhijeetb5323d32008-12-19 10:37:22 +08006651 iwl3945_add_station(priv, iwl_bcast_addr, 0, 0);
Zhu Yi556f8db2007-09-27 11:27:33 +08006652 }
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006653 iwl3945_send_beacon_cmd(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006654
6655 /* FIXME - we need to add code here to detect a totally new
6656 * configuration, reset the AP, unassoc, rxon timing, assoc,
6657 * clear sta table, add BCAST sta... */
6658}
6659
Johannes Berg32bfd352007-12-19 01:31:26 +01006660static int iwl3945_mac_config_interface(struct ieee80211_hw *hw,
6661 struct ieee80211_vif *vif,
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08006662 struct ieee80211_if_conf *conf)
Zhu Yib481de92007-09-25 17:54:57 -07006663{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08006664 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07006665 int rc;
6666
6667 if (conf == NULL)
6668 return -EIO;
6669
Emmanuel Grumbachb716bb92008-03-04 18:09:32 -08006670 if (priv->vif != vif) {
6671 IWL_DEBUG_MAC80211("leave - priv->vif != vif\n");
Emmanuel Grumbachb716bb92008-03-04 18:09:32 -08006672 return 0;
6673 }
6674
Johannes Berg9d139c82008-07-09 14:40:37 +02006675 /* handle this temporarily here */
Johannes Berg05c914f2008-09-11 00:01:58 +02006676 if (priv->iw_mode == NL80211_IFTYPE_ADHOC &&
Johannes Berg9d139c82008-07-09 14:40:37 +02006677 conf->changed & IEEE80211_IFCC_BEACON) {
6678 struct sk_buff *beacon = ieee80211_beacon_get(hw, vif);
6679 if (!beacon)
6680 return -ENOMEM;
Mohamed Abbas9bdf5ec2008-11-07 09:58:35 -08006681 mutex_lock(&priv->mutex);
Johannes Berg9d139c82008-07-09 14:40:37 +02006682 rc = iwl3945_mac_beacon_update(hw, beacon);
Mohamed Abbas9bdf5ec2008-11-07 09:58:35 -08006683 mutex_unlock(&priv->mutex);
Johannes Berg9d139c82008-07-09 14:40:37 +02006684 if (rc)
6685 return rc;
6686 }
6687
Zhu Yi5a66926a2008-01-14 17:46:18 -08006688 if (!iwl3945_is_alive(priv))
6689 return -EAGAIN;
6690
Zhu Yib481de92007-09-25 17:54:57 -07006691 mutex_lock(&priv->mutex);
6692
Zhu Yib481de92007-09-25 17:54:57 -07006693 if (conf->bssid)
Johannes Berge1749612008-10-27 15:59:26 -07006694 IWL_DEBUG_MAC80211("bssid: %pM\n", conf->bssid);
Zhu Yib481de92007-09-25 17:54:57 -07006695
Johannes Berg4150c572007-09-17 01:29:23 -04006696/*
6697 * very dubious code was here; the probe filtering flag is never set:
6698 *
Zhu Yib481de92007-09-25 17:54:57 -07006699 if (unlikely(test_bit(STATUS_SCANNING, &priv->status)) &&
6700 !(priv->hw->flags & IEEE80211_HW_NO_PROBE_FILTERING)) {
Johannes Berg4150c572007-09-17 01:29:23 -04006701 */
Zhu Yib481de92007-09-25 17:54:57 -07006702
Johannes Berg05c914f2008-09-11 00:01:58 +02006703 if (priv->iw_mode == NL80211_IFTYPE_AP) {
Zhu Yib481de92007-09-25 17:54:57 -07006704 if (!conf->bssid) {
6705 conf->bssid = priv->mac_addr;
6706 memcpy(priv->bssid, priv->mac_addr, ETH_ALEN);
Johannes Berge1749612008-10-27 15:59:26 -07006707 IWL_DEBUG_MAC80211("bssid was set to: %pM\n",
6708 conf->bssid);
Zhu Yib481de92007-09-25 17:54:57 -07006709 }
6710 if (priv->ibss_beacon)
6711 dev_kfree_skb(priv->ibss_beacon);
6712
Johannes Berg9d139c82008-07-09 14:40:37 +02006713 priv->ibss_beacon = ieee80211_beacon_get(hw, vif);
Zhu Yib481de92007-09-25 17:54:57 -07006714 }
6715
Mohamed Abbasfde35712007-11-29 11:10:15 +08006716 if (iwl3945_is_rfkill(priv))
6717 goto done;
6718
Zhu Yib481de92007-09-25 17:54:57 -07006719 if (conf->bssid && !is_zero_ether_addr(conf->bssid) &&
6720 !is_multicast_ether_addr(conf->bssid)) {
6721 /* If there is currently a HW scan going on in the background
6722 * then we need to cancel it else the RXON below will fail. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006723 if (iwl3945_scan_cancel_timeout(priv, 100)) {
Winkler, Tomas39aadf82008-12-19 10:37:32 +08006724 IWL_WARN(priv, "Aborted scan still in progress "
Zhu Yib481de92007-09-25 17:54:57 -07006725 "after 100ms\n");
6726 IWL_DEBUG_MAC80211("leaving - scan abort failed.\n");
6727 mutex_unlock(&priv->mutex);
6728 return -EAGAIN;
6729 }
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08006730 memcpy(priv->staging39_rxon.bssid_addr, conf->bssid, ETH_ALEN);
Zhu Yib481de92007-09-25 17:54:57 -07006731
6732 /* TODO: Audit driver for usage of these members and see
6733 * if mac80211 deprecates them (priv->bssid looks like it
6734 * shouldn't be there, but I haven't scanned the IBSS code
6735 * to verify) - jpk */
6736 memcpy(priv->bssid, conf->bssid, ETH_ALEN);
6737
Johannes Berg05c914f2008-09-11 00:01:58 +02006738 if (priv->iw_mode == NL80211_IFTYPE_AP)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006739 iwl3945_config_ap(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006740 else {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006741 rc = iwl3945_commit_rxon(priv);
Johannes Berg05c914f2008-09-11 00:01:58 +02006742 if ((priv->iw_mode == NL80211_IFTYPE_STATION) && rc)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006743 iwl3945_add_station(priv,
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08006744 priv->active39_rxon.bssid_addr, 1, 0);
Zhu Yib481de92007-09-25 17:54:57 -07006745 }
6746
6747 } else {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006748 iwl3945_scan_cancel_timeout(priv, 100);
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08006749 priv->staging39_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006750 iwl3945_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006751 }
6752
Mohamed Abbasfde35712007-11-29 11:10:15 +08006753 done:
Zhu Yib481de92007-09-25 17:54:57 -07006754 IWL_DEBUG_MAC80211("leave\n");
6755 mutex_unlock(&priv->mutex);
6756
6757 return 0;
6758}
6759
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006760static void iwl3945_configure_filter(struct ieee80211_hw *hw,
Johannes Berg4150c572007-09-17 01:29:23 -04006761 unsigned int changed_flags,
6762 unsigned int *total_flags,
6763 int mc_count, struct dev_addr_list *mc_list)
6764{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08006765 struct iwl_priv *priv = hw->priv;
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08006766 __le32 *filter_flags = &priv->staging39_rxon.filter_flags;
Rick Farrington25b3f572008-06-30 17:23:28 +08006767
Zhu, Yi352bc8d2008-11-12 13:14:09 -08006768 IWL_DEBUG_MAC80211("Enter: changed: 0x%x, total: 0x%x\n",
6769 changed_flags, *total_flags);
6770
6771 if (changed_flags & (FIF_OTHER_BSS | FIF_PROMISC_IN_BSS)) {
6772 if (*total_flags & (FIF_OTHER_BSS | FIF_PROMISC_IN_BSS))
6773 *filter_flags |= RXON_FILTER_PROMISC_MSK;
6774 else
6775 *filter_flags &= ~RXON_FILTER_PROMISC_MSK;
Abhijeet Kolekar5ec03972008-05-05 10:22:48 +08006776 }
Zhu, Yi352bc8d2008-11-12 13:14:09 -08006777 if (changed_flags & FIF_ALLMULTI) {
6778 if (*total_flags & FIF_ALLMULTI)
6779 *filter_flags |= RXON_FILTER_ACCEPT_GRP_MSK;
6780 else
6781 *filter_flags &= ~RXON_FILTER_ACCEPT_GRP_MSK;
6782 }
6783 if (changed_flags & FIF_CONTROL) {
6784 if (*total_flags & FIF_CONTROL)
6785 *filter_flags |= RXON_FILTER_CTL2HOST_MSK;
6786 else
6787 *filter_flags &= ~RXON_FILTER_CTL2HOST_MSK;
6788 }
6789 if (changed_flags & FIF_BCN_PRBRESP_PROMISC) {
6790 if (*total_flags & FIF_BCN_PRBRESP_PROMISC)
6791 *filter_flags |= RXON_FILTER_BCON_AWARE_MSK;
6792 else
6793 *filter_flags &= ~RXON_FILTER_BCON_AWARE_MSK;
6794 }
6795
6796 /* We avoid iwl_commit_rxon here to commit the new filter flags
6797 * since mac80211 will call ieee80211_hw_config immediately.
6798 * (mc_list is not supported at this time). Otherwise, we need to
6799 * queue a background iwl_commit_rxon work.
6800 */
6801
6802 *total_flags &= FIF_OTHER_BSS | FIF_ALLMULTI | FIF_PROMISC_IN_BSS |
Rick Farrington25b3f572008-06-30 17:23:28 +08006803 FIF_BCN_PRBRESP_PROMISC | FIF_CONTROL;
Johannes Berg4150c572007-09-17 01:29:23 -04006804}
6805
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006806static void iwl3945_mac_remove_interface(struct ieee80211_hw *hw,
Zhu Yib481de92007-09-25 17:54:57 -07006807 struct ieee80211_if_init_conf *conf)
6808{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08006809 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07006810
6811 IWL_DEBUG_MAC80211("enter\n");
6812
6813 mutex_lock(&priv->mutex);
Mohamed Abbas6ef89d02007-10-25 17:15:47 +08006814
Mohamed Abbasfde35712007-11-29 11:10:15 +08006815 if (iwl3945_is_ready_rf(priv)) {
6816 iwl3945_scan_cancel_timeout(priv, 100);
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08006817 priv->staging39_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
Mohamed Abbasfde35712007-11-29 11:10:15 +08006818 iwl3945_commit_rxon(priv);
6819 }
Johannes Berg32bfd352007-12-19 01:31:26 +01006820 if (priv->vif == conf->vif) {
6821 priv->vif = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07006822 memset(priv->bssid, 0, ETH_ALEN);
Zhu Yib481de92007-09-25 17:54:57 -07006823 }
6824 mutex_unlock(&priv->mutex);
6825
6826 IWL_DEBUG_MAC80211("leave\n");
Zhu Yib481de92007-09-25 17:54:57 -07006827}
6828
Abhijeet Kolekarcd56d332008-09-03 11:26:21 +08006829#define IWL_DELAY_NEXT_SCAN_AFTER_ASSOC (HZ*6)
6830
6831static void iwl3945_bss_info_changed(struct ieee80211_hw *hw,
6832 struct ieee80211_vif *vif,
6833 struct ieee80211_bss_conf *bss_conf,
6834 u32 changes)
6835{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08006836 struct iwl_priv *priv = hw->priv;
Abhijeet Kolekarcd56d332008-09-03 11:26:21 +08006837
6838 IWL_DEBUG_MAC80211("changes = 0x%X\n", changes);
6839
6840 if (changes & BSS_CHANGED_ERP_PREAMBLE) {
6841 IWL_DEBUG_MAC80211("ERP_PREAMBLE %d\n",
6842 bss_conf->use_short_preamble);
6843 if (bss_conf->use_short_preamble)
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 else
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08006846 priv->staging39_rxon.flags &= ~RXON_FLG_SHORT_PREAMBLE_MSK;
Abhijeet Kolekarcd56d332008-09-03 11:26:21 +08006847 }
6848
6849 if (changes & BSS_CHANGED_ERP_CTS_PROT) {
6850 IWL_DEBUG_MAC80211("ERP_CTS %d\n", bss_conf->use_cts_prot);
6851 if (bss_conf->use_cts_prot && (priv->band != IEEE80211_BAND_5GHZ))
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 else
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08006854 priv->staging39_rxon.flags &= ~RXON_FLG_TGG_PROTECT_MSK;
Abhijeet Kolekarcd56d332008-09-03 11:26:21 +08006855 }
6856
6857 if (changes & BSS_CHANGED_ASSOC) {
6858 IWL_DEBUG_MAC80211("ASSOC %d\n", bss_conf->assoc);
6859 /* This should never happen as this function should
6860 * never be called from interrupt context. */
6861 if (WARN_ON_ONCE(in_interrupt()))
6862 return;
6863 if (bss_conf->assoc) {
6864 priv->assoc_id = bss_conf->aid;
6865 priv->beacon_int = bss_conf->beacon_int;
Tomas Winkler28afaf92008-12-19 10:37:06 +08006866 priv->timestamp = bss_conf->timestamp;
Abhijeet Kolekarcd56d332008-09-03 11:26:21 +08006867 priv->assoc_capability = bss_conf->assoc_capability;
6868 priv->next_scan_jiffies = jiffies +
6869 IWL_DELAY_NEXT_SCAN_AFTER_ASSOC;
6870 mutex_lock(&priv->mutex);
6871 iwl3945_post_associate(priv);
6872 mutex_unlock(&priv->mutex);
6873 } else {
6874 priv->assoc_id = 0;
6875 IWL_DEBUG_MAC80211("DISASSOC %d\n", bss_conf->assoc);
6876 }
6877 } else if (changes && iwl3945_is_associated(priv) && priv->assoc_id) {
6878 IWL_DEBUG_MAC80211("Associated Changes %d\n", changes);
6879 iwl3945_send_rxon_assoc(priv);
6880 }
6881
6882}
6883
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006884static int iwl3945_mac_hw_scan(struct ieee80211_hw *hw, u8 *ssid, size_t len)
Zhu Yib481de92007-09-25 17:54:57 -07006885{
6886 int rc = 0;
6887 unsigned long flags;
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08006888 struct iwl_priv *priv = hw->priv;
John W. Linville9387b7c2008-09-30 20:59:05 -04006889 DECLARE_SSID_BUF(ssid_buf);
Zhu Yib481de92007-09-25 17:54:57 -07006890
6891 IWL_DEBUG_MAC80211("enter\n");
6892
Mohamed Abbas15e869d2007-10-25 17:15:46 +08006893 mutex_lock(&priv->mutex);
Zhu Yib481de92007-09-25 17:54:57 -07006894 spin_lock_irqsave(&priv->lock, flags);
6895
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006896 if (!iwl3945_is_ready_rf(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07006897 rc = -EIO;
6898 IWL_DEBUG_MAC80211("leave - not ready or exit pending\n");
6899 goto out_unlock;
6900 }
6901
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08006902 /* we don't schedule scan within next_scan_jiffies period */
6903 if (priv->next_scan_jiffies &&
6904 time_after(priv->next_scan_jiffies, jiffies)) {
6905 rc = -EAGAIN;
6906 goto out_unlock;
6907 }
Bill Moss15dbf1b2008-05-06 11:05:15 +08006908 /* if we just finished scan ask for delay for a broadcast scan */
6909 if ((len == 0) && priv->last_scan_jiffies &&
6910 time_after(priv->last_scan_jiffies + IWL_DELAY_NEXT_SCAN,
6911 jiffies)) {
Zhu Yib481de92007-09-25 17:54:57 -07006912 rc = -EAGAIN;
6913 goto out_unlock;
6914 }
6915 if (len) {
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08006916 IWL_DEBUG_SCAN("direct scan for %s [%d]\n ",
John W. Linville9387b7c2008-09-30 20:59:05 -04006917 print_ssid(ssid_buf, ssid, len), (int)len);
Zhu Yib481de92007-09-25 17:54:57 -07006918
6919 priv->one_direct_scan = 1;
6920 priv->direct_ssid_len = (u8)
6921 min((u8) len, (u8) IW_ESSID_MAX_SIZE);
6922 memcpy(priv->direct_ssid, ssid, priv->direct_ssid_len);
Mohamed Abbas6ef89d02007-10-25 17:15:47 +08006923 } else
6924 priv->one_direct_scan = 0;
Zhu Yib481de92007-09-25 17:54:57 -07006925
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006926 rc = iwl3945_scan_initiate(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006927
6928 IWL_DEBUG_MAC80211("leave\n");
6929
6930out_unlock:
6931 spin_unlock_irqrestore(&priv->lock, flags);
Mohamed Abbas15e869d2007-10-25 17:15:46 +08006932 mutex_unlock(&priv->mutex);
Zhu Yib481de92007-09-25 17:54:57 -07006933
6934 return rc;
6935}
6936
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006937static int iwl3945_mac_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
Zhu Yib481de92007-09-25 17:54:57 -07006938 const u8 *local_addr, const u8 *addr,
6939 struct ieee80211_key_conf *key)
6940{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08006941 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07006942 int rc = 0;
6943 u8 sta_id;
6944
6945 IWL_DEBUG_MAC80211("enter\n");
6946
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006947 if (!iwl3945_param_hwcrypto) {
Zhu Yib481de92007-09-25 17:54:57 -07006948 IWL_DEBUG_MAC80211("leave - hwcrypto disabled\n");
6949 return -EOPNOTSUPP;
6950 }
6951
6952 if (is_zero_ether_addr(addr))
6953 /* only support pairwise keys */
6954 return -EOPNOTSUPP;
6955
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006956 sta_id = iwl3945_hw_find_station(priv, addr);
Zhu Yib481de92007-09-25 17:54:57 -07006957 if (sta_id == IWL_INVALID_STATION) {
Johannes Berge1749612008-10-27 15:59:26 -07006958 IWL_DEBUG_MAC80211("leave - %pM not in station map.\n",
6959 addr);
Zhu Yib481de92007-09-25 17:54:57 -07006960 return -EINVAL;
6961 }
6962
6963 mutex_lock(&priv->mutex);
6964
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006965 iwl3945_scan_cancel_timeout(priv, 100);
Mohamed Abbas15e869d2007-10-25 17:15:46 +08006966
Zhu Yib481de92007-09-25 17:54:57 -07006967 switch (cmd) {
6968 case SET_KEY:
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006969 rc = iwl3945_update_sta_key_info(priv, key, sta_id);
Zhu Yib481de92007-09-25 17:54:57 -07006970 if (!rc) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006971 iwl3945_set_rxon_hwcrypto(priv, 1);
6972 iwl3945_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006973 key->hw_key_idx = sta_id;
6974 IWL_DEBUG_MAC80211("set_key success, using hwcrypto\n");
6975 key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
6976 }
6977 break;
6978 case DISABLE_KEY:
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006979 rc = iwl3945_clear_sta_key_info(priv, sta_id);
Zhu Yib481de92007-09-25 17:54:57 -07006980 if (!rc) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006981 iwl3945_set_rxon_hwcrypto(priv, 0);
6982 iwl3945_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006983 IWL_DEBUG_MAC80211("disable hwcrypto key\n");
6984 }
6985 break;
6986 default:
6987 rc = -EINVAL;
6988 }
6989
6990 IWL_DEBUG_MAC80211("leave\n");
6991 mutex_unlock(&priv->mutex);
6992
6993 return rc;
6994}
6995
Johannes Berge100bb62008-04-30 18:51:21 +02006996static int iwl3945_mac_conf_tx(struct ieee80211_hw *hw, u16 queue,
Zhu Yib481de92007-09-25 17:54:57 -07006997 const struct ieee80211_tx_queue_params *params)
6998{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08006999 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07007000 unsigned long flags;
7001 int q;
Zhu Yib481de92007-09-25 17:54:57 -07007002
7003 IWL_DEBUG_MAC80211("enter\n");
7004
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007005 if (!iwl3945_is_ready_rf(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07007006 IWL_DEBUG_MAC80211("leave - RF not ready\n");
7007 return -EIO;
7008 }
7009
7010 if (queue >= AC_NUM) {
7011 IWL_DEBUG_MAC80211("leave - queue >= AC_NUM %d\n", queue);
7012 return 0;
7013 }
7014
Zhu Yib481de92007-09-25 17:54:57 -07007015 q = AC_NUM - 1 - queue;
7016
7017 spin_lock_irqsave(&priv->lock, flags);
7018
7019 priv->qos_data.def_qos_parm.ac[q].cw_min = cpu_to_le16(params->cw_min);
7020 priv->qos_data.def_qos_parm.ac[q].cw_max = cpu_to_le16(params->cw_max);
7021 priv->qos_data.def_qos_parm.ac[q].aifsn = params->aifs;
7022 priv->qos_data.def_qos_parm.ac[q].edca_txop =
Johannes Berg3330d7b2008-02-10 16:49:38 +01007023 cpu_to_le16((params->txop * 32));
Zhu Yib481de92007-09-25 17:54:57 -07007024
7025 priv->qos_data.def_qos_parm.ac[q].reserved1 = 0;
7026 priv->qos_data.qos_active = 1;
7027
7028 spin_unlock_irqrestore(&priv->lock, flags);
7029
7030 mutex_lock(&priv->mutex);
Johannes Berg05c914f2008-09-11 00:01:58 +02007031 if (priv->iw_mode == NL80211_IFTYPE_AP)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007032 iwl3945_activate_qos(priv, 1);
7033 else if (priv->assoc_id && iwl3945_is_associated(priv))
7034 iwl3945_activate_qos(priv, 0);
Zhu Yib481de92007-09-25 17:54:57 -07007035
7036 mutex_unlock(&priv->mutex);
7037
Zhu Yib481de92007-09-25 17:54:57 -07007038 IWL_DEBUG_MAC80211("leave\n");
7039 return 0;
7040}
7041
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007042static int iwl3945_mac_get_tx_stats(struct ieee80211_hw *hw,
Zhu Yib481de92007-09-25 17:54:57 -07007043 struct ieee80211_tx_queue_stats *stats)
7044{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08007045 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07007046 int i, avail;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007047 struct iwl3945_tx_queue *txq;
Samuel Ortizd20b3c62008-12-19 10:37:15 +08007048 struct iwl_queue *q;
Zhu Yib481de92007-09-25 17:54:57 -07007049 unsigned long flags;
7050
7051 IWL_DEBUG_MAC80211("enter\n");
7052
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007053 if (!iwl3945_is_ready_rf(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07007054 IWL_DEBUG_MAC80211("leave - RF not ready\n");
7055 return -EIO;
7056 }
7057
7058 spin_lock_irqsave(&priv->lock, flags);
7059
7060 for (i = 0; i < AC_NUM; i++) {
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08007061 txq = &priv->txq39[i];
Zhu Yib481de92007-09-25 17:54:57 -07007062 q = &txq->q;
Samuel Ortizd20b3c62008-12-19 10:37:15 +08007063 avail = iwl_queue_space(q);
Zhu Yib481de92007-09-25 17:54:57 -07007064
Johannes Berg57ffc582008-04-29 17:18:59 +02007065 stats[i].len = q->n_window - avail;
7066 stats[i].limit = q->n_window - q->high_mark;
7067 stats[i].count = q->n_window;
Zhu Yib481de92007-09-25 17:54:57 -07007068
7069 }
7070 spin_unlock_irqrestore(&priv->lock, flags);
7071
7072 IWL_DEBUG_MAC80211("leave\n");
7073
7074 return 0;
7075}
7076
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007077static void iwl3945_mac_reset_tsf(struct ieee80211_hw *hw)
Zhu Yib481de92007-09-25 17:54:57 -07007078{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08007079 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07007080 unsigned long flags;
7081
7082 mutex_lock(&priv->mutex);
7083 IWL_DEBUG_MAC80211("enter\n");
7084
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007085 iwl3945_reset_qos(priv);
Ron Rindjunsky292ae172008-02-06 11:20:39 -08007086
Zhu Yib481de92007-09-25 17:54:57 -07007087 spin_lock_irqsave(&priv->lock, flags);
7088 priv->assoc_id = 0;
7089 priv->assoc_capability = 0;
7090 priv->call_post_assoc_from_beacon = 0;
7091
7092 /* new association get rid of ibss beacon skb */
7093 if (priv->ibss_beacon)
7094 dev_kfree_skb(priv->ibss_beacon);
7095
7096 priv->ibss_beacon = NULL;
7097
7098 priv->beacon_int = priv->hw->conf.beacon_int;
Tomas Winkler28afaf92008-12-19 10:37:06 +08007099 priv->timestamp = 0;
Johannes Berg05c914f2008-09-11 00:01:58 +02007100 if ((priv->iw_mode == NL80211_IFTYPE_STATION))
Zhu Yib481de92007-09-25 17:54:57 -07007101 priv->beacon_int = 0;
7102
7103 spin_unlock_irqrestore(&priv->lock, flags);
7104
Mohamed Abbasfde35712007-11-29 11:10:15 +08007105 if (!iwl3945_is_ready_rf(priv)) {
7106 IWL_DEBUG_MAC80211("leave - not ready\n");
7107 mutex_unlock(&priv->mutex);
7108 return;
7109 }
7110
Mohamed Abbas15e869d2007-10-25 17:15:46 +08007111 /* we are restarting association process
7112 * clear RXON_FILTER_ASSOC_MSK bit
7113 */
Johannes Berg05c914f2008-09-11 00:01:58 +02007114 if (priv->iw_mode != NL80211_IFTYPE_AP) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007115 iwl3945_scan_cancel_timeout(priv, 100);
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08007116 priv->staging39_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007117 iwl3945_commit_rxon(priv);
Mohamed Abbas15e869d2007-10-25 17:15:46 +08007118 }
7119
Zhu Yib481de92007-09-25 17:54:57 -07007120 /* Per mac80211.h: This is only used in IBSS mode... */
Johannes Berg05c914f2008-09-11 00:01:58 +02007121 if (priv->iw_mode != NL80211_IFTYPE_ADHOC) {
Mohamed Abbas15e869d2007-10-25 17:15:46 +08007122
Zhu Yib481de92007-09-25 17:54:57 -07007123 IWL_DEBUG_MAC80211("leave - not in IBSS\n");
7124 mutex_unlock(&priv->mutex);
7125 return;
7126 }
7127
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007128 iwl3945_set_rate(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007129
7130 mutex_unlock(&priv->mutex);
7131
7132 IWL_DEBUG_MAC80211("leave\n");
7133
7134}
7135
Johannes Berge039fa42008-05-15 12:55:29 +02007136static int iwl3945_mac_beacon_update(struct ieee80211_hw *hw, struct sk_buff *skb)
Zhu Yib481de92007-09-25 17:54:57 -07007137{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08007138 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07007139 unsigned long flags;
7140
Zhu Yib481de92007-09-25 17:54:57 -07007141 IWL_DEBUG_MAC80211("enter\n");
7142
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007143 if (!iwl3945_is_ready_rf(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07007144 IWL_DEBUG_MAC80211("leave - RF not ready\n");
Zhu Yib481de92007-09-25 17:54:57 -07007145 return -EIO;
7146 }
7147
Johannes Berg05c914f2008-09-11 00:01:58 +02007148 if (priv->iw_mode != NL80211_IFTYPE_ADHOC) {
Zhu Yib481de92007-09-25 17:54:57 -07007149 IWL_DEBUG_MAC80211("leave - not IBSS\n");
Zhu Yib481de92007-09-25 17:54:57 -07007150 return -EIO;
7151 }
7152
7153 spin_lock_irqsave(&priv->lock, flags);
7154
7155 if (priv->ibss_beacon)
7156 dev_kfree_skb(priv->ibss_beacon);
7157
7158 priv->ibss_beacon = skb;
7159
7160 priv->assoc_id = 0;
7161
7162 IWL_DEBUG_MAC80211("leave\n");
7163 spin_unlock_irqrestore(&priv->lock, flags);
7164
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007165 iwl3945_reset_qos(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007166
Abhijeet Kolekardc4b1e72008-09-03 11:26:30 +08007167 iwl3945_post_associate(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007168
Zhu Yib481de92007-09-25 17:54:57 -07007169
7170 return 0;
7171}
7172
7173/*****************************************************************************
7174 *
7175 * sysfs attributes
7176 *
7177 *****************************************************************************/
7178
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08007179#ifdef CONFIG_IWL3945_DEBUG
Zhu Yib481de92007-09-25 17:54:57 -07007180
7181/*
7182 * The following adds a new attribute to the sysfs representation
7183 * of this device driver (i.e. a new file in /sys/bus/pci/drivers/iwl/)
7184 * used for controlling the debug level.
7185 *
7186 * See the level definitions in iwl for details.
7187 */
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08007188static ssize_t show_debug_level(struct device *d,
7189 struct device_attribute *attr, char *buf)
Zhu Yib481de92007-09-25 17:54:57 -07007190{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08007191 struct iwl_priv *priv = d->driver_data;
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08007192
7193 return sprintf(buf, "0x%08X\n", priv->debug_level);
Zhu Yib481de92007-09-25 17:54:57 -07007194}
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08007195static ssize_t store_debug_level(struct device *d,
7196 struct device_attribute *attr,
Zhu Yib481de92007-09-25 17:54:57 -07007197 const char *buf, size_t count)
7198{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08007199 struct iwl_priv *priv = d->driver_data;
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08007200 unsigned long val;
7201 int ret;
Zhu Yib481de92007-09-25 17:54:57 -07007202
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08007203 ret = strict_strtoul(buf, 0, &val);
7204 if (ret)
Tomas Winkler978785a2008-12-19 10:37:31 +08007205 IWL_INFO(priv, "%s is not in hex or decimal form.\n", buf);
Zhu Yib481de92007-09-25 17:54:57 -07007206 else
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08007207 priv->debug_level = val;
Zhu Yib481de92007-09-25 17:54:57 -07007208
7209 return strnlen(buf, count);
7210}
7211
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08007212static DEVICE_ATTR(debug_level, S_IWUSR | S_IRUGO,
7213 show_debug_level, store_debug_level);
Zhu Yib481de92007-09-25 17:54:57 -07007214
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08007215#endif /* CONFIG_IWL3945_DEBUG */
Zhu Yib481de92007-09-25 17:54:57 -07007216
Zhu Yib481de92007-09-25 17:54:57 -07007217static ssize_t show_temperature(struct device *d,
7218 struct device_attribute *attr, char *buf)
7219{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08007220 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07007221
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007222 if (!iwl3945_is_alive(priv))
Zhu Yib481de92007-09-25 17:54:57 -07007223 return -EAGAIN;
7224
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007225 return sprintf(buf, "%d\n", iwl3945_hw_get_temperature(priv));
Zhu Yib481de92007-09-25 17:54:57 -07007226}
7227
7228static DEVICE_ATTR(temperature, S_IRUGO, show_temperature, NULL);
7229
Zhu Yib481de92007-09-25 17:54:57 -07007230static ssize_t show_tx_power(struct device *d,
7231 struct device_attribute *attr, char *buf)
7232{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08007233 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07007234 return sprintf(buf, "%d\n", priv->user_txpower_limit);
7235}
7236
7237static ssize_t store_tx_power(struct device *d,
7238 struct device_attribute *attr,
7239 const char *buf, size_t count)
7240{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08007241 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07007242 char *p = (char *)buf;
7243 u32 val;
7244
7245 val = simple_strtoul(p, &p, 10);
7246 if (p == buf)
Tomas Winkler978785a2008-12-19 10:37:31 +08007247 IWL_INFO(priv, ": %s is not in decimal form.\n", buf);
Zhu Yib481de92007-09-25 17:54:57 -07007248 else
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007249 iwl3945_hw_reg_set_txpower(priv, val);
Zhu Yib481de92007-09-25 17:54:57 -07007250
7251 return count;
7252}
7253
7254static DEVICE_ATTR(tx_power, S_IWUSR | S_IRUGO, show_tx_power, store_tx_power);
7255
7256static ssize_t show_flags(struct device *d,
7257 struct device_attribute *attr, char *buf)
7258{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08007259 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07007260
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08007261 return sprintf(buf, "0x%04X\n", priv->active39_rxon.flags);
Zhu Yib481de92007-09-25 17:54:57 -07007262}
7263
7264static ssize_t store_flags(struct device *d,
7265 struct device_attribute *attr,
7266 const char *buf, size_t count)
7267{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08007268 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07007269 u32 flags = simple_strtoul(buf, NULL, 0);
7270
7271 mutex_lock(&priv->mutex);
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08007272 if (le32_to_cpu(priv->staging39_rxon.flags) != flags) {
Zhu Yib481de92007-09-25 17:54:57 -07007273 /* Cancel any currently running scans... */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007274 if (iwl3945_scan_cancel_timeout(priv, 100))
Winkler, Tomas39aadf82008-12-19 10:37:32 +08007275 IWL_WARN(priv, "Could not cancel scan.\n");
Zhu Yib481de92007-09-25 17:54:57 -07007276 else {
7277 IWL_DEBUG_INFO("Committing rxon.flags = 0x%04X\n",
7278 flags);
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08007279 priv->staging39_rxon.flags = cpu_to_le32(flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007280 iwl3945_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007281 }
7282 }
7283 mutex_unlock(&priv->mutex);
7284
7285 return count;
7286}
7287
7288static DEVICE_ATTR(flags, S_IWUSR | S_IRUGO, show_flags, store_flags);
7289
7290static ssize_t show_filter_flags(struct device *d,
7291 struct device_attribute *attr, char *buf)
7292{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08007293 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07007294
7295 return sprintf(buf, "0x%04X\n",
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08007296 le32_to_cpu(priv->active39_rxon.filter_flags));
Zhu Yib481de92007-09-25 17:54:57 -07007297}
7298
7299static ssize_t store_filter_flags(struct device *d,
7300 struct device_attribute *attr,
7301 const char *buf, size_t count)
7302{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08007303 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07007304 u32 filter_flags = simple_strtoul(buf, NULL, 0);
7305
7306 mutex_lock(&priv->mutex);
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08007307 if (le32_to_cpu(priv->staging39_rxon.filter_flags) != filter_flags) {
Zhu Yib481de92007-09-25 17:54:57 -07007308 /* Cancel any currently running scans... */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007309 if (iwl3945_scan_cancel_timeout(priv, 100))
Winkler, Tomas39aadf82008-12-19 10:37:32 +08007310 IWL_WARN(priv, "Could not cancel scan.\n");
Zhu Yib481de92007-09-25 17:54:57 -07007311 else {
7312 IWL_DEBUG_INFO("Committing rxon.filter_flags = "
7313 "0x%04X\n", filter_flags);
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08007314 priv->staging39_rxon.filter_flags =
Zhu Yib481de92007-09-25 17:54:57 -07007315 cpu_to_le32(filter_flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007316 iwl3945_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007317 }
7318 }
7319 mutex_unlock(&priv->mutex);
7320
7321 return count;
7322}
7323
7324static DEVICE_ATTR(filter_flags, S_IWUSR | S_IRUGO, show_filter_flags,
7325 store_filter_flags);
7326
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08007327#ifdef CONFIG_IWL3945_SPECTRUM_MEASUREMENT
Zhu Yib481de92007-09-25 17:54:57 -07007328
7329static ssize_t show_measurement(struct device *d,
7330 struct device_attribute *attr, char *buf)
7331{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08007332 struct iwl_priv *priv = dev_get_drvdata(d);
Tomas Winkler600c0e12008-12-19 10:37:04 +08007333 struct iwl_spectrum_notification measure_report;
Zhu Yib481de92007-09-25 17:54:57 -07007334 u32 size = sizeof(measure_report), len = 0, ofs = 0;
Tomas Winkler3ac7f142008-07-21 02:40:14 +03007335 u8 *data = (u8 *)&measure_report;
Zhu Yib481de92007-09-25 17:54:57 -07007336 unsigned long flags;
7337
7338 spin_lock_irqsave(&priv->lock, flags);
7339 if (!(priv->measurement_status & MEASUREMENT_READY)) {
7340 spin_unlock_irqrestore(&priv->lock, flags);
7341 return 0;
7342 }
7343 memcpy(&measure_report, &priv->measure_report, size);
7344 priv->measurement_status = 0;
7345 spin_unlock_irqrestore(&priv->lock, flags);
7346
7347 while (size && (PAGE_SIZE - len)) {
7348 hex_dump_to_buffer(data + ofs, size, 16, 1, buf + len,
7349 PAGE_SIZE - len, 1);
7350 len = strlen(buf);
7351 if (PAGE_SIZE - len)
7352 buf[len++] = '\n';
7353
7354 ofs += 16;
7355 size -= min(size, 16U);
7356 }
7357
7358 return len;
7359}
7360
7361static ssize_t store_measurement(struct device *d,
7362 struct device_attribute *attr,
7363 const char *buf, size_t count)
7364{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08007365 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07007366 struct ieee80211_measurement_params params = {
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08007367 .channel = le16_to_cpu(priv->active39_rxon.channel),
Zhu Yib481de92007-09-25 17:54:57 -07007368 .start_time = cpu_to_le64(priv->last_tsf),
7369 .duration = cpu_to_le16(1),
7370 };
7371 u8 type = IWL_MEASURE_BASIC;
7372 u8 buffer[32];
7373 u8 channel;
7374
7375 if (count) {
7376 char *p = buffer;
7377 strncpy(buffer, buf, min(sizeof(buffer), count));
7378 channel = simple_strtoul(p, NULL, 0);
7379 if (channel)
7380 params.channel = channel;
7381
7382 p = buffer;
7383 while (*p && *p != ' ')
7384 p++;
7385 if (*p)
7386 type = simple_strtoul(p + 1, NULL, 0);
7387 }
7388
7389 IWL_DEBUG_INFO("Invoking measurement of type %d on "
7390 "channel %d (for '%s')\n", type, params.channel, buf);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007391 iwl3945_get_measurement(priv, &params, type);
Zhu Yib481de92007-09-25 17:54:57 -07007392
7393 return count;
7394}
7395
7396static DEVICE_ATTR(measurement, S_IRUSR | S_IWUSR,
7397 show_measurement, store_measurement);
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08007398#endif /* CONFIG_IWL3945_SPECTRUM_MEASUREMENT */
Zhu Yib481de92007-09-25 17:54:57 -07007399
Zhu Yib481de92007-09-25 17:54:57 -07007400static ssize_t store_retry_rate(struct device *d,
7401 struct device_attribute *attr,
7402 const char *buf, size_t count)
7403{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08007404 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07007405
7406 priv->retry_rate = simple_strtoul(buf, NULL, 0);
7407 if (priv->retry_rate <= 0)
7408 priv->retry_rate = 1;
7409
7410 return count;
7411}
7412
7413static ssize_t show_retry_rate(struct device *d,
7414 struct device_attribute *attr, char *buf)
7415{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08007416 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07007417 return sprintf(buf, "%d", priv->retry_rate);
7418}
7419
7420static DEVICE_ATTR(retry_rate, S_IWUSR | S_IRUSR, show_retry_rate,
7421 store_retry_rate);
7422
7423static ssize_t store_power_level(struct device *d,
7424 struct device_attribute *attr,
7425 const char *buf, size_t count)
7426{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08007427 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07007428 int rc;
7429 int mode;
7430
7431 mode = simple_strtoul(buf, NULL, 0);
7432 mutex_lock(&priv->mutex);
7433
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007434 if (!iwl3945_is_ready(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07007435 rc = -EAGAIN;
7436 goto out;
7437 }
7438
Samuel Ortiz1125eff2008-12-19 10:37:14 +08007439 if ((mode < 1) || (mode > IWL39_POWER_LIMIT) ||
7440 (mode == IWL39_POWER_AC))
7441 mode = IWL39_POWER_AC;
Zhu Yib481de92007-09-25 17:54:57 -07007442 else
7443 mode |= IWL_POWER_ENABLED;
7444
7445 if (mode != priv->power_mode) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007446 rc = iwl3945_send_power_mode(priv, IWL_POWER_LEVEL(mode));
Zhu Yib481de92007-09-25 17:54:57 -07007447 if (rc) {
7448 IWL_DEBUG_MAC80211("failed setting power mode.\n");
7449 goto out;
7450 }
7451 priv->power_mode = mode;
7452 }
7453
7454 rc = count;
7455
7456 out:
7457 mutex_unlock(&priv->mutex);
7458 return rc;
7459}
7460
7461#define MAX_WX_STRING 80
7462
7463/* Values are in microsecond */
7464static const s32 timeout_duration[] = {
7465 350000,
7466 250000,
7467 75000,
7468 37000,
7469 25000,
7470};
7471static const s32 period_duration[] = {
7472 400000,
7473 700000,
7474 1000000,
7475 1000000,
7476 1000000
7477};
7478
7479static ssize_t show_power_level(struct device *d,
7480 struct device_attribute *attr, char *buf)
7481{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08007482 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07007483 int level = IWL_POWER_LEVEL(priv->power_mode);
7484 char *p = buf;
7485
7486 p += sprintf(p, "%d ", level);
7487 switch (level) {
7488 case IWL_POWER_MODE_CAM:
Samuel Ortiz1125eff2008-12-19 10:37:14 +08007489 case IWL39_POWER_AC:
Zhu Yib481de92007-09-25 17:54:57 -07007490 p += sprintf(p, "(AC)");
7491 break;
Samuel Ortiz1125eff2008-12-19 10:37:14 +08007492 case IWL39_POWER_BATTERY:
Zhu Yib481de92007-09-25 17:54:57 -07007493 p += sprintf(p, "(BATTERY)");
7494 break;
7495 default:
7496 p += sprintf(p,
7497 "(Timeout %dms, Period %dms)",
7498 timeout_duration[level - 1] / 1000,
7499 period_duration[level - 1] / 1000);
7500 }
7501
7502 if (!(priv->power_mode & IWL_POWER_ENABLED))
7503 p += sprintf(p, " OFF\n");
7504 else
7505 p += sprintf(p, " \n");
7506
Tomas Winkler3ac7f142008-07-21 02:40:14 +03007507 return p - buf + 1;
Zhu Yib481de92007-09-25 17:54:57 -07007508
7509}
7510
7511static DEVICE_ATTR(power_level, S_IWUSR | S_IRUSR, show_power_level,
7512 store_power_level);
7513
7514static ssize_t show_channels(struct device *d,
7515 struct device_attribute *attr, char *buf)
7516{
Johannes Berg8318d782008-01-24 19:38:38 +01007517 /* all this shit doesn't belong into sysfs anyway */
7518 return 0;
Zhu Yib481de92007-09-25 17:54:57 -07007519}
7520
7521static DEVICE_ATTR(channels, S_IRUSR, show_channels, NULL);
7522
7523static ssize_t show_statistics(struct device *d,
7524 struct device_attribute *attr, char *buf)
7525{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08007526 struct iwl_priv *priv = dev_get_drvdata(d);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007527 u32 size = sizeof(struct iwl3945_notif_statistics);
Zhu Yib481de92007-09-25 17:54:57 -07007528 u32 len = 0, ofs = 0;
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08007529 u8 *data = (u8 *)&priv->statistics_39;
Zhu Yib481de92007-09-25 17:54:57 -07007530 int rc = 0;
7531
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007532 if (!iwl3945_is_alive(priv))
Zhu Yib481de92007-09-25 17:54:57 -07007533 return -EAGAIN;
7534
7535 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007536 rc = iwl3945_send_statistics_request(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007537 mutex_unlock(&priv->mutex);
7538
7539 if (rc) {
7540 len = sprintf(buf,
7541 "Error sending statistics request: 0x%08X\n", rc);
7542 return len;
7543 }
7544
7545 while (size && (PAGE_SIZE - len)) {
7546 hex_dump_to_buffer(data + ofs, size, 16, 1, buf + len,
7547 PAGE_SIZE - len, 1);
7548 len = strlen(buf);
7549 if (PAGE_SIZE - len)
7550 buf[len++] = '\n';
7551
7552 ofs += 16;
7553 size -= min(size, 16U);
7554 }
7555
7556 return len;
7557}
7558
7559static DEVICE_ATTR(statistics, S_IRUGO, show_statistics, NULL);
7560
7561static ssize_t show_antenna(struct device *d,
7562 struct device_attribute *attr, char *buf)
7563{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08007564 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07007565
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007566 if (!iwl3945_is_alive(priv))
Zhu Yib481de92007-09-25 17:54:57 -07007567 return -EAGAIN;
7568
7569 return sprintf(buf, "%d\n", priv->antenna);
7570}
7571
7572static ssize_t store_antenna(struct device *d,
7573 struct device_attribute *attr,
7574 const char *buf, size_t count)
7575{
7576 int ant;
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08007577 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07007578
7579 if (count == 0)
7580 return 0;
7581
7582 if (sscanf(buf, "%1i", &ant) != 1) {
7583 IWL_DEBUG_INFO("not in hex or decimal form.\n");
7584 return count;
7585 }
7586
7587 if ((ant >= 0) && (ant <= 2)) {
7588 IWL_DEBUG_INFO("Setting antenna select to %d.\n", ant);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007589 priv->antenna = (enum iwl3945_antenna)ant;
Zhu Yib481de92007-09-25 17:54:57 -07007590 } else
7591 IWL_DEBUG_INFO("Bad antenna select value %d.\n", ant);
7592
7593
7594 return count;
7595}
7596
7597static DEVICE_ATTR(antenna, S_IWUSR | S_IRUGO, show_antenna, store_antenna);
7598
7599static ssize_t show_status(struct device *d,
7600 struct device_attribute *attr, char *buf)
7601{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08007602 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007603 if (!iwl3945_is_alive(priv))
Zhu Yib481de92007-09-25 17:54:57 -07007604 return -EAGAIN;
7605 return sprintf(buf, "0x%08x\n", (int)priv->status);
7606}
7607
7608static DEVICE_ATTR(status, S_IRUGO, show_status, NULL);
7609
7610static ssize_t dump_error_log(struct device *d,
7611 struct device_attribute *attr,
7612 const char *buf, size_t count)
7613{
7614 char *p = (char *)buf;
7615
7616 if (p[0] == '1')
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08007617 iwl3945_dump_nic_error_log((struct iwl_priv *)d->driver_data);
Zhu Yib481de92007-09-25 17:54:57 -07007618
7619 return strnlen(buf, count);
7620}
7621
7622static DEVICE_ATTR(dump_errors, S_IWUSR, NULL, dump_error_log);
7623
7624static ssize_t dump_event_log(struct device *d,
7625 struct device_attribute *attr,
7626 const char *buf, size_t count)
7627{
7628 char *p = (char *)buf;
7629
7630 if (p[0] == '1')
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08007631 iwl3945_dump_nic_event_log((struct iwl_priv *)d->driver_data);
Zhu Yib481de92007-09-25 17:54:57 -07007632
7633 return strnlen(buf, count);
7634}
7635
7636static DEVICE_ATTR(dump_events, S_IWUSR, NULL, dump_event_log);
7637
7638/*****************************************************************************
7639 *
Tomas Winklera96a27f2008-10-23 23:48:56 -07007640 * driver setup and tear down
Zhu Yib481de92007-09-25 17:54:57 -07007641 *
7642 *****************************************************************************/
7643
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08007644static void iwl3945_setup_deferred_work(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07007645{
7646 priv->workqueue = create_workqueue(DRV_NAME);
7647
7648 init_waitqueue_head(&priv->wait_command_queue);
7649
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007650 INIT_WORK(&priv->up, iwl3945_bg_up);
7651 INIT_WORK(&priv->restart, iwl3945_bg_restart);
7652 INIT_WORK(&priv->rx_replenish, iwl3945_bg_rx_replenish);
7653 INIT_WORK(&priv->scan_completed, iwl3945_bg_scan_completed);
7654 INIT_WORK(&priv->request_scan, iwl3945_bg_request_scan);
7655 INIT_WORK(&priv->abort_scan, iwl3945_bg_abort_scan);
7656 INIT_WORK(&priv->rf_kill, iwl3945_bg_rf_kill);
7657 INIT_WORK(&priv->beacon_update, iwl3945_bg_beacon_update);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007658 INIT_DELAYED_WORK(&priv->init_alive_start, iwl3945_bg_init_alive_start);
7659 INIT_DELAYED_WORK(&priv->alive_start, iwl3945_bg_alive_start);
7660 INIT_DELAYED_WORK(&priv->scan_check, iwl3945_bg_scan_check);
Zhu Yib481de92007-09-25 17:54:57 -07007661
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007662 iwl3945_hw_setup_deferred_work(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007663
7664 tasklet_init(&priv->irq_tasklet, (void (*)(unsigned long))
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007665 iwl3945_irq_tasklet, (unsigned long)priv);
Zhu Yib481de92007-09-25 17:54:57 -07007666}
7667
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08007668static void iwl3945_cancel_deferred_work(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07007669{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007670 iwl3945_hw_cancel_deferred_work(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007671
Joonwoo Parke47eb6a2007-11-29 10:42:49 +09007672 cancel_delayed_work_sync(&priv->init_alive_start);
Zhu Yib481de92007-09-25 17:54:57 -07007673 cancel_delayed_work(&priv->scan_check);
7674 cancel_delayed_work(&priv->alive_start);
Zhu Yib481de92007-09-25 17:54:57 -07007675 cancel_work_sync(&priv->beacon_update);
7676}
7677
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007678static struct attribute *iwl3945_sysfs_entries[] = {
Zhu Yib481de92007-09-25 17:54:57 -07007679 &dev_attr_antenna.attr,
7680 &dev_attr_channels.attr,
7681 &dev_attr_dump_errors.attr,
7682 &dev_attr_dump_events.attr,
7683 &dev_attr_flags.attr,
7684 &dev_attr_filter_flags.attr,
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08007685#ifdef CONFIG_IWL3945_SPECTRUM_MEASUREMENT
Zhu Yib481de92007-09-25 17:54:57 -07007686 &dev_attr_measurement.attr,
7687#endif
7688 &dev_attr_power_level.attr,
Zhu Yib481de92007-09-25 17:54:57 -07007689 &dev_attr_retry_rate.attr,
Zhu Yib481de92007-09-25 17:54:57 -07007690 &dev_attr_statistics.attr,
7691 &dev_attr_status.attr,
7692 &dev_attr_temperature.attr,
Zhu Yib481de92007-09-25 17:54:57 -07007693 &dev_attr_tx_power.attr,
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08007694#ifdef CONFIG_IWL3945_DEBUG
7695 &dev_attr_debug_level.attr,
7696#endif
Zhu Yib481de92007-09-25 17:54:57 -07007697 NULL
7698};
7699
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007700static struct attribute_group iwl3945_attribute_group = {
Zhu Yib481de92007-09-25 17:54:57 -07007701 .name = NULL, /* put in device directory */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007702 .attrs = iwl3945_sysfs_entries,
Zhu Yib481de92007-09-25 17:54:57 -07007703};
7704
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007705static struct ieee80211_ops iwl3945_hw_ops = {
7706 .tx = iwl3945_mac_tx,
7707 .start = iwl3945_mac_start,
7708 .stop = iwl3945_mac_stop,
7709 .add_interface = iwl3945_mac_add_interface,
7710 .remove_interface = iwl3945_mac_remove_interface,
7711 .config = iwl3945_mac_config,
7712 .config_interface = iwl3945_mac_config_interface,
7713 .configure_filter = iwl3945_configure_filter,
7714 .set_key = iwl3945_mac_set_key,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007715 .get_tx_stats = iwl3945_mac_get_tx_stats,
7716 .conf_tx = iwl3945_mac_conf_tx,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007717 .reset_tsf = iwl3945_mac_reset_tsf,
Abhijeet Kolekarcd56d332008-09-03 11:26:21 +08007718 .bss_info_changed = iwl3945_bss_info_changed,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007719 .hw_scan = iwl3945_mac_hw_scan
Zhu Yib481de92007-09-25 17:54:57 -07007720};
7721
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007722static int iwl3945_pci_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
Zhu Yib481de92007-09-25 17:54:57 -07007723{
7724 int err = 0;
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08007725 struct iwl_priv *priv;
Zhu Yib481de92007-09-25 17:54:57 -07007726 struct ieee80211_hw *hw;
Kolekar, Abhijeetc0f20d92008-12-19 10:37:19 +08007727 struct iwl_cfg *cfg = (struct iwl_cfg *)(ent->driver_data);
Mohamed Abbas0359fac2008-03-28 16:21:08 -07007728 unsigned long flags;
Zhu Yib481de92007-09-25 17:54:57 -07007729
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08007730 /***********************
7731 * 1. Allocating HW data
7732 * ********************/
7733
Zhu Yib481de92007-09-25 17:54:57 -07007734 /* mac80211 allocates memory for this device instance, including
7735 * space for this driver's private structure */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08007736 hw = ieee80211_alloc_hw(sizeof(struct iwl_priv), &iwl3945_hw_ops);
Zhu Yib481de92007-09-25 17:54:57 -07007737 if (hw == NULL) {
Samuel Ortiza3139c52008-12-19 10:37:09 +08007738 printk(KERN_ERR DRV_NAME "Can not allocate network device\n");
Zhu Yib481de92007-09-25 17:54:57 -07007739 err = -ENOMEM;
7740 goto out;
7741 }
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08007742
Zhu Yib481de92007-09-25 17:54:57 -07007743 SET_IEEE80211_DEV(hw, &pdev->dev);
7744
Zhu Yib481de92007-09-25 17:54:57 -07007745 priv = hw->priv;
7746 priv->hw = hw;
Zhu Yib481de92007-09-25 17:54:57 -07007747 priv->pci_dev = pdev;
Tomas Winkler82b9a122008-03-04 18:09:30 -08007748 priv->cfg = cfg;
Cahill, Ben M6440adb2007-11-29 11:09:55 +08007749
Samuel Ortiza3139c52008-12-19 10:37:09 +08007750 if ((iwl3945_param_queues_num > IWL39_MAX_NUM_QUEUES) ||
7751 (iwl3945_param_queues_num < IWL_MIN_NUM_QUEUES)) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08007752 IWL_ERR(priv,
7753 "invalid queues_num, should be between %d and %d\n",
7754 IWL_MIN_NUM_QUEUES, IWL39_MAX_NUM_QUEUES);
Samuel Ortiza3139c52008-12-19 10:37:09 +08007755 err = -EINVAL;
7756 goto out;
7757 }
7758
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08007759 /* Disabling hardware scan means that mac80211 will perform scans
7760 * "the hard way", rather than using device's scan. */
7761 if (iwl3945_param_disable_hw_scan) {
7762 IWL_DEBUG_INFO("Disabling hw_scan\n");
7763 iwl3945_hw_ops.hw_scan = NULL;
7764 }
7765
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08007766 IWL_DEBUG_INFO("*** LOAD DRIVER ***\n");
7767 hw->rate_control_algorithm = "iwl-3945-rs";
7768 hw->sta_data_size = sizeof(struct iwl3945_sta_priv);
7769
Cahill, Ben M6440adb2007-11-29 11:09:55 +08007770 /* Select antenna (may be helpful if only one antenna is connected) */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007771 priv->antenna = (enum iwl3945_antenna)iwl3945_param_antenna;
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08007772#ifdef CONFIG_IWL3945_DEBUG
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08007773 priv->debug_level = iwl3945_param_debug;
Zhu Yib481de92007-09-25 17:54:57 -07007774 atomic_set(&priv->restrict_refcnt, 0);
7775#endif
Zhu Yib481de92007-09-25 17:54:57 -07007776
Bruno Randolf566bfe52008-05-08 19:15:40 +02007777 /* Tell mac80211 our characteristics */
Johannes Berg605a0bd2008-07-15 10:10:01 +02007778 hw->flags = IEEE80211_HW_SIGNAL_DBM |
Bruno Randolf566bfe52008-05-08 19:15:40 +02007779 IEEE80211_HW_NOISE_DBM;
Zhu Yib481de92007-09-25 17:54:57 -07007780
Luis R. Rodriguezf59ac042008-08-29 16:26:43 -07007781 hw->wiphy->interface_modes =
Luis R. Rodriguezf59ac042008-08-29 16:26:43 -07007782 BIT(NL80211_IFTYPE_STATION) |
7783 BIT(NL80211_IFTYPE_ADHOC);
7784
Luis R. Rodriguezea4a82dc2008-11-12 14:22:04 -08007785 hw->wiphy->fw_handles_regulatory = true;
7786
Cahill, Ben M6440adb2007-11-29 11:09:55 +08007787 /* 4 EDCA QOS priorities */
Zhu Yib481de92007-09-25 17:54:57 -07007788 hw->queues = 4;
7789
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08007790 /***************************
7791 * 2. Initializing PCI bus
7792 * *************************/
Zhu Yib481de92007-09-25 17:54:57 -07007793 if (pci_enable_device(pdev)) {
7794 err = -ENODEV;
7795 goto out_ieee80211_free_hw;
7796 }
7797
7798 pci_set_master(pdev);
7799
Zhu Yib481de92007-09-25 17:54:57 -07007800 err = pci_set_dma_mask(pdev, DMA_32BIT_MASK);
7801 if (!err)
7802 err = pci_set_consistent_dma_mask(pdev, DMA_32BIT_MASK);
7803 if (err) {
Tomas Winkler978785a2008-12-19 10:37:31 +08007804 IWL_WARN(priv, "No suitable DMA available.\n");
Zhu Yib481de92007-09-25 17:54:57 -07007805 goto out_pci_disable_device;
7806 }
7807
7808 pci_set_drvdata(pdev, priv);
7809 err = pci_request_regions(pdev, DRV_NAME);
7810 if (err)
7811 goto out_pci_disable_device;
Cahill, Ben M6440adb2007-11-29 11:09:55 +08007812
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08007813 /***********************
7814 * 3. Read REV Register
7815 * ********************/
Zhu Yib481de92007-09-25 17:54:57 -07007816 priv->hw_base = pci_iomap(pdev, 0, 0);
7817 if (!priv->hw_base) {
7818 err = -ENODEV;
7819 goto out_pci_release_regions;
7820 }
7821
7822 IWL_DEBUG_INFO("pci_resource_len = 0x%08llx\n",
7823 (unsigned long long) pci_resource_len(pdev, 0));
7824 IWL_DEBUG_INFO("pci_resource_base = %p\n", priv->hw_base);
7825
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08007826 /* We disable the RETRY_TIMEOUT register (0x41) to keep
7827 * PCI Tx retries from interfering with C3 CPU state */
7828 pci_write_config_byte(pdev, 0x41, 0x00);
Zhu Yib481de92007-09-25 17:54:57 -07007829
Zhu Yi5a66926a2008-01-14 17:46:18 -08007830 /* nic init */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08007831 iwl_set_bit(priv, CSR_GIO_CHICKEN_BITS,
Tomas Winkler3ac7f142008-07-21 02:40:14 +03007832 CSR_GIO_CHICKEN_BITS_REG_BIT_DIS_L0S_EXIT_TIMER);
Zhu Yi5a66926a2008-01-14 17:46:18 -08007833
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08007834 iwl_set_bit(priv, CSR_GP_CNTRL, CSR_GP_CNTRL_REG_FLAG_INIT_DONE);
7835 err = iwl_poll_direct_bit(priv, CSR_GP_CNTRL,
Zhu, Yi73d7b5a2008-12-05 07:58:40 -08007836 CSR_GP_CNTRL_REG_FLAG_MAC_CLOCK_READY, 25000);
Tomas Winkler3ac7f142008-07-21 02:40:14 +03007837 if (err < 0) {
7838 IWL_DEBUG_INFO("Failed to init the card\n");
Zhu Yi5a66926a2008-01-14 17:46:18 -08007839 goto out_remove_sysfs;
Tomas Winkler3ac7f142008-07-21 02:40:14 +03007840 }
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08007841
7842 /***********************
7843 * 4. Read EEPROM
7844 * ********************/
Zhu Yi5a66926a2008-01-14 17:46:18 -08007845 /* Read the EEPROM */
7846 err = iwl3945_eeprom_init(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007847 if (err) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08007848 IWL_ERR(priv, "Unable to init EEPROM\n");
Zhu Yi5a66926a2008-01-14 17:46:18 -08007849 goto out_remove_sysfs;
7850 }
7851 /* MAC Address location in EEPROM same for 3945/4965 */
7852 get_eeprom_mac(priv, priv->mac_addr);
Johannes Berge1749612008-10-27 15:59:26 -07007853 IWL_DEBUG_INFO("MAC address: %pM\n", priv->mac_addr);
Zhu Yi5a66926a2008-01-14 17:46:18 -08007854 SET_IEEE80211_PERM_ADDR(priv->hw, priv->mac_addr);
7855
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08007856 /***********************
7857 * 5. Setup HW Constants
7858 * ********************/
7859 /* Device-specific setup */
Abhijeet Kolekar3832ec92008-12-19 10:37:26 +08007860 if (iwl3945_hw_set_hw_params(priv)) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08007861 IWL_ERR(priv, "failed to set hw settings\n");
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08007862 goto out_iounmap;
7863 }
7864
7865 /***********************
7866 * 6. Setup priv
7867 * ********************/
7868 priv->retry_rate = 1;
7869 priv->ibss_beacon = NULL;
7870
7871 spin_lock_init(&priv->lock);
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08007872 spin_lock_init(&priv->power_data_39.lock);
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08007873 spin_lock_init(&priv->sta_lock);
7874 spin_lock_init(&priv->hcmd_lock);
7875
7876 INIT_LIST_HEAD(&priv->free_frames);
7877 mutex_init(&priv->mutex);
7878
7879 /* Clear the driver's (not device's) station table */
7880 iwl3945_clear_stations_table(priv);
7881
7882 priv->data_retry_limit = -1;
7883 priv->ieee_channels = NULL;
7884 priv->ieee_rates = NULL;
7885 priv->band = IEEE80211_BAND_2GHZ;
7886
7887 priv->iw_mode = NL80211_IFTYPE_STATION;
7888
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08007889 iwl3945_reset_qos(priv);
7890
7891 priv->qos_data.qos_active = 0;
7892 priv->qos_data.qos_cap.val = 0;
7893
7894
7895 priv->rates_mask = IWL_RATES_MASK;
7896 /* If power management is turned on, default to AC mode */
Samuel Ortiz1125eff2008-12-19 10:37:14 +08007897 priv->power_mode = IWL39_POWER_AC;
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08007898 priv->user_txpower_limit = IWL_DEFAULT_TX_POWER;
7899
Reinette Chatre849e0dc2008-01-23 10:15:18 -08007900 err = iwl3945_init_channel_map(priv);
7901 if (err) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08007902 IWL_ERR(priv, "initializing regulatory failed: %d\n", err);
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08007903 goto out_release_irq;
Reinette Chatre849e0dc2008-01-23 10:15:18 -08007904 }
7905
7906 err = iwl3945_init_geos(priv);
7907 if (err) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08007908 IWL_ERR(priv, "initializing geos failed: %d\n", err);
Reinette Chatre849e0dc2008-01-23 10:15:18 -08007909 goto out_free_channel_map;
7910 }
Reinette Chatre849e0dc2008-01-23 10:15:18 -08007911
Tomas Winkler978785a2008-12-19 10:37:31 +08007912 IWL_INFO(priv, "Detected Intel Wireless WiFi Link %s\n",
7913 priv->cfg->name);
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08007914
7915 /***********************************
7916 * 7. Initialize Module Parameters
7917 * **********************************/
7918
7919 /* Initialize module parameter values here */
7920 /* Disable radio (SW RF KILL) via parameter when loading driver */
7921 if (iwl3945_param_disable) {
7922 set_bit(STATUS_RF_KILL_SW, &priv->status);
7923 IWL_DEBUG_INFO("Radio disabled.\n");
7924 }
7925
7926
7927 /***********************
7928 * 8. Setup Services
7929 * ********************/
7930
7931 spin_lock_irqsave(&priv->lock, flags);
7932 iwl3945_disable_interrupts(priv);
7933 spin_unlock_irqrestore(&priv->lock, flags);
7934
7935 err = sysfs_create_group(&pdev->dev.kobj, &iwl3945_attribute_group);
7936 if (err) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08007937 IWL_ERR(priv, "failed to create sysfs device attributes\n");
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08007938 goto out_free_geos;
7939 }
7940
7941 iwl3945_set_rxon_channel(priv, IEEE80211_BAND_2GHZ, 6);
7942 iwl3945_setup_deferred_work(priv);
7943 iwl3945_setup_rx_handlers(priv);
7944
7945 /***********************
7946 * 9. Conclude
7947 * ********************/
7948 pci_save_state(pdev);
7949 pci_disable_device(pdev);
7950
7951 /*********************************
7952 * 10. Setup and Register mac80211
7953 * *******************************/
7954
Zhu Yi5a66926a2008-01-14 17:46:18 -08007955 err = ieee80211_register_hw(priv->hw);
7956 if (err) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08007957 IWL_ERR(priv, "Failed to register network device: %d\n", err);
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08007958 goto out_remove_sysfs;
Zhu Yib481de92007-09-25 17:54:57 -07007959 }
7960
Zhu Yi5a66926a2008-01-14 17:46:18 -08007961 priv->hw->conf.beacon_int = 100;
7962 priv->mac80211_registered = 1;
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08007963
Zhu Yib481de92007-09-25 17:54:57 -07007964
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08007965 err = iwl3945_rfkill_init(priv);
7966 if (err)
Winkler, Tomas15b16872008-12-19 10:37:33 +08007967 IWL_ERR(priv, "Unable to initialize RFKILL system. "
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08007968 "Ignoring error: %d\n", err);
7969
Zhu Yib481de92007-09-25 17:54:57 -07007970 return 0;
7971
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08007972 out_remove_sysfs:
7973 sysfs_remove_group(&pdev->dev.kobj, &iwl3945_attribute_group);
Reinette Chatre849e0dc2008-01-23 10:15:18 -08007974 out_free_geos:
7975 iwl3945_free_geos(priv);
7976 out_free_channel_map:
7977 iwl3945_free_channel_map(priv);
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08007978
Zhu Yib481de92007-09-25 17:54:57 -07007979
7980 out_release_irq:
Zhu Yib481de92007-09-25 17:54:57 -07007981 destroy_workqueue(priv->workqueue);
7982 priv->workqueue = NULL;
Abhijeet Kolekar3832ec92008-12-19 10:37:26 +08007983 iwl3945_unset_hw_params(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007984
7985 out_iounmap:
7986 pci_iounmap(pdev, priv->hw_base);
7987 out_pci_release_regions:
7988 pci_release_regions(pdev);
7989 out_pci_disable_device:
7990 pci_disable_device(pdev);
7991 pci_set_drvdata(pdev, NULL);
7992 out_ieee80211_free_hw:
7993 ieee80211_free_hw(priv->hw);
7994 out:
7995 return err;
7996}
7997
Reinette Chatrec83dbf62008-03-21 13:53:41 -07007998static void __devexit iwl3945_pci_remove(struct pci_dev *pdev)
Zhu Yib481de92007-09-25 17:54:57 -07007999{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08008000 struct iwl_priv *priv = pci_get_drvdata(pdev);
Mohamed Abbas0359fac2008-03-28 16:21:08 -07008001 unsigned long flags;
Zhu Yib481de92007-09-25 17:54:57 -07008002
8003 if (!priv)
8004 return;
8005
8006 IWL_DEBUG_INFO("*** UNLOAD DRIVER ***\n");
8007
Zhu Yib481de92007-09-25 17:54:57 -07008008 set_bit(STATUS_EXIT_PENDING, &priv->status);
Zhu Yib24d22b2007-12-19 13:59:52 +08008009
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008010 iwl3945_down(priv);
Zhu Yib481de92007-09-25 17:54:57 -07008011
Mohamed Abbas0359fac2008-03-28 16:21:08 -07008012 /* make sure we flush any pending irq or
8013 * tasklet for the driver
8014 */
8015 spin_lock_irqsave(&priv->lock, flags);
8016 iwl3945_disable_interrupts(priv);
8017 spin_unlock_irqrestore(&priv->lock, flags);
8018
8019 iwl_synchronize_irq(priv);
8020
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008021 sysfs_remove_group(&pdev->dev.kobj, &iwl3945_attribute_group);
Zhu Yib481de92007-09-25 17:54:57 -07008022
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08008023 iwl3945_rfkill_unregister(priv);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008024 iwl3945_dealloc_ucode_pci(priv);
Zhu Yib481de92007-09-25 17:54:57 -07008025
8026 if (priv->rxq.bd)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008027 iwl3945_rx_queue_free(priv, &priv->rxq);
8028 iwl3945_hw_txq_ctx_free(priv);
Zhu Yib481de92007-09-25 17:54:57 -07008029
Abhijeet Kolekar3832ec92008-12-19 10:37:26 +08008030 iwl3945_unset_hw_params(priv);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008031 iwl3945_clear_stations_table(priv);
Zhu Yib481de92007-09-25 17:54:57 -07008032
Tomas Winkler3ac7f142008-07-21 02:40:14 +03008033 if (priv->mac80211_registered)
Zhu Yib481de92007-09-25 17:54:57 -07008034 ieee80211_unregister_hw(priv->hw);
Zhu Yib481de92007-09-25 17:54:57 -07008035
Mohamed Abbas6ef89d02007-10-25 17:15:47 +08008036 /*netif_stop_queue(dev); */
8037 flush_workqueue(priv->workqueue);
8038
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008039 /* ieee80211_unregister_hw calls iwl3945_mac_stop, which flushes
Zhu Yib481de92007-09-25 17:54:57 -07008040 * priv->workqueue... so we can't take down the workqueue
8041 * until now... */
8042 destroy_workqueue(priv->workqueue);
8043 priv->workqueue = NULL;
8044
Zhu Yib481de92007-09-25 17:54:57 -07008045 pci_iounmap(pdev, priv->hw_base);
8046 pci_release_regions(pdev);
8047 pci_disable_device(pdev);
8048 pci_set_drvdata(pdev, NULL);
8049
Reinette Chatre849e0dc2008-01-23 10:15:18 -08008050 iwl3945_free_channel_map(priv);
8051 iwl3945_free_geos(priv);
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08008052 kfree(priv->scan39);
Zhu Yib481de92007-09-25 17:54:57 -07008053 if (priv->ibss_beacon)
8054 dev_kfree_skb(priv->ibss_beacon);
8055
8056 ieee80211_free_hw(priv->hw);
8057}
8058
8059#ifdef CONFIG_PM
8060
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008061static int iwl3945_pci_suspend(struct pci_dev *pdev, pm_message_t state)
Zhu Yib481de92007-09-25 17:54:57 -07008062{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08008063 struct iwl_priv *priv = pci_get_drvdata(pdev);
Zhu Yib481de92007-09-25 17:54:57 -07008064
Zhu Yie655b9f2008-01-24 02:19:38 -08008065 if (priv->is_open) {
8066 set_bit(STATUS_IN_SUSPEND, &priv->status);
8067 iwl3945_mac_stop(priv->hw);
8068 priv->is_open = 1;
8069 }
Zhu Yib481de92007-09-25 17:54:57 -07008070
Zhu Yib481de92007-09-25 17:54:57 -07008071 pci_set_power_state(pdev, PCI_D3hot);
8072
Zhu Yib481de92007-09-25 17:54:57 -07008073 return 0;
8074}
8075
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008076static int iwl3945_pci_resume(struct pci_dev *pdev)
Zhu Yib481de92007-09-25 17:54:57 -07008077{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08008078 struct iwl_priv *priv = pci_get_drvdata(pdev);
Zhu Yib481de92007-09-25 17:54:57 -07008079
Zhu Yib481de92007-09-25 17:54:57 -07008080 pci_set_power_state(pdev, PCI_D0);
Zhu Yib481de92007-09-25 17:54:57 -07008081
Zhu Yie655b9f2008-01-24 02:19:38 -08008082 if (priv->is_open)
8083 iwl3945_mac_start(priv->hw);
Zhu Yib481de92007-09-25 17:54:57 -07008084
Zhu Yie655b9f2008-01-24 02:19:38 -08008085 clear_bit(STATUS_IN_SUSPEND, &priv->status);
Zhu Yib481de92007-09-25 17:54:57 -07008086 return 0;
8087}
8088
8089#endif /* CONFIG_PM */
8090
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08008091/*************** RFKILL FUNCTIONS **********/
Adel Gadllah80fcc9e2008-07-01 17:49:50 +02008092#ifdef CONFIG_IWL3945_RFKILL
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08008093/* software rf-kill from user */
8094static int iwl3945_rfkill_soft_rf_kill(void *data, enum rfkill_state state)
8095{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08008096 struct iwl_priv *priv = data;
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08008097 int err = 0;
8098
Adel Gadllah80fcc9e2008-07-01 17:49:50 +02008099 if (!priv->rfkill)
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08008100 return 0;
8101
8102 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
8103 return 0;
8104
Tomas Winklera96a27f2008-10-23 23:48:56 -07008105 IWL_DEBUG_RF_KILL("we received soft RFKILL set to state %d\n", state);
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08008106 mutex_lock(&priv->mutex);
8107
8108 switch (state) {
Zhu Yiacdfe9b2008-06-30 17:23:32 +08008109 case RFKILL_STATE_UNBLOCKED:
Adel Gadllah80fcc9e2008-07-01 17:49:50 +02008110 if (iwl3945_is_rfkill_hw(priv)) {
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08008111 err = -EBUSY;
Adel Gadllah80fcc9e2008-07-01 17:49:50 +02008112 goto out_unlock;
8113 }
8114 iwl3945_radio_kill_sw(priv, 0);
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08008115 break;
Zhu Yiacdfe9b2008-06-30 17:23:32 +08008116 case RFKILL_STATE_SOFT_BLOCKED:
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08008117 iwl3945_radio_kill_sw(priv, 1);
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08008118 break;
Zhu Yiacdfe9b2008-06-30 17:23:32 +08008119 default:
Winkler, Tomas39aadf82008-12-19 10:37:32 +08008120 IWL_WARN(priv, "received unexpected RFKILL state %d\n", state);
Zhu Yiacdfe9b2008-06-30 17:23:32 +08008121 break;
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08008122 }
Adel Gadllah80fcc9e2008-07-01 17:49:50 +02008123out_unlock:
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08008124 mutex_unlock(&priv->mutex);
8125
8126 return err;
8127}
8128
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08008129int iwl3945_rfkill_init(struct iwl_priv *priv)
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08008130{
8131 struct device *device = wiphy_dev(priv->hw->wiphy);
8132 int ret = 0;
8133
8134 BUG_ON(device == NULL);
8135
8136 IWL_DEBUG_RF_KILL("Initializing RFKILL.\n");
Adel Gadllah80fcc9e2008-07-01 17:49:50 +02008137 priv->rfkill = rfkill_allocate(device, RFKILL_TYPE_WLAN);
8138 if (!priv->rfkill) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08008139 IWL_ERR(priv, "Unable to allocate rfkill device.\n");
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08008140 ret = -ENOMEM;
8141 goto error;
8142 }
8143
Adel Gadllah80fcc9e2008-07-01 17:49:50 +02008144 priv->rfkill->name = priv->cfg->name;
8145 priv->rfkill->data = priv;
8146 priv->rfkill->state = RFKILL_STATE_UNBLOCKED;
8147 priv->rfkill->toggle_radio = iwl3945_rfkill_soft_rf_kill;
8148 priv->rfkill->user_claim_unsupported = 1;
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08008149
Adel Gadllah80fcc9e2008-07-01 17:49:50 +02008150 priv->rfkill->dev.class->suspend = NULL;
8151 priv->rfkill->dev.class->resume = NULL;
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08008152
Adel Gadllah80fcc9e2008-07-01 17:49:50 +02008153 ret = rfkill_register(priv->rfkill);
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08008154 if (ret) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08008155 IWL_ERR(priv, "Unable to register rfkill: %d\n", ret);
Adel Gadllah80fcc9e2008-07-01 17:49:50 +02008156 goto freed_rfkill;
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08008157 }
8158
8159 IWL_DEBUG_RF_KILL("RFKILL initialization complete.\n");
8160 return ret;
8161
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08008162freed_rfkill:
Adel Gadllah80fcc9e2008-07-01 17:49:50 +02008163 if (priv->rfkill != NULL)
8164 rfkill_free(priv->rfkill);
8165 priv->rfkill = NULL;
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08008166
8167error:
8168 IWL_DEBUG_RF_KILL("RFKILL initialization complete.\n");
8169 return ret;
8170}
8171
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08008172void iwl3945_rfkill_unregister(struct iwl_priv *priv)
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08008173{
Adel Gadllah80fcc9e2008-07-01 17:49:50 +02008174 if (priv->rfkill)
8175 rfkill_unregister(priv->rfkill);
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08008176
Adel Gadllah80fcc9e2008-07-01 17:49:50 +02008177 priv->rfkill = NULL;
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08008178}
8179
8180/* set rf-kill to the right state. */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08008181void iwl3945_rfkill_set_hw_state(struct iwl_priv *priv)
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08008182{
8183
Adel Gadllah80fcc9e2008-07-01 17:49:50 +02008184 if (!priv->rfkill)
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08008185 return;
8186
Adel Gadllah80fcc9e2008-07-01 17:49:50 +02008187 if (iwl3945_is_rfkill_hw(priv)) {
8188 rfkill_force_state(priv->rfkill, RFKILL_STATE_HARD_BLOCKED);
8189 return;
8190 }
8191
8192 if (!iwl3945_is_rfkill_sw(priv))
8193 rfkill_force_state(priv->rfkill, RFKILL_STATE_UNBLOCKED);
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08008194 else
Adel Gadllah80fcc9e2008-07-01 17:49:50 +02008195 rfkill_force_state(priv->rfkill, RFKILL_STATE_SOFT_BLOCKED);
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08008196}
8197#endif
8198
Zhu Yib481de92007-09-25 17:54:57 -07008199/*****************************************************************************
8200 *
8201 * driver and module entry point
8202 *
8203 *****************************************************************************/
8204
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008205static struct pci_driver iwl3945_driver = {
Zhu Yib481de92007-09-25 17:54:57 -07008206 .name = DRV_NAME,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008207 .id_table = iwl3945_hw_card_ids,
8208 .probe = iwl3945_pci_probe,
8209 .remove = __devexit_p(iwl3945_pci_remove),
Zhu Yib481de92007-09-25 17:54:57 -07008210#ifdef CONFIG_PM
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008211 .suspend = iwl3945_pci_suspend,
8212 .resume = iwl3945_pci_resume,
Zhu Yib481de92007-09-25 17:54:57 -07008213#endif
8214};
8215
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008216static int __init iwl3945_init(void)
Zhu Yib481de92007-09-25 17:54:57 -07008217{
8218
8219 int ret;
8220 printk(KERN_INFO DRV_NAME ": " DRV_DESCRIPTION ", " DRV_VERSION "\n");
8221 printk(KERN_INFO DRV_NAME ": " DRV_COPYRIGHT "\n");
Reinette Chatre897e1cf2008-03-28 16:21:09 -07008222
8223 ret = iwl3945_rate_control_register();
8224 if (ret) {
Samuel Ortiza3139c52008-12-19 10:37:09 +08008225 printk(KERN_ERR DRV_NAME
8226 "Unable to register rate control algorithm: %d\n", ret);
Reinette Chatre897e1cf2008-03-28 16:21:09 -07008227 return ret;
8228 }
8229
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008230 ret = pci_register_driver(&iwl3945_driver);
Zhu Yib481de92007-09-25 17:54:57 -07008231 if (ret) {
Samuel Ortiza3139c52008-12-19 10:37:09 +08008232 printk(KERN_ERR DRV_NAME "Unable to initialize PCI module\n");
Reinette Chatre897e1cf2008-03-28 16:21:09 -07008233 goto error_register;
Zhu Yib481de92007-09-25 17:54:57 -07008234 }
Zhu Yib481de92007-09-25 17:54:57 -07008235
8236 return ret;
Reinette Chatre897e1cf2008-03-28 16:21:09 -07008237
Reinette Chatre897e1cf2008-03-28 16:21:09 -07008238error_register:
8239 iwl3945_rate_control_unregister();
8240 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07008241}
8242
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008243static void __exit iwl3945_exit(void)
Zhu Yib481de92007-09-25 17:54:57 -07008244{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008245 pci_unregister_driver(&iwl3945_driver);
Reinette Chatre897e1cf2008-03-28 16:21:09 -07008246 iwl3945_rate_control_unregister();
Zhu Yib481de92007-09-25 17:54:57 -07008247}
8248
Reinette Chatrea0987a82008-12-02 12:14:06 -08008249MODULE_FIRMWARE(IWL3945_MODULE_FIRMWARE(IWL3945_UCODE_API_MAX));
Zhu Yi25cb6ca2008-09-11 11:45:22 +08008250
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008251module_param_named(antenna, iwl3945_param_antenna, int, 0444);
Zhu Yib481de92007-09-25 17:54:57 -07008252MODULE_PARM_DESC(antenna, "select antenna (1=Main, 2=Aux, default 0 [both])");
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008253module_param_named(disable, iwl3945_param_disable, int, 0444);
Zhu Yib481de92007-09-25 17:54:57 -07008254MODULE_PARM_DESC(disable, "manually disable the radio (default 0 [radio on])");
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008255module_param_named(hwcrypto, iwl3945_param_hwcrypto, int, 0444);
Zhu Yib481de92007-09-25 17:54:57 -07008256MODULE_PARM_DESC(hwcrypto,
8257 "using hardware crypto engine (default 0 [software])\n");
Wu, Fengguang95aa1942008-12-17 16:52:30 +08008258module_param_named(debug, iwl3945_param_debug, uint, 0444);
Zhu Yib481de92007-09-25 17:54:57 -07008259MODULE_PARM_DESC(debug, "debug output mask");
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008260module_param_named(disable_hw_scan, iwl3945_param_disable_hw_scan, int, 0444);
Zhu Yib481de92007-09-25 17:54:57 -07008261MODULE_PARM_DESC(disable_hw_scan, "disable hardware scanning (default 0)");
8262
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008263module_param_named(queues_num, iwl3945_param_queues_num, int, 0444);
Zhu Yib481de92007-09-25 17:54:57 -07008264MODULE_PARM_DESC(queues_num, "number of hw queues.");
8265
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008266module_exit(iwl3945_exit);
8267module_init(iwl3945_init);