blob: d145002d0a69e0b1de5cab0af57ed59eb932b893 [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
Winkler, Tomasdbb66542008-12-22 11:31:14 +080051#include "iwl-fh.h"
52#include "iwl-3945-fh.h"
Tomas Winkler600c0e12008-12-19 10:37:04 +080053#include "iwl-commands.h"
Zhu Yib481de92007-09-25 17:54:57 -070054#include "iwl-3945.h"
55#include "iwl-helpers.h"
Kolekar, Abhijeet5747d472008-12-19 10:37:18 +080056#include "iwl-core.h"
Samuel Ortizd20b3c62008-12-19 10:37:15 +080057#include "iwl-dev.h"
Zhu Yib481de92007-09-25 17:54:57 -070058
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +080059static int iwl3945_tx_queue_update_write_ptr(struct iwl_priv *priv,
Samuel Ortiz188cf6c2008-12-22 11:31:16 +080060 struct iwl_tx_queue *txq);
Christoph Hellwig416e1432007-10-25 17:15:49 +080061
Zhu Yib481de92007-09-25 17:54:57 -070062/*
63 * module name, copyright, version, etc.
Zhu Yib481de92007-09-25 17:54:57 -070064 */
65
66#define DRV_DESCRIPTION \
67"Intel(R) PRO/Wireless 3945ABG/BG Network Connection driver for Linux"
68
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +080069#ifdef CONFIG_IWL3945_DEBUG
Zhu Yib481de92007-09-25 17:54:57 -070070#define VD "d"
71#else
72#define VD
73#endif
74
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +080075#ifdef CONFIG_IWL3945_SPECTRUM_MEASUREMENT
Zhu Yib481de92007-09-25 17:54:57 -070076#define VS "s"
77#else
78#define VS
79#endif
80
Kolekar, Abhijeeteaa686c2008-12-19 10:37:17 +080081#define IWL39_VERSION "1.2.26k" VD VS
Reinette Chatreeb7ae892008-03-11 16:17:17 -070082#define DRV_COPYRIGHT "Copyright(c) 2003-2008 Intel Corporation"
Tomas Winklera7b75202008-12-11 10:33:41 -080083#define DRV_AUTHOR "<ilw@linux.intel.com>"
Kolekar, Abhijeeteaa686c2008-12-19 10:37:17 +080084#define DRV_VERSION IWL39_VERSION
Zhu Yib481de92007-09-25 17:54:57 -070085
Zhu Yib481de92007-09-25 17:54:57 -070086
87MODULE_DESCRIPTION(DRV_DESCRIPTION);
88MODULE_VERSION(DRV_VERSION);
Tomas Winklera7b75202008-12-11 10:33:41 -080089MODULE_AUTHOR(DRV_COPYRIGHT " " DRV_AUTHOR);
Zhu Yib481de92007-09-25 17:54:57 -070090MODULE_LICENSE("GPL");
91
Kolekar, Abhijeetdf878d82008-12-19 10:37:35 +080092 /* module parameters */
93struct iwl_mod_params iwl3945_mod_params = {
94 .num_of_queues = IWL39_MAX_NUM_QUEUES,
95 /* the rest are 0 by default */
96};
97
Zhu Yib481de92007-09-25 17:54:57 -070098/*************** DMA-QUEUE-GENERAL-FUNCTIONS *****
99 * DMA services
100 *
101 * Theory of operation
102 *
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800103 * A Tx or Rx queue resides in host DRAM, and is comprised of a circular buffer
104 * of buffer descriptors, each of which points to one or more data buffers for
105 * the device to read from or fill. Driver and device exchange status of each
106 * queue via "read" and "write" pointers. Driver keeps minimum of 2 empty
107 * entries in each circular buffer, to protect against confusing empty and full
108 * queue states.
109 *
110 * The device reads or writes the data in the queues via the device's several
111 * DMA/FIFO channels. Each queue is mapped to a single DMA channel.
Zhu Yib481de92007-09-25 17:54:57 -0700112 *
113 * For Tx queue, there are low mark and high mark limits. If, after queuing
114 * the packet for Tx, free space become < low mark, Tx queue stopped. When
115 * reclaiming packets (on 'tx done IRQ), if free space become > high mark,
116 * Tx queue resumed.
117 *
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800118 * The 3945 operates with six queues: One receive queue, one transmit queue
119 * (#4) for sending commands to the device firmware, and four transmit queues
120 * (#0-3) for data tx via EDCA. An additional 2 HCCA queues are unused.
Zhu Yib481de92007-09-25 17:54:57 -0700121 ***************************************************/
122
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800123/**
124 * iwl3945_queue_init - Initialize queue's high/low-water and read/write indexes
125 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800126static int iwl3945_queue_init(struct iwl_priv *priv, struct iwl_queue *q,
Zhu Yib481de92007-09-25 17:54:57 -0700127 int count, int slots_num, u32 id)
128{
129 q->n_bd = count;
130 q->n_window = slots_num;
131 q->id = id;
132
Tomas Winklerc54b6792008-03-06 17:36:53 -0800133 /* count must be power-of-two size, otherwise iwl_queue_inc_wrap
134 * and iwl_queue_dec_wrap are broken. */
Zhu Yib481de92007-09-25 17:54:57 -0700135 BUG_ON(!is_power_of_2(count));
136
137 /* slots_num must be power-of-two size, otherwise
138 * get_cmd_index is broken. */
139 BUG_ON(!is_power_of_2(slots_num));
140
141 q->low_mark = q->n_window / 4;
142 if (q->low_mark < 4)
143 q->low_mark = 4;
144
145 q->high_mark = q->n_window / 8;
146 if (q->high_mark < 2)
147 q->high_mark = 2;
148
Tomas Winklerfc4b6852007-10-25 17:15:24 +0800149 q->write_ptr = q->read_ptr = 0;
Zhu Yib481de92007-09-25 17:54:57 -0700150
151 return 0;
152}
153
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800154/**
155 * iwl3945_tx_queue_alloc - Alloc driver data and TFD CB for one Tx/cmd queue
156 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800157static int iwl3945_tx_queue_alloc(struct iwl_priv *priv,
Samuel Ortiz188cf6c2008-12-22 11:31:16 +0800158 struct iwl_tx_queue *txq, u32 id)
Zhu Yib481de92007-09-25 17:54:57 -0700159{
160 struct pci_dev *dev = priv->pci_dev;
161
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800162 /* Driver private data, only for Tx (not command) queues,
163 * not shared with device. */
Zhu Yib481de92007-09-25 17:54:57 -0700164 if (id != IWL_CMD_QUEUE_NUM) {
165 txq->txb = kmalloc(sizeof(txq->txb[0]) *
166 TFD_QUEUE_SIZE_MAX, GFP_KERNEL);
167 if (!txq->txb) {
Winkler, Tomas15b16872008-12-19 10:37:33 +0800168 IWL_ERR(priv, "kmalloc for auxiliary BD "
Zhu Yib481de92007-09-25 17:54:57 -0700169 "structures failed\n");
170 goto error;
171 }
172 } else
173 txq->txb = NULL;
174
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800175 /* Circular buffer of transmit frame descriptors (TFDs),
176 * shared with device */
Samuel Ortiz188cf6c2008-12-22 11:31:16 +0800177 txq->tfds39 = pci_alloc_consistent(dev,
178 sizeof(txq->tfds39[0]) * TFD_QUEUE_SIZE_MAX,
Zhu Yib481de92007-09-25 17:54:57 -0700179 &txq->q.dma_addr);
180
Samuel Ortiz188cf6c2008-12-22 11:31:16 +0800181 if (!txq->tfds39) {
Winkler, Tomas15b16872008-12-19 10:37:33 +0800182 IWL_ERR(priv, "pci_alloc_consistent(%zd) failed\n",
Samuel Ortiz188cf6c2008-12-22 11:31:16 +0800183 sizeof(txq->tfds39[0]) * TFD_QUEUE_SIZE_MAX);
Zhu Yib481de92007-09-25 17:54:57 -0700184 goto error;
185 }
186 txq->q.id = id;
187
188 return 0;
189
190 error:
Tomas Winkler3ac7f142008-07-21 02:40:14 +0300191 kfree(txq->txb);
192 txq->txb = NULL;
Zhu Yib481de92007-09-25 17:54:57 -0700193
194 return -ENOMEM;
195}
196
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800197/**
198 * iwl3945_tx_queue_init - Allocate and initialize one tx/cmd queue
199 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800200int iwl3945_tx_queue_init(struct iwl_priv *priv,
Samuel Ortiz188cf6c2008-12-22 11:31:16 +0800201 struct iwl_tx_queue *txq, int slots_num, u32 txq_id)
Zhu Yib481de92007-09-25 17:54:57 -0700202{
Samuel Ortiz188cf6c2008-12-22 11:31:16 +0800203 int len, i;
Zhu Yib481de92007-09-25 17:54:57 -0700204 int rc = 0;
205
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800206 /*
207 * Alloc buffer array for commands (Tx or other types of commands).
208 * For the command queue (#4), allocate command space + one big
209 * command for scan, since scan command is very huge; the system will
210 * not have two scans at the same time, so only one is needed.
211 * For data Tx queues (all other queues), no super-size command
212 * space is needed.
213 */
Samuel Ortiz188cf6c2008-12-22 11:31:16 +0800214 len = sizeof(struct iwl_cmd);
215 for (i = 0; i <= slots_num; i++) {
216 if (i == slots_num) {
217 if (txq_id == IWL_CMD_QUEUE_NUM)
218 len += IWL_MAX_SCAN_SIZE;
219 else
220 continue;
221 }
222
223 txq->cmd[i] = kmalloc(len, GFP_KERNEL);
224 if (!txq->cmd[i])
225 goto err;
226 }
Zhu Yib481de92007-09-25 17:54:57 -0700227
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800228 /* Alloc driver data array and TFD circular buffer */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800229 rc = iwl3945_tx_queue_alloc(priv, txq, txq_id);
Samuel Ortiz188cf6c2008-12-22 11:31:16 +0800230 if (rc)
231 goto err;
Zhu Yib481de92007-09-25 17:54:57 -0700232
Zhu Yib481de92007-09-25 17:54:57 -0700233 txq->need_update = 0;
234
235 /* TFD_QUEUE_SIZE_MAX must be power-of-two size, otherwise
Tomas Winklerc54b6792008-03-06 17:36:53 -0800236 * iwl_queue_inc_wrap and iwl_queue_dec_wrap are broken. */
Zhu Yib481de92007-09-25 17:54:57 -0700237 BUILD_BUG_ON(TFD_QUEUE_SIZE_MAX & (TFD_QUEUE_SIZE_MAX - 1));
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800238
239 /* Initialize queue high/low-water, head/tail indexes */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800240 iwl3945_queue_init(priv, &txq->q, TFD_QUEUE_SIZE_MAX, slots_num, txq_id);
Zhu Yib481de92007-09-25 17:54:57 -0700241
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800242 /* Tell device where to find queue, enable DMA channel. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800243 iwl3945_hw_tx_queue_init(priv, txq);
Zhu Yib481de92007-09-25 17:54:57 -0700244
245 return 0;
Samuel Ortiz188cf6c2008-12-22 11:31:16 +0800246err:
247 for (i = 0; i < slots_num; i++) {
248 kfree(txq->cmd[i]);
249 txq->cmd[i] = NULL;
250 }
251
252 if (txq_id == IWL_CMD_QUEUE_NUM) {
253 kfree(txq->cmd[slots_num]);
254 txq->cmd[slots_num] = NULL;
255 }
256 return -ENOMEM;
Zhu Yib481de92007-09-25 17:54:57 -0700257}
258
259/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800260 * iwl3945_tx_queue_free - Deallocate DMA queue.
Zhu Yib481de92007-09-25 17:54:57 -0700261 * @txq: Transmit queue to deallocate.
262 *
263 * Empty queue by removing and destroying all BD's.
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800264 * Free all buffers.
265 * 0-fill, but do not free "txq" descriptor structure.
Zhu Yib481de92007-09-25 17:54:57 -0700266 */
Samuel Ortiz188cf6c2008-12-22 11:31:16 +0800267void iwl3945_tx_queue_free(struct iwl_priv *priv, struct iwl_tx_queue *txq)
Zhu Yib481de92007-09-25 17:54:57 -0700268{
Samuel Ortizd20b3c62008-12-19 10:37:15 +0800269 struct iwl_queue *q = &txq->q;
Zhu Yib481de92007-09-25 17:54:57 -0700270 struct pci_dev *dev = priv->pci_dev;
Samuel Ortiz188cf6c2008-12-22 11:31:16 +0800271 int len, i;
Zhu Yib481de92007-09-25 17:54:57 -0700272
273 if (q->n_bd == 0)
274 return;
275
276 /* first, empty all BD's */
Tomas Winklerfc4b6852007-10-25 17:15:24 +0800277 for (; q->write_ptr != q->read_ptr;
Tomas Winklerc54b6792008-03-06 17:36:53 -0800278 q->read_ptr = iwl_queue_inc_wrap(q->read_ptr, q->n_bd))
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800279 iwl3945_hw_txq_free_tfd(priv, txq);
Zhu Yib481de92007-09-25 17:54:57 -0700280
Winkler, Tomasc2d79b42008-12-19 10:37:34 +0800281 len = sizeof(struct iwl_cmd) * q->n_window;
Zhu Yib481de92007-09-25 17:54:57 -0700282 if (q->id == IWL_CMD_QUEUE_NUM)
283 len += IWL_MAX_SCAN_SIZE;
284
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800285 /* De-alloc array of command/tx buffers */
Samuel Ortiz188cf6c2008-12-22 11:31:16 +0800286 for (i = 0; i < TFD_TX_CMD_SLOTS; i++)
287 kfree(txq->cmd[i]);
Zhu Yib481de92007-09-25 17:54:57 -0700288
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800289 /* De-alloc circular buffer of TFDs */
Zhu Yib481de92007-09-25 17:54:57 -0700290 if (txq->q.n_bd)
Winkler, Tomasdbb66542008-12-22 11:31:14 +0800291 pci_free_consistent(dev, sizeof(struct iwl3945_tfd) *
Samuel Ortiz188cf6c2008-12-22 11:31:16 +0800292 txq->q.n_bd, txq->tfds39, txq->q.dma_addr);
Zhu Yib481de92007-09-25 17:54:57 -0700293
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800294 /* De-alloc array of per-TFD driver data */
Tomas Winkler3ac7f142008-07-21 02:40:14 +0300295 kfree(txq->txb);
296 txq->txb = NULL;
Zhu Yib481de92007-09-25 17:54:57 -0700297
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800298 /* 0-fill queue descriptor structure */
Zhu Yib481de92007-09-25 17:54:57 -0700299 memset(txq, 0, sizeof(*txq));
300}
301
Zhu Yib481de92007-09-25 17:54:57 -0700302/*************** STATION TABLE MANAGEMENT ****
Ben Cahill9fbab512007-11-29 11:09:47 +0800303 * mac80211 should be examined to determine if sta_info is duplicating
Zhu Yib481de92007-09-25 17:54:57 -0700304 * the functionality provided here
305 */
306
307/**************************************************************/
Ian Schram01ebd062007-10-25 17:15:22 +0800308#if 0 /* temporary disable till we add real remove station */
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800309/**
310 * iwl3945_remove_station - Remove driver's knowledge of station.
311 *
312 * NOTE: This does not remove station from device's station table.
313 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800314static u8 iwl3945_remove_station(struct iwl_priv *priv, const u8 *addr, int is_ap)
Zhu Yib481de92007-09-25 17:54:57 -0700315{
316 int index = IWL_INVALID_STATION;
317 int i;
318 unsigned long flags;
319
320 spin_lock_irqsave(&priv->sta_lock, flags);
321
322 if (is_ap)
323 index = IWL_AP_ID;
324 else if (is_broadcast_ether_addr(addr))
Abhijeet Kolekar3832ec92008-12-19 10:37:26 +0800325 index = priv->hw_params.bcast_sta_id;
Zhu Yib481de92007-09-25 17:54:57 -0700326 else
Abhijeet Kolekar3832ec92008-12-19 10:37:26 +0800327 for (i = IWL_STA_ID; i < priv->hw_params.max_stations; i++)
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800328 if (priv->stations_39[i].used &&
329 !compare_ether_addr(priv->stations_39[i].sta.sta.addr,
Zhu Yib481de92007-09-25 17:54:57 -0700330 addr)) {
331 index = i;
332 break;
333 }
334
335 if (unlikely(index == IWL_INVALID_STATION))
336 goto out;
337
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800338 if (priv->stations_39[index].used) {
339 priv->stations_39[index].used = 0;
Zhu Yib481de92007-09-25 17:54:57 -0700340 priv->num_stations--;
341 }
342
343 BUG_ON(priv->num_stations < 0);
344
345out:
346 spin_unlock_irqrestore(&priv->sta_lock, flags);
347 return 0;
348}
Zhu Yi556f8db2007-09-27 11:27:33 +0800349#endif
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800350
351/**
352 * iwl3945_clear_stations_table - Clear the driver's station table
353 *
354 * NOTE: This does not clear or otherwise alter the device's station table.
355 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800356static void iwl3945_clear_stations_table(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700357{
358 unsigned long flags;
359
360 spin_lock_irqsave(&priv->sta_lock, flags);
361
362 priv->num_stations = 0;
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800363 memset(priv->stations_39, 0, sizeof(priv->stations_39));
Zhu Yib481de92007-09-25 17:54:57 -0700364
365 spin_unlock_irqrestore(&priv->sta_lock, flags);
366}
367
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800368/**
369 * iwl3945_add_station - Add station to station tables in driver and device
370 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800371u8 iwl3945_add_station(struct iwl_priv *priv, const u8 *addr, int is_ap, u8 flags)
Zhu Yib481de92007-09-25 17:54:57 -0700372{
373 int i;
374 int index = IWL_INVALID_STATION;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800375 struct iwl3945_station_entry *station;
Zhu Yib481de92007-09-25 17:54:57 -0700376 unsigned long flags_spin;
Zhu Yic14c5212007-09-27 11:27:35 +0800377 u8 rate;
Zhu Yib481de92007-09-25 17:54:57 -0700378
379 spin_lock_irqsave(&priv->sta_lock, flags_spin);
380 if (is_ap)
381 index = IWL_AP_ID;
382 else if (is_broadcast_ether_addr(addr))
Abhijeet Kolekar3832ec92008-12-19 10:37:26 +0800383 index = priv->hw_params.bcast_sta_id;
Zhu Yib481de92007-09-25 17:54:57 -0700384 else
Abhijeet Kolekar3832ec92008-12-19 10:37:26 +0800385 for (i = IWL_STA_ID; i < priv->hw_params.max_stations; i++) {
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800386 if (!compare_ether_addr(priv->stations_39[i].sta.sta.addr,
Zhu Yib481de92007-09-25 17:54:57 -0700387 addr)) {
388 index = i;
389 break;
390 }
391
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800392 if (!priv->stations_39[i].used &&
Zhu Yib481de92007-09-25 17:54:57 -0700393 index == IWL_INVALID_STATION)
394 index = i;
395 }
396
Ian Schram01ebd062007-10-25 17:15:22 +0800397 /* These two conditions has the same outcome but keep them separate
Zhu Yib481de92007-09-25 17:54:57 -0700398 since they have different meaning */
399 if (unlikely(index == IWL_INVALID_STATION)) {
400 spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
401 return index;
402 }
403
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800404 if (priv->stations_39[index].used &&
405 !compare_ether_addr(priv->stations_39[index].sta.sta.addr, addr)) {
Zhu Yib481de92007-09-25 17:54:57 -0700406 spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
407 return index;
408 }
409
Johannes Berge1749612008-10-27 15:59:26 -0700410 IWL_DEBUG_ASSOC("Add STA ID %d: %pM\n", index, addr);
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800411 station = &priv->stations_39[index];
Zhu Yib481de92007-09-25 17:54:57 -0700412 station->used = 1;
413 priv->num_stations++;
414
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800415 /* Set up the REPLY_ADD_STA command to send to device */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800416 memset(&station->sta, 0, sizeof(struct iwl3945_addsta_cmd));
Zhu Yib481de92007-09-25 17:54:57 -0700417 memcpy(station->sta.sta.addr, addr, ETH_ALEN);
418 station->sta.mode = 0;
419 station->sta.sta.sta_id = index;
420 station->sta.station_flags = 0;
421
Johannes Berg8318d782008-01-24 19:38:38 +0100422 if (priv->band == IEEE80211_BAND_5GHZ)
Tomas Winkler69946332007-10-25 17:15:27 +0800423 rate = IWL_RATE_6M_PLCP;
424 else
425 rate = IWL_RATE_1M_PLCP;
Zhu Yic14c5212007-09-27 11:27:35 +0800426
427 /* Turn on both antennas for the station... */
428 station->sta.rate_n_flags =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800429 iwl3945_hw_set_rate_n_flags(rate, RATE_MCS_ANT_AB_MSK);
Zhu Yic14c5212007-09-27 11:27:35 +0800430
Zhu Yib481de92007-09-25 17:54:57 -0700431 spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800432
433 /* Add station to device's station table */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800434 iwl3945_send_add_station(priv, &station->sta, flags);
Zhu Yib481de92007-09-25 17:54:57 -0700435 return index;
436
437}
438
Zhu Yib481de92007-09-25 17:54:57 -0700439
440/*************** HOST COMMAND QUEUE FUNCTIONS *****/
441
Abhijeet Kolekarc3056062008-11-12 13:14:08 -0800442#define IWL_CMD(x) case x: return #x
Zhu Yib481de92007-09-25 17:54:57 -0700443#define HOST_COMPLETE_TIMEOUT (HZ / 2)
444
445/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800446 * iwl3945_enqueue_hcmd - enqueue a uCode command
Zhu Yib481de92007-09-25 17:54:57 -0700447 * @priv: device private data point
448 * @cmd: a point to the ucode command structure
449 *
450 * The function returns < 0 values to indicate the operation is
451 * failed. On success, it turns the index (> 0) of command in the
452 * command queue.
453 */
Winkler, Tomasc2d79b42008-12-19 10:37:34 +0800454static int iwl3945_enqueue_hcmd(struct iwl_priv *priv, struct iwl_host_cmd *cmd)
Zhu Yib481de92007-09-25 17:54:57 -0700455{
Samuel Ortiz188cf6c2008-12-22 11:31:16 +0800456 struct iwl_tx_queue *txq = &priv->txq[IWL_CMD_QUEUE_NUM];
Samuel Ortizd20b3c62008-12-19 10:37:15 +0800457 struct iwl_queue *q = &txq->q;
Winkler, Tomasdbb66542008-12-22 11:31:14 +0800458 struct iwl3945_tfd *tfd;
Winkler, Tomasc2d79b42008-12-19 10:37:34 +0800459 struct iwl_cmd *out_cmd;
Zhu Yib481de92007-09-25 17:54:57 -0700460 u32 idx;
461 u16 fix_size = (u16)(cmd->len + sizeof(out_cmd->hdr));
462 dma_addr_t phys_addr;
463 int pad;
Samuel Ortiz188cf6c2008-12-22 11:31:16 +0800464 int ret, len;
Zhu Yib481de92007-09-25 17:54:57 -0700465 unsigned long flags;
466
467 /* If any of the command structures end up being larger than
468 * the TFD_MAX_PAYLOAD_SIZE, and it sent as a 'small' command then
469 * we will need to increase the size of the TFD entries */
Winkler, Tomasc2d79b42008-12-19 10:37:34 +0800470 BUG_ON((fix_size > TFD_MAX_PAYLOAD_SIZE) &&
Zhu Yib481de92007-09-25 17:54:57 -0700471 !(cmd->meta.flags & CMD_SIZE_HUGE));
472
Gregory Greenmanc342a1b2008-02-06 11:20:40 -0800473
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +0800474 if (iwl_is_rfkill(priv)) {
Gregory Greenmanc342a1b2008-02-06 11:20:40 -0800475 IWL_DEBUG_INFO("Not sending command - RF KILL");
476 return -EIO;
477 }
478
Samuel Ortizd20b3c62008-12-19 10:37:15 +0800479 if (iwl_queue_space(q) < ((cmd->meta.flags & CMD_ASYNC) ? 2 : 1)) {
Winkler, Tomas15b16872008-12-19 10:37:33 +0800480 IWL_ERR(priv, "No space for Tx\n");
Zhu Yib481de92007-09-25 17:54:57 -0700481 return -ENOSPC;
482 }
483
484 spin_lock_irqsave(&priv->hcmd_lock, flags);
485
Samuel Ortiz188cf6c2008-12-22 11:31:16 +0800486 tfd = &txq->tfds39[q->write_ptr];
Zhu Yib481de92007-09-25 17:54:57 -0700487 memset(tfd, 0, sizeof(*tfd));
488
Tomas Winklerfc4b6852007-10-25 17:15:24 +0800489 idx = get_cmd_index(q, q->write_ptr, cmd->meta.flags & CMD_SIZE_HUGE);
Samuel Ortiz188cf6c2008-12-22 11:31:16 +0800490 out_cmd = txq->cmd[idx];
Zhu Yib481de92007-09-25 17:54:57 -0700491
492 out_cmd->hdr.cmd = cmd->id;
493 memcpy(&out_cmd->meta, &cmd->meta, sizeof(cmd->meta));
494 memcpy(&out_cmd->cmd.payload, cmd->data, cmd->len);
495
496 /* At this point, the out_cmd now has all of the incoming cmd
497 * information */
498
499 out_cmd->hdr.flags = 0;
500 out_cmd->hdr.sequence = cpu_to_le16(QUEUE_TO_SEQ(IWL_CMD_QUEUE_NUM) |
Tomas Winklerfc4b6852007-10-25 17:15:24 +0800501 INDEX_TO_SEQ(q->write_ptr));
Zhu Yib481de92007-09-25 17:54:57 -0700502 if (out_cmd->meta.flags & CMD_SIZE_HUGE)
Tomas Winkler600c0e12008-12-19 10:37:04 +0800503 out_cmd->hdr.sequence |= SEQ_HUGE_FRAME;
Zhu Yib481de92007-09-25 17:54:57 -0700504
Samuel Ortiz188cf6c2008-12-22 11:31:16 +0800505 len = (idx == TFD_CMD_SLOTS) ?
506 IWL_MAX_SCAN_SIZE : sizeof(struct iwl_cmd);
507
508 phys_addr = pci_map_single(priv->pci_dev, out_cmd,
509 len, PCI_DMA_TODEVICE);
510 pci_unmap_addr_set(&out_cmd->meta, mapping, phys_addr);
511 pci_unmap_len_set(&out_cmd->meta, len, len);
512 phys_addr += offsetof(struct iwl_cmd, hdr);
513
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800514 iwl3945_hw_txq_attach_buf_to_tfd(priv, tfd, phys_addr, fix_size);
Zhu Yib481de92007-09-25 17:54:57 -0700515
516 pad = U32_PAD(cmd->len);
Winkler, Tomasdbb66542008-12-22 11:31:14 +0800517 tfd->control_flags |= cpu_to_le32(TFD_CTL_PAD_SET(pad));
Zhu Yib481de92007-09-25 17:54:57 -0700518
519 IWL_DEBUG_HC("Sending command %s (#%x), seq: 0x%04X, "
520 "%d bytes at %d[%d]:%d\n",
521 get_cmd_string(out_cmd->hdr.cmd),
522 out_cmd->hdr.cmd, le16_to_cpu(out_cmd->hdr.sequence),
Tomas Winklerfc4b6852007-10-25 17:15:24 +0800523 fix_size, q->write_ptr, idx, IWL_CMD_QUEUE_NUM);
Zhu Yib481de92007-09-25 17:54:57 -0700524
525 txq->need_update = 1;
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800526
527 /* Increment and update queue's write index */
Tomas Winklerc54b6792008-03-06 17:36:53 -0800528 q->write_ptr = iwl_queue_inc_wrap(q->write_ptr, q->n_bd);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800529 ret = iwl3945_tx_queue_update_write_ptr(priv, txq);
Zhu Yib481de92007-09-25 17:54:57 -0700530
531 spin_unlock_irqrestore(&priv->hcmd_lock, flags);
532 return ret ? ret : idx;
533}
534
Winkler, Tomasc2d79b42008-12-19 10:37:34 +0800535static int iwl3945_send_cmd_async(struct iwl_priv *priv,
536 struct iwl_host_cmd *cmd)
Zhu Yib481de92007-09-25 17:54:57 -0700537{
538 int ret;
539
540 BUG_ON(!(cmd->meta.flags & CMD_ASYNC));
541
542 /* An asynchronous command can not expect an SKB to be set. */
543 BUG_ON(cmd->meta.flags & CMD_WANT_SKB);
544
545 /* An asynchronous command MUST have a callback. */
546 BUG_ON(!cmd->meta.u.callback);
547
548 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
549 return -EBUSY;
550
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800551 ret = iwl3945_enqueue_hcmd(priv, cmd);
Zhu Yib481de92007-09-25 17:54:57 -0700552 if (ret < 0) {
Winkler, Tomas15b16872008-12-19 10:37:33 +0800553 IWL_ERR(priv,
554 "Error sending %s: iwl3945_enqueue_hcmd failed: %d\n",
555 get_cmd_string(cmd->id), ret);
Zhu Yib481de92007-09-25 17:54:57 -0700556 return ret;
557 }
558 return 0;
559}
560
Winkler, Tomasc2d79b42008-12-19 10:37:34 +0800561static int iwl3945_send_cmd_sync(struct iwl_priv *priv,
562 struct iwl_host_cmd *cmd)
Zhu Yib481de92007-09-25 17:54:57 -0700563{
564 int cmd_idx;
565 int ret;
Zhu Yib481de92007-09-25 17:54:57 -0700566
567 BUG_ON(cmd->meta.flags & CMD_ASYNC);
568
569 /* A synchronous command can not have a callback set. */
570 BUG_ON(cmd->meta.u.callback != NULL);
571
Tomas Winklere5472972008-03-28 16:21:12 -0700572 if (test_and_set_bit(STATUS_HCMD_SYNC_ACTIVE, &priv->status)) {
Winkler, Tomas15b16872008-12-19 10:37:33 +0800573 IWL_ERR(priv,
574 "Error sending %s: Already sending a host command\n",
575 get_cmd_string(cmd->id));
Tomas Winklere5472972008-03-28 16:21:12 -0700576 ret = -EBUSY;
577 goto out;
Zhu Yib481de92007-09-25 17:54:57 -0700578 }
579
580 set_bit(STATUS_HCMD_ACTIVE, &priv->status);
581
582 if (cmd->meta.flags & CMD_WANT_SKB)
583 cmd->meta.source = &cmd->meta;
584
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800585 cmd_idx = iwl3945_enqueue_hcmd(priv, cmd);
Zhu Yib481de92007-09-25 17:54:57 -0700586 if (cmd_idx < 0) {
587 ret = cmd_idx;
Winkler, Tomas15b16872008-12-19 10:37:33 +0800588 IWL_ERR(priv,
589 "Error sending %s: iwl3945_enqueue_hcmd failed: %d\n",
590 get_cmd_string(cmd->id), ret);
Zhu Yib481de92007-09-25 17:54:57 -0700591 goto out;
592 }
593
594 ret = wait_event_interruptible_timeout(priv->wait_command_queue,
595 !test_bit(STATUS_HCMD_ACTIVE, &priv->status),
596 HOST_COMPLETE_TIMEOUT);
597 if (!ret) {
598 if (test_bit(STATUS_HCMD_ACTIVE, &priv->status)) {
Winkler, Tomas15b16872008-12-19 10:37:33 +0800599 IWL_ERR(priv, "Error sending %s: time out after %dms\n",
Zhu Yib481de92007-09-25 17:54:57 -0700600 get_cmd_string(cmd->id),
601 jiffies_to_msecs(HOST_COMPLETE_TIMEOUT));
602
603 clear_bit(STATUS_HCMD_ACTIVE, &priv->status);
604 ret = -ETIMEDOUT;
605 goto cancel;
606 }
607 }
608
609 if (test_bit(STATUS_RF_KILL_HW, &priv->status)) {
610 IWL_DEBUG_INFO("Command %s aborted: RF KILL Switch\n",
611 get_cmd_string(cmd->id));
612 ret = -ECANCELED;
613 goto fail;
614 }
615 if (test_bit(STATUS_FW_ERROR, &priv->status)) {
616 IWL_DEBUG_INFO("Command %s failed: FW Error\n",
617 get_cmd_string(cmd->id));
618 ret = -EIO;
619 goto fail;
620 }
621 if ((cmd->meta.flags & CMD_WANT_SKB) && !cmd->meta.u.skb) {
Winkler, Tomas15b16872008-12-19 10:37:33 +0800622 IWL_ERR(priv, "Error: Response NULL in '%s'\n",
Zhu Yib481de92007-09-25 17:54:57 -0700623 get_cmd_string(cmd->id));
624 ret = -EIO;
Zhu Yi73e1a652009-01-08 10:19:58 -0800625 goto cancel;
Zhu Yib481de92007-09-25 17:54:57 -0700626 }
627
628 ret = 0;
629 goto out;
630
631cancel:
632 if (cmd->meta.flags & CMD_WANT_SKB) {
Winkler, Tomasc2d79b42008-12-19 10:37:34 +0800633 struct iwl_cmd *qcmd;
Zhu Yib481de92007-09-25 17:54:57 -0700634
635 /* Cancel the CMD_WANT_SKB flag for the cmd in the
636 * TX cmd queue. Otherwise in case the cmd comes
637 * in later, it will possibly set an invalid
638 * address (cmd->meta.source). */
Samuel Ortiz188cf6c2008-12-22 11:31:16 +0800639 qcmd = priv->txq[IWL_CMD_QUEUE_NUM].cmd[cmd_idx];
Zhu Yib481de92007-09-25 17:54:57 -0700640 qcmd->meta.flags &= ~CMD_WANT_SKB;
641 }
642fail:
643 if (cmd->meta.u.skb) {
644 dev_kfree_skb_any(cmd->meta.u.skb);
645 cmd->meta.u.skb = NULL;
646 }
647out:
Tomas Winklere5472972008-03-28 16:21:12 -0700648 clear_bit(STATUS_HCMD_SYNC_ACTIVE, &priv->status);
Zhu Yib481de92007-09-25 17:54:57 -0700649 return ret;
650}
651
Winkler, Tomasc2d79b42008-12-19 10:37:34 +0800652int iwl3945_send_cmd(struct iwl_priv *priv, struct iwl_host_cmd *cmd)
Zhu Yib481de92007-09-25 17:54:57 -0700653{
Zhu Yib481de92007-09-25 17:54:57 -0700654 if (cmd->meta.flags & CMD_ASYNC)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800655 return iwl3945_send_cmd_async(priv, cmd);
Zhu Yib481de92007-09-25 17:54:57 -0700656
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800657 return iwl3945_send_cmd_sync(priv, cmd);
Zhu Yib481de92007-09-25 17:54:57 -0700658}
659
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800660int iwl3945_send_cmd_pdu(struct iwl_priv *priv, u8 id, u16 len, const void *data)
Zhu Yib481de92007-09-25 17:54:57 -0700661{
Winkler, Tomasc2d79b42008-12-19 10:37:34 +0800662 struct iwl_host_cmd cmd = {
Zhu Yib481de92007-09-25 17:54:57 -0700663 .id = id,
664 .len = len,
665 .data = data,
666 };
667
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800668 return iwl3945_send_cmd_sync(priv, &cmd);
Zhu Yib481de92007-09-25 17:54:57 -0700669}
670
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800671static int __must_check iwl3945_send_cmd_u32(struct iwl_priv *priv, u8 id, u32 val)
Zhu Yib481de92007-09-25 17:54:57 -0700672{
Winkler, Tomasc2d79b42008-12-19 10:37:34 +0800673 struct iwl_host_cmd cmd = {
Zhu Yib481de92007-09-25 17:54:57 -0700674 .id = id,
675 .len = sizeof(val),
676 .data = &val,
677 };
678
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800679 return iwl3945_send_cmd_sync(priv, &cmd);
Zhu Yib481de92007-09-25 17:54:57 -0700680}
681
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800682int iwl3945_send_statistics_request(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700683{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800684 return iwl3945_send_cmd_u32(priv, REPLY_STATISTICS_CMD, 0);
Zhu Yib481de92007-09-25 17:54:57 -0700685}
686
687/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800688 * iwl3945_set_rxon_channel - Set the phymode and channel values in staging RXON
Johannes Berg8318d782008-01-24 19:38:38 +0100689 * @band: 2.4 or 5 GHz band
690 * @channel: Any channel valid for the requested band
Zhu Yib481de92007-09-25 17:54:57 -0700691
Johannes Berg8318d782008-01-24 19:38:38 +0100692 * In addition to setting the staging RXON, priv->band is also set.
Zhu Yib481de92007-09-25 17:54:57 -0700693 *
694 * NOTE: Does not commit to the hardware; it sets appropriate bit fields
Johannes Berg8318d782008-01-24 19:38:38 +0100695 * in the staging RXON flag structure based on the band
Zhu Yib481de92007-09-25 17:54:57 -0700696 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800697static int iwl3945_set_rxon_channel(struct iwl_priv *priv,
Johannes Berg8318d782008-01-24 19:38:38 +0100698 enum ieee80211_band band,
699 u16 channel)
Zhu Yib481de92007-09-25 17:54:57 -0700700{
Johannes Berg8318d782008-01-24 19:38:38 +0100701 if (!iwl3945_get_channel_info(priv, band, channel)) {
Zhu Yib481de92007-09-25 17:54:57 -0700702 IWL_DEBUG_INFO("Could not set channel to %d [%d]\n",
Johannes Berg8318d782008-01-24 19:38:38 +0100703 channel, band);
Zhu Yib481de92007-09-25 17:54:57 -0700704 return -EINVAL;
705 }
706
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800707 if ((le16_to_cpu(priv->staging39_rxon.channel) == channel) &&
Johannes Berg8318d782008-01-24 19:38:38 +0100708 (priv->band == band))
Zhu Yib481de92007-09-25 17:54:57 -0700709 return 0;
710
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800711 priv->staging39_rxon.channel = cpu_to_le16(channel);
Johannes Berg8318d782008-01-24 19:38:38 +0100712 if (band == IEEE80211_BAND_5GHZ)
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800713 priv->staging39_rxon.flags &= ~RXON_FLG_BAND_24G_MSK;
Zhu Yib481de92007-09-25 17:54:57 -0700714 else
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800715 priv->staging39_rxon.flags |= RXON_FLG_BAND_24G_MSK;
Zhu Yib481de92007-09-25 17:54:57 -0700716
Johannes Berg8318d782008-01-24 19:38:38 +0100717 priv->band = band;
Zhu Yib481de92007-09-25 17:54:57 -0700718
Johannes Berg8318d782008-01-24 19:38:38 +0100719 IWL_DEBUG_INFO("Staging channel set to %d [%d]\n", channel, band);
Zhu Yib481de92007-09-25 17:54:57 -0700720
721 return 0;
722}
723
724/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800725 * iwl3945_check_rxon_cmd - validate RXON structure is valid
Zhu Yib481de92007-09-25 17:54:57 -0700726 *
727 * NOTE: This is really only useful during development and can eventually
728 * be #ifdef'd out once the driver is stable and folks aren't actively
729 * making changes
730 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800731static int iwl3945_check_rxon_cmd(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700732{
733 int error = 0;
734 int counter = 1;
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800735 struct iwl3945_rxon_cmd *rxon = &priv->staging39_rxon;
Zhu Yib481de92007-09-25 17:54:57 -0700736
737 if (rxon->flags & RXON_FLG_BAND_24G_MSK) {
738 error |= le32_to_cpu(rxon->flags &
739 (RXON_FLG_TGJ_NARROW_BAND_MSK |
740 RXON_FLG_RADAR_DETECT_MSK));
741 if (error)
Winkler, Tomas39aadf82008-12-19 10:37:32 +0800742 IWL_WARN(priv, "check 24G fields %d | %d\n",
Zhu Yib481de92007-09-25 17:54:57 -0700743 counter++, error);
744 } else {
745 error |= (rxon->flags & RXON_FLG_SHORT_SLOT_MSK) ?
746 0 : le32_to_cpu(RXON_FLG_SHORT_SLOT_MSK);
747 if (error)
Winkler, Tomas39aadf82008-12-19 10:37:32 +0800748 IWL_WARN(priv, "check 52 fields %d | %d\n",
Zhu Yib481de92007-09-25 17:54:57 -0700749 counter++, error);
750 error |= le32_to_cpu(rxon->flags & RXON_FLG_CCK_MSK);
751 if (error)
Winkler, Tomas39aadf82008-12-19 10:37:32 +0800752 IWL_WARN(priv, "check 52 CCK %d | %d\n",
Zhu Yib481de92007-09-25 17:54:57 -0700753 counter++, error);
754 }
755 error |= (rxon->node_addr[0] | rxon->bssid_addr[0]) & 0x1;
756 if (error)
Winkler, Tomas39aadf82008-12-19 10:37:32 +0800757 IWL_WARN(priv, "check mac addr %d | %d\n", counter++, error);
Zhu Yib481de92007-09-25 17:54:57 -0700758
759 /* make sure basic rates 6Mbps and 1Mbps are supported */
760 error |= (((rxon->ofdm_basic_rates & IWL_RATE_6M_MASK) == 0) &&
761 ((rxon->cck_basic_rates & IWL_RATE_1M_MASK) == 0));
762 if (error)
Winkler, Tomas39aadf82008-12-19 10:37:32 +0800763 IWL_WARN(priv, "check basic rate %d | %d\n", counter++, error);
Zhu Yib481de92007-09-25 17:54:57 -0700764
765 error |= (le16_to_cpu(rxon->assoc_id) > 2007);
766 if (error)
Winkler, Tomas39aadf82008-12-19 10:37:32 +0800767 IWL_WARN(priv, "check assoc id %d | %d\n", counter++, error);
Zhu Yib481de92007-09-25 17:54:57 -0700768
769 error |= ((rxon->flags & (RXON_FLG_CCK_MSK | RXON_FLG_SHORT_SLOT_MSK))
770 == (RXON_FLG_CCK_MSK | RXON_FLG_SHORT_SLOT_MSK));
771 if (error)
Winkler, Tomas39aadf82008-12-19 10:37:32 +0800772 IWL_WARN(priv, "check CCK and short slot %d | %d\n",
Zhu Yib481de92007-09-25 17:54:57 -0700773 counter++, error);
774
775 error |= ((rxon->flags & (RXON_FLG_CCK_MSK | RXON_FLG_AUTO_DETECT_MSK))
776 == (RXON_FLG_CCK_MSK | RXON_FLG_AUTO_DETECT_MSK));
777 if (error)
Winkler, Tomas39aadf82008-12-19 10:37:32 +0800778 IWL_WARN(priv, "check CCK & auto detect %d | %d\n",
Zhu Yib481de92007-09-25 17:54:57 -0700779 counter++, error);
780
781 error |= ((rxon->flags & (RXON_FLG_AUTO_DETECT_MSK |
782 RXON_FLG_TGG_PROTECT_MSK)) == RXON_FLG_TGG_PROTECT_MSK);
783 if (error)
Winkler, Tomas39aadf82008-12-19 10:37:32 +0800784 IWL_WARN(priv, "check TGG and auto detect %d | %d\n",
Zhu Yib481de92007-09-25 17:54:57 -0700785 counter++, error);
786
787 if ((rxon->flags & RXON_FLG_DIS_DIV_MSK))
788 error |= ((rxon->flags & (RXON_FLG_ANT_B_MSK |
789 RXON_FLG_ANT_A_MSK)) == 0);
790 if (error)
Winkler, Tomas39aadf82008-12-19 10:37:32 +0800791 IWL_WARN(priv, "check antenna %d %d\n", counter++, error);
Zhu Yib481de92007-09-25 17:54:57 -0700792
793 if (error)
Winkler, Tomas39aadf82008-12-19 10:37:32 +0800794 IWL_WARN(priv, "Tuning to channel %d\n",
Zhu Yib481de92007-09-25 17:54:57 -0700795 le16_to_cpu(rxon->channel));
796
797 if (error) {
Winkler, Tomas15b16872008-12-19 10:37:33 +0800798 IWL_ERR(priv, "Not a valid rxon_assoc_cmd field values\n");
Zhu Yib481de92007-09-25 17:54:57 -0700799 return -1;
800 }
801 return 0;
802}
803
804/**
Ben Cahill9fbab512007-11-29 11:09:47 +0800805 * iwl3945_full_rxon_required - check if full RXON (vs RXON_ASSOC) cmd is needed
Ian Schram01ebd062007-10-25 17:15:22 +0800806 * @priv: staging_rxon is compared to active_rxon
Zhu Yib481de92007-09-25 17:54:57 -0700807 *
Ben Cahill9fbab512007-11-29 11:09:47 +0800808 * If the RXON structure is changing enough to require a new tune,
809 * or is clearing the RXON_FILTER_ASSOC_MSK, then return 1 to indicate that
810 * a new tune (full RXON command, rather than RXON_ASSOC cmd) is required.
Zhu Yib481de92007-09-25 17:54:57 -0700811 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800812static int iwl3945_full_rxon_required(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700813{
814
815 /* These items are only settable from the full RXON command */
Ron Rindjunsky5d1e2322008-06-30 17:23:04 +0800816 if (!(iwl3945_is_associated(priv)) ||
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800817 compare_ether_addr(priv->staging39_rxon.bssid_addr,
818 priv->active39_rxon.bssid_addr) ||
819 compare_ether_addr(priv->staging39_rxon.node_addr,
820 priv->active39_rxon.node_addr) ||
821 compare_ether_addr(priv->staging39_rxon.wlap_bssid_addr,
822 priv->active39_rxon.wlap_bssid_addr) ||
823 (priv->staging39_rxon.dev_type != priv->active39_rxon.dev_type) ||
824 (priv->staging39_rxon.channel != priv->active39_rxon.channel) ||
825 (priv->staging39_rxon.air_propagation !=
826 priv->active39_rxon.air_propagation) ||
827 (priv->staging39_rxon.assoc_id != priv->active39_rxon.assoc_id))
Zhu Yib481de92007-09-25 17:54:57 -0700828 return 1;
829
830 /* flags, filter_flags, ofdm_basic_rates, and cck_basic_rates can
831 * be updated with the RXON_ASSOC command -- however only some
832 * flag transitions are allowed using RXON_ASSOC */
833
834 /* Check if we are not switching bands */
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800835 if ((priv->staging39_rxon.flags & RXON_FLG_BAND_24G_MSK) !=
836 (priv->active39_rxon.flags & RXON_FLG_BAND_24G_MSK))
Zhu Yib481de92007-09-25 17:54:57 -0700837 return 1;
838
839 /* Check if we are switching association toggle */
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800840 if ((priv->staging39_rxon.filter_flags & RXON_FILTER_ASSOC_MSK) !=
841 (priv->active39_rxon.filter_flags & RXON_FILTER_ASSOC_MSK))
Zhu Yib481de92007-09-25 17:54:57 -0700842 return 1;
843
844 return 0;
845}
846
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800847static int iwl3945_send_rxon_assoc(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700848{
849 int rc = 0;
Tomas Winkler3d24a9f2008-12-19 10:37:07 +0800850 struct iwl_rx_packet *res = NULL;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800851 struct iwl3945_rxon_assoc_cmd rxon_assoc;
Winkler, Tomasc2d79b42008-12-19 10:37:34 +0800852 struct iwl_host_cmd cmd = {
Zhu Yib481de92007-09-25 17:54:57 -0700853 .id = REPLY_RXON_ASSOC,
854 .len = sizeof(rxon_assoc),
855 .meta.flags = CMD_WANT_SKB,
856 .data = &rxon_assoc,
857 };
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800858 const struct iwl3945_rxon_cmd *rxon1 = &priv->staging39_rxon;
859 const struct iwl3945_rxon_cmd *rxon2 = &priv->active39_rxon;
Zhu Yib481de92007-09-25 17:54:57 -0700860
861 if ((rxon1->flags == rxon2->flags) &&
862 (rxon1->filter_flags == rxon2->filter_flags) &&
863 (rxon1->cck_basic_rates == rxon2->cck_basic_rates) &&
864 (rxon1->ofdm_basic_rates == rxon2->ofdm_basic_rates)) {
865 IWL_DEBUG_INFO("Using current RXON_ASSOC. Not resending.\n");
866 return 0;
867 }
868
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800869 rxon_assoc.flags = priv->staging39_rxon.flags;
870 rxon_assoc.filter_flags = priv->staging39_rxon.filter_flags;
871 rxon_assoc.ofdm_basic_rates = priv->staging39_rxon.ofdm_basic_rates;
872 rxon_assoc.cck_basic_rates = priv->staging39_rxon.cck_basic_rates;
Zhu Yib481de92007-09-25 17:54:57 -0700873 rxon_assoc.reserved = 0;
874
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800875 rc = iwl3945_send_cmd_sync(priv, &cmd);
Zhu Yib481de92007-09-25 17:54:57 -0700876 if (rc)
877 return rc;
878
Tomas Winkler3d24a9f2008-12-19 10:37:07 +0800879 res = (struct iwl_rx_packet *)cmd.meta.u.skb->data;
Zhu Yib481de92007-09-25 17:54:57 -0700880 if (res->hdr.flags & IWL_CMD_FAILED_MSK) {
Winkler, Tomas15b16872008-12-19 10:37:33 +0800881 IWL_ERR(priv, "Bad return from REPLY_RXON_ASSOC command\n");
Zhu Yib481de92007-09-25 17:54:57 -0700882 rc = -EIO;
883 }
884
885 priv->alloc_rxb_skb--;
886 dev_kfree_skb_any(cmd.meta.u.skb);
887
888 return rc;
889}
890
891/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800892 * iwl3945_commit_rxon - commit staging_rxon to hardware
Zhu Yib481de92007-09-25 17:54:57 -0700893 *
Ian Schram01ebd062007-10-25 17:15:22 +0800894 * The RXON command in staging_rxon is committed to the hardware and
Zhu Yib481de92007-09-25 17:54:57 -0700895 * the active_rxon structure is updated with the new data. This
896 * function correctly transitions out of the RXON_ASSOC_MSK state if
897 * a HW tune is required based on the RXON structure changes.
898 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800899static int iwl3945_commit_rxon(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700900{
901 /* cast away the const for active_rxon in this function */
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800902 struct iwl3945_rxon_cmd *active_rxon = (void *)&priv->active39_rxon;
Zhu Yib481de92007-09-25 17:54:57 -0700903 int rc = 0;
904
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +0800905 if (!iwl_is_alive(priv))
Zhu Yib481de92007-09-25 17:54:57 -0700906 return -1;
907
908 /* always get timestamp with Rx frame */
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800909 priv->staging39_rxon.flags |= RXON_FLG_TSF2HOST_MSK;
Zhu Yib481de92007-09-25 17:54:57 -0700910
911 /* select antenna */
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800912 priv->staging39_rxon.flags &=
Zhu Yib481de92007-09-25 17:54:57 -0700913 ~(RXON_FLG_DIS_DIV_MSK | RXON_FLG_ANT_SEL_MSK);
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800914 priv->staging39_rxon.flags |= iwl3945_get_antenna_flags(priv);
Zhu Yib481de92007-09-25 17:54:57 -0700915
Samuel Ortiza3139c52008-12-19 10:37:09 +0800916 rc = iwl3945_check_rxon_cmd(priv);
Zhu Yib481de92007-09-25 17:54:57 -0700917 if (rc) {
Winkler, Tomas15b16872008-12-19 10:37:33 +0800918 IWL_ERR(priv, "Invalid RXON configuration. Not committing.\n");
Zhu Yib481de92007-09-25 17:54:57 -0700919 return -EINVAL;
920 }
921
922 /* If we don't need to send a full RXON, we can use
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800923 * iwl3945_rxon_assoc_cmd which is used to reconfigure filter
Zhu Yib481de92007-09-25 17:54:57 -0700924 * and other flags for the current radio configuration. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800925 if (!iwl3945_full_rxon_required(priv)) {
926 rc = iwl3945_send_rxon_assoc(priv);
Zhu Yib481de92007-09-25 17:54:57 -0700927 if (rc) {
Winkler, Tomas15b16872008-12-19 10:37:33 +0800928 IWL_ERR(priv, "Error setting RXON_ASSOC "
Zhu Yib481de92007-09-25 17:54:57 -0700929 "configuration (%d).\n", rc);
930 return rc;
931 }
932
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800933 memcpy(active_rxon, &priv->staging39_rxon, sizeof(*active_rxon));
Zhu Yib481de92007-09-25 17:54:57 -0700934
935 return 0;
936 }
937
938 /* If we are currently associated and the new config requires
939 * an RXON_ASSOC and the new config wants the associated mask enabled,
940 * we must clear the associated from the active configuration
941 * before we apply the new config */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800942 if (iwl3945_is_associated(priv) &&
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800943 (priv->staging39_rxon.filter_flags & RXON_FILTER_ASSOC_MSK)) {
Zhu Yib481de92007-09-25 17:54:57 -0700944 IWL_DEBUG_INFO("Toggling associated bit on current RXON\n");
945 active_rxon->filter_flags &= ~RXON_FILTER_ASSOC_MSK;
946
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800947 rc = iwl3945_send_cmd_pdu(priv, REPLY_RXON,
948 sizeof(struct iwl3945_rxon_cmd),
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800949 &priv->active39_rxon);
Zhu Yib481de92007-09-25 17:54:57 -0700950
951 /* If the mask clearing failed then we set
952 * active_rxon back to what it was previously */
953 if (rc) {
954 active_rxon->filter_flags |= RXON_FILTER_ASSOC_MSK;
Winkler, Tomas15b16872008-12-19 10:37:33 +0800955 IWL_ERR(priv, "Error clearing ASSOC_MSK on current "
Zhu Yib481de92007-09-25 17:54:57 -0700956 "configuration (%d).\n", rc);
957 return rc;
958 }
Zhu Yib481de92007-09-25 17:54:57 -0700959 }
960
961 IWL_DEBUG_INFO("Sending RXON\n"
962 "* with%s RXON_FILTER_ASSOC_MSK\n"
963 "* channel = %d\n"
Johannes Berge1749612008-10-27 15:59:26 -0700964 "* bssid = %pM\n",
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800965 ((priv->staging39_rxon.filter_flags &
Zhu Yib481de92007-09-25 17:54:57 -0700966 RXON_FILTER_ASSOC_MSK) ? "" : "out"),
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800967 le16_to_cpu(priv->staging39_rxon.channel),
Johannes Berge1749612008-10-27 15:59:26 -0700968 priv->staging_rxon.bssid_addr);
Zhu Yib481de92007-09-25 17:54:57 -0700969
970 /* Apply the new configuration */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800971 rc = iwl3945_send_cmd_pdu(priv, REPLY_RXON,
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800972 sizeof(struct iwl3945_rxon_cmd), &priv->staging39_rxon);
Zhu Yib481de92007-09-25 17:54:57 -0700973 if (rc) {
Winkler, Tomas15b16872008-12-19 10:37:33 +0800974 IWL_ERR(priv, "Error setting new configuration (%d).\n", rc);
Zhu Yib481de92007-09-25 17:54:57 -0700975 return rc;
976 }
977
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800978 memcpy(active_rxon, &priv->staging39_rxon, sizeof(*active_rxon));
Zhu Yib481de92007-09-25 17:54:57 -0700979
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800980 iwl3945_clear_stations_table(priv);
Zhu Yi556f8db2007-09-27 11:27:33 +0800981
Zhu Yib481de92007-09-25 17:54:57 -0700982 /* If we issue a new RXON command which required a tune then we must
983 * send a new TXPOWER command or we won't be able to Tx any frames */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800984 rc = iwl3945_hw_reg_send_txpower(priv);
Zhu Yib481de92007-09-25 17:54:57 -0700985 if (rc) {
Winkler, Tomas15b16872008-12-19 10:37:33 +0800986 IWL_ERR(priv, "Error setting Tx power (%d).\n", rc);
Zhu Yib481de92007-09-25 17:54:57 -0700987 return rc;
988 }
989
990 /* Add the broadcast address so we can send broadcast frames */
Kolekar, Abhijeetb5323d32008-12-19 10:37:22 +0800991 if (iwl3945_add_station(priv, iwl_bcast_addr, 0, 0) ==
Zhu Yib481de92007-09-25 17:54:57 -0700992 IWL_INVALID_STATION) {
Winkler, Tomas15b16872008-12-19 10:37:33 +0800993 IWL_ERR(priv, "Error adding BROADCAST address for transmit.\n");
Zhu Yib481de92007-09-25 17:54:57 -0700994 return -EIO;
995 }
996
997 /* If we have set the ASSOC_MSK and we are in BSS mode then
998 * add the IWL_AP_ID to the station rate table */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800999 if (iwl3945_is_associated(priv) &&
Johannes Berg05c914f2008-09-11 00:01:58 +02001000 (priv->iw_mode == NL80211_IFTYPE_STATION))
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001001 if (iwl3945_add_station(priv, priv->active39_rxon.bssid_addr, 1, 0)
Zhu Yib481de92007-09-25 17:54:57 -07001002 == IWL_INVALID_STATION) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08001003 IWL_ERR(priv, "Error adding AP address for transmit\n");
Zhu Yib481de92007-09-25 17:54:57 -07001004 return -EIO;
1005 }
1006
Johannes Berg8318d782008-01-24 19:38:38 +01001007 /* Init the hardware's rate fallback order based on the band */
Zhu Yib481de92007-09-25 17:54:57 -07001008 rc = iwl3945_init_hw_rate_table(priv);
1009 if (rc) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08001010 IWL_ERR(priv, "Error setting HW rate table: %02X\n", rc);
Zhu Yib481de92007-09-25 17:54:57 -07001011 return -EIO;
1012 }
1013
1014 return 0;
1015}
1016
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001017static int iwl3945_send_bt_config(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001018{
Tomas Winkler4c897252008-12-19 10:37:05 +08001019 struct iwl_bt_cmd bt_cmd = {
Zhu Yib481de92007-09-25 17:54:57 -07001020 .flags = 3,
1021 .lead_time = 0xAA,
1022 .max_kill = 1,
1023 .kill_ack_mask = 0,
1024 .kill_cts_mask = 0,
1025 };
1026
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001027 return iwl3945_send_cmd_pdu(priv, REPLY_BT_CONFIG,
Tomas Winkler4c897252008-12-19 10:37:05 +08001028 sizeof(bt_cmd), &bt_cmd);
Zhu Yib481de92007-09-25 17:54:57 -07001029}
1030
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001031static int iwl3945_send_scan_abort(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001032{
1033 int rc = 0;
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08001034 struct iwl_rx_packet *res;
Winkler, Tomasc2d79b42008-12-19 10:37:34 +08001035 struct iwl_host_cmd cmd = {
Zhu Yib481de92007-09-25 17:54:57 -07001036 .id = REPLY_SCAN_ABORT_CMD,
1037 .meta.flags = CMD_WANT_SKB,
1038 };
1039
1040 /* If there isn't a scan actively going on in the hardware
1041 * then we are in between scan bands and not actually
1042 * actively scanning, so don't send the abort command */
1043 if (!test_bit(STATUS_SCAN_HW, &priv->status)) {
1044 clear_bit(STATUS_SCAN_ABORTING, &priv->status);
1045 return 0;
1046 }
1047
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001048 rc = iwl3945_send_cmd_sync(priv, &cmd);
Zhu Yib481de92007-09-25 17:54:57 -07001049 if (rc) {
1050 clear_bit(STATUS_SCAN_ABORTING, &priv->status);
1051 return rc;
1052 }
1053
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08001054 res = (struct iwl_rx_packet *)cmd.meta.u.skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07001055 if (res->u.status != CAN_ABORT_STATUS) {
1056 /* The scan abort will return 1 for success or
1057 * 2 for "failure". A failure condition can be
1058 * due to simply not being in an active scan which
1059 * can occur if we send the scan abort before we
1060 * the microcode has notified us that a scan is
1061 * completed. */
1062 IWL_DEBUG_INFO("SCAN_ABORT returned %d.\n", res->u.status);
1063 clear_bit(STATUS_SCAN_ABORTING, &priv->status);
1064 clear_bit(STATUS_SCAN_HW, &priv->status);
1065 }
1066
1067 dev_kfree_skb_any(cmd.meta.u.skb);
1068
1069 return rc;
1070}
1071
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001072static int iwl3945_add_sta_sync_callback(struct iwl_priv *priv,
Winkler, Tomasc2d79b42008-12-19 10:37:34 +08001073 struct iwl_cmd *cmd, struct sk_buff *skb)
Zhu Yib481de92007-09-25 17:54:57 -07001074{
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08001075 struct iwl_rx_packet *res = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07001076
1077 if (!skb) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08001078 IWL_ERR(priv, "Error: Response NULL in REPLY_ADD_STA.\n");
Zhu Yib481de92007-09-25 17:54:57 -07001079 return 1;
1080 }
1081
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08001082 res = (struct iwl_rx_packet *)skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07001083 if (res->hdr.flags & IWL_CMD_FAILED_MSK) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08001084 IWL_ERR(priv, "Bad return from REPLY_ADD_STA (0x%08X)\n",
Zhu Yib481de92007-09-25 17:54:57 -07001085 res->hdr.flags);
1086 return 1;
1087 }
1088
1089 switch (res->u.add_sta.status) {
1090 case ADD_STA_SUCCESS_MSK:
1091 break;
1092 default:
1093 break;
1094 }
1095
1096 /* We didn't cache the SKB; let the caller free it */
1097 return 1;
1098}
1099
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001100int iwl3945_send_add_station(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001101 struct iwl3945_addsta_cmd *sta, u8 flags)
Zhu Yib481de92007-09-25 17:54:57 -07001102{
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08001103 struct iwl_rx_packet *res = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07001104 int rc = 0;
Winkler, Tomasc2d79b42008-12-19 10:37:34 +08001105 struct iwl_host_cmd cmd = {
Zhu Yib481de92007-09-25 17:54:57 -07001106 .id = REPLY_ADD_STA,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001107 .len = sizeof(struct iwl3945_addsta_cmd),
Zhu Yib481de92007-09-25 17:54:57 -07001108 .meta.flags = flags,
1109 .data = sta,
1110 };
1111
1112 if (flags & CMD_ASYNC)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001113 cmd.meta.u.callback = iwl3945_add_sta_sync_callback;
Zhu Yib481de92007-09-25 17:54:57 -07001114 else
1115 cmd.meta.flags |= CMD_WANT_SKB;
1116
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001117 rc = iwl3945_send_cmd(priv, &cmd);
Zhu Yib481de92007-09-25 17:54:57 -07001118
1119 if (rc || (flags & CMD_ASYNC))
1120 return rc;
1121
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08001122 res = (struct iwl_rx_packet *)cmd.meta.u.skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07001123 if (res->hdr.flags & IWL_CMD_FAILED_MSK) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08001124 IWL_ERR(priv, "Bad return from REPLY_ADD_STA (0x%08X)\n",
Zhu Yib481de92007-09-25 17:54:57 -07001125 res->hdr.flags);
1126 rc = -EIO;
1127 }
1128
1129 if (rc == 0) {
1130 switch (res->u.add_sta.status) {
1131 case ADD_STA_SUCCESS_MSK:
1132 IWL_DEBUG_INFO("REPLY_ADD_STA PASSED\n");
1133 break;
1134 default:
1135 rc = -EIO;
Winkler, Tomas39aadf82008-12-19 10:37:32 +08001136 IWL_WARN(priv, "REPLY_ADD_STA failed\n");
Zhu Yib481de92007-09-25 17:54:57 -07001137 break;
1138 }
1139 }
1140
1141 priv->alloc_rxb_skb--;
1142 dev_kfree_skb_any(cmd.meta.u.skb);
1143
1144 return rc;
1145}
1146
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001147static int iwl3945_update_sta_key_info(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07001148 struct ieee80211_key_conf *keyconf,
1149 u8 sta_id)
1150{
1151 unsigned long flags;
1152 __le16 key_flags = 0;
1153
1154 switch (keyconf->alg) {
1155 case ALG_CCMP:
1156 key_flags |= STA_KEY_FLG_CCMP;
1157 key_flags |= cpu_to_le16(
1158 keyconf->keyidx << STA_KEY_FLG_KEYID_POS);
1159 key_flags &= ~STA_KEY_FLG_INVALID;
1160 break;
1161 case ALG_TKIP:
1162 case ALG_WEP:
Zhu Yib481de92007-09-25 17:54:57 -07001163 default:
1164 return -EINVAL;
1165 }
1166 spin_lock_irqsave(&priv->sta_lock, flags);
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001167 priv->stations_39[sta_id].keyinfo.alg = keyconf->alg;
1168 priv->stations_39[sta_id].keyinfo.keylen = keyconf->keylen;
1169 memcpy(priv->stations_39[sta_id].keyinfo.key, keyconf->key,
Zhu Yib481de92007-09-25 17:54:57 -07001170 keyconf->keylen);
1171
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001172 memcpy(priv->stations_39[sta_id].sta.key.key, keyconf->key,
Zhu Yib481de92007-09-25 17:54:57 -07001173 keyconf->keylen);
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001174 priv->stations_39[sta_id].sta.key.key_flags = key_flags;
1175 priv->stations_39[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
1176 priv->stations_39[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07001177
1178 spin_unlock_irqrestore(&priv->sta_lock, flags);
1179
1180 IWL_DEBUG_INFO("hwcrypto: modify ucode station key info\n");
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001181 iwl3945_send_add_station(priv, &priv->stations_39[sta_id].sta, 0);
Zhu Yib481de92007-09-25 17:54:57 -07001182 return 0;
1183}
1184
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001185static int iwl3945_clear_sta_key_info(struct iwl_priv *priv, u8 sta_id)
Zhu Yib481de92007-09-25 17:54:57 -07001186{
1187 unsigned long flags;
1188
1189 spin_lock_irqsave(&priv->sta_lock, flags);
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001190 memset(&priv->stations_39[sta_id].keyinfo, 0, sizeof(struct iwl3945_hw_key));
1191 memset(&priv->stations_39[sta_id].sta.key, 0,
Tomas Winkler4c897252008-12-19 10:37:05 +08001192 sizeof(struct iwl4965_keyinfo));
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001193 priv->stations_39[sta_id].sta.key.key_flags = STA_KEY_FLG_NO_ENC;
1194 priv->stations_39[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
1195 priv->stations_39[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07001196 spin_unlock_irqrestore(&priv->sta_lock, flags);
1197
1198 IWL_DEBUG_INFO("hwcrypto: clear ucode station key info\n");
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001199 iwl3945_send_add_station(priv, &priv->stations_39[sta_id].sta, 0);
Zhu Yib481de92007-09-25 17:54:57 -07001200 return 0;
1201}
1202
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001203static void iwl3945_clear_free_frames(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001204{
1205 struct list_head *element;
1206
1207 IWL_DEBUG_INFO("%d frames on pre-allocated heap on clear.\n",
1208 priv->frames_count);
1209
1210 while (!list_empty(&priv->free_frames)) {
1211 element = priv->free_frames.next;
1212 list_del(element);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001213 kfree(list_entry(element, struct iwl3945_frame, list));
Zhu Yib481de92007-09-25 17:54:57 -07001214 priv->frames_count--;
1215 }
1216
1217 if (priv->frames_count) {
Winkler, Tomas39aadf82008-12-19 10:37:32 +08001218 IWL_WARN(priv, "%d frames still in use. Did we lose one?\n",
Zhu Yib481de92007-09-25 17:54:57 -07001219 priv->frames_count);
1220 priv->frames_count = 0;
1221 }
1222}
1223
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001224static struct iwl3945_frame *iwl3945_get_free_frame(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001225{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001226 struct iwl3945_frame *frame;
Zhu Yib481de92007-09-25 17:54:57 -07001227 struct list_head *element;
1228 if (list_empty(&priv->free_frames)) {
1229 frame = kzalloc(sizeof(*frame), GFP_KERNEL);
1230 if (!frame) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08001231 IWL_ERR(priv, "Could not allocate frame!\n");
Zhu Yib481de92007-09-25 17:54:57 -07001232 return NULL;
1233 }
1234
1235 priv->frames_count++;
1236 return frame;
1237 }
1238
1239 element = priv->free_frames.next;
1240 list_del(element);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001241 return list_entry(element, struct iwl3945_frame, list);
Zhu Yib481de92007-09-25 17:54:57 -07001242}
1243
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001244static void iwl3945_free_frame(struct iwl_priv *priv, struct iwl3945_frame *frame)
Zhu Yib481de92007-09-25 17:54:57 -07001245{
1246 memset(frame, 0, sizeof(*frame));
1247 list_add(&frame->list, &priv->free_frames);
1248}
1249
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001250unsigned int iwl3945_fill_beacon_frame(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07001251 struct ieee80211_hdr *hdr,
Rami Rosen73ec1cc2008-12-16 09:37:07 +02001252 int left)
Zhu Yib481de92007-09-25 17:54:57 -07001253{
1254
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001255 if (!iwl3945_is_associated(priv) || !priv->ibss_beacon ||
Johannes Berg05c914f2008-09-11 00:01:58 +02001256 ((priv->iw_mode != NL80211_IFTYPE_ADHOC) &&
1257 (priv->iw_mode != NL80211_IFTYPE_AP)))
Zhu Yib481de92007-09-25 17:54:57 -07001258 return 0;
1259
1260 if (priv->ibss_beacon->len > left)
1261 return 0;
1262
1263 memcpy(hdr, priv->ibss_beacon->data, priv->ibss_beacon->len);
1264
1265 return priv->ibss_beacon->len;
1266}
1267
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001268static u8 iwl3945_rate_get_lowest_plcp(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001269{
1270 u8 i;
Kolekar, Abhijeetc24f0812008-11-07 09:58:44 -08001271 int rate_mask;
1272
1273 /* Set rate mask*/
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001274 if (priv->staging39_rxon.flags & RXON_FLG_BAND_24G_MSK)
Chatre, Reinettedbce56a2008-11-12 13:14:07 -08001275 rate_mask = priv->active_rate_basic & IWL_CCK_RATES_MASK;
Kolekar, Abhijeetc24f0812008-11-07 09:58:44 -08001276 else
Chatre, Reinettedbce56a2008-11-12 13:14:07 -08001277 rate_mask = priv->active_rate_basic & IWL_OFDM_RATES_MASK;
Zhu Yib481de92007-09-25 17:54:57 -07001278
1279 for (i = IWL_RATE_1M_INDEX; i != IWL_RATE_INVALID;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001280 i = iwl3945_rates[i].next_ieee) {
Zhu Yib481de92007-09-25 17:54:57 -07001281 if (rate_mask & (1 << i))
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001282 return iwl3945_rates[i].plcp;
Zhu Yib481de92007-09-25 17:54:57 -07001283 }
1284
Kolekar, Abhijeetc24f0812008-11-07 09:58:44 -08001285 /* No valid rate was found. Assign the lowest one */
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001286 if (priv->staging39_rxon.flags & RXON_FLG_BAND_24G_MSK)
Kolekar, Abhijeetc24f0812008-11-07 09:58:44 -08001287 return IWL_RATE_1M_PLCP;
1288 else
1289 return IWL_RATE_6M_PLCP;
Zhu Yib481de92007-09-25 17:54:57 -07001290}
1291
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001292static int iwl3945_send_beacon_cmd(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001293{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001294 struct iwl3945_frame *frame;
Zhu Yib481de92007-09-25 17:54:57 -07001295 unsigned int frame_size;
1296 int rc;
1297 u8 rate;
1298
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001299 frame = iwl3945_get_free_frame(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001300
1301 if (!frame) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08001302 IWL_ERR(priv, "Could not obtain free frame buffer for beacon "
Zhu Yib481de92007-09-25 17:54:57 -07001303 "command.\n");
1304 return -ENOMEM;
1305 }
1306
Kolekar, Abhijeetc24f0812008-11-07 09:58:44 -08001307 rate = iwl3945_rate_get_lowest_plcp(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001308
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001309 frame_size = iwl3945_hw_get_beacon_cmd(priv, frame, rate);
Zhu Yib481de92007-09-25 17:54:57 -07001310
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001311 rc = iwl3945_send_cmd_pdu(priv, REPLY_TX_BEACON, frame_size,
Zhu Yib481de92007-09-25 17:54:57 -07001312 &frame->u.cmd[0]);
1313
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001314 iwl3945_free_frame(priv, frame);
Zhu Yib481de92007-09-25 17:54:57 -07001315
1316 return rc;
1317}
1318
1319/******************************************************************************
1320 *
1321 * EEPROM related functions
1322 *
1323 ******************************************************************************/
1324
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001325static void get_eeprom_mac(struct iwl_priv *priv, u8 *mac)
Zhu Yib481de92007-09-25 17:54:57 -07001326{
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001327 memcpy(mac, priv->eeprom39.mac_address, 6);
Zhu Yib481de92007-09-25 17:54:57 -07001328}
1329
Reinette Chatre74a3a252008-01-23 10:15:19 -08001330/*
1331 * Clear the OWNER_MSK, to establish driver (instead of uCode running on
1332 * embedded controller) as EEPROM reader; each read is a series of pulses
1333 * to/from the EEPROM chip, not a single event, so even reads could conflict
1334 * if they weren't arbitrated by some ownership mechanism. Here, the driver
1335 * simply claims ownership, which should be safe when this function is called
1336 * (i.e. before loading uCode!).
1337 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001338static inline int iwl3945_eeprom_acquire_semaphore(struct iwl_priv *priv)
Reinette Chatre74a3a252008-01-23 10:15:19 -08001339{
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08001340 _iwl_clear_bit(priv, CSR_EEPROM_GP, CSR_EEPROM_GP_IF_OWNER_MSK);
Reinette Chatre74a3a252008-01-23 10:15:19 -08001341 return 0;
1342}
1343
Zhu Yib481de92007-09-25 17:54:57 -07001344/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001345 * iwl3945_eeprom_init - read EEPROM contents
Zhu Yib481de92007-09-25 17:54:57 -07001346 *
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001347 * Load the EEPROM contents from adapter into priv->eeprom39
Zhu Yib481de92007-09-25 17:54:57 -07001348 *
1349 * NOTE: This routine uses the non-debug IO access functions.
1350 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001351int iwl3945_eeprom_init(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001352{
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001353 u16 *e = (u16 *)&priv->eeprom39;
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08001354 u32 gp = iwl_read32(priv, CSR_EEPROM_GP);
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001355 int sz = sizeof(priv->eeprom39);
Zhu, Yi3d5717a2008-12-11 10:33:36 -08001356 int ret;
Zhu Yib481de92007-09-25 17:54:57 -07001357 u16 addr;
1358
1359 /* The EEPROM structure has several padding buffers within it
1360 * and when adding new EEPROM maps is subject to programmer errors
1361 * which may be very difficult to identify without explicitly
1362 * checking the resulting size of the eeprom map. */
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001363 BUILD_BUG_ON(sizeof(priv->eeprom39) != IWL_EEPROM_IMAGE_SIZE);
Zhu Yib481de92007-09-25 17:54:57 -07001364
1365 if ((gp & CSR_EEPROM_GP_VALID_MSK) == CSR_EEPROM_GP_BAD_SIGNATURE) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08001366 IWL_ERR(priv, "EEPROM not found, EEPROM_GP=0x%08x\n", gp);
Zhu Yib481de92007-09-25 17:54:57 -07001367 return -ENOENT;
1368 }
1369
Cahill, Ben M6440adb2007-11-29 11:09:55 +08001370 /* Make sure driver (instead of uCode) is allowed to read EEPROM */
Zhu, Yi3d5717a2008-12-11 10:33:36 -08001371 ret = iwl3945_eeprom_acquire_semaphore(priv);
1372 if (ret < 0) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08001373 IWL_ERR(priv, "Failed to acquire EEPROM semaphore.\n");
Zhu Yib481de92007-09-25 17:54:57 -07001374 return -ENOENT;
1375 }
1376
1377 /* eeprom is an array of 16bit values */
1378 for (addr = 0; addr < sz; addr += sizeof(u16)) {
Zhu, Yi3d5717a2008-12-11 10:33:36 -08001379 u32 r;
1380
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08001381 _iwl_write32(priv, CSR_EEPROM_REG,
Zhu, Yi3d5717a2008-12-11 10:33:36 -08001382 CSR_EEPROM_REG_MSK_ADDR & (addr << 1));
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08001383 _iwl_clear_bit(priv, CSR_EEPROM_REG, CSR_EEPROM_REG_BIT_CMD);
1384 ret = iwl_poll_direct_bit(priv, CSR_EEPROM_REG,
Zhu, Yi3d5717a2008-12-11 10:33:36 -08001385 CSR_EEPROM_REG_READ_VALID_MSK,
1386 IWL_EEPROM_ACCESS_TIMEOUT);
1387 if (ret < 0) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08001388 IWL_ERR(priv, "Time out reading EEPROM[%d]\n", addr);
Zhu, Yi3d5717a2008-12-11 10:33:36 -08001389 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07001390 }
Zhu, Yi3d5717a2008-12-11 10:33:36 -08001391
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08001392 r = _iwl_read_direct32(priv, CSR_EEPROM_REG);
Tomas Winkler58ff6d42008-02-13 02:47:54 +02001393 e[addr / 2] = le16_to_cpu((__force __le16)(r >> 16));
Zhu Yib481de92007-09-25 17:54:57 -07001394 }
1395
1396 return 0;
1397}
1398
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001399static void iwl3945_unset_hw_params(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001400{
Abhijeet Kolekar3832ec92008-12-19 10:37:26 +08001401 if (priv->shared_virt)
Zhu Yib481de92007-09-25 17:54:57 -07001402 pci_free_consistent(priv->pci_dev,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001403 sizeof(struct iwl3945_shared),
Abhijeet Kolekar3832ec92008-12-19 10:37:26 +08001404 priv->shared_virt,
1405 priv->shared_phys);
Zhu Yib481de92007-09-25 17:54:57 -07001406}
1407
1408/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001409 * iwl3945_supported_rate_to_ie - fill in the supported rate in IE field
Zhu Yib481de92007-09-25 17:54:57 -07001410 *
1411 * return : set the bit for each supported rate insert in ie
1412 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001413static u16 iwl3945_supported_rate_to_ie(u8 *ie, u16 supported_rate,
Tomas Winklerc7c46672007-10-18 02:04:15 +02001414 u16 basic_rate, int *left)
Zhu Yib481de92007-09-25 17:54:57 -07001415{
1416 u16 ret_rates = 0, bit;
1417 int i;
Tomas Winklerc7c46672007-10-18 02:04:15 +02001418 u8 *cnt = ie;
1419 u8 *rates = ie + 1;
Zhu Yib481de92007-09-25 17:54:57 -07001420
1421 for (bit = 1, i = 0; i < IWL_RATE_COUNT; i++, bit <<= 1) {
1422 if (bit & supported_rate) {
1423 ret_rates |= bit;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001424 rates[*cnt] = iwl3945_rates[i].ieee |
Tomas Winklerc7c46672007-10-18 02:04:15 +02001425 ((bit & basic_rate) ? 0x80 : 0x00);
1426 (*cnt)++;
1427 (*left)--;
1428 if ((*left <= 0) ||
1429 (*cnt >= IWL_SUPPORTED_RATES_IE_LEN))
Zhu Yib481de92007-09-25 17:54:57 -07001430 break;
1431 }
1432 }
1433
1434 return ret_rates;
1435}
1436
1437/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001438 * iwl3945_fill_probe_req - fill in all required fields and IE for probe request
Zhu Yib481de92007-09-25 17:54:57 -07001439 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001440static u16 iwl3945_fill_probe_req(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07001441 struct ieee80211_mgmt *frame,
Johannes Berg430cfe92008-10-28 18:06:02 +01001442 int left)
Zhu Yib481de92007-09-25 17:54:57 -07001443{
1444 int len = 0;
1445 u8 *pos = NULL;
Tomas Winklerc7c46672007-10-18 02:04:15 +02001446 u16 active_rates, ret_rates, cck_rates;
Zhu Yib481de92007-09-25 17:54:57 -07001447
1448 /* Make sure there is enough space for the probe request,
1449 * two mandatory IEs and the data */
1450 left -= 24;
1451 if (left < 0)
1452 return 0;
1453 len += 24;
1454
1455 frame->frame_control = cpu_to_le16(IEEE80211_STYPE_PROBE_REQ);
Kolekar, Abhijeetb5323d32008-12-19 10:37:22 +08001456 memcpy(frame->da, iwl_bcast_addr, ETH_ALEN);
Zhu Yib481de92007-09-25 17:54:57 -07001457 memcpy(frame->sa, priv->mac_addr, ETH_ALEN);
Kolekar, Abhijeetb5323d32008-12-19 10:37:22 +08001458 memcpy(frame->bssid, iwl_bcast_addr, ETH_ALEN);
Zhu Yib481de92007-09-25 17:54:57 -07001459 frame->seq_ctrl = 0;
1460
1461 /* fill in our indirect SSID IE */
1462 /* ...next IE... */
1463
1464 left -= 2;
1465 if (left < 0)
1466 return 0;
1467 len += 2;
1468 pos = &(frame->u.probe_req.variable[0]);
1469 *pos++ = WLAN_EID_SSID;
1470 *pos++ = 0;
1471
Zhu Yib481de92007-09-25 17:54:57 -07001472 /* fill in supported rate */
1473 /* ...next IE... */
1474 left -= 2;
1475 if (left < 0)
1476 return 0;
Tomas Winklerc7c46672007-10-18 02:04:15 +02001477
Zhu Yib481de92007-09-25 17:54:57 -07001478 /* ... fill it in... */
1479 *pos++ = WLAN_EID_SUPP_RATES;
1480 *pos = 0;
Tomas Winklerc7c46672007-10-18 02:04:15 +02001481
1482 priv->active_rate = priv->rates_mask;
1483 active_rates = priv->active_rate;
Zhu Yib481de92007-09-25 17:54:57 -07001484 priv->active_rate_basic = priv->rates_mask & IWL_BASIC_RATES_MASK;
1485
Tomas Winklerc7c46672007-10-18 02:04:15 +02001486 cck_rates = IWL_CCK_RATES_MASK & active_rates;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001487 ret_rates = iwl3945_supported_rate_to_ie(pos, cck_rates,
Tomas Winklerc7c46672007-10-18 02:04:15 +02001488 priv->active_rate_basic, &left);
1489 active_rates &= ~ret_rates;
1490
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001491 ret_rates = iwl3945_supported_rate_to_ie(pos, active_rates,
Tomas Winklerc7c46672007-10-18 02:04:15 +02001492 priv->active_rate_basic, &left);
1493 active_rates &= ~ret_rates;
1494
Zhu Yib481de92007-09-25 17:54:57 -07001495 len += 2 + *pos;
1496 pos += (*pos) + 1;
Tomas Winklerc7c46672007-10-18 02:04:15 +02001497 if (active_rates == 0)
Zhu Yib481de92007-09-25 17:54:57 -07001498 goto fill_end;
1499
1500 /* fill in supported extended rate */
1501 /* ...next IE... */
1502 left -= 2;
1503 if (left < 0)
1504 return 0;
1505 /* ... fill it in... */
1506 *pos++ = WLAN_EID_EXT_SUPP_RATES;
1507 *pos = 0;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001508 iwl3945_supported_rate_to_ie(pos, active_rates,
Tomas Winklerc7c46672007-10-18 02:04:15 +02001509 priv->active_rate_basic, &left);
Zhu Yib481de92007-09-25 17:54:57 -07001510 if (*pos > 0)
1511 len += 2 + *pos;
1512
1513 fill_end:
1514 return (u16)len;
1515}
1516
1517/*
1518 * QoS support
1519*/
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001520static int iwl3945_send_qos_params_command(struct iwl_priv *priv,
Tomas Winkler4c897252008-12-19 10:37:05 +08001521 struct iwl_qosparam_cmd *qos)
Zhu Yib481de92007-09-25 17:54:57 -07001522{
1523
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001524 return iwl3945_send_cmd_pdu(priv, REPLY_QOS_PARAM,
Tomas Winkler4c897252008-12-19 10:37:05 +08001525 sizeof(struct iwl_qosparam_cmd), qos);
Zhu Yib481de92007-09-25 17:54:57 -07001526}
1527
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001528static void iwl3945_activate_qos(struct iwl_priv *priv, u8 force)
Zhu Yib481de92007-09-25 17:54:57 -07001529{
1530 unsigned long flags;
1531
Zhu Yib481de92007-09-25 17:54:57 -07001532 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
1533 return;
1534
Zhu Yib481de92007-09-25 17:54:57 -07001535 spin_lock_irqsave(&priv->lock, flags);
1536 priv->qos_data.def_qos_parm.qos_flags = 0;
1537
1538 if (priv->qos_data.qos_cap.q_AP.queue_request &&
1539 !priv->qos_data.qos_cap.q_AP.txop_request)
1540 priv->qos_data.def_qos_parm.qos_flags |=
1541 QOS_PARAM_FLG_TXOP_TYPE_MSK;
1542
1543 if (priv->qos_data.qos_active)
1544 priv->qos_data.def_qos_parm.qos_flags |=
1545 QOS_PARAM_FLG_UPDATE_EDCA_MSK;
1546
1547 spin_unlock_irqrestore(&priv->lock, flags);
1548
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001549 if (force || iwl3945_is_associated(priv)) {
Tomas Winklera96a27f2008-10-23 23:48:56 -07001550 IWL_DEBUG_QOS("send QoS cmd with QoS active %d \n",
Zhu Yib481de92007-09-25 17:54:57 -07001551 priv->qos_data.qos_active);
1552
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001553 iwl3945_send_qos_params_command(priv,
Zhu Yib481de92007-09-25 17:54:57 -07001554 &(priv->qos_data.def_qos_parm));
1555 }
1556}
1557
Zhu Yib481de92007-09-25 17:54:57 -07001558/*
1559 * Power management (not Tx power!) functions
1560 */
1561#define MSEC_TO_USEC 1024
1562
Tomas Winkler600c0e12008-12-19 10:37:04 +08001563
1564#define NOSLP __constant_cpu_to_le16(0), 0, 0
1565#define SLP IWL_POWER_DRIVER_ALLOW_SLEEP_MSK, 0, 0
Zhu Yib481de92007-09-25 17:54:57 -07001566#define SLP_TIMEOUT(T) __constant_cpu_to_le32((T) * MSEC_TO_USEC)
1567#define SLP_VEC(X0, X1, X2, X3, X4) {__constant_cpu_to_le32(X0), \
1568 __constant_cpu_to_le32(X1), \
1569 __constant_cpu_to_le32(X2), \
1570 __constant_cpu_to_le32(X3), \
1571 __constant_cpu_to_le32(X4)}
1572
Zhu Yib481de92007-09-25 17:54:57 -07001573/* default power management (not Tx power) table values */
Tomas Winklera96a27f2008-10-23 23:48:56 -07001574/* for TIM 0-10 */
Samuel Ortiz1125eff2008-12-19 10:37:14 +08001575static struct iwl_power_vec_entry range_0[IWL39_POWER_AC] = {
Zhu Yib481de92007-09-25 17:54:57 -07001576 {{NOSLP, SLP_TIMEOUT(0), SLP_TIMEOUT(0), SLP_VEC(0, 0, 0, 0, 0)}, 0},
1577 {{SLP, SLP_TIMEOUT(200), SLP_TIMEOUT(500), SLP_VEC(1, 2, 3, 4, 4)}, 0},
1578 {{SLP, SLP_TIMEOUT(200), SLP_TIMEOUT(300), SLP_VEC(2, 4, 6, 7, 7)}, 0},
1579 {{SLP, SLP_TIMEOUT(50), SLP_TIMEOUT(100), SLP_VEC(2, 6, 9, 9, 10)}, 0},
1580 {{SLP, SLP_TIMEOUT(50), SLP_TIMEOUT(25), SLP_VEC(2, 7, 9, 9, 10)}, 1},
1581 {{SLP, SLP_TIMEOUT(25), SLP_TIMEOUT(25), SLP_VEC(4, 7, 10, 10, 10)}, 1}
1582};
1583
Tomas Winklera96a27f2008-10-23 23:48:56 -07001584/* for TIM > 10 */
Samuel Ortiz1125eff2008-12-19 10:37:14 +08001585static struct iwl_power_vec_entry range_1[IWL39_POWER_AC] = {
Zhu Yib481de92007-09-25 17:54:57 -07001586 {{NOSLP, SLP_TIMEOUT(0), SLP_TIMEOUT(0), SLP_VEC(0, 0, 0, 0, 0)}, 0},
1587 {{SLP, SLP_TIMEOUT(200), SLP_TIMEOUT(500),
1588 SLP_VEC(1, 2, 3, 4, 0xFF)}, 0},
1589 {{SLP, SLP_TIMEOUT(200), SLP_TIMEOUT(300),
1590 SLP_VEC(2, 4, 6, 7, 0xFF)}, 0},
1591 {{SLP, SLP_TIMEOUT(50), SLP_TIMEOUT(100),
1592 SLP_VEC(2, 6, 9, 9, 0xFF)}, 0},
1593 {{SLP, SLP_TIMEOUT(50), SLP_TIMEOUT(25), SLP_VEC(2, 7, 9, 9, 0xFF)}, 0},
1594 {{SLP, SLP_TIMEOUT(25), SLP_TIMEOUT(25),
1595 SLP_VEC(4, 7, 10, 10, 0xFF)}, 0}
1596};
1597
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001598int iwl3945_power_init_handle(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001599{
1600 int rc = 0, i;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001601 struct iwl3945_power_mgr *pow_data;
Samuel Ortiz1125eff2008-12-19 10:37:14 +08001602 int size = sizeof(struct iwl_power_vec_entry) * IWL39_POWER_AC;
Zhu Yib481de92007-09-25 17:54:57 -07001603 u16 pci_pm;
1604
1605 IWL_DEBUG_POWER("Initialize power \n");
1606
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001607 pow_data = &(priv->power_data_39);
Zhu Yib481de92007-09-25 17:54:57 -07001608
1609 memset(pow_data, 0, sizeof(*pow_data));
1610
1611 pow_data->active_index = IWL_POWER_RANGE_0;
1612 pow_data->dtim_val = 0xffff;
1613
1614 memcpy(&pow_data->pwr_range_0[0], &range_0[0], size);
1615 memcpy(&pow_data->pwr_range_1[0], &range_1[0], size);
1616
1617 rc = pci_read_config_word(priv->pci_dev, PCI_LINK_CTRL, &pci_pm);
1618 if (rc != 0)
1619 return 0;
1620 else {
Tomas Winkler600c0e12008-12-19 10:37:04 +08001621 struct iwl_powertable_cmd *cmd;
Zhu Yib481de92007-09-25 17:54:57 -07001622
1623 IWL_DEBUG_POWER("adjust power command flags\n");
1624
Samuel Ortiz1125eff2008-12-19 10:37:14 +08001625 for (i = 0; i < IWL39_POWER_AC; i++) {
Zhu Yib481de92007-09-25 17:54:57 -07001626 cmd = &pow_data->pwr_range_0[i].cmd;
1627
1628 if (pci_pm & 0x1)
1629 cmd->flags &= ~IWL_POWER_PCI_PM_MSK;
1630 else
1631 cmd->flags |= IWL_POWER_PCI_PM_MSK;
1632 }
1633 }
1634 return rc;
1635}
1636
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001637static int iwl3945_update_power_cmd(struct iwl_priv *priv,
Tomas Winkler600c0e12008-12-19 10:37:04 +08001638 struct iwl_powertable_cmd *cmd, u32 mode)
Zhu Yib481de92007-09-25 17:54:57 -07001639{
1640 int rc = 0, i;
1641 u8 skip;
1642 u32 max_sleep = 0;
Samuel Ortiz1125eff2008-12-19 10:37:14 +08001643 struct iwl_power_vec_entry *range;
Zhu Yib481de92007-09-25 17:54:57 -07001644 u8 period = 0;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001645 struct iwl3945_power_mgr *pow_data;
Zhu Yib481de92007-09-25 17:54:57 -07001646
1647 if (mode > IWL_POWER_INDEX_5) {
1648 IWL_DEBUG_POWER("Error invalid power mode \n");
1649 return -1;
1650 }
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001651 pow_data = &(priv->power_data_39);
Zhu Yib481de92007-09-25 17:54:57 -07001652
1653 if (pow_data->active_index == IWL_POWER_RANGE_0)
1654 range = &pow_data->pwr_range_0[0];
1655 else
1656 range = &pow_data->pwr_range_1[1];
1657
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001658 memcpy(cmd, &range[mode].cmd, sizeof(struct iwl3945_powertable_cmd));
Zhu Yib481de92007-09-25 17:54:57 -07001659
1660#ifdef IWL_MAC80211_DISABLE
1661 if (priv->assoc_network != NULL) {
1662 unsigned long flags;
1663
1664 period = priv->assoc_network->tim.tim_period;
1665 }
1666#endif /*IWL_MAC80211_DISABLE */
1667 skip = range[mode].no_dtim;
1668
1669 if (period == 0) {
1670 period = 1;
1671 skip = 0;
1672 }
1673
1674 if (skip == 0) {
1675 max_sleep = period;
1676 cmd->flags &= ~IWL_POWER_SLEEP_OVER_DTIM_MSK;
1677 } else {
1678 __le32 slp_itrvl = cmd->sleep_interval[IWL_POWER_VEC_SIZE - 1];
1679 max_sleep = (le32_to_cpu(slp_itrvl) / period) * period;
1680 cmd->flags |= IWL_POWER_SLEEP_OVER_DTIM_MSK;
1681 }
1682
1683 for (i = 0; i < IWL_POWER_VEC_SIZE; i++) {
1684 if (le32_to_cpu(cmd->sleep_interval[i]) > max_sleep)
1685 cmd->sleep_interval[i] = cpu_to_le32(max_sleep);
1686 }
1687
1688 IWL_DEBUG_POWER("Flags value = 0x%08X\n", cmd->flags);
1689 IWL_DEBUG_POWER("Tx timeout = %u\n", le32_to_cpu(cmd->tx_data_timeout));
1690 IWL_DEBUG_POWER("Rx timeout = %u\n", le32_to_cpu(cmd->rx_data_timeout));
1691 IWL_DEBUG_POWER("Sleep interval vector = { %d , %d , %d , %d , %d }\n",
1692 le32_to_cpu(cmd->sleep_interval[0]),
1693 le32_to_cpu(cmd->sleep_interval[1]),
1694 le32_to_cpu(cmd->sleep_interval[2]),
1695 le32_to_cpu(cmd->sleep_interval[3]),
1696 le32_to_cpu(cmd->sleep_interval[4]));
1697
1698 return rc;
1699}
1700
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001701static int iwl3945_send_power_mode(struct iwl_priv *priv, u32 mode)
Zhu Yib481de92007-09-25 17:54:57 -07001702{
John W. Linville9a62f732007-11-15 16:27:36 -05001703 u32 uninitialized_var(final_mode);
Zhu Yib481de92007-09-25 17:54:57 -07001704 int rc;
Tomas Winkler600c0e12008-12-19 10:37:04 +08001705 struct iwl_powertable_cmd cmd;
Zhu Yib481de92007-09-25 17:54:57 -07001706
1707 /* If on battery, set to 3,
Ian Schram01ebd062007-10-25 17:15:22 +08001708 * if plugged into AC power, set to CAM ("continuously aware mode"),
Zhu Yib481de92007-09-25 17:54:57 -07001709 * else user level */
1710 switch (mode) {
Samuel Ortiz1125eff2008-12-19 10:37:14 +08001711 case IWL39_POWER_BATTERY:
Zhu Yib481de92007-09-25 17:54:57 -07001712 final_mode = IWL_POWER_INDEX_3;
1713 break;
Samuel Ortiz1125eff2008-12-19 10:37:14 +08001714 case IWL39_POWER_AC:
Zhu Yib481de92007-09-25 17:54:57 -07001715 final_mode = IWL_POWER_MODE_CAM;
1716 break;
1717 default:
1718 final_mode = mode;
1719 break;
1720 }
1721
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001722 iwl3945_update_power_cmd(priv, &cmd, final_mode);
Zhu Yib481de92007-09-25 17:54:57 -07001723
Tomas Winkler600c0e12008-12-19 10:37:04 +08001724 /* FIXME use get_hcmd_size 3945 command is 4 bytes shorter */
1725 rc = iwl3945_send_cmd_pdu(priv, POWER_TABLE_CMD,
1726 sizeof(struct iwl3945_powertable_cmd), &cmd);
Zhu Yib481de92007-09-25 17:54:57 -07001727
1728 if (final_mode == IWL_POWER_MODE_CAM)
1729 clear_bit(STATUS_POWER_PMI, &priv->status);
1730 else
1731 set_bit(STATUS_POWER_PMI, &priv->status);
1732
1733 return rc;
1734}
1735
Zhu Yib481de92007-09-25 17:54:57 -07001736/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001737 * iwl3945_scan_cancel - Cancel any currently executing HW scan
Zhu Yib481de92007-09-25 17:54:57 -07001738 *
1739 * NOTE: priv->mutex is not required before calling this function
1740 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001741static int iwl3945_scan_cancel(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001742{
1743 if (!test_bit(STATUS_SCAN_HW, &priv->status)) {
1744 clear_bit(STATUS_SCANNING, &priv->status);
1745 return 0;
1746 }
1747
1748 if (test_bit(STATUS_SCANNING, &priv->status)) {
1749 if (!test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
1750 IWL_DEBUG_SCAN("Queuing scan abort.\n");
1751 set_bit(STATUS_SCAN_ABORTING, &priv->status);
1752 queue_work(priv->workqueue, &priv->abort_scan);
1753
1754 } else
1755 IWL_DEBUG_SCAN("Scan abort already in progress.\n");
1756
1757 return test_bit(STATUS_SCANNING, &priv->status);
1758 }
1759
1760 return 0;
1761}
1762
1763/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001764 * iwl3945_scan_cancel_timeout - Cancel any currently executing HW scan
Zhu Yib481de92007-09-25 17:54:57 -07001765 * @ms: amount of time to wait (in milliseconds) for scan to abort
1766 *
1767 * NOTE: priv->mutex must be held before calling this function
1768 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001769static int iwl3945_scan_cancel_timeout(struct iwl_priv *priv, unsigned long ms)
Zhu Yib481de92007-09-25 17:54:57 -07001770{
1771 unsigned long now = jiffies;
1772 int ret;
1773
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001774 ret = iwl3945_scan_cancel(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001775 if (ret && ms) {
1776 mutex_unlock(&priv->mutex);
1777 while (!time_after(jiffies, now + msecs_to_jiffies(ms)) &&
1778 test_bit(STATUS_SCANNING, &priv->status))
1779 msleep(1);
1780 mutex_lock(&priv->mutex);
1781
1782 return test_bit(STATUS_SCANNING, &priv->status);
1783 }
1784
1785 return ret;
1786}
1787
Zhu Yib481de92007-09-25 17:54:57 -07001788#define MAX_UCODE_BEACON_INTERVAL 1024
1789#define INTEL_CONN_LISTEN_INTERVAL __constant_cpu_to_le16(0xA)
1790
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001791static __le16 iwl3945_adjust_beacon_interval(u16 beacon_val)
Zhu Yib481de92007-09-25 17:54:57 -07001792{
1793 u16 new_val = 0;
1794 u16 beacon_factor = 0;
1795
1796 beacon_factor =
1797 (beacon_val + MAX_UCODE_BEACON_INTERVAL)
1798 / MAX_UCODE_BEACON_INTERVAL;
1799 new_val = beacon_val / beacon_factor;
1800
1801 return cpu_to_le16(new_val);
1802}
1803
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001804static void iwl3945_setup_rxon_timing(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001805{
1806 u64 interval_tm_unit;
1807 u64 tsf, result;
1808 unsigned long flags;
1809 struct ieee80211_conf *conf = NULL;
1810 u16 beacon_int = 0;
1811
1812 conf = ieee80211_get_hw_conf(priv->hw);
1813
1814 spin_lock_irqsave(&priv->lock, flags);
Tomas Winkler28afaf92008-12-19 10:37:06 +08001815 priv->rxon_timing.timestamp = cpu_to_le64(priv->timestamp);
Zhu Yib481de92007-09-25 17:54:57 -07001816 priv->rxon_timing.listen_interval = INTEL_CONN_LISTEN_INTERVAL;
1817
Tomas Winkler28afaf92008-12-19 10:37:06 +08001818 tsf = priv->timestamp;
Zhu Yib481de92007-09-25 17:54:57 -07001819
1820 beacon_int = priv->beacon_int;
1821 spin_unlock_irqrestore(&priv->lock, flags);
1822
Johannes Berg05c914f2008-09-11 00:01:58 +02001823 if (priv->iw_mode == NL80211_IFTYPE_STATION) {
Zhu Yib481de92007-09-25 17:54:57 -07001824 if (beacon_int == 0) {
1825 priv->rxon_timing.beacon_interval = cpu_to_le16(100);
1826 priv->rxon_timing.beacon_init_val = cpu_to_le32(102400);
1827 } else {
1828 priv->rxon_timing.beacon_interval =
1829 cpu_to_le16(beacon_int);
1830 priv->rxon_timing.beacon_interval =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001831 iwl3945_adjust_beacon_interval(
Zhu Yib481de92007-09-25 17:54:57 -07001832 le16_to_cpu(priv->rxon_timing.beacon_interval));
1833 }
1834
1835 priv->rxon_timing.atim_window = 0;
1836 } else {
1837 priv->rxon_timing.beacon_interval =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001838 iwl3945_adjust_beacon_interval(conf->beacon_int);
Zhu Yib481de92007-09-25 17:54:57 -07001839 /* TODO: we need to get atim_window from upper stack
1840 * for now we set to 0 */
1841 priv->rxon_timing.atim_window = 0;
1842 }
1843
1844 interval_tm_unit =
1845 (le16_to_cpu(priv->rxon_timing.beacon_interval) * 1024);
1846 result = do_div(tsf, interval_tm_unit);
1847 priv->rxon_timing.beacon_init_val =
1848 cpu_to_le32((u32) ((u64) interval_tm_unit - result));
1849
1850 IWL_DEBUG_ASSOC
1851 ("beacon interval %d beacon timer %d beacon tim %d\n",
1852 le16_to_cpu(priv->rxon_timing.beacon_interval),
1853 le32_to_cpu(priv->rxon_timing.beacon_init_val),
1854 le16_to_cpu(priv->rxon_timing.atim_window));
1855}
1856
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001857static int iwl3945_scan_initiate(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001858{
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08001859 if (!iwl_is_ready_rf(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07001860 IWL_DEBUG_SCAN("Aborting scan due to not ready.\n");
1861 return -EIO;
1862 }
1863
1864 if (test_bit(STATUS_SCANNING, &priv->status)) {
1865 IWL_DEBUG_SCAN("Scan already in progress.\n");
1866 return -EAGAIN;
1867 }
1868
1869 if (test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
1870 IWL_DEBUG_SCAN("Scan request while abort pending. "
1871 "Queuing.\n");
1872 return -EAGAIN;
1873 }
1874
1875 IWL_DEBUG_INFO("Starting scan...\n");
Ron Rindjunsky66b50042008-06-25 16:46:31 +08001876 if (priv->cfg->sku & IWL_SKU_G)
1877 priv->scan_bands |= BIT(IEEE80211_BAND_2GHZ);
1878 if (priv->cfg->sku & IWL_SKU_A)
1879 priv->scan_bands |= BIT(IEEE80211_BAND_5GHZ);
Zhu Yib481de92007-09-25 17:54:57 -07001880 set_bit(STATUS_SCANNING, &priv->status);
1881 priv->scan_start = jiffies;
1882 priv->scan_pass_start = priv->scan_start;
1883
1884 queue_work(priv->workqueue, &priv->request_scan);
1885
1886 return 0;
1887}
1888
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001889static int iwl3945_set_rxon_hwcrypto(struct iwl_priv *priv, int hw_decrypt)
Zhu Yib481de92007-09-25 17:54:57 -07001890{
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001891 struct iwl3945_rxon_cmd *rxon = &priv->staging39_rxon;
Zhu Yib481de92007-09-25 17:54:57 -07001892
1893 if (hw_decrypt)
1894 rxon->filter_flags &= ~RXON_FILTER_DIS_DECRYPT_MSK;
1895 else
1896 rxon->filter_flags |= RXON_FILTER_DIS_DECRYPT_MSK;
1897
1898 return 0;
1899}
1900
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001901static void iwl3945_set_flags_for_phymode(struct iwl_priv *priv,
Johannes Berg8318d782008-01-24 19:38:38 +01001902 enum ieee80211_band band)
Zhu Yib481de92007-09-25 17:54:57 -07001903{
Johannes Berg8318d782008-01-24 19:38:38 +01001904 if (band == IEEE80211_BAND_5GHZ) {
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001905 priv->staging39_rxon.flags &=
Zhu Yib481de92007-09-25 17:54:57 -07001906 ~(RXON_FLG_BAND_24G_MSK | RXON_FLG_AUTO_DETECT_MSK
1907 | RXON_FLG_CCK_MSK);
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001908 priv->staging39_rxon.flags |= RXON_FLG_SHORT_SLOT_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07001909 } else {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001910 /* Copied from iwl3945_bg_post_associate() */
Zhu Yib481de92007-09-25 17:54:57 -07001911 if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_SLOT_TIME)
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001912 priv->staging39_rxon.flags |= RXON_FLG_SHORT_SLOT_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07001913 else
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001914 priv->staging39_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07001915
Johannes Berg05c914f2008-09-11 00:01:58 +02001916 if (priv->iw_mode == NL80211_IFTYPE_ADHOC)
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001917 priv->staging39_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07001918
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001919 priv->staging39_rxon.flags |= RXON_FLG_BAND_24G_MSK;
1920 priv->staging39_rxon.flags |= RXON_FLG_AUTO_DETECT_MSK;
1921 priv->staging39_rxon.flags &= ~RXON_FLG_CCK_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07001922 }
1923}
1924
1925/*
Ian Schram01ebd062007-10-25 17:15:22 +08001926 * initialize rxon structure with default values from eeprom
Zhu Yib481de92007-09-25 17:54:57 -07001927 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001928static void iwl3945_connection_init_rx_config(struct iwl_priv *priv,
Zhu, Yi60294de2008-10-29 14:05:45 -07001929 int mode)
Zhu Yib481de92007-09-25 17:54:57 -07001930{
Samuel Ortizd20b3c62008-12-19 10:37:15 +08001931 const struct iwl_channel_info *ch_info;
Zhu Yib481de92007-09-25 17:54:57 -07001932
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001933 memset(&priv->staging39_rxon, 0, sizeof(priv->staging39_rxon));
Zhu Yib481de92007-09-25 17:54:57 -07001934
Zhu, Yi60294de2008-10-29 14:05:45 -07001935 switch (mode) {
Johannes Berg05c914f2008-09-11 00:01:58 +02001936 case NL80211_IFTYPE_AP:
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001937 priv->staging39_rxon.dev_type = RXON_DEV_TYPE_AP;
Zhu Yib481de92007-09-25 17:54:57 -07001938 break;
1939
Johannes Berg05c914f2008-09-11 00:01:58 +02001940 case NL80211_IFTYPE_STATION:
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001941 priv->staging39_rxon.dev_type = RXON_DEV_TYPE_ESS;
1942 priv->staging39_rxon.filter_flags = RXON_FILTER_ACCEPT_GRP_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07001943 break;
1944
Johannes Berg05c914f2008-09-11 00:01:58 +02001945 case NL80211_IFTYPE_ADHOC:
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001946 priv->staging39_rxon.dev_type = RXON_DEV_TYPE_IBSS;
1947 priv->staging39_rxon.flags = RXON_FLG_SHORT_PREAMBLE_MSK;
1948 priv->staging39_rxon.filter_flags = RXON_FILTER_BCON_AWARE_MSK |
Zhu Yib481de92007-09-25 17:54:57 -07001949 RXON_FILTER_ACCEPT_GRP_MSK;
1950 break;
1951
Johannes Berg05c914f2008-09-11 00:01:58 +02001952 case NL80211_IFTYPE_MONITOR:
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001953 priv->staging39_rxon.dev_type = RXON_DEV_TYPE_SNIFFER;
1954 priv->staging39_rxon.filter_flags = RXON_FILTER_PROMISC_MSK |
Zhu Yib481de92007-09-25 17:54:57 -07001955 RXON_FILTER_CTL2HOST_MSK | RXON_FILTER_ACCEPT_GRP_MSK;
1956 break;
Tomas Winkler69dc5d92008-03-25 16:33:41 -07001957 default:
Winkler, Tomas15b16872008-12-19 10:37:33 +08001958 IWL_ERR(priv, "Unsupported interface type %d\n", mode);
Tomas Winkler69dc5d92008-03-25 16:33:41 -07001959 break;
Zhu Yib481de92007-09-25 17:54:57 -07001960 }
1961
1962#if 0
1963 /* TODO: Figure out when short_preamble would be set and cache from
1964 * that */
1965 if (!hw_to_local(priv->hw)->short_preamble)
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001966 priv->staging39_rxon.flags &= ~RXON_FLG_SHORT_PREAMBLE_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07001967 else
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001968 priv->staging39_rxon.flags |= RXON_FLG_SHORT_PREAMBLE_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07001969#endif
1970
Johannes Berg8318d782008-01-24 19:38:38 +01001971 ch_info = iwl3945_get_channel_info(priv, priv->band,
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001972 le16_to_cpu(priv->active39_rxon.channel));
Zhu Yib481de92007-09-25 17:54:57 -07001973
1974 if (!ch_info)
1975 ch_info = &priv->channel_info[0];
1976
1977 /*
1978 * in some case A channels are all non IBSS
1979 * in this case force B/G channel
1980 */
Zhu, Yi60294de2008-10-29 14:05:45 -07001981 if ((mode == NL80211_IFTYPE_ADHOC) && !(is_channel_ibss(ch_info)))
Zhu Yib481de92007-09-25 17:54:57 -07001982 ch_info = &priv->channel_info[0];
1983
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001984 priv->staging39_rxon.channel = cpu_to_le16(ch_info->channel);
Zhu Yib481de92007-09-25 17:54:57 -07001985 if (is_channel_a_band(ch_info))
Johannes Berg8318d782008-01-24 19:38:38 +01001986 priv->band = IEEE80211_BAND_5GHZ;
Zhu Yib481de92007-09-25 17:54:57 -07001987 else
Johannes Berg8318d782008-01-24 19:38:38 +01001988 priv->band = IEEE80211_BAND_2GHZ;
Zhu Yib481de92007-09-25 17:54:57 -07001989
Johannes Berg8318d782008-01-24 19:38:38 +01001990 iwl3945_set_flags_for_phymode(priv, priv->band);
Zhu Yib481de92007-09-25 17:54:57 -07001991
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001992 priv->staging39_rxon.ofdm_basic_rates =
Zhu Yib481de92007-09-25 17:54:57 -07001993 (IWL_OFDM_RATES_MASK >> IWL_FIRST_OFDM_RATE) & 0xFF;
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001994 priv->staging39_rxon.cck_basic_rates =
Zhu Yib481de92007-09-25 17:54:57 -07001995 (IWL_CCK_RATES_MASK >> IWL_FIRST_CCK_RATE) & 0xF;
1996}
1997
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001998static int iwl3945_set_mode(struct iwl_priv *priv, int mode)
Zhu Yib481de92007-09-25 17:54:57 -07001999{
Johannes Berg05c914f2008-09-11 00:01:58 +02002000 if (mode == NL80211_IFTYPE_ADHOC) {
Samuel Ortizd20b3c62008-12-19 10:37:15 +08002001 const struct iwl_channel_info *ch_info;
Zhu Yib481de92007-09-25 17:54:57 -07002002
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002003 ch_info = iwl3945_get_channel_info(priv,
Johannes Berg8318d782008-01-24 19:38:38 +01002004 priv->band,
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08002005 le16_to_cpu(priv->staging39_rxon.channel));
Zhu Yib481de92007-09-25 17:54:57 -07002006
2007 if (!ch_info || !is_channel_ibss(ch_info)) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08002008 IWL_ERR(priv, "channel %d not IBSS channel\n",
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08002009 le16_to_cpu(priv->staging39_rxon.channel));
Zhu Yib481de92007-09-25 17:54:57 -07002010 return -EINVAL;
2011 }
2012 }
2013
Zhu, Yi60294de2008-10-29 14:05:45 -07002014 iwl3945_connection_init_rx_config(priv, mode);
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08002015 memcpy(priv->staging39_rxon.node_addr, priv->mac_addr, ETH_ALEN);
Zhu Yib481de92007-09-25 17:54:57 -07002016
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002017 iwl3945_clear_stations_table(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002018
Tomas Winklera96a27f2008-10-23 23:48:56 -07002019 /* don't commit rxon if rf-kill is on*/
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08002020 if (!iwl_is_ready_rf(priv))
Mohamed Abbasfde35712007-11-29 11:10:15 +08002021 return -EAGAIN;
2022
2023 cancel_delayed_work(&priv->scan_check);
2024 if (iwl3945_scan_cancel_timeout(priv, 100)) {
Winkler, Tomas39aadf82008-12-19 10:37:32 +08002025 IWL_WARN(priv, "Aborted scan still in progress after 100ms\n");
Mohamed Abbasfde35712007-11-29 11:10:15 +08002026 IWL_DEBUG_MAC80211("leaving - scan abort failed.\n");
2027 return -EAGAIN;
2028 }
2029
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002030 iwl3945_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002031
2032 return 0;
2033}
2034
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002035static void iwl3945_build_tx_cmd_hwcrypto(struct iwl_priv *priv,
Johannes Berge039fa42008-05-15 12:55:29 +02002036 struct ieee80211_tx_info *info,
Winkler, Tomasc2d79b42008-12-19 10:37:34 +08002037 struct iwl_cmd *cmd,
Zhu Yib481de92007-09-25 17:54:57 -07002038 struct sk_buff *skb_frag,
2039 int last_frag)
2040{
Winkler, Tomase52119c2008-12-22 11:31:19 +08002041 struct iwl3945_tx_cmd *tx = (struct iwl3945_tx_cmd *)cmd->cmd.payload;
Ivo van Doorn1c014422008-04-17 19:41:02 +02002042 struct iwl3945_hw_key *keyinfo =
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08002043 &priv->stations_39[info->control.hw_key->hw_key_idx].keyinfo;
Zhu Yib481de92007-09-25 17:54:57 -07002044
2045 switch (keyinfo->alg) {
2046 case ALG_CCMP:
Winkler, Tomase52119c2008-12-22 11:31:19 +08002047 tx->sec_ctl = TX_CMD_SEC_CCM;
2048 memcpy(tx->key, keyinfo->key, keyinfo->keylen);
Tomas Winklera96a27f2008-10-23 23:48:56 -07002049 IWL_DEBUG_TX("tx_cmd with AES hwcrypto\n");
Zhu Yib481de92007-09-25 17:54:57 -07002050 break;
2051
2052 case ALG_TKIP:
2053#if 0
Winkler, Tomase52119c2008-12-22 11:31:19 +08002054 tx->sec_ctl = TX_CMD_SEC_TKIP;
Zhu Yib481de92007-09-25 17:54:57 -07002055
2056 if (last_frag)
Winkler, Tomase52119c2008-12-22 11:31:19 +08002057 memcpy(tx->tkip_mic.byte, skb_frag->tail - 8,
Zhu Yib481de92007-09-25 17:54:57 -07002058 8);
2059 else
Winkler, Tomase52119c2008-12-22 11:31:19 +08002060 memset(tx->tkip_mic.byte, 0, 8);
Zhu Yib481de92007-09-25 17:54:57 -07002061#endif
2062 break;
2063
2064 case ALG_WEP:
Winkler, Tomase52119c2008-12-22 11:31:19 +08002065 tx->sec_ctl = TX_CMD_SEC_WEP |
Johannes Berge039fa42008-05-15 12:55:29 +02002066 (info->control.hw_key->hw_key_idx & TX_CMD_SEC_MSK) << TX_CMD_SEC_SHIFT;
Zhu Yib481de92007-09-25 17:54:57 -07002067
2068 if (keyinfo->keylen == 13)
Winkler, Tomase52119c2008-12-22 11:31:19 +08002069 tx->sec_ctl |= TX_CMD_SEC_KEY128;
Zhu Yib481de92007-09-25 17:54:57 -07002070
Winkler, Tomase52119c2008-12-22 11:31:19 +08002071 memcpy(&tx->key[3], keyinfo->key, keyinfo->keylen);
Zhu Yib481de92007-09-25 17:54:57 -07002072
2073 IWL_DEBUG_TX("Configuring packet for WEP encryption "
Johannes Berge039fa42008-05-15 12:55:29 +02002074 "with key %d\n", info->control.hw_key->hw_key_idx);
Zhu Yib481de92007-09-25 17:54:57 -07002075 break;
2076
Zhu Yib481de92007-09-25 17:54:57 -07002077 default:
Tomas Winkler978785a2008-12-19 10:37:31 +08002078 IWL_ERR(priv, "Unknown encode alg %d\n", keyinfo->alg);
Zhu Yib481de92007-09-25 17:54:57 -07002079 break;
2080 }
2081}
2082
2083/*
2084 * handle build REPLY_TX command notification.
2085 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002086static void iwl3945_build_tx_cmd_basic(struct iwl_priv *priv,
Winkler, Tomasc2d79b42008-12-19 10:37:34 +08002087 struct iwl_cmd *cmd,
Johannes Berge039fa42008-05-15 12:55:29 +02002088 struct ieee80211_tx_info *info,
Winkler, Tomase52119c2008-12-22 11:31:19 +08002089 struct ieee80211_hdr *hdr, u8 std_id)
Zhu Yib481de92007-09-25 17:54:57 -07002090{
Winkler, Tomase52119c2008-12-22 11:31:19 +08002091 struct iwl3945_tx_cmd *tx = (struct iwl3945_tx_cmd *)cmd->cmd.payload;
2092 __le32 tx_flags = tx->tx_flags;
Harvey Harrisonfd7c8a42008-06-11 14:21:56 -07002093 __le16 fc = hdr->frame_control;
Johannes Berge6a98542008-10-21 12:40:02 +02002094 u8 rc_flags = info->control.rates[0].flags;
Zhu Yib481de92007-09-25 17:54:57 -07002095
Winkler, Tomase52119c2008-12-22 11:31:19 +08002096 tx->stop_time.life_time = TX_CMD_LIFE_TIME_INFINITE;
Johannes Berge039fa42008-05-15 12:55:29 +02002097 if (!(info->flags & IEEE80211_TX_CTL_NO_ACK)) {
Zhu Yib481de92007-09-25 17:54:57 -07002098 tx_flags |= TX_CMD_FLG_ACK_MSK;
Harvey Harrisonfd7c8a42008-06-11 14:21:56 -07002099 if (ieee80211_is_mgmt(fc))
Zhu Yib481de92007-09-25 17:54:57 -07002100 tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
Harvey Harrisonfd7c8a42008-06-11 14:21:56 -07002101 if (ieee80211_is_probe_resp(fc) &&
Zhu Yib481de92007-09-25 17:54:57 -07002102 !(le16_to_cpu(hdr->seq_ctrl) & 0xf))
2103 tx_flags |= TX_CMD_FLG_TSF_MSK;
2104 } else {
2105 tx_flags &= (~TX_CMD_FLG_ACK_MSK);
2106 tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
2107 }
2108
Winkler, Tomase52119c2008-12-22 11:31:19 +08002109 tx->sta_id = std_id;
Harvey Harrison8b7b1e02008-06-11 14:21:56 -07002110 if (ieee80211_has_morefrags(fc))
Zhu Yib481de92007-09-25 17:54:57 -07002111 tx_flags |= TX_CMD_FLG_MORE_FRAG_MSK;
2112
Harvey Harrisonfd7c8a42008-06-11 14:21:56 -07002113 if (ieee80211_is_data_qos(fc)) {
2114 u8 *qc = ieee80211_get_qos_ctl(hdr);
Winkler, Tomase52119c2008-12-22 11:31:19 +08002115 tx->tid_tspec = qc[0] & 0xf;
Zhu Yib481de92007-09-25 17:54:57 -07002116 tx_flags &= ~TX_CMD_FLG_SEQ_CTL_MSK;
Tomas Winkler54dbb522008-05-15 13:54:06 +08002117 } else {
Zhu Yib481de92007-09-25 17:54:57 -07002118 tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
Tomas Winkler54dbb522008-05-15 13:54:06 +08002119 }
Zhu Yib481de92007-09-25 17:54:57 -07002120
Johannes Berge6a98542008-10-21 12:40:02 +02002121 if (rc_flags & IEEE80211_TX_RC_USE_RTS_CTS) {
Zhu Yib481de92007-09-25 17:54:57 -07002122 tx_flags |= TX_CMD_FLG_RTS_MSK;
2123 tx_flags &= ~TX_CMD_FLG_CTS_MSK;
Johannes Berge6a98542008-10-21 12:40:02 +02002124 } else if (rc_flags & IEEE80211_TX_RC_USE_CTS_PROTECT) {
Zhu Yib481de92007-09-25 17:54:57 -07002125 tx_flags &= ~TX_CMD_FLG_RTS_MSK;
2126 tx_flags |= TX_CMD_FLG_CTS_MSK;
2127 }
2128
2129 if ((tx_flags & TX_CMD_FLG_RTS_MSK) || (tx_flags & TX_CMD_FLG_CTS_MSK))
2130 tx_flags |= TX_CMD_FLG_FULL_TXOP_PROT_MSK;
2131
2132 tx_flags &= ~(TX_CMD_FLG_ANT_SEL_MSK);
Harvey Harrisonfd7c8a42008-06-11 14:21:56 -07002133 if (ieee80211_is_mgmt(fc)) {
2134 if (ieee80211_is_assoc_req(fc) || ieee80211_is_reassoc_req(fc))
Winkler, Tomase52119c2008-12-22 11:31:19 +08002135 tx->timeout.pm_frame_timeout = cpu_to_le16(3);
Zhu Yib481de92007-09-25 17:54:57 -07002136 else
Winkler, Tomase52119c2008-12-22 11:31:19 +08002137 tx->timeout.pm_frame_timeout = cpu_to_le16(2);
Mohamed Abbasab53d8a2008-03-25 16:33:36 -07002138 } else {
Winkler, Tomase52119c2008-12-22 11:31:19 +08002139 tx->timeout.pm_frame_timeout = 0;
Mohamed Abbasab53d8a2008-03-25 16:33:36 -07002140#ifdef CONFIG_IWL3945_LEDS
2141 priv->rxtxpackets += le16_to_cpu(cmd->cmd.tx.len);
2142#endif
2143 }
Zhu Yib481de92007-09-25 17:54:57 -07002144
Winkler, Tomase52119c2008-12-22 11:31:19 +08002145 tx->driver_txop = 0;
2146 tx->tx_flags = tx_flags;
2147 tx->next_frame_len = 0;
Zhu Yib481de92007-09-25 17:54:57 -07002148}
2149
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002150/**
2151 * iwl3945_get_sta_id - Find station's index within station table
2152 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002153static int iwl3945_get_sta_id(struct iwl_priv *priv, struct ieee80211_hdr *hdr)
Zhu Yib481de92007-09-25 17:54:57 -07002154{
2155 int sta_id;
2156 u16 fc = le16_to_cpu(hdr->frame_control);
2157
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002158 /* If this frame is broadcast or management, use broadcast station id */
Zhu Yib481de92007-09-25 17:54:57 -07002159 if (((fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_DATA) ||
2160 is_multicast_ether_addr(hdr->addr1))
Abhijeet Kolekar3832ec92008-12-19 10:37:26 +08002161 return priv->hw_params.bcast_sta_id;
Zhu Yib481de92007-09-25 17:54:57 -07002162
2163 switch (priv->iw_mode) {
2164
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002165 /* If we are a client station in a BSS network, use the special
2166 * AP station entry (that's the only station we communicate with) */
Johannes Berg05c914f2008-09-11 00:01:58 +02002167 case NL80211_IFTYPE_STATION:
Zhu Yib481de92007-09-25 17:54:57 -07002168 return IWL_AP_ID;
2169
2170 /* If we are an AP, then find the station, or use BCAST */
Johannes Berg05c914f2008-09-11 00:01:58 +02002171 case NL80211_IFTYPE_AP:
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002172 sta_id = iwl3945_hw_find_station(priv, hdr->addr1);
Zhu Yib481de92007-09-25 17:54:57 -07002173 if (sta_id != IWL_INVALID_STATION)
2174 return sta_id;
Abhijeet Kolekar3832ec92008-12-19 10:37:26 +08002175 return priv->hw_params.bcast_sta_id;
Zhu Yib481de92007-09-25 17:54:57 -07002176
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002177 /* If this frame is going out to an IBSS network, find the station,
2178 * or create a new station table entry */
Johannes Berg05c914f2008-09-11 00:01:58 +02002179 case NL80211_IFTYPE_ADHOC: {
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002180 /* Create new station table entry */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002181 sta_id = iwl3945_hw_find_station(priv, hdr->addr1);
Zhu Yib481de92007-09-25 17:54:57 -07002182 if (sta_id != IWL_INVALID_STATION)
2183 return sta_id;
2184
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002185 sta_id = iwl3945_add_station(priv, hdr->addr1, 0, CMD_ASYNC);
Zhu Yib481de92007-09-25 17:54:57 -07002186
2187 if (sta_id != IWL_INVALID_STATION)
2188 return sta_id;
2189
Johannes Berge1749612008-10-27 15:59:26 -07002190 IWL_DEBUG_DROP("Station %pM not in station map. "
Zhu Yib481de92007-09-25 17:54:57 -07002191 "Defaulting to broadcast...\n",
Johannes Berge1749612008-10-27 15:59:26 -07002192 hdr->addr1);
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08002193 iwl_print_hex_dump(priv, IWL_DL_DROP, (u8 *) hdr, sizeof(*hdr));
Abhijeet Kolekar3832ec92008-12-19 10:37:26 +08002194 return priv->hw_params.bcast_sta_id;
Joe Perches0795af52007-10-03 17:59:30 -07002195 }
Stefanik Gábor914233d2008-06-30 17:23:30 +08002196 /* If we are in monitor mode, use BCAST. This is required for
2197 * packet injection. */
Johannes Berg05c914f2008-09-11 00:01:58 +02002198 case NL80211_IFTYPE_MONITOR:
Abhijeet Kolekar3832ec92008-12-19 10:37:26 +08002199 return priv->hw_params.bcast_sta_id;
Stefanik Gábor914233d2008-06-30 17:23:30 +08002200
Zhu Yib481de92007-09-25 17:54:57 -07002201 default:
Winkler, Tomas39aadf82008-12-19 10:37:32 +08002202 IWL_WARN(priv, "Unknown mode of operation: %d\n",
2203 priv->iw_mode);
Abhijeet Kolekar3832ec92008-12-19 10:37:26 +08002204 return priv->hw_params.bcast_sta_id;
Zhu Yib481de92007-09-25 17:54:57 -07002205 }
2206}
2207
2208/*
2209 * start REPLY_TX command process
2210 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002211static int iwl3945_tx_skb(struct iwl_priv *priv, struct sk_buff *skb)
Zhu Yib481de92007-09-25 17:54:57 -07002212{
2213 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
Johannes Berge039fa42008-05-15 12:55:29 +02002214 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
Winkler, Tomasdbb66542008-12-22 11:31:14 +08002215 struct iwl3945_tfd *tfd;
Winkler, Tomase52119c2008-12-22 11:31:19 +08002216 struct iwl3945_tx_cmd *tx;
Samuel Ortiz188cf6c2008-12-22 11:31:16 +08002217 struct iwl_tx_queue *txq = NULL;
Samuel Ortizd20b3c62008-12-19 10:37:15 +08002218 struct iwl_queue *q = NULL;
Winkler, Tomase52119c2008-12-22 11:31:19 +08002219 struct iwl_cmd *out_cmd = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07002220 dma_addr_t phys_addr;
2221 dma_addr_t txcmd_phys;
Winkler, Tomase52119c2008-12-22 11:31:19 +08002222 int txq_id = skb_get_queue_mapping(skb);
Tomas Winkler54dbb522008-05-15 13:54:06 +08002223 u16 len, idx, len_org, hdr_len;
2224 u8 id;
2225 u8 unicast;
Zhu Yib481de92007-09-25 17:54:57 -07002226 u8 sta_id;
Tomas Winkler54dbb522008-05-15 13:54:06 +08002227 u8 tid = 0;
Zhu Yib481de92007-09-25 17:54:57 -07002228 u16 seq_number = 0;
Harvey Harrisonfd7c8a42008-06-11 14:21:56 -07002229 __le16 fc;
Zhu Yib481de92007-09-25 17:54:57 -07002230 u8 wait_write_ptr = 0;
Tomas Winkler54dbb522008-05-15 13:54:06 +08002231 u8 *qc = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07002232 unsigned long flags;
2233 int rc;
2234
2235 spin_lock_irqsave(&priv->lock, flags);
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08002236 if (iwl_is_rfkill(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07002237 IWL_DEBUG_DROP("Dropping - RF KILL\n");
2238 goto drop_unlock;
2239 }
2240
Johannes Berge039fa42008-05-15 12:55:29 +02002241 if ((ieee80211_get_tx_rate(priv->hw, info)->hw_value & 0xFF) == IWL_INVALID_RATE) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08002242 IWL_ERR(priv, "ERROR: No TX rate available.\n");
Zhu Yib481de92007-09-25 17:54:57 -07002243 goto drop_unlock;
2244 }
2245
2246 unicast = !is_multicast_ether_addr(hdr->addr1);
2247 id = 0;
2248
Harvey Harrisonfd7c8a42008-06-11 14:21:56 -07002249 fc = hdr->frame_control;
Zhu Yib481de92007-09-25 17:54:57 -07002250
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08002251#ifdef CONFIG_IWL3945_DEBUG
Zhu Yib481de92007-09-25 17:54:57 -07002252 if (ieee80211_is_auth(fc))
2253 IWL_DEBUG_TX("Sending AUTH frame\n");
Harvey Harrisonfd7c8a42008-06-11 14:21:56 -07002254 else if (ieee80211_is_assoc_req(fc))
Zhu Yib481de92007-09-25 17:54:57 -07002255 IWL_DEBUG_TX("Sending ASSOC frame\n");
Harvey Harrisonfd7c8a42008-06-11 14:21:56 -07002256 else if (ieee80211_is_reassoc_req(fc))
Zhu Yib481de92007-09-25 17:54:57 -07002257 IWL_DEBUG_TX("Sending REASSOC frame\n");
2258#endif
2259
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08002260 /* drop all data frame if we are not associated */
Stefanik Gábor914233d2008-06-30 17:23:30 +08002261 if (ieee80211_is_data(fc) &&
Johannes Berg05c914f2008-09-11 00:01:58 +02002262 (priv->iw_mode != NL80211_IFTYPE_MONITOR) && /* packet injection */
Stefanik Gábor914233d2008-06-30 17:23:30 +08002263 (!iwl3945_is_associated(priv) ||
Johannes Berg05c914f2008-09-11 00:01:58 +02002264 ((priv->iw_mode == NL80211_IFTYPE_STATION) && !priv->assoc_id))) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002265 IWL_DEBUG_DROP("Dropping - !iwl3945_is_associated\n");
Zhu Yib481de92007-09-25 17:54:57 -07002266 goto drop_unlock;
2267 }
2268
2269 spin_unlock_irqrestore(&priv->lock, flags);
2270
Harvey Harrison7294ec92008-07-15 18:43:59 -07002271 hdr_len = ieee80211_hdrlen(fc);
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002272
2273 /* Find (or create) index into station table for destination station */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002274 sta_id = iwl3945_get_sta_id(priv, hdr);
Zhu Yib481de92007-09-25 17:54:57 -07002275 if (sta_id == IWL_INVALID_STATION) {
Johannes Berge1749612008-10-27 15:59:26 -07002276 IWL_DEBUG_DROP("Dropping - INVALID STATION: %pM\n",
2277 hdr->addr1);
Zhu Yib481de92007-09-25 17:54:57 -07002278 goto drop;
2279 }
2280
2281 IWL_DEBUG_RATE("station Id %d\n", sta_id);
2282
Harvey Harrisonfd7c8a42008-06-11 14:21:56 -07002283 if (ieee80211_is_data_qos(fc)) {
2284 qc = ieee80211_get_qos_ctl(hdr);
Harvey Harrison7294ec92008-07-15 18:43:59 -07002285 tid = qc[0] & IEEE80211_QOS_CTL_TID_MASK;
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08002286 seq_number = priv->stations_39[sta_id].tid[tid].seq_number &
Zhu Yib481de92007-09-25 17:54:57 -07002287 IEEE80211_SCTL_SEQ;
2288 hdr->seq_ctrl = cpu_to_le16(seq_number) |
2289 (hdr->seq_ctrl &
2290 __constant_cpu_to_le16(IEEE80211_SCTL_FRAG));
2291 seq_number += 0x10;
2292 }
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002293
2294 /* Descriptor for chosen Tx queue */
Samuel Ortiz188cf6c2008-12-22 11:31:16 +08002295 txq = &priv->txq[txq_id];
Zhu Yib481de92007-09-25 17:54:57 -07002296 q = &txq->q;
2297
2298 spin_lock_irqsave(&priv->lock, flags);
2299
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002300 /* Set up first empty TFD within this queue's circular TFD buffer */
Samuel Ortiz188cf6c2008-12-22 11:31:16 +08002301 tfd = &txq->tfds39[q->write_ptr];
Zhu Yib481de92007-09-25 17:54:57 -07002302 memset(tfd, 0, sizeof(*tfd));
Tomas Winklerfc4b6852007-10-25 17:15:24 +08002303 idx = get_cmd_index(q, q->write_ptr, 0);
Zhu Yib481de92007-09-25 17:54:57 -07002304
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002305 /* Set up driver data for this TFD */
Winkler, Tomasdbb66542008-12-22 11:31:14 +08002306 memset(&(txq->txb[q->write_ptr]), 0, sizeof(struct iwl_tx_info));
Tomas Winklerfc4b6852007-10-25 17:15:24 +08002307 txq->txb[q->write_ptr].skb[0] = skb;
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002308
2309 /* Init first empty entry in queue's array of Tx/cmd buffers */
Samuel Ortiz188cf6c2008-12-22 11:31:16 +08002310 out_cmd = txq->cmd[idx];
Winkler, Tomase52119c2008-12-22 11:31:19 +08002311 tx = (struct iwl3945_tx_cmd *)out_cmd->cmd.payload;
Zhu Yib481de92007-09-25 17:54:57 -07002312 memset(&out_cmd->hdr, 0, sizeof(out_cmd->hdr));
Winkler, Tomase52119c2008-12-22 11:31:19 +08002313 memset(tx, 0, sizeof(*tx));
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002314
2315 /*
2316 * Set up the Tx-command (not MAC!) header.
2317 * Store the chosen Tx queue and TFD index within the sequence field;
2318 * after Tx, uCode's Tx response will return this value so driver can
2319 * locate the frame within the tx queue and do post-tx processing.
2320 */
Zhu Yib481de92007-09-25 17:54:57 -07002321 out_cmd->hdr.cmd = REPLY_TX;
2322 out_cmd->hdr.sequence = cpu_to_le16((u16)(QUEUE_TO_SEQ(txq_id) |
Tomas Winklerfc4b6852007-10-25 17:15:24 +08002323 INDEX_TO_SEQ(q->write_ptr)));
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002324
2325 /* Copy MAC header from skb into command buffer */
Winkler, Tomase52119c2008-12-22 11:31:19 +08002326 memcpy(tx->hdr, hdr, hdr_len);
Zhu Yib481de92007-09-25 17:54:57 -07002327
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002328 /*
2329 * Use the first empty entry in this queue's command buffer array
2330 * to contain the Tx command and MAC header concatenated together
2331 * (payload data will be in another buffer).
2332 * Size of this varies, due to varying MAC header length.
2333 * If end is not dword aligned, we'll have 2 extra bytes at the end
2334 * of the MAC header (device reads on dword boundaries).
2335 * We'll tell device about this padding later.
2336 */
Abhijeet Kolekar3832ec92008-12-19 10:37:26 +08002337 len = sizeof(struct iwl3945_tx_cmd) +
Tomas Winkler4c897252008-12-19 10:37:05 +08002338 sizeof(struct iwl_cmd_header) + hdr_len;
Zhu Yib481de92007-09-25 17:54:57 -07002339
2340 len_org = len;
2341 len = (len + 3) & ~3;
2342
2343 if (len_org != len)
2344 len_org = 1;
2345 else
2346 len_org = 0;
2347
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002348 /* Physical address of this Tx command's header (not MAC header!),
2349 * within command buffer array. */
Samuel Ortiz188cf6c2008-12-22 11:31:16 +08002350 txcmd_phys = pci_map_single(priv->pci_dev,
2351 out_cmd, sizeof(struct iwl_cmd),
2352 PCI_DMA_TODEVICE);
2353 pci_unmap_addr_set(&out_cmd->meta, mapping, txcmd_phys);
2354 pci_unmap_len_set(&out_cmd->meta, len, sizeof(struct iwl_cmd));
2355 /* Add buffer containing Tx command and MAC(!) header to TFD's
2356 * first entry */
2357 txcmd_phys += offsetof(struct iwl_cmd, hdr);
Zhu Yib481de92007-09-25 17:54:57 -07002358
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002359 /* Add buffer containing Tx command and MAC(!) header to TFD's
2360 * first entry */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002361 iwl3945_hw_txq_attach_buf_to_tfd(priv, tfd, txcmd_phys, len);
Zhu Yib481de92007-09-25 17:54:57 -07002362
Johannes Bergd0f09802008-07-29 11:32:07 +02002363 if (info->control.hw_key)
Johannes Berge039fa42008-05-15 12:55:29 +02002364 iwl3945_build_tx_cmd_hwcrypto(priv, info, out_cmd, skb, 0);
Zhu Yib481de92007-09-25 17:54:57 -07002365
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002366 /* Set up TFD's 2nd entry to point directly to remainder of skb,
2367 * if any (802.11 null frames have no payload). */
Zhu Yib481de92007-09-25 17:54:57 -07002368 len = skb->len - hdr_len;
2369 if (len) {
2370 phys_addr = pci_map_single(priv->pci_dev, skb->data + hdr_len,
2371 len, PCI_DMA_TODEVICE);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002372 iwl3945_hw_txq_attach_buf_to_tfd(priv, tfd, phys_addr, len);
Zhu Yib481de92007-09-25 17:54:57 -07002373 }
2374
Zhu Yib481de92007-09-25 17:54:57 -07002375 if (!len)
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002376 /* If there is no payload, then we use only one Tx buffer */
Winkler, Tomasdbb66542008-12-22 11:31:14 +08002377 tfd->control_flags = cpu_to_le32(TFD_CTL_COUNT_SET(1));
Zhu Yib481de92007-09-25 17:54:57 -07002378 else
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002379 /* Else use 2 buffers.
2380 * Tell 3945 about any padding after MAC header */
Winkler, Tomasdbb66542008-12-22 11:31:14 +08002381 tfd->control_flags = cpu_to_le32(TFD_CTL_COUNT_SET(2) |
2382 TFD_CTL_PAD_SET(U32_PAD(len)));
Zhu Yib481de92007-09-25 17:54:57 -07002383
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002384 /* Total # bytes to be transmitted */
Zhu Yib481de92007-09-25 17:54:57 -07002385 len = (u16)skb->len;
Winkler, Tomase52119c2008-12-22 11:31:19 +08002386 tx->len = cpu_to_le16(len);
Zhu Yib481de92007-09-25 17:54:57 -07002387
2388 /* TODO need this for burst mode later on */
Winkler, Tomase52119c2008-12-22 11:31:19 +08002389 iwl3945_build_tx_cmd_basic(priv, out_cmd, info, hdr, sta_id);
Zhu Yib481de92007-09-25 17:54:57 -07002390
2391 /* set is_hcca to 0; it probably will never be implemented */
Johannes Berge039fa42008-05-15 12:55:29 +02002392 iwl3945_hw_build_tx_cmd_rate(priv, out_cmd, info, hdr, sta_id, 0);
Zhu Yib481de92007-09-25 17:54:57 -07002393
Winkler, Tomase52119c2008-12-22 11:31:19 +08002394 tx->tx_flags &= ~TX_CMD_FLG_ANT_A_MSK;
2395 tx->tx_flags &= ~TX_CMD_FLG_ANT_B_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07002396
Harvey Harrison8b7b1e02008-06-11 14:21:56 -07002397 if (!ieee80211_has_morefrags(hdr->frame_control)) {
Zhu Yib481de92007-09-25 17:54:57 -07002398 txq->need_update = 1;
Tomas Winkler3ac7f142008-07-21 02:40:14 +03002399 if (qc)
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08002400 priv->stations_39[sta_id].tid[tid].seq_number = seq_number;
Zhu Yib481de92007-09-25 17:54:57 -07002401 } else {
2402 wait_write_ptr = 1;
2403 txq->need_update = 0;
2404 }
2405
Winkler, Tomase52119c2008-12-22 11:31:19 +08002406 iwl_print_hex_dump(priv, IWL_DL_TX, tx, sizeof(*tx));
Zhu Yib481de92007-09-25 17:54:57 -07002407
Winkler, Tomase52119c2008-12-22 11:31:19 +08002408 iwl_print_hex_dump(priv, IWL_DL_TX, (u8 *)tx->hdr,
Harvey Harrison7294ec92008-07-15 18:43:59 -07002409 ieee80211_hdrlen(fc));
Zhu Yib481de92007-09-25 17:54:57 -07002410
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002411 /* Tell device the write index *just past* this latest filled TFD */
Tomas Winklerc54b6792008-03-06 17:36:53 -08002412 q->write_ptr = iwl_queue_inc_wrap(q->write_ptr, q->n_bd);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002413 rc = iwl3945_tx_queue_update_write_ptr(priv, txq);
Zhu Yib481de92007-09-25 17:54:57 -07002414 spin_unlock_irqrestore(&priv->lock, flags);
2415
2416 if (rc)
2417 return rc;
2418
Samuel Ortizd20b3c62008-12-19 10:37:15 +08002419 if ((iwl_queue_space(q) < q->high_mark)
Zhu Yib481de92007-09-25 17:54:57 -07002420 && priv->mac80211_registered) {
2421 if (wait_write_ptr) {
2422 spin_lock_irqsave(&priv->lock, flags);
2423 txq->need_update = 1;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002424 iwl3945_tx_queue_update_write_ptr(priv, txq);
Zhu Yib481de92007-09-25 17:54:57 -07002425 spin_unlock_irqrestore(&priv->lock, flags);
2426 }
2427
Johannes Berge2530082008-05-17 00:57:14 +02002428 ieee80211_stop_queue(priv->hw, skb_get_queue_mapping(skb));
Zhu Yib481de92007-09-25 17:54:57 -07002429 }
2430
2431 return 0;
2432
2433drop_unlock:
2434 spin_unlock_irqrestore(&priv->lock, flags);
2435drop:
2436 return -1;
2437}
2438
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002439static void iwl3945_set_rate(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002440{
Johannes Berg8318d782008-01-24 19:38:38 +01002441 const struct ieee80211_supported_band *sband = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07002442 struct ieee80211_rate *rate;
2443 int i;
2444
Kolekar, Abhijeetcbba18c2008-12-19 10:37:42 +08002445 sband = iwl_get_hw_mode(priv, priv->band);
Johannes Berg8318d782008-01-24 19:38:38 +01002446 if (!sband) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08002447 IWL_ERR(priv, "Failed to set rate: unable to get hw mode\n");
Saleem Abdulrasoolc4ba9622007-11-18 23:59:08 -08002448 return;
2449 }
Zhu Yib481de92007-09-25 17:54:57 -07002450
2451 priv->active_rate = 0;
2452 priv->active_rate_basic = 0;
2453
Johannes Berg8318d782008-01-24 19:38:38 +01002454 IWL_DEBUG_RATE("Setting rates for %s GHz\n",
2455 sband->band == IEEE80211_BAND_2GHZ ? "2.4" : "5");
Zhu Yib481de92007-09-25 17:54:57 -07002456
Johannes Berg8318d782008-01-24 19:38:38 +01002457 for (i = 0; i < sband->n_bitrates; i++) {
2458 rate = &sband->bitrates[i];
2459 if ((rate->hw_value < IWL_RATE_COUNT) &&
2460 !(rate->flags & IEEE80211_CHAN_DISABLED)) {
2461 IWL_DEBUG_RATE("Adding rate index %d (plcp %d)\n",
2462 rate->hw_value, iwl3945_rates[rate->hw_value].plcp);
2463 priv->active_rate |= (1 << rate->hw_value);
2464 }
Zhu Yib481de92007-09-25 17:54:57 -07002465 }
2466
2467 IWL_DEBUG_RATE("Set active_rate = %0x, active_rate_basic = %0x\n",
2468 priv->active_rate, priv->active_rate_basic);
2469
2470 /*
2471 * If a basic rate is configured, then use it (adding IWL_RATE_1M_MASK)
2472 * otherwise set it to the default of all CCK rates and 6, 12, 24 for
2473 * OFDM
2474 */
2475 if (priv->active_rate_basic & IWL_CCK_BASIC_RATES_MASK)
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08002476 priv->staging39_rxon.cck_basic_rates =
Zhu Yib481de92007-09-25 17:54:57 -07002477 ((priv->active_rate_basic &
2478 IWL_CCK_RATES_MASK) >> IWL_FIRST_CCK_RATE) & 0xF;
2479 else
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08002480 priv->staging39_rxon.cck_basic_rates =
Zhu Yib481de92007-09-25 17:54:57 -07002481 (IWL_CCK_BASIC_RATES_MASK >> IWL_FIRST_CCK_RATE) & 0xF;
2482
2483 if (priv->active_rate_basic & IWL_OFDM_BASIC_RATES_MASK)
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08002484 priv->staging39_rxon.ofdm_basic_rates =
Zhu Yib481de92007-09-25 17:54:57 -07002485 ((priv->active_rate_basic &
2486 (IWL_OFDM_BASIC_RATES_MASK | IWL_RATE_6M_MASK)) >>
2487 IWL_FIRST_OFDM_RATE) & 0xFF;
2488 else
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08002489 priv->staging39_rxon.ofdm_basic_rates =
Zhu Yib481de92007-09-25 17:54:57 -07002490 (IWL_OFDM_BASIC_RATES_MASK >> IWL_FIRST_OFDM_RATE) & 0xFF;
2491}
2492
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002493static void iwl3945_radio_kill_sw(struct iwl_priv *priv, int disable_radio)
Zhu Yib481de92007-09-25 17:54:57 -07002494{
2495 unsigned long flags;
2496
2497 if (!!disable_radio == test_bit(STATUS_RF_KILL_SW, &priv->status))
2498 return;
2499
2500 IWL_DEBUG_RF_KILL("Manual SW RF KILL set to: RADIO %s\n",
2501 disable_radio ? "OFF" : "ON");
2502
2503 if (disable_radio) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002504 iwl3945_scan_cancel(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002505 /* FIXME: This is a workaround for AP */
Johannes Berg05c914f2008-09-11 00:01:58 +02002506 if (priv->iw_mode != NL80211_IFTYPE_AP) {
Zhu Yib481de92007-09-25 17:54:57 -07002507 spin_lock_irqsave(&priv->lock, flags);
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08002508 iwl_write32(priv, CSR_UCODE_DRV_GP1_SET,
Zhu Yib481de92007-09-25 17:54:57 -07002509 CSR_UCODE_SW_BIT_RFKILL);
2510 spin_unlock_irqrestore(&priv->lock, flags);
Samuel Ortizc4962942008-12-22 11:31:18 +08002511 iwl_send_card_state(priv, CARD_STATE_CMD_DISABLE, 0);
Zhu Yib481de92007-09-25 17:54:57 -07002512 set_bit(STATUS_RF_KILL_SW, &priv->status);
2513 }
2514 return;
2515 }
2516
2517 spin_lock_irqsave(&priv->lock, flags);
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08002518 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
Zhu Yib481de92007-09-25 17:54:57 -07002519
2520 clear_bit(STATUS_RF_KILL_SW, &priv->status);
2521 spin_unlock_irqrestore(&priv->lock, flags);
2522
2523 /* wake up ucode */
2524 msleep(10);
2525
2526 spin_lock_irqsave(&priv->lock, flags);
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08002527 iwl_read32(priv, CSR_UCODE_DRV_GP1);
2528 if (!iwl_grab_nic_access(priv))
2529 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002530 spin_unlock_irqrestore(&priv->lock, flags);
2531
2532 if (test_bit(STATUS_RF_KILL_HW, &priv->status)) {
2533 IWL_DEBUG_RF_KILL("Can not turn radio back on - "
2534 "disabled by HW switch\n");
2535 return;
2536 }
2537
Zhu Yi808e72a2008-06-12 09:47:17 +08002538 if (priv->is_open)
2539 queue_work(priv->workqueue, &priv->restart);
Zhu Yib481de92007-09-25 17:54:57 -07002540 return;
2541}
2542
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002543void iwl3945_set_decrypted_flag(struct iwl_priv *priv, struct sk_buff *skb,
Zhu Yib481de92007-09-25 17:54:57 -07002544 u32 decrypt_res, struct ieee80211_rx_status *stats)
2545{
2546 u16 fc =
2547 le16_to_cpu(((struct ieee80211_hdr *)skb->data)->frame_control);
2548
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08002549 if (priv->active39_rxon.filter_flags & RXON_FILTER_DIS_DECRYPT_MSK)
Zhu Yib481de92007-09-25 17:54:57 -07002550 return;
2551
2552 if (!(fc & IEEE80211_FCTL_PROTECTED))
2553 return;
2554
2555 IWL_DEBUG_RX("decrypt_res:0x%x\n", decrypt_res);
2556 switch (decrypt_res & RX_RES_STATUS_SEC_TYPE_MSK) {
2557 case RX_RES_STATUS_SEC_TYPE_TKIP:
2558 if ((decrypt_res & RX_RES_STATUS_DECRYPT_TYPE_MSK) ==
2559 RX_RES_STATUS_BAD_ICV_MIC)
2560 stats->flag |= RX_FLAG_MMIC_ERROR;
2561 case RX_RES_STATUS_SEC_TYPE_WEP:
2562 case RX_RES_STATUS_SEC_TYPE_CCMP:
2563 if ((decrypt_res & RX_RES_STATUS_DECRYPT_TYPE_MSK) ==
2564 RX_RES_STATUS_DECRYPT_OK) {
2565 IWL_DEBUG_RX("hw decrypt successfully!!!\n");
2566 stats->flag |= RX_FLAG_DECRYPTED;
2567 }
2568 break;
2569
2570 default:
2571 break;
2572 }
2573}
2574
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08002575#ifdef CONFIG_IWL3945_SPECTRUM_MEASUREMENT
Zhu Yib481de92007-09-25 17:54:57 -07002576
2577#include "iwl-spectrum.h"
2578
2579#define BEACON_TIME_MASK_LOW 0x00FFFFFF
2580#define BEACON_TIME_MASK_HIGH 0xFF000000
2581#define TIME_UNIT 1024
2582
2583/*
2584 * extended beacon time format
2585 * time in usec will be changed into a 32-bit value in 8:24 format
2586 * the high 1 byte is the beacon counts
2587 * the lower 3 bytes is the time in usec within one beacon interval
2588 */
2589
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002590static u32 iwl3945_usecs_to_beacons(u32 usec, u32 beacon_interval)
Zhu Yib481de92007-09-25 17:54:57 -07002591{
2592 u32 quot;
2593 u32 rem;
2594 u32 interval = beacon_interval * 1024;
2595
2596 if (!interval || !usec)
2597 return 0;
2598
2599 quot = (usec / interval) & (BEACON_TIME_MASK_HIGH >> 24);
2600 rem = (usec % interval) & BEACON_TIME_MASK_LOW;
2601
2602 return (quot << 24) + rem;
2603}
2604
2605/* base is usually what we get from ucode with each received frame,
2606 * the same as HW timer counter counting down
2607 */
2608
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002609static __le32 iwl3945_add_beacon_time(u32 base, u32 addon, u32 beacon_interval)
Zhu Yib481de92007-09-25 17:54:57 -07002610{
2611 u32 base_low = base & BEACON_TIME_MASK_LOW;
2612 u32 addon_low = addon & BEACON_TIME_MASK_LOW;
2613 u32 interval = beacon_interval * TIME_UNIT;
2614 u32 res = (base & BEACON_TIME_MASK_HIGH) +
2615 (addon & BEACON_TIME_MASK_HIGH);
2616
2617 if (base_low > addon_low)
2618 res += base_low - addon_low;
2619 else if (base_low < addon_low) {
2620 res += interval + base_low - addon_low;
2621 res += (1 << 24);
2622 } else
2623 res += (1 << 24);
2624
2625 return cpu_to_le32(res);
2626}
2627
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002628static int iwl3945_get_measurement(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07002629 struct ieee80211_measurement_params *params,
2630 u8 type)
2631{
Tomas Winkler600c0e12008-12-19 10:37:04 +08002632 struct iwl_spectrum_cmd spectrum;
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08002633 struct iwl_rx_packet *res;
Winkler, Tomasc2d79b42008-12-19 10:37:34 +08002634 struct iwl_host_cmd cmd = {
Zhu Yib481de92007-09-25 17:54:57 -07002635 .id = REPLY_SPECTRUM_MEASUREMENT_CMD,
2636 .data = (void *)&spectrum,
2637 .meta.flags = CMD_WANT_SKB,
2638 };
2639 u32 add_time = le64_to_cpu(params->start_time);
2640 int rc;
2641 int spectrum_resp_status;
2642 int duration = le16_to_cpu(params->duration);
2643
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002644 if (iwl3945_is_associated(priv))
Zhu Yib481de92007-09-25 17:54:57 -07002645 add_time =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002646 iwl3945_usecs_to_beacons(
Zhu Yib481de92007-09-25 17:54:57 -07002647 le64_to_cpu(params->start_time) - priv->last_tsf,
2648 le16_to_cpu(priv->rxon_timing.beacon_interval));
2649
2650 memset(&spectrum, 0, sizeof(spectrum));
2651
2652 spectrum.channel_count = cpu_to_le16(1);
2653 spectrum.flags =
2654 RXON_FLG_TSF2HOST_MSK | RXON_FLG_ANT_A_MSK | RXON_FLG_DIS_DIV_MSK;
2655 spectrum.filter_flags = MEASUREMENT_FILTER_FLAG;
2656 cmd.len = sizeof(spectrum);
2657 spectrum.len = cpu_to_le16(cmd.len - sizeof(spectrum.len));
2658
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002659 if (iwl3945_is_associated(priv))
Zhu Yib481de92007-09-25 17:54:57 -07002660 spectrum.start_time =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002661 iwl3945_add_beacon_time(priv->last_beacon_time,
Zhu Yib481de92007-09-25 17:54:57 -07002662 add_time,
2663 le16_to_cpu(priv->rxon_timing.beacon_interval));
2664 else
2665 spectrum.start_time = 0;
2666
2667 spectrum.channels[0].duration = cpu_to_le32(duration * TIME_UNIT);
2668 spectrum.channels[0].channel = params->channel;
2669 spectrum.channels[0].type = type;
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08002670 if (priv->active39_rxon.flags & RXON_FLG_BAND_24G_MSK)
Zhu Yib481de92007-09-25 17:54:57 -07002671 spectrum.flags |= RXON_FLG_BAND_24G_MSK |
2672 RXON_FLG_AUTO_DETECT_MSK | RXON_FLG_TGG_PROTECT_MSK;
2673
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002674 rc = iwl3945_send_cmd_sync(priv, &cmd);
Zhu Yib481de92007-09-25 17:54:57 -07002675 if (rc)
2676 return rc;
2677
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08002678 res = (struct iwl_rx_packet *)cmd.meta.u.skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07002679 if (res->hdr.flags & IWL_CMD_FAILED_MSK) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08002680 IWL_ERR(priv, "Bad return from REPLY_RX_ON_ASSOC command\n");
Zhu Yib481de92007-09-25 17:54:57 -07002681 rc = -EIO;
2682 }
2683
2684 spectrum_resp_status = le16_to_cpu(res->u.spectrum.status);
2685 switch (spectrum_resp_status) {
2686 case 0: /* Command will be handled */
2687 if (res->u.spectrum.id != 0xff) {
Ian Schrambc434dd2007-10-25 17:15:29 +08002688 IWL_DEBUG_INFO("Replaced existing measurement: %d\n",
2689 res->u.spectrum.id);
Zhu Yib481de92007-09-25 17:54:57 -07002690 priv->measurement_status &= ~MEASUREMENT_READY;
2691 }
2692 priv->measurement_status |= MEASUREMENT_ACTIVE;
2693 rc = 0;
2694 break;
2695
2696 case 1: /* Command will not be handled */
2697 rc = -EAGAIN;
2698 break;
2699 }
2700
2701 dev_kfree_skb_any(cmd.meta.u.skb);
2702
2703 return rc;
2704}
2705#endif
2706
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002707static void iwl3945_rx_reply_alive(struct iwl_priv *priv,
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08002708 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07002709{
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08002710 struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
2711 struct iwl_alive_resp *palive;
Zhu Yib481de92007-09-25 17:54:57 -07002712 struct delayed_work *pwork;
2713
2714 palive = &pkt->u.alive_frame;
2715
2716 IWL_DEBUG_INFO("Alive ucode status 0x%08X revision "
2717 "0x%01X 0x%01X\n",
2718 palive->is_valid, palive->ver_type,
2719 palive->ver_subtype);
2720
2721 if (palive->ver_subtype == INITIALIZE_SUBTYPE) {
2722 IWL_DEBUG_INFO("Initialization Alive received.\n");
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08002723 memcpy(&priv->card_alive_init, &pkt->u.alive_frame,
2724 sizeof(struct iwl_alive_resp));
Zhu Yib481de92007-09-25 17:54:57 -07002725 pwork = &priv->init_alive_start;
2726 } else {
2727 IWL_DEBUG_INFO("Runtime Alive received.\n");
2728 memcpy(&priv->card_alive, &pkt->u.alive_frame,
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08002729 sizeof(struct iwl_alive_resp));
Zhu Yib481de92007-09-25 17:54:57 -07002730 pwork = &priv->alive_start;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002731 iwl3945_disable_events(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002732 }
2733
2734 /* We delay the ALIVE response by 5ms to
2735 * give the HW RF Kill time to activate... */
2736 if (palive->is_valid == UCODE_VALID_OK)
2737 queue_delayed_work(priv->workqueue, pwork,
2738 msecs_to_jiffies(5));
2739 else
Winkler, Tomas39aadf82008-12-19 10:37:32 +08002740 IWL_WARN(priv, "uCode did not respond OK.\n");
Zhu Yib481de92007-09-25 17:54:57 -07002741}
2742
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002743static void iwl3945_rx_reply_add_sta(struct iwl_priv *priv,
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08002744 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07002745{
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08002746 struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07002747
2748 IWL_DEBUG_RX("Received REPLY_ADD_STA: 0x%02X\n", pkt->u.status);
2749 return;
2750}
2751
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002752static void iwl3945_rx_reply_error(struct iwl_priv *priv,
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08002753 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07002754{
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08002755 struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07002756
Winkler, Tomas15b16872008-12-19 10:37:33 +08002757 IWL_ERR(priv, "Error Reply type 0x%08X cmd %s (0x%02X) "
Zhu Yib481de92007-09-25 17:54:57 -07002758 "seq 0x%04X ser 0x%08X\n",
2759 le32_to_cpu(pkt->u.err_resp.error_type),
2760 get_cmd_string(pkt->u.err_resp.cmd_id),
2761 pkt->u.err_resp.cmd_id,
2762 le16_to_cpu(pkt->u.err_resp.bad_cmd_seq_num),
2763 le32_to_cpu(pkt->u.err_resp.error_info));
2764}
2765
2766#define TX_STATUS_ENTRY(x) case TX_STATUS_FAIL_ ## x: return #x
2767
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002768static void iwl3945_rx_csa(struct iwl_priv *priv, struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07002769{
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08002770 struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08002771 struct iwl3945_rxon_cmd *rxon = (void *)&priv->active39_rxon;
Tomas Winkler600c0e12008-12-19 10:37:04 +08002772 struct iwl_csa_notification *csa = &(pkt->u.csa_notif);
Zhu Yib481de92007-09-25 17:54:57 -07002773 IWL_DEBUG_11H("CSA notif: channel %d, status %d\n",
2774 le16_to_cpu(csa->channel), le32_to_cpu(csa->status));
2775 rxon->channel = csa->channel;
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08002776 priv->staging39_rxon.channel = csa->channel;
Zhu Yib481de92007-09-25 17:54:57 -07002777}
2778
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002779static void iwl3945_rx_spectrum_measure_notif(struct iwl_priv *priv,
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08002780 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07002781{
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08002782#ifdef CONFIG_IWL3945_SPECTRUM_MEASUREMENT
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08002783 struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
Tomas Winkler600c0e12008-12-19 10:37:04 +08002784 struct iwl_spectrum_notification *report = &(pkt->u.spectrum_notif);
Zhu Yib481de92007-09-25 17:54:57 -07002785
2786 if (!report->state) {
2787 IWL_DEBUG(IWL_DL_11H | IWL_DL_INFO,
2788 "Spectrum Measure Notification: Start\n");
2789 return;
2790 }
2791
2792 memcpy(&priv->measure_report, report, sizeof(*report));
2793 priv->measurement_status |= MEASUREMENT_READY;
2794#endif
2795}
2796
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002797static void iwl3945_rx_pm_sleep_notif(struct iwl_priv *priv,
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08002798 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07002799{
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08002800#ifdef CONFIG_IWL3945_DEBUG
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08002801 struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
Tomas Winkler600c0e12008-12-19 10:37:04 +08002802 struct iwl_sleep_notification *sleep = &(pkt->u.sleep_notif);
Zhu Yib481de92007-09-25 17:54:57 -07002803 IWL_DEBUG_RX("sleep mode: %d, src: %d\n",
2804 sleep->pm_sleep_mode, sleep->pm_wakeup_src);
2805#endif
2806}
2807
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002808static void iwl3945_rx_pm_debug_statistics_notif(struct iwl_priv *priv,
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08002809 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07002810{
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08002811 struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07002812 IWL_DEBUG_RADIO("Dumping %d bytes of unhandled "
2813 "notification for %s:\n",
2814 le32_to_cpu(pkt->len), get_cmd_string(pkt->hdr.cmd));
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08002815 iwl_print_hex_dump(priv, IWL_DL_RADIO, pkt->u.raw,
2816 le32_to_cpu(pkt->len));
Zhu Yib481de92007-09-25 17:54:57 -07002817}
2818
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002819static void iwl3945_bg_beacon_update(struct work_struct *work)
Zhu Yib481de92007-09-25 17:54:57 -07002820{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002821 struct iwl_priv *priv =
2822 container_of(work, struct iwl_priv, beacon_update);
Zhu Yib481de92007-09-25 17:54:57 -07002823 struct sk_buff *beacon;
2824
2825 /* Pull updated AP beacon from mac80211. will fail if not in AP mode */
Johannes Berge039fa42008-05-15 12:55:29 +02002826 beacon = ieee80211_beacon_get(priv->hw, priv->vif);
Zhu Yib481de92007-09-25 17:54:57 -07002827
2828 if (!beacon) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08002829 IWL_ERR(priv, "update beacon failed\n");
Zhu Yib481de92007-09-25 17:54:57 -07002830 return;
2831 }
2832
2833 mutex_lock(&priv->mutex);
2834 /* new beacon skb is allocated every time; dispose previous.*/
2835 if (priv->ibss_beacon)
2836 dev_kfree_skb(priv->ibss_beacon);
2837
2838 priv->ibss_beacon = beacon;
2839 mutex_unlock(&priv->mutex);
2840
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002841 iwl3945_send_beacon_cmd(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002842}
2843
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002844static void iwl3945_rx_beacon_notif(struct iwl_priv *priv,
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08002845 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07002846{
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08002847#ifdef CONFIG_IWL3945_DEBUG
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08002848 struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002849 struct iwl3945_beacon_notif *beacon = &(pkt->u.beacon_status);
Zhu Yib481de92007-09-25 17:54:57 -07002850 u8 rate = beacon->beacon_notify_hdr.rate;
2851
2852 IWL_DEBUG_RX("beacon status %x retries %d iss %d "
2853 "tsf %d %d rate %d\n",
2854 le32_to_cpu(beacon->beacon_notify_hdr.status) & TX_STATUS_MSK,
2855 beacon->beacon_notify_hdr.failure_frame,
2856 le32_to_cpu(beacon->ibss_mgr_status),
2857 le32_to_cpu(beacon->high_tsf),
2858 le32_to_cpu(beacon->low_tsf), rate);
2859#endif
2860
Johannes Berg05c914f2008-09-11 00:01:58 +02002861 if ((priv->iw_mode == NL80211_IFTYPE_AP) &&
Zhu Yib481de92007-09-25 17:54:57 -07002862 (!test_bit(STATUS_EXIT_PENDING, &priv->status)))
2863 queue_work(priv->workqueue, &priv->beacon_update);
2864}
2865
2866/* Service response to REPLY_SCAN_CMD (0x80) */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002867static void iwl3945_rx_reply_scan(struct iwl_priv *priv,
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08002868 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07002869{
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08002870#ifdef CONFIG_IWL3945_DEBUG
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08002871 struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
Tomas Winkler4c897252008-12-19 10:37:05 +08002872 struct iwl_scanreq_notification *notif =
2873 (struct iwl_scanreq_notification *)pkt->u.raw;
Zhu Yib481de92007-09-25 17:54:57 -07002874
2875 IWL_DEBUG_RX("Scan request status = 0x%x\n", notif->status);
2876#endif
2877}
2878
2879/* Service SCAN_START_NOTIFICATION (0x82) */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002880static void iwl3945_rx_scan_start_notif(struct iwl_priv *priv,
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08002881 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07002882{
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08002883 struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
Tomas Winkler4c897252008-12-19 10:37:05 +08002884 struct iwl_scanstart_notification *notif =
2885 (struct iwl_scanstart_notification *)pkt->u.raw;
Zhu Yib481de92007-09-25 17:54:57 -07002886 priv->scan_start_tsf = le32_to_cpu(notif->tsf_low);
2887 IWL_DEBUG_SCAN("Scan start: "
2888 "%d [802.11%s] "
2889 "(TSF: 0x%08X:%08X) - %d (beacon timer %u)\n",
2890 notif->channel,
2891 notif->band ? "bg" : "a",
2892 notif->tsf_high,
2893 notif->tsf_low, notif->status, notif->beacon_timer);
2894}
2895
2896/* Service SCAN_RESULTS_NOTIFICATION (0x83) */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002897static void iwl3945_rx_scan_results_notif(struct iwl_priv *priv,
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08002898 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07002899{
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08002900 struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
Tomas Winkler4c897252008-12-19 10:37:05 +08002901 struct iwl_scanresults_notification *notif =
2902 (struct iwl_scanresults_notification *)pkt->u.raw;
Zhu Yib481de92007-09-25 17:54:57 -07002903
2904 IWL_DEBUG_SCAN("Scan ch.res: "
2905 "%d [802.11%s] "
2906 "(TSF: 0x%08X:%08X) - %d "
2907 "elapsed=%lu usec (%dms since last)\n",
2908 notif->channel,
2909 notif->band ? "bg" : "a",
2910 le32_to_cpu(notif->tsf_high),
2911 le32_to_cpu(notif->tsf_low),
2912 le32_to_cpu(notif->statistics[0]),
2913 le32_to_cpu(notif->tsf_low) - priv->scan_start_tsf,
2914 jiffies_to_msecs(elapsed_jiffies
2915 (priv->last_scan_jiffies, jiffies)));
2916
2917 priv->last_scan_jiffies = jiffies;
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08002918 priv->next_scan_jiffies = 0;
Zhu Yib481de92007-09-25 17:54:57 -07002919}
2920
2921/* Service SCAN_COMPLETE_NOTIFICATION (0x84) */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002922static void iwl3945_rx_scan_complete_notif(struct iwl_priv *priv,
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08002923 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07002924{
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08002925 struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
Tomas Winkler4c897252008-12-19 10:37:05 +08002926 struct iwl_scancomplete_notification *scan_notif = (void *)pkt->u.raw;
Zhu Yib481de92007-09-25 17:54:57 -07002927
2928 IWL_DEBUG_SCAN("Scan complete: %d channels (TSF 0x%08X:%08X) - %d\n",
2929 scan_notif->scanned_channels,
2930 scan_notif->tsf_low,
2931 scan_notif->tsf_high, scan_notif->status);
2932
2933 /* The HW is no longer scanning */
2934 clear_bit(STATUS_SCAN_HW, &priv->status);
2935
2936 /* The scan completion notification came in, so kill that timer... */
2937 cancel_delayed_work(&priv->scan_check);
2938
2939 IWL_DEBUG_INFO("Scan pass on %sGHz took %dms\n",
Ron Rindjunsky66b50042008-06-25 16:46:31 +08002940 (priv->scan_bands & BIT(IEEE80211_BAND_2GHZ)) ?
2941 "2.4" : "5.2",
Zhu Yib481de92007-09-25 17:54:57 -07002942 jiffies_to_msecs(elapsed_jiffies
2943 (priv->scan_pass_start, jiffies)));
2944
Ron Rindjunsky66b50042008-06-25 16:46:31 +08002945 /* Remove this scanned band from the list of pending
2946 * bands to scan, band G precedes A in order of scanning
2947 * as seen in iwl3945_bg_request_scan */
2948 if (priv->scan_bands & BIT(IEEE80211_BAND_2GHZ))
2949 priv->scan_bands &= ~BIT(IEEE80211_BAND_2GHZ);
2950 else if (priv->scan_bands & BIT(IEEE80211_BAND_5GHZ))
2951 priv->scan_bands &= ~BIT(IEEE80211_BAND_5GHZ);
Zhu Yib481de92007-09-25 17:54:57 -07002952
2953 /* If a request to abort was given, or the scan did not succeed
2954 * then we reset the scan state machine and terminate,
2955 * re-queuing another scan if one has been requested */
2956 if (test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
2957 IWL_DEBUG_INFO("Aborted scan completed.\n");
2958 clear_bit(STATUS_SCAN_ABORTING, &priv->status);
2959 } else {
2960 /* If there are more bands on this scan pass reschedule */
2961 if (priv->scan_bands > 0)
2962 goto reschedule;
2963 }
2964
2965 priv->last_scan_jiffies = jiffies;
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08002966 priv->next_scan_jiffies = 0;
Zhu Yib481de92007-09-25 17:54:57 -07002967 IWL_DEBUG_INFO("Setting scan to off\n");
2968
2969 clear_bit(STATUS_SCANNING, &priv->status);
2970
2971 IWL_DEBUG_INFO("Scan took %dms\n",
2972 jiffies_to_msecs(elapsed_jiffies(priv->scan_start, jiffies)));
2973
2974 queue_work(priv->workqueue, &priv->scan_completed);
2975
2976 return;
2977
2978reschedule:
2979 priv->scan_pass_start = jiffies;
2980 queue_work(priv->workqueue, &priv->request_scan);
2981}
2982
2983/* Handle notification from uCode that card's power state is changing
2984 * due to software, hardware, or critical temperature RFKILL */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002985static void iwl3945_rx_card_state_notif(struct iwl_priv *priv,
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08002986 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07002987{
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08002988 struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07002989 u32 flags = le32_to_cpu(pkt->u.card_state_notif.flags);
2990 unsigned long status = priv->status;
2991
2992 IWL_DEBUG_RF_KILL("Card state received: HW:%s SW:%s\n",
2993 (flags & HW_CARD_DISABLED) ? "Kill" : "On",
2994 (flags & SW_CARD_DISABLED) ? "Kill" : "On");
2995
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08002996 iwl_write32(priv, CSR_UCODE_DRV_GP1_SET,
Zhu Yib481de92007-09-25 17:54:57 -07002997 CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
2998
2999 if (flags & HW_CARD_DISABLED)
3000 set_bit(STATUS_RF_KILL_HW, &priv->status);
3001 else
3002 clear_bit(STATUS_RF_KILL_HW, &priv->status);
3003
3004
3005 if (flags & SW_CARD_DISABLED)
3006 set_bit(STATUS_RF_KILL_SW, &priv->status);
3007 else
3008 clear_bit(STATUS_RF_KILL_SW, &priv->status);
3009
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003010 iwl3945_scan_cancel(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003011
3012 if ((test_bit(STATUS_RF_KILL_HW, &status) !=
3013 test_bit(STATUS_RF_KILL_HW, &priv->status)) ||
3014 (test_bit(STATUS_RF_KILL_SW, &status) !=
3015 test_bit(STATUS_RF_KILL_SW, &priv->status)))
3016 queue_work(priv->workqueue, &priv->rf_kill);
3017 else
3018 wake_up_interruptible(&priv->wait_command_queue);
3019}
3020
3021/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003022 * iwl3945_setup_rx_handlers - Initialize Rx handler callbacks
Zhu Yib481de92007-09-25 17:54:57 -07003023 *
3024 * Setup the RX handlers for each of the reply types sent from the uCode
3025 * to the host.
3026 *
3027 * This function chains into the hardware specific files for them to setup
3028 * any hardware specific handlers as well.
3029 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003030static void iwl3945_setup_rx_handlers(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003031{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003032 priv->rx_handlers[REPLY_ALIVE] = iwl3945_rx_reply_alive;
3033 priv->rx_handlers[REPLY_ADD_STA] = iwl3945_rx_reply_add_sta;
3034 priv->rx_handlers[REPLY_ERROR] = iwl3945_rx_reply_error;
3035 priv->rx_handlers[CHANNEL_SWITCH_NOTIFICATION] = iwl3945_rx_csa;
Zhu Yib481de92007-09-25 17:54:57 -07003036 priv->rx_handlers[SPECTRUM_MEASURE_NOTIFICATION] =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003037 iwl3945_rx_spectrum_measure_notif;
3038 priv->rx_handlers[PM_SLEEP_NOTIFICATION] = iwl3945_rx_pm_sleep_notif;
Zhu Yib481de92007-09-25 17:54:57 -07003039 priv->rx_handlers[PM_DEBUG_STATISTIC_NOTIFIC] =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003040 iwl3945_rx_pm_debug_statistics_notif;
3041 priv->rx_handlers[BEACON_NOTIFICATION] = iwl3945_rx_beacon_notif;
Zhu Yib481de92007-09-25 17:54:57 -07003042
Ben Cahill9fbab512007-11-29 11:09:47 +08003043 /*
3044 * The same handler is used for both the REPLY to a discrete
3045 * statistics request from the host as well as for the periodic
3046 * statistics notifications (after received beacons) from the uCode.
Zhu Yib481de92007-09-25 17:54:57 -07003047 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003048 priv->rx_handlers[REPLY_STATISTICS_CMD] = iwl3945_hw_rx_statistics;
3049 priv->rx_handlers[STATISTICS_NOTIFICATION] = iwl3945_hw_rx_statistics;
Zhu Yib481de92007-09-25 17:54:57 -07003050
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003051 priv->rx_handlers[REPLY_SCAN_CMD] = iwl3945_rx_reply_scan;
3052 priv->rx_handlers[SCAN_START_NOTIFICATION] = iwl3945_rx_scan_start_notif;
Zhu Yib481de92007-09-25 17:54:57 -07003053 priv->rx_handlers[SCAN_RESULTS_NOTIFICATION] =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003054 iwl3945_rx_scan_results_notif;
Zhu Yib481de92007-09-25 17:54:57 -07003055 priv->rx_handlers[SCAN_COMPLETE_NOTIFICATION] =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003056 iwl3945_rx_scan_complete_notif;
3057 priv->rx_handlers[CARD_STATE_NOTIFICATION] = iwl3945_rx_card_state_notif;
Zhu Yib481de92007-09-25 17:54:57 -07003058
Ben Cahill9fbab512007-11-29 11:09:47 +08003059 /* Set up hardware specific Rx handlers */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003060 iwl3945_hw_rx_handler_setup(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003061}
3062
3063/**
Tomas Winkler91c066f2008-03-06 17:36:55 -08003064 * iwl3945_cmd_queue_reclaim - Reclaim CMD queue entries
3065 * When FW advances 'R' index, all entries between old and new 'R' index
3066 * need to be reclaimed.
3067 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003068static void iwl3945_cmd_queue_reclaim(struct iwl_priv *priv,
Tomas Winkler91c066f2008-03-06 17:36:55 -08003069 int txq_id, int index)
3070{
Samuel Ortiz188cf6c2008-12-22 11:31:16 +08003071 struct iwl_tx_queue *txq = &priv->txq[txq_id];
Samuel Ortizd20b3c62008-12-19 10:37:15 +08003072 struct iwl_queue *q = &txq->q;
Tomas Winkler91c066f2008-03-06 17:36:55 -08003073 int nfreed = 0;
3074
Winkler, Tomas625a3812009-01-08 10:19:55 -08003075 if ((index >= q->n_bd) || (iwl_queue_used(q, index) == 0)) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08003076 IWL_ERR(priv, "Read index for DMA queue txq id (%d), index %d, "
Tomas Winkler91c066f2008-03-06 17:36:55 -08003077 "is out of range [0-%d] %d %d.\n", txq_id,
3078 index, q->n_bd, q->write_ptr, q->read_ptr);
3079 return;
3080 }
3081
3082 for (index = iwl_queue_inc_wrap(index, q->n_bd); q->read_ptr != index;
3083 q->read_ptr = iwl_queue_inc_wrap(q->read_ptr, q->n_bd)) {
3084 if (nfreed > 1) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08003085 IWL_ERR(priv, "HCMD skipped: index (%d) %d %d\n", index,
Tomas Winkler91c066f2008-03-06 17:36:55 -08003086 q->write_ptr, q->read_ptr);
3087 queue_work(priv->workqueue, &priv->restart);
3088 break;
3089 }
3090 nfreed++;
3091 }
3092}
3093
3094
3095/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003096 * iwl3945_tx_cmd_complete - Pull unused buffers off the queue and reclaim them
Zhu Yib481de92007-09-25 17:54:57 -07003097 * @rxb: Rx buffer to reclaim
3098 *
3099 * If an Rx buffer has an async callback associated with it the callback
3100 * will be executed. The attached skb (if present) will only be freed
3101 * if the callback returns 1
3102 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003103static void iwl3945_tx_cmd_complete(struct iwl_priv *priv,
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08003104 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003105{
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08003106 struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07003107 u16 sequence = le16_to_cpu(pkt->hdr.sequence);
3108 int txq_id = SEQ_TO_QUEUE(sequence);
3109 int index = SEQ_TO_INDEX(sequence);
Tomas Winkler600c0e12008-12-19 10:37:04 +08003110 int huge = !!(pkt->hdr.sequence & SEQ_HUGE_FRAME);
Zhu Yib481de92007-09-25 17:54:57 -07003111 int cmd_index;
Winkler, Tomasc2d79b42008-12-19 10:37:34 +08003112 struct iwl_cmd *cmd;
Zhu Yib481de92007-09-25 17:54:57 -07003113
Zhu Yib481de92007-09-25 17:54:57 -07003114 BUG_ON(txq_id != IWL_CMD_QUEUE_NUM);
3115
Samuel Ortiz188cf6c2008-12-22 11:31:16 +08003116 cmd_index = get_cmd_index(&priv->txq[IWL_CMD_QUEUE_NUM].q, index, huge);
3117 cmd = priv->txq[IWL_CMD_QUEUE_NUM].cmd[cmd_index];
Zhu Yib481de92007-09-25 17:54:57 -07003118
3119 /* Input error checking is done when commands are added to queue. */
3120 if (cmd->meta.flags & CMD_WANT_SKB) {
3121 cmd->meta.source->u.skb = rxb->skb;
3122 rxb->skb = NULL;
3123 } else if (cmd->meta.u.callback &&
3124 !cmd->meta.u.callback(priv, cmd, rxb->skb))
3125 rxb->skb = NULL;
3126
Tomas Winkler91c066f2008-03-06 17:36:55 -08003127 iwl3945_cmd_queue_reclaim(priv, txq_id, index);
Zhu Yib481de92007-09-25 17:54:57 -07003128
3129 if (!(cmd->meta.flags & CMD_ASYNC)) {
3130 clear_bit(STATUS_HCMD_ACTIVE, &priv->status);
3131 wake_up_interruptible(&priv->wait_command_queue);
3132 }
3133}
3134
3135/************************** RX-FUNCTIONS ****************************/
3136/*
3137 * Rx theory of operation
3138 *
3139 * The host allocates 32 DMA target addresses and passes the host address
3140 * to the firmware at register IWL_RFDS_TABLE_LOWER + N * RFD_SIZE where N is
3141 * 0 to 31
3142 *
3143 * Rx Queue Indexes
3144 * The host/firmware share two index registers for managing the Rx buffers.
3145 *
3146 * The READ index maps to the first position that the firmware may be writing
3147 * to -- the driver can read up to (but not including) this position and get
3148 * good data.
3149 * The READ index is managed by the firmware once the card is enabled.
3150 *
3151 * The WRITE index maps to the last position the driver has read from -- the
3152 * position preceding WRITE is the last slot the firmware can place a packet.
3153 *
3154 * The queue is empty (no good data) if WRITE = READ - 1, and is full if
3155 * WRITE = READ.
3156 *
Ben Cahill9fbab512007-11-29 11:09:47 +08003157 * During initialization, the host sets up the READ queue position to the first
Zhu Yib481de92007-09-25 17:54:57 -07003158 * INDEX position, and WRITE to the last (READ - 1 wrapped)
3159 *
Ben Cahill9fbab512007-11-29 11:09:47 +08003160 * When the firmware places a packet in a buffer, it will advance the READ index
Zhu Yib481de92007-09-25 17:54:57 -07003161 * and fire the RX interrupt. The driver can then query the READ index and
3162 * process as many packets as possible, moving the WRITE index forward as it
3163 * resets the Rx queue buffers with new memory.
3164 *
3165 * The management in the driver is as follows:
3166 * + A list of pre-allocated SKBs is stored in iwl->rxq->rx_free. When
3167 * iwl->rxq->free_count drops to or below RX_LOW_WATERMARK, work is scheduled
Ian Schram01ebd062007-10-25 17:15:22 +08003168 * to replenish the iwl->rxq->rx_free.
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003169 * + In iwl3945_rx_replenish (scheduled) if 'processed' != 'read' then the
Zhu Yib481de92007-09-25 17:54:57 -07003170 * iwl->rxq is replenished and the READ INDEX is updated (updating the
3171 * 'processed' and 'read' driver indexes as well)
3172 * + A received packet is processed and handed to the kernel network stack,
3173 * detached from the iwl->rxq. The driver 'processed' index is updated.
3174 * + The Host/Firmware iwl->rxq is replenished at tasklet time from the rx_free
3175 * list. If there are no allocated buffers in iwl->rxq->rx_free, the READ
3176 * INDEX is not incremented and iwl->status(RX_STALLED) is set. If there
3177 * were enough free buffers and RX_STALLED is set it is cleared.
3178 *
3179 *
3180 * Driver sequence:
3181 *
Ben Cahill9fbab512007-11-29 11:09:47 +08003182 * iwl3945_rx_replenish() Replenishes rx_free list from rx_used, and calls
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003183 * iwl3945_rx_queue_restock
Ben Cahill9fbab512007-11-29 11:09:47 +08003184 * iwl3945_rx_queue_restock() Moves available buffers from rx_free into Rx
Zhu Yib481de92007-09-25 17:54:57 -07003185 * queue, updates firmware pointers, and updates
3186 * the WRITE index. If insufficient rx_free buffers
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003187 * are available, schedules iwl3945_rx_replenish
Zhu Yib481de92007-09-25 17:54:57 -07003188 *
3189 * -- enable interrupts --
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08003190 * ISR - iwl3945_rx() Detach iwl_rx_mem_buffers from pool up to the
Zhu Yib481de92007-09-25 17:54:57 -07003191 * READ INDEX, detaching the SKB from the pool.
3192 * Moves the packet buffer from queue to rx_used.
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003193 * Calls iwl3945_rx_queue_restock to refill any empty
Zhu Yib481de92007-09-25 17:54:57 -07003194 * slots.
3195 * ...
3196 *
3197 */
3198
3199/**
Ben Cahill9fbab512007-11-29 11:09:47 +08003200 * iwl3945_dma_addr2rbd_ptr - convert a DMA address to a uCode read buffer ptr
Zhu Yib481de92007-09-25 17:54:57 -07003201 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003202static inline __le32 iwl3945_dma_addr2rbd_ptr(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07003203 dma_addr_t dma_addr)
3204{
3205 return cpu_to_le32((u32)dma_addr);
3206}
3207
3208/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003209 * iwl3945_rx_queue_restock - refill RX queue from pre-allocated pool
Zhu Yib481de92007-09-25 17:54:57 -07003210 *
Ben Cahill9fbab512007-11-29 11:09:47 +08003211 * If there are slots in the RX queue that need to be restocked,
Zhu Yib481de92007-09-25 17:54:57 -07003212 * and we have free pre-allocated buffers, fill the ranks as much
Ben Cahill9fbab512007-11-29 11:09:47 +08003213 * as we can, pulling from rx_free.
Zhu Yib481de92007-09-25 17:54:57 -07003214 *
3215 * This moves the 'write' index forward to catch up with 'processed', and
3216 * also updates the memory address in the firmware to reference the new
3217 * target buffer.
3218 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003219static int iwl3945_rx_queue_restock(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003220{
Abhijeet Kolekarcc2f3622008-12-19 10:37:25 +08003221 struct iwl_rx_queue *rxq = &priv->rxq;
Zhu Yib481de92007-09-25 17:54:57 -07003222 struct list_head *element;
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08003223 struct iwl_rx_mem_buffer *rxb;
Zhu Yib481de92007-09-25 17:54:57 -07003224 unsigned long flags;
3225 int write, rc;
3226
3227 spin_lock_irqsave(&rxq->lock, flags);
3228 write = rxq->write & ~0x7;
Winkler, Tomas37d68312009-01-08 10:19:54 -08003229 while ((iwl_rx_queue_space(rxq) > 0) && (rxq->free_count)) {
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003230 /* Get next free Rx buffer, remove from free list */
Zhu Yib481de92007-09-25 17:54:57 -07003231 element = rxq->rx_free.next;
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08003232 rxb = list_entry(element, struct iwl_rx_mem_buffer, list);
Zhu Yib481de92007-09-25 17:54:57 -07003233 list_del(element);
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003234
3235 /* Point to Rx buffer via next RBD in circular buffer */
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08003236 rxq->bd[rxq->write] = iwl3945_dma_addr2rbd_ptr(priv, rxb->real_dma_addr);
Zhu Yib481de92007-09-25 17:54:57 -07003237 rxq->queue[rxq->write] = rxb;
3238 rxq->write = (rxq->write + 1) & RX_QUEUE_MASK;
3239 rxq->free_count--;
3240 }
3241 spin_unlock_irqrestore(&rxq->lock, flags);
3242 /* If the pre-allocated buffer pool is dropping low, schedule to
3243 * refill it */
3244 if (rxq->free_count <= RX_LOW_WATERMARK)
3245 queue_work(priv->workqueue, &priv->rx_replenish);
3246
3247
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003248 /* If we've added more space for the firmware to place data, tell it.
3249 * Increment device's write pointer in multiples of 8. */
Zhu Yib481de92007-09-25 17:54:57 -07003250 if ((write != (rxq->write & ~0x7))
3251 || (abs(rxq->write - rxq->read) > 7)) {
3252 spin_lock_irqsave(&rxq->lock, flags);
3253 rxq->need_update = 1;
3254 spin_unlock_irqrestore(&rxq->lock, flags);
Winkler, Tomas141c43a2009-01-08 10:19:53 -08003255 rc = iwl_rx_queue_update_write_ptr(priv, rxq);
Zhu Yib481de92007-09-25 17:54:57 -07003256 if (rc)
3257 return rc;
3258 }
3259
3260 return 0;
3261}
3262
3263/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003264 * iwl3945_rx_replenish - Move all used packet from rx_used to rx_free
Zhu Yib481de92007-09-25 17:54:57 -07003265 *
3266 * When moving to rx_free an SKB is allocated for the slot.
3267 *
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003268 * Also restock the Rx queue via iwl3945_rx_queue_restock.
Ian Schram01ebd062007-10-25 17:15:22 +08003269 * This is called as a scheduled work item (except for during initialization)
Zhu Yib481de92007-09-25 17:54:57 -07003270 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003271static void iwl3945_rx_allocate(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003272{
Abhijeet Kolekarcc2f3622008-12-19 10:37:25 +08003273 struct iwl_rx_queue *rxq = &priv->rxq;
Zhu Yib481de92007-09-25 17:54:57 -07003274 struct list_head *element;
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08003275 struct iwl_rx_mem_buffer *rxb;
Zhu Yib481de92007-09-25 17:54:57 -07003276 unsigned long flags;
3277 spin_lock_irqsave(&rxq->lock, flags);
3278 while (!list_empty(&rxq->rx_used)) {
3279 element = rxq->rx_used.next;
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08003280 rxb = list_entry(element, struct iwl_rx_mem_buffer, list);
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003281
3282 /* Alloc a new receive buffer */
Zhu Yib481de92007-09-25 17:54:57 -07003283 rxb->skb =
Winkler, Tomas1e33dc62009-01-08 10:19:57 -08003284 alloc_skb(priv->hw_params.rx_buf_size,
3285 __GFP_NOWARN | GFP_ATOMIC);
Zhu Yib481de92007-09-25 17:54:57 -07003286 if (!rxb->skb) {
3287 if (net_ratelimit())
Tomas Winkler978785a2008-12-19 10:37:31 +08003288 IWL_CRIT(priv, ": Can not allocate SKB buffers\n");
Zhu Yib481de92007-09-25 17:54:57 -07003289 /* We don't reschedule replenish work here -- we will
3290 * call the restock method and if it still needs
3291 * more buffers it will schedule replenish */
3292 break;
3293 }
Zhu Yi12342c42007-12-20 11:27:32 +08003294
3295 /* If radiotap head is required, reserve some headroom here.
3296 * The physical head count is a variable rx_stats->phy_count.
3297 * We reserve 4 bytes here. Plus these extra bytes, the
3298 * headroom of the physical head should be enough for the
3299 * radiotap head that iwl3945 supported. See iwl3945_rt.
3300 */
3301 skb_reserve(rxb->skb, 4);
3302
Zhu Yib481de92007-09-25 17:54:57 -07003303 priv->alloc_rxb_skb++;
3304 list_del(element);
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003305
3306 /* Get physical address of RB/SKB */
Winkler, Tomas1e33dc62009-01-08 10:19:57 -08003307 rxb->real_dma_addr = pci_map_single(priv->pci_dev,
3308 rxb->skb->data,
3309 priv->hw_params.rx_buf_size,
3310 PCI_DMA_FROMDEVICE);
Zhu Yib481de92007-09-25 17:54:57 -07003311 list_add_tail(&rxb->list, &rxq->rx_free);
3312 rxq->free_count++;
3313 }
3314 spin_unlock_irqrestore(&rxq->lock, flags);
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08003315}
3316
3317/*
3318 * this should be called while priv->lock is locked
3319 */
Tomas Winkler4fd1f842007-12-05 20:59:58 +02003320static void __iwl3945_rx_replenish(void *data)
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08003321{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003322 struct iwl_priv *priv = data;
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08003323
3324 iwl3945_rx_allocate(priv);
3325 iwl3945_rx_queue_restock(priv);
3326}
3327
3328
3329void iwl3945_rx_replenish(void *data)
3330{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003331 struct iwl_priv *priv = data;
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08003332 unsigned long flags;
3333
3334 iwl3945_rx_allocate(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003335
3336 spin_lock_irqsave(&priv->lock, flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003337 iwl3945_rx_queue_restock(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003338 spin_unlock_irqrestore(&priv->lock, flags);
3339}
3340
Zhu Yib481de92007-09-25 17:54:57 -07003341/* Convert linear signal-to-noise ratio into dB */
3342static u8 ratio2dB[100] = {
3343/* 0 1 2 3 4 5 6 7 8 9 */
3344 0, 0, 6, 10, 12, 14, 16, 17, 18, 19, /* 00 - 09 */
3345 20, 21, 22, 22, 23, 23, 24, 25, 26, 26, /* 10 - 19 */
3346 26, 26, 26, 27, 27, 28, 28, 28, 29, 29, /* 20 - 29 */
3347 29, 30, 30, 30, 31, 31, 31, 31, 32, 32, /* 30 - 39 */
3348 32, 32, 32, 33, 33, 33, 33, 33, 34, 34, /* 40 - 49 */
3349 34, 34, 34, 34, 35, 35, 35, 35, 35, 35, /* 50 - 59 */
3350 36, 36, 36, 36, 36, 36, 36, 37, 37, 37, /* 60 - 69 */
3351 37, 37, 37, 37, 37, 38, 38, 38, 38, 38, /* 70 - 79 */
3352 38, 38, 38, 38, 38, 39, 39, 39, 39, 39, /* 80 - 89 */
3353 39, 39, 39, 39, 39, 40, 40, 40, 40, 40 /* 90 - 99 */
3354};
3355
3356/* Calculates a relative dB value from a ratio of linear
3357 * (i.e. not dB) signal levels.
3358 * Conversion assumes that levels are voltages (20*log), not powers (10*log). */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003359int iwl3945_calc_db_from_ratio(int sig_ratio)
Zhu Yib481de92007-09-25 17:54:57 -07003360{
Adrian Bunk221c80c2008-02-02 23:19:01 +02003361 /* 1000:1 or higher just report as 60 dB */
3362 if (sig_ratio >= 1000)
Zhu Yib481de92007-09-25 17:54:57 -07003363 return 60;
3364
Adrian Bunk221c80c2008-02-02 23:19:01 +02003365 /* 100:1 or higher, divide by 10 and use table,
Zhu Yib481de92007-09-25 17:54:57 -07003366 * add 20 dB to make up for divide by 10 */
Adrian Bunk221c80c2008-02-02 23:19:01 +02003367 if (sig_ratio >= 100)
Tomas Winkler3ac7f142008-07-21 02:40:14 +03003368 return 20 + (int)ratio2dB[sig_ratio/10];
Zhu Yib481de92007-09-25 17:54:57 -07003369
3370 /* We shouldn't see this */
3371 if (sig_ratio < 1)
3372 return 0;
3373
3374 /* Use table for ratios 1:1 - 99:1 */
3375 return (int)ratio2dB[sig_ratio];
3376}
3377
3378#define PERFECT_RSSI (-20) /* dBm */
3379#define WORST_RSSI (-95) /* dBm */
3380#define RSSI_RANGE (PERFECT_RSSI - WORST_RSSI)
3381
3382/* Calculate an indication of rx signal quality (a percentage, not dBm!).
3383 * See http://www.ces.clemson.edu/linux/signal_quality.shtml for info
3384 * about formulas used below. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003385int iwl3945_calc_sig_qual(int rssi_dbm, int noise_dbm)
Zhu Yib481de92007-09-25 17:54:57 -07003386{
3387 int sig_qual;
3388 int degradation = PERFECT_RSSI - rssi_dbm;
3389
3390 /* If we get a noise measurement, use signal-to-noise ratio (SNR)
3391 * as indicator; formula is (signal dbm - noise dbm).
3392 * SNR at or above 40 is a great signal (100%).
3393 * Below that, scale to fit SNR of 0 - 40 dB within 0 - 100% indicator.
3394 * Weakest usable signal is usually 10 - 15 dB SNR. */
3395 if (noise_dbm) {
3396 if (rssi_dbm - noise_dbm >= 40)
3397 return 100;
3398 else if (rssi_dbm < noise_dbm)
3399 return 0;
3400 sig_qual = ((rssi_dbm - noise_dbm) * 5) / 2;
3401
3402 /* Else use just the signal level.
3403 * This formula is a least squares fit of data points collected and
3404 * compared with a reference system that had a percentage (%) display
3405 * for signal quality. */
3406 } else
3407 sig_qual = (100 * (RSSI_RANGE * RSSI_RANGE) - degradation *
3408 (15 * RSSI_RANGE + 62 * degradation)) /
3409 (RSSI_RANGE * RSSI_RANGE);
3410
3411 if (sig_qual > 100)
3412 sig_qual = 100;
3413 else if (sig_qual < 1)
3414 sig_qual = 0;
3415
3416 return sig_qual;
3417}
3418
3419/**
Ben Cahill9fbab512007-11-29 11:09:47 +08003420 * iwl3945_rx_handle - Main entry function for receiving responses from uCode
Zhu Yib481de92007-09-25 17:54:57 -07003421 *
3422 * Uses the priv->rx_handlers callback function array to invoke
3423 * the appropriate handlers, including command responses,
3424 * frame-received notifications, and other notifications.
3425 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003426static void iwl3945_rx_handle(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003427{
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08003428 struct iwl_rx_mem_buffer *rxb;
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08003429 struct iwl_rx_packet *pkt;
Abhijeet Kolekarcc2f3622008-12-19 10:37:25 +08003430 struct iwl_rx_queue *rxq = &priv->rxq;
Zhu Yib481de92007-09-25 17:54:57 -07003431 u32 r, i;
3432 int reclaim;
3433 unsigned long flags;
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08003434 u8 fill_rx = 0;
Mohamed Abbasd68ab682008-02-07 13:16:33 -08003435 u32 count = 8;
Zhu Yib481de92007-09-25 17:54:57 -07003436
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003437 /* uCode's read index (stored in shared DRAM) indicates the last Rx
3438 * buffer that the driver may process (last buffer filled by ucode). */
Winkler, Tomas8cd812b2008-12-19 10:37:43 +08003439 r = le16_to_cpu(rxq->rb_stts->closed_rb_num) & 0x0FFF;
Zhu Yib481de92007-09-25 17:54:57 -07003440 i = rxq->read;
3441
Winkler, Tomas37d68312009-01-08 10:19:54 -08003442 if (iwl_rx_queue_space(rxq) > (RX_QUEUE_SIZE / 2))
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08003443 fill_rx = 1;
Zhu Yib481de92007-09-25 17:54:57 -07003444 /* Rx interrupt, but nothing sent from uCode */
3445 if (i == r)
3446 IWL_DEBUG(IWL_DL_RX | IWL_DL_ISR, "r = %d, i = %d\n", r, i);
3447
3448 while (i != r) {
3449 rxb = rxq->queue[i];
3450
Ben Cahill9fbab512007-11-29 11:09:47 +08003451 /* If an RXB doesn't have a Rx queue slot associated with it,
Zhu Yib481de92007-09-25 17:54:57 -07003452 * then a bug has been introduced in the queue refilling
3453 * routines -- catch it here */
3454 BUG_ON(rxb == NULL);
3455
3456 rxq->queue[i] = NULL;
3457
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08003458 pci_dma_sync_single_for_cpu(priv->pci_dev, rxb->real_dma_addr,
Winkler, Tomas1e33dc62009-01-08 10:19:57 -08003459 priv->hw_params.rx_buf_size,
Zhu Yib481de92007-09-25 17:54:57 -07003460 PCI_DMA_FROMDEVICE);
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08003461 pkt = (struct iwl_rx_packet *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07003462
3463 /* Reclaim a command buffer only if this packet is a response
3464 * to a (driver-originated) command.
3465 * If the packet (e.g. Rx frame) originated from uCode,
3466 * there is no command buffer to reclaim.
3467 * Ucode should set SEQ_RX_FRAME bit if ucode-originated,
3468 * but apparently a few don't get set; catch them here. */
3469 reclaim = !(pkt->hdr.sequence & SEQ_RX_FRAME) &&
3470 (pkt->hdr.cmd != STATISTICS_NOTIFICATION) &&
3471 (pkt->hdr.cmd != REPLY_TX);
3472
3473 /* Based on type of command response or notification,
3474 * handle those that need handling via function in
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003475 * rx_handlers table. See iwl3945_setup_rx_handlers() */
Zhu Yib481de92007-09-25 17:54:57 -07003476 if (priv->rx_handlers[pkt->hdr.cmd]) {
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08003477 IWL_DEBUG(IWL_DL_HCMD | IWL_DL_RX | IWL_DL_ISR,
Zhu Yib481de92007-09-25 17:54:57 -07003478 "r = %d, i = %d, %s, 0x%02x\n", r, i,
3479 get_cmd_string(pkt->hdr.cmd), pkt->hdr.cmd);
3480 priv->rx_handlers[pkt->hdr.cmd] (priv, rxb);
3481 } else {
3482 /* No handling needed */
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08003483 IWL_DEBUG(IWL_DL_HCMD | IWL_DL_RX | IWL_DL_ISR,
Zhu Yib481de92007-09-25 17:54:57 -07003484 "r %d i %d No handler needed for %s, 0x%02x\n",
3485 r, i, get_cmd_string(pkt->hdr.cmd),
3486 pkt->hdr.cmd);
3487 }
3488
3489 if (reclaim) {
Ben Cahill9fbab512007-11-29 11:09:47 +08003490 /* Invoke any callbacks, transfer the skb to caller, and
3491 * fire off the (possibly) blocking iwl3945_send_cmd()
Zhu Yib481de92007-09-25 17:54:57 -07003492 * as we reclaim the driver command queue */
3493 if (rxb && rxb->skb)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003494 iwl3945_tx_cmd_complete(priv, rxb);
Zhu Yib481de92007-09-25 17:54:57 -07003495 else
Winkler, Tomas39aadf82008-12-19 10:37:32 +08003496 IWL_WARN(priv, "Claim null rxb?\n");
Zhu Yib481de92007-09-25 17:54:57 -07003497 }
3498
3499 /* For now we just don't re-use anything. We can tweak this
3500 * later to try and re-use notification packets and SKBs that
3501 * fail to Rx correctly */
3502 if (rxb->skb != NULL) {
3503 priv->alloc_rxb_skb--;
3504 dev_kfree_skb_any(rxb->skb);
3505 rxb->skb = NULL;
3506 }
3507
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08003508 pci_unmap_single(priv->pci_dev, rxb->real_dma_addr,
Winkler, Tomas1e33dc62009-01-08 10:19:57 -08003509 priv->hw_params.rx_buf_size,
3510 PCI_DMA_FROMDEVICE);
Zhu Yib481de92007-09-25 17:54:57 -07003511 spin_lock_irqsave(&rxq->lock, flags);
3512 list_add_tail(&rxb->list, &priv->rxq.rx_used);
3513 spin_unlock_irqrestore(&rxq->lock, flags);
3514 i = (i + 1) & RX_QUEUE_MASK;
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08003515 /* If there are a lot of unused frames,
3516 * restock the Rx queue so ucode won't assert. */
3517 if (fill_rx) {
3518 count++;
3519 if (count >= 8) {
3520 priv->rxq.read = i;
3521 __iwl3945_rx_replenish(priv);
3522 count = 0;
3523 }
3524 }
Zhu Yib481de92007-09-25 17:54:57 -07003525 }
3526
3527 /* Backtrack one entry */
3528 priv->rxq.read = i;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003529 iwl3945_rx_queue_restock(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003530}
3531
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003532/**
3533 * iwl3945_tx_queue_update_write_ptr - Send new write index to hardware
3534 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003535static int iwl3945_tx_queue_update_write_ptr(struct iwl_priv *priv,
Samuel Ortiz188cf6c2008-12-22 11:31:16 +08003536 struct iwl_tx_queue *txq)
Zhu Yib481de92007-09-25 17:54:57 -07003537{
3538 u32 reg = 0;
3539 int rc = 0;
3540 int txq_id = txq->q.id;
3541
3542 if (txq->need_update == 0)
3543 return rc;
3544
3545 /* if we're trying to save power */
3546 if (test_bit(STATUS_POWER_PMI, &priv->status)) {
3547 /* wake up nic if it's powered down ...
3548 * uCode will wake up, and interrupt us again, so next
3549 * time we'll skip this part. */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003550 reg = iwl_read32(priv, CSR_UCODE_DRV_GP1);
Zhu Yib481de92007-09-25 17:54:57 -07003551
3552 if (reg & CSR_UCODE_DRV_GP1_BIT_MAC_SLEEP) {
3553 IWL_DEBUG_INFO("Requesting wakeup, GP1 = 0x%x\n", reg);
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003554 iwl_set_bit(priv, CSR_GP_CNTRL,
Zhu Yib481de92007-09-25 17:54:57 -07003555 CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
3556 return rc;
3557 }
3558
3559 /* restore this queue's parameters in nic hardware. */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003560 rc = iwl_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003561 if (rc)
3562 return rc;
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003563 iwl_write_direct32(priv, HBUS_TARG_WRPTR,
Tomas Winklerfc4b6852007-10-25 17:15:24 +08003564 txq->q.write_ptr | (txq_id << 8));
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003565 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003566
3567 /* else not in power-save mode, uCode will never sleep when we're
3568 * trying to tx (during RFKILL, we're not trying to tx). */
3569 } else
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003570 iwl_write32(priv, HBUS_TARG_WRPTR,
Tomas Winklerfc4b6852007-10-25 17:15:24 +08003571 txq->q.write_ptr | (txq_id << 8));
Zhu Yib481de92007-09-25 17:54:57 -07003572
3573 txq->need_update = 0;
3574
3575 return rc;
3576}
3577
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08003578#ifdef CONFIG_IWL3945_DEBUG
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003579static void iwl3945_print_rx_config_cmd(struct iwl_priv *priv,
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08003580 struct iwl3945_rxon_cmd *rxon)
Zhu Yib481de92007-09-25 17:54:57 -07003581{
3582 IWL_DEBUG_RADIO("RX CONFIG:\n");
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08003583 iwl_print_hex_dump(priv, IWL_DL_RADIO, (u8 *) rxon, sizeof(*rxon));
Zhu Yib481de92007-09-25 17:54:57 -07003584 IWL_DEBUG_RADIO("u16 channel: 0x%x\n", le16_to_cpu(rxon->channel));
3585 IWL_DEBUG_RADIO("u32 flags: 0x%08X\n", le32_to_cpu(rxon->flags));
3586 IWL_DEBUG_RADIO("u32 filter_flags: 0x%08x\n",
3587 le32_to_cpu(rxon->filter_flags));
3588 IWL_DEBUG_RADIO("u8 dev_type: 0x%x\n", rxon->dev_type);
3589 IWL_DEBUG_RADIO("u8 ofdm_basic_rates: 0x%02x\n",
3590 rxon->ofdm_basic_rates);
3591 IWL_DEBUG_RADIO("u8 cck_basic_rates: 0x%02x\n", rxon->cck_basic_rates);
Johannes Berge1749612008-10-27 15:59:26 -07003592 IWL_DEBUG_RADIO("u8[6] node_addr: %pM\n", rxon->node_addr);
3593 IWL_DEBUG_RADIO("u8[6] bssid_addr: %pM\n", rxon->bssid_addr);
Zhu Yib481de92007-09-25 17:54:57 -07003594 IWL_DEBUG_RADIO("u16 assoc_id: 0x%x\n", le16_to_cpu(rxon->assoc_id));
3595}
3596#endif
3597
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003598static void iwl3945_enable_interrupts(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003599{
3600 IWL_DEBUG_ISR("Enabling interrupts\n");
3601 set_bit(STATUS_INT_ENABLED, &priv->status);
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003602 iwl_write32(priv, CSR_INT_MASK, CSR_INI_SET_MASK);
Zhu Yib481de92007-09-25 17:54:57 -07003603}
3604
Mohamed Abbas0359fac2008-03-28 16:21:08 -07003605
3606/* call this function to flush any scheduled tasklet */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003607static inline void iwl_synchronize_irq(struct iwl_priv *priv)
Mohamed Abbas0359fac2008-03-28 16:21:08 -07003608{
Tomas Winklera96a27f2008-10-23 23:48:56 -07003609 /* wait to make sure we flush pending tasklet*/
Mohamed Abbas0359fac2008-03-28 16:21:08 -07003610 synchronize_irq(priv->pci_dev->irq);
3611 tasklet_kill(&priv->irq_tasklet);
3612}
3613
3614
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003615static inline void iwl3945_disable_interrupts(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003616{
3617 clear_bit(STATUS_INT_ENABLED, &priv->status);
3618
3619 /* disable interrupts from uCode/NIC to host */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003620 iwl_write32(priv, CSR_INT_MASK, 0x00000000);
Zhu Yib481de92007-09-25 17:54:57 -07003621
3622 /* acknowledge/clear/reset any interrupts still pending
3623 * from uCode or flow handler (Rx/Tx DMA) */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003624 iwl_write32(priv, CSR_INT, 0xffffffff);
3625 iwl_write32(priv, CSR_FH_INT_STATUS, 0xffffffff);
Zhu Yib481de92007-09-25 17:54:57 -07003626 IWL_DEBUG_ISR("Disabled interrupts\n");
3627}
3628
3629static const char *desc_lookup(int i)
3630{
3631 switch (i) {
3632 case 1:
3633 return "FAIL";
3634 case 2:
3635 return "BAD_PARAM";
3636 case 3:
3637 return "BAD_CHECKSUM";
3638 case 4:
3639 return "NMI_INTERRUPT";
3640 case 5:
3641 return "SYSASSERT";
3642 case 6:
3643 return "FATAL_ERROR";
3644 }
3645
3646 return "UNKNOWN";
3647}
3648
3649#define ERROR_START_OFFSET (1 * sizeof(u32))
3650#define ERROR_ELEM_SIZE (7 * sizeof(u32))
3651
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003652static void iwl3945_dump_nic_error_log(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003653{
3654 u32 i;
3655 u32 desc, time, count, base, data1;
3656 u32 blink1, blink2, ilink1, ilink2;
3657 int rc;
3658
3659 base = le32_to_cpu(priv->card_alive.error_event_table_ptr);
3660
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003661 if (!iwl3945_hw_valid_rtc_data_addr(base)) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08003662 IWL_ERR(priv, "Not valid error log pointer 0x%08X\n", base);
Zhu Yib481de92007-09-25 17:54:57 -07003663 return;
3664 }
3665
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003666 rc = iwl_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003667 if (rc) {
Winkler, Tomas39aadf82008-12-19 10:37:32 +08003668 IWL_WARN(priv, "Can not read from adapter at this time.\n");
Zhu Yib481de92007-09-25 17:54:57 -07003669 return;
3670 }
3671
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003672 count = iwl_read_targ_mem(priv, base);
Zhu Yib481de92007-09-25 17:54:57 -07003673
3674 if (ERROR_START_OFFSET <= count * ERROR_ELEM_SIZE) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08003675 IWL_ERR(priv, "Start IWL Error Log Dump:\n");
3676 IWL_ERR(priv, "Status: 0x%08lX, count: %d\n",
3677 priv->status, count);
Zhu Yib481de92007-09-25 17:54:57 -07003678 }
3679
Winkler, Tomas15b16872008-12-19 10:37:33 +08003680 IWL_ERR(priv, "Desc Time asrtPC blink2 "
Zhu Yib481de92007-09-25 17:54:57 -07003681 "ilink1 nmiPC Line\n");
3682 for (i = ERROR_START_OFFSET;
3683 i < (count * ERROR_ELEM_SIZE) + ERROR_START_OFFSET;
3684 i += ERROR_ELEM_SIZE) {
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003685 desc = iwl_read_targ_mem(priv, base + i);
Zhu Yib481de92007-09-25 17:54:57 -07003686 time =
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003687 iwl_read_targ_mem(priv, base + i + 1 * sizeof(u32));
Zhu Yib481de92007-09-25 17:54:57 -07003688 blink1 =
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003689 iwl_read_targ_mem(priv, base + i + 2 * sizeof(u32));
Zhu Yib481de92007-09-25 17:54:57 -07003690 blink2 =
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003691 iwl_read_targ_mem(priv, base + i + 3 * sizeof(u32));
Zhu Yib481de92007-09-25 17:54:57 -07003692 ilink1 =
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003693 iwl_read_targ_mem(priv, base + i + 4 * sizeof(u32));
Zhu Yib481de92007-09-25 17:54:57 -07003694 ilink2 =
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003695 iwl_read_targ_mem(priv, base + i + 5 * sizeof(u32));
Zhu Yib481de92007-09-25 17:54:57 -07003696 data1 =
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003697 iwl_read_targ_mem(priv, base + i + 6 * sizeof(u32));
Zhu Yib481de92007-09-25 17:54:57 -07003698
Winkler, Tomas15b16872008-12-19 10:37:33 +08003699 IWL_ERR(priv,
3700 "%-13s (#%d) %010u 0x%05X 0x%05X 0x%05X 0x%05X %u\n\n",
3701 desc_lookup(desc), desc, time, blink1, blink2,
3702 ilink1, ilink2, data1);
Zhu Yib481de92007-09-25 17:54:57 -07003703 }
3704
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003705 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003706
3707}
3708
Ben Cahillf58177b2007-11-29 11:09:43 +08003709#define EVENT_START_OFFSET (6 * sizeof(u32))
Zhu Yib481de92007-09-25 17:54:57 -07003710
3711/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003712 * iwl3945_print_event_log - Dump error event log to syslog
Zhu Yib481de92007-09-25 17:54:57 -07003713 *
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003714 * NOTE: Must be called with iwl_grab_nic_access() already obtained!
Zhu Yib481de92007-09-25 17:54:57 -07003715 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003716static void iwl3945_print_event_log(struct iwl_priv *priv, u32 start_idx,
Zhu Yib481de92007-09-25 17:54:57 -07003717 u32 num_events, u32 mode)
3718{
3719 u32 i;
3720 u32 base; /* SRAM byte address of event log header */
3721 u32 event_size; /* 2 u32s, or 3 u32s if timestamp recorded */
3722 u32 ptr; /* SRAM byte address of log data */
3723 u32 ev, time, data; /* event log data */
3724
3725 if (num_events == 0)
3726 return;
3727
3728 base = le32_to_cpu(priv->card_alive.log_event_table_ptr);
3729
3730 if (mode == 0)
3731 event_size = 2 * sizeof(u32);
3732 else
3733 event_size = 3 * sizeof(u32);
3734
3735 ptr = base + EVENT_START_OFFSET + (start_idx * event_size);
3736
3737 /* "time" is actually "data" for mode 0 (no timestamp).
3738 * place event id # at far right for easier visual parsing. */
3739 for (i = 0; i < num_events; i++) {
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003740 ev = iwl_read_targ_mem(priv, ptr);
Zhu Yib481de92007-09-25 17:54:57 -07003741 ptr += sizeof(u32);
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003742 time = iwl_read_targ_mem(priv, ptr);
Zhu Yib481de92007-09-25 17:54:57 -07003743 ptr += sizeof(u32);
Winkler, Tomas15b16872008-12-19 10:37:33 +08003744 if (mode == 0) {
3745 /* data, ev */
3746 IWL_ERR(priv, "0x%08x\t%04u\n", time, ev);
3747 } else {
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003748 data = iwl_read_targ_mem(priv, ptr);
Zhu Yib481de92007-09-25 17:54:57 -07003749 ptr += sizeof(u32);
Winkler, Tomas15b16872008-12-19 10:37:33 +08003750 IWL_ERR(priv, "%010u\t0x%08x\t%04u\n", time, data, ev);
Zhu Yib481de92007-09-25 17:54:57 -07003751 }
3752 }
3753}
3754
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003755static void iwl3945_dump_nic_event_log(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003756{
3757 int rc;
3758 u32 base; /* SRAM byte address of event log header */
3759 u32 capacity; /* event log capacity in # entries */
3760 u32 mode; /* 0 - no timestamp, 1 - timestamp recorded */
3761 u32 num_wraps; /* # times uCode wrapped to top of log */
3762 u32 next_entry; /* index of next entry to be written by uCode */
3763 u32 size; /* # entries that we'll print */
3764
3765 base = le32_to_cpu(priv->card_alive.log_event_table_ptr);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003766 if (!iwl3945_hw_valid_rtc_data_addr(base)) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08003767 IWL_ERR(priv, "Invalid event log pointer 0x%08X\n", base);
Zhu Yib481de92007-09-25 17:54:57 -07003768 return;
3769 }
3770
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003771 rc = iwl_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003772 if (rc) {
Winkler, Tomas39aadf82008-12-19 10:37:32 +08003773 IWL_WARN(priv, "Can not read from adapter at this time.\n");
Zhu Yib481de92007-09-25 17:54:57 -07003774 return;
3775 }
3776
3777 /* event log header */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003778 capacity = iwl_read_targ_mem(priv, base);
3779 mode = iwl_read_targ_mem(priv, base + (1 * sizeof(u32)));
3780 num_wraps = iwl_read_targ_mem(priv, base + (2 * sizeof(u32)));
3781 next_entry = iwl_read_targ_mem(priv, base + (3 * sizeof(u32)));
Zhu Yib481de92007-09-25 17:54:57 -07003782
3783 size = num_wraps ? capacity : next_entry;
3784
3785 /* bail out if nothing in log */
3786 if (size == 0) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08003787 IWL_ERR(priv, "Start IWL Event Log Dump: nothing in log\n");
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003788 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003789 return;
3790 }
3791
Winkler, Tomas15b16872008-12-19 10:37:33 +08003792 IWL_ERR(priv, "Start IWL Event Log Dump: display count %d, wraps %d\n",
Zhu Yib481de92007-09-25 17:54:57 -07003793 size, num_wraps);
3794
3795 /* if uCode has wrapped back to top of log, start at the oldest entry,
3796 * i.e the next one that uCode would fill. */
3797 if (num_wraps)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003798 iwl3945_print_event_log(priv, next_entry,
Zhu Yib481de92007-09-25 17:54:57 -07003799 capacity - next_entry, mode);
3800
3801 /* (then/else) start at top of log */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003802 iwl3945_print_event_log(priv, 0, next_entry, mode);
Zhu Yib481de92007-09-25 17:54:57 -07003803
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003804 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003805}
3806
3807/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003808 * iwl3945_irq_handle_error - called for HW or SW error interrupt from card
Zhu Yib481de92007-09-25 17:54:57 -07003809 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003810static void iwl3945_irq_handle_error(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003811{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003812 /* Set the FW error flag -- cleared on iwl3945_down */
Zhu Yib481de92007-09-25 17:54:57 -07003813 set_bit(STATUS_FW_ERROR, &priv->status);
3814
3815 /* Cancel currently queued command. */
3816 clear_bit(STATUS_HCMD_ACTIVE, &priv->status);
3817
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08003818#ifdef CONFIG_IWL3945_DEBUG
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08003819 if (priv->debug_level & IWL_DL_FW_ERRORS) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003820 iwl3945_dump_nic_error_log(priv);
3821 iwl3945_dump_nic_event_log(priv);
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08003822 iwl3945_print_rx_config_cmd(priv, &priv->staging39_rxon);
Zhu Yib481de92007-09-25 17:54:57 -07003823 }
3824#endif
3825
3826 wake_up_interruptible(&priv->wait_command_queue);
3827
3828 /* Keep the restart process from trying to send host
3829 * commands by clearing the INIT status bit */
3830 clear_bit(STATUS_READY, &priv->status);
3831
3832 if (!test_bit(STATUS_EXIT_PENDING, &priv->status)) {
3833 IWL_DEBUG(IWL_DL_INFO | IWL_DL_FW_ERRORS,
3834 "Restarting adapter due to uCode error.\n");
3835
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003836 if (iwl3945_is_associated(priv)) {
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08003837 memcpy(&priv->recovery39_rxon, &priv->active39_rxon,
3838 sizeof(priv->recovery39_rxon));
Zhu Yib481de92007-09-25 17:54:57 -07003839 priv->error_recovering = 1;
3840 }
3841 queue_work(priv->workqueue, &priv->restart);
3842 }
3843}
3844
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003845static void iwl3945_error_recovery(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003846{
3847 unsigned long flags;
3848
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08003849 memcpy(&priv->staging39_rxon, &priv->recovery39_rxon,
3850 sizeof(priv->staging39_rxon));
3851 priv->staging39_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003852 iwl3945_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003853
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003854 iwl3945_add_station(priv, priv->bssid, 1, 0);
Zhu Yib481de92007-09-25 17:54:57 -07003855
3856 spin_lock_irqsave(&priv->lock, flags);
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08003857 priv->assoc_id = le16_to_cpu(priv->staging39_rxon.assoc_id);
Zhu Yib481de92007-09-25 17:54:57 -07003858 priv->error_recovering = 0;
3859 spin_unlock_irqrestore(&priv->lock, flags);
3860}
3861
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003862static void iwl3945_irq_tasklet(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003863{
3864 u32 inta, handled = 0;
3865 u32 inta_fh;
3866 unsigned long flags;
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08003867#ifdef CONFIG_IWL3945_DEBUG
Zhu Yib481de92007-09-25 17:54:57 -07003868 u32 inta_mask;
3869#endif
3870
3871 spin_lock_irqsave(&priv->lock, flags);
3872
3873 /* Ack/clear/reset pending uCode interrupts.
3874 * Note: Some bits in CSR_INT are "OR" of bits in CSR_FH_INT_STATUS,
3875 * and will clear only when CSR_FH_INT_STATUS gets cleared. */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003876 inta = iwl_read32(priv, CSR_INT);
3877 iwl_write32(priv, CSR_INT, inta);
Zhu Yib481de92007-09-25 17:54:57 -07003878
3879 /* Ack/clear/reset pending flow-handler (DMA) interrupts.
3880 * Any new interrupts that happen after this, either while we're
3881 * in this tasklet, or later, will show up in next ISR/tasklet. */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003882 inta_fh = iwl_read32(priv, CSR_FH_INT_STATUS);
3883 iwl_write32(priv, CSR_FH_INT_STATUS, inta_fh);
Zhu Yib481de92007-09-25 17:54:57 -07003884
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08003885#ifdef CONFIG_IWL3945_DEBUG
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08003886 if (priv->debug_level & IWL_DL_ISR) {
Ben Cahill9fbab512007-11-29 11:09:47 +08003887 /* just for debug */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003888 inta_mask = iwl_read32(priv, CSR_INT_MASK);
Zhu Yib481de92007-09-25 17:54:57 -07003889 IWL_DEBUG_ISR("inta 0x%08x, enabled 0x%08x, fh 0x%08x\n",
3890 inta, inta_mask, inta_fh);
3891 }
3892#endif
3893
3894 /* Since CSR_INT and CSR_FH_INT_STATUS reads and clears are not
3895 * atomic, make sure that inta covers all the interrupts that
3896 * we've discovered, even if FH interrupt came in just after
3897 * reading CSR_INT. */
Tomas Winkler6f83eaa2008-03-04 18:09:28 -08003898 if (inta_fh & CSR39_FH_INT_RX_MASK)
Zhu Yib481de92007-09-25 17:54:57 -07003899 inta |= CSR_INT_BIT_FH_RX;
Tomas Winkler6f83eaa2008-03-04 18:09:28 -08003900 if (inta_fh & CSR39_FH_INT_TX_MASK)
Zhu Yib481de92007-09-25 17:54:57 -07003901 inta |= CSR_INT_BIT_FH_TX;
3902
3903 /* Now service all interrupt bits discovered above. */
3904 if (inta & CSR_INT_BIT_HW_ERR) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08003905 IWL_ERR(priv, "Microcode HW error detected. Restarting.\n");
Zhu Yib481de92007-09-25 17:54:57 -07003906
3907 /* Tell the device to stop sending interrupts */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003908 iwl3945_disable_interrupts(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003909
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003910 iwl3945_irq_handle_error(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003911
3912 handled |= CSR_INT_BIT_HW_ERR;
3913
3914 spin_unlock_irqrestore(&priv->lock, flags);
3915
3916 return;
3917 }
3918
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08003919#ifdef CONFIG_IWL3945_DEBUG
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08003920 if (priv->debug_level & (IWL_DL_ISR)) {
Zhu Yib481de92007-09-25 17:54:57 -07003921 /* NIC fires this, but we don't use it, redundant with WAKEUP */
Joonwoo Park25c03d82008-01-23 10:15:20 -08003922 if (inta & CSR_INT_BIT_SCD)
3923 IWL_DEBUG_ISR("Scheduler finished to transmit "
3924 "the frame/frames.\n");
Zhu Yib481de92007-09-25 17:54:57 -07003925
3926 /* Alive notification via Rx interrupt will do the real work */
3927 if (inta & CSR_INT_BIT_ALIVE)
3928 IWL_DEBUG_ISR("Alive interrupt\n");
3929 }
3930#endif
3931 /* Safely ignore these bits for debug checks below */
Joonwoo Park25c03d82008-01-23 10:15:20 -08003932 inta &= ~(CSR_INT_BIT_SCD | CSR_INT_BIT_ALIVE);
Zhu Yib481de92007-09-25 17:54:57 -07003933
Zhu Yib481de92007-09-25 17:54:57 -07003934 /* Error detected by uCode */
3935 if (inta & CSR_INT_BIT_SW_ERR) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08003936 IWL_ERR(priv, "Microcode SW error detected. "
3937 "Restarting 0x%X.\n", inta);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003938 iwl3945_irq_handle_error(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003939 handled |= CSR_INT_BIT_SW_ERR;
3940 }
3941
3942 /* uCode wakes up after power-down sleep */
3943 if (inta & CSR_INT_BIT_WAKEUP) {
3944 IWL_DEBUG_ISR("Wakeup interrupt\n");
Winkler, Tomas141c43a2009-01-08 10:19:53 -08003945 iwl_rx_queue_update_write_ptr(priv, &priv->rxq);
Samuel Ortiz188cf6c2008-12-22 11:31:16 +08003946 iwl3945_tx_queue_update_write_ptr(priv, &priv->txq[0]);
3947 iwl3945_tx_queue_update_write_ptr(priv, &priv->txq[1]);
3948 iwl3945_tx_queue_update_write_ptr(priv, &priv->txq[2]);
3949 iwl3945_tx_queue_update_write_ptr(priv, &priv->txq[3]);
3950 iwl3945_tx_queue_update_write_ptr(priv, &priv->txq[4]);
3951 iwl3945_tx_queue_update_write_ptr(priv, &priv->txq[5]);
Zhu Yib481de92007-09-25 17:54:57 -07003952
3953 handled |= CSR_INT_BIT_WAKEUP;
3954 }
3955
3956 /* All uCode command responses, including Tx command responses,
3957 * Rx "responses" (frame-received notification), and other
3958 * notifications from uCode come through here*/
3959 if (inta & (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX)) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003960 iwl3945_rx_handle(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003961 handled |= (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX);
3962 }
3963
3964 if (inta & CSR_INT_BIT_FH_TX) {
3965 IWL_DEBUG_ISR("Tx interrupt\n");
3966
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003967 iwl_write32(priv, CSR_FH_INT_STATUS, (1 << 6));
3968 if (!iwl_grab_nic_access(priv)) {
3969 iwl_write_direct32(priv, FH39_TCSR_CREDIT
Tomas Winklerbddadf82008-12-19 10:37:01 +08003970 (FH39_SRVC_CHNL), 0x0);
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003971 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003972 }
3973 handled |= CSR_INT_BIT_FH_TX;
3974 }
3975
3976 if (inta & ~handled)
Winkler, Tomas15b16872008-12-19 10:37:33 +08003977 IWL_ERR(priv, "Unhandled INTA bits 0x%08x\n", inta & ~handled);
Zhu Yib481de92007-09-25 17:54:57 -07003978
3979 if (inta & ~CSR_INI_SET_MASK) {
Winkler, Tomas39aadf82008-12-19 10:37:32 +08003980 IWL_WARN(priv, "Disabled INTA bits 0x%08x were pending\n",
Zhu Yib481de92007-09-25 17:54:57 -07003981 inta & ~CSR_INI_SET_MASK);
Winkler, Tomas39aadf82008-12-19 10:37:32 +08003982 IWL_WARN(priv, " with FH_INT = 0x%08x\n", inta_fh);
Zhu Yib481de92007-09-25 17:54:57 -07003983 }
3984
3985 /* Re-enable all interrupts */
Mohamed Abbas0359fac2008-03-28 16:21:08 -07003986 /* only Re-enable if disabled by irq */
3987 if (test_bit(STATUS_INT_ENABLED, &priv->status))
3988 iwl3945_enable_interrupts(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003989
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08003990#ifdef CONFIG_IWL3945_DEBUG
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08003991 if (priv->debug_level & (IWL_DL_ISR)) {
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003992 inta = iwl_read32(priv, CSR_INT);
3993 inta_mask = iwl_read32(priv, CSR_INT_MASK);
3994 inta_fh = iwl_read32(priv, CSR_FH_INT_STATUS);
Zhu Yib481de92007-09-25 17:54:57 -07003995 IWL_DEBUG_ISR("End inta 0x%08x, enabled 0x%08x, fh 0x%08x, "
3996 "flags 0x%08lx\n", inta, inta_mask, inta_fh, flags);
3997 }
3998#endif
3999 spin_unlock_irqrestore(&priv->lock, flags);
4000}
4001
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004002static irqreturn_t iwl3945_isr(int irq, void *data)
Zhu Yib481de92007-09-25 17:54:57 -07004003{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08004004 struct iwl_priv *priv = data;
Zhu Yib481de92007-09-25 17:54:57 -07004005 u32 inta, inta_mask;
4006 u32 inta_fh;
4007 if (!priv)
4008 return IRQ_NONE;
4009
4010 spin_lock(&priv->lock);
4011
4012 /* Disable (but don't clear!) interrupts here to avoid
4013 * back-to-back ISRs and sporadic interrupts from our NIC.
4014 * If we have something to service, the tasklet will re-enable ints.
4015 * If we *don't* have something, we'll re-enable before leaving here. */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08004016 inta_mask = iwl_read32(priv, CSR_INT_MASK); /* just for debug */
4017 iwl_write32(priv, CSR_INT_MASK, 0x00000000);
Zhu Yib481de92007-09-25 17:54:57 -07004018
4019 /* Discover which interrupts are active/pending */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08004020 inta = iwl_read32(priv, CSR_INT);
4021 inta_fh = iwl_read32(priv, CSR_FH_INT_STATUS);
Zhu Yib481de92007-09-25 17:54:57 -07004022
4023 /* Ignore interrupt if there's nothing in NIC to service.
4024 * This may be due to IRQ shared with another device,
4025 * or due to sporadic interrupts thrown from our NIC. */
4026 if (!inta && !inta_fh) {
4027 IWL_DEBUG_ISR("Ignore interrupt, inta == 0, inta_fh == 0\n");
4028 goto none;
4029 }
4030
4031 if ((inta == 0xFFFFFFFF) || ((inta & 0xFFFFFFF0) == 0xa5a5a5a0)) {
4032 /* Hardware disappeared */
Winkler, Tomas39aadf82008-12-19 10:37:32 +08004033 IWL_WARN(priv, "HARDWARE GONE?? INTA == 0x%08x\n", inta);
Oliver Neukumcb4da1a2007-11-13 21:10:32 -08004034 goto unplugged;
Zhu Yib481de92007-09-25 17:54:57 -07004035 }
4036
4037 IWL_DEBUG_ISR("ISR inta 0x%08x, enabled 0x%08x, fh 0x%08x\n",
4038 inta, inta_mask, inta_fh);
4039
Joonwoo Park25c03d82008-01-23 10:15:20 -08004040 inta &= ~CSR_INT_BIT_SCD;
4041
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004042 /* iwl3945_irq_tasklet() will service interrupts and re-enable them */
Joonwoo Park25c03d82008-01-23 10:15:20 -08004043 if (likely(inta || inta_fh))
4044 tasklet_schedule(&priv->irq_tasklet);
Oliver Neukumcb4da1a2007-11-13 21:10:32 -08004045unplugged:
Zhu Yib481de92007-09-25 17:54:57 -07004046 spin_unlock(&priv->lock);
4047
4048 return IRQ_HANDLED;
4049
4050 none:
4051 /* re-enable interrupts here since we don't have anything to service. */
Mohamed Abbas0359fac2008-03-28 16:21:08 -07004052 /* only Re-enable if disabled by irq */
4053 if (test_bit(STATUS_INT_ENABLED, &priv->status))
4054 iwl3945_enable_interrupts(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004055 spin_unlock(&priv->lock);
4056 return IRQ_NONE;
4057}
4058
4059/************************** EEPROM BANDS ****************************
4060 *
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004061 * The iwl3945_eeprom_band definitions below provide the mapping from the
Zhu Yib481de92007-09-25 17:54:57 -07004062 * EEPROM contents to the specific channel number supported for each
4063 * band.
4064 *
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08004065 * For example, iwl3945_priv->eeprom39.band_3_channels[4] from the band_3
Zhu Yib481de92007-09-25 17:54:57 -07004066 * definition below maps to physical channel 42 in the 5.2GHz spectrum.
4067 * The specific geography and calibration information for that channel
4068 * is contained in the eeprom map itself.
4069 *
4070 * During init, we copy the eeprom information and channel map
4071 * information into priv->channel_info_24/52 and priv->channel_map_24/52
4072 *
4073 * channel_map_24/52 provides the index in the channel_info array for a
4074 * given channel. We have to have two separate maps as there is channel
4075 * overlap with the 2.4GHz and 5.2GHz spectrum as seen in band_1 and
4076 * band_2
4077 *
4078 * A value of 0xff stored in the channel_map indicates that the channel
4079 * is not supported by the hardware at all.
4080 *
4081 * A value of 0xfe in the channel_map indicates that the channel is not
4082 * valid for Tx with the current hardware. This means that
4083 * while the system can tune and receive on a given channel, it may not
4084 * be able to associate or transmit any frames on that
4085 * channel. There is no corresponding channel information for that
4086 * entry.
4087 *
4088 *********************************************************************/
4089
4090/* 2.4 GHz */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004091static const u8 iwl3945_eeprom_band_1[14] = {
Zhu Yib481de92007-09-25 17:54:57 -07004092 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14
4093};
4094
4095/* 5.2 GHz bands */
Ben Cahill9fbab512007-11-29 11:09:47 +08004096static const u8 iwl3945_eeprom_band_2[] = { /* 4915-5080MHz */
Zhu Yib481de92007-09-25 17:54:57 -07004097 183, 184, 185, 187, 188, 189, 192, 196, 7, 8, 11, 12, 16
4098};
4099
Ben Cahill9fbab512007-11-29 11:09:47 +08004100static const u8 iwl3945_eeprom_band_3[] = { /* 5170-5320MHz */
Zhu Yib481de92007-09-25 17:54:57 -07004101 34, 36, 38, 40, 42, 44, 46, 48, 52, 56, 60, 64
4102};
4103
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004104static const u8 iwl3945_eeprom_band_4[] = { /* 5500-5700MHz */
Zhu Yib481de92007-09-25 17:54:57 -07004105 100, 104, 108, 112, 116, 120, 124, 128, 132, 136, 140
4106};
4107
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004108static const u8 iwl3945_eeprom_band_5[] = { /* 5725-5825MHz */
Zhu Yib481de92007-09-25 17:54:57 -07004109 145, 149, 153, 157, 161, 165
4110};
4111
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08004112static void iwl3945_init_band_reference(const struct iwl_priv *priv, int band,
Zhu Yib481de92007-09-25 17:54:57 -07004113 int *eeprom_ch_count,
Samuel Ortiz0f741d92008-12-19 10:37:10 +08004114 const struct iwl_eeprom_channel
Zhu Yib481de92007-09-25 17:54:57 -07004115 **eeprom_ch_info,
4116 const u8 **eeprom_ch_index)
4117{
4118 switch (band) {
4119 case 1: /* 2.4GHz band */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004120 *eeprom_ch_count = ARRAY_SIZE(iwl3945_eeprom_band_1);
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08004121 *eeprom_ch_info = priv->eeprom39.band_1_channels;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004122 *eeprom_ch_index = iwl3945_eeprom_band_1;
Zhu Yib481de92007-09-25 17:54:57 -07004123 break;
Ben Cahill9fbab512007-11-29 11:09:47 +08004124 case 2: /* 4.9GHz band */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004125 *eeprom_ch_count = ARRAY_SIZE(iwl3945_eeprom_band_2);
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08004126 *eeprom_ch_info = priv->eeprom39.band_2_channels;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004127 *eeprom_ch_index = iwl3945_eeprom_band_2;
Zhu Yib481de92007-09-25 17:54:57 -07004128 break;
4129 case 3: /* 5.2GHz band */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004130 *eeprom_ch_count = ARRAY_SIZE(iwl3945_eeprom_band_3);
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08004131 *eeprom_ch_info = priv->eeprom39.band_3_channels;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004132 *eeprom_ch_index = iwl3945_eeprom_band_3;
Zhu Yib481de92007-09-25 17:54:57 -07004133 break;
Ben Cahill9fbab512007-11-29 11:09:47 +08004134 case 4: /* 5.5GHz band */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004135 *eeprom_ch_count = ARRAY_SIZE(iwl3945_eeprom_band_4);
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08004136 *eeprom_ch_info = priv->eeprom39.band_4_channels;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004137 *eeprom_ch_index = iwl3945_eeprom_band_4;
Zhu Yib481de92007-09-25 17:54:57 -07004138 break;
Ben Cahill9fbab512007-11-29 11:09:47 +08004139 case 5: /* 5.7GHz band */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004140 *eeprom_ch_count = ARRAY_SIZE(iwl3945_eeprom_band_5);
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08004141 *eeprom_ch_info = priv->eeprom39.band_5_channels;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004142 *eeprom_ch_index = iwl3945_eeprom_band_5;
Zhu Yib481de92007-09-25 17:54:57 -07004143 break;
4144 default:
4145 BUG();
4146 return;
4147 }
4148}
4149
Cahill, Ben M6440adb2007-11-29 11:09:55 +08004150/**
4151 * iwl3945_get_channel_info - Find driver's private channel info
4152 *
4153 * Based on band and channel number.
4154 */
Samuel Ortizd20b3c62008-12-19 10:37:15 +08004155const struct iwl_channel_info *
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08004156iwl3945_get_channel_info(const struct iwl_priv *priv,
Samuel Ortizd20b3c62008-12-19 10:37:15 +08004157 enum ieee80211_band band, u16 channel)
Zhu Yib481de92007-09-25 17:54:57 -07004158{
4159 int i;
4160
Johannes Berg8318d782008-01-24 19:38:38 +01004161 switch (band) {
4162 case IEEE80211_BAND_5GHZ:
Zhu Yib481de92007-09-25 17:54:57 -07004163 for (i = 14; i < priv->channel_count; i++) {
4164 if (priv->channel_info[i].channel == channel)
4165 return &priv->channel_info[i];
4166 }
4167 break;
4168
Johannes Berg8318d782008-01-24 19:38:38 +01004169 case IEEE80211_BAND_2GHZ:
Zhu Yib481de92007-09-25 17:54:57 -07004170 if (channel >= 1 && channel <= 14)
4171 return &priv->channel_info[channel - 1];
4172 break;
Johannes Berg8318d782008-01-24 19:38:38 +01004173 case IEEE80211_NUM_BANDS:
4174 WARN_ON(1);
Zhu Yib481de92007-09-25 17:54:57 -07004175 }
4176
4177 return NULL;
4178}
4179
4180#define CHECK_AND_PRINT(x) ((eeprom_ch_info[ch].flags & EEPROM_CHANNEL_##x) \
4181 ? # x " " : "")
4182
Cahill, Ben M6440adb2007-11-29 11:09:55 +08004183/**
4184 * iwl3945_init_channel_map - Set up driver's info for all possible channels
4185 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08004186static int iwl3945_init_channel_map(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004187{
4188 int eeprom_ch_count = 0;
4189 const u8 *eeprom_ch_index = NULL;
Samuel Ortiz0f741d92008-12-19 10:37:10 +08004190 const struct iwl_eeprom_channel *eeprom_ch_info = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07004191 int band, ch;
Samuel Ortizd20b3c62008-12-19 10:37:15 +08004192 struct iwl_channel_info *ch_info;
Zhu Yib481de92007-09-25 17:54:57 -07004193
4194 if (priv->channel_count) {
4195 IWL_DEBUG_INFO("Channel map already initialized.\n");
4196 return 0;
4197 }
4198
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08004199 if (priv->eeprom39.version < 0x2f) {
Winkler, Tomas39aadf82008-12-19 10:37:32 +08004200 IWL_WARN(priv, "Unsupported EEPROM version: 0x%04X\n",
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08004201 priv->eeprom39.version);
Zhu Yib481de92007-09-25 17:54:57 -07004202 return -EINVAL;
4203 }
4204
4205 IWL_DEBUG_INFO("Initializing regulatory info from EEPROM\n");
4206
4207 priv->channel_count =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004208 ARRAY_SIZE(iwl3945_eeprom_band_1) +
4209 ARRAY_SIZE(iwl3945_eeprom_band_2) +
4210 ARRAY_SIZE(iwl3945_eeprom_band_3) +
4211 ARRAY_SIZE(iwl3945_eeprom_band_4) +
4212 ARRAY_SIZE(iwl3945_eeprom_band_5);
Zhu Yib481de92007-09-25 17:54:57 -07004213
4214 IWL_DEBUG_INFO("Parsing data for %d channels.\n", priv->channel_count);
4215
Samuel Ortizd20b3c62008-12-19 10:37:15 +08004216 priv->channel_info = kzalloc(sizeof(struct iwl_channel_info) *
Zhu Yib481de92007-09-25 17:54:57 -07004217 priv->channel_count, GFP_KERNEL);
4218 if (!priv->channel_info) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08004219 IWL_ERR(priv, "Could not allocate channel_info\n");
Zhu Yib481de92007-09-25 17:54:57 -07004220 priv->channel_count = 0;
4221 return -ENOMEM;
4222 }
4223
4224 ch_info = priv->channel_info;
4225
4226 /* Loop through the 5 EEPROM bands adding them in order to the
4227 * channel map we maintain (that contains additional information than
4228 * what just in the EEPROM) */
4229 for (band = 1; band <= 5; band++) {
4230
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004231 iwl3945_init_band_reference(priv, band, &eeprom_ch_count,
Zhu Yib481de92007-09-25 17:54:57 -07004232 &eeprom_ch_info, &eeprom_ch_index);
4233
4234 /* Loop through each band adding each of the channels */
4235 for (ch = 0; ch < eeprom_ch_count; ch++) {
4236 ch_info->channel = eeprom_ch_index[ch];
Johannes Berg8318d782008-01-24 19:38:38 +01004237 ch_info->band = (band == 1) ? IEEE80211_BAND_2GHZ :
4238 IEEE80211_BAND_5GHZ;
Zhu Yib481de92007-09-25 17:54:57 -07004239
4240 /* permanently store EEPROM's channel regulatory flags
4241 * and max power in channel info database. */
4242 ch_info->eeprom = eeprom_ch_info[ch];
4243
4244 /* Copy the run-time flags so they are there even on
4245 * invalid channels */
4246 ch_info->flags = eeprom_ch_info[ch].flags;
4247
4248 if (!(is_channel_valid(ch_info))) {
4249 IWL_DEBUG_INFO("Ch. %d Flags %x [%sGHz] - "
4250 "No traffic\n",
4251 ch_info->channel,
4252 ch_info->flags,
4253 is_channel_a_band(ch_info) ?
4254 "5.2" : "2.4");
4255 ch_info++;
4256 continue;
4257 }
4258
4259 /* Initialize regulatory-based run-time data */
4260 ch_info->max_power_avg = ch_info->curr_txpow =
4261 eeprom_ch_info[ch].max_power_avg;
4262 ch_info->scan_power = eeprom_ch_info[ch].max_power_avg;
4263 ch_info->min_power = 0;
4264
Guy Cohenfe7c4042008-04-21 15:41:56 -07004265 IWL_DEBUG_INFO("Ch. %d [%sGHz] %s%s%s%s%s%s(0x%02x"
Zhu Yib481de92007-09-25 17:54:57 -07004266 " %ddBm): Ad-Hoc %ssupported\n",
4267 ch_info->channel,
4268 is_channel_a_band(ch_info) ?
4269 "5.2" : "2.4",
Tomas Winkler8211ef72008-03-02 01:36:04 +02004270 CHECK_AND_PRINT(VALID),
Zhu Yib481de92007-09-25 17:54:57 -07004271 CHECK_AND_PRINT(IBSS),
4272 CHECK_AND_PRINT(ACTIVE),
4273 CHECK_AND_PRINT(RADAR),
4274 CHECK_AND_PRINT(WIDE),
Zhu Yib481de92007-09-25 17:54:57 -07004275 CHECK_AND_PRINT(DFS),
4276 eeprom_ch_info[ch].flags,
4277 eeprom_ch_info[ch].max_power_avg,
4278 ((eeprom_ch_info[ch].
4279 flags & EEPROM_CHANNEL_IBSS)
4280 && !(eeprom_ch_info[ch].
4281 flags & EEPROM_CHANNEL_RADAR))
4282 ? "" : "not ");
4283
4284 /* Set the user_txpower_limit to the highest power
4285 * supported by any channel */
4286 if (eeprom_ch_info[ch].max_power_avg >
4287 priv->user_txpower_limit)
4288 priv->user_txpower_limit =
4289 eeprom_ch_info[ch].max_power_avg;
4290
4291 ch_info++;
4292 }
4293 }
4294
Cahill, Ben M6440adb2007-11-29 11:09:55 +08004295 /* Set up txpower settings in driver for all channels */
Zhu Yib481de92007-09-25 17:54:57 -07004296 if (iwl3945_txpower_set_from_eeprom(priv))
4297 return -EIO;
4298
4299 return 0;
4300}
4301
Reinette Chatre849e0dc2008-01-23 10:15:18 -08004302/*
4303 * iwl3945_free_channel_map - undo allocations in iwl3945_init_channel_map
4304 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08004305static void iwl3945_free_channel_map(struct iwl_priv *priv)
Reinette Chatre849e0dc2008-01-23 10:15:18 -08004306{
4307 kfree(priv->channel_info);
4308 priv->channel_count = 0;
4309}
4310
Zhu Yib481de92007-09-25 17:54:57 -07004311/* For active scan, listen ACTIVE_DWELL_TIME (msec) on each channel after
4312 * sending probe req. This should be set long enough to hear probe responses
4313 * from more than one AP. */
Abhijeet Kolekarf9340522008-09-03 11:26:58 +08004314#define IWL_ACTIVE_DWELL_TIME_24 (30) /* all times in msec */
4315#define IWL_ACTIVE_DWELL_TIME_52 (20)
4316
4317#define IWL_ACTIVE_DWELL_FACTOR_24GHZ (3)
4318#define IWL_ACTIVE_DWELL_FACTOR_52GHZ (2)
Zhu Yib481de92007-09-25 17:54:57 -07004319
4320/* For faster active scanning, scan will move to the next channel if fewer than
4321 * PLCP_QUIET_THRESH packets are heard on this channel within
4322 * ACTIVE_QUIET_TIME after sending probe request. This shortens the dwell
4323 * time if it's a quiet channel (nothing responded to our probe, and there's
4324 * no other traffic).
4325 * Disable "quiet" feature by setting PLCP_QUIET_THRESH to 0. */
4326#define IWL_PLCP_QUIET_THRESH __constant_cpu_to_le16(1) /* packets */
Abhijeet Kolekarf9340522008-09-03 11:26:58 +08004327#define IWL_ACTIVE_QUIET_TIME __constant_cpu_to_le16(10) /* msec */
Zhu Yib481de92007-09-25 17:54:57 -07004328
4329/* For passive scan, listen PASSIVE_DWELL_TIME (msec) on each channel.
4330 * Must be set longer than active dwell time.
4331 * For the most reliable scan, set > AP beacon interval (typically 100msec). */
4332#define IWL_PASSIVE_DWELL_TIME_24 (20) /* all times in msec */
4333#define IWL_PASSIVE_DWELL_TIME_52 (10)
4334#define IWL_PASSIVE_DWELL_BASE (100)
4335#define IWL_CHANNEL_TUNE_TIME 5
4336
Kolekar, Abhijeete720ce92008-11-07 09:58:42 -08004337#define IWL_SCAN_PROBE_MASK(n) (BIT(n) | (BIT(n) - BIT(1)))
Abhijeet Kolekarf9340522008-09-03 11:26:58 +08004338
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08004339static inline u16 iwl3945_get_active_dwell_time(struct iwl_priv *priv,
Abhijeet Kolekarf9340522008-09-03 11:26:58 +08004340 enum ieee80211_band band,
4341 u8 n_probes)
Zhu Yib481de92007-09-25 17:54:57 -07004342{
Johannes Berg8318d782008-01-24 19:38:38 +01004343 if (band == IEEE80211_BAND_5GHZ)
Abhijeet Kolekarf9340522008-09-03 11:26:58 +08004344 return IWL_ACTIVE_DWELL_TIME_52 +
4345 IWL_ACTIVE_DWELL_FACTOR_52GHZ * (n_probes + 1);
Zhu Yib481de92007-09-25 17:54:57 -07004346 else
Abhijeet Kolekarf9340522008-09-03 11:26:58 +08004347 return IWL_ACTIVE_DWELL_TIME_24 +
4348 IWL_ACTIVE_DWELL_FACTOR_24GHZ * (n_probes + 1);
Zhu Yib481de92007-09-25 17:54:57 -07004349}
4350
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08004351static u16 iwl3945_get_passive_dwell_time(struct iwl_priv *priv,
Johannes Berg8318d782008-01-24 19:38:38 +01004352 enum ieee80211_band band)
Zhu Yib481de92007-09-25 17:54:57 -07004353{
Johannes Berg8318d782008-01-24 19:38:38 +01004354 u16 passive = (band == IEEE80211_BAND_2GHZ) ?
Zhu Yib481de92007-09-25 17:54:57 -07004355 IWL_PASSIVE_DWELL_BASE + IWL_PASSIVE_DWELL_TIME_24 :
4356 IWL_PASSIVE_DWELL_BASE + IWL_PASSIVE_DWELL_TIME_52;
4357
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004358 if (iwl3945_is_associated(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07004359 /* If we're associated, we clamp the maximum passive
4360 * dwell time to be 98% of the beacon interval (minus
4361 * 2 * channel tune time) */
4362 passive = priv->beacon_int;
4363 if ((passive > IWL_PASSIVE_DWELL_BASE) || !passive)
4364 passive = IWL_PASSIVE_DWELL_BASE;
4365 passive = (passive * 98) / 100 - IWL_CHANNEL_TUNE_TIME * 2;
4366 }
4367
Zhu Yib481de92007-09-25 17:54:57 -07004368 return passive;
4369}
4370
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08004371static int iwl3945_get_channels_for_scan(struct iwl_priv *priv,
Johannes Berg8318d782008-01-24 19:38:38 +01004372 enum ieee80211_band band,
Abhijeet Kolekarf9340522008-09-03 11:26:58 +08004373 u8 is_active, u8 n_probes,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004374 struct iwl3945_scan_channel *scan_ch)
Zhu Yib481de92007-09-25 17:54:57 -07004375{
4376 const struct ieee80211_channel *channels = NULL;
Johannes Berg8318d782008-01-24 19:38:38 +01004377 const struct ieee80211_supported_band *sband;
Samuel Ortizd20b3c62008-12-19 10:37:15 +08004378 const struct iwl_channel_info *ch_info;
Zhu Yib481de92007-09-25 17:54:57 -07004379 u16 passive_dwell = 0;
4380 u16 active_dwell = 0;
4381 int added, i;
4382
Kolekar, Abhijeetcbba18c2008-12-19 10:37:42 +08004383 sband = iwl_get_hw_mode(priv, band);
Johannes Berg8318d782008-01-24 19:38:38 +01004384 if (!sband)
Zhu Yib481de92007-09-25 17:54:57 -07004385 return 0;
4386
Johannes Berg8318d782008-01-24 19:38:38 +01004387 channels = sband->channels;
Zhu Yib481de92007-09-25 17:54:57 -07004388
Abhijeet Kolekarf9340522008-09-03 11:26:58 +08004389 active_dwell = iwl3945_get_active_dwell_time(priv, band, n_probes);
Johannes Berg8318d782008-01-24 19:38:38 +01004390 passive_dwell = iwl3945_get_passive_dwell_time(priv, band);
Zhu Yib481de92007-09-25 17:54:57 -07004391
Abhijeet Kolekar8f4807a2008-09-03 11:26:31 +08004392 if (passive_dwell <= active_dwell)
4393 passive_dwell = active_dwell + 1;
4394
Johannes Berg8318d782008-01-24 19:38:38 +01004395 for (i = 0, added = 0; i < sband->n_channels; i++) {
Johannes Berg182e2e62008-04-04 10:41:56 +02004396 if (channels[i].flags & IEEE80211_CHAN_DISABLED)
4397 continue;
4398
Johannes Berg8318d782008-01-24 19:38:38 +01004399 scan_ch->channel = channels[i].hw_value;
Zhu Yib481de92007-09-25 17:54:57 -07004400
Johannes Berg8318d782008-01-24 19:38:38 +01004401 ch_info = iwl3945_get_channel_info(priv, band, scan_ch->channel);
Zhu Yib481de92007-09-25 17:54:57 -07004402 if (!is_channel_valid(ch_info)) {
Ron Rindjunsky66b50042008-06-25 16:46:31 +08004403 IWL_DEBUG_SCAN("Channel %d is INVALID for this band.\n",
Zhu Yib481de92007-09-25 17:54:57 -07004404 scan_ch->channel);
4405 continue;
4406 }
4407
Zhu Yib481de92007-09-25 17:54:57 -07004408 scan_ch->active_dwell = cpu_to_le16(active_dwell);
4409 scan_ch->passive_dwell = cpu_to_le16(passive_dwell);
Abhijeet Kolekar011a0332008-12-02 12:14:07 -08004410 /* If passive , set up for auto-switch
4411 * and use long active_dwell time.
4412 */
4413 if (!is_active || is_channel_passive(ch_info) ||
4414 (channels[i].flags & IEEE80211_CHAN_PASSIVE_SCAN)) {
4415 scan_ch->type = 0; /* passive */
4416 if (IWL_UCODE_API(priv->ucode_ver) == 1)
4417 scan_ch->active_dwell = cpu_to_le16(passive_dwell - 1);
4418 } else {
4419 scan_ch->type = 1; /* active */
4420 }
4421
4422 /* Set direct probe bits. These may be used both for active
4423 * scan channels (probes gets sent right away),
4424 * or for passive channels (probes get se sent only after
4425 * hearing clear Rx packet).*/
4426 if (IWL_UCODE_API(priv->ucode_ver) >= 2) {
4427 if (n_probes)
4428 scan_ch->type |= IWL_SCAN_PROBE_MASK(n_probes);
4429 } else {
4430 /* uCode v1 does not allow setting direct probe bits on
4431 * passive channel. */
4432 if ((scan_ch->type & 1) && n_probes)
4433 scan_ch->type |= IWL_SCAN_PROBE_MASK(n_probes);
4434 }
Zhu Yib481de92007-09-25 17:54:57 -07004435
Ben Cahill9fbab512007-11-29 11:09:47 +08004436 /* Set txpower levels to defaults */
Zhu Yib481de92007-09-25 17:54:57 -07004437 scan_ch->tpc.dsp_atten = 110;
4438 /* scan_pwr_info->tpc.dsp_atten; */
4439
4440 /*scan_pwr_info->tpc.tx_gain; */
Johannes Berg8318d782008-01-24 19:38:38 +01004441 if (band == IEEE80211_BAND_5GHZ)
Zhu Yib481de92007-09-25 17:54:57 -07004442 scan_ch->tpc.tx_gain = ((1 << 5) | (3 << 3)) | 3;
4443 else {
4444 scan_ch->tpc.tx_gain = ((1 << 5) | (5 << 3));
4445 /* NOTE: if we were doing 6Mb OFDM for scans we'd use
Ben Cahill9fbab512007-11-29 11:09:47 +08004446 * power level:
Reinette Chatre8a1b0242008-01-14 17:46:25 -08004447 * scan_ch->tpc.tx_gain = ((1 << 5) | (2 << 3)) | 3;
Zhu Yib481de92007-09-25 17:54:57 -07004448 */
4449 }
4450
4451 IWL_DEBUG_SCAN("Scanning %d [%s %d]\n",
4452 scan_ch->channel,
4453 (scan_ch->type & 1) ? "ACTIVE" : "PASSIVE",
4454 (scan_ch->type & 1) ?
4455 active_dwell : passive_dwell);
4456
4457 scan_ch++;
4458 added++;
4459 }
4460
4461 IWL_DEBUG_SCAN("total channels to scan %d \n", added);
4462 return added;
4463}
4464
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08004465static void iwl3945_init_hw_rates(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07004466 struct ieee80211_rate *rates)
4467{
4468 int i;
4469
4470 for (i = 0; i < IWL_RATE_COUNT; i++) {
Johannes Berg8318d782008-01-24 19:38:38 +01004471 rates[i].bitrate = iwl3945_rates[i].ieee * 5;
4472 rates[i].hw_value = i; /* Rate scaling will work on indexes */
4473 rates[i].hw_value_short = i;
4474 rates[i].flags = 0;
Samuel Ortizd9829a62008-12-19 10:37:12 +08004475 if ((i > IWL39_LAST_OFDM_RATE) || (i < IWL_FIRST_OFDM_RATE)) {
Zhu Yib481de92007-09-25 17:54:57 -07004476 /*
Johannes Berg8318d782008-01-24 19:38:38 +01004477 * If CCK != 1M then set short preamble rate flag.
Zhu Yib481de92007-09-25 17:54:57 -07004478 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004479 rates[i].flags |= (iwl3945_rates[i].plcp == 10) ?
Johannes Berg8318d782008-01-24 19:38:38 +01004480 0 : IEEE80211_RATE_SHORT_PREAMBLE;
Zhu Yib481de92007-09-25 17:54:57 -07004481 }
Zhu Yib481de92007-09-25 17:54:57 -07004482 }
4483}
4484
4485/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004486 * iwl3945_init_geos - Initialize mac80211's geo/channel info based from eeprom
Zhu Yib481de92007-09-25 17:54:57 -07004487 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08004488static int iwl3945_init_geos(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004489{
Samuel Ortizd20b3c62008-12-19 10:37:15 +08004490 struct iwl_channel_info *ch;
Tomas Winkler8211ef72008-03-02 01:36:04 +02004491 struct ieee80211_supported_band *sband;
Zhu Yib481de92007-09-25 17:54:57 -07004492 struct ieee80211_channel *channels;
4493 struct ieee80211_channel *geo_ch;
4494 struct ieee80211_rate *rates;
4495 int i = 0;
Zhu Yib481de92007-09-25 17:54:57 -07004496
Johannes Berg8318d782008-01-24 19:38:38 +01004497 if (priv->bands[IEEE80211_BAND_2GHZ].n_bitrates ||
4498 priv->bands[IEEE80211_BAND_5GHZ].n_bitrates) {
Zhu Yib481de92007-09-25 17:54:57 -07004499 IWL_DEBUG_INFO("Geography modes already initialized.\n");
4500 set_bit(STATUS_GEO_CONFIGURED, &priv->status);
4501 return 0;
4502 }
4503
Zhu Yib481de92007-09-25 17:54:57 -07004504 channels = kzalloc(sizeof(struct ieee80211_channel) *
4505 priv->channel_count, GFP_KERNEL);
Johannes Berg8318d782008-01-24 19:38:38 +01004506 if (!channels)
Zhu Yib481de92007-09-25 17:54:57 -07004507 return -ENOMEM;
Zhu Yib481de92007-09-25 17:54:57 -07004508
Tomas Winkler8211ef72008-03-02 01:36:04 +02004509 rates = kzalloc((sizeof(struct ieee80211_rate) * (IWL_RATE_COUNT + 1)),
Zhu Yib481de92007-09-25 17:54:57 -07004510 GFP_KERNEL);
4511 if (!rates) {
Zhu Yib481de92007-09-25 17:54:57 -07004512 kfree(channels);
4513 return -ENOMEM;
4514 }
4515
Zhu Yib481de92007-09-25 17:54:57 -07004516 /* 5.2GHz channels start after the 2.4GHz channels */
Tomas Winkler8211ef72008-03-02 01:36:04 +02004517 sband = &priv->bands[IEEE80211_BAND_5GHZ];
4518 sband->channels = &channels[ARRAY_SIZE(iwl3945_eeprom_band_1)];
4519 /* just OFDM */
4520 sband->bitrates = &rates[IWL_FIRST_OFDM_RATE];
4521 sband->n_bitrates = IWL_RATE_COUNT - IWL_FIRST_OFDM_RATE;
Zhu Yib481de92007-09-25 17:54:57 -07004522
Tomas Winkler8211ef72008-03-02 01:36:04 +02004523 sband = &priv->bands[IEEE80211_BAND_2GHZ];
4524 sband->channels = channels;
4525 /* OFDM & CCK */
4526 sband->bitrates = rates;
4527 sband->n_bitrates = IWL_RATE_COUNT;
Zhu Yib481de92007-09-25 17:54:57 -07004528
4529 priv->ieee_channels = channels;
4530 priv->ieee_rates = rates;
4531
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004532 iwl3945_init_hw_rates(priv, rates);
Zhu Yib481de92007-09-25 17:54:57 -07004533
Tomas Winkler8211ef72008-03-02 01:36:04 +02004534 for (i = 0; i < priv->channel_count; i++) {
Zhu Yib481de92007-09-25 17:54:57 -07004535 ch = &priv->channel_info[i];
4536
Tomas Winkler8211ef72008-03-02 01:36:04 +02004537 /* FIXME: might be removed if scan is OK*/
4538 if (!is_channel_valid(ch))
Zhu Yib481de92007-09-25 17:54:57 -07004539 continue;
Zhu Yib481de92007-09-25 17:54:57 -07004540
4541 if (is_channel_a_band(ch))
Tomas Winkler8211ef72008-03-02 01:36:04 +02004542 sband = &priv->bands[IEEE80211_BAND_5GHZ];
Johannes Berg8318d782008-01-24 19:38:38 +01004543 else
Tomas Winkler8211ef72008-03-02 01:36:04 +02004544 sband = &priv->bands[IEEE80211_BAND_2GHZ];
Zhu Yib481de92007-09-25 17:54:57 -07004545
Tomas Winkler8211ef72008-03-02 01:36:04 +02004546 geo_ch = &sband->channels[sband->n_channels++];
4547
4548 geo_ch->center_freq = ieee80211_channel_to_frequency(ch->channel);
Johannes Berg8318d782008-01-24 19:38:38 +01004549 geo_ch->max_power = ch->max_power_avg;
4550 geo_ch->max_antenna_gain = 0xff;
Mohamed Abbas7b723042008-01-31 21:46:40 -08004551 geo_ch->hw_value = ch->channel;
Zhu Yib481de92007-09-25 17:54:57 -07004552
4553 if (is_channel_valid(ch)) {
Johannes Berg8318d782008-01-24 19:38:38 +01004554 if (!(ch->flags & EEPROM_CHANNEL_IBSS))
4555 geo_ch->flags |= IEEE80211_CHAN_NO_IBSS;
Zhu Yib481de92007-09-25 17:54:57 -07004556
Johannes Berg8318d782008-01-24 19:38:38 +01004557 if (!(ch->flags & EEPROM_CHANNEL_ACTIVE))
4558 geo_ch->flags |= IEEE80211_CHAN_PASSIVE_SCAN;
Zhu Yib481de92007-09-25 17:54:57 -07004559
4560 if (ch->flags & EEPROM_CHANNEL_RADAR)
Johannes Berg8318d782008-01-24 19:38:38 +01004561 geo_ch->flags |= IEEE80211_CHAN_RADAR;
Zhu Yib481de92007-09-25 17:54:57 -07004562
4563 if (ch->max_power_avg > priv->max_channel_txpower_limit)
4564 priv->max_channel_txpower_limit =
4565 ch->max_power_avg;
Tomas Winkler8211ef72008-03-02 01:36:04 +02004566 } else {
Johannes Berg8318d782008-01-24 19:38:38 +01004567 geo_ch->flags |= IEEE80211_CHAN_DISABLED;
Tomas Winkler8211ef72008-03-02 01:36:04 +02004568 }
4569
4570 /* Save flags for reg domain usage */
4571 geo_ch->orig_flags = geo_ch->flags;
4572
4573 IWL_DEBUG_INFO("Channel %d Freq=%d[%sGHz] %s flag=0%X\n",
4574 ch->channel, geo_ch->center_freq,
4575 is_channel_a_band(ch) ? "5.2" : "2.4",
4576 geo_ch->flags & IEEE80211_CHAN_DISABLED ?
4577 "restricted" : "valid",
4578 geo_ch->flags);
Zhu Yib481de92007-09-25 17:54:57 -07004579 }
4580
Tomas Winkler82b9a122008-03-04 18:09:30 -08004581 if ((priv->bands[IEEE80211_BAND_5GHZ].n_channels == 0) &&
4582 priv->cfg->sku & IWL_SKU_A) {
Tomas Winkler978785a2008-12-19 10:37:31 +08004583 IWL_INFO(priv, "Incorrectly detected BG card as ABG. "
4584 "Please send your PCI ID 0x%04X:0x%04X to maintainer.\n",
4585 priv->pci_dev->device, priv->pci_dev->subsystem_device);
Tomas Winkler82b9a122008-03-04 18:09:30 -08004586 priv->cfg->sku &= ~IWL_SKU_A;
Zhu Yib481de92007-09-25 17:54:57 -07004587 }
4588
Tomas Winkler978785a2008-12-19 10:37:31 +08004589 IWL_INFO(priv, "Tunable channels: %d 802.11bg, %d 802.11a channels\n",
Johannes Berg8318d782008-01-24 19:38:38 +01004590 priv->bands[IEEE80211_BAND_2GHZ].n_channels,
4591 priv->bands[IEEE80211_BAND_5GHZ].n_channels);
Zhu Yib481de92007-09-25 17:54:57 -07004592
John W. Linvillee0e0a672008-03-25 15:58:40 -04004593 if (priv->bands[IEEE80211_BAND_2GHZ].n_channels)
4594 priv->hw->wiphy->bands[IEEE80211_BAND_2GHZ] =
4595 &priv->bands[IEEE80211_BAND_2GHZ];
4596 if (priv->bands[IEEE80211_BAND_5GHZ].n_channels)
4597 priv->hw->wiphy->bands[IEEE80211_BAND_5GHZ] =
4598 &priv->bands[IEEE80211_BAND_5GHZ];
Zhu Yib481de92007-09-25 17:54:57 -07004599
Zhu Yib481de92007-09-25 17:54:57 -07004600 set_bit(STATUS_GEO_CONFIGURED, &priv->status);
4601
4602 return 0;
4603}
4604
Reinette Chatre849e0dc2008-01-23 10:15:18 -08004605/*
4606 * iwl3945_free_geos - undo allocations in iwl3945_init_geos
4607 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08004608static void iwl3945_free_geos(struct iwl_priv *priv)
Reinette Chatre849e0dc2008-01-23 10:15:18 -08004609{
Reinette Chatre849e0dc2008-01-23 10:15:18 -08004610 kfree(priv->ieee_channels);
4611 kfree(priv->ieee_rates);
4612 clear_bit(STATUS_GEO_CONFIGURED, &priv->status);
4613}
4614
Zhu Yib481de92007-09-25 17:54:57 -07004615/******************************************************************************
4616 *
4617 * uCode download functions
4618 *
4619 ******************************************************************************/
4620
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08004621static void iwl3945_dealloc_ucode_pci(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004622{
Tomas Winkler98c92212008-01-14 17:46:20 -08004623 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_code);
4624 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_data);
4625 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_data_backup);
4626 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_init);
4627 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_init_data);
4628 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_boot);
Zhu Yib481de92007-09-25 17:54:57 -07004629}
4630
4631/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004632 * iwl3945_verify_inst_full - verify runtime uCode image in card vs. host,
Zhu Yib481de92007-09-25 17:54:57 -07004633 * looking at all data.
4634 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08004635static int iwl3945_verify_inst_full(struct iwl_priv *priv, __le32 *image, u32 len)
Zhu Yib481de92007-09-25 17:54:57 -07004636{
4637 u32 val;
4638 u32 save_len = len;
4639 int rc = 0;
4640 u32 errcnt;
4641
4642 IWL_DEBUG_INFO("ucode inst image size is %u\n", len);
4643
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08004644 rc = iwl_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004645 if (rc)
4646 return rc;
4647
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08004648 iwl_write_direct32(priv, HBUS_TARG_MEM_RADDR,
Samuel Ortiz250bdd22008-12-19 10:37:11 +08004649 IWL39_RTC_INST_LOWER_BOUND);
Zhu Yib481de92007-09-25 17:54:57 -07004650
4651 errcnt = 0;
4652 for (; len > 0; len -= sizeof(u32), image++) {
4653 /* read data comes through single port, auto-incr addr */
4654 /* NOTE: Use the debugless read so we don't flood kernel log
4655 * if IWL_DL_IO is set */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08004656 val = _iwl_read_direct32(priv, HBUS_TARG_MEM_RDAT);
Zhu Yib481de92007-09-25 17:54:57 -07004657 if (val != le32_to_cpu(*image)) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08004658 IWL_ERR(priv, "uCode INST section is invalid at "
Zhu Yib481de92007-09-25 17:54:57 -07004659 "offset 0x%x, is 0x%x, s/b 0x%x\n",
4660 save_len - len, val, le32_to_cpu(*image));
4661 rc = -EIO;
4662 errcnt++;
4663 if (errcnt >= 20)
4664 break;
4665 }
4666 }
4667
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08004668 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004669
4670 if (!errcnt)
Ian Schrambc434dd2007-10-25 17:15:29 +08004671 IWL_DEBUG_INFO("ucode image in INSTRUCTION memory is good\n");
Zhu Yib481de92007-09-25 17:54:57 -07004672
4673 return rc;
4674}
4675
4676
4677/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004678 * iwl3945_verify_inst_sparse - verify runtime uCode image in card vs. host,
Zhu Yib481de92007-09-25 17:54:57 -07004679 * using sample data 100 bytes apart. If these sample points are good,
4680 * it's a pretty good bet that everything between them is good, too.
4681 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08004682static int iwl3945_verify_inst_sparse(struct iwl_priv *priv, __le32 *image, u32 len)
Zhu Yib481de92007-09-25 17:54:57 -07004683{
4684 u32 val;
4685 int rc = 0;
4686 u32 errcnt = 0;
4687 u32 i;
4688
4689 IWL_DEBUG_INFO("ucode inst image size is %u\n", len);
4690
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08004691 rc = iwl_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004692 if (rc)
4693 return rc;
4694
4695 for (i = 0; i < len; i += 100, image += 100/sizeof(u32)) {
4696 /* read data comes through single port, auto-incr addr */
4697 /* NOTE: Use the debugless read so we don't flood kernel log
4698 * if IWL_DL_IO is set */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08004699 iwl_write_direct32(priv, HBUS_TARG_MEM_RADDR,
Samuel Ortiz250bdd22008-12-19 10:37:11 +08004700 i + IWL39_RTC_INST_LOWER_BOUND);
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08004701 val = _iwl_read_direct32(priv, HBUS_TARG_MEM_RDAT);
Zhu Yib481de92007-09-25 17:54:57 -07004702 if (val != le32_to_cpu(*image)) {
4703#if 0 /* Enable this if you want to see details */
Winkler, Tomas15b16872008-12-19 10:37:33 +08004704 IWL_ERR(priv, "uCode INST section is invalid at "
Zhu Yib481de92007-09-25 17:54:57 -07004705 "offset 0x%x, is 0x%x, s/b 0x%x\n",
4706 i, val, *image);
4707#endif
4708 rc = -EIO;
4709 errcnt++;
4710 if (errcnt >= 3)
4711 break;
4712 }
4713 }
4714
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08004715 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004716
4717 return rc;
4718}
4719
4720
4721/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004722 * iwl3945_verify_ucode - determine which instruction image is in SRAM,
Zhu Yib481de92007-09-25 17:54:57 -07004723 * and verify its contents
4724 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08004725static int iwl3945_verify_ucode(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004726{
4727 __le32 *image;
4728 u32 len;
4729 int rc = 0;
4730
4731 /* Try bootstrap */
4732 image = (__le32 *)priv->ucode_boot.v_addr;
4733 len = priv->ucode_boot.len;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004734 rc = iwl3945_verify_inst_sparse(priv, image, len);
Zhu Yib481de92007-09-25 17:54:57 -07004735 if (rc == 0) {
4736 IWL_DEBUG_INFO("Bootstrap uCode is good in inst SRAM\n");
4737 return 0;
4738 }
4739
4740 /* Try initialize */
4741 image = (__le32 *)priv->ucode_init.v_addr;
4742 len = priv->ucode_init.len;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004743 rc = iwl3945_verify_inst_sparse(priv, image, len);
Zhu Yib481de92007-09-25 17:54:57 -07004744 if (rc == 0) {
4745 IWL_DEBUG_INFO("Initialize uCode is good in inst SRAM\n");
4746 return 0;
4747 }
4748
4749 /* Try runtime/protocol */
4750 image = (__le32 *)priv->ucode_code.v_addr;
4751 len = priv->ucode_code.len;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004752 rc = iwl3945_verify_inst_sparse(priv, image, len);
Zhu Yib481de92007-09-25 17:54:57 -07004753 if (rc == 0) {
4754 IWL_DEBUG_INFO("Runtime uCode is good in inst SRAM\n");
4755 return 0;
4756 }
4757
Winkler, Tomas15b16872008-12-19 10:37:33 +08004758 IWL_ERR(priv, "NO VALID UCODE IMAGE IN INSTRUCTION SRAM!!\n");
Zhu Yib481de92007-09-25 17:54:57 -07004759
Ben Cahill9fbab512007-11-29 11:09:47 +08004760 /* Since nothing seems to match, show first several data entries in
4761 * instruction SRAM, so maybe visual inspection will give a clue.
4762 * Selection of bootstrap image (vs. other images) is arbitrary. */
Zhu Yib481de92007-09-25 17:54:57 -07004763 image = (__le32 *)priv->ucode_boot.v_addr;
4764 len = priv->ucode_boot.len;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004765 rc = iwl3945_verify_inst_full(priv, image, len);
Zhu Yib481de92007-09-25 17:54:57 -07004766
4767 return rc;
4768}
4769
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08004770static void iwl3945_nic_start(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004771{
4772 /* Remove all resets to allow NIC to operate */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08004773 iwl_write32(priv, CSR_RESET, 0);
Zhu Yib481de92007-09-25 17:54:57 -07004774}
4775
4776/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004777 * iwl3945_read_ucode - Read uCode images from disk file.
Zhu Yib481de92007-09-25 17:54:57 -07004778 *
4779 * Copy into buffers for card to fetch via bus-mastering
4780 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08004781static int iwl3945_read_ucode(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004782{
Kolekar, Abhijeeta78fe752008-12-19 10:37:21 +08004783 struct iwl_ucode *ucode;
Reinette Chatrea0987a82008-12-02 12:14:06 -08004784 int ret = -EINVAL, index;
Zhu Yib481de92007-09-25 17:54:57 -07004785 const struct firmware *ucode_raw;
4786 /* firmware file name contains uCode/driver compatibility version */
Reinette Chatrea0987a82008-12-02 12:14:06 -08004787 const char *name_pre = priv->cfg->fw_name_pre;
4788 const unsigned int api_max = priv->cfg->ucode_api_max;
4789 const unsigned int api_min = priv->cfg->ucode_api_min;
4790 char buf[25];
Zhu Yib481de92007-09-25 17:54:57 -07004791 u8 *src;
4792 size_t len;
Reinette Chatrea0987a82008-12-02 12:14:06 -08004793 u32 api_ver, inst_size, data_size, init_size, init_data_size, boot_size;
Zhu Yib481de92007-09-25 17:54:57 -07004794
4795 /* Ask kernel firmware_class module to get the boot firmware off disk.
4796 * request_firmware() is synchronous, file is in memory on return. */
Reinette Chatrea0987a82008-12-02 12:14:06 -08004797 for (index = api_max; index >= api_min; index--) {
4798 sprintf(buf, "%s%u%s", name_pre, index, ".ucode");
4799 ret = request_firmware(&ucode_raw, buf, &priv->pci_dev->dev);
4800 if (ret < 0) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08004801 IWL_ERR(priv, "%s firmware file req failed: %d\n",
Reinette Chatrea0987a82008-12-02 12:14:06 -08004802 buf, ret);
4803 if (ret == -ENOENT)
4804 continue;
4805 else
4806 goto error;
4807 } else {
4808 if (index < api_max)
Winkler, Tomas15b16872008-12-19 10:37:33 +08004809 IWL_ERR(priv, "Loaded firmware %s, "
4810 "which is deprecated. "
4811 " Please use API v%u instead.\n",
Reinette Chatrea0987a82008-12-02 12:14:06 -08004812 buf, api_max);
4813 IWL_DEBUG_INFO("Got firmware '%s' file (%zd bytes) from disk\n",
4814 buf, ucode_raw->size);
4815 break;
4816 }
Zhu Yib481de92007-09-25 17:54:57 -07004817 }
4818
Reinette Chatrea0987a82008-12-02 12:14:06 -08004819 if (ret < 0)
4820 goto error;
Zhu Yib481de92007-09-25 17:54:57 -07004821
4822 /* Make sure that we got at least our header! */
4823 if (ucode_raw->size < sizeof(*ucode)) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08004824 IWL_ERR(priv, "File size way too small!\n");
Tomas Winkler90e759d2007-11-29 11:09:41 +08004825 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07004826 goto err_release;
4827 }
4828
4829 /* Data from ucode file: header followed by uCode images */
4830 ucode = (void *)ucode_raw->data;
4831
Chatre, Reinettec02b3ac2008-12-02 12:14:05 -08004832 priv->ucode_ver = le32_to_cpu(ucode->ver);
Reinette Chatrea0987a82008-12-02 12:14:06 -08004833 api_ver = IWL_UCODE_API(priv->ucode_ver);
Zhu Yib481de92007-09-25 17:54:57 -07004834 inst_size = le32_to_cpu(ucode->inst_size);
4835 data_size = le32_to_cpu(ucode->data_size);
4836 init_size = le32_to_cpu(ucode->init_size);
4837 init_data_size = le32_to_cpu(ucode->init_data_size);
4838 boot_size = le32_to_cpu(ucode->boot_size);
4839
Reinette Chatrea0987a82008-12-02 12:14:06 -08004840 /* api_ver should match the api version forming part of the
4841 * firmware filename ... but we don't check for that and only rely
4842 * on the API version read from firware header from here on forward */
4843
4844 if (api_ver < api_min || api_ver > api_max) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08004845 IWL_ERR(priv, "Driver unable to support your firmware API. "
Reinette Chatrea0987a82008-12-02 12:14:06 -08004846 "Driver supports v%u, firmware is v%u.\n",
4847 api_max, api_ver);
4848 priv->ucode_ver = 0;
4849 ret = -EINVAL;
4850 goto err_release;
4851 }
4852 if (api_ver != api_max)
Winkler, Tomas15b16872008-12-19 10:37:33 +08004853 IWL_ERR(priv, "Firmware has old API version. Expected %u, "
Reinette Chatrea0987a82008-12-02 12:14:06 -08004854 "got %u. New firmware can be obtained "
4855 "from http://www.intellinuxwireless.org.\n",
4856 api_max, api_ver);
4857
Tomas Winkler978785a2008-12-19 10:37:31 +08004858 IWL_INFO(priv, "loaded firmware version %u.%u.%u.%u\n",
4859 IWL_UCODE_MAJOR(priv->ucode_ver),
4860 IWL_UCODE_MINOR(priv->ucode_ver),
4861 IWL_UCODE_API(priv->ucode_ver),
4862 IWL_UCODE_SERIAL(priv->ucode_ver));
4863
Reinette Chatrea0987a82008-12-02 12:14:06 -08004864 IWL_DEBUG_INFO("f/w package hdr ucode version raw = 0x%x\n",
4865 priv->ucode_ver);
Ian Schrambc434dd2007-10-25 17:15:29 +08004866 IWL_DEBUG_INFO("f/w package hdr runtime inst size = %u\n", inst_size);
4867 IWL_DEBUG_INFO("f/w package hdr runtime data size = %u\n", data_size);
4868 IWL_DEBUG_INFO("f/w package hdr init inst size = %u\n", init_size);
4869 IWL_DEBUG_INFO("f/w package hdr init data size = %u\n", init_data_size);
4870 IWL_DEBUG_INFO("f/w package hdr boot inst size = %u\n", boot_size);
Zhu Yib481de92007-09-25 17:54:57 -07004871
Reinette Chatrea0987a82008-12-02 12:14:06 -08004872
Zhu Yib481de92007-09-25 17:54:57 -07004873 /* Verify size of file vs. image size info in file's header */
4874 if (ucode_raw->size < sizeof(*ucode) +
4875 inst_size + data_size + init_size +
4876 init_data_size + boot_size) {
4877
4878 IWL_DEBUG_INFO("uCode file size %d too small\n",
4879 (int)ucode_raw->size);
Tomas Winkler90e759d2007-11-29 11:09:41 +08004880 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07004881 goto err_release;
4882 }
4883
4884 /* Verify that uCode images will fit in card's SRAM */
Samuel Ortiz250bdd22008-12-19 10:37:11 +08004885 if (inst_size > IWL39_MAX_INST_SIZE) {
Tomas Winkler90e759d2007-11-29 11:09:41 +08004886 IWL_DEBUG_INFO("uCode instr len %d too large to fit in\n",
4887 inst_size);
4888 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07004889 goto err_release;
4890 }
4891
Samuel Ortiz250bdd22008-12-19 10:37:11 +08004892 if (data_size > IWL39_MAX_DATA_SIZE) {
Tomas Winkler90e759d2007-11-29 11:09:41 +08004893 IWL_DEBUG_INFO("uCode data len %d too large to fit in\n",
4894 data_size);
4895 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07004896 goto err_release;
4897 }
Samuel Ortiz250bdd22008-12-19 10:37:11 +08004898 if (init_size > IWL39_MAX_INST_SIZE) {
Tomas Winkler90e759d2007-11-29 11:09:41 +08004899 IWL_DEBUG_INFO("uCode init instr len %d too large to fit in\n",
4900 init_size);
4901 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07004902 goto err_release;
4903 }
Samuel Ortiz250bdd22008-12-19 10:37:11 +08004904 if (init_data_size > IWL39_MAX_DATA_SIZE) {
Tomas Winkler90e759d2007-11-29 11:09:41 +08004905 IWL_DEBUG_INFO("uCode init data len %d too large to fit in\n",
4906 init_data_size);
4907 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07004908 goto err_release;
4909 }
Samuel Ortiz250bdd22008-12-19 10:37:11 +08004910 if (boot_size > IWL39_MAX_BSM_SIZE) {
Tomas Winkler90e759d2007-11-29 11:09:41 +08004911 IWL_DEBUG_INFO("uCode boot instr len %d too large to fit in\n",
4912 boot_size);
4913 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07004914 goto err_release;
4915 }
4916
4917 /* Allocate ucode buffers for card's bus-master loading ... */
4918
4919 /* Runtime instructions and 2 copies of data:
4920 * 1) unmodified from disk
4921 * 2) backup cache for save/restore during power-downs */
4922 priv->ucode_code.len = inst_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08004923 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_code);
Zhu Yib481de92007-09-25 17:54:57 -07004924
4925 priv->ucode_data.len = data_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08004926 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_data);
Zhu Yib481de92007-09-25 17:54:57 -07004927
4928 priv->ucode_data_backup.len = data_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08004929 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_data_backup);
Zhu Yib481de92007-09-25 17:54:57 -07004930
4931 if (!priv->ucode_code.v_addr || !priv->ucode_data.v_addr ||
Tomas Winkler90e759d2007-11-29 11:09:41 +08004932 !priv->ucode_data_backup.v_addr)
Zhu Yib481de92007-09-25 17:54:57 -07004933 goto err_pci_alloc;
4934
Tomas Winkler90e759d2007-11-29 11:09:41 +08004935 /* Initialization instructions and data */
4936 if (init_size && init_data_size) {
4937 priv->ucode_init.len = init_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08004938 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_init);
Tomas Winkler90e759d2007-11-29 11:09:41 +08004939
4940 priv->ucode_init_data.len = init_data_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08004941 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_init_data);
Tomas Winkler90e759d2007-11-29 11:09:41 +08004942
4943 if (!priv->ucode_init.v_addr || !priv->ucode_init_data.v_addr)
4944 goto err_pci_alloc;
4945 }
4946
4947 /* Bootstrap (instructions only, no data) */
4948 if (boot_size) {
4949 priv->ucode_boot.len = boot_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08004950 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_boot);
Tomas Winkler90e759d2007-11-29 11:09:41 +08004951
4952 if (!priv->ucode_boot.v_addr)
4953 goto err_pci_alloc;
4954 }
4955
Zhu Yib481de92007-09-25 17:54:57 -07004956 /* Copy images into buffers for card's bus-master reads ... */
4957
4958 /* Runtime instructions (first block of data in file) */
4959 src = &ucode->data[0];
4960 len = priv->ucode_code.len;
Tomas Winkler90e759d2007-11-29 11:09:41 +08004961 IWL_DEBUG_INFO("Copying (but not loading) uCode instr len %Zd\n", len);
Zhu Yib481de92007-09-25 17:54:57 -07004962 memcpy(priv->ucode_code.v_addr, src, len);
4963 IWL_DEBUG_INFO("uCode instr buf vaddr = 0x%p, paddr = 0x%08x\n",
4964 priv->ucode_code.v_addr, (u32)priv->ucode_code.p_addr);
4965
4966 /* Runtime data (2nd block)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004967 * NOTE: Copy into backup buffer will be done in iwl3945_up() */
Zhu Yib481de92007-09-25 17:54:57 -07004968 src = &ucode->data[inst_size];
4969 len = priv->ucode_data.len;
Tomas Winkler90e759d2007-11-29 11:09:41 +08004970 IWL_DEBUG_INFO("Copying (but not loading) uCode data len %Zd\n", len);
Zhu Yib481de92007-09-25 17:54:57 -07004971 memcpy(priv->ucode_data.v_addr, src, len);
4972 memcpy(priv->ucode_data_backup.v_addr, src, len);
4973
4974 /* Initialization instructions (3rd block) */
4975 if (init_size) {
4976 src = &ucode->data[inst_size + data_size];
4977 len = priv->ucode_init.len;
Tomas Winkler90e759d2007-11-29 11:09:41 +08004978 IWL_DEBUG_INFO("Copying (but not loading) init instr len %Zd\n",
4979 len);
Zhu Yib481de92007-09-25 17:54:57 -07004980 memcpy(priv->ucode_init.v_addr, src, len);
4981 }
4982
4983 /* Initialization data (4th block) */
4984 if (init_data_size) {
4985 src = &ucode->data[inst_size + data_size + init_size];
4986 len = priv->ucode_init_data.len;
4987 IWL_DEBUG_INFO("Copying (but not loading) init data len %d\n",
4988 (int)len);
4989 memcpy(priv->ucode_init_data.v_addr, src, len);
4990 }
4991
4992 /* Bootstrap instructions (5th block) */
4993 src = &ucode->data[inst_size + data_size + init_size + init_data_size];
4994 len = priv->ucode_boot.len;
4995 IWL_DEBUG_INFO("Copying (but not loading) boot instr len %d\n",
4996 (int)len);
4997 memcpy(priv->ucode_boot.v_addr, src, len);
4998
4999 /* We have our copies now, allow OS release its copies */
5000 release_firmware(ucode_raw);
5001 return 0;
5002
5003 err_pci_alloc:
Winkler, Tomas15b16872008-12-19 10:37:33 +08005004 IWL_ERR(priv, "failed to allocate pci memory\n");
Tomas Winkler90e759d2007-11-29 11:09:41 +08005005 ret = -ENOMEM;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005006 iwl3945_dealloc_ucode_pci(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005007
5008 err_release:
5009 release_firmware(ucode_raw);
5010
5011 error:
Tomas Winkler90e759d2007-11-29 11:09:41 +08005012 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07005013}
5014
5015
5016/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005017 * iwl3945_set_ucode_ptrs - Set uCode address location
Zhu Yib481de92007-09-25 17:54:57 -07005018 *
5019 * Tell initialization uCode where to find runtime uCode.
5020 *
5021 * BSM registers initially contain pointers to initialization uCode.
5022 * We need to replace them to load runtime uCode inst and data,
5023 * and to save runtime data when powering down.
5024 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08005025static int iwl3945_set_ucode_ptrs(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005026{
5027 dma_addr_t pinst;
5028 dma_addr_t pdata;
5029 int rc = 0;
5030 unsigned long flags;
5031
5032 /* bits 31:0 for 3945 */
5033 pinst = priv->ucode_code.p_addr;
5034 pdata = priv->ucode_data_backup.p_addr;
5035
5036 spin_lock_irqsave(&priv->lock, flags);
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08005037 rc = iwl_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005038 if (rc) {
5039 spin_unlock_irqrestore(&priv->lock, flags);
5040 return rc;
5041 }
5042
5043 /* Tell bootstrap uCode where to find image to load */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08005044 iwl_write_prph(priv, BSM_DRAM_INST_PTR_REG, pinst);
5045 iwl_write_prph(priv, BSM_DRAM_DATA_PTR_REG, pdata);
5046 iwl_write_prph(priv, BSM_DRAM_DATA_BYTECOUNT_REG,
Zhu Yib481de92007-09-25 17:54:57 -07005047 priv->ucode_data.len);
5048
Tomas Winklera96a27f2008-10-23 23:48:56 -07005049 /* Inst byte count must be last to set up, bit 31 signals uCode
Zhu Yib481de92007-09-25 17:54:57 -07005050 * that all new ptr/size info is in place */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08005051 iwl_write_prph(priv, BSM_DRAM_INST_BYTECOUNT_REG,
Zhu Yib481de92007-09-25 17:54:57 -07005052 priv->ucode_code.len | BSM_DRAM_INST_LOAD);
5053
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08005054 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005055
5056 spin_unlock_irqrestore(&priv->lock, flags);
5057
5058 IWL_DEBUG_INFO("Runtime uCode pointers are set.\n");
5059
5060 return rc;
5061}
5062
5063/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005064 * iwl3945_init_alive_start - Called after REPLY_ALIVE notification received
Zhu Yib481de92007-09-25 17:54:57 -07005065 *
5066 * Called after REPLY_ALIVE notification received from "initialize" uCode.
5067 *
Zhu Yib481de92007-09-25 17:54:57 -07005068 * Tell "initialize" uCode to go ahead and load the runtime uCode.
Ben Cahill9fbab512007-11-29 11:09:47 +08005069 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08005070static void iwl3945_init_alive_start(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005071{
5072 /* Check alive response for "valid" sign from uCode */
5073 if (priv->card_alive_init.is_valid != UCODE_VALID_OK) {
5074 /* We had an error bringing up the hardware, so take it
5075 * all the way back down so we can try again */
5076 IWL_DEBUG_INFO("Initialize Alive failed.\n");
5077 goto restart;
5078 }
5079
5080 /* Bootstrap uCode has loaded initialize uCode ... verify inst image.
5081 * This is a paranoid check, because we would not have gotten the
5082 * "initialize" alive if code weren't properly loaded. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005083 if (iwl3945_verify_ucode(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07005084 /* Runtime instruction load was bad;
5085 * take it all the way back down so we can try again */
5086 IWL_DEBUG_INFO("Bad \"initialize\" uCode load.\n");
5087 goto restart;
5088 }
5089
5090 /* Send pointers to protocol/runtime uCode image ... init code will
5091 * load and launch runtime uCode, which will send us another "Alive"
5092 * notification. */
5093 IWL_DEBUG_INFO("Initialization Alive received.\n");
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005094 if (iwl3945_set_ucode_ptrs(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07005095 /* Runtime instruction load won't happen;
5096 * take it all the way back down so we can try again */
5097 IWL_DEBUG_INFO("Couldn't set up uCode pointers.\n");
5098 goto restart;
5099 }
5100 return;
5101
5102 restart:
5103 queue_work(priv->workqueue, &priv->restart);
5104}
5105
5106
Mohamed Abbas9bdf5ec2008-11-07 09:58:35 -08005107/* temporary */
5108static int iwl3945_mac_beacon_update(struct ieee80211_hw *hw,
5109 struct sk_buff *skb);
5110
Zhu Yib481de92007-09-25 17:54:57 -07005111/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005112 * iwl3945_alive_start - called after REPLY_ALIVE notification received
Zhu Yib481de92007-09-25 17:54:57 -07005113 * from protocol/runtime uCode (initialization uCode's
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005114 * Alive gets handled by iwl3945_init_alive_start()).
Zhu Yib481de92007-09-25 17:54:57 -07005115 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08005116static void iwl3945_alive_start(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005117{
5118 int rc = 0;
5119 int thermal_spin = 0;
5120 u32 rfkill;
5121
5122 IWL_DEBUG_INFO("Runtime Alive received.\n");
5123
5124 if (priv->card_alive.is_valid != UCODE_VALID_OK) {
5125 /* We had an error bringing up the hardware, so take it
5126 * all the way back down so we can try again */
5127 IWL_DEBUG_INFO("Alive failed.\n");
5128 goto restart;
5129 }
5130
5131 /* Initialize uCode has loaded Runtime uCode ... verify inst image.
5132 * This is a paranoid check, because we would not have gotten the
5133 * "runtime" alive if code weren't properly loaded. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005134 if (iwl3945_verify_ucode(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07005135 /* Runtime instruction load was bad;
5136 * take it all the way back down so we can try again */
5137 IWL_DEBUG_INFO("Bad runtime uCode load.\n");
5138 goto restart;
5139 }
5140
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005141 iwl3945_clear_stations_table(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005142
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08005143 rc = iwl_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005144 if (rc) {
Winkler, Tomas39aadf82008-12-19 10:37:32 +08005145 IWL_WARN(priv, "Can not read RFKILL status from adapter\n");
Zhu Yib481de92007-09-25 17:54:57 -07005146 return;
5147 }
5148
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08005149 rfkill = iwl_read_prph(priv, APMG_RFKILL_REG);
Zhu Yib481de92007-09-25 17:54:57 -07005150 IWL_DEBUG_INFO("RFKILL status: 0x%x\n", rfkill);
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08005151 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005152
5153 if (rfkill & 0x1) {
5154 clear_bit(STATUS_RF_KILL_HW, &priv->status);
Tomas Winklera96a27f2008-10-23 23:48:56 -07005155 /* if RFKILL is not on, then wait for thermal
Zhu Yib481de92007-09-25 17:54:57 -07005156 * sensor in adapter to kick in */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005157 while (iwl3945_hw_get_temperature(priv) == 0) {
Zhu Yib481de92007-09-25 17:54:57 -07005158 thermal_spin++;
5159 udelay(10);
5160 }
5161
5162 if (thermal_spin)
5163 IWL_DEBUG_INFO("Thermal calibration took %dus\n",
5164 thermal_spin * 10);
5165 } else
5166 set_bit(STATUS_RF_KILL_HW, &priv->status);
5167
Ben Cahill9fbab512007-11-29 11:09:47 +08005168 /* After the ALIVE response, we can send commands to 3945 uCode */
Zhu Yib481de92007-09-25 17:54:57 -07005169 set_bit(STATUS_ALIVE, &priv->status);
5170
5171 /* Clear out the uCode error bit if it is set */
5172 clear_bit(STATUS_FW_ERROR, &priv->status);
5173
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08005174 if (iwl_is_rfkill(priv))
Zhu Yib481de92007-09-25 17:54:57 -07005175 return;
5176
Johannes Berg36d68252008-05-15 12:55:26 +02005177 ieee80211_wake_queues(priv->hw);
Zhu Yib481de92007-09-25 17:54:57 -07005178
5179 priv->active_rate = priv->rates_mask;
5180 priv->active_rate_basic = priv->rates_mask & IWL_BASIC_RATES_MASK;
5181
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005182 iwl3945_send_power_mode(priv, IWL_POWER_LEVEL(priv->power_mode));
Zhu Yib481de92007-09-25 17:54:57 -07005183
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005184 if (iwl3945_is_associated(priv)) {
5185 struct iwl3945_rxon_cmd *active_rxon =
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08005186 (struct iwl3945_rxon_cmd *)(&priv->active39_rxon);
Zhu Yib481de92007-09-25 17:54:57 -07005187
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08005188 memcpy(&priv->staging39_rxon, &priv->active39_rxon,
5189 sizeof(priv->staging39_rxon));
Zhu Yib481de92007-09-25 17:54:57 -07005190 active_rxon->filter_flags &= ~RXON_FILTER_ASSOC_MSK;
5191 } else {
5192 /* Initialize our rx_config data */
Zhu, Yi60294de2008-10-29 14:05:45 -07005193 iwl3945_connection_init_rx_config(priv, priv->iw_mode);
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08005194 memcpy(priv->staging39_rxon.node_addr, priv->mac_addr, ETH_ALEN);
Zhu Yib481de92007-09-25 17:54:57 -07005195 }
5196
Ben Cahill9fbab512007-11-29 11:09:47 +08005197 /* Configure Bluetooth device coexistence support */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005198 iwl3945_send_bt_config(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005199
5200 /* Configure the adapter for unassociated operation */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005201 iwl3945_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005202
Zhu Yib481de92007-09-25 17:54:57 -07005203 iwl3945_reg_txpower_periodic(priv);
5204
Reinette Chatrefe00b5a2008-04-03 16:05:23 -07005205 iwl3945_led_register(priv);
5206
Zhu Yib481de92007-09-25 17:54:57 -07005207 IWL_DEBUG_INFO("ALIVE processing complete.\n");
Rick Farringtona9f46782008-03-18 14:57:49 -07005208 set_bit(STATUS_READY, &priv->status);
Zhu Yi5a669262008-01-14 17:46:18 -08005209 wake_up_interruptible(&priv->wait_command_queue);
Zhu Yib481de92007-09-25 17:54:57 -07005210
5211 if (priv->error_recovering)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005212 iwl3945_error_recovery(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005213
Mohamed Abbas9bdf5ec2008-11-07 09:58:35 -08005214 /* reassociate for ADHOC mode */
5215 if (priv->vif && (priv->iw_mode == NL80211_IFTYPE_ADHOC)) {
5216 struct sk_buff *beacon = ieee80211_beacon_get(priv->hw,
5217 priv->vif);
5218 if (beacon)
5219 iwl3945_mac_beacon_update(priv->hw, beacon);
5220 }
5221
Zhu Yib481de92007-09-25 17:54:57 -07005222 return;
5223
5224 restart:
5225 queue_work(priv->workqueue, &priv->restart);
5226}
5227
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08005228static void iwl3945_cancel_deferred_work(struct iwl_priv *priv);
Zhu Yib481de92007-09-25 17:54:57 -07005229
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08005230static void __iwl3945_down(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005231{
5232 unsigned long flags;
5233 int exit_pending = test_bit(STATUS_EXIT_PENDING, &priv->status);
5234 struct ieee80211_conf *conf = NULL;
5235
5236 IWL_DEBUG_INFO(DRV_NAME " is going down\n");
5237
5238 conf = ieee80211_get_hw_conf(priv->hw);
5239
5240 if (!exit_pending)
5241 set_bit(STATUS_EXIT_PENDING, &priv->status);
5242
Mohamed Abbasab53d8a2008-03-25 16:33:36 -07005243 iwl3945_led_unregister(priv);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005244 iwl3945_clear_stations_table(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005245
5246 /* Unblock any waiting calls */
5247 wake_up_interruptible_all(&priv->wait_command_queue);
5248
Zhu Yib481de92007-09-25 17:54:57 -07005249 /* Wipe out the EXIT_PENDING status bit if we are not actually
5250 * exiting the module */
5251 if (!exit_pending)
5252 clear_bit(STATUS_EXIT_PENDING, &priv->status);
5253
5254 /* stop and reset the on-board processor */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08005255 iwl_write32(priv, CSR_RESET, CSR_RESET_REG_FLAG_NEVO_RESET);
Zhu Yib481de92007-09-25 17:54:57 -07005256
5257 /* tell the device to stop sending interrupts */
Mohamed Abbas0359fac2008-03-28 16:21:08 -07005258 spin_lock_irqsave(&priv->lock, flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005259 iwl3945_disable_interrupts(priv);
Mohamed Abbas0359fac2008-03-28 16:21:08 -07005260 spin_unlock_irqrestore(&priv->lock, flags);
5261 iwl_synchronize_irq(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005262
5263 if (priv->mac80211_registered)
5264 ieee80211_stop_queues(priv->hw);
5265
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005266 /* If we have not previously called iwl3945_init() then
Zhu Yib481de92007-09-25 17:54:57 -07005267 * clear all bits but the RF Kill and SUSPEND bits and return */
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08005268 if (!iwl_is_init(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07005269 priv->status = test_bit(STATUS_RF_KILL_HW, &priv->status) <<
5270 STATUS_RF_KILL_HW |
5271 test_bit(STATUS_RF_KILL_SW, &priv->status) <<
5272 STATUS_RF_KILL_SW |
Reinette Chatre97888642008-02-06 11:20:38 -08005273 test_bit(STATUS_GEO_CONFIGURED, &priv->status) <<
5274 STATUS_GEO_CONFIGURED |
Zhu Yib481de92007-09-25 17:54:57 -07005275 test_bit(STATUS_IN_SUSPEND, &priv->status) <<
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08005276 STATUS_IN_SUSPEND |
5277 test_bit(STATUS_EXIT_PENDING, &priv->status) <<
5278 STATUS_EXIT_PENDING;
Zhu Yib481de92007-09-25 17:54:57 -07005279 goto exit;
5280 }
5281
5282 /* ...otherwise clear out all the status bits but the RF Kill and
5283 * SUSPEND bits and continue taking the NIC down. */
5284 priv->status &= test_bit(STATUS_RF_KILL_HW, &priv->status) <<
5285 STATUS_RF_KILL_HW |
5286 test_bit(STATUS_RF_KILL_SW, &priv->status) <<
5287 STATUS_RF_KILL_SW |
Reinette Chatre97888642008-02-06 11:20:38 -08005288 test_bit(STATUS_GEO_CONFIGURED, &priv->status) <<
5289 STATUS_GEO_CONFIGURED |
Zhu Yib481de92007-09-25 17:54:57 -07005290 test_bit(STATUS_IN_SUSPEND, &priv->status) <<
5291 STATUS_IN_SUSPEND |
5292 test_bit(STATUS_FW_ERROR, &priv->status) <<
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08005293 STATUS_FW_ERROR |
5294 test_bit(STATUS_EXIT_PENDING, &priv->status) <<
5295 STATUS_EXIT_PENDING;
Zhu Yib481de92007-09-25 17:54:57 -07005296
5297 spin_lock_irqsave(&priv->lock, flags);
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08005298 iwl_clear_bit(priv, CSR_GP_CNTRL, CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
Zhu Yib481de92007-09-25 17:54:57 -07005299 spin_unlock_irqrestore(&priv->lock, flags);
5300
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005301 iwl3945_hw_txq_ctx_stop(priv);
5302 iwl3945_hw_rxq_stop(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005303
5304 spin_lock_irqsave(&priv->lock, flags);
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08005305 if (!iwl_grab_nic_access(priv)) {
5306 iwl_write_prph(priv, APMG_CLK_DIS_REG,
Zhu Yib481de92007-09-25 17:54:57 -07005307 APMG_CLK_VAL_DMA_CLK_RQT);
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08005308 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005309 }
5310 spin_unlock_irqrestore(&priv->lock, flags);
5311
5312 udelay(5);
5313
Kolekar, Abhijeet01ec6162008-12-19 10:37:38 +08005314 priv->cfg->ops->lib->apm_ops.reset(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005315 exit:
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08005316 memset(&priv->card_alive, 0, sizeof(struct iwl_alive_resp));
Zhu Yib481de92007-09-25 17:54:57 -07005317
5318 if (priv->ibss_beacon)
5319 dev_kfree_skb(priv->ibss_beacon);
5320 priv->ibss_beacon = NULL;
5321
5322 /* clear out any free frames */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005323 iwl3945_clear_free_frames(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005324}
5325
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08005326static void iwl3945_down(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005327{
5328 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005329 __iwl3945_down(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005330 mutex_unlock(&priv->mutex);
Zhu Yib24d22b2007-12-19 13:59:52 +08005331
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005332 iwl3945_cancel_deferred_work(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005333}
5334
5335#define MAX_HW_RESTARTS 5
5336
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08005337static int __iwl3945_up(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005338{
5339 int rc, i;
5340
5341 if (test_bit(STATUS_EXIT_PENDING, &priv->status)) {
Winkler, Tomas39aadf82008-12-19 10:37:32 +08005342 IWL_WARN(priv, "Exit pending; will not bring the NIC up\n");
Zhu Yib481de92007-09-25 17:54:57 -07005343 return -EIO;
5344 }
5345
5346 if (test_bit(STATUS_RF_KILL_SW, &priv->status)) {
Winkler, Tomas39aadf82008-12-19 10:37:32 +08005347 IWL_WARN(priv, "Radio disabled by SW RF kill (module "
Zhu Yib481de92007-09-25 17:54:57 -07005348 "parameter)\n");
Zhu Yie655b9f2008-01-24 02:19:38 -08005349 return -ENODEV;
5350 }
5351
Reinette Chatree903fbd2008-01-30 22:05:15 -08005352 if (!priv->ucode_data_backup.v_addr || !priv->ucode_data.v_addr) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08005353 IWL_ERR(priv, "ucode not available for device bring up\n");
Reinette Chatree903fbd2008-01-30 22:05:15 -08005354 return -EIO;
5355 }
5356
Zhu Yie655b9f2008-01-24 02:19:38 -08005357 /* If platform's RF_KILL switch is NOT set to KILL */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08005358 if (iwl_read32(priv, CSR_GP_CNTRL) &
Zhu Yie655b9f2008-01-24 02:19:38 -08005359 CSR_GP_CNTRL_REG_FLAG_HW_RF_KILL_SW)
5360 clear_bit(STATUS_RF_KILL_HW, &priv->status);
5361 else {
5362 set_bit(STATUS_RF_KILL_HW, &priv->status);
5363 if (!test_bit(STATUS_IN_SUSPEND, &priv->status)) {
Winkler, Tomas39aadf82008-12-19 10:37:32 +08005364 IWL_WARN(priv, "Radio disabled by HW RF Kill switch\n");
Zhu Yie655b9f2008-01-24 02:19:38 -08005365 return -ENODEV;
5366 }
Zhu Yib481de92007-09-25 17:54:57 -07005367 }
Adel Gadllah80fcc9e2008-07-01 17:49:50 +02005368
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08005369 iwl_write32(priv, CSR_INT, 0xFFFFFFFF);
Zhu Yib481de92007-09-25 17:54:57 -07005370
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005371 rc = iwl3945_hw_nic_init(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005372 if (rc) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08005373 IWL_ERR(priv, "Unable to int nic\n");
Zhu Yib481de92007-09-25 17:54:57 -07005374 return rc;
5375 }
5376
5377 /* make sure rfkill handshake bits are cleared */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08005378 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
5379 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR,
Zhu Yib481de92007-09-25 17:54:57 -07005380 CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
5381
5382 /* clear (again), then enable host interrupts */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08005383 iwl_write32(priv, CSR_INT, 0xFFFFFFFF);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005384 iwl3945_enable_interrupts(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005385
5386 /* really make sure rfkill handshake bits are cleared */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08005387 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
5388 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
Zhu Yib481de92007-09-25 17:54:57 -07005389
5390 /* Copy original ucode data image from disk into backup cache.
5391 * This will be used to initialize the on-board processor's
5392 * data SRAM for a clean start when the runtime program first loads. */
5393 memcpy(priv->ucode_data_backup.v_addr, priv->ucode_data.v_addr,
Zhu Yi5a669262008-01-14 17:46:18 -08005394 priv->ucode_data.len);
Zhu Yib481de92007-09-25 17:54:57 -07005395
Zhu Yie655b9f2008-01-24 02:19:38 -08005396 /* We return success when we resume from suspend and rf_kill is on. */
5397 if (test_bit(STATUS_RF_KILL_HW, &priv->status))
5398 return 0;
5399
Zhu Yib481de92007-09-25 17:54:57 -07005400 for (i = 0; i < MAX_HW_RESTARTS; i++) {
5401
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005402 iwl3945_clear_stations_table(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005403
5404 /* load bootstrap state machine,
5405 * load bootstrap program into processor's memory,
5406 * prepare to load the "initialize" uCode */
Kolekar, Abhijeet0164b9b2008-12-19 10:37:37 +08005407 priv->cfg->ops->lib->load_ucode(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005408
5409 if (rc) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08005410 IWL_ERR(priv,
5411 "Unable to set up bootstrap uCode: %d\n", rc);
Zhu Yib481de92007-09-25 17:54:57 -07005412 continue;
5413 }
5414
5415 /* start card; "initialize" will load runtime ucode */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005416 iwl3945_nic_start(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005417
Zhu Yib481de92007-09-25 17:54:57 -07005418 IWL_DEBUG_INFO(DRV_NAME " is coming up\n");
5419
5420 return 0;
5421 }
5422
5423 set_bit(STATUS_EXIT_PENDING, &priv->status);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005424 __iwl3945_down(priv);
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08005425 clear_bit(STATUS_EXIT_PENDING, &priv->status);
Zhu Yib481de92007-09-25 17:54:57 -07005426
5427 /* tried to restart and config the device for as long as our
5428 * patience could withstand */
Winkler, Tomas15b16872008-12-19 10:37:33 +08005429 IWL_ERR(priv, "Unable to initialize device after %d attempts.\n", i);
Zhu Yib481de92007-09-25 17:54:57 -07005430 return -EIO;
5431}
5432
5433
5434/*****************************************************************************
5435 *
5436 * Workqueue callbacks
5437 *
5438 *****************************************************************************/
5439
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005440static void iwl3945_bg_init_alive_start(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07005441{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08005442 struct iwl_priv *priv =
5443 container_of(data, struct iwl_priv, init_alive_start.work);
Zhu Yib481de92007-09-25 17:54:57 -07005444
5445 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
5446 return;
5447
5448 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005449 iwl3945_init_alive_start(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005450 mutex_unlock(&priv->mutex);
5451}
5452
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005453static void iwl3945_bg_alive_start(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07005454{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08005455 struct iwl_priv *priv =
5456 container_of(data, struct iwl_priv, alive_start.work);
Zhu Yib481de92007-09-25 17:54:57 -07005457
5458 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
5459 return;
5460
5461 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005462 iwl3945_alive_start(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005463 mutex_unlock(&priv->mutex);
5464}
5465
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005466static void iwl3945_bg_rf_kill(struct work_struct *work)
Zhu Yib481de92007-09-25 17:54:57 -07005467{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08005468 struct iwl_priv *priv = container_of(work, struct iwl_priv, rf_kill);
Zhu Yib481de92007-09-25 17:54:57 -07005469
5470 wake_up_interruptible(&priv->wait_command_queue);
5471
5472 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
5473 return;
5474
5475 mutex_lock(&priv->mutex);
5476
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08005477 if (!iwl_is_rfkill(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07005478 IWL_DEBUG(IWL_DL_INFO | IWL_DL_RF_KILL,
5479 "HW and/or SW RF Kill no longer active, restarting "
5480 "device\n");
5481 if (!test_bit(STATUS_EXIT_PENDING, &priv->status))
5482 queue_work(priv->workqueue, &priv->restart);
5483 } else {
5484
5485 if (!test_bit(STATUS_RF_KILL_HW, &priv->status))
5486 IWL_DEBUG_RF_KILL("Can not turn radio back on - "
5487 "disabled by SW switch\n");
5488 else
Winkler, Tomas39aadf82008-12-19 10:37:32 +08005489 IWL_WARN(priv, "Radio Frequency Kill Switch is On:\n"
Zhu Yib481de92007-09-25 17:54:57 -07005490 "Kill switch must be turned off for "
5491 "wireless networking to work.\n");
5492 }
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08005493
Zhu Yib481de92007-09-25 17:54:57 -07005494 mutex_unlock(&priv->mutex);
Adel Gadllah80fcc9e2008-07-01 17:49:50 +02005495 iwl3945_rfkill_set_hw_state(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005496}
5497
5498#define IWL_SCAN_CHECK_WATCHDOG (7 * HZ)
5499
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005500static void iwl3945_bg_scan_check(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07005501{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08005502 struct iwl_priv *priv =
5503 container_of(data, struct iwl_priv, scan_check.work);
Zhu Yib481de92007-09-25 17:54:57 -07005504
5505 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
5506 return;
5507
5508 mutex_lock(&priv->mutex);
5509 if (test_bit(STATUS_SCANNING, &priv->status) ||
5510 test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
5511 IWL_DEBUG(IWL_DL_INFO | IWL_DL_SCAN,
5512 "Scan completion watchdog resetting adapter (%dms)\n",
5513 jiffies_to_msecs(IWL_SCAN_CHECK_WATCHDOG));
Mohamed Abbas15e869d2007-10-25 17:15:46 +08005514
Zhu Yib481de92007-09-25 17:54:57 -07005515 if (!test_bit(STATUS_EXIT_PENDING, &priv->status))
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005516 iwl3945_send_scan_abort(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005517 }
5518 mutex_unlock(&priv->mutex);
5519}
5520
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005521static void iwl3945_bg_request_scan(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07005522{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08005523 struct iwl_priv *priv =
5524 container_of(data, struct iwl_priv, request_scan);
Winkler, Tomasc2d79b42008-12-19 10:37:34 +08005525 struct iwl_host_cmd cmd = {
Zhu Yib481de92007-09-25 17:54:57 -07005526 .id = REPLY_SCAN_CMD,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005527 .len = sizeof(struct iwl3945_scan_cmd),
Zhu Yib481de92007-09-25 17:54:57 -07005528 .meta.flags = CMD_SIZE_HUGE,
5529 };
5530 int rc = 0;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005531 struct iwl3945_scan_cmd *scan;
Zhu Yib481de92007-09-25 17:54:57 -07005532 struct ieee80211_conf *conf = NULL;
Abhijeet Kolekarf9340522008-09-03 11:26:58 +08005533 u8 n_probes = 2;
Johannes Berg8318d782008-01-24 19:38:38 +01005534 enum ieee80211_band band;
John W. Linville9387b7c2008-09-30 20:59:05 -04005535 DECLARE_SSID_BUF(ssid);
Zhu Yib481de92007-09-25 17:54:57 -07005536
5537 conf = ieee80211_get_hw_conf(priv->hw);
5538
5539 mutex_lock(&priv->mutex);
5540
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08005541 if (!iwl_is_ready(priv)) {
Winkler, Tomas39aadf82008-12-19 10:37:32 +08005542 IWL_WARN(priv, "request scan called when driver not ready.\n");
Zhu Yib481de92007-09-25 17:54:57 -07005543 goto done;
5544 }
5545
Tomas Winklera96a27f2008-10-23 23:48:56 -07005546 /* Make sure the scan wasn't canceled before this queued work
Zhu Yib481de92007-09-25 17:54:57 -07005547 * was given the chance to run... */
5548 if (!test_bit(STATUS_SCANNING, &priv->status))
5549 goto done;
5550
5551 /* This should never be called or scheduled if there is currently
5552 * a scan active in the hardware. */
5553 if (test_bit(STATUS_SCAN_HW, &priv->status)) {
5554 IWL_DEBUG_INFO("Multiple concurrent scan requests in parallel. "
5555 "Ignoring second request.\n");
5556 rc = -EIO;
5557 goto done;
5558 }
5559
5560 if (test_bit(STATUS_EXIT_PENDING, &priv->status)) {
5561 IWL_DEBUG_SCAN("Aborting scan due to device shutdown\n");
5562 goto done;
5563 }
5564
5565 if (test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
5566 IWL_DEBUG_HC("Scan request while abort pending. Queuing.\n");
5567 goto done;
5568 }
5569
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08005570 if (iwl_is_rfkill(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07005571 IWL_DEBUG_HC("Aborting scan due to RF Kill activation\n");
5572 goto done;
5573 }
5574
5575 if (!test_bit(STATUS_READY, &priv->status)) {
5576 IWL_DEBUG_HC("Scan request while uninitialized. Queuing.\n");
5577 goto done;
5578 }
5579
5580 if (!priv->scan_bands) {
5581 IWL_DEBUG_HC("Aborting scan due to no requested bands\n");
5582 goto done;
5583 }
5584
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08005585 if (!priv->scan39) {
5586 priv->scan39 = kmalloc(sizeof(struct iwl3945_scan_cmd) +
Zhu Yib481de92007-09-25 17:54:57 -07005587 IWL_MAX_SCAN_SIZE, GFP_KERNEL);
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08005588 if (!priv->scan39) {
Zhu Yib481de92007-09-25 17:54:57 -07005589 rc = -ENOMEM;
5590 goto done;
5591 }
5592 }
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08005593 scan = priv->scan39;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005594 memset(scan, 0, sizeof(struct iwl3945_scan_cmd) + IWL_MAX_SCAN_SIZE);
Zhu Yib481de92007-09-25 17:54:57 -07005595
5596 scan->quiet_plcp_th = IWL_PLCP_QUIET_THRESH;
5597 scan->quiet_time = IWL_ACTIVE_QUIET_TIME;
5598
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005599 if (iwl3945_is_associated(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07005600 u16 interval = 0;
5601 u32 extra;
5602 u32 suspend_time = 100;
5603 u32 scan_suspend_time = 100;
5604 unsigned long flags;
5605
5606 IWL_DEBUG_INFO("Scanning while associated...\n");
5607
5608 spin_lock_irqsave(&priv->lock, flags);
5609 interval = priv->beacon_int;
5610 spin_unlock_irqrestore(&priv->lock, flags);
5611
5612 scan->suspend_time = 0;
Mohamed Abbas15e869d2007-10-25 17:15:46 +08005613 scan->max_out_time = cpu_to_le32(200 * 1024);
Zhu Yib481de92007-09-25 17:54:57 -07005614 if (!interval)
5615 interval = suspend_time;
5616 /*
5617 * suspend time format:
5618 * 0-19: beacon interval in usec (time before exec.)
5619 * 20-23: 0
5620 * 24-31: number of beacons (suspend between channels)
5621 */
5622
5623 extra = (suspend_time / interval) << 24;
5624 scan_suspend_time = 0xFF0FFFFF &
5625 (extra | ((suspend_time % interval) * 1024));
5626
5627 scan->suspend_time = cpu_to_le32(scan_suspend_time);
5628 IWL_DEBUG_SCAN("suspend_time 0x%X beacon interval %d\n",
5629 scan_suspend_time, interval);
5630 }
5631
5632 /* We should add the ability for user to lock to PASSIVE ONLY */
5633 if (priv->one_direct_scan) {
5634 IWL_DEBUG_SCAN
5635 ("Kicking off one direct scan for '%s'\n",
John W. Linville9387b7c2008-09-30 20:59:05 -04005636 print_ssid(ssid, priv->direct_ssid,
5637 priv->direct_ssid_len));
Zhu Yib481de92007-09-25 17:54:57 -07005638 scan->direct_scan[0].id = WLAN_EID_SSID;
5639 scan->direct_scan[0].len = priv->direct_ssid_len;
5640 memcpy(scan->direct_scan[0].ssid,
5641 priv->direct_ssid, priv->direct_ssid_len);
Abhijeet Kolekarf9340522008-09-03 11:26:58 +08005642 n_probes++;
Abhijeet Kolekarf9340522008-09-03 11:26:58 +08005643 } else
Bill Moss786b4552008-04-17 16:03:40 -07005644 IWL_DEBUG_SCAN("Kicking off one indirect scan.\n");
Zhu Yib481de92007-09-25 17:54:57 -07005645
5646 /* We don't build a direct scan probe request; the uCode will do
5647 * that based on the direct_mask added to each channel entry */
5648 scan->tx_cmd.len = cpu_to_le16(
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005649 iwl3945_fill_probe_req(priv, (struct ieee80211_mgmt *)scan->data,
Johannes Berg430cfe92008-10-28 18:06:02 +01005650 IWL_MAX_SCAN_SIZE - sizeof(*scan)));
Zhu Yib481de92007-09-25 17:54:57 -07005651 scan->tx_cmd.tx_flags = TX_CMD_FLG_SEQ_CTL_MSK;
Abhijeet Kolekar3832ec92008-12-19 10:37:26 +08005652 scan->tx_cmd.sta_id = priv->hw_params.bcast_sta_id;
Zhu Yib481de92007-09-25 17:54:57 -07005653 scan->tx_cmd.stop_time.life_time = TX_CMD_LIFE_TIME_INFINITE;
5654
5655 /* flags + rate selection */
5656
Ron Rindjunsky66b50042008-06-25 16:46:31 +08005657 if (priv->scan_bands & BIT(IEEE80211_BAND_2GHZ)) {
Zhu Yib481de92007-09-25 17:54:57 -07005658 scan->flags = RXON_FLG_BAND_24G_MSK | RXON_FLG_AUTO_DETECT_MSK;
5659 scan->tx_cmd.rate = IWL_RATE_1M_PLCP;
5660 scan->good_CRC_th = 0;
Johannes Berg8318d782008-01-24 19:38:38 +01005661 band = IEEE80211_BAND_2GHZ;
Ron Rindjunsky66b50042008-06-25 16:46:31 +08005662 } else if (priv->scan_bands & BIT(IEEE80211_BAND_5GHZ)) {
Zhu Yib481de92007-09-25 17:54:57 -07005663 scan->tx_cmd.rate = IWL_RATE_6M_PLCP;
5664 scan->good_CRC_th = IWL_GOOD_CRC_TH;
Johannes Berg8318d782008-01-24 19:38:38 +01005665 band = IEEE80211_BAND_5GHZ;
Ron Rindjunsky66b50042008-06-25 16:46:31 +08005666 } else {
Winkler, Tomas39aadf82008-12-19 10:37:32 +08005667 IWL_WARN(priv, "Invalid scan band count\n");
Zhu Yib481de92007-09-25 17:54:57 -07005668 goto done;
5669 }
5670
5671 /* select Rx antennas */
5672 scan->flags |= iwl3945_get_antenna_flags(priv);
5673
Johannes Berg05c914f2008-09-11 00:01:58 +02005674 if (priv->iw_mode == NL80211_IFTYPE_MONITOR)
Zhu Yib481de92007-09-25 17:54:57 -07005675 scan->filter_flags = RXON_FILTER_PROMISC_MSK;
5676
Abhijeet Kolekarf9340522008-09-03 11:26:58 +08005677 scan->channel_count =
5678 iwl3945_get_channels_for_scan(priv, band, 1, /* active */
5679 n_probes,
5680 (void *)&scan->data[le16_to_cpu(scan->tx_cmd.len)]);
Zhu Yib481de92007-09-25 17:54:57 -07005681
Reinette Chatre14b54332008-11-04 12:21:35 -08005682 if (scan->channel_count == 0) {
5683 IWL_DEBUG_SCAN("channel count %d\n", scan->channel_count);
5684 goto done;
5685 }
5686
Zhu Yib481de92007-09-25 17:54:57 -07005687 cmd.len += le16_to_cpu(scan->tx_cmd.len) +
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005688 scan->channel_count * sizeof(struct iwl3945_scan_channel);
Zhu Yib481de92007-09-25 17:54:57 -07005689 cmd.data = scan;
5690 scan->len = cpu_to_le16(cmd.len);
5691
5692 set_bit(STATUS_SCAN_HW, &priv->status);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005693 rc = iwl3945_send_cmd_sync(priv, &cmd);
Zhu Yib481de92007-09-25 17:54:57 -07005694 if (rc)
5695 goto done;
5696
5697 queue_delayed_work(priv->workqueue, &priv->scan_check,
5698 IWL_SCAN_CHECK_WATCHDOG);
5699
5700 mutex_unlock(&priv->mutex);
5701 return;
5702
5703 done:
Mohamed Abbas2420ebc2008-11-04 12:21:34 -08005704 /* can not perform scan make sure we clear scanning
5705 * bits from status so next scan request can be performed.
5706 * if we dont clear scanning status bit here all next scan
5707 * will fail
5708 */
5709 clear_bit(STATUS_SCAN_HW, &priv->status);
5710 clear_bit(STATUS_SCANNING, &priv->status);
5711
Ian Schram01ebd062007-10-25 17:15:22 +08005712 /* inform mac80211 scan aborted */
Zhu Yib481de92007-09-25 17:54:57 -07005713 queue_work(priv->workqueue, &priv->scan_completed);
5714 mutex_unlock(&priv->mutex);
5715}
5716
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005717static void iwl3945_bg_up(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07005718{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08005719 struct iwl_priv *priv = container_of(data, struct iwl_priv, up);
Zhu Yib481de92007-09-25 17:54:57 -07005720
5721 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
5722 return;
5723
5724 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005725 __iwl3945_up(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005726 mutex_unlock(&priv->mutex);
Adel Gadllah80fcc9e2008-07-01 17:49:50 +02005727 iwl3945_rfkill_set_hw_state(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005728}
5729
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005730static void iwl3945_bg_restart(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07005731{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08005732 struct iwl_priv *priv = container_of(data, struct iwl_priv, restart);
Zhu Yib481de92007-09-25 17:54:57 -07005733
5734 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
5735 return;
5736
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005737 iwl3945_down(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005738 queue_work(priv->workqueue, &priv->up);
5739}
5740
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005741static void iwl3945_bg_rx_replenish(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07005742{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08005743 struct iwl_priv *priv =
5744 container_of(data, struct iwl_priv, rx_replenish);
Zhu Yib481de92007-09-25 17:54:57 -07005745
5746 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
5747 return;
5748
5749 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005750 iwl3945_rx_replenish(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005751 mutex_unlock(&priv->mutex);
5752}
5753
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08005754#define IWL_DELAY_NEXT_SCAN (HZ*2)
5755
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08005756static void iwl3945_post_associate(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005757{
Zhu Yib481de92007-09-25 17:54:57 -07005758 int rc = 0;
5759 struct ieee80211_conf *conf = NULL;
5760
Johannes Berg05c914f2008-09-11 00:01:58 +02005761 if (priv->iw_mode == NL80211_IFTYPE_AP) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08005762 IWL_ERR(priv, "%s Should not be called in AP mode\n", __func__);
Zhu Yib481de92007-09-25 17:54:57 -07005763 return;
5764 }
5765
5766
Johannes Berge1749612008-10-27 15:59:26 -07005767 IWL_DEBUG_ASSOC("Associated as %d to: %pM\n",
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08005768 priv->assoc_id, priv->active39_rxon.bssid_addr);
Zhu Yib481de92007-09-25 17:54:57 -07005769
5770 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
5771 return;
5772
Abhijeet Kolekar322a9812008-09-03 11:26:27 +08005773 if (!priv->vif || !priv->is_open)
Mohamed Abbas6ef89d02007-10-25 17:15:47 +08005774 return;
Abhijeet Kolekar322a9812008-09-03 11:26:27 +08005775
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005776 iwl3945_scan_cancel_timeout(priv, 200);
Mohamed Abbas15e869d2007-10-25 17:15:46 +08005777
Zhu Yib481de92007-09-25 17:54:57 -07005778 conf = ieee80211_get_hw_conf(priv->hw);
5779
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08005780 priv->staging39_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005781 iwl3945_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005782
Tomas Winkler28afaf92008-12-19 10:37:06 +08005783 memset(&priv->rxon_timing, 0, sizeof(struct iwl_rxon_time_cmd));
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005784 iwl3945_setup_rxon_timing(priv);
5785 rc = iwl3945_send_cmd_pdu(priv, REPLY_RXON_TIMING,
Zhu Yib481de92007-09-25 17:54:57 -07005786 sizeof(priv->rxon_timing), &priv->rxon_timing);
5787 if (rc)
Winkler, Tomas39aadf82008-12-19 10:37:32 +08005788 IWL_WARN(priv, "REPLY_RXON_TIMING failed - "
Zhu Yib481de92007-09-25 17:54:57 -07005789 "Attempting to continue.\n");
5790
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08005791 priv->staging39_rxon.filter_flags |= RXON_FILTER_ASSOC_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07005792
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08005793 priv->staging39_rxon.assoc_id = cpu_to_le16(priv->assoc_id);
Zhu Yib481de92007-09-25 17:54:57 -07005794
5795 IWL_DEBUG_ASSOC("assoc id %d beacon interval %d\n",
5796 priv->assoc_id, priv->beacon_int);
5797
5798 if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_PREAMBLE)
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08005799 priv->staging39_rxon.flags |= RXON_FLG_SHORT_PREAMBLE_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07005800 else
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08005801 priv->staging39_rxon.flags &= ~RXON_FLG_SHORT_PREAMBLE_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07005802
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08005803 if (priv->staging39_rxon.flags & RXON_FLG_BAND_24G_MSK) {
Zhu Yib481de92007-09-25 17:54:57 -07005804 if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_SLOT_TIME)
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08005805 priv->staging39_rxon.flags |= RXON_FLG_SHORT_SLOT_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07005806 else
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08005807 priv->staging39_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07005808
Johannes Berg05c914f2008-09-11 00:01:58 +02005809 if (priv->iw_mode == NL80211_IFTYPE_ADHOC)
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08005810 priv->staging39_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07005811
5812 }
5813
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005814 iwl3945_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005815
5816 switch (priv->iw_mode) {
Johannes Berg05c914f2008-09-11 00:01:58 +02005817 case NL80211_IFTYPE_STATION:
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005818 iwl3945_rate_scale_init(priv->hw, IWL_AP_ID);
Zhu Yib481de92007-09-25 17:54:57 -07005819 break;
5820
Johannes Berg05c914f2008-09-11 00:01:58 +02005821 case NL80211_IFTYPE_ADHOC:
Zhu Yib481de92007-09-25 17:54:57 -07005822
Abhijeet Kolekarce546fd2008-11-19 15:32:22 -08005823 priv->assoc_id = 1;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005824 iwl3945_add_station(priv, priv->bssid, 0, 0);
Zhu Yib481de92007-09-25 17:54:57 -07005825 iwl3945_sync_sta(priv, IWL_STA_ID,
Johannes Berg8318d782008-01-24 19:38:38 +01005826 (priv->band == IEEE80211_BAND_5GHZ) ?
Zhu Yib481de92007-09-25 17:54:57 -07005827 IWL_RATE_6M_PLCP : IWL_RATE_1M_PLCP,
5828 CMD_ASYNC);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005829 iwl3945_rate_scale_init(priv->hw, IWL_STA_ID);
5830 iwl3945_send_beacon_cmd(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005831
5832 break;
5833
5834 default:
Winkler, Tomas15b16872008-12-19 10:37:33 +08005835 IWL_ERR(priv, "%s Should not be called in %d mode\n",
Tomas Winkler3ac7f142008-07-21 02:40:14 +03005836 __func__, priv->iw_mode);
Zhu Yib481de92007-09-25 17:54:57 -07005837 break;
5838 }
5839
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005840 iwl3945_activate_qos(priv, 0);
Ron Rindjunsky292ae172008-02-06 11:20:39 -08005841
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08005842 /* we have just associated, don't start scan too early */
5843 priv->next_scan_jiffies = jiffies + IWL_DELAY_NEXT_SCAN;
Abhijeet Kolekarcd56d332008-09-03 11:26:21 +08005844}
5845
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005846static void iwl3945_bg_abort_scan(struct work_struct *work)
Zhu Yib481de92007-09-25 17:54:57 -07005847{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08005848 struct iwl_priv *priv = container_of(work, struct iwl_priv, abort_scan);
Zhu Yib481de92007-09-25 17:54:57 -07005849
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08005850 if (!iwl_is_ready(priv))
Zhu Yib481de92007-09-25 17:54:57 -07005851 return;
5852
5853 mutex_lock(&priv->mutex);
5854
5855 set_bit(STATUS_SCAN_ABORTING, &priv->status);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005856 iwl3945_send_scan_abort(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005857
5858 mutex_unlock(&priv->mutex);
5859}
5860
Johannes Berge8975582008-10-09 12:18:51 +02005861static int iwl3945_mac_config(struct ieee80211_hw *hw, u32 changed);
Zhu Yi76bb77e2007-11-22 10:53:22 +08005862
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005863static void iwl3945_bg_scan_completed(struct work_struct *work)
Zhu Yib481de92007-09-25 17:54:57 -07005864{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08005865 struct iwl_priv *priv =
5866 container_of(work, struct iwl_priv, scan_completed);
Zhu Yib481de92007-09-25 17:54:57 -07005867
5868 IWL_DEBUG(IWL_DL_INFO | IWL_DL_SCAN, "SCAN complete scan\n");
5869
5870 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
5871 return;
5872
Zhu Yia0646472007-12-20 14:10:01 +08005873 if (test_bit(STATUS_CONF_PENDING, &priv->status))
Johannes Berge8975582008-10-09 12:18:51 +02005874 iwl3945_mac_config(priv->hw, 0);
Zhu Yi76bb77e2007-11-22 10:53:22 +08005875
Zhu Yib481de92007-09-25 17:54:57 -07005876 ieee80211_scan_completed(priv->hw);
5877
5878 /* Since setting the TXPOWER may have been deferred while
5879 * performing the scan, fire one off */
5880 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005881 iwl3945_hw_reg_send_txpower(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005882 mutex_unlock(&priv->mutex);
5883}
5884
5885/*****************************************************************************
5886 *
5887 * mac80211 entry point functions
5888 *
5889 *****************************************************************************/
5890
Zhu Yi5a669262008-01-14 17:46:18 -08005891#define UCODE_READY_TIMEOUT (2 * HZ)
5892
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005893static int iwl3945_mac_start(struct ieee80211_hw *hw)
Zhu Yib481de92007-09-25 17:54:57 -07005894{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08005895 struct iwl_priv *priv = hw->priv;
Zhu Yi5a669262008-01-14 17:46:18 -08005896 int ret;
Zhu Yib481de92007-09-25 17:54:57 -07005897
5898 IWL_DEBUG_MAC80211("enter\n");
5899
Zhu Yi5a669262008-01-14 17:46:18 -08005900 if (pci_enable_device(priv->pci_dev)) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08005901 IWL_ERR(priv, "Fail to pci_enable_device\n");
Zhu Yi5a669262008-01-14 17:46:18 -08005902 return -ENODEV;
5903 }
5904 pci_restore_state(priv->pci_dev);
5905 pci_enable_msi(priv->pci_dev);
5906
5907 ret = request_irq(priv->pci_dev->irq, iwl3945_isr, IRQF_SHARED,
5908 DRV_NAME, priv);
5909 if (ret) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08005910 IWL_ERR(priv, "Error allocating IRQ %d\n", priv->pci_dev->irq);
Zhu Yi5a669262008-01-14 17:46:18 -08005911 goto out_disable_msi;
5912 }
5913
Zhu Yib481de92007-09-25 17:54:57 -07005914 /* we should be verifying the device is ready to be opened */
5915 mutex_lock(&priv->mutex);
5916
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08005917 memset(&priv->staging39_rxon, 0, sizeof(struct iwl3945_rxon_cmd));
Zhu Yi5a669262008-01-14 17:46:18 -08005918 /* fetch ucode file from disk, alloc and copy to bus-master buffers ...
5919 * ucode filename and max sizes are card-specific. */
5920
5921 if (!priv->ucode_code.len) {
5922 ret = iwl3945_read_ucode(priv);
5923 if (ret) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08005924 IWL_ERR(priv, "Could not read microcode: %d\n", ret);
Zhu Yi5a669262008-01-14 17:46:18 -08005925 mutex_unlock(&priv->mutex);
5926 goto out_release_irq;
5927 }
5928 }
5929
Zhu Yie655b9f2008-01-24 02:19:38 -08005930 ret = __iwl3945_up(priv);
Zhu Yi5a669262008-01-14 17:46:18 -08005931
Zhu Yib481de92007-09-25 17:54:57 -07005932 mutex_unlock(&priv->mutex);
Zhu Yi5a669262008-01-14 17:46:18 -08005933
Adel Gadllah80fcc9e2008-07-01 17:49:50 +02005934 iwl3945_rfkill_set_hw_state(priv);
5935
Zhu Yie655b9f2008-01-24 02:19:38 -08005936 if (ret)
5937 goto out_release_irq;
5938
5939 IWL_DEBUG_INFO("Start UP work.\n");
5940
5941 if (test_bit(STATUS_IN_SUSPEND, &priv->status))
5942 return 0;
5943
Zhu Yi5a669262008-01-14 17:46:18 -08005944 /* Wait for START_ALIVE from ucode. Otherwise callbacks from
5945 * mac80211 will not be run successfully. */
5946 ret = wait_event_interruptible_timeout(priv->wait_command_queue,
5947 test_bit(STATUS_READY, &priv->status),
5948 UCODE_READY_TIMEOUT);
5949 if (!ret) {
5950 if (!test_bit(STATUS_READY, &priv->status)) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08005951 IWL_ERR(priv,
5952 "Wait for START_ALIVE timeout after %dms.\n",
5953 jiffies_to_msecs(UCODE_READY_TIMEOUT));
Zhu Yi5a669262008-01-14 17:46:18 -08005954 ret = -ETIMEDOUT;
5955 goto out_release_irq;
5956 }
5957 }
5958
Zhu Yie655b9f2008-01-24 02:19:38 -08005959 priv->is_open = 1;
Zhu Yib481de92007-09-25 17:54:57 -07005960 IWL_DEBUG_MAC80211("leave\n");
5961 return 0;
Zhu Yi5a669262008-01-14 17:46:18 -08005962
5963out_release_irq:
5964 free_irq(priv->pci_dev->irq, priv);
5965out_disable_msi:
5966 pci_disable_msi(priv->pci_dev);
Zhu Yie655b9f2008-01-24 02:19:38 -08005967 pci_disable_device(priv->pci_dev);
5968 priv->is_open = 0;
5969 IWL_DEBUG_MAC80211("leave - failed\n");
Zhu Yi5a669262008-01-14 17:46:18 -08005970 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07005971}
5972
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005973static void iwl3945_mac_stop(struct ieee80211_hw *hw)
Zhu Yib481de92007-09-25 17:54:57 -07005974{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08005975 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07005976
5977 IWL_DEBUG_MAC80211("enter\n");
Mohamed Abbas6ef89d02007-10-25 17:15:47 +08005978
Zhu Yie655b9f2008-01-24 02:19:38 -08005979 if (!priv->is_open) {
5980 IWL_DEBUG_MAC80211("leave - skip\n");
5981 return;
5982 }
5983
Zhu Yib481de92007-09-25 17:54:57 -07005984 priv->is_open = 0;
Zhu Yi5a669262008-01-14 17:46:18 -08005985
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08005986 if (iwl_is_ready_rf(priv)) {
Zhu Yie655b9f2008-01-24 02:19:38 -08005987 /* stop mac, cancel any scan request and clear
5988 * RXON_FILTER_ASSOC_MSK BIT
5989 */
Zhu Yi5a669262008-01-14 17:46:18 -08005990 mutex_lock(&priv->mutex);
5991 iwl3945_scan_cancel_timeout(priv, 100);
Mohamed Abbasfde35712007-11-29 11:10:15 +08005992 mutex_unlock(&priv->mutex);
Mohamed Abbasfde35712007-11-29 11:10:15 +08005993 }
5994
Zhu Yi5a669262008-01-14 17:46:18 -08005995 iwl3945_down(priv);
5996
5997 flush_workqueue(priv->workqueue);
5998 free_irq(priv->pci_dev->irq, priv);
5999 pci_disable_msi(priv->pci_dev);
6000 pci_save_state(priv->pci_dev);
6001 pci_disable_device(priv->pci_dev);
Mohamed Abbas6ef89d02007-10-25 17:15:47 +08006002
Zhu Yib481de92007-09-25 17:54:57 -07006003 IWL_DEBUG_MAC80211("leave\n");
Zhu Yib481de92007-09-25 17:54:57 -07006004}
6005
Johannes Berge039fa42008-05-15 12:55:29 +02006006static int iwl3945_mac_tx(struct ieee80211_hw *hw, struct sk_buff *skb)
Zhu Yib481de92007-09-25 17:54:57 -07006007{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08006008 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07006009
6010 IWL_DEBUG_MAC80211("enter\n");
6011
Zhu Yib481de92007-09-25 17:54:57 -07006012 IWL_DEBUG_TX("dev->xmit(%d bytes) at rate 0x%02x\n", skb->len,
Johannes Berge039fa42008-05-15 12:55:29 +02006013 ieee80211_get_tx_rate(hw, IEEE80211_SKB_CB(skb))->bitrate);
Zhu Yib481de92007-09-25 17:54:57 -07006014
Johannes Berge039fa42008-05-15 12:55:29 +02006015 if (iwl3945_tx_skb(priv, skb))
Zhu Yib481de92007-09-25 17:54:57 -07006016 dev_kfree_skb_any(skb);
6017
6018 IWL_DEBUG_MAC80211("leave\n");
Reinette Chatre637f8832009-01-19 15:30:32 -08006019 return NETDEV_TX_OK;
Zhu Yib481de92007-09-25 17:54:57 -07006020}
6021
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006022static int iwl3945_mac_add_interface(struct ieee80211_hw *hw,
Zhu Yib481de92007-09-25 17:54:57 -07006023 struct ieee80211_if_init_conf *conf)
6024{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08006025 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07006026 unsigned long flags;
6027
Johannes Berg32bfd352007-12-19 01:31:26 +01006028 IWL_DEBUG_MAC80211("enter: type %d\n", conf->type);
Zhu Yib481de92007-09-25 17:54:57 -07006029
Johannes Berg32bfd352007-12-19 01:31:26 +01006030 if (priv->vif) {
6031 IWL_DEBUG_MAC80211("leave - vif != NULL\n");
Tomas Winkler864792e2007-11-27 21:00:52 +02006032 return -EOPNOTSUPP;
Zhu Yib481de92007-09-25 17:54:57 -07006033 }
6034
6035 spin_lock_irqsave(&priv->lock, flags);
Johannes Berg32bfd352007-12-19 01:31:26 +01006036 priv->vif = conf->vif;
Zhu, Yi60294de2008-10-29 14:05:45 -07006037 priv->iw_mode = conf->type;
Zhu Yib481de92007-09-25 17:54:57 -07006038
6039 spin_unlock_irqrestore(&priv->lock, flags);
6040
6041 mutex_lock(&priv->mutex);
Tomas Winkler864792e2007-11-27 21:00:52 +02006042
6043 if (conf->mac_addr) {
Johannes Berge1749612008-10-27 15:59:26 -07006044 IWL_DEBUG_MAC80211("Set: %pM\n", conf->mac_addr);
Tomas Winkler864792e2007-11-27 21:00:52 +02006045 memcpy(priv->mac_addr, conf->mac_addr, ETH_ALEN);
6046 }
6047
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08006048 if (iwl_is_ready(priv))
Zhu Yi5a669262008-01-14 17:46:18 -08006049 iwl3945_set_mode(priv, conf->type);
Zhu Yib481de92007-09-25 17:54:57 -07006050
Zhu Yib481de92007-09-25 17:54:57 -07006051 mutex_unlock(&priv->mutex);
6052
Zhu Yi5a669262008-01-14 17:46:18 -08006053 IWL_DEBUG_MAC80211("leave\n");
Zhu Yib481de92007-09-25 17:54:57 -07006054 return 0;
6055}
6056
6057/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006058 * iwl3945_mac_config - mac80211 config callback
Zhu Yib481de92007-09-25 17:54:57 -07006059 *
6060 * We ignore conf->flags & IEEE80211_CONF_SHORT_SLOT_TIME since it seems to
6061 * be set inappropriately and the driver currently sets the hardware up to
6062 * use it whenever needed.
6063 */
Johannes Berge8975582008-10-09 12:18:51 +02006064static int iwl3945_mac_config(struct ieee80211_hw *hw, u32 changed)
Zhu Yib481de92007-09-25 17:54:57 -07006065{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08006066 struct iwl_priv *priv = hw->priv;
Samuel Ortizd20b3c62008-12-19 10:37:15 +08006067 const struct iwl_channel_info *ch_info;
Johannes Berge8975582008-10-09 12:18:51 +02006068 struct ieee80211_conf *conf = &hw->conf;
Zhu Yib481de92007-09-25 17:54:57 -07006069 unsigned long flags;
Zhu Yi76bb77e2007-11-22 10:53:22 +08006070 int ret = 0;
Zhu Yib481de92007-09-25 17:54:57 -07006071
6072 mutex_lock(&priv->mutex);
Johannes Berg8318d782008-01-24 19:38:38 +01006073 IWL_DEBUG_MAC80211("enter to channel %d\n", conf->channel->hw_value);
Zhu Yib481de92007-09-25 17:54:57 -07006074
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08006075 if (!iwl_is_ready(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07006076 IWL_DEBUG_MAC80211("leave - not ready\n");
Zhu Yi76bb77e2007-11-22 10:53:22 +08006077 ret = -EIO;
6078 goto out;
Zhu Yib481de92007-09-25 17:54:57 -07006079 }
6080
Kolekar, Abhijeetdf878d82008-12-19 10:37:35 +08006081 if (unlikely(!iwl3945_mod_params.disable_hw_scan &&
Zhu Yib481de92007-09-25 17:54:57 -07006082 test_bit(STATUS_SCANNING, &priv->status))) {
Zhu Yia0646472007-12-20 14:10:01 +08006083 IWL_DEBUG_MAC80211("leave - scanning\n");
6084 set_bit(STATUS_CONF_PENDING, &priv->status);
Zhu Yib481de92007-09-25 17:54:57 -07006085 mutex_unlock(&priv->mutex);
Zhu Yia0646472007-12-20 14:10:01 +08006086 return 0;
Zhu Yib481de92007-09-25 17:54:57 -07006087 }
6088
6089 spin_lock_irqsave(&priv->lock, flags);
6090
Johannes Berg8318d782008-01-24 19:38:38 +01006091 ch_info = iwl3945_get_channel_info(priv, conf->channel->band,
6092 conf->channel->hw_value);
Zhu Yib481de92007-09-25 17:54:57 -07006093 if (!is_channel_valid(ch_info)) {
Ron Rindjunsky66b50042008-06-25 16:46:31 +08006094 IWL_DEBUG_SCAN("Channel %d [%d] is INVALID for this band.\n",
Johannes Berg8318d782008-01-24 19:38:38 +01006095 conf->channel->hw_value, conf->channel->band);
Zhu Yib481de92007-09-25 17:54:57 -07006096 IWL_DEBUG_MAC80211("leave - invalid channel\n");
6097 spin_unlock_irqrestore(&priv->lock, flags);
Zhu Yi76bb77e2007-11-22 10:53:22 +08006098 ret = -EINVAL;
6099 goto out;
Zhu Yib481de92007-09-25 17:54:57 -07006100 }
6101
Johannes Berg8318d782008-01-24 19:38:38 +01006102 iwl3945_set_rxon_channel(priv, conf->channel->band, conf->channel->hw_value);
Zhu Yib481de92007-09-25 17:54:57 -07006103
Johannes Berg8318d782008-01-24 19:38:38 +01006104 iwl3945_set_flags_for_phymode(priv, conf->channel->band);
Zhu Yib481de92007-09-25 17:54:57 -07006105
6106 /* The list of supported rates and rate mask can be different
6107 * for each phymode; since the phymode may have changed, reset
6108 * the rate mask to what mac80211 lists */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006109 iwl3945_set_rate(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006110
6111 spin_unlock_irqrestore(&priv->lock, flags);
6112
6113#ifdef IEEE80211_CONF_CHANNEL_SWITCH
6114 if (conf->flags & IEEE80211_CONF_CHANNEL_SWITCH) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006115 iwl3945_hw_channel_switch(priv, conf->channel);
Zhu Yi76bb77e2007-11-22 10:53:22 +08006116 goto out;
Zhu Yib481de92007-09-25 17:54:57 -07006117 }
6118#endif
6119
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006120 iwl3945_radio_kill_sw(priv, !conf->radio_enabled);
Zhu Yib481de92007-09-25 17:54:57 -07006121
6122 if (!conf->radio_enabled) {
6123 IWL_DEBUG_MAC80211("leave - radio disabled\n");
Zhu Yi76bb77e2007-11-22 10:53:22 +08006124 goto out;
Zhu Yib481de92007-09-25 17:54:57 -07006125 }
6126
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08006127 if (iwl_is_rfkill(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07006128 IWL_DEBUG_MAC80211("leave - RF kill\n");
Zhu Yi76bb77e2007-11-22 10:53:22 +08006129 ret = -EIO;
6130 goto out;
Zhu Yib481de92007-09-25 17:54:57 -07006131 }
6132
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006133 iwl3945_set_rate(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006134
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08006135 if (memcmp(&priv->active39_rxon,
6136 &priv->staging39_rxon, sizeof(priv->staging39_rxon)))
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006137 iwl3945_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006138 else
6139 IWL_DEBUG_INFO("No re-sending same RXON configuration.\n");
6140
6141 IWL_DEBUG_MAC80211("leave\n");
6142
Zhu Yi76bb77e2007-11-22 10:53:22 +08006143out:
Zhu Yia0646472007-12-20 14:10:01 +08006144 clear_bit(STATUS_CONF_PENDING, &priv->status);
Zhu Yib481de92007-09-25 17:54:57 -07006145 mutex_unlock(&priv->mutex);
Zhu Yi76bb77e2007-11-22 10:53:22 +08006146 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07006147}
6148
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08006149static void iwl3945_config_ap(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07006150{
6151 int rc = 0;
6152
Maarten Lankhorstd986bcd2008-01-23 10:15:16 -08006153 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
Zhu Yib481de92007-09-25 17:54:57 -07006154 return;
6155
6156 /* The following should be done only at AP bring up */
Ron Rindjunsky5d1e2322008-06-30 17:23:04 +08006157 if (!(iwl3945_is_associated(priv))) {
Zhu Yib481de92007-09-25 17:54:57 -07006158
6159 /* RXON - unassoc (to set timing command) */
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08006160 priv->staging39_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006161 iwl3945_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006162
6163 /* RXON Timing */
Tomas Winkler28afaf92008-12-19 10:37:06 +08006164 memset(&priv->rxon_timing, 0, sizeof(struct iwl_rxon_time_cmd));
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006165 iwl3945_setup_rxon_timing(priv);
6166 rc = iwl3945_send_cmd_pdu(priv, REPLY_RXON_TIMING,
Zhu Yib481de92007-09-25 17:54:57 -07006167 sizeof(priv->rxon_timing), &priv->rxon_timing);
6168 if (rc)
Winkler, Tomas39aadf82008-12-19 10:37:32 +08006169 IWL_WARN(priv, "REPLY_RXON_TIMING failed - "
Zhu Yib481de92007-09-25 17:54:57 -07006170 "Attempting to continue.\n");
6171
6172 /* FIXME: what should be the assoc_id for AP? */
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08006173 priv->staging39_rxon.assoc_id = cpu_to_le16(priv->assoc_id);
Zhu Yib481de92007-09-25 17:54:57 -07006174 if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_PREAMBLE)
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08006175 priv->staging39_rxon.flags |=
Zhu Yib481de92007-09-25 17:54:57 -07006176 RXON_FLG_SHORT_PREAMBLE_MSK;
6177 else
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08006178 priv->staging39_rxon.flags &=
Zhu Yib481de92007-09-25 17:54:57 -07006179 ~RXON_FLG_SHORT_PREAMBLE_MSK;
6180
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08006181 if (priv->staging39_rxon.flags & RXON_FLG_BAND_24G_MSK) {
Zhu Yib481de92007-09-25 17:54:57 -07006182 if (priv->assoc_capability &
6183 WLAN_CAPABILITY_SHORT_SLOT_TIME)
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08006184 priv->staging39_rxon.flags |=
Zhu Yib481de92007-09-25 17:54:57 -07006185 RXON_FLG_SHORT_SLOT_MSK;
6186 else
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08006187 priv->staging39_rxon.flags &=
Zhu Yib481de92007-09-25 17:54:57 -07006188 ~RXON_FLG_SHORT_SLOT_MSK;
6189
Johannes Berg05c914f2008-09-11 00:01:58 +02006190 if (priv->iw_mode == NL80211_IFTYPE_ADHOC)
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08006191 priv->staging39_rxon.flags &=
Zhu Yib481de92007-09-25 17:54:57 -07006192 ~RXON_FLG_SHORT_SLOT_MSK;
6193 }
6194 /* restore RXON assoc */
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08006195 priv->staging39_rxon.filter_flags |= RXON_FILTER_ASSOC_MSK;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006196 iwl3945_commit_rxon(priv);
Kolekar, Abhijeetb5323d32008-12-19 10:37:22 +08006197 iwl3945_add_station(priv, iwl_bcast_addr, 0, 0);
Zhu Yi556f8db2007-09-27 11:27:33 +08006198 }
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006199 iwl3945_send_beacon_cmd(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006200
6201 /* FIXME - we need to add code here to detect a totally new
6202 * configuration, reset the AP, unassoc, rxon timing, assoc,
6203 * clear sta table, add BCAST sta... */
6204}
6205
Johannes Berg32bfd352007-12-19 01:31:26 +01006206static int iwl3945_mac_config_interface(struct ieee80211_hw *hw,
6207 struct ieee80211_vif *vif,
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08006208 struct ieee80211_if_conf *conf)
Zhu Yib481de92007-09-25 17:54:57 -07006209{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08006210 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07006211 int rc;
6212
6213 if (conf == NULL)
6214 return -EIO;
6215
Emmanuel Grumbachb716bb92008-03-04 18:09:32 -08006216 if (priv->vif != vif) {
6217 IWL_DEBUG_MAC80211("leave - priv->vif != vif\n");
Emmanuel Grumbachb716bb92008-03-04 18:09:32 -08006218 return 0;
6219 }
6220
Johannes Berg9d139c82008-07-09 14:40:37 +02006221 /* handle this temporarily here */
Johannes Berg05c914f2008-09-11 00:01:58 +02006222 if (priv->iw_mode == NL80211_IFTYPE_ADHOC &&
Johannes Berg9d139c82008-07-09 14:40:37 +02006223 conf->changed & IEEE80211_IFCC_BEACON) {
6224 struct sk_buff *beacon = ieee80211_beacon_get(hw, vif);
6225 if (!beacon)
6226 return -ENOMEM;
Mohamed Abbas9bdf5ec2008-11-07 09:58:35 -08006227 mutex_lock(&priv->mutex);
Johannes Berg9d139c82008-07-09 14:40:37 +02006228 rc = iwl3945_mac_beacon_update(hw, beacon);
Mohamed Abbas9bdf5ec2008-11-07 09:58:35 -08006229 mutex_unlock(&priv->mutex);
Johannes Berg9d139c82008-07-09 14:40:37 +02006230 if (rc)
6231 return rc;
6232 }
6233
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08006234 if (!iwl_is_alive(priv))
Zhu Yi5a669262008-01-14 17:46:18 -08006235 return -EAGAIN;
6236
Zhu Yib481de92007-09-25 17:54:57 -07006237 mutex_lock(&priv->mutex);
6238
Zhu Yib481de92007-09-25 17:54:57 -07006239 if (conf->bssid)
Johannes Berge1749612008-10-27 15:59:26 -07006240 IWL_DEBUG_MAC80211("bssid: %pM\n", conf->bssid);
Zhu Yib481de92007-09-25 17:54:57 -07006241
Johannes Berg4150c572007-09-17 01:29:23 -04006242/*
6243 * very dubious code was here; the probe filtering flag is never set:
6244 *
Zhu Yib481de92007-09-25 17:54:57 -07006245 if (unlikely(test_bit(STATUS_SCANNING, &priv->status)) &&
6246 !(priv->hw->flags & IEEE80211_HW_NO_PROBE_FILTERING)) {
Johannes Berg4150c572007-09-17 01:29:23 -04006247 */
Zhu Yib481de92007-09-25 17:54:57 -07006248
Johannes Berg05c914f2008-09-11 00:01:58 +02006249 if (priv->iw_mode == NL80211_IFTYPE_AP) {
Zhu Yib481de92007-09-25 17:54:57 -07006250 if (!conf->bssid) {
6251 conf->bssid = priv->mac_addr;
6252 memcpy(priv->bssid, priv->mac_addr, ETH_ALEN);
Johannes Berge1749612008-10-27 15:59:26 -07006253 IWL_DEBUG_MAC80211("bssid was set to: %pM\n",
6254 conf->bssid);
Zhu Yib481de92007-09-25 17:54:57 -07006255 }
6256 if (priv->ibss_beacon)
6257 dev_kfree_skb(priv->ibss_beacon);
6258
Johannes Berg9d139c82008-07-09 14:40:37 +02006259 priv->ibss_beacon = ieee80211_beacon_get(hw, vif);
Zhu Yib481de92007-09-25 17:54:57 -07006260 }
6261
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08006262 if (iwl_is_rfkill(priv))
Mohamed Abbasfde35712007-11-29 11:10:15 +08006263 goto done;
6264
Zhu Yib481de92007-09-25 17:54:57 -07006265 if (conf->bssid && !is_zero_ether_addr(conf->bssid) &&
6266 !is_multicast_ether_addr(conf->bssid)) {
6267 /* If there is currently a HW scan going on in the background
6268 * then we need to cancel it else the RXON below will fail. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006269 if (iwl3945_scan_cancel_timeout(priv, 100)) {
Winkler, Tomas39aadf82008-12-19 10:37:32 +08006270 IWL_WARN(priv, "Aborted scan still in progress "
Zhu Yib481de92007-09-25 17:54:57 -07006271 "after 100ms\n");
6272 IWL_DEBUG_MAC80211("leaving - scan abort failed.\n");
6273 mutex_unlock(&priv->mutex);
6274 return -EAGAIN;
6275 }
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08006276 memcpy(priv->staging39_rxon.bssid_addr, conf->bssid, ETH_ALEN);
Zhu Yib481de92007-09-25 17:54:57 -07006277
6278 /* TODO: Audit driver for usage of these members and see
6279 * if mac80211 deprecates them (priv->bssid looks like it
6280 * shouldn't be there, but I haven't scanned the IBSS code
6281 * to verify) - jpk */
6282 memcpy(priv->bssid, conf->bssid, ETH_ALEN);
6283
Johannes Berg05c914f2008-09-11 00:01:58 +02006284 if (priv->iw_mode == NL80211_IFTYPE_AP)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006285 iwl3945_config_ap(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006286 else {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006287 rc = iwl3945_commit_rxon(priv);
Johannes Berg05c914f2008-09-11 00:01:58 +02006288 if ((priv->iw_mode == NL80211_IFTYPE_STATION) && rc)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006289 iwl3945_add_station(priv,
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08006290 priv->active39_rxon.bssid_addr, 1, 0);
Zhu Yib481de92007-09-25 17:54:57 -07006291 }
6292
6293 } else {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006294 iwl3945_scan_cancel_timeout(priv, 100);
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08006295 priv->staging39_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006296 iwl3945_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006297 }
6298
Mohamed Abbasfde35712007-11-29 11:10:15 +08006299 done:
Zhu Yib481de92007-09-25 17:54:57 -07006300 IWL_DEBUG_MAC80211("leave\n");
6301 mutex_unlock(&priv->mutex);
6302
6303 return 0;
6304}
6305
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006306static void iwl3945_configure_filter(struct ieee80211_hw *hw,
Johannes Berg4150c572007-09-17 01:29:23 -04006307 unsigned int changed_flags,
6308 unsigned int *total_flags,
6309 int mc_count, struct dev_addr_list *mc_list)
6310{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08006311 struct iwl_priv *priv = hw->priv;
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08006312 __le32 *filter_flags = &priv->staging39_rxon.filter_flags;
Rick Farrington25b3f572008-06-30 17:23:28 +08006313
Zhu, Yi352bc8d2008-11-12 13:14:09 -08006314 IWL_DEBUG_MAC80211("Enter: changed: 0x%x, total: 0x%x\n",
6315 changed_flags, *total_flags);
6316
6317 if (changed_flags & (FIF_OTHER_BSS | FIF_PROMISC_IN_BSS)) {
6318 if (*total_flags & (FIF_OTHER_BSS | FIF_PROMISC_IN_BSS))
6319 *filter_flags |= RXON_FILTER_PROMISC_MSK;
6320 else
6321 *filter_flags &= ~RXON_FILTER_PROMISC_MSK;
Abhijeet Kolekar5ec03972008-05-05 10:22:48 +08006322 }
Zhu, Yi352bc8d2008-11-12 13:14:09 -08006323 if (changed_flags & FIF_ALLMULTI) {
6324 if (*total_flags & FIF_ALLMULTI)
6325 *filter_flags |= RXON_FILTER_ACCEPT_GRP_MSK;
6326 else
6327 *filter_flags &= ~RXON_FILTER_ACCEPT_GRP_MSK;
6328 }
6329 if (changed_flags & FIF_CONTROL) {
6330 if (*total_flags & FIF_CONTROL)
6331 *filter_flags |= RXON_FILTER_CTL2HOST_MSK;
6332 else
6333 *filter_flags &= ~RXON_FILTER_CTL2HOST_MSK;
6334 }
6335 if (changed_flags & FIF_BCN_PRBRESP_PROMISC) {
6336 if (*total_flags & FIF_BCN_PRBRESP_PROMISC)
6337 *filter_flags |= RXON_FILTER_BCON_AWARE_MSK;
6338 else
6339 *filter_flags &= ~RXON_FILTER_BCON_AWARE_MSK;
6340 }
6341
6342 /* We avoid iwl_commit_rxon here to commit the new filter flags
6343 * since mac80211 will call ieee80211_hw_config immediately.
6344 * (mc_list is not supported at this time). Otherwise, we need to
6345 * queue a background iwl_commit_rxon work.
6346 */
6347
6348 *total_flags &= FIF_OTHER_BSS | FIF_ALLMULTI | FIF_PROMISC_IN_BSS |
Rick Farrington25b3f572008-06-30 17:23:28 +08006349 FIF_BCN_PRBRESP_PROMISC | FIF_CONTROL;
Johannes Berg4150c572007-09-17 01:29:23 -04006350}
6351
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006352static void iwl3945_mac_remove_interface(struct ieee80211_hw *hw,
Zhu Yib481de92007-09-25 17:54:57 -07006353 struct ieee80211_if_init_conf *conf)
6354{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08006355 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07006356
6357 IWL_DEBUG_MAC80211("enter\n");
6358
6359 mutex_lock(&priv->mutex);
Mohamed Abbas6ef89d02007-10-25 17:15:47 +08006360
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08006361 if (iwl_is_ready_rf(priv)) {
Mohamed Abbasfde35712007-11-29 11:10:15 +08006362 iwl3945_scan_cancel_timeout(priv, 100);
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08006363 priv->staging39_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
Mohamed Abbasfde35712007-11-29 11:10:15 +08006364 iwl3945_commit_rxon(priv);
6365 }
Johannes Berg32bfd352007-12-19 01:31:26 +01006366 if (priv->vif == conf->vif) {
6367 priv->vif = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07006368 memset(priv->bssid, 0, ETH_ALEN);
Zhu Yib481de92007-09-25 17:54:57 -07006369 }
6370 mutex_unlock(&priv->mutex);
6371
6372 IWL_DEBUG_MAC80211("leave\n");
Zhu Yib481de92007-09-25 17:54:57 -07006373}
6374
Abhijeet Kolekarcd56d332008-09-03 11:26:21 +08006375#define IWL_DELAY_NEXT_SCAN_AFTER_ASSOC (HZ*6)
6376
6377static void iwl3945_bss_info_changed(struct ieee80211_hw *hw,
6378 struct ieee80211_vif *vif,
6379 struct ieee80211_bss_conf *bss_conf,
6380 u32 changes)
6381{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08006382 struct iwl_priv *priv = hw->priv;
Abhijeet Kolekarcd56d332008-09-03 11:26:21 +08006383
6384 IWL_DEBUG_MAC80211("changes = 0x%X\n", changes);
6385
6386 if (changes & BSS_CHANGED_ERP_PREAMBLE) {
6387 IWL_DEBUG_MAC80211("ERP_PREAMBLE %d\n",
6388 bss_conf->use_short_preamble);
6389 if (bss_conf->use_short_preamble)
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08006390 priv->staging39_rxon.flags |= RXON_FLG_SHORT_PREAMBLE_MSK;
Abhijeet Kolekarcd56d332008-09-03 11:26:21 +08006391 else
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08006392 priv->staging39_rxon.flags &= ~RXON_FLG_SHORT_PREAMBLE_MSK;
Abhijeet Kolekarcd56d332008-09-03 11:26:21 +08006393 }
6394
6395 if (changes & BSS_CHANGED_ERP_CTS_PROT) {
6396 IWL_DEBUG_MAC80211("ERP_CTS %d\n", bss_conf->use_cts_prot);
6397 if (bss_conf->use_cts_prot && (priv->band != IEEE80211_BAND_5GHZ))
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08006398 priv->staging39_rxon.flags |= RXON_FLG_TGG_PROTECT_MSK;
Abhijeet Kolekarcd56d332008-09-03 11:26:21 +08006399 else
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08006400 priv->staging39_rxon.flags &= ~RXON_FLG_TGG_PROTECT_MSK;
Abhijeet Kolekarcd56d332008-09-03 11:26:21 +08006401 }
6402
6403 if (changes & BSS_CHANGED_ASSOC) {
6404 IWL_DEBUG_MAC80211("ASSOC %d\n", bss_conf->assoc);
6405 /* This should never happen as this function should
6406 * never be called from interrupt context. */
6407 if (WARN_ON_ONCE(in_interrupt()))
6408 return;
6409 if (bss_conf->assoc) {
6410 priv->assoc_id = bss_conf->aid;
6411 priv->beacon_int = bss_conf->beacon_int;
Tomas Winkler28afaf92008-12-19 10:37:06 +08006412 priv->timestamp = bss_conf->timestamp;
Abhijeet Kolekarcd56d332008-09-03 11:26:21 +08006413 priv->assoc_capability = bss_conf->assoc_capability;
6414 priv->next_scan_jiffies = jiffies +
6415 IWL_DELAY_NEXT_SCAN_AFTER_ASSOC;
6416 mutex_lock(&priv->mutex);
6417 iwl3945_post_associate(priv);
6418 mutex_unlock(&priv->mutex);
6419 } else {
6420 priv->assoc_id = 0;
6421 IWL_DEBUG_MAC80211("DISASSOC %d\n", bss_conf->assoc);
6422 }
6423 } else if (changes && iwl3945_is_associated(priv) && priv->assoc_id) {
6424 IWL_DEBUG_MAC80211("Associated Changes %d\n", changes);
6425 iwl3945_send_rxon_assoc(priv);
6426 }
6427
6428}
6429
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006430static int iwl3945_mac_hw_scan(struct ieee80211_hw *hw, u8 *ssid, size_t len)
Zhu Yib481de92007-09-25 17:54:57 -07006431{
6432 int rc = 0;
6433 unsigned long flags;
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08006434 struct iwl_priv *priv = hw->priv;
John W. Linville9387b7c2008-09-30 20:59:05 -04006435 DECLARE_SSID_BUF(ssid_buf);
Zhu Yib481de92007-09-25 17:54:57 -07006436
6437 IWL_DEBUG_MAC80211("enter\n");
6438
Mohamed Abbas15e869d2007-10-25 17:15:46 +08006439 mutex_lock(&priv->mutex);
Zhu Yib481de92007-09-25 17:54:57 -07006440 spin_lock_irqsave(&priv->lock, flags);
6441
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08006442 if (!iwl_is_ready_rf(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07006443 rc = -EIO;
6444 IWL_DEBUG_MAC80211("leave - not ready or exit pending\n");
6445 goto out_unlock;
6446 }
6447
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08006448 /* we don't schedule scan within next_scan_jiffies period */
6449 if (priv->next_scan_jiffies &&
6450 time_after(priv->next_scan_jiffies, jiffies)) {
6451 rc = -EAGAIN;
6452 goto out_unlock;
6453 }
Bill Moss15dbf1b2008-05-06 11:05:15 +08006454 /* if we just finished scan ask for delay for a broadcast scan */
6455 if ((len == 0) && priv->last_scan_jiffies &&
6456 time_after(priv->last_scan_jiffies + IWL_DELAY_NEXT_SCAN,
6457 jiffies)) {
Zhu Yib481de92007-09-25 17:54:57 -07006458 rc = -EAGAIN;
6459 goto out_unlock;
6460 }
6461 if (len) {
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08006462 IWL_DEBUG_SCAN("direct scan for %s [%d]\n ",
John W. Linville9387b7c2008-09-30 20:59:05 -04006463 print_ssid(ssid_buf, ssid, len), (int)len);
Zhu Yib481de92007-09-25 17:54:57 -07006464
6465 priv->one_direct_scan = 1;
6466 priv->direct_ssid_len = (u8)
6467 min((u8) len, (u8) IW_ESSID_MAX_SIZE);
6468 memcpy(priv->direct_ssid, ssid, priv->direct_ssid_len);
Mohamed Abbas6ef89d02007-10-25 17:15:47 +08006469 } else
6470 priv->one_direct_scan = 0;
Zhu Yib481de92007-09-25 17:54:57 -07006471
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006472 rc = iwl3945_scan_initiate(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006473
6474 IWL_DEBUG_MAC80211("leave\n");
6475
6476out_unlock:
6477 spin_unlock_irqrestore(&priv->lock, flags);
Mohamed Abbas15e869d2007-10-25 17:15:46 +08006478 mutex_unlock(&priv->mutex);
Zhu Yib481de92007-09-25 17:54:57 -07006479
6480 return rc;
6481}
6482
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006483static int iwl3945_mac_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
Johannes Bergdc822b52008-12-29 12:55:09 +01006484 struct ieee80211_vif *vif,
6485 struct ieee80211_sta *sta,
6486 struct ieee80211_key_conf *key)
Zhu Yib481de92007-09-25 17:54:57 -07006487{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08006488 struct iwl_priv *priv = hw->priv;
Johannes Bergdc822b52008-12-29 12:55:09 +01006489 const u8 *addr;
Zhu Yib481de92007-09-25 17:54:57 -07006490 int rc = 0;
6491 u8 sta_id;
Johannes Bergdc822b52008-12-29 12:55:09 +01006492 static const u8 bcast_addr[ETH_ALEN] =
6493 { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
Zhu Yib481de92007-09-25 17:54:57 -07006494
6495 IWL_DEBUG_MAC80211("enter\n");
6496
Kolekar, Abhijeetdf878d82008-12-19 10:37:35 +08006497 if (iwl3945_mod_params.sw_crypto) {
Zhu Yib481de92007-09-25 17:54:57 -07006498 IWL_DEBUG_MAC80211("leave - hwcrypto disabled\n");
6499 return -EOPNOTSUPP;
6500 }
6501
Johannes Bergdc822b52008-12-29 12:55:09 +01006502 addr = sta ? sta->addr : bcast_addr;
Zhu Yib481de92007-09-25 17:54:57 -07006503
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006504 sta_id = iwl3945_hw_find_station(priv, addr);
Zhu Yib481de92007-09-25 17:54:57 -07006505 if (sta_id == IWL_INVALID_STATION) {
Johannes Berge1749612008-10-27 15:59:26 -07006506 IWL_DEBUG_MAC80211("leave - %pM not in station map.\n",
6507 addr);
Zhu Yib481de92007-09-25 17:54:57 -07006508 return -EINVAL;
6509 }
6510
6511 mutex_lock(&priv->mutex);
6512
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006513 iwl3945_scan_cancel_timeout(priv, 100);
Mohamed Abbas15e869d2007-10-25 17:15:46 +08006514
Zhu Yib481de92007-09-25 17:54:57 -07006515 switch (cmd) {
6516 case SET_KEY:
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006517 rc = iwl3945_update_sta_key_info(priv, key, sta_id);
Zhu Yib481de92007-09-25 17:54:57 -07006518 if (!rc) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006519 iwl3945_set_rxon_hwcrypto(priv, 1);
6520 iwl3945_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006521 key->hw_key_idx = sta_id;
6522 IWL_DEBUG_MAC80211("set_key success, using hwcrypto\n");
6523 key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
6524 }
6525 break;
6526 case DISABLE_KEY:
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006527 rc = iwl3945_clear_sta_key_info(priv, sta_id);
Zhu Yib481de92007-09-25 17:54:57 -07006528 if (!rc) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006529 iwl3945_set_rxon_hwcrypto(priv, 0);
6530 iwl3945_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006531 IWL_DEBUG_MAC80211("disable hwcrypto key\n");
6532 }
6533 break;
6534 default:
6535 rc = -EINVAL;
6536 }
6537
6538 IWL_DEBUG_MAC80211("leave\n");
6539 mutex_unlock(&priv->mutex);
6540
6541 return rc;
6542}
6543
Johannes Berge100bb62008-04-30 18:51:21 +02006544static int iwl3945_mac_conf_tx(struct ieee80211_hw *hw, u16 queue,
Zhu Yib481de92007-09-25 17:54:57 -07006545 const struct ieee80211_tx_queue_params *params)
6546{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08006547 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07006548 unsigned long flags;
6549 int q;
Zhu Yib481de92007-09-25 17:54:57 -07006550
6551 IWL_DEBUG_MAC80211("enter\n");
6552
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08006553 if (!iwl_is_ready_rf(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07006554 IWL_DEBUG_MAC80211("leave - RF not ready\n");
6555 return -EIO;
6556 }
6557
6558 if (queue >= AC_NUM) {
6559 IWL_DEBUG_MAC80211("leave - queue >= AC_NUM %d\n", queue);
6560 return 0;
6561 }
6562
Zhu Yib481de92007-09-25 17:54:57 -07006563 q = AC_NUM - 1 - queue;
6564
6565 spin_lock_irqsave(&priv->lock, flags);
6566
6567 priv->qos_data.def_qos_parm.ac[q].cw_min = cpu_to_le16(params->cw_min);
6568 priv->qos_data.def_qos_parm.ac[q].cw_max = cpu_to_le16(params->cw_max);
6569 priv->qos_data.def_qos_parm.ac[q].aifsn = params->aifs;
6570 priv->qos_data.def_qos_parm.ac[q].edca_txop =
Johannes Berg3330d7b2008-02-10 16:49:38 +01006571 cpu_to_le16((params->txop * 32));
Zhu Yib481de92007-09-25 17:54:57 -07006572
6573 priv->qos_data.def_qos_parm.ac[q].reserved1 = 0;
6574 priv->qos_data.qos_active = 1;
6575
6576 spin_unlock_irqrestore(&priv->lock, flags);
6577
6578 mutex_lock(&priv->mutex);
Johannes Berg05c914f2008-09-11 00:01:58 +02006579 if (priv->iw_mode == NL80211_IFTYPE_AP)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006580 iwl3945_activate_qos(priv, 1);
6581 else if (priv->assoc_id && iwl3945_is_associated(priv))
6582 iwl3945_activate_qos(priv, 0);
Zhu Yib481de92007-09-25 17:54:57 -07006583
6584 mutex_unlock(&priv->mutex);
6585
Zhu Yib481de92007-09-25 17:54:57 -07006586 IWL_DEBUG_MAC80211("leave\n");
6587 return 0;
6588}
6589
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006590static int iwl3945_mac_get_tx_stats(struct ieee80211_hw *hw,
Zhu Yib481de92007-09-25 17:54:57 -07006591 struct ieee80211_tx_queue_stats *stats)
6592{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08006593 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07006594 int i, avail;
Samuel Ortiz188cf6c2008-12-22 11:31:16 +08006595 struct iwl_tx_queue *txq;
Samuel Ortizd20b3c62008-12-19 10:37:15 +08006596 struct iwl_queue *q;
Zhu Yib481de92007-09-25 17:54:57 -07006597 unsigned long flags;
6598
6599 IWL_DEBUG_MAC80211("enter\n");
6600
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08006601 if (!iwl_is_ready_rf(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07006602 IWL_DEBUG_MAC80211("leave - RF not ready\n");
6603 return -EIO;
6604 }
6605
6606 spin_lock_irqsave(&priv->lock, flags);
6607
6608 for (i = 0; i < AC_NUM; i++) {
Samuel Ortiz188cf6c2008-12-22 11:31:16 +08006609 txq = &priv->txq[i];
Zhu Yib481de92007-09-25 17:54:57 -07006610 q = &txq->q;
Samuel Ortizd20b3c62008-12-19 10:37:15 +08006611 avail = iwl_queue_space(q);
Zhu Yib481de92007-09-25 17:54:57 -07006612
Johannes Berg57ffc582008-04-29 17:18:59 +02006613 stats[i].len = q->n_window - avail;
6614 stats[i].limit = q->n_window - q->high_mark;
6615 stats[i].count = q->n_window;
Zhu Yib481de92007-09-25 17:54:57 -07006616
6617 }
6618 spin_unlock_irqrestore(&priv->lock, flags);
6619
6620 IWL_DEBUG_MAC80211("leave\n");
6621
6622 return 0;
6623}
6624
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006625static void iwl3945_mac_reset_tsf(struct ieee80211_hw *hw)
Zhu Yib481de92007-09-25 17:54:57 -07006626{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08006627 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07006628 unsigned long flags;
6629
6630 mutex_lock(&priv->mutex);
6631 IWL_DEBUG_MAC80211("enter\n");
6632
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08006633 iwl_reset_qos(priv);
Ron Rindjunsky292ae172008-02-06 11:20:39 -08006634
Zhu Yib481de92007-09-25 17:54:57 -07006635 spin_lock_irqsave(&priv->lock, flags);
6636 priv->assoc_id = 0;
6637 priv->assoc_capability = 0;
6638 priv->call_post_assoc_from_beacon = 0;
6639
6640 /* new association get rid of ibss beacon skb */
6641 if (priv->ibss_beacon)
6642 dev_kfree_skb(priv->ibss_beacon);
6643
6644 priv->ibss_beacon = NULL;
6645
6646 priv->beacon_int = priv->hw->conf.beacon_int;
Tomas Winkler28afaf92008-12-19 10:37:06 +08006647 priv->timestamp = 0;
Johannes Berg05c914f2008-09-11 00:01:58 +02006648 if ((priv->iw_mode == NL80211_IFTYPE_STATION))
Zhu Yib481de92007-09-25 17:54:57 -07006649 priv->beacon_int = 0;
6650
6651 spin_unlock_irqrestore(&priv->lock, flags);
6652
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08006653 if (!iwl_is_ready_rf(priv)) {
Mohamed Abbasfde35712007-11-29 11:10:15 +08006654 IWL_DEBUG_MAC80211("leave - not ready\n");
6655 mutex_unlock(&priv->mutex);
6656 return;
6657 }
6658
Mohamed Abbas15e869d2007-10-25 17:15:46 +08006659 /* we are restarting association process
6660 * clear RXON_FILTER_ASSOC_MSK bit
6661 */
Johannes Berg05c914f2008-09-11 00:01:58 +02006662 if (priv->iw_mode != NL80211_IFTYPE_AP) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006663 iwl3945_scan_cancel_timeout(priv, 100);
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08006664 priv->staging39_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006665 iwl3945_commit_rxon(priv);
Mohamed Abbas15e869d2007-10-25 17:15:46 +08006666 }
6667
Zhu Yib481de92007-09-25 17:54:57 -07006668 /* Per mac80211.h: This is only used in IBSS mode... */
Johannes Berg05c914f2008-09-11 00:01:58 +02006669 if (priv->iw_mode != NL80211_IFTYPE_ADHOC) {
Mohamed Abbas15e869d2007-10-25 17:15:46 +08006670
Zhu Yib481de92007-09-25 17:54:57 -07006671 IWL_DEBUG_MAC80211("leave - not in IBSS\n");
6672 mutex_unlock(&priv->mutex);
6673 return;
6674 }
6675
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006676 iwl3945_set_rate(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006677
6678 mutex_unlock(&priv->mutex);
6679
6680 IWL_DEBUG_MAC80211("leave\n");
6681
6682}
6683
Johannes Berge039fa42008-05-15 12:55:29 +02006684static int iwl3945_mac_beacon_update(struct ieee80211_hw *hw, struct sk_buff *skb)
Zhu Yib481de92007-09-25 17:54:57 -07006685{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08006686 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07006687 unsigned long flags;
6688
Zhu Yib481de92007-09-25 17:54:57 -07006689 IWL_DEBUG_MAC80211("enter\n");
6690
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08006691 if (!iwl_is_ready_rf(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07006692 IWL_DEBUG_MAC80211("leave - RF not ready\n");
Zhu Yib481de92007-09-25 17:54:57 -07006693 return -EIO;
6694 }
6695
Johannes Berg05c914f2008-09-11 00:01:58 +02006696 if (priv->iw_mode != NL80211_IFTYPE_ADHOC) {
Zhu Yib481de92007-09-25 17:54:57 -07006697 IWL_DEBUG_MAC80211("leave - not IBSS\n");
Zhu Yib481de92007-09-25 17:54:57 -07006698 return -EIO;
6699 }
6700
6701 spin_lock_irqsave(&priv->lock, flags);
6702
6703 if (priv->ibss_beacon)
6704 dev_kfree_skb(priv->ibss_beacon);
6705
6706 priv->ibss_beacon = skb;
6707
6708 priv->assoc_id = 0;
6709
6710 IWL_DEBUG_MAC80211("leave\n");
6711 spin_unlock_irqrestore(&priv->lock, flags);
6712
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08006713 iwl_reset_qos(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006714
Abhijeet Kolekardc4b1e72008-09-03 11:26:30 +08006715 iwl3945_post_associate(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006716
Zhu Yib481de92007-09-25 17:54:57 -07006717
6718 return 0;
6719}
6720
6721/*****************************************************************************
6722 *
6723 * sysfs attributes
6724 *
6725 *****************************************************************************/
6726
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08006727#ifdef CONFIG_IWL3945_DEBUG
Zhu Yib481de92007-09-25 17:54:57 -07006728
6729/*
6730 * The following adds a new attribute to the sysfs representation
6731 * of this device driver (i.e. a new file in /sys/bus/pci/drivers/iwl/)
6732 * used for controlling the debug level.
6733 *
6734 * See the level definitions in iwl for details.
6735 */
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08006736static ssize_t show_debug_level(struct device *d,
6737 struct device_attribute *attr, char *buf)
Zhu Yib481de92007-09-25 17:54:57 -07006738{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08006739 struct iwl_priv *priv = d->driver_data;
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08006740
6741 return sprintf(buf, "0x%08X\n", priv->debug_level);
Zhu Yib481de92007-09-25 17:54:57 -07006742}
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08006743static ssize_t store_debug_level(struct device *d,
6744 struct device_attribute *attr,
Zhu Yib481de92007-09-25 17:54:57 -07006745 const char *buf, size_t count)
6746{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08006747 struct iwl_priv *priv = d->driver_data;
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08006748 unsigned long val;
6749 int ret;
Zhu Yib481de92007-09-25 17:54:57 -07006750
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08006751 ret = strict_strtoul(buf, 0, &val);
6752 if (ret)
Tomas Winkler978785a2008-12-19 10:37:31 +08006753 IWL_INFO(priv, "%s is not in hex or decimal form.\n", buf);
Zhu Yib481de92007-09-25 17:54:57 -07006754 else
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08006755 priv->debug_level = val;
Zhu Yib481de92007-09-25 17:54:57 -07006756
6757 return strnlen(buf, count);
6758}
6759
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08006760static DEVICE_ATTR(debug_level, S_IWUSR | S_IRUGO,
6761 show_debug_level, store_debug_level);
Zhu Yib481de92007-09-25 17:54:57 -07006762
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08006763#endif /* CONFIG_IWL3945_DEBUG */
Zhu Yib481de92007-09-25 17:54:57 -07006764
Zhu Yib481de92007-09-25 17:54:57 -07006765static ssize_t show_temperature(struct device *d,
6766 struct device_attribute *attr, char *buf)
6767{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08006768 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07006769
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08006770 if (!iwl_is_alive(priv))
Zhu Yib481de92007-09-25 17:54:57 -07006771 return -EAGAIN;
6772
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006773 return sprintf(buf, "%d\n", iwl3945_hw_get_temperature(priv));
Zhu Yib481de92007-09-25 17:54:57 -07006774}
6775
6776static DEVICE_ATTR(temperature, S_IRUGO, show_temperature, NULL);
6777
Zhu Yib481de92007-09-25 17:54:57 -07006778static ssize_t show_tx_power(struct device *d,
6779 struct device_attribute *attr, char *buf)
6780{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08006781 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07006782 return sprintf(buf, "%d\n", priv->user_txpower_limit);
6783}
6784
6785static ssize_t store_tx_power(struct device *d,
6786 struct device_attribute *attr,
6787 const char *buf, size_t count)
6788{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08006789 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07006790 char *p = (char *)buf;
6791 u32 val;
6792
6793 val = simple_strtoul(p, &p, 10);
6794 if (p == buf)
Tomas Winkler978785a2008-12-19 10:37:31 +08006795 IWL_INFO(priv, ": %s is not in decimal form.\n", buf);
Zhu Yib481de92007-09-25 17:54:57 -07006796 else
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006797 iwl3945_hw_reg_set_txpower(priv, val);
Zhu Yib481de92007-09-25 17:54:57 -07006798
6799 return count;
6800}
6801
6802static DEVICE_ATTR(tx_power, S_IWUSR | S_IRUGO, show_tx_power, store_tx_power);
6803
6804static ssize_t show_flags(struct device *d,
6805 struct device_attribute *attr, char *buf)
6806{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08006807 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07006808
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08006809 return sprintf(buf, "0x%04X\n", priv->active39_rxon.flags);
Zhu Yib481de92007-09-25 17:54:57 -07006810}
6811
6812static ssize_t store_flags(struct device *d,
6813 struct device_attribute *attr,
6814 const char *buf, size_t count)
6815{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08006816 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07006817 u32 flags = simple_strtoul(buf, NULL, 0);
6818
6819 mutex_lock(&priv->mutex);
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08006820 if (le32_to_cpu(priv->staging39_rxon.flags) != flags) {
Zhu Yib481de92007-09-25 17:54:57 -07006821 /* Cancel any currently running scans... */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006822 if (iwl3945_scan_cancel_timeout(priv, 100))
Winkler, Tomas39aadf82008-12-19 10:37:32 +08006823 IWL_WARN(priv, "Could not cancel scan.\n");
Zhu Yib481de92007-09-25 17:54:57 -07006824 else {
6825 IWL_DEBUG_INFO("Committing rxon.flags = 0x%04X\n",
6826 flags);
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08006827 priv->staging39_rxon.flags = cpu_to_le32(flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006828 iwl3945_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006829 }
6830 }
6831 mutex_unlock(&priv->mutex);
6832
6833 return count;
6834}
6835
6836static DEVICE_ATTR(flags, S_IWUSR | S_IRUGO, show_flags, store_flags);
6837
6838static ssize_t show_filter_flags(struct device *d,
6839 struct device_attribute *attr, char *buf)
6840{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08006841 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07006842
6843 return sprintf(buf, "0x%04X\n",
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08006844 le32_to_cpu(priv->active39_rxon.filter_flags));
Zhu Yib481de92007-09-25 17:54:57 -07006845}
6846
6847static ssize_t store_filter_flags(struct device *d,
6848 struct device_attribute *attr,
6849 const char *buf, size_t count)
6850{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08006851 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07006852 u32 filter_flags = simple_strtoul(buf, NULL, 0);
6853
6854 mutex_lock(&priv->mutex);
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08006855 if (le32_to_cpu(priv->staging39_rxon.filter_flags) != filter_flags) {
Zhu Yib481de92007-09-25 17:54:57 -07006856 /* Cancel any currently running scans... */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006857 if (iwl3945_scan_cancel_timeout(priv, 100))
Winkler, Tomas39aadf82008-12-19 10:37:32 +08006858 IWL_WARN(priv, "Could not cancel scan.\n");
Zhu Yib481de92007-09-25 17:54:57 -07006859 else {
6860 IWL_DEBUG_INFO("Committing rxon.filter_flags = "
6861 "0x%04X\n", filter_flags);
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08006862 priv->staging39_rxon.filter_flags =
Zhu Yib481de92007-09-25 17:54:57 -07006863 cpu_to_le32(filter_flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006864 iwl3945_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006865 }
6866 }
6867 mutex_unlock(&priv->mutex);
6868
6869 return count;
6870}
6871
6872static DEVICE_ATTR(filter_flags, S_IWUSR | S_IRUGO, show_filter_flags,
6873 store_filter_flags);
6874
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08006875#ifdef CONFIG_IWL3945_SPECTRUM_MEASUREMENT
Zhu Yib481de92007-09-25 17:54:57 -07006876
6877static ssize_t show_measurement(struct device *d,
6878 struct device_attribute *attr, char *buf)
6879{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08006880 struct iwl_priv *priv = dev_get_drvdata(d);
Tomas Winkler600c0e12008-12-19 10:37:04 +08006881 struct iwl_spectrum_notification measure_report;
Zhu Yib481de92007-09-25 17:54:57 -07006882 u32 size = sizeof(measure_report), len = 0, ofs = 0;
Tomas Winkler3ac7f142008-07-21 02:40:14 +03006883 u8 *data = (u8 *)&measure_report;
Zhu Yib481de92007-09-25 17:54:57 -07006884 unsigned long flags;
6885
6886 spin_lock_irqsave(&priv->lock, flags);
6887 if (!(priv->measurement_status & MEASUREMENT_READY)) {
6888 spin_unlock_irqrestore(&priv->lock, flags);
6889 return 0;
6890 }
6891 memcpy(&measure_report, &priv->measure_report, size);
6892 priv->measurement_status = 0;
6893 spin_unlock_irqrestore(&priv->lock, flags);
6894
6895 while (size && (PAGE_SIZE - len)) {
6896 hex_dump_to_buffer(data + ofs, size, 16, 1, buf + len,
6897 PAGE_SIZE - len, 1);
6898 len = strlen(buf);
6899 if (PAGE_SIZE - len)
6900 buf[len++] = '\n';
6901
6902 ofs += 16;
6903 size -= min(size, 16U);
6904 }
6905
6906 return len;
6907}
6908
6909static ssize_t store_measurement(struct device *d,
6910 struct device_attribute *attr,
6911 const char *buf, size_t count)
6912{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08006913 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07006914 struct ieee80211_measurement_params params = {
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08006915 .channel = le16_to_cpu(priv->active39_rxon.channel),
Zhu Yib481de92007-09-25 17:54:57 -07006916 .start_time = cpu_to_le64(priv->last_tsf),
6917 .duration = cpu_to_le16(1),
6918 };
6919 u8 type = IWL_MEASURE_BASIC;
6920 u8 buffer[32];
6921 u8 channel;
6922
6923 if (count) {
6924 char *p = buffer;
6925 strncpy(buffer, buf, min(sizeof(buffer), count));
6926 channel = simple_strtoul(p, NULL, 0);
6927 if (channel)
6928 params.channel = channel;
6929
6930 p = buffer;
6931 while (*p && *p != ' ')
6932 p++;
6933 if (*p)
6934 type = simple_strtoul(p + 1, NULL, 0);
6935 }
6936
6937 IWL_DEBUG_INFO("Invoking measurement of type %d on "
6938 "channel %d (for '%s')\n", type, params.channel, buf);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006939 iwl3945_get_measurement(priv, &params, type);
Zhu Yib481de92007-09-25 17:54:57 -07006940
6941 return count;
6942}
6943
6944static DEVICE_ATTR(measurement, S_IRUSR | S_IWUSR,
6945 show_measurement, store_measurement);
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08006946#endif /* CONFIG_IWL3945_SPECTRUM_MEASUREMENT */
Zhu Yib481de92007-09-25 17:54:57 -07006947
Zhu Yib481de92007-09-25 17:54:57 -07006948static ssize_t store_retry_rate(struct device *d,
6949 struct device_attribute *attr,
6950 const char *buf, size_t count)
6951{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08006952 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07006953
6954 priv->retry_rate = simple_strtoul(buf, NULL, 0);
6955 if (priv->retry_rate <= 0)
6956 priv->retry_rate = 1;
6957
6958 return count;
6959}
6960
6961static ssize_t show_retry_rate(struct device *d,
6962 struct device_attribute *attr, char *buf)
6963{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08006964 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07006965 return sprintf(buf, "%d", priv->retry_rate);
6966}
6967
6968static DEVICE_ATTR(retry_rate, S_IWUSR | S_IRUSR, show_retry_rate,
6969 store_retry_rate);
6970
6971static ssize_t store_power_level(struct device *d,
6972 struct device_attribute *attr,
6973 const char *buf, size_t count)
6974{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08006975 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07006976 int rc;
6977 int mode;
6978
6979 mode = simple_strtoul(buf, NULL, 0);
6980 mutex_lock(&priv->mutex);
6981
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08006982 if (!iwl_is_ready(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07006983 rc = -EAGAIN;
6984 goto out;
6985 }
6986
Samuel Ortiz1125eff2008-12-19 10:37:14 +08006987 if ((mode < 1) || (mode > IWL39_POWER_LIMIT) ||
6988 (mode == IWL39_POWER_AC))
6989 mode = IWL39_POWER_AC;
Zhu Yib481de92007-09-25 17:54:57 -07006990 else
6991 mode |= IWL_POWER_ENABLED;
6992
6993 if (mode != priv->power_mode) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006994 rc = iwl3945_send_power_mode(priv, IWL_POWER_LEVEL(mode));
Zhu Yib481de92007-09-25 17:54:57 -07006995 if (rc) {
6996 IWL_DEBUG_MAC80211("failed setting power mode.\n");
6997 goto out;
6998 }
6999 priv->power_mode = mode;
7000 }
7001
7002 rc = count;
7003
7004 out:
7005 mutex_unlock(&priv->mutex);
7006 return rc;
7007}
7008
7009#define MAX_WX_STRING 80
7010
7011/* Values are in microsecond */
7012static const s32 timeout_duration[] = {
7013 350000,
7014 250000,
7015 75000,
7016 37000,
7017 25000,
7018};
7019static const s32 period_duration[] = {
7020 400000,
7021 700000,
7022 1000000,
7023 1000000,
7024 1000000
7025};
7026
7027static ssize_t show_power_level(struct device *d,
7028 struct device_attribute *attr, char *buf)
7029{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08007030 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07007031 int level = IWL_POWER_LEVEL(priv->power_mode);
7032 char *p = buf;
7033
7034 p += sprintf(p, "%d ", level);
7035 switch (level) {
7036 case IWL_POWER_MODE_CAM:
Samuel Ortiz1125eff2008-12-19 10:37:14 +08007037 case IWL39_POWER_AC:
Zhu Yib481de92007-09-25 17:54:57 -07007038 p += sprintf(p, "(AC)");
7039 break;
Samuel Ortiz1125eff2008-12-19 10:37:14 +08007040 case IWL39_POWER_BATTERY:
Zhu Yib481de92007-09-25 17:54:57 -07007041 p += sprintf(p, "(BATTERY)");
7042 break;
7043 default:
7044 p += sprintf(p,
7045 "(Timeout %dms, Period %dms)",
7046 timeout_duration[level - 1] / 1000,
7047 period_duration[level - 1] / 1000);
7048 }
7049
7050 if (!(priv->power_mode & IWL_POWER_ENABLED))
7051 p += sprintf(p, " OFF\n");
7052 else
7053 p += sprintf(p, " \n");
7054
Tomas Winkler3ac7f142008-07-21 02:40:14 +03007055 return p - buf + 1;
Zhu Yib481de92007-09-25 17:54:57 -07007056
7057}
7058
7059static DEVICE_ATTR(power_level, S_IWUSR | S_IRUSR, show_power_level,
7060 store_power_level);
7061
7062static ssize_t show_channels(struct device *d,
7063 struct device_attribute *attr, char *buf)
7064{
Johannes Berg8318d782008-01-24 19:38:38 +01007065 /* all this shit doesn't belong into sysfs anyway */
7066 return 0;
Zhu Yib481de92007-09-25 17:54:57 -07007067}
7068
7069static DEVICE_ATTR(channels, S_IRUSR, show_channels, NULL);
7070
7071static ssize_t show_statistics(struct device *d,
7072 struct device_attribute *attr, char *buf)
7073{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08007074 struct iwl_priv *priv = dev_get_drvdata(d);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007075 u32 size = sizeof(struct iwl3945_notif_statistics);
Zhu Yib481de92007-09-25 17:54:57 -07007076 u32 len = 0, ofs = 0;
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08007077 u8 *data = (u8 *)&priv->statistics_39;
Zhu Yib481de92007-09-25 17:54:57 -07007078 int rc = 0;
7079
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08007080 if (!iwl_is_alive(priv))
Zhu Yib481de92007-09-25 17:54:57 -07007081 return -EAGAIN;
7082
7083 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007084 rc = iwl3945_send_statistics_request(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007085 mutex_unlock(&priv->mutex);
7086
7087 if (rc) {
7088 len = sprintf(buf,
7089 "Error sending statistics request: 0x%08X\n", rc);
7090 return len;
7091 }
7092
7093 while (size && (PAGE_SIZE - len)) {
7094 hex_dump_to_buffer(data + ofs, size, 16, 1, buf + len,
7095 PAGE_SIZE - len, 1);
7096 len = strlen(buf);
7097 if (PAGE_SIZE - len)
7098 buf[len++] = '\n';
7099
7100 ofs += 16;
7101 size -= min(size, 16U);
7102 }
7103
7104 return len;
7105}
7106
7107static DEVICE_ATTR(statistics, S_IRUGO, show_statistics, NULL);
7108
7109static ssize_t show_antenna(struct device *d,
7110 struct device_attribute *attr, char *buf)
7111{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08007112 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07007113
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08007114 if (!iwl_is_alive(priv))
Zhu Yib481de92007-09-25 17:54:57 -07007115 return -EAGAIN;
7116
7117 return sprintf(buf, "%d\n", priv->antenna);
7118}
7119
7120static ssize_t store_antenna(struct device *d,
7121 struct device_attribute *attr,
7122 const char *buf, size_t count)
7123{
7124 int ant;
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08007125 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07007126
7127 if (count == 0)
7128 return 0;
7129
7130 if (sscanf(buf, "%1i", &ant) != 1) {
7131 IWL_DEBUG_INFO("not in hex or decimal form.\n");
7132 return count;
7133 }
7134
7135 if ((ant >= 0) && (ant <= 2)) {
7136 IWL_DEBUG_INFO("Setting antenna select to %d.\n", ant);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007137 priv->antenna = (enum iwl3945_antenna)ant;
Zhu Yib481de92007-09-25 17:54:57 -07007138 } else
7139 IWL_DEBUG_INFO("Bad antenna select value %d.\n", ant);
7140
7141
7142 return count;
7143}
7144
7145static DEVICE_ATTR(antenna, S_IWUSR | S_IRUGO, show_antenna, store_antenna);
7146
7147static ssize_t show_status(struct device *d,
7148 struct device_attribute *attr, char *buf)
7149{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08007150 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08007151 if (!iwl_is_alive(priv))
Zhu Yib481de92007-09-25 17:54:57 -07007152 return -EAGAIN;
7153 return sprintf(buf, "0x%08x\n", (int)priv->status);
7154}
7155
7156static DEVICE_ATTR(status, S_IRUGO, show_status, NULL);
7157
7158static ssize_t dump_error_log(struct device *d,
7159 struct device_attribute *attr,
7160 const char *buf, size_t count)
7161{
7162 char *p = (char *)buf;
7163
7164 if (p[0] == '1')
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08007165 iwl3945_dump_nic_error_log((struct iwl_priv *)d->driver_data);
Zhu Yib481de92007-09-25 17:54:57 -07007166
7167 return strnlen(buf, count);
7168}
7169
7170static DEVICE_ATTR(dump_errors, S_IWUSR, NULL, dump_error_log);
7171
7172static ssize_t dump_event_log(struct device *d,
7173 struct device_attribute *attr,
7174 const char *buf, size_t count)
7175{
7176 char *p = (char *)buf;
7177
7178 if (p[0] == '1')
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08007179 iwl3945_dump_nic_event_log((struct iwl_priv *)d->driver_data);
Zhu Yib481de92007-09-25 17:54:57 -07007180
7181 return strnlen(buf, count);
7182}
7183
7184static DEVICE_ATTR(dump_events, S_IWUSR, NULL, dump_event_log);
7185
7186/*****************************************************************************
7187 *
Tomas Winklera96a27f2008-10-23 23:48:56 -07007188 * driver setup and tear down
Zhu Yib481de92007-09-25 17:54:57 -07007189 *
7190 *****************************************************************************/
7191
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08007192static void iwl3945_setup_deferred_work(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07007193{
7194 priv->workqueue = create_workqueue(DRV_NAME);
7195
7196 init_waitqueue_head(&priv->wait_command_queue);
7197
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007198 INIT_WORK(&priv->up, iwl3945_bg_up);
7199 INIT_WORK(&priv->restart, iwl3945_bg_restart);
7200 INIT_WORK(&priv->rx_replenish, iwl3945_bg_rx_replenish);
7201 INIT_WORK(&priv->scan_completed, iwl3945_bg_scan_completed);
7202 INIT_WORK(&priv->request_scan, iwl3945_bg_request_scan);
7203 INIT_WORK(&priv->abort_scan, iwl3945_bg_abort_scan);
7204 INIT_WORK(&priv->rf_kill, iwl3945_bg_rf_kill);
7205 INIT_WORK(&priv->beacon_update, iwl3945_bg_beacon_update);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007206 INIT_DELAYED_WORK(&priv->init_alive_start, iwl3945_bg_init_alive_start);
7207 INIT_DELAYED_WORK(&priv->alive_start, iwl3945_bg_alive_start);
7208 INIT_DELAYED_WORK(&priv->scan_check, iwl3945_bg_scan_check);
Zhu Yib481de92007-09-25 17:54:57 -07007209
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007210 iwl3945_hw_setup_deferred_work(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007211
7212 tasklet_init(&priv->irq_tasklet, (void (*)(unsigned long))
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007213 iwl3945_irq_tasklet, (unsigned long)priv);
Zhu Yib481de92007-09-25 17:54:57 -07007214}
7215
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08007216static void iwl3945_cancel_deferred_work(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07007217{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007218 iwl3945_hw_cancel_deferred_work(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007219
Joonwoo Parke47eb6a2007-11-29 10:42:49 +09007220 cancel_delayed_work_sync(&priv->init_alive_start);
Zhu Yib481de92007-09-25 17:54:57 -07007221 cancel_delayed_work(&priv->scan_check);
7222 cancel_delayed_work(&priv->alive_start);
Zhu Yib481de92007-09-25 17:54:57 -07007223 cancel_work_sync(&priv->beacon_update);
7224}
7225
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007226static struct attribute *iwl3945_sysfs_entries[] = {
Zhu Yib481de92007-09-25 17:54:57 -07007227 &dev_attr_antenna.attr,
7228 &dev_attr_channels.attr,
7229 &dev_attr_dump_errors.attr,
7230 &dev_attr_dump_events.attr,
7231 &dev_attr_flags.attr,
7232 &dev_attr_filter_flags.attr,
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08007233#ifdef CONFIG_IWL3945_SPECTRUM_MEASUREMENT
Zhu Yib481de92007-09-25 17:54:57 -07007234 &dev_attr_measurement.attr,
7235#endif
7236 &dev_attr_power_level.attr,
Zhu Yib481de92007-09-25 17:54:57 -07007237 &dev_attr_retry_rate.attr,
Zhu Yib481de92007-09-25 17:54:57 -07007238 &dev_attr_statistics.attr,
7239 &dev_attr_status.attr,
7240 &dev_attr_temperature.attr,
Zhu Yib481de92007-09-25 17:54:57 -07007241 &dev_attr_tx_power.attr,
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08007242#ifdef CONFIG_IWL3945_DEBUG
7243 &dev_attr_debug_level.attr,
7244#endif
Zhu Yib481de92007-09-25 17:54:57 -07007245 NULL
7246};
7247
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007248static struct attribute_group iwl3945_attribute_group = {
Zhu Yib481de92007-09-25 17:54:57 -07007249 .name = NULL, /* put in device directory */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007250 .attrs = iwl3945_sysfs_entries,
Zhu Yib481de92007-09-25 17:54:57 -07007251};
7252
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007253static struct ieee80211_ops iwl3945_hw_ops = {
7254 .tx = iwl3945_mac_tx,
7255 .start = iwl3945_mac_start,
7256 .stop = iwl3945_mac_stop,
7257 .add_interface = iwl3945_mac_add_interface,
7258 .remove_interface = iwl3945_mac_remove_interface,
7259 .config = iwl3945_mac_config,
7260 .config_interface = iwl3945_mac_config_interface,
7261 .configure_filter = iwl3945_configure_filter,
7262 .set_key = iwl3945_mac_set_key,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007263 .get_tx_stats = iwl3945_mac_get_tx_stats,
7264 .conf_tx = iwl3945_mac_conf_tx,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007265 .reset_tsf = iwl3945_mac_reset_tsf,
Abhijeet Kolekarcd56d332008-09-03 11:26:21 +08007266 .bss_info_changed = iwl3945_bss_info_changed,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007267 .hw_scan = iwl3945_mac_hw_scan
Zhu Yib481de92007-09-25 17:54:57 -07007268};
7269
Winkler, Tomase52119c2008-12-22 11:31:19 +08007270static int iwl3945_init_drv(struct iwl_priv *priv)
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08007271{
7272 int ret;
7273
7274 priv->retry_rate = 1;
7275 priv->ibss_beacon = NULL;
7276
7277 spin_lock_init(&priv->lock);
7278 spin_lock_init(&priv->power_data.lock);
7279 spin_lock_init(&priv->sta_lock);
7280 spin_lock_init(&priv->hcmd_lock);
7281
7282 INIT_LIST_HEAD(&priv->free_frames);
7283
7284 mutex_init(&priv->mutex);
7285
7286 /* Clear the driver's (not device's) station table */
7287 iwl3945_clear_stations_table(priv);
7288
7289 priv->data_retry_limit = -1;
7290 priv->ieee_channels = NULL;
7291 priv->ieee_rates = NULL;
7292 priv->band = IEEE80211_BAND_2GHZ;
7293
7294 priv->iw_mode = NL80211_IFTYPE_STATION;
7295
7296 iwl_reset_qos(priv);
7297
7298 priv->qos_data.qos_active = 0;
7299 priv->qos_data.qos_cap.val = 0;
7300
7301 priv->rates_mask = IWL_RATES_MASK;
7302 /* If power management is turned on, default to AC mode */
7303 priv->power_mode = IWL_POWER_AC;
7304 priv->user_txpower_limit = IWL_DEFAULT_TX_POWER;
7305
7306 ret = iwl3945_init_channel_map(priv);
7307 if (ret) {
7308 IWL_ERR(priv, "initializing regulatory failed: %d\n", ret);
7309 goto err;
7310 }
7311
7312 ret = iwl3945_init_geos(priv);
7313 if (ret) {
7314 IWL_ERR(priv, "initializing geos failed: %d\n", ret);
7315 goto err_free_channel_map;
7316 }
7317
7318 return 0;
7319
7320err_free_channel_map:
7321 iwl3945_free_channel_map(priv);
7322err:
7323 return ret;
7324}
7325
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007326static int iwl3945_pci_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
Zhu Yib481de92007-09-25 17:54:57 -07007327{
7328 int err = 0;
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08007329 struct iwl_priv *priv;
Zhu Yib481de92007-09-25 17:54:57 -07007330 struct ieee80211_hw *hw;
Kolekar, Abhijeetc0f20d92008-12-19 10:37:19 +08007331 struct iwl_cfg *cfg = (struct iwl_cfg *)(ent->driver_data);
Mohamed Abbas0359fac2008-03-28 16:21:08 -07007332 unsigned long flags;
Zhu Yib481de92007-09-25 17:54:57 -07007333
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08007334 /***********************
7335 * 1. Allocating HW data
7336 * ********************/
7337
Zhu Yib481de92007-09-25 17:54:57 -07007338 /* mac80211 allocates memory for this device instance, including
7339 * space for this driver's private structure */
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08007340 hw = iwl_alloc_all(cfg, &iwl3945_hw_ops);
Zhu Yib481de92007-09-25 17:54:57 -07007341 if (hw == NULL) {
Samuel Ortiza3139c52008-12-19 10:37:09 +08007342 printk(KERN_ERR DRV_NAME "Can not allocate network device\n");
Zhu Yib481de92007-09-25 17:54:57 -07007343 err = -ENOMEM;
7344 goto out;
7345 }
Zhu Yib481de92007-09-25 17:54:57 -07007346 priv = hw->priv;
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08007347 SET_IEEE80211_DEV(hw, &pdev->dev);
Cahill, Ben M6440adb2007-11-29 11:09:55 +08007348
Kolekar, Abhijeetdf878d82008-12-19 10:37:35 +08007349 if ((iwl3945_mod_params.num_of_queues > IWL39_MAX_NUM_QUEUES) ||
7350 (iwl3945_mod_params.num_of_queues < IWL_MIN_NUM_QUEUES)) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08007351 IWL_ERR(priv,
7352 "invalid queues_num, should be between %d and %d\n",
7353 IWL_MIN_NUM_QUEUES, IWL39_MAX_NUM_QUEUES);
Samuel Ortiza3139c52008-12-19 10:37:09 +08007354 err = -EINVAL;
7355 goto out;
7356 }
7357
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08007358 /*
7359 * Disabling hardware scan means that mac80211 will perform scans
7360 * "the hard way", rather than using device's scan.
7361 */
Kolekar, Abhijeetdf878d82008-12-19 10:37:35 +08007362 if (iwl3945_mod_params.disable_hw_scan) {
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08007363 IWL_DEBUG_INFO("Disabling hw_scan\n");
7364 iwl3945_hw_ops.hw_scan = NULL;
7365 }
7366
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08007367
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08007368 IWL_DEBUG_INFO("*** LOAD DRIVER ***\n");
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08007369 priv->cfg = cfg;
7370 priv->pci_dev = pdev;
7371
7372#ifdef CONFIG_IWL3945_DEBUG
7373 priv->debug_level = iwl3945_mod_params.debug;
7374 atomic_set(&priv->restrict_refcnt, 0);
7375#endif
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08007376 hw->rate_control_algorithm = "iwl-3945-rs";
7377 hw->sta_data_size = sizeof(struct iwl3945_sta_priv);
7378
Cahill, Ben M6440adb2007-11-29 11:09:55 +08007379 /* Select antenna (may be helpful if only one antenna is connected) */
Kolekar, Abhijeetdf878d82008-12-19 10:37:35 +08007380 priv->antenna = (enum iwl3945_antenna)iwl3945_mod_params.antenna;
Zhu Yib481de92007-09-25 17:54:57 -07007381
Bruno Randolf566bfe52008-05-08 19:15:40 +02007382 /* Tell mac80211 our characteristics */
Johannes Berg605a0bd2008-07-15 10:10:01 +02007383 hw->flags = IEEE80211_HW_SIGNAL_DBM |
Bruno Randolf566bfe52008-05-08 19:15:40 +02007384 IEEE80211_HW_NOISE_DBM;
Zhu Yib481de92007-09-25 17:54:57 -07007385
Luis R. Rodriguezf59ac042008-08-29 16:26:43 -07007386 hw->wiphy->interface_modes =
Luis R. Rodriguezf59ac042008-08-29 16:26:43 -07007387 BIT(NL80211_IFTYPE_STATION) |
7388 BIT(NL80211_IFTYPE_ADHOC);
7389
Luis R. Rodriguezea4a82dc2008-11-12 14:22:04 -08007390 hw->wiphy->fw_handles_regulatory = true;
7391
Cahill, Ben M6440adb2007-11-29 11:09:55 +08007392 /* 4 EDCA QOS priorities */
Zhu Yib481de92007-09-25 17:54:57 -07007393 hw->queues = 4;
7394
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08007395 /***************************
7396 * 2. Initializing PCI bus
7397 * *************************/
Zhu Yib481de92007-09-25 17:54:57 -07007398 if (pci_enable_device(pdev)) {
7399 err = -ENODEV;
7400 goto out_ieee80211_free_hw;
7401 }
7402
7403 pci_set_master(pdev);
7404
Zhu Yib481de92007-09-25 17:54:57 -07007405 err = pci_set_dma_mask(pdev, DMA_32BIT_MASK);
7406 if (!err)
7407 err = pci_set_consistent_dma_mask(pdev, DMA_32BIT_MASK);
7408 if (err) {
Tomas Winkler978785a2008-12-19 10:37:31 +08007409 IWL_WARN(priv, "No suitable DMA available.\n");
Zhu Yib481de92007-09-25 17:54:57 -07007410 goto out_pci_disable_device;
7411 }
7412
7413 pci_set_drvdata(pdev, priv);
7414 err = pci_request_regions(pdev, DRV_NAME);
7415 if (err)
7416 goto out_pci_disable_device;
Cahill, Ben M6440adb2007-11-29 11:09:55 +08007417
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08007418 /***********************
7419 * 3. Read REV Register
7420 * ********************/
Zhu Yib481de92007-09-25 17:54:57 -07007421 priv->hw_base = pci_iomap(pdev, 0, 0);
7422 if (!priv->hw_base) {
7423 err = -ENODEV;
7424 goto out_pci_release_regions;
7425 }
7426
7427 IWL_DEBUG_INFO("pci_resource_len = 0x%08llx\n",
7428 (unsigned long long) pci_resource_len(pdev, 0));
7429 IWL_DEBUG_INFO("pci_resource_base = %p\n", priv->hw_base);
7430
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08007431 /* We disable the RETRY_TIMEOUT register (0x41) to keep
7432 * PCI Tx retries from interfering with C3 CPU state */
7433 pci_write_config_byte(pdev, 0x41, 0x00);
Zhu Yib481de92007-09-25 17:54:57 -07007434
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08007435 /* amp init */
7436 err = priv->cfg->ops->lib->apm_ops.init(priv);
Tomas Winkler3ac7f142008-07-21 02:40:14 +03007437 if (err < 0) {
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08007438 IWL_DEBUG_INFO("Failed to init APMG\n");
7439 goto out_iounmap;
Tomas Winkler3ac7f142008-07-21 02:40:14 +03007440 }
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08007441
7442 /***********************
7443 * 4. Read EEPROM
7444 * ********************/
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08007445
Zhu Yi5a669262008-01-14 17:46:18 -08007446 /* Read the EEPROM */
7447 err = iwl3945_eeprom_init(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007448 if (err) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08007449 IWL_ERR(priv, "Unable to init EEPROM\n");
Zhu Yi5a669262008-01-14 17:46:18 -08007450 goto out_remove_sysfs;
7451 }
7452 /* MAC Address location in EEPROM same for 3945/4965 */
7453 get_eeprom_mac(priv, priv->mac_addr);
Johannes Berge1749612008-10-27 15:59:26 -07007454 IWL_DEBUG_INFO("MAC address: %pM\n", priv->mac_addr);
Zhu Yi5a669262008-01-14 17:46:18 -08007455 SET_IEEE80211_PERM_ADDR(priv->hw, priv->mac_addr);
7456
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08007457 /***********************
7458 * 5. Setup HW Constants
7459 * ********************/
7460 /* Device-specific setup */
Abhijeet Kolekar3832ec92008-12-19 10:37:26 +08007461 if (iwl3945_hw_set_hw_params(priv)) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08007462 IWL_ERR(priv, "failed to set hw settings\n");
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08007463 goto out_iounmap;
7464 }
7465
7466 /***********************
7467 * 6. Setup priv
7468 * ********************/
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08007469
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08007470 err = iwl3945_init_drv(priv);
Reinette Chatre849e0dc2008-01-23 10:15:18 -08007471 if (err) {
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08007472 IWL_ERR(priv, "initializing driver failed\n");
7473 goto out_free_geos;
Reinette Chatre849e0dc2008-01-23 10:15:18 -08007474 }
Reinette Chatre849e0dc2008-01-23 10:15:18 -08007475
Tomas Winkler978785a2008-12-19 10:37:31 +08007476 IWL_INFO(priv, "Detected Intel Wireless WiFi Link %s\n",
7477 priv->cfg->name);
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08007478
7479 /***********************************
7480 * 7. Initialize Module Parameters
7481 * **********************************/
7482
7483 /* Initialize module parameter values here */
7484 /* Disable radio (SW RF KILL) via parameter when loading driver */
Kolekar, Abhijeetdf878d82008-12-19 10:37:35 +08007485 if (iwl3945_mod_params.disable) {
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08007486 set_bit(STATUS_RF_KILL_SW, &priv->status);
7487 IWL_DEBUG_INFO("Radio disabled.\n");
7488 }
7489
7490
7491 /***********************
7492 * 8. Setup Services
7493 * ********************/
7494
7495 spin_lock_irqsave(&priv->lock, flags);
7496 iwl3945_disable_interrupts(priv);
7497 spin_unlock_irqrestore(&priv->lock, flags);
7498
7499 err = sysfs_create_group(&pdev->dev.kobj, &iwl3945_attribute_group);
7500 if (err) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08007501 IWL_ERR(priv, "failed to create sysfs device attributes\n");
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08007502 goto out_release_irq;
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08007503 }
7504
7505 iwl3945_set_rxon_channel(priv, IEEE80211_BAND_2GHZ, 6);
7506 iwl3945_setup_deferred_work(priv);
7507 iwl3945_setup_rx_handlers(priv);
7508
7509 /***********************
7510 * 9. Conclude
7511 * ********************/
7512 pci_save_state(pdev);
7513 pci_disable_device(pdev);
7514
7515 /*********************************
7516 * 10. Setup and Register mac80211
7517 * *******************************/
7518
Zhu Yi5a669262008-01-14 17:46:18 -08007519 err = ieee80211_register_hw(priv->hw);
7520 if (err) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08007521 IWL_ERR(priv, "Failed to register network device: %d\n", err);
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08007522 goto out_remove_sysfs;
Zhu Yib481de92007-09-25 17:54:57 -07007523 }
7524
Zhu Yi5a669262008-01-14 17:46:18 -08007525 priv->hw->conf.beacon_int = 100;
7526 priv->mac80211_registered = 1;
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08007527
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08007528 err = iwl3945_rfkill_init(priv);
7529 if (err)
Winkler, Tomas15b16872008-12-19 10:37:33 +08007530 IWL_ERR(priv, "Unable to initialize RFKILL system. "
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08007531 "Ignoring error: %d\n", err);
7532
Zhu Yib481de92007-09-25 17:54:57 -07007533 return 0;
7534
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08007535 out_remove_sysfs:
7536 sysfs_remove_group(&pdev->dev.kobj, &iwl3945_attribute_group);
Reinette Chatre849e0dc2008-01-23 10:15:18 -08007537 out_free_geos:
7538 iwl3945_free_geos(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007539
7540 out_release_irq:
Zhu Yib481de92007-09-25 17:54:57 -07007541 destroy_workqueue(priv->workqueue);
7542 priv->workqueue = NULL;
Abhijeet Kolekar3832ec92008-12-19 10:37:26 +08007543 iwl3945_unset_hw_params(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007544
7545 out_iounmap:
7546 pci_iounmap(pdev, priv->hw_base);
7547 out_pci_release_regions:
7548 pci_release_regions(pdev);
7549 out_pci_disable_device:
7550 pci_disable_device(pdev);
7551 pci_set_drvdata(pdev, NULL);
7552 out_ieee80211_free_hw:
7553 ieee80211_free_hw(priv->hw);
7554 out:
7555 return err;
7556}
7557
Reinette Chatrec83dbf62008-03-21 13:53:41 -07007558static void __devexit iwl3945_pci_remove(struct pci_dev *pdev)
Zhu Yib481de92007-09-25 17:54:57 -07007559{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08007560 struct iwl_priv *priv = pci_get_drvdata(pdev);
Mohamed Abbas0359fac2008-03-28 16:21:08 -07007561 unsigned long flags;
Zhu Yib481de92007-09-25 17:54:57 -07007562
7563 if (!priv)
7564 return;
7565
7566 IWL_DEBUG_INFO("*** UNLOAD DRIVER ***\n");
7567
Zhu Yib481de92007-09-25 17:54:57 -07007568 set_bit(STATUS_EXIT_PENDING, &priv->status);
Zhu Yib24d22b2007-12-19 13:59:52 +08007569
Kolekar, Abhijeetd552bfb2008-12-19 10:37:41 +08007570 if (priv->mac80211_registered) {
7571 ieee80211_unregister_hw(priv->hw);
7572 priv->mac80211_registered = 0;
7573 } else {
7574 iwl3945_down(priv);
7575 }
Zhu Yib481de92007-09-25 17:54:57 -07007576
Mohamed Abbas0359fac2008-03-28 16:21:08 -07007577 /* make sure we flush any pending irq or
7578 * tasklet for the driver
7579 */
7580 spin_lock_irqsave(&priv->lock, flags);
7581 iwl3945_disable_interrupts(priv);
7582 spin_unlock_irqrestore(&priv->lock, flags);
7583
7584 iwl_synchronize_irq(priv);
7585
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007586 sysfs_remove_group(&pdev->dev.kobj, &iwl3945_attribute_group);
Zhu Yib481de92007-09-25 17:54:57 -07007587
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08007588 iwl3945_rfkill_unregister(priv);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007589 iwl3945_dealloc_ucode_pci(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007590
7591 if (priv->rxq.bd)
Winkler, Tomas51af3d32008-12-22 11:31:23 +08007592 iwl_rx_queue_free(priv, &priv->rxq);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007593 iwl3945_hw_txq_ctx_free(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007594
Abhijeet Kolekar3832ec92008-12-19 10:37:26 +08007595 iwl3945_unset_hw_params(priv);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007596 iwl3945_clear_stations_table(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007597
Mohamed Abbas6ef89d02007-10-25 17:15:47 +08007598 /*netif_stop_queue(dev); */
7599 flush_workqueue(priv->workqueue);
7600
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007601 /* ieee80211_unregister_hw calls iwl3945_mac_stop, which flushes
Zhu Yib481de92007-09-25 17:54:57 -07007602 * priv->workqueue... so we can't take down the workqueue
7603 * until now... */
7604 destroy_workqueue(priv->workqueue);
7605 priv->workqueue = NULL;
7606
Zhu Yib481de92007-09-25 17:54:57 -07007607 pci_iounmap(pdev, priv->hw_base);
7608 pci_release_regions(pdev);
7609 pci_disable_device(pdev);
7610 pci_set_drvdata(pdev, NULL);
7611
Reinette Chatre849e0dc2008-01-23 10:15:18 -08007612 iwl3945_free_channel_map(priv);
7613 iwl3945_free_geos(priv);
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08007614 kfree(priv->scan39);
Zhu Yib481de92007-09-25 17:54:57 -07007615 if (priv->ibss_beacon)
7616 dev_kfree_skb(priv->ibss_beacon);
7617
7618 ieee80211_free_hw(priv->hw);
7619}
7620
7621#ifdef CONFIG_PM
7622
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007623static int iwl3945_pci_suspend(struct pci_dev *pdev, pm_message_t state)
Zhu Yib481de92007-09-25 17:54:57 -07007624{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08007625 struct iwl_priv *priv = pci_get_drvdata(pdev);
Zhu Yib481de92007-09-25 17:54:57 -07007626
Zhu Yie655b9f2008-01-24 02:19:38 -08007627 if (priv->is_open) {
7628 set_bit(STATUS_IN_SUSPEND, &priv->status);
7629 iwl3945_mac_stop(priv->hw);
7630 priv->is_open = 1;
7631 }
Zhu Yib481de92007-09-25 17:54:57 -07007632
Zhu Yib481de92007-09-25 17:54:57 -07007633 pci_set_power_state(pdev, PCI_D3hot);
7634
Zhu Yib481de92007-09-25 17:54:57 -07007635 return 0;
7636}
7637
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007638static int iwl3945_pci_resume(struct pci_dev *pdev)
Zhu Yib481de92007-09-25 17:54:57 -07007639{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08007640 struct iwl_priv *priv = pci_get_drvdata(pdev);
Zhu Yib481de92007-09-25 17:54:57 -07007641
Zhu Yib481de92007-09-25 17:54:57 -07007642 pci_set_power_state(pdev, PCI_D0);
Zhu Yib481de92007-09-25 17:54:57 -07007643
Zhu Yie655b9f2008-01-24 02:19:38 -08007644 if (priv->is_open)
7645 iwl3945_mac_start(priv->hw);
Zhu Yib481de92007-09-25 17:54:57 -07007646
Zhu Yie655b9f2008-01-24 02:19:38 -08007647 clear_bit(STATUS_IN_SUSPEND, &priv->status);
Zhu Yib481de92007-09-25 17:54:57 -07007648 return 0;
7649}
7650
7651#endif /* CONFIG_PM */
7652
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08007653/*************** RFKILL FUNCTIONS **********/
Adel Gadllah80fcc9e2008-07-01 17:49:50 +02007654#ifdef CONFIG_IWL3945_RFKILL
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08007655/* software rf-kill from user */
7656static int iwl3945_rfkill_soft_rf_kill(void *data, enum rfkill_state state)
7657{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08007658 struct iwl_priv *priv = data;
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08007659 int err = 0;
7660
Adel Gadllah80fcc9e2008-07-01 17:49:50 +02007661 if (!priv->rfkill)
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08007662 return 0;
7663
7664 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
7665 return 0;
7666
Tomas Winklera96a27f2008-10-23 23:48:56 -07007667 IWL_DEBUG_RF_KILL("we received soft RFKILL set to state %d\n", state);
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08007668 mutex_lock(&priv->mutex);
7669
7670 switch (state) {
Zhu Yiacdfe9b2008-06-30 17:23:32 +08007671 case RFKILL_STATE_UNBLOCKED:
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08007672 if (iwl_is_rfkill_hw(priv)) {
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08007673 err = -EBUSY;
Adel Gadllah80fcc9e2008-07-01 17:49:50 +02007674 goto out_unlock;
7675 }
7676 iwl3945_radio_kill_sw(priv, 0);
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08007677 break;
Zhu Yiacdfe9b2008-06-30 17:23:32 +08007678 case RFKILL_STATE_SOFT_BLOCKED:
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08007679 iwl3945_radio_kill_sw(priv, 1);
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08007680 break;
Zhu Yiacdfe9b2008-06-30 17:23:32 +08007681 default:
Winkler, Tomas39aadf82008-12-19 10:37:32 +08007682 IWL_WARN(priv, "received unexpected RFKILL state %d\n", state);
Zhu Yiacdfe9b2008-06-30 17:23:32 +08007683 break;
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08007684 }
Adel Gadllah80fcc9e2008-07-01 17:49:50 +02007685out_unlock:
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08007686 mutex_unlock(&priv->mutex);
7687
7688 return err;
7689}
7690
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08007691int iwl3945_rfkill_init(struct iwl_priv *priv)
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08007692{
7693 struct device *device = wiphy_dev(priv->hw->wiphy);
7694 int ret = 0;
7695
7696 BUG_ON(device == NULL);
7697
7698 IWL_DEBUG_RF_KILL("Initializing RFKILL.\n");
Adel Gadllah80fcc9e2008-07-01 17:49:50 +02007699 priv->rfkill = rfkill_allocate(device, RFKILL_TYPE_WLAN);
7700 if (!priv->rfkill) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08007701 IWL_ERR(priv, "Unable to allocate rfkill device.\n");
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08007702 ret = -ENOMEM;
7703 goto error;
7704 }
7705
Adel Gadllah80fcc9e2008-07-01 17:49:50 +02007706 priv->rfkill->name = priv->cfg->name;
7707 priv->rfkill->data = priv;
7708 priv->rfkill->state = RFKILL_STATE_UNBLOCKED;
7709 priv->rfkill->toggle_radio = iwl3945_rfkill_soft_rf_kill;
7710 priv->rfkill->user_claim_unsupported = 1;
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08007711
Adel Gadllah80fcc9e2008-07-01 17:49:50 +02007712 priv->rfkill->dev.class->suspend = NULL;
7713 priv->rfkill->dev.class->resume = NULL;
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08007714
Adel Gadllah80fcc9e2008-07-01 17:49:50 +02007715 ret = rfkill_register(priv->rfkill);
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08007716 if (ret) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08007717 IWL_ERR(priv, "Unable to register rfkill: %d\n", ret);
Adel Gadllah80fcc9e2008-07-01 17:49:50 +02007718 goto freed_rfkill;
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08007719 }
7720
7721 IWL_DEBUG_RF_KILL("RFKILL initialization complete.\n");
7722 return ret;
7723
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08007724freed_rfkill:
Adel Gadllah80fcc9e2008-07-01 17:49:50 +02007725 if (priv->rfkill != NULL)
7726 rfkill_free(priv->rfkill);
7727 priv->rfkill = NULL;
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08007728
7729error:
7730 IWL_DEBUG_RF_KILL("RFKILL initialization complete.\n");
7731 return ret;
7732}
7733
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08007734void iwl3945_rfkill_unregister(struct iwl_priv *priv)
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08007735{
Adel Gadllah80fcc9e2008-07-01 17:49:50 +02007736 if (priv->rfkill)
7737 rfkill_unregister(priv->rfkill);
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08007738
Adel Gadllah80fcc9e2008-07-01 17:49:50 +02007739 priv->rfkill = NULL;
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08007740}
7741
7742/* set rf-kill to the right state. */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08007743void iwl3945_rfkill_set_hw_state(struct iwl_priv *priv)
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08007744{
7745
Adel Gadllah80fcc9e2008-07-01 17:49:50 +02007746 if (!priv->rfkill)
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08007747 return;
7748
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08007749 if (iwl_is_rfkill_hw(priv)) {
Adel Gadllah80fcc9e2008-07-01 17:49:50 +02007750 rfkill_force_state(priv->rfkill, RFKILL_STATE_HARD_BLOCKED);
7751 return;
7752 }
7753
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08007754 if (!iwl_is_rfkill_sw(priv))
Adel Gadllah80fcc9e2008-07-01 17:49:50 +02007755 rfkill_force_state(priv->rfkill, RFKILL_STATE_UNBLOCKED);
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08007756 else
Adel Gadllah80fcc9e2008-07-01 17:49:50 +02007757 rfkill_force_state(priv->rfkill, RFKILL_STATE_SOFT_BLOCKED);
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08007758}
7759#endif
7760
Zhu Yib481de92007-09-25 17:54:57 -07007761/*****************************************************************************
7762 *
7763 * driver and module entry point
7764 *
7765 *****************************************************************************/
7766
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007767static struct pci_driver iwl3945_driver = {
Zhu Yib481de92007-09-25 17:54:57 -07007768 .name = DRV_NAME,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007769 .id_table = iwl3945_hw_card_ids,
7770 .probe = iwl3945_pci_probe,
7771 .remove = __devexit_p(iwl3945_pci_remove),
Zhu Yib481de92007-09-25 17:54:57 -07007772#ifdef CONFIG_PM
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007773 .suspend = iwl3945_pci_suspend,
7774 .resume = iwl3945_pci_resume,
Zhu Yib481de92007-09-25 17:54:57 -07007775#endif
7776};
7777
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007778static int __init iwl3945_init(void)
Zhu Yib481de92007-09-25 17:54:57 -07007779{
7780
7781 int ret;
7782 printk(KERN_INFO DRV_NAME ": " DRV_DESCRIPTION ", " DRV_VERSION "\n");
7783 printk(KERN_INFO DRV_NAME ": " DRV_COPYRIGHT "\n");
Reinette Chatre897e1cf2008-03-28 16:21:09 -07007784
7785 ret = iwl3945_rate_control_register();
7786 if (ret) {
Samuel Ortiza3139c52008-12-19 10:37:09 +08007787 printk(KERN_ERR DRV_NAME
7788 "Unable to register rate control algorithm: %d\n", ret);
Reinette Chatre897e1cf2008-03-28 16:21:09 -07007789 return ret;
7790 }
7791
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007792 ret = pci_register_driver(&iwl3945_driver);
Zhu Yib481de92007-09-25 17:54:57 -07007793 if (ret) {
Samuel Ortiza3139c52008-12-19 10:37:09 +08007794 printk(KERN_ERR DRV_NAME "Unable to initialize PCI module\n");
Reinette Chatre897e1cf2008-03-28 16:21:09 -07007795 goto error_register;
Zhu Yib481de92007-09-25 17:54:57 -07007796 }
Zhu Yib481de92007-09-25 17:54:57 -07007797
7798 return ret;
Reinette Chatre897e1cf2008-03-28 16:21:09 -07007799
Reinette Chatre897e1cf2008-03-28 16:21:09 -07007800error_register:
7801 iwl3945_rate_control_unregister();
7802 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07007803}
7804
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007805static void __exit iwl3945_exit(void)
Zhu Yib481de92007-09-25 17:54:57 -07007806{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007807 pci_unregister_driver(&iwl3945_driver);
Reinette Chatre897e1cf2008-03-28 16:21:09 -07007808 iwl3945_rate_control_unregister();
Zhu Yib481de92007-09-25 17:54:57 -07007809}
7810
Reinette Chatrea0987a82008-12-02 12:14:06 -08007811MODULE_FIRMWARE(IWL3945_MODULE_FIRMWARE(IWL3945_UCODE_API_MAX));
Zhu Yi25cb6ca2008-09-11 11:45:22 +08007812
Kolekar, Abhijeetdf878d82008-12-19 10:37:35 +08007813module_param_named(antenna, iwl3945_mod_params.antenna, int, 0444);
Zhu Yib481de92007-09-25 17:54:57 -07007814MODULE_PARM_DESC(antenna, "select antenna (1=Main, 2=Aux, default 0 [both])");
Kolekar, Abhijeetdf878d82008-12-19 10:37:35 +08007815module_param_named(disable, iwl3945_mod_params.disable, int, 0444);
Zhu Yib481de92007-09-25 17:54:57 -07007816MODULE_PARM_DESC(disable, "manually disable the radio (default 0 [radio on])");
Kolekar, Abhijeetdf878d82008-12-19 10:37:35 +08007817module_param_named(hwcrypto, iwl3945_mod_params.sw_crypto, int, 0444);
Zhu Yib481de92007-09-25 17:54:57 -07007818MODULE_PARM_DESC(hwcrypto,
7819 "using hardware crypto engine (default 0 [software])\n");
Kolekar, Abhijeetdf878d82008-12-19 10:37:35 +08007820module_param_named(debug, iwl3945_mod_params.debug, uint, 0444);
Zhu Yib481de92007-09-25 17:54:57 -07007821MODULE_PARM_DESC(debug, "debug output mask");
Kolekar, Abhijeetdf878d82008-12-19 10:37:35 +08007822module_param_named(disable_hw_scan, iwl3945_mod_params.disable_hw_scan, int, 0444);
Zhu Yib481de92007-09-25 17:54:57 -07007823MODULE_PARM_DESC(disable_hw_scan, "disable hardware scanning (default 0)");
7824
Kolekar, Abhijeetdf878d82008-12-19 10:37:35 +08007825module_param_named(queues_num, iwl3945_mod_params.num_of_queues, int, 0444);
Zhu Yib481de92007-09-25 17:54:57 -07007826MODULE_PARM_DESC(queues_num, "number of hw queues.");
7827
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007828module_exit(iwl3945_exit);
7829module_init(iwl3945_init);