blob: c8a1bdeaa435c7e6fb17f044532dea6a918c5789 [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
Samuel Ortiz518099a2009-01-19 15:30:27 -0800437int iwl3945_send_statistics_request(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700438{
Samuel Ortiz518099a2009-01-19 15:30:27 -0800439 u32 val = 0;
Zhu Yib481de92007-09-25 17:54:57 -0700440
Winkler, Tomasc2d79b42008-12-19 10:37:34 +0800441 struct iwl_host_cmd cmd = {
Samuel Ortiz518099a2009-01-19 15:30:27 -0800442 .id = REPLY_STATISTICS_CMD,
Zhu Yib481de92007-09-25 17:54:57 -0700443 .len = sizeof(val),
444 .data = &val,
445 };
446
Samuel Ortiz518099a2009-01-19 15:30:27 -0800447 return iwl_send_cmd_sync(priv, &cmd);
Zhu Yib481de92007-09-25 17:54:57 -0700448}
449
450/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800451 * iwl3945_set_rxon_channel - Set the phymode and channel values in staging RXON
Johannes Berg8318d782008-01-24 19:38:38 +0100452 * @band: 2.4 or 5 GHz band
453 * @channel: Any channel valid for the requested band
Zhu Yib481de92007-09-25 17:54:57 -0700454
Johannes Berg8318d782008-01-24 19:38:38 +0100455 * In addition to setting the staging RXON, priv->band is also set.
Zhu Yib481de92007-09-25 17:54:57 -0700456 *
457 * NOTE: Does not commit to the hardware; it sets appropriate bit fields
Johannes Berg8318d782008-01-24 19:38:38 +0100458 * in the staging RXON flag structure based on the band
Zhu Yib481de92007-09-25 17:54:57 -0700459 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800460static int iwl3945_set_rxon_channel(struct iwl_priv *priv,
Johannes Berg8318d782008-01-24 19:38:38 +0100461 enum ieee80211_band band,
462 u16 channel)
Zhu Yib481de92007-09-25 17:54:57 -0700463{
Johannes Berg8318d782008-01-24 19:38:38 +0100464 if (!iwl3945_get_channel_info(priv, band, channel)) {
Zhu Yib481de92007-09-25 17:54:57 -0700465 IWL_DEBUG_INFO("Could not set channel to %d [%d]\n",
Johannes Berg8318d782008-01-24 19:38:38 +0100466 channel, band);
Zhu Yib481de92007-09-25 17:54:57 -0700467 return -EINVAL;
468 }
469
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800470 if ((le16_to_cpu(priv->staging39_rxon.channel) == channel) &&
Johannes Berg8318d782008-01-24 19:38:38 +0100471 (priv->band == band))
Zhu Yib481de92007-09-25 17:54:57 -0700472 return 0;
473
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800474 priv->staging39_rxon.channel = cpu_to_le16(channel);
Johannes Berg8318d782008-01-24 19:38:38 +0100475 if (band == IEEE80211_BAND_5GHZ)
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800476 priv->staging39_rxon.flags &= ~RXON_FLG_BAND_24G_MSK;
Zhu Yib481de92007-09-25 17:54:57 -0700477 else
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800478 priv->staging39_rxon.flags |= RXON_FLG_BAND_24G_MSK;
Zhu Yib481de92007-09-25 17:54:57 -0700479
Johannes Berg8318d782008-01-24 19:38:38 +0100480 priv->band = band;
Zhu Yib481de92007-09-25 17:54:57 -0700481
Johannes Berg8318d782008-01-24 19:38:38 +0100482 IWL_DEBUG_INFO("Staging channel set to %d [%d]\n", channel, band);
Zhu Yib481de92007-09-25 17:54:57 -0700483
484 return 0;
485}
486
487/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800488 * iwl3945_check_rxon_cmd - validate RXON structure is valid
Zhu Yib481de92007-09-25 17:54:57 -0700489 *
490 * NOTE: This is really only useful during development and can eventually
491 * be #ifdef'd out once the driver is stable and folks aren't actively
492 * making changes
493 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800494static int iwl3945_check_rxon_cmd(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700495{
496 int error = 0;
497 int counter = 1;
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800498 struct iwl3945_rxon_cmd *rxon = &priv->staging39_rxon;
Zhu Yib481de92007-09-25 17:54:57 -0700499
500 if (rxon->flags & RXON_FLG_BAND_24G_MSK) {
501 error |= le32_to_cpu(rxon->flags &
502 (RXON_FLG_TGJ_NARROW_BAND_MSK |
503 RXON_FLG_RADAR_DETECT_MSK));
504 if (error)
Winkler, Tomas39aadf82008-12-19 10:37:32 +0800505 IWL_WARN(priv, "check 24G fields %d | %d\n",
Zhu Yib481de92007-09-25 17:54:57 -0700506 counter++, error);
507 } else {
508 error |= (rxon->flags & RXON_FLG_SHORT_SLOT_MSK) ?
509 0 : le32_to_cpu(RXON_FLG_SHORT_SLOT_MSK);
510 if (error)
Winkler, Tomas39aadf82008-12-19 10:37:32 +0800511 IWL_WARN(priv, "check 52 fields %d | %d\n",
Zhu Yib481de92007-09-25 17:54:57 -0700512 counter++, error);
513 error |= le32_to_cpu(rxon->flags & RXON_FLG_CCK_MSK);
514 if (error)
Winkler, Tomas39aadf82008-12-19 10:37:32 +0800515 IWL_WARN(priv, "check 52 CCK %d | %d\n",
Zhu Yib481de92007-09-25 17:54:57 -0700516 counter++, error);
517 }
518 error |= (rxon->node_addr[0] | rxon->bssid_addr[0]) & 0x1;
519 if (error)
Winkler, Tomas39aadf82008-12-19 10:37:32 +0800520 IWL_WARN(priv, "check mac addr %d | %d\n", counter++, error);
Zhu Yib481de92007-09-25 17:54:57 -0700521
522 /* make sure basic rates 6Mbps and 1Mbps are supported */
523 error |= (((rxon->ofdm_basic_rates & IWL_RATE_6M_MASK) == 0) &&
524 ((rxon->cck_basic_rates & IWL_RATE_1M_MASK) == 0));
525 if (error)
Winkler, Tomas39aadf82008-12-19 10:37:32 +0800526 IWL_WARN(priv, "check basic rate %d | %d\n", counter++, error);
Zhu Yib481de92007-09-25 17:54:57 -0700527
528 error |= (le16_to_cpu(rxon->assoc_id) > 2007);
529 if (error)
Winkler, Tomas39aadf82008-12-19 10:37:32 +0800530 IWL_WARN(priv, "check assoc id %d | %d\n", counter++, error);
Zhu Yib481de92007-09-25 17:54:57 -0700531
532 error |= ((rxon->flags & (RXON_FLG_CCK_MSK | RXON_FLG_SHORT_SLOT_MSK))
533 == (RXON_FLG_CCK_MSK | RXON_FLG_SHORT_SLOT_MSK));
534 if (error)
Winkler, Tomas39aadf82008-12-19 10:37:32 +0800535 IWL_WARN(priv, "check CCK and short slot %d | %d\n",
Zhu Yib481de92007-09-25 17:54:57 -0700536 counter++, error);
537
538 error |= ((rxon->flags & (RXON_FLG_CCK_MSK | RXON_FLG_AUTO_DETECT_MSK))
539 == (RXON_FLG_CCK_MSK | RXON_FLG_AUTO_DETECT_MSK));
540 if (error)
Winkler, Tomas39aadf82008-12-19 10:37:32 +0800541 IWL_WARN(priv, "check CCK & auto detect %d | %d\n",
Zhu Yib481de92007-09-25 17:54:57 -0700542 counter++, error);
543
544 error |= ((rxon->flags & (RXON_FLG_AUTO_DETECT_MSK |
545 RXON_FLG_TGG_PROTECT_MSK)) == RXON_FLG_TGG_PROTECT_MSK);
546 if (error)
Winkler, Tomas39aadf82008-12-19 10:37:32 +0800547 IWL_WARN(priv, "check TGG and auto detect %d | %d\n",
Zhu Yib481de92007-09-25 17:54:57 -0700548 counter++, error);
549
550 if ((rxon->flags & RXON_FLG_DIS_DIV_MSK))
551 error |= ((rxon->flags & (RXON_FLG_ANT_B_MSK |
552 RXON_FLG_ANT_A_MSK)) == 0);
553 if (error)
Winkler, Tomas39aadf82008-12-19 10:37:32 +0800554 IWL_WARN(priv, "check antenna %d %d\n", counter++, error);
Zhu Yib481de92007-09-25 17:54:57 -0700555
556 if (error)
Winkler, Tomas39aadf82008-12-19 10:37:32 +0800557 IWL_WARN(priv, "Tuning to channel %d\n",
Zhu Yib481de92007-09-25 17:54:57 -0700558 le16_to_cpu(rxon->channel));
559
560 if (error) {
Winkler, Tomas15b16872008-12-19 10:37:33 +0800561 IWL_ERR(priv, "Not a valid rxon_assoc_cmd field values\n");
Zhu Yib481de92007-09-25 17:54:57 -0700562 return -1;
563 }
564 return 0;
565}
566
567/**
Ben Cahill9fbab512007-11-29 11:09:47 +0800568 * iwl3945_full_rxon_required - check if full RXON (vs RXON_ASSOC) cmd is needed
Ian Schram01ebd062007-10-25 17:15:22 +0800569 * @priv: staging_rxon is compared to active_rxon
Zhu Yib481de92007-09-25 17:54:57 -0700570 *
Ben Cahill9fbab512007-11-29 11:09:47 +0800571 * If the RXON structure is changing enough to require a new tune,
572 * or is clearing the RXON_FILTER_ASSOC_MSK, then return 1 to indicate that
573 * a new tune (full RXON command, rather than RXON_ASSOC cmd) is required.
Zhu Yib481de92007-09-25 17:54:57 -0700574 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800575static int iwl3945_full_rxon_required(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700576{
577
578 /* These items are only settable from the full RXON command */
Ron Rindjunsky5d1e2322008-06-30 17:23:04 +0800579 if (!(iwl3945_is_associated(priv)) ||
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800580 compare_ether_addr(priv->staging39_rxon.bssid_addr,
581 priv->active39_rxon.bssid_addr) ||
582 compare_ether_addr(priv->staging39_rxon.node_addr,
583 priv->active39_rxon.node_addr) ||
584 compare_ether_addr(priv->staging39_rxon.wlap_bssid_addr,
585 priv->active39_rxon.wlap_bssid_addr) ||
586 (priv->staging39_rxon.dev_type != priv->active39_rxon.dev_type) ||
587 (priv->staging39_rxon.channel != priv->active39_rxon.channel) ||
588 (priv->staging39_rxon.air_propagation !=
589 priv->active39_rxon.air_propagation) ||
590 (priv->staging39_rxon.assoc_id != priv->active39_rxon.assoc_id))
Zhu Yib481de92007-09-25 17:54:57 -0700591 return 1;
592
593 /* flags, filter_flags, ofdm_basic_rates, and cck_basic_rates can
594 * be updated with the RXON_ASSOC command -- however only some
595 * flag transitions are allowed using RXON_ASSOC */
596
597 /* Check if we are not switching bands */
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800598 if ((priv->staging39_rxon.flags & RXON_FLG_BAND_24G_MSK) !=
599 (priv->active39_rxon.flags & RXON_FLG_BAND_24G_MSK))
Zhu Yib481de92007-09-25 17:54:57 -0700600 return 1;
601
602 /* Check if we are switching association toggle */
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800603 if ((priv->staging39_rxon.filter_flags & RXON_FILTER_ASSOC_MSK) !=
604 (priv->active39_rxon.filter_flags & RXON_FILTER_ASSOC_MSK))
Zhu Yib481de92007-09-25 17:54:57 -0700605 return 1;
606
607 return 0;
608}
609
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800610static int iwl3945_send_rxon_assoc(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700611{
612 int rc = 0;
Tomas Winkler3d24a9f2008-12-19 10:37:07 +0800613 struct iwl_rx_packet *res = NULL;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800614 struct iwl3945_rxon_assoc_cmd rxon_assoc;
Winkler, Tomasc2d79b42008-12-19 10:37:34 +0800615 struct iwl_host_cmd cmd = {
Zhu Yib481de92007-09-25 17:54:57 -0700616 .id = REPLY_RXON_ASSOC,
617 .len = sizeof(rxon_assoc),
618 .meta.flags = CMD_WANT_SKB,
619 .data = &rxon_assoc,
620 };
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800621 const struct iwl3945_rxon_cmd *rxon1 = &priv->staging39_rxon;
622 const struct iwl3945_rxon_cmd *rxon2 = &priv->active39_rxon;
Zhu Yib481de92007-09-25 17:54:57 -0700623
624 if ((rxon1->flags == rxon2->flags) &&
625 (rxon1->filter_flags == rxon2->filter_flags) &&
626 (rxon1->cck_basic_rates == rxon2->cck_basic_rates) &&
627 (rxon1->ofdm_basic_rates == rxon2->ofdm_basic_rates)) {
628 IWL_DEBUG_INFO("Using current RXON_ASSOC. Not resending.\n");
629 return 0;
630 }
631
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800632 rxon_assoc.flags = priv->staging39_rxon.flags;
633 rxon_assoc.filter_flags = priv->staging39_rxon.filter_flags;
634 rxon_assoc.ofdm_basic_rates = priv->staging39_rxon.ofdm_basic_rates;
635 rxon_assoc.cck_basic_rates = priv->staging39_rxon.cck_basic_rates;
Zhu Yib481de92007-09-25 17:54:57 -0700636 rxon_assoc.reserved = 0;
637
Samuel Ortiz518099a2009-01-19 15:30:27 -0800638 rc = iwl_send_cmd_sync(priv, &cmd);
Zhu Yib481de92007-09-25 17:54:57 -0700639 if (rc)
640 return rc;
641
Tomas Winkler3d24a9f2008-12-19 10:37:07 +0800642 res = (struct iwl_rx_packet *)cmd.meta.u.skb->data;
Zhu Yib481de92007-09-25 17:54:57 -0700643 if (res->hdr.flags & IWL_CMD_FAILED_MSK) {
Winkler, Tomas15b16872008-12-19 10:37:33 +0800644 IWL_ERR(priv, "Bad return from REPLY_RXON_ASSOC command\n");
Zhu Yib481de92007-09-25 17:54:57 -0700645 rc = -EIO;
646 }
647
648 priv->alloc_rxb_skb--;
649 dev_kfree_skb_any(cmd.meta.u.skb);
650
651 return rc;
652}
653
654/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800655 * iwl3945_commit_rxon - commit staging_rxon to hardware
Zhu Yib481de92007-09-25 17:54:57 -0700656 *
Ian Schram01ebd062007-10-25 17:15:22 +0800657 * The RXON command in staging_rxon is committed to the hardware and
Zhu Yib481de92007-09-25 17:54:57 -0700658 * the active_rxon structure is updated with the new data. This
659 * function correctly transitions out of the RXON_ASSOC_MSK state if
660 * a HW tune is required based on the RXON structure changes.
661 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800662static int iwl3945_commit_rxon(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700663{
664 /* cast away the const for active_rxon in this function */
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800665 struct iwl3945_rxon_cmd *active_rxon = (void *)&priv->active39_rxon;
Zhu Yib481de92007-09-25 17:54:57 -0700666 int rc = 0;
667
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +0800668 if (!iwl_is_alive(priv))
Zhu Yib481de92007-09-25 17:54:57 -0700669 return -1;
670
671 /* always get timestamp with Rx frame */
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800672 priv->staging39_rxon.flags |= RXON_FLG_TSF2HOST_MSK;
Zhu Yib481de92007-09-25 17:54:57 -0700673
674 /* select antenna */
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800675 priv->staging39_rxon.flags &=
Zhu Yib481de92007-09-25 17:54:57 -0700676 ~(RXON_FLG_DIS_DIV_MSK | RXON_FLG_ANT_SEL_MSK);
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800677 priv->staging39_rxon.flags |= iwl3945_get_antenna_flags(priv);
Zhu Yib481de92007-09-25 17:54:57 -0700678
Samuel Ortiza3139c52008-12-19 10:37:09 +0800679 rc = iwl3945_check_rxon_cmd(priv);
Zhu Yib481de92007-09-25 17:54:57 -0700680 if (rc) {
Winkler, Tomas15b16872008-12-19 10:37:33 +0800681 IWL_ERR(priv, "Invalid RXON configuration. Not committing.\n");
Zhu Yib481de92007-09-25 17:54:57 -0700682 return -EINVAL;
683 }
684
685 /* If we don't need to send a full RXON, we can use
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800686 * iwl3945_rxon_assoc_cmd which is used to reconfigure filter
Zhu Yib481de92007-09-25 17:54:57 -0700687 * and other flags for the current radio configuration. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800688 if (!iwl3945_full_rxon_required(priv)) {
689 rc = iwl3945_send_rxon_assoc(priv);
Zhu Yib481de92007-09-25 17:54:57 -0700690 if (rc) {
Winkler, Tomas15b16872008-12-19 10:37:33 +0800691 IWL_ERR(priv, "Error setting RXON_ASSOC "
Zhu Yib481de92007-09-25 17:54:57 -0700692 "configuration (%d).\n", rc);
693 return rc;
694 }
695
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800696 memcpy(active_rxon, &priv->staging39_rxon, sizeof(*active_rxon));
Zhu Yib481de92007-09-25 17:54:57 -0700697
698 return 0;
699 }
700
701 /* If we are currently associated and the new config requires
702 * an RXON_ASSOC and the new config wants the associated mask enabled,
703 * we must clear the associated from the active configuration
704 * before we apply the new config */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800705 if (iwl3945_is_associated(priv) &&
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800706 (priv->staging39_rxon.filter_flags & RXON_FILTER_ASSOC_MSK)) {
Zhu Yib481de92007-09-25 17:54:57 -0700707 IWL_DEBUG_INFO("Toggling associated bit on current RXON\n");
708 active_rxon->filter_flags &= ~RXON_FILTER_ASSOC_MSK;
709
Samuel Ortiz518099a2009-01-19 15:30:27 -0800710 rc = iwl_send_cmd_pdu(priv, REPLY_RXON,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800711 sizeof(struct iwl3945_rxon_cmd),
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800712 &priv->active39_rxon);
Zhu Yib481de92007-09-25 17:54:57 -0700713
714 /* If the mask clearing failed then we set
715 * active_rxon back to what it was previously */
716 if (rc) {
717 active_rxon->filter_flags |= RXON_FILTER_ASSOC_MSK;
Winkler, Tomas15b16872008-12-19 10:37:33 +0800718 IWL_ERR(priv, "Error clearing ASSOC_MSK on current "
Zhu Yib481de92007-09-25 17:54:57 -0700719 "configuration (%d).\n", rc);
720 return rc;
721 }
Zhu Yib481de92007-09-25 17:54:57 -0700722 }
723
724 IWL_DEBUG_INFO("Sending RXON\n"
725 "* with%s RXON_FILTER_ASSOC_MSK\n"
726 "* channel = %d\n"
Johannes Berge1749612008-10-27 15:59:26 -0700727 "* bssid = %pM\n",
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800728 ((priv->staging39_rxon.filter_flags &
Zhu Yib481de92007-09-25 17:54:57 -0700729 RXON_FILTER_ASSOC_MSK) ? "" : "out"),
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800730 le16_to_cpu(priv->staging39_rxon.channel),
Johannes Berge1749612008-10-27 15:59:26 -0700731 priv->staging_rxon.bssid_addr);
Zhu Yib481de92007-09-25 17:54:57 -0700732
733 /* Apply the new configuration */
Samuel Ortiz518099a2009-01-19 15:30:27 -0800734 rc = iwl_send_cmd_pdu(priv, REPLY_RXON,
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800735 sizeof(struct iwl3945_rxon_cmd), &priv->staging39_rxon);
Zhu Yib481de92007-09-25 17:54:57 -0700736 if (rc) {
Winkler, Tomas15b16872008-12-19 10:37:33 +0800737 IWL_ERR(priv, "Error setting new configuration (%d).\n", rc);
Zhu Yib481de92007-09-25 17:54:57 -0700738 return rc;
739 }
740
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800741 memcpy(active_rxon, &priv->staging39_rxon, sizeof(*active_rxon));
Zhu Yib481de92007-09-25 17:54:57 -0700742
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800743 iwl3945_clear_stations_table(priv);
Zhu Yi556f8db2007-09-27 11:27:33 +0800744
Zhu Yib481de92007-09-25 17:54:57 -0700745 /* If we issue a new RXON command which required a tune then we must
746 * send a new TXPOWER command or we won't be able to Tx any frames */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800747 rc = iwl3945_hw_reg_send_txpower(priv);
Zhu Yib481de92007-09-25 17:54:57 -0700748 if (rc) {
Winkler, Tomas15b16872008-12-19 10:37:33 +0800749 IWL_ERR(priv, "Error setting Tx power (%d).\n", rc);
Zhu Yib481de92007-09-25 17:54:57 -0700750 return rc;
751 }
752
753 /* Add the broadcast address so we can send broadcast frames */
Kolekar, Abhijeetb5323d32008-12-19 10:37:22 +0800754 if (iwl3945_add_station(priv, iwl_bcast_addr, 0, 0) ==
Zhu Yib481de92007-09-25 17:54:57 -0700755 IWL_INVALID_STATION) {
Winkler, Tomas15b16872008-12-19 10:37:33 +0800756 IWL_ERR(priv, "Error adding BROADCAST address for transmit.\n");
Zhu Yib481de92007-09-25 17:54:57 -0700757 return -EIO;
758 }
759
760 /* If we have set the ASSOC_MSK and we are in BSS mode then
761 * add the IWL_AP_ID to the station rate table */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800762 if (iwl3945_is_associated(priv) &&
Johannes Berg05c914f2008-09-11 00:01:58 +0200763 (priv->iw_mode == NL80211_IFTYPE_STATION))
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800764 if (iwl3945_add_station(priv, priv->active39_rxon.bssid_addr, 1, 0)
Zhu Yib481de92007-09-25 17:54:57 -0700765 == IWL_INVALID_STATION) {
Winkler, Tomas15b16872008-12-19 10:37:33 +0800766 IWL_ERR(priv, "Error adding AP address for transmit\n");
Zhu Yib481de92007-09-25 17:54:57 -0700767 return -EIO;
768 }
769
Johannes Berg8318d782008-01-24 19:38:38 +0100770 /* Init the hardware's rate fallback order based on the band */
Zhu Yib481de92007-09-25 17:54:57 -0700771 rc = iwl3945_init_hw_rate_table(priv);
772 if (rc) {
Winkler, Tomas15b16872008-12-19 10:37:33 +0800773 IWL_ERR(priv, "Error setting HW rate table: %02X\n", rc);
Zhu Yib481de92007-09-25 17:54:57 -0700774 return -EIO;
775 }
776
777 return 0;
778}
779
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800780static int iwl3945_send_bt_config(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700781{
Tomas Winkler4c897252008-12-19 10:37:05 +0800782 struct iwl_bt_cmd bt_cmd = {
Zhu Yib481de92007-09-25 17:54:57 -0700783 .flags = 3,
784 .lead_time = 0xAA,
785 .max_kill = 1,
786 .kill_ack_mask = 0,
787 .kill_cts_mask = 0,
788 };
789
Samuel Ortiz518099a2009-01-19 15:30:27 -0800790 return iwl_send_cmd_pdu(priv, REPLY_BT_CONFIG,
Tomas Winkler4c897252008-12-19 10:37:05 +0800791 sizeof(bt_cmd), &bt_cmd);
Zhu Yib481de92007-09-25 17:54:57 -0700792}
793
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800794static int iwl3945_send_scan_abort(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700795{
796 int rc = 0;
Tomas Winkler3d24a9f2008-12-19 10:37:07 +0800797 struct iwl_rx_packet *res;
Winkler, Tomasc2d79b42008-12-19 10:37:34 +0800798 struct iwl_host_cmd cmd = {
Zhu Yib481de92007-09-25 17:54:57 -0700799 .id = REPLY_SCAN_ABORT_CMD,
800 .meta.flags = CMD_WANT_SKB,
801 };
802
803 /* If there isn't a scan actively going on in the hardware
804 * then we are in between scan bands and not actually
805 * actively scanning, so don't send the abort command */
806 if (!test_bit(STATUS_SCAN_HW, &priv->status)) {
807 clear_bit(STATUS_SCAN_ABORTING, &priv->status);
808 return 0;
809 }
810
Samuel Ortiz518099a2009-01-19 15:30:27 -0800811 rc = iwl_send_cmd_sync(priv, &cmd);
Zhu Yib481de92007-09-25 17:54:57 -0700812 if (rc) {
813 clear_bit(STATUS_SCAN_ABORTING, &priv->status);
814 return rc;
815 }
816
Tomas Winkler3d24a9f2008-12-19 10:37:07 +0800817 res = (struct iwl_rx_packet *)cmd.meta.u.skb->data;
Zhu Yib481de92007-09-25 17:54:57 -0700818 if (res->u.status != CAN_ABORT_STATUS) {
819 /* The scan abort will return 1 for success or
820 * 2 for "failure". A failure condition can be
821 * due to simply not being in an active scan which
822 * can occur if we send the scan abort before we
823 * the microcode has notified us that a scan is
824 * completed. */
825 IWL_DEBUG_INFO("SCAN_ABORT returned %d.\n", res->u.status);
826 clear_bit(STATUS_SCAN_ABORTING, &priv->status);
827 clear_bit(STATUS_SCAN_HW, &priv->status);
828 }
829
830 dev_kfree_skb_any(cmd.meta.u.skb);
831
832 return rc;
833}
834
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800835static int iwl3945_add_sta_sync_callback(struct iwl_priv *priv,
Winkler, Tomasc2d79b42008-12-19 10:37:34 +0800836 struct iwl_cmd *cmd, struct sk_buff *skb)
Zhu Yib481de92007-09-25 17:54:57 -0700837{
Tomas Winkler3d24a9f2008-12-19 10:37:07 +0800838 struct iwl_rx_packet *res = NULL;
Zhu Yib481de92007-09-25 17:54:57 -0700839
840 if (!skb) {
Winkler, Tomas15b16872008-12-19 10:37:33 +0800841 IWL_ERR(priv, "Error: Response NULL in REPLY_ADD_STA.\n");
Zhu Yib481de92007-09-25 17:54:57 -0700842 return 1;
843 }
844
Tomas Winkler3d24a9f2008-12-19 10:37:07 +0800845 res = (struct iwl_rx_packet *)skb->data;
Zhu Yib481de92007-09-25 17:54:57 -0700846 if (res->hdr.flags & IWL_CMD_FAILED_MSK) {
Winkler, Tomas15b16872008-12-19 10:37:33 +0800847 IWL_ERR(priv, "Bad return from REPLY_ADD_STA (0x%08X)\n",
Zhu Yib481de92007-09-25 17:54:57 -0700848 res->hdr.flags);
849 return 1;
850 }
851
852 switch (res->u.add_sta.status) {
853 case ADD_STA_SUCCESS_MSK:
854 break;
855 default:
856 break;
857 }
858
859 /* We didn't cache the SKB; let the caller free it */
860 return 1;
861}
862
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800863int iwl3945_send_add_station(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800864 struct iwl3945_addsta_cmd *sta, u8 flags)
Zhu Yib481de92007-09-25 17:54:57 -0700865{
Tomas Winkler3d24a9f2008-12-19 10:37:07 +0800866 struct iwl_rx_packet *res = NULL;
Zhu Yib481de92007-09-25 17:54:57 -0700867 int rc = 0;
Winkler, Tomasc2d79b42008-12-19 10:37:34 +0800868 struct iwl_host_cmd cmd = {
Zhu Yib481de92007-09-25 17:54:57 -0700869 .id = REPLY_ADD_STA,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800870 .len = sizeof(struct iwl3945_addsta_cmd),
Zhu Yib481de92007-09-25 17:54:57 -0700871 .meta.flags = flags,
872 .data = sta,
873 };
874
875 if (flags & CMD_ASYNC)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800876 cmd.meta.u.callback = iwl3945_add_sta_sync_callback;
Zhu Yib481de92007-09-25 17:54:57 -0700877 else
878 cmd.meta.flags |= CMD_WANT_SKB;
879
Samuel Ortiz518099a2009-01-19 15:30:27 -0800880 rc = iwl_send_cmd(priv, &cmd);
Zhu Yib481de92007-09-25 17:54:57 -0700881
882 if (rc || (flags & CMD_ASYNC))
883 return rc;
884
Tomas Winkler3d24a9f2008-12-19 10:37:07 +0800885 res = (struct iwl_rx_packet *)cmd.meta.u.skb->data;
Zhu Yib481de92007-09-25 17:54:57 -0700886 if (res->hdr.flags & IWL_CMD_FAILED_MSK) {
Winkler, Tomas15b16872008-12-19 10:37:33 +0800887 IWL_ERR(priv, "Bad return from REPLY_ADD_STA (0x%08X)\n",
Zhu Yib481de92007-09-25 17:54:57 -0700888 res->hdr.flags);
889 rc = -EIO;
890 }
891
892 if (rc == 0) {
893 switch (res->u.add_sta.status) {
894 case ADD_STA_SUCCESS_MSK:
895 IWL_DEBUG_INFO("REPLY_ADD_STA PASSED\n");
896 break;
897 default:
898 rc = -EIO;
Winkler, Tomas39aadf82008-12-19 10:37:32 +0800899 IWL_WARN(priv, "REPLY_ADD_STA failed\n");
Zhu Yib481de92007-09-25 17:54:57 -0700900 break;
901 }
902 }
903
904 priv->alloc_rxb_skb--;
905 dev_kfree_skb_any(cmd.meta.u.skb);
906
907 return rc;
908}
909
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800910static int iwl3945_update_sta_key_info(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -0700911 struct ieee80211_key_conf *keyconf,
912 u8 sta_id)
913{
914 unsigned long flags;
915 __le16 key_flags = 0;
916
917 switch (keyconf->alg) {
918 case ALG_CCMP:
919 key_flags |= STA_KEY_FLG_CCMP;
920 key_flags |= cpu_to_le16(
921 keyconf->keyidx << STA_KEY_FLG_KEYID_POS);
922 key_flags &= ~STA_KEY_FLG_INVALID;
923 break;
924 case ALG_TKIP:
925 case ALG_WEP:
Zhu Yib481de92007-09-25 17:54:57 -0700926 default:
927 return -EINVAL;
928 }
929 spin_lock_irqsave(&priv->sta_lock, flags);
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800930 priv->stations_39[sta_id].keyinfo.alg = keyconf->alg;
931 priv->stations_39[sta_id].keyinfo.keylen = keyconf->keylen;
932 memcpy(priv->stations_39[sta_id].keyinfo.key, keyconf->key,
Zhu Yib481de92007-09-25 17:54:57 -0700933 keyconf->keylen);
934
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800935 memcpy(priv->stations_39[sta_id].sta.key.key, keyconf->key,
Zhu Yib481de92007-09-25 17:54:57 -0700936 keyconf->keylen);
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800937 priv->stations_39[sta_id].sta.key.key_flags = key_flags;
938 priv->stations_39[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
939 priv->stations_39[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
Zhu Yib481de92007-09-25 17:54:57 -0700940
941 spin_unlock_irqrestore(&priv->sta_lock, flags);
942
943 IWL_DEBUG_INFO("hwcrypto: modify ucode station key info\n");
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800944 iwl3945_send_add_station(priv, &priv->stations_39[sta_id].sta, 0);
Zhu Yib481de92007-09-25 17:54:57 -0700945 return 0;
946}
947
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800948static int iwl3945_clear_sta_key_info(struct iwl_priv *priv, u8 sta_id)
Zhu Yib481de92007-09-25 17:54:57 -0700949{
950 unsigned long flags;
951
952 spin_lock_irqsave(&priv->sta_lock, flags);
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800953 memset(&priv->stations_39[sta_id].keyinfo, 0, sizeof(struct iwl3945_hw_key));
954 memset(&priv->stations_39[sta_id].sta.key, 0,
Tomas Winkler4c897252008-12-19 10:37:05 +0800955 sizeof(struct iwl4965_keyinfo));
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800956 priv->stations_39[sta_id].sta.key.key_flags = STA_KEY_FLG_NO_ENC;
957 priv->stations_39[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
958 priv->stations_39[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
Zhu Yib481de92007-09-25 17:54:57 -0700959 spin_unlock_irqrestore(&priv->sta_lock, flags);
960
961 IWL_DEBUG_INFO("hwcrypto: clear ucode station key info\n");
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800962 iwl3945_send_add_station(priv, &priv->stations_39[sta_id].sta, 0);
Zhu Yib481de92007-09-25 17:54:57 -0700963 return 0;
964}
965
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800966static void iwl3945_clear_free_frames(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700967{
968 struct list_head *element;
969
970 IWL_DEBUG_INFO("%d frames on pre-allocated heap on clear.\n",
971 priv->frames_count);
972
973 while (!list_empty(&priv->free_frames)) {
974 element = priv->free_frames.next;
975 list_del(element);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800976 kfree(list_entry(element, struct iwl3945_frame, list));
Zhu Yib481de92007-09-25 17:54:57 -0700977 priv->frames_count--;
978 }
979
980 if (priv->frames_count) {
Winkler, Tomas39aadf82008-12-19 10:37:32 +0800981 IWL_WARN(priv, "%d frames still in use. Did we lose one?\n",
Zhu Yib481de92007-09-25 17:54:57 -0700982 priv->frames_count);
983 priv->frames_count = 0;
984 }
985}
986
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800987static struct iwl3945_frame *iwl3945_get_free_frame(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700988{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800989 struct iwl3945_frame *frame;
Zhu Yib481de92007-09-25 17:54:57 -0700990 struct list_head *element;
991 if (list_empty(&priv->free_frames)) {
992 frame = kzalloc(sizeof(*frame), GFP_KERNEL);
993 if (!frame) {
Winkler, Tomas15b16872008-12-19 10:37:33 +0800994 IWL_ERR(priv, "Could not allocate frame!\n");
Zhu Yib481de92007-09-25 17:54:57 -0700995 return NULL;
996 }
997
998 priv->frames_count++;
999 return frame;
1000 }
1001
1002 element = priv->free_frames.next;
1003 list_del(element);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001004 return list_entry(element, struct iwl3945_frame, list);
Zhu Yib481de92007-09-25 17:54:57 -07001005}
1006
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001007static void iwl3945_free_frame(struct iwl_priv *priv, struct iwl3945_frame *frame)
Zhu Yib481de92007-09-25 17:54:57 -07001008{
1009 memset(frame, 0, sizeof(*frame));
1010 list_add(&frame->list, &priv->free_frames);
1011}
1012
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001013unsigned int iwl3945_fill_beacon_frame(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07001014 struct ieee80211_hdr *hdr,
Rami Rosen73ec1cc2008-12-16 09:37:07 +02001015 int left)
Zhu Yib481de92007-09-25 17:54:57 -07001016{
1017
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001018 if (!iwl3945_is_associated(priv) || !priv->ibss_beacon ||
Johannes Berg05c914f2008-09-11 00:01:58 +02001019 ((priv->iw_mode != NL80211_IFTYPE_ADHOC) &&
1020 (priv->iw_mode != NL80211_IFTYPE_AP)))
Zhu Yib481de92007-09-25 17:54:57 -07001021 return 0;
1022
1023 if (priv->ibss_beacon->len > left)
1024 return 0;
1025
1026 memcpy(hdr, priv->ibss_beacon->data, priv->ibss_beacon->len);
1027
1028 return priv->ibss_beacon->len;
1029}
1030
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001031static u8 iwl3945_rate_get_lowest_plcp(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001032{
1033 u8 i;
Kolekar, Abhijeetc24f0812008-11-07 09:58:44 -08001034 int rate_mask;
1035
1036 /* Set rate mask*/
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001037 if (priv->staging39_rxon.flags & RXON_FLG_BAND_24G_MSK)
Chatre, Reinettedbce56a2008-11-12 13:14:07 -08001038 rate_mask = priv->active_rate_basic & IWL_CCK_RATES_MASK;
Kolekar, Abhijeetc24f0812008-11-07 09:58:44 -08001039 else
Chatre, Reinettedbce56a2008-11-12 13:14:07 -08001040 rate_mask = priv->active_rate_basic & IWL_OFDM_RATES_MASK;
Zhu Yib481de92007-09-25 17:54:57 -07001041
1042 for (i = IWL_RATE_1M_INDEX; i != IWL_RATE_INVALID;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001043 i = iwl3945_rates[i].next_ieee) {
Zhu Yib481de92007-09-25 17:54:57 -07001044 if (rate_mask & (1 << i))
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001045 return iwl3945_rates[i].plcp;
Zhu Yib481de92007-09-25 17:54:57 -07001046 }
1047
Kolekar, Abhijeetc24f0812008-11-07 09:58:44 -08001048 /* No valid rate was found. Assign the lowest one */
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001049 if (priv->staging39_rxon.flags & RXON_FLG_BAND_24G_MSK)
Kolekar, Abhijeetc24f0812008-11-07 09:58:44 -08001050 return IWL_RATE_1M_PLCP;
1051 else
1052 return IWL_RATE_6M_PLCP;
Zhu Yib481de92007-09-25 17:54:57 -07001053}
1054
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001055static int iwl3945_send_beacon_cmd(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001056{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001057 struct iwl3945_frame *frame;
Zhu Yib481de92007-09-25 17:54:57 -07001058 unsigned int frame_size;
1059 int rc;
1060 u8 rate;
1061
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001062 frame = iwl3945_get_free_frame(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001063
1064 if (!frame) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08001065 IWL_ERR(priv, "Could not obtain free frame buffer for beacon "
Zhu Yib481de92007-09-25 17:54:57 -07001066 "command.\n");
1067 return -ENOMEM;
1068 }
1069
Kolekar, Abhijeetc24f0812008-11-07 09:58:44 -08001070 rate = iwl3945_rate_get_lowest_plcp(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001071
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001072 frame_size = iwl3945_hw_get_beacon_cmd(priv, frame, rate);
Zhu Yib481de92007-09-25 17:54:57 -07001073
Samuel Ortiz518099a2009-01-19 15:30:27 -08001074 rc = iwl_send_cmd_pdu(priv, REPLY_TX_BEACON, frame_size,
Zhu Yib481de92007-09-25 17:54:57 -07001075 &frame->u.cmd[0]);
1076
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001077 iwl3945_free_frame(priv, frame);
Zhu Yib481de92007-09-25 17:54:57 -07001078
1079 return rc;
1080}
1081
1082/******************************************************************************
1083 *
1084 * EEPROM related functions
1085 *
1086 ******************************************************************************/
1087
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001088static void get_eeprom_mac(struct iwl_priv *priv, u8 *mac)
Zhu Yib481de92007-09-25 17:54:57 -07001089{
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001090 memcpy(mac, priv->eeprom39.mac_address, 6);
Zhu Yib481de92007-09-25 17:54:57 -07001091}
1092
Reinette Chatre74a3a252008-01-23 10:15:19 -08001093/*
1094 * Clear the OWNER_MSK, to establish driver (instead of uCode running on
1095 * embedded controller) as EEPROM reader; each read is a series of pulses
1096 * to/from the EEPROM chip, not a single event, so even reads could conflict
1097 * if they weren't arbitrated by some ownership mechanism. Here, the driver
1098 * simply claims ownership, which should be safe when this function is called
1099 * (i.e. before loading uCode!).
1100 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001101static inline int iwl3945_eeprom_acquire_semaphore(struct iwl_priv *priv)
Reinette Chatre74a3a252008-01-23 10:15:19 -08001102{
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08001103 _iwl_clear_bit(priv, CSR_EEPROM_GP, CSR_EEPROM_GP_IF_OWNER_MSK);
Reinette Chatre74a3a252008-01-23 10:15:19 -08001104 return 0;
1105}
1106
Zhu Yib481de92007-09-25 17:54:57 -07001107/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001108 * iwl3945_eeprom_init - read EEPROM contents
Zhu Yib481de92007-09-25 17:54:57 -07001109 *
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001110 * Load the EEPROM contents from adapter into priv->eeprom39
Zhu Yib481de92007-09-25 17:54:57 -07001111 *
1112 * NOTE: This routine uses the non-debug IO access functions.
1113 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001114int iwl3945_eeprom_init(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001115{
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001116 u16 *e = (u16 *)&priv->eeprom39;
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08001117 u32 gp = iwl_read32(priv, CSR_EEPROM_GP);
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001118 int sz = sizeof(priv->eeprom39);
Zhu, Yi3d5717a2008-12-11 10:33:36 -08001119 int ret;
Zhu Yib481de92007-09-25 17:54:57 -07001120 u16 addr;
1121
1122 /* The EEPROM structure has several padding buffers within it
1123 * and when adding new EEPROM maps is subject to programmer errors
1124 * which may be very difficult to identify without explicitly
1125 * checking the resulting size of the eeprom map. */
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001126 BUILD_BUG_ON(sizeof(priv->eeprom39) != IWL_EEPROM_IMAGE_SIZE);
Zhu Yib481de92007-09-25 17:54:57 -07001127
1128 if ((gp & CSR_EEPROM_GP_VALID_MSK) == CSR_EEPROM_GP_BAD_SIGNATURE) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08001129 IWL_ERR(priv, "EEPROM not found, EEPROM_GP=0x%08x\n", gp);
Zhu Yib481de92007-09-25 17:54:57 -07001130 return -ENOENT;
1131 }
1132
Cahill, Ben M6440adb2007-11-29 11:09:55 +08001133 /* Make sure driver (instead of uCode) is allowed to read EEPROM */
Zhu, Yi3d5717a2008-12-11 10:33:36 -08001134 ret = iwl3945_eeprom_acquire_semaphore(priv);
1135 if (ret < 0) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08001136 IWL_ERR(priv, "Failed to acquire EEPROM semaphore.\n");
Zhu Yib481de92007-09-25 17:54:57 -07001137 return -ENOENT;
1138 }
1139
1140 /* eeprom is an array of 16bit values */
1141 for (addr = 0; addr < sz; addr += sizeof(u16)) {
Zhu, Yi3d5717a2008-12-11 10:33:36 -08001142 u32 r;
1143
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08001144 _iwl_write32(priv, CSR_EEPROM_REG,
Zhu, Yi3d5717a2008-12-11 10:33:36 -08001145 CSR_EEPROM_REG_MSK_ADDR & (addr << 1));
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08001146 _iwl_clear_bit(priv, CSR_EEPROM_REG, CSR_EEPROM_REG_BIT_CMD);
1147 ret = iwl_poll_direct_bit(priv, CSR_EEPROM_REG,
Zhu, Yi3d5717a2008-12-11 10:33:36 -08001148 CSR_EEPROM_REG_READ_VALID_MSK,
1149 IWL_EEPROM_ACCESS_TIMEOUT);
1150 if (ret < 0) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08001151 IWL_ERR(priv, "Time out reading EEPROM[%d]\n", addr);
Zhu, Yi3d5717a2008-12-11 10:33:36 -08001152 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07001153 }
Zhu, Yi3d5717a2008-12-11 10:33:36 -08001154
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08001155 r = _iwl_read_direct32(priv, CSR_EEPROM_REG);
Tomas Winkler58ff6d42008-02-13 02:47:54 +02001156 e[addr / 2] = le16_to_cpu((__force __le16)(r >> 16));
Zhu Yib481de92007-09-25 17:54:57 -07001157 }
1158
1159 return 0;
1160}
1161
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001162static void iwl3945_unset_hw_params(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001163{
Abhijeet Kolekar3832ec92008-12-19 10:37:26 +08001164 if (priv->shared_virt)
Zhu Yib481de92007-09-25 17:54:57 -07001165 pci_free_consistent(priv->pci_dev,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001166 sizeof(struct iwl3945_shared),
Abhijeet Kolekar3832ec92008-12-19 10:37:26 +08001167 priv->shared_virt,
1168 priv->shared_phys);
Zhu Yib481de92007-09-25 17:54:57 -07001169}
1170
1171/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001172 * iwl3945_supported_rate_to_ie - fill in the supported rate in IE field
Zhu Yib481de92007-09-25 17:54:57 -07001173 *
1174 * return : set the bit for each supported rate insert in ie
1175 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001176static u16 iwl3945_supported_rate_to_ie(u8 *ie, u16 supported_rate,
Tomas Winklerc7c46672007-10-18 02:04:15 +02001177 u16 basic_rate, int *left)
Zhu Yib481de92007-09-25 17:54:57 -07001178{
1179 u16 ret_rates = 0, bit;
1180 int i;
Tomas Winklerc7c46672007-10-18 02:04:15 +02001181 u8 *cnt = ie;
1182 u8 *rates = ie + 1;
Zhu Yib481de92007-09-25 17:54:57 -07001183
1184 for (bit = 1, i = 0; i < IWL_RATE_COUNT; i++, bit <<= 1) {
1185 if (bit & supported_rate) {
1186 ret_rates |= bit;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001187 rates[*cnt] = iwl3945_rates[i].ieee |
Tomas Winklerc7c46672007-10-18 02:04:15 +02001188 ((bit & basic_rate) ? 0x80 : 0x00);
1189 (*cnt)++;
1190 (*left)--;
1191 if ((*left <= 0) ||
1192 (*cnt >= IWL_SUPPORTED_RATES_IE_LEN))
Zhu Yib481de92007-09-25 17:54:57 -07001193 break;
1194 }
1195 }
1196
1197 return ret_rates;
1198}
1199
1200/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001201 * iwl3945_fill_probe_req - fill in all required fields and IE for probe request
Zhu Yib481de92007-09-25 17:54:57 -07001202 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001203static u16 iwl3945_fill_probe_req(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07001204 struct ieee80211_mgmt *frame,
Johannes Berg430cfe92008-10-28 18:06:02 +01001205 int left)
Zhu Yib481de92007-09-25 17:54:57 -07001206{
1207 int len = 0;
1208 u8 *pos = NULL;
Tomas Winklerc7c46672007-10-18 02:04:15 +02001209 u16 active_rates, ret_rates, cck_rates;
Zhu Yib481de92007-09-25 17:54:57 -07001210
1211 /* Make sure there is enough space for the probe request,
1212 * two mandatory IEs and the data */
1213 left -= 24;
1214 if (left < 0)
1215 return 0;
1216 len += 24;
1217
1218 frame->frame_control = cpu_to_le16(IEEE80211_STYPE_PROBE_REQ);
Kolekar, Abhijeetb5323d32008-12-19 10:37:22 +08001219 memcpy(frame->da, iwl_bcast_addr, ETH_ALEN);
Zhu Yib481de92007-09-25 17:54:57 -07001220 memcpy(frame->sa, priv->mac_addr, ETH_ALEN);
Kolekar, Abhijeetb5323d32008-12-19 10:37:22 +08001221 memcpy(frame->bssid, iwl_bcast_addr, ETH_ALEN);
Zhu Yib481de92007-09-25 17:54:57 -07001222 frame->seq_ctrl = 0;
1223
1224 /* fill in our indirect SSID IE */
1225 /* ...next IE... */
1226
1227 left -= 2;
1228 if (left < 0)
1229 return 0;
1230 len += 2;
1231 pos = &(frame->u.probe_req.variable[0]);
1232 *pos++ = WLAN_EID_SSID;
1233 *pos++ = 0;
1234
Zhu Yib481de92007-09-25 17:54:57 -07001235 /* fill in supported rate */
1236 /* ...next IE... */
1237 left -= 2;
1238 if (left < 0)
1239 return 0;
Tomas Winklerc7c46672007-10-18 02:04:15 +02001240
Zhu Yib481de92007-09-25 17:54:57 -07001241 /* ... fill it in... */
1242 *pos++ = WLAN_EID_SUPP_RATES;
1243 *pos = 0;
Tomas Winklerc7c46672007-10-18 02:04:15 +02001244
1245 priv->active_rate = priv->rates_mask;
1246 active_rates = priv->active_rate;
Zhu Yib481de92007-09-25 17:54:57 -07001247 priv->active_rate_basic = priv->rates_mask & IWL_BASIC_RATES_MASK;
1248
Tomas Winklerc7c46672007-10-18 02:04:15 +02001249 cck_rates = IWL_CCK_RATES_MASK & active_rates;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001250 ret_rates = iwl3945_supported_rate_to_ie(pos, cck_rates,
Tomas Winklerc7c46672007-10-18 02:04:15 +02001251 priv->active_rate_basic, &left);
1252 active_rates &= ~ret_rates;
1253
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001254 ret_rates = iwl3945_supported_rate_to_ie(pos, active_rates,
Tomas Winklerc7c46672007-10-18 02:04:15 +02001255 priv->active_rate_basic, &left);
1256 active_rates &= ~ret_rates;
1257
Zhu Yib481de92007-09-25 17:54:57 -07001258 len += 2 + *pos;
1259 pos += (*pos) + 1;
Tomas Winklerc7c46672007-10-18 02:04:15 +02001260 if (active_rates == 0)
Zhu Yib481de92007-09-25 17:54:57 -07001261 goto fill_end;
1262
1263 /* fill in supported extended rate */
1264 /* ...next IE... */
1265 left -= 2;
1266 if (left < 0)
1267 return 0;
1268 /* ... fill it in... */
1269 *pos++ = WLAN_EID_EXT_SUPP_RATES;
1270 *pos = 0;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001271 iwl3945_supported_rate_to_ie(pos, active_rates,
Tomas Winklerc7c46672007-10-18 02:04:15 +02001272 priv->active_rate_basic, &left);
Zhu Yib481de92007-09-25 17:54:57 -07001273 if (*pos > 0)
1274 len += 2 + *pos;
1275
1276 fill_end:
1277 return (u16)len;
1278}
1279
1280/*
1281 * QoS support
1282*/
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001283static int iwl3945_send_qos_params_command(struct iwl_priv *priv,
Tomas Winkler4c897252008-12-19 10:37:05 +08001284 struct iwl_qosparam_cmd *qos)
Zhu Yib481de92007-09-25 17:54:57 -07001285{
1286
Samuel Ortiz518099a2009-01-19 15:30:27 -08001287 return iwl_send_cmd_pdu(priv, REPLY_QOS_PARAM,
Tomas Winkler4c897252008-12-19 10:37:05 +08001288 sizeof(struct iwl_qosparam_cmd), qos);
Zhu Yib481de92007-09-25 17:54:57 -07001289}
1290
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001291static void iwl3945_activate_qos(struct iwl_priv *priv, u8 force)
Zhu Yib481de92007-09-25 17:54:57 -07001292{
1293 unsigned long flags;
1294
Zhu Yib481de92007-09-25 17:54:57 -07001295 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
1296 return;
1297
Zhu Yib481de92007-09-25 17:54:57 -07001298 spin_lock_irqsave(&priv->lock, flags);
1299 priv->qos_data.def_qos_parm.qos_flags = 0;
1300
1301 if (priv->qos_data.qos_cap.q_AP.queue_request &&
1302 !priv->qos_data.qos_cap.q_AP.txop_request)
1303 priv->qos_data.def_qos_parm.qos_flags |=
1304 QOS_PARAM_FLG_TXOP_TYPE_MSK;
1305
1306 if (priv->qos_data.qos_active)
1307 priv->qos_data.def_qos_parm.qos_flags |=
1308 QOS_PARAM_FLG_UPDATE_EDCA_MSK;
1309
1310 spin_unlock_irqrestore(&priv->lock, flags);
1311
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001312 if (force || iwl3945_is_associated(priv)) {
Tomas Winklera96a27f2008-10-23 23:48:56 -07001313 IWL_DEBUG_QOS("send QoS cmd with QoS active %d \n",
Zhu Yib481de92007-09-25 17:54:57 -07001314 priv->qos_data.qos_active);
1315
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001316 iwl3945_send_qos_params_command(priv,
Zhu Yib481de92007-09-25 17:54:57 -07001317 &(priv->qos_data.def_qos_parm));
1318 }
1319}
1320
Zhu Yib481de92007-09-25 17:54:57 -07001321/*
1322 * Power management (not Tx power!) functions
1323 */
1324#define MSEC_TO_USEC 1024
1325
Tomas Winkler600c0e12008-12-19 10:37:04 +08001326
1327#define NOSLP __constant_cpu_to_le16(0), 0, 0
1328#define SLP IWL_POWER_DRIVER_ALLOW_SLEEP_MSK, 0, 0
Zhu Yib481de92007-09-25 17:54:57 -07001329#define SLP_TIMEOUT(T) __constant_cpu_to_le32((T) * MSEC_TO_USEC)
1330#define SLP_VEC(X0, X1, X2, X3, X4) {__constant_cpu_to_le32(X0), \
1331 __constant_cpu_to_le32(X1), \
1332 __constant_cpu_to_le32(X2), \
1333 __constant_cpu_to_le32(X3), \
1334 __constant_cpu_to_le32(X4)}
1335
Zhu Yib481de92007-09-25 17:54:57 -07001336/* default power management (not Tx power) table values */
Tomas Winklera96a27f2008-10-23 23:48:56 -07001337/* for TIM 0-10 */
Samuel Ortiz1125eff2008-12-19 10:37:14 +08001338static struct iwl_power_vec_entry range_0[IWL39_POWER_AC] = {
Zhu Yib481de92007-09-25 17:54:57 -07001339 {{NOSLP, SLP_TIMEOUT(0), SLP_TIMEOUT(0), SLP_VEC(0, 0, 0, 0, 0)}, 0},
1340 {{SLP, SLP_TIMEOUT(200), SLP_TIMEOUT(500), SLP_VEC(1, 2, 3, 4, 4)}, 0},
1341 {{SLP, SLP_TIMEOUT(200), SLP_TIMEOUT(300), SLP_VEC(2, 4, 6, 7, 7)}, 0},
1342 {{SLP, SLP_TIMEOUT(50), SLP_TIMEOUT(100), SLP_VEC(2, 6, 9, 9, 10)}, 0},
1343 {{SLP, SLP_TIMEOUT(50), SLP_TIMEOUT(25), SLP_VEC(2, 7, 9, 9, 10)}, 1},
1344 {{SLP, SLP_TIMEOUT(25), SLP_TIMEOUT(25), SLP_VEC(4, 7, 10, 10, 10)}, 1}
1345};
1346
Tomas Winklera96a27f2008-10-23 23:48:56 -07001347/* for TIM > 10 */
Samuel Ortiz1125eff2008-12-19 10:37:14 +08001348static struct iwl_power_vec_entry range_1[IWL39_POWER_AC] = {
Zhu Yib481de92007-09-25 17:54:57 -07001349 {{NOSLP, SLP_TIMEOUT(0), SLP_TIMEOUT(0), SLP_VEC(0, 0, 0, 0, 0)}, 0},
1350 {{SLP, SLP_TIMEOUT(200), SLP_TIMEOUT(500),
1351 SLP_VEC(1, 2, 3, 4, 0xFF)}, 0},
1352 {{SLP, SLP_TIMEOUT(200), SLP_TIMEOUT(300),
1353 SLP_VEC(2, 4, 6, 7, 0xFF)}, 0},
1354 {{SLP, SLP_TIMEOUT(50), SLP_TIMEOUT(100),
1355 SLP_VEC(2, 6, 9, 9, 0xFF)}, 0},
1356 {{SLP, SLP_TIMEOUT(50), SLP_TIMEOUT(25), SLP_VEC(2, 7, 9, 9, 0xFF)}, 0},
1357 {{SLP, SLP_TIMEOUT(25), SLP_TIMEOUT(25),
1358 SLP_VEC(4, 7, 10, 10, 0xFF)}, 0}
1359};
1360
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001361int iwl3945_power_init_handle(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001362{
1363 int rc = 0, i;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001364 struct iwl3945_power_mgr *pow_data;
Samuel Ortiz1125eff2008-12-19 10:37:14 +08001365 int size = sizeof(struct iwl_power_vec_entry) * IWL39_POWER_AC;
Zhu Yib481de92007-09-25 17:54:57 -07001366 u16 pci_pm;
1367
1368 IWL_DEBUG_POWER("Initialize power \n");
1369
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001370 pow_data = &(priv->power_data_39);
Zhu Yib481de92007-09-25 17:54:57 -07001371
1372 memset(pow_data, 0, sizeof(*pow_data));
1373
1374 pow_data->active_index = IWL_POWER_RANGE_0;
1375 pow_data->dtim_val = 0xffff;
1376
1377 memcpy(&pow_data->pwr_range_0[0], &range_0[0], size);
1378 memcpy(&pow_data->pwr_range_1[0], &range_1[0], size);
1379
1380 rc = pci_read_config_word(priv->pci_dev, PCI_LINK_CTRL, &pci_pm);
1381 if (rc != 0)
1382 return 0;
1383 else {
Tomas Winkler600c0e12008-12-19 10:37:04 +08001384 struct iwl_powertable_cmd *cmd;
Zhu Yib481de92007-09-25 17:54:57 -07001385
1386 IWL_DEBUG_POWER("adjust power command flags\n");
1387
Samuel Ortiz1125eff2008-12-19 10:37:14 +08001388 for (i = 0; i < IWL39_POWER_AC; i++) {
Zhu Yib481de92007-09-25 17:54:57 -07001389 cmd = &pow_data->pwr_range_0[i].cmd;
1390
1391 if (pci_pm & 0x1)
1392 cmd->flags &= ~IWL_POWER_PCI_PM_MSK;
1393 else
1394 cmd->flags |= IWL_POWER_PCI_PM_MSK;
1395 }
1396 }
1397 return rc;
1398}
1399
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001400static int iwl3945_update_power_cmd(struct iwl_priv *priv,
Tomas Winkler600c0e12008-12-19 10:37:04 +08001401 struct iwl_powertable_cmd *cmd, u32 mode)
Zhu Yib481de92007-09-25 17:54:57 -07001402{
1403 int rc = 0, i;
1404 u8 skip;
1405 u32 max_sleep = 0;
Samuel Ortiz1125eff2008-12-19 10:37:14 +08001406 struct iwl_power_vec_entry *range;
Zhu Yib481de92007-09-25 17:54:57 -07001407 u8 period = 0;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001408 struct iwl3945_power_mgr *pow_data;
Zhu Yib481de92007-09-25 17:54:57 -07001409
1410 if (mode > IWL_POWER_INDEX_5) {
1411 IWL_DEBUG_POWER("Error invalid power mode \n");
1412 return -1;
1413 }
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001414 pow_data = &(priv->power_data_39);
Zhu Yib481de92007-09-25 17:54:57 -07001415
1416 if (pow_data->active_index == IWL_POWER_RANGE_0)
1417 range = &pow_data->pwr_range_0[0];
1418 else
1419 range = &pow_data->pwr_range_1[1];
1420
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001421 memcpy(cmd, &range[mode].cmd, sizeof(struct iwl3945_powertable_cmd));
Zhu Yib481de92007-09-25 17:54:57 -07001422
1423#ifdef IWL_MAC80211_DISABLE
1424 if (priv->assoc_network != NULL) {
1425 unsigned long flags;
1426
1427 period = priv->assoc_network->tim.tim_period;
1428 }
1429#endif /*IWL_MAC80211_DISABLE */
1430 skip = range[mode].no_dtim;
1431
1432 if (period == 0) {
1433 period = 1;
1434 skip = 0;
1435 }
1436
1437 if (skip == 0) {
1438 max_sleep = period;
1439 cmd->flags &= ~IWL_POWER_SLEEP_OVER_DTIM_MSK;
1440 } else {
1441 __le32 slp_itrvl = cmd->sleep_interval[IWL_POWER_VEC_SIZE - 1];
1442 max_sleep = (le32_to_cpu(slp_itrvl) / period) * period;
1443 cmd->flags |= IWL_POWER_SLEEP_OVER_DTIM_MSK;
1444 }
1445
1446 for (i = 0; i < IWL_POWER_VEC_SIZE; i++) {
1447 if (le32_to_cpu(cmd->sleep_interval[i]) > max_sleep)
1448 cmd->sleep_interval[i] = cpu_to_le32(max_sleep);
1449 }
1450
1451 IWL_DEBUG_POWER("Flags value = 0x%08X\n", cmd->flags);
1452 IWL_DEBUG_POWER("Tx timeout = %u\n", le32_to_cpu(cmd->tx_data_timeout));
1453 IWL_DEBUG_POWER("Rx timeout = %u\n", le32_to_cpu(cmd->rx_data_timeout));
1454 IWL_DEBUG_POWER("Sleep interval vector = { %d , %d , %d , %d , %d }\n",
1455 le32_to_cpu(cmd->sleep_interval[0]),
1456 le32_to_cpu(cmd->sleep_interval[1]),
1457 le32_to_cpu(cmd->sleep_interval[2]),
1458 le32_to_cpu(cmd->sleep_interval[3]),
1459 le32_to_cpu(cmd->sleep_interval[4]));
1460
1461 return rc;
1462}
1463
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001464static int iwl3945_send_power_mode(struct iwl_priv *priv, u32 mode)
Zhu Yib481de92007-09-25 17:54:57 -07001465{
John W. Linville9a62f732007-11-15 16:27:36 -05001466 u32 uninitialized_var(final_mode);
Zhu Yib481de92007-09-25 17:54:57 -07001467 int rc;
Tomas Winkler600c0e12008-12-19 10:37:04 +08001468 struct iwl_powertable_cmd cmd;
Zhu Yib481de92007-09-25 17:54:57 -07001469
1470 /* If on battery, set to 3,
Ian Schram01ebd062007-10-25 17:15:22 +08001471 * if plugged into AC power, set to CAM ("continuously aware mode"),
Zhu Yib481de92007-09-25 17:54:57 -07001472 * else user level */
1473 switch (mode) {
Samuel Ortiz1125eff2008-12-19 10:37:14 +08001474 case IWL39_POWER_BATTERY:
Zhu Yib481de92007-09-25 17:54:57 -07001475 final_mode = IWL_POWER_INDEX_3;
1476 break;
Samuel Ortiz1125eff2008-12-19 10:37:14 +08001477 case IWL39_POWER_AC:
Zhu Yib481de92007-09-25 17:54:57 -07001478 final_mode = IWL_POWER_MODE_CAM;
1479 break;
1480 default:
1481 final_mode = mode;
1482 break;
1483 }
1484
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001485 iwl3945_update_power_cmd(priv, &cmd, final_mode);
Zhu Yib481de92007-09-25 17:54:57 -07001486
Tomas Winkler600c0e12008-12-19 10:37:04 +08001487 /* FIXME use get_hcmd_size 3945 command is 4 bytes shorter */
Samuel Ortiz518099a2009-01-19 15:30:27 -08001488 rc = iwl_send_cmd_pdu(priv, POWER_TABLE_CMD,
1489 sizeof(struct iwl3945_powertable_cmd), &cmd);
Zhu Yib481de92007-09-25 17:54:57 -07001490
1491 if (final_mode == IWL_POWER_MODE_CAM)
1492 clear_bit(STATUS_POWER_PMI, &priv->status);
1493 else
1494 set_bit(STATUS_POWER_PMI, &priv->status);
1495
1496 return rc;
1497}
1498
Zhu Yib481de92007-09-25 17:54:57 -07001499#define MAX_UCODE_BEACON_INTERVAL 1024
1500#define INTEL_CONN_LISTEN_INTERVAL __constant_cpu_to_le16(0xA)
1501
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001502static __le16 iwl3945_adjust_beacon_interval(u16 beacon_val)
Zhu Yib481de92007-09-25 17:54:57 -07001503{
1504 u16 new_val = 0;
1505 u16 beacon_factor = 0;
1506
1507 beacon_factor =
1508 (beacon_val + MAX_UCODE_BEACON_INTERVAL)
1509 / MAX_UCODE_BEACON_INTERVAL;
1510 new_val = beacon_val / beacon_factor;
1511
1512 return cpu_to_le16(new_val);
1513}
1514
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001515static void iwl3945_setup_rxon_timing(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001516{
1517 u64 interval_tm_unit;
1518 u64 tsf, result;
1519 unsigned long flags;
1520 struct ieee80211_conf *conf = NULL;
1521 u16 beacon_int = 0;
1522
1523 conf = ieee80211_get_hw_conf(priv->hw);
1524
1525 spin_lock_irqsave(&priv->lock, flags);
Tomas Winkler28afaf92008-12-19 10:37:06 +08001526 priv->rxon_timing.timestamp = cpu_to_le64(priv->timestamp);
Zhu Yib481de92007-09-25 17:54:57 -07001527 priv->rxon_timing.listen_interval = INTEL_CONN_LISTEN_INTERVAL;
1528
Tomas Winkler28afaf92008-12-19 10:37:06 +08001529 tsf = priv->timestamp;
Zhu Yib481de92007-09-25 17:54:57 -07001530
1531 beacon_int = priv->beacon_int;
1532 spin_unlock_irqrestore(&priv->lock, flags);
1533
Johannes Berg05c914f2008-09-11 00:01:58 +02001534 if (priv->iw_mode == NL80211_IFTYPE_STATION) {
Zhu Yib481de92007-09-25 17:54:57 -07001535 if (beacon_int == 0) {
1536 priv->rxon_timing.beacon_interval = cpu_to_le16(100);
1537 priv->rxon_timing.beacon_init_val = cpu_to_le32(102400);
1538 } else {
1539 priv->rxon_timing.beacon_interval =
1540 cpu_to_le16(beacon_int);
1541 priv->rxon_timing.beacon_interval =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001542 iwl3945_adjust_beacon_interval(
Zhu Yib481de92007-09-25 17:54:57 -07001543 le16_to_cpu(priv->rxon_timing.beacon_interval));
1544 }
1545
1546 priv->rxon_timing.atim_window = 0;
1547 } else {
1548 priv->rxon_timing.beacon_interval =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001549 iwl3945_adjust_beacon_interval(conf->beacon_int);
Zhu Yib481de92007-09-25 17:54:57 -07001550 /* TODO: we need to get atim_window from upper stack
1551 * for now we set to 0 */
1552 priv->rxon_timing.atim_window = 0;
1553 }
1554
1555 interval_tm_unit =
1556 (le16_to_cpu(priv->rxon_timing.beacon_interval) * 1024);
1557 result = do_div(tsf, interval_tm_unit);
1558 priv->rxon_timing.beacon_init_val =
1559 cpu_to_le32((u32) ((u64) interval_tm_unit - result));
1560
1561 IWL_DEBUG_ASSOC
1562 ("beacon interval %d beacon timer %d beacon tim %d\n",
1563 le16_to_cpu(priv->rxon_timing.beacon_interval),
1564 le32_to_cpu(priv->rxon_timing.beacon_init_val),
1565 le16_to_cpu(priv->rxon_timing.atim_window));
1566}
1567
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001568static int iwl3945_scan_initiate(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001569{
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08001570 if (!iwl_is_ready_rf(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07001571 IWL_DEBUG_SCAN("Aborting scan due to not ready.\n");
1572 return -EIO;
1573 }
1574
1575 if (test_bit(STATUS_SCANNING, &priv->status)) {
1576 IWL_DEBUG_SCAN("Scan already in progress.\n");
1577 return -EAGAIN;
1578 }
1579
1580 if (test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
1581 IWL_DEBUG_SCAN("Scan request while abort pending. "
1582 "Queuing.\n");
1583 return -EAGAIN;
1584 }
1585
1586 IWL_DEBUG_INFO("Starting scan...\n");
Ron Rindjunsky66b50042008-06-25 16:46:31 +08001587 if (priv->cfg->sku & IWL_SKU_G)
1588 priv->scan_bands |= BIT(IEEE80211_BAND_2GHZ);
1589 if (priv->cfg->sku & IWL_SKU_A)
1590 priv->scan_bands |= BIT(IEEE80211_BAND_5GHZ);
Zhu Yib481de92007-09-25 17:54:57 -07001591 set_bit(STATUS_SCANNING, &priv->status);
1592 priv->scan_start = jiffies;
1593 priv->scan_pass_start = priv->scan_start;
1594
1595 queue_work(priv->workqueue, &priv->request_scan);
1596
1597 return 0;
1598}
1599
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001600static int iwl3945_set_rxon_hwcrypto(struct iwl_priv *priv, int hw_decrypt)
Zhu Yib481de92007-09-25 17:54:57 -07001601{
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001602 struct iwl3945_rxon_cmd *rxon = &priv->staging39_rxon;
Zhu Yib481de92007-09-25 17:54:57 -07001603
1604 if (hw_decrypt)
1605 rxon->filter_flags &= ~RXON_FILTER_DIS_DECRYPT_MSK;
1606 else
1607 rxon->filter_flags |= RXON_FILTER_DIS_DECRYPT_MSK;
1608
1609 return 0;
1610}
1611
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001612static void iwl3945_set_flags_for_phymode(struct iwl_priv *priv,
Johannes Berg8318d782008-01-24 19:38:38 +01001613 enum ieee80211_band band)
Zhu Yib481de92007-09-25 17:54:57 -07001614{
Johannes Berg8318d782008-01-24 19:38:38 +01001615 if (band == IEEE80211_BAND_5GHZ) {
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001616 priv->staging39_rxon.flags &=
Zhu Yib481de92007-09-25 17:54:57 -07001617 ~(RXON_FLG_BAND_24G_MSK | RXON_FLG_AUTO_DETECT_MSK
1618 | RXON_FLG_CCK_MSK);
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001619 priv->staging39_rxon.flags |= RXON_FLG_SHORT_SLOT_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07001620 } else {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001621 /* Copied from iwl3945_bg_post_associate() */
Zhu Yib481de92007-09-25 17:54:57 -07001622 if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_SLOT_TIME)
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001623 priv->staging39_rxon.flags |= RXON_FLG_SHORT_SLOT_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07001624 else
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001625 priv->staging39_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07001626
Johannes Berg05c914f2008-09-11 00:01:58 +02001627 if (priv->iw_mode == NL80211_IFTYPE_ADHOC)
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001628 priv->staging39_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07001629
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001630 priv->staging39_rxon.flags |= RXON_FLG_BAND_24G_MSK;
1631 priv->staging39_rxon.flags |= RXON_FLG_AUTO_DETECT_MSK;
1632 priv->staging39_rxon.flags &= ~RXON_FLG_CCK_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07001633 }
1634}
1635
1636/*
Ian Schram01ebd062007-10-25 17:15:22 +08001637 * initialize rxon structure with default values from eeprom
Zhu Yib481de92007-09-25 17:54:57 -07001638 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001639static void iwl3945_connection_init_rx_config(struct iwl_priv *priv,
Zhu, Yi60294de2008-10-29 14:05:45 -07001640 int mode)
Zhu Yib481de92007-09-25 17:54:57 -07001641{
Samuel Ortizd20b3c62008-12-19 10:37:15 +08001642 const struct iwl_channel_info *ch_info;
Zhu Yib481de92007-09-25 17:54:57 -07001643
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001644 memset(&priv->staging39_rxon, 0, sizeof(priv->staging39_rxon));
Zhu Yib481de92007-09-25 17:54:57 -07001645
Zhu, Yi60294de2008-10-29 14:05:45 -07001646 switch (mode) {
Johannes Berg05c914f2008-09-11 00:01:58 +02001647 case NL80211_IFTYPE_AP:
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001648 priv->staging39_rxon.dev_type = RXON_DEV_TYPE_AP;
Zhu Yib481de92007-09-25 17:54:57 -07001649 break;
1650
Johannes Berg05c914f2008-09-11 00:01:58 +02001651 case NL80211_IFTYPE_STATION:
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001652 priv->staging39_rxon.dev_type = RXON_DEV_TYPE_ESS;
1653 priv->staging39_rxon.filter_flags = RXON_FILTER_ACCEPT_GRP_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07001654 break;
1655
Johannes Berg05c914f2008-09-11 00:01:58 +02001656 case NL80211_IFTYPE_ADHOC:
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001657 priv->staging39_rxon.dev_type = RXON_DEV_TYPE_IBSS;
1658 priv->staging39_rxon.flags = RXON_FLG_SHORT_PREAMBLE_MSK;
1659 priv->staging39_rxon.filter_flags = RXON_FILTER_BCON_AWARE_MSK |
Zhu Yib481de92007-09-25 17:54:57 -07001660 RXON_FILTER_ACCEPT_GRP_MSK;
1661 break;
1662
Johannes Berg05c914f2008-09-11 00:01:58 +02001663 case NL80211_IFTYPE_MONITOR:
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001664 priv->staging39_rxon.dev_type = RXON_DEV_TYPE_SNIFFER;
1665 priv->staging39_rxon.filter_flags = RXON_FILTER_PROMISC_MSK |
Zhu Yib481de92007-09-25 17:54:57 -07001666 RXON_FILTER_CTL2HOST_MSK | RXON_FILTER_ACCEPT_GRP_MSK;
1667 break;
Tomas Winkler69dc5d92008-03-25 16:33:41 -07001668 default:
Winkler, Tomas15b16872008-12-19 10:37:33 +08001669 IWL_ERR(priv, "Unsupported interface type %d\n", mode);
Tomas Winkler69dc5d92008-03-25 16:33:41 -07001670 break;
Zhu Yib481de92007-09-25 17:54:57 -07001671 }
1672
1673#if 0
1674 /* TODO: Figure out when short_preamble would be set and cache from
1675 * that */
1676 if (!hw_to_local(priv->hw)->short_preamble)
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001677 priv->staging39_rxon.flags &= ~RXON_FLG_SHORT_PREAMBLE_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07001678 else
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001679 priv->staging39_rxon.flags |= RXON_FLG_SHORT_PREAMBLE_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07001680#endif
1681
Johannes Berg8318d782008-01-24 19:38:38 +01001682 ch_info = iwl3945_get_channel_info(priv, priv->band,
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001683 le16_to_cpu(priv->active39_rxon.channel));
Zhu Yib481de92007-09-25 17:54:57 -07001684
1685 if (!ch_info)
1686 ch_info = &priv->channel_info[0];
1687
1688 /*
1689 * in some case A channels are all non IBSS
1690 * in this case force B/G channel
1691 */
Zhu, Yi60294de2008-10-29 14:05:45 -07001692 if ((mode == NL80211_IFTYPE_ADHOC) && !(is_channel_ibss(ch_info)))
Zhu Yib481de92007-09-25 17:54:57 -07001693 ch_info = &priv->channel_info[0];
1694
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001695 priv->staging39_rxon.channel = cpu_to_le16(ch_info->channel);
Zhu Yib481de92007-09-25 17:54:57 -07001696 if (is_channel_a_band(ch_info))
Johannes Berg8318d782008-01-24 19:38:38 +01001697 priv->band = IEEE80211_BAND_5GHZ;
Zhu Yib481de92007-09-25 17:54:57 -07001698 else
Johannes Berg8318d782008-01-24 19:38:38 +01001699 priv->band = IEEE80211_BAND_2GHZ;
Zhu Yib481de92007-09-25 17:54:57 -07001700
Johannes Berg8318d782008-01-24 19:38:38 +01001701 iwl3945_set_flags_for_phymode(priv, priv->band);
Zhu Yib481de92007-09-25 17:54:57 -07001702
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001703 priv->staging39_rxon.ofdm_basic_rates =
Zhu Yib481de92007-09-25 17:54:57 -07001704 (IWL_OFDM_RATES_MASK >> IWL_FIRST_OFDM_RATE) & 0xFF;
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001705 priv->staging39_rxon.cck_basic_rates =
Zhu Yib481de92007-09-25 17:54:57 -07001706 (IWL_CCK_RATES_MASK >> IWL_FIRST_CCK_RATE) & 0xF;
1707}
1708
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001709static int iwl3945_set_mode(struct iwl_priv *priv, int mode)
Zhu Yib481de92007-09-25 17:54:57 -07001710{
Johannes Berg05c914f2008-09-11 00:01:58 +02001711 if (mode == NL80211_IFTYPE_ADHOC) {
Samuel Ortizd20b3c62008-12-19 10:37:15 +08001712 const struct iwl_channel_info *ch_info;
Zhu Yib481de92007-09-25 17:54:57 -07001713
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001714 ch_info = iwl3945_get_channel_info(priv,
Johannes Berg8318d782008-01-24 19:38:38 +01001715 priv->band,
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001716 le16_to_cpu(priv->staging39_rxon.channel));
Zhu Yib481de92007-09-25 17:54:57 -07001717
1718 if (!ch_info || !is_channel_ibss(ch_info)) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08001719 IWL_ERR(priv, "channel %d not IBSS channel\n",
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001720 le16_to_cpu(priv->staging39_rxon.channel));
Zhu Yib481de92007-09-25 17:54:57 -07001721 return -EINVAL;
1722 }
1723 }
1724
Zhu, Yi60294de2008-10-29 14:05:45 -07001725 iwl3945_connection_init_rx_config(priv, mode);
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001726 memcpy(priv->staging39_rxon.node_addr, priv->mac_addr, ETH_ALEN);
Zhu Yib481de92007-09-25 17:54:57 -07001727
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001728 iwl3945_clear_stations_table(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001729
Tomas Winklera96a27f2008-10-23 23:48:56 -07001730 /* don't commit rxon if rf-kill is on*/
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08001731 if (!iwl_is_ready_rf(priv))
Mohamed Abbasfde35712007-11-29 11:10:15 +08001732 return -EAGAIN;
1733
1734 cancel_delayed_work(&priv->scan_check);
Winkler, Tomasaf0053d2009-01-19 15:30:23 -08001735 if (iwl_scan_cancel_timeout(priv, 100)) {
Winkler, Tomas39aadf82008-12-19 10:37:32 +08001736 IWL_WARN(priv, "Aborted scan still in progress after 100ms\n");
Mohamed Abbasfde35712007-11-29 11:10:15 +08001737 IWL_DEBUG_MAC80211("leaving - scan abort failed.\n");
1738 return -EAGAIN;
1739 }
1740
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001741 iwl3945_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001742
1743 return 0;
1744}
1745
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001746static void iwl3945_build_tx_cmd_hwcrypto(struct iwl_priv *priv,
Johannes Berge039fa42008-05-15 12:55:29 +02001747 struct ieee80211_tx_info *info,
Winkler, Tomasc2d79b42008-12-19 10:37:34 +08001748 struct iwl_cmd *cmd,
Zhu Yib481de92007-09-25 17:54:57 -07001749 struct sk_buff *skb_frag,
1750 int last_frag)
1751{
Winkler, Tomase52119c2008-12-22 11:31:19 +08001752 struct iwl3945_tx_cmd *tx = (struct iwl3945_tx_cmd *)cmd->cmd.payload;
Ivo van Doorn1c014422008-04-17 19:41:02 +02001753 struct iwl3945_hw_key *keyinfo =
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001754 &priv->stations_39[info->control.hw_key->hw_key_idx].keyinfo;
Zhu Yib481de92007-09-25 17:54:57 -07001755
1756 switch (keyinfo->alg) {
1757 case ALG_CCMP:
Winkler, Tomase52119c2008-12-22 11:31:19 +08001758 tx->sec_ctl = TX_CMD_SEC_CCM;
1759 memcpy(tx->key, keyinfo->key, keyinfo->keylen);
Tomas Winklera96a27f2008-10-23 23:48:56 -07001760 IWL_DEBUG_TX("tx_cmd with AES hwcrypto\n");
Zhu Yib481de92007-09-25 17:54:57 -07001761 break;
1762
1763 case ALG_TKIP:
1764#if 0
Winkler, Tomase52119c2008-12-22 11:31:19 +08001765 tx->sec_ctl = TX_CMD_SEC_TKIP;
Zhu Yib481de92007-09-25 17:54:57 -07001766
1767 if (last_frag)
Winkler, Tomase52119c2008-12-22 11:31:19 +08001768 memcpy(tx->tkip_mic.byte, skb_frag->tail - 8,
Zhu Yib481de92007-09-25 17:54:57 -07001769 8);
1770 else
Winkler, Tomase52119c2008-12-22 11:31:19 +08001771 memset(tx->tkip_mic.byte, 0, 8);
Zhu Yib481de92007-09-25 17:54:57 -07001772#endif
1773 break;
1774
1775 case ALG_WEP:
Winkler, Tomase52119c2008-12-22 11:31:19 +08001776 tx->sec_ctl = TX_CMD_SEC_WEP |
Johannes Berge039fa42008-05-15 12:55:29 +02001777 (info->control.hw_key->hw_key_idx & TX_CMD_SEC_MSK) << TX_CMD_SEC_SHIFT;
Zhu Yib481de92007-09-25 17:54:57 -07001778
1779 if (keyinfo->keylen == 13)
Winkler, Tomase52119c2008-12-22 11:31:19 +08001780 tx->sec_ctl |= TX_CMD_SEC_KEY128;
Zhu Yib481de92007-09-25 17:54:57 -07001781
Winkler, Tomase52119c2008-12-22 11:31:19 +08001782 memcpy(&tx->key[3], keyinfo->key, keyinfo->keylen);
Zhu Yib481de92007-09-25 17:54:57 -07001783
1784 IWL_DEBUG_TX("Configuring packet for WEP encryption "
Johannes Berge039fa42008-05-15 12:55:29 +02001785 "with key %d\n", info->control.hw_key->hw_key_idx);
Zhu Yib481de92007-09-25 17:54:57 -07001786 break;
1787
Zhu Yib481de92007-09-25 17:54:57 -07001788 default:
Tomas Winkler978785a2008-12-19 10:37:31 +08001789 IWL_ERR(priv, "Unknown encode alg %d\n", keyinfo->alg);
Zhu Yib481de92007-09-25 17:54:57 -07001790 break;
1791 }
1792}
1793
1794/*
1795 * handle build REPLY_TX command notification.
1796 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001797static void iwl3945_build_tx_cmd_basic(struct iwl_priv *priv,
Winkler, Tomasc2d79b42008-12-19 10:37:34 +08001798 struct iwl_cmd *cmd,
Johannes Berge039fa42008-05-15 12:55:29 +02001799 struct ieee80211_tx_info *info,
Winkler, Tomase52119c2008-12-22 11:31:19 +08001800 struct ieee80211_hdr *hdr, u8 std_id)
Zhu Yib481de92007-09-25 17:54:57 -07001801{
Winkler, Tomase52119c2008-12-22 11:31:19 +08001802 struct iwl3945_tx_cmd *tx = (struct iwl3945_tx_cmd *)cmd->cmd.payload;
1803 __le32 tx_flags = tx->tx_flags;
Harvey Harrisonfd7c8a42008-06-11 14:21:56 -07001804 __le16 fc = hdr->frame_control;
Johannes Berge6a98542008-10-21 12:40:02 +02001805 u8 rc_flags = info->control.rates[0].flags;
Zhu Yib481de92007-09-25 17:54:57 -07001806
Winkler, Tomase52119c2008-12-22 11:31:19 +08001807 tx->stop_time.life_time = TX_CMD_LIFE_TIME_INFINITE;
Johannes Berge039fa42008-05-15 12:55:29 +02001808 if (!(info->flags & IEEE80211_TX_CTL_NO_ACK)) {
Zhu Yib481de92007-09-25 17:54:57 -07001809 tx_flags |= TX_CMD_FLG_ACK_MSK;
Harvey Harrisonfd7c8a42008-06-11 14:21:56 -07001810 if (ieee80211_is_mgmt(fc))
Zhu Yib481de92007-09-25 17:54:57 -07001811 tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
Harvey Harrisonfd7c8a42008-06-11 14:21:56 -07001812 if (ieee80211_is_probe_resp(fc) &&
Zhu Yib481de92007-09-25 17:54:57 -07001813 !(le16_to_cpu(hdr->seq_ctrl) & 0xf))
1814 tx_flags |= TX_CMD_FLG_TSF_MSK;
1815 } else {
1816 tx_flags &= (~TX_CMD_FLG_ACK_MSK);
1817 tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
1818 }
1819
Winkler, Tomase52119c2008-12-22 11:31:19 +08001820 tx->sta_id = std_id;
Harvey Harrison8b7b1e02008-06-11 14:21:56 -07001821 if (ieee80211_has_morefrags(fc))
Zhu Yib481de92007-09-25 17:54:57 -07001822 tx_flags |= TX_CMD_FLG_MORE_FRAG_MSK;
1823
Harvey Harrisonfd7c8a42008-06-11 14:21:56 -07001824 if (ieee80211_is_data_qos(fc)) {
1825 u8 *qc = ieee80211_get_qos_ctl(hdr);
Winkler, Tomase52119c2008-12-22 11:31:19 +08001826 tx->tid_tspec = qc[0] & 0xf;
Zhu Yib481de92007-09-25 17:54:57 -07001827 tx_flags &= ~TX_CMD_FLG_SEQ_CTL_MSK;
Tomas Winkler54dbb522008-05-15 13:54:06 +08001828 } else {
Zhu Yib481de92007-09-25 17:54:57 -07001829 tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
Tomas Winkler54dbb522008-05-15 13:54:06 +08001830 }
Zhu Yib481de92007-09-25 17:54:57 -07001831
Johannes Berge6a98542008-10-21 12:40:02 +02001832 if (rc_flags & IEEE80211_TX_RC_USE_RTS_CTS) {
Zhu Yib481de92007-09-25 17:54:57 -07001833 tx_flags |= TX_CMD_FLG_RTS_MSK;
1834 tx_flags &= ~TX_CMD_FLG_CTS_MSK;
Johannes Berge6a98542008-10-21 12:40:02 +02001835 } else if (rc_flags & IEEE80211_TX_RC_USE_CTS_PROTECT) {
Zhu Yib481de92007-09-25 17:54:57 -07001836 tx_flags &= ~TX_CMD_FLG_RTS_MSK;
1837 tx_flags |= TX_CMD_FLG_CTS_MSK;
1838 }
1839
1840 if ((tx_flags & TX_CMD_FLG_RTS_MSK) || (tx_flags & TX_CMD_FLG_CTS_MSK))
1841 tx_flags |= TX_CMD_FLG_FULL_TXOP_PROT_MSK;
1842
1843 tx_flags &= ~(TX_CMD_FLG_ANT_SEL_MSK);
Harvey Harrisonfd7c8a42008-06-11 14:21:56 -07001844 if (ieee80211_is_mgmt(fc)) {
1845 if (ieee80211_is_assoc_req(fc) || ieee80211_is_reassoc_req(fc))
Winkler, Tomase52119c2008-12-22 11:31:19 +08001846 tx->timeout.pm_frame_timeout = cpu_to_le16(3);
Zhu Yib481de92007-09-25 17:54:57 -07001847 else
Winkler, Tomase52119c2008-12-22 11:31:19 +08001848 tx->timeout.pm_frame_timeout = cpu_to_le16(2);
Mohamed Abbasab53d8a2008-03-25 16:33:36 -07001849 } else {
Winkler, Tomase52119c2008-12-22 11:31:19 +08001850 tx->timeout.pm_frame_timeout = 0;
Mohamed Abbasab53d8a2008-03-25 16:33:36 -07001851#ifdef CONFIG_IWL3945_LEDS
1852 priv->rxtxpackets += le16_to_cpu(cmd->cmd.tx.len);
1853#endif
1854 }
Zhu Yib481de92007-09-25 17:54:57 -07001855
Winkler, Tomase52119c2008-12-22 11:31:19 +08001856 tx->driver_txop = 0;
1857 tx->tx_flags = tx_flags;
1858 tx->next_frame_len = 0;
Zhu Yib481de92007-09-25 17:54:57 -07001859}
1860
Cahill, Ben M6440adb2007-11-29 11:09:55 +08001861/**
1862 * iwl3945_get_sta_id - Find station's index within station table
1863 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001864static int iwl3945_get_sta_id(struct iwl_priv *priv, struct ieee80211_hdr *hdr)
Zhu Yib481de92007-09-25 17:54:57 -07001865{
1866 int sta_id;
1867 u16 fc = le16_to_cpu(hdr->frame_control);
1868
Cahill, Ben M6440adb2007-11-29 11:09:55 +08001869 /* If this frame is broadcast or management, use broadcast station id */
Zhu Yib481de92007-09-25 17:54:57 -07001870 if (((fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_DATA) ||
1871 is_multicast_ether_addr(hdr->addr1))
Abhijeet Kolekar3832ec92008-12-19 10:37:26 +08001872 return priv->hw_params.bcast_sta_id;
Zhu Yib481de92007-09-25 17:54:57 -07001873
1874 switch (priv->iw_mode) {
1875
Cahill, Ben M6440adb2007-11-29 11:09:55 +08001876 /* If we are a client station in a BSS network, use the special
1877 * AP station entry (that's the only station we communicate with) */
Johannes Berg05c914f2008-09-11 00:01:58 +02001878 case NL80211_IFTYPE_STATION:
Zhu Yib481de92007-09-25 17:54:57 -07001879 return IWL_AP_ID;
1880
1881 /* If we are an AP, then find the station, or use BCAST */
Johannes Berg05c914f2008-09-11 00:01:58 +02001882 case NL80211_IFTYPE_AP:
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001883 sta_id = iwl3945_hw_find_station(priv, hdr->addr1);
Zhu Yib481de92007-09-25 17:54:57 -07001884 if (sta_id != IWL_INVALID_STATION)
1885 return sta_id;
Abhijeet Kolekar3832ec92008-12-19 10:37:26 +08001886 return priv->hw_params.bcast_sta_id;
Zhu Yib481de92007-09-25 17:54:57 -07001887
Cahill, Ben M6440adb2007-11-29 11:09:55 +08001888 /* If this frame is going out to an IBSS network, find the station,
1889 * or create a new station table entry */
Johannes Berg05c914f2008-09-11 00:01:58 +02001890 case NL80211_IFTYPE_ADHOC: {
Cahill, Ben M6440adb2007-11-29 11:09:55 +08001891 /* Create new station table entry */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001892 sta_id = iwl3945_hw_find_station(priv, hdr->addr1);
Zhu Yib481de92007-09-25 17:54:57 -07001893 if (sta_id != IWL_INVALID_STATION)
1894 return sta_id;
1895
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001896 sta_id = iwl3945_add_station(priv, hdr->addr1, 0, CMD_ASYNC);
Zhu Yib481de92007-09-25 17:54:57 -07001897
1898 if (sta_id != IWL_INVALID_STATION)
1899 return sta_id;
1900
Johannes Berge1749612008-10-27 15:59:26 -07001901 IWL_DEBUG_DROP("Station %pM not in station map. "
Zhu Yib481de92007-09-25 17:54:57 -07001902 "Defaulting to broadcast...\n",
Johannes Berge1749612008-10-27 15:59:26 -07001903 hdr->addr1);
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08001904 iwl_print_hex_dump(priv, IWL_DL_DROP, (u8 *) hdr, sizeof(*hdr));
Abhijeet Kolekar3832ec92008-12-19 10:37:26 +08001905 return priv->hw_params.bcast_sta_id;
Joe Perches0795af52007-10-03 17:59:30 -07001906 }
Stefanik Gábor914233d2008-06-30 17:23:30 +08001907 /* If we are in monitor mode, use BCAST. This is required for
1908 * packet injection. */
Johannes Berg05c914f2008-09-11 00:01:58 +02001909 case NL80211_IFTYPE_MONITOR:
Abhijeet Kolekar3832ec92008-12-19 10:37:26 +08001910 return priv->hw_params.bcast_sta_id;
Stefanik Gábor914233d2008-06-30 17:23:30 +08001911
Zhu Yib481de92007-09-25 17:54:57 -07001912 default:
Winkler, Tomas39aadf82008-12-19 10:37:32 +08001913 IWL_WARN(priv, "Unknown mode of operation: %d\n",
1914 priv->iw_mode);
Abhijeet Kolekar3832ec92008-12-19 10:37:26 +08001915 return priv->hw_params.bcast_sta_id;
Zhu Yib481de92007-09-25 17:54:57 -07001916 }
1917}
1918
1919/*
1920 * start REPLY_TX command process
1921 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001922static int iwl3945_tx_skb(struct iwl_priv *priv, struct sk_buff *skb)
Zhu Yib481de92007-09-25 17:54:57 -07001923{
1924 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
Johannes Berge039fa42008-05-15 12:55:29 +02001925 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
Winkler, Tomase52119c2008-12-22 11:31:19 +08001926 struct iwl3945_tx_cmd *tx;
Samuel Ortiz188cf6c2008-12-22 11:31:16 +08001927 struct iwl_tx_queue *txq = NULL;
Samuel Ortizd20b3c62008-12-19 10:37:15 +08001928 struct iwl_queue *q = NULL;
Winkler, Tomase52119c2008-12-22 11:31:19 +08001929 struct iwl_cmd *out_cmd = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07001930 dma_addr_t phys_addr;
1931 dma_addr_t txcmd_phys;
Winkler, Tomase52119c2008-12-22 11:31:19 +08001932 int txq_id = skb_get_queue_mapping(skb);
Tomas Winkler54dbb522008-05-15 13:54:06 +08001933 u16 len, idx, len_org, hdr_len;
1934 u8 id;
1935 u8 unicast;
Zhu Yib481de92007-09-25 17:54:57 -07001936 u8 sta_id;
Tomas Winkler54dbb522008-05-15 13:54:06 +08001937 u8 tid = 0;
Zhu Yib481de92007-09-25 17:54:57 -07001938 u16 seq_number = 0;
Harvey Harrisonfd7c8a42008-06-11 14:21:56 -07001939 __le16 fc;
Zhu Yib481de92007-09-25 17:54:57 -07001940 u8 wait_write_ptr = 0;
Tomas Winkler54dbb522008-05-15 13:54:06 +08001941 u8 *qc = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07001942 unsigned long flags;
1943 int rc;
1944
1945 spin_lock_irqsave(&priv->lock, flags);
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08001946 if (iwl_is_rfkill(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07001947 IWL_DEBUG_DROP("Dropping - RF KILL\n");
1948 goto drop_unlock;
1949 }
1950
Johannes Berge039fa42008-05-15 12:55:29 +02001951 if ((ieee80211_get_tx_rate(priv->hw, info)->hw_value & 0xFF) == IWL_INVALID_RATE) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08001952 IWL_ERR(priv, "ERROR: No TX rate available.\n");
Zhu Yib481de92007-09-25 17:54:57 -07001953 goto drop_unlock;
1954 }
1955
1956 unicast = !is_multicast_ether_addr(hdr->addr1);
1957 id = 0;
1958
Harvey Harrisonfd7c8a42008-06-11 14:21:56 -07001959 fc = hdr->frame_control;
Zhu Yib481de92007-09-25 17:54:57 -07001960
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08001961#ifdef CONFIG_IWL3945_DEBUG
Zhu Yib481de92007-09-25 17:54:57 -07001962 if (ieee80211_is_auth(fc))
1963 IWL_DEBUG_TX("Sending AUTH frame\n");
Harvey Harrisonfd7c8a42008-06-11 14:21:56 -07001964 else if (ieee80211_is_assoc_req(fc))
Zhu Yib481de92007-09-25 17:54:57 -07001965 IWL_DEBUG_TX("Sending ASSOC frame\n");
Harvey Harrisonfd7c8a42008-06-11 14:21:56 -07001966 else if (ieee80211_is_reassoc_req(fc))
Zhu Yib481de92007-09-25 17:54:57 -07001967 IWL_DEBUG_TX("Sending REASSOC frame\n");
1968#endif
1969
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08001970 /* drop all data frame if we are not associated */
Stefanik Gábor914233d2008-06-30 17:23:30 +08001971 if (ieee80211_is_data(fc) &&
Johannes Berg05c914f2008-09-11 00:01:58 +02001972 (priv->iw_mode != NL80211_IFTYPE_MONITOR) && /* packet injection */
Stefanik Gábor914233d2008-06-30 17:23:30 +08001973 (!iwl3945_is_associated(priv) ||
Johannes Berg05c914f2008-09-11 00:01:58 +02001974 ((priv->iw_mode == NL80211_IFTYPE_STATION) && !priv->assoc_id))) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001975 IWL_DEBUG_DROP("Dropping - !iwl3945_is_associated\n");
Zhu Yib481de92007-09-25 17:54:57 -07001976 goto drop_unlock;
1977 }
1978
1979 spin_unlock_irqrestore(&priv->lock, flags);
1980
Harvey Harrison7294ec92008-07-15 18:43:59 -07001981 hdr_len = ieee80211_hdrlen(fc);
Cahill, Ben M6440adb2007-11-29 11:09:55 +08001982
1983 /* Find (or create) index into station table for destination station */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001984 sta_id = iwl3945_get_sta_id(priv, hdr);
Zhu Yib481de92007-09-25 17:54:57 -07001985 if (sta_id == IWL_INVALID_STATION) {
Johannes Berge1749612008-10-27 15:59:26 -07001986 IWL_DEBUG_DROP("Dropping - INVALID STATION: %pM\n",
1987 hdr->addr1);
Zhu Yib481de92007-09-25 17:54:57 -07001988 goto drop;
1989 }
1990
1991 IWL_DEBUG_RATE("station Id %d\n", sta_id);
1992
Harvey Harrisonfd7c8a42008-06-11 14:21:56 -07001993 if (ieee80211_is_data_qos(fc)) {
1994 qc = ieee80211_get_qos_ctl(hdr);
Harvey Harrison7294ec92008-07-15 18:43:59 -07001995 tid = qc[0] & IEEE80211_QOS_CTL_TID_MASK;
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001996 seq_number = priv->stations_39[sta_id].tid[tid].seq_number &
Zhu Yib481de92007-09-25 17:54:57 -07001997 IEEE80211_SCTL_SEQ;
1998 hdr->seq_ctrl = cpu_to_le16(seq_number) |
1999 (hdr->seq_ctrl &
2000 __constant_cpu_to_le16(IEEE80211_SCTL_FRAG));
2001 seq_number += 0x10;
2002 }
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002003
2004 /* Descriptor for chosen Tx queue */
Samuel Ortiz188cf6c2008-12-22 11:31:16 +08002005 txq = &priv->txq[txq_id];
Zhu Yib481de92007-09-25 17:54:57 -07002006 q = &txq->q;
2007
2008 spin_lock_irqsave(&priv->lock, flags);
2009
Tomas Winklerfc4b6852007-10-25 17:15:24 +08002010 idx = get_cmd_index(q, q->write_ptr, 0);
Zhu Yib481de92007-09-25 17:54:57 -07002011
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002012 /* Set up driver data for this TFD */
Winkler, Tomasdbb66542008-12-22 11:31:14 +08002013 memset(&(txq->txb[q->write_ptr]), 0, sizeof(struct iwl_tx_info));
Tomas Winklerfc4b6852007-10-25 17:15:24 +08002014 txq->txb[q->write_ptr].skb[0] = skb;
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002015
2016 /* Init first empty entry in queue's array of Tx/cmd buffers */
Samuel Ortiz188cf6c2008-12-22 11:31:16 +08002017 out_cmd = txq->cmd[idx];
Winkler, Tomase52119c2008-12-22 11:31:19 +08002018 tx = (struct iwl3945_tx_cmd *)out_cmd->cmd.payload;
Zhu Yib481de92007-09-25 17:54:57 -07002019 memset(&out_cmd->hdr, 0, sizeof(out_cmd->hdr));
Winkler, Tomase52119c2008-12-22 11:31:19 +08002020 memset(tx, 0, sizeof(*tx));
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002021
2022 /*
2023 * Set up the Tx-command (not MAC!) header.
2024 * Store the chosen Tx queue and TFD index within the sequence field;
2025 * after Tx, uCode's Tx response will return this value so driver can
2026 * locate the frame within the tx queue and do post-tx processing.
2027 */
Zhu Yib481de92007-09-25 17:54:57 -07002028 out_cmd->hdr.cmd = REPLY_TX;
2029 out_cmd->hdr.sequence = cpu_to_le16((u16)(QUEUE_TO_SEQ(txq_id) |
Tomas Winklerfc4b6852007-10-25 17:15:24 +08002030 INDEX_TO_SEQ(q->write_ptr)));
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002031
2032 /* Copy MAC header from skb into command buffer */
Winkler, Tomase52119c2008-12-22 11:31:19 +08002033 memcpy(tx->hdr, hdr, hdr_len);
Zhu Yib481de92007-09-25 17:54:57 -07002034
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002035 /*
2036 * Use the first empty entry in this queue's command buffer array
2037 * to contain the Tx command and MAC header concatenated together
2038 * (payload data will be in another buffer).
2039 * Size of this varies, due to varying MAC header length.
2040 * If end is not dword aligned, we'll have 2 extra bytes at the end
2041 * of the MAC header (device reads on dword boundaries).
2042 * We'll tell device about this padding later.
2043 */
Abhijeet Kolekar3832ec92008-12-19 10:37:26 +08002044 len = sizeof(struct iwl3945_tx_cmd) +
Tomas Winkler4c897252008-12-19 10:37:05 +08002045 sizeof(struct iwl_cmd_header) + hdr_len;
Zhu Yib481de92007-09-25 17:54:57 -07002046
2047 len_org = len;
2048 len = (len + 3) & ~3;
2049
2050 if (len_org != len)
2051 len_org = 1;
2052 else
2053 len_org = 0;
2054
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002055 /* Physical address of this Tx command's header (not MAC header!),
2056 * within command buffer array. */
Samuel Ortiz188cf6c2008-12-22 11:31:16 +08002057 txcmd_phys = pci_map_single(priv->pci_dev,
2058 out_cmd, sizeof(struct iwl_cmd),
2059 PCI_DMA_TODEVICE);
2060 pci_unmap_addr_set(&out_cmd->meta, mapping, txcmd_phys);
2061 pci_unmap_len_set(&out_cmd->meta, len, sizeof(struct iwl_cmd));
2062 /* Add buffer containing Tx command and MAC(!) header to TFD's
2063 * first entry */
2064 txcmd_phys += offsetof(struct iwl_cmd, hdr);
Zhu Yib481de92007-09-25 17:54:57 -07002065
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002066 /* Add buffer containing Tx command and MAC(!) header to TFD's
2067 * first entry */
Samuel Ortiz7aaa1d72009-01-19 15:30:26 -08002068 priv->cfg->ops->lib->txq_attach_buf_to_tfd(priv, txq,
2069 txcmd_phys, len, 1, 0);
Zhu Yib481de92007-09-25 17:54:57 -07002070
Johannes Bergd0f09802008-07-29 11:32:07 +02002071 if (info->control.hw_key)
Johannes Berge039fa42008-05-15 12:55:29 +02002072 iwl3945_build_tx_cmd_hwcrypto(priv, info, out_cmd, skb, 0);
Zhu Yib481de92007-09-25 17:54:57 -07002073
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002074 /* Set up TFD's 2nd entry to point directly to remainder of skb,
2075 * if any (802.11 null frames have no payload). */
Zhu Yib481de92007-09-25 17:54:57 -07002076 len = skb->len - hdr_len;
2077 if (len) {
2078 phys_addr = pci_map_single(priv->pci_dev, skb->data + hdr_len,
2079 len, PCI_DMA_TODEVICE);
Samuel Ortiz7aaa1d72009-01-19 15:30:26 -08002080 priv->cfg->ops->lib->txq_attach_buf_to_tfd(priv, txq,
2081 phys_addr, len,
2082 0, U32_PAD(len));
Zhu Yib481de92007-09-25 17:54:57 -07002083 }
2084
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002085 /* Total # bytes to be transmitted */
Zhu Yib481de92007-09-25 17:54:57 -07002086 len = (u16)skb->len;
Winkler, Tomase52119c2008-12-22 11:31:19 +08002087 tx->len = cpu_to_le16(len);
Zhu Yib481de92007-09-25 17:54:57 -07002088
2089 /* TODO need this for burst mode later on */
Winkler, Tomase52119c2008-12-22 11:31:19 +08002090 iwl3945_build_tx_cmd_basic(priv, out_cmd, info, hdr, sta_id);
Zhu Yib481de92007-09-25 17:54:57 -07002091
2092 /* set is_hcca to 0; it probably will never be implemented */
Johannes Berge039fa42008-05-15 12:55:29 +02002093 iwl3945_hw_build_tx_cmd_rate(priv, out_cmd, info, hdr, sta_id, 0);
Zhu Yib481de92007-09-25 17:54:57 -07002094
Winkler, Tomase52119c2008-12-22 11:31:19 +08002095 tx->tx_flags &= ~TX_CMD_FLG_ANT_A_MSK;
2096 tx->tx_flags &= ~TX_CMD_FLG_ANT_B_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07002097
Harvey Harrison8b7b1e02008-06-11 14:21:56 -07002098 if (!ieee80211_has_morefrags(hdr->frame_control)) {
Zhu Yib481de92007-09-25 17:54:57 -07002099 txq->need_update = 1;
Tomas Winkler3ac7f142008-07-21 02:40:14 +03002100 if (qc)
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08002101 priv->stations_39[sta_id].tid[tid].seq_number = seq_number;
Zhu Yib481de92007-09-25 17:54:57 -07002102 } else {
2103 wait_write_ptr = 1;
2104 txq->need_update = 0;
2105 }
2106
Winkler, Tomase52119c2008-12-22 11:31:19 +08002107 iwl_print_hex_dump(priv, IWL_DL_TX, tx, sizeof(*tx));
Zhu Yib481de92007-09-25 17:54:57 -07002108
Winkler, Tomase52119c2008-12-22 11:31:19 +08002109 iwl_print_hex_dump(priv, IWL_DL_TX, (u8 *)tx->hdr,
Harvey Harrison7294ec92008-07-15 18:43:59 -07002110 ieee80211_hdrlen(fc));
Zhu Yib481de92007-09-25 17:54:57 -07002111
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002112 /* Tell device the write index *just past* this latest filled TFD */
Tomas Winklerc54b6792008-03-06 17:36:53 -08002113 q->write_ptr = iwl_queue_inc_wrap(q->write_ptr, q->n_bd);
Samuel Ortiz4f3602c2009-01-19 15:30:25 -08002114 rc = iwl_txq_update_write_ptr(priv, txq);
Zhu Yib481de92007-09-25 17:54:57 -07002115 spin_unlock_irqrestore(&priv->lock, flags);
2116
2117 if (rc)
2118 return rc;
2119
Samuel Ortizd20b3c62008-12-19 10:37:15 +08002120 if ((iwl_queue_space(q) < q->high_mark)
Zhu Yib481de92007-09-25 17:54:57 -07002121 && priv->mac80211_registered) {
2122 if (wait_write_ptr) {
2123 spin_lock_irqsave(&priv->lock, flags);
2124 txq->need_update = 1;
Samuel Ortiz4f3602c2009-01-19 15:30:25 -08002125 iwl_txq_update_write_ptr(priv, txq);
Zhu Yib481de92007-09-25 17:54:57 -07002126 spin_unlock_irqrestore(&priv->lock, flags);
2127 }
2128
Johannes Berge2530082008-05-17 00:57:14 +02002129 ieee80211_stop_queue(priv->hw, skb_get_queue_mapping(skb));
Zhu Yib481de92007-09-25 17:54:57 -07002130 }
2131
2132 return 0;
2133
2134drop_unlock:
2135 spin_unlock_irqrestore(&priv->lock, flags);
2136drop:
2137 return -1;
2138}
2139
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002140static void iwl3945_set_rate(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002141{
Johannes Berg8318d782008-01-24 19:38:38 +01002142 const struct ieee80211_supported_band *sband = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07002143 struct ieee80211_rate *rate;
2144 int i;
2145
Kolekar, Abhijeetcbba18c2008-12-19 10:37:42 +08002146 sband = iwl_get_hw_mode(priv, priv->band);
Johannes Berg8318d782008-01-24 19:38:38 +01002147 if (!sband) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08002148 IWL_ERR(priv, "Failed to set rate: unable to get hw mode\n");
Saleem Abdulrasoolc4ba9622007-11-18 23:59:08 -08002149 return;
2150 }
Zhu Yib481de92007-09-25 17:54:57 -07002151
2152 priv->active_rate = 0;
2153 priv->active_rate_basic = 0;
2154
Johannes Berg8318d782008-01-24 19:38:38 +01002155 IWL_DEBUG_RATE("Setting rates for %s GHz\n",
2156 sband->band == IEEE80211_BAND_2GHZ ? "2.4" : "5");
Zhu Yib481de92007-09-25 17:54:57 -07002157
Johannes Berg8318d782008-01-24 19:38:38 +01002158 for (i = 0; i < sband->n_bitrates; i++) {
2159 rate = &sband->bitrates[i];
2160 if ((rate->hw_value < IWL_RATE_COUNT) &&
2161 !(rate->flags & IEEE80211_CHAN_DISABLED)) {
2162 IWL_DEBUG_RATE("Adding rate index %d (plcp %d)\n",
2163 rate->hw_value, iwl3945_rates[rate->hw_value].plcp);
2164 priv->active_rate |= (1 << rate->hw_value);
2165 }
Zhu Yib481de92007-09-25 17:54:57 -07002166 }
2167
2168 IWL_DEBUG_RATE("Set active_rate = %0x, active_rate_basic = %0x\n",
2169 priv->active_rate, priv->active_rate_basic);
2170
2171 /*
2172 * If a basic rate is configured, then use it (adding IWL_RATE_1M_MASK)
2173 * otherwise set it to the default of all CCK rates and 6, 12, 24 for
2174 * OFDM
2175 */
2176 if (priv->active_rate_basic & IWL_CCK_BASIC_RATES_MASK)
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08002177 priv->staging39_rxon.cck_basic_rates =
Zhu Yib481de92007-09-25 17:54:57 -07002178 ((priv->active_rate_basic &
2179 IWL_CCK_RATES_MASK) >> IWL_FIRST_CCK_RATE) & 0xF;
2180 else
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08002181 priv->staging39_rxon.cck_basic_rates =
Zhu Yib481de92007-09-25 17:54:57 -07002182 (IWL_CCK_BASIC_RATES_MASK >> IWL_FIRST_CCK_RATE) & 0xF;
2183
2184 if (priv->active_rate_basic & IWL_OFDM_BASIC_RATES_MASK)
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08002185 priv->staging39_rxon.ofdm_basic_rates =
Zhu Yib481de92007-09-25 17:54:57 -07002186 ((priv->active_rate_basic &
2187 (IWL_OFDM_BASIC_RATES_MASK | IWL_RATE_6M_MASK)) >>
2188 IWL_FIRST_OFDM_RATE) & 0xFF;
2189 else
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08002190 priv->staging39_rxon.ofdm_basic_rates =
Zhu Yib481de92007-09-25 17:54:57 -07002191 (IWL_OFDM_BASIC_RATES_MASK >> IWL_FIRST_OFDM_RATE) & 0xFF;
2192}
2193
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002194static void iwl3945_radio_kill_sw(struct iwl_priv *priv, int disable_radio)
Zhu Yib481de92007-09-25 17:54:57 -07002195{
2196 unsigned long flags;
2197
2198 if (!!disable_radio == test_bit(STATUS_RF_KILL_SW, &priv->status))
2199 return;
2200
2201 IWL_DEBUG_RF_KILL("Manual SW RF KILL set to: RADIO %s\n",
2202 disable_radio ? "OFF" : "ON");
2203
2204 if (disable_radio) {
Winkler, Tomasaf0053d2009-01-19 15:30:23 -08002205 iwl_scan_cancel(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002206 /* FIXME: This is a workaround for AP */
Johannes Berg05c914f2008-09-11 00:01:58 +02002207 if (priv->iw_mode != NL80211_IFTYPE_AP) {
Zhu Yib481de92007-09-25 17:54:57 -07002208 spin_lock_irqsave(&priv->lock, flags);
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08002209 iwl_write32(priv, CSR_UCODE_DRV_GP1_SET,
Zhu Yib481de92007-09-25 17:54:57 -07002210 CSR_UCODE_SW_BIT_RFKILL);
2211 spin_unlock_irqrestore(&priv->lock, flags);
Samuel Ortizc4962942008-12-22 11:31:18 +08002212 iwl_send_card_state(priv, CARD_STATE_CMD_DISABLE, 0);
Zhu Yib481de92007-09-25 17:54:57 -07002213 set_bit(STATUS_RF_KILL_SW, &priv->status);
2214 }
2215 return;
2216 }
2217
2218 spin_lock_irqsave(&priv->lock, flags);
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08002219 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
Zhu Yib481de92007-09-25 17:54:57 -07002220
2221 clear_bit(STATUS_RF_KILL_SW, &priv->status);
2222 spin_unlock_irqrestore(&priv->lock, flags);
2223
2224 /* wake up ucode */
2225 msleep(10);
2226
2227 spin_lock_irqsave(&priv->lock, flags);
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08002228 iwl_read32(priv, CSR_UCODE_DRV_GP1);
2229 if (!iwl_grab_nic_access(priv))
2230 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002231 spin_unlock_irqrestore(&priv->lock, flags);
2232
2233 if (test_bit(STATUS_RF_KILL_HW, &priv->status)) {
2234 IWL_DEBUG_RF_KILL("Can not turn radio back on - "
2235 "disabled by HW switch\n");
2236 return;
2237 }
2238
Zhu Yi808e72a2008-06-12 09:47:17 +08002239 if (priv->is_open)
2240 queue_work(priv->workqueue, &priv->restart);
Zhu Yib481de92007-09-25 17:54:57 -07002241 return;
2242}
2243
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002244void iwl3945_set_decrypted_flag(struct iwl_priv *priv, struct sk_buff *skb,
Zhu Yib481de92007-09-25 17:54:57 -07002245 u32 decrypt_res, struct ieee80211_rx_status *stats)
2246{
2247 u16 fc =
2248 le16_to_cpu(((struct ieee80211_hdr *)skb->data)->frame_control);
2249
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08002250 if (priv->active39_rxon.filter_flags & RXON_FILTER_DIS_DECRYPT_MSK)
Zhu Yib481de92007-09-25 17:54:57 -07002251 return;
2252
2253 if (!(fc & IEEE80211_FCTL_PROTECTED))
2254 return;
2255
2256 IWL_DEBUG_RX("decrypt_res:0x%x\n", decrypt_res);
2257 switch (decrypt_res & RX_RES_STATUS_SEC_TYPE_MSK) {
2258 case RX_RES_STATUS_SEC_TYPE_TKIP:
2259 if ((decrypt_res & RX_RES_STATUS_DECRYPT_TYPE_MSK) ==
2260 RX_RES_STATUS_BAD_ICV_MIC)
2261 stats->flag |= RX_FLAG_MMIC_ERROR;
2262 case RX_RES_STATUS_SEC_TYPE_WEP:
2263 case RX_RES_STATUS_SEC_TYPE_CCMP:
2264 if ((decrypt_res & RX_RES_STATUS_DECRYPT_TYPE_MSK) ==
2265 RX_RES_STATUS_DECRYPT_OK) {
2266 IWL_DEBUG_RX("hw decrypt successfully!!!\n");
2267 stats->flag |= RX_FLAG_DECRYPTED;
2268 }
2269 break;
2270
2271 default:
2272 break;
2273 }
2274}
2275
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08002276#ifdef CONFIG_IWL3945_SPECTRUM_MEASUREMENT
Zhu Yib481de92007-09-25 17:54:57 -07002277
2278#include "iwl-spectrum.h"
2279
2280#define BEACON_TIME_MASK_LOW 0x00FFFFFF
2281#define BEACON_TIME_MASK_HIGH 0xFF000000
2282#define TIME_UNIT 1024
2283
2284/*
2285 * extended beacon time format
2286 * time in usec will be changed into a 32-bit value in 8:24 format
2287 * the high 1 byte is the beacon counts
2288 * the lower 3 bytes is the time in usec within one beacon interval
2289 */
2290
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002291static u32 iwl3945_usecs_to_beacons(u32 usec, u32 beacon_interval)
Zhu Yib481de92007-09-25 17:54:57 -07002292{
2293 u32 quot;
2294 u32 rem;
2295 u32 interval = beacon_interval * 1024;
2296
2297 if (!interval || !usec)
2298 return 0;
2299
2300 quot = (usec / interval) & (BEACON_TIME_MASK_HIGH >> 24);
2301 rem = (usec % interval) & BEACON_TIME_MASK_LOW;
2302
2303 return (quot << 24) + rem;
2304}
2305
2306/* base is usually what we get from ucode with each received frame,
2307 * the same as HW timer counter counting down
2308 */
2309
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002310static __le32 iwl3945_add_beacon_time(u32 base, u32 addon, u32 beacon_interval)
Zhu Yib481de92007-09-25 17:54:57 -07002311{
2312 u32 base_low = base & BEACON_TIME_MASK_LOW;
2313 u32 addon_low = addon & BEACON_TIME_MASK_LOW;
2314 u32 interval = beacon_interval * TIME_UNIT;
2315 u32 res = (base & BEACON_TIME_MASK_HIGH) +
2316 (addon & BEACON_TIME_MASK_HIGH);
2317
2318 if (base_low > addon_low)
2319 res += base_low - addon_low;
2320 else if (base_low < addon_low) {
2321 res += interval + base_low - addon_low;
2322 res += (1 << 24);
2323 } else
2324 res += (1 << 24);
2325
2326 return cpu_to_le32(res);
2327}
2328
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002329static int iwl3945_get_measurement(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07002330 struct ieee80211_measurement_params *params,
2331 u8 type)
2332{
Tomas Winkler600c0e12008-12-19 10:37:04 +08002333 struct iwl_spectrum_cmd spectrum;
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08002334 struct iwl_rx_packet *res;
Winkler, Tomasc2d79b42008-12-19 10:37:34 +08002335 struct iwl_host_cmd cmd = {
Zhu Yib481de92007-09-25 17:54:57 -07002336 .id = REPLY_SPECTRUM_MEASUREMENT_CMD,
2337 .data = (void *)&spectrum,
2338 .meta.flags = CMD_WANT_SKB,
2339 };
2340 u32 add_time = le64_to_cpu(params->start_time);
2341 int rc;
2342 int spectrum_resp_status;
2343 int duration = le16_to_cpu(params->duration);
2344
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002345 if (iwl3945_is_associated(priv))
Zhu Yib481de92007-09-25 17:54:57 -07002346 add_time =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002347 iwl3945_usecs_to_beacons(
Zhu Yib481de92007-09-25 17:54:57 -07002348 le64_to_cpu(params->start_time) - priv->last_tsf,
2349 le16_to_cpu(priv->rxon_timing.beacon_interval));
2350
2351 memset(&spectrum, 0, sizeof(spectrum));
2352
2353 spectrum.channel_count = cpu_to_le16(1);
2354 spectrum.flags =
2355 RXON_FLG_TSF2HOST_MSK | RXON_FLG_ANT_A_MSK | RXON_FLG_DIS_DIV_MSK;
2356 spectrum.filter_flags = MEASUREMENT_FILTER_FLAG;
2357 cmd.len = sizeof(spectrum);
2358 spectrum.len = cpu_to_le16(cmd.len - sizeof(spectrum.len));
2359
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002360 if (iwl3945_is_associated(priv))
Zhu Yib481de92007-09-25 17:54:57 -07002361 spectrum.start_time =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002362 iwl3945_add_beacon_time(priv->last_beacon_time,
Zhu Yib481de92007-09-25 17:54:57 -07002363 add_time,
2364 le16_to_cpu(priv->rxon_timing.beacon_interval));
2365 else
2366 spectrum.start_time = 0;
2367
2368 spectrum.channels[0].duration = cpu_to_le32(duration * TIME_UNIT);
2369 spectrum.channels[0].channel = params->channel;
2370 spectrum.channels[0].type = type;
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08002371 if (priv->active39_rxon.flags & RXON_FLG_BAND_24G_MSK)
Zhu Yib481de92007-09-25 17:54:57 -07002372 spectrum.flags |= RXON_FLG_BAND_24G_MSK |
2373 RXON_FLG_AUTO_DETECT_MSK | RXON_FLG_TGG_PROTECT_MSK;
2374
Samuel Ortiz518099a2009-01-19 15:30:27 -08002375 rc = iwl_send_cmd_sync(priv, &cmd);
Zhu Yib481de92007-09-25 17:54:57 -07002376 if (rc)
2377 return rc;
2378
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08002379 res = (struct iwl_rx_packet *)cmd.meta.u.skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07002380 if (res->hdr.flags & IWL_CMD_FAILED_MSK) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08002381 IWL_ERR(priv, "Bad return from REPLY_RX_ON_ASSOC command\n");
Zhu Yib481de92007-09-25 17:54:57 -07002382 rc = -EIO;
2383 }
2384
2385 spectrum_resp_status = le16_to_cpu(res->u.spectrum.status);
2386 switch (spectrum_resp_status) {
2387 case 0: /* Command will be handled */
2388 if (res->u.spectrum.id != 0xff) {
Ian Schrambc434dd2007-10-25 17:15:29 +08002389 IWL_DEBUG_INFO("Replaced existing measurement: %d\n",
2390 res->u.spectrum.id);
Zhu Yib481de92007-09-25 17:54:57 -07002391 priv->measurement_status &= ~MEASUREMENT_READY;
2392 }
2393 priv->measurement_status |= MEASUREMENT_ACTIVE;
2394 rc = 0;
2395 break;
2396
2397 case 1: /* Command will not be handled */
2398 rc = -EAGAIN;
2399 break;
2400 }
2401
2402 dev_kfree_skb_any(cmd.meta.u.skb);
2403
2404 return rc;
2405}
2406#endif
2407
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002408static void iwl3945_rx_reply_alive(struct iwl_priv *priv,
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08002409 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07002410{
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08002411 struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
2412 struct iwl_alive_resp *palive;
Zhu Yib481de92007-09-25 17:54:57 -07002413 struct delayed_work *pwork;
2414
2415 palive = &pkt->u.alive_frame;
2416
2417 IWL_DEBUG_INFO("Alive ucode status 0x%08X revision "
2418 "0x%01X 0x%01X\n",
2419 palive->is_valid, palive->ver_type,
2420 palive->ver_subtype);
2421
2422 if (palive->ver_subtype == INITIALIZE_SUBTYPE) {
2423 IWL_DEBUG_INFO("Initialization Alive received.\n");
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08002424 memcpy(&priv->card_alive_init, &pkt->u.alive_frame,
2425 sizeof(struct iwl_alive_resp));
Zhu Yib481de92007-09-25 17:54:57 -07002426 pwork = &priv->init_alive_start;
2427 } else {
2428 IWL_DEBUG_INFO("Runtime Alive received.\n");
2429 memcpy(&priv->card_alive, &pkt->u.alive_frame,
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08002430 sizeof(struct iwl_alive_resp));
Zhu Yib481de92007-09-25 17:54:57 -07002431 pwork = &priv->alive_start;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002432 iwl3945_disable_events(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002433 }
2434
2435 /* We delay the ALIVE response by 5ms to
2436 * give the HW RF Kill time to activate... */
2437 if (palive->is_valid == UCODE_VALID_OK)
2438 queue_delayed_work(priv->workqueue, pwork,
2439 msecs_to_jiffies(5));
2440 else
Winkler, Tomas39aadf82008-12-19 10:37:32 +08002441 IWL_WARN(priv, "uCode did not respond OK.\n");
Zhu Yib481de92007-09-25 17:54:57 -07002442}
2443
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002444static void iwl3945_rx_reply_add_sta(struct iwl_priv *priv,
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08002445 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07002446{
Helmut Schaac7e035a2009-01-19 13:02:15 +01002447#ifdef CONFIG_IWLWIFI_DEBUG
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08002448 struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
Helmut Schaac7e035a2009-01-19 13:02:15 +01002449#endif
Zhu Yib481de92007-09-25 17:54:57 -07002450
2451 IWL_DEBUG_RX("Received REPLY_ADD_STA: 0x%02X\n", pkt->u.status);
2452 return;
2453}
2454
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002455static void iwl3945_rx_reply_error(struct iwl_priv *priv,
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08002456 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07002457{
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08002458 struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07002459
Winkler, Tomas15b16872008-12-19 10:37:33 +08002460 IWL_ERR(priv, "Error Reply type 0x%08X cmd %s (0x%02X) "
Zhu Yib481de92007-09-25 17:54:57 -07002461 "seq 0x%04X ser 0x%08X\n",
2462 le32_to_cpu(pkt->u.err_resp.error_type),
2463 get_cmd_string(pkt->u.err_resp.cmd_id),
2464 pkt->u.err_resp.cmd_id,
2465 le16_to_cpu(pkt->u.err_resp.bad_cmd_seq_num),
2466 le32_to_cpu(pkt->u.err_resp.error_info));
2467}
2468
2469#define TX_STATUS_ENTRY(x) case TX_STATUS_FAIL_ ## x: return #x
2470
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002471static void iwl3945_rx_csa(struct iwl_priv *priv, struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07002472{
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08002473 struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08002474 struct iwl3945_rxon_cmd *rxon = (void *)&priv->active39_rxon;
Tomas Winkler600c0e12008-12-19 10:37:04 +08002475 struct iwl_csa_notification *csa = &(pkt->u.csa_notif);
Zhu Yib481de92007-09-25 17:54:57 -07002476 IWL_DEBUG_11H("CSA notif: channel %d, status %d\n",
2477 le16_to_cpu(csa->channel), le32_to_cpu(csa->status));
2478 rxon->channel = csa->channel;
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08002479 priv->staging39_rxon.channel = csa->channel;
Zhu Yib481de92007-09-25 17:54:57 -07002480}
2481
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002482static void iwl3945_rx_spectrum_measure_notif(struct iwl_priv *priv,
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08002483 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07002484{
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08002485#ifdef CONFIG_IWL3945_SPECTRUM_MEASUREMENT
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08002486 struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
Tomas Winkler600c0e12008-12-19 10:37:04 +08002487 struct iwl_spectrum_notification *report = &(pkt->u.spectrum_notif);
Zhu Yib481de92007-09-25 17:54:57 -07002488
2489 if (!report->state) {
2490 IWL_DEBUG(IWL_DL_11H | IWL_DL_INFO,
2491 "Spectrum Measure Notification: Start\n");
2492 return;
2493 }
2494
2495 memcpy(&priv->measure_report, report, sizeof(*report));
2496 priv->measurement_status |= MEASUREMENT_READY;
2497#endif
2498}
2499
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002500static void iwl3945_rx_pm_sleep_notif(struct iwl_priv *priv,
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08002501 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07002502{
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08002503#ifdef CONFIG_IWL3945_DEBUG
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08002504 struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
Tomas Winkler600c0e12008-12-19 10:37:04 +08002505 struct iwl_sleep_notification *sleep = &(pkt->u.sleep_notif);
Zhu Yib481de92007-09-25 17:54:57 -07002506 IWL_DEBUG_RX("sleep mode: %d, src: %d\n",
2507 sleep->pm_sleep_mode, sleep->pm_wakeup_src);
2508#endif
2509}
2510
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002511static void iwl3945_rx_pm_debug_statistics_notif(struct iwl_priv *priv,
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08002512 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07002513{
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08002514 struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07002515 IWL_DEBUG_RADIO("Dumping %d bytes of unhandled "
2516 "notification for %s:\n",
2517 le32_to_cpu(pkt->len), get_cmd_string(pkt->hdr.cmd));
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08002518 iwl_print_hex_dump(priv, IWL_DL_RADIO, pkt->u.raw,
2519 le32_to_cpu(pkt->len));
Zhu Yib481de92007-09-25 17:54:57 -07002520}
2521
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002522static void iwl3945_bg_beacon_update(struct work_struct *work)
Zhu Yib481de92007-09-25 17:54:57 -07002523{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002524 struct iwl_priv *priv =
2525 container_of(work, struct iwl_priv, beacon_update);
Zhu Yib481de92007-09-25 17:54:57 -07002526 struct sk_buff *beacon;
2527
2528 /* Pull updated AP beacon from mac80211. will fail if not in AP mode */
Johannes Berge039fa42008-05-15 12:55:29 +02002529 beacon = ieee80211_beacon_get(priv->hw, priv->vif);
Zhu Yib481de92007-09-25 17:54:57 -07002530
2531 if (!beacon) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08002532 IWL_ERR(priv, "update beacon failed\n");
Zhu Yib481de92007-09-25 17:54:57 -07002533 return;
2534 }
2535
2536 mutex_lock(&priv->mutex);
2537 /* new beacon skb is allocated every time; dispose previous.*/
2538 if (priv->ibss_beacon)
2539 dev_kfree_skb(priv->ibss_beacon);
2540
2541 priv->ibss_beacon = beacon;
2542 mutex_unlock(&priv->mutex);
2543
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002544 iwl3945_send_beacon_cmd(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002545}
2546
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002547static void iwl3945_rx_beacon_notif(struct iwl_priv *priv,
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08002548 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07002549{
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08002550#ifdef CONFIG_IWL3945_DEBUG
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08002551 struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002552 struct iwl3945_beacon_notif *beacon = &(pkt->u.beacon_status);
Zhu Yib481de92007-09-25 17:54:57 -07002553 u8 rate = beacon->beacon_notify_hdr.rate;
2554
2555 IWL_DEBUG_RX("beacon status %x retries %d iss %d "
2556 "tsf %d %d rate %d\n",
2557 le32_to_cpu(beacon->beacon_notify_hdr.status) & TX_STATUS_MSK,
2558 beacon->beacon_notify_hdr.failure_frame,
2559 le32_to_cpu(beacon->ibss_mgr_status),
2560 le32_to_cpu(beacon->high_tsf),
2561 le32_to_cpu(beacon->low_tsf), rate);
2562#endif
2563
Johannes Berg05c914f2008-09-11 00:01:58 +02002564 if ((priv->iw_mode == NL80211_IFTYPE_AP) &&
Zhu Yib481de92007-09-25 17:54:57 -07002565 (!test_bit(STATUS_EXIT_PENDING, &priv->status)))
2566 queue_work(priv->workqueue, &priv->beacon_update);
2567}
2568
2569/* Service response to REPLY_SCAN_CMD (0x80) */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002570static void iwl3945_rx_reply_scan(struct iwl_priv *priv,
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08002571 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07002572{
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08002573#ifdef CONFIG_IWL3945_DEBUG
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08002574 struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
Tomas Winkler4c897252008-12-19 10:37:05 +08002575 struct iwl_scanreq_notification *notif =
2576 (struct iwl_scanreq_notification *)pkt->u.raw;
Zhu Yib481de92007-09-25 17:54:57 -07002577
2578 IWL_DEBUG_RX("Scan request status = 0x%x\n", notif->status);
2579#endif
2580}
2581
2582/* Service SCAN_START_NOTIFICATION (0x82) */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002583static void iwl3945_rx_scan_start_notif(struct iwl_priv *priv,
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08002584 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07002585{
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08002586 struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
Tomas Winkler4c897252008-12-19 10:37:05 +08002587 struct iwl_scanstart_notification *notif =
2588 (struct iwl_scanstart_notification *)pkt->u.raw;
Zhu Yib481de92007-09-25 17:54:57 -07002589 priv->scan_start_tsf = le32_to_cpu(notif->tsf_low);
2590 IWL_DEBUG_SCAN("Scan start: "
2591 "%d [802.11%s] "
2592 "(TSF: 0x%08X:%08X) - %d (beacon timer %u)\n",
2593 notif->channel,
2594 notif->band ? "bg" : "a",
2595 notif->tsf_high,
2596 notif->tsf_low, notif->status, notif->beacon_timer);
2597}
2598
2599/* Service SCAN_RESULTS_NOTIFICATION (0x83) */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002600static void iwl3945_rx_scan_results_notif(struct iwl_priv *priv,
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08002601 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07002602{
Helmut Schaac7e035a2009-01-19 13:02:15 +01002603#ifdef CONFIG_IWLWIFI_DEBUG
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08002604 struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
Tomas Winkler4c897252008-12-19 10:37:05 +08002605 struct iwl_scanresults_notification *notif =
2606 (struct iwl_scanresults_notification *)pkt->u.raw;
Helmut Schaac7e035a2009-01-19 13:02:15 +01002607#endif
Zhu Yib481de92007-09-25 17:54:57 -07002608
2609 IWL_DEBUG_SCAN("Scan ch.res: "
2610 "%d [802.11%s] "
2611 "(TSF: 0x%08X:%08X) - %d "
2612 "elapsed=%lu usec (%dms since last)\n",
2613 notif->channel,
2614 notif->band ? "bg" : "a",
2615 le32_to_cpu(notif->tsf_high),
2616 le32_to_cpu(notif->tsf_low),
2617 le32_to_cpu(notif->statistics[0]),
2618 le32_to_cpu(notif->tsf_low) - priv->scan_start_tsf,
2619 jiffies_to_msecs(elapsed_jiffies
2620 (priv->last_scan_jiffies, jiffies)));
2621
2622 priv->last_scan_jiffies = jiffies;
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08002623 priv->next_scan_jiffies = 0;
Zhu Yib481de92007-09-25 17:54:57 -07002624}
2625
2626/* Service SCAN_COMPLETE_NOTIFICATION (0x84) */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002627static void iwl3945_rx_scan_complete_notif(struct iwl_priv *priv,
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08002628 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07002629{
Helmut Schaac7e035a2009-01-19 13:02:15 +01002630#ifdef CONFIG_IWLWIFI_DEBUG
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08002631 struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
Tomas Winkler4c897252008-12-19 10:37:05 +08002632 struct iwl_scancomplete_notification *scan_notif = (void *)pkt->u.raw;
Helmut Schaac7e035a2009-01-19 13:02:15 +01002633#endif
Zhu Yib481de92007-09-25 17:54:57 -07002634
2635 IWL_DEBUG_SCAN("Scan complete: %d channels (TSF 0x%08X:%08X) - %d\n",
2636 scan_notif->scanned_channels,
2637 scan_notif->tsf_low,
2638 scan_notif->tsf_high, scan_notif->status);
2639
2640 /* The HW is no longer scanning */
2641 clear_bit(STATUS_SCAN_HW, &priv->status);
2642
2643 /* The scan completion notification came in, so kill that timer... */
2644 cancel_delayed_work(&priv->scan_check);
2645
2646 IWL_DEBUG_INFO("Scan pass on %sGHz took %dms\n",
Ron Rindjunsky66b50042008-06-25 16:46:31 +08002647 (priv->scan_bands & BIT(IEEE80211_BAND_2GHZ)) ?
2648 "2.4" : "5.2",
Zhu Yib481de92007-09-25 17:54:57 -07002649 jiffies_to_msecs(elapsed_jiffies
2650 (priv->scan_pass_start, jiffies)));
2651
Ron Rindjunsky66b50042008-06-25 16:46:31 +08002652 /* Remove this scanned band from the list of pending
2653 * bands to scan, band G precedes A in order of scanning
2654 * as seen in iwl3945_bg_request_scan */
2655 if (priv->scan_bands & BIT(IEEE80211_BAND_2GHZ))
2656 priv->scan_bands &= ~BIT(IEEE80211_BAND_2GHZ);
2657 else if (priv->scan_bands & BIT(IEEE80211_BAND_5GHZ))
2658 priv->scan_bands &= ~BIT(IEEE80211_BAND_5GHZ);
Zhu Yib481de92007-09-25 17:54:57 -07002659
2660 /* If a request to abort was given, or the scan did not succeed
2661 * then we reset the scan state machine and terminate,
2662 * re-queuing another scan if one has been requested */
2663 if (test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
2664 IWL_DEBUG_INFO("Aborted scan completed.\n");
2665 clear_bit(STATUS_SCAN_ABORTING, &priv->status);
2666 } else {
2667 /* If there are more bands on this scan pass reschedule */
2668 if (priv->scan_bands > 0)
2669 goto reschedule;
2670 }
2671
2672 priv->last_scan_jiffies = jiffies;
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08002673 priv->next_scan_jiffies = 0;
Zhu Yib481de92007-09-25 17:54:57 -07002674 IWL_DEBUG_INFO("Setting scan to off\n");
2675
2676 clear_bit(STATUS_SCANNING, &priv->status);
2677
2678 IWL_DEBUG_INFO("Scan took %dms\n",
2679 jiffies_to_msecs(elapsed_jiffies(priv->scan_start, jiffies)));
2680
2681 queue_work(priv->workqueue, &priv->scan_completed);
2682
2683 return;
2684
2685reschedule:
2686 priv->scan_pass_start = jiffies;
2687 queue_work(priv->workqueue, &priv->request_scan);
2688}
2689
2690/* Handle notification from uCode that card's power state is changing
2691 * due to software, hardware, or critical temperature RFKILL */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002692static void iwl3945_rx_card_state_notif(struct iwl_priv *priv,
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08002693 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07002694{
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08002695 struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07002696 u32 flags = le32_to_cpu(pkt->u.card_state_notif.flags);
2697 unsigned long status = priv->status;
2698
2699 IWL_DEBUG_RF_KILL("Card state received: HW:%s SW:%s\n",
2700 (flags & HW_CARD_DISABLED) ? "Kill" : "On",
2701 (flags & SW_CARD_DISABLED) ? "Kill" : "On");
2702
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08002703 iwl_write32(priv, CSR_UCODE_DRV_GP1_SET,
Zhu Yib481de92007-09-25 17:54:57 -07002704 CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
2705
2706 if (flags & HW_CARD_DISABLED)
2707 set_bit(STATUS_RF_KILL_HW, &priv->status);
2708 else
2709 clear_bit(STATUS_RF_KILL_HW, &priv->status);
2710
2711
2712 if (flags & SW_CARD_DISABLED)
2713 set_bit(STATUS_RF_KILL_SW, &priv->status);
2714 else
2715 clear_bit(STATUS_RF_KILL_SW, &priv->status);
2716
Winkler, Tomasaf0053d2009-01-19 15:30:23 -08002717 iwl_scan_cancel(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002718
2719 if ((test_bit(STATUS_RF_KILL_HW, &status) !=
2720 test_bit(STATUS_RF_KILL_HW, &priv->status)) ||
2721 (test_bit(STATUS_RF_KILL_SW, &status) !=
2722 test_bit(STATUS_RF_KILL_SW, &priv->status)))
2723 queue_work(priv->workqueue, &priv->rf_kill);
2724 else
2725 wake_up_interruptible(&priv->wait_command_queue);
2726}
2727
2728/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002729 * iwl3945_setup_rx_handlers - Initialize Rx handler callbacks
Zhu Yib481de92007-09-25 17:54:57 -07002730 *
2731 * Setup the RX handlers for each of the reply types sent from the uCode
2732 * to the host.
2733 *
2734 * This function chains into the hardware specific files for them to setup
2735 * any hardware specific handlers as well.
2736 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002737static void iwl3945_setup_rx_handlers(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002738{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002739 priv->rx_handlers[REPLY_ALIVE] = iwl3945_rx_reply_alive;
2740 priv->rx_handlers[REPLY_ADD_STA] = iwl3945_rx_reply_add_sta;
2741 priv->rx_handlers[REPLY_ERROR] = iwl3945_rx_reply_error;
2742 priv->rx_handlers[CHANNEL_SWITCH_NOTIFICATION] = iwl3945_rx_csa;
Zhu Yib481de92007-09-25 17:54:57 -07002743 priv->rx_handlers[SPECTRUM_MEASURE_NOTIFICATION] =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002744 iwl3945_rx_spectrum_measure_notif;
2745 priv->rx_handlers[PM_SLEEP_NOTIFICATION] = iwl3945_rx_pm_sleep_notif;
Zhu Yib481de92007-09-25 17:54:57 -07002746 priv->rx_handlers[PM_DEBUG_STATISTIC_NOTIFIC] =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002747 iwl3945_rx_pm_debug_statistics_notif;
2748 priv->rx_handlers[BEACON_NOTIFICATION] = iwl3945_rx_beacon_notif;
Zhu Yib481de92007-09-25 17:54:57 -07002749
Ben Cahill9fbab512007-11-29 11:09:47 +08002750 /*
2751 * The same handler is used for both the REPLY to a discrete
2752 * statistics request from the host as well as for the periodic
2753 * statistics notifications (after received beacons) from the uCode.
Zhu Yib481de92007-09-25 17:54:57 -07002754 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002755 priv->rx_handlers[REPLY_STATISTICS_CMD] = iwl3945_hw_rx_statistics;
2756 priv->rx_handlers[STATISTICS_NOTIFICATION] = iwl3945_hw_rx_statistics;
Zhu Yib481de92007-09-25 17:54:57 -07002757
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002758 priv->rx_handlers[REPLY_SCAN_CMD] = iwl3945_rx_reply_scan;
2759 priv->rx_handlers[SCAN_START_NOTIFICATION] = iwl3945_rx_scan_start_notif;
Zhu Yib481de92007-09-25 17:54:57 -07002760 priv->rx_handlers[SCAN_RESULTS_NOTIFICATION] =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002761 iwl3945_rx_scan_results_notif;
Zhu Yib481de92007-09-25 17:54:57 -07002762 priv->rx_handlers[SCAN_COMPLETE_NOTIFICATION] =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002763 iwl3945_rx_scan_complete_notif;
2764 priv->rx_handlers[CARD_STATE_NOTIFICATION] = iwl3945_rx_card_state_notif;
Zhu Yib481de92007-09-25 17:54:57 -07002765
Ben Cahill9fbab512007-11-29 11:09:47 +08002766 /* Set up hardware specific Rx handlers */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002767 iwl3945_hw_rx_handler_setup(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002768}
2769
2770/**
Tomas Winkler91c066f2008-03-06 17:36:55 -08002771 * iwl3945_cmd_queue_reclaim - Reclaim CMD queue entries
2772 * When FW advances 'R' index, all entries between old and new 'R' index
2773 * need to be reclaimed.
2774 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002775static void iwl3945_cmd_queue_reclaim(struct iwl_priv *priv,
Tomas Winkler91c066f2008-03-06 17:36:55 -08002776 int txq_id, int index)
2777{
Samuel Ortiz188cf6c2008-12-22 11:31:16 +08002778 struct iwl_tx_queue *txq = &priv->txq[txq_id];
Samuel Ortizd20b3c62008-12-19 10:37:15 +08002779 struct iwl_queue *q = &txq->q;
Tomas Winkler91c066f2008-03-06 17:36:55 -08002780 int nfreed = 0;
2781
Winkler, Tomas625a3812009-01-08 10:19:55 -08002782 if ((index >= q->n_bd) || (iwl_queue_used(q, index) == 0)) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08002783 IWL_ERR(priv, "Read index for DMA queue txq id (%d), index %d, "
Tomas Winkler91c066f2008-03-06 17:36:55 -08002784 "is out of range [0-%d] %d %d.\n", txq_id,
2785 index, q->n_bd, q->write_ptr, q->read_ptr);
2786 return;
2787 }
2788
2789 for (index = iwl_queue_inc_wrap(index, q->n_bd); q->read_ptr != index;
2790 q->read_ptr = iwl_queue_inc_wrap(q->read_ptr, q->n_bd)) {
2791 if (nfreed > 1) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08002792 IWL_ERR(priv, "HCMD skipped: index (%d) %d %d\n", index,
Tomas Winkler91c066f2008-03-06 17:36:55 -08002793 q->write_ptr, q->read_ptr);
2794 queue_work(priv->workqueue, &priv->restart);
2795 break;
2796 }
2797 nfreed++;
2798 }
2799}
2800
2801
2802/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002803 * iwl3945_tx_cmd_complete - Pull unused buffers off the queue and reclaim them
Zhu Yib481de92007-09-25 17:54:57 -07002804 * @rxb: Rx buffer to reclaim
2805 *
2806 * If an Rx buffer has an async callback associated with it the callback
2807 * will be executed. The attached skb (if present) will only be freed
2808 * if the callback returns 1
2809 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002810static void iwl3945_tx_cmd_complete(struct iwl_priv *priv,
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08002811 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07002812{
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08002813 struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07002814 u16 sequence = le16_to_cpu(pkt->hdr.sequence);
2815 int txq_id = SEQ_TO_QUEUE(sequence);
2816 int index = SEQ_TO_INDEX(sequence);
Tomas Winkler600c0e12008-12-19 10:37:04 +08002817 int huge = !!(pkt->hdr.sequence & SEQ_HUGE_FRAME);
Zhu Yib481de92007-09-25 17:54:57 -07002818 int cmd_index;
Winkler, Tomasc2d79b42008-12-19 10:37:34 +08002819 struct iwl_cmd *cmd;
Zhu Yib481de92007-09-25 17:54:57 -07002820
Chatre, Reinette638d0eb2009-01-19 15:30:24 -08002821 if (WARN(txq_id != IWL_CMD_QUEUE_NUM,
2822 "wrong command queue %d, sequence 0x%X readp=%d writep=%d\n",
2823 txq_id, sequence,
2824 priv->txq[IWL_CMD_QUEUE_NUM].q.read_ptr,
2825 priv->txq[IWL_CMD_QUEUE_NUM].q.write_ptr)) {
2826 iwl_print_hex_dump(priv, IWL_DL_INFO , rxb, 32);
2827 return;
2828 }
Zhu Yib481de92007-09-25 17:54:57 -07002829
Samuel Ortiz188cf6c2008-12-22 11:31:16 +08002830 cmd_index = get_cmd_index(&priv->txq[IWL_CMD_QUEUE_NUM].q, index, huge);
2831 cmd = priv->txq[IWL_CMD_QUEUE_NUM].cmd[cmd_index];
Zhu Yib481de92007-09-25 17:54:57 -07002832
2833 /* Input error checking is done when commands are added to queue. */
2834 if (cmd->meta.flags & CMD_WANT_SKB) {
2835 cmd->meta.source->u.skb = rxb->skb;
2836 rxb->skb = NULL;
2837 } else if (cmd->meta.u.callback &&
2838 !cmd->meta.u.callback(priv, cmd, rxb->skb))
2839 rxb->skb = NULL;
2840
Tomas Winkler91c066f2008-03-06 17:36:55 -08002841 iwl3945_cmd_queue_reclaim(priv, txq_id, index);
Zhu Yib481de92007-09-25 17:54:57 -07002842
2843 if (!(cmd->meta.flags & CMD_ASYNC)) {
2844 clear_bit(STATUS_HCMD_ACTIVE, &priv->status);
2845 wake_up_interruptible(&priv->wait_command_queue);
2846 }
2847}
2848
2849/************************** RX-FUNCTIONS ****************************/
2850/*
2851 * Rx theory of operation
2852 *
2853 * The host allocates 32 DMA target addresses and passes the host address
2854 * to the firmware at register IWL_RFDS_TABLE_LOWER + N * RFD_SIZE where N is
2855 * 0 to 31
2856 *
2857 * Rx Queue Indexes
2858 * The host/firmware share two index registers for managing the Rx buffers.
2859 *
2860 * The READ index maps to the first position that the firmware may be writing
2861 * to -- the driver can read up to (but not including) this position and get
2862 * good data.
2863 * The READ index is managed by the firmware once the card is enabled.
2864 *
2865 * The WRITE index maps to the last position the driver has read from -- the
2866 * position preceding WRITE is the last slot the firmware can place a packet.
2867 *
2868 * The queue is empty (no good data) if WRITE = READ - 1, and is full if
2869 * WRITE = READ.
2870 *
Ben Cahill9fbab512007-11-29 11:09:47 +08002871 * During initialization, the host sets up the READ queue position to the first
Zhu Yib481de92007-09-25 17:54:57 -07002872 * INDEX position, and WRITE to the last (READ - 1 wrapped)
2873 *
Ben Cahill9fbab512007-11-29 11:09:47 +08002874 * When the firmware places a packet in a buffer, it will advance the READ index
Zhu Yib481de92007-09-25 17:54:57 -07002875 * and fire the RX interrupt. The driver can then query the READ index and
2876 * process as many packets as possible, moving the WRITE index forward as it
2877 * resets the Rx queue buffers with new memory.
2878 *
2879 * The management in the driver is as follows:
2880 * + A list of pre-allocated SKBs is stored in iwl->rxq->rx_free. When
2881 * iwl->rxq->free_count drops to or below RX_LOW_WATERMARK, work is scheduled
Ian Schram01ebd062007-10-25 17:15:22 +08002882 * to replenish the iwl->rxq->rx_free.
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002883 * + In iwl3945_rx_replenish (scheduled) if 'processed' != 'read' then the
Zhu Yib481de92007-09-25 17:54:57 -07002884 * iwl->rxq is replenished and the READ INDEX is updated (updating the
2885 * 'processed' and 'read' driver indexes as well)
2886 * + A received packet is processed and handed to the kernel network stack,
2887 * detached from the iwl->rxq. The driver 'processed' index is updated.
2888 * + The Host/Firmware iwl->rxq is replenished at tasklet time from the rx_free
2889 * list. If there are no allocated buffers in iwl->rxq->rx_free, the READ
2890 * INDEX is not incremented and iwl->status(RX_STALLED) is set. If there
2891 * were enough free buffers and RX_STALLED is set it is cleared.
2892 *
2893 *
2894 * Driver sequence:
2895 *
Ben Cahill9fbab512007-11-29 11:09:47 +08002896 * iwl3945_rx_replenish() Replenishes rx_free list from rx_used, and calls
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002897 * iwl3945_rx_queue_restock
Ben Cahill9fbab512007-11-29 11:09:47 +08002898 * iwl3945_rx_queue_restock() Moves available buffers from rx_free into Rx
Zhu Yib481de92007-09-25 17:54:57 -07002899 * queue, updates firmware pointers, and updates
2900 * the WRITE index. If insufficient rx_free buffers
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002901 * are available, schedules iwl3945_rx_replenish
Zhu Yib481de92007-09-25 17:54:57 -07002902 *
2903 * -- enable interrupts --
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08002904 * ISR - iwl3945_rx() Detach iwl_rx_mem_buffers from pool up to the
Zhu Yib481de92007-09-25 17:54:57 -07002905 * READ INDEX, detaching the SKB from the pool.
2906 * Moves the packet buffer from queue to rx_used.
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002907 * Calls iwl3945_rx_queue_restock to refill any empty
Zhu Yib481de92007-09-25 17:54:57 -07002908 * slots.
2909 * ...
2910 *
2911 */
2912
2913/**
Ben Cahill9fbab512007-11-29 11:09:47 +08002914 * iwl3945_dma_addr2rbd_ptr - convert a DMA address to a uCode read buffer ptr
Zhu Yib481de92007-09-25 17:54:57 -07002915 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002916static inline __le32 iwl3945_dma_addr2rbd_ptr(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07002917 dma_addr_t dma_addr)
2918{
2919 return cpu_to_le32((u32)dma_addr);
2920}
2921
2922/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002923 * iwl3945_rx_queue_restock - refill RX queue from pre-allocated pool
Zhu Yib481de92007-09-25 17:54:57 -07002924 *
Ben Cahill9fbab512007-11-29 11:09:47 +08002925 * If there are slots in the RX queue that need to be restocked,
Zhu Yib481de92007-09-25 17:54:57 -07002926 * and we have free pre-allocated buffers, fill the ranks as much
Ben Cahill9fbab512007-11-29 11:09:47 +08002927 * as we can, pulling from rx_free.
Zhu Yib481de92007-09-25 17:54:57 -07002928 *
2929 * This moves the 'write' index forward to catch up with 'processed', and
2930 * also updates the memory address in the firmware to reference the new
2931 * target buffer.
2932 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002933static int iwl3945_rx_queue_restock(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002934{
Abhijeet Kolekarcc2f3622008-12-19 10:37:25 +08002935 struct iwl_rx_queue *rxq = &priv->rxq;
Zhu Yib481de92007-09-25 17:54:57 -07002936 struct list_head *element;
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08002937 struct iwl_rx_mem_buffer *rxb;
Zhu Yib481de92007-09-25 17:54:57 -07002938 unsigned long flags;
2939 int write, rc;
2940
2941 spin_lock_irqsave(&rxq->lock, flags);
2942 write = rxq->write & ~0x7;
Winkler, Tomas37d68312009-01-08 10:19:54 -08002943 while ((iwl_rx_queue_space(rxq) > 0) && (rxq->free_count)) {
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002944 /* Get next free Rx buffer, remove from free list */
Zhu Yib481de92007-09-25 17:54:57 -07002945 element = rxq->rx_free.next;
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08002946 rxb = list_entry(element, struct iwl_rx_mem_buffer, list);
Zhu Yib481de92007-09-25 17:54:57 -07002947 list_del(element);
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002948
2949 /* Point to Rx buffer via next RBD in circular buffer */
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08002950 rxq->bd[rxq->write] = iwl3945_dma_addr2rbd_ptr(priv, rxb->real_dma_addr);
Zhu Yib481de92007-09-25 17:54:57 -07002951 rxq->queue[rxq->write] = rxb;
2952 rxq->write = (rxq->write + 1) & RX_QUEUE_MASK;
2953 rxq->free_count--;
2954 }
2955 spin_unlock_irqrestore(&rxq->lock, flags);
2956 /* If the pre-allocated buffer pool is dropping low, schedule to
2957 * refill it */
2958 if (rxq->free_count <= RX_LOW_WATERMARK)
2959 queue_work(priv->workqueue, &priv->rx_replenish);
2960
2961
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002962 /* If we've added more space for the firmware to place data, tell it.
2963 * Increment device's write pointer in multiples of 8. */
Zhu Yib481de92007-09-25 17:54:57 -07002964 if ((write != (rxq->write & ~0x7))
2965 || (abs(rxq->write - rxq->read) > 7)) {
2966 spin_lock_irqsave(&rxq->lock, flags);
2967 rxq->need_update = 1;
2968 spin_unlock_irqrestore(&rxq->lock, flags);
Winkler, Tomas141c43a2009-01-08 10:19:53 -08002969 rc = iwl_rx_queue_update_write_ptr(priv, rxq);
Zhu Yib481de92007-09-25 17:54:57 -07002970 if (rc)
2971 return rc;
2972 }
2973
2974 return 0;
2975}
2976
2977/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002978 * iwl3945_rx_replenish - Move all used packet from rx_used to rx_free
Zhu Yib481de92007-09-25 17:54:57 -07002979 *
2980 * When moving to rx_free an SKB is allocated for the slot.
2981 *
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002982 * Also restock the Rx queue via iwl3945_rx_queue_restock.
Ian Schram01ebd062007-10-25 17:15:22 +08002983 * This is called as a scheduled work item (except for during initialization)
Zhu Yib481de92007-09-25 17:54:57 -07002984 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002985static void iwl3945_rx_allocate(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002986{
Abhijeet Kolekarcc2f3622008-12-19 10:37:25 +08002987 struct iwl_rx_queue *rxq = &priv->rxq;
Zhu Yib481de92007-09-25 17:54:57 -07002988 struct list_head *element;
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08002989 struct iwl_rx_mem_buffer *rxb;
Zhu Yib481de92007-09-25 17:54:57 -07002990 unsigned long flags;
2991 spin_lock_irqsave(&rxq->lock, flags);
2992 while (!list_empty(&rxq->rx_used)) {
2993 element = rxq->rx_used.next;
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08002994 rxb = list_entry(element, struct iwl_rx_mem_buffer, list);
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002995
2996 /* Alloc a new receive buffer */
Zhu Yib481de92007-09-25 17:54:57 -07002997 rxb->skb =
Winkler, Tomas1e33dc62009-01-08 10:19:57 -08002998 alloc_skb(priv->hw_params.rx_buf_size,
2999 __GFP_NOWARN | GFP_ATOMIC);
Zhu Yib481de92007-09-25 17:54:57 -07003000 if (!rxb->skb) {
3001 if (net_ratelimit())
Tomas Winkler978785a2008-12-19 10:37:31 +08003002 IWL_CRIT(priv, ": Can not allocate SKB buffers\n");
Zhu Yib481de92007-09-25 17:54:57 -07003003 /* We don't reschedule replenish work here -- we will
3004 * call the restock method and if it still needs
3005 * more buffers it will schedule replenish */
3006 break;
3007 }
Zhu Yi12342c42007-12-20 11:27:32 +08003008
3009 /* If radiotap head is required, reserve some headroom here.
3010 * The physical head count is a variable rx_stats->phy_count.
3011 * We reserve 4 bytes here. Plus these extra bytes, the
3012 * headroom of the physical head should be enough for the
3013 * radiotap head that iwl3945 supported. See iwl3945_rt.
3014 */
3015 skb_reserve(rxb->skb, 4);
3016
Zhu Yib481de92007-09-25 17:54:57 -07003017 priv->alloc_rxb_skb++;
3018 list_del(element);
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003019
3020 /* Get physical address of RB/SKB */
Winkler, Tomas1e33dc62009-01-08 10:19:57 -08003021 rxb->real_dma_addr = pci_map_single(priv->pci_dev,
3022 rxb->skb->data,
3023 priv->hw_params.rx_buf_size,
3024 PCI_DMA_FROMDEVICE);
Zhu Yib481de92007-09-25 17:54:57 -07003025 list_add_tail(&rxb->list, &rxq->rx_free);
3026 rxq->free_count++;
3027 }
3028 spin_unlock_irqrestore(&rxq->lock, flags);
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08003029}
3030
3031/*
3032 * this should be called while priv->lock is locked
3033 */
Tomas Winkler4fd1f842007-12-05 20:59:58 +02003034static void __iwl3945_rx_replenish(void *data)
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08003035{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003036 struct iwl_priv *priv = data;
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08003037
3038 iwl3945_rx_allocate(priv);
3039 iwl3945_rx_queue_restock(priv);
3040}
3041
3042
3043void iwl3945_rx_replenish(void *data)
3044{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003045 struct iwl_priv *priv = data;
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08003046 unsigned long flags;
3047
3048 iwl3945_rx_allocate(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003049
3050 spin_lock_irqsave(&priv->lock, flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003051 iwl3945_rx_queue_restock(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003052 spin_unlock_irqrestore(&priv->lock, flags);
3053}
3054
Zhu Yib481de92007-09-25 17:54:57 -07003055/* Convert linear signal-to-noise ratio into dB */
3056static u8 ratio2dB[100] = {
3057/* 0 1 2 3 4 5 6 7 8 9 */
3058 0, 0, 6, 10, 12, 14, 16, 17, 18, 19, /* 00 - 09 */
3059 20, 21, 22, 22, 23, 23, 24, 25, 26, 26, /* 10 - 19 */
3060 26, 26, 26, 27, 27, 28, 28, 28, 29, 29, /* 20 - 29 */
3061 29, 30, 30, 30, 31, 31, 31, 31, 32, 32, /* 30 - 39 */
3062 32, 32, 32, 33, 33, 33, 33, 33, 34, 34, /* 40 - 49 */
3063 34, 34, 34, 34, 35, 35, 35, 35, 35, 35, /* 50 - 59 */
3064 36, 36, 36, 36, 36, 36, 36, 37, 37, 37, /* 60 - 69 */
3065 37, 37, 37, 37, 37, 38, 38, 38, 38, 38, /* 70 - 79 */
3066 38, 38, 38, 38, 38, 39, 39, 39, 39, 39, /* 80 - 89 */
3067 39, 39, 39, 39, 39, 40, 40, 40, 40, 40 /* 90 - 99 */
3068};
3069
3070/* Calculates a relative dB value from a ratio of linear
3071 * (i.e. not dB) signal levels.
3072 * Conversion assumes that levels are voltages (20*log), not powers (10*log). */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003073int iwl3945_calc_db_from_ratio(int sig_ratio)
Zhu Yib481de92007-09-25 17:54:57 -07003074{
Adrian Bunk221c80c2008-02-02 23:19:01 +02003075 /* 1000:1 or higher just report as 60 dB */
3076 if (sig_ratio >= 1000)
Zhu Yib481de92007-09-25 17:54:57 -07003077 return 60;
3078
Adrian Bunk221c80c2008-02-02 23:19:01 +02003079 /* 100:1 or higher, divide by 10 and use table,
Zhu Yib481de92007-09-25 17:54:57 -07003080 * add 20 dB to make up for divide by 10 */
Adrian Bunk221c80c2008-02-02 23:19:01 +02003081 if (sig_ratio >= 100)
Tomas Winkler3ac7f142008-07-21 02:40:14 +03003082 return 20 + (int)ratio2dB[sig_ratio/10];
Zhu Yib481de92007-09-25 17:54:57 -07003083
3084 /* We shouldn't see this */
3085 if (sig_ratio < 1)
3086 return 0;
3087
3088 /* Use table for ratios 1:1 - 99:1 */
3089 return (int)ratio2dB[sig_ratio];
3090}
3091
3092#define PERFECT_RSSI (-20) /* dBm */
3093#define WORST_RSSI (-95) /* dBm */
3094#define RSSI_RANGE (PERFECT_RSSI - WORST_RSSI)
3095
3096/* Calculate an indication of rx signal quality (a percentage, not dBm!).
3097 * See http://www.ces.clemson.edu/linux/signal_quality.shtml for info
3098 * about formulas used below. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003099int iwl3945_calc_sig_qual(int rssi_dbm, int noise_dbm)
Zhu Yib481de92007-09-25 17:54:57 -07003100{
3101 int sig_qual;
3102 int degradation = PERFECT_RSSI - rssi_dbm;
3103
3104 /* If we get a noise measurement, use signal-to-noise ratio (SNR)
3105 * as indicator; formula is (signal dbm - noise dbm).
3106 * SNR at or above 40 is a great signal (100%).
3107 * Below that, scale to fit SNR of 0 - 40 dB within 0 - 100% indicator.
3108 * Weakest usable signal is usually 10 - 15 dB SNR. */
3109 if (noise_dbm) {
3110 if (rssi_dbm - noise_dbm >= 40)
3111 return 100;
3112 else if (rssi_dbm < noise_dbm)
3113 return 0;
3114 sig_qual = ((rssi_dbm - noise_dbm) * 5) / 2;
3115
3116 /* Else use just the signal level.
3117 * This formula is a least squares fit of data points collected and
3118 * compared with a reference system that had a percentage (%) display
3119 * for signal quality. */
3120 } else
3121 sig_qual = (100 * (RSSI_RANGE * RSSI_RANGE) - degradation *
3122 (15 * RSSI_RANGE + 62 * degradation)) /
3123 (RSSI_RANGE * RSSI_RANGE);
3124
3125 if (sig_qual > 100)
3126 sig_qual = 100;
3127 else if (sig_qual < 1)
3128 sig_qual = 0;
3129
3130 return sig_qual;
3131}
3132
3133/**
Ben Cahill9fbab512007-11-29 11:09:47 +08003134 * iwl3945_rx_handle - Main entry function for receiving responses from uCode
Zhu Yib481de92007-09-25 17:54:57 -07003135 *
3136 * Uses the priv->rx_handlers callback function array to invoke
3137 * the appropriate handlers, including command responses,
3138 * frame-received notifications, and other notifications.
3139 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003140static void iwl3945_rx_handle(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003141{
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08003142 struct iwl_rx_mem_buffer *rxb;
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08003143 struct iwl_rx_packet *pkt;
Abhijeet Kolekarcc2f3622008-12-19 10:37:25 +08003144 struct iwl_rx_queue *rxq = &priv->rxq;
Zhu Yib481de92007-09-25 17:54:57 -07003145 u32 r, i;
3146 int reclaim;
3147 unsigned long flags;
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08003148 u8 fill_rx = 0;
Mohamed Abbasd68ab682008-02-07 13:16:33 -08003149 u32 count = 8;
Zhu Yib481de92007-09-25 17:54:57 -07003150
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003151 /* uCode's read index (stored in shared DRAM) indicates the last Rx
3152 * buffer that the driver may process (last buffer filled by ucode). */
Winkler, Tomas8cd812b2008-12-19 10:37:43 +08003153 r = le16_to_cpu(rxq->rb_stts->closed_rb_num) & 0x0FFF;
Zhu Yib481de92007-09-25 17:54:57 -07003154 i = rxq->read;
3155
Winkler, Tomas37d68312009-01-08 10:19:54 -08003156 if (iwl_rx_queue_space(rxq) > (RX_QUEUE_SIZE / 2))
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08003157 fill_rx = 1;
Zhu Yib481de92007-09-25 17:54:57 -07003158 /* Rx interrupt, but nothing sent from uCode */
3159 if (i == r)
3160 IWL_DEBUG(IWL_DL_RX | IWL_DL_ISR, "r = %d, i = %d\n", r, i);
3161
3162 while (i != r) {
3163 rxb = rxq->queue[i];
3164
Ben Cahill9fbab512007-11-29 11:09:47 +08003165 /* If an RXB doesn't have a Rx queue slot associated with it,
Zhu Yib481de92007-09-25 17:54:57 -07003166 * then a bug has been introduced in the queue refilling
3167 * routines -- catch it here */
3168 BUG_ON(rxb == NULL);
3169
3170 rxq->queue[i] = NULL;
3171
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08003172 pci_dma_sync_single_for_cpu(priv->pci_dev, rxb->real_dma_addr,
Winkler, Tomas1e33dc62009-01-08 10:19:57 -08003173 priv->hw_params.rx_buf_size,
Zhu Yib481de92007-09-25 17:54:57 -07003174 PCI_DMA_FROMDEVICE);
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08003175 pkt = (struct iwl_rx_packet *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07003176
3177 /* Reclaim a command buffer only if this packet is a response
3178 * to a (driver-originated) command.
3179 * If the packet (e.g. Rx frame) originated from uCode,
3180 * there is no command buffer to reclaim.
3181 * Ucode should set SEQ_RX_FRAME bit if ucode-originated,
3182 * but apparently a few don't get set; catch them here. */
3183 reclaim = !(pkt->hdr.sequence & SEQ_RX_FRAME) &&
3184 (pkt->hdr.cmd != STATISTICS_NOTIFICATION) &&
3185 (pkt->hdr.cmd != REPLY_TX);
3186
3187 /* Based on type of command response or notification,
3188 * handle those that need handling via function in
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003189 * rx_handlers table. See iwl3945_setup_rx_handlers() */
Zhu Yib481de92007-09-25 17:54:57 -07003190 if (priv->rx_handlers[pkt->hdr.cmd]) {
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08003191 IWL_DEBUG(IWL_DL_HCMD | IWL_DL_RX | IWL_DL_ISR,
Zhu Yib481de92007-09-25 17:54:57 -07003192 "r = %d, i = %d, %s, 0x%02x\n", r, i,
3193 get_cmd_string(pkt->hdr.cmd), pkt->hdr.cmd);
3194 priv->rx_handlers[pkt->hdr.cmd] (priv, rxb);
3195 } else {
3196 /* No handling needed */
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08003197 IWL_DEBUG(IWL_DL_HCMD | IWL_DL_RX | IWL_DL_ISR,
Zhu Yib481de92007-09-25 17:54:57 -07003198 "r %d i %d No handler needed for %s, 0x%02x\n",
3199 r, i, get_cmd_string(pkt->hdr.cmd),
3200 pkt->hdr.cmd);
3201 }
3202
3203 if (reclaim) {
Ben Cahill9fbab512007-11-29 11:09:47 +08003204 /* Invoke any callbacks, transfer the skb to caller, and
Samuel Ortiz518099a2009-01-19 15:30:27 -08003205 * fire off the (possibly) blocking iwl_send_cmd()
Zhu Yib481de92007-09-25 17:54:57 -07003206 * as we reclaim the driver command queue */
3207 if (rxb && rxb->skb)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003208 iwl3945_tx_cmd_complete(priv, rxb);
Zhu Yib481de92007-09-25 17:54:57 -07003209 else
Winkler, Tomas39aadf82008-12-19 10:37:32 +08003210 IWL_WARN(priv, "Claim null rxb?\n");
Zhu Yib481de92007-09-25 17:54:57 -07003211 }
3212
3213 /* For now we just don't re-use anything. We can tweak this
3214 * later to try and re-use notification packets and SKBs that
3215 * fail to Rx correctly */
3216 if (rxb->skb != NULL) {
3217 priv->alloc_rxb_skb--;
3218 dev_kfree_skb_any(rxb->skb);
3219 rxb->skb = NULL;
3220 }
3221
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08003222 pci_unmap_single(priv->pci_dev, rxb->real_dma_addr,
Winkler, Tomas1e33dc62009-01-08 10:19:57 -08003223 priv->hw_params.rx_buf_size,
3224 PCI_DMA_FROMDEVICE);
Zhu Yib481de92007-09-25 17:54:57 -07003225 spin_lock_irqsave(&rxq->lock, flags);
3226 list_add_tail(&rxb->list, &priv->rxq.rx_used);
3227 spin_unlock_irqrestore(&rxq->lock, flags);
3228 i = (i + 1) & RX_QUEUE_MASK;
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08003229 /* If there are a lot of unused frames,
3230 * restock the Rx queue so ucode won't assert. */
3231 if (fill_rx) {
3232 count++;
3233 if (count >= 8) {
3234 priv->rxq.read = i;
3235 __iwl3945_rx_replenish(priv);
3236 count = 0;
3237 }
3238 }
Zhu Yib481de92007-09-25 17:54:57 -07003239 }
3240
3241 /* Backtrack one entry */
3242 priv->rxq.read = i;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003243 iwl3945_rx_queue_restock(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003244}
3245
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08003246#ifdef CONFIG_IWL3945_DEBUG
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003247static void iwl3945_print_rx_config_cmd(struct iwl_priv *priv,
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08003248 struct iwl3945_rxon_cmd *rxon)
Zhu Yib481de92007-09-25 17:54:57 -07003249{
3250 IWL_DEBUG_RADIO("RX CONFIG:\n");
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08003251 iwl_print_hex_dump(priv, IWL_DL_RADIO, (u8 *) rxon, sizeof(*rxon));
Zhu Yib481de92007-09-25 17:54:57 -07003252 IWL_DEBUG_RADIO("u16 channel: 0x%x\n", le16_to_cpu(rxon->channel));
3253 IWL_DEBUG_RADIO("u32 flags: 0x%08X\n", le32_to_cpu(rxon->flags));
3254 IWL_DEBUG_RADIO("u32 filter_flags: 0x%08x\n",
3255 le32_to_cpu(rxon->filter_flags));
3256 IWL_DEBUG_RADIO("u8 dev_type: 0x%x\n", rxon->dev_type);
3257 IWL_DEBUG_RADIO("u8 ofdm_basic_rates: 0x%02x\n",
3258 rxon->ofdm_basic_rates);
3259 IWL_DEBUG_RADIO("u8 cck_basic_rates: 0x%02x\n", rxon->cck_basic_rates);
Johannes Berge1749612008-10-27 15:59:26 -07003260 IWL_DEBUG_RADIO("u8[6] node_addr: %pM\n", rxon->node_addr);
3261 IWL_DEBUG_RADIO("u8[6] bssid_addr: %pM\n", rxon->bssid_addr);
Zhu Yib481de92007-09-25 17:54:57 -07003262 IWL_DEBUG_RADIO("u16 assoc_id: 0x%x\n", le16_to_cpu(rxon->assoc_id));
3263}
3264#endif
3265
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003266static void iwl3945_enable_interrupts(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003267{
3268 IWL_DEBUG_ISR("Enabling interrupts\n");
3269 set_bit(STATUS_INT_ENABLED, &priv->status);
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003270 iwl_write32(priv, CSR_INT_MASK, CSR_INI_SET_MASK);
Zhu Yib481de92007-09-25 17:54:57 -07003271}
3272
Mohamed Abbas0359fac2008-03-28 16:21:08 -07003273
3274/* call this function to flush any scheduled tasklet */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003275static inline void iwl_synchronize_irq(struct iwl_priv *priv)
Mohamed Abbas0359fac2008-03-28 16:21:08 -07003276{
Tomas Winklera96a27f2008-10-23 23:48:56 -07003277 /* wait to make sure we flush pending tasklet*/
Mohamed Abbas0359fac2008-03-28 16:21:08 -07003278 synchronize_irq(priv->pci_dev->irq);
3279 tasklet_kill(&priv->irq_tasklet);
3280}
3281
3282
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003283static inline void iwl3945_disable_interrupts(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003284{
3285 clear_bit(STATUS_INT_ENABLED, &priv->status);
3286
3287 /* disable interrupts from uCode/NIC to host */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003288 iwl_write32(priv, CSR_INT_MASK, 0x00000000);
Zhu Yib481de92007-09-25 17:54:57 -07003289
3290 /* acknowledge/clear/reset any interrupts still pending
3291 * from uCode or flow handler (Rx/Tx DMA) */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003292 iwl_write32(priv, CSR_INT, 0xffffffff);
3293 iwl_write32(priv, CSR_FH_INT_STATUS, 0xffffffff);
Zhu Yib481de92007-09-25 17:54:57 -07003294 IWL_DEBUG_ISR("Disabled interrupts\n");
3295}
3296
3297static const char *desc_lookup(int i)
3298{
3299 switch (i) {
3300 case 1:
3301 return "FAIL";
3302 case 2:
3303 return "BAD_PARAM";
3304 case 3:
3305 return "BAD_CHECKSUM";
3306 case 4:
3307 return "NMI_INTERRUPT";
3308 case 5:
3309 return "SYSASSERT";
3310 case 6:
3311 return "FATAL_ERROR";
3312 }
3313
3314 return "UNKNOWN";
3315}
3316
3317#define ERROR_START_OFFSET (1 * sizeof(u32))
3318#define ERROR_ELEM_SIZE (7 * sizeof(u32))
3319
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003320static void iwl3945_dump_nic_error_log(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003321{
3322 u32 i;
3323 u32 desc, time, count, base, data1;
3324 u32 blink1, blink2, ilink1, ilink2;
3325 int rc;
3326
3327 base = le32_to_cpu(priv->card_alive.error_event_table_ptr);
3328
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003329 if (!iwl3945_hw_valid_rtc_data_addr(base)) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08003330 IWL_ERR(priv, "Not valid error log pointer 0x%08X\n", base);
Zhu Yib481de92007-09-25 17:54:57 -07003331 return;
3332 }
3333
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003334 rc = iwl_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003335 if (rc) {
Winkler, Tomas39aadf82008-12-19 10:37:32 +08003336 IWL_WARN(priv, "Can not read from adapter at this time.\n");
Zhu Yib481de92007-09-25 17:54:57 -07003337 return;
3338 }
3339
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003340 count = iwl_read_targ_mem(priv, base);
Zhu Yib481de92007-09-25 17:54:57 -07003341
3342 if (ERROR_START_OFFSET <= count * ERROR_ELEM_SIZE) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08003343 IWL_ERR(priv, "Start IWL Error Log Dump:\n");
3344 IWL_ERR(priv, "Status: 0x%08lX, count: %d\n",
3345 priv->status, count);
Zhu Yib481de92007-09-25 17:54:57 -07003346 }
3347
Winkler, Tomas15b16872008-12-19 10:37:33 +08003348 IWL_ERR(priv, "Desc Time asrtPC blink2 "
Zhu Yib481de92007-09-25 17:54:57 -07003349 "ilink1 nmiPC Line\n");
3350 for (i = ERROR_START_OFFSET;
3351 i < (count * ERROR_ELEM_SIZE) + ERROR_START_OFFSET;
3352 i += ERROR_ELEM_SIZE) {
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003353 desc = iwl_read_targ_mem(priv, base + i);
Zhu Yib481de92007-09-25 17:54:57 -07003354 time =
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003355 iwl_read_targ_mem(priv, base + i + 1 * sizeof(u32));
Zhu Yib481de92007-09-25 17:54:57 -07003356 blink1 =
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003357 iwl_read_targ_mem(priv, base + i + 2 * sizeof(u32));
Zhu Yib481de92007-09-25 17:54:57 -07003358 blink2 =
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003359 iwl_read_targ_mem(priv, base + i + 3 * sizeof(u32));
Zhu Yib481de92007-09-25 17:54:57 -07003360 ilink1 =
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003361 iwl_read_targ_mem(priv, base + i + 4 * sizeof(u32));
Zhu Yib481de92007-09-25 17:54:57 -07003362 ilink2 =
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003363 iwl_read_targ_mem(priv, base + i + 5 * sizeof(u32));
Zhu Yib481de92007-09-25 17:54:57 -07003364 data1 =
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003365 iwl_read_targ_mem(priv, base + i + 6 * sizeof(u32));
Zhu Yib481de92007-09-25 17:54:57 -07003366
Winkler, Tomas15b16872008-12-19 10:37:33 +08003367 IWL_ERR(priv,
3368 "%-13s (#%d) %010u 0x%05X 0x%05X 0x%05X 0x%05X %u\n\n",
3369 desc_lookup(desc), desc, time, blink1, blink2,
3370 ilink1, ilink2, data1);
Zhu Yib481de92007-09-25 17:54:57 -07003371 }
3372
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003373 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003374
3375}
3376
Ben Cahillf58177b2007-11-29 11:09:43 +08003377#define EVENT_START_OFFSET (6 * sizeof(u32))
Zhu Yib481de92007-09-25 17:54:57 -07003378
3379/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003380 * iwl3945_print_event_log - Dump error event log to syslog
Zhu Yib481de92007-09-25 17:54:57 -07003381 *
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003382 * NOTE: Must be called with iwl_grab_nic_access() already obtained!
Zhu Yib481de92007-09-25 17:54:57 -07003383 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003384static void iwl3945_print_event_log(struct iwl_priv *priv, u32 start_idx,
Zhu Yib481de92007-09-25 17:54:57 -07003385 u32 num_events, u32 mode)
3386{
3387 u32 i;
3388 u32 base; /* SRAM byte address of event log header */
3389 u32 event_size; /* 2 u32s, or 3 u32s if timestamp recorded */
3390 u32 ptr; /* SRAM byte address of log data */
3391 u32 ev, time, data; /* event log data */
3392
3393 if (num_events == 0)
3394 return;
3395
3396 base = le32_to_cpu(priv->card_alive.log_event_table_ptr);
3397
3398 if (mode == 0)
3399 event_size = 2 * sizeof(u32);
3400 else
3401 event_size = 3 * sizeof(u32);
3402
3403 ptr = base + EVENT_START_OFFSET + (start_idx * event_size);
3404
3405 /* "time" is actually "data" for mode 0 (no timestamp).
3406 * place event id # at far right for easier visual parsing. */
3407 for (i = 0; i < num_events; i++) {
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003408 ev = iwl_read_targ_mem(priv, ptr);
Zhu Yib481de92007-09-25 17:54:57 -07003409 ptr += sizeof(u32);
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003410 time = iwl_read_targ_mem(priv, ptr);
Zhu Yib481de92007-09-25 17:54:57 -07003411 ptr += sizeof(u32);
Winkler, Tomas15b16872008-12-19 10:37:33 +08003412 if (mode == 0) {
3413 /* data, ev */
3414 IWL_ERR(priv, "0x%08x\t%04u\n", time, ev);
3415 } else {
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003416 data = iwl_read_targ_mem(priv, ptr);
Zhu Yib481de92007-09-25 17:54:57 -07003417 ptr += sizeof(u32);
Winkler, Tomas15b16872008-12-19 10:37:33 +08003418 IWL_ERR(priv, "%010u\t0x%08x\t%04u\n", time, data, ev);
Zhu Yib481de92007-09-25 17:54:57 -07003419 }
3420 }
3421}
3422
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003423static void iwl3945_dump_nic_event_log(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003424{
3425 int rc;
3426 u32 base; /* SRAM byte address of event log header */
3427 u32 capacity; /* event log capacity in # entries */
3428 u32 mode; /* 0 - no timestamp, 1 - timestamp recorded */
3429 u32 num_wraps; /* # times uCode wrapped to top of log */
3430 u32 next_entry; /* index of next entry to be written by uCode */
3431 u32 size; /* # entries that we'll print */
3432
3433 base = le32_to_cpu(priv->card_alive.log_event_table_ptr);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003434 if (!iwl3945_hw_valid_rtc_data_addr(base)) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08003435 IWL_ERR(priv, "Invalid event log pointer 0x%08X\n", base);
Zhu Yib481de92007-09-25 17:54:57 -07003436 return;
3437 }
3438
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003439 rc = iwl_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003440 if (rc) {
Winkler, Tomas39aadf82008-12-19 10:37:32 +08003441 IWL_WARN(priv, "Can not read from adapter at this time.\n");
Zhu Yib481de92007-09-25 17:54:57 -07003442 return;
3443 }
3444
3445 /* event log header */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003446 capacity = iwl_read_targ_mem(priv, base);
3447 mode = iwl_read_targ_mem(priv, base + (1 * sizeof(u32)));
3448 num_wraps = iwl_read_targ_mem(priv, base + (2 * sizeof(u32)));
3449 next_entry = iwl_read_targ_mem(priv, base + (3 * sizeof(u32)));
Zhu Yib481de92007-09-25 17:54:57 -07003450
3451 size = num_wraps ? capacity : next_entry;
3452
3453 /* bail out if nothing in log */
3454 if (size == 0) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08003455 IWL_ERR(priv, "Start IWL Event Log Dump: nothing in log\n");
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003456 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003457 return;
3458 }
3459
Winkler, Tomas15b16872008-12-19 10:37:33 +08003460 IWL_ERR(priv, "Start IWL Event Log Dump: display count %d, wraps %d\n",
Zhu Yib481de92007-09-25 17:54:57 -07003461 size, num_wraps);
3462
3463 /* if uCode has wrapped back to top of log, start at the oldest entry,
3464 * i.e the next one that uCode would fill. */
3465 if (num_wraps)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003466 iwl3945_print_event_log(priv, next_entry,
Zhu Yib481de92007-09-25 17:54:57 -07003467 capacity - next_entry, mode);
3468
3469 /* (then/else) start at top of log */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003470 iwl3945_print_event_log(priv, 0, next_entry, mode);
Zhu Yib481de92007-09-25 17:54:57 -07003471
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003472 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003473}
3474
3475/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003476 * iwl3945_irq_handle_error - called for HW or SW error interrupt from card
Zhu Yib481de92007-09-25 17:54:57 -07003477 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003478static void iwl3945_irq_handle_error(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003479{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003480 /* Set the FW error flag -- cleared on iwl3945_down */
Zhu Yib481de92007-09-25 17:54:57 -07003481 set_bit(STATUS_FW_ERROR, &priv->status);
3482
3483 /* Cancel currently queued command. */
3484 clear_bit(STATUS_HCMD_ACTIVE, &priv->status);
3485
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08003486#ifdef CONFIG_IWL3945_DEBUG
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08003487 if (priv->debug_level & IWL_DL_FW_ERRORS) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003488 iwl3945_dump_nic_error_log(priv);
3489 iwl3945_dump_nic_event_log(priv);
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08003490 iwl3945_print_rx_config_cmd(priv, &priv->staging39_rxon);
Zhu Yib481de92007-09-25 17:54:57 -07003491 }
3492#endif
3493
3494 wake_up_interruptible(&priv->wait_command_queue);
3495
3496 /* Keep the restart process from trying to send host
3497 * commands by clearing the INIT status bit */
3498 clear_bit(STATUS_READY, &priv->status);
3499
3500 if (!test_bit(STATUS_EXIT_PENDING, &priv->status)) {
3501 IWL_DEBUG(IWL_DL_INFO | IWL_DL_FW_ERRORS,
3502 "Restarting adapter due to uCode error.\n");
3503
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003504 if (iwl3945_is_associated(priv)) {
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08003505 memcpy(&priv->recovery39_rxon, &priv->active39_rxon,
3506 sizeof(priv->recovery39_rxon));
Zhu Yib481de92007-09-25 17:54:57 -07003507 priv->error_recovering = 1;
3508 }
3509 queue_work(priv->workqueue, &priv->restart);
3510 }
3511}
3512
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003513static void iwl3945_error_recovery(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003514{
3515 unsigned long flags;
3516
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08003517 memcpy(&priv->staging39_rxon, &priv->recovery39_rxon,
3518 sizeof(priv->staging39_rxon));
3519 priv->staging39_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003520 iwl3945_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003521
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003522 iwl3945_add_station(priv, priv->bssid, 1, 0);
Zhu Yib481de92007-09-25 17:54:57 -07003523
3524 spin_lock_irqsave(&priv->lock, flags);
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08003525 priv->assoc_id = le16_to_cpu(priv->staging39_rxon.assoc_id);
Zhu Yib481de92007-09-25 17:54:57 -07003526 priv->error_recovering = 0;
3527 spin_unlock_irqrestore(&priv->lock, flags);
3528}
3529
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003530static void iwl3945_irq_tasklet(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003531{
3532 u32 inta, handled = 0;
3533 u32 inta_fh;
3534 unsigned long flags;
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08003535#ifdef CONFIG_IWL3945_DEBUG
Zhu Yib481de92007-09-25 17:54:57 -07003536 u32 inta_mask;
3537#endif
3538
3539 spin_lock_irqsave(&priv->lock, flags);
3540
3541 /* Ack/clear/reset pending uCode interrupts.
3542 * Note: Some bits in CSR_INT are "OR" of bits in CSR_FH_INT_STATUS,
3543 * and will clear only when CSR_FH_INT_STATUS gets cleared. */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003544 inta = iwl_read32(priv, CSR_INT);
3545 iwl_write32(priv, CSR_INT, inta);
Zhu Yib481de92007-09-25 17:54:57 -07003546
3547 /* Ack/clear/reset pending flow-handler (DMA) interrupts.
3548 * Any new interrupts that happen after this, either while we're
3549 * in this tasklet, or later, will show up in next ISR/tasklet. */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003550 inta_fh = iwl_read32(priv, CSR_FH_INT_STATUS);
3551 iwl_write32(priv, CSR_FH_INT_STATUS, inta_fh);
Zhu Yib481de92007-09-25 17:54:57 -07003552
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08003553#ifdef CONFIG_IWL3945_DEBUG
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08003554 if (priv->debug_level & IWL_DL_ISR) {
Ben Cahill9fbab512007-11-29 11:09:47 +08003555 /* just for debug */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003556 inta_mask = iwl_read32(priv, CSR_INT_MASK);
Zhu Yib481de92007-09-25 17:54:57 -07003557 IWL_DEBUG_ISR("inta 0x%08x, enabled 0x%08x, fh 0x%08x\n",
3558 inta, inta_mask, inta_fh);
3559 }
3560#endif
3561
3562 /* Since CSR_INT and CSR_FH_INT_STATUS reads and clears are not
3563 * atomic, make sure that inta covers all the interrupts that
3564 * we've discovered, even if FH interrupt came in just after
3565 * reading CSR_INT. */
Tomas Winkler6f83eaa2008-03-04 18:09:28 -08003566 if (inta_fh & CSR39_FH_INT_RX_MASK)
Zhu Yib481de92007-09-25 17:54:57 -07003567 inta |= CSR_INT_BIT_FH_RX;
Tomas Winkler6f83eaa2008-03-04 18:09:28 -08003568 if (inta_fh & CSR39_FH_INT_TX_MASK)
Zhu Yib481de92007-09-25 17:54:57 -07003569 inta |= CSR_INT_BIT_FH_TX;
3570
3571 /* Now service all interrupt bits discovered above. */
3572 if (inta & CSR_INT_BIT_HW_ERR) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08003573 IWL_ERR(priv, "Microcode HW error detected. Restarting.\n");
Zhu Yib481de92007-09-25 17:54:57 -07003574
3575 /* Tell the device to stop sending interrupts */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003576 iwl3945_disable_interrupts(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003577
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003578 iwl3945_irq_handle_error(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003579
3580 handled |= CSR_INT_BIT_HW_ERR;
3581
3582 spin_unlock_irqrestore(&priv->lock, flags);
3583
3584 return;
3585 }
3586
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08003587#ifdef CONFIG_IWL3945_DEBUG
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08003588 if (priv->debug_level & (IWL_DL_ISR)) {
Zhu Yib481de92007-09-25 17:54:57 -07003589 /* NIC fires this, but we don't use it, redundant with WAKEUP */
Joonwoo Park25c03d82008-01-23 10:15:20 -08003590 if (inta & CSR_INT_BIT_SCD)
3591 IWL_DEBUG_ISR("Scheduler finished to transmit "
3592 "the frame/frames.\n");
Zhu Yib481de92007-09-25 17:54:57 -07003593
3594 /* Alive notification via Rx interrupt will do the real work */
3595 if (inta & CSR_INT_BIT_ALIVE)
3596 IWL_DEBUG_ISR("Alive interrupt\n");
3597 }
3598#endif
3599 /* Safely ignore these bits for debug checks below */
Joonwoo Park25c03d82008-01-23 10:15:20 -08003600 inta &= ~(CSR_INT_BIT_SCD | CSR_INT_BIT_ALIVE);
Zhu Yib481de92007-09-25 17:54:57 -07003601
Zhu Yib481de92007-09-25 17:54:57 -07003602 /* Error detected by uCode */
3603 if (inta & CSR_INT_BIT_SW_ERR) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08003604 IWL_ERR(priv, "Microcode SW error detected. "
3605 "Restarting 0x%X.\n", inta);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003606 iwl3945_irq_handle_error(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003607 handled |= CSR_INT_BIT_SW_ERR;
3608 }
3609
3610 /* uCode wakes up after power-down sleep */
3611 if (inta & CSR_INT_BIT_WAKEUP) {
3612 IWL_DEBUG_ISR("Wakeup interrupt\n");
Winkler, Tomas141c43a2009-01-08 10:19:53 -08003613 iwl_rx_queue_update_write_ptr(priv, &priv->rxq);
Samuel Ortiz4f3602c2009-01-19 15:30:25 -08003614 iwl_txq_update_write_ptr(priv, &priv->txq[0]);
3615 iwl_txq_update_write_ptr(priv, &priv->txq[1]);
3616 iwl_txq_update_write_ptr(priv, &priv->txq[2]);
3617 iwl_txq_update_write_ptr(priv, &priv->txq[3]);
3618 iwl_txq_update_write_ptr(priv, &priv->txq[4]);
3619 iwl_txq_update_write_ptr(priv, &priv->txq[5]);
Zhu Yib481de92007-09-25 17:54:57 -07003620
3621 handled |= CSR_INT_BIT_WAKEUP;
3622 }
3623
3624 /* All uCode command responses, including Tx command responses,
3625 * Rx "responses" (frame-received notification), and other
3626 * notifications from uCode come through here*/
3627 if (inta & (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX)) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003628 iwl3945_rx_handle(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003629 handled |= (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX);
3630 }
3631
3632 if (inta & CSR_INT_BIT_FH_TX) {
3633 IWL_DEBUG_ISR("Tx interrupt\n");
3634
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003635 iwl_write32(priv, CSR_FH_INT_STATUS, (1 << 6));
3636 if (!iwl_grab_nic_access(priv)) {
3637 iwl_write_direct32(priv, FH39_TCSR_CREDIT
Tomas Winklerbddadf82008-12-19 10:37:01 +08003638 (FH39_SRVC_CHNL), 0x0);
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003639 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003640 }
3641 handled |= CSR_INT_BIT_FH_TX;
3642 }
3643
3644 if (inta & ~handled)
Winkler, Tomas15b16872008-12-19 10:37:33 +08003645 IWL_ERR(priv, "Unhandled INTA bits 0x%08x\n", inta & ~handled);
Zhu Yib481de92007-09-25 17:54:57 -07003646
3647 if (inta & ~CSR_INI_SET_MASK) {
Winkler, Tomas39aadf82008-12-19 10:37:32 +08003648 IWL_WARN(priv, "Disabled INTA bits 0x%08x were pending\n",
Zhu Yib481de92007-09-25 17:54:57 -07003649 inta & ~CSR_INI_SET_MASK);
Winkler, Tomas39aadf82008-12-19 10:37:32 +08003650 IWL_WARN(priv, " with FH_INT = 0x%08x\n", inta_fh);
Zhu Yib481de92007-09-25 17:54:57 -07003651 }
3652
3653 /* Re-enable all interrupts */
Mohamed Abbas0359fac2008-03-28 16:21:08 -07003654 /* only Re-enable if disabled by irq */
3655 if (test_bit(STATUS_INT_ENABLED, &priv->status))
3656 iwl3945_enable_interrupts(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003657
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08003658#ifdef CONFIG_IWL3945_DEBUG
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08003659 if (priv->debug_level & (IWL_DL_ISR)) {
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003660 inta = iwl_read32(priv, CSR_INT);
3661 inta_mask = iwl_read32(priv, CSR_INT_MASK);
3662 inta_fh = iwl_read32(priv, CSR_FH_INT_STATUS);
Zhu Yib481de92007-09-25 17:54:57 -07003663 IWL_DEBUG_ISR("End inta 0x%08x, enabled 0x%08x, fh 0x%08x, "
3664 "flags 0x%08lx\n", inta, inta_mask, inta_fh, flags);
3665 }
3666#endif
3667 spin_unlock_irqrestore(&priv->lock, flags);
3668}
3669
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003670static irqreturn_t iwl3945_isr(int irq, void *data)
Zhu Yib481de92007-09-25 17:54:57 -07003671{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003672 struct iwl_priv *priv = data;
Zhu Yib481de92007-09-25 17:54:57 -07003673 u32 inta, inta_mask;
3674 u32 inta_fh;
3675 if (!priv)
3676 return IRQ_NONE;
3677
3678 spin_lock(&priv->lock);
3679
3680 /* Disable (but don't clear!) interrupts here to avoid
3681 * back-to-back ISRs and sporadic interrupts from our NIC.
3682 * If we have something to service, the tasklet will re-enable ints.
3683 * If we *don't* have something, we'll re-enable before leaving here. */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003684 inta_mask = iwl_read32(priv, CSR_INT_MASK); /* just for debug */
3685 iwl_write32(priv, CSR_INT_MASK, 0x00000000);
Zhu Yib481de92007-09-25 17:54:57 -07003686
3687 /* Discover which interrupts are active/pending */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003688 inta = iwl_read32(priv, CSR_INT);
3689 inta_fh = iwl_read32(priv, CSR_FH_INT_STATUS);
Zhu Yib481de92007-09-25 17:54:57 -07003690
3691 /* Ignore interrupt if there's nothing in NIC to service.
3692 * This may be due to IRQ shared with another device,
3693 * or due to sporadic interrupts thrown from our NIC. */
3694 if (!inta && !inta_fh) {
3695 IWL_DEBUG_ISR("Ignore interrupt, inta == 0, inta_fh == 0\n");
3696 goto none;
3697 }
3698
3699 if ((inta == 0xFFFFFFFF) || ((inta & 0xFFFFFFF0) == 0xa5a5a5a0)) {
3700 /* Hardware disappeared */
Winkler, Tomas39aadf82008-12-19 10:37:32 +08003701 IWL_WARN(priv, "HARDWARE GONE?? INTA == 0x%08x\n", inta);
Oliver Neukumcb4da1a2007-11-13 21:10:32 -08003702 goto unplugged;
Zhu Yib481de92007-09-25 17:54:57 -07003703 }
3704
3705 IWL_DEBUG_ISR("ISR inta 0x%08x, enabled 0x%08x, fh 0x%08x\n",
3706 inta, inta_mask, inta_fh);
3707
Joonwoo Park25c03d82008-01-23 10:15:20 -08003708 inta &= ~CSR_INT_BIT_SCD;
3709
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003710 /* iwl3945_irq_tasklet() will service interrupts and re-enable them */
Joonwoo Park25c03d82008-01-23 10:15:20 -08003711 if (likely(inta || inta_fh))
3712 tasklet_schedule(&priv->irq_tasklet);
Oliver Neukumcb4da1a2007-11-13 21:10:32 -08003713unplugged:
Zhu Yib481de92007-09-25 17:54:57 -07003714 spin_unlock(&priv->lock);
3715
3716 return IRQ_HANDLED;
3717
3718 none:
3719 /* re-enable interrupts here since we don't have anything to service. */
Mohamed Abbas0359fac2008-03-28 16:21:08 -07003720 /* only Re-enable if disabled by irq */
3721 if (test_bit(STATUS_INT_ENABLED, &priv->status))
3722 iwl3945_enable_interrupts(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003723 spin_unlock(&priv->lock);
3724 return IRQ_NONE;
3725}
3726
3727/************************** EEPROM BANDS ****************************
3728 *
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003729 * The iwl3945_eeprom_band definitions below provide the mapping from the
Zhu Yib481de92007-09-25 17:54:57 -07003730 * EEPROM contents to the specific channel number supported for each
3731 * band.
3732 *
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08003733 * For example, iwl3945_priv->eeprom39.band_3_channels[4] from the band_3
Zhu Yib481de92007-09-25 17:54:57 -07003734 * definition below maps to physical channel 42 in the 5.2GHz spectrum.
3735 * The specific geography and calibration information for that channel
3736 * is contained in the eeprom map itself.
3737 *
3738 * During init, we copy the eeprom information and channel map
3739 * information into priv->channel_info_24/52 and priv->channel_map_24/52
3740 *
3741 * channel_map_24/52 provides the index in the channel_info array for a
3742 * given channel. We have to have two separate maps as there is channel
3743 * overlap with the 2.4GHz and 5.2GHz spectrum as seen in band_1 and
3744 * band_2
3745 *
3746 * A value of 0xff stored in the channel_map indicates that the channel
3747 * is not supported by the hardware at all.
3748 *
3749 * A value of 0xfe in the channel_map indicates that the channel is not
3750 * valid for Tx with the current hardware. This means that
3751 * while the system can tune and receive on a given channel, it may not
3752 * be able to associate or transmit any frames on that
3753 * channel. There is no corresponding channel information for that
3754 * entry.
3755 *
3756 *********************************************************************/
3757
3758/* 2.4 GHz */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003759static const u8 iwl3945_eeprom_band_1[14] = {
Zhu Yib481de92007-09-25 17:54:57 -07003760 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14
3761};
3762
3763/* 5.2 GHz bands */
Ben Cahill9fbab512007-11-29 11:09:47 +08003764static const u8 iwl3945_eeprom_band_2[] = { /* 4915-5080MHz */
Zhu Yib481de92007-09-25 17:54:57 -07003765 183, 184, 185, 187, 188, 189, 192, 196, 7, 8, 11, 12, 16
3766};
3767
Ben Cahill9fbab512007-11-29 11:09:47 +08003768static const u8 iwl3945_eeprom_band_3[] = { /* 5170-5320MHz */
Zhu Yib481de92007-09-25 17:54:57 -07003769 34, 36, 38, 40, 42, 44, 46, 48, 52, 56, 60, 64
3770};
3771
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003772static const u8 iwl3945_eeprom_band_4[] = { /* 5500-5700MHz */
Zhu Yib481de92007-09-25 17:54:57 -07003773 100, 104, 108, 112, 116, 120, 124, 128, 132, 136, 140
3774};
3775
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003776static const u8 iwl3945_eeprom_band_5[] = { /* 5725-5825MHz */
Zhu Yib481de92007-09-25 17:54:57 -07003777 145, 149, 153, 157, 161, 165
3778};
3779
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003780static void iwl3945_init_band_reference(const struct iwl_priv *priv, int band,
Zhu Yib481de92007-09-25 17:54:57 -07003781 int *eeprom_ch_count,
Samuel Ortiz0f741d92008-12-19 10:37:10 +08003782 const struct iwl_eeprom_channel
Zhu Yib481de92007-09-25 17:54:57 -07003783 **eeprom_ch_info,
3784 const u8 **eeprom_ch_index)
3785{
3786 switch (band) {
3787 case 1: /* 2.4GHz band */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003788 *eeprom_ch_count = ARRAY_SIZE(iwl3945_eeprom_band_1);
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08003789 *eeprom_ch_info = priv->eeprom39.band_1_channels;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003790 *eeprom_ch_index = iwl3945_eeprom_band_1;
Zhu Yib481de92007-09-25 17:54:57 -07003791 break;
Ben Cahill9fbab512007-11-29 11:09:47 +08003792 case 2: /* 4.9GHz band */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003793 *eeprom_ch_count = ARRAY_SIZE(iwl3945_eeprom_band_2);
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08003794 *eeprom_ch_info = priv->eeprom39.band_2_channels;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003795 *eeprom_ch_index = iwl3945_eeprom_band_2;
Zhu Yib481de92007-09-25 17:54:57 -07003796 break;
3797 case 3: /* 5.2GHz band */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003798 *eeprom_ch_count = ARRAY_SIZE(iwl3945_eeprom_band_3);
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08003799 *eeprom_ch_info = priv->eeprom39.band_3_channels;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003800 *eeprom_ch_index = iwl3945_eeprom_band_3;
Zhu Yib481de92007-09-25 17:54:57 -07003801 break;
Ben Cahill9fbab512007-11-29 11:09:47 +08003802 case 4: /* 5.5GHz band */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003803 *eeprom_ch_count = ARRAY_SIZE(iwl3945_eeprom_band_4);
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08003804 *eeprom_ch_info = priv->eeprom39.band_4_channels;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003805 *eeprom_ch_index = iwl3945_eeprom_band_4;
Zhu Yib481de92007-09-25 17:54:57 -07003806 break;
Ben Cahill9fbab512007-11-29 11:09:47 +08003807 case 5: /* 5.7GHz band */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003808 *eeprom_ch_count = ARRAY_SIZE(iwl3945_eeprom_band_5);
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08003809 *eeprom_ch_info = priv->eeprom39.band_5_channels;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003810 *eeprom_ch_index = iwl3945_eeprom_band_5;
Zhu Yib481de92007-09-25 17:54:57 -07003811 break;
3812 default:
3813 BUG();
3814 return;
3815 }
3816}
3817
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003818/**
3819 * iwl3945_get_channel_info - Find driver's private channel info
3820 *
3821 * Based on band and channel number.
3822 */
Samuel Ortizd20b3c62008-12-19 10:37:15 +08003823const struct iwl_channel_info *
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003824iwl3945_get_channel_info(const struct iwl_priv *priv,
Samuel Ortizd20b3c62008-12-19 10:37:15 +08003825 enum ieee80211_band band, u16 channel)
Zhu Yib481de92007-09-25 17:54:57 -07003826{
3827 int i;
3828
Johannes Berg8318d782008-01-24 19:38:38 +01003829 switch (band) {
3830 case IEEE80211_BAND_5GHZ:
Zhu Yib481de92007-09-25 17:54:57 -07003831 for (i = 14; i < priv->channel_count; i++) {
3832 if (priv->channel_info[i].channel == channel)
3833 return &priv->channel_info[i];
3834 }
3835 break;
3836
Johannes Berg8318d782008-01-24 19:38:38 +01003837 case IEEE80211_BAND_2GHZ:
Zhu Yib481de92007-09-25 17:54:57 -07003838 if (channel >= 1 && channel <= 14)
3839 return &priv->channel_info[channel - 1];
3840 break;
Johannes Berg8318d782008-01-24 19:38:38 +01003841 case IEEE80211_NUM_BANDS:
3842 WARN_ON(1);
Zhu Yib481de92007-09-25 17:54:57 -07003843 }
3844
3845 return NULL;
3846}
3847
3848#define CHECK_AND_PRINT(x) ((eeprom_ch_info[ch].flags & EEPROM_CHANNEL_##x) \
3849 ? # x " " : "")
3850
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003851/**
3852 * iwl3945_init_channel_map - Set up driver's info for all possible channels
3853 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003854static int iwl3945_init_channel_map(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003855{
3856 int eeprom_ch_count = 0;
3857 const u8 *eeprom_ch_index = NULL;
Samuel Ortiz0f741d92008-12-19 10:37:10 +08003858 const struct iwl_eeprom_channel *eeprom_ch_info = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07003859 int band, ch;
Samuel Ortizd20b3c62008-12-19 10:37:15 +08003860 struct iwl_channel_info *ch_info;
Zhu Yib481de92007-09-25 17:54:57 -07003861
3862 if (priv->channel_count) {
3863 IWL_DEBUG_INFO("Channel map already initialized.\n");
3864 return 0;
3865 }
3866
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08003867 if (priv->eeprom39.version < 0x2f) {
Winkler, Tomas39aadf82008-12-19 10:37:32 +08003868 IWL_WARN(priv, "Unsupported EEPROM version: 0x%04X\n",
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08003869 priv->eeprom39.version);
Zhu Yib481de92007-09-25 17:54:57 -07003870 return -EINVAL;
3871 }
3872
3873 IWL_DEBUG_INFO("Initializing regulatory info from EEPROM\n");
3874
3875 priv->channel_count =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003876 ARRAY_SIZE(iwl3945_eeprom_band_1) +
3877 ARRAY_SIZE(iwl3945_eeprom_band_2) +
3878 ARRAY_SIZE(iwl3945_eeprom_band_3) +
3879 ARRAY_SIZE(iwl3945_eeprom_band_4) +
3880 ARRAY_SIZE(iwl3945_eeprom_band_5);
Zhu Yib481de92007-09-25 17:54:57 -07003881
3882 IWL_DEBUG_INFO("Parsing data for %d channels.\n", priv->channel_count);
3883
Samuel Ortizd20b3c62008-12-19 10:37:15 +08003884 priv->channel_info = kzalloc(sizeof(struct iwl_channel_info) *
Zhu Yib481de92007-09-25 17:54:57 -07003885 priv->channel_count, GFP_KERNEL);
3886 if (!priv->channel_info) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08003887 IWL_ERR(priv, "Could not allocate channel_info\n");
Zhu Yib481de92007-09-25 17:54:57 -07003888 priv->channel_count = 0;
3889 return -ENOMEM;
3890 }
3891
3892 ch_info = priv->channel_info;
3893
3894 /* Loop through the 5 EEPROM bands adding them in order to the
3895 * channel map we maintain (that contains additional information than
3896 * what just in the EEPROM) */
3897 for (band = 1; band <= 5; band++) {
3898
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003899 iwl3945_init_band_reference(priv, band, &eeprom_ch_count,
Zhu Yib481de92007-09-25 17:54:57 -07003900 &eeprom_ch_info, &eeprom_ch_index);
3901
3902 /* Loop through each band adding each of the channels */
3903 for (ch = 0; ch < eeprom_ch_count; ch++) {
3904 ch_info->channel = eeprom_ch_index[ch];
Johannes Berg8318d782008-01-24 19:38:38 +01003905 ch_info->band = (band == 1) ? IEEE80211_BAND_2GHZ :
3906 IEEE80211_BAND_5GHZ;
Zhu Yib481de92007-09-25 17:54:57 -07003907
3908 /* permanently store EEPROM's channel regulatory flags
3909 * and max power in channel info database. */
3910 ch_info->eeprom = eeprom_ch_info[ch];
3911
3912 /* Copy the run-time flags so they are there even on
3913 * invalid channels */
3914 ch_info->flags = eeprom_ch_info[ch].flags;
3915
3916 if (!(is_channel_valid(ch_info))) {
3917 IWL_DEBUG_INFO("Ch. %d Flags %x [%sGHz] - "
3918 "No traffic\n",
3919 ch_info->channel,
3920 ch_info->flags,
3921 is_channel_a_band(ch_info) ?
3922 "5.2" : "2.4");
3923 ch_info++;
3924 continue;
3925 }
3926
3927 /* Initialize regulatory-based run-time data */
3928 ch_info->max_power_avg = ch_info->curr_txpow =
3929 eeprom_ch_info[ch].max_power_avg;
3930 ch_info->scan_power = eeprom_ch_info[ch].max_power_avg;
3931 ch_info->min_power = 0;
3932
Guy Cohenfe7c4042008-04-21 15:41:56 -07003933 IWL_DEBUG_INFO("Ch. %d [%sGHz] %s%s%s%s%s%s(0x%02x"
Zhu Yib481de92007-09-25 17:54:57 -07003934 " %ddBm): Ad-Hoc %ssupported\n",
3935 ch_info->channel,
3936 is_channel_a_band(ch_info) ?
3937 "5.2" : "2.4",
Tomas Winkler8211ef72008-03-02 01:36:04 +02003938 CHECK_AND_PRINT(VALID),
Zhu Yib481de92007-09-25 17:54:57 -07003939 CHECK_AND_PRINT(IBSS),
3940 CHECK_AND_PRINT(ACTIVE),
3941 CHECK_AND_PRINT(RADAR),
3942 CHECK_AND_PRINT(WIDE),
Zhu Yib481de92007-09-25 17:54:57 -07003943 CHECK_AND_PRINT(DFS),
3944 eeprom_ch_info[ch].flags,
3945 eeprom_ch_info[ch].max_power_avg,
3946 ((eeprom_ch_info[ch].
3947 flags & EEPROM_CHANNEL_IBSS)
3948 && !(eeprom_ch_info[ch].
3949 flags & EEPROM_CHANNEL_RADAR))
3950 ? "" : "not ");
3951
3952 /* Set the user_txpower_limit to the highest power
3953 * supported by any channel */
3954 if (eeprom_ch_info[ch].max_power_avg >
3955 priv->user_txpower_limit)
3956 priv->user_txpower_limit =
3957 eeprom_ch_info[ch].max_power_avg;
3958
3959 ch_info++;
3960 }
3961 }
3962
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003963 /* Set up txpower settings in driver for all channels */
Zhu Yib481de92007-09-25 17:54:57 -07003964 if (iwl3945_txpower_set_from_eeprom(priv))
3965 return -EIO;
3966
3967 return 0;
3968}
3969
Reinette Chatre849e0dc2008-01-23 10:15:18 -08003970/*
3971 * iwl3945_free_channel_map - undo allocations in iwl3945_init_channel_map
3972 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003973static void iwl3945_free_channel_map(struct iwl_priv *priv)
Reinette Chatre849e0dc2008-01-23 10:15:18 -08003974{
3975 kfree(priv->channel_info);
3976 priv->channel_count = 0;
3977}
3978
Zhu Yib481de92007-09-25 17:54:57 -07003979/* For active scan, listen ACTIVE_DWELL_TIME (msec) on each channel after
3980 * sending probe req. This should be set long enough to hear probe responses
3981 * from more than one AP. */
Abhijeet Kolekarf9340522008-09-03 11:26:58 +08003982#define IWL_ACTIVE_DWELL_TIME_24 (30) /* all times in msec */
3983#define IWL_ACTIVE_DWELL_TIME_52 (20)
3984
3985#define IWL_ACTIVE_DWELL_FACTOR_24GHZ (3)
3986#define IWL_ACTIVE_DWELL_FACTOR_52GHZ (2)
Zhu Yib481de92007-09-25 17:54:57 -07003987
3988/* For faster active scanning, scan will move to the next channel if fewer than
3989 * PLCP_QUIET_THRESH packets are heard on this channel within
3990 * ACTIVE_QUIET_TIME after sending probe request. This shortens the dwell
3991 * time if it's a quiet channel (nothing responded to our probe, and there's
3992 * no other traffic).
3993 * Disable "quiet" feature by setting PLCP_QUIET_THRESH to 0. */
3994#define IWL_PLCP_QUIET_THRESH __constant_cpu_to_le16(1) /* packets */
Abhijeet Kolekarf9340522008-09-03 11:26:58 +08003995#define IWL_ACTIVE_QUIET_TIME __constant_cpu_to_le16(10) /* msec */
Zhu Yib481de92007-09-25 17:54:57 -07003996
3997/* For passive scan, listen PASSIVE_DWELL_TIME (msec) on each channel.
3998 * Must be set longer than active dwell time.
3999 * For the most reliable scan, set > AP beacon interval (typically 100msec). */
4000#define IWL_PASSIVE_DWELL_TIME_24 (20) /* all times in msec */
4001#define IWL_PASSIVE_DWELL_TIME_52 (10)
4002#define IWL_PASSIVE_DWELL_BASE (100)
4003#define IWL_CHANNEL_TUNE_TIME 5
4004
Kolekar, Abhijeete720ce92008-11-07 09:58:42 -08004005#define IWL_SCAN_PROBE_MASK(n) (BIT(n) | (BIT(n) - BIT(1)))
Abhijeet Kolekarf9340522008-09-03 11:26:58 +08004006
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08004007static inline u16 iwl3945_get_active_dwell_time(struct iwl_priv *priv,
Abhijeet Kolekarf9340522008-09-03 11:26:58 +08004008 enum ieee80211_band band,
4009 u8 n_probes)
Zhu Yib481de92007-09-25 17:54:57 -07004010{
Johannes Berg8318d782008-01-24 19:38:38 +01004011 if (band == IEEE80211_BAND_5GHZ)
Abhijeet Kolekarf9340522008-09-03 11:26:58 +08004012 return IWL_ACTIVE_DWELL_TIME_52 +
4013 IWL_ACTIVE_DWELL_FACTOR_52GHZ * (n_probes + 1);
Zhu Yib481de92007-09-25 17:54:57 -07004014 else
Abhijeet Kolekarf9340522008-09-03 11:26:58 +08004015 return IWL_ACTIVE_DWELL_TIME_24 +
4016 IWL_ACTIVE_DWELL_FACTOR_24GHZ * (n_probes + 1);
Zhu Yib481de92007-09-25 17:54:57 -07004017}
4018
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08004019static u16 iwl3945_get_passive_dwell_time(struct iwl_priv *priv,
Johannes Berg8318d782008-01-24 19:38:38 +01004020 enum ieee80211_band band)
Zhu Yib481de92007-09-25 17:54:57 -07004021{
Johannes Berg8318d782008-01-24 19:38:38 +01004022 u16 passive = (band == IEEE80211_BAND_2GHZ) ?
Zhu Yib481de92007-09-25 17:54:57 -07004023 IWL_PASSIVE_DWELL_BASE + IWL_PASSIVE_DWELL_TIME_24 :
4024 IWL_PASSIVE_DWELL_BASE + IWL_PASSIVE_DWELL_TIME_52;
4025
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004026 if (iwl3945_is_associated(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07004027 /* If we're associated, we clamp the maximum passive
4028 * dwell time to be 98% of the beacon interval (minus
4029 * 2 * channel tune time) */
4030 passive = priv->beacon_int;
4031 if ((passive > IWL_PASSIVE_DWELL_BASE) || !passive)
4032 passive = IWL_PASSIVE_DWELL_BASE;
4033 passive = (passive * 98) / 100 - IWL_CHANNEL_TUNE_TIME * 2;
4034 }
4035
Zhu Yib481de92007-09-25 17:54:57 -07004036 return passive;
4037}
4038
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08004039static int iwl3945_get_channels_for_scan(struct iwl_priv *priv,
Johannes Berg8318d782008-01-24 19:38:38 +01004040 enum ieee80211_band band,
Abhijeet Kolekarf9340522008-09-03 11:26:58 +08004041 u8 is_active, u8 n_probes,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004042 struct iwl3945_scan_channel *scan_ch)
Zhu Yib481de92007-09-25 17:54:57 -07004043{
4044 const struct ieee80211_channel *channels = NULL;
Johannes Berg8318d782008-01-24 19:38:38 +01004045 const struct ieee80211_supported_band *sband;
Samuel Ortizd20b3c62008-12-19 10:37:15 +08004046 const struct iwl_channel_info *ch_info;
Zhu Yib481de92007-09-25 17:54:57 -07004047 u16 passive_dwell = 0;
4048 u16 active_dwell = 0;
4049 int added, i;
4050
Kolekar, Abhijeetcbba18c2008-12-19 10:37:42 +08004051 sband = iwl_get_hw_mode(priv, band);
Johannes Berg8318d782008-01-24 19:38:38 +01004052 if (!sband)
Zhu Yib481de92007-09-25 17:54:57 -07004053 return 0;
4054
Johannes Berg8318d782008-01-24 19:38:38 +01004055 channels = sband->channels;
Zhu Yib481de92007-09-25 17:54:57 -07004056
Abhijeet Kolekarf9340522008-09-03 11:26:58 +08004057 active_dwell = iwl3945_get_active_dwell_time(priv, band, n_probes);
Johannes Berg8318d782008-01-24 19:38:38 +01004058 passive_dwell = iwl3945_get_passive_dwell_time(priv, band);
Zhu Yib481de92007-09-25 17:54:57 -07004059
Abhijeet Kolekar8f4807a2008-09-03 11:26:31 +08004060 if (passive_dwell <= active_dwell)
4061 passive_dwell = active_dwell + 1;
4062
Johannes Berg8318d782008-01-24 19:38:38 +01004063 for (i = 0, added = 0; i < sband->n_channels; i++) {
Johannes Berg182e2e62008-04-04 10:41:56 +02004064 if (channels[i].flags & IEEE80211_CHAN_DISABLED)
4065 continue;
4066
Johannes Berg8318d782008-01-24 19:38:38 +01004067 scan_ch->channel = channels[i].hw_value;
Zhu Yib481de92007-09-25 17:54:57 -07004068
Johannes Berg8318d782008-01-24 19:38:38 +01004069 ch_info = iwl3945_get_channel_info(priv, band, scan_ch->channel);
Zhu Yib481de92007-09-25 17:54:57 -07004070 if (!is_channel_valid(ch_info)) {
Ron Rindjunsky66b50042008-06-25 16:46:31 +08004071 IWL_DEBUG_SCAN("Channel %d is INVALID for this band.\n",
Zhu Yib481de92007-09-25 17:54:57 -07004072 scan_ch->channel);
4073 continue;
4074 }
4075
Zhu Yib481de92007-09-25 17:54:57 -07004076 scan_ch->active_dwell = cpu_to_le16(active_dwell);
4077 scan_ch->passive_dwell = cpu_to_le16(passive_dwell);
Abhijeet Kolekar011a0332008-12-02 12:14:07 -08004078 /* If passive , set up for auto-switch
4079 * and use long active_dwell time.
4080 */
4081 if (!is_active || is_channel_passive(ch_info) ||
4082 (channels[i].flags & IEEE80211_CHAN_PASSIVE_SCAN)) {
4083 scan_ch->type = 0; /* passive */
4084 if (IWL_UCODE_API(priv->ucode_ver) == 1)
4085 scan_ch->active_dwell = cpu_to_le16(passive_dwell - 1);
4086 } else {
4087 scan_ch->type = 1; /* active */
4088 }
4089
4090 /* Set direct probe bits. These may be used both for active
4091 * scan channels (probes gets sent right away),
4092 * or for passive channels (probes get se sent only after
4093 * hearing clear Rx packet).*/
4094 if (IWL_UCODE_API(priv->ucode_ver) >= 2) {
4095 if (n_probes)
4096 scan_ch->type |= IWL_SCAN_PROBE_MASK(n_probes);
4097 } else {
4098 /* uCode v1 does not allow setting direct probe bits on
4099 * passive channel. */
4100 if ((scan_ch->type & 1) && n_probes)
4101 scan_ch->type |= IWL_SCAN_PROBE_MASK(n_probes);
4102 }
Zhu Yib481de92007-09-25 17:54:57 -07004103
Ben Cahill9fbab512007-11-29 11:09:47 +08004104 /* Set txpower levels to defaults */
Zhu Yib481de92007-09-25 17:54:57 -07004105 scan_ch->tpc.dsp_atten = 110;
4106 /* scan_pwr_info->tpc.dsp_atten; */
4107
4108 /*scan_pwr_info->tpc.tx_gain; */
Johannes Berg8318d782008-01-24 19:38:38 +01004109 if (band == IEEE80211_BAND_5GHZ)
Zhu Yib481de92007-09-25 17:54:57 -07004110 scan_ch->tpc.tx_gain = ((1 << 5) | (3 << 3)) | 3;
4111 else {
4112 scan_ch->tpc.tx_gain = ((1 << 5) | (5 << 3));
4113 /* NOTE: if we were doing 6Mb OFDM for scans we'd use
Ben Cahill9fbab512007-11-29 11:09:47 +08004114 * power level:
Reinette Chatre8a1b0242008-01-14 17:46:25 -08004115 * scan_ch->tpc.tx_gain = ((1 << 5) | (2 << 3)) | 3;
Zhu Yib481de92007-09-25 17:54:57 -07004116 */
4117 }
4118
4119 IWL_DEBUG_SCAN("Scanning %d [%s %d]\n",
4120 scan_ch->channel,
4121 (scan_ch->type & 1) ? "ACTIVE" : "PASSIVE",
4122 (scan_ch->type & 1) ?
4123 active_dwell : passive_dwell);
4124
4125 scan_ch++;
4126 added++;
4127 }
4128
4129 IWL_DEBUG_SCAN("total channels to scan %d \n", added);
4130 return added;
4131}
4132
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08004133static void iwl3945_init_hw_rates(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07004134 struct ieee80211_rate *rates)
4135{
4136 int i;
4137
4138 for (i = 0; i < IWL_RATE_COUNT; i++) {
Johannes Berg8318d782008-01-24 19:38:38 +01004139 rates[i].bitrate = iwl3945_rates[i].ieee * 5;
4140 rates[i].hw_value = i; /* Rate scaling will work on indexes */
4141 rates[i].hw_value_short = i;
4142 rates[i].flags = 0;
Samuel Ortizd9829a62008-12-19 10:37:12 +08004143 if ((i > IWL39_LAST_OFDM_RATE) || (i < IWL_FIRST_OFDM_RATE)) {
Zhu Yib481de92007-09-25 17:54:57 -07004144 /*
Johannes Berg8318d782008-01-24 19:38:38 +01004145 * If CCK != 1M then set short preamble rate flag.
Zhu Yib481de92007-09-25 17:54:57 -07004146 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004147 rates[i].flags |= (iwl3945_rates[i].plcp == 10) ?
Johannes Berg8318d782008-01-24 19:38:38 +01004148 0 : IEEE80211_RATE_SHORT_PREAMBLE;
Zhu Yib481de92007-09-25 17:54:57 -07004149 }
Zhu Yib481de92007-09-25 17:54:57 -07004150 }
4151}
4152
4153/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004154 * iwl3945_init_geos - Initialize mac80211's geo/channel info based from eeprom
Zhu Yib481de92007-09-25 17:54:57 -07004155 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08004156static int iwl3945_init_geos(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004157{
Samuel Ortizd20b3c62008-12-19 10:37:15 +08004158 struct iwl_channel_info *ch;
Tomas Winkler8211ef72008-03-02 01:36:04 +02004159 struct ieee80211_supported_band *sband;
Zhu Yib481de92007-09-25 17:54:57 -07004160 struct ieee80211_channel *channels;
4161 struct ieee80211_channel *geo_ch;
4162 struct ieee80211_rate *rates;
4163 int i = 0;
Zhu Yib481de92007-09-25 17:54:57 -07004164
Johannes Berg8318d782008-01-24 19:38:38 +01004165 if (priv->bands[IEEE80211_BAND_2GHZ].n_bitrates ||
4166 priv->bands[IEEE80211_BAND_5GHZ].n_bitrates) {
Zhu Yib481de92007-09-25 17:54:57 -07004167 IWL_DEBUG_INFO("Geography modes already initialized.\n");
4168 set_bit(STATUS_GEO_CONFIGURED, &priv->status);
4169 return 0;
4170 }
4171
Zhu Yib481de92007-09-25 17:54:57 -07004172 channels = kzalloc(sizeof(struct ieee80211_channel) *
4173 priv->channel_count, GFP_KERNEL);
Johannes Berg8318d782008-01-24 19:38:38 +01004174 if (!channels)
Zhu Yib481de92007-09-25 17:54:57 -07004175 return -ENOMEM;
Zhu Yib481de92007-09-25 17:54:57 -07004176
Tomas Winkler8211ef72008-03-02 01:36:04 +02004177 rates = kzalloc((sizeof(struct ieee80211_rate) * (IWL_RATE_COUNT + 1)),
Zhu Yib481de92007-09-25 17:54:57 -07004178 GFP_KERNEL);
4179 if (!rates) {
Zhu Yib481de92007-09-25 17:54:57 -07004180 kfree(channels);
4181 return -ENOMEM;
4182 }
4183
Zhu Yib481de92007-09-25 17:54:57 -07004184 /* 5.2GHz channels start after the 2.4GHz channels */
Tomas Winkler8211ef72008-03-02 01:36:04 +02004185 sband = &priv->bands[IEEE80211_BAND_5GHZ];
4186 sband->channels = &channels[ARRAY_SIZE(iwl3945_eeprom_band_1)];
4187 /* just OFDM */
4188 sband->bitrates = &rates[IWL_FIRST_OFDM_RATE];
4189 sband->n_bitrates = IWL_RATE_COUNT - IWL_FIRST_OFDM_RATE;
Zhu Yib481de92007-09-25 17:54:57 -07004190
Tomas Winkler8211ef72008-03-02 01:36:04 +02004191 sband = &priv->bands[IEEE80211_BAND_2GHZ];
4192 sband->channels = channels;
4193 /* OFDM & CCK */
4194 sband->bitrates = rates;
4195 sband->n_bitrates = IWL_RATE_COUNT;
Zhu Yib481de92007-09-25 17:54:57 -07004196
4197 priv->ieee_channels = channels;
4198 priv->ieee_rates = rates;
4199
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004200 iwl3945_init_hw_rates(priv, rates);
Zhu Yib481de92007-09-25 17:54:57 -07004201
Tomas Winkler8211ef72008-03-02 01:36:04 +02004202 for (i = 0; i < priv->channel_count; i++) {
Zhu Yib481de92007-09-25 17:54:57 -07004203 ch = &priv->channel_info[i];
4204
Tomas Winkler8211ef72008-03-02 01:36:04 +02004205 /* FIXME: might be removed if scan is OK*/
4206 if (!is_channel_valid(ch))
Zhu Yib481de92007-09-25 17:54:57 -07004207 continue;
Zhu Yib481de92007-09-25 17:54:57 -07004208
4209 if (is_channel_a_band(ch))
Tomas Winkler8211ef72008-03-02 01:36:04 +02004210 sband = &priv->bands[IEEE80211_BAND_5GHZ];
Johannes Berg8318d782008-01-24 19:38:38 +01004211 else
Tomas Winkler8211ef72008-03-02 01:36:04 +02004212 sband = &priv->bands[IEEE80211_BAND_2GHZ];
Zhu Yib481de92007-09-25 17:54:57 -07004213
Tomas Winkler8211ef72008-03-02 01:36:04 +02004214 geo_ch = &sband->channels[sband->n_channels++];
4215
4216 geo_ch->center_freq = ieee80211_channel_to_frequency(ch->channel);
Johannes Berg8318d782008-01-24 19:38:38 +01004217 geo_ch->max_power = ch->max_power_avg;
4218 geo_ch->max_antenna_gain = 0xff;
Mohamed Abbas7b723042008-01-31 21:46:40 -08004219 geo_ch->hw_value = ch->channel;
Zhu Yib481de92007-09-25 17:54:57 -07004220
4221 if (is_channel_valid(ch)) {
Johannes Berg8318d782008-01-24 19:38:38 +01004222 if (!(ch->flags & EEPROM_CHANNEL_IBSS))
4223 geo_ch->flags |= IEEE80211_CHAN_NO_IBSS;
Zhu Yib481de92007-09-25 17:54:57 -07004224
Johannes Berg8318d782008-01-24 19:38:38 +01004225 if (!(ch->flags & EEPROM_CHANNEL_ACTIVE))
4226 geo_ch->flags |= IEEE80211_CHAN_PASSIVE_SCAN;
Zhu Yib481de92007-09-25 17:54:57 -07004227
4228 if (ch->flags & EEPROM_CHANNEL_RADAR)
Johannes Berg8318d782008-01-24 19:38:38 +01004229 geo_ch->flags |= IEEE80211_CHAN_RADAR;
Zhu Yib481de92007-09-25 17:54:57 -07004230
4231 if (ch->max_power_avg > priv->max_channel_txpower_limit)
4232 priv->max_channel_txpower_limit =
4233 ch->max_power_avg;
Tomas Winkler8211ef72008-03-02 01:36:04 +02004234 } else {
Johannes Berg8318d782008-01-24 19:38:38 +01004235 geo_ch->flags |= IEEE80211_CHAN_DISABLED;
Tomas Winkler8211ef72008-03-02 01:36:04 +02004236 }
4237
4238 /* Save flags for reg domain usage */
4239 geo_ch->orig_flags = geo_ch->flags;
4240
4241 IWL_DEBUG_INFO("Channel %d Freq=%d[%sGHz] %s flag=0%X\n",
4242 ch->channel, geo_ch->center_freq,
4243 is_channel_a_band(ch) ? "5.2" : "2.4",
4244 geo_ch->flags & IEEE80211_CHAN_DISABLED ?
4245 "restricted" : "valid",
4246 geo_ch->flags);
Zhu Yib481de92007-09-25 17:54:57 -07004247 }
4248
Tomas Winkler82b9a122008-03-04 18:09:30 -08004249 if ((priv->bands[IEEE80211_BAND_5GHZ].n_channels == 0) &&
4250 priv->cfg->sku & IWL_SKU_A) {
Tomas Winkler978785a2008-12-19 10:37:31 +08004251 IWL_INFO(priv, "Incorrectly detected BG card as ABG. "
4252 "Please send your PCI ID 0x%04X:0x%04X to maintainer.\n",
4253 priv->pci_dev->device, priv->pci_dev->subsystem_device);
Tomas Winkler82b9a122008-03-04 18:09:30 -08004254 priv->cfg->sku &= ~IWL_SKU_A;
Zhu Yib481de92007-09-25 17:54:57 -07004255 }
4256
Tomas Winkler978785a2008-12-19 10:37:31 +08004257 IWL_INFO(priv, "Tunable channels: %d 802.11bg, %d 802.11a channels\n",
Johannes Berg8318d782008-01-24 19:38:38 +01004258 priv->bands[IEEE80211_BAND_2GHZ].n_channels,
4259 priv->bands[IEEE80211_BAND_5GHZ].n_channels);
Zhu Yib481de92007-09-25 17:54:57 -07004260
John W. Linvillee0e0a672008-03-25 15:58:40 -04004261 if (priv->bands[IEEE80211_BAND_2GHZ].n_channels)
4262 priv->hw->wiphy->bands[IEEE80211_BAND_2GHZ] =
4263 &priv->bands[IEEE80211_BAND_2GHZ];
4264 if (priv->bands[IEEE80211_BAND_5GHZ].n_channels)
4265 priv->hw->wiphy->bands[IEEE80211_BAND_5GHZ] =
4266 &priv->bands[IEEE80211_BAND_5GHZ];
Zhu Yib481de92007-09-25 17:54:57 -07004267
Zhu Yib481de92007-09-25 17:54:57 -07004268 set_bit(STATUS_GEO_CONFIGURED, &priv->status);
4269
4270 return 0;
4271}
4272
Reinette Chatre849e0dc2008-01-23 10:15:18 -08004273/*
4274 * iwl3945_free_geos - undo allocations in iwl3945_init_geos
4275 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08004276static void iwl3945_free_geos(struct iwl_priv *priv)
Reinette Chatre849e0dc2008-01-23 10:15:18 -08004277{
Reinette Chatre849e0dc2008-01-23 10:15:18 -08004278 kfree(priv->ieee_channels);
4279 kfree(priv->ieee_rates);
4280 clear_bit(STATUS_GEO_CONFIGURED, &priv->status);
4281}
4282
Zhu Yib481de92007-09-25 17:54:57 -07004283/******************************************************************************
4284 *
4285 * uCode download functions
4286 *
4287 ******************************************************************************/
4288
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08004289static void iwl3945_dealloc_ucode_pci(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004290{
Tomas Winkler98c92212008-01-14 17:46:20 -08004291 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_code);
4292 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_data);
4293 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_data_backup);
4294 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_init);
4295 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_init_data);
4296 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_boot);
Zhu Yib481de92007-09-25 17:54:57 -07004297}
4298
4299/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004300 * iwl3945_verify_inst_full - verify runtime uCode image in card vs. host,
Zhu Yib481de92007-09-25 17:54:57 -07004301 * looking at all data.
4302 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08004303static int iwl3945_verify_inst_full(struct iwl_priv *priv, __le32 *image, u32 len)
Zhu Yib481de92007-09-25 17:54:57 -07004304{
4305 u32 val;
4306 u32 save_len = len;
4307 int rc = 0;
4308 u32 errcnt;
4309
4310 IWL_DEBUG_INFO("ucode inst image size is %u\n", len);
4311
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08004312 rc = iwl_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004313 if (rc)
4314 return rc;
4315
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08004316 iwl_write_direct32(priv, HBUS_TARG_MEM_RADDR,
Samuel Ortiz250bdd22008-12-19 10:37:11 +08004317 IWL39_RTC_INST_LOWER_BOUND);
Zhu Yib481de92007-09-25 17:54:57 -07004318
4319 errcnt = 0;
4320 for (; len > 0; len -= sizeof(u32), image++) {
4321 /* read data comes through single port, auto-incr addr */
4322 /* NOTE: Use the debugless read so we don't flood kernel log
4323 * if IWL_DL_IO is set */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08004324 val = _iwl_read_direct32(priv, HBUS_TARG_MEM_RDAT);
Zhu Yib481de92007-09-25 17:54:57 -07004325 if (val != le32_to_cpu(*image)) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08004326 IWL_ERR(priv, "uCode INST section is invalid at "
Zhu Yib481de92007-09-25 17:54:57 -07004327 "offset 0x%x, is 0x%x, s/b 0x%x\n",
4328 save_len - len, val, le32_to_cpu(*image));
4329 rc = -EIO;
4330 errcnt++;
4331 if (errcnt >= 20)
4332 break;
4333 }
4334 }
4335
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08004336 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004337
4338 if (!errcnt)
Ian Schrambc434dd2007-10-25 17:15:29 +08004339 IWL_DEBUG_INFO("ucode image in INSTRUCTION memory is good\n");
Zhu Yib481de92007-09-25 17:54:57 -07004340
4341 return rc;
4342}
4343
4344
4345/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004346 * iwl3945_verify_inst_sparse - verify runtime uCode image in card vs. host,
Zhu Yib481de92007-09-25 17:54:57 -07004347 * using sample data 100 bytes apart. If these sample points are good,
4348 * it's a pretty good bet that everything between them is good, too.
4349 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08004350static int iwl3945_verify_inst_sparse(struct iwl_priv *priv, __le32 *image, u32 len)
Zhu Yib481de92007-09-25 17:54:57 -07004351{
4352 u32 val;
4353 int rc = 0;
4354 u32 errcnt = 0;
4355 u32 i;
4356
4357 IWL_DEBUG_INFO("ucode inst image size is %u\n", len);
4358
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08004359 rc = iwl_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004360 if (rc)
4361 return rc;
4362
4363 for (i = 0; i < len; i += 100, image += 100/sizeof(u32)) {
4364 /* read data comes through single port, auto-incr addr */
4365 /* NOTE: Use the debugless read so we don't flood kernel log
4366 * if IWL_DL_IO is set */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08004367 iwl_write_direct32(priv, HBUS_TARG_MEM_RADDR,
Samuel Ortiz250bdd22008-12-19 10:37:11 +08004368 i + IWL39_RTC_INST_LOWER_BOUND);
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08004369 val = _iwl_read_direct32(priv, HBUS_TARG_MEM_RDAT);
Zhu Yib481de92007-09-25 17:54:57 -07004370 if (val != le32_to_cpu(*image)) {
4371#if 0 /* Enable this if you want to see details */
Winkler, Tomas15b16872008-12-19 10:37:33 +08004372 IWL_ERR(priv, "uCode INST section is invalid at "
Zhu Yib481de92007-09-25 17:54:57 -07004373 "offset 0x%x, is 0x%x, s/b 0x%x\n",
4374 i, val, *image);
4375#endif
4376 rc = -EIO;
4377 errcnt++;
4378 if (errcnt >= 3)
4379 break;
4380 }
4381 }
4382
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08004383 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004384
4385 return rc;
4386}
4387
4388
4389/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004390 * iwl3945_verify_ucode - determine which instruction image is in SRAM,
Zhu Yib481de92007-09-25 17:54:57 -07004391 * and verify its contents
4392 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08004393static int iwl3945_verify_ucode(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004394{
4395 __le32 *image;
4396 u32 len;
4397 int rc = 0;
4398
4399 /* Try bootstrap */
4400 image = (__le32 *)priv->ucode_boot.v_addr;
4401 len = priv->ucode_boot.len;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004402 rc = iwl3945_verify_inst_sparse(priv, image, len);
Zhu Yib481de92007-09-25 17:54:57 -07004403 if (rc == 0) {
4404 IWL_DEBUG_INFO("Bootstrap uCode is good in inst SRAM\n");
4405 return 0;
4406 }
4407
4408 /* Try initialize */
4409 image = (__le32 *)priv->ucode_init.v_addr;
4410 len = priv->ucode_init.len;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004411 rc = iwl3945_verify_inst_sparse(priv, image, len);
Zhu Yib481de92007-09-25 17:54:57 -07004412 if (rc == 0) {
4413 IWL_DEBUG_INFO("Initialize uCode is good in inst SRAM\n");
4414 return 0;
4415 }
4416
4417 /* Try runtime/protocol */
4418 image = (__le32 *)priv->ucode_code.v_addr;
4419 len = priv->ucode_code.len;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004420 rc = iwl3945_verify_inst_sparse(priv, image, len);
Zhu Yib481de92007-09-25 17:54:57 -07004421 if (rc == 0) {
4422 IWL_DEBUG_INFO("Runtime uCode is good in inst SRAM\n");
4423 return 0;
4424 }
4425
Winkler, Tomas15b16872008-12-19 10:37:33 +08004426 IWL_ERR(priv, "NO VALID UCODE IMAGE IN INSTRUCTION SRAM!!\n");
Zhu Yib481de92007-09-25 17:54:57 -07004427
Ben Cahill9fbab512007-11-29 11:09:47 +08004428 /* Since nothing seems to match, show first several data entries in
4429 * instruction SRAM, so maybe visual inspection will give a clue.
4430 * Selection of bootstrap image (vs. other images) is arbitrary. */
Zhu Yib481de92007-09-25 17:54:57 -07004431 image = (__le32 *)priv->ucode_boot.v_addr;
4432 len = priv->ucode_boot.len;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004433 rc = iwl3945_verify_inst_full(priv, image, len);
Zhu Yib481de92007-09-25 17:54:57 -07004434
4435 return rc;
4436}
4437
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08004438static void iwl3945_nic_start(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004439{
4440 /* Remove all resets to allow NIC to operate */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08004441 iwl_write32(priv, CSR_RESET, 0);
Zhu Yib481de92007-09-25 17:54:57 -07004442}
4443
4444/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004445 * iwl3945_read_ucode - Read uCode images from disk file.
Zhu Yib481de92007-09-25 17:54:57 -07004446 *
4447 * Copy into buffers for card to fetch via bus-mastering
4448 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08004449static int iwl3945_read_ucode(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004450{
Kolekar, Abhijeeta78fe752008-12-19 10:37:21 +08004451 struct iwl_ucode *ucode;
Reinette Chatrea0987a82008-12-02 12:14:06 -08004452 int ret = -EINVAL, index;
Zhu Yib481de92007-09-25 17:54:57 -07004453 const struct firmware *ucode_raw;
4454 /* firmware file name contains uCode/driver compatibility version */
Reinette Chatrea0987a82008-12-02 12:14:06 -08004455 const char *name_pre = priv->cfg->fw_name_pre;
4456 const unsigned int api_max = priv->cfg->ucode_api_max;
4457 const unsigned int api_min = priv->cfg->ucode_api_min;
4458 char buf[25];
Zhu Yib481de92007-09-25 17:54:57 -07004459 u8 *src;
4460 size_t len;
Reinette Chatrea0987a82008-12-02 12:14:06 -08004461 u32 api_ver, inst_size, data_size, init_size, init_data_size, boot_size;
Zhu Yib481de92007-09-25 17:54:57 -07004462
4463 /* Ask kernel firmware_class module to get the boot firmware off disk.
4464 * request_firmware() is synchronous, file is in memory on return. */
Reinette Chatrea0987a82008-12-02 12:14:06 -08004465 for (index = api_max; index >= api_min; index--) {
4466 sprintf(buf, "%s%u%s", name_pre, index, ".ucode");
4467 ret = request_firmware(&ucode_raw, buf, &priv->pci_dev->dev);
4468 if (ret < 0) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08004469 IWL_ERR(priv, "%s firmware file req failed: %d\n",
Reinette Chatrea0987a82008-12-02 12:14:06 -08004470 buf, ret);
4471 if (ret == -ENOENT)
4472 continue;
4473 else
4474 goto error;
4475 } else {
4476 if (index < api_max)
Winkler, Tomas15b16872008-12-19 10:37:33 +08004477 IWL_ERR(priv, "Loaded firmware %s, "
4478 "which is deprecated. "
4479 " Please use API v%u instead.\n",
Reinette Chatrea0987a82008-12-02 12:14:06 -08004480 buf, api_max);
4481 IWL_DEBUG_INFO("Got firmware '%s' file (%zd bytes) from disk\n",
4482 buf, ucode_raw->size);
4483 break;
4484 }
Zhu Yib481de92007-09-25 17:54:57 -07004485 }
4486
Reinette Chatrea0987a82008-12-02 12:14:06 -08004487 if (ret < 0)
4488 goto error;
Zhu Yib481de92007-09-25 17:54:57 -07004489
4490 /* Make sure that we got at least our header! */
4491 if (ucode_raw->size < sizeof(*ucode)) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08004492 IWL_ERR(priv, "File size way too small!\n");
Tomas Winkler90e759d2007-11-29 11:09:41 +08004493 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07004494 goto err_release;
4495 }
4496
4497 /* Data from ucode file: header followed by uCode images */
4498 ucode = (void *)ucode_raw->data;
4499
Chatre, Reinettec02b3ac2008-12-02 12:14:05 -08004500 priv->ucode_ver = le32_to_cpu(ucode->ver);
Reinette Chatrea0987a82008-12-02 12:14:06 -08004501 api_ver = IWL_UCODE_API(priv->ucode_ver);
Zhu Yib481de92007-09-25 17:54:57 -07004502 inst_size = le32_to_cpu(ucode->inst_size);
4503 data_size = le32_to_cpu(ucode->data_size);
4504 init_size = le32_to_cpu(ucode->init_size);
4505 init_data_size = le32_to_cpu(ucode->init_data_size);
4506 boot_size = le32_to_cpu(ucode->boot_size);
4507
Reinette Chatrea0987a82008-12-02 12:14:06 -08004508 /* api_ver should match the api version forming part of the
4509 * firmware filename ... but we don't check for that and only rely
4510 * on the API version read from firware header from here on forward */
4511
4512 if (api_ver < api_min || api_ver > api_max) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08004513 IWL_ERR(priv, "Driver unable to support your firmware API. "
Reinette Chatrea0987a82008-12-02 12:14:06 -08004514 "Driver supports v%u, firmware is v%u.\n",
4515 api_max, api_ver);
4516 priv->ucode_ver = 0;
4517 ret = -EINVAL;
4518 goto err_release;
4519 }
4520 if (api_ver != api_max)
Winkler, Tomas15b16872008-12-19 10:37:33 +08004521 IWL_ERR(priv, "Firmware has old API version. Expected %u, "
Reinette Chatrea0987a82008-12-02 12:14:06 -08004522 "got %u. New firmware can be obtained "
4523 "from http://www.intellinuxwireless.org.\n",
4524 api_max, api_ver);
4525
Tomas Winkler978785a2008-12-19 10:37:31 +08004526 IWL_INFO(priv, "loaded firmware version %u.%u.%u.%u\n",
4527 IWL_UCODE_MAJOR(priv->ucode_ver),
4528 IWL_UCODE_MINOR(priv->ucode_ver),
4529 IWL_UCODE_API(priv->ucode_ver),
4530 IWL_UCODE_SERIAL(priv->ucode_ver));
4531
Reinette Chatrea0987a82008-12-02 12:14:06 -08004532 IWL_DEBUG_INFO("f/w package hdr ucode version raw = 0x%x\n",
4533 priv->ucode_ver);
Ian Schrambc434dd2007-10-25 17:15:29 +08004534 IWL_DEBUG_INFO("f/w package hdr runtime inst size = %u\n", inst_size);
4535 IWL_DEBUG_INFO("f/w package hdr runtime data size = %u\n", data_size);
4536 IWL_DEBUG_INFO("f/w package hdr init inst size = %u\n", init_size);
4537 IWL_DEBUG_INFO("f/w package hdr init data size = %u\n", init_data_size);
4538 IWL_DEBUG_INFO("f/w package hdr boot inst size = %u\n", boot_size);
Zhu Yib481de92007-09-25 17:54:57 -07004539
Reinette Chatrea0987a82008-12-02 12:14:06 -08004540
Zhu Yib481de92007-09-25 17:54:57 -07004541 /* Verify size of file vs. image size info in file's header */
4542 if (ucode_raw->size < sizeof(*ucode) +
4543 inst_size + data_size + init_size +
4544 init_data_size + boot_size) {
4545
4546 IWL_DEBUG_INFO("uCode file size %d too small\n",
4547 (int)ucode_raw->size);
Tomas Winkler90e759d2007-11-29 11:09:41 +08004548 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07004549 goto err_release;
4550 }
4551
4552 /* Verify that uCode images will fit in card's SRAM */
Samuel Ortiz250bdd22008-12-19 10:37:11 +08004553 if (inst_size > IWL39_MAX_INST_SIZE) {
Tomas Winkler90e759d2007-11-29 11:09:41 +08004554 IWL_DEBUG_INFO("uCode instr len %d too large to fit in\n",
4555 inst_size);
4556 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07004557 goto err_release;
4558 }
4559
Samuel Ortiz250bdd22008-12-19 10:37:11 +08004560 if (data_size > IWL39_MAX_DATA_SIZE) {
Tomas Winkler90e759d2007-11-29 11:09:41 +08004561 IWL_DEBUG_INFO("uCode data len %d too large to fit in\n",
4562 data_size);
4563 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07004564 goto err_release;
4565 }
Samuel Ortiz250bdd22008-12-19 10:37:11 +08004566 if (init_size > IWL39_MAX_INST_SIZE) {
Tomas Winkler90e759d2007-11-29 11:09:41 +08004567 IWL_DEBUG_INFO("uCode init instr len %d too large to fit in\n",
4568 init_size);
4569 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07004570 goto err_release;
4571 }
Samuel Ortiz250bdd22008-12-19 10:37:11 +08004572 if (init_data_size > IWL39_MAX_DATA_SIZE) {
Tomas Winkler90e759d2007-11-29 11:09:41 +08004573 IWL_DEBUG_INFO("uCode init data len %d too large to fit in\n",
4574 init_data_size);
4575 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07004576 goto err_release;
4577 }
Samuel Ortiz250bdd22008-12-19 10:37:11 +08004578 if (boot_size > IWL39_MAX_BSM_SIZE) {
Tomas Winkler90e759d2007-11-29 11:09:41 +08004579 IWL_DEBUG_INFO("uCode boot instr len %d too large to fit in\n",
4580 boot_size);
4581 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07004582 goto err_release;
4583 }
4584
4585 /* Allocate ucode buffers for card's bus-master loading ... */
4586
4587 /* Runtime instructions and 2 copies of data:
4588 * 1) unmodified from disk
4589 * 2) backup cache for save/restore during power-downs */
4590 priv->ucode_code.len = inst_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08004591 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_code);
Zhu Yib481de92007-09-25 17:54:57 -07004592
4593 priv->ucode_data.len = data_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08004594 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_data);
Zhu Yib481de92007-09-25 17:54:57 -07004595
4596 priv->ucode_data_backup.len = data_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08004597 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_data_backup);
Zhu Yib481de92007-09-25 17:54:57 -07004598
4599 if (!priv->ucode_code.v_addr || !priv->ucode_data.v_addr ||
Tomas Winkler90e759d2007-11-29 11:09:41 +08004600 !priv->ucode_data_backup.v_addr)
Zhu Yib481de92007-09-25 17:54:57 -07004601 goto err_pci_alloc;
4602
Tomas Winkler90e759d2007-11-29 11:09:41 +08004603 /* Initialization instructions and data */
4604 if (init_size && init_data_size) {
4605 priv->ucode_init.len = init_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08004606 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_init);
Tomas Winkler90e759d2007-11-29 11:09:41 +08004607
4608 priv->ucode_init_data.len = init_data_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08004609 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_init_data);
Tomas Winkler90e759d2007-11-29 11:09:41 +08004610
4611 if (!priv->ucode_init.v_addr || !priv->ucode_init_data.v_addr)
4612 goto err_pci_alloc;
4613 }
4614
4615 /* Bootstrap (instructions only, no data) */
4616 if (boot_size) {
4617 priv->ucode_boot.len = boot_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08004618 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_boot);
Tomas Winkler90e759d2007-11-29 11:09:41 +08004619
4620 if (!priv->ucode_boot.v_addr)
4621 goto err_pci_alloc;
4622 }
4623
Zhu Yib481de92007-09-25 17:54:57 -07004624 /* Copy images into buffers for card's bus-master reads ... */
4625
4626 /* Runtime instructions (first block of data in file) */
4627 src = &ucode->data[0];
4628 len = priv->ucode_code.len;
Tomas Winkler90e759d2007-11-29 11:09:41 +08004629 IWL_DEBUG_INFO("Copying (but not loading) uCode instr len %Zd\n", len);
Zhu Yib481de92007-09-25 17:54:57 -07004630 memcpy(priv->ucode_code.v_addr, src, len);
4631 IWL_DEBUG_INFO("uCode instr buf vaddr = 0x%p, paddr = 0x%08x\n",
4632 priv->ucode_code.v_addr, (u32)priv->ucode_code.p_addr);
4633
4634 /* Runtime data (2nd block)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004635 * NOTE: Copy into backup buffer will be done in iwl3945_up() */
Zhu Yib481de92007-09-25 17:54:57 -07004636 src = &ucode->data[inst_size];
4637 len = priv->ucode_data.len;
Tomas Winkler90e759d2007-11-29 11:09:41 +08004638 IWL_DEBUG_INFO("Copying (but not loading) uCode data len %Zd\n", len);
Zhu Yib481de92007-09-25 17:54:57 -07004639 memcpy(priv->ucode_data.v_addr, src, len);
4640 memcpy(priv->ucode_data_backup.v_addr, src, len);
4641
4642 /* Initialization instructions (3rd block) */
4643 if (init_size) {
4644 src = &ucode->data[inst_size + data_size];
4645 len = priv->ucode_init.len;
Tomas Winkler90e759d2007-11-29 11:09:41 +08004646 IWL_DEBUG_INFO("Copying (but not loading) init instr len %Zd\n",
4647 len);
Zhu Yib481de92007-09-25 17:54:57 -07004648 memcpy(priv->ucode_init.v_addr, src, len);
4649 }
4650
4651 /* Initialization data (4th block) */
4652 if (init_data_size) {
4653 src = &ucode->data[inst_size + data_size + init_size];
4654 len = priv->ucode_init_data.len;
4655 IWL_DEBUG_INFO("Copying (but not loading) init data len %d\n",
4656 (int)len);
4657 memcpy(priv->ucode_init_data.v_addr, src, len);
4658 }
4659
4660 /* Bootstrap instructions (5th block) */
4661 src = &ucode->data[inst_size + data_size + init_size + init_data_size];
4662 len = priv->ucode_boot.len;
4663 IWL_DEBUG_INFO("Copying (but not loading) boot instr len %d\n",
4664 (int)len);
4665 memcpy(priv->ucode_boot.v_addr, src, len);
4666
4667 /* We have our copies now, allow OS release its copies */
4668 release_firmware(ucode_raw);
4669 return 0;
4670
4671 err_pci_alloc:
Winkler, Tomas15b16872008-12-19 10:37:33 +08004672 IWL_ERR(priv, "failed to allocate pci memory\n");
Tomas Winkler90e759d2007-11-29 11:09:41 +08004673 ret = -ENOMEM;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004674 iwl3945_dealloc_ucode_pci(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004675
4676 err_release:
4677 release_firmware(ucode_raw);
4678
4679 error:
Tomas Winkler90e759d2007-11-29 11:09:41 +08004680 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07004681}
4682
4683
4684/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004685 * iwl3945_set_ucode_ptrs - Set uCode address location
Zhu Yib481de92007-09-25 17:54:57 -07004686 *
4687 * Tell initialization uCode where to find runtime uCode.
4688 *
4689 * BSM registers initially contain pointers to initialization uCode.
4690 * We need to replace them to load runtime uCode inst and data,
4691 * and to save runtime data when powering down.
4692 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08004693static int iwl3945_set_ucode_ptrs(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004694{
4695 dma_addr_t pinst;
4696 dma_addr_t pdata;
4697 int rc = 0;
4698 unsigned long flags;
4699
4700 /* bits 31:0 for 3945 */
4701 pinst = priv->ucode_code.p_addr;
4702 pdata = priv->ucode_data_backup.p_addr;
4703
4704 spin_lock_irqsave(&priv->lock, flags);
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08004705 rc = iwl_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004706 if (rc) {
4707 spin_unlock_irqrestore(&priv->lock, flags);
4708 return rc;
4709 }
4710
4711 /* Tell bootstrap uCode where to find image to load */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08004712 iwl_write_prph(priv, BSM_DRAM_INST_PTR_REG, pinst);
4713 iwl_write_prph(priv, BSM_DRAM_DATA_PTR_REG, pdata);
4714 iwl_write_prph(priv, BSM_DRAM_DATA_BYTECOUNT_REG,
Zhu Yib481de92007-09-25 17:54:57 -07004715 priv->ucode_data.len);
4716
Tomas Winklera96a27f2008-10-23 23:48:56 -07004717 /* Inst byte count must be last to set up, bit 31 signals uCode
Zhu Yib481de92007-09-25 17:54:57 -07004718 * that all new ptr/size info is in place */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08004719 iwl_write_prph(priv, BSM_DRAM_INST_BYTECOUNT_REG,
Zhu Yib481de92007-09-25 17:54:57 -07004720 priv->ucode_code.len | BSM_DRAM_INST_LOAD);
4721
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08004722 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004723
4724 spin_unlock_irqrestore(&priv->lock, flags);
4725
4726 IWL_DEBUG_INFO("Runtime uCode pointers are set.\n");
4727
4728 return rc;
4729}
4730
4731/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004732 * iwl3945_init_alive_start - Called after REPLY_ALIVE notification received
Zhu Yib481de92007-09-25 17:54:57 -07004733 *
4734 * Called after REPLY_ALIVE notification received from "initialize" uCode.
4735 *
Zhu Yib481de92007-09-25 17:54:57 -07004736 * Tell "initialize" uCode to go ahead and load the runtime uCode.
Ben Cahill9fbab512007-11-29 11:09:47 +08004737 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08004738static void iwl3945_init_alive_start(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004739{
4740 /* Check alive response for "valid" sign from uCode */
4741 if (priv->card_alive_init.is_valid != UCODE_VALID_OK) {
4742 /* We had an error bringing up the hardware, so take it
4743 * all the way back down so we can try again */
4744 IWL_DEBUG_INFO("Initialize Alive failed.\n");
4745 goto restart;
4746 }
4747
4748 /* Bootstrap uCode has loaded initialize uCode ... verify inst image.
4749 * This is a paranoid check, because we would not have gotten the
4750 * "initialize" alive if code weren't properly loaded. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004751 if (iwl3945_verify_ucode(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07004752 /* Runtime instruction load was bad;
4753 * take it all the way back down so we can try again */
4754 IWL_DEBUG_INFO("Bad \"initialize\" uCode load.\n");
4755 goto restart;
4756 }
4757
4758 /* Send pointers to protocol/runtime uCode image ... init code will
4759 * load and launch runtime uCode, which will send us another "Alive"
4760 * notification. */
4761 IWL_DEBUG_INFO("Initialization Alive received.\n");
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004762 if (iwl3945_set_ucode_ptrs(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07004763 /* Runtime instruction load won't happen;
4764 * take it all the way back down so we can try again */
4765 IWL_DEBUG_INFO("Couldn't set up uCode pointers.\n");
4766 goto restart;
4767 }
4768 return;
4769
4770 restart:
4771 queue_work(priv->workqueue, &priv->restart);
4772}
4773
4774
Mohamed Abbas9bdf5ec2008-11-07 09:58:35 -08004775/* temporary */
4776static int iwl3945_mac_beacon_update(struct ieee80211_hw *hw,
4777 struct sk_buff *skb);
4778
Zhu Yib481de92007-09-25 17:54:57 -07004779/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004780 * iwl3945_alive_start - called after REPLY_ALIVE notification received
Zhu Yib481de92007-09-25 17:54:57 -07004781 * from protocol/runtime uCode (initialization uCode's
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004782 * Alive gets handled by iwl3945_init_alive_start()).
Zhu Yib481de92007-09-25 17:54:57 -07004783 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08004784static void iwl3945_alive_start(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004785{
4786 int rc = 0;
4787 int thermal_spin = 0;
4788 u32 rfkill;
4789
4790 IWL_DEBUG_INFO("Runtime Alive received.\n");
4791
4792 if (priv->card_alive.is_valid != UCODE_VALID_OK) {
4793 /* We had an error bringing up the hardware, so take it
4794 * all the way back down so we can try again */
4795 IWL_DEBUG_INFO("Alive failed.\n");
4796 goto restart;
4797 }
4798
4799 /* Initialize uCode has loaded Runtime uCode ... verify inst image.
4800 * This is a paranoid check, because we would not have gotten the
4801 * "runtime" alive if code weren't properly loaded. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004802 if (iwl3945_verify_ucode(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07004803 /* Runtime instruction load was bad;
4804 * take it all the way back down so we can try again */
4805 IWL_DEBUG_INFO("Bad runtime uCode load.\n");
4806 goto restart;
4807 }
4808
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004809 iwl3945_clear_stations_table(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004810
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08004811 rc = iwl_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004812 if (rc) {
Winkler, Tomas39aadf82008-12-19 10:37:32 +08004813 IWL_WARN(priv, "Can not read RFKILL status from adapter\n");
Zhu Yib481de92007-09-25 17:54:57 -07004814 return;
4815 }
4816
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08004817 rfkill = iwl_read_prph(priv, APMG_RFKILL_REG);
Zhu Yib481de92007-09-25 17:54:57 -07004818 IWL_DEBUG_INFO("RFKILL status: 0x%x\n", rfkill);
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08004819 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004820
4821 if (rfkill & 0x1) {
4822 clear_bit(STATUS_RF_KILL_HW, &priv->status);
Tomas Winklera96a27f2008-10-23 23:48:56 -07004823 /* if RFKILL is not on, then wait for thermal
Zhu Yib481de92007-09-25 17:54:57 -07004824 * sensor in adapter to kick in */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004825 while (iwl3945_hw_get_temperature(priv) == 0) {
Zhu Yib481de92007-09-25 17:54:57 -07004826 thermal_spin++;
4827 udelay(10);
4828 }
4829
4830 if (thermal_spin)
4831 IWL_DEBUG_INFO("Thermal calibration took %dus\n",
4832 thermal_spin * 10);
4833 } else
4834 set_bit(STATUS_RF_KILL_HW, &priv->status);
4835
Ben Cahill9fbab512007-11-29 11:09:47 +08004836 /* After the ALIVE response, we can send commands to 3945 uCode */
Zhu Yib481de92007-09-25 17:54:57 -07004837 set_bit(STATUS_ALIVE, &priv->status);
4838
4839 /* Clear out the uCode error bit if it is set */
4840 clear_bit(STATUS_FW_ERROR, &priv->status);
4841
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08004842 if (iwl_is_rfkill(priv))
Zhu Yib481de92007-09-25 17:54:57 -07004843 return;
4844
Johannes Berg36d68252008-05-15 12:55:26 +02004845 ieee80211_wake_queues(priv->hw);
Zhu Yib481de92007-09-25 17:54:57 -07004846
4847 priv->active_rate = priv->rates_mask;
4848 priv->active_rate_basic = priv->rates_mask & IWL_BASIC_RATES_MASK;
4849
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004850 iwl3945_send_power_mode(priv, IWL_POWER_LEVEL(priv->power_mode));
Zhu Yib481de92007-09-25 17:54:57 -07004851
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004852 if (iwl3945_is_associated(priv)) {
4853 struct iwl3945_rxon_cmd *active_rxon =
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08004854 (struct iwl3945_rxon_cmd *)(&priv->active39_rxon);
Zhu Yib481de92007-09-25 17:54:57 -07004855
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08004856 memcpy(&priv->staging39_rxon, &priv->active39_rxon,
4857 sizeof(priv->staging39_rxon));
Zhu Yib481de92007-09-25 17:54:57 -07004858 active_rxon->filter_flags &= ~RXON_FILTER_ASSOC_MSK;
4859 } else {
4860 /* Initialize our rx_config data */
Zhu, Yi60294de2008-10-29 14:05:45 -07004861 iwl3945_connection_init_rx_config(priv, priv->iw_mode);
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08004862 memcpy(priv->staging39_rxon.node_addr, priv->mac_addr, ETH_ALEN);
Zhu Yib481de92007-09-25 17:54:57 -07004863 }
4864
Ben Cahill9fbab512007-11-29 11:09:47 +08004865 /* Configure Bluetooth device coexistence support */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004866 iwl3945_send_bt_config(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004867
4868 /* Configure the adapter for unassociated operation */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004869 iwl3945_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004870
Zhu Yib481de92007-09-25 17:54:57 -07004871 iwl3945_reg_txpower_periodic(priv);
4872
Reinette Chatrefe00b5a2008-04-03 16:05:23 -07004873 iwl3945_led_register(priv);
4874
Zhu Yib481de92007-09-25 17:54:57 -07004875 IWL_DEBUG_INFO("ALIVE processing complete.\n");
Rick Farringtona9f46782008-03-18 14:57:49 -07004876 set_bit(STATUS_READY, &priv->status);
Zhu Yi5a669262008-01-14 17:46:18 -08004877 wake_up_interruptible(&priv->wait_command_queue);
Zhu Yib481de92007-09-25 17:54:57 -07004878
4879 if (priv->error_recovering)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004880 iwl3945_error_recovery(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004881
Mohamed Abbas9bdf5ec2008-11-07 09:58:35 -08004882 /* reassociate for ADHOC mode */
4883 if (priv->vif && (priv->iw_mode == NL80211_IFTYPE_ADHOC)) {
4884 struct sk_buff *beacon = ieee80211_beacon_get(priv->hw,
4885 priv->vif);
4886 if (beacon)
4887 iwl3945_mac_beacon_update(priv->hw, beacon);
4888 }
4889
Zhu Yib481de92007-09-25 17:54:57 -07004890 return;
4891
4892 restart:
4893 queue_work(priv->workqueue, &priv->restart);
4894}
4895
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08004896static void iwl3945_cancel_deferred_work(struct iwl_priv *priv);
Zhu Yib481de92007-09-25 17:54:57 -07004897
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08004898static void __iwl3945_down(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004899{
4900 unsigned long flags;
4901 int exit_pending = test_bit(STATUS_EXIT_PENDING, &priv->status);
4902 struct ieee80211_conf *conf = NULL;
4903
4904 IWL_DEBUG_INFO(DRV_NAME " is going down\n");
4905
4906 conf = ieee80211_get_hw_conf(priv->hw);
4907
4908 if (!exit_pending)
4909 set_bit(STATUS_EXIT_PENDING, &priv->status);
4910
Mohamed Abbasab53d8a2008-03-25 16:33:36 -07004911 iwl3945_led_unregister(priv);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004912 iwl3945_clear_stations_table(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004913
4914 /* Unblock any waiting calls */
4915 wake_up_interruptible_all(&priv->wait_command_queue);
4916
Zhu Yib481de92007-09-25 17:54:57 -07004917 /* Wipe out the EXIT_PENDING status bit if we are not actually
4918 * exiting the module */
4919 if (!exit_pending)
4920 clear_bit(STATUS_EXIT_PENDING, &priv->status);
4921
4922 /* stop and reset the on-board processor */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08004923 iwl_write32(priv, CSR_RESET, CSR_RESET_REG_FLAG_NEVO_RESET);
Zhu Yib481de92007-09-25 17:54:57 -07004924
4925 /* tell the device to stop sending interrupts */
Mohamed Abbas0359fac2008-03-28 16:21:08 -07004926 spin_lock_irqsave(&priv->lock, flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004927 iwl3945_disable_interrupts(priv);
Mohamed Abbas0359fac2008-03-28 16:21:08 -07004928 spin_unlock_irqrestore(&priv->lock, flags);
4929 iwl_synchronize_irq(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004930
4931 if (priv->mac80211_registered)
4932 ieee80211_stop_queues(priv->hw);
4933
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004934 /* If we have not previously called iwl3945_init() then
Zhu Yib481de92007-09-25 17:54:57 -07004935 * clear all bits but the RF Kill and SUSPEND bits and return */
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08004936 if (!iwl_is_init(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07004937 priv->status = test_bit(STATUS_RF_KILL_HW, &priv->status) <<
4938 STATUS_RF_KILL_HW |
4939 test_bit(STATUS_RF_KILL_SW, &priv->status) <<
4940 STATUS_RF_KILL_SW |
Reinette Chatre97888642008-02-06 11:20:38 -08004941 test_bit(STATUS_GEO_CONFIGURED, &priv->status) <<
4942 STATUS_GEO_CONFIGURED |
Zhu Yib481de92007-09-25 17:54:57 -07004943 test_bit(STATUS_IN_SUSPEND, &priv->status) <<
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08004944 STATUS_IN_SUSPEND |
4945 test_bit(STATUS_EXIT_PENDING, &priv->status) <<
4946 STATUS_EXIT_PENDING;
Zhu Yib481de92007-09-25 17:54:57 -07004947 goto exit;
4948 }
4949
4950 /* ...otherwise clear out all the status bits but the RF Kill and
4951 * SUSPEND bits and continue taking the NIC down. */
4952 priv->status &= test_bit(STATUS_RF_KILL_HW, &priv->status) <<
4953 STATUS_RF_KILL_HW |
4954 test_bit(STATUS_RF_KILL_SW, &priv->status) <<
4955 STATUS_RF_KILL_SW |
Reinette Chatre97888642008-02-06 11:20:38 -08004956 test_bit(STATUS_GEO_CONFIGURED, &priv->status) <<
4957 STATUS_GEO_CONFIGURED |
Zhu Yib481de92007-09-25 17:54:57 -07004958 test_bit(STATUS_IN_SUSPEND, &priv->status) <<
4959 STATUS_IN_SUSPEND |
4960 test_bit(STATUS_FW_ERROR, &priv->status) <<
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08004961 STATUS_FW_ERROR |
4962 test_bit(STATUS_EXIT_PENDING, &priv->status) <<
4963 STATUS_EXIT_PENDING;
Zhu Yib481de92007-09-25 17:54:57 -07004964
4965 spin_lock_irqsave(&priv->lock, flags);
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08004966 iwl_clear_bit(priv, CSR_GP_CNTRL, CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
Zhu Yib481de92007-09-25 17:54:57 -07004967 spin_unlock_irqrestore(&priv->lock, flags);
4968
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004969 iwl3945_hw_txq_ctx_stop(priv);
4970 iwl3945_hw_rxq_stop(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004971
4972 spin_lock_irqsave(&priv->lock, flags);
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08004973 if (!iwl_grab_nic_access(priv)) {
4974 iwl_write_prph(priv, APMG_CLK_DIS_REG,
Zhu Yib481de92007-09-25 17:54:57 -07004975 APMG_CLK_VAL_DMA_CLK_RQT);
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08004976 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004977 }
4978 spin_unlock_irqrestore(&priv->lock, flags);
4979
4980 udelay(5);
4981
Kolekar, Abhijeet01ec6162008-12-19 10:37:38 +08004982 priv->cfg->ops->lib->apm_ops.reset(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004983 exit:
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08004984 memset(&priv->card_alive, 0, sizeof(struct iwl_alive_resp));
Zhu Yib481de92007-09-25 17:54:57 -07004985
4986 if (priv->ibss_beacon)
4987 dev_kfree_skb(priv->ibss_beacon);
4988 priv->ibss_beacon = NULL;
4989
4990 /* clear out any free frames */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004991 iwl3945_clear_free_frames(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004992}
4993
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08004994static void iwl3945_down(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004995{
4996 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004997 __iwl3945_down(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004998 mutex_unlock(&priv->mutex);
Zhu Yib24d22b2007-12-19 13:59:52 +08004999
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005000 iwl3945_cancel_deferred_work(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005001}
5002
5003#define MAX_HW_RESTARTS 5
5004
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08005005static int __iwl3945_up(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005006{
5007 int rc, i;
5008
5009 if (test_bit(STATUS_EXIT_PENDING, &priv->status)) {
Winkler, Tomas39aadf82008-12-19 10:37:32 +08005010 IWL_WARN(priv, "Exit pending; will not bring the NIC up\n");
Zhu Yib481de92007-09-25 17:54:57 -07005011 return -EIO;
5012 }
5013
5014 if (test_bit(STATUS_RF_KILL_SW, &priv->status)) {
Winkler, Tomas39aadf82008-12-19 10:37:32 +08005015 IWL_WARN(priv, "Radio disabled by SW RF kill (module "
Zhu Yib481de92007-09-25 17:54:57 -07005016 "parameter)\n");
Zhu Yie655b9f2008-01-24 02:19:38 -08005017 return -ENODEV;
5018 }
5019
Reinette Chatree903fbd2008-01-30 22:05:15 -08005020 if (!priv->ucode_data_backup.v_addr || !priv->ucode_data.v_addr) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08005021 IWL_ERR(priv, "ucode not available for device bring up\n");
Reinette Chatree903fbd2008-01-30 22:05:15 -08005022 return -EIO;
5023 }
5024
Zhu Yie655b9f2008-01-24 02:19:38 -08005025 /* If platform's RF_KILL switch is NOT set to KILL */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08005026 if (iwl_read32(priv, CSR_GP_CNTRL) &
Zhu Yie655b9f2008-01-24 02:19:38 -08005027 CSR_GP_CNTRL_REG_FLAG_HW_RF_KILL_SW)
5028 clear_bit(STATUS_RF_KILL_HW, &priv->status);
5029 else {
5030 set_bit(STATUS_RF_KILL_HW, &priv->status);
5031 if (!test_bit(STATUS_IN_SUSPEND, &priv->status)) {
Winkler, Tomas39aadf82008-12-19 10:37:32 +08005032 IWL_WARN(priv, "Radio disabled by HW RF Kill switch\n");
Zhu Yie655b9f2008-01-24 02:19:38 -08005033 return -ENODEV;
5034 }
Zhu Yib481de92007-09-25 17:54:57 -07005035 }
Adel Gadllah80fcc9e2008-07-01 17:49:50 +02005036
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08005037 iwl_write32(priv, CSR_INT, 0xFFFFFFFF);
Zhu Yib481de92007-09-25 17:54:57 -07005038
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005039 rc = iwl3945_hw_nic_init(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005040 if (rc) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08005041 IWL_ERR(priv, "Unable to int nic\n");
Zhu Yib481de92007-09-25 17:54:57 -07005042 return rc;
5043 }
5044
5045 /* make sure rfkill handshake bits are cleared */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08005046 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
5047 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR,
Zhu Yib481de92007-09-25 17:54:57 -07005048 CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
5049
5050 /* clear (again), then enable host interrupts */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08005051 iwl_write32(priv, CSR_INT, 0xFFFFFFFF);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005052 iwl3945_enable_interrupts(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005053
5054 /* really make sure rfkill handshake bits are cleared */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08005055 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
5056 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
Zhu Yib481de92007-09-25 17:54:57 -07005057
5058 /* Copy original ucode data image from disk into backup cache.
5059 * This will be used to initialize the on-board processor's
5060 * data SRAM for a clean start when the runtime program first loads. */
5061 memcpy(priv->ucode_data_backup.v_addr, priv->ucode_data.v_addr,
Zhu Yi5a669262008-01-14 17:46:18 -08005062 priv->ucode_data.len);
Zhu Yib481de92007-09-25 17:54:57 -07005063
Zhu Yie655b9f2008-01-24 02:19:38 -08005064 /* We return success when we resume from suspend and rf_kill is on. */
5065 if (test_bit(STATUS_RF_KILL_HW, &priv->status))
5066 return 0;
5067
Zhu Yib481de92007-09-25 17:54:57 -07005068 for (i = 0; i < MAX_HW_RESTARTS; i++) {
5069
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005070 iwl3945_clear_stations_table(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005071
5072 /* load bootstrap state machine,
5073 * load bootstrap program into processor's memory,
5074 * prepare to load the "initialize" uCode */
Kolekar, Abhijeet0164b9b2008-12-19 10:37:37 +08005075 priv->cfg->ops->lib->load_ucode(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005076
5077 if (rc) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08005078 IWL_ERR(priv,
5079 "Unable to set up bootstrap uCode: %d\n", rc);
Zhu Yib481de92007-09-25 17:54:57 -07005080 continue;
5081 }
5082
5083 /* start card; "initialize" will load runtime ucode */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005084 iwl3945_nic_start(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005085
Zhu Yib481de92007-09-25 17:54:57 -07005086 IWL_DEBUG_INFO(DRV_NAME " is coming up\n");
5087
5088 return 0;
5089 }
5090
5091 set_bit(STATUS_EXIT_PENDING, &priv->status);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005092 __iwl3945_down(priv);
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08005093 clear_bit(STATUS_EXIT_PENDING, &priv->status);
Zhu Yib481de92007-09-25 17:54:57 -07005094
5095 /* tried to restart and config the device for as long as our
5096 * patience could withstand */
Winkler, Tomas15b16872008-12-19 10:37:33 +08005097 IWL_ERR(priv, "Unable to initialize device after %d attempts.\n", i);
Zhu Yib481de92007-09-25 17:54:57 -07005098 return -EIO;
5099}
5100
5101
5102/*****************************************************************************
5103 *
5104 * Workqueue callbacks
5105 *
5106 *****************************************************************************/
5107
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005108static void iwl3945_bg_init_alive_start(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07005109{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08005110 struct iwl_priv *priv =
5111 container_of(data, struct iwl_priv, init_alive_start.work);
Zhu Yib481de92007-09-25 17:54:57 -07005112
5113 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
5114 return;
5115
5116 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005117 iwl3945_init_alive_start(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005118 mutex_unlock(&priv->mutex);
5119}
5120
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005121static void iwl3945_bg_alive_start(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07005122{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08005123 struct iwl_priv *priv =
5124 container_of(data, struct iwl_priv, alive_start.work);
Zhu Yib481de92007-09-25 17:54:57 -07005125
5126 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
5127 return;
5128
5129 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005130 iwl3945_alive_start(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005131 mutex_unlock(&priv->mutex);
5132}
5133
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005134static void iwl3945_bg_rf_kill(struct work_struct *work)
Zhu Yib481de92007-09-25 17:54:57 -07005135{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08005136 struct iwl_priv *priv = container_of(work, struct iwl_priv, rf_kill);
Zhu Yib481de92007-09-25 17:54:57 -07005137
5138 wake_up_interruptible(&priv->wait_command_queue);
5139
5140 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
5141 return;
5142
5143 mutex_lock(&priv->mutex);
5144
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08005145 if (!iwl_is_rfkill(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07005146 IWL_DEBUG(IWL_DL_INFO | IWL_DL_RF_KILL,
5147 "HW and/or SW RF Kill no longer active, restarting "
5148 "device\n");
Helmut Schaa26635162009-01-15 09:38:44 +01005149 if (!test_bit(STATUS_EXIT_PENDING, &priv->status) &&
5150 test_bit(STATUS_ALIVE, &priv->status))
Zhu Yib481de92007-09-25 17:54:57 -07005151 queue_work(priv->workqueue, &priv->restart);
5152 } else {
5153
5154 if (!test_bit(STATUS_RF_KILL_HW, &priv->status))
5155 IWL_DEBUG_RF_KILL("Can not turn radio back on - "
5156 "disabled by SW switch\n");
5157 else
Winkler, Tomas39aadf82008-12-19 10:37:32 +08005158 IWL_WARN(priv, "Radio Frequency Kill Switch is On:\n"
Zhu Yib481de92007-09-25 17:54:57 -07005159 "Kill switch must be turned off for "
5160 "wireless networking to work.\n");
5161 }
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08005162
Zhu Yib481de92007-09-25 17:54:57 -07005163 mutex_unlock(&priv->mutex);
Adel Gadllah80fcc9e2008-07-01 17:49:50 +02005164 iwl3945_rfkill_set_hw_state(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005165}
5166
Helmut Schaa26635162009-01-15 09:38:44 +01005167static void iwl3945_rfkill_poll(struct work_struct *data)
5168{
5169 struct iwl_priv *priv =
5170 container_of(data, struct iwl_priv, rfkill_poll.work);
5171 unsigned long status = priv->status;
5172
5173 if (iwl_read32(priv, CSR_GP_CNTRL) & CSR_GP_CNTRL_REG_FLAG_HW_RF_KILL_SW)
5174 clear_bit(STATUS_RF_KILL_HW, &priv->status);
5175 else
5176 set_bit(STATUS_RF_KILL_HW, &priv->status);
5177
5178 if (test_bit(STATUS_RF_KILL_HW, &status) != test_bit(STATUS_RF_KILL_HW, &priv->status))
5179 queue_work(priv->workqueue, &priv->rf_kill);
5180
5181 queue_delayed_work(priv->workqueue, &priv->rfkill_poll,
5182 round_jiffies_relative(2 * HZ));
5183
5184}
5185
Zhu Yib481de92007-09-25 17:54:57 -07005186#define IWL_SCAN_CHECK_WATCHDOG (7 * HZ)
5187
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005188static void iwl3945_bg_scan_check(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07005189{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08005190 struct iwl_priv *priv =
5191 container_of(data, struct iwl_priv, scan_check.work);
Zhu Yib481de92007-09-25 17:54:57 -07005192
5193 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
5194 return;
5195
5196 mutex_lock(&priv->mutex);
5197 if (test_bit(STATUS_SCANNING, &priv->status) ||
5198 test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
5199 IWL_DEBUG(IWL_DL_INFO | IWL_DL_SCAN,
5200 "Scan completion watchdog resetting adapter (%dms)\n",
5201 jiffies_to_msecs(IWL_SCAN_CHECK_WATCHDOG));
Mohamed Abbas15e869d2007-10-25 17:15:46 +08005202
Zhu Yib481de92007-09-25 17:54:57 -07005203 if (!test_bit(STATUS_EXIT_PENDING, &priv->status))
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005204 iwl3945_send_scan_abort(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005205 }
5206 mutex_unlock(&priv->mutex);
5207}
5208
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005209static void iwl3945_bg_request_scan(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07005210{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08005211 struct iwl_priv *priv =
5212 container_of(data, struct iwl_priv, request_scan);
Winkler, Tomasc2d79b42008-12-19 10:37:34 +08005213 struct iwl_host_cmd cmd = {
Zhu Yib481de92007-09-25 17:54:57 -07005214 .id = REPLY_SCAN_CMD,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005215 .len = sizeof(struct iwl3945_scan_cmd),
Zhu Yib481de92007-09-25 17:54:57 -07005216 .meta.flags = CMD_SIZE_HUGE,
5217 };
5218 int rc = 0;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005219 struct iwl3945_scan_cmd *scan;
Zhu Yib481de92007-09-25 17:54:57 -07005220 struct ieee80211_conf *conf = NULL;
Abhijeet Kolekarf9340522008-09-03 11:26:58 +08005221 u8 n_probes = 2;
Johannes Berg8318d782008-01-24 19:38:38 +01005222 enum ieee80211_band band;
John W. Linville9387b7c2008-09-30 20:59:05 -04005223 DECLARE_SSID_BUF(ssid);
Zhu Yib481de92007-09-25 17:54:57 -07005224
5225 conf = ieee80211_get_hw_conf(priv->hw);
5226
5227 mutex_lock(&priv->mutex);
5228
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08005229 if (!iwl_is_ready(priv)) {
Winkler, Tomas39aadf82008-12-19 10:37:32 +08005230 IWL_WARN(priv, "request scan called when driver not ready.\n");
Zhu Yib481de92007-09-25 17:54:57 -07005231 goto done;
5232 }
5233
Tomas Winklera96a27f2008-10-23 23:48:56 -07005234 /* Make sure the scan wasn't canceled before this queued work
Zhu Yib481de92007-09-25 17:54:57 -07005235 * was given the chance to run... */
5236 if (!test_bit(STATUS_SCANNING, &priv->status))
5237 goto done;
5238
5239 /* This should never be called or scheduled if there is currently
5240 * a scan active in the hardware. */
5241 if (test_bit(STATUS_SCAN_HW, &priv->status)) {
5242 IWL_DEBUG_INFO("Multiple concurrent scan requests in parallel. "
5243 "Ignoring second request.\n");
5244 rc = -EIO;
5245 goto done;
5246 }
5247
5248 if (test_bit(STATUS_EXIT_PENDING, &priv->status)) {
5249 IWL_DEBUG_SCAN("Aborting scan due to device shutdown\n");
5250 goto done;
5251 }
5252
5253 if (test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
5254 IWL_DEBUG_HC("Scan request while abort pending. Queuing.\n");
5255 goto done;
5256 }
5257
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08005258 if (iwl_is_rfkill(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07005259 IWL_DEBUG_HC("Aborting scan due to RF Kill activation\n");
5260 goto done;
5261 }
5262
5263 if (!test_bit(STATUS_READY, &priv->status)) {
5264 IWL_DEBUG_HC("Scan request while uninitialized. Queuing.\n");
5265 goto done;
5266 }
5267
5268 if (!priv->scan_bands) {
5269 IWL_DEBUG_HC("Aborting scan due to no requested bands\n");
5270 goto done;
5271 }
5272
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08005273 if (!priv->scan39) {
5274 priv->scan39 = kmalloc(sizeof(struct iwl3945_scan_cmd) +
Zhu Yib481de92007-09-25 17:54:57 -07005275 IWL_MAX_SCAN_SIZE, GFP_KERNEL);
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08005276 if (!priv->scan39) {
Zhu Yib481de92007-09-25 17:54:57 -07005277 rc = -ENOMEM;
5278 goto done;
5279 }
5280 }
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08005281 scan = priv->scan39;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005282 memset(scan, 0, sizeof(struct iwl3945_scan_cmd) + IWL_MAX_SCAN_SIZE);
Zhu Yib481de92007-09-25 17:54:57 -07005283
5284 scan->quiet_plcp_th = IWL_PLCP_QUIET_THRESH;
5285 scan->quiet_time = IWL_ACTIVE_QUIET_TIME;
5286
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005287 if (iwl3945_is_associated(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07005288 u16 interval = 0;
5289 u32 extra;
5290 u32 suspend_time = 100;
5291 u32 scan_suspend_time = 100;
5292 unsigned long flags;
5293
5294 IWL_DEBUG_INFO("Scanning while associated...\n");
5295
5296 spin_lock_irqsave(&priv->lock, flags);
5297 interval = priv->beacon_int;
5298 spin_unlock_irqrestore(&priv->lock, flags);
5299
5300 scan->suspend_time = 0;
Mohamed Abbas15e869d2007-10-25 17:15:46 +08005301 scan->max_out_time = cpu_to_le32(200 * 1024);
Zhu Yib481de92007-09-25 17:54:57 -07005302 if (!interval)
5303 interval = suspend_time;
5304 /*
5305 * suspend time format:
5306 * 0-19: beacon interval in usec (time before exec.)
5307 * 20-23: 0
5308 * 24-31: number of beacons (suspend between channels)
5309 */
5310
5311 extra = (suspend_time / interval) << 24;
5312 scan_suspend_time = 0xFF0FFFFF &
5313 (extra | ((suspend_time % interval) * 1024));
5314
5315 scan->suspend_time = cpu_to_le32(scan_suspend_time);
5316 IWL_DEBUG_SCAN("suspend_time 0x%X beacon interval %d\n",
5317 scan_suspend_time, interval);
5318 }
5319
5320 /* We should add the ability for user to lock to PASSIVE ONLY */
5321 if (priv->one_direct_scan) {
5322 IWL_DEBUG_SCAN
5323 ("Kicking off one direct scan for '%s'\n",
John W. Linville9387b7c2008-09-30 20:59:05 -04005324 print_ssid(ssid, priv->direct_ssid,
5325 priv->direct_ssid_len));
Zhu Yib481de92007-09-25 17:54:57 -07005326 scan->direct_scan[0].id = WLAN_EID_SSID;
5327 scan->direct_scan[0].len = priv->direct_ssid_len;
5328 memcpy(scan->direct_scan[0].ssid,
5329 priv->direct_ssid, priv->direct_ssid_len);
Abhijeet Kolekarf9340522008-09-03 11:26:58 +08005330 n_probes++;
Abhijeet Kolekarf9340522008-09-03 11:26:58 +08005331 } else
Bill Moss786b4552008-04-17 16:03:40 -07005332 IWL_DEBUG_SCAN("Kicking off one indirect scan.\n");
Zhu Yib481de92007-09-25 17:54:57 -07005333
5334 /* We don't build a direct scan probe request; the uCode will do
5335 * that based on the direct_mask added to each channel entry */
5336 scan->tx_cmd.len = cpu_to_le16(
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005337 iwl3945_fill_probe_req(priv, (struct ieee80211_mgmt *)scan->data,
Johannes Berg430cfe92008-10-28 18:06:02 +01005338 IWL_MAX_SCAN_SIZE - sizeof(*scan)));
Zhu Yib481de92007-09-25 17:54:57 -07005339 scan->tx_cmd.tx_flags = TX_CMD_FLG_SEQ_CTL_MSK;
Abhijeet Kolekar3832ec92008-12-19 10:37:26 +08005340 scan->tx_cmd.sta_id = priv->hw_params.bcast_sta_id;
Zhu Yib481de92007-09-25 17:54:57 -07005341 scan->tx_cmd.stop_time.life_time = TX_CMD_LIFE_TIME_INFINITE;
5342
5343 /* flags + rate selection */
5344
Ron Rindjunsky66b50042008-06-25 16:46:31 +08005345 if (priv->scan_bands & BIT(IEEE80211_BAND_2GHZ)) {
Zhu Yib481de92007-09-25 17:54:57 -07005346 scan->flags = RXON_FLG_BAND_24G_MSK | RXON_FLG_AUTO_DETECT_MSK;
5347 scan->tx_cmd.rate = IWL_RATE_1M_PLCP;
5348 scan->good_CRC_th = 0;
Johannes Berg8318d782008-01-24 19:38:38 +01005349 band = IEEE80211_BAND_2GHZ;
Ron Rindjunsky66b50042008-06-25 16:46:31 +08005350 } else if (priv->scan_bands & BIT(IEEE80211_BAND_5GHZ)) {
Zhu Yib481de92007-09-25 17:54:57 -07005351 scan->tx_cmd.rate = IWL_RATE_6M_PLCP;
5352 scan->good_CRC_th = IWL_GOOD_CRC_TH;
Johannes Berg8318d782008-01-24 19:38:38 +01005353 band = IEEE80211_BAND_5GHZ;
Ron Rindjunsky66b50042008-06-25 16:46:31 +08005354 } else {
Winkler, Tomas39aadf82008-12-19 10:37:32 +08005355 IWL_WARN(priv, "Invalid scan band count\n");
Zhu Yib481de92007-09-25 17:54:57 -07005356 goto done;
5357 }
5358
5359 /* select Rx antennas */
5360 scan->flags |= iwl3945_get_antenna_flags(priv);
5361
Johannes Berg05c914f2008-09-11 00:01:58 +02005362 if (priv->iw_mode == NL80211_IFTYPE_MONITOR)
Zhu Yib481de92007-09-25 17:54:57 -07005363 scan->filter_flags = RXON_FILTER_PROMISC_MSK;
5364
Abhijeet Kolekarf9340522008-09-03 11:26:58 +08005365 scan->channel_count =
5366 iwl3945_get_channels_for_scan(priv, band, 1, /* active */
5367 n_probes,
5368 (void *)&scan->data[le16_to_cpu(scan->tx_cmd.len)]);
Zhu Yib481de92007-09-25 17:54:57 -07005369
Reinette Chatre14b54332008-11-04 12:21:35 -08005370 if (scan->channel_count == 0) {
5371 IWL_DEBUG_SCAN("channel count %d\n", scan->channel_count);
5372 goto done;
5373 }
5374
Zhu Yib481de92007-09-25 17:54:57 -07005375 cmd.len += le16_to_cpu(scan->tx_cmd.len) +
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005376 scan->channel_count * sizeof(struct iwl3945_scan_channel);
Zhu Yib481de92007-09-25 17:54:57 -07005377 cmd.data = scan;
5378 scan->len = cpu_to_le16(cmd.len);
5379
5380 set_bit(STATUS_SCAN_HW, &priv->status);
Samuel Ortiz518099a2009-01-19 15:30:27 -08005381 rc = iwl_send_cmd_sync(priv, &cmd);
Zhu Yib481de92007-09-25 17:54:57 -07005382 if (rc)
5383 goto done;
5384
5385 queue_delayed_work(priv->workqueue, &priv->scan_check,
5386 IWL_SCAN_CHECK_WATCHDOG);
5387
5388 mutex_unlock(&priv->mutex);
5389 return;
5390
5391 done:
Mohamed Abbas2420ebc2008-11-04 12:21:34 -08005392 /* can not perform scan make sure we clear scanning
5393 * bits from status so next scan request can be performed.
5394 * if we dont clear scanning status bit here all next scan
5395 * will fail
5396 */
5397 clear_bit(STATUS_SCAN_HW, &priv->status);
5398 clear_bit(STATUS_SCANNING, &priv->status);
5399
Ian Schram01ebd062007-10-25 17:15:22 +08005400 /* inform mac80211 scan aborted */
Zhu Yib481de92007-09-25 17:54:57 -07005401 queue_work(priv->workqueue, &priv->scan_completed);
5402 mutex_unlock(&priv->mutex);
5403}
5404
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005405static void iwl3945_bg_up(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07005406{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08005407 struct iwl_priv *priv = container_of(data, struct iwl_priv, up);
Zhu Yib481de92007-09-25 17:54:57 -07005408
5409 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
5410 return;
5411
5412 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005413 __iwl3945_up(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005414 mutex_unlock(&priv->mutex);
Adel Gadllah80fcc9e2008-07-01 17:49:50 +02005415 iwl3945_rfkill_set_hw_state(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005416}
5417
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005418static void iwl3945_bg_restart(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07005419{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08005420 struct iwl_priv *priv = container_of(data, struct iwl_priv, restart);
Zhu Yib481de92007-09-25 17:54:57 -07005421
5422 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
5423 return;
5424
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005425 iwl3945_down(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005426 queue_work(priv->workqueue, &priv->up);
5427}
5428
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005429static void iwl3945_bg_rx_replenish(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07005430{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08005431 struct iwl_priv *priv =
5432 container_of(data, struct iwl_priv, rx_replenish);
Zhu Yib481de92007-09-25 17:54:57 -07005433
5434 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
5435 return;
5436
5437 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005438 iwl3945_rx_replenish(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005439 mutex_unlock(&priv->mutex);
5440}
5441
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08005442#define IWL_DELAY_NEXT_SCAN (HZ*2)
5443
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08005444static void iwl3945_post_associate(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005445{
Zhu Yib481de92007-09-25 17:54:57 -07005446 int rc = 0;
5447 struct ieee80211_conf *conf = NULL;
5448
Johannes Berg05c914f2008-09-11 00:01:58 +02005449 if (priv->iw_mode == NL80211_IFTYPE_AP) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08005450 IWL_ERR(priv, "%s Should not be called in AP mode\n", __func__);
Zhu Yib481de92007-09-25 17:54:57 -07005451 return;
5452 }
5453
5454
Johannes Berge1749612008-10-27 15:59:26 -07005455 IWL_DEBUG_ASSOC("Associated as %d to: %pM\n",
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08005456 priv->assoc_id, priv->active39_rxon.bssid_addr);
Zhu Yib481de92007-09-25 17:54:57 -07005457
5458 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
5459 return;
5460
Abhijeet Kolekar322a9812008-09-03 11:26:27 +08005461 if (!priv->vif || !priv->is_open)
Mohamed Abbas6ef89d02007-10-25 17:15:47 +08005462 return;
Abhijeet Kolekar322a9812008-09-03 11:26:27 +08005463
Winkler, Tomasaf0053d2009-01-19 15:30:23 -08005464 iwl_scan_cancel_timeout(priv, 200);
Mohamed Abbas15e869d2007-10-25 17:15:46 +08005465
Zhu Yib481de92007-09-25 17:54:57 -07005466 conf = ieee80211_get_hw_conf(priv->hw);
5467
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08005468 priv->staging39_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005469 iwl3945_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005470
Tomas Winkler28afaf92008-12-19 10:37:06 +08005471 memset(&priv->rxon_timing, 0, sizeof(struct iwl_rxon_time_cmd));
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005472 iwl3945_setup_rxon_timing(priv);
Samuel Ortiz518099a2009-01-19 15:30:27 -08005473 rc = iwl_send_cmd_pdu(priv, REPLY_RXON_TIMING,
Zhu Yib481de92007-09-25 17:54:57 -07005474 sizeof(priv->rxon_timing), &priv->rxon_timing);
5475 if (rc)
Winkler, Tomas39aadf82008-12-19 10:37:32 +08005476 IWL_WARN(priv, "REPLY_RXON_TIMING failed - "
Zhu Yib481de92007-09-25 17:54:57 -07005477 "Attempting to continue.\n");
5478
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08005479 priv->staging39_rxon.filter_flags |= RXON_FILTER_ASSOC_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07005480
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08005481 priv->staging39_rxon.assoc_id = cpu_to_le16(priv->assoc_id);
Zhu Yib481de92007-09-25 17:54:57 -07005482
5483 IWL_DEBUG_ASSOC("assoc id %d beacon interval %d\n",
5484 priv->assoc_id, priv->beacon_int);
5485
5486 if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_PREAMBLE)
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08005487 priv->staging39_rxon.flags |= RXON_FLG_SHORT_PREAMBLE_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07005488 else
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08005489 priv->staging39_rxon.flags &= ~RXON_FLG_SHORT_PREAMBLE_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07005490
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08005491 if (priv->staging39_rxon.flags & RXON_FLG_BAND_24G_MSK) {
Zhu Yib481de92007-09-25 17:54:57 -07005492 if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_SLOT_TIME)
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08005493 priv->staging39_rxon.flags |= RXON_FLG_SHORT_SLOT_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07005494 else
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08005495 priv->staging39_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07005496
Johannes Berg05c914f2008-09-11 00:01:58 +02005497 if (priv->iw_mode == NL80211_IFTYPE_ADHOC)
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08005498 priv->staging39_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07005499
5500 }
5501
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005502 iwl3945_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005503
5504 switch (priv->iw_mode) {
Johannes Berg05c914f2008-09-11 00:01:58 +02005505 case NL80211_IFTYPE_STATION:
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005506 iwl3945_rate_scale_init(priv->hw, IWL_AP_ID);
Zhu Yib481de92007-09-25 17:54:57 -07005507 break;
5508
Johannes Berg05c914f2008-09-11 00:01:58 +02005509 case NL80211_IFTYPE_ADHOC:
Zhu Yib481de92007-09-25 17:54:57 -07005510
Abhijeet Kolekarce546fd2008-11-19 15:32:22 -08005511 priv->assoc_id = 1;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005512 iwl3945_add_station(priv, priv->bssid, 0, 0);
Zhu Yib481de92007-09-25 17:54:57 -07005513 iwl3945_sync_sta(priv, IWL_STA_ID,
Johannes Berg8318d782008-01-24 19:38:38 +01005514 (priv->band == IEEE80211_BAND_5GHZ) ?
Zhu Yib481de92007-09-25 17:54:57 -07005515 IWL_RATE_6M_PLCP : IWL_RATE_1M_PLCP,
5516 CMD_ASYNC);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005517 iwl3945_rate_scale_init(priv->hw, IWL_STA_ID);
5518 iwl3945_send_beacon_cmd(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005519
5520 break;
5521
5522 default:
Winkler, Tomas15b16872008-12-19 10:37:33 +08005523 IWL_ERR(priv, "%s Should not be called in %d mode\n",
Tomas Winkler3ac7f142008-07-21 02:40:14 +03005524 __func__, priv->iw_mode);
Zhu Yib481de92007-09-25 17:54:57 -07005525 break;
5526 }
5527
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005528 iwl3945_activate_qos(priv, 0);
Ron Rindjunsky292ae172008-02-06 11:20:39 -08005529
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08005530 /* we have just associated, don't start scan too early */
5531 priv->next_scan_jiffies = jiffies + IWL_DELAY_NEXT_SCAN;
Abhijeet Kolekarcd56d332008-09-03 11:26:21 +08005532}
5533
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005534static void iwl3945_bg_abort_scan(struct work_struct *work)
Zhu Yib481de92007-09-25 17:54:57 -07005535{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08005536 struct iwl_priv *priv = container_of(work, struct iwl_priv, abort_scan);
Zhu Yib481de92007-09-25 17:54:57 -07005537
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08005538 if (!iwl_is_ready(priv))
Zhu Yib481de92007-09-25 17:54:57 -07005539 return;
5540
5541 mutex_lock(&priv->mutex);
5542
5543 set_bit(STATUS_SCAN_ABORTING, &priv->status);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005544 iwl3945_send_scan_abort(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005545
5546 mutex_unlock(&priv->mutex);
5547}
5548
Johannes Berge8975582008-10-09 12:18:51 +02005549static int iwl3945_mac_config(struct ieee80211_hw *hw, u32 changed);
Zhu Yi76bb77e2007-11-22 10:53:22 +08005550
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005551static void iwl3945_bg_scan_completed(struct work_struct *work)
Zhu Yib481de92007-09-25 17:54:57 -07005552{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08005553 struct iwl_priv *priv =
5554 container_of(work, struct iwl_priv, scan_completed);
Zhu Yib481de92007-09-25 17:54:57 -07005555
5556 IWL_DEBUG(IWL_DL_INFO | IWL_DL_SCAN, "SCAN complete scan\n");
5557
5558 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
5559 return;
5560
Zhu Yia0646472007-12-20 14:10:01 +08005561 if (test_bit(STATUS_CONF_PENDING, &priv->status))
Johannes Berge8975582008-10-09 12:18:51 +02005562 iwl3945_mac_config(priv->hw, 0);
Zhu Yi76bb77e2007-11-22 10:53:22 +08005563
Zhu Yib481de92007-09-25 17:54:57 -07005564 ieee80211_scan_completed(priv->hw);
5565
5566 /* Since setting the TXPOWER may have been deferred while
5567 * performing the scan, fire one off */
5568 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005569 iwl3945_hw_reg_send_txpower(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005570 mutex_unlock(&priv->mutex);
5571}
5572
5573/*****************************************************************************
5574 *
5575 * mac80211 entry point functions
5576 *
5577 *****************************************************************************/
5578
Zhu Yi5a669262008-01-14 17:46:18 -08005579#define UCODE_READY_TIMEOUT (2 * HZ)
5580
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005581static int iwl3945_mac_start(struct ieee80211_hw *hw)
Zhu Yib481de92007-09-25 17:54:57 -07005582{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08005583 struct iwl_priv *priv = hw->priv;
Zhu Yi5a669262008-01-14 17:46:18 -08005584 int ret;
Zhu Yib481de92007-09-25 17:54:57 -07005585
5586 IWL_DEBUG_MAC80211("enter\n");
5587
5588 /* we should be verifying the device is ready to be opened */
5589 mutex_lock(&priv->mutex);
5590
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08005591 memset(&priv->staging39_rxon, 0, sizeof(struct iwl3945_rxon_cmd));
Zhu Yi5a669262008-01-14 17:46:18 -08005592 /* fetch ucode file from disk, alloc and copy to bus-master buffers ...
5593 * ucode filename and max sizes are card-specific. */
5594
5595 if (!priv->ucode_code.len) {
5596 ret = iwl3945_read_ucode(priv);
5597 if (ret) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08005598 IWL_ERR(priv, "Could not read microcode: %d\n", ret);
Zhu Yi5a669262008-01-14 17:46:18 -08005599 mutex_unlock(&priv->mutex);
5600 goto out_release_irq;
5601 }
5602 }
5603
Zhu Yie655b9f2008-01-24 02:19:38 -08005604 ret = __iwl3945_up(priv);
Zhu Yi5a669262008-01-14 17:46:18 -08005605
Zhu Yib481de92007-09-25 17:54:57 -07005606 mutex_unlock(&priv->mutex);
Zhu Yi5a669262008-01-14 17:46:18 -08005607
Adel Gadllah80fcc9e2008-07-01 17:49:50 +02005608 iwl3945_rfkill_set_hw_state(priv);
5609
Zhu Yie655b9f2008-01-24 02:19:38 -08005610 if (ret)
5611 goto out_release_irq;
5612
5613 IWL_DEBUG_INFO("Start UP work.\n");
5614
5615 if (test_bit(STATUS_IN_SUSPEND, &priv->status))
5616 return 0;
5617
Zhu Yi5a669262008-01-14 17:46:18 -08005618 /* Wait for START_ALIVE from ucode. Otherwise callbacks from
5619 * mac80211 will not be run successfully. */
5620 ret = wait_event_interruptible_timeout(priv->wait_command_queue,
5621 test_bit(STATUS_READY, &priv->status),
5622 UCODE_READY_TIMEOUT);
5623 if (!ret) {
5624 if (!test_bit(STATUS_READY, &priv->status)) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08005625 IWL_ERR(priv,
5626 "Wait for START_ALIVE timeout after %dms.\n",
5627 jiffies_to_msecs(UCODE_READY_TIMEOUT));
Zhu Yi5a669262008-01-14 17:46:18 -08005628 ret = -ETIMEDOUT;
5629 goto out_release_irq;
5630 }
5631 }
5632
Helmut Schaa26635162009-01-15 09:38:44 +01005633 /* ucode is running and will send rfkill notifications,
5634 * no need to poll the killswitch state anymore */
5635 cancel_delayed_work(&priv->rfkill_poll);
5636
Zhu Yie655b9f2008-01-24 02:19:38 -08005637 priv->is_open = 1;
Zhu Yib481de92007-09-25 17:54:57 -07005638 IWL_DEBUG_MAC80211("leave\n");
5639 return 0;
Zhu Yi5a669262008-01-14 17:46:18 -08005640
5641out_release_irq:
Zhu Yie655b9f2008-01-24 02:19:38 -08005642 priv->is_open = 0;
5643 IWL_DEBUG_MAC80211("leave - failed\n");
Zhu Yi5a669262008-01-14 17:46:18 -08005644 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07005645}
5646
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005647static void iwl3945_mac_stop(struct ieee80211_hw *hw)
Zhu Yib481de92007-09-25 17:54:57 -07005648{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08005649 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07005650
5651 IWL_DEBUG_MAC80211("enter\n");
Mohamed Abbas6ef89d02007-10-25 17:15:47 +08005652
Zhu Yie655b9f2008-01-24 02:19:38 -08005653 if (!priv->is_open) {
5654 IWL_DEBUG_MAC80211("leave - skip\n");
5655 return;
5656 }
5657
Zhu Yib481de92007-09-25 17:54:57 -07005658 priv->is_open = 0;
Zhu Yi5a669262008-01-14 17:46:18 -08005659
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08005660 if (iwl_is_ready_rf(priv)) {
Zhu Yie655b9f2008-01-24 02:19:38 -08005661 /* stop mac, cancel any scan request and clear
5662 * RXON_FILTER_ASSOC_MSK BIT
5663 */
Zhu Yi5a669262008-01-14 17:46:18 -08005664 mutex_lock(&priv->mutex);
Winkler, Tomasaf0053d2009-01-19 15:30:23 -08005665 iwl_scan_cancel_timeout(priv, 100);
Mohamed Abbasfde35712007-11-29 11:10:15 +08005666 mutex_unlock(&priv->mutex);
Mohamed Abbasfde35712007-11-29 11:10:15 +08005667 }
5668
Zhu Yi5a669262008-01-14 17:46:18 -08005669 iwl3945_down(priv);
5670
5671 flush_workqueue(priv->workqueue);
Helmut Schaa26635162009-01-15 09:38:44 +01005672
5673 /* start polling the killswitch state again */
5674 queue_delayed_work(priv->workqueue, &priv->rfkill_poll,
5675 round_jiffies_relative(2 * HZ));
Mohamed Abbas6ef89d02007-10-25 17:15:47 +08005676
Zhu Yib481de92007-09-25 17:54:57 -07005677 IWL_DEBUG_MAC80211("leave\n");
Zhu Yib481de92007-09-25 17:54:57 -07005678}
5679
Johannes Berge039fa42008-05-15 12:55:29 +02005680static int iwl3945_mac_tx(struct ieee80211_hw *hw, struct sk_buff *skb)
Zhu Yib481de92007-09-25 17:54:57 -07005681{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08005682 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07005683
5684 IWL_DEBUG_MAC80211("enter\n");
5685
Zhu Yib481de92007-09-25 17:54:57 -07005686 IWL_DEBUG_TX("dev->xmit(%d bytes) at rate 0x%02x\n", skb->len,
Johannes Berge039fa42008-05-15 12:55:29 +02005687 ieee80211_get_tx_rate(hw, IEEE80211_SKB_CB(skb))->bitrate);
Zhu Yib481de92007-09-25 17:54:57 -07005688
Johannes Berge039fa42008-05-15 12:55:29 +02005689 if (iwl3945_tx_skb(priv, skb))
Zhu Yib481de92007-09-25 17:54:57 -07005690 dev_kfree_skb_any(skb);
5691
5692 IWL_DEBUG_MAC80211("leave\n");
Reinette Chatre637f8832009-01-19 15:30:32 -08005693 return NETDEV_TX_OK;
Zhu Yib481de92007-09-25 17:54:57 -07005694}
5695
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005696static int iwl3945_mac_add_interface(struct ieee80211_hw *hw,
Zhu Yib481de92007-09-25 17:54:57 -07005697 struct ieee80211_if_init_conf *conf)
5698{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08005699 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07005700 unsigned long flags;
5701
Johannes Berg32bfd352007-12-19 01:31:26 +01005702 IWL_DEBUG_MAC80211("enter: type %d\n", conf->type);
Zhu Yib481de92007-09-25 17:54:57 -07005703
Johannes Berg32bfd352007-12-19 01:31:26 +01005704 if (priv->vif) {
5705 IWL_DEBUG_MAC80211("leave - vif != NULL\n");
Tomas Winkler864792e2007-11-27 21:00:52 +02005706 return -EOPNOTSUPP;
Zhu Yib481de92007-09-25 17:54:57 -07005707 }
5708
5709 spin_lock_irqsave(&priv->lock, flags);
Johannes Berg32bfd352007-12-19 01:31:26 +01005710 priv->vif = conf->vif;
Zhu, Yi60294de2008-10-29 14:05:45 -07005711 priv->iw_mode = conf->type;
Zhu Yib481de92007-09-25 17:54:57 -07005712
5713 spin_unlock_irqrestore(&priv->lock, flags);
5714
5715 mutex_lock(&priv->mutex);
Tomas Winkler864792e2007-11-27 21:00:52 +02005716
5717 if (conf->mac_addr) {
Johannes Berge1749612008-10-27 15:59:26 -07005718 IWL_DEBUG_MAC80211("Set: %pM\n", conf->mac_addr);
Tomas Winkler864792e2007-11-27 21:00:52 +02005719 memcpy(priv->mac_addr, conf->mac_addr, ETH_ALEN);
5720 }
5721
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08005722 if (iwl_is_ready(priv))
Zhu Yi5a669262008-01-14 17:46:18 -08005723 iwl3945_set_mode(priv, conf->type);
Zhu Yib481de92007-09-25 17:54:57 -07005724
Zhu Yib481de92007-09-25 17:54:57 -07005725 mutex_unlock(&priv->mutex);
5726
Zhu Yi5a669262008-01-14 17:46:18 -08005727 IWL_DEBUG_MAC80211("leave\n");
Zhu Yib481de92007-09-25 17:54:57 -07005728 return 0;
5729}
5730
5731/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005732 * iwl3945_mac_config - mac80211 config callback
Zhu Yib481de92007-09-25 17:54:57 -07005733 *
5734 * We ignore conf->flags & IEEE80211_CONF_SHORT_SLOT_TIME since it seems to
5735 * be set inappropriately and the driver currently sets the hardware up to
5736 * use it whenever needed.
5737 */
Johannes Berge8975582008-10-09 12:18:51 +02005738static int iwl3945_mac_config(struct ieee80211_hw *hw, u32 changed)
Zhu Yib481de92007-09-25 17:54:57 -07005739{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08005740 struct iwl_priv *priv = hw->priv;
Samuel Ortizd20b3c62008-12-19 10:37:15 +08005741 const struct iwl_channel_info *ch_info;
Johannes Berge8975582008-10-09 12:18:51 +02005742 struct ieee80211_conf *conf = &hw->conf;
Zhu Yib481de92007-09-25 17:54:57 -07005743 unsigned long flags;
Zhu Yi76bb77e2007-11-22 10:53:22 +08005744 int ret = 0;
Zhu Yib481de92007-09-25 17:54:57 -07005745
5746 mutex_lock(&priv->mutex);
Johannes Berg8318d782008-01-24 19:38:38 +01005747 IWL_DEBUG_MAC80211("enter to channel %d\n", conf->channel->hw_value);
Zhu Yib481de92007-09-25 17:54:57 -07005748
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08005749 if (!iwl_is_ready(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07005750 IWL_DEBUG_MAC80211("leave - not ready\n");
Zhu Yi76bb77e2007-11-22 10:53:22 +08005751 ret = -EIO;
5752 goto out;
Zhu Yib481de92007-09-25 17:54:57 -07005753 }
5754
Kolekar, Abhijeetdf878d82008-12-19 10:37:35 +08005755 if (unlikely(!iwl3945_mod_params.disable_hw_scan &&
Zhu Yib481de92007-09-25 17:54:57 -07005756 test_bit(STATUS_SCANNING, &priv->status))) {
Zhu Yia0646472007-12-20 14:10:01 +08005757 IWL_DEBUG_MAC80211("leave - scanning\n");
5758 set_bit(STATUS_CONF_PENDING, &priv->status);
Zhu Yib481de92007-09-25 17:54:57 -07005759 mutex_unlock(&priv->mutex);
Zhu Yia0646472007-12-20 14:10:01 +08005760 return 0;
Zhu Yib481de92007-09-25 17:54:57 -07005761 }
5762
5763 spin_lock_irqsave(&priv->lock, flags);
5764
Johannes Berg8318d782008-01-24 19:38:38 +01005765 ch_info = iwl3945_get_channel_info(priv, conf->channel->band,
5766 conf->channel->hw_value);
Zhu Yib481de92007-09-25 17:54:57 -07005767 if (!is_channel_valid(ch_info)) {
Ron Rindjunsky66b50042008-06-25 16:46:31 +08005768 IWL_DEBUG_SCAN("Channel %d [%d] is INVALID for this band.\n",
Johannes Berg8318d782008-01-24 19:38:38 +01005769 conf->channel->hw_value, conf->channel->band);
Zhu Yib481de92007-09-25 17:54:57 -07005770 IWL_DEBUG_MAC80211("leave - invalid channel\n");
5771 spin_unlock_irqrestore(&priv->lock, flags);
Zhu Yi76bb77e2007-11-22 10:53:22 +08005772 ret = -EINVAL;
5773 goto out;
Zhu Yib481de92007-09-25 17:54:57 -07005774 }
5775
Johannes Berg8318d782008-01-24 19:38:38 +01005776 iwl3945_set_rxon_channel(priv, conf->channel->band, conf->channel->hw_value);
Zhu Yib481de92007-09-25 17:54:57 -07005777
Johannes Berg8318d782008-01-24 19:38:38 +01005778 iwl3945_set_flags_for_phymode(priv, conf->channel->band);
Zhu Yib481de92007-09-25 17:54:57 -07005779
5780 /* The list of supported rates and rate mask can be different
5781 * for each phymode; since the phymode may have changed, reset
5782 * the rate mask to what mac80211 lists */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005783 iwl3945_set_rate(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005784
5785 spin_unlock_irqrestore(&priv->lock, flags);
5786
5787#ifdef IEEE80211_CONF_CHANNEL_SWITCH
5788 if (conf->flags & IEEE80211_CONF_CHANNEL_SWITCH) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005789 iwl3945_hw_channel_switch(priv, conf->channel);
Zhu Yi76bb77e2007-11-22 10:53:22 +08005790 goto out;
Zhu Yib481de92007-09-25 17:54:57 -07005791 }
5792#endif
5793
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005794 iwl3945_radio_kill_sw(priv, !conf->radio_enabled);
Zhu Yib481de92007-09-25 17:54:57 -07005795
5796 if (!conf->radio_enabled) {
5797 IWL_DEBUG_MAC80211("leave - radio disabled\n");
Zhu Yi76bb77e2007-11-22 10:53:22 +08005798 goto out;
Zhu Yib481de92007-09-25 17:54:57 -07005799 }
5800
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08005801 if (iwl_is_rfkill(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07005802 IWL_DEBUG_MAC80211("leave - RF kill\n");
Zhu Yi76bb77e2007-11-22 10:53:22 +08005803 ret = -EIO;
5804 goto out;
Zhu Yib481de92007-09-25 17:54:57 -07005805 }
5806
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005807 iwl3945_set_rate(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005808
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08005809 if (memcmp(&priv->active39_rxon,
5810 &priv->staging39_rxon, sizeof(priv->staging39_rxon)))
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005811 iwl3945_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005812 else
5813 IWL_DEBUG_INFO("No re-sending same RXON configuration.\n");
5814
5815 IWL_DEBUG_MAC80211("leave\n");
5816
Zhu Yi76bb77e2007-11-22 10:53:22 +08005817out:
Zhu Yia0646472007-12-20 14:10:01 +08005818 clear_bit(STATUS_CONF_PENDING, &priv->status);
Zhu Yib481de92007-09-25 17:54:57 -07005819 mutex_unlock(&priv->mutex);
Zhu Yi76bb77e2007-11-22 10:53:22 +08005820 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07005821}
5822
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08005823static void iwl3945_config_ap(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005824{
5825 int rc = 0;
5826
Maarten Lankhorstd986bcd2008-01-23 10:15:16 -08005827 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
Zhu Yib481de92007-09-25 17:54:57 -07005828 return;
5829
5830 /* The following should be done only at AP bring up */
Ron Rindjunsky5d1e2322008-06-30 17:23:04 +08005831 if (!(iwl3945_is_associated(priv))) {
Zhu Yib481de92007-09-25 17:54:57 -07005832
5833 /* RXON - unassoc (to set timing command) */
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08005834 priv->staging39_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005835 iwl3945_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005836
5837 /* RXON Timing */
Tomas Winkler28afaf92008-12-19 10:37:06 +08005838 memset(&priv->rxon_timing, 0, sizeof(struct iwl_rxon_time_cmd));
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005839 iwl3945_setup_rxon_timing(priv);
Samuel Ortiz518099a2009-01-19 15:30:27 -08005840 rc = iwl_send_cmd_pdu(priv, REPLY_RXON_TIMING,
5841 sizeof(priv->rxon_timing),
5842 &priv->rxon_timing);
Zhu Yib481de92007-09-25 17:54:57 -07005843 if (rc)
Winkler, Tomas39aadf82008-12-19 10:37:32 +08005844 IWL_WARN(priv, "REPLY_RXON_TIMING failed - "
Zhu Yib481de92007-09-25 17:54:57 -07005845 "Attempting to continue.\n");
5846
5847 /* FIXME: what should be the assoc_id for AP? */
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08005848 priv->staging39_rxon.assoc_id = cpu_to_le16(priv->assoc_id);
Zhu Yib481de92007-09-25 17:54:57 -07005849 if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_PREAMBLE)
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08005850 priv->staging39_rxon.flags |=
Zhu Yib481de92007-09-25 17:54:57 -07005851 RXON_FLG_SHORT_PREAMBLE_MSK;
5852 else
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08005853 priv->staging39_rxon.flags &=
Zhu Yib481de92007-09-25 17:54:57 -07005854 ~RXON_FLG_SHORT_PREAMBLE_MSK;
5855
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08005856 if (priv->staging39_rxon.flags & RXON_FLG_BAND_24G_MSK) {
Zhu Yib481de92007-09-25 17:54:57 -07005857 if (priv->assoc_capability &
5858 WLAN_CAPABILITY_SHORT_SLOT_TIME)
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08005859 priv->staging39_rxon.flags |=
Zhu Yib481de92007-09-25 17:54:57 -07005860 RXON_FLG_SHORT_SLOT_MSK;
5861 else
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08005862 priv->staging39_rxon.flags &=
Zhu Yib481de92007-09-25 17:54:57 -07005863 ~RXON_FLG_SHORT_SLOT_MSK;
5864
Johannes Berg05c914f2008-09-11 00:01:58 +02005865 if (priv->iw_mode == NL80211_IFTYPE_ADHOC)
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08005866 priv->staging39_rxon.flags &=
Zhu Yib481de92007-09-25 17:54:57 -07005867 ~RXON_FLG_SHORT_SLOT_MSK;
5868 }
5869 /* restore RXON assoc */
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08005870 priv->staging39_rxon.filter_flags |= RXON_FILTER_ASSOC_MSK;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005871 iwl3945_commit_rxon(priv);
Kolekar, Abhijeetb5323d32008-12-19 10:37:22 +08005872 iwl3945_add_station(priv, iwl_bcast_addr, 0, 0);
Zhu Yi556f8db2007-09-27 11:27:33 +08005873 }
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005874 iwl3945_send_beacon_cmd(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005875
5876 /* FIXME - we need to add code here to detect a totally new
5877 * configuration, reset the AP, unassoc, rxon timing, assoc,
5878 * clear sta table, add BCAST sta... */
5879}
5880
Johannes Berg32bfd352007-12-19 01:31:26 +01005881static int iwl3945_mac_config_interface(struct ieee80211_hw *hw,
5882 struct ieee80211_vif *vif,
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08005883 struct ieee80211_if_conf *conf)
Zhu Yib481de92007-09-25 17:54:57 -07005884{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08005885 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07005886 int rc;
5887
5888 if (conf == NULL)
5889 return -EIO;
5890
Emmanuel Grumbachb716bb92008-03-04 18:09:32 -08005891 if (priv->vif != vif) {
5892 IWL_DEBUG_MAC80211("leave - priv->vif != vif\n");
Emmanuel Grumbachb716bb92008-03-04 18:09:32 -08005893 return 0;
5894 }
5895
Johannes Berg9d139c82008-07-09 14:40:37 +02005896 /* handle this temporarily here */
Johannes Berg05c914f2008-09-11 00:01:58 +02005897 if (priv->iw_mode == NL80211_IFTYPE_ADHOC &&
Johannes Berg9d139c82008-07-09 14:40:37 +02005898 conf->changed & IEEE80211_IFCC_BEACON) {
5899 struct sk_buff *beacon = ieee80211_beacon_get(hw, vif);
5900 if (!beacon)
5901 return -ENOMEM;
Mohamed Abbas9bdf5ec2008-11-07 09:58:35 -08005902 mutex_lock(&priv->mutex);
Johannes Berg9d139c82008-07-09 14:40:37 +02005903 rc = iwl3945_mac_beacon_update(hw, beacon);
Mohamed Abbas9bdf5ec2008-11-07 09:58:35 -08005904 mutex_unlock(&priv->mutex);
Johannes Berg9d139c82008-07-09 14:40:37 +02005905 if (rc)
5906 return rc;
5907 }
5908
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08005909 if (!iwl_is_alive(priv))
Zhu Yi5a669262008-01-14 17:46:18 -08005910 return -EAGAIN;
5911
Zhu Yib481de92007-09-25 17:54:57 -07005912 mutex_lock(&priv->mutex);
5913
Zhu Yib481de92007-09-25 17:54:57 -07005914 if (conf->bssid)
Johannes Berge1749612008-10-27 15:59:26 -07005915 IWL_DEBUG_MAC80211("bssid: %pM\n", conf->bssid);
Zhu Yib481de92007-09-25 17:54:57 -07005916
Johannes Berg4150c572007-09-17 01:29:23 -04005917/*
5918 * very dubious code was here; the probe filtering flag is never set:
5919 *
Zhu Yib481de92007-09-25 17:54:57 -07005920 if (unlikely(test_bit(STATUS_SCANNING, &priv->status)) &&
5921 !(priv->hw->flags & IEEE80211_HW_NO_PROBE_FILTERING)) {
Johannes Berg4150c572007-09-17 01:29:23 -04005922 */
Zhu Yib481de92007-09-25 17:54:57 -07005923
Johannes Berg05c914f2008-09-11 00:01:58 +02005924 if (priv->iw_mode == NL80211_IFTYPE_AP) {
Zhu Yib481de92007-09-25 17:54:57 -07005925 if (!conf->bssid) {
5926 conf->bssid = priv->mac_addr;
5927 memcpy(priv->bssid, priv->mac_addr, ETH_ALEN);
Johannes Berge1749612008-10-27 15:59:26 -07005928 IWL_DEBUG_MAC80211("bssid was set to: %pM\n",
5929 conf->bssid);
Zhu Yib481de92007-09-25 17:54:57 -07005930 }
5931 if (priv->ibss_beacon)
5932 dev_kfree_skb(priv->ibss_beacon);
5933
Johannes Berg9d139c82008-07-09 14:40:37 +02005934 priv->ibss_beacon = ieee80211_beacon_get(hw, vif);
Zhu Yib481de92007-09-25 17:54:57 -07005935 }
5936
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08005937 if (iwl_is_rfkill(priv))
Mohamed Abbasfde35712007-11-29 11:10:15 +08005938 goto done;
5939
Zhu Yib481de92007-09-25 17:54:57 -07005940 if (conf->bssid && !is_zero_ether_addr(conf->bssid) &&
5941 !is_multicast_ether_addr(conf->bssid)) {
5942 /* If there is currently a HW scan going on in the background
5943 * then we need to cancel it else the RXON below will fail. */
Winkler, Tomasaf0053d2009-01-19 15:30:23 -08005944 if (iwl_scan_cancel_timeout(priv, 100)) {
Winkler, Tomas39aadf82008-12-19 10:37:32 +08005945 IWL_WARN(priv, "Aborted scan still in progress "
Zhu Yib481de92007-09-25 17:54:57 -07005946 "after 100ms\n");
5947 IWL_DEBUG_MAC80211("leaving - scan abort failed.\n");
5948 mutex_unlock(&priv->mutex);
5949 return -EAGAIN;
5950 }
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08005951 memcpy(priv->staging39_rxon.bssid_addr, conf->bssid, ETH_ALEN);
Zhu Yib481de92007-09-25 17:54:57 -07005952
5953 /* TODO: Audit driver for usage of these members and see
5954 * if mac80211 deprecates them (priv->bssid looks like it
5955 * shouldn't be there, but I haven't scanned the IBSS code
5956 * to verify) - jpk */
5957 memcpy(priv->bssid, conf->bssid, ETH_ALEN);
5958
Johannes Berg05c914f2008-09-11 00:01:58 +02005959 if (priv->iw_mode == NL80211_IFTYPE_AP)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005960 iwl3945_config_ap(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005961 else {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005962 rc = iwl3945_commit_rxon(priv);
Johannes Berg05c914f2008-09-11 00:01:58 +02005963 if ((priv->iw_mode == NL80211_IFTYPE_STATION) && rc)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005964 iwl3945_add_station(priv,
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08005965 priv->active39_rxon.bssid_addr, 1, 0);
Zhu Yib481de92007-09-25 17:54:57 -07005966 }
5967
5968 } else {
Winkler, Tomasaf0053d2009-01-19 15:30:23 -08005969 iwl_scan_cancel_timeout(priv, 100);
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08005970 priv->staging39_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005971 iwl3945_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005972 }
5973
Mohamed Abbasfde35712007-11-29 11:10:15 +08005974 done:
Zhu Yib481de92007-09-25 17:54:57 -07005975 IWL_DEBUG_MAC80211("leave\n");
5976 mutex_unlock(&priv->mutex);
5977
5978 return 0;
5979}
5980
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005981static void iwl3945_configure_filter(struct ieee80211_hw *hw,
Johannes Berg4150c572007-09-17 01:29:23 -04005982 unsigned int changed_flags,
5983 unsigned int *total_flags,
5984 int mc_count, struct dev_addr_list *mc_list)
5985{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08005986 struct iwl_priv *priv = hw->priv;
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08005987 __le32 *filter_flags = &priv->staging39_rxon.filter_flags;
Rick Farrington25b3f572008-06-30 17:23:28 +08005988
Zhu, Yi352bc8d2008-11-12 13:14:09 -08005989 IWL_DEBUG_MAC80211("Enter: changed: 0x%x, total: 0x%x\n",
5990 changed_flags, *total_flags);
5991
5992 if (changed_flags & (FIF_OTHER_BSS | FIF_PROMISC_IN_BSS)) {
5993 if (*total_flags & (FIF_OTHER_BSS | FIF_PROMISC_IN_BSS))
5994 *filter_flags |= RXON_FILTER_PROMISC_MSK;
5995 else
5996 *filter_flags &= ~RXON_FILTER_PROMISC_MSK;
Abhijeet Kolekar5ec03972008-05-05 10:22:48 +08005997 }
Zhu, Yi352bc8d2008-11-12 13:14:09 -08005998 if (changed_flags & FIF_ALLMULTI) {
5999 if (*total_flags & FIF_ALLMULTI)
6000 *filter_flags |= RXON_FILTER_ACCEPT_GRP_MSK;
6001 else
6002 *filter_flags &= ~RXON_FILTER_ACCEPT_GRP_MSK;
6003 }
6004 if (changed_flags & FIF_CONTROL) {
6005 if (*total_flags & FIF_CONTROL)
6006 *filter_flags |= RXON_FILTER_CTL2HOST_MSK;
6007 else
6008 *filter_flags &= ~RXON_FILTER_CTL2HOST_MSK;
6009 }
6010 if (changed_flags & FIF_BCN_PRBRESP_PROMISC) {
6011 if (*total_flags & FIF_BCN_PRBRESP_PROMISC)
6012 *filter_flags |= RXON_FILTER_BCON_AWARE_MSK;
6013 else
6014 *filter_flags &= ~RXON_FILTER_BCON_AWARE_MSK;
6015 }
6016
6017 /* We avoid iwl_commit_rxon here to commit the new filter flags
6018 * since mac80211 will call ieee80211_hw_config immediately.
6019 * (mc_list is not supported at this time). Otherwise, we need to
6020 * queue a background iwl_commit_rxon work.
6021 */
6022
6023 *total_flags &= FIF_OTHER_BSS | FIF_ALLMULTI | FIF_PROMISC_IN_BSS |
Rick Farrington25b3f572008-06-30 17:23:28 +08006024 FIF_BCN_PRBRESP_PROMISC | FIF_CONTROL;
Johannes Berg4150c572007-09-17 01:29:23 -04006025}
6026
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006027static void iwl3945_mac_remove_interface(struct ieee80211_hw *hw,
Zhu Yib481de92007-09-25 17:54:57 -07006028 struct ieee80211_if_init_conf *conf)
6029{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08006030 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07006031
6032 IWL_DEBUG_MAC80211("enter\n");
6033
6034 mutex_lock(&priv->mutex);
Mohamed Abbas6ef89d02007-10-25 17:15:47 +08006035
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08006036 if (iwl_is_ready_rf(priv)) {
Winkler, Tomasaf0053d2009-01-19 15:30:23 -08006037 iwl_scan_cancel_timeout(priv, 100);
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08006038 priv->staging39_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
Mohamed Abbasfde35712007-11-29 11:10:15 +08006039 iwl3945_commit_rxon(priv);
6040 }
Johannes Berg32bfd352007-12-19 01:31:26 +01006041 if (priv->vif == conf->vif) {
6042 priv->vif = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07006043 memset(priv->bssid, 0, ETH_ALEN);
Zhu Yib481de92007-09-25 17:54:57 -07006044 }
6045 mutex_unlock(&priv->mutex);
6046
6047 IWL_DEBUG_MAC80211("leave\n");
Zhu Yib481de92007-09-25 17:54:57 -07006048}
6049
Abhijeet Kolekarcd56d332008-09-03 11:26:21 +08006050#define IWL_DELAY_NEXT_SCAN_AFTER_ASSOC (HZ*6)
6051
6052static void iwl3945_bss_info_changed(struct ieee80211_hw *hw,
6053 struct ieee80211_vif *vif,
6054 struct ieee80211_bss_conf *bss_conf,
6055 u32 changes)
6056{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08006057 struct iwl_priv *priv = hw->priv;
Abhijeet Kolekarcd56d332008-09-03 11:26:21 +08006058
6059 IWL_DEBUG_MAC80211("changes = 0x%X\n", changes);
6060
6061 if (changes & BSS_CHANGED_ERP_PREAMBLE) {
6062 IWL_DEBUG_MAC80211("ERP_PREAMBLE %d\n",
6063 bss_conf->use_short_preamble);
6064 if (bss_conf->use_short_preamble)
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08006065 priv->staging39_rxon.flags |= RXON_FLG_SHORT_PREAMBLE_MSK;
Abhijeet Kolekarcd56d332008-09-03 11:26:21 +08006066 else
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08006067 priv->staging39_rxon.flags &= ~RXON_FLG_SHORT_PREAMBLE_MSK;
Abhijeet Kolekarcd56d332008-09-03 11:26:21 +08006068 }
6069
6070 if (changes & BSS_CHANGED_ERP_CTS_PROT) {
6071 IWL_DEBUG_MAC80211("ERP_CTS %d\n", bss_conf->use_cts_prot);
6072 if (bss_conf->use_cts_prot && (priv->band != IEEE80211_BAND_5GHZ))
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08006073 priv->staging39_rxon.flags |= RXON_FLG_TGG_PROTECT_MSK;
Abhijeet Kolekarcd56d332008-09-03 11:26:21 +08006074 else
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08006075 priv->staging39_rxon.flags &= ~RXON_FLG_TGG_PROTECT_MSK;
Abhijeet Kolekarcd56d332008-09-03 11:26:21 +08006076 }
6077
6078 if (changes & BSS_CHANGED_ASSOC) {
6079 IWL_DEBUG_MAC80211("ASSOC %d\n", bss_conf->assoc);
6080 /* This should never happen as this function should
6081 * never be called from interrupt context. */
6082 if (WARN_ON_ONCE(in_interrupt()))
6083 return;
6084 if (bss_conf->assoc) {
6085 priv->assoc_id = bss_conf->aid;
6086 priv->beacon_int = bss_conf->beacon_int;
Tomas Winkler28afaf92008-12-19 10:37:06 +08006087 priv->timestamp = bss_conf->timestamp;
Abhijeet Kolekarcd56d332008-09-03 11:26:21 +08006088 priv->assoc_capability = bss_conf->assoc_capability;
6089 priv->next_scan_jiffies = jiffies +
6090 IWL_DELAY_NEXT_SCAN_AFTER_ASSOC;
6091 mutex_lock(&priv->mutex);
6092 iwl3945_post_associate(priv);
6093 mutex_unlock(&priv->mutex);
6094 } else {
6095 priv->assoc_id = 0;
6096 IWL_DEBUG_MAC80211("DISASSOC %d\n", bss_conf->assoc);
6097 }
6098 } else if (changes && iwl3945_is_associated(priv) && priv->assoc_id) {
6099 IWL_DEBUG_MAC80211("Associated Changes %d\n", changes);
6100 iwl3945_send_rxon_assoc(priv);
6101 }
6102
6103}
6104
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006105static int iwl3945_mac_hw_scan(struct ieee80211_hw *hw, u8 *ssid, size_t len)
Zhu Yib481de92007-09-25 17:54:57 -07006106{
6107 int rc = 0;
6108 unsigned long flags;
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08006109 struct iwl_priv *priv = hw->priv;
John W. Linville9387b7c2008-09-30 20:59:05 -04006110 DECLARE_SSID_BUF(ssid_buf);
Zhu Yib481de92007-09-25 17:54:57 -07006111
6112 IWL_DEBUG_MAC80211("enter\n");
6113
Mohamed Abbas15e869d2007-10-25 17:15:46 +08006114 mutex_lock(&priv->mutex);
Zhu Yib481de92007-09-25 17:54:57 -07006115 spin_lock_irqsave(&priv->lock, flags);
6116
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08006117 if (!iwl_is_ready_rf(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07006118 rc = -EIO;
6119 IWL_DEBUG_MAC80211("leave - not ready or exit pending\n");
6120 goto out_unlock;
6121 }
6122
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08006123 /* we don't schedule scan within next_scan_jiffies period */
6124 if (priv->next_scan_jiffies &&
6125 time_after(priv->next_scan_jiffies, jiffies)) {
6126 rc = -EAGAIN;
6127 goto out_unlock;
6128 }
Bill Moss15dbf1b2008-05-06 11:05:15 +08006129 /* if we just finished scan ask for delay for a broadcast scan */
6130 if ((len == 0) && priv->last_scan_jiffies &&
6131 time_after(priv->last_scan_jiffies + IWL_DELAY_NEXT_SCAN,
6132 jiffies)) {
Zhu Yib481de92007-09-25 17:54:57 -07006133 rc = -EAGAIN;
6134 goto out_unlock;
6135 }
6136 if (len) {
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08006137 IWL_DEBUG_SCAN("direct scan for %s [%d]\n ",
John W. Linville9387b7c2008-09-30 20:59:05 -04006138 print_ssid(ssid_buf, ssid, len), (int)len);
Zhu Yib481de92007-09-25 17:54:57 -07006139
6140 priv->one_direct_scan = 1;
6141 priv->direct_ssid_len = (u8)
6142 min((u8) len, (u8) IW_ESSID_MAX_SIZE);
6143 memcpy(priv->direct_ssid, ssid, priv->direct_ssid_len);
Mohamed Abbas6ef89d02007-10-25 17:15:47 +08006144 } else
6145 priv->one_direct_scan = 0;
Zhu Yib481de92007-09-25 17:54:57 -07006146
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006147 rc = iwl3945_scan_initiate(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006148
6149 IWL_DEBUG_MAC80211("leave\n");
6150
6151out_unlock:
6152 spin_unlock_irqrestore(&priv->lock, flags);
Mohamed Abbas15e869d2007-10-25 17:15:46 +08006153 mutex_unlock(&priv->mutex);
Zhu Yib481de92007-09-25 17:54:57 -07006154
6155 return rc;
6156}
6157
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006158static int iwl3945_mac_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
Johannes Bergdc822b52008-12-29 12:55:09 +01006159 struct ieee80211_vif *vif,
6160 struct ieee80211_sta *sta,
6161 struct ieee80211_key_conf *key)
Zhu Yib481de92007-09-25 17:54:57 -07006162{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08006163 struct iwl_priv *priv = hw->priv;
Johannes Bergdc822b52008-12-29 12:55:09 +01006164 const u8 *addr;
Winkler, Tomas42986792009-01-19 15:30:22 -08006165 int ret;
Zhu Yib481de92007-09-25 17:54:57 -07006166 u8 sta_id;
6167
6168 IWL_DEBUG_MAC80211("enter\n");
6169
Kolekar, Abhijeetdf878d82008-12-19 10:37:35 +08006170 if (iwl3945_mod_params.sw_crypto) {
Zhu Yib481de92007-09-25 17:54:57 -07006171 IWL_DEBUG_MAC80211("leave - hwcrypto disabled\n");
6172 return -EOPNOTSUPP;
6173 }
6174
Winkler, Tomas42986792009-01-19 15:30:22 -08006175 addr = sta ? sta->addr : iwl_bcast_addr;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006176 sta_id = iwl3945_hw_find_station(priv, addr);
Zhu Yib481de92007-09-25 17:54:57 -07006177 if (sta_id == IWL_INVALID_STATION) {
Johannes Berge1749612008-10-27 15:59:26 -07006178 IWL_DEBUG_MAC80211("leave - %pM not in station map.\n",
6179 addr);
Zhu Yib481de92007-09-25 17:54:57 -07006180 return -EINVAL;
6181 }
6182
6183 mutex_lock(&priv->mutex);
6184
Winkler, Tomasaf0053d2009-01-19 15:30:23 -08006185 iwl_scan_cancel_timeout(priv, 100);
Mohamed Abbas15e869d2007-10-25 17:15:46 +08006186
Zhu Yib481de92007-09-25 17:54:57 -07006187 switch (cmd) {
6188 case SET_KEY:
Winkler, Tomas42986792009-01-19 15:30:22 -08006189 ret = iwl3945_update_sta_key_info(priv, key, sta_id);
6190 if (!ret) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006191 iwl3945_set_rxon_hwcrypto(priv, 1);
6192 iwl3945_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006193 key->hw_key_idx = sta_id;
6194 IWL_DEBUG_MAC80211("set_key success, using hwcrypto\n");
6195 key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
6196 }
6197 break;
6198 case DISABLE_KEY:
Winkler, Tomas42986792009-01-19 15:30:22 -08006199 ret = iwl3945_clear_sta_key_info(priv, sta_id);
6200 if (!ret) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006201 iwl3945_set_rxon_hwcrypto(priv, 0);
6202 iwl3945_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006203 IWL_DEBUG_MAC80211("disable hwcrypto key\n");
6204 }
6205 break;
6206 default:
Winkler, Tomas42986792009-01-19 15:30:22 -08006207 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07006208 }
6209
6210 IWL_DEBUG_MAC80211("leave\n");
6211 mutex_unlock(&priv->mutex);
6212
Winkler, Tomas42986792009-01-19 15:30:22 -08006213 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07006214}
6215
Johannes Berge100bb62008-04-30 18:51:21 +02006216static int iwl3945_mac_conf_tx(struct ieee80211_hw *hw, u16 queue,
Zhu Yib481de92007-09-25 17:54:57 -07006217 const struct ieee80211_tx_queue_params *params)
6218{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08006219 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07006220 unsigned long flags;
6221 int q;
Zhu Yib481de92007-09-25 17:54:57 -07006222
6223 IWL_DEBUG_MAC80211("enter\n");
6224
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08006225 if (!iwl_is_ready_rf(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07006226 IWL_DEBUG_MAC80211("leave - RF not ready\n");
6227 return -EIO;
6228 }
6229
6230 if (queue >= AC_NUM) {
6231 IWL_DEBUG_MAC80211("leave - queue >= AC_NUM %d\n", queue);
6232 return 0;
6233 }
6234
Zhu Yib481de92007-09-25 17:54:57 -07006235 q = AC_NUM - 1 - queue;
6236
6237 spin_lock_irqsave(&priv->lock, flags);
6238
6239 priv->qos_data.def_qos_parm.ac[q].cw_min = cpu_to_le16(params->cw_min);
6240 priv->qos_data.def_qos_parm.ac[q].cw_max = cpu_to_le16(params->cw_max);
6241 priv->qos_data.def_qos_parm.ac[q].aifsn = params->aifs;
6242 priv->qos_data.def_qos_parm.ac[q].edca_txop =
Johannes Berg3330d7b2008-02-10 16:49:38 +01006243 cpu_to_le16((params->txop * 32));
Zhu Yib481de92007-09-25 17:54:57 -07006244
6245 priv->qos_data.def_qos_parm.ac[q].reserved1 = 0;
6246 priv->qos_data.qos_active = 1;
6247
6248 spin_unlock_irqrestore(&priv->lock, flags);
6249
6250 mutex_lock(&priv->mutex);
Johannes Berg05c914f2008-09-11 00:01:58 +02006251 if (priv->iw_mode == NL80211_IFTYPE_AP)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006252 iwl3945_activate_qos(priv, 1);
6253 else if (priv->assoc_id && iwl3945_is_associated(priv))
6254 iwl3945_activate_qos(priv, 0);
Zhu Yib481de92007-09-25 17:54:57 -07006255
6256 mutex_unlock(&priv->mutex);
6257
Zhu Yib481de92007-09-25 17:54:57 -07006258 IWL_DEBUG_MAC80211("leave\n");
6259 return 0;
6260}
6261
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006262static int iwl3945_mac_get_tx_stats(struct ieee80211_hw *hw,
Zhu Yib481de92007-09-25 17:54:57 -07006263 struct ieee80211_tx_queue_stats *stats)
6264{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08006265 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07006266 int i, avail;
Samuel Ortiz188cf6c2008-12-22 11:31:16 +08006267 struct iwl_tx_queue *txq;
Samuel Ortizd20b3c62008-12-19 10:37:15 +08006268 struct iwl_queue *q;
Zhu Yib481de92007-09-25 17:54:57 -07006269 unsigned long flags;
6270
6271 IWL_DEBUG_MAC80211("enter\n");
6272
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08006273 if (!iwl_is_ready_rf(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07006274 IWL_DEBUG_MAC80211("leave - RF not ready\n");
6275 return -EIO;
6276 }
6277
6278 spin_lock_irqsave(&priv->lock, flags);
6279
6280 for (i = 0; i < AC_NUM; i++) {
Samuel Ortiz188cf6c2008-12-22 11:31:16 +08006281 txq = &priv->txq[i];
Zhu Yib481de92007-09-25 17:54:57 -07006282 q = &txq->q;
Samuel Ortizd20b3c62008-12-19 10:37:15 +08006283 avail = iwl_queue_space(q);
Zhu Yib481de92007-09-25 17:54:57 -07006284
Johannes Berg57ffc582008-04-29 17:18:59 +02006285 stats[i].len = q->n_window - avail;
6286 stats[i].limit = q->n_window - q->high_mark;
6287 stats[i].count = q->n_window;
Zhu Yib481de92007-09-25 17:54:57 -07006288
6289 }
6290 spin_unlock_irqrestore(&priv->lock, flags);
6291
6292 IWL_DEBUG_MAC80211("leave\n");
6293
6294 return 0;
6295}
6296
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006297static void iwl3945_mac_reset_tsf(struct ieee80211_hw *hw)
Zhu Yib481de92007-09-25 17:54:57 -07006298{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08006299 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07006300 unsigned long flags;
6301
6302 mutex_lock(&priv->mutex);
6303 IWL_DEBUG_MAC80211("enter\n");
6304
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08006305 iwl_reset_qos(priv);
Ron Rindjunsky292ae172008-02-06 11:20:39 -08006306
Zhu Yib481de92007-09-25 17:54:57 -07006307 spin_lock_irqsave(&priv->lock, flags);
6308 priv->assoc_id = 0;
6309 priv->assoc_capability = 0;
6310 priv->call_post_assoc_from_beacon = 0;
6311
6312 /* new association get rid of ibss beacon skb */
6313 if (priv->ibss_beacon)
6314 dev_kfree_skb(priv->ibss_beacon);
6315
6316 priv->ibss_beacon = NULL;
6317
6318 priv->beacon_int = priv->hw->conf.beacon_int;
Tomas Winkler28afaf92008-12-19 10:37:06 +08006319 priv->timestamp = 0;
Johannes Berg05c914f2008-09-11 00:01:58 +02006320 if ((priv->iw_mode == NL80211_IFTYPE_STATION))
Zhu Yib481de92007-09-25 17:54:57 -07006321 priv->beacon_int = 0;
6322
6323 spin_unlock_irqrestore(&priv->lock, flags);
6324
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08006325 if (!iwl_is_ready_rf(priv)) {
Mohamed Abbasfde35712007-11-29 11:10:15 +08006326 IWL_DEBUG_MAC80211("leave - not ready\n");
6327 mutex_unlock(&priv->mutex);
6328 return;
6329 }
6330
Mohamed Abbas15e869d2007-10-25 17:15:46 +08006331 /* we are restarting association process
6332 * clear RXON_FILTER_ASSOC_MSK bit
6333 */
Johannes Berg05c914f2008-09-11 00:01:58 +02006334 if (priv->iw_mode != NL80211_IFTYPE_AP) {
Winkler, Tomasaf0053d2009-01-19 15:30:23 -08006335 iwl_scan_cancel_timeout(priv, 100);
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08006336 priv->staging39_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006337 iwl3945_commit_rxon(priv);
Mohamed Abbas15e869d2007-10-25 17:15:46 +08006338 }
6339
Zhu Yib481de92007-09-25 17:54:57 -07006340 /* Per mac80211.h: This is only used in IBSS mode... */
Johannes Berg05c914f2008-09-11 00:01:58 +02006341 if (priv->iw_mode != NL80211_IFTYPE_ADHOC) {
Mohamed Abbas15e869d2007-10-25 17:15:46 +08006342
Zhu Yib481de92007-09-25 17:54:57 -07006343 IWL_DEBUG_MAC80211("leave - not in IBSS\n");
6344 mutex_unlock(&priv->mutex);
6345 return;
6346 }
6347
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006348 iwl3945_set_rate(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006349
6350 mutex_unlock(&priv->mutex);
6351
6352 IWL_DEBUG_MAC80211("leave\n");
6353
6354}
6355
Johannes Berge039fa42008-05-15 12:55:29 +02006356static int iwl3945_mac_beacon_update(struct ieee80211_hw *hw, struct sk_buff *skb)
Zhu Yib481de92007-09-25 17:54:57 -07006357{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08006358 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07006359 unsigned long flags;
6360
Zhu Yib481de92007-09-25 17:54:57 -07006361 IWL_DEBUG_MAC80211("enter\n");
6362
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08006363 if (!iwl_is_ready_rf(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07006364 IWL_DEBUG_MAC80211("leave - RF not ready\n");
Zhu Yib481de92007-09-25 17:54:57 -07006365 return -EIO;
6366 }
6367
Johannes Berg05c914f2008-09-11 00:01:58 +02006368 if (priv->iw_mode != NL80211_IFTYPE_ADHOC) {
Zhu Yib481de92007-09-25 17:54:57 -07006369 IWL_DEBUG_MAC80211("leave - not IBSS\n");
Zhu Yib481de92007-09-25 17:54:57 -07006370 return -EIO;
6371 }
6372
6373 spin_lock_irqsave(&priv->lock, flags);
6374
6375 if (priv->ibss_beacon)
6376 dev_kfree_skb(priv->ibss_beacon);
6377
6378 priv->ibss_beacon = skb;
6379
6380 priv->assoc_id = 0;
6381
6382 IWL_DEBUG_MAC80211("leave\n");
6383 spin_unlock_irqrestore(&priv->lock, flags);
6384
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08006385 iwl_reset_qos(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006386
Abhijeet Kolekardc4b1e72008-09-03 11:26:30 +08006387 iwl3945_post_associate(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006388
Zhu Yib481de92007-09-25 17:54:57 -07006389
6390 return 0;
6391}
6392
6393/*****************************************************************************
6394 *
6395 * sysfs attributes
6396 *
6397 *****************************************************************************/
6398
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08006399#ifdef CONFIG_IWL3945_DEBUG
Zhu Yib481de92007-09-25 17:54:57 -07006400
6401/*
6402 * The following adds a new attribute to the sysfs representation
6403 * of this device driver (i.e. a new file in /sys/bus/pci/drivers/iwl/)
6404 * used for controlling the debug level.
6405 *
6406 * See the level definitions in iwl for details.
6407 */
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08006408static ssize_t show_debug_level(struct device *d,
6409 struct device_attribute *attr, char *buf)
Zhu Yib481de92007-09-25 17:54:57 -07006410{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08006411 struct iwl_priv *priv = d->driver_data;
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08006412
6413 return sprintf(buf, "0x%08X\n", priv->debug_level);
Zhu Yib481de92007-09-25 17:54:57 -07006414}
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08006415static ssize_t store_debug_level(struct device *d,
6416 struct device_attribute *attr,
Zhu Yib481de92007-09-25 17:54:57 -07006417 const char *buf, size_t count)
6418{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08006419 struct iwl_priv *priv = d->driver_data;
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08006420 unsigned long val;
6421 int ret;
Zhu Yib481de92007-09-25 17:54:57 -07006422
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08006423 ret = strict_strtoul(buf, 0, &val);
6424 if (ret)
Tomas Winkler978785a2008-12-19 10:37:31 +08006425 IWL_INFO(priv, "%s is not in hex or decimal form.\n", buf);
Zhu Yib481de92007-09-25 17:54:57 -07006426 else
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08006427 priv->debug_level = val;
Zhu Yib481de92007-09-25 17:54:57 -07006428
6429 return strnlen(buf, count);
6430}
6431
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08006432static DEVICE_ATTR(debug_level, S_IWUSR | S_IRUGO,
6433 show_debug_level, store_debug_level);
Zhu Yib481de92007-09-25 17:54:57 -07006434
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08006435#endif /* CONFIG_IWL3945_DEBUG */
Zhu Yib481de92007-09-25 17:54:57 -07006436
Zhu Yib481de92007-09-25 17:54:57 -07006437static ssize_t show_temperature(struct device *d,
6438 struct device_attribute *attr, char *buf)
6439{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08006440 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07006441
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08006442 if (!iwl_is_alive(priv))
Zhu Yib481de92007-09-25 17:54:57 -07006443 return -EAGAIN;
6444
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006445 return sprintf(buf, "%d\n", iwl3945_hw_get_temperature(priv));
Zhu Yib481de92007-09-25 17:54:57 -07006446}
6447
6448static DEVICE_ATTR(temperature, S_IRUGO, show_temperature, NULL);
6449
Zhu Yib481de92007-09-25 17:54:57 -07006450static ssize_t show_tx_power(struct device *d,
6451 struct device_attribute *attr, char *buf)
6452{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08006453 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07006454 return sprintf(buf, "%d\n", priv->user_txpower_limit);
6455}
6456
6457static ssize_t store_tx_power(struct device *d,
6458 struct device_attribute *attr,
6459 const char *buf, size_t count)
6460{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08006461 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07006462 char *p = (char *)buf;
6463 u32 val;
6464
6465 val = simple_strtoul(p, &p, 10);
6466 if (p == buf)
Tomas Winkler978785a2008-12-19 10:37:31 +08006467 IWL_INFO(priv, ": %s is not in decimal form.\n", buf);
Zhu Yib481de92007-09-25 17:54:57 -07006468 else
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006469 iwl3945_hw_reg_set_txpower(priv, val);
Zhu Yib481de92007-09-25 17:54:57 -07006470
6471 return count;
6472}
6473
6474static DEVICE_ATTR(tx_power, S_IWUSR | S_IRUGO, show_tx_power, store_tx_power);
6475
6476static ssize_t show_flags(struct device *d,
6477 struct device_attribute *attr, char *buf)
6478{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08006479 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07006480
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08006481 return sprintf(buf, "0x%04X\n", priv->active39_rxon.flags);
Zhu Yib481de92007-09-25 17:54:57 -07006482}
6483
6484static ssize_t store_flags(struct device *d,
6485 struct device_attribute *attr,
6486 const char *buf, size_t count)
6487{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08006488 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07006489 u32 flags = simple_strtoul(buf, NULL, 0);
6490
6491 mutex_lock(&priv->mutex);
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08006492 if (le32_to_cpu(priv->staging39_rxon.flags) != flags) {
Zhu Yib481de92007-09-25 17:54:57 -07006493 /* Cancel any currently running scans... */
Winkler, Tomasaf0053d2009-01-19 15:30:23 -08006494 if (iwl_scan_cancel_timeout(priv, 100))
Winkler, Tomas39aadf82008-12-19 10:37:32 +08006495 IWL_WARN(priv, "Could not cancel scan.\n");
Zhu Yib481de92007-09-25 17:54:57 -07006496 else {
6497 IWL_DEBUG_INFO("Committing rxon.flags = 0x%04X\n",
6498 flags);
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08006499 priv->staging39_rxon.flags = cpu_to_le32(flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006500 iwl3945_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006501 }
6502 }
6503 mutex_unlock(&priv->mutex);
6504
6505 return count;
6506}
6507
6508static DEVICE_ATTR(flags, S_IWUSR | S_IRUGO, show_flags, store_flags);
6509
6510static ssize_t show_filter_flags(struct device *d,
6511 struct device_attribute *attr, char *buf)
6512{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08006513 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07006514
6515 return sprintf(buf, "0x%04X\n",
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08006516 le32_to_cpu(priv->active39_rxon.filter_flags));
Zhu Yib481de92007-09-25 17:54:57 -07006517}
6518
6519static ssize_t store_filter_flags(struct device *d,
6520 struct device_attribute *attr,
6521 const char *buf, size_t count)
6522{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08006523 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07006524 u32 filter_flags = simple_strtoul(buf, NULL, 0);
6525
6526 mutex_lock(&priv->mutex);
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08006527 if (le32_to_cpu(priv->staging39_rxon.filter_flags) != filter_flags) {
Zhu Yib481de92007-09-25 17:54:57 -07006528 /* Cancel any currently running scans... */
Winkler, Tomasaf0053d2009-01-19 15:30:23 -08006529 if (iwl_scan_cancel_timeout(priv, 100))
Winkler, Tomas39aadf82008-12-19 10:37:32 +08006530 IWL_WARN(priv, "Could not cancel scan.\n");
Zhu Yib481de92007-09-25 17:54:57 -07006531 else {
6532 IWL_DEBUG_INFO("Committing rxon.filter_flags = "
6533 "0x%04X\n", filter_flags);
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08006534 priv->staging39_rxon.filter_flags =
Zhu Yib481de92007-09-25 17:54:57 -07006535 cpu_to_le32(filter_flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006536 iwl3945_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006537 }
6538 }
6539 mutex_unlock(&priv->mutex);
6540
6541 return count;
6542}
6543
6544static DEVICE_ATTR(filter_flags, S_IWUSR | S_IRUGO, show_filter_flags,
6545 store_filter_flags);
6546
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08006547#ifdef CONFIG_IWL3945_SPECTRUM_MEASUREMENT
Zhu Yib481de92007-09-25 17:54:57 -07006548
6549static ssize_t show_measurement(struct device *d,
6550 struct device_attribute *attr, char *buf)
6551{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08006552 struct iwl_priv *priv = dev_get_drvdata(d);
Tomas Winkler600c0e12008-12-19 10:37:04 +08006553 struct iwl_spectrum_notification measure_report;
Zhu Yib481de92007-09-25 17:54:57 -07006554 u32 size = sizeof(measure_report), len = 0, ofs = 0;
Tomas Winkler3ac7f142008-07-21 02:40:14 +03006555 u8 *data = (u8 *)&measure_report;
Zhu Yib481de92007-09-25 17:54:57 -07006556 unsigned long flags;
6557
6558 spin_lock_irqsave(&priv->lock, flags);
6559 if (!(priv->measurement_status & MEASUREMENT_READY)) {
6560 spin_unlock_irqrestore(&priv->lock, flags);
6561 return 0;
6562 }
6563 memcpy(&measure_report, &priv->measure_report, size);
6564 priv->measurement_status = 0;
6565 spin_unlock_irqrestore(&priv->lock, flags);
6566
6567 while (size && (PAGE_SIZE - len)) {
6568 hex_dump_to_buffer(data + ofs, size, 16, 1, buf + len,
6569 PAGE_SIZE - len, 1);
6570 len = strlen(buf);
6571 if (PAGE_SIZE - len)
6572 buf[len++] = '\n';
6573
6574 ofs += 16;
6575 size -= min(size, 16U);
6576 }
6577
6578 return len;
6579}
6580
6581static ssize_t store_measurement(struct device *d,
6582 struct device_attribute *attr,
6583 const char *buf, size_t count)
6584{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08006585 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07006586 struct ieee80211_measurement_params params = {
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08006587 .channel = le16_to_cpu(priv->active39_rxon.channel),
Zhu Yib481de92007-09-25 17:54:57 -07006588 .start_time = cpu_to_le64(priv->last_tsf),
6589 .duration = cpu_to_le16(1),
6590 };
6591 u8 type = IWL_MEASURE_BASIC;
6592 u8 buffer[32];
6593 u8 channel;
6594
6595 if (count) {
6596 char *p = buffer;
6597 strncpy(buffer, buf, min(sizeof(buffer), count));
6598 channel = simple_strtoul(p, NULL, 0);
6599 if (channel)
6600 params.channel = channel;
6601
6602 p = buffer;
6603 while (*p && *p != ' ')
6604 p++;
6605 if (*p)
6606 type = simple_strtoul(p + 1, NULL, 0);
6607 }
6608
6609 IWL_DEBUG_INFO("Invoking measurement of type %d on "
6610 "channel %d (for '%s')\n", type, params.channel, buf);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006611 iwl3945_get_measurement(priv, &params, type);
Zhu Yib481de92007-09-25 17:54:57 -07006612
6613 return count;
6614}
6615
6616static DEVICE_ATTR(measurement, S_IRUSR | S_IWUSR,
6617 show_measurement, store_measurement);
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08006618#endif /* CONFIG_IWL3945_SPECTRUM_MEASUREMENT */
Zhu Yib481de92007-09-25 17:54:57 -07006619
Zhu Yib481de92007-09-25 17:54:57 -07006620static ssize_t store_retry_rate(struct device *d,
6621 struct device_attribute *attr,
6622 const char *buf, size_t count)
6623{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08006624 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07006625
6626 priv->retry_rate = simple_strtoul(buf, NULL, 0);
6627 if (priv->retry_rate <= 0)
6628 priv->retry_rate = 1;
6629
6630 return count;
6631}
6632
6633static ssize_t show_retry_rate(struct device *d,
6634 struct device_attribute *attr, char *buf)
6635{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08006636 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07006637 return sprintf(buf, "%d", priv->retry_rate);
6638}
6639
6640static DEVICE_ATTR(retry_rate, S_IWUSR | S_IRUSR, show_retry_rate,
6641 store_retry_rate);
6642
6643static ssize_t store_power_level(struct device *d,
6644 struct device_attribute *attr,
6645 const char *buf, size_t count)
6646{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08006647 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07006648 int rc;
6649 int mode;
6650
6651 mode = simple_strtoul(buf, NULL, 0);
6652 mutex_lock(&priv->mutex);
6653
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08006654 if (!iwl_is_ready(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07006655 rc = -EAGAIN;
6656 goto out;
6657 }
6658
Samuel Ortiz1125eff2008-12-19 10:37:14 +08006659 if ((mode < 1) || (mode > IWL39_POWER_LIMIT) ||
6660 (mode == IWL39_POWER_AC))
6661 mode = IWL39_POWER_AC;
Zhu Yib481de92007-09-25 17:54:57 -07006662 else
6663 mode |= IWL_POWER_ENABLED;
6664
6665 if (mode != priv->power_mode) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006666 rc = iwl3945_send_power_mode(priv, IWL_POWER_LEVEL(mode));
Zhu Yib481de92007-09-25 17:54:57 -07006667 if (rc) {
6668 IWL_DEBUG_MAC80211("failed setting power mode.\n");
6669 goto out;
6670 }
6671 priv->power_mode = mode;
6672 }
6673
6674 rc = count;
6675
6676 out:
6677 mutex_unlock(&priv->mutex);
6678 return rc;
6679}
6680
6681#define MAX_WX_STRING 80
6682
6683/* Values are in microsecond */
6684static const s32 timeout_duration[] = {
6685 350000,
6686 250000,
6687 75000,
6688 37000,
6689 25000,
6690};
6691static const s32 period_duration[] = {
6692 400000,
6693 700000,
6694 1000000,
6695 1000000,
6696 1000000
6697};
6698
6699static ssize_t show_power_level(struct device *d,
6700 struct device_attribute *attr, char *buf)
6701{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08006702 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07006703 int level = IWL_POWER_LEVEL(priv->power_mode);
6704 char *p = buf;
6705
6706 p += sprintf(p, "%d ", level);
6707 switch (level) {
6708 case IWL_POWER_MODE_CAM:
Samuel Ortiz1125eff2008-12-19 10:37:14 +08006709 case IWL39_POWER_AC:
Zhu Yib481de92007-09-25 17:54:57 -07006710 p += sprintf(p, "(AC)");
6711 break;
Samuel Ortiz1125eff2008-12-19 10:37:14 +08006712 case IWL39_POWER_BATTERY:
Zhu Yib481de92007-09-25 17:54:57 -07006713 p += sprintf(p, "(BATTERY)");
6714 break;
6715 default:
6716 p += sprintf(p,
6717 "(Timeout %dms, Period %dms)",
6718 timeout_duration[level - 1] / 1000,
6719 period_duration[level - 1] / 1000);
6720 }
6721
6722 if (!(priv->power_mode & IWL_POWER_ENABLED))
6723 p += sprintf(p, " OFF\n");
6724 else
6725 p += sprintf(p, " \n");
6726
Tomas Winkler3ac7f142008-07-21 02:40:14 +03006727 return p - buf + 1;
Zhu Yib481de92007-09-25 17:54:57 -07006728
6729}
6730
6731static DEVICE_ATTR(power_level, S_IWUSR | S_IRUSR, show_power_level,
6732 store_power_level);
6733
6734static ssize_t show_channels(struct device *d,
6735 struct device_attribute *attr, char *buf)
6736{
Johannes Berg8318d782008-01-24 19:38:38 +01006737 /* all this shit doesn't belong into sysfs anyway */
6738 return 0;
Zhu Yib481de92007-09-25 17:54:57 -07006739}
6740
6741static DEVICE_ATTR(channels, S_IRUSR, show_channels, NULL);
6742
6743static ssize_t show_statistics(struct device *d,
6744 struct device_attribute *attr, char *buf)
6745{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08006746 struct iwl_priv *priv = dev_get_drvdata(d);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006747 u32 size = sizeof(struct iwl3945_notif_statistics);
Zhu Yib481de92007-09-25 17:54:57 -07006748 u32 len = 0, ofs = 0;
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08006749 u8 *data = (u8 *)&priv->statistics_39;
Zhu Yib481de92007-09-25 17:54:57 -07006750 int rc = 0;
6751
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08006752 if (!iwl_is_alive(priv))
Zhu Yib481de92007-09-25 17:54:57 -07006753 return -EAGAIN;
6754
6755 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006756 rc = iwl3945_send_statistics_request(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006757 mutex_unlock(&priv->mutex);
6758
6759 if (rc) {
6760 len = sprintf(buf,
6761 "Error sending statistics request: 0x%08X\n", rc);
6762 return len;
6763 }
6764
6765 while (size && (PAGE_SIZE - len)) {
6766 hex_dump_to_buffer(data + ofs, size, 16, 1, buf + len,
6767 PAGE_SIZE - len, 1);
6768 len = strlen(buf);
6769 if (PAGE_SIZE - len)
6770 buf[len++] = '\n';
6771
6772 ofs += 16;
6773 size -= min(size, 16U);
6774 }
6775
6776 return len;
6777}
6778
6779static DEVICE_ATTR(statistics, S_IRUGO, show_statistics, NULL);
6780
6781static ssize_t show_antenna(struct device *d,
6782 struct device_attribute *attr, char *buf)
6783{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08006784 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07006785
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08006786 if (!iwl_is_alive(priv))
Zhu Yib481de92007-09-25 17:54:57 -07006787 return -EAGAIN;
6788
6789 return sprintf(buf, "%d\n", priv->antenna);
6790}
6791
6792static ssize_t store_antenna(struct device *d,
6793 struct device_attribute *attr,
6794 const char *buf, size_t count)
6795{
6796 int ant;
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08006797 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07006798
6799 if (count == 0)
6800 return 0;
6801
6802 if (sscanf(buf, "%1i", &ant) != 1) {
6803 IWL_DEBUG_INFO("not in hex or decimal form.\n");
6804 return count;
6805 }
6806
6807 if ((ant >= 0) && (ant <= 2)) {
6808 IWL_DEBUG_INFO("Setting antenna select to %d.\n", ant);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006809 priv->antenna = (enum iwl3945_antenna)ant;
Zhu Yib481de92007-09-25 17:54:57 -07006810 } else
6811 IWL_DEBUG_INFO("Bad antenna select value %d.\n", ant);
6812
6813
6814 return count;
6815}
6816
6817static DEVICE_ATTR(antenna, S_IWUSR | S_IRUGO, show_antenna, store_antenna);
6818
6819static ssize_t show_status(struct device *d,
6820 struct device_attribute *attr, char *buf)
6821{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08006822 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08006823 if (!iwl_is_alive(priv))
Zhu Yib481de92007-09-25 17:54:57 -07006824 return -EAGAIN;
6825 return sprintf(buf, "0x%08x\n", (int)priv->status);
6826}
6827
6828static DEVICE_ATTR(status, S_IRUGO, show_status, NULL);
6829
6830static ssize_t dump_error_log(struct device *d,
6831 struct device_attribute *attr,
6832 const char *buf, size_t count)
6833{
6834 char *p = (char *)buf;
6835
6836 if (p[0] == '1')
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08006837 iwl3945_dump_nic_error_log((struct iwl_priv *)d->driver_data);
Zhu Yib481de92007-09-25 17:54:57 -07006838
6839 return strnlen(buf, count);
6840}
6841
6842static DEVICE_ATTR(dump_errors, S_IWUSR, NULL, dump_error_log);
6843
6844static ssize_t dump_event_log(struct device *d,
6845 struct device_attribute *attr,
6846 const char *buf, size_t count)
6847{
6848 char *p = (char *)buf;
6849
6850 if (p[0] == '1')
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08006851 iwl3945_dump_nic_event_log((struct iwl_priv *)d->driver_data);
Zhu Yib481de92007-09-25 17:54:57 -07006852
6853 return strnlen(buf, count);
6854}
6855
6856static DEVICE_ATTR(dump_events, S_IWUSR, NULL, dump_event_log);
6857
6858/*****************************************************************************
6859 *
Tomas Winklera96a27f2008-10-23 23:48:56 -07006860 * driver setup and tear down
Zhu Yib481de92007-09-25 17:54:57 -07006861 *
6862 *****************************************************************************/
6863
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08006864static void iwl3945_setup_deferred_work(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07006865{
6866 priv->workqueue = create_workqueue(DRV_NAME);
6867
6868 init_waitqueue_head(&priv->wait_command_queue);
6869
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006870 INIT_WORK(&priv->up, iwl3945_bg_up);
6871 INIT_WORK(&priv->restart, iwl3945_bg_restart);
6872 INIT_WORK(&priv->rx_replenish, iwl3945_bg_rx_replenish);
6873 INIT_WORK(&priv->scan_completed, iwl3945_bg_scan_completed);
6874 INIT_WORK(&priv->request_scan, iwl3945_bg_request_scan);
6875 INIT_WORK(&priv->abort_scan, iwl3945_bg_abort_scan);
6876 INIT_WORK(&priv->rf_kill, iwl3945_bg_rf_kill);
6877 INIT_WORK(&priv->beacon_update, iwl3945_bg_beacon_update);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006878 INIT_DELAYED_WORK(&priv->init_alive_start, iwl3945_bg_init_alive_start);
6879 INIT_DELAYED_WORK(&priv->alive_start, iwl3945_bg_alive_start);
6880 INIT_DELAYED_WORK(&priv->scan_check, iwl3945_bg_scan_check);
Helmut Schaa26635162009-01-15 09:38:44 +01006881 INIT_DELAYED_WORK(&priv->rfkill_poll, iwl3945_rfkill_poll);
Zhu Yib481de92007-09-25 17:54:57 -07006882
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006883 iwl3945_hw_setup_deferred_work(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006884
6885 tasklet_init(&priv->irq_tasklet, (void (*)(unsigned long))
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006886 iwl3945_irq_tasklet, (unsigned long)priv);
Zhu Yib481de92007-09-25 17:54:57 -07006887}
6888
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08006889static void iwl3945_cancel_deferred_work(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07006890{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006891 iwl3945_hw_cancel_deferred_work(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006892
Joonwoo Parke47eb6a2007-11-29 10:42:49 +09006893 cancel_delayed_work_sync(&priv->init_alive_start);
Zhu Yib481de92007-09-25 17:54:57 -07006894 cancel_delayed_work(&priv->scan_check);
6895 cancel_delayed_work(&priv->alive_start);
Zhu Yib481de92007-09-25 17:54:57 -07006896 cancel_work_sync(&priv->beacon_update);
6897}
6898
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006899static struct attribute *iwl3945_sysfs_entries[] = {
Zhu Yib481de92007-09-25 17:54:57 -07006900 &dev_attr_antenna.attr,
6901 &dev_attr_channels.attr,
6902 &dev_attr_dump_errors.attr,
6903 &dev_attr_dump_events.attr,
6904 &dev_attr_flags.attr,
6905 &dev_attr_filter_flags.attr,
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08006906#ifdef CONFIG_IWL3945_SPECTRUM_MEASUREMENT
Zhu Yib481de92007-09-25 17:54:57 -07006907 &dev_attr_measurement.attr,
6908#endif
6909 &dev_attr_power_level.attr,
Zhu Yib481de92007-09-25 17:54:57 -07006910 &dev_attr_retry_rate.attr,
Zhu Yib481de92007-09-25 17:54:57 -07006911 &dev_attr_statistics.attr,
6912 &dev_attr_status.attr,
6913 &dev_attr_temperature.attr,
Zhu Yib481de92007-09-25 17:54:57 -07006914 &dev_attr_tx_power.attr,
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08006915#ifdef CONFIG_IWL3945_DEBUG
6916 &dev_attr_debug_level.attr,
6917#endif
Zhu Yib481de92007-09-25 17:54:57 -07006918 NULL
6919};
6920
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006921static struct attribute_group iwl3945_attribute_group = {
Zhu Yib481de92007-09-25 17:54:57 -07006922 .name = NULL, /* put in device directory */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006923 .attrs = iwl3945_sysfs_entries,
Zhu Yib481de92007-09-25 17:54:57 -07006924};
6925
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006926static struct ieee80211_ops iwl3945_hw_ops = {
6927 .tx = iwl3945_mac_tx,
6928 .start = iwl3945_mac_start,
6929 .stop = iwl3945_mac_stop,
6930 .add_interface = iwl3945_mac_add_interface,
6931 .remove_interface = iwl3945_mac_remove_interface,
6932 .config = iwl3945_mac_config,
6933 .config_interface = iwl3945_mac_config_interface,
6934 .configure_filter = iwl3945_configure_filter,
6935 .set_key = iwl3945_mac_set_key,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006936 .get_tx_stats = iwl3945_mac_get_tx_stats,
6937 .conf_tx = iwl3945_mac_conf_tx,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006938 .reset_tsf = iwl3945_mac_reset_tsf,
Abhijeet Kolekarcd56d332008-09-03 11:26:21 +08006939 .bss_info_changed = iwl3945_bss_info_changed,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006940 .hw_scan = iwl3945_mac_hw_scan
Zhu Yib481de92007-09-25 17:54:57 -07006941};
6942
Winkler, Tomase52119c2008-12-22 11:31:19 +08006943static int iwl3945_init_drv(struct iwl_priv *priv)
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08006944{
6945 int ret;
6946
6947 priv->retry_rate = 1;
6948 priv->ibss_beacon = NULL;
6949
6950 spin_lock_init(&priv->lock);
Samuel Ortizc7a7c8e2009-01-08 10:20:01 -08006951 spin_lock_init(&priv->power_data_39.lock);
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08006952 spin_lock_init(&priv->sta_lock);
6953 spin_lock_init(&priv->hcmd_lock);
6954
6955 INIT_LIST_HEAD(&priv->free_frames);
6956
6957 mutex_init(&priv->mutex);
6958
6959 /* Clear the driver's (not device's) station table */
6960 iwl3945_clear_stations_table(priv);
6961
6962 priv->data_retry_limit = -1;
6963 priv->ieee_channels = NULL;
6964 priv->ieee_rates = NULL;
6965 priv->band = IEEE80211_BAND_2GHZ;
6966
6967 priv->iw_mode = NL80211_IFTYPE_STATION;
6968
6969 iwl_reset_qos(priv);
6970
6971 priv->qos_data.qos_active = 0;
6972 priv->qos_data.qos_cap.val = 0;
6973
6974 priv->rates_mask = IWL_RATES_MASK;
6975 /* If power management is turned on, default to AC mode */
Samuel Ortizc7a7c8e2009-01-08 10:20:01 -08006976 priv->power_mode = IWL39_POWER_AC;
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08006977 priv->user_txpower_limit = IWL_DEFAULT_TX_POWER;
6978
6979 ret = iwl3945_init_channel_map(priv);
6980 if (ret) {
6981 IWL_ERR(priv, "initializing regulatory failed: %d\n", ret);
6982 goto err;
6983 }
6984
6985 ret = iwl3945_init_geos(priv);
6986 if (ret) {
6987 IWL_ERR(priv, "initializing geos failed: %d\n", ret);
6988 goto err_free_channel_map;
6989 }
6990
6991 return 0;
6992
6993err_free_channel_map:
6994 iwl3945_free_channel_map(priv);
6995err:
6996 return ret;
6997}
6998
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006999static int iwl3945_pci_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
Zhu Yib481de92007-09-25 17:54:57 -07007000{
7001 int err = 0;
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08007002 struct iwl_priv *priv;
Zhu Yib481de92007-09-25 17:54:57 -07007003 struct ieee80211_hw *hw;
Kolekar, Abhijeetc0f20d92008-12-19 10:37:19 +08007004 struct iwl_cfg *cfg = (struct iwl_cfg *)(ent->driver_data);
Mohamed Abbas0359fac2008-03-28 16:21:08 -07007005 unsigned long flags;
Zhu Yib481de92007-09-25 17:54:57 -07007006
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08007007 /***********************
7008 * 1. Allocating HW data
7009 * ********************/
7010
Zhu Yib481de92007-09-25 17:54:57 -07007011 /* mac80211 allocates memory for this device instance, including
7012 * space for this driver's private structure */
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08007013 hw = iwl_alloc_all(cfg, &iwl3945_hw_ops);
Zhu Yib481de92007-09-25 17:54:57 -07007014 if (hw == NULL) {
Samuel Ortiza3139c52008-12-19 10:37:09 +08007015 printk(KERN_ERR DRV_NAME "Can not allocate network device\n");
Zhu Yib481de92007-09-25 17:54:57 -07007016 err = -ENOMEM;
7017 goto out;
7018 }
Zhu Yib481de92007-09-25 17:54:57 -07007019 priv = hw->priv;
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08007020 SET_IEEE80211_DEV(hw, &pdev->dev);
Cahill, Ben M6440adb2007-11-29 11:09:55 +08007021
Kolekar, Abhijeetdf878d82008-12-19 10:37:35 +08007022 if ((iwl3945_mod_params.num_of_queues > IWL39_MAX_NUM_QUEUES) ||
7023 (iwl3945_mod_params.num_of_queues < IWL_MIN_NUM_QUEUES)) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08007024 IWL_ERR(priv,
7025 "invalid queues_num, should be between %d and %d\n",
7026 IWL_MIN_NUM_QUEUES, IWL39_MAX_NUM_QUEUES);
Samuel Ortiza3139c52008-12-19 10:37:09 +08007027 err = -EINVAL;
7028 goto out;
7029 }
7030
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08007031 /*
7032 * Disabling hardware scan means that mac80211 will perform scans
7033 * "the hard way", rather than using device's scan.
7034 */
Kolekar, Abhijeetdf878d82008-12-19 10:37:35 +08007035 if (iwl3945_mod_params.disable_hw_scan) {
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08007036 IWL_DEBUG_INFO("Disabling hw_scan\n");
7037 iwl3945_hw_ops.hw_scan = NULL;
7038 }
7039
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08007040
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08007041 IWL_DEBUG_INFO("*** LOAD DRIVER ***\n");
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08007042 priv->cfg = cfg;
7043 priv->pci_dev = pdev;
7044
7045#ifdef CONFIG_IWL3945_DEBUG
7046 priv->debug_level = iwl3945_mod_params.debug;
7047 atomic_set(&priv->restrict_refcnt, 0);
7048#endif
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08007049 hw->rate_control_algorithm = "iwl-3945-rs";
7050 hw->sta_data_size = sizeof(struct iwl3945_sta_priv);
7051
Cahill, Ben M6440adb2007-11-29 11:09:55 +08007052 /* Select antenna (may be helpful if only one antenna is connected) */
Kolekar, Abhijeetdf878d82008-12-19 10:37:35 +08007053 priv->antenna = (enum iwl3945_antenna)iwl3945_mod_params.antenna;
Zhu Yib481de92007-09-25 17:54:57 -07007054
Bruno Randolf566bfe52008-05-08 19:15:40 +02007055 /* Tell mac80211 our characteristics */
Johannes Berg605a0bd2008-07-15 10:10:01 +02007056 hw->flags = IEEE80211_HW_SIGNAL_DBM |
Bruno Randolf566bfe52008-05-08 19:15:40 +02007057 IEEE80211_HW_NOISE_DBM;
Zhu Yib481de92007-09-25 17:54:57 -07007058
Luis R. Rodriguezf59ac042008-08-29 16:26:43 -07007059 hw->wiphy->interface_modes =
Luis R. Rodriguezf59ac042008-08-29 16:26:43 -07007060 BIT(NL80211_IFTYPE_STATION) |
7061 BIT(NL80211_IFTYPE_ADHOC);
7062
Luis R. Rodriguezea4a82dc2008-11-12 14:22:04 -08007063 hw->wiphy->fw_handles_regulatory = true;
7064
Cahill, Ben M6440adb2007-11-29 11:09:55 +08007065 /* 4 EDCA QOS priorities */
Zhu Yib481de92007-09-25 17:54:57 -07007066 hw->queues = 4;
7067
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08007068 /***************************
7069 * 2. Initializing PCI bus
7070 * *************************/
Zhu Yib481de92007-09-25 17:54:57 -07007071 if (pci_enable_device(pdev)) {
7072 err = -ENODEV;
7073 goto out_ieee80211_free_hw;
7074 }
7075
7076 pci_set_master(pdev);
7077
Zhu Yib481de92007-09-25 17:54:57 -07007078 err = pci_set_dma_mask(pdev, DMA_32BIT_MASK);
7079 if (!err)
7080 err = pci_set_consistent_dma_mask(pdev, DMA_32BIT_MASK);
7081 if (err) {
Tomas Winkler978785a2008-12-19 10:37:31 +08007082 IWL_WARN(priv, "No suitable DMA available.\n");
Zhu Yib481de92007-09-25 17:54:57 -07007083 goto out_pci_disable_device;
7084 }
7085
7086 pci_set_drvdata(pdev, priv);
7087 err = pci_request_regions(pdev, DRV_NAME);
7088 if (err)
7089 goto out_pci_disable_device;
Cahill, Ben M6440adb2007-11-29 11:09:55 +08007090
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08007091 /***********************
7092 * 3. Read REV Register
7093 * ********************/
Zhu Yib481de92007-09-25 17:54:57 -07007094 priv->hw_base = pci_iomap(pdev, 0, 0);
7095 if (!priv->hw_base) {
7096 err = -ENODEV;
7097 goto out_pci_release_regions;
7098 }
7099
7100 IWL_DEBUG_INFO("pci_resource_len = 0x%08llx\n",
7101 (unsigned long long) pci_resource_len(pdev, 0));
7102 IWL_DEBUG_INFO("pci_resource_base = %p\n", priv->hw_base);
7103
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08007104 /* We disable the RETRY_TIMEOUT register (0x41) to keep
7105 * PCI Tx retries from interfering with C3 CPU state */
7106 pci_write_config_byte(pdev, 0x41, 0x00);
Zhu Yib481de92007-09-25 17:54:57 -07007107
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08007108 /* amp init */
7109 err = priv->cfg->ops->lib->apm_ops.init(priv);
Tomas Winkler3ac7f142008-07-21 02:40:14 +03007110 if (err < 0) {
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08007111 IWL_DEBUG_INFO("Failed to init APMG\n");
7112 goto out_iounmap;
Tomas Winkler3ac7f142008-07-21 02:40:14 +03007113 }
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08007114
7115 /***********************
7116 * 4. Read EEPROM
7117 * ********************/
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08007118
Zhu Yi5a669262008-01-14 17:46:18 -08007119 /* Read the EEPROM */
7120 err = iwl3945_eeprom_init(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007121 if (err) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08007122 IWL_ERR(priv, "Unable to init EEPROM\n");
Zhu Yi5a669262008-01-14 17:46:18 -08007123 goto out_remove_sysfs;
7124 }
7125 /* MAC Address location in EEPROM same for 3945/4965 */
7126 get_eeprom_mac(priv, priv->mac_addr);
Johannes Berge1749612008-10-27 15:59:26 -07007127 IWL_DEBUG_INFO("MAC address: %pM\n", priv->mac_addr);
Zhu Yi5a669262008-01-14 17:46:18 -08007128 SET_IEEE80211_PERM_ADDR(priv->hw, priv->mac_addr);
7129
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08007130 /***********************
7131 * 5. Setup HW Constants
7132 * ********************/
7133 /* Device-specific setup */
Abhijeet Kolekar3832ec92008-12-19 10:37:26 +08007134 if (iwl3945_hw_set_hw_params(priv)) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08007135 IWL_ERR(priv, "failed to set hw settings\n");
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08007136 goto out_iounmap;
7137 }
7138
7139 /***********************
7140 * 6. Setup priv
7141 * ********************/
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08007142
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08007143 err = iwl3945_init_drv(priv);
Reinette Chatre849e0dc2008-01-23 10:15:18 -08007144 if (err) {
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08007145 IWL_ERR(priv, "initializing driver failed\n");
7146 goto out_free_geos;
Reinette Chatre849e0dc2008-01-23 10:15:18 -08007147 }
Reinette Chatre849e0dc2008-01-23 10:15:18 -08007148
Tomas Winkler978785a2008-12-19 10:37:31 +08007149 IWL_INFO(priv, "Detected Intel Wireless WiFi Link %s\n",
7150 priv->cfg->name);
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08007151
7152 /***********************************
7153 * 7. Initialize Module Parameters
7154 * **********************************/
7155
7156 /* Initialize module parameter values here */
7157 /* Disable radio (SW RF KILL) via parameter when loading driver */
Kolekar, Abhijeetdf878d82008-12-19 10:37:35 +08007158 if (iwl3945_mod_params.disable) {
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08007159 set_bit(STATUS_RF_KILL_SW, &priv->status);
7160 IWL_DEBUG_INFO("Radio disabled.\n");
7161 }
7162
7163
7164 /***********************
7165 * 8. Setup Services
7166 * ********************/
7167
7168 spin_lock_irqsave(&priv->lock, flags);
7169 iwl3945_disable_interrupts(priv);
7170 spin_unlock_irqrestore(&priv->lock, flags);
7171
Helmut Schaa26635162009-01-15 09:38:44 +01007172 pci_enable_msi(priv->pci_dev);
7173
7174 err = request_irq(priv->pci_dev->irq, iwl3945_isr, IRQF_SHARED,
7175 DRV_NAME, priv);
7176 if (err) {
7177 IWL_ERR(priv, "Error allocating IRQ %d\n", priv->pci_dev->irq);
7178 goto out_disable_msi;
7179 }
7180
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08007181 err = sysfs_create_group(&pdev->dev.kobj, &iwl3945_attribute_group);
7182 if (err) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08007183 IWL_ERR(priv, "failed to create sysfs device attributes\n");
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08007184 goto out_release_irq;
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08007185 }
7186
7187 iwl3945_set_rxon_channel(priv, IEEE80211_BAND_2GHZ, 6);
7188 iwl3945_setup_deferred_work(priv);
7189 iwl3945_setup_rx_handlers(priv);
7190
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08007191 /*********************************
Helmut Schaa26635162009-01-15 09:38:44 +01007192 * 9. Setup and Register mac80211
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08007193 * *******************************/
7194
Zhu Yi5a669262008-01-14 17:46:18 -08007195 err = ieee80211_register_hw(priv->hw);
7196 if (err) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08007197 IWL_ERR(priv, "Failed to register network device: %d\n", err);
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08007198 goto out_remove_sysfs;
Zhu Yib481de92007-09-25 17:54:57 -07007199 }
7200
Zhu Yi5a669262008-01-14 17:46:18 -08007201 priv->hw->conf.beacon_int = 100;
7202 priv->mac80211_registered = 1;
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08007203
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08007204 err = iwl3945_rfkill_init(priv);
7205 if (err)
Winkler, Tomas15b16872008-12-19 10:37:33 +08007206 IWL_ERR(priv, "Unable to initialize RFKILL system. "
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08007207 "Ignoring error: %d\n", err);
7208
Helmut Schaa26635162009-01-15 09:38:44 +01007209 /* Start monitoring the killswitch */
7210 queue_delayed_work(priv->workqueue, &priv->rfkill_poll,
7211 2 * HZ);
7212
Zhu Yib481de92007-09-25 17:54:57 -07007213 return 0;
7214
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08007215 out_remove_sysfs:
7216 sysfs_remove_group(&pdev->dev.kobj, &iwl3945_attribute_group);
Reinette Chatre849e0dc2008-01-23 10:15:18 -08007217 out_free_geos:
7218 iwl3945_free_geos(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007219
7220 out_release_irq:
Helmut Schaa26635162009-01-15 09:38:44 +01007221 free_irq(priv->pci_dev->irq, priv);
Zhu Yib481de92007-09-25 17:54:57 -07007222 destroy_workqueue(priv->workqueue);
7223 priv->workqueue = NULL;
Abhijeet Kolekar3832ec92008-12-19 10:37:26 +08007224 iwl3945_unset_hw_params(priv);
Helmut Schaa26635162009-01-15 09:38:44 +01007225 out_disable_msi:
7226 pci_disable_msi(priv->pci_dev);
Zhu Yib481de92007-09-25 17:54:57 -07007227 out_iounmap:
7228 pci_iounmap(pdev, priv->hw_base);
7229 out_pci_release_regions:
7230 pci_release_regions(pdev);
7231 out_pci_disable_device:
7232 pci_disable_device(pdev);
7233 pci_set_drvdata(pdev, NULL);
7234 out_ieee80211_free_hw:
7235 ieee80211_free_hw(priv->hw);
7236 out:
7237 return err;
7238}
7239
Reinette Chatrec83dbf62008-03-21 13:53:41 -07007240static void __devexit iwl3945_pci_remove(struct pci_dev *pdev)
Zhu Yib481de92007-09-25 17:54:57 -07007241{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08007242 struct iwl_priv *priv = pci_get_drvdata(pdev);
Mohamed Abbas0359fac2008-03-28 16:21:08 -07007243 unsigned long flags;
Zhu Yib481de92007-09-25 17:54:57 -07007244
7245 if (!priv)
7246 return;
7247
7248 IWL_DEBUG_INFO("*** UNLOAD DRIVER ***\n");
7249
Zhu Yib481de92007-09-25 17:54:57 -07007250 set_bit(STATUS_EXIT_PENDING, &priv->status);
Zhu Yib24d22b2007-12-19 13:59:52 +08007251
Kolekar, Abhijeetd552bfb2008-12-19 10:37:41 +08007252 if (priv->mac80211_registered) {
7253 ieee80211_unregister_hw(priv->hw);
7254 priv->mac80211_registered = 0;
7255 } else {
7256 iwl3945_down(priv);
7257 }
Zhu Yib481de92007-09-25 17:54:57 -07007258
Mohamed Abbas0359fac2008-03-28 16:21:08 -07007259 /* make sure we flush any pending irq or
7260 * tasklet for the driver
7261 */
7262 spin_lock_irqsave(&priv->lock, flags);
7263 iwl3945_disable_interrupts(priv);
7264 spin_unlock_irqrestore(&priv->lock, flags);
7265
7266 iwl_synchronize_irq(priv);
7267
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007268 sysfs_remove_group(&pdev->dev.kobj, &iwl3945_attribute_group);
Zhu Yib481de92007-09-25 17:54:57 -07007269
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08007270 iwl3945_rfkill_unregister(priv);
Helmut Schaa26635162009-01-15 09:38:44 +01007271 cancel_delayed_work(&priv->rfkill_poll);
7272
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007273 iwl3945_dealloc_ucode_pci(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007274
7275 if (priv->rxq.bd)
Winkler, Tomas51af3d32008-12-22 11:31:23 +08007276 iwl_rx_queue_free(priv, &priv->rxq);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007277 iwl3945_hw_txq_ctx_free(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007278
Abhijeet Kolekar3832ec92008-12-19 10:37:26 +08007279 iwl3945_unset_hw_params(priv);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007280 iwl3945_clear_stations_table(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007281
Mohamed Abbas6ef89d02007-10-25 17:15:47 +08007282 /*netif_stop_queue(dev); */
7283 flush_workqueue(priv->workqueue);
7284
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007285 /* ieee80211_unregister_hw calls iwl3945_mac_stop, which flushes
Zhu Yib481de92007-09-25 17:54:57 -07007286 * priv->workqueue... so we can't take down the workqueue
7287 * until now... */
7288 destroy_workqueue(priv->workqueue);
7289 priv->workqueue = NULL;
7290
Helmut Schaa26635162009-01-15 09:38:44 +01007291 free_irq(pdev->irq, priv);
7292 pci_disable_msi(pdev);
7293
Zhu Yib481de92007-09-25 17:54:57 -07007294 pci_iounmap(pdev, priv->hw_base);
7295 pci_release_regions(pdev);
7296 pci_disable_device(pdev);
7297 pci_set_drvdata(pdev, NULL);
7298
Reinette Chatre849e0dc2008-01-23 10:15:18 -08007299 iwl3945_free_channel_map(priv);
7300 iwl3945_free_geos(priv);
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08007301 kfree(priv->scan39);
Zhu Yib481de92007-09-25 17:54:57 -07007302 if (priv->ibss_beacon)
7303 dev_kfree_skb(priv->ibss_beacon);
7304
7305 ieee80211_free_hw(priv->hw);
7306}
7307
7308#ifdef CONFIG_PM
7309
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007310static int iwl3945_pci_suspend(struct pci_dev *pdev, pm_message_t state)
Zhu Yib481de92007-09-25 17:54:57 -07007311{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08007312 struct iwl_priv *priv = pci_get_drvdata(pdev);
Zhu Yib481de92007-09-25 17:54:57 -07007313
Zhu Yie655b9f2008-01-24 02:19:38 -08007314 if (priv->is_open) {
7315 set_bit(STATUS_IN_SUSPEND, &priv->status);
7316 iwl3945_mac_stop(priv->hw);
7317 priv->is_open = 1;
7318 }
Helmut Schaa26635162009-01-15 09:38:44 +01007319 pci_save_state(pdev);
7320 pci_disable_device(pdev);
Zhu Yib481de92007-09-25 17:54:57 -07007321 pci_set_power_state(pdev, PCI_D3hot);
7322
Zhu Yib481de92007-09-25 17:54:57 -07007323 return 0;
7324}
7325
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007326static int iwl3945_pci_resume(struct pci_dev *pdev)
Zhu Yib481de92007-09-25 17:54:57 -07007327{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08007328 struct iwl_priv *priv = pci_get_drvdata(pdev);
Zhu Yib481de92007-09-25 17:54:57 -07007329
Zhu Yib481de92007-09-25 17:54:57 -07007330 pci_set_power_state(pdev, PCI_D0);
Helmut Schaa26635162009-01-15 09:38:44 +01007331 pci_enable_device(pdev);
7332 pci_restore_state(pdev);
Zhu Yib481de92007-09-25 17:54:57 -07007333
Zhu Yie655b9f2008-01-24 02:19:38 -08007334 if (priv->is_open)
7335 iwl3945_mac_start(priv->hw);
Zhu Yib481de92007-09-25 17:54:57 -07007336
Zhu Yie655b9f2008-01-24 02:19:38 -08007337 clear_bit(STATUS_IN_SUSPEND, &priv->status);
Zhu Yib481de92007-09-25 17:54:57 -07007338 return 0;
7339}
7340
7341#endif /* CONFIG_PM */
7342
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08007343/*************** RFKILL FUNCTIONS **********/
Adel Gadllah80fcc9e2008-07-01 17:49:50 +02007344#ifdef CONFIG_IWL3945_RFKILL
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08007345/* software rf-kill from user */
7346static int iwl3945_rfkill_soft_rf_kill(void *data, enum rfkill_state state)
7347{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08007348 struct iwl_priv *priv = data;
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08007349 int err = 0;
7350
Adel Gadllah80fcc9e2008-07-01 17:49:50 +02007351 if (!priv->rfkill)
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08007352 return 0;
7353
7354 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
7355 return 0;
7356
Tomas Winklera96a27f2008-10-23 23:48:56 -07007357 IWL_DEBUG_RF_KILL("we received soft RFKILL set to state %d\n", state);
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08007358 mutex_lock(&priv->mutex);
7359
7360 switch (state) {
Zhu Yiacdfe9b2008-06-30 17:23:32 +08007361 case RFKILL_STATE_UNBLOCKED:
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08007362 if (iwl_is_rfkill_hw(priv)) {
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08007363 err = -EBUSY;
Adel Gadllah80fcc9e2008-07-01 17:49:50 +02007364 goto out_unlock;
7365 }
7366 iwl3945_radio_kill_sw(priv, 0);
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08007367 break;
Zhu Yiacdfe9b2008-06-30 17:23:32 +08007368 case RFKILL_STATE_SOFT_BLOCKED:
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08007369 iwl3945_radio_kill_sw(priv, 1);
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08007370 break;
Zhu Yiacdfe9b2008-06-30 17:23:32 +08007371 default:
Winkler, Tomas39aadf82008-12-19 10:37:32 +08007372 IWL_WARN(priv, "received unexpected RFKILL state %d\n", state);
Zhu Yiacdfe9b2008-06-30 17:23:32 +08007373 break;
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08007374 }
Adel Gadllah80fcc9e2008-07-01 17:49:50 +02007375out_unlock:
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08007376 mutex_unlock(&priv->mutex);
7377
7378 return err;
7379}
7380
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08007381int iwl3945_rfkill_init(struct iwl_priv *priv)
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08007382{
7383 struct device *device = wiphy_dev(priv->hw->wiphy);
7384 int ret = 0;
7385
7386 BUG_ON(device == NULL);
7387
7388 IWL_DEBUG_RF_KILL("Initializing RFKILL.\n");
Adel Gadllah80fcc9e2008-07-01 17:49:50 +02007389 priv->rfkill = rfkill_allocate(device, RFKILL_TYPE_WLAN);
7390 if (!priv->rfkill) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08007391 IWL_ERR(priv, "Unable to allocate rfkill device.\n");
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08007392 ret = -ENOMEM;
7393 goto error;
7394 }
7395
Adel Gadllah80fcc9e2008-07-01 17:49:50 +02007396 priv->rfkill->name = priv->cfg->name;
7397 priv->rfkill->data = priv;
7398 priv->rfkill->state = RFKILL_STATE_UNBLOCKED;
7399 priv->rfkill->toggle_radio = iwl3945_rfkill_soft_rf_kill;
7400 priv->rfkill->user_claim_unsupported = 1;
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08007401
Adel Gadllah80fcc9e2008-07-01 17:49:50 +02007402 priv->rfkill->dev.class->suspend = NULL;
7403 priv->rfkill->dev.class->resume = NULL;
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08007404
Adel Gadllah80fcc9e2008-07-01 17:49:50 +02007405 ret = rfkill_register(priv->rfkill);
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08007406 if (ret) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08007407 IWL_ERR(priv, "Unable to register rfkill: %d\n", ret);
Adel Gadllah80fcc9e2008-07-01 17:49:50 +02007408 goto freed_rfkill;
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08007409 }
7410
7411 IWL_DEBUG_RF_KILL("RFKILL initialization complete.\n");
7412 return ret;
7413
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08007414freed_rfkill:
Adel Gadllah80fcc9e2008-07-01 17:49:50 +02007415 if (priv->rfkill != NULL)
7416 rfkill_free(priv->rfkill);
7417 priv->rfkill = NULL;
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08007418
7419error:
7420 IWL_DEBUG_RF_KILL("RFKILL initialization complete.\n");
7421 return ret;
7422}
7423
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08007424void iwl3945_rfkill_unregister(struct iwl_priv *priv)
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08007425{
Adel Gadllah80fcc9e2008-07-01 17:49:50 +02007426 if (priv->rfkill)
7427 rfkill_unregister(priv->rfkill);
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08007428
Adel Gadllah80fcc9e2008-07-01 17:49:50 +02007429 priv->rfkill = NULL;
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08007430}
7431
7432/* set rf-kill to the right state. */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08007433void iwl3945_rfkill_set_hw_state(struct iwl_priv *priv)
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08007434{
7435
Adel Gadllah80fcc9e2008-07-01 17:49:50 +02007436 if (!priv->rfkill)
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08007437 return;
7438
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08007439 if (iwl_is_rfkill_hw(priv)) {
Adel Gadllah80fcc9e2008-07-01 17:49:50 +02007440 rfkill_force_state(priv->rfkill, RFKILL_STATE_HARD_BLOCKED);
7441 return;
7442 }
7443
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08007444 if (!iwl_is_rfkill_sw(priv))
Adel Gadllah80fcc9e2008-07-01 17:49:50 +02007445 rfkill_force_state(priv->rfkill, RFKILL_STATE_UNBLOCKED);
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08007446 else
Adel Gadllah80fcc9e2008-07-01 17:49:50 +02007447 rfkill_force_state(priv->rfkill, RFKILL_STATE_SOFT_BLOCKED);
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08007448}
7449#endif
7450
Zhu Yib481de92007-09-25 17:54:57 -07007451/*****************************************************************************
7452 *
7453 * driver and module entry point
7454 *
7455 *****************************************************************************/
7456
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007457static struct pci_driver iwl3945_driver = {
Zhu Yib481de92007-09-25 17:54:57 -07007458 .name = DRV_NAME,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007459 .id_table = iwl3945_hw_card_ids,
7460 .probe = iwl3945_pci_probe,
7461 .remove = __devexit_p(iwl3945_pci_remove),
Zhu Yib481de92007-09-25 17:54:57 -07007462#ifdef CONFIG_PM
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007463 .suspend = iwl3945_pci_suspend,
7464 .resume = iwl3945_pci_resume,
Zhu Yib481de92007-09-25 17:54:57 -07007465#endif
7466};
7467
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007468static int __init iwl3945_init(void)
Zhu Yib481de92007-09-25 17:54:57 -07007469{
7470
7471 int ret;
7472 printk(KERN_INFO DRV_NAME ": " DRV_DESCRIPTION ", " DRV_VERSION "\n");
7473 printk(KERN_INFO DRV_NAME ": " DRV_COPYRIGHT "\n");
Reinette Chatre897e1cf2008-03-28 16:21:09 -07007474
7475 ret = iwl3945_rate_control_register();
7476 if (ret) {
Samuel Ortiza3139c52008-12-19 10:37:09 +08007477 printk(KERN_ERR DRV_NAME
7478 "Unable to register rate control algorithm: %d\n", ret);
Reinette Chatre897e1cf2008-03-28 16:21:09 -07007479 return ret;
7480 }
7481
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007482 ret = pci_register_driver(&iwl3945_driver);
Zhu Yib481de92007-09-25 17:54:57 -07007483 if (ret) {
Samuel Ortiza3139c52008-12-19 10:37:09 +08007484 printk(KERN_ERR DRV_NAME "Unable to initialize PCI module\n");
Reinette Chatre897e1cf2008-03-28 16:21:09 -07007485 goto error_register;
Zhu Yib481de92007-09-25 17:54:57 -07007486 }
Zhu Yib481de92007-09-25 17:54:57 -07007487
7488 return ret;
Reinette Chatre897e1cf2008-03-28 16:21:09 -07007489
Reinette Chatre897e1cf2008-03-28 16:21:09 -07007490error_register:
7491 iwl3945_rate_control_unregister();
7492 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07007493}
7494
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007495static void __exit iwl3945_exit(void)
Zhu Yib481de92007-09-25 17:54:57 -07007496{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007497 pci_unregister_driver(&iwl3945_driver);
Reinette Chatre897e1cf2008-03-28 16:21:09 -07007498 iwl3945_rate_control_unregister();
Zhu Yib481de92007-09-25 17:54:57 -07007499}
7500
Reinette Chatrea0987a82008-12-02 12:14:06 -08007501MODULE_FIRMWARE(IWL3945_MODULE_FIRMWARE(IWL3945_UCODE_API_MAX));
Zhu Yi25cb6ca2008-09-11 11:45:22 +08007502
Kolekar, Abhijeetdf878d82008-12-19 10:37:35 +08007503module_param_named(antenna, iwl3945_mod_params.antenna, int, 0444);
Zhu Yib481de92007-09-25 17:54:57 -07007504MODULE_PARM_DESC(antenna, "select antenna (1=Main, 2=Aux, default 0 [both])");
Kolekar, Abhijeetdf878d82008-12-19 10:37:35 +08007505module_param_named(disable, iwl3945_mod_params.disable, int, 0444);
Zhu Yib481de92007-09-25 17:54:57 -07007506MODULE_PARM_DESC(disable, "manually disable the radio (default 0 [radio on])");
Samuel Ortiz9c74d9f2009-01-08 10:19:59 -08007507module_param_named(swcrypto, iwl3945_mod_params.sw_crypto, int, 0444);
7508MODULE_PARM_DESC(swcrypto,
7509 "using software crypto (default 1 [software])\n");
Kolekar, Abhijeetdf878d82008-12-19 10:37:35 +08007510module_param_named(debug, iwl3945_mod_params.debug, uint, 0444);
Zhu Yib481de92007-09-25 17:54:57 -07007511MODULE_PARM_DESC(debug, "debug output mask");
Kolekar, Abhijeetdf878d82008-12-19 10:37:35 +08007512module_param_named(disable_hw_scan, iwl3945_mod_params.disable_hw_scan, int, 0444);
Zhu Yib481de92007-09-25 17:54:57 -07007513MODULE_PARM_DESC(disable_hw_scan, "disable hardware scanning (default 0)");
7514
Kolekar, Abhijeetdf878d82008-12-19 10:37:35 +08007515module_param_named(queues_num, iwl3945_mod_params.num_of_queues, int, 0444);
Zhu Yib481de92007-09-25 17:54:57 -07007516MODULE_PARM_DESC(queues_num, "number of hw queues.");
7517
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007518module_exit(iwl3945_exit);
7519module_init(iwl3945_init);