blob: 4474a4c3ddcd9ba0ca8bf0f8eb6d2fe1bdde4540 [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
Zhu Yib481de92007-09-25 17:54:57 -070059/*
60 * module name, copyright, version, etc.
Zhu Yib481de92007-09-25 17:54:57 -070061 */
62
63#define DRV_DESCRIPTION \
64"Intel(R) PRO/Wireless 3945ABG/BG Network Connection driver for Linux"
65
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +080066#ifdef CONFIG_IWL3945_DEBUG
Zhu Yib481de92007-09-25 17:54:57 -070067#define VD "d"
68#else
69#define VD
70#endif
71
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +080072#ifdef CONFIG_IWL3945_SPECTRUM_MEASUREMENT
Zhu Yib481de92007-09-25 17:54:57 -070073#define VS "s"
74#else
75#define VS
76#endif
77
Kolekar, Abhijeeteaa686c2008-12-19 10:37:17 +080078#define IWL39_VERSION "1.2.26k" VD VS
Reinette Chatre01f81622009-01-08 10:20:02 -080079#define DRV_COPYRIGHT "Copyright(c) 2003-2009 Intel Corporation"
Tomas Winklera7b75202008-12-11 10:33:41 -080080#define DRV_AUTHOR "<ilw@linux.intel.com>"
Kolekar, Abhijeeteaa686c2008-12-19 10:37:17 +080081#define DRV_VERSION IWL39_VERSION
Zhu Yib481de92007-09-25 17:54:57 -070082
Zhu Yib481de92007-09-25 17:54:57 -070083
84MODULE_DESCRIPTION(DRV_DESCRIPTION);
85MODULE_VERSION(DRV_VERSION);
Tomas Winklera7b75202008-12-11 10:33:41 -080086MODULE_AUTHOR(DRV_COPYRIGHT " " DRV_AUTHOR);
Zhu Yib481de92007-09-25 17:54:57 -070087MODULE_LICENSE("GPL");
88
Kolekar, Abhijeetdf878d82008-12-19 10:37:35 +080089 /* module parameters */
90struct iwl_mod_params iwl3945_mod_params = {
91 .num_of_queues = IWL39_MAX_NUM_QUEUES,
Samuel Ortiz9c74d9f2009-01-08 10:19:59 -080092 .sw_crypto = 1,
Kolekar, Abhijeetdf878d82008-12-19 10:37:35 +080093 /* the rest are 0 by default */
94};
95
Zhu Yib481de92007-09-25 17:54:57 -070096/*************** DMA-QUEUE-GENERAL-FUNCTIONS *****
97 * DMA services
98 *
99 * Theory of operation
100 *
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800101 * A Tx or Rx queue resides in host DRAM, and is comprised of a circular buffer
102 * of buffer descriptors, each of which points to one or more data buffers for
103 * the device to read from or fill. Driver and device exchange status of each
104 * queue via "read" and "write" pointers. Driver keeps minimum of 2 empty
105 * entries in each circular buffer, to protect against confusing empty and full
106 * queue states.
107 *
108 * The device reads or writes the data in the queues via the device's several
109 * DMA/FIFO channels. Each queue is mapped to a single DMA channel.
Zhu Yib481de92007-09-25 17:54:57 -0700110 *
111 * For Tx queue, there are low mark and high mark limits. If, after queuing
112 * the packet for Tx, free space become < low mark, Tx queue stopped. When
113 * reclaiming packets (on 'tx done IRQ), if free space become > high mark,
114 * Tx queue resumed.
115 *
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800116 * The 3945 operates with six queues: One receive queue, one transmit queue
117 * (#4) for sending commands to the device firmware, and four transmit queues
118 * (#0-3) for data tx via EDCA. An additional 2 HCCA queues are unused.
Zhu Yib481de92007-09-25 17:54:57 -0700119 ***************************************************/
120
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800121/**
122 * iwl3945_queue_init - Initialize queue's high/low-water and read/write indexes
123 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800124static int iwl3945_queue_init(struct iwl_priv *priv, struct iwl_queue *q,
Zhu Yib481de92007-09-25 17:54:57 -0700125 int count, int slots_num, u32 id)
126{
127 q->n_bd = count;
128 q->n_window = slots_num;
129 q->id = id;
130
Tomas Winklerc54b6792008-03-06 17:36:53 -0800131 /* count must be power-of-two size, otherwise iwl_queue_inc_wrap
132 * and iwl_queue_dec_wrap are broken. */
Zhu Yib481de92007-09-25 17:54:57 -0700133 BUG_ON(!is_power_of_2(count));
134
135 /* slots_num must be power-of-two size, otherwise
136 * get_cmd_index is broken. */
137 BUG_ON(!is_power_of_2(slots_num));
138
139 q->low_mark = q->n_window / 4;
140 if (q->low_mark < 4)
141 q->low_mark = 4;
142
143 q->high_mark = q->n_window / 8;
144 if (q->high_mark < 2)
145 q->high_mark = 2;
146
Tomas Winklerfc4b6852007-10-25 17:15:24 +0800147 q->write_ptr = q->read_ptr = 0;
Zhu Yib481de92007-09-25 17:54:57 -0700148
149 return 0;
150}
151
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800152/**
153 * iwl3945_tx_queue_alloc - Alloc driver data and TFD CB for one Tx/cmd queue
154 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800155static int iwl3945_tx_queue_alloc(struct iwl_priv *priv,
Samuel Ortiz188cf6c2008-12-22 11:31:16 +0800156 struct iwl_tx_queue *txq, u32 id)
Zhu Yib481de92007-09-25 17:54:57 -0700157{
158 struct pci_dev *dev = priv->pci_dev;
159
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800160 /* Driver private data, only for Tx (not command) queues,
161 * not shared with device. */
Zhu Yib481de92007-09-25 17:54:57 -0700162 if (id != IWL_CMD_QUEUE_NUM) {
163 txq->txb = kmalloc(sizeof(txq->txb[0]) *
164 TFD_QUEUE_SIZE_MAX, GFP_KERNEL);
165 if (!txq->txb) {
Winkler, Tomas15b16872008-12-19 10:37:33 +0800166 IWL_ERR(priv, "kmalloc for auxiliary BD "
Zhu Yib481de92007-09-25 17:54:57 -0700167 "structures failed\n");
168 goto error;
169 }
170 } else
171 txq->txb = NULL;
172
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800173 /* Circular buffer of transmit frame descriptors (TFDs),
174 * shared with device */
Samuel Ortiz188cf6c2008-12-22 11:31:16 +0800175 txq->tfds39 = pci_alloc_consistent(dev,
176 sizeof(txq->tfds39[0]) * TFD_QUEUE_SIZE_MAX,
Zhu Yib481de92007-09-25 17:54:57 -0700177 &txq->q.dma_addr);
178
Samuel Ortiz188cf6c2008-12-22 11:31:16 +0800179 if (!txq->tfds39) {
Winkler, Tomas15b16872008-12-19 10:37:33 +0800180 IWL_ERR(priv, "pci_alloc_consistent(%zd) failed\n",
Samuel Ortiz188cf6c2008-12-22 11:31:16 +0800181 sizeof(txq->tfds39[0]) * TFD_QUEUE_SIZE_MAX);
Zhu Yib481de92007-09-25 17:54:57 -0700182 goto error;
183 }
184 txq->q.id = id;
185
186 return 0;
187
188 error:
Tomas Winkler3ac7f142008-07-21 02:40:14 +0300189 kfree(txq->txb);
190 txq->txb = NULL;
Zhu Yib481de92007-09-25 17:54:57 -0700191
192 return -ENOMEM;
193}
194
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800195/**
196 * iwl3945_tx_queue_init - Allocate and initialize one tx/cmd queue
197 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800198int iwl3945_tx_queue_init(struct iwl_priv *priv,
Samuel Ortiz188cf6c2008-12-22 11:31:16 +0800199 struct iwl_tx_queue *txq, int slots_num, u32 txq_id)
Zhu Yib481de92007-09-25 17:54:57 -0700200{
Samuel Ortiz188cf6c2008-12-22 11:31:16 +0800201 int len, i;
Zhu Yib481de92007-09-25 17:54:57 -0700202 int rc = 0;
203
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800204 /*
205 * Alloc buffer array for commands (Tx or other types of commands).
206 * For the command queue (#4), allocate command space + one big
207 * command for scan, since scan command is very huge; the system will
208 * not have two scans at the same time, so only one is needed.
209 * For data Tx queues (all other queues), no super-size command
210 * space is needed.
211 */
Samuel Ortiz188cf6c2008-12-22 11:31:16 +0800212 len = sizeof(struct iwl_cmd);
213 for (i = 0; i <= slots_num; i++) {
214 if (i == slots_num) {
215 if (txq_id == IWL_CMD_QUEUE_NUM)
216 len += IWL_MAX_SCAN_SIZE;
217 else
218 continue;
219 }
220
221 txq->cmd[i] = kmalloc(len, GFP_KERNEL);
222 if (!txq->cmd[i])
223 goto err;
224 }
Zhu Yib481de92007-09-25 17:54:57 -0700225
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800226 /* Alloc driver data array and TFD circular buffer */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800227 rc = iwl3945_tx_queue_alloc(priv, txq, txq_id);
Samuel Ortiz188cf6c2008-12-22 11:31:16 +0800228 if (rc)
229 goto err;
Zhu Yib481de92007-09-25 17:54:57 -0700230
Zhu Yib481de92007-09-25 17:54:57 -0700231 txq->need_update = 0;
232
233 /* TFD_QUEUE_SIZE_MAX must be power-of-two size, otherwise
Tomas Winklerc54b6792008-03-06 17:36:53 -0800234 * iwl_queue_inc_wrap and iwl_queue_dec_wrap are broken. */
Zhu Yib481de92007-09-25 17:54:57 -0700235 BUILD_BUG_ON(TFD_QUEUE_SIZE_MAX & (TFD_QUEUE_SIZE_MAX - 1));
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800236
237 /* Initialize queue high/low-water, head/tail indexes */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800238 iwl3945_queue_init(priv, &txq->q, TFD_QUEUE_SIZE_MAX, slots_num, txq_id);
Zhu Yib481de92007-09-25 17:54:57 -0700239
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800240 /* Tell device where to find queue, enable DMA channel. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800241 iwl3945_hw_tx_queue_init(priv, txq);
Zhu Yib481de92007-09-25 17:54:57 -0700242
243 return 0;
Samuel Ortiz188cf6c2008-12-22 11:31:16 +0800244err:
245 for (i = 0; i < slots_num; i++) {
246 kfree(txq->cmd[i]);
247 txq->cmd[i] = NULL;
248 }
249
250 if (txq_id == IWL_CMD_QUEUE_NUM) {
251 kfree(txq->cmd[slots_num]);
252 txq->cmd[slots_num] = NULL;
253 }
254 return -ENOMEM;
Zhu Yib481de92007-09-25 17:54:57 -0700255}
256
257/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800258 * iwl3945_tx_queue_free - Deallocate DMA queue.
Zhu Yib481de92007-09-25 17:54:57 -0700259 * @txq: Transmit queue to deallocate.
260 *
261 * Empty queue by removing and destroying all BD's.
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800262 * Free all buffers.
263 * 0-fill, but do not free "txq" descriptor structure.
Zhu Yib481de92007-09-25 17:54:57 -0700264 */
Samuel Ortiz188cf6c2008-12-22 11:31:16 +0800265void iwl3945_tx_queue_free(struct iwl_priv *priv, struct iwl_tx_queue *txq)
Zhu Yib481de92007-09-25 17:54:57 -0700266{
Samuel Ortizd20b3c62008-12-19 10:37:15 +0800267 struct iwl_queue *q = &txq->q;
Zhu Yib481de92007-09-25 17:54:57 -0700268 struct pci_dev *dev = priv->pci_dev;
Samuel Ortiz188cf6c2008-12-22 11:31:16 +0800269 int len, i;
Zhu Yib481de92007-09-25 17:54:57 -0700270
271 if (q->n_bd == 0)
272 return;
273
274 /* first, empty all BD's */
Tomas Winklerfc4b6852007-10-25 17:15:24 +0800275 for (; q->write_ptr != q->read_ptr;
Tomas Winklerc54b6792008-03-06 17:36:53 -0800276 q->read_ptr = iwl_queue_inc_wrap(q->read_ptr, q->n_bd))
Samuel Ortiz7aaa1d72009-01-19 15:30:26 -0800277 priv->cfg->ops->lib->txq_free_tfd(priv, txq);
Zhu Yib481de92007-09-25 17:54:57 -0700278
Winkler, Tomasc2d79b42008-12-19 10:37:34 +0800279 len = sizeof(struct iwl_cmd) * q->n_window;
Zhu Yib481de92007-09-25 17:54:57 -0700280 if (q->id == IWL_CMD_QUEUE_NUM)
281 len += IWL_MAX_SCAN_SIZE;
282
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800283 /* De-alloc array of command/tx buffers */
Samuel Ortiz188cf6c2008-12-22 11:31:16 +0800284 for (i = 0; i < TFD_TX_CMD_SLOTS; i++)
285 kfree(txq->cmd[i]);
Zhu Yib481de92007-09-25 17:54:57 -0700286
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800287 /* De-alloc circular buffer of TFDs */
Zhu Yib481de92007-09-25 17:54:57 -0700288 if (txq->q.n_bd)
Winkler, Tomasdbb66542008-12-22 11:31:14 +0800289 pci_free_consistent(dev, sizeof(struct iwl3945_tfd) *
Samuel Ortiz188cf6c2008-12-22 11:31:16 +0800290 txq->q.n_bd, txq->tfds39, txq->q.dma_addr);
Zhu Yib481de92007-09-25 17:54:57 -0700291
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800292 /* De-alloc array of per-TFD driver data */
Tomas Winkler3ac7f142008-07-21 02:40:14 +0300293 kfree(txq->txb);
294 txq->txb = NULL;
Zhu Yib481de92007-09-25 17:54:57 -0700295
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800296 /* 0-fill queue descriptor structure */
Zhu Yib481de92007-09-25 17:54:57 -0700297 memset(txq, 0, sizeof(*txq));
298}
299
Zhu Yib481de92007-09-25 17:54:57 -0700300/*************** STATION TABLE MANAGEMENT ****
Ben Cahill9fbab512007-11-29 11:09:47 +0800301 * mac80211 should be examined to determine if sta_info is duplicating
Zhu Yib481de92007-09-25 17:54:57 -0700302 * the functionality provided here
303 */
304
305/**************************************************************/
Ian Schram01ebd062007-10-25 17:15:22 +0800306#if 0 /* temporary disable till we add real remove station */
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800307/**
308 * iwl3945_remove_station - Remove driver's knowledge of station.
309 *
310 * NOTE: This does not remove station from device's station table.
311 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800312static u8 iwl3945_remove_station(struct iwl_priv *priv, const u8 *addr, int is_ap)
Zhu Yib481de92007-09-25 17:54:57 -0700313{
314 int index = IWL_INVALID_STATION;
315 int i;
316 unsigned long flags;
317
318 spin_lock_irqsave(&priv->sta_lock, flags);
319
320 if (is_ap)
321 index = IWL_AP_ID;
322 else if (is_broadcast_ether_addr(addr))
Abhijeet Kolekar3832ec92008-12-19 10:37:26 +0800323 index = priv->hw_params.bcast_sta_id;
Zhu Yib481de92007-09-25 17:54:57 -0700324 else
Abhijeet Kolekar3832ec92008-12-19 10:37:26 +0800325 for (i = IWL_STA_ID; i < priv->hw_params.max_stations; i++)
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800326 if (priv->stations_39[i].used &&
327 !compare_ether_addr(priv->stations_39[i].sta.sta.addr,
Zhu Yib481de92007-09-25 17:54:57 -0700328 addr)) {
329 index = i;
330 break;
331 }
332
333 if (unlikely(index == IWL_INVALID_STATION))
334 goto out;
335
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800336 if (priv->stations_39[index].used) {
337 priv->stations_39[index].used = 0;
Zhu Yib481de92007-09-25 17:54:57 -0700338 priv->num_stations--;
339 }
340
341 BUG_ON(priv->num_stations < 0);
342
343out:
344 spin_unlock_irqrestore(&priv->sta_lock, flags);
345 return 0;
346}
Zhu Yi556f8db2007-09-27 11:27:33 +0800347#endif
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800348
349/**
350 * iwl3945_clear_stations_table - Clear the driver's station table
351 *
352 * NOTE: This does not clear or otherwise alter the device's station table.
353 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800354static void iwl3945_clear_stations_table(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700355{
356 unsigned long flags;
357
358 spin_lock_irqsave(&priv->sta_lock, flags);
359
360 priv->num_stations = 0;
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800361 memset(priv->stations_39, 0, sizeof(priv->stations_39));
Zhu Yib481de92007-09-25 17:54:57 -0700362
363 spin_unlock_irqrestore(&priv->sta_lock, flags);
364}
365
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800366/**
367 * iwl3945_add_station - Add station to station tables in driver and device
368 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800369u8 iwl3945_add_station(struct iwl_priv *priv, const u8 *addr, int is_ap, u8 flags)
Zhu Yib481de92007-09-25 17:54:57 -0700370{
371 int i;
372 int index = IWL_INVALID_STATION;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800373 struct iwl3945_station_entry *station;
Zhu Yib481de92007-09-25 17:54:57 -0700374 unsigned long flags_spin;
Zhu Yic14c5212007-09-27 11:27:35 +0800375 u8 rate;
Zhu Yib481de92007-09-25 17:54:57 -0700376
377 spin_lock_irqsave(&priv->sta_lock, flags_spin);
378 if (is_ap)
379 index = IWL_AP_ID;
380 else if (is_broadcast_ether_addr(addr))
Abhijeet Kolekar3832ec92008-12-19 10:37:26 +0800381 index = priv->hw_params.bcast_sta_id;
Zhu Yib481de92007-09-25 17:54:57 -0700382 else
Abhijeet Kolekar3832ec92008-12-19 10:37:26 +0800383 for (i = IWL_STA_ID; i < priv->hw_params.max_stations; i++) {
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800384 if (!compare_ether_addr(priv->stations_39[i].sta.sta.addr,
Zhu Yib481de92007-09-25 17:54:57 -0700385 addr)) {
386 index = i;
387 break;
388 }
389
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800390 if (!priv->stations_39[i].used &&
Zhu Yib481de92007-09-25 17:54:57 -0700391 index == IWL_INVALID_STATION)
392 index = i;
393 }
394
Ian Schram01ebd062007-10-25 17:15:22 +0800395 /* These two conditions has the same outcome but keep them separate
Zhu Yib481de92007-09-25 17:54:57 -0700396 since they have different meaning */
397 if (unlikely(index == IWL_INVALID_STATION)) {
398 spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
399 return index;
400 }
401
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800402 if (priv->stations_39[index].used &&
403 !compare_ether_addr(priv->stations_39[index].sta.sta.addr, addr)) {
Zhu Yib481de92007-09-25 17:54:57 -0700404 spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
405 return index;
406 }
407
Johannes Berge1749612008-10-27 15:59:26 -0700408 IWL_DEBUG_ASSOC("Add STA ID %d: %pM\n", index, addr);
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800409 station = &priv->stations_39[index];
Zhu Yib481de92007-09-25 17:54:57 -0700410 station->used = 1;
411 priv->num_stations++;
412
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800413 /* Set up the REPLY_ADD_STA command to send to device */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800414 memset(&station->sta, 0, sizeof(struct iwl3945_addsta_cmd));
Zhu Yib481de92007-09-25 17:54:57 -0700415 memcpy(station->sta.sta.addr, addr, ETH_ALEN);
416 station->sta.mode = 0;
417 station->sta.sta.sta_id = index;
418 station->sta.station_flags = 0;
419
Johannes Berg8318d782008-01-24 19:38:38 +0100420 if (priv->band == IEEE80211_BAND_5GHZ)
Tomas Winkler69946332007-10-25 17:15:27 +0800421 rate = IWL_RATE_6M_PLCP;
422 else
423 rate = IWL_RATE_1M_PLCP;
Zhu Yic14c5212007-09-27 11:27:35 +0800424
425 /* Turn on both antennas for the station... */
426 station->sta.rate_n_flags =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800427 iwl3945_hw_set_rate_n_flags(rate, RATE_MCS_ANT_AB_MSK);
Zhu Yic14c5212007-09-27 11:27:35 +0800428
Zhu Yib481de92007-09-25 17:54:57 -0700429 spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800430
431 /* Add station to device's station table */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800432 iwl3945_send_add_station(priv, &station->sta, flags);
Zhu Yib481de92007-09-25 17:54:57 -0700433 return index;
434
435}
436
Zhu Yib481de92007-09-25 17:54:57 -0700437
438/*************** HOST COMMAND QUEUE FUNCTIONS *****/
439
Abhijeet Kolekarc3056062008-11-12 13:14:08 -0800440#define IWL_CMD(x) case x: return #x
Zhu Yib481de92007-09-25 17:54:57 -0700441#define HOST_COMPLETE_TIMEOUT (HZ / 2)
442
443/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800444 * iwl3945_enqueue_hcmd - enqueue a uCode command
Zhu Yib481de92007-09-25 17:54:57 -0700445 * @priv: device private data point
446 * @cmd: a point to the ucode command structure
447 *
448 * The function returns < 0 values to indicate the operation is
449 * failed. On success, it turns the index (> 0) of command in the
450 * command queue.
451 */
Winkler, Tomasc2d79b42008-12-19 10:37:34 +0800452static int iwl3945_enqueue_hcmd(struct iwl_priv *priv, struct iwl_host_cmd *cmd)
Zhu Yib481de92007-09-25 17:54:57 -0700453{
Samuel Ortiz188cf6c2008-12-22 11:31:16 +0800454 struct iwl_tx_queue *txq = &priv->txq[IWL_CMD_QUEUE_NUM];
Samuel Ortizd20b3c62008-12-19 10:37:15 +0800455 struct iwl_queue *q = &txq->q;
Winkler, Tomasc2d79b42008-12-19 10:37:34 +0800456 struct iwl_cmd *out_cmd;
Zhu Yib481de92007-09-25 17:54:57 -0700457 u32 idx;
458 u16 fix_size = (u16)(cmd->len + sizeof(out_cmd->hdr));
459 dma_addr_t phys_addr;
Samuel Ortiz188cf6c2008-12-22 11:31:16 +0800460 int ret, len;
Zhu Yib481de92007-09-25 17:54:57 -0700461 unsigned long flags;
462
463 /* If any of the command structures end up being larger than
464 * the TFD_MAX_PAYLOAD_SIZE, and it sent as a 'small' command then
465 * we will need to increase the size of the TFD entries */
Winkler, Tomasc2d79b42008-12-19 10:37:34 +0800466 BUG_ON((fix_size > TFD_MAX_PAYLOAD_SIZE) &&
Zhu Yib481de92007-09-25 17:54:57 -0700467 !(cmd->meta.flags & CMD_SIZE_HUGE));
468
Gregory Greenmanc342a1b2008-02-06 11:20:40 -0800469
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +0800470 if (iwl_is_rfkill(priv)) {
Gregory Greenmanc342a1b2008-02-06 11:20:40 -0800471 IWL_DEBUG_INFO("Not sending command - RF KILL");
472 return -EIO;
473 }
474
Samuel Ortizd20b3c62008-12-19 10:37:15 +0800475 if (iwl_queue_space(q) < ((cmd->meta.flags & CMD_ASYNC) ? 2 : 1)) {
Winkler, Tomas15b16872008-12-19 10:37:33 +0800476 IWL_ERR(priv, "No space for Tx\n");
Zhu Yib481de92007-09-25 17:54:57 -0700477 return -ENOSPC;
478 }
479
480 spin_lock_irqsave(&priv->hcmd_lock, flags);
481
Tomas Winklerfc4b6852007-10-25 17:15:24 +0800482 idx = get_cmd_index(q, q->write_ptr, cmd->meta.flags & CMD_SIZE_HUGE);
Samuel Ortiz188cf6c2008-12-22 11:31:16 +0800483 out_cmd = txq->cmd[idx];
Zhu Yib481de92007-09-25 17:54:57 -0700484
485 out_cmd->hdr.cmd = cmd->id;
486 memcpy(&out_cmd->meta, &cmd->meta, sizeof(cmd->meta));
487 memcpy(&out_cmd->cmd.payload, cmd->data, cmd->len);
488
489 /* At this point, the out_cmd now has all of the incoming cmd
490 * information */
491
492 out_cmd->hdr.flags = 0;
493 out_cmd->hdr.sequence = cpu_to_le16(QUEUE_TO_SEQ(IWL_CMD_QUEUE_NUM) |
Tomas Winklerfc4b6852007-10-25 17:15:24 +0800494 INDEX_TO_SEQ(q->write_ptr));
Zhu Yib481de92007-09-25 17:54:57 -0700495 if (out_cmd->meta.flags & CMD_SIZE_HUGE)
Tomas Winkler600c0e12008-12-19 10:37:04 +0800496 out_cmd->hdr.sequence |= SEQ_HUGE_FRAME;
Zhu Yib481de92007-09-25 17:54:57 -0700497
Samuel Ortiz188cf6c2008-12-22 11:31:16 +0800498 len = (idx == TFD_CMD_SLOTS) ?
499 IWL_MAX_SCAN_SIZE : sizeof(struct iwl_cmd);
500
501 phys_addr = pci_map_single(priv->pci_dev, out_cmd,
502 len, PCI_DMA_TODEVICE);
503 pci_unmap_addr_set(&out_cmd->meta, mapping, phys_addr);
504 pci_unmap_len_set(&out_cmd->meta, len, len);
505 phys_addr += offsetof(struct iwl_cmd, hdr);
506
Samuel Ortiz7aaa1d72009-01-19 15:30:26 -0800507 priv->cfg->ops->lib->txq_attach_buf_to_tfd(priv, txq,
508 phys_addr, fix_size,
509 1, U32_PAD(cmd->len));
Zhu Yib481de92007-09-25 17:54:57 -0700510
511 IWL_DEBUG_HC("Sending command %s (#%x), seq: 0x%04X, "
512 "%d bytes at %d[%d]:%d\n",
513 get_cmd_string(out_cmd->hdr.cmd),
514 out_cmd->hdr.cmd, le16_to_cpu(out_cmd->hdr.sequence),
Tomas Winklerfc4b6852007-10-25 17:15:24 +0800515 fix_size, q->write_ptr, idx, IWL_CMD_QUEUE_NUM);
Zhu Yib481de92007-09-25 17:54:57 -0700516
517 txq->need_update = 1;
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800518
519 /* Increment and update queue's write index */
Tomas Winklerc54b6792008-03-06 17:36:53 -0800520 q->write_ptr = iwl_queue_inc_wrap(q->write_ptr, q->n_bd);
Samuel Ortiz4f3602c2009-01-19 15:30:25 -0800521 ret = iwl_txq_update_write_ptr(priv, txq);
Zhu Yib481de92007-09-25 17:54:57 -0700522
523 spin_unlock_irqrestore(&priv->hcmd_lock, flags);
524 return ret ? ret : idx;
525}
526
Winkler, Tomasc2d79b42008-12-19 10:37:34 +0800527static int iwl3945_send_cmd_async(struct iwl_priv *priv,
528 struct iwl_host_cmd *cmd)
Zhu Yib481de92007-09-25 17:54:57 -0700529{
530 int ret;
531
532 BUG_ON(!(cmd->meta.flags & CMD_ASYNC));
533
534 /* An asynchronous command can not expect an SKB to be set. */
535 BUG_ON(cmd->meta.flags & CMD_WANT_SKB);
536
537 /* An asynchronous command MUST have a callback. */
538 BUG_ON(!cmd->meta.u.callback);
539
540 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
541 return -EBUSY;
542
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800543 ret = iwl3945_enqueue_hcmd(priv, cmd);
Zhu Yib481de92007-09-25 17:54:57 -0700544 if (ret < 0) {
Winkler, Tomas15b16872008-12-19 10:37:33 +0800545 IWL_ERR(priv,
546 "Error sending %s: iwl3945_enqueue_hcmd failed: %d\n",
547 get_cmd_string(cmd->id), ret);
Zhu Yib481de92007-09-25 17:54:57 -0700548 return ret;
549 }
550 return 0;
551}
552
Winkler, Tomasc2d79b42008-12-19 10:37:34 +0800553static int iwl3945_send_cmd_sync(struct iwl_priv *priv,
554 struct iwl_host_cmd *cmd)
Zhu Yib481de92007-09-25 17:54:57 -0700555{
556 int cmd_idx;
557 int ret;
Zhu Yib481de92007-09-25 17:54:57 -0700558
559 BUG_ON(cmd->meta.flags & CMD_ASYNC);
560
561 /* A synchronous command can not have a callback set. */
562 BUG_ON(cmd->meta.u.callback != NULL);
563
Tomas Winklere5472972008-03-28 16:21:12 -0700564 if (test_and_set_bit(STATUS_HCMD_SYNC_ACTIVE, &priv->status)) {
Winkler, Tomas15b16872008-12-19 10:37:33 +0800565 IWL_ERR(priv,
566 "Error sending %s: Already sending a host command\n",
567 get_cmd_string(cmd->id));
Tomas Winklere5472972008-03-28 16:21:12 -0700568 ret = -EBUSY;
569 goto out;
Zhu Yib481de92007-09-25 17:54:57 -0700570 }
571
572 set_bit(STATUS_HCMD_ACTIVE, &priv->status);
573
574 if (cmd->meta.flags & CMD_WANT_SKB)
575 cmd->meta.source = &cmd->meta;
576
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800577 cmd_idx = iwl3945_enqueue_hcmd(priv, cmd);
Zhu Yib481de92007-09-25 17:54:57 -0700578 if (cmd_idx < 0) {
579 ret = cmd_idx;
Winkler, Tomas15b16872008-12-19 10:37:33 +0800580 IWL_ERR(priv,
581 "Error sending %s: iwl3945_enqueue_hcmd failed: %d\n",
582 get_cmd_string(cmd->id), ret);
Zhu Yib481de92007-09-25 17:54:57 -0700583 goto out;
584 }
585
586 ret = wait_event_interruptible_timeout(priv->wait_command_queue,
587 !test_bit(STATUS_HCMD_ACTIVE, &priv->status),
588 HOST_COMPLETE_TIMEOUT);
589 if (!ret) {
590 if (test_bit(STATUS_HCMD_ACTIVE, &priv->status)) {
Winkler, Tomas15b16872008-12-19 10:37:33 +0800591 IWL_ERR(priv, "Error sending %s: time out after %dms\n",
Zhu Yib481de92007-09-25 17:54:57 -0700592 get_cmd_string(cmd->id),
593 jiffies_to_msecs(HOST_COMPLETE_TIMEOUT));
594
595 clear_bit(STATUS_HCMD_ACTIVE, &priv->status);
596 ret = -ETIMEDOUT;
597 goto cancel;
598 }
599 }
600
601 if (test_bit(STATUS_RF_KILL_HW, &priv->status)) {
602 IWL_DEBUG_INFO("Command %s aborted: RF KILL Switch\n",
603 get_cmd_string(cmd->id));
604 ret = -ECANCELED;
605 goto fail;
606 }
607 if (test_bit(STATUS_FW_ERROR, &priv->status)) {
608 IWL_DEBUG_INFO("Command %s failed: FW Error\n",
609 get_cmd_string(cmd->id));
610 ret = -EIO;
611 goto fail;
612 }
613 if ((cmd->meta.flags & CMD_WANT_SKB) && !cmd->meta.u.skb) {
Winkler, Tomas15b16872008-12-19 10:37:33 +0800614 IWL_ERR(priv, "Error: Response NULL in '%s'\n",
Zhu Yib481de92007-09-25 17:54:57 -0700615 get_cmd_string(cmd->id));
616 ret = -EIO;
Zhu Yi73e1a652009-01-08 10:19:58 -0800617 goto cancel;
Zhu Yib481de92007-09-25 17:54:57 -0700618 }
619
620 ret = 0;
621 goto out;
622
623cancel:
624 if (cmd->meta.flags & CMD_WANT_SKB) {
Winkler, Tomasc2d79b42008-12-19 10:37:34 +0800625 struct iwl_cmd *qcmd;
Zhu Yib481de92007-09-25 17:54:57 -0700626
627 /* Cancel the CMD_WANT_SKB flag for the cmd in the
628 * TX cmd queue. Otherwise in case the cmd comes
629 * in later, it will possibly set an invalid
630 * address (cmd->meta.source). */
Samuel Ortiz188cf6c2008-12-22 11:31:16 +0800631 qcmd = priv->txq[IWL_CMD_QUEUE_NUM].cmd[cmd_idx];
Zhu Yib481de92007-09-25 17:54:57 -0700632 qcmd->meta.flags &= ~CMD_WANT_SKB;
633 }
634fail:
635 if (cmd->meta.u.skb) {
636 dev_kfree_skb_any(cmd->meta.u.skb);
637 cmd->meta.u.skb = NULL;
638 }
639out:
Tomas Winklere5472972008-03-28 16:21:12 -0700640 clear_bit(STATUS_HCMD_SYNC_ACTIVE, &priv->status);
Zhu Yib481de92007-09-25 17:54:57 -0700641 return ret;
642}
643
Winkler, Tomasc2d79b42008-12-19 10:37:34 +0800644int iwl3945_send_cmd(struct iwl_priv *priv, struct iwl_host_cmd *cmd)
Zhu Yib481de92007-09-25 17:54:57 -0700645{
Zhu Yib481de92007-09-25 17:54:57 -0700646 if (cmd->meta.flags & CMD_ASYNC)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800647 return iwl3945_send_cmd_async(priv, cmd);
Zhu Yib481de92007-09-25 17:54:57 -0700648
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800649 return iwl3945_send_cmd_sync(priv, cmd);
Zhu Yib481de92007-09-25 17:54:57 -0700650}
651
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800652int iwl3945_send_cmd_pdu(struct iwl_priv *priv, u8 id, u16 len, const void *data)
Zhu Yib481de92007-09-25 17:54:57 -0700653{
Winkler, Tomasc2d79b42008-12-19 10:37:34 +0800654 struct iwl_host_cmd cmd = {
Zhu Yib481de92007-09-25 17:54:57 -0700655 .id = id,
656 .len = len,
657 .data = data,
658 };
659
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800660 return iwl3945_send_cmd_sync(priv, &cmd);
Zhu Yib481de92007-09-25 17:54:57 -0700661}
662
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800663static int __must_check iwl3945_send_cmd_u32(struct iwl_priv *priv, u8 id, u32 val)
Zhu Yib481de92007-09-25 17:54:57 -0700664{
Winkler, Tomasc2d79b42008-12-19 10:37:34 +0800665 struct iwl_host_cmd cmd = {
Zhu Yib481de92007-09-25 17:54:57 -0700666 .id = id,
667 .len = sizeof(val),
668 .data = &val,
669 };
670
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800671 return iwl3945_send_cmd_sync(priv, &cmd);
Zhu Yib481de92007-09-25 17:54:57 -0700672}
673
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800674int iwl3945_send_statistics_request(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700675{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800676 return iwl3945_send_cmd_u32(priv, REPLY_STATISTICS_CMD, 0);
Zhu Yib481de92007-09-25 17:54:57 -0700677}
678
679/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800680 * iwl3945_set_rxon_channel - Set the phymode and channel values in staging RXON
Johannes Berg8318d782008-01-24 19:38:38 +0100681 * @band: 2.4 or 5 GHz band
682 * @channel: Any channel valid for the requested band
Zhu Yib481de92007-09-25 17:54:57 -0700683
Johannes Berg8318d782008-01-24 19:38:38 +0100684 * In addition to setting the staging RXON, priv->band is also set.
Zhu Yib481de92007-09-25 17:54:57 -0700685 *
686 * NOTE: Does not commit to the hardware; it sets appropriate bit fields
Johannes Berg8318d782008-01-24 19:38:38 +0100687 * in the staging RXON flag structure based on the band
Zhu Yib481de92007-09-25 17:54:57 -0700688 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800689static int iwl3945_set_rxon_channel(struct iwl_priv *priv,
Johannes Berg8318d782008-01-24 19:38:38 +0100690 enum ieee80211_band band,
691 u16 channel)
Zhu Yib481de92007-09-25 17:54:57 -0700692{
Johannes Berg8318d782008-01-24 19:38:38 +0100693 if (!iwl3945_get_channel_info(priv, band, channel)) {
Zhu Yib481de92007-09-25 17:54:57 -0700694 IWL_DEBUG_INFO("Could not set channel to %d [%d]\n",
Johannes Berg8318d782008-01-24 19:38:38 +0100695 channel, band);
Zhu Yib481de92007-09-25 17:54:57 -0700696 return -EINVAL;
697 }
698
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800699 if ((le16_to_cpu(priv->staging39_rxon.channel) == channel) &&
Johannes Berg8318d782008-01-24 19:38:38 +0100700 (priv->band == band))
Zhu Yib481de92007-09-25 17:54:57 -0700701 return 0;
702
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800703 priv->staging39_rxon.channel = cpu_to_le16(channel);
Johannes Berg8318d782008-01-24 19:38:38 +0100704 if (band == IEEE80211_BAND_5GHZ)
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800705 priv->staging39_rxon.flags &= ~RXON_FLG_BAND_24G_MSK;
Zhu Yib481de92007-09-25 17:54:57 -0700706 else
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800707 priv->staging39_rxon.flags |= RXON_FLG_BAND_24G_MSK;
Zhu Yib481de92007-09-25 17:54:57 -0700708
Johannes Berg8318d782008-01-24 19:38:38 +0100709 priv->band = band;
Zhu Yib481de92007-09-25 17:54:57 -0700710
Johannes Berg8318d782008-01-24 19:38:38 +0100711 IWL_DEBUG_INFO("Staging channel set to %d [%d]\n", channel, band);
Zhu Yib481de92007-09-25 17:54:57 -0700712
713 return 0;
714}
715
716/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800717 * iwl3945_check_rxon_cmd - validate RXON structure is valid
Zhu Yib481de92007-09-25 17:54:57 -0700718 *
719 * NOTE: This is really only useful during development and can eventually
720 * be #ifdef'd out once the driver is stable and folks aren't actively
721 * making changes
722 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800723static int iwl3945_check_rxon_cmd(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700724{
725 int error = 0;
726 int counter = 1;
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800727 struct iwl3945_rxon_cmd *rxon = &priv->staging39_rxon;
Zhu Yib481de92007-09-25 17:54:57 -0700728
729 if (rxon->flags & RXON_FLG_BAND_24G_MSK) {
730 error |= le32_to_cpu(rxon->flags &
731 (RXON_FLG_TGJ_NARROW_BAND_MSK |
732 RXON_FLG_RADAR_DETECT_MSK));
733 if (error)
Winkler, Tomas39aadf82008-12-19 10:37:32 +0800734 IWL_WARN(priv, "check 24G fields %d | %d\n",
Zhu Yib481de92007-09-25 17:54:57 -0700735 counter++, error);
736 } else {
737 error |= (rxon->flags & RXON_FLG_SHORT_SLOT_MSK) ?
738 0 : le32_to_cpu(RXON_FLG_SHORT_SLOT_MSK);
739 if (error)
Winkler, Tomas39aadf82008-12-19 10:37:32 +0800740 IWL_WARN(priv, "check 52 fields %d | %d\n",
Zhu Yib481de92007-09-25 17:54:57 -0700741 counter++, error);
742 error |= le32_to_cpu(rxon->flags & RXON_FLG_CCK_MSK);
743 if (error)
Winkler, Tomas39aadf82008-12-19 10:37:32 +0800744 IWL_WARN(priv, "check 52 CCK %d | %d\n",
Zhu Yib481de92007-09-25 17:54:57 -0700745 counter++, error);
746 }
747 error |= (rxon->node_addr[0] | rxon->bssid_addr[0]) & 0x1;
748 if (error)
Winkler, Tomas39aadf82008-12-19 10:37:32 +0800749 IWL_WARN(priv, "check mac addr %d | %d\n", counter++, error);
Zhu Yib481de92007-09-25 17:54:57 -0700750
751 /* make sure basic rates 6Mbps and 1Mbps are supported */
752 error |= (((rxon->ofdm_basic_rates & IWL_RATE_6M_MASK) == 0) &&
753 ((rxon->cck_basic_rates & IWL_RATE_1M_MASK) == 0));
754 if (error)
Winkler, Tomas39aadf82008-12-19 10:37:32 +0800755 IWL_WARN(priv, "check basic rate %d | %d\n", counter++, error);
Zhu Yib481de92007-09-25 17:54:57 -0700756
757 error |= (le16_to_cpu(rxon->assoc_id) > 2007);
758 if (error)
Winkler, Tomas39aadf82008-12-19 10:37:32 +0800759 IWL_WARN(priv, "check assoc id %d | %d\n", counter++, error);
Zhu Yib481de92007-09-25 17:54:57 -0700760
761 error |= ((rxon->flags & (RXON_FLG_CCK_MSK | RXON_FLG_SHORT_SLOT_MSK))
762 == (RXON_FLG_CCK_MSK | RXON_FLG_SHORT_SLOT_MSK));
763 if (error)
Winkler, Tomas39aadf82008-12-19 10:37:32 +0800764 IWL_WARN(priv, "check CCK and short slot %d | %d\n",
Zhu Yib481de92007-09-25 17:54:57 -0700765 counter++, error);
766
767 error |= ((rxon->flags & (RXON_FLG_CCK_MSK | RXON_FLG_AUTO_DETECT_MSK))
768 == (RXON_FLG_CCK_MSK | RXON_FLG_AUTO_DETECT_MSK));
769 if (error)
Winkler, Tomas39aadf82008-12-19 10:37:32 +0800770 IWL_WARN(priv, "check CCK & auto detect %d | %d\n",
Zhu Yib481de92007-09-25 17:54:57 -0700771 counter++, error);
772
773 error |= ((rxon->flags & (RXON_FLG_AUTO_DETECT_MSK |
774 RXON_FLG_TGG_PROTECT_MSK)) == RXON_FLG_TGG_PROTECT_MSK);
775 if (error)
Winkler, Tomas39aadf82008-12-19 10:37:32 +0800776 IWL_WARN(priv, "check TGG and auto detect %d | %d\n",
Zhu Yib481de92007-09-25 17:54:57 -0700777 counter++, error);
778
779 if ((rxon->flags & RXON_FLG_DIS_DIV_MSK))
780 error |= ((rxon->flags & (RXON_FLG_ANT_B_MSK |
781 RXON_FLG_ANT_A_MSK)) == 0);
782 if (error)
Winkler, Tomas39aadf82008-12-19 10:37:32 +0800783 IWL_WARN(priv, "check antenna %d %d\n", counter++, error);
Zhu Yib481de92007-09-25 17:54:57 -0700784
785 if (error)
Winkler, Tomas39aadf82008-12-19 10:37:32 +0800786 IWL_WARN(priv, "Tuning to channel %d\n",
Zhu Yib481de92007-09-25 17:54:57 -0700787 le16_to_cpu(rxon->channel));
788
789 if (error) {
Winkler, Tomas15b16872008-12-19 10:37:33 +0800790 IWL_ERR(priv, "Not a valid rxon_assoc_cmd field values\n");
Zhu Yib481de92007-09-25 17:54:57 -0700791 return -1;
792 }
793 return 0;
794}
795
796/**
Ben Cahill9fbab512007-11-29 11:09:47 +0800797 * iwl3945_full_rxon_required - check if full RXON (vs RXON_ASSOC) cmd is needed
Ian Schram01ebd062007-10-25 17:15:22 +0800798 * @priv: staging_rxon is compared to active_rxon
Zhu Yib481de92007-09-25 17:54:57 -0700799 *
Ben Cahill9fbab512007-11-29 11:09:47 +0800800 * If the RXON structure is changing enough to require a new tune,
801 * or is clearing the RXON_FILTER_ASSOC_MSK, then return 1 to indicate that
802 * a new tune (full RXON command, rather than RXON_ASSOC cmd) is required.
Zhu Yib481de92007-09-25 17:54:57 -0700803 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800804static int iwl3945_full_rxon_required(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700805{
806
807 /* These items are only settable from the full RXON command */
Ron Rindjunsky5d1e2322008-06-30 17:23:04 +0800808 if (!(iwl3945_is_associated(priv)) ||
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800809 compare_ether_addr(priv->staging39_rxon.bssid_addr,
810 priv->active39_rxon.bssid_addr) ||
811 compare_ether_addr(priv->staging39_rxon.node_addr,
812 priv->active39_rxon.node_addr) ||
813 compare_ether_addr(priv->staging39_rxon.wlap_bssid_addr,
814 priv->active39_rxon.wlap_bssid_addr) ||
815 (priv->staging39_rxon.dev_type != priv->active39_rxon.dev_type) ||
816 (priv->staging39_rxon.channel != priv->active39_rxon.channel) ||
817 (priv->staging39_rxon.air_propagation !=
818 priv->active39_rxon.air_propagation) ||
819 (priv->staging39_rxon.assoc_id != priv->active39_rxon.assoc_id))
Zhu Yib481de92007-09-25 17:54:57 -0700820 return 1;
821
822 /* flags, filter_flags, ofdm_basic_rates, and cck_basic_rates can
823 * be updated with the RXON_ASSOC command -- however only some
824 * flag transitions are allowed using RXON_ASSOC */
825
826 /* Check if we are not switching bands */
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800827 if ((priv->staging39_rxon.flags & RXON_FLG_BAND_24G_MSK) !=
828 (priv->active39_rxon.flags & RXON_FLG_BAND_24G_MSK))
Zhu Yib481de92007-09-25 17:54:57 -0700829 return 1;
830
831 /* Check if we are switching association toggle */
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800832 if ((priv->staging39_rxon.filter_flags & RXON_FILTER_ASSOC_MSK) !=
833 (priv->active39_rxon.filter_flags & RXON_FILTER_ASSOC_MSK))
Zhu Yib481de92007-09-25 17:54:57 -0700834 return 1;
835
836 return 0;
837}
838
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800839static int iwl3945_send_rxon_assoc(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700840{
841 int rc = 0;
Tomas Winkler3d24a9f2008-12-19 10:37:07 +0800842 struct iwl_rx_packet *res = NULL;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800843 struct iwl3945_rxon_assoc_cmd rxon_assoc;
Winkler, Tomasc2d79b42008-12-19 10:37:34 +0800844 struct iwl_host_cmd cmd = {
Zhu Yib481de92007-09-25 17:54:57 -0700845 .id = REPLY_RXON_ASSOC,
846 .len = sizeof(rxon_assoc),
847 .meta.flags = CMD_WANT_SKB,
848 .data = &rxon_assoc,
849 };
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800850 const struct iwl3945_rxon_cmd *rxon1 = &priv->staging39_rxon;
851 const struct iwl3945_rxon_cmd *rxon2 = &priv->active39_rxon;
Zhu Yib481de92007-09-25 17:54:57 -0700852
853 if ((rxon1->flags == rxon2->flags) &&
854 (rxon1->filter_flags == rxon2->filter_flags) &&
855 (rxon1->cck_basic_rates == rxon2->cck_basic_rates) &&
856 (rxon1->ofdm_basic_rates == rxon2->ofdm_basic_rates)) {
857 IWL_DEBUG_INFO("Using current RXON_ASSOC. Not resending.\n");
858 return 0;
859 }
860
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800861 rxon_assoc.flags = priv->staging39_rxon.flags;
862 rxon_assoc.filter_flags = priv->staging39_rxon.filter_flags;
863 rxon_assoc.ofdm_basic_rates = priv->staging39_rxon.ofdm_basic_rates;
864 rxon_assoc.cck_basic_rates = priv->staging39_rxon.cck_basic_rates;
Zhu Yib481de92007-09-25 17:54:57 -0700865 rxon_assoc.reserved = 0;
866
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800867 rc = iwl3945_send_cmd_sync(priv, &cmd);
Zhu Yib481de92007-09-25 17:54:57 -0700868 if (rc)
869 return rc;
870
Tomas Winkler3d24a9f2008-12-19 10:37:07 +0800871 res = (struct iwl_rx_packet *)cmd.meta.u.skb->data;
Zhu Yib481de92007-09-25 17:54:57 -0700872 if (res->hdr.flags & IWL_CMD_FAILED_MSK) {
Winkler, Tomas15b16872008-12-19 10:37:33 +0800873 IWL_ERR(priv, "Bad return from REPLY_RXON_ASSOC command\n");
Zhu Yib481de92007-09-25 17:54:57 -0700874 rc = -EIO;
875 }
876
877 priv->alloc_rxb_skb--;
878 dev_kfree_skb_any(cmd.meta.u.skb);
879
880 return rc;
881}
882
883/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800884 * iwl3945_commit_rxon - commit staging_rxon to hardware
Zhu Yib481de92007-09-25 17:54:57 -0700885 *
Ian Schram01ebd062007-10-25 17:15:22 +0800886 * The RXON command in staging_rxon is committed to the hardware and
Zhu Yib481de92007-09-25 17:54:57 -0700887 * the active_rxon structure is updated with the new data. This
888 * function correctly transitions out of the RXON_ASSOC_MSK state if
889 * a HW tune is required based on the RXON structure changes.
890 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800891static int iwl3945_commit_rxon(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700892{
893 /* cast away the const for active_rxon in this function */
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800894 struct iwl3945_rxon_cmd *active_rxon = (void *)&priv->active39_rxon;
Zhu Yib481de92007-09-25 17:54:57 -0700895 int rc = 0;
896
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +0800897 if (!iwl_is_alive(priv))
Zhu Yib481de92007-09-25 17:54:57 -0700898 return -1;
899
900 /* always get timestamp with Rx frame */
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800901 priv->staging39_rxon.flags |= RXON_FLG_TSF2HOST_MSK;
Zhu Yib481de92007-09-25 17:54:57 -0700902
903 /* select antenna */
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800904 priv->staging39_rxon.flags &=
Zhu Yib481de92007-09-25 17:54:57 -0700905 ~(RXON_FLG_DIS_DIV_MSK | RXON_FLG_ANT_SEL_MSK);
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800906 priv->staging39_rxon.flags |= iwl3945_get_antenna_flags(priv);
Zhu Yib481de92007-09-25 17:54:57 -0700907
Samuel Ortiza3139c52008-12-19 10:37:09 +0800908 rc = iwl3945_check_rxon_cmd(priv);
Zhu Yib481de92007-09-25 17:54:57 -0700909 if (rc) {
Winkler, Tomas15b16872008-12-19 10:37:33 +0800910 IWL_ERR(priv, "Invalid RXON configuration. Not committing.\n");
Zhu Yib481de92007-09-25 17:54:57 -0700911 return -EINVAL;
912 }
913
914 /* If we don't need to send a full RXON, we can use
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800915 * iwl3945_rxon_assoc_cmd which is used to reconfigure filter
Zhu Yib481de92007-09-25 17:54:57 -0700916 * and other flags for the current radio configuration. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800917 if (!iwl3945_full_rxon_required(priv)) {
918 rc = iwl3945_send_rxon_assoc(priv);
Zhu Yib481de92007-09-25 17:54:57 -0700919 if (rc) {
Winkler, Tomas15b16872008-12-19 10:37:33 +0800920 IWL_ERR(priv, "Error setting RXON_ASSOC "
Zhu Yib481de92007-09-25 17:54:57 -0700921 "configuration (%d).\n", rc);
922 return rc;
923 }
924
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800925 memcpy(active_rxon, &priv->staging39_rxon, sizeof(*active_rxon));
Zhu Yib481de92007-09-25 17:54:57 -0700926
927 return 0;
928 }
929
930 /* If we are currently associated and the new config requires
931 * an RXON_ASSOC and the new config wants the associated mask enabled,
932 * we must clear the associated from the active configuration
933 * before we apply the new config */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800934 if (iwl3945_is_associated(priv) &&
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800935 (priv->staging39_rxon.filter_flags & RXON_FILTER_ASSOC_MSK)) {
Zhu Yib481de92007-09-25 17:54:57 -0700936 IWL_DEBUG_INFO("Toggling associated bit on current RXON\n");
937 active_rxon->filter_flags &= ~RXON_FILTER_ASSOC_MSK;
938
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800939 rc = iwl3945_send_cmd_pdu(priv, REPLY_RXON,
940 sizeof(struct iwl3945_rxon_cmd),
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800941 &priv->active39_rxon);
Zhu Yib481de92007-09-25 17:54:57 -0700942
943 /* If the mask clearing failed then we set
944 * active_rxon back to what it was previously */
945 if (rc) {
946 active_rxon->filter_flags |= RXON_FILTER_ASSOC_MSK;
Winkler, Tomas15b16872008-12-19 10:37:33 +0800947 IWL_ERR(priv, "Error clearing ASSOC_MSK on current "
Zhu Yib481de92007-09-25 17:54:57 -0700948 "configuration (%d).\n", rc);
949 return rc;
950 }
Zhu Yib481de92007-09-25 17:54:57 -0700951 }
952
953 IWL_DEBUG_INFO("Sending RXON\n"
954 "* with%s RXON_FILTER_ASSOC_MSK\n"
955 "* channel = %d\n"
Johannes Berge1749612008-10-27 15:59:26 -0700956 "* bssid = %pM\n",
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800957 ((priv->staging39_rxon.filter_flags &
Zhu Yib481de92007-09-25 17:54:57 -0700958 RXON_FILTER_ASSOC_MSK) ? "" : "out"),
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800959 le16_to_cpu(priv->staging39_rxon.channel),
Johannes Berge1749612008-10-27 15:59:26 -0700960 priv->staging_rxon.bssid_addr);
Zhu Yib481de92007-09-25 17:54:57 -0700961
962 /* Apply the new configuration */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800963 rc = iwl3945_send_cmd_pdu(priv, REPLY_RXON,
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800964 sizeof(struct iwl3945_rxon_cmd), &priv->staging39_rxon);
Zhu Yib481de92007-09-25 17:54:57 -0700965 if (rc) {
Winkler, Tomas15b16872008-12-19 10:37:33 +0800966 IWL_ERR(priv, "Error setting new configuration (%d).\n", rc);
Zhu Yib481de92007-09-25 17:54:57 -0700967 return rc;
968 }
969
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800970 memcpy(active_rxon, &priv->staging39_rxon, sizeof(*active_rxon));
Zhu Yib481de92007-09-25 17:54:57 -0700971
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800972 iwl3945_clear_stations_table(priv);
Zhu Yi556f8db2007-09-27 11:27:33 +0800973
Zhu Yib481de92007-09-25 17:54:57 -0700974 /* If we issue a new RXON command which required a tune then we must
975 * send a new TXPOWER command or we won't be able to Tx any frames */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800976 rc = iwl3945_hw_reg_send_txpower(priv);
Zhu Yib481de92007-09-25 17:54:57 -0700977 if (rc) {
Winkler, Tomas15b16872008-12-19 10:37:33 +0800978 IWL_ERR(priv, "Error setting Tx power (%d).\n", rc);
Zhu Yib481de92007-09-25 17:54:57 -0700979 return rc;
980 }
981
982 /* Add the broadcast address so we can send broadcast frames */
Kolekar, Abhijeetb5323d32008-12-19 10:37:22 +0800983 if (iwl3945_add_station(priv, iwl_bcast_addr, 0, 0) ==
Zhu Yib481de92007-09-25 17:54:57 -0700984 IWL_INVALID_STATION) {
Winkler, Tomas15b16872008-12-19 10:37:33 +0800985 IWL_ERR(priv, "Error adding BROADCAST address for transmit.\n");
Zhu Yib481de92007-09-25 17:54:57 -0700986 return -EIO;
987 }
988
989 /* If we have set the ASSOC_MSK and we are in BSS mode then
990 * add the IWL_AP_ID to the station rate table */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800991 if (iwl3945_is_associated(priv) &&
Johannes Berg05c914f2008-09-11 00:01:58 +0200992 (priv->iw_mode == NL80211_IFTYPE_STATION))
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800993 if (iwl3945_add_station(priv, priv->active39_rxon.bssid_addr, 1, 0)
Zhu Yib481de92007-09-25 17:54:57 -0700994 == IWL_INVALID_STATION) {
Winkler, Tomas15b16872008-12-19 10:37:33 +0800995 IWL_ERR(priv, "Error adding AP address for transmit\n");
Zhu Yib481de92007-09-25 17:54:57 -0700996 return -EIO;
997 }
998
Johannes Berg8318d782008-01-24 19:38:38 +0100999 /* Init the hardware's rate fallback order based on the band */
Zhu Yib481de92007-09-25 17:54:57 -07001000 rc = iwl3945_init_hw_rate_table(priv);
1001 if (rc) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08001002 IWL_ERR(priv, "Error setting HW rate table: %02X\n", rc);
Zhu Yib481de92007-09-25 17:54:57 -07001003 return -EIO;
1004 }
1005
1006 return 0;
1007}
1008
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001009static int iwl3945_send_bt_config(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001010{
Tomas Winkler4c897252008-12-19 10:37:05 +08001011 struct iwl_bt_cmd bt_cmd = {
Zhu Yib481de92007-09-25 17:54:57 -07001012 .flags = 3,
1013 .lead_time = 0xAA,
1014 .max_kill = 1,
1015 .kill_ack_mask = 0,
1016 .kill_cts_mask = 0,
1017 };
1018
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001019 return iwl3945_send_cmd_pdu(priv, REPLY_BT_CONFIG,
Tomas Winkler4c897252008-12-19 10:37:05 +08001020 sizeof(bt_cmd), &bt_cmd);
Zhu Yib481de92007-09-25 17:54:57 -07001021}
1022
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001023static int iwl3945_send_scan_abort(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001024{
1025 int rc = 0;
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08001026 struct iwl_rx_packet *res;
Winkler, Tomasc2d79b42008-12-19 10:37:34 +08001027 struct iwl_host_cmd cmd = {
Zhu Yib481de92007-09-25 17:54:57 -07001028 .id = REPLY_SCAN_ABORT_CMD,
1029 .meta.flags = CMD_WANT_SKB,
1030 };
1031
1032 /* If there isn't a scan actively going on in the hardware
1033 * then we are in between scan bands and not actually
1034 * actively scanning, so don't send the abort command */
1035 if (!test_bit(STATUS_SCAN_HW, &priv->status)) {
1036 clear_bit(STATUS_SCAN_ABORTING, &priv->status);
1037 return 0;
1038 }
1039
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001040 rc = iwl3945_send_cmd_sync(priv, &cmd);
Zhu Yib481de92007-09-25 17:54:57 -07001041 if (rc) {
1042 clear_bit(STATUS_SCAN_ABORTING, &priv->status);
1043 return rc;
1044 }
1045
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08001046 res = (struct iwl_rx_packet *)cmd.meta.u.skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07001047 if (res->u.status != CAN_ABORT_STATUS) {
1048 /* The scan abort will return 1 for success or
1049 * 2 for "failure". A failure condition can be
1050 * due to simply not being in an active scan which
1051 * can occur if we send the scan abort before we
1052 * the microcode has notified us that a scan is
1053 * completed. */
1054 IWL_DEBUG_INFO("SCAN_ABORT returned %d.\n", res->u.status);
1055 clear_bit(STATUS_SCAN_ABORTING, &priv->status);
1056 clear_bit(STATUS_SCAN_HW, &priv->status);
1057 }
1058
1059 dev_kfree_skb_any(cmd.meta.u.skb);
1060
1061 return rc;
1062}
1063
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001064static int iwl3945_add_sta_sync_callback(struct iwl_priv *priv,
Winkler, Tomasc2d79b42008-12-19 10:37:34 +08001065 struct iwl_cmd *cmd, struct sk_buff *skb)
Zhu Yib481de92007-09-25 17:54:57 -07001066{
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08001067 struct iwl_rx_packet *res = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07001068
1069 if (!skb) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08001070 IWL_ERR(priv, "Error: Response NULL in REPLY_ADD_STA.\n");
Zhu Yib481de92007-09-25 17:54:57 -07001071 return 1;
1072 }
1073
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08001074 res = (struct iwl_rx_packet *)skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07001075 if (res->hdr.flags & IWL_CMD_FAILED_MSK) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08001076 IWL_ERR(priv, "Bad return from REPLY_ADD_STA (0x%08X)\n",
Zhu Yib481de92007-09-25 17:54:57 -07001077 res->hdr.flags);
1078 return 1;
1079 }
1080
1081 switch (res->u.add_sta.status) {
1082 case ADD_STA_SUCCESS_MSK:
1083 break;
1084 default:
1085 break;
1086 }
1087
1088 /* We didn't cache the SKB; let the caller free it */
1089 return 1;
1090}
1091
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001092int iwl3945_send_add_station(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001093 struct iwl3945_addsta_cmd *sta, u8 flags)
Zhu Yib481de92007-09-25 17:54:57 -07001094{
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08001095 struct iwl_rx_packet *res = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07001096 int rc = 0;
Winkler, Tomasc2d79b42008-12-19 10:37:34 +08001097 struct iwl_host_cmd cmd = {
Zhu Yib481de92007-09-25 17:54:57 -07001098 .id = REPLY_ADD_STA,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001099 .len = sizeof(struct iwl3945_addsta_cmd),
Zhu Yib481de92007-09-25 17:54:57 -07001100 .meta.flags = flags,
1101 .data = sta,
1102 };
1103
1104 if (flags & CMD_ASYNC)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001105 cmd.meta.u.callback = iwl3945_add_sta_sync_callback;
Zhu Yib481de92007-09-25 17:54:57 -07001106 else
1107 cmd.meta.flags |= CMD_WANT_SKB;
1108
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001109 rc = iwl3945_send_cmd(priv, &cmd);
Zhu Yib481de92007-09-25 17:54:57 -07001110
1111 if (rc || (flags & CMD_ASYNC))
1112 return rc;
1113
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08001114 res = (struct iwl_rx_packet *)cmd.meta.u.skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07001115 if (res->hdr.flags & IWL_CMD_FAILED_MSK) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08001116 IWL_ERR(priv, "Bad return from REPLY_ADD_STA (0x%08X)\n",
Zhu Yib481de92007-09-25 17:54:57 -07001117 res->hdr.flags);
1118 rc = -EIO;
1119 }
1120
1121 if (rc == 0) {
1122 switch (res->u.add_sta.status) {
1123 case ADD_STA_SUCCESS_MSK:
1124 IWL_DEBUG_INFO("REPLY_ADD_STA PASSED\n");
1125 break;
1126 default:
1127 rc = -EIO;
Winkler, Tomas39aadf82008-12-19 10:37:32 +08001128 IWL_WARN(priv, "REPLY_ADD_STA failed\n");
Zhu Yib481de92007-09-25 17:54:57 -07001129 break;
1130 }
1131 }
1132
1133 priv->alloc_rxb_skb--;
1134 dev_kfree_skb_any(cmd.meta.u.skb);
1135
1136 return rc;
1137}
1138
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001139static int iwl3945_update_sta_key_info(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07001140 struct ieee80211_key_conf *keyconf,
1141 u8 sta_id)
1142{
1143 unsigned long flags;
1144 __le16 key_flags = 0;
1145
1146 switch (keyconf->alg) {
1147 case ALG_CCMP:
1148 key_flags |= STA_KEY_FLG_CCMP;
1149 key_flags |= cpu_to_le16(
1150 keyconf->keyidx << STA_KEY_FLG_KEYID_POS);
1151 key_flags &= ~STA_KEY_FLG_INVALID;
1152 break;
1153 case ALG_TKIP:
1154 case ALG_WEP:
Zhu Yib481de92007-09-25 17:54:57 -07001155 default:
1156 return -EINVAL;
1157 }
1158 spin_lock_irqsave(&priv->sta_lock, flags);
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001159 priv->stations_39[sta_id].keyinfo.alg = keyconf->alg;
1160 priv->stations_39[sta_id].keyinfo.keylen = keyconf->keylen;
1161 memcpy(priv->stations_39[sta_id].keyinfo.key, keyconf->key,
Zhu Yib481de92007-09-25 17:54:57 -07001162 keyconf->keylen);
1163
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001164 memcpy(priv->stations_39[sta_id].sta.key.key, keyconf->key,
Zhu Yib481de92007-09-25 17:54:57 -07001165 keyconf->keylen);
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001166 priv->stations_39[sta_id].sta.key.key_flags = key_flags;
1167 priv->stations_39[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
1168 priv->stations_39[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07001169
1170 spin_unlock_irqrestore(&priv->sta_lock, flags);
1171
1172 IWL_DEBUG_INFO("hwcrypto: modify ucode station key info\n");
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001173 iwl3945_send_add_station(priv, &priv->stations_39[sta_id].sta, 0);
Zhu Yib481de92007-09-25 17:54:57 -07001174 return 0;
1175}
1176
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001177static int iwl3945_clear_sta_key_info(struct iwl_priv *priv, u8 sta_id)
Zhu Yib481de92007-09-25 17:54:57 -07001178{
1179 unsigned long flags;
1180
1181 spin_lock_irqsave(&priv->sta_lock, flags);
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001182 memset(&priv->stations_39[sta_id].keyinfo, 0, sizeof(struct iwl3945_hw_key));
1183 memset(&priv->stations_39[sta_id].sta.key, 0,
Tomas Winkler4c897252008-12-19 10:37:05 +08001184 sizeof(struct iwl4965_keyinfo));
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001185 priv->stations_39[sta_id].sta.key.key_flags = STA_KEY_FLG_NO_ENC;
1186 priv->stations_39[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
1187 priv->stations_39[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07001188 spin_unlock_irqrestore(&priv->sta_lock, flags);
1189
1190 IWL_DEBUG_INFO("hwcrypto: clear ucode station key info\n");
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001191 iwl3945_send_add_station(priv, &priv->stations_39[sta_id].sta, 0);
Zhu Yib481de92007-09-25 17:54:57 -07001192 return 0;
1193}
1194
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001195static void iwl3945_clear_free_frames(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001196{
1197 struct list_head *element;
1198
1199 IWL_DEBUG_INFO("%d frames on pre-allocated heap on clear.\n",
1200 priv->frames_count);
1201
1202 while (!list_empty(&priv->free_frames)) {
1203 element = priv->free_frames.next;
1204 list_del(element);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001205 kfree(list_entry(element, struct iwl3945_frame, list));
Zhu Yib481de92007-09-25 17:54:57 -07001206 priv->frames_count--;
1207 }
1208
1209 if (priv->frames_count) {
Winkler, Tomas39aadf82008-12-19 10:37:32 +08001210 IWL_WARN(priv, "%d frames still in use. Did we lose one?\n",
Zhu Yib481de92007-09-25 17:54:57 -07001211 priv->frames_count);
1212 priv->frames_count = 0;
1213 }
1214}
1215
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001216static struct iwl3945_frame *iwl3945_get_free_frame(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001217{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001218 struct iwl3945_frame *frame;
Zhu Yib481de92007-09-25 17:54:57 -07001219 struct list_head *element;
1220 if (list_empty(&priv->free_frames)) {
1221 frame = kzalloc(sizeof(*frame), GFP_KERNEL);
1222 if (!frame) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08001223 IWL_ERR(priv, "Could not allocate frame!\n");
Zhu Yib481de92007-09-25 17:54:57 -07001224 return NULL;
1225 }
1226
1227 priv->frames_count++;
1228 return frame;
1229 }
1230
1231 element = priv->free_frames.next;
1232 list_del(element);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001233 return list_entry(element, struct iwl3945_frame, list);
Zhu Yib481de92007-09-25 17:54:57 -07001234}
1235
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001236static void iwl3945_free_frame(struct iwl_priv *priv, struct iwl3945_frame *frame)
Zhu Yib481de92007-09-25 17:54:57 -07001237{
1238 memset(frame, 0, sizeof(*frame));
1239 list_add(&frame->list, &priv->free_frames);
1240}
1241
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001242unsigned int iwl3945_fill_beacon_frame(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07001243 struct ieee80211_hdr *hdr,
Rami Rosen73ec1cc2008-12-16 09:37:07 +02001244 int left)
Zhu Yib481de92007-09-25 17:54:57 -07001245{
1246
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001247 if (!iwl3945_is_associated(priv) || !priv->ibss_beacon ||
Johannes Berg05c914f2008-09-11 00:01:58 +02001248 ((priv->iw_mode != NL80211_IFTYPE_ADHOC) &&
1249 (priv->iw_mode != NL80211_IFTYPE_AP)))
Zhu Yib481de92007-09-25 17:54:57 -07001250 return 0;
1251
1252 if (priv->ibss_beacon->len > left)
1253 return 0;
1254
1255 memcpy(hdr, priv->ibss_beacon->data, priv->ibss_beacon->len);
1256
1257 return priv->ibss_beacon->len;
1258}
1259
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001260static u8 iwl3945_rate_get_lowest_plcp(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001261{
1262 u8 i;
Kolekar, Abhijeetc24f0812008-11-07 09:58:44 -08001263 int rate_mask;
1264
1265 /* Set rate mask*/
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001266 if (priv->staging39_rxon.flags & RXON_FLG_BAND_24G_MSK)
Chatre, Reinettedbce56a2008-11-12 13:14:07 -08001267 rate_mask = priv->active_rate_basic & IWL_CCK_RATES_MASK;
Kolekar, Abhijeetc24f0812008-11-07 09:58:44 -08001268 else
Chatre, Reinettedbce56a2008-11-12 13:14:07 -08001269 rate_mask = priv->active_rate_basic & IWL_OFDM_RATES_MASK;
Zhu Yib481de92007-09-25 17:54:57 -07001270
1271 for (i = IWL_RATE_1M_INDEX; i != IWL_RATE_INVALID;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001272 i = iwl3945_rates[i].next_ieee) {
Zhu Yib481de92007-09-25 17:54:57 -07001273 if (rate_mask & (1 << i))
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001274 return iwl3945_rates[i].plcp;
Zhu Yib481de92007-09-25 17:54:57 -07001275 }
1276
Kolekar, Abhijeetc24f0812008-11-07 09:58:44 -08001277 /* No valid rate was found. Assign the lowest one */
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001278 if (priv->staging39_rxon.flags & RXON_FLG_BAND_24G_MSK)
Kolekar, Abhijeetc24f0812008-11-07 09:58:44 -08001279 return IWL_RATE_1M_PLCP;
1280 else
1281 return IWL_RATE_6M_PLCP;
Zhu Yib481de92007-09-25 17:54:57 -07001282}
1283
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001284static int iwl3945_send_beacon_cmd(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001285{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001286 struct iwl3945_frame *frame;
Zhu Yib481de92007-09-25 17:54:57 -07001287 unsigned int frame_size;
1288 int rc;
1289 u8 rate;
1290
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001291 frame = iwl3945_get_free_frame(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001292
1293 if (!frame) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08001294 IWL_ERR(priv, "Could not obtain free frame buffer for beacon "
Zhu Yib481de92007-09-25 17:54:57 -07001295 "command.\n");
1296 return -ENOMEM;
1297 }
1298
Kolekar, Abhijeetc24f0812008-11-07 09:58:44 -08001299 rate = iwl3945_rate_get_lowest_plcp(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001300
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001301 frame_size = iwl3945_hw_get_beacon_cmd(priv, frame, rate);
Zhu Yib481de92007-09-25 17:54:57 -07001302
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001303 rc = iwl3945_send_cmd_pdu(priv, REPLY_TX_BEACON, frame_size,
Zhu Yib481de92007-09-25 17:54:57 -07001304 &frame->u.cmd[0]);
1305
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001306 iwl3945_free_frame(priv, frame);
Zhu Yib481de92007-09-25 17:54:57 -07001307
1308 return rc;
1309}
1310
1311/******************************************************************************
1312 *
1313 * EEPROM related functions
1314 *
1315 ******************************************************************************/
1316
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001317static void get_eeprom_mac(struct iwl_priv *priv, u8 *mac)
Zhu Yib481de92007-09-25 17:54:57 -07001318{
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001319 memcpy(mac, priv->eeprom39.mac_address, 6);
Zhu Yib481de92007-09-25 17:54:57 -07001320}
1321
Reinette Chatre74a3a252008-01-23 10:15:19 -08001322/*
1323 * Clear the OWNER_MSK, to establish driver (instead of uCode running on
1324 * embedded controller) as EEPROM reader; each read is a series of pulses
1325 * to/from the EEPROM chip, not a single event, so even reads could conflict
1326 * if they weren't arbitrated by some ownership mechanism. Here, the driver
1327 * simply claims ownership, which should be safe when this function is called
1328 * (i.e. before loading uCode!).
1329 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001330static inline int iwl3945_eeprom_acquire_semaphore(struct iwl_priv *priv)
Reinette Chatre74a3a252008-01-23 10:15:19 -08001331{
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08001332 _iwl_clear_bit(priv, CSR_EEPROM_GP, CSR_EEPROM_GP_IF_OWNER_MSK);
Reinette Chatre74a3a252008-01-23 10:15:19 -08001333 return 0;
1334}
1335
Zhu Yib481de92007-09-25 17:54:57 -07001336/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001337 * iwl3945_eeprom_init - read EEPROM contents
Zhu Yib481de92007-09-25 17:54:57 -07001338 *
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001339 * Load the EEPROM contents from adapter into priv->eeprom39
Zhu Yib481de92007-09-25 17:54:57 -07001340 *
1341 * NOTE: This routine uses the non-debug IO access functions.
1342 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001343int iwl3945_eeprom_init(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001344{
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001345 u16 *e = (u16 *)&priv->eeprom39;
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08001346 u32 gp = iwl_read32(priv, CSR_EEPROM_GP);
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001347 int sz = sizeof(priv->eeprom39);
Zhu, Yi3d5717a2008-12-11 10:33:36 -08001348 int ret;
Zhu Yib481de92007-09-25 17:54:57 -07001349 u16 addr;
1350
1351 /* The EEPROM structure has several padding buffers within it
1352 * and when adding new EEPROM maps is subject to programmer errors
1353 * which may be very difficult to identify without explicitly
1354 * checking the resulting size of the eeprom map. */
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001355 BUILD_BUG_ON(sizeof(priv->eeprom39) != IWL_EEPROM_IMAGE_SIZE);
Zhu Yib481de92007-09-25 17:54:57 -07001356
1357 if ((gp & CSR_EEPROM_GP_VALID_MSK) == CSR_EEPROM_GP_BAD_SIGNATURE) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08001358 IWL_ERR(priv, "EEPROM not found, EEPROM_GP=0x%08x\n", gp);
Zhu Yib481de92007-09-25 17:54:57 -07001359 return -ENOENT;
1360 }
1361
Cahill, Ben M6440adb2007-11-29 11:09:55 +08001362 /* Make sure driver (instead of uCode) is allowed to read EEPROM */
Zhu, Yi3d5717a2008-12-11 10:33:36 -08001363 ret = iwl3945_eeprom_acquire_semaphore(priv);
1364 if (ret < 0) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08001365 IWL_ERR(priv, "Failed to acquire EEPROM semaphore.\n");
Zhu Yib481de92007-09-25 17:54:57 -07001366 return -ENOENT;
1367 }
1368
1369 /* eeprom is an array of 16bit values */
1370 for (addr = 0; addr < sz; addr += sizeof(u16)) {
Zhu, Yi3d5717a2008-12-11 10:33:36 -08001371 u32 r;
1372
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08001373 _iwl_write32(priv, CSR_EEPROM_REG,
Zhu, Yi3d5717a2008-12-11 10:33:36 -08001374 CSR_EEPROM_REG_MSK_ADDR & (addr << 1));
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08001375 _iwl_clear_bit(priv, CSR_EEPROM_REG, CSR_EEPROM_REG_BIT_CMD);
1376 ret = iwl_poll_direct_bit(priv, CSR_EEPROM_REG,
Zhu, Yi3d5717a2008-12-11 10:33:36 -08001377 CSR_EEPROM_REG_READ_VALID_MSK,
1378 IWL_EEPROM_ACCESS_TIMEOUT);
1379 if (ret < 0) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08001380 IWL_ERR(priv, "Time out reading EEPROM[%d]\n", addr);
Zhu, Yi3d5717a2008-12-11 10:33:36 -08001381 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07001382 }
Zhu, Yi3d5717a2008-12-11 10:33:36 -08001383
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08001384 r = _iwl_read_direct32(priv, CSR_EEPROM_REG);
Tomas Winkler58ff6d42008-02-13 02:47:54 +02001385 e[addr / 2] = le16_to_cpu((__force __le16)(r >> 16));
Zhu Yib481de92007-09-25 17:54:57 -07001386 }
1387
1388 return 0;
1389}
1390
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001391static void iwl3945_unset_hw_params(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001392{
Abhijeet Kolekar3832ec92008-12-19 10:37:26 +08001393 if (priv->shared_virt)
Zhu Yib481de92007-09-25 17:54:57 -07001394 pci_free_consistent(priv->pci_dev,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001395 sizeof(struct iwl3945_shared),
Abhijeet Kolekar3832ec92008-12-19 10:37:26 +08001396 priv->shared_virt,
1397 priv->shared_phys);
Zhu Yib481de92007-09-25 17:54:57 -07001398}
1399
1400/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001401 * iwl3945_supported_rate_to_ie - fill in the supported rate in IE field
Zhu Yib481de92007-09-25 17:54:57 -07001402 *
1403 * return : set the bit for each supported rate insert in ie
1404 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001405static u16 iwl3945_supported_rate_to_ie(u8 *ie, u16 supported_rate,
Tomas Winklerc7c46672007-10-18 02:04:15 +02001406 u16 basic_rate, int *left)
Zhu Yib481de92007-09-25 17:54:57 -07001407{
1408 u16 ret_rates = 0, bit;
1409 int i;
Tomas Winklerc7c46672007-10-18 02:04:15 +02001410 u8 *cnt = ie;
1411 u8 *rates = ie + 1;
Zhu Yib481de92007-09-25 17:54:57 -07001412
1413 for (bit = 1, i = 0; i < IWL_RATE_COUNT; i++, bit <<= 1) {
1414 if (bit & supported_rate) {
1415 ret_rates |= bit;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001416 rates[*cnt] = iwl3945_rates[i].ieee |
Tomas Winklerc7c46672007-10-18 02:04:15 +02001417 ((bit & basic_rate) ? 0x80 : 0x00);
1418 (*cnt)++;
1419 (*left)--;
1420 if ((*left <= 0) ||
1421 (*cnt >= IWL_SUPPORTED_RATES_IE_LEN))
Zhu Yib481de92007-09-25 17:54:57 -07001422 break;
1423 }
1424 }
1425
1426 return ret_rates;
1427}
1428
1429/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001430 * iwl3945_fill_probe_req - fill in all required fields and IE for probe request
Zhu Yib481de92007-09-25 17:54:57 -07001431 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001432static u16 iwl3945_fill_probe_req(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07001433 struct ieee80211_mgmt *frame,
Johannes Berg430cfe92008-10-28 18:06:02 +01001434 int left)
Zhu Yib481de92007-09-25 17:54:57 -07001435{
1436 int len = 0;
1437 u8 *pos = NULL;
Tomas Winklerc7c46672007-10-18 02:04:15 +02001438 u16 active_rates, ret_rates, cck_rates;
Zhu Yib481de92007-09-25 17:54:57 -07001439
1440 /* Make sure there is enough space for the probe request,
1441 * two mandatory IEs and the data */
1442 left -= 24;
1443 if (left < 0)
1444 return 0;
1445 len += 24;
1446
1447 frame->frame_control = cpu_to_le16(IEEE80211_STYPE_PROBE_REQ);
Kolekar, Abhijeetb5323d32008-12-19 10:37:22 +08001448 memcpy(frame->da, iwl_bcast_addr, ETH_ALEN);
Zhu Yib481de92007-09-25 17:54:57 -07001449 memcpy(frame->sa, priv->mac_addr, ETH_ALEN);
Kolekar, Abhijeetb5323d32008-12-19 10:37:22 +08001450 memcpy(frame->bssid, iwl_bcast_addr, ETH_ALEN);
Zhu Yib481de92007-09-25 17:54:57 -07001451 frame->seq_ctrl = 0;
1452
1453 /* fill in our indirect SSID IE */
1454 /* ...next IE... */
1455
1456 left -= 2;
1457 if (left < 0)
1458 return 0;
1459 len += 2;
1460 pos = &(frame->u.probe_req.variable[0]);
1461 *pos++ = WLAN_EID_SSID;
1462 *pos++ = 0;
1463
Zhu Yib481de92007-09-25 17:54:57 -07001464 /* fill in supported rate */
1465 /* ...next IE... */
1466 left -= 2;
1467 if (left < 0)
1468 return 0;
Tomas Winklerc7c46672007-10-18 02:04:15 +02001469
Zhu Yib481de92007-09-25 17:54:57 -07001470 /* ... fill it in... */
1471 *pos++ = WLAN_EID_SUPP_RATES;
1472 *pos = 0;
Tomas Winklerc7c46672007-10-18 02:04:15 +02001473
1474 priv->active_rate = priv->rates_mask;
1475 active_rates = priv->active_rate;
Zhu Yib481de92007-09-25 17:54:57 -07001476 priv->active_rate_basic = priv->rates_mask & IWL_BASIC_RATES_MASK;
1477
Tomas Winklerc7c46672007-10-18 02:04:15 +02001478 cck_rates = IWL_CCK_RATES_MASK & active_rates;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001479 ret_rates = iwl3945_supported_rate_to_ie(pos, cck_rates,
Tomas Winklerc7c46672007-10-18 02:04:15 +02001480 priv->active_rate_basic, &left);
1481 active_rates &= ~ret_rates;
1482
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001483 ret_rates = iwl3945_supported_rate_to_ie(pos, active_rates,
Tomas Winklerc7c46672007-10-18 02:04:15 +02001484 priv->active_rate_basic, &left);
1485 active_rates &= ~ret_rates;
1486
Zhu Yib481de92007-09-25 17:54:57 -07001487 len += 2 + *pos;
1488 pos += (*pos) + 1;
Tomas Winklerc7c46672007-10-18 02:04:15 +02001489 if (active_rates == 0)
Zhu Yib481de92007-09-25 17:54:57 -07001490 goto fill_end;
1491
1492 /* fill in supported extended rate */
1493 /* ...next IE... */
1494 left -= 2;
1495 if (left < 0)
1496 return 0;
1497 /* ... fill it in... */
1498 *pos++ = WLAN_EID_EXT_SUPP_RATES;
1499 *pos = 0;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001500 iwl3945_supported_rate_to_ie(pos, active_rates,
Tomas Winklerc7c46672007-10-18 02:04:15 +02001501 priv->active_rate_basic, &left);
Zhu Yib481de92007-09-25 17:54:57 -07001502 if (*pos > 0)
1503 len += 2 + *pos;
1504
1505 fill_end:
1506 return (u16)len;
1507}
1508
1509/*
1510 * QoS support
1511*/
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001512static int iwl3945_send_qos_params_command(struct iwl_priv *priv,
Tomas Winkler4c897252008-12-19 10:37:05 +08001513 struct iwl_qosparam_cmd *qos)
Zhu Yib481de92007-09-25 17:54:57 -07001514{
1515
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001516 return iwl3945_send_cmd_pdu(priv, REPLY_QOS_PARAM,
Tomas Winkler4c897252008-12-19 10:37:05 +08001517 sizeof(struct iwl_qosparam_cmd), qos);
Zhu Yib481de92007-09-25 17:54:57 -07001518}
1519
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001520static void iwl3945_activate_qos(struct iwl_priv *priv, u8 force)
Zhu Yib481de92007-09-25 17:54:57 -07001521{
1522 unsigned long flags;
1523
Zhu Yib481de92007-09-25 17:54:57 -07001524 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
1525 return;
1526
Zhu Yib481de92007-09-25 17:54:57 -07001527 spin_lock_irqsave(&priv->lock, flags);
1528 priv->qos_data.def_qos_parm.qos_flags = 0;
1529
1530 if (priv->qos_data.qos_cap.q_AP.queue_request &&
1531 !priv->qos_data.qos_cap.q_AP.txop_request)
1532 priv->qos_data.def_qos_parm.qos_flags |=
1533 QOS_PARAM_FLG_TXOP_TYPE_MSK;
1534
1535 if (priv->qos_data.qos_active)
1536 priv->qos_data.def_qos_parm.qos_flags |=
1537 QOS_PARAM_FLG_UPDATE_EDCA_MSK;
1538
1539 spin_unlock_irqrestore(&priv->lock, flags);
1540
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001541 if (force || iwl3945_is_associated(priv)) {
Tomas Winklera96a27f2008-10-23 23:48:56 -07001542 IWL_DEBUG_QOS("send QoS cmd with QoS active %d \n",
Zhu Yib481de92007-09-25 17:54:57 -07001543 priv->qos_data.qos_active);
1544
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001545 iwl3945_send_qos_params_command(priv,
Zhu Yib481de92007-09-25 17:54:57 -07001546 &(priv->qos_data.def_qos_parm));
1547 }
1548}
1549
Zhu Yib481de92007-09-25 17:54:57 -07001550/*
1551 * Power management (not Tx power!) functions
1552 */
1553#define MSEC_TO_USEC 1024
1554
Tomas Winkler600c0e12008-12-19 10:37:04 +08001555
1556#define NOSLP __constant_cpu_to_le16(0), 0, 0
1557#define SLP IWL_POWER_DRIVER_ALLOW_SLEEP_MSK, 0, 0
Zhu Yib481de92007-09-25 17:54:57 -07001558#define SLP_TIMEOUT(T) __constant_cpu_to_le32((T) * MSEC_TO_USEC)
1559#define SLP_VEC(X0, X1, X2, X3, X4) {__constant_cpu_to_le32(X0), \
1560 __constant_cpu_to_le32(X1), \
1561 __constant_cpu_to_le32(X2), \
1562 __constant_cpu_to_le32(X3), \
1563 __constant_cpu_to_le32(X4)}
1564
Zhu Yib481de92007-09-25 17:54:57 -07001565/* default power management (not Tx power) table values */
Tomas Winklera96a27f2008-10-23 23:48:56 -07001566/* for TIM 0-10 */
Samuel Ortiz1125eff2008-12-19 10:37:14 +08001567static struct iwl_power_vec_entry range_0[IWL39_POWER_AC] = {
Zhu Yib481de92007-09-25 17:54:57 -07001568 {{NOSLP, SLP_TIMEOUT(0), SLP_TIMEOUT(0), SLP_VEC(0, 0, 0, 0, 0)}, 0},
1569 {{SLP, SLP_TIMEOUT(200), SLP_TIMEOUT(500), SLP_VEC(1, 2, 3, 4, 4)}, 0},
1570 {{SLP, SLP_TIMEOUT(200), SLP_TIMEOUT(300), SLP_VEC(2, 4, 6, 7, 7)}, 0},
1571 {{SLP, SLP_TIMEOUT(50), SLP_TIMEOUT(100), SLP_VEC(2, 6, 9, 9, 10)}, 0},
1572 {{SLP, SLP_TIMEOUT(50), SLP_TIMEOUT(25), SLP_VEC(2, 7, 9, 9, 10)}, 1},
1573 {{SLP, SLP_TIMEOUT(25), SLP_TIMEOUT(25), SLP_VEC(4, 7, 10, 10, 10)}, 1}
1574};
1575
Tomas Winklera96a27f2008-10-23 23:48:56 -07001576/* for TIM > 10 */
Samuel Ortiz1125eff2008-12-19 10:37:14 +08001577static struct iwl_power_vec_entry range_1[IWL39_POWER_AC] = {
Zhu Yib481de92007-09-25 17:54:57 -07001578 {{NOSLP, SLP_TIMEOUT(0), SLP_TIMEOUT(0), SLP_VEC(0, 0, 0, 0, 0)}, 0},
1579 {{SLP, SLP_TIMEOUT(200), SLP_TIMEOUT(500),
1580 SLP_VEC(1, 2, 3, 4, 0xFF)}, 0},
1581 {{SLP, SLP_TIMEOUT(200), SLP_TIMEOUT(300),
1582 SLP_VEC(2, 4, 6, 7, 0xFF)}, 0},
1583 {{SLP, SLP_TIMEOUT(50), SLP_TIMEOUT(100),
1584 SLP_VEC(2, 6, 9, 9, 0xFF)}, 0},
1585 {{SLP, SLP_TIMEOUT(50), SLP_TIMEOUT(25), SLP_VEC(2, 7, 9, 9, 0xFF)}, 0},
1586 {{SLP, SLP_TIMEOUT(25), SLP_TIMEOUT(25),
1587 SLP_VEC(4, 7, 10, 10, 0xFF)}, 0}
1588};
1589
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001590int iwl3945_power_init_handle(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001591{
1592 int rc = 0, i;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001593 struct iwl3945_power_mgr *pow_data;
Samuel Ortiz1125eff2008-12-19 10:37:14 +08001594 int size = sizeof(struct iwl_power_vec_entry) * IWL39_POWER_AC;
Zhu Yib481de92007-09-25 17:54:57 -07001595 u16 pci_pm;
1596
1597 IWL_DEBUG_POWER("Initialize power \n");
1598
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001599 pow_data = &(priv->power_data_39);
Zhu Yib481de92007-09-25 17:54:57 -07001600
1601 memset(pow_data, 0, sizeof(*pow_data));
1602
1603 pow_data->active_index = IWL_POWER_RANGE_0;
1604 pow_data->dtim_val = 0xffff;
1605
1606 memcpy(&pow_data->pwr_range_0[0], &range_0[0], size);
1607 memcpy(&pow_data->pwr_range_1[0], &range_1[0], size);
1608
1609 rc = pci_read_config_word(priv->pci_dev, PCI_LINK_CTRL, &pci_pm);
1610 if (rc != 0)
1611 return 0;
1612 else {
Tomas Winkler600c0e12008-12-19 10:37:04 +08001613 struct iwl_powertable_cmd *cmd;
Zhu Yib481de92007-09-25 17:54:57 -07001614
1615 IWL_DEBUG_POWER("adjust power command flags\n");
1616
Samuel Ortiz1125eff2008-12-19 10:37:14 +08001617 for (i = 0; i < IWL39_POWER_AC; i++) {
Zhu Yib481de92007-09-25 17:54:57 -07001618 cmd = &pow_data->pwr_range_0[i].cmd;
1619
1620 if (pci_pm & 0x1)
1621 cmd->flags &= ~IWL_POWER_PCI_PM_MSK;
1622 else
1623 cmd->flags |= IWL_POWER_PCI_PM_MSK;
1624 }
1625 }
1626 return rc;
1627}
1628
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001629static int iwl3945_update_power_cmd(struct iwl_priv *priv,
Tomas Winkler600c0e12008-12-19 10:37:04 +08001630 struct iwl_powertable_cmd *cmd, u32 mode)
Zhu Yib481de92007-09-25 17:54:57 -07001631{
1632 int rc = 0, i;
1633 u8 skip;
1634 u32 max_sleep = 0;
Samuel Ortiz1125eff2008-12-19 10:37:14 +08001635 struct iwl_power_vec_entry *range;
Zhu Yib481de92007-09-25 17:54:57 -07001636 u8 period = 0;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001637 struct iwl3945_power_mgr *pow_data;
Zhu Yib481de92007-09-25 17:54:57 -07001638
1639 if (mode > IWL_POWER_INDEX_5) {
1640 IWL_DEBUG_POWER("Error invalid power mode \n");
1641 return -1;
1642 }
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001643 pow_data = &(priv->power_data_39);
Zhu Yib481de92007-09-25 17:54:57 -07001644
1645 if (pow_data->active_index == IWL_POWER_RANGE_0)
1646 range = &pow_data->pwr_range_0[0];
1647 else
1648 range = &pow_data->pwr_range_1[1];
1649
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001650 memcpy(cmd, &range[mode].cmd, sizeof(struct iwl3945_powertable_cmd));
Zhu Yib481de92007-09-25 17:54:57 -07001651
1652#ifdef IWL_MAC80211_DISABLE
1653 if (priv->assoc_network != NULL) {
1654 unsigned long flags;
1655
1656 period = priv->assoc_network->tim.tim_period;
1657 }
1658#endif /*IWL_MAC80211_DISABLE */
1659 skip = range[mode].no_dtim;
1660
1661 if (period == 0) {
1662 period = 1;
1663 skip = 0;
1664 }
1665
1666 if (skip == 0) {
1667 max_sleep = period;
1668 cmd->flags &= ~IWL_POWER_SLEEP_OVER_DTIM_MSK;
1669 } else {
1670 __le32 slp_itrvl = cmd->sleep_interval[IWL_POWER_VEC_SIZE - 1];
1671 max_sleep = (le32_to_cpu(slp_itrvl) / period) * period;
1672 cmd->flags |= IWL_POWER_SLEEP_OVER_DTIM_MSK;
1673 }
1674
1675 for (i = 0; i < IWL_POWER_VEC_SIZE; i++) {
1676 if (le32_to_cpu(cmd->sleep_interval[i]) > max_sleep)
1677 cmd->sleep_interval[i] = cpu_to_le32(max_sleep);
1678 }
1679
1680 IWL_DEBUG_POWER("Flags value = 0x%08X\n", cmd->flags);
1681 IWL_DEBUG_POWER("Tx timeout = %u\n", le32_to_cpu(cmd->tx_data_timeout));
1682 IWL_DEBUG_POWER("Rx timeout = %u\n", le32_to_cpu(cmd->rx_data_timeout));
1683 IWL_DEBUG_POWER("Sleep interval vector = { %d , %d , %d , %d , %d }\n",
1684 le32_to_cpu(cmd->sleep_interval[0]),
1685 le32_to_cpu(cmd->sleep_interval[1]),
1686 le32_to_cpu(cmd->sleep_interval[2]),
1687 le32_to_cpu(cmd->sleep_interval[3]),
1688 le32_to_cpu(cmd->sleep_interval[4]));
1689
1690 return rc;
1691}
1692
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001693static int iwl3945_send_power_mode(struct iwl_priv *priv, u32 mode)
Zhu Yib481de92007-09-25 17:54:57 -07001694{
John W. Linville9a62f732007-11-15 16:27:36 -05001695 u32 uninitialized_var(final_mode);
Zhu Yib481de92007-09-25 17:54:57 -07001696 int rc;
Tomas Winkler600c0e12008-12-19 10:37:04 +08001697 struct iwl_powertable_cmd cmd;
Zhu Yib481de92007-09-25 17:54:57 -07001698
1699 /* If on battery, set to 3,
Ian Schram01ebd062007-10-25 17:15:22 +08001700 * if plugged into AC power, set to CAM ("continuously aware mode"),
Zhu Yib481de92007-09-25 17:54:57 -07001701 * else user level */
1702 switch (mode) {
Samuel Ortiz1125eff2008-12-19 10:37:14 +08001703 case IWL39_POWER_BATTERY:
Zhu Yib481de92007-09-25 17:54:57 -07001704 final_mode = IWL_POWER_INDEX_3;
1705 break;
Samuel Ortiz1125eff2008-12-19 10:37:14 +08001706 case IWL39_POWER_AC:
Zhu Yib481de92007-09-25 17:54:57 -07001707 final_mode = IWL_POWER_MODE_CAM;
1708 break;
1709 default:
1710 final_mode = mode;
1711 break;
1712 }
1713
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001714 iwl3945_update_power_cmd(priv, &cmd, final_mode);
Zhu Yib481de92007-09-25 17:54:57 -07001715
Tomas Winkler600c0e12008-12-19 10:37:04 +08001716 /* FIXME use get_hcmd_size 3945 command is 4 bytes shorter */
1717 rc = iwl3945_send_cmd_pdu(priv, POWER_TABLE_CMD,
1718 sizeof(struct iwl3945_powertable_cmd), &cmd);
Zhu Yib481de92007-09-25 17:54:57 -07001719
1720 if (final_mode == IWL_POWER_MODE_CAM)
1721 clear_bit(STATUS_POWER_PMI, &priv->status);
1722 else
1723 set_bit(STATUS_POWER_PMI, &priv->status);
1724
1725 return rc;
1726}
1727
Zhu Yib481de92007-09-25 17:54:57 -07001728#define MAX_UCODE_BEACON_INTERVAL 1024
1729#define INTEL_CONN_LISTEN_INTERVAL __constant_cpu_to_le16(0xA)
1730
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001731static __le16 iwl3945_adjust_beacon_interval(u16 beacon_val)
Zhu Yib481de92007-09-25 17:54:57 -07001732{
1733 u16 new_val = 0;
1734 u16 beacon_factor = 0;
1735
1736 beacon_factor =
1737 (beacon_val + MAX_UCODE_BEACON_INTERVAL)
1738 / MAX_UCODE_BEACON_INTERVAL;
1739 new_val = beacon_val / beacon_factor;
1740
1741 return cpu_to_le16(new_val);
1742}
1743
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001744static void iwl3945_setup_rxon_timing(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001745{
1746 u64 interval_tm_unit;
1747 u64 tsf, result;
1748 unsigned long flags;
1749 struct ieee80211_conf *conf = NULL;
1750 u16 beacon_int = 0;
1751
1752 conf = ieee80211_get_hw_conf(priv->hw);
1753
1754 spin_lock_irqsave(&priv->lock, flags);
Tomas Winkler28afaf92008-12-19 10:37:06 +08001755 priv->rxon_timing.timestamp = cpu_to_le64(priv->timestamp);
Zhu Yib481de92007-09-25 17:54:57 -07001756 priv->rxon_timing.listen_interval = INTEL_CONN_LISTEN_INTERVAL;
1757
Tomas Winkler28afaf92008-12-19 10:37:06 +08001758 tsf = priv->timestamp;
Zhu Yib481de92007-09-25 17:54:57 -07001759
1760 beacon_int = priv->beacon_int;
1761 spin_unlock_irqrestore(&priv->lock, flags);
1762
Johannes Berg05c914f2008-09-11 00:01:58 +02001763 if (priv->iw_mode == NL80211_IFTYPE_STATION) {
Zhu Yib481de92007-09-25 17:54:57 -07001764 if (beacon_int == 0) {
1765 priv->rxon_timing.beacon_interval = cpu_to_le16(100);
1766 priv->rxon_timing.beacon_init_val = cpu_to_le32(102400);
1767 } else {
1768 priv->rxon_timing.beacon_interval =
1769 cpu_to_le16(beacon_int);
1770 priv->rxon_timing.beacon_interval =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001771 iwl3945_adjust_beacon_interval(
Zhu Yib481de92007-09-25 17:54:57 -07001772 le16_to_cpu(priv->rxon_timing.beacon_interval));
1773 }
1774
1775 priv->rxon_timing.atim_window = 0;
1776 } else {
1777 priv->rxon_timing.beacon_interval =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001778 iwl3945_adjust_beacon_interval(conf->beacon_int);
Zhu Yib481de92007-09-25 17:54:57 -07001779 /* TODO: we need to get atim_window from upper stack
1780 * for now we set to 0 */
1781 priv->rxon_timing.atim_window = 0;
1782 }
1783
1784 interval_tm_unit =
1785 (le16_to_cpu(priv->rxon_timing.beacon_interval) * 1024);
1786 result = do_div(tsf, interval_tm_unit);
1787 priv->rxon_timing.beacon_init_val =
1788 cpu_to_le32((u32) ((u64) interval_tm_unit - result));
1789
1790 IWL_DEBUG_ASSOC
1791 ("beacon interval %d beacon timer %d beacon tim %d\n",
1792 le16_to_cpu(priv->rxon_timing.beacon_interval),
1793 le32_to_cpu(priv->rxon_timing.beacon_init_val),
1794 le16_to_cpu(priv->rxon_timing.atim_window));
1795}
1796
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001797static int iwl3945_scan_initiate(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001798{
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08001799 if (!iwl_is_ready_rf(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07001800 IWL_DEBUG_SCAN("Aborting scan due to not ready.\n");
1801 return -EIO;
1802 }
1803
1804 if (test_bit(STATUS_SCANNING, &priv->status)) {
1805 IWL_DEBUG_SCAN("Scan already in progress.\n");
1806 return -EAGAIN;
1807 }
1808
1809 if (test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
1810 IWL_DEBUG_SCAN("Scan request while abort pending. "
1811 "Queuing.\n");
1812 return -EAGAIN;
1813 }
1814
1815 IWL_DEBUG_INFO("Starting scan...\n");
Ron Rindjunsky66b50042008-06-25 16:46:31 +08001816 if (priv->cfg->sku & IWL_SKU_G)
1817 priv->scan_bands |= BIT(IEEE80211_BAND_2GHZ);
1818 if (priv->cfg->sku & IWL_SKU_A)
1819 priv->scan_bands |= BIT(IEEE80211_BAND_5GHZ);
Zhu Yib481de92007-09-25 17:54:57 -07001820 set_bit(STATUS_SCANNING, &priv->status);
1821 priv->scan_start = jiffies;
1822 priv->scan_pass_start = priv->scan_start;
1823
1824 queue_work(priv->workqueue, &priv->request_scan);
1825
1826 return 0;
1827}
1828
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001829static int iwl3945_set_rxon_hwcrypto(struct iwl_priv *priv, int hw_decrypt)
Zhu Yib481de92007-09-25 17:54:57 -07001830{
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001831 struct iwl3945_rxon_cmd *rxon = &priv->staging39_rxon;
Zhu Yib481de92007-09-25 17:54:57 -07001832
1833 if (hw_decrypt)
1834 rxon->filter_flags &= ~RXON_FILTER_DIS_DECRYPT_MSK;
1835 else
1836 rxon->filter_flags |= RXON_FILTER_DIS_DECRYPT_MSK;
1837
1838 return 0;
1839}
1840
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001841static void iwl3945_set_flags_for_phymode(struct iwl_priv *priv,
Johannes Berg8318d782008-01-24 19:38:38 +01001842 enum ieee80211_band band)
Zhu Yib481de92007-09-25 17:54:57 -07001843{
Johannes Berg8318d782008-01-24 19:38:38 +01001844 if (band == IEEE80211_BAND_5GHZ) {
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001845 priv->staging39_rxon.flags &=
Zhu Yib481de92007-09-25 17:54:57 -07001846 ~(RXON_FLG_BAND_24G_MSK | RXON_FLG_AUTO_DETECT_MSK
1847 | RXON_FLG_CCK_MSK);
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001848 priv->staging39_rxon.flags |= RXON_FLG_SHORT_SLOT_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07001849 } else {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001850 /* Copied from iwl3945_bg_post_associate() */
Zhu Yib481de92007-09-25 17:54:57 -07001851 if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_SLOT_TIME)
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001852 priv->staging39_rxon.flags |= RXON_FLG_SHORT_SLOT_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07001853 else
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001854 priv->staging39_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07001855
Johannes Berg05c914f2008-09-11 00:01:58 +02001856 if (priv->iw_mode == NL80211_IFTYPE_ADHOC)
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001857 priv->staging39_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07001858
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001859 priv->staging39_rxon.flags |= RXON_FLG_BAND_24G_MSK;
1860 priv->staging39_rxon.flags |= RXON_FLG_AUTO_DETECT_MSK;
1861 priv->staging39_rxon.flags &= ~RXON_FLG_CCK_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07001862 }
1863}
1864
1865/*
Ian Schram01ebd062007-10-25 17:15:22 +08001866 * initialize rxon structure with default values from eeprom
Zhu Yib481de92007-09-25 17:54:57 -07001867 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001868static void iwl3945_connection_init_rx_config(struct iwl_priv *priv,
Zhu, Yi60294de2008-10-29 14:05:45 -07001869 int mode)
Zhu Yib481de92007-09-25 17:54:57 -07001870{
Samuel Ortizd20b3c62008-12-19 10:37:15 +08001871 const struct iwl_channel_info *ch_info;
Zhu Yib481de92007-09-25 17:54:57 -07001872
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001873 memset(&priv->staging39_rxon, 0, sizeof(priv->staging39_rxon));
Zhu Yib481de92007-09-25 17:54:57 -07001874
Zhu, Yi60294de2008-10-29 14:05:45 -07001875 switch (mode) {
Johannes Berg05c914f2008-09-11 00:01:58 +02001876 case NL80211_IFTYPE_AP:
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001877 priv->staging39_rxon.dev_type = RXON_DEV_TYPE_AP;
Zhu Yib481de92007-09-25 17:54:57 -07001878 break;
1879
Johannes Berg05c914f2008-09-11 00:01:58 +02001880 case NL80211_IFTYPE_STATION:
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001881 priv->staging39_rxon.dev_type = RXON_DEV_TYPE_ESS;
1882 priv->staging39_rxon.filter_flags = RXON_FILTER_ACCEPT_GRP_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07001883 break;
1884
Johannes Berg05c914f2008-09-11 00:01:58 +02001885 case NL80211_IFTYPE_ADHOC:
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001886 priv->staging39_rxon.dev_type = RXON_DEV_TYPE_IBSS;
1887 priv->staging39_rxon.flags = RXON_FLG_SHORT_PREAMBLE_MSK;
1888 priv->staging39_rxon.filter_flags = RXON_FILTER_BCON_AWARE_MSK |
Zhu Yib481de92007-09-25 17:54:57 -07001889 RXON_FILTER_ACCEPT_GRP_MSK;
1890 break;
1891
Johannes Berg05c914f2008-09-11 00:01:58 +02001892 case NL80211_IFTYPE_MONITOR:
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001893 priv->staging39_rxon.dev_type = RXON_DEV_TYPE_SNIFFER;
1894 priv->staging39_rxon.filter_flags = RXON_FILTER_PROMISC_MSK |
Zhu Yib481de92007-09-25 17:54:57 -07001895 RXON_FILTER_CTL2HOST_MSK | RXON_FILTER_ACCEPT_GRP_MSK;
1896 break;
Tomas Winkler69dc5d92008-03-25 16:33:41 -07001897 default:
Winkler, Tomas15b16872008-12-19 10:37:33 +08001898 IWL_ERR(priv, "Unsupported interface type %d\n", mode);
Tomas Winkler69dc5d92008-03-25 16:33:41 -07001899 break;
Zhu Yib481de92007-09-25 17:54:57 -07001900 }
1901
1902#if 0
1903 /* TODO: Figure out when short_preamble would be set and cache from
1904 * that */
1905 if (!hw_to_local(priv->hw)->short_preamble)
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001906 priv->staging39_rxon.flags &= ~RXON_FLG_SHORT_PREAMBLE_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07001907 else
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001908 priv->staging39_rxon.flags |= RXON_FLG_SHORT_PREAMBLE_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07001909#endif
1910
Johannes Berg8318d782008-01-24 19:38:38 +01001911 ch_info = iwl3945_get_channel_info(priv, priv->band,
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001912 le16_to_cpu(priv->active39_rxon.channel));
Zhu Yib481de92007-09-25 17:54:57 -07001913
1914 if (!ch_info)
1915 ch_info = &priv->channel_info[0];
1916
1917 /*
1918 * in some case A channels are all non IBSS
1919 * in this case force B/G channel
1920 */
Zhu, Yi60294de2008-10-29 14:05:45 -07001921 if ((mode == NL80211_IFTYPE_ADHOC) && !(is_channel_ibss(ch_info)))
Zhu Yib481de92007-09-25 17:54:57 -07001922 ch_info = &priv->channel_info[0];
1923
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001924 priv->staging39_rxon.channel = cpu_to_le16(ch_info->channel);
Zhu Yib481de92007-09-25 17:54:57 -07001925 if (is_channel_a_band(ch_info))
Johannes Berg8318d782008-01-24 19:38:38 +01001926 priv->band = IEEE80211_BAND_5GHZ;
Zhu Yib481de92007-09-25 17:54:57 -07001927 else
Johannes Berg8318d782008-01-24 19:38:38 +01001928 priv->band = IEEE80211_BAND_2GHZ;
Zhu Yib481de92007-09-25 17:54:57 -07001929
Johannes Berg8318d782008-01-24 19:38:38 +01001930 iwl3945_set_flags_for_phymode(priv, priv->band);
Zhu Yib481de92007-09-25 17:54:57 -07001931
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001932 priv->staging39_rxon.ofdm_basic_rates =
Zhu Yib481de92007-09-25 17:54:57 -07001933 (IWL_OFDM_RATES_MASK >> IWL_FIRST_OFDM_RATE) & 0xFF;
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001934 priv->staging39_rxon.cck_basic_rates =
Zhu Yib481de92007-09-25 17:54:57 -07001935 (IWL_CCK_RATES_MASK >> IWL_FIRST_CCK_RATE) & 0xF;
1936}
1937
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001938static int iwl3945_set_mode(struct iwl_priv *priv, int mode)
Zhu Yib481de92007-09-25 17:54:57 -07001939{
Johannes Berg05c914f2008-09-11 00:01:58 +02001940 if (mode == NL80211_IFTYPE_ADHOC) {
Samuel Ortizd20b3c62008-12-19 10:37:15 +08001941 const struct iwl_channel_info *ch_info;
Zhu Yib481de92007-09-25 17:54:57 -07001942
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001943 ch_info = iwl3945_get_channel_info(priv,
Johannes Berg8318d782008-01-24 19:38:38 +01001944 priv->band,
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001945 le16_to_cpu(priv->staging39_rxon.channel));
Zhu Yib481de92007-09-25 17:54:57 -07001946
1947 if (!ch_info || !is_channel_ibss(ch_info)) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08001948 IWL_ERR(priv, "channel %d not IBSS channel\n",
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001949 le16_to_cpu(priv->staging39_rxon.channel));
Zhu Yib481de92007-09-25 17:54:57 -07001950 return -EINVAL;
1951 }
1952 }
1953
Zhu, Yi60294de2008-10-29 14:05:45 -07001954 iwl3945_connection_init_rx_config(priv, mode);
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001955 memcpy(priv->staging39_rxon.node_addr, priv->mac_addr, ETH_ALEN);
Zhu Yib481de92007-09-25 17:54:57 -07001956
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001957 iwl3945_clear_stations_table(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001958
Tomas Winklera96a27f2008-10-23 23:48:56 -07001959 /* don't commit rxon if rf-kill is on*/
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08001960 if (!iwl_is_ready_rf(priv))
Mohamed Abbasfde35712007-11-29 11:10:15 +08001961 return -EAGAIN;
1962
1963 cancel_delayed_work(&priv->scan_check);
Winkler, Tomasaf0053d2009-01-19 15:30:23 -08001964 if (iwl_scan_cancel_timeout(priv, 100)) {
Winkler, Tomas39aadf82008-12-19 10:37:32 +08001965 IWL_WARN(priv, "Aborted scan still in progress after 100ms\n");
Mohamed Abbasfde35712007-11-29 11:10:15 +08001966 IWL_DEBUG_MAC80211("leaving - scan abort failed.\n");
1967 return -EAGAIN;
1968 }
1969
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001970 iwl3945_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001971
1972 return 0;
1973}
1974
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001975static void iwl3945_build_tx_cmd_hwcrypto(struct iwl_priv *priv,
Johannes Berge039fa42008-05-15 12:55:29 +02001976 struct ieee80211_tx_info *info,
Winkler, Tomasc2d79b42008-12-19 10:37:34 +08001977 struct iwl_cmd *cmd,
Zhu Yib481de92007-09-25 17:54:57 -07001978 struct sk_buff *skb_frag,
1979 int last_frag)
1980{
Winkler, Tomase52119c2008-12-22 11:31:19 +08001981 struct iwl3945_tx_cmd *tx = (struct iwl3945_tx_cmd *)cmd->cmd.payload;
Ivo van Doorn1c014422008-04-17 19:41:02 +02001982 struct iwl3945_hw_key *keyinfo =
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001983 &priv->stations_39[info->control.hw_key->hw_key_idx].keyinfo;
Zhu Yib481de92007-09-25 17:54:57 -07001984
1985 switch (keyinfo->alg) {
1986 case ALG_CCMP:
Winkler, Tomase52119c2008-12-22 11:31:19 +08001987 tx->sec_ctl = TX_CMD_SEC_CCM;
1988 memcpy(tx->key, keyinfo->key, keyinfo->keylen);
Tomas Winklera96a27f2008-10-23 23:48:56 -07001989 IWL_DEBUG_TX("tx_cmd with AES hwcrypto\n");
Zhu Yib481de92007-09-25 17:54:57 -07001990 break;
1991
1992 case ALG_TKIP:
1993#if 0
Winkler, Tomase52119c2008-12-22 11:31:19 +08001994 tx->sec_ctl = TX_CMD_SEC_TKIP;
Zhu Yib481de92007-09-25 17:54:57 -07001995
1996 if (last_frag)
Winkler, Tomase52119c2008-12-22 11:31:19 +08001997 memcpy(tx->tkip_mic.byte, skb_frag->tail - 8,
Zhu Yib481de92007-09-25 17:54:57 -07001998 8);
1999 else
Winkler, Tomase52119c2008-12-22 11:31:19 +08002000 memset(tx->tkip_mic.byte, 0, 8);
Zhu Yib481de92007-09-25 17:54:57 -07002001#endif
2002 break;
2003
2004 case ALG_WEP:
Winkler, Tomase52119c2008-12-22 11:31:19 +08002005 tx->sec_ctl = TX_CMD_SEC_WEP |
Johannes Berge039fa42008-05-15 12:55:29 +02002006 (info->control.hw_key->hw_key_idx & TX_CMD_SEC_MSK) << TX_CMD_SEC_SHIFT;
Zhu Yib481de92007-09-25 17:54:57 -07002007
2008 if (keyinfo->keylen == 13)
Winkler, Tomase52119c2008-12-22 11:31:19 +08002009 tx->sec_ctl |= TX_CMD_SEC_KEY128;
Zhu Yib481de92007-09-25 17:54:57 -07002010
Winkler, Tomase52119c2008-12-22 11:31:19 +08002011 memcpy(&tx->key[3], keyinfo->key, keyinfo->keylen);
Zhu Yib481de92007-09-25 17:54:57 -07002012
2013 IWL_DEBUG_TX("Configuring packet for WEP encryption "
Johannes Berge039fa42008-05-15 12:55:29 +02002014 "with key %d\n", info->control.hw_key->hw_key_idx);
Zhu Yib481de92007-09-25 17:54:57 -07002015 break;
2016
Zhu Yib481de92007-09-25 17:54:57 -07002017 default:
Tomas Winkler978785a2008-12-19 10:37:31 +08002018 IWL_ERR(priv, "Unknown encode alg %d\n", keyinfo->alg);
Zhu Yib481de92007-09-25 17:54:57 -07002019 break;
2020 }
2021}
2022
2023/*
2024 * handle build REPLY_TX command notification.
2025 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002026static void iwl3945_build_tx_cmd_basic(struct iwl_priv *priv,
Winkler, Tomasc2d79b42008-12-19 10:37:34 +08002027 struct iwl_cmd *cmd,
Johannes Berge039fa42008-05-15 12:55:29 +02002028 struct ieee80211_tx_info *info,
Winkler, Tomase52119c2008-12-22 11:31:19 +08002029 struct ieee80211_hdr *hdr, u8 std_id)
Zhu Yib481de92007-09-25 17:54:57 -07002030{
Winkler, Tomase52119c2008-12-22 11:31:19 +08002031 struct iwl3945_tx_cmd *tx = (struct iwl3945_tx_cmd *)cmd->cmd.payload;
2032 __le32 tx_flags = tx->tx_flags;
Harvey Harrisonfd7c8a42008-06-11 14:21:56 -07002033 __le16 fc = hdr->frame_control;
Johannes Berge6a98542008-10-21 12:40:02 +02002034 u8 rc_flags = info->control.rates[0].flags;
Zhu Yib481de92007-09-25 17:54:57 -07002035
Winkler, Tomase52119c2008-12-22 11:31:19 +08002036 tx->stop_time.life_time = TX_CMD_LIFE_TIME_INFINITE;
Johannes Berge039fa42008-05-15 12:55:29 +02002037 if (!(info->flags & IEEE80211_TX_CTL_NO_ACK)) {
Zhu Yib481de92007-09-25 17:54:57 -07002038 tx_flags |= TX_CMD_FLG_ACK_MSK;
Harvey Harrisonfd7c8a42008-06-11 14:21:56 -07002039 if (ieee80211_is_mgmt(fc))
Zhu Yib481de92007-09-25 17:54:57 -07002040 tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
Harvey Harrisonfd7c8a42008-06-11 14:21:56 -07002041 if (ieee80211_is_probe_resp(fc) &&
Zhu Yib481de92007-09-25 17:54:57 -07002042 !(le16_to_cpu(hdr->seq_ctrl) & 0xf))
2043 tx_flags |= TX_CMD_FLG_TSF_MSK;
2044 } else {
2045 tx_flags &= (~TX_CMD_FLG_ACK_MSK);
2046 tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
2047 }
2048
Winkler, Tomase52119c2008-12-22 11:31:19 +08002049 tx->sta_id = std_id;
Harvey Harrison8b7b1e02008-06-11 14:21:56 -07002050 if (ieee80211_has_morefrags(fc))
Zhu Yib481de92007-09-25 17:54:57 -07002051 tx_flags |= TX_CMD_FLG_MORE_FRAG_MSK;
2052
Harvey Harrisonfd7c8a42008-06-11 14:21:56 -07002053 if (ieee80211_is_data_qos(fc)) {
2054 u8 *qc = ieee80211_get_qos_ctl(hdr);
Winkler, Tomase52119c2008-12-22 11:31:19 +08002055 tx->tid_tspec = qc[0] & 0xf;
Zhu Yib481de92007-09-25 17:54:57 -07002056 tx_flags &= ~TX_CMD_FLG_SEQ_CTL_MSK;
Tomas Winkler54dbb522008-05-15 13:54:06 +08002057 } else {
Zhu Yib481de92007-09-25 17:54:57 -07002058 tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
Tomas Winkler54dbb522008-05-15 13:54:06 +08002059 }
Zhu Yib481de92007-09-25 17:54:57 -07002060
Johannes Berge6a98542008-10-21 12:40:02 +02002061 if (rc_flags & IEEE80211_TX_RC_USE_RTS_CTS) {
Zhu Yib481de92007-09-25 17:54:57 -07002062 tx_flags |= TX_CMD_FLG_RTS_MSK;
2063 tx_flags &= ~TX_CMD_FLG_CTS_MSK;
Johannes Berge6a98542008-10-21 12:40:02 +02002064 } else if (rc_flags & IEEE80211_TX_RC_USE_CTS_PROTECT) {
Zhu Yib481de92007-09-25 17:54:57 -07002065 tx_flags &= ~TX_CMD_FLG_RTS_MSK;
2066 tx_flags |= TX_CMD_FLG_CTS_MSK;
2067 }
2068
2069 if ((tx_flags & TX_CMD_FLG_RTS_MSK) || (tx_flags & TX_CMD_FLG_CTS_MSK))
2070 tx_flags |= TX_CMD_FLG_FULL_TXOP_PROT_MSK;
2071
2072 tx_flags &= ~(TX_CMD_FLG_ANT_SEL_MSK);
Harvey Harrisonfd7c8a42008-06-11 14:21:56 -07002073 if (ieee80211_is_mgmt(fc)) {
2074 if (ieee80211_is_assoc_req(fc) || ieee80211_is_reassoc_req(fc))
Winkler, Tomase52119c2008-12-22 11:31:19 +08002075 tx->timeout.pm_frame_timeout = cpu_to_le16(3);
Zhu Yib481de92007-09-25 17:54:57 -07002076 else
Winkler, Tomase52119c2008-12-22 11:31:19 +08002077 tx->timeout.pm_frame_timeout = cpu_to_le16(2);
Mohamed Abbasab53d8a2008-03-25 16:33:36 -07002078 } else {
Winkler, Tomase52119c2008-12-22 11:31:19 +08002079 tx->timeout.pm_frame_timeout = 0;
Mohamed Abbasab53d8a2008-03-25 16:33:36 -07002080#ifdef CONFIG_IWL3945_LEDS
2081 priv->rxtxpackets += le16_to_cpu(cmd->cmd.tx.len);
2082#endif
2083 }
Zhu Yib481de92007-09-25 17:54:57 -07002084
Winkler, Tomase52119c2008-12-22 11:31:19 +08002085 tx->driver_txop = 0;
2086 tx->tx_flags = tx_flags;
2087 tx->next_frame_len = 0;
Zhu Yib481de92007-09-25 17:54:57 -07002088}
2089
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002090/**
2091 * iwl3945_get_sta_id - Find station's index within station table
2092 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002093static int iwl3945_get_sta_id(struct iwl_priv *priv, struct ieee80211_hdr *hdr)
Zhu Yib481de92007-09-25 17:54:57 -07002094{
2095 int sta_id;
2096 u16 fc = le16_to_cpu(hdr->frame_control);
2097
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002098 /* If this frame is broadcast or management, use broadcast station id */
Zhu Yib481de92007-09-25 17:54:57 -07002099 if (((fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_DATA) ||
2100 is_multicast_ether_addr(hdr->addr1))
Abhijeet Kolekar3832ec92008-12-19 10:37:26 +08002101 return priv->hw_params.bcast_sta_id;
Zhu Yib481de92007-09-25 17:54:57 -07002102
2103 switch (priv->iw_mode) {
2104
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002105 /* If we are a client station in a BSS network, use the special
2106 * AP station entry (that's the only station we communicate with) */
Johannes Berg05c914f2008-09-11 00:01:58 +02002107 case NL80211_IFTYPE_STATION:
Zhu Yib481de92007-09-25 17:54:57 -07002108 return IWL_AP_ID;
2109
2110 /* If we are an AP, then find the station, or use BCAST */
Johannes Berg05c914f2008-09-11 00:01:58 +02002111 case NL80211_IFTYPE_AP:
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002112 sta_id = iwl3945_hw_find_station(priv, hdr->addr1);
Zhu Yib481de92007-09-25 17:54:57 -07002113 if (sta_id != IWL_INVALID_STATION)
2114 return sta_id;
Abhijeet Kolekar3832ec92008-12-19 10:37:26 +08002115 return priv->hw_params.bcast_sta_id;
Zhu Yib481de92007-09-25 17:54:57 -07002116
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002117 /* If this frame is going out to an IBSS network, find the station,
2118 * or create a new station table entry */
Johannes Berg05c914f2008-09-11 00:01:58 +02002119 case NL80211_IFTYPE_ADHOC: {
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002120 /* Create new station table entry */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002121 sta_id = iwl3945_hw_find_station(priv, hdr->addr1);
Zhu Yib481de92007-09-25 17:54:57 -07002122 if (sta_id != IWL_INVALID_STATION)
2123 return sta_id;
2124
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002125 sta_id = iwl3945_add_station(priv, hdr->addr1, 0, CMD_ASYNC);
Zhu Yib481de92007-09-25 17:54:57 -07002126
2127 if (sta_id != IWL_INVALID_STATION)
2128 return sta_id;
2129
Johannes Berge1749612008-10-27 15:59:26 -07002130 IWL_DEBUG_DROP("Station %pM not in station map. "
Zhu Yib481de92007-09-25 17:54:57 -07002131 "Defaulting to broadcast...\n",
Johannes Berge1749612008-10-27 15:59:26 -07002132 hdr->addr1);
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08002133 iwl_print_hex_dump(priv, IWL_DL_DROP, (u8 *) hdr, sizeof(*hdr));
Abhijeet Kolekar3832ec92008-12-19 10:37:26 +08002134 return priv->hw_params.bcast_sta_id;
Joe Perches0795af52007-10-03 17:59:30 -07002135 }
Stefanik Gábor914233d2008-06-30 17:23:30 +08002136 /* If we are in monitor mode, use BCAST. This is required for
2137 * packet injection. */
Johannes Berg05c914f2008-09-11 00:01:58 +02002138 case NL80211_IFTYPE_MONITOR:
Abhijeet Kolekar3832ec92008-12-19 10:37:26 +08002139 return priv->hw_params.bcast_sta_id;
Stefanik Gábor914233d2008-06-30 17:23:30 +08002140
Zhu Yib481de92007-09-25 17:54:57 -07002141 default:
Winkler, Tomas39aadf82008-12-19 10:37:32 +08002142 IWL_WARN(priv, "Unknown mode of operation: %d\n",
2143 priv->iw_mode);
Abhijeet Kolekar3832ec92008-12-19 10:37:26 +08002144 return priv->hw_params.bcast_sta_id;
Zhu Yib481de92007-09-25 17:54:57 -07002145 }
2146}
2147
2148/*
2149 * start REPLY_TX command process
2150 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002151static int iwl3945_tx_skb(struct iwl_priv *priv, struct sk_buff *skb)
Zhu Yib481de92007-09-25 17:54:57 -07002152{
2153 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
Johannes Berge039fa42008-05-15 12:55:29 +02002154 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
Winkler, Tomase52119c2008-12-22 11:31:19 +08002155 struct iwl3945_tx_cmd *tx;
Samuel Ortiz188cf6c2008-12-22 11:31:16 +08002156 struct iwl_tx_queue *txq = NULL;
Samuel Ortizd20b3c62008-12-19 10:37:15 +08002157 struct iwl_queue *q = NULL;
Winkler, Tomase52119c2008-12-22 11:31:19 +08002158 struct iwl_cmd *out_cmd = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07002159 dma_addr_t phys_addr;
2160 dma_addr_t txcmd_phys;
Winkler, Tomase52119c2008-12-22 11:31:19 +08002161 int txq_id = skb_get_queue_mapping(skb);
Tomas Winkler54dbb522008-05-15 13:54:06 +08002162 u16 len, idx, len_org, hdr_len;
2163 u8 id;
2164 u8 unicast;
Zhu Yib481de92007-09-25 17:54:57 -07002165 u8 sta_id;
Tomas Winkler54dbb522008-05-15 13:54:06 +08002166 u8 tid = 0;
Zhu Yib481de92007-09-25 17:54:57 -07002167 u16 seq_number = 0;
Harvey Harrisonfd7c8a42008-06-11 14:21:56 -07002168 __le16 fc;
Zhu Yib481de92007-09-25 17:54:57 -07002169 u8 wait_write_ptr = 0;
Tomas Winkler54dbb522008-05-15 13:54:06 +08002170 u8 *qc = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07002171 unsigned long flags;
2172 int rc;
2173
2174 spin_lock_irqsave(&priv->lock, flags);
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08002175 if (iwl_is_rfkill(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07002176 IWL_DEBUG_DROP("Dropping - RF KILL\n");
2177 goto drop_unlock;
2178 }
2179
Johannes Berge039fa42008-05-15 12:55:29 +02002180 if ((ieee80211_get_tx_rate(priv->hw, info)->hw_value & 0xFF) == IWL_INVALID_RATE) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08002181 IWL_ERR(priv, "ERROR: No TX rate available.\n");
Zhu Yib481de92007-09-25 17:54:57 -07002182 goto drop_unlock;
2183 }
2184
2185 unicast = !is_multicast_ether_addr(hdr->addr1);
2186 id = 0;
2187
Harvey Harrisonfd7c8a42008-06-11 14:21:56 -07002188 fc = hdr->frame_control;
Zhu Yib481de92007-09-25 17:54:57 -07002189
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08002190#ifdef CONFIG_IWL3945_DEBUG
Zhu Yib481de92007-09-25 17:54:57 -07002191 if (ieee80211_is_auth(fc))
2192 IWL_DEBUG_TX("Sending AUTH frame\n");
Harvey Harrisonfd7c8a42008-06-11 14:21:56 -07002193 else if (ieee80211_is_assoc_req(fc))
Zhu Yib481de92007-09-25 17:54:57 -07002194 IWL_DEBUG_TX("Sending ASSOC frame\n");
Harvey Harrisonfd7c8a42008-06-11 14:21:56 -07002195 else if (ieee80211_is_reassoc_req(fc))
Zhu Yib481de92007-09-25 17:54:57 -07002196 IWL_DEBUG_TX("Sending REASSOC frame\n");
2197#endif
2198
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08002199 /* drop all data frame if we are not associated */
Stefanik Gábor914233d2008-06-30 17:23:30 +08002200 if (ieee80211_is_data(fc) &&
Johannes Berg05c914f2008-09-11 00:01:58 +02002201 (priv->iw_mode != NL80211_IFTYPE_MONITOR) && /* packet injection */
Stefanik Gábor914233d2008-06-30 17:23:30 +08002202 (!iwl3945_is_associated(priv) ||
Johannes Berg05c914f2008-09-11 00:01:58 +02002203 ((priv->iw_mode == NL80211_IFTYPE_STATION) && !priv->assoc_id))) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002204 IWL_DEBUG_DROP("Dropping - !iwl3945_is_associated\n");
Zhu Yib481de92007-09-25 17:54:57 -07002205 goto drop_unlock;
2206 }
2207
2208 spin_unlock_irqrestore(&priv->lock, flags);
2209
Harvey Harrison7294ec92008-07-15 18:43:59 -07002210 hdr_len = ieee80211_hdrlen(fc);
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002211
2212 /* Find (or create) index into station table for destination station */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002213 sta_id = iwl3945_get_sta_id(priv, hdr);
Zhu Yib481de92007-09-25 17:54:57 -07002214 if (sta_id == IWL_INVALID_STATION) {
Johannes Berge1749612008-10-27 15:59:26 -07002215 IWL_DEBUG_DROP("Dropping - INVALID STATION: %pM\n",
2216 hdr->addr1);
Zhu Yib481de92007-09-25 17:54:57 -07002217 goto drop;
2218 }
2219
2220 IWL_DEBUG_RATE("station Id %d\n", sta_id);
2221
Harvey Harrisonfd7c8a42008-06-11 14:21:56 -07002222 if (ieee80211_is_data_qos(fc)) {
2223 qc = ieee80211_get_qos_ctl(hdr);
Harvey Harrison7294ec92008-07-15 18:43:59 -07002224 tid = qc[0] & IEEE80211_QOS_CTL_TID_MASK;
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08002225 seq_number = priv->stations_39[sta_id].tid[tid].seq_number &
Zhu Yib481de92007-09-25 17:54:57 -07002226 IEEE80211_SCTL_SEQ;
2227 hdr->seq_ctrl = cpu_to_le16(seq_number) |
2228 (hdr->seq_ctrl &
2229 __constant_cpu_to_le16(IEEE80211_SCTL_FRAG));
2230 seq_number += 0x10;
2231 }
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002232
2233 /* Descriptor for chosen Tx queue */
Samuel Ortiz188cf6c2008-12-22 11:31:16 +08002234 txq = &priv->txq[txq_id];
Zhu Yib481de92007-09-25 17:54:57 -07002235 q = &txq->q;
2236
2237 spin_lock_irqsave(&priv->lock, flags);
2238
Tomas Winklerfc4b6852007-10-25 17:15:24 +08002239 idx = get_cmd_index(q, q->write_ptr, 0);
Zhu Yib481de92007-09-25 17:54:57 -07002240
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002241 /* Set up driver data for this TFD */
Winkler, Tomasdbb66542008-12-22 11:31:14 +08002242 memset(&(txq->txb[q->write_ptr]), 0, sizeof(struct iwl_tx_info));
Tomas Winklerfc4b6852007-10-25 17:15:24 +08002243 txq->txb[q->write_ptr].skb[0] = skb;
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002244
2245 /* Init first empty entry in queue's array of Tx/cmd buffers */
Samuel Ortiz188cf6c2008-12-22 11:31:16 +08002246 out_cmd = txq->cmd[idx];
Winkler, Tomase52119c2008-12-22 11:31:19 +08002247 tx = (struct iwl3945_tx_cmd *)out_cmd->cmd.payload;
Zhu Yib481de92007-09-25 17:54:57 -07002248 memset(&out_cmd->hdr, 0, sizeof(out_cmd->hdr));
Winkler, Tomase52119c2008-12-22 11:31:19 +08002249 memset(tx, 0, sizeof(*tx));
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002250
2251 /*
2252 * Set up the Tx-command (not MAC!) header.
2253 * Store the chosen Tx queue and TFD index within the sequence field;
2254 * after Tx, uCode's Tx response will return this value so driver can
2255 * locate the frame within the tx queue and do post-tx processing.
2256 */
Zhu Yib481de92007-09-25 17:54:57 -07002257 out_cmd->hdr.cmd = REPLY_TX;
2258 out_cmd->hdr.sequence = cpu_to_le16((u16)(QUEUE_TO_SEQ(txq_id) |
Tomas Winklerfc4b6852007-10-25 17:15:24 +08002259 INDEX_TO_SEQ(q->write_ptr)));
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002260
2261 /* Copy MAC header from skb into command buffer */
Winkler, Tomase52119c2008-12-22 11:31:19 +08002262 memcpy(tx->hdr, hdr, hdr_len);
Zhu Yib481de92007-09-25 17:54:57 -07002263
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002264 /*
2265 * Use the first empty entry in this queue's command buffer array
2266 * to contain the Tx command and MAC header concatenated together
2267 * (payload data will be in another buffer).
2268 * Size of this varies, due to varying MAC header length.
2269 * If end is not dword aligned, we'll have 2 extra bytes at the end
2270 * of the MAC header (device reads on dword boundaries).
2271 * We'll tell device about this padding later.
2272 */
Abhijeet Kolekar3832ec92008-12-19 10:37:26 +08002273 len = sizeof(struct iwl3945_tx_cmd) +
Tomas Winkler4c897252008-12-19 10:37:05 +08002274 sizeof(struct iwl_cmd_header) + hdr_len;
Zhu Yib481de92007-09-25 17:54:57 -07002275
2276 len_org = len;
2277 len = (len + 3) & ~3;
2278
2279 if (len_org != len)
2280 len_org = 1;
2281 else
2282 len_org = 0;
2283
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002284 /* Physical address of this Tx command's header (not MAC header!),
2285 * within command buffer array. */
Samuel Ortiz188cf6c2008-12-22 11:31:16 +08002286 txcmd_phys = pci_map_single(priv->pci_dev,
2287 out_cmd, sizeof(struct iwl_cmd),
2288 PCI_DMA_TODEVICE);
2289 pci_unmap_addr_set(&out_cmd->meta, mapping, txcmd_phys);
2290 pci_unmap_len_set(&out_cmd->meta, len, sizeof(struct iwl_cmd));
2291 /* Add buffer containing Tx command and MAC(!) header to TFD's
2292 * first entry */
2293 txcmd_phys += offsetof(struct iwl_cmd, hdr);
Zhu Yib481de92007-09-25 17:54:57 -07002294
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002295 /* Add buffer containing Tx command and MAC(!) header to TFD's
2296 * first entry */
Samuel Ortiz7aaa1d72009-01-19 15:30:26 -08002297 priv->cfg->ops->lib->txq_attach_buf_to_tfd(priv, txq,
2298 txcmd_phys, len, 1, 0);
Zhu Yib481de92007-09-25 17:54:57 -07002299
Johannes Bergd0f09802008-07-29 11:32:07 +02002300 if (info->control.hw_key)
Johannes Berge039fa42008-05-15 12:55:29 +02002301 iwl3945_build_tx_cmd_hwcrypto(priv, info, out_cmd, skb, 0);
Zhu Yib481de92007-09-25 17:54:57 -07002302
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002303 /* Set up TFD's 2nd entry to point directly to remainder of skb,
2304 * if any (802.11 null frames have no payload). */
Zhu Yib481de92007-09-25 17:54:57 -07002305 len = skb->len - hdr_len;
2306 if (len) {
2307 phys_addr = pci_map_single(priv->pci_dev, skb->data + hdr_len,
2308 len, PCI_DMA_TODEVICE);
Samuel Ortiz7aaa1d72009-01-19 15:30:26 -08002309 priv->cfg->ops->lib->txq_attach_buf_to_tfd(priv, txq,
2310 phys_addr, len,
2311 0, U32_PAD(len));
Zhu Yib481de92007-09-25 17:54:57 -07002312 }
2313
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002314 /* Total # bytes to be transmitted */
Zhu Yib481de92007-09-25 17:54:57 -07002315 len = (u16)skb->len;
Winkler, Tomase52119c2008-12-22 11:31:19 +08002316 tx->len = cpu_to_le16(len);
Zhu Yib481de92007-09-25 17:54:57 -07002317
2318 /* TODO need this for burst mode later on */
Winkler, Tomase52119c2008-12-22 11:31:19 +08002319 iwl3945_build_tx_cmd_basic(priv, out_cmd, info, hdr, sta_id);
Zhu Yib481de92007-09-25 17:54:57 -07002320
2321 /* set is_hcca to 0; it probably will never be implemented */
Johannes Berge039fa42008-05-15 12:55:29 +02002322 iwl3945_hw_build_tx_cmd_rate(priv, out_cmd, info, hdr, sta_id, 0);
Zhu Yib481de92007-09-25 17:54:57 -07002323
Winkler, Tomase52119c2008-12-22 11:31:19 +08002324 tx->tx_flags &= ~TX_CMD_FLG_ANT_A_MSK;
2325 tx->tx_flags &= ~TX_CMD_FLG_ANT_B_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07002326
Harvey Harrison8b7b1e02008-06-11 14:21:56 -07002327 if (!ieee80211_has_morefrags(hdr->frame_control)) {
Zhu Yib481de92007-09-25 17:54:57 -07002328 txq->need_update = 1;
Tomas Winkler3ac7f142008-07-21 02:40:14 +03002329 if (qc)
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08002330 priv->stations_39[sta_id].tid[tid].seq_number = seq_number;
Zhu Yib481de92007-09-25 17:54:57 -07002331 } else {
2332 wait_write_ptr = 1;
2333 txq->need_update = 0;
2334 }
2335
Winkler, Tomase52119c2008-12-22 11:31:19 +08002336 iwl_print_hex_dump(priv, IWL_DL_TX, tx, sizeof(*tx));
Zhu Yib481de92007-09-25 17:54:57 -07002337
Winkler, Tomase52119c2008-12-22 11:31:19 +08002338 iwl_print_hex_dump(priv, IWL_DL_TX, (u8 *)tx->hdr,
Harvey Harrison7294ec92008-07-15 18:43:59 -07002339 ieee80211_hdrlen(fc));
Zhu Yib481de92007-09-25 17:54:57 -07002340
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002341 /* Tell device the write index *just past* this latest filled TFD */
Tomas Winklerc54b6792008-03-06 17:36:53 -08002342 q->write_ptr = iwl_queue_inc_wrap(q->write_ptr, q->n_bd);
Samuel Ortiz4f3602c2009-01-19 15:30:25 -08002343 rc = iwl_txq_update_write_ptr(priv, txq);
Zhu Yib481de92007-09-25 17:54:57 -07002344 spin_unlock_irqrestore(&priv->lock, flags);
2345
2346 if (rc)
2347 return rc;
2348
Samuel Ortizd20b3c62008-12-19 10:37:15 +08002349 if ((iwl_queue_space(q) < q->high_mark)
Zhu Yib481de92007-09-25 17:54:57 -07002350 && priv->mac80211_registered) {
2351 if (wait_write_ptr) {
2352 spin_lock_irqsave(&priv->lock, flags);
2353 txq->need_update = 1;
Samuel Ortiz4f3602c2009-01-19 15:30:25 -08002354 iwl_txq_update_write_ptr(priv, txq);
Zhu Yib481de92007-09-25 17:54:57 -07002355 spin_unlock_irqrestore(&priv->lock, flags);
2356 }
2357
Johannes Berge2530082008-05-17 00:57:14 +02002358 ieee80211_stop_queue(priv->hw, skb_get_queue_mapping(skb));
Zhu Yib481de92007-09-25 17:54:57 -07002359 }
2360
2361 return 0;
2362
2363drop_unlock:
2364 spin_unlock_irqrestore(&priv->lock, flags);
2365drop:
2366 return -1;
2367}
2368
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002369static void iwl3945_set_rate(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002370{
Johannes Berg8318d782008-01-24 19:38:38 +01002371 const struct ieee80211_supported_band *sband = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07002372 struct ieee80211_rate *rate;
2373 int i;
2374
Kolekar, Abhijeetcbba18c2008-12-19 10:37:42 +08002375 sband = iwl_get_hw_mode(priv, priv->band);
Johannes Berg8318d782008-01-24 19:38:38 +01002376 if (!sband) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08002377 IWL_ERR(priv, "Failed to set rate: unable to get hw mode\n");
Saleem Abdulrasoolc4ba9622007-11-18 23:59:08 -08002378 return;
2379 }
Zhu Yib481de92007-09-25 17:54:57 -07002380
2381 priv->active_rate = 0;
2382 priv->active_rate_basic = 0;
2383
Johannes Berg8318d782008-01-24 19:38:38 +01002384 IWL_DEBUG_RATE("Setting rates for %s GHz\n",
2385 sband->band == IEEE80211_BAND_2GHZ ? "2.4" : "5");
Zhu Yib481de92007-09-25 17:54:57 -07002386
Johannes Berg8318d782008-01-24 19:38:38 +01002387 for (i = 0; i < sband->n_bitrates; i++) {
2388 rate = &sband->bitrates[i];
2389 if ((rate->hw_value < IWL_RATE_COUNT) &&
2390 !(rate->flags & IEEE80211_CHAN_DISABLED)) {
2391 IWL_DEBUG_RATE("Adding rate index %d (plcp %d)\n",
2392 rate->hw_value, iwl3945_rates[rate->hw_value].plcp);
2393 priv->active_rate |= (1 << rate->hw_value);
2394 }
Zhu Yib481de92007-09-25 17:54:57 -07002395 }
2396
2397 IWL_DEBUG_RATE("Set active_rate = %0x, active_rate_basic = %0x\n",
2398 priv->active_rate, priv->active_rate_basic);
2399
2400 /*
2401 * If a basic rate is configured, then use it (adding IWL_RATE_1M_MASK)
2402 * otherwise set it to the default of all CCK rates and 6, 12, 24 for
2403 * OFDM
2404 */
2405 if (priv->active_rate_basic & IWL_CCK_BASIC_RATES_MASK)
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08002406 priv->staging39_rxon.cck_basic_rates =
Zhu Yib481de92007-09-25 17:54:57 -07002407 ((priv->active_rate_basic &
2408 IWL_CCK_RATES_MASK) >> IWL_FIRST_CCK_RATE) & 0xF;
2409 else
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08002410 priv->staging39_rxon.cck_basic_rates =
Zhu Yib481de92007-09-25 17:54:57 -07002411 (IWL_CCK_BASIC_RATES_MASK >> IWL_FIRST_CCK_RATE) & 0xF;
2412
2413 if (priv->active_rate_basic & IWL_OFDM_BASIC_RATES_MASK)
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08002414 priv->staging39_rxon.ofdm_basic_rates =
Zhu Yib481de92007-09-25 17:54:57 -07002415 ((priv->active_rate_basic &
2416 (IWL_OFDM_BASIC_RATES_MASK | IWL_RATE_6M_MASK)) >>
2417 IWL_FIRST_OFDM_RATE) & 0xFF;
2418 else
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08002419 priv->staging39_rxon.ofdm_basic_rates =
Zhu Yib481de92007-09-25 17:54:57 -07002420 (IWL_OFDM_BASIC_RATES_MASK >> IWL_FIRST_OFDM_RATE) & 0xFF;
2421}
2422
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002423static void iwl3945_radio_kill_sw(struct iwl_priv *priv, int disable_radio)
Zhu Yib481de92007-09-25 17:54:57 -07002424{
2425 unsigned long flags;
2426
2427 if (!!disable_radio == test_bit(STATUS_RF_KILL_SW, &priv->status))
2428 return;
2429
2430 IWL_DEBUG_RF_KILL("Manual SW RF KILL set to: RADIO %s\n",
2431 disable_radio ? "OFF" : "ON");
2432
2433 if (disable_radio) {
Winkler, Tomasaf0053d2009-01-19 15:30:23 -08002434 iwl_scan_cancel(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002435 /* FIXME: This is a workaround for AP */
Johannes Berg05c914f2008-09-11 00:01:58 +02002436 if (priv->iw_mode != NL80211_IFTYPE_AP) {
Zhu Yib481de92007-09-25 17:54:57 -07002437 spin_lock_irqsave(&priv->lock, flags);
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08002438 iwl_write32(priv, CSR_UCODE_DRV_GP1_SET,
Zhu Yib481de92007-09-25 17:54:57 -07002439 CSR_UCODE_SW_BIT_RFKILL);
2440 spin_unlock_irqrestore(&priv->lock, flags);
Samuel Ortizc4962942008-12-22 11:31:18 +08002441 iwl_send_card_state(priv, CARD_STATE_CMD_DISABLE, 0);
Zhu Yib481de92007-09-25 17:54:57 -07002442 set_bit(STATUS_RF_KILL_SW, &priv->status);
2443 }
2444 return;
2445 }
2446
2447 spin_lock_irqsave(&priv->lock, flags);
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08002448 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
Zhu Yib481de92007-09-25 17:54:57 -07002449
2450 clear_bit(STATUS_RF_KILL_SW, &priv->status);
2451 spin_unlock_irqrestore(&priv->lock, flags);
2452
2453 /* wake up ucode */
2454 msleep(10);
2455
2456 spin_lock_irqsave(&priv->lock, flags);
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08002457 iwl_read32(priv, CSR_UCODE_DRV_GP1);
2458 if (!iwl_grab_nic_access(priv))
2459 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002460 spin_unlock_irqrestore(&priv->lock, flags);
2461
2462 if (test_bit(STATUS_RF_KILL_HW, &priv->status)) {
2463 IWL_DEBUG_RF_KILL("Can not turn radio back on - "
2464 "disabled by HW switch\n");
2465 return;
2466 }
2467
Zhu Yi808e72a2008-06-12 09:47:17 +08002468 if (priv->is_open)
2469 queue_work(priv->workqueue, &priv->restart);
Zhu Yib481de92007-09-25 17:54:57 -07002470 return;
2471}
2472
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002473void iwl3945_set_decrypted_flag(struct iwl_priv *priv, struct sk_buff *skb,
Zhu Yib481de92007-09-25 17:54:57 -07002474 u32 decrypt_res, struct ieee80211_rx_status *stats)
2475{
2476 u16 fc =
2477 le16_to_cpu(((struct ieee80211_hdr *)skb->data)->frame_control);
2478
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08002479 if (priv->active39_rxon.filter_flags & RXON_FILTER_DIS_DECRYPT_MSK)
Zhu Yib481de92007-09-25 17:54:57 -07002480 return;
2481
2482 if (!(fc & IEEE80211_FCTL_PROTECTED))
2483 return;
2484
2485 IWL_DEBUG_RX("decrypt_res:0x%x\n", decrypt_res);
2486 switch (decrypt_res & RX_RES_STATUS_SEC_TYPE_MSK) {
2487 case RX_RES_STATUS_SEC_TYPE_TKIP:
2488 if ((decrypt_res & RX_RES_STATUS_DECRYPT_TYPE_MSK) ==
2489 RX_RES_STATUS_BAD_ICV_MIC)
2490 stats->flag |= RX_FLAG_MMIC_ERROR;
2491 case RX_RES_STATUS_SEC_TYPE_WEP:
2492 case RX_RES_STATUS_SEC_TYPE_CCMP:
2493 if ((decrypt_res & RX_RES_STATUS_DECRYPT_TYPE_MSK) ==
2494 RX_RES_STATUS_DECRYPT_OK) {
2495 IWL_DEBUG_RX("hw decrypt successfully!!!\n");
2496 stats->flag |= RX_FLAG_DECRYPTED;
2497 }
2498 break;
2499
2500 default:
2501 break;
2502 }
2503}
2504
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08002505#ifdef CONFIG_IWL3945_SPECTRUM_MEASUREMENT
Zhu Yib481de92007-09-25 17:54:57 -07002506
2507#include "iwl-spectrum.h"
2508
2509#define BEACON_TIME_MASK_LOW 0x00FFFFFF
2510#define BEACON_TIME_MASK_HIGH 0xFF000000
2511#define TIME_UNIT 1024
2512
2513/*
2514 * extended beacon time format
2515 * time in usec will be changed into a 32-bit value in 8:24 format
2516 * the high 1 byte is the beacon counts
2517 * the lower 3 bytes is the time in usec within one beacon interval
2518 */
2519
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002520static u32 iwl3945_usecs_to_beacons(u32 usec, u32 beacon_interval)
Zhu Yib481de92007-09-25 17:54:57 -07002521{
2522 u32 quot;
2523 u32 rem;
2524 u32 interval = beacon_interval * 1024;
2525
2526 if (!interval || !usec)
2527 return 0;
2528
2529 quot = (usec / interval) & (BEACON_TIME_MASK_HIGH >> 24);
2530 rem = (usec % interval) & BEACON_TIME_MASK_LOW;
2531
2532 return (quot << 24) + rem;
2533}
2534
2535/* base is usually what we get from ucode with each received frame,
2536 * the same as HW timer counter counting down
2537 */
2538
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002539static __le32 iwl3945_add_beacon_time(u32 base, u32 addon, u32 beacon_interval)
Zhu Yib481de92007-09-25 17:54:57 -07002540{
2541 u32 base_low = base & BEACON_TIME_MASK_LOW;
2542 u32 addon_low = addon & BEACON_TIME_MASK_LOW;
2543 u32 interval = beacon_interval * TIME_UNIT;
2544 u32 res = (base & BEACON_TIME_MASK_HIGH) +
2545 (addon & BEACON_TIME_MASK_HIGH);
2546
2547 if (base_low > addon_low)
2548 res += base_low - addon_low;
2549 else if (base_low < addon_low) {
2550 res += interval + base_low - addon_low;
2551 res += (1 << 24);
2552 } else
2553 res += (1 << 24);
2554
2555 return cpu_to_le32(res);
2556}
2557
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002558static int iwl3945_get_measurement(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07002559 struct ieee80211_measurement_params *params,
2560 u8 type)
2561{
Tomas Winkler600c0e12008-12-19 10:37:04 +08002562 struct iwl_spectrum_cmd spectrum;
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08002563 struct iwl_rx_packet *res;
Winkler, Tomasc2d79b42008-12-19 10:37:34 +08002564 struct iwl_host_cmd cmd = {
Zhu Yib481de92007-09-25 17:54:57 -07002565 .id = REPLY_SPECTRUM_MEASUREMENT_CMD,
2566 .data = (void *)&spectrum,
2567 .meta.flags = CMD_WANT_SKB,
2568 };
2569 u32 add_time = le64_to_cpu(params->start_time);
2570 int rc;
2571 int spectrum_resp_status;
2572 int duration = le16_to_cpu(params->duration);
2573
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002574 if (iwl3945_is_associated(priv))
Zhu Yib481de92007-09-25 17:54:57 -07002575 add_time =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002576 iwl3945_usecs_to_beacons(
Zhu Yib481de92007-09-25 17:54:57 -07002577 le64_to_cpu(params->start_time) - priv->last_tsf,
2578 le16_to_cpu(priv->rxon_timing.beacon_interval));
2579
2580 memset(&spectrum, 0, sizeof(spectrum));
2581
2582 spectrum.channel_count = cpu_to_le16(1);
2583 spectrum.flags =
2584 RXON_FLG_TSF2HOST_MSK | RXON_FLG_ANT_A_MSK | RXON_FLG_DIS_DIV_MSK;
2585 spectrum.filter_flags = MEASUREMENT_FILTER_FLAG;
2586 cmd.len = sizeof(spectrum);
2587 spectrum.len = cpu_to_le16(cmd.len - sizeof(spectrum.len));
2588
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002589 if (iwl3945_is_associated(priv))
Zhu Yib481de92007-09-25 17:54:57 -07002590 spectrum.start_time =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002591 iwl3945_add_beacon_time(priv->last_beacon_time,
Zhu Yib481de92007-09-25 17:54:57 -07002592 add_time,
2593 le16_to_cpu(priv->rxon_timing.beacon_interval));
2594 else
2595 spectrum.start_time = 0;
2596
2597 spectrum.channels[0].duration = cpu_to_le32(duration * TIME_UNIT);
2598 spectrum.channels[0].channel = params->channel;
2599 spectrum.channels[0].type = type;
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08002600 if (priv->active39_rxon.flags & RXON_FLG_BAND_24G_MSK)
Zhu Yib481de92007-09-25 17:54:57 -07002601 spectrum.flags |= RXON_FLG_BAND_24G_MSK |
2602 RXON_FLG_AUTO_DETECT_MSK | RXON_FLG_TGG_PROTECT_MSK;
2603
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002604 rc = iwl3945_send_cmd_sync(priv, &cmd);
Zhu Yib481de92007-09-25 17:54:57 -07002605 if (rc)
2606 return rc;
2607
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08002608 res = (struct iwl_rx_packet *)cmd.meta.u.skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07002609 if (res->hdr.flags & IWL_CMD_FAILED_MSK) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08002610 IWL_ERR(priv, "Bad return from REPLY_RX_ON_ASSOC command\n");
Zhu Yib481de92007-09-25 17:54:57 -07002611 rc = -EIO;
2612 }
2613
2614 spectrum_resp_status = le16_to_cpu(res->u.spectrum.status);
2615 switch (spectrum_resp_status) {
2616 case 0: /* Command will be handled */
2617 if (res->u.spectrum.id != 0xff) {
Ian Schrambc434dd2007-10-25 17:15:29 +08002618 IWL_DEBUG_INFO("Replaced existing measurement: %d\n",
2619 res->u.spectrum.id);
Zhu Yib481de92007-09-25 17:54:57 -07002620 priv->measurement_status &= ~MEASUREMENT_READY;
2621 }
2622 priv->measurement_status |= MEASUREMENT_ACTIVE;
2623 rc = 0;
2624 break;
2625
2626 case 1: /* Command will not be handled */
2627 rc = -EAGAIN;
2628 break;
2629 }
2630
2631 dev_kfree_skb_any(cmd.meta.u.skb);
2632
2633 return rc;
2634}
2635#endif
2636
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002637static void iwl3945_rx_reply_alive(struct iwl_priv *priv,
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08002638 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07002639{
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08002640 struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
2641 struct iwl_alive_resp *palive;
Zhu Yib481de92007-09-25 17:54:57 -07002642 struct delayed_work *pwork;
2643
2644 palive = &pkt->u.alive_frame;
2645
2646 IWL_DEBUG_INFO("Alive ucode status 0x%08X revision "
2647 "0x%01X 0x%01X\n",
2648 palive->is_valid, palive->ver_type,
2649 palive->ver_subtype);
2650
2651 if (palive->ver_subtype == INITIALIZE_SUBTYPE) {
2652 IWL_DEBUG_INFO("Initialization Alive received.\n");
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08002653 memcpy(&priv->card_alive_init, &pkt->u.alive_frame,
2654 sizeof(struct iwl_alive_resp));
Zhu Yib481de92007-09-25 17:54:57 -07002655 pwork = &priv->init_alive_start;
2656 } else {
2657 IWL_DEBUG_INFO("Runtime Alive received.\n");
2658 memcpy(&priv->card_alive, &pkt->u.alive_frame,
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08002659 sizeof(struct iwl_alive_resp));
Zhu Yib481de92007-09-25 17:54:57 -07002660 pwork = &priv->alive_start;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002661 iwl3945_disable_events(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002662 }
2663
2664 /* We delay the ALIVE response by 5ms to
2665 * give the HW RF Kill time to activate... */
2666 if (palive->is_valid == UCODE_VALID_OK)
2667 queue_delayed_work(priv->workqueue, pwork,
2668 msecs_to_jiffies(5));
2669 else
Winkler, Tomas39aadf82008-12-19 10:37:32 +08002670 IWL_WARN(priv, "uCode did not respond OK.\n");
Zhu Yib481de92007-09-25 17:54:57 -07002671}
2672
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002673static void iwl3945_rx_reply_add_sta(struct iwl_priv *priv,
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08002674 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07002675{
Helmut Schaac7e035a2009-01-19 13:02:15 +01002676#ifdef CONFIG_IWLWIFI_DEBUG
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08002677 struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
Helmut Schaac7e035a2009-01-19 13:02:15 +01002678#endif
Zhu Yib481de92007-09-25 17:54:57 -07002679
2680 IWL_DEBUG_RX("Received REPLY_ADD_STA: 0x%02X\n", pkt->u.status);
2681 return;
2682}
2683
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002684static void iwl3945_rx_reply_error(struct iwl_priv *priv,
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08002685 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07002686{
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08002687 struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07002688
Winkler, Tomas15b16872008-12-19 10:37:33 +08002689 IWL_ERR(priv, "Error Reply type 0x%08X cmd %s (0x%02X) "
Zhu Yib481de92007-09-25 17:54:57 -07002690 "seq 0x%04X ser 0x%08X\n",
2691 le32_to_cpu(pkt->u.err_resp.error_type),
2692 get_cmd_string(pkt->u.err_resp.cmd_id),
2693 pkt->u.err_resp.cmd_id,
2694 le16_to_cpu(pkt->u.err_resp.bad_cmd_seq_num),
2695 le32_to_cpu(pkt->u.err_resp.error_info));
2696}
2697
2698#define TX_STATUS_ENTRY(x) case TX_STATUS_FAIL_ ## x: return #x
2699
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002700static void iwl3945_rx_csa(struct iwl_priv *priv, struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07002701{
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08002702 struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08002703 struct iwl3945_rxon_cmd *rxon = (void *)&priv->active39_rxon;
Tomas Winkler600c0e12008-12-19 10:37:04 +08002704 struct iwl_csa_notification *csa = &(pkt->u.csa_notif);
Zhu Yib481de92007-09-25 17:54:57 -07002705 IWL_DEBUG_11H("CSA notif: channel %d, status %d\n",
2706 le16_to_cpu(csa->channel), le32_to_cpu(csa->status));
2707 rxon->channel = csa->channel;
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08002708 priv->staging39_rxon.channel = csa->channel;
Zhu Yib481de92007-09-25 17:54:57 -07002709}
2710
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002711static void iwl3945_rx_spectrum_measure_notif(struct iwl_priv *priv,
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08002712 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07002713{
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08002714#ifdef CONFIG_IWL3945_SPECTRUM_MEASUREMENT
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08002715 struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
Tomas Winkler600c0e12008-12-19 10:37:04 +08002716 struct iwl_spectrum_notification *report = &(pkt->u.spectrum_notif);
Zhu Yib481de92007-09-25 17:54:57 -07002717
2718 if (!report->state) {
2719 IWL_DEBUG(IWL_DL_11H | IWL_DL_INFO,
2720 "Spectrum Measure Notification: Start\n");
2721 return;
2722 }
2723
2724 memcpy(&priv->measure_report, report, sizeof(*report));
2725 priv->measurement_status |= MEASUREMENT_READY;
2726#endif
2727}
2728
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002729static void iwl3945_rx_pm_sleep_notif(struct iwl_priv *priv,
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08002730 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07002731{
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08002732#ifdef CONFIG_IWL3945_DEBUG
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08002733 struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
Tomas Winkler600c0e12008-12-19 10:37:04 +08002734 struct iwl_sleep_notification *sleep = &(pkt->u.sleep_notif);
Zhu Yib481de92007-09-25 17:54:57 -07002735 IWL_DEBUG_RX("sleep mode: %d, src: %d\n",
2736 sleep->pm_sleep_mode, sleep->pm_wakeup_src);
2737#endif
2738}
2739
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002740static void iwl3945_rx_pm_debug_statistics_notif(struct iwl_priv *priv,
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08002741 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07002742{
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08002743 struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07002744 IWL_DEBUG_RADIO("Dumping %d bytes of unhandled "
2745 "notification for %s:\n",
2746 le32_to_cpu(pkt->len), get_cmd_string(pkt->hdr.cmd));
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08002747 iwl_print_hex_dump(priv, IWL_DL_RADIO, pkt->u.raw,
2748 le32_to_cpu(pkt->len));
Zhu Yib481de92007-09-25 17:54:57 -07002749}
2750
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002751static void iwl3945_bg_beacon_update(struct work_struct *work)
Zhu Yib481de92007-09-25 17:54:57 -07002752{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002753 struct iwl_priv *priv =
2754 container_of(work, struct iwl_priv, beacon_update);
Zhu Yib481de92007-09-25 17:54:57 -07002755 struct sk_buff *beacon;
2756
2757 /* Pull updated AP beacon from mac80211. will fail if not in AP mode */
Johannes Berge039fa42008-05-15 12:55:29 +02002758 beacon = ieee80211_beacon_get(priv->hw, priv->vif);
Zhu Yib481de92007-09-25 17:54:57 -07002759
2760 if (!beacon) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08002761 IWL_ERR(priv, "update beacon failed\n");
Zhu Yib481de92007-09-25 17:54:57 -07002762 return;
2763 }
2764
2765 mutex_lock(&priv->mutex);
2766 /* new beacon skb is allocated every time; dispose previous.*/
2767 if (priv->ibss_beacon)
2768 dev_kfree_skb(priv->ibss_beacon);
2769
2770 priv->ibss_beacon = beacon;
2771 mutex_unlock(&priv->mutex);
2772
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002773 iwl3945_send_beacon_cmd(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002774}
2775
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002776static void iwl3945_rx_beacon_notif(struct iwl_priv *priv,
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08002777 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07002778{
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08002779#ifdef CONFIG_IWL3945_DEBUG
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08002780 struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002781 struct iwl3945_beacon_notif *beacon = &(pkt->u.beacon_status);
Zhu Yib481de92007-09-25 17:54:57 -07002782 u8 rate = beacon->beacon_notify_hdr.rate;
2783
2784 IWL_DEBUG_RX("beacon status %x retries %d iss %d "
2785 "tsf %d %d rate %d\n",
2786 le32_to_cpu(beacon->beacon_notify_hdr.status) & TX_STATUS_MSK,
2787 beacon->beacon_notify_hdr.failure_frame,
2788 le32_to_cpu(beacon->ibss_mgr_status),
2789 le32_to_cpu(beacon->high_tsf),
2790 le32_to_cpu(beacon->low_tsf), rate);
2791#endif
2792
Johannes Berg05c914f2008-09-11 00:01:58 +02002793 if ((priv->iw_mode == NL80211_IFTYPE_AP) &&
Zhu Yib481de92007-09-25 17:54:57 -07002794 (!test_bit(STATUS_EXIT_PENDING, &priv->status)))
2795 queue_work(priv->workqueue, &priv->beacon_update);
2796}
2797
2798/* Service response to REPLY_SCAN_CMD (0x80) */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002799static void iwl3945_rx_reply_scan(struct iwl_priv *priv,
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08002800 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07002801{
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08002802#ifdef CONFIG_IWL3945_DEBUG
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08002803 struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
Tomas Winkler4c897252008-12-19 10:37:05 +08002804 struct iwl_scanreq_notification *notif =
2805 (struct iwl_scanreq_notification *)pkt->u.raw;
Zhu Yib481de92007-09-25 17:54:57 -07002806
2807 IWL_DEBUG_RX("Scan request status = 0x%x\n", notif->status);
2808#endif
2809}
2810
2811/* Service SCAN_START_NOTIFICATION (0x82) */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002812static void iwl3945_rx_scan_start_notif(struct iwl_priv *priv,
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08002813 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07002814{
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08002815 struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
Tomas Winkler4c897252008-12-19 10:37:05 +08002816 struct iwl_scanstart_notification *notif =
2817 (struct iwl_scanstart_notification *)pkt->u.raw;
Zhu Yib481de92007-09-25 17:54:57 -07002818 priv->scan_start_tsf = le32_to_cpu(notif->tsf_low);
2819 IWL_DEBUG_SCAN("Scan start: "
2820 "%d [802.11%s] "
2821 "(TSF: 0x%08X:%08X) - %d (beacon timer %u)\n",
2822 notif->channel,
2823 notif->band ? "bg" : "a",
2824 notif->tsf_high,
2825 notif->tsf_low, notif->status, notif->beacon_timer);
2826}
2827
2828/* Service SCAN_RESULTS_NOTIFICATION (0x83) */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002829static void iwl3945_rx_scan_results_notif(struct iwl_priv *priv,
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08002830 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07002831{
Helmut Schaac7e035a2009-01-19 13:02:15 +01002832#ifdef CONFIG_IWLWIFI_DEBUG
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08002833 struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
Tomas Winkler4c897252008-12-19 10:37:05 +08002834 struct iwl_scanresults_notification *notif =
2835 (struct iwl_scanresults_notification *)pkt->u.raw;
Helmut Schaac7e035a2009-01-19 13:02:15 +01002836#endif
Zhu Yib481de92007-09-25 17:54:57 -07002837
2838 IWL_DEBUG_SCAN("Scan ch.res: "
2839 "%d [802.11%s] "
2840 "(TSF: 0x%08X:%08X) - %d "
2841 "elapsed=%lu usec (%dms since last)\n",
2842 notif->channel,
2843 notif->band ? "bg" : "a",
2844 le32_to_cpu(notif->tsf_high),
2845 le32_to_cpu(notif->tsf_low),
2846 le32_to_cpu(notif->statistics[0]),
2847 le32_to_cpu(notif->tsf_low) - priv->scan_start_tsf,
2848 jiffies_to_msecs(elapsed_jiffies
2849 (priv->last_scan_jiffies, jiffies)));
2850
2851 priv->last_scan_jiffies = jiffies;
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08002852 priv->next_scan_jiffies = 0;
Zhu Yib481de92007-09-25 17:54:57 -07002853}
2854
2855/* Service SCAN_COMPLETE_NOTIFICATION (0x84) */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002856static void iwl3945_rx_scan_complete_notif(struct iwl_priv *priv,
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08002857 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07002858{
Helmut Schaac7e035a2009-01-19 13:02:15 +01002859#ifdef CONFIG_IWLWIFI_DEBUG
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08002860 struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
Tomas Winkler4c897252008-12-19 10:37:05 +08002861 struct iwl_scancomplete_notification *scan_notif = (void *)pkt->u.raw;
Helmut Schaac7e035a2009-01-19 13:02:15 +01002862#endif
Zhu Yib481de92007-09-25 17:54:57 -07002863
2864 IWL_DEBUG_SCAN("Scan complete: %d channels (TSF 0x%08X:%08X) - %d\n",
2865 scan_notif->scanned_channels,
2866 scan_notif->tsf_low,
2867 scan_notif->tsf_high, scan_notif->status);
2868
2869 /* The HW is no longer scanning */
2870 clear_bit(STATUS_SCAN_HW, &priv->status);
2871
2872 /* The scan completion notification came in, so kill that timer... */
2873 cancel_delayed_work(&priv->scan_check);
2874
2875 IWL_DEBUG_INFO("Scan pass on %sGHz took %dms\n",
Ron Rindjunsky66b50042008-06-25 16:46:31 +08002876 (priv->scan_bands & BIT(IEEE80211_BAND_2GHZ)) ?
2877 "2.4" : "5.2",
Zhu Yib481de92007-09-25 17:54:57 -07002878 jiffies_to_msecs(elapsed_jiffies
2879 (priv->scan_pass_start, jiffies)));
2880
Ron Rindjunsky66b50042008-06-25 16:46:31 +08002881 /* Remove this scanned band from the list of pending
2882 * bands to scan, band G precedes A in order of scanning
2883 * as seen in iwl3945_bg_request_scan */
2884 if (priv->scan_bands & BIT(IEEE80211_BAND_2GHZ))
2885 priv->scan_bands &= ~BIT(IEEE80211_BAND_2GHZ);
2886 else if (priv->scan_bands & BIT(IEEE80211_BAND_5GHZ))
2887 priv->scan_bands &= ~BIT(IEEE80211_BAND_5GHZ);
Zhu Yib481de92007-09-25 17:54:57 -07002888
2889 /* If a request to abort was given, or the scan did not succeed
2890 * then we reset the scan state machine and terminate,
2891 * re-queuing another scan if one has been requested */
2892 if (test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
2893 IWL_DEBUG_INFO("Aborted scan completed.\n");
2894 clear_bit(STATUS_SCAN_ABORTING, &priv->status);
2895 } else {
2896 /* If there are more bands on this scan pass reschedule */
2897 if (priv->scan_bands > 0)
2898 goto reschedule;
2899 }
2900
2901 priv->last_scan_jiffies = jiffies;
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08002902 priv->next_scan_jiffies = 0;
Zhu Yib481de92007-09-25 17:54:57 -07002903 IWL_DEBUG_INFO("Setting scan to off\n");
2904
2905 clear_bit(STATUS_SCANNING, &priv->status);
2906
2907 IWL_DEBUG_INFO("Scan took %dms\n",
2908 jiffies_to_msecs(elapsed_jiffies(priv->scan_start, jiffies)));
2909
2910 queue_work(priv->workqueue, &priv->scan_completed);
2911
2912 return;
2913
2914reschedule:
2915 priv->scan_pass_start = jiffies;
2916 queue_work(priv->workqueue, &priv->request_scan);
2917}
2918
2919/* Handle notification from uCode that card's power state is changing
2920 * due to software, hardware, or critical temperature RFKILL */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002921static void iwl3945_rx_card_state_notif(struct iwl_priv *priv,
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08002922 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07002923{
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08002924 struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07002925 u32 flags = le32_to_cpu(pkt->u.card_state_notif.flags);
2926 unsigned long status = priv->status;
2927
2928 IWL_DEBUG_RF_KILL("Card state received: HW:%s SW:%s\n",
2929 (flags & HW_CARD_DISABLED) ? "Kill" : "On",
2930 (flags & SW_CARD_DISABLED) ? "Kill" : "On");
2931
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08002932 iwl_write32(priv, CSR_UCODE_DRV_GP1_SET,
Zhu Yib481de92007-09-25 17:54:57 -07002933 CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
2934
2935 if (flags & HW_CARD_DISABLED)
2936 set_bit(STATUS_RF_KILL_HW, &priv->status);
2937 else
2938 clear_bit(STATUS_RF_KILL_HW, &priv->status);
2939
2940
2941 if (flags & SW_CARD_DISABLED)
2942 set_bit(STATUS_RF_KILL_SW, &priv->status);
2943 else
2944 clear_bit(STATUS_RF_KILL_SW, &priv->status);
2945
Winkler, Tomasaf0053d2009-01-19 15:30:23 -08002946 iwl_scan_cancel(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002947
2948 if ((test_bit(STATUS_RF_KILL_HW, &status) !=
2949 test_bit(STATUS_RF_KILL_HW, &priv->status)) ||
2950 (test_bit(STATUS_RF_KILL_SW, &status) !=
2951 test_bit(STATUS_RF_KILL_SW, &priv->status)))
2952 queue_work(priv->workqueue, &priv->rf_kill);
2953 else
2954 wake_up_interruptible(&priv->wait_command_queue);
2955}
2956
2957/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002958 * iwl3945_setup_rx_handlers - Initialize Rx handler callbacks
Zhu Yib481de92007-09-25 17:54:57 -07002959 *
2960 * Setup the RX handlers for each of the reply types sent from the uCode
2961 * to the host.
2962 *
2963 * This function chains into the hardware specific files for them to setup
2964 * any hardware specific handlers as well.
2965 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002966static void iwl3945_setup_rx_handlers(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002967{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002968 priv->rx_handlers[REPLY_ALIVE] = iwl3945_rx_reply_alive;
2969 priv->rx_handlers[REPLY_ADD_STA] = iwl3945_rx_reply_add_sta;
2970 priv->rx_handlers[REPLY_ERROR] = iwl3945_rx_reply_error;
2971 priv->rx_handlers[CHANNEL_SWITCH_NOTIFICATION] = iwl3945_rx_csa;
Zhu Yib481de92007-09-25 17:54:57 -07002972 priv->rx_handlers[SPECTRUM_MEASURE_NOTIFICATION] =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002973 iwl3945_rx_spectrum_measure_notif;
2974 priv->rx_handlers[PM_SLEEP_NOTIFICATION] = iwl3945_rx_pm_sleep_notif;
Zhu Yib481de92007-09-25 17:54:57 -07002975 priv->rx_handlers[PM_DEBUG_STATISTIC_NOTIFIC] =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002976 iwl3945_rx_pm_debug_statistics_notif;
2977 priv->rx_handlers[BEACON_NOTIFICATION] = iwl3945_rx_beacon_notif;
Zhu Yib481de92007-09-25 17:54:57 -07002978
Ben Cahill9fbab512007-11-29 11:09:47 +08002979 /*
2980 * The same handler is used for both the REPLY to a discrete
2981 * statistics request from the host as well as for the periodic
2982 * statistics notifications (after received beacons) from the uCode.
Zhu Yib481de92007-09-25 17:54:57 -07002983 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002984 priv->rx_handlers[REPLY_STATISTICS_CMD] = iwl3945_hw_rx_statistics;
2985 priv->rx_handlers[STATISTICS_NOTIFICATION] = iwl3945_hw_rx_statistics;
Zhu Yib481de92007-09-25 17:54:57 -07002986
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002987 priv->rx_handlers[REPLY_SCAN_CMD] = iwl3945_rx_reply_scan;
2988 priv->rx_handlers[SCAN_START_NOTIFICATION] = iwl3945_rx_scan_start_notif;
Zhu Yib481de92007-09-25 17:54:57 -07002989 priv->rx_handlers[SCAN_RESULTS_NOTIFICATION] =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002990 iwl3945_rx_scan_results_notif;
Zhu Yib481de92007-09-25 17:54:57 -07002991 priv->rx_handlers[SCAN_COMPLETE_NOTIFICATION] =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002992 iwl3945_rx_scan_complete_notif;
2993 priv->rx_handlers[CARD_STATE_NOTIFICATION] = iwl3945_rx_card_state_notif;
Zhu Yib481de92007-09-25 17:54:57 -07002994
Ben Cahill9fbab512007-11-29 11:09:47 +08002995 /* Set up hardware specific Rx handlers */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002996 iwl3945_hw_rx_handler_setup(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002997}
2998
2999/**
Tomas Winkler91c066f2008-03-06 17:36:55 -08003000 * iwl3945_cmd_queue_reclaim - Reclaim CMD queue entries
3001 * When FW advances 'R' index, all entries between old and new 'R' index
3002 * need to be reclaimed.
3003 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003004static void iwl3945_cmd_queue_reclaim(struct iwl_priv *priv,
Tomas Winkler91c066f2008-03-06 17:36:55 -08003005 int txq_id, int index)
3006{
Samuel Ortiz188cf6c2008-12-22 11:31:16 +08003007 struct iwl_tx_queue *txq = &priv->txq[txq_id];
Samuel Ortizd20b3c62008-12-19 10:37:15 +08003008 struct iwl_queue *q = &txq->q;
Tomas Winkler91c066f2008-03-06 17:36:55 -08003009 int nfreed = 0;
3010
Winkler, Tomas625a3812009-01-08 10:19:55 -08003011 if ((index >= q->n_bd) || (iwl_queue_used(q, index) == 0)) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08003012 IWL_ERR(priv, "Read index for DMA queue txq id (%d), index %d, "
Tomas Winkler91c066f2008-03-06 17:36:55 -08003013 "is out of range [0-%d] %d %d.\n", txq_id,
3014 index, q->n_bd, q->write_ptr, q->read_ptr);
3015 return;
3016 }
3017
3018 for (index = iwl_queue_inc_wrap(index, q->n_bd); q->read_ptr != index;
3019 q->read_ptr = iwl_queue_inc_wrap(q->read_ptr, q->n_bd)) {
3020 if (nfreed > 1) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08003021 IWL_ERR(priv, "HCMD skipped: index (%d) %d %d\n", index,
Tomas Winkler91c066f2008-03-06 17:36:55 -08003022 q->write_ptr, q->read_ptr);
3023 queue_work(priv->workqueue, &priv->restart);
3024 break;
3025 }
3026 nfreed++;
3027 }
3028}
3029
3030
3031/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003032 * iwl3945_tx_cmd_complete - Pull unused buffers off the queue and reclaim them
Zhu Yib481de92007-09-25 17:54:57 -07003033 * @rxb: Rx buffer to reclaim
3034 *
3035 * If an Rx buffer has an async callback associated with it the callback
3036 * will be executed. The attached skb (if present) will only be freed
3037 * if the callback returns 1
3038 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003039static void iwl3945_tx_cmd_complete(struct iwl_priv *priv,
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08003040 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003041{
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08003042 struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07003043 u16 sequence = le16_to_cpu(pkt->hdr.sequence);
3044 int txq_id = SEQ_TO_QUEUE(sequence);
3045 int index = SEQ_TO_INDEX(sequence);
Tomas Winkler600c0e12008-12-19 10:37:04 +08003046 int huge = !!(pkt->hdr.sequence & SEQ_HUGE_FRAME);
Zhu Yib481de92007-09-25 17:54:57 -07003047 int cmd_index;
Winkler, Tomasc2d79b42008-12-19 10:37:34 +08003048 struct iwl_cmd *cmd;
Zhu Yib481de92007-09-25 17:54:57 -07003049
Chatre, Reinette638d0eb2009-01-19 15:30:24 -08003050 if (WARN(txq_id != IWL_CMD_QUEUE_NUM,
3051 "wrong command queue %d, sequence 0x%X readp=%d writep=%d\n",
3052 txq_id, sequence,
3053 priv->txq[IWL_CMD_QUEUE_NUM].q.read_ptr,
3054 priv->txq[IWL_CMD_QUEUE_NUM].q.write_ptr)) {
3055 iwl_print_hex_dump(priv, IWL_DL_INFO , rxb, 32);
3056 return;
3057 }
Zhu Yib481de92007-09-25 17:54:57 -07003058
Samuel Ortiz188cf6c2008-12-22 11:31:16 +08003059 cmd_index = get_cmd_index(&priv->txq[IWL_CMD_QUEUE_NUM].q, index, huge);
3060 cmd = priv->txq[IWL_CMD_QUEUE_NUM].cmd[cmd_index];
Zhu Yib481de92007-09-25 17:54:57 -07003061
3062 /* Input error checking is done when commands are added to queue. */
3063 if (cmd->meta.flags & CMD_WANT_SKB) {
3064 cmd->meta.source->u.skb = rxb->skb;
3065 rxb->skb = NULL;
3066 } else if (cmd->meta.u.callback &&
3067 !cmd->meta.u.callback(priv, cmd, rxb->skb))
3068 rxb->skb = NULL;
3069
Tomas Winkler91c066f2008-03-06 17:36:55 -08003070 iwl3945_cmd_queue_reclaim(priv, txq_id, index);
Zhu Yib481de92007-09-25 17:54:57 -07003071
3072 if (!(cmd->meta.flags & CMD_ASYNC)) {
3073 clear_bit(STATUS_HCMD_ACTIVE, &priv->status);
3074 wake_up_interruptible(&priv->wait_command_queue);
3075 }
3076}
3077
3078/************************** RX-FUNCTIONS ****************************/
3079/*
3080 * Rx theory of operation
3081 *
3082 * The host allocates 32 DMA target addresses and passes the host address
3083 * to the firmware at register IWL_RFDS_TABLE_LOWER + N * RFD_SIZE where N is
3084 * 0 to 31
3085 *
3086 * Rx Queue Indexes
3087 * The host/firmware share two index registers for managing the Rx buffers.
3088 *
3089 * The READ index maps to the first position that the firmware may be writing
3090 * to -- the driver can read up to (but not including) this position and get
3091 * good data.
3092 * The READ index is managed by the firmware once the card is enabled.
3093 *
3094 * The WRITE index maps to the last position the driver has read from -- the
3095 * position preceding WRITE is the last slot the firmware can place a packet.
3096 *
3097 * The queue is empty (no good data) if WRITE = READ - 1, and is full if
3098 * WRITE = READ.
3099 *
Ben Cahill9fbab512007-11-29 11:09:47 +08003100 * During initialization, the host sets up the READ queue position to the first
Zhu Yib481de92007-09-25 17:54:57 -07003101 * INDEX position, and WRITE to the last (READ - 1 wrapped)
3102 *
Ben Cahill9fbab512007-11-29 11:09:47 +08003103 * When the firmware places a packet in a buffer, it will advance the READ index
Zhu Yib481de92007-09-25 17:54:57 -07003104 * and fire the RX interrupt. The driver can then query the READ index and
3105 * process as many packets as possible, moving the WRITE index forward as it
3106 * resets the Rx queue buffers with new memory.
3107 *
3108 * The management in the driver is as follows:
3109 * + A list of pre-allocated SKBs is stored in iwl->rxq->rx_free. When
3110 * iwl->rxq->free_count drops to or below RX_LOW_WATERMARK, work is scheduled
Ian Schram01ebd062007-10-25 17:15:22 +08003111 * to replenish the iwl->rxq->rx_free.
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003112 * + In iwl3945_rx_replenish (scheduled) if 'processed' != 'read' then the
Zhu Yib481de92007-09-25 17:54:57 -07003113 * iwl->rxq is replenished and the READ INDEX is updated (updating the
3114 * 'processed' and 'read' driver indexes as well)
3115 * + A received packet is processed and handed to the kernel network stack,
3116 * detached from the iwl->rxq. The driver 'processed' index is updated.
3117 * + The Host/Firmware iwl->rxq is replenished at tasklet time from the rx_free
3118 * list. If there are no allocated buffers in iwl->rxq->rx_free, the READ
3119 * INDEX is not incremented and iwl->status(RX_STALLED) is set. If there
3120 * were enough free buffers and RX_STALLED is set it is cleared.
3121 *
3122 *
3123 * Driver sequence:
3124 *
Ben Cahill9fbab512007-11-29 11:09:47 +08003125 * iwl3945_rx_replenish() Replenishes rx_free list from rx_used, and calls
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003126 * iwl3945_rx_queue_restock
Ben Cahill9fbab512007-11-29 11:09:47 +08003127 * iwl3945_rx_queue_restock() Moves available buffers from rx_free into Rx
Zhu Yib481de92007-09-25 17:54:57 -07003128 * queue, updates firmware pointers, and updates
3129 * the WRITE index. If insufficient rx_free buffers
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003130 * are available, schedules iwl3945_rx_replenish
Zhu Yib481de92007-09-25 17:54:57 -07003131 *
3132 * -- enable interrupts --
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08003133 * ISR - iwl3945_rx() Detach iwl_rx_mem_buffers from pool up to the
Zhu Yib481de92007-09-25 17:54:57 -07003134 * READ INDEX, detaching the SKB from the pool.
3135 * Moves the packet buffer from queue to rx_used.
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003136 * Calls iwl3945_rx_queue_restock to refill any empty
Zhu Yib481de92007-09-25 17:54:57 -07003137 * slots.
3138 * ...
3139 *
3140 */
3141
3142/**
Ben Cahill9fbab512007-11-29 11:09:47 +08003143 * iwl3945_dma_addr2rbd_ptr - convert a DMA address to a uCode read buffer ptr
Zhu Yib481de92007-09-25 17:54:57 -07003144 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003145static inline __le32 iwl3945_dma_addr2rbd_ptr(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07003146 dma_addr_t dma_addr)
3147{
3148 return cpu_to_le32((u32)dma_addr);
3149}
3150
3151/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003152 * iwl3945_rx_queue_restock - refill RX queue from pre-allocated pool
Zhu Yib481de92007-09-25 17:54:57 -07003153 *
Ben Cahill9fbab512007-11-29 11:09:47 +08003154 * If there are slots in the RX queue that need to be restocked,
Zhu Yib481de92007-09-25 17:54:57 -07003155 * and we have free pre-allocated buffers, fill the ranks as much
Ben Cahill9fbab512007-11-29 11:09:47 +08003156 * as we can, pulling from rx_free.
Zhu Yib481de92007-09-25 17:54:57 -07003157 *
3158 * This moves the 'write' index forward to catch up with 'processed', and
3159 * also updates the memory address in the firmware to reference the new
3160 * target buffer.
3161 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003162static int iwl3945_rx_queue_restock(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003163{
Abhijeet Kolekarcc2f3622008-12-19 10:37:25 +08003164 struct iwl_rx_queue *rxq = &priv->rxq;
Zhu Yib481de92007-09-25 17:54:57 -07003165 struct list_head *element;
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08003166 struct iwl_rx_mem_buffer *rxb;
Zhu Yib481de92007-09-25 17:54:57 -07003167 unsigned long flags;
3168 int write, rc;
3169
3170 spin_lock_irqsave(&rxq->lock, flags);
3171 write = rxq->write & ~0x7;
Winkler, Tomas37d68312009-01-08 10:19:54 -08003172 while ((iwl_rx_queue_space(rxq) > 0) && (rxq->free_count)) {
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003173 /* Get next free Rx buffer, remove from free list */
Zhu Yib481de92007-09-25 17:54:57 -07003174 element = rxq->rx_free.next;
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08003175 rxb = list_entry(element, struct iwl_rx_mem_buffer, list);
Zhu Yib481de92007-09-25 17:54:57 -07003176 list_del(element);
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003177
3178 /* Point to Rx buffer via next RBD in circular buffer */
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08003179 rxq->bd[rxq->write] = iwl3945_dma_addr2rbd_ptr(priv, rxb->real_dma_addr);
Zhu Yib481de92007-09-25 17:54:57 -07003180 rxq->queue[rxq->write] = rxb;
3181 rxq->write = (rxq->write + 1) & RX_QUEUE_MASK;
3182 rxq->free_count--;
3183 }
3184 spin_unlock_irqrestore(&rxq->lock, flags);
3185 /* If the pre-allocated buffer pool is dropping low, schedule to
3186 * refill it */
3187 if (rxq->free_count <= RX_LOW_WATERMARK)
3188 queue_work(priv->workqueue, &priv->rx_replenish);
3189
3190
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003191 /* If we've added more space for the firmware to place data, tell it.
3192 * Increment device's write pointer in multiples of 8. */
Zhu Yib481de92007-09-25 17:54:57 -07003193 if ((write != (rxq->write & ~0x7))
3194 || (abs(rxq->write - rxq->read) > 7)) {
3195 spin_lock_irqsave(&rxq->lock, flags);
3196 rxq->need_update = 1;
3197 spin_unlock_irqrestore(&rxq->lock, flags);
Winkler, Tomas141c43a2009-01-08 10:19:53 -08003198 rc = iwl_rx_queue_update_write_ptr(priv, rxq);
Zhu Yib481de92007-09-25 17:54:57 -07003199 if (rc)
3200 return rc;
3201 }
3202
3203 return 0;
3204}
3205
3206/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003207 * iwl3945_rx_replenish - Move all used packet from rx_used to rx_free
Zhu Yib481de92007-09-25 17:54:57 -07003208 *
3209 * When moving to rx_free an SKB is allocated for the slot.
3210 *
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003211 * Also restock the Rx queue via iwl3945_rx_queue_restock.
Ian Schram01ebd062007-10-25 17:15:22 +08003212 * This is called as a scheduled work item (except for during initialization)
Zhu Yib481de92007-09-25 17:54:57 -07003213 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003214static void iwl3945_rx_allocate(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003215{
Abhijeet Kolekarcc2f3622008-12-19 10:37:25 +08003216 struct iwl_rx_queue *rxq = &priv->rxq;
Zhu Yib481de92007-09-25 17:54:57 -07003217 struct list_head *element;
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08003218 struct iwl_rx_mem_buffer *rxb;
Zhu Yib481de92007-09-25 17:54:57 -07003219 unsigned long flags;
3220 spin_lock_irqsave(&rxq->lock, flags);
3221 while (!list_empty(&rxq->rx_used)) {
3222 element = rxq->rx_used.next;
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08003223 rxb = list_entry(element, struct iwl_rx_mem_buffer, list);
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003224
3225 /* Alloc a new receive buffer */
Zhu Yib481de92007-09-25 17:54:57 -07003226 rxb->skb =
Winkler, Tomas1e33dc62009-01-08 10:19:57 -08003227 alloc_skb(priv->hw_params.rx_buf_size,
3228 __GFP_NOWARN | GFP_ATOMIC);
Zhu Yib481de92007-09-25 17:54:57 -07003229 if (!rxb->skb) {
3230 if (net_ratelimit())
Tomas Winkler978785a2008-12-19 10:37:31 +08003231 IWL_CRIT(priv, ": Can not allocate SKB buffers\n");
Zhu Yib481de92007-09-25 17:54:57 -07003232 /* We don't reschedule replenish work here -- we will
3233 * call the restock method and if it still needs
3234 * more buffers it will schedule replenish */
3235 break;
3236 }
Zhu Yi12342c42007-12-20 11:27:32 +08003237
3238 /* If radiotap head is required, reserve some headroom here.
3239 * The physical head count is a variable rx_stats->phy_count.
3240 * We reserve 4 bytes here. Plus these extra bytes, the
3241 * headroom of the physical head should be enough for the
3242 * radiotap head that iwl3945 supported. See iwl3945_rt.
3243 */
3244 skb_reserve(rxb->skb, 4);
3245
Zhu Yib481de92007-09-25 17:54:57 -07003246 priv->alloc_rxb_skb++;
3247 list_del(element);
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003248
3249 /* Get physical address of RB/SKB */
Winkler, Tomas1e33dc62009-01-08 10:19:57 -08003250 rxb->real_dma_addr = pci_map_single(priv->pci_dev,
3251 rxb->skb->data,
3252 priv->hw_params.rx_buf_size,
3253 PCI_DMA_FROMDEVICE);
Zhu Yib481de92007-09-25 17:54:57 -07003254 list_add_tail(&rxb->list, &rxq->rx_free);
3255 rxq->free_count++;
3256 }
3257 spin_unlock_irqrestore(&rxq->lock, flags);
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08003258}
3259
3260/*
3261 * this should be called while priv->lock is locked
3262 */
Tomas Winkler4fd1f842007-12-05 20:59:58 +02003263static void __iwl3945_rx_replenish(void *data)
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08003264{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003265 struct iwl_priv *priv = data;
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08003266
3267 iwl3945_rx_allocate(priv);
3268 iwl3945_rx_queue_restock(priv);
3269}
3270
3271
3272void iwl3945_rx_replenish(void *data)
3273{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003274 struct iwl_priv *priv = data;
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08003275 unsigned long flags;
3276
3277 iwl3945_rx_allocate(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003278
3279 spin_lock_irqsave(&priv->lock, flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003280 iwl3945_rx_queue_restock(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003281 spin_unlock_irqrestore(&priv->lock, flags);
3282}
3283
Zhu Yib481de92007-09-25 17:54:57 -07003284/* Convert linear signal-to-noise ratio into dB */
3285static u8 ratio2dB[100] = {
3286/* 0 1 2 3 4 5 6 7 8 9 */
3287 0, 0, 6, 10, 12, 14, 16, 17, 18, 19, /* 00 - 09 */
3288 20, 21, 22, 22, 23, 23, 24, 25, 26, 26, /* 10 - 19 */
3289 26, 26, 26, 27, 27, 28, 28, 28, 29, 29, /* 20 - 29 */
3290 29, 30, 30, 30, 31, 31, 31, 31, 32, 32, /* 30 - 39 */
3291 32, 32, 32, 33, 33, 33, 33, 33, 34, 34, /* 40 - 49 */
3292 34, 34, 34, 34, 35, 35, 35, 35, 35, 35, /* 50 - 59 */
3293 36, 36, 36, 36, 36, 36, 36, 37, 37, 37, /* 60 - 69 */
3294 37, 37, 37, 37, 37, 38, 38, 38, 38, 38, /* 70 - 79 */
3295 38, 38, 38, 38, 38, 39, 39, 39, 39, 39, /* 80 - 89 */
3296 39, 39, 39, 39, 39, 40, 40, 40, 40, 40 /* 90 - 99 */
3297};
3298
3299/* Calculates a relative dB value from a ratio of linear
3300 * (i.e. not dB) signal levels.
3301 * Conversion assumes that levels are voltages (20*log), not powers (10*log). */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003302int iwl3945_calc_db_from_ratio(int sig_ratio)
Zhu Yib481de92007-09-25 17:54:57 -07003303{
Adrian Bunk221c80c2008-02-02 23:19:01 +02003304 /* 1000:1 or higher just report as 60 dB */
3305 if (sig_ratio >= 1000)
Zhu Yib481de92007-09-25 17:54:57 -07003306 return 60;
3307
Adrian Bunk221c80c2008-02-02 23:19:01 +02003308 /* 100:1 or higher, divide by 10 and use table,
Zhu Yib481de92007-09-25 17:54:57 -07003309 * add 20 dB to make up for divide by 10 */
Adrian Bunk221c80c2008-02-02 23:19:01 +02003310 if (sig_ratio >= 100)
Tomas Winkler3ac7f142008-07-21 02:40:14 +03003311 return 20 + (int)ratio2dB[sig_ratio/10];
Zhu Yib481de92007-09-25 17:54:57 -07003312
3313 /* We shouldn't see this */
3314 if (sig_ratio < 1)
3315 return 0;
3316
3317 /* Use table for ratios 1:1 - 99:1 */
3318 return (int)ratio2dB[sig_ratio];
3319}
3320
3321#define PERFECT_RSSI (-20) /* dBm */
3322#define WORST_RSSI (-95) /* dBm */
3323#define RSSI_RANGE (PERFECT_RSSI - WORST_RSSI)
3324
3325/* Calculate an indication of rx signal quality (a percentage, not dBm!).
3326 * See http://www.ces.clemson.edu/linux/signal_quality.shtml for info
3327 * about formulas used below. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003328int iwl3945_calc_sig_qual(int rssi_dbm, int noise_dbm)
Zhu Yib481de92007-09-25 17:54:57 -07003329{
3330 int sig_qual;
3331 int degradation = PERFECT_RSSI - rssi_dbm;
3332
3333 /* If we get a noise measurement, use signal-to-noise ratio (SNR)
3334 * as indicator; formula is (signal dbm - noise dbm).
3335 * SNR at or above 40 is a great signal (100%).
3336 * Below that, scale to fit SNR of 0 - 40 dB within 0 - 100% indicator.
3337 * Weakest usable signal is usually 10 - 15 dB SNR. */
3338 if (noise_dbm) {
3339 if (rssi_dbm - noise_dbm >= 40)
3340 return 100;
3341 else if (rssi_dbm < noise_dbm)
3342 return 0;
3343 sig_qual = ((rssi_dbm - noise_dbm) * 5) / 2;
3344
3345 /* Else use just the signal level.
3346 * This formula is a least squares fit of data points collected and
3347 * compared with a reference system that had a percentage (%) display
3348 * for signal quality. */
3349 } else
3350 sig_qual = (100 * (RSSI_RANGE * RSSI_RANGE) - degradation *
3351 (15 * RSSI_RANGE + 62 * degradation)) /
3352 (RSSI_RANGE * RSSI_RANGE);
3353
3354 if (sig_qual > 100)
3355 sig_qual = 100;
3356 else if (sig_qual < 1)
3357 sig_qual = 0;
3358
3359 return sig_qual;
3360}
3361
3362/**
Ben Cahill9fbab512007-11-29 11:09:47 +08003363 * iwl3945_rx_handle - Main entry function for receiving responses from uCode
Zhu Yib481de92007-09-25 17:54:57 -07003364 *
3365 * Uses the priv->rx_handlers callback function array to invoke
3366 * the appropriate handlers, including command responses,
3367 * frame-received notifications, and other notifications.
3368 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003369static void iwl3945_rx_handle(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003370{
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08003371 struct iwl_rx_mem_buffer *rxb;
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08003372 struct iwl_rx_packet *pkt;
Abhijeet Kolekarcc2f3622008-12-19 10:37:25 +08003373 struct iwl_rx_queue *rxq = &priv->rxq;
Zhu Yib481de92007-09-25 17:54:57 -07003374 u32 r, i;
3375 int reclaim;
3376 unsigned long flags;
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08003377 u8 fill_rx = 0;
Mohamed Abbasd68ab682008-02-07 13:16:33 -08003378 u32 count = 8;
Zhu Yib481de92007-09-25 17:54:57 -07003379
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003380 /* uCode's read index (stored in shared DRAM) indicates the last Rx
3381 * buffer that the driver may process (last buffer filled by ucode). */
Winkler, Tomas8cd812b2008-12-19 10:37:43 +08003382 r = le16_to_cpu(rxq->rb_stts->closed_rb_num) & 0x0FFF;
Zhu Yib481de92007-09-25 17:54:57 -07003383 i = rxq->read;
3384
Winkler, Tomas37d68312009-01-08 10:19:54 -08003385 if (iwl_rx_queue_space(rxq) > (RX_QUEUE_SIZE / 2))
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08003386 fill_rx = 1;
Zhu Yib481de92007-09-25 17:54:57 -07003387 /* Rx interrupt, but nothing sent from uCode */
3388 if (i == r)
3389 IWL_DEBUG(IWL_DL_RX | IWL_DL_ISR, "r = %d, i = %d\n", r, i);
3390
3391 while (i != r) {
3392 rxb = rxq->queue[i];
3393
Ben Cahill9fbab512007-11-29 11:09:47 +08003394 /* If an RXB doesn't have a Rx queue slot associated with it,
Zhu Yib481de92007-09-25 17:54:57 -07003395 * then a bug has been introduced in the queue refilling
3396 * routines -- catch it here */
3397 BUG_ON(rxb == NULL);
3398
3399 rxq->queue[i] = NULL;
3400
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08003401 pci_dma_sync_single_for_cpu(priv->pci_dev, rxb->real_dma_addr,
Winkler, Tomas1e33dc62009-01-08 10:19:57 -08003402 priv->hw_params.rx_buf_size,
Zhu Yib481de92007-09-25 17:54:57 -07003403 PCI_DMA_FROMDEVICE);
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08003404 pkt = (struct iwl_rx_packet *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07003405
3406 /* Reclaim a command buffer only if this packet is a response
3407 * to a (driver-originated) command.
3408 * If the packet (e.g. Rx frame) originated from uCode,
3409 * there is no command buffer to reclaim.
3410 * Ucode should set SEQ_RX_FRAME bit if ucode-originated,
3411 * but apparently a few don't get set; catch them here. */
3412 reclaim = !(pkt->hdr.sequence & SEQ_RX_FRAME) &&
3413 (pkt->hdr.cmd != STATISTICS_NOTIFICATION) &&
3414 (pkt->hdr.cmd != REPLY_TX);
3415
3416 /* Based on type of command response or notification,
3417 * handle those that need handling via function in
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003418 * rx_handlers table. See iwl3945_setup_rx_handlers() */
Zhu Yib481de92007-09-25 17:54:57 -07003419 if (priv->rx_handlers[pkt->hdr.cmd]) {
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08003420 IWL_DEBUG(IWL_DL_HCMD | IWL_DL_RX | IWL_DL_ISR,
Zhu Yib481de92007-09-25 17:54:57 -07003421 "r = %d, i = %d, %s, 0x%02x\n", r, i,
3422 get_cmd_string(pkt->hdr.cmd), pkt->hdr.cmd);
3423 priv->rx_handlers[pkt->hdr.cmd] (priv, rxb);
3424 } else {
3425 /* No handling needed */
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08003426 IWL_DEBUG(IWL_DL_HCMD | IWL_DL_RX | IWL_DL_ISR,
Zhu Yib481de92007-09-25 17:54:57 -07003427 "r %d i %d No handler needed for %s, 0x%02x\n",
3428 r, i, get_cmd_string(pkt->hdr.cmd),
3429 pkt->hdr.cmd);
3430 }
3431
3432 if (reclaim) {
Ben Cahill9fbab512007-11-29 11:09:47 +08003433 /* Invoke any callbacks, transfer the skb to caller, and
3434 * fire off the (possibly) blocking iwl3945_send_cmd()
Zhu Yib481de92007-09-25 17:54:57 -07003435 * as we reclaim the driver command queue */
3436 if (rxb && rxb->skb)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003437 iwl3945_tx_cmd_complete(priv, rxb);
Zhu Yib481de92007-09-25 17:54:57 -07003438 else
Winkler, Tomas39aadf82008-12-19 10:37:32 +08003439 IWL_WARN(priv, "Claim null rxb?\n");
Zhu Yib481de92007-09-25 17:54:57 -07003440 }
3441
3442 /* For now we just don't re-use anything. We can tweak this
3443 * later to try and re-use notification packets and SKBs that
3444 * fail to Rx correctly */
3445 if (rxb->skb != NULL) {
3446 priv->alloc_rxb_skb--;
3447 dev_kfree_skb_any(rxb->skb);
3448 rxb->skb = NULL;
3449 }
3450
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08003451 pci_unmap_single(priv->pci_dev, rxb->real_dma_addr,
Winkler, Tomas1e33dc62009-01-08 10:19:57 -08003452 priv->hw_params.rx_buf_size,
3453 PCI_DMA_FROMDEVICE);
Zhu Yib481de92007-09-25 17:54:57 -07003454 spin_lock_irqsave(&rxq->lock, flags);
3455 list_add_tail(&rxb->list, &priv->rxq.rx_used);
3456 spin_unlock_irqrestore(&rxq->lock, flags);
3457 i = (i + 1) & RX_QUEUE_MASK;
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08003458 /* If there are a lot of unused frames,
3459 * restock the Rx queue so ucode won't assert. */
3460 if (fill_rx) {
3461 count++;
3462 if (count >= 8) {
3463 priv->rxq.read = i;
3464 __iwl3945_rx_replenish(priv);
3465 count = 0;
3466 }
3467 }
Zhu Yib481de92007-09-25 17:54:57 -07003468 }
3469
3470 /* Backtrack one entry */
3471 priv->rxq.read = i;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003472 iwl3945_rx_queue_restock(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003473}
3474
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08003475#ifdef CONFIG_IWL3945_DEBUG
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003476static void iwl3945_print_rx_config_cmd(struct iwl_priv *priv,
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08003477 struct iwl3945_rxon_cmd *rxon)
Zhu Yib481de92007-09-25 17:54:57 -07003478{
3479 IWL_DEBUG_RADIO("RX CONFIG:\n");
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08003480 iwl_print_hex_dump(priv, IWL_DL_RADIO, (u8 *) rxon, sizeof(*rxon));
Zhu Yib481de92007-09-25 17:54:57 -07003481 IWL_DEBUG_RADIO("u16 channel: 0x%x\n", le16_to_cpu(rxon->channel));
3482 IWL_DEBUG_RADIO("u32 flags: 0x%08X\n", le32_to_cpu(rxon->flags));
3483 IWL_DEBUG_RADIO("u32 filter_flags: 0x%08x\n",
3484 le32_to_cpu(rxon->filter_flags));
3485 IWL_DEBUG_RADIO("u8 dev_type: 0x%x\n", rxon->dev_type);
3486 IWL_DEBUG_RADIO("u8 ofdm_basic_rates: 0x%02x\n",
3487 rxon->ofdm_basic_rates);
3488 IWL_DEBUG_RADIO("u8 cck_basic_rates: 0x%02x\n", rxon->cck_basic_rates);
Johannes Berge1749612008-10-27 15:59:26 -07003489 IWL_DEBUG_RADIO("u8[6] node_addr: %pM\n", rxon->node_addr);
3490 IWL_DEBUG_RADIO("u8[6] bssid_addr: %pM\n", rxon->bssid_addr);
Zhu Yib481de92007-09-25 17:54:57 -07003491 IWL_DEBUG_RADIO("u16 assoc_id: 0x%x\n", le16_to_cpu(rxon->assoc_id));
3492}
3493#endif
3494
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003495static void iwl3945_enable_interrupts(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003496{
3497 IWL_DEBUG_ISR("Enabling interrupts\n");
3498 set_bit(STATUS_INT_ENABLED, &priv->status);
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003499 iwl_write32(priv, CSR_INT_MASK, CSR_INI_SET_MASK);
Zhu Yib481de92007-09-25 17:54:57 -07003500}
3501
Mohamed Abbas0359fac2008-03-28 16:21:08 -07003502
3503/* call this function to flush any scheduled tasklet */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003504static inline void iwl_synchronize_irq(struct iwl_priv *priv)
Mohamed Abbas0359fac2008-03-28 16:21:08 -07003505{
Tomas Winklera96a27f2008-10-23 23:48:56 -07003506 /* wait to make sure we flush pending tasklet*/
Mohamed Abbas0359fac2008-03-28 16:21:08 -07003507 synchronize_irq(priv->pci_dev->irq);
3508 tasklet_kill(&priv->irq_tasklet);
3509}
3510
3511
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003512static inline void iwl3945_disable_interrupts(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003513{
3514 clear_bit(STATUS_INT_ENABLED, &priv->status);
3515
3516 /* disable interrupts from uCode/NIC to host */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003517 iwl_write32(priv, CSR_INT_MASK, 0x00000000);
Zhu Yib481de92007-09-25 17:54:57 -07003518
3519 /* acknowledge/clear/reset any interrupts still pending
3520 * from uCode or flow handler (Rx/Tx DMA) */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003521 iwl_write32(priv, CSR_INT, 0xffffffff);
3522 iwl_write32(priv, CSR_FH_INT_STATUS, 0xffffffff);
Zhu Yib481de92007-09-25 17:54:57 -07003523 IWL_DEBUG_ISR("Disabled interrupts\n");
3524}
3525
3526static const char *desc_lookup(int i)
3527{
3528 switch (i) {
3529 case 1:
3530 return "FAIL";
3531 case 2:
3532 return "BAD_PARAM";
3533 case 3:
3534 return "BAD_CHECKSUM";
3535 case 4:
3536 return "NMI_INTERRUPT";
3537 case 5:
3538 return "SYSASSERT";
3539 case 6:
3540 return "FATAL_ERROR";
3541 }
3542
3543 return "UNKNOWN";
3544}
3545
3546#define ERROR_START_OFFSET (1 * sizeof(u32))
3547#define ERROR_ELEM_SIZE (7 * sizeof(u32))
3548
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003549static void iwl3945_dump_nic_error_log(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003550{
3551 u32 i;
3552 u32 desc, time, count, base, data1;
3553 u32 blink1, blink2, ilink1, ilink2;
3554 int rc;
3555
3556 base = le32_to_cpu(priv->card_alive.error_event_table_ptr);
3557
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003558 if (!iwl3945_hw_valid_rtc_data_addr(base)) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08003559 IWL_ERR(priv, "Not valid error log pointer 0x%08X\n", base);
Zhu Yib481de92007-09-25 17:54:57 -07003560 return;
3561 }
3562
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003563 rc = iwl_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003564 if (rc) {
Winkler, Tomas39aadf82008-12-19 10:37:32 +08003565 IWL_WARN(priv, "Can not read from adapter at this time.\n");
Zhu Yib481de92007-09-25 17:54:57 -07003566 return;
3567 }
3568
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003569 count = iwl_read_targ_mem(priv, base);
Zhu Yib481de92007-09-25 17:54:57 -07003570
3571 if (ERROR_START_OFFSET <= count * ERROR_ELEM_SIZE) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08003572 IWL_ERR(priv, "Start IWL Error Log Dump:\n");
3573 IWL_ERR(priv, "Status: 0x%08lX, count: %d\n",
3574 priv->status, count);
Zhu Yib481de92007-09-25 17:54:57 -07003575 }
3576
Winkler, Tomas15b16872008-12-19 10:37:33 +08003577 IWL_ERR(priv, "Desc Time asrtPC blink2 "
Zhu Yib481de92007-09-25 17:54:57 -07003578 "ilink1 nmiPC Line\n");
3579 for (i = ERROR_START_OFFSET;
3580 i < (count * ERROR_ELEM_SIZE) + ERROR_START_OFFSET;
3581 i += ERROR_ELEM_SIZE) {
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003582 desc = iwl_read_targ_mem(priv, base + i);
Zhu Yib481de92007-09-25 17:54:57 -07003583 time =
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003584 iwl_read_targ_mem(priv, base + i + 1 * sizeof(u32));
Zhu Yib481de92007-09-25 17:54:57 -07003585 blink1 =
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003586 iwl_read_targ_mem(priv, base + i + 2 * sizeof(u32));
Zhu Yib481de92007-09-25 17:54:57 -07003587 blink2 =
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003588 iwl_read_targ_mem(priv, base + i + 3 * sizeof(u32));
Zhu Yib481de92007-09-25 17:54:57 -07003589 ilink1 =
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003590 iwl_read_targ_mem(priv, base + i + 4 * sizeof(u32));
Zhu Yib481de92007-09-25 17:54:57 -07003591 ilink2 =
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003592 iwl_read_targ_mem(priv, base + i + 5 * sizeof(u32));
Zhu Yib481de92007-09-25 17:54:57 -07003593 data1 =
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003594 iwl_read_targ_mem(priv, base + i + 6 * sizeof(u32));
Zhu Yib481de92007-09-25 17:54:57 -07003595
Winkler, Tomas15b16872008-12-19 10:37:33 +08003596 IWL_ERR(priv,
3597 "%-13s (#%d) %010u 0x%05X 0x%05X 0x%05X 0x%05X %u\n\n",
3598 desc_lookup(desc), desc, time, blink1, blink2,
3599 ilink1, ilink2, data1);
Zhu Yib481de92007-09-25 17:54:57 -07003600 }
3601
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003602 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003603
3604}
3605
Ben Cahillf58177b2007-11-29 11:09:43 +08003606#define EVENT_START_OFFSET (6 * sizeof(u32))
Zhu Yib481de92007-09-25 17:54:57 -07003607
3608/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003609 * iwl3945_print_event_log - Dump error event log to syslog
Zhu Yib481de92007-09-25 17:54:57 -07003610 *
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003611 * NOTE: Must be called with iwl_grab_nic_access() already obtained!
Zhu Yib481de92007-09-25 17:54:57 -07003612 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003613static void iwl3945_print_event_log(struct iwl_priv *priv, u32 start_idx,
Zhu Yib481de92007-09-25 17:54:57 -07003614 u32 num_events, u32 mode)
3615{
3616 u32 i;
3617 u32 base; /* SRAM byte address of event log header */
3618 u32 event_size; /* 2 u32s, or 3 u32s if timestamp recorded */
3619 u32 ptr; /* SRAM byte address of log data */
3620 u32 ev, time, data; /* event log data */
3621
3622 if (num_events == 0)
3623 return;
3624
3625 base = le32_to_cpu(priv->card_alive.log_event_table_ptr);
3626
3627 if (mode == 0)
3628 event_size = 2 * sizeof(u32);
3629 else
3630 event_size = 3 * sizeof(u32);
3631
3632 ptr = base + EVENT_START_OFFSET + (start_idx * event_size);
3633
3634 /* "time" is actually "data" for mode 0 (no timestamp).
3635 * place event id # at far right for easier visual parsing. */
3636 for (i = 0; i < num_events; i++) {
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003637 ev = iwl_read_targ_mem(priv, ptr);
Zhu Yib481de92007-09-25 17:54:57 -07003638 ptr += sizeof(u32);
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003639 time = iwl_read_targ_mem(priv, ptr);
Zhu Yib481de92007-09-25 17:54:57 -07003640 ptr += sizeof(u32);
Winkler, Tomas15b16872008-12-19 10:37:33 +08003641 if (mode == 0) {
3642 /* data, ev */
3643 IWL_ERR(priv, "0x%08x\t%04u\n", time, ev);
3644 } else {
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003645 data = iwl_read_targ_mem(priv, ptr);
Zhu Yib481de92007-09-25 17:54:57 -07003646 ptr += sizeof(u32);
Winkler, Tomas15b16872008-12-19 10:37:33 +08003647 IWL_ERR(priv, "%010u\t0x%08x\t%04u\n", time, data, ev);
Zhu Yib481de92007-09-25 17:54:57 -07003648 }
3649 }
3650}
3651
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003652static void iwl3945_dump_nic_event_log(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003653{
3654 int rc;
3655 u32 base; /* SRAM byte address of event log header */
3656 u32 capacity; /* event log capacity in # entries */
3657 u32 mode; /* 0 - no timestamp, 1 - timestamp recorded */
3658 u32 num_wraps; /* # times uCode wrapped to top of log */
3659 u32 next_entry; /* index of next entry to be written by uCode */
3660 u32 size; /* # entries that we'll print */
3661
3662 base = le32_to_cpu(priv->card_alive.log_event_table_ptr);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003663 if (!iwl3945_hw_valid_rtc_data_addr(base)) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08003664 IWL_ERR(priv, "Invalid event log pointer 0x%08X\n", base);
Zhu Yib481de92007-09-25 17:54:57 -07003665 return;
3666 }
3667
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003668 rc = iwl_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003669 if (rc) {
Winkler, Tomas39aadf82008-12-19 10:37:32 +08003670 IWL_WARN(priv, "Can not read from adapter at this time.\n");
Zhu Yib481de92007-09-25 17:54:57 -07003671 return;
3672 }
3673
3674 /* event log header */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003675 capacity = iwl_read_targ_mem(priv, base);
3676 mode = iwl_read_targ_mem(priv, base + (1 * sizeof(u32)));
3677 num_wraps = iwl_read_targ_mem(priv, base + (2 * sizeof(u32)));
3678 next_entry = iwl_read_targ_mem(priv, base + (3 * sizeof(u32)));
Zhu Yib481de92007-09-25 17:54:57 -07003679
3680 size = num_wraps ? capacity : next_entry;
3681
3682 /* bail out if nothing in log */
3683 if (size == 0) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08003684 IWL_ERR(priv, "Start IWL Event Log Dump: nothing in log\n");
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003685 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003686 return;
3687 }
3688
Winkler, Tomas15b16872008-12-19 10:37:33 +08003689 IWL_ERR(priv, "Start IWL Event Log Dump: display count %d, wraps %d\n",
Zhu Yib481de92007-09-25 17:54:57 -07003690 size, num_wraps);
3691
3692 /* if uCode has wrapped back to top of log, start at the oldest entry,
3693 * i.e the next one that uCode would fill. */
3694 if (num_wraps)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003695 iwl3945_print_event_log(priv, next_entry,
Zhu Yib481de92007-09-25 17:54:57 -07003696 capacity - next_entry, mode);
3697
3698 /* (then/else) start at top of log */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003699 iwl3945_print_event_log(priv, 0, next_entry, mode);
Zhu Yib481de92007-09-25 17:54:57 -07003700
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003701 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003702}
3703
3704/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003705 * iwl3945_irq_handle_error - called for HW or SW error interrupt from card
Zhu Yib481de92007-09-25 17:54:57 -07003706 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003707static void iwl3945_irq_handle_error(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003708{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003709 /* Set the FW error flag -- cleared on iwl3945_down */
Zhu Yib481de92007-09-25 17:54:57 -07003710 set_bit(STATUS_FW_ERROR, &priv->status);
3711
3712 /* Cancel currently queued command. */
3713 clear_bit(STATUS_HCMD_ACTIVE, &priv->status);
3714
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08003715#ifdef CONFIG_IWL3945_DEBUG
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08003716 if (priv->debug_level & IWL_DL_FW_ERRORS) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003717 iwl3945_dump_nic_error_log(priv);
3718 iwl3945_dump_nic_event_log(priv);
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08003719 iwl3945_print_rx_config_cmd(priv, &priv->staging39_rxon);
Zhu Yib481de92007-09-25 17:54:57 -07003720 }
3721#endif
3722
3723 wake_up_interruptible(&priv->wait_command_queue);
3724
3725 /* Keep the restart process from trying to send host
3726 * commands by clearing the INIT status bit */
3727 clear_bit(STATUS_READY, &priv->status);
3728
3729 if (!test_bit(STATUS_EXIT_PENDING, &priv->status)) {
3730 IWL_DEBUG(IWL_DL_INFO | IWL_DL_FW_ERRORS,
3731 "Restarting adapter due to uCode error.\n");
3732
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003733 if (iwl3945_is_associated(priv)) {
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08003734 memcpy(&priv->recovery39_rxon, &priv->active39_rxon,
3735 sizeof(priv->recovery39_rxon));
Zhu Yib481de92007-09-25 17:54:57 -07003736 priv->error_recovering = 1;
3737 }
3738 queue_work(priv->workqueue, &priv->restart);
3739 }
3740}
3741
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003742static void iwl3945_error_recovery(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003743{
3744 unsigned long flags;
3745
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08003746 memcpy(&priv->staging39_rxon, &priv->recovery39_rxon,
3747 sizeof(priv->staging39_rxon));
3748 priv->staging39_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003749 iwl3945_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003750
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003751 iwl3945_add_station(priv, priv->bssid, 1, 0);
Zhu Yib481de92007-09-25 17:54:57 -07003752
3753 spin_lock_irqsave(&priv->lock, flags);
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08003754 priv->assoc_id = le16_to_cpu(priv->staging39_rxon.assoc_id);
Zhu Yib481de92007-09-25 17:54:57 -07003755 priv->error_recovering = 0;
3756 spin_unlock_irqrestore(&priv->lock, flags);
3757}
3758
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003759static void iwl3945_irq_tasklet(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003760{
3761 u32 inta, handled = 0;
3762 u32 inta_fh;
3763 unsigned long flags;
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08003764#ifdef CONFIG_IWL3945_DEBUG
Zhu Yib481de92007-09-25 17:54:57 -07003765 u32 inta_mask;
3766#endif
3767
3768 spin_lock_irqsave(&priv->lock, flags);
3769
3770 /* Ack/clear/reset pending uCode interrupts.
3771 * Note: Some bits in CSR_INT are "OR" of bits in CSR_FH_INT_STATUS,
3772 * and will clear only when CSR_FH_INT_STATUS gets cleared. */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003773 inta = iwl_read32(priv, CSR_INT);
3774 iwl_write32(priv, CSR_INT, inta);
Zhu Yib481de92007-09-25 17:54:57 -07003775
3776 /* Ack/clear/reset pending flow-handler (DMA) interrupts.
3777 * Any new interrupts that happen after this, either while we're
3778 * in this tasklet, or later, will show up in next ISR/tasklet. */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003779 inta_fh = iwl_read32(priv, CSR_FH_INT_STATUS);
3780 iwl_write32(priv, CSR_FH_INT_STATUS, inta_fh);
Zhu Yib481de92007-09-25 17:54:57 -07003781
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08003782#ifdef CONFIG_IWL3945_DEBUG
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08003783 if (priv->debug_level & IWL_DL_ISR) {
Ben Cahill9fbab512007-11-29 11:09:47 +08003784 /* just for debug */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003785 inta_mask = iwl_read32(priv, CSR_INT_MASK);
Zhu Yib481de92007-09-25 17:54:57 -07003786 IWL_DEBUG_ISR("inta 0x%08x, enabled 0x%08x, fh 0x%08x\n",
3787 inta, inta_mask, inta_fh);
3788 }
3789#endif
3790
3791 /* Since CSR_INT and CSR_FH_INT_STATUS reads and clears are not
3792 * atomic, make sure that inta covers all the interrupts that
3793 * we've discovered, even if FH interrupt came in just after
3794 * reading CSR_INT. */
Tomas Winkler6f83eaa2008-03-04 18:09:28 -08003795 if (inta_fh & CSR39_FH_INT_RX_MASK)
Zhu Yib481de92007-09-25 17:54:57 -07003796 inta |= CSR_INT_BIT_FH_RX;
Tomas Winkler6f83eaa2008-03-04 18:09:28 -08003797 if (inta_fh & CSR39_FH_INT_TX_MASK)
Zhu Yib481de92007-09-25 17:54:57 -07003798 inta |= CSR_INT_BIT_FH_TX;
3799
3800 /* Now service all interrupt bits discovered above. */
3801 if (inta & CSR_INT_BIT_HW_ERR) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08003802 IWL_ERR(priv, "Microcode HW error detected. Restarting.\n");
Zhu Yib481de92007-09-25 17:54:57 -07003803
3804 /* Tell the device to stop sending interrupts */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003805 iwl3945_disable_interrupts(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003806
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003807 iwl3945_irq_handle_error(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003808
3809 handled |= CSR_INT_BIT_HW_ERR;
3810
3811 spin_unlock_irqrestore(&priv->lock, flags);
3812
3813 return;
3814 }
3815
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08003816#ifdef CONFIG_IWL3945_DEBUG
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08003817 if (priv->debug_level & (IWL_DL_ISR)) {
Zhu Yib481de92007-09-25 17:54:57 -07003818 /* NIC fires this, but we don't use it, redundant with WAKEUP */
Joonwoo Park25c03d82008-01-23 10:15:20 -08003819 if (inta & CSR_INT_BIT_SCD)
3820 IWL_DEBUG_ISR("Scheduler finished to transmit "
3821 "the frame/frames.\n");
Zhu Yib481de92007-09-25 17:54:57 -07003822
3823 /* Alive notification via Rx interrupt will do the real work */
3824 if (inta & CSR_INT_BIT_ALIVE)
3825 IWL_DEBUG_ISR("Alive interrupt\n");
3826 }
3827#endif
3828 /* Safely ignore these bits for debug checks below */
Joonwoo Park25c03d82008-01-23 10:15:20 -08003829 inta &= ~(CSR_INT_BIT_SCD | CSR_INT_BIT_ALIVE);
Zhu Yib481de92007-09-25 17:54:57 -07003830
Zhu Yib481de92007-09-25 17:54:57 -07003831 /* Error detected by uCode */
3832 if (inta & CSR_INT_BIT_SW_ERR) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08003833 IWL_ERR(priv, "Microcode SW error detected. "
3834 "Restarting 0x%X.\n", inta);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003835 iwl3945_irq_handle_error(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003836 handled |= CSR_INT_BIT_SW_ERR;
3837 }
3838
3839 /* uCode wakes up after power-down sleep */
3840 if (inta & CSR_INT_BIT_WAKEUP) {
3841 IWL_DEBUG_ISR("Wakeup interrupt\n");
Winkler, Tomas141c43a2009-01-08 10:19:53 -08003842 iwl_rx_queue_update_write_ptr(priv, &priv->rxq);
Samuel Ortiz4f3602c2009-01-19 15:30:25 -08003843 iwl_txq_update_write_ptr(priv, &priv->txq[0]);
3844 iwl_txq_update_write_ptr(priv, &priv->txq[1]);
3845 iwl_txq_update_write_ptr(priv, &priv->txq[2]);
3846 iwl_txq_update_write_ptr(priv, &priv->txq[3]);
3847 iwl_txq_update_write_ptr(priv, &priv->txq[4]);
3848 iwl_txq_update_write_ptr(priv, &priv->txq[5]);
Zhu Yib481de92007-09-25 17:54:57 -07003849
3850 handled |= CSR_INT_BIT_WAKEUP;
3851 }
3852
3853 /* All uCode command responses, including Tx command responses,
3854 * Rx "responses" (frame-received notification), and other
3855 * notifications from uCode come through here*/
3856 if (inta & (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX)) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003857 iwl3945_rx_handle(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003858 handled |= (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX);
3859 }
3860
3861 if (inta & CSR_INT_BIT_FH_TX) {
3862 IWL_DEBUG_ISR("Tx interrupt\n");
3863
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003864 iwl_write32(priv, CSR_FH_INT_STATUS, (1 << 6));
3865 if (!iwl_grab_nic_access(priv)) {
3866 iwl_write_direct32(priv, FH39_TCSR_CREDIT
Tomas Winklerbddadf82008-12-19 10:37:01 +08003867 (FH39_SRVC_CHNL), 0x0);
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003868 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003869 }
3870 handled |= CSR_INT_BIT_FH_TX;
3871 }
3872
3873 if (inta & ~handled)
Winkler, Tomas15b16872008-12-19 10:37:33 +08003874 IWL_ERR(priv, "Unhandled INTA bits 0x%08x\n", inta & ~handled);
Zhu Yib481de92007-09-25 17:54:57 -07003875
3876 if (inta & ~CSR_INI_SET_MASK) {
Winkler, Tomas39aadf82008-12-19 10:37:32 +08003877 IWL_WARN(priv, "Disabled INTA bits 0x%08x were pending\n",
Zhu Yib481de92007-09-25 17:54:57 -07003878 inta & ~CSR_INI_SET_MASK);
Winkler, Tomas39aadf82008-12-19 10:37:32 +08003879 IWL_WARN(priv, " with FH_INT = 0x%08x\n", inta_fh);
Zhu Yib481de92007-09-25 17:54:57 -07003880 }
3881
3882 /* Re-enable all interrupts */
Mohamed Abbas0359fac2008-03-28 16:21:08 -07003883 /* only Re-enable if disabled by irq */
3884 if (test_bit(STATUS_INT_ENABLED, &priv->status))
3885 iwl3945_enable_interrupts(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003886
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08003887#ifdef CONFIG_IWL3945_DEBUG
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08003888 if (priv->debug_level & (IWL_DL_ISR)) {
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003889 inta = iwl_read32(priv, CSR_INT);
3890 inta_mask = iwl_read32(priv, CSR_INT_MASK);
3891 inta_fh = iwl_read32(priv, CSR_FH_INT_STATUS);
Zhu Yib481de92007-09-25 17:54:57 -07003892 IWL_DEBUG_ISR("End inta 0x%08x, enabled 0x%08x, fh 0x%08x, "
3893 "flags 0x%08lx\n", inta, inta_mask, inta_fh, flags);
3894 }
3895#endif
3896 spin_unlock_irqrestore(&priv->lock, flags);
3897}
3898
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003899static irqreturn_t iwl3945_isr(int irq, void *data)
Zhu Yib481de92007-09-25 17:54:57 -07003900{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003901 struct iwl_priv *priv = data;
Zhu Yib481de92007-09-25 17:54:57 -07003902 u32 inta, inta_mask;
3903 u32 inta_fh;
3904 if (!priv)
3905 return IRQ_NONE;
3906
3907 spin_lock(&priv->lock);
3908
3909 /* Disable (but don't clear!) interrupts here to avoid
3910 * back-to-back ISRs and sporadic interrupts from our NIC.
3911 * If we have something to service, the tasklet will re-enable ints.
3912 * If we *don't* have something, we'll re-enable before leaving here. */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003913 inta_mask = iwl_read32(priv, CSR_INT_MASK); /* just for debug */
3914 iwl_write32(priv, CSR_INT_MASK, 0x00000000);
Zhu Yib481de92007-09-25 17:54:57 -07003915
3916 /* Discover which interrupts are active/pending */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003917 inta = iwl_read32(priv, CSR_INT);
3918 inta_fh = iwl_read32(priv, CSR_FH_INT_STATUS);
Zhu Yib481de92007-09-25 17:54:57 -07003919
3920 /* Ignore interrupt if there's nothing in NIC to service.
3921 * This may be due to IRQ shared with another device,
3922 * or due to sporadic interrupts thrown from our NIC. */
3923 if (!inta && !inta_fh) {
3924 IWL_DEBUG_ISR("Ignore interrupt, inta == 0, inta_fh == 0\n");
3925 goto none;
3926 }
3927
3928 if ((inta == 0xFFFFFFFF) || ((inta & 0xFFFFFFF0) == 0xa5a5a5a0)) {
3929 /* Hardware disappeared */
Winkler, Tomas39aadf82008-12-19 10:37:32 +08003930 IWL_WARN(priv, "HARDWARE GONE?? INTA == 0x%08x\n", inta);
Oliver Neukumcb4da1a2007-11-13 21:10:32 -08003931 goto unplugged;
Zhu Yib481de92007-09-25 17:54:57 -07003932 }
3933
3934 IWL_DEBUG_ISR("ISR inta 0x%08x, enabled 0x%08x, fh 0x%08x\n",
3935 inta, inta_mask, inta_fh);
3936
Joonwoo Park25c03d82008-01-23 10:15:20 -08003937 inta &= ~CSR_INT_BIT_SCD;
3938
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003939 /* iwl3945_irq_tasklet() will service interrupts and re-enable them */
Joonwoo Park25c03d82008-01-23 10:15:20 -08003940 if (likely(inta || inta_fh))
3941 tasklet_schedule(&priv->irq_tasklet);
Oliver Neukumcb4da1a2007-11-13 21:10:32 -08003942unplugged:
Zhu Yib481de92007-09-25 17:54:57 -07003943 spin_unlock(&priv->lock);
3944
3945 return IRQ_HANDLED;
3946
3947 none:
3948 /* re-enable interrupts here since we don't have anything to service. */
Mohamed Abbas0359fac2008-03-28 16:21:08 -07003949 /* only Re-enable if disabled by irq */
3950 if (test_bit(STATUS_INT_ENABLED, &priv->status))
3951 iwl3945_enable_interrupts(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003952 spin_unlock(&priv->lock);
3953 return IRQ_NONE;
3954}
3955
3956/************************** EEPROM BANDS ****************************
3957 *
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003958 * The iwl3945_eeprom_band definitions below provide the mapping from the
Zhu Yib481de92007-09-25 17:54:57 -07003959 * EEPROM contents to the specific channel number supported for each
3960 * band.
3961 *
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08003962 * For example, iwl3945_priv->eeprom39.band_3_channels[4] from the band_3
Zhu Yib481de92007-09-25 17:54:57 -07003963 * definition below maps to physical channel 42 in the 5.2GHz spectrum.
3964 * The specific geography and calibration information for that channel
3965 * is contained in the eeprom map itself.
3966 *
3967 * During init, we copy the eeprom information and channel map
3968 * information into priv->channel_info_24/52 and priv->channel_map_24/52
3969 *
3970 * channel_map_24/52 provides the index in the channel_info array for a
3971 * given channel. We have to have two separate maps as there is channel
3972 * overlap with the 2.4GHz and 5.2GHz spectrum as seen in band_1 and
3973 * band_2
3974 *
3975 * A value of 0xff stored in the channel_map indicates that the channel
3976 * is not supported by the hardware at all.
3977 *
3978 * A value of 0xfe in the channel_map indicates that the channel is not
3979 * valid for Tx with the current hardware. This means that
3980 * while the system can tune and receive on a given channel, it may not
3981 * be able to associate or transmit any frames on that
3982 * channel. There is no corresponding channel information for that
3983 * entry.
3984 *
3985 *********************************************************************/
3986
3987/* 2.4 GHz */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003988static const u8 iwl3945_eeprom_band_1[14] = {
Zhu Yib481de92007-09-25 17:54:57 -07003989 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14
3990};
3991
3992/* 5.2 GHz bands */
Ben Cahill9fbab512007-11-29 11:09:47 +08003993static const u8 iwl3945_eeprom_band_2[] = { /* 4915-5080MHz */
Zhu Yib481de92007-09-25 17:54:57 -07003994 183, 184, 185, 187, 188, 189, 192, 196, 7, 8, 11, 12, 16
3995};
3996
Ben Cahill9fbab512007-11-29 11:09:47 +08003997static const u8 iwl3945_eeprom_band_3[] = { /* 5170-5320MHz */
Zhu Yib481de92007-09-25 17:54:57 -07003998 34, 36, 38, 40, 42, 44, 46, 48, 52, 56, 60, 64
3999};
4000
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004001static const u8 iwl3945_eeprom_band_4[] = { /* 5500-5700MHz */
Zhu Yib481de92007-09-25 17:54:57 -07004002 100, 104, 108, 112, 116, 120, 124, 128, 132, 136, 140
4003};
4004
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004005static const u8 iwl3945_eeprom_band_5[] = { /* 5725-5825MHz */
Zhu Yib481de92007-09-25 17:54:57 -07004006 145, 149, 153, 157, 161, 165
4007};
4008
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08004009static void iwl3945_init_band_reference(const struct iwl_priv *priv, int band,
Zhu Yib481de92007-09-25 17:54:57 -07004010 int *eeprom_ch_count,
Samuel Ortiz0f741d92008-12-19 10:37:10 +08004011 const struct iwl_eeprom_channel
Zhu Yib481de92007-09-25 17:54:57 -07004012 **eeprom_ch_info,
4013 const u8 **eeprom_ch_index)
4014{
4015 switch (band) {
4016 case 1: /* 2.4GHz band */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004017 *eeprom_ch_count = ARRAY_SIZE(iwl3945_eeprom_band_1);
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08004018 *eeprom_ch_info = priv->eeprom39.band_1_channels;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004019 *eeprom_ch_index = iwl3945_eeprom_band_1;
Zhu Yib481de92007-09-25 17:54:57 -07004020 break;
Ben Cahill9fbab512007-11-29 11:09:47 +08004021 case 2: /* 4.9GHz band */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004022 *eeprom_ch_count = ARRAY_SIZE(iwl3945_eeprom_band_2);
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08004023 *eeprom_ch_info = priv->eeprom39.band_2_channels;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004024 *eeprom_ch_index = iwl3945_eeprom_band_2;
Zhu Yib481de92007-09-25 17:54:57 -07004025 break;
4026 case 3: /* 5.2GHz band */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004027 *eeprom_ch_count = ARRAY_SIZE(iwl3945_eeprom_band_3);
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08004028 *eeprom_ch_info = priv->eeprom39.band_3_channels;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004029 *eeprom_ch_index = iwl3945_eeprom_band_3;
Zhu Yib481de92007-09-25 17:54:57 -07004030 break;
Ben Cahill9fbab512007-11-29 11:09:47 +08004031 case 4: /* 5.5GHz band */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004032 *eeprom_ch_count = ARRAY_SIZE(iwl3945_eeprom_band_4);
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08004033 *eeprom_ch_info = priv->eeprom39.band_4_channels;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004034 *eeprom_ch_index = iwl3945_eeprom_band_4;
Zhu Yib481de92007-09-25 17:54:57 -07004035 break;
Ben Cahill9fbab512007-11-29 11:09:47 +08004036 case 5: /* 5.7GHz band */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004037 *eeprom_ch_count = ARRAY_SIZE(iwl3945_eeprom_band_5);
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08004038 *eeprom_ch_info = priv->eeprom39.band_5_channels;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004039 *eeprom_ch_index = iwl3945_eeprom_band_5;
Zhu Yib481de92007-09-25 17:54:57 -07004040 break;
4041 default:
4042 BUG();
4043 return;
4044 }
4045}
4046
Cahill, Ben M6440adb2007-11-29 11:09:55 +08004047/**
4048 * iwl3945_get_channel_info - Find driver's private channel info
4049 *
4050 * Based on band and channel number.
4051 */
Samuel Ortizd20b3c62008-12-19 10:37:15 +08004052const struct iwl_channel_info *
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08004053iwl3945_get_channel_info(const struct iwl_priv *priv,
Samuel Ortizd20b3c62008-12-19 10:37:15 +08004054 enum ieee80211_band band, u16 channel)
Zhu Yib481de92007-09-25 17:54:57 -07004055{
4056 int i;
4057
Johannes Berg8318d782008-01-24 19:38:38 +01004058 switch (band) {
4059 case IEEE80211_BAND_5GHZ:
Zhu Yib481de92007-09-25 17:54:57 -07004060 for (i = 14; i < priv->channel_count; i++) {
4061 if (priv->channel_info[i].channel == channel)
4062 return &priv->channel_info[i];
4063 }
4064 break;
4065
Johannes Berg8318d782008-01-24 19:38:38 +01004066 case IEEE80211_BAND_2GHZ:
Zhu Yib481de92007-09-25 17:54:57 -07004067 if (channel >= 1 && channel <= 14)
4068 return &priv->channel_info[channel - 1];
4069 break;
Johannes Berg8318d782008-01-24 19:38:38 +01004070 case IEEE80211_NUM_BANDS:
4071 WARN_ON(1);
Zhu Yib481de92007-09-25 17:54:57 -07004072 }
4073
4074 return NULL;
4075}
4076
4077#define CHECK_AND_PRINT(x) ((eeprom_ch_info[ch].flags & EEPROM_CHANNEL_##x) \
4078 ? # x " " : "")
4079
Cahill, Ben M6440adb2007-11-29 11:09:55 +08004080/**
4081 * iwl3945_init_channel_map - Set up driver's info for all possible channels
4082 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08004083static int iwl3945_init_channel_map(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004084{
4085 int eeprom_ch_count = 0;
4086 const u8 *eeprom_ch_index = NULL;
Samuel Ortiz0f741d92008-12-19 10:37:10 +08004087 const struct iwl_eeprom_channel *eeprom_ch_info = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07004088 int band, ch;
Samuel Ortizd20b3c62008-12-19 10:37:15 +08004089 struct iwl_channel_info *ch_info;
Zhu Yib481de92007-09-25 17:54:57 -07004090
4091 if (priv->channel_count) {
4092 IWL_DEBUG_INFO("Channel map already initialized.\n");
4093 return 0;
4094 }
4095
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08004096 if (priv->eeprom39.version < 0x2f) {
Winkler, Tomas39aadf82008-12-19 10:37:32 +08004097 IWL_WARN(priv, "Unsupported EEPROM version: 0x%04X\n",
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08004098 priv->eeprom39.version);
Zhu Yib481de92007-09-25 17:54:57 -07004099 return -EINVAL;
4100 }
4101
4102 IWL_DEBUG_INFO("Initializing regulatory info from EEPROM\n");
4103
4104 priv->channel_count =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004105 ARRAY_SIZE(iwl3945_eeprom_band_1) +
4106 ARRAY_SIZE(iwl3945_eeprom_band_2) +
4107 ARRAY_SIZE(iwl3945_eeprom_band_3) +
4108 ARRAY_SIZE(iwl3945_eeprom_band_4) +
4109 ARRAY_SIZE(iwl3945_eeprom_band_5);
Zhu Yib481de92007-09-25 17:54:57 -07004110
4111 IWL_DEBUG_INFO("Parsing data for %d channels.\n", priv->channel_count);
4112
Samuel Ortizd20b3c62008-12-19 10:37:15 +08004113 priv->channel_info = kzalloc(sizeof(struct iwl_channel_info) *
Zhu Yib481de92007-09-25 17:54:57 -07004114 priv->channel_count, GFP_KERNEL);
4115 if (!priv->channel_info) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08004116 IWL_ERR(priv, "Could not allocate channel_info\n");
Zhu Yib481de92007-09-25 17:54:57 -07004117 priv->channel_count = 0;
4118 return -ENOMEM;
4119 }
4120
4121 ch_info = priv->channel_info;
4122
4123 /* Loop through the 5 EEPROM bands adding them in order to the
4124 * channel map we maintain (that contains additional information than
4125 * what just in the EEPROM) */
4126 for (band = 1; band <= 5; band++) {
4127
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004128 iwl3945_init_band_reference(priv, band, &eeprom_ch_count,
Zhu Yib481de92007-09-25 17:54:57 -07004129 &eeprom_ch_info, &eeprom_ch_index);
4130
4131 /* Loop through each band adding each of the channels */
4132 for (ch = 0; ch < eeprom_ch_count; ch++) {
4133 ch_info->channel = eeprom_ch_index[ch];
Johannes Berg8318d782008-01-24 19:38:38 +01004134 ch_info->band = (band == 1) ? IEEE80211_BAND_2GHZ :
4135 IEEE80211_BAND_5GHZ;
Zhu Yib481de92007-09-25 17:54:57 -07004136
4137 /* permanently store EEPROM's channel regulatory flags
4138 * and max power in channel info database. */
4139 ch_info->eeprom = eeprom_ch_info[ch];
4140
4141 /* Copy the run-time flags so they are there even on
4142 * invalid channels */
4143 ch_info->flags = eeprom_ch_info[ch].flags;
4144
4145 if (!(is_channel_valid(ch_info))) {
4146 IWL_DEBUG_INFO("Ch. %d Flags %x [%sGHz] - "
4147 "No traffic\n",
4148 ch_info->channel,
4149 ch_info->flags,
4150 is_channel_a_band(ch_info) ?
4151 "5.2" : "2.4");
4152 ch_info++;
4153 continue;
4154 }
4155
4156 /* Initialize regulatory-based run-time data */
4157 ch_info->max_power_avg = ch_info->curr_txpow =
4158 eeprom_ch_info[ch].max_power_avg;
4159 ch_info->scan_power = eeprom_ch_info[ch].max_power_avg;
4160 ch_info->min_power = 0;
4161
Guy Cohenfe7c4042008-04-21 15:41:56 -07004162 IWL_DEBUG_INFO("Ch. %d [%sGHz] %s%s%s%s%s%s(0x%02x"
Zhu Yib481de92007-09-25 17:54:57 -07004163 " %ddBm): Ad-Hoc %ssupported\n",
4164 ch_info->channel,
4165 is_channel_a_band(ch_info) ?
4166 "5.2" : "2.4",
Tomas Winkler8211ef72008-03-02 01:36:04 +02004167 CHECK_AND_PRINT(VALID),
Zhu Yib481de92007-09-25 17:54:57 -07004168 CHECK_AND_PRINT(IBSS),
4169 CHECK_AND_PRINT(ACTIVE),
4170 CHECK_AND_PRINT(RADAR),
4171 CHECK_AND_PRINT(WIDE),
Zhu Yib481de92007-09-25 17:54:57 -07004172 CHECK_AND_PRINT(DFS),
4173 eeprom_ch_info[ch].flags,
4174 eeprom_ch_info[ch].max_power_avg,
4175 ((eeprom_ch_info[ch].
4176 flags & EEPROM_CHANNEL_IBSS)
4177 && !(eeprom_ch_info[ch].
4178 flags & EEPROM_CHANNEL_RADAR))
4179 ? "" : "not ");
4180
4181 /* Set the user_txpower_limit to the highest power
4182 * supported by any channel */
4183 if (eeprom_ch_info[ch].max_power_avg >
4184 priv->user_txpower_limit)
4185 priv->user_txpower_limit =
4186 eeprom_ch_info[ch].max_power_avg;
4187
4188 ch_info++;
4189 }
4190 }
4191
Cahill, Ben M6440adb2007-11-29 11:09:55 +08004192 /* Set up txpower settings in driver for all channels */
Zhu Yib481de92007-09-25 17:54:57 -07004193 if (iwl3945_txpower_set_from_eeprom(priv))
4194 return -EIO;
4195
4196 return 0;
4197}
4198
Reinette Chatre849e0dc2008-01-23 10:15:18 -08004199/*
4200 * iwl3945_free_channel_map - undo allocations in iwl3945_init_channel_map
4201 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08004202static void iwl3945_free_channel_map(struct iwl_priv *priv)
Reinette Chatre849e0dc2008-01-23 10:15:18 -08004203{
4204 kfree(priv->channel_info);
4205 priv->channel_count = 0;
4206}
4207
Zhu Yib481de92007-09-25 17:54:57 -07004208/* For active scan, listen ACTIVE_DWELL_TIME (msec) on each channel after
4209 * sending probe req. This should be set long enough to hear probe responses
4210 * from more than one AP. */
Abhijeet Kolekarf9340522008-09-03 11:26:58 +08004211#define IWL_ACTIVE_DWELL_TIME_24 (30) /* all times in msec */
4212#define IWL_ACTIVE_DWELL_TIME_52 (20)
4213
4214#define IWL_ACTIVE_DWELL_FACTOR_24GHZ (3)
4215#define IWL_ACTIVE_DWELL_FACTOR_52GHZ (2)
Zhu Yib481de92007-09-25 17:54:57 -07004216
4217/* For faster active scanning, scan will move to the next channel if fewer than
4218 * PLCP_QUIET_THRESH packets are heard on this channel within
4219 * ACTIVE_QUIET_TIME after sending probe request. This shortens the dwell
4220 * time if it's a quiet channel (nothing responded to our probe, and there's
4221 * no other traffic).
4222 * Disable "quiet" feature by setting PLCP_QUIET_THRESH to 0. */
4223#define IWL_PLCP_QUIET_THRESH __constant_cpu_to_le16(1) /* packets */
Abhijeet Kolekarf9340522008-09-03 11:26:58 +08004224#define IWL_ACTIVE_QUIET_TIME __constant_cpu_to_le16(10) /* msec */
Zhu Yib481de92007-09-25 17:54:57 -07004225
4226/* For passive scan, listen PASSIVE_DWELL_TIME (msec) on each channel.
4227 * Must be set longer than active dwell time.
4228 * For the most reliable scan, set > AP beacon interval (typically 100msec). */
4229#define IWL_PASSIVE_DWELL_TIME_24 (20) /* all times in msec */
4230#define IWL_PASSIVE_DWELL_TIME_52 (10)
4231#define IWL_PASSIVE_DWELL_BASE (100)
4232#define IWL_CHANNEL_TUNE_TIME 5
4233
Kolekar, Abhijeete720ce92008-11-07 09:58:42 -08004234#define IWL_SCAN_PROBE_MASK(n) (BIT(n) | (BIT(n) - BIT(1)))
Abhijeet Kolekarf9340522008-09-03 11:26:58 +08004235
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08004236static inline u16 iwl3945_get_active_dwell_time(struct iwl_priv *priv,
Abhijeet Kolekarf9340522008-09-03 11:26:58 +08004237 enum ieee80211_band band,
4238 u8 n_probes)
Zhu Yib481de92007-09-25 17:54:57 -07004239{
Johannes Berg8318d782008-01-24 19:38:38 +01004240 if (band == IEEE80211_BAND_5GHZ)
Abhijeet Kolekarf9340522008-09-03 11:26:58 +08004241 return IWL_ACTIVE_DWELL_TIME_52 +
4242 IWL_ACTIVE_DWELL_FACTOR_52GHZ * (n_probes + 1);
Zhu Yib481de92007-09-25 17:54:57 -07004243 else
Abhijeet Kolekarf9340522008-09-03 11:26:58 +08004244 return IWL_ACTIVE_DWELL_TIME_24 +
4245 IWL_ACTIVE_DWELL_FACTOR_24GHZ * (n_probes + 1);
Zhu Yib481de92007-09-25 17:54:57 -07004246}
4247
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08004248static u16 iwl3945_get_passive_dwell_time(struct iwl_priv *priv,
Johannes Berg8318d782008-01-24 19:38:38 +01004249 enum ieee80211_band band)
Zhu Yib481de92007-09-25 17:54:57 -07004250{
Johannes Berg8318d782008-01-24 19:38:38 +01004251 u16 passive = (band == IEEE80211_BAND_2GHZ) ?
Zhu Yib481de92007-09-25 17:54:57 -07004252 IWL_PASSIVE_DWELL_BASE + IWL_PASSIVE_DWELL_TIME_24 :
4253 IWL_PASSIVE_DWELL_BASE + IWL_PASSIVE_DWELL_TIME_52;
4254
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004255 if (iwl3945_is_associated(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07004256 /* If we're associated, we clamp the maximum passive
4257 * dwell time to be 98% of the beacon interval (minus
4258 * 2 * channel tune time) */
4259 passive = priv->beacon_int;
4260 if ((passive > IWL_PASSIVE_DWELL_BASE) || !passive)
4261 passive = IWL_PASSIVE_DWELL_BASE;
4262 passive = (passive * 98) / 100 - IWL_CHANNEL_TUNE_TIME * 2;
4263 }
4264
Zhu Yib481de92007-09-25 17:54:57 -07004265 return passive;
4266}
4267
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08004268static int iwl3945_get_channels_for_scan(struct iwl_priv *priv,
Johannes Berg8318d782008-01-24 19:38:38 +01004269 enum ieee80211_band band,
Abhijeet Kolekarf9340522008-09-03 11:26:58 +08004270 u8 is_active, u8 n_probes,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004271 struct iwl3945_scan_channel *scan_ch)
Zhu Yib481de92007-09-25 17:54:57 -07004272{
4273 const struct ieee80211_channel *channels = NULL;
Johannes Berg8318d782008-01-24 19:38:38 +01004274 const struct ieee80211_supported_band *sband;
Samuel Ortizd20b3c62008-12-19 10:37:15 +08004275 const struct iwl_channel_info *ch_info;
Zhu Yib481de92007-09-25 17:54:57 -07004276 u16 passive_dwell = 0;
4277 u16 active_dwell = 0;
4278 int added, i;
4279
Kolekar, Abhijeetcbba18c2008-12-19 10:37:42 +08004280 sband = iwl_get_hw_mode(priv, band);
Johannes Berg8318d782008-01-24 19:38:38 +01004281 if (!sband)
Zhu Yib481de92007-09-25 17:54:57 -07004282 return 0;
4283
Johannes Berg8318d782008-01-24 19:38:38 +01004284 channels = sband->channels;
Zhu Yib481de92007-09-25 17:54:57 -07004285
Abhijeet Kolekarf9340522008-09-03 11:26:58 +08004286 active_dwell = iwl3945_get_active_dwell_time(priv, band, n_probes);
Johannes Berg8318d782008-01-24 19:38:38 +01004287 passive_dwell = iwl3945_get_passive_dwell_time(priv, band);
Zhu Yib481de92007-09-25 17:54:57 -07004288
Abhijeet Kolekar8f4807a2008-09-03 11:26:31 +08004289 if (passive_dwell <= active_dwell)
4290 passive_dwell = active_dwell + 1;
4291
Johannes Berg8318d782008-01-24 19:38:38 +01004292 for (i = 0, added = 0; i < sband->n_channels; i++) {
Johannes Berg182e2e62008-04-04 10:41:56 +02004293 if (channels[i].flags & IEEE80211_CHAN_DISABLED)
4294 continue;
4295
Johannes Berg8318d782008-01-24 19:38:38 +01004296 scan_ch->channel = channels[i].hw_value;
Zhu Yib481de92007-09-25 17:54:57 -07004297
Johannes Berg8318d782008-01-24 19:38:38 +01004298 ch_info = iwl3945_get_channel_info(priv, band, scan_ch->channel);
Zhu Yib481de92007-09-25 17:54:57 -07004299 if (!is_channel_valid(ch_info)) {
Ron Rindjunsky66b50042008-06-25 16:46:31 +08004300 IWL_DEBUG_SCAN("Channel %d is INVALID for this band.\n",
Zhu Yib481de92007-09-25 17:54:57 -07004301 scan_ch->channel);
4302 continue;
4303 }
4304
Zhu Yib481de92007-09-25 17:54:57 -07004305 scan_ch->active_dwell = cpu_to_le16(active_dwell);
4306 scan_ch->passive_dwell = cpu_to_le16(passive_dwell);
Abhijeet Kolekar011a0332008-12-02 12:14:07 -08004307 /* If passive , set up for auto-switch
4308 * and use long active_dwell time.
4309 */
4310 if (!is_active || is_channel_passive(ch_info) ||
4311 (channels[i].flags & IEEE80211_CHAN_PASSIVE_SCAN)) {
4312 scan_ch->type = 0; /* passive */
4313 if (IWL_UCODE_API(priv->ucode_ver) == 1)
4314 scan_ch->active_dwell = cpu_to_le16(passive_dwell - 1);
4315 } else {
4316 scan_ch->type = 1; /* active */
4317 }
4318
4319 /* Set direct probe bits. These may be used both for active
4320 * scan channels (probes gets sent right away),
4321 * or for passive channels (probes get se sent only after
4322 * hearing clear Rx packet).*/
4323 if (IWL_UCODE_API(priv->ucode_ver) >= 2) {
4324 if (n_probes)
4325 scan_ch->type |= IWL_SCAN_PROBE_MASK(n_probes);
4326 } else {
4327 /* uCode v1 does not allow setting direct probe bits on
4328 * passive channel. */
4329 if ((scan_ch->type & 1) && n_probes)
4330 scan_ch->type |= IWL_SCAN_PROBE_MASK(n_probes);
4331 }
Zhu Yib481de92007-09-25 17:54:57 -07004332
Ben Cahill9fbab512007-11-29 11:09:47 +08004333 /* Set txpower levels to defaults */
Zhu Yib481de92007-09-25 17:54:57 -07004334 scan_ch->tpc.dsp_atten = 110;
4335 /* scan_pwr_info->tpc.dsp_atten; */
4336
4337 /*scan_pwr_info->tpc.tx_gain; */
Johannes Berg8318d782008-01-24 19:38:38 +01004338 if (band == IEEE80211_BAND_5GHZ)
Zhu Yib481de92007-09-25 17:54:57 -07004339 scan_ch->tpc.tx_gain = ((1 << 5) | (3 << 3)) | 3;
4340 else {
4341 scan_ch->tpc.tx_gain = ((1 << 5) | (5 << 3));
4342 /* NOTE: if we were doing 6Mb OFDM for scans we'd use
Ben Cahill9fbab512007-11-29 11:09:47 +08004343 * power level:
Reinette Chatre8a1b0242008-01-14 17:46:25 -08004344 * scan_ch->tpc.tx_gain = ((1 << 5) | (2 << 3)) | 3;
Zhu Yib481de92007-09-25 17:54:57 -07004345 */
4346 }
4347
4348 IWL_DEBUG_SCAN("Scanning %d [%s %d]\n",
4349 scan_ch->channel,
4350 (scan_ch->type & 1) ? "ACTIVE" : "PASSIVE",
4351 (scan_ch->type & 1) ?
4352 active_dwell : passive_dwell);
4353
4354 scan_ch++;
4355 added++;
4356 }
4357
4358 IWL_DEBUG_SCAN("total channels to scan %d \n", added);
4359 return added;
4360}
4361
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08004362static void iwl3945_init_hw_rates(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07004363 struct ieee80211_rate *rates)
4364{
4365 int i;
4366
4367 for (i = 0; i < IWL_RATE_COUNT; i++) {
Johannes Berg8318d782008-01-24 19:38:38 +01004368 rates[i].bitrate = iwl3945_rates[i].ieee * 5;
4369 rates[i].hw_value = i; /* Rate scaling will work on indexes */
4370 rates[i].hw_value_short = i;
4371 rates[i].flags = 0;
Samuel Ortizd9829a62008-12-19 10:37:12 +08004372 if ((i > IWL39_LAST_OFDM_RATE) || (i < IWL_FIRST_OFDM_RATE)) {
Zhu Yib481de92007-09-25 17:54:57 -07004373 /*
Johannes Berg8318d782008-01-24 19:38:38 +01004374 * If CCK != 1M then set short preamble rate flag.
Zhu Yib481de92007-09-25 17:54:57 -07004375 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004376 rates[i].flags |= (iwl3945_rates[i].plcp == 10) ?
Johannes Berg8318d782008-01-24 19:38:38 +01004377 0 : IEEE80211_RATE_SHORT_PREAMBLE;
Zhu Yib481de92007-09-25 17:54:57 -07004378 }
Zhu Yib481de92007-09-25 17:54:57 -07004379 }
4380}
4381
4382/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004383 * iwl3945_init_geos - Initialize mac80211's geo/channel info based from eeprom
Zhu Yib481de92007-09-25 17:54:57 -07004384 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08004385static int iwl3945_init_geos(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004386{
Samuel Ortizd20b3c62008-12-19 10:37:15 +08004387 struct iwl_channel_info *ch;
Tomas Winkler8211ef72008-03-02 01:36:04 +02004388 struct ieee80211_supported_band *sband;
Zhu Yib481de92007-09-25 17:54:57 -07004389 struct ieee80211_channel *channels;
4390 struct ieee80211_channel *geo_ch;
4391 struct ieee80211_rate *rates;
4392 int i = 0;
Zhu Yib481de92007-09-25 17:54:57 -07004393
Johannes Berg8318d782008-01-24 19:38:38 +01004394 if (priv->bands[IEEE80211_BAND_2GHZ].n_bitrates ||
4395 priv->bands[IEEE80211_BAND_5GHZ].n_bitrates) {
Zhu Yib481de92007-09-25 17:54:57 -07004396 IWL_DEBUG_INFO("Geography modes already initialized.\n");
4397 set_bit(STATUS_GEO_CONFIGURED, &priv->status);
4398 return 0;
4399 }
4400
Zhu Yib481de92007-09-25 17:54:57 -07004401 channels = kzalloc(sizeof(struct ieee80211_channel) *
4402 priv->channel_count, GFP_KERNEL);
Johannes Berg8318d782008-01-24 19:38:38 +01004403 if (!channels)
Zhu Yib481de92007-09-25 17:54:57 -07004404 return -ENOMEM;
Zhu Yib481de92007-09-25 17:54:57 -07004405
Tomas Winkler8211ef72008-03-02 01:36:04 +02004406 rates = kzalloc((sizeof(struct ieee80211_rate) * (IWL_RATE_COUNT + 1)),
Zhu Yib481de92007-09-25 17:54:57 -07004407 GFP_KERNEL);
4408 if (!rates) {
Zhu Yib481de92007-09-25 17:54:57 -07004409 kfree(channels);
4410 return -ENOMEM;
4411 }
4412
Zhu Yib481de92007-09-25 17:54:57 -07004413 /* 5.2GHz channels start after the 2.4GHz channels */
Tomas Winkler8211ef72008-03-02 01:36:04 +02004414 sband = &priv->bands[IEEE80211_BAND_5GHZ];
4415 sband->channels = &channels[ARRAY_SIZE(iwl3945_eeprom_band_1)];
4416 /* just OFDM */
4417 sband->bitrates = &rates[IWL_FIRST_OFDM_RATE];
4418 sband->n_bitrates = IWL_RATE_COUNT - IWL_FIRST_OFDM_RATE;
Zhu Yib481de92007-09-25 17:54:57 -07004419
Tomas Winkler8211ef72008-03-02 01:36:04 +02004420 sband = &priv->bands[IEEE80211_BAND_2GHZ];
4421 sband->channels = channels;
4422 /* OFDM & CCK */
4423 sband->bitrates = rates;
4424 sband->n_bitrates = IWL_RATE_COUNT;
Zhu Yib481de92007-09-25 17:54:57 -07004425
4426 priv->ieee_channels = channels;
4427 priv->ieee_rates = rates;
4428
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004429 iwl3945_init_hw_rates(priv, rates);
Zhu Yib481de92007-09-25 17:54:57 -07004430
Tomas Winkler8211ef72008-03-02 01:36:04 +02004431 for (i = 0; i < priv->channel_count; i++) {
Zhu Yib481de92007-09-25 17:54:57 -07004432 ch = &priv->channel_info[i];
4433
Tomas Winkler8211ef72008-03-02 01:36:04 +02004434 /* FIXME: might be removed if scan is OK*/
4435 if (!is_channel_valid(ch))
Zhu Yib481de92007-09-25 17:54:57 -07004436 continue;
Zhu Yib481de92007-09-25 17:54:57 -07004437
4438 if (is_channel_a_band(ch))
Tomas Winkler8211ef72008-03-02 01:36:04 +02004439 sband = &priv->bands[IEEE80211_BAND_5GHZ];
Johannes Berg8318d782008-01-24 19:38:38 +01004440 else
Tomas Winkler8211ef72008-03-02 01:36:04 +02004441 sband = &priv->bands[IEEE80211_BAND_2GHZ];
Zhu Yib481de92007-09-25 17:54:57 -07004442
Tomas Winkler8211ef72008-03-02 01:36:04 +02004443 geo_ch = &sband->channels[sband->n_channels++];
4444
4445 geo_ch->center_freq = ieee80211_channel_to_frequency(ch->channel);
Johannes Berg8318d782008-01-24 19:38:38 +01004446 geo_ch->max_power = ch->max_power_avg;
4447 geo_ch->max_antenna_gain = 0xff;
Mohamed Abbas7b723042008-01-31 21:46:40 -08004448 geo_ch->hw_value = ch->channel;
Zhu Yib481de92007-09-25 17:54:57 -07004449
4450 if (is_channel_valid(ch)) {
Johannes Berg8318d782008-01-24 19:38:38 +01004451 if (!(ch->flags & EEPROM_CHANNEL_IBSS))
4452 geo_ch->flags |= IEEE80211_CHAN_NO_IBSS;
Zhu Yib481de92007-09-25 17:54:57 -07004453
Johannes Berg8318d782008-01-24 19:38:38 +01004454 if (!(ch->flags & EEPROM_CHANNEL_ACTIVE))
4455 geo_ch->flags |= IEEE80211_CHAN_PASSIVE_SCAN;
Zhu Yib481de92007-09-25 17:54:57 -07004456
4457 if (ch->flags & EEPROM_CHANNEL_RADAR)
Johannes Berg8318d782008-01-24 19:38:38 +01004458 geo_ch->flags |= IEEE80211_CHAN_RADAR;
Zhu Yib481de92007-09-25 17:54:57 -07004459
4460 if (ch->max_power_avg > priv->max_channel_txpower_limit)
4461 priv->max_channel_txpower_limit =
4462 ch->max_power_avg;
Tomas Winkler8211ef72008-03-02 01:36:04 +02004463 } else {
Johannes Berg8318d782008-01-24 19:38:38 +01004464 geo_ch->flags |= IEEE80211_CHAN_DISABLED;
Tomas Winkler8211ef72008-03-02 01:36:04 +02004465 }
4466
4467 /* Save flags for reg domain usage */
4468 geo_ch->orig_flags = geo_ch->flags;
4469
4470 IWL_DEBUG_INFO("Channel %d Freq=%d[%sGHz] %s flag=0%X\n",
4471 ch->channel, geo_ch->center_freq,
4472 is_channel_a_band(ch) ? "5.2" : "2.4",
4473 geo_ch->flags & IEEE80211_CHAN_DISABLED ?
4474 "restricted" : "valid",
4475 geo_ch->flags);
Zhu Yib481de92007-09-25 17:54:57 -07004476 }
4477
Tomas Winkler82b9a122008-03-04 18:09:30 -08004478 if ((priv->bands[IEEE80211_BAND_5GHZ].n_channels == 0) &&
4479 priv->cfg->sku & IWL_SKU_A) {
Tomas Winkler978785a2008-12-19 10:37:31 +08004480 IWL_INFO(priv, "Incorrectly detected BG card as ABG. "
4481 "Please send your PCI ID 0x%04X:0x%04X to maintainer.\n",
4482 priv->pci_dev->device, priv->pci_dev->subsystem_device);
Tomas Winkler82b9a122008-03-04 18:09:30 -08004483 priv->cfg->sku &= ~IWL_SKU_A;
Zhu Yib481de92007-09-25 17:54:57 -07004484 }
4485
Tomas Winkler978785a2008-12-19 10:37:31 +08004486 IWL_INFO(priv, "Tunable channels: %d 802.11bg, %d 802.11a channels\n",
Johannes Berg8318d782008-01-24 19:38:38 +01004487 priv->bands[IEEE80211_BAND_2GHZ].n_channels,
4488 priv->bands[IEEE80211_BAND_5GHZ].n_channels);
Zhu Yib481de92007-09-25 17:54:57 -07004489
John W. Linvillee0e0a672008-03-25 15:58:40 -04004490 if (priv->bands[IEEE80211_BAND_2GHZ].n_channels)
4491 priv->hw->wiphy->bands[IEEE80211_BAND_2GHZ] =
4492 &priv->bands[IEEE80211_BAND_2GHZ];
4493 if (priv->bands[IEEE80211_BAND_5GHZ].n_channels)
4494 priv->hw->wiphy->bands[IEEE80211_BAND_5GHZ] =
4495 &priv->bands[IEEE80211_BAND_5GHZ];
Zhu Yib481de92007-09-25 17:54:57 -07004496
Zhu Yib481de92007-09-25 17:54:57 -07004497 set_bit(STATUS_GEO_CONFIGURED, &priv->status);
4498
4499 return 0;
4500}
4501
Reinette Chatre849e0dc2008-01-23 10:15:18 -08004502/*
4503 * iwl3945_free_geos - undo allocations in iwl3945_init_geos
4504 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08004505static void iwl3945_free_geos(struct iwl_priv *priv)
Reinette Chatre849e0dc2008-01-23 10:15:18 -08004506{
Reinette Chatre849e0dc2008-01-23 10:15:18 -08004507 kfree(priv->ieee_channels);
4508 kfree(priv->ieee_rates);
4509 clear_bit(STATUS_GEO_CONFIGURED, &priv->status);
4510}
4511
Zhu Yib481de92007-09-25 17:54:57 -07004512/******************************************************************************
4513 *
4514 * uCode download functions
4515 *
4516 ******************************************************************************/
4517
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08004518static void iwl3945_dealloc_ucode_pci(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004519{
Tomas Winkler98c92212008-01-14 17:46:20 -08004520 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_code);
4521 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_data);
4522 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_data_backup);
4523 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_init);
4524 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_init_data);
4525 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_boot);
Zhu Yib481de92007-09-25 17:54:57 -07004526}
4527
4528/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004529 * iwl3945_verify_inst_full - verify runtime uCode image in card vs. host,
Zhu Yib481de92007-09-25 17:54:57 -07004530 * looking at all data.
4531 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08004532static int iwl3945_verify_inst_full(struct iwl_priv *priv, __le32 *image, u32 len)
Zhu Yib481de92007-09-25 17:54:57 -07004533{
4534 u32 val;
4535 u32 save_len = len;
4536 int rc = 0;
4537 u32 errcnt;
4538
4539 IWL_DEBUG_INFO("ucode inst image size is %u\n", len);
4540
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08004541 rc = iwl_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004542 if (rc)
4543 return rc;
4544
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08004545 iwl_write_direct32(priv, HBUS_TARG_MEM_RADDR,
Samuel Ortiz250bdd22008-12-19 10:37:11 +08004546 IWL39_RTC_INST_LOWER_BOUND);
Zhu Yib481de92007-09-25 17:54:57 -07004547
4548 errcnt = 0;
4549 for (; len > 0; len -= sizeof(u32), image++) {
4550 /* read data comes through single port, auto-incr addr */
4551 /* NOTE: Use the debugless read so we don't flood kernel log
4552 * if IWL_DL_IO is set */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08004553 val = _iwl_read_direct32(priv, HBUS_TARG_MEM_RDAT);
Zhu Yib481de92007-09-25 17:54:57 -07004554 if (val != le32_to_cpu(*image)) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08004555 IWL_ERR(priv, "uCode INST section is invalid at "
Zhu Yib481de92007-09-25 17:54:57 -07004556 "offset 0x%x, is 0x%x, s/b 0x%x\n",
4557 save_len - len, val, le32_to_cpu(*image));
4558 rc = -EIO;
4559 errcnt++;
4560 if (errcnt >= 20)
4561 break;
4562 }
4563 }
4564
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08004565 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004566
4567 if (!errcnt)
Ian Schrambc434dd2007-10-25 17:15:29 +08004568 IWL_DEBUG_INFO("ucode image in INSTRUCTION memory is good\n");
Zhu Yib481de92007-09-25 17:54:57 -07004569
4570 return rc;
4571}
4572
4573
4574/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004575 * iwl3945_verify_inst_sparse - verify runtime uCode image in card vs. host,
Zhu Yib481de92007-09-25 17:54:57 -07004576 * using sample data 100 bytes apart. If these sample points are good,
4577 * it's a pretty good bet that everything between them is good, too.
4578 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08004579static int iwl3945_verify_inst_sparse(struct iwl_priv *priv, __le32 *image, u32 len)
Zhu Yib481de92007-09-25 17:54:57 -07004580{
4581 u32 val;
4582 int rc = 0;
4583 u32 errcnt = 0;
4584 u32 i;
4585
4586 IWL_DEBUG_INFO("ucode inst image size is %u\n", len);
4587
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08004588 rc = iwl_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004589 if (rc)
4590 return rc;
4591
4592 for (i = 0; i < len; i += 100, image += 100/sizeof(u32)) {
4593 /* read data comes through single port, auto-incr addr */
4594 /* NOTE: Use the debugless read so we don't flood kernel log
4595 * if IWL_DL_IO is set */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08004596 iwl_write_direct32(priv, HBUS_TARG_MEM_RADDR,
Samuel Ortiz250bdd22008-12-19 10:37:11 +08004597 i + IWL39_RTC_INST_LOWER_BOUND);
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08004598 val = _iwl_read_direct32(priv, HBUS_TARG_MEM_RDAT);
Zhu Yib481de92007-09-25 17:54:57 -07004599 if (val != le32_to_cpu(*image)) {
4600#if 0 /* Enable this if you want to see details */
Winkler, Tomas15b16872008-12-19 10:37:33 +08004601 IWL_ERR(priv, "uCode INST section is invalid at "
Zhu Yib481de92007-09-25 17:54:57 -07004602 "offset 0x%x, is 0x%x, s/b 0x%x\n",
4603 i, val, *image);
4604#endif
4605 rc = -EIO;
4606 errcnt++;
4607 if (errcnt >= 3)
4608 break;
4609 }
4610 }
4611
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08004612 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004613
4614 return rc;
4615}
4616
4617
4618/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004619 * iwl3945_verify_ucode - determine which instruction image is in SRAM,
Zhu Yib481de92007-09-25 17:54:57 -07004620 * and verify its contents
4621 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08004622static int iwl3945_verify_ucode(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004623{
4624 __le32 *image;
4625 u32 len;
4626 int rc = 0;
4627
4628 /* Try bootstrap */
4629 image = (__le32 *)priv->ucode_boot.v_addr;
4630 len = priv->ucode_boot.len;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004631 rc = iwl3945_verify_inst_sparse(priv, image, len);
Zhu Yib481de92007-09-25 17:54:57 -07004632 if (rc == 0) {
4633 IWL_DEBUG_INFO("Bootstrap uCode is good in inst SRAM\n");
4634 return 0;
4635 }
4636
4637 /* Try initialize */
4638 image = (__le32 *)priv->ucode_init.v_addr;
4639 len = priv->ucode_init.len;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004640 rc = iwl3945_verify_inst_sparse(priv, image, len);
Zhu Yib481de92007-09-25 17:54:57 -07004641 if (rc == 0) {
4642 IWL_DEBUG_INFO("Initialize uCode is good in inst SRAM\n");
4643 return 0;
4644 }
4645
4646 /* Try runtime/protocol */
4647 image = (__le32 *)priv->ucode_code.v_addr;
4648 len = priv->ucode_code.len;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004649 rc = iwl3945_verify_inst_sparse(priv, image, len);
Zhu Yib481de92007-09-25 17:54:57 -07004650 if (rc == 0) {
4651 IWL_DEBUG_INFO("Runtime uCode is good in inst SRAM\n");
4652 return 0;
4653 }
4654
Winkler, Tomas15b16872008-12-19 10:37:33 +08004655 IWL_ERR(priv, "NO VALID UCODE IMAGE IN INSTRUCTION SRAM!!\n");
Zhu Yib481de92007-09-25 17:54:57 -07004656
Ben Cahill9fbab512007-11-29 11:09:47 +08004657 /* Since nothing seems to match, show first several data entries in
4658 * instruction SRAM, so maybe visual inspection will give a clue.
4659 * Selection of bootstrap image (vs. other images) is arbitrary. */
Zhu Yib481de92007-09-25 17:54:57 -07004660 image = (__le32 *)priv->ucode_boot.v_addr;
4661 len = priv->ucode_boot.len;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004662 rc = iwl3945_verify_inst_full(priv, image, len);
Zhu Yib481de92007-09-25 17:54:57 -07004663
4664 return rc;
4665}
4666
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08004667static void iwl3945_nic_start(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004668{
4669 /* Remove all resets to allow NIC to operate */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08004670 iwl_write32(priv, CSR_RESET, 0);
Zhu Yib481de92007-09-25 17:54:57 -07004671}
4672
4673/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004674 * iwl3945_read_ucode - Read uCode images from disk file.
Zhu Yib481de92007-09-25 17:54:57 -07004675 *
4676 * Copy into buffers for card to fetch via bus-mastering
4677 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08004678static int iwl3945_read_ucode(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004679{
Kolekar, Abhijeeta78fe752008-12-19 10:37:21 +08004680 struct iwl_ucode *ucode;
Reinette Chatrea0987a82008-12-02 12:14:06 -08004681 int ret = -EINVAL, index;
Zhu Yib481de92007-09-25 17:54:57 -07004682 const struct firmware *ucode_raw;
4683 /* firmware file name contains uCode/driver compatibility version */
Reinette Chatrea0987a82008-12-02 12:14:06 -08004684 const char *name_pre = priv->cfg->fw_name_pre;
4685 const unsigned int api_max = priv->cfg->ucode_api_max;
4686 const unsigned int api_min = priv->cfg->ucode_api_min;
4687 char buf[25];
Zhu Yib481de92007-09-25 17:54:57 -07004688 u8 *src;
4689 size_t len;
Reinette Chatrea0987a82008-12-02 12:14:06 -08004690 u32 api_ver, inst_size, data_size, init_size, init_data_size, boot_size;
Zhu Yib481de92007-09-25 17:54:57 -07004691
4692 /* Ask kernel firmware_class module to get the boot firmware off disk.
4693 * request_firmware() is synchronous, file is in memory on return. */
Reinette Chatrea0987a82008-12-02 12:14:06 -08004694 for (index = api_max; index >= api_min; index--) {
4695 sprintf(buf, "%s%u%s", name_pre, index, ".ucode");
4696 ret = request_firmware(&ucode_raw, buf, &priv->pci_dev->dev);
4697 if (ret < 0) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08004698 IWL_ERR(priv, "%s firmware file req failed: %d\n",
Reinette Chatrea0987a82008-12-02 12:14:06 -08004699 buf, ret);
4700 if (ret == -ENOENT)
4701 continue;
4702 else
4703 goto error;
4704 } else {
4705 if (index < api_max)
Winkler, Tomas15b16872008-12-19 10:37:33 +08004706 IWL_ERR(priv, "Loaded firmware %s, "
4707 "which is deprecated. "
4708 " Please use API v%u instead.\n",
Reinette Chatrea0987a82008-12-02 12:14:06 -08004709 buf, api_max);
4710 IWL_DEBUG_INFO("Got firmware '%s' file (%zd bytes) from disk\n",
4711 buf, ucode_raw->size);
4712 break;
4713 }
Zhu Yib481de92007-09-25 17:54:57 -07004714 }
4715
Reinette Chatrea0987a82008-12-02 12:14:06 -08004716 if (ret < 0)
4717 goto error;
Zhu Yib481de92007-09-25 17:54:57 -07004718
4719 /* Make sure that we got at least our header! */
4720 if (ucode_raw->size < sizeof(*ucode)) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08004721 IWL_ERR(priv, "File size way too small!\n");
Tomas Winkler90e759d2007-11-29 11:09:41 +08004722 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07004723 goto err_release;
4724 }
4725
4726 /* Data from ucode file: header followed by uCode images */
4727 ucode = (void *)ucode_raw->data;
4728
Chatre, Reinettec02b3ac2008-12-02 12:14:05 -08004729 priv->ucode_ver = le32_to_cpu(ucode->ver);
Reinette Chatrea0987a82008-12-02 12:14:06 -08004730 api_ver = IWL_UCODE_API(priv->ucode_ver);
Zhu Yib481de92007-09-25 17:54:57 -07004731 inst_size = le32_to_cpu(ucode->inst_size);
4732 data_size = le32_to_cpu(ucode->data_size);
4733 init_size = le32_to_cpu(ucode->init_size);
4734 init_data_size = le32_to_cpu(ucode->init_data_size);
4735 boot_size = le32_to_cpu(ucode->boot_size);
4736
Reinette Chatrea0987a82008-12-02 12:14:06 -08004737 /* api_ver should match the api version forming part of the
4738 * firmware filename ... but we don't check for that and only rely
4739 * on the API version read from firware header from here on forward */
4740
4741 if (api_ver < api_min || api_ver > api_max) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08004742 IWL_ERR(priv, "Driver unable to support your firmware API. "
Reinette Chatrea0987a82008-12-02 12:14:06 -08004743 "Driver supports v%u, firmware is v%u.\n",
4744 api_max, api_ver);
4745 priv->ucode_ver = 0;
4746 ret = -EINVAL;
4747 goto err_release;
4748 }
4749 if (api_ver != api_max)
Winkler, Tomas15b16872008-12-19 10:37:33 +08004750 IWL_ERR(priv, "Firmware has old API version. Expected %u, "
Reinette Chatrea0987a82008-12-02 12:14:06 -08004751 "got %u. New firmware can be obtained "
4752 "from http://www.intellinuxwireless.org.\n",
4753 api_max, api_ver);
4754
Tomas Winkler978785a2008-12-19 10:37:31 +08004755 IWL_INFO(priv, "loaded firmware version %u.%u.%u.%u\n",
4756 IWL_UCODE_MAJOR(priv->ucode_ver),
4757 IWL_UCODE_MINOR(priv->ucode_ver),
4758 IWL_UCODE_API(priv->ucode_ver),
4759 IWL_UCODE_SERIAL(priv->ucode_ver));
4760
Reinette Chatrea0987a82008-12-02 12:14:06 -08004761 IWL_DEBUG_INFO("f/w package hdr ucode version raw = 0x%x\n",
4762 priv->ucode_ver);
Ian Schrambc434dd2007-10-25 17:15:29 +08004763 IWL_DEBUG_INFO("f/w package hdr runtime inst size = %u\n", inst_size);
4764 IWL_DEBUG_INFO("f/w package hdr runtime data size = %u\n", data_size);
4765 IWL_DEBUG_INFO("f/w package hdr init inst size = %u\n", init_size);
4766 IWL_DEBUG_INFO("f/w package hdr init data size = %u\n", init_data_size);
4767 IWL_DEBUG_INFO("f/w package hdr boot inst size = %u\n", boot_size);
Zhu Yib481de92007-09-25 17:54:57 -07004768
Reinette Chatrea0987a82008-12-02 12:14:06 -08004769
Zhu Yib481de92007-09-25 17:54:57 -07004770 /* Verify size of file vs. image size info in file's header */
4771 if (ucode_raw->size < sizeof(*ucode) +
4772 inst_size + data_size + init_size +
4773 init_data_size + boot_size) {
4774
4775 IWL_DEBUG_INFO("uCode file size %d too small\n",
4776 (int)ucode_raw->size);
Tomas Winkler90e759d2007-11-29 11:09:41 +08004777 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07004778 goto err_release;
4779 }
4780
4781 /* Verify that uCode images will fit in card's SRAM */
Samuel Ortiz250bdd22008-12-19 10:37:11 +08004782 if (inst_size > IWL39_MAX_INST_SIZE) {
Tomas Winkler90e759d2007-11-29 11:09:41 +08004783 IWL_DEBUG_INFO("uCode instr len %d too large to fit in\n",
4784 inst_size);
4785 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07004786 goto err_release;
4787 }
4788
Samuel Ortiz250bdd22008-12-19 10:37:11 +08004789 if (data_size > IWL39_MAX_DATA_SIZE) {
Tomas Winkler90e759d2007-11-29 11:09:41 +08004790 IWL_DEBUG_INFO("uCode data len %d too large to fit in\n",
4791 data_size);
4792 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07004793 goto err_release;
4794 }
Samuel Ortiz250bdd22008-12-19 10:37:11 +08004795 if (init_size > IWL39_MAX_INST_SIZE) {
Tomas Winkler90e759d2007-11-29 11:09:41 +08004796 IWL_DEBUG_INFO("uCode init instr len %d too large to fit in\n",
4797 init_size);
4798 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07004799 goto err_release;
4800 }
Samuel Ortiz250bdd22008-12-19 10:37:11 +08004801 if (init_data_size > IWL39_MAX_DATA_SIZE) {
Tomas Winkler90e759d2007-11-29 11:09:41 +08004802 IWL_DEBUG_INFO("uCode init data len %d too large to fit in\n",
4803 init_data_size);
4804 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07004805 goto err_release;
4806 }
Samuel Ortiz250bdd22008-12-19 10:37:11 +08004807 if (boot_size > IWL39_MAX_BSM_SIZE) {
Tomas Winkler90e759d2007-11-29 11:09:41 +08004808 IWL_DEBUG_INFO("uCode boot instr len %d too large to fit in\n",
4809 boot_size);
4810 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07004811 goto err_release;
4812 }
4813
4814 /* Allocate ucode buffers for card's bus-master loading ... */
4815
4816 /* Runtime instructions and 2 copies of data:
4817 * 1) unmodified from disk
4818 * 2) backup cache for save/restore during power-downs */
4819 priv->ucode_code.len = inst_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08004820 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_code);
Zhu Yib481de92007-09-25 17:54:57 -07004821
4822 priv->ucode_data.len = data_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08004823 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_data);
Zhu Yib481de92007-09-25 17:54:57 -07004824
4825 priv->ucode_data_backup.len = data_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08004826 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_data_backup);
Zhu Yib481de92007-09-25 17:54:57 -07004827
4828 if (!priv->ucode_code.v_addr || !priv->ucode_data.v_addr ||
Tomas Winkler90e759d2007-11-29 11:09:41 +08004829 !priv->ucode_data_backup.v_addr)
Zhu Yib481de92007-09-25 17:54:57 -07004830 goto err_pci_alloc;
4831
Tomas Winkler90e759d2007-11-29 11:09:41 +08004832 /* Initialization instructions and data */
4833 if (init_size && init_data_size) {
4834 priv->ucode_init.len = init_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08004835 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_init);
Tomas Winkler90e759d2007-11-29 11:09:41 +08004836
4837 priv->ucode_init_data.len = init_data_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08004838 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_init_data);
Tomas Winkler90e759d2007-11-29 11:09:41 +08004839
4840 if (!priv->ucode_init.v_addr || !priv->ucode_init_data.v_addr)
4841 goto err_pci_alloc;
4842 }
4843
4844 /* Bootstrap (instructions only, no data) */
4845 if (boot_size) {
4846 priv->ucode_boot.len = boot_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08004847 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_boot);
Tomas Winkler90e759d2007-11-29 11:09:41 +08004848
4849 if (!priv->ucode_boot.v_addr)
4850 goto err_pci_alloc;
4851 }
4852
Zhu Yib481de92007-09-25 17:54:57 -07004853 /* Copy images into buffers for card's bus-master reads ... */
4854
4855 /* Runtime instructions (first block of data in file) */
4856 src = &ucode->data[0];
4857 len = priv->ucode_code.len;
Tomas Winkler90e759d2007-11-29 11:09:41 +08004858 IWL_DEBUG_INFO("Copying (but not loading) uCode instr len %Zd\n", len);
Zhu Yib481de92007-09-25 17:54:57 -07004859 memcpy(priv->ucode_code.v_addr, src, len);
4860 IWL_DEBUG_INFO("uCode instr buf vaddr = 0x%p, paddr = 0x%08x\n",
4861 priv->ucode_code.v_addr, (u32)priv->ucode_code.p_addr);
4862
4863 /* Runtime data (2nd block)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004864 * NOTE: Copy into backup buffer will be done in iwl3945_up() */
Zhu Yib481de92007-09-25 17:54:57 -07004865 src = &ucode->data[inst_size];
4866 len = priv->ucode_data.len;
Tomas Winkler90e759d2007-11-29 11:09:41 +08004867 IWL_DEBUG_INFO("Copying (but not loading) uCode data len %Zd\n", len);
Zhu Yib481de92007-09-25 17:54:57 -07004868 memcpy(priv->ucode_data.v_addr, src, len);
4869 memcpy(priv->ucode_data_backup.v_addr, src, len);
4870
4871 /* Initialization instructions (3rd block) */
4872 if (init_size) {
4873 src = &ucode->data[inst_size + data_size];
4874 len = priv->ucode_init.len;
Tomas Winkler90e759d2007-11-29 11:09:41 +08004875 IWL_DEBUG_INFO("Copying (but not loading) init instr len %Zd\n",
4876 len);
Zhu Yib481de92007-09-25 17:54:57 -07004877 memcpy(priv->ucode_init.v_addr, src, len);
4878 }
4879
4880 /* Initialization data (4th block) */
4881 if (init_data_size) {
4882 src = &ucode->data[inst_size + data_size + init_size];
4883 len = priv->ucode_init_data.len;
4884 IWL_DEBUG_INFO("Copying (but not loading) init data len %d\n",
4885 (int)len);
4886 memcpy(priv->ucode_init_data.v_addr, src, len);
4887 }
4888
4889 /* Bootstrap instructions (5th block) */
4890 src = &ucode->data[inst_size + data_size + init_size + init_data_size];
4891 len = priv->ucode_boot.len;
4892 IWL_DEBUG_INFO("Copying (but not loading) boot instr len %d\n",
4893 (int)len);
4894 memcpy(priv->ucode_boot.v_addr, src, len);
4895
4896 /* We have our copies now, allow OS release its copies */
4897 release_firmware(ucode_raw);
4898 return 0;
4899
4900 err_pci_alloc:
Winkler, Tomas15b16872008-12-19 10:37:33 +08004901 IWL_ERR(priv, "failed to allocate pci memory\n");
Tomas Winkler90e759d2007-11-29 11:09:41 +08004902 ret = -ENOMEM;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004903 iwl3945_dealloc_ucode_pci(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004904
4905 err_release:
4906 release_firmware(ucode_raw);
4907
4908 error:
Tomas Winkler90e759d2007-11-29 11:09:41 +08004909 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07004910}
4911
4912
4913/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004914 * iwl3945_set_ucode_ptrs - Set uCode address location
Zhu Yib481de92007-09-25 17:54:57 -07004915 *
4916 * Tell initialization uCode where to find runtime uCode.
4917 *
4918 * BSM registers initially contain pointers to initialization uCode.
4919 * We need to replace them to load runtime uCode inst and data,
4920 * and to save runtime data when powering down.
4921 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08004922static int iwl3945_set_ucode_ptrs(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004923{
4924 dma_addr_t pinst;
4925 dma_addr_t pdata;
4926 int rc = 0;
4927 unsigned long flags;
4928
4929 /* bits 31:0 for 3945 */
4930 pinst = priv->ucode_code.p_addr;
4931 pdata = priv->ucode_data_backup.p_addr;
4932
4933 spin_lock_irqsave(&priv->lock, flags);
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08004934 rc = iwl_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004935 if (rc) {
4936 spin_unlock_irqrestore(&priv->lock, flags);
4937 return rc;
4938 }
4939
4940 /* Tell bootstrap uCode where to find image to load */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08004941 iwl_write_prph(priv, BSM_DRAM_INST_PTR_REG, pinst);
4942 iwl_write_prph(priv, BSM_DRAM_DATA_PTR_REG, pdata);
4943 iwl_write_prph(priv, BSM_DRAM_DATA_BYTECOUNT_REG,
Zhu Yib481de92007-09-25 17:54:57 -07004944 priv->ucode_data.len);
4945
Tomas Winklera96a27f2008-10-23 23:48:56 -07004946 /* Inst byte count must be last to set up, bit 31 signals uCode
Zhu Yib481de92007-09-25 17:54:57 -07004947 * that all new ptr/size info is in place */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08004948 iwl_write_prph(priv, BSM_DRAM_INST_BYTECOUNT_REG,
Zhu Yib481de92007-09-25 17:54:57 -07004949 priv->ucode_code.len | BSM_DRAM_INST_LOAD);
4950
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08004951 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004952
4953 spin_unlock_irqrestore(&priv->lock, flags);
4954
4955 IWL_DEBUG_INFO("Runtime uCode pointers are set.\n");
4956
4957 return rc;
4958}
4959
4960/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004961 * iwl3945_init_alive_start - Called after REPLY_ALIVE notification received
Zhu Yib481de92007-09-25 17:54:57 -07004962 *
4963 * Called after REPLY_ALIVE notification received from "initialize" uCode.
4964 *
Zhu Yib481de92007-09-25 17:54:57 -07004965 * Tell "initialize" uCode to go ahead and load the runtime uCode.
Ben Cahill9fbab512007-11-29 11:09:47 +08004966 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08004967static void iwl3945_init_alive_start(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004968{
4969 /* Check alive response for "valid" sign from uCode */
4970 if (priv->card_alive_init.is_valid != UCODE_VALID_OK) {
4971 /* We had an error bringing up the hardware, so take it
4972 * all the way back down so we can try again */
4973 IWL_DEBUG_INFO("Initialize Alive failed.\n");
4974 goto restart;
4975 }
4976
4977 /* Bootstrap uCode has loaded initialize uCode ... verify inst image.
4978 * This is a paranoid check, because we would not have gotten the
4979 * "initialize" alive if code weren't properly loaded. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004980 if (iwl3945_verify_ucode(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07004981 /* Runtime instruction load was bad;
4982 * take it all the way back down so we can try again */
4983 IWL_DEBUG_INFO("Bad \"initialize\" uCode load.\n");
4984 goto restart;
4985 }
4986
4987 /* Send pointers to protocol/runtime uCode image ... init code will
4988 * load and launch runtime uCode, which will send us another "Alive"
4989 * notification. */
4990 IWL_DEBUG_INFO("Initialization Alive received.\n");
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004991 if (iwl3945_set_ucode_ptrs(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07004992 /* Runtime instruction load won't happen;
4993 * take it all the way back down so we can try again */
4994 IWL_DEBUG_INFO("Couldn't set up uCode pointers.\n");
4995 goto restart;
4996 }
4997 return;
4998
4999 restart:
5000 queue_work(priv->workqueue, &priv->restart);
5001}
5002
5003
Mohamed Abbas9bdf5ec2008-11-07 09:58:35 -08005004/* temporary */
5005static int iwl3945_mac_beacon_update(struct ieee80211_hw *hw,
5006 struct sk_buff *skb);
5007
Zhu Yib481de92007-09-25 17:54:57 -07005008/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005009 * iwl3945_alive_start - called after REPLY_ALIVE notification received
Zhu Yib481de92007-09-25 17:54:57 -07005010 * from protocol/runtime uCode (initialization uCode's
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005011 * Alive gets handled by iwl3945_init_alive_start()).
Zhu Yib481de92007-09-25 17:54:57 -07005012 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08005013static void iwl3945_alive_start(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005014{
5015 int rc = 0;
5016 int thermal_spin = 0;
5017 u32 rfkill;
5018
5019 IWL_DEBUG_INFO("Runtime Alive received.\n");
5020
5021 if (priv->card_alive.is_valid != UCODE_VALID_OK) {
5022 /* We had an error bringing up the hardware, so take it
5023 * all the way back down so we can try again */
5024 IWL_DEBUG_INFO("Alive failed.\n");
5025 goto restart;
5026 }
5027
5028 /* Initialize uCode has loaded Runtime uCode ... verify inst image.
5029 * This is a paranoid check, because we would not have gotten the
5030 * "runtime" alive if code weren't properly loaded. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005031 if (iwl3945_verify_ucode(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07005032 /* Runtime instruction load was bad;
5033 * take it all the way back down so we can try again */
5034 IWL_DEBUG_INFO("Bad runtime uCode load.\n");
5035 goto restart;
5036 }
5037
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005038 iwl3945_clear_stations_table(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005039
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08005040 rc = iwl_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005041 if (rc) {
Winkler, Tomas39aadf82008-12-19 10:37:32 +08005042 IWL_WARN(priv, "Can not read RFKILL status from adapter\n");
Zhu Yib481de92007-09-25 17:54:57 -07005043 return;
5044 }
5045
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08005046 rfkill = iwl_read_prph(priv, APMG_RFKILL_REG);
Zhu Yib481de92007-09-25 17:54:57 -07005047 IWL_DEBUG_INFO("RFKILL status: 0x%x\n", rfkill);
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08005048 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005049
5050 if (rfkill & 0x1) {
5051 clear_bit(STATUS_RF_KILL_HW, &priv->status);
Tomas Winklera96a27f2008-10-23 23:48:56 -07005052 /* if RFKILL is not on, then wait for thermal
Zhu Yib481de92007-09-25 17:54:57 -07005053 * sensor in adapter to kick in */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005054 while (iwl3945_hw_get_temperature(priv) == 0) {
Zhu Yib481de92007-09-25 17:54:57 -07005055 thermal_spin++;
5056 udelay(10);
5057 }
5058
5059 if (thermal_spin)
5060 IWL_DEBUG_INFO("Thermal calibration took %dus\n",
5061 thermal_spin * 10);
5062 } else
5063 set_bit(STATUS_RF_KILL_HW, &priv->status);
5064
Ben Cahill9fbab512007-11-29 11:09:47 +08005065 /* After the ALIVE response, we can send commands to 3945 uCode */
Zhu Yib481de92007-09-25 17:54:57 -07005066 set_bit(STATUS_ALIVE, &priv->status);
5067
5068 /* Clear out the uCode error bit if it is set */
5069 clear_bit(STATUS_FW_ERROR, &priv->status);
5070
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08005071 if (iwl_is_rfkill(priv))
Zhu Yib481de92007-09-25 17:54:57 -07005072 return;
5073
Johannes Berg36d68252008-05-15 12:55:26 +02005074 ieee80211_wake_queues(priv->hw);
Zhu Yib481de92007-09-25 17:54:57 -07005075
5076 priv->active_rate = priv->rates_mask;
5077 priv->active_rate_basic = priv->rates_mask & IWL_BASIC_RATES_MASK;
5078
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005079 iwl3945_send_power_mode(priv, IWL_POWER_LEVEL(priv->power_mode));
Zhu Yib481de92007-09-25 17:54:57 -07005080
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005081 if (iwl3945_is_associated(priv)) {
5082 struct iwl3945_rxon_cmd *active_rxon =
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08005083 (struct iwl3945_rxon_cmd *)(&priv->active39_rxon);
Zhu Yib481de92007-09-25 17:54:57 -07005084
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08005085 memcpy(&priv->staging39_rxon, &priv->active39_rxon,
5086 sizeof(priv->staging39_rxon));
Zhu Yib481de92007-09-25 17:54:57 -07005087 active_rxon->filter_flags &= ~RXON_FILTER_ASSOC_MSK;
5088 } else {
5089 /* Initialize our rx_config data */
Zhu, Yi60294de2008-10-29 14:05:45 -07005090 iwl3945_connection_init_rx_config(priv, priv->iw_mode);
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08005091 memcpy(priv->staging39_rxon.node_addr, priv->mac_addr, ETH_ALEN);
Zhu Yib481de92007-09-25 17:54:57 -07005092 }
5093
Ben Cahill9fbab512007-11-29 11:09:47 +08005094 /* Configure Bluetooth device coexistence support */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005095 iwl3945_send_bt_config(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005096
5097 /* Configure the adapter for unassociated operation */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005098 iwl3945_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005099
Zhu Yib481de92007-09-25 17:54:57 -07005100 iwl3945_reg_txpower_periodic(priv);
5101
Reinette Chatrefe00b5a2008-04-03 16:05:23 -07005102 iwl3945_led_register(priv);
5103
Zhu Yib481de92007-09-25 17:54:57 -07005104 IWL_DEBUG_INFO("ALIVE processing complete.\n");
Rick Farringtona9f46782008-03-18 14:57:49 -07005105 set_bit(STATUS_READY, &priv->status);
Zhu Yi5a669262008-01-14 17:46:18 -08005106 wake_up_interruptible(&priv->wait_command_queue);
Zhu Yib481de92007-09-25 17:54:57 -07005107
5108 if (priv->error_recovering)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005109 iwl3945_error_recovery(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005110
Mohamed Abbas9bdf5ec2008-11-07 09:58:35 -08005111 /* reassociate for ADHOC mode */
5112 if (priv->vif && (priv->iw_mode == NL80211_IFTYPE_ADHOC)) {
5113 struct sk_buff *beacon = ieee80211_beacon_get(priv->hw,
5114 priv->vif);
5115 if (beacon)
5116 iwl3945_mac_beacon_update(priv->hw, beacon);
5117 }
5118
Zhu Yib481de92007-09-25 17:54:57 -07005119 return;
5120
5121 restart:
5122 queue_work(priv->workqueue, &priv->restart);
5123}
5124
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08005125static void iwl3945_cancel_deferred_work(struct iwl_priv *priv);
Zhu Yib481de92007-09-25 17:54:57 -07005126
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08005127static void __iwl3945_down(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005128{
5129 unsigned long flags;
5130 int exit_pending = test_bit(STATUS_EXIT_PENDING, &priv->status);
5131 struct ieee80211_conf *conf = NULL;
5132
5133 IWL_DEBUG_INFO(DRV_NAME " is going down\n");
5134
5135 conf = ieee80211_get_hw_conf(priv->hw);
5136
5137 if (!exit_pending)
5138 set_bit(STATUS_EXIT_PENDING, &priv->status);
5139
Mohamed Abbasab53d8a2008-03-25 16:33:36 -07005140 iwl3945_led_unregister(priv);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005141 iwl3945_clear_stations_table(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005142
5143 /* Unblock any waiting calls */
5144 wake_up_interruptible_all(&priv->wait_command_queue);
5145
Zhu Yib481de92007-09-25 17:54:57 -07005146 /* Wipe out the EXIT_PENDING status bit if we are not actually
5147 * exiting the module */
5148 if (!exit_pending)
5149 clear_bit(STATUS_EXIT_PENDING, &priv->status);
5150
5151 /* stop and reset the on-board processor */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08005152 iwl_write32(priv, CSR_RESET, CSR_RESET_REG_FLAG_NEVO_RESET);
Zhu Yib481de92007-09-25 17:54:57 -07005153
5154 /* tell the device to stop sending interrupts */
Mohamed Abbas0359fac2008-03-28 16:21:08 -07005155 spin_lock_irqsave(&priv->lock, flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005156 iwl3945_disable_interrupts(priv);
Mohamed Abbas0359fac2008-03-28 16:21:08 -07005157 spin_unlock_irqrestore(&priv->lock, flags);
5158 iwl_synchronize_irq(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005159
5160 if (priv->mac80211_registered)
5161 ieee80211_stop_queues(priv->hw);
5162
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005163 /* If we have not previously called iwl3945_init() then
Zhu Yib481de92007-09-25 17:54:57 -07005164 * clear all bits but the RF Kill and SUSPEND bits and return */
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08005165 if (!iwl_is_init(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07005166 priv->status = test_bit(STATUS_RF_KILL_HW, &priv->status) <<
5167 STATUS_RF_KILL_HW |
5168 test_bit(STATUS_RF_KILL_SW, &priv->status) <<
5169 STATUS_RF_KILL_SW |
Reinette Chatre97888642008-02-06 11:20:38 -08005170 test_bit(STATUS_GEO_CONFIGURED, &priv->status) <<
5171 STATUS_GEO_CONFIGURED |
Zhu Yib481de92007-09-25 17:54:57 -07005172 test_bit(STATUS_IN_SUSPEND, &priv->status) <<
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08005173 STATUS_IN_SUSPEND |
5174 test_bit(STATUS_EXIT_PENDING, &priv->status) <<
5175 STATUS_EXIT_PENDING;
Zhu Yib481de92007-09-25 17:54:57 -07005176 goto exit;
5177 }
5178
5179 /* ...otherwise clear out all the status bits but the RF Kill and
5180 * SUSPEND bits and continue taking the NIC down. */
5181 priv->status &= test_bit(STATUS_RF_KILL_HW, &priv->status) <<
5182 STATUS_RF_KILL_HW |
5183 test_bit(STATUS_RF_KILL_SW, &priv->status) <<
5184 STATUS_RF_KILL_SW |
Reinette Chatre97888642008-02-06 11:20:38 -08005185 test_bit(STATUS_GEO_CONFIGURED, &priv->status) <<
5186 STATUS_GEO_CONFIGURED |
Zhu Yib481de92007-09-25 17:54:57 -07005187 test_bit(STATUS_IN_SUSPEND, &priv->status) <<
5188 STATUS_IN_SUSPEND |
5189 test_bit(STATUS_FW_ERROR, &priv->status) <<
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08005190 STATUS_FW_ERROR |
5191 test_bit(STATUS_EXIT_PENDING, &priv->status) <<
5192 STATUS_EXIT_PENDING;
Zhu Yib481de92007-09-25 17:54:57 -07005193
5194 spin_lock_irqsave(&priv->lock, flags);
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08005195 iwl_clear_bit(priv, CSR_GP_CNTRL, CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
Zhu Yib481de92007-09-25 17:54:57 -07005196 spin_unlock_irqrestore(&priv->lock, flags);
5197
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005198 iwl3945_hw_txq_ctx_stop(priv);
5199 iwl3945_hw_rxq_stop(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005200
5201 spin_lock_irqsave(&priv->lock, flags);
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08005202 if (!iwl_grab_nic_access(priv)) {
5203 iwl_write_prph(priv, APMG_CLK_DIS_REG,
Zhu Yib481de92007-09-25 17:54:57 -07005204 APMG_CLK_VAL_DMA_CLK_RQT);
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08005205 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005206 }
5207 spin_unlock_irqrestore(&priv->lock, flags);
5208
5209 udelay(5);
5210
Kolekar, Abhijeet01ec6162008-12-19 10:37:38 +08005211 priv->cfg->ops->lib->apm_ops.reset(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005212 exit:
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08005213 memset(&priv->card_alive, 0, sizeof(struct iwl_alive_resp));
Zhu Yib481de92007-09-25 17:54:57 -07005214
5215 if (priv->ibss_beacon)
5216 dev_kfree_skb(priv->ibss_beacon);
5217 priv->ibss_beacon = NULL;
5218
5219 /* clear out any free frames */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005220 iwl3945_clear_free_frames(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005221}
5222
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08005223static void iwl3945_down(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005224{
5225 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005226 __iwl3945_down(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005227 mutex_unlock(&priv->mutex);
Zhu Yib24d22b2007-12-19 13:59:52 +08005228
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005229 iwl3945_cancel_deferred_work(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005230}
5231
5232#define MAX_HW_RESTARTS 5
5233
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08005234static int __iwl3945_up(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005235{
5236 int rc, i;
5237
5238 if (test_bit(STATUS_EXIT_PENDING, &priv->status)) {
Winkler, Tomas39aadf82008-12-19 10:37:32 +08005239 IWL_WARN(priv, "Exit pending; will not bring the NIC up\n");
Zhu Yib481de92007-09-25 17:54:57 -07005240 return -EIO;
5241 }
5242
5243 if (test_bit(STATUS_RF_KILL_SW, &priv->status)) {
Winkler, Tomas39aadf82008-12-19 10:37:32 +08005244 IWL_WARN(priv, "Radio disabled by SW RF kill (module "
Zhu Yib481de92007-09-25 17:54:57 -07005245 "parameter)\n");
Zhu Yie655b9f2008-01-24 02:19:38 -08005246 return -ENODEV;
5247 }
5248
Reinette Chatree903fbd2008-01-30 22:05:15 -08005249 if (!priv->ucode_data_backup.v_addr || !priv->ucode_data.v_addr) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08005250 IWL_ERR(priv, "ucode not available for device bring up\n");
Reinette Chatree903fbd2008-01-30 22:05:15 -08005251 return -EIO;
5252 }
5253
Zhu Yie655b9f2008-01-24 02:19:38 -08005254 /* If platform's RF_KILL switch is NOT set to KILL */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08005255 if (iwl_read32(priv, CSR_GP_CNTRL) &
Zhu Yie655b9f2008-01-24 02:19:38 -08005256 CSR_GP_CNTRL_REG_FLAG_HW_RF_KILL_SW)
5257 clear_bit(STATUS_RF_KILL_HW, &priv->status);
5258 else {
5259 set_bit(STATUS_RF_KILL_HW, &priv->status);
5260 if (!test_bit(STATUS_IN_SUSPEND, &priv->status)) {
Winkler, Tomas39aadf82008-12-19 10:37:32 +08005261 IWL_WARN(priv, "Radio disabled by HW RF Kill switch\n");
Zhu Yie655b9f2008-01-24 02:19:38 -08005262 return -ENODEV;
5263 }
Zhu Yib481de92007-09-25 17:54:57 -07005264 }
Adel Gadllah80fcc9e2008-07-01 17:49:50 +02005265
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08005266 iwl_write32(priv, CSR_INT, 0xFFFFFFFF);
Zhu Yib481de92007-09-25 17:54:57 -07005267
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005268 rc = iwl3945_hw_nic_init(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005269 if (rc) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08005270 IWL_ERR(priv, "Unable to int nic\n");
Zhu Yib481de92007-09-25 17:54:57 -07005271 return rc;
5272 }
5273
5274 /* make sure rfkill handshake bits are cleared */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08005275 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
5276 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR,
Zhu Yib481de92007-09-25 17:54:57 -07005277 CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
5278
5279 /* clear (again), then enable host interrupts */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08005280 iwl_write32(priv, CSR_INT, 0xFFFFFFFF);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005281 iwl3945_enable_interrupts(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005282
5283 /* really make sure rfkill handshake bits are cleared */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08005284 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
5285 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
Zhu Yib481de92007-09-25 17:54:57 -07005286
5287 /* Copy original ucode data image from disk into backup cache.
5288 * This will be used to initialize the on-board processor's
5289 * data SRAM for a clean start when the runtime program first loads. */
5290 memcpy(priv->ucode_data_backup.v_addr, priv->ucode_data.v_addr,
Zhu Yi5a669262008-01-14 17:46:18 -08005291 priv->ucode_data.len);
Zhu Yib481de92007-09-25 17:54:57 -07005292
Zhu Yie655b9f2008-01-24 02:19:38 -08005293 /* We return success when we resume from suspend and rf_kill is on. */
5294 if (test_bit(STATUS_RF_KILL_HW, &priv->status))
5295 return 0;
5296
Zhu Yib481de92007-09-25 17:54:57 -07005297 for (i = 0; i < MAX_HW_RESTARTS; i++) {
5298
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005299 iwl3945_clear_stations_table(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005300
5301 /* load bootstrap state machine,
5302 * load bootstrap program into processor's memory,
5303 * prepare to load the "initialize" uCode */
Kolekar, Abhijeet0164b9b2008-12-19 10:37:37 +08005304 priv->cfg->ops->lib->load_ucode(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005305
5306 if (rc) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08005307 IWL_ERR(priv,
5308 "Unable to set up bootstrap uCode: %d\n", rc);
Zhu Yib481de92007-09-25 17:54:57 -07005309 continue;
5310 }
5311
5312 /* start card; "initialize" will load runtime ucode */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005313 iwl3945_nic_start(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005314
Zhu Yib481de92007-09-25 17:54:57 -07005315 IWL_DEBUG_INFO(DRV_NAME " is coming up\n");
5316
5317 return 0;
5318 }
5319
5320 set_bit(STATUS_EXIT_PENDING, &priv->status);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005321 __iwl3945_down(priv);
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08005322 clear_bit(STATUS_EXIT_PENDING, &priv->status);
Zhu Yib481de92007-09-25 17:54:57 -07005323
5324 /* tried to restart and config the device for as long as our
5325 * patience could withstand */
Winkler, Tomas15b16872008-12-19 10:37:33 +08005326 IWL_ERR(priv, "Unable to initialize device after %d attempts.\n", i);
Zhu Yib481de92007-09-25 17:54:57 -07005327 return -EIO;
5328}
5329
5330
5331/*****************************************************************************
5332 *
5333 * Workqueue callbacks
5334 *
5335 *****************************************************************************/
5336
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005337static void iwl3945_bg_init_alive_start(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07005338{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08005339 struct iwl_priv *priv =
5340 container_of(data, struct iwl_priv, init_alive_start.work);
Zhu Yib481de92007-09-25 17:54:57 -07005341
5342 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
5343 return;
5344
5345 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005346 iwl3945_init_alive_start(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005347 mutex_unlock(&priv->mutex);
5348}
5349
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005350static void iwl3945_bg_alive_start(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07005351{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08005352 struct iwl_priv *priv =
5353 container_of(data, struct iwl_priv, alive_start.work);
Zhu Yib481de92007-09-25 17:54:57 -07005354
5355 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
5356 return;
5357
5358 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005359 iwl3945_alive_start(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005360 mutex_unlock(&priv->mutex);
5361}
5362
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005363static void iwl3945_bg_rf_kill(struct work_struct *work)
Zhu Yib481de92007-09-25 17:54:57 -07005364{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08005365 struct iwl_priv *priv = container_of(work, struct iwl_priv, rf_kill);
Zhu Yib481de92007-09-25 17:54:57 -07005366
5367 wake_up_interruptible(&priv->wait_command_queue);
5368
5369 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
5370 return;
5371
5372 mutex_lock(&priv->mutex);
5373
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08005374 if (!iwl_is_rfkill(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07005375 IWL_DEBUG(IWL_DL_INFO | IWL_DL_RF_KILL,
5376 "HW and/or SW RF Kill no longer active, restarting "
5377 "device\n");
Helmut Schaa26635162009-01-15 09:38:44 +01005378 if (!test_bit(STATUS_EXIT_PENDING, &priv->status) &&
5379 test_bit(STATUS_ALIVE, &priv->status))
Zhu Yib481de92007-09-25 17:54:57 -07005380 queue_work(priv->workqueue, &priv->restart);
5381 } else {
5382
5383 if (!test_bit(STATUS_RF_KILL_HW, &priv->status))
5384 IWL_DEBUG_RF_KILL("Can not turn radio back on - "
5385 "disabled by SW switch\n");
5386 else
Winkler, Tomas39aadf82008-12-19 10:37:32 +08005387 IWL_WARN(priv, "Radio Frequency Kill Switch is On:\n"
Zhu Yib481de92007-09-25 17:54:57 -07005388 "Kill switch must be turned off for "
5389 "wireless networking to work.\n");
5390 }
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08005391
Zhu Yib481de92007-09-25 17:54:57 -07005392 mutex_unlock(&priv->mutex);
Adel Gadllah80fcc9e2008-07-01 17:49:50 +02005393 iwl3945_rfkill_set_hw_state(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005394}
5395
Helmut Schaa26635162009-01-15 09:38:44 +01005396static void iwl3945_rfkill_poll(struct work_struct *data)
5397{
5398 struct iwl_priv *priv =
5399 container_of(data, struct iwl_priv, rfkill_poll.work);
5400 unsigned long status = priv->status;
5401
5402 if (iwl_read32(priv, CSR_GP_CNTRL) & CSR_GP_CNTRL_REG_FLAG_HW_RF_KILL_SW)
5403 clear_bit(STATUS_RF_KILL_HW, &priv->status);
5404 else
5405 set_bit(STATUS_RF_KILL_HW, &priv->status);
5406
5407 if (test_bit(STATUS_RF_KILL_HW, &status) != test_bit(STATUS_RF_KILL_HW, &priv->status))
5408 queue_work(priv->workqueue, &priv->rf_kill);
5409
5410 queue_delayed_work(priv->workqueue, &priv->rfkill_poll,
5411 round_jiffies_relative(2 * HZ));
5412
5413}
5414
Zhu Yib481de92007-09-25 17:54:57 -07005415#define IWL_SCAN_CHECK_WATCHDOG (7 * HZ)
5416
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005417static void iwl3945_bg_scan_check(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07005418{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08005419 struct iwl_priv *priv =
5420 container_of(data, struct iwl_priv, scan_check.work);
Zhu Yib481de92007-09-25 17:54:57 -07005421
5422 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
5423 return;
5424
5425 mutex_lock(&priv->mutex);
5426 if (test_bit(STATUS_SCANNING, &priv->status) ||
5427 test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
5428 IWL_DEBUG(IWL_DL_INFO | IWL_DL_SCAN,
5429 "Scan completion watchdog resetting adapter (%dms)\n",
5430 jiffies_to_msecs(IWL_SCAN_CHECK_WATCHDOG));
Mohamed Abbas15e869d2007-10-25 17:15:46 +08005431
Zhu Yib481de92007-09-25 17:54:57 -07005432 if (!test_bit(STATUS_EXIT_PENDING, &priv->status))
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005433 iwl3945_send_scan_abort(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005434 }
5435 mutex_unlock(&priv->mutex);
5436}
5437
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005438static void iwl3945_bg_request_scan(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07005439{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08005440 struct iwl_priv *priv =
5441 container_of(data, struct iwl_priv, request_scan);
Winkler, Tomasc2d79b42008-12-19 10:37:34 +08005442 struct iwl_host_cmd cmd = {
Zhu Yib481de92007-09-25 17:54:57 -07005443 .id = REPLY_SCAN_CMD,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005444 .len = sizeof(struct iwl3945_scan_cmd),
Zhu Yib481de92007-09-25 17:54:57 -07005445 .meta.flags = CMD_SIZE_HUGE,
5446 };
5447 int rc = 0;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005448 struct iwl3945_scan_cmd *scan;
Zhu Yib481de92007-09-25 17:54:57 -07005449 struct ieee80211_conf *conf = NULL;
Abhijeet Kolekarf9340522008-09-03 11:26:58 +08005450 u8 n_probes = 2;
Johannes Berg8318d782008-01-24 19:38:38 +01005451 enum ieee80211_band band;
John W. Linville9387b7c2008-09-30 20:59:05 -04005452 DECLARE_SSID_BUF(ssid);
Zhu Yib481de92007-09-25 17:54:57 -07005453
5454 conf = ieee80211_get_hw_conf(priv->hw);
5455
5456 mutex_lock(&priv->mutex);
5457
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08005458 if (!iwl_is_ready(priv)) {
Winkler, Tomas39aadf82008-12-19 10:37:32 +08005459 IWL_WARN(priv, "request scan called when driver not ready.\n");
Zhu Yib481de92007-09-25 17:54:57 -07005460 goto done;
5461 }
5462
Tomas Winklera96a27f2008-10-23 23:48:56 -07005463 /* Make sure the scan wasn't canceled before this queued work
Zhu Yib481de92007-09-25 17:54:57 -07005464 * was given the chance to run... */
5465 if (!test_bit(STATUS_SCANNING, &priv->status))
5466 goto done;
5467
5468 /* This should never be called or scheduled if there is currently
5469 * a scan active in the hardware. */
5470 if (test_bit(STATUS_SCAN_HW, &priv->status)) {
5471 IWL_DEBUG_INFO("Multiple concurrent scan requests in parallel. "
5472 "Ignoring second request.\n");
5473 rc = -EIO;
5474 goto done;
5475 }
5476
5477 if (test_bit(STATUS_EXIT_PENDING, &priv->status)) {
5478 IWL_DEBUG_SCAN("Aborting scan due to device shutdown\n");
5479 goto done;
5480 }
5481
5482 if (test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
5483 IWL_DEBUG_HC("Scan request while abort pending. Queuing.\n");
5484 goto done;
5485 }
5486
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08005487 if (iwl_is_rfkill(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07005488 IWL_DEBUG_HC("Aborting scan due to RF Kill activation\n");
5489 goto done;
5490 }
5491
5492 if (!test_bit(STATUS_READY, &priv->status)) {
5493 IWL_DEBUG_HC("Scan request while uninitialized. Queuing.\n");
5494 goto done;
5495 }
5496
5497 if (!priv->scan_bands) {
5498 IWL_DEBUG_HC("Aborting scan due to no requested bands\n");
5499 goto done;
5500 }
5501
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08005502 if (!priv->scan39) {
5503 priv->scan39 = kmalloc(sizeof(struct iwl3945_scan_cmd) +
Zhu Yib481de92007-09-25 17:54:57 -07005504 IWL_MAX_SCAN_SIZE, GFP_KERNEL);
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08005505 if (!priv->scan39) {
Zhu Yib481de92007-09-25 17:54:57 -07005506 rc = -ENOMEM;
5507 goto done;
5508 }
5509 }
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08005510 scan = priv->scan39;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005511 memset(scan, 0, sizeof(struct iwl3945_scan_cmd) + IWL_MAX_SCAN_SIZE);
Zhu Yib481de92007-09-25 17:54:57 -07005512
5513 scan->quiet_plcp_th = IWL_PLCP_QUIET_THRESH;
5514 scan->quiet_time = IWL_ACTIVE_QUIET_TIME;
5515
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005516 if (iwl3945_is_associated(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07005517 u16 interval = 0;
5518 u32 extra;
5519 u32 suspend_time = 100;
5520 u32 scan_suspend_time = 100;
5521 unsigned long flags;
5522
5523 IWL_DEBUG_INFO("Scanning while associated...\n");
5524
5525 spin_lock_irqsave(&priv->lock, flags);
5526 interval = priv->beacon_int;
5527 spin_unlock_irqrestore(&priv->lock, flags);
5528
5529 scan->suspend_time = 0;
Mohamed Abbas15e869d2007-10-25 17:15:46 +08005530 scan->max_out_time = cpu_to_le32(200 * 1024);
Zhu Yib481de92007-09-25 17:54:57 -07005531 if (!interval)
5532 interval = suspend_time;
5533 /*
5534 * suspend time format:
5535 * 0-19: beacon interval in usec (time before exec.)
5536 * 20-23: 0
5537 * 24-31: number of beacons (suspend between channels)
5538 */
5539
5540 extra = (suspend_time / interval) << 24;
5541 scan_suspend_time = 0xFF0FFFFF &
5542 (extra | ((suspend_time % interval) * 1024));
5543
5544 scan->suspend_time = cpu_to_le32(scan_suspend_time);
5545 IWL_DEBUG_SCAN("suspend_time 0x%X beacon interval %d\n",
5546 scan_suspend_time, interval);
5547 }
5548
5549 /* We should add the ability for user to lock to PASSIVE ONLY */
5550 if (priv->one_direct_scan) {
5551 IWL_DEBUG_SCAN
5552 ("Kicking off one direct scan for '%s'\n",
John W. Linville9387b7c2008-09-30 20:59:05 -04005553 print_ssid(ssid, priv->direct_ssid,
5554 priv->direct_ssid_len));
Zhu Yib481de92007-09-25 17:54:57 -07005555 scan->direct_scan[0].id = WLAN_EID_SSID;
5556 scan->direct_scan[0].len = priv->direct_ssid_len;
5557 memcpy(scan->direct_scan[0].ssid,
5558 priv->direct_ssid, priv->direct_ssid_len);
Abhijeet Kolekarf9340522008-09-03 11:26:58 +08005559 n_probes++;
Abhijeet Kolekarf9340522008-09-03 11:26:58 +08005560 } else
Bill Moss786b4552008-04-17 16:03:40 -07005561 IWL_DEBUG_SCAN("Kicking off one indirect scan.\n");
Zhu Yib481de92007-09-25 17:54:57 -07005562
5563 /* We don't build a direct scan probe request; the uCode will do
5564 * that based on the direct_mask added to each channel entry */
5565 scan->tx_cmd.len = cpu_to_le16(
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005566 iwl3945_fill_probe_req(priv, (struct ieee80211_mgmt *)scan->data,
Johannes Berg430cfe92008-10-28 18:06:02 +01005567 IWL_MAX_SCAN_SIZE - sizeof(*scan)));
Zhu Yib481de92007-09-25 17:54:57 -07005568 scan->tx_cmd.tx_flags = TX_CMD_FLG_SEQ_CTL_MSK;
Abhijeet Kolekar3832ec92008-12-19 10:37:26 +08005569 scan->tx_cmd.sta_id = priv->hw_params.bcast_sta_id;
Zhu Yib481de92007-09-25 17:54:57 -07005570 scan->tx_cmd.stop_time.life_time = TX_CMD_LIFE_TIME_INFINITE;
5571
5572 /* flags + rate selection */
5573
Ron Rindjunsky66b50042008-06-25 16:46:31 +08005574 if (priv->scan_bands & BIT(IEEE80211_BAND_2GHZ)) {
Zhu Yib481de92007-09-25 17:54:57 -07005575 scan->flags = RXON_FLG_BAND_24G_MSK | RXON_FLG_AUTO_DETECT_MSK;
5576 scan->tx_cmd.rate = IWL_RATE_1M_PLCP;
5577 scan->good_CRC_th = 0;
Johannes Berg8318d782008-01-24 19:38:38 +01005578 band = IEEE80211_BAND_2GHZ;
Ron Rindjunsky66b50042008-06-25 16:46:31 +08005579 } else if (priv->scan_bands & BIT(IEEE80211_BAND_5GHZ)) {
Zhu Yib481de92007-09-25 17:54:57 -07005580 scan->tx_cmd.rate = IWL_RATE_6M_PLCP;
5581 scan->good_CRC_th = IWL_GOOD_CRC_TH;
Johannes Berg8318d782008-01-24 19:38:38 +01005582 band = IEEE80211_BAND_5GHZ;
Ron Rindjunsky66b50042008-06-25 16:46:31 +08005583 } else {
Winkler, Tomas39aadf82008-12-19 10:37:32 +08005584 IWL_WARN(priv, "Invalid scan band count\n");
Zhu Yib481de92007-09-25 17:54:57 -07005585 goto done;
5586 }
5587
5588 /* select Rx antennas */
5589 scan->flags |= iwl3945_get_antenna_flags(priv);
5590
Johannes Berg05c914f2008-09-11 00:01:58 +02005591 if (priv->iw_mode == NL80211_IFTYPE_MONITOR)
Zhu Yib481de92007-09-25 17:54:57 -07005592 scan->filter_flags = RXON_FILTER_PROMISC_MSK;
5593
Abhijeet Kolekarf9340522008-09-03 11:26:58 +08005594 scan->channel_count =
5595 iwl3945_get_channels_for_scan(priv, band, 1, /* active */
5596 n_probes,
5597 (void *)&scan->data[le16_to_cpu(scan->tx_cmd.len)]);
Zhu Yib481de92007-09-25 17:54:57 -07005598
Reinette Chatre14b54332008-11-04 12:21:35 -08005599 if (scan->channel_count == 0) {
5600 IWL_DEBUG_SCAN("channel count %d\n", scan->channel_count);
5601 goto done;
5602 }
5603
Zhu Yib481de92007-09-25 17:54:57 -07005604 cmd.len += le16_to_cpu(scan->tx_cmd.len) +
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005605 scan->channel_count * sizeof(struct iwl3945_scan_channel);
Zhu Yib481de92007-09-25 17:54:57 -07005606 cmd.data = scan;
5607 scan->len = cpu_to_le16(cmd.len);
5608
5609 set_bit(STATUS_SCAN_HW, &priv->status);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005610 rc = iwl3945_send_cmd_sync(priv, &cmd);
Zhu Yib481de92007-09-25 17:54:57 -07005611 if (rc)
5612 goto done;
5613
5614 queue_delayed_work(priv->workqueue, &priv->scan_check,
5615 IWL_SCAN_CHECK_WATCHDOG);
5616
5617 mutex_unlock(&priv->mutex);
5618 return;
5619
5620 done:
Mohamed Abbas2420ebc2008-11-04 12:21:34 -08005621 /* can not perform scan make sure we clear scanning
5622 * bits from status so next scan request can be performed.
5623 * if we dont clear scanning status bit here all next scan
5624 * will fail
5625 */
5626 clear_bit(STATUS_SCAN_HW, &priv->status);
5627 clear_bit(STATUS_SCANNING, &priv->status);
5628
Ian Schram01ebd062007-10-25 17:15:22 +08005629 /* inform mac80211 scan aborted */
Zhu Yib481de92007-09-25 17:54:57 -07005630 queue_work(priv->workqueue, &priv->scan_completed);
5631 mutex_unlock(&priv->mutex);
5632}
5633
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005634static void iwl3945_bg_up(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07005635{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08005636 struct iwl_priv *priv = container_of(data, struct iwl_priv, up);
Zhu Yib481de92007-09-25 17:54:57 -07005637
5638 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
5639 return;
5640
5641 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005642 __iwl3945_up(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005643 mutex_unlock(&priv->mutex);
Adel Gadllah80fcc9e2008-07-01 17:49:50 +02005644 iwl3945_rfkill_set_hw_state(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005645}
5646
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005647static void iwl3945_bg_restart(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07005648{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08005649 struct iwl_priv *priv = container_of(data, struct iwl_priv, restart);
Zhu Yib481de92007-09-25 17:54:57 -07005650
5651 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
5652 return;
5653
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005654 iwl3945_down(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005655 queue_work(priv->workqueue, &priv->up);
5656}
5657
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005658static void iwl3945_bg_rx_replenish(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07005659{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08005660 struct iwl_priv *priv =
5661 container_of(data, struct iwl_priv, rx_replenish);
Zhu Yib481de92007-09-25 17:54:57 -07005662
5663 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
5664 return;
5665
5666 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005667 iwl3945_rx_replenish(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005668 mutex_unlock(&priv->mutex);
5669}
5670
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08005671#define IWL_DELAY_NEXT_SCAN (HZ*2)
5672
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08005673static void iwl3945_post_associate(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005674{
Zhu Yib481de92007-09-25 17:54:57 -07005675 int rc = 0;
5676 struct ieee80211_conf *conf = NULL;
5677
Johannes Berg05c914f2008-09-11 00:01:58 +02005678 if (priv->iw_mode == NL80211_IFTYPE_AP) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08005679 IWL_ERR(priv, "%s Should not be called in AP mode\n", __func__);
Zhu Yib481de92007-09-25 17:54:57 -07005680 return;
5681 }
5682
5683
Johannes Berge1749612008-10-27 15:59:26 -07005684 IWL_DEBUG_ASSOC("Associated as %d to: %pM\n",
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08005685 priv->assoc_id, priv->active39_rxon.bssid_addr);
Zhu Yib481de92007-09-25 17:54:57 -07005686
5687 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
5688 return;
5689
Abhijeet Kolekar322a9812008-09-03 11:26:27 +08005690 if (!priv->vif || !priv->is_open)
Mohamed Abbas6ef89d02007-10-25 17:15:47 +08005691 return;
Abhijeet Kolekar322a9812008-09-03 11:26:27 +08005692
Winkler, Tomasaf0053d2009-01-19 15:30:23 -08005693 iwl_scan_cancel_timeout(priv, 200);
Mohamed Abbas15e869d2007-10-25 17:15:46 +08005694
Zhu Yib481de92007-09-25 17:54:57 -07005695 conf = ieee80211_get_hw_conf(priv->hw);
5696
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08005697 priv->staging39_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005698 iwl3945_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005699
Tomas Winkler28afaf92008-12-19 10:37:06 +08005700 memset(&priv->rxon_timing, 0, sizeof(struct iwl_rxon_time_cmd));
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005701 iwl3945_setup_rxon_timing(priv);
5702 rc = iwl3945_send_cmd_pdu(priv, REPLY_RXON_TIMING,
Zhu Yib481de92007-09-25 17:54:57 -07005703 sizeof(priv->rxon_timing), &priv->rxon_timing);
5704 if (rc)
Winkler, Tomas39aadf82008-12-19 10:37:32 +08005705 IWL_WARN(priv, "REPLY_RXON_TIMING failed - "
Zhu Yib481de92007-09-25 17:54:57 -07005706 "Attempting to continue.\n");
5707
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08005708 priv->staging39_rxon.filter_flags |= RXON_FILTER_ASSOC_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07005709
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08005710 priv->staging39_rxon.assoc_id = cpu_to_le16(priv->assoc_id);
Zhu Yib481de92007-09-25 17:54:57 -07005711
5712 IWL_DEBUG_ASSOC("assoc id %d beacon interval %d\n",
5713 priv->assoc_id, priv->beacon_int);
5714
5715 if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_PREAMBLE)
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08005716 priv->staging39_rxon.flags |= RXON_FLG_SHORT_PREAMBLE_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07005717 else
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08005718 priv->staging39_rxon.flags &= ~RXON_FLG_SHORT_PREAMBLE_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07005719
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08005720 if (priv->staging39_rxon.flags & RXON_FLG_BAND_24G_MSK) {
Zhu Yib481de92007-09-25 17:54:57 -07005721 if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_SLOT_TIME)
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08005722 priv->staging39_rxon.flags |= RXON_FLG_SHORT_SLOT_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07005723 else
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08005724 priv->staging39_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07005725
Johannes Berg05c914f2008-09-11 00:01:58 +02005726 if (priv->iw_mode == NL80211_IFTYPE_ADHOC)
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08005727 priv->staging39_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07005728
5729 }
5730
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005731 iwl3945_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005732
5733 switch (priv->iw_mode) {
Johannes Berg05c914f2008-09-11 00:01:58 +02005734 case NL80211_IFTYPE_STATION:
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005735 iwl3945_rate_scale_init(priv->hw, IWL_AP_ID);
Zhu Yib481de92007-09-25 17:54:57 -07005736 break;
5737
Johannes Berg05c914f2008-09-11 00:01:58 +02005738 case NL80211_IFTYPE_ADHOC:
Zhu Yib481de92007-09-25 17:54:57 -07005739
Abhijeet Kolekarce546fd2008-11-19 15:32:22 -08005740 priv->assoc_id = 1;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005741 iwl3945_add_station(priv, priv->bssid, 0, 0);
Zhu Yib481de92007-09-25 17:54:57 -07005742 iwl3945_sync_sta(priv, IWL_STA_ID,
Johannes Berg8318d782008-01-24 19:38:38 +01005743 (priv->band == IEEE80211_BAND_5GHZ) ?
Zhu Yib481de92007-09-25 17:54:57 -07005744 IWL_RATE_6M_PLCP : IWL_RATE_1M_PLCP,
5745 CMD_ASYNC);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005746 iwl3945_rate_scale_init(priv->hw, IWL_STA_ID);
5747 iwl3945_send_beacon_cmd(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005748
5749 break;
5750
5751 default:
Winkler, Tomas15b16872008-12-19 10:37:33 +08005752 IWL_ERR(priv, "%s Should not be called in %d mode\n",
Tomas Winkler3ac7f142008-07-21 02:40:14 +03005753 __func__, priv->iw_mode);
Zhu Yib481de92007-09-25 17:54:57 -07005754 break;
5755 }
5756
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005757 iwl3945_activate_qos(priv, 0);
Ron Rindjunsky292ae172008-02-06 11:20:39 -08005758
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08005759 /* we have just associated, don't start scan too early */
5760 priv->next_scan_jiffies = jiffies + IWL_DELAY_NEXT_SCAN;
Abhijeet Kolekarcd56d332008-09-03 11:26:21 +08005761}
5762
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005763static void iwl3945_bg_abort_scan(struct work_struct *work)
Zhu Yib481de92007-09-25 17:54:57 -07005764{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08005765 struct iwl_priv *priv = container_of(work, struct iwl_priv, abort_scan);
Zhu Yib481de92007-09-25 17:54:57 -07005766
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08005767 if (!iwl_is_ready(priv))
Zhu Yib481de92007-09-25 17:54:57 -07005768 return;
5769
5770 mutex_lock(&priv->mutex);
5771
5772 set_bit(STATUS_SCAN_ABORTING, &priv->status);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005773 iwl3945_send_scan_abort(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005774
5775 mutex_unlock(&priv->mutex);
5776}
5777
Johannes Berge8975582008-10-09 12:18:51 +02005778static int iwl3945_mac_config(struct ieee80211_hw *hw, u32 changed);
Zhu Yi76bb77e2007-11-22 10:53:22 +08005779
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005780static void iwl3945_bg_scan_completed(struct work_struct *work)
Zhu Yib481de92007-09-25 17:54:57 -07005781{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08005782 struct iwl_priv *priv =
5783 container_of(work, struct iwl_priv, scan_completed);
Zhu Yib481de92007-09-25 17:54:57 -07005784
5785 IWL_DEBUG(IWL_DL_INFO | IWL_DL_SCAN, "SCAN complete scan\n");
5786
5787 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
5788 return;
5789
Zhu Yia0646472007-12-20 14:10:01 +08005790 if (test_bit(STATUS_CONF_PENDING, &priv->status))
Johannes Berge8975582008-10-09 12:18:51 +02005791 iwl3945_mac_config(priv->hw, 0);
Zhu Yi76bb77e2007-11-22 10:53:22 +08005792
Zhu Yib481de92007-09-25 17:54:57 -07005793 ieee80211_scan_completed(priv->hw);
5794
5795 /* Since setting the TXPOWER may have been deferred while
5796 * performing the scan, fire one off */
5797 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005798 iwl3945_hw_reg_send_txpower(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005799 mutex_unlock(&priv->mutex);
5800}
5801
5802/*****************************************************************************
5803 *
5804 * mac80211 entry point functions
5805 *
5806 *****************************************************************************/
5807
Zhu Yi5a669262008-01-14 17:46:18 -08005808#define UCODE_READY_TIMEOUT (2 * HZ)
5809
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005810static int iwl3945_mac_start(struct ieee80211_hw *hw)
Zhu Yib481de92007-09-25 17:54:57 -07005811{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08005812 struct iwl_priv *priv = hw->priv;
Zhu Yi5a669262008-01-14 17:46:18 -08005813 int ret;
Zhu Yib481de92007-09-25 17:54:57 -07005814
5815 IWL_DEBUG_MAC80211("enter\n");
5816
5817 /* we should be verifying the device is ready to be opened */
5818 mutex_lock(&priv->mutex);
5819
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08005820 memset(&priv->staging39_rxon, 0, sizeof(struct iwl3945_rxon_cmd));
Zhu Yi5a669262008-01-14 17:46:18 -08005821 /* fetch ucode file from disk, alloc and copy to bus-master buffers ...
5822 * ucode filename and max sizes are card-specific. */
5823
5824 if (!priv->ucode_code.len) {
5825 ret = iwl3945_read_ucode(priv);
5826 if (ret) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08005827 IWL_ERR(priv, "Could not read microcode: %d\n", ret);
Zhu Yi5a669262008-01-14 17:46:18 -08005828 mutex_unlock(&priv->mutex);
5829 goto out_release_irq;
5830 }
5831 }
5832
Zhu Yie655b9f2008-01-24 02:19:38 -08005833 ret = __iwl3945_up(priv);
Zhu Yi5a669262008-01-14 17:46:18 -08005834
Zhu Yib481de92007-09-25 17:54:57 -07005835 mutex_unlock(&priv->mutex);
Zhu Yi5a669262008-01-14 17:46:18 -08005836
Adel Gadllah80fcc9e2008-07-01 17:49:50 +02005837 iwl3945_rfkill_set_hw_state(priv);
5838
Zhu Yie655b9f2008-01-24 02:19:38 -08005839 if (ret)
5840 goto out_release_irq;
5841
5842 IWL_DEBUG_INFO("Start UP work.\n");
5843
5844 if (test_bit(STATUS_IN_SUSPEND, &priv->status))
5845 return 0;
5846
Zhu Yi5a669262008-01-14 17:46:18 -08005847 /* Wait for START_ALIVE from ucode. Otherwise callbacks from
5848 * mac80211 will not be run successfully. */
5849 ret = wait_event_interruptible_timeout(priv->wait_command_queue,
5850 test_bit(STATUS_READY, &priv->status),
5851 UCODE_READY_TIMEOUT);
5852 if (!ret) {
5853 if (!test_bit(STATUS_READY, &priv->status)) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08005854 IWL_ERR(priv,
5855 "Wait for START_ALIVE timeout after %dms.\n",
5856 jiffies_to_msecs(UCODE_READY_TIMEOUT));
Zhu Yi5a669262008-01-14 17:46:18 -08005857 ret = -ETIMEDOUT;
5858 goto out_release_irq;
5859 }
5860 }
5861
Helmut Schaa26635162009-01-15 09:38:44 +01005862 /* ucode is running and will send rfkill notifications,
5863 * no need to poll the killswitch state anymore */
5864 cancel_delayed_work(&priv->rfkill_poll);
5865
Zhu Yie655b9f2008-01-24 02:19:38 -08005866 priv->is_open = 1;
Zhu Yib481de92007-09-25 17:54:57 -07005867 IWL_DEBUG_MAC80211("leave\n");
5868 return 0;
Zhu Yi5a669262008-01-14 17:46:18 -08005869
5870out_release_irq:
Zhu Yie655b9f2008-01-24 02:19:38 -08005871 priv->is_open = 0;
5872 IWL_DEBUG_MAC80211("leave - failed\n");
Zhu Yi5a669262008-01-14 17:46:18 -08005873 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07005874}
5875
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005876static void iwl3945_mac_stop(struct ieee80211_hw *hw)
Zhu Yib481de92007-09-25 17:54:57 -07005877{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08005878 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07005879
5880 IWL_DEBUG_MAC80211("enter\n");
Mohamed Abbas6ef89d02007-10-25 17:15:47 +08005881
Zhu Yie655b9f2008-01-24 02:19:38 -08005882 if (!priv->is_open) {
5883 IWL_DEBUG_MAC80211("leave - skip\n");
5884 return;
5885 }
5886
Zhu Yib481de92007-09-25 17:54:57 -07005887 priv->is_open = 0;
Zhu Yi5a669262008-01-14 17:46:18 -08005888
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08005889 if (iwl_is_ready_rf(priv)) {
Zhu Yie655b9f2008-01-24 02:19:38 -08005890 /* stop mac, cancel any scan request and clear
5891 * RXON_FILTER_ASSOC_MSK BIT
5892 */
Zhu Yi5a669262008-01-14 17:46:18 -08005893 mutex_lock(&priv->mutex);
Winkler, Tomasaf0053d2009-01-19 15:30:23 -08005894 iwl_scan_cancel_timeout(priv, 100);
Mohamed Abbasfde35712007-11-29 11:10:15 +08005895 mutex_unlock(&priv->mutex);
Mohamed Abbasfde35712007-11-29 11:10:15 +08005896 }
5897
Zhu Yi5a669262008-01-14 17:46:18 -08005898 iwl3945_down(priv);
5899
5900 flush_workqueue(priv->workqueue);
Helmut Schaa26635162009-01-15 09:38:44 +01005901
5902 /* start polling the killswitch state again */
5903 queue_delayed_work(priv->workqueue, &priv->rfkill_poll,
5904 round_jiffies_relative(2 * HZ));
Mohamed Abbas6ef89d02007-10-25 17:15:47 +08005905
Zhu Yib481de92007-09-25 17:54:57 -07005906 IWL_DEBUG_MAC80211("leave\n");
Zhu Yib481de92007-09-25 17:54:57 -07005907}
5908
Johannes Berge039fa42008-05-15 12:55:29 +02005909static int iwl3945_mac_tx(struct ieee80211_hw *hw, struct sk_buff *skb)
Zhu Yib481de92007-09-25 17:54:57 -07005910{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08005911 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07005912
5913 IWL_DEBUG_MAC80211("enter\n");
5914
Zhu Yib481de92007-09-25 17:54:57 -07005915 IWL_DEBUG_TX("dev->xmit(%d bytes) at rate 0x%02x\n", skb->len,
Johannes Berge039fa42008-05-15 12:55:29 +02005916 ieee80211_get_tx_rate(hw, IEEE80211_SKB_CB(skb))->bitrate);
Zhu Yib481de92007-09-25 17:54:57 -07005917
Johannes Berge039fa42008-05-15 12:55:29 +02005918 if (iwl3945_tx_skb(priv, skb))
Zhu Yib481de92007-09-25 17:54:57 -07005919 dev_kfree_skb_any(skb);
5920
5921 IWL_DEBUG_MAC80211("leave\n");
Reinette Chatre637f8832009-01-19 15:30:32 -08005922 return NETDEV_TX_OK;
Zhu Yib481de92007-09-25 17:54:57 -07005923}
5924
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005925static int iwl3945_mac_add_interface(struct ieee80211_hw *hw,
Zhu Yib481de92007-09-25 17:54:57 -07005926 struct ieee80211_if_init_conf *conf)
5927{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08005928 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07005929 unsigned long flags;
5930
Johannes Berg32bfd352007-12-19 01:31:26 +01005931 IWL_DEBUG_MAC80211("enter: type %d\n", conf->type);
Zhu Yib481de92007-09-25 17:54:57 -07005932
Johannes Berg32bfd352007-12-19 01:31:26 +01005933 if (priv->vif) {
5934 IWL_DEBUG_MAC80211("leave - vif != NULL\n");
Tomas Winkler864792e2007-11-27 21:00:52 +02005935 return -EOPNOTSUPP;
Zhu Yib481de92007-09-25 17:54:57 -07005936 }
5937
5938 spin_lock_irqsave(&priv->lock, flags);
Johannes Berg32bfd352007-12-19 01:31:26 +01005939 priv->vif = conf->vif;
Zhu, Yi60294de2008-10-29 14:05:45 -07005940 priv->iw_mode = conf->type;
Zhu Yib481de92007-09-25 17:54:57 -07005941
5942 spin_unlock_irqrestore(&priv->lock, flags);
5943
5944 mutex_lock(&priv->mutex);
Tomas Winkler864792e2007-11-27 21:00:52 +02005945
5946 if (conf->mac_addr) {
Johannes Berge1749612008-10-27 15:59:26 -07005947 IWL_DEBUG_MAC80211("Set: %pM\n", conf->mac_addr);
Tomas Winkler864792e2007-11-27 21:00:52 +02005948 memcpy(priv->mac_addr, conf->mac_addr, ETH_ALEN);
5949 }
5950
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08005951 if (iwl_is_ready(priv))
Zhu Yi5a669262008-01-14 17:46:18 -08005952 iwl3945_set_mode(priv, conf->type);
Zhu Yib481de92007-09-25 17:54:57 -07005953
Zhu Yib481de92007-09-25 17:54:57 -07005954 mutex_unlock(&priv->mutex);
5955
Zhu Yi5a669262008-01-14 17:46:18 -08005956 IWL_DEBUG_MAC80211("leave\n");
Zhu Yib481de92007-09-25 17:54:57 -07005957 return 0;
5958}
5959
5960/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005961 * iwl3945_mac_config - mac80211 config callback
Zhu Yib481de92007-09-25 17:54:57 -07005962 *
5963 * We ignore conf->flags & IEEE80211_CONF_SHORT_SLOT_TIME since it seems to
5964 * be set inappropriately and the driver currently sets the hardware up to
5965 * use it whenever needed.
5966 */
Johannes Berge8975582008-10-09 12:18:51 +02005967static int iwl3945_mac_config(struct ieee80211_hw *hw, u32 changed)
Zhu Yib481de92007-09-25 17:54:57 -07005968{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08005969 struct iwl_priv *priv = hw->priv;
Samuel Ortizd20b3c62008-12-19 10:37:15 +08005970 const struct iwl_channel_info *ch_info;
Johannes Berge8975582008-10-09 12:18:51 +02005971 struct ieee80211_conf *conf = &hw->conf;
Zhu Yib481de92007-09-25 17:54:57 -07005972 unsigned long flags;
Zhu Yi76bb77e2007-11-22 10:53:22 +08005973 int ret = 0;
Zhu Yib481de92007-09-25 17:54:57 -07005974
5975 mutex_lock(&priv->mutex);
Johannes Berg8318d782008-01-24 19:38:38 +01005976 IWL_DEBUG_MAC80211("enter to channel %d\n", conf->channel->hw_value);
Zhu Yib481de92007-09-25 17:54:57 -07005977
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08005978 if (!iwl_is_ready(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07005979 IWL_DEBUG_MAC80211("leave - not ready\n");
Zhu Yi76bb77e2007-11-22 10:53:22 +08005980 ret = -EIO;
5981 goto out;
Zhu Yib481de92007-09-25 17:54:57 -07005982 }
5983
Kolekar, Abhijeetdf878d82008-12-19 10:37:35 +08005984 if (unlikely(!iwl3945_mod_params.disable_hw_scan &&
Zhu Yib481de92007-09-25 17:54:57 -07005985 test_bit(STATUS_SCANNING, &priv->status))) {
Zhu Yia0646472007-12-20 14:10:01 +08005986 IWL_DEBUG_MAC80211("leave - scanning\n");
5987 set_bit(STATUS_CONF_PENDING, &priv->status);
Zhu Yib481de92007-09-25 17:54:57 -07005988 mutex_unlock(&priv->mutex);
Zhu Yia0646472007-12-20 14:10:01 +08005989 return 0;
Zhu Yib481de92007-09-25 17:54:57 -07005990 }
5991
5992 spin_lock_irqsave(&priv->lock, flags);
5993
Johannes Berg8318d782008-01-24 19:38:38 +01005994 ch_info = iwl3945_get_channel_info(priv, conf->channel->band,
5995 conf->channel->hw_value);
Zhu Yib481de92007-09-25 17:54:57 -07005996 if (!is_channel_valid(ch_info)) {
Ron Rindjunsky66b50042008-06-25 16:46:31 +08005997 IWL_DEBUG_SCAN("Channel %d [%d] is INVALID for this band.\n",
Johannes Berg8318d782008-01-24 19:38:38 +01005998 conf->channel->hw_value, conf->channel->band);
Zhu Yib481de92007-09-25 17:54:57 -07005999 IWL_DEBUG_MAC80211("leave - invalid channel\n");
6000 spin_unlock_irqrestore(&priv->lock, flags);
Zhu Yi76bb77e2007-11-22 10:53:22 +08006001 ret = -EINVAL;
6002 goto out;
Zhu Yib481de92007-09-25 17:54:57 -07006003 }
6004
Johannes Berg8318d782008-01-24 19:38:38 +01006005 iwl3945_set_rxon_channel(priv, conf->channel->band, conf->channel->hw_value);
Zhu Yib481de92007-09-25 17:54:57 -07006006
Johannes Berg8318d782008-01-24 19:38:38 +01006007 iwl3945_set_flags_for_phymode(priv, conf->channel->band);
Zhu Yib481de92007-09-25 17:54:57 -07006008
6009 /* The list of supported rates and rate mask can be different
6010 * for each phymode; since the phymode may have changed, reset
6011 * the rate mask to what mac80211 lists */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006012 iwl3945_set_rate(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006013
6014 spin_unlock_irqrestore(&priv->lock, flags);
6015
6016#ifdef IEEE80211_CONF_CHANNEL_SWITCH
6017 if (conf->flags & IEEE80211_CONF_CHANNEL_SWITCH) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006018 iwl3945_hw_channel_switch(priv, conf->channel);
Zhu Yi76bb77e2007-11-22 10:53:22 +08006019 goto out;
Zhu Yib481de92007-09-25 17:54:57 -07006020 }
6021#endif
6022
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006023 iwl3945_radio_kill_sw(priv, !conf->radio_enabled);
Zhu Yib481de92007-09-25 17:54:57 -07006024
6025 if (!conf->radio_enabled) {
6026 IWL_DEBUG_MAC80211("leave - radio disabled\n");
Zhu Yi76bb77e2007-11-22 10:53:22 +08006027 goto out;
Zhu Yib481de92007-09-25 17:54:57 -07006028 }
6029
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08006030 if (iwl_is_rfkill(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07006031 IWL_DEBUG_MAC80211("leave - RF kill\n");
Zhu Yi76bb77e2007-11-22 10:53:22 +08006032 ret = -EIO;
6033 goto out;
Zhu Yib481de92007-09-25 17:54:57 -07006034 }
6035
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006036 iwl3945_set_rate(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006037
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08006038 if (memcmp(&priv->active39_rxon,
6039 &priv->staging39_rxon, sizeof(priv->staging39_rxon)))
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006040 iwl3945_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006041 else
6042 IWL_DEBUG_INFO("No re-sending same RXON configuration.\n");
6043
6044 IWL_DEBUG_MAC80211("leave\n");
6045
Zhu Yi76bb77e2007-11-22 10:53:22 +08006046out:
Zhu Yia0646472007-12-20 14:10:01 +08006047 clear_bit(STATUS_CONF_PENDING, &priv->status);
Zhu Yib481de92007-09-25 17:54:57 -07006048 mutex_unlock(&priv->mutex);
Zhu Yi76bb77e2007-11-22 10:53:22 +08006049 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07006050}
6051
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08006052static void iwl3945_config_ap(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07006053{
6054 int rc = 0;
6055
Maarten Lankhorstd986bcd2008-01-23 10:15:16 -08006056 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
Zhu Yib481de92007-09-25 17:54:57 -07006057 return;
6058
6059 /* The following should be done only at AP bring up */
Ron Rindjunsky5d1e2322008-06-30 17:23:04 +08006060 if (!(iwl3945_is_associated(priv))) {
Zhu Yib481de92007-09-25 17:54:57 -07006061
6062 /* RXON - unassoc (to set timing command) */
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08006063 priv->staging39_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006064 iwl3945_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006065
6066 /* RXON Timing */
Tomas Winkler28afaf92008-12-19 10:37:06 +08006067 memset(&priv->rxon_timing, 0, sizeof(struct iwl_rxon_time_cmd));
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006068 iwl3945_setup_rxon_timing(priv);
6069 rc = iwl3945_send_cmd_pdu(priv, REPLY_RXON_TIMING,
Zhu Yib481de92007-09-25 17:54:57 -07006070 sizeof(priv->rxon_timing), &priv->rxon_timing);
6071 if (rc)
Winkler, Tomas39aadf82008-12-19 10:37:32 +08006072 IWL_WARN(priv, "REPLY_RXON_TIMING failed - "
Zhu Yib481de92007-09-25 17:54:57 -07006073 "Attempting to continue.\n");
6074
6075 /* FIXME: what should be the assoc_id for AP? */
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08006076 priv->staging39_rxon.assoc_id = cpu_to_le16(priv->assoc_id);
Zhu Yib481de92007-09-25 17:54:57 -07006077 if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_PREAMBLE)
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08006078 priv->staging39_rxon.flags |=
Zhu Yib481de92007-09-25 17:54:57 -07006079 RXON_FLG_SHORT_PREAMBLE_MSK;
6080 else
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08006081 priv->staging39_rxon.flags &=
Zhu Yib481de92007-09-25 17:54:57 -07006082 ~RXON_FLG_SHORT_PREAMBLE_MSK;
6083
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08006084 if (priv->staging39_rxon.flags & RXON_FLG_BAND_24G_MSK) {
Zhu Yib481de92007-09-25 17:54:57 -07006085 if (priv->assoc_capability &
6086 WLAN_CAPABILITY_SHORT_SLOT_TIME)
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08006087 priv->staging39_rxon.flags |=
Zhu Yib481de92007-09-25 17:54:57 -07006088 RXON_FLG_SHORT_SLOT_MSK;
6089 else
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08006090 priv->staging39_rxon.flags &=
Zhu Yib481de92007-09-25 17:54:57 -07006091 ~RXON_FLG_SHORT_SLOT_MSK;
6092
Johannes Berg05c914f2008-09-11 00:01:58 +02006093 if (priv->iw_mode == NL80211_IFTYPE_ADHOC)
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08006094 priv->staging39_rxon.flags &=
Zhu Yib481de92007-09-25 17:54:57 -07006095 ~RXON_FLG_SHORT_SLOT_MSK;
6096 }
6097 /* restore RXON assoc */
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08006098 priv->staging39_rxon.filter_flags |= RXON_FILTER_ASSOC_MSK;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006099 iwl3945_commit_rxon(priv);
Kolekar, Abhijeetb5323d32008-12-19 10:37:22 +08006100 iwl3945_add_station(priv, iwl_bcast_addr, 0, 0);
Zhu Yi556f8db2007-09-27 11:27:33 +08006101 }
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006102 iwl3945_send_beacon_cmd(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006103
6104 /* FIXME - we need to add code here to detect a totally new
6105 * configuration, reset the AP, unassoc, rxon timing, assoc,
6106 * clear sta table, add BCAST sta... */
6107}
6108
Johannes Berg32bfd352007-12-19 01:31:26 +01006109static int iwl3945_mac_config_interface(struct ieee80211_hw *hw,
6110 struct ieee80211_vif *vif,
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08006111 struct ieee80211_if_conf *conf)
Zhu Yib481de92007-09-25 17:54:57 -07006112{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08006113 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07006114 int rc;
6115
6116 if (conf == NULL)
6117 return -EIO;
6118
Emmanuel Grumbachb716bb92008-03-04 18:09:32 -08006119 if (priv->vif != vif) {
6120 IWL_DEBUG_MAC80211("leave - priv->vif != vif\n");
Emmanuel Grumbachb716bb92008-03-04 18:09:32 -08006121 return 0;
6122 }
6123
Johannes Berg9d139c82008-07-09 14:40:37 +02006124 /* handle this temporarily here */
Johannes Berg05c914f2008-09-11 00:01:58 +02006125 if (priv->iw_mode == NL80211_IFTYPE_ADHOC &&
Johannes Berg9d139c82008-07-09 14:40:37 +02006126 conf->changed & IEEE80211_IFCC_BEACON) {
6127 struct sk_buff *beacon = ieee80211_beacon_get(hw, vif);
6128 if (!beacon)
6129 return -ENOMEM;
Mohamed Abbas9bdf5ec2008-11-07 09:58:35 -08006130 mutex_lock(&priv->mutex);
Johannes Berg9d139c82008-07-09 14:40:37 +02006131 rc = iwl3945_mac_beacon_update(hw, beacon);
Mohamed Abbas9bdf5ec2008-11-07 09:58:35 -08006132 mutex_unlock(&priv->mutex);
Johannes Berg9d139c82008-07-09 14:40:37 +02006133 if (rc)
6134 return rc;
6135 }
6136
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08006137 if (!iwl_is_alive(priv))
Zhu Yi5a669262008-01-14 17:46:18 -08006138 return -EAGAIN;
6139
Zhu Yib481de92007-09-25 17:54:57 -07006140 mutex_lock(&priv->mutex);
6141
Zhu Yib481de92007-09-25 17:54:57 -07006142 if (conf->bssid)
Johannes Berge1749612008-10-27 15:59:26 -07006143 IWL_DEBUG_MAC80211("bssid: %pM\n", conf->bssid);
Zhu Yib481de92007-09-25 17:54:57 -07006144
Johannes Berg4150c572007-09-17 01:29:23 -04006145/*
6146 * very dubious code was here; the probe filtering flag is never set:
6147 *
Zhu Yib481de92007-09-25 17:54:57 -07006148 if (unlikely(test_bit(STATUS_SCANNING, &priv->status)) &&
6149 !(priv->hw->flags & IEEE80211_HW_NO_PROBE_FILTERING)) {
Johannes Berg4150c572007-09-17 01:29:23 -04006150 */
Zhu Yib481de92007-09-25 17:54:57 -07006151
Johannes Berg05c914f2008-09-11 00:01:58 +02006152 if (priv->iw_mode == NL80211_IFTYPE_AP) {
Zhu Yib481de92007-09-25 17:54:57 -07006153 if (!conf->bssid) {
6154 conf->bssid = priv->mac_addr;
6155 memcpy(priv->bssid, priv->mac_addr, ETH_ALEN);
Johannes Berge1749612008-10-27 15:59:26 -07006156 IWL_DEBUG_MAC80211("bssid was set to: %pM\n",
6157 conf->bssid);
Zhu Yib481de92007-09-25 17:54:57 -07006158 }
6159 if (priv->ibss_beacon)
6160 dev_kfree_skb(priv->ibss_beacon);
6161
Johannes Berg9d139c82008-07-09 14:40:37 +02006162 priv->ibss_beacon = ieee80211_beacon_get(hw, vif);
Zhu Yib481de92007-09-25 17:54:57 -07006163 }
6164
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08006165 if (iwl_is_rfkill(priv))
Mohamed Abbasfde35712007-11-29 11:10:15 +08006166 goto done;
6167
Zhu Yib481de92007-09-25 17:54:57 -07006168 if (conf->bssid && !is_zero_ether_addr(conf->bssid) &&
6169 !is_multicast_ether_addr(conf->bssid)) {
6170 /* If there is currently a HW scan going on in the background
6171 * then we need to cancel it else the RXON below will fail. */
Winkler, Tomasaf0053d2009-01-19 15:30:23 -08006172 if (iwl_scan_cancel_timeout(priv, 100)) {
Winkler, Tomas39aadf82008-12-19 10:37:32 +08006173 IWL_WARN(priv, "Aborted scan still in progress "
Zhu Yib481de92007-09-25 17:54:57 -07006174 "after 100ms\n");
6175 IWL_DEBUG_MAC80211("leaving - scan abort failed.\n");
6176 mutex_unlock(&priv->mutex);
6177 return -EAGAIN;
6178 }
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08006179 memcpy(priv->staging39_rxon.bssid_addr, conf->bssid, ETH_ALEN);
Zhu Yib481de92007-09-25 17:54:57 -07006180
6181 /* TODO: Audit driver for usage of these members and see
6182 * if mac80211 deprecates them (priv->bssid looks like it
6183 * shouldn't be there, but I haven't scanned the IBSS code
6184 * to verify) - jpk */
6185 memcpy(priv->bssid, conf->bssid, ETH_ALEN);
6186
Johannes Berg05c914f2008-09-11 00:01:58 +02006187 if (priv->iw_mode == NL80211_IFTYPE_AP)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006188 iwl3945_config_ap(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006189 else {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006190 rc = iwl3945_commit_rxon(priv);
Johannes Berg05c914f2008-09-11 00:01:58 +02006191 if ((priv->iw_mode == NL80211_IFTYPE_STATION) && rc)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006192 iwl3945_add_station(priv,
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08006193 priv->active39_rxon.bssid_addr, 1, 0);
Zhu Yib481de92007-09-25 17:54:57 -07006194 }
6195
6196 } else {
Winkler, Tomasaf0053d2009-01-19 15:30:23 -08006197 iwl_scan_cancel_timeout(priv, 100);
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08006198 priv->staging39_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006199 iwl3945_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006200 }
6201
Mohamed Abbasfde35712007-11-29 11:10:15 +08006202 done:
Zhu Yib481de92007-09-25 17:54:57 -07006203 IWL_DEBUG_MAC80211("leave\n");
6204 mutex_unlock(&priv->mutex);
6205
6206 return 0;
6207}
6208
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006209static void iwl3945_configure_filter(struct ieee80211_hw *hw,
Johannes Berg4150c572007-09-17 01:29:23 -04006210 unsigned int changed_flags,
6211 unsigned int *total_flags,
6212 int mc_count, struct dev_addr_list *mc_list)
6213{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08006214 struct iwl_priv *priv = hw->priv;
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08006215 __le32 *filter_flags = &priv->staging39_rxon.filter_flags;
Rick Farrington25b3f572008-06-30 17:23:28 +08006216
Zhu, Yi352bc8d2008-11-12 13:14:09 -08006217 IWL_DEBUG_MAC80211("Enter: changed: 0x%x, total: 0x%x\n",
6218 changed_flags, *total_flags);
6219
6220 if (changed_flags & (FIF_OTHER_BSS | FIF_PROMISC_IN_BSS)) {
6221 if (*total_flags & (FIF_OTHER_BSS | FIF_PROMISC_IN_BSS))
6222 *filter_flags |= RXON_FILTER_PROMISC_MSK;
6223 else
6224 *filter_flags &= ~RXON_FILTER_PROMISC_MSK;
Abhijeet Kolekar5ec03972008-05-05 10:22:48 +08006225 }
Zhu, Yi352bc8d2008-11-12 13:14:09 -08006226 if (changed_flags & FIF_ALLMULTI) {
6227 if (*total_flags & FIF_ALLMULTI)
6228 *filter_flags |= RXON_FILTER_ACCEPT_GRP_MSK;
6229 else
6230 *filter_flags &= ~RXON_FILTER_ACCEPT_GRP_MSK;
6231 }
6232 if (changed_flags & FIF_CONTROL) {
6233 if (*total_flags & FIF_CONTROL)
6234 *filter_flags |= RXON_FILTER_CTL2HOST_MSK;
6235 else
6236 *filter_flags &= ~RXON_FILTER_CTL2HOST_MSK;
6237 }
6238 if (changed_flags & FIF_BCN_PRBRESP_PROMISC) {
6239 if (*total_flags & FIF_BCN_PRBRESP_PROMISC)
6240 *filter_flags |= RXON_FILTER_BCON_AWARE_MSK;
6241 else
6242 *filter_flags &= ~RXON_FILTER_BCON_AWARE_MSK;
6243 }
6244
6245 /* We avoid iwl_commit_rxon here to commit the new filter flags
6246 * since mac80211 will call ieee80211_hw_config immediately.
6247 * (mc_list is not supported at this time). Otherwise, we need to
6248 * queue a background iwl_commit_rxon work.
6249 */
6250
6251 *total_flags &= FIF_OTHER_BSS | FIF_ALLMULTI | FIF_PROMISC_IN_BSS |
Rick Farrington25b3f572008-06-30 17:23:28 +08006252 FIF_BCN_PRBRESP_PROMISC | FIF_CONTROL;
Johannes Berg4150c572007-09-17 01:29:23 -04006253}
6254
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006255static void iwl3945_mac_remove_interface(struct ieee80211_hw *hw,
Zhu Yib481de92007-09-25 17:54:57 -07006256 struct ieee80211_if_init_conf *conf)
6257{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08006258 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07006259
6260 IWL_DEBUG_MAC80211("enter\n");
6261
6262 mutex_lock(&priv->mutex);
Mohamed Abbas6ef89d02007-10-25 17:15:47 +08006263
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08006264 if (iwl_is_ready_rf(priv)) {
Winkler, Tomasaf0053d2009-01-19 15:30:23 -08006265 iwl_scan_cancel_timeout(priv, 100);
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08006266 priv->staging39_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
Mohamed Abbasfde35712007-11-29 11:10:15 +08006267 iwl3945_commit_rxon(priv);
6268 }
Johannes Berg32bfd352007-12-19 01:31:26 +01006269 if (priv->vif == conf->vif) {
6270 priv->vif = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07006271 memset(priv->bssid, 0, ETH_ALEN);
Zhu Yib481de92007-09-25 17:54:57 -07006272 }
6273 mutex_unlock(&priv->mutex);
6274
6275 IWL_DEBUG_MAC80211("leave\n");
Zhu Yib481de92007-09-25 17:54:57 -07006276}
6277
Abhijeet Kolekarcd56d332008-09-03 11:26:21 +08006278#define IWL_DELAY_NEXT_SCAN_AFTER_ASSOC (HZ*6)
6279
6280static void iwl3945_bss_info_changed(struct ieee80211_hw *hw,
6281 struct ieee80211_vif *vif,
6282 struct ieee80211_bss_conf *bss_conf,
6283 u32 changes)
6284{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08006285 struct iwl_priv *priv = hw->priv;
Abhijeet Kolekarcd56d332008-09-03 11:26:21 +08006286
6287 IWL_DEBUG_MAC80211("changes = 0x%X\n", changes);
6288
6289 if (changes & BSS_CHANGED_ERP_PREAMBLE) {
6290 IWL_DEBUG_MAC80211("ERP_PREAMBLE %d\n",
6291 bss_conf->use_short_preamble);
6292 if (bss_conf->use_short_preamble)
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08006293 priv->staging39_rxon.flags |= RXON_FLG_SHORT_PREAMBLE_MSK;
Abhijeet Kolekarcd56d332008-09-03 11:26:21 +08006294 else
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08006295 priv->staging39_rxon.flags &= ~RXON_FLG_SHORT_PREAMBLE_MSK;
Abhijeet Kolekarcd56d332008-09-03 11:26:21 +08006296 }
6297
6298 if (changes & BSS_CHANGED_ERP_CTS_PROT) {
6299 IWL_DEBUG_MAC80211("ERP_CTS %d\n", bss_conf->use_cts_prot);
6300 if (bss_conf->use_cts_prot && (priv->band != IEEE80211_BAND_5GHZ))
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08006301 priv->staging39_rxon.flags |= RXON_FLG_TGG_PROTECT_MSK;
Abhijeet Kolekarcd56d332008-09-03 11:26:21 +08006302 else
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08006303 priv->staging39_rxon.flags &= ~RXON_FLG_TGG_PROTECT_MSK;
Abhijeet Kolekarcd56d332008-09-03 11:26:21 +08006304 }
6305
6306 if (changes & BSS_CHANGED_ASSOC) {
6307 IWL_DEBUG_MAC80211("ASSOC %d\n", bss_conf->assoc);
6308 /* This should never happen as this function should
6309 * never be called from interrupt context. */
6310 if (WARN_ON_ONCE(in_interrupt()))
6311 return;
6312 if (bss_conf->assoc) {
6313 priv->assoc_id = bss_conf->aid;
6314 priv->beacon_int = bss_conf->beacon_int;
Tomas Winkler28afaf92008-12-19 10:37:06 +08006315 priv->timestamp = bss_conf->timestamp;
Abhijeet Kolekarcd56d332008-09-03 11:26:21 +08006316 priv->assoc_capability = bss_conf->assoc_capability;
6317 priv->next_scan_jiffies = jiffies +
6318 IWL_DELAY_NEXT_SCAN_AFTER_ASSOC;
6319 mutex_lock(&priv->mutex);
6320 iwl3945_post_associate(priv);
6321 mutex_unlock(&priv->mutex);
6322 } else {
6323 priv->assoc_id = 0;
6324 IWL_DEBUG_MAC80211("DISASSOC %d\n", bss_conf->assoc);
6325 }
6326 } else if (changes && iwl3945_is_associated(priv) && priv->assoc_id) {
6327 IWL_DEBUG_MAC80211("Associated Changes %d\n", changes);
6328 iwl3945_send_rxon_assoc(priv);
6329 }
6330
6331}
6332
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006333static int iwl3945_mac_hw_scan(struct ieee80211_hw *hw, u8 *ssid, size_t len)
Zhu Yib481de92007-09-25 17:54:57 -07006334{
6335 int rc = 0;
6336 unsigned long flags;
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08006337 struct iwl_priv *priv = hw->priv;
John W. Linville9387b7c2008-09-30 20:59:05 -04006338 DECLARE_SSID_BUF(ssid_buf);
Zhu Yib481de92007-09-25 17:54:57 -07006339
6340 IWL_DEBUG_MAC80211("enter\n");
6341
Mohamed Abbas15e869d2007-10-25 17:15:46 +08006342 mutex_lock(&priv->mutex);
Zhu Yib481de92007-09-25 17:54:57 -07006343 spin_lock_irqsave(&priv->lock, flags);
6344
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08006345 if (!iwl_is_ready_rf(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07006346 rc = -EIO;
6347 IWL_DEBUG_MAC80211("leave - not ready or exit pending\n");
6348 goto out_unlock;
6349 }
6350
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08006351 /* we don't schedule scan within next_scan_jiffies period */
6352 if (priv->next_scan_jiffies &&
6353 time_after(priv->next_scan_jiffies, jiffies)) {
6354 rc = -EAGAIN;
6355 goto out_unlock;
6356 }
Bill Moss15dbf1b2008-05-06 11:05:15 +08006357 /* if we just finished scan ask for delay for a broadcast scan */
6358 if ((len == 0) && priv->last_scan_jiffies &&
6359 time_after(priv->last_scan_jiffies + IWL_DELAY_NEXT_SCAN,
6360 jiffies)) {
Zhu Yib481de92007-09-25 17:54:57 -07006361 rc = -EAGAIN;
6362 goto out_unlock;
6363 }
6364 if (len) {
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08006365 IWL_DEBUG_SCAN("direct scan for %s [%d]\n ",
John W. Linville9387b7c2008-09-30 20:59:05 -04006366 print_ssid(ssid_buf, ssid, len), (int)len);
Zhu Yib481de92007-09-25 17:54:57 -07006367
6368 priv->one_direct_scan = 1;
6369 priv->direct_ssid_len = (u8)
6370 min((u8) len, (u8) IW_ESSID_MAX_SIZE);
6371 memcpy(priv->direct_ssid, ssid, priv->direct_ssid_len);
Mohamed Abbas6ef89d02007-10-25 17:15:47 +08006372 } else
6373 priv->one_direct_scan = 0;
Zhu Yib481de92007-09-25 17:54:57 -07006374
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006375 rc = iwl3945_scan_initiate(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006376
6377 IWL_DEBUG_MAC80211("leave\n");
6378
6379out_unlock:
6380 spin_unlock_irqrestore(&priv->lock, flags);
Mohamed Abbas15e869d2007-10-25 17:15:46 +08006381 mutex_unlock(&priv->mutex);
Zhu Yib481de92007-09-25 17:54:57 -07006382
6383 return rc;
6384}
6385
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006386static int iwl3945_mac_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
Johannes Bergdc822b52008-12-29 12:55:09 +01006387 struct ieee80211_vif *vif,
6388 struct ieee80211_sta *sta,
6389 struct ieee80211_key_conf *key)
Zhu Yib481de92007-09-25 17:54:57 -07006390{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08006391 struct iwl_priv *priv = hw->priv;
Johannes Bergdc822b52008-12-29 12:55:09 +01006392 const u8 *addr;
Winkler, Tomas42986792009-01-19 15:30:22 -08006393 int ret;
Zhu Yib481de92007-09-25 17:54:57 -07006394 u8 sta_id;
6395
6396 IWL_DEBUG_MAC80211("enter\n");
6397
Kolekar, Abhijeetdf878d82008-12-19 10:37:35 +08006398 if (iwl3945_mod_params.sw_crypto) {
Zhu Yib481de92007-09-25 17:54:57 -07006399 IWL_DEBUG_MAC80211("leave - hwcrypto disabled\n");
6400 return -EOPNOTSUPP;
6401 }
6402
Winkler, Tomas42986792009-01-19 15:30:22 -08006403 addr = sta ? sta->addr : iwl_bcast_addr;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006404 sta_id = iwl3945_hw_find_station(priv, addr);
Zhu Yib481de92007-09-25 17:54:57 -07006405 if (sta_id == IWL_INVALID_STATION) {
Johannes Berge1749612008-10-27 15:59:26 -07006406 IWL_DEBUG_MAC80211("leave - %pM not in station map.\n",
6407 addr);
Zhu Yib481de92007-09-25 17:54:57 -07006408 return -EINVAL;
6409 }
6410
6411 mutex_lock(&priv->mutex);
6412
Winkler, Tomasaf0053d2009-01-19 15:30:23 -08006413 iwl_scan_cancel_timeout(priv, 100);
Mohamed Abbas15e869d2007-10-25 17:15:46 +08006414
Zhu Yib481de92007-09-25 17:54:57 -07006415 switch (cmd) {
6416 case SET_KEY:
Winkler, Tomas42986792009-01-19 15:30:22 -08006417 ret = iwl3945_update_sta_key_info(priv, key, sta_id);
6418 if (!ret) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006419 iwl3945_set_rxon_hwcrypto(priv, 1);
6420 iwl3945_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006421 key->hw_key_idx = sta_id;
6422 IWL_DEBUG_MAC80211("set_key success, using hwcrypto\n");
6423 key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
6424 }
6425 break;
6426 case DISABLE_KEY:
Winkler, Tomas42986792009-01-19 15:30:22 -08006427 ret = iwl3945_clear_sta_key_info(priv, sta_id);
6428 if (!ret) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006429 iwl3945_set_rxon_hwcrypto(priv, 0);
6430 iwl3945_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006431 IWL_DEBUG_MAC80211("disable hwcrypto key\n");
6432 }
6433 break;
6434 default:
Winkler, Tomas42986792009-01-19 15:30:22 -08006435 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07006436 }
6437
6438 IWL_DEBUG_MAC80211("leave\n");
6439 mutex_unlock(&priv->mutex);
6440
Winkler, Tomas42986792009-01-19 15:30:22 -08006441 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07006442}
6443
Johannes Berge100bb62008-04-30 18:51:21 +02006444static int iwl3945_mac_conf_tx(struct ieee80211_hw *hw, u16 queue,
Zhu Yib481de92007-09-25 17:54:57 -07006445 const struct ieee80211_tx_queue_params *params)
6446{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08006447 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07006448 unsigned long flags;
6449 int q;
Zhu Yib481de92007-09-25 17:54:57 -07006450
6451 IWL_DEBUG_MAC80211("enter\n");
6452
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08006453 if (!iwl_is_ready_rf(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07006454 IWL_DEBUG_MAC80211("leave - RF not ready\n");
6455 return -EIO;
6456 }
6457
6458 if (queue >= AC_NUM) {
6459 IWL_DEBUG_MAC80211("leave - queue >= AC_NUM %d\n", queue);
6460 return 0;
6461 }
6462
Zhu Yib481de92007-09-25 17:54:57 -07006463 q = AC_NUM - 1 - queue;
6464
6465 spin_lock_irqsave(&priv->lock, flags);
6466
6467 priv->qos_data.def_qos_parm.ac[q].cw_min = cpu_to_le16(params->cw_min);
6468 priv->qos_data.def_qos_parm.ac[q].cw_max = cpu_to_le16(params->cw_max);
6469 priv->qos_data.def_qos_parm.ac[q].aifsn = params->aifs;
6470 priv->qos_data.def_qos_parm.ac[q].edca_txop =
Johannes Berg3330d7b2008-02-10 16:49:38 +01006471 cpu_to_le16((params->txop * 32));
Zhu Yib481de92007-09-25 17:54:57 -07006472
6473 priv->qos_data.def_qos_parm.ac[q].reserved1 = 0;
6474 priv->qos_data.qos_active = 1;
6475
6476 spin_unlock_irqrestore(&priv->lock, flags);
6477
6478 mutex_lock(&priv->mutex);
Johannes Berg05c914f2008-09-11 00:01:58 +02006479 if (priv->iw_mode == NL80211_IFTYPE_AP)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006480 iwl3945_activate_qos(priv, 1);
6481 else if (priv->assoc_id && iwl3945_is_associated(priv))
6482 iwl3945_activate_qos(priv, 0);
Zhu Yib481de92007-09-25 17:54:57 -07006483
6484 mutex_unlock(&priv->mutex);
6485
Zhu Yib481de92007-09-25 17:54:57 -07006486 IWL_DEBUG_MAC80211("leave\n");
6487 return 0;
6488}
6489
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006490static int iwl3945_mac_get_tx_stats(struct ieee80211_hw *hw,
Zhu Yib481de92007-09-25 17:54:57 -07006491 struct ieee80211_tx_queue_stats *stats)
6492{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08006493 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07006494 int i, avail;
Samuel Ortiz188cf6c2008-12-22 11:31:16 +08006495 struct iwl_tx_queue *txq;
Samuel Ortizd20b3c62008-12-19 10:37:15 +08006496 struct iwl_queue *q;
Zhu Yib481de92007-09-25 17:54:57 -07006497 unsigned long flags;
6498
6499 IWL_DEBUG_MAC80211("enter\n");
6500
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08006501 if (!iwl_is_ready_rf(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07006502 IWL_DEBUG_MAC80211("leave - RF not ready\n");
6503 return -EIO;
6504 }
6505
6506 spin_lock_irqsave(&priv->lock, flags);
6507
6508 for (i = 0; i < AC_NUM; i++) {
Samuel Ortiz188cf6c2008-12-22 11:31:16 +08006509 txq = &priv->txq[i];
Zhu Yib481de92007-09-25 17:54:57 -07006510 q = &txq->q;
Samuel Ortizd20b3c62008-12-19 10:37:15 +08006511 avail = iwl_queue_space(q);
Zhu Yib481de92007-09-25 17:54:57 -07006512
Johannes Berg57ffc582008-04-29 17:18:59 +02006513 stats[i].len = q->n_window - avail;
6514 stats[i].limit = q->n_window - q->high_mark;
6515 stats[i].count = q->n_window;
Zhu Yib481de92007-09-25 17:54:57 -07006516
6517 }
6518 spin_unlock_irqrestore(&priv->lock, flags);
6519
6520 IWL_DEBUG_MAC80211("leave\n");
6521
6522 return 0;
6523}
6524
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006525static void iwl3945_mac_reset_tsf(struct ieee80211_hw *hw)
Zhu Yib481de92007-09-25 17:54:57 -07006526{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08006527 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07006528 unsigned long flags;
6529
6530 mutex_lock(&priv->mutex);
6531 IWL_DEBUG_MAC80211("enter\n");
6532
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08006533 iwl_reset_qos(priv);
Ron Rindjunsky292ae172008-02-06 11:20:39 -08006534
Zhu Yib481de92007-09-25 17:54:57 -07006535 spin_lock_irqsave(&priv->lock, flags);
6536 priv->assoc_id = 0;
6537 priv->assoc_capability = 0;
6538 priv->call_post_assoc_from_beacon = 0;
6539
6540 /* new association get rid of ibss beacon skb */
6541 if (priv->ibss_beacon)
6542 dev_kfree_skb(priv->ibss_beacon);
6543
6544 priv->ibss_beacon = NULL;
6545
6546 priv->beacon_int = priv->hw->conf.beacon_int;
Tomas Winkler28afaf92008-12-19 10:37:06 +08006547 priv->timestamp = 0;
Johannes Berg05c914f2008-09-11 00:01:58 +02006548 if ((priv->iw_mode == NL80211_IFTYPE_STATION))
Zhu Yib481de92007-09-25 17:54:57 -07006549 priv->beacon_int = 0;
6550
6551 spin_unlock_irqrestore(&priv->lock, flags);
6552
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08006553 if (!iwl_is_ready_rf(priv)) {
Mohamed Abbasfde35712007-11-29 11:10:15 +08006554 IWL_DEBUG_MAC80211("leave - not ready\n");
6555 mutex_unlock(&priv->mutex);
6556 return;
6557 }
6558
Mohamed Abbas15e869d2007-10-25 17:15:46 +08006559 /* we are restarting association process
6560 * clear RXON_FILTER_ASSOC_MSK bit
6561 */
Johannes Berg05c914f2008-09-11 00:01:58 +02006562 if (priv->iw_mode != NL80211_IFTYPE_AP) {
Winkler, Tomasaf0053d2009-01-19 15:30:23 -08006563 iwl_scan_cancel_timeout(priv, 100);
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08006564 priv->staging39_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006565 iwl3945_commit_rxon(priv);
Mohamed Abbas15e869d2007-10-25 17:15:46 +08006566 }
6567
Zhu Yib481de92007-09-25 17:54:57 -07006568 /* Per mac80211.h: This is only used in IBSS mode... */
Johannes Berg05c914f2008-09-11 00:01:58 +02006569 if (priv->iw_mode != NL80211_IFTYPE_ADHOC) {
Mohamed Abbas15e869d2007-10-25 17:15:46 +08006570
Zhu Yib481de92007-09-25 17:54:57 -07006571 IWL_DEBUG_MAC80211("leave - not in IBSS\n");
6572 mutex_unlock(&priv->mutex);
6573 return;
6574 }
6575
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006576 iwl3945_set_rate(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006577
6578 mutex_unlock(&priv->mutex);
6579
6580 IWL_DEBUG_MAC80211("leave\n");
6581
6582}
6583
Johannes Berge039fa42008-05-15 12:55:29 +02006584static int iwl3945_mac_beacon_update(struct ieee80211_hw *hw, struct sk_buff *skb)
Zhu Yib481de92007-09-25 17:54:57 -07006585{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08006586 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07006587 unsigned long flags;
6588
Zhu Yib481de92007-09-25 17:54:57 -07006589 IWL_DEBUG_MAC80211("enter\n");
6590
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08006591 if (!iwl_is_ready_rf(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07006592 IWL_DEBUG_MAC80211("leave - RF not ready\n");
Zhu Yib481de92007-09-25 17:54:57 -07006593 return -EIO;
6594 }
6595
Johannes Berg05c914f2008-09-11 00:01:58 +02006596 if (priv->iw_mode != NL80211_IFTYPE_ADHOC) {
Zhu Yib481de92007-09-25 17:54:57 -07006597 IWL_DEBUG_MAC80211("leave - not IBSS\n");
Zhu Yib481de92007-09-25 17:54:57 -07006598 return -EIO;
6599 }
6600
6601 spin_lock_irqsave(&priv->lock, flags);
6602
6603 if (priv->ibss_beacon)
6604 dev_kfree_skb(priv->ibss_beacon);
6605
6606 priv->ibss_beacon = skb;
6607
6608 priv->assoc_id = 0;
6609
6610 IWL_DEBUG_MAC80211("leave\n");
6611 spin_unlock_irqrestore(&priv->lock, flags);
6612
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08006613 iwl_reset_qos(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006614
Abhijeet Kolekardc4b1e72008-09-03 11:26:30 +08006615 iwl3945_post_associate(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006616
Zhu Yib481de92007-09-25 17:54:57 -07006617
6618 return 0;
6619}
6620
6621/*****************************************************************************
6622 *
6623 * sysfs attributes
6624 *
6625 *****************************************************************************/
6626
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08006627#ifdef CONFIG_IWL3945_DEBUG
Zhu Yib481de92007-09-25 17:54:57 -07006628
6629/*
6630 * The following adds a new attribute to the sysfs representation
6631 * of this device driver (i.e. a new file in /sys/bus/pci/drivers/iwl/)
6632 * used for controlling the debug level.
6633 *
6634 * See the level definitions in iwl for details.
6635 */
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08006636static ssize_t show_debug_level(struct device *d,
6637 struct device_attribute *attr, char *buf)
Zhu Yib481de92007-09-25 17:54:57 -07006638{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08006639 struct iwl_priv *priv = d->driver_data;
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08006640
6641 return sprintf(buf, "0x%08X\n", priv->debug_level);
Zhu Yib481de92007-09-25 17:54:57 -07006642}
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08006643static ssize_t store_debug_level(struct device *d,
6644 struct device_attribute *attr,
Zhu Yib481de92007-09-25 17:54:57 -07006645 const char *buf, size_t count)
6646{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08006647 struct iwl_priv *priv = d->driver_data;
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08006648 unsigned long val;
6649 int ret;
Zhu Yib481de92007-09-25 17:54:57 -07006650
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08006651 ret = strict_strtoul(buf, 0, &val);
6652 if (ret)
Tomas Winkler978785a2008-12-19 10:37:31 +08006653 IWL_INFO(priv, "%s is not in hex or decimal form.\n", buf);
Zhu Yib481de92007-09-25 17:54:57 -07006654 else
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08006655 priv->debug_level = val;
Zhu Yib481de92007-09-25 17:54:57 -07006656
6657 return strnlen(buf, count);
6658}
6659
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08006660static DEVICE_ATTR(debug_level, S_IWUSR | S_IRUGO,
6661 show_debug_level, store_debug_level);
Zhu Yib481de92007-09-25 17:54:57 -07006662
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08006663#endif /* CONFIG_IWL3945_DEBUG */
Zhu Yib481de92007-09-25 17:54:57 -07006664
Zhu Yib481de92007-09-25 17:54:57 -07006665static ssize_t show_temperature(struct device *d,
6666 struct device_attribute *attr, char *buf)
6667{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08006668 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07006669
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08006670 if (!iwl_is_alive(priv))
Zhu Yib481de92007-09-25 17:54:57 -07006671 return -EAGAIN;
6672
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006673 return sprintf(buf, "%d\n", iwl3945_hw_get_temperature(priv));
Zhu Yib481de92007-09-25 17:54:57 -07006674}
6675
6676static DEVICE_ATTR(temperature, S_IRUGO, show_temperature, NULL);
6677
Zhu Yib481de92007-09-25 17:54:57 -07006678static ssize_t show_tx_power(struct device *d,
6679 struct device_attribute *attr, char *buf)
6680{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08006681 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07006682 return sprintf(buf, "%d\n", priv->user_txpower_limit);
6683}
6684
6685static ssize_t store_tx_power(struct device *d,
6686 struct device_attribute *attr,
6687 const char *buf, size_t count)
6688{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08006689 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07006690 char *p = (char *)buf;
6691 u32 val;
6692
6693 val = simple_strtoul(p, &p, 10);
6694 if (p == buf)
Tomas Winkler978785a2008-12-19 10:37:31 +08006695 IWL_INFO(priv, ": %s is not in decimal form.\n", buf);
Zhu Yib481de92007-09-25 17:54:57 -07006696 else
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006697 iwl3945_hw_reg_set_txpower(priv, val);
Zhu Yib481de92007-09-25 17:54:57 -07006698
6699 return count;
6700}
6701
6702static DEVICE_ATTR(tx_power, S_IWUSR | S_IRUGO, show_tx_power, store_tx_power);
6703
6704static ssize_t show_flags(struct device *d,
6705 struct device_attribute *attr, char *buf)
6706{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08006707 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07006708
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08006709 return sprintf(buf, "0x%04X\n", priv->active39_rxon.flags);
Zhu Yib481de92007-09-25 17:54:57 -07006710}
6711
6712static ssize_t store_flags(struct device *d,
6713 struct device_attribute *attr,
6714 const char *buf, size_t count)
6715{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08006716 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07006717 u32 flags = simple_strtoul(buf, NULL, 0);
6718
6719 mutex_lock(&priv->mutex);
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08006720 if (le32_to_cpu(priv->staging39_rxon.flags) != flags) {
Zhu Yib481de92007-09-25 17:54:57 -07006721 /* Cancel any currently running scans... */
Winkler, Tomasaf0053d2009-01-19 15:30:23 -08006722 if (iwl_scan_cancel_timeout(priv, 100))
Winkler, Tomas39aadf82008-12-19 10:37:32 +08006723 IWL_WARN(priv, "Could not cancel scan.\n");
Zhu Yib481de92007-09-25 17:54:57 -07006724 else {
6725 IWL_DEBUG_INFO("Committing rxon.flags = 0x%04X\n",
6726 flags);
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08006727 priv->staging39_rxon.flags = cpu_to_le32(flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006728 iwl3945_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006729 }
6730 }
6731 mutex_unlock(&priv->mutex);
6732
6733 return count;
6734}
6735
6736static DEVICE_ATTR(flags, S_IWUSR | S_IRUGO, show_flags, store_flags);
6737
6738static ssize_t show_filter_flags(struct device *d,
6739 struct device_attribute *attr, char *buf)
6740{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08006741 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07006742
6743 return sprintf(buf, "0x%04X\n",
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08006744 le32_to_cpu(priv->active39_rxon.filter_flags));
Zhu Yib481de92007-09-25 17:54:57 -07006745}
6746
6747static ssize_t store_filter_flags(struct device *d,
6748 struct device_attribute *attr,
6749 const char *buf, size_t count)
6750{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08006751 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07006752 u32 filter_flags = simple_strtoul(buf, NULL, 0);
6753
6754 mutex_lock(&priv->mutex);
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08006755 if (le32_to_cpu(priv->staging39_rxon.filter_flags) != filter_flags) {
Zhu Yib481de92007-09-25 17:54:57 -07006756 /* Cancel any currently running scans... */
Winkler, Tomasaf0053d2009-01-19 15:30:23 -08006757 if (iwl_scan_cancel_timeout(priv, 100))
Winkler, Tomas39aadf82008-12-19 10:37:32 +08006758 IWL_WARN(priv, "Could not cancel scan.\n");
Zhu Yib481de92007-09-25 17:54:57 -07006759 else {
6760 IWL_DEBUG_INFO("Committing rxon.filter_flags = "
6761 "0x%04X\n", filter_flags);
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08006762 priv->staging39_rxon.filter_flags =
Zhu Yib481de92007-09-25 17:54:57 -07006763 cpu_to_le32(filter_flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006764 iwl3945_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006765 }
6766 }
6767 mutex_unlock(&priv->mutex);
6768
6769 return count;
6770}
6771
6772static DEVICE_ATTR(filter_flags, S_IWUSR | S_IRUGO, show_filter_flags,
6773 store_filter_flags);
6774
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08006775#ifdef CONFIG_IWL3945_SPECTRUM_MEASUREMENT
Zhu Yib481de92007-09-25 17:54:57 -07006776
6777static ssize_t show_measurement(struct device *d,
6778 struct device_attribute *attr, char *buf)
6779{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08006780 struct iwl_priv *priv = dev_get_drvdata(d);
Tomas Winkler600c0e12008-12-19 10:37:04 +08006781 struct iwl_spectrum_notification measure_report;
Zhu Yib481de92007-09-25 17:54:57 -07006782 u32 size = sizeof(measure_report), len = 0, ofs = 0;
Tomas Winkler3ac7f142008-07-21 02:40:14 +03006783 u8 *data = (u8 *)&measure_report;
Zhu Yib481de92007-09-25 17:54:57 -07006784 unsigned long flags;
6785
6786 spin_lock_irqsave(&priv->lock, flags);
6787 if (!(priv->measurement_status & MEASUREMENT_READY)) {
6788 spin_unlock_irqrestore(&priv->lock, flags);
6789 return 0;
6790 }
6791 memcpy(&measure_report, &priv->measure_report, size);
6792 priv->measurement_status = 0;
6793 spin_unlock_irqrestore(&priv->lock, flags);
6794
6795 while (size && (PAGE_SIZE - len)) {
6796 hex_dump_to_buffer(data + ofs, size, 16, 1, buf + len,
6797 PAGE_SIZE - len, 1);
6798 len = strlen(buf);
6799 if (PAGE_SIZE - len)
6800 buf[len++] = '\n';
6801
6802 ofs += 16;
6803 size -= min(size, 16U);
6804 }
6805
6806 return len;
6807}
6808
6809static ssize_t store_measurement(struct device *d,
6810 struct device_attribute *attr,
6811 const char *buf, size_t count)
6812{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08006813 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07006814 struct ieee80211_measurement_params params = {
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08006815 .channel = le16_to_cpu(priv->active39_rxon.channel),
Zhu Yib481de92007-09-25 17:54:57 -07006816 .start_time = cpu_to_le64(priv->last_tsf),
6817 .duration = cpu_to_le16(1),
6818 };
6819 u8 type = IWL_MEASURE_BASIC;
6820 u8 buffer[32];
6821 u8 channel;
6822
6823 if (count) {
6824 char *p = buffer;
6825 strncpy(buffer, buf, min(sizeof(buffer), count));
6826 channel = simple_strtoul(p, NULL, 0);
6827 if (channel)
6828 params.channel = channel;
6829
6830 p = buffer;
6831 while (*p && *p != ' ')
6832 p++;
6833 if (*p)
6834 type = simple_strtoul(p + 1, NULL, 0);
6835 }
6836
6837 IWL_DEBUG_INFO("Invoking measurement of type %d on "
6838 "channel %d (for '%s')\n", type, params.channel, buf);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006839 iwl3945_get_measurement(priv, &params, type);
Zhu Yib481de92007-09-25 17:54:57 -07006840
6841 return count;
6842}
6843
6844static DEVICE_ATTR(measurement, S_IRUSR | S_IWUSR,
6845 show_measurement, store_measurement);
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08006846#endif /* CONFIG_IWL3945_SPECTRUM_MEASUREMENT */
Zhu Yib481de92007-09-25 17:54:57 -07006847
Zhu Yib481de92007-09-25 17:54:57 -07006848static ssize_t store_retry_rate(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 = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07006853
6854 priv->retry_rate = simple_strtoul(buf, NULL, 0);
6855 if (priv->retry_rate <= 0)
6856 priv->retry_rate = 1;
6857
6858 return count;
6859}
6860
6861static ssize_t show_retry_rate(struct device *d,
6862 struct device_attribute *attr, char *buf)
6863{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08006864 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07006865 return sprintf(buf, "%d", priv->retry_rate);
6866}
6867
6868static DEVICE_ATTR(retry_rate, S_IWUSR | S_IRUSR, show_retry_rate,
6869 store_retry_rate);
6870
6871static ssize_t store_power_level(struct device *d,
6872 struct device_attribute *attr,
6873 const char *buf, size_t count)
6874{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08006875 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07006876 int rc;
6877 int mode;
6878
6879 mode = simple_strtoul(buf, NULL, 0);
6880 mutex_lock(&priv->mutex);
6881
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08006882 if (!iwl_is_ready(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07006883 rc = -EAGAIN;
6884 goto out;
6885 }
6886
Samuel Ortiz1125eff2008-12-19 10:37:14 +08006887 if ((mode < 1) || (mode > IWL39_POWER_LIMIT) ||
6888 (mode == IWL39_POWER_AC))
6889 mode = IWL39_POWER_AC;
Zhu Yib481de92007-09-25 17:54:57 -07006890 else
6891 mode |= IWL_POWER_ENABLED;
6892
6893 if (mode != priv->power_mode) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006894 rc = iwl3945_send_power_mode(priv, IWL_POWER_LEVEL(mode));
Zhu Yib481de92007-09-25 17:54:57 -07006895 if (rc) {
6896 IWL_DEBUG_MAC80211("failed setting power mode.\n");
6897 goto out;
6898 }
6899 priv->power_mode = mode;
6900 }
6901
6902 rc = count;
6903
6904 out:
6905 mutex_unlock(&priv->mutex);
6906 return rc;
6907}
6908
6909#define MAX_WX_STRING 80
6910
6911/* Values are in microsecond */
6912static const s32 timeout_duration[] = {
6913 350000,
6914 250000,
6915 75000,
6916 37000,
6917 25000,
6918};
6919static const s32 period_duration[] = {
6920 400000,
6921 700000,
6922 1000000,
6923 1000000,
6924 1000000
6925};
6926
6927static ssize_t show_power_level(struct device *d,
6928 struct device_attribute *attr, char *buf)
6929{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08006930 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07006931 int level = IWL_POWER_LEVEL(priv->power_mode);
6932 char *p = buf;
6933
6934 p += sprintf(p, "%d ", level);
6935 switch (level) {
6936 case IWL_POWER_MODE_CAM:
Samuel Ortiz1125eff2008-12-19 10:37:14 +08006937 case IWL39_POWER_AC:
Zhu Yib481de92007-09-25 17:54:57 -07006938 p += sprintf(p, "(AC)");
6939 break;
Samuel Ortiz1125eff2008-12-19 10:37:14 +08006940 case IWL39_POWER_BATTERY:
Zhu Yib481de92007-09-25 17:54:57 -07006941 p += sprintf(p, "(BATTERY)");
6942 break;
6943 default:
6944 p += sprintf(p,
6945 "(Timeout %dms, Period %dms)",
6946 timeout_duration[level - 1] / 1000,
6947 period_duration[level - 1] / 1000);
6948 }
6949
6950 if (!(priv->power_mode & IWL_POWER_ENABLED))
6951 p += sprintf(p, " OFF\n");
6952 else
6953 p += sprintf(p, " \n");
6954
Tomas Winkler3ac7f142008-07-21 02:40:14 +03006955 return p - buf + 1;
Zhu Yib481de92007-09-25 17:54:57 -07006956
6957}
6958
6959static DEVICE_ATTR(power_level, S_IWUSR | S_IRUSR, show_power_level,
6960 store_power_level);
6961
6962static ssize_t show_channels(struct device *d,
6963 struct device_attribute *attr, char *buf)
6964{
Johannes Berg8318d782008-01-24 19:38:38 +01006965 /* all this shit doesn't belong into sysfs anyway */
6966 return 0;
Zhu Yib481de92007-09-25 17:54:57 -07006967}
6968
6969static DEVICE_ATTR(channels, S_IRUSR, show_channels, NULL);
6970
6971static ssize_t show_statistics(struct device *d,
6972 struct device_attribute *attr, char *buf)
6973{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08006974 struct iwl_priv *priv = dev_get_drvdata(d);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006975 u32 size = sizeof(struct iwl3945_notif_statistics);
Zhu Yib481de92007-09-25 17:54:57 -07006976 u32 len = 0, ofs = 0;
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08006977 u8 *data = (u8 *)&priv->statistics_39;
Zhu Yib481de92007-09-25 17:54:57 -07006978 int rc = 0;
6979
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08006980 if (!iwl_is_alive(priv))
Zhu Yib481de92007-09-25 17:54:57 -07006981 return -EAGAIN;
6982
6983 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006984 rc = iwl3945_send_statistics_request(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006985 mutex_unlock(&priv->mutex);
6986
6987 if (rc) {
6988 len = sprintf(buf,
6989 "Error sending statistics request: 0x%08X\n", rc);
6990 return len;
6991 }
6992
6993 while (size && (PAGE_SIZE - len)) {
6994 hex_dump_to_buffer(data + ofs, size, 16, 1, buf + len,
6995 PAGE_SIZE - len, 1);
6996 len = strlen(buf);
6997 if (PAGE_SIZE - len)
6998 buf[len++] = '\n';
6999
7000 ofs += 16;
7001 size -= min(size, 16U);
7002 }
7003
7004 return len;
7005}
7006
7007static DEVICE_ATTR(statistics, S_IRUGO, show_statistics, NULL);
7008
7009static ssize_t show_antenna(struct device *d,
7010 struct device_attribute *attr, char *buf)
7011{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08007012 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07007013
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08007014 if (!iwl_is_alive(priv))
Zhu Yib481de92007-09-25 17:54:57 -07007015 return -EAGAIN;
7016
7017 return sprintf(buf, "%d\n", priv->antenna);
7018}
7019
7020static ssize_t store_antenna(struct device *d,
7021 struct device_attribute *attr,
7022 const char *buf, size_t count)
7023{
7024 int ant;
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08007025 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07007026
7027 if (count == 0)
7028 return 0;
7029
7030 if (sscanf(buf, "%1i", &ant) != 1) {
7031 IWL_DEBUG_INFO("not in hex or decimal form.\n");
7032 return count;
7033 }
7034
7035 if ((ant >= 0) && (ant <= 2)) {
7036 IWL_DEBUG_INFO("Setting antenna select to %d.\n", ant);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007037 priv->antenna = (enum iwl3945_antenna)ant;
Zhu Yib481de92007-09-25 17:54:57 -07007038 } else
7039 IWL_DEBUG_INFO("Bad antenna select value %d.\n", ant);
7040
7041
7042 return count;
7043}
7044
7045static DEVICE_ATTR(antenna, S_IWUSR | S_IRUGO, show_antenna, store_antenna);
7046
7047static ssize_t show_status(struct device *d,
7048 struct device_attribute *attr, char *buf)
7049{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08007050 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08007051 if (!iwl_is_alive(priv))
Zhu Yib481de92007-09-25 17:54:57 -07007052 return -EAGAIN;
7053 return sprintf(buf, "0x%08x\n", (int)priv->status);
7054}
7055
7056static DEVICE_ATTR(status, S_IRUGO, show_status, NULL);
7057
7058static ssize_t dump_error_log(struct device *d,
7059 struct device_attribute *attr,
7060 const char *buf, size_t count)
7061{
7062 char *p = (char *)buf;
7063
7064 if (p[0] == '1')
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08007065 iwl3945_dump_nic_error_log((struct iwl_priv *)d->driver_data);
Zhu Yib481de92007-09-25 17:54:57 -07007066
7067 return strnlen(buf, count);
7068}
7069
7070static DEVICE_ATTR(dump_errors, S_IWUSR, NULL, dump_error_log);
7071
7072static ssize_t dump_event_log(struct device *d,
7073 struct device_attribute *attr,
7074 const char *buf, size_t count)
7075{
7076 char *p = (char *)buf;
7077
7078 if (p[0] == '1')
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08007079 iwl3945_dump_nic_event_log((struct iwl_priv *)d->driver_data);
Zhu Yib481de92007-09-25 17:54:57 -07007080
7081 return strnlen(buf, count);
7082}
7083
7084static DEVICE_ATTR(dump_events, S_IWUSR, NULL, dump_event_log);
7085
7086/*****************************************************************************
7087 *
Tomas Winklera96a27f2008-10-23 23:48:56 -07007088 * driver setup and tear down
Zhu Yib481de92007-09-25 17:54:57 -07007089 *
7090 *****************************************************************************/
7091
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08007092static void iwl3945_setup_deferred_work(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07007093{
7094 priv->workqueue = create_workqueue(DRV_NAME);
7095
7096 init_waitqueue_head(&priv->wait_command_queue);
7097
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007098 INIT_WORK(&priv->up, iwl3945_bg_up);
7099 INIT_WORK(&priv->restart, iwl3945_bg_restart);
7100 INIT_WORK(&priv->rx_replenish, iwl3945_bg_rx_replenish);
7101 INIT_WORK(&priv->scan_completed, iwl3945_bg_scan_completed);
7102 INIT_WORK(&priv->request_scan, iwl3945_bg_request_scan);
7103 INIT_WORK(&priv->abort_scan, iwl3945_bg_abort_scan);
7104 INIT_WORK(&priv->rf_kill, iwl3945_bg_rf_kill);
7105 INIT_WORK(&priv->beacon_update, iwl3945_bg_beacon_update);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007106 INIT_DELAYED_WORK(&priv->init_alive_start, iwl3945_bg_init_alive_start);
7107 INIT_DELAYED_WORK(&priv->alive_start, iwl3945_bg_alive_start);
7108 INIT_DELAYED_WORK(&priv->scan_check, iwl3945_bg_scan_check);
Helmut Schaa26635162009-01-15 09:38:44 +01007109 INIT_DELAYED_WORK(&priv->rfkill_poll, iwl3945_rfkill_poll);
Zhu Yib481de92007-09-25 17:54:57 -07007110
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007111 iwl3945_hw_setup_deferred_work(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007112
7113 tasklet_init(&priv->irq_tasklet, (void (*)(unsigned long))
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007114 iwl3945_irq_tasklet, (unsigned long)priv);
Zhu Yib481de92007-09-25 17:54:57 -07007115}
7116
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08007117static void iwl3945_cancel_deferred_work(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07007118{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007119 iwl3945_hw_cancel_deferred_work(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007120
Joonwoo Parke47eb6a2007-11-29 10:42:49 +09007121 cancel_delayed_work_sync(&priv->init_alive_start);
Zhu Yib481de92007-09-25 17:54:57 -07007122 cancel_delayed_work(&priv->scan_check);
7123 cancel_delayed_work(&priv->alive_start);
Zhu Yib481de92007-09-25 17:54:57 -07007124 cancel_work_sync(&priv->beacon_update);
7125}
7126
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007127static struct attribute *iwl3945_sysfs_entries[] = {
Zhu Yib481de92007-09-25 17:54:57 -07007128 &dev_attr_antenna.attr,
7129 &dev_attr_channels.attr,
7130 &dev_attr_dump_errors.attr,
7131 &dev_attr_dump_events.attr,
7132 &dev_attr_flags.attr,
7133 &dev_attr_filter_flags.attr,
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08007134#ifdef CONFIG_IWL3945_SPECTRUM_MEASUREMENT
Zhu Yib481de92007-09-25 17:54:57 -07007135 &dev_attr_measurement.attr,
7136#endif
7137 &dev_attr_power_level.attr,
Zhu Yib481de92007-09-25 17:54:57 -07007138 &dev_attr_retry_rate.attr,
Zhu Yib481de92007-09-25 17:54:57 -07007139 &dev_attr_statistics.attr,
7140 &dev_attr_status.attr,
7141 &dev_attr_temperature.attr,
Zhu Yib481de92007-09-25 17:54:57 -07007142 &dev_attr_tx_power.attr,
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08007143#ifdef CONFIG_IWL3945_DEBUG
7144 &dev_attr_debug_level.attr,
7145#endif
Zhu Yib481de92007-09-25 17:54:57 -07007146 NULL
7147};
7148
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007149static struct attribute_group iwl3945_attribute_group = {
Zhu Yib481de92007-09-25 17:54:57 -07007150 .name = NULL, /* put in device directory */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007151 .attrs = iwl3945_sysfs_entries,
Zhu Yib481de92007-09-25 17:54:57 -07007152};
7153
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007154static struct ieee80211_ops iwl3945_hw_ops = {
7155 .tx = iwl3945_mac_tx,
7156 .start = iwl3945_mac_start,
7157 .stop = iwl3945_mac_stop,
7158 .add_interface = iwl3945_mac_add_interface,
7159 .remove_interface = iwl3945_mac_remove_interface,
7160 .config = iwl3945_mac_config,
7161 .config_interface = iwl3945_mac_config_interface,
7162 .configure_filter = iwl3945_configure_filter,
7163 .set_key = iwl3945_mac_set_key,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007164 .get_tx_stats = iwl3945_mac_get_tx_stats,
7165 .conf_tx = iwl3945_mac_conf_tx,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007166 .reset_tsf = iwl3945_mac_reset_tsf,
Abhijeet Kolekarcd56d332008-09-03 11:26:21 +08007167 .bss_info_changed = iwl3945_bss_info_changed,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007168 .hw_scan = iwl3945_mac_hw_scan
Zhu Yib481de92007-09-25 17:54:57 -07007169};
7170
Winkler, Tomase52119c2008-12-22 11:31:19 +08007171static int iwl3945_init_drv(struct iwl_priv *priv)
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08007172{
7173 int ret;
7174
7175 priv->retry_rate = 1;
7176 priv->ibss_beacon = NULL;
7177
7178 spin_lock_init(&priv->lock);
Samuel Ortizc7a7c8e2009-01-08 10:20:01 -08007179 spin_lock_init(&priv->power_data_39.lock);
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08007180 spin_lock_init(&priv->sta_lock);
7181 spin_lock_init(&priv->hcmd_lock);
7182
7183 INIT_LIST_HEAD(&priv->free_frames);
7184
7185 mutex_init(&priv->mutex);
7186
7187 /* Clear the driver's (not device's) station table */
7188 iwl3945_clear_stations_table(priv);
7189
7190 priv->data_retry_limit = -1;
7191 priv->ieee_channels = NULL;
7192 priv->ieee_rates = NULL;
7193 priv->band = IEEE80211_BAND_2GHZ;
7194
7195 priv->iw_mode = NL80211_IFTYPE_STATION;
7196
7197 iwl_reset_qos(priv);
7198
7199 priv->qos_data.qos_active = 0;
7200 priv->qos_data.qos_cap.val = 0;
7201
7202 priv->rates_mask = IWL_RATES_MASK;
7203 /* If power management is turned on, default to AC mode */
Samuel Ortizc7a7c8e2009-01-08 10:20:01 -08007204 priv->power_mode = IWL39_POWER_AC;
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08007205 priv->user_txpower_limit = IWL_DEFAULT_TX_POWER;
7206
7207 ret = iwl3945_init_channel_map(priv);
7208 if (ret) {
7209 IWL_ERR(priv, "initializing regulatory failed: %d\n", ret);
7210 goto err;
7211 }
7212
7213 ret = iwl3945_init_geos(priv);
7214 if (ret) {
7215 IWL_ERR(priv, "initializing geos failed: %d\n", ret);
7216 goto err_free_channel_map;
7217 }
7218
7219 return 0;
7220
7221err_free_channel_map:
7222 iwl3945_free_channel_map(priv);
7223err:
7224 return ret;
7225}
7226
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007227static int iwl3945_pci_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
Zhu Yib481de92007-09-25 17:54:57 -07007228{
7229 int err = 0;
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08007230 struct iwl_priv *priv;
Zhu Yib481de92007-09-25 17:54:57 -07007231 struct ieee80211_hw *hw;
Kolekar, Abhijeetc0f20d92008-12-19 10:37:19 +08007232 struct iwl_cfg *cfg = (struct iwl_cfg *)(ent->driver_data);
Mohamed Abbas0359fac2008-03-28 16:21:08 -07007233 unsigned long flags;
Zhu Yib481de92007-09-25 17:54:57 -07007234
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08007235 /***********************
7236 * 1. Allocating HW data
7237 * ********************/
7238
Zhu Yib481de92007-09-25 17:54:57 -07007239 /* mac80211 allocates memory for this device instance, including
7240 * space for this driver's private structure */
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08007241 hw = iwl_alloc_all(cfg, &iwl3945_hw_ops);
Zhu Yib481de92007-09-25 17:54:57 -07007242 if (hw == NULL) {
Samuel Ortiza3139c52008-12-19 10:37:09 +08007243 printk(KERN_ERR DRV_NAME "Can not allocate network device\n");
Zhu Yib481de92007-09-25 17:54:57 -07007244 err = -ENOMEM;
7245 goto out;
7246 }
Zhu Yib481de92007-09-25 17:54:57 -07007247 priv = hw->priv;
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08007248 SET_IEEE80211_DEV(hw, &pdev->dev);
Cahill, Ben M6440adb2007-11-29 11:09:55 +08007249
Kolekar, Abhijeetdf878d82008-12-19 10:37:35 +08007250 if ((iwl3945_mod_params.num_of_queues > IWL39_MAX_NUM_QUEUES) ||
7251 (iwl3945_mod_params.num_of_queues < IWL_MIN_NUM_QUEUES)) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08007252 IWL_ERR(priv,
7253 "invalid queues_num, should be between %d and %d\n",
7254 IWL_MIN_NUM_QUEUES, IWL39_MAX_NUM_QUEUES);
Samuel Ortiza3139c52008-12-19 10:37:09 +08007255 err = -EINVAL;
7256 goto out;
7257 }
7258
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08007259 /*
7260 * Disabling hardware scan means that mac80211 will perform scans
7261 * "the hard way", rather than using device's scan.
7262 */
Kolekar, Abhijeetdf878d82008-12-19 10:37:35 +08007263 if (iwl3945_mod_params.disable_hw_scan) {
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08007264 IWL_DEBUG_INFO("Disabling hw_scan\n");
7265 iwl3945_hw_ops.hw_scan = NULL;
7266 }
7267
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08007268
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08007269 IWL_DEBUG_INFO("*** LOAD DRIVER ***\n");
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08007270 priv->cfg = cfg;
7271 priv->pci_dev = pdev;
7272
7273#ifdef CONFIG_IWL3945_DEBUG
7274 priv->debug_level = iwl3945_mod_params.debug;
7275 atomic_set(&priv->restrict_refcnt, 0);
7276#endif
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08007277 hw->rate_control_algorithm = "iwl-3945-rs";
7278 hw->sta_data_size = sizeof(struct iwl3945_sta_priv);
7279
Cahill, Ben M6440adb2007-11-29 11:09:55 +08007280 /* Select antenna (may be helpful if only one antenna is connected) */
Kolekar, Abhijeetdf878d82008-12-19 10:37:35 +08007281 priv->antenna = (enum iwl3945_antenna)iwl3945_mod_params.antenna;
Zhu Yib481de92007-09-25 17:54:57 -07007282
Bruno Randolf566bfe52008-05-08 19:15:40 +02007283 /* Tell mac80211 our characteristics */
Johannes Berg605a0bd2008-07-15 10:10:01 +02007284 hw->flags = IEEE80211_HW_SIGNAL_DBM |
Bruno Randolf566bfe52008-05-08 19:15:40 +02007285 IEEE80211_HW_NOISE_DBM;
Zhu Yib481de92007-09-25 17:54:57 -07007286
Luis R. Rodriguezf59ac042008-08-29 16:26:43 -07007287 hw->wiphy->interface_modes =
Luis R. Rodriguezf59ac042008-08-29 16:26:43 -07007288 BIT(NL80211_IFTYPE_STATION) |
7289 BIT(NL80211_IFTYPE_ADHOC);
7290
Luis R. Rodriguezea4a82dc2008-11-12 14:22:04 -08007291 hw->wiphy->fw_handles_regulatory = true;
7292
Cahill, Ben M6440adb2007-11-29 11:09:55 +08007293 /* 4 EDCA QOS priorities */
Zhu Yib481de92007-09-25 17:54:57 -07007294 hw->queues = 4;
7295
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08007296 /***************************
7297 * 2. Initializing PCI bus
7298 * *************************/
Zhu Yib481de92007-09-25 17:54:57 -07007299 if (pci_enable_device(pdev)) {
7300 err = -ENODEV;
7301 goto out_ieee80211_free_hw;
7302 }
7303
7304 pci_set_master(pdev);
7305
Zhu Yib481de92007-09-25 17:54:57 -07007306 err = pci_set_dma_mask(pdev, DMA_32BIT_MASK);
7307 if (!err)
7308 err = pci_set_consistent_dma_mask(pdev, DMA_32BIT_MASK);
7309 if (err) {
Tomas Winkler978785a2008-12-19 10:37:31 +08007310 IWL_WARN(priv, "No suitable DMA available.\n");
Zhu Yib481de92007-09-25 17:54:57 -07007311 goto out_pci_disable_device;
7312 }
7313
7314 pci_set_drvdata(pdev, priv);
7315 err = pci_request_regions(pdev, DRV_NAME);
7316 if (err)
7317 goto out_pci_disable_device;
Cahill, Ben M6440adb2007-11-29 11:09:55 +08007318
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08007319 /***********************
7320 * 3. Read REV Register
7321 * ********************/
Zhu Yib481de92007-09-25 17:54:57 -07007322 priv->hw_base = pci_iomap(pdev, 0, 0);
7323 if (!priv->hw_base) {
7324 err = -ENODEV;
7325 goto out_pci_release_regions;
7326 }
7327
7328 IWL_DEBUG_INFO("pci_resource_len = 0x%08llx\n",
7329 (unsigned long long) pci_resource_len(pdev, 0));
7330 IWL_DEBUG_INFO("pci_resource_base = %p\n", priv->hw_base);
7331
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08007332 /* We disable the RETRY_TIMEOUT register (0x41) to keep
7333 * PCI Tx retries from interfering with C3 CPU state */
7334 pci_write_config_byte(pdev, 0x41, 0x00);
Zhu Yib481de92007-09-25 17:54:57 -07007335
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08007336 /* amp init */
7337 err = priv->cfg->ops->lib->apm_ops.init(priv);
Tomas Winkler3ac7f142008-07-21 02:40:14 +03007338 if (err < 0) {
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08007339 IWL_DEBUG_INFO("Failed to init APMG\n");
7340 goto out_iounmap;
Tomas Winkler3ac7f142008-07-21 02:40:14 +03007341 }
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08007342
7343 /***********************
7344 * 4. Read EEPROM
7345 * ********************/
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08007346
Zhu Yi5a669262008-01-14 17:46:18 -08007347 /* Read the EEPROM */
7348 err = iwl3945_eeprom_init(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007349 if (err) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08007350 IWL_ERR(priv, "Unable to init EEPROM\n");
Zhu Yi5a669262008-01-14 17:46:18 -08007351 goto out_remove_sysfs;
7352 }
7353 /* MAC Address location in EEPROM same for 3945/4965 */
7354 get_eeprom_mac(priv, priv->mac_addr);
Johannes Berge1749612008-10-27 15:59:26 -07007355 IWL_DEBUG_INFO("MAC address: %pM\n", priv->mac_addr);
Zhu Yi5a669262008-01-14 17:46:18 -08007356 SET_IEEE80211_PERM_ADDR(priv->hw, priv->mac_addr);
7357
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08007358 /***********************
7359 * 5. Setup HW Constants
7360 * ********************/
7361 /* Device-specific setup */
Abhijeet Kolekar3832ec92008-12-19 10:37:26 +08007362 if (iwl3945_hw_set_hw_params(priv)) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08007363 IWL_ERR(priv, "failed to set hw settings\n");
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08007364 goto out_iounmap;
7365 }
7366
7367 /***********************
7368 * 6. Setup priv
7369 * ********************/
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08007370
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08007371 err = iwl3945_init_drv(priv);
Reinette Chatre849e0dc2008-01-23 10:15:18 -08007372 if (err) {
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08007373 IWL_ERR(priv, "initializing driver failed\n");
7374 goto out_free_geos;
Reinette Chatre849e0dc2008-01-23 10:15:18 -08007375 }
Reinette Chatre849e0dc2008-01-23 10:15:18 -08007376
Tomas Winkler978785a2008-12-19 10:37:31 +08007377 IWL_INFO(priv, "Detected Intel Wireless WiFi Link %s\n",
7378 priv->cfg->name);
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08007379
7380 /***********************************
7381 * 7. Initialize Module Parameters
7382 * **********************************/
7383
7384 /* Initialize module parameter values here */
7385 /* Disable radio (SW RF KILL) via parameter when loading driver */
Kolekar, Abhijeetdf878d82008-12-19 10:37:35 +08007386 if (iwl3945_mod_params.disable) {
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08007387 set_bit(STATUS_RF_KILL_SW, &priv->status);
7388 IWL_DEBUG_INFO("Radio disabled.\n");
7389 }
7390
7391
7392 /***********************
7393 * 8. Setup Services
7394 * ********************/
7395
7396 spin_lock_irqsave(&priv->lock, flags);
7397 iwl3945_disable_interrupts(priv);
7398 spin_unlock_irqrestore(&priv->lock, flags);
7399
Helmut Schaa26635162009-01-15 09:38:44 +01007400 pci_enable_msi(priv->pci_dev);
7401
7402 err = request_irq(priv->pci_dev->irq, iwl3945_isr, IRQF_SHARED,
7403 DRV_NAME, priv);
7404 if (err) {
7405 IWL_ERR(priv, "Error allocating IRQ %d\n", priv->pci_dev->irq);
7406 goto out_disable_msi;
7407 }
7408
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08007409 err = sysfs_create_group(&pdev->dev.kobj, &iwl3945_attribute_group);
7410 if (err) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08007411 IWL_ERR(priv, "failed to create sysfs device attributes\n");
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08007412 goto out_release_irq;
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08007413 }
7414
7415 iwl3945_set_rxon_channel(priv, IEEE80211_BAND_2GHZ, 6);
7416 iwl3945_setup_deferred_work(priv);
7417 iwl3945_setup_rx_handlers(priv);
7418
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08007419 /*********************************
Helmut Schaa26635162009-01-15 09:38:44 +01007420 * 9. Setup and Register mac80211
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08007421 * *******************************/
7422
Zhu Yi5a669262008-01-14 17:46:18 -08007423 err = ieee80211_register_hw(priv->hw);
7424 if (err) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08007425 IWL_ERR(priv, "Failed to register network device: %d\n", err);
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08007426 goto out_remove_sysfs;
Zhu Yib481de92007-09-25 17:54:57 -07007427 }
7428
Zhu Yi5a669262008-01-14 17:46:18 -08007429 priv->hw->conf.beacon_int = 100;
7430 priv->mac80211_registered = 1;
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08007431
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08007432 err = iwl3945_rfkill_init(priv);
7433 if (err)
Winkler, Tomas15b16872008-12-19 10:37:33 +08007434 IWL_ERR(priv, "Unable to initialize RFKILL system. "
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08007435 "Ignoring error: %d\n", err);
7436
Helmut Schaa26635162009-01-15 09:38:44 +01007437 /* Start monitoring the killswitch */
7438 queue_delayed_work(priv->workqueue, &priv->rfkill_poll,
7439 2 * HZ);
7440
Zhu Yib481de92007-09-25 17:54:57 -07007441 return 0;
7442
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08007443 out_remove_sysfs:
7444 sysfs_remove_group(&pdev->dev.kobj, &iwl3945_attribute_group);
Reinette Chatre849e0dc2008-01-23 10:15:18 -08007445 out_free_geos:
7446 iwl3945_free_geos(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007447
7448 out_release_irq:
Helmut Schaa26635162009-01-15 09:38:44 +01007449 free_irq(priv->pci_dev->irq, priv);
Zhu Yib481de92007-09-25 17:54:57 -07007450 destroy_workqueue(priv->workqueue);
7451 priv->workqueue = NULL;
Abhijeet Kolekar3832ec92008-12-19 10:37:26 +08007452 iwl3945_unset_hw_params(priv);
Helmut Schaa26635162009-01-15 09:38:44 +01007453 out_disable_msi:
7454 pci_disable_msi(priv->pci_dev);
Zhu Yib481de92007-09-25 17:54:57 -07007455 out_iounmap:
7456 pci_iounmap(pdev, priv->hw_base);
7457 out_pci_release_regions:
7458 pci_release_regions(pdev);
7459 out_pci_disable_device:
7460 pci_disable_device(pdev);
7461 pci_set_drvdata(pdev, NULL);
7462 out_ieee80211_free_hw:
7463 ieee80211_free_hw(priv->hw);
7464 out:
7465 return err;
7466}
7467
Reinette Chatrec83dbf62008-03-21 13:53:41 -07007468static void __devexit iwl3945_pci_remove(struct pci_dev *pdev)
Zhu Yib481de92007-09-25 17:54:57 -07007469{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08007470 struct iwl_priv *priv = pci_get_drvdata(pdev);
Mohamed Abbas0359fac2008-03-28 16:21:08 -07007471 unsigned long flags;
Zhu Yib481de92007-09-25 17:54:57 -07007472
7473 if (!priv)
7474 return;
7475
7476 IWL_DEBUG_INFO("*** UNLOAD DRIVER ***\n");
7477
Zhu Yib481de92007-09-25 17:54:57 -07007478 set_bit(STATUS_EXIT_PENDING, &priv->status);
Zhu Yib24d22b2007-12-19 13:59:52 +08007479
Kolekar, Abhijeetd552bfb2008-12-19 10:37:41 +08007480 if (priv->mac80211_registered) {
7481 ieee80211_unregister_hw(priv->hw);
7482 priv->mac80211_registered = 0;
7483 } else {
7484 iwl3945_down(priv);
7485 }
Zhu Yib481de92007-09-25 17:54:57 -07007486
Mohamed Abbas0359fac2008-03-28 16:21:08 -07007487 /* make sure we flush any pending irq or
7488 * tasklet for the driver
7489 */
7490 spin_lock_irqsave(&priv->lock, flags);
7491 iwl3945_disable_interrupts(priv);
7492 spin_unlock_irqrestore(&priv->lock, flags);
7493
7494 iwl_synchronize_irq(priv);
7495
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007496 sysfs_remove_group(&pdev->dev.kobj, &iwl3945_attribute_group);
Zhu Yib481de92007-09-25 17:54:57 -07007497
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08007498 iwl3945_rfkill_unregister(priv);
Helmut Schaa26635162009-01-15 09:38:44 +01007499 cancel_delayed_work(&priv->rfkill_poll);
7500
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007501 iwl3945_dealloc_ucode_pci(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007502
7503 if (priv->rxq.bd)
Winkler, Tomas51af3d32008-12-22 11:31:23 +08007504 iwl_rx_queue_free(priv, &priv->rxq);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007505 iwl3945_hw_txq_ctx_free(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007506
Abhijeet Kolekar3832ec92008-12-19 10:37:26 +08007507 iwl3945_unset_hw_params(priv);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007508 iwl3945_clear_stations_table(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007509
Mohamed Abbas6ef89d02007-10-25 17:15:47 +08007510 /*netif_stop_queue(dev); */
7511 flush_workqueue(priv->workqueue);
7512
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007513 /* ieee80211_unregister_hw calls iwl3945_mac_stop, which flushes
Zhu Yib481de92007-09-25 17:54:57 -07007514 * priv->workqueue... so we can't take down the workqueue
7515 * until now... */
7516 destroy_workqueue(priv->workqueue);
7517 priv->workqueue = NULL;
7518
Helmut Schaa26635162009-01-15 09:38:44 +01007519 free_irq(pdev->irq, priv);
7520 pci_disable_msi(pdev);
7521
Zhu Yib481de92007-09-25 17:54:57 -07007522 pci_iounmap(pdev, priv->hw_base);
7523 pci_release_regions(pdev);
7524 pci_disable_device(pdev);
7525 pci_set_drvdata(pdev, NULL);
7526
Reinette Chatre849e0dc2008-01-23 10:15:18 -08007527 iwl3945_free_channel_map(priv);
7528 iwl3945_free_geos(priv);
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08007529 kfree(priv->scan39);
Zhu Yib481de92007-09-25 17:54:57 -07007530 if (priv->ibss_beacon)
7531 dev_kfree_skb(priv->ibss_beacon);
7532
7533 ieee80211_free_hw(priv->hw);
7534}
7535
7536#ifdef CONFIG_PM
7537
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007538static int iwl3945_pci_suspend(struct pci_dev *pdev, pm_message_t state)
Zhu Yib481de92007-09-25 17:54:57 -07007539{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08007540 struct iwl_priv *priv = pci_get_drvdata(pdev);
Zhu Yib481de92007-09-25 17:54:57 -07007541
Zhu Yie655b9f2008-01-24 02:19:38 -08007542 if (priv->is_open) {
7543 set_bit(STATUS_IN_SUSPEND, &priv->status);
7544 iwl3945_mac_stop(priv->hw);
7545 priv->is_open = 1;
7546 }
Helmut Schaa26635162009-01-15 09:38:44 +01007547 pci_save_state(pdev);
7548 pci_disable_device(pdev);
Zhu Yib481de92007-09-25 17:54:57 -07007549 pci_set_power_state(pdev, PCI_D3hot);
7550
Zhu Yib481de92007-09-25 17:54:57 -07007551 return 0;
7552}
7553
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007554static int iwl3945_pci_resume(struct pci_dev *pdev)
Zhu Yib481de92007-09-25 17:54:57 -07007555{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08007556 struct iwl_priv *priv = pci_get_drvdata(pdev);
Zhu Yib481de92007-09-25 17:54:57 -07007557
Zhu Yib481de92007-09-25 17:54:57 -07007558 pci_set_power_state(pdev, PCI_D0);
Helmut Schaa26635162009-01-15 09:38:44 +01007559 pci_enable_device(pdev);
7560 pci_restore_state(pdev);
Zhu Yib481de92007-09-25 17:54:57 -07007561
Zhu Yie655b9f2008-01-24 02:19:38 -08007562 if (priv->is_open)
7563 iwl3945_mac_start(priv->hw);
Zhu Yib481de92007-09-25 17:54:57 -07007564
Zhu Yie655b9f2008-01-24 02:19:38 -08007565 clear_bit(STATUS_IN_SUSPEND, &priv->status);
Zhu Yib481de92007-09-25 17:54:57 -07007566 return 0;
7567}
7568
7569#endif /* CONFIG_PM */
7570
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08007571/*************** RFKILL FUNCTIONS **********/
Adel Gadllah80fcc9e2008-07-01 17:49:50 +02007572#ifdef CONFIG_IWL3945_RFKILL
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08007573/* software rf-kill from user */
7574static int iwl3945_rfkill_soft_rf_kill(void *data, enum rfkill_state state)
7575{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08007576 struct iwl_priv *priv = data;
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08007577 int err = 0;
7578
Adel Gadllah80fcc9e2008-07-01 17:49:50 +02007579 if (!priv->rfkill)
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08007580 return 0;
7581
7582 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
7583 return 0;
7584
Tomas Winklera96a27f2008-10-23 23:48:56 -07007585 IWL_DEBUG_RF_KILL("we received soft RFKILL set to state %d\n", state);
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08007586 mutex_lock(&priv->mutex);
7587
7588 switch (state) {
Zhu Yiacdfe9b2008-06-30 17:23:32 +08007589 case RFKILL_STATE_UNBLOCKED:
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08007590 if (iwl_is_rfkill_hw(priv)) {
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08007591 err = -EBUSY;
Adel Gadllah80fcc9e2008-07-01 17:49:50 +02007592 goto out_unlock;
7593 }
7594 iwl3945_radio_kill_sw(priv, 0);
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08007595 break;
Zhu Yiacdfe9b2008-06-30 17:23:32 +08007596 case RFKILL_STATE_SOFT_BLOCKED:
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08007597 iwl3945_radio_kill_sw(priv, 1);
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08007598 break;
Zhu Yiacdfe9b2008-06-30 17:23:32 +08007599 default:
Winkler, Tomas39aadf82008-12-19 10:37:32 +08007600 IWL_WARN(priv, "received unexpected RFKILL state %d\n", state);
Zhu Yiacdfe9b2008-06-30 17:23:32 +08007601 break;
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08007602 }
Adel Gadllah80fcc9e2008-07-01 17:49:50 +02007603out_unlock:
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08007604 mutex_unlock(&priv->mutex);
7605
7606 return err;
7607}
7608
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08007609int iwl3945_rfkill_init(struct iwl_priv *priv)
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08007610{
7611 struct device *device = wiphy_dev(priv->hw->wiphy);
7612 int ret = 0;
7613
7614 BUG_ON(device == NULL);
7615
7616 IWL_DEBUG_RF_KILL("Initializing RFKILL.\n");
Adel Gadllah80fcc9e2008-07-01 17:49:50 +02007617 priv->rfkill = rfkill_allocate(device, RFKILL_TYPE_WLAN);
7618 if (!priv->rfkill) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08007619 IWL_ERR(priv, "Unable to allocate rfkill device.\n");
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08007620 ret = -ENOMEM;
7621 goto error;
7622 }
7623
Adel Gadllah80fcc9e2008-07-01 17:49:50 +02007624 priv->rfkill->name = priv->cfg->name;
7625 priv->rfkill->data = priv;
7626 priv->rfkill->state = RFKILL_STATE_UNBLOCKED;
7627 priv->rfkill->toggle_radio = iwl3945_rfkill_soft_rf_kill;
7628 priv->rfkill->user_claim_unsupported = 1;
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08007629
Adel Gadllah80fcc9e2008-07-01 17:49:50 +02007630 priv->rfkill->dev.class->suspend = NULL;
7631 priv->rfkill->dev.class->resume = NULL;
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08007632
Adel Gadllah80fcc9e2008-07-01 17:49:50 +02007633 ret = rfkill_register(priv->rfkill);
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08007634 if (ret) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08007635 IWL_ERR(priv, "Unable to register rfkill: %d\n", ret);
Adel Gadllah80fcc9e2008-07-01 17:49:50 +02007636 goto freed_rfkill;
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08007637 }
7638
7639 IWL_DEBUG_RF_KILL("RFKILL initialization complete.\n");
7640 return ret;
7641
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08007642freed_rfkill:
Adel Gadllah80fcc9e2008-07-01 17:49:50 +02007643 if (priv->rfkill != NULL)
7644 rfkill_free(priv->rfkill);
7645 priv->rfkill = NULL;
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08007646
7647error:
7648 IWL_DEBUG_RF_KILL("RFKILL initialization complete.\n");
7649 return ret;
7650}
7651
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08007652void iwl3945_rfkill_unregister(struct iwl_priv *priv)
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08007653{
Adel Gadllah80fcc9e2008-07-01 17:49:50 +02007654 if (priv->rfkill)
7655 rfkill_unregister(priv->rfkill);
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08007656
Adel Gadllah80fcc9e2008-07-01 17:49:50 +02007657 priv->rfkill = NULL;
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08007658}
7659
7660/* set rf-kill to the right state. */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08007661void iwl3945_rfkill_set_hw_state(struct iwl_priv *priv)
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08007662{
7663
Adel Gadllah80fcc9e2008-07-01 17:49:50 +02007664 if (!priv->rfkill)
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08007665 return;
7666
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08007667 if (iwl_is_rfkill_hw(priv)) {
Adel Gadllah80fcc9e2008-07-01 17:49:50 +02007668 rfkill_force_state(priv->rfkill, RFKILL_STATE_HARD_BLOCKED);
7669 return;
7670 }
7671
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08007672 if (!iwl_is_rfkill_sw(priv))
Adel Gadllah80fcc9e2008-07-01 17:49:50 +02007673 rfkill_force_state(priv->rfkill, RFKILL_STATE_UNBLOCKED);
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08007674 else
Adel Gadllah80fcc9e2008-07-01 17:49:50 +02007675 rfkill_force_state(priv->rfkill, RFKILL_STATE_SOFT_BLOCKED);
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08007676}
7677#endif
7678
Zhu Yib481de92007-09-25 17:54:57 -07007679/*****************************************************************************
7680 *
7681 * driver and module entry point
7682 *
7683 *****************************************************************************/
7684
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007685static struct pci_driver iwl3945_driver = {
Zhu Yib481de92007-09-25 17:54:57 -07007686 .name = DRV_NAME,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007687 .id_table = iwl3945_hw_card_ids,
7688 .probe = iwl3945_pci_probe,
7689 .remove = __devexit_p(iwl3945_pci_remove),
Zhu Yib481de92007-09-25 17:54:57 -07007690#ifdef CONFIG_PM
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007691 .suspend = iwl3945_pci_suspend,
7692 .resume = iwl3945_pci_resume,
Zhu Yib481de92007-09-25 17:54:57 -07007693#endif
7694};
7695
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007696static int __init iwl3945_init(void)
Zhu Yib481de92007-09-25 17:54:57 -07007697{
7698
7699 int ret;
7700 printk(KERN_INFO DRV_NAME ": " DRV_DESCRIPTION ", " DRV_VERSION "\n");
7701 printk(KERN_INFO DRV_NAME ": " DRV_COPYRIGHT "\n");
Reinette Chatre897e1cf2008-03-28 16:21:09 -07007702
7703 ret = iwl3945_rate_control_register();
7704 if (ret) {
Samuel Ortiza3139c52008-12-19 10:37:09 +08007705 printk(KERN_ERR DRV_NAME
7706 "Unable to register rate control algorithm: %d\n", ret);
Reinette Chatre897e1cf2008-03-28 16:21:09 -07007707 return ret;
7708 }
7709
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007710 ret = pci_register_driver(&iwl3945_driver);
Zhu Yib481de92007-09-25 17:54:57 -07007711 if (ret) {
Samuel Ortiza3139c52008-12-19 10:37:09 +08007712 printk(KERN_ERR DRV_NAME "Unable to initialize PCI module\n");
Reinette Chatre897e1cf2008-03-28 16:21:09 -07007713 goto error_register;
Zhu Yib481de92007-09-25 17:54:57 -07007714 }
Zhu Yib481de92007-09-25 17:54:57 -07007715
7716 return ret;
Reinette Chatre897e1cf2008-03-28 16:21:09 -07007717
Reinette Chatre897e1cf2008-03-28 16:21:09 -07007718error_register:
7719 iwl3945_rate_control_unregister();
7720 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07007721}
7722
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007723static void __exit iwl3945_exit(void)
Zhu Yib481de92007-09-25 17:54:57 -07007724{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007725 pci_unregister_driver(&iwl3945_driver);
Reinette Chatre897e1cf2008-03-28 16:21:09 -07007726 iwl3945_rate_control_unregister();
Zhu Yib481de92007-09-25 17:54:57 -07007727}
7728
Reinette Chatrea0987a82008-12-02 12:14:06 -08007729MODULE_FIRMWARE(IWL3945_MODULE_FIRMWARE(IWL3945_UCODE_API_MAX));
Zhu Yi25cb6ca2008-09-11 11:45:22 +08007730
Kolekar, Abhijeetdf878d82008-12-19 10:37:35 +08007731module_param_named(antenna, iwl3945_mod_params.antenna, int, 0444);
Zhu Yib481de92007-09-25 17:54:57 -07007732MODULE_PARM_DESC(antenna, "select antenna (1=Main, 2=Aux, default 0 [both])");
Kolekar, Abhijeetdf878d82008-12-19 10:37:35 +08007733module_param_named(disable, iwl3945_mod_params.disable, int, 0444);
Zhu Yib481de92007-09-25 17:54:57 -07007734MODULE_PARM_DESC(disable, "manually disable the radio (default 0 [radio on])");
Samuel Ortiz9c74d9f2009-01-08 10:19:59 -08007735module_param_named(swcrypto, iwl3945_mod_params.sw_crypto, int, 0444);
7736MODULE_PARM_DESC(swcrypto,
7737 "using software crypto (default 1 [software])\n");
Kolekar, Abhijeetdf878d82008-12-19 10:37:35 +08007738module_param_named(debug, iwl3945_mod_params.debug, uint, 0444);
Zhu Yib481de92007-09-25 17:54:57 -07007739MODULE_PARM_DESC(debug, "debug output mask");
Kolekar, Abhijeetdf878d82008-12-19 10:37:35 +08007740module_param_named(disable_hw_scan, iwl3945_mod_params.disable_hw_scan, int, 0444);
Zhu Yib481de92007-09-25 17:54:57 -07007741MODULE_PARM_DESC(disable_hw_scan, "disable hardware scanning (default 0)");
7742
Kolekar, Abhijeetdf878d82008-12-19 10:37:35 +08007743module_param_named(queues_num, iwl3945_mod_params.num_of_queues, int, 0444);
Zhu Yib481de92007-09-25 17:54:57 -07007744MODULE_PARM_DESC(queues_num, "number of hw queues.");
7745
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007746module_exit(iwl3945_exit);
7747module_init(iwl3945_init);