blob: 76577b6bc9efaa57e40607c66a01341688b741ba [file] [log] [blame]
Zhu Yib481de92007-09-25 17:54:57 -07001/******************************************************************************
2 *
Reinette Chatre01f81622009-01-08 10:20:02 -08003 * Copyright(c) 2003 - 2009 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 Chatre01f81622009-01-08 10:20:02 -080082#define DRV_COPYRIGHT "Copyright(c) 2003-2009 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,
Samuel Ortiz9c74d9f2009-01-08 10:19:59 -080095 .sw_crypto = 1,
Kolekar, Abhijeetdf878d82008-12-19 10:37:35 +080096 /* the rest are 0 by default */
97};
98
Zhu Yib481de92007-09-25 17:54:57 -070099/*************** DMA-QUEUE-GENERAL-FUNCTIONS *****
100 * DMA services
101 *
102 * Theory of operation
103 *
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800104 * A Tx or Rx queue resides in host DRAM, and is comprised of a circular buffer
105 * of buffer descriptors, each of which points to one or more data buffers for
106 * the device to read from or fill. Driver and device exchange status of each
107 * queue via "read" and "write" pointers. Driver keeps minimum of 2 empty
108 * entries in each circular buffer, to protect against confusing empty and full
109 * queue states.
110 *
111 * The device reads or writes the data in the queues via the device's several
112 * DMA/FIFO channels. Each queue is mapped to a single DMA channel.
Zhu Yib481de92007-09-25 17:54:57 -0700113 *
114 * For Tx queue, there are low mark and high mark limits. If, after queuing
115 * the packet for Tx, free space become < low mark, Tx queue stopped. When
116 * reclaiming packets (on 'tx done IRQ), if free space become > high mark,
117 * Tx queue resumed.
118 *
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800119 * The 3945 operates with six queues: One receive queue, one transmit queue
120 * (#4) for sending commands to the device firmware, and four transmit queues
121 * (#0-3) for data tx via EDCA. An additional 2 HCCA queues are unused.
Zhu Yib481de92007-09-25 17:54:57 -0700122 ***************************************************/
123
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800124/**
125 * iwl3945_queue_init - Initialize queue's high/low-water and read/write indexes
126 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800127static int iwl3945_queue_init(struct iwl_priv *priv, struct iwl_queue *q,
Zhu Yib481de92007-09-25 17:54:57 -0700128 int count, int slots_num, u32 id)
129{
130 q->n_bd = count;
131 q->n_window = slots_num;
132 q->id = id;
133
Tomas Winklerc54b6792008-03-06 17:36:53 -0800134 /* count must be power-of-two size, otherwise iwl_queue_inc_wrap
135 * and iwl_queue_dec_wrap are broken. */
Zhu Yib481de92007-09-25 17:54:57 -0700136 BUG_ON(!is_power_of_2(count));
137
138 /* slots_num must be power-of-two size, otherwise
139 * get_cmd_index is broken. */
140 BUG_ON(!is_power_of_2(slots_num));
141
142 q->low_mark = q->n_window / 4;
143 if (q->low_mark < 4)
144 q->low_mark = 4;
145
146 q->high_mark = q->n_window / 8;
147 if (q->high_mark < 2)
148 q->high_mark = 2;
149
Tomas Winklerfc4b6852007-10-25 17:15:24 +0800150 q->write_ptr = q->read_ptr = 0;
Zhu Yib481de92007-09-25 17:54:57 -0700151
152 return 0;
153}
154
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800155/**
156 * iwl3945_tx_queue_alloc - Alloc driver data and TFD CB for one Tx/cmd queue
157 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800158static int iwl3945_tx_queue_alloc(struct iwl_priv *priv,
Samuel Ortiz188cf6c2008-12-22 11:31:16 +0800159 struct iwl_tx_queue *txq, u32 id)
Zhu Yib481de92007-09-25 17:54:57 -0700160{
161 struct pci_dev *dev = priv->pci_dev;
162
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800163 /* Driver private data, only for Tx (not command) queues,
164 * not shared with device. */
Zhu Yib481de92007-09-25 17:54:57 -0700165 if (id != IWL_CMD_QUEUE_NUM) {
166 txq->txb = kmalloc(sizeof(txq->txb[0]) *
167 TFD_QUEUE_SIZE_MAX, GFP_KERNEL);
168 if (!txq->txb) {
Winkler, Tomas15b16872008-12-19 10:37:33 +0800169 IWL_ERR(priv, "kmalloc for auxiliary BD "
Zhu Yib481de92007-09-25 17:54:57 -0700170 "structures failed\n");
171 goto error;
172 }
173 } else
174 txq->txb = NULL;
175
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800176 /* Circular buffer of transmit frame descriptors (TFDs),
177 * shared with device */
Samuel Ortiz188cf6c2008-12-22 11:31:16 +0800178 txq->tfds39 = pci_alloc_consistent(dev,
179 sizeof(txq->tfds39[0]) * TFD_QUEUE_SIZE_MAX,
Zhu Yib481de92007-09-25 17:54:57 -0700180 &txq->q.dma_addr);
181
Samuel Ortiz188cf6c2008-12-22 11:31:16 +0800182 if (!txq->tfds39) {
Winkler, Tomas15b16872008-12-19 10:37:33 +0800183 IWL_ERR(priv, "pci_alloc_consistent(%zd) failed\n",
Samuel Ortiz188cf6c2008-12-22 11:31:16 +0800184 sizeof(txq->tfds39[0]) * TFD_QUEUE_SIZE_MAX);
Zhu Yib481de92007-09-25 17:54:57 -0700185 goto error;
186 }
187 txq->q.id = id;
188
189 return 0;
190
191 error:
Tomas Winkler3ac7f142008-07-21 02:40:14 +0300192 kfree(txq->txb);
193 txq->txb = NULL;
Zhu Yib481de92007-09-25 17:54:57 -0700194
195 return -ENOMEM;
196}
197
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800198/**
199 * iwl3945_tx_queue_init - Allocate and initialize one tx/cmd queue
200 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800201int iwl3945_tx_queue_init(struct iwl_priv *priv,
Samuel Ortiz188cf6c2008-12-22 11:31:16 +0800202 struct iwl_tx_queue *txq, int slots_num, u32 txq_id)
Zhu Yib481de92007-09-25 17:54:57 -0700203{
Samuel Ortiz188cf6c2008-12-22 11:31:16 +0800204 int len, i;
Zhu Yib481de92007-09-25 17:54:57 -0700205 int rc = 0;
206
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800207 /*
208 * Alloc buffer array for commands (Tx or other types of commands).
209 * For the command queue (#4), allocate command space + one big
210 * command for scan, since scan command is very huge; the system will
211 * not have two scans at the same time, so only one is needed.
212 * For data Tx queues (all other queues), no super-size command
213 * space is needed.
214 */
Samuel Ortiz188cf6c2008-12-22 11:31:16 +0800215 len = sizeof(struct iwl_cmd);
216 for (i = 0; i <= slots_num; i++) {
217 if (i == slots_num) {
218 if (txq_id == IWL_CMD_QUEUE_NUM)
219 len += IWL_MAX_SCAN_SIZE;
220 else
221 continue;
222 }
223
224 txq->cmd[i] = kmalloc(len, GFP_KERNEL);
225 if (!txq->cmd[i])
226 goto err;
227 }
Zhu Yib481de92007-09-25 17:54:57 -0700228
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800229 /* Alloc driver data array and TFD circular buffer */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800230 rc = iwl3945_tx_queue_alloc(priv, txq, txq_id);
Samuel Ortiz188cf6c2008-12-22 11:31:16 +0800231 if (rc)
232 goto err;
Zhu Yib481de92007-09-25 17:54:57 -0700233
Zhu Yib481de92007-09-25 17:54:57 -0700234 txq->need_update = 0;
235
236 /* TFD_QUEUE_SIZE_MAX must be power-of-two size, otherwise
Tomas Winklerc54b6792008-03-06 17:36:53 -0800237 * iwl_queue_inc_wrap and iwl_queue_dec_wrap are broken. */
Zhu Yib481de92007-09-25 17:54:57 -0700238 BUILD_BUG_ON(TFD_QUEUE_SIZE_MAX & (TFD_QUEUE_SIZE_MAX - 1));
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800239
240 /* Initialize queue high/low-water, head/tail indexes */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800241 iwl3945_queue_init(priv, &txq->q, TFD_QUEUE_SIZE_MAX, slots_num, txq_id);
Zhu Yib481de92007-09-25 17:54:57 -0700242
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800243 /* Tell device where to find queue, enable DMA channel. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800244 iwl3945_hw_tx_queue_init(priv, txq);
Zhu Yib481de92007-09-25 17:54:57 -0700245
246 return 0;
Samuel Ortiz188cf6c2008-12-22 11:31:16 +0800247err:
248 for (i = 0; i < slots_num; i++) {
249 kfree(txq->cmd[i]);
250 txq->cmd[i] = NULL;
251 }
252
253 if (txq_id == IWL_CMD_QUEUE_NUM) {
254 kfree(txq->cmd[slots_num]);
255 txq->cmd[slots_num] = NULL;
256 }
257 return -ENOMEM;
Zhu Yib481de92007-09-25 17:54:57 -0700258}
259
260/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800261 * iwl3945_tx_queue_free - Deallocate DMA queue.
Zhu Yib481de92007-09-25 17:54:57 -0700262 * @txq: Transmit queue to deallocate.
263 *
264 * Empty queue by removing and destroying all BD's.
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800265 * Free all buffers.
266 * 0-fill, but do not free "txq" descriptor structure.
Zhu Yib481de92007-09-25 17:54:57 -0700267 */
Samuel Ortiz188cf6c2008-12-22 11:31:16 +0800268void iwl3945_tx_queue_free(struct iwl_priv *priv, struct iwl_tx_queue *txq)
Zhu Yib481de92007-09-25 17:54:57 -0700269{
Samuel Ortizd20b3c62008-12-19 10:37:15 +0800270 struct iwl_queue *q = &txq->q;
Zhu Yib481de92007-09-25 17:54:57 -0700271 struct pci_dev *dev = priv->pci_dev;
Samuel Ortiz188cf6c2008-12-22 11:31:16 +0800272 int len, i;
Zhu Yib481de92007-09-25 17:54:57 -0700273
274 if (q->n_bd == 0)
275 return;
276
277 /* first, empty all BD's */
Tomas Winklerfc4b6852007-10-25 17:15:24 +0800278 for (; q->write_ptr != q->read_ptr;
Tomas Winklerc54b6792008-03-06 17:36:53 -0800279 q->read_ptr = iwl_queue_inc_wrap(q->read_ptr, q->n_bd))
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800280 iwl3945_hw_txq_free_tfd(priv, txq);
Zhu Yib481de92007-09-25 17:54:57 -0700281
Winkler, Tomasc2d79b42008-12-19 10:37:34 +0800282 len = sizeof(struct iwl_cmd) * q->n_window;
Zhu Yib481de92007-09-25 17:54:57 -0700283 if (q->id == IWL_CMD_QUEUE_NUM)
284 len += IWL_MAX_SCAN_SIZE;
285
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800286 /* De-alloc array of command/tx buffers */
Samuel Ortiz188cf6c2008-12-22 11:31:16 +0800287 for (i = 0; i < TFD_TX_CMD_SLOTS; i++)
288 kfree(txq->cmd[i]);
Zhu Yib481de92007-09-25 17:54:57 -0700289
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800290 /* De-alloc circular buffer of TFDs */
Zhu Yib481de92007-09-25 17:54:57 -0700291 if (txq->q.n_bd)
Winkler, Tomasdbb66542008-12-22 11:31:14 +0800292 pci_free_consistent(dev, sizeof(struct iwl3945_tfd) *
Samuel Ortiz188cf6c2008-12-22 11:31:16 +0800293 txq->q.n_bd, txq->tfds39, txq->q.dma_addr);
Zhu Yib481de92007-09-25 17:54:57 -0700294
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800295 /* De-alloc array of per-TFD driver data */
Tomas Winkler3ac7f142008-07-21 02:40:14 +0300296 kfree(txq->txb);
297 txq->txb = NULL;
Zhu Yib481de92007-09-25 17:54:57 -0700298
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800299 /* 0-fill queue descriptor structure */
Zhu Yib481de92007-09-25 17:54:57 -0700300 memset(txq, 0, sizeof(*txq));
301}
302
Zhu Yib481de92007-09-25 17:54:57 -0700303/*************** STATION TABLE MANAGEMENT ****
Ben Cahill9fbab512007-11-29 11:09:47 +0800304 * mac80211 should be examined to determine if sta_info is duplicating
Zhu Yib481de92007-09-25 17:54:57 -0700305 * the functionality provided here
306 */
307
308/**************************************************************/
Ian Schram01ebd062007-10-25 17:15:22 +0800309#if 0 /* temporary disable till we add real remove station */
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800310/**
311 * iwl3945_remove_station - Remove driver's knowledge of station.
312 *
313 * NOTE: This does not remove station from device's station table.
314 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800315static u8 iwl3945_remove_station(struct iwl_priv *priv, const u8 *addr, int is_ap)
Zhu Yib481de92007-09-25 17:54:57 -0700316{
317 int index = IWL_INVALID_STATION;
318 int i;
319 unsigned long flags;
320
321 spin_lock_irqsave(&priv->sta_lock, flags);
322
323 if (is_ap)
324 index = IWL_AP_ID;
325 else if (is_broadcast_ether_addr(addr))
Abhijeet Kolekar3832ec92008-12-19 10:37:26 +0800326 index = priv->hw_params.bcast_sta_id;
Zhu Yib481de92007-09-25 17:54:57 -0700327 else
Abhijeet Kolekar3832ec92008-12-19 10:37:26 +0800328 for (i = IWL_STA_ID; i < priv->hw_params.max_stations; i++)
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800329 if (priv->stations_39[i].used &&
330 !compare_ether_addr(priv->stations_39[i].sta.sta.addr,
Zhu Yib481de92007-09-25 17:54:57 -0700331 addr)) {
332 index = i;
333 break;
334 }
335
336 if (unlikely(index == IWL_INVALID_STATION))
337 goto out;
338
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800339 if (priv->stations_39[index].used) {
340 priv->stations_39[index].used = 0;
Zhu Yib481de92007-09-25 17:54:57 -0700341 priv->num_stations--;
342 }
343
344 BUG_ON(priv->num_stations < 0);
345
346out:
347 spin_unlock_irqrestore(&priv->sta_lock, flags);
348 return 0;
349}
Zhu Yi556f8db2007-09-27 11:27:33 +0800350#endif
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800351
352/**
353 * iwl3945_clear_stations_table - Clear the driver's station table
354 *
355 * NOTE: This does not clear or otherwise alter the device's station table.
356 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800357static void iwl3945_clear_stations_table(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700358{
359 unsigned long flags;
360
361 spin_lock_irqsave(&priv->sta_lock, flags);
362
363 priv->num_stations = 0;
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800364 memset(priv->stations_39, 0, sizeof(priv->stations_39));
Zhu Yib481de92007-09-25 17:54:57 -0700365
366 spin_unlock_irqrestore(&priv->sta_lock, flags);
367}
368
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800369/**
370 * iwl3945_add_station - Add station to station tables in driver and device
371 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800372u8 iwl3945_add_station(struct iwl_priv *priv, const u8 *addr, int is_ap, u8 flags)
Zhu Yib481de92007-09-25 17:54:57 -0700373{
374 int i;
375 int index = IWL_INVALID_STATION;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800376 struct iwl3945_station_entry *station;
Zhu Yib481de92007-09-25 17:54:57 -0700377 unsigned long flags_spin;
Zhu Yic14c5212007-09-27 11:27:35 +0800378 u8 rate;
Zhu Yib481de92007-09-25 17:54:57 -0700379
380 spin_lock_irqsave(&priv->sta_lock, flags_spin);
381 if (is_ap)
382 index = IWL_AP_ID;
383 else if (is_broadcast_ether_addr(addr))
Abhijeet Kolekar3832ec92008-12-19 10:37:26 +0800384 index = priv->hw_params.bcast_sta_id;
Zhu Yib481de92007-09-25 17:54:57 -0700385 else
Abhijeet Kolekar3832ec92008-12-19 10:37:26 +0800386 for (i = IWL_STA_ID; i < priv->hw_params.max_stations; i++) {
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800387 if (!compare_ether_addr(priv->stations_39[i].sta.sta.addr,
Zhu Yib481de92007-09-25 17:54:57 -0700388 addr)) {
389 index = i;
390 break;
391 }
392
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800393 if (!priv->stations_39[i].used &&
Zhu Yib481de92007-09-25 17:54:57 -0700394 index == IWL_INVALID_STATION)
395 index = i;
396 }
397
Ian Schram01ebd062007-10-25 17:15:22 +0800398 /* These two conditions has the same outcome but keep them separate
Zhu Yib481de92007-09-25 17:54:57 -0700399 since they have different meaning */
400 if (unlikely(index == IWL_INVALID_STATION)) {
401 spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
402 return index;
403 }
404
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800405 if (priv->stations_39[index].used &&
406 !compare_ether_addr(priv->stations_39[index].sta.sta.addr, addr)) {
Zhu Yib481de92007-09-25 17:54:57 -0700407 spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
408 return index;
409 }
410
Johannes Berge1749612008-10-27 15:59:26 -0700411 IWL_DEBUG_ASSOC("Add STA ID %d: %pM\n", index, addr);
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800412 station = &priv->stations_39[index];
Zhu Yib481de92007-09-25 17:54:57 -0700413 station->used = 1;
414 priv->num_stations++;
415
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800416 /* Set up the REPLY_ADD_STA command to send to device */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800417 memset(&station->sta, 0, sizeof(struct iwl3945_addsta_cmd));
Zhu Yib481de92007-09-25 17:54:57 -0700418 memcpy(station->sta.sta.addr, addr, ETH_ALEN);
419 station->sta.mode = 0;
420 station->sta.sta.sta_id = index;
421 station->sta.station_flags = 0;
422
Johannes Berg8318d782008-01-24 19:38:38 +0100423 if (priv->band == IEEE80211_BAND_5GHZ)
Tomas Winkler69946332007-10-25 17:15:27 +0800424 rate = IWL_RATE_6M_PLCP;
425 else
426 rate = IWL_RATE_1M_PLCP;
Zhu Yic14c5212007-09-27 11:27:35 +0800427
428 /* Turn on both antennas for the station... */
429 station->sta.rate_n_flags =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800430 iwl3945_hw_set_rate_n_flags(rate, RATE_MCS_ANT_AB_MSK);
Zhu Yic14c5212007-09-27 11:27:35 +0800431
Zhu Yib481de92007-09-25 17:54:57 -0700432 spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800433
434 /* Add station to device's station table */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800435 iwl3945_send_add_station(priv, &station->sta, flags);
Zhu Yib481de92007-09-25 17:54:57 -0700436 return index;
437
438}
439
Zhu Yib481de92007-09-25 17:54:57 -0700440
441/*************** HOST COMMAND QUEUE FUNCTIONS *****/
442
Abhijeet Kolekarc3056062008-11-12 13:14:08 -0800443#define IWL_CMD(x) case x: return #x
Zhu Yib481de92007-09-25 17:54:57 -0700444#define HOST_COMPLETE_TIMEOUT (HZ / 2)
445
446/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800447 * iwl3945_enqueue_hcmd - enqueue a uCode command
Zhu Yib481de92007-09-25 17:54:57 -0700448 * @priv: device private data point
449 * @cmd: a point to the ucode command structure
450 *
451 * The function returns < 0 values to indicate the operation is
452 * failed. On success, it turns the index (> 0) of command in the
453 * command queue.
454 */
Winkler, Tomasc2d79b42008-12-19 10:37:34 +0800455static int iwl3945_enqueue_hcmd(struct iwl_priv *priv, struct iwl_host_cmd *cmd)
Zhu Yib481de92007-09-25 17:54:57 -0700456{
Samuel Ortiz188cf6c2008-12-22 11:31:16 +0800457 struct iwl_tx_queue *txq = &priv->txq[IWL_CMD_QUEUE_NUM];
Samuel Ortizd20b3c62008-12-19 10:37:15 +0800458 struct iwl_queue *q = &txq->q;
Winkler, Tomasdbb66542008-12-22 11:31:14 +0800459 struct iwl3945_tfd *tfd;
Winkler, Tomasc2d79b42008-12-19 10:37:34 +0800460 struct iwl_cmd *out_cmd;
Zhu Yib481de92007-09-25 17:54:57 -0700461 u32 idx;
462 u16 fix_size = (u16)(cmd->len + sizeof(out_cmd->hdr));
463 dma_addr_t phys_addr;
464 int pad;
Samuel Ortiz188cf6c2008-12-22 11:31:16 +0800465 int ret, len;
Zhu Yib481de92007-09-25 17:54:57 -0700466 unsigned long flags;
467
468 /* If any of the command structures end up being larger than
469 * the TFD_MAX_PAYLOAD_SIZE, and it sent as a 'small' command then
470 * we will need to increase the size of the TFD entries */
Winkler, Tomasc2d79b42008-12-19 10:37:34 +0800471 BUG_ON((fix_size > TFD_MAX_PAYLOAD_SIZE) &&
Zhu Yib481de92007-09-25 17:54:57 -0700472 !(cmd->meta.flags & CMD_SIZE_HUGE));
473
Gregory Greenmanc342a1b2008-02-06 11:20:40 -0800474
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +0800475 if (iwl_is_rfkill(priv)) {
Gregory Greenmanc342a1b2008-02-06 11:20:40 -0800476 IWL_DEBUG_INFO("Not sending command - RF KILL");
477 return -EIO;
478 }
479
Samuel Ortizd20b3c62008-12-19 10:37:15 +0800480 if (iwl_queue_space(q) < ((cmd->meta.flags & CMD_ASYNC) ? 2 : 1)) {
Winkler, Tomas15b16872008-12-19 10:37:33 +0800481 IWL_ERR(priv, "No space for Tx\n");
Zhu Yib481de92007-09-25 17:54:57 -0700482 return -ENOSPC;
483 }
484
485 spin_lock_irqsave(&priv->hcmd_lock, flags);
486
Samuel Ortiz188cf6c2008-12-22 11:31:16 +0800487 tfd = &txq->tfds39[q->write_ptr];
Zhu Yib481de92007-09-25 17:54:57 -0700488 memset(tfd, 0, sizeof(*tfd));
489
Tomas Winklerfc4b6852007-10-25 17:15:24 +0800490 idx = get_cmd_index(q, q->write_ptr, cmd->meta.flags & CMD_SIZE_HUGE);
Samuel Ortiz188cf6c2008-12-22 11:31:16 +0800491 out_cmd = txq->cmd[idx];
Zhu Yib481de92007-09-25 17:54:57 -0700492
493 out_cmd->hdr.cmd = cmd->id;
494 memcpy(&out_cmd->meta, &cmd->meta, sizeof(cmd->meta));
495 memcpy(&out_cmd->cmd.payload, cmd->data, cmd->len);
496
497 /* At this point, the out_cmd now has all of the incoming cmd
498 * information */
499
500 out_cmd->hdr.flags = 0;
501 out_cmd->hdr.sequence = cpu_to_le16(QUEUE_TO_SEQ(IWL_CMD_QUEUE_NUM) |
Tomas Winklerfc4b6852007-10-25 17:15:24 +0800502 INDEX_TO_SEQ(q->write_ptr));
Zhu Yib481de92007-09-25 17:54:57 -0700503 if (out_cmd->meta.flags & CMD_SIZE_HUGE)
Tomas Winkler600c0e12008-12-19 10:37:04 +0800504 out_cmd->hdr.sequence |= SEQ_HUGE_FRAME;
Zhu Yib481de92007-09-25 17:54:57 -0700505
Samuel Ortiz188cf6c2008-12-22 11:31:16 +0800506 len = (idx == TFD_CMD_SLOTS) ?
507 IWL_MAX_SCAN_SIZE : sizeof(struct iwl_cmd);
508
509 phys_addr = pci_map_single(priv->pci_dev, out_cmd,
510 len, PCI_DMA_TODEVICE);
511 pci_unmap_addr_set(&out_cmd->meta, mapping, phys_addr);
512 pci_unmap_len_set(&out_cmd->meta, len, len);
513 phys_addr += offsetof(struct iwl_cmd, hdr);
514
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800515 iwl3945_hw_txq_attach_buf_to_tfd(priv, tfd, phys_addr, fix_size);
Zhu Yib481de92007-09-25 17:54:57 -0700516
517 pad = U32_PAD(cmd->len);
Winkler, Tomasdbb66542008-12-22 11:31:14 +0800518 tfd->control_flags |= cpu_to_le32(TFD_CTL_PAD_SET(pad));
Zhu Yib481de92007-09-25 17:54:57 -0700519
520 IWL_DEBUG_HC("Sending command %s (#%x), seq: 0x%04X, "
521 "%d bytes at %d[%d]:%d\n",
522 get_cmd_string(out_cmd->hdr.cmd),
523 out_cmd->hdr.cmd, le16_to_cpu(out_cmd->hdr.sequence),
Tomas Winklerfc4b6852007-10-25 17:15:24 +0800524 fix_size, q->write_ptr, idx, IWL_CMD_QUEUE_NUM);
Zhu Yib481de92007-09-25 17:54:57 -0700525
526 txq->need_update = 1;
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800527
528 /* Increment and update queue's write index */
Tomas Winklerc54b6792008-03-06 17:36:53 -0800529 q->write_ptr = iwl_queue_inc_wrap(q->write_ptr, q->n_bd);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800530 ret = iwl3945_tx_queue_update_write_ptr(priv, txq);
Zhu Yib481de92007-09-25 17:54:57 -0700531
532 spin_unlock_irqrestore(&priv->hcmd_lock, flags);
533 return ret ? ret : idx;
534}
535
Winkler, Tomasc2d79b42008-12-19 10:37:34 +0800536static int iwl3945_send_cmd_async(struct iwl_priv *priv,
537 struct iwl_host_cmd *cmd)
Zhu Yib481de92007-09-25 17:54:57 -0700538{
539 int ret;
540
541 BUG_ON(!(cmd->meta.flags & CMD_ASYNC));
542
543 /* An asynchronous command can not expect an SKB to be set. */
544 BUG_ON(cmd->meta.flags & CMD_WANT_SKB);
545
546 /* An asynchronous command MUST have a callback. */
547 BUG_ON(!cmd->meta.u.callback);
548
549 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
550 return -EBUSY;
551
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800552 ret = iwl3945_enqueue_hcmd(priv, cmd);
Zhu Yib481de92007-09-25 17:54:57 -0700553 if (ret < 0) {
Winkler, Tomas15b16872008-12-19 10:37:33 +0800554 IWL_ERR(priv,
555 "Error sending %s: iwl3945_enqueue_hcmd failed: %d\n",
556 get_cmd_string(cmd->id), ret);
Zhu Yib481de92007-09-25 17:54:57 -0700557 return ret;
558 }
559 return 0;
560}
561
Winkler, Tomasc2d79b42008-12-19 10:37:34 +0800562static int iwl3945_send_cmd_sync(struct iwl_priv *priv,
563 struct iwl_host_cmd *cmd)
Zhu Yib481de92007-09-25 17:54:57 -0700564{
565 int cmd_idx;
566 int ret;
Zhu Yib481de92007-09-25 17:54:57 -0700567
568 BUG_ON(cmd->meta.flags & CMD_ASYNC);
569
570 /* A synchronous command can not have a callback set. */
571 BUG_ON(cmd->meta.u.callback != NULL);
572
Tomas Winklere5472972008-03-28 16:21:12 -0700573 if (test_and_set_bit(STATUS_HCMD_SYNC_ACTIVE, &priv->status)) {
Winkler, Tomas15b16872008-12-19 10:37:33 +0800574 IWL_ERR(priv,
575 "Error sending %s: Already sending a host command\n",
576 get_cmd_string(cmd->id));
Tomas Winklere5472972008-03-28 16:21:12 -0700577 ret = -EBUSY;
578 goto out;
Zhu Yib481de92007-09-25 17:54:57 -0700579 }
580
581 set_bit(STATUS_HCMD_ACTIVE, &priv->status);
582
583 if (cmd->meta.flags & CMD_WANT_SKB)
584 cmd->meta.source = &cmd->meta;
585
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800586 cmd_idx = iwl3945_enqueue_hcmd(priv, cmd);
Zhu Yib481de92007-09-25 17:54:57 -0700587 if (cmd_idx < 0) {
588 ret = cmd_idx;
Winkler, Tomas15b16872008-12-19 10:37:33 +0800589 IWL_ERR(priv,
590 "Error sending %s: iwl3945_enqueue_hcmd failed: %d\n",
591 get_cmd_string(cmd->id), ret);
Zhu Yib481de92007-09-25 17:54:57 -0700592 goto out;
593 }
594
595 ret = wait_event_interruptible_timeout(priv->wait_command_queue,
596 !test_bit(STATUS_HCMD_ACTIVE, &priv->status),
597 HOST_COMPLETE_TIMEOUT);
598 if (!ret) {
599 if (test_bit(STATUS_HCMD_ACTIVE, &priv->status)) {
Winkler, Tomas15b16872008-12-19 10:37:33 +0800600 IWL_ERR(priv, "Error sending %s: time out after %dms\n",
Zhu Yib481de92007-09-25 17:54:57 -0700601 get_cmd_string(cmd->id),
602 jiffies_to_msecs(HOST_COMPLETE_TIMEOUT));
603
604 clear_bit(STATUS_HCMD_ACTIVE, &priv->status);
605 ret = -ETIMEDOUT;
606 goto cancel;
607 }
608 }
609
610 if (test_bit(STATUS_RF_KILL_HW, &priv->status)) {
611 IWL_DEBUG_INFO("Command %s aborted: RF KILL Switch\n",
612 get_cmd_string(cmd->id));
613 ret = -ECANCELED;
614 goto fail;
615 }
616 if (test_bit(STATUS_FW_ERROR, &priv->status)) {
617 IWL_DEBUG_INFO("Command %s failed: FW Error\n",
618 get_cmd_string(cmd->id));
619 ret = -EIO;
620 goto fail;
621 }
622 if ((cmd->meta.flags & CMD_WANT_SKB) && !cmd->meta.u.skb) {
Winkler, Tomas15b16872008-12-19 10:37:33 +0800623 IWL_ERR(priv, "Error: Response NULL in '%s'\n",
Zhu Yib481de92007-09-25 17:54:57 -0700624 get_cmd_string(cmd->id));
625 ret = -EIO;
Zhu Yi73e1a652009-01-08 10:19:58 -0800626 goto cancel;
Zhu Yib481de92007-09-25 17:54:57 -0700627 }
628
629 ret = 0;
630 goto out;
631
632cancel:
633 if (cmd->meta.flags & CMD_WANT_SKB) {
Winkler, Tomasc2d79b42008-12-19 10:37:34 +0800634 struct iwl_cmd *qcmd;
Zhu Yib481de92007-09-25 17:54:57 -0700635
636 /* Cancel the CMD_WANT_SKB flag for the cmd in the
637 * TX cmd queue. Otherwise in case the cmd comes
638 * in later, it will possibly set an invalid
639 * address (cmd->meta.source). */
Samuel Ortiz188cf6c2008-12-22 11:31:16 +0800640 qcmd = priv->txq[IWL_CMD_QUEUE_NUM].cmd[cmd_idx];
Zhu Yib481de92007-09-25 17:54:57 -0700641 qcmd->meta.flags &= ~CMD_WANT_SKB;
642 }
643fail:
644 if (cmd->meta.u.skb) {
645 dev_kfree_skb_any(cmd->meta.u.skb);
646 cmd->meta.u.skb = NULL;
647 }
648out:
Tomas Winklere5472972008-03-28 16:21:12 -0700649 clear_bit(STATUS_HCMD_SYNC_ACTIVE, &priv->status);
Zhu Yib481de92007-09-25 17:54:57 -0700650 return ret;
651}
652
Winkler, Tomasc2d79b42008-12-19 10:37:34 +0800653int iwl3945_send_cmd(struct iwl_priv *priv, struct iwl_host_cmd *cmd)
Zhu Yib481de92007-09-25 17:54:57 -0700654{
Zhu Yib481de92007-09-25 17:54:57 -0700655 if (cmd->meta.flags & CMD_ASYNC)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800656 return iwl3945_send_cmd_async(priv, cmd);
Zhu Yib481de92007-09-25 17:54:57 -0700657
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800658 return iwl3945_send_cmd_sync(priv, cmd);
Zhu Yib481de92007-09-25 17:54:57 -0700659}
660
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800661int iwl3945_send_cmd_pdu(struct iwl_priv *priv, u8 id, u16 len, const void *data)
Zhu Yib481de92007-09-25 17:54:57 -0700662{
Winkler, Tomasc2d79b42008-12-19 10:37:34 +0800663 struct iwl_host_cmd cmd = {
Zhu Yib481de92007-09-25 17:54:57 -0700664 .id = id,
665 .len = len,
666 .data = data,
667 };
668
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800669 return iwl3945_send_cmd_sync(priv, &cmd);
Zhu Yib481de92007-09-25 17:54:57 -0700670}
671
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800672static int __must_check iwl3945_send_cmd_u32(struct iwl_priv *priv, u8 id, u32 val)
Zhu Yib481de92007-09-25 17:54:57 -0700673{
Winkler, Tomasc2d79b42008-12-19 10:37:34 +0800674 struct iwl_host_cmd cmd = {
Zhu Yib481de92007-09-25 17:54:57 -0700675 .id = id,
676 .len = sizeof(val),
677 .data = &val,
678 };
679
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800680 return iwl3945_send_cmd_sync(priv, &cmd);
Zhu Yib481de92007-09-25 17:54:57 -0700681}
682
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800683int iwl3945_send_statistics_request(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700684{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800685 return iwl3945_send_cmd_u32(priv, REPLY_STATISTICS_CMD, 0);
Zhu Yib481de92007-09-25 17:54:57 -0700686}
687
688/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800689 * iwl3945_set_rxon_channel - Set the phymode and channel values in staging RXON
Johannes Berg8318d782008-01-24 19:38:38 +0100690 * @band: 2.4 or 5 GHz band
691 * @channel: Any channel valid for the requested band
Zhu Yib481de92007-09-25 17:54:57 -0700692
Johannes Berg8318d782008-01-24 19:38:38 +0100693 * In addition to setting the staging RXON, priv->band is also set.
Zhu Yib481de92007-09-25 17:54:57 -0700694 *
695 * NOTE: Does not commit to the hardware; it sets appropriate bit fields
Johannes Berg8318d782008-01-24 19:38:38 +0100696 * in the staging RXON flag structure based on the band
Zhu Yib481de92007-09-25 17:54:57 -0700697 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800698static int iwl3945_set_rxon_channel(struct iwl_priv *priv,
Johannes Berg8318d782008-01-24 19:38:38 +0100699 enum ieee80211_band band,
700 u16 channel)
Zhu Yib481de92007-09-25 17:54:57 -0700701{
Johannes Berg8318d782008-01-24 19:38:38 +0100702 if (!iwl3945_get_channel_info(priv, band, channel)) {
Zhu Yib481de92007-09-25 17:54:57 -0700703 IWL_DEBUG_INFO("Could not set channel to %d [%d]\n",
Johannes Berg8318d782008-01-24 19:38:38 +0100704 channel, band);
Zhu Yib481de92007-09-25 17:54:57 -0700705 return -EINVAL;
706 }
707
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800708 if ((le16_to_cpu(priv->staging39_rxon.channel) == channel) &&
Johannes Berg8318d782008-01-24 19:38:38 +0100709 (priv->band == band))
Zhu Yib481de92007-09-25 17:54:57 -0700710 return 0;
711
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800712 priv->staging39_rxon.channel = cpu_to_le16(channel);
Johannes Berg8318d782008-01-24 19:38:38 +0100713 if (band == IEEE80211_BAND_5GHZ)
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800714 priv->staging39_rxon.flags &= ~RXON_FLG_BAND_24G_MSK;
Zhu Yib481de92007-09-25 17:54:57 -0700715 else
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800716 priv->staging39_rxon.flags |= RXON_FLG_BAND_24G_MSK;
Zhu Yib481de92007-09-25 17:54:57 -0700717
Johannes Berg8318d782008-01-24 19:38:38 +0100718 priv->band = band;
Zhu Yib481de92007-09-25 17:54:57 -0700719
Johannes Berg8318d782008-01-24 19:38:38 +0100720 IWL_DEBUG_INFO("Staging channel set to %d [%d]\n", channel, band);
Zhu Yib481de92007-09-25 17:54:57 -0700721
722 return 0;
723}
724
725/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800726 * iwl3945_check_rxon_cmd - validate RXON structure is valid
Zhu Yib481de92007-09-25 17:54:57 -0700727 *
728 * NOTE: This is really only useful during development and can eventually
729 * be #ifdef'd out once the driver is stable and folks aren't actively
730 * making changes
731 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800732static int iwl3945_check_rxon_cmd(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700733{
734 int error = 0;
735 int counter = 1;
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800736 struct iwl3945_rxon_cmd *rxon = &priv->staging39_rxon;
Zhu Yib481de92007-09-25 17:54:57 -0700737
738 if (rxon->flags & RXON_FLG_BAND_24G_MSK) {
739 error |= le32_to_cpu(rxon->flags &
740 (RXON_FLG_TGJ_NARROW_BAND_MSK |
741 RXON_FLG_RADAR_DETECT_MSK));
742 if (error)
Winkler, Tomas39aadf82008-12-19 10:37:32 +0800743 IWL_WARN(priv, "check 24G fields %d | %d\n",
Zhu Yib481de92007-09-25 17:54:57 -0700744 counter++, error);
745 } else {
746 error |= (rxon->flags & RXON_FLG_SHORT_SLOT_MSK) ?
747 0 : le32_to_cpu(RXON_FLG_SHORT_SLOT_MSK);
748 if (error)
Winkler, Tomas39aadf82008-12-19 10:37:32 +0800749 IWL_WARN(priv, "check 52 fields %d | %d\n",
Zhu Yib481de92007-09-25 17:54:57 -0700750 counter++, error);
751 error |= le32_to_cpu(rxon->flags & RXON_FLG_CCK_MSK);
752 if (error)
Winkler, Tomas39aadf82008-12-19 10:37:32 +0800753 IWL_WARN(priv, "check 52 CCK %d | %d\n",
Zhu Yib481de92007-09-25 17:54:57 -0700754 counter++, error);
755 }
756 error |= (rxon->node_addr[0] | rxon->bssid_addr[0]) & 0x1;
757 if (error)
Winkler, Tomas39aadf82008-12-19 10:37:32 +0800758 IWL_WARN(priv, "check mac addr %d | %d\n", counter++, error);
Zhu Yib481de92007-09-25 17:54:57 -0700759
760 /* make sure basic rates 6Mbps and 1Mbps are supported */
761 error |= (((rxon->ofdm_basic_rates & IWL_RATE_6M_MASK) == 0) &&
762 ((rxon->cck_basic_rates & IWL_RATE_1M_MASK) == 0));
763 if (error)
Winkler, Tomas39aadf82008-12-19 10:37:32 +0800764 IWL_WARN(priv, "check basic rate %d | %d\n", counter++, error);
Zhu Yib481de92007-09-25 17:54:57 -0700765
766 error |= (le16_to_cpu(rxon->assoc_id) > 2007);
767 if (error)
Winkler, Tomas39aadf82008-12-19 10:37:32 +0800768 IWL_WARN(priv, "check assoc id %d | %d\n", counter++, error);
Zhu Yib481de92007-09-25 17:54:57 -0700769
770 error |= ((rxon->flags & (RXON_FLG_CCK_MSK | RXON_FLG_SHORT_SLOT_MSK))
771 == (RXON_FLG_CCK_MSK | RXON_FLG_SHORT_SLOT_MSK));
772 if (error)
Winkler, Tomas39aadf82008-12-19 10:37:32 +0800773 IWL_WARN(priv, "check CCK and short slot %d | %d\n",
Zhu Yib481de92007-09-25 17:54:57 -0700774 counter++, error);
775
776 error |= ((rxon->flags & (RXON_FLG_CCK_MSK | RXON_FLG_AUTO_DETECT_MSK))
777 == (RXON_FLG_CCK_MSK | RXON_FLG_AUTO_DETECT_MSK));
778 if (error)
Winkler, Tomas39aadf82008-12-19 10:37:32 +0800779 IWL_WARN(priv, "check CCK & auto detect %d | %d\n",
Zhu Yib481de92007-09-25 17:54:57 -0700780 counter++, error);
781
782 error |= ((rxon->flags & (RXON_FLG_AUTO_DETECT_MSK |
783 RXON_FLG_TGG_PROTECT_MSK)) == RXON_FLG_TGG_PROTECT_MSK);
784 if (error)
Winkler, Tomas39aadf82008-12-19 10:37:32 +0800785 IWL_WARN(priv, "check TGG and auto detect %d | %d\n",
Zhu Yib481de92007-09-25 17:54:57 -0700786 counter++, error);
787
788 if ((rxon->flags & RXON_FLG_DIS_DIV_MSK))
789 error |= ((rxon->flags & (RXON_FLG_ANT_B_MSK |
790 RXON_FLG_ANT_A_MSK)) == 0);
791 if (error)
Winkler, Tomas39aadf82008-12-19 10:37:32 +0800792 IWL_WARN(priv, "check antenna %d %d\n", counter++, error);
Zhu Yib481de92007-09-25 17:54:57 -0700793
794 if (error)
Winkler, Tomas39aadf82008-12-19 10:37:32 +0800795 IWL_WARN(priv, "Tuning to channel %d\n",
Zhu Yib481de92007-09-25 17:54:57 -0700796 le16_to_cpu(rxon->channel));
797
798 if (error) {
Winkler, Tomas15b16872008-12-19 10:37:33 +0800799 IWL_ERR(priv, "Not a valid rxon_assoc_cmd field values\n");
Zhu Yib481de92007-09-25 17:54:57 -0700800 return -1;
801 }
802 return 0;
803}
804
805/**
Ben Cahill9fbab512007-11-29 11:09:47 +0800806 * iwl3945_full_rxon_required - check if full RXON (vs RXON_ASSOC) cmd is needed
Ian Schram01ebd062007-10-25 17:15:22 +0800807 * @priv: staging_rxon is compared to active_rxon
Zhu Yib481de92007-09-25 17:54:57 -0700808 *
Ben Cahill9fbab512007-11-29 11:09:47 +0800809 * If the RXON structure is changing enough to require a new tune,
810 * or is clearing the RXON_FILTER_ASSOC_MSK, then return 1 to indicate that
811 * a new tune (full RXON command, rather than RXON_ASSOC cmd) is required.
Zhu Yib481de92007-09-25 17:54:57 -0700812 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800813static int iwl3945_full_rxon_required(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700814{
815
816 /* These items are only settable from the full RXON command */
Ron Rindjunsky5d1e2322008-06-30 17:23:04 +0800817 if (!(iwl3945_is_associated(priv)) ||
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800818 compare_ether_addr(priv->staging39_rxon.bssid_addr,
819 priv->active39_rxon.bssid_addr) ||
820 compare_ether_addr(priv->staging39_rxon.node_addr,
821 priv->active39_rxon.node_addr) ||
822 compare_ether_addr(priv->staging39_rxon.wlap_bssid_addr,
823 priv->active39_rxon.wlap_bssid_addr) ||
824 (priv->staging39_rxon.dev_type != priv->active39_rxon.dev_type) ||
825 (priv->staging39_rxon.channel != priv->active39_rxon.channel) ||
826 (priv->staging39_rxon.air_propagation !=
827 priv->active39_rxon.air_propagation) ||
828 (priv->staging39_rxon.assoc_id != priv->active39_rxon.assoc_id))
Zhu Yib481de92007-09-25 17:54:57 -0700829 return 1;
830
831 /* flags, filter_flags, ofdm_basic_rates, and cck_basic_rates can
832 * be updated with the RXON_ASSOC command -- however only some
833 * flag transitions are allowed using RXON_ASSOC */
834
835 /* Check if we are not switching bands */
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800836 if ((priv->staging39_rxon.flags & RXON_FLG_BAND_24G_MSK) !=
837 (priv->active39_rxon.flags & RXON_FLG_BAND_24G_MSK))
Zhu Yib481de92007-09-25 17:54:57 -0700838 return 1;
839
840 /* Check if we are switching association toggle */
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800841 if ((priv->staging39_rxon.filter_flags & RXON_FILTER_ASSOC_MSK) !=
842 (priv->active39_rxon.filter_flags & RXON_FILTER_ASSOC_MSK))
Zhu Yib481de92007-09-25 17:54:57 -0700843 return 1;
844
845 return 0;
846}
847
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800848static int iwl3945_send_rxon_assoc(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700849{
850 int rc = 0;
Tomas Winkler3d24a9f2008-12-19 10:37:07 +0800851 struct iwl_rx_packet *res = NULL;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800852 struct iwl3945_rxon_assoc_cmd rxon_assoc;
Winkler, Tomasc2d79b42008-12-19 10:37:34 +0800853 struct iwl_host_cmd cmd = {
Zhu Yib481de92007-09-25 17:54:57 -0700854 .id = REPLY_RXON_ASSOC,
855 .len = sizeof(rxon_assoc),
856 .meta.flags = CMD_WANT_SKB,
857 .data = &rxon_assoc,
858 };
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800859 const struct iwl3945_rxon_cmd *rxon1 = &priv->staging39_rxon;
860 const struct iwl3945_rxon_cmd *rxon2 = &priv->active39_rxon;
Zhu Yib481de92007-09-25 17:54:57 -0700861
862 if ((rxon1->flags == rxon2->flags) &&
863 (rxon1->filter_flags == rxon2->filter_flags) &&
864 (rxon1->cck_basic_rates == rxon2->cck_basic_rates) &&
865 (rxon1->ofdm_basic_rates == rxon2->ofdm_basic_rates)) {
866 IWL_DEBUG_INFO("Using current RXON_ASSOC. Not resending.\n");
867 return 0;
868 }
869
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800870 rxon_assoc.flags = priv->staging39_rxon.flags;
871 rxon_assoc.filter_flags = priv->staging39_rxon.filter_flags;
872 rxon_assoc.ofdm_basic_rates = priv->staging39_rxon.ofdm_basic_rates;
873 rxon_assoc.cck_basic_rates = priv->staging39_rxon.cck_basic_rates;
Zhu Yib481de92007-09-25 17:54:57 -0700874 rxon_assoc.reserved = 0;
875
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800876 rc = iwl3945_send_cmd_sync(priv, &cmd);
Zhu Yib481de92007-09-25 17:54:57 -0700877 if (rc)
878 return rc;
879
Tomas Winkler3d24a9f2008-12-19 10:37:07 +0800880 res = (struct iwl_rx_packet *)cmd.meta.u.skb->data;
Zhu Yib481de92007-09-25 17:54:57 -0700881 if (res->hdr.flags & IWL_CMD_FAILED_MSK) {
Winkler, Tomas15b16872008-12-19 10:37:33 +0800882 IWL_ERR(priv, "Bad return from REPLY_RXON_ASSOC command\n");
Zhu Yib481de92007-09-25 17:54:57 -0700883 rc = -EIO;
884 }
885
886 priv->alloc_rxb_skb--;
887 dev_kfree_skb_any(cmd.meta.u.skb);
888
889 return rc;
890}
891
892/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800893 * iwl3945_commit_rxon - commit staging_rxon to hardware
Zhu Yib481de92007-09-25 17:54:57 -0700894 *
Ian Schram01ebd062007-10-25 17:15:22 +0800895 * The RXON command in staging_rxon is committed to the hardware and
Zhu Yib481de92007-09-25 17:54:57 -0700896 * the active_rxon structure is updated with the new data. This
897 * function correctly transitions out of the RXON_ASSOC_MSK state if
898 * a HW tune is required based on the RXON structure changes.
899 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800900static int iwl3945_commit_rxon(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700901{
902 /* cast away the const for active_rxon in this function */
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800903 struct iwl3945_rxon_cmd *active_rxon = (void *)&priv->active39_rxon;
Zhu Yib481de92007-09-25 17:54:57 -0700904 int rc = 0;
905
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +0800906 if (!iwl_is_alive(priv))
Zhu Yib481de92007-09-25 17:54:57 -0700907 return -1;
908
909 /* always get timestamp with Rx frame */
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800910 priv->staging39_rxon.flags |= RXON_FLG_TSF2HOST_MSK;
Zhu Yib481de92007-09-25 17:54:57 -0700911
912 /* select antenna */
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800913 priv->staging39_rxon.flags &=
Zhu Yib481de92007-09-25 17:54:57 -0700914 ~(RXON_FLG_DIS_DIV_MSK | RXON_FLG_ANT_SEL_MSK);
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800915 priv->staging39_rxon.flags |= iwl3945_get_antenna_flags(priv);
Zhu Yib481de92007-09-25 17:54:57 -0700916
Samuel Ortiza3139c52008-12-19 10:37:09 +0800917 rc = iwl3945_check_rxon_cmd(priv);
Zhu Yib481de92007-09-25 17:54:57 -0700918 if (rc) {
Winkler, Tomas15b16872008-12-19 10:37:33 +0800919 IWL_ERR(priv, "Invalid RXON configuration. Not committing.\n");
Zhu Yib481de92007-09-25 17:54:57 -0700920 return -EINVAL;
921 }
922
923 /* If we don't need to send a full RXON, we can use
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800924 * iwl3945_rxon_assoc_cmd which is used to reconfigure filter
Zhu Yib481de92007-09-25 17:54:57 -0700925 * and other flags for the current radio configuration. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800926 if (!iwl3945_full_rxon_required(priv)) {
927 rc = iwl3945_send_rxon_assoc(priv);
Zhu Yib481de92007-09-25 17:54:57 -0700928 if (rc) {
Winkler, Tomas15b16872008-12-19 10:37:33 +0800929 IWL_ERR(priv, "Error setting RXON_ASSOC "
Zhu Yib481de92007-09-25 17:54:57 -0700930 "configuration (%d).\n", rc);
931 return rc;
932 }
933
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800934 memcpy(active_rxon, &priv->staging39_rxon, sizeof(*active_rxon));
Zhu Yib481de92007-09-25 17:54:57 -0700935
936 return 0;
937 }
938
939 /* If we are currently associated and the new config requires
940 * an RXON_ASSOC and the new config wants the associated mask enabled,
941 * we must clear the associated from the active configuration
942 * before we apply the new config */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800943 if (iwl3945_is_associated(priv) &&
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800944 (priv->staging39_rxon.filter_flags & RXON_FILTER_ASSOC_MSK)) {
Zhu Yib481de92007-09-25 17:54:57 -0700945 IWL_DEBUG_INFO("Toggling associated bit on current RXON\n");
946 active_rxon->filter_flags &= ~RXON_FILTER_ASSOC_MSK;
947
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800948 rc = iwl3945_send_cmd_pdu(priv, REPLY_RXON,
949 sizeof(struct iwl3945_rxon_cmd),
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800950 &priv->active39_rxon);
Zhu Yib481de92007-09-25 17:54:57 -0700951
952 /* If the mask clearing failed then we set
953 * active_rxon back to what it was previously */
954 if (rc) {
955 active_rxon->filter_flags |= RXON_FILTER_ASSOC_MSK;
Winkler, Tomas15b16872008-12-19 10:37:33 +0800956 IWL_ERR(priv, "Error clearing ASSOC_MSK on current "
Zhu Yib481de92007-09-25 17:54:57 -0700957 "configuration (%d).\n", rc);
958 return rc;
959 }
Zhu Yib481de92007-09-25 17:54:57 -0700960 }
961
962 IWL_DEBUG_INFO("Sending RXON\n"
963 "* with%s RXON_FILTER_ASSOC_MSK\n"
964 "* channel = %d\n"
Johannes Berge1749612008-10-27 15:59:26 -0700965 "* bssid = %pM\n",
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800966 ((priv->staging39_rxon.filter_flags &
Zhu Yib481de92007-09-25 17:54:57 -0700967 RXON_FILTER_ASSOC_MSK) ? "" : "out"),
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800968 le16_to_cpu(priv->staging39_rxon.channel),
Johannes Berge1749612008-10-27 15:59:26 -0700969 priv->staging_rxon.bssid_addr);
Zhu Yib481de92007-09-25 17:54:57 -0700970
971 /* Apply the new configuration */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800972 rc = iwl3945_send_cmd_pdu(priv, REPLY_RXON,
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800973 sizeof(struct iwl3945_rxon_cmd), &priv->staging39_rxon);
Zhu Yib481de92007-09-25 17:54:57 -0700974 if (rc) {
Winkler, Tomas15b16872008-12-19 10:37:33 +0800975 IWL_ERR(priv, "Error setting new configuration (%d).\n", rc);
Zhu Yib481de92007-09-25 17:54:57 -0700976 return rc;
977 }
978
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800979 memcpy(active_rxon, &priv->staging39_rxon, sizeof(*active_rxon));
Zhu Yib481de92007-09-25 17:54:57 -0700980
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800981 iwl3945_clear_stations_table(priv);
Zhu Yi556f8db2007-09-27 11:27:33 +0800982
Zhu Yib481de92007-09-25 17:54:57 -0700983 /* If we issue a new RXON command which required a tune then we must
984 * send a new TXPOWER command or we won't be able to Tx any frames */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800985 rc = iwl3945_hw_reg_send_txpower(priv);
Zhu Yib481de92007-09-25 17:54:57 -0700986 if (rc) {
Winkler, Tomas15b16872008-12-19 10:37:33 +0800987 IWL_ERR(priv, "Error setting Tx power (%d).\n", rc);
Zhu Yib481de92007-09-25 17:54:57 -0700988 return rc;
989 }
990
991 /* Add the broadcast address so we can send broadcast frames */
Kolekar, Abhijeetb5323d32008-12-19 10:37:22 +0800992 if (iwl3945_add_station(priv, iwl_bcast_addr, 0, 0) ==
Zhu Yib481de92007-09-25 17:54:57 -0700993 IWL_INVALID_STATION) {
Winkler, Tomas15b16872008-12-19 10:37:33 +0800994 IWL_ERR(priv, "Error adding BROADCAST address for transmit.\n");
Zhu Yib481de92007-09-25 17:54:57 -0700995 return -EIO;
996 }
997
998 /* If we have set the ASSOC_MSK and we are in BSS mode then
999 * add the IWL_AP_ID to the station rate table */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001000 if (iwl3945_is_associated(priv) &&
Johannes Berg05c914f2008-09-11 00:01:58 +02001001 (priv->iw_mode == NL80211_IFTYPE_STATION))
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001002 if (iwl3945_add_station(priv, priv->active39_rxon.bssid_addr, 1, 0)
Zhu Yib481de92007-09-25 17:54:57 -07001003 == IWL_INVALID_STATION) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08001004 IWL_ERR(priv, "Error adding AP address for transmit\n");
Zhu Yib481de92007-09-25 17:54:57 -07001005 return -EIO;
1006 }
1007
Johannes Berg8318d782008-01-24 19:38:38 +01001008 /* Init the hardware's rate fallback order based on the band */
Zhu Yib481de92007-09-25 17:54:57 -07001009 rc = iwl3945_init_hw_rate_table(priv);
1010 if (rc) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08001011 IWL_ERR(priv, "Error setting HW rate table: %02X\n", rc);
Zhu Yib481de92007-09-25 17:54:57 -07001012 return -EIO;
1013 }
1014
1015 return 0;
1016}
1017
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001018static int iwl3945_send_bt_config(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001019{
Tomas Winkler4c897252008-12-19 10:37:05 +08001020 struct iwl_bt_cmd bt_cmd = {
Zhu Yib481de92007-09-25 17:54:57 -07001021 .flags = 3,
1022 .lead_time = 0xAA,
1023 .max_kill = 1,
1024 .kill_ack_mask = 0,
1025 .kill_cts_mask = 0,
1026 };
1027
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001028 return iwl3945_send_cmd_pdu(priv, REPLY_BT_CONFIG,
Tomas Winkler4c897252008-12-19 10:37:05 +08001029 sizeof(bt_cmd), &bt_cmd);
Zhu Yib481de92007-09-25 17:54:57 -07001030}
1031
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001032static int iwl3945_send_scan_abort(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001033{
1034 int rc = 0;
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08001035 struct iwl_rx_packet *res;
Winkler, Tomasc2d79b42008-12-19 10:37:34 +08001036 struct iwl_host_cmd cmd = {
Zhu Yib481de92007-09-25 17:54:57 -07001037 .id = REPLY_SCAN_ABORT_CMD,
1038 .meta.flags = CMD_WANT_SKB,
1039 };
1040
1041 /* If there isn't a scan actively going on in the hardware
1042 * then we are in between scan bands and not actually
1043 * actively scanning, so don't send the abort command */
1044 if (!test_bit(STATUS_SCAN_HW, &priv->status)) {
1045 clear_bit(STATUS_SCAN_ABORTING, &priv->status);
1046 return 0;
1047 }
1048
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001049 rc = iwl3945_send_cmd_sync(priv, &cmd);
Zhu Yib481de92007-09-25 17:54:57 -07001050 if (rc) {
1051 clear_bit(STATUS_SCAN_ABORTING, &priv->status);
1052 return rc;
1053 }
1054
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08001055 res = (struct iwl_rx_packet *)cmd.meta.u.skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07001056 if (res->u.status != CAN_ABORT_STATUS) {
1057 /* The scan abort will return 1 for success or
1058 * 2 for "failure". A failure condition can be
1059 * due to simply not being in an active scan which
1060 * can occur if we send the scan abort before we
1061 * the microcode has notified us that a scan is
1062 * completed. */
1063 IWL_DEBUG_INFO("SCAN_ABORT returned %d.\n", res->u.status);
1064 clear_bit(STATUS_SCAN_ABORTING, &priv->status);
1065 clear_bit(STATUS_SCAN_HW, &priv->status);
1066 }
1067
1068 dev_kfree_skb_any(cmd.meta.u.skb);
1069
1070 return rc;
1071}
1072
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001073static int iwl3945_add_sta_sync_callback(struct iwl_priv *priv,
Winkler, Tomasc2d79b42008-12-19 10:37:34 +08001074 struct iwl_cmd *cmd, struct sk_buff *skb)
Zhu Yib481de92007-09-25 17:54:57 -07001075{
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08001076 struct iwl_rx_packet *res = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07001077
1078 if (!skb) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08001079 IWL_ERR(priv, "Error: Response NULL in REPLY_ADD_STA.\n");
Zhu Yib481de92007-09-25 17:54:57 -07001080 return 1;
1081 }
1082
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08001083 res = (struct iwl_rx_packet *)skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07001084 if (res->hdr.flags & IWL_CMD_FAILED_MSK) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08001085 IWL_ERR(priv, "Bad return from REPLY_ADD_STA (0x%08X)\n",
Zhu Yib481de92007-09-25 17:54:57 -07001086 res->hdr.flags);
1087 return 1;
1088 }
1089
1090 switch (res->u.add_sta.status) {
1091 case ADD_STA_SUCCESS_MSK:
1092 break;
1093 default:
1094 break;
1095 }
1096
1097 /* We didn't cache the SKB; let the caller free it */
1098 return 1;
1099}
1100
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001101int iwl3945_send_add_station(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001102 struct iwl3945_addsta_cmd *sta, u8 flags)
Zhu Yib481de92007-09-25 17:54:57 -07001103{
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08001104 struct iwl_rx_packet *res = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07001105 int rc = 0;
Winkler, Tomasc2d79b42008-12-19 10:37:34 +08001106 struct iwl_host_cmd cmd = {
Zhu Yib481de92007-09-25 17:54:57 -07001107 .id = REPLY_ADD_STA,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001108 .len = sizeof(struct iwl3945_addsta_cmd),
Zhu Yib481de92007-09-25 17:54:57 -07001109 .meta.flags = flags,
1110 .data = sta,
1111 };
1112
1113 if (flags & CMD_ASYNC)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001114 cmd.meta.u.callback = iwl3945_add_sta_sync_callback;
Zhu Yib481de92007-09-25 17:54:57 -07001115 else
1116 cmd.meta.flags |= CMD_WANT_SKB;
1117
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001118 rc = iwl3945_send_cmd(priv, &cmd);
Zhu Yib481de92007-09-25 17:54:57 -07001119
1120 if (rc || (flags & CMD_ASYNC))
1121 return rc;
1122
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08001123 res = (struct iwl_rx_packet *)cmd.meta.u.skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07001124 if (res->hdr.flags & IWL_CMD_FAILED_MSK) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08001125 IWL_ERR(priv, "Bad return from REPLY_ADD_STA (0x%08X)\n",
Zhu Yib481de92007-09-25 17:54:57 -07001126 res->hdr.flags);
1127 rc = -EIO;
1128 }
1129
1130 if (rc == 0) {
1131 switch (res->u.add_sta.status) {
1132 case ADD_STA_SUCCESS_MSK:
1133 IWL_DEBUG_INFO("REPLY_ADD_STA PASSED\n");
1134 break;
1135 default:
1136 rc = -EIO;
Winkler, Tomas39aadf82008-12-19 10:37:32 +08001137 IWL_WARN(priv, "REPLY_ADD_STA failed\n");
Zhu Yib481de92007-09-25 17:54:57 -07001138 break;
1139 }
1140 }
1141
1142 priv->alloc_rxb_skb--;
1143 dev_kfree_skb_any(cmd.meta.u.skb);
1144
1145 return rc;
1146}
1147
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001148static int iwl3945_update_sta_key_info(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07001149 struct ieee80211_key_conf *keyconf,
1150 u8 sta_id)
1151{
1152 unsigned long flags;
1153 __le16 key_flags = 0;
1154
1155 switch (keyconf->alg) {
1156 case ALG_CCMP:
1157 key_flags |= STA_KEY_FLG_CCMP;
1158 key_flags |= cpu_to_le16(
1159 keyconf->keyidx << STA_KEY_FLG_KEYID_POS);
1160 key_flags &= ~STA_KEY_FLG_INVALID;
1161 break;
1162 case ALG_TKIP:
1163 case ALG_WEP:
Zhu Yib481de92007-09-25 17:54:57 -07001164 default:
1165 return -EINVAL;
1166 }
1167 spin_lock_irqsave(&priv->sta_lock, flags);
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001168 priv->stations_39[sta_id].keyinfo.alg = keyconf->alg;
1169 priv->stations_39[sta_id].keyinfo.keylen = keyconf->keylen;
1170 memcpy(priv->stations_39[sta_id].keyinfo.key, keyconf->key,
Zhu Yib481de92007-09-25 17:54:57 -07001171 keyconf->keylen);
1172
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001173 memcpy(priv->stations_39[sta_id].sta.key.key, keyconf->key,
Zhu Yib481de92007-09-25 17:54:57 -07001174 keyconf->keylen);
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001175 priv->stations_39[sta_id].sta.key.key_flags = key_flags;
1176 priv->stations_39[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
1177 priv->stations_39[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07001178
1179 spin_unlock_irqrestore(&priv->sta_lock, flags);
1180
1181 IWL_DEBUG_INFO("hwcrypto: modify ucode station key info\n");
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001182 iwl3945_send_add_station(priv, &priv->stations_39[sta_id].sta, 0);
Zhu Yib481de92007-09-25 17:54:57 -07001183 return 0;
1184}
1185
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001186static int iwl3945_clear_sta_key_info(struct iwl_priv *priv, u8 sta_id)
Zhu Yib481de92007-09-25 17:54:57 -07001187{
1188 unsigned long flags;
1189
1190 spin_lock_irqsave(&priv->sta_lock, flags);
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001191 memset(&priv->stations_39[sta_id].keyinfo, 0, sizeof(struct iwl3945_hw_key));
1192 memset(&priv->stations_39[sta_id].sta.key, 0,
Tomas Winkler4c897252008-12-19 10:37:05 +08001193 sizeof(struct iwl4965_keyinfo));
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001194 priv->stations_39[sta_id].sta.key.key_flags = STA_KEY_FLG_NO_ENC;
1195 priv->stations_39[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
1196 priv->stations_39[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07001197 spin_unlock_irqrestore(&priv->sta_lock, flags);
1198
1199 IWL_DEBUG_INFO("hwcrypto: clear ucode station key info\n");
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001200 iwl3945_send_add_station(priv, &priv->stations_39[sta_id].sta, 0);
Zhu Yib481de92007-09-25 17:54:57 -07001201 return 0;
1202}
1203
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001204static void iwl3945_clear_free_frames(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001205{
1206 struct list_head *element;
1207
1208 IWL_DEBUG_INFO("%d frames on pre-allocated heap on clear.\n",
1209 priv->frames_count);
1210
1211 while (!list_empty(&priv->free_frames)) {
1212 element = priv->free_frames.next;
1213 list_del(element);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001214 kfree(list_entry(element, struct iwl3945_frame, list));
Zhu Yib481de92007-09-25 17:54:57 -07001215 priv->frames_count--;
1216 }
1217
1218 if (priv->frames_count) {
Winkler, Tomas39aadf82008-12-19 10:37:32 +08001219 IWL_WARN(priv, "%d frames still in use. Did we lose one?\n",
Zhu Yib481de92007-09-25 17:54:57 -07001220 priv->frames_count);
1221 priv->frames_count = 0;
1222 }
1223}
1224
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001225static struct iwl3945_frame *iwl3945_get_free_frame(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001226{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001227 struct iwl3945_frame *frame;
Zhu Yib481de92007-09-25 17:54:57 -07001228 struct list_head *element;
1229 if (list_empty(&priv->free_frames)) {
1230 frame = kzalloc(sizeof(*frame), GFP_KERNEL);
1231 if (!frame) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08001232 IWL_ERR(priv, "Could not allocate frame!\n");
Zhu Yib481de92007-09-25 17:54:57 -07001233 return NULL;
1234 }
1235
1236 priv->frames_count++;
1237 return frame;
1238 }
1239
1240 element = priv->free_frames.next;
1241 list_del(element);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001242 return list_entry(element, struct iwl3945_frame, list);
Zhu Yib481de92007-09-25 17:54:57 -07001243}
1244
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001245static void iwl3945_free_frame(struct iwl_priv *priv, struct iwl3945_frame *frame)
Zhu Yib481de92007-09-25 17:54:57 -07001246{
1247 memset(frame, 0, sizeof(*frame));
1248 list_add(&frame->list, &priv->free_frames);
1249}
1250
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001251unsigned int iwl3945_fill_beacon_frame(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07001252 struct ieee80211_hdr *hdr,
Rami Rosen73ec1cc2008-12-16 09:37:07 +02001253 int left)
Zhu Yib481de92007-09-25 17:54:57 -07001254{
1255
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001256 if (!iwl3945_is_associated(priv) || !priv->ibss_beacon ||
Johannes Berg05c914f2008-09-11 00:01:58 +02001257 ((priv->iw_mode != NL80211_IFTYPE_ADHOC) &&
1258 (priv->iw_mode != NL80211_IFTYPE_AP)))
Zhu Yib481de92007-09-25 17:54:57 -07001259 return 0;
1260
1261 if (priv->ibss_beacon->len > left)
1262 return 0;
1263
1264 memcpy(hdr, priv->ibss_beacon->data, priv->ibss_beacon->len);
1265
1266 return priv->ibss_beacon->len;
1267}
1268
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001269static u8 iwl3945_rate_get_lowest_plcp(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001270{
1271 u8 i;
Kolekar, Abhijeetc24f0812008-11-07 09:58:44 -08001272 int rate_mask;
1273
1274 /* Set rate mask*/
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001275 if (priv->staging39_rxon.flags & RXON_FLG_BAND_24G_MSK)
Chatre, Reinettedbce56a2008-11-12 13:14:07 -08001276 rate_mask = priv->active_rate_basic & IWL_CCK_RATES_MASK;
Kolekar, Abhijeetc24f0812008-11-07 09:58:44 -08001277 else
Chatre, Reinettedbce56a2008-11-12 13:14:07 -08001278 rate_mask = priv->active_rate_basic & IWL_OFDM_RATES_MASK;
Zhu Yib481de92007-09-25 17:54:57 -07001279
1280 for (i = IWL_RATE_1M_INDEX; i != IWL_RATE_INVALID;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001281 i = iwl3945_rates[i].next_ieee) {
Zhu Yib481de92007-09-25 17:54:57 -07001282 if (rate_mask & (1 << i))
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001283 return iwl3945_rates[i].plcp;
Zhu Yib481de92007-09-25 17:54:57 -07001284 }
1285
Kolekar, Abhijeetc24f0812008-11-07 09:58:44 -08001286 /* No valid rate was found. Assign the lowest one */
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001287 if (priv->staging39_rxon.flags & RXON_FLG_BAND_24G_MSK)
Kolekar, Abhijeetc24f0812008-11-07 09:58:44 -08001288 return IWL_RATE_1M_PLCP;
1289 else
1290 return IWL_RATE_6M_PLCP;
Zhu Yib481de92007-09-25 17:54:57 -07001291}
1292
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001293static int iwl3945_send_beacon_cmd(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001294{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001295 struct iwl3945_frame *frame;
Zhu Yib481de92007-09-25 17:54:57 -07001296 unsigned int frame_size;
1297 int rc;
1298 u8 rate;
1299
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001300 frame = iwl3945_get_free_frame(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001301
1302 if (!frame) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08001303 IWL_ERR(priv, "Could not obtain free frame buffer for beacon "
Zhu Yib481de92007-09-25 17:54:57 -07001304 "command.\n");
1305 return -ENOMEM;
1306 }
1307
Kolekar, Abhijeetc24f0812008-11-07 09:58:44 -08001308 rate = iwl3945_rate_get_lowest_plcp(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001309
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001310 frame_size = iwl3945_hw_get_beacon_cmd(priv, frame, rate);
Zhu Yib481de92007-09-25 17:54:57 -07001311
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001312 rc = iwl3945_send_cmd_pdu(priv, REPLY_TX_BEACON, frame_size,
Zhu Yib481de92007-09-25 17:54:57 -07001313 &frame->u.cmd[0]);
1314
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001315 iwl3945_free_frame(priv, frame);
Zhu Yib481de92007-09-25 17:54:57 -07001316
1317 return rc;
1318}
1319
1320/******************************************************************************
1321 *
1322 * EEPROM related functions
1323 *
1324 ******************************************************************************/
1325
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001326static void get_eeprom_mac(struct iwl_priv *priv, u8 *mac)
Zhu Yib481de92007-09-25 17:54:57 -07001327{
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001328 memcpy(mac, priv->eeprom39.mac_address, 6);
Zhu Yib481de92007-09-25 17:54:57 -07001329}
1330
Reinette Chatre74a3a252008-01-23 10:15:19 -08001331/*
1332 * Clear the OWNER_MSK, to establish driver (instead of uCode running on
1333 * embedded controller) as EEPROM reader; each read is a series of pulses
1334 * to/from the EEPROM chip, not a single event, so even reads could conflict
1335 * if they weren't arbitrated by some ownership mechanism. Here, the driver
1336 * simply claims ownership, which should be safe when this function is called
1337 * (i.e. before loading uCode!).
1338 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001339static inline int iwl3945_eeprom_acquire_semaphore(struct iwl_priv *priv)
Reinette Chatre74a3a252008-01-23 10:15:19 -08001340{
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08001341 _iwl_clear_bit(priv, CSR_EEPROM_GP, CSR_EEPROM_GP_IF_OWNER_MSK);
Reinette Chatre74a3a252008-01-23 10:15:19 -08001342 return 0;
1343}
1344
Zhu Yib481de92007-09-25 17:54:57 -07001345/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001346 * iwl3945_eeprom_init - read EEPROM contents
Zhu Yib481de92007-09-25 17:54:57 -07001347 *
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001348 * Load the EEPROM contents from adapter into priv->eeprom39
Zhu Yib481de92007-09-25 17:54:57 -07001349 *
1350 * NOTE: This routine uses the non-debug IO access functions.
1351 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001352int iwl3945_eeprom_init(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001353{
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001354 u16 *e = (u16 *)&priv->eeprom39;
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08001355 u32 gp = iwl_read32(priv, CSR_EEPROM_GP);
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001356 int sz = sizeof(priv->eeprom39);
Zhu, Yi3d5717a2008-12-11 10:33:36 -08001357 int ret;
Zhu Yib481de92007-09-25 17:54:57 -07001358 u16 addr;
1359
1360 /* The EEPROM structure has several padding buffers within it
1361 * and when adding new EEPROM maps is subject to programmer errors
1362 * which may be very difficult to identify without explicitly
1363 * checking the resulting size of the eeprom map. */
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001364 BUILD_BUG_ON(sizeof(priv->eeprom39) != IWL_EEPROM_IMAGE_SIZE);
Zhu Yib481de92007-09-25 17:54:57 -07001365
1366 if ((gp & CSR_EEPROM_GP_VALID_MSK) == CSR_EEPROM_GP_BAD_SIGNATURE) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08001367 IWL_ERR(priv, "EEPROM not found, EEPROM_GP=0x%08x\n", gp);
Zhu Yib481de92007-09-25 17:54:57 -07001368 return -ENOENT;
1369 }
1370
Cahill, Ben M6440adb2007-11-29 11:09:55 +08001371 /* Make sure driver (instead of uCode) is allowed to read EEPROM */
Zhu, Yi3d5717a2008-12-11 10:33:36 -08001372 ret = iwl3945_eeprom_acquire_semaphore(priv);
1373 if (ret < 0) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08001374 IWL_ERR(priv, "Failed to acquire EEPROM semaphore.\n");
Zhu Yib481de92007-09-25 17:54:57 -07001375 return -ENOENT;
1376 }
1377
1378 /* eeprom is an array of 16bit values */
1379 for (addr = 0; addr < sz; addr += sizeof(u16)) {
Zhu, Yi3d5717a2008-12-11 10:33:36 -08001380 u32 r;
1381
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08001382 _iwl_write32(priv, CSR_EEPROM_REG,
Zhu, Yi3d5717a2008-12-11 10:33:36 -08001383 CSR_EEPROM_REG_MSK_ADDR & (addr << 1));
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08001384 _iwl_clear_bit(priv, CSR_EEPROM_REG, CSR_EEPROM_REG_BIT_CMD);
1385 ret = iwl_poll_direct_bit(priv, CSR_EEPROM_REG,
Zhu, Yi3d5717a2008-12-11 10:33:36 -08001386 CSR_EEPROM_REG_READ_VALID_MSK,
1387 IWL_EEPROM_ACCESS_TIMEOUT);
1388 if (ret < 0) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08001389 IWL_ERR(priv, "Time out reading EEPROM[%d]\n", addr);
Zhu, Yi3d5717a2008-12-11 10:33:36 -08001390 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07001391 }
Zhu, Yi3d5717a2008-12-11 10:33:36 -08001392
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08001393 r = _iwl_read_direct32(priv, CSR_EEPROM_REG);
Tomas Winkler58ff6d42008-02-13 02:47:54 +02001394 e[addr / 2] = le16_to_cpu((__force __le16)(r >> 16));
Zhu Yib481de92007-09-25 17:54:57 -07001395 }
1396
1397 return 0;
1398}
1399
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001400static void iwl3945_unset_hw_params(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001401{
Abhijeet Kolekar3832ec92008-12-19 10:37:26 +08001402 if (priv->shared_virt)
Zhu Yib481de92007-09-25 17:54:57 -07001403 pci_free_consistent(priv->pci_dev,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001404 sizeof(struct iwl3945_shared),
Abhijeet Kolekar3832ec92008-12-19 10:37:26 +08001405 priv->shared_virt,
1406 priv->shared_phys);
Zhu Yib481de92007-09-25 17:54:57 -07001407}
1408
1409/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001410 * iwl3945_supported_rate_to_ie - fill in the supported rate in IE field
Zhu Yib481de92007-09-25 17:54:57 -07001411 *
1412 * return : set the bit for each supported rate insert in ie
1413 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001414static u16 iwl3945_supported_rate_to_ie(u8 *ie, u16 supported_rate,
Tomas Winklerc7c46672007-10-18 02:04:15 +02001415 u16 basic_rate, int *left)
Zhu Yib481de92007-09-25 17:54:57 -07001416{
1417 u16 ret_rates = 0, bit;
1418 int i;
Tomas Winklerc7c46672007-10-18 02:04:15 +02001419 u8 *cnt = ie;
1420 u8 *rates = ie + 1;
Zhu Yib481de92007-09-25 17:54:57 -07001421
1422 for (bit = 1, i = 0; i < IWL_RATE_COUNT; i++, bit <<= 1) {
1423 if (bit & supported_rate) {
1424 ret_rates |= bit;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001425 rates[*cnt] = iwl3945_rates[i].ieee |
Tomas Winklerc7c46672007-10-18 02:04:15 +02001426 ((bit & basic_rate) ? 0x80 : 0x00);
1427 (*cnt)++;
1428 (*left)--;
1429 if ((*left <= 0) ||
1430 (*cnt >= IWL_SUPPORTED_RATES_IE_LEN))
Zhu Yib481de92007-09-25 17:54:57 -07001431 break;
1432 }
1433 }
1434
1435 return ret_rates;
1436}
1437
1438/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001439 * iwl3945_fill_probe_req - fill in all required fields and IE for probe request
Zhu Yib481de92007-09-25 17:54:57 -07001440 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001441static u16 iwl3945_fill_probe_req(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07001442 struct ieee80211_mgmt *frame,
Johannes Berg430cfe92008-10-28 18:06:02 +01001443 int left)
Zhu Yib481de92007-09-25 17:54:57 -07001444{
1445 int len = 0;
1446 u8 *pos = NULL;
Tomas Winklerc7c46672007-10-18 02:04:15 +02001447 u16 active_rates, ret_rates, cck_rates;
Zhu Yib481de92007-09-25 17:54:57 -07001448
1449 /* Make sure there is enough space for the probe request,
1450 * two mandatory IEs and the data */
1451 left -= 24;
1452 if (left < 0)
1453 return 0;
1454 len += 24;
1455
1456 frame->frame_control = cpu_to_le16(IEEE80211_STYPE_PROBE_REQ);
Kolekar, Abhijeetb5323d32008-12-19 10:37:22 +08001457 memcpy(frame->da, iwl_bcast_addr, ETH_ALEN);
Zhu Yib481de92007-09-25 17:54:57 -07001458 memcpy(frame->sa, priv->mac_addr, ETH_ALEN);
Kolekar, Abhijeetb5323d32008-12-19 10:37:22 +08001459 memcpy(frame->bssid, iwl_bcast_addr, ETH_ALEN);
Zhu Yib481de92007-09-25 17:54:57 -07001460 frame->seq_ctrl = 0;
1461
1462 /* fill in our indirect SSID IE */
1463 /* ...next IE... */
1464
1465 left -= 2;
1466 if (left < 0)
1467 return 0;
1468 len += 2;
1469 pos = &(frame->u.probe_req.variable[0]);
1470 *pos++ = WLAN_EID_SSID;
1471 *pos++ = 0;
1472
Zhu Yib481de92007-09-25 17:54:57 -07001473 /* fill in supported rate */
1474 /* ...next IE... */
1475 left -= 2;
1476 if (left < 0)
1477 return 0;
Tomas Winklerc7c46672007-10-18 02:04:15 +02001478
Zhu Yib481de92007-09-25 17:54:57 -07001479 /* ... fill it in... */
1480 *pos++ = WLAN_EID_SUPP_RATES;
1481 *pos = 0;
Tomas Winklerc7c46672007-10-18 02:04:15 +02001482
1483 priv->active_rate = priv->rates_mask;
1484 active_rates = priv->active_rate;
Zhu Yib481de92007-09-25 17:54:57 -07001485 priv->active_rate_basic = priv->rates_mask & IWL_BASIC_RATES_MASK;
1486
Tomas Winklerc7c46672007-10-18 02:04:15 +02001487 cck_rates = IWL_CCK_RATES_MASK & active_rates;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001488 ret_rates = iwl3945_supported_rate_to_ie(pos, cck_rates,
Tomas Winklerc7c46672007-10-18 02:04:15 +02001489 priv->active_rate_basic, &left);
1490 active_rates &= ~ret_rates;
1491
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001492 ret_rates = iwl3945_supported_rate_to_ie(pos, active_rates,
Tomas Winklerc7c46672007-10-18 02:04:15 +02001493 priv->active_rate_basic, &left);
1494 active_rates &= ~ret_rates;
1495
Zhu Yib481de92007-09-25 17:54:57 -07001496 len += 2 + *pos;
1497 pos += (*pos) + 1;
Tomas Winklerc7c46672007-10-18 02:04:15 +02001498 if (active_rates == 0)
Zhu Yib481de92007-09-25 17:54:57 -07001499 goto fill_end;
1500
1501 /* fill in supported extended rate */
1502 /* ...next IE... */
1503 left -= 2;
1504 if (left < 0)
1505 return 0;
1506 /* ... fill it in... */
1507 *pos++ = WLAN_EID_EXT_SUPP_RATES;
1508 *pos = 0;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001509 iwl3945_supported_rate_to_ie(pos, active_rates,
Tomas Winklerc7c46672007-10-18 02:04:15 +02001510 priv->active_rate_basic, &left);
Zhu Yib481de92007-09-25 17:54:57 -07001511 if (*pos > 0)
1512 len += 2 + *pos;
1513
1514 fill_end:
1515 return (u16)len;
1516}
1517
1518/*
1519 * QoS support
1520*/
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001521static int iwl3945_send_qos_params_command(struct iwl_priv *priv,
Tomas Winkler4c897252008-12-19 10:37:05 +08001522 struct iwl_qosparam_cmd *qos)
Zhu Yib481de92007-09-25 17:54:57 -07001523{
1524
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001525 return iwl3945_send_cmd_pdu(priv, REPLY_QOS_PARAM,
Tomas Winkler4c897252008-12-19 10:37:05 +08001526 sizeof(struct iwl_qosparam_cmd), qos);
Zhu Yib481de92007-09-25 17:54:57 -07001527}
1528
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001529static void iwl3945_activate_qos(struct iwl_priv *priv, u8 force)
Zhu Yib481de92007-09-25 17:54:57 -07001530{
1531 unsigned long flags;
1532
Zhu Yib481de92007-09-25 17:54:57 -07001533 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
1534 return;
1535
Zhu Yib481de92007-09-25 17:54:57 -07001536 spin_lock_irqsave(&priv->lock, flags);
1537 priv->qos_data.def_qos_parm.qos_flags = 0;
1538
1539 if (priv->qos_data.qos_cap.q_AP.queue_request &&
1540 !priv->qos_data.qos_cap.q_AP.txop_request)
1541 priv->qos_data.def_qos_parm.qos_flags |=
1542 QOS_PARAM_FLG_TXOP_TYPE_MSK;
1543
1544 if (priv->qos_data.qos_active)
1545 priv->qos_data.def_qos_parm.qos_flags |=
1546 QOS_PARAM_FLG_UPDATE_EDCA_MSK;
1547
1548 spin_unlock_irqrestore(&priv->lock, flags);
1549
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001550 if (force || iwl3945_is_associated(priv)) {
Tomas Winklera96a27f2008-10-23 23:48:56 -07001551 IWL_DEBUG_QOS("send QoS cmd with QoS active %d \n",
Zhu Yib481de92007-09-25 17:54:57 -07001552 priv->qos_data.qos_active);
1553
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001554 iwl3945_send_qos_params_command(priv,
Zhu Yib481de92007-09-25 17:54:57 -07001555 &(priv->qos_data.def_qos_parm));
1556 }
1557}
1558
Zhu Yib481de92007-09-25 17:54:57 -07001559/*
1560 * Power management (not Tx power!) functions
1561 */
1562#define MSEC_TO_USEC 1024
1563
Tomas Winkler600c0e12008-12-19 10:37:04 +08001564
1565#define NOSLP __constant_cpu_to_le16(0), 0, 0
1566#define SLP IWL_POWER_DRIVER_ALLOW_SLEEP_MSK, 0, 0
Zhu Yib481de92007-09-25 17:54:57 -07001567#define SLP_TIMEOUT(T) __constant_cpu_to_le32((T) * MSEC_TO_USEC)
1568#define SLP_VEC(X0, X1, X2, X3, X4) {__constant_cpu_to_le32(X0), \
1569 __constant_cpu_to_le32(X1), \
1570 __constant_cpu_to_le32(X2), \
1571 __constant_cpu_to_le32(X3), \
1572 __constant_cpu_to_le32(X4)}
1573
Zhu Yib481de92007-09-25 17:54:57 -07001574/* default power management (not Tx power) table values */
Tomas Winklera96a27f2008-10-23 23:48:56 -07001575/* for TIM 0-10 */
Samuel Ortiz1125eff2008-12-19 10:37:14 +08001576static struct iwl_power_vec_entry range_0[IWL39_POWER_AC] = {
Zhu Yib481de92007-09-25 17:54:57 -07001577 {{NOSLP, SLP_TIMEOUT(0), SLP_TIMEOUT(0), SLP_VEC(0, 0, 0, 0, 0)}, 0},
1578 {{SLP, SLP_TIMEOUT(200), SLP_TIMEOUT(500), SLP_VEC(1, 2, 3, 4, 4)}, 0},
1579 {{SLP, SLP_TIMEOUT(200), SLP_TIMEOUT(300), SLP_VEC(2, 4, 6, 7, 7)}, 0},
1580 {{SLP, SLP_TIMEOUT(50), SLP_TIMEOUT(100), SLP_VEC(2, 6, 9, 9, 10)}, 0},
1581 {{SLP, SLP_TIMEOUT(50), SLP_TIMEOUT(25), SLP_VEC(2, 7, 9, 9, 10)}, 1},
1582 {{SLP, SLP_TIMEOUT(25), SLP_TIMEOUT(25), SLP_VEC(4, 7, 10, 10, 10)}, 1}
1583};
1584
Tomas Winklera96a27f2008-10-23 23:48:56 -07001585/* for TIM > 10 */
Samuel Ortiz1125eff2008-12-19 10:37:14 +08001586static struct iwl_power_vec_entry range_1[IWL39_POWER_AC] = {
Zhu Yib481de92007-09-25 17:54:57 -07001587 {{NOSLP, SLP_TIMEOUT(0), SLP_TIMEOUT(0), SLP_VEC(0, 0, 0, 0, 0)}, 0},
1588 {{SLP, SLP_TIMEOUT(200), SLP_TIMEOUT(500),
1589 SLP_VEC(1, 2, 3, 4, 0xFF)}, 0},
1590 {{SLP, SLP_TIMEOUT(200), SLP_TIMEOUT(300),
1591 SLP_VEC(2, 4, 6, 7, 0xFF)}, 0},
1592 {{SLP, SLP_TIMEOUT(50), SLP_TIMEOUT(100),
1593 SLP_VEC(2, 6, 9, 9, 0xFF)}, 0},
1594 {{SLP, SLP_TIMEOUT(50), SLP_TIMEOUT(25), SLP_VEC(2, 7, 9, 9, 0xFF)}, 0},
1595 {{SLP, SLP_TIMEOUT(25), SLP_TIMEOUT(25),
1596 SLP_VEC(4, 7, 10, 10, 0xFF)}, 0}
1597};
1598
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001599int iwl3945_power_init_handle(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001600{
1601 int rc = 0, i;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001602 struct iwl3945_power_mgr *pow_data;
Samuel Ortiz1125eff2008-12-19 10:37:14 +08001603 int size = sizeof(struct iwl_power_vec_entry) * IWL39_POWER_AC;
Zhu Yib481de92007-09-25 17:54:57 -07001604 u16 pci_pm;
1605
1606 IWL_DEBUG_POWER("Initialize power \n");
1607
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001608 pow_data = &(priv->power_data_39);
Zhu Yib481de92007-09-25 17:54:57 -07001609
1610 memset(pow_data, 0, sizeof(*pow_data));
1611
1612 pow_data->active_index = IWL_POWER_RANGE_0;
1613 pow_data->dtim_val = 0xffff;
1614
1615 memcpy(&pow_data->pwr_range_0[0], &range_0[0], size);
1616 memcpy(&pow_data->pwr_range_1[0], &range_1[0], size);
1617
1618 rc = pci_read_config_word(priv->pci_dev, PCI_LINK_CTRL, &pci_pm);
1619 if (rc != 0)
1620 return 0;
1621 else {
Tomas Winkler600c0e12008-12-19 10:37:04 +08001622 struct iwl_powertable_cmd *cmd;
Zhu Yib481de92007-09-25 17:54:57 -07001623
1624 IWL_DEBUG_POWER("adjust power command flags\n");
1625
Samuel Ortiz1125eff2008-12-19 10:37:14 +08001626 for (i = 0; i < IWL39_POWER_AC; i++) {
Zhu Yib481de92007-09-25 17:54:57 -07001627 cmd = &pow_data->pwr_range_0[i].cmd;
1628
1629 if (pci_pm & 0x1)
1630 cmd->flags &= ~IWL_POWER_PCI_PM_MSK;
1631 else
1632 cmd->flags |= IWL_POWER_PCI_PM_MSK;
1633 }
1634 }
1635 return rc;
1636}
1637
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001638static int iwl3945_update_power_cmd(struct iwl_priv *priv,
Tomas Winkler600c0e12008-12-19 10:37:04 +08001639 struct iwl_powertable_cmd *cmd, u32 mode)
Zhu Yib481de92007-09-25 17:54:57 -07001640{
1641 int rc = 0, i;
1642 u8 skip;
1643 u32 max_sleep = 0;
Samuel Ortiz1125eff2008-12-19 10:37:14 +08001644 struct iwl_power_vec_entry *range;
Zhu Yib481de92007-09-25 17:54:57 -07001645 u8 period = 0;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001646 struct iwl3945_power_mgr *pow_data;
Zhu Yib481de92007-09-25 17:54:57 -07001647
1648 if (mode > IWL_POWER_INDEX_5) {
1649 IWL_DEBUG_POWER("Error invalid power mode \n");
1650 return -1;
1651 }
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001652 pow_data = &(priv->power_data_39);
Zhu Yib481de92007-09-25 17:54:57 -07001653
1654 if (pow_data->active_index == IWL_POWER_RANGE_0)
1655 range = &pow_data->pwr_range_0[0];
1656 else
1657 range = &pow_data->pwr_range_1[1];
1658
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001659 memcpy(cmd, &range[mode].cmd, sizeof(struct iwl3945_powertable_cmd));
Zhu Yib481de92007-09-25 17:54:57 -07001660
1661#ifdef IWL_MAC80211_DISABLE
1662 if (priv->assoc_network != NULL) {
1663 unsigned long flags;
1664
1665 period = priv->assoc_network->tim.tim_period;
1666 }
1667#endif /*IWL_MAC80211_DISABLE */
1668 skip = range[mode].no_dtim;
1669
1670 if (period == 0) {
1671 period = 1;
1672 skip = 0;
1673 }
1674
1675 if (skip == 0) {
1676 max_sleep = period;
1677 cmd->flags &= ~IWL_POWER_SLEEP_OVER_DTIM_MSK;
1678 } else {
1679 __le32 slp_itrvl = cmd->sleep_interval[IWL_POWER_VEC_SIZE - 1];
1680 max_sleep = (le32_to_cpu(slp_itrvl) / period) * period;
1681 cmd->flags |= IWL_POWER_SLEEP_OVER_DTIM_MSK;
1682 }
1683
1684 for (i = 0; i < IWL_POWER_VEC_SIZE; i++) {
1685 if (le32_to_cpu(cmd->sleep_interval[i]) > max_sleep)
1686 cmd->sleep_interval[i] = cpu_to_le32(max_sleep);
1687 }
1688
1689 IWL_DEBUG_POWER("Flags value = 0x%08X\n", cmd->flags);
1690 IWL_DEBUG_POWER("Tx timeout = %u\n", le32_to_cpu(cmd->tx_data_timeout));
1691 IWL_DEBUG_POWER("Rx timeout = %u\n", le32_to_cpu(cmd->rx_data_timeout));
1692 IWL_DEBUG_POWER("Sleep interval vector = { %d , %d , %d , %d , %d }\n",
1693 le32_to_cpu(cmd->sleep_interval[0]),
1694 le32_to_cpu(cmd->sleep_interval[1]),
1695 le32_to_cpu(cmd->sleep_interval[2]),
1696 le32_to_cpu(cmd->sleep_interval[3]),
1697 le32_to_cpu(cmd->sleep_interval[4]));
1698
1699 return rc;
1700}
1701
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001702static int iwl3945_send_power_mode(struct iwl_priv *priv, u32 mode)
Zhu Yib481de92007-09-25 17:54:57 -07001703{
John W. Linville9a62f732007-11-15 16:27:36 -05001704 u32 uninitialized_var(final_mode);
Zhu Yib481de92007-09-25 17:54:57 -07001705 int rc;
Tomas Winkler600c0e12008-12-19 10:37:04 +08001706 struct iwl_powertable_cmd cmd;
Zhu Yib481de92007-09-25 17:54:57 -07001707
1708 /* If on battery, set to 3,
Ian Schram01ebd062007-10-25 17:15:22 +08001709 * if plugged into AC power, set to CAM ("continuously aware mode"),
Zhu Yib481de92007-09-25 17:54:57 -07001710 * else user level */
1711 switch (mode) {
Samuel Ortiz1125eff2008-12-19 10:37:14 +08001712 case IWL39_POWER_BATTERY:
Zhu Yib481de92007-09-25 17:54:57 -07001713 final_mode = IWL_POWER_INDEX_3;
1714 break;
Samuel Ortiz1125eff2008-12-19 10:37:14 +08001715 case IWL39_POWER_AC:
Zhu Yib481de92007-09-25 17:54:57 -07001716 final_mode = IWL_POWER_MODE_CAM;
1717 break;
1718 default:
1719 final_mode = mode;
1720 break;
1721 }
1722
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001723 iwl3945_update_power_cmd(priv, &cmd, final_mode);
Zhu Yib481de92007-09-25 17:54:57 -07001724
Tomas Winkler600c0e12008-12-19 10:37:04 +08001725 /* FIXME use get_hcmd_size 3945 command is 4 bytes shorter */
1726 rc = iwl3945_send_cmd_pdu(priv, POWER_TABLE_CMD,
1727 sizeof(struct iwl3945_powertable_cmd), &cmd);
Zhu Yib481de92007-09-25 17:54:57 -07001728
1729 if (final_mode == IWL_POWER_MODE_CAM)
1730 clear_bit(STATUS_POWER_PMI, &priv->status);
1731 else
1732 set_bit(STATUS_POWER_PMI, &priv->status);
1733
1734 return rc;
1735}
1736
Zhu Yib481de92007-09-25 17:54:57 -07001737/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001738 * iwl3945_scan_cancel - Cancel any currently executing HW scan
Zhu Yib481de92007-09-25 17:54:57 -07001739 *
1740 * NOTE: priv->mutex is not required before calling this function
1741 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001742static int iwl3945_scan_cancel(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001743{
1744 if (!test_bit(STATUS_SCAN_HW, &priv->status)) {
1745 clear_bit(STATUS_SCANNING, &priv->status);
1746 return 0;
1747 }
1748
1749 if (test_bit(STATUS_SCANNING, &priv->status)) {
1750 if (!test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
1751 IWL_DEBUG_SCAN("Queuing scan abort.\n");
1752 set_bit(STATUS_SCAN_ABORTING, &priv->status);
1753 queue_work(priv->workqueue, &priv->abort_scan);
1754
1755 } else
1756 IWL_DEBUG_SCAN("Scan abort already in progress.\n");
1757
1758 return test_bit(STATUS_SCANNING, &priv->status);
1759 }
1760
1761 return 0;
1762}
1763
1764/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001765 * iwl3945_scan_cancel_timeout - Cancel any currently executing HW scan
Zhu Yib481de92007-09-25 17:54:57 -07001766 * @ms: amount of time to wait (in milliseconds) for scan to abort
1767 *
1768 * NOTE: priv->mutex must be held before calling this function
1769 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001770static int iwl3945_scan_cancel_timeout(struct iwl_priv *priv, unsigned long ms)
Zhu Yib481de92007-09-25 17:54:57 -07001771{
1772 unsigned long now = jiffies;
1773 int ret;
1774
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001775 ret = iwl3945_scan_cancel(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001776 if (ret && ms) {
1777 mutex_unlock(&priv->mutex);
1778 while (!time_after(jiffies, now + msecs_to_jiffies(ms)) &&
1779 test_bit(STATUS_SCANNING, &priv->status))
1780 msleep(1);
1781 mutex_lock(&priv->mutex);
1782
1783 return test_bit(STATUS_SCANNING, &priv->status);
1784 }
1785
1786 return ret;
1787}
1788
Zhu Yib481de92007-09-25 17:54:57 -07001789#define MAX_UCODE_BEACON_INTERVAL 1024
1790#define INTEL_CONN_LISTEN_INTERVAL __constant_cpu_to_le16(0xA)
1791
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001792static __le16 iwl3945_adjust_beacon_interval(u16 beacon_val)
Zhu Yib481de92007-09-25 17:54:57 -07001793{
1794 u16 new_val = 0;
1795 u16 beacon_factor = 0;
1796
1797 beacon_factor =
1798 (beacon_val + MAX_UCODE_BEACON_INTERVAL)
1799 / MAX_UCODE_BEACON_INTERVAL;
1800 new_val = beacon_val / beacon_factor;
1801
1802 return cpu_to_le16(new_val);
1803}
1804
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001805static void iwl3945_setup_rxon_timing(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001806{
1807 u64 interval_tm_unit;
1808 u64 tsf, result;
1809 unsigned long flags;
1810 struct ieee80211_conf *conf = NULL;
1811 u16 beacon_int = 0;
1812
1813 conf = ieee80211_get_hw_conf(priv->hw);
1814
1815 spin_lock_irqsave(&priv->lock, flags);
Tomas Winkler28afaf92008-12-19 10:37:06 +08001816 priv->rxon_timing.timestamp = cpu_to_le64(priv->timestamp);
Zhu Yib481de92007-09-25 17:54:57 -07001817 priv->rxon_timing.listen_interval = INTEL_CONN_LISTEN_INTERVAL;
1818
Tomas Winkler28afaf92008-12-19 10:37:06 +08001819 tsf = priv->timestamp;
Zhu Yib481de92007-09-25 17:54:57 -07001820
1821 beacon_int = priv->beacon_int;
1822 spin_unlock_irqrestore(&priv->lock, flags);
1823
Johannes Berg05c914f2008-09-11 00:01:58 +02001824 if (priv->iw_mode == NL80211_IFTYPE_STATION) {
Zhu Yib481de92007-09-25 17:54:57 -07001825 if (beacon_int == 0) {
1826 priv->rxon_timing.beacon_interval = cpu_to_le16(100);
1827 priv->rxon_timing.beacon_init_val = cpu_to_le32(102400);
1828 } else {
1829 priv->rxon_timing.beacon_interval =
1830 cpu_to_le16(beacon_int);
1831 priv->rxon_timing.beacon_interval =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001832 iwl3945_adjust_beacon_interval(
Zhu Yib481de92007-09-25 17:54:57 -07001833 le16_to_cpu(priv->rxon_timing.beacon_interval));
1834 }
1835
1836 priv->rxon_timing.atim_window = 0;
1837 } else {
1838 priv->rxon_timing.beacon_interval =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001839 iwl3945_adjust_beacon_interval(conf->beacon_int);
Zhu Yib481de92007-09-25 17:54:57 -07001840 /* TODO: we need to get atim_window from upper stack
1841 * for now we set to 0 */
1842 priv->rxon_timing.atim_window = 0;
1843 }
1844
1845 interval_tm_unit =
1846 (le16_to_cpu(priv->rxon_timing.beacon_interval) * 1024);
1847 result = do_div(tsf, interval_tm_unit);
1848 priv->rxon_timing.beacon_init_val =
1849 cpu_to_le32((u32) ((u64) interval_tm_unit - result));
1850
1851 IWL_DEBUG_ASSOC
1852 ("beacon interval %d beacon timer %d beacon tim %d\n",
1853 le16_to_cpu(priv->rxon_timing.beacon_interval),
1854 le32_to_cpu(priv->rxon_timing.beacon_init_val),
1855 le16_to_cpu(priv->rxon_timing.atim_window));
1856}
1857
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001858static int iwl3945_scan_initiate(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001859{
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08001860 if (!iwl_is_ready_rf(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07001861 IWL_DEBUG_SCAN("Aborting scan due to not ready.\n");
1862 return -EIO;
1863 }
1864
1865 if (test_bit(STATUS_SCANNING, &priv->status)) {
1866 IWL_DEBUG_SCAN("Scan already in progress.\n");
1867 return -EAGAIN;
1868 }
1869
1870 if (test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
1871 IWL_DEBUG_SCAN("Scan request while abort pending. "
1872 "Queuing.\n");
1873 return -EAGAIN;
1874 }
1875
1876 IWL_DEBUG_INFO("Starting scan...\n");
Ron Rindjunsky66b50042008-06-25 16:46:31 +08001877 if (priv->cfg->sku & IWL_SKU_G)
1878 priv->scan_bands |= BIT(IEEE80211_BAND_2GHZ);
1879 if (priv->cfg->sku & IWL_SKU_A)
1880 priv->scan_bands |= BIT(IEEE80211_BAND_5GHZ);
Zhu Yib481de92007-09-25 17:54:57 -07001881 set_bit(STATUS_SCANNING, &priv->status);
1882 priv->scan_start = jiffies;
1883 priv->scan_pass_start = priv->scan_start;
1884
1885 queue_work(priv->workqueue, &priv->request_scan);
1886
1887 return 0;
1888}
1889
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001890static int iwl3945_set_rxon_hwcrypto(struct iwl_priv *priv, int hw_decrypt)
Zhu Yib481de92007-09-25 17:54:57 -07001891{
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001892 struct iwl3945_rxon_cmd *rxon = &priv->staging39_rxon;
Zhu Yib481de92007-09-25 17:54:57 -07001893
1894 if (hw_decrypt)
1895 rxon->filter_flags &= ~RXON_FILTER_DIS_DECRYPT_MSK;
1896 else
1897 rxon->filter_flags |= RXON_FILTER_DIS_DECRYPT_MSK;
1898
1899 return 0;
1900}
1901
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001902static void iwl3945_set_flags_for_phymode(struct iwl_priv *priv,
Johannes Berg8318d782008-01-24 19:38:38 +01001903 enum ieee80211_band band)
Zhu Yib481de92007-09-25 17:54:57 -07001904{
Johannes Berg8318d782008-01-24 19:38:38 +01001905 if (band == IEEE80211_BAND_5GHZ) {
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001906 priv->staging39_rxon.flags &=
Zhu Yib481de92007-09-25 17:54:57 -07001907 ~(RXON_FLG_BAND_24G_MSK | RXON_FLG_AUTO_DETECT_MSK
1908 | RXON_FLG_CCK_MSK);
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001909 priv->staging39_rxon.flags |= RXON_FLG_SHORT_SLOT_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07001910 } else {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001911 /* Copied from iwl3945_bg_post_associate() */
Zhu Yib481de92007-09-25 17:54:57 -07001912 if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_SLOT_TIME)
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001913 priv->staging39_rxon.flags |= RXON_FLG_SHORT_SLOT_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07001914 else
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001915 priv->staging39_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07001916
Johannes Berg05c914f2008-09-11 00:01:58 +02001917 if (priv->iw_mode == NL80211_IFTYPE_ADHOC)
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001918 priv->staging39_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07001919
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001920 priv->staging39_rxon.flags |= RXON_FLG_BAND_24G_MSK;
1921 priv->staging39_rxon.flags |= RXON_FLG_AUTO_DETECT_MSK;
1922 priv->staging39_rxon.flags &= ~RXON_FLG_CCK_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07001923 }
1924}
1925
1926/*
Ian Schram01ebd062007-10-25 17:15:22 +08001927 * initialize rxon structure with default values from eeprom
Zhu Yib481de92007-09-25 17:54:57 -07001928 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001929static void iwl3945_connection_init_rx_config(struct iwl_priv *priv,
Zhu, Yi60294de2008-10-29 14:05:45 -07001930 int mode)
Zhu Yib481de92007-09-25 17:54:57 -07001931{
Samuel Ortizd20b3c62008-12-19 10:37:15 +08001932 const struct iwl_channel_info *ch_info;
Zhu Yib481de92007-09-25 17:54:57 -07001933
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001934 memset(&priv->staging39_rxon, 0, sizeof(priv->staging39_rxon));
Zhu Yib481de92007-09-25 17:54:57 -07001935
Zhu, Yi60294de2008-10-29 14:05:45 -07001936 switch (mode) {
Johannes Berg05c914f2008-09-11 00:01:58 +02001937 case NL80211_IFTYPE_AP:
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001938 priv->staging39_rxon.dev_type = RXON_DEV_TYPE_AP;
Zhu Yib481de92007-09-25 17:54:57 -07001939 break;
1940
Johannes Berg05c914f2008-09-11 00:01:58 +02001941 case NL80211_IFTYPE_STATION:
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001942 priv->staging39_rxon.dev_type = RXON_DEV_TYPE_ESS;
1943 priv->staging39_rxon.filter_flags = RXON_FILTER_ACCEPT_GRP_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07001944 break;
1945
Johannes Berg05c914f2008-09-11 00:01:58 +02001946 case NL80211_IFTYPE_ADHOC:
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001947 priv->staging39_rxon.dev_type = RXON_DEV_TYPE_IBSS;
1948 priv->staging39_rxon.flags = RXON_FLG_SHORT_PREAMBLE_MSK;
1949 priv->staging39_rxon.filter_flags = RXON_FILTER_BCON_AWARE_MSK |
Zhu Yib481de92007-09-25 17:54:57 -07001950 RXON_FILTER_ACCEPT_GRP_MSK;
1951 break;
1952
Johannes Berg05c914f2008-09-11 00:01:58 +02001953 case NL80211_IFTYPE_MONITOR:
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001954 priv->staging39_rxon.dev_type = RXON_DEV_TYPE_SNIFFER;
1955 priv->staging39_rxon.filter_flags = RXON_FILTER_PROMISC_MSK |
Zhu Yib481de92007-09-25 17:54:57 -07001956 RXON_FILTER_CTL2HOST_MSK | RXON_FILTER_ACCEPT_GRP_MSK;
1957 break;
Tomas Winkler69dc5d92008-03-25 16:33:41 -07001958 default:
Winkler, Tomas15b16872008-12-19 10:37:33 +08001959 IWL_ERR(priv, "Unsupported interface type %d\n", mode);
Tomas Winkler69dc5d92008-03-25 16:33:41 -07001960 break;
Zhu Yib481de92007-09-25 17:54:57 -07001961 }
1962
1963#if 0
1964 /* TODO: Figure out when short_preamble would be set and cache from
1965 * that */
1966 if (!hw_to_local(priv->hw)->short_preamble)
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001967 priv->staging39_rxon.flags &= ~RXON_FLG_SHORT_PREAMBLE_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07001968 else
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001969 priv->staging39_rxon.flags |= RXON_FLG_SHORT_PREAMBLE_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07001970#endif
1971
Johannes Berg8318d782008-01-24 19:38:38 +01001972 ch_info = iwl3945_get_channel_info(priv, priv->band,
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001973 le16_to_cpu(priv->active39_rxon.channel));
Zhu Yib481de92007-09-25 17:54:57 -07001974
1975 if (!ch_info)
1976 ch_info = &priv->channel_info[0];
1977
1978 /*
1979 * in some case A channels are all non IBSS
1980 * in this case force B/G channel
1981 */
Zhu, Yi60294de2008-10-29 14:05:45 -07001982 if ((mode == NL80211_IFTYPE_ADHOC) && !(is_channel_ibss(ch_info)))
Zhu Yib481de92007-09-25 17:54:57 -07001983 ch_info = &priv->channel_info[0];
1984
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001985 priv->staging39_rxon.channel = cpu_to_le16(ch_info->channel);
Zhu Yib481de92007-09-25 17:54:57 -07001986 if (is_channel_a_band(ch_info))
Johannes Berg8318d782008-01-24 19:38:38 +01001987 priv->band = IEEE80211_BAND_5GHZ;
Zhu Yib481de92007-09-25 17:54:57 -07001988 else
Johannes Berg8318d782008-01-24 19:38:38 +01001989 priv->band = IEEE80211_BAND_2GHZ;
Zhu Yib481de92007-09-25 17:54:57 -07001990
Johannes Berg8318d782008-01-24 19:38:38 +01001991 iwl3945_set_flags_for_phymode(priv, priv->band);
Zhu Yib481de92007-09-25 17:54:57 -07001992
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001993 priv->staging39_rxon.ofdm_basic_rates =
Zhu Yib481de92007-09-25 17:54:57 -07001994 (IWL_OFDM_RATES_MASK >> IWL_FIRST_OFDM_RATE) & 0xFF;
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001995 priv->staging39_rxon.cck_basic_rates =
Zhu Yib481de92007-09-25 17:54:57 -07001996 (IWL_CCK_RATES_MASK >> IWL_FIRST_CCK_RATE) & 0xF;
1997}
1998
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001999static int iwl3945_set_mode(struct iwl_priv *priv, int mode)
Zhu Yib481de92007-09-25 17:54:57 -07002000{
Johannes Berg05c914f2008-09-11 00:01:58 +02002001 if (mode == NL80211_IFTYPE_ADHOC) {
Samuel Ortizd20b3c62008-12-19 10:37:15 +08002002 const struct iwl_channel_info *ch_info;
Zhu Yib481de92007-09-25 17:54:57 -07002003
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002004 ch_info = iwl3945_get_channel_info(priv,
Johannes Berg8318d782008-01-24 19:38:38 +01002005 priv->band,
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08002006 le16_to_cpu(priv->staging39_rxon.channel));
Zhu Yib481de92007-09-25 17:54:57 -07002007
2008 if (!ch_info || !is_channel_ibss(ch_info)) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08002009 IWL_ERR(priv, "channel %d not IBSS channel\n",
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08002010 le16_to_cpu(priv->staging39_rxon.channel));
Zhu Yib481de92007-09-25 17:54:57 -07002011 return -EINVAL;
2012 }
2013 }
2014
Zhu, Yi60294de2008-10-29 14:05:45 -07002015 iwl3945_connection_init_rx_config(priv, mode);
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08002016 memcpy(priv->staging39_rxon.node_addr, priv->mac_addr, ETH_ALEN);
Zhu Yib481de92007-09-25 17:54:57 -07002017
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002018 iwl3945_clear_stations_table(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002019
Tomas Winklera96a27f2008-10-23 23:48:56 -07002020 /* don't commit rxon if rf-kill is on*/
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08002021 if (!iwl_is_ready_rf(priv))
Mohamed Abbasfde35712007-11-29 11:10:15 +08002022 return -EAGAIN;
2023
2024 cancel_delayed_work(&priv->scan_check);
2025 if (iwl3945_scan_cancel_timeout(priv, 100)) {
Winkler, Tomas39aadf82008-12-19 10:37:32 +08002026 IWL_WARN(priv, "Aborted scan still in progress after 100ms\n");
Mohamed Abbasfde35712007-11-29 11:10:15 +08002027 IWL_DEBUG_MAC80211("leaving - scan abort failed.\n");
2028 return -EAGAIN;
2029 }
2030
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002031 iwl3945_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002032
2033 return 0;
2034}
2035
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002036static void iwl3945_build_tx_cmd_hwcrypto(struct iwl_priv *priv,
Johannes Berge039fa42008-05-15 12:55:29 +02002037 struct ieee80211_tx_info *info,
Winkler, Tomasc2d79b42008-12-19 10:37:34 +08002038 struct iwl_cmd *cmd,
Zhu Yib481de92007-09-25 17:54:57 -07002039 struct sk_buff *skb_frag,
2040 int last_frag)
2041{
Winkler, Tomase52119c2008-12-22 11:31:19 +08002042 struct iwl3945_tx_cmd *tx = (struct iwl3945_tx_cmd *)cmd->cmd.payload;
Ivo van Doorn1c014422008-04-17 19:41:02 +02002043 struct iwl3945_hw_key *keyinfo =
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08002044 &priv->stations_39[info->control.hw_key->hw_key_idx].keyinfo;
Zhu Yib481de92007-09-25 17:54:57 -07002045
2046 switch (keyinfo->alg) {
2047 case ALG_CCMP:
Winkler, Tomase52119c2008-12-22 11:31:19 +08002048 tx->sec_ctl = TX_CMD_SEC_CCM;
2049 memcpy(tx->key, keyinfo->key, keyinfo->keylen);
Tomas Winklera96a27f2008-10-23 23:48:56 -07002050 IWL_DEBUG_TX("tx_cmd with AES hwcrypto\n");
Zhu Yib481de92007-09-25 17:54:57 -07002051 break;
2052
2053 case ALG_TKIP:
2054#if 0
Winkler, Tomase52119c2008-12-22 11:31:19 +08002055 tx->sec_ctl = TX_CMD_SEC_TKIP;
Zhu Yib481de92007-09-25 17:54:57 -07002056
2057 if (last_frag)
Winkler, Tomase52119c2008-12-22 11:31:19 +08002058 memcpy(tx->tkip_mic.byte, skb_frag->tail - 8,
Zhu Yib481de92007-09-25 17:54:57 -07002059 8);
2060 else
Winkler, Tomase52119c2008-12-22 11:31:19 +08002061 memset(tx->tkip_mic.byte, 0, 8);
Zhu Yib481de92007-09-25 17:54:57 -07002062#endif
2063 break;
2064
2065 case ALG_WEP:
Winkler, Tomase52119c2008-12-22 11:31:19 +08002066 tx->sec_ctl = TX_CMD_SEC_WEP |
Johannes Berge039fa42008-05-15 12:55:29 +02002067 (info->control.hw_key->hw_key_idx & TX_CMD_SEC_MSK) << TX_CMD_SEC_SHIFT;
Zhu Yib481de92007-09-25 17:54:57 -07002068
2069 if (keyinfo->keylen == 13)
Winkler, Tomase52119c2008-12-22 11:31:19 +08002070 tx->sec_ctl |= TX_CMD_SEC_KEY128;
Zhu Yib481de92007-09-25 17:54:57 -07002071
Winkler, Tomase52119c2008-12-22 11:31:19 +08002072 memcpy(&tx->key[3], keyinfo->key, keyinfo->keylen);
Zhu Yib481de92007-09-25 17:54:57 -07002073
2074 IWL_DEBUG_TX("Configuring packet for WEP encryption "
Johannes Berge039fa42008-05-15 12:55:29 +02002075 "with key %d\n", info->control.hw_key->hw_key_idx);
Zhu Yib481de92007-09-25 17:54:57 -07002076 break;
2077
Zhu Yib481de92007-09-25 17:54:57 -07002078 default:
Tomas Winkler978785a2008-12-19 10:37:31 +08002079 IWL_ERR(priv, "Unknown encode alg %d\n", keyinfo->alg);
Zhu Yib481de92007-09-25 17:54:57 -07002080 break;
2081 }
2082}
2083
2084/*
2085 * handle build REPLY_TX command notification.
2086 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002087static void iwl3945_build_tx_cmd_basic(struct iwl_priv *priv,
Winkler, Tomasc2d79b42008-12-19 10:37:34 +08002088 struct iwl_cmd *cmd,
Johannes Berge039fa42008-05-15 12:55:29 +02002089 struct ieee80211_tx_info *info,
Winkler, Tomase52119c2008-12-22 11:31:19 +08002090 struct ieee80211_hdr *hdr, u8 std_id)
Zhu Yib481de92007-09-25 17:54:57 -07002091{
Winkler, Tomase52119c2008-12-22 11:31:19 +08002092 struct iwl3945_tx_cmd *tx = (struct iwl3945_tx_cmd *)cmd->cmd.payload;
2093 __le32 tx_flags = tx->tx_flags;
Harvey Harrisonfd7c8a42008-06-11 14:21:56 -07002094 __le16 fc = hdr->frame_control;
Johannes Berge6a98542008-10-21 12:40:02 +02002095 u8 rc_flags = info->control.rates[0].flags;
Zhu Yib481de92007-09-25 17:54:57 -07002096
Winkler, Tomase52119c2008-12-22 11:31:19 +08002097 tx->stop_time.life_time = TX_CMD_LIFE_TIME_INFINITE;
Johannes Berge039fa42008-05-15 12:55:29 +02002098 if (!(info->flags & IEEE80211_TX_CTL_NO_ACK)) {
Zhu Yib481de92007-09-25 17:54:57 -07002099 tx_flags |= TX_CMD_FLG_ACK_MSK;
Harvey Harrisonfd7c8a42008-06-11 14:21:56 -07002100 if (ieee80211_is_mgmt(fc))
Zhu Yib481de92007-09-25 17:54:57 -07002101 tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
Harvey Harrisonfd7c8a42008-06-11 14:21:56 -07002102 if (ieee80211_is_probe_resp(fc) &&
Zhu Yib481de92007-09-25 17:54:57 -07002103 !(le16_to_cpu(hdr->seq_ctrl) & 0xf))
2104 tx_flags |= TX_CMD_FLG_TSF_MSK;
2105 } else {
2106 tx_flags &= (~TX_CMD_FLG_ACK_MSK);
2107 tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
2108 }
2109
Winkler, Tomase52119c2008-12-22 11:31:19 +08002110 tx->sta_id = std_id;
Harvey Harrison8b7b1e02008-06-11 14:21:56 -07002111 if (ieee80211_has_morefrags(fc))
Zhu Yib481de92007-09-25 17:54:57 -07002112 tx_flags |= TX_CMD_FLG_MORE_FRAG_MSK;
2113
Harvey Harrisonfd7c8a42008-06-11 14:21:56 -07002114 if (ieee80211_is_data_qos(fc)) {
2115 u8 *qc = ieee80211_get_qos_ctl(hdr);
Winkler, Tomase52119c2008-12-22 11:31:19 +08002116 tx->tid_tspec = qc[0] & 0xf;
Zhu Yib481de92007-09-25 17:54:57 -07002117 tx_flags &= ~TX_CMD_FLG_SEQ_CTL_MSK;
Tomas Winkler54dbb522008-05-15 13:54:06 +08002118 } else {
Zhu Yib481de92007-09-25 17:54:57 -07002119 tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
Tomas Winkler54dbb522008-05-15 13:54:06 +08002120 }
Zhu Yib481de92007-09-25 17:54:57 -07002121
Johannes Berge6a98542008-10-21 12:40:02 +02002122 if (rc_flags & IEEE80211_TX_RC_USE_RTS_CTS) {
Zhu Yib481de92007-09-25 17:54:57 -07002123 tx_flags |= TX_CMD_FLG_RTS_MSK;
2124 tx_flags &= ~TX_CMD_FLG_CTS_MSK;
Johannes Berge6a98542008-10-21 12:40:02 +02002125 } else if (rc_flags & IEEE80211_TX_RC_USE_CTS_PROTECT) {
Zhu Yib481de92007-09-25 17:54:57 -07002126 tx_flags &= ~TX_CMD_FLG_RTS_MSK;
2127 tx_flags |= TX_CMD_FLG_CTS_MSK;
2128 }
2129
2130 if ((tx_flags & TX_CMD_FLG_RTS_MSK) || (tx_flags & TX_CMD_FLG_CTS_MSK))
2131 tx_flags |= TX_CMD_FLG_FULL_TXOP_PROT_MSK;
2132
2133 tx_flags &= ~(TX_CMD_FLG_ANT_SEL_MSK);
Harvey Harrisonfd7c8a42008-06-11 14:21:56 -07002134 if (ieee80211_is_mgmt(fc)) {
2135 if (ieee80211_is_assoc_req(fc) || ieee80211_is_reassoc_req(fc))
Winkler, Tomase52119c2008-12-22 11:31:19 +08002136 tx->timeout.pm_frame_timeout = cpu_to_le16(3);
Zhu Yib481de92007-09-25 17:54:57 -07002137 else
Winkler, Tomase52119c2008-12-22 11:31:19 +08002138 tx->timeout.pm_frame_timeout = cpu_to_le16(2);
Mohamed Abbasab53d8a2008-03-25 16:33:36 -07002139 } else {
Winkler, Tomase52119c2008-12-22 11:31:19 +08002140 tx->timeout.pm_frame_timeout = 0;
Mohamed Abbasab53d8a2008-03-25 16:33:36 -07002141#ifdef CONFIG_IWL3945_LEDS
2142 priv->rxtxpackets += le16_to_cpu(cmd->cmd.tx.len);
2143#endif
2144 }
Zhu Yib481de92007-09-25 17:54:57 -07002145
Winkler, Tomase52119c2008-12-22 11:31:19 +08002146 tx->driver_txop = 0;
2147 tx->tx_flags = tx_flags;
2148 tx->next_frame_len = 0;
Zhu Yib481de92007-09-25 17:54:57 -07002149}
2150
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002151/**
2152 * iwl3945_get_sta_id - Find station's index within station table
2153 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002154static int iwl3945_get_sta_id(struct iwl_priv *priv, struct ieee80211_hdr *hdr)
Zhu Yib481de92007-09-25 17:54:57 -07002155{
2156 int sta_id;
2157 u16 fc = le16_to_cpu(hdr->frame_control);
2158
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002159 /* If this frame is broadcast or management, use broadcast station id */
Zhu Yib481de92007-09-25 17:54:57 -07002160 if (((fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_DATA) ||
2161 is_multicast_ether_addr(hdr->addr1))
Abhijeet Kolekar3832ec92008-12-19 10:37:26 +08002162 return priv->hw_params.bcast_sta_id;
Zhu Yib481de92007-09-25 17:54:57 -07002163
2164 switch (priv->iw_mode) {
2165
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002166 /* If we are a client station in a BSS network, use the special
2167 * AP station entry (that's the only station we communicate with) */
Johannes Berg05c914f2008-09-11 00:01:58 +02002168 case NL80211_IFTYPE_STATION:
Zhu Yib481de92007-09-25 17:54:57 -07002169 return IWL_AP_ID;
2170
2171 /* If we are an AP, then find the station, or use BCAST */
Johannes Berg05c914f2008-09-11 00:01:58 +02002172 case NL80211_IFTYPE_AP:
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002173 sta_id = iwl3945_hw_find_station(priv, hdr->addr1);
Zhu Yib481de92007-09-25 17:54:57 -07002174 if (sta_id != IWL_INVALID_STATION)
2175 return sta_id;
Abhijeet Kolekar3832ec92008-12-19 10:37:26 +08002176 return priv->hw_params.bcast_sta_id;
Zhu Yib481de92007-09-25 17:54:57 -07002177
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002178 /* If this frame is going out to an IBSS network, find the station,
2179 * or create a new station table entry */
Johannes Berg05c914f2008-09-11 00:01:58 +02002180 case NL80211_IFTYPE_ADHOC: {
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002181 /* Create new station table entry */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002182 sta_id = iwl3945_hw_find_station(priv, hdr->addr1);
Zhu Yib481de92007-09-25 17:54:57 -07002183 if (sta_id != IWL_INVALID_STATION)
2184 return sta_id;
2185
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002186 sta_id = iwl3945_add_station(priv, hdr->addr1, 0, CMD_ASYNC);
Zhu Yib481de92007-09-25 17:54:57 -07002187
2188 if (sta_id != IWL_INVALID_STATION)
2189 return sta_id;
2190
Johannes Berge1749612008-10-27 15:59:26 -07002191 IWL_DEBUG_DROP("Station %pM not in station map. "
Zhu Yib481de92007-09-25 17:54:57 -07002192 "Defaulting to broadcast...\n",
Johannes Berge1749612008-10-27 15:59:26 -07002193 hdr->addr1);
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08002194 iwl_print_hex_dump(priv, IWL_DL_DROP, (u8 *) hdr, sizeof(*hdr));
Abhijeet Kolekar3832ec92008-12-19 10:37:26 +08002195 return priv->hw_params.bcast_sta_id;
Joe Perches0795af52007-10-03 17:59:30 -07002196 }
Stefanik Gábor914233d2008-06-30 17:23:30 +08002197 /* If we are in monitor mode, use BCAST. This is required for
2198 * packet injection. */
Johannes Berg05c914f2008-09-11 00:01:58 +02002199 case NL80211_IFTYPE_MONITOR:
Abhijeet Kolekar3832ec92008-12-19 10:37:26 +08002200 return priv->hw_params.bcast_sta_id;
Stefanik Gábor914233d2008-06-30 17:23:30 +08002201
Zhu Yib481de92007-09-25 17:54:57 -07002202 default:
Winkler, Tomas39aadf82008-12-19 10:37:32 +08002203 IWL_WARN(priv, "Unknown mode of operation: %d\n",
2204 priv->iw_mode);
Abhijeet Kolekar3832ec92008-12-19 10:37:26 +08002205 return priv->hw_params.bcast_sta_id;
Zhu Yib481de92007-09-25 17:54:57 -07002206 }
2207}
2208
2209/*
2210 * start REPLY_TX command process
2211 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002212static int iwl3945_tx_skb(struct iwl_priv *priv, struct sk_buff *skb)
Zhu Yib481de92007-09-25 17:54:57 -07002213{
2214 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
Johannes Berge039fa42008-05-15 12:55:29 +02002215 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
Winkler, Tomasdbb66542008-12-22 11:31:14 +08002216 struct iwl3945_tfd *tfd;
Winkler, Tomase52119c2008-12-22 11:31:19 +08002217 struct iwl3945_tx_cmd *tx;
Samuel Ortiz188cf6c2008-12-22 11:31:16 +08002218 struct iwl_tx_queue *txq = NULL;
Samuel Ortizd20b3c62008-12-19 10:37:15 +08002219 struct iwl_queue *q = NULL;
Winkler, Tomase52119c2008-12-22 11:31:19 +08002220 struct iwl_cmd *out_cmd = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07002221 dma_addr_t phys_addr;
2222 dma_addr_t txcmd_phys;
Winkler, Tomase52119c2008-12-22 11:31:19 +08002223 int txq_id = skb_get_queue_mapping(skb);
Tomas Winkler54dbb522008-05-15 13:54:06 +08002224 u16 len, idx, len_org, hdr_len;
2225 u8 id;
2226 u8 unicast;
Zhu Yib481de92007-09-25 17:54:57 -07002227 u8 sta_id;
Tomas Winkler54dbb522008-05-15 13:54:06 +08002228 u8 tid = 0;
Zhu Yib481de92007-09-25 17:54:57 -07002229 u16 seq_number = 0;
Harvey Harrisonfd7c8a42008-06-11 14:21:56 -07002230 __le16 fc;
Zhu Yib481de92007-09-25 17:54:57 -07002231 u8 wait_write_ptr = 0;
Tomas Winkler54dbb522008-05-15 13:54:06 +08002232 u8 *qc = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07002233 unsigned long flags;
2234 int rc;
2235
2236 spin_lock_irqsave(&priv->lock, flags);
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08002237 if (iwl_is_rfkill(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07002238 IWL_DEBUG_DROP("Dropping - RF KILL\n");
2239 goto drop_unlock;
2240 }
2241
Johannes Berge039fa42008-05-15 12:55:29 +02002242 if ((ieee80211_get_tx_rate(priv->hw, info)->hw_value & 0xFF) == IWL_INVALID_RATE) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08002243 IWL_ERR(priv, "ERROR: No TX rate available.\n");
Zhu Yib481de92007-09-25 17:54:57 -07002244 goto drop_unlock;
2245 }
2246
2247 unicast = !is_multicast_ether_addr(hdr->addr1);
2248 id = 0;
2249
Harvey Harrisonfd7c8a42008-06-11 14:21:56 -07002250 fc = hdr->frame_control;
Zhu Yib481de92007-09-25 17:54:57 -07002251
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08002252#ifdef CONFIG_IWL3945_DEBUG
Zhu Yib481de92007-09-25 17:54:57 -07002253 if (ieee80211_is_auth(fc))
2254 IWL_DEBUG_TX("Sending AUTH frame\n");
Harvey Harrisonfd7c8a42008-06-11 14:21:56 -07002255 else if (ieee80211_is_assoc_req(fc))
Zhu Yib481de92007-09-25 17:54:57 -07002256 IWL_DEBUG_TX("Sending ASSOC frame\n");
Harvey Harrisonfd7c8a42008-06-11 14:21:56 -07002257 else if (ieee80211_is_reassoc_req(fc))
Zhu Yib481de92007-09-25 17:54:57 -07002258 IWL_DEBUG_TX("Sending REASSOC frame\n");
2259#endif
2260
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08002261 /* drop all data frame if we are not associated */
Stefanik Gábor914233d2008-06-30 17:23:30 +08002262 if (ieee80211_is_data(fc) &&
Johannes Berg05c914f2008-09-11 00:01:58 +02002263 (priv->iw_mode != NL80211_IFTYPE_MONITOR) && /* packet injection */
Stefanik Gábor914233d2008-06-30 17:23:30 +08002264 (!iwl3945_is_associated(priv) ||
Johannes Berg05c914f2008-09-11 00:01:58 +02002265 ((priv->iw_mode == NL80211_IFTYPE_STATION) && !priv->assoc_id))) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002266 IWL_DEBUG_DROP("Dropping - !iwl3945_is_associated\n");
Zhu Yib481de92007-09-25 17:54:57 -07002267 goto drop_unlock;
2268 }
2269
2270 spin_unlock_irqrestore(&priv->lock, flags);
2271
Harvey Harrison7294ec92008-07-15 18:43:59 -07002272 hdr_len = ieee80211_hdrlen(fc);
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002273
2274 /* Find (or create) index into station table for destination station */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002275 sta_id = iwl3945_get_sta_id(priv, hdr);
Zhu Yib481de92007-09-25 17:54:57 -07002276 if (sta_id == IWL_INVALID_STATION) {
Johannes Berge1749612008-10-27 15:59:26 -07002277 IWL_DEBUG_DROP("Dropping - INVALID STATION: %pM\n",
2278 hdr->addr1);
Zhu Yib481de92007-09-25 17:54:57 -07002279 goto drop;
2280 }
2281
2282 IWL_DEBUG_RATE("station Id %d\n", sta_id);
2283
Harvey Harrisonfd7c8a42008-06-11 14:21:56 -07002284 if (ieee80211_is_data_qos(fc)) {
2285 qc = ieee80211_get_qos_ctl(hdr);
Harvey Harrison7294ec92008-07-15 18:43:59 -07002286 tid = qc[0] & IEEE80211_QOS_CTL_TID_MASK;
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08002287 seq_number = priv->stations_39[sta_id].tid[tid].seq_number &
Zhu Yib481de92007-09-25 17:54:57 -07002288 IEEE80211_SCTL_SEQ;
2289 hdr->seq_ctrl = cpu_to_le16(seq_number) |
2290 (hdr->seq_ctrl &
2291 __constant_cpu_to_le16(IEEE80211_SCTL_FRAG));
2292 seq_number += 0x10;
2293 }
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002294
2295 /* Descriptor for chosen Tx queue */
Samuel Ortiz188cf6c2008-12-22 11:31:16 +08002296 txq = &priv->txq[txq_id];
Zhu Yib481de92007-09-25 17:54:57 -07002297 q = &txq->q;
2298
2299 spin_lock_irqsave(&priv->lock, flags);
2300
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002301 /* Set up first empty TFD within this queue's circular TFD buffer */
Samuel Ortiz188cf6c2008-12-22 11:31:16 +08002302 tfd = &txq->tfds39[q->write_ptr];
Zhu Yib481de92007-09-25 17:54:57 -07002303 memset(tfd, 0, sizeof(*tfd));
Tomas Winklerfc4b6852007-10-25 17:15:24 +08002304 idx = get_cmd_index(q, q->write_ptr, 0);
Zhu Yib481de92007-09-25 17:54:57 -07002305
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002306 /* Set up driver data for this TFD */
Winkler, Tomasdbb66542008-12-22 11:31:14 +08002307 memset(&(txq->txb[q->write_ptr]), 0, sizeof(struct iwl_tx_info));
Tomas Winklerfc4b6852007-10-25 17:15:24 +08002308 txq->txb[q->write_ptr].skb[0] = skb;
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002309
2310 /* Init first empty entry in queue's array of Tx/cmd buffers */
Samuel Ortiz188cf6c2008-12-22 11:31:16 +08002311 out_cmd = txq->cmd[idx];
Winkler, Tomase52119c2008-12-22 11:31:19 +08002312 tx = (struct iwl3945_tx_cmd *)out_cmd->cmd.payload;
Zhu Yib481de92007-09-25 17:54:57 -07002313 memset(&out_cmd->hdr, 0, sizeof(out_cmd->hdr));
Winkler, Tomase52119c2008-12-22 11:31:19 +08002314 memset(tx, 0, sizeof(*tx));
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002315
2316 /*
2317 * Set up the Tx-command (not MAC!) header.
2318 * Store the chosen Tx queue and TFD index within the sequence field;
2319 * after Tx, uCode's Tx response will return this value so driver can
2320 * locate the frame within the tx queue and do post-tx processing.
2321 */
Zhu Yib481de92007-09-25 17:54:57 -07002322 out_cmd->hdr.cmd = REPLY_TX;
2323 out_cmd->hdr.sequence = cpu_to_le16((u16)(QUEUE_TO_SEQ(txq_id) |
Tomas Winklerfc4b6852007-10-25 17:15:24 +08002324 INDEX_TO_SEQ(q->write_ptr)));
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002325
2326 /* Copy MAC header from skb into command buffer */
Winkler, Tomase52119c2008-12-22 11:31:19 +08002327 memcpy(tx->hdr, hdr, hdr_len);
Zhu Yib481de92007-09-25 17:54:57 -07002328
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002329 /*
2330 * Use the first empty entry in this queue's command buffer array
2331 * to contain the Tx command and MAC header concatenated together
2332 * (payload data will be in another buffer).
2333 * Size of this varies, due to varying MAC header length.
2334 * If end is not dword aligned, we'll have 2 extra bytes at the end
2335 * of the MAC header (device reads on dword boundaries).
2336 * We'll tell device about this padding later.
2337 */
Abhijeet Kolekar3832ec92008-12-19 10:37:26 +08002338 len = sizeof(struct iwl3945_tx_cmd) +
Tomas Winkler4c897252008-12-19 10:37:05 +08002339 sizeof(struct iwl_cmd_header) + hdr_len;
Zhu Yib481de92007-09-25 17:54:57 -07002340
2341 len_org = len;
2342 len = (len + 3) & ~3;
2343
2344 if (len_org != len)
2345 len_org = 1;
2346 else
2347 len_org = 0;
2348
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002349 /* Physical address of this Tx command's header (not MAC header!),
2350 * within command buffer array. */
Samuel Ortiz188cf6c2008-12-22 11:31:16 +08002351 txcmd_phys = pci_map_single(priv->pci_dev,
2352 out_cmd, sizeof(struct iwl_cmd),
2353 PCI_DMA_TODEVICE);
2354 pci_unmap_addr_set(&out_cmd->meta, mapping, txcmd_phys);
2355 pci_unmap_len_set(&out_cmd->meta, len, sizeof(struct iwl_cmd));
2356 /* Add buffer containing Tx command and MAC(!) header to TFD's
2357 * first entry */
2358 txcmd_phys += offsetof(struct iwl_cmd, hdr);
Zhu Yib481de92007-09-25 17:54:57 -07002359
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002360 /* Add buffer containing Tx command and MAC(!) header to TFD's
2361 * first entry */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002362 iwl3945_hw_txq_attach_buf_to_tfd(priv, tfd, txcmd_phys, len);
Zhu Yib481de92007-09-25 17:54:57 -07002363
Johannes Bergd0f09802008-07-29 11:32:07 +02002364 if (info->control.hw_key)
Johannes Berge039fa42008-05-15 12:55:29 +02002365 iwl3945_build_tx_cmd_hwcrypto(priv, info, out_cmd, skb, 0);
Zhu Yib481de92007-09-25 17:54:57 -07002366
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002367 /* Set up TFD's 2nd entry to point directly to remainder of skb,
2368 * if any (802.11 null frames have no payload). */
Zhu Yib481de92007-09-25 17:54:57 -07002369 len = skb->len - hdr_len;
2370 if (len) {
2371 phys_addr = pci_map_single(priv->pci_dev, skb->data + hdr_len,
2372 len, PCI_DMA_TODEVICE);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002373 iwl3945_hw_txq_attach_buf_to_tfd(priv, tfd, phys_addr, len);
Zhu Yib481de92007-09-25 17:54:57 -07002374 }
2375
Zhu Yib481de92007-09-25 17:54:57 -07002376 if (!len)
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002377 /* If there is no payload, then we use only one Tx buffer */
Winkler, Tomasdbb66542008-12-22 11:31:14 +08002378 tfd->control_flags = cpu_to_le32(TFD_CTL_COUNT_SET(1));
Zhu Yib481de92007-09-25 17:54:57 -07002379 else
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002380 /* Else use 2 buffers.
2381 * Tell 3945 about any padding after MAC header */
Winkler, Tomasdbb66542008-12-22 11:31:14 +08002382 tfd->control_flags = cpu_to_le32(TFD_CTL_COUNT_SET(2) |
2383 TFD_CTL_PAD_SET(U32_PAD(len)));
Zhu Yib481de92007-09-25 17:54:57 -07002384
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002385 /* Total # bytes to be transmitted */
Zhu Yib481de92007-09-25 17:54:57 -07002386 len = (u16)skb->len;
Winkler, Tomase52119c2008-12-22 11:31:19 +08002387 tx->len = cpu_to_le16(len);
Zhu Yib481de92007-09-25 17:54:57 -07002388
2389 /* TODO need this for burst mode later on */
Winkler, Tomase52119c2008-12-22 11:31:19 +08002390 iwl3945_build_tx_cmd_basic(priv, out_cmd, info, hdr, sta_id);
Zhu Yib481de92007-09-25 17:54:57 -07002391
2392 /* set is_hcca to 0; it probably will never be implemented */
Johannes Berge039fa42008-05-15 12:55:29 +02002393 iwl3945_hw_build_tx_cmd_rate(priv, out_cmd, info, hdr, sta_id, 0);
Zhu Yib481de92007-09-25 17:54:57 -07002394
Winkler, Tomase52119c2008-12-22 11:31:19 +08002395 tx->tx_flags &= ~TX_CMD_FLG_ANT_A_MSK;
2396 tx->tx_flags &= ~TX_CMD_FLG_ANT_B_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07002397
Harvey Harrison8b7b1e02008-06-11 14:21:56 -07002398 if (!ieee80211_has_morefrags(hdr->frame_control)) {
Zhu Yib481de92007-09-25 17:54:57 -07002399 txq->need_update = 1;
Tomas Winkler3ac7f142008-07-21 02:40:14 +03002400 if (qc)
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08002401 priv->stations_39[sta_id].tid[tid].seq_number = seq_number;
Zhu Yib481de92007-09-25 17:54:57 -07002402 } else {
2403 wait_write_ptr = 1;
2404 txq->need_update = 0;
2405 }
2406
Winkler, Tomase52119c2008-12-22 11:31:19 +08002407 iwl_print_hex_dump(priv, IWL_DL_TX, tx, sizeof(*tx));
Zhu Yib481de92007-09-25 17:54:57 -07002408
Winkler, Tomase52119c2008-12-22 11:31:19 +08002409 iwl_print_hex_dump(priv, IWL_DL_TX, (u8 *)tx->hdr,
Harvey Harrison7294ec92008-07-15 18:43:59 -07002410 ieee80211_hdrlen(fc));
Zhu Yib481de92007-09-25 17:54:57 -07002411
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002412 /* Tell device the write index *just past* this latest filled TFD */
Tomas Winklerc54b6792008-03-06 17:36:53 -08002413 q->write_ptr = iwl_queue_inc_wrap(q->write_ptr, q->n_bd);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002414 rc = iwl3945_tx_queue_update_write_ptr(priv, txq);
Zhu Yib481de92007-09-25 17:54:57 -07002415 spin_unlock_irqrestore(&priv->lock, flags);
2416
2417 if (rc)
2418 return rc;
2419
Samuel Ortizd20b3c62008-12-19 10:37:15 +08002420 if ((iwl_queue_space(q) < q->high_mark)
Zhu Yib481de92007-09-25 17:54:57 -07002421 && priv->mac80211_registered) {
2422 if (wait_write_ptr) {
2423 spin_lock_irqsave(&priv->lock, flags);
2424 txq->need_update = 1;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002425 iwl3945_tx_queue_update_write_ptr(priv, txq);
Zhu Yib481de92007-09-25 17:54:57 -07002426 spin_unlock_irqrestore(&priv->lock, flags);
2427 }
2428
Johannes Berge2530082008-05-17 00:57:14 +02002429 ieee80211_stop_queue(priv->hw, skb_get_queue_mapping(skb));
Zhu Yib481de92007-09-25 17:54:57 -07002430 }
2431
2432 return 0;
2433
2434drop_unlock:
2435 spin_unlock_irqrestore(&priv->lock, flags);
2436drop:
2437 return -1;
2438}
2439
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002440static void iwl3945_set_rate(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002441{
Johannes Berg8318d782008-01-24 19:38:38 +01002442 const struct ieee80211_supported_band *sband = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07002443 struct ieee80211_rate *rate;
2444 int i;
2445
Kolekar, Abhijeetcbba18c2008-12-19 10:37:42 +08002446 sband = iwl_get_hw_mode(priv, priv->band);
Johannes Berg8318d782008-01-24 19:38:38 +01002447 if (!sband) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08002448 IWL_ERR(priv, "Failed to set rate: unable to get hw mode\n");
Saleem Abdulrasoolc4ba9622007-11-18 23:59:08 -08002449 return;
2450 }
Zhu Yib481de92007-09-25 17:54:57 -07002451
2452 priv->active_rate = 0;
2453 priv->active_rate_basic = 0;
2454
Johannes Berg8318d782008-01-24 19:38:38 +01002455 IWL_DEBUG_RATE("Setting rates for %s GHz\n",
2456 sband->band == IEEE80211_BAND_2GHZ ? "2.4" : "5");
Zhu Yib481de92007-09-25 17:54:57 -07002457
Johannes Berg8318d782008-01-24 19:38:38 +01002458 for (i = 0; i < sband->n_bitrates; i++) {
2459 rate = &sband->bitrates[i];
2460 if ((rate->hw_value < IWL_RATE_COUNT) &&
2461 !(rate->flags & IEEE80211_CHAN_DISABLED)) {
2462 IWL_DEBUG_RATE("Adding rate index %d (plcp %d)\n",
2463 rate->hw_value, iwl3945_rates[rate->hw_value].plcp);
2464 priv->active_rate |= (1 << rate->hw_value);
2465 }
Zhu Yib481de92007-09-25 17:54:57 -07002466 }
2467
2468 IWL_DEBUG_RATE("Set active_rate = %0x, active_rate_basic = %0x\n",
2469 priv->active_rate, priv->active_rate_basic);
2470
2471 /*
2472 * If a basic rate is configured, then use it (adding IWL_RATE_1M_MASK)
2473 * otherwise set it to the default of all CCK rates and 6, 12, 24 for
2474 * OFDM
2475 */
2476 if (priv->active_rate_basic & IWL_CCK_BASIC_RATES_MASK)
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08002477 priv->staging39_rxon.cck_basic_rates =
Zhu Yib481de92007-09-25 17:54:57 -07002478 ((priv->active_rate_basic &
2479 IWL_CCK_RATES_MASK) >> IWL_FIRST_CCK_RATE) & 0xF;
2480 else
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08002481 priv->staging39_rxon.cck_basic_rates =
Zhu Yib481de92007-09-25 17:54:57 -07002482 (IWL_CCK_BASIC_RATES_MASK >> IWL_FIRST_CCK_RATE) & 0xF;
2483
2484 if (priv->active_rate_basic & IWL_OFDM_BASIC_RATES_MASK)
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08002485 priv->staging39_rxon.ofdm_basic_rates =
Zhu Yib481de92007-09-25 17:54:57 -07002486 ((priv->active_rate_basic &
2487 (IWL_OFDM_BASIC_RATES_MASK | IWL_RATE_6M_MASK)) >>
2488 IWL_FIRST_OFDM_RATE) & 0xFF;
2489 else
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08002490 priv->staging39_rxon.ofdm_basic_rates =
Zhu Yib481de92007-09-25 17:54:57 -07002491 (IWL_OFDM_BASIC_RATES_MASK >> IWL_FIRST_OFDM_RATE) & 0xFF;
2492}
2493
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002494static void iwl3945_radio_kill_sw(struct iwl_priv *priv, int disable_radio)
Zhu Yib481de92007-09-25 17:54:57 -07002495{
2496 unsigned long flags;
2497
2498 if (!!disable_radio == test_bit(STATUS_RF_KILL_SW, &priv->status))
2499 return;
2500
2501 IWL_DEBUG_RF_KILL("Manual SW RF KILL set to: RADIO %s\n",
2502 disable_radio ? "OFF" : "ON");
2503
2504 if (disable_radio) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002505 iwl3945_scan_cancel(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002506 /* FIXME: This is a workaround for AP */
Johannes Berg05c914f2008-09-11 00:01:58 +02002507 if (priv->iw_mode != NL80211_IFTYPE_AP) {
Zhu Yib481de92007-09-25 17:54:57 -07002508 spin_lock_irqsave(&priv->lock, flags);
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08002509 iwl_write32(priv, CSR_UCODE_DRV_GP1_SET,
Zhu Yib481de92007-09-25 17:54:57 -07002510 CSR_UCODE_SW_BIT_RFKILL);
2511 spin_unlock_irqrestore(&priv->lock, flags);
Samuel Ortizc4962942008-12-22 11:31:18 +08002512 iwl_send_card_state(priv, CARD_STATE_CMD_DISABLE, 0);
Zhu Yib481de92007-09-25 17:54:57 -07002513 set_bit(STATUS_RF_KILL_SW, &priv->status);
2514 }
2515 return;
2516 }
2517
2518 spin_lock_irqsave(&priv->lock, flags);
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08002519 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
Zhu Yib481de92007-09-25 17:54:57 -07002520
2521 clear_bit(STATUS_RF_KILL_SW, &priv->status);
2522 spin_unlock_irqrestore(&priv->lock, flags);
2523
2524 /* wake up ucode */
2525 msleep(10);
2526
2527 spin_lock_irqsave(&priv->lock, flags);
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08002528 iwl_read32(priv, CSR_UCODE_DRV_GP1);
2529 if (!iwl_grab_nic_access(priv))
2530 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002531 spin_unlock_irqrestore(&priv->lock, flags);
2532
2533 if (test_bit(STATUS_RF_KILL_HW, &priv->status)) {
2534 IWL_DEBUG_RF_KILL("Can not turn radio back on - "
2535 "disabled by HW switch\n");
2536 return;
2537 }
2538
Zhu Yi808e72a2008-06-12 09:47:17 +08002539 if (priv->is_open)
2540 queue_work(priv->workqueue, &priv->restart);
Zhu Yib481de92007-09-25 17:54:57 -07002541 return;
2542}
2543
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002544void iwl3945_set_decrypted_flag(struct iwl_priv *priv, struct sk_buff *skb,
Zhu Yib481de92007-09-25 17:54:57 -07002545 u32 decrypt_res, struct ieee80211_rx_status *stats)
2546{
2547 u16 fc =
2548 le16_to_cpu(((struct ieee80211_hdr *)skb->data)->frame_control);
2549
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08002550 if (priv->active39_rxon.filter_flags & RXON_FILTER_DIS_DECRYPT_MSK)
Zhu Yib481de92007-09-25 17:54:57 -07002551 return;
2552
2553 if (!(fc & IEEE80211_FCTL_PROTECTED))
2554 return;
2555
2556 IWL_DEBUG_RX("decrypt_res:0x%x\n", decrypt_res);
2557 switch (decrypt_res & RX_RES_STATUS_SEC_TYPE_MSK) {
2558 case RX_RES_STATUS_SEC_TYPE_TKIP:
2559 if ((decrypt_res & RX_RES_STATUS_DECRYPT_TYPE_MSK) ==
2560 RX_RES_STATUS_BAD_ICV_MIC)
2561 stats->flag |= RX_FLAG_MMIC_ERROR;
2562 case RX_RES_STATUS_SEC_TYPE_WEP:
2563 case RX_RES_STATUS_SEC_TYPE_CCMP:
2564 if ((decrypt_res & RX_RES_STATUS_DECRYPT_TYPE_MSK) ==
2565 RX_RES_STATUS_DECRYPT_OK) {
2566 IWL_DEBUG_RX("hw decrypt successfully!!!\n");
2567 stats->flag |= RX_FLAG_DECRYPTED;
2568 }
2569 break;
2570
2571 default:
2572 break;
2573 }
2574}
2575
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08002576#ifdef CONFIG_IWL3945_SPECTRUM_MEASUREMENT
Zhu Yib481de92007-09-25 17:54:57 -07002577
2578#include "iwl-spectrum.h"
2579
2580#define BEACON_TIME_MASK_LOW 0x00FFFFFF
2581#define BEACON_TIME_MASK_HIGH 0xFF000000
2582#define TIME_UNIT 1024
2583
2584/*
2585 * extended beacon time format
2586 * time in usec will be changed into a 32-bit value in 8:24 format
2587 * the high 1 byte is the beacon counts
2588 * the lower 3 bytes is the time in usec within one beacon interval
2589 */
2590
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002591static u32 iwl3945_usecs_to_beacons(u32 usec, u32 beacon_interval)
Zhu Yib481de92007-09-25 17:54:57 -07002592{
2593 u32 quot;
2594 u32 rem;
2595 u32 interval = beacon_interval * 1024;
2596
2597 if (!interval || !usec)
2598 return 0;
2599
2600 quot = (usec / interval) & (BEACON_TIME_MASK_HIGH >> 24);
2601 rem = (usec % interval) & BEACON_TIME_MASK_LOW;
2602
2603 return (quot << 24) + rem;
2604}
2605
2606/* base is usually what we get from ucode with each received frame,
2607 * the same as HW timer counter counting down
2608 */
2609
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002610static __le32 iwl3945_add_beacon_time(u32 base, u32 addon, u32 beacon_interval)
Zhu Yib481de92007-09-25 17:54:57 -07002611{
2612 u32 base_low = base & BEACON_TIME_MASK_LOW;
2613 u32 addon_low = addon & BEACON_TIME_MASK_LOW;
2614 u32 interval = beacon_interval * TIME_UNIT;
2615 u32 res = (base & BEACON_TIME_MASK_HIGH) +
2616 (addon & BEACON_TIME_MASK_HIGH);
2617
2618 if (base_low > addon_low)
2619 res += base_low - addon_low;
2620 else if (base_low < addon_low) {
2621 res += interval + base_low - addon_low;
2622 res += (1 << 24);
2623 } else
2624 res += (1 << 24);
2625
2626 return cpu_to_le32(res);
2627}
2628
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002629static int iwl3945_get_measurement(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07002630 struct ieee80211_measurement_params *params,
2631 u8 type)
2632{
Tomas Winkler600c0e12008-12-19 10:37:04 +08002633 struct iwl_spectrum_cmd spectrum;
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08002634 struct iwl_rx_packet *res;
Winkler, Tomasc2d79b42008-12-19 10:37:34 +08002635 struct iwl_host_cmd cmd = {
Zhu Yib481de92007-09-25 17:54:57 -07002636 .id = REPLY_SPECTRUM_MEASUREMENT_CMD,
2637 .data = (void *)&spectrum,
2638 .meta.flags = CMD_WANT_SKB,
2639 };
2640 u32 add_time = le64_to_cpu(params->start_time);
2641 int rc;
2642 int spectrum_resp_status;
2643 int duration = le16_to_cpu(params->duration);
2644
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002645 if (iwl3945_is_associated(priv))
Zhu Yib481de92007-09-25 17:54:57 -07002646 add_time =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002647 iwl3945_usecs_to_beacons(
Zhu Yib481de92007-09-25 17:54:57 -07002648 le64_to_cpu(params->start_time) - priv->last_tsf,
2649 le16_to_cpu(priv->rxon_timing.beacon_interval));
2650
2651 memset(&spectrum, 0, sizeof(spectrum));
2652
2653 spectrum.channel_count = cpu_to_le16(1);
2654 spectrum.flags =
2655 RXON_FLG_TSF2HOST_MSK | RXON_FLG_ANT_A_MSK | RXON_FLG_DIS_DIV_MSK;
2656 spectrum.filter_flags = MEASUREMENT_FILTER_FLAG;
2657 cmd.len = sizeof(spectrum);
2658 spectrum.len = cpu_to_le16(cmd.len - sizeof(spectrum.len));
2659
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002660 if (iwl3945_is_associated(priv))
Zhu Yib481de92007-09-25 17:54:57 -07002661 spectrum.start_time =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002662 iwl3945_add_beacon_time(priv->last_beacon_time,
Zhu Yib481de92007-09-25 17:54:57 -07002663 add_time,
2664 le16_to_cpu(priv->rxon_timing.beacon_interval));
2665 else
2666 spectrum.start_time = 0;
2667
2668 spectrum.channels[0].duration = cpu_to_le32(duration * TIME_UNIT);
2669 spectrum.channels[0].channel = params->channel;
2670 spectrum.channels[0].type = type;
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08002671 if (priv->active39_rxon.flags & RXON_FLG_BAND_24G_MSK)
Zhu Yib481de92007-09-25 17:54:57 -07002672 spectrum.flags |= RXON_FLG_BAND_24G_MSK |
2673 RXON_FLG_AUTO_DETECT_MSK | RXON_FLG_TGG_PROTECT_MSK;
2674
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002675 rc = iwl3945_send_cmd_sync(priv, &cmd);
Zhu Yib481de92007-09-25 17:54:57 -07002676 if (rc)
2677 return rc;
2678
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08002679 res = (struct iwl_rx_packet *)cmd.meta.u.skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07002680 if (res->hdr.flags & IWL_CMD_FAILED_MSK) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08002681 IWL_ERR(priv, "Bad return from REPLY_RX_ON_ASSOC command\n");
Zhu Yib481de92007-09-25 17:54:57 -07002682 rc = -EIO;
2683 }
2684
2685 spectrum_resp_status = le16_to_cpu(res->u.spectrum.status);
2686 switch (spectrum_resp_status) {
2687 case 0: /* Command will be handled */
2688 if (res->u.spectrum.id != 0xff) {
Ian Schrambc434dd2007-10-25 17:15:29 +08002689 IWL_DEBUG_INFO("Replaced existing measurement: %d\n",
2690 res->u.spectrum.id);
Zhu Yib481de92007-09-25 17:54:57 -07002691 priv->measurement_status &= ~MEASUREMENT_READY;
2692 }
2693 priv->measurement_status |= MEASUREMENT_ACTIVE;
2694 rc = 0;
2695 break;
2696
2697 case 1: /* Command will not be handled */
2698 rc = -EAGAIN;
2699 break;
2700 }
2701
2702 dev_kfree_skb_any(cmd.meta.u.skb);
2703
2704 return rc;
2705}
2706#endif
2707
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002708static void iwl3945_rx_reply_alive(struct iwl_priv *priv,
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08002709 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07002710{
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08002711 struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
2712 struct iwl_alive_resp *palive;
Zhu Yib481de92007-09-25 17:54:57 -07002713 struct delayed_work *pwork;
2714
2715 palive = &pkt->u.alive_frame;
2716
2717 IWL_DEBUG_INFO("Alive ucode status 0x%08X revision "
2718 "0x%01X 0x%01X\n",
2719 palive->is_valid, palive->ver_type,
2720 palive->ver_subtype);
2721
2722 if (palive->ver_subtype == INITIALIZE_SUBTYPE) {
2723 IWL_DEBUG_INFO("Initialization Alive received.\n");
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08002724 memcpy(&priv->card_alive_init, &pkt->u.alive_frame,
2725 sizeof(struct iwl_alive_resp));
Zhu Yib481de92007-09-25 17:54:57 -07002726 pwork = &priv->init_alive_start;
2727 } else {
2728 IWL_DEBUG_INFO("Runtime Alive received.\n");
2729 memcpy(&priv->card_alive, &pkt->u.alive_frame,
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08002730 sizeof(struct iwl_alive_resp));
Zhu Yib481de92007-09-25 17:54:57 -07002731 pwork = &priv->alive_start;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002732 iwl3945_disable_events(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002733 }
2734
2735 /* We delay the ALIVE response by 5ms to
2736 * give the HW RF Kill time to activate... */
2737 if (palive->is_valid == UCODE_VALID_OK)
2738 queue_delayed_work(priv->workqueue, pwork,
2739 msecs_to_jiffies(5));
2740 else
Winkler, Tomas39aadf82008-12-19 10:37:32 +08002741 IWL_WARN(priv, "uCode did not respond OK.\n");
Zhu Yib481de92007-09-25 17:54:57 -07002742}
2743
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002744static void iwl3945_rx_reply_add_sta(struct iwl_priv *priv,
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08002745 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07002746{
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08002747 struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07002748
2749 IWL_DEBUG_RX("Received REPLY_ADD_STA: 0x%02X\n", pkt->u.status);
2750 return;
2751}
2752
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002753static void iwl3945_rx_reply_error(struct iwl_priv *priv,
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08002754 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07002755{
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08002756 struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07002757
Winkler, Tomas15b16872008-12-19 10:37:33 +08002758 IWL_ERR(priv, "Error Reply type 0x%08X cmd %s (0x%02X) "
Zhu Yib481de92007-09-25 17:54:57 -07002759 "seq 0x%04X ser 0x%08X\n",
2760 le32_to_cpu(pkt->u.err_resp.error_type),
2761 get_cmd_string(pkt->u.err_resp.cmd_id),
2762 pkt->u.err_resp.cmd_id,
2763 le16_to_cpu(pkt->u.err_resp.bad_cmd_seq_num),
2764 le32_to_cpu(pkt->u.err_resp.error_info));
2765}
2766
2767#define TX_STATUS_ENTRY(x) case TX_STATUS_FAIL_ ## x: return #x
2768
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002769static void iwl3945_rx_csa(struct iwl_priv *priv, struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07002770{
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08002771 struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08002772 struct iwl3945_rxon_cmd *rxon = (void *)&priv->active39_rxon;
Tomas Winkler600c0e12008-12-19 10:37:04 +08002773 struct iwl_csa_notification *csa = &(pkt->u.csa_notif);
Zhu Yib481de92007-09-25 17:54:57 -07002774 IWL_DEBUG_11H("CSA notif: channel %d, status %d\n",
2775 le16_to_cpu(csa->channel), le32_to_cpu(csa->status));
2776 rxon->channel = csa->channel;
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08002777 priv->staging39_rxon.channel = csa->channel;
Zhu Yib481de92007-09-25 17:54:57 -07002778}
2779
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002780static void iwl3945_rx_spectrum_measure_notif(struct iwl_priv *priv,
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08002781 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07002782{
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08002783#ifdef CONFIG_IWL3945_SPECTRUM_MEASUREMENT
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08002784 struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
Tomas Winkler600c0e12008-12-19 10:37:04 +08002785 struct iwl_spectrum_notification *report = &(pkt->u.spectrum_notif);
Zhu Yib481de92007-09-25 17:54:57 -07002786
2787 if (!report->state) {
2788 IWL_DEBUG(IWL_DL_11H | IWL_DL_INFO,
2789 "Spectrum Measure Notification: Start\n");
2790 return;
2791 }
2792
2793 memcpy(&priv->measure_report, report, sizeof(*report));
2794 priv->measurement_status |= MEASUREMENT_READY;
2795#endif
2796}
2797
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002798static void iwl3945_rx_pm_sleep_notif(struct iwl_priv *priv,
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08002799 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07002800{
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08002801#ifdef CONFIG_IWL3945_DEBUG
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08002802 struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
Tomas Winkler600c0e12008-12-19 10:37:04 +08002803 struct iwl_sleep_notification *sleep = &(pkt->u.sleep_notif);
Zhu Yib481de92007-09-25 17:54:57 -07002804 IWL_DEBUG_RX("sleep mode: %d, src: %d\n",
2805 sleep->pm_sleep_mode, sleep->pm_wakeup_src);
2806#endif
2807}
2808
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002809static void iwl3945_rx_pm_debug_statistics_notif(struct iwl_priv *priv,
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08002810 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07002811{
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08002812 struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07002813 IWL_DEBUG_RADIO("Dumping %d bytes of unhandled "
2814 "notification for %s:\n",
2815 le32_to_cpu(pkt->len), get_cmd_string(pkt->hdr.cmd));
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08002816 iwl_print_hex_dump(priv, IWL_DL_RADIO, pkt->u.raw,
2817 le32_to_cpu(pkt->len));
Zhu Yib481de92007-09-25 17:54:57 -07002818}
2819
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002820static void iwl3945_bg_beacon_update(struct work_struct *work)
Zhu Yib481de92007-09-25 17:54:57 -07002821{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002822 struct iwl_priv *priv =
2823 container_of(work, struct iwl_priv, beacon_update);
Zhu Yib481de92007-09-25 17:54:57 -07002824 struct sk_buff *beacon;
2825
2826 /* Pull updated AP beacon from mac80211. will fail if not in AP mode */
Johannes Berge039fa42008-05-15 12:55:29 +02002827 beacon = ieee80211_beacon_get(priv->hw, priv->vif);
Zhu Yib481de92007-09-25 17:54:57 -07002828
2829 if (!beacon) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08002830 IWL_ERR(priv, "update beacon failed\n");
Zhu Yib481de92007-09-25 17:54:57 -07002831 return;
2832 }
2833
2834 mutex_lock(&priv->mutex);
2835 /* new beacon skb is allocated every time; dispose previous.*/
2836 if (priv->ibss_beacon)
2837 dev_kfree_skb(priv->ibss_beacon);
2838
2839 priv->ibss_beacon = beacon;
2840 mutex_unlock(&priv->mutex);
2841
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002842 iwl3945_send_beacon_cmd(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002843}
2844
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002845static void iwl3945_rx_beacon_notif(struct iwl_priv *priv,
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08002846 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07002847{
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08002848#ifdef CONFIG_IWL3945_DEBUG
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08002849 struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002850 struct iwl3945_beacon_notif *beacon = &(pkt->u.beacon_status);
Zhu Yib481de92007-09-25 17:54:57 -07002851 u8 rate = beacon->beacon_notify_hdr.rate;
2852
2853 IWL_DEBUG_RX("beacon status %x retries %d iss %d "
2854 "tsf %d %d rate %d\n",
2855 le32_to_cpu(beacon->beacon_notify_hdr.status) & TX_STATUS_MSK,
2856 beacon->beacon_notify_hdr.failure_frame,
2857 le32_to_cpu(beacon->ibss_mgr_status),
2858 le32_to_cpu(beacon->high_tsf),
2859 le32_to_cpu(beacon->low_tsf), rate);
2860#endif
2861
Johannes Berg05c914f2008-09-11 00:01:58 +02002862 if ((priv->iw_mode == NL80211_IFTYPE_AP) &&
Zhu Yib481de92007-09-25 17:54:57 -07002863 (!test_bit(STATUS_EXIT_PENDING, &priv->status)))
2864 queue_work(priv->workqueue, &priv->beacon_update);
2865}
2866
2867/* Service response to REPLY_SCAN_CMD (0x80) */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002868static void iwl3945_rx_reply_scan(struct iwl_priv *priv,
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08002869 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07002870{
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08002871#ifdef CONFIG_IWL3945_DEBUG
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08002872 struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
Tomas Winkler4c897252008-12-19 10:37:05 +08002873 struct iwl_scanreq_notification *notif =
2874 (struct iwl_scanreq_notification *)pkt->u.raw;
Zhu Yib481de92007-09-25 17:54:57 -07002875
2876 IWL_DEBUG_RX("Scan request status = 0x%x\n", notif->status);
2877#endif
2878}
2879
2880/* Service SCAN_START_NOTIFICATION (0x82) */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002881static void iwl3945_rx_scan_start_notif(struct iwl_priv *priv,
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08002882 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07002883{
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08002884 struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
Tomas Winkler4c897252008-12-19 10:37:05 +08002885 struct iwl_scanstart_notification *notif =
2886 (struct iwl_scanstart_notification *)pkt->u.raw;
Zhu Yib481de92007-09-25 17:54:57 -07002887 priv->scan_start_tsf = le32_to_cpu(notif->tsf_low);
2888 IWL_DEBUG_SCAN("Scan start: "
2889 "%d [802.11%s] "
2890 "(TSF: 0x%08X:%08X) - %d (beacon timer %u)\n",
2891 notif->channel,
2892 notif->band ? "bg" : "a",
2893 notif->tsf_high,
2894 notif->tsf_low, notif->status, notif->beacon_timer);
2895}
2896
2897/* Service SCAN_RESULTS_NOTIFICATION (0x83) */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002898static void iwl3945_rx_scan_results_notif(struct iwl_priv *priv,
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08002899 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07002900{
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08002901 struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
Tomas Winkler4c897252008-12-19 10:37:05 +08002902 struct iwl_scanresults_notification *notif =
2903 (struct iwl_scanresults_notification *)pkt->u.raw;
Zhu Yib481de92007-09-25 17:54:57 -07002904
2905 IWL_DEBUG_SCAN("Scan ch.res: "
2906 "%d [802.11%s] "
2907 "(TSF: 0x%08X:%08X) - %d "
2908 "elapsed=%lu usec (%dms since last)\n",
2909 notif->channel,
2910 notif->band ? "bg" : "a",
2911 le32_to_cpu(notif->tsf_high),
2912 le32_to_cpu(notif->tsf_low),
2913 le32_to_cpu(notif->statistics[0]),
2914 le32_to_cpu(notif->tsf_low) - priv->scan_start_tsf,
2915 jiffies_to_msecs(elapsed_jiffies
2916 (priv->last_scan_jiffies, jiffies)));
2917
2918 priv->last_scan_jiffies = jiffies;
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08002919 priv->next_scan_jiffies = 0;
Zhu Yib481de92007-09-25 17:54:57 -07002920}
2921
2922/* Service SCAN_COMPLETE_NOTIFICATION (0x84) */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002923static void iwl3945_rx_scan_complete_notif(struct iwl_priv *priv,
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08002924 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07002925{
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08002926 struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
Tomas Winkler4c897252008-12-19 10:37:05 +08002927 struct iwl_scancomplete_notification *scan_notif = (void *)pkt->u.raw;
Zhu Yib481de92007-09-25 17:54:57 -07002928
2929 IWL_DEBUG_SCAN("Scan complete: %d channels (TSF 0x%08X:%08X) - %d\n",
2930 scan_notif->scanned_channels,
2931 scan_notif->tsf_low,
2932 scan_notif->tsf_high, scan_notif->status);
2933
2934 /* The HW is no longer scanning */
2935 clear_bit(STATUS_SCAN_HW, &priv->status);
2936
2937 /* The scan completion notification came in, so kill that timer... */
2938 cancel_delayed_work(&priv->scan_check);
2939
2940 IWL_DEBUG_INFO("Scan pass on %sGHz took %dms\n",
Ron Rindjunsky66b50042008-06-25 16:46:31 +08002941 (priv->scan_bands & BIT(IEEE80211_BAND_2GHZ)) ?
2942 "2.4" : "5.2",
Zhu Yib481de92007-09-25 17:54:57 -07002943 jiffies_to_msecs(elapsed_jiffies
2944 (priv->scan_pass_start, jiffies)));
2945
Ron Rindjunsky66b50042008-06-25 16:46:31 +08002946 /* Remove this scanned band from the list of pending
2947 * bands to scan, band G precedes A in order of scanning
2948 * as seen in iwl3945_bg_request_scan */
2949 if (priv->scan_bands & BIT(IEEE80211_BAND_2GHZ))
2950 priv->scan_bands &= ~BIT(IEEE80211_BAND_2GHZ);
2951 else if (priv->scan_bands & BIT(IEEE80211_BAND_5GHZ))
2952 priv->scan_bands &= ~BIT(IEEE80211_BAND_5GHZ);
Zhu Yib481de92007-09-25 17:54:57 -07002953
2954 /* If a request to abort was given, or the scan did not succeed
2955 * then we reset the scan state machine and terminate,
2956 * re-queuing another scan if one has been requested */
2957 if (test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
2958 IWL_DEBUG_INFO("Aborted scan completed.\n");
2959 clear_bit(STATUS_SCAN_ABORTING, &priv->status);
2960 } else {
2961 /* If there are more bands on this scan pass reschedule */
2962 if (priv->scan_bands > 0)
2963 goto reschedule;
2964 }
2965
2966 priv->last_scan_jiffies = jiffies;
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08002967 priv->next_scan_jiffies = 0;
Zhu Yib481de92007-09-25 17:54:57 -07002968 IWL_DEBUG_INFO("Setting scan to off\n");
2969
2970 clear_bit(STATUS_SCANNING, &priv->status);
2971
2972 IWL_DEBUG_INFO("Scan took %dms\n",
2973 jiffies_to_msecs(elapsed_jiffies(priv->scan_start, jiffies)));
2974
2975 queue_work(priv->workqueue, &priv->scan_completed);
2976
2977 return;
2978
2979reschedule:
2980 priv->scan_pass_start = jiffies;
2981 queue_work(priv->workqueue, &priv->request_scan);
2982}
2983
2984/* Handle notification from uCode that card's power state is changing
2985 * due to software, hardware, or critical temperature RFKILL */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002986static void iwl3945_rx_card_state_notif(struct iwl_priv *priv,
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08002987 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07002988{
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08002989 struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07002990 u32 flags = le32_to_cpu(pkt->u.card_state_notif.flags);
2991 unsigned long status = priv->status;
2992
2993 IWL_DEBUG_RF_KILL("Card state received: HW:%s SW:%s\n",
2994 (flags & HW_CARD_DISABLED) ? "Kill" : "On",
2995 (flags & SW_CARD_DISABLED) ? "Kill" : "On");
2996
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08002997 iwl_write32(priv, CSR_UCODE_DRV_GP1_SET,
Zhu Yib481de92007-09-25 17:54:57 -07002998 CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
2999
3000 if (flags & HW_CARD_DISABLED)
3001 set_bit(STATUS_RF_KILL_HW, &priv->status);
3002 else
3003 clear_bit(STATUS_RF_KILL_HW, &priv->status);
3004
3005
3006 if (flags & SW_CARD_DISABLED)
3007 set_bit(STATUS_RF_KILL_SW, &priv->status);
3008 else
3009 clear_bit(STATUS_RF_KILL_SW, &priv->status);
3010
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003011 iwl3945_scan_cancel(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003012
3013 if ((test_bit(STATUS_RF_KILL_HW, &status) !=
3014 test_bit(STATUS_RF_KILL_HW, &priv->status)) ||
3015 (test_bit(STATUS_RF_KILL_SW, &status) !=
3016 test_bit(STATUS_RF_KILL_SW, &priv->status)))
3017 queue_work(priv->workqueue, &priv->rf_kill);
3018 else
3019 wake_up_interruptible(&priv->wait_command_queue);
3020}
3021
3022/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003023 * iwl3945_setup_rx_handlers - Initialize Rx handler callbacks
Zhu Yib481de92007-09-25 17:54:57 -07003024 *
3025 * Setup the RX handlers for each of the reply types sent from the uCode
3026 * to the host.
3027 *
3028 * This function chains into the hardware specific files for them to setup
3029 * any hardware specific handlers as well.
3030 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003031static void iwl3945_setup_rx_handlers(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003032{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003033 priv->rx_handlers[REPLY_ALIVE] = iwl3945_rx_reply_alive;
3034 priv->rx_handlers[REPLY_ADD_STA] = iwl3945_rx_reply_add_sta;
3035 priv->rx_handlers[REPLY_ERROR] = iwl3945_rx_reply_error;
3036 priv->rx_handlers[CHANNEL_SWITCH_NOTIFICATION] = iwl3945_rx_csa;
Zhu Yib481de92007-09-25 17:54:57 -07003037 priv->rx_handlers[SPECTRUM_MEASURE_NOTIFICATION] =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003038 iwl3945_rx_spectrum_measure_notif;
3039 priv->rx_handlers[PM_SLEEP_NOTIFICATION] = iwl3945_rx_pm_sleep_notif;
Zhu Yib481de92007-09-25 17:54:57 -07003040 priv->rx_handlers[PM_DEBUG_STATISTIC_NOTIFIC] =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003041 iwl3945_rx_pm_debug_statistics_notif;
3042 priv->rx_handlers[BEACON_NOTIFICATION] = iwl3945_rx_beacon_notif;
Zhu Yib481de92007-09-25 17:54:57 -07003043
Ben Cahill9fbab512007-11-29 11:09:47 +08003044 /*
3045 * The same handler is used for both the REPLY to a discrete
3046 * statistics request from the host as well as for the periodic
3047 * statistics notifications (after received beacons) from the uCode.
Zhu Yib481de92007-09-25 17:54:57 -07003048 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003049 priv->rx_handlers[REPLY_STATISTICS_CMD] = iwl3945_hw_rx_statistics;
3050 priv->rx_handlers[STATISTICS_NOTIFICATION] = iwl3945_hw_rx_statistics;
Zhu Yib481de92007-09-25 17:54:57 -07003051
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003052 priv->rx_handlers[REPLY_SCAN_CMD] = iwl3945_rx_reply_scan;
3053 priv->rx_handlers[SCAN_START_NOTIFICATION] = iwl3945_rx_scan_start_notif;
Zhu Yib481de92007-09-25 17:54:57 -07003054 priv->rx_handlers[SCAN_RESULTS_NOTIFICATION] =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003055 iwl3945_rx_scan_results_notif;
Zhu Yib481de92007-09-25 17:54:57 -07003056 priv->rx_handlers[SCAN_COMPLETE_NOTIFICATION] =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003057 iwl3945_rx_scan_complete_notif;
3058 priv->rx_handlers[CARD_STATE_NOTIFICATION] = iwl3945_rx_card_state_notif;
Zhu Yib481de92007-09-25 17:54:57 -07003059
Ben Cahill9fbab512007-11-29 11:09:47 +08003060 /* Set up hardware specific Rx handlers */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003061 iwl3945_hw_rx_handler_setup(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003062}
3063
3064/**
Tomas Winkler91c066f2008-03-06 17:36:55 -08003065 * iwl3945_cmd_queue_reclaim - Reclaim CMD queue entries
3066 * When FW advances 'R' index, all entries between old and new 'R' index
3067 * need to be reclaimed.
3068 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003069static void iwl3945_cmd_queue_reclaim(struct iwl_priv *priv,
Tomas Winkler91c066f2008-03-06 17:36:55 -08003070 int txq_id, int index)
3071{
Samuel Ortiz188cf6c2008-12-22 11:31:16 +08003072 struct iwl_tx_queue *txq = &priv->txq[txq_id];
Samuel Ortizd20b3c62008-12-19 10:37:15 +08003073 struct iwl_queue *q = &txq->q;
Tomas Winkler91c066f2008-03-06 17:36:55 -08003074 int nfreed = 0;
3075
Winkler, Tomas625a3812009-01-08 10:19:55 -08003076 if ((index >= q->n_bd) || (iwl_queue_used(q, index) == 0)) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08003077 IWL_ERR(priv, "Read index for DMA queue txq id (%d), index %d, "
Tomas Winkler91c066f2008-03-06 17:36:55 -08003078 "is out of range [0-%d] %d %d.\n", txq_id,
3079 index, q->n_bd, q->write_ptr, q->read_ptr);
3080 return;
3081 }
3082
3083 for (index = iwl_queue_inc_wrap(index, q->n_bd); q->read_ptr != index;
3084 q->read_ptr = iwl_queue_inc_wrap(q->read_ptr, q->n_bd)) {
3085 if (nfreed > 1) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08003086 IWL_ERR(priv, "HCMD skipped: index (%d) %d %d\n", index,
Tomas Winkler91c066f2008-03-06 17:36:55 -08003087 q->write_ptr, q->read_ptr);
3088 queue_work(priv->workqueue, &priv->restart);
3089 break;
3090 }
3091 nfreed++;
3092 }
3093}
3094
3095
3096/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003097 * iwl3945_tx_cmd_complete - Pull unused buffers off the queue and reclaim them
Zhu Yib481de92007-09-25 17:54:57 -07003098 * @rxb: Rx buffer to reclaim
3099 *
3100 * If an Rx buffer has an async callback associated with it the callback
3101 * will be executed. The attached skb (if present) will only be freed
3102 * if the callback returns 1
3103 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003104static void iwl3945_tx_cmd_complete(struct iwl_priv *priv,
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08003105 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003106{
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08003107 struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07003108 u16 sequence = le16_to_cpu(pkt->hdr.sequence);
3109 int txq_id = SEQ_TO_QUEUE(sequence);
3110 int index = SEQ_TO_INDEX(sequence);
Tomas Winkler600c0e12008-12-19 10:37:04 +08003111 int huge = !!(pkt->hdr.sequence & SEQ_HUGE_FRAME);
Zhu Yib481de92007-09-25 17:54:57 -07003112 int cmd_index;
Winkler, Tomasc2d79b42008-12-19 10:37:34 +08003113 struct iwl_cmd *cmd;
Zhu Yib481de92007-09-25 17:54:57 -07003114
Zhu Yib481de92007-09-25 17:54:57 -07003115 BUG_ON(txq_id != IWL_CMD_QUEUE_NUM);
3116
Samuel Ortiz188cf6c2008-12-22 11:31:16 +08003117 cmd_index = get_cmd_index(&priv->txq[IWL_CMD_QUEUE_NUM].q, index, huge);
3118 cmd = priv->txq[IWL_CMD_QUEUE_NUM].cmd[cmd_index];
Zhu Yib481de92007-09-25 17:54:57 -07003119
3120 /* Input error checking is done when commands are added to queue. */
3121 if (cmd->meta.flags & CMD_WANT_SKB) {
3122 cmd->meta.source->u.skb = rxb->skb;
3123 rxb->skb = NULL;
3124 } else if (cmd->meta.u.callback &&
3125 !cmd->meta.u.callback(priv, cmd, rxb->skb))
3126 rxb->skb = NULL;
3127
Tomas Winkler91c066f2008-03-06 17:36:55 -08003128 iwl3945_cmd_queue_reclaim(priv, txq_id, index);
Zhu Yib481de92007-09-25 17:54:57 -07003129
3130 if (!(cmd->meta.flags & CMD_ASYNC)) {
3131 clear_bit(STATUS_HCMD_ACTIVE, &priv->status);
3132 wake_up_interruptible(&priv->wait_command_queue);
3133 }
3134}
3135
3136/************************** RX-FUNCTIONS ****************************/
3137/*
3138 * Rx theory of operation
3139 *
3140 * The host allocates 32 DMA target addresses and passes the host address
3141 * to the firmware at register IWL_RFDS_TABLE_LOWER + N * RFD_SIZE where N is
3142 * 0 to 31
3143 *
3144 * Rx Queue Indexes
3145 * The host/firmware share two index registers for managing the Rx buffers.
3146 *
3147 * The READ index maps to the first position that the firmware may be writing
3148 * to -- the driver can read up to (but not including) this position and get
3149 * good data.
3150 * The READ index is managed by the firmware once the card is enabled.
3151 *
3152 * The WRITE index maps to the last position the driver has read from -- the
3153 * position preceding WRITE is the last slot the firmware can place a packet.
3154 *
3155 * The queue is empty (no good data) if WRITE = READ - 1, and is full if
3156 * WRITE = READ.
3157 *
Ben Cahill9fbab512007-11-29 11:09:47 +08003158 * During initialization, the host sets up the READ queue position to the first
Zhu Yib481de92007-09-25 17:54:57 -07003159 * INDEX position, and WRITE to the last (READ - 1 wrapped)
3160 *
Ben Cahill9fbab512007-11-29 11:09:47 +08003161 * When the firmware places a packet in a buffer, it will advance the READ index
Zhu Yib481de92007-09-25 17:54:57 -07003162 * and fire the RX interrupt. The driver can then query the READ index and
3163 * process as many packets as possible, moving the WRITE index forward as it
3164 * resets the Rx queue buffers with new memory.
3165 *
3166 * The management in the driver is as follows:
3167 * + A list of pre-allocated SKBs is stored in iwl->rxq->rx_free. When
3168 * iwl->rxq->free_count drops to or below RX_LOW_WATERMARK, work is scheduled
Ian Schram01ebd062007-10-25 17:15:22 +08003169 * to replenish the iwl->rxq->rx_free.
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003170 * + In iwl3945_rx_replenish (scheduled) if 'processed' != 'read' then the
Zhu Yib481de92007-09-25 17:54:57 -07003171 * iwl->rxq is replenished and the READ INDEX is updated (updating the
3172 * 'processed' and 'read' driver indexes as well)
3173 * + A received packet is processed and handed to the kernel network stack,
3174 * detached from the iwl->rxq. The driver 'processed' index is updated.
3175 * + The Host/Firmware iwl->rxq is replenished at tasklet time from the rx_free
3176 * list. If there are no allocated buffers in iwl->rxq->rx_free, the READ
3177 * INDEX is not incremented and iwl->status(RX_STALLED) is set. If there
3178 * were enough free buffers and RX_STALLED is set it is cleared.
3179 *
3180 *
3181 * Driver sequence:
3182 *
Ben Cahill9fbab512007-11-29 11:09:47 +08003183 * iwl3945_rx_replenish() Replenishes rx_free list from rx_used, and calls
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003184 * iwl3945_rx_queue_restock
Ben Cahill9fbab512007-11-29 11:09:47 +08003185 * iwl3945_rx_queue_restock() Moves available buffers from rx_free into Rx
Zhu Yib481de92007-09-25 17:54:57 -07003186 * queue, updates firmware pointers, and updates
3187 * the WRITE index. If insufficient rx_free buffers
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003188 * are available, schedules iwl3945_rx_replenish
Zhu Yib481de92007-09-25 17:54:57 -07003189 *
3190 * -- enable interrupts --
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08003191 * ISR - iwl3945_rx() Detach iwl_rx_mem_buffers from pool up to the
Zhu Yib481de92007-09-25 17:54:57 -07003192 * READ INDEX, detaching the SKB from the pool.
3193 * Moves the packet buffer from queue to rx_used.
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003194 * Calls iwl3945_rx_queue_restock to refill any empty
Zhu Yib481de92007-09-25 17:54:57 -07003195 * slots.
3196 * ...
3197 *
3198 */
3199
3200/**
Ben Cahill9fbab512007-11-29 11:09:47 +08003201 * iwl3945_dma_addr2rbd_ptr - convert a DMA address to a uCode read buffer ptr
Zhu Yib481de92007-09-25 17:54:57 -07003202 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003203static inline __le32 iwl3945_dma_addr2rbd_ptr(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07003204 dma_addr_t dma_addr)
3205{
3206 return cpu_to_le32((u32)dma_addr);
3207}
3208
3209/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003210 * iwl3945_rx_queue_restock - refill RX queue from pre-allocated pool
Zhu Yib481de92007-09-25 17:54:57 -07003211 *
Ben Cahill9fbab512007-11-29 11:09:47 +08003212 * If there are slots in the RX queue that need to be restocked,
Zhu Yib481de92007-09-25 17:54:57 -07003213 * and we have free pre-allocated buffers, fill the ranks as much
Ben Cahill9fbab512007-11-29 11:09:47 +08003214 * as we can, pulling from rx_free.
Zhu Yib481de92007-09-25 17:54:57 -07003215 *
3216 * This moves the 'write' index forward to catch up with 'processed', and
3217 * also updates the memory address in the firmware to reference the new
3218 * target buffer.
3219 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003220static int iwl3945_rx_queue_restock(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003221{
Abhijeet Kolekarcc2f3622008-12-19 10:37:25 +08003222 struct iwl_rx_queue *rxq = &priv->rxq;
Zhu Yib481de92007-09-25 17:54:57 -07003223 struct list_head *element;
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08003224 struct iwl_rx_mem_buffer *rxb;
Zhu Yib481de92007-09-25 17:54:57 -07003225 unsigned long flags;
3226 int write, rc;
3227
3228 spin_lock_irqsave(&rxq->lock, flags);
3229 write = rxq->write & ~0x7;
Winkler, Tomas37d68312009-01-08 10:19:54 -08003230 while ((iwl_rx_queue_space(rxq) > 0) && (rxq->free_count)) {
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003231 /* Get next free Rx buffer, remove from free list */
Zhu Yib481de92007-09-25 17:54:57 -07003232 element = rxq->rx_free.next;
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08003233 rxb = list_entry(element, struct iwl_rx_mem_buffer, list);
Zhu Yib481de92007-09-25 17:54:57 -07003234 list_del(element);
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003235
3236 /* Point to Rx buffer via next RBD in circular buffer */
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08003237 rxq->bd[rxq->write] = iwl3945_dma_addr2rbd_ptr(priv, rxb->real_dma_addr);
Zhu Yib481de92007-09-25 17:54:57 -07003238 rxq->queue[rxq->write] = rxb;
3239 rxq->write = (rxq->write + 1) & RX_QUEUE_MASK;
3240 rxq->free_count--;
3241 }
3242 spin_unlock_irqrestore(&rxq->lock, flags);
3243 /* If the pre-allocated buffer pool is dropping low, schedule to
3244 * refill it */
3245 if (rxq->free_count <= RX_LOW_WATERMARK)
3246 queue_work(priv->workqueue, &priv->rx_replenish);
3247
3248
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003249 /* If we've added more space for the firmware to place data, tell it.
3250 * Increment device's write pointer in multiples of 8. */
Zhu Yib481de92007-09-25 17:54:57 -07003251 if ((write != (rxq->write & ~0x7))
3252 || (abs(rxq->write - rxq->read) > 7)) {
3253 spin_lock_irqsave(&rxq->lock, flags);
3254 rxq->need_update = 1;
3255 spin_unlock_irqrestore(&rxq->lock, flags);
Winkler, Tomas141c43a2009-01-08 10:19:53 -08003256 rc = iwl_rx_queue_update_write_ptr(priv, rxq);
Zhu Yib481de92007-09-25 17:54:57 -07003257 if (rc)
3258 return rc;
3259 }
3260
3261 return 0;
3262}
3263
3264/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003265 * iwl3945_rx_replenish - Move all used packet from rx_used to rx_free
Zhu Yib481de92007-09-25 17:54:57 -07003266 *
3267 * When moving to rx_free an SKB is allocated for the slot.
3268 *
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003269 * Also restock the Rx queue via iwl3945_rx_queue_restock.
Ian Schram01ebd062007-10-25 17:15:22 +08003270 * This is called as a scheduled work item (except for during initialization)
Zhu Yib481de92007-09-25 17:54:57 -07003271 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003272static void iwl3945_rx_allocate(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003273{
Abhijeet Kolekarcc2f3622008-12-19 10:37:25 +08003274 struct iwl_rx_queue *rxq = &priv->rxq;
Zhu Yib481de92007-09-25 17:54:57 -07003275 struct list_head *element;
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08003276 struct iwl_rx_mem_buffer *rxb;
Zhu Yib481de92007-09-25 17:54:57 -07003277 unsigned long flags;
3278 spin_lock_irqsave(&rxq->lock, flags);
3279 while (!list_empty(&rxq->rx_used)) {
3280 element = rxq->rx_used.next;
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08003281 rxb = list_entry(element, struct iwl_rx_mem_buffer, list);
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003282
3283 /* Alloc a new receive buffer */
Zhu Yib481de92007-09-25 17:54:57 -07003284 rxb->skb =
Winkler, Tomas1e33dc62009-01-08 10:19:57 -08003285 alloc_skb(priv->hw_params.rx_buf_size,
3286 __GFP_NOWARN | GFP_ATOMIC);
Zhu Yib481de92007-09-25 17:54:57 -07003287 if (!rxb->skb) {
3288 if (net_ratelimit())
Tomas Winkler978785a2008-12-19 10:37:31 +08003289 IWL_CRIT(priv, ": Can not allocate SKB buffers\n");
Zhu Yib481de92007-09-25 17:54:57 -07003290 /* We don't reschedule replenish work here -- we will
3291 * call the restock method and if it still needs
3292 * more buffers it will schedule replenish */
3293 break;
3294 }
Zhu Yi12342c42007-12-20 11:27:32 +08003295
3296 /* If radiotap head is required, reserve some headroom here.
3297 * The physical head count is a variable rx_stats->phy_count.
3298 * We reserve 4 bytes here. Plus these extra bytes, the
3299 * headroom of the physical head should be enough for the
3300 * radiotap head that iwl3945 supported. See iwl3945_rt.
3301 */
3302 skb_reserve(rxb->skb, 4);
3303
Zhu Yib481de92007-09-25 17:54:57 -07003304 priv->alloc_rxb_skb++;
3305 list_del(element);
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003306
3307 /* Get physical address of RB/SKB */
Winkler, Tomas1e33dc62009-01-08 10:19:57 -08003308 rxb->real_dma_addr = pci_map_single(priv->pci_dev,
3309 rxb->skb->data,
3310 priv->hw_params.rx_buf_size,
3311 PCI_DMA_FROMDEVICE);
Zhu Yib481de92007-09-25 17:54:57 -07003312 list_add_tail(&rxb->list, &rxq->rx_free);
3313 rxq->free_count++;
3314 }
3315 spin_unlock_irqrestore(&rxq->lock, flags);
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08003316}
3317
3318/*
3319 * this should be called while priv->lock is locked
3320 */
Tomas Winkler4fd1f842007-12-05 20:59:58 +02003321static void __iwl3945_rx_replenish(void *data)
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08003322{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003323 struct iwl_priv *priv = data;
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08003324
3325 iwl3945_rx_allocate(priv);
3326 iwl3945_rx_queue_restock(priv);
3327}
3328
3329
3330void iwl3945_rx_replenish(void *data)
3331{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003332 struct iwl_priv *priv = data;
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08003333 unsigned long flags;
3334
3335 iwl3945_rx_allocate(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003336
3337 spin_lock_irqsave(&priv->lock, flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003338 iwl3945_rx_queue_restock(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003339 spin_unlock_irqrestore(&priv->lock, flags);
3340}
3341
Zhu Yib481de92007-09-25 17:54:57 -07003342/* Convert linear signal-to-noise ratio into dB */
3343static u8 ratio2dB[100] = {
3344/* 0 1 2 3 4 5 6 7 8 9 */
3345 0, 0, 6, 10, 12, 14, 16, 17, 18, 19, /* 00 - 09 */
3346 20, 21, 22, 22, 23, 23, 24, 25, 26, 26, /* 10 - 19 */
3347 26, 26, 26, 27, 27, 28, 28, 28, 29, 29, /* 20 - 29 */
3348 29, 30, 30, 30, 31, 31, 31, 31, 32, 32, /* 30 - 39 */
3349 32, 32, 32, 33, 33, 33, 33, 33, 34, 34, /* 40 - 49 */
3350 34, 34, 34, 34, 35, 35, 35, 35, 35, 35, /* 50 - 59 */
3351 36, 36, 36, 36, 36, 36, 36, 37, 37, 37, /* 60 - 69 */
3352 37, 37, 37, 37, 37, 38, 38, 38, 38, 38, /* 70 - 79 */
3353 38, 38, 38, 38, 38, 39, 39, 39, 39, 39, /* 80 - 89 */
3354 39, 39, 39, 39, 39, 40, 40, 40, 40, 40 /* 90 - 99 */
3355};
3356
3357/* Calculates a relative dB value from a ratio of linear
3358 * (i.e. not dB) signal levels.
3359 * Conversion assumes that levels are voltages (20*log), not powers (10*log). */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003360int iwl3945_calc_db_from_ratio(int sig_ratio)
Zhu Yib481de92007-09-25 17:54:57 -07003361{
Adrian Bunk221c80c2008-02-02 23:19:01 +02003362 /* 1000:1 or higher just report as 60 dB */
3363 if (sig_ratio >= 1000)
Zhu Yib481de92007-09-25 17:54:57 -07003364 return 60;
3365
Adrian Bunk221c80c2008-02-02 23:19:01 +02003366 /* 100:1 or higher, divide by 10 and use table,
Zhu Yib481de92007-09-25 17:54:57 -07003367 * add 20 dB to make up for divide by 10 */
Adrian Bunk221c80c2008-02-02 23:19:01 +02003368 if (sig_ratio >= 100)
Tomas Winkler3ac7f142008-07-21 02:40:14 +03003369 return 20 + (int)ratio2dB[sig_ratio/10];
Zhu Yib481de92007-09-25 17:54:57 -07003370
3371 /* We shouldn't see this */
3372 if (sig_ratio < 1)
3373 return 0;
3374
3375 /* Use table for ratios 1:1 - 99:1 */
3376 return (int)ratio2dB[sig_ratio];
3377}
3378
3379#define PERFECT_RSSI (-20) /* dBm */
3380#define WORST_RSSI (-95) /* dBm */
3381#define RSSI_RANGE (PERFECT_RSSI - WORST_RSSI)
3382
3383/* Calculate an indication of rx signal quality (a percentage, not dBm!).
3384 * See http://www.ces.clemson.edu/linux/signal_quality.shtml for info
3385 * about formulas used below. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003386int iwl3945_calc_sig_qual(int rssi_dbm, int noise_dbm)
Zhu Yib481de92007-09-25 17:54:57 -07003387{
3388 int sig_qual;
3389 int degradation = PERFECT_RSSI - rssi_dbm;
3390
3391 /* If we get a noise measurement, use signal-to-noise ratio (SNR)
3392 * as indicator; formula is (signal dbm - noise dbm).
3393 * SNR at or above 40 is a great signal (100%).
3394 * Below that, scale to fit SNR of 0 - 40 dB within 0 - 100% indicator.
3395 * Weakest usable signal is usually 10 - 15 dB SNR. */
3396 if (noise_dbm) {
3397 if (rssi_dbm - noise_dbm >= 40)
3398 return 100;
3399 else if (rssi_dbm < noise_dbm)
3400 return 0;
3401 sig_qual = ((rssi_dbm - noise_dbm) * 5) / 2;
3402
3403 /* Else use just the signal level.
3404 * This formula is a least squares fit of data points collected and
3405 * compared with a reference system that had a percentage (%) display
3406 * for signal quality. */
3407 } else
3408 sig_qual = (100 * (RSSI_RANGE * RSSI_RANGE) - degradation *
3409 (15 * RSSI_RANGE + 62 * degradation)) /
3410 (RSSI_RANGE * RSSI_RANGE);
3411
3412 if (sig_qual > 100)
3413 sig_qual = 100;
3414 else if (sig_qual < 1)
3415 sig_qual = 0;
3416
3417 return sig_qual;
3418}
3419
3420/**
Ben Cahill9fbab512007-11-29 11:09:47 +08003421 * iwl3945_rx_handle - Main entry function for receiving responses from uCode
Zhu Yib481de92007-09-25 17:54:57 -07003422 *
3423 * Uses the priv->rx_handlers callback function array to invoke
3424 * the appropriate handlers, including command responses,
3425 * frame-received notifications, and other notifications.
3426 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003427static void iwl3945_rx_handle(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003428{
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08003429 struct iwl_rx_mem_buffer *rxb;
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08003430 struct iwl_rx_packet *pkt;
Abhijeet Kolekarcc2f3622008-12-19 10:37:25 +08003431 struct iwl_rx_queue *rxq = &priv->rxq;
Zhu Yib481de92007-09-25 17:54:57 -07003432 u32 r, i;
3433 int reclaim;
3434 unsigned long flags;
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08003435 u8 fill_rx = 0;
Mohamed Abbasd68ab682008-02-07 13:16:33 -08003436 u32 count = 8;
Zhu Yib481de92007-09-25 17:54:57 -07003437
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003438 /* uCode's read index (stored in shared DRAM) indicates the last Rx
3439 * buffer that the driver may process (last buffer filled by ucode). */
Winkler, Tomas8cd812b2008-12-19 10:37:43 +08003440 r = le16_to_cpu(rxq->rb_stts->closed_rb_num) & 0x0FFF;
Zhu Yib481de92007-09-25 17:54:57 -07003441 i = rxq->read;
3442
Winkler, Tomas37d68312009-01-08 10:19:54 -08003443 if (iwl_rx_queue_space(rxq) > (RX_QUEUE_SIZE / 2))
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08003444 fill_rx = 1;
Zhu Yib481de92007-09-25 17:54:57 -07003445 /* Rx interrupt, but nothing sent from uCode */
3446 if (i == r)
3447 IWL_DEBUG(IWL_DL_RX | IWL_DL_ISR, "r = %d, i = %d\n", r, i);
3448
3449 while (i != r) {
3450 rxb = rxq->queue[i];
3451
Ben Cahill9fbab512007-11-29 11:09:47 +08003452 /* If an RXB doesn't have a Rx queue slot associated with it,
Zhu Yib481de92007-09-25 17:54:57 -07003453 * then a bug has been introduced in the queue refilling
3454 * routines -- catch it here */
3455 BUG_ON(rxb == NULL);
3456
3457 rxq->queue[i] = NULL;
3458
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08003459 pci_dma_sync_single_for_cpu(priv->pci_dev, rxb->real_dma_addr,
Winkler, Tomas1e33dc62009-01-08 10:19:57 -08003460 priv->hw_params.rx_buf_size,
Zhu Yib481de92007-09-25 17:54:57 -07003461 PCI_DMA_FROMDEVICE);
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08003462 pkt = (struct iwl_rx_packet *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07003463
3464 /* Reclaim a command buffer only if this packet is a response
3465 * to a (driver-originated) command.
3466 * If the packet (e.g. Rx frame) originated from uCode,
3467 * there is no command buffer to reclaim.
3468 * Ucode should set SEQ_RX_FRAME bit if ucode-originated,
3469 * but apparently a few don't get set; catch them here. */
3470 reclaim = !(pkt->hdr.sequence & SEQ_RX_FRAME) &&
3471 (pkt->hdr.cmd != STATISTICS_NOTIFICATION) &&
3472 (pkt->hdr.cmd != REPLY_TX);
3473
3474 /* Based on type of command response or notification,
3475 * handle those that need handling via function in
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003476 * rx_handlers table. See iwl3945_setup_rx_handlers() */
Zhu Yib481de92007-09-25 17:54:57 -07003477 if (priv->rx_handlers[pkt->hdr.cmd]) {
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08003478 IWL_DEBUG(IWL_DL_HCMD | IWL_DL_RX | IWL_DL_ISR,
Zhu Yib481de92007-09-25 17:54:57 -07003479 "r = %d, i = %d, %s, 0x%02x\n", r, i,
3480 get_cmd_string(pkt->hdr.cmd), pkt->hdr.cmd);
3481 priv->rx_handlers[pkt->hdr.cmd] (priv, rxb);
3482 } else {
3483 /* No handling needed */
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08003484 IWL_DEBUG(IWL_DL_HCMD | IWL_DL_RX | IWL_DL_ISR,
Zhu Yib481de92007-09-25 17:54:57 -07003485 "r %d i %d No handler needed for %s, 0x%02x\n",
3486 r, i, get_cmd_string(pkt->hdr.cmd),
3487 pkt->hdr.cmd);
3488 }
3489
3490 if (reclaim) {
Ben Cahill9fbab512007-11-29 11:09:47 +08003491 /* Invoke any callbacks, transfer the skb to caller, and
3492 * fire off the (possibly) blocking iwl3945_send_cmd()
Zhu Yib481de92007-09-25 17:54:57 -07003493 * as we reclaim the driver command queue */
3494 if (rxb && rxb->skb)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003495 iwl3945_tx_cmd_complete(priv, rxb);
Zhu Yib481de92007-09-25 17:54:57 -07003496 else
Winkler, Tomas39aadf82008-12-19 10:37:32 +08003497 IWL_WARN(priv, "Claim null rxb?\n");
Zhu Yib481de92007-09-25 17:54:57 -07003498 }
3499
3500 /* For now we just don't re-use anything. We can tweak this
3501 * later to try and re-use notification packets and SKBs that
3502 * fail to Rx correctly */
3503 if (rxb->skb != NULL) {
3504 priv->alloc_rxb_skb--;
3505 dev_kfree_skb_any(rxb->skb);
3506 rxb->skb = NULL;
3507 }
3508
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08003509 pci_unmap_single(priv->pci_dev, rxb->real_dma_addr,
Winkler, Tomas1e33dc62009-01-08 10:19:57 -08003510 priv->hw_params.rx_buf_size,
3511 PCI_DMA_FROMDEVICE);
Zhu Yib481de92007-09-25 17:54:57 -07003512 spin_lock_irqsave(&rxq->lock, flags);
3513 list_add_tail(&rxb->list, &priv->rxq.rx_used);
3514 spin_unlock_irqrestore(&rxq->lock, flags);
3515 i = (i + 1) & RX_QUEUE_MASK;
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08003516 /* If there are a lot of unused frames,
3517 * restock the Rx queue so ucode won't assert. */
3518 if (fill_rx) {
3519 count++;
3520 if (count >= 8) {
3521 priv->rxq.read = i;
3522 __iwl3945_rx_replenish(priv);
3523 count = 0;
3524 }
3525 }
Zhu Yib481de92007-09-25 17:54:57 -07003526 }
3527
3528 /* Backtrack one entry */
3529 priv->rxq.read = i;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003530 iwl3945_rx_queue_restock(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003531}
3532
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003533/**
3534 * iwl3945_tx_queue_update_write_ptr - Send new write index to hardware
3535 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003536static int iwl3945_tx_queue_update_write_ptr(struct iwl_priv *priv,
Samuel Ortiz188cf6c2008-12-22 11:31:16 +08003537 struct iwl_tx_queue *txq)
Zhu Yib481de92007-09-25 17:54:57 -07003538{
3539 u32 reg = 0;
3540 int rc = 0;
3541 int txq_id = txq->q.id;
3542
3543 if (txq->need_update == 0)
3544 return rc;
3545
3546 /* if we're trying to save power */
3547 if (test_bit(STATUS_POWER_PMI, &priv->status)) {
3548 /* wake up nic if it's powered down ...
3549 * uCode will wake up, and interrupt us again, so next
3550 * time we'll skip this part. */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003551 reg = iwl_read32(priv, CSR_UCODE_DRV_GP1);
Zhu Yib481de92007-09-25 17:54:57 -07003552
3553 if (reg & CSR_UCODE_DRV_GP1_BIT_MAC_SLEEP) {
3554 IWL_DEBUG_INFO("Requesting wakeup, GP1 = 0x%x\n", reg);
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003555 iwl_set_bit(priv, CSR_GP_CNTRL,
Zhu Yib481de92007-09-25 17:54:57 -07003556 CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
3557 return rc;
3558 }
3559
3560 /* restore this queue's parameters in nic hardware. */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003561 rc = iwl_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003562 if (rc)
3563 return rc;
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003564 iwl_write_direct32(priv, HBUS_TARG_WRPTR,
Tomas Winklerfc4b6852007-10-25 17:15:24 +08003565 txq->q.write_ptr | (txq_id << 8));
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003566 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003567
3568 /* else not in power-save mode, uCode will never sleep when we're
3569 * trying to tx (during RFKILL, we're not trying to tx). */
3570 } else
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003571 iwl_write32(priv, HBUS_TARG_WRPTR,
Tomas Winklerfc4b6852007-10-25 17:15:24 +08003572 txq->q.write_ptr | (txq_id << 8));
Zhu Yib481de92007-09-25 17:54:57 -07003573
3574 txq->need_update = 0;
3575
3576 return rc;
3577}
3578
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08003579#ifdef CONFIG_IWL3945_DEBUG
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003580static void iwl3945_print_rx_config_cmd(struct iwl_priv *priv,
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08003581 struct iwl3945_rxon_cmd *rxon)
Zhu Yib481de92007-09-25 17:54:57 -07003582{
3583 IWL_DEBUG_RADIO("RX CONFIG:\n");
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08003584 iwl_print_hex_dump(priv, IWL_DL_RADIO, (u8 *) rxon, sizeof(*rxon));
Zhu Yib481de92007-09-25 17:54:57 -07003585 IWL_DEBUG_RADIO("u16 channel: 0x%x\n", le16_to_cpu(rxon->channel));
3586 IWL_DEBUG_RADIO("u32 flags: 0x%08X\n", le32_to_cpu(rxon->flags));
3587 IWL_DEBUG_RADIO("u32 filter_flags: 0x%08x\n",
3588 le32_to_cpu(rxon->filter_flags));
3589 IWL_DEBUG_RADIO("u8 dev_type: 0x%x\n", rxon->dev_type);
3590 IWL_DEBUG_RADIO("u8 ofdm_basic_rates: 0x%02x\n",
3591 rxon->ofdm_basic_rates);
3592 IWL_DEBUG_RADIO("u8 cck_basic_rates: 0x%02x\n", rxon->cck_basic_rates);
Johannes Berge1749612008-10-27 15:59:26 -07003593 IWL_DEBUG_RADIO("u8[6] node_addr: %pM\n", rxon->node_addr);
3594 IWL_DEBUG_RADIO("u8[6] bssid_addr: %pM\n", rxon->bssid_addr);
Zhu Yib481de92007-09-25 17:54:57 -07003595 IWL_DEBUG_RADIO("u16 assoc_id: 0x%x\n", le16_to_cpu(rxon->assoc_id));
3596}
3597#endif
3598
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003599static void iwl3945_enable_interrupts(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003600{
3601 IWL_DEBUG_ISR("Enabling interrupts\n");
3602 set_bit(STATUS_INT_ENABLED, &priv->status);
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003603 iwl_write32(priv, CSR_INT_MASK, CSR_INI_SET_MASK);
Zhu Yib481de92007-09-25 17:54:57 -07003604}
3605
Mohamed Abbas0359fac2008-03-28 16:21:08 -07003606
3607/* call this function to flush any scheduled tasklet */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003608static inline void iwl_synchronize_irq(struct iwl_priv *priv)
Mohamed Abbas0359fac2008-03-28 16:21:08 -07003609{
Tomas Winklera96a27f2008-10-23 23:48:56 -07003610 /* wait to make sure we flush pending tasklet*/
Mohamed Abbas0359fac2008-03-28 16:21:08 -07003611 synchronize_irq(priv->pci_dev->irq);
3612 tasklet_kill(&priv->irq_tasklet);
3613}
3614
3615
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003616static inline void iwl3945_disable_interrupts(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003617{
3618 clear_bit(STATUS_INT_ENABLED, &priv->status);
3619
3620 /* disable interrupts from uCode/NIC to host */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003621 iwl_write32(priv, CSR_INT_MASK, 0x00000000);
Zhu Yib481de92007-09-25 17:54:57 -07003622
3623 /* acknowledge/clear/reset any interrupts still pending
3624 * from uCode or flow handler (Rx/Tx DMA) */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003625 iwl_write32(priv, CSR_INT, 0xffffffff);
3626 iwl_write32(priv, CSR_FH_INT_STATUS, 0xffffffff);
Zhu Yib481de92007-09-25 17:54:57 -07003627 IWL_DEBUG_ISR("Disabled interrupts\n");
3628}
3629
3630static const char *desc_lookup(int i)
3631{
3632 switch (i) {
3633 case 1:
3634 return "FAIL";
3635 case 2:
3636 return "BAD_PARAM";
3637 case 3:
3638 return "BAD_CHECKSUM";
3639 case 4:
3640 return "NMI_INTERRUPT";
3641 case 5:
3642 return "SYSASSERT";
3643 case 6:
3644 return "FATAL_ERROR";
3645 }
3646
3647 return "UNKNOWN";
3648}
3649
3650#define ERROR_START_OFFSET (1 * sizeof(u32))
3651#define ERROR_ELEM_SIZE (7 * sizeof(u32))
3652
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003653static void iwl3945_dump_nic_error_log(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003654{
3655 u32 i;
3656 u32 desc, time, count, base, data1;
3657 u32 blink1, blink2, ilink1, ilink2;
3658 int rc;
3659
3660 base = le32_to_cpu(priv->card_alive.error_event_table_ptr);
3661
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003662 if (!iwl3945_hw_valid_rtc_data_addr(base)) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08003663 IWL_ERR(priv, "Not valid error log pointer 0x%08X\n", base);
Zhu Yib481de92007-09-25 17:54:57 -07003664 return;
3665 }
3666
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003667 rc = iwl_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003668 if (rc) {
Winkler, Tomas39aadf82008-12-19 10:37:32 +08003669 IWL_WARN(priv, "Can not read from adapter at this time.\n");
Zhu Yib481de92007-09-25 17:54:57 -07003670 return;
3671 }
3672
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003673 count = iwl_read_targ_mem(priv, base);
Zhu Yib481de92007-09-25 17:54:57 -07003674
3675 if (ERROR_START_OFFSET <= count * ERROR_ELEM_SIZE) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08003676 IWL_ERR(priv, "Start IWL Error Log Dump:\n");
3677 IWL_ERR(priv, "Status: 0x%08lX, count: %d\n",
3678 priv->status, count);
Zhu Yib481de92007-09-25 17:54:57 -07003679 }
3680
Winkler, Tomas15b16872008-12-19 10:37:33 +08003681 IWL_ERR(priv, "Desc Time asrtPC blink2 "
Zhu Yib481de92007-09-25 17:54:57 -07003682 "ilink1 nmiPC Line\n");
3683 for (i = ERROR_START_OFFSET;
3684 i < (count * ERROR_ELEM_SIZE) + ERROR_START_OFFSET;
3685 i += ERROR_ELEM_SIZE) {
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003686 desc = iwl_read_targ_mem(priv, base + i);
Zhu Yib481de92007-09-25 17:54:57 -07003687 time =
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003688 iwl_read_targ_mem(priv, base + i + 1 * sizeof(u32));
Zhu Yib481de92007-09-25 17:54:57 -07003689 blink1 =
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003690 iwl_read_targ_mem(priv, base + i + 2 * sizeof(u32));
Zhu Yib481de92007-09-25 17:54:57 -07003691 blink2 =
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003692 iwl_read_targ_mem(priv, base + i + 3 * sizeof(u32));
Zhu Yib481de92007-09-25 17:54:57 -07003693 ilink1 =
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003694 iwl_read_targ_mem(priv, base + i + 4 * sizeof(u32));
Zhu Yib481de92007-09-25 17:54:57 -07003695 ilink2 =
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003696 iwl_read_targ_mem(priv, base + i + 5 * sizeof(u32));
Zhu Yib481de92007-09-25 17:54:57 -07003697 data1 =
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003698 iwl_read_targ_mem(priv, base + i + 6 * sizeof(u32));
Zhu Yib481de92007-09-25 17:54:57 -07003699
Winkler, Tomas15b16872008-12-19 10:37:33 +08003700 IWL_ERR(priv,
3701 "%-13s (#%d) %010u 0x%05X 0x%05X 0x%05X 0x%05X %u\n\n",
3702 desc_lookup(desc), desc, time, blink1, blink2,
3703 ilink1, ilink2, data1);
Zhu Yib481de92007-09-25 17:54:57 -07003704 }
3705
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003706 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003707
3708}
3709
Ben Cahillf58177b2007-11-29 11:09:43 +08003710#define EVENT_START_OFFSET (6 * sizeof(u32))
Zhu Yib481de92007-09-25 17:54:57 -07003711
3712/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003713 * iwl3945_print_event_log - Dump error event log to syslog
Zhu Yib481de92007-09-25 17:54:57 -07003714 *
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003715 * NOTE: Must be called with iwl_grab_nic_access() already obtained!
Zhu Yib481de92007-09-25 17:54:57 -07003716 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003717static void iwl3945_print_event_log(struct iwl_priv *priv, u32 start_idx,
Zhu Yib481de92007-09-25 17:54:57 -07003718 u32 num_events, u32 mode)
3719{
3720 u32 i;
3721 u32 base; /* SRAM byte address of event log header */
3722 u32 event_size; /* 2 u32s, or 3 u32s if timestamp recorded */
3723 u32 ptr; /* SRAM byte address of log data */
3724 u32 ev, time, data; /* event log data */
3725
3726 if (num_events == 0)
3727 return;
3728
3729 base = le32_to_cpu(priv->card_alive.log_event_table_ptr);
3730
3731 if (mode == 0)
3732 event_size = 2 * sizeof(u32);
3733 else
3734 event_size = 3 * sizeof(u32);
3735
3736 ptr = base + EVENT_START_OFFSET + (start_idx * event_size);
3737
3738 /* "time" is actually "data" for mode 0 (no timestamp).
3739 * place event id # at far right for easier visual parsing. */
3740 for (i = 0; i < num_events; i++) {
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003741 ev = iwl_read_targ_mem(priv, ptr);
Zhu Yib481de92007-09-25 17:54:57 -07003742 ptr += sizeof(u32);
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003743 time = iwl_read_targ_mem(priv, ptr);
Zhu Yib481de92007-09-25 17:54:57 -07003744 ptr += sizeof(u32);
Winkler, Tomas15b16872008-12-19 10:37:33 +08003745 if (mode == 0) {
3746 /* data, ev */
3747 IWL_ERR(priv, "0x%08x\t%04u\n", time, ev);
3748 } else {
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003749 data = iwl_read_targ_mem(priv, ptr);
Zhu Yib481de92007-09-25 17:54:57 -07003750 ptr += sizeof(u32);
Winkler, Tomas15b16872008-12-19 10:37:33 +08003751 IWL_ERR(priv, "%010u\t0x%08x\t%04u\n", time, data, ev);
Zhu Yib481de92007-09-25 17:54:57 -07003752 }
3753 }
3754}
3755
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003756static void iwl3945_dump_nic_event_log(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003757{
3758 int rc;
3759 u32 base; /* SRAM byte address of event log header */
3760 u32 capacity; /* event log capacity in # entries */
3761 u32 mode; /* 0 - no timestamp, 1 - timestamp recorded */
3762 u32 num_wraps; /* # times uCode wrapped to top of log */
3763 u32 next_entry; /* index of next entry to be written by uCode */
3764 u32 size; /* # entries that we'll print */
3765
3766 base = le32_to_cpu(priv->card_alive.log_event_table_ptr);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003767 if (!iwl3945_hw_valid_rtc_data_addr(base)) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08003768 IWL_ERR(priv, "Invalid event log pointer 0x%08X\n", base);
Zhu Yib481de92007-09-25 17:54:57 -07003769 return;
3770 }
3771
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003772 rc = iwl_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003773 if (rc) {
Winkler, Tomas39aadf82008-12-19 10:37:32 +08003774 IWL_WARN(priv, "Can not read from adapter at this time.\n");
Zhu Yib481de92007-09-25 17:54:57 -07003775 return;
3776 }
3777
3778 /* event log header */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003779 capacity = iwl_read_targ_mem(priv, base);
3780 mode = iwl_read_targ_mem(priv, base + (1 * sizeof(u32)));
3781 num_wraps = iwl_read_targ_mem(priv, base + (2 * sizeof(u32)));
3782 next_entry = iwl_read_targ_mem(priv, base + (3 * sizeof(u32)));
Zhu Yib481de92007-09-25 17:54:57 -07003783
3784 size = num_wraps ? capacity : next_entry;
3785
3786 /* bail out if nothing in log */
3787 if (size == 0) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08003788 IWL_ERR(priv, "Start IWL Event Log Dump: nothing in log\n");
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003789 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003790 return;
3791 }
3792
Winkler, Tomas15b16872008-12-19 10:37:33 +08003793 IWL_ERR(priv, "Start IWL Event Log Dump: display count %d, wraps %d\n",
Zhu Yib481de92007-09-25 17:54:57 -07003794 size, num_wraps);
3795
3796 /* if uCode has wrapped back to top of log, start at the oldest entry,
3797 * i.e the next one that uCode would fill. */
3798 if (num_wraps)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003799 iwl3945_print_event_log(priv, next_entry,
Zhu Yib481de92007-09-25 17:54:57 -07003800 capacity - next_entry, mode);
3801
3802 /* (then/else) start at top of log */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003803 iwl3945_print_event_log(priv, 0, next_entry, mode);
Zhu Yib481de92007-09-25 17:54:57 -07003804
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003805 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003806}
3807
3808/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003809 * iwl3945_irq_handle_error - called for HW or SW error interrupt from card
Zhu Yib481de92007-09-25 17:54:57 -07003810 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003811static void iwl3945_irq_handle_error(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003812{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003813 /* Set the FW error flag -- cleared on iwl3945_down */
Zhu Yib481de92007-09-25 17:54:57 -07003814 set_bit(STATUS_FW_ERROR, &priv->status);
3815
3816 /* Cancel currently queued command. */
3817 clear_bit(STATUS_HCMD_ACTIVE, &priv->status);
3818
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08003819#ifdef CONFIG_IWL3945_DEBUG
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08003820 if (priv->debug_level & IWL_DL_FW_ERRORS) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003821 iwl3945_dump_nic_error_log(priv);
3822 iwl3945_dump_nic_event_log(priv);
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08003823 iwl3945_print_rx_config_cmd(priv, &priv->staging39_rxon);
Zhu Yib481de92007-09-25 17:54:57 -07003824 }
3825#endif
3826
3827 wake_up_interruptible(&priv->wait_command_queue);
3828
3829 /* Keep the restart process from trying to send host
3830 * commands by clearing the INIT status bit */
3831 clear_bit(STATUS_READY, &priv->status);
3832
3833 if (!test_bit(STATUS_EXIT_PENDING, &priv->status)) {
3834 IWL_DEBUG(IWL_DL_INFO | IWL_DL_FW_ERRORS,
3835 "Restarting adapter due to uCode error.\n");
3836
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003837 if (iwl3945_is_associated(priv)) {
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08003838 memcpy(&priv->recovery39_rxon, &priv->active39_rxon,
3839 sizeof(priv->recovery39_rxon));
Zhu Yib481de92007-09-25 17:54:57 -07003840 priv->error_recovering = 1;
3841 }
3842 queue_work(priv->workqueue, &priv->restart);
3843 }
3844}
3845
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003846static void iwl3945_error_recovery(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003847{
3848 unsigned long flags;
3849
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08003850 memcpy(&priv->staging39_rxon, &priv->recovery39_rxon,
3851 sizeof(priv->staging39_rxon));
3852 priv->staging39_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003853 iwl3945_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003854
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003855 iwl3945_add_station(priv, priv->bssid, 1, 0);
Zhu Yib481de92007-09-25 17:54:57 -07003856
3857 spin_lock_irqsave(&priv->lock, flags);
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08003858 priv->assoc_id = le16_to_cpu(priv->staging39_rxon.assoc_id);
Zhu Yib481de92007-09-25 17:54:57 -07003859 priv->error_recovering = 0;
3860 spin_unlock_irqrestore(&priv->lock, flags);
3861}
3862
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003863static void iwl3945_irq_tasklet(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003864{
3865 u32 inta, handled = 0;
3866 u32 inta_fh;
3867 unsigned long flags;
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08003868#ifdef CONFIG_IWL3945_DEBUG
Zhu Yib481de92007-09-25 17:54:57 -07003869 u32 inta_mask;
3870#endif
3871
3872 spin_lock_irqsave(&priv->lock, flags);
3873
3874 /* Ack/clear/reset pending uCode interrupts.
3875 * Note: Some bits in CSR_INT are "OR" of bits in CSR_FH_INT_STATUS,
3876 * and will clear only when CSR_FH_INT_STATUS gets cleared. */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003877 inta = iwl_read32(priv, CSR_INT);
3878 iwl_write32(priv, CSR_INT, inta);
Zhu Yib481de92007-09-25 17:54:57 -07003879
3880 /* Ack/clear/reset pending flow-handler (DMA) interrupts.
3881 * Any new interrupts that happen after this, either while we're
3882 * in this tasklet, or later, will show up in next ISR/tasklet. */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003883 inta_fh = iwl_read32(priv, CSR_FH_INT_STATUS);
3884 iwl_write32(priv, CSR_FH_INT_STATUS, inta_fh);
Zhu Yib481de92007-09-25 17:54:57 -07003885
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08003886#ifdef CONFIG_IWL3945_DEBUG
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08003887 if (priv->debug_level & IWL_DL_ISR) {
Ben Cahill9fbab512007-11-29 11:09:47 +08003888 /* just for debug */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003889 inta_mask = iwl_read32(priv, CSR_INT_MASK);
Zhu Yib481de92007-09-25 17:54:57 -07003890 IWL_DEBUG_ISR("inta 0x%08x, enabled 0x%08x, fh 0x%08x\n",
3891 inta, inta_mask, inta_fh);
3892 }
3893#endif
3894
3895 /* Since CSR_INT and CSR_FH_INT_STATUS reads and clears are not
3896 * atomic, make sure that inta covers all the interrupts that
3897 * we've discovered, even if FH interrupt came in just after
3898 * reading CSR_INT. */
Tomas Winkler6f83eaa2008-03-04 18:09:28 -08003899 if (inta_fh & CSR39_FH_INT_RX_MASK)
Zhu Yib481de92007-09-25 17:54:57 -07003900 inta |= CSR_INT_BIT_FH_RX;
Tomas Winkler6f83eaa2008-03-04 18:09:28 -08003901 if (inta_fh & CSR39_FH_INT_TX_MASK)
Zhu Yib481de92007-09-25 17:54:57 -07003902 inta |= CSR_INT_BIT_FH_TX;
3903
3904 /* Now service all interrupt bits discovered above. */
3905 if (inta & CSR_INT_BIT_HW_ERR) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08003906 IWL_ERR(priv, "Microcode HW error detected. Restarting.\n");
Zhu Yib481de92007-09-25 17:54:57 -07003907
3908 /* Tell the device to stop sending interrupts */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003909 iwl3945_disable_interrupts(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003910
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003911 iwl3945_irq_handle_error(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003912
3913 handled |= CSR_INT_BIT_HW_ERR;
3914
3915 spin_unlock_irqrestore(&priv->lock, flags);
3916
3917 return;
3918 }
3919
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08003920#ifdef CONFIG_IWL3945_DEBUG
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08003921 if (priv->debug_level & (IWL_DL_ISR)) {
Zhu Yib481de92007-09-25 17:54:57 -07003922 /* NIC fires this, but we don't use it, redundant with WAKEUP */
Joonwoo Park25c03d82008-01-23 10:15:20 -08003923 if (inta & CSR_INT_BIT_SCD)
3924 IWL_DEBUG_ISR("Scheduler finished to transmit "
3925 "the frame/frames.\n");
Zhu Yib481de92007-09-25 17:54:57 -07003926
3927 /* Alive notification via Rx interrupt will do the real work */
3928 if (inta & CSR_INT_BIT_ALIVE)
3929 IWL_DEBUG_ISR("Alive interrupt\n");
3930 }
3931#endif
3932 /* Safely ignore these bits for debug checks below */
Joonwoo Park25c03d82008-01-23 10:15:20 -08003933 inta &= ~(CSR_INT_BIT_SCD | CSR_INT_BIT_ALIVE);
Zhu Yib481de92007-09-25 17:54:57 -07003934
Zhu Yib481de92007-09-25 17:54:57 -07003935 /* Error detected by uCode */
3936 if (inta & CSR_INT_BIT_SW_ERR) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08003937 IWL_ERR(priv, "Microcode SW error detected. "
3938 "Restarting 0x%X.\n", inta);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003939 iwl3945_irq_handle_error(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003940 handled |= CSR_INT_BIT_SW_ERR;
3941 }
3942
3943 /* uCode wakes up after power-down sleep */
3944 if (inta & CSR_INT_BIT_WAKEUP) {
3945 IWL_DEBUG_ISR("Wakeup interrupt\n");
Winkler, Tomas141c43a2009-01-08 10:19:53 -08003946 iwl_rx_queue_update_write_ptr(priv, &priv->rxq);
Samuel Ortiz188cf6c2008-12-22 11:31:16 +08003947 iwl3945_tx_queue_update_write_ptr(priv, &priv->txq[0]);
3948 iwl3945_tx_queue_update_write_ptr(priv, &priv->txq[1]);
3949 iwl3945_tx_queue_update_write_ptr(priv, &priv->txq[2]);
3950 iwl3945_tx_queue_update_write_ptr(priv, &priv->txq[3]);
3951 iwl3945_tx_queue_update_write_ptr(priv, &priv->txq[4]);
3952 iwl3945_tx_queue_update_write_ptr(priv, &priv->txq[5]);
Zhu Yib481de92007-09-25 17:54:57 -07003953
3954 handled |= CSR_INT_BIT_WAKEUP;
3955 }
3956
3957 /* All uCode command responses, including Tx command responses,
3958 * Rx "responses" (frame-received notification), and other
3959 * notifications from uCode come through here*/
3960 if (inta & (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX)) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003961 iwl3945_rx_handle(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003962 handled |= (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX);
3963 }
3964
3965 if (inta & CSR_INT_BIT_FH_TX) {
3966 IWL_DEBUG_ISR("Tx interrupt\n");
3967
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003968 iwl_write32(priv, CSR_FH_INT_STATUS, (1 << 6));
3969 if (!iwl_grab_nic_access(priv)) {
3970 iwl_write_direct32(priv, FH39_TCSR_CREDIT
Tomas Winklerbddadf82008-12-19 10:37:01 +08003971 (FH39_SRVC_CHNL), 0x0);
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003972 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003973 }
3974 handled |= CSR_INT_BIT_FH_TX;
3975 }
3976
3977 if (inta & ~handled)
Winkler, Tomas15b16872008-12-19 10:37:33 +08003978 IWL_ERR(priv, "Unhandled INTA bits 0x%08x\n", inta & ~handled);
Zhu Yib481de92007-09-25 17:54:57 -07003979
3980 if (inta & ~CSR_INI_SET_MASK) {
Winkler, Tomas39aadf82008-12-19 10:37:32 +08003981 IWL_WARN(priv, "Disabled INTA bits 0x%08x were pending\n",
Zhu Yib481de92007-09-25 17:54:57 -07003982 inta & ~CSR_INI_SET_MASK);
Winkler, Tomas39aadf82008-12-19 10:37:32 +08003983 IWL_WARN(priv, " with FH_INT = 0x%08x\n", inta_fh);
Zhu Yib481de92007-09-25 17:54:57 -07003984 }
3985
3986 /* Re-enable all interrupts */
Mohamed Abbas0359fac2008-03-28 16:21:08 -07003987 /* only Re-enable if disabled by irq */
3988 if (test_bit(STATUS_INT_ENABLED, &priv->status))
3989 iwl3945_enable_interrupts(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003990
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08003991#ifdef CONFIG_IWL3945_DEBUG
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08003992 if (priv->debug_level & (IWL_DL_ISR)) {
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003993 inta = iwl_read32(priv, CSR_INT);
3994 inta_mask = iwl_read32(priv, CSR_INT_MASK);
3995 inta_fh = iwl_read32(priv, CSR_FH_INT_STATUS);
Zhu Yib481de92007-09-25 17:54:57 -07003996 IWL_DEBUG_ISR("End inta 0x%08x, enabled 0x%08x, fh 0x%08x, "
3997 "flags 0x%08lx\n", inta, inta_mask, inta_fh, flags);
3998 }
3999#endif
4000 spin_unlock_irqrestore(&priv->lock, flags);
4001}
4002
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004003static irqreturn_t iwl3945_isr(int irq, void *data)
Zhu Yib481de92007-09-25 17:54:57 -07004004{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08004005 struct iwl_priv *priv = data;
Zhu Yib481de92007-09-25 17:54:57 -07004006 u32 inta, inta_mask;
4007 u32 inta_fh;
4008 if (!priv)
4009 return IRQ_NONE;
4010
4011 spin_lock(&priv->lock);
4012
4013 /* Disable (but don't clear!) interrupts here to avoid
4014 * back-to-back ISRs and sporadic interrupts from our NIC.
4015 * If we have something to service, the tasklet will re-enable ints.
4016 * If we *don't* have something, we'll re-enable before leaving here. */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08004017 inta_mask = iwl_read32(priv, CSR_INT_MASK); /* just for debug */
4018 iwl_write32(priv, CSR_INT_MASK, 0x00000000);
Zhu Yib481de92007-09-25 17:54:57 -07004019
4020 /* Discover which interrupts are active/pending */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08004021 inta = iwl_read32(priv, CSR_INT);
4022 inta_fh = iwl_read32(priv, CSR_FH_INT_STATUS);
Zhu Yib481de92007-09-25 17:54:57 -07004023
4024 /* Ignore interrupt if there's nothing in NIC to service.
4025 * This may be due to IRQ shared with another device,
4026 * or due to sporadic interrupts thrown from our NIC. */
4027 if (!inta && !inta_fh) {
4028 IWL_DEBUG_ISR("Ignore interrupt, inta == 0, inta_fh == 0\n");
4029 goto none;
4030 }
4031
4032 if ((inta == 0xFFFFFFFF) || ((inta & 0xFFFFFFF0) == 0xa5a5a5a0)) {
4033 /* Hardware disappeared */
Winkler, Tomas39aadf82008-12-19 10:37:32 +08004034 IWL_WARN(priv, "HARDWARE GONE?? INTA == 0x%08x\n", inta);
Oliver Neukumcb4da1a2007-11-13 21:10:32 -08004035 goto unplugged;
Zhu Yib481de92007-09-25 17:54:57 -07004036 }
4037
4038 IWL_DEBUG_ISR("ISR inta 0x%08x, enabled 0x%08x, fh 0x%08x\n",
4039 inta, inta_mask, inta_fh);
4040
Joonwoo Park25c03d82008-01-23 10:15:20 -08004041 inta &= ~CSR_INT_BIT_SCD;
4042
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004043 /* iwl3945_irq_tasklet() will service interrupts and re-enable them */
Joonwoo Park25c03d82008-01-23 10:15:20 -08004044 if (likely(inta || inta_fh))
4045 tasklet_schedule(&priv->irq_tasklet);
Oliver Neukumcb4da1a2007-11-13 21:10:32 -08004046unplugged:
Zhu Yib481de92007-09-25 17:54:57 -07004047 spin_unlock(&priv->lock);
4048
4049 return IRQ_HANDLED;
4050
4051 none:
4052 /* re-enable interrupts here since we don't have anything to service. */
Mohamed Abbas0359fac2008-03-28 16:21:08 -07004053 /* only Re-enable if disabled by irq */
4054 if (test_bit(STATUS_INT_ENABLED, &priv->status))
4055 iwl3945_enable_interrupts(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004056 spin_unlock(&priv->lock);
4057 return IRQ_NONE;
4058}
4059
4060/************************** EEPROM BANDS ****************************
4061 *
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004062 * The iwl3945_eeprom_band definitions below provide the mapping from the
Zhu Yib481de92007-09-25 17:54:57 -07004063 * EEPROM contents to the specific channel number supported for each
4064 * band.
4065 *
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08004066 * For example, iwl3945_priv->eeprom39.band_3_channels[4] from the band_3
Zhu Yib481de92007-09-25 17:54:57 -07004067 * definition below maps to physical channel 42 in the 5.2GHz spectrum.
4068 * The specific geography and calibration information for that channel
4069 * is contained in the eeprom map itself.
4070 *
4071 * During init, we copy the eeprom information and channel map
4072 * information into priv->channel_info_24/52 and priv->channel_map_24/52
4073 *
4074 * channel_map_24/52 provides the index in the channel_info array for a
4075 * given channel. We have to have two separate maps as there is channel
4076 * overlap with the 2.4GHz and 5.2GHz spectrum as seen in band_1 and
4077 * band_2
4078 *
4079 * A value of 0xff stored in the channel_map indicates that the channel
4080 * is not supported by the hardware at all.
4081 *
4082 * A value of 0xfe in the channel_map indicates that the channel is not
4083 * valid for Tx with the current hardware. This means that
4084 * while the system can tune and receive on a given channel, it may not
4085 * be able to associate or transmit any frames on that
4086 * channel. There is no corresponding channel information for that
4087 * entry.
4088 *
4089 *********************************************************************/
4090
4091/* 2.4 GHz */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004092static const u8 iwl3945_eeprom_band_1[14] = {
Zhu Yib481de92007-09-25 17:54:57 -07004093 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14
4094};
4095
4096/* 5.2 GHz bands */
Ben Cahill9fbab512007-11-29 11:09:47 +08004097static const u8 iwl3945_eeprom_band_2[] = { /* 4915-5080MHz */
Zhu Yib481de92007-09-25 17:54:57 -07004098 183, 184, 185, 187, 188, 189, 192, 196, 7, 8, 11, 12, 16
4099};
4100
Ben Cahill9fbab512007-11-29 11:09:47 +08004101static const u8 iwl3945_eeprom_band_3[] = { /* 5170-5320MHz */
Zhu Yib481de92007-09-25 17:54:57 -07004102 34, 36, 38, 40, 42, 44, 46, 48, 52, 56, 60, 64
4103};
4104
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004105static const u8 iwl3945_eeprom_band_4[] = { /* 5500-5700MHz */
Zhu Yib481de92007-09-25 17:54:57 -07004106 100, 104, 108, 112, 116, 120, 124, 128, 132, 136, 140
4107};
4108
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004109static const u8 iwl3945_eeprom_band_5[] = { /* 5725-5825MHz */
Zhu Yib481de92007-09-25 17:54:57 -07004110 145, 149, 153, 157, 161, 165
4111};
4112
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08004113static void iwl3945_init_band_reference(const struct iwl_priv *priv, int band,
Zhu Yib481de92007-09-25 17:54:57 -07004114 int *eeprom_ch_count,
Samuel Ortiz0f741d92008-12-19 10:37:10 +08004115 const struct iwl_eeprom_channel
Zhu Yib481de92007-09-25 17:54:57 -07004116 **eeprom_ch_info,
4117 const u8 **eeprom_ch_index)
4118{
4119 switch (band) {
4120 case 1: /* 2.4GHz band */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004121 *eeprom_ch_count = ARRAY_SIZE(iwl3945_eeprom_band_1);
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08004122 *eeprom_ch_info = priv->eeprom39.band_1_channels;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004123 *eeprom_ch_index = iwl3945_eeprom_band_1;
Zhu Yib481de92007-09-25 17:54:57 -07004124 break;
Ben Cahill9fbab512007-11-29 11:09:47 +08004125 case 2: /* 4.9GHz band */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004126 *eeprom_ch_count = ARRAY_SIZE(iwl3945_eeprom_band_2);
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08004127 *eeprom_ch_info = priv->eeprom39.band_2_channels;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004128 *eeprom_ch_index = iwl3945_eeprom_band_2;
Zhu Yib481de92007-09-25 17:54:57 -07004129 break;
4130 case 3: /* 5.2GHz band */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004131 *eeprom_ch_count = ARRAY_SIZE(iwl3945_eeprom_band_3);
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08004132 *eeprom_ch_info = priv->eeprom39.band_3_channels;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004133 *eeprom_ch_index = iwl3945_eeprom_band_3;
Zhu Yib481de92007-09-25 17:54:57 -07004134 break;
Ben Cahill9fbab512007-11-29 11:09:47 +08004135 case 4: /* 5.5GHz band */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004136 *eeprom_ch_count = ARRAY_SIZE(iwl3945_eeprom_band_4);
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08004137 *eeprom_ch_info = priv->eeprom39.band_4_channels;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004138 *eeprom_ch_index = iwl3945_eeprom_band_4;
Zhu Yib481de92007-09-25 17:54:57 -07004139 break;
Ben Cahill9fbab512007-11-29 11:09:47 +08004140 case 5: /* 5.7GHz band */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004141 *eeprom_ch_count = ARRAY_SIZE(iwl3945_eeprom_band_5);
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08004142 *eeprom_ch_info = priv->eeprom39.band_5_channels;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004143 *eeprom_ch_index = iwl3945_eeprom_band_5;
Zhu Yib481de92007-09-25 17:54:57 -07004144 break;
4145 default:
4146 BUG();
4147 return;
4148 }
4149}
4150
Cahill, Ben M6440adb2007-11-29 11:09:55 +08004151/**
4152 * iwl3945_get_channel_info - Find driver's private channel info
4153 *
4154 * Based on band and channel number.
4155 */
Samuel Ortizd20b3c62008-12-19 10:37:15 +08004156const struct iwl_channel_info *
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08004157iwl3945_get_channel_info(const struct iwl_priv *priv,
Samuel Ortizd20b3c62008-12-19 10:37:15 +08004158 enum ieee80211_band band, u16 channel)
Zhu Yib481de92007-09-25 17:54:57 -07004159{
4160 int i;
4161
Johannes Berg8318d782008-01-24 19:38:38 +01004162 switch (band) {
4163 case IEEE80211_BAND_5GHZ:
Zhu Yib481de92007-09-25 17:54:57 -07004164 for (i = 14; i < priv->channel_count; i++) {
4165 if (priv->channel_info[i].channel == channel)
4166 return &priv->channel_info[i];
4167 }
4168 break;
4169
Johannes Berg8318d782008-01-24 19:38:38 +01004170 case IEEE80211_BAND_2GHZ:
Zhu Yib481de92007-09-25 17:54:57 -07004171 if (channel >= 1 && channel <= 14)
4172 return &priv->channel_info[channel - 1];
4173 break;
Johannes Berg8318d782008-01-24 19:38:38 +01004174 case IEEE80211_NUM_BANDS:
4175 WARN_ON(1);
Zhu Yib481de92007-09-25 17:54:57 -07004176 }
4177
4178 return NULL;
4179}
4180
4181#define CHECK_AND_PRINT(x) ((eeprom_ch_info[ch].flags & EEPROM_CHANNEL_##x) \
4182 ? # x " " : "")
4183
Cahill, Ben M6440adb2007-11-29 11:09:55 +08004184/**
4185 * iwl3945_init_channel_map - Set up driver's info for all possible channels
4186 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08004187static int iwl3945_init_channel_map(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004188{
4189 int eeprom_ch_count = 0;
4190 const u8 *eeprom_ch_index = NULL;
Samuel Ortiz0f741d92008-12-19 10:37:10 +08004191 const struct iwl_eeprom_channel *eeprom_ch_info = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07004192 int band, ch;
Samuel Ortizd20b3c62008-12-19 10:37:15 +08004193 struct iwl_channel_info *ch_info;
Zhu Yib481de92007-09-25 17:54:57 -07004194
4195 if (priv->channel_count) {
4196 IWL_DEBUG_INFO("Channel map already initialized.\n");
4197 return 0;
4198 }
4199
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08004200 if (priv->eeprom39.version < 0x2f) {
Winkler, Tomas39aadf82008-12-19 10:37:32 +08004201 IWL_WARN(priv, "Unsupported EEPROM version: 0x%04X\n",
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08004202 priv->eeprom39.version);
Zhu Yib481de92007-09-25 17:54:57 -07004203 return -EINVAL;
4204 }
4205
4206 IWL_DEBUG_INFO("Initializing regulatory info from EEPROM\n");
4207
4208 priv->channel_count =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004209 ARRAY_SIZE(iwl3945_eeprom_band_1) +
4210 ARRAY_SIZE(iwl3945_eeprom_band_2) +
4211 ARRAY_SIZE(iwl3945_eeprom_band_3) +
4212 ARRAY_SIZE(iwl3945_eeprom_band_4) +
4213 ARRAY_SIZE(iwl3945_eeprom_band_5);
Zhu Yib481de92007-09-25 17:54:57 -07004214
4215 IWL_DEBUG_INFO("Parsing data for %d channels.\n", priv->channel_count);
4216
Samuel Ortizd20b3c62008-12-19 10:37:15 +08004217 priv->channel_info = kzalloc(sizeof(struct iwl_channel_info) *
Zhu Yib481de92007-09-25 17:54:57 -07004218 priv->channel_count, GFP_KERNEL);
4219 if (!priv->channel_info) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08004220 IWL_ERR(priv, "Could not allocate channel_info\n");
Zhu Yib481de92007-09-25 17:54:57 -07004221 priv->channel_count = 0;
4222 return -ENOMEM;
4223 }
4224
4225 ch_info = priv->channel_info;
4226
4227 /* Loop through the 5 EEPROM bands adding them in order to the
4228 * channel map we maintain (that contains additional information than
4229 * what just in the EEPROM) */
4230 for (band = 1; band <= 5; band++) {
4231
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004232 iwl3945_init_band_reference(priv, band, &eeprom_ch_count,
Zhu Yib481de92007-09-25 17:54:57 -07004233 &eeprom_ch_info, &eeprom_ch_index);
4234
4235 /* Loop through each band adding each of the channels */
4236 for (ch = 0; ch < eeprom_ch_count; ch++) {
4237 ch_info->channel = eeprom_ch_index[ch];
Johannes Berg8318d782008-01-24 19:38:38 +01004238 ch_info->band = (band == 1) ? IEEE80211_BAND_2GHZ :
4239 IEEE80211_BAND_5GHZ;
Zhu Yib481de92007-09-25 17:54:57 -07004240
4241 /* permanently store EEPROM's channel regulatory flags
4242 * and max power in channel info database. */
4243 ch_info->eeprom = eeprom_ch_info[ch];
4244
4245 /* Copy the run-time flags so they are there even on
4246 * invalid channels */
4247 ch_info->flags = eeprom_ch_info[ch].flags;
4248
4249 if (!(is_channel_valid(ch_info))) {
4250 IWL_DEBUG_INFO("Ch. %d Flags %x [%sGHz] - "
4251 "No traffic\n",
4252 ch_info->channel,
4253 ch_info->flags,
4254 is_channel_a_band(ch_info) ?
4255 "5.2" : "2.4");
4256 ch_info++;
4257 continue;
4258 }
4259
4260 /* Initialize regulatory-based run-time data */
4261 ch_info->max_power_avg = ch_info->curr_txpow =
4262 eeprom_ch_info[ch].max_power_avg;
4263 ch_info->scan_power = eeprom_ch_info[ch].max_power_avg;
4264 ch_info->min_power = 0;
4265
Guy Cohenfe7c4042008-04-21 15:41:56 -07004266 IWL_DEBUG_INFO("Ch. %d [%sGHz] %s%s%s%s%s%s(0x%02x"
Zhu Yib481de92007-09-25 17:54:57 -07004267 " %ddBm): Ad-Hoc %ssupported\n",
4268 ch_info->channel,
4269 is_channel_a_band(ch_info) ?
4270 "5.2" : "2.4",
Tomas Winkler8211ef72008-03-02 01:36:04 +02004271 CHECK_AND_PRINT(VALID),
Zhu Yib481de92007-09-25 17:54:57 -07004272 CHECK_AND_PRINT(IBSS),
4273 CHECK_AND_PRINT(ACTIVE),
4274 CHECK_AND_PRINT(RADAR),
4275 CHECK_AND_PRINT(WIDE),
Zhu Yib481de92007-09-25 17:54:57 -07004276 CHECK_AND_PRINT(DFS),
4277 eeprom_ch_info[ch].flags,
4278 eeprom_ch_info[ch].max_power_avg,
4279 ((eeprom_ch_info[ch].
4280 flags & EEPROM_CHANNEL_IBSS)
4281 && !(eeprom_ch_info[ch].
4282 flags & EEPROM_CHANNEL_RADAR))
4283 ? "" : "not ");
4284
4285 /* Set the user_txpower_limit to the highest power
4286 * supported by any channel */
4287 if (eeprom_ch_info[ch].max_power_avg >
4288 priv->user_txpower_limit)
4289 priv->user_txpower_limit =
4290 eeprom_ch_info[ch].max_power_avg;
4291
4292 ch_info++;
4293 }
4294 }
4295
Cahill, Ben M6440adb2007-11-29 11:09:55 +08004296 /* Set up txpower settings in driver for all channels */
Zhu Yib481de92007-09-25 17:54:57 -07004297 if (iwl3945_txpower_set_from_eeprom(priv))
4298 return -EIO;
4299
4300 return 0;
4301}
4302
Reinette Chatre849e0dc2008-01-23 10:15:18 -08004303/*
4304 * iwl3945_free_channel_map - undo allocations in iwl3945_init_channel_map
4305 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08004306static void iwl3945_free_channel_map(struct iwl_priv *priv)
Reinette Chatre849e0dc2008-01-23 10:15:18 -08004307{
4308 kfree(priv->channel_info);
4309 priv->channel_count = 0;
4310}
4311
Zhu Yib481de92007-09-25 17:54:57 -07004312/* For active scan, listen ACTIVE_DWELL_TIME (msec) on each channel after
4313 * sending probe req. This should be set long enough to hear probe responses
4314 * from more than one AP. */
Abhijeet Kolekarf9340522008-09-03 11:26:58 +08004315#define IWL_ACTIVE_DWELL_TIME_24 (30) /* all times in msec */
4316#define IWL_ACTIVE_DWELL_TIME_52 (20)
4317
4318#define IWL_ACTIVE_DWELL_FACTOR_24GHZ (3)
4319#define IWL_ACTIVE_DWELL_FACTOR_52GHZ (2)
Zhu Yib481de92007-09-25 17:54:57 -07004320
4321/* For faster active scanning, scan will move to the next channel if fewer than
4322 * PLCP_QUIET_THRESH packets are heard on this channel within
4323 * ACTIVE_QUIET_TIME after sending probe request. This shortens the dwell
4324 * time if it's a quiet channel (nothing responded to our probe, and there's
4325 * no other traffic).
4326 * Disable "quiet" feature by setting PLCP_QUIET_THRESH to 0. */
4327#define IWL_PLCP_QUIET_THRESH __constant_cpu_to_le16(1) /* packets */
Abhijeet Kolekarf9340522008-09-03 11:26:58 +08004328#define IWL_ACTIVE_QUIET_TIME __constant_cpu_to_le16(10) /* msec */
Zhu Yib481de92007-09-25 17:54:57 -07004329
4330/* For passive scan, listen PASSIVE_DWELL_TIME (msec) on each channel.
4331 * Must be set longer than active dwell time.
4332 * For the most reliable scan, set > AP beacon interval (typically 100msec). */
4333#define IWL_PASSIVE_DWELL_TIME_24 (20) /* all times in msec */
4334#define IWL_PASSIVE_DWELL_TIME_52 (10)
4335#define IWL_PASSIVE_DWELL_BASE (100)
4336#define IWL_CHANNEL_TUNE_TIME 5
4337
Kolekar, Abhijeete720ce92008-11-07 09:58:42 -08004338#define IWL_SCAN_PROBE_MASK(n) (BIT(n) | (BIT(n) - BIT(1)))
Abhijeet Kolekarf9340522008-09-03 11:26:58 +08004339
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08004340static inline u16 iwl3945_get_active_dwell_time(struct iwl_priv *priv,
Abhijeet Kolekarf9340522008-09-03 11:26:58 +08004341 enum ieee80211_band band,
4342 u8 n_probes)
Zhu Yib481de92007-09-25 17:54:57 -07004343{
Johannes Berg8318d782008-01-24 19:38:38 +01004344 if (band == IEEE80211_BAND_5GHZ)
Abhijeet Kolekarf9340522008-09-03 11:26:58 +08004345 return IWL_ACTIVE_DWELL_TIME_52 +
4346 IWL_ACTIVE_DWELL_FACTOR_52GHZ * (n_probes + 1);
Zhu Yib481de92007-09-25 17:54:57 -07004347 else
Abhijeet Kolekarf9340522008-09-03 11:26:58 +08004348 return IWL_ACTIVE_DWELL_TIME_24 +
4349 IWL_ACTIVE_DWELL_FACTOR_24GHZ * (n_probes + 1);
Zhu Yib481de92007-09-25 17:54:57 -07004350}
4351
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08004352static u16 iwl3945_get_passive_dwell_time(struct iwl_priv *priv,
Johannes Berg8318d782008-01-24 19:38:38 +01004353 enum ieee80211_band band)
Zhu Yib481de92007-09-25 17:54:57 -07004354{
Johannes Berg8318d782008-01-24 19:38:38 +01004355 u16 passive = (band == IEEE80211_BAND_2GHZ) ?
Zhu Yib481de92007-09-25 17:54:57 -07004356 IWL_PASSIVE_DWELL_BASE + IWL_PASSIVE_DWELL_TIME_24 :
4357 IWL_PASSIVE_DWELL_BASE + IWL_PASSIVE_DWELL_TIME_52;
4358
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004359 if (iwl3945_is_associated(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07004360 /* If we're associated, we clamp the maximum passive
4361 * dwell time to be 98% of the beacon interval (minus
4362 * 2 * channel tune time) */
4363 passive = priv->beacon_int;
4364 if ((passive > IWL_PASSIVE_DWELL_BASE) || !passive)
4365 passive = IWL_PASSIVE_DWELL_BASE;
4366 passive = (passive * 98) / 100 - IWL_CHANNEL_TUNE_TIME * 2;
4367 }
4368
Zhu Yib481de92007-09-25 17:54:57 -07004369 return passive;
4370}
4371
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08004372static int iwl3945_get_channels_for_scan(struct iwl_priv *priv,
Johannes Berg8318d782008-01-24 19:38:38 +01004373 enum ieee80211_band band,
Abhijeet Kolekarf9340522008-09-03 11:26:58 +08004374 u8 is_active, u8 n_probes,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004375 struct iwl3945_scan_channel *scan_ch)
Zhu Yib481de92007-09-25 17:54:57 -07004376{
4377 const struct ieee80211_channel *channels = NULL;
Johannes Berg8318d782008-01-24 19:38:38 +01004378 const struct ieee80211_supported_band *sband;
Samuel Ortizd20b3c62008-12-19 10:37:15 +08004379 const struct iwl_channel_info *ch_info;
Zhu Yib481de92007-09-25 17:54:57 -07004380 u16 passive_dwell = 0;
4381 u16 active_dwell = 0;
4382 int added, i;
4383
Kolekar, Abhijeetcbba18c2008-12-19 10:37:42 +08004384 sband = iwl_get_hw_mode(priv, band);
Johannes Berg8318d782008-01-24 19:38:38 +01004385 if (!sband)
Zhu Yib481de92007-09-25 17:54:57 -07004386 return 0;
4387
Johannes Berg8318d782008-01-24 19:38:38 +01004388 channels = sband->channels;
Zhu Yib481de92007-09-25 17:54:57 -07004389
Abhijeet Kolekarf9340522008-09-03 11:26:58 +08004390 active_dwell = iwl3945_get_active_dwell_time(priv, band, n_probes);
Johannes Berg8318d782008-01-24 19:38:38 +01004391 passive_dwell = iwl3945_get_passive_dwell_time(priv, band);
Zhu Yib481de92007-09-25 17:54:57 -07004392
Abhijeet Kolekar8f4807a2008-09-03 11:26:31 +08004393 if (passive_dwell <= active_dwell)
4394 passive_dwell = active_dwell + 1;
4395
Johannes Berg8318d782008-01-24 19:38:38 +01004396 for (i = 0, added = 0; i < sband->n_channels; i++) {
Johannes Berg182e2e62008-04-04 10:41:56 +02004397 if (channels[i].flags & IEEE80211_CHAN_DISABLED)
4398 continue;
4399
Johannes Berg8318d782008-01-24 19:38:38 +01004400 scan_ch->channel = channels[i].hw_value;
Zhu Yib481de92007-09-25 17:54:57 -07004401
Johannes Berg8318d782008-01-24 19:38:38 +01004402 ch_info = iwl3945_get_channel_info(priv, band, scan_ch->channel);
Zhu Yib481de92007-09-25 17:54:57 -07004403 if (!is_channel_valid(ch_info)) {
Ron Rindjunsky66b50042008-06-25 16:46:31 +08004404 IWL_DEBUG_SCAN("Channel %d is INVALID for this band.\n",
Zhu Yib481de92007-09-25 17:54:57 -07004405 scan_ch->channel);
4406 continue;
4407 }
4408
Zhu Yib481de92007-09-25 17:54:57 -07004409 scan_ch->active_dwell = cpu_to_le16(active_dwell);
4410 scan_ch->passive_dwell = cpu_to_le16(passive_dwell);
Abhijeet Kolekar011a0332008-12-02 12:14:07 -08004411 /* If passive , set up for auto-switch
4412 * and use long active_dwell time.
4413 */
4414 if (!is_active || is_channel_passive(ch_info) ||
4415 (channels[i].flags & IEEE80211_CHAN_PASSIVE_SCAN)) {
4416 scan_ch->type = 0; /* passive */
4417 if (IWL_UCODE_API(priv->ucode_ver) == 1)
4418 scan_ch->active_dwell = cpu_to_le16(passive_dwell - 1);
4419 } else {
4420 scan_ch->type = 1; /* active */
4421 }
4422
4423 /* Set direct probe bits. These may be used both for active
4424 * scan channels (probes gets sent right away),
4425 * or for passive channels (probes get se sent only after
4426 * hearing clear Rx packet).*/
4427 if (IWL_UCODE_API(priv->ucode_ver) >= 2) {
4428 if (n_probes)
4429 scan_ch->type |= IWL_SCAN_PROBE_MASK(n_probes);
4430 } else {
4431 /* uCode v1 does not allow setting direct probe bits on
4432 * passive channel. */
4433 if ((scan_ch->type & 1) && n_probes)
4434 scan_ch->type |= IWL_SCAN_PROBE_MASK(n_probes);
4435 }
Zhu Yib481de92007-09-25 17:54:57 -07004436
Ben Cahill9fbab512007-11-29 11:09:47 +08004437 /* Set txpower levels to defaults */
Zhu Yib481de92007-09-25 17:54:57 -07004438 scan_ch->tpc.dsp_atten = 110;
4439 /* scan_pwr_info->tpc.dsp_atten; */
4440
4441 /*scan_pwr_info->tpc.tx_gain; */
Johannes Berg8318d782008-01-24 19:38:38 +01004442 if (band == IEEE80211_BAND_5GHZ)
Zhu Yib481de92007-09-25 17:54:57 -07004443 scan_ch->tpc.tx_gain = ((1 << 5) | (3 << 3)) | 3;
4444 else {
4445 scan_ch->tpc.tx_gain = ((1 << 5) | (5 << 3));
4446 /* NOTE: if we were doing 6Mb OFDM for scans we'd use
Ben Cahill9fbab512007-11-29 11:09:47 +08004447 * power level:
Reinette Chatre8a1b0242008-01-14 17:46:25 -08004448 * scan_ch->tpc.tx_gain = ((1 << 5) | (2 << 3)) | 3;
Zhu Yib481de92007-09-25 17:54:57 -07004449 */
4450 }
4451
4452 IWL_DEBUG_SCAN("Scanning %d [%s %d]\n",
4453 scan_ch->channel,
4454 (scan_ch->type & 1) ? "ACTIVE" : "PASSIVE",
4455 (scan_ch->type & 1) ?
4456 active_dwell : passive_dwell);
4457
4458 scan_ch++;
4459 added++;
4460 }
4461
4462 IWL_DEBUG_SCAN("total channels to scan %d \n", added);
4463 return added;
4464}
4465
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08004466static void iwl3945_init_hw_rates(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07004467 struct ieee80211_rate *rates)
4468{
4469 int i;
4470
4471 for (i = 0; i < IWL_RATE_COUNT; i++) {
Johannes Berg8318d782008-01-24 19:38:38 +01004472 rates[i].bitrate = iwl3945_rates[i].ieee * 5;
4473 rates[i].hw_value = i; /* Rate scaling will work on indexes */
4474 rates[i].hw_value_short = i;
4475 rates[i].flags = 0;
Samuel Ortizd9829a62008-12-19 10:37:12 +08004476 if ((i > IWL39_LAST_OFDM_RATE) || (i < IWL_FIRST_OFDM_RATE)) {
Zhu Yib481de92007-09-25 17:54:57 -07004477 /*
Johannes Berg8318d782008-01-24 19:38:38 +01004478 * If CCK != 1M then set short preamble rate flag.
Zhu Yib481de92007-09-25 17:54:57 -07004479 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004480 rates[i].flags |= (iwl3945_rates[i].plcp == 10) ?
Johannes Berg8318d782008-01-24 19:38:38 +01004481 0 : IEEE80211_RATE_SHORT_PREAMBLE;
Zhu Yib481de92007-09-25 17:54:57 -07004482 }
Zhu Yib481de92007-09-25 17:54:57 -07004483 }
4484}
4485
4486/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004487 * iwl3945_init_geos - Initialize mac80211's geo/channel info based from eeprom
Zhu Yib481de92007-09-25 17:54:57 -07004488 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08004489static int iwl3945_init_geos(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004490{
Samuel Ortizd20b3c62008-12-19 10:37:15 +08004491 struct iwl_channel_info *ch;
Tomas Winkler8211ef72008-03-02 01:36:04 +02004492 struct ieee80211_supported_band *sband;
Zhu Yib481de92007-09-25 17:54:57 -07004493 struct ieee80211_channel *channels;
4494 struct ieee80211_channel *geo_ch;
4495 struct ieee80211_rate *rates;
4496 int i = 0;
Zhu Yib481de92007-09-25 17:54:57 -07004497
Johannes Berg8318d782008-01-24 19:38:38 +01004498 if (priv->bands[IEEE80211_BAND_2GHZ].n_bitrates ||
4499 priv->bands[IEEE80211_BAND_5GHZ].n_bitrates) {
Zhu Yib481de92007-09-25 17:54:57 -07004500 IWL_DEBUG_INFO("Geography modes already initialized.\n");
4501 set_bit(STATUS_GEO_CONFIGURED, &priv->status);
4502 return 0;
4503 }
4504
Zhu Yib481de92007-09-25 17:54:57 -07004505 channels = kzalloc(sizeof(struct ieee80211_channel) *
4506 priv->channel_count, GFP_KERNEL);
Johannes Berg8318d782008-01-24 19:38:38 +01004507 if (!channels)
Zhu Yib481de92007-09-25 17:54:57 -07004508 return -ENOMEM;
Zhu Yib481de92007-09-25 17:54:57 -07004509
Tomas Winkler8211ef72008-03-02 01:36:04 +02004510 rates = kzalloc((sizeof(struct ieee80211_rate) * (IWL_RATE_COUNT + 1)),
Zhu Yib481de92007-09-25 17:54:57 -07004511 GFP_KERNEL);
4512 if (!rates) {
Zhu Yib481de92007-09-25 17:54:57 -07004513 kfree(channels);
4514 return -ENOMEM;
4515 }
4516
Zhu Yib481de92007-09-25 17:54:57 -07004517 /* 5.2GHz channels start after the 2.4GHz channels */
Tomas Winkler8211ef72008-03-02 01:36:04 +02004518 sband = &priv->bands[IEEE80211_BAND_5GHZ];
4519 sband->channels = &channels[ARRAY_SIZE(iwl3945_eeprom_band_1)];
4520 /* just OFDM */
4521 sband->bitrates = &rates[IWL_FIRST_OFDM_RATE];
4522 sband->n_bitrates = IWL_RATE_COUNT - IWL_FIRST_OFDM_RATE;
Zhu Yib481de92007-09-25 17:54:57 -07004523
Tomas Winkler8211ef72008-03-02 01:36:04 +02004524 sband = &priv->bands[IEEE80211_BAND_2GHZ];
4525 sband->channels = channels;
4526 /* OFDM & CCK */
4527 sband->bitrates = rates;
4528 sband->n_bitrates = IWL_RATE_COUNT;
Zhu Yib481de92007-09-25 17:54:57 -07004529
4530 priv->ieee_channels = channels;
4531 priv->ieee_rates = rates;
4532
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004533 iwl3945_init_hw_rates(priv, rates);
Zhu Yib481de92007-09-25 17:54:57 -07004534
Tomas Winkler8211ef72008-03-02 01:36:04 +02004535 for (i = 0; i < priv->channel_count; i++) {
Zhu Yib481de92007-09-25 17:54:57 -07004536 ch = &priv->channel_info[i];
4537
Tomas Winkler8211ef72008-03-02 01:36:04 +02004538 /* FIXME: might be removed if scan is OK*/
4539 if (!is_channel_valid(ch))
Zhu Yib481de92007-09-25 17:54:57 -07004540 continue;
Zhu Yib481de92007-09-25 17:54:57 -07004541
4542 if (is_channel_a_band(ch))
Tomas Winkler8211ef72008-03-02 01:36:04 +02004543 sband = &priv->bands[IEEE80211_BAND_5GHZ];
Johannes Berg8318d782008-01-24 19:38:38 +01004544 else
Tomas Winkler8211ef72008-03-02 01:36:04 +02004545 sband = &priv->bands[IEEE80211_BAND_2GHZ];
Zhu Yib481de92007-09-25 17:54:57 -07004546
Tomas Winkler8211ef72008-03-02 01:36:04 +02004547 geo_ch = &sband->channels[sband->n_channels++];
4548
4549 geo_ch->center_freq = ieee80211_channel_to_frequency(ch->channel);
Johannes Berg8318d782008-01-24 19:38:38 +01004550 geo_ch->max_power = ch->max_power_avg;
4551 geo_ch->max_antenna_gain = 0xff;
Mohamed Abbas7b723042008-01-31 21:46:40 -08004552 geo_ch->hw_value = ch->channel;
Zhu Yib481de92007-09-25 17:54:57 -07004553
4554 if (is_channel_valid(ch)) {
Johannes Berg8318d782008-01-24 19:38:38 +01004555 if (!(ch->flags & EEPROM_CHANNEL_IBSS))
4556 geo_ch->flags |= IEEE80211_CHAN_NO_IBSS;
Zhu Yib481de92007-09-25 17:54:57 -07004557
Johannes Berg8318d782008-01-24 19:38:38 +01004558 if (!(ch->flags & EEPROM_CHANNEL_ACTIVE))
4559 geo_ch->flags |= IEEE80211_CHAN_PASSIVE_SCAN;
Zhu Yib481de92007-09-25 17:54:57 -07004560
4561 if (ch->flags & EEPROM_CHANNEL_RADAR)
Johannes Berg8318d782008-01-24 19:38:38 +01004562 geo_ch->flags |= IEEE80211_CHAN_RADAR;
Zhu Yib481de92007-09-25 17:54:57 -07004563
4564 if (ch->max_power_avg > priv->max_channel_txpower_limit)
4565 priv->max_channel_txpower_limit =
4566 ch->max_power_avg;
Tomas Winkler8211ef72008-03-02 01:36:04 +02004567 } else {
Johannes Berg8318d782008-01-24 19:38:38 +01004568 geo_ch->flags |= IEEE80211_CHAN_DISABLED;
Tomas Winkler8211ef72008-03-02 01:36:04 +02004569 }
4570
4571 /* Save flags for reg domain usage */
4572 geo_ch->orig_flags = geo_ch->flags;
4573
4574 IWL_DEBUG_INFO("Channel %d Freq=%d[%sGHz] %s flag=0%X\n",
4575 ch->channel, geo_ch->center_freq,
4576 is_channel_a_band(ch) ? "5.2" : "2.4",
4577 geo_ch->flags & IEEE80211_CHAN_DISABLED ?
4578 "restricted" : "valid",
4579 geo_ch->flags);
Zhu Yib481de92007-09-25 17:54:57 -07004580 }
4581
Tomas Winkler82b9a122008-03-04 18:09:30 -08004582 if ((priv->bands[IEEE80211_BAND_5GHZ].n_channels == 0) &&
4583 priv->cfg->sku & IWL_SKU_A) {
Tomas Winkler978785a2008-12-19 10:37:31 +08004584 IWL_INFO(priv, "Incorrectly detected BG card as ABG. "
4585 "Please send your PCI ID 0x%04X:0x%04X to maintainer.\n",
4586 priv->pci_dev->device, priv->pci_dev->subsystem_device);
Tomas Winkler82b9a122008-03-04 18:09:30 -08004587 priv->cfg->sku &= ~IWL_SKU_A;
Zhu Yib481de92007-09-25 17:54:57 -07004588 }
4589
Tomas Winkler978785a2008-12-19 10:37:31 +08004590 IWL_INFO(priv, "Tunable channels: %d 802.11bg, %d 802.11a channels\n",
Johannes Berg8318d782008-01-24 19:38:38 +01004591 priv->bands[IEEE80211_BAND_2GHZ].n_channels,
4592 priv->bands[IEEE80211_BAND_5GHZ].n_channels);
Zhu Yib481de92007-09-25 17:54:57 -07004593
John W. Linvillee0e0a672008-03-25 15:58:40 -04004594 if (priv->bands[IEEE80211_BAND_2GHZ].n_channels)
4595 priv->hw->wiphy->bands[IEEE80211_BAND_2GHZ] =
4596 &priv->bands[IEEE80211_BAND_2GHZ];
4597 if (priv->bands[IEEE80211_BAND_5GHZ].n_channels)
4598 priv->hw->wiphy->bands[IEEE80211_BAND_5GHZ] =
4599 &priv->bands[IEEE80211_BAND_5GHZ];
Zhu Yib481de92007-09-25 17:54:57 -07004600
Zhu Yib481de92007-09-25 17:54:57 -07004601 set_bit(STATUS_GEO_CONFIGURED, &priv->status);
4602
4603 return 0;
4604}
4605
Reinette Chatre849e0dc2008-01-23 10:15:18 -08004606/*
4607 * iwl3945_free_geos - undo allocations in iwl3945_init_geos
4608 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08004609static void iwl3945_free_geos(struct iwl_priv *priv)
Reinette Chatre849e0dc2008-01-23 10:15:18 -08004610{
Reinette Chatre849e0dc2008-01-23 10:15:18 -08004611 kfree(priv->ieee_channels);
4612 kfree(priv->ieee_rates);
4613 clear_bit(STATUS_GEO_CONFIGURED, &priv->status);
4614}
4615
Zhu Yib481de92007-09-25 17:54:57 -07004616/******************************************************************************
4617 *
4618 * uCode download functions
4619 *
4620 ******************************************************************************/
4621
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08004622static void iwl3945_dealloc_ucode_pci(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004623{
Tomas Winkler98c92212008-01-14 17:46:20 -08004624 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_code);
4625 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_data);
4626 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_data_backup);
4627 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_init);
4628 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_init_data);
4629 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_boot);
Zhu Yib481de92007-09-25 17:54:57 -07004630}
4631
4632/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004633 * iwl3945_verify_inst_full - verify runtime uCode image in card vs. host,
Zhu Yib481de92007-09-25 17:54:57 -07004634 * looking at all data.
4635 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08004636static int iwl3945_verify_inst_full(struct iwl_priv *priv, __le32 *image, u32 len)
Zhu Yib481de92007-09-25 17:54:57 -07004637{
4638 u32 val;
4639 u32 save_len = len;
4640 int rc = 0;
4641 u32 errcnt;
4642
4643 IWL_DEBUG_INFO("ucode inst image size is %u\n", len);
4644
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08004645 rc = iwl_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004646 if (rc)
4647 return rc;
4648
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08004649 iwl_write_direct32(priv, HBUS_TARG_MEM_RADDR,
Samuel Ortiz250bdd22008-12-19 10:37:11 +08004650 IWL39_RTC_INST_LOWER_BOUND);
Zhu Yib481de92007-09-25 17:54:57 -07004651
4652 errcnt = 0;
4653 for (; len > 0; len -= sizeof(u32), image++) {
4654 /* read data comes through single port, auto-incr addr */
4655 /* NOTE: Use the debugless read so we don't flood kernel log
4656 * if IWL_DL_IO is set */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08004657 val = _iwl_read_direct32(priv, HBUS_TARG_MEM_RDAT);
Zhu Yib481de92007-09-25 17:54:57 -07004658 if (val != le32_to_cpu(*image)) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08004659 IWL_ERR(priv, "uCode INST section is invalid at "
Zhu Yib481de92007-09-25 17:54:57 -07004660 "offset 0x%x, is 0x%x, s/b 0x%x\n",
4661 save_len - len, val, le32_to_cpu(*image));
4662 rc = -EIO;
4663 errcnt++;
4664 if (errcnt >= 20)
4665 break;
4666 }
4667 }
4668
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08004669 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004670
4671 if (!errcnt)
Ian Schrambc434dd2007-10-25 17:15:29 +08004672 IWL_DEBUG_INFO("ucode image in INSTRUCTION memory is good\n");
Zhu Yib481de92007-09-25 17:54:57 -07004673
4674 return rc;
4675}
4676
4677
4678/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004679 * iwl3945_verify_inst_sparse - verify runtime uCode image in card vs. host,
Zhu Yib481de92007-09-25 17:54:57 -07004680 * using sample data 100 bytes apart. If these sample points are good,
4681 * it's a pretty good bet that everything between them is good, too.
4682 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08004683static int iwl3945_verify_inst_sparse(struct iwl_priv *priv, __le32 *image, u32 len)
Zhu Yib481de92007-09-25 17:54:57 -07004684{
4685 u32 val;
4686 int rc = 0;
4687 u32 errcnt = 0;
4688 u32 i;
4689
4690 IWL_DEBUG_INFO("ucode inst image size is %u\n", len);
4691
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08004692 rc = iwl_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004693 if (rc)
4694 return rc;
4695
4696 for (i = 0; i < len; i += 100, image += 100/sizeof(u32)) {
4697 /* read data comes through single port, auto-incr addr */
4698 /* NOTE: Use the debugless read so we don't flood kernel log
4699 * if IWL_DL_IO is set */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08004700 iwl_write_direct32(priv, HBUS_TARG_MEM_RADDR,
Samuel Ortiz250bdd22008-12-19 10:37:11 +08004701 i + IWL39_RTC_INST_LOWER_BOUND);
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08004702 val = _iwl_read_direct32(priv, HBUS_TARG_MEM_RDAT);
Zhu Yib481de92007-09-25 17:54:57 -07004703 if (val != le32_to_cpu(*image)) {
4704#if 0 /* Enable this if you want to see details */
Winkler, Tomas15b16872008-12-19 10:37:33 +08004705 IWL_ERR(priv, "uCode INST section is invalid at "
Zhu Yib481de92007-09-25 17:54:57 -07004706 "offset 0x%x, is 0x%x, s/b 0x%x\n",
4707 i, val, *image);
4708#endif
4709 rc = -EIO;
4710 errcnt++;
4711 if (errcnt >= 3)
4712 break;
4713 }
4714 }
4715
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08004716 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004717
4718 return rc;
4719}
4720
4721
4722/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004723 * iwl3945_verify_ucode - determine which instruction image is in SRAM,
Zhu Yib481de92007-09-25 17:54:57 -07004724 * and verify its contents
4725 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08004726static int iwl3945_verify_ucode(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004727{
4728 __le32 *image;
4729 u32 len;
4730 int rc = 0;
4731
4732 /* Try bootstrap */
4733 image = (__le32 *)priv->ucode_boot.v_addr;
4734 len = priv->ucode_boot.len;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004735 rc = iwl3945_verify_inst_sparse(priv, image, len);
Zhu Yib481de92007-09-25 17:54:57 -07004736 if (rc == 0) {
4737 IWL_DEBUG_INFO("Bootstrap uCode is good in inst SRAM\n");
4738 return 0;
4739 }
4740
4741 /* Try initialize */
4742 image = (__le32 *)priv->ucode_init.v_addr;
4743 len = priv->ucode_init.len;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004744 rc = iwl3945_verify_inst_sparse(priv, image, len);
Zhu Yib481de92007-09-25 17:54:57 -07004745 if (rc == 0) {
4746 IWL_DEBUG_INFO("Initialize uCode is good in inst SRAM\n");
4747 return 0;
4748 }
4749
4750 /* Try runtime/protocol */
4751 image = (__le32 *)priv->ucode_code.v_addr;
4752 len = priv->ucode_code.len;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004753 rc = iwl3945_verify_inst_sparse(priv, image, len);
Zhu Yib481de92007-09-25 17:54:57 -07004754 if (rc == 0) {
4755 IWL_DEBUG_INFO("Runtime uCode is good in inst SRAM\n");
4756 return 0;
4757 }
4758
Winkler, Tomas15b16872008-12-19 10:37:33 +08004759 IWL_ERR(priv, "NO VALID UCODE IMAGE IN INSTRUCTION SRAM!!\n");
Zhu Yib481de92007-09-25 17:54:57 -07004760
Ben Cahill9fbab512007-11-29 11:09:47 +08004761 /* Since nothing seems to match, show first several data entries in
4762 * instruction SRAM, so maybe visual inspection will give a clue.
4763 * Selection of bootstrap image (vs. other images) is arbitrary. */
Zhu Yib481de92007-09-25 17:54:57 -07004764 image = (__le32 *)priv->ucode_boot.v_addr;
4765 len = priv->ucode_boot.len;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004766 rc = iwl3945_verify_inst_full(priv, image, len);
Zhu Yib481de92007-09-25 17:54:57 -07004767
4768 return rc;
4769}
4770
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08004771static void iwl3945_nic_start(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004772{
4773 /* Remove all resets to allow NIC to operate */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08004774 iwl_write32(priv, CSR_RESET, 0);
Zhu Yib481de92007-09-25 17:54:57 -07004775}
4776
4777/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004778 * iwl3945_read_ucode - Read uCode images from disk file.
Zhu Yib481de92007-09-25 17:54:57 -07004779 *
4780 * Copy into buffers for card to fetch via bus-mastering
4781 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08004782static int iwl3945_read_ucode(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004783{
Kolekar, Abhijeeta78fe752008-12-19 10:37:21 +08004784 struct iwl_ucode *ucode;
Reinette Chatrea0987a82008-12-02 12:14:06 -08004785 int ret = -EINVAL, index;
Zhu Yib481de92007-09-25 17:54:57 -07004786 const struct firmware *ucode_raw;
4787 /* firmware file name contains uCode/driver compatibility version */
Reinette Chatrea0987a82008-12-02 12:14:06 -08004788 const char *name_pre = priv->cfg->fw_name_pre;
4789 const unsigned int api_max = priv->cfg->ucode_api_max;
4790 const unsigned int api_min = priv->cfg->ucode_api_min;
4791 char buf[25];
Zhu Yib481de92007-09-25 17:54:57 -07004792 u8 *src;
4793 size_t len;
Reinette Chatrea0987a82008-12-02 12:14:06 -08004794 u32 api_ver, inst_size, data_size, init_size, init_data_size, boot_size;
Zhu Yib481de92007-09-25 17:54:57 -07004795
4796 /* Ask kernel firmware_class module to get the boot firmware off disk.
4797 * request_firmware() is synchronous, file is in memory on return. */
Reinette Chatrea0987a82008-12-02 12:14:06 -08004798 for (index = api_max; index >= api_min; index--) {
4799 sprintf(buf, "%s%u%s", name_pre, index, ".ucode");
4800 ret = request_firmware(&ucode_raw, buf, &priv->pci_dev->dev);
4801 if (ret < 0) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08004802 IWL_ERR(priv, "%s firmware file req failed: %d\n",
Reinette Chatrea0987a82008-12-02 12:14:06 -08004803 buf, ret);
4804 if (ret == -ENOENT)
4805 continue;
4806 else
4807 goto error;
4808 } else {
4809 if (index < api_max)
Winkler, Tomas15b16872008-12-19 10:37:33 +08004810 IWL_ERR(priv, "Loaded firmware %s, "
4811 "which is deprecated. "
4812 " Please use API v%u instead.\n",
Reinette Chatrea0987a82008-12-02 12:14:06 -08004813 buf, api_max);
4814 IWL_DEBUG_INFO("Got firmware '%s' file (%zd bytes) from disk\n",
4815 buf, ucode_raw->size);
4816 break;
4817 }
Zhu Yib481de92007-09-25 17:54:57 -07004818 }
4819
Reinette Chatrea0987a82008-12-02 12:14:06 -08004820 if (ret < 0)
4821 goto error;
Zhu Yib481de92007-09-25 17:54:57 -07004822
4823 /* Make sure that we got at least our header! */
4824 if (ucode_raw->size < sizeof(*ucode)) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08004825 IWL_ERR(priv, "File size way too small!\n");
Tomas Winkler90e759d2007-11-29 11:09:41 +08004826 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07004827 goto err_release;
4828 }
4829
4830 /* Data from ucode file: header followed by uCode images */
4831 ucode = (void *)ucode_raw->data;
4832
Chatre, Reinettec02b3ac2008-12-02 12:14:05 -08004833 priv->ucode_ver = le32_to_cpu(ucode->ver);
Reinette Chatrea0987a82008-12-02 12:14:06 -08004834 api_ver = IWL_UCODE_API(priv->ucode_ver);
Zhu Yib481de92007-09-25 17:54:57 -07004835 inst_size = le32_to_cpu(ucode->inst_size);
4836 data_size = le32_to_cpu(ucode->data_size);
4837 init_size = le32_to_cpu(ucode->init_size);
4838 init_data_size = le32_to_cpu(ucode->init_data_size);
4839 boot_size = le32_to_cpu(ucode->boot_size);
4840
Reinette Chatrea0987a82008-12-02 12:14:06 -08004841 /* api_ver should match the api version forming part of the
4842 * firmware filename ... but we don't check for that and only rely
4843 * on the API version read from firware header from here on forward */
4844
4845 if (api_ver < api_min || api_ver > api_max) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08004846 IWL_ERR(priv, "Driver unable to support your firmware API. "
Reinette Chatrea0987a82008-12-02 12:14:06 -08004847 "Driver supports v%u, firmware is v%u.\n",
4848 api_max, api_ver);
4849 priv->ucode_ver = 0;
4850 ret = -EINVAL;
4851 goto err_release;
4852 }
4853 if (api_ver != api_max)
Winkler, Tomas15b16872008-12-19 10:37:33 +08004854 IWL_ERR(priv, "Firmware has old API version. Expected %u, "
Reinette Chatrea0987a82008-12-02 12:14:06 -08004855 "got %u. New firmware can be obtained "
4856 "from http://www.intellinuxwireless.org.\n",
4857 api_max, api_ver);
4858
Tomas Winkler978785a2008-12-19 10:37:31 +08004859 IWL_INFO(priv, "loaded firmware version %u.%u.%u.%u\n",
4860 IWL_UCODE_MAJOR(priv->ucode_ver),
4861 IWL_UCODE_MINOR(priv->ucode_ver),
4862 IWL_UCODE_API(priv->ucode_ver),
4863 IWL_UCODE_SERIAL(priv->ucode_ver));
4864
Reinette Chatrea0987a82008-12-02 12:14:06 -08004865 IWL_DEBUG_INFO("f/w package hdr ucode version raw = 0x%x\n",
4866 priv->ucode_ver);
Ian Schrambc434dd2007-10-25 17:15:29 +08004867 IWL_DEBUG_INFO("f/w package hdr runtime inst size = %u\n", inst_size);
4868 IWL_DEBUG_INFO("f/w package hdr runtime data size = %u\n", data_size);
4869 IWL_DEBUG_INFO("f/w package hdr init inst size = %u\n", init_size);
4870 IWL_DEBUG_INFO("f/w package hdr init data size = %u\n", init_data_size);
4871 IWL_DEBUG_INFO("f/w package hdr boot inst size = %u\n", boot_size);
Zhu Yib481de92007-09-25 17:54:57 -07004872
Reinette Chatrea0987a82008-12-02 12:14:06 -08004873
Zhu Yib481de92007-09-25 17:54:57 -07004874 /* Verify size of file vs. image size info in file's header */
4875 if (ucode_raw->size < sizeof(*ucode) +
4876 inst_size + data_size + init_size +
4877 init_data_size + boot_size) {
4878
4879 IWL_DEBUG_INFO("uCode file size %d too small\n",
4880 (int)ucode_raw->size);
Tomas Winkler90e759d2007-11-29 11:09:41 +08004881 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07004882 goto err_release;
4883 }
4884
4885 /* Verify that uCode images will fit in card's SRAM */
Samuel Ortiz250bdd22008-12-19 10:37:11 +08004886 if (inst_size > IWL39_MAX_INST_SIZE) {
Tomas Winkler90e759d2007-11-29 11:09:41 +08004887 IWL_DEBUG_INFO("uCode instr len %d too large to fit in\n",
4888 inst_size);
4889 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07004890 goto err_release;
4891 }
4892
Samuel Ortiz250bdd22008-12-19 10:37:11 +08004893 if (data_size > IWL39_MAX_DATA_SIZE) {
Tomas Winkler90e759d2007-11-29 11:09:41 +08004894 IWL_DEBUG_INFO("uCode data len %d too large to fit in\n",
4895 data_size);
4896 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07004897 goto err_release;
4898 }
Samuel Ortiz250bdd22008-12-19 10:37:11 +08004899 if (init_size > IWL39_MAX_INST_SIZE) {
Tomas Winkler90e759d2007-11-29 11:09:41 +08004900 IWL_DEBUG_INFO("uCode init instr len %d too large to fit in\n",
4901 init_size);
4902 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07004903 goto err_release;
4904 }
Samuel Ortiz250bdd22008-12-19 10:37:11 +08004905 if (init_data_size > IWL39_MAX_DATA_SIZE) {
Tomas Winkler90e759d2007-11-29 11:09:41 +08004906 IWL_DEBUG_INFO("uCode init data len %d too large to fit in\n",
4907 init_data_size);
4908 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07004909 goto err_release;
4910 }
Samuel Ortiz250bdd22008-12-19 10:37:11 +08004911 if (boot_size > IWL39_MAX_BSM_SIZE) {
Tomas Winkler90e759d2007-11-29 11:09:41 +08004912 IWL_DEBUG_INFO("uCode boot instr len %d too large to fit in\n",
4913 boot_size);
4914 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07004915 goto err_release;
4916 }
4917
4918 /* Allocate ucode buffers for card's bus-master loading ... */
4919
4920 /* Runtime instructions and 2 copies of data:
4921 * 1) unmodified from disk
4922 * 2) backup cache for save/restore during power-downs */
4923 priv->ucode_code.len = inst_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08004924 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_code);
Zhu Yib481de92007-09-25 17:54:57 -07004925
4926 priv->ucode_data.len = data_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08004927 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_data);
Zhu Yib481de92007-09-25 17:54:57 -07004928
4929 priv->ucode_data_backup.len = data_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08004930 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_data_backup);
Zhu Yib481de92007-09-25 17:54:57 -07004931
4932 if (!priv->ucode_code.v_addr || !priv->ucode_data.v_addr ||
Tomas Winkler90e759d2007-11-29 11:09:41 +08004933 !priv->ucode_data_backup.v_addr)
Zhu Yib481de92007-09-25 17:54:57 -07004934 goto err_pci_alloc;
4935
Tomas Winkler90e759d2007-11-29 11:09:41 +08004936 /* Initialization instructions and data */
4937 if (init_size && init_data_size) {
4938 priv->ucode_init.len = init_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08004939 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_init);
Tomas Winkler90e759d2007-11-29 11:09:41 +08004940
4941 priv->ucode_init_data.len = init_data_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08004942 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_init_data);
Tomas Winkler90e759d2007-11-29 11:09:41 +08004943
4944 if (!priv->ucode_init.v_addr || !priv->ucode_init_data.v_addr)
4945 goto err_pci_alloc;
4946 }
4947
4948 /* Bootstrap (instructions only, no data) */
4949 if (boot_size) {
4950 priv->ucode_boot.len = boot_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08004951 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_boot);
Tomas Winkler90e759d2007-11-29 11:09:41 +08004952
4953 if (!priv->ucode_boot.v_addr)
4954 goto err_pci_alloc;
4955 }
4956
Zhu Yib481de92007-09-25 17:54:57 -07004957 /* Copy images into buffers for card's bus-master reads ... */
4958
4959 /* Runtime instructions (first block of data in file) */
4960 src = &ucode->data[0];
4961 len = priv->ucode_code.len;
Tomas Winkler90e759d2007-11-29 11:09:41 +08004962 IWL_DEBUG_INFO("Copying (but not loading) uCode instr len %Zd\n", len);
Zhu Yib481de92007-09-25 17:54:57 -07004963 memcpy(priv->ucode_code.v_addr, src, len);
4964 IWL_DEBUG_INFO("uCode instr buf vaddr = 0x%p, paddr = 0x%08x\n",
4965 priv->ucode_code.v_addr, (u32)priv->ucode_code.p_addr);
4966
4967 /* Runtime data (2nd block)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004968 * NOTE: Copy into backup buffer will be done in iwl3945_up() */
Zhu Yib481de92007-09-25 17:54:57 -07004969 src = &ucode->data[inst_size];
4970 len = priv->ucode_data.len;
Tomas Winkler90e759d2007-11-29 11:09:41 +08004971 IWL_DEBUG_INFO("Copying (but not loading) uCode data len %Zd\n", len);
Zhu Yib481de92007-09-25 17:54:57 -07004972 memcpy(priv->ucode_data.v_addr, src, len);
4973 memcpy(priv->ucode_data_backup.v_addr, src, len);
4974
4975 /* Initialization instructions (3rd block) */
4976 if (init_size) {
4977 src = &ucode->data[inst_size + data_size];
4978 len = priv->ucode_init.len;
Tomas Winkler90e759d2007-11-29 11:09:41 +08004979 IWL_DEBUG_INFO("Copying (but not loading) init instr len %Zd\n",
4980 len);
Zhu Yib481de92007-09-25 17:54:57 -07004981 memcpy(priv->ucode_init.v_addr, src, len);
4982 }
4983
4984 /* Initialization data (4th block) */
4985 if (init_data_size) {
4986 src = &ucode->data[inst_size + data_size + init_size];
4987 len = priv->ucode_init_data.len;
4988 IWL_DEBUG_INFO("Copying (but not loading) init data len %d\n",
4989 (int)len);
4990 memcpy(priv->ucode_init_data.v_addr, src, len);
4991 }
4992
4993 /* Bootstrap instructions (5th block) */
4994 src = &ucode->data[inst_size + data_size + init_size + init_data_size];
4995 len = priv->ucode_boot.len;
4996 IWL_DEBUG_INFO("Copying (but not loading) boot instr len %d\n",
4997 (int)len);
4998 memcpy(priv->ucode_boot.v_addr, src, len);
4999
5000 /* We have our copies now, allow OS release its copies */
5001 release_firmware(ucode_raw);
5002 return 0;
5003
5004 err_pci_alloc:
Winkler, Tomas15b16872008-12-19 10:37:33 +08005005 IWL_ERR(priv, "failed to allocate pci memory\n");
Tomas Winkler90e759d2007-11-29 11:09:41 +08005006 ret = -ENOMEM;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005007 iwl3945_dealloc_ucode_pci(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005008
5009 err_release:
5010 release_firmware(ucode_raw);
5011
5012 error:
Tomas Winkler90e759d2007-11-29 11:09:41 +08005013 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07005014}
5015
5016
5017/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005018 * iwl3945_set_ucode_ptrs - Set uCode address location
Zhu Yib481de92007-09-25 17:54:57 -07005019 *
5020 * Tell initialization uCode where to find runtime uCode.
5021 *
5022 * BSM registers initially contain pointers to initialization uCode.
5023 * We need to replace them to load runtime uCode inst and data,
5024 * and to save runtime data when powering down.
5025 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08005026static int iwl3945_set_ucode_ptrs(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005027{
5028 dma_addr_t pinst;
5029 dma_addr_t pdata;
5030 int rc = 0;
5031 unsigned long flags;
5032
5033 /* bits 31:0 for 3945 */
5034 pinst = priv->ucode_code.p_addr;
5035 pdata = priv->ucode_data_backup.p_addr;
5036
5037 spin_lock_irqsave(&priv->lock, flags);
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08005038 rc = iwl_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005039 if (rc) {
5040 spin_unlock_irqrestore(&priv->lock, flags);
5041 return rc;
5042 }
5043
5044 /* Tell bootstrap uCode where to find image to load */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08005045 iwl_write_prph(priv, BSM_DRAM_INST_PTR_REG, pinst);
5046 iwl_write_prph(priv, BSM_DRAM_DATA_PTR_REG, pdata);
5047 iwl_write_prph(priv, BSM_DRAM_DATA_BYTECOUNT_REG,
Zhu Yib481de92007-09-25 17:54:57 -07005048 priv->ucode_data.len);
5049
Tomas Winklera96a27f2008-10-23 23:48:56 -07005050 /* Inst byte count must be last to set up, bit 31 signals uCode
Zhu Yib481de92007-09-25 17:54:57 -07005051 * that all new ptr/size info is in place */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08005052 iwl_write_prph(priv, BSM_DRAM_INST_BYTECOUNT_REG,
Zhu Yib481de92007-09-25 17:54:57 -07005053 priv->ucode_code.len | BSM_DRAM_INST_LOAD);
5054
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08005055 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005056
5057 spin_unlock_irqrestore(&priv->lock, flags);
5058
5059 IWL_DEBUG_INFO("Runtime uCode pointers are set.\n");
5060
5061 return rc;
5062}
5063
5064/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005065 * iwl3945_init_alive_start - Called after REPLY_ALIVE notification received
Zhu Yib481de92007-09-25 17:54:57 -07005066 *
5067 * Called after REPLY_ALIVE notification received from "initialize" uCode.
5068 *
Zhu Yib481de92007-09-25 17:54:57 -07005069 * Tell "initialize" uCode to go ahead and load the runtime uCode.
Ben Cahill9fbab512007-11-29 11:09:47 +08005070 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08005071static void iwl3945_init_alive_start(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005072{
5073 /* Check alive response for "valid" sign from uCode */
5074 if (priv->card_alive_init.is_valid != UCODE_VALID_OK) {
5075 /* We had an error bringing up the hardware, so take it
5076 * all the way back down so we can try again */
5077 IWL_DEBUG_INFO("Initialize Alive failed.\n");
5078 goto restart;
5079 }
5080
5081 /* Bootstrap uCode has loaded initialize uCode ... verify inst image.
5082 * This is a paranoid check, because we would not have gotten the
5083 * "initialize" alive if code weren't properly loaded. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005084 if (iwl3945_verify_ucode(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07005085 /* Runtime instruction load was bad;
5086 * take it all the way back down so we can try again */
5087 IWL_DEBUG_INFO("Bad \"initialize\" uCode load.\n");
5088 goto restart;
5089 }
5090
5091 /* Send pointers to protocol/runtime uCode image ... init code will
5092 * load and launch runtime uCode, which will send us another "Alive"
5093 * notification. */
5094 IWL_DEBUG_INFO("Initialization Alive received.\n");
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005095 if (iwl3945_set_ucode_ptrs(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07005096 /* Runtime instruction load won't happen;
5097 * take it all the way back down so we can try again */
5098 IWL_DEBUG_INFO("Couldn't set up uCode pointers.\n");
5099 goto restart;
5100 }
5101 return;
5102
5103 restart:
5104 queue_work(priv->workqueue, &priv->restart);
5105}
5106
5107
Mohamed Abbas9bdf5ec2008-11-07 09:58:35 -08005108/* temporary */
5109static int iwl3945_mac_beacon_update(struct ieee80211_hw *hw,
5110 struct sk_buff *skb);
5111
Zhu Yib481de92007-09-25 17:54:57 -07005112/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005113 * iwl3945_alive_start - called after REPLY_ALIVE notification received
Zhu Yib481de92007-09-25 17:54:57 -07005114 * from protocol/runtime uCode (initialization uCode's
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005115 * Alive gets handled by iwl3945_init_alive_start()).
Zhu Yib481de92007-09-25 17:54:57 -07005116 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08005117static void iwl3945_alive_start(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005118{
5119 int rc = 0;
5120 int thermal_spin = 0;
5121 u32 rfkill;
5122
5123 IWL_DEBUG_INFO("Runtime Alive received.\n");
5124
5125 if (priv->card_alive.is_valid != UCODE_VALID_OK) {
5126 /* We had an error bringing up the hardware, so take it
5127 * all the way back down so we can try again */
5128 IWL_DEBUG_INFO("Alive failed.\n");
5129 goto restart;
5130 }
5131
5132 /* Initialize uCode has loaded Runtime uCode ... verify inst image.
5133 * This is a paranoid check, because we would not have gotten the
5134 * "runtime" alive if code weren't properly loaded. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005135 if (iwl3945_verify_ucode(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07005136 /* Runtime instruction load was bad;
5137 * take it all the way back down so we can try again */
5138 IWL_DEBUG_INFO("Bad runtime uCode load.\n");
5139 goto restart;
5140 }
5141
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005142 iwl3945_clear_stations_table(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005143
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08005144 rc = iwl_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005145 if (rc) {
Winkler, Tomas39aadf82008-12-19 10:37:32 +08005146 IWL_WARN(priv, "Can not read RFKILL status from adapter\n");
Zhu Yib481de92007-09-25 17:54:57 -07005147 return;
5148 }
5149
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08005150 rfkill = iwl_read_prph(priv, APMG_RFKILL_REG);
Zhu Yib481de92007-09-25 17:54:57 -07005151 IWL_DEBUG_INFO("RFKILL status: 0x%x\n", rfkill);
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08005152 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005153
5154 if (rfkill & 0x1) {
5155 clear_bit(STATUS_RF_KILL_HW, &priv->status);
Tomas Winklera96a27f2008-10-23 23:48:56 -07005156 /* if RFKILL is not on, then wait for thermal
Zhu Yib481de92007-09-25 17:54:57 -07005157 * sensor in adapter to kick in */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005158 while (iwl3945_hw_get_temperature(priv) == 0) {
Zhu Yib481de92007-09-25 17:54:57 -07005159 thermal_spin++;
5160 udelay(10);
5161 }
5162
5163 if (thermal_spin)
5164 IWL_DEBUG_INFO("Thermal calibration took %dus\n",
5165 thermal_spin * 10);
5166 } else
5167 set_bit(STATUS_RF_KILL_HW, &priv->status);
5168
Ben Cahill9fbab512007-11-29 11:09:47 +08005169 /* After the ALIVE response, we can send commands to 3945 uCode */
Zhu Yib481de92007-09-25 17:54:57 -07005170 set_bit(STATUS_ALIVE, &priv->status);
5171
5172 /* Clear out the uCode error bit if it is set */
5173 clear_bit(STATUS_FW_ERROR, &priv->status);
5174
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08005175 if (iwl_is_rfkill(priv))
Zhu Yib481de92007-09-25 17:54:57 -07005176 return;
5177
Johannes Berg36d68252008-05-15 12:55:26 +02005178 ieee80211_wake_queues(priv->hw);
Zhu Yib481de92007-09-25 17:54:57 -07005179
5180 priv->active_rate = priv->rates_mask;
5181 priv->active_rate_basic = priv->rates_mask & IWL_BASIC_RATES_MASK;
5182
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005183 iwl3945_send_power_mode(priv, IWL_POWER_LEVEL(priv->power_mode));
Zhu Yib481de92007-09-25 17:54:57 -07005184
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005185 if (iwl3945_is_associated(priv)) {
5186 struct iwl3945_rxon_cmd *active_rxon =
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08005187 (struct iwl3945_rxon_cmd *)(&priv->active39_rxon);
Zhu Yib481de92007-09-25 17:54:57 -07005188
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08005189 memcpy(&priv->staging39_rxon, &priv->active39_rxon,
5190 sizeof(priv->staging39_rxon));
Zhu Yib481de92007-09-25 17:54:57 -07005191 active_rxon->filter_flags &= ~RXON_FILTER_ASSOC_MSK;
5192 } else {
5193 /* Initialize our rx_config data */
Zhu, Yi60294de2008-10-29 14:05:45 -07005194 iwl3945_connection_init_rx_config(priv, priv->iw_mode);
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08005195 memcpy(priv->staging39_rxon.node_addr, priv->mac_addr, ETH_ALEN);
Zhu Yib481de92007-09-25 17:54:57 -07005196 }
5197
Ben Cahill9fbab512007-11-29 11:09:47 +08005198 /* Configure Bluetooth device coexistence support */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005199 iwl3945_send_bt_config(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005200
5201 /* Configure the adapter for unassociated operation */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005202 iwl3945_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005203
Zhu Yib481de92007-09-25 17:54:57 -07005204 iwl3945_reg_txpower_periodic(priv);
5205
Reinette Chatrefe00b5a2008-04-03 16:05:23 -07005206 iwl3945_led_register(priv);
5207
Zhu Yib481de92007-09-25 17:54:57 -07005208 IWL_DEBUG_INFO("ALIVE processing complete.\n");
Rick Farringtona9f46782008-03-18 14:57:49 -07005209 set_bit(STATUS_READY, &priv->status);
Zhu Yi5a669262008-01-14 17:46:18 -08005210 wake_up_interruptible(&priv->wait_command_queue);
Zhu Yib481de92007-09-25 17:54:57 -07005211
5212 if (priv->error_recovering)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005213 iwl3945_error_recovery(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005214
Mohamed Abbas9bdf5ec2008-11-07 09:58:35 -08005215 /* reassociate for ADHOC mode */
5216 if (priv->vif && (priv->iw_mode == NL80211_IFTYPE_ADHOC)) {
5217 struct sk_buff *beacon = ieee80211_beacon_get(priv->hw,
5218 priv->vif);
5219 if (beacon)
5220 iwl3945_mac_beacon_update(priv->hw, beacon);
5221 }
5222
Zhu Yib481de92007-09-25 17:54:57 -07005223 return;
5224
5225 restart:
5226 queue_work(priv->workqueue, &priv->restart);
5227}
5228
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08005229static void iwl3945_cancel_deferred_work(struct iwl_priv *priv);
Zhu Yib481de92007-09-25 17:54:57 -07005230
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08005231static void __iwl3945_down(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005232{
5233 unsigned long flags;
5234 int exit_pending = test_bit(STATUS_EXIT_PENDING, &priv->status);
5235 struct ieee80211_conf *conf = NULL;
5236
5237 IWL_DEBUG_INFO(DRV_NAME " is going down\n");
5238
5239 conf = ieee80211_get_hw_conf(priv->hw);
5240
5241 if (!exit_pending)
5242 set_bit(STATUS_EXIT_PENDING, &priv->status);
5243
Mohamed Abbasab53d8a2008-03-25 16:33:36 -07005244 iwl3945_led_unregister(priv);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005245 iwl3945_clear_stations_table(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005246
5247 /* Unblock any waiting calls */
5248 wake_up_interruptible_all(&priv->wait_command_queue);
5249
Zhu Yib481de92007-09-25 17:54:57 -07005250 /* Wipe out the EXIT_PENDING status bit if we are not actually
5251 * exiting the module */
5252 if (!exit_pending)
5253 clear_bit(STATUS_EXIT_PENDING, &priv->status);
5254
5255 /* stop and reset the on-board processor */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08005256 iwl_write32(priv, CSR_RESET, CSR_RESET_REG_FLAG_NEVO_RESET);
Zhu Yib481de92007-09-25 17:54:57 -07005257
5258 /* tell the device to stop sending interrupts */
Mohamed Abbas0359fac2008-03-28 16:21:08 -07005259 spin_lock_irqsave(&priv->lock, flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005260 iwl3945_disable_interrupts(priv);
Mohamed Abbas0359fac2008-03-28 16:21:08 -07005261 spin_unlock_irqrestore(&priv->lock, flags);
5262 iwl_synchronize_irq(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005263
5264 if (priv->mac80211_registered)
5265 ieee80211_stop_queues(priv->hw);
5266
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005267 /* If we have not previously called iwl3945_init() then
Zhu Yib481de92007-09-25 17:54:57 -07005268 * clear all bits but the RF Kill and SUSPEND bits and return */
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08005269 if (!iwl_is_init(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07005270 priv->status = test_bit(STATUS_RF_KILL_HW, &priv->status) <<
5271 STATUS_RF_KILL_HW |
5272 test_bit(STATUS_RF_KILL_SW, &priv->status) <<
5273 STATUS_RF_KILL_SW |
Reinette Chatre97888642008-02-06 11:20:38 -08005274 test_bit(STATUS_GEO_CONFIGURED, &priv->status) <<
5275 STATUS_GEO_CONFIGURED |
Zhu Yib481de92007-09-25 17:54:57 -07005276 test_bit(STATUS_IN_SUSPEND, &priv->status) <<
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08005277 STATUS_IN_SUSPEND |
5278 test_bit(STATUS_EXIT_PENDING, &priv->status) <<
5279 STATUS_EXIT_PENDING;
Zhu Yib481de92007-09-25 17:54:57 -07005280 goto exit;
5281 }
5282
5283 /* ...otherwise clear out all the status bits but the RF Kill and
5284 * SUSPEND bits and continue taking the NIC down. */
5285 priv->status &= test_bit(STATUS_RF_KILL_HW, &priv->status) <<
5286 STATUS_RF_KILL_HW |
5287 test_bit(STATUS_RF_KILL_SW, &priv->status) <<
5288 STATUS_RF_KILL_SW |
Reinette Chatre97888642008-02-06 11:20:38 -08005289 test_bit(STATUS_GEO_CONFIGURED, &priv->status) <<
5290 STATUS_GEO_CONFIGURED |
Zhu Yib481de92007-09-25 17:54:57 -07005291 test_bit(STATUS_IN_SUSPEND, &priv->status) <<
5292 STATUS_IN_SUSPEND |
5293 test_bit(STATUS_FW_ERROR, &priv->status) <<
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08005294 STATUS_FW_ERROR |
5295 test_bit(STATUS_EXIT_PENDING, &priv->status) <<
5296 STATUS_EXIT_PENDING;
Zhu Yib481de92007-09-25 17:54:57 -07005297
5298 spin_lock_irqsave(&priv->lock, flags);
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08005299 iwl_clear_bit(priv, CSR_GP_CNTRL, CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
Zhu Yib481de92007-09-25 17:54:57 -07005300 spin_unlock_irqrestore(&priv->lock, flags);
5301
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005302 iwl3945_hw_txq_ctx_stop(priv);
5303 iwl3945_hw_rxq_stop(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005304
5305 spin_lock_irqsave(&priv->lock, flags);
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08005306 if (!iwl_grab_nic_access(priv)) {
5307 iwl_write_prph(priv, APMG_CLK_DIS_REG,
Zhu Yib481de92007-09-25 17:54:57 -07005308 APMG_CLK_VAL_DMA_CLK_RQT);
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08005309 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005310 }
5311 spin_unlock_irqrestore(&priv->lock, flags);
5312
5313 udelay(5);
5314
Kolekar, Abhijeet01ec6162008-12-19 10:37:38 +08005315 priv->cfg->ops->lib->apm_ops.reset(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005316 exit:
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08005317 memset(&priv->card_alive, 0, sizeof(struct iwl_alive_resp));
Zhu Yib481de92007-09-25 17:54:57 -07005318
5319 if (priv->ibss_beacon)
5320 dev_kfree_skb(priv->ibss_beacon);
5321 priv->ibss_beacon = NULL;
5322
5323 /* clear out any free frames */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005324 iwl3945_clear_free_frames(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005325}
5326
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08005327static void iwl3945_down(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005328{
5329 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005330 __iwl3945_down(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005331 mutex_unlock(&priv->mutex);
Zhu Yib24d22b2007-12-19 13:59:52 +08005332
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005333 iwl3945_cancel_deferred_work(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005334}
5335
5336#define MAX_HW_RESTARTS 5
5337
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08005338static int __iwl3945_up(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005339{
5340 int rc, i;
5341
5342 if (test_bit(STATUS_EXIT_PENDING, &priv->status)) {
Winkler, Tomas39aadf82008-12-19 10:37:32 +08005343 IWL_WARN(priv, "Exit pending; will not bring the NIC up\n");
Zhu Yib481de92007-09-25 17:54:57 -07005344 return -EIO;
5345 }
5346
5347 if (test_bit(STATUS_RF_KILL_SW, &priv->status)) {
Winkler, Tomas39aadf82008-12-19 10:37:32 +08005348 IWL_WARN(priv, "Radio disabled by SW RF kill (module "
Zhu Yib481de92007-09-25 17:54:57 -07005349 "parameter)\n");
Zhu Yie655b9f2008-01-24 02:19:38 -08005350 return -ENODEV;
5351 }
5352
Reinette Chatree903fbd2008-01-30 22:05:15 -08005353 if (!priv->ucode_data_backup.v_addr || !priv->ucode_data.v_addr) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08005354 IWL_ERR(priv, "ucode not available for device bring up\n");
Reinette Chatree903fbd2008-01-30 22:05:15 -08005355 return -EIO;
5356 }
5357
Zhu Yie655b9f2008-01-24 02:19:38 -08005358 /* If platform's RF_KILL switch is NOT set to KILL */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08005359 if (iwl_read32(priv, CSR_GP_CNTRL) &
Zhu Yie655b9f2008-01-24 02:19:38 -08005360 CSR_GP_CNTRL_REG_FLAG_HW_RF_KILL_SW)
5361 clear_bit(STATUS_RF_KILL_HW, &priv->status);
5362 else {
5363 set_bit(STATUS_RF_KILL_HW, &priv->status);
5364 if (!test_bit(STATUS_IN_SUSPEND, &priv->status)) {
Winkler, Tomas39aadf82008-12-19 10:37:32 +08005365 IWL_WARN(priv, "Radio disabled by HW RF Kill switch\n");
Zhu Yie655b9f2008-01-24 02:19:38 -08005366 return -ENODEV;
5367 }
Zhu Yib481de92007-09-25 17:54:57 -07005368 }
Adel Gadllah80fcc9e2008-07-01 17:49:50 +02005369
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08005370 iwl_write32(priv, CSR_INT, 0xFFFFFFFF);
Zhu Yib481de92007-09-25 17:54:57 -07005371
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005372 rc = iwl3945_hw_nic_init(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005373 if (rc) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08005374 IWL_ERR(priv, "Unable to int nic\n");
Zhu Yib481de92007-09-25 17:54:57 -07005375 return rc;
5376 }
5377
5378 /* make sure rfkill handshake bits are cleared */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08005379 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
5380 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR,
Zhu Yib481de92007-09-25 17:54:57 -07005381 CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
5382
5383 /* clear (again), then enable host interrupts */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08005384 iwl_write32(priv, CSR_INT, 0xFFFFFFFF);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005385 iwl3945_enable_interrupts(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005386
5387 /* really make sure rfkill handshake bits are cleared */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08005388 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
5389 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
Zhu Yib481de92007-09-25 17:54:57 -07005390
5391 /* Copy original ucode data image from disk into backup cache.
5392 * This will be used to initialize the on-board processor's
5393 * data SRAM for a clean start when the runtime program first loads. */
5394 memcpy(priv->ucode_data_backup.v_addr, priv->ucode_data.v_addr,
Zhu Yi5a669262008-01-14 17:46:18 -08005395 priv->ucode_data.len);
Zhu Yib481de92007-09-25 17:54:57 -07005396
Zhu Yie655b9f2008-01-24 02:19:38 -08005397 /* We return success when we resume from suspend and rf_kill is on. */
5398 if (test_bit(STATUS_RF_KILL_HW, &priv->status))
5399 return 0;
5400
Zhu Yib481de92007-09-25 17:54:57 -07005401 for (i = 0; i < MAX_HW_RESTARTS; i++) {
5402
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005403 iwl3945_clear_stations_table(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005404
5405 /* load bootstrap state machine,
5406 * load bootstrap program into processor's memory,
5407 * prepare to load the "initialize" uCode */
Kolekar, Abhijeet0164b9b2008-12-19 10:37:37 +08005408 priv->cfg->ops->lib->load_ucode(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005409
5410 if (rc) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08005411 IWL_ERR(priv,
5412 "Unable to set up bootstrap uCode: %d\n", rc);
Zhu Yib481de92007-09-25 17:54:57 -07005413 continue;
5414 }
5415
5416 /* start card; "initialize" will load runtime ucode */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005417 iwl3945_nic_start(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005418
Zhu Yib481de92007-09-25 17:54:57 -07005419 IWL_DEBUG_INFO(DRV_NAME " is coming up\n");
5420
5421 return 0;
5422 }
5423
5424 set_bit(STATUS_EXIT_PENDING, &priv->status);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005425 __iwl3945_down(priv);
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08005426 clear_bit(STATUS_EXIT_PENDING, &priv->status);
Zhu Yib481de92007-09-25 17:54:57 -07005427
5428 /* tried to restart and config the device for as long as our
5429 * patience could withstand */
Winkler, Tomas15b16872008-12-19 10:37:33 +08005430 IWL_ERR(priv, "Unable to initialize device after %d attempts.\n", i);
Zhu Yib481de92007-09-25 17:54:57 -07005431 return -EIO;
5432}
5433
5434
5435/*****************************************************************************
5436 *
5437 * Workqueue callbacks
5438 *
5439 *****************************************************************************/
5440
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005441static void iwl3945_bg_init_alive_start(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07005442{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08005443 struct iwl_priv *priv =
5444 container_of(data, struct iwl_priv, init_alive_start.work);
Zhu Yib481de92007-09-25 17:54:57 -07005445
5446 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
5447 return;
5448
5449 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005450 iwl3945_init_alive_start(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005451 mutex_unlock(&priv->mutex);
5452}
5453
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005454static void iwl3945_bg_alive_start(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07005455{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08005456 struct iwl_priv *priv =
5457 container_of(data, struct iwl_priv, alive_start.work);
Zhu Yib481de92007-09-25 17:54:57 -07005458
5459 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
5460 return;
5461
5462 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005463 iwl3945_alive_start(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005464 mutex_unlock(&priv->mutex);
5465}
5466
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005467static void iwl3945_bg_rf_kill(struct work_struct *work)
Zhu Yib481de92007-09-25 17:54:57 -07005468{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08005469 struct iwl_priv *priv = container_of(work, struct iwl_priv, rf_kill);
Zhu Yib481de92007-09-25 17:54:57 -07005470
5471 wake_up_interruptible(&priv->wait_command_queue);
5472
5473 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
5474 return;
5475
5476 mutex_lock(&priv->mutex);
5477
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08005478 if (!iwl_is_rfkill(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07005479 IWL_DEBUG(IWL_DL_INFO | IWL_DL_RF_KILL,
5480 "HW and/or SW RF Kill no longer active, restarting "
5481 "device\n");
5482 if (!test_bit(STATUS_EXIT_PENDING, &priv->status))
5483 queue_work(priv->workqueue, &priv->restart);
5484 } else {
5485
5486 if (!test_bit(STATUS_RF_KILL_HW, &priv->status))
5487 IWL_DEBUG_RF_KILL("Can not turn radio back on - "
5488 "disabled by SW switch\n");
5489 else
Winkler, Tomas39aadf82008-12-19 10:37:32 +08005490 IWL_WARN(priv, "Radio Frequency Kill Switch is On:\n"
Zhu Yib481de92007-09-25 17:54:57 -07005491 "Kill switch must be turned off for "
5492 "wireless networking to work.\n");
5493 }
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08005494
Zhu Yib481de92007-09-25 17:54:57 -07005495 mutex_unlock(&priv->mutex);
Adel Gadllah80fcc9e2008-07-01 17:49:50 +02005496 iwl3945_rfkill_set_hw_state(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005497}
5498
5499#define IWL_SCAN_CHECK_WATCHDOG (7 * HZ)
5500
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005501static void iwl3945_bg_scan_check(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07005502{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08005503 struct iwl_priv *priv =
5504 container_of(data, struct iwl_priv, scan_check.work);
Zhu Yib481de92007-09-25 17:54:57 -07005505
5506 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
5507 return;
5508
5509 mutex_lock(&priv->mutex);
5510 if (test_bit(STATUS_SCANNING, &priv->status) ||
5511 test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
5512 IWL_DEBUG(IWL_DL_INFO | IWL_DL_SCAN,
5513 "Scan completion watchdog resetting adapter (%dms)\n",
5514 jiffies_to_msecs(IWL_SCAN_CHECK_WATCHDOG));
Mohamed Abbas15e869d2007-10-25 17:15:46 +08005515
Zhu Yib481de92007-09-25 17:54:57 -07005516 if (!test_bit(STATUS_EXIT_PENDING, &priv->status))
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005517 iwl3945_send_scan_abort(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005518 }
5519 mutex_unlock(&priv->mutex);
5520}
5521
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005522static void iwl3945_bg_request_scan(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07005523{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08005524 struct iwl_priv *priv =
5525 container_of(data, struct iwl_priv, request_scan);
Winkler, Tomasc2d79b42008-12-19 10:37:34 +08005526 struct iwl_host_cmd cmd = {
Zhu Yib481de92007-09-25 17:54:57 -07005527 .id = REPLY_SCAN_CMD,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005528 .len = sizeof(struct iwl3945_scan_cmd),
Zhu Yib481de92007-09-25 17:54:57 -07005529 .meta.flags = CMD_SIZE_HUGE,
5530 };
5531 int rc = 0;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005532 struct iwl3945_scan_cmd *scan;
Zhu Yib481de92007-09-25 17:54:57 -07005533 struct ieee80211_conf *conf = NULL;
Abhijeet Kolekarf9340522008-09-03 11:26:58 +08005534 u8 n_probes = 2;
Johannes Berg8318d782008-01-24 19:38:38 +01005535 enum ieee80211_band band;
John W. Linville9387b7c2008-09-30 20:59:05 -04005536 DECLARE_SSID_BUF(ssid);
Zhu Yib481de92007-09-25 17:54:57 -07005537
5538 conf = ieee80211_get_hw_conf(priv->hw);
5539
5540 mutex_lock(&priv->mutex);
5541
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08005542 if (!iwl_is_ready(priv)) {
Winkler, Tomas39aadf82008-12-19 10:37:32 +08005543 IWL_WARN(priv, "request scan called when driver not ready.\n");
Zhu Yib481de92007-09-25 17:54:57 -07005544 goto done;
5545 }
5546
Tomas Winklera96a27f2008-10-23 23:48:56 -07005547 /* Make sure the scan wasn't canceled before this queued work
Zhu Yib481de92007-09-25 17:54:57 -07005548 * was given the chance to run... */
5549 if (!test_bit(STATUS_SCANNING, &priv->status))
5550 goto done;
5551
5552 /* This should never be called or scheduled if there is currently
5553 * a scan active in the hardware. */
5554 if (test_bit(STATUS_SCAN_HW, &priv->status)) {
5555 IWL_DEBUG_INFO("Multiple concurrent scan requests in parallel. "
5556 "Ignoring second request.\n");
5557 rc = -EIO;
5558 goto done;
5559 }
5560
5561 if (test_bit(STATUS_EXIT_PENDING, &priv->status)) {
5562 IWL_DEBUG_SCAN("Aborting scan due to device shutdown\n");
5563 goto done;
5564 }
5565
5566 if (test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
5567 IWL_DEBUG_HC("Scan request while abort pending. Queuing.\n");
5568 goto done;
5569 }
5570
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08005571 if (iwl_is_rfkill(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07005572 IWL_DEBUG_HC("Aborting scan due to RF Kill activation\n");
5573 goto done;
5574 }
5575
5576 if (!test_bit(STATUS_READY, &priv->status)) {
5577 IWL_DEBUG_HC("Scan request while uninitialized. Queuing.\n");
5578 goto done;
5579 }
5580
5581 if (!priv->scan_bands) {
5582 IWL_DEBUG_HC("Aborting scan due to no requested bands\n");
5583 goto done;
5584 }
5585
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08005586 if (!priv->scan39) {
5587 priv->scan39 = kmalloc(sizeof(struct iwl3945_scan_cmd) +
Zhu Yib481de92007-09-25 17:54:57 -07005588 IWL_MAX_SCAN_SIZE, GFP_KERNEL);
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08005589 if (!priv->scan39) {
Zhu Yib481de92007-09-25 17:54:57 -07005590 rc = -ENOMEM;
5591 goto done;
5592 }
5593 }
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08005594 scan = priv->scan39;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005595 memset(scan, 0, sizeof(struct iwl3945_scan_cmd) + IWL_MAX_SCAN_SIZE);
Zhu Yib481de92007-09-25 17:54:57 -07005596
5597 scan->quiet_plcp_th = IWL_PLCP_QUIET_THRESH;
5598 scan->quiet_time = IWL_ACTIVE_QUIET_TIME;
5599
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005600 if (iwl3945_is_associated(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07005601 u16 interval = 0;
5602 u32 extra;
5603 u32 suspend_time = 100;
5604 u32 scan_suspend_time = 100;
5605 unsigned long flags;
5606
5607 IWL_DEBUG_INFO("Scanning while associated...\n");
5608
5609 spin_lock_irqsave(&priv->lock, flags);
5610 interval = priv->beacon_int;
5611 spin_unlock_irqrestore(&priv->lock, flags);
5612
5613 scan->suspend_time = 0;
Mohamed Abbas15e869d2007-10-25 17:15:46 +08005614 scan->max_out_time = cpu_to_le32(200 * 1024);
Zhu Yib481de92007-09-25 17:54:57 -07005615 if (!interval)
5616 interval = suspend_time;
5617 /*
5618 * suspend time format:
5619 * 0-19: beacon interval in usec (time before exec.)
5620 * 20-23: 0
5621 * 24-31: number of beacons (suspend between channels)
5622 */
5623
5624 extra = (suspend_time / interval) << 24;
5625 scan_suspend_time = 0xFF0FFFFF &
5626 (extra | ((suspend_time % interval) * 1024));
5627
5628 scan->suspend_time = cpu_to_le32(scan_suspend_time);
5629 IWL_DEBUG_SCAN("suspend_time 0x%X beacon interval %d\n",
5630 scan_suspend_time, interval);
5631 }
5632
5633 /* We should add the ability for user to lock to PASSIVE ONLY */
5634 if (priv->one_direct_scan) {
5635 IWL_DEBUG_SCAN
5636 ("Kicking off one direct scan for '%s'\n",
John W. Linville9387b7c2008-09-30 20:59:05 -04005637 print_ssid(ssid, priv->direct_ssid,
5638 priv->direct_ssid_len));
Zhu Yib481de92007-09-25 17:54:57 -07005639 scan->direct_scan[0].id = WLAN_EID_SSID;
5640 scan->direct_scan[0].len = priv->direct_ssid_len;
5641 memcpy(scan->direct_scan[0].ssid,
5642 priv->direct_ssid, priv->direct_ssid_len);
Abhijeet Kolekarf9340522008-09-03 11:26:58 +08005643 n_probes++;
Abhijeet Kolekarf9340522008-09-03 11:26:58 +08005644 } else
Bill Moss786b4552008-04-17 16:03:40 -07005645 IWL_DEBUG_SCAN("Kicking off one indirect scan.\n");
Zhu Yib481de92007-09-25 17:54:57 -07005646
5647 /* We don't build a direct scan probe request; the uCode will do
5648 * that based on the direct_mask added to each channel entry */
5649 scan->tx_cmd.len = cpu_to_le16(
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005650 iwl3945_fill_probe_req(priv, (struct ieee80211_mgmt *)scan->data,
Johannes Berg430cfe92008-10-28 18:06:02 +01005651 IWL_MAX_SCAN_SIZE - sizeof(*scan)));
Zhu Yib481de92007-09-25 17:54:57 -07005652 scan->tx_cmd.tx_flags = TX_CMD_FLG_SEQ_CTL_MSK;
Abhijeet Kolekar3832ec92008-12-19 10:37:26 +08005653 scan->tx_cmd.sta_id = priv->hw_params.bcast_sta_id;
Zhu Yib481de92007-09-25 17:54:57 -07005654 scan->tx_cmd.stop_time.life_time = TX_CMD_LIFE_TIME_INFINITE;
5655
5656 /* flags + rate selection */
5657
Ron Rindjunsky66b50042008-06-25 16:46:31 +08005658 if (priv->scan_bands & BIT(IEEE80211_BAND_2GHZ)) {
Zhu Yib481de92007-09-25 17:54:57 -07005659 scan->flags = RXON_FLG_BAND_24G_MSK | RXON_FLG_AUTO_DETECT_MSK;
5660 scan->tx_cmd.rate = IWL_RATE_1M_PLCP;
5661 scan->good_CRC_th = 0;
Johannes Berg8318d782008-01-24 19:38:38 +01005662 band = IEEE80211_BAND_2GHZ;
Ron Rindjunsky66b50042008-06-25 16:46:31 +08005663 } else if (priv->scan_bands & BIT(IEEE80211_BAND_5GHZ)) {
Zhu Yib481de92007-09-25 17:54:57 -07005664 scan->tx_cmd.rate = IWL_RATE_6M_PLCP;
5665 scan->good_CRC_th = IWL_GOOD_CRC_TH;
Johannes Berg8318d782008-01-24 19:38:38 +01005666 band = IEEE80211_BAND_5GHZ;
Ron Rindjunsky66b50042008-06-25 16:46:31 +08005667 } else {
Winkler, Tomas39aadf82008-12-19 10:37:32 +08005668 IWL_WARN(priv, "Invalid scan band count\n");
Zhu Yib481de92007-09-25 17:54:57 -07005669 goto done;
5670 }
5671
5672 /* select Rx antennas */
5673 scan->flags |= iwl3945_get_antenna_flags(priv);
5674
Johannes Berg05c914f2008-09-11 00:01:58 +02005675 if (priv->iw_mode == NL80211_IFTYPE_MONITOR)
Zhu Yib481de92007-09-25 17:54:57 -07005676 scan->filter_flags = RXON_FILTER_PROMISC_MSK;
5677
Abhijeet Kolekarf9340522008-09-03 11:26:58 +08005678 scan->channel_count =
5679 iwl3945_get_channels_for_scan(priv, band, 1, /* active */
5680 n_probes,
5681 (void *)&scan->data[le16_to_cpu(scan->tx_cmd.len)]);
Zhu Yib481de92007-09-25 17:54:57 -07005682
Reinette Chatre14b54332008-11-04 12:21:35 -08005683 if (scan->channel_count == 0) {
5684 IWL_DEBUG_SCAN("channel count %d\n", scan->channel_count);
5685 goto done;
5686 }
5687
Zhu Yib481de92007-09-25 17:54:57 -07005688 cmd.len += le16_to_cpu(scan->tx_cmd.len) +
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005689 scan->channel_count * sizeof(struct iwl3945_scan_channel);
Zhu Yib481de92007-09-25 17:54:57 -07005690 cmd.data = scan;
5691 scan->len = cpu_to_le16(cmd.len);
5692
5693 set_bit(STATUS_SCAN_HW, &priv->status);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005694 rc = iwl3945_send_cmd_sync(priv, &cmd);
Zhu Yib481de92007-09-25 17:54:57 -07005695 if (rc)
5696 goto done;
5697
5698 queue_delayed_work(priv->workqueue, &priv->scan_check,
5699 IWL_SCAN_CHECK_WATCHDOG);
5700
5701 mutex_unlock(&priv->mutex);
5702 return;
5703
5704 done:
Mohamed Abbas2420ebc2008-11-04 12:21:34 -08005705 /* can not perform scan make sure we clear scanning
5706 * bits from status so next scan request can be performed.
5707 * if we dont clear scanning status bit here all next scan
5708 * will fail
5709 */
5710 clear_bit(STATUS_SCAN_HW, &priv->status);
5711 clear_bit(STATUS_SCANNING, &priv->status);
5712
Ian Schram01ebd062007-10-25 17:15:22 +08005713 /* inform mac80211 scan aborted */
Zhu Yib481de92007-09-25 17:54:57 -07005714 queue_work(priv->workqueue, &priv->scan_completed);
5715 mutex_unlock(&priv->mutex);
5716}
5717
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005718static void iwl3945_bg_up(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07005719{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08005720 struct iwl_priv *priv = container_of(data, struct iwl_priv, up);
Zhu Yib481de92007-09-25 17:54:57 -07005721
5722 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
5723 return;
5724
5725 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005726 __iwl3945_up(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005727 mutex_unlock(&priv->mutex);
Adel Gadllah80fcc9e2008-07-01 17:49:50 +02005728 iwl3945_rfkill_set_hw_state(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005729}
5730
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005731static void iwl3945_bg_restart(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07005732{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08005733 struct iwl_priv *priv = container_of(data, struct iwl_priv, restart);
Zhu Yib481de92007-09-25 17:54:57 -07005734
5735 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
5736 return;
5737
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005738 iwl3945_down(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005739 queue_work(priv->workqueue, &priv->up);
5740}
5741
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005742static void iwl3945_bg_rx_replenish(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07005743{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08005744 struct iwl_priv *priv =
5745 container_of(data, struct iwl_priv, rx_replenish);
Zhu Yib481de92007-09-25 17:54:57 -07005746
5747 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
5748 return;
5749
5750 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005751 iwl3945_rx_replenish(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005752 mutex_unlock(&priv->mutex);
5753}
5754
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08005755#define IWL_DELAY_NEXT_SCAN (HZ*2)
5756
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08005757static void iwl3945_post_associate(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005758{
Zhu Yib481de92007-09-25 17:54:57 -07005759 int rc = 0;
5760 struct ieee80211_conf *conf = NULL;
5761
Johannes Berg05c914f2008-09-11 00:01:58 +02005762 if (priv->iw_mode == NL80211_IFTYPE_AP) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08005763 IWL_ERR(priv, "%s Should not be called in AP mode\n", __func__);
Zhu Yib481de92007-09-25 17:54:57 -07005764 return;
5765 }
5766
5767
Johannes Berge1749612008-10-27 15:59:26 -07005768 IWL_DEBUG_ASSOC("Associated as %d to: %pM\n",
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08005769 priv->assoc_id, priv->active39_rxon.bssid_addr);
Zhu Yib481de92007-09-25 17:54:57 -07005770
5771 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
5772 return;
5773
Abhijeet Kolekar322a9812008-09-03 11:26:27 +08005774 if (!priv->vif || !priv->is_open)
Mohamed Abbas6ef89d02007-10-25 17:15:47 +08005775 return;
Abhijeet Kolekar322a9812008-09-03 11:26:27 +08005776
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005777 iwl3945_scan_cancel_timeout(priv, 200);
Mohamed Abbas15e869d2007-10-25 17:15:46 +08005778
Zhu Yib481de92007-09-25 17:54:57 -07005779 conf = ieee80211_get_hw_conf(priv->hw);
5780
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08005781 priv->staging39_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005782 iwl3945_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005783
Tomas Winkler28afaf92008-12-19 10:37:06 +08005784 memset(&priv->rxon_timing, 0, sizeof(struct iwl_rxon_time_cmd));
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005785 iwl3945_setup_rxon_timing(priv);
5786 rc = iwl3945_send_cmd_pdu(priv, REPLY_RXON_TIMING,
Zhu Yib481de92007-09-25 17:54:57 -07005787 sizeof(priv->rxon_timing), &priv->rxon_timing);
5788 if (rc)
Winkler, Tomas39aadf82008-12-19 10:37:32 +08005789 IWL_WARN(priv, "REPLY_RXON_TIMING failed - "
Zhu Yib481de92007-09-25 17:54:57 -07005790 "Attempting to continue.\n");
5791
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08005792 priv->staging39_rxon.filter_flags |= RXON_FILTER_ASSOC_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07005793
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08005794 priv->staging39_rxon.assoc_id = cpu_to_le16(priv->assoc_id);
Zhu Yib481de92007-09-25 17:54:57 -07005795
5796 IWL_DEBUG_ASSOC("assoc id %d beacon interval %d\n",
5797 priv->assoc_id, priv->beacon_int);
5798
5799 if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_PREAMBLE)
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08005800 priv->staging39_rxon.flags |= RXON_FLG_SHORT_PREAMBLE_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07005801 else
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08005802 priv->staging39_rxon.flags &= ~RXON_FLG_SHORT_PREAMBLE_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07005803
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08005804 if (priv->staging39_rxon.flags & RXON_FLG_BAND_24G_MSK) {
Zhu Yib481de92007-09-25 17:54:57 -07005805 if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_SLOT_TIME)
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08005806 priv->staging39_rxon.flags |= RXON_FLG_SHORT_SLOT_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07005807 else
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08005808 priv->staging39_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07005809
Johannes Berg05c914f2008-09-11 00:01:58 +02005810 if (priv->iw_mode == NL80211_IFTYPE_ADHOC)
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08005811 priv->staging39_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07005812
5813 }
5814
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005815 iwl3945_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005816
5817 switch (priv->iw_mode) {
Johannes Berg05c914f2008-09-11 00:01:58 +02005818 case NL80211_IFTYPE_STATION:
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005819 iwl3945_rate_scale_init(priv->hw, IWL_AP_ID);
Zhu Yib481de92007-09-25 17:54:57 -07005820 break;
5821
Johannes Berg05c914f2008-09-11 00:01:58 +02005822 case NL80211_IFTYPE_ADHOC:
Zhu Yib481de92007-09-25 17:54:57 -07005823
Abhijeet Kolekarce546fd2008-11-19 15:32:22 -08005824 priv->assoc_id = 1;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005825 iwl3945_add_station(priv, priv->bssid, 0, 0);
Zhu Yib481de92007-09-25 17:54:57 -07005826 iwl3945_sync_sta(priv, IWL_STA_ID,
Johannes Berg8318d782008-01-24 19:38:38 +01005827 (priv->band == IEEE80211_BAND_5GHZ) ?
Zhu Yib481de92007-09-25 17:54:57 -07005828 IWL_RATE_6M_PLCP : IWL_RATE_1M_PLCP,
5829 CMD_ASYNC);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005830 iwl3945_rate_scale_init(priv->hw, IWL_STA_ID);
5831 iwl3945_send_beacon_cmd(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005832
5833 break;
5834
5835 default:
Winkler, Tomas15b16872008-12-19 10:37:33 +08005836 IWL_ERR(priv, "%s Should not be called in %d mode\n",
Tomas Winkler3ac7f142008-07-21 02:40:14 +03005837 __func__, priv->iw_mode);
Zhu Yib481de92007-09-25 17:54:57 -07005838 break;
5839 }
5840
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005841 iwl3945_activate_qos(priv, 0);
Ron Rindjunsky292ae172008-02-06 11:20:39 -08005842
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08005843 /* we have just associated, don't start scan too early */
5844 priv->next_scan_jiffies = jiffies + IWL_DELAY_NEXT_SCAN;
Abhijeet Kolekarcd56d332008-09-03 11:26:21 +08005845}
5846
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005847static void iwl3945_bg_abort_scan(struct work_struct *work)
Zhu Yib481de92007-09-25 17:54:57 -07005848{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08005849 struct iwl_priv *priv = container_of(work, struct iwl_priv, abort_scan);
Zhu Yib481de92007-09-25 17:54:57 -07005850
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08005851 if (!iwl_is_ready(priv))
Zhu Yib481de92007-09-25 17:54:57 -07005852 return;
5853
5854 mutex_lock(&priv->mutex);
5855
5856 set_bit(STATUS_SCAN_ABORTING, &priv->status);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005857 iwl3945_send_scan_abort(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005858
5859 mutex_unlock(&priv->mutex);
5860}
5861
Johannes Berge8975582008-10-09 12:18:51 +02005862static int iwl3945_mac_config(struct ieee80211_hw *hw, u32 changed);
Zhu Yi76bb77e2007-11-22 10:53:22 +08005863
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005864static void iwl3945_bg_scan_completed(struct work_struct *work)
Zhu Yib481de92007-09-25 17:54:57 -07005865{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08005866 struct iwl_priv *priv =
5867 container_of(work, struct iwl_priv, scan_completed);
Zhu Yib481de92007-09-25 17:54:57 -07005868
5869 IWL_DEBUG(IWL_DL_INFO | IWL_DL_SCAN, "SCAN complete scan\n");
5870
5871 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
5872 return;
5873
Zhu Yia0646472007-12-20 14:10:01 +08005874 if (test_bit(STATUS_CONF_PENDING, &priv->status))
Johannes Berge8975582008-10-09 12:18:51 +02005875 iwl3945_mac_config(priv->hw, 0);
Zhu Yi76bb77e2007-11-22 10:53:22 +08005876
Zhu Yib481de92007-09-25 17:54:57 -07005877 ieee80211_scan_completed(priv->hw);
5878
5879 /* Since setting the TXPOWER may have been deferred while
5880 * performing the scan, fire one off */
5881 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005882 iwl3945_hw_reg_send_txpower(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005883 mutex_unlock(&priv->mutex);
5884}
5885
5886/*****************************************************************************
5887 *
5888 * mac80211 entry point functions
5889 *
5890 *****************************************************************************/
5891
Zhu Yi5a669262008-01-14 17:46:18 -08005892#define UCODE_READY_TIMEOUT (2 * HZ)
5893
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005894static int iwl3945_mac_start(struct ieee80211_hw *hw)
Zhu Yib481de92007-09-25 17:54:57 -07005895{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08005896 struct iwl_priv *priv = hw->priv;
Zhu Yi5a669262008-01-14 17:46:18 -08005897 int ret;
Zhu Yib481de92007-09-25 17:54:57 -07005898
5899 IWL_DEBUG_MAC80211("enter\n");
5900
Zhu Yi5a669262008-01-14 17:46:18 -08005901 if (pci_enable_device(priv->pci_dev)) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08005902 IWL_ERR(priv, "Fail to pci_enable_device\n");
Zhu Yi5a669262008-01-14 17:46:18 -08005903 return -ENODEV;
5904 }
5905 pci_restore_state(priv->pci_dev);
5906 pci_enable_msi(priv->pci_dev);
5907
5908 ret = request_irq(priv->pci_dev->irq, iwl3945_isr, IRQF_SHARED,
5909 DRV_NAME, priv);
5910 if (ret) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08005911 IWL_ERR(priv, "Error allocating IRQ %d\n", priv->pci_dev->irq);
Zhu Yi5a669262008-01-14 17:46:18 -08005912 goto out_disable_msi;
5913 }
5914
Zhu Yib481de92007-09-25 17:54:57 -07005915 /* we should be verifying the device is ready to be opened */
5916 mutex_lock(&priv->mutex);
5917
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08005918 memset(&priv->staging39_rxon, 0, sizeof(struct iwl3945_rxon_cmd));
Zhu Yi5a669262008-01-14 17:46:18 -08005919 /* fetch ucode file from disk, alloc and copy to bus-master buffers ...
5920 * ucode filename and max sizes are card-specific. */
5921
5922 if (!priv->ucode_code.len) {
5923 ret = iwl3945_read_ucode(priv);
5924 if (ret) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08005925 IWL_ERR(priv, "Could not read microcode: %d\n", ret);
Zhu Yi5a669262008-01-14 17:46:18 -08005926 mutex_unlock(&priv->mutex);
5927 goto out_release_irq;
5928 }
5929 }
5930
Zhu Yie655b9f2008-01-24 02:19:38 -08005931 ret = __iwl3945_up(priv);
Zhu Yi5a669262008-01-14 17:46:18 -08005932
Zhu Yib481de92007-09-25 17:54:57 -07005933 mutex_unlock(&priv->mutex);
Zhu Yi5a669262008-01-14 17:46:18 -08005934
Adel Gadllah80fcc9e2008-07-01 17:49:50 +02005935 iwl3945_rfkill_set_hw_state(priv);
5936
Zhu Yie655b9f2008-01-24 02:19:38 -08005937 if (ret)
5938 goto out_release_irq;
5939
5940 IWL_DEBUG_INFO("Start UP work.\n");
5941
5942 if (test_bit(STATUS_IN_SUSPEND, &priv->status))
5943 return 0;
5944
Zhu Yi5a669262008-01-14 17:46:18 -08005945 /* Wait for START_ALIVE from ucode. Otherwise callbacks from
5946 * mac80211 will not be run successfully. */
5947 ret = wait_event_interruptible_timeout(priv->wait_command_queue,
5948 test_bit(STATUS_READY, &priv->status),
5949 UCODE_READY_TIMEOUT);
5950 if (!ret) {
5951 if (!test_bit(STATUS_READY, &priv->status)) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08005952 IWL_ERR(priv,
5953 "Wait for START_ALIVE timeout after %dms.\n",
5954 jiffies_to_msecs(UCODE_READY_TIMEOUT));
Zhu Yi5a669262008-01-14 17:46:18 -08005955 ret = -ETIMEDOUT;
5956 goto out_release_irq;
5957 }
5958 }
5959
Zhu Yie655b9f2008-01-24 02:19:38 -08005960 priv->is_open = 1;
Zhu Yib481de92007-09-25 17:54:57 -07005961 IWL_DEBUG_MAC80211("leave\n");
5962 return 0;
Zhu Yi5a669262008-01-14 17:46:18 -08005963
5964out_release_irq:
5965 free_irq(priv->pci_dev->irq, priv);
5966out_disable_msi:
5967 pci_disable_msi(priv->pci_dev);
Zhu Yie655b9f2008-01-24 02:19:38 -08005968 pci_disable_device(priv->pci_dev);
5969 priv->is_open = 0;
5970 IWL_DEBUG_MAC80211("leave - failed\n");
Zhu Yi5a669262008-01-14 17:46:18 -08005971 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07005972}
5973
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005974static void iwl3945_mac_stop(struct ieee80211_hw *hw)
Zhu Yib481de92007-09-25 17:54:57 -07005975{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08005976 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07005977
5978 IWL_DEBUG_MAC80211("enter\n");
Mohamed Abbas6ef89d02007-10-25 17:15:47 +08005979
Zhu Yie655b9f2008-01-24 02:19:38 -08005980 if (!priv->is_open) {
5981 IWL_DEBUG_MAC80211("leave - skip\n");
5982 return;
5983 }
5984
Zhu Yib481de92007-09-25 17:54:57 -07005985 priv->is_open = 0;
Zhu Yi5a669262008-01-14 17:46:18 -08005986
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08005987 if (iwl_is_ready_rf(priv)) {
Zhu Yie655b9f2008-01-24 02:19:38 -08005988 /* stop mac, cancel any scan request and clear
5989 * RXON_FILTER_ASSOC_MSK BIT
5990 */
Zhu Yi5a669262008-01-14 17:46:18 -08005991 mutex_lock(&priv->mutex);
5992 iwl3945_scan_cancel_timeout(priv, 100);
Mohamed Abbasfde35712007-11-29 11:10:15 +08005993 mutex_unlock(&priv->mutex);
Mohamed Abbasfde35712007-11-29 11:10:15 +08005994 }
5995
Zhu Yi5a669262008-01-14 17:46:18 -08005996 iwl3945_down(priv);
5997
5998 flush_workqueue(priv->workqueue);
5999 free_irq(priv->pci_dev->irq, priv);
6000 pci_disable_msi(priv->pci_dev);
6001 pci_save_state(priv->pci_dev);
6002 pci_disable_device(priv->pci_dev);
Mohamed Abbas6ef89d02007-10-25 17:15:47 +08006003
Zhu Yib481de92007-09-25 17:54:57 -07006004 IWL_DEBUG_MAC80211("leave\n");
Zhu Yib481de92007-09-25 17:54:57 -07006005}
6006
Johannes Berge039fa42008-05-15 12:55:29 +02006007static int iwl3945_mac_tx(struct ieee80211_hw *hw, struct sk_buff *skb)
Zhu Yib481de92007-09-25 17:54:57 -07006008{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08006009 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07006010
6011 IWL_DEBUG_MAC80211("enter\n");
6012
Zhu Yib481de92007-09-25 17:54:57 -07006013 IWL_DEBUG_TX("dev->xmit(%d bytes) at rate 0x%02x\n", skb->len,
Johannes Berge039fa42008-05-15 12:55:29 +02006014 ieee80211_get_tx_rate(hw, IEEE80211_SKB_CB(skb))->bitrate);
Zhu Yib481de92007-09-25 17:54:57 -07006015
Johannes Berge039fa42008-05-15 12:55:29 +02006016 if (iwl3945_tx_skb(priv, skb))
Zhu Yib481de92007-09-25 17:54:57 -07006017 dev_kfree_skb_any(skb);
6018
6019 IWL_DEBUG_MAC80211("leave\n");
Reinette Chatre637f8832009-01-19 15:30:32 -08006020 return NETDEV_TX_OK;
Zhu Yib481de92007-09-25 17:54:57 -07006021}
6022
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006023static int iwl3945_mac_add_interface(struct ieee80211_hw *hw,
Zhu Yib481de92007-09-25 17:54:57 -07006024 struct ieee80211_if_init_conf *conf)
6025{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08006026 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07006027 unsigned long flags;
6028
Johannes Berg32bfd352007-12-19 01:31:26 +01006029 IWL_DEBUG_MAC80211("enter: type %d\n", conf->type);
Zhu Yib481de92007-09-25 17:54:57 -07006030
Johannes Berg32bfd352007-12-19 01:31:26 +01006031 if (priv->vif) {
6032 IWL_DEBUG_MAC80211("leave - vif != NULL\n");
Tomas Winkler864792e2007-11-27 21:00:52 +02006033 return -EOPNOTSUPP;
Zhu Yib481de92007-09-25 17:54:57 -07006034 }
6035
6036 spin_lock_irqsave(&priv->lock, flags);
Johannes Berg32bfd352007-12-19 01:31:26 +01006037 priv->vif = conf->vif;
Zhu, Yi60294de2008-10-29 14:05:45 -07006038 priv->iw_mode = conf->type;
Zhu Yib481de92007-09-25 17:54:57 -07006039
6040 spin_unlock_irqrestore(&priv->lock, flags);
6041
6042 mutex_lock(&priv->mutex);
Tomas Winkler864792e2007-11-27 21:00:52 +02006043
6044 if (conf->mac_addr) {
Johannes Berge1749612008-10-27 15:59:26 -07006045 IWL_DEBUG_MAC80211("Set: %pM\n", conf->mac_addr);
Tomas Winkler864792e2007-11-27 21:00:52 +02006046 memcpy(priv->mac_addr, conf->mac_addr, ETH_ALEN);
6047 }
6048
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08006049 if (iwl_is_ready(priv))
Zhu Yi5a669262008-01-14 17:46:18 -08006050 iwl3945_set_mode(priv, conf->type);
Zhu Yib481de92007-09-25 17:54:57 -07006051
Zhu Yib481de92007-09-25 17:54:57 -07006052 mutex_unlock(&priv->mutex);
6053
Zhu Yi5a669262008-01-14 17:46:18 -08006054 IWL_DEBUG_MAC80211("leave\n");
Zhu Yib481de92007-09-25 17:54:57 -07006055 return 0;
6056}
6057
6058/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006059 * iwl3945_mac_config - mac80211 config callback
Zhu Yib481de92007-09-25 17:54:57 -07006060 *
6061 * We ignore conf->flags & IEEE80211_CONF_SHORT_SLOT_TIME since it seems to
6062 * be set inappropriately and the driver currently sets the hardware up to
6063 * use it whenever needed.
6064 */
Johannes Berge8975582008-10-09 12:18:51 +02006065static int iwl3945_mac_config(struct ieee80211_hw *hw, u32 changed)
Zhu Yib481de92007-09-25 17:54:57 -07006066{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08006067 struct iwl_priv *priv = hw->priv;
Samuel Ortizd20b3c62008-12-19 10:37:15 +08006068 const struct iwl_channel_info *ch_info;
Johannes Berge8975582008-10-09 12:18:51 +02006069 struct ieee80211_conf *conf = &hw->conf;
Zhu Yib481de92007-09-25 17:54:57 -07006070 unsigned long flags;
Zhu Yi76bb77e2007-11-22 10:53:22 +08006071 int ret = 0;
Zhu Yib481de92007-09-25 17:54:57 -07006072
6073 mutex_lock(&priv->mutex);
Johannes Berg8318d782008-01-24 19:38:38 +01006074 IWL_DEBUG_MAC80211("enter to channel %d\n", conf->channel->hw_value);
Zhu Yib481de92007-09-25 17:54:57 -07006075
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08006076 if (!iwl_is_ready(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07006077 IWL_DEBUG_MAC80211("leave - not ready\n");
Zhu Yi76bb77e2007-11-22 10:53:22 +08006078 ret = -EIO;
6079 goto out;
Zhu Yib481de92007-09-25 17:54:57 -07006080 }
6081
Kolekar, Abhijeetdf878d82008-12-19 10:37:35 +08006082 if (unlikely(!iwl3945_mod_params.disable_hw_scan &&
Zhu Yib481de92007-09-25 17:54:57 -07006083 test_bit(STATUS_SCANNING, &priv->status))) {
Zhu Yia0646472007-12-20 14:10:01 +08006084 IWL_DEBUG_MAC80211("leave - scanning\n");
6085 set_bit(STATUS_CONF_PENDING, &priv->status);
Zhu Yib481de92007-09-25 17:54:57 -07006086 mutex_unlock(&priv->mutex);
Zhu Yia0646472007-12-20 14:10:01 +08006087 return 0;
Zhu Yib481de92007-09-25 17:54:57 -07006088 }
6089
6090 spin_lock_irqsave(&priv->lock, flags);
6091
Johannes Berg8318d782008-01-24 19:38:38 +01006092 ch_info = iwl3945_get_channel_info(priv, conf->channel->band,
6093 conf->channel->hw_value);
Zhu Yib481de92007-09-25 17:54:57 -07006094 if (!is_channel_valid(ch_info)) {
Ron Rindjunsky66b50042008-06-25 16:46:31 +08006095 IWL_DEBUG_SCAN("Channel %d [%d] is INVALID for this band.\n",
Johannes Berg8318d782008-01-24 19:38:38 +01006096 conf->channel->hw_value, conf->channel->band);
Zhu Yib481de92007-09-25 17:54:57 -07006097 IWL_DEBUG_MAC80211("leave - invalid channel\n");
6098 spin_unlock_irqrestore(&priv->lock, flags);
Zhu Yi76bb77e2007-11-22 10:53:22 +08006099 ret = -EINVAL;
6100 goto out;
Zhu Yib481de92007-09-25 17:54:57 -07006101 }
6102
Johannes Berg8318d782008-01-24 19:38:38 +01006103 iwl3945_set_rxon_channel(priv, conf->channel->band, conf->channel->hw_value);
Zhu Yib481de92007-09-25 17:54:57 -07006104
Johannes Berg8318d782008-01-24 19:38:38 +01006105 iwl3945_set_flags_for_phymode(priv, conf->channel->band);
Zhu Yib481de92007-09-25 17:54:57 -07006106
6107 /* The list of supported rates and rate mask can be different
6108 * for each phymode; since the phymode may have changed, reset
6109 * the rate mask to what mac80211 lists */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006110 iwl3945_set_rate(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006111
6112 spin_unlock_irqrestore(&priv->lock, flags);
6113
6114#ifdef IEEE80211_CONF_CHANNEL_SWITCH
6115 if (conf->flags & IEEE80211_CONF_CHANNEL_SWITCH) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006116 iwl3945_hw_channel_switch(priv, conf->channel);
Zhu Yi76bb77e2007-11-22 10:53:22 +08006117 goto out;
Zhu Yib481de92007-09-25 17:54:57 -07006118 }
6119#endif
6120
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006121 iwl3945_radio_kill_sw(priv, !conf->radio_enabled);
Zhu Yib481de92007-09-25 17:54:57 -07006122
6123 if (!conf->radio_enabled) {
6124 IWL_DEBUG_MAC80211("leave - radio disabled\n");
Zhu Yi76bb77e2007-11-22 10:53:22 +08006125 goto out;
Zhu Yib481de92007-09-25 17:54:57 -07006126 }
6127
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08006128 if (iwl_is_rfkill(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07006129 IWL_DEBUG_MAC80211("leave - RF kill\n");
Zhu Yi76bb77e2007-11-22 10:53:22 +08006130 ret = -EIO;
6131 goto out;
Zhu Yib481de92007-09-25 17:54:57 -07006132 }
6133
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006134 iwl3945_set_rate(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006135
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08006136 if (memcmp(&priv->active39_rxon,
6137 &priv->staging39_rxon, sizeof(priv->staging39_rxon)))
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006138 iwl3945_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006139 else
6140 IWL_DEBUG_INFO("No re-sending same RXON configuration.\n");
6141
6142 IWL_DEBUG_MAC80211("leave\n");
6143
Zhu Yi76bb77e2007-11-22 10:53:22 +08006144out:
Zhu Yia0646472007-12-20 14:10:01 +08006145 clear_bit(STATUS_CONF_PENDING, &priv->status);
Zhu Yib481de92007-09-25 17:54:57 -07006146 mutex_unlock(&priv->mutex);
Zhu Yi76bb77e2007-11-22 10:53:22 +08006147 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07006148}
6149
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08006150static void iwl3945_config_ap(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07006151{
6152 int rc = 0;
6153
Maarten Lankhorstd986bcd2008-01-23 10:15:16 -08006154 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
Zhu Yib481de92007-09-25 17:54:57 -07006155 return;
6156
6157 /* The following should be done only at AP bring up */
Ron Rindjunsky5d1e2322008-06-30 17:23:04 +08006158 if (!(iwl3945_is_associated(priv))) {
Zhu Yib481de92007-09-25 17:54:57 -07006159
6160 /* RXON - unassoc (to set timing command) */
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08006161 priv->staging39_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006162 iwl3945_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006163
6164 /* RXON Timing */
Tomas Winkler28afaf92008-12-19 10:37:06 +08006165 memset(&priv->rxon_timing, 0, sizeof(struct iwl_rxon_time_cmd));
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006166 iwl3945_setup_rxon_timing(priv);
6167 rc = iwl3945_send_cmd_pdu(priv, REPLY_RXON_TIMING,
Zhu Yib481de92007-09-25 17:54:57 -07006168 sizeof(priv->rxon_timing), &priv->rxon_timing);
6169 if (rc)
Winkler, Tomas39aadf82008-12-19 10:37:32 +08006170 IWL_WARN(priv, "REPLY_RXON_TIMING failed - "
Zhu Yib481de92007-09-25 17:54:57 -07006171 "Attempting to continue.\n");
6172
6173 /* FIXME: what should be the assoc_id for AP? */
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08006174 priv->staging39_rxon.assoc_id = cpu_to_le16(priv->assoc_id);
Zhu Yib481de92007-09-25 17:54:57 -07006175 if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_PREAMBLE)
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08006176 priv->staging39_rxon.flags |=
Zhu Yib481de92007-09-25 17:54:57 -07006177 RXON_FLG_SHORT_PREAMBLE_MSK;
6178 else
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08006179 priv->staging39_rxon.flags &=
Zhu Yib481de92007-09-25 17:54:57 -07006180 ~RXON_FLG_SHORT_PREAMBLE_MSK;
6181
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08006182 if (priv->staging39_rxon.flags & RXON_FLG_BAND_24G_MSK) {
Zhu Yib481de92007-09-25 17:54:57 -07006183 if (priv->assoc_capability &
6184 WLAN_CAPABILITY_SHORT_SLOT_TIME)
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08006185 priv->staging39_rxon.flags |=
Zhu Yib481de92007-09-25 17:54:57 -07006186 RXON_FLG_SHORT_SLOT_MSK;
6187 else
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08006188 priv->staging39_rxon.flags &=
Zhu Yib481de92007-09-25 17:54:57 -07006189 ~RXON_FLG_SHORT_SLOT_MSK;
6190
Johannes Berg05c914f2008-09-11 00:01:58 +02006191 if (priv->iw_mode == NL80211_IFTYPE_ADHOC)
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08006192 priv->staging39_rxon.flags &=
Zhu Yib481de92007-09-25 17:54:57 -07006193 ~RXON_FLG_SHORT_SLOT_MSK;
6194 }
6195 /* restore RXON assoc */
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08006196 priv->staging39_rxon.filter_flags |= RXON_FILTER_ASSOC_MSK;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006197 iwl3945_commit_rxon(priv);
Kolekar, Abhijeetb5323d32008-12-19 10:37:22 +08006198 iwl3945_add_station(priv, iwl_bcast_addr, 0, 0);
Zhu Yi556f8db2007-09-27 11:27:33 +08006199 }
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006200 iwl3945_send_beacon_cmd(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006201
6202 /* FIXME - we need to add code here to detect a totally new
6203 * configuration, reset the AP, unassoc, rxon timing, assoc,
6204 * clear sta table, add BCAST sta... */
6205}
6206
Johannes Berg32bfd352007-12-19 01:31:26 +01006207static int iwl3945_mac_config_interface(struct ieee80211_hw *hw,
6208 struct ieee80211_vif *vif,
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08006209 struct ieee80211_if_conf *conf)
Zhu Yib481de92007-09-25 17:54:57 -07006210{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08006211 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07006212 int rc;
6213
6214 if (conf == NULL)
6215 return -EIO;
6216
Emmanuel Grumbachb716bb92008-03-04 18:09:32 -08006217 if (priv->vif != vif) {
6218 IWL_DEBUG_MAC80211("leave - priv->vif != vif\n");
Emmanuel Grumbachb716bb92008-03-04 18:09:32 -08006219 return 0;
6220 }
6221
Johannes Berg9d139c82008-07-09 14:40:37 +02006222 /* handle this temporarily here */
Johannes Berg05c914f2008-09-11 00:01:58 +02006223 if (priv->iw_mode == NL80211_IFTYPE_ADHOC &&
Johannes Berg9d139c82008-07-09 14:40:37 +02006224 conf->changed & IEEE80211_IFCC_BEACON) {
6225 struct sk_buff *beacon = ieee80211_beacon_get(hw, vif);
6226 if (!beacon)
6227 return -ENOMEM;
Mohamed Abbas9bdf5ec2008-11-07 09:58:35 -08006228 mutex_lock(&priv->mutex);
Johannes Berg9d139c82008-07-09 14:40:37 +02006229 rc = iwl3945_mac_beacon_update(hw, beacon);
Mohamed Abbas9bdf5ec2008-11-07 09:58:35 -08006230 mutex_unlock(&priv->mutex);
Johannes Berg9d139c82008-07-09 14:40:37 +02006231 if (rc)
6232 return rc;
6233 }
6234
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08006235 if (!iwl_is_alive(priv))
Zhu Yi5a669262008-01-14 17:46:18 -08006236 return -EAGAIN;
6237
Zhu Yib481de92007-09-25 17:54:57 -07006238 mutex_lock(&priv->mutex);
6239
Zhu Yib481de92007-09-25 17:54:57 -07006240 if (conf->bssid)
Johannes Berge1749612008-10-27 15:59:26 -07006241 IWL_DEBUG_MAC80211("bssid: %pM\n", conf->bssid);
Zhu Yib481de92007-09-25 17:54:57 -07006242
Johannes Berg4150c572007-09-17 01:29:23 -04006243/*
6244 * very dubious code was here; the probe filtering flag is never set:
6245 *
Zhu Yib481de92007-09-25 17:54:57 -07006246 if (unlikely(test_bit(STATUS_SCANNING, &priv->status)) &&
6247 !(priv->hw->flags & IEEE80211_HW_NO_PROBE_FILTERING)) {
Johannes Berg4150c572007-09-17 01:29:23 -04006248 */
Zhu Yib481de92007-09-25 17:54:57 -07006249
Johannes Berg05c914f2008-09-11 00:01:58 +02006250 if (priv->iw_mode == NL80211_IFTYPE_AP) {
Zhu Yib481de92007-09-25 17:54:57 -07006251 if (!conf->bssid) {
6252 conf->bssid = priv->mac_addr;
6253 memcpy(priv->bssid, priv->mac_addr, ETH_ALEN);
Johannes Berge1749612008-10-27 15:59:26 -07006254 IWL_DEBUG_MAC80211("bssid was set to: %pM\n",
6255 conf->bssid);
Zhu Yib481de92007-09-25 17:54:57 -07006256 }
6257 if (priv->ibss_beacon)
6258 dev_kfree_skb(priv->ibss_beacon);
6259
Johannes Berg9d139c82008-07-09 14:40:37 +02006260 priv->ibss_beacon = ieee80211_beacon_get(hw, vif);
Zhu Yib481de92007-09-25 17:54:57 -07006261 }
6262
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08006263 if (iwl_is_rfkill(priv))
Mohamed Abbasfde35712007-11-29 11:10:15 +08006264 goto done;
6265
Zhu Yib481de92007-09-25 17:54:57 -07006266 if (conf->bssid && !is_zero_ether_addr(conf->bssid) &&
6267 !is_multicast_ether_addr(conf->bssid)) {
6268 /* If there is currently a HW scan going on in the background
6269 * then we need to cancel it else the RXON below will fail. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006270 if (iwl3945_scan_cancel_timeout(priv, 100)) {
Winkler, Tomas39aadf82008-12-19 10:37:32 +08006271 IWL_WARN(priv, "Aborted scan still in progress "
Zhu Yib481de92007-09-25 17:54:57 -07006272 "after 100ms\n");
6273 IWL_DEBUG_MAC80211("leaving - scan abort failed.\n");
6274 mutex_unlock(&priv->mutex);
6275 return -EAGAIN;
6276 }
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08006277 memcpy(priv->staging39_rxon.bssid_addr, conf->bssid, ETH_ALEN);
Zhu Yib481de92007-09-25 17:54:57 -07006278
6279 /* TODO: Audit driver for usage of these members and see
6280 * if mac80211 deprecates them (priv->bssid looks like it
6281 * shouldn't be there, but I haven't scanned the IBSS code
6282 * to verify) - jpk */
6283 memcpy(priv->bssid, conf->bssid, ETH_ALEN);
6284
Johannes Berg05c914f2008-09-11 00:01:58 +02006285 if (priv->iw_mode == NL80211_IFTYPE_AP)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006286 iwl3945_config_ap(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006287 else {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006288 rc = iwl3945_commit_rxon(priv);
Johannes Berg05c914f2008-09-11 00:01:58 +02006289 if ((priv->iw_mode == NL80211_IFTYPE_STATION) && rc)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006290 iwl3945_add_station(priv,
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08006291 priv->active39_rxon.bssid_addr, 1, 0);
Zhu Yib481de92007-09-25 17:54:57 -07006292 }
6293
6294 } else {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006295 iwl3945_scan_cancel_timeout(priv, 100);
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08006296 priv->staging39_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006297 iwl3945_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006298 }
6299
Mohamed Abbasfde35712007-11-29 11:10:15 +08006300 done:
Zhu Yib481de92007-09-25 17:54:57 -07006301 IWL_DEBUG_MAC80211("leave\n");
6302 mutex_unlock(&priv->mutex);
6303
6304 return 0;
6305}
6306
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006307static void iwl3945_configure_filter(struct ieee80211_hw *hw,
Johannes Berg4150c572007-09-17 01:29:23 -04006308 unsigned int changed_flags,
6309 unsigned int *total_flags,
6310 int mc_count, struct dev_addr_list *mc_list)
6311{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08006312 struct iwl_priv *priv = hw->priv;
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08006313 __le32 *filter_flags = &priv->staging39_rxon.filter_flags;
Rick Farrington25b3f572008-06-30 17:23:28 +08006314
Zhu, Yi352bc8d2008-11-12 13:14:09 -08006315 IWL_DEBUG_MAC80211("Enter: changed: 0x%x, total: 0x%x\n",
6316 changed_flags, *total_flags);
6317
6318 if (changed_flags & (FIF_OTHER_BSS | FIF_PROMISC_IN_BSS)) {
6319 if (*total_flags & (FIF_OTHER_BSS | FIF_PROMISC_IN_BSS))
6320 *filter_flags |= RXON_FILTER_PROMISC_MSK;
6321 else
6322 *filter_flags &= ~RXON_FILTER_PROMISC_MSK;
Abhijeet Kolekar5ec03972008-05-05 10:22:48 +08006323 }
Zhu, Yi352bc8d2008-11-12 13:14:09 -08006324 if (changed_flags & FIF_ALLMULTI) {
6325 if (*total_flags & FIF_ALLMULTI)
6326 *filter_flags |= RXON_FILTER_ACCEPT_GRP_MSK;
6327 else
6328 *filter_flags &= ~RXON_FILTER_ACCEPT_GRP_MSK;
6329 }
6330 if (changed_flags & FIF_CONTROL) {
6331 if (*total_flags & FIF_CONTROL)
6332 *filter_flags |= RXON_FILTER_CTL2HOST_MSK;
6333 else
6334 *filter_flags &= ~RXON_FILTER_CTL2HOST_MSK;
6335 }
6336 if (changed_flags & FIF_BCN_PRBRESP_PROMISC) {
6337 if (*total_flags & FIF_BCN_PRBRESP_PROMISC)
6338 *filter_flags |= RXON_FILTER_BCON_AWARE_MSK;
6339 else
6340 *filter_flags &= ~RXON_FILTER_BCON_AWARE_MSK;
6341 }
6342
6343 /* We avoid iwl_commit_rxon here to commit the new filter flags
6344 * since mac80211 will call ieee80211_hw_config immediately.
6345 * (mc_list is not supported at this time). Otherwise, we need to
6346 * queue a background iwl_commit_rxon work.
6347 */
6348
6349 *total_flags &= FIF_OTHER_BSS | FIF_ALLMULTI | FIF_PROMISC_IN_BSS |
Rick Farrington25b3f572008-06-30 17:23:28 +08006350 FIF_BCN_PRBRESP_PROMISC | FIF_CONTROL;
Johannes Berg4150c572007-09-17 01:29:23 -04006351}
6352
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006353static void iwl3945_mac_remove_interface(struct ieee80211_hw *hw,
Zhu Yib481de92007-09-25 17:54:57 -07006354 struct ieee80211_if_init_conf *conf)
6355{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08006356 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07006357
6358 IWL_DEBUG_MAC80211("enter\n");
6359
6360 mutex_lock(&priv->mutex);
Mohamed Abbas6ef89d02007-10-25 17:15:47 +08006361
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08006362 if (iwl_is_ready_rf(priv)) {
Mohamed Abbasfde35712007-11-29 11:10:15 +08006363 iwl3945_scan_cancel_timeout(priv, 100);
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08006364 priv->staging39_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
Mohamed Abbasfde35712007-11-29 11:10:15 +08006365 iwl3945_commit_rxon(priv);
6366 }
Johannes Berg32bfd352007-12-19 01:31:26 +01006367 if (priv->vif == conf->vif) {
6368 priv->vif = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07006369 memset(priv->bssid, 0, ETH_ALEN);
Zhu Yib481de92007-09-25 17:54:57 -07006370 }
6371 mutex_unlock(&priv->mutex);
6372
6373 IWL_DEBUG_MAC80211("leave\n");
Zhu Yib481de92007-09-25 17:54:57 -07006374}
6375
Abhijeet Kolekarcd56d332008-09-03 11:26:21 +08006376#define IWL_DELAY_NEXT_SCAN_AFTER_ASSOC (HZ*6)
6377
6378static void iwl3945_bss_info_changed(struct ieee80211_hw *hw,
6379 struct ieee80211_vif *vif,
6380 struct ieee80211_bss_conf *bss_conf,
6381 u32 changes)
6382{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08006383 struct iwl_priv *priv = hw->priv;
Abhijeet Kolekarcd56d332008-09-03 11:26:21 +08006384
6385 IWL_DEBUG_MAC80211("changes = 0x%X\n", changes);
6386
6387 if (changes & BSS_CHANGED_ERP_PREAMBLE) {
6388 IWL_DEBUG_MAC80211("ERP_PREAMBLE %d\n",
6389 bss_conf->use_short_preamble);
6390 if (bss_conf->use_short_preamble)
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08006391 priv->staging39_rxon.flags |= RXON_FLG_SHORT_PREAMBLE_MSK;
Abhijeet Kolekarcd56d332008-09-03 11:26:21 +08006392 else
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08006393 priv->staging39_rxon.flags &= ~RXON_FLG_SHORT_PREAMBLE_MSK;
Abhijeet Kolekarcd56d332008-09-03 11:26:21 +08006394 }
6395
6396 if (changes & BSS_CHANGED_ERP_CTS_PROT) {
6397 IWL_DEBUG_MAC80211("ERP_CTS %d\n", bss_conf->use_cts_prot);
6398 if (bss_conf->use_cts_prot && (priv->band != IEEE80211_BAND_5GHZ))
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08006399 priv->staging39_rxon.flags |= RXON_FLG_TGG_PROTECT_MSK;
Abhijeet Kolekarcd56d332008-09-03 11:26:21 +08006400 else
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08006401 priv->staging39_rxon.flags &= ~RXON_FLG_TGG_PROTECT_MSK;
Abhijeet Kolekarcd56d332008-09-03 11:26:21 +08006402 }
6403
6404 if (changes & BSS_CHANGED_ASSOC) {
6405 IWL_DEBUG_MAC80211("ASSOC %d\n", bss_conf->assoc);
6406 /* This should never happen as this function should
6407 * never be called from interrupt context. */
6408 if (WARN_ON_ONCE(in_interrupt()))
6409 return;
6410 if (bss_conf->assoc) {
6411 priv->assoc_id = bss_conf->aid;
6412 priv->beacon_int = bss_conf->beacon_int;
Tomas Winkler28afaf92008-12-19 10:37:06 +08006413 priv->timestamp = bss_conf->timestamp;
Abhijeet Kolekarcd56d332008-09-03 11:26:21 +08006414 priv->assoc_capability = bss_conf->assoc_capability;
6415 priv->next_scan_jiffies = jiffies +
6416 IWL_DELAY_NEXT_SCAN_AFTER_ASSOC;
6417 mutex_lock(&priv->mutex);
6418 iwl3945_post_associate(priv);
6419 mutex_unlock(&priv->mutex);
6420 } else {
6421 priv->assoc_id = 0;
6422 IWL_DEBUG_MAC80211("DISASSOC %d\n", bss_conf->assoc);
6423 }
6424 } else if (changes && iwl3945_is_associated(priv) && priv->assoc_id) {
6425 IWL_DEBUG_MAC80211("Associated Changes %d\n", changes);
6426 iwl3945_send_rxon_assoc(priv);
6427 }
6428
6429}
6430
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006431static int iwl3945_mac_hw_scan(struct ieee80211_hw *hw, u8 *ssid, size_t len)
Zhu Yib481de92007-09-25 17:54:57 -07006432{
6433 int rc = 0;
6434 unsigned long flags;
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08006435 struct iwl_priv *priv = hw->priv;
John W. Linville9387b7c2008-09-30 20:59:05 -04006436 DECLARE_SSID_BUF(ssid_buf);
Zhu Yib481de92007-09-25 17:54:57 -07006437
6438 IWL_DEBUG_MAC80211("enter\n");
6439
Mohamed Abbas15e869d2007-10-25 17:15:46 +08006440 mutex_lock(&priv->mutex);
Zhu Yib481de92007-09-25 17:54:57 -07006441 spin_lock_irqsave(&priv->lock, flags);
6442
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08006443 if (!iwl_is_ready_rf(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07006444 rc = -EIO;
6445 IWL_DEBUG_MAC80211("leave - not ready or exit pending\n");
6446 goto out_unlock;
6447 }
6448
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08006449 /* we don't schedule scan within next_scan_jiffies period */
6450 if (priv->next_scan_jiffies &&
6451 time_after(priv->next_scan_jiffies, jiffies)) {
6452 rc = -EAGAIN;
6453 goto out_unlock;
6454 }
Bill Moss15dbf1b2008-05-06 11:05:15 +08006455 /* if we just finished scan ask for delay for a broadcast scan */
6456 if ((len == 0) && priv->last_scan_jiffies &&
6457 time_after(priv->last_scan_jiffies + IWL_DELAY_NEXT_SCAN,
6458 jiffies)) {
Zhu Yib481de92007-09-25 17:54:57 -07006459 rc = -EAGAIN;
6460 goto out_unlock;
6461 }
6462 if (len) {
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08006463 IWL_DEBUG_SCAN("direct scan for %s [%d]\n ",
John W. Linville9387b7c2008-09-30 20:59:05 -04006464 print_ssid(ssid_buf, ssid, len), (int)len);
Zhu Yib481de92007-09-25 17:54:57 -07006465
6466 priv->one_direct_scan = 1;
6467 priv->direct_ssid_len = (u8)
6468 min((u8) len, (u8) IW_ESSID_MAX_SIZE);
6469 memcpy(priv->direct_ssid, ssid, priv->direct_ssid_len);
Mohamed Abbas6ef89d02007-10-25 17:15:47 +08006470 } else
6471 priv->one_direct_scan = 0;
Zhu Yib481de92007-09-25 17:54:57 -07006472
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006473 rc = iwl3945_scan_initiate(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006474
6475 IWL_DEBUG_MAC80211("leave\n");
6476
6477out_unlock:
6478 spin_unlock_irqrestore(&priv->lock, flags);
Mohamed Abbas15e869d2007-10-25 17:15:46 +08006479 mutex_unlock(&priv->mutex);
Zhu Yib481de92007-09-25 17:54:57 -07006480
6481 return rc;
6482}
6483
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006484static int iwl3945_mac_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
Johannes Bergdc822b52008-12-29 12:55:09 +01006485 struct ieee80211_vif *vif,
6486 struct ieee80211_sta *sta,
6487 struct ieee80211_key_conf *key)
Zhu Yib481de92007-09-25 17:54:57 -07006488{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08006489 struct iwl_priv *priv = hw->priv;
Johannes Bergdc822b52008-12-29 12:55:09 +01006490 const u8 *addr;
Zhu Yib481de92007-09-25 17:54:57 -07006491 int rc = 0;
6492 u8 sta_id;
Johannes Bergdc822b52008-12-29 12:55:09 +01006493 static const u8 bcast_addr[ETH_ALEN] =
6494 { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
Zhu Yib481de92007-09-25 17:54:57 -07006495
6496 IWL_DEBUG_MAC80211("enter\n");
6497
Kolekar, Abhijeetdf878d82008-12-19 10:37:35 +08006498 if (iwl3945_mod_params.sw_crypto) {
Zhu Yib481de92007-09-25 17:54:57 -07006499 IWL_DEBUG_MAC80211("leave - hwcrypto disabled\n");
6500 return -EOPNOTSUPP;
6501 }
6502
Johannes Bergdc822b52008-12-29 12:55:09 +01006503 addr = sta ? sta->addr : bcast_addr;
Zhu Yib481de92007-09-25 17:54:57 -07006504
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006505 sta_id = iwl3945_hw_find_station(priv, addr);
Zhu Yib481de92007-09-25 17:54:57 -07006506 if (sta_id == IWL_INVALID_STATION) {
Johannes Berge1749612008-10-27 15:59:26 -07006507 IWL_DEBUG_MAC80211("leave - %pM not in station map.\n",
6508 addr);
Zhu Yib481de92007-09-25 17:54:57 -07006509 return -EINVAL;
6510 }
6511
6512 mutex_lock(&priv->mutex);
6513
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006514 iwl3945_scan_cancel_timeout(priv, 100);
Mohamed Abbas15e869d2007-10-25 17:15:46 +08006515
Zhu Yib481de92007-09-25 17:54:57 -07006516 switch (cmd) {
6517 case SET_KEY:
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006518 rc = iwl3945_update_sta_key_info(priv, key, sta_id);
Zhu Yib481de92007-09-25 17:54:57 -07006519 if (!rc) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006520 iwl3945_set_rxon_hwcrypto(priv, 1);
6521 iwl3945_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006522 key->hw_key_idx = sta_id;
6523 IWL_DEBUG_MAC80211("set_key success, using hwcrypto\n");
6524 key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
6525 }
6526 break;
6527 case DISABLE_KEY:
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006528 rc = iwl3945_clear_sta_key_info(priv, sta_id);
Zhu Yib481de92007-09-25 17:54:57 -07006529 if (!rc) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006530 iwl3945_set_rxon_hwcrypto(priv, 0);
6531 iwl3945_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006532 IWL_DEBUG_MAC80211("disable hwcrypto key\n");
6533 }
6534 break;
6535 default:
6536 rc = -EINVAL;
6537 }
6538
6539 IWL_DEBUG_MAC80211("leave\n");
6540 mutex_unlock(&priv->mutex);
6541
6542 return rc;
6543}
6544
Johannes Berge100bb62008-04-30 18:51:21 +02006545static int iwl3945_mac_conf_tx(struct ieee80211_hw *hw, u16 queue,
Zhu Yib481de92007-09-25 17:54:57 -07006546 const struct ieee80211_tx_queue_params *params)
6547{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08006548 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07006549 unsigned long flags;
6550 int q;
Zhu Yib481de92007-09-25 17:54:57 -07006551
6552 IWL_DEBUG_MAC80211("enter\n");
6553
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08006554 if (!iwl_is_ready_rf(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07006555 IWL_DEBUG_MAC80211("leave - RF not ready\n");
6556 return -EIO;
6557 }
6558
6559 if (queue >= AC_NUM) {
6560 IWL_DEBUG_MAC80211("leave - queue >= AC_NUM %d\n", queue);
6561 return 0;
6562 }
6563
Zhu Yib481de92007-09-25 17:54:57 -07006564 q = AC_NUM - 1 - queue;
6565
6566 spin_lock_irqsave(&priv->lock, flags);
6567
6568 priv->qos_data.def_qos_parm.ac[q].cw_min = cpu_to_le16(params->cw_min);
6569 priv->qos_data.def_qos_parm.ac[q].cw_max = cpu_to_le16(params->cw_max);
6570 priv->qos_data.def_qos_parm.ac[q].aifsn = params->aifs;
6571 priv->qos_data.def_qos_parm.ac[q].edca_txop =
Johannes Berg3330d7b2008-02-10 16:49:38 +01006572 cpu_to_le16((params->txop * 32));
Zhu Yib481de92007-09-25 17:54:57 -07006573
6574 priv->qos_data.def_qos_parm.ac[q].reserved1 = 0;
6575 priv->qos_data.qos_active = 1;
6576
6577 spin_unlock_irqrestore(&priv->lock, flags);
6578
6579 mutex_lock(&priv->mutex);
Johannes Berg05c914f2008-09-11 00:01:58 +02006580 if (priv->iw_mode == NL80211_IFTYPE_AP)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006581 iwl3945_activate_qos(priv, 1);
6582 else if (priv->assoc_id && iwl3945_is_associated(priv))
6583 iwl3945_activate_qos(priv, 0);
Zhu Yib481de92007-09-25 17:54:57 -07006584
6585 mutex_unlock(&priv->mutex);
6586
Zhu Yib481de92007-09-25 17:54:57 -07006587 IWL_DEBUG_MAC80211("leave\n");
6588 return 0;
6589}
6590
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006591static int iwl3945_mac_get_tx_stats(struct ieee80211_hw *hw,
Zhu Yib481de92007-09-25 17:54:57 -07006592 struct ieee80211_tx_queue_stats *stats)
6593{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08006594 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07006595 int i, avail;
Samuel Ortiz188cf6c2008-12-22 11:31:16 +08006596 struct iwl_tx_queue *txq;
Samuel Ortizd20b3c62008-12-19 10:37:15 +08006597 struct iwl_queue *q;
Zhu Yib481de92007-09-25 17:54:57 -07006598 unsigned long flags;
6599
6600 IWL_DEBUG_MAC80211("enter\n");
6601
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08006602 if (!iwl_is_ready_rf(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07006603 IWL_DEBUG_MAC80211("leave - RF not ready\n");
6604 return -EIO;
6605 }
6606
6607 spin_lock_irqsave(&priv->lock, flags);
6608
6609 for (i = 0; i < AC_NUM; i++) {
Samuel Ortiz188cf6c2008-12-22 11:31:16 +08006610 txq = &priv->txq[i];
Zhu Yib481de92007-09-25 17:54:57 -07006611 q = &txq->q;
Samuel Ortizd20b3c62008-12-19 10:37:15 +08006612 avail = iwl_queue_space(q);
Zhu Yib481de92007-09-25 17:54:57 -07006613
Johannes Berg57ffc582008-04-29 17:18:59 +02006614 stats[i].len = q->n_window - avail;
6615 stats[i].limit = q->n_window - q->high_mark;
6616 stats[i].count = q->n_window;
Zhu Yib481de92007-09-25 17:54:57 -07006617
6618 }
6619 spin_unlock_irqrestore(&priv->lock, flags);
6620
6621 IWL_DEBUG_MAC80211("leave\n");
6622
6623 return 0;
6624}
6625
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006626static void iwl3945_mac_reset_tsf(struct ieee80211_hw *hw)
Zhu Yib481de92007-09-25 17:54:57 -07006627{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08006628 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07006629 unsigned long flags;
6630
6631 mutex_lock(&priv->mutex);
6632 IWL_DEBUG_MAC80211("enter\n");
6633
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08006634 iwl_reset_qos(priv);
Ron Rindjunsky292ae172008-02-06 11:20:39 -08006635
Zhu Yib481de92007-09-25 17:54:57 -07006636 spin_lock_irqsave(&priv->lock, flags);
6637 priv->assoc_id = 0;
6638 priv->assoc_capability = 0;
6639 priv->call_post_assoc_from_beacon = 0;
6640
6641 /* new association get rid of ibss beacon skb */
6642 if (priv->ibss_beacon)
6643 dev_kfree_skb(priv->ibss_beacon);
6644
6645 priv->ibss_beacon = NULL;
6646
6647 priv->beacon_int = priv->hw->conf.beacon_int;
Tomas Winkler28afaf92008-12-19 10:37:06 +08006648 priv->timestamp = 0;
Johannes Berg05c914f2008-09-11 00:01:58 +02006649 if ((priv->iw_mode == NL80211_IFTYPE_STATION))
Zhu Yib481de92007-09-25 17:54:57 -07006650 priv->beacon_int = 0;
6651
6652 spin_unlock_irqrestore(&priv->lock, flags);
6653
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08006654 if (!iwl_is_ready_rf(priv)) {
Mohamed Abbasfde35712007-11-29 11:10:15 +08006655 IWL_DEBUG_MAC80211("leave - not ready\n");
6656 mutex_unlock(&priv->mutex);
6657 return;
6658 }
6659
Mohamed Abbas15e869d2007-10-25 17:15:46 +08006660 /* we are restarting association process
6661 * clear RXON_FILTER_ASSOC_MSK bit
6662 */
Johannes Berg05c914f2008-09-11 00:01:58 +02006663 if (priv->iw_mode != NL80211_IFTYPE_AP) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006664 iwl3945_scan_cancel_timeout(priv, 100);
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08006665 priv->staging39_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006666 iwl3945_commit_rxon(priv);
Mohamed Abbas15e869d2007-10-25 17:15:46 +08006667 }
6668
Zhu Yib481de92007-09-25 17:54:57 -07006669 /* Per mac80211.h: This is only used in IBSS mode... */
Johannes Berg05c914f2008-09-11 00:01:58 +02006670 if (priv->iw_mode != NL80211_IFTYPE_ADHOC) {
Mohamed Abbas15e869d2007-10-25 17:15:46 +08006671
Zhu Yib481de92007-09-25 17:54:57 -07006672 IWL_DEBUG_MAC80211("leave - not in IBSS\n");
6673 mutex_unlock(&priv->mutex);
6674 return;
6675 }
6676
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006677 iwl3945_set_rate(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006678
6679 mutex_unlock(&priv->mutex);
6680
6681 IWL_DEBUG_MAC80211("leave\n");
6682
6683}
6684
Johannes Berge039fa42008-05-15 12:55:29 +02006685static int iwl3945_mac_beacon_update(struct ieee80211_hw *hw, struct sk_buff *skb)
Zhu Yib481de92007-09-25 17:54:57 -07006686{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08006687 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07006688 unsigned long flags;
6689
Zhu Yib481de92007-09-25 17:54:57 -07006690 IWL_DEBUG_MAC80211("enter\n");
6691
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08006692 if (!iwl_is_ready_rf(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07006693 IWL_DEBUG_MAC80211("leave - RF not ready\n");
Zhu Yib481de92007-09-25 17:54:57 -07006694 return -EIO;
6695 }
6696
Johannes Berg05c914f2008-09-11 00:01:58 +02006697 if (priv->iw_mode != NL80211_IFTYPE_ADHOC) {
Zhu Yib481de92007-09-25 17:54:57 -07006698 IWL_DEBUG_MAC80211("leave - not IBSS\n");
Zhu Yib481de92007-09-25 17:54:57 -07006699 return -EIO;
6700 }
6701
6702 spin_lock_irqsave(&priv->lock, flags);
6703
6704 if (priv->ibss_beacon)
6705 dev_kfree_skb(priv->ibss_beacon);
6706
6707 priv->ibss_beacon = skb;
6708
6709 priv->assoc_id = 0;
6710
6711 IWL_DEBUG_MAC80211("leave\n");
6712 spin_unlock_irqrestore(&priv->lock, flags);
6713
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08006714 iwl_reset_qos(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006715
Abhijeet Kolekardc4b1e72008-09-03 11:26:30 +08006716 iwl3945_post_associate(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006717
Zhu Yib481de92007-09-25 17:54:57 -07006718
6719 return 0;
6720}
6721
6722/*****************************************************************************
6723 *
6724 * sysfs attributes
6725 *
6726 *****************************************************************************/
6727
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08006728#ifdef CONFIG_IWL3945_DEBUG
Zhu Yib481de92007-09-25 17:54:57 -07006729
6730/*
6731 * The following adds a new attribute to the sysfs representation
6732 * of this device driver (i.e. a new file in /sys/bus/pci/drivers/iwl/)
6733 * used for controlling the debug level.
6734 *
6735 * See the level definitions in iwl for details.
6736 */
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08006737static ssize_t show_debug_level(struct device *d,
6738 struct device_attribute *attr, char *buf)
Zhu Yib481de92007-09-25 17:54:57 -07006739{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08006740 struct iwl_priv *priv = d->driver_data;
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08006741
6742 return sprintf(buf, "0x%08X\n", priv->debug_level);
Zhu Yib481de92007-09-25 17:54:57 -07006743}
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08006744static ssize_t store_debug_level(struct device *d,
6745 struct device_attribute *attr,
Zhu Yib481de92007-09-25 17:54:57 -07006746 const char *buf, size_t count)
6747{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08006748 struct iwl_priv *priv = d->driver_data;
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08006749 unsigned long val;
6750 int ret;
Zhu Yib481de92007-09-25 17:54:57 -07006751
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08006752 ret = strict_strtoul(buf, 0, &val);
6753 if (ret)
Tomas Winkler978785a2008-12-19 10:37:31 +08006754 IWL_INFO(priv, "%s is not in hex or decimal form.\n", buf);
Zhu Yib481de92007-09-25 17:54:57 -07006755 else
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08006756 priv->debug_level = val;
Zhu Yib481de92007-09-25 17:54:57 -07006757
6758 return strnlen(buf, count);
6759}
6760
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08006761static DEVICE_ATTR(debug_level, S_IWUSR | S_IRUGO,
6762 show_debug_level, store_debug_level);
Zhu Yib481de92007-09-25 17:54:57 -07006763
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08006764#endif /* CONFIG_IWL3945_DEBUG */
Zhu Yib481de92007-09-25 17:54:57 -07006765
Zhu Yib481de92007-09-25 17:54:57 -07006766static ssize_t show_temperature(struct device *d,
6767 struct device_attribute *attr, char *buf)
6768{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08006769 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07006770
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08006771 if (!iwl_is_alive(priv))
Zhu Yib481de92007-09-25 17:54:57 -07006772 return -EAGAIN;
6773
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006774 return sprintf(buf, "%d\n", iwl3945_hw_get_temperature(priv));
Zhu Yib481de92007-09-25 17:54:57 -07006775}
6776
6777static DEVICE_ATTR(temperature, S_IRUGO, show_temperature, NULL);
6778
Zhu Yib481de92007-09-25 17:54:57 -07006779static ssize_t show_tx_power(struct device *d,
6780 struct device_attribute *attr, char *buf)
6781{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08006782 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07006783 return sprintf(buf, "%d\n", priv->user_txpower_limit);
6784}
6785
6786static ssize_t store_tx_power(struct device *d,
6787 struct device_attribute *attr,
6788 const char *buf, size_t count)
6789{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08006790 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07006791 char *p = (char *)buf;
6792 u32 val;
6793
6794 val = simple_strtoul(p, &p, 10);
6795 if (p == buf)
Tomas Winkler978785a2008-12-19 10:37:31 +08006796 IWL_INFO(priv, ": %s is not in decimal form.\n", buf);
Zhu Yib481de92007-09-25 17:54:57 -07006797 else
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006798 iwl3945_hw_reg_set_txpower(priv, val);
Zhu Yib481de92007-09-25 17:54:57 -07006799
6800 return count;
6801}
6802
6803static DEVICE_ATTR(tx_power, S_IWUSR | S_IRUGO, show_tx_power, store_tx_power);
6804
6805static ssize_t show_flags(struct device *d,
6806 struct device_attribute *attr, char *buf)
6807{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08006808 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07006809
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08006810 return sprintf(buf, "0x%04X\n", priv->active39_rxon.flags);
Zhu Yib481de92007-09-25 17:54:57 -07006811}
6812
6813static ssize_t store_flags(struct device *d,
6814 struct device_attribute *attr,
6815 const char *buf, size_t count)
6816{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08006817 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07006818 u32 flags = simple_strtoul(buf, NULL, 0);
6819
6820 mutex_lock(&priv->mutex);
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08006821 if (le32_to_cpu(priv->staging39_rxon.flags) != flags) {
Zhu Yib481de92007-09-25 17:54:57 -07006822 /* Cancel any currently running scans... */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006823 if (iwl3945_scan_cancel_timeout(priv, 100))
Winkler, Tomas39aadf82008-12-19 10:37:32 +08006824 IWL_WARN(priv, "Could not cancel scan.\n");
Zhu Yib481de92007-09-25 17:54:57 -07006825 else {
6826 IWL_DEBUG_INFO("Committing rxon.flags = 0x%04X\n",
6827 flags);
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08006828 priv->staging39_rxon.flags = cpu_to_le32(flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006829 iwl3945_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006830 }
6831 }
6832 mutex_unlock(&priv->mutex);
6833
6834 return count;
6835}
6836
6837static DEVICE_ATTR(flags, S_IWUSR | S_IRUGO, show_flags, store_flags);
6838
6839static ssize_t show_filter_flags(struct device *d,
6840 struct device_attribute *attr, char *buf)
6841{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08006842 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07006843
6844 return sprintf(buf, "0x%04X\n",
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08006845 le32_to_cpu(priv->active39_rxon.filter_flags));
Zhu Yib481de92007-09-25 17:54:57 -07006846}
6847
6848static ssize_t store_filter_flags(struct device *d,
6849 struct device_attribute *attr,
6850 const char *buf, size_t count)
6851{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08006852 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07006853 u32 filter_flags = simple_strtoul(buf, NULL, 0);
6854
6855 mutex_lock(&priv->mutex);
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08006856 if (le32_to_cpu(priv->staging39_rxon.filter_flags) != filter_flags) {
Zhu Yib481de92007-09-25 17:54:57 -07006857 /* Cancel any currently running scans... */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006858 if (iwl3945_scan_cancel_timeout(priv, 100))
Winkler, Tomas39aadf82008-12-19 10:37:32 +08006859 IWL_WARN(priv, "Could not cancel scan.\n");
Zhu Yib481de92007-09-25 17:54:57 -07006860 else {
6861 IWL_DEBUG_INFO("Committing rxon.filter_flags = "
6862 "0x%04X\n", filter_flags);
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08006863 priv->staging39_rxon.filter_flags =
Zhu Yib481de92007-09-25 17:54:57 -07006864 cpu_to_le32(filter_flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006865 iwl3945_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006866 }
6867 }
6868 mutex_unlock(&priv->mutex);
6869
6870 return count;
6871}
6872
6873static DEVICE_ATTR(filter_flags, S_IWUSR | S_IRUGO, show_filter_flags,
6874 store_filter_flags);
6875
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08006876#ifdef CONFIG_IWL3945_SPECTRUM_MEASUREMENT
Zhu Yib481de92007-09-25 17:54:57 -07006877
6878static ssize_t show_measurement(struct device *d,
6879 struct device_attribute *attr, char *buf)
6880{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08006881 struct iwl_priv *priv = dev_get_drvdata(d);
Tomas Winkler600c0e12008-12-19 10:37:04 +08006882 struct iwl_spectrum_notification measure_report;
Zhu Yib481de92007-09-25 17:54:57 -07006883 u32 size = sizeof(measure_report), len = 0, ofs = 0;
Tomas Winkler3ac7f142008-07-21 02:40:14 +03006884 u8 *data = (u8 *)&measure_report;
Zhu Yib481de92007-09-25 17:54:57 -07006885 unsigned long flags;
6886
6887 spin_lock_irqsave(&priv->lock, flags);
6888 if (!(priv->measurement_status & MEASUREMENT_READY)) {
6889 spin_unlock_irqrestore(&priv->lock, flags);
6890 return 0;
6891 }
6892 memcpy(&measure_report, &priv->measure_report, size);
6893 priv->measurement_status = 0;
6894 spin_unlock_irqrestore(&priv->lock, flags);
6895
6896 while (size && (PAGE_SIZE - len)) {
6897 hex_dump_to_buffer(data + ofs, size, 16, 1, buf + len,
6898 PAGE_SIZE - len, 1);
6899 len = strlen(buf);
6900 if (PAGE_SIZE - len)
6901 buf[len++] = '\n';
6902
6903 ofs += 16;
6904 size -= min(size, 16U);
6905 }
6906
6907 return len;
6908}
6909
6910static ssize_t store_measurement(struct device *d,
6911 struct device_attribute *attr,
6912 const char *buf, size_t count)
6913{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08006914 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07006915 struct ieee80211_measurement_params params = {
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08006916 .channel = le16_to_cpu(priv->active39_rxon.channel),
Zhu Yib481de92007-09-25 17:54:57 -07006917 .start_time = cpu_to_le64(priv->last_tsf),
6918 .duration = cpu_to_le16(1),
6919 };
6920 u8 type = IWL_MEASURE_BASIC;
6921 u8 buffer[32];
6922 u8 channel;
6923
6924 if (count) {
6925 char *p = buffer;
6926 strncpy(buffer, buf, min(sizeof(buffer), count));
6927 channel = simple_strtoul(p, NULL, 0);
6928 if (channel)
6929 params.channel = channel;
6930
6931 p = buffer;
6932 while (*p && *p != ' ')
6933 p++;
6934 if (*p)
6935 type = simple_strtoul(p + 1, NULL, 0);
6936 }
6937
6938 IWL_DEBUG_INFO("Invoking measurement of type %d on "
6939 "channel %d (for '%s')\n", type, params.channel, buf);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006940 iwl3945_get_measurement(priv, &params, type);
Zhu Yib481de92007-09-25 17:54:57 -07006941
6942 return count;
6943}
6944
6945static DEVICE_ATTR(measurement, S_IRUSR | S_IWUSR,
6946 show_measurement, store_measurement);
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08006947#endif /* CONFIG_IWL3945_SPECTRUM_MEASUREMENT */
Zhu Yib481de92007-09-25 17:54:57 -07006948
Zhu Yib481de92007-09-25 17:54:57 -07006949static ssize_t store_retry_rate(struct device *d,
6950 struct device_attribute *attr,
6951 const char *buf, size_t count)
6952{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08006953 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07006954
6955 priv->retry_rate = simple_strtoul(buf, NULL, 0);
6956 if (priv->retry_rate <= 0)
6957 priv->retry_rate = 1;
6958
6959 return count;
6960}
6961
6962static ssize_t show_retry_rate(struct device *d,
6963 struct device_attribute *attr, char *buf)
6964{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08006965 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07006966 return sprintf(buf, "%d", priv->retry_rate);
6967}
6968
6969static DEVICE_ATTR(retry_rate, S_IWUSR | S_IRUSR, show_retry_rate,
6970 store_retry_rate);
6971
6972static ssize_t store_power_level(struct device *d,
6973 struct device_attribute *attr,
6974 const char *buf, size_t count)
6975{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08006976 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07006977 int rc;
6978 int mode;
6979
6980 mode = simple_strtoul(buf, NULL, 0);
6981 mutex_lock(&priv->mutex);
6982
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08006983 if (!iwl_is_ready(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07006984 rc = -EAGAIN;
6985 goto out;
6986 }
6987
Samuel Ortiz1125eff2008-12-19 10:37:14 +08006988 if ((mode < 1) || (mode > IWL39_POWER_LIMIT) ||
6989 (mode == IWL39_POWER_AC))
6990 mode = IWL39_POWER_AC;
Zhu Yib481de92007-09-25 17:54:57 -07006991 else
6992 mode |= IWL_POWER_ENABLED;
6993
6994 if (mode != priv->power_mode) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006995 rc = iwl3945_send_power_mode(priv, IWL_POWER_LEVEL(mode));
Zhu Yib481de92007-09-25 17:54:57 -07006996 if (rc) {
6997 IWL_DEBUG_MAC80211("failed setting power mode.\n");
6998 goto out;
6999 }
7000 priv->power_mode = mode;
7001 }
7002
7003 rc = count;
7004
7005 out:
7006 mutex_unlock(&priv->mutex);
7007 return rc;
7008}
7009
7010#define MAX_WX_STRING 80
7011
7012/* Values are in microsecond */
7013static const s32 timeout_duration[] = {
7014 350000,
7015 250000,
7016 75000,
7017 37000,
7018 25000,
7019};
7020static const s32 period_duration[] = {
7021 400000,
7022 700000,
7023 1000000,
7024 1000000,
7025 1000000
7026};
7027
7028static ssize_t show_power_level(struct device *d,
7029 struct device_attribute *attr, char *buf)
7030{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08007031 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07007032 int level = IWL_POWER_LEVEL(priv->power_mode);
7033 char *p = buf;
7034
7035 p += sprintf(p, "%d ", level);
7036 switch (level) {
7037 case IWL_POWER_MODE_CAM:
Samuel Ortiz1125eff2008-12-19 10:37:14 +08007038 case IWL39_POWER_AC:
Zhu Yib481de92007-09-25 17:54:57 -07007039 p += sprintf(p, "(AC)");
7040 break;
Samuel Ortiz1125eff2008-12-19 10:37:14 +08007041 case IWL39_POWER_BATTERY:
Zhu Yib481de92007-09-25 17:54:57 -07007042 p += sprintf(p, "(BATTERY)");
7043 break;
7044 default:
7045 p += sprintf(p,
7046 "(Timeout %dms, Period %dms)",
7047 timeout_duration[level - 1] / 1000,
7048 period_duration[level - 1] / 1000);
7049 }
7050
7051 if (!(priv->power_mode & IWL_POWER_ENABLED))
7052 p += sprintf(p, " OFF\n");
7053 else
7054 p += sprintf(p, " \n");
7055
Tomas Winkler3ac7f142008-07-21 02:40:14 +03007056 return p - buf + 1;
Zhu Yib481de92007-09-25 17:54:57 -07007057
7058}
7059
7060static DEVICE_ATTR(power_level, S_IWUSR | S_IRUSR, show_power_level,
7061 store_power_level);
7062
7063static ssize_t show_channels(struct device *d,
7064 struct device_attribute *attr, char *buf)
7065{
Johannes Berg8318d782008-01-24 19:38:38 +01007066 /* all this shit doesn't belong into sysfs anyway */
7067 return 0;
Zhu Yib481de92007-09-25 17:54:57 -07007068}
7069
7070static DEVICE_ATTR(channels, S_IRUSR, show_channels, NULL);
7071
7072static ssize_t show_statistics(struct device *d,
7073 struct device_attribute *attr, char *buf)
7074{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08007075 struct iwl_priv *priv = dev_get_drvdata(d);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007076 u32 size = sizeof(struct iwl3945_notif_statistics);
Zhu Yib481de92007-09-25 17:54:57 -07007077 u32 len = 0, ofs = 0;
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08007078 u8 *data = (u8 *)&priv->statistics_39;
Zhu Yib481de92007-09-25 17:54:57 -07007079 int rc = 0;
7080
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08007081 if (!iwl_is_alive(priv))
Zhu Yib481de92007-09-25 17:54:57 -07007082 return -EAGAIN;
7083
7084 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007085 rc = iwl3945_send_statistics_request(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007086 mutex_unlock(&priv->mutex);
7087
7088 if (rc) {
7089 len = sprintf(buf,
7090 "Error sending statistics request: 0x%08X\n", rc);
7091 return len;
7092 }
7093
7094 while (size && (PAGE_SIZE - len)) {
7095 hex_dump_to_buffer(data + ofs, size, 16, 1, buf + len,
7096 PAGE_SIZE - len, 1);
7097 len = strlen(buf);
7098 if (PAGE_SIZE - len)
7099 buf[len++] = '\n';
7100
7101 ofs += 16;
7102 size -= min(size, 16U);
7103 }
7104
7105 return len;
7106}
7107
7108static DEVICE_ATTR(statistics, S_IRUGO, show_statistics, NULL);
7109
7110static ssize_t show_antenna(struct device *d,
7111 struct device_attribute *attr, char *buf)
7112{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08007113 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07007114
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08007115 if (!iwl_is_alive(priv))
Zhu Yib481de92007-09-25 17:54:57 -07007116 return -EAGAIN;
7117
7118 return sprintf(buf, "%d\n", priv->antenna);
7119}
7120
7121static ssize_t store_antenna(struct device *d,
7122 struct device_attribute *attr,
7123 const char *buf, size_t count)
7124{
7125 int ant;
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08007126 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07007127
7128 if (count == 0)
7129 return 0;
7130
7131 if (sscanf(buf, "%1i", &ant) != 1) {
7132 IWL_DEBUG_INFO("not in hex or decimal form.\n");
7133 return count;
7134 }
7135
7136 if ((ant >= 0) && (ant <= 2)) {
7137 IWL_DEBUG_INFO("Setting antenna select to %d.\n", ant);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007138 priv->antenna = (enum iwl3945_antenna)ant;
Zhu Yib481de92007-09-25 17:54:57 -07007139 } else
7140 IWL_DEBUG_INFO("Bad antenna select value %d.\n", ant);
7141
7142
7143 return count;
7144}
7145
7146static DEVICE_ATTR(antenna, S_IWUSR | S_IRUGO, show_antenna, store_antenna);
7147
7148static ssize_t show_status(struct device *d,
7149 struct device_attribute *attr, char *buf)
7150{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08007151 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08007152 if (!iwl_is_alive(priv))
Zhu Yib481de92007-09-25 17:54:57 -07007153 return -EAGAIN;
7154 return sprintf(buf, "0x%08x\n", (int)priv->status);
7155}
7156
7157static DEVICE_ATTR(status, S_IRUGO, show_status, NULL);
7158
7159static ssize_t dump_error_log(struct device *d,
7160 struct device_attribute *attr,
7161 const char *buf, size_t count)
7162{
7163 char *p = (char *)buf;
7164
7165 if (p[0] == '1')
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08007166 iwl3945_dump_nic_error_log((struct iwl_priv *)d->driver_data);
Zhu Yib481de92007-09-25 17:54:57 -07007167
7168 return strnlen(buf, count);
7169}
7170
7171static DEVICE_ATTR(dump_errors, S_IWUSR, NULL, dump_error_log);
7172
7173static ssize_t dump_event_log(struct device *d,
7174 struct device_attribute *attr,
7175 const char *buf, size_t count)
7176{
7177 char *p = (char *)buf;
7178
7179 if (p[0] == '1')
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08007180 iwl3945_dump_nic_event_log((struct iwl_priv *)d->driver_data);
Zhu Yib481de92007-09-25 17:54:57 -07007181
7182 return strnlen(buf, count);
7183}
7184
7185static DEVICE_ATTR(dump_events, S_IWUSR, NULL, dump_event_log);
7186
7187/*****************************************************************************
7188 *
Tomas Winklera96a27f2008-10-23 23:48:56 -07007189 * driver setup and tear down
Zhu Yib481de92007-09-25 17:54:57 -07007190 *
7191 *****************************************************************************/
7192
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08007193static void iwl3945_setup_deferred_work(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07007194{
7195 priv->workqueue = create_workqueue(DRV_NAME);
7196
7197 init_waitqueue_head(&priv->wait_command_queue);
7198
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007199 INIT_WORK(&priv->up, iwl3945_bg_up);
7200 INIT_WORK(&priv->restart, iwl3945_bg_restart);
7201 INIT_WORK(&priv->rx_replenish, iwl3945_bg_rx_replenish);
7202 INIT_WORK(&priv->scan_completed, iwl3945_bg_scan_completed);
7203 INIT_WORK(&priv->request_scan, iwl3945_bg_request_scan);
7204 INIT_WORK(&priv->abort_scan, iwl3945_bg_abort_scan);
7205 INIT_WORK(&priv->rf_kill, iwl3945_bg_rf_kill);
7206 INIT_WORK(&priv->beacon_update, iwl3945_bg_beacon_update);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007207 INIT_DELAYED_WORK(&priv->init_alive_start, iwl3945_bg_init_alive_start);
7208 INIT_DELAYED_WORK(&priv->alive_start, iwl3945_bg_alive_start);
7209 INIT_DELAYED_WORK(&priv->scan_check, iwl3945_bg_scan_check);
Zhu Yib481de92007-09-25 17:54:57 -07007210
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007211 iwl3945_hw_setup_deferred_work(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007212
7213 tasklet_init(&priv->irq_tasklet, (void (*)(unsigned long))
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007214 iwl3945_irq_tasklet, (unsigned long)priv);
Zhu Yib481de92007-09-25 17:54:57 -07007215}
7216
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08007217static void iwl3945_cancel_deferred_work(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07007218{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007219 iwl3945_hw_cancel_deferred_work(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007220
Joonwoo Parke47eb6a2007-11-29 10:42:49 +09007221 cancel_delayed_work_sync(&priv->init_alive_start);
Zhu Yib481de92007-09-25 17:54:57 -07007222 cancel_delayed_work(&priv->scan_check);
7223 cancel_delayed_work(&priv->alive_start);
Zhu Yib481de92007-09-25 17:54:57 -07007224 cancel_work_sync(&priv->beacon_update);
7225}
7226
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007227static struct attribute *iwl3945_sysfs_entries[] = {
Zhu Yib481de92007-09-25 17:54:57 -07007228 &dev_attr_antenna.attr,
7229 &dev_attr_channels.attr,
7230 &dev_attr_dump_errors.attr,
7231 &dev_attr_dump_events.attr,
7232 &dev_attr_flags.attr,
7233 &dev_attr_filter_flags.attr,
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08007234#ifdef CONFIG_IWL3945_SPECTRUM_MEASUREMENT
Zhu Yib481de92007-09-25 17:54:57 -07007235 &dev_attr_measurement.attr,
7236#endif
7237 &dev_attr_power_level.attr,
Zhu Yib481de92007-09-25 17:54:57 -07007238 &dev_attr_retry_rate.attr,
Zhu Yib481de92007-09-25 17:54:57 -07007239 &dev_attr_statistics.attr,
7240 &dev_attr_status.attr,
7241 &dev_attr_temperature.attr,
Zhu Yib481de92007-09-25 17:54:57 -07007242 &dev_attr_tx_power.attr,
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08007243#ifdef CONFIG_IWL3945_DEBUG
7244 &dev_attr_debug_level.attr,
7245#endif
Zhu Yib481de92007-09-25 17:54:57 -07007246 NULL
7247};
7248
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007249static struct attribute_group iwl3945_attribute_group = {
Zhu Yib481de92007-09-25 17:54:57 -07007250 .name = NULL, /* put in device directory */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007251 .attrs = iwl3945_sysfs_entries,
Zhu Yib481de92007-09-25 17:54:57 -07007252};
7253
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007254static struct ieee80211_ops iwl3945_hw_ops = {
7255 .tx = iwl3945_mac_tx,
7256 .start = iwl3945_mac_start,
7257 .stop = iwl3945_mac_stop,
7258 .add_interface = iwl3945_mac_add_interface,
7259 .remove_interface = iwl3945_mac_remove_interface,
7260 .config = iwl3945_mac_config,
7261 .config_interface = iwl3945_mac_config_interface,
7262 .configure_filter = iwl3945_configure_filter,
7263 .set_key = iwl3945_mac_set_key,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007264 .get_tx_stats = iwl3945_mac_get_tx_stats,
7265 .conf_tx = iwl3945_mac_conf_tx,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007266 .reset_tsf = iwl3945_mac_reset_tsf,
Abhijeet Kolekarcd56d332008-09-03 11:26:21 +08007267 .bss_info_changed = iwl3945_bss_info_changed,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007268 .hw_scan = iwl3945_mac_hw_scan
Zhu Yib481de92007-09-25 17:54:57 -07007269};
7270
Winkler, Tomase52119c2008-12-22 11:31:19 +08007271static int iwl3945_init_drv(struct iwl_priv *priv)
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08007272{
7273 int ret;
7274
7275 priv->retry_rate = 1;
7276 priv->ibss_beacon = NULL;
7277
7278 spin_lock_init(&priv->lock);
7279 spin_lock_init(&priv->power_data.lock);
7280 spin_lock_init(&priv->sta_lock);
7281 spin_lock_init(&priv->hcmd_lock);
7282
7283 INIT_LIST_HEAD(&priv->free_frames);
7284
7285 mutex_init(&priv->mutex);
7286
7287 /* Clear the driver's (not device's) station table */
7288 iwl3945_clear_stations_table(priv);
7289
7290 priv->data_retry_limit = -1;
7291 priv->ieee_channels = NULL;
7292 priv->ieee_rates = NULL;
7293 priv->band = IEEE80211_BAND_2GHZ;
7294
7295 priv->iw_mode = NL80211_IFTYPE_STATION;
7296
7297 iwl_reset_qos(priv);
7298
7299 priv->qos_data.qos_active = 0;
7300 priv->qos_data.qos_cap.val = 0;
7301
7302 priv->rates_mask = IWL_RATES_MASK;
7303 /* If power management is turned on, default to AC mode */
7304 priv->power_mode = IWL_POWER_AC;
7305 priv->user_txpower_limit = IWL_DEFAULT_TX_POWER;
7306
7307 ret = iwl3945_init_channel_map(priv);
7308 if (ret) {
7309 IWL_ERR(priv, "initializing regulatory failed: %d\n", ret);
7310 goto err;
7311 }
7312
7313 ret = iwl3945_init_geos(priv);
7314 if (ret) {
7315 IWL_ERR(priv, "initializing geos failed: %d\n", ret);
7316 goto err_free_channel_map;
7317 }
7318
7319 return 0;
7320
7321err_free_channel_map:
7322 iwl3945_free_channel_map(priv);
7323err:
7324 return ret;
7325}
7326
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007327static int iwl3945_pci_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
Zhu Yib481de92007-09-25 17:54:57 -07007328{
7329 int err = 0;
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08007330 struct iwl_priv *priv;
Zhu Yib481de92007-09-25 17:54:57 -07007331 struct ieee80211_hw *hw;
Kolekar, Abhijeetc0f20d92008-12-19 10:37:19 +08007332 struct iwl_cfg *cfg = (struct iwl_cfg *)(ent->driver_data);
Mohamed Abbas0359fac2008-03-28 16:21:08 -07007333 unsigned long flags;
Zhu Yib481de92007-09-25 17:54:57 -07007334
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08007335 /***********************
7336 * 1. Allocating HW data
7337 * ********************/
7338
Zhu Yib481de92007-09-25 17:54:57 -07007339 /* mac80211 allocates memory for this device instance, including
7340 * space for this driver's private structure */
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08007341 hw = iwl_alloc_all(cfg, &iwl3945_hw_ops);
Zhu Yib481de92007-09-25 17:54:57 -07007342 if (hw == NULL) {
Samuel Ortiza3139c52008-12-19 10:37:09 +08007343 printk(KERN_ERR DRV_NAME "Can not allocate network device\n");
Zhu Yib481de92007-09-25 17:54:57 -07007344 err = -ENOMEM;
7345 goto out;
7346 }
Zhu Yib481de92007-09-25 17:54:57 -07007347 priv = hw->priv;
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08007348 SET_IEEE80211_DEV(hw, &pdev->dev);
Cahill, Ben M6440adb2007-11-29 11:09:55 +08007349
Kolekar, Abhijeetdf878d82008-12-19 10:37:35 +08007350 if ((iwl3945_mod_params.num_of_queues > IWL39_MAX_NUM_QUEUES) ||
7351 (iwl3945_mod_params.num_of_queues < IWL_MIN_NUM_QUEUES)) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08007352 IWL_ERR(priv,
7353 "invalid queues_num, should be between %d and %d\n",
7354 IWL_MIN_NUM_QUEUES, IWL39_MAX_NUM_QUEUES);
Samuel Ortiza3139c52008-12-19 10:37:09 +08007355 err = -EINVAL;
7356 goto out;
7357 }
7358
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08007359 /*
7360 * Disabling hardware scan means that mac80211 will perform scans
7361 * "the hard way", rather than using device's scan.
7362 */
Kolekar, Abhijeetdf878d82008-12-19 10:37:35 +08007363 if (iwl3945_mod_params.disable_hw_scan) {
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08007364 IWL_DEBUG_INFO("Disabling hw_scan\n");
7365 iwl3945_hw_ops.hw_scan = NULL;
7366 }
7367
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08007368
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08007369 IWL_DEBUG_INFO("*** LOAD DRIVER ***\n");
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08007370 priv->cfg = cfg;
7371 priv->pci_dev = pdev;
7372
7373#ifdef CONFIG_IWL3945_DEBUG
7374 priv->debug_level = iwl3945_mod_params.debug;
7375 atomic_set(&priv->restrict_refcnt, 0);
7376#endif
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08007377 hw->rate_control_algorithm = "iwl-3945-rs";
7378 hw->sta_data_size = sizeof(struct iwl3945_sta_priv);
7379
Cahill, Ben M6440adb2007-11-29 11:09:55 +08007380 /* Select antenna (may be helpful if only one antenna is connected) */
Kolekar, Abhijeetdf878d82008-12-19 10:37:35 +08007381 priv->antenna = (enum iwl3945_antenna)iwl3945_mod_params.antenna;
Zhu Yib481de92007-09-25 17:54:57 -07007382
Bruno Randolf566bfe52008-05-08 19:15:40 +02007383 /* Tell mac80211 our characteristics */
Johannes Berg605a0bd2008-07-15 10:10:01 +02007384 hw->flags = IEEE80211_HW_SIGNAL_DBM |
Bruno Randolf566bfe52008-05-08 19:15:40 +02007385 IEEE80211_HW_NOISE_DBM;
Zhu Yib481de92007-09-25 17:54:57 -07007386
Luis R. Rodriguezf59ac042008-08-29 16:26:43 -07007387 hw->wiphy->interface_modes =
Luis R. Rodriguezf59ac042008-08-29 16:26:43 -07007388 BIT(NL80211_IFTYPE_STATION) |
7389 BIT(NL80211_IFTYPE_ADHOC);
7390
Luis R. Rodriguezea4a82dc2008-11-12 14:22:04 -08007391 hw->wiphy->fw_handles_regulatory = true;
7392
Cahill, Ben M6440adb2007-11-29 11:09:55 +08007393 /* 4 EDCA QOS priorities */
Zhu Yib481de92007-09-25 17:54:57 -07007394 hw->queues = 4;
7395
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08007396 /***************************
7397 * 2. Initializing PCI bus
7398 * *************************/
Zhu Yib481de92007-09-25 17:54:57 -07007399 if (pci_enable_device(pdev)) {
7400 err = -ENODEV;
7401 goto out_ieee80211_free_hw;
7402 }
7403
7404 pci_set_master(pdev);
7405
Zhu Yib481de92007-09-25 17:54:57 -07007406 err = pci_set_dma_mask(pdev, DMA_32BIT_MASK);
7407 if (!err)
7408 err = pci_set_consistent_dma_mask(pdev, DMA_32BIT_MASK);
7409 if (err) {
Tomas Winkler978785a2008-12-19 10:37:31 +08007410 IWL_WARN(priv, "No suitable DMA available.\n");
Zhu Yib481de92007-09-25 17:54:57 -07007411 goto out_pci_disable_device;
7412 }
7413
7414 pci_set_drvdata(pdev, priv);
7415 err = pci_request_regions(pdev, DRV_NAME);
7416 if (err)
7417 goto out_pci_disable_device;
Cahill, Ben M6440adb2007-11-29 11:09:55 +08007418
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08007419 /***********************
7420 * 3. Read REV Register
7421 * ********************/
Zhu Yib481de92007-09-25 17:54:57 -07007422 priv->hw_base = pci_iomap(pdev, 0, 0);
7423 if (!priv->hw_base) {
7424 err = -ENODEV;
7425 goto out_pci_release_regions;
7426 }
7427
7428 IWL_DEBUG_INFO("pci_resource_len = 0x%08llx\n",
7429 (unsigned long long) pci_resource_len(pdev, 0));
7430 IWL_DEBUG_INFO("pci_resource_base = %p\n", priv->hw_base);
7431
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08007432 /* We disable the RETRY_TIMEOUT register (0x41) to keep
7433 * PCI Tx retries from interfering with C3 CPU state */
7434 pci_write_config_byte(pdev, 0x41, 0x00);
Zhu Yib481de92007-09-25 17:54:57 -07007435
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08007436 /* amp init */
7437 err = priv->cfg->ops->lib->apm_ops.init(priv);
Tomas Winkler3ac7f142008-07-21 02:40:14 +03007438 if (err < 0) {
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08007439 IWL_DEBUG_INFO("Failed to init APMG\n");
7440 goto out_iounmap;
Tomas Winkler3ac7f142008-07-21 02:40:14 +03007441 }
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08007442
7443 /***********************
7444 * 4. Read EEPROM
7445 * ********************/
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08007446
Zhu Yi5a669262008-01-14 17:46:18 -08007447 /* Read the EEPROM */
7448 err = iwl3945_eeprom_init(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007449 if (err) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08007450 IWL_ERR(priv, "Unable to init EEPROM\n");
Zhu Yi5a669262008-01-14 17:46:18 -08007451 goto out_remove_sysfs;
7452 }
7453 /* MAC Address location in EEPROM same for 3945/4965 */
7454 get_eeprom_mac(priv, priv->mac_addr);
Johannes Berge1749612008-10-27 15:59:26 -07007455 IWL_DEBUG_INFO("MAC address: %pM\n", priv->mac_addr);
Zhu Yi5a669262008-01-14 17:46:18 -08007456 SET_IEEE80211_PERM_ADDR(priv->hw, priv->mac_addr);
7457
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08007458 /***********************
7459 * 5. Setup HW Constants
7460 * ********************/
7461 /* Device-specific setup */
Abhijeet Kolekar3832ec92008-12-19 10:37:26 +08007462 if (iwl3945_hw_set_hw_params(priv)) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08007463 IWL_ERR(priv, "failed to set hw settings\n");
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08007464 goto out_iounmap;
7465 }
7466
7467 /***********************
7468 * 6. Setup priv
7469 * ********************/
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08007470
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08007471 err = iwl3945_init_drv(priv);
Reinette Chatre849e0dc2008-01-23 10:15:18 -08007472 if (err) {
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08007473 IWL_ERR(priv, "initializing driver failed\n");
7474 goto out_free_geos;
Reinette Chatre849e0dc2008-01-23 10:15:18 -08007475 }
Reinette Chatre849e0dc2008-01-23 10:15:18 -08007476
Tomas Winkler978785a2008-12-19 10:37:31 +08007477 IWL_INFO(priv, "Detected Intel Wireless WiFi Link %s\n",
7478 priv->cfg->name);
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08007479
7480 /***********************************
7481 * 7. Initialize Module Parameters
7482 * **********************************/
7483
7484 /* Initialize module parameter values here */
7485 /* Disable radio (SW RF KILL) via parameter when loading driver */
Kolekar, Abhijeetdf878d82008-12-19 10:37:35 +08007486 if (iwl3945_mod_params.disable) {
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08007487 set_bit(STATUS_RF_KILL_SW, &priv->status);
7488 IWL_DEBUG_INFO("Radio disabled.\n");
7489 }
7490
7491
7492 /***********************
7493 * 8. Setup Services
7494 * ********************/
7495
7496 spin_lock_irqsave(&priv->lock, flags);
7497 iwl3945_disable_interrupts(priv);
7498 spin_unlock_irqrestore(&priv->lock, flags);
7499
7500 err = sysfs_create_group(&pdev->dev.kobj, &iwl3945_attribute_group);
7501 if (err) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08007502 IWL_ERR(priv, "failed to create sysfs device attributes\n");
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08007503 goto out_release_irq;
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08007504 }
7505
7506 iwl3945_set_rxon_channel(priv, IEEE80211_BAND_2GHZ, 6);
7507 iwl3945_setup_deferred_work(priv);
7508 iwl3945_setup_rx_handlers(priv);
7509
7510 /***********************
7511 * 9. Conclude
7512 * ********************/
7513 pci_save_state(pdev);
7514 pci_disable_device(pdev);
7515
7516 /*********************************
7517 * 10. Setup and Register mac80211
7518 * *******************************/
7519
Zhu Yi5a669262008-01-14 17:46:18 -08007520 err = ieee80211_register_hw(priv->hw);
7521 if (err) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08007522 IWL_ERR(priv, "Failed to register network device: %d\n", err);
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08007523 goto out_remove_sysfs;
Zhu Yib481de92007-09-25 17:54:57 -07007524 }
7525
Zhu Yi5a669262008-01-14 17:46:18 -08007526 priv->hw->conf.beacon_int = 100;
7527 priv->mac80211_registered = 1;
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08007528
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08007529 err = iwl3945_rfkill_init(priv);
7530 if (err)
Winkler, Tomas15b16872008-12-19 10:37:33 +08007531 IWL_ERR(priv, "Unable to initialize RFKILL system. "
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08007532 "Ignoring error: %d\n", err);
7533
Zhu Yib481de92007-09-25 17:54:57 -07007534 return 0;
7535
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08007536 out_remove_sysfs:
7537 sysfs_remove_group(&pdev->dev.kobj, &iwl3945_attribute_group);
Reinette Chatre849e0dc2008-01-23 10:15:18 -08007538 out_free_geos:
7539 iwl3945_free_geos(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007540
7541 out_release_irq:
Zhu Yib481de92007-09-25 17:54:57 -07007542 destroy_workqueue(priv->workqueue);
7543 priv->workqueue = NULL;
Abhijeet Kolekar3832ec92008-12-19 10:37:26 +08007544 iwl3945_unset_hw_params(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007545
7546 out_iounmap:
7547 pci_iounmap(pdev, priv->hw_base);
7548 out_pci_release_regions:
7549 pci_release_regions(pdev);
7550 out_pci_disable_device:
7551 pci_disable_device(pdev);
7552 pci_set_drvdata(pdev, NULL);
7553 out_ieee80211_free_hw:
7554 ieee80211_free_hw(priv->hw);
7555 out:
7556 return err;
7557}
7558
Reinette Chatrec83dbf62008-03-21 13:53:41 -07007559static void __devexit iwl3945_pci_remove(struct pci_dev *pdev)
Zhu Yib481de92007-09-25 17:54:57 -07007560{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08007561 struct iwl_priv *priv = pci_get_drvdata(pdev);
Mohamed Abbas0359fac2008-03-28 16:21:08 -07007562 unsigned long flags;
Zhu Yib481de92007-09-25 17:54:57 -07007563
7564 if (!priv)
7565 return;
7566
7567 IWL_DEBUG_INFO("*** UNLOAD DRIVER ***\n");
7568
Zhu Yib481de92007-09-25 17:54:57 -07007569 set_bit(STATUS_EXIT_PENDING, &priv->status);
Zhu Yib24d22b2007-12-19 13:59:52 +08007570
Kolekar, Abhijeetd552bfb2008-12-19 10:37:41 +08007571 if (priv->mac80211_registered) {
7572 ieee80211_unregister_hw(priv->hw);
7573 priv->mac80211_registered = 0;
7574 } else {
7575 iwl3945_down(priv);
7576 }
Zhu Yib481de92007-09-25 17:54:57 -07007577
Mohamed Abbas0359fac2008-03-28 16:21:08 -07007578 /* make sure we flush any pending irq or
7579 * tasklet for the driver
7580 */
7581 spin_lock_irqsave(&priv->lock, flags);
7582 iwl3945_disable_interrupts(priv);
7583 spin_unlock_irqrestore(&priv->lock, flags);
7584
7585 iwl_synchronize_irq(priv);
7586
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007587 sysfs_remove_group(&pdev->dev.kobj, &iwl3945_attribute_group);
Zhu Yib481de92007-09-25 17:54:57 -07007588
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08007589 iwl3945_rfkill_unregister(priv);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007590 iwl3945_dealloc_ucode_pci(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007591
7592 if (priv->rxq.bd)
Winkler, Tomas51af3d32008-12-22 11:31:23 +08007593 iwl_rx_queue_free(priv, &priv->rxq);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007594 iwl3945_hw_txq_ctx_free(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007595
Abhijeet Kolekar3832ec92008-12-19 10:37:26 +08007596 iwl3945_unset_hw_params(priv);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007597 iwl3945_clear_stations_table(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007598
Mohamed Abbas6ef89d02007-10-25 17:15:47 +08007599 /*netif_stop_queue(dev); */
7600 flush_workqueue(priv->workqueue);
7601
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007602 /* ieee80211_unregister_hw calls iwl3945_mac_stop, which flushes
Zhu Yib481de92007-09-25 17:54:57 -07007603 * priv->workqueue... so we can't take down the workqueue
7604 * until now... */
7605 destroy_workqueue(priv->workqueue);
7606 priv->workqueue = NULL;
7607
Zhu Yib481de92007-09-25 17:54:57 -07007608 pci_iounmap(pdev, priv->hw_base);
7609 pci_release_regions(pdev);
7610 pci_disable_device(pdev);
7611 pci_set_drvdata(pdev, NULL);
7612
Reinette Chatre849e0dc2008-01-23 10:15:18 -08007613 iwl3945_free_channel_map(priv);
7614 iwl3945_free_geos(priv);
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08007615 kfree(priv->scan39);
Zhu Yib481de92007-09-25 17:54:57 -07007616 if (priv->ibss_beacon)
7617 dev_kfree_skb(priv->ibss_beacon);
7618
7619 ieee80211_free_hw(priv->hw);
7620}
7621
7622#ifdef CONFIG_PM
7623
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007624static int iwl3945_pci_suspend(struct pci_dev *pdev, pm_message_t state)
Zhu Yib481de92007-09-25 17:54:57 -07007625{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08007626 struct iwl_priv *priv = pci_get_drvdata(pdev);
Zhu Yib481de92007-09-25 17:54:57 -07007627
Zhu Yie655b9f2008-01-24 02:19:38 -08007628 if (priv->is_open) {
7629 set_bit(STATUS_IN_SUSPEND, &priv->status);
7630 iwl3945_mac_stop(priv->hw);
7631 priv->is_open = 1;
7632 }
Zhu Yib481de92007-09-25 17:54:57 -07007633
Zhu Yib481de92007-09-25 17:54:57 -07007634 pci_set_power_state(pdev, PCI_D3hot);
7635
Zhu Yib481de92007-09-25 17:54:57 -07007636 return 0;
7637}
7638
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007639static int iwl3945_pci_resume(struct pci_dev *pdev)
Zhu Yib481de92007-09-25 17:54:57 -07007640{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08007641 struct iwl_priv *priv = pci_get_drvdata(pdev);
Zhu Yib481de92007-09-25 17:54:57 -07007642
Zhu Yib481de92007-09-25 17:54:57 -07007643 pci_set_power_state(pdev, PCI_D0);
Zhu Yib481de92007-09-25 17:54:57 -07007644
Zhu Yie655b9f2008-01-24 02:19:38 -08007645 if (priv->is_open)
7646 iwl3945_mac_start(priv->hw);
Zhu Yib481de92007-09-25 17:54:57 -07007647
Zhu Yie655b9f2008-01-24 02:19:38 -08007648 clear_bit(STATUS_IN_SUSPEND, &priv->status);
Zhu Yib481de92007-09-25 17:54:57 -07007649 return 0;
7650}
7651
7652#endif /* CONFIG_PM */
7653
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08007654/*************** RFKILL FUNCTIONS **********/
Adel Gadllah80fcc9e2008-07-01 17:49:50 +02007655#ifdef CONFIG_IWL3945_RFKILL
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08007656/* software rf-kill from user */
7657static int iwl3945_rfkill_soft_rf_kill(void *data, enum rfkill_state state)
7658{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08007659 struct iwl_priv *priv = data;
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08007660 int err = 0;
7661
Adel Gadllah80fcc9e2008-07-01 17:49:50 +02007662 if (!priv->rfkill)
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08007663 return 0;
7664
7665 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
7666 return 0;
7667
Tomas Winklera96a27f2008-10-23 23:48:56 -07007668 IWL_DEBUG_RF_KILL("we received soft RFKILL set to state %d\n", state);
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08007669 mutex_lock(&priv->mutex);
7670
7671 switch (state) {
Zhu Yiacdfe9b2008-06-30 17:23:32 +08007672 case RFKILL_STATE_UNBLOCKED:
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08007673 if (iwl_is_rfkill_hw(priv)) {
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08007674 err = -EBUSY;
Adel Gadllah80fcc9e2008-07-01 17:49:50 +02007675 goto out_unlock;
7676 }
7677 iwl3945_radio_kill_sw(priv, 0);
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08007678 break;
Zhu Yiacdfe9b2008-06-30 17:23:32 +08007679 case RFKILL_STATE_SOFT_BLOCKED:
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08007680 iwl3945_radio_kill_sw(priv, 1);
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08007681 break;
Zhu Yiacdfe9b2008-06-30 17:23:32 +08007682 default:
Winkler, Tomas39aadf82008-12-19 10:37:32 +08007683 IWL_WARN(priv, "received unexpected RFKILL state %d\n", state);
Zhu Yiacdfe9b2008-06-30 17:23:32 +08007684 break;
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08007685 }
Adel Gadllah80fcc9e2008-07-01 17:49:50 +02007686out_unlock:
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08007687 mutex_unlock(&priv->mutex);
7688
7689 return err;
7690}
7691
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08007692int iwl3945_rfkill_init(struct iwl_priv *priv)
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08007693{
7694 struct device *device = wiphy_dev(priv->hw->wiphy);
7695 int ret = 0;
7696
7697 BUG_ON(device == NULL);
7698
7699 IWL_DEBUG_RF_KILL("Initializing RFKILL.\n");
Adel Gadllah80fcc9e2008-07-01 17:49:50 +02007700 priv->rfkill = rfkill_allocate(device, RFKILL_TYPE_WLAN);
7701 if (!priv->rfkill) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08007702 IWL_ERR(priv, "Unable to allocate rfkill device.\n");
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08007703 ret = -ENOMEM;
7704 goto error;
7705 }
7706
Adel Gadllah80fcc9e2008-07-01 17:49:50 +02007707 priv->rfkill->name = priv->cfg->name;
7708 priv->rfkill->data = priv;
7709 priv->rfkill->state = RFKILL_STATE_UNBLOCKED;
7710 priv->rfkill->toggle_radio = iwl3945_rfkill_soft_rf_kill;
7711 priv->rfkill->user_claim_unsupported = 1;
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08007712
Adel Gadllah80fcc9e2008-07-01 17:49:50 +02007713 priv->rfkill->dev.class->suspend = NULL;
7714 priv->rfkill->dev.class->resume = NULL;
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08007715
Adel Gadllah80fcc9e2008-07-01 17:49:50 +02007716 ret = rfkill_register(priv->rfkill);
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08007717 if (ret) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08007718 IWL_ERR(priv, "Unable to register rfkill: %d\n", ret);
Adel Gadllah80fcc9e2008-07-01 17:49:50 +02007719 goto freed_rfkill;
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08007720 }
7721
7722 IWL_DEBUG_RF_KILL("RFKILL initialization complete.\n");
7723 return ret;
7724
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08007725freed_rfkill:
Adel Gadllah80fcc9e2008-07-01 17:49:50 +02007726 if (priv->rfkill != NULL)
7727 rfkill_free(priv->rfkill);
7728 priv->rfkill = NULL;
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08007729
7730error:
7731 IWL_DEBUG_RF_KILL("RFKILL initialization complete.\n");
7732 return ret;
7733}
7734
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08007735void iwl3945_rfkill_unregister(struct iwl_priv *priv)
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08007736{
Adel Gadllah80fcc9e2008-07-01 17:49:50 +02007737 if (priv->rfkill)
7738 rfkill_unregister(priv->rfkill);
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08007739
Adel Gadllah80fcc9e2008-07-01 17:49:50 +02007740 priv->rfkill = NULL;
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08007741}
7742
7743/* set rf-kill to the right state. */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08007744void iwl3945_rfkill_set_hw_state(struct iwl_priv *priv)
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08007745{
7746
Adel Gadllah80fcc9e2008-07-01 17:49:50 +02007747 if (!priv->rfkill)
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08007748 return;
7749
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08007750 if (iwl_is_rfkill_hw(priv)) {
Adel Gadllah80fcc9e2008-07-01 17:49:50 +02007751 rfkill_force_state(priv->rfkill, RFKILL_STATE_HARD_BLOCKED);
7752 return;
7753 }
7754
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08007755 if (!iwl_is_rfkill_sw(priv))
Adel Gadllah80fcc9e2008-07-01 17:49:50 +02007756 rfkill_force_state(priv->rfkill, RFKILL_STATE_UNBLOCKED);
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08007757 else
Adel Gadllah80fcc9e2008-07-01 17:49:50 +02007758 rfkill_force_state(priv->rfkill, RFKILL_STATE_SOFT_BLOCKED);
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08007759}
7760#endif
7761
Zhu Yib481de92007-09-25 17:54:57 -07007762/*****************************************************************************
7763 *
7764 * driver and module entry point
7765 *
7766 *****************************************************************************/
7767
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007768static struct pci_driver iwl3945_driver = {
Zhu Yib481de92007-09-25 17:54:57 -07007769 .name = DRV_NAME,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007770 .id_table = iwl3945_hw_card_ids,
7771 .probe = iwl3945_pci_probe,
7772 .remove = __devexit_p(iwl3945_pci_remove),
Zhu Yib481de92007-09-25 17:54:57 -07007773#ifdef CONFIG_PM
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007774 .suspend = iwl3945_pci_suspend,
7775 .resume = iwl3945_pci_resume,
Zhu Yib481de92007-09-25 17:54:57 -07007776#endif
7777};
7778
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007779static int __init iwl3945_init(void)
Zhu Yib481de92007-09-25 17:54:57 -07007780{
7781
7782 int ret;
7783 printk(KERN_INFO DRV_NAME ": " DRV_DESCRIPTION ", " DRV_VERSION "\n");
7784 printk(KERN_INFO DRV_NAME ": " DRV_COPYRIGHT "\n");
Reinette Chatre897e1cf2008-03-28 16:21:09 -07007785
7786 ret = iwl3945_rate_control_register();
7787 if (ret) {
Samuel Ortiza3139c52008-12-19 10:37:09 +08007788 printk(KERN_ERR DRV_NAME
7789 "Unable to register rate control algorithm: %d\n", ret);
Reinette Chatre897e1cf2008-03-28 16:21:09 -07007790 return ret;
7791 }
7792
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007793 ret = pci_register_driver(&iwl3945_driver);
Zhu Yib481de92007-09-25 17:54:57 -07007794 if (ret) {
Samuel Ortiza3139c52008-12-19 10:37:09 +08007795 printk(KERN_ERR DRV_NAME "Unable to initialize PCI module\n");
Reinette Chatre897e1cf2008-03-28 16:21:09 -07007796 goto error_register;
Zhu Yib481de92007-09-25 17:54:57 -07007797 }
Zhu Yib481de92007-09-25 17:54:57 -07007798
7799 return ret;
Reinette Chatre897e1cf2008-03-28 16:21:09 -07007800
Reinette Chatre897e1cf2008-03-28 16:21:09 -07007801error_register:
7802 iwl3945_rate_control_unregister();
7803 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07007804}
7805
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007806static void __exit iwl3945_exit(void)
Zhu Yib481de92007-09-25 17:54:57 -07007807{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007808 pci_unregister_driver(&iwl3945_driver);
Reinette Chatre897e1cf2008-03-28 16:21:09 -07007809 iwl3945_rate_control_unregister();
Zhu Yib481de92007-09-25 17:54:57 -07007810}
7811
Reinette Chatrea0987a82008-12-02 12:14:06 -08007812MODULE_FIRMWARE(IWL3945_MODULE_FIRMWARE(IWL3945_UCODE_API_MAX));
Zhu Yi25cb6ca2008-09-11 11:45:22 +08007813
Kolekar, Abhijeetdf878d82008-12-19 10:37:35 +08007814module_param_named(antenna, iwl3945_mod_params.antenna, int, 0444);
Zhu Yib481de92007-09-25 17:54:57 -07007815MODULE_PARM_DESC(antenna, "select antenna (1=Main, 2=Aux, default 0 [both])");
Kolekar, Abhijeetdf878d82008-12-19 10:37:35 +08007816module_param_named(disable, iwl3945_mod_params.disable, int, 0444);
Zhu Yib481de92007-09-25 17:54:57 -07007817MODULE_PARM_DESC(disable, "manually disable the radio (default 0 [radio on])");
Samuel Ortiz9c74d9f2009-01-08 10:19:59 -08007818module_param_named(swcrypto, iwl3945_mod_params.sw_crypto, int, 0444);
7819MODULE_PARM_DESC(swcrypto,
7820 "using software crypto (default 1 [software])\n");
Kolekar, Abhijeetdf878d82008-12-19 10:37:35 +08007821module_param_named(debug, iwl3945_mod_params.debug, uint, 0444);
Zhu Yib481de92007-09-25 17:54:57 -07007822MODULE_PARM_DESC(debug, "debug output mask");
Kolekar, Abhijeetdf878d82008-12-19 10:37:35 +08007823module_param_named(disable_hw_scan, iwl3945_mod_params.disable_hw_scan, int, 0444);
Zhu Yib481de92007-09-25 17:54:57 -07007824MODULE_PARM_DESC(disable_hw_scan, "disable hardware scanning (default 0)");
7825
Kolekar, Abhijeetdf878d82008-12-19 10:37:35 +08007826module_param_named(queues_num, iwl3945_mod_params.num_of_queues, int, 0444);
Zhu Yib481de92007-09-25 17:54:57 -07007827MODULE_PARM_DESC(queues_num, "number of hw queues.");
7828
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007829module_exit(iwl3945_exit);
7830module_init(iwl3945_init);